aboutsummaryrefslogtreecommitdiff
path: root/test/linux-generic/mmap_vlan_ins/pktio_env
blob: 345b5bd5642ae618bbaae4285c416b64525b4fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/sh
#
# Copyright (c) 2015, Linaro Limited
# All rights reserved.
#
# SPDX-License-Identifier:	BSD-3-Clause
#
# Test script wrapper for running ODP pktio apps on linux-generic.
#
# For linux-generic the default behavior is to create two pairs of
# virtual Ethernet interfaces and provide the names of these via
# environment variables to pktio apps, the interfaces will be removed
# before the script exits.
#
# Note that the creation of virtual Ethernet devices depends on having
# CONFIG_VETH enabled in the kernel, if not enabled the env setup will be skipped.
#
# Network set up
# IF0 <---> IF1
# IF2 <---> IF3
IF0=pktiop0p1
IF1=pktiop1p0
IF2=pktiop2p3
IF3=pktiop3p2

if [ "$0" = "$BASH_SOURCE" ]; then
	echo "Error: Platform specific env file has to be sourced."
fi

check_for_root()
{
	if [ "$(id -u)" != "0" ]; then
		echo "check_for_root(): need to be root to setup VETH"
		return 1
	fi
	return 0
}

# wait for a network interface's operational state to be "up"
wait_for_iface_up()
{
	iface=$1
	cnt=0

	while [ $cnt -lt 50 ]; do
		read operstate < /sys/class/net/$iface/operstate

		if [ $? -ne 0 ]; then
			break
		elif [ "$operstate" = "up" ]; then
			return 0
		fi

		sleep 0.1
		cnt=`expr $cnt + 1`
	done

	return 1
}

setup_pktio_env()
{
	echo "pktio: setting up test interfaces $IF0, $IF1, $IF2, $IF3."

	check_for_root
	if [ $? -ne 0 ]; then
		return 1
	fi

	for iface in $IF0 $IF1 $IF2 $IF3; do
		ip link show $iface 2> /dev/null
		if [ $? -eq 0 ]; then
			echo "pktio: interface $iface already exist $?"
			return 2
		fi
	done

	if [ "$1" = "clean" ]; then
		trap cleanup_pktio_env EXIT
	fi

	ip link add $IF0 type veth peer name $IF1
	if [ $? -ne 0 ]; then
		echo "pktio: error: unable to create veth pair"
		return 3
	fi
	ip link add $IF2 type veth peer name $IF3
	if [ $? -ne 0 ]; then
		echo "pktio: error: unable to create veth pair"
		return 4
	fi

	for iface in $IF0 $IF1 $IF2 $IF3; do
		ip link set $iface mtu 9216 up
		ifconfig $iface -arp
	done

	# check that the interface has come up before starting the test
	for iface in $IF0 $IF1 $IF2 $IF3; do
		wait_for_iface_up $iface
		if [ $? -ne 0 ]; then
			echo "pktio: interface $iface failed to come up"
			return 5
		fi
	done
}

cleanup_pktio_env()
{
	echo "pktio: removing test interfaces $IF0, $IF1, $IF2, $IF3"
	check_for_root
	if [ $? -ne 0 ]; then
		return 1
	fi

	for iface in $IF0 $IF1 $IF2 $IF3; do
		ip link del $iface 2> /dev/null
	done
	return 0
}