xref: /linux/tools/testing/selftests/bpf/test_xsk.sh (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
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		echo 1 > /proc/sys/net/ipv6/conf/${VETH1}/disable_ipv6
120	fi
121	if [[ $verbose -eq 1 ]]; then
122	        echo "setting up ${VETH1}"
123	fi
124
125	if [[ $busy_poll -eq 1 ]]; then
126	        echo 2 > /sys/class/net/${VETH0}/napi_defer_hard_irqs
127		echo 200000 > /sys/class/net/${VETH0}/gro_flush_timeout
128		echo 2 > /sys/class/net/${VETH1}/napi_defer_hard_irqs
129		echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout
130	fi
131
132	ip link set ${VETH1} mtu ${MTU}
133	ip link set ${VETH0} mtu ${MTU}
134	ip link set ${VETH1} up
135	ip link set ${VETH0} up
136}
137
138if [ ! -z $ETH ]; then
139	VETH0=${ETH}
140	VETH1=${ETH}
141else
142	validate_root_exec
143	validate_veth_support ${VETH0}
144	validate_ip_utility
145	setup_vethPairs
146
147	retval=$?
148	if [ $retval -ne 0 ]; then
149		test_status $retval "${TEST_NAME}"
150		cleanup_exit ${VETH0} ${VETH1}
151		exit $retval
152	fi
153fi
154
155
156if [[ $verbose -eq 1 ]]; then
157	ARGS+="-v "
158fi
159
160if [[ $dump_pkts -eq 1 ]]; then
161	ARGS="-D "
162fi
163
164retval=$?
165test_status $retval "${TEST_NAME}"
166
167## START TESTS
168
169statusList=()
170
171TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ"
172
173if [[ $debug -eq 1 ]]; then
174    echo "-i" ${VETH0} "-i" ${VETH1}
175    exit
176fi
177
178exec_xskxceiver
179
180if [ -z $ETH ]; then
181	cleanup_exit ${VETH0} ${VETH1}
182fi
183TEST_NAME="XSK_SELFTESTS_${VETH0}_BUSY_POLL"
184busy_poll=1
185
186if [ -z $ETH ]; then
187	setup_vethPairs
188fi
189exec_xskxceiver
190
191## END TESTS
192
193if [ -z $ETH ]; then
194	cleanup_exit ${VETH0} ${VETH1}
195fi
196
197failures=0
198echo -e "\nSummary:"
199for i in "${!statusList[@]}"
200do
201	if [ ${statusList[$i]} -ne 0 ]; then
202	        test_status ${statusList[$i]} ${nameList[$i]}
203		failures=1
204	fi
205done
206
207if [ $failures -eq 0 ]; then
208        echo "All tests successful!"
209fi
210