Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 1 | /* 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 Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 8 | #include <linux/if_link.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 9 | #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> |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 20 | #include "bpf/libbpf.h" |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 21 | #include <bpf/bpf.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 22 | #include "bpf_util.h" |
| 23 | #include "xdp_tx_iptunnel_common.h" |
| 24 | |
| 25 | #define STATS_INTERVAL_S 2U |
| 26 | |
| 27 | static int ifindex = -1; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 28 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 29 | static int rxcnt_map_fd; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 30 | |
| 31 | static void int_exit(int sig) |
| 32 | { |
| 33 | if (ifindex > -1) |
Eric Leblond | b259c2f | 2018-01-30 21:55:04 +0100 | [diff] [blame] | 34 | bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 35 | exit(0); |
| 36 | } |
| 37 | |
| 38 | /* simple per-protocol drop counter |
| 39 | */ |
| 40 | static void poll_stats(unsigned int kill_after_s) |
| 41 | { |
| 42 | const unsigned int nr_protos = 256; |
| 43 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
| 44 | time_t started_at = time(NULL); |
| 45 | __u64 values[nr_cpus], prev[nr_protos][nr_cpus]; |
| 46 | __u32 proto; |
| 47 | int i; |
| 48 | |
| 49 | memset(prev, 0, sizeof(prev)); |
| 50 | |
| 51 | while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { |
| 52 | sleep(STATS_INTERVAL_S); |
| 53 | |
| 54 | for (proto = 0; proto < nr_protos; proto++) { |
| 55 | __u64 sum = 0; |
| 56 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 57 | assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto, |
| 58 | values) == 0); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 59 | for (i = 0; i < nr_cpus; i++) |
| 60 | sum += (values[i] - prev[proto][i]); |
| 61 | |
| 62 | if (sum) |
| 63 | printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n", |
| 64 | proto, sum, sum / STATS_INTERVAL_S); |
| 65 | memcpy(prev[proto], values, sizeof(values)); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void usage(const char *cmd) |
| 71 | { |
| 72 | printf("Start a XDP prog which encapsulates incoming packets\n" |
| 73 | "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n" |
| 74 | "is used to select packets to encapsulate\n\n"); |
| 75 | printf("Usage: %s [...]\n", cmd); |
| 76 | printf(" -i <ifindex> Interface Index\n"); |
| 77 | printf(" -a <vip-service-address> IPv4 or IPv6\n"); |
| 78 | printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n"); |
| 79 | printf(" -s <source-ip> Used in the IPTunnel header\n"); |
| 80 | printf(" -d <dest-ip> Used in the IPTunnel header\n"); |
| 81 | printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n"); |
| 82 | printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n"); |
| 83 | printf(" -P <IP-Protocol> Default is TCP\n"); |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 84 | printf(" -S use skb-mode\n"); |
| 85 | printf(" -N enforce native mode\n"); |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 86 | printf(" -F Force loading the XDP prog\n"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 87 | printf(" -h Display this help\n"); |
| 88 | } |
| 89 | |
| 90 | static int parse_ipstr(const char *ipstr, unsigned int *addr) |
| 91 | { |
| 92 | if (inet_pton(AF_INET6, ipstr, addr) == 1) { |
| 93 | return AF_INET6; |
| 94 | } else if (inet_pton(AF_INET, ipstr, addr) == 1) { |
| 95 | addr[1] = addr[2] = addr[3] = 0; |
| 96 | return AF_INET; |
| 97 | } |
| 98 | |
| 99 | fprintf(stderr, "%s is an invalid IP\n", ipstr); |
| 100 | return AF_UNSPEC; |
| 101 | } |
| 102 | |
| 103 | static int parse_ports(const char *port_str, int *min_port, int *max_port) |
| 104 | { |
| 105 | char *end; |
| 106 | long tmp_min_port; |
| 107 | long tmp_max_port; |
| 108 | |
| 109 | tmp_min_port = strtol(optarg, &end, 10); |
| 110 | if (tmp_min_port < 1 || tmp_min_port > 65535) { |
| 111 | fprintf(stderr, "Invalid port(s):%s\n", optarg); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | if (*end == '-') { |
| 116 | end++; |
| 117 | tmp_max_port = strtol(end, NULL, 10); |
| 118 | if (tmp_max_port < 1 || tmp_max_port > 65535) { |
| 119 | fprintf(stderr, "Invalid port(s):%s\n", optarg); |
| 120 | return 1; |
| 121 | } |
| 122 | } else { |
| 123 | tmp_max_port = tmp_min_port; |
| 124 | } |
| 125 | |
| 126 | if (tmp_min_port > tmp_max_port) { |
| 127 | fprintf(stderr, "Invalid port(s):%s\n", optarg); |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) { |
| 132 | fprintf(stderr, "Port range (%s) is larger than %u\n", |
| 133 | port_str, MAX_IPTNL_ENTRIES); |
| 134 | return 1; |
| 135 | } |
| 136 | *min_port = tmp_min_port; |
| 137 | *max_port = tmp_max_port; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int main(int argc, char **argv) |
| 143 | { |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 144 | struct bpf_prog_load_attr prog_load_attr = { |
| 145 | .prog_type = BPF_PROG_TYPE_XDP, |
| 146 | }; |
| 147 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
| 148 | int min_port = 0, max_port = 0, vip2tnl_map_fd; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 149 | const char *optstr = "i:a:p:s:d:m:T:P:FSNh"; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 150 | unsigned char opt_flags[256] = {}; |
| 151 | unsigned int kill_after_s = 0; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 152 | struct iptnl_info tnl = {}; |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 153 | struct bpf_object *obj; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 154 | struct vip vip = {}; |
| 155 | char filename[256]; |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 156 | int opt, prog_fd; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 157 | int i; |
| 158 | |
| 159 | tnl.family = AF_UNSPEC; |
| 160 | vip.protocol = IPPROTO_TCP; |
| 161 | |
| 162 | for (i = 0; i < strlen(optstr); i++) |
| 163 | if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z') |
| 164 | opt_flags[(unsigned char)optstr[i]] = 1; |
| 165 | |
| 166 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 167 | unsigned short family; |
| 168 | unsigned int *v6; |
| 169 | |
| 170 | switch (opt) { |
| 171 | case 'i': |
| 172 | ifindex = atoi(optarg); |
| 173 | break; |
| 174 | case 'a': |
| 175 | vip.family = parse_ipstr(optarg, vip.daddr.v6); |
| 176 | if (vip.family == AF_UNSPEC) |
| 177 | return 1; |
| 178 | break; |
| 179 | case 'p': |
| 180 | if (parse_ports(optarg, &min_port, &max_port)) |
| 181 | return 1; |
| 182 | break; |
| 183 | case 'P': |
| 184 | vip.protocol = atoi(optarg); |
| 185 | break; |
| 186 | case 's': |
| 187 | case 'd': |
| 188 | if (opt == 's') |
| 189 | v6 = tnl.saddr.v6; |
| 190 | else |
| 191 | v6 = tnl.daddr.v6; |
| 192 | |
| 193 | family = parse_ipstr(optarg, v6); |
| 194 | if (family == AF_UNSPEC) |
| 195 | return 1; |
| 196 | if (tnl.family == AF_UNSPEC) { |
| 197 | tnl.family = family; |
| 198 | } else if (tnl.family != family) { |
| 199 | fprintf(stderr, |
| 200 | "The IP version of the src and dst addresses used in the IP encapsulation does not match\n"); |
| 201 | return 1; |
| 202 | } |
| 203 | break; |
| 204 | case 'm': |
| 205 | if (!ether_aton_r(optarg, |
| 206 | (struct ether_addr *)tnl.dmac)) { |
| 207 | fprintf(stderr, "Invalid mac address:%s\n", |
| 208 | optarg); |
| 209 | return 1; |
| 210 | } |
| 211 | break; |
| 212 | case 'T': |
| 213 | kill_after_s = atoi(optarg); |
| 214 | break; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 215 | case 'S': |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 216 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 217 | break; |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 218 | case 'N': |
| 219 | xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 220 | break; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 221 | case 'F': |
| 222 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 223 | break; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 224 | default: |
| 225 | usage(argv[0]); |
| 226 | return 1; |
| 227 | } |
| 228 | opt_flags[opt] = 0; |
| 229 | } |
| 230 | |
| 231 | for (i = 0; i < strlen(optstr); i++) { |
| 232 | if (opt_flags[(unsigned int)optstr[i]]) { |
| 233 | fprintf(stderr, "Missing argument -%c\n", optstr[i]); |
| 234 | usage(argv[0]); |
| 235 | return 1; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 240 | perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 245 | prog_load_attr.file = filename; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 246 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 247 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
| 248 | return 1; |
| 249 | |
| 250 | if (!prog_fd) { |
| 251 | printf("load_bpf_file: %s\n", strerror(errno)); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 252 | return 1; |
| 253 | } |
| 254 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 255 | rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt"); |
| 256 | vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl"); |
| 257 | if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) { |
| 258 | printf("bpf_object__find_map_fd_by_name failed\n"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | signal(SIGINT, int_exit); |
Andy Gospodarek | ad990db | 2017-05-11 15:52:30 -0400 | [diff] [blame] | 263 | signal(SIGTERM, int_exit); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 264 | |
| 265 | while (min_port <= max_port) { |
| 266 | vip.dport = htons(min_port++); |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 267 | if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl, |
| 268 | BPF_NOEXIST)) { |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 269 | perror("bpf_map_update_elem(&vip2tnl)"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 270 | return 1; |
| 271 | } |
| 272 | } |
| 273 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 274 | if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 275 | printf("link set xdp fd failed\n"); |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | poll_stats(kill_after_s); |
| 280 | |
Eric Leblond | b259c2f | 2018-01-30 21:55:04 +0100 | [diff] [blame] | 281 | bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |