提交 74cf6795 编写于 作者: P Paolo Abeni

Merge branch 'selftests-net-isolate-l2_tos_ttl_inherit-sh-in-its-own-netns'

Guillaume Nault says:

====================
selftests/net: Isolate l2_tos_ttl_inherit.sh in its own netns.

l2_tos_ttl_inherit.sh uses a veth pair to run its tests, but only one
of the veth interfaces runs in a dedicated netns. The other one remains
in the initial namespace where the existing network configuration can
interfere with the setup used for the tests.

Isolate both veth devices in their own netns and ensure everything gets
cleaned up when the script exits.

Link: https://lore.kernel.org/netdev/924f1062-ab59-9b88-3b43-c44e73a30387@alu.unizg.hr/
====================

Link: https://lore.kernel.org/r/cover.1673191942.git.gnault@redhat.comSigned-off-by: NPaolo Abeni <pabeni@redhat.com>
...@@ -12,19 +12,27 @@ ...@@ -12,19 +12,27 @@
# In addition this script also checks if forcing a specific field in the # In addition this script also checks if forcing a specific field in the
# outer header is working. # outer header is working.
# Return 4 by default (Kselftest SKIP code)
ERR=4
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
echo "Please run as root." echo "Please run as root."
exit 0 exit $ERR
fi fi
if ! which tcpdump > /dev/null 2>&1; then if ! which tcpdump > /dev/null 2>&1; then
echo "No tcpdump found. Required for this test." echo "No tcpdump found. Required for this test."
exit 0 exit $ERR
fi fi
expected_tos="0x00" expected_tos="0x00"
expected_ttl="0" expected_ttl="0"
failed=false failed=false
readonly NS0=$(mktemp -u ns0-XXXXXXXX)
readonly NS1=$(mktemp -u ns1-XXXXXXXX)
RUN_NS0="ip netns exec ${NS0}"
get_random_tos() { get_random_tos() {
# Get a random hex tos value between 0x00 and 0xfc, a multiple of 4 # Get a random hex tos value between 0x00 and 0xfc, a multiple of 4
echo "0x$(tr -dc '0-9a-f' < /dev/urandom | head -c 1)\ echo "0x$(tr -dc '0-9a-f' < /dev/urandom | head -c 1)\
...@@ -61,7 +69,6 @@ setup() { ...@@ -61,7 +69,6 @@ setup() {
local vlan="$5" local vlan="$5"
local test_tos="0x00" local test_tos="0x00"
local test_ttl="0" local test_ttl="0"
local ns="ip netns exec testing"
# We don't want a test-tos of 0x00, # We don't want a test-tos of 0x00,
# because this is the value that we get when no tos is set. # because this is the value that we get when no tos is set.
...@@ -94,14 +101,15 @@ setup() { ...@@ -94,14 +101,15 @@ setup() {
printf "│%7s │%6s │%6s │%13s │%13s │%6s │" \ printf "│%7s │%6s │%6s │%13s │%13s │%6s │" \
"$type" "$outer" "$inner" "$tos" "$ttl" "$vlan" "$type" "$outer" "$inner" "$tos" "$ttl" "$vlan"
# Create 'testing' netns, veth pair and connect main ns with testing ns # Create netns NS0 and NS1 and connect them with a veth pair
ip netns add testing ip netns add "${NS0}"
ip link add type veth ip netns add "${NS1}"
ip link set veth1 netns testing ip link add name veth0 netns "${NS0}" type veth \
ip link set veth0 up peer name veth1 netns "${NS1}"
$ns ip link set veth1 up ip -netns "${NS0}" link set dev veth0 up
ip addr flush dev veth0 ip -netns "${NS1}" link set dev veth1 up
$ns ip addr flush dev veth1 ip -netns "${NS0}" address flush dev veth0
ip -netns "${NS1}" address flush dev veth1
local local_addr1="" local local_addr1=""
local local_addr2="" local local_addr2=""
...@@ -127,51 +135,59 @@ setup() { ...@@ -127,51 +135,59 @@ setup() {
if [ "$type" = "gre" ]; then if [ "$type" = "gre" ]; then
type="gretap" type="gretap"
fi fi
ip addr add 198.18.0.1/24 dev veth0 ip -netns "${NS0}" address add 198.18.0.1/24 dev veth0
$ns ip addr add 198.18.0.2/24 dev veth1 ip -netns "${NS1}" address add 198.18.0.2/24 dev veth1
ip link add name tep0 type $type $local_addr1 remote \ ip -netns "${NS0}" link add name tep0 type $type $local_addr1 \
198.18.0.2 tos $test_tos ttl $test_ttl $vxlan $geneve remote 198.18.0.2 tos $test_tos ttl $test_ttl \
$ns ip link add name tep1 type $type $local_addr2 remote \ $vxlan $geneve
198.18.0.1 tos $test_tos ttl $test_ttl $vxlan $geneve ip -netns "${NS1}" link add name tep1 type $type $local_addr2 \
remote 198.18.0.1 tos $test_tos ttl $test_ttl \
$vxlan $geneve
elif [ "$outer" = "6" ]; then elif [ "$outer" = "6" ]; then
if [ "$type" = "gre" ]; then if [ "$type" = "gre" ]; then
type="ip6gretap" type="ip6gretap"
fi fi
ip addr add fdd1:ced0:5d88:3fce::1/64 dev veth0 ip -netns "${NS0}" address add fdd1:ced0:5d88:3fce::1/64 \
$ns ip addr add fdd1:ced0:5d88:3fce::2/64 dev veth1 dev veth0 nodad
ip link add name tep0 type $type $local_addr1 \ ip -netns "${NS1}" address add fdd1:ced0:5d88:3fce::2/64 \
remote fdd1:ced0:5d88:3fce::2 tos $test_tos ttl $test_ttl \ dev veth1 nodad
$vxlan $geneve ip -netns "${NS0}" link add name tep0 type $type $local_addr1 \
$ns ip link add name tep1 type $type $local_addr2 \ remote fdd1:ced0:5d88:3fce::2 tos $test_tos \
remote fdd1:ced0:5d88:3fce::1 tos $test_tos ttl $test_ttl \ ttl $test_ttl $vxlan $geneve
$vxlan $geneve ip -netns "${NS1}" link add name tep1 type $type $local_addr2 \
remote fdd1:ced0:5d88:3fce::1 tos $test_tos \
ttl $test_ttl $vxlan $geneve
fi fi
# Bring L2-tunnel link up and create VLAN on top # Bring L2-tunnel link up and create VLAN on top
ip link set tep0 up ip -netns "${NS0}" link set tep0 up
$ns ip link set tep1 up ip -netns "${NS1}" link set tep1 up
ip addr flush dev tep0 ip -netns "${NS0}" address flush dev tep0
$ns ip addr flush dev tep1 ip -netns "${NS1}" address flush dev tep1
local parent local parent
if $vlan; then if $vlan; then
parent="vlan99-" parent="vlan99-"
ip link add link tep0 name ${parent}0 type vlan id 99 ip -netns "${NS0}" link add link tep0 name ${parent}0 \
$ns ip link add link tep1 name ${parent}1 type vlan id 99 type vlan id 99
ip link set ${parent}0 up ip -netns "${NS1}" link add link tep1 name ${parent}1 \
$ns ip link set ${parent}1 up type vlan id 99
ip addr flush dev ${parent}0 ip -netns "${NS0}" link set dev ${parent}0 up
$ns ip addr flush dev ${parent}1 ip -netns "${NS1}" link set dev ${parent}1 up
ip -netns "${NS0}" address flush dev ${parent}0
ip -netns "${NS1}" address flush dev ${parent}1
else else
parent="tep" parent="tep"
fi fi
# Assign inner IPv4/IPv6 addresses # Assign inner IPv4/IPv6 addresses
if [ "$inner" = "4" ] || [ "$inner" = "other" ]; then if [ "$inner" = "4" ] || [ "$inner" = "other" ]; then
ip addr add 198.19.0.1/24 brd + dev ${parent}0 ip -netns "${NS0}" address add 198.19.0.1/24 brd + dev ${parent}0
$ns ip addr add 198.19.0.2/24 brd + dev ${parent}1 ip -netns "${NS1}" address add 198.19.0.2/24 brd + dev ${parent}1
elif [ "$inner" = "6" ]; then elif [ "$inner" = "6" ]; then
ip addr add fdd4:96cf:4eae:443b::1/64 dev ${parent}0 ip -netns "${NS0}" address add fdd4:96cf:4eae:443b::1/64 \
$ns ip addr add fdd4:96cf:4eae:443b::2/64 dev ${parent}1 dev ${parent}0 nodad
ip -netns "${NS1}" address add fdd4:96cf:4eae:443b::2/64 \
dev ${parent}1 nodad
fi fi
} }
...@@ -192,10 +208,10 @@ verify() { ...@@ -192,10 +208,10 @@ verify() {
ping_dst="198.19.0.3" # Generates ARPs which are not IPv4/IPv6 ping_dst="198.19.0.3" # Generates ARPs which are not IPv4/IPv6
fi fi
if [ "$tos_ttl" = "inherit" ]; then if [ "$tos_ttl" = "inherit" ]; then
ping -i 0.1 $ping_dst -Q "$expected_tos" -t "$expected_ttl" \ ${RUN_NS0} ping -i 0.1 $ping_dst -Q "$expected_tos" \
2>/dev/null 1>&2 & ping_pid="$!" -t "$expected_ttl" 2>/dev/null 1>&2 & ping_pid="$!"
else else
ping -i 0.1 $ping_dst 2>/dev/null 1>&2 & ping_pid="$!" ${RUN_NS0} ping -i 0.1 $ping_dst 2>/dev/null 1>&2 & ping_pid="$!"
fi fi
local tunnel_type_offset tunnel_type_proto req_proto_offset req_offset local tunnel_type_offset tunnel_type_proto req_proto_offset req_offset
if [ "$type" = "gre" ]; then if [ "$type" = "gre" ]; then
...@@ -216,10 +232,12 @@ verify() { ...@@ -216,10 +232,12 @@ verify() {
req_proto_offset="$((req_proto_offset + 4))" req_proto_offset="$((req_proto_offset + 4))"
req_offset="$((req_offset + 4))" req_offset="$((req_offset + 4))"
fi fi
out="$(tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \ out="$(${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
-i veth0 -n \
ip[$tunnel_type_offset] = $tunnel_type_proto and \ ip[$tunnel_type_offset] = $tunnel_type_proto and \
ip[$req_proto_offset] = 0x01 and \ ip[$req_proto_offset] = 0x01 and \
ip[$req_offset] = 0x08 2>/dev/null | head -n 1)" ip[$req_offset] = 0x08 2>/dev/null \
| head -n 1)"
elif [ "$inner" = "6" ]; then elif [ "$inner" = "6" ]; then
req_proto_offset="44" req_proto_offset="44"
req_offset="78" req_offset="78"
...@@ -231,10 +249,12 @@ verify() { ...@@ -231,10 +249,12 @@ verify() {
req_proto_offset="$((req_proto_offset + 4))" req_proto_offset="$((req_proto_offset + 4))"
req_offset="$((req_offset + 4))" req_offset="$((req_offset + 4))"
fi fi
out="$(tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \ out="$(${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
-i veth0 -n \
ip[$tunnel_type_offset] = $tunnel_type_proto and \ ip[$tunnel_type_offset] = $tunnel_type_proto and \
ip[$req_proto_offset] = 0x3a and \ ip[$req_proto_offset] = 0x3a and \
ip[$req_offset] = 0x80 2>/dev/null | head -n 1)" ip[$req_offset] = 0x80 2>/dev/null \
| head -n 1)"
elif [ "$inner" = "other" ]; then elif [ "$inner" = "other" ]; then
req_proto_offset="36" req_proto_offset="36"
req_offset="45" req_offset="45"
...@@ -250,11 +270,13 @@ verify() { ...@@ -250,11 +270,13 @@ verify() {
expected_tos="0x00" expected_tos="0x00"
expected_ttl="64" expected_ttl="64"
fi fi
out="$(tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \ out="$(${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
-i veth0 -n \
ip[$tunnel_type_offset] = $tunnel_type_proto and \ ip[$tunnel_type_offset] = $tunnel_type_proto and \
ip[$req_proto_offset] = 0x08 and \ ip[$req_proto_offset] = 0x08 and \
ip[$((req_proto_offset + 1))] = 0x06 and \ ip[$((req_proto_offset + 1))] = 0x06 and \
ip[$req_offset] = 0x01 2>/dev/null | head -n 1)" ip[$req_offset] = 0x01 2>/dev/null \
| head -n 1)"
fi fi
elif [ "$outer" = "6" ]; then elif [ "$outer" = "6" ]; then
if [ "$type" = "gre" ]; then if [ "$type" = "gre" ]; then
...@@ -273,10 +295,12 @@ verify() { ...@@ -273,10 +295,12 @@ verify() {
req_proto_offset="$((req_proto_offset + 4))" req_proto_offset="$((req_proto_offset + 4))"
req_offset="$((req_offset + 4))" req_offset="$((req_offset + 4))"
fi fi
out="$(tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \ out="$(${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
-i veth0 -n \
ip6[$tunnel_type_offset] = $tunnel_type_proto and \ ip6[$tunnel_type_offset] = $tunnel_type_proto and \
ip6[$req_proto_offset] = 0x01 and \ ip6[$req_proto_offset] = 0x01 and \
ip6[$req_offset] = 0x08 2>/dev/null | head -n 1)" ip6[$req_offset] = 0x08 2>/dev/null \
| head -n 1)"
elif [ "$inner" = "6" ]; then elif [ "$inner" = "6" ]; then
local req_proto_offset="72" local req_proto_offset="72"
local req_offset="106" local req_offset="106"
...@@ -288,10 +312,12 @@ verify() { ...@@ -288,10 +312,12 @@ verify() {
req_proto_offset="$((req_proto_offset + 4))" req_proto_offset="$((req_proto_offset + 4))"
req_offset="$((req_offset + 4))" req_offset="$((req_offset + 4))"
fi fi
out="$(tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \ out="$(${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
-i veth0 -n \
ip6[$tunnel_type_offset] = $tunnel_type_proto and \ ip6[$tunnel_type_offset] = $tunnel_type_proto and \
ip6[$req_proto_offset] = 0x3a and \ ip6[$req_proto_offset] = 0x3a and \
ip6[$req_offset] = 0x80 2>/dev/null | head -n 1)" ip6[$req_offset] = 0x80 2>/dev/null \
| head -n 1)"
elif [ "$inner" = "other" ]; then elif [ "$inner" = "other" ]; then
local req_proto_offset="64" local req_proto_offset="64"
local req_offset="73" local req_offset="73"
...@@ -307,15 +333,17 @@ verify() { ...@@ -307,15 +333,17 @@ verify() {
expected_tos="0x00" expected_tos="0x00"
expected_ttl="64" expected_ttl="64"
fi fi
out="$(tcpdump --immediate-mode -p -c 1 -v -i veth0 -n \ out="$(${RUN_NS0} tcpdump --immediate-mode -p -c 1 -v \
-i veth0 -n \
ip6[$tunnel_type_offset] = $tunnel_type_proto and \ ip6[$tunnel_type_offset] = $tunnel_type_proto and \
ip6[$req_proto_offset] = 0x08 and \ ip6[$req_proto_offset] = 0x08 and \
ip6[$((req_proto_offset + 1))] = 0x06 and \ ip6[$((req_proto_offset + 1))] = 0x06 and \
ip6[$req_offset] = 0x01 2>/dev/null | head -n 1)" ip6[$req_offset] = 0x01 2>/dev/null \
| head -n 1)"
fi fi
fi fi
kill -9 $ping_pid kill -9 $ping_pid
wait $ping_pid 2>/dev/null wait $ping_pid 2>/dev/null || true
result="FAIL" result="FAIL"
if [ "$outer" = "4" ]; then if [ "$outer" = "4" ]; then
captured_ttl="$(get_field "ttl" "$out")" captured_ttl="$(get_field "ttl" "$out")"
...@@ -351,11 +379,35 @@ verify() { ...@@ -351,11 +379,35 @@ verify() {
} }
cleanup() { cleanup() {
ip link del veth0 2>/dev/null ip netns del "${NS0}" 2>/dev/null
ip netns del testing 2>/dev/null ip netns del "${NS1}" 2>/dev/null
ip link del tep0 2>/dev/null }
exit_handler() {
# Don't exit immediately if one of the intermediate commands fails.
# We might be called at the end of the script, when the network
# namespaces have already been deleted. So cleanup() may fail, but we
# still need to run until 'exit $ERR' or the script won't return the
# correct error code.
set +e
cleanup
exit $ERR
} }
# Restore the default SIGINT handler (just in case) and exit.
# The exit handler will take care of cleaning everything up.
interrupted() {
trap - INT
exit $ERR
}
set -e
trap exit_handler EXIT
trap interrupted INT
printf "┌────────┬───────┬───────┬──────────────┬" printf "┌────────┬───────┬───────┬──────────────┬"
printf "──────────────┬───────┬────────┐\n" printf "──────────────┬───────┬────────┐\n"
for type in gre vxlan geneve; do for type in gre vxlan geneve; do
...@@ -385,6 +437,10 @@ done ...@@ -385,6 +437,10 @@ done
printf "└────────┴───────┴───────┴──────────────┴" printf "└────────┴───────┴───────┴──────────────┴"
printf "──────────────┴───────┴────────┘\n" printf "──────────────┴───────┴────────┘\n"
# All tests done.
# Set ERR appropriately: it will be returned by the exit handler.
if $failed; then if $failed; then
exit 1 ERR=1
else
ERR=0
fi fi
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册