1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4readonly NS="ns1-$(mktemp -u XXXXXX)"
5readonly V0_IP4=10.10.0.11
6readonly V1_IP4=10.10.0.1
7readonly V0_IP6=2001:db8::11
8readonly V1_IP6=2001:db8::1
9
10ret=1
11
12setup() {
13	{
14		ip netns add ${NS}
15
16		ip link add v1 type veth peer name v0 netns ${NS}
17
18		ip link set v1 up
19		ip addr add $V1_IP4/24 dev v1
20		ip addr add $V1_IP6/64 nodad dev v1
21		ip -n ${NS} link set dev v0 up
22		ip -n ${NS} addr add $V0_IP4/24 dev v0
23		ip -n ${NS} addr add $V0_IP6/64 nodad dev v0
24
25		# Enable XDP mode and disable checksum offload
26		ethtool -K v1 gro on
27		ethtool -K v1 tx-checksumming off
28		ip netns exec ${NS} ethtool -K v0 gro on
29		ip netns exec ${NS} ethtool -K v0 tx-checksumming off
30	} > /dev/null 2>&1
31}
32
33cleanup() {
34	ip link del v1 2> /dev/null
35	ip netns del ${NS} 2> /dev/null
36	[ "$(pidof xdp_features)" = "" ] || kill $(pidof xdp_features) 2> /dev/null
37}
38
39wait_for_dut_server() {
40	while sleep 1; do
41		ss -tlp | grep -q xdp_features
42		[ $? -eq 0 ] && break
43	done
44}
45
46test_xdp_features() {
47	setup
48
49	## XDP_PASS
50	./xdp_features -f XDP_PASS -D $V1_IP6 -T $V0_IP6 v1 &
51	wait_for_dut_server
52	ip netns exec ${NS} ./xdp_features -t -f XDP_PASS \
53					   -D $V1_IP6 -C $V1_IP6 \
54					   -T $V0_IP6 v0
55	[ $? -ne 0 ] && exit
56
57	## XDP_DROP
58	./xdp_features -f XDP_DROP -D ::ffff:$V1_IP4 -T ::ffff:$V0_IP4 v1 &
59	wait_for_dut_server
60	ip netns exec ${NS} ./xdp_features -t -f XDP_DROP \
61					   -D ::ffff:$V1_IP4 \
62					   -C ::ffff:$V1_IP4 \
63					   -T ::ffff:$V0_IP4 v0
64	[ $? -ne 0 ] && exit
65
66	## XDP_ABORTED
67	./xdp_features -f XDP_ABORTED -D $V1_IP6 -T $V0_IP6 v1 &
68	wait_for_dut_server
69	ip netns exec ${NS} ./xdp_features -t -f XDP_ABORTED \
70					   -D $V1_IP6 -C $V1_IP6 \
71					   -T $V0_IP6 v0
72	[ $? -ne 0 ] && exit
73
74	## XDP_TX
75	./xdp_features -f XDP_TX -D ::ffff:$V1_IP4 -T ::ffff:$V0_IP4 v1 &
76	wait_for_dut_server
77	ip netns exec ${NS} ./xdp_features -t -f XDP_TX \
78					   -D ::ffff:$V1_IP4 \
79					   -C ::ffff:$V1_IP4 \
80					   -T ::ffff:$V0_IP4 v0
81	[ $? -ne 0 ] && exit
82
83	## XDP_REDIRECT
84	./xdp_features -f XDP_REDIRECT -D $V1_IP6 -T $V0_IP6 v1 &
85	wait_for_dut_server
86	ip netns exec ${NS} ./xdp_features -t -f XDP_REDIRECT \
87					   -D $V1_IP6 -C $V1_IP6 \
88					   -T $V0_IP6 v0
89	[ $? -ne 0 ] && exit
90
91	## XDP_NDO_XMIT
92	./xdp_features -f XDP_NDO_XMIT -D ::ffff:$V1_IP4 -T ::ffff:$V0_IP4 v1 &
93	wait_for_dut_server
94	ip netns exec ${NS} ./xdp_features -t -f XDP_NDO_XMIT \
95					   -D ::ffff:$V1_IP4 \
96					   -C ::ffff:$V1_IP4 \
97					   -T ::ffff:$V0_IP4 v0
98	ret=$?
99	cleanup
100}
101
102set -e
103trap cleanup 2 3 6 9
104
105test_xdp_features
106
107exit $ret
108