Skip to content

Commit 9f30cd5

Browse files
committed
Merge branch 'bpf-xdp-fwd-sample-improvements'
Jesper Dangaard Brouer says: ==================== V3: Hopefully fixed all issues point out by Yonghong Song V2: Addressed issues point out by Yonghong Song - Please ACK patch 2/3 again - Added ACKs and reviewed-by to other patches This patchset is focused on improvements for XDP forwarding sample named xdp_fwd, which leverage the existing FIB routing tables as described in LPC2018[1] talk by David Ahern. The primary motivation is to illustrate how Toke's recent work improves usability of XDP_REDIRECT via lookups in devmap. The other patches are to help users understand the sample. I have more improvements to xdp_fwd, but those might requires changes to libbpf. Thus, sending these patches first as they are isolated. [1] http://vger.kernel.org/lpc-networking2018.html#session-1 ==================== Signed-off-by: Daniel Borkmann <[email protected]>
2 parents d9973ce + abcce73 commit 9f30cd5

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

samples/bpf/xdp_fwd_kern.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
2525

26-
struct bpf_map_def SEC("maps") tx_port = {
26+
/* For TX-traffic redirect requires net_device ifindex to be in this devmap */
27+
struct bpf_map_def SEC("maps") xdp_tx_ports = {
2728
.type = BPF_MAP_TYPE_DEVMAP,
2829
.key_size = sizeof(int),
2930
.value_size = sizeof(int),
@@ -102,22 +103,42 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
102103
fib_params.ifindex = ctx->ingress_ifindex;
103104

104105
rc = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags);
105-
106-
/* verify egress index has xdp support
107-
* TO-DO bpf_map_lookup_elem(&tx_port, &key) fails with
108-
* cannot pass map_type 14 into func bpf_map_lookup_elem#1:
109-
* NOTE: without verification that egress index supports XDP
110-
* forwarding packets are dropped.
106+
/*
107+
* Some rc (return codes) from bpf_fib_lookup() are important,
108+
* to understand how this XDP-prog interacts with network stack.
109+
*
110+
* BPF_FIB_LKUP_RET_NO_NEIGH:
111+
* Even if route lookup was a success, then the MAC-addresses are also
112+
* needed. This is obtained from arp/neighbour table, but if table is
113+
* (still) empty then BPF_FIB_LKUP_RET_NO_NEIGH is returned. To avoid
114+
* doing ARP lookup directly from XDP, then send packet to normal
115+
* network stack via XDP_PASS and expect it will do ARP resolution.
116+
*
117+
* BPF_FIB_LKUP_RET_FWD_DISABLED:
118+
* The bpf_fib_lookup respect sysctl net.ipv{4,6}.conf.all.forwarding
119+
* setting, and will return BPF_FIB_LKUP_RET_FWD_DISABLED if not
120+
* enabled this on ingress device.
111121
*/
112-
if (rc == 0) {
122+
if (rc == BPF_FIB_LKUP_RET_SUCCESS) {
123+
/* Verify egress index has been configured as TX-port.
124+
* (Note: User can still have inserted an egress ifindex that
125+
* doesn't support XDP xmit, which will result in packet drops).
126+
*
127+
* Note: lookup in devmap supported since 0cdbb4b09a0.
128+
* If not supported will fail with:
129+
* cannot pass map_type 14 into func bpf_map_lookup_elem#1:
130+
*/
131+
if (!bpf_map_lookup_elem(&xdp_tx_ports, &fib_params.ifindex))
132+
return XDP_PASS;
133+
113134
if (h_proto == htons(ETH_P_IP))
114135
ip_decrease_ttl(iph);
115136
else if (h_proto == htons(ETH_P_IPV6))
116137
ip6h->hop_limit--;
117138

118139
memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
119140
memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
120-
return bpf_redirect_map(&tx_port, fib_params.ifindex, 0);
141+
return bpf_redirect_map(&xdp_tx_ports, fib_params.ifindex, 0);
121142
}
122143

123144
return XDP_PASS;

samples/bpf/xdp_fwd_user.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@
2727
#include "libbpf.h"
2828
#include <bpf/bpf.h>
2929

30-
31-
static int do_attach(int idx, int fd, const char *name)
30+
static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
3231
{
3332
int err;
3433

35-
err = bpf_set_link_xdp_fd(idx, fd, 0);
36-
if (err < 0)
34+
err = bpf_set_link_xdp_fd(idx, prog_fd, 0);
35+
if (err < 0) {
3736
printf("ERROR: failed to attach program to %s\n", name);
37+
return err;
38+
}
39+
40+
/* Adding ifindex as a possible egress TX port */
41+
err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
42+
if (err)
43+
printf("ERROR: failed using device %s as TX-port\n", name);
3844

3945
return err;
4046
}
@@ -47,6 +53,9 @@ static int do_detach(int idx, const char *name)
4753
if (err < 0)
4854
printf("ERROR: failed to detach program from %s\n", name);
4955

56+
/* TODO: Remember to cleanup map, when adding use of shared map
57+
* bpf_map_delete_elem((map_fd, &idx);
58+
*/
5059
return err;
5160
}
5261

@@ -67,10 +76,10 @@ int main(int argc, char **argv)
6776
};
6877
const char *prog_name = "xdp_fwd";
6978
struct bpf_program *prog;
79+
int prog_fd, map_fd = -1;
7080
char filename[PATH_MAX];
7181
struct bpf_object *obj;
7282
int opt, i, idx, err;
73-
int prog_fd, map_fd;
7483
int attach = 1;
7584
int ret = 0;
7685

@@ -103,8 +112,14 @@ int main(int argc, char **argv)
103112
return 1;
104113
}
105114

106-
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
115+
err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
116+
if (err) {
117+
printf("Does kernel support devmap lookup?\n");
118+
/* If not, the error message will be:
119+
* "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
120+
*/
107121
return 1;
122+
}
108123

109124
prog = bpf_object__find_program_by_title(obj, prog_name);
110125
prog_fd = bpf_program__fd(prog);
@@ -113,16 +128,12 @@ int main(int argc, char **argv)
113128
return 1;
114129
}
115130
map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
116-
"tx_port"));
131+
"xdp_tx_ports"));
117132
if (map_fd < 0) {
118133
printf("map not found: %s\n", strerror(map_fd));
119134
return 1;
120135
}
121136
}
122-
if (attach) {
123-
for (i = 1; i < 64; ++i)
124-
bpf_map_update_elem(map_fd, &i, &i, 0);
125-
}
126137

127138
for (i = optind; i < argc; ++i) {
128139
idx = if_nametoindex(argv[i]);
@@ -138,7 +149,7 @@ int main(int argc, char **argv)
138149
if (err)
139150
ret = err;
140151
} else {
141-
err = do_attach(idx, prog_fd, argv[i]);
152+
err = do_attach(idx, prog_fd, map_fd, argv[i]);
142153
if (err)
143154
ret = err;
144155
}

0 commit comments

Comments
 (0)