lib.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. ##############################################################################
  4. # Defines
  5. # Can be overridden by the configuration file.
  6. PING=${PING:=ping}
  7. PING6=${PING6:=ping6}
  8. MZ=${MZ:=mausezahn}
  9. WAIT_TIME=${WAIT_TIME:=5}
  10. PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
  11. PAUSE_ON_CLEANUP=${PAUSE_ON_CLEANUP:=no}
  12. NETIF_TYPE=${NETIF_TYPE:=veth}
  13. NETIF_CREATE=${NETIF_CREATE:=yes}
  14. if [[ -f forwarding.config ]]; then
  15. source forwarding.config
  16. fi
  17. ##############################################################################
  18. # Sanity checks
  19. check_tc_version()
  20. {
  21. tc -j &> /dev/null
  22. if [[ $? -ne 0 ]]; then
  23. echo "SKIP: iproute2 too old; tc is missing JSON support"
  24. exit 1
  25. fi
  26. tc filter help 2>&1 | grep block &> /dev/null
  27. if [[ $? -ne 0 ]]; then
  28. echo "SKIP: iproute2 too old; tc is missing shared block support"
  29. exit 1
  30. fi
  31. }
  32. if [[ "$(id -u)" -ne 0 ]]; then
  33. echo "SKIP: need root privileges"
  34. exit 0
  35. fi
  36. if [[ "$CHECK_TC" = "yes" ]]; then
  37. check_tc_version
  38. fi
  39. if [[ ! -x "$(command -v jq)" ]]; then
  40. echo "SKIP: jq not installed"
  41. exit 1
  42. fi
  43. if [[ ! -x "$(command -v $MZ)" ]]; then
  44. echo "SKIP: $MZ not installed"
  45. exit 1
  46. fi
  47. if [[ ! -v NUM_NETIFS ]]; then
  48. echo "SKIP: importer does not define \"NUM_NETIFS\""
  49. exit 1
  50. fi
  51. ##############################################################################
  52. # Command line options handling
  53. count=0
  54. while [[ $# -gt 0 ]]; do
  55. if [[ "$count" -eq "0" ]]; then
  56. unset NETIFS
  57. declare -A NETIFS
  58. fi
  59. count=$((count + 1))
  60. NETIFS[p$count]="$1"
  61. shift
  62. done
  63. ##############################################################################
  64. # Network interfaces configuration
  65. create_netif_veth()
  66. {
  67. local i
  68. for i in $(eval echo {1..$NUM_NETIFS}); do
  69. local j=$((i+1))
  70. ip link show dev ${NETIFS[p$i]} &> /dev/null
  71. if [[ $? -ne 0 ]]; then
  72. ip link add ${NETIFS[p$i]} type veth \
  73. peer name ${NETIFS[p$j]}
  74. if [[ $? -ne 0 ]]; then
  75. echo "Failed to create netif"
  76. exit 1
  77. fi
  78. fi
  79. i=$j
  80. done
  81. }
  82. create_netif()
  83. {
  84. case "$NETIF_TYPE" in
  85. veth) create_netif_veth
  86. ;;
  87. *) echo "Can not create interfaces of type \'$NETIF_TYPE\'"
  88. exit 1
  89. ;;
  90. esac
  91. }
  92. if [[ "$NETIF_CREATE" = "yes" ]]; then
  93. create_netif
  94. fi
  95. for i in $(eval echo {1..$NUM_NETIFS}); do
  96. ip link show dev ${NETIFS[p$i]} &> /dev/null
  97. if [[ $? -ne 0 ]]; then
  98. echo "SKIP: could not find all required interfaces"
  99. exit 1
  100. fi
  101. done
  102. ##############################################################################
  103. # Helpers
  104. # Exit status to return at the end. Set in case one of the tests fails.
  105. EXIT_STATUS=0
  106. # Per-test return value. Clear at the beginning of each test.
  107. RET=0
  108. check_err()
  109. {
  110. local err=$1
  111. local msg=$2
  112. if [[ $RET -eq 0 && $err -ne 0 ]]; then
  113. RET=$err
  114. retmsg=$msg
  115. fi
  116. }
  117. check_fail()
  118. {
  119. local err=$1
  120. local msg=$2
  121. if [[ $RET -eq 0 && $err -eq 0 ]]; then
  122. RET=1
  123. retmsg=$msg
  124. fi
  125. }
  126. log_test()
  127. {
  128. local test_name=$1
  129. local opt_str=$2
  130. if [[ $# -eq 2 ]]; then
  131. opt_str="($opt_str)"
  132. fi
  133. if [[ $RET -ne 0 ]]; then
  134. EXIT_STATUS=1
  135. printf "TEST: %-60s [FAIL]\n" "$test_name $opt_str"
  136. if [[ ! -z "$retmsg" ]]; then
  137. printf "\t%s\n" "$retmsg"
  138. fi
  139. if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
  140. echo "Hit enter to continue, 'q' to quit"
  141. read a
  142. [ "$a" = "q" ] && exit 1
  143. fi
  144. return 1
  145. fi
  146. printf "TEST: %-60s [PASS]\n" "$test_name $opt_str"
  147. return 0
  148. }
  149. log_info()
  150. {
  151. local msg=$1
  152. echo "INFO: $msg"
  153. }
  154. setup_wait()
  155. {
  156. for i in $(eval echo {1..$NUM_NETIFS}); do
  157. while true; do
  158. ip link show dev ${NETIFS[p$i]} up \
  159. | grep 'state UP' &> /dev/null
  160. if [[ $? -ne 0 ]]; then
  161. sleep 1
  162. else
  163. break
  164. fi
  165. done
  166. done
  167. # Make sure links are ready.
  168. sleep $WAIT_TIME
  169. }
  170. pre_cleanup()
  171. {
  172. if [ "${PAUSE_ON_CLEANUP}" = "yes" ]; then
  173. echo "Pausing before cleanup, hit any key to continue"
  174. read
  175. fi
  176. }
  177. vrf_prepare()
  178. {
  179. ip -4 rule add pref 32765 table local
  180. ip -4 rule del pref 0
  181. ip -6 rule add pref 32765 table local
  182. ip -6 rule del pref 0
  183. }
  184. vrf_cleanup()
  185. {
  186. ip -6 rule add pref 0 table local
  187. ip -6 rule del pref 32765
  188. ip -4 rule add pref 0 table local
  189. ip -4 rule del pref 32765
  190. }
  191. __last_tb_id=0
  192. declare -A __TB_IDS
  193. __vrf_td_id_assign()
  194. {
  195. local vrf_name=$1
  196. __last_tb_id=$((__last_tb_id + 1))
  197. __TB_IDS[$vrf_name]=$__last_tb_id
  198. return $__last_tb_id
  199. }
  200. __vrf_td_id_lookup()
  201. {
  202. local vrf_name=$1
  203. return ${__TB_IDS[$vrf_name]}
  204. }
  205. vrf_create()
  206. {
  207. local vrf_name=$1
  208. local tb_id
  209. __vrf_td_id_assign $vrf_name
  210. tb_id=$?
  211. ip link add dev $vrf_name type vrf table $tb_id
  212. ip -4 route add table $tb_id unreachable default metric 4278198272
  213. ip -6 route add table $tb_id unreachable default metric 4278198272
  214. }
  215. vrf_destroy()
  216. {
  217. local vrf_name=$1
  218. local tb_id
  219. __vrf_td_id_lookup $vrf_name
  220. tb_id=$?
  221. ip -6 route del table $tb_id unreachable default metric 4278198272
  222. ip -4 route del table $tb_id unreachable default metric 4278198272
  223. ip link del dev $vrf_name
  224. }
  225. __addr_add_del()
  226. {
  227. local if_name=$1
  228. local add_del=$2
  229. local array
  230. shift
  231. shift
  232. array=("${@}")
  233. for addrstr in "${array[@]}"; do
  234. ip address $add_del $addrstr dev $if_name
  235. done
  236. }
  237. simple_if_init()
  238. {
  239. local if_name=$1
  240. local vrf_name
  241. local array
  242. shift
  243. vrf_name=v$if_name
  244. array=("${@}")
  245. vrf_create $vrf_name
  246. ip link set dev $if_name master $vrf_name
  247. ip link set dev $vrf_name up
  248. ip link set dev $if_name up
  249. __addr_add_del $if_name add "${array[@]}"
  250. }
  251. simple_if_fini()
  252. {
  253. local if_name=$1
  254. local vrf_name
  255. local array
  256. shift
  257. vrf_name=v$if_name
  258. array=("${@}")
  259. __addr_add_del $if_name del "${array[@]}"
  260. ip link set dev $if_name down
  261. vrf_destroy $vrf_name
  262. }
  263. tunnel_create()
  264. {
  265. local name=$1; shift
  266. local type=$1; shift
  267. local local=$1; shift
  268. local remote=$1; shift
  269. ip link add name $name type $type \
  270. local $local remote $remote "$@"
  271. ip link set dev $name up
  272. }
  273. tunnel_destroy()
  274. {
  275. local name=$1; shift
  276. ip link del dev $name
  277. }
  278. vlan_create()
  279. {
  280. local if_name=$1; shift
  281. local vid=$1; shift
  282. local vrf=$1; shift
  283. local ips=("${@}")
  284. local name=$if_name.$vid
  285. ip link add name $name link $if_name type vlan id $vid
  286. if [ "$vrf" != "" ]; then
  287. ip link set dev $name master $vrf
  288. fi
  289. ip link set dev $name up
  290. __addr_add_del $name add "${ips[@]}"
  291. }
  292. vlan_destroy()
  293. {
  294. local if_name=$1; shift
  295. local vid=$1; shift
  296. local name=$if_name.$vid
  297. ip link del dev $name
  298. }
  299. master_name_get()
  300. {
  301. local if_name=$1
  302. ip -j link show dev $if_name | jq -r '.[]["master"]'
  303. }
  304. link_stats_tx_packets_get()
  305. {
  306. local if_name=$1
  307. ip -j -s link show dev $if_name | jq '.[]["stats64"]["tx"]["packets"]'
  308. }
  309. tc_rule_stats_get()
  310. {
  311. local dev=$1; shift
  312. local pref=$1; shift
  313. tc -j -s filter show dev $dev ingress pref $pref |
  314. jq '.[1].options.actions[].stats.packets'
  315. }
  316. mac_get()
  317. {
  318. local if_name=$1
  319. ip -j link show dev $if_name | jq -r '.[]["address"]'
  320. }
  321. bridge_ageing_time_get()
  322. {
  323. local bridge=$1
  324. local ageing_time
  325. # Need to divide by 100 to convert to seconds.
  326. ageing_time=$(ip -j -d link show dev $bridge \
  327. | jq '.[]["linkinfo"]["info_data"]["ageing_time"]')
  328. echo $((ageing_time / 100))
  329. }
  330. declare -A SYSCTL_ORIG
  331. sysctl_set()
  332. {
  333. local key=$1; shift
  334. local value=$1; shift
  335. SYSCTL_ORIG[$key]=$(sysctl -n $key)
  336. sysctl -qw $key=$value
  337. }
  338. sysctl_restore()
  339. {
  340. local key=$1; shift
  341. sysctl -qw $key=${SYSCTL_ORIG["$key"]}
  342. }
  343. forwarding_enable()
  344. {
  345. sysctl_set net.ipv4.conf.all.forwarding 1
  346. sysctl_set net.ipv6.conf.all.forwarding 1
  347. }
  348. forwarding_restore()
  349. {
  350. sysctl_restore net.ipv6.conf.all.forwarding
  351. sysctl_restore net.ipv4.conf.all.forwarding
  352. }
  353. tc_offload_check()
  354. {
  355. for i in $(eval echo {1..$NUM_NETIFS}); do
  356. ethtool -k ${NETIFS[p$i]} \
  357. | grep "hw-tc-offload: on" &> /dev/null
  358. if [[ $? -ne 0 ]]; then
  359. return 1
  360. fi
  361. done
  362. return 0
  363. }
  364. trap_install()
  365. {
  366. local dev=$1; shift
  367. local direction=$1; shift
  368. # For slow-path testing, we need to install a trap to get to
  369. # slow path the packets that would otherwise be switched in HW.
  370. tc filter add dev $dev $direction pref 1 flower skip_sw action trap
  371. }
  372. trap_uninstall()
  373. {
  374. local dev=$1; shift
  375. local direction=$1; shift
  376. tc filter del dev $dev $direction pref 1 flower skip_sw
  377. }
  378. slow_path_trap_install()
  379. {
  380. if [ "${tcflags/skip_hw}" != "$tcflags" ]; then
  381. trap_install "$@"
  382. fi
  383. }
  384. slow_path_trap_uninstall()
  385. {
  386. if [ "${tcflags/skip_hw}" != "$tcflags" ]; then
  387. trap_uninstall "$@"
  388. fi
  389. }
  390. __icmp_capture_add_del()
  391. {
  392. local add_del=$1; shift
  393. local pref=$1; shift
  394. local vsuf=$1; shift
  395. local tundev=$1; shift
  396. local filter=$1; shift
  397. tc filter $add_del dev "$tundev" ingress \
  398. proto ip$vsuf pref $pref \
  399. flower ip_proto icmp$vsuf $filter \
  400. action pass
  401. }
  402. icmp_capture_install()
  403. {
  404. __icmp_capture_add_del add 100 "" "$@"
  405. }
  406. icmp_capture_uninstall()
  407. {
  408. __icmp_capture_add_del del 100 "" "$@"
  409. }
  410. icmp6_capture_install()
  411. {
  412. __icmp_capture_add_del add 100 v6 "$@"
  413. }
  414. icmp6_capture_uninstall()
  415. {
  416. __icmp_capture_add_del del 100 v6 "$@"
  417. }
  418. __vlan_capture_add_del()
  419. {
  420. local add_del=$1; shift
  421. local pref=$1; shift
  422. local dev=$1; shift
  423. local filter=$1; shift
  424. tc filter $add_del dev "$dev" ingress \
  425. proto 802.1q pref $pref \
  426. flower $filter \
  427. action pass
  428. }
  429. vlan_capture_install()
  430. {
  431. __vlan_capture_add_del add 100 "$@"
  432. }
  433. vlan_capture_uninstall()
  434. {
  435. __vlan_capture_add_del del 100 "$@"
  436. }
  437. matchall_sink_create()
  438. {
  439. local dev=$1; shift
  440. tc qdisc add dev $dev clsact
  441. tc filter add dev $dev ingress \
  442. pref 10000 \
  443. matchall \
  444. action drop
  445. }
  446. tests_run()
  447. {
  448. local current_test
  449. for current_test in ${TESTS:-$ALL_TESTS}; do
  450. $current_test
  451. done
  452. }
  453. ##############################################################################
  454. # Tests
  455. ping_test()
  456. {
  457. local if_name=$1
  458. local dip=$2
  459. local vrf_name
  460. RET=0
  461. vrf_name=$(master_name_get $if_name)
  462. ip vrf exec $vrf_name $PING $dip -c 10 -i 0.1 -w 2 &> /dev/null
  463. check_err $?
  464. log_test "ping"
  465. }
  466. ping6_test()
  467. {
  468. local if_name=$1
  469. local dip=$2
  470. local vrf_name
  471. RET=0
  472. vrf_name=$(master_name_get $if_name)
  473. ip vrf exec $vrf_name $PING6 $dip -c 10 -i 0.1 -w 2 &> /dev/null
  474. check_err $?
  475. log_test "ping6"
  476. }
  477. learning_test()
  478. {
  479. local bridge=$1
  480. local br_port1=$2 # Connected to `host1_if`.
  481. local host1_if=$3
  482. local host2_if=$4
  483. local mac=de:ad:be:ef:13:37
  484. local ageing_time
  485. RET=0
  486. bridge -j fdb show br $bridge brport $br_port1 \
  487. | jq -e ".[] | select(.mac == \"$mac\")" &> /dev/null
  488. check_fail $? "Found FDB record when should not"
  489. # Disable unknown unicast flooding on `br_port1` to make sure
  490. # packets are only forwarded through the port after a matching
  491. # FDB entry was installed.
  492. bridge link set dev $br_port1 flood off
  493. tc qdisc add dev $host1_if ingress
  494. tc filter add dev $host1_if ingress protocol ip pref 1 handle 101 \
  495. flower dst_mac $mac action drop
  496. $MZ $host2_if -c 1 -p 64 -b $mac -t ip -q
  497. sleep 1
  498. tc -j -s filter show dev $host1_if ingress \
  499. | jq -e ".[] | select(.options.handle == 101) \
  500. | select(.options.actions[0].stats.packets == 1)" &> /dev/null
  501. check_fail $? "Packet reached second host when should not"
  502. $MZ $host1_if -c 1 -p 64 -a $mac -t ip -q
  503. sleep 1
  504. bridge -j fdb show br $bridge brport $br_port1 \
  505. | jq -e ".[] | select(.mac == \"$mac\")" &> /dev/null
  506. check_err $? "Did not find FDB record when should"
  507. $MZ $host2_if -c 1 -p 64 -b $mac -t ip -q
  508. sleep 1
  509. tc -j -s filter show dev $host1_if ingress \
  510. | jq -e ".[] | select(.options.handle == 101) \
  511. | select(.options.actions[0].stats.packets == 1)" &> /dev/null
  512. check_err $? "Packet did not reach second host when should"
  513. # Wait for 10 seconds after the ageing time to make sure FDB
  514. # record was aged-out.
  515. ageing_time=$(bridge_ageing_time_get $bridge)
  516. sleep $((ageing_time + 10))
  517. bridge -j fdb show br $bridge brport $br_port1 \
  518. | jq -e ".[] | select(.mac == \"$mac\")" &> /dev/null
  519. check_fail $? "Found FDB record when should not"
  520. bridge link set dev $br_port1 learning off
  521. $MZ $host1_if -c 1 -p 64 -a $mac -t ip -q
  522. sleep 1
  523. bridge -j fdb show br $bridge brport $br_port1 \
  524. | jq -e ".[] | select(.mac == \"$mac\")" &> /dev/null
  525. check_fail $? "Found FDB record when should not"
  526. bridge link set dev $br_port1 learning on
  527. tc filter del dev $host1_if ingress protocol ip pref 1 handle 101 flower
  528. tc qdisc del dev $host1_if ingress
  529. bridge link set dev $br_port1 flood on
  530. log_test "FDB learning"
  531. }
  532. flood_test_do()
  533. {
  534. local should_flood=$1
  535. local mac=$2
  536. local ip=$3
  537. local host1_if=$4
  538. local host2_if=$5
  539. local err=0
  540. # Add an ACL on `host2_if` which will tell us whether the packet
  541. # was flooded to it or not.
  542. tc qdisc add dev $host2_if ingress
  543. tc filter add dev $host2_if ingress protocol ip pref 1 handle 101 \
  544. flower dst_mac $mac action drop
  545. $MZ $host1_if -c 1 -p 64 -b $mac -B $ip -t ip -q
  546. sleep 1
  547. tc -j -s filter show dev $host2_if ingress \
  548. | jq -e ".[] | select(.options.handle == 101) \
  549. | select(.options.actions[0].stats.packets == 1)" &> /dev/null
  550. if [[ $? -ne 0 && $should_flood == "true" || \
  551. $? -eq 0 && $should_flood == "false" ]]; then
  552. err=1
  553. fi
  554. tc filter del dev $host2_if ingress protocol ip pref 1 handle 101 flower
  555. tc qdisc del dev $host2_if ingress
  556. return $err
  557. }
  558. flood_unicast_test()
  559. {
  560. local br_port=$1
  561. local host1_if=$2
  562. local host2_if=$3
  563. local mac=de:ad:be:ef:13:37
  564. local ip=192.0.2.100
  565. RET=0
  566. bridge link set dev $br_port flood off
  567. flood_test_do false $mac $ip $host1_if $host2_if
  568. check_err $? "Packet flooded when should not"
  569. bridge link set dev $br_port flood on
  570. flood_test_do true $mac $ip $host1_if $host2_if
  571. check_err $? "Packet was not flooded when should"
  572. log_test "Unknown unicast flood"
  573. }
  574. flood_multicast_test()
  575. {
  576. local br_port=$1
  577. local host1_if=$2
  578. local host2_if=$3
  579. local mac=01:00:5e:00:00:01
  580. local ip=239.0.0.1
  581. RET=0
  582. bridge link set dev $br_port mcast_flood off
  583. flood_test_do false $mac $ip $host1_if $host2_if
  584. check_err $? "Packet flooded when should not"
  585. bridge link set dev $br_port mcast_flood on
  586. flood_test_do true $mac $ip $host1_if $host2_if
  587. check_err $? "Packet was not flooded when should"
  588. log_test "Unregistered multicast flood"
  589. }
  590. flood_test()
  591. {
  592. # `br_port` is connected to `host2_if`
  593. local br_port=$1
  594. local host1_if=$2
  595. local host2_if=$3
  596. flood_unicast_test $br_port $host1_if $host2_if
  597. flood_multicast_test $br_port $host1_if $host2_if
  598. }