pmtu.sh 14 KB

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