Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <linux/perf_event.h>
6#include <linux/bpf.h>
7#include <net/if.h>
8#include <errno.h>
9#include <assert.h>
10#include <sys/sysinfo.h>
11#include <sys/ioctl.h>
12#include <signal.h>
13#include <libbpf.h>
14#include <bpf/bpf.h>
15#include <sys/resource.h>
16#include <libgen.h>
17#include <linux/if_link.h>
18
19#include "perf-sys.h"
20#include "trace_helpers.h"
21
22#define MAX_CPUS 128
23static int pmu_fds[MAX_CPUS], if_idx;
24static struct perf_event_mmap_page *headers[MAX_CPUS];
25static char *if_name;
26static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
27static __u32 prog_id;
28
29static int do_attach(int idx, int fd, const char *name)
30{
31 struct bpf_prog_info info = {};
32 __u32 info_len = sizeof(info);
33 int err;
34
35 err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
36 if (err < 0) {
37 printf("ERROR: failed to attach program to %s\n", name);
38 return err;
39 }
40
41 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
42 if (err) {
43 printf("can't get prog info - %s\n", strerror(errno));
44 return err;
45 }
46 prog_id = info.id;
47
48 return err;
49}
50
51static int do_detach(int idx, const char *name)
52{
53 __u32 curr_prog_id = 0;
54 int err = 0;
55
56 err = bpf_get_link_xdp_id(idx, &curr_prog_id, 0);
57 if (err) {
58 printf("bpf_get_link_xdp_id failed\n");
59 return err;
60 }
61 if (prog_id == curr_prog_id) {
62 err = bpf_set_link_xdp_fd(idx, -1, 0);
63 if (err < 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);
67 } else {
68 printf("program on interface changed, not removing\n");
69 }
70
71 return err;
72}
73
74#define SAMPLE_SIZE 64
75
76static int print_bpf_output(void *data, int size)
77{
78 struct {
79 __u16 cookie;
80 __u16 pkt_len;
81 __u8 pkt_data[SAMPLE_SIZE];
82 } __packed *e = data;
83 int i;
84
85 if (e->cookie != 0xdead) {
86 printf("BUG cookie %x sized %d\n",
87 e->cookie, size);
88 return LIBBPF_PERF_EVENT_ERROR;
89 }
90
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]);
94 printf("\n");
95
96 return LIBBPF_PERF_EVENT_CONT;
97}
98
99static void test_bpf_perf_event(int map_fd, int num)
100{
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 */
106 };
107 int i;
108
109 for (i = 0; i < num; i++) {
110 int key = i;
111
112 pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
113 -1/*group_fd*/, 0);
114
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);
119 }
120}
121
122static void sig_handler(int signo)
123{
124 do_detach(if_idx, if_name);
125 exit(0);
126}
127
128static void usage(const char *prog)
129{
130 fprintf(stderr,
131 "%s: %s [OPTS] <ifname|ifindex>\n\n"
132 "OPTS:\n"
133 " -F force loading prog\n",
134 __func__, prog);
135}
136
137int main(int argc, char **argv)
138{
139 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
140 struct bpf_prog_load_attr prog_load_attr = {
141 .prog_type = BPF_PROG_TYPE_XDP,
142 };
143 const char *optstr = "F";
144 int prog_fd, map_fd, opt;
145 struct bpf_object *obj;
146 struct bpf_map *map;
147 char filename[256];
148 int ret, err, i;
149 int numcpus;
150
151 while ((opt = getopt(argc, argv, optstr)) != -1) {
152 switch (opt) {
153 case 'F':
154 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
155 break;
156 default:
157 usage(basename(argv[0]));
158 return 1;
159 }
160 }
161
162 if (optind == argc) {
163 usage(basename(argv[0]));
164 return 1;
165 }
166
167 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
168 perror("setrlimit(RLIMIT_MEMLOCK)");
169 return 1;
170 }
171
172 numcpus = get_nprocs();
173 if (numcpus > MAX_CPUS)
174 numcpus = MAX_CPUS;
175
176 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
177 prog_load_attr.file = filename;
178
179 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
180 return 1;
181
182 if (!prog_fd) {
183 printf("load_bpf_file: %s\n", strerror(errno));
184 return 1;
185 }
186
187 map = bpf_map__next(NULL, obj);
188 if (!map) {
189 printf("finding a map in obj file failed\n");
190 return 1;
191 }
192 map_fd = bpf_map__fd(map);
193
194 if_idx = if_nametoindex(argv[optind]);
195 if (!if_idx)
196 if_idx = strtoul(argv[optind], NULL, 0);
197
198 if (!if_idx) {
199 fprintf(stderr, "Invalid ifname\n");
200 return 1;
201 }
202 if_name = argv[optind];
203 err = do_attach(if_idx, prog_fd, if_name);
204 if (err)
205 return err;
206
207 if (signal(SIGINT, sig_handler) ||
208 signal(SIGHUP, sig_handler) ||
209 signal(SIGTERM, sig_handler)) {
210 perror("signal");
211 return 1;
212 }
213
214 test_bpf_perf_event(map_fd, numcpus);
215
216 for (i = 0; i < numcpus; i++)
217 if (perf_event_mmap_header(pmu_fds[i], &headers[i]) < 0)
218 return 1;
219
220 ret = perf_event_poller_multi(pmu_fds, headers, numcpus,
221 print_bpf_output);
222 kill(0, SIGINT);
223 return ret;
224}