502 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# author: Andrea Mayer <andrea.mayer@uniroma2.it>5# author: Paolo Lungaroni <paolo.lungaroni@cnit.it>6 7# This test is designed for evaluating the new SRv6 End.DT6 behavior used for8# implementing IPv6 L3 VPN use cases.9#10# Hereafter a network diagram is shown, where two different tenants (named 10011# and 200) offer IPv6 L3 VPN services allowing hosts to communicate with each12# other across an IPv6 network.13#14# Only hosts belonging to the same tenant (and to the same VPN) can communicate15# with each other. Instead, the communication among hosts of different tenants16# is forbidden.17# In other words, hosts hs-t100-1 and hs-t100-2 are connected through the IPv618# L3 VPN of tenant 100 while hs-t200-3 and hs-t200-4 are connected using the19# IPv6 L3 VPN of tenant 200. Cross connection between tenant 100 and tenant 20020# is forbidden and thus, for example, hs-t100-1 cannot reach hs-t200-3 and vice21# versa.22#23# Routers rt-1 and rt-2 implement IPv6 L3 VPN services leveraging the SRv624# architecture. The key components for such VPNs are: a) SRv6 Encap behavior,25# b) SRv6 End.DT6 behavior and c) VRF.26#27# To explain how an IPv6 L3 VPN based on SRv6 works, let us briefly consider an28# example where, within the same domain of tenant 100, the host hs-t100-1 pings29# the host hs-t100-2.30#31# First of all, L2 reachability of the host hs-t100-2 is taken into account by32# the router rt-1 which acts as a ndp proxy.33#34# When the host hs-t100-1 sends an IPv6 packet destined to hs-t100-2, the35# router rt-1 receives the packet on the internal veth-t100 interface. Such36# interface is enslaved to the VRF vrf-100 whose associated table contains the37# SRv6 Encap route for encapsulating any IPv6 packet in a IPv6 plus the Segment38# Routing Header (SRH) packet. This packet is sent through the (IPv6) core39# network up to the router rt-2 that receives it on veth0 interface.40#41# The rt-2 router uses the 'localsid' routing table to process incoming42# IPv6+SRH packets which belong to the VPN of the tenant 100. For each of these43# packets, the SRv6 End.DT6 behavior removes the outer IPv6+SRH headers and44# performs the lookup on the vrf-100 table using the destination address of45# the decapsulated IPv6 packet. Afterwards, the packet is sent to the host46# hs-t100-2 through the veth-t100 interface.47#48# The ping response follows the same processing but this time the role of rt-149# and rt-2 are swapped.50#51# Of course, the IPv6 L3 VPN for tenant 200 works exactly as the IPv6 L3 VPN52# for tenant 100. In this case, only hosts hs-t200-3 and hs-t200-4 are able to53# connect with each other.54#55#56# +-------------------+ +-------------------+57# | | | |58# | hs-t100-1 netns | | hs-t100-2 netns |59# | | | |60# | +-------------+ | | +-------------+ |61# | | veth0 | | | | veth0 | |62# | | cafe::1/64 | | | | cafe::2/64 | |63# | +-------------+ | | +-------------+ |64# | . | | . |65# +-------------------+ +-------------------+66# . .67# . .68# . .69# +-----------------------------------+ +-----------------------------------+70# | . | | . |71# | +---------------+ | | +---------------- |72# | | veth-t100 | | | | veth-t100 | |73# | | cafe::254/64 | +----------+ | | +----------+ | cafe::254/64 | |74# | +-------+-------+ | localsid | | | | localsid | +-------+-------- |75# | | | table | | | | table | | |76# | +----+----+ +----------+ | | +----------+ +----+----+ |77# | | vrf-100 | | | | vrf-100 | |78# | +---------+ +------------+ | | +------------+ +---------+ |79# | | veth0 | | | | veth0 | |80# | | fd00::1/64 |.|...|.| fd00::2/64 | |81# | +---------+ +------------+ | | +------------+ +---------+ |82# | | vrf-200 | | | | vrf-200 | |83# | +----+----+ | | +----+----+ |84# | | | | | |85# | +-------+-------+ | | +-------+-------- |86# | | veth-t200 | | | | veth-t200 | |87# | | cafe::254/64 | | | | cafe::254/64 | |88# | +---------------+ rt-1 netns | | rt-2 netns +---------------- |89# | . | | . |90# +-----------------------------------+ +-----------------------------------+91# . .92# . .93# . .94# . .95# +-------------------+ +-------------------+96# | . | | . |97# | +-------------+ | | +-------------+ |98# | | veth0 | | | | veth0 | |99# | | cafe::3/64 | | | | cafe::4/64 | |100# | +-------------+ | | +-------------+ |101# | | | |102# | hs-t200-3 netns | | hs-t200-4 netns |103# | | | |104# +-------------------+ +-------------------+105#106#107# ~~~~~~~~~~~~~~~~~~~~~~~~~108# | Network configuration |109# ~~~~~~~~~~~~~~~~~~~~~~~~~110#111# rt-1: localsid table (table 90)112# +-------------------------------------------------+113# |SID |Action |114# +-------------------------------------------------+115# |fc00:21:100::6006|apply SRv6 End.DT6 vrftable 100|116# +-------------------------------------------------+117# |fc00:21:200::6006|apply SRv6 End.DT6 vrftable 200|118# +-------------------------------------------------+119#120# rt-1: VRF tenant 100 (table 100)121# +---------------------------------------------------+122# |host |Action |123# +---------------------------------------------------+124# |cafe::2 |apply seg6 encap segs fc00:12:100::6006|125# +---------------------------------------------------+126# |cafe::/64 |forward to dev veth_t100 |127# +---------------------------------------------------+128#129# rt-1: VRF tenant 200 (table 200)130# +---------------------------------------------------+131# |host |Action |132# +---------------------------------------------------+133# |cafe::4 |apply seg6 encap segs fc00:12:200::6006|134# +---------------------------------------------------+135# |cafe::/64 |forward to dev veth_t200 |136# +---------------------------------------------------+137#138#139# rt-2: localsid table (table 90)140# +-------------------------------------------------+141# |SID |Action |142# +-------------------------------------------------+143# |fc00:12:100::6006|apply SRv6 End.DT6 vrftable 100|144# +-------------------------------------------------+145# |fc00:12:200::6006|apply SRv6 End.DT6 vrftable 200|146# +-------------------------------------------------+147#148# rt-2: VRF tenant 100 (table 100)149# +---------------------------------------------------+150# |host |Action |151# +---------------------------------------------------+152# |cafe::1 |apply seg6 encap segs fc00:21:100::6006|153# +---------------------------------------------------+154# |cafe::/64 |forward to dev veth_t100 |155# +---------------------------------------------------+156#157# rt-2: VRF tenant 200 (table 200)158# +---------------------------------------------------+159# |host |Action |160# +---------------------------------------------------+161# |cafe::3 |apply seg6 encap segs fc00:21:200::6006|162# +---------------------------------------------------+163# |cafe::/64 |forward to dev veth_t200 |164# +---------------------------------------------------+165#166 167source lib.sh168 169readonly LOCALSID_TABLE_ID=90170readonly IPv6_RT_NETWORK=fd00171readonly IPv6_HS_NETWORK=cafe172readonly VPN_LOCATOR_SERVICE=fc00173PING_TIMEOUT_SEC=4174 175ret=0176 177PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}178 179log_test()180{181 local rc=$1182 local expected=$2183 local msg="$3"184 185 if [ ${rc} -eq ${expected} ]; then186 nsuccess=$((nsuccess+1))187 printf "\n TEST: %-60s [ OK ]\n" "${msg}"188 else189 ret=1190 nfail=$((nfail+1))191 printf "\n TEST: %-60s [FAIL]\n" "${msg}"192 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then193 echo194 echo "hit enter to continue, 'q' to quit"195 read a196 [ "$a" = "q" ] && exit 1197 fi198 fi199}200 201print_log_test_results()202{203 if [ "$TESTS" != "none" ]; then204 printf "\nTests passed: %3d\n" ${nsuccess}205 printf "Tests failed: %3d\n" ${nfail}206 fi207}208 209log_section()210{211 echo212 echo "################################################################################"213 echo "TEST SECTION: $*"214 echo "################################################################################"215}216 217cleanup()218{219 ip link del veth-rt-1 2>/dev/null || true220 ip link del veth-rt-2 2>/dev/null || true221 222 cleanup_all_ns223}224 225# Setup the basic networking for the routers226setup_rt_networking()227{228 local id=$1229 eval local nsname=\${rt_${id}}230 231 ip link set veth-rt-${id} netns ${nsname}232 ip -netns ${nsname} link set veth-rt-${id} name veth0233 234 ip netns exec ${nsname} sysctl -wq net.ipv6.conf.all.accept_dad=0235 ip netns exec ${nsname} sysctl -wq net.ipv6.conf.default.accept_dad=0236 237 ip -netns ${nsname} addr add ${IPv6_RT_NETWORK}::${id}/64 dev veth0 nodad238 ip -netns ${nsname} link set veth0 up239 ip -netns ${nsname} link set lo up240 241 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 # set the networking for the host254 ip netns exec ${hsname} sysctl -wq net.ipv6.conf.all.accept_dad=0255 ip netns exec ${hsname} sysctl -wq net.ipv6.conf.default.accept_dad=0256 257 ip -netns ${hsname} link add veth0 type veth peer name ${rtveth}258 ip -netns ${hsname} link set ${rtveth} netns ${rtname}259 ip -netns ${hsname} addr add ${IPv6_HS_NETWORK}::${hid}/64 dev veth0 nodad260 ip -netns ${hsname} link set veth0 up261 ip -netns ${hsname} link set lo up262 263 # configure the VRF for the tenant X on the router which is directly264 # connected to the source host.265 ip -netns ${rtname} link add vrf-${tid} type vrf table ${tid}266 ip -netns ${rtname} link set vrf-${tid} up267 268 ip netns exec ${rtname} sysctl -wq net.ipv6.conf.all.accept_dad=0269 ip netns exec ${rtname} sysctl -wq net.ipv6.conf.default.accept_dad=0270 271 # enslave the veth-tX interface to the vrf-X in the access router272 ip -netns ${rtname} link set ${rtveth} master vrf-${tid}273 ip -netns ${rtname} addr add ${IPv6_HS_NETWORK}::254/64 dev ${rtveth} nodad274 ip -netns ${rtname} link set ${rtveth} up275 276 ip netns exec ${rtname} sysctl -wq net.ipv6.conf.${rtveth}.proxy_ndp=1277 278 ip netns exec ${rtname} sh -c "echo 1 > /proc/sys/net/vrf/strict_mode"279}280 281setup_vpn_config()282{283 local hssrc=$1284 local rtsrc=$2285 local hsdst=$3286 local rtdst=$4287 local tid=$5288 289 eval local rtsrc_name=\${rt_${rtsrc}}290 eval local rtdst_name=\${rt_${rtdst}}291 local rtveth=veth-t${tid}292 local vpn_sid=${VPN_LOCATOR_SERVICE}:${hssrc}${hsdst}:${tid}::6006293 294 ip -netns ${rtsrc_name} -6 neigh add proxy ${IPv6_HS_NETWORK}::${hsdst} dev ${rtveth}295 296 # set the encap route for encapsulating packets which arrive from the297 # host hssrc and destined to the access router rtsrc.298 ip -netns ${rtsrc_name} -6 route add ${IPv6_HS_NETWORK}::${hsdst}/128 vrf vrf-${tid} \299 encap seg6 mode encap segs ${vpn_sid} dev veth0300 ip -netns ${rtsrc_name} -6 route add ${vpn_sid}/128 vrf vrf-${tid} \301 via fd00::${rtdst} dev veth0302 303 # set the decap route for decapsulating packets which arrive from304 # the rtdst router and destined to the hsdst host.305 ip -netns ${rtdst_name} -6 route add ${vpn_sid}/128 table ${LOCALSID_TABLE_ID} \306 encap seg6local action End.DT6 vrftable ${tid} dev vrf-${tid}307 308 # all sids for VPNs start with a common locator which is fc00::/16.309 # Routes for handling the SRv6 End.DT6 behavior instances are grouped310 # together in the 'localsid' table.311 #312 # NOTE: added only once313 if [ -z "$(ip -netns ${rtdst_name} -6 rule show | \314 grep "to ${VPN_LOCATOR_SERVICE}::/16 lookup ${LOCALSID_TABLE_ID}")" ]; then315 ip -netns ${rtdst_name} -6 rule add \316 to ${VPN_LOCATOR_SERVICE}::/16 \317 lookup ${LOCALSID_TABLE_ID} prio 999318 fi319}320 321setup()322{323 ip link add veth-rt-1 type veth peer name veth-rt-2324 # setup the networking for router rt-1 and router rt-2325 setup_ns rt_1 rt_2326 setup_rt_networking 1327 setup_rt_networking 2328 329 # setup two hosts for the tenant 100.330 # - host hs-1 is directly connected to the router rt-1;331 # - host hs-2 is directly connected to the router rt-2.332 setup_ns hs_t100_1 hs_t100_2333 setup_hs 1 1 100 #args: host router tenant334 setup_hs 2 2 100335 336 # setup two hosts for the tenant 200337 # - host hs-3 is directly connected to the router rt-1;338 # - host hs-4 is directly connected to the router rt-2.339 setup_ns hs_t200_3 hs_t200_4340 setup_hs 3 1 200341 setup_hs 4 2 200342 343 # setup the IPv6 L3 VPN which connects the host hs-t100-1 and host344 # hs-t100-2 within the same tenant 100.345 setup_vpn_config 1 1 2 2 100 #args: src_host src_router dst_host dst_router tenant346 setup_vpn_config 2 2 1 1 100347 348 # setup the IPv6 L3 VPN which connects the host hs-t200-3 and host349 # hs-t200-4 within the same tenant 200.350 setup_vpn_config 3 1 4 2 200351 setup_vpn_config 4 2 3 1 200352}353 354check_rt_connectivity()355{356 local rtsrc=$1357 local rtdst=$2358 eval local nsname=\${rt_${rtsrc}}359 360 ip netns exec ${nsname} ping -c 1 -W 1 ${IPv6_RT_NETWORK}::${rtdst} \361 >/dev/null 2>&1362}363 364check_and_log_rt_connectivity()365{366 local rtsrc=$1367 local rtdst=$2368 369 check_rt_connectivity ${rtsrc} ${rtdst}370 log_test $? 0 "Routers connectivity: rt-${rtsrc} -> rt-${rtdst}"371}372 373check_hs_connectivity()374{375 local hssrc=$1376 local hsdst=$2377 local tid=$3378 eval local nsname=\${hs_t${tid}_${hssrc}}379 380 ip netns exec ${nsname} ping -c 1 -W ${PING_TIMEOUT_SEC} \381 ${IPv6_HS_NETWORK}::${hsdst} >/dev/null 2>&1382}383 384check_and_log_hs_connectivity()385{386 local hssrc=$1387 local hsdst=$2388 local tid=$3389 390 check_hs_connectivity ${hssrc} ${hsdst} ${tid}391 log_test $? 0 "Hosts connectivity: hs-t${tid}-${hssrc} -> hs-t${tid}-${hsdst} (tenant ${tid})"392}393 394check_and_log_hs_isolation()395{396 local hssrc=$1397 local tidsrc=$2398 local hsdst=$3399 local tiddst=$4400 401 check_hs_connectivity ${hssrc} ${hsdst} ${tidsrc}402 # NOTE: ping should fail403 log_test $? 1 "Hosts isolation: hs-t${tidsrc}-${hssrc} -X-> hs-t${tiddst}-${hsdst}"404}405 406 407check_and_log_hs2gw_connectivity()408{409 local hssrc=$1410 local tid=$2411 412 check_hs_connectivity ${hssrc} 254 ${tid}413 log_test $? 0 "Hosts connectivity: hs-t${tid}-${hssrc} -> gw (tenant ${tid})"414}415 416router_tests()417{418 log_section "IPv6 routers connectivity test"419 420 check_and_log_rt_connectivity 1 2421 check_and_log_rt_connectivity 2 1422}423 424host2gateway_tests()425{426 log_section "IPv6 connectivity test among hosts and gateway"427 428 check_and_log_hs2gw_connectivity 1 100429 check_and_log_hs2gw_connectivity 2 100430 431 check_and_log_hs2gw_connectivity 3 200432 check_and_log_hs2gw_connectivity 4 200433}434 435host_vpn_tests()436{437 log_section "SRv6 VPN connectivity test among hosts in the same tenant"438 439 check_and_log_hs_connectivity 1 2 100440 check_and_log_hs_connectivity 2 1 100441 442 check_and_log_hs_connectivity 3 4 200443 check_and_log_hs_connectivity 4 3 200444}445 446host_vpn_isolation_tests()447{448 local i449 local j450 local k451 local tmp452 local l1="1 2"453 local l2="3 4"454 local t1=100455 local t2=200456 457 log_section "SRv6 VPN isolation test among hosts in different tentants"458 459 for k in 0 1; do460 for i in ${l1}; do461 for j in ${l2}; do462 check_and_log_hs_isolation ${i} ${t1} ${j} ${t2}463 done464 done465 466 # let us test the reverse path467 tmp="${l1}"; l1="${l2}"; l2="${tmp}"468 tmp=${t1}; t1=${t2}; t2=${tmp}469 done470}471 472if [ "$(id -u)" -ne 0 ];then473 echo "SKIP: Need root privileges"474 exit $ksft_skip475fi476 477if [ ! -x "$(command -v ip)" ]; then478 echo "SKIP: Could not run test without ip tool"479 exit $ksft_skip480fi481 482modprobe vrf &>/dev/null483if [ ! -e /proc/sys/net/vrf/strict_mode ]; then484 echo "SKIP: vrf sysctl does not exist"485 exit $ksft_skip486fi487 488cleanup &>/dev/null489 490setup491 492router_tests493host2gateway_tests494host_vpn_tests495host_vpn_isolation_tests496 497print_log_test_results498 499cleanup &>/dev/null500 501exit ${ret}502