1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4readonly STATS="$(mktemp -p /tmp ns-XXXXXX)"
5readonly BASE=`basename $STATS`
6readonly SRC=2
7readonly DST=1
8readonly DST_NAT=100
9readonly NS_SRC=$BASE$SRC
10readonly NS_DST=$BASE$DST
11
12# "baremetal" network used for raw UDP traffic
13readonly BM_NET_V4=192.168.1.
14readonly BM_NET_V6=2001:db8::
15
16readonly NPROCS=`nproc`
17ret=0
18
19cleanup() {
20	local ns
21	local -r jobs="$(jobs -p)"
22	[ -n "${jobs}" ] && kill -1 ${jobs} 2>/dev/null
23	rm -f $STATS
24
25	for ns in $NS_SRC $NS_DST; do
26		ip netns del $ns 2>/dev/null
27	done
28}
29
30trap cleanup EXIT
31
32create_ns() {
33	local ns
34
35	for ns in $NS_SRC $NS_DST; do
36		ip netns add $ns
37		ip -n $ns link set dev lo up
38	done
39
40	ip link add name veth$SRC type veth peer name veth$DST
41
42	for ns in $SRC $DST; do
43		ip link set dev veth$ns netns $BASE$ns up
44		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V4$ns/24
45		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V6$ns/64 nodad
46	done
47	echo "#kernel" > $BASE
48	chmod go-rw $BASE
49}
50
51__chk_flag() {
52	local msg="$1"
53	local target=$2
54	local expected=$3
55	local flagname=$4
56
57	local flag=`ip netns exec $BASE$target ethtool -k veth$target |\
58		    grep $flagname | awk '{print $2}'`
59
60	printf "%-60s" "$msg"
61	if [ "$flag" = "$expected" ]; then
62		echo " ok "
63	else
64		echo " fail - expected $expected found $flag"
65		ret=1
66	fi
67}
68
69chk_gro_flag() {
70	__chk_flag "$1" $2 $3 generic-receive-offload
71}
72
73chk_tso_flag() {
74	__chk_flag "$1" $2 $3 tcp-segmentation-offload
75}
76
77chk_gro() {
78	local msg="$1"
79	local expected=$2
80
81	ip netns exec $BASE$SRC ping -qc 1 $BM_NET_V4$DST >/dev/null
82	NSTAT_HISTORY=$STATS ip netns exec $NS_DST nstat -n
83
84	printf "%-60s" "$msg"
85	ip netns exec $BASE$DST ./udpgso_bench_rx -C 1000 -R 10 &
86	local spid=$!
87	sleep 0.1
88
89	ip netns exec $NS_SRC ./udpgso_bench_tx -4 -s 13000 -S 1300 -M 1 -D $BM_NET_V4$DST
90	local retc=$?
91	wait $spid
92	local rets=$?
93	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
94		echo " fail client exit code $retc, server $rets"
95		ret=1
96		return
97	fi
98
99	local pkts=`NSTAT_HISTORY=$STATS ip netns exec $NS_DST nstat IpInReceives | \
100		    awk '{print $2}' | tail -n 1`
101	if [ "$pkts" = "$expected" ]; then
102		echo " ok "
103	else
104		echo " fail - got $pkts packets, expected $expected "
105		ret=1
106	fi
107}
108
109if [ ! -f ../bpf/xdp_dummy.o ]; then
110	echo "Missing xdp_dummy helper. Build bpf selftest first"
111	exit -1
112fi
113
114create_ns
115chk_gro_flag "default - gro flag" $SRC off
116chk_gro_flag "        - peer gro flag" $DST off
117chk_tso_flag "        - tso flag" $SRC on
118chk_tso_flag "        - peer tso flag" $DST on
119chk_gro "        - aggregation" 1
120ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
121chk_gro "        - aggregation with TSO off" 10
122cleanup
123
124create_ns
125ip netns exec $NS_DST ethtool -K veth$DST gro on
126chk_gro_flag "with gro on - gro flag" $DST on
127chk_gro_flag "        - peer gro flag" $SRC off
128chk_tso_flag "        - tso flag" $SRC on
129chk_tso_flag "        - peer tso flag" $DST on
130ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
131ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
132chk_gro "        - aggregation with TSO off" 1
133cleanup
134
135create_ns
136ip -n $NS_DST link set dev veth$DST down
137ip netns exec $NS_DST ethtool -K veth$DST gro on
138chk_gro_flag "with gro enabled on link down - gro flag" $DST on
139chk_gro_flag "        - peer gro flag" $SRC off
140chk_tso_flag "        - tso flag" $SRC on
141chk_tso_flag "        - peer tso flag" $DST on
142ip -n $NS_DST link set dev veth$DST up
143ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
144ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
145chk_gro "        - aggregation with TSO off" 1
146cleanup
147
148create_ns
149ip -n $NS_DST link set dev veth$DST xdp object ../bpf/xdp_dummy.o section xdp_dummy 2>/dev/null
150chk_gro_flag "with xdp attached - gro flag" $DST on
151chk_gro_flag "        - peer gro flag" $SRC off
152chk_tso_flag "        - tso flag" $SRC off
153chk_tso_flag "        - peer tso flag" $DST on
154ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
155chk_gro "        - aggregation" 1
156
157
158ip -n $NS_DST link set dev veth$DST down
159ip -n $NS_SRC link set dev veth$SRC down
160chk_gro_flag "        - after dev off, flag" $DST on
161chk_gro_flag "        - peer flag" $SRC off
162
163ip netns exec $NS_DST ethtool -K veth$DST gro on
164ip -n $NS_DST link set dev veth$DST xdp off
165chk_gro_flag "        - after gro on xdp off, gro flag" $DST on
166chk_gro_flag "        - peer gro flag" $SRC off
167chk_tso_flag "        - tso flag" $SRC on
168chk_tso_flag "        - peer tso flag" $DST on
169ip -n $NS_DST link set dev veth$DST up
170ip -n $NS_SRC link set dev veth$SRC up
171chk_gro "        - aggregation" 1
172
173ip netns exec $NS_DST ethtool -K veth$DST gro off
174ip netns exec $NS_SRC ethtool -K veth$SRC tx-udp-segmentation off
175chk_gro "aggregation again with default and TSO off" 10
176
177exit $ret
178