1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright(c) 2020 Intel Corporation, Weqaar Janjua <weqaar.a.janjua@intel.com>
4
5# AF_XDP selftests based on veth
6#
7# End-to-end AF_XDP over Veth test
8#
9# Topology:
10# ---------
11#                 -----------
12#               _ | Process | _
13#              /  -----------  \
14#             /        |        \
15#            /         |         \
16#      -----------     |     -----------
17#      | Thread1 |     |     | Thread2 |
18#      -----------     |     -----------
19#           |          |          |
20#      -----------     |     -----------
21#      |  xskX   |     |     |  xskY   |
22#      -----------     |     -----------
23#           |          |          |
24#      -----------     |     ----------
25#      |  vethX  | --------- |  vethY |
26#      -----------   peer    ----------
27#           |          |          |
28#      namespaceX      |     namespaceY
29#
30# AF_XDP is an address family optimized for high performance packet processing,
31# it is XDP’s user-space interface.
32#
33# An AF_XDP socket is linked to a single UMEM which is a region of virtual
34# contiguous memory, divided into equal-sized frames.
35#
36# Refer to AF_XDP Kernel Documentation for detailed information:
37# https://www.kernel.org/doc/html/latest/networking/af_xdp.html
38#
39# Prerequisites setup by script:
40#
41#   Set up veth interfaces as per the topology shown ^^:
42#   * setup two veth interfaces and one namespace
43#   ** veth<xxxx> in root namespace
44#   ** veth<yyyy> in af_xdp<xxxx> namespace
45#   ** namespace af_xdp<xxxx>
46#   *** xxxx and yyyy are randomly generated 4 digit numbers used to avoid
47#       conflict with any existing interface
48#   * tests the veth and xsk layers of the topology
49#
50# See the source xskxceiver.c for information on each test
51#
52# Kernel configuration:
53# ---------------------
54# See "config" file for recommended kernel config options.
55#
56# Turn on XDP sockets and veth support when compiling i.e.
57# 	Networking support -->
58# 		Networking options -->
59# 			[ * ] XDP sockets
60#
61# Executing Tests:
62# ----------------
63# Must run with CAP_NET_ADMIN capability.
64#
65# Run:
66#   sudo ./test_xsk.sh
67#
68# If running from kselftests:
69#   sudo make run_tests
70#
71# Run with verbose output:
72#   sudo ./test_xsk.sh -v
73#
74# Run and dump packet contents:
75#   sudo ./test_xsk.sh -D
76#
77# Set up veth interfaces and leave them up so xskxceiver can be launched in a debugger:
78#   sudo ./test_xsk.sh -d
79#
80# Run test suite for physical device in loopback mode
81#   sudo ./test_xsk.sh -i IFACE
82
83. xsk_prereqs.sh
84
85ETH=""
86
87while getopts "vDi:d" flag
88do
89	case "${flag}" in
90		v) verbose=1;;
91		D) dump_pkts=1;;
92		d) debug=1;;
93		i) ETH=${OPTARG};;
94	esac
95done
96
97TEST_NAME="PREREQUISITES"
98
99URANDOM=/dev/urandom
100[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit $ksft_fail; }
101
102VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
103VETH0=ve${VETH0_POSTFIX}
104VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
105VETH1=ve${VETH1_POSTFIX}
106NS0=root
107NS1=af_xdp${VETH1_POSTFIX}
108MTU=1500
109
110trap ctrl_c INT
111
112function ctrl_c() {
113        cleanup_exit ${VETH0} ${VETH1} ${NS1}
114	exit 1
115}
116
117setup_vethPairs() {
118	if [[ $verbose -eq 1 ]]; then
119	        echo "setting up ${VETH0}: namespace: ${NS0}"
120	fi
121	ip netns add ${NS1}
122	ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4
123	if [ -f /proc/net/if_inet6 ]; then
124		echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
125	fi
126	if [[ $verbose -eq 1 ]]; then
127	        echo "setting up ${VETH1}: namespace: ${NS1}"
128	fi
129
130	if [[ $busy_poll -eq 1 ]]; then
131	        echo 2 > /sys/class/net/${VETH0}/napi_defer_hard_irqs
132		echo 200000 > /sys/class/net/${VETH0}/gro_flush_timeout
133		echo 2 > /sys/class/net/${VETH1}/napi_defer_hard_irqs
134		echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout
135	fi
136
137	ip link set ${VETH1} netns ${NS1}
138	ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
139	ip link set ${VETH0} mtu ${MTU}
140	ip netns exec ${NS1} ip link set ${VETH1} up
141	ip netns exec ${NS1} ip link set dev lo up
142	ip link set ${VETH0} up
143}
144
145if [ ! -z $ETH ]; then
146	VETH0=${ETH}
147	VETH1=${ETH}
148	NS1=""
149else
150	validate_root_exec
151	validate_veth_support ${VETH0}
152	validate_ip_utility
153	setup_vethPairs
154
155	retval=$?
156	if [ $retval -ne 0 ]; then
157		test_status $retval "${TEST_NAME}"
158		cleanup_exit ${VETH0} ${VETH1} ${NS1}
159		exit $retval
160	fi
161fi
162
163
164if [[ $verbose -eq 1 ]]; then
165	ARGS+="-v "
166fi
167
168if [[ $dump_pkts -eq 1 ]]; then
169	ARGS="-D "
170fi
171
172retval=$?
173test_status $retval "${TEST_NAME}"
174
175## START TESTS
176
177statusList=()
178
179TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ"
180
181if [[ $debug -eq 1 ]]; then
182    echo "-i" ${VETH0} "-i" ${VETH1},${NS1}
183    exit
184fi
185
186exec_xskxceiver
187
188if [ -z $ETH ]; then
189	cleanup_exit ${VETH0} ${VETH1} ${NS1}
190fi
191TEST_NAME="XSK_SELFTESTS_${VETH0}_BUSY_POLL"
192busy_poll=1
193
194if [ -z $ETH ]; then
195	setup_vethPairs
196fi
197exec_xskxceiver
198
199## END TESTS
200
201if [ -z $ETH ]; then
202	cleanup_exit ${VETH0} ${VETH1} ${NS1}
203fi
204
205failures=0
206echo -e "\nSummary:"
207for i in "${!statusList[@]}"
208do
209	if [ ${statusList[$i]} -ne 0 ]; then
210	        test_status ${statusList[$i]} ${nameList[$i]}
211		failures=1
212	fi
213done
214
215if [ $failures -eq 0 ]; then
216        echo "All tests successful!"
217fi
218