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
11SPECFILE=veth.spec
12XSKOBJ=xdpxceiver
13
14validate_root_exec()
15{
16	msg="skip all tests:"
17	if [ $UID != 0 ]; then
18		echo $msg must be run as root >&2
19		test_exit $ksft_fail 2
20	else
21		return $ksft_pass
22	fi
23}
24
25validate_veth_support()
26{
27	msg="skip all tests:"
28	if [ $(ip link add $1 type veth 2>/dev/null; echo $?;) != 0 ]; then
29		echo $msg veth kernel support not available >&2
30		test_exit $ksft_skip 1
31	else
32		ip link del $1
33		return $ksft_pass
34	fi
35}
36
37validate_veth_spec_file()
38{
39	if [ ! -f ${SPECFILE} ]; then
40		test_exit $ksft_skip 1
41	fi
42}
43
44test_status()
45{
46	statusval=$1
47	if [ $statusval -eq 2 ]; then
48		echo -e "$2: [ FAIL ]"
49	elif [ $statusval -eq 1 ]; then
50		echo -e "$2: [ SKIPPED ]"
51	elif [ $statusval -eq 0 ]; then
52		echo -e "$2: [ PASS ]"
53	fi
54}
55
56test_exit()
57{
58	retval=$1
59	if [ $2 -ne 0 ]; then
60		test_status $2 $(basename $0)
61	fi
62	exit $retval
63}
64
65clear_configs()
66{
67	if [ $(ip netns show | grep $3 &>/dev/null; echo $?;) == 0 ]; then
68		[ $(ip netns exec $3 ip link show $2 &>/dev/null; echo $?;) == 0 ] &&
69			{ ip netns exec $3 ip link del $2; }
70		ip netns del $3
71	fi
72	#Once we delete a veth pair node, the entire veth pair is removed,
73	#this is just to be cautious just incase the NS does not exist then
74	#veth node inside NS won't get removed so we explicitly remove it
75	[ $(ip link show $1 &>/dev/null; echo $?;) == 0 ] &&
76		{ ip link del $1; }
77	if [ -f ${SPECFILE} ]; then
78		rm -f ${SPECFILE}
79	fi
80}
81
82cleanup_exit()
83{
84	clear_configs $1 $2 $3
85}
86
87validate_ip_utility()
88{
89	[ ! $(type -P ip) ] && { echo "'ip' not found. Skipping tests."; test_exit $ksft_skip 1; }
90}
91
92execxdpxceiver()
93{
94	./${XSKOBJ} -i ${VETH0} -i ${VETH1},${NS1} ${VERBOSE_ARG} ${DUMP_PKTS_ARG}
95}
96