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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.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>
57 #include <asm/system.h>
59 #include <linux/uaccess.h>
60 #include <linux/errno.h>
61 #include <linux/netdevice.h>
62 #include <linux/inetdevice.h>
63 #include <linux/igmp.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
67 #include <linux/rtnetlink.h>
68 #include <linux/smp.h>
69 #include <linux/if_ether.h>
71 #include <linux/mii.h>
72 #include <linux/ethtool.h>
73 #include <linux/if_vlan.h>
74 #include <linux/if_bonding.h>
75 #include <linux/jiffies.h>
76 #include <linux/preempt.h>
77 #include <net/route.h>
78 #include <net/net_namespace.h>
79 #include <net/netns/generic.h>
84 /*---------------------------- Module parameters ----------------------------*/
86 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
87 #define BOND_LINK_MON_INTERV 0
88 #define BOND_LINK_ARP_INTERV 0
90 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
91 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
92 static int num_grat_arp = 1;
93 static int num_unsol_na = 1;
94 static int miimon = BOND_LINK_MON_INTERV;
97 static int use_carrier = 1;
100 static char *primary_reselect;
101 static char *lacp_rate;
102 static char *ad_select;
103 static char *xmit_hash_policy;
104 static int arp_interval = BOND_LINK_ARP_INTERV;
105 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
106 static char *arp_validate;
107 static char *fail_over_mac;
108 static int all_slaves_active = 0;
109 static struct bond_params bonding_defaults;
110 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
112 module_param(max_bonds, int, 0);
113 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
114 module_param(tx_queues, int, 0);
115 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
116 module_param(num_grat_arp, int, 0644);
117 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
118 module_param(num_unsol_na, int, 0644);
119 MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
120 module_param(miimon, int, 0);
121 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
122 module_param(updelay, int, 0);
123 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
124 module_param(downdelay, int, 0);
125 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
127 module_param(use_carrier, int, 0);
128 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
129 "0 for off, 1 for on (default)");
130 module_param(mode, charp, 0);
131 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
132 "1 for active-backup, 2 for balance-xor, "
133 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
134 "6 for balance-alb");
135 module_param(primary, charp, 0);
136 MODULE_PARM_DESC(primary, "Primary network device to use");
137 module_param(primary_reselect, charp, 0);
138 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
140 "0 for always (default), "
141 "1 for only if speed of primary is "
143 "2 for only on active slave "
145 module_param(lacp_rate, charp, 0);
146 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
148 module_param(ad_select, charp, 0);
149 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
150 module_param(xmit_hash_policy, charp, 0);
151 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
152 ", 1 for layer 3+4");
153 module_param(arp_interval, int, 0);
154 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
155 module_param_array(arp_ip_target, charp, NULL, 0);
156 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
157 module_param(arp_validate, charp, 0);
158 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
159 module_param(fail_over_mac, charp, 0);
160 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
161 module_param(all_slaves_active, int, 0);
162 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
163 "by setting active flag for all slaves. "
164 "0 for never (default), 1 for always.");
165 module_param(resend_igmp, int, 0);
166 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
168 /*----------------------------- Global variables ----------------------------*/
170 #ifdef CONFIG_NET_POLL_CONTROLLER
171 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
174 int bond_net_id __read_mostly;
176 static __be32 arp_target[BOND_MAX_ARP_TARGETS];
177 static int arp_ip_count;
178 static int bond_mode = BOND_MODE_ROUNDROBIN;
179 static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
180 static int lacp_fast;
182 const struct bond_parm_tbl bond_lacp_tbl[] = {
183 { "slow", AD_LACP_SLOW},
184 { "fast", AD_LACP_FAST},
188 const struct bond_parm_tbl bond_mode_tbl[] = {
189 { "balance-rr", BOND_MODE_ROUNDROBIN},
190 { "active-backup", BOND_MODE_ACTIVEBACKUP},
191 { "balance-xor", BOND_MODE_XOR},
192 { "broadcast", BOND_MODE_BROADCAST},
193 { "802.3ad", BOND_MODE_8023AD},
194 { "balance-tlb", BOND_MODE_TLB},
195 { "balance-alb", BOND_MODE_ALB},
199 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
200 { "layer2", BOND_XMIT_POLICY_LAYER2},
201 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
202 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
206 const struct bond_parm_tbl arp_validate_tbl[] = {
207 { "none", BOND_ARP_VALIDATE_NONE},
208 { "active", BOND_ARP_VALIDATE_ACTIVE},
209 { "backup", BOND_ARP_VALIDATE_BACKUP},
210 { "all", BOND_ARP_VALIDATE_ALL},
214 const struct bond_parm_tbl fail_over_mac_tbl[] = {
215 { "none", BOND_FOM_NONE},
216 { "active", BOND_FOM_ACTIVE},
217 { "follow", BOND_FOM_FOLLOW},
221 const struct bond_parm_tbl pri_reselect_tbl[] = {
222 { "always", BOND_PRI_RESELECT_ALWAYS},
223 { "better", BOND_PRI_RESELECT_BETTER},
224 { "failure", BOND_PRI_RESELECT_FAILURE},
228 struct bond_parm_tbl ad_select_tbl[] = {
229 { "stable", BOND_AD_STABLE},
230 { "bandwidth", BOND_AD_BANDWIDTH},
231 { "count", BOND_AD_COUNT},
235 /*-------------------------- Forward declarations ---------------------------*/
237 static void bond_send_gratuitous_arp(struct bonding *bond);
238 static int bond_init(struct net_device *bond_dev);
239 static void bond_uninit(struct net_device *bond_dev);
241 /*---------------------------- General routines -----------------------------*/
243 const char *bond_mode_name(int mode)
245 static const char *names[] = {
246 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
247 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
248 [BOND_MODE_XOR] = "load balancing (xor)",
249 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
250 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
251 [BOND_MODE_TLB] = "transmit load balancing",
252 [BOND_MODE_ALB] = "adaptive load balancing",
255 if (mode < 0 || mode > BOND_MODE_ALB)
261 /*---------------------------------- VLAN -----------------------------------*/
264 * bond_add_vlan - add a new vlan id on bond
265 * @bond: bond that got the notification
266 * @vlan_id: the vlan id to add
268 * Returns -ENOMEM if allocation failed.
270 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
272 struct vlan_entry *vlan;
274 pr_debug("bond: %s, vlan id %d\n",
275 (bond ? bond->dev->name : "None"), vlan_id);
277 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
281 INIT_LIST_HEAD(&vlan->vlan_list);
282 vlan->vlan_id = vlan_id;
284 write_lock_bh(&bond->lock);
286 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
288 write_unlock_bh(&bond->lock);
290 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
296 * bond_del_vlan - delete a vlan id from bond
297 * @bond: bond that got the notification
298 * @vlan_id: the vlan id to delete
300 * returns -ENODEV if @vlan_id was not found in @bond.
302 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
304 struct vlan_entry *vlan;
307 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
310 write_lock_bh(&bond->lock);
312 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
313 if (vlan->vlan_id == vlan_id) {
314 list_del(&vlan->vlan_list);
316 if (bond_is_lb(bond))
317 bond_alb_clear_vlan(bond, vlan_id);
319 pr_debug("removed VLAN ID %d from bond %s\n",
320 vlan_id, bond->dev->name);
324 if (list_empty(&bond->vlan_list) &&
325 (bond->slave_cnt == 0)) {
326 /* Last VLAN removed and no slaves, so
327 * restore block on adding VLANs. This will
328 * be removed once new slaves that are not
329 * VLAN challenged will be added.
331 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
339 pr_debug("couldn't find VLAN ID %d in bond %s\n",
340 vlan_id, bond->dev->name);
343 write_unlock_bh(&bond->lock);
344 unblock_netpoll_tx();
349 * bond_has_challenged_slaves
350 * @bond: the bond we're working on
352 * Searches the slave list. Returns 1 if a vlan challenged slave
353 * was found, 0 otherwise.
355 * Assumes bond->lock is held.
357 static int bond_has_challenged_slaves(struct bonding *bond)
362 bond_for_each_slave(bond, slave, i) {
363 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
364 pr_debug("found VLAN challenged slave - %s\n",
370 pr_debug("no VLAN challenged slaves found\n");
375 * bond_next_vlan - safely skip to the next item in the vlans list.
376 * @bond: the bond we're working on
377 * @curr: item we're advancing from
379 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
380 * or @curr->next otherwise (even if it is @curr itself again).
382 * Caller must hold bond->lock
384 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
386 struct vlan_entry *next, *last;
388 if (list_empty(&bond->vlan_list))
392 next = list_entry(bond->vlan_list.next,
393 struct vlan_entry, vlan_list);
395 last = list_entry(bond->vlan_list.prev,
396 struct vlan_entry, vlan_list);
398 next = list_entry(bond->vlan_list.next,
399 struct vlan_entry, vlan_list);
401 next = list_entry(curr->vlan_list.next,
402 struct vlan_entry, vlan_list);
410 * bond_dev_queue_xmit - Prepare skb for xmit.
412 * @bond: bond device that got this skb for tx.
413 * @skb: hw accel VLAN tagged skb to transmit
414 * @slave_dev: slave that is supposed to xmit this skbuff
416 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
417 struct net_device *slave_dev)
419 skb->dev = slave_dev;
421 if (unlikely(netpoll_tx_running(slave_dev)))
422 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
430 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
431 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
433 * a. This operation is performed in IOCTL context,
434 * b. The operation is protected by the RTNL semaphore in the 8021q code,
435 * c. Holding a lock with BH disabled while directly calling a base driver
436 * entry point is generally a BAD idea.
438 * The design of synchronization/protection for this operation in the 8021q
439 * module is good for one or more VLAN devices over a single physical device
440 * and cannot be extended for a teaming solution like bonding, so there is a
441 * potential race condition here where a net device from the vlan group might
442 * be referenced (either by a base driver or the 8021q code) while it is being
443 * removed from the system. However, it turns out we're not making matters
444 * worse, and if it works for regular VLAN usage it will work here too.
448 * bond_vlan_rx_register - Propagates registration to slaves
449 * @bond_dev: bonding net device that got called
450 * @grp: vlan group being registered
452 static void bond_vlan_rx_register(struct net_device *bond_dev,
453 struct vlan_group *grp)
455 struct bonding *bond = netdev_priv(bond_dev);
459 write_lock_bh(&bond->lock);
461 write_unlock_bh(&bond->lock);
463 bond_for_each_slave(bond, slave, i) {
464 struct net_device *slave_dev = slave->dev;
465 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
467 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
468 slave_ops->ndo_vlan_rx_register) {
469 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
475 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
476 * @bond_dev: bonding net device that got called
477 * @vid: vlan id being added
479 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
481 struct bonding *bond = netdev_priv(bond_dev);
485 bond_for_each_slave(bond, slave, i) {
486 struct net_device *slave_dev = slave->dev;
487 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
489 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
490 slave_ops->ndo_vlan_rx_add_vid) {
491 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
495 res = bond_add_vlan(bond, vid);
497 pr_err("%s: Error: Failed to add vlan id %d\n",
498 bond_dev->name, vid);
503 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
504 * @bond_dev: bonding net device that got called
505 * @vid: vlan id being removed
507 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
509 struct bonding *bond = netdev_priv(bond_dev);
511 struct net_device *vlan_dev;
514 bond_for_each_slave(bond, slave, i) {
515 struct net_device *slave_dev = slave->dev;
516 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
518 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
519 slave_ops->ndo_vlan_rx_kill_vid) {
520 /* Save and then restore vlan_dev in the grp array,
521 * since the slave's driver might clear it.
523 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
524 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
525 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
529 res = bond_del_vlan(bond, vid);
531 pr_err("%s: Error: Failed to remove vlan id %d\n",
532 bond_dev->name, vid);
536 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
538 struct vlan_entry *vlan;
539 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
544 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
545 slave_ops->ndo_vlan_rx_register)
546 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
548 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
549 !(slave_ops->ndo_vlan_rx_add_vid))
552 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
553 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
556 static void bond_del_vlans_from_slave(struct bonding *bond,
557 struct net_device *slave_dev)
559 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
560 struct vlan_entry *vlan;
561 struct net_device *vlan_dev;
566 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
567 !(slave_ops->ndo_vlan_rx_kill_vid))
570 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
573 /* Save and then restore vlan_dev in the grp array,
574 * since the slave's driver might clear it.
576 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
577 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
578 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
582 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
583 slave_ops->ndo_vlan_rx_register)
584 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
587 /*------------------------------- Link status -------------------------------*/
590 * Set the carrier state for the master according to the state of its
591 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
592 * do special 802.3ad magic.
594 * Returns zero if carrier state does not change, nonzero if it does.
596 static int bond_set_carrier(struct bonding *bond)
601 if (bond->slave_cnt == 0)
604 if (bond->params.mode == BOND_MODE_8023AD)
605 return bond_3ad_set_carrier(bond);
607 bond_for_each_slave(bond, slave, i) {
608 if (slave->link == BOND_LINK_UP) {
609 if (!netif_carrier_ok(bond->dev)) {
610 netif_carrier_on(bond->dev);
618 if (netif_carrier_ok(bond->dev)) {
619 netif_carrier_off(bond->dev);
626 * Get link speed and duplex from the slave's base driver
627 * using ethtool. If for some reason the call fails or the
628 * values are invalid, fake speed and duplex to 100/Full
631 static int bond_update_speed_duplex(struct slave *slave)
633 struct net_device *slave_dev = slave->dev;
634 struct ethtool_cmd etool;
637 /* Fake speed and duplex */
638 slave->speed = SPEED_100;
639 slave->duplex = DUPLEX_FULL;
641 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
644 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
648 switch (etool.speed) {
658 switch (etool.duplex) {
666 slave->speed = etool.speed;
667 slave->duplex = etool.duplex;
673 * if <dev> supports MII link status reporting, check its link status.
675 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
676 * depending upon the setting of the use_carrier parameter.
678 * Return either BMSR_LSTATUS, meaning that the link is up (or we
679 * can't tell and just pretend it is), or 0, meaning that the link is
682 * If reporting is non-zero, instead of faking link up, return -1 if
683 * both ETHTOOL and MII ioctls fail (meaning the device does not
684 * support them). If use_carrier is set, return whatever it says.
685 * It'd be nice if there was a good way to tell if a driver supports
686 * netif_carrier, but there really isn't.
688 static int bond_check_dev_link(struct bonding *bond,
689 struct net_device *slave_dev, int reporting)
691 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
692 int (*ioctl)(struct net_device *, struct ifreq *, int);
694 struct mii_ioctl_data *mii;
696 if (!reporting && !netif_running(slave_dev))
699 if (bond->params.use_carrier)
700 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
702 /* Try to get link status using Ethtool first. */
703 if (slave_dev->ethtool_ops) {
704 if (slave_dev->ethtool_ops->get_link) {
707 link = slave_dev->ethtool_ops->get_link(slave_dev);
709 return link ? BMSR_LSTATUS : 0;
713 /* Ethtool can't be used, fallback to MII ioctls. */
714 ioctl = slave_ops->ndo_do_ioctl;
716 /* TODO: set pointer to correct ioctl on a per team member */
717 /* bases to make this more efficient. that is, once */
718 /* we determine the correct ioctl, we will always */
719 /* call it and not the others for that team */
723 * We cannot assume that SIOCGMIIPHY will also read a
724 * register; not all network drivers (e.g., e100)
728 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
729 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
731 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
732 mii->reg_num = MII_BMSR;
733 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
734 return mii->val_out & BMSR_LSTATUS;
739 * If reporting, report that either there's no dev->do_ioctl,
740 * or both SIOCGMIIREG and get_link failed (meaning that we
741 * cannot report link status). If not reporting, pretend
744 return reporting ? -1 : BMSR_LSTATUS;
747 /*----------------------------- Multicast list ------------------------------*/
750 * Push the promiscuity flag down to appropriate slaves
752 static int bond_set_promiscuity(struct bonding *bond, int inc)
755 if (USES_PRIMARY(bond->params.mode)) {
756 /* write lock already acquired */
757 if (bond->curr_active_slave) {
758 err = dev_set_promiscuity(bond->curr_active_slave->dev,
764 bond_for_each_slave(bond, slave, i) {
765 err = dev_set_promiscuity(slave->dev, inc);
774 * Push the allmulti flag down to all slaves
776 static int bond_set_allmulti(struct bonding *bond, int inc)
779 if (USES_PRIMARY(bond->params.mode)) {
780 /* write lock already acquired */
781 if (bond->curr_active_slave) {
782 err = dev_set_allmulti(bond->curr_active_slave->dev,
788 bond_for_each_slave(bond, slave, i) {
789 err = dev_set_allmulti(slave->dev, inc);
798 * Add a Multicast address to slaves
801 static void bond_mc_add(struct bonding *bond, void *addr)
803 if (USES_PRIMARY(bond->params.mode)) {
804 /* write lock already acquired */
805 if (bond->curr_active_slave)
806 dev_mc_add(bond->curr_active_slave->dev, addr);
811 bond_for_each_slave(bond, slave, i)
812 dev_mc_add(slave->dev, addr);
817 * Remove a multicast address from slave
820 static void bond_mc_del(struct bonding *bond, void *addr)
822 if (USES_PRIMARY(bond->params.mode)) {
823 /* write lock already acquired */
824 if (bond->curr_active_slave)
825 dev_mc_del(bond->curr_active_slave->dev, addr);
829 bond_for_each_slave(bond, slave, i) {
830 dev_mc_del(slave->dev, addr);
836 static void __bond_resend_igmp_join_requests(struct net_device *dev)
838 struct in_device *in_dev;
841 in_dev = __in_dev_get_rcu(dev);
843 ip_mc_rejoin_groups(in_dev);
848 * Retrieve the list of registered multicast addresses for the bonding
849 * device and retransmit an IGMP JOIN request to the current active
852 static void bond_resend_igmp_join_requests(struct bonding *bond)
854 struct net_device *vlan_dev;
855 struct vlan_entry *vlan;
857 read_lock(&bond->lock);
859 /* rejoin all groups on bond device */
860 __bond_resend_igmp_join_requests(bond->dev);
862 /* rejoin all groups on vlan devices */
864 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
865 vlan_dev = vlan_group_get_device(bond->vlgrp,
868 __bond_resend_igmp_join_requests(vlan_dev);
872 if (--bond->igmp_retrans > 0)
873 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
875 read_unlock(&bond->lock);
878 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
880 struct bonding *bond = container_of(work, struct bonding,
882 bond_resend_igmp_join_requests(bond);
886 * flush all members of flush->mc_list from device dev->mc_list
888 static void bond_mc_list_flush(struct net_device *bond_dev,
889 struct net_device *slave_dev)
891 struct bonding *bond = netdev_priv(bond_dev);
892 struct netdev_hw_addr *ha;
894 netdev_for_each_mc_addr(ha, bond_dev)
895 dev_mc_del(slave_dev, ha->addr);
897 if (bond->params.mode == BOND_MODE_8023AD) {
898 /* del lacpdu mc addr from mc list */
899 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
901 dev_mc_del(slave_dev, lacpdu_multicast);
905 /*--------------------------- Active slave change ---------------------------*/
908 * Update the mc list and multicast-related flags for the new and
909 * old active slaves (if any) according to the multicast mode, and
910 * promiscuous flags unconditionally.
912 static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
913 struct slave *old_active)
915 struct netdev_hw_addr *ha;
917 if (!USES_PRIMARY(bond->params.mode))
918 /* nothing to do - mc list is already up-to-date on
924 if (bond->dev->flags & IFF_PROMISC)
925 dev_set_promiscuity(old_active->dev, -1);
927 if (bond->dev->flags & IFF_ALLMULTI)
928 dev_set_allmulti(old_active->dev, -1);
930 netdev_for_each_mc_addr(ha, bond->dev)
931 dev_mc_del(old_active->dev, ha->addr);
935 /* FIXME: Signal errors upstream. */
936 if (bond->dev->flags & IFF_PROMISC)
937 dev_set_promiscuity(new_active->dev, 1);
939 if (bond->dev->flags & IFF_ALLMULTI)
940 dev_set_allmulti(new_active->dev, 1);
942 netdev_for_each_mc_addr(ha, bond->dev)
943 dev_mc_add(new_active->dev, ha->addr);
948 * bond_do_fail_over_mac
950 * Perform special MAC address swapping for fail_over_mac settings
952 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
954 static void bond_do_fail_over_mac(struct bonding *bond,
955 struct slave *new_active,
956 struct slave *old_active)
957 __releases(&bond->curr_slave_lock)
958 __releases(&bond->lock)
959 __acquires(&bond->lock)
960 __acquires(&bond->curr_slave_lock)
962 u8 tmp_mac[ETH_ALEN];
963 struct sockaddr saddr;
966 switch (bond->params.fail_over_mac) {
967 case BOND_FOM_ACTIVE:
969 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
970 new_active->dev->addr_len);
972 case BOND_FOM_FOLLOW:
974 * if new_active && old_active, swap them
975 * if just old_active, do nothing (going to no active slave)
976 * if just new_active, set new_active to bond's MAC
981 write_unlock_bh(&bond->curr_slave_lock);
982 read_unlock(&bond->lock);
985 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
986 memcpy(saddr.sa_data, old_active->dev->dev_addr,
988 saddr.sa_family = new_active->dev->type;
990 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
991 saddr.sa_family = bond->dev->type;
994 rv = dev_set_mac_address(new_active->dev, &saddr);
996 pr_err("%s: Error %d setting MAC of slave %s\n",
997 bond->dev->name, -rv, new_active->dev->name);
1004 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1005 saddr.sa_family = old_active->dev->type;
1007 rv = dev_set_mac_address(old_active->dev, &saddr);
1009 pr_err("%s: Error %d setting MAC of slave %s\n",
1010 bond->dev->name, -rv, new_active->dev->name);
1012 read_lock(&bond->lock);
1013 write_lock_bh(&bond->curr_slave_lock);
1016 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
1017 bond->dev->name, bond->params.fail_over_mac);
1023 static bool bond_should_change_active(struct bonding *bond)
1025 struct slave *prim = bond->primary_slave;
1026 struct slave *curr = bond->curr_active_slave;
1028 if (!prim || !curr || curr->link != BOND_LINK_UP)
1030 if (bond->force_primary) {
1031 bond->force_primary = false;
1034 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1035 (prim->speed < curr->speed ||
1036 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1038 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1044 * find_best_interface - select the best available slave to be the active one
1045 * @bond: our bonding struct
1047 * Warning: Caller must hold curr_slave_lock for writing.
1049 static struct slave *bond_find_best_slave(struct bonding *bond)
1051 struct slave *new_active, *old_active;
1052 struct slave *bestslave = NULL;
1053 int mintime = bond->params.updelay;
1056 new_active = bond->curr_active_slave;
1058 if (!new_active) { /* there were no active slaves left */
1059 if (bond->slave_cnt > 0) /* found one slave */
1060 new_active = bond->first_slave;
1062 return NULL; /* still no slave, return NULL */
1065 if ((bond->primary_slave) &&
1066 bond->primary_slave->link == BOND_LINK_UP &&
1067 bond_should_change_active(bond)) {
1068 new_active = bond->primary_slave;
1071 /* remember where to stop iterating over the slaves */
1072 old_active = new_active;
1074 bond_for_each_slave_from(bond, new_active, i, old_active) {
1075 if (new_active->link == BOND_LINK_UP) {
1077 } else if (new_active->link == BOND_LINK_BACK &&
1078 IS_UP(new_active->dev)) {
1079 /* link up, but waiting for stabilization */
1080 if (new_active->delay < mintime) {
1081 mintime = new_active->delay;
1082 bestslave = new_active;
1091 * change_active_interface - change the active slave into the specified one
1092 * @bond: our bonding struct
1093 * @new: the new slave to make the active one
1095 * Set the new slave to the bond's settings and unset them on the old
1096 * curr_active_slave.
1097 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1099 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1100 * because it is apparently the best available slave we have, even though its
1101 * updelay hasn't timed out yet.
1103 * If new_active is not NULL, caller must hold bond->lock for read and
1104 * curr_slave_lock for write_bh.
1106 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1108 struct slave *old_active = bond->curr_active_slave;
1110 if (old_active == new_active)
1114 new_active->jiffies = jiffies;
1116 if (new_active->link == BOND_LINK_BACK) {
1117 if (USES_PRIMARY(bond->params.mode)) {
1118 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1119 bond->dev->name, new_active->dev->name,
1120 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1123 new_active->delay = 0;
1124 new_active->link = BOND_LINK_UP;
1126 if (bond->params.mode == BOND_MODE_8023AD)
1127 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1129 if (bond_is_lb(bond))
1130 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1132 if (USES_PRIMARY(bond->params.mode)) {
1133 pr_info("%s: making interface %s the new active one.\n",
1134 bond->dev->name, new_active->dev->name);
1139 if (USES_PRIMARY(bond->params.mode))
1140 bond_mc_swap(bond, new_active, old_active);
1142 if (bond_is_lb(bond)) {
1143 bond_alb_handle_active_change(bond, new_active);
1145 bond_set_slave_inactive_flags(old_active);
1147 bond_set_slave_active_flags(new_active);
1149 bond->curr_active_slave = new_active;
1152 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1154 bond_set_slave_inactive_flags(old_active);
1157 bond_set_slave_active_flags(new_active);
1159 if (bond->params.fail_over_mac)
1160 bond_do_fail_over_mac(bond, new_active,
1163 if (netif_running(bond->dev)) {
1164 bond->send_grat_arp = bond->params.num_grat_arp;
1165 bond_send_gratuitous_arp(bond);
1167 bond->send_unsol_na = bond->params.num_unsol_na;
1168 bond_send_unsolicited_na(bond);
1171 write_unlock_bh(&bond->curr_slave_lock);
1172 read_unlock(&bond->lock);
1174 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
1176 read_lock(&bond->lock);
1177 write_lock_bh(&bond->curr_slave_lock);
1181 /* resend IGMP joins since active slave has changed or
1182 * all were sent on curr_active_slave */
1183 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1184 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1185 netif_running(bond->dev)) {
1186 bond->igmp_retrans = bond->params.resend_igmp;
1187 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
1192 * bond_select_active_slave - select a new active slave, if needed
1193 * @bond: our bonding struct
1195 * This functions should be called when one of the following occurs:
1196 * - The old curr_active_slave has been released or lost its link.
1197 * - The primary_slave has got its link back.
1198 * - A slave has got its link back and there's no old curr_active_slave.
1200 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1202 void bond_select_active_slave(struct bonding *bond)
1204 struct slave *best_slave;
1207 best_slave = bond_find_best_slave(bond);
1208 if (best_slave != bond->curr_active_slave) {
1209 bond_change_active_slave(bond, best_slave);
1210 rv = bond_set_carrier(bond);
1214 if (netif_carrier_ok(bond->dev)) {
1215 pr_info("%s: first active interface up!\n",
1218 pr_info("%s: now running without any active interface !\n",
1224 /*--------------------------- slave list handling ---------------------------*/
1227 * This function attaches the slave to the end of list.
1229 * bond->lock held for writing by caller.
1231 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1233 if (bond->first_slave == NULL) { /* attaching the first slave */
1234 new_slave->next = new_slave;
1235 new_slave->prev = new_slave;
1236 bond->first_slave = new_slave;
1238 new_slave->next = bond->first_slave;
1239 new_slave->prev = bond->first_slave->prev;
1240 new_slave->next->prev = new_slave;
1241 new_slave->prev->next = new_slave;
1248 * This function detaches the slave from the list.
1249 * WARNING: no check is made to verify if the slave effectively
1250 * belongs to <bond>.
1251 * Nothing is freed on return, structures are just unchained.
1252 * If any slave pointer in bond was pointing to <slave>,
1253 * it should be changed by the calling function.
1255 * bond->lock held for writing by caller.
1257 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1260 slave->next->prev = slave->prev;
1263 slave->prev->next = slave->next;
1265 if (bond->first_slave == slave) { /* slave is the first slave */
1266 if (bond->slave_cnt > 1) { /* there are more slave */
1267 bond->first_slave = slave->next;
1269 bond->first_slave = NULL; /* slave was the last one */
1278 #ifdef CONFIG_NET_POLL_CONTROLLER
1279 static inline int slave_enable_netpoll(struct slave *slave)
1284 np = kzalloc(sizeof(*np), GFP_KERNEL);
1289 np->dev = slave->dev;
1290 err = __netpoll_setup(np);
1299 static inline void slave_disable_netpoll(struct slave *slave)
1301 struct netpoll *np = slave->np;
1307 synchronize_rcu_bh();
1308 __netpoll_cleanup(np);
1311 static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1313 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1315 if (!slave_dev->netdev_ops->ndo_poll_controller)
1320 static void bond_poll_controller(struct net_device *bond_dev)
1324 static void __bond_netpoll_cleanup(struct bonding *bond)
1326 struct slave *slave;
1329 bond_for_each_slave(bond, slave, i)
1330 if (IS_UP(slave->dev))
1331 slave_disable_netpoll(slave);
1333 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1335 struct bonding *bond = netdev_priv(bond_dev);
1337 read_lock(&bond->lock);
1338 __bond_netpoll_cleanup(bond);
1339 read_unlock(&bond->lock);
1342 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1344 struct bonding *bond = netdev_priv(dev);
1345 struct slave *slave;
1348 read_lock(&bond->lock);
1349 bond_for_each_slave(bond, slave, i) {
1350 if (!IS_UP(slave->dev))
1352 err = slave_enable_netpoll(slave);
1354 __bond_netpoll_cleanup(bond);
1358 read_unlock(&bond->lock);
1362 static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1364 return bond->dev->npinfo;
1368 static inline int slave_enable_netpoll(struct slave *slave)
1372 static inline void slave_disable_netpoll(struct slave *slave)
1375 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1380 /*---------------------------------- IOCTL ----------------------------------*/
1382 static int bond_sethwaddr(struct net_device *bond_dev,
1383 struct net_device *slave_dev)
1385 pr_debug("bond_dev=%p\n", bond_dev);
1386 pr_debug("slave_dev=%p\n", slave_dev);
1387 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1388 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1392 #define BOND_VLAN_FEATURES \
1393 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1394 NETIF_F_HW_VLAN_FILTER)
1397 * Compute the common dev->feature set available to all slaves. Some
1398 * feature bits are managed elsewhere, so preserve those feature bits
1399 * on the master device.
1401 static int bond_compute_features(struct bonding *bond)
1403 struct slave *slave;
1404 struct net_device *bond_dev = bond->dev;
1405 u32 features = bond_dev->features;
1406 u32 vlan_features = 0;
1407 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1408 bond_dev->hard_header_len);
1411 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1412 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1414 if (!bond->first_slave)
1417 features &= ~NETIF_F_ONE_FOR_ALL;
1419 vlan_features = bond->first_slave->dev->vlan_features;
1420 bond_for_each_slave(bond, slave, i) {
1421 features = netdev_increment_features(features,
1422 slave->dev->features,
1423 NETIF_F_ONE_FOR_ALL);
1424 vlan_features = netdev_increment_features(vlan_features,
1425 slave->dev->vlan_features,
1426 NETIF_F_ONE_FOR_ALL);
1427 if (slave->dev->hard_header_len > max_hard_header_len)
1428 max_hard_header_len = slave->dev->hard_header_len;
1432 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1433 bond_dev->features = netdev_fix_features(bond_dev, features);
1434 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
1435 bond_dev->hard_header_len = max_hard_header_len;
1440 static void bond_setup_by_slave(struct net_device *bond_dev,
1441 struct net_device *slave_dev)
1443 struct bonding *bond = netdev_priv(bond_dev);
1445 bond_dev->header_ops = slave_dev->header_ops;
1447 bond_dev->type = slave_dev->type;
1448 bond_dev->hard_header_len = slave_dev->hard_header_len;
1449 bond_dev->addr_len = slave_dev->addr_len;
1451 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1452 slave_dev->addr_len);
1453 bond->setup_by_slave = 1;
1456 /* On bonding slaves other than the currently active slave, suppress
1457 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
1458 * ARP on active-backup slaves with arp_validate enabled.
1460 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1461 struct slave *slave,
1462 struct bonding *bond)
1464 if (bond_is_slave_inactive(slave)) {
1465 if (slave_do_arp_validate(bond, slave) &&
1466 skb->protocol == __cpu_to_be16(ETH_P_ARP))
1469 if (bond->params.mode == BOND_MODE_ALB &&
1470 skb->pkt_type != PACKET_BROADCAST &&
1471 skb->pkt_type != PACKET_MULTICAST)
1474 if (bond->params.mode == BOND_MODE_8023AD &&
1475 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
1483 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1485 struct sk_buff *skb = *pskb;
1486 struct slave *slave;
1487 struct net_device *bond_dev;
1488 struct bonding *bond;
1490 slave = bond_slave_get_rcu(skb->dev);
1491 bond_dev = ACCESS_ONCE(slave->dev->master);
1492 if (unlikely(!bond_dev))
1493 return RX_HANDLER_PASS;
1495 skb = skb_share_check(skb, GFP_ATOMIC);
1497 return RX_HANDLER_CONSUMED;
1501 bond = netdev_priv(bond_dev);
1503 if (bond->params.arp_interval)
1504 slave->dev->last_rx = jiffies;
1506 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1507 return RX_HANDLER_EXACT;
1510 skb->dev = bond_dev;
1512 if (bond->params.mode == BOND_MODE_ALB &&
1513 bond_dev->priv_flags & IFF_BRIDGE_PORT &&
1514 skb->pkt_type == PACKET_HOST) {
1516 if (unlikely(skb_cow_head(skb,
1517 skb->data - skb_mac_header(skb)))) {
1519 return RX_HANDLER_CONSUMED;
1521 memcpy(eth_hdr(skb)->h_dest, bond_dev->dev_addr, ETH_ALEN);
1524 return RX_HANDLER_ANOTHER;
1527 /* enslave device <slave> to bond device <master> */
1528 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1530 struct bonding *bond = netdev_priv(bond_dev);
1531 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1532 struct slave *new_slave = NULL;
1533 struct netdev_hw_addr *ha;
1534 struct sockaddr addr;
1536 int old_features = bond_dev->features;
1539 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1540 slave_ops->ndo_do_ioctl == NULL) {
1541 pr_warning("%s: Warning: no link monitoring support for %s\n",
1542 bond_dev->name, slave_dev->name);
1545 /* bond must be initialized by bond_open() before enslaving */
1546 if (!(bond_dev->flags & IFF_UP)) {
1547 pr_warning("%s: master_dev is not up in bond_enslave\n",
1551 /* already enslaved */
1552 if (slave_dev->flags & IFF_SLAVE) {
1553 pr_debug("Error, Device was already enslaved\n");
1557 /* vlan challenged mutual exclusion */
1558 /* no need to lock since we're protected by rtnl_lock */
1559 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1560 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1562 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1563 bond_dev->name, slave_dev->name, bond_dev->name);
1566 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1567 bond_dev->name, slave_dev->name,
1568 slave_dev->name, bond_dev->name);
1569 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1572 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1573 if (bond->slave_cnt == 0) {
1574 /* First slave, and it is not VLAN challenged,
1575 * so remove the block of adding VLANs over the bond.
1577 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1582 * Old ifenslave binaries are no longer supported. These can
1583 * be identified with moderate accuracy by the state of the slave:
1584 * the current ifenslave will set the interface down prior to
1585 * enslaving it; the old ifenslave will not.
1587 if ((slave_dev->flags & IFF_UP)) {
1588 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
1591 goto err_undo_flags;
1594 /* set bonding device ether type by slave - bonding netdevices are
1595 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1596 * there is a need to override some of the type dependent attribs/funcs.
1598 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1599 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1601 if (bond->slave_cnt == 0) {
1602 if (bond_dev->type != slave_dev->type) {
1603 pr_debug("%s: change device type from %d to %d\n",
1605 bond_dev->type, slave_dev->type);
1607 res = netdev_bonding_change(bond_dev,
1608 NETDEV_PRE_TYPE_CHANGE);
1609 res = notifier_to_errno(res);
1611 pr_err("%s: refused to change device type\n",
1614 goto err_undo_flags;
1617 /* Flush unicast and multicast addresses */
1618 dev_uc_flush(bond_dev);
1619 dev_mc_flush(bond_dev);
1621 if (slave_dev->type != ARPHRD_ETHER)
1622 bond_setup_by_slave(bond_dev, slave_dev);
1624 ether_setup(bond_dev);
1626 netdev_bonding_change(bond_dev,
1627 NETDEV_POST_TYPE_CHANGE);
1629 } else if (bond_dev->type != slave_dev->type) {
1630 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1632 slave_dev->type, bond_dev->type);
1634 goto err_undo_flags;
1637 if (slave_ops->ndo_set_mac_address == NULL) {
1638 if (bond->slave_cnt == 0) {
1639 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1641 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1642 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1643 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1646 goto err_undo_flags;
1650 /* If this is the first slave, then we need to set the master's hardware
1651 * address to be the same as the slave's. */
1652 if (is_zero_ether_addr(bond->dev->dev_addr))
1653 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1654 slave_dev->addr_len);
1657 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1660 goto err_undo_flags;
1664 * Set the new_slave's queue_id to be zero. Queue ID mapping
1665 * is set via sysfs or module option if desired.
1667 new_slave->queue_id = 0;
1669 /* Save slave's original mtu and then set it to match the bond */
1670 new_slave->original_mtu = slave_dev->mtu;
1671 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1673 pr_debug("Error %d calling dev_set_mtu\n", res);
1678 * Save slave's original ("permanent") mac address for modes
1679 * that need it, and for restoring it upon release, and then
1680 * set it to the master's address
1682 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1684 if (!bond->params.fail_over_mac) {
1686 * Set slave to master's mac address. The application already
1687 * set the master's mac address to that of the first slave
1689 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1690 addr.sa_family = slave_dev->type;
1691 res = dev_set_mac_address(slave_dev, &addr);
1693 pr_debug("Error %d calling set_mac_address\n", res);
1694 goto err_restore_mtu;
1698 res = netdev_set_bond_master(slave_dev, bond_dev);
1700 pr_debug("Error %d calling netdev_set_bond_master\n", res);
1701 goto err_restore_mac;
1703 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1706 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1707 goto err_unset_master;
1710 /* open the slave since the application closed it */
1711 res = dev_open(slave_dev);
1713 pr_debug("Opening slave %s failed\n", slave_dev->name);
1714 goto err_unreg_rxhandler;
1717 new_slave->dev = slave_dev;
1718 slave_dev->priv_flags |= IFF_BONDING;
1720 if (bond_is_lb(bond)) {
1721 /* bond_alb_init_slave() must be called before all other stages since
1722 * it might fail and we do not want to have to undo everything
1724 res = bond_alb_init_slave(bond, new_slave);
1729 /* If the mode USES_PRIMARY, then the new slave gets the
1730 * master's promisc (and mc) settings only if it becomes the
1731 * curr_active_slave, and that is taken care of later when calling
1732 * bond_change_active()
1734 if (!USES_PRIMARY(bond->params.mode)) {
1735 /* set promiscuity level to new slave */
1736 if (bond_dev->flags & IFF_PROMISC) {
1737 res = dev_set_promiscuity(slave_dev, 1);
1742 /* set allmulti level to new slave */
1743 if (bond_dev->flags & IFF_ALLMULTI) {
1744 res = dev_set_allmulti(slave_dev, 1);
1749 netif_addr_lock_bh(bond_dev);
1750 /* upload master's mc_list to new slave */
1751 netdev_for_each_mc_addr(ha, bond_dev)
1752 dev_mc_add(slave_dev, ha->addr);
1753 netif_addr_unlock_bh(bond_dev);
1756 if (bond->params.mode == BOND_MODE_8023AD) {
1757 /* add lacpdu mc addr to mc list */
1758 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1760 dev_mc_add(slave_dev, lacpdu_multicast);
1763 bond_add_vlans_on_slave(bond, slave_dev);
1765 write_lock_bh(&bond->lock);
1767 bond_attach_slave(bond, new_slave);
1769 new_slave->delay = 0;
1770 new_slave->link_failure_count = 0;
1772 bond_compute_features(bond);
1774 write_unlock_bh(&bond->lock);
1776 read_lock(&bond->lock);
1778 new_slave->last_arp_rx = jiffies;
1780 if (bond->params.miimon && !bond->params.use_carrier) {
1781 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1783 if ((link_reporting == -1) && !bond->params.arp_interval) {
1785 * miimon is set but a bonded network driver
1786 * does not support ETHTOOL/MII and
1787 * arp_interval is not set. Note: if
1788 * use_carrier is enabled, we will never go
1789 * here (because netif_carrier is always
1790 * supported); thus, we don't need to change
1791 * the messages for netif_carrier.
1793 pr_warning("%s: Warning: MII and ETHTOOL support not available for interface %s, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details.\n",
1794 bond_dev->name, slave_dev->name);
1795 } else if (link_reporting == -1) {
1796 /* unable get link status using mii/ethtool */
1797 pr_warning("%s: Warning: can't get link status from interface %s; 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",
1798 bond_dev->name, slave_dev->name);
1802 /* check for initial state */
1803 if (!bond->params.miimon ||
1804 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1805 if (bond->params.updelay) {
1806 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
1807 new_slave->link = BOND_LINK_BACK;
1808 new_slave->delay = bond->params.updelay;
1810 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
1811 new_slave->link = BOND_LINK_UP;
1813 new_slave->jiffies = jiffies;
1815 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
1816 new_slave->link = BOND_LINK_DOWN;
1819 if (bond_update_speed_duplex(new_slave) &&
1820 (new_slave->link != BOND_LINK_DOWN)) {
1821 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1822 bond_dev->name, new_slave->dev->name);
1824 if (bond->params.mode == BOND_MODE_8023AD) {
1825 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1830 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1831 /* if there is a primary slave, remember it */
1832 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1833 bond->primary_slave = new_slave;
1834 bond->force_primary = true;
1838 write_lock_bh(&bond->curr_slave_lock);
1840 switch (bond->params.mode) {
1841 case BOND_MODE_ACTIVEBACKUP:
1842 bond_set_slave_inactive_flags(new_slave);
1843 bond_select_active_slave(bond);
1845 case BOND_MODE_8023AD:
1846 /* in 802.3ad mode, the internal mechanism
1847 * will activate the slaves in the selected
1850 bond_set_slave_inactive_flags(new_slave);
1851 /* if this is the first slave */
1852 if (bond->slave_cnt == 1) {
1853 SLAVE_AD_INFO(new_slave).id = 1;
1854 /* Initialize AD with the number of times that the AD timer is called in 1 second
1855 * can be called only after the mac address of the bond is set
1857 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1858 bond->params.lacp_fast);
1860 SLAVE_AD_INFO(new_slave).id =
1861 SLAVE_AD_INFO(new_slave->prev).id + 1;
1864 bond_3ad_bind_slave(new_slave);
1868 bond_set_active_slave(new_slave);
1869 bond_set_slave_inactive_flags(new_slave);
1870 bond_select_active_slave(bond);
1873 pr_debug("This slave is always active in trunk mode\n");
1875 /* always active in trunk mode */
1876 bond_set_active_slave(new_slave);
1878 /* In trunking mode there is little meaning to curr_active_slave
1879 * anyway (it holds no special properties of the bond device),
1880 * so we can change it without calling change_active_interface()
1882 if (!bond->curr_active_slave)
1883 bond->curr_active_slave = new_slave;
1886 } /* switch(bond_mode) */
1888 write_unlock_bh(&bond->curr_slave_lock);
1890 bond_set_carrier(bond);
1892 #ifdef CONFIG_NET_POLL_CONTROLLER
1893 slave_dev->npinfo = bond_netpoll_info(bond);
1894 if (slave_dev->npinfo) {
1895 if (slave_enable_netpoll(new_slave)) {
1896 read_unlock(&bond->lock);
1897 pr_info("Error, %s: master_dev is using netpoll, "
1898 "but new slave device does not support netpoll.\n",
1906 read_unlock(&bond->lock);
1908 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1912 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1913 bond_dev->name, slave_dev->name,
1914 bond_is_active_slave(new_slave) ? "n active" : " backup",
1915 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1917 /* enslave is successful */
1920 /* Undo stages on error */
1922 dev_close(slave_dev);
1924 err_unreg_rxhandler:
1925 netdev_rx_handler_unregister(slave_dev);
1929 netdev_set_bond_master(slave_dev, NULL);
1932 if (!bond->params.fail_over_mac) {
1933 /* XXX TODO - fom follow mode needs to change master's
1934 * MAC if this slave's MAC is in use by the bond, or at
1935 * least print a warning.
1937 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1938 addr.sa_family = slave_dev->type;
1939 dev_set_mac_address(slave_dev, &addr);
1943 dev_set_mtu(slave_dev, new_slave->original_mtu);
1949 bond_dev->features = old_features;
1955 * Try to release the slave device <slave> from the bond device <master>
1956 * It is legal to access curr_active_slave without a lock because all the function
1959 * The rules for slave state should be:
1960 * for Active/Backup:
1961 * Active stays on all backups go down
1962 * for Bonded connections:
1963 * The first up interface should be left on and all others downed.
1965 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1967 struct bonding *bond = netdev_priv(bond_dev);
1968 struct slave *slave, *oldcurrent;
1969 struct sockaddr addr;
1971 /* slave is not a slave or master is not master of this slave */
1972 if (!(slave_dev->flags & IFF_SLAVE) ||
1973 (slave_dev->master != bond_dev)) {
1974 pr_err("%s: Error: cannot release %s.\n",
1975 bond_dev->name, slave_dev->name);
1980 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
1981 write_lock_bh(&bond->lock);
1983 slave = bond_get_slave_by_dev(bond, slave_dev);
1985 /* not a slave of this bond */
1986 pr_info("%s: %s not enslaved\n",
1987 bond_dev->name, slave_dev->name);
1988 write_unlock_bh(&bond->lock);
1989 unblock_netpoll_tx();
1993 if (!bond->params.fail_over_mac) {
1994 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1995 bond->slave_cnt > 1)
1996 pr_warning("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n",
1997 bond_dev->name, slave_dev->name,
1999 bond_dev->name, slave_dev->name);
2002 /* Inform AD package of unbinding of slave. */
2003 if (bond->params.mode == BOND_MODE_8023AD) {
2004 /* must be called before the slave is
2005 * detached from the list
2007 bond_3ad_unbind_slave(slave);
2010 pr_info("%s: releasing %s interface %s\n",
2012 bond_is_active_slave(slave) ? "active" : "backup",
2015 oldcurrent = bond->curr_active_slave;
2017 bond->current_arp_slave = NULL;
2019 /* release the slave from its bond */
2020 bond_detach_slave(bond, slave);
2022 bond_compute_features(bond);
2024 if (bond->primary_slave == slave)
2025 bond->primary_slave = NULL;
2027 if (oldcurrent == slave)
2028 bond_change_active_slave(bond, NULL);
2030 if (bond_is_lb(bond)) {
2031 /* Must be called only after the slave has been
2032 * detached from the list and the curr_active_slave
2033 * has been cleared (if our_slave == old_current),
2034 * but before a new active slave is selected.
2036 write_unlock_bh(&bond->lock);
2037 bond_alb_deinit_slave(bond, slave);
2038 write_lock_bh(&bond->lock);
2041 if (oldcurrent == slave) {
2043 * Note that we hold RTNL over this sequence, so there
2044 * is no concern that another slave add/remove event
2047 write_unlock_bh(&bond->lock);
2048 read_lock(&bond->lock);
2049 write_lock_bh(&bond->curr_slave_lock);
2051 bond_select_active_slave(bond);
2053 write_unlock_bh(&bond->curr_slave_lock);
2054 read_unlock(&bond->lock);
2055 write_lock_bh(&bond->lock);
2058 if (bond->slave_cnt == 0) {
2059 bond_set_carrier(bond);
2061 /* if the last slave was removed, zero the mac address
2062 * of the master so it will be set by the application
2063 * to the mac address of the first slave
2065 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2068 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2070 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2071 bond_dev->name, bond_dev->name);
2072 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2075 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2076 !bond_has_challenged_slaves(bond)) {
2077 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2078 bond_dev->name, slave_dev->name, bond_dev->name);
2079 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2082 write_unlock_bh(&bond->lock);
2083 unblock_netpoll_tx();
2085 /* must do this from outside any spinlocks */
2086 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2088 bond_del_vlans_from_slave(bond, slave_dev);
2090 /* If the mode USES_PRIMARY, then we should only remove its
2091 * promisc and mc settings if it was the curr_active_slave, but that was
2092 * already taken care of above when we detached the slave
2094 if (!USES_PRIMARY(bond->params.mode)) {
2095 /* unset promiscuity level from slave */
2096 if (bond_dev->flags & IFF_PROMISC)
2097 dev_set_promiscuity(slave_dev, -1);
2099 /* unset allmulti level from slave */
2100 if (bond_dev->flags & IFF_ALLMULTI)
2101 dev_set_allmulti(slave_dev, -1);
2103 /* flush master's mc_list from slave */
2104 netif_addr_lock_bh(bond_dev);
2105 bond_mc_list_flush(bond_dev, slave_dev);
2106 netif_addr_unlock_bh(bond_dev);
2109 netdev_rx_handler_unregister(slave_dev);
2111 netdev_set_bond_master(slave_dev, NULL);
2113 slave_disable_netpoll(slave);
2115 /* close slave before restoring its mac address */
2116 dev_close(slave_dev);
2118 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
2119 /* restore original ("permanent") mac address */
2120 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2121 addr.sa_family = slave_dev->type;
2122 dev_set_mac_address(slave_dev, &addr);
2125 dev_set_mtu(slave_dev, slave->original_mtu);
2127 slave_dev->priv_flags &= ~IFF_BONDING;
2131 return 0; /* deletion OK */
2135 * First release a slave and than destroy the bond if no more slaves are left.
2136 * Must be under rtnl_lock when this function is called.
2138 static int bond_release_and_destroy(struct net_device *bond_dev,
2139 struct net_device *slave_dev)
2141 struct bonding *bond = netdev_priv(bond_dev);
2144 ret = bond_release(bond_dev, slave_dev);
2145 if ((ret == 0) && (bond->slave_cnt == 0)) {
2146 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2147 pr_info("%s: destroying bond %s.\n",
2148 bond_dev->name, bond_dev->name);
2149 unregister_netdevice(bond_dev);
2155 * This function releases all slaves.
2157 static int bond_release_all(struct net_device *bond_dev)
2159 struct bonding *bond = netdev_priv(bond_dev);
2160 struct slave *slave;
2161 struct net_device *slave_dev;
2162 struct sockaddr addr;
2164 write_lock_bh(&bond->lock);
2166 netif_carrier_off(bond_dev);
2168 if (bond->slave_cnt == 0)
2171 bond->current_arp_slave = NULL;
2172 bond->primary_slave = NULL;
2173 bond_change_active_slave(bond, NULL);
2175 while ((slave = bond->first_slave) != NULL) {
2176 /* Inform AD package of unbinding of slave
2177 * before slave is detached from the list.
2179 if (bond->params.mode == BOND_MODE_8023AD)
2180 bond_3ad_unbind_slave(slave);
2182 slave_dev = slave->dev;
2183 bond_detach_slave(bond, slave);
2185 /* now that the slave is detached, unlock and perform
2186 * all the undo steps that should not be called from
2189 write_unlock_bh(&bond->lock);
2191 if (bond_is_lb(bond)) {
2192 /* must be called only after the slave
2193 * has been detached from the list
2195 bond_alb_deinit_slave(bond, slave);
2198 bond_compute_features(bond);
2200 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2201 bond_del_vlans_from_slave(bond, slave_dev);
2203 /* If the mode USES_PRIMARY, then we should only remove its
2204 * promisc and mc settings if it was the curr_active_slave, but that was
2205 * already taken care of above when we detached the slave
2207 if (!USES_PRIMARY(bond->params.mode)) {
2208 /* unset promiscuity level from slave */
2209 if (bond_dev->flags & IFF_PROMISC)
2210 dev_set_promiscuity(slave_dev, -1);
2212 /* unset allmulti level from slave */
2213 if (bond_dev->flags & IFF_ALLMULTI)
2214 dev_set_allmulti(slave_dev, -1);
2216 /* flush master's mc_list from slave */
2217 netif_addr_lock_bh(bond_dev);
2218 bond_mc_list_flush(bond_dev, slave_dev);
2219 netif_addr_unlock_bh(bond_dev);
2222 netdev_rx_handler_unregister(slave_dev);
2224 netdev_set_bond_master(slave_dev, NULL);
2226 slave_disable_netpoll(slave);
2228 /* close slave before restoring its mac address */
2229 dev_close(slave_dev);
2231 if (!bond->params.fail_over_mac) {
2232 /* restore original ("permanent") mac address*/
2233 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2234 addr.sa_family = slave_dev->type;
2235 dev_set_mac_address(slave_dev, &addr);
2240 /* re-acquire the lock before getting the next slave */
2241 write_lock_bh(&bond->lock);
2244 /* zero the mac address of the master so it will be
2245 * set by the application to the mac address of the
2248 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2251 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2253 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2254 bond_dev->name, bond_dev->name);
2255 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2259 pr_info("%s: released all slaves\n", bond_dev->name);
2262 write_unlock_bh(&bond->lock);
2267 * This function changes the active slave to slave <slave_dev>.
2268 * It returns -EINVAL in the following cases.
2269 * - <slave_dev> is not found in the list.
2270 * - There is not active slave now.
2271 * - <slave_dev> is already active.
2272 * - The link state of <slave_dev> is not BOND_LINK_UP.
2273 * - <slave_dev> is not running.
2274 * In these cases, this function does nothing.
2275 * In the other cases, current_slave pointer is changed and 0 is returned.
2277 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2279 struct bonding *bond = netdev_priv(bond_dev);
2280 struct slave *old_active = NULL;
2281 struct slave *new_active = NULL;
2284 if (!USES_PRIMARY(bond->params.mode))
2287 /* Verify that master_dev is indeed the master of slave_dev */
2288 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
2291 read_lock(&bond->lock);
2293 read_lock(&bond->curr_slave_lock);
2294 old_active = bond->curr_active_slave;
2295 read_unlock(&bond->curr_slave_lock);
2297 new_active = bond_get_slave_by_dev(bond, slave_dev);
2300 * Changing to the current active: do nothing; return success.
2302 if (new_active && (new_active == old_active)) {
2303 read_unlock(&bond->lock);
2309 (new_active->link == BOND_LINK_UP) &&
2310 IS_UP(new_active->dev)) {
2312 write_lock_bh(&bond->curr_slave_lock);
2313 bond_change_active_slave(bond, new_active);
2314 write_unlock_bh(&bond->curr_slave_lock);
2315 unblock_netpoll_tx();
2319 read_unlock(&bond->lock);
2324 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2326 struct bonding *bond = netdev_priv(bond_dev);
2328 info->bond_mode = bond->params.mode;
2329 info->miimon = bond->params.miimon;
2331 read_lock(&bond->lock);
2332 info->num_slaves = bond->slave_cnt;
2333 read_unlock(&bond->lock);
2338 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2340 struct bonding *bond = netdev_priv(bond_dev);
2341 struct slave *slave;
2342 int i, res = -ENODEV;
2344 read_lock(&bond->lock);
2346 bond_for_each_slave(bond, slave, i) {
2347 if (i == (int)info->slave_id) {
2349 strcpy(info->slave_name, slave->dev->name);
2350 info->link = slave->link;
2351 info->state = bond_slave_state(slave);
2352 info->link_failure_count = slave->link_failure_count;
2357 read_unlock(&bond->lock);
2362 /*-------------------------------- Monitoring -------------------------------*/
2365 static int bond_miimon_inspect(struct bonding *bond)
2367 struct slave *slave;
2368 int i, link_state, commit = 0;
2369 bool ignore_updelay;
2371 ignore_updelay = !bond->curr_active_slave ? true : false;
2373 bond_for_each_slave(bond, slave, i) {
2374 slave->new_link = BOND_LINK_NOCHANGE;
2376 link_state = bond_check_dev_link(bond, slave->dev, 0);
2378 switch (slave->link) {
2383 slave->link = BOND_LINK_FAIL;
2384 slave->delay = bond->params.downdelay;
2386 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2388 (bond->params.mode ==
2389 BOND_MODE_ACTIVEBACKUP) ?
2390 (bond_is_active_slave(slave) ?
2391 "active " : "backup ") : "",
2393 bond->params.downdelay * bond->params.miimon);
2396 case BOND_LINK_FAIL:
2399 * recovered before downdelay expired
2401 slave->link = BOND_LINK_UP;
2402 slave->jiffies = jiffies;
2403 pr_info("%s: link status up again after %d ms for interface %s.\n",
2405 (bond->params.downdelay - slave->delay) *
2406 bond->params.miimon,
2411 if (slave->delay <= 0) {
2412 slave->new_link = BOND_LINK_DOWN;
2420 case BOND_LINK_DOWN:
2424 slave->link = BOND_LINK_BACK;
2425 slave->delay = bond->params.updelay;
2428 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2429 bond->dev->name, slave->dev->name,
2430 ignore_updelay ? 0 :
2431 bond->params.updelay *
2432 bond->params.miimon);
2435 case BOND_LINK_BACK:
2437 slave->link = BOND_LINK_DOWN;
2438 pr_info("%s: link status down again after %d ms for interface %s.\n",
2440 (bond->params.updelay - slave->delay) *
2441 bond->params.miimon,
2450 if (slave->delay <= 0) {
2451 slave->new_link = BOND_LINK_UP;
2453 ignore_updelay = false;
2465 static void bond_miimon_commit(struct bonding *bond)
2467 struct slave *slave;
2470 bond_for_each_slave(bond, slave, i) {
2471 switch (slave->new_link) {
2472 case BOND_LINK_NOCHANGE:
2476 slave->link = BOND_LINK_UP;
2477 slave->jiffies = jiffies;
2479 if (bond->params.mode == BOND_MODE_8023AD) {
2480 /* prevent it from being the active one */
2481 bond_set_backup_slave(slave);
2482 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2483 /* make it immediately active */
2484 bond_set_active_slave(slave);
2485 } else if (slave != bond->primary_slave) {
2486 /* prevent it from being the active one */
2487 bond_set_backup_slave(slave);
2490 bond_update_speed_duplex(slave);
2492 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2493 bond->dev->name, slave->dev->name,
2494 slave->speed, slave->duplex ? "full" : "half");
2496 /* notify ad that the link status has changed */
2497 if (bond->params.mode == BOND_MODE_8023AD)
2498 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2500 if (bond_is_lb(bond))
2501 bond_alb_handle_link_change(bond, slave,
2504 if (!bond->curr_active_slave ||
2505 (slave == bond->primary_slave))
2510 case BOND_LINK_DOWN:
2511 if (slave->link_failure_count < UINT_MAX)
2512 slave->link_failure_count++;
2514 slave->link = BOND_LINK_DOWN;
2516 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2517 bond->params.mode == BOND_MODE_8023AD)
2518 bond_set_slave_inactive_flags(slave);
2520 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2521 bond->dev->name, slave->dev->name);
2523 if (bond->params.mode == BOND_MODE_8023AD)
2524 bond_3ad_handle_link_change(slave,
2527 if (bond_is_lb(bond))
2528 bond_alb_handle_link_change(bond, slave,
2531 if (slave == bond->curr_active_slave)
2537 pr_err("%s: invalid new link %d on slave %s\n",
2538 bond->dev->name, slave->new_link,
2540 slave->new_link = BOND_LINK_NOCHANGE;
2548 write_lock_bh(&bond->curr_slave_lock);
2549 bond_select_active_slave(bond);
2550 write_unlock_bh(&bond->curr_slave_lock);
2551 unblock_netpoll_tx();
2554 bond_set_carrier(bond);
2560 * Really a wrapper that splits the mii monitor into two phases: an
2561 * inspection, then (if inspection indicates something needs to be done)
2562 * an acquisition of appropriate locks followed by a commit phase to
2563 * implement whatever link state changes are indicated.
2565 void bond_mii_monitor(struct work_struct *work)
2567 struct bonding *bond = container_of(work, struct bonding,
2570 read_lock(&bond->lock);
2571 if (bond->kill_timers)
2574 if (bond->slave_cnt == 0)
2577 if (bond->send_grat_arp) {
2578 read_lock(&bond->curr_slave_lock);
2579 bond_send_gratuitous_arp(bond);
2580 read_unlock(&bond->curr_slave_lock);
2583 if (bond->send_unsol_na) {
2584 read_lock(&bond->curr_slave_lock);
2585 bond_send_unsolicited_na(bond);
2586 read_unlock(&bond->curr_slave_lock);
2589 if (bond_miimon_inspect(bond)) {
2590 read_unlock(&bond->lock);
2592 read_lock(&bond->lock);
2594 bond_miimon_commit(bond);
2596 read_unlock(&bond->lock);
2597 rtnl_unlock(); /* might sleep, hold no other locks */
2598 read_lock(&bond->lock);
2602 if (bond->params.miimon)
2603 queue_delayed_work(bond->wq, &bond->mii_work,
2604 msecs_to_jiffies(bond->params.miimon));
2606 read_unlock(&bond->lock);
2609 static __be32 bond_glean_dev_ip(struct net_device *dev)
2611 struct in_device *idev;
2612 struct in_ifaddr *ifa;
2619 idev = __in_dev_get_rcu(dev);
2623 ifa = idev->ifa_list;
2627 addr = ifa->ifa_local;
2633 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2635 struct vlan_entry *vlan;
2637 if (ip == bond->master_ip)
2640 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2641 if (ip == vlan->vlan_ip)
2649 * We go to the (large) trouble of VLAN tagging ARP frames because
2650 * switches in VLAN mode (especially if ports are configured as
2651 * "native" to a VLAN) might not pass non-tagged frames.
2653 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2655 struct sk_buff *skb;
2657 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2658 slave_dev->name, dest_ip, src_ip, vlan_id);
2660 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2661 NULL, slave_dev->dev_addr, NULL);
2664 pr_err("ARP packet allocation failed\n");
2668 skb = vlan_put_tag(skb, vlan_id);
2670 pr_err("failed to insert VLAN tag\n");
2678 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2681 __be32 *targets = bond->params.arp_targets;
2682 struct vlan_entry *vlan;
2683 struct net_device *vlan_dev;
2686 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2689 pr_debug("basa: target %x\n", targets[i]);
2691 pr_debug("basa: empty vlan: arp_send\n");
2692 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2693 bond->master_ip, 0);
2698 * If VLANs are configured, we do a route lookup to
2699 * determine which VLAN interface would be used, so we
2700 * can tag the ARP with the proper VLAN tag.
2702 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2705 if (net_ratelimit()) {
2706 pr_warning("%s: no route to arp_ip_target %pI4\n",
2707 bond->dev->name, &targets[i]);
2713 * This target is not on a VLAN
2715 if (rt->dst.dev == bond->dev) {
2717 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2718 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2719 bond->master_ip, 0);
2724 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2725 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2726 if (vlan_dev == rt->dst.dev) {
2727 vlan_id = vlan->vlan_id;
2728 pr_debug("basa: vlan match on %s %d\n",
2729 vlan_dev->name, vlan_id);
2736 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2737 vlan->vlan_ip, vlan_id);
2741 if (net_ratelimit()) {
2742 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2743 bond->dev->name, &targets[i],
2744 rt->dst.dev ? rt->dst.dev->name : "NULL");
2751 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2752 * for each VLAN above us.
2754 * Caller must hold curr_slave_lock for read or better
2756 static void bond_send_gratuitous_arp(struct bonding *bond)
2758 struct slave *slave = bond->curr_active_slave;
2759 struct vlan_entry *vlan;
2760 struct net_device *vlan_dev;
2762 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2763 bond->dev->name, slave ? slave->dev->name : "NULL");
2765 if (!slave || !bond->send_grat_arp ||
2766 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
2769 bond->send_grat_arp--;
2771 if (bond->master_ip) {
2772 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2773 bond->master_ip, 0);
2779 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2780 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2781 if (vlan->vlan_ip) {
2782 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2783 vlan->vlan_ip, vlan->vlan_id);
2788 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2791 __be32 *targets = bond->params.arp_targets;
2793 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2794 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2795 &sip, &tip, i, &targets[i],
2796 bond_has_this_ip(bond, tip));
2797 if (sip == targets[i]) {
2798 if (bond_has_this_ip(bond, tip))
2799 slave->last_arp_rx = jiffies;
2805 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2808 struct slave *slave;
2809 struct bonding *bond;
2810 unsigned char *arp_ptr;
2813 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2815 * When using VLANS and bonding, dev and oriv_dev may be
2816 * incorrect if the physical interface supports VLAN
2817 * acceleration. With this change ARP validation now
2818 * works for hosts only reachable on the VLAN interface.
2820 dev = vlan_dev_real_dev(dev);
2821 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2824 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2827 bond = netdev_priv(dev);
2828 read_lock(&bond->lock);
2830 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2831 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2832 orig_dev ? orig_dev->name : "NULL");
2834 slave = bond_get_slave_by_dev(bond, orig_dev);
2835 if (!slave || !slave_do_arp_validate(bond, slave))
2838 skb = skb_share_check(skb, GFP_ATOMIC);
2842 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2846 if (arp->ar_hln != dev->addr_len ||
2847 skb->pkt_type == PACKET_OTHERHOST ||
2848 skb->pkt_type == PACKET_LOOPBACK ||
2849 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2850 arp->ar_pro != htons(ETH_P_IP) ||
2854 arp_ptr = (unsigned char *)(arp + 1);
2855 arp_ptr += dev->addr_len;
2856 memcpy(&sip, arp_ptr, 4);
2857 arp_ptr += 4 + dev->addr_len;
2858 memcpy(&tip, arp_ptr, 4);
2860 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2861 bond->dev->name, slave->dev->name, bond_slave_state(slave),
2862 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2866 * Backup slaves won't see the ARP reply, but do come through
2867 * here for each ARP probe (so we swap the sip/tip to validate
2868 * the probe). In a "redundant switch, common router" type of
2869 * configuration, the ARP probe will (hopefully) travel from
2870 * the active, through one switch, the router, then the other
2871 * switch before reaching the backup.
2873 if (bond_is_active_slave(slave))
2874 bond_validate_arp(bond, slave, sip, tip);
2876 bond_validate_arp(bond, slave, tip, sip);
2879 read_unlock(&bond->lock);
2882 return NET_RX_SUCCESS;
2886 * this function is called regularly to monitor each slave's link
2887 * ensuring that traffic is being sent and received when arp monitoring
2888 * is used in load-balancing mode. if the adapter has been dormant, then an
2889 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2890 * arp monitoring in active backup mode.
2892 void bond_loadbalance_arp_mon(struct work_struct *work)
2894 struct bonding *bond = container_of(work, struct bonding,
2896 struct slave *slave, *oldcurrent;
2897 int do_failover = 0;
2901 read_lock(&bond->lock);
2903 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2905 if (bond->kill_timers)
2908 if (bond->slave_cnt == 0)
2911 read_lock(&bond->curr_slave_lock);
2912 oldcurrent = bond->curr_active_slave;
2913 read_unlock(&bond->curr_slave_lock);
2915 /* see if any of the previous devices are up now (i.e. they have
2916 * xmt and rcv traffic). the curr_active_slave does not come into
2917 * the picture unless it is null. also, slave->jiffies is not needed
2918 * here because we send an arp on each slave and give a slave as
2919 * long as it needs to get the tx/rx within the delta.
2920 * TODO: what about up/down delay in arp mode? it wasn't here before
2923 bond_for_each_slave(bond, slave, i) {
2924 unsigned long trans_start = dev_trans_start(slave->dev);
2926 if (slave->link != BOND_LINK_UP) {
2927 if (time_in_range(jiffies,
2928 trans_start - delta_in_ticks,
2929 trans_start + delta_in_ticks) &&
2930 time_in_range(jiffies,
2931 slave->dev->last_rx - delta_in_ticks,
2932 slave->dev->last_rx + delta_in_ticks)) {
2934 slave->link = BOND_LINK_UP;
2935 bond_set_active_slave(slave);
2937 /* primary_slave has no meaning in round-robin
2938 * mode. the window of a slave being up and
2939 * curr_active_slave being null after enslaving
2943 pr_info("%s: link status definitely up for interface %s, ",
2948 pr_info("%s: interface %s is now up\n",
2954 /* slave->link == BOND_LINK_UP */
2956 /* not all switches will respond to an arp request
2957 * when the source ip is 0, so don't take the link down
2958 * if we don't know our ip yet
2960 if (!time_in_range(jiffies,
2961 trans_start - delta_in_ticks,
2962 trans_start + 2 * delta_in_ticks) ||
2963 !time_in_range(jiffies,
2964 slave->dev->last_rx - delta_in_ticks,
2965 slave->dev->last_rx + 2 * delta_in_ticks)) {
2967 slave->link = BOND_LINK_DOWN;
2968 bond_set_backup_slave(slave);
2970 if (slave->link_failure_count < UINT_MAX)
2971 slave->link_failure_count++;
2973 pr_info("%s: interface %s is now down.\n",
2977 if (slave == oldcurrent)
2982 /* note: if switch is in round-robin mode, all links
2983 * must tx arp to ensure all links rx an arp - otherwise
2984 * links may oscillate or not come up at all; if switch is
2985 * in something like xor mode, there is nothing we can
2986 * do - all replies will be rx'ed on same link causing slaves
2987 * to be unstable during low/no traffic periods
2989 if (IS_UP(slave->dev))
2990 bond_arp_send_all(bond, slave);
2995 write_lock_bh(&bond->curr_slave_lock);
2997 bond_select_active_slave(bond);
2999 write_unlock_bh(&bond->curr_slave_lock);
3000 unblock_netpoll_tx();
3004 if (bond->params.arp_interval)
3005 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3007 read_unlock(&bond->lock);
3011 * Called to inspect slaves for active-backup mode ARP monitor link state
3012 * changes. Sets new_link in slaves to specify what action should take
3013 * place for the slave. Returns 0 if no changes are found, >0 if changes
3014 * to link states must be committed.
3016 * Called with bond->lock held for read.
3018 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
3020 struct slave *slave;
3022 unsigned long trans_start;
3024 bond_for_each_slave(bond, slave, i) {
3025 slave->new_link = BOND_LINK_NOCHANGE;
3027 if (slave->link != BOND_LINK_UP) {
3028 if (time_in_range(jiffies,
3029 slave_last_rx(bond, slave) - delta_in_ticks,
3030 slave_last_rx(bond, slave) + delta_in_ticks)) {
3032 slave->new_link = BOND_LINK_UP;
3040 * Give slaves 2*delta after being enslaved or made
3041 * active. This avoids bouncing, as the last receive
3042 * times need a full ARP monitor cycle to be updated.
3044 if (time_in_range(jiffies,
3045 slave->jiffies - delta_in_ticks,
3046 slave->jiffies + 2 * delta_in_ticks))
3050 * Backup slave is down if:
3051 * - No current_arp_slave AND
3052 * - more than 3*delta since last receive AND
3053 * - the bond has an IP address
3055 * Note: a non-null current_arp_slave indicates
3056 * the curr_active_slave went down and we are
3057 * searching for a new one; under this condition
3058 * we only take the curr_active_slave down - this
3059 * gives each slave a chance to tx/rx traffic
3060 * before being taken out
3062 if (!bond_is_active_slave(slave) &&
3063 !bond->current_arp_slave &&
3064 !time_in_range(jiffies,
3065 slave_last_rx(bond, slave) - delta_in_ticks,
3066 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3068 slave->new_link = BOND_LINK_DOWN;
3073 * Active slave is down if:
3074 * - more than 2*delta since transmitting OR
3075 * - (more than 2*delta since receive AND
3076 * the bond has an IP address)
3078 trans_start = dev_trans_start(slave->dev);
3079 if (bond_is_active_slave(slave) &&
3080 (!time_in_range(jiffies,
3081 trans_start - delta_in_ticks,
3082 trans_start + 2 * delta_in_ticks) ||
3083 !time_in_range(jiffies,
3084 slave_last_rx(bond, slave) - delta_in_ticks,
3085 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3087 slave->new_link = BOND_LINK_DOWN;
3096 * Called to commit link state changes noted by inspection step of
3097 * active-backup mode ARP monitor.
3099 * Called with RTNL and bond->lock for read.
3101 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3103 struct slave *slave;
3105 unsigned long trans_start;
3107 bond_for_each_slave(bond, slave, i) {
3108 switch (slave->new_link) {
3109 case BOND_LINK_NOCHANGE:
3113 trans_start = dev_trans_start(slave->dev);
3114 if ((!bond->curr_active_slave &&
3115 time_in_range(jiffies,
3116 trans_start - delta_in_ticks,
3117 trans_start + delta_in_ticks)) ||
3118 bond->curr_active_slave != slave) {
3119 slave->link = BOND_LINK_UP;
3120 bond->current_arp_slave = NULL;
3122 pr_info("%s: link status definitely up for interface %s.\n",
3123 bond->dev->name, slave->dev->name);
3125 if (!bond->curr_active_slave ||
3126 (slave == bond->primary_slave))
3133 case BOND_LINK_DOWN:
3134 if (slave->link_failure_count < UINT_MAX)
3135 slave->link_failure_count++;
3137 slave->link = BOND_LINK_DOWN;
3138 bond_set_slave_inactive_flags(slave);
3140 pr_info("%s: link status definitely down for interface %s, disabling it\n",
3141 bond->dev->name, slave->dev->name);
3143 if (slave == bond->curr_active_slave) {
3144 bond->current_arp_slave = NULL;
3151 pr_err("%s: impossible: new_link %d on slave %s\n",
3152 bond->dev->name, slave->new_link,
3160 write_lock_bh(&bond->curr_slave_lock);
3161 bond_select_active_slave(bond);
3162 write_unlock_bh(&bond->curr_slave_lock);
3163 unblock_netpoll_tx();
3166 bond_set_carrier(bond);
3170 * Send ARP probes for active-backup mode ARP monitor.
3172 * Called with bond->lock held for read.
3174 static void bond_ab_arp_probe(struct bonding *bond)
3176 struct slave *slave;
3179 read_lock(&bond->curr_slave_lock);
3181 if (bond->current_arp_slave && bond->curr_active_slave)
3182 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3183 bond->current_arp_slave->dev->name,
3184 bond->curr_active_slave->dev->name);
3186 if (bond->curr_active_slave) {
3187 bond_arp_send_all(bond, bond->curr_active_slave);
3188 read_unlock(&bond->curr_slave_lock);
3192 read_unlock(&bond->curr_slave_lock);
3194 /* if we don't have a curr_active_slave, search for the next available
3195 * backup slave from the current_arp_slave and make it the candidate
3196 * for becoming the curr_active_slave
3199 if (!bond->current_arp_slave) {
3200 bond->current_arp_slave = bond->first_slave;
3201 if (!bond->current_arp_slave)
3205 bond_set_slave_inactive_flags(bond->current_arp_slave);
3207 /* search for next candidate */
3208 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3209 if (IS_UP(slave->dev)) {
3210 slave->link = BOND_LINK_BACK;
3211 bond_set_slave_active_flags(slave);
3212 bond_arp_send_all(bond, slave);
3213 slave->jiffies = jiffies;
3214 bond->current_arp_slave = slave;
3218 /* if the link state is up at this point, we
3219 * mark it down - this can happen if we have
3220 * simultaneous link failures and
3221 * reselect_active_interface doesn't make this
3222 * one the current slave so it is still marked
3223 * up when it is actually down
3225 if (slave->link == BOND_LINK_UP) {
3226 slave->link = BOND_LINK_DOWN;
3227 if (slave->link_failure_count < UINT_MAX)
3228 slave->link_failure_count++;
3230 bond_set_slave_inactive_flags(slave);
3232 pr_info("%s: backup interface %s is now down.\n",
3233 bond->dev->name, slave->dev->name);
3238 void bond_activebackup_arp_mon(struct work_struct *work)
3240 struct bonding *bond = container_of(work, struct bonding,
3244 read_lock(&bond->lock);
3246 if (bond->kill_timers)
3249 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3251 if (bond->slave_cnt == 0)
3254 if (bond->send_grat_arp) {
3255 read_lock(&bond->curr_slave_lock);
3256 bond_send_gratuitous_arp(bond);
3257 read_unlock(&bond->curr_slave_lock);
3260 if (bond->send_unsol_na) {
3261 read_lock(&bond->curr_slave_lock);
3262 bond_send_unsolicited_na(bond);
3263 read_unlock(&bond->curr_slave_lock);
3266 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3267 read_unlock(&bond->lock);
3269 read_lock(&bond->lock);
3271 bond_ab_arp_commit(bond, delta_in_ticks);
3273 read_unlock(&bond->lock);
3275 read_lock(&bond->lock);
3278 bond_ab_arp_probe(bond);
3281 if (bond->params.arp_interval)
3282 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3284 read_unlock(&bond->lock);
3287 /*-------------------------- netdev event handling --------------------------*/
3290 * Change device name
3292 static int bond_event_changename(struct bonding *bond)
3294 bond_remove_proc_entry(bond);
3295 bond_create_proc_entry(bond);
3297 bond_debug_reregister(bond);
3302 static int bond_master_netdev_event(unsigned long event,
3303 struct net_device *bond_dev)
3305 struct bonding *event_bond = netdev_priv(bond_dev);
3308 case NETDEV_CHANGENAME:
3309 return bond_event_changename(event_bond);
3317 static int bond_slave_netdev_event(unsigned long event,
3318 struct net_device *slave_dev)
3320 struct net_device *bond_dev = slave_dev->master;
3321 struct bonding *bond = netdev_priv(bond_dev);
3324 case NETDEV_UNREGISTER:
3326 if (bond->setup_by_slave)
3327 bond_release_and_destroy(bond_dev, slave_dev);
3329 bond_release(bond_dev, slave_dev);
3333 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3334 struct slave *slave;
3336 slave = bond_get_slave_by_dev(bond, slave_dev);
3338 u16 old_speed = slave->speed;
3339 u16 old_duplex = slave->duplex;
3341 bond_update_speed_duplex(slave);
3343 if (bond_is_lb(bond))
3346 if (old_speed != slave->speed)
3347 bond_3ad_adapter_speed_changed(slave);
3348 if (old_duplex != slave->duplex)
3349 bond_3ad_adapter_duplex_changed(slave);
3356 * ... Or is it this?
3359 case NETDEV_CHANGEMTU:
3361 * TODO: Should slaves be allowed to
3362 * independently alter their MTU? For
3363 * an active-backup bond, slaves need
3364 * not be the same type of device, so
3365 * MTUs may vary. For other modes,
3366 * slaves arguably should have the
3367 * same MTUs. To do this, we'd need to
3368 * take over the slave's change_mtu
3369 * function for the duration of their
3373 case NETDEV_CHANGENAME:
3375 * TODO: handle changing the primary's name
3378 case NETDEV_FEAT_CHANGE:
3379 bond_compute_features(bond);
3389 * bond_netdev_event: handle netdev notifier chain events.
3391 * This function receives events for the netdev chain. The caller (an
3392 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3393 * locks for us to safely manipulate the slave devices (RTNL lock,
3396 static int bond_netdev_event(struct notifier_block *this,
3397 unsigned long event, void *ptr)
3399 struct net_device *event_dev = (struct net_device *)ptr;
3401 pr_debug("event_dev: %s, event: %lx\n",
3402 event_dev ? event_dev->name : "None",
3405 if (!(event_dev->priv_flags & IFF_BONDING))
3408 if (event_dev->flags & IFF_MASTER) {
3409 pr_debug("IFF_MASTER\n");
3410 return bond_master_netdev_event(event, event_dev);
3413 if (event_dev->flags & IFF_SLAVE) {
3414 pr_debug("IFF_SLAVE\n");
3415 return bond_slave_netdev_event(event, event_dev);
3422 * bond_inetaddr_event: handle inetaddr notifier chain events.
3424 * We keep track of device IPs primarily to use as source addresses in
3425 * ARP monitor probes (rather than spewing out broadcasts all the time).
3427 * We track one IP for the main device (if it has one), plus one per VLAN.
3429 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3431 struct in_ifaddr *ifa = ptr;
3432 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3433 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
3434 struct bonding *bond;
3435 struct vlan_entry *vlan;
3437 list_for_each_entry(bond, &bn->dev_list, bond_list) {
3438 if (bond->dev == event_dev) {
3441 bond->master_ip = ifa->ifa_local;
3444 bond->master_ip = bond_glean_dev_ip(bond->dev);
3451 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3454 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3455 if (vlan_dev == event_dev) {
3458 vlan->vlan_ip = ifa->ifa_local;
3462 bond_glean_dev_ip(vlan_dev);
3473 static struct notifier_block bond_netdev_notifier = {
3474 .notifier_call = bond_netdev_event,
3477 static struct notifier_block bond_inetaddr_notifier = {
3478 .notifier_call = bond_inetaddr_event,
3481 /*-------------------------- Packet type handling ---------------------------*/
3483 /* register to receive lacpdus on a bond */
3484 static void bond_register_lacpdu(struct bonding *bond)
3486 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3488 /* initialize packet type */
3489 pk_type->type = PKT_TYPE_LACPDU;
3490 pk_type->dev = bond->dev;
3491 pk_type->func = bond_3ad_lacpdu_recv;
3493 dev_add_pack(pk_type);
3496 /* unregister to receive lacpdus on a bond */
3497 static void bond_unregister_lacpdu(struct bonding *bond)
3499 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3502 void bond_register_arp(struct bonding *bond)
3504 struct packet_type *pt = &bond->arp_mon_pt;
3509 pt->type = htons(ETH_P_ARP);
3510 pt->dev = bond->dev;
3511 pt->func = bond_arp_rcv;
3515 void bond_unregister_arp(struct bonding *bond)
3517 struct packet_type *pt = &bond->arp_mon_pt;
3519 dev_remove_pack(pt);
3523 /*---------------------------- Hashing Policies -----------------------------*/
3526 * Hash for the output device based upon layer 2 and layer 3 data. If
3527 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3529 static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
3531 struct ethhdr *data = (struct ethhdr *)skb->data;
3532 struct iphdr *iph = ip_hdr(skb);
3534 if (skb->protocol == htons(ETH_P_IP)) {
3535 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3536 (data->h_dest[5] ^ data->h_source[5])) % count;
3539 return (data->h_dest[5] ^ data->h_source[5]) % count;
3543 * Hash for the output device based upon layer 3 and layer 4 data. If
3544 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3545 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3547 static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
3549 struct ethhdr *data = (struct ethhdr *)skb->data;
3550 struct iphdr *iph = ip_hdr(skb);
3551 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3554 if (skb->protocol == htons(ETH_P_IP)) {
3555 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
3556 (iph->protocol == IPPROTO_TCP ||
3557 iph->protocol == IPPROTO_UDP)) {
3558 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3560 return (layer4_xor ^
3561 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3565 return (data->h_dest[5] ^ data->h_source[5]) % count;
3569 * Hash for the output device based upon layer 2 data
3571 static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
3573 struct ethhdr *data = (struct ethhdr *)skb->data;
3575 return (data->h_dest[5] ^ data->h_source[5]) % count;
3578 /*-------------------------- Device entry points ----------------------------*/
3580 static int bond_open(struct net_device *bond_dev)
3582 struct bonding *bond = netdev_priv(bond_dev);
3584 bond->kill_timers = 0;
3586 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3588 if (bond_is_lb(bond)) {
3589 /* bond_alb_initialize must be called before the timer
3592 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3593 /* something went wrong - fail the open operation */
3597 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3598 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3601 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3602 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3603 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3606 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3607 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3608 INIT_DELAYED_WORK(&bond->arp_work,
3609 bond_activebackup_arp_mon);
3611 INIT_DELAYED_WORK(&bond->arp_work,
3612 bond_loadbalance_arp_mon);
3614 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3615 if (bond->params.arp_validate)
3616 bond_register_arp(bond);
3619 if (bond->params.mode == BOND_MODE_8023AD) {
3620 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3621 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3622 /* register to receive LACPDUs */
3623 bond_register_lacpdu(bond);
3624 bond_3ad_initiate_agg_selection(bond, 1);
3630 static int bond_close(struct net_device *bond_dev)
3632 struct bonding *bond = netdev_priv(bond_dev);
3634 if (bond->params.mode == BOND_MODE_8023AD) {
3635 /* Unregister the receive of LACPDUs */
3636 bond_unregister_lacpdu(bond);
3639 if (bond->params.arp_validate)
3640 bond_unregister_arp(bond);
3642 write_lock_bh(&bond->lock);
3644 bond->send_grat_arp = 0;
3645 bond->send_unsol_na = 0;
3647 /* signal timers not to re-arm */
3648 bond->kill_timers = 1;
3650 write_unlock_bh(&bond->lock);
3652 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3653 cancel_delayed_work(&bond->mii_work);
3656 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3657 cancel_delayed_work(&bond->arp_work);
3660 switch (bond->params.mode) {
3661 case BOND_MODE_8023AD:
3662 cancel_delayed_work(&bond->ad_work);
3666 cancel_delayed_work(&bond->alb_work);
3672 if (delayed_work_pending(&bond->mcast_work))
3673 cancel_delayed_work(&bond->mcast_work);
3675 if (bond_is_lb(bond)) {
3676 /* Must be called only after all
3677 * slaves have been released
3679 bond_alb_deinitialize(bond);
3685 static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3686 struct rtnl_link_stats64 *stats)
3688 struct bonding *bond = netdev_priv(bond_dev);
3689 struct rtnl_link_stats64 temp;
3690 struct slave *slave;
3693 memset(stats, 0, sizeof(*stats));
3695 read_lock_bh(&bond->lock);
3697 bond_for_each_slave(bond, slave, i) {
3698 const struct rtnl_link_stats64 *sstats =
3699 dev_get_stats(slave->dev, &temp);
3701 stats->rx_packets += sstats->rx_packets;
3702 stats->rx_bytes += sstats->rx_bytes;
3703 stats->rx_errors += sstats->rx_errors;
3704 stats->rx_dropped += sstats->rx_dropped;
3706 stats->tx_packets += sstats->tx_packets;
3707 stats->tx_bytes += sstats->tx_bytes;
3708 stats->tx_errors += sstats->tx_errors;
3709 stats->tx_dropped += sstats->tx_dropped;
3711 stats->multicast += sstats->multicast;
3712 stats->collisions += sstats->collisions;
3714 stats->rx_length_errors += sstats->rx_length_errors;
3715 stats->rx_over_errors += sstats->rx_over_errors;
3716 stats->rx_crc_errors += sstats->rx_crc_errors;
3717 stats->rx_frame_errors += sstats->rx_frame_errors;
3718 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3719 stats->rx_missed_errors += sstats->rx_missed_errors;
3721 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3722 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3723 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3724 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3725 stats->tx_window_errors += sstats->tx_window_errors;
3728 read_unlock_bh(&bond->lock);
3733 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3735 struct net_device *slave_dev = NULL;
3736 struct ifbond k_binfo;
3737 struct ifbond __user *u_binfo = NULL;
3738 struct ifslave k_sinfo;
3739 struct ifslave __user *u_sinfo = NULL;
3740 struct mii_ioctl_data *mii = NULL;
3743 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
3755 * We do this again just in case we were called by SIOCGMIIREG
3756 * instead of SIOCGMIIPHY.
3763 if (mii->reg_num == 1) {
3764 struct bonding *bond = netdev_priv(bond_dev);
3766 read_lock(&bond->lock);
3767 read_lock(&bond->curr_slave_lock);
3768 if (netif_carrier_ok(bond->dev))
3769 mii->val_out = BMSR_LSTATUS;
3771 read_unlock(&bond->curr_slave_lock);
3772 read_unlock(&bond->lock);
3776 case BOND_INFO_QUERY_OLD:
3777 case SIOCBONDINFOQUERY:
3778 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3780 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
3783 res = bond_info_query(bond_dev, &k_binfo);
3785 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3789 case BOND_SLAVE_INFO_QUERY_OLD:
3790 case SIOCBONDSLAVEINFOQUERY:
3791 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3793 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
3796 res = bond_slave_info_query(bond_dev, &k_sinfo);
3798 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3807 if (!capable(CAP_NET_ADMIN))
3810 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
3812 pr_debug("slave_dev=%p:\n", slave_dev);
3817 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
3819 case BOND_ENSLAVE_OLD:
3820 case SIOCBONDENSLAVE:
3821 res = bond_enslave(bond_dev, slave_dev);
3823 case BOND_RELEASE_OLD:
3824 case SIOCBONDRELEASE:
3825 res = bond_release(bond_dev, slave_dev);
3827 case BOND_SETHWADDR_OLD:
3828 case SIOCBONDSETHWADDR:
3829 res = bond_sethwaddr(bond_dev, slave_dev);
3831 case BOND_CHANGE_ACTIVE_OLD:
3832 case SIOCBONDCHANGEACTIVE:
3833 res = bond_ioctl_change_active(bond_dev, slave_dev);
3845 static bool bond_addr_in_mc_list(unsigned char *addr,
3846 struct netdev_hw_addr_list *list,
3849 struct netdev_hw_addr *ha;
3851 netdev_hw_addr_list_for_each(ha, list)
3852 if (!memcmp(ha->addr, addr, addrlen))
3858 static void bond_set_multicast_list(struct net_device *bond_dev)
3860 struct bonding *bond = netdev_priv(bond_dev);
3861 struct netdev_hw_addr *ha;
3865 * Do promisc before checking multicast_mode
3867 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
3869 * FIXME: Need to handle the error when one of the multi-slaves
3872 bond_set_promiscuity(bond, 1);
3875 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
3876 bond_set_promiscuity(bond, -1);
3879 /* set allmulti flag to slaves */
3880 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
3882 * FIXME: Need to handle the error when one of the multi-slaves
3885 bond_set_allmulti(bond, 1);
3888 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
3889 bond_set_allmulti(bond, -1);
3892 read_lock(&bond->lock);
3894 bond->flags = bond_dev->flags;
3896 /* looking for addresses to add to slaves' mc list */
3897 netdev_for_each_mc_addr(ha, bond_dev) {
3898 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3899 bond_dev->addr_len);
3901 bond_mc_add(bond, ha->addr);
3904 /* looking for addresses to delete from slaves' list */
3905 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3906 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3907 bond_dev->addr_len);
3909 bond_mc_del(bond, ha->addr);
3912 /* save master's multicast list */
3913 __hw_addr_flush(&bond->mc_list);
3914 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3915 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
3917 read_unlock(&bond->lock);
3920 static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3922 struct bonding *bond = netdev_priv(dev);
3923 struct slave *slave = bond->first_slave;
3926 const struct net_device_ops *slave_ops
3927 = slave->dev->netdev_ops;
3928 if (slave_ops->ndo_neigh_setup)
3929 return slave_ops->ndo_neigh_setup(slave->dev, parms);
3935 * 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, *stop_at;
3944 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
3945 (bond_dev ? bond_dev->name : "None"), new_mtu);
3947 /* Can't hold bond->lock with bh disabled here since
3948 * some base drivers panic. On the other hand we can't
3949 * hold bond->lock without bh disabled because we'll
3950 * deadlock. The only solution is to rely on the fact
3951 * that we're under rtnl_lock here, and the slaves
3952 * list won't change. This doesn't solve the problem
3953 * of setting the slave's MTU while it is
3954 * transmitting, but the assumption is that the base
3955 * driver can handle that.
3957 * TODO: figure out a way to safely iterate the slaves
3958 * list, but without holding a lock around the actual
3959 * call to the base driver.
3962 bond_for_each_slave(bond, slave, i) {
3963 pr_debug("s %p s->p %p c_m %p\n",
3966 slave->dev->netdev_ops->ndo_change_mtu);
3968 res = dev_set_mtu(slave->dev, new_mtu);
3971 /* If we failed to set the slave's mtu to the new value
3972 * we must abort the operation even in ACTIVE_BACKUP
3973 * mode, because if we allow the backup slaves to have
3974 * different mtu values than the active slave we'll
3975 * need to change their mtu when doing a failover. That
3976 * means changing their mtu from timer context, which
3977 * is probably not a good idea.
3979 pr_debug("err %d %s\n", res, slave->dev->name);
3984 bond_dev->mtu = new_mtu;
3989 /* unwind from head to the slave that failed */
3991 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3994 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3996 pr_debug("unwind err %d dev %s\n",
3997 tmp_res, slave->dev->name);
4007 * Note that many devices must be down to change the HW address, and
4008 * downing the master releases all slaves. We can make bonds full of
4009 * bonding devices to test this, however.
4011 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4013 struct bonding *bond = netdev_priv(bond_dev);
4014 struct sockaddr *sa = addr, tmp_sa;
4015 struct slave *slave, *stop_at;
4019 if (bond->params.mode == BOND_MODE_ALB)
4020 return bond_alb_set_mac_address(bond_dev, addr);
4023 pr_debug("bond=%p, name=%s\n",
4024 bond, bond_dev ? bond_dev->name : "None");
4027 * If fail_over_mac is set to active, do nothing and return
4028 * success. Returning an error causes ifenslave to fail.
4030 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
4033 if (!is_valid_ether_addr(sa->sa_data))
4034 return -EADDRNOTAVAIL;
4036 /* Can't hold bond->lock with bh disabled here since
4037 * some base drivers panic. On the other hand we can't
4038 * hold bond->lock without bh disabled because we'll
4039 * deadlock. The only solution is to rely on the fact
4040 * that we're under rtnl_lock here, and the slaves
4041 * list won't change. This doesn't solve the problem
4042 * of setting the slave's hw address while it is
4043 * transmitting, but the assumption is that the base
4044 * driver can handle that.
4046 * TODO: figure out a way to safely iterate the slaves
4047 * list, but without holding a lock around the actual
4048 * call to the base driver.
4051 bond_for_each_slave(bond, slave, i) {
4052 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
4053 pr_debug("slave %p %s\n", slave, slave->dev->name);
4055 if (slave_ops->ndo_set_mac_address == NULL) {
4057 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
4061 res = dev_set_mac_address(slave->dev, addr);
4063 /* TODO: consider downing the slave
4065 * User should expect communications
4066 * breakage anyway until ARP finish
4069 pr_debug("err %d %s\n", res, slave->dev->name);
4075 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4079 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4080 tmp_sa.sa_family = bond_dev->type;
4082 /* unwind from head to the slave that failed */
4084 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4087 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4089 pr_debug("unwind err %d dev %s\n",
4090 tmp_res, slave->dev->name);
4097 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4099 struct bonding *bond = netdev_priv(bond_dev);
4100 struct slave *slave, *start_at;
4101 int i, slave_no, res = 1;
4102 struct iphdr *iph = ip_hdr(skb);
4104 read_lock(&bond->lock);
4106 if (!BOND_IS_OK(bond))
4109 * Start with the curr_active_slave that joined the bond as the
4110 * default for sending IGMP traffic. For failover purposes one
4111 * needs to maintain some consistency for the interface that will
4112 * send the join/membership reports. The curr_active_slave found
4113 * will send all of this type of traffic.
4115 if ((iph->protocol == IPPROTO_IGMP) &&
4116 (skb->protocol == htons(ETH_P_IP))) {
4118 read_lock(&bond->curr_slave_lock);
4119 slave = bond->curr_active_slave;
4120 read_unlock(&bond->curr_slave_lock);
4126 * Concurrent TX may collide on rr_tx_counter; we accept
4127 * that as being rare enough not to justify using an
4130 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4132 bond_for_each_slave(bond, slave, i) {
4140 bond_for_each_slave_from(bond, slave, i, start_at) {
4141 if (IS_UP(slave->dev) &&
4142 (slave->link == BOND_LINK_UP) &&
4143 bond_is_active_slave(slave)) {
4144 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4151 /* no suitable interface, frame not sent */
4154 read_unlock(&bond->lock);
4155 return NETDEV_TX_OK;
4160 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4161 * the bond has a usable interface.
4163 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4165 struct bonding *bond = netdev_priv(bond_dev);
4168 read_lock(&bond->lock);
4169 read_lock(&bond->curr_slave_lock);
4171 if (!BOND_IS_OK(bond))
4174 if (!bond->curr_active_slave)
4177 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4181 /* no suitable interface, frame not sent */
4184 read_unlock(&bond->curr_slave_lock);
4185 read_unlock(&bond->lock);
4186 return NETDEV_TX_OK;
4190 * In bond_xmit_xor() , we determine the output device by using a pre-
4191 * determined xmit_hash_policy(), If the selected device is not enabled,
4192 * find the next active slave.
4194 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4196 struct bonding *bond = netdev_priv(bond_dev);
4197 struct slave *slave, *start_at;
4202 read_lock(&bond->lock);
4204 if (!BOND_IS_OK(bond))
4207 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
4209 bond_for_each_slave(bond, slave, i) {
4217 bond_for_each_slave_from(bond, slave, i, start_at) {
4218 if (IS_UP(slave->dev) &&
4219 (slave->link == BOND_LINK_UP) &&
4220 bond_is_active_slave(slave)) {
4221 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4228 /* no suitable interface, frame not sent */
4231 read_unlock(&bond->lock);
4232 return NETDEV_TX_OK;
4236 * in broadcast mode, we send everything to all usable interfaces.
4238 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4240 struct bonding *bond = netdev_priv(bond_dev);
4241 struct slave *slave, *start_at;
4242 struct net_device *tx_dev = NULL;
4246 read_lock(&bond->lock);
4248 if (!BOND_IS_OK(bond))
4251 read_lock(&bond->curr_slave_lock);
4252 start_at = bond->curr_active_slave;
4253 read_unlock(&bond->curr_slave_lock);
4258 bond_for_each_slave_from(bond, slave, i, start_at) {
4259 if (IS_UP(slave->dev) &&
4260 (slave->link == BOND_LINK_UP) &&
4261 bond_is_active_slave(slave)) {
4263 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4265 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
4270 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4272 dev_kfree_skb(skb2);
4276 tx_dev = slave->dev;
4281 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4285 /* no suitable interface, frame not sent */
4288 /* frame sent to all suitable interfaces */
4289 read_unlock(&bond->lock);
4290 return NETDEV_TX_OK;
4293 /*------------------------- Device initialization ---------------------------*/
4295 static void bond_set_xmit_hash_policy(struct bonding *bond)
4297 switch (bond->params.xmit_policy) {
4298 case BOND_XMIT_POLICY_LAYER23:
4299 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4301 case BOND_XMIT_POLICY_LAYER34:
4302 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4304 case BOND_XMIT_POLICY_LAYER2:
4306 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4312 * Lookup the slave that corresponds to a qid
4314 static inline int bond_slave_override(struct bonding *bond,
4315 struct sk_buff *skb)
4318 struct slave *slave = NULL;
4319 struct slave *check_slave;
4321 read_lock(&bond->lock);
4323 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4326 /* Find out if any slaves have the same mapping as this skb. */
4327 bond_for_each_slave(bond, check_slave, i) {
4328 if (check_slave->queue_id == skb->queue_mapping) {
4329 slave = check_slave;
4334 /* If the slave isn't UP, use default transmit policy. */
4335 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4336 (slave->link == BOND_LINK_UP)) {
4337 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4341 read_unlock(&bond->lock);
4345 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4348 * This helper function exists to help dev_pick_tx get the correct
4349 * destination queue. Using a helper function skips a call to
4350 * skb_tx_hash and will put the skbs in the queue we expect on their
4351 * way down to the bonding driver.
4353 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4355 if (unlikely(txq >= dev->real_num_tx_queues)) {
4357 txq -= dev->real_num_tx_queues;
4358 while (txq >= dev->real_num_tx_queues);
4363 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4365 struct bonding *bond = netdev_priv(dev);
4368 * If we risk deadlock from transmitting this in the
4369 * netpoll path, tell netpoll to queue the frame for later tx
4371 if (is_netpoll_tx_blocked(dev))
4372 return NETDEV_TX_BUSY;
4374 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4375 if (!bond_slave_override(bond, skb))
4376 return NETDEV_TX_OK;
4379 switch (bond->params.mode) {
4380 case BOND_MODE_ROUNDROBIN:
4381 return bond_xmit_roundrobin(skb, dev);
4382 case BOND_MODE_ACTIVEBACKUP:
4383 return bond_xmit_activebackup(skb, dev);
4385 return bond_xmit_xor(skb, dev);
4386 case BOND_MODE_BROADCAST:
4387 return bond_xmit_broadcast(skb, dev);
4388 case BOND_MODE_8023AD:
4389 return bond_3ad_xmit_xor(skb, dev);
4392 return bond_alb_xmit(skb, dev);
4394 /* Should never happen, mode already checked */
4395 pr_err("%s: Error: Unknown bonding mode %d\n",
4396 dev->name, bond->params.mode);
4399 return NETDEV_TX_OK;
4405 * set bond mode specific net device operations
4407 void bond_set_mode_ops(struct bonding *bond, int mode)
4409 struct net_device *bond_dev = bond->dev;
4412 case BOND_MODE_ROUNDROBIN:
4414 case BOND_MODE_ACTIVEBACKUP:
4417 bond_set_xmit_hash_policy(bond);
4419 case BOND_MODE_BROADCAST:
4421 case BOND_MODE_8023AD:
4422 bond_set_xmit_hash_policy(bond);
4429 /* Should never happen, mode already checked */
4430 pr_err("%s: Error: Unknown bonding mode %d\n",
4431 bond_dev->name, mode);
4436 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4437 struct ethtool_drvinfo *drvinfo)
4439 strncpy(drvinfo->driver, DRV_NAME, 32);
4440 strncpy(drvinfo->version, DRV_VERSION, 32);
4441 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4444 static const struct ethtool_ops bond_ethtool_ops = {
4445 .get_drvinfo = bond_ethtool_get_drvinfo,
4446 .get_link = ethtool_op_get_link,
4447 .get_tx_csum = ethtool_op_get_tx_csum,
4448 .get_sg = ethtool_op_get_sg,
4449 .get_tso = ethtool_op_get_tso,
4450 .get_ufo = ethtool_op_get_ufo,
4451 .get_flags = ethtool_op_get_flags,
4454 static const struct net_device_ops bond_netdev_ops = {
4455 .ndo_init = bond_init,
4456 .ndo_uninit = bond_uninit,
4457 .ndo_open = bond_open,
4458 .ndo_stop = bond_close,
4459 .ndo_start_xmit = bond_start_xmit,
4460 .ndo_select_queue = bond_select_queue,
4461 .ndo_get_stats64 = bond_get_stats,
4462 .ndo_do_ioctl = bond_do_ioctl,
4463 .ndo_set_multicast_list = bond_set_multicast_list,
4464 .ndo_change_mtu = bond_change_mtu,
4465 .ndo_set_mac_address = bond_set_mac_address,
4466 .ndo_neigh_setup = bond_neigh_setup,
4467 .ndo_vlan_rx_register = bond_vlan_rx_register,
4468 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4469 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4470 #ifdef CONFIG_NET_POLL_CONTROLLER
4471 .ndo_netpoll_setup = bond_netpoll_setup,
4472 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4473 .ndo_poll_controller = bond_poll_controller,
4475 .ndo_add_slave = bond_enslave,
4476 .ndo_del_slave = bond_release,
4479 static void bond_destructor(struct net_device *bond_dev)
4481 struct bonding *bond = netdev_priv(bond_dev);
4483 destroy_workqueue(bond->wq);
4484 free_netdev(bond_dev);
4487 static void bond_setup(struct net_device *bond_dev)
4489 struct bonding *bond = netdev_priv(bond_dev);
4491 /* initialize rwlocks */
4492 rwlock_init(&bond->lock);
4493 rwlock_init(&bond->curr_slave_lock);
4495 bond->params = bonding_defaults;
4497 /* Initialize pointers */
4498 bond->dev = bond_dev;
4499 INIT_LIST_HEAD(&bond->vlan_list);
4501 /* Initialize the device entry points */
4502 ether_setup(bond_dev);
4503 bond_dev->netdev_ops = &bond_netdev_ops;
4504 bond_dev->ethtool_ops = &bond_ethtool_ops;
4505 bond_set_mode_ops(bond, bond->params.mode);
4507 bond_dev->destructor = bond_destructor;
4509 /* Initialize the device options */
4510 bond_dev->tx_queue_len = 0;
4511 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4512 bond_dev->priv_flags |= IFF_BONDING;
4513 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4515 /* At first, we block adding VLANs. That's the only way to
4516 * prevent problems that occur when adding VLANs over an
4517 * empty bond. The block will be removed once non-challenged
4518 * slaves are enslaved.
4520 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4522 /* don't acquire bond device's netif_tx_lock when
4524 bond_dev->features |= NETIF_F_LLTX;
4526 /* By default, we declare the bond to be fully
4527 * VLAN hardware accelerated capable. Special
4528 * care is taken in the various xmit functions
4529 * when there are slaves that are not hw accel
4532 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4533 NETIF_F_HW_VLAN_RX |
4534 NETIF_F_HW_VLAN_FILTER);
4536 /* By default, we enable GRO on bonding devices.
4537 * Actual support requires lowlevel drivers are GRO ready.
4539 bond_dev->features |= NETIF_F_GRO;
4542 static void bond_work_cancel_all(struct bonding *bond)
4544 write_lock_bh(&bond->lock);
4545 bond->kill_timers = 1;
4546 write_unlock_bh(&bond->lock);
4548 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4549 cancel_delayed_work(&bond->mii_work);
4551 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4552 cancel_delayed_work(&bond->arp_work);
4554 if (bond->params.mode == BOND_MODE_ALB &&
4555 delayed_work_pending(&bond->alb_work))
4556 cancel_delayed_work(&bond->alb_work);
4558 if (bond->params.mode == BOND_MODE_8023AD &&
4559 delayed_work_pending(&bond->ad_work))
4560 cancel_delayed_work(&bond->ad_work);
4562 if (delayed_work_pending(&bond->mcast_work))
4563 cancel_delayed_work(&bond->mcast_work);
4567 * Destroy a bonding device.
4568 * Must be under rtnl_lock when this function is called.
4570 static void bond_uninit(struct net_device *bond_dev)
4572 struct bonding *bond = netdev_priv(bond_dev);
4573 struct vlan_entry *vlan, *tmp;
4575 bond_netpoll_cleanup(bond_dev);
4577 /* Release the bonded slaves */
4578 bond_release_all(bond_dev);
4580 list_del(&bond->bond_list);
4582 bond_work_cancel_all(bond);
4584 bond_remove_proc_entry(bond);
4586 bond_debug_unregister(bond);
4588 __hw_addr_flush(&bond->mc_list);
4590 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4591 list_del(&vlan->vlan_list);
4596 /*------------------------- Module initialization ---------------------------*/
4599 * Convert string input module parms. Accept either the
4600 * number of the mode or its string name. A bit complicated because
4601 * some mode names are substrings of other names, and calls from sysfs
4602 * may have whitespace in the name (trailing newlines, for example).
4604 int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
4606 int modeint = -1, i, rv;
4607 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4609 for (p = (char *)buf; *p; p++)
4610 if (!(isdigit(*p) || isspace(*p)))
4614 rv = sscanf(buf, "%20s", modestr);
4616 rv = sscanf(buf, "%d", &modeint);
4621 for (i = 0; tbl[i].modename; i++) {
4622 if (modeint == tbl[i].mode)
4624 if (strcmp(modestr, tbl[i].modename) == 0)
4631 static int bond_check_params(struct bond_params *params)
4633 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
4636 * Convert string parameters.
4639 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4640 if (bond_mode == -1) {
4641 pr_err("Error: Invalid bonding mode \"%s\"\n",
4642 mode == NULL ? "NULL" : mode);
4647 if (xmit_hash_policy) {
4648 if ((bond_mode != BOND_MODE_XOR) &&
4649 (bond_mode != BOND_MODE_8023AD)) {
4650 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4651 bond_mode_name(bond_mode));
4653 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4655 if (xmit_hashtype == -1) {
4656 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4657 xmit_hash_policy == NULL ? "NULL" :
4665 if (bond_mode != BOND_MODE_8023AD) {
4666 pr_info("lacp_rate param is irrelevant in mode %s\n",
4667 bond_mode_name(bond_mode));
4669 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4670 if (lacp_fast == -1) {
4671 pr_err("Error: Invalid lacp rate \"%s\"\n",
4672 lacp_rate == NULL ? "NULL" : lacp_rate);
4679 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4680 if (params->ad_select == -1) {
4681 pr_err("Error: Invalid ad_select \"%s\"\n",
4682 ad_select == NULL ? "NULL" : ad_select);
4686 if (bond_mode != BOND_MODE_8023AD) {
4687 pr_warning("ad_select param only affects 802.3ad mode\n");
4690 params->ad_select = BOND_AD_STABLE;
4693 if (max_bonds < 0) {
4694 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4695 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4696 max_bonds = BOND_DEFAULT_MAX_BONDS;
4700 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4701 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4702 miimon = BOND_LINK_MON_INTERV;
4706 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4711 if (downdelay < 0) {
4712 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4713 downdelay, INT_MAX);
4717 if ((use_carrier != 0) && (use_carrier != 1)) {
4718 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4723 if (num_grat_arp < 0 || num_grat_arp > 255) {
4724 pr_warning("Warning: num_grat_arp (%d) not in range 0-255 so it was reset to 1\n",
4729 if (num_unsol_na < 0 || num_unsol_na > 255) {
4730 pr_warning("Warning: num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4735 /* reset values for 802.3ad */
4736 if (bond_mode == BOND_MODE_8023AD) {
4738 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
4739 pr_warning("Forcing miimon to 100msec\n");
4744 if (tx_queues < 1 || tx_queues > 255) {
4745 pr_warning("Warning: tx_queues (%d) should be between "
4746 "1 and 255, resetting to %d\n",
4747 tx_queues, BOND_DEFAULT_TX_QUEUES);
4748 tx_queues = BOND_DEFAULT_TX_QUEUES;
4751 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4752 pr_warning("Warning: all_slaves_active module parameter (%d), "
4753 "not of valid value (0/1), so it was set to "
4754 "0\n", all_slaves_active);
4755 all_slaves_active = 0;
4758 if (resend_igmp < 0 || resend_igmp > 255) {
4759 pr_warning("Warning: resend_igmp (%d) should be between "
4760 "0 and 255, resetting to %d\n",
4761 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4762 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4765 /* reset values for TLB/ALB */
4766 if ((bond_mode == BOND_MODE_TLB) ||
4767 (bond_mode == BOND_MODE_ALB)) {
4769 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure and link speed which are essential for TLB/ALB load balancing\n");
4770 pr_warning("Forcing miimon to 100msec\n");
4775 if (bond_mode == BOND_MODE_ALB) {
4776 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",
4781 if (updelay || downdelay) {
4782 /* just warn the user the up/down delay will have
4783 * no effect since miimon is zero...
4785 pr_warning("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",
4786 updelay, downdelay);
4789 /* don't allow arp monitoring */
4791 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4792 miimon, arp_interval);
4796 if ((updelay % miimon) != 0) {
4797 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4799 (updelay / miimon) * miimon);
4804 if ((downdelay % miimon) != 0) {
4805 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4807 (downdelay / miimon) * miimon);
4810 downdelay /= miimon;
4813 if (arp_interval < 0) {
4814 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4815 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4816 arp_interval = BOND_LINK_ARP_INTERV;
4819 for (arp_ip_count = 0;
4820 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4822 /* not complete check, but should be good enough to
4824 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4825 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4826 arp_ip_target[arp_ip_count]);
4829 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4830 arp_target[arp_ip_count] = ip;
4834 if (arp_interval && !arp_ip_count) {
4835 /* don't allow arping if no arp_ip_target given... */
4836 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4842 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4843 pr_err("arp_validate only supported in active-backup mode\n");
4846 if (!arp_interval) {
4847 pr_err("arp_validate requires arp_interval\n");
4851 arp_validate_value = bond_parse_parm(arp_validate,
4853 if (arp_validate_value == -1) {
4854 pr_err("Error: invalid arp_validate \"%s\"\n",
4855 arp_validate == NULL ? "NULL" : arp_validate);
4859 arp_validate_value = 0;
4862 pr_info("MII link monitoring set to %d ms\n", miimon);
4863 } else if (arp_interval) {
4866 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4868 arp_validate_tbl[arp_validate_value].modename,
4871 for (i = 0; i < arp_ip_count; i++)
4872 pr_info(" %s", arp_ip_target[i]);
4876 } else if (max_bonds) {
4877 /* miimon and arp_interval not set, we need one so things
4878 * work as expected, see bonding.txt for details
4880 pr_warning("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");
4883 if (primary && !USES_PRIMARY(bond_mode)) {
4884 /* currently, using a primary only makes sense
4885 * in active backup, TLB or ALB modes
4887 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4888 primary, bond_mode_name(bond_mode));
4892 if (primary && primary_reselect) {
4893 primary_reselect_value = bond_parse_parm(primary_reselect,
4895 if (primary_reselect_value == -1) {
4896 pr_err("Error: Invalid primary_reselect \"%s\"\n",
4898 NULL ? "NULL" : primary_reselect);
4902 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4905 if (fail_over_mac) {
4906 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4908 if (fail_over_mac_value == -1) {
4909 pr_err("Error: invalid fail_over_mac \"%s\"\n",
4910 arp_validate == NULL ? "NULL" : arp_validate);
4914 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
4915 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
4917 fail_over_mac_value = BOND_FOM_NONE;
4920 /* fill params struct with the proper values */
4921 params->mode = bond_mode;
4922 params->xmit_policy = xmit_hashtype;
4923 params->miimon = miimon;
4924 params->num_grat_arp = num_grat_arp;
4925 params->num_unsol_na = num_unsol_na;
4926 params->arp_interval = arp_interval;
4927 params->arp_validate = arp_validate_value;
4928 params->updelay = updelay;
4929 params->downdelay = downdelay;
4930 params->use_carrier = use_carrier;
4931 params->lacp_fast = lacp_fast;
4932 params->primary[0] = 0;
4933 params->primary_reselect = primary_reselect_value;
4934 params->fail_over_mac = fail_over_mac_value;
4935 params->tx_queues = tx_queues;
4936 params->all_slaves_active = all_slaves_active;
4937 params->resend_igmp = resend_igmp;
4940 strncpy(params->primary, primary, IFNAMSIZ);
4941 params->primary[IFNAMSIZ - 1] = 0;
4944 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4949 static struct lock_class_key bonding_netdev_xmit_lock_key;
4950 static struct lock_class_key bonding_netdev_addr_lock_key;
4952 static void bond_set_lockdep_class_one(struct net_device *dev,
4953 struct netdev_queue *txq,
4956 lockdep_set_class(&txq->_xmit_lock,
4957 &bonding_netdev_xmit_lock_key);
4960 static void bond_set_lockdep_class(struct net_device *dev)
4962 lockdep_set_class(&dev->addr_list_lock,
4963 &bonding_netdev_addr_lock_key);
4964 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
4968 * Called from registration process
4970 static int bond_init(struct net_device *bond_dev)
4972 struct bonding *bond = netdev_priv(bond_dev);
4973 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
4975 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4977 bond->wq = create_singlethread_workqueue(bond_dev->name);
4981 bond_set_lockdep_class(bond_dev);
4983 bond_create_proc_entry(bond);
4984 list_add_tail(&bond->bond_list, &bn->dev_list);
4986 bond_prepare_sysfs_group(bond);
4988 bond_debug_register(bond);
4990 __hw_addr_init(&bond->mc_list);
4994 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4996 if (tb[IFLA_ADDRESS]) {
4997 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4999 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5000 return -EADDRNOTAVAIL;
5005 static struct rtnl_link_ops bond_link_ops __read_mostly = {
5007 .priv_size = sizeof(struct bonding),
5008 .setup = bond_setup,
5009 .validate = bond_validate,
5012 /* Create a new bond based on the specified name and bonding parameters.
5013 * If name is NULL, obtain a suitable "bond%d" name for us.
5014 * Caller must NOT hold rtnl_lock; we need to release it here before we
5015 * set up our sysfs entries.
5017 int bond_create(struct net *net, const char *name)
5019 struct net_device *bond_dev;
5024 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5025 bond_setup, tx_queues);
5027 pr_err("%s: eek! can't alloc netdev!\n", name);
5032 dev_net_set(bond_dev, net);
5033 bond_dev->rtnl_link_ops = &bond_link_ops;
5036 res = dev_alloc_name(bond_dev, "bond%d");
5041 * If we're given a name to register
5042 * we need to ensure that its not already
5046 if (__dev_get_by_name(net, name) != NULL)
5050 res = register_netdevice(bond_dev);
5052 netif_carrier_off(bond_dev);
5057 bond_destructor(bond_dev);
5061 static int __net_init bond_net_init(struct net *net)
5063 struct bond_net *bn = net_generic(net, bond_net_id);
5066 INIT_LIST_HEAD(&bn->dev_list);
5068 bond_create_proc_dir(bn);
5073 static void __net_exit bond_net_exit(struct net *net)
5075 struct bond_net *bn = net_generic(net, bond_net_id);
5077 bond_destroy_proc_dir(bn);
5080 static struct pernet_operations bond_net_ops = {
5081 .init = bond_net_init,
5082 .exit = bond_net_exit,
5084 .size = sizeof(struct bond_net),
5087 static int __init bonding_init(void)
5092 pr_info("%s", bond_version);
5094 res = bond_check_params(&bonding_defaults);
5098 res = register_pernet_subsys(&bond_net_ops);
5102 res = rtnl_link_register(&bond_link_ops);
5106 bond_create_debugfs();
5108 for (i = 0; i < max_bonds; i++) {
5109 res = bond_create(&init_net, NULL);
5114 res = bond_create_sysfs();
5118 register_netdevice_notifier(&bond_netdev_notifier);
5119 register_inetaddr_notifier(&bond_inetaddr_notifier);
5120 bond_register_ipv6_notifier();
5124 rtnl_link_unregister(&bond_link_ops);
5126 unregister_pernet_subsys(&bond_net_ops);
5131 static void __exit bonding_exit(void)
5133 unregister_netdevice_notifier(&bond_netdev_notifier);
5134 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
5135 bond_unregister_ipv6_notifier();
5137 bond_destroy_sysfs();
5138 bond_destroy_debugfs();
5140 rtnl_link_unregister(&bond_link_ops);
5141 unregister_pernet_subsys(&bond_net_ops);
5143 #ifdef CONFIG_NET_POLL_CONTROLLER
5145 * Make sure we don't have an imbalance on our netpoll blocking
5147 WARN_ON(atomic_read(&netpoll_block_tx));
5151 module_init(bonding_init);
5152 module_exit(bonding_exit);
5153 MODULE_LICENSE("GPL");
5154 MODULE_VERSION(DRV_VERSION);
5155 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5156 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5157 MODULE_ALIAS_RTNL_LINK("bond");