xref: /linux/tools/testing/selftests/net/busy_poll_test.sh (revision c34e9ab9a612ee8b18273398ef75c207b01f516d)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3source net_helper.sh
4
5NSIM_SV_ID=$((256 + RANDOM % 256))
6NSIM_SV_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_SV_ID
7NSIM_CL_ID=$((512 + RANDOM % 256))
8NSIM_CL_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_CL_ID
9
10NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device
11NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device
12NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device
13NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device
14
15SERVER_IP=192.168.1.1
16CLIENT_IP=192.168.1.2
17SERVER_PORT=48675
18
19# busy poll config
20MAX_EVENTS=8
21BUSY_POLL_USECS=0
22BUSY_POLL_BUDGET=16
23PREFER_BUSY_POLL=1
24
25# IRQ deferral config
26NAPI_DEFER_HARD_IRQS=100
27GRO_FLUSH_TIMEOUT=50000
28SUSPEND_TIMEOUT=20000000
29
30setup_ns()
31{
32	set -e
33	ip netns add nssv
34	ip netns add nscl
35
36	NSIM_SV_NAME=$(find $NSIM_SV_SYS/net -maxdepth 1 -type d ! \
37		-path $NSIM_SV_SYS/net -exec basename {} \;)
38	NSIM_CL_NAME=$(find $NSIM_CL_SYS/net -maxdepth 1 -type d ! \
39		-path $NSIM_CL_SYS/net -exec basename {} \;)
40
41	# ensure the server has 1 queue
42	ethtool -L $NSIM_SV_NAME combined 1 2>/dev/null
43
44	ip link set $NSIM_SV_NAME netns nssv
45	ip link set $NSIM_CL_NAME netns nscl
46
47	ip netns exec nssv ip addr add "${SERVER_IP}/24" dev $NSIM_SV_NAME
48	ip netns exec nscl ip addr add "${CLIENT_IP}/24" dev $NSIM_CL_NAME
49
50	ip netns exec nssv ip link set dev $NSIM_SV_NAME up
51	ip netns exec nscl ip link set dev $NSIM_CL_NAME up
52
53	set +e
54}
55
56cleanup_ns()
57{
58	ip netns del nscl
59	ip netns del nssv
60}
61
62test_busypoll()
63{
64	suspend_value=${1:-0}
65	tmp_file=$(mktemp)
66	out_file=$(mktemp)
67
68	# fill a test file with random data
69	dd if=/dev/urandom of=${tmp_file} bs=1M count=1 2> /dev/null
70
71	timeout -k 1s 30s ip netns exec nssv ./busy_poller         \
72					     -p${SERVER_PORT}      \
73					     -b${SERVER_IP}        \
74					     -m${MAX_EVENTS}       \
75					     -u${BUSY_POLL_USECS}  \
76					     -P${PREFER_BUSY_POLL} \
77					     -g${BUSY_POLL_BUDGET} \
78					     -i${NSIM_SV_IFIDX}    \
79					     -s${suspend_value}    \
80					     -o${out_file}&
81
82	wait_local_port_listen nssv ${SERVER_PORT} tcp
83
84	ip netns exec nscl socat -u $tmp_file TCP:${SERVER_IP}:${SERVER_PORT}
85
86	wait
87
88	tmp_file_md5sum=$(md5sum $tmp_file | cut -f1 -d' ')
89	out_file_md5sum=$(md5sum $out_file | cut -f1 -d' ')
90
91	if [ "$tmp_file_md5sum" = "$out_file_md5sum" ]; then
92		res=0
93	else
94		echo "md5sum mismatch"
95		echo "input file md5sum: ${tmp_file_md5sum}";
96		echo "output file md5sum: ${out_file_md5sum}";
97		res=1
98	fi
99
100	rm $out_file $tmp_file
101
102	return $res
103}
104
105test_busypoll_with_suspend()
106{
107	test_busypoll ${SUSPEND_TIMEOUT}
108
109	return $?
110}
111
112###
113### Code start
114###
115
116modprobe netdevsim
117
118# linking
119
120echo $NSIM_SV_ID > $NSIM_DEV_SYS_NEW
121echo $NSIM_CL_ID > $NSIM_DEV_SYS_NEW
122udevadm settle
123
124setup_ns
125
126NSIM_SV_FD=$((256 + RANDOM % 256))
127exec {NSIM_SV_FD}</var/run/netns/nssv
128NSIM_SV_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_SV_NAME/ifindex)
129
130NSIM_CL_FD=$((256 + RANDOM % 256))
131exec {NSIM_CL_FD}</var/run/netns/nscl
132NSIM_CL_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_CL_NAME/ifindex)
133
134echo "$NSIM_SV_FD:$NSIM_SV_IFIDX $NSIM_CL_FD:$NSIM_CL_IFIDX" > \
135     $NSIM_DEV_SYS_LINK
136
137if [ $? -ne 0 ]; then
138	echo "linking netdevsim1 with netdevsim2 should succeed"
139	cleanup_ns
140	exit 1
141fi
142
143test_busypoll
144if [ $? -ne 0 ]; then
145	echo "test_busypoll failed"
146	cleanup_ns
147	exit 1
148fi
149
150test_busypoll_with_suspend
151if [ $? -ne 0 ]; then
152	echo "test_busypoll_with_suspend failed"
153	cleanup_ns
154	exit 1
155fi
156
157echo "$NSIM_SV_FD:$NSIM_SV_IFIDX" > $NSIM_DEV_SYS_UNLINK
158
159echo $NSIM_CL_ID > $NSIM_DEV_SYS_DEL
160
161cleanup_ns
162
163modprobe -r netdevsim
164
165exit 0
166