devlink_lib.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Kselftest framework requirement - SKIP code is 4.
  4. ksft_skip=4
  5. ##############################################################################
  6. # Defines
  7. if [[ ! -v DEVLINK_DEV ]]; then
  8. DEVLINK_DEV=$(devlink port show "${NETIFS[p1]:-$NETIF_NO_CABLE}" -j \
  9. | jq -r '.port | keys[]' | cut -d/ -f-2)
  10. if [ -z "$DEVLINK_DEV" ]; then
  11. echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"
  12. exit $ksft_skip
  13. fi
  14. if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
  15. echo "SKIP: devlink device's bus is not PCI"
  16. exit $ksft_skip
  17. fi
  18. DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
  19. -n | cut -d" " -f3)
  20. elif [[ ! -z "$DEVLINK_DEV" ]]; then
  21. devlink dev show $DEVLINK_DEV &> /dev/null
  22. if [ $? -ne 0 ]; then
  23. echo "SKIP: devlink device \"$DEVLINK_DEV\" not found"
  24. exit $ksft_skip
  25. fi
  26. fi
  27. ##############################################################################
  28. # Sanity checks
  29. devlink help 2>&1 | grep resource &> /dev/null
  30. if [ $? -ne 0 ]; then
  31. echo "SKIP: iproute2 too old, missing devlink resource support"
  32. exit $ksft_skip
  33. fi
  34. devlink help 2>&1 | grep trap &> /dev/null
  35. if [ $? -ne 0 ]; then
  36. echo "SKIP: iproute2 too old, missing devlink trap support"
  37. exit $ksft_skip
  38. fi
  39. devlink dev help 2>&1 | grep info &> /dev/null
  40. if [ $? -ne 0 ]; then
  41. echo "SKIP: iproute2 too old, missing devlink dev info support"
  42. exit $ksft_skip
  43. fi
  44. ##############################################################################
  45. # Devlink helpers
  46. devlink_resource_names_to_path()
  47. {
  48. local resource
  49. local path=""
  50. for resource in "${@}"; do
  51. if [ "$path" == "" ]; then
  52. path="$resource"
  53. else
  54. path="${path}/$resource"
  55. fi
  56. done
  57. echo "$path"
  58. }
  59. devlink_resource_get()
  60. {
  61. local name=$1
  62. local resource_name=.[][\"$DEVLINK_DEV\"]
  63. resource_name="$resource_name | .[] | select (.name == \"$name\")"
  64. shift
  65. for resource in "${@}"; do
  66. resource_name="${resource_name} | .[\"resources\"][] | \
  67. select (.name == \"$resource\")"
  68. done
  69. devlink -j resource show "$DEVLINK_DEV" | jq "$resource_name"
  70. }
  71. devlink_resource_size_get()
  72. {
  73. local size=$(devlink_resource_get "$@" | jq '.["size_new"]')
  74. if [ "$size" == "null" ]; then
  75. devlink_resource_get "$@" | jq '.["size"]'
  76. else
  77. echo "$size"
  78. fi
  79. }
  80. devlink_resource_size_set()
  81. {
  82. local new_size=$1
  83. local path
  84. shift
  85. path=$(devlink_resource_names_to_path "$@")
  86. devlink resource set "$DEVLINK_DEV" path "$path" size "$new_size"
  87. check_err $? "Failed setting path $path to size $size"
  88. }
  89. devlink_resource_occ_get()
  90. {
  91. devlink_resource_get "$@" | jq '.["occ"]'
  92. }
  93. devlink_reload()
  94. {
  95. local still_pending
  96. devlink dev reload "$DEVLINK_DEV" &> /dev/null
  97. check_err $? "Failed reload"
  98. still_pending=$(devlink resource show "$DEVLINK_DEV" | \
  99. grep -c "size_new")
  100. check_err $still_pending "Failed reload - There are still unset sizes"
  101. udevadm settle
  102. }
  103. declare -A DEVLINK_ORIG
  104. # Changing pool type from static to dynamic causes reinterpretation of threshold
  105. # values. They therefore need to be saved before pool type is changed, then the
  106. # pool type can be changed, and then the new values need to be set up. Therefore
  107. # instead of saving the current state implicitly in the _set call, provide
  108. # functions for all three primitives: save, set, and restore.
  109. devlink_port_pool_threshold()
  110. {
  111. local port=$1; shift
  112. local pool=$1; shift
  113. devlink sb port pool show $port pool $pool -j \
  114. | jq '.port_pool."'"$port"'"[].threshold'
  115. }
  116. devlink_port_pool_th_save()
  117. {
  118. local port=$1; shift
  119. local pool=$1; shift
  120. local key="port_pool($port,$pool).threshold"
  121. DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool)
  122. }
  123. devlink_port_pool_th_set()
  124. {
  125. local port=$1; shift
  126. local pool=$1; shift
  127. local th=$1; shift
  128. devlink sb port pool set $port pool $pool th $th
  129. }
  130. devlink_port_pool_th_restore()
  131. {
  132. local port=$1; shift
  133. local pool=$1; shift
  134. local key="port_pool($port,$pool).threshold"
  135. local -a orig=(${DEVLINK_ORIG[$key]})
  136. if [[ -z $orig ]]; then
  137. echo "WARNING: Mismatched devlink_port_pool_th_restore"
  138. else
  139. devlink sb port pool set $port pool $pool th $orig
  140. fi
  141. }
  142. devlink_pool_size_thtype()
  143. {
  144. local pool=$1; shift
  145. devlink sb pool show "$DEVLINK_DEV" pool $pool -j \
  146. | jq -r '.pool[][] | (.size, .thtype)'
  147. }
  148. devlink_pool_size_thtype_save()
  149. {
  150. local pool=$1; shift
  151. local key="pool($pool).size_thtype"
  152. DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool)
  153. }
  154. devlink_pool_size_thtype_set()
  155. {
  156. local pool=$1; shift
  157. local thtype=$1; shift
  158. local size=$1; shift
  159. devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype
  160. }
  161. devlink_pool_size_thtype_restore()
  162. {
  163. local pool=$1; shift
  164. local key="pool($pool).size_thtype"
  165. local -a orig=(${DEVLINK_ORIG[$key]})
  166. if [[ -z ${orig[0]} ]]; then
  167. echo "WARNING: Mismatched devlink_pool_size_thtype_restore"
  168. else
  169. devlink sb pool set "$DEVLINK_DEV" pool $pool \
  170. size ${orig[0]} thtype ${orig[1]}
  171. fi
  172. }
  173. devlink_tc_bind_pool_th()
  174. {
  175. local port=$1; shift
  176. local tc=$1; shift
  177. local dir=$1; shift
  178. devlink sb tc bind show $port tc $tc type $dir -j \
  179. | jq -r '.tc_bind[][] | (.pool, .threshold)'
  180. }
  181. devlink_tc_bind_pool_th_save()
  182. {
  183. local port=$1; shift
  184. local tc=$1; shift
  185. local dir=$1; shift
  186. local key="tc_bind($port,$dir,$tc).pool_th"
  187. DEVLINK_ORIG[$key]=$(devlink_tc_bind_pool_th $port $tc $dir)
  188. }
  189. devlink_tc_bind_pool_th_set()
  190. {
  191. local port=$1; shift
  192. local tc=$1; shift
  193. local dir=$1; shift
  194. local pool=$1; shift
  195. local th=$1; shift
  196. devlink sb tc bind set $port tc $tc type $dir pool $pool th $th
  197. }
  198. devlink_tc_bind_pool_th_restore()
  199. {
  200. local port=$1; shift
  201. local tc=$1; shift
  202. local dir=$1; shift
  203. local key="tc_bind($port,$dir,$tc).pool_th"
  204. local -a orig=(${DEVLINK_ORIG[$key]})
  205. if [[ -z ${orig[0]} ]]; then
  206. echo "WARNING: Mismatched devlink_tc_bind_pool_th_restore"
  207. else
  208. devlink sb tc bind set $port tc $tc type $dir \
  209. pool ${orig[0]} th ${orig[1]}
  210. fi
  211. }
  212. devlink_traps_num_get()
  213. {
  214. devlink -j trap | jq '.[]["'$DEVLINK_DEV'"] | length'
  215. }
  216. devlink_traps_get()
  217. {
  218. devlink -j trap | jq -r '.[]["'$DEVLINK_DEV'"][].name'
  219. }
  220. devlink_trap_type_get()
  221. {
  222. local trap_name=$1; shift
  223. devlink -j trap show $DEVLINK_DEV trap $trap_name \
  224. | jq -r '.[][][].type'
  225. }
  226. devlink_trap_action_set()
  227. {
  228. local trap_name=$1; shift
  229. local action=$1; shift
  230. # Pipe output to /dev/null to avoid expected warnings.
  231. devlink trap set $DEVLINK_DEV trap $trap_name \
  232. action $action &> /dev/null
  233. }
  234. devlink_trap_action_get()
  235. {
  236. local trap_name=$1; shift
  237. devlink -j trap show $DEVLINK_DEV trap $trap_name \
  238. | jq -r '.[][][].action'
  239. }
  240. devlink_trap_group_get()
  241. {
  242. devlink -j trap show $DEVLINK_DEV trap $trap_name \
  243. | jq -r '.[][][].group'
  244. }
  245. devlink_trap_metadata_test()
  246. {
  247. local trap_name=$1; shift
  248. local metadata=$1; shift
  249. devlink -jv trap show $DEVLINK_DEV trap $trap_name \
  250. | jq -e '.[][][].metadata | contains(["'$metadata'"])' \
  251. &> /dev/null
  252. }
  253. devlink_trap_rx_packets_get()
  254. {
  255. local trap_name=$1; shift
  256. devlink -js trap show $DEVLINK_DEV trap $trap_name \
  257. | jq '.[][][]["stats"]["rx"]["packets"]'
  258. }
  259. devlink_trap_rx_bytes_get()
  260. {
  261. local trap_name=$1; shift
  262. devlink -js trap show $DEVLINK_DEV trap $trap_name \
  263. | jq '.[][][]["stats"]["rx"]["bytes"]'
  264. }
  265. devlink_trap_drop_packets_get()
  266. {
  267. local trap_name=$1; shift
  268. devlink -js trap show $DEVLINK_DEV trap $trap_name \
  269. | jq '.[][][]["stats"]["rx"]["dropped"]'
  270. }
  271. devlink_trap_stats_idle_test()
  272. {
  273. local trap_name=$1; shift
  274. local t0_packets t0_bytes
  275. local t1_packets t1_bytes
  276. t0_packets=$(devlink_trap_rx_packets_get $trap_name)
  277. t0_bytes=$(devlink_trap_rx_bytes_get $trap_name)
  278. sleep 1
  279. t1_packets=$(devlink_trap_rx_packets_get $trap_name)
  280. t1_bytes=$(devlink_trap_rx_bytes_get $trap_name)
  281. if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
  282. return 0
  283. else
  284. return 1
  285. fi
  286. }
  287. devlink_trap_drop_stats_idle_test()
  288. {
  289. local trap_name=$1; shift
  290. local t0_packets t0_bytes
  291. t0_packets=$(devlink_trap_drop_packets_get $trap_name)
  292. sleep 1
  293. t1_packets=$(devlink_trap_drop_packets_get $trap_name)
  294. if [[ $t0_packets -eq $t1_packets ]]; then
  295. return 0
  296. else
  297. return 1
  298. fi
  299. }
  300. devlink_traps_enable_all()
  301. {
  302. local trap_name
  303. for trap_name in $(devlink_traps_get); do
  304. devlink_trap_action_set $trap_name "trap"
  305. done
  306. }
  307. devlink_traps_disable_all()
  308. {
  309. for trap_name in $(devlink_traps_get); do
  310. devlink_trap_action_set $trap_name "drop"
  311. done
  312. }
  313. devlink_trap_groups_get()
  314. {
  315. devlink -j trap group | jq -r '.[]["'$DEVLINK_DEV'"][].name'
  316. }
  317. devlink_trap_group_action_set()
  318. {
  319. local group_name=$1; shift
  320. local action=$1; shift
  321. # Pipe output to /dev/null to avoid expected warnings.
  322. devlink trap group set $DEVLINK_DEV group $group_name action $action \
  323. &> /dev/null
  324. }
  325. devlink_trap_group_rx_packets_get()
  326. {
  327. local group_name=$1; shift
  328. devlink -js trap group show $DEVLINK_DEV group $group_name \
  329. | jq '.[][][]["stats"]["rx"]["packets"]'
  330. }
  331. devlink_trap_group_rx_bytes_get()
  332. {
  333. local group_name=$1; shift
  334. devlink -js trap group show $DEVLINK_DEV group $group_name \
  335. | jq '.[][][]["stats"]["rx"]["bytes"]'
  336. }
  337. devlink_trap_group_stats_idle_test()
  338. {
  339. local group_name=$1; shift
  340. local t0_packets t0_bytes
  341. local t1_packets t1_bytes
  342. t0_packets=$(devlink_trap_group_rx_packets_get $group_name)
  343. t0_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
  344. sleep 1
  345. t1_packets=$(devlink_trap_group_rx_packets_get $group_name)
  346. t1_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
  347. if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
  348. return 0
  349. else
  350. return 1
  351. fi
  352. }
  353. devlink_trap_exception_test()
  354. {
  355. local trap_name=$1; shift
  356. local group_name
  357. group_name=$(devlink_trap_group_get $trap_name)
  358. devlink_trap_stats_idle_test $trap_name
  359. check_fail $? "Trap stats idle when packets should have been trapped"
  360. devlink_trap_group_stats_idle_test $group_name
  361. check_fail $? "Trap group idle when packets should have been trapped"
  362. }
  363. devlink_trap_drop_test()
  364. {
  365. local trap_name=$1; shift
  366. local dev=$1; shift
  367. local handle=$1; shift
  368. local group_name
  369. group_name=$(devlink_trap_group_get $trap_name)
  370. # This is the common part of all the tests. It checks that stats are
  371. # initially idle, then non-idle after changing the trap action and
  372. # finally idle again. It also makes sure the packets are dropped and
  373. # never forwarded.
  374. devlink_trap_stats_idle_test $trap_name
  375. check_err $? "Trap stats not idle with initial drop action"
  376. devlink_trap_group_stats_idle_test $group_name
  377. check_err $? "Trap group stats not idle with initial drop action"
  378. devlink_trap_action_set $trap_name "trap"
  379. devlink_trap_stats_idle_test $trap_name
  380. check_fail $? "Trap stats idle after setting action to trap"
  381. devlink_trap_group_stats_idle_test $group_name
  382. check_fail $? "Trap group stats idle after setting action to trap"
  383. devlink_trap_action_set $trap_name "drop"
  384. devlink_trap_stats_idle_test $trap_name
  385. check_err $? "Trap stats not idle after setting action to drop"
  386. devlink_trap_group_stats_idle_test $group_name
  387. check_err $? "Trap group stats not idle after setting action to drop"
  388. tc_check_packets "dev $dev egress" $handle 0
  389. check_err $? "Packets were not dropped"
  390. }
  391. devlink_trap_drop_cleanup()
  392. {
  393. local mz_pid=$1; shift
  394. local dev=$1; shift
  395. local proto=$1; shift
  396. local pref=$1; shift
  397. local handle=$1; shift
  398. kill $mz_pid && wait $mz_pid &> /dev/null
  399. tc filter del dev $dev egress protocol $proto pref $pref handle $handle flower
  400. }
  401. devlink_trap_stats_check()
  402. {
  403. local trap_name=$1; shift
  404. local send_one="$@"
  405. local t0_packets
  406. local t1_packets
  407. t0_packets=$(devlink_trap_rx_packets_get $trap_name)
  408. $send_one && sleep 1
  409. t1_packets=$(devlink_trap_rx_packets_get $trap_name)
  410. [[ $t1_packets -ne $t0_packets ]]
  411. }
  412. devlink_trap_stats_test()
  413. {
  414. local test_name=$1; shift
  415. RET=0
  416. devlink_trap_stats_check "$@"
  417. check_err $? "Trap stats did not increase"
  418. log_test "$test_name"
  419. }
  420. devlink_trap_policers_num_get()
  421. {
  422. devlink -j -p trap policer show | jq '.[]["'$DEVLINK_DEV'"] | length'
  423. }
  424. devlink_trap_policer_rate_get()
  425. {
  426. local policer_id=$1; shift
  427. devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \
  428. | jq '.[][][]["rate"]'
  429. }
  430. devlink_trap_policer_burst_get()
  431. {
  432. local policer_id=$1; shift
  433. devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \
  434. | jq '.[][][]["burst"]'
  435. }
  436. devlink_trap_policer_rx_dropped_get()
  437. {
  438. local policer_id=$1; shift
  439. devlink -j -p -s trap policer show $DEVLINK_DEV policer $policer_id \
  440. | jq '.[][][]["stats"]["rx"]["dropped"]'
  441. }
  442. devlink_trap_group_policer_get()
  443. {
  444. local group_name=$1; shift
  445. devlink -j -p trap group show $DEVLINK_DEV group $group_name \
  446. | jq '.[][][]["policer"]'
  447. }
  448. devlink_port_by_netdev()
  449. {
  450. local if_name=$1
  451. devlink -j port show $if_name | jq -e '.[] | keys' | jq -r '.[]'
  452. }
  453. devlink_cpu_port_get()
  454. {
  455. local cpu_dl_port_num=$(devlink port list | grep "$DEVLINK_DEV" |
  456. grep cpu | cut -d/ -f3 | cut -d: -f1 |
  457. sed -n '1p')
  458. echo "$DEVLINK_DEV/$cpu_dl_port_num"
  459. }
  460. devlink_cell_size_get()
  461. {
  462. devlink sb pool show "$DEVLINK_DEV" pool 0 -j \
  463. | jq '.pool[][].cell_size'
  464. }
  465. devlink_pool_size_get()
  466. {
  467. devlink sb show "$DEVLINK_DEV" -j | jq '.[][][]["size"]'
  468. }