btrfs: use file:line format for assertion report
[linux/fpc-iii.git] / samples / bpf / xdp_sample_pkts_user.c
blobdc66345a929a8a142001b2f26e863f1c676bab31
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>
19 #include "perf-sys.h"
20 #include "trace_helpers.h"
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 static __u32 prog_id;
29 static int do_attach(int idx, int fd, const char *name)
31 struct bpf_prog_info info = {};
32 __u32 info_len = sizeof(info);
33 int err;
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;
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;
46 prog_id = info.id;
48 return err;
51 static int do_detach(int idx, const char *name)
53 __u32 curr_prog_id = 0;
54 int err = 0;
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;
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");
71 return err;
74 #define SAMPLE_SIZE 64
76 static int print_bpf_output(void *data, int size)
78 struct {
79 __u16 cookie;
80 __u16 pkt_len;
81 __u8 pkt_data[SAMPLE_SIZE];
82 } __packed *e = data;
83 int i;
85 if (e->cookie != 0xdead) {
86 printf("BUG cookie %x sized %d\n",
87 e->cookie, size);
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]);
94 printf("\n");
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 */
107 int i;
109 for (i = 0; i < num; i++) {
110 int key = i;
112 pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
113 -1/*group_fd*/, 0);
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);
125 exit(0);
128 static void usage(const char *prog)
130 fprintf(stderr,
131 "%s: %s [OPTS] <ifname|ifindex>\n\n"
132 "OPTS:\n"
133 " -F force loading prog\n",
134 __func__, prog);
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;
146 struct bpf_map *map;
147 char filename[256];
148 int ret, err, i;
149 int numcpus;
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;
162 if (optind == argc) {
163 usage(basename(argv[0]));
164 return 1;
167 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
168 perror("setrlimit(RLIMIT_MEMLOCK)");
169 return 1;
172 numcpus = get_nprocs();
173 if (numcpus > MAX_CPUS)
174 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))
180 return 1;
182 if (!prog_fd) {
183 printf("load_bpf_file: %s\n", strerror(errno));
184 return 1;
187 map = bpf_map__next(NULL, obj);
188 if (!map) {
189 printf("finding a map in obj file failed\n");
190 return 1;
192 map_fd = bpf_map__fd(map);
194 if_idx = if_nametoindex(argv[optind]);
195 if (!if_idx)
196 if_idx = strtoul(argv[optind], NULL, 0);
198 if (!if_idx) {
199 fprintf(stderr, "Invalid ifname\n");
200 return 1;
202 if_name = argv[optind];
203 err = do_attach(if_idx, prog_fd, if_name);
204 if (err)
205 return err;
207 if (signal(SIGINT, sig_handler) ||
208 signal(SIGHUP, sig_handler) ||
209 signal(SIGTERM, sig_handler)) {
210 perror("signal");
211 return 1;
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)
218 return 1;
220 ret = perf_event_poller_multi(pmu_fds, headers, numcpus,
221 print_bpf_output);
222 kill(0, SIGINT);
223 return ret;