Skip to content

Commit a32a32c

Browse files
netoptimizerborkmann
authored andcommitted
samples/bpf: make xdp_fwd more practically usable via devmap lookup
This address the TODO in samples/bpf/xdp_fwd_kern.c, which points out that the chosen egress index should be checked for existence in the devmap. This can now be done via taking advantage of Toke's work in commit 0cdbb4b ("devmap: Allow map lookups from eBPF"). This change makes xdp_fwd more practically usable, as this allows for a mixed environment, where IP-forwarding fallback to network stack, if the egress device isn't configured to use XDP. Signed-off-by: Jesper Dangaard Brouer <[email protected]> Reviewed-by: David Ahern <[email protected]> Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Acked-by: Yonghong Song <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 3783d43 commit a32a32c

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

samples/bpf/xdp_fwd_kern.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
104104

105105
rc = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags);
106106

107-
/* verify egress index has xdp support
108-
* TO-DO bpf_map_lookup_elem(&tx_port, &key) fails with
109-
* cannot pass map_type 14 into func bpf_map_lookup_elem#1:
110-
* NOTE: without verification that egress index supports XDP
111-
* forwarding packets are dropped.
112-
*/
113107
if (rc == 0) {
108+
/* Verify egress index has been configured as TX-port.
109+
* (Note: User can still have inserted an egress ifindex that
110+
* doesn't support XDP xmit, which will result in packet drops).
111+
*
112+
* Note: lookup in devmap supported since 0cdbb4b09a0.
113+
* If not supported will fail with:
114+
* cannot pass map_type 14 into func bpf_map_lookup_elem#1:
115+
*/
116+
if (!bpf_map_lookup_elem(&xdp_tx_ports, &fib_params.ifindex))
117+
return XDP_PASS;
118+
114119
if (h_proto == htons(ETH_P_IP))
115120
ip_decrease_ttl(iph);
116121
else if (h_proto == htons(ETH_P_IPV6))

samples/bpf/xdp_fwd_user.c

Lines changed: 22 additions & 11 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);
@@ -119,10 +134,6 @@ int main(int argc, char **argv)
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)