2 * originally based on the dummy device.
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
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>
87 #include "bonding_priv.h"
89 /*---------------------------- Module parameters ----------------------------*/
91 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
93 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
94 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
95 static int num_peer_notif = 1;
99 static int use_carrier = 1;
101 static char *primary;
102 static char *primary_reselect;
103 static char *lacp_rate;
104 static int min_links;
105 static char *ad_select;
106 static char *xmit_hash_policy;
107 static int arp_interval;
108 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
109 static char *arp_validate;
110 static char *arp_all_targets;
111 static char *fail_over_mac;
112 static int all_slaves_active;
113 static struct bond_params bonding_defaults;
114 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
115 static int packets_per_slave = 1;
116 static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
118 module_param(max_bonds, int, 0);
119 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
120 module_param(tx_queues, int, 0);
121 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
122 module_param_named(num_grat_arp, num_peer_notif, int, 0644);
123 MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
124 "failover event (alias of num_unsol_na)");
125 module_param_named(num_unsol_na, num_peer_notif, int, 0644);
126 MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
127 "failover event (alias of num_grat_arp)");
128 module_param(miimon, int, 0);
129 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
130 module_param(updelay, int, 0);
131 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
132 module_param(downdelay, int, 0);
133 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
135 module_param(use_carrier, int, 0);
136 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
137 "0 for off, 1 for on (default)");
138 module_param(mode, charp, 0);
139 MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
140 "1 for active-backup, 2 for balance-xor, "
141 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
142 "6 for balance-alb");
143 module_param(primary, charp, 0);
144 MODULE_PARM_DESC(primary, "Primary network device to use");
145 module_param(primary_reselect, charp, 0);
146 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
148 "0 for always (default), "
149 "1 for only if speed of primary is "
151 "2 for only on active slave "
153 module_param(lacp_rate, charp, 0);
154 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
155 "0 for slow, 1 for fast");
156 module_param(ad_select, charp, 0);
157 MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
158 "0 for stable (default), 1 for bandwidth, "
160 module_param(min_links, int, 0);
161 MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
163 module_param(xmit_hash_policy, charp, 0);
164 MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
165 "0 for layer 2 (default), 1 for layer 3+4, "
166 "2 for layer 2+3, 3 for encap layer 2+3, "
167 "4 for encap layer 3+4");
168 module_param(arp_interval, int, 0);
169 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
170 module_param_array(arp_ip_target, charp, NULL, 0);
171 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
172 module_param(arp_validate, charp, 0);
173 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
174 "0 for none (default), 1 for active, "
175 "2 for backup, 3 for all");
176 module_param(arp_all_targets, charp, 0);
177 MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
178 module_param(fail_over_mac, charp, 0);
179 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
180 "the same MAC; 0 for none (default), "
181 "1 for active, 2 for follow");
182 module_param(all_slaves_active, int, 0);
183 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
184 "by setting active flag for all slaves; "
185 "0 for never (default), 1 for always.");
186 module_param(resend_igmp, int, 0);
187 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
189 module_param(packets_per_slave, int, 0);
190 MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
191 "mode; 0 for a random slave, 1 packet per "
192 "slave (default), >1 packets per slave.");
193 module_param(lp_interval, uint, 0);
194 MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
195 "the bonding driver sends learning packets to "
196 "each slaves peer switch. The default is 1.");
198 /*----------------------------- Global variables ----------------------------*/
200 #ifdef CONFIG_NET_POLL_CONTROLLER
201 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
204 unsigned int bond_net_id __read_mostly;
206 static const struct flow_dissector_key flow_keys_bonding_keys[] = {
208 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
209 .offset = offsetof(struct flow_keys, control),
212 .key_id = FLOW_DISSECTOR_KEY_BASIC,
213 .offset = offsetof(struct flow_keys, basic),
216 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
217 .offset = offsetof(struct flow_keys, addrs.v4addrs),
220 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
221 .offset = offsetof(struct flow_keys, addrs.v6addrs),
224 .key_id = FLOW_DISSECTOR_KEY_TIPC,
225 .offset = offsetof(struct flow_keys, addrs.tipckey),
228 .key_id = FLOW_DISSECTOR_KEY_PORTS,
229 .offset = offsetof(struct flow_keys, ports),
232 .key_id = FLOW_DISSECTOR_KEY_ICMP,
233 .offset = offsetof(struct flow_keys, icmp),
236 .key_id = FLOW_DISSECTOR_KEY_VLAN,
237 .offset = offsetof(struct flow_keys, vlan),
240 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
241 .offset = offsetof(struct flow_keys, tags),
244 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
245 .offset = offsetof(struct flow_keys, keyid),
249 static struct flow_dissector flow_keys_bonding __read_mostly;
251 /*-------------------------- Forward declarations ---------------------------*/
253 static int bond_init(struct net_device *bond_dev);
254 static void bond_uninit(struct net_device *bond_dev);
255 static void bond_get_stats(struct net_device *bond_dev,
256 struct rtnl_link_stats64 *stats);
257 static void bond_slave_arr_handler(struct work_struct *work);
258 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
260 static void bond_netdev_notify_work(struct work_struct *work);
262 /*---------------------------- General routines -----------------------------*/
264 const char *bond_mode_name(int mode)
266 static const char *names[] = {
267 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
268 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
269 [BOND_MODE_XOR] = "load balancing (xor)",
270 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
271 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
272 [BOND_MODE_TLB] = "transmit load balancing",
273 [BOND_MODE_ALB] = "adaptive load balancing",
276 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
283 * bond_dev_queue_xmit - Prepare skb for xmit.
285 * @bond: bond device that got this skb for tx.
286 * @skb: hw accel VLAN tagged skb to transmit
287 * @slave_dev: slave that is supposed to xmit this skbuff
289 netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
290 struct net_device *slave_dev)
292 skb->dev = slave_dev;
294 BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
295 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
296 skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
298 if (unlikely(netpoll_tx_running(bond->dev)))
299 return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
301 return dev_queue_xmit(skb);
304 /*---------------------------------- VLAN -----------------------------------*/
306 /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
307 * We don't protect the slave list iteration with a lock because:
308 * a. This operation is performed in IOCTL context,
309 * b. The operation is protected by the RTNL semaphore in the 8021q code,
310 * c. Holding a lock with BH disabled while directly calling a base driver
311 * entry point is generally a BAD idea.
313 * The design of synchronization/protection for this operation in the 8021q
314 * module is good for one or more VLAN devices over a single physical device
315 * and cannot be extended for a teaming solution like bonding, so there is a
316 * potential race condition here where a net device from the vlan group might
317 * be referenced (either by a base driver or the 8021q code) while it is being
318 * removed from the system. However, it turns out we're not making matters
319 * worse, and if it works for regular VLAN usage it will work here too.
323 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
324 * @bond_dev: bonding net device that got called
325 * @proto: network protocol ID
326 * @vid: vlan id being added
328 static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
329 __be16 proto, u16 vid)
331 struct bonding *bond = netdev_priv(bond_dev);
332 struct slave *slave, *rollback_slave;
333 struct list_head *iter;
336 bond_for_each_slave(bond, slave, iter) {
337 res = vlan_vid_add(slave->dev, proto, vid);
345 /* unwind to the slave that failed */
346 bond_for_each_slave(bond, rollback_slave, iter) {
347 if (rollback_slave == slave)
350 vlan_vid_del(rollback_slave->dev, proto, vid);
357 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
358 * @bond_dev: bonding net device that got called
359 * @proto: network protocol ID
360 * @vid: vlan id being removed
362 static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
363 __be16 proto, u16 vid)
365 struct bonding *bond = netdev_priv(bond_dev);
366 struct list_head *iter;
369 bond_for_each_slave(bond, slave, iter)
370 vlan_vid_del(slave->dev, proto, vid);
372 if (bond_is_lb(bond))
373 bond_alb_clear_vlan(bond, vid);
378 /*---------------------------------- XFRM -----------------------------------*/
380 #ifdef CONFIG_XFRM_OFFLOAD
382 * bond_ipsec_add_sa - program device with a security association
383 * @xs: pointer to transformer state struct
385 static int bond_ipsec_add_sa(struct xfrm_state *xs)
387 struct net_device *bond_dev = xs->xso.dev;
388 struct bonding *bond;
394 bond = netdev_priv(bond_dev);
395 slave = rcu_dereference(bond->curr_active_slave);
396 xs->xso.real_dev = slave->dev;
399 if (!(slave->dev->xfrmdev_ops
400 && slave->dev->xfrmdev_ops->xdo_dev_state_add)) {
401 slave_warn(bond_dev, slave->dev, "Slave does not support ipsec offload\n");
405 return slave->dev->xfrmdev_ops->xdo_dev_state_add(xs);
409 * bond_ipsec_del_sa - clear out this specific SA
410 * @xs: pointer to transformer state struct
412 static void bond_ipsec_del_sa(struct xfrm_state *xs)
414 struct net_device *bond_dev = xs->xso.dev;
415 struct bonding *bond;
421 bond = netdev_priv(bond_dev);
422 slave = rcu_dereference(bond->curr_active_slave);
427 xs->xso.real_dev = slave->dev;
429 if (!(slave->dev->xfrmdev_ops
430 && slave->dev->xfrmdev_ops->xdo_dev_state_delete)) {
431 slave_warn(bond_dev, slave->dev, "%s: no slave xdo_dev_state_delete\n", __func__);
435 slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs);
439 * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
440 * @skb: current data packet
441 * @xs: pointer to transformer state struct
443 static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
445 struct net_device *bond_dev = xs->xso.dev;
446 struct bonding *bond = netdev_priv(bond_dev);
447 struct slave *curr_active = rcu_dereference(bond->curr_active_slave);
448 struct net_device *slave_dev = curr_active->dev;
450 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
453 if (!(slave_dev->xfrmdev_ops
454 && slave_dev->xfrmdev_ops->xdo_dev_offload_ok)) {
455 slave_warn(bond_dev, slave_dev, "%s: no slave xdo_dev_offload_ok\n", __func__);
459 xs->xso.real_dev = slave_dev;
460 return slave_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
463 static const struct xfrmdev_ops bond_xfrmdev_ops = {
464 .xdo_dev_state_add = bond_ipsec_add_sa,
465 .xdo_dev_state_delete = bond_ipsec_del_sa,
466 .xdo_dev_offload_ok = bond_ipsec_offload_ok,
468 #endif /* CONFIG_XFRM_OFFLOAD */
470 /*------------------------------- Link status -------------------------------*/
472 /* Set the carrier state for the master according to the state of its
473 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
474 * do special 802.3ad magic.
476 * Returns zero if carrier state does not change, nonzero if it does.
478 int bond_set_carrier(struct bonding *bond)
480 struct list_head *iter;
483 if (!bond_has_slaves(bond))
486 if (BOND_MODE(bond) == BOND_MODE_8023AD)
487 return bond_3ad_set_carrier(bond);
489 bond_for_each_slave(bond, slave, iter) {
490 if (slave->link == BOND_LINK_UP) {
491 if (!netif_carrier_ok(bond->dev)) {
492 netif_carrier_on(bond->dev);
500 if (netif_carrier_ok(bond->dev)) {
501 netif_carrier_off(bond->dev);
507 /* Get link speed and duplex from the slave's base driver
508 * using ethtool. If for some reason the call fails or the
509 * values are invalid, set speed and duplex to -1,
510 * and return. Return 1 if speed or duplex settings are
511 * UNKNOWN; 0 otherwise.
513 static int bond_update_speed_duplex(struct slave *slave)
515 struct net_device *slave_dev = slave->dev;
516 struct ethtool_link_ksettings ecmd;
519 slave->speed = SPEED_UNKNOWN;
520 slave->duplex = DUPLEX_UNKNOWN;
522 res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
525 if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
527 switch (ecmd.base.duplex) {
535 slave->speed = ecmd.base.speed;
536 slave->duplex = ecmd.base.duplex;
541 const char *bond_slave_link_status(s8 link)
557 /* if <dev> supports MII link status reporting, check its link status.
559 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
560 * depending upon the setting of the use_carrier parameter.
562 * Return either BMSR_LSTATUS, meaning that the link is up (or we
563 * can't tell and just pretend it is), or 0, meaning that the link is
566 * If reporting is non-zero, instead of faking link up, return -1 if
567 * both ETHTOOL and MII ioctls fail (meaning the device does not
568 * support them). If use_carrier is set, return whatever it says.
569 * It'd be nice if there was a good way to tell if a driver supports
570 * netif_carrier, but there really isn't.
572 static int bond_check_dev_link(struct bonding *bond,
573 struct net_device *slave_dev, int reporting)
575 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
576 int (*ioctl)(struct net_device *, struct ifreq *, int);
578 struct mii_ioctl_data *mii;
580 if (!reporting && !netif_running(slave_dev))
583 if (bond->params.use_carrier)
584 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
586 /* Try to get link status using Ethtool first. */
587 if (slave_dev->ethtool_ops->get_link)
588 return slave_dev->ethtool_ops->get_link(slave_dev) ?
591 /* Ethtool can't be used, fallback to MII ioctls. */
592 ioctl = slave_ops->ndo_do_ioctl;
594 /* TODO: set pointer to correct ioctl on a per team member
595 * bases to make this more efficient. that is, once
596 * we determine the correct ioctl, we will always
597 * call it and not the others for that team
601 /* We cannot assume that SIOCGMIIPHY will also read a
602 * register; not all network drivers (e.g., e100)
606 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
607 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
609 if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
610 mii->reg_num = MII_BMSR;
611 if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0)
612 return mii->val_out & BMSR_LSTATUS;
616 /* If reporting, report that either there's no dev->do_ioctl,
617 * or both SIOCGMIIREG and get_link failed (meaning that we
618 * cannot report link status). If not reporting, pretend
621 return reporting ? -1 : BMSR_LSTATUS;
624 /*----------------------------- Multicast list ------------------------------*/
626 /* Push the promiscuity flag down to appropriate slaves */
627 static int bond_set_promiscuity(struct bonding *bond, int inc)
629 struct list_head *iter;
632 if (bond_uses_primary(bond)) {
633 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
636 err = dev_set_promiscuity(curr_active->dev, inc);
640 bond_for_each_slave(bond, slave, iter) {
641 err = dev_set_promiscuity(slave->dev, inc);
649 /* Push the allmulti flag down to all slaves */
650 static int bond_set_allmulti(struct bonding *bond, int inc)
652 struct list_head *iter;
655 if (bond_uses_primary(bond)) {
656 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
659 err = dev_set_allmulti(curr_active->dev, inc);
663 bond_for_each_slave(bond, slave, iter) {
664 err = dev_set_allmulti(slave->dev, inc);
672 /* Retrieve the list of registered multicast addresses for the bonding
673 * device and retransmit an IGMP JOIN request to the current active
676 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
678 struct bonding *bond = container_of(work, struct bonding,
681 if (!rtnl_trylock()) {
682 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
685 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
687 if (bond->igmp_retrans > 1) {
688 bond->igmp_retrans--;
689 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
694 /* Flush bond's hardware addresses from slave */
695 static void bond_hw_addr_flush(struct net_device *bond_dev,
696 struct net_device *slave_dev)
698 struct bonding *bond = netdev_priv(bond_dev);
700 dev_uc_unsync(slave_dev, bond_dev);
701 dev_mc_unsync(slave_dev, bond_dev);
703 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
704 /* del lacpdu mc addr from mc list */
705 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
707 dev_mc_del(slave_dev, lacpdu_multicast);
711 /*--------------------------- Active slave change ---------------------------*/
713 /* Update the hardware address list and promisc/allmulti for the new and
714 * old active slaves (if any). Modes that are not using primary keep all
715 * slaves up date at all times; only the modes that use primary need to call
716 * this function to swap these settings during a failover.
718 static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
719 struct slave *old_active)
722 if (bond->dev->flags & IFF_PROMISC)
723 dev_set_promiscuity(old_active->dev, -1);
725 if (bond->dev->flags & IFF_ALLMULTI)
726 dev_set_allmulti(old_active->dev, -1);
728 bond_hw_addr_flush(bond->dev, old_active->dev);
732 /* FIXME: Signal errors upstream. */
733 if (bond->dev->flags & IFF_PROMISC)
734 dev_set_promiscuity(new_active->dev, 1);
736 if (bond->dev->flags & IFF_ALLMULTI)
737 dev_set_allmulti(new_active->dev, 1);
739 netif_addr_lock_bh(bond->dev);
740 dev_uc_sync(new_active->dev, bond->dev);
741 dev_mc_sync(new_active->dev, bond->dev);
742 netif_addr_unlock_bh(bond->dev);
747 * bond_set_dev_addr - clone slave's address to bond
748 * @bond_dev: bond net device
749 * @slave_dev: slave net device
751 * Should be called with RTNL held.
753 static int bond_set_dev_addr(struct net_device *bond_dev,
754 struct net_device *slave_dev)
758 slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
759 bond_dev, slave_dev, slave_dev->addr_len);
760 err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
764 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
765 bond_dev->addr_assign_type = NET_ADDR_STOLEN;
766 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
770 static struct slave *bond_get_old_active(struct bonding *bond,
771 struct slave *new_active)
774 struct list_head *iter;
776 bond_for_each_slave(bond, slave, iter) {
777 if (slave == new_active)
780 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
787 /* bond_do_fail_over_mac
789 * Perform special MAC address swapping for fail_over_mac settings
793 static void bond_do_fail_over_mac(struct bonding *bond,
794 struct slave *new_active,
795 struct slave *old_active)
797 u8 tmp_mac[MAX_ADDR_LEN];
798 struct sockaddr_storage ss;
801 switch (bond->params.fail_over_mac) {
802 case BOND_FOM_ACTIVE:
804 rv = bond_set_dev_addr(bond->dev, new_active->dev);
806 slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
810 case BOND_FOM_FOLLOW:
811 /* if new_active && old_active, swap them
812 * if just old_active, do nothing (going to no active slave)
813 * if just new_active, set new_active to bond's MAC
819 old_active = bond_get_old_active(bond, new_active);
822 bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
823 new_active->dev->addr_len);
824 bond_hw_addr_copy(ss.__data,
825 old_active->dev->dev_addr,
826 old_active->dev->addr_len);
827 ss.ss_family = new_active->dev->type;
829 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
830 bond->dev->addr_len);
831 ss.ss_family = bond->dev->type;
834 rv = dev_set_mac_address(new_active->dev,
835 (struct sockaddr *)&ss, NULL);
837 slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
845 bond_hw_addr_copy(ss.__data, tmp_mac,
846 new_active->dev->addr_len);
847 ss.ss_family = old_active->dev->type;
849 rv = dev_set_mac_address(old_active->dev,
850 (struct sockaddr *)&ss, NULL);
852 slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
857 netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
858 bond->params.fail_over_mac);
864 static struct slave *bond_choose_primary_or_current(struct bonding *bond)
866 struct slave *prim = rtnl_dereference(bond->primary_slave);
867 struct slave *curr = rtnl_dereference(bond->curr_active_slave);
869 if (!prim || prim->link != BOND_LINK_UP) {
870 if (!curr || curr->link != BOND_LINK_UP)
875 if (bond->force_primary) {
876 bond->force_primary = false;
880 if (!curr || curr->link != BOND_LINK_UP)
883 /* At this point, prim and curr are both up */
884 switch (bond->params.primary_reselect) {
885 case BOND_PRI_RESELECT_ALWAYS:
887 case BOND_PRI_RESELECT_BETTER:
888 if (prim->speed < curr->speed)
890 if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
893 case BOND_PRI_RESELECT_FAILURE:
896 netdev_err(bond->dev, "impossible primary_reselect %d\n",
897 bond->params.primary_reselect);
903 * bond_find_best_slave - select the best available slave to be the active one
904 * @bond: our bonding struct
906 static struct slave *bond_find_best_slave(struct bonding *bond)
908 struct slave *slave, *bestslave = NULL;
909 struct list_head *iter;
910 int mintime = bond->params.updelay;
912 slave = bond_choose_primary_or_current(bond);
916 bond_for_each_slave(bond, slave, iter) {
917 if (slave->link == BOND_LINK_UP)
919 if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
920 slave->delay < mintime) {
921 mintime = slave->delay;
929 static bool bond_should_notify_peers(struct bonding *bond)
934 slave = rcu_dereference(bond->curr_active_slave);
937 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
938 slave ? slave->dev->name : "NULL");
940 if (!slave || !bond->send_peer_notif ||
941 bond->send_peer_notif %
942 max(1, bond->params.peer_notif_delay) != 0 ||
943 !netif_carrier_ok(bond->dev) ||
944 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
951 * change_active_interface - change the active slave into the specified one
952 * @bond: our bonding struct
953 * @new_active: the new slave to make the active one
955 * Set the new slave to the bond's settings and unset them on the old
957 * Setting include flags, mc-list, promiscuity, allmulti, etc.
959 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
960 * because it is apparently the best available slave we have, even though its
961 * updelay hasn't timed out yet.
963 * Caller must hold RTNL.
965 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
967 struct slave *old_active;
971 old_active = rtnl_dereference(bond->curr_active_slave);
973 if (old_active == new_active)
976 #ifdef CONFIG_XFRM_OFFLOAD
977 if (old_active && bond->xs)
978 bond_ipsec_del_sa(bond->xs);
979 #endif /* CONFIG_XFRM_OFFLOAD */
982 new_active->last_link_up = jiffies;
984 if (new_active->link == BOND_LINK_BACK) {
985 if (bond_uses_primary(bond)) {
986 slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
987 (bond->params.updelay - new_active->delay) * bond->params.miimon);
990 new_active->delay = 0;
991 bond_set_slave_link_state(new_active, BOND_LINK_UP,
992 BOND_SLAVE_NOTIFY_NOW);
994 if (BOND_MODE(bond) == BOND_MODE_8023AD)
995 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
997 if (bond_is_lb(bond))
998 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1000 if (bond_uses_primary(bond)) {
1001 slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
1006 if (bond_uses_primary(bond))
1007 bond_hw_addr_swap(bond, new_active, old_active);
1009 if (bond_is_lb(bond)) {
1010 bond_alb_handle_active_change(bond, new_active);
1012 bond_set_slave_inactive_flags(old_active,
1013 BOND_SLAVE_NOTIFY_NOW);
1015 bond_set_slave_active_flags(new_active,
1016 BOND_SLAVE_NOTIFY_NOW);
1018 rcu_assign_pointer(bond->curr_active_slave, new_active);
1021 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
1023 bond_set_slave_inactive_flags(old_active,
1024 BOND_SLAVE_NOTIFY_NOW);
1027 bool should_notify_peers = false;
1029 bond_set_slave_active_flags(new_active,
1030 BOND_SLAVE_NOTIFY_NOW);
1032 if (bond->params.fail_over_mac)
1033 bond_do_fail_over_mac(bond, new_active,
1036 if (netif_running(bond->dev)) {
1037 bond->send_peer_notif =
1038 bond->params.num_peer_notif *
1039 max(1, bond->params.peer_notif_delay);
1040 should_notify_peers =
1041 bond_should_notify_peers(bond);
1044 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
1045 if (should_notify_peers) {
1046 bond->send_peer_notif--;
1047 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
1053 #ifdef CONFIG_XFRM_OFFLOAD
1054 if (new_active && bond->xs) {
1055 xfrm_dev_state_flush(dev_net(bond->dev), bond->dev, true);
1056 bond_ipsec_add_sa(bond->xs);
1058 #endif /* CONFIG_XFRM_OFFLOAD */
1060 /* resend IGMP joins since active slave has changed or
1061 * all were sent on curr_active_slave.
1062 * resend only if bond is brought up with the affected
1063 * bonding modes and the retransmission is enabled
1065 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1066 ((bond_uses_primary(bond) && new_active) ||
1067 BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
1068 bond->igmp_retrans = bond->params.resend_igmp;
1069 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
1074 * bond_select_active_slave - select a new active slave, if needed
1075 * @bond: our bonding struct
1077 * This functions should be called when one of the following occurs:
1078 * - The old curr_active_slave has been released or lost its link.
1079 * - The primary_slave has got its link back.
1080 * - A slave has got its link back and there's no old curr_active_slave.
1082 * Caller must hold RTNL.
1084 void bond_select_active_slave(struct bonding *bond)
1086 struct slave *best_slave;
1091 best_slave = bond_find_best_slave(bond);
1092 if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
1093 bond_change_active_slave(bond, best_slave);
1094 rv = bond_set_carrier(bond);
1098 if (netif_carrier_ok(bond->dev))
1099 netdev_info(bond->dev, "active interface up!\n");
1101 netdev_info(bond->dev, "now running without any active interface!\n");
1105 #ifdef CONFIG_NET_POLL_CONTROLLER
1106 static inline int slave_enable_netpoll(struct slave *slave)
1111 np = kzalloc(sizeof(*np), GFP_KERNEL);
1116 err = __netpoll_setup(np, slave->dev);
1125 static inline void slave_disable_netpoll(struct slave *slave)
1127 struct netpoll *np = slave->np;
1137 static void bond_poll_controller(struct net_device *bond_dev)
1139 struct bonding *bond = netdev_priv(bond_dev);
1140 struct slave *slave = NULL;
1141 struct list_head *iter;
1142 struct ad_info ad_info;
1144 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1145 if (bond_3ad_get_active_agg_info(bond, &ad_info))
1148 bond_for_each_slave_rcu(bond, slave, iter) {
1149 if (!bond_slave_is_up(slave))
1152 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1153 struct aggregator *agg =
1154 SLAVE_AD_INFO(slave)->port.aggregator;
1157 agg->aggregator_identifier != ad_info.aggregator_id)
1161 netpoll_poll_dev(slave->dev);
1165 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1167 struct bonding *bond = netdev_priv(bond_dev);
1168 struct list_head *iter;
1169 struct slave *slave;
1171 bond_for_each_slave(bond, slave, iter)
1172 if (bond_slave_is_up(slave))
1173 slave_disable_netpoll(slave);
1176 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1178 struct bonding *bond = netdev_priv(dev);
1179 struct list_head *iter;
1180 struct slave *slave;
1183 bond_for_each_slave(bond, slave, iter) {
1184 err = slave_enable_netpoll(slave);
1186 bond_netpoll_cleanup(dev);
1193 static inline int slave_enable_netpoll(struct slave *slave)
1197 static inline void slave_disable_netpoll(struct slave *slave)
1200 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1205 /*---------------------------------- IOCTL ----------------------------------*/
1207 static netdev_features_t bond_fix_features(struct net_device *dev,
1208 netdev_features_t features)
1210 struct bonding *bond = netdev_priv(dev);
1211 struct list_head *iter;
1212 netdev_features_t mask;
1213 struct slave *slave;
1217 features &= ~NETIF_F_ONE_FOR_ALL;
1218 features |= NETIF_F_ALL_FOR_ALL;
1220 bond_for_each_slave(bond, slave, iter) {
1221 features = netdev_increment_features(features,
1222 slave->dev->features,
1225 features = netdev_add_tso_features(features, mask);
1230 #define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1231 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
1232 NETIF_F_HIGHDMA | NETIF_F_LRO)
1234 #define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1235 NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
1237 #define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1241 static void bond_compute_features(struct bonding *bond)
1243 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
1244 IFF_XMIT_DST_RELEASE_PERM;
1245 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
1246 netdev_features_t enc_features = BOND_ENC_FEATURES;
1247 #ifdef CONFIG_XFRM_OFFLOAD
1248 netdev_features_t xfrm_features = BOND_XFRM_FEATURES;
1249 #endif /* CONFIG_XFRM_OFFLOAD */
1250 netdev_features_t mpls_features = BOND_MPLS_FEATURES;
1251 struct net_device *bond_dev = bond->dev;
1252 struct list_head *iter;
1253 struct slave *slave;
1254 unsigned short max_hard_header_len = ETH_HLEN;
1255 unsigned int gso_max_size = GSO_MAX_SIZE;
1256 u16 gso_max_segs = GSO_MAX_SEGS;
1258 if (!bond_has_slaves(bond))
1260 vlan_features &= NETIF_F_ALL_FOR_ALL;
1261 mpls_features &= NETIF_F_ALL_FOR_ALL;
1263 bond_for_each_slave(bond, slave, iter) {
1264 vlan_features = netdev_increment_features(vlan_features,
1265 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1267 enc_features = netdev_increment_features(enc_features,
1268 slave->dev->hw_enc_features,
1271 #ifdef CONFIG_XFRM_OFFLOAD
1272 xfrm_features = netdev_increment_features(xfrm_features,
1273 slave->dev->hw_enc_features,
1274 BOND_XFRM_FEATURES);
1275 #endif /* CONFIG_XFRM_OFFLOAD */
1277 mpls_features = netdev_increment_features(mpls_features,
1278 slave->dev->mpls_features,
1279 BOND_MPLS_FEATURES);
1281 dst_release_flag &= slave->dev->priv_flags;
1282 if (slave->dev->hard_header_len > max_hard_header_len)
1283 max_hard_header_len = slave->dev->hard_header_len;
1285 gso_max_size = min(gso_max_size, slave->dev->gso_max_size);
1286 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs);
1288 bond_dev->hard_header_len = max_hard_header_len;
1291 bond_dev->vlan_features = vlan_features;
1292 bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
1293 NETIF_F_HW_VLAN_CTAG_TX |
1294 NETIF_F_HW_VLAN_STAG_TX |
1296 #ifdef CONFIG_XFRM_OFFLOAD
1297 bond_dev->hw_enc_features |= xfrm_features;
1298 #endif /* CONFIG_XFRM_OFFLOAD */
1299 bond_dev->mpls_features = mpls_features;
1300 bond_dev->gso_max_segs = gso_max_segs;
1301 netif_set_gso_max_size(bond_dev, gso_max_size);
1303 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1304 if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
1305 dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1306 bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1308 netdev_change_features(bond_dev);
1311 static void bond_setup_by_slave(struct net_device *bond_dev,
1312 struct net_device *slave_dev)
1314 bond_dev->header_ops = slave_dev->header_ops;
1316 bond_dev->type = slave_dev->type;
1317 bond_dev->hard_header_len = slave_dev->hard_header_len;
1318 bond_dev->addr_len = slave_dev->addr_len;
1320 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1321 slave_dev->addr_len);
1324 /* On bonding slaves other than the currently active slave, suppress
1325 * duplicates except for alb non-mcast/bcast.
1327 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1328 struct slave *slave,
1329 struct bonding *bond)
1331 if (bond_is_slave_inactive(slave)) {
1332 if (BOND_MODE(bond) == BOND_MODE_ALB &&
1333 skb->pkt_type != PACKET_BROADCAST &&
1334 skb->pkt_type != PACKET_MULTICAST)
1341 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1343 struct sk_buff *skb = *pskb;
1344 struct slave *slave;
1345 struct bonding *bond;
1346 int (*recv_probe)(const struct sk_buff *, struct bonding *,
1348 int ret = RX_HANDLER_ANOTHER;
1350 skb = skb_share_check(skb, GFP_ATOMIC);
1352 return RX_HANDLER_CONSUMED;
1356 slave = bond_slave_get_rcu(skb->dev);
1359 recv_probe = READ_ONCE(bond->recv_probe);
1361 ret = recv_probe(skb, bond, slave);
1362 if (ret == RX_HANDLER_CONSUMED) {
1369 * For packets determined by bond_should_deliver_exact_match() call to
1370 * be suppressed we want to make an exception for link-local packets.
1371 * This is necessary for e.g. LLDP daemons to be able to monitor
1372 * inactive slave links without being forced to bind to them
1375 * At the same time, packets that are passed to the bonding master
1376 * (including link-local ones) can have their originating interface
1377 * determined via PACKET_ORIGDEV socket option.
1379 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1380 if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
1381 return RX_HANDLER_PASS;
1382 return RX_HANDLER_EXACT;
1385 skb->dev = bond->dev;
1387 if (BOND_MODE(bond) == BOND_MODE_ALB &&
1388 netif_is_bridge_port(bond->dev) &&
1389 skb->pkt_type == PACKET_HOST) {
1391 if (unlikely(skb_cow_head(skb,
1392 skb->data - skb_mac_header(skb)))) {
1394 return RX_HANDLER_CONSUMED;
1396 bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
1397 bond->dev->addr_len);
1403 static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
1405 switch (BOND_MODE(bond)) {
1406 case BOND_MODE_ROUNDROBIN:
1407 return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
1408 case BOND_MODE_ACTIVEBACKUP:
1409 return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
1410 case BOND_MODE_BROADCAST:
1411 return NETDEV_LAG_TX_TYPE_BROADCAST;
1413 case BOND_MODE_8023AD:
1414 return NETDEV_LAG_TX_TYPE_HASH;
1416 return NETDEV_LAG_TX_TYPE_UNKNOWN;
1420 static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
1421 enum netdev_lag_tx_type type)
1423 if (type != NETDEV_LAG_TX_TYPE_HASH)
1424 return NETDEV_LAG_HASH_NONE;
1426 switch (bond->params.xmit_policy) {
1427 case BOND_XMIT_POLICY_LAYER2:
1428 return NETDEV_LAG_HASH_L2;
1429 case BOND_XMIT_POLICY_LAYER34:
1430 return NETDEV_LAG_HASH_L34;
1431 case BOND_XMIT_POLICY_LAYER23:
1432 return NETDEV_LAG_HASH_L23;
1433 case BOND_XMIT_POLICY_ENCAP23:
1434 return NETDEV_LAG_HASH_E23;
1435 case BOND_XMIT_POLICY_ENCAP34:
1436 return NETDEV_LAG_HASH_E34;
1438 return NETDEV_LAG_HASH_UNKNOWN;
1442 static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
1443 struct netlink_ext_ack *extack)
1445 struct netdev_lag_upper_info lag_upper_info;
1446 enum netdev_lag_tx_type type;
1448 type = bond_lag_tx_type(bond);
1449 lag_upper_info.tx_type = type;
1450 lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
1452 return netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
1453 &lag_upper_info, extack);
1456 static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
1458 netdev_upper_dev_unlink(slave->dev, bond->dev);
1459 slave->dev->flags &= ~IFF_SLAVE;
1462 static struct slave *bond_alloc_slave(struct bonding *bond)
1464 struct slave *slave = NULL;
1466 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1470 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1471 SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
1473 if (!SLAVE_AD_INFO(slave)) {
1478 INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
1483 static void bond_free_slave(struct slave *slave)
1485 struct bonding *bond = bond_get_bond_by_slave(slave);
1487 cancel_delayed_work_sync(&slave->notify_work);
1488 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1489 kfree(SLAVE_AD_INFO(slave));
1494 static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
1496 info->bond_mode = BOND_MODE(bond);
1497 info->miimon = bond->params.miimon;
1498 info->num_slaves = bond->slave_cnt;
1501 static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
1503 strcpy(info->slave_name, slave->dev->name);
1504 info->link = slave->link;
1505 info->state = bond_slave_state(slave);
1506 info->link_failure_count = slave->link_failure_count;
1509 static void bond_netdev_notify_work(struct work_struct *_work)
1511 struct slave *slave = container_of(_work, struct slave,
1514 if (rtnl_trylock()) {
1515 struct netdev_bonding_info binfo;
1517 bond_fill_ifslave(slave, &binfo.slave);
1518 bond_fill_ifbond(slave->bond, &binfo.master);
1519 netdev_bonding_info_change(slave->dev, &binfo);
1522 queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
1526 void bond_queue_slave_event(struct slave *slave)
1528 queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
1531 void bond_lower_state_changed(struct slave *slave)
1533 struct netdev_lag_lower_state_info info;
1535 info.link_up = slave->link == BOND_LINK_UP ||
1536 slave->link == BOND_LINK_FAIL;
1537 info.tx_enabled = bond_is_active_slave(slave);
1538 netdev_lower_state_changed(slave->dev, &info);
1541 /* enslave device <slave> to bond device <master> */
1542 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
1543 struct netlink_ext_ack *extack)
1545 struct bonding *bond = netdev_priv(bond_dev);
1546 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1547 struct slave *new_slave = NULL, *prev_slave;
1548 struct sockaddr_storage ss;
1552 if (!bond->params.use_carrier &&
1553 slave_dev->ethtool_ops->get_link == NULL &&
1554 slave_ops->ndo_do_ioctl == NULL) {
1555 slave_warn(bond_dev, slave_dev, "no link monitoring support\n");
1558 /* already in-use? */
1559 if (netdev_is_rx_handler_busy(slave_dev)) {
1560 NL_SET_ERR_MSG(extack, "Device is in use and cannot be enslaved");
1561 slave_err(bond_dev, slave_dev,
1562 "Error: Device is in use and cannot be enslaved\n");
1566 if (bond_dev == slave_dev) {
1567 NL_SET_ERR_MSG(extack, "Cannot enslave bond to itself.");
1568 netdev_err(bond_dev, "cannot enslave bond to itself.\n");
1572 /* vlan challenged mutual exclusion */
1573 /* no need to lock since we're protected by rtnl_lock */
1574 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1575 slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
1576 if (vlan_uses_dev(bond_dev)) {
1577 NL_SET_ERR_MSG(extack, "Can not enslave VLAN challenged device to VLAN enabled bond");
1578 slave_err(bond_dev, slave_dev, "Error: cannot enslave VLAN challenged slave on VLAN enabled bond\n");
1581 slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
1584 slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
1587 if (slave_dev->features & NETIF_F_HW_ESP)
1588 slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
1590 /* Old ifenslave binaries are no longer supported. These can
1591 * be identified with moderate accuracy by the state of the slave:
1592 * the current ifenslave will set the interface down prior to
1593 * enslaving it; the old ifenslave will not.
1595 if (slave_dev->flags & IFF_UP) {
1596 NL_SET_ERR_MSG(extack, "Device can not be enslaved while up");
1597 slave_err(bond_dev, slave_dev, "slave is up - this may be due to an out of date ifenslave\n");
1601 /* set bonding device ether type by slave - bonding netdevices are
1602 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1603 * there is a need to override some of the type dependent attribs/funcs.
1605 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1606 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1608 if (!bond_has_slaves(bond)) {
1609 if (bond_dev->type != slave_dev->type) {
1610 slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
1611 bond_dev->type, slave_dev->type);
1613 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1615 res = notifier_to_errno(res);
1617 slave_err(bond_dev, slave_dev, "refused to change device type\n");
1621 /* Flush unicast and multicast addresses */
1622 dev_uc_flush(bond_dev);
1623 dev_mc_flush(bond_dev);
1625 if (slave_dev->type != ARPHRD_ETHER)
1626 bond_setup_by_slave(bond_dev, slave_dev);
1628 ether_setup(bond_dev);
1629 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1632 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1635 } else if (bond_dev->type != slave_dev->type) {
1636 NL_SET_ERR_MSG(extack, "Device type is different from other slaves");
1637 slave_err(bond_dev, slave_dev, "ether type (%d) is different from other slaves (%d), can not enslave it\n",
1638 slave_dev->type, bond_dev->type);
1642 if (slave_dev->type == ARPHRD_INFINIBAND &&
1643 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
1644 NL_SET_ERR_MSG(extack, "Only active-backup mode is supported for infiniband slaves");
1645 slave_warn(bond_dev, slave_dev, "Type (%d) supports only active-backup mode\n",
1648 goto err_undo_flags;
1651 if (!slave_ops->ndo_set_mac_address ||
1652 slave_dev->type == ARPHRD_INFINIBAND) {
1653 slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
1654 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1655 bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1656 if (!bond_has_slaves(bond)) {
1657 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1658 slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
1660 NL_SET_ERR_MSG(extack, "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
1661 slave_err(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active\n");
1663 goto err_undo_flags;
1668 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1670 /* If this is the first slave, then we need to set the master's hardware
1671 * address to be the same as the slave's.
1673 if (!bond_has_slaves(bond) &&
1674 bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
1675 res = bond_set_dev_addr(bond->dev, slave_dev);
1677 goto err_undo_flags;
1680 new_slave = bond_alloc_slave(bond);
1683 goto err_undo_flags;
1686 new_slave->bond = bond;
1687 new_slave->dev = slave_dev;
1688 /* Set the new_slave's queue_id to be zero. Queue ID mapping
1689 * is set via sysfs or module option if desired.
1691 new_slave->queue_id = 0;
1693 /* Save slave's original mtu and then set it to match the bond */
1694 new_slave->original_mtu = slave_dev->mtu;
1695 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1697 slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
1701 /* Save slave's original ("permanent") mac address for modes
1702 * that need it, and for restoring it upon release, and then
1703 * set it to the master's address
1705 bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
1706 slave_dev->addr_len);
1708 if (!bond->params.fail_over_mac ||
1709 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
1710 /* Set slave to master's mac address. The application already
1711 * set the master's mac address to that of the first slave
1713 memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
1714 ss.ss_family = slave_dev->type;
1715 res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss,
1718 slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
1719 goto err_restore_mtu;
1723 /* set slave flag before open to prevent IPv6 addrconf */
1724 slave_dev->flags |= IFF_SLAVE;
1726 /* open the slave since the application closed it */
1727 res = dev_open(slave_dev, extack);
1729 slave_err(bond_dev, slave_dev, "Opening slave failed\n");
1730 goto err_restore_mac;
1733 slave_dev->priv_flags |= IFF_BONDING;
1734 /* initialize slave stats */
1735 dev_get_stats(new_slave->dev, &new_slave->slave_stats);
1737 if (bond_is_lb(bond)) {
1738 /* bond_alb_init_slave() must be called before all other stages since
1739 * it might fail and we do not want to have to undo everything
1741 res = bond_alb_init_slave(bond, new_slave);
1746 res = vlan_vids_add_by_dev(slave_dev, bond_dev);
1748 slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
1752 prev_slave = bond_last_slave(bond);
1754 new_slave->delay = 0;
1755 new_slave->link_failure_count = 0;
1757 if (bond_update_speed_duplex(new_slave) &&
1758 bond_needs_speed_duplex(bond))
1759 new_slave->link = BOND_LINK_DOWN;
1761 new_slave->last_rx = jiffies -
1762 (msecs_to_jiffies(bond->params.arp_interval) + 1);
1763 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1764 new_slave->target_last_arp_rx[i] = new_slave->last_rx;
1766 if (bond->params.miimon && !bond->params.use_carrier) {
1767 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1769 if ((link_reporting == -1) && !bond->params.arp_interval) {
1770 /* miimon is set but a bonded network driver
1771 * does not support ETHTOOL/MII and
1772 * arp_interval is not set. Note: if
1773 * use_carrier is enabled, we will never go
1774 * here (because netif_carrier is always
1775 * supported); thus, we don't need to change
1776 * the messages for netif_carrier.
1778 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");
1779 } else if (link_reporting == -1) {
1780 /* unable get link status using mii/ethtool */
1781 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");
1785 /* check for initial state */
1786 new_slave->link = BOND_LINK_NOCHANGE;
1787 if (bond->params.miimon) {
1788 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
1789 if (bond->params.updelay) {
1790 bond_set_slave_link_state(new_slave,
1792 BOND_SLAVE_NOTIFY_NOW);
1793 new_slave->delay = bond->params.updelay;
1795 bond_set_slave_link_state(new_slave,
1797 BOND_SLAVE_NOTIFY_NOW);
1800 bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
1801 BOND_SLAVE_NOTIFY_NOW);
1803 } else if (bond->params.arp_interval) {
1804 bond_set_slave_link_state(new_slave,
1805 (netif_carrier_ok(slave_dev) ?
1806 BOND_LINK_UP : BOND_LINK_DOWN),
1807 BOND_SLAVE_NOTIFY_NOW);
1809 bond_set_slave_link_state(new_slave, BOND_LINK_UP,
1810 BOND_SLAVE_NOTIFY_NOW);
1813 if (new_slave->link != BOND_LINK_DOWN)
1814 new_slave->last_link_up = jiffies;
1815 slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
1816 new_slave->link == BOND_LINK_DOWN ? "DOWN" :
1817 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
1819 if (bond_uses_primary(bond) && bond->params.primary[0]) {
1820 /* if there is a primary slave, remember it */
1821 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1822 rcu_assign_pointer(bond->primary_slave, new_slave);
1823 bond->force_primary = true;
1827 switch (BOND_MODE(bond)) {
1828 case BOND_MODE_ACTIVEBACKUP:
1829 bond_set_slave_inactive_flags(new_slave,
1830 BOND_SLAVE_NOTIFY_NOW);
1832 case BOND_MODE_8023AD:
1833 /* in 802.3ad mode, the internal mechanism
1834 * will activate the slaves in the selected
1837 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
1838 /* if this is the first slave */
1840 SLAVE_AD_INFO(new_slave)->id = 1;
1841 /* Initialize AD with the number of times that the AD timer is called in 1 second
1842 * can be called only after the mac address of the bond is set
1844 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
1846 SLAVE_AD_INFO(new_slave)->id =
1847 SLAVE_AD_INFO(prev_slave)->id + 1;
1850 bond_3ad_bind_slave(new_slave);
1854 bond_set_active_slave(new_slave);
1855 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
1858 slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
1860 /* always active in trunk mode */
1861 bond_set_active_slave(new_slave);
1863 /* In trunking mode there is little meaning to curr_active_slave
1864 * anyway (it holds no special properties of the bond device),
1865 * so we can change it without calling change_active_interface()
1867 if (!rcu_access_pointer(bond->curr_active_slave) &&
1868 new_slave->link == BOND_LINK_UP)
1869 rcu_assign_pointer(bond->curr_active_slave, new_slave);
1872 } /* switch(bond_mode) */
1874 #ifdef CONFIG_NET_POLL_CONTROLLER
1875 if (bond->dev->npinfo) {
1876 if (slave_enable_netpoll(new_slave)) {
1877 slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
1884 if (!(bond_dev->features & NETIF_F_LRO))
1885 dev_disable_lro(slave_dev);
1887 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1890 slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
1894 res = bond_master_upper_dev_link(bond, new_slave, extack);
1896 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
1897 goto err_unregister;
1900 res = bond_sysfs_slave_add(new_slave);
1902 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
1903 goto err_upper_unlink;
1906 /* If the mode uses primary, then the following is handled by
1907 * bond_change_active_slave().
1909 if (!bond_uses_primary(bond)) {
1910 /* set promiscuity level to new slave */
1911 if (bond_dev->flags & IFF_PROMISC) {
1912 res = dev_set_promiscuity(slave_dev, 1);
1917 /* set allmulti level to new slave */
1918 if (bond_dev->flags & IFF_ALLMULTI) {
1919 res = dev_set_allmulti(slave_dev, 1);
1921 if (bond_dev->flags & IFF_PROMISC)
1922 dev_set_promiscuity(slave_dev, -1);
1927 netif_addr_lock_bh(bond_dev);
1928 dev_mc_sync_multiple(slave_dev, bond_dev);
1929 dev_uc_sync_multiple(slave_dev, bond_dev);
1930 netif_addr_unlock_bh(bond_dev);
1932 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1933 /* add lacpdu mc addr to mc list */
1934 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1936 dev_mc_add(slave_dev, lacpdu_multicast);
1941 bond_compute_features(bond);
1942 bond_set_carrier(bond);
1944 if (bond_uses_primary(bond)) {
1946 bond_select_active_slave(bond);
1947 unblock_netpoll_tx();
1950 if (bond_mode_can_use_xmit_hash(bond))
1951 bond_update_slave_arr(bond, NULL);
1954 slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
1955 bond_is_active_slave(new_slave) ? "an active" : "a backup",
1956 new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
1958 /* enslave is successful */
1959 bond_queue_slave_event(new_slave);
1962 /* Undo stages on error */
1964 bond_sysfs_slave_del(new_slave);
1967 bond_upper_dev_unlink(bond, new_slave);
1970 netdev_rx_handler_unregister(slave_dev);
1973 vlan_vids_del_by_dev(slave_dev, bond_dev);
1974 if (rcu_access_pointer(bond->primary_slave) == new_slave)
1975 RCU_INIT_POINTER(bond->primary_slave, NULL);
1976 if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
1978 bond_change_active_slave(bond, NULL);
1979 bond_select_active_slave(bond);
1980 unblock_netpoll_tx();
1982 /* either primary_slave or curr_active_slave might've changed */
1984 slave_disable_netpoll(new_slave);
1987 if (!netif_is_bond_master(slave_dev))
1988 slave_dev->priv_flags &= ~IFF_BONDING;
1989 dev_close(slave_dev);
1992 slave_dev->flags &= ~IFF_SLAVE;
1993 if (!bond->params.fail_over_mac ||
1994 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
1995 /* XXX TODO - fom follow mode needs to change master's
1996 * MAC if this slave's MAC is in use by the bond, or at
1997 * least print a warning.
1999 bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
2000 new_slave->dev->addr_len);
2001 ss.ss_family = slave_dev->type;
2002 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
2006 dev_set_mtu(slave_dev, new_slave->original_mtu);
2009 bond_free_slave(new_slave);
2012 /* Enslave of first slave has failed and we need to fix master's mac */
2013 if (!bond_has_slaves(bond)) {
2014 if (ether_addr_equal_64bits(bond_dev->dev_addr,
2015 slave_dev->dev_addr))
2016 eth_hw_addr_random(bond_dev);
2017 if (bond_dev->type != ARPHRD_ETHER) {
2018 dev_close(bond_dev);
2019 ether_setup(bond_dev);
2020 bond_dev->flags |= IFF_MASTER;
2021 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2028 /* Try to release the slave device <slave> from the bond device <master>
2029 * It is legal to access curr_active_slave without a lock because all the function
2030 * is RTNL-locked. If "all" is true it means that the function is being called
2031 * while destroying a bond interface and all slaves are being released.
2033 * The rules for slave state should be:
2034 * for Active/Backup:
2035 * Active stays on all backups go down
2036 * for Bonded connections:
2037 * The first up interface should be left on and all others downed.
2039 static int __bond_release_one(struct net_device *bond_dev,
2040 struct net_device *slave_dev,
2041 bool all, bool unregister)
2043 struct bonding *bond = netdev_priv(bond_dev);
2044 struct slave *slave, *oldcurrent;
2045 struct sockaddr_storage ss;
2046 int old_flags = bond_dev->flags;
2047 netdev_features_t old_features = bond_dev->features;
2049 /* slave is not a slave or master is not master of this slave */
2050 if (!(slave_dev->flags & IFF_SLAVE) ||
2051 !netdev_has_upper_dev(slave_dev, bond_dev)) {
2052 slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
2058 slave = bond_get_slave_by_dev(bond, slave_dev);
2060 /* not a slave of this bond */
2061 slave_info(bond_dev, slave_dev, "interface not enslaved\n");
2062 unblock_netpoll_tx();
2066 bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
2068 bond_sysfs_slave_del(slave);
2070 /* recompute stats just before removing the slave */
2071 bond_get_stats(bond->dev, &bond->bond_stats);
2073 bond_upper_dev_unlink(bond, slave);
2074 /* unregister rx_handler early so bond_handle_frame wouldn't be called
2075 * for this slave anymore.
2077 netdev_rx_handler_unregister(slave_dev);
2079 if (BOND_MODE(bond) == BOND_MODE_8023AD)
2080 bond_3ad_unbind_slave(slave);
2082 if (bond_mode_can_use_xmit_hash(bond))
2083 bond_update_slave_arr(bond, slave);
2085 slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
2086 bond_is_active_slave(slave) ? "active" : "backup");
2088 oldcurrent = rcu_access_pointer(bond->curr_active_slave);
2090 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
2092 if (!all && (!bond->params.fail_over_mac ||
2093 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
2094 if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
2095 bond_has_slaves(bond))
2096 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",
2097 slave->perm_hwaddr);
2100 if (rtnl_dereference(bond->primary_slave) == slave)
2101 RCU_INIT_POINTER(bond->primary_slave, NULL);
2103 if (oldcurrent == slave)
2104 bond_change_active_slave(bond, NULL);
2106 if (bond_is_lb(bond)) {
2107 /* Must be called only after the slave has been
2108 * detached from the list and the curr_active_slave
2109 * has been cleared (if our_slave == old_current),
2110 * but before a new active slave is selected.
2112 bond_alb_deinit_slave(bond, slave);
2116 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
2117 } else if (oldcurrent == slave) {
2118 /* Note that we hold RTNL over this sequence, so there
2119 * is no concern that another slave add/remove event
2122 bond_select_active_slave(bond);
2125 if (!bond_has_slaves(bond)) {
2126 bond_set_carrier(bond);
2127 eth_hw_addr_random(bond_dev);
2130 unblock_netpoll_tx();
2134 if (!bond_has_slaves(bond)) {
2135 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
2136 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
2139 bond_compute_features(bond);
2140 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2141 (old_features & NETIF_F_VLAN_CHALLENGED))
2142 slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
2144 vlan_vids_del_by_dev(slave_dev, bond_dev);
2146 /* If the mode uses primary, then this case was handled above by
2147 * bond_change_active_slave(..., NULL)
2149 if (!bond_uses_primary(bond)) {
2150 /* unset promiscuity level from slave
2151 * NOTE: The NETDEV_CHANGEADDR call above may change the value
2152 * of the IFF_PROMISC flag in the bond_dev, but we need the
2153 * value of that flag before that change, as that was the value
2154 * when this slave was attached, so we cache at the start of the
2155 * function and use it here. Same goes for ALLMULTI below
2157 if (old_flags & IFF_PROMISC)
2158 dev_set_promiscuity(slave_dev, -1);
2160 /* unset allmulti level from slave */
2161 if (old_flags & IFF_ALLMULTI)
2162 dev_set_allmulti(slave_dev, -1);
2164 bond_hw_addr_flush(bond_dev, slave_dev);
2167 slave_disable_netpoll(slave);
2169 /* close slave before restoring its mac address */
2170 dev_close(slave_dev);
2172 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
2173 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2174 /* restore original ("permanent") mac address */
2175 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2176 slave->dev->addr_len);
2177 ss.ss_family = slave_dev->type;
2178 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
2182 __dev_set_mtu(slave_dev, slave->original_mtu);
2184 dev_set_mtu(slave_dev, slave->original_mtu);
2186 if (!netif_is_bond_master(slave_dev))
2187 slave_dev->priv_flags &= ~IFF_BONDING;
2189 bond_free_slave(slave);
2194 /* A wrapper used because of ndo_del_link */
2195 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2197 return __bond_release_one(bond_dev, slave_dev, false, false);
2200 /* First release a slave and then destroy the bond if no more slaves are left.
2201 * Must be under rtnl_lock when this function is called.
2203 static int bond_release_and_destroy(struct net_device *bond_dev,
2204 struct net_device *slave_dev)
2206 struct bonding *bond = netdev_priv(bond_dev);
2209 ret = __bond_release_one(bond_dev, slave_dev, false, true);
2210 if (ret == 0 && !bond_has_slaves(bond) &&
2211 bond_dev->reg_state != NETREG_UNREGISTERING) {
2212 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2213 netdev_info(bond_dev, "Destroying bond\n");
2214 bond_remove_proc_entry(bond);
2215 unregister_netdevice(bond_dev);
2220 static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2222 struct bonding *bond = netdev_priv(bond_dev);
2223 bond_fill_ifbond(bond, info);
2226 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2228 struct bonding *bond = netdev_priv(bond_dev);
2229 struct list_head *iter;
2230 int i = 0, res = -ENODEV;
2231 struct slave *slave;
2233 bond_for_each_slave(bond, slave, iter) {
2234 if (i++ == (int)info->slave_id) {
2236 bond_fill_ifslave(slave, info);
2244 /*-------------------------------- Monitoring -------------------------------*/
2246 /* called with rcu_read_lock() */
2247 static int bond_miimon_inspect(struct bonding *bond)
2249 int link_state, commit = 0;
2250 struct list_head *iter;
2251 struct slave *slave;
2252 bool ignore_updelay;
2254 ignore_updelay = !rcu_dereference(bond->curr_active_slave);
2256 bond_for_each_slave_rcu(bond, slave, iter) {
2257 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2259 link_state = bond_check_dev_link(bond, slave->dev, 0);
2261 switch (slave->link) {
2266 bond_propose_link_state(slave, BOND_LINK_FAIL);
2268 slave->delay = bond->params.downdelay;
2270 slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
2272 BOND_MODE_ACTIVEBACKUP) ?
2273 (bond_is_active_slave(slave) ?
2274 "active " : "backup ") : "",
2275 bond->params.downdelay * bond->params.miimon);
2278 case BOND_LINK_FAIL:
2280 /* recovered before downdelay expired */
2281 bond_propose_link_state(slave, BOND_LINK_UP);
2282 slave->last_link_up = jiffies;
2283 slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2284 (bond->params.downdelay - slave->delay) *
2285 bond->params.miimon);
2290 if (slave->delay <= 0) {
2291 bond_propose_link_state(slave, BOND_LINK_DOWN);
2299 case BOND_LINK_DOWN:
2303 bond_propose_link_state(slave, BOND_LINK_BACK);
2305 slave->delay = bond->params.updelay;
2308 slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
2309 ignore_updelay ? 0 :
2310 bond->params.updelay *
2311 bond->params.miimon);
2314 case BOND_LINK_BACK:
2316 bond_propose_link_state(slave, BOND_LINK_DOWN);
2317 slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2318 (bond->params.updelay - slave->delay) *
2319 bond->params.miimon);
2327 if (slave->delay <= 0) {
2328 bond_propose_link_state(slave, BOND_LINK_UP);
2330 ignore_updelay = false;
2342 static void bond_miimon_link_change(struct bonding *bond,
2343 struct slave *slave,
2346 switch (BOND_MODE(bond)) {
2347 case BOND_MODE_8023AD:
2348 bond_3ad_handle_link_change(slave, link);
2352 bond_alb_handle_link_change(bond, slave, link);
2355 bond_update_slave_arr(bond, NULL);
2360 static void bond_miimon_commit(struct bonding *bond)
2362 struct list_head *iter;
2363 struct slave *slave, *primary;
2365 bond_for_each_slave(bond, slave, iter) {
2366 switch (slave->link_new_state) {
2367 case BOND_LINK_NOCHANGE:
2368 /* For 802.3ad mode, check current slave speed and
2369 * duplex again in case its port was disabled after
2370 * invalid speed/duplex reporting but recovered before
2371 * link monitoring could make a decision on the actual
2374 if (BOND_MODE(bond) == BOND_MODE_8023AD &&
2375 slave->link == BOND_LINK_UP)
2376 bond_3ad_adapter_speed_duplex_changed(slave);
2380 if (bond_update_speed_duplex(slave) &&
2381 bond_needs_speed_duplex(bond)) {
2382 slave->link = BOND_LINK_DOWN;
2383 if (net_ratelimit())
2384 slave_warn(bond->dev, slave->dev,
2385 "failed to get link speed/duplex\n");
2388 bond_set_slave_link_state(slave, BOND_LINK_UP,
2389 BOND_SLAVE_NOTIFY_NOW);
2390 slave->last_link_up = jiffies;
2392 primary = rtnl_dereference(bond->primary_slave);
2393 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
2394 /* prevent it from being the active one */
2395 bond_set_backup_slave(slave);
2396 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2397 /* make it immediately active */
2398 bond_set_active_slave(slave);
2401 slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2402 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2403 slave->duplex ? "full" : "half");
2405 bond_miimon_link_change(bond, slave, BOND_LINK_UP);
2407 if (!bond->curr_active_slave || slave == primary)
2412 case BOND_LINK_DOWN:
2413 if (slave->link_failure_count < UINT_MAX)
2414 slave->link_failure_count++;
2416 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
2417 BOND_SLAVE_NOTIFY_NOW);
2419 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
2420 BOND_MODE(bond) == BOND_MODE_8023AD)
2421 bond_set_slave_inactive_flags(slave,
2422 BOND_SLAVE_NOTIFY_NOW);
2424 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
2426 bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
2428 if (slave == rcu_access_pointer(bond->curr_active_slave))
2434 slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
2435 slave->link_new_state);
2436 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2443 bond_select_active_slave(bond);
2444 unblock_netpoll_tx();
2447 bond_set_carrier(bond);
2452 * Really a wrapper that splits the mii monitor into two phases: an
2453 * inspection, then (if inspection indicates something needs to be done)
2454 * an acquisition of appropriate locks followed by a commit phase to
2455 * implement whatever link state changes are indicated.
2457 static void bond_mii_monitor(struct work_struct *work)
2459 struct bonding *bond = container_of(work, struct bonding,
2461 bool should_notify_peers = false;
2463 unsigned long delay;
2464 struct slave *slave;
2465 struct list_head *iter;
2467 delay = msecs_to_jiffies(bond->params.miimon);
2469 if (!bond_has_slaves(bond))
2473 should_notify_peers = bond_should_notify_peers(bond);
2474 commit = !!bond_miimon_inspect(bond);
2475 if (bond->send_peer_notif) {
2477 if (rtnl_trylock()) {
2478 bond->send_peer_notif--;
2486 /* Race avoidance with bond_close cancel of workqueue */
2487 if (!rtnl_trylock()) {
2489 should_notify_peers = false;
2493 bond_for_each_slave(bond, slave, iter) {
2494 bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER);
2496 bond_miimon_commit(bond);
2498 rtnl_unlock(); /* might sleep, hold no other locks */
2502 if (bond->params.miimon)
2503 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2505 if (should_notify_peers) {
2506 if (!rtnl_trylock())
2508 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
2513 static int bond_upper_dev_walk(struct net_device *upper, void *data)
2515 __be32 ip = *((__be32 *)data);
2517 return ip == bond_confirm_addr(upper, 0, ip);
2520 static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2524 if (ip == bond_confirm_addr(bond->dev, 0, ip))
2528 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &ip))
2535 /* We go to the (large) trouble of VLAN tagging ARP frames because
2536 * switches in VLAN mode (especially if ports are configured as
2537 * "native" to a VLAN) might not pass non-tagged frames.
2539 static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
2540 __be32 src_ip, struct bond_vlan_tag *tags)
2542 struct sk_buff *skb;
2543 struct bond_vlan_tag *outer_tag = tags;
2544 struct net_device *slave_dev = slave->dev;
2545 struct net_device *bond_dev = slave->bond->dev;
2547 slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
2548 arp_op, &dest_ip, &src_ip);
2550 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2551 NULL, slave_dev->dev_addr, NULL);
2554 net_err_ratelimited("ARP packet allocation failed\n");
2558 if (!tags || tags->vlan_proto == VLAN_N_VID)
2563 /* Go through all the tags backwards and add them to the packet */
2564 while (tags->vlan_proto != VLAN_N_VID) {
2565 if (!tags->vlan_id) {
2570 slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
2571 ntohs(outer_tag->vlan_proto), tags->vlan_id);
2572 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
2575 net_err_ratelimited("failed to insert inner VLAN tag\n");
2581 /* Set the outer tag */
2582 if (outer_tag->vlan_id) {
2583 slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
2584 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
2585 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
2586 outer_tag->vlan_id);
2593 /* Validate the device path between the @start_dev and the @end_dev.
2594 * The path is valid if the @end_dev is reachable through device
2596 * When the path is validated, collect any vlan information in the
2599 struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
2600 struct net_device *end_dev,
2603 struct bond_vlan_tag *tags;
2604 struct net_device *upper;
2605 struct list_head *iter;
2607 if (start_dev == end_dev) {
2608 tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
2610 return ERR_PTR(-ENOMEM);
2611 tags[level].vlan_proto = VLAN_N_VID;
2615 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
2616 tags = bond_verify_device_path(upper, end_dev, level + 1);
2617 if (IS_ERR_OR_NULL(tags)) {
2622 if (is_vlan_dev(upper)) {
2623 tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
2624 tags[level].vlan_id = vlan_dev_vlan_id(upper);
2633 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2636 struct bond_vlan_tag *tags;
2637 __be32 *targets = bond->params.arp_targets, addr;
2640 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
2641 slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
2642 __func__, &targets[i]);
2645 /* Find out through which dev should the packet go */
2646 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2649 /* there's no route to target - try to send arp
2650 * probe to generate any traffic (arp_validate=0)
2652 if (bond->params.arp_validate)
2653 net_warn_ratelimited("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
2656 bond_arp_send(slave, ARPOP_REQUEST, targets[i],
2661 /* bond device itself */
2662 if (rt->dst.dev == bond->dev)
2666 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
2669 if (!IS_ERR_OR_NULL(tags))
2672 /* Not our device - skip */
2673 slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
2674 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
2680 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
2682 bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
2687 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2691 if (!sip || !bond_has_this_ip(bond, tip)) {
2692 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
2693 __func__, &sip, &tip);
2697 i = bond_get_targets_ip(bond->params.arp_targets, sip);
2699 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
2703 slave->last_rx = jiffies;
2704 slave->target_last_arp_rx[i] = jiffies;
2707 int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
2708 struct slave *slave)
2710 struct arphdr *arp = (struct arphdr *)skb->data;
2711 struct slave *curr_active_slave, *curr_arp_slave;
2712 unsigned char *arp_ptr;
2714 int is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
2717 if (!slave_do_arp_validate(bond, slave)) {
2718 if ((slave_do_arp_validate_only(bond) && is_arp) ||
2719 !slave_do_arp_validate_only(bond))
2720 slave->last_rx = jiffies;
2721 return RX_HANDLER_ANOTHER;
2722 } else if (!is_arp) {
2723 return RX_HANDLER_ANOTHER;
2726 alen = arp_hdr_len(bond->dev);
2728 slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
2729 __func__, skb->dev->name);
2731 if (alen > skb_headlen(skb)) {
2732 arp = kmalloc(alen, GFP_ATOMIC);
2735 if (skb_copy_bits(skb, 0, arp, alen) < 0)
2739 if (arp->ar_hln != bond->dev->addr_len ||
2740 skb->pkt_type == PACKET_OTHERHOST ||
2741 skb->pkt_type == PACKET_LOOPBACK ||
2742 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2743 arp->ar_pro != htons(ETH_P_IP) ||
2747 arp_ptr = (unsigned char *)(arp + 1);
2748 arp_ptr += bond->dev->addr_len;
2749 memcpy(&sip, arp_ptr, 4);
2750 arp_ptr += 4 + bond->dev->addr_len;
2751 memcpy(&tip, arp_ptr, 4);
2753 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2754 __func__, slave->dev->name, bond_slave_state(slave),
2755 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2758 curr_active_slave = rcu_dereference(bond->curr_active_slave);
2759 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
2761 /* We 'trust' the received ARP enough to validate it if:
2763 * (a) the slave receiving the ARP is active (which includes the
2764 * current ARP slave, if any), or
2766 * (b) the receiving slave isn't active, but there is a currently
2767 * active slave and it received valid arp reply(s) after it became
2768 * the currently active slave, or
2770 * (c) there is an ARP slave that sent an ARP during the prior ARP
2771 * interval, and we receive an ARP reply on any slave. We accept
2772 * these because switch FDB update delays may deliver the ARP
2773 * reply to a slave other than the sender of the ARP request.
2775 * Note: for (b), backup slaves are receiving the broadcast ARP
2776 * request, not a reply. This request passes from the sending
2777 * slave through the L2 switch(es) to the receiving slave. Since
2778 * this is checking the request, sip/tip are swapped for
2781 * This is done to avoid endless looping when we can't reach the
2782 * arp_ip_target and fool ourselves with our own arp requests.
2784 if (bond_is_active_slave(slave))
2785 bond_validate_arp(bond, slave, sip, tip);
2786 else if (curr_active_slave &&
2787 time_after(slave_last_rx(bond, curr_active_slave),
2788 curr_active_slave->last_link_up))
2789 bond_validate_arp(bond, slave, tip, sip);
2790 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
2791 bond_time_in_interval(bond,
2792 dev_trans_start(curr_arp_slave->dev), 1))
2793 bond_validate_arp(bond, slave, sip, tip);
2796 if (arp != (struct arphdr *)skb->data)
2798 return RX_HANDLER_ANOTHER;
2801 /* function to verify if we're in the arp_interval timeslice, returns true if
2802 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
2803 * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
2805 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
2808 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2810 return time_in_range(jiffies,
2811 last_act - delta_in_ticks,
2812 last_act + mod * delta_in_ticks + delta_in_ticks/2);
2815 /* This function is called regularly to monitor each slave's link
2816 * ensuring that traffic is being sent and received when arp monitoring
2817 * is used in load-balancing mode. if the adapter has been dormant, then an
2818 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2819 * arp monitoring in active backup mode.
2821 static void bond_loadbalance_arp_mon(struct bonding *bond)
2823 struct slave *slave, *oldcurrent;
2824 struct list_head *iter;
2825 int do_failover = 0, slave_state_changed = 0;
2827 if (!bond_has_slaves(bond))
2832 oldcurrent = rcu_dereference(bond->curr_active_slave);
2833 /* see if any of the previous devices are up now (i.e. they have
2834 * xmt and rcv traffic). the curr_active_slave does not come into
2835 * the picture unless it is null. also, slave->last_link_up is not
2836 * needed here because we send an arp on each slave and give a slave
2837 * as long as it needs to get the tx/rx within the delta.
2838 * TODO: what about up/down delay in arp mode? it wasn't here before
2841 bond_for_each_slave_rcu(bond, slave, iter) {
2842 unsigned long trans_start = dev_trans_start(slave->dev);
2844 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2846 if (slave->link != BOND_LINK_UP) {
2847 if (bond_time_in_interval(bond, trans_start, 1) &&
2848 bond_time_in_interval(bond, slave->last_rx, 1)) {
2850 bond_propose_link_state(slave, BOND_LINK_UP);
2851 slave_state_changed = 1;
2853 /* primary_slave has no meaning in round-robin
2854 * mode. the window of a slave being up and
2855 * curr_active_slave being null after enslaving
2859 slave_info(bond->dev, slave->dev, "link status definitely up\n");
2862 slave_info(bond->dev, slave->dev, "interface is now up\n");
2866 /* slave->link == BOND_LINK_UP */
2868 /* not all switches will respond to an arp request
2869 * when the source ip is 0, so don't take the link down
2870 * if we don't know our ip yet
2872 if (!bond_time_in_interval(bond, trans_start, 2) ||
2873 !bond_time_in_interval(bond, slave->last_rx, 2)) {
2875 bond_propose_link_state(slave, BOND_LINK_DOWN);
2876 slave_state_changed = 1;
2878 if (slave->link_failure_count < UINT_MAX)
2879 slave->link_failure_count++;
2881 slave_info(bond->dev, slave->dev, "interface is now down\n");
2883 if (slave == oldcurrent)
2888 /* note: if switch is in round-robin mode, all links
2889 * must tx arp to ensure all links rx an arp - otherwise
2890 * links may oscillate or not come up at all; if switch is
2891 * in something like xor mode, there is nothing we can
2892 * do - all replies will be rx'ed on same link causing slaves
2893 * to be unstable during low/no traffic periods
2895 if (bond_slave_is_up(slave))
2896 bond_arp_send_all(bond, slave);
2901 if (do_failover || slave_state_changed) {
2902 if (!rtnl_trylock())
2905 bond_for_each_slave(bond, slave, iter) {
2906 if (slave->link_new_state != BOND_LINK_NOCHANGE)
2907 slave->link = slave->link_new_state;
2910 if (slave_state_changed) {
2911 bond_slave_state_change(bond);
2912 if (BOND_MODE(bond) == BOND_MODE_XOR)
2913 bond_update_slave_arr(bond, NULL);
2917 bond_select_active_slave(bond);
2918 unblock_netpoll_tx();
2924 if (bond->params.arp_interval)
2925 queue_delayed_work(bond->wq, &bond->arp_work,
2926 msecs_to_jiffies(bond->params.arp_interval));
2929 /* Called to inspect slaves for active-backup mode ARP monitor link state
2930 * changes. Sets proposed link state in slaves to specify what action
2931 * should take place for the slave. Returns 0 if no changes are found, >0
2932 * if changes to link states must be committed.
2934 * Called with rcu_read_lock held.
2936 static int bond_ab_arp_inspect(struct bonding *bond)
2938 unsigned long trans_start, last_rx;
2939 struct list_head *iter;
2940 struct slave *slave;
2943 bond_for_each_slave_rcu(bond, slave, iter) {
2944 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
2945 last_rx = slave_last_rx(bond, slave);
2947 if (slave->link != BOND_LINK_UP) {
2948 if (bond_time_in_interval(bond, last_rx, 1)) {
2949 bond_propose_link_state(slave, BOND_LINK_UP);
2951 } else if (slave->link == BOND_LINK_BACK) {
2952 bond_propose_link_state(slave, BOND_LINK_FAIL);
2958 /* Give slaves 2*delta after being enslaved or made
2959 * active. This avoids bouncing, as the last receive
2960 * times need a full ARP monitor cycle to be updated.
2962 if (bond_time_in_interval(bond, slave->last_link_up, 2))
2965 /* Backup slave is down if:
2966 * - No current_arp_slave AND
2967 * - more than 3*delta since last receive AND
2968 * - the bond has an IP address
2970 * Note: a non-null current_arp_slave indicates
2971 * the curr_active_slave went down and we are
2972 * searching for a new one; under this condition
2973 * we only take the curr_active_slave down - this
2974 * gives each slave a chance to tx/rx traffic
2975 * before being taken out
2977 if (!bond_is_active_slave(slave) &&
2978 !rcu_access_pointer(bond->current_arp_slave) &&
2979 !bond_time_in_interval(bond, last_rx, 3)) {
2980 bond_propose_link_state(slave, BOND_LINK_DOWN);
2984 /* Active slave is down if:
2985 * - more than 2*delta since transmitting OR
2986 * - (more than 2*delta since receive AND
2987 * the bond has an IP address)
2989 trans_start = dev_trans_start(slave->dev);
2990 if (bond_is_active_slave(slave) &&
2991 (!bond_time_in_interval(bond, trans_start, 2) ||
2992 !bond_time_in_interval(bond, last_rx, 2))) {
2993 bond_propose_link_state(slave, BOND_LINK_DOWN);
3001 /* Called to commit link state changes noted by inspection step of
3002 * active-backup mode ARP monitor.
3004 * Called with RTNL hold.
3006 static void bond_ab_arp_commit(struct bonding *bond)
3008 unsigned long trans_start;
3009 struct list_head *iter;
3010 struct slave *slave;
3012 bond_for_each_slave(bond, slave, iter) {
3013 switch (slave->link_new_state) {
3014 case BOND_LINK_NOCHANGE:
3018 trans_start = dev_trans_start(slave->dev);
3019 if (rtnl_dereference(bond->curr_active_slave) != slave ||
3020 (!rtnl_dereference(bond->curr_active_slave) &&
3021 bond_time_in_interval(bond, trans_start, 1))) {
3022 struct slave *current_arp_slave;
3024 current_arp_slave = rtnl_dereference(bond->current_arp_slave);
3025 bond_set_slave_link_state(slave, BOND_LINK_UP,
3026 BOND_SLAVE_NOTIFY_NOW);
3027 if (current_arp_slave) {
3028 bond_set_slave_inactive_flags(
3030 BOND_SLAVE_NOTIFY_NOW);
3031 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3034 slave_info(bond->dev, slave->dev, "link status definitely up\n");
3036 if (!rtnl_dereference(bond->curr_active_slave) ||
3037 slave == rtnl_dereference(bond->primary_slave))
3044 case BOND_LINK_DOWN:
3045 if (slave->link_failure_count < UINT_MAX)
3046 slave->link_failure_count++;
3048 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3049 BOND_SLAVE_NOTIFY_NOW);
3050 bond_set_slave_inactive_flags(slave,
3051 BOND_SLAVE_NOTIFY_NOW);
3053 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
3055 if (slave == rtnl_dereference(bond->curr_active_slave)) {
3056 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3062 case BOND_LINK_FAIL:
3063 bond_set_slave_link_state(slave, BOND_LINK_FAIL,
3064 BOND_SLAVE_NOTIFY_NOW);
3065 bond_set_slave_inactive_flags(slave,
3066 BOND_SLAVE_NOTIFY_NOW);
3068 /* A slave has just been enslaved and has become
3069 * the current active slave.
3071 if (rtnl_dereference(bond->curr_active_slave))
3072 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3076 slave_err(bond->dev, slave->dev,
3077 "impossible: link_new_state %d on slave\n",
3078 slave->link_new_state);
3084 bond_select_active_slave(bond);
3085 unblock_netpoll_tx();
3088 bond_set_carrier(bond);
3091 /* Send ARP probes for active-backup mode ARP monitor.
3093 * Called with rcu_read_lock held.
3095 static bool bond_ab_arp_probe(struct bonding *bond)
3097 struct slave *slave, *before = NULL, *new_slave = NULL,
3098 *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3099 *curr_active_slave = rcu_dereference(bond->curr_active_slave);
3100 struct list_head *iter;
3102 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
3104 if (curr_arp_slave && curr_active_slave)
3105 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3106 curr_arp_slave->dev->name,
3107 curr_active_slave->dev->name);
3109 if (curr_active_slave) {
3110 bond_arp_send_all(bond, curr_active_slave);
3111 return should_notify_rtnl;
3114 /* if we don't have a curr_active_slave, search for the next available
3115 * backup slave from the current_arp_slave and make it the candidate
3116 * for becoming the curr_active_slave
3119 if (!curr_arp_slave) {
3120 curr_arp_slave = bond_first_slave_rcu(bond);
3121 if (!curr_arp_slave)
3122 return should_notify_rtnl;
3125 bond_for_each_slave_rcu(bond, slave, iter) {
3126 if (!found && !before && bond_slave_is_up(slave))
3129 if (found && !new_slave && bond_slave_is_up(slave))
3131 /* if the link state is up at this point, we
3132 * mark it down - this can happen if we have
3133 * simultaneous link failures and
3134 * reselect_active_interface doesn't make this
3135 * one the current slave so it is still marked
3136 * up when it is actually down
3138 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
3139 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3140 BOND_SLAVE_NOTIFY_LATER);
3141 if (slave->link_failure_count < UINT_MAX)
3142 slave->link_failure_count++;
3144 bond_set_slave_inactive_flags(slave,
3145 BOND_SLAVE_NOTIFY_LATER);
3147 slave_info(bond->dev, slave->dev, "backup interface is now down\n");
3149 if (slave == curr_arp_slave)
3153 if (!new_slave && before)
3159 bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
3160 BOND_SLAVE_NOTIFY_LATER);
3161 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
3162 bond_arp_send_all(bond, new_slave);
3163 new_slave->last_link_up = jiffies;
3164 rcu_assign_pointer(bond->current_arp_slave, new_slave);
3167 bond_for_each_slave_rcu(bond, slave, iter) {
3168 if (slave->should_notify || slave->should_notify_link) {
3169 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3173 return should_notify_rtnl;
3176 static void bond_activebackup_arp_mon(struct bonding *bond)
3178 bool should_notify_peers = false;
3179 bool should_notify_rtnl = false;
3182 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3184 if (!bond_has_slaves(bond))
3189 should_notify_peers = bond_should_notify_peers(bond);
3191 if (bond_ab_arp_inspect(bond)) {
3194 /* Race avoidance with bond_close flush of workqueue */
3195 if (!rtnl_trylock()) {
3197 should_notify_peers = false;
3201 bond_ab_arp_commit(bond);
3207 should_notify_rtnl = bond_ab_arp_probe(bond);
3211 if (bond->params.arp_interval)
3212 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3214 if (should_notify_peers || should_notify_rtnl) {
3215 if (!rtnl_trylock())
3218 if (should_notify_peers)
3219 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
3221 if (should_notify_rtnl) {
3222 bond_slave_state_notify(bond);
3223 bond_slave_link_notify(bond);
3230 static void bond_arp_monitor(struct work_struct *work)
3232 struct bonding *bond = container_of(work, struct bonding,
3235 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3236 bond_activebackup_arp_mon(bond);
3238 bond_loadbalance_arp_mon(bond);
3241 /*-------------------------- netdev event handling --------------------------*/
3243 /* Change device name */
3244 static int bond_event_changename(struct bonding *bond)
3246 bond_remove_proc_entry(bond);
3247 bond_create_proc_entry(bond);
3249 bond_debug_reregister(bond);
3254 static int bond_master_netdev_event(unsigned long event,
3255 struct net_device *bond_dev)
3257 struct bonding *event_bond = netdev_priv(bond_dev);
3259 netdev_dbg(bond_dev, "%s called\n", __func__);
3262 case NETDEV_CHANGENAME:
3263 return bond_event_changename(event_bond);
3264 case NETDEV_UNREGISTER:
3265 bond_remove_proc_entry(event_bond);
3267 case NETDEV_REGISTER:
3268 bond_create_proc_entry(event_bond);
3277 static int bond_slave_netdev_event(unsigned long event,
3278 struct net_device *slave_dev)
3280 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
3281 struct bonding *bond;
3282 struct net_device *bond_dev;
3284 /* A netdev event can be generated while enslaving a device
3285 * before netdev_rx_handler_register is called in which case
3286 * slave will be NULL
3289 netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
3293 bond_dev = slave->bond->dev;
3295 primary = rtnl_dereference(bond->primary_slave);
3297 slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3300 case NETDEV_UNREGISTER:
3301 if (bond_dev->type != ARPHRD_ETHER)
3302 bond_release_and_destroy(bond_dev, slave_dev);
3304 __bond_release_one(bond_dev, slave_dev, false, true);
3308 /* For 802.3ad mode only:
3309 * Getting invalid Speed/Duplex values here will put slave
3310 * in weird state. Mark it as link-fail if the link was
3311 * previously up or link-down if it hasn't yet come up, and
3312 * let link-monitoring (miimon) set it right when correct
3313 * speeds/duplex are available.
3315 if (bond_update_speed_duplex(slave) &&
3316 BOND_MODE(bond) == BOND_MODE_8023AD) {
3317 if (slave->last_link_up)
3318 slave->link = BOND_LINK_FAIL;
3320 slave->link = BOND_LINK_DOWN;
3323 if (BOND_MODE(bond) == BOND_MODE_8023AD)
3324 bond_3ad_adapter_speed_duplex_changed(slave);
3327 /* Refresh slave-array if applicable!
3328 * If the setup does not use miimon or arpmon (mode-specific!),
3329 * then these events will not cause the slave-array to be
3330 * refreshed. This will cause xmit to use a slave that is not
3331 * usable. Avoid such situation by refeshing the array at these
3332 * events. If these (miimon/arpmon) parameters are configured
3333 * then array gets refreshed twice and that should be fine!
3335 if (bond_mode_can_use_xmit_hash(bond))
3336 bond_update_slave_arr(bond, NULL);
3338 case NETDEV_CHANGEMTU:
3339 /* TODO: Should slaves be allowed to
3340 * independently alter their MTU? For
3341 * an active-backup bond, slaves need
3342 * not be the same type of device, so
3343 * MTUs may vary. For other modes,
3344 * slaves arguably should have the
3345 * same MTUs. To do this, we'd need to
3346 * take over the slave's change_mtu
3347 * function for the duration of their
3351 case NETDEV_CHANGENAME:
3352 /* we don't care if we don't have primary set */
3353 if (!bond_uses_primary(bond) ||
3354 !bond->params.primary[0])
3357 if (slave == primary) {
3358 /* slave's name changed - he's no longer primary */
3359 RCU_INIT_POINTER(bond->primary_slave, NULL);
3360 } else if (!strcmp(slave_dev->name, bond->params.primary)) {
3361 /* we have a new primary slave */
3362 rcu_assign_pointer(bond->primary_slave, slave);
3363 } else { /* we didn't change primary - exit */
3367 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
3368 primary ? slave_dev->name : "none");
3371 bond_select_active_slave(bond);
3372 unblock_netpoll_tx();
3374 case NETDEV_FEAT_CHANGE:
3375 bond_compute_features(bond);
3377 case NETDEV_RESEND_IGMP:
3378 /* Propagate to master device */
3379 call_netdevice_notifiers(event, slave->bond->dev);
3388 /* bond_netdev_event: handle netdev notifier chain events.
3390 * This function receives events for the netdev chain. The caller (an
3391 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3392 * locks for us to safely manipulate the slave devices (RTNL lock,
3395 static int bond_netdev_event(struct notifier_block *this,
3396 unsigned long event, void *ptr)
3398 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
3400 netdev_dbg(event_dev, "%s received %s\n",
3401 __func__, netdev_cmd_to_name(event));
3403 if (!(event_dev->priv_flags & IFF_BONDING))
3406 if (event_dev->flags & IFF_MASTER) {
3409 ret = bond_master_netdev_event(event, event_dev);
3410 if (ret != NOTIFY_DONE)
3414 if (event_dev->flags & IFF_SLAVE)
3415 return bond_slave_netdev_event(event, event_dev);
3420 static struct notifier_block bond_netdev_notifier = {
3421 .notifier_call = bond_netdev_event,
3424 /*---------------------------- Hashing Policies -----------------------------*/
3426 /* L2 hash helper */
3427 static inline u32 bond_eth_hash(struct sk_buff *skb)
3429 struct ethhdr *ep, hdr_tmp;
3431 ep = skb_header_pointer(skb, 0, sizeof(hdr_tmp), &hdr_tmp);
3433 return ep->h_dest[5] ^ ep->h_source[5] ^ ep->h_proto;
3437 static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk,
3438 int *noff, int *proto, bool l34)
3440 const struct ipv6hdr *iph6;
3441 const struct iphdr *iph;
3443 if (skb->protocol == htons(ETH_P_IP)) {
3444 if (unlikely(!pskb_may_pull(skb, *noff + sizeof(*iph))))
3446 iph = (const struct iphdr *)(skb->data + *noff);
3447 iph_to_flow_copy_v4addrs(fk, iph);
3448 *noff += iph->ihl << 2;
3449 if (!ip_is_fragment(iph))
3450 *proto = iph->protocol;
3451 } else if (skb->protocol == htons(ETH_P_IPV6)) {
3452 if (unlikely(!pskb_may_pull(skb, *noff + sizeof(*iph6))))
3454 iph6 = (const struct ipv6hdr *)(skb->data + *noff);
3455 iph_to_flow_copy_v6addrs(fk, iph6);
3456 *noff += sizeof(*iph6);
3457 *proto = iph6->nexthdr;
3462 if (l34 && *proto >= 0)
3463 fk->ports.ports = skb_flow_get_ports(skb, *noff, *proto);
3468 /* Extract the appropriate headers based on bond's xmit policy */
3469 static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb,
3470 struct flow_keys *fk)
3472 bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
3473 int noff, proto = -1;
3475 if (bond->params.xmit_policy > BOND_XMIT_POLICY_LAYER23) {
3476 memset(fk, 0, sizeof(*fk));
3477 return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
3478 fk, NULL, 0, 0, 0, 0);
3481 fk->ports.ports = 0;
3482 memset(&fk->icmp, 0, sizeof(fk->icmp));
3483 noff = skb_network_offset(skb);
3484 if (!bond_flow_ip(skb, fk, &noff, &proto, l34))
3487 /* ICMP error packets contains at least 8 bytes of the header
3488 * of the packet which generated the error. Use this information
3489 * to correlate ICMP error packets within the same flow which
3490 * generated the error.
3492 if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) {
3493 skb_flow_get_icmp_tci(skb, &fk->icmp, skb->data,
3494 skb_transport_offset(skb),
3496 if (proto == IPPROTO_ICMP) {
3497 if (!icmp_is_err(fk->icmp.type))
3500 noff += sizeof(struct icmphdr);
3501 } else if (proto == IPPROTO_ICMPV6) {
3502 if (!icmpv6_is_err(fk->icmp.type))
3505 noff += sizeof(struct icmp6hdr);
3507 return bond_flow_ip(skb, fk, &noff, &proto, l34);
3514 * bond_xmit_hash - generate a hash value based on the xmit policy
3515 * @bond: bonding device
3516 * @skb: buffer to use for headers
3518 * This function will extract the necessary headers from the skb buffer and use
3519 * them to generate a hash based on the xmit_policy set in the bonding device
3521 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
3523 struct flow_keys flow;
3526 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
3530 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
3531 !bond_flow_dissect(bond, skb, &flow))
3532 return bond_eth_hash(skb);
3534 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
3535 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
3536 hash = bond_eth_hash(skb);
3539 memcpy(&hash, &flow.icmp, sizeof(hash));
3541 memcpy(&hash, &flow.ports.ports, sizeof(hash));
3543 hash ^= (__force u32)flow_get_u32_dst(&flow) ^
3544 (__force u32)flow_get_u32_src(&flow);
3545 hash ^= (hash >> 16);
3546 hash ^= (hash >> 8);
3551 /*-------------------------- Device entry points ----------------------------*/
3553 void bond_work_init_all(struct bonding *bond)
3555 INIT_DELAYED_WORK(&bond->mcast_work,
3556 bond_resend_igmp_join_requests_delayed);
3557 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3558 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3559 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
3560 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3561 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
3564 static void bond_work_cancel_all(struct bonding *bond)
3566 cancel_delayed_work_sync(&bond->mii_work);
3567 cancel_delayed_work_sync(&bond->arp_work);
3568 cancel_delayed_work_sync(&bond->alb_work);
3569 cancel_delayed_work_sync(&bond->ad_work);
3570 cancel_delayed_work_sync(&bond->mcast_work);
3571 cancel_delayed_work_sync(&bond->slave_arr_work);
3574 static int bond_open(struct net_device *bond_dev)
3576 struct bonding *bond = netdev_priv(bond_dev);
3577 struct list_head *iter;
3578 struct slave *slave;
3580 /* reset slave->backup and slave->inactive */
3581 if (bond_has_slaves(bond)) {
3582 bond_for_each_slave(bond, slave, iter) {
3583 if (bond_uses_primary(bond) &&
3584 slave != rcu_access_pointer(bond->curr_active_slave)) {
3585 bond_set_slave_inactive_flags(slave,
3586 BOND_SLAVE_NOTIFY_NOW);
3587 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
3588 bond_set_slave_active_flags(slave,
3589 BOND_SLAVE_NOTIFY_NOW);
3594 if (bond_is_lb(bond)) {
3595 /* bond_alb_initialize must be called before the timer
3598 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
3600 if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
3601 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3604 if (bond->params.miimon) /* link check interval, in milliseconds. */
3605 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3607 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3608 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3609 bond->recv_probe = bond_arp_rcv;
3612 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
3613 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3614 /* register to receive LACPDUs */
3615 bond->recv_probe = bond_3ad_lacpdu_recv;
3616 bond_3ad_initiate_agg_selection(bond, 1);
3619 if (bond_mode_can_use_xmit_hash(bond))
3620 bond_update_slave_arr(bond, NULL);
3625 static int bond_close(struct net_device *bond_dev)
3627 struct bonding *bond = netdev_priv(bond_dev);
3629 bond_work_cancel_all(bond);
3630 bond->send_peer_notif = 0;
3631 if (bond_is_lb(bond))
3632 bond_alb_deinitialize(bond);
3633 bond->recv_probe = NULL;
3638 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
3639 * that some drivers can provide 32bit values only.
3641 static void bond_fold_stats(struct rtnl_link_stats64 *_res,
3642 const struct rtnl_link_stats64 *_new,
3643 const struct rtnl_link_stats64 *_old)
3645 const u64 *new = (const u64 *)_new;
3646 const u64 *old = (const u64 *)_old;
3647 u64 *res = (u64 *)_res;
3650 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
3653 s64 delta = nv - ov;
3655 /* detects if this particular field is 32bit only */
3656 if (((nv | ov) >> 32) == 0)
3657 delta = (s64)(s32)((u32)nv - (u32)ov);
3659 /* filter anomalies, some drivers reset their stats
3660 * at down/up events.
3667 #ifdef CONFIG_LOCKDEP
3668 static int bond_get_lowest_level_rcu(struct net_device *dev)
3670 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
3671 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
3672 int cur = 0, max = 0;
3675 iter = &dev->adj_list.lower;
3680 ldev = netdev_next_lower_dev_rcu(now, &iter);
3685 niter = &ldev->adj_list.lower;
3686 dev_stack[cur] = now;
3687 iter_stack[cur++] = iter;
3696 next = dev_stack[--cur];
3697 niter = iter_stack[cur];
3708 static void bond_get_stats(struct net_device *bond_dev,
3709 struct rtnl_link_stats64 *stats)
3711 struct bonding *bond = netdev_priv(bond_dev);
3712 struct rtnl_link_stats64 temp;
3713 struct list_head *iter;
3714 struct slave *slave;
3719 #ifdef CONFIG_LOCKDEP
3720 nest_level = bond_get_lowest_level_rcu(bond_dev);
3723 spin_lock_nested(&bond->stats_lock, nest_level);
3724 memcpy(stats, &bond->bond_stats, sizeof(*stats));
3726 bond_for_each_slave_rcu(bond, slave, iter) {
3727 const struct rtnl_link_stats64 *new =
3728 dev_get_stats(slave->dev, &temp);
3730 bond_fold_stats(stats, new, &slave->slave_stats);
3732 /* save off the slave stats for the next run */
3733 memcpy(&slave->slave_stats, new, sizeof(*new));
3736 memcpy(&bond->bond_stats, stats, sizeof(*stats));
3737 spin_unlock(&bond->stats_lock);
3741 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3743 struct bonding *bond = netdev_priv(bond_dev);
3744 struct net_device *slave_dev = NULL;
3745 struct ifbond k_binfo;
3746 struct ifbond __user *u_binfo = NULL;
3747 struct ifslave k_sinfo;
3748 struct ifslave __user *u_sinfo = NULL;
3749 struct mii_ioctl_data *mii = NULL;
3750 struct bond_opt_value newval;
3754 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
3765 /* We do this again just in case we were called by SIOCGMIIREG
3766 * instead of SIOCGMIIPHY.
3772 if (mii->reg_num == 1) {
3774 if (netif_carrier_ok(bond->dev))
3775 mii->val_out = BMSR_LSTATUS;
3779 case BOND_INFO_QUERY_OLD:
3780 case SIOCBONDINFOQUERY:
3781 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3783 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
3786 bond_info_query(bond_dev, &k_binfo);
3787 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3791 case BOND_SLAVE_INFO_QUERY_OLD:
3792 case SIOCBONDSLAVEINFOQUERY:
3793 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3795 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
3798 res = bond_slave_info_query(bond_dev, &k_sinfo);
3800 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3808 net = dev_net(bond_dev);
3810 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3813 slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
3815 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
3821 case BOND_ENSLAVE_OLD:
3822 case SIOCBONDENSLAVE:
3823 res = bond_enslave(bond_dev, slave_dev, NULL);
3825 case BOND_RELEASE_OLD:
3826 case SIOCBONDRELEASE:
3827 res = bond_release(bond_dev, slave_dev);
3829 case BOND_SETHWADDR_OLD:
3830 case SIOCBONDSETHWADDR:
3831 res = bond_set_dev_addr(bond_dev, slave_dev);
3833 case BOND_CHANGE_ACTIVE_OLD:
3834 case SIOCBONDCHANGEACTIVE:
3835 bond_opt_initstr(&newval, slave_dev->name);
3836 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
3846 static void bond_change_rx_flags(struct net_device *bond_dev, int change)
3848 struct bonding *bond = netdev_priv(bond_dev);
3850 if (change & IFF_PROMISC)
3851 bond_set_promiscuity(bond,
3852 bond_dev->flags & IFF_PROMISC ? 1 : -1);
3854 if (change & IFF_ALLMULTI)
3855 bond_set_allmulti(bond,
3856 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
3859 static void bond_set_rx_mode(struct net_device *bond_dev)
3861 struct bonding *bond = netdev_priv(bond_dev);
3862 struct list_head *iter;
3863 struct slave *slave;
3866 if (bond_uses_primary(bond)) {
3867 slave = rcu_dereference(bond->curr_active_slave);
3869 dev_uc_sync(slave->dev, bond_dev);
3870 dev_mc_sync(slave->dev, bond_dev);
3873 bond_for_each_slave_rcu(bond, slave, iter) {
3874 dev_uc_sync_multiple(slave->dev, bond_dev);
3875 dev_mc_sync_multiple(slave->dev, bond_dev);
3881 static int bond_neigh_init(struct neighbour *n)
3883 struct bonding *bond = netdev_priv(n->dev);
3884 const struct net_device_ops *slave_ops;
3885 struct neigh_parms parms;
3886 struct slave *slave;
3890 slave = bond_first_slave_rcu(bond);
3893 slave_ops = slave->dev->netdev_ops;
3894 if (!slave_ops->ndo_neigh_setup)
3897 /* TODO: find another way [1] to implement this.
3898 * Passing a zeroed structure is fragile,
3899 * but at least we do not pass garbage.
3901 * [1] One way would be that ndo_neigh_setup() never touch
3902 * struct neigh_parms, but propagate the new neigh_setup()
3903 * back to ___neigh_create() / neigh_parms_alloc()
3905 memset(&parms, 0, sizeof(parms));
3906 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
3911 if (parms.neigh_setup)
3912 ret = parms.neigh_setup(n);
3918 /* The bonding ndo_neigh_setup is called at init time beofre any
3919 * slave exists. So we must declare proxy setup function which will
3920 * be used at run time to resolve the actual slave neigh param setup.
3922 * It's also called by master devices (such as vlans) to setup their
3923 * underlying devices. In that case - do nothing, we're already set up from
3926 static int bond_neigh_setup(struct net_device *dev,
3927 struct neigh_parms *parms)
3929 /* modify only our neigh_parms */
3930 if (parms->dev == dev)
3931 parms->neigh_setup = bond_neigh_init;
3936 /* Change the MTU of all of a master's slaves to match the master */
3937 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3939 struct bonding *bond = netdev_priv(bond_dev);
3940 struct slave *slave, *rollback_slave;
3941 struct list_head *iter;
3944 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
3946 bond_for_each_slave(bond, slave, iter) {
3947 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
3948 slave, slave->dev->netdev_ops->ndo_change_mtu);
3950 res = dev_set_mtu(slave->dev, new_mtu);
3953 /* If we failed to set the slave's mtu to the new value
3954 * we must abort the operation even in ACTIVE_BACKUP
3955 * mode, because if we allow the backup slaves to have
3956 * different mtu values than the active slave we'll
3957 * need to change their mtu when doing a failover. That
3958 * means changing their mtu from timer context, which
3959 * is probably not a good idea.
3961 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
3967 bond_dev->mtu = new_mtu;
3972 /* unwind from head to the slave that failed */
3973 bond_for_each_slave(bond, rollback_slave, iter) {
3976 if (rollback_slave == slave)
3979 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
3981 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
3988 /* Change HW address
3990 * Note that many devices must be down to change the HW address, and
3991 * downing the master releases all slaves. We can make bonds full of
3992 * bonding devices to test this, however.
3994 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3996 struct bonding *bond = netdev_priv(bond_dev);
3997 struct slave *slave, *rollback_slave;
3998 struct sockaddr_storage *ss = addr, tmp_ss;
3999 struct list_head *iter;
4002 if (BOND_MODE(bond) == BOND_MODE_ALB)
4003 return bond_alb_set_mac_address(bond_dev, addr);
4006 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
4008 /* If fail_over_mac is enabled, do nothing and return success.
4009 * Returning an error causes ifenslave to fail.
4011 if (bond->params.fail_over_mac &&
4012 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
4015 if (!is_valid_ether_addr(ss->__data))
4016 return -EADDRNOTAVAIL;
4018 bond_for_each_slave(bond, slave, iter) {
4019 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4021 res = dev_set_mac_address(slave->dev, addr, NULL);
4023 /* TODO: consider downing the slave
4025 * User should expect communications
4026 * breakage anyway until ARP finish
4029 slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4036 memcpy(bond_dev->dev_addr, ss->__data, bond_dev->addr_len);
4040 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4041 tmp_ss.ss_family = bond_dev->type;
4043 /* unwind from head to the slave that failed */
4044 bond_for_each_slave(bond, rollback_slave, iter) {
4047 if (rollback_slave == slave)
4050 tmp_res = dev_set_mac_address(rollback_slave->dev,
4051 (struct sockaddr *)&tmp_ss, NULL);
4053 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4062 * bond_get_slave_by_id - get xmit slave with slave_id
4063 * @bond: bonding device that is transmitting
4064 * @slave_id: slave id up to slave_cnt-1 through which to transmit
4066 * This function tries to get slave with slave_id but in case
4067 * it fails, it tries to find the first available slave for transmission.
4069 static struct slave *bond_get_slave_by_id(struct bonding *bond,
4072 struct list_head *iter;
4073 struct slave *slave;
4076 /* Here we start from the slave with slave_id */
4077 bond_for_each_slave_rcu(bond, slave, iter) {
4079 if (bond_slave_can_tx(slave))
4084 /* Here we start from the first slave up to slave_id */
4086 bond_for_each_slave_rcu(bond, slave, iter) {
4089 if (bond_slave_can_tx(slave))
4092 /* no slave that can tx has been found */
4097 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
4098 * @bond: bonding device to use
4100 * Based on the value of the bonding device's packets_per_slave parameter
4101 * this function generates a slave id, which is usually used as the next
4102 * slave to transmit through.
4104 static u32 bond_rr_gen_slave_id(struct bonding *bond)
4107 struct reciprocal_value reciprocal_packets_per_slave;
4108 int packets_per_slave = bond->params.packets_per_slave;
4110 switch (packets_per_slave) {
4112 slave_id = prandom_u32();
4115 slave_id = bond->rr_tx_counter;
4118 reciprocal_packets_per_slave =
4119 bond->params.reciprocal_packets_per_slave;
4120 slave_id = reciprocal_divide(bond->rr_tx_counter,
4121 reciprocal_packets_per_slave);
4124 bond->rr_tx_counter++;
4129 static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
4130 struct sk_buff *skb)
4132 struct slave *slave;
4136 /* Start with the curr_active_slave that joined the bond as the
4137 * default for sending IGMP traffic. For failover purposes one
4138 * needs to maintain some consistency for the interface that will
4139 * send the join/membership reports. The curr_active_slave found
4140 * will send all of this type of traffic.
4142 if (skb->protocol == htons(ETH_P_IP)) {
4143 int noff = skb_network_offset(skb);
4146 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
4150 if (iph->protocol == IPPROTO_IGMP) {
4151 slave = rcu_dereference(bond->curr_active_slave);
4154 return bond_get_slave_by_id(bond, 0);
4159 slave_cnt = READ_ONCE(bond->slave_cnt);
4160 if (likely(slave_cnt)) {
4161 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4162 return bond_get_slave_by_id(bond, slave_id);
4167 static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
4168 struct net_device *bond_dev)
4170 struct bonding *bond = netdev_priv(bond_dev);
4171 struct slave *slave;
4173 slave = bond_xmit_roundrobin_slave_get(bond, skb);
4175 return bond_dev_queue_xmit(bond, skb, slave->dev);
4177 return bond_tx_drop(bond_dev, skb);
4180 static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond,
4181 struct sk_buff *skb)
4183 return rcu_dereference(bond->curr_active_slave);
4186 /* In active-backup mode, we know that bond->curr_active_slave is always valid if
4187 * the bond has a usable interface.
4189 static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
4190 struct net_device *bond_dev)
4192 struct bonding *bond = netdev_priv(bond_dev);
4193 struct slave *slave;
4195 slave = bond_xmit_activebackup_slave_get(bond, skb);
4197 return bond_dev_queue_xmit(bond, skb, slave->dev);
4199 return bond_tx_drop(bond_dev, skb);
4202 /* Use this to update slave_array when (a) it's not appropriate to update
4203 * slave_array right away (note that update_slave_array() may sleep)
4204 * and / or (b) RTNL is not held.
4206 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
4208 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
4211 /* Slave array work handler. Holds only RTNL */
4212 static void bond_slave_arr_handler(struct work_struct *work)
4214 struct bonding *bond = container_of(work, struct bonding,
4215 slave_arr_work.work);
4218 if (!rtnl_trylock())
4221 ret = bond_update_slave_arr(bond, NULL);
4224 pr_warn_ratelimited("Failed to update slave array from WT\n");
4230 bond_slave_arr_work_rearm(bond, 1);
4233 static void bond_skip_slave(struct bond_up_slave *slaves,
4234 struct slave *skipslave)
4238 /* Rare situation where caller has asked to skip a specific
4239 * slave but allocation failed (most likely!). BTW this is
4240 * only possible when the call is initiated from
4241 * __bond_release_one(). In this situation; overwrite the
4242 * skipslave entry in the array with the last entry from the
4243 * array to avoid a situation where the xmit path may choose
4244 * this to-be-skipped slave to send a packet out.
4246 for (idx = 0; slaves && idx < slaves->count; idx++) {
4247 if (skipslave == slaves->arr[idx]) {
4249 slaves->arr[slaves->count - 1];
4256 static void bond_set_slave_arr(struct bonding *bond,
4257 struct bond_up_slave *usable_slaves,
4258 struct bond_up_slave *all_slaves)
4260 struct bond_up_slave *usable, *all;
4262 usable = rtnl_dereference(bond->usable_slaves);
4263 rcu_assign_pointer(bond->usable_slaves, usable_slaves);
4264 kfree_rcu(usable, rcu);
4266 all = rtnl_dereference(bond->all_slaves);
4267 rcu_assign_pointer(bond->all_slaves, all_slaves);
4268 kfree_rcu(all, rcu);
4271 static void bond_reset_slave_arr(struct bonding *bond)
4273 struct bond_up_slave *usable, *all;
4275 usable = rtnl_dereference(bond->usable_slaves);
4277 RCU_INIT_POINTER(bond->usable_slaves, NULL);
4278 kfree_rcu(usable, rcu);
4281 all = rtnl_dereference(bond->all_slaves);
4283 RCU_INIT_POINTER(bond->all_slaves, NULL);
4284 kfree_rcu(all, rcu);
4288 /* Build the usable slaves array in control path for modes that use xmit-hash
4289 * to determine the slave interface -
4290 * (a) BOND_MODE_8023AD
4292 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
4294 * The caller is expected to hold RTNL only and NO other lock!
4296 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
4298 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
4299 struct slave *slave;
4300 struct list_head *iter;
4304 #ifdef CONFIG_LOCKDEP
4305 WARN_ON(lockdep_is_held(&bond->mode_lock));
4308 usable_slaves = kzalloc(struct_size(usable_slaves, arr,
4309 bond->slave_cnt), GFP_KERNEL);
4310 all_slaves = kzalloc(struct_size(all_slaves, arr,
4311 bond->slave_cnt), GFP_KERNEL);
4312 if (!usable_slaves || !all_slaves) {
4316 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4317 struct ad_info ad_info;
4319 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
4320 pr_debug("bond_3ad_get_active_agg_info failed\n");
4321 /* No active aggragator means it's not safe to use
4322 * the previous array.
4324 bond_reset_slave_arr(bond);
4327 agg_id = ad_info.aggregator_id;
4329 bond_for_each_slave(bond, slave, iter) {
4330 if (skipslave == slave)
4333 all_slaves->arr[all_slaves->count++] = slave;
4334 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4335 struct aggregator *agg;
4337 agg = SLAVE_AD_INFO(slave)->port.aggregator;
4338 if (!agg || agg->aggregator_identifier != agg_id)
4341 if (!bond_slave_can_tx(slave))
4344 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
4345 usable_slaves->count);
4347 usable_slaves->arr[usable_slaves->count++] = slave;
4350 bond_set_slave_arr(bond, usable_slaves, all_slaves);
4353 if (ret != 0 && skipslave) {
4354 bond_skip_slave(rtnl_dereference(bond->all_slaves),
4356 bond_skip_slave(rtnl_dereference(bond->usable_slaves),
4359 kfree_rcu(all_slaves, rcu);
4360 kfree_rcu(usable_slaves, rcu);
4365 static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
4366 struct sk_buff *skb,
4367 struct bond_up_slave *slaves)
4369 struct slave *slave;
4373 hash = bond_xmit_hash(bond, skb);
4374 count = slaves ? READ_ONCE(slaves->count) : 0;
4375 if (unlikely(!count))
4378 slave = slaves->arr[hash % count];
4382 /* Use this Xmit function for 3AD as well as XOR modes. The current
4383 * usable slave array is formed in the control path. The xmit function
4384 * just calculates hash and sends the packet out.
4386 static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
4387 struct net_device *dev)
4389 struct bonding *bond = netdev_priv(dev);
4390 struct bond_up_slave *slaves;
4391 struct slave *slave;
4393 slaves = rcu_dereference(bond->usable_slaves);
4394 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4396 return bond_dev_queue_xmit(bond, skb, slave->dev);
4398 return bond_tx_drop(dev, skb);
4401 /* in broadcast mode, we send everything to all usable interfaces. */
4402 static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
4403 struct net_device *bond_dev)
4405 struct bonding *bond = netdev_priv(bond_dev);
4406 struct slave *slave = NULL;
4407 struct list_head *iter;
4409 bond_for_each_slave_rcu(bond, slave, iter) {
4410 if (bond_is_last_slave(bond, slave))
4412 if (bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
4413 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4416 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
4417 bond_dev->name, __func__);
4420 bond_dev_queue_xmit(bond, skb2, slave->dev);
4423 if (slave && bond_slave_is_up(slave) && slave->link == BOND_LINK_UP)
4424 return bond_dev_queue_xmit(bond, skb, slave->dev);
4426 return bond_tx_drop(bond_dev, skb);
4429 /*------------------------- Device initialization ---------------------------*/
4431 /* Lookup the slave that corresponds to a qid */
4432 static inline int bond_slave_override(struct bonding *bond,
4433 struct sk_buff *skb)
4435 struct slave *slave = NULL;
4436 struct list_head *iter;
4438 if (!skb_rx_queue_recorded(skb))
4441 /* Find out if any slaves have the same mapping as this skb. */
4442 bond_for_each_slave_rcu(bond, slave, iter) {
4443 if (slave->queue_id == skb_get_queue_mapping(skb)) {
4444 if (bond_slave_is_up(slave) &&
4445 slave->link == BOND_LINK_UP) {
4446 bond_dev_queue_xmit(bond, skb, slave->dev);
4449 /* If the slave isn't UP, use default transmit policy. */
4458 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
4459 struct net_device *sb_dev)
4461 /* This helper function exists to help dev_pick_tx get the correct
4462 * destination queue. Using a helper function skips a call to
4463 * skb_tx_hash and will put the skbs in the queue we expect on their
4464 * way down to the bonding driver.
4466 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4468 /* Save the original txq to restore before passing to the driver */
4469 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
4471 if (unlikely(txq >= dev->real_num_tx_queues)) {
4473 txq -= dev->real_num_tx_queues;
4474 } while (txq >= dev->real_num_tx_queues);
4479 static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
4480 struct sk_buff *skb,
4483 struct bonding *bond = netdev_priv(master_dev);
4484 struct bond_up_slave *slaves;
4485 struct slave *slave = NULL;
4487 switch (BOND_MODE(bond)) {
4488 case BOND_MODE_ROUNDROBIN:
4489 slave = bond_xmit_roundrobin_slave_get(bond, skb);
4491 case BOND_MODE_ACTIVEBACKUP:
4492 slave = bond_xmit_activebackup_slave_get(bond, skb);
4494 case BOND_MODE_8023AD:
4497 slaves = rcu_dereference(bond->all_slaves);
4499 slaves = rcu_dereference(bond->usable_slaves);
4500 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4502 case BOND_MODE_BROADCAST:
4505 slave = bond_xmit_alb_slave_get(bond, skb);
4508 slave = bond_xmit_tlb_slave_get(bond, skb);
4511 /* Should never happen, mode already checked */
4512 WARN_ONCE(true, "Unknown bonding mode");
4521 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4523 struct bonding *bond = netdev_priv(dev);
4525 if (bond_should_override_tx_queue(bond) &&
4526 !bond_slave_override(bond, skb))
4527 return NETDEV_TX_OK;
4529 switch (BOND_MODE(bond)) {
4530 case BOND_MODE_ROUNDROBIN:
4531 return bond_xmit_roundrobin(skb, dev);
4532 case BOND_MODE_ACTIVEBACKUP:
4533 return bond_xmit_activebackup(skb, dev);
4534 case BOND_MODE_8023AD:
4536 return bond_3ad_xor_xmit(skb, dev);
4537 case BOND_MODE_BROADCAST:
4538 return bond_xmit_broadcast(skb, dev);
4540 return bond_alb_xmit(skb, dev);
4542 return bond_tlb_xmit(skb, dev);
4544 /* Should never happen, mode already checked */
4545 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
4547 return bond_tx_drop(dev, skb);
4551 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4553 struct bonding *bond = netdev_priv(dev);
4554 netdev_tx_t ret = NETDEV_TX_OK;
4556 /* If we risk deadlock from transmitting this in the
4557 * netpoll path, tell netpoll to queue the frame for later tx
4559 if (unlikely(is_netpoll_tx_blocked(dev)))
4560 return NETDEV_TX_BUSY;
4563 if (bond_has_slaves(bond))
4564 ret = __bond_start_xmit(skb, dev);
4566 ret = bond_tx_drop(dev, skb);
4572 static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
4574 if (speed == 0 || speed == SPEED_UNKNOWN)
4575 speed = slave->speed;
4577 speed = min(speed, slave->speed);
4582 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
4583 struct ethtool_link_ksettings *cmd)
4585 struct bonding *bond = netdev_priv(bond_dev);
4586 struct list_head *iter;
4587 struct slave *slave;
4590 cmd->base.duplex = DUPLEX_UNKNOWN;
4591 cmd->base.port = PORT_OTHER;
4593 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
4594 * do not need to check mode. Though link speed might not represent
4595 * the true receive or transmit bandwidth (not all modes are symmetric)
4596 * this is an accurate maximum.
4598 bond_for_each_slave(bond, slave, iter) {
4599 if (bond_slave_can_tx(slave)) {
4600 if (slave->speed != SPEED_UNKNOWN) {
4601 if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
4602 speed = bond_mode_bcast_speed(slave,
4605 speed += slave->speed;
4607 if (cmd->base.duplex == DUPLEX_UNKNOWN &&
4608 slave->duplex != DUPLEX_UNKNOWN)
4609 cmd->base.duplex = slave->duplex;
4612 cmd->base.speed = speed ? : SPEED_UNKNOWN;
4617 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4618 struct ethtool_drvinfo *drvinfo)
4620 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
4621 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
4625 static const struct ethtool_ops bond_ethtool_ops = {
4626 .get_drvinfo = bond_ethtool_get_drvinfo,
4627 .get_link = ethtool_op_get_link,
4628 .get_link_ksettings = bond_ethtool_get_link_ksettings,
4631 static const struct net_device_ops bond_netdev_ops = {
4632 .ndo_init = bond_init,
4633 .ndo_uninit = bond_uninit,
4634 .ndo_open = bond_open,
4635 .ndo_stop = bond_close,
4636 .ndo_start_xmit = bond_start_xmit,
4637 .ndo_select_queue = bond_select_queue,
4638 .ndo_get_stats64 = bond_get_stats,
4639 .ndo_do_ioctl = bond_do_ioctl,
4640 .ndo_change_rx_flags = bond_change_rx_flags,
4641 .ndo_set_rx_mode = bond_set_rx_mode,
4642 .ndo_change_mtu = bond_change_mtu,
4643 .ndo_set_mac_address = bond_set_mac_address,
4644 .ndo_neigh_setup = bond_neigh_setup,
4645 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4646 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4647 #ifdef CONFIG_NET_POLL_CONTROLLER
4648 .ndo_netpoll_setup = bond_netpoll_setup,
4649 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4650 .ndo_poll_controller = bond_poll_controller,
4652 .ndo_add_slave = bond_enslave,
4653 .ndo_del_slave = bond_release,
4654 .ndo_fix_features = bond_fix_features,
4655 .ndo_features_check = passthru_features_check,
4656 .ndo_get_xmit_slave = bond_xmit_get_slave,
4659 static const struct device_type bond_type = {
4663 static void bond_destructor(struct net_device *bond_dev)
4665 struct bonding *bond = netdev_priv(bond_dev);
4667 destroy_workqueue(bond->wq);
4670 void bond_setup(struct net_device *bond_dev)
4672 struct bonding *bond = netdev_priv(bond_dev);
4674 spin_lock_init(&bond->mode_lock);
4675 bond->params = bonding_defaults;
4677 /* Initialize pointers */
4678 bond->dev = bond_dev;
4680 /* Initialize the device entry points */
4681 ether_setup(bond_dev);
4682 bond_dev->max_mtu = ETH_MAX_MTU;
4683 bond_dev->netdev_ops = &bond_netdev_ops;
4684 bond_dev->ethtool_ops = &bond_ethtool_ops;
4686 bond_dev->needs_free_netdev = true;
4687 bond_dev->priv_destructor = bond_destructor;
4689 SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
4691 /* Initialize the device options */
4692 bond_dev->flags |= IFF_MASTER;
4693 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
4694 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
4696 #ifdef CONFIG_XFRM_OFFLOAD
4697 /* set up xfrm device ops (only supported in active-backup right now) */
4698 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
4700 #endif /* CONFIG_XFRM_OFFLOAD */
4702 /* don't acquire bond device's netif_tx_lock when transmitting */
4703 bond_dev->features |= NETIF_F_LLTX;
4705 /* By default, we declare the bond to be fully
4706 * VLAN hardware accelerated capable. Special
4707 * care is taken in the various xmit functions
4708 * when there are slaves that are not hw accel
4712 /* Don't allow bond devices to change network namespaces. */
4713 bond_dev->features |= NETIF_F_NETNS_LOCAL;
4715 bond_dev->hw_features = BOND_VLAN_FEATURES |
4716 NETIF_F_HW_VLAN_CTAG_RX |
4717 NETIF_F_HW_VLAN_CTAG_FILTER;
4719 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
4720 #ifdef CONFIG_XFRM_OFFLOAD
4721 bond_dev->hw_features |= BOND_XFRM_FEATURES;
4722 #endif /* CONFIG_XFRM_OFFLOAD */
4723 bond_dev->features |= bond_dev->hw_features;
4724 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
4725 #ifdef CONFIG_XFRM_OFFLOAD
4726 /* Disable XFRM features if this isn't an active-backup config */
4727 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
4728 bond_dev->features &= ~BOND_XFRM_FEATURES;
4729 #endif /* CONFIG_XFRM_OFFLOAD */
4732 /* Destroy a bonding device.
4733 * Must be under rtnl_lock when this function is called.
4735 static void bond_uninit(struct net_device *bond_dev)
4737 struct bonding *bond = netdev_priv(bond_dev);
4738 struct bond_up_slave *usable, *all;
4739 struct list_head *iter;
4740 struct slave *slave;
4742 bond_netpoll_cleanup(bond_dev);
4744 /* Release the bonded slaves */
4745 bond_for_each_slave(bond, slave, iter)
4746 __bond_release_one(bond_dev, slave->dev, true, true);
4747 netdev_info(bond_dev, "Released all slaves\n");
4749 usable = rtnl_dereference(bond->usable_slaves);
4751 RCU_INIT_POINTER(bond->usable_slaves, NULL);
4752 kfree_rcu(usable, rcu);
4755 all = rtnl_dereference(bond->all_slaves);
4757 RCU_INIT_POINTER(bond->all_slaves, NULL);
4758 kfree_rcu(all, rcu);
4761 list_del(&bond->bond_list);
4763 bond_debug_unregister(bond);
4766 /*------------------------- Module initialization ---------------------------*/
4768 static int bond_check_params(struct bond_params *params)
4770 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
4771 struct bond_opt_value newval;
4772 const struct bond_opt_value *valptr;
4773 int arp_all_targets_value = 0;
4774 u16 ad_actor_sys_prio = 0;
4775 u16 ad_user_port_key = 0;
4776 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
4778 int bond_mode = BOND_MODE_ROUNDROBIN;
4779 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
4783 /* Convert string parameters. */
4785 bond_opt_initstr(&newval, mode);
4786 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
4788 pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
4791 bond_mode = valptr->value;
4794 if (xmit_hash_policy) {
4795 if (bond_mode == BOND_MODE_ROUNDROBIN ||
4796 bond_mode == BOND_MODE_ACTIVEBACKUP ||
4797 bond_mode == BOND_MODE_BROADCAST) {
4798 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4799 bond_mode_name(bond_mode));
4801 bond_opt_initstr(&newval, xmit_hash_policy);
4802 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
4805 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4809 xmit_hashtype = valptr->value;
4814 if (bond_mode != BOND_MODE_8023AD) {
4815 pr_info("lacp_rate param is irrelevant in mode %s\n",
4816 bond_mode_name(bond_mode));
4818 bond_opt_initstr(&newval, lacp_rate);
4819 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
4822 pr_err("Error: Invalid lacp rate \"%s\"\n",
4826 lacp_fast = valptr->value;
4831 bond_opt_initstr(&newval, ad_select);
4832 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
4835 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
4838 params->ad_select = valptr->value;
4839 if (bond_mode != BOND_MODE_8023AD)
4840 pr_warn("ad_select param only affects 802.3ad mode\n");
4842 params->ad_select = BOND_AD_STABLE;
4845 if (max_bonds < 0) {
4846 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4847 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4848 max_bonds = BOND_DEFAULT_MAX_BONDS;
4852 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4858 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4863 if (downdelay < 0) {
4864 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4865 downdelay, INT_MAX);
4869 if ((use_carrier != 0) && (use_carrier != 1)) {
4870 pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4875 if (num_peer_notif < 0 || num_peer_notif > 255) {
4876 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4881 /* reset values for 802.3ad/TLB/ALB */
4882 if (!bond_mode_uses_arp(bond_mode)) {
4884 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");
4885 pr_warn("Forcing miimon to 100msec\n");
4886 miimon = BOND_DEFAULT_MIIMON;
4890 if (tx_queues < 1 || tx_queues > 255) {
4891 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
4892 tx_queues, BOND_DEFAULT_TX_QUEUES);
4893 tx_queues = BOND_DEFAULT_TX_QUEUES;
4896 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4897 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
4899 all_slaves_active = 0;
4902 if (resend_igmp < 0 || resend_igmp > 255) {
4903 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
4904 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4905 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4908 bond_opt_initval(&newval, packets_per_slave);
4909 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
4910 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
4911 packets_per_slave, USHRT_MAX);
4912 packets_per_slave = 1;
4915 if (bond_mode == BOND_MODE_ALB) {
4916 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",
4921 if (updelay || downdelay) {
4922 /* just warn the user the up/down delay will have
4923 * no effect since miimon is zero...
4925 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",
4926 updelay, downdelay);
4929 /* don't allow arp monitoring */
4931 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4932 miimon, arp_interval);
4936 if ((updelay % miimon) != 0) {
4937 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4938 updelay, miimon, (updelay / miimon) * miimon);
4943 if ((downdelay % miimon) != 0) {
4944 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4946 (downdelay / miimon) * miimon);
4949 downdelay /= miimon;
4952 if (arp_interval < 0) {
4953 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4954 arp_interval, INT_MAX);
4958 for (arp_ip_count = 0, i = 0;
4959 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
4962 /* not a complete check, but good enough to catch mistakes */
4963 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
4964 !bond_is_ip_target_ok(ip)) {
4965 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4969 if (bond_get_targets_ip(arp_target, ip) == -1)
4970 arp_target[arp_ip_count++] = ip;
4972 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
4977 if (arp_interval && !arp_ip_count) {
4978 /* don't allow arping if no arp_ip_target given... */
4979 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4985 if (!arp_interval) {
4986 pr_err("arp_validate requires arp_interval\n");
4990 bond_opt_initstr(&newval, arp_validate);
4991 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
4994 pr_err("Error: invalid arp_validate \"%s\"\n",
4998 arp_validate_value = valptr->value;
5000 arp_validate_value = 0;
5003 if (arp_all_targets) {
5004 bond_opt_initstr(&newval, arp_all_targets);
5005 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
5008 pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
5010 arp_all_targets_value = 0;
5012 arp_all_targets_value = valptr->value;
5017 pr_info("MII link monitoring set to %d ms\n", miimon);
5018 } else if (arp_interval) {
5019 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
5020 arp_validate_value);
5021 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5022 arp_interval, valptr->string, arp_ip_count);
5024 for (i = 0; i < arp_ip_count; i++)
5025 pr_cont(" %s", arp_ip_target[i]);
5029 } else if (max_bonds) {
5030 /* miimon and arp_interval not set, we need one so things
5031 * work as expected, see bonding.txt for details
5033 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");
5036 if (primary && !bond_mode_uses_primary(bond_mode)) {
5037 /* currently, using a primary only makes sense
5038 * in active backup, TLB or ALB modes
5040 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
5041 primary, bond_mode_name(bond_mode));
5045 if (primary && primary_reselect) {
5046 bond_opt_initstr(&newval, primary_reselect);
5047 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
5050 pr_err("Error: Invalid primary_reselect \"%s\"\n",
5054 primary_reselect_value = valptr->value;
5056 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5059 if (fail_over_mac) {
5060 bond_opt_initstr(&newval, fail_over_mac);
5061 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
5064 pr_err("Error: invalid fail_over_mac \"%s\"\n",
5068 fail_over_mac_value = valptr->value;
5069 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5070 pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
5072 fail_over_mac_value = BOND_FOM_NONE;
5075 bond_opt_initstr(&newval, "default");
5076 valptr = bond_opt_parse(
5077 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
5080 pr_err("Error: No ad_actor_sys_prio default value");
5083 ad_actor_sys_prio = valptr->value;
5085 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
5088 pr_err("Error: No ad_user_port_key default value");
5091 ad_user_port_key = valptr->value;
5093 bond_opt_initstr(&newval, "default");
5094 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
5096 pr_err("Error: No tlb_dynamic_lb default value");
5099 tlb_dynamic_lb = valptr->value;
5101 if (lp_interval == 0) {
5102 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
5103 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
5104 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
5107 /* fill params struct with the proper values */
5108 params->mode = bond_mode;
5109 params->xmit_policy = xmit_hashtype;
5110 params->miimon = miimon;
5111 params->num_peer_notif = num_peer_notif;
5112 params->arp_interval = arp_interval;
5113 params->arp_validate = arp_validate_value;
5114 params->arp_all_targets = arp_all_targets_value;
5115 params->updelay = updelay;
5116 params->downdelay = downdelay;
5117 params->peer_notif_delay = 0;
5118 params->use_carrier = use_carrier;
5119 params->lacp_fast = lacp_fast;
5120 params->primary[0] = 0;
5121 params->primary_reselect = primary_reselect_value;
5122 params->fail_over_mac = fail_over_mac_value;
5123 params->tx_queues = tx_queues;
5124 params->all_slaves_active = all_slaves_active;
5125 params->resend_igmp = resend_igmp;
5126 params->min_links = min_links;
5127 params->lp_interval = lp_interval;
5128 params->packets_per_slave = packets_per_slave;
5129 params->tlb_dynamic_lb = tlb_dynamic_lb;
5130 params->ad_actor_sys_prio = ad_actor_sys_prio;
5131 eth_zero_addr(params->ad_actor_system);
5132 params->ad_user_port_key = ad_user_port_key;
5133 if (packets_per_slave > 0) {
5134 params->reciprocal_packets_per_slave =
5135 reciprocal_value(packets_per_slave);
5137 /* reciprocal_packets_per_slave is unused if
5138 * packets_per_slave is 0 or 1, just initialize it
5140 params->reciprocal_packets_per_slave =
5141 (struct reciprocal_value) { 0 };
5145 strncpy(params->primary, primary, IFNAMSIZ);
5146 params->primary[IFNAMSIZ - 1] = 0;
5149 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5154 /* Called from registration process */
5155 static int bond_init(struct net_device *bond_dev)
5157 struct bonding *bond = netdev_priv(bond_dev);
5158 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
5160 netdev_dbg(bond_dev, "Begin bond_init\n");
5162 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM);
5166 spin_lock_init(&bond->stats_lock);
5167 netdev_lockdep_set_classes(bond_dev);
5169 list_add_tail(&bond->bond_list, &bn->dev_list);
5171 bond_prepare_sysfs_group(bond);
5173 bond_debug_register(bond);
5175 /* Ensure valid dev_addr */
5176 if (is_zero_ether_addr(bond_dev->dev_addr) &&
5177 bond_dev->addr_assign_type == NET_ADDR_PERM)
5178 eth_hw_addr_random(bond_dev);
5183 unsigned int bond_get_num_tx_queues(void)
5188 /* Create a new bond based on the specified name and bonding parameters.
5189 * If name is NULL, obtain a suitable "bond%d" name for us.
5190 * Caller must NOT hold rtnl_lock; we need to release it here before we
5191 * set up our sysfs entries.
5193 int bond_create(struct net *net, const char *name)
5195 struct net_device *bond_dev;
5196 struct bonding *bond;
5197 struct alb_bond_info *bond_info;
5202 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
5203 name ? name : "bond%d", NET_NAME_UNKNOWN,
5204 bond_setup, tx_queues);
5206 pr_err("%s: eek! can't alloc netdev!\n", name);
5212 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX.
5213 * It is set to 0 by default which is wrong.
5215 bond = netdev_priv(bond_dev);
5216 bond_info = &(BOND_ALB_INFO(bond));
5217 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
5219 dev_net_set(bond_dev, net);
5220 bond_dev->rtnl_link_ops = &bond_link_ops;
5222 res = register_netdevice(bond_dev);
5224 free_netdev(bond_dev);
5230 netif_carrier_off(bond_dev);
5232 bond_work_init_all(bond);
5238 static int __net_init bond_net_init(struct net *net)
5240 struct bond_net *bn = net_generic(net, bond_net_id);
5243 INIT_LIST_HEAD(&bn->dev_list);
5245 bond_create_proc_dir(bn);
5246 bond_create_sysfs(bn);
5251 static void __net_exit bond_net_exit(struct net *net)
5253 struct bond_net *bn = net_generic(net, bond_net_id);
5254 struct bonding *bond, *tmp_bond;
5257 bond_destroy_sysfs(bn);
5259 /* Kill off any bonds created after unregistering bond rtnl ops */
5261 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
5262 unregister_netdevice_queue(bond->dev, &list);
5263 unregister_netdevice_many(&list);
5266 bond_destroy_proc_dir(bn);
5269 static struct pernet_operations bond_net_ops = {
5270 .init = bond_net_init,
5271 .exit = bond_net_exit,
5273 .size = sizeof(struct bond_net),
5276 static int __init bonding_init(void)
5281 res = bond_check_params(&bonding_defaults);
5285 res = register_pernet_subsys(&bond_net_ops);
5289 res = bond_netlink_init();
5293 bond_create_debugfs();
5295 for (i = 0; i < max_bonds; i++) {
5296 res = bond_create(&init_net, NULL);
5301 skb_flow_dissector_init(&flow_keys_bonding,
5302 flow_keys_bonding_keys,
5303 ARRAY_SIZE(flow_keys_bonding_keys));
5305 register_netdevice_notifier(&bond_netdev_notifier);
5309 bond_destroy_debugfs();
5310 bond_netlink_fini();
5312 unregister_pernet_subsys(&bond_net_ops);
5317 static void __exit bonding_exit(void)
5319 unregister_netdevice_notifier(&bond_netdev_notifier);
5321 bond_destroy_debugfs();
5323 bond_netlink_fini();
5324 unregister_pernet_subsys(&bond_net_ops);
5326 #ifdef CONFIG_NET_POLL_CONTROLLER
5327 /* Make sure we don't have an imbalance on our netpoll blocking */
5328 WARN_ON(atomic_read(&netpoll_block_tx));
5332 module_init(bonding_init);
5333 module_exit(bonding_exit);
5334 MODULE_LICENSE("GPL");
5335 MODULE_DESCRIPTION(DRV_DESCRIPTION);
5336 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");