Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 c9d27c9e8dc7 ("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: c9d27c9e8dc7 ("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

authored by

Wang Hai and committed by
Daniel Borkmann
2620e92a 5a0ae987

+28
+28
samples/bpf/xdpsock_user.c
··· 96 96 static int opt_timeout = 1000; 97 97 static bool opt_need_wakeup = true; 98 98 static u32 opt_num_xsks = 1; 99 + static u32 prog_id; 99 100 static bool opt_busy_poll; 100 101 static bool opt_reduced_cap; 101 102 ··· 462 461 return NULL; 463 462 } 464 463 464 + static void remove_xdp_program(void) 465 + { 466 + u32 curr_prog_id = 0; 467 + 468 + if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) { 469 + printf("bpf_get_link_xdp_id failed\n"); 470 + exit(EXIT_FAILURE); 471 + } 472 + 473 + if (prog_id == curr_prog_id) 474 + bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags); 475 + else if (!curr_prog_id) 476 + printf("couldn't find a prog id on a given interface\n"); 477 + else 478 + printf("program on interface changed, not removing\n"); 479 + } 480 + 465 481 static void int_exit(int sig) 466 482 { 467 483 benchmark_done = true; ··· 489 471 { 490 472 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func, 491 473 line, error, strerror(error)); 474 + 475 + if (opt_num_xsks > 1) 476 + remove_xdp_program(); 492 477 exit(EXIT_FAILURE); 493 478 } 494 479 ··· 511 490 if (write(sock, &cmd, sizeof(int)) < 0) 512 491 exit_with_error(errno); 513 492 } 493 + 494 + if (opt_num_xsks > 1) 495 + remove_xdp_program(); 514 496 } 515 497 516 498 static void swap_mac_addresses(void *data) ··· 878 854 txr = tx ? &xsk->tx : NULL; 879 855 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem, 880 856 rxr, txr, &cfg); 857 + if (ret) 858 + exit_with_error(-ret); 859 + 860 + ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags); 881 861 if (ret) 882 862 exit_with_error(-ret); 883 863