test_ynl_cli.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Test YNL CLI functionality
  4. # Load KTAP test helpers
  5. KSELFTEST_KTAP_HELPERS="$(dirname "$(realpath "$0")")/../../../testing/selftests/kselftest/ktap_helpers.sh"
  6. # shellcheck source=../../../testing/selftests/kselftest/ktap_helpers.sh
  7. source "$KSELFTEST_KTAP_HELPERS"
  8. # Default ynl path for direct execution, can be overridden by make install
  9. ynl="../pyynl/cli.py"
  10. readonly NSIM_ID="1338"
  11. readonly NSIM_DEV_NAME="nsim${NSIM_ID}"
  12. readonly VETH_A="veth_a"
  13. readonly VETH_B="veth_b"
  14. testns="ynl-$(mktemp -u XXXXXX)"
  15. TESTS_NO=0
  16. # Test listing available families
  17. cli_list_families()
  18. {
  19. if $ynl --list-families &>/dev/null; then
  20. ktap_test_pass "YNL CLI list families"
  21. else
  22. ktap_test_fail "YNL CLI list families"
  23. fi
  24. }
  25. TESTS_NO=$((TESTS_NO + 1))
  26. # Test netdev family operations (dev-get, queue-get)
  27. cli_netdev_ops()
  28. {
  29. local dev_output
  30. local ifindex
  31. ifindex=$(ip netns exec "$testns" cat /sys/class/net/"$NSIM_DEV_NAME"/ifindex 2>/dev/null)
  32. dev_output=$(ip netns exec "$testns" $ynl --family netdev \
  33. --do dev-get --json "{\"ifindex\": $ifindex}" 2>/dev/null)
  34. if ! echo "$dev_output" | grep -q "ifindex"; then
  35. ktap_test_fail "YNL CLI netdev operations (netdev dev-get output missing ifindex)"
  36. return
  37. fi
  38. if ! ip netns exec "$testns" $ynl --family netdev \
  39. --dump queue-get --json "{\"ifindex\": $ifindex}" &>/dev/null; then
  40. ktap_test_fail "YNL CLI netdev operations (failed to get netdev queue info)"
  41. return
  42. fi
  43. ktap_test_pass "YNL CLI netdev operations"
  44. }
  45. TESTS_NO=$((TESTS_NO + 1))
  46. # Test ethtool family operations (rings-get, linkinfo-get)
  47. cli_ethtool_ops()
  48. {
  49. local rings_output
  50. local linkinfo_output
  51. rings_output=$(ip netns exec "$testns" $ynl --family ethtool \
  52. --do rings-get --json "{\"header\": {\"dev-name\": \"$NSIM_DEV_NAME\"}}" 2>/dev/null)
  53. if ! echo "$rings_output" | grep -q "header"; then
  54. ktap_test_fail "YNL CLI ethtool operations (ethtool rings-get output missing header)"
  55. return
  56. fi
  57. linkinfo_output=$(ip netns exec "$testns" $ynl --family ethtool \
  58. --do linkinfo-get --json "{\"header\": {\"dev-name\": \"$VETH_A\"}}" 2>/dev/null)
  59. if ! echo "$linkinfo_output" | grep -q "header"; then
  60. ktap_test_fail "YNL CLI ethtool operations (ethtool linkinfo-get output missing header)"
  61. return
  62. fi
  63. ktap_test_pass "YNL CLI ethtool operations"
  64. }
  65. TESTS_NO=$((TESTS_NO + 1))
  66. # Test rt-route family operations
  67. cli_rt_route_ops()
  68. {
  69. local ifindex
  70. if ! $ynl --list-families 2>/dev/null | grep -q "rt-route"; then
  71. ktap_test_skip "YNL CLI rt-route operations (rt-route family not available)"
  72. return
  73. fi
  74. ifindex=$(ip netns exec "$testns" cat /sys/class/net/"$NSIM_DEV_NAME"/ifindex 2>/dev/null)
  75. # Add route: 192.0.2.0/24 dev $dev scope link
  76. if ! ip netns exec "$testns" $ynl --family rt-route --do newroute --create \
  77. --json "{\"dst\": \"192.0.2.0\", \"oif\": $ifindex, \"rtm-dst-len\": 24, \"rtm-family\": 2, \"rtm-scope\": 253, \"rtm-type\": 1, \"rtm-protocol\": 3, \"rtm-table\": 254}" &>/dev/null; then
  78. ktap_test_fail "YNL CLI rt-route operations (failed to add route)"
  79. return
  80. fi
  81. local route_output
  82. route_output=$(ip netns exec "$testns" $ynl --family rt-route --dump getroute 2>/dev/null)
  83. if echo "$route_output" | grep -q "192.0.2.0"; then
  84. ktap_test_pass "YNL CLI rt-route operations"
  85. else
  86. ktap_test_fail "YNL CLI rt-route operations (failed to verify route)"
  87. fi
  88. ip netns exec "$testns" $ynl --family rt-route --do delroute \
  89. --json "{\"dst\": \"192.0.2.0\", \"oif\": $ifindex, \"rtm-dst-len\": 24, \"rtm-family\": 2, \"rtm-scope\": 253, \"rtm-type\": 1, \"rtm-protocol\": 3, \"rtm-table\": 254}" &>/dev/null
  90. }
  91. TESTS_NO=$((TESTS_NO + 1))
  92. # Test rt-addr family operations
  93. cli_rt_addr_ops()
  94. {
  95. local ifindex
  96. if ! $ynl --list-families 2>/dev/null | grep -q "rt-addr"; then
  97. ktap_test_skip "YNL CLI rt-addr operations (rt-addr family not available)"
  98. return
  99. fi
  100. ifindex=$(ip netns exec "$testns" cat /sys/class/net/"$NSIM_DEV_NAME"/ifindex 2>/dev/null)
  101. if ! ip netns exec "$testns" $ynl --family rt-addr --do newaddr \
  102. --json "{\"ifa-index\": $ifindex, \"local\": \"192.0.2.100\", \"ifa-prefixlen\": 24, \"ifa-family\": 2}" &>/dev/null; then
  103. ktap_test_fail "YNL CLI rt-addr operations (failed to add address)"
  104. return
  105. fi
  106. local addr_output
  107. addr_output=$(ip netns exec "$testns" $ynl --family rt-addr --dump getaddr 2>/dev/null)
  108. if echo "$addr_output" | grep -q "192.0.2.100"; then
  109. ktap_test_pass "YNL CLI rt-addr operations"
  110. else
  111. ktap_test_fail "YNL CLI rt-addr operations (failed to verify address)"
  112. fi
  113. ip netns exec "$testns" $ynl --family rt-addr --do deladdr \
  114. --json "{\"ifa-index\": $ifindex, \"local\": \"192.0.2.100\", \"ifa-prefixlen\": 24, \"ifa-family\": 2}" &>/dev/null
  115. }
  116. TESTS_NO=$((TESTS_NO + 1))
  117. # Test rt-link family operations
  118. cli_rt_link_ops()
  119. {
  120. if ! $ynl --list-families 2>/dev/null | grep -q "rt-link"; then
  121. ktap_test_skip "YNL CLI rt-link operations (rt-link family not available)"
  122. return
  123. fi
  124. if ! ip netns exec "$testns" $ynl --family rt-link --do newlink --create \
  125. --json "{\"ifname\": \"dummy0\", \"linkinfo\": {\"kind\": \"dummy\"}}" &>/dev/null; then
  126. ktap_test_fail "YNL CLI rt-link operations (failed to add link)"
  127. return
  128. fi
  129. local link_output
  130. link_output=$(ip netns exec "$testns" $ynl --family rt-link --dump getlink 2>/dev/null)
  131. if echo "$link_output" | grep -q "$NSIM_DEV_NAME" && echo "$link_output" | grep -q "dummy0"; then
  132. ktap_test_pass "YNL CLI rt-link operations"
  133. else
  134. ktap_test_fail "YNL CLI rt-link operations (failed to verify link)"
  135. fi
  136. ip netns exec "$testns" $ynl --family rt-link --do dellink \
  137. --json "{\"ifname\": \"dummy0\"}" &>/dev/null
  138. }
  139. TESTS_NO=$((TESTS_NO + 1))
  140. # Test rt-neigh family operations
  141. cli_rt_neigh_ops()
  142. {
  143. local ifindex
  144. if ! $ynl --list-families 2>/dev/null | grep -q "rt-neigh"; then
  145. ktap_test_skip "YNL CLI rt-neigh operations (rt-neigh family not available)"
  146. return
  147. fi
  148. ifindex=$(ip netns exec "$testns" cat /sys/class/net/"$NSIM_DEV_NAME"/ifindex 2>/dev/null)
  149. # Add neighbor: 192.0.2.1 dev nsim1338 lladdr 11:22:33:44:55:66 PERMANENT
  150. if ! ip netns exec "$testns" $ynl --family rt-neigh --do newneigh --create \
  151. --json "{\"ndm-ifindex\": $ifindex, \"dst\": \"192.0.2.1\", \"lladdr\": \"11:22:33:44:55:66\", \"ndm-family\": 2, \"ndm-state\": 128}" &>/dev/null; then
  152. ktap_test_fail "YNL CLI rt-neigh operations (failed to add neighbor)"
  153. fi
  154. local neigh_output
  155. neigh_output=$(ip netns exec "$testns" $ynl --family rt-neigh --dump getneigh 2>/dev/null)
  156. if echo "$neigh_output" | grep -q "192.0.2.1"; then
  157. ktap_test_pass "YNL CLI rt-neigh operations"
  158. else
  159. ktap_test_fail "YNL CLI rt-neigh operations (failed to verify neighbor)"
  160. fi
  161. ip netns exec "$testns" $ynl --family rt-neigh --do delneigh \
  162. --json "{\"ndm-ifindex\": $ifindex, \"dst\": \"192.0.2.1\", \"lladdr\": \"11:22:33:44:55:66\", \"ndm-family\": 2}" &>/dev/null
  163. }
  164. TESTS_NO=$((TESTS_NO + 1))
  165. # Test rt-rule family operations
  166. cli_rt_rule_ops()
  167. {
  168. if ! $ynl --list-families 2>/dev/null | grep -q "rt-rule"; then
  169. ktap_test_skip "YNL CLI rt-rule operations (rt-rule family not available)"
  170. return
  171. fi
  172. # Add rule: from 192.0.2.0/24 lookup 100 none
  173. if ! ip netns exec "$testns" $ynl --family rt-rule --do newrule \
  174. --json "{\"family\": 2, \"src-len\": 24, \"src\": \"192.0.2.0\", \"table\": 100}" &>/dev/null; then
  175. ktap_test_fail "YNL CLI rt-rule operations (failed to add rule)"
  176. return
  177. fi
  178. local rule_output
  179. rule_output=$(ip netns exec "$testns" $ynl --family rt-rule --dump getrule 2>/dev/null)
  180. if echo "$rule_output" | grep -q "192.0.2.0"; then
  181. ktap_test_pass "YNL CLI rt-rule operations"
  182. else
  183. ktap_test_fail "YNL CLI rt-rule operations (failed to verify rule)"
  184. fi
  185. ip netns exec "$testns" $ynl --family rt-rule --do delrule \
  186. --json "{\"family\": 2, \"src-len\": 24, \"src\": \"192.0.2.0\", \"table\": 100}" &>/dev/null
  187. }
  188. TESTS_NO=$((TESTS_NO + 1))
  189. # Test nlctrl family operations
  190. cli_nlctrl_ops()
  191. {
  192. local family_output
  193. if ! family_output=$($ynl --family nlctrl \
  194. --do getfamily --json "{\"family-name\": \"netdev\"}" 2>/dev/null); then
  195. ktap_test_fail "YNL CLI nlctrl getfamily (failed to get nlctrl family info)"
  196. return
  197. fi
  198. if ! echo "$family_output" | grep -q "family-name"; then
  199. ktap_test_fail "YNL CLI nlctrl getfamily (nlctrl getfamily output missing family-name)"
  200. return
  201. fi
  202. if ! echo "$family_output" | grep -q "family-id"; then
  203. ktap_test_fail "YNL CLI nlctrl getfamily (nlctrl getfamily output missing family-id)"
  204. return
  205. fi
  206. ktap_test_pass "YNL CLI nlctrl getfamily"
  207. }
  208. TESTS_NO=$((TESTS_NO + 1))
  209. setup()
  210. {
  211. modprobe netdevsim &> /dev/null
  212. if ! [ -f /sys/bus/netdevsim/new_device ]; then
  213. ktap_skip_all "netdevsim module not available"
  214. exit "$KSFT_SKIP"
  215. fi
  216. if ! ip netns add "$testns" 2>/dev/null; then
  217. ktap_skip_all "failed to create test namespace"
  218. exit "$KSFT_SKIP"
  219. fi
  220. echo "$NSIM_ID 1" | ip netns exec "$testns" tee /sys/bus/netdevsim/new_device >/dev/null 2>&1 || {
  221. ktap_skip_all "failed to create netdevsim device"
  222. exit "$KSFT_SKIP"
  223. }
  224. local dev
  225. dev=$(ip netns exec "$testns" ls /sys/bus/netdevsim/devices/netdevsim$NSIM_ID/net 2>/dev/null | head -1)
  226. if [[ -z "$dev" ]]; then
  227. ktap_skip_all "failed to find netdevsim device"
  228. exit "$KSFT_SKIP"
  229. fi
  230. ip -netns "$testns" link set dev "$dev" name "$NSIM_DEV_NAME" 2>/dev/null || {
  231. ktap_skip_all "failed to rename netdevsim device"
  232. exit "$KSFT_SKIP"
  233. }
  234. ip -netns "$testns" link set dev "$NSIM_DEV_NAME" up 2>/dev/null
  235. if ! ip -n "$testns" link add "$VETH_A" type veth peer name "$VETH_B" 2>/dev/null; then
  236. ktap_skip_all "failed to create veth pair"
  237. exit "$KSFT_SKIP"
  238. fi
  239. ip -n "$testns" link set "$VETH_A" up 2>/dev/null
  240. ip -n "$testns" link set "$VETH_B" up 2>/dev/null
  241. }
  242. cleanup()
  243. {
  244. ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true
  245. ip netns del "$testns" 2>/dev/null || true
  246. }
  247. # Check if ynl command is available
  248. if ! command -v $ynl &>/dev/null && [[ ! -x $ynl ]]; then
  249. ktap_skip_all "ynl command not found: $ynl"
  250. exit "$KSFT_SKIP"
  251. fi
  252. trap cleanup EXIT
  253. ktap_print_header
  254. setup
  255. ktap_set_plan "${TESTS_NO}"
  256. cli_list_families
  257. cli_netdev_ops
  258. cli_ethtool_ops
  259. cli_rt_route_ops
  260. cli_rt_addr_ops
  261. cli_rt_link_ops
  262. cli_rt_neigh_ops
  263. cli_rt_rule_ops
  264. cli_nlctrl_ops
  265. ktap_finished