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