Skip to contentSkip to conversation

Bonding: add per port priority support

PATCH Superseded Hangbin Liu 5 participants 15 messages Apr 12, 2022 – May 31, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bonding: add per port priority support

Add per port priority support for bonding. A higher number means higher priority. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

This option could only be configured via netlink.

---
 Documentation/networking/bonding.rst |  9 +++++++++
 drivers/net/bonding/bond_main.c      | 27 +++++++++++++++++++++++++++
 drivers/net/bonding/bond_netlink.c   | 12 ++++++++++++
 include/net/bonding.h                |  1 +
 include/uapi/linux/if_link.h         |  1 +
 tools/include/uapi/linux/if_link.h   |  1 +
 6 files changed, 51 insertions(+)
Documentation/networking/bonding.rst +9 -0
index 525e6842dd33..103e292a04a1 100644
@@ -780,6 +780,15 @@ peer_notif_delay
 	value is 0 which means to match the value of the link monitor
 	interval.
 
+prio
+	Slave priority. A higher number means higher priority.
+	The primary slave has the highest priority. This option also
+	follows the primary_reselect rules.
+
+	This option could only be configured via netlink, and is only valid
+	for active-backup(1), balance-tlb (5) and balance-alb (6) mode.
+	The default value is 0.
+
 primary
 
 	A string (eth0, eth2, etc) specifying which slave is the
drivers/net/bonding/bond_main.c +27 -0
index 15eddca7b4b6..4211b79ac619 100644
@@ -1026,12 +1026,38 @@ static void bond_do_fail_over_mac(struct bonding *bond,
 
 }
 
+/**
+ * bond_choose_primary_or_current - select the primary or high priority slave
+ * @bond: our bonding struct
+ *
+ * - Check if there is a primary link. If the primary link was set and is up,
+ *   go on and do link reselection.
+ *
+ * - If primary link is not set or down, find the highest priority link.
+ *   If the highest priority link is not current slave, set it as primary
+ *   link and do link reselection.
+ */
 static struct slave *bond_choose_primary_or_current(struct bonding *bond)
 {
 	struct slave *prim = rtnl_dereference(bond->primary_slave);
 	struct slave *curr = rtnl_dereference(bond->curr_active_slave);
+	struct slave *slave, *hprio = NULL;
+	struct list_head *iter;
 
 	if (!prim || prim->link != BOND_LINK_UP) {
+		bond_for_each_slave(bond, slave, iter) {
+			if (slave->link == BOND_LINK_UP) {
+				hprio = hprio ? hprio : slave;
+				if (slave->prio > hprio->prio)
+					hprio = slave;
+			}
+		}
+
+		if (hprio && hprio != curr) {
+			prim = hprio;
+			goto link_reselect;
+		}
+
 		if (!curr || curr->link != BOND_LINK_UP)
 			return NULL;
 		return curr;
@@ -1042,6 +1068,7 @@ static struct slave *bond_choose_primary_or_current(struct bonding *bond)
 		return prim;
 	}
 
+link_reselect:
 	if (!curr || curr->link != BOND_LINK_UP)
 		return prim;
 
drivers/net/bonding/bond_netlink.c +12 -0
index f427fa1737c7..63066559e7d6 100644
@@ -27,6 +27,7 @@ static size_t bond_get_slave_size(const struct net_device *bond_dev,
 		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
 		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
 		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
+		nla_total_size(sizeof(s32)) +	/* IFLA_BOND_SLAVE_PRIO */
 		0;
 }
 
@@ -53,6 +54,9 @@ static int bond_fill_slave_info(struct sk_buff *skb,
 	if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
 		goto nla_put_failure;
 
+	if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))
+		goto nla_put_failure;
+
 	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
 		const struct aggregator *agg;
 		const struct port *ad_port;
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
 
 static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
 	[IFLA_BOND_SLAVE_QUEUE_ID]	= { .type = NLA_U16 },
+	[IFLA_BOND_SLAVE_PRIO]		= { .type = NLA_S32 },
 };
 
 static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -136,6 +141,7 @@ static int bond_slave_changelink(struct net_device *bond_dev,
 				 struct nlattr *tb[], struct nlattr *data[],
 				 struct netlink_ext_ack *extack)
 {
+	struct slave *slave = bond_slave_get_rtnl(slave_dev);
 	struct bonding *bond = netdev_priv(bond_dev);
 	struct bond_opt_value newval;
 	int err;
@@ -156,6 +162,12 @@ static int bond_slave_changelink(struct net_device *bond_dev,
 			return err;
 	}
 
+	/* No need to bother __bond_opt_set as we only support netlink config */
+	if (data[IFLA_BOND_SLAVE_PRIO]) {
+		slave->prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
+		bond_select_active_slave(bond);
+	}
+
 	return 0;
 }
 
include/net/bonding.h +1 -0
index b14f4c0b4e9e..4ff093fb2289 100644
@@ -176,6 +176,7 @@ struct slave {
 	u32    speed;
 	u16    queue_id;
 	u8     perm_hwaddr[MAX_ADDR_LEN];
+	int    prio;
 	struct ad_slave_info *ad_info;
 	struct tlb_slave_info tlb_info;
 #ifdef CONFIG_NET_POLL_CONTROLLER
include/uapi/linux/if_link.h +1 -0
index cc284c048e69..204e342b107a 100644
@@ -956,6 +956,7 @@ enum {
 	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
 	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
 	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
 	__IFLA_BOND_SLAVE_MAX,
 };
 
tools/include/uapi/linux/if_link.h +1 -0
index e1ba2d51b717..ee5de9f3700b 100644
@@ -888,6 +888,7 @@ enum {
 	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
 	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
 	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
 	__IFLA_BOND_SLAVE_MAX,
 };
 
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/1 iplink: bond_slave: add per port prio support

Add per port priority support for bonding. A higher number means higher priority. This option is only valid for active-backup(1), balance-tlb (5) and balance-alb (6) mode.

---
 ip/iplink_bond_slave.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
ip/iplink_bond_slave.c +13 -1
index d488aaab..7a4d89ff 100644
@@ -19,7 +19,7 @@
 
 static void print_explain(FILE *f)
 {
-	fprintf(f, "Usage: ... bond_slave [ queue_id ID ]\n");
+	fprintf(f, "Usage: ... bond_slave [ queue_id ID ] [ prio PRIORITY ]\n");
 }
 
 static void explain(void)
@@ -120,6 +120,12 @@ static void bond_slave_print_opt(struct link_util *lu, FILE *f, struct rtattr *t
 			  "queue_id %d ",
 			  rta_getattr_u16(tb[IFLA_BOND_SLAVE_QUEUE_ID]));
 
+	if (tb[IFLA_BOND_SLAVE_PRIO])
+		print_int(PRINT_ANY,
+			  "prio",
+			  "prio %d ",
+			  rta_getattr_s32(tb[IFLA_BOND_SLAVE_PRIO]));
+
 	if (tb[IFLA_BOND_SLAVE_AD_AGGREGATOR_ID])
 		print_int(PRINT_ANY,
 			  "ad_aggregator_id",
@@ -151,6 +157,7 @@ static int bond_slave_parse_opt(struct link_util *lu, int argc, char **argv,
 				struct nlmsghdr *n)
 {
 	__u16 queue_id;
+	int prio;
 
 	while (argc > 0) {
 		if (matches(*argv, "queue_id") == 0) {
@@ -158,6 +165,11 @@ static int bond_slave_parse_opt(struct link_util *lu, int argc, char **argv,
 			if (get_u16(&queue_id, *argv, 0))
 				invarg("queue_id is invalid", *argv);
 			addattr16(n, 1024, IFLA_BOND_SLAVE_QUEUE_ID, queue_id);
+		} else if (strcmp(*argv, "prio") == 0) {
+			NEXT_ARG();
+			if (get_s32(&prio, *argv, 0))
+				invarg("prio is invalid", *argv);
+			addattr32(n, 1024, IFLA_BOND_SLAVE_PRIO, prio);
 		} else {
 			if (matches(*argv, "help") != 0)
 				fprintf(stderr,
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH iproute2-next] iplink: bond_slave: add per port prio support

Quoted 14 lines · Hangbin Liu
ip/iplink_bond_slave.c +1 -1
index d488aaab..7a4d89ff 100644
@@ -19,7 +19,7 @@
 
 static void print_explain(FILE *f)
 {
-	fprintf(f, "Usage: ... bond_slave [ queue_id ID ]\n");
+	fprintf(f, "Usage: ... bond_slave [ queue_id ID ] [ prio PRIORITY ]\n");
 }
 
 static void explain(void)

ip/iplink_bond_slave.c static void bond_slave_print_opt(struct link_util *lu, FILE *f, struct rtattr *t · line 126

Quoted 7 lines
ip/iplink_bond_slave.c +4 -0
@@ -120,6 +120,12 @@ static void bond_slave_print_opt(struct link_util *lu, FILE *f, struct rtattr *t
 			  "queue_id %d ",
 			  rta_getattr_u16(tb[IFLA_BOND_SLAVE_QUEUE_ID]));
 
+	if (tb[IFLA_BOND_SLAVE_PRIO])
+		print_int(PRINT_ANY,
+			  "prio",
+			  "prio %d ",

those last 2 lines can go on the first.

Also, seems like a man page should be updated as well.

#3 lore raw
In reply to Hangbin Liu
Quoted 3 lines · Hangbin Liu

Add per port priority support for bonding. A higher number means higher priority. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

The above description (and the Subject) should mention that this
apparently refers to priority in interface selection during failover events.

Quoted 106 lines

This option could only be configured via netlink.

---

Documentation/networking/bonding.rst |  9 +++++++++
drivers/net/bonding/bond_main.c      | 27 +++++++++++++++++++++++++++
drivers/net/bonding/bond_netlink.c   | 12 ++++++++++++
include/net/bonding.h                |  1 +
include/uapi/linux/if_link.h         |  1 +
tools/include/uapi/linux/if_link.h   |  1 +
6 files changed, 51 insertions(+)
Documentation/networking/bonding.rst +9 -0
index 525e6842dd33..103e292a04a1 100644
@@ -780,6 +780,15 @@ peer_notif_delay
 	value is 0 which means to match the value of the link monitor
 	interval.
 
+prio
+	Slave priority. A higher number means higher priority.
+	The primary slave has the highest priority. This option also
+	follows the primary_reselect rules.
+
+	This option could only be configured via netlink, and is only valid
+	for active-backup(1), balance-tlb (5) and balance-alb (6) mode.
+	The default value is 0.
+

primary

A string (eth0, eth2, etc) specifying which slave is the

drivers/net/bonding/bond_main.c +27 -0
index 15eddca7b4b6..4211b79ac619 100644
@@ -1026,12 +1026,38 @@ static void bond_do_fail_over_mac(struct bonding *bond,
 
 }
 
+/**
+ * bond_choose_primary_or_current - select the primary or high priority slave
+ * @bond: our bonding struct
+ *
+ * - Check if there is a primary link. If the primary link was set and is up,
+ *   go on and do link reselection.
+ *
+ * - If primary link is not set or down, find the highest priority link.
+ *   If the highest priority link is not current slave, set it as primary
+ *   link and do link reselection.
+ */
 static struct slave *bond_choose_primary_or_current(struct bonding *bond)
 {
 	struct slave *prim = rtnl_dereference(bond->primary_slave);
 	struct slave *curr = rtnl_dereference(bond->curr_active_slave);
+	struct slave *slave, *hprio = NULL;
+	struct list_head *iter;
 
 	if (!prim || prim->link != BOND_LINK_UP) {
+		bond_for_each_slave(bond, slave, iter) {
+			if (slave->link == BOND_LINK_UP) {
+				hprio = hprio ? hprio : slave;
+				if (slave->prio > hprio->prio)
+					hprio = slave;
+			}
+		}
+
+		if (hprio && hprio != curr) {
+			prim = hprio;
+			goto link_reselect;
+		}
+
 		if (!curr || curr->link != BOND_LINK_UP)
 			return NULL;
 		return curr;
@@ -1042,6 +1068,7 @@ static struct slave *bond_choose_primary_or_current(struct bonding *bond)
 		return prim;
 	}
 
+link_reselect:
 	if (!curr || curr->link != BOND_LINK_UP)
 		return prim;
 
drivers/net/bonding/bond_netlink.c +4 -0
index f427fa1737c7..63066559e7d6 100644
@@ -27,6 +27,7 @@ static size_t bond_get_slave_size(const struct net_device *bond_dev,
 		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
 		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
 		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
+		nla_total_size(sizeof(s32)) +	/* IFLA_BOND_SLAVE_PRIO */
 		0;
 }
 
@@ -53,6 +54,9 @@ static int bond_fill_slave_info(struct sk_buff *skb,
 	if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
 		goto nla_put_failure;
 
+	if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))
+		goto nla_put_failure;
+
 	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
 		const struct aggregator *agg;
 		const struct port *ad_port;

drivers/net/bonding/bond_netlink.c static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = { · line 124

Quoted 4 lines
drivers/net/bonding/bond_netlink.c +1 -0
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
 
 static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
 	[IFLA_BOND_SLAVE_QUEUE_ID]	= { .type = NLA_U16 },
+	[IFLA_BOND_SLAVE_PRIO]		= { .type = NLA_S32 },

Why used signed instead of unsigned?

Regardless, the valid range for the prio value should be
documented.

	-J
Quoted 63 lines
drivers/net/bonding/bond_netlink.c +7 -0
 };
 
 static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -136,6 +141,7 @@ static int bond_slave_changelink(struct net_device *bond_dev,
 				 struct nlattr *tb[], struct nlattr *data[],
 				 struct netlink_ext_ack *extack)
 {
+	struct slave *slave = bond_slave_get_rtnl(slave_dev);
 	struct bonding *bond = netdev_priv(bond_dev);
 	struct bond_opt_value newval;
 	int err;
@@ -156,6 +162,12 @@ static int bond_slave_changelink(struct net_device *bond_dev,
 			return err;
 	}
 
+	/* No need to bother __bond_opt_set as we only support netlink config */
+	if (data[IFLA_BOND_SLAVE_PRIO]) {
+		slave->prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
+		bond_select_active_slave(bond);
+	}
+
 	return 0;
 }
 
include/net/bonding.h +1 -0
index b14f4c0b4e9e..4ff093fb2289 100644
@@ -176,6 +176,7 @@ struct slave {
 	u32    speed;
 	u16    queue_id;
 	u8     perm_hwaddr[MAX_ADDR_LEN];
+	int    prio;
 	struct ad_slave_info *ad_info;
 	struct tlb_slave_info tlb_info;
 #ifdef CONFIG_NET_POLL_CONTROLLER
include/uapi/linux/if_link.h +1 -0
index cc284c048e69..204e342b107a 100644
@@ -956,6 +956,7 @@ enum {
 	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
 	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
 	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
 	__IFLA_BOND_SLAVE_MAX,
 };
 
tools/include/uapi/linux/if_link.h +1 -0
index e1ba2d51b717..ee5de9f3700b 100644
@@ -888,6 +888,7 @@ enum {
 	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
 	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
 	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
 	__IFLA_BOND_SLAVE_MAX,
 };
 
Signature · 1 line
2.35.1

---
-Jay Vosburgh, [email protected]

#4 lore raw
Quoted 9 lines · Jay Vosburgh
Quoted 3 lines · Hangbin Liu

Add per port priority support for bonding. A higher number means higher priority. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

The above description (and the Subject) should mention that this
apparently refers to priority in interface selection during failover events.

OK, will update it. How about:

Bonding: add per port priority for current slave re-selection during failover

Add per port priority support for bonding current re-selection during failover. A higher number means higher priority in selection. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

Quoted 10 lines

drivers/net/bonding/bond_netlink.c static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = { · line 124

Quoted 5 lines
drivers/net/bonding/bond_netlink.c +1 -0
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
 
 static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
 	[IFLA_BOND_SLAVE_QUEUE_ID]	= { .type = NLA_U16 },
+	[IFLA_BOND_SLAVE_PRIO]		= { .type = NLA_S32 },

Why used signed instead of unsigned?

Regardless, the valid range for the prio value should be
documented.

I did this in purpose as team also use singed number. User could use a negative number for a specific link while other links keep using default 0.

BTW, how to document the valid ranger for a int number, -2^31 ~ 2^31-1 or INT_MIN ~ INT_MAX.

Thanks
Hangbin

#5 lore raw
Quoted 7 lines · Hangbin Liu
Quoted 6 lines · Jay Vosburgh
Quoted 3 lines · Hangbin Liu

Add per port priority support for bonding. A higher number means higher priority. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

Quoted 7 lines
Quoted 3 lines

The above description (and the Subject) should mention that this
apparently refers to priority in interface selection during failover events.

OK, will update it. How about:

Bonding: add per port priority for current slave re-selection during failover

That would be better, but something like "add per-port priority
for failover re-selection" would be a bit shorter.

Quoted 3 lines

Add per port priority support for bonding current re-selection during failover. A higher number means higher priority in selection. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

This seems reasonable.

Quoted 8 lines
Quoted 8 lines

drivers/net/bonding/bond_netlink.c static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = { · line 124

Quoted 5 lines
drivers/net/bonding/bond_netlink.c +1 -0
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
 
 static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
 	[IFLA_BOND_SLAVE_QUEUE_ID]	= { .type = NLA_U16 },
+	[IFLA_BOND_SLAVE_PRIO]		= { .type = NLA_S32 },

Why used signed instead of unsigned?

Quoted 5 lines
Quoted 2 lines

Regardless, the valid range for the prio value should be
documented.

I did this in purpose as team also use singed number. User could use a negative number for a specific link while other links keep using default 0.

Fair enough; I had been comparing to the LACP port priority and
route metric, both of which are unsigned.

Quoted 2 lines

BTW, how to document the valid ranger for a int number, -2^31 ~ 2^31-1 or INT_MIN ~ INT_MAX.

The documentation can simply state that the value is a signed 32
bit integer (rather than giving the specifics).

	-J

---
-Jay Vosburgh, [email protected]

#6 lore raw
In reply to Hangbin Liu
Quoted 21 lines · Hangbin Liu

Add per port priority support for bonding. A higher number means higher priority. The primary slave still has the highest priority. This option also follows the primary_reselect rules.

This option could only be configured via netlink.

---
  Documentation/networking/bonding.rst |  9 +++++++++
  drivers/net/bonding/bond_main.c      | 27 +++++++++++++++++++++++++++
  drivers/net/bonding/bond_netlink.c   | 12 ++++++++++++
  include/net/bonding.h                |  1 +
  include/uapi/linux/if_link.h         |  1 +
  tools/include/uapi/linux/if_link.h   |  1 +
  6 files changed, 51 insertions(+)
Documentation/networking/bonding.rst +0 -0
index 525e6842dd33..103e292a04a1 100644

Documentation/networking/bonding.rst peer_notif_delay · line 788

Quoted 9 lines
Documentation/networking/bonding.rst +6 -0
@@ -780,6 +780,15 @@ peer_notif_delay
  	value is 0 which means to match the value of the link monitor
  	interval.
 
+prio
+	Slave priority. A higher number means higher priority.
+	The primary slave has the highest priority. This option also
+	follows the primary_reselect rules.
+
+	This option could only be configured via netlink, and is only valid
                    ^^ can
Quoted 96 lines
Documentation/networking/bonding.rst +3 -0
+	for active-backup(1), balance-tlb (5) and balance-alb (6) mode.
+	The default value is 0.
+
  primary
 
  	A string (eth0, eth2, etc) specifying which slave is the
drivers/net/bonding/bond_main.c +27 -0
index 15eddca7b4b6..4211b79ac619 100644
@@ -1026,12 +1026,38 @@ static void bond_do_fail_over_mac(struct bonding *bond,
 
  }
 
+/**
+ * bond_choose_primary_or_current - select the primary or high priority slave
+ * @bond: our bonding struct
+ *
+ * - Check if there is a primary link. If the primary link was set and is up,
+ *   go on and do link reselection.
+ *
+ * - If primary link is not set or down, find the highest priority link.
+ *   If the highest priority link is not current slave, set it as primary
+ *   link and do link reselection.
+ */
  static struct slave *bond_choose_primary_or_current(struct bonding *bond)
  {
  	struct slave *prim = rtnl_dereference(bond->primary_slave);
  	struct slave *curr = rtnl_dereference(bond->curr_active_slave);
+	struct slave *slave, *hprio = NULL;
+	struct list_head *iter;
 
  	if (!prim || prim->link != BOND_LINK_UP) {
+		bond_for_each_slave(bond, slave, iter) {
+			if (slave->link == BOND_LINK_UP) {
+				hprio = hprio ? hprio : slave;
+				if (slave->prio > hprio->prio)
+					hprio = slave;
+			}
+		}
+
+		if (hprio && hprio != curr) {
+			prim = hprio;
+			goto link_reselect;
+		}
+
  		if (!curr || curr->link != BOND_LINK_UP)
  			return NULL;
  		return curr;
@@ -1042,6 +1068,7 @@ static struct slave *bond_choose_primary_or_current(struct bonding *bond)
  		return prim;
  	}
 
+link_reselect:
  	if (!curr || curr->link != BOND_LINK_UP)
  		return prim;
 
drivers/net/bonding/bond_netlink.c +6 -0
index f427fa1737c7..63066559e7d6 100644
@@ -27,6 +27,7 @@ static size_t bond_get_slave_size(const struct net_device *bond_dev,
  		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
  		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
  		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
+		nla_total_size(sizeof(s32)) +	/* IFLA_BOND_SLAVE_PRIO */
  		0;
  }
 
@@ -53,6 +54,9 @@ static int bond_fill_slave_info(struct sk_buff *skb,
  	if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
  		goto nla_put_failure;
 
+	if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))
+		goto nla_put_failure;
+
  	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  		const struct aggregator *agg;
  		const struct port *ad_port;
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
 
  static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
  	[IFLA_BOND_SLAVE_QUEUE_ID]	= { .type = NLA_U16 },
+	[IFLA_BOND_SLAVE_PRIO]		= { .type = NLA_S32 },
  };
 
  static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -136,6 +141,7 @@ static int bond_slave_changelink(struct net_device *bond_dev,
  				 struct nlattr *tb[], struct nlattr *data[],
  				 struct netlink_ext_ack *extack)
  {
+	struct slave *slave = bond_slave_get_rtnl(slave_dev);
  	struct bonding *bond = netdev_priv(bond_dev);
  	struct bond_opt_value newval;
  	int err;

drivers/net/bonding/bond_netlink.c static int bond_slave_changelink(struct net_device *bond_dev, · line 165

Quoted 4 lines
drivers/net/bonding/bond_netlink.c +1 -0
@@ -156,6 +162,12 @@ static int bond_slave_changelink(struct net_device *bond_dev,
  			return err;
  	}
 
+	/* No need to bother __bond_opt_set as we only support netlink config */

Not sure this comment is necessary, it doesn't add any value. Also I would recommend using bonding's options management, as it would allow for checking if the value is in a defined range. That might not be particularly useful in this context since it appears +/-INT_MAX is the range.

Also, in the Documentation it is mentioned that this parameter is only used in modes active-backup and balance-alb/tlb. Do we need to send an error message back preventing the modification of this value when not in these modes?

Quoted 13 lines
drivers/net/bonding/bond_netlink.c +5 -0
+	if (data[IFLA_BOND_SLAVE_PRIO]) {
+		slave->prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
+		bond_select_active_slave(bond);
+	}
+
  	return 0;
  }
 
include/net/bonding.h +0 -0
index b14f4c0b4e9e..4ff093fb2289 100644

include/net/bonding.h struct slave { · line 179

Quoted 4 lines
include/net/bonding.h +1 -0
@@ -176,6 +176,7 @@ struct slave {
  	u32    speed;
  	u16    queue_id;
  	u8     perm_hwaddr[MAX_ADDR_LEN];
+	int    prio;

Do we want a struct slave_params here instead? That would allow us to define defaults in a central place and set them once instead of setting each parameter.

Quoted 27 lines
include/net/bonding.h +0 -0
  	struct ad_slave_info *ad_info;
  	struct tlb_slave_info tlb_info;
  #ifdef CONFIG_NET_POLL_CONTROLLER
include/uapi/linux/if_link.h +1 -0
index cc284c048e69..204e342b107a 100644
@@ -956,6 +956,7 @@ enum {
  	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
  	__IFLA_BOND_SLAVE_MAX,
  };
 
tools/include/uapi/linux/if_link.h +1 -0
index e1ba2d51b717..ee5de9f3700b 100644
@@ -888,6 +888,7 @@ enum {
  	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
  	__IFLA_BOND_SLAVE_MAX,
  };
 
#7 lore raw
Quoted 119 lines · Jonathan Toppins
Quoted 19 lines · Hangbin Liu

Add per port priority support for bonding. A higher number means higher priority. The primary slave still has the highest priority. This option also follows the primary_reselect rules.
This option could only be configured via netlink.

---
  Documentation/networking/bonding.rst |  9 +++++++++
  drivers/net/bonding/bond_main.c      | 27 +++++++++++++++++++++++++++
  drivers/net/bonding/bond_netlink.c   | 12 ++++++++++++
  include/net/bonding.h                |  1 +
  include/uapi/linux/if_link.h         |  1 +
  tools/include/uapi/linux/if_link.h   |  1 +
  6 files changed, 51 insertions(+)
Documentation/networking/bonding.rst +0 -0
index 525e6842dd33..103e292a04a1 100644

Documentation/networking/bonding.rst peer_notif_delay · line 787

Quoted 8 lines
Documentation/networking/bonding.rst +5 -0
@@ -780,6 +780,15 @@ peer_notif_delay
  	value is 0 which means to match the value of the link monitor
  	interval.
  +prio
+	Slave priority. A higher number means higher priority.
+	The primary slave has the highest priority. This option also
+	follows the primary_reselect rules.
+
+	This option could only be configured via netlink, and is only valid
                   ^^ can
Quoted 90 lines
Documentation/networking/bonding.rst +3 -0
+	for active-backup(1), balance-tlb (5) and balance-alb (6) mode.
+	The default value is 0.
+
  primary
    	A string (eth0, eth2, etc) specifying which slave is the
drivers/net/bonding/bond_main.c +25 -0
index 15eddca7b4b6..4211b79ac619 100644
@@ -1026,12 +1026,38 @@ static void bond_do_fail_over_mac(struct bonding *bond,
    }
  +/**
+ * bond_choose_primary_or_current - select the primary or high priority slave
+ * @bond: our bonding struct
+ *
+ * - Check if there is a primary link. If the primary link was set and is up,
+ *   go on and do link reselection.
+ *
+ * - If primary link is not set or down, find the highest priority link.
+ *   If the highest priority link is not current slave, set it as primary
+ *   link and do link reselection.
+ */
  static struct slave *bond_choose_primary_or_current(struct bonding *bond)
  {
  	struct slave *prim = rtnl_dereference(bond->primary_slave);
  	struct slave *curr = rtnl_dereference(bond->curr_active_slave);
+	struct slave *slave, *hprio = NULL;
+	struct list_head *iter;
    	if (!prim || prim->link != BOND_LINK_UP) {
+		bond_for_each_slave(bond, slave, iter) {
+			if (slave->link == BOND_LINK_UP) {
+				hprio = hprio ? hprio : slave;
+				if (slave->prio > hprio->prio)
+					hprio = slave;
+			}
+		}
+
+		if (hprio && hprio != curr) {
+			prim = hprio;
+			goto link_reselect;
+		}
+
  		if (!curr || curr->link != BOND_LINK_UP)
  			return NULL;
  		return curr;
@@ -1042,6 +1068,7 @@ static struct slave *bond_choose_primary_or_current(struct bonding *bond)
  		return prim;
  	}
  +link_reselect:
  	if (!curr || curr->link != BOND_LINK_UP)
  		return prim;
  diff --git a/drivers/net/bonding/bond_netlink.c

b/drivers/net/bonding/bond_netlink.c
index f427fa1737c7..63066559e7d6 100644

drivers/net/bonding/bond_netlink.c +1 -0
@@ -27,6 +27,7 @@ static size_t bond_get_slave_size(const struct net_device *bond_dev,
  		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
  		nla_total_size(sizeof(u8)) +	/* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
  		nla_total_size(sizeof(u16)) +	/* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
+		nla_total_size(sizeof(s32)) +	/* IFLA_BOND_SLAVE_PRIO */
  		0;
  }
  @@ -53,6 +54,9 @@ static int bond_fill_slave_info(struct sk_buff *skb,

if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
goto nla_put_failure;
+ if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))

drivers/net/bonding/bond_netlink.c +5 -0
+		goto nla_put_failure;
+
  	if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  		const struct aggregator *agg;
  		const struct port *ad_port;
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
    static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX
+ 1] = {
  	[IFLA_BOND_SLAVE_QUEUE_ID]	= { .type = NLA_U16 },
+	[IFLA_BOND_SLAVE_PRIO]		= { .type = NLA_S32 },
  };
    static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -136,6 +141,7 @@ static int bond_slave_changelink(struct net_device *bond_dev,
  				 struct nlattr *tb[], struct nlattr *data[],
  				 struct netlink_ext_ack *extack)
  {
+	struct slave *slave = bond_slave_get_rtnl(slave_dev);
  	struct bonding *bond = netdev_priv(bond_dev);
  	struct bond_opt_value newval;
  	int err;
@@ -156,6 +162,12 @@ static int bond_slave_changelink(struct net_device *bond_dev,
  			return err;
  	}
Quoted 8 lines
Quoted 2 lines
drivers/net/bonding/bond_netlink.c +0 -0
  +	/* No need to bother __bond_opt_set as we only support netlink

config */

Not sure this comment is necessary, it doesn't add any value. Also I would recommend using bonding's options management, as it would allow for checking if the value is in a defined range. That might not be particularly useful in this context since it appears +/-INT_MAX is the range.

Agreed, on both the comment and in regards to using the extant
bonding options management stuff.

Quoted 4 lines

Also, in the Documentation it is mentioned that this parameter is only used in modes active-backup and balance-alb/tlb. Do we need to send an error message back preventing the modification of this value when not in these modes?

	Using the option management stuff would get this for free.

	-J
Quoted 12 lines
Quoted 12 lines
drivers/net/bonding/bond_netlink.c +5 -0
+	if (data[IFLA_BOND_SLAVE_PRIO]) {
+		slave->prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
+		bond_select_active_slave(bond);
+	}
+
  	return 0;
  }
  diff --git a/include/net/bonding.h b/include/net/bonding.h

index b14f4c0b4e9e..4ff093fb2289 100644

include/net/bonding.h +0 -0
@@ -176,6 +176,7 @@ struct slave {
Quoted 8 lines

include/net/bonding.h struct slave { · line 179

Quoted 4 lines
include/net/bonding.h +1 -0
  	u32    speed;
  	u16    queue_id;
  	u8     perm_hwaddr[MAX_ADDR_LEN];
+	int    prio;

Do we want a struct slave_params here instead? That would allow us to define defaults in a central place and set them once instead of setting each parameter.

Presuming that you mean creating a sub-struct here and moving
some set of members of struct slave into it, I'm not sure I see the benefit, as it would only exist here and not really be an independent object. Am I misunderstanding?

	-J
Quoted 19 lines
Quoted 19 lines
include/net/bonding.h +0 -0
  	struct ad_slave_info *ad_info;
  	struct tlb_slave_info tlb_info;
  #ifdef CONFIG_NET_POLL_CONTROLLER
include/uapi/linux/if_link.h +1 -0
index cc284c048e69..204e342b107a 100644
@@ -956,6 +956,7 @@ enum {
  	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
  	__IFLA_BOND_SLAVE_MAX,
  };
  diff --git a/tools/include/uapi/linux/if_link.h

b/tools/include/uapi/linux/if_link.h
index e1ba2d51b717..ee5de9f3700b 100644

tools/include/uapi/linux/if_link.h +0 -0
Quoted 8 lines

tools/include/uapi/linux/if_link.h enum { · line 894

Quoted 8 lines
tools/include/uapi/linux/if_link.h +1 -0
@@ -888,6 +888,7 @@ enum {
  	IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  	IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  	IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
+	IFLA_BOND_SLAVE_PRIO,
  	__IFLA_BOND_SLAVE_MAX,
  };
 

---
-Jay Vosburgh, [email protected]

#8 lore raw
Quoted 12 lines · Jay Vosburgh
Quoted 4 lines
Quoted 4 lines
   diff --git a/include/net/bonding.h b/include/net/bonding.h

index b14f4c0b4e9e..4ff093fb2289 100644

include/net/bonding.h +0 -0
Quoted 8 lines

include/net/bonding.h struct slave { · line 179

Quoted 5 lines
include/net/bonding.h +1 -0
@@ -176,6 +176,7 @@ struct slave {
   	u32    speed;
   	u16    queue_id;
   	u8     perm_hwaddr[MAX_ADDR_LEN];
+	int    prio;

Do we want a struct slave_params here instead? That would allow us to define defaults in a central place and set them once instead of setting each parameter.

Quoted 4 lines

Presuming that you mean creating a sub-struct here and moving
some set of members of struct slave into it, I'm not sure I see the benefit, as it would only exist here and not really be an independent object. Am I misunderstanding?

You are understanding correctly. The goal of this work is to eventually port the majority of the per-port parameters that exist in teaming to bonding, we have not determined the entire set that make sense. Thus there will be more than just port priority as a userspace configurable option. So I was attempting to ask if modeling the initial setting of these parameters like how bonding_defaults is used, made sense.

file: drivers/net/bonding/bond_main.c:

	void bond_setup(struct net_device *bond_dev)
	{
		struct bonding *bond = netdev_priv(bond_dev);

		spin_lock_init(&bond->mode_lock);
		bond->params = bonding_defaults;
	...

We can always refactor this area when there is another option that needs setting.

-Jon

#9 lore raw
Quoted 8 lines · Jonathan Toppins
Quoted 4 lines

Presuming that you mean creating a sub-struct here and moving
some set of members of struct slave into it, I'm not sure I see the benefit, as it would only exist here and not really be an independent object. Am I misunderstanding?

You are understanding correctly. The goal of this work is to eventually port the majority of the per-port parameters that exist in teaming to bonding, we have not determined the entire set that make sense. Thus there will be more

Hi Jay,

As Jon said, I'm working to implement/import teaming specific features to bonding, so users could have more choice. One import feature teaming has is per-port parameters/configurations. A part of the per-port configs are queue_id, prio, lacp_prio, lacp_key, etc. Most of the configs are link_watch parameters. Which means each port/slave has it's own delay up, delay down, interval, arp targets, etc. We are still discussing if bonding need all of them or just a part.

Do you see if it's valuable to add all the per-port link watch configurations to bonding?

Thanks
Hangbin

Quoted 19 lines

than just port priority as a userspace configurable option. So I was attempting to ask if modeling the initial setting of these parameters like how bonding_defaults is used, made sense.

file: drivers/net/bonding/bond_main.c:

	void bond_setup(struct net_device *bond_dev)
	{
		struct bonding *bond = netdev_priv(bond_dev);

		spin_lock_init(&bond->mode_lock);
		bond->params = bonding_defaults;
	...

We can always refactor this area when there is another option that needs setting.

-Jon

#10 lore raw
In reply to Jay Vosburgh
Quoted 23 lines · Jay Vosburgh
Quoted 11 lines
Quoted 11 lines
drivers/net/bonding/bond_netlink.c +1 -0
@@ -136,6 +141,7 @@ static int bond_slave_changelink(struct net_device *bond_dev,
  				 struct nlattr *tb[], struct nlattr *data[],
  				 struct netlink_ext_ack *extack)
  {
+	struct slave *slave = bond_slave_get_rtnl(slave_dev);
  	struct bonding *bond = netdev_priv(bond_dev);
  	struct bond_opt_value newval;
  	int err;
@@ -156,6 +162,12 @@ static int bond_slave_changelink(struct net_device *bond_dev,
  			return err;
  	}
Quoted 8 lines
Quoted 2 lines
(quoted diff) +0 -0
  +	/* No need to bother __bond_opt_set as we only support netlink

config */

Not sure this comment is necessary, it doesn't add any value. Also I would recommend using bonding's options management, as it would allow for checking if the value is in a defined range. That might not be particularly useful in this context since it appears +/-INT_MAX is the range.

Agreed, on both the comment and in regards to using the extant
bonding options management stuff.

Quoted 6 lines
Quoted 4 lines

Also, in the Documentation it is mentioned that this parameter is only used in modes active-backup and balance-alb/tlb. Do we need to send an error message back preventing the modification of this value when not in these modes?

Using the option management stuff would get this for free.

Hi Jav, Jon,

I remembered the reason why I didn't use bond default option management.

It's because the bonding options management only take bond and values. We need to create an extra string to save the slave name and option values. Then in bond option setting function we extract the info from the string and do setting again, like the bond_option_queue_id_set().

I think this is too heavy for just an int value setting for slave. As we only support netlink for new options. There is no need to handle string setting via sysfs. For mode checking, we do just do like:

if (!bond_uses_primary(bond))

	return -EACCES;

So why bother the bonding options management? What do you think? Do you have a easier way to get the slave name in options management? If yes, I'm happy to use the default option management.

Thanks
Hangbin

Quoted 9 lines
	-J
Quoted 6 lines
Quoted 6 lines
drivers/net/bonding/bond_netlink.c +5 -0
+	if (data[IFLA_BOND_SLAVE_PRIO]) {
+		slave->prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
+		bond_select_active_slave(bond);
+	}
+
  	return 0;
#11 lore raw
Quoted 23 lines · Hangbin Liu
Quoted 9 lines

Agreed, on both the comment and in regards to using the extant
bonding options management stuff.

Quoted 4 lines

Also, in the Documentation it is mentioned that this parameter is only used in modes active-backup and balance-alb/tlb. Do we need to send an error message back preventing the modification of this value when not in these modes?

Using the option management stuff would get this for free.

Hi Jav, Jon,

I remembered the reason why I didn't use bond default option management.

It's because the bonding options management only take bond and values. We need to create an extra string to save the slave name and option values. Then in bond option setting function we extract the info from the string and do setting again, like the bond_option_queue_id_set().

I think this is too heavy for just an int value setting for slave. As we only support netlink for new options. There is no need to handle string setting via sysfs. For mode checking, we do just do like:

Quoted 6 lines

if (!bond_uses_primary(bond))

	return -EACCES;

So why bother the bonding options management? What do you think? Do you have a easier way to get the slave name in options management? If yes, I'm happy to use the default option management.

Hi Jan,

Any comments?

Thanks
Hangbin

#12 lore raw
Quoted 27 lines · Hangbin Liu
Quoted 26 lines · Hangbin Liu
Quoted 9 lines

Agreed, on both the comment and in regards to using the extant
bonding options management stuff.

Quoted 4 lines

Also, in the Documentation it is mentioned that this parameter is only used in modes active-backup and balance-alb/tlb. Do we need to send an error message back preventing the modification of this value when not in these modes?

Using the option management stuff would get this for free.

Hi Jav, Jon,

I remembered the reason why I didn't use bond default option management.

It's because the bonding options management only take bond and values. We need to create an extra string to save the slave name and option values. Then in bond option setting function we extract the info from the string and do setting again, like the bond_option_queue_id_set().

I think this is too heavy for just an int value setting for slave. As we only support netlink for new options. There is no need to handle string setting via sysfs. For mode checking, we do just do like:

if (!bond_uses_primary(bond))

	return -EACCES;
Quoted 8 lines
Quoted 3 lines

So why bother the bonding options management? What do you think? Do you have a easier way to get the slave name in options management? If yes, I'm happy to use the default option management.

Hi Jay,

Any comments?

Hi Jay,

I'm still waiting for your comments before post v2 patch. Appreciate if you could have a better way about handling the slave name in options management.

Thanks
Hangbin

#13 lore raw
Quoted 4 lines · Hangbin Liu

Hi Jay,

I'm still waiting for your comments before post v2 patch. Appreciate if you could have a better way about handling the slave name in options management.

Hi Jay,

Would you please help reply when have time?

Thanks
Hangbin

#14 lore raw
In reply to Hangbin Liu
Quoted 34 lines · Hangbin Liu
Quoted 33 lines · Hangbin Liu
Quoted 23 lines · Hangbin Liu
Quoted 9 lines

Agreed, on both the comment and in regards to using the extant
bonding options management stuff.

Quoted 4 lines

Also, in the Documentation it is mentioned that this parameter is only used in modes active-backup and balance-alb/tlb. Do we need to send an error message back preventing the modification of this value when not in these modes?

Using the option management stuff would get this for free.

Hi Jav, Jon,

I remembered the reason why I didn't use bond default option management.

It's because the bonding options management only take bond and values. We need to create an extra string to save the slave name and option values. Then in bond option setting function we extract the info from the string and do setting again, like the bond_option_queue_id_set().

I think this is too heavy for just an int value setting for slave. As we only support netlink for new options. There is no need to handle string setting via sysfs. For mode checking, we do just do like:

Quoted 6 lines

if (!bond_uses_primary(bond))

	return -EACCES;

So why bother the bonding options management? What do you think? Do you have a easier way to get the slave name in options management? If yes, I'm happy to use the default option management.

Hi Jay,

Quoted 7 lines
Quoted 2 lines

Any comments?

Hi Jay,

I'm still waiting for your comments before post v2 patch. Appreciate if you could have a better way about handling the slave name in options management.

Hi Jay, ping?