1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/perf_event.h>
10 #include <sys/sysinfo.h>
11 #include <sys/ioctl.h>
15 #include <sys/resource.h>
17 #include <linux/if_link.h>
24 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
26 static struct perf_buffer *pb = NULL;
28 static int do_attach(int idx, int fd, const char *name)
30 struct bpf_prog_info info = {};
31 __u32 info_len = sizeof(info);
34 err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
36 printf("ERROR: failed to attach program to %s\n", name);
40 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
42 printf("can't get prog info - %s\n", strerror(errno));
50 static int do_detach(int idx, const char *name)
52 __u32 curr_prog_id = 0;
55 err = bpf_get_link_xdp_id(idx, &curr_prog_id, 0);
57 printf("bpf_get_link_xdp_id failed\n");
60 if (prog_id == curr_prog_id) {
61 err = bpf_set_link_xdp_fd(idx, -1, 0);
63 printf("ERROR: failed to detach prog from %s\n", name);
64 } else if (!curr_prog_id) {
65 printf("couldn't find a prog id on a %s\n", name);
67 printf("program on interface changed, not removing\n");
73 #define SAMPLE_SIZE 64
75 static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
80 __u8 pkt_data[SAMPLE_SIZE];
84 if (e->cookie != 0xdead) {
85 printf("BUG cookie %x sized %d\n", e->cookie, size);
89 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
90 for (i = 0; i < 14 && i < e->pkt_len; i++)
91 printf("%02x ", e->pkt_data[i]);
95 static void sig_handler(int signo)
97 do_detach(if_idx, if_name);
98 perf_buffer__free(pb);
102 static void usage(const char *prog)
105 "%s: %s [OPTS] <ifname|ifindex>\n\n"
107 " -F force loading prog\n",
111 int main(int argc, char **argv)
113 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
114 struct bpf_prog_load_attr prog_load_attr = {
115 .prog_type = BPF_PROG_TYPE_XDP,
117 struct perf_buffer_opts pb_opts = {};
118 const char *optstr = "F";
119 int prog_fd, map_fd, opt;
120 struct bpf_object *obj;
125 while ((opt = getopt(argc, argv, optstr)) != -1) {
128 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
131 usage(basename(argv[0]));
136 if (optind == argc) {
137 usage(basename(argv[0]));
141 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
142 perror("setrlimit(RLIMIT_MEMLOCK)");
146 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
147 prog_load_attr.file = filename;
149 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
153 printf("load_bpf_file: %s\n", strerror(errno));
157 map = bpf_map__next(NULL, obj);
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_opts.sample_cb = print_bpf_output;
185 pb = perf_buffer__new(map_fd, 8, &pb_opts);
186 err = libbpf_get_error(pb);
188 perror("perf_buffer setup failed");
192 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {