diff options
-rw-r--r-- | samples/bpf/bpf_load.c | 19 | ||||
-rw-r--r-- | samples/bpf/bpf_load.h | 2 | ||||
-rw-r--r-- | samples/bpf/xdp1_user.c | 40 | ||||
-rw-r--r-- | samples/bpf/xdp_tx_iptunnel_user.c | 13 |
4 files changed, 58 insertions, 16 deletions
diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c index 0d449d8032d1..d4433a47e6c3 100644 --- a/samples/bpf/bpf_load.c +++ b/samples/bpf/bpf_load.c | |||
@@ -563,7 +563,7 @@ struct ksym *ksym_search(long key) | |||
563 | return &syms[0]; | 563 | return &syms[0]; |
564 | } | 564 | } |
565 | 565 | ||
566 | int set_link_xdp_fd(int ifindex, int fd) | 566 | int set_link_xdp_fd(int ifindex, int fd, int flags) |
567 | { | 567 | { |
568 | struct sockaddr_nl sa; | 568 | struct sockaddr_nl sa; |
569 | int sock, seq = 0, len, ret = -1; | 569 | int sock, seq = 0, len, ret = -1; |
@@ -599,15 +599,28 @@ int set_link_xdp_fd(int ifindex, int fd) | |||
599 | req.nh.nlmsg_seq = ++seq; | 599 | req.nh.nlmsg_seq = ++seq; |
600 | req.ifinfo.ifi_family = AF_UNSPEC; | 600 | req.ifinfo.ifi_family = AF_UNSPEC; |
601 | req.ifinfo.ifi_index = ifindex; | 601 | req.ifinfo.ifi_index = ifindex; |
602 | |||
603 | /* started nested attribute for XDP */ | ||
602 | nla = (struct nlattr *)(((char *)&req) | 604 | nla = (struct nlattr *)(((char *)&req) |
603 | + NLMSG_ALIGN(req.nh.nlmsg_len)); | 605 | + NLMSG_ALIGN(req.nh.nlmsg_len)); |
604 | nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/; | 606 | nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/; |
607 | nla->nla_len = NLA_HDRLEN; | ||
605 | 608 | ||
606 | nla_xdp = (struct nlattr *)((char *)nla + NLA_HDRLEN); | 609 | /* add XDP fd */ |
610 | nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); | ||
607 | nla_xdp->nla_type = 1/*IFLA_XDP_FD*/; | 611 | nla_xdp->nla_type = 1/*IFLA_XDP_FD*/; |
608 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(int); | 612 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(int); |
609 | memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd)); | 613 | memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd)); |
610 | nla->nla_len = NLA_HDRLEN + nla_xdp->nla_len; | 614 | nla->nla_len += nla_xdp->nla_len; |
615 | |||
616 | /* if user passed in any flags, add those too */ | ||
617 | if (flags) { | ||
618 | nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); | ||
619 | nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/; | ||
620 | nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags); | ||
621 | memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags)); | ||
622 | nla->nla_len += nla_xdp->nla_len; | ||
623 | } | ||
611 | 624 | ||
612 | req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); | 625 | req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); |
613 | 626 | ||
diff --git a/samples/bpf/bpf_load.h b/samples/bpf/bpf_load.h index 68f6b2d22507..6bfd75ec6a16 100644 --- a/samples/bpf/bpf_load.h +++ b/samples/bpf/bpf_load.h | |||
@@ -47,5 +47,5 @@ struct ksym { | |||
47 | 47 | ||
48 | int load_kallsyms(void); | 48 | int load_kallsyms(void); |
49 | struct ksym *ksym_search(long key); | 49 | struct ksym *ksym_search(long key); |
50 | int set_link_xdp_fd(int ifindex, int fd); | 50 | int set_link_xdp_fd(int ifindex, int fd, int flags); |
51 | #endif | 51 | #endif |
diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c index d2be65d1fd86..deb05e630d84 100644 --- a/samples/bpf/xdp1_user.c +++ b/samples/bpf/xdp1_user.c | |||
@@ -5,6 +5,7 @@ | |||
5 | * License as published by the Free Software Foundation. | 5 | * License as published by the Free Software Foundation. |
6 | */ | 6 | */ |
7 | #include <linux/bpf.h> | 7 | #include <linux/bpf.h> |
8 | #include <linux/if_link.h> | ||
8 | #include <assert.h> | 9 | #include <assert.h> |
9 | #include <errno.h> | 10 | #include <errno.h> |
10 | #include <signal.h> | 11 | #include <signal.h> |
@@ -12,16 +13,18 @@ | |||
12 | #include <stdlib.h> | 13 | #include <stdlib.h> |
13 | #include <string.h> | 14 | #include <string.h> |
14 | #include <unistd.h> | 15 | #include <unistd.h> |
16 | #include <libgen.h> | ||
15 | 17 | ||
16 | #include "bpf_load.h" | 18 | #include "bpf_load.h" |
17 | #include "bpf_util.h" | 19 | #include "bpf_util.h" |
18 | #include "libbpf.h" | 20 | #include "libbpf.h" |
19 | 21 | ||
20 | static int ifindex; | 22 | static int ifindex; |
23 | static int flags; | ||
21 | 24 | ||
22 | static void int_exit(int sig) | 25 | static void int_exit(int sig) |
23 | { | 26 | { |
24 | set_link_xdp_fd(ifindex, -1); | 27 | set_link_xdp_fd(ifindex, -1, flags); |
25 | exit(0); | 28 | exit(0); |
26 | } | 29 | } |
27 | 30 | ||
@@ -54,18 +57,39 @@ static void poll_stats(int interval) | |||
54 | } | 57 | } |
55 | } | 58 | } |
56 | 59 | ||
57 | int main(int ac, char **argv) | 60 | static void usage(const char *prog) |
58 | { | 61 | { |
59 | char filename[256]; | 62 | fprintf(stderr, |
63 | "usage: %s [OPTS] IFINDEX\n\n" | ||
64 | "OPTS:\n" | ||
65 | " -S use skb-mode\n", | ||
66 | prog); | ||
67 | } | ||
60 | 68 | ||
61 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | 69 | int main(int argc, char **argv) |
70 | { | ||
71 | const char *optstr = "S"; | ||
72 | char filename[256]; | ||
73 | int opt; | ||
74 | |||
75 | while ((opt = getopt(argc, argv, optstr)) != -1) { | ||
76 | switch (opt) { | ||
77 | case 'S': | ||
78 | flags |= XDP_FLAGS_SKB_MODE; | ||
79 | break; | ||
80 | default: | ||
81 | usage(basename(argv[0])); | ||
82 | return 1; | ||
83 | } | ||
84 | } | ||
62 | 85 | ||
63 | if (ac != 2) { | 86 | if (optind == argc) { |
64 | printf("usage: %s IFINDEX\n", argv[0]); | 87 | usage(basename(argv[0])); |
65 | return 1; | 88 | return 1; |
66 | } | 89 | } |
90 | ifindex = strtoul(argv[optind], NULL, 0); | ||
67 | 91 | ||
68 | ifindex = strtoul(argv[1], NULL, 0); | 92 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
69 | 93 | ||
70 | if (load_bpf_file(filename)) { | 94 | if (load_bpf_file(filename)) { |
71 | printf("%s", bpf_log_buf); | 95 | printf("%s", bpf_log_buf); |
@@ -79,7 +103,7 @@ int main(int ac, char **argv) | |||
79 | 103 | ||
80 | signal(SIGINT, int_exit); | 104 | signal(SIGINT, int_exit); |
81 | 105 | ||
82 | if (set_link_xdp_fd(ifindex, prog_fd[0]) < 0) { | 106 | if (set_link_xdp_fd(ifindex, prog_fd[0], flags) < 0) { |
83 | printf("link set xdp fd failed\n"); | 107 | printf("link set xdp fd failed\n"); |
84 | return 1; | 108 | return 1; |
85 | } | 109 | } |
diff --git a/samples/bpf/xdp_tx_iptunnel_user.c b/samples/bpf/xdp_tx_iptunnel_user.c index 70e192fc61aa..cb2bda7b5346 100644 --- a/samples/bpf/xdp_tx_iptunnel_user.c +++ b/samples/bpf/xdp_tx_iptunnel_user.c | |||
@@ -5,6 +5,7 @@ | |||
5 | * License as published by the Free Software Foundation. | 5 | * License as published by the Free Software Foundation. |
6 | */ | 6 | */ |
7 | #include <linux/bpf.h> | 7 | #include <linux/bpf.h> |
8 | #include <linux/if_link.h> | ||
8 | #include <assert.h> | 9 | #include <assert.h> |
9 | #include <errno.h> | 10 | #include <errno.h> |
10 | #include <signal.h> | 11 | #include <signal.h> |
@@ -28,7 +29,7 @@ static int ifindex = -1; | |||
28 | static void int_exit(int sig) | 29 | static void int_exit(int sig) |
29 | { | 30 | { |
30 | if (ifindex > -1) | 31 | if (ifindex > -1) |
31 | set_link_xdp_fd(ifindex, -1); | 32 | set_link_xdp_fd(ifindex, -1, 0); |
32 | exit(0); | 33 | exit(0); |
33 | } | 34 | } |
34 | 35 | ||
@@ -136,12 +137,13 @@ int main(int argc, char **argv) | |||
136 | { | 137 | { |
137 | unsigned char opt_flags[256] = {}; | 138 | unsigned char opt_flags[256] = {}; |
138 | unsigned int kill_after_s = 0; | 139 | unsigned int kill_after_s = 0; |
139 | const char *optstr = "i:a:p:s:d:m:T:P:h"; | 140 | const char *optstr = "i:a:p:s:d:m:T:P:Sh"; |
140 | int min_port = 0, max_port = 0; | 141 | int min_port = 0, max_port = 0; |
141 | struct iptnl_info tnl = {}; | 142 | struct iptnl_info tnl = {}; |
142 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; | 143 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
143 | struct vip vip = {}; | 144 | struct vip vip = {}; |
144 | char filename[256]; | 145 | char filename[256]; |
146 | int flags = 0; | ||
145 | int opt; | 147 | int opt; |
146 | int i; | 148 | int i; |
147 | 149 | ||
@@ -201,6 +203,9 @@ int main(int argc, char **argv) | |||
201 | case 'T': | 203 | case 'T': |
202 | kill_after_s = atoi(optarg); | 204 | kill_after_s = atoi(optarg); |
203 | break; | 205 | break; |
206 | case 'S': | ||
207 | flags |= XDP_FLAGS_SKB_MODE; | ||
208 | break; | ||
204 | default: | 209 | default: |
205 | usage(argv[0]); | 210 | usage(argv[0]); |
206 | return 1; | 211 | return 1; |
@@ -243,14 +248,14 @@ int main(int argc, char **argv) | |||
243 | } | 248 | } |
244 | } | 249 | } |
245 | 250 | ||
246 | if (set_link_xdp_fd(ifindex, prog_fd[0]) < 0) { | 251 | if (set_link_xdp_fd(ifindex, prog_fd[0], flags) < 0) { |
247 | printf("link set xdp fd failed\n"); | 252 | printf("link set xdp fd failed\n"); |
248 | return 1; | 253 | return 1; |
249 | } | 254 | } |
250 | 255 | ||
251 | poll_stats(kill_after_s); | 256 | poll_stats(kill_after_s); |
252 | 257 | ||
253 | set_link_xdp_fd(ifindex, -1); | 258 | set_link_xdp_fd(ifindex, -1, flags); |
254 | 259 | ||
255 | return 0; | 260 | return 0; |
256 | } | 261 | } |