1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
5 #include <linux/if_link.h>
13 #include <arpa/inet.h>
14 #include <netinet/ether.h>
17 #include <bpf/libbpf.h>
20 #include "xdp_tx_iptunnel_common.h"
22 #define STATS_INTERVAL_S 2U
24 static int ifindex = -1;
25 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
26 static int rxcnt_map_fd;
29 static void int_exit(int sig)
31 __u32 curr_prog_id = 0;
34 if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
35 printf("bpf_xdp_query_id failed\n");
38 if (prog_id == curr_prog_id)
39 bpf_xdp_detach(ifindex, xdp_flags, NULL);
40 else if (!curr_prog_id)
41 printf("couldn't find a prog id on a given iface\n");
43 printf("program on interface changed, not removing\n");
48 /* simple per-protocol drop counter
50 static void poll_stats(unsigned int kill_after_s)
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];
59 memset(prev, 0, sizeof(prev));
61 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
62 sleep(STATS_INTERVAL_S);
64 for (proto = 0; proto < nr_protos; proto++) {
67 assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto,
69 for (i = 0; i < nr_cpus; i++)
70 sum += (values[i] - prev[proto][i]);
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));
80 static void usage(const char *cmd)
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 <ifname|ifindex> Interface\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");
94 printf(" -S use skb-mode\n");
95 printf(" -N enforce native mode\n");
96 printf(" -F Force loading the XDP prog\n");
97 printf(" -h Display this help\n");
100 static int parse_ipstr(const char *ipstr, unsigned int *addr)
102 if (inet_pton(AF_INET6, ipstr, addr) == 1) {
104 } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
105 addr[1] = addr[2] = addr[3] = 0;
109 fprintf(stderr, "%s is an invalid IP\n", ipstr);
113 static int parse_ports(const char *port_str, int *min_port, int *max_port)
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);
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);
133 tmp_max_port = tmp_min_port;
136 if (tmp_min_port > tmp_max_port) {
137 fprintf(stderr, "Invalid port(s):%s\n", optarg);
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);
146 *min_port = tmp_min_port;
147 *max_port = tmp_max_port;
152 int main(int argc, char **argv)
154 int min_port = 0, max_port = 0, vip2tnl_map_fd;
155 const char *optstr = "i:a:p:s:d:m:T:P:FSNh";
156 unsigned char opt_flags[256] = {};
157 struct bpf_prog_info info = {};
158 __u32 info_len = sizeof(info);
159 unsigned int kill_after_s = 0;
160 struct iptnl_info tnl = {};
161 struct bpf_program *prog;
162 struct bpf_object *obj;
168 tnl.family = AF_UNSPEC;
169 vip.protocol = IPPROTO_TCP;
171 for (i = 0; i < strlen(optstr); i++)
172 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
173 opt_flags[(unsigned char)optstr[i]] = 1;
175 while ((opt = getopt(argc, argv, optstr)) != -1) {
176 unsigned short family;
181 ifindex = if_nametoindex(optarg);
183 ifindex = atoi(optarg);
186 vip.family = parse_ipstr(optarg, vip.daddr.v6);
187 if (vip.family == AF_UNSPEC)
191 if (parse_ports(optarg, &min_port, &max_port))
195 vip.protocol = atoi(optarg);
204 family = parse_ipstr(optarg, v6);
205 if (family == AF_UNSPEC)
207 if (tnl.family == AF_UNSPEC) {
209 } else if (tnl.family != family) {
211 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
216 if (!ether_aton_r(optarg,
217 (struct ether_addr *)tnl.dmac)) {
218 fprintf(stderr, "Invalid mac address:%s\n",
224 kill_after_s = atoi(optarg);
227 xdp_flags |= XDP_FLAGS_SKB_MODE;
230 /* default, set below */
233 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
242 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
243 xdp_flags |= XDP_FLAGS_DRV_MODE;
245 for (i = 0; i < strlen(optstr); i++) {
246 if (opt_flags[(unsigned int)optstr[i]]) {
247 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
254 fprintf(stderr, "Invalid ifname\n");
258 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
260 obj = bpf_object__open_file(filename, NULL);
261 if (libbpf_get_error(obj))
264 prog = bpf_object__next_program(obj, NULL);
265 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
267 err = bpf_object__load(obj);
269 printf("bpf_object__load(): %s\n", strerror(errno));
272 prog_fd = bpf_program__fd(prog);
274 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
275 vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl");
276 if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) {
277 printf("bpf_object__find_map_fd_by_name failed\n");
281 signal(SIGINT, int_exit);
282 signal(SIGTERM, int_exit);
284 while (min_port <= max_port) {
285 vip.dport = htons(min_port++);
286 if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl,
288 perror("bpf_map_update_elem(&vip2tnl)");
293 if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
294 printf("link set xdp fd failed\n");
298 err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
300 printf("can't get prog info - %s\n", strerror(errno));
305 poll_stats(kill_after_s);
307 bpf_xdp_detach(ifindex, xdp_flags, NULL);