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 xskxceiver.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# Run test suite for physical device in loopback mode 78# sudo ./test_xsk.sh -i IFACE 79 80. xsk_prereqs.sh 81 82ETH="" 83 84while getopts "vDi:" flag 85do 86 case "${flag}" in 87 v) verbose=1;; 88 D) dump_pkts=1;; 89 i) ETH=${OPTARG};; 90 esac 91done 92 93TEST_NAME="PREREQUISITES" 94 95URANDOM=/dev/urandom 96[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit $ksft_fail; } 97 98VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4) 99VETH0=ve${VETH0_POSTFIX} 100VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4) 101VETH1=ve${VETH1_POSTFIX} 102NS0=root 103NS1=af_xdp${VETH1_POSTFIX} 104MTU=1500 105 106trap ctrl_c INT 107 108function ctrl_c() { 109 cleanup_exit ${VETH0} ${VETH1} ${NS1} 110 exit 1 111} 112 113setup_vethPairs() { 114 if [[ $verbose -eq 1 ]]; then 115 echo "setting up ${VETH0}: namespace: ${NS0}" 116 fi 117 ip netns add ${NS1} 118 ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4 119 if [ -f /proc/net/if_inet6 ]; then 120 echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6 121 echo 1 > /proc/sys/net/ipv6/conf/${VETH1}/disable_ipv6 122 fi 123 if [[ $verbose -eq 1 ]]; then 124 echo "setting up ${VETH1}: namespace: ${NS1}" 125 fi 126 127 if [[ $busy_poll -eq 1 ]]; then 128 echo 2 > /sys/class/net/${VETH0}/napi_defer_hard_irqs 129 echo 200000 > /sys/class/net/${VETH0}/gro_flush_timeout 130 echo 2 > /sys/class/net/${VETH1}/napi_defer_hard_irqs 131 echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout 132 fi 133 134 ip link set ${VETH1} netns ${NS1} 135 ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU} 136 ip link set ${VETH0} mtu ${MTU} 137 ip netns exec ${NS1} ip link set ${VETH1} up 138 ip netns exec ${NS1} ip link set dev lo up 139 ip link set ${VETH0} up 140} 141 142if [ ! -z $ETH ]; then 143 VETH0=${ETH} 144 VETH1=${ETH} 145 NS1="" 146else 147 validate_root_exec 148 validate_veth_support ${VETH0} 149 validate_ip_utility 150 setup_vethPairs 151 152 retval=$? 153 if [ $retval -ne 0 ]; then 154 test_status $retval "${TEST_NAME}" 155 cleanup_exit ${VETH0} ${VETH1} ${NS1} 156 exit $retval 157 fi 158fi 159 160 161if [[ $verbose -eq 1 ]]; then 162 ARGS+="-v " 163fi 164 165if [[ $dump_pkts -eq 1 ]]; then 166 ARGS="-D " 167fi 168 169retval=$? 170test_status $retval "${TEST_NAME}" 171 172## START TESTS 173 174statusList=() 175 176TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ" 177 178exec_xskxceiver 179 180if [ -z $ETH ]; then 181 cleanup_exit ${VETH0} ${VETH1} ${NS1} 182fi 183TEST_NAME="XSK_SELFTESTS_${VETH0}_BUSY_POLL" 184busy_poll=1 185 186if [ -z $ETH ]; then 187 setup_vethPairs 188fi 189exec_xskxceiver 190 191## END TESTS 192 193if [ -z $ETH ]; then 194 cleanup_exit ${VETH0} ${VETH1} ${NS1} 195fi 196 197failures=0 198echo -e "\nSummary:" 199for i in "${!statusList[@]}" 200do 201 if [ ${statusList[$i]} -ne 0 ]; then 202 test_status ${statusList[$i]} ${nameList[$i]} 203 failures=1 204 fi 205done 206 207if [ $failures -eq 0 ]; then 208 echo "All tests successful!" 209fi 210