brintos

brintos / linux-shallow public Read only

0
0
Text · 16.9 KiB · 79fb81e Raw
497 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# author: Andrea Mayer <andrea.mayer@uniroma2.it>5 6# This test is designed for evaluating the new SRv6 End.DT4 behavior used for7# implementing IPv4 L3 VPN use cases.8#9# Hereafter a network diagram is shown, where two different tenants (named 10010# and 200) offer IPv4 L3 VPN services allowing hosts to communicate with each11# other across an IPv6 network.12#13# Only hosts belonging to the same tenant (and to the same VPN) can communicate14# with each other. Instead, the communication among hosts of different tenants15# is forbidden.16# In other words, hosts hs-t100-1 and hs-t100-2 are connected through the IPv417# L3 VPN of tenant 100 while hs-t200-3 and hs-t200-4 are connected using the18# IPv4 L3 VPN of tenant 200. Cross connection between tenant 100 and tenant 20019# is forbidden and thus, for example, hs-t100-1 cannot reach hs-t200-3 and vice20# versa.21#22# Routers rt-1 and rt-2 implement IPv4 L3 VPN services leveraging the SRv623# architecture. The key components for such VPNs are: a) SRv6 Encap behavior,24# b) SRv6 End.DT4 behavior and c) VRF.25#26# To explain how an IPv4 L3 VPN based on SRv6 works, let us briefly consider an27# example where, within the same domain of tenant 100, the host hs-t100-1 pings28# the host hs-t100-2.29#30# First of all, L2 reachability of the host hs-t100-2 is taken into account by31# the router rt-1 which acts as an arp proxy.32#33# When the host hs-t100-1 sends an IPv4 packet destined to hs-t100-2, the34# router rt-1 receives the packet on the internal veth-t100 interface. Such35# interface is enslaved to the VRF vrf-100 whose associated table contains the36# SRv6 Encap route for encapsulating any IPv4 packet in a IPv6 plus the Segment37# Routing Header (SRH) packet. This packet is sent through the (IPv6) core38# network up to the router rt-2 that receives it on veth0 interface.39#40# The rt-2 router uses the 'localsid' routing table to process incoming41# IPv6+SRH packets which belong to the VPN of the tenant 100. For each of these42# packets, the SRv6 End.DT4 behavior removes the outer IPv6+SRH headers and43# performs the lookup on the vrf-100 table using the destination address of44# the decapsulated IPv4 packet. Afterwards, the packet is sent to the host45# hs-t100-2 through the veth-t100 interface.46#47# The ping response follows the same processing but this time the role of rt-148# and rt-2 are swapped.49#50# Of course, the IPv4 L3 VPN for tenant 200 works exactly as the IPv4 L3 VPN51# for tenant 100. In this case, only hosts hs-t200-3 and hs-t200-4 are able to52# connect with each other.53#54#55# +-------------------+                                   +-------------------+56# |                   |                                   |                   |57# |  hs-t100-1 netns  |                                   |  hs-t100-2 netns  |58# |                   |                                   |                   |59# |  +-------------+  |                                   |  +-------------+  |60# |  |    veth0    |  |                                   |  |    veth0    |  |61# |  | 10.0.0.1/24 |  |                                   |  | 10.0.0.2/24 |  |62# |  +-------------+  |                                   |  +-------------+  |63# |        .          |                                   |         .         |64# +-------------------+                                   +-------------------+65#          .                                                        .66#          .                                                        .67#          .                                                        .68# +-----------------------------------+   +-----------------------------------+69# |        .                          |   |                         .         |70# | +---------------+                 |   |                 +---------------- |71# | |   veth-t100   |                 |   |                 |   veth-t100   | |72# | | 10.0.0.254/24 |    +----------+ |   | +----------+    | 10.0.0.254/24 | |73# | +-------+-------+    | localsid | |   | | localsid |    +-------+-------- |74# |         |            |   table  | |   | |   table  |            |         |75# |    +----+----+       +----------+ |   | +----------+       +----+----+    |76# |    | vrf-100 |                    |   |                    | vrf-100 |    |77# |    +---------+     +------------+ |   | +------------+     +---------+    |78# |                    |   veth0    | |   | |   veth0    |                    |79# |                    | fd00::1/64 |.|...|.| fd00::2/64 |                    |80# |    +---------+     +------------+ |   | +------------+     +---------+    |81# |    | vrf-200 |                    |   |                    | vrf-200 |    |82# |    +----+----+                    |   |                    +----+----+    |83# |         |                         |   |                         |         |84# | +-------+-------+                 |   |                 +-------+-------- |85# | |   veth-t200   |                 |   |                 |   veth-t200   | |86# | | 10.0.0.254/24 |                 |   |                 | 10.0.0.254/24 | |87# | +---------------+      rt-1 netns |   | rt-2 netns      +---------------- |88# |        .                          |   |                          .        |89# +-----------------------------------+   +-----------------------------------+90#          .                                                         .91#          .                                                         .92#          .                                                         .93#          .                                                         .94# +-------------------+                                   +-------------------+95# |        .          |                                   |          .        |96# |  +-------------+  |                                   |  +-------------+  |97# |  |    veth0    |  |                                   |  |    veth0    |  |98# |  | 10.0.0.3/24 |  |                                   |  | 10.0.0.4/24 |  |99# |  +-------------+  |                                   |  +-------------+  |100# |                   |                                   |                   |101# |  hs-t200-3 netns  |                                   |  hs-t200-4 netns  |102# |                   |                                   |                   |103# +-------------------+                                   +-------------------+104#105#106# ~~~~~~~~~~~~~~~~~~~~~~~~~107# | Network configuration |108# ~~~~~~~~~~~~~~~~~~~~~~~~~109#110# rt-1: localsid table (table 90)111# +-------------------------------------------------+112# |SID              |Action                         |113# +-------------------------------------------------+114# |fc00:21:100::6004|apply SRv6 End.DT4 vrftable 100|115# +-------------------------------------------------+116# |fc00:21:200::6004|apply SRv6 End.DT4 vrftable 200|117# +-------------------------------------------------+118#119# rt-1: VRF tenant 100 (table 100)120# +---------------------------------------------------+121# |host       |Action                                 |122# +---------------------------------------------------+123# |10.0.0.2   |apply seg6 encap segs fc00:12:100::6004|124# +---------------------------------------------------+125# |10.0.0.0/24|forward to dev veth_t100               |126# +---------------------------------------------------+127#128# rt-1: VRF tenant 200 (table 200)129# +---------------------------------------------------+130# |host       |Action                                 |131# +---------------------------------------------------+132# |10.0.0.4   |apply seg6 encap segs fc00:12:200::6004|133# +---------------------------------------------------+134# |10.0.0.0/24|forward to dev veth_t200               |135# +---------------------------------------------------+136#137#138# rt-2: localsid table (table 90)139# +-------------------------------------------------+140# |SID              |Action                         |141# +-------------------------------------------------+142# |fc00:12:100::6004|apply SRv6 End.DT4 vrftable 100|143# +-------------------------------------------------+144# |fc00:12:200::6004|apply SRv6 End.DT4 vrftable 200|145# +-------------------------------------------------+146#147# rt-2: VRF tenant 100 (table 100)148# +---------------------------------------------------+149# |host       |Action                                 |150# +---------------------------------------------------+151# |10.0.0.1   |apply seg6 encap segs fc00:21:100::6004|152# +---------------------------------------------------+153# |10.0.0.0/24|forward to dev veth_t100               |154# +---------------------------------------------------+155#156# rt-2: VRF tenant 200 (table 200)157# +---------------------------------------------------+158# |host       |Action                                 |159# +---------------------------------------------------+160# |10.0.0.3   |apply seg6 encap segs fc00:21:200::6004|161# +---------------------------------------------------+162# |10.0.0.0/24|forward to dev veth_t200               |163# +---------------------------------------------------+164#165 166source lib.sh167 168readonly LOCALSID_TABLE_ID=90169readonly IPv6_RT_NETWORK=fd00170readonly IPv4_HS_NETWORK=10.0.0171readonly VPN_LOCATOR_SERVICE=fc00172PING_TIMEOUT_SEC=4173 174ret=0175 176PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}177 178log_test()179{180	local rc=$1181	local expected=$2182	local msg="$3"183 184	if [ ${rc} -eq ${expected} ]; then185		nsuccess=$((nsuccess+1))186		printf "\n    TEST: %-60s  [ OK ]\n" "${msg}"187	else188		ret=1189		nfail=$((nfail+1))190		printf "\n    TEST: %-60s  [FAIL]\n" "${msg}"191		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then192			echo193			echo "hit enter to continue, 'q' to quit"194			read a195			[ "$a" = "q" ] && exit 1196		fi197	fi198}199 200print_log_test_results()201{202	if [ "$TESTS" != "none" ]; then203		printf "\nTests passed: %3d\n" ${nsuccess}204		printf "Tests failed: %3d\n"   ${nfail}205	fi206}207 208log_section()209{210	echo211	echo "################################################################################"212	echo "TEST SECTION: $*"213	echo "################################################################################"214}215 216cleanup()217{218	ip link del veth-rt-1 2>/dev/null || true219	ip link del veth-rt-2 2>/dev/null || true220 221	cleanup_all_ns222}223 224# Setup the basic networking for the routers225setup_rt_networking()226{227	local id=$1228	eval local nsname=\${rt_${id}}229 230	ip netns exec ${nsname} sysctl -wq net.ipv6.conf.all.accept_dad=0231	ip netns exec ${nsname} sysctl -wq net.ipv6.conf.default.accept_dad=0232 233	ip link set veth-rt-${id} netns ${nsname}234	ip -netns ${nsname} link set veth-rt-${id} name veth0235 236	ip -netns ${nsname} addr add ${IPv6_RT_NETWORK}::${id}/64 dev veth0 nodad237	ip -netns ${nsname} link set veth0 up238	ip -netns ${nsname} link set lo up239 240	ip netns exec ${nsname} sysctl -wq net.ipv4.ip_forward=1241	ip netns exec ${nsname} sysctl -wq net.ipv6.conf.all.forwarding=1242}243 244setup_hs()245{246	local hid=$1247	local rid=$2248	local tid=$3249	eval local hsname=\${hs_t${tid}_${hid}}250	eval local rtname=\${rt_${rid}}251	local rtveth=veth-t${tid}252 253	# disable the rp_filter otherwise the kernel gets confused about how254	# to route decap ipv4 packets.255	ip netns exec ${rtname} sysctl -wq net.ipv4.conf.all.rp_filter=0256	ip netns exec ${rtname} sysctl -wq net.ipv4.conf.default.rp_filter=0257 258	ip -netns ${hsname} link add veth0 type veth peer name ${rtveth}259	ip -netns ${hsname} link set ${rtveth} netns ${rtname}260	ip -netns ${hsname} addr add ${IPv4_HS_NETWORK}.${hid}/24 dev veth0261	ip -netns ${hsname} link set veth0 up262	ip -netns ${hsname} link set lo up263 264	# configure the VRF for the tenant X on the router which is directly265	# connected to the source host.266	ip -netns ${rtname} link add vrf-${tid} type vrf table ${tid}267	ip -netns ${rtname} link set vrf-${tid} up268 269	# enslave the veth-tX interface to the vrf-X in the access router270	ip -netns ${rtname} link set ${rtveth} master vrf-${tid}271	ip -netns ${rtname} addr add ${IPv4_HS_NETWORK}.254/24 dev ${rtveth}272	ip -netns ${rtname} link set ${rtveth} up273 274	ip netns exec ${rtname} sysctl -wq net.ipv4.conf.${rtveth}.proxy_arp=1275 276	ip netns exec ${rtname} sh -c "echo 1 > /proc/sys/net/vrf/strict_mode"277}278 279setup_vpn_config()280{281	local hssrc=$1282	local rtsrc=$2283	local hsdst=$3284	local rtdst=$4285	local tid=$5286 287	eval local rtsrc_name=\${rt_${rtsrc}}288	eval local rtdst_name=\${rt_${rtdst}}289	local vpn_sid=${VPN_LOCATOR_SERVICE}:${hssrc}${hsdst}:${tid}::6004290 291	# set the encap route for encapsulating packets which arrive from the292	# host hssrc and destined to the access router rtsrc.293	ip -netns ${rtsrc_name} -4 route add ${IPv4_HS_NETWORK}.${hsdst}/32 vrf vrf-${tid} \294		encap seg6 mode encap segs ${vpn_sid} dev veth0295	ip -netns ${rtsrc_name} -6 route add ${vpn_sid}/128 vrf vrf-${tid} \296		via fd00::${rtdst} dev veth0297 298	# set the decap route for decapsulating packets which arrive from299	# the rtdst router and destined to the hsdst host.300	ip -netns ${rtdst_name} -6 route add ${vpn_sid}/128 table ${LOCALSID_TABLE_ID} \301		encap seg6local action End.DT4 vrftable ${tid} dev vrf-${tid}302 303	# all sids for VPNs start with a common locator which is fc00::/16.304	# Routes for handling the SRv6 End.DT4 behavior instances are grouped305	# together in the 'localsid' table.306	#307	# NOTE: added only once308	if [ -z "$(ip -netns ${rtdst_name} -6 rule show | \309	    grep "to ${VPN_LOCATOR_SERVICE}::/16 lookup ${LOCALSID_TABLE_ID}")" ]; then310		ip -netns ${rtdst_name} -6 rule add \311			to ${VPN_LOCATOR_SERVICE}::/16 \312			lookup ${LOCALSID_TABLE_ID} prio 999313	fi314}315 316setup()317{318	ip link add veth-rt-1 type veth peer name veth-rt-2319	# setup the networking for router rt-1 and router rt-2320	setup_ns rt_1 rt_2321	setup_rt_networking 1322	setup_rt_networking 2323 324	# setup two hosts for the tenant 100.325	#  - host hs-1 is directly connected to the router rt-1;326	#  - host hs-2 is directly connected to the router rt-2.327	setup_ns hs_t100_1 hs_t100_2328	setup_hs 1 1 100  #args: host router tenant329	setup_hs 2 2 100330 331	# setup two hosts for the tenant 200332	#  - host hs-3 is directly connected to the router rt-1;333	#  - host hs-4 is directly connected to the router rt-2.334	setup_ns hs_t200_3 hs_t200_4335	setup_hs 3 1 200336	setup_hs 4 2 200337 338	# setup the IPv4 L3 VPN which connects the host hs-t100-1 and host339	# hs-t100-2 within the same tenant 100.340	setup_vpn_config 1 1 2 2 100  #args: src_host src_router dst_host dst_router tenant341	setup_vpn_config 2 2 1 1 100342 343	# setup the IPv4 L3 VPN which connects the host hs-t200-3 and host344	# hs-t200-4 within the same tenant 200.345	setup_vpn_config 3 1 4 2 200346	setup_vpn_config 4 2 3 1 200347}348 349check_rt_connectivity()350{351	local rtsrc=$1352	local rtdst=$2353	eval local nsname=\${rt_${rtsrc}}354 355	ip netns exec ${nsname} ping -c 1 -W 1 ${IPv6_RT_NETWORK}::${rtdst} \356		>/dev/null 2>&1357}358 359check_and_log_rt_connectivity()360{361	local rtsrc=$1362	local rtdst=$2363 364	check_rt_connectivity ${rtsrc} ${rtdst}365	log_test $? 0 "Routers connectivity: rt-${rtsrc} -> rt-${rtdst}"366}367 368check_hs_connectivity()369{370	local hssrc=$1371	local hsdst=$2372	local tid=$3373	eval local nsname=\${hs_t${tid}_${hssrc}}374 375	ip netns exec ${nsname} ping -c 1 -W ${PING_TIMEOUT_SEC} \376		${IPv4_HS_NETWORK}.${hsdst} >/dev/null 2>&1377}378 379check_and_log_hs_connectivity()380{381	local hssrc=$1382	local hsdst=$2383	local tid=$3384 385	check_hs_connectivity ${hssrc} ${hsdst} ${tid}386	log_test $? 0 "Hosts connectivity: hs-t${tid}-${hssrc} -> hs-t${tid}-${hsdst} (tenant ${tid})"387}388 389check_and_log_hs_isolation()390{391	local hssrc=$1392	local tidsrc=$2393	local hsdst=$3394	local tiddst=$4395 396	check_hs_connectivity ${hssrc} ${hsdst} ${tidsrc}397	# NOTE: ping should fail398	log_test $? 1 "Hosts isolation: hs-t${tidsrc}-${hssrc} -X-> hs-t${tiddst}-${hsdst}"399}400 401 402check_and_log_hs2gw_connectivity()403{404	local hssrc=$1405	local tid=$2406 407	check_hs_connectivity ${hssrc} 254 ${tid}408	log_test $? 0 "Hosts connectivity: hs-t${tid}-${hssrc} -> gw (tenant ${tid})"409}410 411router_tests()412{413	log_section "IPv6 routers connectivity test"414 415	check_and_log_rt_connectivity 1 2416	check_and_log_rt_connectivity 2 1417}418 419host2gateway_tests()420{421	log_section "IPv4 connectivity test among hosts and gateway"422 423	check_and_log_hs2gw_connectivity 1 100424	check_and_log_hs2gw_connectivity 2 100425 426	check_and_log_hs2gw_connectivity 3 200427	check_and_log_hs2gw_connectivity 4 200428}429 430host_vpn_tests()431{432	log_section "SRv6 VPN connectivity test among hosts in the same tenant"433 434	check_and_log_hs_connectivity 1 2 100435	check_and_log_hs_connectivity 2 1 100436 437	check_and_log_hs_connectivity 3 4 200438	check_and_log_hs_connectivity 4 3 200439}440 441host_vpn_isolation_tests()442{443	local i444	local j445	local k446	local tmp447	local l1="1 2"448	local l2="3 4"449	local t1=100450	local t2=200451 452	log_section "SRv6 VPN isolation test among hosts in different tentants"453 454	for k in 0 1; do455		for i in ${l1}; do456			for j in ${l2}; do457				check_and_log_hs_isolation ${i} ${t1} ${j} ${t2}458			done459		done460 461		# let us test the reverse path462		tmp="${l1}"; l1="${l2}"; l2="${tmp}"463		tmp=${t1}; t1=${t2}; t2=${tmp}464	done465}466 467if [ "$(id -u)" -ne 0 ];then468	echo "SKIP: Need root privileges"469	exit $ksft_skip470fi471 472if [ ! -x "$(command -v ip)" ]; then473	echo "SKIP: Could not run test without ip tool"474	exit $ksft_skip475fi476 477modprobe vrf &>/dev/null478if [ ! -e /proc/sys/net/vrf/strict_mode ]; then479        echo "SKIP: vrf sysctl does not exist"480        exit $ksft_skip481fi482 483cleanup &>/dev/null484 485setup486 487router_tests488host2gateway_tests489host_vpn_tests490host_vpn_isolation_tests491 492print_log_test_results493 494cleanup &>/dev/null495 496exit ${ret}497