drm/bridge: adv7511: Switch to atomic operations
[drm/drm-misc.git] / tools / testing / selftests / net / veth.sh
blob6bb7dfaa30b6ae6ea3685ed481fbaaa111a9f41b
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
4 BPF_FILE="xdp_dummy.bpf.o"
5 readonly STATS="$(mktemp -p /tmp ns-XXXXXX)"
6 readonly BASE=`basename $STATS`
7 readonly SRC=2
8 readonly DST=1
9 readonly DST_NAT=100
10 readonly NS_SRC=$BASE$SRC
11 readonly NS_DST=$BASE$DST
13 # "baremetal" network used for raw UDP traffic
14 readonly BM_NET_V4=192.168.1.
15 readonly BM_NET_V6=2001:db8::
17 readonly CPUS=`nproc`
18 ret=0
20 cleanup() {
21 local ns
22 local jobs
23 readonly jobs="$(jobs -p)"
24 [ -n "${jobs}" ] && kill -1 ${jobs} 2>/dev/null
25 rm -f $STATS
27 for ns in $NS_SRC $NS_DST; do
28 ip netns del $ns 2>/dev/null
29 done
32 trap cleanup EXIT
34 create_ns() {
35 local ns
37 for ns in $NS_SRC $NS_DST; do
38 ip netns add $ns
39 ip -n $ns link set dev lo up
40 done
42 ip link add name veth$SRC type veth peer name veth$DST
44 for ns in $SRC $DST; do
45 ip link set dev veth$ns netns $BASE$ns up
46 ip -n $BASE$ns addr add dev veth$ns $BM_NET_V4$ns/24
47 ip -n $BASE$ns addr add dev veth$ns $BM_NET_V6$ns/64 nodad
48 done
51 __chk_flag() {
52 local msg="$1"
53 local target=$2
54 local expected=$3
55 local flagname=$4
57 local flag=`ip netns exec $BASE$target ethtool -k veth$target |\
58 grep $flagname | awk '{print $2}'`
60 printf "%-60s" "$msg"
61 if [ "$flag" = "$expected" ]; then
62 echo " ok "
63 else
64 echo " fail - expected $expected found $flag"
65 ret=1
69 chk_gro_flag() {
70 __chk_flag "$1" $2 $3 generic-receive-offload
73 chk_tso_flag() {
74 __chk_flag "$1" $2 $3 tcp-segmentation-offload
77 chk_channels() {
78 local msg="$1"
79 local target=$2
80 local rx=$3
81 local tx=$4
83 local dev=veth$target
85 local cur_rx=`ip netns exec $BASE$target ethtool -l $dev |\
86 grep RX: | tail -n 1 | awk '{print $2}' `
87 local cur_tx=`ip netns exec $BASE$target ethtool -l $dev |\
88 grep TX: | tail -n 1 | awk '{print $2}'`
89 local cur_combined=`ip netns exec $BASE$target ethtool -l $dev |\
90 grep Combined: | tail -n 1 | awk '{print $2}'`
92 printf "%-60s" "$msg"
93 if [ "$cur_rx" = "$rx" -a "$cur_tx" = "$tx" -a "$cur_combined" = "n/a" ]; then
94 echo " ok "
95 else
96 echo " fail rx:$rx:$cur_rx tx:$tx:$cur_tx combined:n/a:$cur_combined"
100 chk_gro() {
101 local msg="$1"
102 local expected=$2
104 ip netns exec $BASE$SRC ping -qc 1 $BM_NET_V4$DST >/dev/null
105 NSTAT_HISTORY=$STATS ip netns exec $NS_DST nstat -n
107 printf "%-60s" "$msg"
108 ip netns exec $BASE$DST ./udpgso_bench_rx -C 1000 -R 10 &
109 local spid=$!
110 sleep 0.1
112 ip netns exec $NS_SRC ./udpgso_bench_tx -4 -s 13000 -S 1300 -M 1 -D $BM_NET_V4$DST
113 local retc=$?
114 wait $spid
115 local rets=$?
116 if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
117 echo " fail client exit code $retc, server $rets"
118 ret=1
119 return
122 local pkts=`NSTAT_HISTORY=$STATS ip netns exec $NS_DST nstat IpInReceives | \
123 awk '{print $2}' | tail -n 1`
124 if [ "$pkts" = "$expected" ]; then
125 echo " ok "
126 else
127 echo " fail - got $pkts packets, expected $expected "
128 ret=1
132 __change_channels()
134 local cur_cpu
135 local end=$1
136 local cur
137 local i
139 while true; do
140 printf -v cur '%(%s)T'
141 [ $cur -le $end ] || break
143 for i in `seq 1 $CPUS`; do
144 ip netns exec $NS_SRC ethtool -L veth$SRC rx $i tx $i
145 ip netns exec $NS_DST ethtool -L veth$DST rx $i tx $i
146 done
148 for i in `seq 1 $((CPUS - 1))`; do
149 cur_cpu=$((CPUS - $i))
150 ip netns exec $NS_SRC ethtool -L veth$SRC rx $cur_cpu tx $cur_cpu
151 ip netns exec $NS_DST ethtool -L veth$DST rx $cur_cpu tx $cur_cpu
152 done
153 done
156 __send_data() {
157 local end=$1
159 while true; do
160 printf -v cur '%(%s)T'
161 [ $cur -le $end ] || break
163 ip netns exec $NS_SRC ./udpgso_bench_tx -4 -s 1000 -M 300 -D $BM_NET_V4$DST
164 done
167 do_stress() {
168 local end
169 printf -v end '%(%s)T'
170 end=$((end + $STRESS))
172 ip netns exec $NS_SRC ethtool -L veth$SRC rx 3 tx 3
173 ip netns exec $NS_DST ethtool -L veth$DST rx 3 tx 3
175 ip netns exec $NS_DST ./udpgso_bench_rx &
176 local rx_pid=$!
178 echo "Running stress test for $STRESS seconds..."
179 __change_channels $end &
180 local ch_pid=$!
181 __send_data $end &
182 local data_pid_1=$!
183 __send_data $end &
184 local data_pid_2=$!
185 __send_data $end &
186 local data_pid_3=$!
187 __send_data $end &
188 local data_pid_4=$!
190 wait $ch_pid $data_pid_1 $data_pid_2 $data_pid_3 $data_pid_4
191 kill -9 $rx_pid
192 echo "done"
194 # restore previous setting
195 ip netns exec $NS_SRC ethtool -L veth$SRC rx 2 tx 2
196 ip netns exec $NS_DST ethtool -L veth$DST rx 2 tx 1
199 usage() {
200 echo "Usage: $0 [-h] [-s <seconds>]"
201 echo -e "\t-h: show this help"
202 echo -e "\t-s: run optional stress tests for the given amount of seconds"
205 STRESS=0
206 while getopts "hs:" option; do
207 case "$option" in
208 "h")
209 usage $0
210 exit 0
212 "s")
213 STRESS=$OPTARG
215 esac
216 done
218 if [ ! -f ${BPF_FILE} ]; then
219 echo "Missing ${BPF_FILE}. Run 'make' first"
220 exit 1
223 [ $CPUS -lt 2 ] && echo "Only one CPU available, some tests will be skipped"
224 [ $STRESS -gt 0 -a $CPUS -lt 3 ] && echo " stress test will be skipped, too"
226 create_ns
227 chk_gro_flag "default - gro flag" $SRC off
228 chk_gro_flag " - peer gro flag" $DST off
229 chk_tso_flag " - tso flag" $SRC on
230 chk_tso_flag " - peer tso flag" $DST on
231 chk_gro " - aggregation" 1
232 ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
233 chk_gro " - aggregation with TSO off" 10
234 cleanup
236 create_ns
237 ip netns exec $NS_DST ethtool -K veth$DST gro on
238 chk_gro_flag "with gro on - gro flag" $DST on
239 chk_gro_flag " - peer gro flag" $SRC off
240 chk_tso_flag " - tso flag" $SRC on
241 chk_tso_flag " - peer tso flag" $DST on
242 ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
243 ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
244 chk_gro " - aggregation with TSO off" 1
245 cleanup
247 create_ns
248 ip -n $NS_DST link set dev veth$DST up
249 ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp
250 chk_gro_flag "gro vs xdp while down - gro flag off" $DST off
251 ip -n $NS_DST link set dev veth$DST down
252 chk_gro_flag " - after down" $DST off
253 ip -n $NS_DST link set dev veth$DST xdp off
254 chk_gro_flag " - after xdp off" $DST off
255 ip -n $NS_DST link set dev veth$DST up
256 chk_gro_flag " - after up" $DST off
257 ip -n $NS_SRC link set dev veth$SRC xdp object ${BPF_FILE} section xdp
258 chk_gro_flag " - after peer xdp" $DST off
259 cleanup
261 create_ns
262 ip -n $NS_DST link set dev veth$DST up
263 ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp
264 ip netns exec $NS_DST ethtool -K veth$DST generic-receive-offload on
265 chk_gro_flag "gro vs xdp while down - gro flag on" $DST on
266 ip -n $NS_DST link set dev veth$DST down
267 chk_gro_flag " - after down" $DST on
268 ip -n $NS_DST link set dev veth$DST xdp off
269 chk_gro_flag " - after xdp off" $DST on
270 ip -n $NS_DST link set dev veth$DST up
271 chk_gro_flag " - after up" $DST on
272 ip -n $NS_SRC link set dev veth$SRC xdp object ${BPF_FILE} section xdp
273 chk_gro_flag " - after peer xdp" $DST on
274 cleanup
276 create_ns
277 chk_channels "default channels" $DST 1 1
279 ip -n $NS_DST link set dev veth$DST down
280 ip netns exec $NS_DST ethtool -K veth$DST gro on
281 chk_gro_flag "with gro enabled on link down - gro flag" $DST on
282 chk_gro_flag " - peer gro flag" $SRC off
283 chk_tso_flag " - tso flag" $SRC on
284 chk_tso_flag " - peer tso flag" $DST on
285 ip -n $NS_DST link set dev veth$DST up
286 ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
287 ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
288 chk_gro " - aggregation with TSO off" 1
289 cleanup
291 create_ns
293 CUR_TX=1
294 CUR_RX=1
295 if [ $CPUS -gt 1 ]; then
296 ip netns exec $NS_DST ethtool -L veth$DST tx 2
297 chk_channels "setting tx channels" $DST 1 2
298 CUR_TX=2
301 if [ $CPUS -gt 2 ]; then
302 ip netns exec $NS_DST ethtool -L veth$DST rx 3 tx 3
303 chk_channels "setting both rx and tx channels" $DST 3 3
304 CUR_RX=3
305 CUR_TX=3
308 ip netns exec $NS_DST ethtool -L veth$DST combined 2 2>/dev/null
309 chk_channels "bad setting: combined channels" $DST $CUR_RX $CUR_TX
311 ip netns exec $NS_DST ethtool -L veth$DST tx $((CPUS + 1)) 2>/dev/null
312 chk_channels "setting invalid channels nr" $DST $CUR_RX $CUR_TX
314 if [ $CPUS -gt 1 ]; then
315 # this also tests queues nr reduction
316 ip netns exec $NS_DST ethtool -L veth$DST rx 1 tx 2 2>/dev/null
317 ip netns exec $NS_SRC ethtool -L veth$SRC rx 1 tx 2 2>/dev/null
318 printf "%-60s" "bad setting: XDP with RX nr less than TX"
319 ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} \
320 section xdp 2>/dev/null &&\
321 echo "fail - set operation successful ?!?" || echo " ok "
323 # the following tests will run with multiple channels active
324 ip netns exec $NS_SRC ethtool -L veth$SRC rx 2
325 ip netns exec $NS_DST ethtool -L veth$DST rx 2
326 ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} \
327 section xdp 2>/dev/null
328 printf "%-60s" "bad setting: reducing RX nr below peer TX with XDP set"
329 ip netns exec $NS_DST ethtool -L veth$DST rx 1 2>/dev/null &&\
330 echo "fail - set operation successful ?!?" || echo " ok "
331 CUR_RX=2
332 CUR_TX=2
335 if [ $CPUS -gt 2 ]; then
336 printf "%-60s" "bad setting: increasing peer TX nr above RX with XDP set"
337 ip netns exec $NS_SRC ethtool -L veth$SRC tx 3 2>/dev/null &&\
338 echo "fail - set operation successful ?!?" || echo " ok "
339 chk_channels "setting invalid channels nr" $DST 2 2
342 ip -n $NS_DST link set dev veth$DST xdp object ${BPF_FILE} section xdp 2>/dev/null
343 chk_gro_flag "with xdp attached - gro flag" $DST off
344 chk_gro_flag " - peer gro flag" $SRC off
345 chk_tso_flag " - tso flag" $SRC off
346 chk_tso_flag " - peer tso flag" $DST on
347 ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
348 chk_gro " - no aggregation" 10
349 ip netns exec $NS_DST ethtool -K veth$DST generic-receive-offload on
350 chk_gro_flag " - gro flag with GRO on" $DST on
351 chk_gro " - aggregation" 1
354 ip -n $NS_DST link set dev veth$DST down
355 ip -n $NS_SRC link set dev veth$SRC down
356 chk_gro_flag " - after dev off, flag" $DST on
357 chk_gro_flag " - peer flag" $SRC off
359 ip netns exec $NS_DST ethtool -K veth$DST gro on
360 ip -n $NS_DST link set dev veth$DST xdp off
361 chk_gro_flag " - after gro on xdp off, gro flag" $DST on
362 chk_gro_flag " - peer gro flag" $SRC off
363 chk_tso_flag " - tso flag" $SRC on
364 chk_tso_flag " - peer tso flag" $DST on
366 if [ $CPUS -gt 1 ]; then
367 ip netns exec $NS_DST ethtool -L veth$DST tx 1
368 chk_channels "decreasing tx channels with device down" $DST 2 1
371 ip -n $NS_DST link set dev veth$DST up
372 ip -n $NS_SRC link set dev veth$SRC up
373 chk_gro " - aggregation" 1
375 if [ $CPUS -gt 1 ]; then
376 [ $STRESS -gt 0 -a $CPUS -gt 2 ] && do_stress
378 ip -n $NS_DST link set dev veth$DST down
379 ip -n $NS_SRC link set dev veth$SRC down
380 ip netns exec $NS_DST ethtool -L veth$DST tx 2
381 chk_channels "increasing tx channels with device down" $DST 2 2
382 ip -n $NS_DST link set dev veth$DST up
383 ip -n $NS_SRC link set dev veth$SRC up
386 ip netns exec $NS_DST ethtool -K veth$DST gro off
387 ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
388 chk_gro "aggregation again with default and TSO off" 10
390 exit $ret