xref: /linux/tools/testing/selftests/bpf/xsk_prereqs.sh (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright(c) 2020 Intel Corporation.
4
5ksft_pass=0
6ksft_fail=1
7ksft_xfail=2
8ksft_xpass=3
9ksft_skip=4
10
11GREEN='\033[0;92m'
12YELLOW='\033[0;93m'
13RED='\033[0;31m'
14NC='\033[0m'
15STACK_LIM=131072
16SPECFILE=veth.spec
17XSKOBJ=xdpxceiver
18NUMPKTS=10000
19
20validate_root_exec()
21{
22	msg="skip all tests:"
23	if [ $UID != 0 ]; then
24		echo $msg must be run as root >&2
25		test_exit $ksft_fail 2
26	else
27		return $ksft_pass
28	fi
29}
30
31validate_veth_support()
32{
33	msg="skip all tests:"
34	if [ $(ip link add $1 type veth 2>/dev/null; echo $?;) != 0 ]; then
35		echo $msg veth kernel support not available >&2
36		test_exit $ksft_skip 1
37	else
38		ip link del $1
39		return $ksft_pass
40	fi
41}
42
43validate_veth_spec_file()
44{
45	if [ ! -f ${SPECFILE} ]; then
46		test_exit $ksft_skip 1
47	fi
48}
49
50test_status()
51{
52	statusval=$1
53	if [ -n "${colorconsole+set}" ]; then
54		if [ $statusval -eq 2 ]; then
55			echo -e "${YELLOW}$2${NC}: [ ${RED}FAIL${NC} ]"
56		elif [ $statusval -eq 1 ]; then
57			echo -e "${YELLOW}$2${NC}: [ ${RED}SKIPPED${NC} ]"
58		elif [ $statusval -eq 0 ]; then
59			echo -e "${YELLOW}$2${NC}: [ ${GREEN}PASS${NC} ]"
60		fi
61	else
62		if [ $statusval -eq 2 ]; then
63			echo -e "$2: [ FAIL ]"
64		elif [ $statusval -eq 1 ]; then
65			echo -e "$2: [ SKIPPED ]"
66		elif [ $statusval -eq 0 ]; then
67			echo -e "$2: [ PASS ]"
68		fi
69	fi
70}
71
72test_exit()
73{
74	retval=$1
75	if [ $2 -ne 0 ]; then
76		test_status $2 $(basename $0)
77	fi
78	exit $retval
79}
80
81clear_configs()
82{
83	if [ $(ip netns show | grep $3 &>/dev/null; echo $?;) == 0 ]; then
84		[ $(ip netns exec $3 ip link show $2 &>/dev/null; echo $?;) == 0 ] &&
85			{ echo "removing link $1:$2"; ip netns exec $3 ip link del $2; }
86		echo "removing ns $3"
87		ip netns del $3
88	fi
89	#Once we delete a veth pair node, the entire veth pair is removed,
90	#this is just to be cautious just incase the NS does not exist then
91	#veth node inside NS won't get removed so we explicitly remove it
92	[ $(ip link show $1 &>/dev/null; echo $?;) == 0 ] &&
93		{ echo "removing link $1"; ip link del $1; }
94	if [ -f ${SPECFILE} ]; then
95		echo "removing spec file:" ${SPECFILE}
96		rm -f ${SPECFILE}
97	fi
98}
99
100cleanup_exit()
101{
102	echo "cleaning up..."
103	clear_configs $1 $2 $3
104}
105
106validate_ip_utility()
107{
108	[ ! $(type -P ip) ] && { echo "'ip' not found. Skipping tests."; test_exit $ksft_skip 1; }
109}
110
111vethXDPgeneric()
112{
113	ip link set dev $1 xdpdrv off
114	ip netns exec $3 ip link set dev $2 xdpdrv off
115}
116
117vethXDPnative()
118{
119	ip link set dev $1 xdpgeneric off
120	ip netns exec $3 ip link set dev $2 xdpgeneric off
121}
122
123execxdpxceiver()
124{
125	local -a 'paramkeys=("${!'"$1"'[@]}")' copy
126	paramkeysstr=${paramkeys[*]}
127
128	for index in $paramkeysstr;
129		do
130			current=$1"[$index]"
131			copy[$index]=${!current}
132		done
133
134	./${XSKOBJ} -i ${VETH0} -i ${VETH1},${NS1} ${copy[*]} -C ${NUMPKTS}
135}
136