Blame view
samples/bpf/xdp_tx_iptunnel_user.c
7.4 KB
25763b3c8 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
12d8bb64e bpf: xdp: Add XDP... |
2 |
/* Copyright (c) 2016 Facebook |
12d8bb64e bpf: xdp: Add XDP... |
3 4 |
*/ #include <linux/bpf.h> |
3993f2cb9 samples/bpf: Add ... |
5 |
#include <linux/if_link.h> |
12d8bb64e bpf: xdp: Add XDP... |
6 7 8 9 10 11 |
#include <assert.h> #include <errno.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> |
9e859e8f1 samples: bpf: mak... |
12 |
#include <net/if.h> |
12d8bb64e bpf: xdp: Add XDP... |
13 14 15 16 17 |
#include <sys/resource.h> #include <arpa/inet.h> #include <netinet/ether.h> #include <unistd.h> #include <time.h> |
4d18f6de6 samples: bpf: ref... |
18 |
#include "libbpf.h" |
2bf3e2ef4 samples: bpf: inc... |
19 |
#include <bpf/bpf.h> |
12d8bb64e bpf: xdp: Add XDP... |
20 21 22 23 24 25 |
#include "bpf_util.h" #include "xdp_tx_iptunnel_common.h" #define STATS_INTERVAL_S 2U static int ifindex = -1; |
743e568c1 samples/bpf: Add ... |
26 |
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
bbaf6029c samples/bpf: Conv... |
27 |
static int rxcnt_map_fd; |
3b7a8ec2d samples/bpf: Chec... |
28 |
static __u32 prog_id; |
12d8bb64e bpf: xdp: Add XDP... |
29 30 31 |
static void int_exit(int sig) { |
3b7a8ec2d samples/bpf: Chec... |
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
__u32 curr_prog_id = 0; if (ifindex > -1) { if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) { printf("bpf_get_link_xdp_id failed "); exit(1); } if (prog_id == curr_prog_id) bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); else if (!curr_prog_id) printf("couldn't find a prog id on a given iface "); else printf("program on interface changed, not removing "); } |
12d8bb64e bpf: xdp: Add XDP... |
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
exit(0); } /* simple per-protocol drop counter */ static void poll_stats(unsigned int kill_after_s) { const unsigned int nr_protos = 256; unsigned int nr_cpus = bpf_num_possible_cpus(); time_t started_at = time(NULL); __u64 values[nr_cpus], prev[nr_protos][nr_cpus]; __u32 proto; int i; memset(prev, 0, sizeof(prev)); while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { sleep(STATS_INTERVAL_S); for (proto = 0; proto < nr_protos; proto++) { __u64 sum = 0; |
bbaf6029c samples/bpf: Conv... |
70 71 |
assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto, values) == 0); |
12d8bb64e bpf: xdp: Add XDP... |
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
for (i = 0; i < nr_cpus; i++) sum += (values[i] - prev[proto][i]); if (sum) printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s ", proto, sum, sum / STATS_INTERVAL_S); memcpy(prev[proto], values, sizeof(values)); } } } static void usage(const char *cmd) { printf("Start a XDP prog which encapsulates incoming packets " "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT> " "is used to select packets to encapsulate "); printf("Usage: %s [...] ", cmd); |
9e859e8f1 samples: bpf: mak... |
95 96 |
printf(" -i <ifname|ifindex> Interface "); |
12d8bb64e bpf: xdp: Add XDP... |
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
printf(" -a <vip-service-address> IPv4 or IPv6 "); printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed "); printf(" -s <source-ip> Used in the IPTunnel header "); printf(" -d <dest-ip> Used in the IPTunnel header "); printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt "); printf(" -T <stop-after-X-seconds> Default: 0 (forever) "); printf(" -P <IP-Protocol> Default is TCP "); |
0489df9a4 xdp: add flag to ... |
111 112 113 114 |
printf(" -S use skb-mode "); printf(" -N enforce native mode "); |
743e568c1 samples/bpf: Add ... |
115 116 |
printf(" -F Force loading the XDP prog "); |
12d8bb64e bpf: xdp: Add XDP... |
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
printf(" -h Display this help "); } static int parse_ipstr(const char *ipstr, unsigned int *addr) { if (inet_pton(AF_INET6, ipstr, addr) == 1) { return AF_INET6; } else if (inet_pton(AF_INET, ipstr, addr) == 1) { addr[1] = addr[2] = addr[3] = 0; return AF_INET; } fprintf(stderr, "%s is an invalid IP ", ipstr); return AF_UNSPEC; } static int parse_ports(const char *port_str, int *min_port, int *max_port) { char *end; long tmp_min_port; long tmp_max_port; tmp_min_port = strtol(optarg, &end, 10); if (tmp_min_port < 1 || tmp_min_port > 65535) { fprintf(stderr, "Invalid port(s):%s ", optarg); return 1; } if (*end == '-') { end++; tmp_max_port = strtol(end, NULL, 10); if (tmp_max_port < 1 || tmp_max_port > 65535) { fprintf(stderr, "Invalid port(s):%s ", optarg); return 1; } } else { tmp_max_port = tmp_min_port; } if (tmp_min_port > tmp_max_port) { fprintf(stderr, "Invalid port(s):%s ", optarg); return 1; } if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) { fprintf(stderr, "Port range (%s) is larger than %u ", port_str, MAX_IPTNL_ENTRIES); return 1; } *min_port = tmp_min_port; *max_port = tmp_max_port; return 0; } int main(int argc, char **argv) { |
bbaf6029c samples/bpf: Conv... |
180 181 182 183 184 |
struct bpf_prog_load_attr prog_load_attr = { .prog_type = BPF_PROG_TYPE_XDP, }; struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; int min_port = 0, max_port = 0, vip2tnl_map_fd; |
743e568c1 samples/bpf: Add ... |
185 |
const char *optstr = "i:a:p:s:d:m:T:P:FSNh"; |
12d8bb64e bpf: xdp: Add XDP... |
186 |
unsigned char opt_flags[256] = {}; |
3b7a8ec2d samples/bpf: Chec... |
187 188 |
struct bpf_prog_info info = {}; __u32 info_len = sizeof(info); |
12d8bb64e bpf: xdp: Add XDP... |
189 |
unsigned int kill_after_s = 0; |
12d8bb64e bpf: xdp: Add XDP... |
190 |
struct iptnl_info tnl = {}; |
bbaf6029c samples/bpf: Conv... |
191 |
struct bpf_object *obj; |
12d8bb64e bpf: xdp: Add XDP... |
192 193 |
struct vip vip = {}; char filename[256]; |
bbaf6029c samples/bpf: Conv... |
194 |
int opt, prog_fd; |
3b7a8ec2d samples/bpf: Chec... |
195 |
int i, err; |
12d8bb64e bpf: xdp: Add XDP... |
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
tnl.family = AF_UNSPEC; vip.protocol = IPPROTO_TCP; for (i = 0; i < strlen(optstr); i++) if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z') opt_flags[(unsigned char)optstr[i]] = 1; while ((opt = getopt(argc, argv, optstr)) != -1) { unsigned short family; unsigned int *v6; switch (opt) { case 'i': |
9e859e8f1 samples: bpf: mak... |
210 211 212 |
ifindex = if_nametoindex(optarg); if (!ifindex) ifindex = atoi(optarg); |
12d8bb64e bpf: xdp: Add XDP... |
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
break; case 'a': vip.family = parse_ipstr(optarg, vip.daddr.v6); if (vip.family == AF_UNSPEC) return 1; break; case 'p': if (parse_ports(optarg, &min_port, &max_port)) return 1; break; case 'P': vip.protocol = atoi(optarg); break; case 's': case 'd': if (opt == 's') v6 = tnl.saddr.v6; else v6 = tnl.daddr.v6; family = parse_ipstr(optarg, v6); if (family == AF_UNSPEC) return 1; if (tnl.family == AF_UNSPEC) { tnl.family = family; } else if (tnl.family != family) { fprintf(stderr, "The IP version of the src and dst addresses used in the IP encapsulation does not match "); return 1; } break; case 'm': if (!ether_aton_r(optarg, (struct ether_addr *)tnl.dmac)) { fprintf(stderr, "Invalid mac address:%s ", optarg); return 1; } break; case 'T': kill_after_s = atoi(optarg); break; |
3993f2cb9 samples/bpf: Add ... |
257 |
case 'S': |
6387d0111 samples/bpf: fix ... |
258 |
xdp_flags |= XDP_FLAGS_SKB_MODE; |
0489df9a4 xdp: add flag to ... |
259 260 261 |
break; case 'N': xdp_flags |= XDP_FLAGS_DRV_MODE; |
3993f2cb9 samples/bpf: Add ... |
262 |
break; |
743e568c1 samples/bpf: Add ... |
263 264 265 |
case 'F': xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; break; |
12d8bb64e bpf: xdp: Add XDP... |
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
default: usage(argv[0]); return 1; } opt_flags[opt] = 0; } for (i = 0; i < strlen(optstr); i++) { if (opt_flags[(unsigned int)optstr[i]]) { fprintf(stderr, "Missing argument -%c ", optstr[i]); usage(argv[0]); return 1; } } if (setrlimit(RLIMIT_MEMLOCK, &r)) { perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); return 1; } |
9e859e8f1 samples: bpf: mak... |
286 287 288 289 290 |
if (!ifindex) { fprintf(stderr, "Invalid ifname "); return 1; } |
12d8bb64e bpf: xdp: Add XDP... |
291 |
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
bbaf6029c samples/bpf: Conv... |
292 |
prog_load_attr.file = filename; |
12d8bb64e bpf: xdp: Add XDP... |
293 |
|
bbaf6029c samples/bpf: Conv... |
294 |
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
12d8bb64e bpf: xdp: Add XDP... |
295 |
return 1; |
12d8bb64e bpf: xdp: Add XDP... |
296 |
|
bbaf6029c samples/bpf: Conv... |
297 |
if (!prog_fd) { |
12d8bb64e bpf: xdp: Add XDP... |
298 299 300 301 |
printf("load_bpf_file: %s ", strerror(errno)); return 1; } |
bbaf6029c samples/bpf: Conv... |
302 303 304 305 306 307 308 |
rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt"); vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl"); if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) { printf("bpf_object__find_map_fd_by_name failed "); return 1; } |
12d8bb64e bpf: xdp: Add XDP... |
309 |
signal(SIGINT, int_exit); |
ad990dbe6 samples/bpf: run ... |
310 |
signal(SIGTERM, int_exit); |
12d8bb64e bpf: xdp: Add XDP... |
311 312 313 |
while (min_port <= max_port) { vip.dport = htons(min_port++); |
bbaf6029c samples/bpf: Conv... |
314 315 |
if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl, BPF_NOEXIST)) { |
d40fc181e samples/bpf: Make... |
316 |
perror("bpf_map_update_elem(&vip2tnl)"); |
12d8bb64e bpf: xdp: Add XDP... |
317 318 319 |
return 1; } } |
bbaf6029c samples/bpf: Conv... |
320 |
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { |
12d8bb64e bpf: xdp: Add XDP... |
321 322 323 324 |
printf("link set xdp fd failed "); return 1; } |
3b7a8ec2d samples/bpf: Chec... |
325 326 327 328 329 330 331 |
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); if (err) { printf("can't get prog info - %s ", strerror(errno)); return err; } prog_id = info.id; |
12d8bb64e bpf: xdp: Add XDP... |
332 |
poll_stats(kill_after_s); |
b259c2ffd samples/bpf: use ... |
333 |
bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
12d8bb64e bpf: xdp: Add XDP... |
334 335 336 |
return 0; } |