1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Test bond device ether type changing
5#
6
7ALL_TESTS="
8	bond_test_unsuccessful_enslave_type_change
9	bond_test_successful_enslave_type_change
10"
11REQUIRE_MZ=no
12NUM_NETIFS=0
13lib_dir=$(dirname "$0")
14source "$lib_dir"/../../../net/forwarding/lib.sh
15
16bond_check_flags()
17{
18	local bonddev=$1
19
20	ip -d l sh dev "$bonddev" | grep -q "MASTER"
21	check_err $? "MASTER flag is missing from the bond device"
22
23	ip -d l sh dev "$bonddev" | grep -q "SLAVE"
24	check_err $? "SLAVE flag is missing from the bond device"
25}
26
27# test enslaved bond dev type change from ARPHRD_ETHER and back
28# this allows us to test both MASTER and SLAVE flags at once
29bond_test_enslave_type_change()
30{
31	local test_success=$1
32	local devbond0="test-bond0"
33	local devbond1="test-bond1"
34	local devbond2="test-bond2"
35	local nonethdev="test-noneth0"
36
37	# create a non-ARPHRD_ETHER device for testing (e.g. nlmon type)
38	ip link add name "$nonethdev" type nlmon
39	check_err $? "could not create a non-ARPHRD_ETHER device (nlmon)"
40	ip link add name "$devbond0" type bond
41	if [ $test_success -eq 1 ]; then
42		# we need devbond0 in active-backup mode to successfully enslave nonethdev
43		ip link set dev "$devbond0" type bond mode active-backup
44		check_err $? "could not change bond mode to active-backup"
45	fi
46	ip link add name "$devbond1" type bond
47	ip link add name "$devbond2" type bond
48	ip link set dev "$devbond0" master "$devbond1"
49	check_err $? "could not enslave $devbond0 to $devbond1"
50	# change bond type to non-ARPHRD_ETHER
51	ip link set dev "$nonethdev" master "$devbond0" 1>/dev/null 2>/dev/null
52	ip link set dev "$nonethdev" nomaster 1>/dev/null 2>/dev/null
53	# restore ARPHRD_ETHER type by enslaving such device
54	ip link set dev "$devbond2" master "$devbond0"
55	check_err $? "could not enslave $devbond2 to $devbond0"
56
57	bond_check_flags "$devbond0"
58
59	# clean up
60	ip link del dev "$devbond0"
61	ip link del dev "$devbond1"
62	ip link del dev "$devbond2"
63	ip link del dev "$nonethdev"
64}
65
66bond_test_unsuccessful_enslave_type_change()
67{
68	RET=0
69
70	bond_test_enslave_type_change 0
71	log_test "Change ether type of an enslaved bond device with unsuccessful enslave"
72}
73
74bond_test_successful_enslave_type_change()
75{
76	RET=0
77
78	bond_test_enslave_type_change 1
79	log_test "Change ether type of an enslaved bond device with successful enslave"
80}
81
82tests_run
83
84exit "$EXIT_STATUS"
85