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