test_vxlan_under_vrf.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # This test is for checking VXLAN underlay in a non-default VRF.
  4. #
  5. # It simulates two hypervisors running a VM each using four network namespaces:
  6. # two for the HVs, two for the VMs.
  7. # A small VXLAN tunnel is made between the two hypervisors to have the two vms
  8. # in the same virtual L2:
  9. #
  10. # +-------------------+ +-------------------+
  11. # | | | |
  12. # | vm-1 netns | | vm-2 netns |
  13. # | | | |
  14. # | +-------------+ | | +-------------+ |
  15. # | | veth-hv | | | | veth-hv | |
  16. # | | 10.0.0.1/24 | | | | 10.0.0.2/24 | |
  17. # | +-------------+ | | +-------------+ |
  18. # | . | | . |
  19. # +-------------------+ +-------------------+
  20. # . .
  21. # . .
  22. # . .
  23. # +-----------------------------------+ +------------------------------------+
  24. # | . | | . |
  25. # | +----------+ | | +----------+ |
  26. # | | veth-tap | | | | veth-tap | |
  27. # | +----+-----+ | | +----+-----+ |
  28. # | | | | | |
  29. # | +--+--+ +--------------+ | | +--------------+ +--+--+ |
  30. # | | br0 | | vrf-underlay | | | | vrf-underlay | | br0 | |
  31. # | +--+--+ +-------+------+ | | +------+-------+ +--+--+ |
  32. # | | | | | | | |
  33. # | +---+----+ +-------+-------+ | | +-------+-------+ +---+----+ |
  34. # | | vxlan0 |....| veth0 |.|...|.| veth0 |....| vxlan0 | |
  35. # | +--------+ | 172.16.0.1/24 | | | | 172.16.0.2/24 | +--------+ |
  36. # | +---------------+ | | +---------------+ |
  37. # | | | |
  38. # | hv-1 netns | | hv-2 netns |
  39. # | | | |
  40. # +-----------------------------------+ +------------------------------------+
  41. #
  42. # This tests both the connectivity between vm-1 and vm-2, and that the underlay
  43. # can be moved in and out of the vrf by unsetting and setting veth0's master.
  44. source lib.sh
  45. set -e
  46. cleanup() {
  47. ip link del veth-hv-1 2>/dev/null || true
  48. ip link del veth-tap 2>/dev/null || true
  49. cleanup_ns $hv_1 $hv_2 $vm_1 $vm_2
  50. }
  51. # Clean start
  52. cleanup &> /dev/null
  53. [[ $1 == "clean" ]] && exit 0
  54. trap cleanup EXIT
  55. setup_ns hv_1 hv_2 vm_1 vm_2
  56. hv[1]=$hv_1
  57. hv[2]=$hv_2
  58. vm[1]=$vm_1
  59. vm[2]=$vm_2
  60. # Setup "Hypervisors" simulated with netns
  61. ip link add veth-hv-1 type veth peer name veth-hv-2
  62. setup-hv-networking() {
  63. id=$1
  64. ip link set veth-hv-$id netns ${hv[$id]}
  65. ip -netns ${hv[$id]} link set veth-hv-$id name veth0
  66. ip -netns ${hv[$id]} link add vrf-underlay type vrf table 1
  67. ip -netns ${hv[$id]} link set vrf-underlay up
  68. ip -netns ${hv[$id]} addr add 172.16.0.$id/24 dev veth0
  69. ip -netns ${hv[$id]} link set veth0 up
  70. ip -netns ${hv[$id]} link add br0 type bridge
  71. ip -netns ${hv[$id]} link set br0 up
  72. ip -netns ${hv[$id]} link add vxlan0 type vxlan id 10 local 172.16.0.$id dev veth0 dstport 4789
  73. ip -netns ${hv[$id]} link set vxlan0 master br0
  74. ip -netns ${hv[$id]} link set vxlan0 up
  75. }
  76. setup-hv-networking 1
  77. setup-hv-networking 2
  78. # Check connectivity between HVs by pinging hv-2 from hv-1
  79. echo -n "Checking HV connectivity "
  80. ip netns exec $hv_1 ping -c 1 -W 1 172.16.0.2 &> /dev/null || (echo "[FAIL]"; false)
  81. echo "[ OK ]"
  82. # Setups a "VM" simulated by a netns an a veth pair
  83. setup-vm() {
  84. id=$1
  85. ip link add veth-tap type veth peer name veth-hv
  86. ip link set veth-tap netns ${hv[$id]}
  87. ip -netns ${hv[$id]} link set veth-tap master br0
  88. ip -netns ${hv[$id]} link set veth-tap up
  89. ip link set veth-hv address 02:1d:8d:dd:0c:6$id
  90. ip link set veth-hv netns ${vm[$id]}
  91. ip -netns ${vm[$id]} addr add 10.0.0.$id/24 dev veth-hv
  92. ip -netns ${vm[$id]} link set veth-hv up
  93. }
  94. setup-vm 1
  95. setup-vm 2
  96. # Setup VTEP routes to make ARP work
  97. bridge -netns $hv_1 fdb add 00:00:00:00:00:00 dev vxlan0 dst 172.16.0.2 self permanent
  98. bridge -netns $hv_2 fdb add 00:00:00:00:00:00 dev vxlan0 dst 172.16.0.1 self permanent
  99. echo -n "Check VM connectivity through VXLAN (underlay in the default VRF) "
  100. ip netns exec $vm_1 ping -c 1 -W 1 10.0.0.2 &> /dev/null || (echo "[FAIL]"; false)
  101. echo "[ OK ]"
  102. # Move the underlay to a non-default VRF
  103. ip -netns $hv_1 link set veth0 vrf vrf-underlay
  104. ip -netns $hv_1 link set vxlan0 down
  105. ip -netns $hv_1 link set vxlan0 up
  106. ip -netns $hv_2 link set veth0 vrf vrf-underlay
  107. ip -netns $hv_2 link set vxlan0 down
  108. ip -netns $hv_2 link set vxlan0 up
  109. echo -n "Check VM connectivity through VXLAN (underlay in a VRF) "
  110. ip netns exec $vm_1 ping -c 1 -W 1 10.0.0.2 &> /dev/null || (echo "[FAIL]"; false)
  111. echo "[ OK ]"