blob: cb2bda7b5346bcc8d98231beafe1196bcdfdf923 [file] [log] [blame]
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -08001/* 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>
David Ahern3993f2c2017-04-27 09:11:13 -07008#include <linux/if_link.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -08009#include <assert.h>
10#include <errno.h>
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/resource.h>
16#include <arpa/inet.h>
17#include <netinet/ether.h>
18#include <unistd.h>
19#include <time.h>
20#include "bpf_load.h"
21#include "libbpf.h"
22#include "bpf_util.h"
23#include "xdp_tx_iptunnel_common.h"
24
25#define STATS_INTERVAL_S 2U
26
27static int ifindex = -1;
28
29static void int_exit(int sig)
30{
31 if (ifindex > -1)
David Ahern3993f2c2017-04-27 09:11:13 -070032 set_link_xdp_fd(ifindex, -1, 0);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080033 exit(0);
34}
35
36/* simple per-protocol drop counter
37 */
38static void poll_stats(unsigned int kill_after_s)
39{
40 const unsigned int nr_protos = 256;
41 unsigned int nr_cpus = bpf_num_possible_cpus();
42 time_t started_at = time(NULL);
43 __u64 values[nr_cpus], prev[nr_protos][nr_cpus];
44 __u32 proto;
45 int i;
46
47 memset(prev, 0, sizeof(prev));
48
49 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
50 sleep(STATS_INTERVAL_S);
51
52 for (proto = 0; proto < nr_protos; proto++) {
53 __u64 sum = 0;
54
Joe Stringerd40fc182016-12-14 14:43:38 -080055 assert(bpf_map_lookup_elem(map_fd[0], &proto, values) == 0);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080056 for (i = 0; i < nr_cpus; i++)
57 sum += (values[i] - prev[proto][i]);
58
59 if (sum)
60 printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
61 proto, sum, sum / STATS_INTERVAL_S);
62 memcpy(prev[proto], values, sizeof(values));
63 }
64 }
65}
66
67static void usage(const char *cmd)
68{
69 printf("Start a XDP prog which encapsulates incoming packets\n"
70 "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n"
71 "is used to select packets to encapsulate\n\n");
72 printf("Usage: %s [...]\n", cmd);
73 printf(" -i <ifindex> Interface Index\n");
74 printf(" -a <vip-service-address> IPv4 or IPv6\n");
75 printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
76 printf(" -s <source-ip> Used in the IPTunnel header\n");
77 printf(" -d <dest-ip> Used in the IPTunnel header\n");
78 printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
79 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
80 printf(" -P <IP-Protocol> Default is TCP\n");
81 printf(" -h Display this help\n");
82}
83
84static int parse_ipstr(const char *ipstr, unsigned int *addr)
85{
86 if (inet_pton(AF_INET6, ipstr, addr) == 1) {
87 return AF_INET6;
88 } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
89 addr[1] = addr[2] = addr[3] = 0;
90 return AF_INET;
91 }
92
93 fprintf(stderr, "%s is an invalid IP\n", ipstr);
94 return AF_UNSPEC;
95}
96
97static int parse_ports(const char *port_str, int *min_port, int *max_port)
98{
99 char *end;
100 long tmp_min_port;
101 long tmp_max_port;
102
103 tmp_min_port = strtol(optarg, &end, 10);
104 if (tmp_min_port < 1 || tmp_min_port > 65535) {
105 fprintf(stderr, "Invalid port(s):%s\n", optarg);
106 return 1;
107 }
108
109 if (*end == '-') {
110 end++;
111 tmp_max_port = strtol(end, NULL, 10);
112 if (tmp_max_port < 1 || tmp_max_port > 65535) {
113 fprintf(stderr, "Invalid port(s):%s\n", optarg);
114 return 1;
115 }
116 } else {
117 tmp_max_port = tmp_min_port;
118 }
119
120 if (tmp_min_port > tmp_max_port) {
121 fprintf(stderr, "Invalid port(s):%s\n", optarg);
122 return 1;
123 }
124
125 if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
126 fprintf(stderr, "Port range (%s) is larger than %u\n",
127 port_str, MAX_IPTNL_ENTRIES);
128 return 1;
129 }
130 *min_port = tmp_min_port;
131 *max_port = tmp_max_port;
132
133 return 0;
134}
135
136int main(int argc, char **argv)
137{
138 unsigned char opt_flags[256] = {};
139 unsigned int kill_after_s = 0;
David Ahern3993f2c2017-04-27 09:11:13 -0700140 const char *optstr = "i:a:p:s:d:m:T:P:Sh";
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800141 int min_port = 0, max_port = 0;
142 struct iptnl_info tnl = {};
143 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
144 struct vip vip = {};
145 char filename[256];
David Ahern3993f2c2017-04-27 09:11:13 -0700146 int flags = 0;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800147 int opt;
148 int i;
149
150 tnl.family = AF_UNSPEC;
151 vip.protocol = IPPROTO_TCP;
152
153 for (i = 0; i < strlen(optstr); i++)
154 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
155 opt_flags[(unsigned char)optstr[i]] = 1;
156
157 while ((opt = getopt(argc, argv, optstr)) != -1) {
158 unsigned short family;
159 unsigned int *v6;
160
161 switch (opt) {
162 case 'i':
163 ifindex = atoi(optarg);
164 break;
165 case 'a':
166 vip.family = parse_ipstr(optarg, vip.daddr.v6);
167 if (vip.family == AF_UNSPEC)
168 return 1;
169 break;
170 case 'p':
171 if (parse_ports(optarg, &min_port, &max_port))
172 return 1;
173 break;
174 case 'P':
175 vip.protocol = atoi(optarg);
176 break;
177 case 's':
178 case 'd':
179 if (opt == 's')
180 v6 = tnl.saddr.v6;
181 else
182 v6 = tnl.daddr.v6;
183
184 family = parse_ipstr(optarg, v6);
185 if (family == AF_UNSPEC)
186 return 1;
187 if (tnl.family == AF_UNSPEC) {
188 tnl.family = family;
189 } else if (tnl.family != family) {
190 fprintf(stderr,
191 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
192 return 1;
193 }
194 break;
195 case 'm':
196 if (!ether_aton_r(optarg,
197 (struct ether_addr *)tnl.dmac)) {
198 fprintf(stderr, "Invalid mac address:%s\n",
199 optarg);
200 return 1;
201 }
202 break;
203 case 'T':
204 kill_after_s = atoi(optarg);
205 break;
David Ahern3993f2c2017-04-27 09:11:13 -0700206 case 'S':
207 flags |= XDP_FLAGS_SKB_MODE;
208 break;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800209 default:
210 usage(argv[0]);
211 return 1;
212 }
213 opt_flags[opt] = 0;
214 }
215
216 for (i = 0; i < strlen(optstr); i++) {
217 if (opt_flags[(unsigned int)optstr[i]]) {
218 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
219 usage(argv[0]);
220 return 1;
221 }
222 }
223
224 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
225 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
226 return 1;
227 }
228
229 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
230
231 if (load_bpf_file(filename)) {
232 printf("%s", bpf_log_buf);
233 return 1;
234 }
235
236 if (!prog_fd[0]) {
237 printf("load_bpf_file: %s\n", strerror(errno));
238 return 1;
239 }
240
241 signal(SIGINT, int_exit);
242
243 while (min_port <= max_port) {
244 vip.dport = htons(min_port++);
Joe Stringerd40fc182016-12-14 14:43:38 -0800245 if (bpf_map_update_elem(map_fd[1], &vip, &tnl, BPF_NOEXIST)) {
246 perror("bpf_map_update_elem(&vip2tnl)");
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800247 return 1;
248 }
249 }
250
David Ahern3993f2c2017-04-27 09:11:13 -0700251 if (set_link_xdp_fd(ifindex, prog_fd[0], flags) < 0) {
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800252 printf("link set xdp fd failed\n");
253 return 1;
254 }
255
256 poll_stats(kill_after_s);
257
David Ahern3993f2c2017-04-27 09:11:13 -0700258 set_link_xdp_fd(ifindex, -1, flags);
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -0800259
260 return 0;
261}