pmtu.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Check that route PMTU values match expectations, and that initial device MTU
  5. # values are assigned correctly
  6. #
  7. # Tests currently implemented:
  8. #
  9. # - pmtu_ipv4
  10. # Set up two namespaces, A and B, with two paths between them over routers
  11. # R1 and R2 (also implemented with namespaces), with different MTUs:
  12. #
  13. # segment a_r1 segment b_r1 a_r1: 2000
  14. # .--------------R1--------------. a_r2: 1500
  15. # A B a_r3: 2000
  16. # '--------------R2--------------' a_r4: 1400
  17. # segment a_r2 segment b_r2
  18. #
  19. # Check that PMTU exceptions with the correct PMTU are created. Then
  20. # decrease and increase the MTU of the local link for one of the paths,
  21. # A to R1, checking that route exception PMTU changes accordingly over
  22. # this path. Also check that locked exceptions are created when an ICMP
  23. # message advertising a PMTU smaller than net.ipv4.route.min_pmtu is
  24. # received
  25. #
  26. # - pmtu_ipv6
  27. # Same as pmtu_ipv4, except for locked PMTU tests, using IPv6
  28. #
  29. # - pmtu_vti4_exception
  30. # Set up vti tunnel on top of veth, with xfrm states and policies, in two
  31. # namespaces with matching endpoints. Check that route exception is not
  32. # created if link layer MTU is not exceeded, then exceed it and check that
  33. # exception is created with the expected PMTU. The approach described
  34. # below for IPv6 doesn't apply here, because, on IPv4, administrative MTU
  35. # changes alone won't affect PMTU
  36. #
  37. # - pmtu_vti6_exception
  38. # Set up vti6 tunnel on top of veth, with xfrm states and policies, in two
  39. # namespaces with matching endpoints. Check that route exception is
  40. # created by exceeding link layer MTU with ping to other endpoint. Then
  41. # decrease and increase MTU of tunnel, checking that route exception PMTU
  42. # changes accordingly
  43. #
  44. # - pmtu_vti4_default_mtu
  45. # Set up vti4 tunnel on top of veth, in two namespaces with matching
  46. # endpoints. Check that MTU assigned to vti interface is the MTU of the
  47. # lower layer (veth) minus additional lower layer headers (zero, for veth)
  48. # minus IPv4 header length
  49. #
  50. # - pmtu_vti6_default_mtu
  51. # Same as above, for IPv6
  52. #
  53. # - pmtu_vti4_link_add_mtu
  54. # Set up vti4 interface passing MTU value at link creation, check MTU is
  55. # configured, and that link is not created with invalid MTU values
  56. #
  57. # - pmtu_vti6_link_add_mtu
  58. # Same as above, for IPv6
  59. #
  60. # - pmtu_vti6_link_change_mtu
  61. # Set up two dummy interfaces with different MTUs, create a vti6 tunnel
  62. # and check that configured MTU is used on link creation and changes, and
  63. # that MTU is properly calculated instead when MTU is not configured from
  64. # userspace
  65. # Kselftest framework requirement - SKIP code is 4.
  66. ksft_skip=4
  67. # Some systems don't have a ping6 binary anymore
  68. which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
  69. tests="
  70. pmtu_ipv4_exception ipv4: PMTU exceptions
  71. pmtu_ipv6_exception ipv6: PMTU exceptions
  72. pmtu_vti6_exception vti6: PMTU exceptions
  73. pmtu_vti4_exception vti4: PMTU exceptions
  74. pmtu_vti4_default_mtu vti4: default MTU assignment
  75. pmtu_vti6_default_mtu vti6: default MTU assignment
  76. pmtu_vti4_link_add_mtu vti4: MTU setting on link creation
  77. pmtu_vti6_link_add_mtu vti6: MTU setting on link creation
  78. pmtu_vti6_link_change_mtu vti6: MTU changes on link changes"
  79. NS_A="ns-$(mktemp -u XXXXXX)"
  80. NS_B="ns-$(mktemp -u XXXXXX)"
  81. NS_R1="ns-$(mktemp -u XXXXXX)"
  82. NS_R2="ns-$(mktemp -u XXXXXX)"
  83. ns_a="ip netns exec ${NS_A}"
  84. ns_b="ip netns exec ${NS_B}"
  85. ns_r1="ip netns exec ${NS_R1}"
  86. ns_r2="ip netns exec ${NS_R2}"
  87. # Addressing and routing for tests with routers: four network segments, with
  88. # index SEGMENT between 1 and 4, a common prefix (PREFIX4 or PREFIX6) and an
  89. # identifier ID, which is 1 for hosts (A and B), 2 for routers (R1 and R2).
  90. # Addresses are:
  91. # - IPv4: PREFIX4.SEGMENT.ID (/24)
  92. # - IPv6: PREFIX6:SEGMENT::ID (/64)
  93. prefix4="192.168"
  94. prefix6="fd00"
  95. a_r1=1
  96. a_r2=2
  97. b_r1=3
  98. b_r2=4
  99. # ns peer segment
  100. routing_addrs="
  101. A R1 ${a_r1}
  102. A R2 ${a_r2}
  103. B R1 ${b_r1}
  104. B R2 ${b_r2}
  105. "
  106. # Traffic from A to B goes through R1 by default, and through R2, if destined to
  107. # B's address on the b_r2 segment.
  108. # Traffic from B to A goes through R1.
  109. # ns destination gateway
  110. routes="
  111. A default ${prefix4}.${a_r1}.2
  112. A ${prefix4}.${b_r2}.1 ${prefix4}.${a_r2}.2
  113. B default ${prefix4}.${b_r1}.2
  114. A default ${prefix6}:${a_r1}::2
  115. A ${prefix6}:${b_r2}::1 ${prefix6}:${a_r2}::2
  116. B default ${prefix6}:${b_r1}::2
  117. "
  118. veth4_a_addr="192.168.1.1"
  119. veth4_b_addr="192.168.1.2"
  120. veth4_mask="24"
  121. veth6_a_addr="fd00:1::a"
  122. veth6_b_addr="fd00:1::b"
  123. veth6_mask="64"
  124. vti4_a_addr="192.168.2.1"
  125. vti4_b_addr="192.168.2.2"
  126. vti4_mask="24"
  127. vti6_a_addr="fd00:2::a"
  128. vti6_b_addr="fd00:2::b"
  129. vti6_mask="64"
  130. dummy6_0_addr="fc00:1000::0"
  131. dummy6_1_addr="fc00:1001::0"
  132. dummy6_mask="64"
  133. cleanup_done=1
  134. err_buf=
  135. tcpdump_pids=
  136. err() {
  137. err_buf="${err_buf}${1}
  138. "
  139. }
  140. err_flush() {
  141. echo -n "${err_buf}"
  142. err_buf=
  143. }
  144. # Find the auto-generated name for this namespace
  145. nsname() {
  146. eval echo \$NS_$1
  147. }
  148. setup_namespaces() {
  149. for n in ${NS_A} ${NS_B} ${NS_R1} ${NS_R2}; do
  150. ip netns add ${n} || return 1
  151. done
  152. }
  153. setup_veth() {
  154. ${ns_a} ip link add veth_a type veth peer name veth_b || return 1
  155. ${ns_a} ip link set veth_b netns ${NS_B}
  156. ${ns_a} ip addr add ${veth4_a_addr}/${veth4_mask} dev veth_a
  157. ${ns_b} ip addr add ${veth4_b_addr}/${veth4_mask} dev veth_b
  158. ${ns_a} ip addr add ${veth6_a_addr}/${veth6_mask} dev veth_a
  159. ${ns_b} ip addr add ${veth6_b_addr}/${veth6_mask} dev veth_b
  160. ${ns_a} ip link set veth_a up
  161. ${ns_b} ip link set veth_b up
  162. }
  163. setup_vti() {
  164. proto=${1}
  165. veth_a_addr="${2}"
  166. veth_b_addr="${3}"
  167. vti_a_addr="${4}"
  168. vti_b_addr="${5}"
  169. vti_mask=${6}
  170. [ ${proto} -eq 6 ] && vti_type="vti6" || vti_type="vti"
  171. ${ns_a} ip link add vti${proto}_a type ${vti_type} local ${veth_a_addr} remote ${veth_b_addr} key 10 || return 1
  172. ${ns_b} ip link add vti${proto}_b type ${vti_type} local ${veth_b_addr} remote ${veth_a_addr} key 10
  173. ${ns_a} ip addr add ${vti_a_addr}/${vti_mask} dev vti${proto}_a
  174. ${ns_b} ip addr add ${vti_b_addr}/${vti_mask} dev vti${proto}_b
  175. ${ns_a} ip link set vti${proto}_a up
  176. ${ns_b} ip link set vti${proto}_b up
  177. sleep 1
  178. }
  179. setup_vti4() {
  180. setup_vti 4 ${veth4_a_addr} ${veth4_b_addr} ${vti4_a_addr} ${vti4_b_addr} ${vti4_mask}
  181. }
  182. setup_vti6() {
  183. setup_vti 6 ${veth6_a_addr} ${veth6_b_addr} ${vti6_a_addr} ${vti6_b_addr} ${vti6_mask}
  184. }
  185. setup_xfrm() {
  186. proto=${1}
  187. veth_a_addr="${2}"
  188. veth_b_addr="${3}"
  189. ${ns_a} ip -${proto} xfrm state add src ${veth_a_addr} dst ${veth_b_addr} spi 0x1000 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel || return 1
  190. ${ns_a} ip -${proto} xfrm state add src ${veth_b_addr} dst ${veth_a_addr} spi 0x1001 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel
  191. ${ns_a} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
  192. ${ns_a} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
  193. ${ns_b} ip -${proto} xfrm state add src ${veth_a_addr} dst ${veth_b_addr} spi 0x1000 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel
  194. ${ns_b} ip -${proto} xfrm state add src ${veth_b_addr} dst ${veth_a_addr} spi 0x1001 proto esp aead "rfc4106(gcm(aes))" 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f 128 mode tunnel
  195. ${ns_b} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
  196. ${ns_b} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
  197. }
  198. setup_xfrm4() {
  199. setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
  200. }
  201. setup_xfrm6() {
  202. setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
  203. }
  204. setup_routing() {
  205. for i in ${NS_R1} ${NS_R2}; do
  206. ip netns exec ${i} sysctl -q net/ipv4/ip_forward=1
  207. ip netns exec ${i} sysctl -q net/ipv6/conf/all/forwarding=1
  208. done
  209. for i in ${routing_addrs}; do
  210. [ "${ns}" = "" ] && ns="${i}" && continue
  211. [ "${peer}" = "" ] && peer="${i}" && continue
  212. [ "${segment}" = "" ] && segment="${i}"
  213. ns_name="$(nsname ${ns})"
  214. peer_name="$(nsname ${peer})"
  215. if="veth_${ns}-${peer}"
  216. ifpeer="veth_${peer}-${ns}"
  217. # Create veth links
  218. ip link add ${if} up netns ${ns_name} type veth peer name ${ifpeer} netns ${peer_name} || return 1
  219. ip -n ${peer_name} link set dev ${ifpeer} up
  220. # Add addresses
  221. ip -n ${ns_name} addr add ${prefix4}.${segment}.1/24 dev ${if}
  222. ip -n ${ns_name} addr add ${prefix6}:${segment}::1/64 dev ${if}
  223. ip -n ${peer_name} addr add ${prefix4}.${segment}.2/24 dev ${ifpeer}
  224. ip -n ${peer_name} addr add ${prefix6}:${segment}::2/64 dev ${ifpeer}
  225. ns=""; peer=""; segment=""
  226. done
  227. for i in ${routes}; do
  228. [ "${ns}" = "" ] && ns="${i}" && continue
  229. [ "${addr}" = "" ] && addr="${i}" && continue
  230. [ "${gw}" = "" ] && gw="${i}"
  231. ns_name="$(nsname ${ns})"
  232. ip -n ${ns_name} route add ${addr} via ${gw}
  233. ns=""; addr=""; gw=""
  234. done
  235. }
  236. setup() {
  237. [ "$(id -u)" -ne 0 ] && echo " need to run as root" && return $ksft_skip
  238. cleanup_done=0
  239. for arg do
  240. eval setup_${arg} || { echo " ${arg} not supported"; return 1; }
  241. done
  242. }
  243. trace() {
  244. [ $tracing -eq 0 ] && return
  245. for arg do
  246. [ "${ns_cmd}" = "" ] && ns_cmd="${arg}" && continue
  247. ${ns_cmd} tcpdump -s 0 -i "${arg}" -w "${name}_${arg}.pcap" 2> /dev/null &
  248. tcpdump_pids="${tcpdump_pids} $!"
  249. ns_cmd=
  250. done
  251. sleep 1
  252. }
  253. cleanup() {
  254. for pid in ${tcpdump_pids}; do
  255. kill ${pid}
  256. done
  257. tcpdump_pids=
  258. [ ${cleanup_done} -eq 1 ] && return
  259. for n in ${NS_A} ${NS_B} ${NS_R1} ${NS_R2}; do
  260. ip netns del ${n} 2> /dev/null
  261. done
  262. cleanup_done=1
  263. }
  264. mtu() {
  265. ns_cmd="${1}"
  266. dev="${2}"
  267. mtu="${3}"
  268. ${ns_cmd} ip link set dev ${dev} mtu ${mtu}
  269. }
  270. mtu_parse() {
  271. input="${1}"
  272. next=0
  273. for i in ${input}; do
  274. [ ${next} -eq 1 -a "${i}" = "lock" ] && next=2 && continue
  275. [ ${next} -eq 1 ] && echo "${i}" && return
  276. [ ${next} -eq 2 ] && echo "lock ${i}" && return
  277. [ "${i}" = "mtu" ] && next=1
  278. done
  279. }
  280. link_get() {
  281. ns_cmd="${1}"
  282. name="${2}"
  283. ${ns_cmd} ip link show dev "${name}"
  284. }
  285. link_get_mtu() {
  286. ns_cmd="${1}"
  287. name="${2}"
  288. mtu_parse "$(link_get "${ns_cmd}" ${name})"
  289. }
  290. route_get_dst_exception() {
  291. ns_cmd="${1}"
  292. dst="${2}"
  293. ${ns_cmd} ip route get "${dst}"
  294. }
  295. route_get_dst_pmtu_from_exception() {
  296. ns_cmd="${1}"
  297. dst="${2}"
  298. mtu_parse "$(route_get_dst_exception "${ns_cmd}" ${dst})"
  299. }
  300. check_pmtu_value() {
  301. expected="${1}"
  302. value="${2}"
  303. event="${3}"
  304. [ "${expected}" = "any" ] && [ -n "${value}" ] && return 0
  305. [ "${value}" = "${expected}" ] && return 0
  306. [ -z "${value}" ] && err " PMTU exception wasn't created after ${event}" && return 1
  307. [ -z "${expected}" ] && err " PMTU exception shouldn't exist after ${event}" && return 1
  308. err " found PMTU exception with incorrect MTU ${value}, expected ${expected}, after ${event}"
  309. return 1
  310. }
  311. test_pmtu_ipvX() {
  312. family=${1}
  313. setup namespaces routing || return 2
  314. trace "${ns_a}" veth_A-R1 "${ns_r1}" veth_R1-A \
  315. "${ns_r1}" veth_R1-B "${ns_b}" veth_B-R1 \
  316. "${ns_a}" veth_A-R2 "${ns_r2}" veth_R2-A \
  317. "${ns_r2}" veth_R2-B "${ns_b}" veth_B-R2
  318. if [ ${family} -eq 4 ]; then
  319. ping=ping
  320. dst1="${prefix4}.${b_r1}.1"
  321. dst2="${prefix4}.${b_r2}.1"
  322. else
  323. ping=${ping6}
  324. dst1="${prefix6}:${b_r1}::1"
  325. dst2="${prefix6}:${b_r2}::1"
  326. fi
  327. # Set up initial MTU values
  328. mtu "${ns_a}" veth_A-R1 2000
  329. mtu "${ns_r1}" veth_R1-A 2000
  330. mtu "${ns_r1}" veth_R1-B 1400
  331. mtu "${ns_b}" veth_B-R1 1400
  332. mtu "${ns_a}" veth_A-R2 2000
  333. mtu "${ns_r2}" veth_R2-A 2000
  334. mtu "${ns_r2}" veth_R2-B 1500
  335. mtu "${ns_b}" veth_B-R2 1500
  336. # Create route exceptions
  337. ${ns_a} ${ping} -q -M want -i 0.1 -w 2 -s 1800 ${dst1} > /dev/null
  338. ${ns_a} ${ping} -q -M want -i 0.1 -w 2 -s 1800 ${dst2} > /dev/null
  339. # Check that exceptions have been created with the correct PMTU
  340. pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
  341. check_pmtu_value "1400" "${pmtu_1}" "exceeding MTU" || return 1
  342. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  343. check_pmtu_value "1500" "${pmtu_2}" "exceeding MTU" || return 1
  344. # Decrease local MTU below PMTU, check for PMTU decrease in route exception
  345. mtu "${ns_a}" veth_A-R1 1300
  346. mtu "${ns_r1}" veth_R1-A 1300
  347. pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
  348. check_pmtu_value "1300" "${pmtu_1}" "decreasing local MTU" || return 1
  349. # Second exception shouldn't be modified
  350. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  351. check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
  352. # Increase MTU, check for PMTU increase in route exception
  353. mtu "${ns_a}" veth_A-R1 1700
  354. mtu "${ns_r1}" veth_R1-A 1700
  355. pmtu_1="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst1})"
  356. check_pmtu_value "1700" "${pmtu_1}" "increasing local MTU" || return 1
  357. # Second exception shouldn't be modified
  358. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  359. check_pmtu_value "1500" "${pmtu_2}" "changing local MTU on a link not on this path" || return 1
  360. # Skip PMTU locking tests for IPv6
  361. [ $family -eq 6 ] && return 0
  362. # Decrease remote MTU on path via R2, get new exception
  363. mtu "${ns_r2}" veth_R2-B 400
  364. mtu "${ns_b}" veth_B-R2 400
  365. ${ns_a} ${ping} -q -M want -i 0.1 -w 2 -s 1400 ${dst2} > /dev/null
  366. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  367. check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
  368. # Decrease local MTU below PMTU
  369. mtu "${ns_a}" veth_A-R2 500
  370. mtu "${ns_r2}" veth_R2-A 500
  371. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  372. check_pmtu_value "500" "${pmtu_2}" "decreasing local MTU" || return 1
  373. # Increase local MTU
  374. mtu "${ns_a}" veth_A-R2 1500
  375. mtu "${ns_r2}" veth_R2-A 1500
  376. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  377. check_pmtu_value "1500" "${pmtu_2}" "increasing local MTU" || return 1
  378. # Get new exception
  379. ${ns_a} ${ping} -q -M want -i 0.1 -w 2 -s 1400 ${dst2} > /dev/null
  380. pmtu_2="$(route_get_dst_pmtu_from_exception "${ns_a}" ${dst2})"
  381. check_pmtu_value "lock 552" "${pmtu_2}" "exceeding MTU, with MTU < min_pmtu" || return 1
  382. }
  383. test_pmtu_ipv4_exception() {
  384. test_pmtu_ipvX 4
  385. }
  386. test_pmtu_ipv6_exception() {
  387. test_pmtu_ipvX 6
  388. }
  389. test_pmtu_vti4_exception() {
  390. setup namespaces veth vti4 xfrm4 || return 2
  391. trace "${ns_a}" veth_a "${ns_b}" veth_b \
  392. "${ns_a}" vti4_a "${ns_b}" vti4_b
  393. veth_mtu=1500
  394. vti_mtu=$((veth_mtu - 20))
  395. # SPI SN IV ICV pad length next header
  396. esp_payload_rfc4106=$((vti_mtu - 4 - 4 - 8 - 16 - 1 - 1))
  397. ping_payload=$((esp_payload_rfc4106 - 28))
  398. mtu "${ns_a}" veth_a ${veth_mtu}
  399. mtu "${ns_b}" veth_b ${veth_mtu}
  400. mtu "${ns_a}" vti4_a ${vti_mtu}
  401. mtu "${ns_b}" vti4_b ${vti_mtu}
  402. # Send DF packet without exceeding link layer MTU, check that no
  403. # exception is created
  404. ${ns_a} ping -q -M want -i 0.1 -w 2 -s ${ping_payload} ${vti4_b_addr} > /dev/null
  405. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti4_b_addr})"
  406. check_pmtu_value "" "${pmtu}" "sending packet smaller than PMTU (IP payload length ${esp_payload_rfc4106})" || return 1
  407. # Now exceed link layer MTU by one byte, check that exception is created
  408. # with the right PMTU value
  409. ${ns_a} ping -q -M want -i 0.1 -w 2 -s $((ping_payload + 1)) ${vti4_b_addr} > /dev/null
  410. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti4_b_addr})"
  411. check_pmtu_value "${esp_payload_rfc4106}" "${pmtu}" "exceeding PMTU (IP payload length $((esp_payload_rfc4106 + 1)))"
  412. }
  413. test_pmtu_vti6_exception() {
  414. setup namespaces veth vti6 xfrm6 || return 2
  415. trace "${ns_a}" veth_a "${ns_b}" veth_b \
  416. "${ns_a}" vti6_a "${ns_b}" vti6_b
  417. fail=0
  418. # Create route exception by exceeding link layer MTU
  419. mtu "${ns_a}" veth_a 4000
  420. mtu "${ns_b}" veth_b 4000
  421. mtu "${ns_a}" vti6_a 5000
  422. mtu "${ns_b}" vti6_b 5000
  423. ${ns_a} ${ping6} -q -i 0.1 -w 2 -s 60000 ${vti6_b_addr} > /dev/null
  424. # Check that exception was created
  425. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})"
  426. check_pmtu_value any "${pmtu}" "creating tunnel exceeding link layer MTU" || return 1
  427. # Decrease tunnel MTU, check for PMTU decrease in route exception
  428. mtu "${ns_a}" vti6_a 3000
  429. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})"
  430. check_pmtu_value "3000" "${pmtu}" "decreasing tunnel MTU" || fail=1
  431. # Increase tunnel MTU, check for PMTU increase in route exception
  432. mtu "${ns_a}" vti6_a 9000
  433. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})"
  434. check_pmtu_value "9000" "${pmtu}" "increasing tunnel MTU" || fail=1
  435. return ${fail}
  436. }
  437. test_pmtu_vti4_default_mtu() {
  438. setup namespaces veth vti4 || return 2
  439. # Check that MTU of vti device is MTU of veth minus IPv4 header length
  440. veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
  441. vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
  442. if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
  443. err " vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
  444. return 1
  445. fi
  446. }
  447. test_pmtu_vti6_default_mtu() {
  448. setup namespaces veth vti6 || return 2
  449. # Check that MTU of vti device is MTU of veth minus IPv6 header length
  450. veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
  451. vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  452. if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
  453. err " vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
  454. return 1
  455. fi
  456. }
  457. test_pmtu_vti4_link_add_mtu() {
  458. setup namespaces || return 2
  459. ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
  460. [ $? -ne 0 ] && err " vti not supported" && return 2
  461. ${ns_a} ip link del vti4_a
  462. fail=0
  463. min=68
  464. max=$((65535 - 20))
  465. # Check invalid values first
  466. for v in $((min - 1)) $((max + 1)); do
  467. ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10 2>/dev/null
  468. # This can fail, or MTU can be adjusted to a proper value
  469. [ $? -ne 0 ] && continue
  470. mtu="$(link_get_mtu "${ns_a}" vti4_a)"
  471. if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
  472. err " vti tunnel created with invalid MTU ${mtu}"
  473. fail=1
  474. fi
  475. ${ns_a} ip link del vti4_a
  476. done
  477. # Now check valid values
  478. for v in ${min} 1300 ${max}; do
  479. ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
  480. mtu="$(link_get_mtu "${ns_a}" vti4_a)"
  481. ${ns_a} ip link del vti4_a
  482. if [ "${mtu}" != "${v}" ]; then
  483. err " vti MTU ${mtu} doesn't match configured value ${v}"
  484. fail=1
  485. fi
  486. done
  487. return ${fail}
  488. }
  489. test_pmtu_vti6_link_add_mtu() {
  490. setup namespaces || return 2
  491. ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
  492. [ $? -ne 0 ] && err " vti6 not supported" && return 2
  493. ${ns_a} ip link del vti6_a
  494. fail=0
  495. min=68 # vti6 can carry IPv4 packets too
  496. max=$((65535 - 40))
  497. # Check invalid values first
  498. for v in $((min - 1)) $((max + 1)); do
  499. ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10 2>/dev/null
  500. # This can fail, or MTU can be adjusted to a proper value
  501. [ $? -ne 0 ] && continue
  502. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  503. if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
  504. err " vti6 tunnel created with invalid MTU ${v}"
  505. fail=1
  506. fi
  507. ${ns_a} ip link del vti6_a
  508. done
  509. # Now check valid values
  510. for v in 68 1280 1300 $((65535 - 40)); do
  511. ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
  512. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  513. ${ns_a} ip link del vti6_a
  514. if [ "${mtu}" != "${v}" ]; then
  515. err " vti6 MTU ${mtu} doesn't match configured value ${v}"
  516. fail=1
  517. fi
  518. done
  519. return ${fail}
  520. }
  521. test_pmtu_vti6_link_change_mtu() {
  522. setup namespaces || return 2
  523. ${ns_a} ip link add dummy0 mtu 1500 type dummy
  524. [ $? -ne 0 ] && err " dummy not supported" && return 2
  525. ${ns_a} ip link add dummy1 mtu 3000 type dummy
  526. ${ns_a} ip link set dummy0 up
  527. ${ns_a} ip link set dummy1 up
  528. ${ns_a} ip addr add ${dummy6_0_addr}/${dummy6_mask} dev dummy0
  529. ${ns_a} ip addr add ${dummy6_1_addr}/${dummy6_mask} dev dummy1
  530. fail=0
  531. # Create vti6 interface bound to device, passing MTU, check it
  532. ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_addr} local ${dummy6_0_addr}
  533. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  534. if [ ${mtu} -ne 1300 ]; then
  535. err " vti6 MTU ${mtu} doesn't match configured value 1300"
  536. fail=1
  537. fi
  538. # Move to another device with different MTU, without passing MTU, check
  539. # MTU is adjusted
  540. ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_addr} local ${dummy6_1_addr}
  541. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  542. if [ ${mtu} -ne $((3000 - 40)) ]; then
  543. err " vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
  544. fail=1
  545. fi
  546. # Move it back, passing MTU, check MTU is not overridden
  547. ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_addr} local ${dummy6_0_addr}
  548. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  549. if [ ${mtu} -ne 1280 ]; then
  550. err " vti6 MTU ${mtu} doesn't match configured value 1280"
  551. fail=1
  552. fi
  553. return ${fail}
  554. }
  555. usage() {
  556. echo
  557. echo "$0 [OPTIONS] [TEST]..."
  558. echo "If no TEST argument is given, all tests will be run."
  559. echo
  560. echo "Options"
  561. echo " --trace: capture traffic to TEST_INTERFACE.pcap"
  562. echo
  563. echo "Available tests${tests}"
  564. exit 1
  565. }
  566. exitcode=0
  567. desc=0
  568. IFS="
  569. "
  570. tracing=0
  571. for arg do
  572. if [ "${arg}" != "${arg#--*}" ]; then
  573. opt="${arg#--}"
  574. if [ "${opt}" = "trace" ]; then
  575. if which tcpdump > /dev/null 2>&1; then
  576. tracing=1
  577. else
  578. echo "=== tcpdump not available, tracing disabled"
  579. fi
  580. else
  581. usage
  582. fi
  583. else
  584. # Check first that all requested tests are available before
  585. # running any
  586. command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
  587. fi
  588. done
  589. trap cleanup EXIT
  590. for t in ${tests}; do
  591. [ $desc -eq 0 ] && name="${t}" && desc=1 && continue || desc=0
  592. run_this=1
  593. for arg do
  594. [ "${arg}" != "${arg#--*}" ] && continue
  595. [ "${arg}" = "${name}" ] && run_this=1 && break
  596. run_this=0
  597. done
  598. [ $run_this -eq 0 ] && continue
  599. (
  600. unset IFS
  601. eval test_${name}
  602. ret=$?
  603. cleanup
  604. if [ $ret -eq 0 ]; then
  605. printf "TEST: %-60s [ OK ]\n" "${t}"
  606. elif [ $ret -eq 1 ]; then
  607. printf "TEST: %-60s [FAIL]\n" "${t}"
  608. err_flush
  609. exit 1
  610. elif [ $ret -eq 2 ]; then
  611. printf "TEST: %-60s [SKIP]\n" "${t}"
  612. err_flush
  613. fi
  614. )
  615. [ $? -ne 0 ] && exitcode=1
  616. done
  617. exit ${exitcode}