public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
From: Michael Tremer <michael.tremer@ipfire.org>
To: development@lists.ipfire.org
Cc: Michael Tremer <michael.tremer@ipfire.org>
Subject: [PATCH 2/6] network: Add support for bonds
Date: Tue, 29 Jul 2025 14:42:16 +0000	[thread overview]
Message-ID: <20250729144220.1332214-2-michael.tremer@ipfire.org> (raw)
In-Reply-To: <20250729144220.1332214-1-michael.tremer@ipfire.org>

This is a bare-minimum implementation to realise this. It changes the
bridge script because the two of them have quite a bit in common, so we
should avoid further code duplication.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 config/udev/network-hotplug-bridges | 97 +++++++++++++++++++++--------
 config/udev/network-hotplug-rename  |  2 +-
 2 files changed, 71 insertions(+), 28 deletions(-)

diff --git a/config/udev/network-hotplug-bridges b/config/udev/network-hotplug-bridges
index 39faeb5a9..4fa1154c2 100644
--- a/config/udev/network-hotplug-bridges
+++ b/config/udev/network-hotplug-bridges
@@ -71,57 +71,86 @@ ZONE=$(detect_zone)
 
 # Cannot proceed if we could not find a zone
 if [ -z "${ZONE}" ]; then
-	logger "Could not find a bridged zone for ${INTERFACE}"
+	logger "Could not find a master zone for ${INTERFACE}"
 	exit 0
 fi
 
 # Determine the mode of this zone
 MODE="$(get_value "${ZONE}_MODE")"
 
-# The name of the virtual bridge
-BRIDGE="$(get_value "${ZONE}_DEV")"
+# Exit if there is no MODE
+if [ -z "${MODE}" ]; then
+       exit 0
+fi
+
+# The name of the virtual master interface
+MASTER="$(get_value "${ZONE}_DEV")"
+
+# Fetch the MTU
 MTU="$(get_value "${ZONE}_MTU")"
-STP="$(get_value "${ZONE}_STP")"
-STP_PRIORITY="$(get_value "${ZONE}_STP_PRIORITY")"
+
+# Set default MTU if nothing is set
+if [ -z "${MTU}" ]; then
+	MTU=1500
+fi
+
+# Fetch the MAC address of the master interface
+ADDRESS="$(get_value "${ZONE}_MACADDR")"
+
+# If no address has been configured, generate a random one
+if [ -z "${ADDRESS}" ]; then
+	ADDRESS="$(random_mac_address)"
+fi
 
 case "${MODE}" in
-	bridge)
-		# Set default MTU if nothing is set
-		if [ -z "${MTU}" ]; then
-			MTU=1500
+	# Bond
+	bond)
+		BOND_MODE="$(get_value "${ZONE}_BOND_MODE")"
+		if [ -z "${BOND_MODE}" ]; then
+			BOND_MODE="802.3ad"
+		fi
+
+		# Check for some valid BOND_MODE
+		case "${BOND_MODE}" in
+			balance-rr|active-backup|balance-xor|broadcast|802.3ad|balance-tlb|balance-alb)
+				;;
+			*)
+				logger "Invalid bond mode ${BOND_MODE} for ${MASTER}. Falling back to 802.3ad"
+				BOND_MODE="802.3ad"
+				;;
+		esac
+
+		# Create the master interface if it does not exist
+		if [ ! -d "/sys/class/net/${MASTER}" ]; then
+			if ! ip link add "${MASTER}" address "${ADDRESS}" mtu "${MTU}" \
+					type bond mode "${BOND_MODE}"; then
+				logger "Failed to create bonding interface ${MASTER}"
+				exit 1
+			fi
 		fi
+		;;
+
+	# Bridge
+	bridge)
+		# Fetch spanning tree settings
+		STP="$(get_value "${ZONE}_STP")"
+		STP_PRIORITY="$(get_value "${ZONE}_STP_PRIORITY")"
 
 		# We need to check if $STP_PRIORITY has a valid value if not set it
 		if [ -z "${STP_PRIORITY}" ]; then
 			STP_PRIORITY=16384
 		fi
 
-		ADDRESS="$(get_value "${ZONE}_MACADDR")"
-		[ -n "${ADDRESS}" ] || ADDRESS="$(random_mac_address)"
-
 		# We need to create the bridge if it doesn't exist, yet
-		if [ ! -d "/sys/class/net/${BRIDGE}" ]; then
-			ip link add "${BRIDGE}" address "${ADDRESS}" mtu "${MTU}" type bridge \
+		if [ ! -d "/sys/class/net/${MASTER}" ]; then
+			ip link add "${MASTER}" address "${ADDRESS}" mtu "${MTU}" type bridge \
 				$([ "${STP}" = "on" ] && echo "stp_state 1  priority ${STP_PRIORITY}" )
-			#ip link set "${BRIDGE}" up
 		fi
 
 		# Try setting wireless interfaces into master mode
 		if [ -d "/sys/class/net/${INTERFACE}/phy80211" ]; then
 			iw dev "${INTERFACE}" set type __ap
 		fi
-
-		# Attempt to set the MTU
-		ip link set dev "${INTERFACE}" mtu "${MTU}"
-
-		# Attach the physical device
-		logger "Attach ${INTERFACE} to ${BRIDGE}"
-		ip link set dev "${INTERFACE}" master "${BRIDGE}"
-		ip link set dev "${INTERFACE}" up
-		;;
-
-	"")
-		exit 0
 		;;
 
 	*)
@@ -129,3 +158,17 @@ case "${MODE}" in
 		exit 1
 		;;
 esac
+
+# Attempt to set the MTU
+ip link set dev "${INTERFACE}" mtu "${MTU}"
+
+# Ensure the physical interface is down
+ip link set dev "${INTERFACE}" down
+
+# Attach the physical device
+logger "Attach ${INTERFACE} to ${MASTER}"
+ip link set dev "${INTERFACE}" master "${MASTER}"
+ip link set dev "${INTERFACE}" up
+
+# Done!
+exit 0
diff --git a/config/udev/network-hotplug-rename b/config/udev/network-hotplug-rename
index 7c81bdb78..f27eecae6 100644
--- a/config/udev/network-hotplug-rename
+++ b/config/udev/network-hotplug-rename
@@ -78,7 +78,7 @@ for zone in ${ZONES}; do
 	# If a matching interface has been found we will
 	# print the name to which udev will rename it.
 	case "${!mode}" in
-		bridge)
+		bond|bridge)
 			counter=0
 			for slave in ${!slaves}; do
 				if [ "${slave,,}" = "${ADDRESS,,}" ]; then
-- 
2.47.2



  reply	other threads:[~2025-07-29 14:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-29 14:42 [PATCH 1/6] linux: Don't create bond0 when bonding is being loaded Michael Tremer
2025-07-29 14:42 ` Michael Tremer [this message]
2025-07-29 14:42 ` [PATCH 3/6] network: Rename the bridge hotplug script Michael Tremer
2025-07-29 14:42 ` [PATCH 4/6] network: Fail if no master device has been configured for slave zones Michael Tremer
2025-07-29 14:42 ` [PATCH 5/6] network: Add support for some more auxiliary zones Michael Tremer
2025-07-29 14:42 ` [PATCH 6/6] network: Ensure that we only run once at a time Michael Tremer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250729144220.1332214-2-michael.tremer@ipfire.org \
    --to=michael.tremer@ipfire.org \
    --cc=development@lists.ipfire.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox