]> asedeno.scripts.mit.edu Git - linux.git/blob - samples/bpf/xdp_sample_pkts_user.c
62f34827c7756a072f067faed097dbe791dc3068
[linux.git] / samples / bpf / xdp_sample_pkts_user.c
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
23 static int pmu_fds[MAX_CPUS], if_idx;
24 static struct perf_event_mmap_page *headers[MAX_CPUS];
25 static char *if_name;
26 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
27
28 static int do_attach(int idx, int fd, const char *name)
29 {
30         int err;
31
32         err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
33         if (err < 0)
34                 printf("ERROR: failed to attach program to %s\n", name);
35
36         return err;
37 }
38
39 static int do_detach(int idx, const char *name)
40 {
41         int err;
42
43         err = bpf_set_link_xdp_fd(idx, -1, 0);
44         if (err < 0)
45                 printf("ERROR: failed to detach program from %s\n", name);
46
47         return err;
48 }
49
50 #define SAMPLE_SIZE 64
51
52 static int print_bpf_output(void *data, int size)
53 {
54         struct {
55                 __u16 cookie;
56                 __u16 pkt_len;
57                 __u8  pkt_data[SAMPLE_SIZE];
58         } __packed *e = data;
59         int i;
60
61         if (e->cookie != 0xdead) {
62                 printf("BUG cookie %x sized %d\n",
63                        e->cookie, size);
64                 return LIBBPF_PERF_EVENT_ERROR;
65         }
66
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]);
70         printf("\n");
71
72         return LIBBPF_PERF_EVENT_CONT;
73 }
74
75 static void test_bpf_perf_event(int map_fd, int num)
76 {
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 */
82         };
83         int i;
84
85         for (i = 0; i < num; i++) {
86                 int key = i;
87
88                 pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
89                                                  -1/*group_fd*/, 0);
90
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);
95         }
96 }
97
98 static void sig_handler(int signo)
99 {
100         do_detach(if_idx, if_name);
101         exit(0);
102 }
103
104 static void usage(const char *prog)
105 {
106         fprintf(stderr,
107                 "%s: %s [OPTS] <ifname|ifindex>\n\n"
108                 "OPTS:\n"
109                 "    -F    force loading prog\n",
110                 __func__, prog);
111 }
112
113 int main(int argc, char **argv)
114 {
115         struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
116         struct bpf_prog_load_attr prog_load_attr = {
117                 .prog_type      = BPF_PROG_TYPE_XDP,
118         };
119         const char *optstr = "F";
120         int prog_fd, map_fd, opt;
121         struct bpf_object *obj;
122         struct bpf_map *map;
123         char filename[256];
124         int ret, err, i;
125         int numcpus;
126
127         while ((opt = getopt(argc, argv, optstr)) != -1) {
128                 switch (opt) {
129                 case 'F':
130                         xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
131                         break;
132                 default:
133                         usage(basename(argv[0]));
134                         return 1;
135                 }
136         }
137
138         if (optind == argc) {
139                 usage(basename(argv[0]));
140                 return 1;
141         }
142
143         if (setrlimit(RLIMIT_MEMLOCK, &r)) {
144                 perror("setrlimit(RLIMIT_MEMLOCK)");
145                 return 1;
146         }
147
148         numcpus = get_nprocs();
149         if (numcpus > MAX_CPUS)
150                 numcpus = MAX_CPUS;
151
152         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
153         prog_load_attr.file = filename;
154
155         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
156                 return 1;
157
158         if (!prog_fd) {
159                 printf("load_bpf_file: %s\n", strerror(errno));
160                 return 1;
161         }
162
163         map = bpf_map__next(NULL, obj);
164         if (!map) {
165                 printf("finding a map in obj file failed\n");
166                 return 1;
167         }
168         map_fd = bpf_map__fd(map);
169
170         if_idx = if_nametoindex(argv[optind]);
171         if (!if_idx)
172                 if_idx = strtoul(argv[optind], NULL, 0);
173
174         if (!if_idx) {
175                 fprintf(stderr, "Invalid ifname\n");
176                 return 1;
177         }
178         if_name = argv[optind];
179         err = do_attach(if_idx, prog_fd, if_name);
180         if (err)
181                 return err;
182
183         if (signal(SIGINT, sig_handler) ||
184             signal(SIGHUP, sig_handler) ||
185             signal(SIGTERM, sig_handler)) {
186                 perror("signal");
187                 return 1;
188         }
189
190         test_bpf_perf_event(map_fd, numcpus);
191
192         for (i = 0; i < numcpus; i++)
193                 if (perf_event_mmap_header(pmu_fds[i], &headers[i]) < 0)
194                         return 1;
195
196         ret = perf_event_poller_multi(pmu_fds, headers, numcpus,
197                                       print_bpf_output);
198         kill(0, SIGINT);
199         return ret;
200 }