1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Create 3 namespaces with 3 veth peers, and
5# forward packets in-between using native XDP
6#
7#                      XDP_TX
8# NS1(veth11)        NS2(veth22)        NS3(veth33)
9#      |                  |                  |
10#      |                  |                  |
11#   (veth1,            (veth2,            (veth3,
12#   id:111)            id:122)            id:133)
13#     ^ |                ^ |                ^ |
14#     | |  XDP_REDIRECT  | |  XDP_REDIRECT  | |
15#     | ------------------ ------------------ |
16#     -----------------------------------------
17#                    XDP_REDIRECT
18
19# Kselftest framework requirement - SKIP code is 4.
20ksft_skip=4
21
22TESTNAME=xdp_veth
23BPF_FS=$(awk '$3 == "bpf" {print $2; exit}' /proc/mounts)
24BPF_DIR=$BPF_FS/test_$TESTNAME
25readonly NS1="ns1-$(mktemp -u XXXXXX)"
26readonly NS2="ns2-$(mktemp -u XXXXXX)"
27readonly NS3="ns3-$(mktemp -u XXXXXX)"
28
29_cleanup()
30{
31	set +e
32	ip link del veth1 2> /dev/null
33	ip link del veth2 2> /dev/null
34	ip link del veth3 2> /dev/null
35	ip netns del ${NS1} 2> /dev/null
36	ip netns del ${NS2} 2> /dev/null
37	ip netns del ${NS3} 2> /dev/null
38	rm -rf $BPF_DIR 2> /dev/null
39}
40
41cleanup_skip()
42{
43	echo "selftests: $TESTNAME [SKIP]"
44	_cleanup
45
46	exit $ksft_skip
47}
48
49cleanup()
50{
51	if [ "$?" = 0 ]; then
52		echo "selftests: $TESTNAME [PASS]"
53	else
54		echo "selftests: $TESTNAME [FAILED]"
55	fi
56	_cleanup
57}
58
59if [ $(id -u) -ne 0 ]; then
60	echo "selftests: $TESTNAME [SKIP] Need root privileges"
61	exit $ksft_skip
62fi
63
64if ! ip link set dev lo xdp off > /dev/null 2>&1; then
65	echo "selftests: $TESTNAME [SKIP] Could not run test without the ip xdp support"
66	exit $ksft_skip
67fi
68
69if [ -z "$BPF_FS" ]; then
70	echo "selftests: $TESTNAME [SKIP] Could not run test without bpffs mounted"
71	exit $ksft_skip
72fi
73
74if ! bpftool version > /dev/null 2>&1; then
75	echo "selftests: $TESTNAME [SKIP] Could not run test without bpftool"
76	exit $ksft_skip
77fi
78
79set -e
80
81trap cleanup_skip EXIT
82
83ip netns add ${NS1}
84ip netns add ${NS2}
85ip netns add ${NS3}
86
87ip link add veth1 index 111 type veth peer name veth11 netns ${NS1}
88ip link add veth2 index 122 type veth peer name veth22 netns ${NS2}
89ip link add veth3 index 133 type veth peer name veth33 netns ${NS3}
90
91ip link set veth1 up
92ip link set veth2 up
93ip link set veth3 up
94
95ip -n ${NS1} addr add 10.1.1.11/24 dev veth11
96ip -n ${NS3} addr add 10.1.1.33/24 dev veth33
97
98ip -n ${NS1} link set dev veth11 up
99ip -n ${NS2} link set dev veth22 up
100ip -n ${NS3} link set dev veth33 up
101
102mkdir $BPF_DIR
103bpftool prog loadall \
104	xdp_redirect_map.bpf.o $BPF_DIR/progs type xdp \
105	pinmaps $BPF_DIR/maps
106bpftool map update pinned $BPF_DIR/maps/tx_port key 0 0 0 0 value 122 0 0 0
107bpftool map update pinned $BPF_DIR/maps/tx_port key 1 0 0 0 value 133 0 0 0
108bpftool map update pinned $BPF_DIR/maps/tx_port key 2 0 0 0 value 111 0 0 0
109ip link set dev veth1 xdp pinned $BPF_DIR/progs/xdp_redirect_map_0
110ip link set dev veth2 xdp pinned $BPF_DIR/progs/xdp_redirect_map_1
111ip link set dev veth3 xdp pinned $BPF_DIR/progs/xdp_redirect_map_2
112
113ip -n ${NS1} link set dev veth11 xdp obj xdp_dummy.bpf.o sec xdp
114ip -n ${NS2} link set dev veth22 xdp obj xdp_tx.bpf.o sec xdp
115ip -n ${NS3} link set dev veth33 xdp obj xdp_dummy.bpf.o sec xdp
116
117trap cleanup EXIT
118
119ip netns exec ${NS1} ping -c 1 -W 1 10.1.1.33
120
121exit 0
122