$ ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:01:02:03:04:05 brd ff:ff:ff:ff:ff:ff
NOTE:
Linux 不允许同一网卡同时附着多个 XDP 程序,会返回 -EBUSY 忙错误。可使用 bpftool net detach xdp dev eth0 命令将附着在 eth0 的程序 detach。
intmain(int argc, char ** argv) { structxdppass_bpf *skel =NULL; int err; int ifindex = 0;
/* Set up libbpf errors and debug info callback */ libbpf_set_print(libbpf_print_fn);
if(argc > 1) ifindex = atoi(argv[1]);
/* Open load and verify BPF application */ skel = xdppass_bpf__open_and_load(); if (!skel) { fprintf(stderr, "Failed to open BPF skeleton\n"); return1; } skel->links.xdp_pass = bpf_program__attach_xdp(skel->progs.xdp_pass, ifindex); if (!skel->links.xdp_pass) { err = -errno; fprintf(stderr, "Failed to attach XDP program to ifindex %d\n", ifindex); goto cleanup; }
err = xdppass_bpf__attach(skel); if (err) { fprintf(stderr, "Failed to auto-attach BPF skeleton: %d\n", err); goto cleanup; }
if (signal(SIGINT, sig_int) == SIG_ERR) { fprintf(stderr, "can't set signal handler: %s\n", strerror(errno)); goto cleanup; }
printf("Successfully started! Please run `sudo cat /sys/kernel/debug/tracing/trace_pipe` " "to see output of the BPF programs.\n");