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			{ ip netns exec $3 ip link del $2; }
86		ip netns del $3
87	fi
88	#Once we delete a veth pair node, the entire veth pair is removed,
89	#this is just to be cautious just incase the NS does not exist then
90	#veth node inside NS won't get removed so we explicitly remove it
91	[ $(ip link show $1 &>/dev/null; echo $?;) == 0 ] &&
92		{ ip link del $1; }
93	if [ -f ${SPECFILE} ]; then
94		rm -f ${SPECFILE}
95	fi
96}
97
98cleanup_exit()
99{
100	clear_configs $1 $2 $3
101}
102
103validate_ip_utility()
104{
105	[ ! $(type -P ip) ] && { echo "'ip' not found. Skipping tests."; test_exit $ksft_skip 1; }
106}
107
108execxdpxceiver()
109{
110	./${XSKOBJ} -i ${VETH0} -i ${VETH1},${NS1} -C ${NUMPKTS} ${VERBOSE_ARG} ${DUMP_PKTS_ARG}
111}
112