Skip to content

Commit

Permalink
Support detaching xdp_link-based attachments fix #361
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Sep 20, 2023
1 parent 9aba9e9 commit e1d2153
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion lib/libxdp/libxdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3223,6 +3223,42 @@ static int xdp_multiprog__unpin(struct xdp_multiprog *mp)
return err;
}

static void xdp_detach_links(uint32_t ifindex) {
struct bpf_link_info link_info;
u_int32_t link_info_len = sizeof(link_info), id;
int err, fd;

while (true) {
err = bpf_link_get_next_id(id, &id);
if (err) {
if (errno == ENOENT)
break;
pr_debug("can't get next link: %s (id %d)", strerror(errno), id);
break;
}

fd = bpf_link_get_fd_by_id(id);
if (fd < 0) {
pr_debug("can't get link by id (%u): %s", id, strerror(errno));
continue;
}
memset(&link_info, 0, sizeof(link_info));
err = bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
if (err) {
pr_debug("can't get link info for %u: %s", id, strerror(errno));
continue;
}
if (link_info.type == BPF_LINK_TYPE_XDP && link_info.xdp.ifindex == ifindex) {
err = bpf_link_detach(fd);
if (err) {
pr_debug("can't detach link %u: %s", id, strerror(errno));
}
close(fd);
break;
}
}
}

static int xdp_multiprog__attach(struct xdp_multiprog *old_mp,
struct xdp_multiprog *mp,
enum xdp_attach_mode mode)
Expand Down Expand Up @@ -3253,8 +3289,15 @@ static int xdp_multiprog__attach(struct xdp_multiprog *old_mp,


err = xdp_attach_fd(prog_fd, old_fd, ifindex, mode);
if (err < 0)
if (err < 0) {
if (!mp) {
pr_debug("Detaching links on ifindex %d%s\n", ifindex,
mode == XDP_MODE_SKB? " in skb mode":"");
xdp_detach_links(ifindex);
return 0;
}
goto err;
}

if (mp)
pr_debug("Loaded %zu programs on ifindex %d%s\n",
Expand Down

0 comments on commit e1d2153

Please sign in to comment.