Skip to content

Commit 9f4c53c

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf, selftests: Add redirect_peer selftest
Extend the test_tc_redirect test and add a small test that exercises the new redirect_peer() helper for the IPv4 and IPv6 case. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20201010234006.7075-7-daniel@iogearbox.net
1 parent 57a73fe commit 9f4c53c

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stdint.h>
3+
#include <stdbool.h>
4+
5+
#include <linux/bpf.h>
6+
#include <linux/stddef.h>
7+
#include <linux/pkt_cls.h>
8+
9+
#include <bpf/bpf_helpers.h>
10+
11+
enum {
12+
dev_src,
13+
dev_dst,
14+
};
15+
16+
struct bpf_map_def SEC("maps") ifindex_map = {
17+
.type = BPF_MAP_TYPE_ARRAY,
18+
.key_size = sizeof(int),
19+
.value_size = sizeof(int),
20+
.max_entries = 2,
21+
};
22+
23+
static __always_inline int get_dev_ifindex(int which)
24+
{
25+
int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
26+
27+
return ifindex ? *ifindex : 0;
28+
}
29+
30+
SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
31+
{
32+
return TC_ACT_SHOT;
33+
}
34+
35+
SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
36+
{
37+
return bpf_redirect_peer(get_dev_ifindex(dev_src), 0);
38+
}
39+
40+
SEC("src_ingress") int tc_src(struct __sk_buff *skb)
41+
{
42+
return bpf_redirect_peer(get_dev_ifindex(dev_dst), 0);
43+
}
44+
45+
char __license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_tc_redirect.sh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
# This test sets up 3 netns (src <-> fwd <-> dst). There is no direct veth link
55
# between src and dst. The netns fwd has veth links to each src and dst. The
66
# client is in src and server in dst. The test installs a TC BPF program to each
7-
# host facing veth in fwd which calls into bpf_redirect_peer() to perform the
8-
# neigh addr population and redirect; it also installs a dropper prog on the
9-
# egress side to drop skbs if neigh addrs were not populated.
7+
# host facing veth in fwd which calls into i) bpf_redirect_neigh() to perform the
8+
# neigh addr population and redirect or ii) bpf_redirect_peer() for namespace
9+
# switch from ingress side; it also installs a checker prog on the egress side
10+
# to drop unexpected traffic.
1011

1112
if [[ $EUID -ne 0 ]]; then
1213
echo "This script must be run as root"
@@ -166,15 +167,17 @@ hex_mem_str()
166167
perl -e 'print join(" ", unpack("(H2)8", pack("L", @ARGV)))' $1
167168
}
168169

169-
netns_setup_neigh()
170+
netns_setup_bpf()
170171
{
172+
local obj=$1
173+
171174
ip netns exec ${NS_FWD} tc qdisc add dev veth_src_fwd clsact
172-
ip netns exec ${NS_FWD} tc filter add dev veth_src_fwd ingress bpf da obj test_tc_neigh.o sec src_ingress
173-
ip netns exec ${NS_FWD} tc filter add dev veth_src_fwd egress bpf da obj test_tc_neigh.o sec chk_egress
175+
ip netns exec ${NS_FWD} tc filter add dev veth_src_fwd ingress bpf da obj $obj sec src_ingress
176+
ip netns exec ${NS_FWD} tc filter add dev veth_src_fwd egress bpf da obj $obj sec chk_egress
174177

175178
ip netns exec ${NS_FWD} tc qdisc add dev veth_dst_fwd clsact
176-
ip netns exec ${NS_FWD} tc filter add dev veth_dst_fwd ingress bpf da obj test_tc_neigh.o sec dst_ingress
177-
ip netns exec ${NS_FWD} tc filter add dev veth_dst_fwd egress bpf da obj test_tc_neigh.o sec chk_egress
179+
ip netns exec ${NS_FWD} tc filter add dev veth_dst_fwd ingress bpf da obj $obj sec dst_ingress
180+
ip netns exec ${NS_FWD} tc filter add dev veth_dst_fwd egress bpf da obj $obj sec chk_egress
178181

179182
veth_src=$(ip netns exec ${NS_FWD} cat /sys/class/net/veth_src_fwd/ifindex)
180183
veth_dst=$(ip netns exec ${NS_FWD} cat /sys/class/net/veth_dst_fwd/ifindex)
@@ -193,5 +196,9 @@ trap netns_cleanup EXIT
193196
set -e
194197

195198
netns_setup
196-
netns_setup_neigh
199+
netns_setup_bpf test_tc_neigh.o
200+
netns_test_connectivity
201+
netns_cleanup
202+
netns_setup
203+
netns_setup_bpf test_tc_peer.o
197204
netns_test_connectivity

0 commit comments

Comments
 (0)