aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorJesper Dangaard Brouer <brouer@redhat.com>2017-08-29 10:38:06 -0400
committerDavid S. Miller <davem@davemloft.net>2017-08-29 13:51:29 -0400
commit306da4e685b415f5c875cef275001b5cdc182da9 (patch)
tree76ba2be2cdb8d5a01fa43270a66fdbf758837f99 /samples
parent59a308967589f5b3f1f42793ab49bc2e18069769 (diff)
samples/bpf: xdp_redirect load XDP dummy prog on TX device
For supporting XDP_REDIRECT, a device driver must (obviously) implement the "TX" function ndo_xdp_xmit(). An additional requirement is you cannot TX out a device, unless it also have a xdp bpf program attached. This dependency is caused by the driver code need to setup XDP resources before it can ndo_xdp_xmit. Update bpf samples xdp_redirect and xdp_redirect_map to automatically attach a dummy XDP program to the configured ifindex_out device. Use the XDP flag XDP_FLAGS_UPDATE_IF_NOEXIST on the dummy load, to avoid overriding an existing XDP prog on the device. Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/xdp_redirect_kern.c11
-rw-r--r--samples/bpf/xdp_redirect_map_kern.c11
-rw-r--r--samples/bpf/xdp_redirect_map_user.c22
-rw-r--r--samples/bpf/xdp_redirect_user.c21
4 files changed, 50 insertions, 15 deletions
diff --git a/samples/bpf/xdp_redirect_kern.c b/samples/bpf/xdp_redirect_kern.c
index a34ad457a684..1c90288d0203 100644
--- a/samples/bpf/xdp_redirect_kern.c
+++ b/samples/bpf/xdp_redirect_kern.c
@@ -26,6 +26,9 @@ struct bpf_map_def SEC("maps") tx_port = {
26 .max_entries = 1, 26 .max_entries = 1,
27}; 27};
28 28
29/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success
30 * feedback. Redirect TX errors can be caught via a tracepoint.
31 */
29struct bpf_map_def SEC("maps") rxcnt = { 32struct bpf_map_def SEC("maps") rxcnt = {
30 .type = BPF_MAP_TYPE_PERCPU_ARRAY, 33 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
31 .key_size = sizeof(u32), 34 .key_size = sizeof(u32),
@@ -33,7 +36,6 @@ struct bpf_map_def SEC("maps") rxcnt = {
33 .max_entries = 1, 36 .max_entries = 1,
34}; 37};
35 38
36
37static void swap_src_dst_mac(void *data) 39static void swap_src_dst_mac(void *data)
38{ 40{
39 unsigned short *p = data; 41 unsigned short *p = data;
@@ -78,4 +80,11 @@ int xdp_redirect_prog(struct xdp_md *ctx)
78 return bpf_redirect(*ifindex, 0); 80 return bpf_redirect(*ifindex, 0);
79} 81}
80 82
83/* Redirect require an XDP bpf_prog loaded on the TX device */
84SEC("xdp_redirect_dummy")
85int xdp_redirect_dummy(struct xdp_md *ctx)
86{
87 return XDP_PASS;
88}
89
81char _license[] SEC("license") = "GPL"; 90char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/xdp_redirect_map_kern.c b/samples/bpf/xdp_redirect_map_kern.c
index 2faf196e17ea..79795d41ad0d 100644
--- a/samples/bpf/xdp_redirect_map_kern.c
+++ b/samples/bpf/xdp_redirect_map_kern.c
@@ -26,6 +26,9 @@ struct bpf_map_def SEC("maps") tx_port = {
26 .max_entries = 100, 26 .max_entries = 100,
27}; 27};
28 28
29/* Count RX packets, as XDP bpf_prog doesn't get direct TX-success
30 * feedback. Redirect TX errors can be caught via a tracepoint.
31 */
29struct bpf_map_def SEC("maps") rxcnt = { 32struct bpf_map_def SEC("maps") rxcnt = {
30 .type = BPF_MAP_TYPE_PERCPU_ARRAY, 33 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
31 .key_size = sizeof(u32), 34 .key_size = sizeof(u32),
@@ -33,7 +36,6 @@ struct bpf_map_def SEC("maps") rxcnt = {
33 .max_entries = 1, 36 .max_entries = 1,
34}; 37};
35 38
36
37static void swap_src_dst_mac(void *data) 39static void swap_src_dst_mac(void *data)
38{ 40{
39 unsigned short *p = data; 41 unsigned short *p = data;
@@ -80,4 +82,11 @@ int xdp_redirect_map_prog(struct xdp_md *ctx)
80 return bpf_redirect_map(&tx_port, vport, 0); 82 return bpf_redirect_map(&tx_port, vport, 0);
81} 83}
82 84
85/* Redirect require an XDP bpf_prog loaded on the TX device */
86SEC("xdp_redirect_dummy")
87int xdp_redirect_dummy(struct xdp_md *ctx)
88{
89 return XDP_PASS;
90}
91
83char _license[] SEC("license") = "GPL"; 92char _license[] SEC("license") = "GPL";
diff --git a/samples/bpf/xdp_redirect_map_user.c b/samples/bpf/xdp_redirect_map_user.c
index a1ad00fdaa8a..d4d86a273fba 100644
--- a/samples/bpf/xdp_redirect_map_user.c
+++ b/samples/bpf/xdp_redirect_map_user.c
@@ -16,6 +16,7 @@
16#include <signal.h> 16#include <signal.h>
17#include <stdio.h> 17#include <stdio.h>
18#include <stdlib.h> 18#include <stdlib.h>
19#include <stdbool.h>
19#include <string.h> 20#include <string.h>
20#include <unistd.h> 21#include <unistd.h>
21#include <libgen.h> 22#include <libgen.h>
@@ -26,17 +27,18 @@
26 27
27static int ifindex_in; 28static int ifindex_in;
28static int ifindex_out; 29static int ifindex_out;
30static bool ifindex_out_xdp_dummy_attached = true;
29 31
30static __u32 xdp_flags; 32static __u32 xdp_flags;
31 33
32static void int_exit(int sig) 34static void int_exit(int sig)
33{ 35{
34 set_link_xdp_fd(ifindex_in, -1, xdp_flags); 36 set_link_xdp_fd(ifindex_in, -1, xdp_flags);
37 if (ifindex_out_xdp_dummy_attached)
38 set_link_xdp_fd(ifindex_out, -1, xdp_flags);
35 exit(0); 39 exit(0);
36} 40}
37 41
38/* simple per-protocol drop counter
39 */
40static void poll_stats(int interval, int ifindex) 42static void poll_stats(int interval, int ifindex)
41{ 43{
42 unsigned int nr_cpus = bpf_num_possible_cpus(); 44 unsigned int nr_cpus = bpf_num_possible_cpus();
@@ -70,7 +72,6 @@ static void usage(const char *prog)
70 prog); 72 prog);
71} 73}
72 74
73
74int main(int argc, char **argv) 75int main(int argc, char **argv)
75{ 76{
76 const char *optstr = "SN"; 77 const char *optstr = "SN";
@@ -112,14 +113,21 @@ int main(int argc, char **argv)
112 return 1; 113 return 1;
113 } 114 }
114 115
115 signal(SIGINT, int_exit);
116 signal(SIGTERM, int_exit);
117
118 if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) { 116 if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
119 printf("link set xdp fd failed\n"); 117 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
120 return 1; 118 return 1;
121 } 119 }
122 120
121 /* Loading dummy XDP prog on out-device */
122 if (set_link_xdp_fd(ifindex_out, prog_fd[1],
123 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
124 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
125 ifindex_out_xdp_dummy_attached = false;
126 }
127
128 signal(SIGINT, int_exit);
129 signal(SIGTERM, int_exit);
130
123 printf("map[0] (vports) = %i, map[1] (map) = %i, map[2] (count) = %i\n", 131 printf("map[0] (vports) = %i, map[1] (map) = %i, map[2] (count) = %i\n",
124 map_fd[0], map_fd[1], map_fd[2]); 132 map_fd[0], map_fd[1], map_fd[2]);
125 133
diff --git a/samples/bpf/xdp_redirect_user.c b/samples/bpf/xdp_redirect_user.c
index f705a1905d2d..4475d837bf2c 100644
--- a/samples/bpf/xdp_redirect_user.c
+++ b/samples/bpf/xdp_redirect_user.c
@@ -16,6 +16,7 @@
16#include <signal.h> 16#include <signal.h>
17#include <stdio.h> 17#include <stdio.h>
18#include <stdlib.h> 18#include <stdlib.h>
19#include <stdbool.h>
19#include <string.h> 20#include <string.h>
20#include <unistd.h> 21#include <unistd.h>
21#include <libgen.h> 22#include <libgen.h>
@@ -26,17 +27,18 @@
26 27
27static int ifindex_in; 28static int ifindex_in;
28static int ifindex_out; 29static int ifindex_out;
30static bool ifindex_out_xdp_dummy_attached = true;
29 31
30static __u32 xdp_flags; 32static __u32 xdp_flags;
31 33
32static void int_exit(int sig) 34static void int_exit(int sig)
33{ 35{
34 set_link_xdp_fd(ifindex_in, -1, xdp_flags); 36 set_link_xdp_fd(ifindex_in, -1, xdp_flags);
37 if (ifindex_out_xdp_dummy_attached)
38 set_link_xdp_fd(ifindex_out, -1, xdp_flags);
35 exit(0); 39 exit(0);
36} 40}
37 41
38/* simple per-protocol drop counter
39 */
40static void poll_stats(int interval, int ifindex) 42static void poll_stats(int interval, int ifindex)
41{ 43{
42 unsigned int nr_cpus = bpf_num_possible_cpus(); 44 unsigned int nr_cpus = bpf_num_possible_cpus();
@@ -112,14 +114,21 @@ int main(int argc, char **argv)
112 return 1; 114 return 1;