xref: /linux/tools/testing/selftests/net/busy_poll_test.sh (revision 1b98f357dadd6ea613a435fbaef1a5dd7b35fd21)
1347fcdc4SJoe Damato#!/bin/bash
2347fcdc4SJoe Damato# SPDX-License-Identifier: GPL-2.0
3*d9d836bfSHangbin Liusource lib.sh
4347fcdc4SJoe Damato
5347fcdc4SJoe DamatoNSIM_SV_ID=$((256 + RANDOM % 256))
6347fcdc4SJoe DamatoNSIM_SV_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_SV_ID
7347fcdc4SJoe DamatoNSIM_CL_ID=$((512 + RANDOM % 256))
8347fcdc4SJoe DamatoNSIM_CL_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_CL_ID
9347fcdc4SJoe Damato
10347fcdc4SJoe DamatoNSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device
11347fcdc4SJoe DamatoNSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device
12347fcdc4SJoe DamatoNSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device
13347fcdc4SJoe DamatoNSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device
14347fcdc4SJoe Damato
15347fcdc4SJoe DamatoSERVER_IP=192.168.1.1
16347fcdc4SJoe DamatoCLIENT_IP=192.168.1.2
17347fcdc4SJoe DamatoSERVER_PORT=48675
18347fcdc4SJoe Damato
19347fcdc4SJoe Damato# busy poll config
20347fcdc4SJoe DamatoMAX_EVENTS=8
21347fcdc4SJoe DamatoBUSY_POLL_USECS=0
22347fcdc4SJoe DamatoBUSY_POLL_BUDGET=16
23347fcdc4SJoe DamatoPREFER_BUSY_POLL=1
24347fcdc4SJoe Damato
25347fcdc4SJoe Damato# IRQ deferral config
26347fcdc4SJoe DamatoNAPI_DEFER_HARD_IRQS=100
27347fcdc4SJoe DamatoGRO_FLUSH_TIMEOUT=50000
28347fcdc4SJoe DamatoSUSPEND_TIMEOUT=20000000
29347fcdc4SJoe Damato
30347fcdc4SJoe Damatosetup_ns()
31347fcdc4SJoe Damato{
32347fcdc4SJoe Damato	set -e
33347fcdc4SJoe Damato	ip netns add nssv
34347fcdc4SJoe Damato	ip netns add nscl
35347fcdc4SJoe Damato
36347fcdc4SJoe Damato	NSIM_SV_NAME=$(find $NSIM_SV_SYS/net -maxdepth 1 -type d ! \
37347fcdc4SJoe Damato		-path $NSIM_SV_SYS/net -exec basename {} \;)
38347fcdc4SJoe Damato	NSIM_CL_NAME=$(find $NSIM_CL_SYS/net -maxdepth 1 -type d ! \
39347fcdc4SJoe Damato		-path $NSIM_CL_SYS/net -exec basename {} \;)
40347fcdc4SJoe Damato
41347fcdc4SJoe Damato	# ensure the server has 1 queue
42347fcdc4SJoe Damato	ethtool -L $NSIM_SV_NAME combined 1 2>/dev/null
43347fcdc4SJoe Damato
44347fcdc4SJoe Damato	ip link set $NSIM_SV_NAME netns nssv
45347fcdc4SJoe Damato	ip link set $NSIM_CL_NAME netns nscl
46347fcdc4SJoe Damato
47347fcdc4SJoe Damato	ip netns exec nssv ip addr add "${SERVER_IP}/24" dev $NSIM_SV_NAME
48347fcdc4SJoe Damato	ip netns exec nscl ip addr add "${CLIENT_IP}/24" dev $NSIM_CL_NAME
49347fcdc4SJoe Damato
50347fcdc4SJoe Damato	ip netns exec nssv ip link set dev $NSIM_SV_NAME up
51347fcdc4SJoe Damato	ip netns exec nscl ip link set dev $NSIM_CL_NAME up
52347fcdc4SJoe Damato
53347fcdc4SJoe Damato	set +e
54347fcdc4SJoe Damato}
55347fcdc4SJoe Damato
56347fcdc4SJoe Damatocleanup_ns()
57347fcdc4SJoe Damato{
58347fcdc4SJoe Damato	ip netns del nscl
59347fcdc4SJoe Damato	ip netns del nssv
60347fcdc4SJoe Damato}
61347fcdc4SJoe Damato
62347fcdc4SJoe Damatotest_busypoll()
63347fcdc4SJoe Damato{
64347fcdc4SJoe Damato	suspend_value=${1:-0}
65347fcdc4SJoe Damato	tmp_file=$(mktemp)
66347fcdc4SJoe Damato	out_file=$(mktemp)
67347fcdc4SJoe Damato
68347fcdc4SJoe Damato	# fill a test file with random data
69347fcdc4SJoe Damato	dd if=/dev/urandom of=${tmp_file} bs=1M count=1 2> /dev/null
70347fcdc4SJoe Damato
71347fcdc4SJoe Damato	timeout -k 1s 30s ip netns exec nssv ./busy_poller         \
72347fcdc4SJoe Damato					     -p${SERVER_PORT}      \
73347fcdc4SJoe Damato					     -b${SERVER_IP}        \
74347fcdc4SJoe Damato					     -m${MAX_EVENTS}       \
75347fcdc4SJoe Damato					     -u${BUSY_POLL_USECS}  \
76347fcdc4SJoe Damato					     -P${PREFER_BUSY_POLL} \
77347fcdc4SJoe Damato					     -g${BUSY_POLL_BUDGET} \
78347fcdc4SJoe Damato					     -i${NSIM_SV_IFIDX}    \
79347fcdc4SJoe Damato					     -s${suspend_value}    \
80347fcdc4SJoe Damato					     -o${out_file}&
81347fcdc4SJoe Damato
82347fcdc4SJoe Damato	wait_local_port_listen nssv ${SERVER_PORT} tcp
83347fcdc4SJoe Damato
84347fcdc4SJoe Damato	ip netns exec nscl socat -u $tmp_file TCP:${SERVER_IP}:${SERVER_PORT}
85347fcdc4SJoe Damato
86347fcdc4SJoe Damato	wait
87347fcdc4SJoe Damato
88347fcdc4SJoe Damato	tmp_file_md5sum=$(md5sum $tmp_file | cut -f1 -d' ')
89347fcdc4SJoe Damato	out_file_md5sum=$(md5sum $out_file | cut -f1 -d' ')
90347fcdc4SJoe Damato
91347fcdc4SJoe Damato	if [ "$tmp_file_md5sum" = "$out_file_md5sum" ]; then
92347fcdc4SJoe Damato		res=0
93347fcdc4SJoe Damato	else
94347fcdc4SJoe Damato		echo "md5sum mismatch"
95347fcdc4SJoe Damato		echo "input file md5sum: ${tmp_file_md5sum}";
96347fcdc4SJoe Damato		echo "output file md5sum: ${out_file_md5sum}";
97347fcdc4SJoe Damato		res=1
98347fcdc4SJoe Damato	fi
99347fcdc4SJoe Damato
100347fcdc4SJoe Damato	rm $out_file $tmp_file
101347fcdc4SJoe Damato
102347fcdc4SJoe Damato	return $res
103347fcdc4SJoe Damato}
104347fcdc4SJoe Damato
105347fcdc4SJoe Damatotest_busypoll_with_suspend()
106347fcdc4SJoe Damato{
107347fcdc4SJoe Damato	test_busypoll ${SUSPEND_TIMEOUT}
108347fcdc4SJoe Damato
109347fcdc4SJoe Damato	return $?
110347fcdc4SJoe Damato}
111347fcdc4SJoe Damato
112347fcdc4SJoe Damato###
113347fcdc4SJoe Damato### Code start
114347fcdc4SJoe Damato###
115347fcdc4SJoe Damato
116347fcdc4SJoe Damatomodprobe netdevsim
117347fcdc4SJoe Damato
118347fcdc4SJoe Damato# linking
119347fcdc4SJoe Damato
120347fcdc4SJoe Damatoecho $NSIM_SV_ID > $NSIM_DEV_SYS_NEW
121347fcdc4SJoe Damatoecho $NSIM_CL_ID > $NSIM_DEV_SYS_NEW
122347fcdc4SJoe Damatoudevadm settle
123347fcdc4SJoe Damato
124347fcdc4SJoe Damatosetup_ns
125347fcdc4SJoe Damato
126347fcdc4SJoe DamatoNSIM_SV_FD=$((256 + RANDOM % 256))
127347fcdc4SJoe Damatoexec {NSIM_SV_FD}</var/run/netns/nssv
128347fcdc4SJoe DamatoNSIM_SV_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_SV_NAME/ifindex)
129347fcdc4SJoe Damato
130347fcdc4SJoe DamatoNSIM_CL_FD=$((256 + RANDOM % 256))
131347fcdc4SJoe Damatoexec {NSIM_CL_FD}</var/run/netns/nscl
132347fcdc4SJoe DamatoNSIM_CL_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_CL_NAME/ifindex)
133347fcdc4SJoe Damato
134347fcdc4SJoe Damatoecho "$NSIM_SV_FD:$NSIM_SV_IFIDX $NSIM_CL_FD:$NSIM_CL_IFIDX" > \
135347fcdc4SJoe Damato     $NSIM_DEV_SYS_LINK
136347fcdc4SJoe Damato
137347fcdc4SJoe Damatoif [ $? -ne 0 ]; then
138347fcdc4SJoe Damato	echo "linking netdevsim1 with netdevsim2 should succeed"
139347fcdc4SJoe Damato	cleanup_ns
140347fcdc4SJoe Damato	exit 1
141347fcdc4SJoe Damatofi
142347fcdc4SJoe Damato
143347fcdc4SJoe Damatotest_busypoll
144347fcdc4SJoe Damatoif [ $? -ne 0 ]; then
145347fcdc4SJoe Damato	echo "test_busypoll failed"
146347fcdc4SJoe Damato	cleanup_ns
147347fcdc4SJoe Damato	exit 1
148347fcdc4SJoe Damatofi
149347fcdc4SJoe Damato
150347fcdc4SJoe Damatotest_busypoll_with_suspend
151347fcdc4SJoe Damatoif [ $? -ne 0 ]; then
152347fcdc4SJoe Damato	echo "test_busypoll_with_suspend failed"
153347fcdc4SJoe Damato	cleanup_ns
154347fcdc4SJoe Damato	exit 1
155347fcdc4SJoe Damatofi
156347fcdc4SJoe Damato
157347fcdc4SJoe Damatoecho "$NSIM_SV_FD:$NSIM_SV_IFIDX" > $NSIM_DEV_SYS_UNLINK
158347fcdc4SJoe Damato
159347fcdc4SJoe Damatoecho $NSIM_CL_ID > $NSIM_DEV_SYS_DEL
160347fcdc4SJoe Damato
161347fcdc4SJoe Damatocleanup_ns
162347fcdc4SJoe Damato
163347fcdc4SJoe Damatomodprobe -r netdevsim
164347fcdc4SJoe Damato
165347fcdc4SJoe Damatoexit 0
166