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>
20 #include "trace_helpers.h"
23 static int pmu_fds[MAX_CPUS], if_idx;
24 static struct perf_event_mmap_page *headers[MAX_CPUS];
26 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
28 static int do_attach(int idx, int fd, const char *name)
32 err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
34 printf("ERROR: failed to attach program to %s\n", name);
39 static int do_detach(int idx, const char *name)
43 err = bpf_set_link_xdp_fd(idx, -1, 0);
45 printf("ERROR: failed to detach program from %s\n", name);
50 #define SAMPLE_SIZE 64
52 static int print_bpf_output(void *data, int size)
57 __u8 pkt_data[SAMPLE_SIZE];
61 if (e->cookie != 0xdead) {
62 printf("BUG cookie %x sized %d\n",
64 return LIBBPF_PERF_EVENT_ERROR;
67 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
68 for (i = 0; i < 14 && i < e->pkt_len; i++)
69 printf("%02x ", e->pkt_data[i]);
72 return LIBBPF_PERF_EVENT_CONT;
75 static void test_bpf_perf_event(int map_fd, int num)
77 struct perf_event_attr attr = {
78 .sample_type = PERF_SAMPLE_RAW,
79 .type = PERF_TYPE_SOFTWARE,
80 .config = PERF_COUNT_SW_BPF_OUTPUT,
81 .wakeup_events = 1, /* get an fd notification for every event */
85 for (i = 0; i < num; i++) {
88 pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
91 assert(pmu_fds[i] >= 0);
92 assert(bpf_map_update_elem(map_fd, &key,
93 &pmu_fds[i], BPF_ANY) == 0);
94 ioctl(pmu_fds[i], PERF_EVENT_IOC_ENABLE, 0);
98 static void sig_handler(int signo)
100 do_detach(if_idx, if_name);
104 static void usage(const char *prog)
107 "%s: %s [OPTS] <ifname|ifindex>\n\n"
109 " -F force loading prog\n",
113 int main(int argc, char **argv)
115 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
116 struct bpf_prog_load_attr prog_load_attr = {
117 .prog_type = BPF_PROG_TYPE_XDP,
119 const char *optstr = "F";
120 int prog_fd, map_fd, opt;
121 struct bpf_object *obj;
127 while ((opt = getopt(argc, argv, optstr)) != -1) {
130 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
133 usage(basename(argv[0]));
138 if (optind == argc) {
139 usage(basename(argv[0]));
143 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
144 perror("setrlimit(RLIMIT_MEMLOCK)");
148 numcpus = get_nprocs();
149 if (numcpus > MAX_CPUS)
152 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
153 prog_load_attr.file = filename;
155 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
159 printf("load_bpf_file: %s\n", strerror(errno));
163 map = bpf_map__next(NULL, obj);
165 printf("finding a map in obj file failed\n");
168 map_fd = bpf_map__fd(map);
170 if_idx = if_nametoindex(argv[optind]);
172 if_idx = strtoul(argv[optind], NULL, 0);
175 fprintf(stderr, "Invalid ifname\n");
178 if_name = argv[optind];
179 err = do_attach(if_idx, prog_fd, if_name);
183 if (signal(SIGINT, sig_handler) ||
184 signal(SIGHUP, sig_handler) ||
185 signal(SIGTERM, sig_handler)) {
190 test_bpf_perf_event(map_fd, numcpus);
192 for (i = 0; i < numcpus; i++)
193 if (perf_event_mmap_header(pmu_fds[i], &headers[i]) < 0)
196 ret = perf_event_poller_multi(pmu_fds, headers, numcpus,