Skip to content

Commit 2620e92

Browse files
Wang Haiborkmann
authored andcommitted
bpf, samples: Fix xdpsock with '-M' parameter missing unload process
Execute the following command and exit, then execute it again, the following error will be reported: $ sudo ./samples/bpf/xdpsock -i ens4f2 -M ^C $ sudo ./samples/bpf/xdpsock -i ens4f2 -M libbpf: elf: skipping unrecognized data section(16) .eh_frame libbpf: elf: skipping relo section(17) .rel.eh_frame for section(16) .eh_frame libbpf: Kernel error message: XDP program already attached ERROR: link set xdp fd failed Commit c9d27c9 ("samples: bpf: Do not unload prog within xdpsock") removed the unloading prog code because of the presence of bpf_link. This is fine if XDP_SHARED_UMEM is disabled, but if it is enabled, unloading the prog is still needed. Fixes: c9d27c9 ("samples: bpf: Do not unload prog within xdpsock") Signed-off-by: Wang Hai <wanghai38@huawei.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Magnus Karlsson <magnus.karlsson@intel.com> Cc: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Link: https://lore.kernel.org/bpf/20210628091815.2373487-1-wanghai38@huawei.com
1 parent 5a0ae98 commit 2620e92

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎samples/bpf/xdpsock_user.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
9696
static int opt_timeout = 1000;
9797
static bool opt_need_wakeup = true;
9898
static u32 opt_num_xsks = 1;
99+
static u32 prog_id;
99100
static bool opt_busy_poll;
100101
static bool opt_reduced_cap;
101102

@@ -461,6 +462,23 @@ static void *poller(void *arg)
461462
return NULL;
462463
}
463464

465+
static void remove_xdp_program(void)
466+
{
467+
u32 curr_prog_id = 0;
468+
469+
if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
470+
printf("bpf_get_link_xdp_id failed\n");
471+
exit(EXIT_FAILURE);
472+
}
473+
474+
if (prog_id == curr_prog_id)
475+
bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
476+
else if (!curr_prog_id)
477+
printf("couldn't find a prog id on a given interface\n");
478+
else
479+
printf("program on interface changed, not removing\n");
480+
}
481+
464482
static void int_exit(int sig)
465483
{
466484
benchmark_done = true;
@@ -471,6 +489,9 @@ static void __exit_with_error(int error, const char *file, const char *func,
471489
{
472490
fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
473491
line, error, strerror(error));
492+
493+
if (opt_num_xsks > 1)
494+
remove_xdp_program();
474495
exit(EXIT_FAILURE);
475496
}
476497

@@ -490,6 +511,9 @@ static void xdpsock_cleanup(void)
490511
if (write(sock, &cmd, sizeof(int)) < 0)
491512
exit_with_error(errno);
492513
}
514+
515+
if (opt_num_xsks > 1)
516+
remove_xdp_program();
493517
}
494518

495519
static void swap_mac_addresses(void *data)
@@ -857,6 +881,10 @@ static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
857881
if (ret)
858882
exit_with_error(-ret);
859883

884+
ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
885+
if (ret)
886+
exit_with_error(-ret);
887+
860888
xsk->app_stats.rx_empty_polls = 0;
861889
xsk->app_stats.fill_fail_polls = 0;
862890
xsk->app_stats.copy_tx_sendtos = 0;

0 commit comments

Comments
 (0)