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
;
29 static int do_attach(int idx
, int fd
, const char *name
)
31 struct bpf_prog_info info
= {};
32 __u32 info_len
= sizeof(info
);
35 err
= bpf_set_link_xdp_fd(idx
, fd
, xdp_flags
);
37 printf("ERROR: failed to attach program to %s\n", name
);
41 err
= bpf_obj_get_info_by_fd(fd
, &info
, &info_len
);
43 printf("can't get prog info - %s\n", strerror(errno
));
51 static int do_detach(int idx
, const char *name
)
53 __u32 curr_prog_id
= 0;
56 err
= bpf_get_link_xdp_id(idx
, &curr_prog_id
, 0);
58 printf("bpf_get_link_xdp_id failed\n");
61 if (prog_id
== curr_prog_id
) {
62 err
= bpf_set_link_xdp_fd(idx
, -1, 0);
64 printf("ERROR: failed to detach prog from %s\n", name
);
65 } else if (!curr_prog_id
) {
66 printf("couldn't find a prog id on a %s\n", name
);
68 printf("program on interface changed, not removing\n");
74 #define SAMPLE_SIZE 64
76 static int print_bpf_output(void *data
, int size
)
81 __u8 pkt_data
[SAMPLE_SIZE
];
85 if (e
->cookie
!= 0xdead) {
86 printf("BUG cookie %x sized %d\n",
88 return LIBBPF_PERF_EVENT_ERROR
;
91 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e
->pkt_len
);
92 for (i
= 0; i
< 14 && i
< e
->pkt_len
; i
++)
93 printf("%02x ", e
->pkt_data
[i
]);
96 return LIBBPF_PERF_EVENT_CONT
;
99 static void test_bpf_perf_event(int map_fd
, int num
)
101 struct perf_event_attr attr
= {
102 .sample_type
= PERF_SAMPLE_RAW
,
103 .type
= PERF_TYPE_SOFTWARE
,
104 .config
= PERF_COUNT_SW_BPF_OUTPUT
,
105 .wakeup_events
= 1, /* get an fd notification for every event */
109 for (i
= 0; i
< num
; i
++) {
112 pmu_fds
[i
] = sys_perf_event_open(&attr
, -1/*pid*/, i
/*cpu*/,
115 assert(pmu_fds
[i
] >= 0);
116 assert(bpf_map_update_elem(map_fd
, &key
,
117 &pmu_fds
[i
], BPF_ANY
) == 0);
118 ioctl(pmu_fds
[i
], PERF_EVENT_IOC_ENABLE
, 0);
122 static void sig_handler(int signo
)
124 do_detach(if_idx
, if_name
);
128 static void usage(const char *prog
)
131 "%s: %s [OPTS] <ifname|ifindex>\n\n"
133 " -F force loading prog\n",
137 int main(int argc
, char **argv
)
139 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
140 struct bpf_prog_load_attr prog_load_attr
= {
141 .prog_type
= BPF_PROG_TYPE_XDP
,
143 const char *optstr
= "F";
144 int prog_fd
, map_fd
, opt
;
145 struct bpf_object
*obj
;
151 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
154 xdp_flags
&= ~XDP_FLAGS_UPDATE_IF_NOEXIST
;
157 usage(basename(argv
[0]));
162 if (optind
== argc
) {
163 usage(basename(argv
[0]));
167 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
168 perror("setrlimit(RLIMIT_MEMLOCK)");
172 numcpus
= get_nprocs();
173 if (numcpus
> MAX_CPUS
)
176 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
177 prog_load_attr
.file
= filename
;
179 if (bpf_prog_load_xattr(&prog_load_attr
, &obj
, &prog_fd
))
183 printf("load_bpf_file: %s\n", strerror(errno
));
187 map
= bpf_map__next(NULL
, obj
);
189 printf("finding a map in obj file failed\n");
192 map_fd
= bpf_map__fd(map
);
194 if_idx
= if_nametoindex(argv
[optind
]);
196 if_idx
= strtoul(argv
[optind
], NULL
, 0);
199 fprintf(stderr
, "Invalid ifname\n");
202 if_name
= argv
[optind
];
203 err
= do_attach(if_idx
, prog_fd
, if_name
);
207 if (signal(SIGINT
, sig_handler
) ||
208 signal(SIGHUP
, sig_handler
) ||
209 signal(SIGTERM
, sig_handler
)) {
214 test_bpf_perf_event(map_fd
, numcpus
);
216 for (i
= 0; i
< numcpus
; i
++)
217 if (perf_event_mmap_header(pmu_fds
[i
], &headers
[i
]) < 0)
220 ret
= perf_event_poller_multi(pmu_fds
, headers
, numcpus
,