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 25 26_cleanup() 27{ 28 set +e 29 ip link del veth1 2> /dev/null 30 ip link del veth2 2> /dev/null 31 ip link del veth3 2> /dev/null 32 ip netns del ns1 2> /dev/null 33 ip netns del ns2 2> /dev/null 34 ip netns del ns3 2> /dev/null 35 rm -rf $BPF_DIR 2> /dev/null 36} 37 38cleanup_skip() 39{ 40 echo "selftests: $TESTNAME [SKIP]" 41 _cleanup 42 43 exit $ksft_skip 44} 45 46cleanup() 47{ 48 if [ "$?" = 0 ]; then 49 echo "selftests: $TESTNAME [PASS]" 50 else 51 echo "selftests: $TESTNAME [FAILED]" 52 fi 53 _cleanup 54} 55 56if [ $(id -u) -ne 0 ]; then 57 echo "selftests: $TESTNAME [SKIP] Need root privileges" 58 exit $ksft_skip 59fi 60 61if ! ip link set dev lo xdp off > /dev/null 2>&1; then 62 echo "selftests: $TESTNAME [SKIP] Could not run test without the ip xdp support" 63 exit $ksft_skip 64fi 65 66if [ -z "$BPF_FS" ]; then 67 echo "selftests: $TESTNAME [SKIP] Could not run test without bpffs mounted" 68 exit $ksft_skip 69fi 70 71if ! bpftool version > /dev/null 2>&1; then 72 echo "selftests: $TESTNAME [SKIP] Could not run test without bpftool" 73 exit $ksft_skip 74fi 75 76set -e 77 78trap cleanup_skip EXIT 79 80ip netns add ns1 81ip netns add ns2 82ip netns add ns3 83 84ip link add veth1 index 111 type veth peer name veth11 netns ns1 85ip link add veth2 index 122 type veth peer name veth22 netns ns2 86ip link add veth3 index 133 type veth peer name veth33 netns ns3 87 88ip link set veth1 up 89ip link set veth2 up 90ip link set veth3 up 91 92ip -n ns1 addr add 10.1.1.11/24 dev veth11 93ip -n ns3 addr add 10.1.1.33/24 dev veth33 94 95ip -n ns1 link set dev veth11 up 96ip -n ns2 link set dev veth22 up 97ip -n ns3 link set dev veth33 up 98 99mkdir $BPF_DIR 100bpftool prog loadall \ 101 xdp_redirect_map.o $BPF_DIR/progs type xdp \ 102 pinmaps $BPF_DIR/maps 103bpftool map update pinned $BPF_DIR/maps/tx_port key 0 0 0 0 value 122 0 0 0 104bpftool map update pinned $BPF_DIR/maps/tx_port key 1 0 0 0 value 133 0 0 0 105bpftool map update pinned $BPF_DIR/maps/tx_port key 2 0 0 0 value 111 0 0 0 106ip link set dev veth1 xdp pinned $BPF_DIR/progs/redirect_map_0 107ip link set dev veth2 xdp pinned $BPF_DIR/progs/redirect_map_1 108ip link set dev veth3 xdp pinned $BPF_DIR/progs/redirect_map_2 109 110ip -n ns1 link set dev veth11 xdp obj xdp_dummy.o sec xdp 111ip -n ns2 link set dev veth22 xdp obj xdp_tx.o sec xdp 112ip -n ns3 link set dev veth33 xdp obj xdp_dummy.o sec xdp 113 114trap cleanup EXIT 115 116ip netns exec ns1 ping -c 1 -W 1 10.1.1.33 117 118exit 0 119