1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
5 #include <linux/if_link.h>
13 #include <sys/resource.h>
14 #include <arpa/inet.h>
15 #include <netinet/ether.h>
21 #include "xdp_tx_iptunnel_common.h"
23 #define STATS_INTERVAL_S 2U
25 static int ifindex
= -1;
26 static __u32 xdp_flags
= XDP_FLAGS_UPDATE_IF_NOEXIST
;
27 static int rxcnt_map_fd
;
30 static void int_exit(int sig
)
32 __u32 curr_prog_id
= 0;
35 if (bpf_get_link_xdp_id(ifindex
, &curr_prog_id
, xdp_flags
)) {
36 printf("bpf_get_link_xdp_id failed\n");
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");
44 printf("program on interface changed, not removing\n");
49 /* simple per-protocol drop counter
51 static void poll_stats(unsigned int kill_after_s
)
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
];
60 memset(prev
, 0, sizeof(prev
));
62 while (!kill_after_s
|| time(NULL
) - started_at
<= kill_after_s
) {
63 sleep(STATS_INTERVAL_S
);
65 for (proto
= 0; proto
< nr_protos
; proto
++) {
68 assert(bpf_map_lookup_elem(rxcnt_map_fd
, &proto
,
70 for (i
= 0; i
< nr_cpus
; i
++)
71 sum
+= (values
[i
] - prev
[proto
][i
]);
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
));
81 static void usage(const char *cmd
)
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");
101 static int parse_ipstr(const char *ipstr
, unsigned int *addr
)
103 if (inet_pton(AF_INET6
, ipstr
, addr
) == 1) {
105 } else if (inet_pton(AF_INET
, ipstr
, addr
) == 1) {
106 addr
[1] = addr
[2] = addr
[3] = 0;
110 fprintf(stderr
, "%s is an invalid IP\n", ipstr
);
114 static int parse_ports(const char *port_str
, int *min_port
, int *max_port
)
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
);
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
);
134 tmp_max_port
= tmp_min_port
;
137 if (tmp_min_port
> tmp_max_port
) {
138 fprintf(stderr
, "Invalid port(s):%s\n", optarg
);
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
);
147 *min_port
= tmp_min_port
;
148 *max_port
= tmp_max_port
;
153 int main(int argc
, char **argv
)
155 struct bpf_prog_load_attr prog_load_attr
= {
156 .prog_type
= BPF_PROG_TYPE_XDP
,
158 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
159 int min_port
= 0, max_port
= 0, vip2tnl_map_fd
;
160 const char *optstr
= "i:a:p:s:d:m:T:P:FSNh";
161 unsigned char opt_flags
[256] = {};
162 struct bpf_prog_info info
= {};
163 __u32 info_len
= sizeof(info
);
164 unsigned int kill_after_s
= 0;
165 struct iptnl_info tnl
= {};
166 struct bpf_object
*obj
;
172 tnl
.family
= AF_UNSPEC
;
173 vip
.protocol
= IPPROTO_TCP
;
175 for (i
= 0; i
< strlen(optstr
); i
++)
176 if (optstr
[i
] != 'h' && 'a' <= optstr
[i
] && optstr
[i
] <= 'z')
177 opt_flags
[(unsigned char)optstr
[i
]] = 1;
179 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
180 unsigned short family
;
185 ifindex
= if_nametoindex(optarg
);
187 ifindex
= atoi(optarg
);
190 vip
.family
= parse_ipstr(optarg
, vip
.daddr
.v6
);
191 if (vip
.family
== AF_UNSPEC
)
195 if (parse_ports(optarg
, &min_port
, &max_port
))
199 vip
.protocol
= atoi(optarg
);
208 family
= parse_ipstr(optarg
, v6
);
209 if (family
== AF_UNSPEC
)
211 if (tnl
.family
== AF_UNSPEC
) {
213 } else if (tnl
.family
!= family
) {
215 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
220 if (!ether_aton_r(optarg
,
221 (struct ether_addr
*)tnl
.dmac
)) {
222 fprintf(stderr
, "Invalid mac address:%s\n",
228 kill_after_s
= atoi(optarg
);
231 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
234 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
237 xdp_flags
&= ~XDP_FLAGS_UPDATE_IF_NOEXIST
;
246 for (i
= 0; i
< strlen(optstr
); i
++) {
247 if (opt_flags
[(unsigned int)optstr
[i
]]) {
248 fprintf(stderr
, "Missing argument -%c\n", optstr
[i
]);
254 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
255 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
260 fprintf(stderr
, "Invalid ifname\n");
264 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
265 prog_load_attr
.file
= filename
;
267 if (bpf_prog_load_xattr(&prog_load_attr
, &obj
, &prog_fd
))
271 printf("load_bpf_file: %s\n", strerror(errno
));
275 rxcnt_map_fd
= bpf_object__find_map_fd_by_name(obj
, "rxcnt");
276 vip2tnl_map_fd
= bpf_object__find_map_fd_by_name(obj
, "vip2tnl");
277 if (vip2tnl_map_fd
< 0 || rxcnt_map_fd
< 0) {
278 printf("bpf_object__find_map_fd_by_name failed\n");
282 signal(SIGINT
, int_exit
);
283 signal(SIGTERM
, int_exit
);
285 while (min_port
<= max_port
) {
286 vip
.dport
= htons(min_port
++);
287 if (bpf_map_update_elem(vip2tnl_map_fd
, &vip
, &tnl
,
289 perror("bpf_map_update_elem(&vip2tnl)");
294 if (bpf_set_link_xdp_fd(ifindex
, prog_fd
, xdp_flags
) < 0) {
295 printf("link set xdp fd failed\n");
299 err
= bpf_obj_get_info_by_fd(prog_fd
, &info
, &info_len
);
301 printf("can't get prog info - %s\n", strerror(errno
));
306 poll_stats(kill_after_s
);
308 bpf_set_link_xdp_fd(ifindex
, -1, xdp_flags
);