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 
16 #include "perf-sys.h"
17 #include "trace_helpers.h"
18 
19 #define MAX_CPUS 128
20 static int pmu_fds[MAX_CPUS], if_idx;
21 static struct perf_event_mmap_page *headers[MAX_CPUS];
22 static char *if_name;
23 
do_attach(int idx,int fd,const char * name)24 static int do_attach(int idx, int fd, const char *name)
25 {
26 	int err;
27 
28 	err = bpf_set_link_xdp_fd(idx, fd, 0);
29 	if (err < 0)
30 		printf("ERROR: failed to attach program to %s\n", name);
31 
32 	return err;
33 }
34 
do_detach(int idx,const char * name)35 static int do_detach(int idx, const char *name)
36 {
37 	int err;
38 
39 	err = bpf_set_link_xdp_fd(idx, -1, 0);
40 	if (err < 0)
41 		printf("ERROR: failed to detach program from %s\n", name);
42 
43 	return err;
44 }
45 
46 #define SAMPLE_SIZE 64
47 
print_bpf_output(void * data,int size)48 static int print_bpf_output(void *data, int size)
49 {
50 	struct {
51 		__u16 cookie;
52 		__u16 pkt_len;
53 		__u8  pkt_data[SAMPLE_SIZE];
54 	} __packed *e = data;
55 	int i;
56 
57 	if (e->cookie != 0xdead) {
58 		printf("BUG cookie %x sized %d\n",
59 		       e->cookie, size);
60 		return LIBBPF_PERF_EVENT_ERROR;
61 	}
62 
63 	printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
64 	for (i = 0; i < 14 && i < e->pkt_len; i++)
65 		printf("%02x ", e->pkt_data[i]);
66 	printf("\n");
67 
68 	return LIBBPF_PERF_EVENT_CONT;
69 }
70 
test_bpf_perf_event(int map_fd,int num)71 static void test_bpf_perf_event(int map_fd, int num)
72 {
73 	struct perf_event_attr attr = {
74 		.sample_type = PERF_SAMPLE_RAW,
75 		.type = PERF_TYPE_SOFTWARE,
76 		.config = PERF_COUNT_SW_BPF_OUTPUT,
77 		.wakeup_events = 1, /* get an fd notification for every event */
78 	};
79 	int i;
80 
81 	for (i = 0; i < num; i++) {
82 		int key = i;
83 
84 		pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
85 						 -1/*group_fd*/, 0);
86 
87 		assert(pmu_fds[i] >= 0);
88 		assert(bpf_map_update_elem(map_fd, &key,
89 					   &pmu_fds[i], BPF_ANY) == 0);
90 		ioctl(pmu_fds[i], PERF_EVENT_IOC_ENABLE, 0);
91 	}
92 }
93 
sig_handler(int signo)94 static void sig_handler(int signo)
95 {
96 	do_detach(if_idx, if_name);
97 	exit(0);
98 }
99 
main(int argc,char ** argv)100 int main(int argc, char **argv)
101 {
102 	struct bpf_prog_load_attr prog_load_attr = {
103 		.prog_type	= BPF_PROG_TYPE_XDP,
104 	};
105 	struct bpf_object *obj;
106 	struct bpf_map *map;
107 	int prog_fd, map_fd;
108 	char filename[256];
109 	int ret, err, i;
110 	int numcpus;
111 
112 	if (argc < 2) {
113 		printf("Usage: %s <ifname>\n", argv[0]);
114 		return 1;
115 	}
116 
117 	numcpus = get_nprocs();
118 	if (numcpus > MAX_CPUS)
119 		numcpus = MAX_CPUS;
120 
121 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
122 	prog_load_attr.file = filename;
123 
124 	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
125 		return 1;
126 
127 	if (!prog_fd) {
128 		printf("load_bpf_file: %s\n", strerror(errno));
129 		return 1;
130 	}
131 
132 	map = bpf_map__next(NULL, obj);
133 	if (!map) {
134 		printf("finding a map in obj file failed\n");
135 		return 1;
136 	}
137 	map_fd = bpf_map__fd(map);
138 
139 	if_idx = if_nametoindex(argv[1]);
140 	if (!if_idx)
141 		if_idx = strtoul(argv[1], NULL, 0);
142 
143 	if (!if_idx) {
144 		fprintf(stderr, "Invalid ifname\n");
145 		return 1;
146 	}
147 	if_name = argv[1];
148 	err = do_attach(if_idx, prog_fd, argv[1]);
149 	if (err)
150 		return err;
151 
152 	if (signal(SIGINT, sig_handler) ||
153 	    signal(SIGHUP, sig_handler) ||
154 	    signal(SIGTERM, sig_handler)) {
155 		perror("signal");
156 		return 1;
157 	}
158 
159 	test_bpf_perf_event(map_fd, numcpus);
160 
161 	for (i = 0; i < numcpus; i++)
162 		if (perf_event_mmap_header(pmu_fds[i], &headers[i]) < 0)
163 			return 1;
164 
165 	ret = perf_event_poller_multi(pmu_fds, headers, numcpus,
166 				      print_bpf_output);
167 	kill(0, SIGINT);
168 	return ret;
169 }
170