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#   *** xxxx and yyyy are randomly generated 4 digit numbers used to avoid
47#       conflict with any existing interface
48#   * tests the veth and xsk layers of the topology
49#
50# See the source xdpxceiver.c for information on each test
51#
52# Kernel configuration:
53# ---------------------
54# See "config" file for recommended kernel config options.
55#
56# Turn on XDP sockets and veth support when compiling i.e.
57# 	Networking support -->
58# 		Networking options -->
59# 			[ * ] XDP sockets
60#
61# Executing Tests:
62# ----------------
63# Must run with CAP_NET_ADMIN capability.
64#
65# Run:
66#   sudo ./test_xsk.sh
67#
68# If running from kselftests:
69#   sudo make run_tests
70#
71# Run with verbose output:
72#   sudo ./test_xsk.sh -v
73#
74# Run and dump packet contents:
75#   sudo ./test_xsk.sh -D
76
77. xsk_prereqs.sh
78
79while getopts "vD" flag
80do
81	case "${flag}" in
82		v) verbose=1;;
83		D) dump_pkts=1;;
84	esac
85done
86
87TEST_NAME="PREREQUISITES"
88
89URANDOM=/dev/urandom
90[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit 1 1; }
91
92VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
93VETH0=ve${VETH0_POSTFIX}
94VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
95VETH1=ve${VETH1_POSTFIX}
96NS0=root
97NS1=af_xdp${VETH1_POSTFIX}
98MTU=1500
99
100setup_vethPairs() {
101	if [[ $verbose -eq 1 ]]; then
102	        echo "setting up ${VETH0}: namespace: ${NS0}"
103	fi
104	ip netns add ${NS1}
105	ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4
106	if [ -f /proc/net/if_inet6 ]; then
107		echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
108	fi
109	if [[ $verbose -eq 1 ]]; then
110	        echo "setting up ${VETH1}: namespace: ${NS1}"
111	fi
112
113	if [[ $busy_poll -eq 1 ]]; then
114	        echo 2 > /sys/class/net/${VETH0}/napi_defer_hard_irqs
115		echo 200000 > /sys/class/net/${VETH0}/gro_flush_timeout
116		echo 2 > /sys/class/net/${VETH1}/napi_defer_hard_irqs
117		echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout
118	fi
119
120	ip link set ${VETH1} netns ${NS1}
121	ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
122	ip link set ${VETH0} mtu ${MTU}
123	ip netns exec ${NS1} ip link set ${VETH1} up
124	ip netns exec ${NS1} ip link set dev lo up
125	ip link set ${VETH0} up
126}
127
128validate_root_exec
129validate_veth_support ${VETH0}
130validate_ip_utility
131setup_vethPairs
132
133retval=$?
134if [ $retval -ne 0 ]; then
135	test_status $retval "${TEST_NAME}"
136	cleanup_exit ${VETH0} ${VETH1} ${NS1}
137	exit $retval
138fi
139
140if [[ $verbose -eq 1 ]]; then
141	ARGS+="-v "
142fi
143
144if [[ $dump_pkts -eq 1 ]]; then
145	ARGS="-D "
146fi
147
148test_status $retval "${TEST_NAME}"
149
150## START TESTS
151
152statusList=()
153
154TEST_NAME="XSK_SELFTESTS_SOFTIRQ"
155
156execxdpxceiver
157
158retval=$?
159test_status $retval "${TEST_NAME}"
160statusList+=($retval)
161
162cleanup_exit ${VETH0} ${VETH1} ${NS1}
163TEST_NAME="XSK_SELFTESTS_BUSY_POLL"
164busy_poll=1
165
166setup_vethPairs
167execxdpxceiver
168
169retval=$?
170test_status $retval "${TEST_NAME}"
171statusList+=($retval)
172
173## END TESTS
174
175cleanup_exit ${VETH0} ${VETH1} ${NS1}
176
177for _status in "${statusList[@]}"
178do
179	if [ $_status -ne 0 ]; then
180		test_exit $ksft_fail 0
181	fi
182done
183
184test_exit $ksft_pass 0
185