2 * originally based on the dummy device.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
7 * bonding.c: an Ethernet Bonding driver
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
14 * and probably many L2 switches ...
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
24 * will release all slaves, marking them as down.
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
44 #include <linux/icmp.h>
45 #include <linux/icmpv6.h>
46 #include <linux/tcp.h>
47 #include <linux/udp.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <linux/timer.h>
52 #include <linux/socket.h>
53 #include <linux/ctype.h>
54 #include <linux/inet.h>
55 #include <linux/bitops.h>
58 #include <linux/uaccess.h>
59 #include <linux/errno.h>
60 #include <linux/netdevice.h>
61 #include <linux/inetdevice.h>
62 #include <linux/igmp.h>
63 #include <linux/etherdevice.h>
64 #include <linux/skbuff.h>
66 #include <linux/rtnetlink.h>
67 #include <linux/smp.h>
68 #include <linux/if_ether.h>
70 #include <linux/mii.h>
71 #include <linux/ethtool.h>
72 #include <linux/if_vlan.h>
73 #include <linux/if_bonding.h>
74 #include <linux/jiffies.h>
75 #include <linux/preempt.h>
76 #include <net/route.h>
77 #include <net/net_namespace.h>
78 #include <net/netns/generic.h>
79 #include <net/pkt_sched.h>
80 #include <linux/rculist.h>
81 #include <net/flow_dissector.h>
83 #include <net/bonding.h>
84 #include <net/bond_3ad.h>
85 #include <net/bond_alb.h>
86 #if IS_ENABLED(CONFIG_TLS_DEVICE)
90 #include "bonding_priv.h"
92 /*---------------------------- Module parameters ----------------------------*/
94 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
96 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
97 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
98 static int num_peer_notif = 1;
101 static int downdelay;
102 static int use_carrier = 1;
104 static char *primary;
105 static char *primary_reselect;
106 static char *lacp_rate;
107 static int min_links;
108 static char *ad_select;
109 static char *xmit_hash_policy;
110 static int arp_interval;
111 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
112 static char *arp_validate;
113 static char *arp_all_targets;
114 static char *fail_over_mac;
115 static int all_slaves_active;
116 static struct bond_params bonding_defaults;
117 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
118 static int packets_per_slave = 1;
119 static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
121 module_param(max_bonds, int, 0);
122 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
123 module_param(tx_queues, int, 0);
124 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
125 module_param_named(num_grat_arp, num_peer_notif, int, 0644);
126 MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
127 "failover event (alias of num_unsol_na)");
128 module_param_named(num_unsol_na, num_peer_notif, int, 0644);
129 MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
130 "failover event (alias of num_grat_arp)");
131 module_param(miimon, int, 0);
132 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
133 module_param(updelay, int, 0);
134 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
135 module_param(downdelay, int, 0);
136 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
138 module_param(use_carrier, int, 0);
139 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
140 "0 for off, 1 for on (default)");
141 module_param(mode, charp, 0);
142 MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
143 "1 for active-backup, 2 for balance-xor, "
144 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
145 "6 for balance-alb");
146 module_param(primary, charp, 0);
147 MODULE_PARM_DESC(primary, "Primary network device to use");
148 module_param(primary_reselect, charp, 0);
149 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
151 "0 for always (default), "
152 "1 for only if speed of primary is "
154 "2 for only on active slave "
156 module_param(lacp_rate, charp, 0);
157 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
158 "0 for slow, 1 for fast");
159 module_param(ad_select, charp, 0);
160 MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
161 "0 for stable (default), 1 for bandwidth, "
163 module_param(min_links, int, 0);
164 MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
166 module_param(xmit_hash_policy, charp, 0);
167 MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
168 "0 for layer 2 (default), 1 for layer 3+4, "
169 "2 for layer 2+3, 3 for encap layer 2+3, "
170 "4 for encap layer 3+4, 5 for vlan+srcmac");
171 module_param(arp_interval, int, 0);
172 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
173 module_param_array(arp_ip_target, charp, NULL, 0);
174 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
175 module_param(arp_validate, charp, 0);
176 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
177 "0 for none (default), 1 for active, "
178 "2 for backup, 3 for all");
179 module_param(arp_all_targets, charp, 0);
180 MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
181 module_param(fail_over_mac, charp, 0);
182 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
183 "the same MAC; 0 for none (default), "
184 "1 for active, 2 for follow");
185 module_param(all_slaves_active, int, 0);
186 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
187 "by setting active flag for all slaves; "
188 "0 for never (default), 1 for always.");
189 module_param(resend_igmp, int, 0);
190 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
192 module_param(packets_per_slave, int, 0);
193 MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
194 "mode; 0 for a random slave, 1 packet per "
195 "slave (default), >1 packets per slave.");
196 module_param(lp_interval, uint, 0);
197 MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
198 "the bonding driver sends learning packets to "
199 "each slaves peer switch. The default is 1.");
201 /*----------------------------- Global variables ----------------------------*/
203 #ifdef CONFIG_NET_POLL_CONTROLLER
204 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
207 unsigned int bond_net_id __read_mostly;
209 static const struct flow_dissector_key flow_keys_bonding_keys[] = {
211 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
212 .offset = offsetof(struct flow_keys, control),
215 .key_id = FLOW_DISSECTOR_KEY_BASIC,
216 .offset = offsetof(struct flow_keys, basic),
219 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
220 .offset = offsetof(struct flow_keys, addrs.v4addrs),
223 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
224 .offset = offsetof(struct flow_keys, addrs.v6addrs),
227 .key_id = FLOW_DISSECTOR_KEY_TIPC,
228 .offset = offsetof(struct flow_keys, addrs.tipckey),
231 .key_id = FLOW_DISSECTOR_KEY_PORTS,
232 .offset = offsetof(struct flow_keys, ports),
235 .key_id = FLOW_DISSECTOR_KEY_ICMP,
236 .offset = offsetof(struct flow_keys, icmp),
239 .key_id = FLOW_DISSECTOR_KEY_VLAN,
240 .offset = offsetof(struct flow_keys, vlan),
243 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
244 .offset = offsetof(struct flow_keys, tags),
247 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
248 .offset = offsetof(struct flow_keys, keyid),
252 static struct flow_dissector flow_keys_bonding __read_mostly;
254 /*-------------------------- Forward declarations ---------------------------*/
256 static int bond_init(struct net_device *bond_dev);
257 static void bond_uninit(struct net_device *bond_dev);
258 static void bond_get_stats(struct net_device *bond_dev,
259 struct rtnl_link_stats64 *stats);
260 static void bond_slave_arr_handler(struct work_struct *work);
261 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
263 static void bond_netdev_notify_work(struct work_struct *work);
265 /*---------------------------- General routines -----------------------------*/
267 const char *bond_mode_name(int mode)
269 static const char *names[] = {
270 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
271 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
272 [BOND_MODE_XOR] = "load balancing (xor)",
273 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
274 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
275 [BOND_MODE_TLB] = "transmit load balancing",
276 [BOND_MODE_ALB] = "adaptive load balancing",
279 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
286 * bond_dev_queue_xmit - Prepare skb for xmit.
288 * @bond: bond device that got this skb for tx.
289 * @skb: hw accel VLAN tagged skb to transmit
290 * @slave_dev: slave that is supposed to xmit this skbuff
292 netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
293 struct net_device *slave_dev)
295 skb->dev = slave_dev;
297 BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
298 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
299 skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
301 if (unlikely(netpoll_tx_running(bond->dev)))
302 return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
304 return dev_queue_xmit(skb);
307 bool bond_sk_check(struct bonding *bond)
309 switch (BOND_MODE(bond)) {
310 case BOND_MODE_8023AD:
312 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
320 static bool bond_xdp_check(struct bonding *bond)
322 switch (BOND_MODE(bond)) {
323 case BOND_MODE_ROUNDROBIN:
324 case BOND_MODE_ACTIVEBACKUP:
326 case BOND_MODE_8023AD:
328 /* vlan+srcmac is not supported with XDP as in most cases the 802.1q
329 * payload is not in the packet due to hardware offload.
331 if (bond->params.xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC)
339 /*---------------------------------- VLAN -----------------------------------*/
341 /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
342 * We don't protect the slave list iteration with a lock because:
343 * a. This operation is performed in IOCTL context,
344 * b. The operation is protected by the RTNL semaphore in the 8021q code,
345 * c. Holding a lock with BH disabled while directly calling a base driver
346 * entry point is generally a BAD idea.
348 * The design of synchronization/protection for this operation in the 8021q
349 * module is good for one or more VLAN devices over a single physical device
350 * and cannot be extended for a teaming solution like bonding, so there is a
351 * potential race condition here where a net device from the vlan group might
352 * be referenced (either by a base driver or the 8021q code) while it is being
353 * removed from the system. However, it turns out we're not making matters
354 * worse, and if it works for regular VLAN usage it will work here too.
358 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
359 * @bond_dev: bonding net device that got called
360 * @proto: network protocol ID
361 * @vid: vlan id being added
363 static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
364 __be16 proto, u16 vid)
366 struct bonding *bond = netdev_priv(bond_dev);
367 struct slave *slave, *rollback_slave;
368 struct list_head *iter;
371 bond_for_each_slave(bond, slave, iter) {
372 res = vlan_vid_add(slave->dev, proto, vid);
380 /* unwind to the slave that failed */
381 bond_for_each_slave(bond, rollback_slave, iter) {
382 if (rollback_slave == slave)
385 vlan_vid_del(rollback_slave->dev, proto, vid);
392 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
393 * @bond_dev: bonding net device that got called
394 * @proto: network protocol ID
395 * @vid: vlan id being removed
397 static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
398 __be16 proto, u16 vid)
400 struct bonding *bond = netdev_priv(bond_dev);
401 struct list_head *iter;
404 bond_for_each_slave(bond, slave, iter)
405 vlan_vid_del(slave->dev, proto, vid);
407 if (bond_is_lb(bond))
408 bond_alb_clear_vlan(bond, vid);
413 /*---------------------------------- XFRM -----------------------------------*/
415 #ifdef CONFIG_XFRM_OFFLOAD
417 * bond_ipsec_add_sa - program device with a security association
418 * @xs: pointer to transformer state struct
420 static int bond_ipsec_add_sa(struct xfrm_state *xs)
422 struct net_device *bond_dev = xs->xso.dev;
423 struct bond_ipsec *ipsec;
424 struct bonding *bond;
432 bond = netdev_priv(bond_dev);
433 slave = rcu_dereference(bond->curr_active_slave);
439 if (!slave->dev->xfrmdev_ops ||
440 !slave->dev->xfrmdev_ops->xdo_dev_state_add ||
441 netif_is_bond_master(slave->dev)) {
442 slave_warn(bond_dev, slave->dev, "Slave does not support ipsec offload\n");
447 ipsec = kmalloc(sizeof(*ipsec), GFP_ATOMIC);
452 xs->xso.real_dev = slave->dev;
454 err = slave->dev->xfrmdev_ops->xdo_dev_state_add(xs);
457 INIT_LIST_HEAD(&ipsec->list);
458 spin_lock_bh(&bond->ipsec_lock);
459 list_add(&ipsec->list, &bond->ipsec_list);
460 spin_unlock_bh(&bond->ipsec_lock);
468 static void bond_ipsec_add_sa_all(struct bonding *bond)
470 struct net_device *bond_dev = bond->dev;
471 struct bond_ipsec *ipsec;
475 slave = rcu_dereference(bond->curr_active_slave);
479 if (!slave->dev->xfrmdev_ops ||
480 !slave->dev->xfrmdev_ops->xdo_dev_state_add ||
481 netif_is_bond_master(slave->dev)) {
482 spin_lock_bh(&bond->ipsec_lock);
483 if (!list_empty(&bond->ipsec_list))
484 slave_warn(bond_dev, slave->dev,
485 "%s: no slave xdo_dev_state_add\n",
487 spin_unlock_bh(&bond->ipsec_lock);
491 spin_lock_bh(&bond->ipsec_lock);
492 list_for_each_entry(ipsec, &bond->ipsec_list, list) {
493 ipsec->xs->xso.real_dev = slave->dev;
494 if (slave->dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs)) {
495 slave_warn(bond_dev, slave->dev, "%s: failed to add SA\n", __func__);
496 ipsec->xs->xso.real_dev = NULL;
499 spin_unlock_bh(&bond->ipsec_lock);
505 * bond_ipsec_del_sa - clear out this specific SA
506 * @xs: pointer to transformer state struct
508 static void bond_ipsec_del_sa(struct xfrm_state *xs)
510 struct net_device *bond_dev = xs->xso.dev;
511 struct bond_ipsec *ipsec;
512 struct bonding *bond;
519 bond = netdev_priv(bond_dev);
520 slave = rcu_dereference(bond->curr_active_slave);
525 if (!xs->xso.real_dev)
528 WARN_ON(xs->xso.real_dev != slave->dev);
530 if (!slave->dev->xfrmdev_ops ||
531 !slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
532 netif_is_bond_master(slave->dev)) {
533 slave_warn(bond_dev, slave->dev, "%s: no slave xdo_dev_state_delete\n", __func__);
537 slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs);
539 spin_lock_bh(&bond->ipsec_lock);
540 list_for_each_entry(ipsec, &bond->ipsec_list, list) {
541 if (ipsec->xs == xs) {
542 list_del(&ipsec->list);
547 spin_unlock_bh(&bond->ipsec_lock);
551 static void bond_ipsec_del_sa_all(struct bonding *bond)
553 struct net_device *bond_dev = bond->dev;
554 struct bond_ipsec *ipsec;
558 slave = rcu_dereference(bond->curr_active_slave);
564 spin_lock_bh(&bond->ipsec_lock);
565 list_for_each_entry(ipsec, &bond->ipsec_list, list) {
566 if (!ipsec->xs->xso.real_dev)
569 if (!slave->dev->xfrmdev_ops ||
570 !slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
571 netif_is_bond_master(slave->dev)) {
572 slave_warn(bond_dev, slave->dev,
573 "%s: no slave xdo_dev_state_delete\n",
576 slave->dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs);
578 ipsec->xs->xso.real_dev = NULL;
580 spin_unlock_bh(&bond->ipsec_lock);
585 * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
586 * @skb: current data packet
587 * @xs: pointer to transformer state struct
589 static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
591 struct net_device *bond_dev = xs->xso.dev;
592 struct net_device *real_dev;
593 struct slave *curr_active;
594 struct bonding *bond;
597 bond = netdev_priv(bond_dev);
599 curr_active = rcu_dereference(bond->curr_active_slave);
600 real_dev = curr_active->dev;
602 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
607 if (!xs->xso.real_dev) {
612 if (!real_dev->xfrmdev_ops ||
613 !real_dev->xfrmdev_ops->xdo_dev_offload_ok ||
614 netif_is_bond_master(real_dev)) {
619 err = real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
625 static const struct xfrmdev_ops bond_xfrmdev_ops = {
626 .xdo_dev_state_add = bond_ipsec_add_sa,
627 .xdo_dev_state_delete = bond_ipsec_del_sa,
628 .xdo_dev_offload_ok = bond_ipsec_offload_ok,
630 #endif /* CONFIG_XFRM_OFFLOAD */
632 /*------------------------------- Link status -------------------------------*/
634 /* Set the carrier state for the master according to the state of its
635 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
636 * do special 802.3ad magic.
638 * Returns zero if carrier state does not change, nonzero if it does.
640 int bond_set_carrier(struct bonding *bond)
642 struct list_head *iter;
645 if (!bond_has_slaves(bond))
648 if (BOND_MODE(bond) == BOND_MODE_8023AD)
649 return bond_3ad_set_carrier(bond);
651 bond_for_each_slave(bond, slave, iter) {
652 if (slave->link == BOND_LINK_UP) {
653 if (!netif_carrier_ok(bond->dev)) {
654 netif_carrier_on(bond->dev);
662 if (netif_carrier_ok(bond->dev)) {
663 netif_carrier_off(bond->dev);
669 /* Get link speed and duplex from the slave's base driver
670 * using ethtool. If for some reason the call fails or the
671 * values are invalid, set speed and duplex to -1,
672 * and return. Return 1 if speed or duplex settings are
673 * UNKNOWN; 0 otherwise.
675 static int bond_update_speed_duplex(struct slave *slave)
677 struct net_device *slave_dev = slave->dev;
678 struct ethtool_link_ksettings ecmd;
681 slave->speed = SPEED_UNKNOWN;
682 slave->duplex = DUPLEX_UNKNOWN;
684 res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
687 if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
689 switch (ecmd.base.duplex) {
697 slave->speed = ecmd.base.speed;
698 slave->duplex = ecmd.base.duplex;
703 const char *bond_slave_link_status(s8 link)
719 /* if <dev> supports MII link status reporting, check its link status.
721 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
722 * depending upon the setting of the use_carrier parameter.
724 * Return either BMSR_LSTATUS, meaning that the link is up (or we
725 * can't tell and just pretend it is), or 0, meaning that the link is
728 * If reporting is non-zero, instead of faking link up, return -1 if
729 * both ETHTOOL and MII ioctls fail (meaning the device does not
730 * support them). If use_carrier is set, return whatever it says.
731 * It'd be nice if there was a good way to tell if a driver supports
732 * netif_carrier, but there really isn't.
734 static int bond_check_dev_link(struct bonding *bond,
735 struct net_device *slave_dev, int reporting)
737 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
738 int (*ioctl)(struct net_device *, struct ifreq *, int);
740 struct mii_ioctl_data *mii;
742 if (!reporting && !netif_running(slave_dev))
745 if (bond->params.use_carrier)
746 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
748 /* Try to get link status using Ethtool first. */
749 if (slave_dev->ethtool_ops->get_link)
750 return slave_dev->ethtool_ops->get_link(slave_dev) ?
753 /* Ethtool can't be used, fallback to MII ioctls. */
754 ioctl = slave_ops->ndo_eth_ioctl;
756 /* TODO: set pointer to correct ioctl on a per team member
757 * bases to make this more efficient. that is, once
758 * we determine the correct ioctl, we will always
759 * call it and not the others for that team
763 /* We cannot assume that SIOCGMIIPHY will also read a
764 * register; not all network drivers (e.g., e100)
768 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
769 strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
771 if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
772 mii->reg_num = MII_BMSR;
773 if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0)
774 return mii->val_out & BMSR_LSTATUS;
778 /* If reporting, report that either there's no ndo_eth_ioctl,
779 * or both SIOCGMIIREG and get_link failed (meaning that we
780 * cannot report link status). If not reporting, pretend
783 return reporting ? -1 : BMSR_LSTATUS;
786 /*----------------------------- Multicast list ------------------------------*/
788 /* Push the promiscuity flag down to appropriate slaves */
789 static int bond_set_promiscuity(struct bonding *bond, int inc)
791 struct list_head *iter;
794 if (bond_uses_primary(bond)) {
795 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
798 err = dev_set_promiscuity(curr_active->dev, inc);
802 bond_for_each_slave(bond, slave, iter) {
803 err = dev_set_promiscuity(slave->dev, inc);
811 /* Push the allmulti flag down to all slaves */
812 static int bond_set_allmulti(struct bonding *bond, int inc)
814 struct list_head *iter;
817 if (bond_uses_primary(bond)) {
818 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
821 err = dev_set_allmulti(curr_active->dev, inc);
825 bond_for_each_slave(bond, slave, iter) {
826 err = dev_set_allmulti(slave->dev, inc);
834 /* Retrieve the list of registered multicast addresses for the bonding
835 * device and retransmit an IGMP JOIN request to the current active
838 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
840 struct bonding *bond = container_of(work, struct bonding,
843 if (!rtnl_trylock()) {
844 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
847 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
849 if (bond->igmp_retrans > 1) {
850 bond->igmp_retrans--;
851 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
856 /* Flush bond's hardware addresses from slave */
857 static void bond_hw_addr_flush(struct net_device *bond_dev,
858 struct net_device *slave_dev)
860 struct bonding *bond = netdev_priv(bond_dev);
862 dev_uc_unsync(slave_dev, bond_dev);
863 dev_mc_unsync(slave_dev, bond_dev);
865 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
866 /* del lacpdu mc addr from mc list */
867 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
869 dev_mc_del(slave_dev, lacpdu_multicast);
873 /*--------------------------- Active slave change ---------------------------*/
875 /* Update the hardware address list and promisc/allmulti for the new and
876 * old active slaves (if any). Modes that are not using primary keep all
877 * slaves up date at all times; only the modes that use primary need to call
878 * this function to swap these settings during a failover.
880 static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
881 struct slave *old_active)
884 if (bond->dev->flags & IFF_PROMISC)
885 dev_set_promiscuity(old_active->dev, -1);
887 if (bond->dev->flags & IFF_ALLMULTI)
888 dev_set_allmulti(old_active->dev, -1);
890 bond_hw_addr_flush(bond->dev, old_active->dev);
894 /* FIXME: Signal errors upstream. */
895 if (bond->dev->flags & IFF_PROMISC)
896 dev_set_promiscuity(new_active->dev, 1);
898 if (bond->dev->flags & IFF_ALLMULTI)
899 dev_set_allmulti(new_active->dev, 1);
901 netif_addr_lock_bh(bond->dev);
902 dev_uc_sync(new_active->dev, bond->dev);
903 dev_mc_sync(new_active->dev, bond->dev);
904 netif_addr_unlock_bh(bond->dev);
909 * bond_set_dev_addr - clone slave's address to bond
910 * @bond_dev: bond net device
911 * @slave_dev: slave net device
913 * Should be called with RTNL held.
915 static int bond_set_dev_addr(struct net_device *bond_dev,
916 struct net_device *slave_dev)
920 slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
921 bond_dev, slave_dev, slave_dev->addr_len);
922 err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
926 __dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len);
927 bond_dev->addr_assign_type = NET_ADDR_STOLEN;
928 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
932 static struct slave *bond_get_old_active(struct bonding *bond,
933 struct slave *new_active)
936 struct list_head *iter;
938 bond_for_each_slave(bond, slave, iter) {
939 if (slave == new_active)
942 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
949 /* bond_do_fail_over_mac
951 * Perform special MAC address swapping for fail_over_mac settings
955 static void bond_do_fail_over_mac(struct bonding *bond,
956 struct slave *new_active,
957 struct slave *old_active)
959 u8 tmp_mac[MAX_ADDR_LEN];
960 struct sockaddr_storage ss;
963 switch (bond->params.fail_over_mac) {
964 case BOND_FOM_ACTIVE:
966 rv = bond_set_dev_addr(bond->dev, new_active->dev);
968 slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
972 case BOND_FOM_FOLLOW:
973 /* if new_active && old_active, swap them
974 * if just old_active, do nothing (going to no active slave)
975 * if just new_active, set new_active to bond's MAC
981 old_active = bond_get_old_active(bond, new_active);
984 bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
985 new_active->dev->addr_len);
986 bond_hw_addr_copy(ss.__data,
987 old_active->dev->dev_addr,
988 old_active->dev->addr_len);
989 ss.ss_family = new_active->dev->type;
991 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
992 bond->dev->addr_len);
993 ss.ss_family = bond->dev->type;
996 rv = dev_set_mac_address(new_active->dev,
997 (struct sockaddr *)&ss, NULL);
999 slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
1007 bond_hw_addr_copy(ss.__data, tmp_mac,
1008 new_active->dev->addr_len);
1009 ss.ss_family = old_active->dev->type;
1011 rv = dev_set_mac_address(old_active->dev,
1012 (struct sockaddr *)&ss, NULL);
1014 slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
1019 netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
1020 bond->params.fail_over_mac);
1026 static struct slave *bond_choose_primary_or_current(struct bonding *bond)
1028 struct slave *prim = rtnl_dereference(bond->primary_slave);
1029 struct slave *curr = rtnl_dereference(bond->curr_active_slave);
1031 if (!prim || prim->link != BOND_LINK_UP) {
1032 if (!curr || curr->link != BOND_LINK_UP)
1037 if (bond->force_primary) {
1038 bond->force_primary = false;
1042 if (!curr || curr->link != BOND_LINK_UP)
1045 /* At this point, prim and curr are both up */
1046 switch (bond->params.primary_reselect) {
1047 case BOND_PRI_RESELECT_ALWAYS:
1049 case BOND_PRI_RESELECT_BETTER:
1050 if (prim->speed < curr->speed)
1052 if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
1055 case BOND_PRI_RESELECT_FAILURE:
1058 netdev_err(bond->dev, "impossible primary_reselect %d\n",
1059 bond->params.primary_reselect);
1065 * bond_find_best_slave - select the best available slave to be the active one
1066 * @bond: our bonding struct
1068 static struct slave *bond_find_best_slave(struct bonding *bond)
1070 struct slave *slave, *bestslave = NULL;
1071 struct list_head *iter;
1072 int mintime = bond->params.updelay;
1074 slave = bond_choose_primary_or_current(bond);
1078 bond_for_each_slave(bond, slave, iter) {
1079 if (slave->link == BOND_LINK_UP)
1081 if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
1082 slave->delay < mintime) {
1083 mintime = slave->delay;
1091 static bool bond_should_notify_peers(struct bonding *bond)
1093 struct slave *slave;
1096 slave = rcu_dereference(bond->curr_active_slave);
1099 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
1100 slave ? slave->dev->name : "NULL");
1102 if (!slave || !bond->send_peer_notif ||
1103 bond->send_peer_notif %
1104 max(1, bond->params.peer_notif_delay) != 0 ||
1105 !netif_carrier_ok(bond->dev) ||
1106 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
1113 * bond_change_active_slave - change the active slave into the specified one
1114 * @bond: our bonding struct
1115 * @new_active: the new slave to make the active one
1117 * Set the new slave to the bond's settings and unset them on the old
1118 * curr_active_slave.
1119 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1121 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1122 * because it is apparently the best available slave we have, even though its
1123 * updelay hasn't timed out yet.
1125 * Caller must hold RTNL.
1127 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1129 struct slave *old_active;
1133 old_active = rtnl_dereference(bond->curr_active_slave);
1135 if (old_active == new_active)
1138 #ifdef CONFIG_XFRM_OFFLOAD
1139 bond_ipsec_del_sa_all(bond);
1140 #endif /* CONFIG_XFRM_OFFLOAD */
1143 new_active->last_link_up = jiffies;
1145 if (new_active->link == BOND_LINK_BACK) {
1146 if (bond_uses_primary(bond)) {
1147 slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
1148 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1151 new_active->delay = 0;
1152 bond_set_slave_link_state(new_active, BOND_LINK_UP,
1153 BOND_SLAVE_NOTIFY_NOW);
1155 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1156 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1158 if (bond_is_lb(bond))
1159 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1161 if (bond_uses_primary(bond))
1162 slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
1166 if (bond_uses_primary(bond))
1167 bond_hw_addr_swap(bond, new_active, old_active);
1169 if (bond_is_lb(bond)) {
1170 bond_alb_handle_active_change(bond, new_active);
1172 bond_set_slave_inactive_flags(old_active,
1173 BOND_SLAVE_NOTIFY_NOW);
1175 bond_set_slave_active_flags(new_active,
1176 BOND_SLAVE_NOTIFY_NOW);
1178 rcu_assign_pointer(bond->curr_active_slave, new_active);
1181 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
1183 bond_set_slave_inactive_flags(old_active,
1184 BOND_SLAVE_NOTIFY_NOW);
1187 bool should_notify_peers = false;
1189 bond_set_slave_active_flags(new_active,
1190 BOND_SLAVE_NOTIFY_NOW);
1192 if (bond->params.fail_over_mac)
1193 bond_do_fail_over_mac(bond, new_active,
1196 if (netif_running(bond->dev)) {
1197 bond->send_peer_notif =
1198 bond->params.num_peer_notif *
1199 max(1, bond->params.peer_notif_delay);
1200 should_notify_peers =
1201 bond_should_notify_peers(bond);
1204 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
1205 if (should_notify_peers) {
1206 bond->send_peer_notif--;
1207 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
1213 #ifdef CONFIG_XFRM_OFFLOAD
1214 bond_ipsec_add_sa_all(bond);
1215 #endif /* CONFIG_XFRM_OFFLOAD */
1217 /* resend IGMP joins since active slave has changed or
1218 * all were sent on curr_active_slave.
1219 * resend only if bond is brought up with the affected
1220 * bonding modes and the retransmission is enabled
1222 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1223 ((bond_uses_primary(bond) && new_active) ||
1224 BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
1225 bond->igmp_retrans = bond->params.resend_igmp;
1226 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
1231 * bond_select_active_slave - select a new active slave, if needed
1232 * @bond: our bonding struct
1234 * This functions should be called when one of the following occurs:
1235 * - The old curr_active_slave has been released or lost its link.
1236 * - The primary_slave has got its link back.
1237 * - A slave has got its link back and there's no old curr_active_slave.
1239 * Caller must hold RTNL.
1241 void bond_select_active_slave(struct bonding *bond)
1243 struct slave *best_slave;
1248 best_slave = bond_find_best_slave(bond);
1249 if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
1250 bond_change_active_slave(bond, best_slave);
1251 rv = bond_set_carrier(bond);
1255 if (netif_carrier_ok(bond->dev))
1256 netdev_info(bond->dev, "active interface up!\n");
1258 netdev_info(bond->dev, "now running without any active interface!\n");
1262 #ifdef CONFIG_NET_POLL_CONTROLLER
1263 static inline int slave_enable_netpoll(struct slave *slave)
1268 np = kzalloc(sizeof(*np), GFP_KERNEL);
1273 err = __netpoll_setup(np, slave->dev);
1282 static inline void slave_disable_netpoll(struct slave *slave)
1284 struct netpoll *np = slave->np;
1294 static void bond_poll_controller(struct net_device *bond_dev)
1296 struct bonding *bond = netdev_priv(bond_dev);
1297 struct slave *slave = NULL;
1298 struct list_head *iter;
1299 struct ad_info ad_info;
1301 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1302 if (bond_3ad_get_active_agg_info(bond, &ad_info))
1305 bond_for_each_slave_rcu(bond, slave, iter) {
1306 if (!bond_slave_is_up(slave))
1309 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1310 struct aggregator *agg =
1311 SLAVE_AD_INFO(slave)->port.aggregator;
1314 agg->aggregator_identifier != ad_info.aggregator_id)
1318 netpoll_poll_dev(slave->dev);
1322 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1324 struct bonding *bond = netdev_priv(bond_dev);
1325 struct list_head *iter;
1326 struct slave *slave;
1328 bond_for_each_slave(bond, slave, iter)
1329 if (bond_slave_is_up(slave))
1330 slave_disable_netpoll(slave);
1333 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1335 struct bonding *bond = netdev_priv(dev);
1336 struct list_head *iter;
1337 struct slave *slave;
1340 bond_for_each_slave(bond, slave, iter) {
1341 err = slave_enable_netpoll(slave);
1343 bond_netpoll_cleanup(dev);
1350 static inline int slave_enable_netpoll(struct slave *slave)
1354 static inline void slave_disable_netpoll(struct slave *slave)
1357 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1362 /*---------------------------------- IOCTL ----------------------------------*/
1364 static netdev_features_t bond_fix_features(struct net_device *dev,
1365 netdev_features_t features)
1367 struct bonding *bond = netdev_priv(dev);
1368 struct list_head *iter;
1369 netdev_features_t mask;
1370 struct slave *slave;
1372 #if IS_ENABLED(CONFIG_TLS_DEVICE)
1373 if (bond_sk_check(bond))
1374 features |= BOND_TLS_FEATURES;
1376 features &= ~BOND_TLS_FEATURES;
1381 features &= ~NETIF_F_ONE_FOR_ALL;
1382 features |= NETIF_F_ALL_FOR_ALL;
1384 bond_for_each_slave(bond, slave, iter) {
1385 features = netdev_increment_features(features,
1386 slave->dev->features,
1389 features = netdev_add_tso_features(features, mask);
1394 #define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1395 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \
1396 NETIF_F_HIGHDMA | NETIF_F_LRO)
1398 #define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1399 NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE)
1401 #define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1402 NETIF_F_GSO_SOFTWARE)
1405 static void bond_compute_features(struct bonding *bond)
1407 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
1408 IFF_XMIT_DST_RELEASE_PERM;
1409 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
1410 netdev_features_t enc_features = BOND_ENC_FEATURES;
1411 #ifdef CONFIG_XFRM_OFFLOAD
1412 netdev_features_t xfrm_features = BOND_XFRM_FEATURES;
1413 #endif /* CONFIG_XFRM_OFFLOAD */
1414 netdev_features_t mpls_features = BOND_MPLS_FEATURES;
1415 struct net_device *bond_dev = bond->dev;
1416 struct list_head *iter;
1417 struct slave *slave;
1418 unsigned short max_hard_header_len = ETH_HLEN;
1419 unsigned int gso_max_size = GSO_MAX_SIZE;
1420 u16 gso_max_segs = GSO_MAX_SEGS;
1422 if (!bond_has_slaves(bond))
1424 vlan_features &= NETIF_F_ALL_FOR_ALL;
1425 mpls_features &= NETIF_F_ALL_FOR_ALL;
1427 bond_for_each_slave(bond, slave, iter) {
1428 vlan_features = netdev_increment_features(vlan_features,
1429 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1431 enc_features = netdev_increment_features(enc_features,
1432 slave->dev->hw_enc_features,
1435 #ifdef CONFIG_XFRM_OFFLOAD
1436 xfrm_features = netdev_increment_features(xfrm_features,
1437 slave->dev->hw_enc_features,
1438 BOND_XFRM_FEATURES);
1439 #endif /* CONFIG_XFRM_OFFLOAD */
1441 mpls_features = netdev_increment_features(mpls_features,
1442 slave->dev->mpls_features,
1443 BOND_MPLS_FEATURES);
1445 dst_release_flag &= slave->dev->priv_flags;
1446 if (slave->dev->hard_header_len > max_hard_header_len)
1447 max_hard_header_len = slave->dev->hard_header_len;
1449 gso_max_size = min(gso_max_size, slave->dev->gso_max_size);
1450 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs);
1452 bond_dev->hard_header_len = max_hard_header_len;
1455 bond_dev->vlan_features = vlan_features;
1456 bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
1457 NETIF_F_HW_VLAN_CTAG_TX |
1458 NETIF_F_HW_VLAN_STAG_TX;
1459 #ifdef CONFIG_XFRM_OFFLOAD
1460 bond_dev->hw_enc_features |= xfrm_features;
1461 #endif /* CONFIG_XFRM_OFFLOAD */
1462 bond_dev->mpls_features = mpls_features;
1463 bond_dev->gso_max_segs = gso_max_segs;
1464 netif_set_gso_max_size(bond_dev, gso_max_size);
1466 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1467 if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
1468 dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1469 bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1471 netdev_change_features(bond_dev);
1474 static void bond_setup_by_slave(struct net_device *bond_dev,
1475 struct net_device *slave_dev)
1477 bond_dev->header_ops = slave_dev->header_ops;
1479 bond_dev->type = slave_dev->type;
1480 bond_dev->hard_header_len = slave_dev->hard_header_len;
1481 bond_dev->needed_headroom = slave_dev->needed_headroom;
1482 bond_dev->addr_len = slave_dev->addr_len;
1484 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1485 slave_dev->addr_len);
1488 /* On bonding slaves other than the currently active slave, suppress
1489 * duplicates except for alb non-mcast/bcast.
1491 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1492 struct slave *slave,
1493 struct bonding *bond)
1495 if (bond_is_slave_inactive(slave)) {
1496 if (BOND_MODE(bond) == BOND_MODE_ALB &&
1497 skb->pkt_type != PACKET_BROADCAST &&
1498 skb->pkt_type != PACKET_MULTICAST)
1505 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1507 struct sk_buff *skb = *pskb;
1508 struct slave *slave;
1509 struct bonding *bond;
1510 int (*recv_probe)(const struct sk_buff *, struct bonding *,
1512 int ret = RX_HANDLER_ANOTHER;
1514 skb = skb_share_check(skb, GFP_ATOMIC);
1516 return RX_HANDLER_CONSUMED;
1520 slave = bond_slave_get_rcu(skb->dev);
1523 recv_probe = READ_ONCE(bond->recv_probe);
1525 ret = recv_probe(skb, bond, slave);
1526 if (ret == RX_HANDLER_CONSUMED) {
1533 * For packets determined by bond_should_deliver_exact_match() call to
1534 * be suppressed we want to make an exception for link-local packets.
1535 * This is necessary for e.g. LLDP daemons to be able to monitor
1536 * inactive slave links without being forced to bind to them
1539 * At the same time, packets that are passed to the bonding master
1540 * (including link-local ones) can have their originating interface
1541 * determined via PACKET_ORIGDEV socket option.
1543 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1544 if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
1545 return RX_HANDLER_PASS;
1546 return RX_HANDLER_EXACT;
1549 skb->dev = bond->dev;
1551 if (BOND_MODE(bond) == BOND_MODE_ALB &&
1552 netif_is_bridge_port(bond->dev) &&
1553 skb->pkt_type == PACKET_HOST) {
1555 if (unlikely(skb_cow_head(skb,
1556 skb->data - skb_mac_header(skb)))) {
1558 return RX_HANDLER_CONSUMED;
1560 bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
1561 bond->dev->addr_len);
1567 static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
1569 switch (BOND_MODE(bond)) {
1570 case BOND_MODE_ROUNDROBIN:
1571 return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
1572 case BOND_MODE_ACTIVEBACKUP:
1573 return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
1574 case BOND_MODE_BROADCAST:
1575 return NETDEV_LAG_TX_TYPE_BROADCAST;
1577 case BOND_MODE_8023AD:
1578 return NETDEV_LAG_TX_TYPE_HASH;
1580 return NETDEV_LAG_TX_TYPE_UNKNOWN;
1584 static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
1585 enum netdev_lag_tx_type type)
1587 if (type != NETDEV_LAG_TX_TYPE_HASH)
1588 return NETDEV_LAG_HASH_NONE;
1590 switch (bond->params.xmit_policy) {
1591 case BOND_XMIT_POLICY_LAYER2:
1592 return NETDEV_LAG_HASH_L2;
1593 case BOND_XMIT_POLICY_LAYER34:
1594 return NETDEV_LAG_HASH_L34;
1595 case BOND_XMIT_POLICY_LAYER23:
1596 return NETDEV_LAG_HASH_L23;
1597 case BOND_XMIT_POLICY_ENCAP23:
1598 return NETDEV_LAG_HASH_E23;
1599 case BOND_XMIT_POLICY_ENCAP34:
1600 return NETDEV_LAG_HASH_E34;
1601 case BOND_XMIT_POLICY_VLAN_SRCMAC:
1602 return NETDEV_LAG_HASH_VLAN_SRCMAC;
1604 return NETDEV_LAG_HASH_UNKNOWN;
1608 static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
1609 struct netlink_ext_ack *extack)
1611 struct netdev_lag_upper_info lag_upper_info;
1612 enum netdev_lag_tx_type type;
1614 type = bond_lag_tx_type(bond);
1615 lag_upper_info.tx_type = type;
1616 lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
1618 return netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
1619 &lag_upper_info, extack);
1622 static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
1624 netdev_upper_dev_unlink(slave->dev, bond->dev);
1625 slave->dev->flags &= ~IFF_SLAVE;
1628 static void slave_kobj_release(struct kobject *kobj)
1630 struct slave *slave = to_slave(kobj);
1631 struct bonding *bond = bond_get_bond_by_slave(slave);
1633 cancel_delayed_work_sync(&slave->notify_work);
1634 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1635 kfree(SLAVE_AD_INFO(slave));
1640 static struct kobj_type slave_ktype = {
1641 .release = slave_kobj_release,
1643 .sysfs_ops = &slave_sysfs_ops,
1647 static int bond_kobj_init(struct slave *slave)
1651 err = kobject_init_and_add(&slave->kobj, &slave_ktype,
1652 &(slave->dev->dev.kobj), "bonding_slave");
1654 kobject_put(&slave->kobj);
1659 static struct slave *bond_alloc_slave(struct bonding *bond,
1660 struct net_device *slave_dev)
1662 struct slave *slave = NULL;
1664 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1669 slave->dev = slave_dev;
1670 INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
1672 if (bond_kobj_init(slave))
1675 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1676 SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
1678 if (!SLAVE_AD_INFO(slave)) {
1679 kobject_put(&slave->kobj);
1687 static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
1689 info->bond_mode = BOND_MODE(bond);
1690 info->miimon = bond->params.miimon;
1691 info->num_slaves = bond->slave_cnt;
1694 static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
1696 strcpy(info->slave_name, slave->dev->name);
1697 info->link = slave->link;
1698 info->state = bond_slave_state(slave);
1699 info->link_failure_count = slave->link_failure_count;
1702 static void bond_netdev_notify_work(struct work_struct *_work)
1704 struct slave *slave = container_of(_work, struct slave,
1707 if (rtnl_trylock()) {
1708 struct netdev_bonding_info binfo;
1710 bond_fill_ifslave(slave, &binfo.slave);
1711 bond_fill_ifbond(slave->bond, &binfo.master);
1712 netdev_bonding_info_change(slave->dev, &binfo);
1715 queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
1719 void bond_queue_slave_event(struct slave *slave)
1721 queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
1724 void bond_lower_state_changed(struct slave *slave)
1726 struct netdev_lag_lower_state_info info;
1728 info.link_up = slave->link == BOND_LINK_UP ||
1729 slave->link == BOND_LINK_FAIL;
1730 info.tx_enabled = bond_is_active_slave(slave);
1731 netdev_lower_state_changed(slave->dev, &info);
1734 #define BOND_NL_ERR(bond_dev, extack, errmsg) do { \
1736 NL_SET_ERR_MSG(extack, errmsg); \
1738 netdev_err(bond_dev, "Error: %s\n", errmsg); \
1741 #define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do { \
1743 NL_SET_ERR_MSG(extack, errmsg); \
1745 slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg); \
1748 /* enslave device <slave> to bond device <master> */
1749 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
1750 struct netlink_ext_ack *extack)
1752 struct bonding *bond = netdev_priv(bond_dev);
1753 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1754 struct slave *new_slave = NULL, *prev_slave;
1755 struct sockaddr_storage ss;
1759 if (slave_dev->flags & IFF_MASTER &&
1760 !netif_is_bond_master(slave_dev)) {
1761 BOND_NL_ERR(bond_dev, extack,
1762 "Device type (master device) cannot be enslaved");
1766 if (!bond->params.use_carrier &&
1767 slave_dev->ethtool_ops->get_link == NULL &&
1768 slave_ops->ndo_eth_ioctl == NULL) {
1769 slave_warn(bond_dev, slave_dev, "no link monitoring support\n");
1772 /* already in-use? */
1773 if (netdev_is_rx_handler_busy(slave_dev)) {
1774 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1775 "Device is in use and cannot be enslaved");
1779 if (bond_dev == slave_dev) {
1780 BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself.");
1784 /* vlan challenged mutual exclusion */
1785 /* no need to lock since we're protected by rtnl_lock */
1786 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1787 slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
1788 if (vlan_uses_dev(bond_dev)) {
1789 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1790 "Can not enslave VLAN challenged device to VLAN enabled bond");
1793 slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
1796 slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
1799 if (slave_dev->features & NETIF_F_HW_ESP)
1800 slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
1802 /* Old ifenslave binaries are no longer supported. These can
1803 * be identified with moderate accuracy by the state of the slave:
1804 * the current ifenslave will set the interface down prior to
1805 * enslaving it; the old ifenslave will not.
1807 if (slave_dev->flags & IFF_UP) {
1808 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1809 "Device can not be enslaved while up");
1813 /* set bonding device ether type by slave - bonding netdevices are
1814 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1815 * there is a need to override some of the type dependent attribs/funcs.
1817 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1818 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1820 if (!bond_has_slaves(bond)) {
1821 if (bond_dev->type != slave_dev->type) {
1822 slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
1823 bond_dev->type, slave_dev->type);
1825 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1827 res = notifier_to_errno(res);
1829 slave_err(bond_dev, slave_dev, "refused to change device type\n");
1833 /* Flush unicast and multicast addresses */
1834 dev_uc_flush(bond_dev);
1835 dev_mc_flush(bond_dev);
1837 if (slave_dev->type != ARPHRD_ETHER)
1838 bond_setup_by_slave(bond_dev, slave_dev);
1840 ether_setup(bond_dev);
1841 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1844 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1847 } else if (bond_dev->type != slave_dev->type) {
1848 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1849 "Device type is different from other slaves");
1853 if (slave_dev->type == ARPHRD_INFINIBAND &&
1854 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
1855 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1856 "Only active-backup mode is supported for infiniband slaves");
1858 goto err_undo_flags;
1861 if (!slave_ops->ndo_set_mac_address ||
1862 slave_dev->type == ARPHRD_INFINIBAND) {
1863 slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
1864 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1865 bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1866 if (!bond_has_slaves(bond)) {
1867 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1868 slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
1870 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1871 "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
1873 goto err_undo_flags;
1878 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1880 /* If this is the first slave, then we need to set the master's hardware
1881 * address to be the same as the slave's.
1883 if (!bond_has_slaves(bond) &&
1884 bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
1885 res = bond_set_dev_addr(bond->dev, slave_dev);
1887 goto err_undo_flags;
1890 new_slave = bond_alloc_slave(bond, slave_dev);
1893 goto err_undo_flags;
1896 /* Set the new_slave's queue_id to be zero. Queue ID mapping
1897 * is set via sysfs or module option if desired.
1899 new_slave->queue_id = 0;
1901 /* Save slave's original mtu and then set it to match the bond */
1902 new_slave->original_mtu = slave_dev->mtu;
1903 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1905 slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
1909 /* Save slave's original ("permanent") mac address for modes
1910 * that need it, and for restoring it upon release, and then
1911 * set it to the master's address
1913 bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
1914 slave_dev->addr_len);
1916 if (!bond->params.fail_over_mac ||
1917 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
1918 /* Set slave to master's mac address. The application already
1919 * set the master's mac address to that of the first slave
1921 memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
1922 ss.ss_family = slave_dev->type;
1923 res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss,
1926 slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
1927 goto err_restore_mtu;
1931 /* set slave flag before open to prevent IPv6 addrconf */
1932 slave_dev->flags |= IFF_SLAVE;
1934 /* open the slave since the application closed it */
1935 res = dev_open(slave_dev, extack);
1937 slave_err(bond_dev, slave_dev, "Opening slave failed\n");
1938 goto err_restore_mac;
1941 slave_dev->priv_flags |= IFF_BONDING;
1942 /* initialize slave stats */
1943 dev_get_stats(new_slave->dev, &new_slave->slave_stats);
1945 if (bond_is_lb(bond)) {
1946 /* bond_alb_init_slave() must be called before all other stages since
1947 * it might fail and we do not want to have to undo everything
1949 res = bond_alb_init_slave(bond, new_slave);
1954 res = vlan_vids_add_by_dev(slave_dev, bond_dev);
1956 slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
1960 prev_slave = bond_last_slave(bond);
1962 new_slave->delay = 0;
1963 new_slave->link_failure_count = 0;
1965 if (bond_update_speed_duplex(new_slave) &&
1966 bond_needs_speed_duplex(bond))
1967 new_slave->link = BOND_LINK_DOWN;
1969 new_slave->last_rx = jiffies -
1970 (msecs_to_jiffies(bond->params.arp_interval) + 1);
1971 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1972 new_slave->target_last_arp_rx[i] = new_slave->last_rx;
1974 if (bond->params.miimon && !bond->params.use_carrier) {
1975 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1977 if ((link_reporting == -1) && !bond->params.arp_interval) {
1978 /* miimon is set but a bonded network driver
1979 * does not support ETHTOOL/MII and
1980 * arp_interval is not set. Note: if
1981 * use_carrier is enabled, we will never go
1982 * here (because netif_carrier is always
1983 * supported); thus, we don't need to change
1984 * the messages for netif_carrier.
1986 slave_warn(bond_dev, slave_dev, "MII and ETHTOOL support not available for slave, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details\n");
1987 } else if (link_reporting == -1) {
1988 /* unable get link status using mii/ethtool */
1989 slave_warn(bond_dev, slave_dev, "can't get link status from slave; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface\n");
1993 /* check for initial state */
1994 new_slave->link = BOND_LINK_NOCHANGE;
1995 if (bond->params.miimon) {
1996 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
1997 if (bond->params.updelay) {
1998 bond_set_slave_link_state(new_slave,
2000 BOND_SLAVE_NOTIFY_NOW);
2001 new_slave->delay = bond->params.updelay;
2003 bond_set_slave_link_state(new_slave,
2005 BOND_SLAVE_NOTIFY_NOW);
2008 bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
2009 BOND_SLAVE_NOTIFY_NOW);
2011 } else if (bond->params.arp_interval) {
2012 bond_set_slave_link_state(new_slave,
2013 (netif_carrier_ok(slave_dev) ?
2014 BOND_LINK_UP : BOND_LINK_DOWN),
2015 BOND_SLAVE_NOTIFY_NOW);
2017 bond_set_slave_link_state(new_slave, BOND_LINK_UP,
2018 BOND_SLAVE_NOTIFY_NOW);
2021 if (new_slave->link != BOND_LINK_DOWN)
2022 new_slave->last_link_up = jiffies;
2023 slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
2024 new_slave->link == BOND_LINK_DOWN ? "DOWN" :
2025 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
2027 if (bond_uses_primary(bond) && bond->params.primary[0]) {
2028 /* if there is a primary slave, remember it */
2029 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
2030 rcu_assign_pointer(bond->primary_slave, new_slave);
2031 bond->force_primary = true;
2035 switch (BOND_MODE(bond)) {
2036 case BOND_MODE_ACTIVEBACKUP:
2037 bond_set_slave_inactive_flags(new_slave,
2038 BOND_SLAVE_NOTIFY_NOW);
2040 case BOND_MODE_8023AD:
2041 /* in 802.3ad mode, the internal mechanism
2042 * will activate the slaves in the selected
2045 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
2046 /* if this is the first slave */
2048 SLAVE_AD_INFO(new_slave)->id = 1;
2049 /* Initialize AD with the number of times that the AD timer is called in 1 second
2050 * can be called only after the mac address of the bond is set
2052 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
2054 SLAVE_AD_INFO(new_slave)->id =
2055 SLAVE_AD_INFO(prev_slave)->id + 1;
2058 bond_3ad_bind_slave(new_slave);
2062 bond_set_active_slave(new_slave);
2063 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
2066 slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
2068 /* always active in trunk mode */
2069 bond_set_active_slave(new_slave);
2071 /* In trunking mode there is little meaning to curr_active_slave
2072 * anyway (it holds no special properties of the bond device),
2073 * so we can change it without calling change_active_interface()
2075 if (!rcu_access_pointer(bond->curr_active_slave) &&
2076 new_slave->link == BOND_LINK_UP)
2077 rcu_assign_pointer(bond->curr_active_slave, new_slave);
2080 } /* switch(bond_mode) */
2082 #ifdef CONFIG_NET_POLL_CONTROLLER
2083 if (bond->dev->npinfo) {
2084 if (slave_enable_netpoll(new_slave)) {
2085 slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
2092 if (!(bond_dev->features & NETIF_F_LRO))
2093 dev_disable_lro(slave_dev);
2095 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
2098 slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
2102 res = bond_master_upper_dev_link(bond, new_slave, extack);
2104 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
2105 goto err_unregister;
2108 bond_lower_state_changed(new_slave);
2110 res = bond_sysfs_slave_add(new_slave);
2112 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
2113 goto err_upper_unlink;
2116 /* If the mode uses primary, then the following is handled by
2117 * bond_change_active_slave().
2119 if (!bond_uses_primary(bond)) {
2120 /* set promiscuity level to new slave */
2121 if (bond_dev->flags & IFF_PROMISC) {
2122 res = dev_set_promiscuity(slave_dev, 1);
2127 /* set allmulti level to new slave */
2128 if (bond_dev->flags & IFF_ALLMULTI) {
2129 res = dev_set_allmulti(slave_dev, 1);
2131 if (bond_dev->flags & IFF_PROMISC)
2132 dev_set_promiscuity(slave_dev, -1);
2137 netif_addr_lock_bh(bond_dev);
2138 dev_mc_sync_multiple(slave_dev, bond_dev);
2139 dev_uc_sync_multiple(slave_dev, bond_dev);
2140 netif_addr_unlock_bh(bond_dev);
2142 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
2143 /* add lacpdu mc addr to mc list */
2144 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
2146 dev_mc_add(slave_dev, lacpdu_multicast);
2151 bond_compute_features(bond);
2152 bond_set_carrier(bond);
2154 if (bond_uses_primary(bond)) {
2156 bond_select_active_slave(bond);
2157 unblock_netpoll_tx();
2160 if (bond_mode_can_use_xmit_hash(bond))
2161 bond_update_slave_arr(bond, NULL);
2164 if (!slave_dev->netdev_ops->ndo_bpf ||
2165 !slave_dev->netdev_ops->ndo_xdp_xmit) {
2166 if (bond->xdp_prog) {
2167 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2168 "Slave does not support XDP");
2172 } else if (bond->xdp_prog) {
2173 struct netdev_bpf xdp = {
2174 .command = XDP_SETUP_PROG,
2176 .prog = bond->xdp_prog,
2180 if (dev_xdp_prog_count(slave_dev) > 0) {
2181 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2182 "Slave has XDP program loaded, please unload before enslaving");
2187 res = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
2189 /* ndo_bpf() sets extack error message */
2190 slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
2194 bpf_prog_inc(bond->xdp_prog);
2197 slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
2198 bond_is_active_slave(new_slave) ? "an active" : "a backup",
2199 new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
2201 /* enslave is successful */
2202 bond_queue_slave_event(new_slave);
2205 /* Undo stages on error */
2207 bond_sysfs_slave_del(new_slave);
2210 bond_upper_dev_unlink(bond, new_slave);
2213 netdev_rx_handler_unregister(slave_dev);
2216 vlan_vids_del_by_dev(slave_dev, bond_dev);
2217 if (rcu_access_pointer(bond->primary_slave) == new_slave)
2218 RCU_INIT_POINTER(bond->primary_slave, NULL);
2219 if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
2221 bond_change_active_slave(bond, NULL);
2222 bond_select_active_slave(bond);
2223 unblock_netpoll_tx();
2225 /* either primary_slave or curr_active_slave might've changed */
2227 slave_disable_netpoll(new_slave);
2230 if (!netif_is_bond_master(slave_dev))
2231 slave_dev->priv_flags &= ~IFF_BONDING;
2232 dev_close(slave_dev);
2235 slave_dev->flags &= ~IFF_SLAVE;
2236 if (!bond->params.fail_over_mac ||
2237 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2238 /* XXX TODO - fom follow mode needs to change master's
2239 * MAC if this slave's MAC is in use by the bond, or at
2240 * least print a warning.
2242 bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
2243 new_slave->dev->addr_len);
2244 ss.ss_family = slave_dev->type;
2245 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
2249 dev_set_mtu(slave_dev, new_slave->original_mtu);
2252 kobject_put(&new_slave->kobj);
2255 /* Enslave of first slave has failed and we need to fix master's mac */
2256 if (!bond_has_slaves(bond)) {
2257 if (ether_addr_equal_64bits(bond_dev->dev_addr,
2258 slave_dev->dev_addr))
2259 eth_hw_addr_random(bond_dev);
2260 if (bond_dev->type != ARPHRD_ETHER) {
2261 dev_close(bond_dev);
2262 ether_setup(bond_dev);
2263 bond_dev->flags |= IFF_MASTER;
2264 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2271 /* Try to release the slave device <slave> from the bond device <master>
2272 * It is legal to access curr_active_slave without a lock because all the function
2273 * is RTNL-locked. If "all" is true it means that the function is being called
2274 * while destroying a bond interface and all slaves are being released.
2276 * The rules for slave state should be:
2277 * for Active/Backup:
2278 * Active stays on all backups go down
2279 * for Bonded connections:
2280 * The first up interface should be left on and all others downed.
2282 static int __bond_release_one(struct net_device *bond_dev,
2283 struct net_device *slave_dev,
2284 bool all, bool unregister)
2286 struct bonding *bond = netdev_priv(bond_dev);
2287 struct slave *slave, *oldcurrent;
2288 struct sockaddr_storage ss;
2289 int old_flags = bond_dev->flags;
2290 netdev_features_t old_features = bond_dev->features;
2292 /* slave is not a slave or master is not master of this slave */
2293 if (!(slave_dev->flags & IFF_SLAVE) ||
2294 !netdev_has_upper_dev(slave_dev, bond_dev)) {
2295 slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
2301 slave = bond_get_slave_by_dev(bond, slave_dev);
2303 /* not a slave of this bond */
2304 slave_info(bond_dev, slave_dev, "interface not enslaved\n");
2305 unblock_netpoll_tx();
2309 bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
2311 bond_sysfs_slave_del(slave);
2313 /* recompute stats just before removing the slave */
2314 bond_get_stats(bond->dev, &bond->bond_stats);
2316 if (bond->xdp_prog) {
2317 struct netdev_bpf xdp = {
2318 .command = XDP_SETUP_PROG,
2323 if (slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp))
2324 slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n");
2327 /* unregister rx_handler early so bond_handle_frame wouldn't be called
2328 * for this slave anymore.
2330 netdev_rx_handler_unregister(slave_dev);
2332 if (BOND_MODE(bond) == BOND_MODE_8023AD)
2333 bond_3ad_unbind_slave(slave);
2335 bond_upper_dev_unlink(bond, slave);
2337 if (bond_mode_can_use_xmit_hash(bond))
2338 bond_update_slave_arr(bond, slave);
2340 slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
2341 bond_is_active_slave(slave) ? "active" : "backup");
2343 oldcurrent = rcu_access_pointer(bond->curr_active_slave);
2345 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
2347 if (!all && (!bond->params.fail_over_mac ||
2348 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
2349 if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
2350 bond_has_slaves(bond))
2351 slave_warn(bond_dev, slave_dev, "the permanent HWaddr of slave - %pM - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts\n",
2352 slave->perm_hwaddr);
2355 if (rtnl_dereference(bond->primary_slave) == slave)
2356 RCU_INIT_POINTER(bond->primary_slave, NULL);
2358 if (oldcurrent == slave)
2359 bond_change_active_slave(bond, NULL);
2361 if (bond_is_lb(bond)) {
2362 /* Must be called only after the slave has been
2363 * detached from the list and the curr_active_slave
2364 * has been cleared (if our_slave == old_current),
2365 * but before a new active slave is selected.
2367 bond_alb_deinit_slave(bond, slave);
2371 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
2372 } else if (oldcurrent == slave) {
2373 /* Note that we hold RTNL over this sequence, so there
2374 * is no concern that another slave add/remove event
2377 bond_select_active_slave(bond);
2380 if (!bond_has_slaves(bond)) {
2381 bond_set_carrier(bond);
2382 eth_hw_addr_random(bond_dev);
2385 unblock_netpoll_tx();
2389 if (!bond_has_slaves(bond)) {
2390 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
2391 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
2394 bond_compute_features(bond);
2395 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2396 (old_features & NETIF_F_VLAN_CHALLENGED))
2397 slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
2399 vlan_vids_del_by_dev(slave_dev, bond_dev);
2401 /* If the mode uses primary, then this case was handled above by
2402 * bond_change_active_slave(..., NULL)
2404 if (!bond_uses_primary(bond)) {
2405 /* unset promiscuity level from slave
2406 * NOTE: The NETDEV_CHANGEADDR call above may change the value
2407 * of the IFF_PROMISC flag in the bond_dev, but we need the
2408 * value of that flag before that change, as that was the value
2409 * when this slave was attached, so we cache at the start of the
2410 * function and use it here. Same goes for ALLMULTI below
2412 if (old_flags & IFF_PROMISC)
2413 dev_set_promiscuity(slave_dev, -1);
2415 /* unset allmulti level from slave */
2416 if (old_flags & IFF_ALLMULTI)
2417 dev_set_allmulti(slave_dev, -1);
2419 bond_hw_addr_flush(bond_dev, slave_dev);
2422 slave_disable_netpoll(slave);
2424 /* close slave before restoring its mac address */
2425 dev_close(slave_dev);
2427 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
2428 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2429 /* restore original ("permanent") mac address */
2430 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2431 slave->dev->addr_len);
2432 ss.ss_family = slave_dev->type;
2433 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
2437 __dev_set_mtu(slave_dev, slave->original_mtu);
2439 dev_set_mtu(slave_dev, slave->original_mtu);
2441 if (!netif_is_bond_master(slave_dev))
2442 slave_dev->priv_flags &= ~IFF_BONDING;
2444 kobject_put(&slave->kobj);
2449 /* A wrapper used because of ndo_del_link */
2450 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2452 return __bond_release_one(bond_dev, slave_dev, false, false);
2455 /* First release a slave and then destroy the bond if no more slaves are left.
2456 * Must be under rtnl_lock when this function is called.
2458 static int bond_release_and_destroy(struct net_device *bond_dev,
2459 struct net_device *slave_dev)
2461 struct bonding *bond = netdev_priv(bond_dev);
2464 ret = __bond_release_one(bond_dev, slave_dev, false, true);
2465 if (ret == 0 && !bond_has_slaves(bond) &&
2466 bond_dev->reg_state != NETREG_UNREGISTERING) {
2467 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2468 netdev_info(bond_dev, "Destroying bond\n");
2469 bond_remove_proc_entry(bond);
2470 unregister_netdevice(bond_dev);
2475 static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2477 struct bonding *bond = netdev_priv(bond_dev);
2479 bond_fill_ifbond(bond, info);
2482 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2484 struct bonding *bond = netdev_priv(bond_dev);
2485 struct list_head *iter;
2486 int i = 0, res = -ENODEV;
2487 struct slave *slave;
2489 bond_for_each_slave(bond, slave, iter) {
2490 if (i++ == (int)info->slave_id) {
2492 bond_fill_ifslave(slave, info);
2500 /*-------------------------------- Monitoring -------------------------------*/
2502 /* called with rcu_read_lock() */
2503 static int bond_miimon_inspect(struct bonding *bond)
2505 int link_state, commit = 0;
2506 struct list_head *iter;
2507 struct slave *slave;
2508 bool ignore_updelay;
2510 ignore_updelay = !rcu_dereference(bond->curr_active_slave);
2512 bond_for_each_slave_rcu(bond, slave, iter) {
2513 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2515 link_state = bond_check_dev_link(bond, slave->dev, 0);
2517 switch (slave->link) {
2522 bond_propose_link_state(slave, BOND_LINK_FAIL);
2524 slave->delay = bond->params.downdelay;
2526 slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
2528 BOND_MODE_ACTIVEBACKUP) ?
2529 (bond_is_active_slave(slave) ?
2530 "active " : "backup ") : "",
2531 bond->params.downdelay * bond->params.miimon);
2534 case BOND_LINK_FAIL:
2536 /* recovered before downdelay expired */
2537 bond_propose_link_state(slave, BOND_LINK_UP);
2538 slave->last_link_up = jiffies;
2539 slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2540 (bond->params.downdelay - slave->delay) *
2541 bond->params.miimon);
2546 if (slave->delay <= 0) {
2547 bond_propose_link_state(slave, BOND_LINK_DOWN);
2555 case BOND_LINK_DOWN:
2559 bond_propose_link_state(slave, BOND_LINK_BACK);
2561 slave->delay = bond->params.updelay;
2564 slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
2565 ignore_updelay ? 0 :
2566 bond->params.updelay *
2567 bond->params.miimon);
2570 case BOND_LINK_BACK:
2572 bond_propose_link_state(slave, BOND_LINK_DOWN);
2573 slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2574 (bond->params.updelay - slave->delay) *
2575 bond->params.miimon);
2583 if (slave->delay <= 0) {
2584 bond_propose_link_state(slave, BOND_LINK_UP);
2586 ignore_updelay = false;
2598 static void bond_miimon_link_change(struct bonding *bond,
2599 struct slave *slave,
2602 switch (BOND_MODE(bond)) {
2603 case BOND_MODE_8023AD:
2604 bond_3ad_handle_link_change(slave, link);
2608 bond_alb_handle_link_change(bond, slave, link);
2611 bond_update_slave_arr(bond, NULL);
2616 static void bond_miimon_commit(struct bonding *bond)
2618 struct list_head *iter;
2619 struct slave *slave, *primary;
2621 bond_for_each_slave(bond, slave, iter) {
2622 switch (slave->link_new_state) {
2623 case BOND_LINK_NOCHANGE:
2624 /* For 802.3ad mode, check current slave speed and
2625 * duplex again in case its port was disabled after
2626 * invalid speed/duplex reporting but recovered before
2627 * link monitoring could make a decision on the actual
2630 if (BOND_MODE(bond) == BOND_MODE_8023AD &&
2631 slave->link == BOND_LINK_UP)
2632 bond_3ad_adapter_speed_duplex_changed(slave);
2636 if (bond_update_speed_duplex(slave) &&
2637 bond_needs_speed_duplex(bond)) {
2638 slave->link = BOND_LINK_DOWN;
2639 if (net_ratelimit())
2640 slave_warn(bond->dev, slave->dev,
2641 "failed to get link speed/duplex\n");
2644 bond_set_slave_link_state(slave, BOND_LINK_UP,
2645 BOND_SLAVE_NOTIFY_NOW);
2646 slave->last_link_up = jiffies;
2648 primary = rtnl_dereference(bond->primary_slave);
2649 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
2650 /* prevent it from being the active one */
2651 bond_set_backup_slave(slave);
2652 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2653 /* make it immediately active */
2654 bond_set_active_slave(slave);
2657 slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2658 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2659 slave->duplex ? "full" : "half");
2661 bond_miimon_link_change(bond, slave, BOND_LINK_UP);
2663 if (!bond->curr_active_slave || slave == primary)
2668 case BOND_LINK_DOWN:
2669 if (slave->link_failure_count < UINT_MAX)
2670 slave->link_failure_count++;
2672 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
2673 BOND_SLAVE_NOTIFY_NOW);
2675 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
2676 BOND_MODE(bond) == BOND_MODE_8023AD)
2677 bond_set_slave_inactive_flags(slave,
2678 BOND_SLAVE_NOTIFY_NOW);
2680 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
2682 bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
2684 if (slave == rcu_access_pointer(bond->curr_active_slave))
2690 slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
2691 slave->link_new_state);
2692 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2699 bond_select_active_slave(bond);
2700 unblock_netpoll_tx();
2703 bond_set_carrier(bond);
2708 * Really a wrapper that splits the mii monitor into two phases: an
2709 * inspection, then (if inspection indicates something needs to be done)
2710 * an acquisition of appropriate locks followed by a commit phase to
2711 * implement whatever link state changes are indicated.
2713 static void bond_mii_monitor(struct work_struct *work)
2715 struct bonding *bond = container_of(work, struct bonding,
2717 bool should_notify_peers = false;
2719 unsigned long delay;
2720 struct slave *slave;
2721 struct list_head *iter;
2723 delay = msecs_to_jiffies(bond->params.miimon);
2725 if (!bond_has_slaves(bond))
2729 should_notify_peers = bond_should_notify_peers(bond);
2730 commit = !!bond_miimon_inspect(bond);
2731 if (bond->send_peer_notif) {
2733 if (rtnl_trylock()) {
2734 bond->send_peer_notif--;
2742 /* Race avoidance with bond_close cancel of workqueue */
2743 if (!rtnl_trylock()) {
2745 should_notify_peers = false;
2749 bond_for_each_slave(bond, slave, iter) {
2750 bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER);
2752 bond_miimon_commit(bond);
2754 rtnl_unlock(); /* might sleep, hold no other locks */
2758 if (bond->params.miimon)
2759 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2761 if (should_notify_peers) {
2762 if (!rtnl_trylock())
2764 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
2769 static int bond_upper_dev_walk(struct net_device *upper,
2770 struct netdev_nested_priv *priv)
2772 __be32 ip = *(__be32 *)priv->data;
2774 return ip == bond_confirm_addr(upper, 0, ip);
2777 static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2779 struct netdev_nested_priv priv = {
2780 .data = (void *)&ip,
2784 if (ip == bond_confirm_addr(bond->dev, 0, ip))
2788 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
2795 /* We go to the (large) trouble of VLAN tagging ARP frames because
2796 * switches in VLAN mode (especially if ports are configured as
2797 * "native" to a VLAN) might not pass non-tagged frames.
2799 static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
2800 __be32 src_ip, struct bond_vlan_tag *tags)
2802 struct sk_buff *skb;
2803 struct bond_vlan_tag *outer_tag = tags;
2804 struct net_device *slave_dev = slave->dev;
2805 struct net_device *bond_dev = slave->bond->dev;
2807 slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
2808 arp_op, &dest_ip, &src_ip);
2810 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2811 NULL, slave_dev->dev_addr, NULL);
2814 net_err_ratelimited("ARP packet allocation failed\n");
2818 if (!tags || tags->vlan_proto == VLAN_N_VID)
2823 /* Go through all the tags backwards and add them to the packet */
2824 while (tags->vlan_proto != VLAN_N_VID) {
2825 if (!tags->vlan_id) {
2830 slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
2831 ntohs(outer_tag->vlan_proto), tags->vlan_id);
2832 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
2835 net_err_ratelimited("failed to insert inner VLAN tag\n");
2841 /* Set the outer tag */
2842 if (outer_tag->vlan_id) {
2843 slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
2844 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
2845 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
2846 outer_tag->vlan_id);
2853 /* Validate the device path between the @start_dev and the @end_dev.
2854 * The path is valid if the @end_dev is reachable through device
2856 * When the path is validated, collect any vlan information in the
2859 struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
2860 struct net_device *end_dev,
2863 struct bond_vlan_tag *tags;
2864 struct net_device *upper;
2865 struct list_head *iter;
2867 if (start_dev == end_dev) {
2868 tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
2870 return ERR_PTR(-ENOMEM);
2871 tags[level].vlan_proto = VLAN_N_VID;
2875 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
2876 tags = bond_verify_device_path(upper, end_dev, level + 1);
2877 if (IS_ERR_OR_NULL(tags)) {
2882 if (is_vlan_dev(upper)) {
2883 tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
2884 tags[level].vlan_id = vlan_dev_vlan_id(upper);
2893 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2896 struct bond_vlan_tag *tags;
2897 __be32 *targets = bond->params.arp_targets, addr;
2900 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
2901 slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
2902 __func__, &targets[i]);
2905 /* Find out through which dev should the packet go */
2906 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2909 /* there's no route to target - try to send arp
2910 * probe to generate any traffic (arp_validate=0)
2912 if (bond->params.arp_validate)
2913 pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
2916 bond_arp_send(slave, ARPOP_REQUEST, targets[i],
2921 /* bond device itself */
2922 if (rt->dst.dev == bond->dev)
2926 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
2929 if (!IS_ERR_OR_NULL(tags))
2932 /* Not our device - skip */
2933 slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
2934 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
2940 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
2942 bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
2947 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2951 if (!sip || !bond_has_this_ip(bond, tip)) {
2952 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
2953 __func__, &sip, &tip);
2957 i = bond_get_targets_ip(bond->params.arp_targets, sip);
2959 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
2963 slave->last_rx = jiffies;
2964 slave->target_last_arp_rx[i] = jiffies;
2967 int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
2968 struct slave *slave)
2970 struct arphdr *arp = (struct arphdr *)skb->data;
2971 struct slave *curr_active_slave, *curr_arp_slave;
2972 unsigned char *arp_ptr;
2974 int is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
2977 if (!slave_do_arp_validate(bond, slave)) {
2978 if ((slave_do_arp_validate_only(bond) && is_arp) ||
2979 !slave_do_arp_validate_only(bond))
2980 slave->last_rx = jiffies;
2981 return RX_HANDLER_ANOTHER;
2982 } else if (!is_arp) {
2983 return RX_HANDLER_ANOTHER;
2986 alen = arp_hdr_len(bond->dev);
2988 slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
2989 __func__, skb->dev->name);
2991 if (alen > skb_headlen(skb)) {
2992 arp = kmalloc(alen, GFP_ATOMIC);
2995 if (skb_copy_bits(skb, 0, arp, alen) < 0)
2999 if (arp->ar_hln != bond->dev->addr_len ||
3000 skb->pkt_type == PACKET_OTHERHOST ||
3001 skb->pkt_type == PACKET_LOOPBACK ||
3002 arp->ar_hrd != htons(ARPHRD_ETHER) ||
3003 arp->ar_pro != htons(ETH_P_IP) ||
3007 arp_ptr = (unsigned char *)(arp + 1);
3008 arp_ptr += bond->dev->addr_len;
3009 memcpy(&sip, arp_ptr, 4);
3010 arp_ptr += 4 + bond->dev->addr_len;
3011 memcpy(&tip, arp_ptr, 4);
3013 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
3014 __func__, slave->dev->name, bond_slave_state(slave),
3015 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
3018 curr_active_slave = rcu_dereference(bond->curr_active_slave);
3019 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
3021 /* We 'trust' the received ARP enough to validate it if:
3023 * (a) the slave receiving the ARP is active (which includes the
3024 * current ARP slave, if any), or
3026 * (b) the receiving slave isn't active, but there is a currently
3027 * active slave and it received valid arp reply(s) after it became
3028 * the currently active slave, or
3030 * (c) there is an ARP slave that sent an ARP during the prior ARP
3031 * interval, and we receive an ARP reply on any slave. We accept
3032 * these because switch FDB update delays may deliver the ARP
3033 * reply to a slave other than the sender of the ARP request.
3035 * Note: for (b), backup slaves are receiving the broadcast ARP
3036 * request, not a reply. This request passes from the sending
3037 * slave through the L2 switch(es) to the receiving slave. Since
3038 * this is checking the request, sip/tip are swapped for
3041 * This is done to avoid endless looping when we can't reach the
3042 * arp_ip_target and fool ourselves with our own arp requests.
3044 if (bond_is_active_slave(slave))
3045 bond_validate_arp(bond, slave, sip, tip);
3046 else if (curr_active_slave &&
3047 time_after(slave_last_rx(bond, curr_active_slave),
3048 curr_active_slave->last_link_up))
3049 bond_validate_arp(bond, slave, tip, sip);
3050 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
3051 bond_time_in_interval(bond,
3052 dev_trans_start(curr_arp_slave->dev), 1))
3053 bond_validate_arp(bond, slave, sip, tip);
3056 if (arp != (struct arphdr *)skb->data)
3058 return RX_HANDLER_ANOTHER;
3061 /* function to verify if we're in the arp_interval timeslice, returns true if
3062 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
3063 * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
3065 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
3068 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3070 return time_in_range(jiffies,
3071 last_act - delta_in_ticks,
3072 last_act + mod * delta_in_ticks + delta_in_ticks/2);
3075 /* This function is called regularly to monitor each slave's link
3076 * ensuring that traffic is being sent and received when arp monitoring
3077 * is used in load-balancing mode. if the adapter has been dormant, then an
3078 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
3079 * arp monitoring in active backup mode.
3081 static void bond_loadbalance_arp_mon(struct bonding *bond)
3083 struct slave *slave, *oldcurrent;
3084 struct list_head *iter;
3085 int do_failover = 0, slave_state_changed = 0;
3087 if (!bond_has_slaves(bond))
3092 oldcurrent = rcu_dereference(bond->curr_active_slave);
3093 /* see if any of the previous devices are up now (i.e. they have
3094 * xmt and rcv traffic). the curr_active_slave does not come into
3095 * the picture unless it is null. also, slave->last_link_up is not
3096 * needed here because we send an arp on each slave and give a slave
3097 * as long as it needs to get the tx/rx within the delta.
3098 * TODO: what about up/down delay in arp mode? it wasn't here before
3101 bond_for_each_slave_rcu(bond, slave, iter) {
3102 unsigned long trans_start = dev_trans_start(slave->dev);
3104 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3106 if (slave->link != BOND_LINK_UP) {
3107 if (bond_time_in_interval(bond, trans_start, 1) &&
3108 bond_time_in_interval(bond, slave->last_rx, 1)) {
3110 bond_propose_link_state(slave, BOND_LINK_UP);
3111 slave_state_changed = 1;
3113 /* primary_slave has no meaning in round-robin
3114 * mode. the window of a slave being up and
3115 * curr_active_slave being null after enslaving
3119 slave_info(bond->dev, slave->dev, "link status definitely up\n");
3122 slave_info(bond->dev, slave->dev, "interface is now up\n");
3126 /* slave->link == BOND_LINK_UP */
3128 /* not all switches will respond to an arp request
3129 * when the source ip is 0, so don't take the link down
3130 * if we don't know our ip yet
3132 if (!bond_time_in_interval(bond, trans_start, 2) ||
3133 !bond_time_in_interval(bond, slave->last_rx, 2)) {
3135 bond_propose_link_state(slave, BOND_LINK_DOWN);
3136 slave_state_changed = 1;
3138 if (slave->link_failure_count < UINT_MAX)
3139 slave->link_failure_count++;
3141 slave_info(bond->dev, slave->dev, "interface is now down\n");
3143 if (slave == oldcurrent)
3148 /* note: if switch is in round-robin mode, all links
3149 * must tx arp to ensure all links rx an arp - otherwise
3150 * links may oscillate or not come up at all; if switch is
3151 * in something like xor mode, there is nothing we can
3152 * do - all replies will be rx'ed on same link causing slaves
3153 * to be unstable during low/no traffic periods
3155 if (bond_slave_is_up(slave))
3156 bond_arp_send_all(bond, slave);
3161 if (do_failover || slave_state_changed) {
3162 if (!rtnl_trylock())
3165 bond_for_each_slave(bond, slave, iter) {
3166 if (slave->link_new_state != BOND_LINK_NOCHANGE)
3167 slave->link = slave->link_new_state;
3170 if (slave_state_changed) {
3171 bond_slave_state_change(bond);
3172 if (BOND_MODE(bond) == BOND_MODE_XOR)
3173 bond_update_slave_arr(bond, NULL);
3177 bond_select_active_slave(bond);
3178 unblock_netpoll_tx();
3184 if (bond->params.arp_interval)
3185 queue_delayed_work(bond->wq, &bond->arp_work,
3186 msecs_to_jiffies(bond->params.arp_interval));
3189 /* Called to inspect slaves for active-backup mode ARP monitor link state
3190 * changes. Sets proposed link state in slaves to specify what action
3191 * should take place for the slave. Returns 0 if no changes are found, >0
3192 * if changes to link states must be committed.
3194 * Called with rcu_read_lock held.
3196 static int bond_ab_arp_inspect(struct bonding *bond)
3198 unsigned long trans_start, last_rx;
3199 struct list_head *iter;
3200 struct slave *slave;
3203 bond_for_each_slave_rcu(bond, slave, iter) {
3204 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3205 last_rx = slave_last_rx(bond, slave);
3207 if (slave->link != BOND_LINK_UP) {
3208 if (bond_time_in_interval(bond, last_rx, 1)) {
3209 bond_propose_link_state(slave, BOND_LINK_UP);
3211 } else if (slave->link == BOND_LINK_BACK) {
3212 bond_propose_link_state(slave, BOND_LINK_FAIL);
3218 /* Give slaves 2*delta after being enslaved or made
3219 * active. This avoids bouncing, as the last receive
3220 * times need a full ARP monitor cycle to be updated.
3222 if (bond_time_in_interval(bond, slave->last_link_up, 2))
3225 /* Backup slave is down if:
3226 * - No current_arp_slave AND
3227 * - more than 3*delta since last receive AND
3228 * - the bond has an IP address
3230 * Note: a non-null current_arp_slave indicates
3231 * the curr_active_slave went down and we are
3232 * searching for a new one; under this condition
3233 * we only take the curr_active_slave down - this
3234 * gives each slave a chance to tx/rx traffic
3235 * before being taken out
3237 if (!bond_is_active_slave(slave) &&
3238 !rcu_access_pointer(bond->current_arp_slave) &&
3239 !bond_time_in_interval(bond, last_rx, 3)) {
3240 bond_propose_link_state(slave, BOND_LINK_DOWN);
3244 /* Active slave is down if:
3245 * - more than 2*delta since transmitting OR
3246 * - (more than 2*delta since receive AND
3247 * the bond has an IP address)
3249 trans_start = dev_trans_start(slave->dev);
3250 if (bond_is_active_slave(slave) &&
3251 (!bond_time_in_interval(bond, trans_start, 2) ||
3252 !bond_time_in_interval(bond, last_rx, 2))) {
3253 bond_propose_link_state(slave, BOND_LINK_DOWN);
3261 /* Called to commit link state changes noted by inspection step of
3262 * active-backup mode ARP monitor.
3264 * Called with RTNL hold.
3266 static void bond_ab_arp_commit(struct bonding *bond)
3268 unsigned long trans_start;
3269 struct list_head *iter;
3270 struct slave *slave;
3272 bond_for_each_slave(bond, slave, iter) {
3273 switch (slave->link_new_state) {
3274 case BOND_LINK_NOCHANGE:
3278 trans_start = dev_trans_start(slave->dev);
3279 if (rtnl_dereference(bond->curr_active_slave) != slave ||
3280 (!rtnl_dereference(bond->curr_active_slave) &&
3281 bond_time_in_interval(bond, trans_start, 1))) {
3282 struct slave *current_arp_slave;
3284 current_arp_slave = rtnl_dereference(bond->current_arp_slave);
3285 bond_set_slave_link_state(slave, BOND_LINK_UP,
3286 BOND_SLAVE_NOTIFY_NOW);
3287 if (current_arp_slave) {
3288 bond_set_slave_inactive_flags(
3290 BOND_SLAVE_NOTIFY_NOW);
3291 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3294 slave_info(bond->dev, slave->dev, "link status definitely up\n");
3296 if (!rtnl_dereference(bond->curr_active_slave) ||
3297 slave == rtnl_dereference(bond->primary_slave))
3304 case BOND_LINK_DOWN:
3305 if (slave->link_failure_count < UINT_MAX)
3306 slave->link_failure_count++;
3308 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3309 BOND_SLAVE_NOTIFY_NOW);
3310 bond_set_slave_inactive_flags(slave,
3311 BOND_SLAVE_NOTIFY_NOW);
3313 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
3315 if (slave == rtnl_dereference(bond->curr_active_slave)) {
3316 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3322 case BOND_LINK_FAIL:
3323 bond_set_slave_link_state(slave, BOND_LINK_FAIL,
3324 BOND_SLAVE_NOTIFY_NOW);
3325 bond_set_slave_inactive_flags(slave,
3326 BOND_SLAVE_NOTIFY_NOW);
3328 /* A slave has just been enslaved and has become
3329 * the current active slave.
3331 if (rtnl_dereference(bond->curr_active_slave))
3332 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3336 slave_err(bond->dev, slave->dev,
3337 "impossible: link_new_state %d on slave\n",
3338 slave->link_new_state);
3344 bond_select_active_slave(bond);
3345 unblock_netpoll_tx();
3348 bond_set_carrier(bond);
3351 /* Send ARP probes for active-backup mode ARP monitor.
3353 * Called with rcu_read_lock held.
3355 static bool bond_ab_arp_probe(struct bonding *bond)
3357 struct slave *slave, *before = NULL, *new_slave = NULL,
3358 *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3359 *curr_active_slave = rcu_dereference(bond->curr_active_slave);
3360 struct list_head *iter;
3362 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
3364 if (curr_arp_slave && curr_active_slave)
3365 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3366 curr_arp_slave->dev->name,
3367 curr_active_slave->dev->name);
3369 if (curr_active_slave) {
3370 bond_arp_send_all(bond, curr_active_slave);
3371 return should_notify_rtnl;
3374 /* if we don't have a curr_active_slave, search for the next available
3375 * backup slave from the current_arp_slave and make it the candidate
3376 * for becoming the curr_active_slave
3379 if (!curr_arp_slave) {
3380 curr_arp_slave = bond_first_slave_rcu(bond);
3381 if (!curr_arp_slave)
3382 return should_notify_rtnl;
3385 bond_for_each_slave_rcu(bond, slave, iter) {
3386 if (!found && !before && bond_slave_is_up(slave))
3389 if (found && !new_slave && bond_slave_is_up(slave))
3391 /* if the link state is up at this point, we
3392 * mark it down - this can happen if we have
3393 * simultaneous link failures and
3394 * reselect_active_interface doesn't make this
3395 * one the current slave so it is still marked
3396 * up when it is actually down
3398 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
3399 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3400 BOND_SLAVE_NOTIFY_LATER);
3401 if (slave->link_failure_count < UINT_MAX)
3402 slave->link_failure_count++;
3404 bond_set_slave_inactive_flags(slave,
3405 BOND_SLAVE_NOTIFY_LATER);
3407 slave_info(bond->dev, slave->dev, "backup interface is now down\n");
3409 if (slave == curr_arp_slave)
3413 if (!new_slave && before)
3419 bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
3420 BOND_SLAVE_NOTIFY_LATER);
3421 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
3422 bond_arp_send_all(bond, new_slave);
3423 new_slave->last_link_up = jiffies;
3424 rcu_assign_pointer(bond->current_arp_slave, new_slave);
3427 bond_for_each_slave_rcu(bond, slave, iter) {
3428 if (slave->should_notify || slave->should_notify_link) {
3429 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3433 return should_notify_rtnl;
3436 static void bond_activebackup_arp_mon(struct bonding *bond)
3438 bool should_notify_peers = false;
3439 bool should_notify_rtnl = false;
3442 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3444 if (!bond_has_slaves(bond))
3449 should_notify_peers = bond_should_notify_peers(bond);
3451 if (bond_ab_arp_inspect(bond)) {
3454 /* Race avoidance with bond_close flush of workqueue */
3455 if (!rtnl_trylock()) {
3457 should_notify_peers = false;
3461 bond_ab_arp_commit(bond);
3467 should_notify_rtnl = bond_ab_arp_probe(bond);
3471 if (bond->params.arp_interval)
3472 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3474 if (should_notify_peers || should_notify_rtnl) {
3475 if (!rtnl_trylock())
3478 if (should_notify_peers)
3479 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
3481 if (should_notify_rtnl) {
3482 bond_slave_state_notify(bond);
3483 bond_slave_link_notify(bond);
3490 static void bond_arp_monitor(struct work_struct *work)
3492 struct bonding *bond = container_of(work, struct bonding,
3495 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3496 bond_activebackup_arp_mon(bond);
3498 bond_loadbalance_arp_mon(bond);
3501 /*-------------------------- netdev event handling --------------------------*/
3503 /* Change device name */
3504 static int bond_event_changename(struct bonding *bond)
3506 bond_remove_proc_entry(bond);
3507 bond_create_proc_entry(bond);
3509 bond_debug_reregister(bond);
3514 static int bond_master_netdev_event(unsigned long event,
3515 struct net_device *bond_dev)
3517 struct bonding *event_bond = netdev_priv(bond_dev);
3519 netdev_dbg(bond_dev, "%s called\n", __func__);
3522 case NETDEV_CHANGENAME:
3523 return bond_event_changename(event_bond);
3524 case NETDEV_UNREGISTER:
3525 bond_remove_proc_entry(event_bond);
3526 #ifdef CONFIG_XFRM_OFFLOAD
3527 xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
3528 #endif /* CONFIG_XFRM_OFFLOAD */
3530 case NETDEV_REGISTER:
3531 bond_create_proc_entry(event_bond);
3540 static int bond_slave_netdev_event(unsigned long event,
3541 struct net_device *slave_dev)
3543 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
3544 struct bonding *bond;
3545 struct net_device *bond_dev;
3547 /* A netdev event can be generated while enslaving a device
3548 * before netdev_rx_handler_register is called in which case
3549 * slave will be NULL
3552 netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
3556 bond_dev = slave->bond->dev;
3558 primary = rtnl_dereference(bond->primary_slave);
3560 slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3563 case NETDEV_UNREGISTER:
3564 if (bond_dev->type != ARPHRD_ETHER)
3565 bond_release_and_destroy(bond_dev, slave_dev);
3567 __bond_release_one(bond_dev, slave_dev, false, true);
3571 /* For 802.3ad mode only:
3572 * Getting invalid Speed/Duplex values here will put slave
3573 * in weird state. Mark it as link-fail if the link was
3574 * previously up or link-down if it hasn't yet come up, and
3575 * let link-monitoring (miimon) set it right when correct
3576 * speeds/duplex are available.
3578 if (bond_update_speed_duplex(slave) &&
3579 BOND_MODE(bond) == BOND_MODE_8023AD) {
3580 if (slave->last_link_up)
3581 slave->link = BOND_LINK_FAIL;
3583 slave->link = BOND_LINK_DOWN;
3586 if (BOND_MODE(bond) == BOND_MODE_8023AD)
3587 bond_3ad_adapter_speed_duplex_changed(slave);
3590 /* Refresh slave-array if applicable!
3591 * If the setup does not use miimon or arpmon (mode-specific!),
3592 * then these events will not cause the slave-array to be
3593 * refreshed. This will cause xmit to use a slave that is not
3594 * usable. Avoid such situation by refeshing the array at these
3595 * events. If these (miimon/arpmon) parameters are configured
3596 * then array gets refreshed twice and that should be fine!
3598 if (bond_mode_can_use_xmit_hash(bond))
3599 bond_update_slave_arr(bond, NULL);
3601 case NETDEV_CHANGEMTU:
3602 /* TODO: Should slaves be allowed to
3603 * independently alter their MTU? For
3604 * an active-backup bond, slaves need
3605 * not be the same type of device, so
3606 * MTUs may vary. For other modes,
3607 * slaves arguably should have the
3608 * same MTUs. To do this, we'd need to
3609 * take over the slave's change_mtu
3610 * function for the duration of their
3614 case NETDEV_CHANGENAME:
3615 /* we don't care if we don't have primary set */
3616 if (!bond_uses_primary(bond) ||
3617 !bond->params.primary[0])
3620 if (slave == primary) {
3621 /* slave's name changed - he's no longer primary */
3622 RCU_INIT_POINTER(bond->primary_slave, NULL);
3623 } else if (!strcmp(slave_dev->name, bond->params.primary)) {
3624 /* we have a new primary slave */
3625 rcu_assign_pointer(bond->primary_slave, slave);
3626 } else { /* we didn't change primary - exit */
3630 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
3631 primary ? slave_dev->name : "none");
3634 bond_select_active_slave(bond);
3635 unblock_netpoll_tx();
3637 case NETDEV_FEAT_CHANGE:
3638 bond_compute_features(bond);
3640 case NETDEV_RESEND_IGMP:
3641 /* Propagate to master device */
3642 call_netdevice_notifiers(event, slave->bond->dev);
3651 /* bond_netdev_event: handle netdev notifier chain events.
3653 * This function receives events for the netdev chain. The caller (an
3654 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3655 * locks for us to safely manipulate the slave devices (RTNL lock,
3658 static int bond_netdev_event(struct notifier_block *this,
3659 unsigned long event, void *ptr)
3661 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
3663 netdev_dbg(event_dev, "%s received %s\n",
3664 __func__, netdev_cmd_to_name(event));
3666 if (!(event_dev->priv_flags & IFF_BONDING))
3669 if (event_dev->flags & IFF_MASTER) {
3672 ret = bond_master_netdev_event(event, event_dev);
3673 if (ret != NOTIFY_DONE)
3677 if (event_dev->flags & IFF_SLAVE)
3678 return bond_slave_netdev_event(event, event_dev);
3683 static struct notifier_block bond_netdev_notifier = {
3684 .notifier_call = bond_netdev_event,
3687 /*---------------------------- Hashing Policies -----------------------------*/
3689 /* Helper to access data in a packet, with or without a backing skb.
3690 * If skb is given the data is linearized if necessary via pskb_may_pull.
3692 static inline const void *bond_pull_data(struct sk_buff *skb,
3693 const void *data, int hlen, int n)
3695 if (likely(n <= hlen))
3697 else if (skb && likely(pskb_may_pull(skb, n)))
3703 /* L2 hash helper */
3704 static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
3708 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
3712 ep = (struct ethhdr *)(data + mhoff);
3713 return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
3716 static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
3717 int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
3719 const struct ipv6hdr *iph6;
3720 const struct iphdr *iph;
3722 if (l2_proto == htons(ETH_P_IP)) {
3723 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
3727 iph = (const struct iphdr *)(data + *nhoff);
3728 iph_to_flow_copy_v4addrs(fk, iph);
3729 *nhoff += iph->ihl << 2;
3730 if (!ip_is_fragment(iph))
3731 *ip_proto = iph->protocol;
3732 } else if (l2_proto == htons(ETH_P_IPV6)) {
3733 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
3737 iph6 = (const struct ipv6hdr *)(data + *nhoff);
3738 iph_to_flow_copy_v6addrs(fk, iph6);
3739 *nhoff += sizeof(*iph6);
3740 *ip_proto = iph6->nexthdr;
3745 if (l34 && *ip_proto >= 0)
3746 fk->ports.ports = __skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
3751 static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
3753 u32 srcmac_vendor = 0, srcmac_dev = 0;
3754 struct ethhdr *mac_hdr;
3758 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
3761 mac_hdr = (struct ethhdr *)(data + mhoff);
3763 for (i = 0; i < 3; i++)
3764 srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
3766 for (i = 3; i < ETH_ALEN; i++)
3767 srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
3769 if (skb && skb_vlan_tag_present(skb))
3770 vlan = skb_vlan_tag_get(skb);
3772 return vlan ^ srcmac_vendor ^ srcmac_dev;
3775 /* Extract the appropriate headers based on bond's xmit policy */
3776 static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
3777 __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
3779 bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
3782 switch (bond->params.xmit_policy) {
3783 case BOND_XMIT_POLICY_ENCAP23:
3784 case BOND_XMIT_POLICY_ENCAP34:
3785 memset(fk, 0, sizeof(*fk));
3786 return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
3787 fk, data, l2_proto, nhoff, hlen, 0);
3792 fk->ports.ports = 0;
3793 memset(&fk->icmp, 0, sizeof(fk->icmp));
3794 if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
3797 /* ICMP error packets contains at least 8 bytes of the header
3798 * of the packet which generated the error. Use this information
3799 * to correlate ICMP error packets within the same flow which
3800 * generated the error.
3802 if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
3803 skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
3804 if (ip_proto == IPPROTO_ICMP) {
3805 if (!icmp_is_err(fk->icmp.type))
3808 nhoff += sizeof(struct icmphdr);
3809 } else if (ip_proto == IPPROTO_ICMPV6) {
3810 if (!icmpv6_is_err(fk->icmp.type))
3813 nhoff += sizeof(struct icmp6hdr);
3815 return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
3821 static u32 bond_ip_hash(u32 hash, struct flow_keys *flow)
3823 hash ^= (__force u32)flow_get_u32_dst(flow) ^
3824 (__force u32)flow_get_u32_src(flow);
3825 hash ^= (hash >> 16);
3826 hash ^= (hash >> 8);
3827 /* discard lowest hash bit to deal with the common even ports pattern */
3831 /* Generate hash based on xmit policy. If @skb is given it is used to linearize
3832 * the data as required, but this function can be used without it if the data is
3833 * known to be linear (e.g. with xdp_buff).
3835 static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
3836 __be16 l2_proto, int mhoff, int nhoff, int hlen)
3838 struct flow_keys flow;
3841 if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
3842 return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
3844 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
3845 !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
3846 return bond_eth_hash(skb, data, mhoff, hlen);
3848 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
3849 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
3850 hash = bond_eth_hash(skb, data, mhoff, hlen);
3853 memcpy(&hash, &flow.icmp, sizeof(hash));
3855 memcpy(&hash, &flow.ports.ports, sizeof(hash));
3858 return bond_ip_hash(hash, &flow);
3862 * bond_xmit_hash - generate a hash value based on the xmit policy
3863 * @bond: bonding device
3864 * @skb: buffer to use for headers
3866 * This function will extract the necessary headers from the skb buffer and use
3867 * them to generate a hash based on the xmit_policy set in the bonding device
3869 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
3871 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
3875 return __bond_xmit_hash(bond, skb, skb->head, skb->protocol,
3876 skb->mac_header, skb->network_header,
3881 * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
3882 * @bond: bonding device
3883 * @xdp: buffer to use for headers
3885 * The XDP variant of bond_xmit_hash.
3887 static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
3891 if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
3894 eth = (struct ethhdr *)xdp->data;
3896 return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
3897 sizeof(struct ethhdr), xdp->data_end - xdp->data);
3900 /*-------------------------- Device entry points ----------------------------*/
3902 void bond_work_init_all(struct bonding *bond)
3904 INIT_DELAYED_WORK(&bond->mcast_work,
3905 bond_resend_igmp_join_requests_delayed);
3906 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3907 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3908 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
3909 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3910 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
3913 static void bond_work_cancel_all(struct bonding *bond)
3915 cancel_delayed_work_sync(&bond->mii_work);
3916 cancel_delayed_work_sync(&bond->arp_work);
3917 cancel_delayed_work_sync(&bond->alb_work);
3918 cancel_delayed_work_sync(&bond->ad_work);
3919 cancel_delayed_work_sync(&bond->mcast_work);
3920 cancel_delayed_work_sync(&bond->slave_arr_work);
3923 static int bond_open(struct net_device *bond_dev)
3925 struct bonding *bond = netdev_priv(bond_dev);
3926 struct list_head *iter;
3927 struct slave *slave;
3929 /* reset slave->backup and slave->inactive */
3930 if (bond_has_slaves(bond)) {
3931 bond_for_each_slave(bond, slave, iter) {
3932 if (bond_uses_primary(bond) &&
3933 slave != rcu_access_pointer(bond->curr_active_slave)) {
3934 bond_set_slave_inactive_flags(slave,
3935 BOND_SLAVE_NOTIFY_NOW);
3936 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
3937 bond_set_slave_active_flags(slave,
3938 BOND_SLAVE_NOTIFY_NOW);
3943 if (bond_is_lb(bond)) {
3944 /* bond_alb_initialize must be called before the timer
3947 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
3949 if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
3950 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3953 if (bond->params.miimon) /* link check interval, in milliseconds. */
3954 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3956 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3957 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3958 bond->recv_probe = bond_arp_rcv;
3961 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
3962 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3963 /* register to receive LACPDUs */
3964 bond->recv_probe = bond_3ad_lacpdu_recv;
3965 bond_3ad_initiate_agg_selection(bond, 1);
3968 if (bond_mode_can_use_xmit_hash(bond))
3969 bond_update_slave_arr(bond, NULL);
3974 static int bond_close(struct net_device *bond_dev)
3976 struct bonding *bond = netdev_priv(bond_dev);
3978 bond_work_cancel_all(bond);
3979 bond->send_peer_notif = 0;
3980 if (bond_is_lb(bond))
3981 bond_alb_deinitialize(bond);
3982 bond->recv_probe = NULL;
3987 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
3988 * that some drivers can provide 32bit values only.
3990 static void bond_fold_stats(struct rtnl_link_stats64 *_res,
3991 const struct rtnl_link_stats64 *_new,
3992 const struct rtnl_link_stats64 *_old)
3994 const u64 *new = (const u64 *)_new;
3995 const u64 *old = (const u64 *)_old;
3996 u64 *res = (u64 *)_res;
3999 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
4002 s64 delta = nv - ov;
4004 /* detects if this particular field is 32bit only */
4005 if (((nv | ov) >> 32) == 0)
4006 delta = (s64)(s32)((u32)nv - (u32)ov);
4008 /* filter anomalies, some drivers reset their stats
4009 * at down/up events.
4016 #ifdef CONFIG_LOCKDEP
4017 static int bond_get_lowest_level_rcu(struct net_device *dev)
4019 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
4020 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
4021 int cur = 0, max = 0;
4024 iter = &dev->adj_list.lower;
4029 ldev = netdev_next_lower_dev_rcu(now, &iter);
4034 niter = &ldev->adj_list.lower;
4035 dev_stack[cur] = now;
4036 iter_stack[cur++] = iter;
4045 next = dev_stack[--cur];
4046 niter = iter_stack[cur];
4057 static void bond_get_stats(struct net_device *bond_dev,
4058 struct rtnl_link_stats64 *stats)
4060 struct bonding *bond = netdev_priv(bond_dev);
4061 struct rtnl_link_stats64 temp;
4062 struct list_head *iter;
4063 struct slave *slave;
4068 #ifdef CONFIG_LOCKDEP
4069 nest_level = bond_get_lowest_level_rcu(bond_dev);
4072 spin_lock_nested(&bond->stats_lock, nest_level);
4073 memcpy(stats, &bond->bond_stats, sizeof(*stats));
4075 bond_for_each_slave_rcu(bond, slave, iter) {
4076 const struct rtnl_link_stats64 *new =
4077 dev_get_stats(slave->dev, &temp);
4079 bond_fold_stats(stats, new, &slave->slave_stats);
4081 /* save off the slave stats for the next run */
4082 memcpy(&slave->slave_stats, new, sizeof(*new));
4085 memcpy(&bond->bond_stats, stats, sizeof(*stats));
4086 spin_unlock(&bond->stats_lock);
4090 static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4092 struct bonding *bond = netdev_priv(bond_dev);
4093 struct mii_ioctl_data *mii = NULL;
4096 netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
4107 /* We do this again just in case we were called by SIOCGMIIREG
4108 * instead of SIOCGMIIPHY.
4114 if (mii->reg_num == 1) {
4116 if (netif_carrier_ok(bond->dev))
4117 mii->val_out = BMSR_LSTATUS;
4128 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4130 struct bonding *bond = netdev_priv(bond_dev);
4131 struct net_device *slave_dev = NULL;
4132 struct ifbond k_binfo;
4133 struct ifbond __user *u_binfo = NULL;
4134 struct ifslave k_sinfo;
4135 struct ifslave __user *u_sinfo = NULL;
4136 struct bond_opt_value newval;
4140 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
4143 case SIOCBONDINFOQUERY:
4144 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4146 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
4149 bond_info_query(bond_dev, &k_binfo);
4150 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4154 case SIOCBONDSLAVEINFOQUERY:
4155 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4157 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
4160 res = bond_slave_info_query(bond_dev, &k_sinfo);
4162 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4170 net = dev_net(bond_dev);
4172 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4175 slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
4177 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
4183 case SIOCBONDENSLAVE:
4184 res = bond_enslave(bond_dev, slave_dev, NULL);
4186 case SIOCBONDRELEASE:
4187 res = bond_release(bond_dev, slave_dev);
4189 case SIOCBONDSETHWADDR:
4190 res = bond_set_dev_addr(bond_dev, slave_dev);
4192 case SIOCBONDCHANGEACTIVE:
4193 bond_opt_initstr(&newval, slave_dev->name);
4194 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
4204 static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
4205 void __user *data, int cmd)
4207 struct ifreq ifrdata = { .ifr_data = data };
4210 case BOND_INFO_QUERY_OLD:
4211 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
4212 case BOND_SLAVE_INFO_QUERY_OLD:
4213 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
4214 case BOND_ENSLAVE_OLD:
4215 return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
4216 case BOND_RELEASE_OLD:
4217 return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
4218 case BOND_SETHWADDR_OLD:
4219 return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
4220 case BOND_CHANGE_ACTIVE_OLD:
4221 return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
4227 static void bond_change_rx_flags(struct net_device *bond_dev, int change)
4229 struct bonding *bond = netdev_priv(bond_dev);
4231 if (change & IFF_PROMISC)
4232 bond_set_promiscuity(bond,
4233 bond_dev->flags & IFF_PROMISC ? 1 : -1);
4235 if (change & IFF_ALLMULTI)
4236 bond_set_allmulti(bond,
4237 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
4240 static void bond_set_rx_mode(struct net_device *bond_dev)
4242 struct bonding *bond = netdev_priv(bond_dev);
4243 struct list_head *iter;
4244 struct slave *slave;
4247 if (bond_uses_primary(bond)) {
4248 slave = rcu_dereference(bond->curr_active_slave);
4250 dev_uc_sync(slave->dev, bond_dev);
4251 dev_mc_sync(slave->dev, bond_dev);
4254 bond_for_each_slave_rcu(bond, slave, iter) {
4255 dev_uc_sync_multiple(slave->dev, bond_dev);
4256 dev_mc_sync_multiple(slave->dev, bond_dev);
4262 static int bond_neigh_init(struct neighbour *n)
4264 struct bonding *bond = netdev_priv(n->dev);
4265 const struct net_device_ops *slave_ops;
4266 struct neigh_parms parms;
4267 struct slave *slave;
4271 slave = bond_first_slave_rcu(bond);
4274 slave_ops = slave->dev->netdev_ops;
4275 if (!slave_ops->ndo_neigh_setup)
4278 /* TODO: find another way [1] to implement this.
4279 * Passing a zeroed structure is fragile,
4280 * but at least we do not pass garbage.
4282 * [1] One way would be that ndo_neigh_setup() never touch
4283 * struct neigh_parms, but propagate the new neigh_setup()
4284 * back to ___neigh_create() / neigh_parms_alloc()
4286 memset(&parms, 0, sizeof(parms));
4287 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
4292 if (parms.neigh_setup)
4293 ret = parms.neigh_setup(n);
4299 /* The bonding ndo_neigh_setup is called at init time beofre any
4300 * slave exists. So we must declare proxy setup function which will
4301 * be used at run time to resolve the actual slave neigh param setup.
4303 * It's also called by master devices (such as vlans) to setup their
4304 * underlying devices. In that case - do nothing, we're already set up from
4307 static int bond_neigh_setup(struct net_device *dev,
4308 struct neigh_parms *parms)
4310 /* modify only our neigh_parms */
4311 if (parms->dev == dev)
4312 parms->neigh_setup = bond_neigh_init;
4317 /* Change the MTU of all of a master's slaves to match the master */
4318 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4320 struct bonding *bond = netdev_priv(bond_dev);
4321 struct slave *slave, *rollback_slave;
4322 struct list_head *iter;
4325 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
4327 bond_for_each_slave(bond, slave, iter) {
4328 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
4329 slave, slave->dev->netdev_ops->ndo_change_mtu);
4331 res = dev_set_mtu(slave->dev, new_mtu);
4334 /* If we failed to set the slave's mtu to the new value
4335 * we must abort the operation even in ACTIVE_BACKUP
4336 * mode, because if we allow the backup slaves to have
4337 * different mtu values than the active slave we'll
4338 * need to change their mtu when doing a failover. That
4339 * means changing their mtu from timer context, which
4340 * is probably not a good idea.
4342 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
4348 bond_dev->mtu = new_mtu;
4353 /* unwind from head to the slave that failed */
4354 bond_for_each_slave(bond, rollback_slave, iter) {
4357 if (rollback_slave == slave)
4360 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
4362 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
4369 /* Change HW address
4371 * Note that many devices must be down to change the HW address, and
4372 * downing the master releases all slaves. We can make bonds full of
4373 * bonding devices to test this, however.
4375 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4377 struct bonding *bond = netdev_priv(bond_dev);
4378 struct slave *slave, *rollback_slave;
4379 struct sockaddr_storage *ss = addr, tmp_ss;
4380 struct list_head *iter;
4383 if (BOND_MODE(bond) == BOND_MODE_ALB)
4384 return bond_alb_set_mac_address(bond_dev, addr);
4387 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
4389 /* If fail_over_mac is enabled, do nothing and return success.
4390 * Returning an error causes ifenslave to fail.
4392 if (bond->params.fail_over_mac &&
4393 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
4396 if (!is_valid_ether_addr(ss->__data))
4397 return -EADDRNOTAVAIL;
4399 bond_for_each_slave(bond, slave, iter) {
4400 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4402 res = dev_set_mac_address(slave->dev, addr, NULL);
4404 /* TODO: consider downing the slave
4406 * User should expect communications
4407 * breakage anyway until ARP finish
4410 slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4417 dev_addr_set(bond_dev, ss->__data);
4421 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4422 tmp_ss.ss_family = bond_dev->type;
4424 /* unwind from head to the slave that failed */
4425 bond_for_each_slave(bond, rollback_slave, iter) {
4428 if (rollback_slave == slave)
4431 tmp_res = dev_set_mac_address(rollback_slave->dev,
4432 (struct sockaddr *)&tmp_ss, NULL);
4434 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4443 * bond_get_slave_by_id - get xmit slave with slave_id
4444 * @bond: bonding device that is transmitting
4445 * @slave_id: slave id up to slave_cnt-1 through which to transmit
4447 * This function tries to get slave with slave_id but in case
4448 * it fails, it tries to find the first available slave for transmission.
4450 static struct slave *bond_get_slave_by_id(struct bonding *bond,
4453 struct list_head *iter;
4454 struct slave *slave;
4457 /* Here we start from the slave with slave_id */
4458 bond_for_each_slave_rcu(bond, slave, iter) {
4460 if (bond_slave_can_tx(slave))
4465 /* Here we start from the first slave up to slave_id */
4467 bond_for_each_slave_rcu(bond, slave, iter) {
4470 if (bond_slave_can_tx(slave))
4473 /* no slave that can tx has been found */
4478 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
4479 * @bond: bonding device to use
4481 * Based on the value of the bonding device's packets_per_slave parameter
4482 * this function generates a slave id, which is usually used as the next
4483 * slave to transmit through.
4485 static u32 bond_rr_gen_slave_id(struct bonding *bond)
4488 struct reciprocal_value reciprocal_packets_per_slave;
4489 int packets_per_slave = bond->params.packets_per_slave;
4491 switch (packets_per_slave) {
4493 slave_id = prandom_u32();
4496 slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4499 reciprocal_packets_per_slave =
4500 bond->params.reciprocal_packets_per_slave;
4501 slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4502 slave_id = reciprocal_divide(slave_id,
4503 reciprocal_packets_per_slave);
4510 static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
4511 struct sk_buff *skb)
4513 struct slave *slave;
4517 /* Start with the curr_active_slave that joined the bond as the
4518 * default for sending IGMP traffic. For failover purposes one
4519 * needs to maintain some consistency for the interface that will
4520 * send the join/membership reports. The curr_active_slave found
4521 * will send all of this type of traffic.
4523 if (skb->protocol == htons(ETH_P_IP)) {
4524 int noff = skb_network_offset(skb);
4527 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
4531 if (iph->protocol == IPPROTO_IGMP) {
4532 slave = rcu_dereference(bond->curr_active_slave);
4535 return bond_get_slave_by_id(bond, 0);
4540 slave_cnt = READ_ONCE(bond->slave_cnt);
4541 if (likely(slave_cnt)) {
4542 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4543 return bond_get_slave_by_id(bond, slave_id);
4548 static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
4549 struct xdp_buff *xdp)
4551 struct slave *slave;
4554 const struct ethhdr *eth;
4555 void *data = xdp->data;
4557 if (data + sizeof(struct ethhdr) > xdp->data_end)
4560 eth = (struct ethhdr *)data;
4561 data += sizeof(struct ethhdr);
4563 /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
4564 if (eth->h_proto == htons(ETH_P_IP)) {
4565 const struct iphdr *iph;
4567 if (data + sizeof(struct iphdr) > xdp->data_end)
4570 iph = (struct iphdr *)data;
4572 if (iph->protocol == IPPROTO_IGMP) {
4573 slave = rcu_dereference(bond->curr_active_slave);
4576 return bond_get_slave_by_id(bond, 0);
4581 slave_cnt = READ_ONCE(bond->slave_cnt);
4582 if (likely(slave_cnt)) {
4583 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4584 return bond_get_slave_by_id(bond, slave_id);
4589 static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
4590 struct net_device *bond_dev)
4592 struct bonding *bond = netdev_priv(bond_dev);
4593 struct slave *slave;
4595 slave = bond_xmit_roundrobin_slave_get(bond, skb);
4597 return bond_dev_queue_xmit(bond, skb, slave->dev);
4599 return bond_tx_drop(bond_dev, skb);
4602 static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
4604 return rcu_dereference(bond->curr_active_slave);
4607 /* In active-backup mode, we know that bond->curr_active_slave is always valid if
4608 * the bond has a usable interface.
4610 static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
4611 struct net_device *bond_dev)
4613 struct bonding *bond = netdev_priv(bond_dev);
4614 struct slave *slave;
4616 slave = bond_xmit_activebackup_slave_get(bond);
4618 return bond_dev_queue_xmit(bond, skb, slave->dev);
4620 return bond_tx_drop(bond_dev, skb);
4623 /* Use this to update slave_array when (a) it's not appropriate to update
4624 * slave_array right away (note that update_slave_array() may sleep)
4625 * and / or (b) RTNL is not held.
4627 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
4629 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
4632 /* Slave array work handler. Holds only RTNL */
4633 static void bond_slave_arr_handler(struct work_struct *work)
4635 struct bonding *bond = container_of(work, struct bonding,
4636 slave_arr_work.work);
4639 if (!rtnl_trylock())
4642 ret = bond_update_slave_arr(bond, NULL);
4645 pr_warn_ratelimited("Failed to update slave array from WT\n");
4651 bond_slave_arr_work_rearm(bond, 1);
4654 static void bond_skip_slave(struct bond_up_slave *slaves,
4655 struct slave *skipslave)
4659 /* Rare situation where caller has asked to skip a specific
4660 * slave but allocation failed (most likely!). BTW this is
4661 * only possible when the call is initiated from
4662 * __bond_release_one(). In this situation; overwrite the
4663 * skipslave entry in the array with the last entry from the
4664 * array to avoid a situation where the xmit path may choose
4665 * this to-be-skipped slave to send a packet out.
4667 for (idx = 0; slaves && idx < slaves->count; idx++) {
4668 if (skipslave == slaves->arr[idx]) {
4670 slaves->arr[slaves->count - 1];
4677 static void bond_set_slave_arr(struct bonding *bond,
4678 struct bond_up_slave *usable_slaves,
4679 struct bond_up_slave *all_slaves)
4681 struct bond_up_slave *usable, *all;
4683 usable = rtnl_dereference(bond->usable_slaves);
4684 rcu_assign_pointer(bond->usable_slaves, usable_slaves);
4685 kfree_rcu(usable, rcu);
4687 all = rtnl_dereference(bond->all_slaves);
4688 rcu_assign_pointer(bond->all_slaves, all_slaves);
4689 kfree_rcu(all, rcu);
4692 static void bond_reset_slave_arr(struct bonding *bond)
4694 struct bond_up_slave *usable, *all;
4696 usable = rtnl_dereference(bond->usable_slaves);
4698 RCU_INIT_POINTER(bond->usable_slaves, NULL);
4699 kfree_rcu(usable, rcu);
4702 all = rtnl_dereference(bond->all_slaves);
4704 RCU_INIT_POINTER(bond->all_slaves, NULL);
4705 kfree_rcu(all, rcu);
4709 /* Build the usable slaves array in control path for modes that use xmit-hash
4710 * to determine the slave interface -
4711 * (a) BOND_MODE_8023AD
4713 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
4715 * The caller is expected to hold RTNL only and NO other lock!
4717 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
4719 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
4720 struct slave *slave;
4721 struct list_head *iter;
4727 usable_slaves = kzalloc(struct_size(usable_slaves, arr,
4728 bond->slave_cnt), GFP_KERNEL);
4729 all_slaves = kzalloc(struct_size(all_slaves, arr,
4730 bond->slave_cnt), GFP_KERNEL);
4731 if (!usable_slaves || !all_slaves) {
4735 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4736 struct ad_info ad_info;
4738 spin_lock_bh(&bond->mode_lock);
4739 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
4740 spin_unlock_bh(&bond->mode_lock);
4741 pr_debug("bond_3ad_get_active_agg_info failed\n");
4742 /* No active aggragator means it's not safe to use
4743 * the previous array.
4745 bond_reset_slave_arr(bond);
4748 spin_unlock_bh(&bond->mode_lock);
4749 agg_id = ad_info.aggregator_id;
4751 bond_for_each_slave(bond, slave, iter) {
4752 if (skipslave == slave)
4755 all_slaves->arr[all_slaves->count++] = slave;
4756 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4757 struct aggregator *agg;
4759 agg = SLAVE_AD_INFO(slave)->port.aggregator;
4760 if (!agg || agg->aggregator_identifier != agg_id)
4763 if (!bond_slave_can_tx(slave))
4766 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
4767 usable_slaves->count);
4769 usable_slaves->arr[usable_slaves->count++] = slave;
4772 bond_set_slave_arr(bond, usable_slaves, all_slaves);
4775 if (ret != 0 && skipslave) {
4776 bond_skip_slave(rtnl_dereference(bond->all_slaves),
4778 bond_skip_slave(rtnl_dereference(bond->usable_slaves),
4781 kfree_rcu(all_slaves, rcu);
4782 kfree_rcu(usable_slaves, rcu);
4787 static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
4788 struct sk_buff *skb,
4789 struct bond_up_slave *slaves)
4791 struct slave *slave;
4795 hash = bond_xmit_hash(bond, skb);
4796 count = slaves ? READ_ONCE(slaves->count) : 0;
4797 if (unlikely(!count))
4800 slave = slaves->arr[hash % count];
4804 static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
4805 struct xdp_buff *xdp)
4807 struct bond_up_slave *slaves;
4811 hash = bond_xmit_hash_xdp(bond, xdp);
4812 slaves = rcu_dereference(bond->usable_slaves);
4813 count = slaves ? READ_ONCE(slaves->count) : 0;
4814 if (unlikely(!count))
4817 return slaves->arr[hash % count];
4820 /* Use this Xmit function for 3AD as well as XOR modes. The current
4821 * usable slave array is formed in the control path. The xmit function
4822 * just calculates hash and sends the packet out.
4824 static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
4825 struct net_device *dev)
4827 struct bonding *bond = netdev_priv(dev);
4828 struct bond_up_slave *slaves;
4829 struct slave *slave;
4831 slaves = rcu_dereference(bond->usable_slaves);
4832 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4834 return bond_dev_queue_xmit(bond, skb, slave->dev);
4836 return bond_tx_drop(dev, skb);
4839 /* in broadcast mode, we send everything to all usable interfaces. */
4840 static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
4841 struct net_device *bond_dev)
4843 struct bonding *bond = netdev_priv(bond_dev);
4844 struct slave *slave = NULL;
4845 struct list_head *iter;
4847 bond_for_each_slave_rcu(bond, slave, iter) {
4848 if (bond_is_last_slave(bond, slave))
4850 if (bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
4851 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4854 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
4855 bond_dev->name, __func__);
4858 bond_dev_queue_xmit(bond, skb2, slave->dev);
4861 if (slave && bond_slave_is_up(slave) && slave->link == BOND_LINK_UP)
4862 return bond_dev_queue_xmit(bond, skb, slave->dev);
4864 return bond_tx_drop(bond_dev, skb);
4867 /*------------------------- Device initialization ---------------------------*/
4869 /* Lookup the slave that corresponds to a qid */
4870 static inline int bond_slave_override(struct bonding *bond,
4871 struct sk_buff *skb)
4873 struct slave *slave = NULL;
4874 struct list_head *iter;
4876 if (!skb_rx_queue_recorded(skb))
4879 /* Find out if any slaves have the same mapping as this skb. */
4880 bond_for_each_slave_rcu(bond, slave, iter) {
4881 if (slave->queue_id == skb_get_queue_mapping(skb)) {
4882 if (bond_slave_is_up(slave) &&
4883 slave->link == BOND_LINK_UP) {
4884 bond_dev_queue_xmit(bond, skb, slave->dev);
4887 /* If the slave isn't UP, use default transmit policy. */
4896 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
4897 struct net_device *sb_dev)
4899 /* This helper function exists to help dev_pick_tx get the correct
4900 * destination queue. Using a helper function skips a call to
4901 * skb_tx_hash and will put the skbs in the queue we expect on their
4902 * way down to the bonding driver.
4904 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4906 /* Save the original txq to restore before passing to the driver */
4907 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
4909 if (unlikely(txq >= dev->real_num_tx_queues)) {
4911 txq -= dev->real_num_tx_queues;
4912 } while (txq >= dev->real_num_tx_queues);
4917 static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
4918 struct sk_buff *skb,
4921 struct bonding *bond = netdev_priv(master_dev);
4922 struct bond_up_slave *slaves;
4923 struct slave *slave = NULL;
4925 switch (BOND_MODE(bond)) {
4926 case BOND_MODE_ROUNDROBIN:
4927 slave = bond_xmit_roundrobin_slave_get(bond, skb);
4929 case BOND_MODE_ACTIVEBACKUP:
4930 slave = bond_xmit_activebackup_slave_get(bond);
4932 case BOND_MODE_8023AD:
4935 slaves = rcu_dereference(bond->all_slaves);
4937 slaves = rcu_dereference(bond->usable_slaves);
4938 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4940 case BOND_MODE_BROADCAST:
4943 slave = bond_xmit_alb_slave_get(bond, skb);
4946 slave = bond_xmit_tlb_slave_get(bond, skb);
4949 /* Should never happen, mode already checked */
4950 WARN_ONCE(true, "Unknown bonding mode");
4959 static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
4961 switch (sk->sk_family) {
4962 #if IS_ENABLED(CONFIG_IPV6)
4964 if (sk->sk_ipv6only ||
4965 ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
4966 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
4967 flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
4968 flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
4973 default: /* AF_INET */
4974 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
4975 flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
4976 flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
4980 flow->ports.src = inet_sk(sk)->inet_sport;
4981 flow->ports.dst = inet_sk(sk)->inet_dport;
4985 * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
4986 * @sk: socket to use for headers
4988 * This function will extract the necessary field from the socket and use
4989 * them to generate a hash based on the LAYER34 xmit_policy.
4990 * Assumes that sk is a TCP or UDP socket.
4992 static u32 bond_sk_hash_l34(struct sock *sk)
4994 struct flow_keys flow;
4997 bond_sk_to_flow(sk, &flow);
5000 memcpy(&hash, &flow.ports.ports, sizeof(hash));
5002 return bond_ip_hash(hash, &flow);
5005 static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
5008 struct bond_up_slave *slaves;
5009 struct slave *slave;
5013 slaves = rcu_dereference(bond->usable_slaves);
5014 count = slaves ? READ_ONCE(slaves->count) : 0;
5015 if (unlikely(!count))
5018 hash = bond_sk_hash_l34(sk);
5019 slave = slaves->arr[hash % count];
5024 static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
5027 struct bonding *bond = netdev_priv(dev);
5028 struct net_device *lower = NULL;
5031 if (bond_sk_check(bond))
5032 lower = __bond_sk_get_lower_dev(bond, sk);
5038 #if IS_ENABLED(CONFIG_TLS_DEVICE)
5039 static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
5040 struct net_device *dev)
5042 if (likely(bond_get_slave_by_dev(bond, tls_get_ctx(skb->sk)->netdev)))
5043 return bond_dev_queue_xmit(bond, skb, tls_get_ctx(skb->sk)->netdev);
5044 return bond_tx_drop(dev, skb);
5048 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5050 struct bonding *bond = netdev_priv(dev);
5052 if (bond_should_override_tx_queue(bond) &&
5053 !bond_slave_override(bond, skb))
5054 return NETDEV_TX_OK;
5056 #if IS_ENABLED(CONFIG_TLS_DEVICE)
5057 if (skb->sk && tls_is_sk_tx_device_offloaded(skb->sk))
5058 return bond_tls_device_xmit(bond, skb, dev);
5061 switch (BOND_MODE(bond)) {
5062 case BOND_MODE_ROUNDROBIN:
5063 return bond_xmit_roundrobin(skb, dev);
5064 case BOND_MODE_ACTIVEBACKUP:
5065 return bond_xmit_activebackup(skb, dev);
5066 case BOND_MODE_8023AD:
5068 return bond_3ad_xor_xmit(skb, dev);
5069 case BOND_MODE_BROADCAST:
5070 return bond_xmit_broadcast(skb, dev);
5072 return bond_alb_xmit(skb, dev);
5074 return bond_tlb_xmit(skb, dev);
5076 /* Should never happen, mode already checked */
5077 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
5079 return bond_tx_drop(dev, skb);
5083 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5085 struct bonding *bond = netdev_priv(dev);
5086 netdev_tx_t ret = NETDEV_TX_OK;
5088 /* If we risk deadlock from transmitting this in the
5089 * netpoll path, tell netpoll to queue the frame for later tx
5091 if (unlikely(is_netpoll_tx_blocked(dev)))
5092 return NETDEV_TX_BUSY;
5095 if (bond_has_slaves(bond))
5096 ret = __bond_start_xmit(skb, dev);
5098 ret = bond_tx_drop(dev, skb);
5104 static struct net_device *
5105 bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
5107 struct bonding *bond = netdev_priv(bond_dev);
5108 struct slave *slave;
5110 /* Caller needs to hold rcu_read_lock() */
5112 switch (BOND_MODE(bond)) {
5113 case BOND_MODE_ROUNDROBIN:
5114 slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
5117 case BOND_MODE_ACTIVEBACKUP:
5118 slave = bond_xmit_activebackup_slave_get(bond);
5121 case BOND_MODE_8023AD:
5123 slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
5127 /* Should never happen. Mode guarded by bond_xdp_check() */
5128 netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n", BOND_MODE(bond));
5139 static int bond_xdp_xmit(struct net_device *bond_dev,
5140 int n, struct xdp_frame **frames, u32 flags)
5142 int nxmit, err = -ENXIO;
5146 for (nxmit = 0; nxmit < n; nxmit++) {
5147 struct xdp_frame *frame = frames[nxmit];
5148 struct xdp_frame *frames1[] = {frame};
5149 struct net_device *slave_dev;
5150 struct xdp_buff xdp;
5152 xdp_convert_frame_to_buff(frame, &xdp);
5154 slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
5160 err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
5167 /* If error happened on the first frame then we can pass the error up, otherwise
5168 * report the number of frames that were xmitted.
5171 return (nxmit == 0 ? err : nxmit);
5176 static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
5177 struct netlink_ext_ack *extack)
5179 struct bonding *bond = netdev_priv(dev);
5180 struct list_head *iter;
5181 struct slave *slave, *rollback_slave;
5182 struct bpf_prog *old_prog;
5183 struct netdev_bpf xdp = {
5184 .command = XDP_SETUP_PROG,
5193 if (!bond_xdp_check(bond))
5196 old_prog = bond->xdp_prog;
5197 bond->xdp_prog = prog;
5199 bond_for_each_slave(bond, slave, iter) {
5200 struct net_device *slave_dev = slave->dev;
5202 if (!slave_dev->netdev_ops->ndo_bpf ||
5203 !slave_dev->netdev_ops->ndo_xdp_xmit) {
5204 SLAVE_NL_ERR(dev, slave_dev, extack,
5205 "Slave device does not support XDP");
5210 if (dev_xdp_prog_count(slave_dev) > 0) {
5211 SLAVE_NL_ERR(dev, slave_dev, extack,
5212 "Slave has XDP program loaded, please unload before enslaving");
5217 err = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
5219 /* ndo_bpf() sets extack error message */
5220 slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
5228 static_branch_inc(&bpf_master_redirect_enabled_key);
5229 } else if (old_prog) {
5230 bpf_prog_put(old_prog);
5231 static_branch_dec(&bpf_master_redirect_enabled_key);
5237 /* unwind the program changes */
5238 bond->xdp_prog = old_prog;
5239 xdp.prog = old_prog;
5240 xdp.extack = NULL; /* do not overwrite original error */
5242 bond_for_each_slave(bond, rollback_slave, iter) {
5243 struct net_device *slave_dev = rollback_slave->dev;
5246 if (slave == rollback_slave)
5249 err_unwind = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
5251 slave_err(dev, slave_dev,
5252 "Error %d when unwinding XDP program change\n", err_unwind);
5254 bpf_prog_inc(xdp.prog);
5259 static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5261 switch (xdp->command) {
5262 case XDP_SETUP_PROG:
5263 return bond_xdp_set(dev, xdp->prog, xdp->extack);
5269 static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
5271 if (speed == 0 || speed == SPEED_UNKNOWN)
5272 speed = slave->speed;
5274 speed = min(speed, slave->speed);
5279 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
5280 struct ethtool_link_ksettings *cmd)
5282 struct bonding *bond = netdev_priv(bond_dev);
5283 struct list_head *iter;
5284 struct slave *slave;
5287 cmd->base.duplex = DUPLEX_UNKNOWN;
5288 cmd->base.port = PORT_OTHER;
5290 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
5291 * do not need to check mode. Though link speed might not represent
5292 * the true receive or transmit bandwidth (not all modes are symmetric)
5293 * this is an accurate maximum.
5295 bond_for_each_slave(bond, slave, iter) {
5296 if (bond_slave_can_tx(slave)) {
5297 if (slave->speed != SPEED_UNKNOWN) {
5298 if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
5299 speed = bond_mode_bcast_speed(slave,
5302 speed += slave->speed;
5304 if (cmd->base.duplex == DUPLEX_UNKNOWN &&
5305 slave->duplex != DUPLEX_UNKNOWN)
5306 cmd->base.duplex = slave->duplex;
5309 cmd->base.speed = speed ? : SPEED_UNKNOWN;
5314 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
5315 struct ethtool_drvinfo *drvinfo)
5317 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
5318 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
5322 static const struct ethtool_ops bond_ethtool_ops = {
5323 .get_drvinfo = bond_ethtool_get_drvinfo,
5324 .get_link = ethtool_op_get_link,
5325 .get_link_ksettings = bond_ethtool_get_link_ksettings,
5328 static const struct net_device_ops bond_netdev_ops = {
5329 .ndo_init = bond_init,
5330 .ndo_uninit = bond_uninit,
5331 .ndo_open = bond_open,
5332 .ndo_stop = bond_close,
5333 .ndo_start_xmit = bond_start_xmit,
5334 .ndo_select_queue = bond_select_queue,
5335 .ndo_get_stats64 = bond_get_stats,
5336 .ndo_eth_ioctl = bond_eth_ioctl,
5337 .ndo_siocbond = bond_do_ioctl,
5338 .ndo_siocdevprivate = bond_siocdevprivate,
5339 .ndo_change_rx_flags = bond_change_rx_flags,
5340 .ndo_set_rx_mode = bond_set_rx_mode,
5341 .ndo_change_mtu = bond_change_mtu,
5342 .ndo_set_mac_address = bond_set_mac_address,
5343 .ndo_neigh_setup = bond_neigh_setup,
5344 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
5345 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
5346 #ifdef CONFIG_NET_POLL_CONTROLLER
5347 .ndo_netpoll_setup = bond_netpoll_setup,
5348 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
5349 .ndo_poll_controller = bond_poll_controller,
5351 .ndo_add_slave = bond_enslave,
5352 .ndo_del_slave = bond_release,
5353 .ndo_fix_features = bond_fix_features,
5354 .ndo_features_check = passthru_features_check,
5355 .ndo_get_xmit_slave = bond_xmit_get_slave,
5356 .ndo_sk_get_lower_dev = bond_sk_get_lower_dev,
5357 .ndo_bpf = bond_xdp,
5358 .ndo_xdp_xmit = bond_xdp_xmit,
5359 .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
5362 static const struct device_type bond_type = {
5366 static void bond_destructor(struct net_device *bond_dev)
5368 struct bonding *bond = netdev_priv(bond_dev);
5371 destroy_workqueue(bond->wq);
5373 if (bond->rr_tx_counter)
5374 free_percpu(bond->rr_tx_counter);
5377 void bond_setup(struct net_device *bond_dev)
5379 struct bonding *bond = netdev_priv(bond_dev);
5381 spin_lock_init(&bond->mode_lock);
5382 bond->params = bonding_defaults;
5384 /* Initialize pointers */
5385 bond->dev = bond_dev;
5387 /* Initialize the device entry points */
5388 ether_setup(bond_dev);
5389 bond_dev->max_mtu = ETH_MAX_MTU;
5390 bond_dev->netdev_ops = &bond_netdev_ops;
5391 bond_dev->ethtool_ops = &bond_ethtool_ops;
5393 bond_dev->needs_free_netdev = true;
5394 bond_dev->priv_destructor = bond_destructor;
5396 SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
5398 /* Initialize the device options */
5399 bond_dev->flags |= IFF_MASTER;
5400 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
5401 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
5403 #ifdef CONFIG_XFRM_OFFLOAD
5404 /* set up xfrm device ops (only supported in active-backup right now) */
5405 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
5406 INIT_LIST_HEAD(&bond->ipsec_list);
5407 spin_lock_init(&bond->ipsec_lock);
5408 #endif /* CONFIG_XFRM_OFFLOAD */
5410 /* don't acquire bond device's netif_tx_lock when transmitting */
5411 bond_dev->features |= NETIF_F_LLTX;
5413 /* By default, we declare the bond to be fully
5414 * VLAN hardware accelerated capable. Special
5415 * care is taken in the various xmit functions
5416 * when there are slaves that are not hw accel
5420 /* Don't allow bond devices to change network namespaces. */
5421 bond_dev->features |= NETIF_F_NETNS_LOCAL;
5423 bond_dev->hw_features = BOND_VLAN_FEATURES |
5424 NETIF_F_HW_VLAN_CTAG_RX |
5425 NETIF_F_HW_VLAN_CTAG_FILTER;
5427 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
5428 bond_dev->features |= bond_dev->hw_features;
5429 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
5430 #ifdef CONFIG_XFRM_OFFLOAD
5431 bond_dev->hw_features |= BOND_XFRM_FEATURES;
5432 /* Only enable XFRM features if this is an active-backup config */
5433 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
5434 bond_dev->features |= BOND_XFRM_FEATURES;
5435 #endif /* CONFIG_XFRM_OFFLOAD */
5436 #if IS_ENABLED(CONFIG_TLS_DEVICE)
5437 if (bond_sk_check(bond))
5438 bond_dev->features |= BOND_TLS_FEATURES;
5442 /* Destroy a bonding device.
5443 * Must be under rtnl_lock when this function is called.
5445 static void bond_uninit(struct net_device *bond_dev)
5447 struct bonding *bond = netdev_priv(bond_dev);
5448 struct bond_up_slave *usable, *all;
5449 struct list_head *iter;
5450 struct slave *slave;
5452 bond_netpoll_cleanup(bond_dev);
5454 /* Release the bonded slaves */
5455 bond_for_each_slave(bond, slave, iter)
5456 __bond_release_one(bond_dev, slave->dev, true, true);
5457 netdev_info(bond_dev, "Released all slaves\n");
5459 usable = rtnl_dereference(bond->usable_slaves);
5461 RCU_INIT_POINTER(bond->usable_slaves, NULL);
5462 kfree_rcu(usable, rcu);
5465 all = rtnl_dereference(bond->all_slaves);
5467 RCU_INIT_POINTER(bond->all_slaves, NULL);
5468 kfree_rcu(all, rcu);
5471 list_del(&bond->bond_list);
5473 bond_debug_unregister(bond);
5476 /*------------------------- Module initialization ---------------------------*/
5478 static int bond_check_params(struct bond_params *params)
5480 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
5481 struct bond_opt_value newval;
5482 const struct bond_opt_value *valptr;
5483 int arp_all_targets_value = 0;
5484 u16 ad_actor_sys_prio = 0;
5485 u16 ad_user_port_key = 0;
5486 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
5488 int bond_mode = BOND_MODE_ROUNDROBIN;
5489 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
5493 /* Convert string parameters. */
5495 bond_opt_initstr(&newval, mode);
5496 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
5498 pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
5501 bond_mode = valptr->value;
5504 if (xmit_hash_policy) {
5505 if (bond_mode == BOND_MODE_ROUNDROBIN ||
5506 bond_mode == BOND_MODE_ACTIVEBACKUP ||
5507 bond_mode == BOND_MODE_BROADCAST) {
5508 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
5509 bond_mode_name(bond_mode));
5511 bond_opt_initstr(&newval, xmit_hash_policy);
5512 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
5515 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
5519 xmit_hashtype = valptr->value;
5524 if (bond_mode != BOND_MODE_8023AD) {
5525 pr_info("lacp_rate param is irrelevant in mode %s\n",
5526 bond_mode_name(bond_mode));
5528 bond_opt_initstr(&newval, lacp_rate);
5529 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
5532 pr_err("Error: Invalid lacp rate \"%s\"\n",
5536 lacp_fast = valptr->value;
5541 bond_opt_initstr(&newval, ad_select);
5542 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
5545 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
5548 params->ad_select = valptr->value;
5549 if (bond_mode != BOND_MODE_8023AD)
5550 pr_warn("ad_select param only affects 802.3ad mode\n");
5552 params->ad_select = BOND_AD_STABLE;
5555 if (max_bonds < 0) {
5556 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
5557 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
5558 max_bonds = BOND_DEFAULT_MAX_BONDS;
5562 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5568 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5573 if (downdelay < 0) {
5574 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5575 downdelay, INT_MAX);
5579 if ((use_carrier != 0) && (use_carrier != 1)) {
5580 pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
5585 if (num_peer_notif < 0 || num_peer_notif > 255) {
5586 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
5591 /* reset values for 802.3ad/TLB/ALB */
5592 if (!bond_mode_uses_arp(bond_mode)) {
5594 pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
5595 pr_warn("Forcing miimon to 100msec\n");
5596 miimon = BOND_DEFAULT_MIIMON;
5600 if (tx_queues < 1 || tx_queues > 255) {
5601 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
5602 tx_queues, BOND_DEFAULT_TX_QUEUES);
5603 tx_queues = BOND_DEFAULT_TX_QUEUES;
5606 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
5607 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
5609 all_slaves_active = 0;
5612 if (resend_igmp < 0 || resend_igmp > 255) {
5613 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
5614 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
5615 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
5618 bond_opt_initval(&newval, packets_per_slave);
5619 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
5620 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
5621 packets_per_slave, USHRT_MAX);
5622 packets_per_slave = 1;
5625 if (bond_mode == BOND_MODE_ALB) {
5626 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
5631 if (updelay || downdelay) {
5632 /* just warn the user the up/down delay will have
5633 * no effect since miimon is zero...
5635 pr_warn("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
5636 updelay, downdelay);
5639 /* don't allow arp monitoring */
5641 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5642 miimon, arp_interval);
5646 if ((updelay % miimon) != 0) {
5647 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5648 updelay, miimon, (updelay / miimon) * miimon);
5653 if ((downdelay % miimon) != 0) {
5654 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5656 (downdelay / miimon) * miimon);
5659 downdelay /= miimon;
5662 if (arp_interval < 0) {
5663 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5664 arp_interval, INT_MAX);
5668 for (arp_ip_count = 0, i = 0;
5669 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
5672 /* not a complete check, but good enough to catch mistakes */
5673 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
5674 !bond_is_ip_target_ok(ip)) {
5675 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5679 if (bond_get_targets_ip(arp_target, ip) == -1)
5680 arp_target[arp_ip_count++] = ip;
5682 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
5687 if (arp_interval && !arp_ip_count) {
5688 /* don't allow arping if no arp_ip_target given... */
5689 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5695 if (!arp_interval) {
5696 pr_err("arp_validate requires arp_interval\n");
5700 bond_opt_initstr(&newval, arp_validate);
5701 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
5704 pr_err("Error: invalid arp_validate \"%s\"\n",
5708 arp_validate_value = valptr->value;
5710 arp_validate_value = 0;
5713 if (arp_all_targets) {
5714 bond_opt_initstr(&newval, arp_all_targets);
5715 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
5718 pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
5720 arp_all_targets_value = 0;
5722 arp_all_targets_value = valptr->value;
5727 pr_info("MII link monitoring set to %d ms\n", miimon);
5728 } else if (arp_interval) {
5729 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
5730 arp_validate_value);
5731 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5732 arp_interval, valptr->string, arp_ip_count);
5734 for (i = 0; i < arp_ip_count; i++)
5735 pr_cont(" %s", arp_ip_target[i]);
5739 } else if (max_bonds) {
5740 /* miimon and arp_interval not set, we need one so things
5741 * work as expected, see bonding.txt for details
5743 pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n");
5746 if (primary && !bond_mode_uses_primary(bond_mode)) {
5747 /* currently, using a primary only makes sense
5748 * in active backup, TLB or ALB modes
5750 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
5751 primary, bond_mode_name(bond_mode));
5755 if (primary && primary_reselect) {
5756 bond_opt_initstr(&newval, primary_reselect);
5757 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
5760 pr_err("Error: Invalid primary_reselect \"%s\"\n",
5764 primary_reselect_value = valptr->value;
5766 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5769 if (fail_over_mac) {
5770 bond_opt_initstr(&newval, fail_over_mac);
5771 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
5774 pr_err("Error: invalid fail_over_mac \"%s\"\n",
5778 fail_over_mac_value = valptr->value;
5779 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5780 pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
5782 fail_over_mac_value = BOND_FOM_NONE;
5785 bond_opt_initstr(&newval, "default");
5786 valptr = bond_opt_parse(
5787 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
5790 pr_err("Error: No ad_actor_sys_prio default value");
5793 ad_actor_sys_prio = valptr->value;
5795 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
5798 pr_err("Error: No ad_user_port_key default value");
5801 ad_user_port_key = valptr->value;
5803 bond_opt_initstr(&newval, "default");
5804 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
5806 pr_err("Error: No tlb_dynamic_lb default value");
5809 tlb_dynamic_lb = valptr->value;
5811 if (lp_interval == 0) {
5812 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
5813 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
5814 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
5817 /* fill params struct with the proper values */
5818 params->mode = bond_mode;
5819 params->xmit_policy = xmit_hashtype;
5820 params->miimon = miimon;
5821 params->num_peer_notif = num_peer_notif;
5822 params->arp_interval = arp_interval;
5823 params->arp_validate = arp_validate_value;
5824 params->arp_all_targets = arp_all_targets_value;
5825 params->updelay = updelay;
5826 params->downdelay = downdelay;
5827 params->peer_notif_delay = 0;
5828 params->use_carrier = use_carrier;
5829 params->lacp_active = 1;
5830 params->lacp_fast = lacp_fast;
5831 params->primary[0] = 0;
5832 params->primary_reselect = primary_reselect_value;
5833 params->fail_over_mac = fail_over_mac_value;
5834 params->tx_queues = tx_queues;
5835 params->all_slaves_active = all_slaves_active;
5836 params->resend_igmp = resend_igmp;
5837 params->min_links = min_links;
5838 params->lp_interval = lp_interval;
5839 params->packets_per_slave = packets_per_slave;
5840 params->tlb_dynamic_lb = tlb_dynamic_lb;
5841 params->ad_actor_sys_prio = ad_actor_sys_prio;
5842 eth_zero_addr(params->ad_actor_system);
5843 params->ad_user_port_key = ad_user_port_key;
5844 if (packets_per_slave > 0) {
5845 params->reciprocal_packets_per_slave =
5846 reciprocal_value(packets_per_slave);
5848 /* reciprocal_packets_per_slave is unused if
5849 * packets_per_slave is 0 or 1, just initialize it
5851 params->reciprocal_packets_per_slave =
5852 (struct reciprocal_value) { 0 };
5856 strscpy_pad(params->primary, primary, sizeof(params->primary));
5858 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5863 /* Called from registration process */
5864 static int bond_init(struct net_device *bond_dev)
5866 struct bonding *bond = netdev_priv(bond_dev);
5867 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
5869 netdev_dbg(bond_dev, "Begin bond_init\n");
5871 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM);
5875 if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN) {
5876 bond->rr_tx_counter = alloc_percpu(u32);
5877 if (!bond->rr_tx_counter) {
5878 destroy_workqueue(bond->wq);
5884 spin_lock_init(&bond->stats_lock);
5885 netdev_lockdep_set_classes(bond_dev);
5887 list_add_tail(&bond->bond_list, &bn->dev_list);
5889 bond_prepare_sysfs_group(bond);
5891 bond_debug_register(bond);
5893 /* Ensure valid dev_addr */
5894 if (is_zero_ether_addr(bond_dev->dev_addr) &&
5895 bond_dev->addr_assign_type == NET_ADDR_PERM)
5896 eth_hw_addr_random(bond_dev);
5901 unsigned int bond_get_num_tx_queues(void)
5906 /* Create a new bond based on the specified name and bonding parameters.
5907 * If name is NULL, obtain a suitable "bond%d" name for us.
5908 * Caller must NOT hold rtnl_lock; we need to release it here before we
5909 * set up our sysfs entries.
5911 int bond_create(struct net *net, const char *name)
5913 struct net_device *bond_dev;
5914 struct bonding *bond;
5915 struct alb_bond_info *bond_info;
5920 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
5921 name ? name : "bond%d", NET_NAME_UNKNOWN,
5922 bond_setup, tx_queues);
5924 pr_err("%s: eek! can't alloc netdev!\n", name);
5930 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX.
5931 * It is set to 0 by default which is wrong.
5933 bond = netdev_priv(bond_dev);
5934 bond_info = &(BOND_ALB_INFO(bond));
5935 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
5937 dev_net_set(bond_dev, net);
5938 bond_dev->rtnl_link_ops = &bond_link_ops;
5940 res = register_netdevice(bond_dev);
5942 free_netdev(bond_dev);
5948 netif_carrier_off(bond_dev);
5950 bond_work_init_all(bond);
5956 static int __net_init bond_net_init(struct net *net)
5958 struct bond_net *bn = net_generic(net, bond_net_id);
5961 INIT_LIST_HEAD(&bn->dev_list);
5963 bond_create_proc_dir(bn);
5964 bond_create_sysfs(bn);
5969 static void __net_exit bond_net_exit(struct net *net)
5971 struct bond_net *bn = net_generic(net, bond_net_id);
5972 struct bonding *bond, *tmp_bond;
5975 bond_destroy_sysfs(bn);
5977 /* Kill off any bonds created after unregistering bond rtnl ops */
5979 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
5980 unregister_netdevice_queue(bond->dev, &list);
5981 unregister_netdevice_many(&list);
5984 bond_destroy_proc_dir(bn);
5987 static struct pernet_operations bond_net_ops = {
5988 .init = bond_net_init,
5989 .exit = bond_net_exit,
5991 .size = sizeof(struct bond_net),
5994 static int __init bonding_init(void)
5999 res = bond_check_params(&bonding_defaults);
6003 res = register_pernet_subsys(&bond_net_ops);
6007 res = bond_netlink_init();
6011 bond_create_debugfs();
6013 for (i = 0; i < max_bonds; i++) {
6014 res = bond_create(&init_net, NULL);
6019 skb_flow_dissector_init(&flow_keys_bonding,
6020 flow_keys_bonding_keys,
6021 ARRAY_SIZE(flow_keys_bonding_keys));
6023 register_netdevice_notifier(&bond_netdev_notifier);
6027 bond_destroy_debugfs();
6028 bond_netlink_fini();
6030 unregister_pernet_subsys(&bond_net_ops);
6035 static void __exit bonding_exit(void)
6037 unregister_netdevice_notifier(&bond_netdev_notifier);
6039 bond_destroy_debugfs();
6041 bond_netlink_fini();
6042 unregister_pernet_subsys(&bond_net_ops);
6044 #ifdef CONFIG_NET_POLL_CONTROLLER
6045 /* Make sure we don't have an imbalance on our netpoll blocking */
6046 WARN_ON(atomic_read(&netpoll_block_tx));
6050 module_init(bonding_init);
6051 module_exit(bonding_exit);
6052 MODULE_LICENSE("GPL");
6053 MODULE_DESCRIPTION(DRV_DESCRIPTION);