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/proc_fs.h>
69 #include <linux/seq_file.h>
70 #include <linux/smp.h>
71 #include <linux/if_ether.h>
73 #include <linux/mii.h>
74 #include <linux/ethtool.h>
75 #include <linux/if_vlan.h>
76 #include <linux/if_bonding.h>
77 #include <linux/jiffies.h>
78 #include <linux/preempt.h>
79 #include <net/route.h>
80 #include <net/net_namespace.h>
81 #include <net/netns/generic.h>
86 /*---------------------------- Module parameters ----------------------------*/
88 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
89 #define BOND_LINK_MON_INTERV 0
90 #define BOND_LINK_ARP_INTERV 0
92 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
93 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
94 static int num_grat_arp = 1;
95 static int num_unsol_na = 1;
96 static int miimon = BOND_LINK_MON_INTERV;
99 static int use_carrier = 1;
101 static char *primary;
102 static char *primary_reselect;
103 static char *lacp_rate;
104 static char *ad_select;
105 static char *xmit_hash_policy;
106 static int arp_interval = BOND_LINK_ARP_INTERV;
107 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
108 static char *arp_validate;
109 static char *fail_over_mac;
110 static int all_slaves_active = 0;
111 static struct bond_params bonding_defaults;
112 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
114 module_param(max_bonds, int, 0);
115 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
116 module_param(tx_queues, int, 0);
117 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
118 module_param(num_grat_arp, int, 0644);
119 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
120 module_param(num_unsol_na, int, 0644);
121 MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
122 module_param(miimon, int, 0);
123 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
124 module_param(updelay, int, 0);
125 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
126 module_param(downdelay, int, 0);
127 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
129 module_param(use_carrier, int, 0);
130 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
131 "0 for off, 1 for on (default)");
132 module_param(mode, charp, 0);
133 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
134 "1 for active-backup, 2 for balance-xor, "
135 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
136 "6 for balance-alb");
137 module_param(primary, charp, 0);
138 MODULE_PARM_DESC(primary, "Primary network device to use");
139 module_param(primary_reselect, charp, 0);
140 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
142 "0 for always (default), "
143 "1 for only if speed of primary is "
145 "2 for only on active slave "
147 module_param(lacp_rate, charp, 0);
148 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
150 module_param(ad_select, charp, 0);
151 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
152 module_param(xmit_hash_policy, charp, 0);
153 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
154 ", 1 for layer 3+4");
155 module_param(arp_interval, int, 0);
156 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
157 module_param_array(arp_ip_target, charp, NULL, 0);
158 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
159 module_param(arp_validate, charp, 0);
160 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
161 module_param(fail_over_mac, charp, 0);
162 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
163 module_param(all_slaves_active, int, 0);
164 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
165 "by setting active flag for all slaves. "
166 "0 for never (default), 1 for always.");
167 module_param(resend_igmp, int, 0);
168 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
170 /*----------------------------- Global variables ----------------------------*/
172 #ifdef CONFIG_NET_POLL_CONTROLLER
173 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
176 static const char * const version =
177 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
179 int bond_net_id __read_mostly;
181 static __be32 arp_target[BOND_MAX_ARP_TARGETS];
182 static int arp_ip_count;
183 static int bond_mode = BOND_MODE_ROUNDROBIN;
184 static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
185 static int lacp_fast;
187 const struct bond_parm_tbl bond_lacp_tbl[] = {
188 { "slow", AD_LACP_SLOW},
189 { "fast", AD_LACP_FAST},
193 const struct bond_parm_tbl bond_mode_tbl[] = {
194 { "balance-rr", BOND_MODE_ROUNDROBIN},
195 { "active-backup", BOND_MODE_ACTIVEBACKUP},
196 { "balance-xor", BOND_MODE_XOR},
197 { "broadcast", BOND_MODE_BROADCAST},
198 { "802.3ad", BOND_MODE_8023AD},
199 { "balance-tlb", BOND_MODE_TLB},
200 { "balance-alb", BOND_MODE_ALB},
204 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
205 { "layer2", BOND_XMIT_POLICY_LAYER2},
206 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
207 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
211 const struct bond_parm_tbl arp_validate_tbl[] = {
212 { "none", BOND_ARP_VALIDATE_NONE},
213 { "active", BOND_ARP_VALIDATE_ACTIVE},
214 { "backup", BOND_ARP_VALIDATE_BACKUP},
215 { "all", BOND_ARP_VALIDATE_ALL},
219 const struct bond_parm_tbl fail_over_mac_tbl[] = {
220 { "none", BOND_FOM_NONE},
221 { "active", BOND_FOM_ACTIVE},
222 { "follow", BOND_FOM_FOLLOW},
226 const struct bond_parm_tbl pri_reselect_tbl[] = {
227 { "always", BOND_PRI_RESELECT_ALWAYS},
228 { "better", BOND_PRI_RESELECT_BETTER},
229 { "failure", BOND_PRI_RESELECT_FAILURE},
233 struct bond_parm_tbl ad_select_tbl[] = {
234 { "stable", BOND_AD_STABLE},
235 { "bandwidth", BOND_AD_BANDWIDTH},
236 { "count", BOND_AD_COUNT},
240 /*-------------------------- Forward declarations ---------------------------*/
242 static void bond_send_gratuitous_arp(struct bonding *bond);
243 static int bond_init(struct net_device *bond_dev);
244 static void bond_uninit(struct net_device *bond_dev);
246 /*---------------------------- General routines -----------------------------*/
248 static const char *bond_mode_name(int mode)
250 static const char *names[] = {
251 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
252 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
253 [BOND_MODE_XOR] = "load balancing (xor)",
254 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
255 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
256 [BOND_MODE_TLB] = "transmit load balancing",
257 [BOND_MODE_ALB] = "adaptive load balancing",
260 if (mode < 0 || mode > BOND_MODE_ALB)
266 /*---------------------------------- VLAN -----------------------------------*/
269 * bond_add_vlan - add a new vlan id on bond
270 * @bond: bond that got the notification
271 * @vlan_id: the vlan id to add
273 * Returns -ENOMEM if allocation failed.
275 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
277 struct vlan_entry *vlan;
279 pr_debug("bond: %s, vlan id %d\n",
280 (bond ? bond->dev->name : "None"), vlan_id);
282 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
286 INIT_LIST_HEAD(&vlan->vlan_list);
287 vlan->vlan_id = vlan_id;
289 write_lock_bh(&bond->lock);
291 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
293 write_unlock_bh(&bond->lock);
295 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
301 * bond_del_vlan - delete a vlan id from bond
302 * @bond: bond that got the notification
303 * @vlan_id: the vlan id to delete
305 * returns -ENODEV if @vlan_id was not found in @bond.
307 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
309 struct vlan_entry *vlan;
312 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
315 write_lock_bh(&bond->lock);
317 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
318 if (vlan->vlan_id == vlan_id) {
319 list_del(&vlan->vlan_list);
321 if (bond_is_lb(bond))
322 bond_alb_clear_vlan(bond, vlan_id);
324 pr_debug("removed VLAN ID %d from bond %s\n",
325 vlan_id, bond->dev->name);
329 if (list_empty(&bond->vlan_list) &&
330 (bond->slave_cnt == 0)) {
331 /* Last VLAN removed and no slaves, so
332 * restore block on adding VLANs. This will
333 * be removed once new slaves that are not
334 * VLAN challenged will be added.
336 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
344 pr_debug("couldn't find VLAN ID %d in bond %s\n",
345 vlan_id, bond->dev->name);
348 write_unlock_bh(&bond->lock);
349 unblock_netpoll_tx();
354 * bond_has_challenged_slaves
355 * @bond: the bond we're working on
357 * Searches the slave list. Returns 1 if a vlan challenged slave
358 * was found, 0 otherwise.
360 * Assumes bond->lock is held.
362 static int bond_has_challenged_slaves(struct bonding *bond)
367 bond_for_each_slave(bond, slave, i) {
368 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
369 pr_debug("found VLAN challenged slave - %s\n",
375 pr_debug("no VLAN challenged slaves found\n");
380 * bond_next_vlan - safely skip to the next item in the vlans list.
381 * @bond: the bond we're working on
382 * @curr: item we're advancing from
384 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
385 * or @curr->next otherwise (even if it is @curr itself again).
387 * Caller must hold bond->lock
389 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
391 struct vlan_entry *next, *last;
393 if (list_empty(&bond->vlan_list))
397 next = list_entry(bond->vlan_list.next,
398 struct vlan_entry, vlan_list);
400 last = list_entry(bond->vlan_list.prev,
401 struct vlan_entry, vlan_list);
403 next = list_entry(bond->vlan_list.next,
404 struct vlan_entry, vlan_list);
406 next = list_entry(curr->vlan_list.next,
407 struct vlan_entry, vlan_list);
415 * bond_dev_queue_xmit - Prepare skb for xmit.
417 * @bond: bond device that got this skb for tx.
418 * @skb: hw accel VLAN tagged skb to transmit
419 * @slave_dev: slave that is supposed to xmit this skbuff
421 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
422 struct net_device *slave_dev)
424 skb->dev = slave_dev;
426 if (unlikely(netpoll_tx_running(slave_dev)))
427 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
435 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
436 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
438 * a. This operation is performed in IOCTL context,
439 * b. The operation is protected by the RTNL semaphore in the 8021q code,
440 * c. Holding a lock with BH disabled while directly calling a base driver
441 * entry point is generally a BAD idea.
443 * The design of synchronization/protection for this operation in the 8021q
444 * module is good for one or more VLAN devices over a single physical device
445 * and cannot be extended for a teaming solution like bonding, so there is a
446 * potential race condition here where a net device from the vlan group might
447 * be referenced (either by a base driver or the 8021q code) while it is being
448 * removed from the system. However, it turns out we're not making matters
449 * worse, and if it works for regular VLAN usage it will work here too.
453 * bond_vlan_rx_register - Propagates registration to slaves
454 * @bond_dev: bonding net device that got called
455 * @grp: vlan group being registered
457 static void bond_vlan_rx_register(struct net_device *bond_dev,
458 struct vlan_group *grp)
460 struct bonding *bond = netdev_priv(bond_dev);
464 write_lock_bh(&bond->lock);
466 write_unlock_bh(&bond->lock);
468 bond_for_each_slave(bond, slave, i) {
469 struct net_device *slave_dev = slave->dev;
470 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
472 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
473 slave_ops->ndo_vlan_rx_register) {
474 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
480 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
481 * @bond_dev: bonding net device that got called
482 * @vid: vlan id being added
484 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
486 struct bonding *bond = netdev_priv(bond_dev);
490 bond_for_each_slave(bond, slave, i) {
491 struct net_device *slave_dev = slave->dev;
492 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
494 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
495 slave_ops->ndo_vlan_rx_add_vid) {
496 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
500 res = bond_add_vlan(bond, vid);
502 pr_err("%s: Error: Failed to add vlan id %d\n",
503 bond_dev->name, vid);
508 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
509 * @bond_dev: bonding net device that got called
510 * @vid: vlan id being removed
512 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
514 struct bonding *bond = netdev_priv(bond_dev);
516 struct net_device *vlan_dev;
519 bond_for_each_slave(bond, slave, i) {
520 struct net_device *slave_dev = slave->dev;
521 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
523 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
524 slave_ops->ndo_vlan_rx_kill_vid) {
525 /* Save and then restore vlan_dev in the grp array,
526 * since the slave's driver might clear it.
528 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
529 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
530 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
534 res = bond_del_vlan(bond, vid);
536 pr_err("%s: Error: Failed to remove vlan id %d\n",
537 bond_dev->name, vid);
541 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
543 struct vlan_entry *vlan;
544 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
549 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
550 slave_ops->ndo_vlan_rx_register)
551 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
553 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
554 !(slave_ops->ndo_vlan_rx_add_vid))
557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
558 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
561 static void bond_del_vlans_from_slave(struct bonding *bond,
562 struct net_device *slave_dev)
564 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
565 struct vlan_entry *vlan;
566 struct net_device *vlan_dev;
571 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
572 !(slave_ops->ndo_vlan_rx_kill_vid))
575 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
578 /* Save and then restore vlan_dev in the grp array,
579 * since the slave's driver might clear it.
581 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
582 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
583 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
587 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
588 slave_ops->ndo_vlan_rx_register)
589 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
592 /*------------------------------- Link status -------------------------------*/
595 * Set the carrier state for the master according to the state of its
596 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
597 * do special 802.3ad magic.
599 * Returns zero if carrier state does not change, nonzero if it does.
601 static int bond_set_carrier(struct bonding *bond)
606 if (bond->slave_cnt == 0)
609 if (bond->params.mode == BOND_MODE_8023AD)
610 return bond_3ad_set_carrier(bond);
612 bond_for_each_slave(bond, slave, i) {
613 if (slave->link == BOND_LINK_UP) {
614 if (!netif_carrier_ok(bond->dev)) {
615 netif_carrier_on(bond->dev);
623 if (netif_carrier_ok(bond->dev)) {
624 netif_carrier_off(bond->dev);
631 * Get link speed and duplex from the slave's base driver
632 * using ethtool. If for some reason the call fails or the
633 * values are invalid, fake speed and duplex to 100/Full
636 static int bond_update_speed_duplex(struct slave *slave)
638 struct net_device *slave_dev = slave->dev;
639 struct ethtool_cmd etool;
642 /* Fake speed and duplex */
643 slave->speed = SPEED_100;
644 slave->duplex = DUPLEX_FULL;
646 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
649 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
653 switch (etool.speed) {
663 switch (etool.duplex) {
671 slave->speed = etool.speed;
672 slave->duplex = etool.duplex;
678 * if <dev> supports MII link status reporting, check its link status.
680 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
681 * depending upon the setting of the use_carrier parameter.
683 * Return either BMSR_LSTATUS, meaning that the link is up (or we
684 * can't tell and just pretend it is), or 0, meaning that the link is
687 * If reporting is non-zero, instead of faking link up, return -1 if
688 * both ETHTOOL and MII ioctls fail (meaning the device does not
689 * support them). If use_carrier is set, return whatever it says.
690 * It'd be nice if there was a good way to tell if a driver supports
691 * netif_carrier, but there really isn't.
693 static int bond_check_dev_link(struct bonding *bond,
694 struct net_device *slave_dev, int reporting)
696 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
697 int (*ioctl)(struct net_device *, struct ifreq *, int);
699 struct mii_ioctl_data *mii;
701 if (!reporting && !netif_running(slave_dev))
704 if (bond->params.use_carrier)
705 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
707 /* Try to get link status using Ethtool first. */
708 if (slave_dev->ethtool_ops) {
709 if (slave_dev->ethtool_ops->get_link) {
712 link = slave_dev->ethtool_ops->get_link(slave_dev);
714 return link ? BMSR_LSTATUS : 0;
718 /* Ethtool can't be used, fallback to MII ioctls. */
719 ioctl = slave_ops->ndo_do_ioctl;
721 /* TODO: set pointer to correct ioctl on a per team member */
722 /* bases to make this more efficient. that is, once */
723 /* we determine the correct ioctl, we will always */
724 /* call it and not the others for that team */
728 * We cannot assume that SIOCGMIIPHY will also read a
729 * register; not all network drivers (e.g., e100)
733 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
734 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
736 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
737 mii->reg_num = MII_BMSR;
738 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
739 return mii->val_out & BMSR_LSTATUS;
744 * If reporting, report that either there's no dev->do_ioctl,
745 * or both SIOCGMIIREG and get_link failed (meaning that we
746 * cannot report link status). If not reporting, pretend
749 return reporting ? -1 : BMSR_LSTATUS;
752 /*----------------------------- Multicast list ------------------------------*/
755 * Push the promiscuity flag down to appropriate slaves
757 static int bond_set_promiscuity(struct bonding *bond, int inc)
760 if (USES_PRIMARY(bond->params.mode)) {
761 /* write lock already acquired */
762 if (bond->curr_active_slave) {
763 err = dev_set_promiscuity(bond->curr_active_slave->dev,
769 bond_for_each_slave(bond, slave, i) {
770 err = dev_set_promiscuity(slave->dev, inc);
779 * Push the allmulti flag down to all slaves
781 static int bond_set_allmulti(struct bonding *bond, int inc)
784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
787 err = dev_set_allmulti(bond->curr_active_slave->dev,
793 bond_for_each_slave(bond, slave, i) {
794 err = dev_set_allmulti(slave->dev, inc);
803 * Add a Multicast address to slaves
806 static void bond_mc_add(struct bonding *bond, void *addr)
808 if (USES_PRIMARY(bond->params.mode)) {
809 /* write lock already acquired */
810 if (bond->curr_active_slave)
811 dev_mc_add(bond->curr_active_slave->dev, addr);
816 bond_for_each_slave(bond, slave, i)
817 dev_mc_add(slave->dev, addr);
822 * Remove a multicast address from slave
825 static void bond_mc_del(struct bonding *bond, void *addr)
827 if (USES_PRIMARY(bond->params.mode)) {
828 /* write lock already acquired */
829 if (bond->curr_active_slave)
830 dev_mc_del(bond->curr_active_slave->dev, addr);
834 bond_for_each_slave(bond, slave, i) {
835 dev_mc_del(slave->dev, addr);
841 static void __bond_resend_igmp_join_requests(struct net_device *dev)
843 struct in_device *in_dev;
846 in_dev = __in_dev_get_rcu(dev);
848 ip_mc_rejoin_groups(in_dev);
853 * Retrieve the list of registered multicast addresses for the bonding
854 * device and retransmit an IGMP JOIN request to the current active
857 static void bond_resend_igmp_join_requests(struct bonding *bond)
859 struct net_device *vlan_dev;
860 struct vlan_entry *vlan;
862 read_lock(&bond->lock);
864 /* rejoin all groups on bond device */
865 __bond_resend_igmp_join_requests(bond->dev);
867 /* rejoin all groups on vlan devices */
869 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
870 vlan_dev = vlan_group_get_device(bond->vlgrp,
873 __bond_resend_igmp_join_requests(vlan_dev);
877 if (--bond->igmp_retrans > 0)
878 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
880 read_unlock(&bond->lock);
883 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
885 struct bonding *bond = container_of(work, struct bonding,
887 bond_resend_igmp_join_requests(bond);
891 * flush all members of flush->mc_list from device dev->mc_list
893 static void bond_mc_list_flush(struct net_device *bond_dev,
894 struct net_device *slave_dev)
896 struct bonding *bond = netdev_priv(bond_dev);
897 struct netdev_hw_addr *ha;
899 netdev_for_each_mc_addr(ha, bond_dev)
900 dev_mc_del(slave_dev, ha->addr);
902 if (bond->params.mode == BOND_MODE_8023AD) {
903 /* del lacpdu mc addr from mc list */
904 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
906 dev_mc_del(slave_dev, lacpdu_multicast);
910 /*--------------------------- Active slave change ---------------------------*/
913 * Update the mc list and multicast-related flags for the new and
914 * old active slaves (if any) according to the multicast mode, and
915 * promiscuous flags unconditionally.
917 static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
918 struct slave *old_active)
920 struct netdev_hw_addr *ha;
922 if (!USES_PRIMARY(bond->params.mode))
923 /* nothing to do - mc list is already up-to-date on
929 if (bond->dev->flags & IFF_PROMISC)
930 dev_set_promiscuity(old_active->dev, -1);
932 if (bond->dev->flags & IFF_ALLMULTI)
933 dev_set_allmulti(old_active->dev, -1);
935 netdev_for_each_mc_addr(ha, bond->dev)
936 dev_mc_del(old_active->dev, ha->addr);
940 /* FIXME: Signal errors upstream. */
941 if (bond->dev->flags & IFF_PROMISC)
942 dev_set_promiscuity(new_active->dev, 1);
944 if (bond->dev->flags & IFF_ALLMULTI)
945 dev_set_allmulti(new_active->dev, 1);
947 netdev_for_each_mc_addr(ha, bond->dev)
948 dev_mc_add(new_active->dev, ha->addr);
953 * bond_do_fail_over_mac
955 * Perform special MAC address swapping for fail_over_mac settings
957 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
959 static void bond_do_fail_over_mac(struct bonding *bond,
960 struct slave *new_active,
961 struct slave *old_active)
962 __releases(&bond->curr_slave_lock)
963 __releases(&bond->lock)
964 __acquires(&bond->lock)
965 __acquires(&bond->curr_slave_lock)
967 u8 tmp_mac[ETH_ALEN];
968 struct sockaddr saddr;
971 switch (bond->params.fail_over_mac) {
972 case BOND_FOM_ACTIVE:
974 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
975 new_active->dev->addr_len);
977 case BOND_FOM_FOLLOW:
979 * if new_active && old_active, swap them
980 * if just old_active, do nothing (going to no active slave)
981 * if just new_active, set new_active to bond's MAC
986 write_unlock_bh(&bond->curr_slave_lock);
987 read_unlock(&bond->lock);
990 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
991 memcpy(saddr.sa_data, old_active->dev->dev_addr,
993 saddr.sa_family = new_active->dev->type;
995 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
996 saddr.sa_family = bond->dev->type;
999 rv = dev_set_mac_address(new_active->dev, &saddr);
1001 pr_err("%s: Error %d setting MAC of slave %s\n",
1002 bond->dev->name, -rv, new_active->dev->name);
1009 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1010 saddr.sa_family = old_active->dev->type;
1012 rv = dev_set_mac_address(old_active->dev, &saddr);
1014 pr_err("%s: Error %d setting MAC of slave %s\n",
1015 bond->dev->name, -rv, new_active->dev->name);
1017 read_lock(&bond->lock);
1018 write_lock_bh(&bond->curr_slave_lock);
1021 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
1022 bond->dev->name, bond->params.fail_over_mac);
1028 static bool bond_should_change_active(struct bonding *bond)
1030 struct slave *prim = bond->primary_slave;
1031 struct slave *curr = bond->curr_active_slave;
1033 if (!prim || !curr || curr->link != BOND_LINK_UP)
1035 if (bond->force_primary) {
1036 bond->force_primary = false;
1039 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1040 (prim->speed < curr->speed ||
1041 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1043 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1049 * find_best_interface - select the best available slave to be the active one
1050 * @bond: our bonding struct
1052 * Warning: Caller must hold curr_slave_lock for writing.
1054 static struct slave *bond_find_best_slave(struct bonding *bond)
1056 struct slave *new_active, *old_active;
1057 struct slave *bestslave = NULL;
1058 int mintime = bond->params.updelay;
1061 new_active = bond->curr_active_slave;
1063 if (!new_active) { /* there were no active slaves left */
1064 if (bond->slave_cnt > 0) /* found one slave */
1065 new_active = bond->first_slave;
1067 return NULL; /* still no slave, return NULL */
1070 if ((bond->primary_slave) &&
1071 bond->primary_slave->link == BOND_LINK_UP &&
1072 bond_should_change_active(bond)) {
1073 new_active = bond->primary_slave;
1076 /* remember where to stop iterating over the slaves */
1077 old_active = new_active;
1079 bond_for_each_slave_from(bond, new_active, i, old_active) {
1080 if (new_active->link == BOND_LINK_UP) {
1082 } else if (new_active->link == BOND_LINK_BACK &&
1083 IS_UP(new_active->dev)) {
1084 /* link up, but waiting for stabilization */
1085 if (new_active->delay < mintime) {
1086 mintime = new_active->delay;
1087 bestslave = new_active;
1096 * change_active_interface - change the active slave into the specified one
1097 * @bond: our bonding struct
1098 * @new: the new slave to make the active one
1100 * Set the new slave to the bond's settings and unset them on the old
1101 * curr_active_slave.
1102 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1104 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1105 * because it is apparently the best available slave we have, even though its
1106 * updelay hasn't timed out yet.
1108 * If new_active is not NULL, caller must hold bond->lock for read and
1109 * curr_slave_lock for write_bh.
1111 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1113 struct slave *old_active = bond->curr_active_slave;
1115 if (old_active == new_active)
1119 new_active->jiffies = jiffies;
1121 if (new_active->link == BOND_LINK_BACK) {
1122 if (USES_PRIMARY(bond->params.mode)) {
1123 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1124 bond->dev->name, new_active->dev->name,
1125 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1128 new_active->delay = 0;
1129 new_active->link = BOND_LINK_UP;
1131 if (bond->params.mode == BOND_MODE_8023AD)
1132 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1134 if (bond_is_lb(bond))
1135 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1137 if (USES_PRIMARY(bond->params.mode)) {
1138 pr_info("%s: making interface %s the new active one.\n",
1139 bond->dev->name, new_active->dev->name);
1144 if (USES_PRIMARY(bond->params.mode))
1145 bond_mc_swap(bond, new_active, old_active);
1147 if (bond_is_lb(bond)) {
1148 bond_alb_handle_active_change(bond, new_active);
1150 bond_set_slave_inactive_flags(old_active);
1152 bond_set_slave_active_flags(new_active);
1154 bond->curr_active_slave = new_active;
1157 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1159 bond_set_slave_inactive_flags(old_active);
1162 bond_set_slave_active_flags(new_active);
1164 if (bond->params.fail_over_mac)
1165 bond_do_fail_over_mac(bond, new_active,
1168 if (netif_running(bond->dev)) {
1169 bond->send_grat_arp = bond->params.num_grat_arp;
1170 bond_send_gratuitous_arp(bond);
1172 bond->send_unsol_na = bond->params.num_unsol_na;
1173 bond_send_unsolicited_na(bond);
1176 write_unlock_bh(&bond->curr_slave_lock);
1177 read_unlock(&bond->lock);
1179 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
1181 read_lock(&bond->lock);
1182 write_lock_bh(&bond->curr_slave_lock);
1186 /* resend IGMP joins since active slave has changed or
1187 * all were sent on curr_active_slave */
1188 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1189 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1190 netif_running(bond->dev)) {
1191 bond->igmp_retrans = bond->params.resend_igmp;
1192 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
1197 * bond_select_active_slave - select a new active slave, if needed
1198 * @bond: our bonding struct
1200 * This functions should be called when one of the following occurs:
1201 * - The old curr_active_slave has been released or lost its link.
1202 * - The primary_slave has got its link back.
1203 * - A slave has got its link back and there's no old curr_active_slave.
1205 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1207 void bond_select_active_slave(struct bonding *bond)
1209 struct slave *best_slave;
1212 best_slave = bond_find_best_slave(bond);
1213 if (best_slave != bond->curr_active_slave) {
1214 bond_change_active_slave(bond, best_slave);
1215 rv = bond_set_carrier(bond);
1219 if (netif_carrier_ok(bond->dev)) {
1220 pr_info("%s: first active interface up!\n",
1223 pr_info("%s: now running without any active interface !\n",
1229 /*--------------------------- slave list handling ---------------------------*/
1232 * This function attaches the slave to the end of list.
1234 * bond->lock held for writing by caller.
1236 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1238 if (bond->first_slave == NULL) { /* attaching the first slave */
1239 new_slave->next = new_slave;
1240 new_slave->prev = new_slave;
1241 bond->first_slave = new_slave;
1243 new_slave->next = bond->first_slave;
1244 new_slave->prev = bond->first_slave->prev;
1245 new_slave->next->prev = new_slave;
1246 new_slave->prev->next = new_slave;
1253 * This function detaches the slave from the list.
1254 * WARNING: no check is made to verify if the slave effectively
1255 * belongs to <bond>.
1256 * Nothing is freed on return, structures are just unchained.
1257 * If any slave pointer in bond was pointing to <slave>,
1258 * it should be changed by the calling function.
1260 * bond->lock held for writing by caller.
1262 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1265 slave->next->prev = slave->prev;
1268 slave->prev->next = slave->next;
1270 if (bond->first_slave == slave) { /* slave is the first slave */
1271 if (bond->slave_cnt > 1) { /* there are more slave */
1272 bond->first_slave = slave->next;
1274 bond->first_slave = NULL; /* slave was the last one */
1283 #ifdef CONFIG_NET_POLL_CONTROLLER
1284 static inline int slave_enable_netpoll(struct slave *slave)
1289 np = kzalloc(sizeof(*np), GFP_KERNEL);
1294 np->dev = slave->dev;
1295 err = __netpoll_setup(np);
1304 static inline void slave_disable_netpoll(struct slave *slave)
1306 struct netpoll *np = slave->np;
1312 synchronize_rcu_bh();
1313 __netpoll_cleanup(np);
1316 static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1318 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1320 if (!slave_dev->netdev_ops->ndo_poll_controller)
1325 static void bond_poll_controller(struct net_device *bond_dev)
1329 static void __bond_netpoll_cleanup(struct bonding *bond)
1331 struct slave *slave;
1334 bond_for_each_slave(bond, slave, i)
1335 if (IS_UP(slave->dev))
1336 slave_disable_netpoll(slave);
1338 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1340 struct bonding *bond = netdev_priv(bond_dev);
1342 read_lock(&bond->lock);
1343 __bond_netpoll_cleanup(bond);
1344 read_unlock(&bond->lock);
1347 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1349 struct bonding *bond = netdev_priv(dev);
1350 struct slave *slave;
1353 read_lock(&bond->lock);
1354 bond_for_each_slave(bond, slave, i) {
1355 if (!IS_UP(slave->dev))
1357 err = slave_enable_netpoll(slave);
1359 __bond_netpoll_cleanup(bond);
1363 read_unlock(&bond->lock);
1367 static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1369 return bond->dev->npinfo;
1373 static inline int slave_enable_netpoll(struct slave *slave)
1377 static inline void slave_disable_netpoll(struct slave *slave)
1380 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1383 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1387 static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1393 /*---------------------------------- IOCTL ----------------------------------*/
1395 static int bond_sethwaddr(struct net_device *bond_dev,
1396 struct net_device *slave_dev)
1398 pr_debug("bond_dev=%p\n", bond_dev);
1399 pr_debug("slave_dev=%p\n", slave_dev);
1400 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1401 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1405 #define BOND_VLAN_FEATURES \
1406 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1407 NETIF_F_HW_VLAN_FILTER)
1410 * Compute the common dev->feature set available to all slaves. Some
1411 * feature bits are managed elsewhere, so preserve those feature bits
1412 * on the master device.
1414 static int bond_compute_features(struct bonding *bond)
1416 struct slave *slave;
1417 struct net_device *bond_dev = bond->dev;
1418 u32 features = bond_dev->features;
1419 u32 vlan_features = 0;
1420 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1421 bond_dev->hard_header_len);
1424 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1425 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1427 if (!bond->first_slave)
1430 features &= ~NETIF_F_ONE_FOR_ALL;
1432 vlan_features = bond->first_slave->dev->vlan_features;
1433 bond_for_each_slave(bond, slave, i) {
1434 features = netdev_increment_features(features,
1435 slave->dev->features,
1436 NETIF_F_ONE_FOR_ALL);
1437 vlan_features = netdev_increment_features(vlan_features,
1438 slave->dev->vlan_features,
1439 NETIF_F_ONE_FOR_ALL);
1440 if (slave->dev->hard_header_len > max_hard_header_len)
1441 max_hard_header_len = slave->dev->hard_header_len;
1445 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1446 bond_dev->features = netdev_fix_features(bond_dev, features);
1447 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
1448 bond_dev->hard_header_len = max_hard_header_len;
1453 static void bond_setup_by_slave(struct net_device *bond_dev,
1454 struct net_device *slave_dev)
1456 struct bonding *bond = netdev_priv(bond_dev);
1458 bond_dev->header_ops = slave_dev->header_ops;
1460 bond_dev->type = slave_dev->type;
1461 bond_dev->hard_header_len = slave_dev->hard_header_len;
1462 bond_dev->addr_len = slave_dev->addr_len;
1464 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1465 slave_dev->addr_len);
1466 bond->setup_by_slave = 1;
1469 /* enslave device <slave> to bond device <master> */
1470 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1472 struct bonding *bond = netdev_priv(bond_dev);
1473 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1474 struct slave *new_slave = NULL;
1475 struct netdev_hw_addr *ha;
1476 struct sockaddr addr;
1478 int old_features = bond_dev->features;
1481 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1482 slave_ops->ndo_do_ioctl == NULL) {
1483 pr_warning("%s: Warning: no link monitoring support for %s\n",
1484 bond_dev->name, slave_dev->name);
1487 /* bond must be initialized by bond_open() before enslaving */
1488 if (!(bond_dev->flags & IFF_UP)) {
1489 pr_warning("%s: master_dev is not up in bond_enslave\n",
1493 /* already enslaved */
1494 if (slave_dev->flags & IFF_SLAVE) {
1495 pr_debug("Error, Device was already enslaved\n");
1499 /* vlan challenged mutual exclusion */
1500 /* no need to lock since we're protected by rtnl_lock */
1501 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1502 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1504 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1505 bond_dev->name, slave_dev->name, bond_dev->name);
1508 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1509 bond_dev->name, slave_dev->name,
1510 slave_dev->name, bond_dev->name);
1511 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1514 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1515 if (bond->slave_cnt == 0) {
1516 /* First slave, and it is not VLAN challenged,
1517 * so remove the block of adding VLANs over the bond.
1519 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1524 * Old ifenslave binaries are no longer supported. These can
1525 * be identified with moderate accuracy by the state of the slave:
1526 * the current ifenslave will set the interface down prior to
1527 * enslaving it; the old ifenslave will not.
1529 if ((slave_dev->flags & IFF_UP)) {
1530 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
1533 goto err_undo_flags;
1536 /* set bonding device ether type by slave - bonding netdevices are
1537 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1538 * there is a need to override some of the type dependent attribs/funcs.
1540 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1541 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1543 if (bond->slave_cnt == 0) {
1544 if (bond_dev->type != slave_dev->type) {
1545 pr_debug("%s: change device type from %d to %d\n",
1547 bond_dev->type, slave_dev->type);
1549 res = netdev_bonding_change(bond_dev,
1550 NETDEV_PRE_TYPE_CHANGE);
1551 res = notifier_to_errno(res);
1553 pr_err("%s: refused to change device type\n",
1556 goto err_undo_flags;
1559 /* Flush unicast and multicast addresses */
1560 dev_uc_flush(bond_dev);
1561 dev_mc_flush(bond_dev);
1563 if (slave_dev->type != ARPHRD_ETHER)
1564 bond_setup_by_slave(bond_dev, slave_dev);
1566 ether_setup(bond_dev);
1568 netdev_bonding_change(bond_dev,
1569 NETDEV_POST_TYPE_CHANGE);
1571 } else if (bond_dev->type != slave_dev->type) {
1572 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1574 slave_dev->type, bond_dev->type);
1576 goto err_undo_flags;
1579 if (slave_ops->ndo_set_mac_address == NULL) {
1580 if (bond->slave_cnt == 0) {
1581 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1583 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1584 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1585 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",
1588 goto err_undo_flags;
1592 /* If this is the first slave, then we need to set the master's hardware
1593 * address to be the same as the slave's. */
1594 if (is_zero_ether_addr(bond->dev->dev_addr))
1595 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1596 slave_dev->addr_len);
1599 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1602 goto err_undo_flags;
1606 * Set the new_slave's queue_id to be zero. Queue ID mapping
1607 * is set via sysfs or module option if desired.
1609 new_slave->queue_id = 0;
1611 /* Save slave's original mtu and then set it to match the bond */
1612 new_slave->original_mtu = slave_dev->mtu;
1613 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1615 pr_debug("Error %d calling dev_set_mtu\n", res);
1620 * Save slave's original ("permanent") mac address for modes
1621 * that need it, and for restoring it upon release, and then
1622 * set it to the master's address
1624 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1626 if (!bond->params.fail_over_mac) {
1628 * Set slave to master's mac address. The application already
1629 * set the master's mac address to that of the first slave
1631 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1632 addr.sa_family = slave_dev->type;
1633 res = dev_set_mac_address(slave_dev, &addr);
1635 pr_debug("Error %d calling set_mac_address\n", res);
1636 goto err_restore_mtu;
1640 res = netdev_set_bond_master(slave_dev, bond_dev);
1642 pr_debug("Error %d calling netdev_set_bond_master\n", res);
1643 goto err_restore_mac;
1645 /* open the slave since the application closed it */
1646 res = dev_open(slave_dev);
1648 pr_debug("Opening slave %s failed\n", slave_dev->name);
1649 goto err_unset_master;
1652 new_slave->dev = slave_dev;
1653 slave_dev->priv_flags |= IFF_BONDING;
1655 if (bond_is_lb(bond)) {
1656 /* bond_alb_init_slave() must be called before all other stages since
1657 * it might fail and we do not want to have to undo everything
1659 res = bond_alb_init_slave(bond, new_slave);
1664 /* If the mode USES_PRIMARY, then the new slave gets the
1665 * master's promisc (and mc) settings only if it becomes the
1666 * curr_active_slave, and that is taken care of later when calling
1667 * bond_change_active()
1669 if (!USES_PRIMARY(bond->params.mode)) {
1670 /* set promiscuity level to new slave */
1671 if (bond_dev->flags & IFF_PROMISC) {
1672 res = dev_set_promiscuity(slave_dev, 1);
1677 /* set allmulti level to new slave */
1678 if (bond_dev->flags & IFF_ALLMULTI) {
1679 res = dev_set_allmulti(slave_dev, 1);
1684 netif_addr_lock_bh(bond_dev);
1685 /* upload master's mc_list to new slave */
1686 netdev_for_each_mc_addr(ha, bond_dev)
1687 dev_mc_add(slave_dev, ha->addr);
1688 netif_addr_unlock_bh(bond_dev);
1691 if (bond->params.mode == BOND_MODE_8023AD) {
1692 /* add lacpdu mc addr to mc list */
1693 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1695 dev_mc_add(slave_dev, lacpdu_multicast);
1698 bond_add_vlans_on_slave(bond, slave_dev);
1700 write_lock_bh(&bond->lock);
1702 bond_attach_slave(bond, new_slave);
1704 new_slave->delay = 0;
1705 new_slave->link_failure_count = 0;
1707 bond_compute_features(bond);
1709 write_unlock_bh(&bond->lock);
1711 read_lock(&bond->lock);
1713 new_slave->last_arp_rx = jiffies;
1715 if (bond->params.miimon && !bond->params.use_carrier) {
1716 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1718 if ((link_reporting == -1) && !bond->params.arp_interval) {
1720 * miimon is set but a bonded network driver
1721 * does not support ETHTOOL/MII and
1722 * arp_interval is not set. Note: if
1723 * use_carrier is enabled, we will never go
1724 * here (because netif_carrier is always
1725 * supported); thus, we don't need to change
1726 * the messages for netif_carrier.
1728 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",
1729 bond_dev->name, slave_dev->name);
1730 } else if (link_reporting == -1) {
1731 /* unable get link status using mii/ethtool */
1732 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",
1733 bond_dev->name, slave_dev->name);
1737 /* check for initial state */
1738 if (!bond->params.miimon ||
1739 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1740 if (bond->params.updelay) {
1741 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
1742 new_slave->link = BOND_LINK_BACK;
1743 new_slave->delay = bond->params.updelay;
1745 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
1746 new_slave->link = BOND_LINK_UP;
1748 new_slave->jiffies = jiffies;
1750 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
1751 new_slave->link = BOND_LINK_DOWN;
1754 if (bond_update_speed_duplex(new_slave) &&
1755 (new_slave->link != BOND_LINK_DOWN)) {
1756 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1757 bond_dev->name, new_slave->dev->name);
1759 if (bond->params.mode == BOND_MODE_8023AD) {
1760 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1765 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1766 /* if there is a primary slave, remember it */
1767 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1768 bond->primary_slave = new_slave;
1769 bond->force_primary = true;
1773 write_lock_bh(&bond->curr_slave_lock);
1775 switch (bond->params.mode) {
1776 case BOND_MODE_ACTIVEBACKUP:
1777 bond_set_slave_inactive_flags(new_slave);
1778 bond_select_active_slave(bond);
1780 case BOND_MODE_8023AD:
1781 /* in 802.3ad mode, the internal mechanism
1782 * will activate the slaves in the selected
1785 bond_set_slave_inactive_flags(new_slave);
1786 /* if this is the first slave */
1787 if (bond->slave_cnt == 1) {
1788 SLAVE_AD_INFO(new_slave).id = 1;
1789 /* Initialize AD with the number of times that the AD timer is called in 1 second
1790 * can be called only after the mac address of the bond is set
1792 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1793 bond->params.lacp_fast);
1795 SLAVE_AD_INFO(new_slave).id =
1796 SLAVE_AD_INFO(new_slave->prev).id + 1;
1799 bond_3ad_bind_slave(new_slave);
1803 new_slave->state = BOND_STATE_ACTIVE;
1804 bond_set_slave_inactive_flags(new_slave);
1805 bond_select_active_slave(bond);
1808 pr_debug("This slave is always active in trunk mode\n");
1810 /* always active in trunk mode */
1811 new_slave->state = BOND_STATE_ACTIVE;
1813 /* In trunking mode there is little meaning to curr_active_slave
1814 * anyway (it holds no special properties of the bond device),
1815 * so we can change it without calling change_active_interface()
1817 if (!bond->curr_active_slave)
1818 bond->curr_active_slave = new_slave;
1821 } /* switch(bond_mode) */
1823 write_unlock_bh(&bond->curr_slave_lock);
1825 bond_set_carrier(bond);
1827 #ifdef CONFIG_NET_POLL_CONTROLLER
1828 slave_dev->npinfo = bond_netpoll_info(bond);
1829 if (slave_dev->npinfo) {
1830 if (slave_enable_netpoll(new_slave)) {
1831 read_unlock(&bond->lock);
1832 pr_info("Error, %s: master_dev is using netpoll, "
1833 "but new slave device does not support netpoll.\n",
1841 read_unlock(&bond->lock);
1843 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1847 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1848 bond_dev->name, slave_dev->name,
1849 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1850 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1852 /* enslave is successful */
1855 /* Undo stages on error */
1857 dev_close(slave_dev);
1860 netdev_set_bond_master(slave_dev, NULL);
1863 if (!bond->params.fail_over_mac) {
1864 /* XXX TODO - fom follow mode needs to change master's
1865 * MAC if this slave's MAC is in use by the bond, or at
1866 * least print a warning.
1868 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1869 addr.sa_family = slave_dev->type;
1870 dev_set_mac_address(slave_dev, &addr);
1874 dev_set_mtu(slave_dev, new_slave->original_mtu);
1880 bond_dev->features = old_features;
1886 * Try to release the slave device <slave> from the bond device <master>
1887 * It is legal to access curr_active_slave without a lock because all the function
1890 * The rules for slave state should be:
1891 * for Active/Backup:
1892 * Active stays on all backups go down
1893 * for Bonded connections:
1894 * The first up interface should be left on and all others downed.
1896 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1898 struct bonding *bond = netdev_priv(bond_dev);
1899 struct slave *slave, *oldcurrent;
1900 struct sockaddr addr;
1902 /* slave is not a slave or master is not master of this slave */
1903 if (!(slave_dev->flags & IFF_SLAVE) ||
1904 (slave_dev->master != bond_dev)) {
1905 pr_err("%s: Error: cannot release %s.\n",
1906 bond_dev->name, slave_dev->name);
1911 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
1912 write_lock_bh(&bond->lock);
1914 slave = bond_get_slave_by_dev(bond, slave_dev);
1916 /* not a slave of this bond */
1917 pr_info("%s: %s not enslaved\n",
1918 bond_dev->name, slave_dev->name);
1919 write_unlock_bh(&bond->lock);
1920 unblock_netpoll_tx();
1924 if (!bond->params.fail_over_mac) {
1925 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1926 bond->slave_cnt > 1)
1927 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",
1928 bond_dev->name, slave_dev->name,
1930 bond_dev->name, slave_dev->name);
1933 /* Inform AD package of unbinding of slave. */
1934 if (bond->params.mode == BOND_MODE_8023AD) {
1935 /* must be called before the slave is
1936 * detached from the list
1938 bond_3ad_unbind_slave(slave);
1941 pr_info("%s: releasing %s interface %s\n",
1943 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
1946 oldcurrent = bond->curr_active_slave;
1948 bond->current_arp_slave = NULL;
1950 /* release the slave from its bond */
1951 bond_detach_slave(bond, slave);
1953 bond_compute_features(bond);
1955 if (bond->primary_slave == slave)
1956 bond->primary_slave = NULL;
1958 if (oldcurrent == slave)
1959 bond_change_active_slave(bond, NULL);
1961 if (bond_is_lb(bond)) {
1962 /* Must be called only after the slave has been
1963 * detached from the list and the curr_active_slave
1964 * has been cleared (if our_slave == old_current),
1965 * but before a new active slave is selected.
1967 write_unlock_bh(&bond->lock);
1968 bond_alb_deinit_slave(bond, slave);
1969 write_lock_bh(&bond->lock);
1972 if (oldcurrent == slave) {
1974 * Note that we hold RTNL over this sequence, so there
1975 * is no concern that another slave add/remove event
1978 write_unlock_bh(&bond->lock);
1979 read_lock(&bond->lock);
1980 write_lock_bh(&bond->curr_slave_lock);
1982 bond_select_active_slave(bond);
1984 write_unlock_bh(&bond->curr_slave_lock);
1985 read_unlock(&bond->lock);
1986 write_lock_bh(&bond->lock);
1989 if (bond->slave_cnt == 0) {
1990 bond_set_carrier(bond);
1992 /* if the last slave was removed, zero the mac address
1993 * of the master so it will be set by the application
1994 * to the mac address of the first slave
1996 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1999 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2001 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2002 bond_dev->name, bond_dev->name);
2003 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2006 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2007 !bond_has_challenged_slaves(bond)) {
2008 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2009 bond_dev->name, slave_dev->name, bond_dev->name);
2010 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2013 write_unlock_bh(&bond->lock);
2014 unblock_netpoll_tx();
2016 /* must do this from outside any spinlocks */
2017 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2019 bond_del_vlans_from_slave(bond, slave_dev);
2021 /* If the mode USES_PRIMARY, then we should only remove its
2022 * promisc and mc settings if it was the curr_active_slave, but that was
2023 * already taken care of above when we detached the slave
2025 if (!USES_PRIMARY(bond->params.mode)) {
2026 /* unset promiscuity level from slave */
2027 if (bond_dev->flags & IFF_PROMISC)
2028 dev_set_promiscuity(slave_dev, -1);
2030 /* unset allmulti level from slave */
2031 if (bond_dev->flags & IFF_ALLMULTI)
2032 dev_set_allmulti(slave_dev, -1);
2034 /* flush master's mc_list from slave */
2035 netif_addr_lock_bh(bond_dev);
2036 bond_mc_list_flush(bond_dev, slave_dev);
2037 netif_addr_unlock_bh(bond_dev);
2040 netdev_set_bond_master(slave_dev, NULL);
2042 slave_disable_netpoll(slave);
2044 /* close slave before restoring its mac address */
2045 dev_close(slave_dev);
2047 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
2048 /* restore original ("permanent") mac address */
2049 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2050 addr.sa_family = slave_dev->type;
2051 dev_set_mac_address(slave_dev, &addr);
2054 dev_set_mtu(slave_dev, slave->original_mtu);
2056 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2057 IFF_SLAVE_INACTIVE | IFF_BONDING |
2062 return 0; /* deletion OK */
2066 * First release a slave and than destroy the bond if no more slaves are left.
2067 * Must be under rtnl_lock when this function is called.
2069 static int bond_release_and_destroy(struct net_device *bond_dev,
2070 struct net_device *slave_dev)
2072 struct bonding *bond = netdev_priv(bond_dev);
2075 ret = bond_release(bond_dev, slave_dev);
2076 if ((ret == 0) && (bond->slave_cnt == 0)) {
2077 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2078 pr_info("%s: destroying bond %s.\n",
2079 bond_dev->name, bond_dev->name);
2080 unregister_netdevice(bond_dev);
2086 * This function releases all slaves.
2088 static int bond_release_all(struct net_device *bond_dev)
2090 struct bonding *bond = netdev_priv(bond_dev);
2091 struct slave *slave;
2092 struct net_device *slave_dev;
2093 struct sockaddr addr;
2095 write_lock_bh(&bond->lock);
2097 netif_carrier_off(bond_dev);
2099 if (bond->slave_cnt == 0)
2102 bond->current_arp_slave = NULL;
2103 bond->primary_slave = NULL;
2104 bond_change_active_slave(bond, NULL);
2106 while ((slave = bond->first_slave) != NULL) {
2107 /* Inform AD package of unbinding of slave
2108 * before slave is detached from the list.
2110 if (bond->params.mode == BOND_MODE_8023AD)
2111 bond_3ad_unbind_slave(slave);
2113 slave_dev = slave->dev;
2114 bond_detach_slave(bond, slave);
2116 /* now that the slave is detached, unlock and perform
2117 * all the undo steps that should not be called from
2120 write_unlock_bh(&bond->lock);
2122 if (bond_is_lb(bond)) {
2123 /* must be called only after the slave
2124 * has been detached from the list
2126 bond_alb_deinit_slave(bond, slave);
2129 bond_compute_features(bond);
2131 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2132 bond_del_vlans_from_slave(bond, slave_dev);
2134 /* If the mode USES_PRIMARY, then we should only remove its
2135 * promisc and mc settings if it was the curr_active_slave, but that was
2136 * already taken care of above when we detached the slave
2138 if (!USES_PRIMARY(bond->params.mode)) {
2139 /* unset promiscuity level from slave */
2140 if (bond_dev->flags & IFF_PROMISC)
2141 dev_set_promiscuity(slave_dev, -1);
2143 /* unset allmulti level from slave */
2144 if (bond_dev->flags & IFF_ALLMULTI)
2145 dev_set_allmulti(slave_dev, -1);
2147 /* flush master's mc_list from slave */
2148 netif_addr_lock_bh(bond_dev);
2149 bond_mc_list_flush(bond_dev, slave_dev);
2150 netif_addr_unlock_bh(bond_dev);
2153 netdev_set_bond_master(slave_dev, NULL);
2155 slave_disable_netpoll(slave);
2157 /* close slave before restoring its mac address */
2158 dev_close(slave_dev);
2160 if (!bond->params.fail_over_mac) {
2161 /* restore original ("permanent") mac address*/
2162 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2163 addr.sa_family = slave_dev->type;
2164 dev_set_mac_address(slave_dev, &addr);
2167 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2168 IFF_SLAVE_INACTIVE);
2172 /* re-acquire the lock before getting the next slave */
2173 write_lock_bh(&bond->lock);
2176 /* zero the mac address of the master so it will be
2177 * set by the application to the mac address of the
2180 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2183 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2185 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2186 bond_dev->name, bond_dev->name);
2187 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2191 pr_info("%s: released all slaves\n", bond_dev->name);
2194 write_unlock_bh(&bond->lock);
2199 * This function changes the active slave to slave <slave_dev>.
2200 * It returns -EINVAL in the following cases.
2201 * - <slave_dev> is not found in the list.
2202 * - There is not active slave now.
2203 * - <slave_dev> is already active.
2204 * - The link state of <slave_dev> is not BOND_LINK_UP.
2205 * - <slave_dev> is not running.
2206 * In these cases, this function does nothing.
2207 * In the other cases, current_slave pointer is changed and 0 is returned.
2209 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2211 struct bonding *bond = netdev_priv(bond_dev);
2212 struct slave *old_active = NULL;
2213 struct slave *new_active = NULL;
2216 if (!USES_PRIMARY(bond->params.mode))
2219 /* Verify that master_dev is indeed the master of slave_dev */
2220 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
2223 read_lock(&bond->lock);
2225 read_lock(&bond->curr_slave_lock);
2226 old_active = bond->curr_active_slave;
2227 read_unlock(&bond->curr_slave_lock);
2229 new_active = bond_get_slave_by_dev(bond, slave_dev);
2232 * Changing to the current active: do nothing; return success.
2234 if (new_active && (new_active == old_active)) {
2235 read_unlock(&bond->lock);
2241 (new_active->link == BOND_LINK_UP) &&
2242 IS_UP(new_active->dev)) {
2244 write_lock_bh(&bond->curr_slave_lock);
2245 bond_change_active_slave(bond, new_active);
2246 write_unlock_bh(&bond->curr_slave_lock);
2247 unblock_netpoll_tx();
2251 read_unlock(&bond->lock);
2256 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2258 struct bonding *bond = netdev_priv(bond_dev);
2260 info->bond_mode = bond->params.mode;
2261 info->miimon = bond->params.miimon;
2263 read_lock(&bond->lock);
2264 info->num_slaves = bond->slave_cnt;
2265 read_unlock(&bond->lock);
2270 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2272 struct bonding *bond = netdev_priv(bond_dev);
2273 struct slave *slave;
2274 int i, res = -ENODEV;
2276 read_lock(&bond->lock);
2278 bond_for_each_slave(bond, slave, i) {
2279 if (i == (int)info->slave_id) {
2281 strcpy(info->slave_name, slave->dev->name);
2282 info->link = slave->link;
2283 info->state = slave->state;
2284 info->link_failure_count = slave->link_failure_count;
2289 read_unlock(&bond->lock);
2294 /*-------------------------------- Monitoring -------------------------------*/
2297 static int bond_miimon_inspect(struct bonding *bond)
2299 struct slave *slave;
2300 int i, link_state, commit = 0;
2301 bool ignore_updelay;
2303 ignore_updelay = !bond->curr_active_slave ? true : false;
2305 bond_for_each_slave(bond, slave, i) {
2306 slave->new_link = BOND_LINK_NOCHANGE;
2308 link_state = bond_check_dev_link(bond, slave->dev, 0);
2310 switch (slave->link) {
2315 slave->link = BOND_LINK_FAIL;
2316 slave->delay = bond->params.downdelay;
2318 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2320 (bond->params.mode ==
2321 BOND_MODE_ACTIVEBACKUP) ?
2322 ((slave->state == BOND_STATE_ACTIVE) ?
2323 "active " : "backup ") : "",
2325 bond->params.downdelay * bond->params.miimon);
2328 case BOND_LINK_FAIL:
2331 * recovered before downdelay expired
2333 slave->link = BOND_LINK_UP;
2334 slave->jiffies = jiffies;
2335 pr_info("%s: link status up again after %d ms for interface %s.\n",
2337 (bond->params.downdelay - slave->delay) *
2338 bond->params.miimon,
2343 if (slave->delay <= 0) {
2344 slave->new_link = BOND_LINK_DOWN;
2352 case BOND_LINK_DOWN:
2356 slave->link = BOND_LINK_BACK;
2357 slave->delay = bond->params.updelay;
2360 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2361 bond->dev->name, slave->dev->name,
2362 ignore_updelay ? 0 :
2363 bond->params.updelay *
2364 bond->params.miimon);
2367 case BOND_LINK_BACK:
2369 slave->link = BOND_LINK_DOWN;
2370 pr_info("%s: link status down again after %d ms for interface %s.\n",
2372 (bond->params.updelay - slave->delay) *
2373 bond->params.miimon,
2382 if (slave->delay <= 0) {
2383 slave->new_link = BOND_LINK_UP;
2385 ignore_updelay = false;
2397 static void bond_miimon_commit(struct bonding *bond)
2399 struct slave *slave;
2402 bond_for_each_slave(bond, slave, i) {
2403 switch (slave->new_link) {
2404 case BOND_LINK_NOCHANGE:
2408 slave->link = BOND_LINK_UP;
2409 slave->jiffies = jiffies;
2411 if (bond->params.mode == BOND_MODE_8023AD) {
2412 /* prevent it from being the active one */
2413 slave->state = BOND_STATE_BACKUP;
2414 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2415 /* make it immediately active */
2416 slave->state = BOND_STATE_ACTIVE;
2417 } else if (slave != bond->primary_slave) {
2418 /* prevent it from being the active one */
2419 slave->state = BOND_STATE_BACKUP;
2422 bond_update_speed_duplex(slave);
2424 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2425 bond->dev->name, slave->dev->name,
2426 slave->speed, slave->duplex ? "full" : "half");
2428 /* notify ad that the link status has changed */
2429 if (bond->params.mode == BOND_MODE_8023AD)
2430 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2432 if (bond_is_lb(bond))
2433 bond_alb_handle_link_change(bond, slave,
2436 if (!bond->curr_active_slave ||
2437 (slave == bond->primary_slave))
2442 case BOND_LINK_DOWN:
2443 if (slave->link_failure_count < UINT_MAX)
2444 slave->link_failure_count++;
2446 slave->link = BOND_LINK_DOWN;
2448 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2449 bond->params.mode == BOND_MODE_8023AD)
2450 bond_set_slave_inactive_flags(slave);
2452 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2453 bond->dev->name, slave->dev->name);
2455 if (bond->params.mode == BOND_MODE_8023AD)
2456 bond_3ad_handle_link_change(slave,
2459 if (bond_is_lb(bond))
2460 bond_alb_handle_link_change(bond, slave,
2463 if (slave == bond->curr_active_slave)
2469 pr_err("%s: invalid new link %d on slave %s\n",
2470 bond->dev->name, slave->new_link,
2472 slave->new_link = BOND_LINK_NOCHANGE;
2480 write_lock_bh(&bond->curr_slave_lock);
2481 bond_select_active_slave(bond);
2482 write_unlock_bh(&bond->curr_slave_lock);
2483 unblock_netpoll_tx();
2486 bond_set_carrier(bond);
2492 * Really a wrapper that splits the mii monitor into two phases: an
2493 * inspection, then (if inspection indicates something needs to be done)
2494 * an acquisition of appropriate locks followed by a commit phase to
2495 * implement whatever link state changes are indicated.
2497 void bond_mii_monitor(struct work_struct *work)
2499 struct bonding *bond = container_of(work, struct bonding,
2502 read_lock(&bond->lock);
2503 if (bond->kill_timers)
2506 if (bond->slave_cnt == 0)
2509 if (bond->send_grat_arp) {
2510 read_lock(&bond->curr_slave_lock);
2511 bond_send_gratuitous_arp(bond);
2512 read_unlock(&bond->curr_slave_lock);
2515 if (bond->send_unsol_na) {
2516 read_lock(&bond->curr_slave_lock);
2517 bond_send_unsolicited_na(bond);
2518 read_unlock(&bond->curr_slave_lock);
2521 if (bond_miimon_inspect(bond)) {
2522 read_unlock(&bond->lock);
2524 read_lock(&bond->lock);
2526 bond_miimon_commit(bond);
2528 read_unlock(&bond->lock);
2529 rtnl_unlock(); /* might sleep, hold no other locks */
2530 read_lock(&bond->lock);
2534 if (bond->params.miimon)
2535 queue_delayed_work(bond->wq, &bond->mii_work,
2536 msecs_to_jiffies(bond->params.miimon));
2538 read_unlock(&bond->lock);
2541 static __be32 bond_glean_dev_ip(struct net_device *dev)
2543 struct in_device *idev;
2544 struct in_ifaddr *ifa;
2551 idev = __in_dev_get_rcu(dev);
2555 ifa = idev->ifa_list;
2559 addr = ifa->ifa_local;
2565 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2567 struct vlan_entry *vlan;
2569 if (ip == bond->master_ip)
2572 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2573 if (ip == vlan->vlan_ip)
2581 * We go to the (large) trouble of VLAN tagging ARP frames because
2582 * switches in VLAN mode (especially if ports are configured as
2583 * "native" to a VLAN) might not pass non-tagged frames.
2585 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2587 struct sk_buff *skb;
2589 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2590 slave_dev->name, dest_ip, src_ip, vlan_id);
2592 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2593 NULL, slave_dev->dev_addr, NULL);
2596 pr_err("ARP packet allocation failed\n");
2600 skb = vlan_put_tag(skb, vlan_id);
2602 pr_err("failed to insert VLAN tag\n");
2610 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2613 __be32 *targets = bond->params.arp_targets;
2614 struct vlan_entry *vlan;
2615 struct net_device *vlan_dev;
2619 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2622 pr_debug("basa: target %x\n", targets[i]);
2624 pr_debug("basa: empty vlan: arp_send\n");
2625 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2626 bond->master_ip, 0);
2631 * If VLANs are configured, we do a route lookup to
2632 * determine which VLAN interface would be used, so we
2633 * can tag the ARP with the proper VLAN tag.
2635 memset(&fl, 0, sizeof(fl));
2636 fl.fl4_dst = targets[i];
2637 fl.fl4_tos = RTO_ONLINK;
2639 rv = ip_route_output_key(dev_net(bond->dev), &rt, &fl);
2641 if (net_ratelimit()) {
2642 pr_warning("%s: no route to arp_ip_target %pI4\n",
2643 bond->dev->name, &fl.fl4_dst);
2649 * This target is not on a VLAN
2651 if (rt->dst.dev == bond->dev) {
2653 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2654 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2655 bond->master_ip, 0);
2660 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2661 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2662 if (vlan_dev == rt->dst.dev) {
2663 vlan_id = vlan->vlan_id;
2664 pr_debug("basa: vlan match on %s %d\n",
2665 vlan_dev->name, vlan_id);
2672 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2673 vlan->vlan_ip, vlan_id);
2677 if (net_ratelimit()) {
2678 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2679 bond->dev->name, &fl.fl4_dst,
2680 rt->dst.dev ? rt->dst.dev->name : "NULL");
2687 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2688 * for each VLAN above us.
2690 * Caller must hold curr_slave_lock for read or better
2692 static void bond_send_gratuitous_arp(struct bonding *bond)
2694 struct slave *slave = bond->curr_active_slave;
2695 struct vlan_entry *vlan;
2696 struct net_device *vlan_dev;
2698 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2699 bond->dev->name, slave ? slave->dev->name : "NULL");
2701 if (!slave || !bond->send_grat_arp ||
2702 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
2705 bond->send_grat_arp--;
2707 if (bond->master_ip) {
2708 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2709 bond->master_ip, 0);
2715 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2716 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2717 if (vlan->vlan_ip) {
2718 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2719 vlan->vlan_ip, vlan->vlan_id);
2724 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2727 __be32 *targets = bond->params.arp_targets;
2729 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2730 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2731 &sip, &tip, i, &targets[i],
2732 bond_has_this_ip(bond, tip));
2733 if (sip == targets[i]) {
2734 if (bond_has_this_ip(bond, tip))
2735 slave->last_arp_rx = jiffies;
2741 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2744 struct slave *slave;
2745 struct bonding *bond;
2746 unsigned char *arp_ptr;
2749 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2751 * When using VLANS and bonding, dev and oriv_dev may be
2752 * incorrect if the physical interface supports VLAN
2753 * acceleration. With this change ARP validation now
2754 * works for hosts only reachable on the VLAN interface.
2756 dev = vlan_dev_real_dev(dev);
2757 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2760 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2763 bond = netdev_priv(dev);
2764 read_lock(&bond->lock);
2766 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2767 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2768 orig_dev ? orig_dev->name : "NULL");
2770 slave = bond_get_slave_by_dev(bond, orig_dev);
2771 if (!slave || !slave_do_arp_validate(bond, slave))
2774 skb = skb_share_check(skb, GFP_ATOMIC);
2778 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2782 if (arp->ar_hln != dev->addr_len ||
2783 skb->pkt_type == PACKET_OTHERHOST ||
2784 skb->pkt_type == PACKET_LOOPBACK ||
2785 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2786 arp->ar_pro != htons(ETH_P_IP) ||
2790 arp_ptr = (unsigned char *)(arp + 1);
2791 arp_ptr += dev->addr_len;
2792 memcpy(&sip, arp_ptr, 4);
2793 arp_ptr += 4 + dev->addr_len;
2794 memcpy(&tip, arp_ptr, 4);
2796 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2797 bond->dev->name, slave->dev->name, slave->state,
2798 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2802 * Backup slaves won't see the ARP reply, but do come through
2803 * here for each ARP probe (so we swap the sip/tip to validate
2804 * the probe). In a "redundant switch, common router" type of
2805 * configuration, the ARP probe will (hopefully) travel from
2806 * the active, through one switch, the router, then the other
2807 * switch before reaching the backup.
2809 if (slave->state == BOND_STATE_ACTIVE)
2810 bond_validate_arp(bond, slave, sip, tip);
2812 bond_validate_arp(bond, slave, tip, sip);
2815 read_unlock(&bond->lock);
2818 return NET_RX_SUCCESS;
2822 * this function is called regularly to monitor each slave's link
2823 * ensuring that traffic is being sent and received when arp monitoring
2824 * is used in load-balancing mode. if the adapter has been dormant, then an
2825 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2826 * arp monitoring in active backup mode.
2828 void bond_loadbalance_arp_mon(struct work_struct *work)
2830 struct bonding *bond = container_of(work, struct bonding,
2832 struct slave *slave, *oldcurrent;
2833 int do_failover = 0;
2837 read_lock(&bond->lock);
2839 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2841 if (bond->kill_timers)
2844 if (bond->slave_cnt == 0)
2847 read_lock(&bond->curr_slave_lock);
2848 oldcurrent = bond->curr_active_slave;
2849 read_unlock(&bond->curr_slave_lock);
2851 /* see if any of the previous devices are up now (i.e. they have
2852 * xmt and rcv traffic). the curr_active_slave does not come into
2853 * the picture unless it is null. also, slave->jiffies is not needed
2854 * here because we send an arp on each slave and give a slave as
2855 * long as it needs to get the tx/rx within the delta.
2856 * TODO: what about up/down delay in arp mode? it wasn't here before
2859 bond_for_each_slave(bond, slave, i) {
2860 unsigned long trans_start = dev_trans_start(slave->dev);
2862 if (slave->link != BOND_LINK_UP) {
2863 if (time_in_range(jiffies,
2864 trans_start - delta_in_ticks,
2865 trans_start + delta_in_ticks) &&
2866 time_in_range(jiffies,
2867 slave->dev->last_rx - delta_in_ticks,
2868 slave->dev->last_rx + delta_in_ticks)) {
2870 slave->link = BOND_LINK_UP;
2871 slave->state = BOND_STATE_ACTIVE;
2873 /* primary_slave has no meaning in round-robin
2874 * mode. the window of a slave being up and
2875 * curr_active_slave being null after enslaving
2879 pr_info("%s: link status definitely up for interface %s, ",
2884 pr_info("%s: interface %s is now up\n",
2890 /* slave->link == BOND_LINK_UP */
2892 /* not all switches will respond to an arp request
2893 * when the source ip is 0, so don't take the link down
2894 * if we don't know our ip yet
2896 if (!time_in_range(jiffies,
2897 trans_start - delta_in_ticks,
2898 trans_start + 2 * delta_in_ticks) ||
2899 !time_in_range(jiffies,
2900 slave->dev->last_rx - delta_in_ticks,
2901 slave->dev->last_rx + 2 * delta_in_ticks)) {
2903 slave->link = BOND_LINK_DOWN;
2904 slave->state = BOND_STATE_BACKUP;
2906 if (slave->link_failure_count < UINT_MAX)
2907 slave->link_failure_count++;
2909 pr_info("%s: interface %s is now down.\n",
2913 if (slave == oldcurrent)
2918 /* note: if switch is in round-robin mode, all links
2919 * must tx arp to ensure all links rx an arp - otherwise
2920 * links may oscillate or not come up at all; if switch is
2921 * in something like xor mode, there is nothing we can
2922 * do - all replies will be rx'ed on same link causing slaves
2923 * to be unstable during low/no traffic periods
2925 if (IS_UP(slave->dev))
2926 bond_arp_send_all(bond, slave);
2931 write_lock_bh(&bond->curr_slave_lock);
2933 bond_select_active_slave(bond);
2935 write_unlock_bh(&bond->curr_slave_lock);
2936 unblock_netpoll_tx();
2940 if (bond->params.arp_interval)
2941 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2943 read_unlock(&bond->lock);
2947 * Called to inspect slaves for active-backup mode ARP monitor link state
2948 * changes. Sets new_link in slaves to specify what action should take
2949 * place for the slave. Returns 0 if no changes are found, >0 if changes
2950 * to link states must be committed.
2952 * Called with bond->lock held for read.
2954 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
2956 struct slave *slave;
2958 unsigned long trans_start;
2960 bond_for_each_slave(bond, slave, i) {
2961 slave->new_link = BOND_LINK_NOCHANGE;
2963 if (slave->link != BOND_LINK_UP) {
2964 if (time_in_range(jiffies,
2965 slave_last_rx(bond, slave) - delta_in_ticks,
2966 slave_last_rx(bond, slave) + delta_in_ticks)) {
2968 slave->new_link = BOND_LINK_UP;
2976 * Give slaves 2*delta after being enslaved or made
2977 * active. This avoids bouncing, as the last receive
2978 * times need a full ARP monitor cycle to be updated.
2980 if (time_in_range(jiffies,
2981 slave->jiffies - delta_in_ticks,
2982 slave->jiffies + 2 * delta_in_ticks))
2986 * Backup slave is down if:
2987 * - No current_arp_slave AND
2988 * - more than 3*delta since last receive AND
2989 * - the bond has an IP address
2991 * Note: a non-null current_arp_slave indicates
2992 * the curr_active_slave went down and we are
2993 * searching for a new one; under this condition
2994 * we only take the curr_active_slave down - this
2995 * gives each slave a chance to tx/rx traffic
2996 * before being taken out
2998 if (slave->state == BOND_STATE_BACKUP &&
2999 !bond->current_arp_slave &&
3000 !time_in_range(jiffies,
3001 slave_last_rx(bond, slave) - delta_in_ticks,
3002 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3004 slave->new_link = BOND_LINK_DOWN;
3009 * Active slave is down if:
3010 * - more than 2*delta since transmitting OR
3011 * - (more than 2*delta since receive AND
3012 * the bond has an IP address)
3014 trans_start = dev_trans_start(slave->dev);
3015 if ((slave->state == BOND_STATE_ACTIVE) &&
3016 (!time_in_range(jiffies,
3017 trans_start - delta_in_ticks,
3018 trans_start + 2 * delta_in_ticks) ||
3019 !time_in_range(jiffies,
3020 slave_last_rx(bond, slave) - delta_in_ticks,
3021 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3023 slave->new_link = BOND_LINK_DOWN;
3032 * Called to commit link state changes noted by inspection step of
3033 * active-backup mode ARP monitor.
3035 * Called with RTNL and bond->lock for read.
3037 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3039 struct slave *slave;
3041 unsigned long trans_start;
3043 bond_for_each_slave(bond, slave, i) {
3044 switch (slave->new_link) {
3045 case BOND_LINK_NOCHANGE:
3049 trans_start = dev_trans_start(slave->dev);
3050 if ((!bond->curr_active_slave &&
3051 time_in_range(jiffies,
3052 trans_start - delta_in_ticks,
3053 trans_start + delta_in_ticks)) ||
3054 bond->curr_active_slave != slave) {
3055 slave->link = BOND_LINK_UP;
3056 bond->current_arp_slave = NULL;
3058 pr_info("%s: link status definitely up for interface %s.\n",
3059 bond->dev->name, slave->dev->name);
3061 if (!bond->curr_active_slave ||
3062 (slave == bond->primary_slave))
3069 case BOND_LINK_DOWN:
3070 if (slave->link_failure_count < UINT_MAX)
3071 slave->link_failure_count++;
3073 slave->link = BOND_LINK_DOWN;
3074 bond_set_slave_inactive_flags(slave);
3076 pr_info("%s: link status definitely down for interface %s, disabling it\n",
3077 bond->dev->name, slave->dev->name);
3079 if (slave == bond->curr_active_slave) {
3080 bond->current_arp_slave = NULL;
3087 pr_err("%s: impossible: new_link %d on slave %s\n",
3088 bond->dev->name, slave->new_link,
3096 write_lock_bh(&bond->curr_slave_lock);
3097 bond_select_active_slave(bond);
3098 write_unlock_bh(&bond->curr_slave_lock);
3099 unblock_netpoll_tx();
3102 bond_set_carrier(bond);
3106 * Send ARP probes for active-backup mode ARP monitor.
3108 * Called with bond->lock held for read.
3110 static void bond_ab_arp_probe(struct bonding *bond)
3112 struct slave *slave;
3115 read_lock(&bond->curr_slave_lock);
3117 if (bond->current_arp_slave && bond->curr_active_slave)
3118 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3119 bond->current_arp_slave->dev->name,
3120 bond->curr_active_slave->dev->name);
3122 if (bond->curr_active_slave) {
3123 bond_arp_send_all(bond, bond->curr_active_slave);
3124 read_unlock(&bond->curr_slave_lock);
3128 read_unlock(&bond->curr_slave_lock);
3130 /* if we don't have a curr_active_slave, search for the next available
3131 * backup slave from the current_arp_slave and make it the candidate
3132 * for becoming the curr_active_slave
3135 if (!bond->current_arp_slave) {
3136 bond->current_arp_slave = bond->first_slave;
3137 if (!bond->current_arp_slave)
3141 bond_set_slave_inactive_flags(bond->current_arp_slave);
3143 /* search for next candidate */
3144 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3145 if (IS_UP(slave->dev)) {
3146 slave->link = BOND_LINK_BACK;
3147 bond_set_slave_active_flags(slave);
3148 bond_arp_send_all(bond, slave);
3149 slave->jiffies = jiffies;
3150 bond->current_arp_slave = slave;
3154 /* if the link state is up at this point, we
3155 * mark it down - this can happen if we have
3156 * simultaneous link failures and
3157 * reselect_active_interface doesn't make this
3158 * one the current slave so it is still marked
3159 * up when it is actually down
3161 if (slave->link == BOND_LINK_UP) {
3162 slave->link = BOND_LINK_DOWN;
3163 if (slave->link_failure_count < UINT_MAX)
3164 slave->link_failure_count++;
3166 bond_set_slave_inactive_flags(slave);
3168 pr_info("%s: backup interface %s is now down.\n",
3169 bond->dev->name, slave->dev->name);
3174 void bond_activebackup_arp_mon(struct work_struct *work)
3176 struct bonding *bond = container_of(work, struct bonding,
3180 read_lock(&bond->lock);
3182 if (bond->kill_timers)
3185 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3187 if (bond->slave_cnt == 0)
3190 if (bond->send_grat_arp) {
3191 read_lock(&bond->curr_slave_lock);
3192 bond_send_gratuitous_arp(bond);
3193 read_unlock(&bond->curr_slave_lock);
3196 if (bond->send_unsol_na) {
3197 read_lock(&bond->curr_slave_lock);
3198 bond_send_unsolicited_na(bond);
3199 read_unlock(&bond->curr_slave_lock);
3202 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3203 read_unlock(&bond->lock);
3205 read_lock(&bond->lock);
3207 bond_ab_arp_commit(bond, delta_in_ticks);
3209 read_unlock(&bond->lock);
3211 read_lock(&bond->lock);
3214 bond_ab_arp_probe(bond);
3217 if (bond->params.arp_interval)
3218 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3220 read_unlock(&bond->lock);
3223 /*------------------------------ proc/seq_file-------------------------------*/
3225 #ifdef CONFIG_PROC_FS
3227 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3229 __acquires(&bond->lock)
3231 struct bonding *bond = seq->private;
3233 struct slave *slave;
3236 /* make sure the bond won't be taken away */
3238 read_lock(&bond->lock);
3241 return SEQ_START_TOKEN;
3243 bond_for_each_slave(bond, slave, i) {
3251 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3253 struct bonding *bond = seq->private;
3254 struct slave *slave = v;
3257 if (v == SEQ_START_TOKEN)
3258 return bond->first_slave;
3260 slave = slave->next;
3262 return (slave == bond->first_slave) ? NULL : slave;
3265 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3266 __releases(&bond->lock)
3269 struct bonding *bond = seq->private;
3271 read_unlock(&bond->lock);
3275 static void bond_info_show_master(struct seq_file *seq)
3277 struct bonding *bond = seq->private;
3281 read_lock(&bond->curr_slave_lock);
3282 curr = bond->curr_active_slave;
3283 read_unlock(&bond->curr_slave_lock);
3285 seq_printf(seq, "Bonding Mode: %s",
3286 bond_mode_name(bond->params.mode));
3288 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3289 bond->params.fail_over_mac)
3290 seq_printf(seq, " (fail_over_mac %s)",
3291 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
3293 seq_printf(seq, "\n");
3295 if (bond->params.mode == BOND_MODE_XOR ||
3296 bond->params.mode == BOND_MODE_8023AD) {
3297 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3298 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3299 bond->params.xmit_policy);
3302 if (USES_PRIMARY(bond->params.mode)) {
3303 seq_printf(seq, "Primary Slave: %s",
3304 (bond->primary_slave) ?
3305 bond->primary_slave->dev->name : "None");
3306 if (bond->primary_slave)
3307 seq_printf(seq, " (primary_reselect %s)",
3308 pri_reselect_tbl[bond->params.primary_reselect].modename);
3310 seq_printf(seq, "\nCurrently Active Slave: %s\n",
3311 (curr) ? curr->dev->name : "None");
3314 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3316 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3317 seq_printf(seq, "Up Delay (ms): %d\n",
3318 bond->params.updelay * bond->params.miimon);
3319 seq_printf(seq, "Down Delay (ms): %d\n",
3320 bond->params.downdelay * bond->params.miimon);
3323 /* ARP information */
3324 if (bond->params.arp_interval > 0) {
3326 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3327 bond->params.arp_interval);
3329 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3331 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
3332 if (!bond->params.arp_targets[i])
3335 seq_printf(seq, ",");
3336 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
3339 seq_printf(seq, "\n");
3342 if (bond->params.mode == BOND_MODE_8023AD) {
3343 struct ad_info ad_info;
3345 seq_puts(seq, "\n802.3ad info\n");
3346 seq_printf(seq, "LACP rate: %s\n",
3347 (bond->params.lacp_fast) ? "fast" : "slow");
3348 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3349 ad_select_tbl[bond->params.ad_select].modename);
3351 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3352 seq_printf(seq, "bond %s has no active aggregator\n",
3355 seq_printf(seq, "Active Aggregator Info:\n");
3357 seq_printf(seq, "\tAggregator ID: %d\n",
3358 ad_info.aggregator_id);
3359 seq_printf(seq, "\tNumber of ports: %d\n",
3361 seq_printf(seq, "\tActor Key: %d\n",
3363 seq_printf(seq, "\tPartner Key: %d\n",
3364 ad_info.partner_key);
3365 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3366 ad_info.partner_system);
3371 static void bond_info_show_slave(struct seq_file *seq,
3372 const struct slave *slave)
3374 struct bonding *bond = seq->private;
3376 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3377 seq_printf(seq, "MII Status: %s\n",
3378 (slave->link == BOND_LINK_UP) ? "up" : "down");
3379 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3380 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
3381 seq_printf(seq, "Link Failure Count: %u\n",
3382 slave->link_failure_count);
3384 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
3386 if (bond->params.mode == BOND_MODE_8023AD) {
3387 const struct aggregator *agg
3388 = SLAVE_AD_INFO(slave).port.aggregator;
3391 seq_printf(seq, "Aggregator ID: %d\n",
3392 agg->aggregator_identifier);
3394 seq_puts(seq, "Aggregator ID: N/A\n");
3396 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
3399 static int bond_info_seq_show(struct seq_file *seq, void *v)
3401 if (v == SEQ_START_TOKEN) {
3402 seq_printf(seq, "%s\n", version);
3403 bond_info_show_master(seq);
3405 bond_info_show_slave(seq, v);
3410 static const struct seq_operations bond_info_seq_ops = {
3411 .start = bond_info_seq_start,
3412 .next = bond_info_seq_next,
3413 .stop = bond_info_seq_stop,
3414 .show = bond_info_seq_show,
3417 static int bond_info_open(struct inode *inode, struct file *file)
3419 struct seq_file *seq;
3420 struct proc_dir_entry *proc;
3423 res = seq_open(file, &bond_info_seq_ops);
3425 /* recover the pointer buried in proc_dir_entry data */
3426 seq = file->private_data;
3428 seq->private = proc->data;
3434 static const struct file_operations bond_info_fops = {
3435 .owner = THIS_MODULE,
3436 .open = bond_info_open,
3438 .llseek = seq_lseek,
3439 .release = seq_release,
3442 static void bond_create_proc_entry(struct bonding *bond)
3444 struct net_device *bond_dev = bond->dev;
3445 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3448 bond->proc_entry = proc_create_data(bond_dev->name,
3449 S_IRUGO, bn->proc_dir,
3450 &bond_info_fops, bond);
3451 if (bond->proc_entry == NULL)
3452 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3453 DRV_NAME, bond_dev->name);
3455 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3459 static void bond_remove_proc_entry(struct bonding *bond)
3461 struct net_device *bond_dev = bond->dev;
3462 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3464 if (bn->proc_dir && bond->proc_entry) {
3465 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
3466 memset(bond->proc_file_name, 0, IFNAMSIZ);
3467 bond->proc_entry = NULL;
3471 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3472 * Caller must hold rtnl_lock.
3474 static void __net_init bond_create_proc_dir(struct bond_net *bn)
3476 if (!bn->proc_dir) {
3477 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3479 pr_warning("Warning: cannot create /proc/net/%s\n",
3484 /* Destroy the bonding directory under /proc/net, if empty.
3485 * Caller must hold rtnl_lock.
3487 static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
3490 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3491 bn->proc_dir = NULL;
3495 #else /* !CONFIG_PROC_FS */
3497 static void bond_create_proc_entry(struct bonding *bond)
3501 static void bond_remove_proc_entry(struct bonding *bond)
3505 static inline void bond_create_proc_dir(struct bond_net *bn)
3509 static inline void bond_destroy_proc_dir(struct bond_net *bn)
3513 #endif /* CONFIG_PROC_FS */
3516 /*-------------------------- netdev event handling --------------------------*/
3519 * Change device name
3521 static int bond_event_changename(struct bonding *bond)
3523 bond_remove_proc_entry(bond);
3524 bond_create_proc_entry(bond);
3526 bond_debug_reregister(bond);
3531 static int bond_master_netdev_event(unsigned long event,
3532 struct net_device *bond_dev)
3534 struct bonding *event_bond = netdev_priv(bond_dev);
3537 case NETDEV_CHANGENAME:
3538 return bond_event_changename(event_bond);
3546 static int bond_slave_netdev_event(unsigned long event,
3547 struct net_device *slave_dev)
3549 struct net_device *bond_dev = slave_dev->master;
3550 struct bonding *bond = netdev_priv(bond_dev);
3553 case NETDEV_UNREGISTER:
3555 if (bond->setup_by_slave)
3556 bond_release_and_destroy(bond_dev, slave_dev);
3558 bond_release(bond_dev, slave_dev);
3562 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3563 struct slave *slave;
3565 slave = bond_get_slave_by_dev(bond, slave_dev);
3567 u16 old_speed = slave->speed;
3568 u16 old_duplex = slave->duplex;
3570 bond_update_speed_duplex(slave);
3572 if (bond_is_lb(bond))
3575 if (old_speed != slave->speed)
3576 bond_3ad_adapter_speed_changed(slave);
3577 if (old_duplex != slave->duplex)
3578 bond_3ad_adapter_duplex_changed(slave);
3585 * ... Or is it this?
3588 case NETDEV_CHANGEMTU:
3590 * TODO: Should slaves be allowed to
3591 * independently alter their MTU? For
3592 * an active-backup bond, slaves need
3593 * not be the same type of device, so
3594 * MTUs may vary. For other modes,
3595 * slaves arguably should have the
3596 * same MTUs. To do this, we'd need to
3597 * take over the slave's change_mtu
3598 * function for the duration of their
3602 case NETDEV_CHANGENAME:
3604 * TODO: handle changing the primary's name
3607 case NETDEV_FEAT_CHANGE:
3608 bond_compute_features(bond);
3618 * bond_netdev_event: handle netdev notifier chain events.
3620 * This function receives events for the netdev chain. The caller (an
3621 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3622 * locks for us to safely manipulate the slave devices (RTNL lock,
3625 static int bond_netdev_event(struct notifier_block *this,
3626 unsigned long event, void *ptr)
3628 struct net_device *event_dev = (struct net_device *)ptr;
3630 pr_debug("event_dev: %s, event: %lx\n",
3631 event_dev ? event_dev->name : "None",
3634 if (!(event_dev->priv_flags & IFF_BONDING))
3637 if (event_dev->flags & IFF_MASTER) {
3638 pr_debug("IFF_MASTER\n");
3639 return bond_master_netdev_event(event, event_dev);
3642 if (event_dev->flags & IFF_SLAVE) {
3643 pr_debug("IFF_SLAVE\n");
3644 return bond_slave_netdev_event(event, event_dev);
3651 * bond_inetaddr_event: handle inetaddr notifier chain events.
3653 * We keep track of device IPs primarily to use as source addresses in
3654 * ARP monitor probes (rather than spewing out broadcasts all the time).
3656 * We track one IP for the main device (if it has one), plus one per VLAN.
3658 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3660 struct in_ifaddr *ifa = ptr;
3661 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3662 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
3663 struct bonding *bond;
3664 struct vlan_entry *vlan;
3666 list_for_each_entry(bond, &bn->dev_list, bond_list) {
3667 if (bond->dev == event_dev) {
3670 bond->master_ip = ifa->ifa_local;
3673 bond->master_ip = bond_glean_dev_ip(bond->dev);
3680 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3683 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3684 if (vlan_dev == event_dev) {
3687 vlan->vlan_ip = ifa->ifa_local;
3691 bond_glean_dev_ip(vlan_dev);
3702 static struct notifier_block bond_netdev_notifier = {
3703 .notifier_call = bond_netdev_event,
3706 static struct notifier_block bond_inetaddr_notifier = {
3707 .notifier_call = bond_inetaddr_event,
3710 /*-------------------------- Packet type handling ---------------------------*/
3712 /* register to receive lacpdus on a bond */
3713 static void bond_register_lacpdu(struct bonding *bond)
3715 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3717 /* initialize packet type */
3718 pk_type->type = PKT_TYPE_LACPDU;
3719 pk_type->dev = bond->dev;
3720 pk_type->func = bond_3ad_lacpdu_recv;
3722 dev_add_pack(pk_type);
3725 /* unregister to receive lacpdus on a bond */
3726 static void bond_unregister_lacpdu(struct bonding *bond)
3728 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3731 void bond_register_arp(struct bonding *bond)
3733 struct packet_type *pt = &bond->arp_mon_pt;
3738 pt->type = htons(ETH_P_ARP);
3739 pt->dev = bond->dev;
3740 pt->func = bond_arp_rcv;
3744 void bond_unregister_arp(struct bonding *bond)
3746 struct packet_type *pt = &bond->arp_mon_pt;
3748 dev_remove_pack(pt);
3752 /*---------------------------- Hashing Policies -----------------------------*/
3755 * Hash for the output device based upon layer 2 and layer 3 data. If
3756 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3758 static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
3760 struct ethhdr *data = (struct ethhdr *)skb->data;
3761 struct iphdr *iph = ip_hdr(skb);
3763 if (skb->protocol == htons(ETH_P_IP)) {
3764 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3765 (data->h_dest[5] ^ data->h_source[5])) % count;
3768 return (data->h_dest[5] ^ data->h_source[5]) % count;
3772 * Hash for the output device based upon layer 3 and layer 4 data. If
3773 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3774 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3776 static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
3778 struct ethhdr *data = (struct ethhdr *)skb->data;
3779 struct iphdr *iph = ip_hdr(skb);
3780 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3783 if (skb->protocol == htons(ETH_P_IP)) {
3784 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
3785 (iph->protocol == IPPROTO_TCP ||
3786 iph->protocol == IPPROTO_UDP)) {
3787 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3789 return (layer4_xor ^
3790 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3794 return (data->h_dest[5] ^ data->h_source[5]) % count;
3798 * Hash for the output device based upon layer 2 data
3800 static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
3802 struct ethhdr *data = (struct ethhdr *)skb->data;
3804 return (data->h_dest[5] ^ data->h_source[5]) % count;
3807 /*-------------------------- Device entry points ----------------------------*/
3809 static int bond_open(struct net_device *bond_dev)
3811 struct bonding *bond = netdev_priv(bond_dev);
3813 bond->kill_timers = 0;
3815 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3817 if (bond_is_lb(bond)) {
3818 /* bond_alb_initialize must be called before the timer
3821 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3822 /* something went wrong - fail the open operation */
3826 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3827 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3830 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3831 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3832 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3835 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3836 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3837 INIT_DELAYED_WORK(&bond->arp_work,
3838 bond_activebackup_arp_mon);
3840 INIT_DELAYED_WORK(&bond->arp_work,
3841 bond_loadbalance_arp_mon);
3843 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3844 if (bond->params.arp_validate)
3845 bond_register_arp(bond);
3848 if (bond->params.mode == BOND_MODE_8023AD) {
3849 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3850 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3851 /* register to receive LACPDUs */
3852 bond_register_lacpdu(bond);
3853 bond_3ad_initiate_agg_selection(bond, 1);
3859 static int bond_close(struct net_device *bond_dev)
3861 struct bonding *bond = netdev_priv(bond_dev);
3863 if (bond->params.mode == BOND_MODE_8023AD) {
3864 /* Unregister the receive of LACPDUs */
3865 bond_unregister_lacpdu(bond);
3868 if (bond->params.arp_validate)
3869 bond_unregister_arp(bond);
3871 write_lock_bh(&bond->lock);
3873 bond->send_grat_arp = 0;
3874 bond->send_unsol_na = 0;
3876 /* signal timers not to re-arm */
3877 bond->kill_timers = 1;
3879 write_unlock_bh(&bond->lock);
3881 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3882 cancel_delayed_work(&bond->mii_work);
3885 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3886 cancel_delayed_work(&bond->arp_work);
3889 switch (bond->params.mode) {
3890 case BOND_MODE_8023AD:
3891 cancel_delayed_work(&bond->ad_work);
3895 cancel_delayed_work(&bond->alb_work);
3901 if (delayed_work_pending(&bond->mcast_work))
3902 cancel_delayed_work(&bond->mcast_work);
3904 if (bond_is_lb(bond)) {
3905 /* Must be called only after all
3906 * slaves have been released
3908 bond_alb_deinitialize(bond);
3914 static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3915 struct rtnl_link_stats64 *stats)
3917 struct bonding *bond = netdev_priv(bond_dev);
3918 struct rtnl_link_stats64 temp;
3919 struct slave *slave;
3922 memset(stats, 0, sizeof(*stats));
3924 read_lock_bh(&bond->lock);
3926 bond_for_each_slave(bond, slave, i) {
3927 const struct rtnl_link_stats64 *sstats =
3928 dev_get_stats(slave->dev, &temp);
3930 stats->rx_packets += sstats->rx_packets;
3931 stats->rx_bytes += sstats->rx_bytes;
3932 stats->rx_errors += sstats->rx_errors;
3933 stats->rx_dropped += sstats->rx_dropped;
3935 stats->tx_packets += sstats->tx_packets;
3936 stats->tx_bytes += sstats->tx_bytes;
3937 stats->tx_errors += sstats->tx_errors;
3938 stats->tx_dropped += sstats->tx_dropped;
3940 stats->multicast += sstats->multicast;
3941 stats->collisions += sstats->collisions;
3943 stats->rx_length_errors += sstats->rx_length_errors;
3944 stats->rx_over_errors += sstats->rx_over_errors;
3945 stats->rx_crc_errors += sstats->rx_crc_errors;
3946 stats->rx_frame_errors += sstats->rx_frame_errors;
3947 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3948 stats->rx_missed_errors += sstats->rx_missed_errors;
3950 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3951 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3952 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3953 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3954 stats->tx_window_errors += sstats->tx_window_errors;
3957 read_unlock_bh(&bond->lock);
3962 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3964 struct net_device *slave_dev = NULL;
3965 struct ifbond k_binfo;
3966 struct ifbond __user *u_binfo = NULL;
3967 struct ifslave k_sinfo;
3968 struct ifslave __user *u_sinfo = NULL;
3969 struct mii_ioctl_data *mii = NULL;
3972 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
3984 * We do this again just in case we were called by SIOCGMIIREG
3985 * instead of SIOCGMIIPHY.
3992 if (mii->reg_num == 1) {
3993 struct bonding *bond = netdev_priv(bond_dev);
3995 read_lock(&bond->lock);
3996 read_lock(&bond->curr_slave_lock);
3997 if (netif_carrier_ok(bond->dev))
3998 mii->val_out = BMSR_LSTATUS;
4000 read_unlock(&bond->curr_slave_lock);
4001 read_unlock(&bond->lock);
4005 case BOND_INFO_QUERY_OLD:
4006 case SIOCBONDINFOQUERY:
4007 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4009 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
4012 res = bond_info_query(bond_dev, &k_binfo);
4014 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4018 case BOND_SLAVE_INFO_QUERY_OLD:
4019 case SIOCBONDSLAVEINFOQUERY:
4020 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4022 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
4025 res = bond_slave_info_query(bond_dev, &k_sinfo);
4027 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4036 if (!capable(CAP_NET_ADMIN))
4039 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
4041 pr_debug("slave_dev=%p:\n", slave_dev);
4046 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
4048 case BOND_ENSLAVE_OLD:
4049 case SIOCBONDENSLAVE:
4050 res = bond_enslave(bond_dev, slave_dev);
4052 case BOND_RELEASE_OLD:
4053 case SIOCBONDRELEASE:
4054 res = bond_release(bond_dev, slave_dev);
4056 case BOND_SETHWADDR_OLD:
4057 case SIOCBONDSETHWADDR:
4058 res = bond_sethwaddr(bond_dev, slave_dev);
4060 case BOND_CHANGE_ACTIVE_OLD:
4061 case SIOCBONDCHANGEACTIVE:
4062 res = bond_ioctl_change_active(bond_dev, slave_dev);
4074 static bool bond_addr_in_mc_list(unsigned char *addr,
4075 struct netdev_hw_addr_list *list,
4078 struct netdev_hw_addr *ha;
4080 netdev_hw_addr_list_for_each(ha, list)
4081 if (!memcmp(ha->addr, addr, addrlen))
4087 static void bond_set_multicast_list(struct net_device *bond_dev)
4089 struct bonding *bond = netdev_priv(bond_dev);
4090 struct netdev_hw_addr *ha;
4094 * Do promisc before checking multicast_mode
4096 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
4098 * FIXME: Need to handle the error when one of the multi-slaves
4101 bond_set_promiscuity(bond, 1);
4104 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
4105 bond_set_promiscuity(bond, -1);
4108 /* set allmulti flag to slaves */
4109 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
4111 * FIXME: Need to handle the error when one of the multi-slaves
4114 bond_set_allmulti(bond, 1);
4117 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
4118 bond_set_allmulti(bond, -1);
4121 read_lock(&bond->lock);
4123 bond->flags = bond_dev->flags;
4125 /* looking for addresses to add to slaves' mc list */
4126 netdev_for_each_mc_addr(ha, bond_dev) {
4127 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4128 bond_dev->addr_len);
4130 bond_mc_add(bond, ha->addr);
4133 /* looking for addresses to delete from slaves' list */
4134 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4135 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4136 bond_dev->addr_len);
4138 bond_mc_del(bond, ha->addr);
4141 /* save master's multicast list */
4142 __hw_addr_flush(&bond->mc_list);
4143 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4144 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
4146 read_unlock(&bond->lock);
4149 static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4151 struct bonding *bond = netdev_priv(dev);
4152 struct slave *slave = bond->first_slave;
4155 const struct net_device_ops *slave_ops
4156 = slave->dev->netdev_ops;
4157 if (slave_ops->ndo_neigh_setup)
4158 return slave_ops->ndo_neigh_setup(slave->dev, parms);
4164 * Change the MTU of all of a master's slaves to match the master
4166 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4168 struct bonding *bond = netdev_priv(bond_dev);
4169 struct slave *slave, *stop_at;
4173 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
4174 (bond_dev ? bond_dev->name : "None"), new_mtu);
4176 /* Can't hold bond->lock with bh disabled here since
4177 * some base drivers panic. On the other hand we can't
4178 * hold bond->lock without bh disabled because we'll
4179 * deadlock. The only solution is to rely on the fact
4180 * that we're under rtnl_lock here, and the slaves
4181 * list won't change. This doesn't solve the problem
4182 * of setting the slave's MTU while it is
4183 * transmitting, but the assumption is that the base
4184 * driver can handle that.
4186 * TODO: figure out a way to safely iterate the slaves
4187 * list, but without holding a lock around the actual
4188 * call to the base driver.
4191 bond_for_each_slave(bond, slave, i) {
4192 pr_debug("s %p s->p %p c_m %p\n",
4195 slave->dev->netdev_ops->ndo_change_mtu);
4197 res = dev_set_mtu(slave->dev, new_mtu);
4200 /* If we failed to set the slave's mtu to the new value
4201 * we must abort the operation even in ACTIVE_BACKUP
4202 * mode, because if we allow the backup slaves to have
4203 * different mtu values than the active slave we'll
4204 * need to change their mtu when doing a failover. That
4205 * means changing their mtu from timer context, which
4206 * is probably not a good idea.
4208 pr_debug("err %d %s\n", res, slave->dev->name);
4213 bond_dev->mtu = new_mtu;
4218 /* unwind from head to the slave that failed */
4220 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4223 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4225 pr_debug("unwind err %d dev %s\n",
4226 tmp_res, slave->dev->name);
4236 * Note that many devices must be down to change the HW address, and
4237 * downing the master releases all slaves. We can make bonds full of
4238 * bonding devices to test this, however.
4240 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4242 struct bonding *bond = netdev_priv(bond_dev);
4243 struct sockaddr *sa = addr, tmp_sa;
4244 struct slave *slave, *stop_at;
4248 if (bond->params.mode == BOND_MODE_ALB)
4249 return bond_alb_set_mac_address(bond_dev, addr);
4252 pr_debug("bond=%p, name=%s\n",
4253 bond, bond_dev ? bond_dev->name : "None");
4256 * If fail_over_mac is set to active, do nothing and return
4257 * success. Returning an error causes ifenslave to fail.
4259 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
4262 if (!is_valid_ether_addr(sa->sa_data))
4263 return -EADDRNOTAVAIL;
4265 /* Can't hold bond->lock with bh disabled here since
4266 * some base drivers panic. On the other hand we can't
4267 * hold bond->lock without bh disabled because we'll
4268 * deadlock. The only solution is to rely on the fact
4269 * that we're under rtnl_lock here, and the slaves
4270 * list won't change. This doesn't solve the problem
4271 * of setting the slave's hw address while it is
4272 * transmitting, but the assumption is that the base
4273 * driver can handle that.
4275 * TODO: figure out a way to safely iterate the slaves
4276 * list, but without holding a lock around the actual
4277 * call to the base driver.
4280 bond_for_each_slave(bond, slave, i) {
4281 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
4282 pr_debug("slave %p %s\n", slave, slave->dev->name);
4284 if (slave_ops->ndo_set_mac_address == NULL) {
4286 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
4290 res = dev_set_mac_address(slave->dev, addr);
4292 /* TODO: consider downing the slave
4294 * User should expect communications
4295 * breakage anyway until ARP finish
4298 pr_debug("err %d %s\n", res, slave->dev->name);
4304 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4308 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4309 tmp_sa.sa_family = bond_dev->type;
4311 /* unwind from head to the slave that failed */
4313 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4316 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4318 pr_debug("unwind err %d dev %s\n",
4319 tmp_res, slave->dev->name);
4326 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4328 struct bonding *bond = netdev_priv(bond_dev);
4329 struct slave *slave, *start_at;
4330 int i, slave_no, res = 1;
4331 struct iphdr *iph = ip_hdr(skb);
4333 read_lock(&bond->lock);
4335 if (!BOND_IS_OK(bond))
4338 * Start with the curr_active_slave that joined the bond as the
4339 * default for sending IGMP traffic. For failover purposes one
4340 * needs to maintain some consistency for the interface that will
4341 * send the join/membership reports. The curr_active_slave found
4342 * will send all of this type of traffic.
4344 if ((iph->protocol == IPPROTO_IGMP) &&
4345 (skb->protocol == htons(ETH_P_IP))) {
4347 read_lock(&bond->curr_slave_lock);
4348 slave = bond->curr_active_slave;
4349 read_unlock(&bond->curr_slave_lock);
4355 * Concurrent TX may collide on rr_tx_counter; we accept
4356 * that as being rare enough not to justify using an
4359 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4361 bond_for_each_slave(bond, slave, i) {
4369 bond_for_each_slave_from(bond, slave, i, start_at) {
4370 if (IS_UP(slave->dev) &&
4371 (slave->link == BOND_LINK_UP) &&
4372 (slave->state == BOND_STATE_ACTIVE)) {
4373 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4380 /* no suitable interface, frame not sent */
4383 read_unlock(&bond->lock);
4384 return NETDEV_TX_OK;
4389 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4390 * the bond has a usable interface.
4392 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4394 struct bonding *bond = netdev_priv(bond_dev);
4397 read_lock(&bond->lock);
4398 read_lock(&bond->curr_slave_lock);
4400 if (!BOND_IS_OK(bond))
4403 if (!bond->curr_active_slave)
4406 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4410 /* no suitable interface, frame not sent */
4413 read_unlock(&bond->curr_slave_lock);
4414 read_unlock(&bond->lock);
4415 return NETDEV_TX_OK;
4419 * In bond_xmit_xor() , we determine the output device by using a pre-
4420 * determined xmit_hash_policy(), If the selected device is not enabled,
4421 * find the next active slave.
4423 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4425 struct bonding *bond = netdev_priv(bond_dev);
4426 struct slave *slave, *start_at;
4431 read_lock(&bond->lock);
4433 if (!BOND_IS_OK(bond))
4436 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
4438 bond_for_each_slave(bond, slave, i) {
4446 bond_for_each_slave_from(bond, slave, i, start_at) {
4447 if (IS_UP(slave->dev) &&
4448 (slave->link == BOND_LINK_UP) &&
4449 (slave->state == BOND_STATE_ACTIVE)) {
4450 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4457 /* no suitable interface, frame not sent */
4460 read_unlock(&bond->lock);
4461 return NETDEV_TX_OK;
4465 * in broadcast mode, we send everything to all usable interfaces.
4467 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4469 struct bonding *bond = netdev_priv(bond_dev);
4470 struct slave *slave, *start_at;
4471 struct net_device *tx_dev = NULL;
4475 read_lock(&bond->lock);
4477 if (!BOND_IS_OK(bond))
4480 read_lock(&bond->curr_slave_lock);
4481 start_at = bond->curr_active_slave;
4482 read_unlock(&bond->curr_slave_lock);
4487 bond_for_each_slave_from(bond, slave, i, start_at) {
4488 if (IS_UP(slave->dev) &&
4489 (slave->link == BOND_LINK_UP) &&
4490 (slave->state == BOND_STATE_ACTIVE)) {
4492 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4494 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
4499 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4501 dev_kfree_skb(skb2);
4505 tx_dev = slave->dev;
4510 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4514 /* no suitable interface, frame not sent */
4517 /* frame sent to all suitable interfaces */
4518 read_unlock(&bond->lock);
4519 return NETDEV_TX_OK;
4522 /*------------------------- Device initialization ---------------------------*/
4524 static void bond_set_xmit_hash_policy(struct bonding *bond)
4526 switch (bond->params.xmit_policy) {
4527 case BOND_XMIT_POLICY_LAYER23:
4528 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4530 case BOND_XMIT_POLICY_LAYER34:
4531 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4533 case BOND_XMIT_POLICY_LAYER2:
4535 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4541 * Lookup the slave that corresponds to a qid
4543 static inline int bond_slave_override(struct bonding *bond,
4544 struct sk_buff *skb)
4547 struct slave *slave = NULL;
4548 struct slave *check_slave;
4550 read_lock(&bond->lock);
4552 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4555 /* Find out if any slaves have the same mapping as this skb. */
4556 bond_for_each_slave(bond, check_slave, i) {
4557 if (check_slave->queue_id == skb->queue_mapping) {
4558 slave = check_slave;
4563 /* If the slave isn't UP, use default transmit policy. */
4564 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4565 (slave->link == BOND_LINK_UP)) {
4566 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4570 read_unlock(&bond->lock);
4574 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4577 * This helper function exists to help dev_pick_tx get the correct
4578 * destination queue. Using a helper function skips the a call to
4579 * skb_tx_hash and will put the skbs in the queue we expect on their
4580 * way down to the bonding driver.
4582 return skb->queue_mapping;
4585 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4587 struct bonding *bond = netdev_priv(dev);
4590 * If we risk deadlock from transmitting this in the
4591 * netpoll path, tell netpoll to queue the frame for later tx
4593 if (is_netpoll_tx_blocked(dev))
4594 return NETDEV_TX_BUSY;
4596 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4597 if (!bond_slave_override(bond, skb))
4598 return NETDEV_TX_OK;
4601 switch (bond->params.mode) {
4602 case BOND_MODE_ROUNDROBIN:
4603 return bond_xmit_roundrobin(skb, dev);
4604 case BOND_MODE_ACTIVEBACKUP:
4605 return bond_xmit_activebackup(skb, dev);
4607 return bond_xmit_xor(skb, dev);
4608 case BOND_MODE_BROADCAST:
4609 return bond_xmit_broadcast(skb, dev);
4610 case BOND_MODE_8023AD:
4611 return bond_3ad_xmit_xor(skb, dev);
4614 return bond_alb_xmit(skb, dev);
4616 /* Should never happen, mode already checked */
4617 pr_err("%s: Error: Unknown bonding mode %d\n",
4618 dev->name, bond->params.mode);
4621 return NETDEV_TX_OK;
4627 * set bond mode specific net device operations
4629 void bond_set_mode_ops(struct bonding *bond, int mode)
4631 struct net_device *bond_dev = bond->dev;
4634 case BOND_MODE_ROUNDROBIN:
4636 case BOND_MODE_ACTIVEBACKUP:
4639 bond_set_xmit_hash_policy(bond);
4641 case BOND_MODE_BROADCAST:
4643 case BOND_MODE_8023AD:
4644 bond_set_master_3ad_flags(bond);
4645 bond_set_xmit_hash_policy(bond);
4648 bond_set_master_alb_flags(bond);
4653 /* Should never happen, mode already checked */
4654 pr_err("%s: Error: Unknown bonding mode %d\n",
4655 bond_dev->name, mode);
4660 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4661 struct ethtool_drvinfo *drvinfo)
4663 strncpy(drvinfo->driver, DRV_NAME, 32);
4664 strncpy(drvinfo->version, DRV_VERSION, 32);
4665 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4668 static const struct ethtool_ops bond_ethtool_ops = {
4669 .get_drvinfo = bond_ethtool_get_drvinfo,
4670 .get_link = ethtool_op_get_link,
4671 .get_tx_csum = ethtool_op_get_tx_csum,
4672 .get_sg = ethtool_op_get_sg,
4673 .get_tso = ethtool_op_get_tso,
4674 .get_ufo = ethtool_op_get_ufo,
4675 .get_flags = ethtool_op_get_flags,
4678 static const struct net_device_ops bond_netdev_ops = {
4679 .ndo_init = bond_init,
4680 .ndo_uninit = bond_uninit,
4681 .ndo_open = bond_open,
4682 .ndo_stop = bond_close,
4683 .ndo_start_xmit = bond_start_xmit,
4684 .ndo_select_queue = bond_select_queue,
4685 .ndo_get_stats64 = bond_get_stats,
4686 .ndo_do_ioctl = bond_do_ioctl,
4687 .ndo_set_multicast_list = bond_set_multicast_list,
4688 .ndo_change_mtu = bond_change_mtu,
4689 .ndo_set_mac_address = bond_set_mac_address,
4690 .ndo_neigh_setup = bond_neigh_setup,
4691 .ndo_vlan_rx_register = bond_vlan_rx_register,
4692 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4693 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4694 #ifdef CONFIG_NET_POLL_CONTROLLER
4695 .ndo_netpoll_setup = bond_netpoll_setup,
4696 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4697 .ndo_poll_controller = bond_poll_controller,
4699 .ndo_add_slave = bond_enslave,
4700 .ndo_del_slave = bond_release,
4703 static void bond_destructor(struct net_device *bond_dev)
4705 struct bonding *bond = netdev_priv(bond_dev);
4707 destroy_workqueue(bond->wq);
4708 free_netdev(bond_dev);
4711 static void bond_setup(struct net_device *bond_dev)
4713 struct bonding *bond = netdev_priv(bond_dev);
4715 /* initialize rwlocks */
4716 rwlock_init(&bond->lock);
4717 rwlock_init(&bond->curr_slave_lock);
4719 bond->params = bonding_defaults;
4721 /* Initialize pointers */
4722 bond->dev = bond_dev;
4723 INIT_LIST_HEAD(&bond->vlan_list);
4725 /* Initialize the device entry points */
4726 ether_setup(bond_dev);
4727 bond_dev->netdev_ops = &bond_netdev_ops;
4728 bond_dev->ethtool_ops = &bond_ethtool_ops;
4729 bond_set_mode_ops(bond, bond->params.mode);
4731 bond_dev->destructor = bond_destructor;
4733 /* Initialize the device options */
4734 bond_dev->tx_queue_len = 0;
4735 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4736 bond_dev->priv_flags |= IFF_BONDING;
4737 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4739 if (bond->params.arp_interval)
4740 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
4742 /* At first, we block adding VLANs. That's the only way to
4743 * prevent problems that occur when adding VLANs over an
4744 * empty bond. The block will be removed once non-challenged
4745 * slaves are enslaved.
4747 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4749 /* don't acquire bond device's netif_tx_lock when
4751 bond_dev->features |= NETIF_F_LLTX;
4753 /* By default, we declare the bond to be fully
4754 * VLAN hardware accelerated capable. Special
4755 * care is taken in the various xmit functions
4756 * when there are slaves that are not hw accel
4759 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4760 NETIF_F_HW_VLAN_RX |
4761 NETIF_F_HW_VLAN_FILTER);
4763 /* By default, we enable GRO on bonding devices.
4764 * Actual support requires lowlevel drivers are GRO ready.
4766 bond_dev->features |= NETIF_F_GRO;
4769 static void bond_work_cancel_all(struct bonding *bond)
4771 write_lock_bh(&bond->lock);
4772 bond->kill_timers = 1;
4773 write_unlock_bh(&bond->lock);
4775 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4776 cancel_delayed_work(&bond->mii_work);
4778 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4779 cancel_delayed_work(&bond->arp_work);
4781 if (bond->params.mode == BOND_MODE_ALB &&
4782 delayed_work_pending(&bond->alb_work))
4783 cancel_delayed_work(&bond->alb_work);
4785 if (bond->params.mode == BOND_MODE_8023AD &&
4786 delayed_work_pending(&bond->ad_work))
4787 cancel_delayed_work(&bond->ad_work);
4789 if (delayed_work_pending(&bond->mcast_work))
4790 cancel_delayed_work(&bond->mcast_work);
4794 * Destroy a bonding device.
4795 * Must be under rtnl_lock when this function is called.
4797 static void bond_uninit(struct net_device *bond_dev)
4799 struct bonding *bond = netdev_priv(bond_dev);
4800 struct vlan_entry *vlan, *tmp;
4802 bond_netpoll_cleanup(bond_dev);
4804 /* Release the bonded slaves */
4805 bond_release_all(bond_dev);
4807 list_del(&bond->bond_list);
4809 bond_work_cancel_all(bond);
4811 bond_remove_proc_entry(bond);
4813 bond_debug_unregister(bond);
4815 __hw_addr_flush(&bond->mc_list);
4817 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4818 list_del(&vlan->vlan_list);
4823 /*------------------------- Module initialization ---------------------------*/
4826 * Convert string input module parms. Accept either the
4827 * number of the mode or its string name. A bit complicated because
4828 * some mode names are substrings of other names, and calls from sysfs
4829 * may have whitespace in the name (trailing newlines, for example).
4831 int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
4833 int modeint = -1, i, rv;
4834 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4836 for (p = (char *)buf; *p; p++)
4837 if (!(isdigit(*p) || isspace(*p)))
4841 rv = sscanf(buf, "%20s", modestr);
4843 rv = sscanf(buf, "%d", &modeint);
4848 for (i = 0; tbl[i].modename; i++) {
4849 if (modeint == tbl[i].mode)
4851 if (strcmp(modestr, tbl[i].modename) == 0)
4858 static int bond_check_params(struct bond_params *params)
4860 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
4863 * Convert string parameters.
4866 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4867 if (bond_mode == -1) {
4868 pr_err("Error: Invalid bonding mode \"%s\"\n",
4869 mode == NULL ? "NULL" : mode);
4874 if (xmit_hash_policy) {
4875 if ((bond_mode != BOND_MODE_XOR) &&
4876 (bond_mode != BOND_MODE_8023AD)) {
4877 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4878 bond_mode_name(bond_mode));
4880 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4882 if (xmit_hashtype == -1) {
4883 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4884 xmit_hash_policy == NULL ? "NULL" :
4892 if (bond_mode != BOND_MODE_8023AD) {
4893 pr_info("lacp_rate param is irrelevant in mode %s\n",
4894 bond_mode_name(bond_mode));
4896 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4897 if (lacp_fast == -1) {
4898 pr_err("Error: Invalid lacp rate \"%s\"\n",
4899 lacp_rate == NULL ? "NULL" : lacp_rate);
4906 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4907 if (params->ad_select == -1) {
4908 pr_err("Error: Invalid ad_select \"%s\"\n",
4909 ad_select == NULL ? "NULL" : ad_select);
4913 if (bond_mode != BOND_MODE_8023AD) {
4914 pr_warning("ad_select param only affects 802.3ad mode\n");
4917 params->ad_select = BOND_AD_STABLE;
4920 if (max_bonds < 0) {
4921 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4922 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4923 max_bonds = BOND_DEFAULT_MAX_BONDS;
4927 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4928 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4929 miimon = BOND_LINK_MON_INTERV;
4933 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4938 if (downdelay < 0) {
4939 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4940 downdelay, INT_MAX);
4944 if ((use_carrier != 0) && (use_carrier != 1)) {
4945 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4950 if (num_grat_arp < 0 || num_grat_arp > 255) {
4951 pr_warning("Warning: num_grat_arp (%d) not in range 0-255 so it was reset to 1\n",
4956 if (num_unsol_na < 0 || num_unsol_na > 255) {
4957 pr_warning("Warning: num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4962 /* reset values for 802.3ad */
4963 if (bond_mode == BOND_MODE_8023AD) {
4965 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");
4966 pr_warning("Forcing miimon to 100msec\n");
4971 if (tx_queues < 1 || tx_queues > 255) {
4972 pr_warning("Warning: tx_queues (%d) should be between "
4973 "1 and 255, resetting to %d\n",
4974 tx_queues, BOND_DEFAULT_TX_QUEUES);
4975 tx_queues = BOND_DEFAULT_TX_QUEUES;
4978 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4979 pr_warning("Warning: all_slaves_active module parameter (%d), "
4980 "not of valid value (0/1), so it was set to "
4981 "0\n", all_slaves_active);
4982 all_slaves_active = 0;
4985 if (resend_igmp < 0 || resend_igmp > 255) {
4986 pr_warning("Warning: resend_igmp (%d) should be between "
4987 "0 and 255, resetting to %d\n",
4988 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4989 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4992 /* reset values for TLB/ALB */
4993 if ((bond_mode == BOND_MODE_TLB) ||
4994 (bond_mode == BOND_MODE_ALB)) {
4996 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");
4997 pr_warning("Forcing miimon to 100msec\n");
5002 if (bond_mode == BOND_MODE_ALB) {
5003 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",
5008 if (updelay || downdelay) {
5009 /* just warn the user the up/down delay will have
5010 * no effect since miimon is zero...
5012 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",
5013 updelay, downdelay);
5016 /* don't allow arp monitoring */
5018 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5019 miimon, arp_interval);
5023 if ((updelay % miimon) != 0) {
5024 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5026 (updelay / miimon) * miimon);
5031 if ((downdelay % miimon) != 0) {
5032 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5034 (downdelay / miimon) * miimon);
5037 downdelay /= miimon;
5040 if (arp_interval < 0) {
5041 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5042 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
5043 arp_interval = BOND_LINK_ARP_INTERV;
5046 for (arp_ip_count = 0;
5047 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5049 /* not complete check, but should be good enough to
5051 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
5052 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5053 arp_ip_target[arp_ip_count]);
5056 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
5057 arp_target[arp_ip_count] = ip;
5061 if (arp_interval && !arp_ip_count) {
5062 /* don't allow arping if no arp_ip_target given... */
5063 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5069 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
5070 pr_err("arp_validate only supported in active-backup mode\n");
5073 if (!arp_interval) {
5074 pr_err("arp_validate requires arp_interval\n");
5078 arp_validate_value = bond_parse_parm(arp_validate,
5080 if (arp_validate_value == -1) {
5081 pr_err("Error: invalid arp_validate \"%s\"\n",
5082 arp_validate == NULL ? "NULL" : arp_validate);
5086 arp_validate_value = 0;
5089 pr_info("MII link monitoring set to %d ms\n", miimon);
5090 } else if (arp_interval) {
5093 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5095 arp_validate_tbl[arp_validate_value].modename,
5098 for (i = 0; i < arp_ip_count; i++)
5099 pr_info(" %s", arp_ip_target[i]);
5103 } else if (max_bonds) {
5104 /* miimon and arp_interval not set, we need one so things
5105 * work as expected, see bonding.txt for details
5107 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");
5110 if (primary && !USES_PRIMARY(bond_mode)) {
5111 /* currently, using a primary only makes sense
5112 * in active backup, TLB or ALB modes
5114 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5115 primary, bond_mode_name(bond_mode));
5119 if (primary && primary_reselect) {
5120 primary_reselect_value = bond_parse_parm(primary_reselect,
5122 if (primary_reselect_value == -1) {
5123 pr_err("Error: Invalid primary_reselect \"%s\"\n",
5125 NULL ? "NULL" : primary_reselect);
5129 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5132 if (fail_over_mac) {
5133 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5135 if (fail_over_mac_value == -1) {
5136 pr_err("Error: invalid fail_over_mac \"%s\"\n",
5137 arp_validate == NULL ? "NULL" : arp_validate);
5141 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5142 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
5144 fail_over_mac_value = BOND_FOM_NONE;
5147 /* fill params struct with the proper values */
5148 params->mode = bond_mode;
5149 params->xmit_policy = xmit_hashtype;
5150 params->miimon = miimon;
5151 params->num_grat_arp = num_grat_arp;
5152 params->num_unsol_na = num_unsol_na;
5153 params->arp_interval = arp_interval;
5154 params->arp_validate = arp_validate_value;
5155 params->updelay = updelay;
5156 params->downdelay = downdelay;
5157 params->use_carrier = use_carrier;
5158 params->lacp_fast = lacp_fast;
5159 params->primary[0] = 0;
5160 params->primary_reselect = primary_reselect_value;
5161 params->fail_over_mac = fail_over_mac_value;
5162 params->tx_queues = tx_queues;
5163 params->all_slaves_active = all_slaves_active;
5164 params->resend_igmp = resend_igmp;
5167 strncpy(params->primary, primary, IFNAMSIZ);
5168 params->primary[IFNAMSIZ - 1] = 0;
5171 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5176 static struct lock_class_key bonding_netdev_xmit_lock_key;
5177 static struct lock_class_key bonding_netdev_addr_lock_key;
5179 static void bond_set_lockdep_class_one(struct net_device *dev,
5180 struct netdev_queue *txq,
5183 lockdep_set_class(&txq->_xmit_lock,
5184 &bonding_netdev_xmit_lock_key);
5187 static void bond_set_lockdep_class(struct net_device *dev)
5189 lockdep_set_class(&dev->addr_list_lock,
5190 &bonding_netdev_addr_lock_key);
5191 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
5195 * Called from registration process
5197 static int bond_init(struct net_device *bond_dev)
5199 struct bonding *bond = netdev_priv(bond_dev);
5200 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
5202 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5204 bond->wq = create_singlethread_workqueue(bond_dev->name);
5208 bond_set_lockdep_class(bond_dev);
5210 netif_carrier_off(bond_dev);
5212 bond_create_proc_entry(bond);
5213 list_add_tail(&bond->bond_list, &bn->dev_list);
5215 bond_prepare_sysfs_group(bond);
5217 bond_debug_register(bond);
5219 __hw_addr_init(&bond->mc_list);
5223 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5225 if (tb[IFLA_ADDRESS]) {
5226 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5228 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5229 return -EADDRNOTAVAIL;
5234 static struct rtnl_link_ops bond_link_ops __read_mostly = {
5236 .priv_size = sizeof(struct bonding),
5237 .setup = bond_setup,
5238 .validate = bond_validate,
5241 /* Create a new bond based on the specified name and bonding parameters.
5242 * If name is NULL, obtain a suitable "bond%d" name for us.
5243 * Caller must NOT hold rtnl_lock; we need to release it here before we
5244 * set up our sysfs entries.
5246 int bond_create(struct net *net, const char *name)
5248 struct net_device *bond_dev;
5253 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5254 bond_setup, tx_queues);
5256 pr_err("%s: eek! can't alloc netdev!\n", name);
5261 dev_net_set(bond_dev, net);
5262 bond_dev->rtnl_link_ops = &bond_link_ops;
5265 res = dev_alloc_name(bond_dev, "bond%d");
5270 * If we're given a name to register
5271 * we need to ensure that its not already
5275 if (__dev_get_by_name(net, name) != NULL)
5279 res = register_netdevice(bond_dev);
5284 bond_destructor(bond_dev);
5288 static int __net_init bond_net_init(struct net *net)
5290 struct bond_net *bn = net_generic(net, bond_net_id);
5293 INIT_LIST_HEAD(&bn->dev_list);
5295 bond_create_proc_dir(bn);
5300 static void __net_exit bond_net_exit(struct net *net)
5302 struct bond_net *bn = net_generic(net, bond_net_id);
5304 bond_destroy_proc_dir(bn);
5307 static struct pernet_operations bond_net_ops = {
5308 .init = bond_net_init,
5309 .exit = bond_net_exit,
5311 .size = sizeof(struct bond_net),
5314 static int __init bonding_init(void)
5319 pr_info("%s", version);
5321 res = bond_check_params(&bonding_defaults);
5325 res = register_pernet_subsys(&bond_net_ops);
5329 res = rtnl_link_register(&bond_link_ops);
5333 bond_create_debugfs();
5335 for (i = 0; i < max_bonds; i++) {
5336 res = bond_create(&init_net, NULL);
5341 res = bond_create_sysfs();
5345 register_netdevice_notifier(&bond_netdev_notifier);
5346 register_inetaddr_notifier(&bond_inetaddr_notifier);
5347 bond_register_ipv6_notifier();
5351 rtnl_link_unregister(&bond_link_ops);
5353 unregister_pernet_subsys(&bond_net_ops);
5358 static void __exit bonding_exit(void)
5360 unregister_netdevice_notifier(&bond_netdev_notifier);
5361 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
5362 bond_unregister_ipv6_notifier();
5364 bond_destroy_sysfs();
5365 bond_destroy_debugfs();
5367 rtnl_link_unregister(&bond_link_ops);
5368 unregister_pernet_subsys(&bond_net_ops);
5370 #ifdef CONFIG_NET_POLL_CONTROLLER
5372 * Make sure we don't have an imbalance on our netpoll blocking
5374 WARN_ON(atomic_read(&netpoll_block_tx));
5378 module_init(bonding_init);
5379 module_exit(bonding_exit);
5380 MODULE_LICENSE("GPL");
5381 MODULE_VERSION(DRV_VERSION);
5382 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5383 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5384 MODULE_ALIAS_RTNL_LINK("bond");