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