Skip to content

Commit e520114

Browse files
LorenzoBianconitohojo
authored andcommitted
xdp-forward: Add selftest for flowtable mode
Signed-off-by: Lorenzo Bianconi <[email protected]>
1 parent 7881bee commit e520114

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

.github/workflows/selftests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
- name: Prepare packages
4343
run: |
4444
sudo apt-get update
45-
sudo apt-get install zstd binutils-dev elfutils libpcap-dev libelf-dev gcc-multilib pkg-config wireshark tshark bpfcc-tools python3 python3-pip python3-setuptools qemu-kvm rpm2cpio libdw-dev libdwarf-dev libcap-ng-dev
45+
sudo apt-get install zstd binutils-dev elfutils libpcap-dev libelf-dev gcc-multilib pkg-config wireshark tshark bpfcc-tools python3 python3-pip python3-setuptools qemu-kvm rpm2cpio libdw-dev libdwarf-dev libcap-ng-dev socat
4646
- name: Prepare Clang
4747
run: |
4848
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -

lib/testing/test_runner.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ALL_TESTS=""
2525
VERBOSE_TESTS=${V:-0}
2626
NUM_NS=2
2727

28-
NEEDED_TOOLS="capinfos ethtool ip ping sed tc tcpdump timeout nc tshark"
28+
NEEDED_TOOLS="capinfos ethtool ip ping sed tc tcpdump timeout nc tshark nft socat"
2929

3030
if [ -f "$TEST_CONFIG" ]; then
3131
source "$TEST_CONFIG"

xdp-forward/tests/test-xdp-forward.sh

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
XDP_LOADER=${XDP_LOADER:-./xdp-loader}
22
XDP_FORWARD=${XDP_FORWARD:-./xdp-forward}
3-
ALL_TESTS="test_ping test_load test_fwd_full test_fwd_direct"
4-
3+
ALL_TESTS="test_ping test_load test_fwd_full test_fwd_direct test_flowtable"
54

65
test_ping()
76
{
@@ -52,8 +51,89 @@ test_fwd_direct()
5251
check_run $XDP_FORWARD unload ${NS_NAMES[@]}
5352
}
5453

54+
test_flowtable()
55+
{
56+
local INPUT_FILE="${STATEDIR}/in_$$_$RANDOM"
57+
58+
# veth NAPI GRO support added this symbol; forwarding won't work without it
59+
skip_if_missing_kernel_symbol veth_set_features
60+
61+
# disable {tx,rx} checksum offload since it is not currently suported
62+
# by XDP_REDIRECT
63+
for n in ${NS_NAMES[@]}; do
64+
ip netns exec $n ethtool -K veth0 tx-checksumming off rx-checksumming off
65+
ethtool -K $n tx-checksumming off rx-checksumming off
66+
done
67+
68+
# create data to send via tcp
69+
dd if=/dev/urandom of="${INPUT_FILE}" bs=8192 count=32 status=none
70+
71+
# create flowtable configuration in the main namespace
72+
check_run nft -f /dev/stdin <<EOF
73+
table inet nat {
74+
# enable DNAT to server <ip:port> in pre-routing chain
75+
chain prerouting {
76+
type nat hook prerouting priority filter; policy accept;
77+
iifname == "${NS_NAMES[0]}" meta nfproto ipv4 tcp dport 12345 dnat ip to ${ALL_INSIDE_IP4[-1]}:10000
78+
iifname == "${NS_NAMES[0]}" meta nfproto ipv6 tcp dport 12345 dnat ip6 to [${ALL_INSIDE_IP6[-1]}]:10000
79+
}
80+
# enable SNAT of the client ip via masquerading in post-routing chain
81+
chain postrouting {
82+
type nat hook postrouting priority filter; policy accept;
83+
oifname "${NS_NAMES[-1]}" masquerade
84+
}
85+
}
86+
table inet filter {
87+
flowtable ft {
88+
hook ingress priority filter
89+
devices = { ${NS_NAMES[0]}, ${NS_NAMES[-1]} }
90+
}
91+
chain forward {
92+
type filter hook forward priority filter
93+
meta l4proto { tcp } flow add @ft
94+
}
95+
}
96+
EOF
97+
98+
# check if bpf flowtable lookup is available
99+
skip_if_missing_kernel_symbol bpf_xdp_flow_lookup
100+
101+
# Add some nft rules to check {dnat/snat} is done properly in
102+
# the main namespace
103+
check_run ip netns exec ${NS_NAMES[-1]} nft -f /dev/stdin <<EOF
104+
table inet filter {
105+
chain input {
106+
type filter hook input priority 0; policy drop
107+
ip saddr $OUTSIDE_IP4 ip daddr ${ALL_INSIDE_IP4[-1]} tcp dport 10000 accept
108+
ip6 saddr $OUTSIDE_IP6 ip6 daddr ${ALL_INSIDE_IP6[-1]} tcp dport 10000 accept
109+
}
110+
}
111+
EOF
112+
# wait a bit to configure nft
113+
sleep 2
114+
115+
check_run $XDP_FORWARD load -f flowtable ${NS_NAMES[@]}
116+
117+
PID=$(start_background_ns_devnull "socat -4 TCP-LISTEN:10000,reuseaddr,fork -")
118+
check_run ip netns exec ${NS_NAMES[0]} socat ${INPUT_FILE} TCP4:${OUTSIDE_IP4}:12345
119+
stop_background $PID
120+
121+
PID=$(start_background_ns_devnull "socat -6 TCP-LISTEN:10000,reuseaddr,fork -")
122+
check_run ip netns exec ${NS_NAMES[0]} socat ${INPUT_FILE} TCP6:[${OUTSIDE_IP6}]:12345
123+
stop_background $PID
124+
}
125+
55126
cleanup_tests()
56127
{
57-
$XDP_FORWARD unload ${NS_NAMES[@]} >/dev/null 2>&1
58-
$XDP_LOADER unload $NS --all >/dev/null 2>&1
128+
# enable {tx,rx} checksum offload
129+
for n in ${NS_NAMES[@]}; do
130+
ip netns exec $n ethtool -K veth0 tx-checksumming on rx-checksumming on
131+
ethtool -K $n tx-checksumming on rx-checksumming on
132+
done >/dev/null 2>&1
133+
{
134+
$XDP_FORWARD unload ${NS_NAMES[@]}
135+
$XDP_LOADER unload $NS --all
136+
check_run ip netns exec ${NS_NAMES[-1]} nft flush ruleset
137+
check_run nft flush ruleset
138+
} >/dev/null 2>&1
59139
}

0 commit comments

Comments
 (0)