0001
0002
0003
0004 #include <linux/bpf.h>
0005 #include <linux/if_link.h>
0006 #include <assert.h>
0007 #include <errno.h>
0008 #include <signal.h>
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <net/if.h>
0013 #include <arpa/inet.h>
0014 #include <netinet/ether.h>
0015 #include <unistd.h>
0016 #include <time.h>
0017 #include <bpf/libbpf.h>
0018 #include <bpf/bpf.h>
0019 #include "bpf_util.h"
0020 #include "xdp_tx_iptunnel_common.h"
0021
0022 #define STATS_INTERVAL_S 2U
0023
0024 static int ifindex = -1;
0025 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
0026 static int rxcnt_map_fd;
0027 static __u32 prog_id;
0028
0029 static void int_exit(int sig)
0030 {
0031 __u32 curr_prog_id = 0;
0032
0033 if (ifindex > -1) {
0034 if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
0035 printf("bpf_xdp_query_id failed\n");
0036 exit(1);
0037 }
0038 if (prog_id == curr_prog_id)
0039 bpf_xdp_detach(ifindex, xdp_flags, NULL);
0040 else if (!curr_prog_id)
0041 printf("couldn't find a prog id on a given iface\n");
0042 else
0043 printf("program on interface changed, not removing\n");
0044 }
0045 exit(0);
0046 }
0047
0048
0049
0050 static void poll_stats(unsigned int kill_after_s)
0051 {
0052 const unsigned int nr_protos = 256;
0053 unsigned int nr_cpus = bpf_num_possible_cpus();
0054 time_t started_at = time(NULL);
0055 __u64 values[nr_cpus], prev[nr_protos][nr_cpus];
0056 __u32 proto;
0057 int i;
0058
0059 memset(prev, 0, sizeof(prev));
0060
0061 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
0062 sleep(STATS_INTERVAL_S);
0063
0064 for (proto = 0; proto < nr_protos; proto++) {
0065 __u64 sum = 0;
0066
0067 assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto,
0068 values) == 0);
0069 for (i = 0; i < nr_cpus; i++)
0070 sum += (values[i] - prev[proto][i]);
0071
0072 if (sum)
0073 printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
0074 proto, sum, sum / STATS_INTERVAL_S);
0075 memcpy(prev[proto], values, sizeof(values));
0076 }
0077 }
0078 }
0079
0080 static void usage(const char *cmd)
0081 {
0082 printf("Start a XDP prog which encapsulates incoming packets\n"
0083 "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n"
0084 "is used to select packets to encapsulate\n\n");
0085 printf("Usage: %s [...]\n", cmd);
0086 printf(" -i <ifname|ifindex> Interface\n");
0087 printf(" -a <vip-service-address> IPv4 or IPv6\n");
0088 printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
0089 printf(" -s <source-ip> Used in the IPTunnel header\n");
0090 printf(" -d <dest-ip> Used in the IPTunnel header\n");
0091 printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
0092 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
0093 printf(" -P <IP-Protocol> Default is TCP\n");
0094 printf(" -S use skb-mode\n");
0095 printf(" -N enforce native mode\n");
0096 printf(" -F Force loading the XDP prog\n");
0097 printf(" -h Display this help\n");
0098 }
0099
0100 static int parse_ipstr(const char *ipstr, unsigned int *addr)
0101 {
0102 if (inet_pton(AF_INET6, ipstr, addr) == 1) {
0103 return AF_INET6;
0104 } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
0105 addr[1] = addr[2] = addr[3] = 0;
0106 return AF_INET;
0107 }
0108
0109 fprintf(stderr, "%s is an invalid IP\n", ipstr);
0110 return AF_UNSPEC;
0111 }
0112
0113 static int parse_ports(const char *port_str, int *min_port, int *max_port)
0114 {
0115 char *end;
0116 long tmp_min_port;
0117 long tmp_max_port;
0118
0119 tmp_min_port = strtol(optarg, &end, 10);
0120 if (tmp_min_port < 1 || tmp_min_port > 65535) {
0121 fprintf(stderr, "Invalid port(s):%s\n", optarg);
0122 return 1;
0123 }
0124
0125 if (*end == '-') {
0126 end++;
0127 tmp_max_port = strtol(end, NULL, 10);
0128 if (tmp_max_port < 1 || tmp_max_port > 65535) {
0129 fprintf(stderr, "Invalid port(s):%s\n", optarg);
0130 return 1;
0131 }
0132 } else {
0133 tmp_max_port = tmp_min_port;
0134 }
0135
0136 if (tmp_min_port > tmp_max_port) {
0137 fprintf(stderr, "Invalid port(s):%s\n", optarg);
0138 return 1;
0139 }
0140
0141 if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
0142 fprintf(stderr, "Port range (%s) is larger than %u\n",
0143 port_str, MAX_IPTNL_ENTRIES);
0144 return 1;
0145 }
0146 *min_port = tmp_min_port;
0147 *max_port = tmp_max_port;
0148
0149 return 0;
0150 }
0151
0152 int main(int argc, char **argv)
0153 {
0154 int min_port = 0, max_port = 0, vip2tnl_map_fd;
0155 const char *optstr = "i:a:p:s:d:m:T:P:FSNh";
0156 unsigned char opt_flags[256] = {};
0157 struct bpf_prog_info info = {};
0158 __u32 info_len = sizeof(info);
0159 unsigned int kill_after_s = 0;
0160 struct iptnl_info tnl = {};
0161 struct bpf_program *prog;
0162 struct bpf_object *obj;
0163 struct vip vip = {};
0164 char filename[256];
0165 int opt, prog_fd;
0166 int i, err;
0167
0168 tnl.family = AF_UNSPEC;
0169 vip.protocol = IPPROTO_TCP;
0170
0171 for (i = 0; i < strlen(optstr); i++)
0172 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
0173 opt_flags[(unsigned char)optstr[i]] = 1;
0174
0175 while ((opt = getopt(argc, argv, optstr)) != -1) {
0176 unsigned short family;
0177 unsigned int *v6;
0178
0179 switch (opt) {
0180 case 'i':
0181 ifindex = if_nametoindex(optarg);
0182 if (!ifindex)
0183 ifindex = atoi(optarg);
0184 break;
0185 case 'a':
0186 vip.family = parse_ipstr(optarg, vip.daddr.v6);
0187 if (vip.family == AF_UNSPEC)
0188 return 1;
0189 break;
0190 case 'p':
0191 if (parse_ports(optarg, &min_port, &max_port))
0192 return 1;
0193 break;
0194 case 'P':
0195 vip.protocol = atoi(optarg);
0196 break;
0197 case 's':
0198 case 'd':
0199 if (opt == 's')
0200 v6 = tnl.saddr.v6;
0201 else
0202 v6 = tnl.daddr.v6;
0203
0204 family = parse_ipstr(optarg, v6);
0205 if (family == AF_UNSPEC)
0206 return 1;
0207 if (tnl.family == AF_UNSPEC) {
0208 tnl.family = family;
0209 } else if (tnl.family != family) {
0210 fprintf(stderr,
0211 "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
0212 return 1;
0213 }
0214 break;
0215 case 'm':
0216 if (!ether_aton_r(optarg,
0217 (struct ether_addr *)tnl.dmac)) {
0218 fprintf(stderr, "Invalid mac address:%s\n",
0219 optarg);
0220 return 1;
0221 }
0222 break;
0223 case 'T':
0224 kill_after_s = atoi(optarg);
0225 break;
0226 case 'S':
0227 xdp_flags |= XDP_FLAGS_SKB_MODE;
0228 break;
0229 case 'N':
0230
0231 break;
0232 case 'F':
0233 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
0234 break;
0235 default:
0236 usage(argv[0]);
0237 return 1;
0238 }
0239 opt_flags[opt] = 0;
0240 }
0241
0242 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
0243 xdp_flags |= XDP_FLAGS_DRV_MODE;
0244
0245 for (i = 0; i < strlen(optstr); i++) {
0246 if (opt_flags[(unsigned int)optstr[i]]) {
0247 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
0248 usage(argv[0]);
0249 return 1;
0250 }
0251 }
0252
0253 if (!ifindex) {
0254 fprintf(stderr, "Invalid ifname\n");
0255 return 1;
0256 }
0257
0258 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0259
0260 obj = bpf_object__open_file(filename, NULL);
0261 if (libbpf_get_error(obj))
0262 return 1;
0263
0264 prog = bpf_object__next_program(obj, NULL);
0265 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
0266
0267 err = bpf_object__load(obj);
0268 if (err) {
0269 printf("bpf_object__load(): %s\n", strerror(errno));
0270 return 1;
0271 }
0272 prog_fd = bpf_program__fd(prog);
0273
0274 rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
0275 vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl");
0276 if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) {
0277 printf("bpf_object__find_map_fd_by_name failed\n");
0278 return 1;
0279 }
0280
0281 signal(SIGINT, int_exit);
0282 signal(SIGTERM, int_exit);
0283
0284 while (min_port <= max_port) {
0285 vip.dport = htons(min_port++);
0286 if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl,
0287 BPF_NOEXIST)) {
0288 perror("bpf_map_update_elem(&vip2tnl)");
0289 return 1;
0290 }
0291 }
0292
0293 if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
0294 printf("link set xdp fd failed\n");
0295 return 1;
0296 }
0297
0298 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0299 if (err) {
0300 printf("can't get prog info - %s\n", strerror(errno));
0301 return err;
0302 }
0303 prog_id = info.id;
0304
0305 poll_stats(kill_after_s);
0306
0307 bpf_xdp_detach(ifindex, xdp_flags, NULL);
0308
0309 return 0;
0310 }