blob: 4b2a300c750ceac32a4eb68b5fc3498eb36f292c [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
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070021static int if_idx;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020022static char *if_name;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010023static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010024static __u32 prog_id;
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070025static struct perf_buffer *pb = NULL;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020026
27static int do_attach(int idx, int fd, const char *name)
28{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010029 struct bpf_prog_info info = {};
30 __u32 info_len = sizeof(info);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020031 int err;
32
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010033 err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010034 if (err < 0) {
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020035 printf("ERROR: failed to attach program to %s\n", name);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010036 return err;
37 }
38
39 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
40 if (err) {
41 printf("can't get prog info - %s\n", strerror(errno));
42 return err;
43 }
44 prog_id = info.id;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020045
46 return err;
47}
48
49static int do_detach(int idx, const char *name)
50{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010051 __u32 curr_prog_id = 0;
52 int err = 0;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020053
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010054 err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010055 if (err) {
56 printf("bpf_get_link_xdp_id failed\n");
57 return err;
58 }
59 if (prog_id == curr_prog_id) {
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010060 err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010061 if (err < 0)
62 printf("ERROR: failed to detach prog from %s\n", name);
63 } else if (!curr_prog_id) {
64 printf("couldn't find a prog id on a %s\n", name);
65 } else {
66 printf("program on interface changed, not removing\n");
67 }
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020068
69 return err;
70}
71
72#define SAMPLE_SIZE 64
73
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070074static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020075{
76 struct {
77 __u16 cookie;
78 __u16 pkt_len;
79 __u8 pkt_data[SAMPLE_SIZE];
80 } __packed *e = data;
81 int i;
82
83 if (e->cookie != 0xdead) {
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070084 printf("BUG cookie %x sized %d\n", e->cookie, size);
85 return;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020086 }
87
88 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
89 for (i = 0; i < 14 && i < e->pkt_len; i++)
90 printf("%02x ", e->pkt_data[i]);
91 printf("\n");
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020092}
93
94static void sig_handler(int signo)
95{
96 do_detach(if_idx, if_name);
Andrii Nakryikof58a4d52019-07-23 14:34:43 -070097 perf_buffer__free(pb);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +020098 exit(0);
99}
100
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100101static void usage(const char *prog)
102{
103 fprintf(stderr,
104 "%s: %s [OPTS] <ifname|ifindex>\n\n"
105 "OPTS:\n"
106 " -F force loading prog\n",
107 __func__, prog);
108}
109
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200110int main(int argc, char **argv)
111{
Maciej Fijalkowski6a545762019-02-01 22:42:26 +0100112 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200113 struct bpf_prog_load_attr prog_load_attr = {
114 .prog_type = BPF_PROG_TYPE_XDP,
115 };
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700116 struct perf_buffer_opts pb_opts = {};
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100117 const char *optstr = "FS";
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100118 int prog_fd, map_fd, opt;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200119 struct bpf_object *obj;
120 struct bpf_map *map;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200121 char filename[256];
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700122 int ret, err;
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200123
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100124 while ((opt = getopt(argc, argv, optstr)) != -1) {
125 switch (opt) {
126 case 'F':
127 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
128 break;
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100129 case 'S':
130 xdp_flags |= XDP_FLAGS_SKB_MODE;
131 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100132 default:
133 usage(basename(argv[0]));
134 return 1;
135 }
136 }
137
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100138 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
139 xdp_flags |= XDP_FLAGS_DRV_MODE;
140
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100141 if (optind == argc) {
142 usage(basename(argv[0]));
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200143 return 1;
144 }
145
Maciej Fijalkowski6a545762019-02-01 22:42:26 +0100146 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
147 perror("setrlimit(RLIMIT_MEMLOCK)");
148 return 1;
149 }
150
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200151 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
152 prog_load_attr.file = filename;
153
154 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
155 return 1;
156
157 if (!prog_fd) {
Daniel T. Leeafbe3c22019-11-07 09:51:52 +0900158 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200159 return 1;
160 }
161
162 map = bpf_map__next(NULL, obj);
163 if (!map) {
164 printf("finding a map in obj file failed\n");
165 return 1;
166 }
167 map_fd = bpf_map__fd(map);
168
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100169 if_idx = if_nametoindex(argv[optind]);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200170 if (!if_idx)
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100171 if_idx = strtoul(argv[optind], NULL, 0);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200172
173 if (!if_idx) {
174 fprintf(stderr, "Invalid ifname\n");
175 return 1;
176 }
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100177 if_name = argv[optind];
178 err = do_attach(if_idx, prog_fd, if_name);
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200179 if (err)
180 return err;
181
182 if (signal(SIGINT, sig_handler) ||
183 signal(SIGHUP, sig_handler) ||
184 signal(SIGTERM, sig_handler)) {
185 perror("signal");
186 return 1;
187 }
188
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700189 pb_opts.sample_cb = print_bpf_output;
190 pb = perf_buffer__new(map_fd, 8, &pb_opts);
191 err = libbpf_get_error(pb);
192 if (err) {
193 perror("perf_buffer setup failed");
194 return 1;
195 }
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200196
Andrii Nakryikof58a4d52019-07-23 14:34:43 -0700197 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {
198 }
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200199
Toke Høiland-Jørgensen1e54ad22018-06-25 14:25:02 +0200200 kill(0, SIGINT);
201 return ret;
202}