Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9d6e0052 | 2 | /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io |
9d6e0052 JF |
3 | */ |
4 | #include <linux/bpf.h> | |
24251c26 | 5 | #include <linux/if_link.h> |
9d6e0052 JF |
6 | #include <assert.h> |
7 | #include <errno.h> | |
8 | #include <signal.h> | |
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
306da4e6 | 11 | #include <stdbool.h> |
9d6e0052 | 12 | #include <string.h> |
9e859e8f | 13 | #include <net/if.h> |
9d6e0052 | 14 | #include <unistd.h> |
24251c26 | 15 | #include <libgen.h> |
21d72af7 | 16 | #include <sys/resource.h> |
9d6e0052 | 17 | |
9d6e0052 | 18 | #include "bpf_util.h" |
2bf3e2ef | 19 | #include <bpf/bpf.h> |
4d18f6de | 20 | #include "libbpf.h" |
9d6e0052 JF |
21 | |
22 | static int ifindex_in; | |
23 | static int ifindex_out; | |
306da4e6 | 24 | static bool ifindex_out_xdp_dummy_attached = true; |
3b7a8ec2 MF |
25 | static __u32 prog_id; |
26 | static __u32 dummy_prog_id; | |
9d6e0052 | 27 | |
743e568c | 28 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
bbaf6029 | 29 | static int rxcnt_map_fd; |
24251c26 | 30 | |
9d6e0052 JF |
31 | static void int_exit(int sig) |
32 | { | |
3b7a8ec2 MF |
33 | __u32 curr_prog_id = 0; |
34 | ||
35 | if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) { | |
36 | printf("bpf_get_link_xdp_id failed\n"); | |
37 | exit(1); | |
38 | } | |
39 | if (prog_id == curr_prog_id) | |
40 | bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags); | |
41 | else if (!curr_prog_id) | |
42 | printf("couldn't find a prog id on iface IN\n"); | |
43 | else | |
44 | printf("program on iface IN changed, not removing\n"); | |
45 | ||
46 | if (ifindex_out_xdp_dummy_attached) { | |
47 | curr_prog_id = 0; | |
48 | if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id, | |
49 | xdp_flags)) { | |
50 | printf("bpf_get_link_xdp_id failed\n"); | |
51 | exit(1); | |
52 | } | |
915654fd | 53 | if (dummy_prog_id == curr_prog_id) |
3b7a8ec2 MF |
54 | bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags); |
55 | else if (!curr_prog_id) | |
56 | printf("couldn't find a prog id on iface OUT\n"); | |
57 | else | |
58 | printf("program on iface OUT changed, not removing\n"); | |
59 | } | |
9d6e0052 JF |
60 | exit(0); |
61 | } | |
62 | ||
9d6e0052 JF |
63 | static void poll_stats(int interval, int ifindex) |
64 | { | |
65 | unsigned int nr_cpus = bpf_num_possible_cpus(); | |
66 | __u64 values[nr_cpus], prev[nr_cpus]; | |
67 | ||
68 | memset(prev, 0, sizeof(prev)); | |
69 | ||
70 | while (1) { | |
71 | __u64 sum = 0; | |
72 | __u32 key = 0; | |
73 | int i; | |
74 | ||
75 | sleep(interval); | |
bbaf6029 | 76 | assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0); |
9d6e0052 JF |
77 | for (i = 0; i < nr_cpus; i++) |
78 | sum += (values[i] - prev[i]); | |
79 | if (sum) | |
80 | printf("ifindex %i: %10llu pkt/s\n", | |
81 | ifindex, sum / interval); | |
82 | memcpy(prev, values, sizeof(values)); | |
83 | } | |
84 | } | |
85 | ||
24251c26 | 86 | static void usage(const char *prog) |
9d6e0052 | 87 | { |
24251c26 | 88 | fprintf(stderr, |
9e859e8f | 89 | "usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n" |
24251c26 AG |
90 | "OPTS:\n" |
91 | " -S use skb-mode\n" | |
743e568c MF |
92 | " -N enforce native mode\n" |
93 | " -F force loading prog\n", | |
24251c26 AG |
94 | prog); |
95 | } | |
9d6e0052 | 96 | |
24251c26 AG |
97 | int main(int argc, char **argv) |
98 | { | |
21d72af7 | 99 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
bbaf6029 MF |
100 | struct bpf_prog_load_attr prog_load_attr = { |
101 | .prog_type = BPF_PROG_TYPE_XDP, | |
102 | }; | |
103 | struct bpf_program *prog, *dummy_prog; | |
3b7a8ec2 MF |
104 | struct bpf_prog_info info = {}; |
105 | __u32 info_len = sizeof(info); | |
bbaf6029 | 106 | int prog_fd, dummy_prog_fd; |
743e568c | 107 | const char *optstr = "FSN"; |
bbaf6029 | 108 | struct bpf_object *obj; |
24251c26 | 109 | int ret, opt, key = 0; |
bbaf6029 MF |
110 | char filename[256]; |
111 | int tx_port_map_fd; | |
24251c26 AG |
112 | |
113 | while ((opt = getopt(argc, argv, optstr)) != -1) { | |
114 | switch (opt) { | |
115 | case 'S': | |
116 | xdp_flags |= XDP_FLAGS_SKB_MODE; | |
117 | break; | |
118 | case 'N': | |
119 | xdp_flags |= XDP_FLAGS_DRV_MODE; | |
120 | break; | |
743e568c MF |
121 | case 'F': |
122 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; | |
123 | break; | |
24251c26 AG |
124 | default: |
125 | usage(basename(argv[0])); | |
126 | return 1; | |
127 | } | |
128 | } | |
129 | ||
130 | if (optind == argc) { | |
9e859e8f | 131 | printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]); |
9d6e0052 JF |
132 | return 1; |
133 | } | |
134 | ||
21d72af7 TD |
135 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
136 | perror("setrlimit(RLIMIT_MEMLOCK)"); | |
137 | return 1; | |
138 | } | |
139 | ||
9e859e8f DL |
140 | ifindex_in = if_nametoindex(argv[optind]); |
141 | if (!ifindex_in) | |
142 | ifindex_in = strtoul(argv[optind], NULL, 0); | |
143 | ||
144 | ifindex_out = if_nametoindex(argv[optind + 1]); | |
145 | if (!ifindex_out) | |
146 | ifindex_out = strtoul(argv[optind + 1], NULL, 0); | |
147 | ||
24251c26 AG |
148 | printf("input: %d output: %d\n", ifindex_in, ifindex_out); |
149 | ||
150 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | |
bbaf6029 MF |
151 | prog_load_attr.file = filename; |
152 | ||
153 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) | |
154 | return 1; | |
9d6e0052 | 155 | |
bbaf6029 MF |
156 | prog = bpf_program__next(NULL, obj); |
157 | dummy_prog = bpf_program__next(prog, obj); | |
158 | if (!prog || !dummy_prog) { | |
159 | printf("finding a prog in obj file failed\n"); | |
160 | return 1; | |
161 | } | |
162 | /* bpf_prog_load_xattr gives us the pointer to first prog's fd, | |
163 | * so we're missing only the fd for dummy prog | |
164 | */ | |
165 | dummy_prog_fd = bpf_program__fd(dummy_prog); | |
166 | if (prog_fd < 0 || dummy_prog_fd < 0) { | |
167 | printf("bpf_prog_load_xattr: %s\n", strerror(errno)); | |
9d6e0052 JF |
168 | return 1; |
169 | } | |
170 | ||
bbaf6029 MF |
171 | tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port"); |
172 | rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt"); | |
173 | if (tx_port_map_fd < 0 || rxcnt_map_fd < 0) { | |
174 | printf("bpf_object__find_map_fd_by_name failed\n"); | |
9d6e0052 JF |
175 | return 1; |
176 | } | |
177 | ||
bbaf6029 | 178 | if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) { |
306da4e6 | 179 | printf("ERROR: link set xdp fd failed on %d\n", ifindex_in); |
9d6e0052 JF |
180 | return 1; |
181 | } | |
182 | ||
3b7a8ec2 MF |
183 | ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); |
184 | if (ret) { | |
185 | printf("can't get prog info - %s\n", strerror(errno)); | |
186 | return ret; | |
187 | } | |
188 | prog_id = info.id; | |
189 | ||
306da4e6 | 190 | /* Loading dummy XDP prog on out-device */ |
bbaf6029 | 191 | if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd, |
306da4e6 JDB |
192 | (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) { |
193 | printf("WARN: link set xdp fd failed on %d\n", ifindex_out); | |
194 | ifindex_out_xdp_dummy_attached = false; | |
195 | } | |
196 | ||
3b7a8ec2 MF |
197 | memset(&info, 0, sizeof(info)); |
198 | ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len); | |
199 | if (ret) { | |
200 | printf("can't get prog info - %s\n", strerror(errno)); | |
201 | return ret; | |
202 | } | |
203 | dummy_prog_id = info.id; | |
204 | ||
306da4e6 JDB |
205 | signal(SIGINT, int_exit); |
206 | signal(SIGTERM, int_exit); | |
207 | ||
9d6e0052 | 208 | /* populate virtual to physical port map */ |
bbaf6029 | 209 | ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0); |
9d6e0052 JF |
210 | if (ret) { |
211 | perror("bpf_update_elem"); | |
212 | goto out; | |
213 | } | |
214 | ||
215 | poll_stats(2, ifindex_out); | |
216 | ||
217 | out: | |
218 | return 0; | |
219 | } |