I want to run some tests using linux network bonding. I am using qemu VMs and I am using a custom-built kernel to run them where I set it up to have bonding statically linked. I see in the documentation that it's possible to set up parameters for bonding (like miimon) when loading the module. But how can these values be set when it's statically linked?
1 Answer
There are two APIs to handle bonding interfaces, in addition to the obsolete commands (on Linux) ifconfig and ifenslave, which probably aren't able to create a new bond interfaces (and thus require the bonding module's max_bonds parameter to be non zero).
kernel's (rt)netlink API: through most of the modern commands provided by iproute2 including the
ip linkcommand.kernel's sysfs API via pseudo-files: usually mounted in
/sys/.
I can note that "recent" versions of ifenslave actually rely on the the sysfs API and a little on the rtnetlink API through ip link.
You should probably configure bonding to create zero interface instead of the default one or two: this is an historical feature that became obsolete with these two APIs. These interface can be created or removed dynamically later. Of course for a very minimal system leaving it or setting it to create the required needed number is still an option. Adding this to the kernel command line options (usually found in /etc/default/grub's GRUB_CMDLINE_LINUX= entry, but doing this correctly might depend on the distribution) should do it:
bonding.max_bonds=0 Configuring Bonding Manually with iproute2 (
ip link):Warning: the linked documentation is quite incomplete/obsolete and doesn't show that most if not all of the bonding features are available through
ip link.You can create delete alter settings enslave etc. with the right
ip linkcommand. This command should include the keywordstype bondwhenever it has to specify a specific bond option after them.command syntax reminder, to display specific bond settings:
ip link add type bond helpexample (interfaces must be down before being enslaved, but can be set such in the same single command in case former state is not known):
ip link add dev mybond0 type bond mode active-backup miimon 100 ip link set dev eth0 down master mybond0 ip link set dev eth1 down master mybond0 ip link set dev mybond0 up ip link set dev eth0 up ip link set dev eth1 upchanging settings:
ip link set dev mybond0 type bond miimon 200ifenslaveequivalent:ip link set dev mybond0 type bond active_slave eth1some settings can't be changed while there are any enslaved devices. Eg, redefining the bond type:
# ip link set dev mybond0 type bond mode balance-rr RTNETLINK answers: Directory not emptyenslaved devices can be set free with:
ip link set dev eth0 nomasterand the bond device can be deleted with:
ip link delete dev mybond0various information related to a bond device (or to a bond_slave device) can be seen with the additional
-detailsoption. At some point using JSON output can be easier for scripts (eg, this Q/A: iproute2: How to display the TYPE of a network devices?).ip -detail link show dev mybond0 ip -detail link show dev eth0 ip -detail link show dev eth1Configuring Bonding Manually via Sysfs
I don't think using this API makes much sense today unless a specific feature wasn't made available through
ip link. It was probably created before iproute2 tools could handle all of it. Maybe reading from it could still help in case the tools on a restricted/embedded system don't have anymore access to anip linkcommand, but even this seems unlikely.As described in the linked documentation, you can create and delete bonding interfaces through
/sys/class/net/bonding_masterswhich is present whenever the bonding module is present (here as built-in).Here are the equivalent commands from above, simply using
echo ... > ...from a root shell (or having to work around redirections withsudo echo ... | tee ...):echo +mybond0 > /sys/class/net/bonding_masters echo active-backup > /sys/class/net/mybond0/bonding/mode echo 100 > /sys/class/net/mybond0/bonding/miimonI don't see any way to set interfaces up or down through sysfs anyway. To stay safe:
ip link set dev eth0 down ip link set dev eth1 down echo +eth0 > /sys/class/net/mybond0/bonding/slaves echo +eth1 > /sys/class/net/mybond0/bonding/slavesAgain, there's no other way:
ip link set dev mybond0 up ip link set dev eth0 up ip link set dev eth1 upThen continuing like the previous example:
echo 200 > /sys/class/net/mybond0/bonding/miimon echo eth1 > /sys/class/net/mybond0/bonding/active_slave # echo balance-rr > /sys/class/net/mybond0/bonding/mode bash: echo: write error: Directory not empty echo -eth0 > /sys/class/net/mybond0/bonding/slaves echo -mybond0 > /sys/class/net/bonding_mastersSpecific bonding information is available there:
cat /sys/class/net/bonding_masters grep ^ /sys/class/net/mybond0/bonding/* grep ^ /sys/class/net/eth0/bonding_slave/* grep ^ /sys/class/net/eth1/bonding_slave/*
- It seems to be working fine through iproute2.eftshift0– eftshift02021-05-15 03:11:31 +00:00Commented May 15, 2021 at 3:11