1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/perf_event.h>
10 #include <sys/sysinfo.h>
11 #include <sys/ioctl.h>
13 #include <bpf/libbpf.h>
16 #include <linux/if_link.h>
22 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
24 static struct perf_buffer *pb = NULL;
26 static int do_attach(int idx, int fd, const char *name)
28 struct bpf_prog_info info = {};
29 __u32 info_len = sizeof(info);
32 err = bpf_xdp_attach(idx, fd, xdp_flags, NULL);
34 printf("ERROR: failed to attach program to %s\n", name);
38 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
40 printf("can't get prog info - %s\n", strerror(errno));
48 static int do_detach(int idx, const char *name)
50 __u32 curr_prog_id = 0;
53 err = bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id);
55 printf("bpf_xdp_query_id failed\n");
58 if (prog_id == curr_prog_id) {
59 err = bpf_xdp_detach(idx, xdp_flags, NULL);
61 printf("ERROR: failed to detach prog from %s\n", name);
62 } else if (!curr_prog_id) {
63 printf("couldn't find a prog id on a %s\n", name);
65 printf("program on interface changed, not removing\n");
71 #define SAMPLE_SIZE 64
73 static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
78 __u8 pkt_data[SAMPLE_SIZE];
82 if (e->cookie != 0xdead) {
83 printf("BUG cookie %x sized %d\n", e->cookie, size);
87 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
88 for (i = 0; i < 14 && i < e->pkt_len; i++)
89 printf("%02x ", e->pkt_data[i]);
93 static void sig_handler(int signo)
95 do_detach(if_idx, if_name);
96 perf_buffer__free(pb);
100 static void usage(const char *prog)
103 "%s: %s [OPTS] <ifname|ifindex>\n\n"
105 " -F force loading prog\n"
106 " -S use skb-mode\n",
110 int main(int argc, char **argv)
112 const char *optstr = "FS";
113 int prog_fd, map_fd, opt;
114 struct bpf_program *prog;
115 struct bpf_object *obj;
120 while ((opt = getopt(argc, argv, optstr)) != -1) {
123 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
126 xdp_flags |= XDP_FLAGS_SKB_MODE;
129 usage(basename(argv[0]));
134 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
135 xdp_flags |= XDP_FLAGS_DRV_MODE;
137 if (optind == argc) {
138 usage(basename(argv[0]));
142 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
144 obj = bpf_object__open_file(filename, NULL);
145 if (libbpf_get_error(obj))
148 prog = bpf_object__next_program(obj, NULL);
149 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
151 err = bpf_object__load(obj);
155 prog_fd = bpf_program__fd(prog);
157 map = bpf_object__next_map(obj, NULL);
159 printf("finding a map in obj file failed\n");
162 map_fd = bpf_map__fd(map);
164 if_idx = if_nametoindex(argv[optind]);
166 if_idx = strtoul(argv[optind], NULL, 0);
169 fprintf(stderr, "Invalid ifname\n");
172 if_name = argv[optind];
173 err = do_attach(if_idx, prog_fd, if_name);
177 if (signal(SIGINT, sig_handler) ||
178 signal(SIGHUP, sig_handler) ||
179 signal(SIGTERM, sig_handler)) {
184 pb = perf_buffer__new(map_fd, 8, print_bpf_output, NULL, NULL, NULL);
185 err = libbpf_get_error(pb);
187 perror("perf_buffer setup failed");
191 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {