xref: /linux/tools/testing/selftests/bpf/test_xsk.sh (revision ca853314e78b0a65c20b6a889a23c31f918d4aa2)
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#   * create a spec file veth.spec that includes this run-time configuration
47#   *** xxxx and yyyy are randomly generated 4 digit numbers used to avoid
48#       conflict with any existing interface
49#   * tests the veth and xsk layers of the topology
50#
51# See the source xdpxceiver.c for information on each test
52#
53# Kernel configuration:
54# ---------------------
55# See "config" file for recommended kernel config options.
56#
57# Turn on XDP sockets and veth support when compiling i.e.
58# 	Networking support -->
59# 		Networking options -->
60# 			[ * ] XDP sockets
61#
62# Executing Tests:
63# ----------------
64# Must run with CAP_NET_ADMIN capability.
65#
66# Run (full color-coded output):
67#   sudo ./test_xsk.sh -c
68#
69# If running from kselftests:
70#   sudo make colorconsole=1 run_tests
71#
72# Run (full output without color-coding):
73#   sudo ./test_xsk.sh
74
75. xsk_prereqs.sh
76
77while getopts c flag
78do
79	case "${flag}" in
80		c) colorconsole=1;;
81	esac
82done
83
84TEST_NAME="PREREQUISITES"
85
86URANDOM=/dev/urandom
87[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit 1 1; }
88
89VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
90VETH0=ve${VETH0_POSTFIX}
91VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
92VETH1=ve${VETH1_POSTFIX}
93NS0=root
94NS1=af_xdp${VETH1_POSTFIX}
95MTU=1500
96
97setup_vethPairs() {
98	echo "setting up ${VETH0}: namespace: ${NS0}"
99	ip netns add ${NS1}
100	ip link add ${VETH0} type veth peer name ${VETH1}
101	if [ -f /proc/net/if_inet6 ]; then
102		echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
103	fi
104	echo "setting up ${VETH1}: namespace: ${NS1}"
105	ip link set ${VETH1} netns ${NS1}
106	ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
107	ip link set ${VETH0} mtu ${MTU}
108	ip netns exec ${NS1} ip link set ${VETH1} up
109	ip link set ${VETH0} up
110}
111
112validate_root_exec
113validate_veth_support ${VETH0}
114validate_ip_utility
115setup_vethPairs
116
117retval=$?
118if [ $retval -ne 0 ]; then
119	test_status $retval "${TEST_NAME}"
120	cleanup_exit ${VETH0} ${VETH1} ${NS1}
121	exit $retval
122fi
123
124echo "${VETH0}:${VETH1},${NS1}" > ${SPECFILE}
125
126validate_veth_spec_file
127
128echo "Spec file created: ${SPECFILE}"
129
130test_status $retval "${TEST_NAME}"
131
132## START TESTS
133
134statusList=()
135
136### TEST 1
137TEST_NAME="XSK KSELFTEST FRAMEWORK"
138
139echo "Switching interfaces [${VETH0}, ${VETH1}] to XDP Generic mode"
140vethXDPgeneric ${VETH0} ${VETH1} ${NS1}
141
142retval=$?
143if [ $retval -eq 0 ]; then
144	echo "Switching interfaces [${VETH0}, ${VETH1}] to XDP Native mode"
145	vethXDPnative ${VETH0} ${VETH1} ${NS1}
146fi
147
148retval=$?
149test_status $retval "${TEST_NAME}"
150statusList+=($retval)
151
152### TEST 2
153TEST_NAME="SKB NOPOLL"
154
155vethXDPgeneric ${VETH0} ${VETH1} ${NS1}
156
157params=("-S")
158execxdpxceiver params
159
160retval=$?
161test_status $retval "${TEST_NAME}"
162statusList+=($retval)
163
164### TEST 3
165TEST_NAME="SKB POLL"
166
167vethXDPgeneric ${VETH0} ${VETH1} ${NS1}
168
169params=("-S" "-p")
170execxdpxceiver params
171
172retval=$?
173test_status $retval "${TEST_NAME}"
174statusList+=($retval)
175
176### TEST 4
177TEST_NAME="DRV NOPOLL"
178
179vethXDPnative ${VETH0} ${VETH1} ${NS1}
180
181params=("-N")
182execxdpxceiver params
183
184retval=$?
185test_status $retval "${TEST_NAME}"
186statusList+=($retval)
187
188### TEST 5
189TEST_NAME="DRV POLL"
190
191vethXDPnative ${VETH0} ${VETH1} ${NS1}
192
193params=("-N" "-p")
194execxdpxceiver params
195
196retval=$?
197test_status $retval "${TEST_NAME}"
198statusList+=($retval)
199
200### TEST 6
201TEST_NAME="SKB SOCKET TEARDOWN"
202
203vethXDPgeneric ${VETH0} ${VETH1} ${NS1}
204
205params=("-S" "-T")
206execxdpxceiver params
207
208retval=$?
209test_status $retval "${TEST_NAME}"
210statusList+=($retval)
211
212### TEST 7
213TEST_NAME="DRV SOCKET TEARDOWN"
214
215vethXDPnative ${VETH0} ${VETH1} ${NS1}
216
217params=("-N" "-T")
218execxdpxceiver params
219
220retval=$?
221test_status $retval "${TEST_NAME}"
222statusList+=($retval)
223
224### TEST 8
225TEST_NAME="SKB BIDIRECTIONAL SOCKETS"
226
227vethXDPgeneric ${VETH0} ${VETH1} ${NS1}
228
229params=("-S" "-B")
230execxdpxceiver params
231
232retval=$?
233test_status $retval "${TEST_NAME}"
234statusList+=($retval)
235
236### TEST 9
237TEST_NAME="DRV BIDIRECTIONAL SOCKETS"
238
239vethXDPnative ${VETH0} ${VETH1} ${NS1}
240
241params=("-N" "-B")
242execxdpxceiver params
243
244retval=$?
245test_status $retval "${TEST_NAME}"
246statusList+=($retval)
247
248## END TESTS
249
250cleanup_exit ${VETH0} ${VETH1} ${NS1}
251
252for _status in "${statusList[@]}"
253do
254	if [ $_status -ne 0 ]; then
255		test_exit $ksft_fail 0
256	fi
257done
258
259test_exit $ksft_pass 0
260