ipip_lib.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Handles creation and destruction of IP-in-IP or GRE tunnels over the given
  4. # topology. Supports both flat and hierarchical models.
  5. #
  6. # Flat Model:
  7. # Overlay and underlay share the same VRF.
  8. # SW1 uses default VRF so tunnel has no bound dev.
  9. # SW2 uses non-default VRF tunnel has a bound dev.
  10. # +-------------------------+
  11. # | H1 |
  12. # | $h1 + |
  13. # | 192.0.2.1/28 | |
  14. # +-------------------|-----+
  15. # |
  16. # +-------------------|-----+
  17. # | SW1 | |
  18. # | $ol1 + |
  19. # | 192.0.2.2/28 |
  20. # | |
  21. # | + g1a (gre) |
  22. # | loc=192.0.2.65 |
  23. # | rem=192.0.2.66 --. |
  24. # | tos=inherit | |
  25. # | .------------------' |
  26. # | | |
  27. # | v |
  28. # | + $ul1.111 (vlan) |
  29. # | | 192.0.2.129/28 |
  30. # | \ |
  31. # | \_______ |
  32. # | | |
  33. # |VRF default + $ul1 |
  34. # +------------|------------+
  35. # |
  36. # +------------|------------+
  37. # | SW2 + $ul2 |
  38. # | _______| |
  39. # | / |
  40. # | / |
  41. # | + $ul2.111 (vlan) |
  42. # | ^ 192.0.2.130/28 |
  43. # | | |
  44. # | | |
  45. # | '------------------. |
  46. # | + g2a (gre) | |
  47. # | loc=192.0.2.66 | |
  48. # | rem=192.0.2.65 --' |
  49. # | tos=inherit |
  50. # | |
  51. # | $ol2 + |
  52. # | 192.0.2.17/28 | |
  53. # | VRF v$ol2 | |
  54. # +-------------------|-----+
  55. # |
  56. # +-------------------|-----+
  57. # | H2 | |
  58. # | $h2 + |
  59. # | 192.0.2.18/28 |
  60. # +-------------------------+
  61. #
  62. # Hierarchical model:
  63. # The tunnel is bound to a device in a different VRF
  64. #
  65. # +---------------------------+
  66. # | H1 |
  67. # | $h1 + |
  68. # | 192.0.2.1/28 | |
  69. # +-------------------|-------+
  70. # |
  71. # +-------------------|-------+
  72. # | SW1 | |
  73. # | +-----------------|-----+ |
  74. # | | $ol1 + | |
  75. # | | 192.0.2.2/28 | |
  76. # | | | |
  77. # | | + g1a (gre) | |
  78. # | | rem=192.0.2.66 | |
  79. # | | tos=inherit | |
  80. # | | loc=192.0.2.65 | |
  81. # | | ^ | |
  82. # | | VRF v$ol1 | | |
  83. # | +-----------|-----------+ |
  84. # | | |
  85. # | +-----------|-----------+ |
  86. # | | VRF v$ul1 | | |
  87. # | | | | |
  88. # | | | | |
  89. # | | v | |
  90. # | | dummy1 + | |
  91. # | | 192.0.2.65 | |
  92. # | | .-------' | |
  93. # | | | | |
  94. # | | v | |
  95. # | | + $ul1.111 (vlan) | |
  96. # | | | 192.0.2.129/28 | |
  97. # | | \ | |
  98. # | | \_____ | |
  99. # | | | | |
  100. # | | + $ul1 | |
  101. # | +----------|------------+ |
  102. # +------------|--------------+
  103. # |
  104. # +------------|--------------+
  105. # | SW2 | |
  106. # | +----------|------------+ |
  107. # | | + $ul2 | |
  108. # | | _____| | |
  109. # | | / | |
  110. # | | / | |
  111. # | | | $ul2.111 (vlan) | |
  112. # | | + 192.0.2.130/28 | |
  113. # | | ^ | |
  114. # | | | | |
  115. # | | '-------. | |
  116. # | | dummy2 + | |
  117. # | | 192.0.2.66 | |
  118. # | | ^ | |
  119. # | | | | |
  120. # | | | | |
  121. # | | VRF v$ul2 | | |
  122. # | +-----------|-----------+ |
  123. # | | |
  124. # | +-----------|-----------+ |
  125. # | | VRF v$ol2 | | |
  126. # | | | | |
  127. # | | v | |
  128. # | | g2a (gre)+ | |
  129. # | | loc=192.0.2.66 | |
  130. # | | rem=192.0.2.65 | |
  131. # | | tos=inherit | |
  132. # | | | |
  133. # | | $ol2 + | |
  134. # | | 192.0.2.17/28 | | |
  135. # | +-----------------|-----+ |
  136. # +-------------------|-------+
  137. # |
  138. # +-------------------|-------+
  139. # | H2 | |
  140. # | $h2 + |
  141. # | 192.0.2.18/28 |
  142. # +---------------------------+
  143. h1_create()
  144. {
  145. simple_if_init $h1 192.0.2.1/28 2001:db8:1::1/64
  146. ip route add vrf v$h1 192.0.2.16/28 via 192.0.2.2
  147. }
  148. h1_destroy()
  149. {
  150. ip route del vrf v$h1 192.0.2.16/28 via 192.0.2.2
  151. simple_if_fini $h1 192.0.2.1/28
  152. }
  153. h2_create()
  154. {
  155. simple_if_init $h2 192.0.2.18/28
  156. ip route add vrf v$h2 192.0.2.0/28 via 192.0.2.17
  157. }
  158. h2_destroy()
  159. {
  160. ip route del vrf v$h2 192.0.2.0/28 via 192.0.2.17
  161. simple_if_fini $h2 192.0.2.18/28
  162. }
  163. sw1_flat_create()
  164. {
  165. local type=$1; shift
  166. local ol1=$1; shift
  167. local ul1=$1; shift
  168. ip link set dev $ol1 up
  169. __addr_add_del $ol1 add "192.0.2.2/28"
  170. ip link set dev $ul1 up
  171. vlan_create $ul1 111 "" 192.0.2.129/28
  172. tunnel_create g1a $type 192.0.2.65 192.0.2.66 tos inherit "$@"
  173. ip link set dev g1a up
  174. __addr_add_del g1a add "192.0.2.65/32"
  175. ip route add 192.0.2.66/32 via 192.0.2.130
  176. ip route add 192.0.2.16/28 nexthop dev g1a
  177. }
  178. sw1_flat_destroy()
  179. {
  180. local ol1=$1; shift
  181. local ul1=$1; shift
  182. ip route del 192.0.2.16/28
  183. ip route del 192.0.2.66/32 via 192.0.2.130
  184. __simple_if_fini g1a 192.0.2.65/32
  185. tunnel_destroy g1a
  186. vlan_destroy $ul1 111
  187. __simple_if_fini $ul1
  188. __simple_if_fini $ol1 192.0.2.2/28
  189. }
  190. sw2_flat_create()
  191. {
  192. local type=$1; shift
  193. local ol2=$1; shift
  194. local ul2=$1; shift
  195. simple_if_init $ol2 192.0.2.17/28
  196. __simple_if_init $ul2 v$ol2
  197. vlan_create $ul2 111 v$ol2 192.0.2.130/28
  198. tunnel_create g2a $type 192.0.2.66 192.0.2.65 tos inherit dev v$ol2 \
  199. "$@"
  200. __simple_if_init g2a v$ol2 192.0.2.66/32
  201. ip route add vrf v$ol2 192.0.2.65/32 via 192.0.2.129
  202. ip route add vrf v$ol2 192.0.2.0/28 nexthop dev g2a
  203. }
  204. sw2_flat_destroy()
  205. {
  206. local ol2=$1; shift
  207. local ul2=$1; shift
  208. ip route del vrf v$ol2 192.0.2.0/28
  209. ip route del vrf v$ol2 192.0.2.65/32 via 192.0.2.129
  210. __simple_if_fini g2a 192.0.2.66/32
  211. tunnel_destroy g2a
  212. vlan_destroy $ul2 111
  213. __simple_if_fini $ul2
  214. simple_if_fini $ol2 192.0.2.17/28
  215. }
  216. sw1_hierarchical_create()
  217. {
  218. local type=$1; shift
  219. local ol1=$1; shift
  220. local ul1=$1; shift
  221. simple_if_init $ol1 192.0.2.2/28
  222. simple_if_init $ul1
  223. ip link add name dummy1 type dummy
  224. __simple_if_init dummy1 v$ul1 192.0.2.65/32
  225. vlan_create $ul1 111 v$ul1 192.0.2.129/28
  226. tunnel_create g1a $type 192.0.2.65 192.0.2.66 tos inherit dev dummy1 \
  227. "$@"
  228. ip link set dev g1a master v$ol1
  229. ip route add vrf v$ul1 192.0.2.66/32 via 192.0.2.130
  230. ip route add vrf v$ol1 192.0.2.16/28 nexthop dev g1a
  231. }
  232. sw1_hierarchical_destroy()
  233. {
  234. local ol1=$1; shift
  235. local ul1=$1; shift
  236. ip route del vrf v$ol1 192.0.2.16/28
  237. ip route del vrf v$ul1 192.0.2.66/32
  238. tunnel_destroy g1a
  239. vlan_destroy $ul1 111
  240. __simple_if_fini dummy1 192.0.2.65/32
  241. ip link del dev dummy1
  242. simple_if_fini $ul1
  243. simple_if_fini $ol1 192.0.2.2/28
  244. }
  245. sw2_hierarchical_create()
  246. {
  247. local type=$1; shift
  248. local ol2=$1; shift
  249. local ul2=$1; shift
  250. simple_if_init $ol2 192.0.2.17/28
  251. simple_if_init $ul2
  252. ip link add name dummy2 type dummy
  253. __simple_if_init dummy2 v$ul2 192.0.2.66/32
  254. vlan_create $ul2 111 v$ul2 192.0.2.130/28
  255. tunnel_create g2a $type 192.0.2.66 192.0.2.65 tos inherit dev dummy2 \
  256. "$@"
  257. ip link set dev g2a master v$ol2
  258. ip route add vrf v$ul2 192.0.2.65/32 via 192.0.2.129
  259. ip route add vrf v$ol2 192.0.2.0/28 nexthop dev g2a
  260. }
  261. sw2_hierarchical_destroy()
  262. {
  263. local ol2=$1; shift
  264. local ul2=$1; shift
  265. ip route del vrf v$ol2 192.0.2.0/28
  266. ip route del vrf v$ul2 192.0.2.65/32
  267. tunnel_destroy g2a
  268. vlan_destroy $ul2 111
  269. __simple_if_fini dummy2 192.0.2.66/32
  270. ip link del dev dummy2
  271. simple_if_fini $ul2
  272. simple_if_fini $ol2 192.0.2.17/28
  273. }
  274. topo_mtu_change()
  275. {
  276. local mtu=$1
  277. ip link set mtu $mtu dev $h1
  278. ip link set mtu $mtu dev $ol1
  279. ip link set mtu $mtu dev g1a
  280. ip link set mtu $mtu dev $ul1
  281. ip link set mtu $mtu dev $ul1.111
  282. ip link set mtu $mtu dev $h2
  283. ip link set mtu $mtu dev $ol2
  284. ip link set mtu $mtu dev g2a
  285. ip link set mtu $mtu dev $ul2
  286. ip link set mtu $mtu dev $ul2.111
  287. }
  288. test_mtu_change()
  289. {
  290. local encap=$1; shift
  291. RET=0
  292. ping_do $h1 192.0.2.18 "-s 1800 -w 3"
  293. check_fail $? "ping $encap should not pass with size 1800"
  294. RET=0
  295. topo_mtu_change 2000
  296. ping_do $h1 192.0.2.18 "-s 1800 -w 3"
  297. check_err $?
  298. log_test "ping $encap packet size 1800 after MTU change"
  299. }