188091ff5SToshiaki Makita#!/bin/sh 288091ff5SToshiaki Makita# SPDX-License-Identifier: GPL-2.0 388091ff5SToshiaki Makita# 488091ff5SToshiaki Makita# Create 3 namespaces with 3 veth peers, and 588091ff5SToshiaki Makita# forward packets in-between using native XDP 688091ff5SToshiaki Makita# 788091ff5SToshiaki Makita# XDP_TX 888091ff5SToshiaki Makita# NS1(veth11) NS2(veth22) NS3(veth33) 988091ff5SToshiaki Makita# | | | 1088091ff5SToshiaki Makita# | | | 1188091ff5SToshiaki Makita# (veth1, (veth2, (veth3, 1288091ff5SToshiaki Makita# id:111) id:122) id:133) 1388091ff5SToshiaki Makita# ^ | ^ | ^ | 1488091ff5SToshiaki Makita# | | XDP_REDIRECT | | XDP_REDIRECT | | 1588091ff5SToshiaki Makita# | ------------------ ------------------ | 1688091ff5SToshiaki Makita# ----------------------------------------- 1788091ff5SToshiaki Makita# XDP_REDIRECT 1888091ff5SToshiaki Makita 1988091ff5SToshiaki Makita# Kselftest framework requirement - SKIP code is 4. 2088091ff5SToshiaki Makitaksft_skip=4 2188091ff5SToshiaki Makita 2288091ff5SToshiaki MakitaTESTNAME=xdp_veth 2388091ff5SToshiaki MakitaBPF_FS=$(awk '$3 == "bpf" {print $2; exit}' /proc/mounts) 2488091ff5SToshiaki MakitaBPF_DIR=$BPF_FS/test_$TESTNAME 259d66c9ddSHangbin Liureadonly NS1="ns1-$(mktemp -u XXXXXX)" 269d66c9ddSHangbin Liureadonly NS2="ns2-$(mktemp -u XXXXXX)" 279d66c9ddSHangbin Liureadonly NS3="ns3-$(mktemp -u XXXXXX)" 2888091ff5SToshiaki Makita 2988091ff5SToshiaki Makita_cleanup() 3088091ff5SToshiaki Makita{ 3188091ff5SToshiaki Makita set +e 3288091ff5SToshiaki Makita ip link del veth1 2> /dev/null 3388091ff5SToshiaki Makita ip link del veth2 2> /dev/null 3488091ff5SToshiaki Makita ip link del veth3 2> /dev/null 359d66c9ddSHangbin Liu ip netns del ${NS1} 2> /dev/null 369d66c9ddSHangbin Liu ip netns del ${NS2} 2> /dev/null 379d66c9ddSHangbin Liu ip netns del ${NS3} 2> /dev/null 3888091ff5SToshiaki Makita rm -rf $BPF_DIR 2> /dev/null 3988091ff5SToshiaki Makita} 4088091ff5SToshiaki Makita 4188091ff5SToshiaki Makitacleanup_skip() 4288091ff5SToshiaki Makita{ 4388091ff5SToshiaki Makita echo "selftests: $TESTNAME [SKIP]" 4488091ff5SToshiaki Makita _cleanup 4588091ff5SToshiaki Makita 4688091ff5SToshiaki Makita exit $ksft_skip 4788091ff5SToshiaki Makita} 4888091ff5SToshiaki Makita 4988091ff5SToshiaki Makitacleanup() 5088091ff5SToshiaki Makita{ 5188091ff5SToshiaki Makita if [ "$?" = 0 ]; then 5288091ff5SToshiaki Makita echo "selftests: $TESTNAME [PASS]" 5388091ff5SToshiaki Makita else 5488091ff5SToshiaki Makita echo "selftests: $TESTNAME [FAILED]" 5588091ff5SToshiaki Makita fi 5688091ff5SToshiaki Makita _cleanup 5788091ff5SToshiaki Makita} 5888091ff5SToshiaki Makita 5988091ff5SToshiaki Makitaif [ $(id -u) -ne 0 ]; then 6088091ff5SToshiaki Makita echo "selftests: $TESTNAME [SKIP] Need root privileges" 6188091ff5SToshiaki Makita exit $ksft_skip 6288091ff5SToshiaki Makitafi 6388091ff5SToshiaki Makita 6488091ff5SToshiaki Makitaif ! ip link set dev lo xdp off > /dev/null 2>&1; then 6588091ff5SToshiaki Makita echo "selftests: $TESTNAME [SKIP] Could not run test without the ip xdp support" 6688091ff5SToshiaki Makita exit $ksft_skip 6788091ff5SToshiaki Makitafi 6888091ff5SToshiaki Makita 6988091ff5SToshiaki Makitaif [ -z "$BPF_FS" ]; then 7088091ff5SToshiaki Makita echo "selftests: $TESTNAME [SKIP] Could not run test without bpffs mounted" 7188091ff5SToshiaki Makita exit $ksft_skip 7288091ff5SToshiaki Makitafi 7388091ff5SToshiaki Makita 7488091ff5SToshiaki Makitaif ! bpftool version > /dev/null 2>&1; then 7588091ff5SToshiaki Makita echo "selftests: $TESTNAME [SKIP] Could not run test without bpftool" 7688091ff5SToshiaki Makita exit $ksft_skip 7788091ff5SToshiaki Makitafi 7888091ff5SToshiaki Makita 7988091ff5SToshiaki Makitaset -e 8088091ff5SToshiaki Makita 8188091ff5SToshiaki Makitatrap cleanup_skip EXIT 8288091ff5SToshiaki Makita 839d66c9ddSHangbin Liuip netns add ${NS1} 849d66c9ddSHangbin Liuip netns add ${NS2} 859d66c9ddSHangbin Liuip netns add ${NS3} 8688091ff5SToshiaki Makita 879d66c9ddSHangbin Liuip link add veth1 index 111 type veth peer name veth11 netns ${NS1} 889d66c9ddSHangbin Liuip link add veth2 index 122 type veth peer name veth22 netns ${NS2} 899d66c9ddSHangbin Liuip link add veth3 index 133 type veth peer name veth33 netns ${NS3} 9088091ff5SToshiaki Makita 9188091ff5SToshiaki Makitaip link set veth1 up 9288091ff5SToshiaki Makitaip link set veth2 up 9388091ff5SToshiaki Makitaip link set veth3 up 9488091ff5SToshiaki Makita 959d66c9ddSHangbin Liuip -n ${NS1} addr add 10.1.1.11/24 dev veth11 969d66c9ddSHangbin Liuip -n ${NS3} addr add 10.1.1.33/24 dev veth33 9788091ff5SToshiaki Makita 989d66c9ddSHangbin Liuip -n ${NS1} link set dev veth11 up 999d66c9ddSHangbin Liuip -n ${NS2} link set dev veth22 up 1009d66c9ddSHangbin Liuip -n ${NS3} link set dev veth33 up 10188091ff5SToshiaki Makita 10288091ff5SToshiaki Makitamkdir $BPF_DIR 10388091ff5SToshiaki Makitabpftool prog loadall \ 104*afef88e6SDaniel Müller xdp_redirect_map.bpf.o $BPF_DIR/progs type xdp \ 10588091ff5SToshiaki Makita pinmaps $BPF_DIR/maps 10688091ff5SToshiaki Makitabpftool map update pinned $BPF_DIR/maps/tx_port key 0 0 0 0 value 122 0 0 0 10788091ff5SToshiaki Makitabpftool map update pinned $BPF_DIR/maps/tx_port key 1 0 0 0 value 133 0 0 0 10888091ff5SToshiaki Makitabpftool map update pinned $BPF_DIR/maps/tx_port key 2 0 0 0 value 111 0 0 0 109f664f9c6SJie2x Zhouip link set dev veth1 xdp pinned $BPF_DIR/progs/xdp_redirect_map_0 110f664f9c6SJie2x Zhouip link set dev veth2 xdp pinned $BPF_DIR/progs/xdp_redirect_map_1 111f664f9c6SJie2x Zhouip link set dev veth3 xdp pinned $BPF_DIR/progs/xdp_redirect_map_2 11288091ff5SToshiaki Makita 113*afef88e6SDaniel Müllerip -n ${NS1} link set dev veth11 xdp obj xdp_dummy.bpf.o sec xdp 114*afef88e6SDaniel Müllerip -n ${NS2} link set dev veth22 xdp obj xdp_tx.bpf.o sec xdp 115*afef88e6SDaniel Müllerip -n ${NS3} link set dev veth33 xdp obj xdp_dummy.bpf.o sec xdp 11688091ff5SToshiaki Makita 11788091ff5SToshiaki Makitatrap cleanup EXIT 11888091ff5SToshiaki Makita 1199d66c9ddSHangbin Liuip netns exec ${NS1} ping -c 1 -W 1 10.1.1.33 12088091ff5SToshiaki Makita 12188091ff5SToshiaki Makitaexit 0 122