Skip to content

Commit 71379e1

Browse files
liuhangbinkuba-moo
authored andcommitted
selftests: bonding: add fail_over_mac testing
Add a test to check each value of bond fail_over_mac option. Also fix a minor garp_test print issue. Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> Link: https://patch.msgid.link/20250910024336.400253-2-liuhangbin@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 35ae4e8 commit 71379e1

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

tools/testing/selftests/drivers/net/bonding/bond_options.sh

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ALL_TESTS="
77
prio
88
arp_validate
99
num_grat_arp
10+
fail_over_mac
1011
"
1112

1213
lib_dir=$(dirname "$0")
@@ -352,8 +353,8 @@ garp_test()
352353

353354
exp_num=$(echo "${param}" | cut -f6 -d ' ')
354355
active_slave=$(cmd_jq "ip -n ${s_ns} -d -j link show bond0" ".[].linkinfo.info_data.active_slave")
355-
slowwait_for_counter $((exp_num + 5)) $exp_num \
356-
tc_rule_handle_stats_get "dev s${active_slave#eth} ingress" 101 ".packets" "-n ${g_ns}"
356+
slowwait_for_counter $((exp_num + 5)) $exp_num tc_rule_handle_stats_get \
357+
"dev s${active_slave#eth} ingress" 101 ".packets" "-n ${g_ns}" &> /dev/null
357358

358359
# check result
359360
real_num=$(tc_rule_handle_stats_get "dev s${active_slave#eth} ingress" 101 ".packets" "-n ${g_ns}")
@@ -376,6 +377,140 @@ num_grat_arp()
376377
done
377378
}
378379

380+
check_all_mac_same()
381+
{
382+
RET=0
383+
# all slaves should have same mac address (with the first port's mac)
384+
local bond_mac=$(ip -n "$s_ns" -j link show bond0 | jq -r '.[]["address"]')
385+
local eth0_mac=$(ip -n "$s_ns" -j link show eth0 | jq -r '.[]["address"]')
386+
local eth1_mac=$(ip -n "$s_ns" -j link show eth1 | jq -r '.[]["address"]')
387+
local eth2_mac=$(ip -n "$s_ns" -j link show eth2 | jq -r '.[]["address"]')
388+
if [ "$bond_mac" != "${mac[0]}" ] || [ "$eth0_mac" != "$bond_mac" ] || \
389+
[ "$eth1_mac" != "$bond_mac" ] || [ "$eth2_mac" != "$bond_mac" ]; then
390+
RET=1
391+
fi
392+
}
393+
394+
check_bond_mac_same_with_first()
395+
{
396+
RET=0
397+
# bond mac address should be same with the first added slave
398+
local bond_mac=$(ip -n "$s_ns" -j link show bond0 | jq -r '.[]["address"]')
399+
if [ "$bond_mac" != "${mac[0]}" ]; then
400+
RET=1
401+
fi
402+
}
403+
404+
check_bond_mac_same_with_active()
405+
{
406+
RET=0
407+
# bond mac address should be same with active slave
408+
local bond_mac=$(ip -n "$s_ns" -j link show bond0 | jq -r '.[]["address"]')
409+
local active_slave=$(cmd_jq "ip -n ${s_ns} -d -j link show bond0" ".[].linkinfo.info_data.active_slave")
410+
local active_slave_mac=$(ip -n "$s_ns" -j link show "$active_slave" | jq -r '.[]["address"]')
411+
if [ "$bond_mac" != "$active_slave_mac" ]; then
412+
RET=1
413+
fi
414+
}
415+
416+
check_backup_slave_mac_not_change()
417+
{
418+
RET=0
419+
# backup slave's mac address is not changed
420+
if ip -n "$s_ns" -d -j link show type bond_slave | jq -e '.[]
421+
| select(.linkinfo.info_slave_data.state=="BACKUP")
422+
| select(.address != .linkinfo.info_slave_data.perm_hwaddr)' &> /dev/null; then
423+
RET=1
424+
fi
425+
}
426+
427+
check_backup_slave_mac_inherit()
428+
{
429+
local backup_mac
430+
RET=0
431+
432+
# backup slaves should use mac[1] or mac[2]
433+
local backup_macs=$(ip -n "$s_ns" -d -j link show type bond_slave | \
434+
jq -r '.[] | select(.linkinfo.info_slave_data.state=="BACKUP") | .address')
435+
for backup_mac in $backup_macs; do
436+
if [ "$backup_mac" != "${mac[1]}" ] && [ "$backup_mac" != "${mac[2]}" ]; then
437+
RET=1
438+
fi
439+
done
440+
}
441+
442+
check_first_slave_random_mac()
443+
{
444+
RET=0
445+
# remove the first added slave and added it back
446+
ip -n "$s_ns" link set eth0 nomaster
447+
ip -n "$s_ns" link set eth0 master bond0
448+
449+
# the first slave should use random mac address
450+
eth0_mac=$(ip -n "$s_ns" -j link show eth0 | jq -r '.[]["address"]')
451+
[ "$eth0_mac" = "${mac[0]}" ] && RET=1
452+
log_test "bond fail_over_mac follow" "random first slave mac"
453+
454+
# remove the first slave, the permanent MAC address should be restored back
455+
ip -n "$s_ns" link set eth0 nomaster
456+
eth0_mac=$(ip -n "$s_ns" -j link show eth0 | jq -r '.[]["address"]')
457+
[ "$eth0_mac" != "${mac[0]}" ] && RET=1
458+
}
459+
460+
do_active_backup_failover()
461+
{
462+
local active_slave=$(cmd_jq "ip -n ${s_ns} -d -j link show bond0" ".[].linkinfo.info_data.active_slave")
463+
ip -n ${s_ns} link set ${active_slave} down
464+
slowwait 2 active_slave_changed $active_slave
465+
ip -n ${s_ns} link set ${active_slave} up
466+
}
467+
468+
fail_over_mac()
469+
{
470+
# Bring down the first interface on the switch to force the bond to
471+
# select another active interface instead of the first one that joined.
472+
ip -n "$g_ns" link set s0 down
473+
474+
# fail_over_mac none
475+
bond_reset "mode active-backup miimon 100 fail_over_mac 0"
476+
check_all_mac_same
477+
log_test "fail_over_mac 0" "all slaves have same mac"
478+
do_active_backup_failover
479+
check_all_mac_same
480+
log_test "fail_over_mac 0" "failover: all slaves have same mac"
481+
482+
# fail_over_mac active
483+
bond_reset "mode active-backup miimon 100 fail_over_mac 1"
484+
check_bond_mac_same_with_active
485+
log_test "fail_over_mac 1" "bond mac is same with active slave mac"
486+
check_backup_slave_mac_not_change
487+
log_test "fail_over_mac 1" "backup slave mac is not changed"
488+
do_active_backup_failover
489+
check_bond_mac_same_with_active
490+
log_test "fail_over_mac 1" "failover: bond mac is same with active slave mac"
491+
check_backup_slave_mac_not_change
492+
log_test "fail_over_mac 1" "failover: backup slave mac is not changed"
493+
494+
# fail_over_mac follow
495+
bond_reset "mode active-backup miimon 100 fail_over_mac 2"
496+
check_bond_mac_same_with_first
497+
log_test "fail_over_mac 2" "bond mac is same with first slave mac"
498+
check_bond_mac_same_with_active
499+
log_test "fail_over_mac 2" "bond mac is same with active slave mac"
500+
check_backup_slave_mac_inherit
501+
log_test "fail_over_mac 2" "backup slave mac inherit"
502+
do_active_backup_failover
503+
check_bond_mac_same_with_first
504+
log_test "fail_over_mac 2" "failover: bond mac is same with first slave mac"
505+
check_bond_mac_same_with_active
506+
log_test "fail_over_mac 2" "failover: bond mac is same with active slave mac"
507+
check_backup_slave_mac_inherit
508+
log_test "fail_over_mac 2" "failover: backup slave mac inherit"
509+
check_first_slave_random_mac
510+
log_test "fail_over_mac 2" "first slave mac random"
511+
512+
}
513+
379514
trap cleanup EXIT
380515

381516
setup_prepare

tools/testing/selftests/drivers/net/bonding/bond_topo_2d1c.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ g_ip4="192.0.2.254"
3939
s_ip6="2001:db8::1"
4040
c_ip6="2001:db8::10"
4141
g_ip6="2001:db8::254"
42+
mac[0]="00:0a:0b:0c:0d:01"
43+
mac[1]="00:0a:0b:0c:0d:02"
4244

4345
gateway_create()
4446
{
@@ -62,6 +64,7 @@ server_create()
6264

6365
for i in $(seq 0 1); do
6466
ip -n ${s_ns} link add eth${i} type veth peer name s${i} netns ${g_ns}
67+
ip -n "${s_ns}" link set "eth${i}" addr "${mac[$i]}"
6568

6669
ip -n ${g_ns} link set s${i} up
6770
ip -n ${g_ns} link set s${i} master br0

tools/testing/selftests/drivers/net/bonding/bond_topo_3d1c.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# +-------------------------------------+
2727

2828
source bond_topo_2d1c.sh
29+
mac[2]="00:0a:0b:0c:0d:03"
2930

3031
setup_prepare()
3132
{
@@ -36,6 +37,7 @@ setup_prepare()
3637
# Add the extra device as we use 3 down links for bond0
3738
local i=2
3839
ip -n ${s_ns} link add eth${i} type veth peer name s${i} netns ${g_ns}
40+
ip -n "${s_ns}" link set "eth${i}" addr "${mac[$i]}"
3941
ip -n ${g_ns} link set s${i} up
4042
ip -n ${g_ns} link set s${i} master br0
4143
ip -n ${s_ns} link set eth${i} master bond0

0 commit comments

Comments
 (0)