fib_notifications.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. lib_dir=$(dirname $0)/../../../net/forwarding
  4. ALL_TESTS="
  5. ipv4_route_addition_test
  6. ipv4_route_deletion_test
  7. ipv4_route_replacement_test
  8. ipv4_route_offload_failed_test
  9. ipv6_route_addition_test
  10. ipv6_route_deletion_test
  11. ipv6_route_replacement_test
  12. ipv6_route_offload_failed_test
  13. "
  14. NETDEVSIM_PATH=/sys/bus/netdevsim/
  15. DEV_ADDR=1337
  16. DEV=netdevsim${DEV_ADDR}
  17. DEVLINK_DEV=netdevsim/${DEV}
  18. SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
  19. DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV/
  20. NUM_NETIFS=0
  21. source $lib_dir/lib.sh
  22. check_rt_offload_failed()
  23. {
  24. local outfile=$1; shift
  25. local line
  26. # Make sure that the first notification was emitted without
  27. # RTM_F_OFFLOAD_FAILED flag and the second with RTM_F_OFFLOAD_FAILED
  28. # flag
  29. head -n 1 $outfile | grep -q "rt_offload_failed"
  30. if [[ $? -eq 0 ]]; then
  31. return 1
  32. fi
  33. head -n 2 $outfile | tail -n 1 | grep -q "rt_offload_failed"
  34. }
  35. check_rt_trap()
  36. {
  37. local outfile=$1; shift
  38. local line
  39. # Make sure that the first notification was emitted without RTM_F_TRAP
  40. # flag and the second with RTM_F_TRAP flag
  41. head -n 1 $outfile | grep -q "rt_trap"
  42. if [[ $? -eq 0 ]]; then
  43. return 1
  44. fi
  45. head -n 2 $outfile | tail -n 1 | grep -q "rt_trap"
  46. }
  47. route_notify_check()
  48. {
  49. local outfile=$1; shift
  50. local expected_num_lines=$1; shift
  51. local offload_failed=${1:-0}; shift
  52. # check the monitor results
  53. lines=`wc -l $outfile | cut "-d " -f1`
  54. test $lines -eq $expected_num_lines
  55. check_err $? "$expected_num_lines notifications were expected but $lines were received"
  56. if [[ $expected_num_lines -eq 1 ]]; then
  57. return
  58. fi
  59. if [[ $offload_failed -eq 0 ]]; then
  60. check_rt_trap $outfile
  61. check_err $? "Wrong RTM_F_TRAP flags in notifications"
  62. else
  63. check_rt_offload_failed $outfile
  64. check_err $? "Wrong RTM_F_OFFLOAD_FAILED flags in notifications"
  65. fi
  66. }
  67. route_addition_check()
  68. {
  69. local ip=$1; shift
  70. local notify=$1; shift
  71. local route=$1; shift
  72. local expected_num_notifications=$1; shift
  73. local offload_failed=${1:-0}; shift
  74. ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
  75. local outfile=$(mktemp)
  76. $IP monitor route &> $outfile &
  77. sleep 1
  78. $IP route add $route dev dummy1
  79. sleep 1
  80. kill_process %%
  81. route_notify_check $outfile $expected_num_notifications $offload_failed
  82. rm -f $outfile
  83. $IP route del $route dev dummy1
  84. }
  85. ipv4_route_addition_test()
  86. {
  87. RET=0
  88. local ip="ipv4"
  89. local route=192.0.2.0/24
  90. # Make sure a single notification will be emitted for the programmed
  91. # route.
  92. local notify=0
  93. local expected_num_notifications=1
  94. # route_addition_check will assign value to RET.
  95. route_addition_check $ip $notify $route $expected_num_notifications
  96. # Make sure two notifications will be emitted for the programmed route.
  97. notify=1
  98. expected_num_notifications=2
  99. route_addition_check $ip $notify $route $expected_num_notifications
  100. # notify=2 means emit notifications only for failed route installation,
  101. # make sure a single notification will be emitted for the programmed
  102. # route.
  103. notify=2
  104. expected_num_notifications=1
  105. route_addition_check $ip $notify $route $expected_num_notifications
  106. log_test "IPv4 route addition"
  107. }
  108. route_deletion_check()
  109. {
  110. local ip=$1; shift
  111. local notify=$1; shift
  112. local route=$1; shift
  113. local expected_num_notifications=$1; shift
  114. ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
  115. $IP route add $route dev dummy1
  116. sleep 1
  117. local outfile=$(mktemp)
  118. $IP monitor route &> $outfile &
  119. sleep 1
  120. $IP route del $route dev dummy1
  121. sleep 1
  122. kill_process %%
  123. route_notify_check $outfile $expected_num_notifications
  124. rm -f $outfile
  125. }
  126. ipv4_route_deletion_test()
  127. {
  128. RET=0
  129. local ip="ipv4"
  130. local route=192.0.2.0/24
  131. local expected_num_notifications=1
  132. # Make sure a single notification will be emitted for the deleted route,
  133. # regardless of fib_notify_on_flag_change value.
  134. local notify=0
  135. # route_deletion_check will assign value to RET.
  136. route_deletion_check $ip $notify $route $expected_num_notifications
  137. notify=1
  138. route_deletion_check $ip $notify $route $expected_num_notifications
  139. log_test "IPv4 route deletion"
  140. }
  141. route_replacement_check()
  142. {
  143. local ip=$1; shift
  144. local notify=$1; shift
  145. local route=$1; shift
  146. local expected_num_notifications=$1; shift
  147. ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
  148. $IP route add $route dev dummy1
  149. sleep 1
  150. local outfile=$(mktemp)
  151. $IP monitor route &> $outfile &
  152. sleep 1
  153. $IP route replace $route dev dummy2
  154. sleep 1
  155. kill_process %%
  156. route_notify_check $outfile $expected_num_notifications
  157. rm -f $outfile
  158. $IP route del $route dev dummy2
  159. }
  160. ipv4_route_replacement_test()
  161. {
  162. RET=0
  163. local ip="ipv4"
  164. local route=192.0.2.0/24
  165. $IP link add name dummy2 type dummy
  166. $IP link set dev dummy2 up
  167. # Make sure a single notification will be emitted for the new route.
  168. local notify=0
  169. local expected_num_notifications=1
  170. # route_replacement_check will assign value to RET.
  171. route_replacement_check $ip $notify $route $expected_num_notifications
  172. # Make sure two notifications will be emitted for the new route.
  173. notify=1
  174. expected_num_notifications=2
  175. route_replacement_check $ip $notify $route $expected_num_notifications
  176. # notify=2 means emit notifications only for failed route installation,
  177. # make sure a single notification will be emitted for the new route.
  178. notify=2
  179. expected_num_notifications=1
  180. route_replacement_check $ip $notify $route $expected_num_notifications
  181. $IP link del name dummy2
  182. log_test "IPv4 route replacement"
  183. }
  184. ipv4_route_offload_failed_test()
  185. {
  186. RET=0
  187. local ip="ipv4"
  188. local route=192.0.2.0/24
  189. local offload_failed=1
  190. echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
  191. check_err $? "Failed to setup route offload to fail"
  192. # Make sure a single notification will be emitted for the programmed
  193. # route.
  194. local notify=0
  195. local expected_num_notifications=1
  196. route_addition_check $ip $notify $route $expected_num_notifications \
  197. $offload_failed
  198. # Make sure two notifications will be emitted for the new route.
  199. notify=1
  200. expected_num_notifications=2
  201. route_addition_check $ip $notify $route $expected_num_notifications \
  202. $offload_failed
  203. # notify=2 means emit notifications only for failed route installation,
  204. # make sure two notifications will be emitted for the new route.
  205. notify=2
  206. expected_num_notifications=2
  207. route_addition_check $ip $notify $route $expected_num_notifications \
  208. $offload_failed
  209. echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
  210. check_err $? "Failed to setup route offload not to fail"
  211. log_test "IPv4 route offload failed"
  212. }
  213. ipv6_route_addition_test()
  214. {
  215. RET=0
  216. local ip="ipv6"
  217. local route=2001:db8:1::/64
  218. # Make sure a single notification will be emitted for the programmed
  219. # route.
  220. local notify=0
  221. local expected_num_notifications=1
  222. route_addition_check $ip $notify $route $expected_num_notifications
  223. # Make sure two notifications will be emitted for the programmed route.
  224. notify=1
  225. expected_num_notifications=2
  226. route_addition_check $ip $notify $route $expected_num_notifications
  227. # notify=2 means emit notifications only for failed route installation,
  228. # make sure a single notification will be emitted for the programmed
  229. # route.
  230. notify=2
  231. expected_num_notifications=1
  232. route_addition_check $ip $notify $route $expected_num_notifications
  233. log_test "IPv6 route addition"
  234. }
  235. ipv6_route_deletion_test()
  236. {
  237. RET=0
  238. local ip="ipv6"
  239. local route=2001:db8:1::/64
  240. local expected_num_notifications=1
  241. # Make sure a single notification will be emitted for the deleted route,
  242. # regardless of fib_notify_on_flag_change value.
  243. local notify=0
  244. route_deletion_check $ip $notify $route $expected_num_notifications
  245. notify=1
  246. route_deletion_check $ip $notify $route $expected_num_notifications
  247. log_test "IPv6 route deletion"
  248. }
  249. ipv6_route_replacement_test()
  250. {
  251. RET=0
  252. local ip="ipv6"
  253. local route=2001:db8:1::/64
  254. $IP link add name dummy2 type dummy
  255. $IP link set dev dummy2 up
  256. # Make sure a single notification will be emitted for the new route.
  257. local notify=0
  258. local expected_num_notifications=1
  259. route_replacement_check $ip $notify $route $expected_num_notifications
  260. # Make sure two notifications will be emitted for the new route.
  261. notify=1
  262. expected_num_notifications=2
  263. route_replacement_check $ip $notify $route $expected_num_notifications
  264. # notify=2 means emit notifications only for failed route installation,
  265. # make sure a single notification will be emitted for the new route.
  266. notify=2
  267. expected_num_notifications=1
  268. route_replacement_check $ip $notify $route $expected_num_notifications
  269. $IP link del name dummy2
  270. log_test "IPv6 route replacement"
  271. }
  272. ipv6_route_offload_failed_test()
  273. {
  274. RET=0
  275. local ip="ipv6"
  276. local route=2001:db8:1::/64
  277. local offload_failed=1
  278. echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
  279. check_err $? "Failed to setup route offload to fail"
  280. # Make sure a single notification will be emitted for the programmed
  281. # route.
  282. local notify=0
  283. local expected_num_notifications=1
  284. route_addition_check $ip $notify $route $expected_num_notifications \
  285. $offload_failed
  286. # Make sure two notifications will be emitted for the new route.
  287. notify=1
  288. expected_num_notifications=2
  289. route_addition_check $ip $notify $route $expected_num_notifications \
  290. $offload_failed
  291. # notify=2 means emit notifications only for failed route installation,
  292. # make sure two notifications will be emitted for the new route.
  293. notify=2
  294. expected_num_notifications=2
  295. route_addition_check $ip $notify $route $expected_num_notifications \
  296. $offload_failed
  297. echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
  298. check_err $? "Failed to setup route offload not to fail"
  299. log_test "IPv6 route offload failed"
  300. }
  301. setup_prepare()
  302. {
  303. modprobe netdevsim &> /dev/null
  304. echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
  305. while [ ! -d $SYSFS_NET_DIR ] ; do :; done
  306. ip netns add testns1
  307. if [ $? -ne 0 ]; then
  308. echo "Failed to add netns \"testns1\""
  309. exit 1
  310. fi
  311. devlink dev reload $DEVLINK_DEV netns testns1
  312. if [ $? -ne 0 ]; then
  313. echo "Failed to reload into netns \"testns1\""
  314. exit 1
  315. fi
  316. IP="ip -n testns1"
  317. $IP link add name dummy1 type dummy
  318. $IP link set dev dummy1 up
  319. }
  320. cleanup()
  321. {
  322. pre_cleanup
  323. $IP link del name dummy1
  324. ip netns del testns1
  325. echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
  326. modprobe -r netdevsim &> /dev/null
  327. }
  328. trap cleanup EXIT
  329. setup_prepare
  330. tests_run
  331. exit $EXIT_STATUS