Skip to content

Commit fe61605

Browse files
dsahernborkmann
authored andcommitted
samples/bpf: Add example of ipv4 and ipv6 forwarding in XDP
Simple example of fast-path forwarding. It has a serious flaw in not verifying the egress device index supports XDP forwarding. If the egress device does not packets are dropped. Take this only as a simple example of fast-path forwarding. Signed-off-by: David Ahern <dsahern@gmail.com> Acked-by: David S. Miller <davem@davemloft.net> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 87f5fc7 commit fe61605

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed

samples/bpf/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ hostprogs-y += syscall_tp
4646
hostprogs-y += cpustat
4747
hostprogs-y += xdp_adjust_tail
4848
hostprogs-y += xdpsock
49+
hostprogs-y += xdp_fwd
4950

5051
# Libbpf dependencies
5152
LIBBPF := ../../tools/lib/bpf/bpf.o ../../tools/lib/bpf/nlattr.o
@@ -100,6 +101,7 @@ syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
100101
cpustat-objs := bpf_load.o $(LIBBPF) cpustat_user.o
101102
xdp_adjust_tail-objs := bpf_load.o $(LIBBPF) xdp_adjust_tail_user.o
102103
xdpsock-objs := bpf_load.o $(LIBBPF) xdpsock_user.o
104+
xdp_fwd-objs := bpf_load.o $(LIBBPF) xdp_fwd_user.o
103105

104106
# Tell kbuild to always build the programs
105107
always := $(hostprogs-y)
@@ -154,6 +156,7 @@ always += syscall_tp_kern.o
154156
always += cpustat_kern.o
155157
always += xdp_adjust_tail_kern.o
156158
always += xdpsock_kern.o
159+
always += xdp_fwd_kern.o
157160

158161
HOSTCFLAGS += -I$(objtree)/usr/include
159162
HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -201,6 +204,7 @@ HOSTLOADLIBES_syscall_tp += -lelf
201204
HOSTLOADLIBES_cpustat += -lelf
202205
HOSTLOADLIBES_xdp_adjust_tail += -lelf
203206
HOSTLOADLIBES_xdpsock += -lelf -pthread
207+
HOSTLOADLIBES_xdp_fwd += -lelf
204208

205209
# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
206210
# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang

samples/bpf/xdp_fwd_kern.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of version 2 of the GNU General Public
6+
* License as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but
9+
* WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*/
13+
#define KBUILD_MODNAME "foo"
14+
#include <uapi/linux/bpf.h>
15+
#include <linux/in.h>
16+
#include <linux/if_ether.h>
17+
#include <linux/if_packet.h>
18+
#include <linux/if_vlan.h>
19+
#include <linux/ip.h>
20+
#include <linux/ipv6.h>
21+
22+
#include "bpf_helpers.h"
23+
24+
#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
25+
26+
struct bpf_map_def SEC("maps") tx_port = {
27+
.type = BPF_MAP_TYPE_DEVMAP,
28+
.key_size = sizeof(int),
29+
.value_size = sizeof(int),
30+
.max_entries = 64,
31+
};
32+
33+
static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
34+
{
35+
void *data_end = (void *)(long)ctx->data_end;
36+
void *data = (void *)(long)ctx->data;
37+
struct bpf_fib_lookup fib_params;
38+
struct ethhdr *eth = data;
39+
int out_index;
40+
u16 h_proto;
41+
u64 nh_off;
42+
43+
nh_off = sizeof(*eth);
44+
if (data + nh_off > data_end)
45+
return XDP_DROP;
46+
47+
__builtin_memset(&fib_params, 0, sizeof(fib_params));
48+
49+
h_proto = eth->h_proto;
50+
if (h_proto == htons(ETH_P_IP)) {
51+
struct iphdr *iph = data + nh_off;
52+
53+
if (iph + 1 > data_end)
54+
return XDP_DROP;
55+
56+
fib_params.family = AF_INET;
57+
fib_params.tos = iph->tos;
58+
fib_params.l4_protocol = iph->protocol;
59+
fib_params.sport = 0;
60+
fib_params.dport = 0;
61+
fib_params.tot_len = ntohs(iph->tot_len);
62+
fib_params.ipv4_src = iph->saddr;
63+
fib_params.ipv4_dst = iph->daddr;
64+
} else if (h_proto == htons(ETH_P_IPV6)) {
65+
struct in6_addr *src = (struct in6_addr *) fib_params.ipv6_src;
66+
struct in6_addr *dst = (struct in6_addr *) fib_params.ipv6_dst;
67+
struct ipv6hdr *iph = data + nh_off;
68+
69+
if (iph + 1 > data_end)
70+
return XDP_DROP;
71+
72+
fib_params.family = AF_INET6;
73+
fib_params.flowlabel = *(__be32 *)iph & IPV6_FLOWINFO_MASK;
74+
fib_params.l4_protocol = iph->nexthdr;
75+
fib_params.sport = 0;
76+
fib_params.dport = 0;
77+
fib_params.tot_len = ntohs(iph->payload_len);
78+
*src = iph->saddr;
79+
*dst = iph->daddr;
80+
} else {
81+
return XDP_PASS;
82+
}
83+
84+
fib_params.ifindex = ctx->ingress_ifindex;
85+
86+
out_index = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags);
87+
88+
/* verify egress index has xdp support
89+
* TO-DO bpf_map_lookup_elem(&tx_port, &key) fails with
90+
* cannot pass map_type 14 into func bpf_map_lookup_elem#1:
91+
* NOTE: without verification that egress index supports XDP
92+
* forwarding packets are dropped.
93+
*/
94+
if (out_index > 0) {
95+
memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
96+
memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
97+
return bpf_redirect_map(&tx_port, out_index, 0);
98+
}
99+
100+
return XDP_PASS;
101+
}
102+
103+
SEC("xdp_fwd")
104+
int xdp_fwd_prog(struct xdp_md *ctx)
105+
{
106+
return xdp_fwd_flags(ctx, 0);
107+
}
108+
109+
SEC("xdp_fwd_direct")
110+
int xdp_fwd_direct_prog(struct xdp_md *ctx)
111+
{
112+
return xdp_fwd_flags(ctx, BPF_FIB_LOOKUP_DIRECT);
113+
}
114+
115+
char _license[] SEC("license") = "GPL";

samples/bpf/xdp_fwd_user.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of version 2 of the GNU General Public
6+
* License as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but
9+
* WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*/
13+
14+
#include <linux/bpf.h>
15+
#include <linux/if_link.h>
16+
#include <linux/limits.h>
17+
#include <net/if.h>
18+
#include <errno.h>
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
#include <stdbool.h>
22+
#include <string.h>
23+
#include <unistd.h>
24+
#include <fcntl.h>
25+
#include <libgen.h>
26+
27+
#include "bpf_load.h"
28+
#include "bpf_util.h"
29+
#include "libbpf.h"
30+
31+
32+
static int do_attach(int idx, int fd, const char *name)
33+
{
34+
int err;
35+
36+
err = bpf_set_link_xdp_fd(idx, fd, 0);
37+
if (err < 0)
38+
printf("ERROR: failed to attach program to %s\n", name);
39+
40+
return err;
41+
}
42+
43+
static int do_detach(int idx, const char *name)
44+
{
45+
int err;
46+
47+
err = bpf_set_link_xdp_fd(idx, -1, 0);
48+
if (err < 0)
49+
printf("ERROR: failed to detach program from %s\n", name);
50+
51+
return err;
52+
}
53+
54+
static void usage(const char *prog)
55+
{
56+
fprintf(stderr,
57+
"usage: %s [OPTS] interface-list\n"
58+
"\nOPTS:\n"
59+
" -d detach program\n"
60+
" -D direct table lookups (skip fib rules)\n",
61+
prog);
62+
}
63+
64+
int main(int argc, char **argv)
65+
{
66+
char filename[PATH_MAX];
67+
int opt, i, idx, err;
68+
int prog_id = 0;
69+
int attach = 1;
70+
int ret = 0;
71+
72+
while ((opt = getopt(argc, argv, ":dD")) != -1) {
73+
switch (opt) {
74+
case 'd':
75+
attach = 0;
76+
break;
77+
case 'D':
78+
prog_id = 1;
79+
break;
80+
default:
81+
usage(basename(argv[0]));
82+
return 1;
83+
}
84+
}
85+
86+
if (optind == argc) {
87+
usage(basename(argv[0]));
88+
return 1;
89+
}
90+
91+
if (attach) {
92+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
93+
94+
if (access(filename, O_RDONLY) < 0) {
95+
printf("error accessing file %s: %s\n",
96+
filename, strerror(errno));
97+
return 1;
98+
}
99+
100+
if (load_bpf_file(filename)) {
101+
printf("%s", bpf_log_buf);
102+
return 1;
103+
}
104+
105+
if (!prog_fd[prog_id]) {
106+
printf("load_bpf_file: %s\n", strerror(errno));
107+
return 1;
108+
}
109+
}
110+
if (attach) {
111+
for (i = 1; i < 64; ++i)
112+
bpf_map_update_elem(map_fd[0], &i, &i, 0);
113+
}
114+
115+
for (i = optind; i < argc; ++i) {
116+
idx = if_nametoindex(argv[i]);
117+
if (!idx)
118+
idx = strtoul(argv[i], NULL, 0);
119+
120+
if (!idx) {
121+
fprintf(stderr, "Invalid arg\n");
122+
return 1;
123+
}
124+
if (!attach) {
125+
err = do_detach(idx, argv[i]);
126+
if (err)
127+
ret = err;
128+
} else {
129+
err = do_attach(idx, prog_fd[prog_id], argv[i]);
130+
if (err)
131+
ret = err;
132+
}
133+
}
134+
135+
return ret;
136+
}

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ static int (*bpf_skb_get_xfrm_state)(void *ctx, int index, void *state,
103103
(void *) BPF_FUNC_skb_get_xfrm_state;
104104
static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =
105105
(void *) BPF_FUNC_get_stack;
106+
static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params,
107+
int plen, __u32 flags) =
108+
(void *) BPF_FUNC_fib_lookup;
106109

107110
/* llvm builtin functions that eBPF C program may use to
108111
* emit BPF_LD_ABS and BPF_LD_IND instructions

0 commit comments

Comments
 (0)