__u8 attach_mode;
 };
 
+struct bpf_xdp_set_link_opts {
+       size_t sz;
+       __u32 old_fd;
+};
+#define bpf_xdp_set_link_opts__last_field old_fd
+
 LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
+LIBBPF_API int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
+                                       const struct bpf_xdp_set_link_opts *opts);
 LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
 LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
                                     size_t info_size, __u32 flags);
 
        return ret;
 }
 
-int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
+static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
+                                        __u32 flags)
 {
        int sock, seq = 0, ret;
        struct nlattr *nla, *nla_xdp;
                nla->nla_len += nla_xdp->nla_len;
        }
 
+       if (flags & XDP_FLAGS_REPLACE) {
+               nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
+               nla_xdp->nla_type = IFLA_XDP_EXPECTED_FD;
+               nla_xdp->nla_len = NLA_HDRLEN + sizeof(old_fd);
+               memcpy((char *)nla_xdp + NLA_HDRLEN, &old_fd, sizeof(old_fd));
+               nla->nla_len += nla_xdp->nla_len;
+       }
+
        req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
 
        if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
        return ret;
 }
 
+int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
+                            const struct bpf_xdp_set_link_opts *opts)
+{
+       int old_fd = -1;
+
+       if (!OPTS_VALID(opts, bpf_xdp_set_link_opts))
+               return -EINVAL;
+
+       if (OPTS_HAS(opts, old_fd)) {
+               old_fd = OPTS_GET(opts, old_fd, -1);
+               flags |= XDP_FLAGS_REPLACE;
+       }
+
+       return __bpf_set_link_xdp_fd_replace(ifindex, fd,
+                                            old_fd,
+                                            flags);
+}
+
+int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
+{
+       return __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags);
+}
+
 static int __dump_link_nlmsg(struct nlmsghdr *nlh,
                             libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
 {