blob: e746a00d122ee5747117245554761b2f0e28ce13 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -08002/* Copyright (c) 2016 Facebook
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -08003 */
4#include <linux/bpf.h>
David Ahern3993f2c2017-04-27 09:11:13 -07005#include <linux/if_link.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -08006#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 <sys/resource.h>
13#include <arpa/inet.h>
14#include <netinet/ether.h>
15#include <unistd.h>
16#include <time.h>
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010017#include "bpf/libbpf.h"
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070018#include <bpf/bpf.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080019#include "bpf_util.h"
20#include "xdp_tx_iptunnel_common.h"
21
22#define STATS_INTERVAL_S 2U
23
24static int ifindex = -1;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010025static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010026static int rxcnt_map_fd;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010027static __u32 prog_id;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080028
29static void int_exit(int sig)
30{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010031 __u32 curr_prog_id = 0;
32
33 if (ifindex > -1) {
34 if (bpf_get_link_xdp_id(ifindex, &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, -1, xdp_flags);
40 else if (!curr_prog_id)
41 printf("couldn't find a prog id on a given iface\n");
42 else
43 printf("program on interface changed, not removing\n");
44 }
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080045 exit(0);
46}
47
48/* simple per-protocol drop counter
49 */
50static void poll_stats(unsigned int kill_after_s)
51{
52 const unsigned int nr_protos = 256;
53 unsigned int nr_cpus = bpf_num_possible_cpus();
54 time_t started_at = time(NULL);
55 __u64 values[nr_cpus], prev[nr_protos][nr_cpus];
56 __u32 proto;
57 int i;
58
59 memset(prev, 0, sizeof(prev));
60
61 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
62 sleep(STATS_INTERVAL_S);
63
64 for (proto = 0; proto < nr_protos; proto++) {
65 __u64 sum = 0;
66
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +010067 assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto,
68 values) == 0);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080069 for (i = 0; i < nr_cpus; i++)
70 sum += (values[i] - prev[proto][i]);
71
72 if (sum)
73 printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
74 proto, sum, sum / STATS_INTERVAL_S);
75 memcpy(prev[proto], values, sizeof(values));
76 }
77 }
78}
79
80static void usage(const char *cmd)
81{
82 printf("Start a XDP prog which encapsulates incoming packets\n"
83 "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n"
84 "is used to select packets to encapsulate\n\n");
85 printf("Usage: %s [...]\n", cmd);
86 printf(" -i <ifindex> Interface Index\n");
87 printf(" -a <vip-service-address> IPv4 or IPv6\n");
88 printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
89 printf(" -s <source-ip> Used in the IPTunnel header\n");
90 printf(" -d <dest-ip> Used in the IPTunnel header\n");
91 printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
92 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
93 printf(" -P <IP-Protocol> Default is TCP\n");
Daniel Borkmann0489df92017-05-12 01:04:45 +020094 printf(" -S use skb-mode\n");
95 printf(" -N enforce native mode\n");
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010096 printf(" -F Force loading the XDP prog\n");
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080097 printf(" -h Display this help\n");
98}
99
100static int parse_ipstr(const char *ipstr, unsigned int *addr)
101{
102 if (inet_pton(AF_INET6, ipstr, addr) == 1) {
103 return AF_INET6;
104 } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
105 addr[1] = addr[2] = addr[3] = 0;
106 return AF_INET;
107 }
108
109 fprintf(stderr, "%s is an invalid IP\n", ipstr);
110 return AF_UNSPEC;
111}
112
113static int parse_ports(const char *port_str, int *min_port, int *max_port)
114{
115 char *end;
116 long tmp_min_port;
117 long tmp_max_port;
118
119 tmp_min_port = strtol(optarg, &end, 10);
120 if (tmp_min_port < 1 || tmp_min_port > 65535) {
121 fprintf(stderr, "Invalid port(s):%s\n", optarg);
122 return 1;
123 }
124
125 if (*end == '-') {
126 end++;
127 tmp_max_port = strtol(end, NULL, 10);
128 if (tmp_max_port < 1 || tmp_max_port > 65535) {
129 fprintf(stderr, "Invalid port(s):%s\n", optarg);
130 return 1;
131 }
132 } else {
133 tmp_max_port = tmp_min_port;
134 }
135
136 if (tmp_min_port > tmp_max_port) {
137 fprintf(stderr, "Invalid port(s):%s\n", optarg);
138 return 1;
139 }
140
141 if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
142 fprintf(stderr, "Port range (%s) is larger than %u\n",
143 port_str, MAX_IPTNL_ENTRIES);
144 return 1;
145 }
146 *min_port = tmp_min_port;
147 *max_port = tmp_max_port;
148
149 return 0;
150}
151
152int main(int argc, char **argv)
153{
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100154 struct bpf_prog_load_attr prog_load_attr = {
155 .prog_type = BPF_PROG_TYPE_XDP,
156 };
157 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
158 int min_port = 0, max_port = 0, vip2tnl_map_fd;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100159 const char *optstr = "i:a:p:s:d:m:T:P:FSNh";
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800160 unsigned char opt_flags[256] = {};
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100161 struct bpf_prog_info info = {};
162 __u32 info_len = sizeof(info);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800163 unsigned int kill_after_s = 0;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800164 struct iptnl_info tnl = {};
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100165 struct bpf_object *obj;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800166 struct vip vip = {};
167 char filename[256];
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100168 int opt, prog_fd;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100169 int i, err;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800170
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 = atoi(optarg);
185 break;
186 case 'a':
187 vip.family = parse_ipstr(optarg, vip.daddr.v6);
188 if (vip.family == AF_UNSPEC)
189 return 1;
190 break;
191 case 'p':
192 if (parse_ports(optarg, &min_port, &max_port))
193 return 1;
194 break;
195 case 'P':
196 vip.protocol = atoi(optarg);
197 break;
198 case 's':
199 case 'd':
200 if (opt == 's')
201 v6 = tnl.saddr.v6;
202 else
203 v6 = tnl.daddr.v6;
204
205 family = parse_ipstr(optarg, v6);
206 if (family == AF_UNSPEC)
207 return 1;
208 if (tnl.family == AF_UNSPEC) {
209 tnl.family = family;
210 } else if (tnl.family != family) {
211 fprintf(stderr,
212 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
213 return 1;
214 }
215 break;
216 case 'm':
217 if (!ether_aton_r(optarg,
218 (struct ether_addr *)tnl.dmac)) {
219 fprintf(stderr, "Invalid mac address:%s\n",
220 optarg);
221 return 1;
222 }
223 break;
224 case 'T':
225 kill_after_s = atoi(optarg);
226 break;
David Ahern3993f2c2017-04-27 09:11:13 -0700227 case 'S':
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +0200228 xdp_flags |= XDP_FLAGS_SKB_MODE;
David Ahern3993f2c2017-04-27 09:11:13 -0700229 break;
Daniel Borkmann0489df92017-05-12 01:04:45 +0200230 case 'N':
231 xdp_flags |= XDP_FLAGS_DRV_MODE;
232 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100233 case 'F':
234 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
235 break;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800236 default:
237 usage(argv[0]);
238 return 1;
239 }
240 opt_flags[opt] = 0;
241 }
242
243 for (i = 0; i < strlen(optstr); i++) {
244 if (opt_flags[(unsigned int)optstr[i]]) {
245 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
246 usage(argv[0]);
247 return 1;
248 }
249 }
250
251 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
252 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
253 return 1;
254 }
255
256 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100257 prog_load_attr.file = filename;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800258
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100259 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
260 return 1;
261
262 if (!prog_fd) {
263 printf("load_bpf_file: %s\n", strerror(errno));
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800264 return 1;
265 }
266
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100267 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
268 vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl");
269 if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) {
270 printf("bpf_object__find_map_fd_by_name failed\n");
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800271 return 1;
272 }
273
274 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400275 signal(SIGTERM, int_exit);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800276
277 while (min_port <= max_port) {
278 vip.dport = htons(min_port++);
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100279 if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl,
280 BPF_NOEXIST)) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800281 perror("bpf_map_update_elem(&vip2tnl)");
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800282 return 1;
283 }
284 }
285
Maciej Fijalkowskibbaf6022019-02-01 22:42:25 +0100286 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800287 printf("link set xdp fd failed\n");
288 return 1;
289 }
290
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100291 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
292 if (err) {
293 printf("can't get prog info - %s\n", strerror(errno));
294 return err;
295 }
296 prog_id = info.id;
297
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800298 poll_stats(kill_after_s);
299
Eric Leblondb259c2f2018-01-30 21:55:04 +0100300 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800301
302 return 0;
303}