Skip to content

Commit 0a2ff7c

Browse files
liuhangbindavem330
authored andcommitted
Bonding: add per-port priority for failover re-selection
Add per port priority support for bonding active slave 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 option could only be configured via netlink. Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> Acked-by: Jonathan Toppins <jtoppins@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f2b3b28 commit 0a2ff7c

File tree

8 files changed

+90
-0
lines changed

8 files changed

+90
-0
lines changed

Documentation/networking/bonding.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,17 @@ peer_notif_delay
780780
value is 0 which means to match the value of the link monitor
781781
interval.
782782

783+
prio
784+
Slave priority. A higher number means higher priority.
785+
The primary slave has the highest priority. This option also
786+
follows the primary_reselect rules.
787+
788+
This option could only be configured via netlink, and is only valid
789+
for active-backup(1), balance-tlb (5) and balance-alb (6) mode.
790+
The valid value range is a signed 32 bit integer.
791+
792+
The default value is 0.
793+
783794
primary
784795

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

drivers/net/bonding/bond_main.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,12 +1026,38 @@ static void bond_do_fail_over_mac(struct bonding *bond,
10261026

10271027
}
10281028

1029+
/**
1030+
* bond_choose_primary_or_current - select the primary or high priority slave
1031+
* @bond: our bonding struct
1032+
*
1033+
* - Check if there is a primary link. If the primary link was set and is up,
1034+
* go on and do link reselection.
1035+
*
1036+
* - If primary link is not set or down, find the highest priority link.
1037+
* If the highest priority link is not current slave, set it as primary
1038+
* link and do link reselection.
1039+
*/
10291040
static struct slave *bond_choose_primary_or_current(struct bonding *bond)
10301041
{
10311042
struct slave *prim = rtnl_dereference(bond->primary_slave);
10321043
struct slave *curr = rtnl_dereference(bond->curr_active_slave);
1044+
struct slave *slave, *hprio = NULL;
1045+
struct list_head *iter;
10331046

10341047
if (!prim || prim->link != BOND_LINK_UP) {
1048+
bond_for_each_slave(bond, slave, iter) {
1049+
if (slave->link == BOND_LINK_UP) {
1050+
hprio = hprio ?: slave;
1051+
if (slave->prio > hprio->prio)
1052+
hprio = slave;
1053+
}
1054+
}
1055+
1056+
if (hprio && hprio != curr) {
1057+
prim = hprio;
1058+
goto link_reselect;
1059+
}
1060+
10351061
if (!curr || curr->link != BOND_LINK_UP)
10361062
return NULL;
10371063
return curr;
@@ -1042,6 +1068,7 @@ static struct slave *bond_choose_primary_or_current(struct bonding *bond)
10421068
return prim;
10431069
}
10441070

1071+
link_reselect:
10451072
if (!curr || curr->link != BOND_LINK_UP)
10461073
return prim;
10471074

drivers/net/bonding/bond_netlink.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static size_t bond_get_slave_size(const struct net_device *bond_dev,
2727
nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
2828
nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
2929
nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
30+
nla_total_size(sizeof(s32)) + /* IFLA_BOND_SLAVE_PRIO */
3031
0;
3132
}
3233

@@ -53,6 +54,9 @@ static int bond_fill_slave_info(struct sk_buff *skb,
5354
if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
5455
goto nla_put_failure;
5556

57+
if (nla_put_s32(skb, IFLA_BOND_SLAVE_PRIO, slave->prio))
58+
goto nla_put_failure;
59+
5660
if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
5761
const struct aggregator *agg;
5862
const struct port *ad_port;
@@ -117,6 +121,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
117121

118122
static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
119123
[IFLA_BOND_SLAVE_QUEUE_ID] = { .type = NLA_U16 },
124+
[IFLA_BOND_SLAVE_PRIO] = { .type = NLA_S32 },
120125
};
121126

122127
static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -157,6 +162,16 @@ static int bond_slave_changelink(struct net_device *bond_dev,
157162
return err;
158163
}
159164

165+
if (data[IFLA_BOND_SLAVE_PRIO]) {
166+
int prio = nla_get_s32(data[IFLA_BOND_SLAVE_PRIO]);
167+
168+
bond_opt_slave_initval(&newval, &slave_dev, prio);
169+
err = __bond_opt_set(bond, BOND_OPT_PRIO, &newval,
170+
data[IFLA_BOND_SLAVE_PRIO], extack);
171+
if (err)
172+
return err;
173+
}
174+
160175
return 0;
161176
}
162177

drivers/net/bonding/bond_options.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ static int bond_option_arp_validate_set(struct bonding *bond,
4040
const struct bond_opt_value *newval);
4141
static int bond_option_arp_all_targets_set(struct bonding *bond,
4242
const struct bond_opt_value *newval);
43+
static int bond_option_prio_set(struct bonding *bond,
44+
const struct bond_opt_value *newval);
4345
static int bond_option_primary_set(struct bonding *bond,
4446
const struct bond_opt_value *newval);
4547
static int bond_option_primary_reselect_set(struct bonding *bond,
@@ -365,6 +367,16 @@ static const struct bond_option bond_opts[BOND_OPT_LAST] = {
365367
.values = bond_intmax_tbl,
366368
.set = bond_option_miimon_set
367369
},
370+
[BOND_OPT_PRIO] = {
371+
.id = BOND_OPT_PRIO,
372+
.name = "prio",
373+
.desc = "Link priority for failover re-selection",
374+
.flags = BOND_OPTFLAG_RAWVAL,
375+
.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
376+
BIT(BOND_MODE_TLB) |
377+
BIT(BOND_MODE_ALB)),
378+
.set = bond_option_prio_set
379+
},
368380
[BOND_OPT_PRIMARY] = {
369381
.id = BOND_OPT_PRIMARY,
370382
.name = "primary",
@@ -1306,6 +1318,27 @@ static int bond_option_missed_max_set(struct bonding *bond,
13061318
return 0;
13071319
}
13081320

1321+
static int bond_option_prio_set(struct bonding *bond,
1322+
const struct bond_opt_value *newval)
1323+
{
1324+
struct slave *slave;
1325+
1326+
slave = bond_slave_get_rtnl(newval->slave_dev);
1327+
if (!slave) {
1328+
netdev_dbg(newval->slave_dev, "%s called on NULL slave\n", __func__);
1329+
return -ENODEV;
1330+
}
1331+
slave->prio = newval->value;
1332+
1333+
if (rtnl_dereference(bond->primary_slave))
1334+
slave_warn(bond->dev, slave->dev,
1335+
"prio updated, but will not affect failover re-selection as primary slave have been set\n");
1336+
else
1337+
bond_select_active_slave(bond);
1338+
1339+
return 0;
1340+
}
1341+
13091342
static int bond_option_primary_set(struct bonding *bond,
13101343
const struct bond_opt_value *newval)
13111344
{

include/net/bond_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum {
6767
BOND_OPT_LACP_ACTIVE,
6868
BOND_OPT_MISSED_MAX,
6969
BOND_OPT_NS_TARGETS,
70+
BOND_OPT_PRIO,
7071
BOND_OPT_LAST
7172
};
7273

include/net/bonding.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ struct slave {
178178
u32 speed;
179179
u16 queue_id;
180180
u8 perm_hwaddr[MAX_ADDR_LEN];
181+
int prio;
181182
struct ad_slave_info *ad_info;
182183
struct tlb_slave_info tlb_info;
183184
#ifdef CONFIG_NET_POLL_CONTROLLER

include/uapi/linux/if_link.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ enum {
963963
IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
964964
IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
965965
IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
966+
IFLA_BOND_SLAVE_PRIO,
966967
__IFLA_BOND_SLAVE_MAX,
967968
};
968969

tools/include/uapi/linux/if_link.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ enum {
890890
IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
891891
IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
892892
IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
893+
IFLA_BOND_SLAVE_PRIO,
893894
__IFLA_BOND_SLAVE_MAX,
894895
};
895896

0 commit comments

Comments
 (0)