xref: /openbmc/linux/tools/testing/selftests/net/setup_loopback.sh (revision 7d1575014a63caeebb13b000ee152ce711580119)
1*7d157501SCoco Li#!/bin/bash
2*7d157501SCoco Li# SPDX-License-Identifier: GPL-2.0
3*7d157501SCoco Linetdev_check_for_carrier() {
4*7d157501SCoco Li	local -r dev="$1"
5*7d157501SCoco Li
6*7d157501SCoco Li	for i in {1..5}; do
7*7d157501SCoco Li		carrier="$(cat /sys/class/net/${dev}/carrier)"
8*7d157501SCoco Li		if [[ "${carrier}" -ne 1 ]] ; then
9*7d157501SCoco Li			echo "carrier not ready yet..." >&2
10*7d157501SCoco Li			sleep 1
11*7d157501SCoco Li		else
12*7d157501SCoco Li			echo "carrier ready" >&2
13*7d157501SCoco Li			break
14*7d157501SCoco Li		fi
15*7d157501SCoco Li	done
16*7d157501SCoco Li	echo "${carrier}"
17*7d157501SCoco Li}
18*7d157501SCoco Li
19*7d157501SCoco Li# Assumes that there is no existing ipvlan device on the physical device
20*7d157501SCoco Lisetup_loopback_environment() {
21*7d157501SCoco Li    local dev="$1"
22*7d157501SCoco Li
23*7d157501SCoco Li	# Fail hard if cannot turn on loopback mode for current NIC
24*7d157501SCoco Li	ethtool -K "${dev}" loopback on || exit 1
25*7d157501SCoco Li	sleep 1
26*7d157501SCoco Li
27*7d157501SCoco Li	# Check for the carrier
28*7d157501SCoco Li	carrier=$(netdev_check_for_carrier ${dev})
29*7d157501SCoco Li	if [[ "${carrier}" -ne 1 ]] ; then
30*7d157501SCoco Li		echo "setup_loopback_environment failed"
31*7d157501SCoco Li		exit 1
32*7d157501SCoco Li	fi
33*7d157501SCoco Li}
34*7d157501SCoco Li
35*7d157501SCoco Lisetup_macvlan_ns(){
36*7d157501SCoco Li	local -r link_dev="$1"
37*7d157501SCoco Li	local -r ns_name="$2"
38*7d157501SCoco Li	local -r ns_dev="$3"
39*7d157501SCoco Li	local -r ns_mac="$4"
40*7d157501SCoco Li	local -r addr="$5"
41*7d157501SCoco Li
42*7d157501SCoco Li	ip link add link "${link_dev}" dev "${ns_dev}" \
43*7d157501SCoco Li		address "${ns_mac}" type macvlan
44*7d157501SCoco Li	exit_code=$?
45*7d157501SCoco Li	if [[ "${exit_code}" -ne 0 ]]; then
46*7d157501SCoco Li		echo "setup_macvlan_ns failed"
47*7d157501SCoco Li		exit $exit_code
48*7d157501SCoco Li	fi
49*7d157501SCoco Li
50*7d157501SCoco Li	[[ -e /var/run/netns/"${ns_name}" ]] || ip netns add "${ns_name}"
51*7d157501SCoco Li	ip link set dev "${ns_dev}" netns "${ns_name}"
52*7d157501SCoco Li	ip -netns "${ns_name}" link set dev "${ns_dev}" up
53*7d157501SCoco Li	if [[ -n "${addr}" ]]; then
54*7d157501SCoco Li		ip -netns "${ns_name}" addr add dev "${ns_dev}" "${addr}"
55*7d157501SCoco Li	fi
56*7d157501SCoco Li
57*7d157501SCoco Li	sleep 1
58*7d157501SCoco Li}
59*7d157501SCoco Li
60*7d157501SCoco Licleanup_macvlan_ns(){
61*7d157501SCoco Li	while (( $# >= 2 )); do
62*7d157501SCoco Li		ns_name="$1"
63*7d157501SCoco Li		ns_dev="$2"
64*7d157501SCoco Li		ip -netns "${ns_name}" link del dev "${ns_dev}"
65*7d157501SCoco Li		ip netns del "${ns_name}"
66*7d157501SCoco Li		shift 2
67*7d157501SCoco Li	done
68*7d157501SCoco Li}
69*7d157501SCoco Li
70*7d157501SCoco Licleanup_loopback(){
71*7d157501SCoco Li	local -r dev="$1"
72*7d157501SCoco Li
73*7d157501SCoco Li	ethtool -K "${dev}" loopback off
74*7d157501SCoco Li	sleep 1
75*7d157501SCoco Li
76*7d157501SCoco Li	# Check for the carrier
77*7d157501SCoco Li	carrier=$(netdev_check_for_carrier ${dev})
78*7d157501SCoco Li	if [[ "${carrier}" -ne 1 ]] ; then
79*7d157501SCoco Li		echo "setup_loopback_environment failed"
80*7d157501SCoco Li		exit 1
81*7d157501SCoco Li	fi
82*7d157501SCoco Li}
83