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