blob: 991ef6f0880b2226e732412c8a3e1199f6b356f1 [file] [log] [blame]
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +02001// 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>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010013#include <bpf/libbpf.h>
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020014#include <bpf/bpf.h>
Maciej Fijalkowski6a545762019-02-01 22:42:26 +010015#include <sys/resource.h>
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010016#include <libgen.h>
17#include <linux/if_link.h>
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020018
19#include "perf-sys.h"
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020020
21#define MAX_CPUS 128
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070022static int if_idx;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020023static char *if_name;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010024static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010025static __u32 prog_id;
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070026static struct perf_buffer *pb = NULL;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020027
28static int do_attach(int idx, int fd, const char *name)
29{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010030 struct bpf_prog_info info = {};
31 __u32 info_len = sizeof(info);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020032 int err;
33
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010034 err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010035 if (err < 0) {
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020036 printf("ERROR: failed to attach program to %s\n", name);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010037 return err;
38 }
39
40 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
41 if (err) {
42 printf("can't get prog info - %s\n", strerror(errno));
43 return err;
44 }
45 prog_id = info.id;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020046
47 return err;
48}
49
50static int do_detach(int idx, const char *name)
51{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010052 __u32 curr_prog_id = 0;
53 int err = 0;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020054
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010055 err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010056 if (err) {
57 printf("bpf_get_link_xdp_id failed\n");
58 return err;
59 }
60 if (prog_id == curr_prog_id) {
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010061 err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010062 if (err < 0)
63 printf("ERROR: failed to detach prog from %s\n", name);
64 } else if (!curr_prog_id) {
65 printf("couldn't find a prog id on a %s\n", name);
66 } else {
67 printf("program on interface changed, not removing\n");
68 }
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020069
70 return err;
71}
72
73#define SAMPLE_SIZE 64
74
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070075static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020076{
77 struct {
78 __u16 cookie;
79 __u16 pkt_len;
80 __u8 pkt_data[SAMPLE_SIZE];
81 } __packed *e = data;
82 int i;
83
84 if (e->cookie != 0xdead) {
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070085 printf("BUG cookie %x sized %d\n", e->cookie, size);
86 return;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020087 }
88
89 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
90 for (i = 0; i < 14 && i < e->pkt_len; i++)
91 printf("%02x ", e->pkt_data[i]);
92 printf("\n");
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020093}
94
95static void sig_handler(int signo)
96{
97 do_detach(if_idx, if_name);
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070098 perf_buffer__free(pb);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020099 exit(0);
100}
101
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100102static void usage(const char *prog)
103{
104 fprintf(stderr,
105 "%s: %s [OPTS] <ifname|ifindex>\n\n"
106 "OPTS:\n"
107 " -F force loading prog\n",
108 __func__, prog);
109}
110
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200111int main(int argc, char **argv)
112{
Maciej Fijalkowski6a545762019-02-01 22:42:26 +0100113 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200114 struct bpf_prog_load_attr prog_load_attr = {
115 .prog_type = BPF_PROG_TYPE_XDP,
116 };
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700117 struct perf_buffer_opts pb_opts = {};
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100118 const char *optstr = "FS";
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100119 int prog_fd, map_fd, opt;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200120 struct bpf_object *obj;
121 struct bpf_map *map;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200122 char filename[256];
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700123 int ret, err;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200124
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100125 while ((opt = getopt(argc, argv, optstr)) != -1) {
126 switch (opt) {
127 case 'F':
128 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
129 break;
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100130 case 'S':
131 xdp_flags |= XDP_FLAGS_SKB_MODE;
132 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100133 default:
134 usage(basename(argv[0]));
135 return 1;
136 }
137 }
138
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100139 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
140 xdp_flags |= XDP_FLAGS_DRV_MODE;
141
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100142 if (optind == argc) {
143 usage(basename(argv[0]));
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200144 return 1;
145 }
146
Maciej Fijalkowski6a545762019-02-01 22:42:26 +0100147 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
148 perror("setrlimit(RLIMIT_MEMLOCK)");
149 return 1;
150 }
151
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200152 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) {
Daniel T. Leeafbe3c22019-11-07 09:51:52 +0900159 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200160 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
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100170 if_idx = if_nametoindex(argv[optind]);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200171 if (!if_idx)
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100172 if_idx = strtoul(argv[optind], NULL, 0);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200173
174 if (!if_idx) {
175 fprintf(stderr, "Invalid ifname\n");
176 return 1;
177 }
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100178 if_name = argv[optind];
179 err = do_attach(if_idx, prog_fd, if_name);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200180 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
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700190 pb_opts.sample_cb = print_bpf_output;
191 pb = perf_buffer__new(map_fd, 8, &pb_opts);
192 err = libbpf_get_error(pb);
193 if (err) {
194 perror("perf_buffer setup failed");
195 return 1;
196 }
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200197
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700198 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {
199 }
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200200
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200201 kill(0, SIGINT);
202 return ret;
203}