pmtu.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_vti4_exception
  10. # Set up vti tunnel on top of veth, with xfrm states and policies, in two
  11. # namespaces with matching endpoints. Check that route exception is not
  12. # created if link layer MTU is not exceeded, then exceed it and check that
  13. # exception is created with the expected PMTU. The approach described
  14. # below for IPv6 doesn't apply here, because, on IPv4, administrative MTU
  15. # changes alone won't affect PMTU
  16. #
  17. # - pmtu_vti6_exception
  18. # Set up vti6 tunnel on top of veth, with xfrm states and policies, in two
  19. # namespaces with matching endpoints. Check that route exception is
  20. # created by exceeding link layer MTU with ping to other endpoint. Then
  21. # decrease and increase MTU of tunnel, checking that route exception PMTU
  22. # changes accordingly
  23. #
  24. # - pmtu_vti4_default_mtu
  25. # Set up vti4 tunnel on top of veth, in two namespaces with matching
  26. # endpoints. Check that MTU assigned to vti interface is the MTU of the
  27. # lower layer (veth) minus additional lower layer headers (zero, for veth)
  28. # minus IPv4 header length
  29. #
  30. # - pmtu_vti6_default_mtu
  31. # Same as above, for IPv6
  32. #
  33. # - pmtu_vti4_link_add_mtu
  34. # Set up vti4 interface passing MTU value at link creation, check MTU is
  35. # configured, and that link is not created with invalid MTU values
  36. #
  37. # - pmtu_vti6_link_add_mtu
  38. # Same as above, for IPv6
  39. #
  40. # - pmtu_vti6_link_change_mtu
  41. # Set up two dummy interfaces with different MTUs, create a vti6 tunnel
  42. # and check that configured MTU is used on link creation and changes, and
  43. # that MTU is properly calculated instead when MTU is not configured from
  44. # userspace
  45. tests="
  46. pmtu_vti6_exception vti6: PMTU exceptions
  47. pmtu_vti4_exception vti4: PMTU exceptions
  48. pmtu_vti4_default_mtu vti4: default MTU assignment
  49. pmtu_vti6_default_mtu vti6: default MTU assignment
  50. pmtu_vti4_link_add_mtu vti4: MTU setting on link creation
  51. pmtu_vti6_link_add_mtu vti6: MTU setting on link creation
  52. pmtu_vti6_link_change_mtu vti6: MTU changes on link changes"
  53. NS_A="ns-$(mktemp -u XXXXXX)"
  54. NS_B="ns-$(mktemp -u XXXXXX)"
  55. ns_a="ip netns exec ${NS_A}"
  56. ns_b="ip netns exec ${NS_B}"
  57. veth4_a_addr="192.168.1.1"
  58. veth4_b_addr="192.168.1.2"
  59. veth4_mask="24"
  60. veth6_a_addr="fd00:1::a"
  61. veth6_b_addr="fd00:1::b"
  62. veth6_mask="64"
  63. vti4_a_addr="192.168.2.1"
  64. vti4_b_addr="192.168.2.2"
  65. vti4_mask="24"
  66. vti6_a_addr="fd00:2::a"
  67. vti6_b_addr="fd00:2::b"
  68. vti6_mask="64"
  69. dummy6_0_addr="fc00:1000::0"
  70. dummy6_1_addr="fc00:1001::0"
  71. dummy6_mask="64"
  72. cleanup_done=1
  73. err_buf=
  74. err() {
  75. err_buf="${err_buf}${1}
  76. "
  77. }
  78. err_flush() {
  79. echo -n "${err_buf}"
  80. err_buf=
  81. }
  82. setup_namespaces() {
  83. ip netns add ${NS_A} || return 1
  84. ip netns add ${NS_B}
  85. }
  86. setup_veth() {
  87. ${ns_a} ip link add veth_a type veth peer name veth_b || return 1
  88. ${ns_a} ip link set veth_b netns ${NS_B}
  89. ${ns_a} ip addr add ${veth4_a_addr}/${veth4_mask} dev veth_a
  90. ${ns_b} ip addr add ${veth4_b_addr}/${veth4_mask} dev veth_b
  91. ${ns_a} ip addr add ${veth6_a_addr}/${veth6_mask} dev veth_a
  92. ${ns_b} ip addr add ${veth6_b_addr}/${veth6_mask} dev veth_b
  93. ${ns_a} ip link set veth_a up
  94. ${ns_b} ip link set veth_b up
  95. }
  96. setup_vti() {
  97. proto=${1}
  98. veth_a_addr="${2}"
  99. veth_b_addr="${3}"
  100. vti_a_addr="${4}"
  101. vti_b_addr="${5}"
  102. vti_mask=${6}
  103. [ ${proto} -eq 6 ] && vti_type="vti6" || vti_type="vti"
  104. ${ns_a} ip link add vti${proto}_a type ${vti_type} local ${veth_a_addr} remote ${veth_b_addr} key 10 || return 1
  105. ${ns_b} ip link add vti${proto}_b type ${vti_type} local ${veth_b_addr} remote ${veth_a_addr} key 10
  106. ${ns_a} ip addr add ${vti_a_addr}/${vti_mask} dev vti${proto}_a
  107. ${ns_b} ip addr add ${vti_b_addr}/${vti_mask} dev vti${proto}_b
  108. ${ns_a} ip link set vti${proto}_a up
  109. ${ns_b} ip link set vti${proto}_b up
  110. sleep 1
  111. }
  112. setup_vti4() {
  113. setup_vti 4 ${veth4_a_addr} ${veth4_b_addr} ${vti4_a_addr} ${vti4_b_addr} ${vti4_mask}
  114. }
  115. setup_vti6() {
  116. setup_vti 6 ${veth6_a_addr} ${veth6_b_addr} ${vti6_a_addr} ${vti6_b_addr} ${vti6_mask}
  117. }
  118. setup_xfrm() {
  119. proto=${1}
  120. veth_a_addr="${2}"
  121. veth_b_addr="${3}"
  122. ${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
  123. ${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
  124. ${ns_a} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
  125. ${ns_a} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
  126. ${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
  127. ${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
  128. ${ns_b} ip -${proto} xfrm policy add dir out mark 10 tmpl src ${veth_b_addr} dst ${veth_a_addr} proto esp mode tunnel
  129. ${ns_b} ip -${proto} xfrm policy add dir in mark 10 tmpl src ${veth_a_addr} dst ${veth_b_addr} proto esp mode tunnel
  130. }
  131. setup_xfrm4() {
  132. setup_xfrm 4 ${veth4_a_addr} ${veth4_b_addr}
  133. }
  134. setup_xfrm6() {
  135. setup_xfrm 6 ${veth6_a_addr} ${veth6_b_addr}
  136. }
  137. setup() {
  138. [ "$(id -u)" -ne 0 ] && echo " need to run as root" && return 1
  139. cleanup_done=0
  140. for arg do
  141. eval setup_${arg} || { echo " ${arg} not supported"; return 1; }
  142. done
  143. }
  144. cleanup() {
  145. [ ${cleanup_done} -eq 1 ] && return
  146. ip netns del ${NS_A} 2 > /dev/null
  147. ip netns del ${NS_B} 2 > /dev/null
  148. cleanup_done=1
  149. }
  150. mtu() {
  151. ns_cmd="${1}"
  152. dev="${2}"
  153. mtu="${3}"
  154. ${ns_cmd} ip link set dev ${dev} mtu ${mtu}
  155. }
  156. mtu_parse() {
  157. input="${1}"
  158. next=0
  159. for i in ${input}; do
  160. [ ${next} -eq 1 ] && echo "${i}" && return
  161. [ "${i}" = "mtu" ] && next=1
  162. done
  163. }
  164. link_get() {
  165. ns_cmd="${1}"
  166. name="${2}"
  167. ${ns_cmd} ip link show dev "${name}"
  168. }
  169. link_get_mtu() {
  170. ns_cmd="${1}"
  171. name="${2}"
  172. mtu_parse "$(link_get "${ns_cmd}" ${name})"
  173. }
  174. route_get_dst_exception() {
  175. ns_cmd="${1}"
  176. dst="${2}"
  177. ${ns_cmd} ip route get "${dst}"
  178. }
  179. route_get_dst_pmtu_from_exception() {
  180. ns_cmd="${1}"
  181. dst="${2}"
  182. mtu_parse "$(route_get_dst_exception "${ns_cmd}" ${dst})"
  183. }
  184. test_pmtu_vti4_exception() {
  185. setup namespaces veth vti4 xfrm4 || return 2
  186. veth_mtu=1500
  187. vti_mtu=$((veth_mtu - 20))
  188. # SPI SN IV ICV pad length next header
  189. esp_payload_rfc4106=$((vti_mtu - 4 - 4 - 8 - 16 - 1 - 1))
  190. ping_payload=$((esp_payload_rfc4106 - 28))
  191. mtu "${ns_a}" veth_a ${veth_mtu}
  192. mtu "${ns_b}" veth_b ${veth_mtu}
  193. mtu "${ns_a}" vti4_a ${vti_mtu}
  194. mtu "${ns_b}" vti4_b ${vti_mtu}
  195. # Send DF packet without exceeding link layer MTU, check that no
  196. # exception is created
  197. ${ns_a} ping -q -M want -i 0.1 -w 2 -s ${ping_payload} ${vti4_b_addr} > /dev/null
  198. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti4_b_addr})"
  199. if [ "${pmtu}" != "" ]; then
  200. err " unexpected exception created with PMTU ${pmtu} for IP payload length ${esp_payload_rfc4106}"
  201. return 1
  202. fi
  203. # Now exceed link layer MTU by one byte, check that exception is created
  204. ${ns_a} ping -q -M want -i 0.1 -w 2 -s $((ping_payload + 1)) ${vti4_b_addr} > /dev/null
  205. pmtu="$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti4_b_addr})"
  206. if [ "${pmtu}" = "" ]; then
  207. err " exception not created for IP payload length $((esp_payload_rfc4106 + 1))"
  208. return 1
  209. fi
  210. # ...with the right PMTU value
  211. if [ ${pmtu} -ne ${esp_payload_rfc4106} ]; then
  212. err " wrong PMTU ${pmtu} in exception, expected: ${esp_payload_rfc4106}"
  213. return 1
  214. fi
  215. }
  216. test_pmtu_vti6_exception() {
  217. setup namespaces veth vti6 xfrm6 || return 2
  218. fail=0
  219. # Create route exception by exceeding link layer MTU
  220. mtu "${ns_a}" veth_a 4000
  221. mtu "${ns_b}" veth_b 4000
  222. mtu "${ns_a}" vti6_a 5000
  223. mtu "${ns_b}" vti6_b 5000
  224. ${ns_a} ping6 -q -i 0.1 -w 2 -s 60000 ${vti6_b_addr} > /dev/null
  225. # Check that exception was created
  226. if [ "$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})" = "" ]; then
  227. err " tunnel exceeding link layer MTU didn't create route exception"
  228. return 1
  229. fi
  230. # Decrease tunnel MTU, check for PMTU decrease in route exception
  231. mtu "${ns_a}" vti6_a 3000
  232. if [ "$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})" -ne 3000 ]; then
  233. err " decreasing tunnel MTU didn't decrease route exception PMTU"
  234. fail=1
  235. fi
  236. # Increase tunnel MTU, check for PMTU increase in route exception
  237. mtu "${ns_a}" vti6_a 9000
  238. if [ "$(route_get_dst_pmtu_from_exception "${ns_a}" ${vti6_b_addr})" -ne 9000 ]; then
  239. err " increasing tunnel MTU didn't increase route exception PMTU"
  240. fail=1
  241. fi
  242. return ${fail}
  243. }
  244. test_pmtu_vti4_default_mtu() {
  245. setup namespaces veth vti4 || return 2
  246. # Check that MTU of vti device is MTU of veth minus IPv4 header length
  247. veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
  248. vti4_mtu="$(link_get_mtu "${ns_a}" vti4_a)"
  249. if [ $((veth_mtu - vti4_mtu)) -ne 20 ]; then
  250. err " vti MTU ${vti4_mtu} is not veth MTU ${veth_mtu} minus IPv4 header length"
  251. return 1
  252. fi
  253. }
  254. test_pmtu_vti6_default_mtu() {
  255. setup namespaces veth vti6 || return 2
  256. # Check that MTU of vti device is MTU of veth minus IPv6 header length
  257. veth_mtu="$(link_get_mtu "${ns_a}" veth_a)"
  258. vti6_mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  259. if [ $((veth_mtu - vti6_mtu)) -ne 40 ]; then
  260. err " vti MTU ${vti6_mtu} is not veth MTU ${veth_mtu} minus IPv6 header length"
  261. return 1
  262. fi
  263. }
  264. test_pmtu_vti4_link_add_mtu() {
  265. setup namespaces || return 2
  266. ${ns_a} ip link add vti4_a type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
  267. [ $? -ne 0 ] && err " vti not supported" && return 2
  268. ${ns_a} ip link del vti4_a
  269. fail=0
  270. min=68
  271. max=$((65528 - 20))
  272. # Check invalid values first
  273. for v in $((min - 1)) $((max + 1)); do
  274. ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10 2>/dev/null
  275. # This can fail, or MTU can be adjusted to a proper value
  276. [ $? -ne 0 ] && continue
  277. mtu="$(link_get_mtu "${ns_a}" vti4_a)"
  278. if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
  279. err " vti tunnel created with invalid MTU ${mtu}"
  280. fail=1
  281. fi
  282. ${ns_a} ip link del vti4_a
  283. done
  284. # Now check valid values
  285. for v in ${min} 1300 ${max}; do
  286. ${ns_a} ip link add vti4_a mtu ${v} type vti local ${veth4_a_addr} remote ${veth4_b_addr} key 10
  287. mtu="$(link_get_mtu "${ns_a}" vti4_a)"
  288. ${ns_a} ip link del vti4_a
  289. if [ "${mtu}" != "${v}" ]; then
  290. err " vti MTU ${mtu} doesn't match configured value ${v}"
  291. fail=1
  292. fi
  293. done
  294. return ${fail}
  295. }
  296. test_pmtu_vti6_link_add_mtu() {
  297. setup namespaces || return 2
  298. ${ns_a} ip link add vti6_a type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
  299. [ $? -ne 0 ] && err " vti6 not supported" && return 2
  300. ${ns_a} ip link del vti6_a
  301. fail=0
  302. min=68 # vti6 can carry IPv4 packets too
  303. max=$((65535 - 40))
  304. # Check invalid values first
  305. for v in $((min - 1)) $((max + 1)); do
  306. ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10 2>/dev/null
  307. # This can fail, or MTU can be adjusted to a proper value
  308. [ $? -ne 0 ] && continue
  309. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  310. if [ ${mtu} -lt ${min} -o ${mtu} -gt ${max} ]; then
  311. err " vti6 tunnel created with invalid MTU ${v}"
  312. fail=1
  313. fi
  314. ${ns_a} ip link del vti6_a
  315. done
  316. # Now check valid values
  317. for v in 68 1280 1300 $((65535 - 40)); do
  318. ${ns_a} ip link add vti6_a mtu ${v} type vti6 local ${veth6_a_addr} remote ${veth6_b_addr} key 10
  319. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  320. ${ns_a} ip link del vti6_a
  321. if [ "${mtu}" != "${v}" ]; then
  322. err " vti6 MTU ${mtu} doesn't match configured value ${v}"
  323. fail=1
  324. fi
  325. done
  326. return ${fail}
  327. }
  328. test_pmtu_vti6_link_change_mtu() {
  329. setup namespaces || return 2
  330. ${ns_a} ip link add dummy0 mtu 1500 type dummy
  331. [ $? -ne 0 ] && err " dummy not supported" && return 2
  332. ${ns_a} ip link add dummy1 mtu 3000 type dummy
  333. ${ns_a} ip link set dummy0 up
  334. ${ns_a} ip link set dummy1 up
  335. ${ns_a} ip addr add ${dummy6_0_addr}/${dummy6_mask} dev dummy0
  336. ${ns_a} ip addr add ${dummy6_1_addr}/${dummy6_mask} dev dummy1
  337. fail=0
  338. # Create vti6 interface bound to device, passing MTU, check it
  339. ${ns_a} ip link add vti6_a mtu 1300 type vti6 remote ${dummy6_0_addr} local ${dummy6_0_addr}
  340. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  341. if [ ${mtu} -ne 1300 ]; then
  342. err " vti6 MTU ${mtu} doesn't match configured value 1300"
  343. fail=1
  344. fi
  345. # Move to another device with different MTU, without passing MTU, check
  346. # MTU is adjusted
  347. ${ns_a} ip link set vti6_a type vti6 remote ${dummy6_1_addr} local ${dummy6_1_addr}
  348. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  349. if [ ${mtu} -ne $((3000 - 40)) ]; then
  350. err " vti MTU ${mtu} is not dummy MTU 3000 minus IPv6 header length"
  351. fail=1
  352. fi
  353. # Move it back, passing MTU, check MTU is not overridden
  354. ${ns_a} ip link set vti6_a mtu 1280 type vti6 remote ${dummy6_0_addr} local ${dummy6_0_addr}
  355. mtu="$(link_get_mtu "${ns_a}" vti6_a)"
  356. if [ ${mtu} -ne 1280 ]; then
  357. err " vti6 MTU ${mtu} doesn't match configured value 1280"
  358. fail=1
  359. fi
  360. return ${fail}
  361. }
  362. trap cleanup EXIT
  363. exitcode=0
  364. desc=0
  365. IFS="
  366. "
  367. for t in ${tests}; do
  368. [ $desc -eq 0 ] && name="${t}" && desc=1 && continue || desc=0
  369. (
  370. unset IFS
  371. eval test_${name}
  372. ret=$?
  373. cleanup
  374. if [ $ret -eq 0 ]; then
  375. printf "TEST: %-60s [ OK ]\n" "${t}"
  376. elif [ $ret -eq 1 ]; then
  377. printf "TEST: %-60s [FAIL]\n" "${t}"
  378. err_flush
  379. exit 1
  380. elif [ $ret -eq 2 ]; then
  381. printf "TEST: %-60s [SKIP]\n" "${t}"
  382. err_flush
  383. fi
  384. )
  385. [ $? -ne 0 ] && exitcode=1
  386. done
  387. exit ${exitcode}