Loading...
1/* Copyright (c) 2016 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/bpf.h>
8#include <assert.h>
9#include <errno.h>
10#include <signal.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/resource.h>
15#include <arpa/inet.h>
16#include <netinet/ether.h>
17#include <unistd.h>
18#include <time.h>
19#include "bpf_load.h"
20#include "libbpf.h"
21#include "bpf_util.h"
22#include "xdp_tx_iptunnel_common.h"
23
24#define STATS_INTERVAL_S 2U
25
26static int ifindex = -1;
27
28static void int_exit(int sig)
29{
30 if (ifindex > -1)
31 set_link_xdp_fd(ifindex, -1);
32 exit(0);
33}
34
35/* simple per-protocol drop counter
36 */
37static void poll_stats(unsigned int kill_after_s)
38{
39 const unsigned int nr_protos = 256;
40 unsigned int nr_cpus = bpf_num_possible_cpus();
41 time_t started_at = time(NULL);
42 __u64 values[nr_cpus], prev[nr_protos][nr_cpus];
43 __u32 proto;
44 int i;
45
46 memset(prev, 0, sizeof(prev));
47
48 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
49 sleep(STATS_INTERVAL_S);
50
51 for (proto = 0; proto < nr_protos; proto++) {
52 __u64 sum = 0;
53
54 assert(bpf_map_lookup_elem(map_fd[0], &proto, values) == 0);
55 for (i = 0; i < nr_cpus; i++)
56 sum += (values[i] - prev[proto][i]);
57
58 if (sum)
59 printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
60 proto, sum, sum / STATS_INTERVAL_S);
61 memcpy(prev[proto], values, sizeof(values));
62 }
63 }
64}
65
66static void usage(const char *cmd)
67{
68 printf("Start a XDP prog which encapsulates incoming packets\n"
69 "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n"
70 "is used to select packets to encapsulate\n\n");
71 printf("Usage: %s [...]\n", cmd);
72 printf(" -i <ifindex> Interface Index\n");
73 printf(" -a <vip-service-address> IPv4 or IPv6\n");
74 printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
75 printf(" -s <source-ip> Used in the IPTunnel header\n");
76 printf(" -d <dest-ip> Used in the IPTunnel header\n");
77 printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
78 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
79 printf(" -P <IP-Protocol> Default is TCP\n");
80 printf(" -h Display this help\n");
81}
82
83static int parse_ipstr(const char *ipstr, unsigned int *addr)
84{
85 if (inet_pton(AF_INET6, ipstr, addr) == 1) {
86 return AF_INET6;
87 } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
88 addr[1] = addr[2] = addr[3] = 0;
89 return AF_INET;
90 }
91
92 fprintf(stderr, "%s is an invalid IP\n", ipstr);
93 return AF_UNSPEC;
94}
95
96static int parse_ports(const char *port_str, int *min_port, int *max_port)
97{
98 char *end;
99 long tmp_min_port;
100 long tmp_max_port;
101
102 tmp_min_port = strtol(optarg, &end, 10);
103 if (tmp_min_port < 1 || tmp_min_port > 65535) {
104 fprintf(stderr, "Invalid port(s):%s\n", optarg);
105 return 1;
106 }
107
108 if (*end == '-') {
109 end++;
110 tmp_max_port = strtol(end, NULL, 10);
111 if (tmp_max_port < 1 || tmp_max_port > 65535) {
112 fprintf(stderr, "Invalid port(s):%s\n", optarg);
113 return 1;
114 }
115 } else {
116 tmp_max_port = tmp_min_port;
117 }
118
119 if (tmp_min_port > tmp_max_port) {
120 fprintf(stderr, "Invalid port(s):%s\n", optarg);
121 return 1;
122 }
123
124 if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
125 fprintf(stderr, "Port range (%s) is larger than %u\n",
126 port_str, MAX_IPTNL_ENTRIES);
127 return 1;
128 }
129 *min_port = tmp_min_port;
130 *max_port = tmp_max_port;
131
132 return 0;
133}
134
135int main(int argc, char **argv)
136{
137 unsigned char opt_flags[256] = {};
138 unsigned int kill_after_s = 0;
139 const char *optstr = "i:a:p:s:d:m:T:P:h";
140 int min_port = 0, max_port = 0;
141 struct iptnl_info tnl = {};
142 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
143 struct vip vip = {};
144 char filename[256];
145 int opt;
146 int i;
147
148 tnl.family = AF_UNSPEC;
149 vip.protocol = IPPROTO_TCP;
150
151 for (i = 0; i < strlen(optstr); i++)
152 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
153 opt_flags[(unsigned char)optstr[i]] = 1;
154
155 while ((opt = getopt(argc, argv, optstr)) != -1) {
156 unsigned short family;
157 unsigned int *v6;
158
159 switch (opt) {
160 case 'i':
161 ifindex = atoi(optarg);
162 break;
163 case 'a':
164 vip.family = parse_ipstr(optarg, vip.daddr.v6);
165 if (vip.family == AF_UNSPEC)
166 return 1;
167 break;
168 case 'p':
169 if (parse_ports(optarg, &min_port, &max_port))
170 return 1;
171 break;
172 case 'P':
173 vip.protocol = atoi(optarg);
174 break;
175 case 's':
176 case 'd':
177 if (opt == 's')
178 v6 = tnl.saddr.v6;
179 else
180 v6 = tnl.daddr.v6;
181
182 family = parse_ipstr(optarg, v6);
183 if (family == AF_UNSPEC)
184 return 1;
185 if (tnl.family == AF_UNSPEC) {
186 tnl.family = family;
187 } else if (tnl.family != family) {
188 fprintf(stderr,
189 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
190 return 1;
191 }
192 break;
193 case 'm':
194 if (!ether_aton_r(optarg,
195 (struct ether_addr *)tnl.dmac)) {
196 fprintf(stderr, "Invalid mac address:%s\n",
197 optarg);
198 return 1;
199 }
200 break;
201 case 'T':
202 kill_after_s = atoi(optarg);
203 break;
204 default:
205 usage(argv[0]);
206 return 1;
207 }
208 opt_flags[opt] = 0;
209 }
210
211 for (i = 0; i < strlen(optstr); i++) {
212 if (opt_flags[(unsigned int)optstr[i]]) {
213 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
214 usage(argv[0]);
215 return 1;
216 }
217 }
218
219 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
220 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
221 return 1;
222 }
223
224 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
225
226 if (load_bpf_file(filename)) {
227 printf("%s", bpf_log_buf);
228 return 1;
229 }
230
231 if (!prog_fd[0]) {
232 printf("load_bpf_file: %s\n", strerror(errno));
233 return 1;
234 }
235
236 signal(SIGINT, int_exit);
237
238 while (min_port <= max_port) {
239 vip.dport = htons(min_port++);
240 if (bpf_map_update_elem(map_fd[1], &vip, &tnl, BPF_NOEXIST)) {
241 perror("bpf_map_update_elem(&vip2tnl)");
242 return 1;
243 }
244 }
245
246 if (set_link_xdp_fd(ifindex, prog_fd[0]) < 0) {
247 printf("link set xdp fd failed\n");
248 return 1;
249 }
250
251 poll_stats(kill_after_s);
252
253 set_link_xdp_fd(ifindex, -1);
254
255 return 0;
256}
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2016 Facebook
3 */
4#include <linux/bpf.h>
5#include <linux/if_link.h>
6#include <assert.h>
7#include <errno.h>
8#include <signal.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <net/if.h>
13#include <sys/resource.h>
14#include <arpa/inet.h>
15#include <netinet/ether.h>
16#include <unistd.h>
17#include <time.h>
18#include <bpf/libbpf.h>
19#include <bpf/bpf.h>
20#include "bpf_util.h"
21#include "xdp_tx_iptunnel_common.h"
22
23#define STATS_INTERVAL_S 2U
24
25static int ifindex = -1;
26static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
27static int rxcnt_map_fd;
28static __u32 prog_id;
29
30static void int_exit(int sig)
31{
32 __u32 curr_prog_id = 0;
33
34 if (ifindex > -1) {
35 if (bpf_get_link_xdp_id(ifindex, &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, -1, xdp_flags);
41 else if (!curr_prog_id)
42 printf("couldn't find a prog id on a given iface\n");
43 else
44 printf("program on interface changed, not removing\n");
45 }
46 exit(0);
47}
48
49/* simple per-protocol drop counter
50 */
51static void poll_stats(unsigned int kill_after_s)
52{
53 const unsigned int nr_protos = 256;
54 unsigned int nr_cpus = bpf_num_possible_cpus();
55 time_t started_at = time(NULL);
56 __u64 values[nr_cpus], prev[nr_protos][nr_cpus];
57 __u32 proto;
58 int i;
59
60 memset(prev, 0, sizeof(prev));
61
62 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
63 sleep(STATS_INTERVAL_S);
64
65 for (proto = 0; proto < nr_protos; proto++) {
66 __u64 sum = 0;
67
68 assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto,
69 values) == 0);
70 for (i = 0; i < nr_cpus; i++)
71 sum += (values[i] - prev[proto][i]);
72
73 if (sum)
74 printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
75 proto, sum, sum / STATS_INTERVAL_S);
76 memcpy(prev[proto], values, sizeof(values));
77 }
78 }
79}
80
81static void usage(const char *cmd)
82{
83 printf("Start a XDP prog which encapsulates incoming packets\n"
84 "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n"
85 "is used to select packets to encapsulate\n\n");
86 printf("Usage: %s [...]\n", cmd);
87 printf(" -i <ifname|ifindex> Interface\n");
88 printf(" -a <vip-service-address> IPv4 or IPv6\n");
89 printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
90 printf(" -s <source-ip> Used in the IPTunnel header\n");
91 printf(" -d <dest-ip> Used in the IPTunnel header\n");
92 printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
93 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
94 printf(" -P <IP-Protocol> Default is TCP\n");
95 printf(" -S use skb-mode\n");
96 printf(" -N enforce native mode\n");
97 printf(" -F Force loading the XDP prog\n");
98 printf(" -h Display this help\n");
99}
100
101static int parse_ipstr(const char *ipstr, unsigned int *addr)
102{
103 if (inet_pton(AF_INET6, ipstr, addr) == 1) {
104 return AF_INET6;
105 } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
106 addr[1] = addr[2] = addr[3] = 0;
107 return AF_INET;
108 }
109
110 fprintf(stderr, "%s is an invalid IP\n", ipstr);
111 return AF_UNSPEC;
112}
113
114static int parse_ports(const char *port_str, int *min_port, int *max_port)
115{
116 char *end;
117 long tmp_min_port;
118 long tmp_max_port;
119
120 tmp_min_port = strtol(optarg, &end, 10);
121 if (tmp_min_port < 1 || tmp_min_port > 65535) {
122 fprintf(stderr, "Invalid port(s):%s\n", optarg);
123 return 1;
124 }
125
126 if (*end == '-') {
127 end++;
128 tmp_max_port = strtol(end, NULL, 10);
129 if (tmp_max_port < 1 || tmp_max_port > 65535) {
130 fprintf(stderr, "Invalid port(s):%s\n", optarg);
131 return 1;
132 }
133 } else {
134 tmp_max_port = tmp_min_port;
135 }
136
137 if (tmp_min_port > tmp_max_port) {
138 fprintf(stderr, "Invalid port(s):%s\n", optarg);
139 return 1;
140 }
141
142 if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
143 fprintf(stderr, "Port range (%s) is larger than %u\n",
144 port_str, MAX_IPTNL_ENTRIES);
145 return 1;
146 }
147 *min_port = tmp_min_port;
148 *max_port = tmp_max_port;
149
150 return 0;
151}
152
153int main(int argc, char **argv)
154{
155 struct bpf_prog_load_attr prog_load_attr = {
156 .prog_type = BPF_PROG_TYPE_XDP,
157 };
158 int min_port = 0, max_port = 0, vip2tnl_map_fd;
159 const char *optstr = "i:a:p:s:d:m:T:P:FSNh";
160 unsigned char opt_flags[256] = {};
161 struct bpf_prog_info info = {};
162 __u32 info_len = sizeof(info);
163 unsigned int kill_after_s = 0;
164 struct iptnl_info tnl = {};
165 struct bpf_object *obj;
166 struct vip vip = {};
167 char filename[256];
168 int opt, prog_fd;
169 int i, err;
170
171 tnl.family = AF_UNSPEC;
172 vip.protocol = IPPROTO_TCP;
173
174 for (i = 0; i < strlen(optstr); i++)
175 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
176 opt_flags[(unsigned char)optstr[i]] = 1;
177
178 while ((opt = getopt(argc, argv, optstr)) != -1) {
179 unsigned short family;
180 unsigned int *v6;
181
182 switch (opt) {
183 case 'i':
184 ifindex = if_nametoindex(optarg);
185 if (!ifindex)
186 ifindex = atoi(optarg);
187 break;
188 case 'a':
189 vip.family = parse_ipstr(optarg, vip.daddr.v6);
190 if (vip.family == AF_UNSPEC)
191 return 1;
192 break;
193 case 'p':
194 if (parse_ports(optarg, &min_port, &max_port))
195 return 1;
196 break;
197 case 'P':
198 vip.protocol = atoi(optarg);
199 break;
200 case 's':
201 case 'd':
202 if (opt == 's')
203 v6 = tnl.saddr.v6;
204 else
205 v6 = tnl.daddr.v6;
206
207 family = parse_ipstr(optarg, v6);
208 if (family == AF_UNSPEC)
209 return 1;
210 if (tnl.family == AF_UNSPEC) {
211 tnl.family = family;
212 } else if (tnl.family != family) {
213 fprintf(stderr,
214 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
215 return 1;
216 }
217 break;
218 case 'm':
219 if (!ether_aton_r(optarg,
220 (struct ether_addr *)tnl.dmac)) {
221 fprintf(stderr, "Invalid mac address:%s\n",
222 optarg);
223 return 1;
224 }
225 break;
226 case 'T':
227 kill_after_s = atoi(optarg);
228 break;
229 case 'S':
230 xdp_flags |= XDP_FLAGS_SKB_MODE;
231 break;
232 case 'N':
233 /* default, set below */
234 break;
235 case 'F':
236 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
237 break;
238 default:
239 usage(argv[0]);
240 return 1;
241 }
242 opt_flags[opt] = 0;
243 }
244
245 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
246 xdp_flags |= XDP_FLAGS_DRV_MODE;
247
248 for (i = 0; i < strlen(optstr); i++) {
249 if (opt_flags[(unsigned int)optstr[i]]) {
250 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
251 usage(argv[0]);
252 return 1;
253 }
254 }
255
256 if (!ifindex) {
257 fprintf(stderr, "Invalid ifname\n");
258 return 1;
259 }
260
261 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
262 prog_load_attr.file = filename;
263
264 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
265 return 1;
266
267 if (!prog_fd) {
268 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
269 return 1;
270 }
271
272 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
273 vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl");
274 if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) {
275 printf("bpf_object__find_map_fd_by_name failed\n");
276 return 1;
277 }
278
279 signal(SIGINT, int_exit);
280 signal(SIGTERM, int_exit);
281
282 while (min_port <= max_port) {
283 vip.dport = htons(min_port++);
284 if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl,
285 BPF_NOEXIST)) {
286 perror("bpf_map_update_elem(&vip2tnl)");
287 return 1;
288 }
289 }
290
291 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
292 printf("link set xdp fd failed\n");
293 return 1;
294 }
295
296 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
297 if (err) {
298 printf("can't get prog info - %s\n", strerror(errno));
299 return err;
300 }
301 prog_id = info.id;
302
303 poll_stats(kill_after_s);
304
305 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
306
307 return 0;
308}