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