device: fix bonding primary port selection
authorNicolò <[email protected]>
Sun, 4 May 2025 15:19:50 +0000 (17:19 +0200)
committerRobert Marko <[email protected]>
Sat, 17 May 2025 09:33:16 +0000 (11:33 +0200)
The confuguration of the primary port was read
 but never used to properly configure the right port.

This patch fix this, and another small fix suggested
 by nbd to properly handle the "reload" of the
 primary interface when is changed.

Co-developed-by: Felix Fietkau <[email protected]>
Signed-off-by: Nicolò Veronese <[email protected]>
Link: https://github.com/openwrt/netifd/pull/49
Signed-off-by: Robert Marko <[email protected]>
bonding.c

index 3b2575184a31bb54748d31dde36fea7de6c514e0..00a7b28320ace64c2a894d39d86625111d05dd0c 100644 (file)
--- a/bonding.c
+++ b/bonding.c
@@ -35,6 +35,7 @@ struct bonding_device {
        struct blob_attr *config_data;
        bool has_macaddr;
        bool force_active;
+       bool reset_primary;
        bool active;
 };
 
@@ -322,6 +323,7 @@ bonding_create_port(struct bonding_device *bdev, const char *name,
                    struct device *dev, bool hotplug)
 {
        struct bonding_port *bp;
+       struct bonding_config *cfg = &bdev->config;
 
        bp = calloc(1, sizeof(*bp) + strlen(name) + 1);
        if (!bp)
@@ -332,6 +334,11 @@ bonding_create_port(struct bonding_device *bdev, const char *name,
        bp->dev.hotplug = hotplug;
        strcpy(bp->name, name);
        bp->dev.dev = dev;
+
+       if (cfg->primary != NULL) {
+               bp->set_primary = strcmp(cfg->primary, name) == 0;
+       }
+
        vlist_add(&bdev->ports, &bp->node, bp->name);
        /*
         * Need to look up the bonding port again as the above
@@ -371,6 +378,11 @@ bonding_config_init(struct device *dev)
        }
        vlist_flush(&bdev->ports);
 
+       if (bdev->reset_primary) {
+               bonding_reset_primary(bdev);
+               bdev->reset_primary = false;
+       }
+
        if (bdev->n_failed)
                uloop_timeout_set(&bdev->retry, 100);
 }
@@ -588,11 +600,20 @@ bonding_port_update(struct vlist_tree *tree, struct vlist_node *node_new,
 {
        struct bonding_port *bp;
        struct device *dev;
+       struct bonding_device *bdev = container_of(tree, struct bonding_device, ports);
 
        if (node_new) {
                bp = container_of(node_new, struct bonding_port, node);
 
                if (node_old) {
+                       struct bonding_port *bp_old;
+
+                       bp_old = container_of(node_old, struct bonding_port, node);
+                       if (bp_old->set_primary != bp->set_primary) {
+                               bp_old->set_primary = bp->set_primary;
+                               bdev->reset_primary = true;
+                       }
+
                        free(bp);
                        return;
                }