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>
15 #include <sys/resource.h>
17 #include <linux/if_link.h>
23 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
25 static struct perf_buffer *pb = NULL;
27 static int do_attach(int idx, int fd, const char *name)
29 struct bpf_prog_info info = {};
30 __u32 info_len = sizeof(info);
33 err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
35 printf("ERROR: failed to attach program to %s\n", name);
39 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
41 printf("can't get prog info - %s\n", strerror(errno));
49 static int do_detach(int idx, const char *name)
51 __u32 curr_prog_id = 0;
54 err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
56 printf("bpf_get_link_xdp_id failed\n");
59 if (prog_id == curr_prog_id) {
60 err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
62 printf("ERROR: failed to detach prog from %s\n", name);
63 } else if (!curr_prog_id) {
64 printf("couldn't find a prog id on a %s\n", name);
66 printf("program on interface changed, not removing\n");
72 #define SAMPLE_SIZE 64
74 static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
79 __u8 pkt_data[SAMPLE_SIZE];
83 if (e->cookie != 0xdead) {
84 printf("BUG cookie %x sized %d\n", e->cookie, size);
88 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
89 for (i = 0; i < 14 && i < e->pkt_len; i++)
90 printf("%02x ", e->pkt_data[i]);
94 static void sig_handler(int signo)
96 do_detach(if_idx, if_name);
97 perf_buffer__free(pb);
101 static void usage(const char *prog)
104 "%s: %s [OPTS] <ifname|ifindex>\n\n"
106 " -F force loading prog\n",
110 int main(int argc, char **argv)
112 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
113 struct bpf_prog_load_attr prog_load_attr = {
114 .prog_type = BPF_PROG_TYPE_XDP,
116 struct perf_buffer_opts pb_opts = {};
117 const char *optstr = "FS";
118 int prog_fd, map_fd, opt;
119 struct bpf_object *obj;
124 while ((opt = getopt(argc, argv, optstr)) != -1) {
127 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
130 xdp_flags |= XDP_FLAGS_SKB_MODE;
133 usage(basename(argv[0]));
138 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
139 xdp_flags |= XDP_FLAGS_DRV_MODE;
141 if (optind == argc) {
142 usage(basename(argv[0]));
146 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
147 perror("setrlimit(RLIMIT_MEMLOCK)");
151 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
152 prog_load_attr.file = filename;
154 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
158 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
162 map = bpf_map__next(NULL, obj);
164 printf("finding a map in obj file failed\n");
167 map_fd = bpf_map__fd(map);
169 if_idx = if_nametoindex(argv[optind]);
171 if_idx = strtoul(argv[optind], NULL, 0);
174 fprintf(stderr, "Invalid ifname\n");
177 if_name = argv[optind];
178 err = do_attach(if_idx, prog_fd, if_name);
182 if (signal(SIGINT, sig_handler) ||
183 signal(SIGHUP, sig_handler) ||
184 signal(SIGTERM, sig_handler)) {
189 pb_opts.sample_cb = print_bpf_output;
190 pb = perf_buffer__new(map_fd, 8, &pb_opts);
191 err = libbpf_get_error(pb);
193 perror("perf_buffer setup failed");
197 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {