xref: /linux/tools/testing/selftests/bpf/test_xsk.sh (revision a460513ed4b6994bfeb7bd86f72853140bc1ac12)
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# Run with verbose output:
76#   sudo ./test_xsk.sh -v
77#
78# Run and dump packet contents:
79#   sudo ./test_xsk.sh -D
80
81. xsk_prereqs.sh
82
83while getopts "cvD" flag
84do
85	case "${flag}" in
86		c) colorconsole=1;;
87		v) verbose=1;;
88		D) dump_pkts=1;;
89	esac
90done
91
92TEST_NAME="PREREQUISITES"
93
94URANDOM=/dev/urandom
95[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit 1 1; }
96
97VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
98VETH0=ve${VETH0_POSTFIX}
99VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
100VETH1=ve${VETH1_POSTFIX}
101NS0=root
102NS1=af_xdp${VETH1_POSTFIX}
103MTU=1500
104
105setup_vethPairs() {
106	if [[ $verbose -eq 1 ]]; then
107	        echo "setting up ${VETH0}: namespace: ${NS0}"
108	fi
109	ip netns add ${NS1}
110	ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4
111	if [ -f /proc/net/if_inet6 ]; then
112		echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
113	fi
114	if [[ $verbose -eq 1 ]]; then
115	        echo "setting up ${VETH1}: namespace: ${NS1}"
116	fi
117	ip link set ${VETH1} netns ${NS1}
118	ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
119	ip link set ${VETH0} mtu ${MTU}
120	ip netns exec ${NS1} ip link set ${VETH1} up
121	ip netns exec ${NS1} ip link set dev lo up
122	ip link set ${VETH0} up
123}
124
125validate_root_exec
126validate_veth_support ${VETH0}
127validate_ip_utility
128setup_vethPairs
129
130retval=$?
131if [ $retval -ne 0 ]; then
132	test_status $retval "${TEST_NAME}"
133	cleanup_exit ${VETH0} ${VETH1} ${NS1}
134	exit $retval
135fi
136
137echo "${VETH0}:${VETH1},${NS1}" > ${SPECFILE}
138
139validate_veth_spec_file
140
141if [[ $verbose -eq 1 ]]; then
142        echo "Spec file created: ${SPECFILE}"
143	VERBOSE_ARG="-v"
144fi
145
146if [[ $dump_pkts -eq 1 ]]; then
147	DUMP_PKTS_ARG="-D"
148fi
149
150test_status $retval "${TEST_NAME}"
151
152## START TESTS
153
154statusList=()
155
156TEST_NAME="XSK KSELFTESTS"
157
158execxdpxceiver
159
160retval=$?
161test_status $retval "${TEST_NAME}"
162statusList+=($retval)
163
164## END TESTS
165
166cleanup_exit ${VETH0} ${VETH1} ${NS1}
167
168for _status in "${statusList[@]}"
169do
170	if [ $_status -ne 0 ]; then
171		test_exit $ksft_fail 0
172	fi
173done
174
175test_exit $ksft_pass 0
176