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# Run and dump packet contents:
72#   sudo ./test_xsk.sh -D
73#
74# Set up veth interfaces and leave them up so xskxceiver can be launched in a debugger:
75#   sudo ./test_xsk.sh -d
76#
77# Run test suite for physical device in loopback mode
78#   sudo ./test_xsk.sh -i IFACE
79
80. xsk_prereqs.sh
81
82ETH=""
83
84while getopts "vDi:d" flag
85do
86	case "${flag}" in
87		v) verbose=1;;
88		D) dump_pkts=1;;
89		d) debug=1;;
90		i) ETH=${OPTARG};;
91	esac
92done
93
94TEST_NAME="PREREQUISITES"
95
96URANDOM=/dev/urandom
97[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit $ksft_fail; }
98
99VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
100VETH0=ve${VETH0_POSTFIX}
101VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
102VETH1=ve${VETH1_POSTFIX}
103MTU=1500
104
105trap ctrl_c INT
106
107function ctrl_c() {
108        cleanup_exit ${VETH0} ${VETH1}
109	exit 1
110}
111
112setup_vethPairs() {
113	if [[ $verbose -eq 1 ]]; then
114	        echo "setting up ${VETH0}"
115	fi
116	ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4
117	if [ -f /proc/net/if_inet6 ]; then
118		echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
119	fi
120	if [[ $verbose -eq 1 ]]; then
121	        echo "setting up ${VETH1}"
122	fi
123
124	if [[ $busy_poll -eq 1 ]]; then
125	        echo 2 > /sys/class/net/${VETH0}/napi_defer_hard_irqs
126		echo 200000 > /sys/class/net/${VETH0}/gro_flush_timeout
127		echo 2 > /sys/class/net/${VETH1}/napi_defer_hard_irqs
128		echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout
129	fi
130
131	ip link set ${VETH1} mtu ${MTU}
132	ip link set ${VETH0} mtu ${MTU}
133	ip link set ${VETH1} up
134	ip link set ${VETH0} up
135}
136
137if [ ! -z $ETH ]; then
138	VETH0=${ETH}
139	VETH1=${ETH}
140else
141	validate_root_exec
142	validate_veth_support ${VETH0}
143	validate_ip_utility
144	setup_vethPairs
145
146	retval=$?
147	if [ $retval -ne 0 ]; then
148		test_status $retval "${TEST_NAME}"
149		cleanup_exit ${VETH0} ${VETH1}
150		exit $retval
151	fi
152fi
153
154
155if [[ $verbose -eq 1 ]]; then
156	ARGS+="-v "
157fi
158
159if [[ $dump_pkts -eq 1 ]]; then
160	ARGS="-D "
161fi
162
163retval=$?
164test_status $retval "${TEST_NAME}"
165
166## START TESTS
167
168statusList=()
169
170TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ"
171
172if [[ $debug -eq 1 ]]; then
173    echo "-i" ${VETH0} "-i" ${VETH1}
174    exit
175fi
176
177exec_xskxceiver
178
179if [ -z $ETH ]; then
180	cleanup_exit ${VETH0} ${VETH1}
181fi
182TEST_NAME="XSK_SELFTESTS_${VETH0}_BUSY_POLL"
183busy_poll=1
184
185if [ -z $ETH ]; then
186	setup_vethPairs
187fi
188exec_xskxceiver
189
190## END TESTS
191
192if [ -z $ETH ]; then
193	cleanup_exit ${VETH0} ${VETH1}
194fi
195
196failures=0
197echo -e "\nSummary:"
198for i in "${!statusList[@]}"
199do
200	if [ ${statusList[$i]} -ne 0 ]; then
201	        test_status ${statusList[$i]} ${nameList[$i]}
202		failures=1
203	fi
204done
205
206if [ $failures -eq 0 ]; then
207        echo "All tests successful!"
208fi
209