tst_net.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2014-2017 Oracle and/or its affiliates. All Rights Reserved.
  4. # Copyright (c) 2016-2019 Petr Vorel <pvorel@suse.cz>
  5. # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
  6. [ -n "$TST_LIB_NET_LOADED" ] && return 0
  7. TST_LIB_NET_LOADED=1
  8. TST_OPTS="6$TST_OPTS"
  9. TST_PARSE_ARGS_CALLER="$TST_PARSE_ARGS"
  10. TST_PARSE_ARGS="tst_net_parse_args"
  11. TST_USAGE_CALLER="$TST_USAGE"
  12. TST_USAGE="tst_net_usage"
  13. TST_SETUP_CALLER="$TST_SETUP"
  14. TST_SETUP="tst_net_setup"
  15. # Blank for an IPV4 test; 6 for an IPV6 test.
  16. TST_IPV6=${TST_IPV6:-}
  17. TST_IPVER=${TST_IPV6:-4}
  18. tst_net_parse_args()
  19. {
  20. case $1 in
  21. 6) TST_IPV6=6 TST_IPVER=6;;
  22. *) [ "$TST_PARSE_ARGS_CALLER" ] && $TST_PARSE_ARGS_CALLER "$1" "$2";;
  23. esac
  24. }
  25. tst_net_read_opts()
  26. {
  27. local OPTIND
  28. while getopts ":$TST_OPTS" opt; do
  29. $TST_PARSE_ARGS "$opt" "$OPTARG"
  30. done
  31. }
  32. tst_net_usage()
  33. {
  34. if [ -n "$TST_USAGE_CALLER" ]; then
  35. $TST_USAGE_CALLER
  36. else
  37. echo "Usage: $0 [-6]"
  38. echo "OPTIONS"
  39. fi
  40. echo "-6 IPv6 tests"
  41. }
  42. tst_net_remote_tmpdir()
  43. {
  44. [ "$TST_NEEDS_TMPDIR" = 1 ] || return 0
  45. [ -n "$TST_USE_LEGACY_API" ] && tst_tmpdir
  46. tst_rhost_run -c "mkdir -p $TST_TMPDIR"
  47. tst_rhost_run -c "chmod 777 $TST_TMPDIR"
  48. export TST_TMPDIR_RHOST=1
  49. }
  50. tst_net_setup()
  51. {
  52. tst_net_remote_tmpdir
  53. [ -n "$TST_SETUP_CALLER" ] && $TST_SETUP_CALLER
  54. if [ -z "$NS_ICMP_SENDER_DATA_MAXSIZE" ]; then
  55. if [ "$TST_IPV6" ]; then
  56. NS_ICMP_SENDER_DATA_MAXSIZE="$NS_ICMPV6_SENDER_DATA_MAXSIZE"
  57. else
  58. NS_ICMP_SENDER_DATA_MAXSIZE="$NS_ICMPV4_SENDER_DATA_MAXSIZE"
  59. fi
  60. fi
  61. }
  62. [ -n "$TST_USE_LEGACY_API" ] && . test.sh || . tst_test.sh
  63. if [ "$TST_PARSE_ARGS_CALLER" = "$TST_PARSE_ARGS" ]; then
  64. tst_res TWARN "TST_PARSE_ARGS_CALLER same as TST_PARSE_ARGS, unset it ($TST_PARSE_ARGS)"
  65. unset TST_PARSE_ARGS_CALLER
  66. fi
  67. if [ "$TST_SETUP_CALLER" = "$TST_SETUP" ]; then
  68. tst_res TWARN "TST_SETUP_CALLER same as TST_SETUP, unset it ($TST_SETUP)"
  69. unset TST_SETUP_CALLER
  70. fi
  71. if [ "$TST_USAGE_CALLER" = "$TST_USAGE" ]; then
  72. tst_res TWARN "TST_USAGE_CALLER same as TST_USAGE, unset it ($TST_USAGE)"
  73. unset TST_USAGE_CALLER
  74. fi
  75. if [ -n "$TST_USE_LEGACY_API" ]; then
  76. tst_net_read_opts "$@"
  77. fi
  78. # old vs. new API compatibility layer
  79. tst_res_()
  80. {
  81. [ -z "$TST_USE_LEGACY_API" ] && tst_res $@ || tst_resm $@
  82. }
  83. tst_brk_()
  84. {
  85. [ -z "$TST_USE_LEGACY_API" ] && tst_brk $@ || tst_brkm $@
  86. }
  87. init_ltp_netspace()
  88. {
  89. local pid
  90. if [ ! -f /var/run/netns/ltp_ns -a -z "$LTP_NETNS" ]; then
  91. tst_require_cmds ip
  92. tst_require_root
  93. ROD ip li add name ltp_ns_veth1 type veth peer name ltp_ns_veth2
  94. pid="$(ROD ns_create net,mnt)"
  95. mkdir -p /var/run/netns
  96. ROD ln -s /proc/$pid/ns/net /var/run/netns/ltp_ns
  97. ROD ns_exec $pid net,mnt mount --make-rprivate /sys
  98. ROD ns_exec $pid net,mnt mount -t sysfs none /sys
  99. ROD ns_ifmove ltp_ns_veth1 $pid
  100. ROD ns_exec $pid net,mnt ip li set lo up
  101. elif [ -n "$LTP_NETNS" ]; then
  102. tst_res_ TINFO "using not default LTP netns: '$LTP_NETNS'"
  103. fi
  104. LHOST_IFACES="${LHOST_IFACES:-ltp_ns_veth2}"
  105. RHOST_IFACES="${RHOST_IFACES:-ltp_ns_veth1}"
  106. export TST_INIT_NETNS="no"
  107. pid="$(echo $(readlink /var/run/netns/ltp_ns) | cut -f3 -d'/')"
  108. export LTP_NETNS="${LTP_NETNS:-ns_exec $pid net,mnt}"
  109. tst_restore_ipaddr
  110. tst_restore_ipaddr rhost
  111. }
  112. # Run command on remote host.
  113. # tst_rhost_run -c CMD [-b] [-s] [-u USER]
  114. # Options:
  115. # -b run in background
  116. # -c CMD specify command to run (this must be binary, not shell builtin/function)
  117. # -s safe option, if something goes wrong, will exit with TBROK
  118. # -u USER for ssh (default root)
  119. # RETURN: 0 on success, 1 on failure
  120. # TST_NET_RHOST_RUN_DEBUG=1 enables debugging
  121. tst_rhost_run()
  122. {
  123. local post_cmd=' || echo RTERR'
  124. local user="root"
  125. local ret=0
  126. local cmd out output pre_cmd rcmd sh_cmd safe use
  127. local OPTIND
  128. while getopts :bc:su: opt; do
  129. case "$opt" in
  130. b) [ "${TST_USE_NETNS:-}" ] && pre_cmd= || pre_cmd="nohup"
  131. post_cmd=" > /dev/null 2>&1 &"
  132. out="1> /dev/null"
  133. ;;
  134. c) cmd="$OPTARG" ;;
  135. s) safe=1 ;;
  136. u) user="$OPTARG" ;;
  137. *) tst_brk_ TBROK "tst_rhost_run: unknown option: $OPTARG" ;;
  138. esac
  139. done
  140. if [ -z "$cmd" ]; then
  141. [ "$safe" ] && \
  142. tst_brk_ TBROK "tst_rhost_run: command not defined"
  143. tst_res_ TWARN "tst_rhost_run: command not defined"
  144. return 1
  145. fi
  146. sh_cmd="$pre_cmd $cmd $post_cmd"
  147. if [ -n "${TST_USE_NETNS:-}" ]; then
  148. use="NETNS"
  149. rcmd="$LTP_NETNS sh -c"
  150. else
  151. tst_require_cmds ssh
  152. use="SSH"
  153. rcmd="ssh -nq $user@$RHOST"
  154. fi
  155. if [ "$TST_NET_RHOST_RUN_DEBUG" = 1 ]; then
  156. tst_res_ TINFO "tst_rhost_run: cmd: $cmd"
  157. tst_res_ TINFO "$use: $rcmd \"$sh_cmd\" $out 2>&1"
  158. fi
  159. output=$($rcmd "$sh_cmd" $out 2>&1 || echo 'RTERR')
  160. echo "$output" | grep -q 'RTERR$' && ret=1
  161. if [ $ret -eq 1 ]; then
  162. output=$(echo "$output" | sed 's/RTERR//')
  163. [ "$safe" ] && \
  164. tst_brk_ TBROK "'$cmd' failed on '$RHOST': '$output'"
  165. fi
  166. [ -z "$out" -a -n "$output" ] && echo "$output"
  167. return $ret
  168. }
  169. # Run command on both lhost and rhost.
  170. # tst_net_run [-s] [-l LPARAM] [-r RPARAM] [ -q ] CMD [ARG [ARG2]]
  171. # Options:
  172. # -l LPARAM: parameter passed to CMD in lhost
  173. # -r RPARAM: parameter passed to CMD in rhost
  174. # -q: quiet mode (suppress failure warnings)
  175. # CMD: command to run (this must be binary, not shell builtin/function due
  176. # tst_rhost_run() limitation)
  177. # RETURN: 0 on success, 1 on missing CMD or exit code on lhost or rhost
  178. tst_net_run()
  179. {
  180. local cmd
  181. local lparams
  182. local rparams
  183. local lsafe
  184. local rsafe
  185. local lret
  186. local rret
  187. local quiet
  188. local OPTIND
  189. while getopts l:qr:s opt; do
  190. case "$opt" in
  191. l) lparams="$OPTARG" ;;
  192. q) quiet=1 ;;
  193. r) rparams="$OPTARG" ;;
  194. s) lsafe="ROD"; rsafe="-s" ;;
  195. *) tst_brk_ TBROK "tst_net_run: unknown option: $OPTARG" ;;
  196. esac
  197. done
  198. shift $((OPTIND - 1))
  199. cmd="$1"
  200. shift
  201. if [ -z "$cmd" ]; then
  202. [ -n "$lsafe" ] && \
  203. tst_brk_ TBROK "tst_net_run: command not defined"
  204. tst_res_ TWARN "tst_net_run: command not defined"
  205. return 1
  206. fi
  207. $lsafe $cmd $lparams $@
  208. lret=$?
  209. tst_rhost_run $rsafe -c "$cmd $rparams $@"
  210. rret=$?
  211. if [ -z "$quiet" ]; then
  212. [ $lret -ne 0 ] && tst_res_ TWARN "tst_net_run: lhost command failed: $lret"
  213. [ $rret -ne 0 ] && tst_res_ TWARN "tst_net_run: rhost command failed: $rret"
  214. fi
  215. [ $lret -ne 0 ] && return $lret
  216. return $rret
  217. }
  218. EXPECT_RHOST_PASS()
  219. {
  220. local log="$TMPDIR/log.$$"
  221. tst_rhost_run -c "$*" > $log
  222. if [ $? -eq 0 ]; then
  223. tst_res_ TPASS "$* passed as expected"
  224. else
  225. tst_res_ TFAIL "$* failed unexpectedly"
  226. cat $log
  227. fi
  228. rm -f $log
  229. }
  230. EXPECT_RHOST_FAIL()
  231. {
  232. local log="$TMPDIR/log.$$"
  233. tst_rhost_run -c "$*" > $log
  234. if [ $? -ne 0 ]; then
  235. tst_res_ TPASS "$* failed as expected"
  236. else
  237. tst_res_ TFAIL "$* passed unexpectedly"
  238. cat $log
  239. fi
  240. rm -f $log
  241. }
  242. # Get test interface names for local/remote host.
  243. # tst_get_ifaces [TYPE]
  244. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  245. tst_get_ifaces()
  246. {
  247. local type="${1:-lhost}"
  248. if [ "$type" = "lhost" ]; then
  249. echo "$LHOST_IFACES"
  250. else
  251. echo "$RHOST_IFACES"
  252. fi
  253. }
  254. # Get count of test interfaces for local/remote host.
  255. tst_get_ifaces_cnt()
  256. {
  257. tst_require_cmds awk
  258. local type="${1:-lhost}"
  259. echo "$(tst_get_ifaces $type)" | awk '{print NF}'
  260. }
  261. # Get HW addresses from defined test interface names.
  262. # tst_get_hwaddrs [TYPE]
  263. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  264. tst_get_hwaddrs()
  265. {
  266. local type="${1:-lhost}"
  267. local addr=
  268. local list=
  269. for eth in $(tst_get_ifaces $type); do
  270. local addr_path="/sys/class/net/${eth}/address"
  271. case $type in
  272. lhost) addr=$(cat $addr_path) ;;
  273. rhost) addr=$(tst_rhost_run -s -c "cat $addr_path")
  274. esac
  275. [ -z "$list" ] && list="$addr" || list="$list $addr"
  276. done
  277. echo "$list"
  278. }
  279. # Get test HW address.
  280. # tst_hwaddr [TYPE] [LINK]
  281. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  282. # LINK: link number starting from 0. Default value is '0'.
  283. tst_hwaddr()
  284. {
  285. tst_require_cmds awk
  286. local type="${1:-lhost}"
  287. local link_num="${2:-0}"
  288. local hwaddrs=
  289. link_num=$(( $link_num + 1 ))
  290. [ "$type" = "lhost" ] && hwaddrs=$LHOST_HWADDRS || hwaddrs=$RHOST_HWADDRS
  291. echo "$hwaddrs" | awk '{ print $'"$link_num"' }'
  292. }
  293. # Get test interface name.
  294. # tst_iface [TYPE] [LINK]
  295. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  296. # LINK: link number starting from 0. Default value is '0'.
  297. tst_iface()
  298. {
  299. tst_require_cmds awk
  300. local type="${1:-lhost}"
  301. local link_num="${2:-0}"
  302. link_num="$(( $link_num + 1 ))"
  303. echo "$(tst_get_ifaces $type)" | awk '{ print $'"$link_num"' }'
  304. }
  305. # Get IP address
  306. # tst_ipaddr [TYPE]
  307. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  308. tst_ipaddr()
  309. {
  310. local type="${1:-lhost}"
  311. if [ "$TST_IPV6" ]; then
  312. [ "$type" = "lhost" ] && echo "$IPV6_LHOST" || echo "$IPV6_RHOST"
  313. else
  314. [ "$type" = "lhost" ] && echo "$IPV4_LHOST" || echo "$IPV4_RHOST"
  315. fi
  316. }
  317. # Get IP address of unused network, specified either counter and type
  318. # or by net and host.
  319. # counter mode:
  320. # tst_ipaddr_un [-h MIN,MAX] [-n MIN,MAX] [-p] [-c COUNTER] [TYPE]
  321. # net & host mode:
  322. # tst_ipaddr_un [-h MIN,MAX] [-n MIN,MAX] [-p] NET_ID [HOST_ID]
  323. #
  324. # TYPE: { lhost | rhost } (default: 'lhost')
  325. # NET_ID: integer or hex value of net (IPv4: 3rd octet <0,255>, IPv6: 3rd
  326. # hextet <0,65535>)
  327. # HOST_ID: integer or hex value of host (IPv4: 4th octet <0,255>, IPv6: the
  328. # last hextet <0, 65535>, default: 0)
  329. #
  330. # OPTIONS
  331. # -c COUNTER: integer value for counting HOST_ID and NET_ID (default: 1)
  332. #
  333. # -h: specify *host* address range (HOST_ID)
  334. # -h MIN,MAX or -h MIN or -h ,MAX
  335. #
  336. # -n: specify *network* address range (NET_ID)
  337. # -n MIN,MAX or -n MIN or -n ,MAX
  338. #
  339. # -p: print also prefix
  340. tst_ipaddr_un()
  341. {
  342. local default_max=255
  343. [ "$TST_IPV6" ] && default_max=65535
  344. local max_net_id=$default_max
  345. local min_net_id=0
  346. local counter host_id host_range is_counter max_host_id min_host_id net_id prefix tmp type
  347. local OPTIND
  348. while getopts "c:h:n:p" opt; do
  349. case $opt in
  350. c) counter="$OPTARG";;
  351. h)
  352. if echo $OPTARG | grep -q ','; then # 'min,max' or 'min,' or ',max'
  353. min_host_id="$(echo $OPTARG | cut -d, -f1)"
  354. max_host_id="$(echo $OPTARG | cut -d, -f2)"
  355. else # min
  356. min_host_id="$OPTARG"
  357. fi
  358. ;;
  359. n)
  360. if echo $OPTARG | grep -q ','; then # 'min,max' or 'min,' or ',max'
  361. min_net_id="$(echo $OPTARG | cut -d, -f1)"
  362. max_net_id="$(echo $OPTARG | cut -d, -f2)"
  363. else # min
  364. min_net_id="$OPTARG"
  365. fi
  366. ;;
  367. m)
  368. ! tst_is_int "$OPTARG" || [ "$OPTARG" -lt 0 ]|| [ "$OPTARG" -gt $max_net_id ] && \
  369. tst_brk TBROK "tst_ipaddr_un: -m must be integer <0,$max_net_id> ($OPTARG)"
  370. [ "$OPTARG" -gt $max_net_id ] && \
  371. tst_brk_ TBROK "tst_ipaddr_un: -m cannot be higher than $max_net_id ($OPTARG)"
  372. max_host_id="$OPTARG"
  373. ;;
  374. p) [ "$TST_IPV6" ] && prefix="/64" || prefix="/24";;
  375. esac
  376. done
  377. shift $(($OPTIND - 1))
  378. [ $# -eq 0 -o "$1" = "lhost" -o "$1" = "rhost" ] && is_counter=1
  379. if [ -z "$min_host_id" ]; then
  380. [ "$is_counter" ] && min_host_id=1 || min_host_id=0
  381. fi
  382. if [ -z "$max_host_id" ]; then
  383. [ "$is_counter" ] && max_host_id=$((default_max - 1)) || max_host_id=$default_max
  384. fi
  385. ! tst_is_int "$min_host_id" || ! tst_is_int "$max_host_id" || \
  386. [ $min_host_id -lt 0 -o $min_host_id -gt $default_max ] || \
  387. [ $max_host_id -lt 0 -o $max_host_id -gt $default_max ] && \
  388. tst_brk TBROK "tst_ipaddr_un: HOST_ID must be int in range <0,$default_max> ($min_host_id,$max_host_id)"
  389. ! tst_is_int "$min_net_id" || ! tst_is_int "$max_net_id" || \
  390. [ $min_net_id -lt 0 -o $min_net_id -gt $default_max ] || \
  391. [ $max_net_id -lt 0 -o $max_net_id -gt $default_max ] && \
  392. tst_brk TBROK "tst_ipaddr_un: NET_ID must be int in range <0,$default_max> ($min_net_id,$max_net_id)"
  393. [ $min_host_id -gt $max_host_id ] && \
  394. tst_brk TBROK "tst_ipaddr_un: max HOST_ID ($max_host_id) must be >= min HOST_ID ($min_host_id)"
  395. [ $min_net_id -gt $max_net_id ] && \
  396. tst_brk TBROK "tst_ipaddr_un: max NET_ID ($max_net_id) must be >= min NET_ID ($min_net_id)"
  397. # counter
  398. host_range=$((max_host_id - min_host_id + 1))
  399. if [ "$is_counter" ]; then
  400. [ -z "$counter" ] && counter=1
  401. [ $counter -lt 1 ] && counter=1
  402. type="${1:-lhost}"
  403. tmp=$((counter * 2))
  404. [ "$type" = "rhost" ] && tmp=$((tmp - 1))
  405. net_id=$(((tmp - 1) / host_range))
  406. host_id=$((tmp - net_id * host_range + min_host_id - 1))
  407. else # net_id & host_id
  408. net_id="$1"
  409. host_id="${2:-0}"
  410. if [ "$TST_IPV6" ]; then
  411. net_id=$(printf %d $net_id)
  412. host_id=$(printf %d $host_id)
  413. fi
  414. host_id=$((host_id % host_range + min_host_id))
  415. fi
  416. net_id=$((net_id % (max_net_id - min_net_id + 1) + min_net_id))
  417. if [ -z "$TST_IPV6" ]; then
  418. echo "${IPV4_NET16_UNUSED}.${net_id}.${host_id}${prefix}"
  419. return
  420. fi
  421. [ $host_id -gt 0 ] && host_id="$(printf %x $host_id)" || host_id=
  422. [ $net_id -gt 0 ] && net_id="$(printf %x $net_id)" || net_id=
  423. [ "$net_id" ] && net_id=":$net_id"
  424. echo "${IPV6_NET32_UNUSED}${net_id}::${host_id}${prefix}"
  425. }
  426. # tst_init_iface [TYPE] [LINK]
  427. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  428. # LINK: link number starting from 0. Default value is '0'.
  429. tst_init_iface()
  430. {
  431. local type="${1:-lhost}"
  432. local link_num="${2:-0}"
  433. local iface="$(tst_iface $type $link_num)"
  434. tst_res_ TINFO "initialize '$type' '$iface' interface"
  435. if [ "$type" = "lhost" ]; then
  436. if ip xfrm state 1>/dev/null 2>&1; then
  437. ip xfrm policy flush || return $?
  438. ip xfrm state flush || return $?
  439. fi
  440. ip link set $iface down || return $?
  441. ip route flush dev $iface || return $?
  442. ip addr flush dev $iface || return $?
  443. ip link set $iface up
  444. return $?
  445. fi
  446. if tst_rhost_run -c "ip xfrm state 1>/dev/null 2>&1"; then
  447. tst_rhost_run -c "ip xfrm policy flush" || return $?
  448. tst_rhost_run -c "ip xfrm state flush" || return $?
  449. fi
  450. tst_rhost_run -c "ip link set $iface down" || return $?
  451. tst_rhost_run -c "ip route flush dev $iface" || return $?
  452. tst_rhost_run -c "ip addr flush dev $iface" || return $?
  453. tst_rhost_run -c "ip link set $iface up"
  454. }
  455. # tst_add_ipaddr [TYPE] [LINK] [-a IP] [-d] [-q] [-s]
  456. # Options:
  457. # TYPE: { lhost | rhost }, default value is 'lhost'
  458. # LINK: link number starting from 0, default value is '0'
  459. # -a IP: IP address to be added, default value is
  460. # $(tst_ipaddr)/$IPV{4,6}_{L,R}PREFIX
  461. # -d: delete address instead of adding
  462. # -q: quiet mode (don't print info)
  463. # -s: safe option, if something goes wrong, will exit with TBROK
  464. tst_add_ipaddr()
  465. {
  466. local action="add"
  467. local addr dad lsafe mask quiet rsafe
  468. local OPTIND
  469. while getopts a:dqs opt; do
  470. case "$opt" in
  471. a) addr="$OPTARG" ;;
  472. d) action="del" ;;
  473. q) quiet=1 ;;
  474. s) lsafe="ROD"; rsafe="-s" ;;
  475. *) tst_brk TBROK "tst_add_ipaddr: unknown option: $OPTARG" ;;
  476. esac
  477. done
  478. shift $((OPTIND - 1))
  479. local type="${1:-lhost}"
  480. local link_num="${2:-0}"
  481. local iface=$(tst_iface $type $link_num)
  482. if [ "$TST_IPV6" ]; then
  483. dad="nodad"
  484. [ "$type" = "lhost" ] && mask=$IPV6_LPREFIX || mask=$IPV6_RPREFIX
  485. else
  486. [ "$type" = "lhost" ] && mask=$IPV4_LPREFIX || mask=$IPV4_RPREFIX
  487. fi
  488. [ -n "$addr" ] || addr="$(tst_ipaddr $type)"
  489. echo $addr | grep -q / || addr="$addr/$mask"
  490. if [ $type = "lhost" ]; then
  491. [ "$quiet" ] || tst_res_ TINFO "$action local addr $addr"
  492. $lsafe ip addr $action $addr dev $iface $dad
  493. return $?
  494. fi
  495. [ "$quiet" ] || tst_res_ TINFO "$action remote addr $addr"
  496. tst_rhost_run $rsafe -c "ip addr $action $addr dev $iface $dad"
  497. }
  498. # tst_del_ipaddr [ tst_add_ipaddr options ]
  499. # Delete IP address
  500. tst_del_ipaddr()
  501. {
  502. tst_add_ipaddr -d $@
  503. }
  504. # tst_restore_ipaddr [TYPE] [LINK]
  505. # Restore default ip addresses defined in network.sh
  506. # TYPE: { lhost | rhost }; Default value is 'lhost'.
  507. # LINK: link number starting from 0. Default value is '0'.
  508. tst_restore_ipaddr()
  509. {
  510. tst_require_cmds ip
  511. tst_require_root
  512. local type="${1:-lhost}"
  513. local link_num="${2:-0}"
  514. tst_init_iface $type $link_num || return $?
  515. local ret=0
  516. local backup_tst_ipv6=$TST_IPV6
  517. TST_IPV6= tst_add_ipaddr $type $link_num || ret=$?
  518. TST_IPV6=6 tst_add_ipaddr $type $link_num || ret=$?
  519. TST_IPV6=$backup_tst_ipv6
  520. return $ret
  521. }
  522. # tst_wait_ipv6_dad [LHOST_IFACE] [RHOST_IFACE]
  523. # wait for IPv6 DAD completion
  524. tst_wait_ipv6_dad()
  525. {
  526. local ret=
  527. local i=
  528. local iface_loc=${1:-$(tst_iface)}
  529. local iface_rmt=${2:-$(tst_iface rhost)}
  530. for i in $(seq 1 50); do
  531. ip a sh $iface_loc | grep -q tentative
  532. ret=$?
  533. tst_rhost_run -c "ip a sh $iface_rmt | grep -q tentative"
  534. [ $ret -ne 0 -a $? -ne 0 ] && return
  535. [ $(($i % 10)) -eq 0 ] && \
  536. tst_res_ TINFO "wait for IPv6 DAD completion $((i / 10))/5 sec"
  537. tst_sleep 100ms
  538. done
  539. }
  540. tst_dump_rhost_cmd()
  541. {
  542. tst_rhost_run -c "cat $TST_TMPDIR/netstress.log"
  543. }
  544. # Run network load test, see 'netstress -h' for option description
  545. tst_netload()
  546. {
  547. local rfile="tst_netload.res"
  548. local expect_res="pass"
  549. local ret=0
  550. local type="tcp"
  551. local hostopt=
  552. local setup_srchost=0
  553. # common options for client and server
  554. local cs_opts=
  555. local c_num="$TST_NETLOAD_CLN_NUMBER"
  556. local c_requests="$TST_NETLOAD_CLN_REQUESTS"
  557. local c_opts=
  558. # number of server replies after which TCP connection is closed
  559. local s_replies="${TST_NETLOAD_MAX_SRV_REPLIES:-500000}"
  560. local s_opts=
  561. local bind_to_device=1
  562. if [ ! "$TST_NEEDS_TMPDIR" = 1 ]; then
  563. tst_brk_ TBROK "Using tst_netload requires setting TST_NEEDS_TMPDIR=1"
  564. fi
  565. OPTIND=0
  566. while getopts :a:H:d:n:N:r:R:S:b:t:T:fFe:m:A:D: opt; do
  567. case "$opt" in
  568. a) c_num="$OPTARG" ;;
  569. H) c_opts="${c_opts}-H $OPTARG "
  570. hostopt="$OPTARG" ;;
  571. d) rfile="$OPTARG" ;;
  572. n) c_opts="${c_opts}-n $OPTARG " ;;
  573. N) c_opts="${c_opts}-N $OPTARG " ;;
  574. r) c_requests="$OPTARG" ;;
  575. A) c_opts="${c_opts}-A $OPTARG " ;;
  576. R) s_replies="$OPTARG" ;;
  577. S) c_opts="${c_opts}-S $OPTARG "
  578. setup_srchost=1 ;;
  579. b) cs_opts="${cs_opts}-b $OPTARG " ;;
  580. t) cs_opts="${cs_opts}-t $OPTARG " ;;
  581. T) cs_opts="${cs_opts}-T $OPTARG "
  582. type="$OPTARG" ;;
  583. m) cs_opts="${cs_opts}-m $OPTARG " ;;
  584. f) cs_opts="${cs_opts}-f " ;;
  585. F) cs_opts="${cs_opts}-F " ;;
  586. e) expect_res="$OPTARG" ;;
  587. D) [ "$TST_NETLOAD_BINDTODEVICE" = 1 ] && cs_opts="${cs_opts}-D $OPTARG "
  588. bind_to_device=0 ;;
  589. *) tst_brk_ TBROK "tst_netload: unknown option: $OPTARG" ;;
  590. esac
  591. done
  592. OPTIND=0
  593. [ "$setup_srchost" = 1 ] && s_opts="${s_opts}-S $hostopt "
  594. if [ "$bind_to_device" = 1 -a "$TST_NETLOAD_BINDTODEVICE" = 1 ]; then
  595. c_opts="${c_opts}-D $(tst_iface) "
  596. s_opts="${s_opts}-D $(tst_iface rhost) "
  597. fi
  598. local expect_ret=0
  599. [ "$expect_res" != "pass" ] && expect_ret=3
  600. tst_rhost_run -c "pkill -9 netstress\$"
  601. s_opts="${cs_opts}${s_opts}-R $s_replies -B $TST_TMPDIR"
  602. tst_res_ TINFO "run server 'netstress $s_opts'"
  603. tst_rhost_run -c "netstress $s_opts" > tst_netload.log 2>&1
  604. if [ $? -ne 0 ]; then
  605. cat tst_netload.log
  606. local ttype="TFAIL"
  607. grep -e 'CONF:' tst_netload.log && ttype="TCONF"
  608. tst_brk_ $ttype "server failed"
  609. fi
  610. local port=$(tst_rhost_run -s -c "cat $TST_TMPDIR/netstress_port")
  611. c_opts="${cs_opts}${c_opts}-a $c_num -r $c_requests -d $rfile -g $port"
  612. tst_res_ TINFO "run client 'netstress -l $c_opts'"
  613. netstress -l $c_opts > tst_netload.log 2>&1 || ret=$?
  614. tst_rhost_run -c "pkill -9 netstress\$"
  615. if [ "$expect_ret" -ne 0 ]; then
  616. if [ $((ret & expect_ret)) -ne 0 ]; then
  617. tst_res_ TPASS "netstress failed as expected"
  618. else
  619. tst_res_ TFAIL "expected '$expect_res' but ret: '$ret'"
  620. fi
  621. return $ret
  622. fi
  623. if [ "$ret" -ne 0 ]; then
  624. tst_dump_rhost_cmd
  625. cat tst_netload.log
  626. [ $((ret & 3)) -ne 0 ] && \
  627. tst_brk_ TFAIL "expected '$expect_res' but ret: '$ret'"
  628. [ $((ret & 32)) -ne 0 ] && \
  629. tst_brk_ TCONF "not supported configuration"
  630. [ $((ret & 4)) -ne 0 ] && \
  631. tst_res_ TWARN "netstress has warnings"
  632. fi
  633. if [ ! -f $rfile ]; then
  634. tst_dump_rhost_cmd
  635. cat tst_netload.log
  636. tst_brk_ TFAIL "can't read $rfile"
  637. fi
  638. tst_res_ TPASS "netstress passed, time spent '$(cat $rfile)' ms"
  639. return $ret
  640. }
  641. # tst_ping [IFACE] [DST ADDR] [MESSAGE SIZE ARRAY]
  642. # Check icmp connectivity
  643. # IFACE: source interface name or IP address
  644. # DST ADDR: destination IPv4 or IPv6 address
  645. # MESSAGE SIZE ARRAY: message size array
  646. tst_ping()
  647. {
  648. # The max number of ICMP echo request
  649. PING_MAX="${PING_MAX:-500}"
  650. local src_iface="${1:-$(tst_iface)}"
  651. local dst_addr="${2:-$(tst_ipaddr rhost)}"; shift $(( $# >= 2 ? 2 : 0 ))
  652. local msg_sizes="$*"
  653. local msg="tst_ping $dst_addr iface/saddr $src_iface, msg_size"
  654. local cmd="ping"
  655. local ret=0
  656. echo "$dst_addr" | grep -q ':' && cmd="ping6"
  657. tst_require_cmds $cmd
  658. # ping cmd use 56 as default message size
  659. for size in ${msg_sizes:-"56"}; do
  660. $cmd -I $src_iface -c $PING_MAX $dst_addr \
  661. -s $size -i 0 > /dev/null 2>&1
  662. ret=$?
  663. if [ $ret -eq 0 ]; then
  664. tst_res_ TPASS "$msg $size: pass"
  665. else
  666. tst_res_ TFAIL "$msg $size: fail"
  667. break
  668. fi
  669. done
  670. return $ret
  671. }
  672. # tst_icmp -t TIMEOUT -s MESSAGE_SIZE_ARRAY OPTS
  673. # TIMEOUT: total time for the test in seconds
  674. # OPTS: additional options for ns-icmpv4|6-sender tool
  675. tst_icmp()
  676. {
  677. local timeout=1
  678. local msg_sizes=56
  679. local opts=
  680. local num=
  681. local ret=0
  682. OPTIND=0
  683. while getopts :t:s: opt; do
  684. case "$opt" in
  685. t) timeout="$OPTARG" ;;
  686. s) msg_sizes="$OPTARG" ;;
  687. *) opts="-$OPTARG $opts" ;;
  688. esac
  689. done
  690. OPTIND=0
  691. local num=$(echo "$msg_sizes" | wc -w)
  692. timeout="$(($timeout / $num))"
  693. [ "$timeout" -eq 0 ] && timeout=1
  694. opts="${opts}-I $(tst_iface) -S $(tst_ipaddr) -D $(tst_ipaddr rhost) "
  695. opts="${opts}-M $(tst_hwaddr rhost) -t $timeout"
  696. for size in $msg_sizes; do
  697. ns-icmpv${TST_IPVER}_sender -s $size $opts
  698. ret=$?
  699. if [ $ret -eq 0 ]; then
  700. tst_res_ TPASS "'ns-icmpv${TST_IPVER}_sender -s $size $opts' pass"
  701. else
  702. tst_res_ TFAIL "'ns-icmpv${TST_IPVER}_sender -s $size $opts' fail"
  703. break
  704. fi
  705. done
  706. return $ret
  707. }
  708. # tst_set_sysctl NAME VALUE [safe]
  709. # It can handle netns case when sysctl not namespaceified.
  710. tst_set_sysctl()
  711. {
  712. local name="$1"
  713. local value="$2"
  714. local safe=
  715. [ "$3" = "safe" ] && safe="-s"
  716. local rparam=
  717. [ "$TST_USE_NETNS" = "yes" ] && rparam="-r '-e'"
  718. tst_net_run $safe $rparam "sysctl -q -w $name=$value"
  719. }
  720. tst_cleanup_rhost()
  721. {
  722. tst_rhost_run -c "rm -rf $TST_TMPDIR"
  723. }
  724. tst_default_max_pkt()
  725. {
  726. local mtu="$(cat /sys/class/net/$(tst_iface)/mtu)"
  727. echo "$((mtu + mtu / 10))"
  728. }
  729. # Management Link
  730. [ -z "$RHOST" ] && TST_USE_NETNS="yes"
  731. export RHOST="$RHOST"
  732. export PASSWD="${PASSWD:-}"
  733. # Don't use it in new tests, use tst_rhost_run() from tst_net.sh instead.
  734. export LTP_RSH="${LTP_RSH:-ssh -nq}"
  735. # Test Links
  736. # IPV{4,6}_{L,R}HOST can be set with or without prefix (e.g. IP or IP/prefix),
  737. # but if you use IP/prefix form, /prefix will be removed by tst_net_vars.
  738. IPV4_LHOST="${IPV4_LHOST:-10.0.0.2/24}"
  739. IPV4_RHOST="${IPV4_RHOST:-10.0.0.1/24}"
  740. IPV6_LHOST="${IPV6_LHOST:-fd00:1:1:1::2/64}"
  741. IPV6_RHOST="${IPV6_RHOST:-fd00:1:1:1::1/64}"
  742. # tst_net_ip_prefix
  743. # Strip prefix from IP address and save both If no prefix found sets
  744. # default prefix.
  745. #
  746. # tst_net_iface_prefix reads prefix and interface from rtnetlink.
  747. # If nothing found sets default prefix value.
  748. #
  749. # tst_net_vars exports environment variables related to test links and
  750. # networks that aren't reachable through the test links.
  751. #
  752. # For full list of exported environment variables see:
  753. # tst_net_ip_prefix -h
  754. # tst_net_iface_prefix -h
  755. # tst_net_vars -h
  756. if [ -z "$_tst_net_parse_variables" ]; then
  757. eval $(tst_net_ip_prefix $IPV4_LHOST || echo "exit $?")
  758. eval $(tst_net_ip_prefix -r $IPV4_RHOST || echo "exit $?")
  759. eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
  760. eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
  761. fi
  762. [ -n "$TST_USE_NETNS" -a "$TST_INIT_NETNS" != "no" ] && init_ltp_netspace
  763. if [ -z "$_tst_net_parse_variables" ]; then
  764. eval $(tst_net_iface_prefix $IPV4_LHOST || echo "exit $?")
  765. eval $(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV4_RHOST \
  766. || echo "exit $?")
  767. eval $(tst_net_iface_prefix $IPV6_LHOST || echo "exit $?")
  768. eval $(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST \
  769. || echo "exit $?")
  770. eval $(tst_net_vars $IPV4_LHOST/$IPV4_LPREFIX \
  771. $IPV4_RHOST/$IPV4_RPREFIX || echo "exit $?")
  772. eval $(tst_net_vars $IPV6_LHOST/$IPV6_LPREFIX \
  773. $IPV6_RHOST/$IPV6_RPREFIX || echo "exit $?")
  774. tst_res_ TINFO "Network config (local -- remote):"
  775. tst_res_ TINFO "$LHOST_IFACES -- $RHOST_IFACES"
  776. tst_res_ TINFO "$IPV4_LHOST/$IPV4_LPREFIX -- $IPV4_RHOST/$IPV4_RPREFIX"
  777. tst_res_ TINFO "$IPV6_LHOST/$IPV6_LPREFIX -- $IPV6_RHOST/$IPV6_RPREFIX"
  778. export _tst_net_parse_variables="yes"
  779. fi
  780. export TST_NET_DATAROOT="$LTPROOT/testcases/bin/datafiles"
  781. export TST_NETLOAD_CLN_REQUESTS="${TST_NETLOAD_CLN_REQUESTS:-10000}"
  782. export TST_NETLOAD_CLN_NUMBER="${TST_NETLOAD_CLN_NUMBER:-2}"
  783. export TST_NETLOAD_BINDTODEVICE="${TST_NETLOAD_BINDTODEVICE-1}"
  784. export HTTP_DOWNLOAD_DIR="${HTTP_DOWNLOAD_DIR:-/var/www/html}"
  785. export FTP_DOWNLOAD_DIR="${FTP_DOWNLOAD_DIR:-/var/ftp}"
  786. export FTP_UPLOAD_DIR="${FTP_UPLOAD_DIR:-/var/ftp/pub}"
  787. export FTP_UPLOAD_URLDIR="${FTP_UPLOAD_URLDIR:-pub}"
  788. # network/stress tests require additional parameters
  789. export NS_DURATION="${NS_DURATION:-10}"
  790. export NS_TIMES="${NS_TIMES:-10}"
  791. export CONNECTION_TOTAL="${CONNECTION_TOTAL:-10}"
  792. export IP_TOTAL="${IP_TOTAL:-100}"
  793. export IP_TOTAL_FOR_TCPIP="${IP_TOTAL_FOR_TCPIP:-100}"
  794. export ROUTE_TOTAL="${ROUTE_TOTAL:-100}"
  795. export MTU_CHANGE_TIMES="${MTU_CHANGE_TIMES:-100}"
  796. export IF_UPDOWN_TIMES="${IF_UPDOWN_TIMES:-100}"
  797. export DOWNLOAD_BIGFILESIZE="${DOWNLOAD_BIGFILESIZE:-2147483647}"
  798. export DOWNLOAD_REGFILESIZE="${DOWNLOAD_REGFILESIZE:-1048576}"
  799. export UPLOAD_BIGFILESIZE="${UPLOAD_BIGFILESIZE:-2147483647}"
  800. export UPLOAD_REGFILESIZE="${UPLOAD_REGFILESIZE:-1024}"
  801. export MCASTNUM_NORMAL="${MCASTNUM_NORMAL:-20}"
  802. export MCASTNUM_HEAVY="${MCASTNUM_HEAVY:-4000}"
  803. export ROUTE_CHANGE_IP="${ROUTE_CHANGE_IP:-100}"
  804. export ROUTE_CHANGE_NETLINK="${ROUTE_CHANGE_NETLINK:-10000}"
  805. # Warning: make sure to set valid interface names and IP addresses below.
  806. # Set names for test interfaces, e.g. "eth0 eth1"
  807. # This is fallback for LHOST_IFACES in case tst_net_vars finds nothing or we
  808. # want to use more ifaces.
  809. export LHOST_IFACES="${LHOST_IFACES:-eth0}"
  810. export RHOST_IFACES="${RHOST_IFACES:-eth0}"
  811. # Maximum payload size for 'virt' performance tests, by default eqauls to 1.1 * MTU
  812. export TST_NET_MAX_PKT="${TST_NET_MAX_PKT:-$(tst_default_max_pkt)}"
  813. # Set corresponding HW addresses, e.g. "00:00:00:00:00:01 00:00:00:00:00:02"
  814. export LHOST_HWADDRS="${LHOST_HWADDRS:-$(tst_get_hwaddrs lhost)}"
  815. export RHOST_HWADDRS="${RHOST_HWADDRS:-$(tst_get_hwaddrs rhost)}"
  816. export NS_ICMPV4_SENDER_DATA_MAXSIZE=1472
  817. export NS_ICMPV6_SENDER_DATA_MAXSIZE=1452
  818. # More information about network parameters can be found
  819. # in the following document: testcases/network/stress/README
  820. if [ -n "$TST_USE_LEGACY_API" ]; then
  821. tst_net_remote_tmpdir
  822. fi
  823. if [ -z "$TST_USE_LEGACY_API" ] && ! tst_cmd_available ping6; then
  824. ping6()
  825. {
  826. ping -6 $@
  827. }
  828. if [ -z "$_tst_net_ping6_warn_printed" ]; then
  829. tst_res_ TINFO "ping6 binary/symlink is missing, using workaround. Please, report missing ping6 to your distribution."
  830. export _tst_net_ping6_warn_printed=1
  831. fi
  832. fi