Commit b3dddab2 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'selftests/bpf: use temp netns for testing'

Hangbin Liu says:

====================

There are some bpf tests using hard code netns name like ns0, ns1, etc.
This kind of ns name is easily used by other tests or system. If there
is already a such netns, all the related tests will failed. So let's
use temp netns name for testing.

The first patch not only change to temp netns. But also fixed an interface
index issue. So I add fixes tag. For the later patches, I think that
should be an update instead of fixes, so the fixes tag is not added.
====================
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents b6ec7951 4ec25b49
...@@ -23,6 +23,12 @@ ...@@ -23,6 +23,12 @@
# Kselftest framework requirement - SKIP code is 4. # Kselftest framework requirement - SKIP code is 4.
ksft_skip=4 ksft_skip=4
readonly NS1="ns1-$(mktemp -u XXXXXX)"
readonly NS2="ns2-$(mktemp -u XXXXXX)"
readonly NS3="ns3-$(mktemp -u XXXXXX)"
readonly NS4="ns4-$(mktemp -u XXXXXX)"
readonly NS5="ns5-$(mktemp -u XXXXXX)"
readonly NS6="ns6-$(mktemp -u XXXXXX)"
msg="skip all tests:" msg="skip all tests:"
if [ $UID != 0 ]; then if [ $UID != 0 ]; then
...@@ -41,23 +47,23 @@ cleanup() ...@@ -41,23 +47,23 @@ cleanup()
fi fi
set +e set +e
ip netns del ns1 2> /dev/null ip netns del ${NS1} 2> /dev/null
ip netns del ns2 2> /dev/null ip netns del ${NS2} 2> /dev/null
ip netns del ns3 2> /dev/null ip netns del ${NS3} 2> /dev/null
ip netns del ns4 2> /dev/null ip netns del ${NS4} 2> /dev/null
ip netns del ns5 2> /dev/null ip netns del ${NS5} 2> /dev/null
ip netns del ns6 2> /dev/null ip netns del ${NS6} 2> /dev/null
rm -f $TMP_FILE rm -f $TMP_FILE
} }
set -e set -e
ip netns add ns1 ip netns add ${NS1}
ip netns add ns2 ip netns add ${NS2}
ip netns add ns3 ip netns add ${NS3}
ip netns add ns4 ip netns add ${NS4}
ip netns add ns5 ip netns add ${NS5}
ip netns add ns6 ip netns add ${NS6}
trap cleanup 0 2 3 6 9 trap cleanup 0 2 3 6 9
...@@ -67,78 +73,78 @@ ip link add veth5 type veth peer name veth6 ...@@ -67,78 +73,78 @@ ip link add veth5 type veth peer name veth6
ip link add veth7 type veth peer name veth8 ip link add veth7 type veth peer name veth8
ip link add veth9 type veth peer name veth10 ip link add veth9 type veth peer name veth10
ip link set veth1 netns ns1 ip link set veth1 netns ${NS1}
ip link set veth2 netns ns2 ip link set veth2 netns ${NS2}
ip link set veth3 netns ns2 ip link set veth3 netns ${NS2}
ip link set veth4 netns ns3 ip link set veth4 netns ${NS3}
ip link set veth5 netns ns3 ip link set veth5 netns ${NS3}
ip link set veth6 netns ns4 ip link set veth6 netns ${NS4}
ip link set veth7 netns ns4 ip link set veth7 netns ${NS4}
ip link set veth8 netns ns5 ip link set veth8 netns ${NS5}
ip link set veth9 netns ns5 ip link set veth9 netns ${NS5}
ip link set veth10 netns ns6 ip link set veth10 netns ${NS6}
ip netns exec ns1 ip link set dev veth1 up ip netns exec ${NS1} ip link set dev veth1 up
ip netns exec ns2 ip link set dev veth2 up ip netns exec ${NS2} ip link set dev veth2 up
ip netns exec ns2 ip link set dev veth3 up ip netns exec ${NS2} ip link set dev veth3 up
ip netns exec ns3 ip link set dev veth4 up ip netns exec ${NS3} ip link set dev veth4 up
ip netns exec ns3 ip link set dev veth5 up ip netns exec ${NS3} ip link set dev veth5 up
ip netns exec ns4 ip link set dev veth6 up ip netns exec ${NS4} ip link set dev veth6 up
ip netns exec ns4 ip link set dev veth7 up ip netns exec ${NS4} ip link set dev veth7 up
ip netns exec ns5 ip link set dev veth8 up ip netns exec ${NS5} ip link set dev veth8 up
ip netns exec ns5 ip link set dev veth9 up ip netns exec ${NS5} ip link set dev veth9 up
ip netns exec ns6 ip link set dev veth10 up ip netns exec ${NS6} ip link set dev veth10 up
ip netns exec ns6 ip link set dev lo up ip netns exec ${NS6} ip link set dev lo up
# All link scope addresses and routes required between veths # All link scope addresses and routes required between veths
ip netns exec ns1 ip -6 addr add fb00::12/16 dev veth1 scope link ip netns exec ${NS1} ip -6 addr add fb00::12/16 dev veth1 scope link
ip netns exec ns1 ip -6 route add fb00::21 dev veth1 scope link ip netns exec ${NS1} ip -6 route add fb00::21 dev veth1 scope link
ip netns exec ns2 ip -6 addr add fb00::21/16 dev veth2 scope link ip netns exec ${NS2} ip -6 addr add fb00::21/16 dev veth2 scope link
ip netns exec ns2 ip -6 addr add fb00::34/16 dev veth3 scope link ip netns exec ${NS2} ip -6 addr add fb00::34/16 dev veth3 scope link
ip netns exec ns2 ip -6 route add fb00::43 dev veth3 scope link ip netns exec ${NS2} ip -6 route add fb00::43 dev veth3 scope link
ip netns exec ns3 ip -6 route add fb00::65 dev veth5 scope link ip netns exec ${NS3} ip -6 route add fb00::65 dev veth5 scope link
ip netns exec ns3 ip -6 addr add fb00::43/16 dev veth4 scope link ip netns exec ${NS3} ip -6 addr add fb00::43/16 dev veth4 scope link
ip netns exec ns3 ip -6 addr add fb00::56/16 dev veth5 scope link ip netns exec ${NS3} ip -6 addr add fb00::56/16 dev veth5 scope link
ip netns exec ns4 ip -6 addr add fb00::65/16 dev veth6 scope link ip netns exec ${NS4} ip -6 addr add fb00::65/16 dev veth6 scope link
ip netns exec ns4 ip -6 addr add fb00::78/16 dev veth7 scope link ip netns exec ${NS4} ip -6 addr add fb00::78/16 dev veth7 scope link
ip netns exec ns4 ip -6 route add fb00::87 dev veth7 scope link ip netns exec ${NS4} ip -6 route add fb00::87 dev veth7 scope link
ip netns exec ns5 ip -6 addr add fb00::87/16 dev veth8 scope link ip netns exec ${NS5} ip -6 addr add fb00::87/16 dev veth8 scope link
ip netns exec ns5 ip -6 addr add fb00::910/16 dev veth9 scope link ip netns exec ${NS5} ip -6 addr add fb00::910/16 dev veth9 scope link
ip netns exec ns5 ip -6 route add fb00::109 dev veth9 scope link ip netns exec ${NS5} ip -6 route add fb00::109 dev veth9 scope link
ip netns exec ns5 ip -6 route add fb00::109 table 117 dev veth9 scope link ip netns exec ${NS5} ip -6 route add fb00::109 table 117 dev veth9 scope link
ip netns exec ns6 ip -6 addr add fb00::109/16 dev veth10 scope link ip netns exec ${NS6} ip -6 addr add fb00::109/16 dev veth10 scope link
ip netns exec ns1 ip -6 addr add fb00::1/16 dev lo ip netns exec ${NS1} ip -6 addr add fb00::1/16 dev lo
ip netns exec ns1 ip -6 route add fb00::6 dev veth1 via fb00::21 ip netns exec ${NS1} ip -6 route add fb00::6 dev veth1 via fb00::21
ip netns exec ns2 ip -6 route add fb00::6 encap bpf in obj test_lwt_seg6local.o sec encap_srh dev veth2 ip netns exec ${NS2} ip -6 route add fb00::6 encap bpf in obj test_lwt_seg6local.o sec encap_srh dev veth2
ip netns exec ns2 ip -6 route add fd00::1 dev veth3 via fb00::43 scope link ip netns exec ${NS2} ip -6 route add fd00::1 dev veth3 via fb00::43 scope link
ip netns exec ns3 ip -6 route add fc42::1 dev veth5 via fb00::65 ip netns exec ${NS3} ip -6 route add fc42::1 dev veth5 via fb00::65
ip netns exec ns3 ip -6 route add fd00::1 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec add_egr_x dev veth4 ip netns exec ${NS3} ip -6 route add fd00::1 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec add_egr_x dev veth4
ip netns exec ns4 ip -6 route add fd00::2 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec pop_egr dev veth6 ip netns exec ${NS4} ip -6 route add fd00::2 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec pop_egr dev veth6
ip netns exec ns4 ip -6 addr add fc42::1 dev lo ip netns exec ${NS4} ip -6 addr add fc42::1 dev lo
ip netns exec ns4 ip -6 route add fd00::3 dev veth7 via fb00::87 ip netns exec ${NS4} ip -6 route add fd00::3 dev veth7 via fb00::87
ip netns exec ns5 ip -6 route add fd00::4 table 117 dev veth9 via fb00::109 ip netns exec ${NS5} ip -6 route add fd00::4 table 117 dev veth9 via fb00::109
ip netns exec ns5 ip -6 route add fd00::3 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec inspect_t dev veth8 ip netns exec ${NS5} ip -6 route add fd00::3 encap seg6local action End.BPF endpoint obj test_lwt_seg6local.o sec inspect_t dev veth8
ip netns exec ns6 ip -6 addr add fb00::6/16 dev lo ip netns exec ${NS6} ip -6 addr add fb00::6/16 dev lo
ip netns exec ns6 ip -6 addr add fd00::4/16 dev lo ip netns exec ${NS6} ip -6 addr add fd00::4/16 dev lo
ip netns exec ns1 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null ip netns exec ${NS1} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
ip netns exec ns2 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null ip netns exec ${NS2} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
ip netns exec ns3 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null ip netns exec ${NS3} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
ip netns exec ns4 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null ip netns exec ${NS4} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
ip netns exec ns5 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null ip netns exec ${NS5} sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
ip netns exec ns6 sysctl net.ipv6.conf.all.seg6_enabled=1 > /dev/null ip netns exec ${NS6} sysctl net.ipv6.conf.all.seg6_enabled=1 > /dev/null
ip netns exec ns6 sysctl net.ipv6.conf.lo.seg6_enabled=1 > /dev/null ip netns exec ${NS6} sysctl net.ipv6.conf.lo.seg6_enabled=1 > /dev/null
ip netns exec ns6 sysctl net.ipv6.conf.veth10.seg6_enabled=1 > /dev/null ip netns exec ${NS6} sysctl net.ipv6.conf.veth10.seg6_enabled=1 > /dev/null
ip netns exec ns6 nc -l -6 -u -d 7330 > $TMP_FILE & ip netns exec ${NS6} nc -l -6 -u -d 7330 > $TMP_FILE &
ip netns exec ns1 bash -c "echo 'foobar' | nc -w0 -6 -u -p 2121 -s fb00::1 fb00::6 7330" ip netns exec ${NS1} bash -c "echo 'foobar' | nc -w0 -6 -u -p 2121 -s fb00::1 fb00::6 7330"
sleep 5 # wait enough time to ensure the UDP datagram arrived to the last segment sleep 5 # wait enough time to ensure the UDP datagram arrived to the last segment
kill -TERM $! kill -TERM $!
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
# Copyright (c) 2019 Cloudflare # Copyright (c) 2019 Cloudflare
set -eu set -eu
readonly NS1="ns1-$(mktemp -u XXXXXX)"
wait_for_ip() wait_for_ip()
{ {
...@@ -28,12 +29,12 @@ get_prog_id() ...@@ -28,12 +29,12 @@ get_prog_id()
ns1_exec() ns1_exec()
{ {
ip netns exec ns1 "$@" ip netns exec ${NS1} "$@"
} }
setup() setup()
{ {
ip netns add ns1 ip netns add ${NS1}
ns1_exec ip link set lo up ns1_exec ip link set lo up
ns1_exec sysctl -w net.ipv4.tcp_syncookies=2 ns1_exec sysctl -w net.ipv4.tcp_syncookies=2
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# Kselftest framework requirement - SKIP code is 4. # Kselftest framework requirement - SKIP code is 4.
readonly KSFT_SKIP=4 readonly KSFT_SKIP=4
readonly NS1="ns1-$(mktemp -u XXXXXX)"
readonly NS2="ns2-$(mktemp -u XXXXXX)"
cleanup() cleanup()
{ {
...@@ -13,8 +15,8 @@ cleanup() ...@@ -13,8 +15,8 @@ cleanup()
set +e set +e
ip link del veth1 2> /dev/null ip link del veth1 2> /dev/null
ip netns del ns1 2> /dev/null ip netns del ${NS1} 2> /dev/null
ip netns del ns2 2> /dev/null ip netns del ${NS2} 2> /dev/null
} }
ip link set dev lo xdp off 2>/dev/null > /dev/null ip link set dev lo xdp off 2>/dev/null > /dev/null
...@@ -24,32 +26,32 @@ if [ $? -ne 0 ];then ...@@ -24,32 +26,32 @@ if [ $? -ne 0 ];then
fi fi
set -e set -e
ip netns add ns1 ip netns add ${NS1}
ip netns add ns2 ip netns add ${NS2}
trap cleanup 0 2 3 6 9 trap cleanup 0 2 3 6 9
ip link add veth1 type veth peer name veth2 ip link add veth1 type veth peer name veth2
ip link set veth1 netns ns1 ip link set veth1 netns ${NS1}
ip link set veth2 netns ns2 ip link set veth2 netns ${NS2}
ip netns exec ns1 ip addr add 10.1.1.11/24 dev veth1 ip netns exec ${NS1} ip addr add 10.1.1.11/24 dev veth1
ip netns exec ns2 ip addr add 10.1.1.22/24 dev veth2 ip netns exec ${NS2} ip addr add 10.1.1.22/24 dev veth2
ip netns exec ns1 tc qdisc add dev veth1 clsact ip netns exec ${NS1} tc qdisc add dev veth1 clsact
ip netns exec ns2 tc qdisc add dev veth2 clsact ip netns exec ${NS2} tc qdisc add dev veth2 clsact
ip netns exec ns1 tc filter add dev veth1 ingress bpf da obj test_xdp_meta.o sec t ip netns exec ${NS1} tc filter add dev veth1 ingress bpf da obj test_xdp_meta.o sec t
ip netns exec ns2 tc filter add dev veth2 ingress bpf da obj test_xdp_meta.o sec t ip netns exec ${NS2} tc filter add dev veth2 ingress bpf da obj test_xdp_meta.o sec t
ip netns exec ns1 ip link set dev veth1 xdp obj test_xdp_meta.o sec x ip netns exec ${NS1} ip link set dev veth1 xdp obj test_xdp_meta.o sec x
ip netns exec ns2 ip link set dev veth2 xdp obj test_xdp_meta.o sec x ip netns exec ${NS2} ip link set dev veth2 xdp obj test_xdp_meta.o sec x
ip netns exec ns1 ip link set dev veth1 up ip netns exec ${NS1} ip link set dev veth1 up
ip netns exec ns2 ip link set dev veth2 up ip netns exec ${NS2} ip link set dev veth2 up
ip netns exec ns1 ping -c 1 10.1.1.22 ip netns exec ${NS1} ping -c 1 10.1.1.22
ip netns exec ns2 ping -c 1 10.1.1.11 ip netns exec ${NS2} ping -c 1 10.1.1.11
exit 0 exit 0
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
# | xdp forwarding | # | xdp forwarding |
# ------------------ # ------------------
readonly NS1="ns1-$(mktemp -u XXXXXX)"
readonly NS2="ns2-$(mktemp -u XXXXXX)"
ret=0 ret=0
setup() setup()
...@@ -17,27 +19,27 @@ setup() ...@@ -17,27 +19,27 @@ setup()
local xdpmode=$1 local xdpmode=$1
ip netns add ns1 ip netns add ${NS1}
ip netns add ns2 ip netns add ${NS2}
ip link add veth1 index 111 type veth peer name veth11 netns ns1 ip link add veth1 index 111 type veth peer name veth11 netns ${NS1}
ip link add veth2 index 222 type veth peer name veth22 netns ns2 ip link add veth2 index 222 type veth peer name veth22 netns ${NS2}
ip link set veth1 up ip link set veth1 up
ip link set veth2 up ip link set veth2 up
ip -n ns1 link set dev veth11 up ip -n ${NS1} link set dev veth11 up
ip -n ns2 link set dev veth22 up ip -n ${NS2} link set dev veth22 up
ip -n ns1 addr add 10.1.1.11/24 dev veth11 ip -n ${NS1} addr add 10.1.1.11/24 dev veth11
ip -n ns2 addr add 10.1.1.22/24 dev veth22 ip -n ${NS2} addr add 10.1.1.22/24 dev veth22
} }
cleanup() cleanup()
{ {
ip link del veth1 2> /dev/null ip link del veth1 2> /dev/null
ip link del veth2 2> /dev/null ip link del veth2 2> /dev/null
ip netns del ns1 2> /dev/null ip netns del ${NS1} 2> /dev/null
ip netns del ns2 2> /dev/null ip netns del ${NS2} 2> /dev/null
} }
test_xdp_redirect() test_xdp_redirect()
...@@ -52,13 +54,13 @@ test_xdp_redirect() ...@@ -52,13 +54,13 @@ test_xdp_redirect()
return 0 return 0
fi fi
ip -n ns1 link set veth11 $xdpmode obj xdp_dummy.o sec xdp &> /dev/null ip -n ${NS1} link set veth11 $xdpmode obj xdp_dummy.o sec xdp &> /dev/null
ip -n ns2 link set veth22 $xdpmode obj xdp_dummy.o sec xdp &> /dev/null ip -n ${NS2} link set veth22 $xdpmode obj xdp_dummy.o sec xdp &> /dev/null
ip link set dev veth1 $xdpmode obj test_xdp_redirect.o sec redirect_to_222 &> /dev/null ip link set dev veth1 $xdpmode obj test_xdp_redirect.o sec redirect_to_222 &> /dev/null
ip link set dev veth2 $xdpmode obj test_xdp_redirect.o sec redirect_to_111 &> /dev/null ip link set dev veth2 $xdpmode obj test_xdp_redirect.o sec redirect_to_111 &> /dev/null
if ip netns exec ns1 ping -c 1 10.1.1.22 &> /dev/null && if ip netns exec ${NS1} ping -c 1 10.1.1.22 &> /dev/null &&
ip netns exec ns2 ping -c 1 10.1.1.11 &> /dev/null; then ip netns exec ${NS2} ping -c 1 10.1.1.11 &> /dev/null; then
echo "selftests: test_xdp_redirect $xdpmode [PASS]"; echo "selftests: test_xdp_redirect $xdpmode [PASS]";
else else
ret=1 ret=1
......
...@@ -32,6 +32,11 @@ DRV_MODE="xdpgeneric xdpdrv xdpegress" ...@@ -32,6 +32,11 @@ DRV_MODE="xdpgeneric xdpdrv xdpegress"
PASS=0 PASS=0
FAIL=0 FAIL=0
LOG_DIR=$(mktemp -d) LOG_DIR=$(mktemp -d)
declare -a NS
NS[0]="ns0-$(mktemp -u XXXXXX)"
NS[1]="ns1-$(mktemp -u XXXXXX)"
NS[2]="ns2-$(mktemp -u XXXXXX)"
NS[3]="ns3-$(mktemp -u XXXXXX)"
test_pass() test_pass()
{ {
...@@ -47,11 +52,9 @@ test_fail() ...@@ -47,11 +52,9 @@ test_fail()
clean_up() clean_up()
{ {
for i in $(seq $NUM); do for i in $(seq 0 $NUM); do
ip link del veth$i 2> /dev/null ip netns del ${NS[$i]} 2> /dev/null
ip netns del ns$i 2> /dev/null
done done
ip netns del ns0 2> /dev/null
} }
# Kselftest framework requirement - SKIP code is 4. # Kselftest framework requirement - SKIP code is 4.
...@@ -79,23 +82,22 @@ setup_ns() ...@@ -79,23 +82,22 @@ setup_ns()
mode="xdpdrv" mode="xdpdrv"
fi fi
ip netns add ns0 ip netns add ${NS[0]}
for i in $(seq $NUM); do for i in $(seq $NUM); do
ip netns add ns$i ip netns add ${NS[$i]}
ip -n ns$i link add veth0 index 2 type veth \ ip -n ${NS[$i]} link add veth0 type veth peer name veth$i netns ${NS[0]}
peer name veth$i netns ns0 index $((1 + $i)) ip -n ${NS[$i]} link set veth0 up
ip -n ns0 link set veth$i up ip -n ${NS[0]} link set veth$i up
ip -n ns$i link set veth0 up
ip -n ${NS[$i]} addr add 192.0.2.$i/24 dev veth0
ip -n ns$i addr add 192.0.2.$i/24 dev veth0 ip -n ${NS[$i]} addr add 2001:db8::$i/64 dev veth0
ip -n ns$i addr add 2001:db8::$i/64 dev veth0
# Add a neigh entry for IPv4 ping test # Add a neigh entry for IPv4 ping test
ip -n ns$i neigh add 192.0.2.253 lladdr 00:00:00:00:00:01 dev veth0 ip -n ${NS[$i]} neigh add 192.0.2.253 lladdr 00:00:00:00:00:01 dev veth0
ip -n ns$i link set veth0 $mode obj \ ip -n ${NS[$i]} link set veth0 $mode obj \
xdp_dummy.o sec xdp &> /dev/null || \ xdp_dummy.o sec xdp &> /dev/null || \
{ test_fail "Unable to load dummy xdp" && exit 1; } { test_fail "Unable to load dummy xdp" && exit 1; }
IFACES="$IFACES veth$i" IFACES="$IFACES veth$i"
veth_mac[$i]=$(ip -n ns0 link show veth$i | awk '/link\/ether/ {print $2}') veth_mac[$i]=$(ip -n ${NS[0]} link show veth$i | awk '/link\/ether/ {print $2}')
done done
} }
...@@ -104,10 +106,10 @@ do_egress_tests() ...@@ -104,10 +106,10 @@ do_egress_tests()
local mode=$1 local mode=$1
# mac test # mac test
ip netns exec ns2 tcpdump -e -i veth0 -nn -l -e &> ${LOG_DIR}/mac_ns1-2_${mode}.log & ip netns exec ${NS[2]} tcpdump -e -i veth0 -nn -l -e &> ${LOG_DIR}/mac_ns1-2_${mode}.log &
ip netns exec ns3 tcpdump -e -i veth0 -nn -l -e &> ${LOG_DIR}/mac_ns1-3_${mode}.log & ip netns exec ${NS[3]} tcpdump -e -i veth0 -nn -l -e &> ${LOG_DIR}/mac_ns1-3_${mode}.log &
sleep 0.5 sleep 0.5
ip netns exec ns1 ping 192.0.2.254 -i 0.1 -c 4 &> /dev/null ip netns exec ${NS[1]} ping 192.0.2.254 -i 0.1 -c 4 &> /dev/null
sleep 0.5 sleep 0.5
pkill tcpdump pkill tcpdump
...@@ -123,18 +125,18 @@ do_ping_tests() ...@@ -123,18 +125,18 @@ do_ping_tests()
local mode=$1 local mode=$1
# ping6 test: echo request should be redirect back to itself, not others # ping6 test: echo request should be redirect back to itself, not others
ip netns exec ns1 ip neigh add 2001:db8::2 dev veth0 lladdr 00:00:00:00:00:02 ip netns exec ${NS[1]} ip neigh add 2001:db8::2 dev veth0 lladdr 00:00:00:00:00:02
ip netns exec ns1 tcpdump -i veth0 -nn -l -e &> ${LOG_DIR}/ns1-1_${mode}.log & ip netns exec ${NS[1]} tcpdump -i veth0 -nn -l -e &> ${LOG_DIR}/ns1-1_${mode}.log &
ip netns exec ns2 tcpdump -i veth0 -nn -l -e &> ${LOG_DIR}/ns1-2_${mode}.log & ip netns exec ${NS[2]} tcpdump -i veth0 -nn -l -e &> ${LOG_DIR}/ns1-2_${mode}.log &
ip netns exec ns3 tcpdump -i veth0 -nn -l -e &> ${LOG_DIR}/ns1-3_${mode}.log & ip netns exec ${NS[3]} tcpdump -i veth0 -nn -l -e &> ${LOG_DIR}/ns1-3_${mode}.log &
sleep 0.5 sleep 0.5
# ARP test # ARP test
ip netns exec ns1 arping -q -c 2 -I veth0 192.0.2.254 ip netns exec ${NS[1]} arping -q -c 2 -I veth0 192.0.2.254
# IPv4 test # IPv4 test
ip netns exec ns1 ping 192.0.2.253 -i 0.1 -c 4 &> /dev/null ip netns exec ${NS[1]} ping 192.0.2.253 -i 0.1 -c 4 &> /dev/null
# IPv6 test # IPv6 test
ip netns exec ns1 ping6 2001:db8::2 -i 0.1 -c 2 &> /dev/null ip netns exec ${NS[1]} ping6 2001:db8::2 -i 0.1 -c 2 &> /dev/null
sleep 0.5 sleep 0.5
pkill tcpdump pkill tcpdump
...@@ -180,7 +182,7 @@ do_tests() ...@@ -180,7 +182,7 @@ do_tests()
xdpgeneric) drv_p="-S";; xdpgeneric) drv_p="-S";;
esac esac
ip netns exec ns0 ./xdp_redirect_multi $drv_p $IFACES &> ${LOG_DIR}/xdp_redirect_${mode}.log & ip netns exec ${NS[0]} ./xdp_redirect_multi $drv_p $IFACES &> ${LOG_DIR}/xdp_redirect_${mode}.log &
xdp_pid=$! xdp_pid=$!
sleep 1 sleep 1
if ! ps -p $xdp_pid > /dev/null; then if ! ps -p $xdp_pid > /dev/null; then
...@@ -197,10 +199,10 @@ do_tests() ...@@ -197,10 +199,10 @@ do_tests()
kill $xdp_pid kill $xdp_pid
} }
trap clean_up EXIT
check_env check_env
trap clean_up EXIT
for mode in ${DRV_MODE}; do for mode in ${DRV_MODE}; do
setup_ns $mode setup_ns $mode
do_tests $mode do_tests $mode
......
...@@ -22,6 +22,9 @@ ksft_skip=4 ...@@ -22,6 +22,9 @@ ksft_skip=4
TESTNAME=xdp_veth TESTNAME=xdp_veth
BPF_FS=$(awk '$3 == "bpf" {print $2; exit}' /proc/mounts) BPF_FS=$(awk '$3 == "bpf" {print $2; exit}' /proc/mounts)
BPF_DIR=$BPF_FS/test_$TESTNAME BPF_DIR=$BPF_FS/test_$TESTNAME
readonly NS1="ns1-$(mktemp -u XXXXXX)"
readonly NS2="ns2-$(mktemp -u XXXXXX)"
readonly NS3="ns3-$(mktemp -u XXXXXX)"
_cleanup() _cleanup()
{ {
...@@ -29,9 +32,9 @@ _cleanup() ...@@ -29,9 +32,9 @@ _cleanup()
ip link del veth1 2> /dev/null ip link del veth1 2> /dev/null
ip link del veth2 2> /dev/null ip link del veth2 2> /dev/null
ip link del veth3 2> /dev/null ip link del veth3 2> /dev/null
ip netns del ns1 2> /dev/null ip netns del ${NS1} 2> /dev/null
ip netns del ns2 2> /dev/null ip netns del ${NS2} 2> /dev/null
ip netns del ns3 2> /dev/null ip netns del ${NS3} 2> /dev/null
rm -rf $BPF_DIR 2> /dev/null rm -rf $BPF_DIR 2> /dev/null
} }
...@@ -77,24 +80,24 @@ set -e ...@@ -77,24 +80,24 @@ set -e
trap cleanup_skip EXIT trap cleanup_skip EXIT
ip netns add ns1 ip netns add ${NS1}
ip netns add ns2 ip netns add ${NS2}
ip netns add ns3 ip netns add ${NS3}
ip link add veth1 index 111 type veth peer name veth11 netns ns1 ip link add veth1 index 111 type veth peer name veth11 netns ${NS1}
ip link add veth2 index 122 type veth peer name veth22 netns ns2 ip link add veth2 index 122 type veth peer name veth22 netns ${NS2}
ip link add veth3 index 133 type veth peer name veth33 netns ns3 ip link add veth3 index 133 type veth peer name veth33 netns ${NS3}
ip link set veth1 up ip link set veth1 up
ip link set veth2 up ip link set veth2 up
ip link set veth3 up ip link set veth3 up
ip -n ns1 addr add 10.1.1.11/24 dev veth11 ip -n ${NS1} addr add 10.1.1.11/24 dev veth11
ip -n ns3 addr add 10.1.1.33/24 dev veth33 ip -n ${NS3} addr add 10.1.1.33/24 dev veth33
ip -n ns1 link set dev veth11 up ip -n ${NS1} link set dev veth11 up
ip -n ns2 link set dev veth22 up ip -n ${NS2} link set dev veth22 up
ip -n ns3 link set dev veth33 up ip -n ${NS3} link set dev veth33 up
mkdir $BPF_DIR mkdir $BPF_DIR
bpftool prog loadall \ bpftool prog loadall \
...@@ -107,12 +110,12 @@ ip link set dev veth1 xdp pinned $BPF_DIR/progs/redirect_map_0 ...@@ -107,12 +110,12 @@ ip link set dev veth1 xdp pinned $BPF_DIR/progs/redirect_map_0
ip link set dev veth2 xdp pinned $BPF_DIR/progs/redirect_map_1 ip link set dev veth2 xdp pinned $BPF_DIR/progs/redirect_map_1
ip link set dev veth3 xdp pinned $BPF_DIR/progs/redirect_map_2 ip link set dev veth3 xdp pinned $BPF_DIR/progs/redirect_map_2
ip -n ns1 link set dev veth11 xdp obj xdp_dummy.o sec xdp ip -n ${NS1} link set dev veth11 xdp obj xdp_dummy.o sec xdp
ip -n ns2 link set dev veth22 xdp obj xdp_tx.o sec xdp ip -n ${NS2} link set dev veth22 xdp obj xdp_tx.o sec xdp
ip -n ns3 link set dev veth33 xdp obj xdp_dummy.o sec xdp ip -n ${NS3} link set dev veth33 xdp obj xdp_dummy.o sec xdp
trap cleanup EXIT trap cleanup EXIT
ip netns exec ns1 ping -c 1 -W 1 10.1.1.33 ip netns exec ${NS1} ping -c 1 -W 1 10.1.1.33
exit 0 exit 0
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
# Kselftest framework requirement - SKIP code is 4. # Kselftest framework requirement - SKIP code is 4.
readonly KSFT_SKIP=4 readonly KSFT_SKIP=4
readonly NS1="ns1-$(mktemp -u XXXXXX)"
readonly NS2="ns2-$(mktemp -u XXXXXX)"
# Allow wrapper scripts to name test # Allow wrapper scripts to name test
if [ -z "$TESTNAME" ]; then if [ -z "$TESTNAME" ]; then
...@@ -49,15 +51,15 @@ cleanup() ...@@ -49,15 +51,15 @@ cleanup()
if [ -n "$INTERACTIVE" ]; then if [ -n "$INTERACTIVE" ]; then
echo "Namespace setup still active explore with:" echo "Namespace setup still active explore with:"
echo " ip netns exec ns1 bash" echo " ip netns exec ${NS1} bash"
echo " ip netns exec ns2 bash" echo " ip netns exec ${NS2} bash"
exit $status exit $status
fi fi
set +e set +e
ip link del veth1 2> /dev/null ip link del veth1 2> /dev/null
ip netns del ns1 2> /dev/null ip netns del ${NS1} 2> /dev/null
ip netns del ns2 2> /dev/null ip netns del ${NS2} 2> /dev/null
} }
# Using external program "getopt" to get --long-options # Using external program "getopt" to get --long-options
...@@ -126,8 +128,8 @@ fi ...@@ -126,8 +128,8 @@ fi
# Interactive mode likely require us to cleanup netns # Interactive mode likely require us to cleanup netns
if [ -n "$INTERACTIVE" ]; then if [ -n "$INTERACTIVE" ]; then
ip link del veth1 2> /dev/null ip link del veth1 2> /dev/null
ip netns del ns1 2> /dev/null ip netns del ${NS1} 2> /dev/null
ip netns del ns2 2> /dev/null ip netns del ${NS2} 2> /dev/null
fi fi
# Exit on failure # Exit on failure
...@@ -144,8 +146,8 @@ if [ -n "$VERBOSE" ]; then ...@@ -144,8 +146,8 @@ if [ -n "$VERBOSE" ]; then
fi fi
# Create two namespaces # Create two namespaces
ip netns add ns1 ip netns add ${NS1}
ip netns add ns2 ip netns add ${NS2}
# Run cleanup if failing or on kill # Run cleanup if failing or on kill
trap cleanup 0 2 3 6 9 trap cleanup 0 2 3 6 9
...@@ -154,44 +156,44 @@ trap cleanup 0 2 3 6 9 ...@@ -154,44 +156,44 @@ trap cleanup 0 2 3 6 9
ip link add veth1 type veth peer name veth2 ip link add veth1 type veth peer name veth2
# Move veth1 and veth2 into the respective namespaces # Move veth1 and veth2 into the respective namespaces
ip link set veth1 netns ns1 ip link set veth1 netns ${NS1}
ip link set veth2 netns ns2 ip link set veth2 netns ${NS2}
# NOTICE: XDP require VLAN header inside packet payload # NOTICE: XDP require VLAN header inside packet payload
# - Thus, disable VLAN offloading driver features # - Thus, disable VLAN offloading driver features
# - For veth REMEMBER TX side VLAN-offload # - For veth REMEMBER TX side VLAN-offload
# #
# Disable rx-vlan-offload (mostly needed on ns1) # Disable rx-vlan-offload (mostly needed on ns1)
ip netns exec ns1 ethtool -K veth1 rxvlan off ip netns exec ${NS1} ethtool -K veth1 rxvlan off
ip netns exec ns2 ethtool -K veth2 rxvlan off ip netns exec ${NS2} ethtool -K veth2 rxvlan off
# #
# Disable tx-vlan-offload (mostly needed on ns2) # Disable tx-vlan-offload (mostly needed on ns2)
ip netns exec ns2 ethtool -K veth2 txvlan off ip netns exec ${NS2} ethtool -K veth2 txvlan off
ip netns exec ns1 ethtool -K veth1 txvlan off ip netns exec ${NS1} ethtool -K veth1 txvlan off
export IPADDR1=100.64.41.1 export IPADDR1=100.64.41.1
export IPADDR2=100.64.41.2 export IPADDR2=100.64.41.2
# In ns1/veth1 add IP-addr on plain net_device # In ns1/veth1 add IP-addr on plain net_device
ip netns exec ns1 ip addr add ${IPADDR1}/24 dev veth1 ip netns exec ${NS1} ip addr add ${IPADDR1}/24 dev veth1
ip netns exec ns1 ip link set veth1 up ip netns exec ${NS1} ip link set veth1 up
# In ns2/veth2 create VLAN device # In ns2/veth2 create VLAN device
export VLAN=4011 export VLAN=4011
export DEVNS2=veth2 export DEVNS2=veth2
ip netns exec ns2 ip link add link $DEVNS2 name $DEVNS2.$VLAN type vlan id $VLAN ip netns exec ${NS2} ip link add link $DEVNS2 name $DEVNS2.$VLAN type vlan id $VLAN
ip netns exec ns2 ip addr add ${IPADDR2}/24 dev $DEVNS2.$VLAN ip netns exec ${NS2} ip addr add ${IPADDR2}/24 dev $DEVNS2.$VLAN
ip netns exec ns2 ip link set $DEVNS2 up ip netns exec ${NS2} ip link set $DEVNS2 up
ip netns exec ns2 ip link set $DEVNS2.$VLAN up ip netns exec ${NS2} ip link set $DEVNS2.$VLAN up
# Bringup lo in netns (to avoids confusing people using --interactive) # Bringup lo in netns (to avoids confusing people using --interactive)
ip netns exec ns1 ip link set lo up ip netns exec ${NS1} ip link set lo up
ip netns exec ns2 ip link set lo up ip netns exec ${NS2} ip link set lo up
# At this point, the hosts cannot reach each-other, # At this point, the hosts cannot reach each-other,
# because ns2 are using VLAN tags on the packets. # because ns2 are using VLAN tags on the packets.
ip netns exec ns2 sh -c 'ping -W 1 -c 1 100.64.41.1 || echo "Success: First ping must fail"' ip netns exec ${NS2} sh -c 'ping -W 1 -c 1 100.64.41.1 || echo "Success: First ping must fail"'
# Now we can use the test_xdp_vlan.c program to pop/push these VLAN tags # Now we can use the test_xdp_vlan.c program to pop/push these VLAN tags
...@@ -202,19 +204,19 @@ export FILE=test_xdp_vlan.o ...@@ -202,19 +204,19 @@ export FILE=test_xdp_vlan.o
# First test: Remove VLAN by setting VLAN ID 0, using "xdp_vlan_change" # First test: Remove VLAN by setting VLAN ID 0, using "xdp_vlan_change"
export XDP_PROG=xdp_vlan_change export XDP_PROG=xdp_vlan_change
ip netns exec ns1 ip link set $DEVNS1 $XDP_MODE object $FILE section $XDP_PROG ip netns exec ${NS1} ip link set $DEVNS1 $XDP_MODE object $FILE section $XDP_PROG
# In ns1: egress use TC to add back VLAN tag 4011 # In ns1: egress use TC to add back VLAN tag 4011
# (del cmd) # (del cmd)
# tc qdisc del dev $DEVNS1 clsact 2> /dev/null # tc qdisc del dev $DEVNS1 clsact 2> /dev/null
# #
ip netns exec ns1 tc qdisc add dev $DEVNS1 clsact ip netns exec ${NS1} tc qdisc add dev $DEVNS1 clsact
ip netns exec ns1 tc filter add dev $DEVNS1 egress \ ip netns exec ${NS1} tc filter add dev $DEVNS1 egress \
prio 1 handle 1 bpf da obj $FILE sec tc_vlan_push prio 1 handle 1 bpf da obj $FILE sec tc_vlan_push
# Now the namespaces can reach each-other, test with ping: # Now the namespaces can reach each-other, test with ping:
ip netns exec ns2 ping -i 0.2 -W 2 -c 2 $IPADDR1 ip netns exec ${NS2} ping -i 0.2 -W 2 -c 2 $IPADDR1
ip netns exec ns1 ping -i 0.2 -W 2 -c 2 $IPADDR2 ip netns exec ${NS1} ping -i 0.2 -W 2 -c 2 $IPADDR2
# Second test: Replace xdp prog, that fully remove vlan header # Second test: Replace xdp prog, that fully remove vlan header
# #
...@@ -223,9 +225,9 @@ ip netns exec ns1 ping -i 0.2 -W 2 -c 2 $IPADDR2 ...@@ -223,9 +225,9 @@ ip netns exec ns1 ping -i 0.2 -W 2 -c 2 $IPADDR2
# ETH_P_8021Q indication, and this cause overwriting of our changes. # ETH_P_8021Q indication, and this cause overwriting of our changes.
# #
export XDP_PROG=xdp_vlan_remove_outer2 export XDP_PROG=xdp_vlan_remove_outer2
ip netns exec ns1 ip link set $DEVNS1 $XDP_MODE off ip netns exec ${NS1} ip link set $DEVNS1 $XDP_MODE off
ip netns exec ns1 ip link set $DEVNS1 $XDP_MODE object $FILE section $XDP_PROG ip netns exec ${NS1} ip link set $DEVNS1 $XDP_MODE object $FILE section $XDP_PROG
# Now the namespaces should still be able reach each-other, test with ping: # Now the namespaces should still be able reach each-other, test with ping:
ip netns exec ns2 ping -i 0.2 -W 2 -c 2 $IPADDR1 ip netns exec ${NS2} ping -i 0.2 -W 2 -c 2 $IPADDR1
ip netns exec ns1 ping -i 0.2 -W 2 -c 2 $IPADDR2 ip netns exec ${NS1} ping -i 0.2 -W 2 -c 2 $IPADDR2
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment