• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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		echo 1 > /proc/sys/net/ipv6/conf/${VETH1}/disable_ipv6
110	fi
111	if [[ $verbose -eq 1 ]]; then
112	        echo "setting up ${VETH1}: namespace: ${NS1}"
113	fi
114	ip link set ${VETH1} netns ${NS1}
115	ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
116	ip link set ${VETH0} mtu ${MTU}
117	ip netns exec ${NS1} ip link set ${VETH1} up
118	ip netns exec ${NS1} ip link set dev lo up
119	ip link set ${VETH0} up
120}
121
122validate_root_exec
123validate_veth_support ${VETH0}
124validate_ip_utility
125setup_vethPairs
126
127retval=$?
128if [ $retval -ne 0 ]; then
129	test_status $retval "${TEST_NAME}"
130	cleanup_exit ${VETH0} ${VETH1} ${NS1}
131	exit $retval
132fi
133
134echo "${VETH0}:${VETH1},${NS1}" > ${SPECFILE}
135
136validate_veth_spec_file
137
138if [[ $verbose -eq 1 ]]; then
139        echo "Spec file created: ${SPECFILE}"
140	VERBOSE_ARG="-v"
141fi
142
143if [[ $dump_pkts -eq 1 ]]; then
144	DUMP_PKTS_ARG="-D"
145fi
146
147test_status $retval "${TEST_NAME}"
148
149## START TESTS
150
151statusList=()
152
153TEST_NAME="XSK KSELFTESTS"
154
155execxdpxceiver
156
157retval=$?
158test_status $retval "${TEST_NAME}"
159statusList+=($retval)
160
161## END TESTS
162
163cleanup_exit ${VETH0} ${VETH1} ${NS1}
164
165for _status in "${statusList[@]}"
166do
167	if [ $_status -ne 0 ]; then
168		test_exit $ksft_fail 0
169	fi
170done
171
172test_exit $ksft_pass 0
173