Commit | Line | Data |
---|---|---|
1e54ad25 THJ |
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> | |
7cf245a3 | 13 | #include <bpf/libbpf.h> |
1e54ad25 | 14 | #include <bpf/bpf.h> |
743e568c MF |
15 | #include <libgen.h> |
16 | #include <linux/if_link.h> | |
1e54ad25 THJ |
17 | |
18 | #include "perf-sys.h" | |
1e54ad25 | 19 | |
f58a4d51 | 20 | static int if_idx; |
1e54ad25 | 21 | static char *if_name; |
743e568c | 22 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
3b7a8ec2 | 23 | static __u32 prog_id; |
f58a4d51 | 24 | static struct perf_buffer *pb = NULL; |
1e54ad25 THJ |
25 | |
26 | static int do_attach(int idx, int fd, const char *name) | |
27 | { | |
3b7a8ec2 MF |
28 | struct bpf_prog_info info = {}; |
29 | __u32 info_len = sizeof(info); | |
1e54ad25 THJ |
30 | int err; |
31 | ||
d4e34bfc | 32 | err = bpf_xdp_attach(idx, fd, xdp_flags, NULL); |
3b7a8ec2 | 33 | if (err < 0) { |
1e54ad25 | 34 | printf("ERROR: failed to attach program to %s\n", name); |
3b7a8ec2 MF |
35 | return err; |
36 | } | |
37 | ||
c0ca277b | 38 | err = bpf_prog_get_info_by_fd(fd, &info, &info_len); |
3b7a8ec2 MF |
39 | if (err) { |
40 | printf("can't get prog info - %s\n", strerror(errno)); | |
41 | return err; | |
42 | } | |
43 | prog_id = info.id; | |
1e54ad25 THJ |
44 | |
45 | return err; | |
46 | } | |
47 | ||
48 | static int do_detach(int idx, const char *name) | |
49 | { | |
3b7a8ec2 MF |
50 | __u32 curr_prog_id = 0; |
51 | int err = 0; | |
1e54ad25 | 52 | |
d4e34bfc | 53 | err = bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id); |
3b7a8ec2 | 54 | if (err) { |
d4e34bfc | 55 | printf("bpf_xdp_query_id failed\n"); |
3b7a8ec2 MF |
56 | return err; |
57 | } | |
58 | if (prog_id == curr_prog_id) { | |
d4e34bfc | 59 | err = bpf_xdp_detach(idx, xdp_flags, NULL); |
3b7a8ec2 MF |
60 | if (err < 0) |
61 | printf("ERROR: failed to detach prog from %s\n", name); | |
62 | } else if (!curr_prog_id) { | |
63 | printf("couldn't find a prog id on a %s\n", name); | |
64 | } else { | |
65 | printf("program on interface changed, not removing\n"); | |
66 | } | |
1e54ad25 THJ |
67 | |
68 | return err; | |
69 | } | |
70 | ||
71 | #define SAMPLE_SIZE 64 | |
72 | ||
f58a4d51 | 73 | static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size) |
1e54ad25 THJ |
74 | { |
75 | struct { | |
76 | __u16 cookie; | |
77 | __u16 pkt_len; | |
78 | __u8 pkt_data[SAMPLE_SIZE]; | |
79 | } __packed *e = data; | |
80 | int i; | |
81 | ||
82 | if (e->cookie != 0xdead) { | |
f58a4d51 AN |
83 | printf("BUG cookie %x sized %d\n", e->cookie, size); |
84 | return; | |
1e54ad25 THJ |
85 | } |
86 | ||
87 | printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len); | |
88 | for (i = 0; i < 14 && i < e->pkt_len; i++) | |
89 | printf("%02x ", e->pkt_data[i]); | |
90 | printf("\n"); | |
1e54ad25 THJ |
91 | } |
92 | ||
93 | static void sig_handler(int signo) | |
94 | { | |
95 | do_detach(if_idx, if_name); | |
f58a4d51 | 96 | perf_buffer__free(pb); |
1e54ad25 THJ |
97 | exit(0); |
98 | } | |
99 | ||
743e568c MF |
100 | static void usage(const char *prog) |
101 | { | |
102 | fprintf(stderr, | |
103 | "%s: %s [OPTS] <ifname|ifindex>\n\n" | |
104 | "OPTS:\n" | |
dfdda1a0 WH |
105 | " -F force loading prog\n" |
106 | " -S use skb-mode\n", | |
743e568c MF |
107 | __func__, prog); |
108 | } | |
109 | ||
1e54ad25 THJ |
110 | int main(int argc, char **argv) |
111 | { | |
d50ecc46 | 112 | const char *optstr = "FS"; |
743e568c | 113 | int prog_fd, map_fd, opt; |
c58f9815 | 114 | struct bpf_program *prog; |
1e54ad25 THJ |
115 | struct bpf_object *obj; |
116 | struct bpf_map *map; | |
1e54ad25 | 117 | char filename[256]; |
f58a4d51 | 118 | int ret, err; |
1e54ad25 | 119 | |
743e568c MF |
120 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
121 | switch (opt) { | |
122 | case 'F': | |
123 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; | |
124 | break; | |
d50ecc46 THJ |
125 | case 'S': |
126 | xdp_flags |= XDP_FLAGS_SKB_MODE; | |
127 | break; | |
743e568c MF |
128 | default: |
129 | usage(basename(argv[0])); | |
130 | return 1; | |
131 | } | |
132 | } | |
133 | ||
d50ecc46 THJ |
134 | if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
135 | xdp_flags |= XDP_FLAGS_DRV_MODE; | |
136 | ||
743e568c MF |
137 | if (optind == argc) { |
138 | usage(basename(argv[0])); | |
1e54ad25 THJ |
139 | return 1; |
140 | } | |
141 | ||
1e54ad25 | 142 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
1e54ad25 | 143 | |
c58f9815 AN |
144 | obj = bpf_object__open_file(filename, NULL); |
145 | if (libbpf_get_error(obj)) | |
1e54ad25 THJ |
146 | return 1; |
147 | ||
c58f9815 AN |
148 | prog = bpf_object__next_program(obj, NULL); |
149 | bpf_program__set_type(prog, BPF_PROG_TYPE_XDP); | |
150 | ||
151 | err = bpf_object__load(obj); | |
152 | if (err) | |
1e54ad25 | 153 | return 1; |
c58f9815 AN |
154 | |
155 | prog_fd = bpf_program__fd(prog); | |
1e54ad25 | 156 | |
6f2b219b | 157 | map = bpf_object__next_map(obj, NULL); |
1e54ad25 THJ |
158 | if (!map) { |
159 | printf("finding a map in obj file failed\n"); | |
160 | return 1; | |
161 | } | |
162 | map_fd = bpf_map__fd(map); | |
163 | ||
743e568c | 164 | if_idx = if_nametoindex(argv[optind]); |
1e54ad25 | 165 | if (!if_idx) |
743e568c | 166 | if_idx = strtoul(argv[optind], NULL, 0); |
1e54ad25 THJ |
167 | |
168 | if (!if_idx) { | |
169 | fprintf(stderr, "Invalid ifname\n"); | |
170 | return 1; | |
171 | } | |
743e568c MF |
172 | if_name = argv[optind]; |
173 | err = do_attach(if_idx, prog_fd, if_name); | |
1e54ad25 THJ |
174 | if (err) |
175 | return err; | |
176 | ||
177 | if (signal(SIGINT, sig_handler) || | |
178 | signal(SIGHUP, sig_handler) || | |
179 | signal(SIGTERM, sig_handler)) { | |
180 | perror("signal"); | |
181 | return 1; | |
182 | } | |
183 | ||
c58f9815 | 184 | pb = perf_buffer__new(map_fd, 8, print_bpf_output, NULL, NULL, NULL); |
f58a4d51 AN |
185 | err = libbpf_get_error(pb); |
186 | if (err) { | |
187 | perror("perf_buffer setup failed"); | |
188 | return 1; | |
189 | } | |
1e54ad25 | 190 | |
f58a4d51 AN |
191 | while ((ret = perf_buffer__poll(pb, 1000)) >= 0) { |
192 | } | |
1e54ad25 | 193 | |
1e54ad25 THJ |
194 | kill(0, SIGINT); |
195 | return ret; | |
196 | } |