Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2016 Facebook |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 5 | #include <linux/if_link.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 6 | #include <assert.h> |
| 7 | #include <errno.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Daniel T. Lee | 9e859e8 | 2019-06-25 09:55:36 +0900 | [diff] [blame] | 12 | #include <net/if.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 13 | #include <sys/resource.h> |
| 14 | #include <arpa/inet.h> |
| 15 | #include <netinet/ether.h> |
| 16 | #include <unistd.h> |
| 17 | #include <time.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 18 | #include <bpf/libbpf.h> |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 19 | #include <bpf/bpf.h> |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 20 | #include "bpf_util.h" |
| 21 | #include "xdp_tx_iptunnel_common.h" |
| 22 | |
| 23 | #define STATS_INTERVAL_S 2U |
| 24 | |
| 25 | static int ifindex = -1; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 26 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 27 | static int rxcnt_map_fd; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 28 | static __u32 prog_id; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 29 | |
| 30 | static void int_exit(int sig) |
| 31 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 32 | __u32 curr_prog_id = 0; |
| 33 | |
| 34 | if (ifindex > -1) { |
| 35 | if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) { |
| 36 | printf("bpf_get_link_xdp_id failed\n"); |
| 37 | exit(1); |
| 38 | } |
| 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"); |
| 43 | else |
| 44 | printf("program on interface changed, not removing\n"); |
| 45 | } |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 46 | exit(0); |
| 47 | } |
| 48 | |
| 49 | /* simple per-protocol drop counter |
| 50 | */ |
| 51 | static void poll_stats(unsigned int kill_after_s) |
| 52 | { |
| 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]; |
| 57 | __u32 proto; |
| 58 | int i; |
| 59 | |
| 60 | memset(prev, 0, sizeof(prev)); |
| 61 | |
| 62 | while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { |
| 63 | sleep(STATS_INTERVAL_S); |
| 64 | |
| 65 | for (proto = 0; proto < nr_protos; proto++) { |
| 66 | __u64 sum = 0; |
| 67 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 68 | assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto, |
| 69 | values) == 0); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 70 | for (i = 0; i < nr_cpus; i++) |
| 71 | sum += (values[i] - prev[proto][i]); |
| 72 | |
| 73 | if (sum) |
| 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)); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void usage(const char *cmd) |
| 82 | { |
| 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); |
Daniel T. Lee | 9e859e8 | 2019-06-25 09:55:36 +0900 | [diff] [blame] | 87 | printf(" -i <ifname|ifindex> Interface\n"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 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"); |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 95 | printf(" -S use skb-mode\n"); |
| 96 | printf(" -N enforce native mode\n"); |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 97 | printf(" -F Force loading the XDP prog\n"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 98 | printf(" -h Display this help\n"); |
| 99 | } |
| 100 | |
| 101 | static int parse_ipstr(const char *ipstr, unsigned int *addr) |
| 102 | { |
| 103 | if (inet_pton(AF_INET6, ipstr, addr) == 1) { |
| 104 | return AF_INET6; |
| 105 | } else if (inet_pton(AF_INET, ipstr, addr) == 1) { |
| 106 | addr[1] = addr[2] = addr[3] = 0; |
| 107 | return AF_INET; |
| 108 | } |
| 109 | |
| 110 | fprintf(stderr, "%s is an invalid IP\n", ipstr); |
| 111 | return AF_UNSPEC; |
| 112 | } |
| 113 | |
| 114 | static int parse_ports(const char *port_str, int *min_port, int *max_port) |
| 115 | { |
| 116 | char *end; |
| 117 | long tmp_min_port; |
| 118 | long tmp_max_port; |
| 119 | |
| 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); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | if (*end == '-') { |
| 127 | end++; |
| 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); |
| 131 | return 1; |
| 132 | } |
| 133 | } else { |
| 134 | tmp_max_port = tmp_min_port; |
| 135 | } |
| 136 | |
| 137 | if (tmp_min_port > tmp_max_port) { |
| 138 | fprintf(stderr, "Invalid port(s):%s\n", optarg); |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 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); |
| 145 | return 1; |
| 146 | } |
| 147 | *min_port = tmp_min_port; |
| 148 | *max_port = tmp_max_port; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int main(int argc, char **argv) |
| 154 | { |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 155 | struct bpf_prog_load_attr prog_load_attr = { |
| 156 | .prog_type = BPF_PROG_TYPE_XDP, |
| 157 | }; |
| 158 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
| 159 | int min_port = 0, max_port = 0, vip2tnl_map_fd; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 160 | 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] | 161 | unsigned char opt_flags[256] = {}; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 162 | struct bpf_prog_info info = {}; |
| 163 | __u32 info_len = sizeof(info); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 164 | unsigned int kill_after_s = 0; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 165 | struct iptnl_info tnl = {}; |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 166 | struct bpf_object *obj; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 167 | struct vip vip = {}; |
| 168 | char filename[256]; |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 169 | int opt, prog_fd; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 170 | int i, err; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 171 | |
| 172 | tnl.family = AF_UNSPEC; |
| 173 | vip.protocol = IPPROTO_TCP; |
| 174 | |
| 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; |
| 178 | |
| 179 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 180 | unsigned short family; |
| 181 | unsigned int *v6; |
| 182 | |
| 183 | switch (opt) { |
| 184 | case 'i': |
Daniel T. Lee | 9e859e8 | 2019-06-25 09:55:36 +0900 | [diff] [blame] | 185 | ifindex = if_nametoindex(optarg); |
| 186 | if (!ifindex) |
| 187 | ifindex = atoi(optarg); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 188 | break; |
| 189 | case 'a': |
| 190 | vip.family = parse_ipstr(optarg, vip.daddr.v6); |
| 191 | if (vip.family == AF_UNSPEC) |
| 192 | return 1; |
| 193 | break; |
| 194 | case 'p': |
| 195 | if (parse_ports(optarg, &min_port, &max_port)) |
| 196 | return 1; |
| 197 | break; |
| 198 | case 'P': |
| 199 | vip.protocol = atoi(optarg); |
| 200 | break; |
| 201 | case 's': |
| 202 | case 'd': |
| 203 | if (opt == 's') |
| 204 | v6 = tnl.saddr.v6; |
| 205 | else |
| 206 | v6 = tnl.daddr.v6; |
| 207 | |
| 208 | family = parse_ipstr(optarg, v6); |
| 209 | if (family == AF_UNSPEC) |
| 210 | return 1; |
| 211 | if (tnl.family == AF_UNSPEC) { |
| 212 | tnl.family = family; |
| 213 | } else if (tnl.family != family) { |
| 214 | fprintf(stderr, |
| 215 | "The IP version of the src and dst addresses used in the IP encapsulation does not match\n"); |
| 216 | return 1; |
| 217 | } |
| 218 | break; |
| 219 | case 'm': |
| 220 | if (!ether_aton_r(optarg, |
| 221 | (struct ether_addr *)tnl.dmac)) { |
| 222 | fprintf(stderr, "Invalid mac address:%s\n", |
| 223 | optarg); |
| 224 | return 1; |
| 225 | } |
| 226 | break; |
| 227 | case 'T': |
| 228 | kill_after_s = atoi(optarg); |
| 229 | break; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 230 | case 'S': |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 231 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 232 | break; |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 233 | case 'N': |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 234 | /* default, set below */ |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 235 | break; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 236 | case 'F': |
| 237 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 238 | break; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 239 | default: |
| 240 | usage(argv[0]); |
| 241 | return 1; |
| 242 | } |
| 243 | opt_flags[opt] = 0; |
| 244 | } |
| 245 | |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 246 | if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
| 247 | xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 248 | |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 249 | for (i = 0; i < strlen(optstr); i++) { |
| 250 | if (opt_flags[(unsigned int)optstr[i]]) { |
| 251 | fprintf(stderr, "Missing argument -%c\n", optstr[i]); |
| 252 | usage(argv[0]); |
| 253 | return 1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 258 | perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); |
| 259 | return 1; |
| 260 | } |
| 261 | |
Daniel T. Lee | 9e859e8 | 2019-06-25 09:55:36 +0900 | [diff] [blame] | 262 | if (!ifindex) { |
| 263 | fprintf(stderr, "Invalid ifname\n"); |
| 264 | return 1; |
| 265 | } |
| 266 | |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 267 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 268 | prog_load_attr.file = filename; |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 269 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 270 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
| 271 | return 1; |
| 272 | |
| 273 | if (!prog_fd) { |
Daniel T. Lee | afbe3c2 | 2019-11-07 09:51:52 +0900 | [diff] [blame] | 274 | printf("bpf_prog_load_xattr: %s\n", strerror(errno)); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 275 | return 1; |
| 276 | } |
| 277 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 278 | rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt"); |
| 279 | vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl"); |
| 280 | if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) { |
| 281 | printf("bpf_object__find_map_fd_by_name failed\n"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | signal(SIGINT, int_exit); |
Andy Gospodarek | ad990db | 2017-05-11 15:52:30 -0400 | [diff] [blame] | 286 | signal(SIGTERM, int_exit); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 287 | |
| 288 | while (min_port <= max_port) { |
| 289 | vip.dport = htons(min_port++); |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 290 | if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl, |
| 291 | BPF_NOEXIST)) { |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 292 | perror("bpf_map_update_elem(&vip2tnl)"); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 293 | return 1; |
| 294 | } |
| 295 | } |
| 296 | |
Maciej Fijalkowski | bbaf602 | 2019-02-01 22:42:25 +0100 | [diff] [blame] | 297 | 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] | 298 | printf("link set xdp fd failed\n"); |
| 299 | return 1; |
| 300 | } |
| 301 | |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 302 | err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); |
| 303 | if (err) { |
| 304 | printf("can't get prog info - %s\n", strerror(errno)); |
| 305 | return err; |
| 306 | } |
| 307 | prog_id = info.id; |
| 308 | |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 309 | poll_stats(kill_after_s); |
| 310 | |
Eric Leblond | b259c2f | 2018-01-30 21:55:04 +0100 | [diff] [blame] | 311 | bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
Martin KaFai Lau | 12d8bb6 | 2016-12-07 15:53:14 -0800 | [diff] [blame] | 312 | |
| 313 | return 0; |
| 314 | } |