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 BONDING_DEBUG 1
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>
56 #include <asm/system.h>
59 #include <asm/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 <net/route.h>
79 #include <net/net_namespace.h>
84 /*---------------------------- Module parameters ----------------------------*/
86 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
87 #define BOND_LINK_MON_INTERV 0
88 #define BOND_LINK_ARP_INTERV 0
90 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
91 static int miimon = BOND_LINK_MON_INTERV;
92 static int updelay = 0;
93 static int downdelay = 0;
94 static int use_carrier = 1;
95 static char *mode = NULL;
96 static char *primary = NULL;
97 static char *lacp_rate = NULL;
98 static char *xmit_hash_policy = NULL;
99 static int arp_interval = BOND_LINK_ARP_INTERV;
100 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
101 static char *arp_validate = NULL;
102 static int fail_over_mac = 0;
103 struct bond_params bonding_defaults;
105 module_param(max_bonds, int, 0);
106 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
107 module_param(miimon, int, 0);
108 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
109 module_param(updelay, int, 0);
110 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
111 module_param(downdelay, int, 0);
112 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
114 module_param(use_carrier, int, 0);
115 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
116 "0 for off, 1 for on (default)");
117 module_param(mode, charp, 0);
118 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
119 "1 for active-backup, 2 for balance-xor, "
120 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
121 "6 for balance-alb");
122 module_param(primary, charp, 0);
123 MODULE_PARM_DESC(primary, "Primary network device to use");
124 module_param(lacp_rate, charp, 0);
125 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
127 module_param(xmit_hash_policy, charp, 0);
128 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
129 ", 1 for layer 3+4");
130 module_param(arp_interval, int, 0);
131 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
132 module_param_array(arp_ip_target, charp, NULL, 0);
133 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
134 module_param(arp_validate, charp, 0);
135 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
136 module_param(fail_over_mac, int, 0);
137 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on.");
139 /*----------------------------- Global variables ----------------------------*/
141 static const char * const version =
142 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
144 LIST_HEAD(bond_dev_list);
146 #ifdef CONFIG_PROC_FS
147 static struct proc_dir_entry *bond_proc_dir = NULL;
150 extern struct rw_semaphore bonding_rwsem;
151 static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
152 static int arp_ip_count = 0;
153 static int bond_mode = BOND_MODE_ROUNDROBIN;
154 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
155 static int lacp_fast = 0;
158 struct bond_parm_tbl bond_lacp_tbl[] = {
159 { "slow", AD_LACP_SLOW},
160 { "fast", AD_LACP_FAST},
164 struct bond_parm_tbl bond_mode_tbl[] = {
165 { "balance-rr", BOND_MODE_ROUNDROBIN},
166 { "active-backup", BOND_MODE_ACTIVEBACKUP},
167 { "balance-xor", BOND_MODE_XOR},
168 { "broadcast", BOND_MODE_BROADCAST},
169 { "802.3ad", BOND_MODE_8023AD},
170 { "balance-tlb", BOND_MODE_TLB},
171 { "balance-alb", BOND_MODE_ALB},
175 struct bond_parm_tbl xmit_hashtype_tbl[] = {
176 { "layer2", BOND_XMIT_POLICY_LAYER2},
177 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
178 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
182 struct bond_parm_tbl arp_validate_tbl[] = {
183 { "none", BOND_ARP_VALIDATE_NONE},
184 { "active", BOND_ARP_VALIDATE_ACTIVE},
185 { "backup", BOND_ARP_VALIDATE_BACKUP},
186 { "all", BOND_ARP_VALIDATE_ALL},
190 /*-------------------------- Forward declarations ---------------------------*/
192 static void bond_send_gratuitous_arp(struct bonding *bond);
193 static void bond_deinit(struct net_device *bond_dev);
195 /*---------------------------- General routines -----------------------------*/
197 static const char *bond_mode_name(int mode)
200 case BOND_MODE_ROUNDROBIN :
201 return "load balancing (round-robin)";
202 case BOND_MODE_ACTIVEBACKUP :
203 return "fault-tolerance (active-backup)";
205 return "load balancing (xor)";
206 case BOND_MODE_BROADCAST :
207 return "fault-tolerance (broadcast)";
208 case BOND_MODE_8023AD:
209 return "IEEE 802.3ad Dynamic link aggregation";
211 return "transmit load balancing";
213 return "adaptive load balancing";
219 /*---------------------------------- VLAN -----------------------------------*/
222 * bond_add_vlan - add a new vlan id on bond
223 * @bond: bond that got the notification
224 * @vlan_id: the vlan id to add
226 * Returns -ENOMEM if allocation failed.
228 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
230 struct vlan_entry *vlan;
232 dprintk("bond: %s, vlan id %d\n",
233 (bond ? bond->dev->name: "None"), vlan_id);
235 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
240 INIT_LIST_HEAD(&vlan->vlan_list);
241 vlan->vlan_id = vlan_id;
244 write_lock_bh(&bond->lock);
246 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
248 write_unlock_bh(&bond->lock);
250 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
256 * bond_del_vlan - delete a vlan id from bond
257 * @bond: bond that got the notification
258 * @vlan_id: the vlan id to delete
260 * returns -ENODEV if @vlan_id was not found in @bond.
262 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
264 struct vlan_entry *vlan;
267 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
269 write_lock_bh(&bond->lock);
271 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
272 if (vlan->vlan_id == vlan_id) {
273 list_del(&vlan->vlan_list);
275 if ((bond->params.mode == BOND_MODE_TLB) ||
276 (bond->params.mode == BOND_MODE_ALB)) {
277 bond_alb_clear_vlan(bond, vlan_id);
280 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
285 if (list_empty(&bond->vlan_list) &&
286 (bond->slave_cnt == 0)) {
287 /* Last VLAN removed and no slaves, so
288 * restore block on adding VLANs. This will
289 * be removed once new slaves that are not
290 * VLAN challenged will be added.
292 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
300 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
304 write_unlock_bh(&bond->lock);
309 * bond_has_challenged_slaves
310 * @bond: the bond we're working on
312 * Searches the slave list. Returns 1 if a vlan challenged slave
313 * was found, 0 otherwise.
315 * Assumes bond->lock is held.
317 static int bond_has_challenged_slaves(struct bonding *bond)
322 bond_for_each_slave(bond, slave, i) {
323 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
324 dprintk("found VLAN challenged slave - %s\n",
330 dprintk("no VLAN challenged slaves found\n");
335 * bond_next_vlan - safely skip to the next item in the vlans list.
336 * @bond: the bond we're working on
337 * @curr: item we're advancing from
339 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
340 * or @curr->next otherwise (even if it is @curr itself again).
342 * Caller must hold bond->lock
344 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
346 struct vlan_entry *next, *last;
348 if (list_empty(&bond->vlan_list)) {
353 next = list_entry(bond->vlan_list.next,
354 struct vlan_entry, vlan_list);
356 last = list_entry(bond->vlan_list.prev,
357 struct vlan_entry, vlan_list);
359 next = list_entry(bond->vlan_list.next,
360 struct vlan_entry, vlan_list);
362 next = list_entry(curr->vlan_list.next,
363 struct vlan_entry, vlan_list);
371 * bond_dev_queue_xmit - Prepare skb for xmit.
373 * @bond: bond device that got this skb for tx.
374 * @skb: hw accel VLAN tagged skb to transmit
375 * @slave_dev: slave that is supposed to xmit this skbuff
377 * When the bond gets an skb to transmit that is
378 * already hardware accelerated VLAN tagged, and it
379 * needs to relay this skb to a slave that is not
380 * hw accel capable, the skb needs to be "unaccelerated",
381 * i.e. strip the hwaccel tag and re-insert it as part
384 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
386 unsigned short uninitialized_var(vlan_id);
388 if (!list_empty(&bond->vlan_list) &&
389 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
390 vlan_get_tag(skb, &vlan_id) == 0) {
391 skb->dev = slave_dev;
392 skb = vlan_put_tag(skb, vlan_id);
394 /* vlan_put_tag() frees the skb in case of error,
395 * so return success here so the calling functions
396 * won't attempt to free is again.
401 skb->dev = slave_dev;
411 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
412 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
414 * a. This operation is performed in IOCTL context,
415 * b. The operation is protected by the RTNL semaphore in the 8021q code,
416 * c. Holding a lock with BH disabled while directly calling a base driver
417 * entry point is generally a BAD idea.
419 * The design of synchronization/protection for this operation in the 8021q
420 * module is good for one or more VLAN devices over a single physical device
421 * and cannot be extended for a teaming solution like bonding, so there is a
422 * potential race condition here where a net device from the vlan group might
423 * be referenced (either by a base driver or the 8021q code) while it is being
424 * removed from the system. However, it turns out we're not making matters
425 * worse, and if it works for regular VLAN usage it will work here too.
429 * bond_vlan_rx_register - Propagates registration to slaves
430 * @bond_dev: bonding net device that got called
431 * @grp: vlan group being registered
433 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
435 struct bonding *bond = bond_dev->priv;
441 bond_for_each_slave(bond, slave, i) {
442 struct net_device *slave_dev = slave->dev;
444 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
445 slave_dev->vlan_rx_register) {
446 slave_dev->vlan_rx_register(slave_dev, grp);
452 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
453 * @bond_dev: bonding net device that got called
454 * @vid: vlan id being added
456 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
458 struct bonding *bond = bond_dev->priv;
462 bond_for_each_slave(bond, slave, i) {
463 struct net_device *slave_dev = slave->dev;
465 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
466 slave_dev->vlan_rx_add_vid) {
467 slave_dev->vlan_rx_add_vid(slave_dev, vid);
471 res = bond_add_vlan(bond, vid);
473 printk(KERN_ERR DRV_NAME
474 ": %s: Error: Failed to add vlan id %d\n",
475 bond_dev->name, vid);
480 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
481 * @bond_dev: bonding net device that got called
482 * @vid: vlan id being removed
484 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
486 struct bonding *bond = bond_dev->priv;
488 struct net_device *vlan_dev;
491 bond_for_each_slave(bond, slave, i) {
492 struct net_device *slave_dev = slave->dev;
494 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
495 slave_dev->vlan_rx_kill_vid) {
496 /* Save and then restore vlan_dev in the grp array,
497 * since the slave's driver might clear it.
499 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
500 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
501 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
505 res = bond_del_vlan(bond, vid);
507 printk(KERN_ERR DRV_NAME
508 ": %s: Error: Failed to remove vlan id %d\n",
509 bond_dev->name, vid);
513 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
515 struct vlan_entry *vlan;
517 write_lock_bh(&bond->lock);
519 if (list_empty(&bond->vlan_list)) {
523 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
524 slave_dev->vlan_rx_register) {
525 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
528 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
529 !(slave_dev->vlan_rx_add_vid)) {
533 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
534 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
538 write_unlock_bh(&bond->lock);
541 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
543 struct vlan_entry *vlan;
544 struct net_device *vlan_dev;
546 write_lock_bh(&bond->lock);
548 if (list_empty(&bond->vlan_list)) {
552 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
553 !(slave_dev->vlan_rx_kill_vid)) {
557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
558 /* Save and then restore vlan_dev in the grp array,
559 * since the slave's driver might clear it.
561 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
562 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
563 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
567 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
568 slave_dev->vlan_rx_register) {
569 slave_dev->vlan_rx_register(slave_dev, NULL);
573 write_unlock_bh(&bond->lock);
576 /*------------------------------- Link status -------------------------------*/
579 * Set the carrier state for the master according to the state of its
580 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
581 * do special 802.3ad magic.
583 * Returns zero if carrier state does not change, nonzero if it does.
585 static int bond_set_carrier(struct bonding *bond)
590 if (bond->slave_cnt == 0)
593 if (bond->params.mode == BOND_MODE_8023AD)
594 return bond_3ad_set_carrier(bond);
596 bond_for_each_slave(bond, slave, i) {
597 if (slave->link == BOND_LINK_UP) {
598 if (!netif_carrier_ok(bond->dev)) {
599 netif_carrier_on(bond->dev);
607 if (netif_carrier_ok(bond->dev)) {
608 netif_carrier_off(bond->dev);
615 * Get link speed and duplex from the slave's base driver
616 * using ethtool. If for some reason the call fails or the
617 * values are invalid, fake speed and duplex to 100/Full
620 static int bond_update_speed_duplex(struct slave *slave)
622 struct net_device *slave_dev = slave->dev;
623 struct ethtool_cmd etool;
626 /* Fake speed and duplex */
627 slave->speed = SPEED_100;
628 slave->duplex = DUPLEX_FULL;
630 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
633 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
637 switch (etool.speed) {
647 switch (etool.duplex) {
655 slave->speed = etool.speed;
656 slave->duplex = etool.duplex;
662 * if <dev> supports MII link status reporting, check its link status.
664 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
665 * depening upon the setting of the use_carrier parameter.
667 * Return either BMSR_LSTATUS, meaning that the link is up (or we
668 * can't tell and just pretend it is), or 0, meaning that the link is
671 * If reporting is non-zero, instead of faking link up, return -1 if
672 * both ETHTOOL and MII ioctls fail (meaning the device does not
673 * support them). If use_carrier is set, return whatever it says.
674 * It'd be nice if there was a good way to tell if a driver supports
675 * netif_carrier, but there really isn't.
677 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
679 static int (* ioctl)(struct net_device *, struct ifreq *, int);
681 struct mii_ioctl_data *mii;
683 if (bond->params.use_carrier) {
684 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
687 ioctl = slave_dev->do_ioctl;
689 /* TODO: set pointer to correct ioctl on a per team member */
690 /* bases to make this more efficient. that is, once */
691 /* we determine the correct ioctl, we will always */
692 /* call it and not the others for that team */
696 * We cannot assume that SIOCGMIIPHY will also read a
697 * register; not all network drivers (e.g., e100)
701 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
702 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
704 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
705 mii->reg_num = MII_BMSR;
706 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
707 return (mii->val_out & BMSR_LSTATUS);
713 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
714 * attempt to get link status from it if the above MII ioctls fail.
716 if (slave_dev->ethtool_ops) {
717 if (slave_dev->ethtool_ops->get_link) {
720 link = slave_dev->ethtool_ops->get_link(slave_dev);
722 return link ? BMSR_LSTATUS : 0;
727 * If reporting, report that either there's no dev->do_ioctl,
728 * or both SIOCGMIIREG and get_link failed (meaning that we
729 * cannot report link status). If not reporting, pretend
732 return (reporting ? -1 : BMSR_LSTATUS);
735 /*----------------------------- Multicast list ------------------------------*/
738 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
740 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
742 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
743 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
747 * returns dmi entry if found, NULL otherwise
749 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
751 struct dev_mc_list *idmi;
753 for (idmi = mc_list; idmi; idmi = idmi->next) {
754 if (bond_is_dmi_same(dmi, idmi)) {
763 * Push the promiscuity flag down to appropriate slaves
765 static void bond_set_promiscuity(struct bonding *bond, int inc)
767 if (USES_PRIMARY(bond->params.mode)) {
768 /* write lock already acquired */
769 if (bond->curr_active_slave) {
770 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
775 bond_for_each_slave(bond, slave, i) {
776 dev_set_promiscuity(slave->dev, inc);
782 * Push the allmulti flag down to all slaves
784 static void bond_set_allmulti(struct bonding *bond, int inc)
786 if (USES_PRIMARY(bond->params.mode)) {
787 /* write lock already acquired */
788 if (bond->curr_active_slave) {
789 dev_set_allmulti(bond->curr_active_slave->dev, inc);
794 bond_for_each_slave(bond, slave, i) {
795 dev_set_allmulti(slave->dev, inc);
801 * Add a Multicast address to slaves
804 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
806 if (USES_PRIMARY(bond->params.mode)) {
807 /* write lock already acquired */
808 if (bond->curr_active_slave) {
809 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
814 bond_for_each_slave(bond, slave, i) {
815 dev_mc_add(slave->dev, addr, alen, 0);
821 * Remove a multicast address from slave
824 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
826 if (USES_PRIMARY(bond->params.mode)) {
827 /* write lock already acquired */
828 if (bond->curr_active_slave) {
829 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
834 bond_for_each_slave(bond, slave, i) {
835 dev_mc_delete(slave->dev, addr, alen, 0);
842 * Retrieve the list of registered multicast addresses for the bonding
843 * device and retransmit an IGMP JOIN request to the current active
846 static void bond_resend_igmp_join_requests(struct bonding *bond)
848 struct in_device *in_dev;
849 struct ip_mc_list *im;
852 in_dev = __in_dev_get_rcu(bond->dev);
854 for (im = in_dev->mc_list; im; im = im->next) {
855 ip_mc_rejoin_group(im);
863 * Totally destroys the mc_list in bond
865 static void bond_mc_list_destroy(struct bonding *bond)
867 struct dev_mc_list *dmi;
871 bond->mc_list = dmi->next;
875 bond->mc_list = NULL;
879 * Copy all the Multicast addresses from src to the bonding device dst
881 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
884 struct dev_mc_list *dmi, *new_dmi;
886 for (dmi = mc_list; dmi; dmi = dmi->next) {
887 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
890 /* FIXME: Potential memory leak !!! */
894 new_dmi->next = bond->mc_list;
895 bond->mc_list = new_dmi;
896 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
897 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
898 new_dmi->dmi_users = dmi->dmi_users;
899 new_dmi->dmi_gusers = dmi->dmi_gusers;
906 * flush all members of flush->mc_list from device dev->mc_list
908 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
910 struct bonding *bond = bond_dev->priv;
911 struct dev_mc_list *dmi;
913 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
914 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
917 if (bond->params.mode == BOND_MODE_8023AD) {
918 /* del lacpdu mc addr from mc list */
919 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
921 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
925 /*--------------------------- Active slave change ---------------------------*/
928 * Update the mc list and multicast-related flags for the new and
929 * old active slaves (if any) according to the multicast mode, and
930 * promiscuous flags unconditionally.
932 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
934 struct dev_mc_list *dmi;
936 if (!USES_PRIMARY(bond->params.mode)) {
937 /* nothing to do - mc list is already up-to-date on
944 if (bond->dev->flags & IFF_PROMISC) {
945 dev_set_promiscuity(old_active->dev, -1);
948 if (bond->dev->flags & IFF_ALLMULTI) {
949 dev_set_allmulti(old_active->dev, -1);
952 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
953 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
958 if (bond->dev->flags & IFF_PROMISC) {
959 dev_set_promiscuity(new_active->dev, 1);
962 if (bond->dev->flags & IFF_ALLMULTI) {
963 dev_set_allmulti(new_active->dev, 1);
966 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
967 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
969 bond_resend_igmp_join_requests(bond);
974 * find_best_interface - select the best available slave to be the active one
975 * @bond: our bonding struct
977 * Warning: Caller must hold curr_slave_lock for writing.
979 static struct slave *bond_find_best_slave(struct bonding *bond)
981 struct slave *new_active, *old_active;
982 struct slave *bestslave = NULL;
983 int mintime = bond->params.updelay;
986 new_active = old_active = bond->curr_active_slave;
988 if (!new_active) { /* there were no active slaves left */
989 if (bond->slave_cnt > 0) { /* found one slave */
990 new_active = bond->first_slave;
992 return NULL; /* still no slave, return NULL */
996 /* first try the primary link; if arping, a link must tx/rx traffic
997 * before it can be considered the curr_active_slave - also, we would skip
998 * slaves between the curr_active_slave and primary_slave that may be up
1001 if ((bond->primary_slave) &&
1002 (!bond->params.arp_interval) &&
1003 (IS_UP(bond->primary_slave->dev))) {
1004 new_active = bond->primary_slave;
1007 /* remember where to stop iterating over the slaves */
1008 old_active = new_active;
1010 bond_for_each_slave_from(bond, new_active, i, old_active) {
1011 if (IS_UP(new_active->dev)) {
1012 if (new_active->link == BOND_LINK_UP) {
1014 } else if (new_active->link == BOND_LINK_BACK) {
1015 /* link up, but waiting for stabilization */
1016 if (new_active->delay < mintime) {
1017 mintime = new_active->delay;
1018 bestslave = new_active;
1028 * change_active_interface - change the active slave into the specified one
1029 * @bond: our bonding struct
1030 * @new: the new slave to make the active one
1032 * Set the new slave to the bond's settings and unset them on the old
1033 * curr_active_slave.
1034 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1036 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1037 * because it is apparently the best available slave we have, even though its
1038 * updelay hasn't timed out yet.
1040 * Warning: Caller must hold curr_slave_lock for writing.
1042 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1044 struct slave *old_active = bond->curr_active_slave;
1046 if (old_active == new_active) {
1051 if (new_active->link == BOND_LINK_BACK) {
1052 if (USES_PRIMARY(bond->params.mode)) {
1053 printk(KERN_INFO DRV_NAME
1054 ": %s: making interface %s the new "
1055 "active one %d ms earlier.\n",
1056 bond->dev->name, new_active->dev->name,
1057 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1060 new_active->delay = 0;
1061 new_active->link = BOND_LINK_UP;
1062 new_active->jiffies = jiffies;
1064 if (bond->params.mode == BOND_MODE_8023AD) {
1065 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1068 if ((bond->params.mode == BOND_MODE_TLB) ||
1069 (bond->params.mode == BOND_MODE_ALB)) {
1070 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1073 if (USES_PRIMARY(bond->params.mode)) {
1074 printk(KERN_INFO DRV_NAME
1075 ": %s: making interface %s the new "
1077 bond->dev->name, new_active->dev->name);
1082 if (USES_PRIMARY(bond->params.mode)) {
1083 bond_mc_swap(bond, new_active, old_active);
1086 if ((bond->params.mode == BOND_MODE_TLB) ||
1087 (bond->params.mode == BOND_MODE_ALB)) {
1088 bond_alb_handle_active_change(bond, new_active);
1090 bond_set_slave_inactive_flags(old_active);
1092 bond_set_slave_active_flags(new_active);
1094 bond->curr_active_slave = new_active;
1097 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1099 bond_set_slave_inactive_flags(old_active);
1103 bond_set_slave_active_flags(new_active);
1106 /* when bonding does not set the slave MAC address, the bond MAC
1107 * address is the one of the active slave.
1109 if (new_active && bond->params.fail_over_mac)
1110 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1111 new_active->dev->addr_len);
1112 if (bond->curr_active_slave &&
1113 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1114 &bond->curr_active_slave->dev->state)) {
1115 dprintk("delaying gratuitous arp on %s\n",
1116 bond->curr_active_slave->dev->name);
1117 bond->send_grat_arp = 1;
1119 bond_send_gratuitous_arp(bond);
1124 * bond_select_active_slave - select a new active slave, if needed
1125 * @bond: our bonding struct
1127 * This functions shoud be called when one of the following occurs:
1128 * - The old curr_active_slave has been released or lost its link.
1129 * - The primary_slave has got its link back.
1130 * - A slave has got its link back and there's no old curr_active_slave.
1132 * Warning: Caller must hold curr_slave_lock for writing.
1134 void bond_select_active_slave(struct bonding *bond)
1136 struct slave *best_slave;
1139 best_slave = bond_find_best_slave(bond);
1140 if (best_slave != bond->curr_active_slave) {
1141 bond_change_active_slave(bond, best_slave);
1142 rv = bond_set_carrier(bond);
1146 if (netif_carrier_ok(bond->dev)) {
1147 printk(KERN_INFO DRV_NAME
1148 ": %s: first active interface up!\n",
1151 printk(KERN_INFO DRV_NAME ": %s: "
1152 "now running without any active interface !\n",
1158 /*--------------------------- slave list handling ---------------------------*/
1161 * This function attaches the slave to the end of list.
1163 * bond->lock held for writing by caller.
1165 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1167 if (bond->first_slave == NULL) { /* attaching the first slave */
1168 new_slave->next = new_slave;
1169 new_slave->prev = new_slave;
1170 bond->first_slave = new_slave;
1172 new_slave->next = bond->first_slave;
1173 new_slave->prev = bond->first_slave->prev;
1174 new_slave->next->prev = new_slave;
1175 new_slave->prev->next = new_slave;
1182 * This function detaches the slave from the list.
1183 * WARNING: no check is made to verify if the slave effectively
1184 * belongs to <bond>.
1185 * Nothing is freed on return, structures are just unchained.
1186 * If any slave pointer in bond was pointing to <slave>,
1187 * it should be changed by the calling function.
1189 * bond->lock held for writing by caller.
1191 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1194 slave->next->prev = slave->prev;
1198 slave->prev->next = slave->next;
1201 if (bond->first_slave == slave) { /* slave is the first slave */
1202 if (bond->slave_cnt > 1) { /* there are more slave */
1203 bond->first_slave = slave->next;
1205 bond->first_slave = NULL; /* slave was the last one */
1214 /*---------------------------------- IOCTL ----------------------------------*/
1216 static int bond_sethwaddr(struct net_device *bond_dev,
1217 struct net_device *slave_dev)
1219 dprintk("bond_dev=%p\n", bond_dev);
1220 dprintk("slave_dev=%p\n", slave_dev);
1221 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1222 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1226 #define BOND_VLAN_FEATURES \
1227 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1228 NETIF_F_HW_VLAN_FILTER)
1231 * Compute the common dev->feature set available to all slaves. Some
1232 * feature bits are managed elsewhere, so preserve those feature bits
1233 * on the master device.
1235 static int bond_compute_features(struct bonding *bond)
1237 struct slave *slave;
1238 struct net_device *bond_dev = bond->dev;
1239 unsigned long features = bond_dev->features;
1240 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1241 bond_dev->hard_header_len);
1244 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1245 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1246 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1248 bond_for_each_slave(bond, slave, i) {
1249 features = netdev_compute_features(features,
1250 slave->dev->features);
1251 if (slave->dev->hard_header_len > max_hard_header_len)
1252 max_hard_header_len = slave->dev->hard_header_len;
1255 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1256 bond_dev->features = features;
1257 bond_dev->hard_header_len = max_hard_header_len;
1263 static void bond_setup_by_slave(struct net_device *bond_dev,
1264 struct net_device *slave_dev)
1266 struct bonding *bond = bond_dev->priv;
1268 bond_dev->neigh_setup = slave_dev->neigh_setup;
1269 bond_dev->header_ops = slave_dev->header_ops;
1271 bond_dev->type = slave_dev->type;
1272 bond_dev->hard_header_len = slave_dev->hard_header_len;
1273 bond_dev->addr_len = slave_dev->addr_len;
1275 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1276 slave_dev->addr_len);
1277 bond->setup_by_slave = 1;
1280 /* enslave device <slave> to bond device <master> */
1281 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1283 struct bonding *bond = bond_dev->priv;
1284 struct slave *new_slave = NULL;
1285 struct dev_mc_list *dmi;
1286 struct sockaddr addr;
1288 int old_features = bond_dev->features;
1291 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1292 slave_dev->do_ioctl == NULL) {
1293 printk(KERN_WARNING DRV_NAME
1294 ": %s: Warning: no link monitoring support for %s\n",
1295 bond_dev->name, slave_dev->name);
1298 /* bond must be initialized by bond_open() before enslaving */
1299 if (!(bond_dev->flags & IFF_UP)) {
1300 printk(KERN_WARNING DRV_NAME
1301 " %s: master_dev is not up in bond_enslave\n",
1305 /* already enslaved */
1306 if (slave_dev->flags & IFF_SLAVE) {
1307 dprintk("Error, Device was already enslaved\n");
1311 /* vlan challenged mutual exclusion */
1312 /* no need to lock since we're protected by rtnl_lock */
1313 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1314 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1315 if (!list_empty(&bond->vlan_list)) {
1316 printk(KERN_ERR DRV_NAME
1317 ": %s: Error: cannot enslave VLAN "
1318 "challenged slave %s on VLAN enabled "
1319 "bond %s\n", bond_dev->name, slave_dev->name,
1323 printk(KERN_WARNING DRV_NAME
1324 ": %s: Warning: enslaved VLAN challenged "
1325 "slave %s. Adding VLANs will be blocked as "
1326 "long as %s is part of bond %s\n",
1327 bond_dev->name, slave_dev->name, slave_dev->name,
1329 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1332 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1333 if (bond->slave_cnt == 0) {
1334 /* First slave, and it is not VLAN challenged,
1335 * so remove the block of adding VLANs over the bond.
1337 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1342 * Old ifenslave binaries are no longer supported. These can
1343 * be identified with moderate accurary by the state of the slave:
1344 * the current ifenslave will set the interface down prior to
1345 * enslaving it; the old ifenslave will not.
1347 if ((slave_dev->flags & IFF_UP)) {
1348 printk(KERN_ERR DRV_NAME ": %s is up. "
1349 "This may be due to an out of date ifenslave.\n",
1352 goto err_undo_flags;
1355 /* set bonding device ether type by slave - bonding netdevices are
1356 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1357 * there is a need to override some of the type dependent attribs/funcs.
1359 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1360 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1362 if (bond->slave_cnt == 0) {
1363 if (slave_dev->type != ARPHRD_ETHER)
1364 bond_setup_by_slave(bond_dev, slave_dev);
1365 } else if (bond_dev->type != slave_dev->type) {
1366 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1367 "from other slaves (%d), can not enslave it.\n",
1369 slave_dev->type, bond_dev->type);
1371 goto err_undo_flags;
1374 if (slave_dev->set_mac_address == NULL) {
1375 if (bond->slave_cnt == 0) {
1376 printk(KERN_WARNING DRV_NAME
1377 ": %s: Warning: The first slave device "
1378 "specified does not support setting the MAC "
1379 "address. Enabling the fail_over_mac option.",
1381 bond->params.fail_over_mac = 1;
1382 } else if (!bond->params.fail_over_mac) {
1383 printk(KERN_ERR DRV_NAME
1384 ": %s: Error: The slave device specified "
1385 "does not support setting the MAC address, "
1386 "but fail_over_mac is not enabled.\n"
1389 goto err_undo_flags;
1393 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1396 goto err_undo_flags;
1399 /* save slave's original flags before calling
1400 * netdev_set_master and dev_open
1402 new_slave->original_flags = slave_dev->flags;
1405 * Save slave's original ("permanent") mac address for modes
1406 * that need it, and for restoring it upon release, and then
1407 * set it to the master's address
1409 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1411 if (!bond->params.fail_over_mac) {
1413 * Set slave to master's mac address. The application already
1414 * set the master's mac address to that of the first slave
1416 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1417 addr.sa_family = slave_dev->type;
1418 res = dev_set_mac_address(slave_dev, &addr);
1420 dprintk("Error %d calling set_mac_address\n", res);
1425 res = netdev_set_master(slave_dev, bond_dev);
1427 dprintk("Error %d calling netdev_set_master\n", res);
1428 goto err_restore_mac;
1430 /* open the slave since the application closed it */
1431 res = dev_open(slave_dev);
1433 dprintk("Openning slave %s failed\n", slave_dev->name);
1434 goto err_unset_master;
1437 new_slave->dev = slave_dev;
1438 slave_dev->priv_flags |= IFF_BONDING;
1440 if ((bond->params.mode == BOND_MODE_TLB) ||
1441 (bond->params.mode == BOND_MODE_ALB)) {
1442 /* bond_alb_init_slave() must be called before all other stages since
1443 * it might fail and we do not want to have to undo everything
1445 res = bond_alb_init_slave(bond, new_slave);
1451 /* If the mode USES_PRIMARY, then the new slave gets the
1452 * master's promisc (and mc) settings only if it becomes the
1453 * curr_active_slave, and that is taken care of later when calling
1454 * bond_change_active()
1456 if (!USES_PRIMARY(bond->params.mode)) {
1457 /* set promiscuity level to new slave */
1458 if (bond_dev->flags & IFF_PROMISC) {
1459 dev_set_promiscuity(slave_dev, 1);
1462 /* set allmulti level to new slave */
1463 if (bond_dev->flags & IFF_ALLMULTI) {
1464 dev_set_allmulti(slave_dev, 1);
1467 netif_tx_lock_bh(bond_dev);
1468 /* upload master's mc_list to new slave */
1469 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1470 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1472 netif_tx_unlock_bh(bond_dev);
1475 if (bond->params.mode == BOND_MODE_8023AD) {
1476 /* add lacpdu mc addr to mc list */
1477 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1479 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1482 bond_add_vlans_on_slave(bond, slave_dev);
1484 write_lock_bh(&bond->lock);
1486 bond_attach_slave(bond, new_slave);
1488 new_slave->delay = 0;
1489 new_slave->link_failure_count = 0;
1491 bond_compute_features(bond);
1493 new_slave->last_arp_rx = jiffies;
1495 if (bond->params.miimon && !bond->params.use_carrier) {
1496 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1498 if ((link_reporting == -1) && !bond->params.arp_interval) {
1500 * miimon is set but a bonded network driver
1501 * does not support ETHTOOL/MII and
1502 * arp_interval is not set. Note: if
1503 * use_carrier is enabled, we will never go
1504 * here (because netif_carrier is always
1505 * supported); thus, we don't need to change
1506 * the messages for netif_carrier.
1508 printk(KERN_WARNING DRV_NAME
1509 ": %s: Warning: MII and ETHTOOL support not "
1510 "available for interface %s, and "
1511 "arp_interval/arp_ip_target module parameters "
1512 "not specified, thus bonding will not detect "
1513 "link failures! see bonding.txt for details.\n",
1514 bond_dev->name, slave_dev->name);
1515 } else if (link_reporting == -1) {
1516 /* unable get link status using mii/ethtool */
1517 printk(KERN_WARNING DRV_NAME
1518 ": %s: Warning: can't get link status from "
1519 "interface %s; the network driver associated "
1520 "with this interface does not support MII or "
1521 "ETHTOOL link status reporting, thus miimon "
1522 "has no effect on this interface.\n",
1523 bond_dev->name, slave_dev->name);
1527 /* check for initial state */
1528 if (!bond->params.miimon ||
1529 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1530 if (bond->params.updelay) {
1531 dprintk("Initial state of slave_dev is "
1532 "BOND_LINK_BACK\n");
1533 new_slave->link = BOND_LINK_BACK;
1534 new_slave->delay = bond->params.updelay;
1536 dprintk("Initial state of slave_dev is "
1538 new_slave->link = BOND_LINK_UP;
1540 new_slave->jiffies = jiffies;
1542 dprintk("Initial state of slave_dev is "
1543 "BOND_LINK_DOWN\n");
1544 new_slave->link = BOND_LINK_DOWN;
1547 if (bond_update_speed_duplex(new_slave) &&
1548 (new_slave->link != BOND_LINK_DOWN)) {
1549 printk(KERN_WARNING DRV_NAME
1550 ": %s: Warning: failed to get speed and duplex from %s, "
1551 "assumed to be 100Mb/sec and Full.\n",
1552 bond_dev->name, new_slave->dev->name);
1554 if (bond->params.mode == BOND_MODE_8023AD) {
1555 printk(KERN_WARNING DRV_NAME
1556 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1557 "support in base driver for proper aggregator "
1558 "selection.\n", bond_dev->name);
1562 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1563 /* if there is a primary slave, remember it */
1564 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1565 bond->primary_slave = new_slave;
1569 switch (bond->params.mode) {
1570 case BOND_MODE_ACTIVEBACKUP:
1571 bond_set_slave_inactive_flags(new_slave);
1572 bond_select_active_slave(bond);
1574 case BOND_MODE_8023AD:
1575 /* in 802.3ad mode, the internal mechanism
1576 * will activate the slaves in the selected
1579 bond_set_slave_inactive_flags(new_slave);
1580 /* if this is the first slave */
1581 if (bond->slave_cnt == 1) {
1582 SLAVE_AD_INFO(new_slave).id = 1;
1583 /* Initialize AD with the number of times that the AD timer is called in 1 second
1584 * can be called only after the mac address of the bond is set
1586 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1587 bond->params.lacp_fast);
1589 SLAVE_AD_INFO(new_slave).id =
1590 SLAVE_AD_INFO(new_slave->prev).id + 1;
1593 bond_3ad_bind_slave(new_slave);
1597 new_slave->state = BOND_STATE_ACTIVE;
1598 bond_set_slave_inactive_flags(new_slave);
1601 dprintk("This slave is always active in trunk mode\n");
1603 /* always active in trunk mode */
1604 new_slave->state = BOND_STATE_ACTIVE;
1606 /* In trunking mode there is little meaning to curr_active_slave
1607 * anyway (it holds no special properties of the bond device),
1608 * so we can change it without calling change_active_interface()
1610 if (!bond->curr_active_slave) {
1611 bond->curr_active_slave = new_slave;
1614 } /* switch(bond_mode) */
1616 bond_set_carrier(bond);
1618 write_unlock_bh(&bond->lock);
1620 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1624 printk(KERN_INFO DRV_NAME
1625 ": %s: enslaving %s as a%s interface with a%s link.\n",
1626 bond_dev->name, slave_dev->name,
1627 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1628 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1630 /* enslave is successful */
1633 /* Undo stages on error */
1635 dev_close(slave_dev);
1638 netdev_set_master(slave_dev, NULL);
1641 if (!bond->params.fail_over_mac) {
1642 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1643 addr.sa_family = slave_dev->type;
1644 dev_set_mac_address(slave_dev, &addr);
1651 bond_dev->features = old_features;
1657 * Try to release the slave device <slave> from the bond device <master>
1658 * It is legal to access curr_active_slave without a lock because all the function
1661 * The rules for slave state should be:
1662 * for Active/Backup:
1663 * Active stays on all backups go down
1664 * for Bonded connections:
1665 * The first up interface should be left on and all others downed.
1667 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1669 struct bonding *bond = bond_dev->priv;
1670 struct slave *slave, *oldcurrent;
1671 struct sockaddr addr;
1672 int mac_addr_differ;
1673 DECLARE_MAC_BUF(mac);
1675 /* slave is not a slave or master is not master of this slave */
1676 if (!(slave_dev->flags & IFF_SLAVE) ||
1677 (slave_dev->master != bond_dev)) {
1678 printk(KERN_ERR DRV_NAME
1679 ": %s: Error: cannot release %s.\n",
1680 bond_dev->name, slave_dev->name);
1684 write_lock_bh(&bond->lock);
1686 slave = bond_get_slave_by_dev(bond, slave_dev);
1688 /* not a slave of this bond */
1689 printk(KERN_INFO DRV_NAME
1690 ": %s: %s not enslaved\n",
1691 bond_dev->name, slave_dev->name);
1692 write_unlock_bh(&bond->lock);
1696 mac_addr_differ = memcmp(bond_dev->dev_addr,
1699 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1700 printk(KERN_WARNING DRV_NAME
1701 ": %s: Warning: the permanent HWaddr of %s - "
1702 "%s - is still in use by %s. "
1703 "Set the HWaddr of %s to a different address "
1704 "to avoid conflicts.\n",
1707 print_mac(mac, slave->perm_hwaddr),
1712 /* Inform AD package of unbinding of slave. */
1713 if (bond->params.mode == BOND_MODE_8023AD) {
1714 /* must be called before the slave is
1715 * detached from the list
1717 bond_3ad_unbind_slave(slave);
1720 printk(KERN_INFO DRV_NAME
1721 ": %s: releasing %s interface %s\n",
1723 (slave->state == BOND_STATE_ACTIVE)
1724 ? "active" : "backup",
1727 oldcurrent = bond->curr_active_slave;
1729 bond->current_arp_slave = NULL;
1731 /* release the slave from its bond */
1732 bond_detach_slave(bond, slave);
1734 bond_compute_features(bond);
1736 if (bond->primary_slave == slave) {
1737 bond->primary_slave = NULL;
1740 if (oldcurrent == slave) {
1741 bond_change_active_slave(bond, NULL);
1744 if ((bond->params.mode == BOND_MODE_TLB) ||
1745 (bond->params.mode == BOND_MODE_ALB)) {
1746 /* Must be called only after the slave has been
1747 * detached from the list and the curr_active_slave
1748 * has been cleared (if our_slave == old_current),
1749 * but before a new active slave is selected.
1751 write_unlock_bh(&bond->lock);
1752 bond_alb_deinit_slave(bond, slave);
1753 write_lock_bh(&bond->lock);
1756 if (oldcurrent == slave) {
1758 * Note that we hold RTNL over this sequence, so there
1759 * is no concern that another slave add/remove event
1762 write_unlock_bh(&bond->lock);
1763 read_lock(&bond->lock);
1764 write_lock_bh(&bond->curr_slave_lock);
1766 bond_select_active_slave(bond);
1768 write_unlock_bh(&bond->curr_slave_lock);
1769 read_unlock(&bond->lock);
1770 write_lock_bh(&bond->lock);
1773 if (bond->slave_cnt == 0) {
1774 bond_set_carrier(bond);
1776 /* if the last slave was removed, zero the mac address
1777 * of the master so it will be set by the application
1778 * to the mac address of the first slave
1780 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1782 if (list_empty(&bond->vlan_list)) {
1783 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1785 printk(KERN_WARNING DRV_NAME
1786 ": %s: Warning: clearing HW address of %s while it "
1787 "still has VLANs.\n",
1788 bond_dev->name, bond_dev->name);
1789 printk(KERN_WARNING DRV_NAME
1790 ": %s: When re-adding slaves, make sure the bond's "
1791 "HW address matches its VLANs'.\n",
1794 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1795 !bond_has_challenged_slaves(bond)) {
1796 printk(KERN_INFO DRV_NAME
1797 ": %s: last VLAN challenged slave %s "
1798 "left bond %s. VLAN blocking is removed\n",
1799 bond_dev->name, slave_dev->name, bond_dev->name);
1800 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1803 write_unlock_bh(&bond->lock);
1805 /* must do this from outside any spinlocks */
1806 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1808 bond_del_vlans_from_slave(bond, slave_dev);
1810 /* If the mode USES_PRIMARY, then we should only remove its
1811 * promisc and mc settings if it was the curr_active_slave, but that was
1812 * already taken care of above when we detached the slave
1814 if (!USES_PRIMARY(bond->params.mode)) {
1815 /* unset promiscuity level from slave */
1816 if (bond_dev->flags & IFF_PROMISC) {
1817 dev_set_promiscuity(slave_dev, -1);
1820 /* unset allmulti level from slave */
1821 if (bond_dev->flags & IFF_ALLMULTI) {
1822 dev_set_allmulti(slave_dev, -1);
1825 /* flush master's mc_list from slave */
1826 netif_tx_lock_bh(bond_dev);
1827 bond_mc_list_flush(bond_dev, slave_dev);
1828 netif_tx_unlock_bh(bond_dev);
1831 netdev_set_master(slave_dev, NULL);
1833 /* close slave before restoring its mac address */
1834 dev_close(slave_dev);
1836 if (!bond->params.fail_over_mac) {
1837 /* restore original ("permanent") mac address */
1838 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1839 addr.sa_family = slave_dev->type;
1840 dev_set_mac_address(slave_dev, &addr);
1843 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1844 IFF_SLAVE_INACTIVE | IFF_BONDING |
1849 return 0; /* deletion OK */
1853 * Destroy a bonding device.
1854 * Must be under rtnl_lock when this function is called.
1856 void bond_destroy(struct bonding *bond)
1858 bond_deinit(bond->dev);
1859 bond_destroy_sysfs_entry(bond);
1860 unregister_netdevice(bond->dev);
1864 * First release a slave and than destroy the bond if no more slaves iare left.
1865 * Must be under rtnl_lock when this function is called.
1867 int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1869 struct bonding *bond = bond_dev->priv;
1872 ret = bond_release(bond_dev, slave_dev);
1873 if ((ret == 0) && (bond->slave_cnt == 0)) {
1874 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1875 bond_dev->name, bond_dev->name);
1882 * This function releases all slaves.
1884 static int bond_release_all(struct net_device *bond_dev)
1886 struct bonding *bond = bond_dev->priv;
1887 struct slave *slave;
1888 struct net_device *slave_dev;
1889 struct sockaddr addr;
1891 write_lock_bh(&bond->lock);
1893 netif_carrier_off(bond_dev);
1895 if (bond->slave_cnt == 0) {
1899 bond->current_arp_slave = NULL;
1900 bond->primary_slave = NULL;
1901 bond_change_active_slave(bond, NULL);
1903 while ((slave = bond->first_slave) != NULL) {
1904 /* Inform AD package of unbinding of slave
1905 * before slave is detached from the list.
1907 if (bond->params.mode == BOND_MODE_8023AD) {
1908 bond_3ad_unbind_slave(slave);
1911 slave_dev = slave->dev;
1912 bond_detach_slave(bond, slave);
1914 /* now that the slave is detached, unlock and perform
1915 * all the undo steps that should not be called from
1918 write_unlock_bh(&bond->lock);
1920 if ((bond->params.mode == BOND_MODE_TLB) ||
1921 (bond->params.mode == BOND_MODE_ALB)) {
1922 /* must be called only after the slave
1923 * has been detached from the list
1925 bond_alb_deinit_slave(bond, slave);
1928 bond_compute_features(bond);
1930 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1931 bond_del_vlans_from_slave(bond, slave_dev);
1933 /* If the mode USES_PRIMARY, then we should only remove its
1934 * promisc and mc settings if it was the curr_active_slave, but that was
1935 * already taken care of above when we detached the slave
1937 if (!USES_PRIMARY(bond->params.mode)) {
1938 /* unset promiscuity level from slave */
1939 if (bond_dev->flags & IFF_PROMISC) {
1940 dev_set_promiscuity(slave_dev, -1);
1943 /* unset allmulti level from slave */
1944 if (bond_dev->flags & IFF_ALLMULTI) {
1945 dev_set_allmulti(slave_dev, -1);
1948 /* flush master's mc_list from slave */
1949 netif_tx_lock_bh(bond_dev);
1950 bond_mc_list_flush(bond_dev, slave_dev);
1951 netif_tx_unlock_bh(bond_dev);
1954 netdev_set_master(slave_dev, NULL);
1956 /* close slave before restoring its mac address */
1957 dev_close(slave_dev);
1959 if (!bond->params.fail_over_mac) {
1960 /* restore original ("permanent") mac address*/
1961 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1962 addr.sa_family = slave_dev->type;
1963 dev_set_mac_address(slave_dev, &addr);
1966 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1967 IFF_SLAVE_INACTIVE);
1971 /* re-acquire the lock before getting the next slave */
1972 write_lock_bh(&bond->lock);
1975 /* zero the mac address of the master so it will be
1976 * set by the application to the mac address of the
1979 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1981 if (list_empty(&bond->vlan_list)) {
1982 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1984 printk(KERN_WARNING DRV_NAME
1985 ": %s: Warning: clearing HW address of %s while it "
1986 "still has VLANs.\n",
1987 bond_dev->name, bond_dev->name);
1988 printk(KERN_WARNING DRV_NAME
1989 ": %s: When re-adding slaves, make sure the bond's "
1990 "HW address matches its VLANs'.\n",
1994 printk(KERN_INFO DRV_NAME
1995 ": %s: released all slaves\n",
1999 write_unlock_bh(&bond->lock);
2005 * This function changes the active slave to slave <slave_dev>.
2006 * It returns -EINVAL in the following cases.
2007 * - <slave_dev> is not found in the list.
2008 * - There is not active slave now.
2009 * - <slave_dev> is already active.
2010 * - The link state of <slave_dev> is not BOND_LINK_UP.
2011 * - <slave_dev> is not running.
2012 * In these cases, this fuction does nothing.
2013 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2015 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2017 struct bonding *bond = bond_dev->priv;
2018 struct slave *old_active = NULL;
2019 struct slave *new_active = NULL;
2022 if (!USES_PRIMARY(bond->params.mode)) {
2026 /* Verify that master_dev is indeed the master of slave_dev */
2027 if (!(slave_dev->flags & IFF_SLAVE) ||
2028 (slave_dev->master != bond_dev)) {
2032 read_lock(&bond->lock);
2034 read_lock(&bond->curr_slave_lock);
2035 old_active = bond->curr_active_slave;
2036 read_unlock(&bond->curr_slave_lock);
2038 new_active = bond_get_slave_by_dev(bond, slave_dev);
2041 * Changing to the current active: do nothing; return success.
2043 if (new_active && (new_active == old_active)) {
2044 read_unlock(&bond->lock);
2050 (new_active->link == BOND_LINK_UP) &&
2051 IS_UP(new_active->dev)) {
2052 write_lock_bh(&bond->curr_slave_lock);
2053 bond_change_active_slave(bond, new_active);
2054 write_unlock_bh(&bond->curr_slave_lock);
2059 read_unlock(&bond->lock);
2064 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2066 struct bonding *bond = bond_dev->priv;
2068 info->bond_mode = bond->params.mode;
2069 info->miimon = bond->params.miimon;
2071 read_lock(&bond->lock);
2072 info->num_slaves = bond->slave_cnt;
2073 read_unlock(&bond->lock);
2078 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2080 struct bonding *bond = bond_dev->priv;
2081 struct slave *slave;
2084 if (info->slave_id < 0) {
2088 read_lock(&bond->lock);
2090 bond_for_each_slave(bond, slave, i) {
2091 if (i == (int)info->slave_id) {
2097 read_unlock(&bond->lock);
2100 strcpy(info->slave_name, slave->dev->name);
2101 info->link = slave->link;
2102 info->state = slave->state;
2103 info->link_failure_count = slave->link_failure_count;
2111 /*-------------------------------- Monitoring -------------------------------*/
2114 * if !have_locks, return nonzero if a failover is necessary. if
2115 * have_locks, do whatever failover activities are needed.
2117 * This is to separate the inspection and failover steps for locking
2118 * purposes; failover requires rtnl, but acquiring it for every
2119 * inspection is undesirable, so a wrapper first does inspection, and
2120 * the acquires the necessary locks and calls again to perform
2121 * failover if needed. Since all locks are dropped, a complete
2122 * restart is needed between calls.
2124 static int __bond_mii_monitor(struct bonding *bond, int have_locks)
2126 struct slave *slave, *oldcurrent;
2127 int do_failover = 0;
2130 if (bond->slave_cnt == 0)
2133 /* we will try to read the link status of each of our slaves, and
2134 * set their IFF_RUNNING flag appropriately. For each slave not
2135 * supporting MII status, we won't do anything so that a user-space
2136 * program could monitor the link itself if needed.
2139 if (bond->send_grat_arp) {
2140 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2141 &bond->curr_active_slave->dev->state))
2142 dprintk("Needs to send gratuitous arp but not yet\n");
2144 dprintk("sending delayed gratuitous arp on on %s\n",
2145 bond->curr_active_slave->dev->name);
2146 bond_send_gratuitous_arp(bond);
2147 bond->send_grat_arp = 0;
2150 read_lock(&bond->curr_slave_lock);
2151 oldcurrent = bond->curr_active_slave;
2152 read_unlock(&bond->curr_slave_lock);
2154 bond_for_each_slave(bond, slave, i) {
2155 struct net_device *slave_dev = slave->dev;
2157 u16 old_speed = slave->speed;
2158 u8 old_duplex = slave->duplex;
2160 link_state = bond_check_dev_link(bond, slave_dev, 0);
2162 switch (slave->link) {
2163 case BOND_LINK_UP: /* the link was up */
2164 if (link_state == BMSR_LSTATUS) {
2171 } else { /* link going down */
2172 slave->link = BOND_LINK_FAIL;
2173 slave->delay = bond->params.downdelay;
2175 if (slave->link_failure_count < UINT_MAX) {
2176 slave->link_failure_count++;
2179 if (bond->params.downdelay) {
2180 printk(KERN_INFO DRV_NAME
2181 ": %s: link status down for %s "
2182 "interface %s, disabling it in "
2186 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2187 ? ((slave == oldcurrent)
2188 ? "active " : "backup ")
2192 bond->params.downdelay * bond->params.miimon);
2195 /* no break ! fall through the BOND_LINK_FAIL test to
2196 ensure proper action to be taken
2198 case BOND_LINK_FAIL: /* the link has just gone down */
2199 if (link_state != BMSR_LSTATUS) {
2200 /* link stays down */
2201 if (slave->delay <= 0) {
2205 /* link down for too long time */
2206 slave->link = BOND_LINK_DOWN;
2208 /* in active/backup mode, we must
2209 * completely disable this interface
2211 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2212 (bond->params.mode == BOND_MODE_8023AD)) {
2213 bond_set_slave_inactive_flags(slave);
2216 printk(KERN_INFO DRV_NAME
2217 ": %s: link status definitely "
2218 "down for interface %s, "
2223 /* notify ad that the link status has changed */
2224 if (bond->params.mode == BOND_MODE_8023AD) {
2225 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2228 if ((bond->params.mode == BOND_MODE_TLB) ||
2229 (bond->params.mode == BOND_MODE_ALB)) {
2230 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2233 if (slave == oldcurrent) {
2241 slave->link = BOND_LINK_UP;
2242 slave->jiffies = jiffies;
2243 printk(KERN_INFO DRV_NAME
2244 ": %s: link status up again after %d "
2245 "ms for interface %s.\n",
2247 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2251 case BOND_LINK_DOWN: /* the link was down */
2252 if (link_state != BMSR_LSTATUS) {
2253 /* the link stays down, nothing more to do */
2255 } else { /* link going up */
2256 slave->link = BOND_LINK_BACK;
2257 slave->delay = bond->params.updelay;
2259 if (bond->params.updelay) {
2260 /* if updelay == 0, no need to
2261 advertise about a 0 ms delay */
2262 printk(KERN_INFO DRV_NAME
2263 ": %s: link status up for "
2264 "interface %s, enabling it "
2268 bond->params.updelay * bond->params.miimon);
2271 /* no break ! fall through the BOND_LINK_BACK state in
2272 case there's something to do.
2274 case BOND_LINK_BACK: /* the link has just come back */
2275 if (link_state != BMSR_LSTATUS) {
2276 /* link down again */
2277 slave->link = BOND_LINK_DOWN;
2279 printk(KERN_INFO DRV_NAME
2280 ": %s: link status down again after %d "
2281 "ms for interface %s.\n",
2283 (bond->params.updelay - slave->delay) * bond->params.miimon,
2287 if (slave->delay == 0) {
2291 /* now the link has been up for long time enough */
2292 slave->link = BOND_LINK_UP;
2293 slave->jiffies = jiffies;
2295 if (bond->params.mode == BOND_MODE_8023AD) {
2296 /* prevent it from being the active one */
2297 slave->state = BOND_STATE_BACKUP;
2298 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2299 /* make it immediately active */
2300 slave->state = BOND_STATE_ACTIVE;
2301 } else if (slave != bond->primary_slave) {
2302 /* prevent it from being the active one */
2303 slave->state = BOND_STATE_BACKUP;
2306 printk(KERN_INFO DRV_NAME
2307 ": %s: link status definitely "
2308 "up for interface %s.\n",
2312 /* notify ad that the link status has changed */
2313 if (bond->params.mode == BOND_MODE_8023AD) {
2314 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2317 if ((bond->params.mode == BOND_MODE_TLB) ||
2318 (bond->params.mode == BOND_MODE_ALB)) {
2319 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2322 if ((!oldcurrent) ||
2323 (slave == bond->primary_slave)) {
2332 /* Should not happen */
2333 printk(KERN_ERR DRV_NAME
2334 ": %s: Error: %s Illegal value (link=%d)\n",
2339 } /* end of switch (slave->link) */
2341 bond_update_speed_duplex(slave);
2343 if (bond->params.mode == BOND_MODE_8023AD) {
2344 if (old_speed != slave->speed) {
2345 bond_3ad_adapter_speed_changed(slave);
2348 if (old_duplex != slave->duplex) {
2349 bond_3ad_adapter_duplex_changed(slave);
2358 write_lock_bh(&bond->curr_slave_lock);
2360 bond_select_active_slave(bond);
2362 write_unlock_bh(&bond->curr_slave_lock);
2365 bond_set_carrier(bond);
2374 * Really a wrapper that splits the mii monitor into two phases: an
2375 * inspection, then (if inspection indicates something needs to be
2376 * done) an acquisition of appropriate locks followed by another pass
2377 * to implement whatever link state changes are indicated.
2379 void bond_mii_monitor(struct work_struct *work)
2381 struct bonding *bond = container_of(work, struct bonding,
2383 unsigned long delay;
2385 read_lock(&bond->lock);
2386 if (bond->kill_timers) {
2387 read_unlock(&bond->lock);
2390 if (__bond_mii_monitor(bond, 0)) {
2391 read_unlock(&bond->lock);
2393 read_lock(&bond->lock);
2394 __bond_mii_monitor(bond, 1);
2395 read_unlock(&bond->lock);
2396 rtnl_unlock(); /* might sleep, hold no other locks */
2397 read_lock(&bond->lock);
2400 delay = msecs_to_jiffies(bond->params.miimon);
2401 read_unlock(&bond->lock);
2402 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2405 static __be32 bond_glean_dev_ip(struct net_device *dev)
2407 struct in_device *idev;
2408 struct in_ifaddr *ifa;
2415 idev = __in_dev_get_rcu(dev);
2419 ifa = idev->ifa_list;
2423 addr = ifa->ifa_local;
2429 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2431 struct vlan_entry *vlan;
2433 if (ip == bond->master_ip)
2436 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2437 if (ip == vlan->vlan_ip)
2445 * We go to the (large) trouble of VLAN tagging ARP frames because
2446 * switches in VLAN mode (especially if ports are configured as
2447 * "native" to a VLAN) might not pass non-tagged frames.
2449 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2451 struct sk_buff *skb;
2453 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2454 slave_dev->name, dest_ip, src_ip, vlan_id);
2456 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2457 NULL, slave_dev->dev_addr, NULL);
2460 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2464 skb = vlan_put_tag(skb, vlan_id);
2466 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2474 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2477 __be32 *targets = bond->params.arp_targets;
2478 struct vlan_entry *vlan;
2479 struct net_device *vlan_dev;
2483 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2486 dprintk("basa: target %x\n", targets[i]);
2487 if (list_empty(&bond->vlan_list)) {
2488 dprintk("basa: empty vlan: arp_send\n");
2489 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2490 bond->master_ip, 0);
2495 * If VLANs are configured, we do a route lookup to
2496 * determine which VLAN interface would be used, so we
2497 * can tag the ARP with the proper VLAN tag.
2499 memset(&fl, 0, sizeof(fl));
2500 fl.fl4_dst = targets[i];
2501 fl.fl4_tos = RTO_ONLINK;
2503 rv = ip_route_output_key(&init_net, &rt, &fl);
2505 if (net_ratelimit()) {
2506 printk(KERN_WARNING DRV_NAME
2507 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2508 bond->dev->name, NIPQUAD(fl.fl4_dst));
2514 * This target is not on a VLAN
2516 if (rt->u.dst.dev == bond->dev) {
2518 dprintk("basa: rtdev == bond->dev: arp_send\n");
2519 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2520 bond->master_ip, 0);
2525 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2526 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2527 if (vlan_dev == rt->u.dst.dev) {
2528 vlan_id = vlan->vlan_id;
2529 dprintk("basa: vlan match on %s %d\n",
2530 vlan_dev->name, vlan_id);
2537 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2538 vlan->vlan_ip, vlan_id);
2542 if (net_ratelimit()) {
2543 printk(KERN_WARNING DRV_NAME
2544 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2545 bond->dev->name, NIPQUAD(fl.fl4_dst),
2546 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2553 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2554 * for each VLAN above us.
2556 static void bond_send_gratuitous_arp(struct bonding *bond)
2558 struct slave *slave = bond->curr_active_slave;
2559 struct vlan_entry *vlan;
2560 struct net_device *vlan_dev;
2562 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2563 slave ? slave->dev->name : "NULL");
2567 if (bond->master_ip) {
2568 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2569 bond->master_ip, 0);
2572 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2573 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2574 if (vlan->vlan_ip) {
2575 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2576 vlan->vlan_ip, vlan->vlan_id);
2581 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2584 __be32 *targets = bond->params.arp_targets;
2586 targets = bond->params.arp_targets;
2587 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2588 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2589 "%u.%u.%u.%u bhti(tip) %d\n",
2590 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2591 bond_has_this_ip(bond, tip));
2592 if (sip == targets[i]) {
2593 if (bond_has_this_ip(bond, tip))
2594 slave->last_arp_rx = jiffies;
2600 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2603 struct slave *slave;
2604 struct bonding *bond;
2605 unsigned char *arp_ptr;
2608 if (dev_net(dev) != &init_net)
2611 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2615 read_lock(&bond->lock);
2617 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2618 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2619 orig_dev ? orig_dev->name : "NULL");
2621 slave = bond_get_slave_by_dev(bond, orig_dev);
2622 if (!slave || !slave_do_arp_validate(bond, slave))
2625 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2629 if (arp->ar_hln != dev->addr_len ||
2630 skb->pkt_type == PACKET_OTHERHOST ||
2631 skb->pkt_type == PACKET_LOOPBACK ||
2632 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2633 arp->ar_pro != htons(ETH_P_IP) ||
2637 arp_ptr = (unsigned char *)(arp + 1);
2638 arp_ptr += dev->addr_len;
2639 memcpy(&sip, arp_ptr, 4);
2640 arp_ptr += 4 + dev->addr_len;
2641 memcpy(&tip, arp_ptr, 4);
2643 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2644 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2645 slave->state, bond->params.arp_validate,
2646 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2649 * Backup slaves won't see the ARP reply, but do come through
2650 * here for each ARP probe (so we swap the sip/tip to validate
2651 * the probe). In a "redundant switch, common router" type of
2652 * configuration, the ARP probe will (hopefully) travel from
2653 * the active, through one switch, the router, then the other
2654 * switch before reaching the backup.
2656 if (slave->state == BOND_STATE_ACTIVE)
2657 bond_validate_arp(bond, slave, sip, tip);
2659 bond_validate_arp(bond, slave, tip, sip);
2662 read_unlock(&bond->lock);
2665 return NET_RX_SUCCESS;
2669 * this function is called regularly to monitor each slave's link
2670 * ensuring that traffic is being sent and received when arp monitoring
2671 * is used in load-balancing mode. if the adapter has been dormant, then an
2672 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2673 * arp monitoring in active backup mode.
2675 void bond_loadbalance_arp_mon(struct work_struct *work)
2677 struct bonding *bond = container_of(work, struct bonding,
2679 struct slave *slave, *oldcurrent;
2680 int do_failover = 0;
2684 read_lock(&bond->lock);
2686 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2688 if (bond->kill_timers) {
2692 if (bond->slave_cnt == 0) {
2696 read_lock(&bond->curr_slave_lock);
2697 oldcurrent = bond->curr_active_slave;
2698 read_unlock(&bond->curr_slave_lock);
2700 /* see if any of the previous devices are up now (i.e. they have
2701 * xmt and rcv traffic). the curr_active_slave does not come into
2702 * the picture unless it is null. also, slave->jiffies is not needed
2703 * here because we send an arp on each slave and give a slave as
2704 * long as it needs to get the tx/rx within the delta.
2705 * TODO: what about up/down delay in arp mode? it wasn't here before
2708 bond_for_each_slave(bond, slave, i) {
2709 if (slave->link != BOND_LINK_UP) {
2710 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2711 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
2713 slave->link = BOND_LINK_UP;
2714 slave->state = BOND_STATE_ACTIVE;
2716 /* primary_slave has no meaning in round-robin
2717 * mode. the window of a slave being up and
2718 * curr_active_slave being null after enslaving
2722 printk(KERN_INFO DRV_NAME
2723 ": %s: link status definitely "
2724 "up for interface %s, ",
2729 printk(KERN_INFO DRV_NAME
2730 ": %s: interface %s is now up\n",
2736 /* slave->link == BOND_LINK_UP */
2738 /* not all switches will respond to an arp request
2739 * when the source ip is 0, so don't take the link down
2740 * if we don't know our ip yet
2742 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2743 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
2745 slave->link = BOND_LINK_DOWN;
2746 slave->state = BOND_STATE_BACKUP;
2748 if (slave->link_failure_count < UINT_MAX) {
2749 slave->link_failure_count++;
2752 printk(KERN_INFO DRV_NAME
2753 ": %s: interface %s is now down.\n",
2757 if (slave == oldcurrent) {
2763 /* note: if switch is in round-robin mode, all links
2764 * must tx arp to ensure all links rx an arp - otherwise
2765 * links may oscillate or not come up at all; if switch is
2766 * in something like xor mode, there is nothing we can
2767 * do - all replies will be rx'ed on same link causing slaves
2768 * to be unstable during low/no traffic periods
2770 if (IS_UP(slave->dev)) {
2771 bond_arp_send_all(bond, slave);
2776 write_lock_bh(&bond->curr_slave_lock);
2778 bond_select_active_slave(bond);
2780 write_unlock_bh(&bond->curr_slave_lock);
2784 if (bond->params.arp_interval)
2785 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2787 read_unlock(&bond->lock);
2791 * When using arp monitoring in active-backup mode, this function is
2792 * called to determine if any backup slaves have went down or a new
2793 * current slave needs to be found.
2794 * The backup slaves never generate traffic, they are considered up by merely
2795 * receiving traffic. If the current slave goes down, each backup slave will
2796 * be given the opportunity to tx/rx an arp before being taken down - this
2797 * prevents all slaves from being taken down due to the current slave not
2798 * sending any traffic for the backups to receive. The arps are not necessarily
2799 * necessary, any tx and rx traffic will keep the current slave up. While any
2800 * rx traffic will keep the backup slaves up, the current slave is responsible
2801 * for generating traffic to keep them up regardless of any other traffic they
2802 * may have received.
2803 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2805 void bond_activebackup_arp_mon(struct work_struct *work)
2807 struct bonding *bond = container_of(work, struct bonding,
2809 struct slave *slave;
2813 read_lock(&bond->lock);
2815 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2817 if (bond->kill_timers) {
2821 if (bond->slave_cnt == 0) {
2825 /* determine if any slave has come up or any backup slave has
2827 * TODO: what about up/down delay in arp mode? it wasn't here before
2830 bond_for_each_slave(bond, slave, i) {
2831 if (slave->link != BOND_LINK_UP) {
2832 if (time_before_eq(jiffies,
2833 slave_last_rx(bond, slave) + delta_in_ticks)) {
2835 slave->link = BOND_LINK_UP;
2837 write_lock_bh(&bond->curr_slave_lock);
2839 if ((!bond->curr_active_slave) &&
2840 time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks)) {
2841 bond_change_active_slave(bond, slave);
2842 bond->current_arp_slave = NULL;
2843 } else if (bond->curr_active_slave != slave) {
2844 /* this slave has just come up but we
2845 * already have a current slave; this
2846 * can also happen if bond_enslave adds
2847 * a new slave that is up while we are
2848 * searching for a new slave
2850 bond_set_slave_inactive_flags(slave);
2851 bond->current_arp_slave = NULL;
2854 bond_set_carrier(bond);
2856 if (slave == bond->curr_active_slave) {
2857 printk(KERN_INFO DRV_NAME
2858 ": %s: %s is up and now the "
2859 "active interface\n",
2862 netif_carrier_on(bond->dev);
2864 printk(KERN_INFO DRV_NAME
2865 ": %s: backup interface %s is "
2871 write_unlock_bh(&bond->curr_slave_lock);
2874 read_lock(&bond->curr_slave_lock);
2876 if ((slave != bond->curr_active_slave) &&
2877 (!bond->current_arp_slave) &&
2878 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 3*delta_in_ticks))) {
2879 /* a backup slave has gone down; three times
2880 * the delta allows the current slave to be
2881 * taken out before the backup slave.
2882 * note: a non-null current_arp_slave indicates
2883 * the curr_active_slave went down and we are
2884 * searching for a new one; under this
2885 * condition we only take the curr_active_slave
2886 * down - this gives each slave a chance to
2887 * tx/rx traffic before being taken out
2890 read_unlock(&bond->curr_slave_lock);
2892 slave->link = BOND_LINK_DOWN;
2894 if (slave->link_failure_count < UINT_MAX) {
2895 slave->link_failure_count++;
2898 bond_set_slave_inactive_flags(slave);
2900 printk(KERN_INFO DRV_NAME
2901 ": %s: backup interface %s is now down\n",
2905 read_unlock(&bond->curr_slave_lock);
2910 read_lock(&bond->curr_slave_lock);
2911 slave = bond->curr_active_slave;
2912 read_unlock(&bond->curr_slave_lock);
2915 /* if we have sent traffic in the past 2*arp_intervals but
2916 * haven't xmit and rx traffic in that time interval, select
2917 * a different slave. slave->jiffies is only updated when
2918 * a slave first becomes the curr_active_slave - not necessarily
2919 * after every arp; this ensures the slave has a full 2*delta
2920 * before being taken out. if a primary is being used, check
2921 * if it is up and needs to take over as the curr_active_slave
2923 if ((time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2924 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 2*delta_in_ticks))) &&
2925 time_after_eq(jiffies, slave->jiffies + 2*delta_in_ticks)) {
2927 slave->link = BOND_LINK_DOWN;
2929 if (slave->link_failure_count < UINT_MAX) {
2930 slave->link_failure_count++;
2933 printk(KERN_INFO DRV_NAME
2934 ": %s: link status down for active interface "
2935 "%s, disabling it\n",
2939 write_lock_bh(&bond->curr_slave_lock);
2941 bond_select_active_slave(bond);
2942 slave = bond->curr_active_slave;
2944 write_unlock_bh(&bond->curr_slave_lock);
2946 bond->current_arp_slave = slave;
2949 slave->jiffies = jiffies;
2951 } else if ((bond->primary_slave) &&
2952 (bond->primary_slave != slave) &&
2953 (bond->primary_slave->link == BOND_LINK_UP)) {
2954 /* at this point, slave is the curr_active_slave */
2955 printk(KERN_INFO DRV_NAME
2956 ": %s: changing from interface %s to primary "
2960 bond->primary_slave->dev->name);
2962 /* primary is up so switch to it */
2963 write_lock_bh(&bond->curr_slave_lock);
2964 bond_change_active_slave(bond, bond->primary_slave);
2965 write_unlock_bh(&bond->curr_slave_lock);
2967 slave = bond->primary_slave;
2968 slave->jiffies = jiffies;
2970 bond->current_arp_slave = NULL;
2973 /* the current slave must tx an arp to ensure backup slaves
2976 if (slave && IS_UP(slave->dev))
2977 bond_arp_send_all(bond, slave);
2980 /* if we don't have a curr_active_slave, search for the next available
2981 * backup slave from the current_arp_slave and make it the candidate
2982 * for becoming the curr_active_slave
2985 if (!bond->current_arp_slave) {
2986 bond->current_arp_slave = bond->first_slave;
2989 if (bond->current_arp_slave) {
2990 bond_set_slave_inactive_flags(bond->current_arp_slave);
2992 /* search for next candidate */
2993 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
2994 if (IS_UP(slave->dev)) {
2995 slave->link = BOND_LINK_BACK;
2996 bond_set_slave_active_flags(slave);
2997 bond_arp_send_all(bond, slave);
2998 slave->jiffies = jiffies;
2999 bond->current_arp_slave = slave;
3003 /* if the link state is up at this point, we
3004 * mark it down - this can happen if we have
3005 * simultaneous link failures and
3006 * reselect_active_interface doesn't make this
3007 * one the current slave so it is still marked
3008 * up when it is actually down
3010 if (slave->link == BOND_LINK_UP) {
3011 slave->link = BOND_LINK_DOWN;
3012 if (slave->link_failure_count < UINT_MAX) {
3013 slave->link_failure_count++;
3016 bond_set_slave_inactive_flags(slave);
3018 printk(KERN_INFO DRV_NAME
3019 ": %s: backup interface %s is "
3029 if (bond->params.arp_interval) {
3030 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3033 read_unlock(&bond->lock);
3036 /*------------------------------ proc/seq_file-------------------------------*/
3038 #ifdef CONFIG_PROC_FS
3040 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3042 struct bonding *bond = seq->private;
3044 struct slave *slave;
3047 /* make sure the bond won't be taken away */
3048 read_lock(&dev_base_lock);
3049 read_lock(&bond->lock);
3052 return SEQ_START_TOKEN;
3055 bond_for_each_slave(bond, slave, i) {
3056 if (++off == *pos) {
3064 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3066 struct bonding *bond = seq->private;
3067 struct slave *slave = v;
3070 if (v == SEQ_START_TOKEN) {
3071 return bond->first_slave;
3074 slave = slave->next;
3076 return (slave == bond->first_slave) ? NULL : slave;
3079 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3081 struct bonding *bond = seq->private;
3083 read_unlock(&bond->lock);
3084 read_unlock(&dev_base_lock);
3087 static void bond_info_show_master(struct seq_file *seq)
3089 struct bonding *bond = seq->private;
3094 read_lock(&bond->curr_slave_lock);
3095 curr = bond->curr_active_slave;
3096 read_unlock(&bond->curr_slave_lock);
3098 seq_printf(seq, "Bonding Mode: %s",
3099 bond_mode_name(bond->params.mode));
3101 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3102 bond->params.fail_over_mac)
3103 seq_printf(seq, " (fail_over_mac)");
3105 seq_printf(seq, "\n");
3107 if (bond->params.mode == BOND_MODE_XOR ||
3108 bond->params.mode == BOND_MODE_8023AD) {
3109 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3110 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3111 bond->params.xmit_policy);
3114 if (USES_PRIMARY(bond->params.mode)) {
3115 seq_printf(seq, "Primary Slave: %s\n",
3116 (bond->primary_slave) ?
3117 bond->primary_slave->dev->name : "None");
3119 seq_printf(seq, "Currently Active Slave: %s\n",
3120 (curr) ? curr->dev->name : "None");
3123 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3125 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3126 seq_printf(seq, "Up Delay (ms): %d\n",
3127 bond->params.updelay * bond->params.miimon);
3128 seq_printf(seq, "Down Delay (ms): %d\n",
3129 bond->params.downdelay * bond->params.miimon);
3132 /* ARP information */
3133 if(bond->params.arp_interval > 0) {
3135 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3136 bond->params.arp_interval);
3138 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3140 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3141 if (!bond->params.arp_targets[i])
3144 seq_printf(seq, ",");
3145 target = ntohl(bond->params.arp_targets[i]);
3146 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3149 seq_printf(seq, "\n");
3152 if (bond->params.mode == BOND_MODE_8023AD) {
3153 struct ad_info ad_info;
3154 DECLARE_MAC_BUF(mac);
3156 seq_puts(seq, "\n802.3ad info\n");
3157 seq_printf(seq, "LACP rate: %s\n",
3158 (bond->params.lacp_fast) ? "fast" : "slow");
3160 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3161 seq_printf(seq, "bond %s has no active aggregator\n",
3164 seq_printf(seq, "Active Aggregator Info:\n");
3166 seq_printf(seq, "\tAggregator ID: %d\n",
3167 ad_info.aggregator_id);
3168 seq_printf(seq, "\tNumber of ports: %d\n",
3170 seq_printf(seq, "\tActor Key: %d\n",
3172 seq_printf(seq, "\tPartner Key: %d\n",
3173 ad_info.partner_key);
3174 seq_printf(seq, "\tPartner Mac Address: %s\n",
3175 print_mac(mac, ad_info.partner_system));
3180 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3182 struct bonding *bond = seq->private;
3183 DECLARE_MAC_BUF(mac);
3185 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3186 seq_printf(seq, "MII Status: %s\n",
3187 (slave->link == BOND_LINK_UP) ? "up" : "down");
3188 seq_printf(seq, "Link Failure Count: %u\n",
3189 slave->link_failure_count);
3192 "Permanent HW addr: %s\n",
3193 print_mac(mac, slave->perm_hwaddr));
3195 if (bond->params.mode == BOND_MODE_8023AD) {
3196 const struct aggregator *agg
3197 = SLAVE_AD_INFO(slave).port.aggregator;
3200 seq_printf(seq, "Aggregator ID: %d\n",
3201 agg->aggregator_identifier);
3203 seq_puts(seq, "Aggregator ID: N/A\n");
3208 static int bond_info_seq_show(struct seq_file *seq, void *v)
3210 if (v == SEQ_START_TOKEN) {
3211 seq_printf(seq, "%s\n", version);
3212 bond_info_show_master(seq);
3214 bond_info_show_slave(seq, v);
3220 static struct seq_operations bond_info_seq_ops = {
3221 .start = bond_info_seq_start,
3222 .next = bond_info_seq_next,
3223 .stop = bond_info_seq_stop,
3224 .show = bond_info_seq_show,
3227 static int bond_info_open(struct inode *inode, struct file *file)
3229 struct seq_file *seq;
3230 struct proc_dir_entry *proc;
3233 res = seq_open(file, &bond_info_seq_ops);
3235 /* recover the pointer buried in proc_dir_entry data */
3236 seq = file->private_data;
3238 seq->private = proc->data;
3244 static const struct file_operations bond_info_fops = {
3245 .owner = THIS_MODULE,
3246 .open = bond_info_open,
3248 .llseek = seq_lseek,
3249 .release = seq_release,
3252 static int bond_create_proc_entry(struct bonding *bond)
3254 struct net_device *bond_dev = bond->dev;
3256 if (bond_proc_dir) {
3257 bond->proc_entry = proc_create_data(bond_dev->name,
3258 S_IRUGO, bond_proc_dir,
3259 &bond_info_fops, bond);
3260 if (bond->proc_entry == NULL) {
3261 printk(KERN_WARNING DRV_NAME
3262 ": Warning: Cannot create /proc/net/%s/%s\n",
3263 DRV_NAME, bond_dev->name);
3265 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3272 static void bond_remove_proc_entry(struct bonding *bond)
3274 if (bond_proc_dir && bond->proc_entry) {
3275 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3276 memset(bond->proc_file_name, 0, IFNAMSIZ);
3277 bond->proc_entry = NULL;
3281 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3282 * Caller must hold rtnl_lock.
3284 static void bond_create_proc_dir(void)
3286 int len = strlen(DRV_NAME);
3288 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3289 bond_proc_dir = bond_proc_dir->next) {
3290 if ((bond_proc_dir->namelen == len) &&
3291 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3296 if (!bond_proc_dir) {
3297 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3298 if (bond_proc_dir) {
3299 bond_proc_dir->owner = THIS_MODULE;
3301 printk(KERN_WARNING DRV_NAME
3302 ": Warning: cannot create /proc/net/%s\n",
3308 /* Destroy the bonding directory under /proc/net, if empty.
3309 * Caller must hold rtnl_lock.
3311 static void bond_destroy_proc_dir(void)
3313 struct proc_dir_entry *de;
3315 if (!bond_proc_dir) {
3319 /* verify that the /proc dir is empty */
3320 for (de = bond_proc_dir->subdir; de; de = de->next) {
3321 /* ignore . and .. */
3322 if (*(de->name) != '.') {
3328 if (bond_proc_dir->owner == THIS_MODULE) {
3329 bond_proc_dir->owner = NULL;
3332 remove_proc_entry(DRV_NAME, init_net.proc_net);
3333 bond_proc_dir = NULL;
3336 #endif /* CONFIG_PROC_FS */
3338 /*-------------------------- netdev event handling --------------------------*/
3341 * Change device name
3343 static int bond_event_changename(struct bonding *bond)
3345 #ifdef CONFIG_PROC_FS
3346 bond_remove_proc_entry(bond);
3347 bond_create_proc_entry(bond);
3349 down_write(&(bonding_rwsem));
3350 bond_destroy_sysfs_entry(bond);
3351 bond_create_sysfs_entry(bond);
3352 up_write(&(bonding_rwsem));
3356 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3358 struct bonding *event_bond = bond_dev->priv;
3361 case NETDEV_CHANGENAME:
3362 return bond_event_changename(event_bond);
3363 case NETDEV_UNREGISTER:
3364 bond_release_all(event_bond->dev);
3373 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3375 struct net_device *bond_dev = slave_dev->master;
3376 struct bonding *bond = bond_dev->priv;
3379 case NETDEV_UNREGISTER:
3381 if (bond->setup_by_slave)
3382 bond_release_and_destroy(bond_dev, slave_dev);
3384 bond_release(bond_dev, slave_dev);
3389 * TODO: is this what we get if somebody
3390 * sets up a hierarchical bond, then rmmod's
3391 * one of the slave bonding devices?
3396 * ... Or is it this?
3399 case NETDEV_CHANGEMTU:
3401 * TODO: Should slaves be allowed to
3402 * independently alter their MTU? For
3403 * an active-backup bond, slaves need
3404 * not be the same type of device, so
3405 * MTUs may vary. For other modes,
3406 * slaves arguably should have the
3407 * same MTUs. To do this, we'd need to
3408 * take over the slave's change_mtu
3409 * function for the duration of their
3413 case NETDEV_CHANGENAME:
3415 * TODO: handle changing the primary's name
3418 case NETDEV_FEAT_CHANGE:
3419 bond_compute_features(bond);
3429 * bond_netdev_event: handle netdev notifier chain events.
3431 * This function receives events for the netdev chain. The caller (an
3432 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3433 * locks for us to safely manipulate the slave devices (RTNL lock,
3436 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3438 struct net_device *event_dev = (struct net_device *)ptr;
3440 if (dev_net(event_dev) != &init_net)
3443 dprintk("event_dev: %s, event: %lx\n",
3444 (event_dev ? event_dev->name : "None"),
3447 if (!(event_dev->priv_flags & IFF_BONDING))
3450 if (event_dev->flags & IFF_MASTER) {
3451 dprintk("IFF_MASTER\n");
3452 return bond_master_netdev_event(event, event_dev);
3455 if (event_dev->flags & IFF_SLAVE) {
3456 dprintk("IFF_SLAVE\n");
3457 return bond_slave_netdev_event(event, event_dev);
3464 * bond_inetaddr_event: handle inetaddr notifier chain events.
3466 * We keep track of device IPs primarily to use as source addresses in
3467 * ARP monitor probes (rather than spewing out broadcasts all the time).
3469 * We track one IP for the main device (if it has one), plus one per VLAN.
3471 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3473 struct in_ifaddr *ifa = ptr;
3474 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3475 struct bonding *bond;
3476 struct vlan_entry *vlan;
3478 if (dev_net(ifa->ifa_dev->dev) != &init_net)
3481 list_for_each_entry(bond, &bond_dev_list, bond_list) {
3482 if (bond->dev == event_dev) {
3485 bond->master_ip = ifa->ifa_local;
3488 bond->master_ip = bond_glean_dev_ip(bond->dev);
3495 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3496 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3497 if (vlan_dev == event_dev) {
3500 vlan->vlan_ip = ifa->ifa_local;
3504 bond_glean_dev_ip(vlan_dev);
3515 static struct notifier_block bond_netdev_notifier = {
3516 .notifier_call = bond_netdev_event,
3519 static struct notifier_block bond_inetaddr_notifier = {
3520 .notifier_call = bond_inetaddr_event,
3523 /*-------------------------- Packet type handling ---------------------------*/
3525 /* register to receive lacpdus on a bond */
3526 static void bond_register_lacpdu(struct bonding *bond)
3528 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3530 /* initialize packet type */
3531 pk_type->type = PKT_TYPE_LACPDU;
3532 pk_type->dev = bond->dev;
3533 pk_type->func = bond_3ad_lacpdu_recv;
3535 dev_add_pack(pk_type);
3538 /* unregister to receive lacpdus on a bond */
3539 static void bond_unregister_lacpdu(struct bonding *bond)
3541 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3544 void bond_register_arp(struct bonding *bond)
3546 struct packet_type *pt = &bond->arp_mon_pt;
3551 pt->type = htons(ETH_P_ARP);
3552 pt->dev = bond->dev;
3553 pt->func = bond_arp_rcv;
3557 void bond_unregister_arp(struct bonding *bond)
3559 struct packet_type *pt = &bond->arp_mon_pt;
3561 dev_remove_pack(pt);
3565 /*---------------------------- Hashing Policies -----------------------------*/
3568 * Hash for the output device based upon layer 2 and layer 3 data. If
3569 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3571 static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3572 struct net_device *bond_dev, int count)
3574 struct ethhdr *data = (struct ethhdr *)skb->data;
3575 struct iphdr *iph = ip_hdr(skb);
3577 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3578 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3579 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3582 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3586 * Hash for the output device based upon layer 3 and layer 4 data. If
3587 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3588 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3590 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3591 struct net_device *bond_dev, int count)
3593 struct ethhdr *data = (struct ethhdr *)skb->data;
3594 struct iphdr *iph = ip_hdr(skb);
3595 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3598 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3599 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3600 (iph->protocol == IPPROTO_TCP ||
3601 iph->protocol == IPPROTO_UDP)) {
3602 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3604 return (layer4_xor ^
3605 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3609 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3613 * Hash for the output device based upon layer 2 data
3615 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3616 struct net_device *bond_dev, int count)
3618 struct ethhdr *data = (struct ethhdr *)skb->data;
3620 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3623 /*-------------------------- Device entry points ----------------------------*/
3625 static int bond_open(struct net_device *bond_dev)
3627 struct bonding *bond = bond_dev->priv;
3629 bond->kill_timers = 0;
3631 if ((bond->params.mode == BOND_MODE_TLB) ||
3632 (bond->params.mode == BOND_MODE_ALB)) {
3633 /* bond_alb_initialize must be called before the timer
3636 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3637 /* something went wrong - fail the open operation */
3641 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3642 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3645 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3646 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3647 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3650 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3651 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3652 INIT_DELAYED_WORK(&bond->arp_work,
3653 bond_activebackup_arp_mon);
3655 INIT_DELAYED_WORK(&bond->arp_work,
3656 bond_loadbalance_arp_mon);
3658 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3659 if (bond->params.arp_validate)
3660 bond_register_arp(bond);
3663 if (bond->params.mode == BOND_MODE_8023AD) {
3664 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3665 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3666 /* register to receive LACPDUs */
3667 bond_register_lacpdu(bond);
3673 static int bond_close(struct net_device *bond_dev)
3675 struct bonding *bond = bond_dev->priv;
3677 if (bond->params.mode == BOND_MODE_8023AD) {
3678 /* Unregister the receive of LACPDUs */
3679 bond_unregister_lacpdu(bond);
3682 if (bond->params.arp_validate)
3683 bond_unregister_arp(bond);
3685 write_lock_bh(&bond->lock);
3688 /* signal timers not to re-arm */
3689 bond->kill_timers = 1;
3691 write_unlock_bh(&bond->lock);
3693 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3694 cancel_delayed_work(&bond->mii_work);
3697 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3698 cancel_delayed_work(&bond->arp_work);
3701 switch (bond->params.mode) {
3702 case BOND_MODE_8023AD:
3703 cancel_delayed_work(&bond->ad_work);
3707 cancel_delayed_work(&bond->alb_work);
3714 if ((bond->params.mode == BOND_MODE_TLB) ||
3715 (bond->params.mode == BOND_MODE_ALB)) {
3716 /* Must be called only after all
3717 * slaves have been released
3719 bond_alb_deinitialize(bond);
3725 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3727 struct bonding *bond = bond_dev->priv;
3728 struct net_device_stats *stats = &(bond->stats), *sstats;
3729 struct net_device_stats local_stats;
3730 struct slave *slave;
3733 memset(&local_stats, 0, sizeof(struct net_device_stats));
3735 read_lock_bh(&bond->lock);
3737 bond_for_each_slave(bond, slave, i) {
3738 sstats = slave->dev->get_stats(slave->dev);
3739 local_stats.rx_packets += sstats->rx_packets;
3740 local_stats.rx_bytes += sstats->rx_bytes;
3741 local_stats.rx_errors += sstats->rx_errors;
3742 local_stats.rx_dropped += sstats->rx_dropped;
3744 local_stats.tx_packets += sstats->tx_packets;
3745 local_stats.tx_bytes += sstats->tx_bytes;
3746 local_stats.tx_errors += sstats->tx_errors;
3747 local_stats.tx_dropped += sstats->tx_dropped;
3749 local_stats.multicast += sstats->multicast;
3750 local_stats.collisions += sstats->collisions;
3752 local_stats.rx_length_errors += sstats->rx_length_errors;
3753 local_stats.rx_over_errors += sstats->rx_over_errors;
3754 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3755 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3756 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3757 local_stats.rx_missed_errors += sstats->rx_missed_errors;
3759 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3760 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3761 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3762 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3763 local_stats.tx_window_errors += sstats->tx_window_errors;
3766 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3768 read_unlock_bh(&bond->lock);
3773 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3775 struct net_device *slave_dev = NULL;
3776 struct ifbond k_binfo;
3777 struct ifbond __user *u_binfo = NULL;
3778 struct ifslave k_sinfo;
3779 struct ifslave __user *u_sinfo = NULL;
3780 struct mii_ioctl_data *mii = NULL;
3783 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3784 bond_dev->name, cmd);
3796 * We do this again just in case we were called by SIOCGMIIREG
3797 * instead of SIOCGMIIPHY.
3804 if (mii->reg_num == 1) {
3805 struct bonding *bond = bond_dev->priv;
3807 read_lock(&bond->lock);
3808 read_lock(&bond->curr_slave_lock);
3809 if (netif_carrier_ok(bond->dev)) {
3810 mii->val_out = BMSR_LSTATUS;
3812 read_unlock(&bond->curr_slave_lock);
3813 read_unlock(&bond->lock);
3817 case BOND_INFO_QUERY_OLD:
3818 case SIOCBONDINFOQUERY:
3819 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3821 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3825 res = bond_info_query(bond_dev, &k_binfo);
3827 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3833 case BOND_SLAVE_INFO_QUERY_OLD:
3834 case SIOCBONDSLAVEINFOQUERY:
3835 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3837 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3841 res = bond_slave_info_query(bond_dev, &k_sinfo);
3843 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3854 if (!capable(CAP_NET_ADMIN)) {
3858 down_write(&(bonding_rwsem));
3859 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
3861 dprintk("slave_dev=%p: \n", slave_dev);
3866 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3868 case BOND_ENSLAVE_OLD:
3869 case SIOCBONDENSLAVE:
3870 res = bond_enslave(bond_dev, slave_dev);
3872 case BOND_RELEASE_OLD:
3873 case SIOCBONDRELEASE:
3874 res = bond_release(bond_dev, slave_dev);
3876 case BOND_SETHWADDR_OLD:
3877 case SIOCBONDSETHWADDR:
3878 res = bond_sethwaddr(bond_dev, slave_dev);
3880 case BOND_CHANGE_ACTIVE_OLD:
3881 case SIOCBONDCHANGEACTIVE:
3882 res = bond_ioctl_change_active(bond_dev, slave_dev);
3891 up_write(&(bonding_rwsem));
3895 static void bond_set_multicast_list(struct net_device *bond_dev)
3897 struct bonding *bond = bond_dev->priv;
3898 struct dev_mc_list *dmi;
3901 * Do promisc before checking multicast_mode
3903 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3904 bond_set_promiscuity(bond, 1);
3907 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3908 bond_set_promiscuity(bond, -1);
3911 /* set allmulti flag to slaves */
3912 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3913 bond_set_allmulti(bond, 1);
3916 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3917 bond_set_allmulti(bond, -1);
3920 read_lock(&bond->lock);
3922 bond->flags = bond_dev->flags;
3924 /* looking for addresses to add to slaves' mc list */
3925 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3926 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3927 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3931 /* looking for addresses to delete from slaves' list */
3932 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3933 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3934 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3938 /* save master's multicast list */
3939 bond_mc_list_destroy(bond);
3940 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3942 read_unlock(&bond->lock);
3946 * Change the MTU of all of a master's slaves to match the master
3948 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3950 struct bonding *bond = bond_dev->priv;
3951 struct slave *slave, *stop_at;
3955 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3956 (bond_dev ? bond_dev->name : "None"), new_mtu);
3958 /* Can't hold bond->lock with bh disabled here since
3959 * some base drivers panic. On the other hand we can't
3960 * hold bond->lock without bh disabled because we'll
3961 * deadlock. The only solution is to rely on the fact
3962 * that we're under rtnl_lock here, and the slaves
3963 * list won't change. This doesn't solve the problem
3964 * of setting the slave's MTU while it is
3965 * transmitting, but the assumption is that the base
3966 * driver can handle that.
3968 * TODO: figure out a way to safely iterate the slaves
3969 * list, but without holding a lock around the actual
3970 * call to the base driver.
3973 bond_for_each_slave(bond, slave, i) {
3974 dprintk("s %p s->p %p c_m %p\n", slave,
3975 slave->prev, slave->dev->change_mtu);
3977 res = dev_set_mtu(slave->dev, new_mtu);
3980 /* If we failed to set the slave's mtu to the new value
3981 * we must abort the operation even in ACTIVE_BACKUP
3982 * mode, because if we allow the backup slaves to have
3983 * different mtu values than the active slave we'll
3984 * need to change their mtu when doing a failover. That
3985 * means changing their mtu from timer context, which
3986 * is probably not a good idea.
3988 dprintk("err %d %s\n", res, slave->dev->name);
3993 bond_dev->mtu = new_mtu;
3998 /* unwind from head to the slave that failed */
4000 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4003 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4005 dprintk("unwind err %d dev %s\n", tmp_res,
4016 * Note that many devices must be down to change the HW address, and
4017 * downing the master releases all slaves. We can make bonds full of
4018 * bonding devices to test this, however.
4020 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4022 struct bonding *bond = bond_dev->priv;
4023 struct sockaddr *sa = addr, tmp_sa;
4024 struct slave *slave, *stop_at;
4028 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4031 * If fail_over_mac is enabled, do nothing and return success.
4032 * Returning an error causes ifenslave to fail.
4034 if (bond->params.fail_over_mac)
4037 if (!is_valid_ether_addr(sa->sa_data)) {
4038 return -EADDRNOTAVAIL;
4041 /* Can't hold bond->lock with bh disabled here since
4042 * some base drivers panic. On the other hand we can't
4043 * hold bond->lock without bh disabled because we'll
4044 * deadlock. The only solution is to rely on the fact
4045 * that we're under rtnl_lock here, and the slaves
4046 * list won't change. This doesn't solve the problem
4047 * of setting the slave's hw address while it is
4048 * transmitting, but the assumption is that the base
4049 * driver can handle that.
4051 * TODO: figure out a way to safely iterate the slaves
4052 * list, but without holding a lock around the actual
4053 * call to the base driver.
4056 bond_for_each_slave(bond, slave, i) {
4057 dprintk("slave %p %s\n", slave, slave->dev->name);
4059 if (slave->dev->set_mac_address == NULL) {
4061 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4065 res = dev_set_mac_address(slave->dev, addr);
4067 /* TODO: consider downing the slave
4069 * User should expect communications
4070 * breakage anyway until ARP finish
4073 dprintk("err %d %s\n", res, slave->dev->name);
4079 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4083 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4084 tmp_sa.sa_family = bond_dev->type;
4086 /* unwind from head to the slave that failed */
4088 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4091 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4093 dprintk("unwind err %d dev %s\n", tmp_res,
4101 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4103 struct bonding *bond = bond_dev->priv;
4104 struct slave *slave, *start_at;
4105 int i, slave_no, res = 1;
4107 read_lock(&bond->lock);
4109 if (!BOND_IS_OK(bond)) {
4114 * Concurrent TX may collide on rr_tx_counter; we accept that
4115 * as being rare enough not to justify using an atomic op here
4117 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4119 bond_for_each_slave(bond, slave, i) {
4127 bond_for_each_slave_from(bond, slave, i, start_at) {
4128 if (IS_UP(slave->dev) &&
4129 (slave->link == BOND_LINK_UP) &&
4130 (slave->state == BOND_STATE_ACTIVE)) {
4131 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4138 /* no suitable interface, frame not sent */
4141 read_unlock(&bond->lock);
4147 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4148 * the bond has a usable interface.
4150 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4152 struct bonding *bond = bond_dev->priv;
4155 read_lock(&bond->lock);
4156 read_lock(&bond->curr_slave_lock);
4158 if (!BOND_IS_OK(bond)) {
4162 if (!bond->curr_active_slave)
4165 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4169 /* no suitable interface, frame not sent */
4172 read_unlock(&bond->curr_slave_lock);
4173 read_unlock(&bond->lock);
4178 * In bond_xmit_xor() , we determine the output device by using a pre-
4179 * determined xmit_hash_policy(), If the selected device is not enabled,
4180 * find the next active slave.
4182 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4184 struct bonding *bond = bond_dev->priv;
4185 struct slave *slave, *start_at;
4190 read_lock(&bond->lock);
4192 if (!BOND_IS_OK(bond)) {
4196 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4198 bond_for_each_slave(bond, slave, i) {
4207 bond_for_each_slave_from(bond, slave, i, start_at) {
4208 if (IS_UP(slave->dev) &&
4209 (slave->link == BOND_LINK_UP) &&
4210 (slave->state == BOND_STATE_ACTIVE)) {
4211 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4218 /* no suitable interface, frame not sent */
4221 read_unlock(&bond->lock);
4226 * in broadcast mode, we send everything to all usable interfaces.
4228 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4230 struct bonding *bond = bond_dev->priv;
4231 struct slave *slave, *start_at;
4232 struct net_device *tx_dev = NULL;
4236 read_lock(&bond->lock);
4238 if (!BOND_IS_OK(bond)) {
4242 read_lock(&bond->curr_slave_lock);
4243 start_at = bond->curr_active_slave;
4244 read_unlock(&bond->curr_slave_lock);
4250 bond_for_each_slave_from(bond, slave, i, start_at) {
4251 if (IS_UP(slave->dev) &&
4252 (slave->link == BOND_LINK_UP) &&
4253 (slave->state == BOND_STATE_ACTIVE)) {
4255 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4257 printk(KERN_ERR DRV_NAME
4258 ": %s: Error: bond_xmit_broadcast(): "
4259 "skb_clone() failed\n",
4264 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4266 dev_kfree_skb(skb2);
4270 tx_dev = slave->dev;
4275 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4280 /* no suitable interface, frame not sent */
4283 /* frame sent to all suitable interfaces */
4284 read_unlock(&bond->lock);
4288 /*------------------------- Device initialization ---------------------------*/
4290 static void bond_set_xmit_hash_policy(struct bonding *bond)
4292 switch (bond->params.xmit_policy) {
4293 case BOND_XMIT_POLICY_LAYER23:
4294 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4296 case BOND_XMIT_POLICY_LAYER34:
4297 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4299 case BOND_XMIT_POLICY_LAYER2:
4301 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4307 * set bond mode specific net device operations
4309 void bond_set_mode_ops(struct bonding *bond, int mode)
4311 struct net_device *bond_dev = bond->dev;
4314 case BOND_MODE_ROUNDROBIN:
4315 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4317 case BOND_MODE_ACTIVEBACKUP:
4318 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4321 bond_dev->hard_start_xmit = bond_xmit_xor;
4322 bond_set_xmit_hash_policy(bond);
4324 case BOND_MODE_BROADCAST:
4325 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4327 case BOND_MODE_8023AD:
4328 bond_set_master_3ad_flags(bond);
4329 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4330 bond_set_xmit_hash_policy(bond);
4333 bond_set_master_alb_flags(bond);
4336 bond_dev->hard_start_xmit = bond_alb_xmit;
4337 bond_dev->set_mac_address = bond_alb_set_mac_address;
4340 /* Should never happen, mode already checked */
4341 printk(KERN_ERR DRV_NAME
4342 ": %s: Error: Unknown bonding mode %d\n",
4349 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4350 struct ethtool_drvinfo *drvinfo)
4352 strncpy(drvinfo->driver, DRV_NAME, 32);
4353 strncpy(drvinfo->version, DRV_VERSION, 32);
4354 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4357 static const struct ethtool_ops bond_ethtool_ops = {
4358 .get_drvinfo = bond_ethtool_get_drvinfo,
4362 * Does not allocate but creates a /proc entry.
4365 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4367 struct bonding *bond = bond_dev->priv;
4369 dprintk("Begin bond_init for %s\n", bond_dev->name);
4371 /* initialize rwlocks */
4372 rwlock_init(&bond->lock);
4373 rwlock_init(&bond->curr_slave_lock);
4375 bond->params = *params; /* copy params struct */
4377 bond->wq = create_singlethread_workqueue(bond_dev->name);
4381 /* Initialize pointers */
4382 bond->first_slave = NULL;
4383 bond->curr_active_slave = NULL;
4384 bond->current_arp_slave = NULL;
4385 bond->primary_slave = NULL;
4386 bond->dev = bond_dev;
4387 bond->send_grat_arp = 0;
4388 bond->setup_by_slave = 0;
4389 INIT_LIST_HEAD(&bond->vlan_list);
4391 /* Initialize the device entry points */
4392 bond_dev->open = bond_open;
4393 bond_dev->stop = bond_close;
4394 bond_dev->get_stats = bond_get_stats;
4395 bond_dev->do_ioctl = bond_do_ioctl;
4396 bond_dev->ethtool_ops = &bond_ethtool_ops;
4397 bond_dev->set_multicast_list = bond_set_multicast_list;
4398 bond_dev->change_mtu = bond_change_mtu;
4399 bond_dev->set_mac_address = bond_set_mac_address;
4400 bond_dev->validate_addr = NULL;
4402 bond_set_mode_ops(bond, bond->params.mode);
4404 bond_dev->destructor = free_netdev;
4406 /* Initialize the device options */
4407 bond_dev->tx_queue_len = 0;
4408 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4409 bond_dev->priv_flags |= IFF_BONDING;
4411 /* At first, we block adding VLANs. That's the only way to
4412 * prevent problems that occur when adding VLANs over an
4413 * empty bond. The block will be removed once non-challenged
4414 * slaves are enslaved.
4416 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4418 /* don't acquire bond device's netif_tx_lock when
4420 bond_dev->features |= NETIF_F_LLTX;
4422 /* By default, we declare the bond to be fully
4423 * VLAN hardware accelerated capable. Special
4424 * care is taken in the various xmit functions
4425 * when there are slaves that are not hw accel
4428 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4429 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4430 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4431 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4432 NETIF_F_HW_VLAN_RX |
4433 NETIF_F_HW_VLAN_FILTER);
4435 #ifdef CONFIG_PROC_FS
4436 bond_create_proc_entry(bond);
4438 list_add_tail(&bond->bond_list, &bond_dev_list);
4443 /* De-initialize device specific data.
4444 * Caller must hold rtnl_lock.
4446 static void bond_deinit(struct net_device *bond_dev)
4448 struct bonding *bond = bond_dev->priv;
4450 list_del(&bond->bond_list);
4452 #ifdef CONFIG_PROC_FS
4453 bond_remove_proc_entry(bond);
4457 static void bond_work_cancel_all(struct bonding *bond)
4459 write_lock_bh(&bond->lock);
4460 bond->kill_timers = 1;
4461 write_unlock_bh(&bond->lock);
4463 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4464 cancel_delayed_work(&bond->mii_work);
4466 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4467 cancel_delayed_work(&bond->arp_work);
4469 if (bond->params.mode == BOND_MODE_ALB &&
4470 delayed_work_pending(&bond->alb_work))
4471 cancel_delayed_work(&bond->alb_work);
4473 if (bond->params.mode == BOND_MODE_8023AD &&
4474 delayed_work_pending(&bond->ad_work))
4475 cancel_delayed_work(&bond->ad_work);
4478 /* Unregister and free all bond devices.
4479 * Caller must hold rtnl_lock.
4481 static void bond_free_all(void)
4483 struct bonding *bond, *nxt;
4485 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4486 struct net_device *bond_dev = bond->dev;
4488 bond_work_cancel_all(bond);
4489 netif_tx_lock_bh(bond_dev);
4490 bond_mc_list_destroy(bond);
4491 netif_tx_unlock_bh(bond_dev);
4492 /* Release the bonded slaves */
4493 bond_release_all(bond_dev);
4497 #ifdef CONFIG_PROC_FS
4498 bond_destroy_proc_dir();
4502 /*------------------------- Module initialization ---------------------------*/
4505 * Convert string input module parms. Accept either the
4506 * number of the mode or its string name. A bit complicated because
4507 * some mode names are substrings of other names, and calls from sysfs
4508 * may have whitespace in the name (trailing newlines, for example).
4510 int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
4512 int mode = -1, i, rv;
4513 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4515 for (p = (char *)buf; *p; p++)
4516 if (!(isdigit(*p) || isspace(*p)))
4520 rv = sscanf(buf, "%20s", modestr);
4522 rv = sscanf(buf, "%d", &mode);
4527 for (i = 0; tbl[i].modename; i++) {
4528 if (mode == tbl[i].mode)
4530 if (strcmp(modestr, tbl[i].modename) == 0)
4537 static int bond_check_params(struct bond_params *params)
4539 int arp_validate_value;
4542 * Convert string parameters.
4545 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4546 if (bond_mode == -1) {
4547 printk(KERN_ERR DRV_NAME
4548 ": Error: Invalid bonding mode \"%s\"\n",
4549 mode == NULL ? "NULL" : mode);
4554 if (xmit_hash_policy) {
4555 if ((bond_mode != BOND_MODE_XOR) &&
4556 (bond_mode != BOND_MODE_8023AD)) {
4557 printk(KERN_INFO DRV_NAME
4558 ": xor_mode param is irrelevant in mode %s\n",
4559 bond_mode_name(bond_mode));
4561 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4563 if (xmit_hashtype == -1) {
4564 printk(KERN_ERR DRV_NAME
4565 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4566 xmit_hash_policy == NULL ? "NULL" :
4574 if (bond_mode != BOND_MODE_8023AD) {
4575 printk(KERN_INFO DRV_NAME
4576 ": lacp_rate param is irrelevant in mode %s\n",
4577 bond_mode_name(bond_mode));
4579 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4580 if (lacp_fast == -1) {
4581 printk(KERN_ERR DRV_NAME
4582 ": Error: Invalid lacp rate \"%s\"\n",
4583 lacp_rate == NULL ? "NULL" : lacp_rate);
4589 if (max_bonds < 1 || max_bonds > INT_MAX) {
4590 printk(KERN_WARNING DRV_NAME
4591 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4592 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4593 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4594 max_bonds = BOND_DEFAULT_MAX_BONDS;
4598 printk(KERN_WARNING DRV_NAME
4599 ": Warning: miimon module parameter (%d), "
4600 "not in range 0-%d, so it was reset to %d\n",
4601 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4602 miimon = BOND_LINK_MON_INTERV;
4606 printk(KERN_WARNING DRV_NAME
4607 ": Warning: updelay module parameter (%d), "
4608 "not in range 0-%d, so it was reset to 0\n",
4613 if (downdelay < 0) {
4614 printk(KERN_WARNING DRV_NAME
4615 ": Warning: downdelay module parameter (%d), "
4616 "not in range 0-%d, so it was reset to 0\n",
4617 downdelay, INT_MAX);
4621 if ((use_carrier != 0) && (use_carrier != 1)) {
4622 printk(KERN_WARNING DRV_NAME
4623 ": Warning: use_carrier module parameter (%d), "
4624 "not of valid value (0/1), so it was set to 1\n",
4629 /* reset values for 802.3ad */
4630 if (bond_mode == BOND_MODE_8023AD) {
4632 printk(KERN_WARNING DRV_NAME
4633 ": Warning: miimon must be specified, "
4634 "otherwise bonding will not detect link "
4635 "failure, speed and duplex which are "
4636 "essential for 802.3ad operation\n");
4637 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4642 /* reset values for TLB/ALB */
4643 if ((bond_mode == BOND_MODE_TLB) ||
4644 (bond_mode == BOND_MODE_ALB)) {
4646 printk(KERN_WARNING DRV_NAME
4647 ": Warning: miimon must be specified, "
4648 "otherwise bonding will not detect link "
4649 "failure and link speed which are essential "
4650 "for TLB/ALB load balancing\n");
4651 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4656 if (bond_mode == BOND_MODE_ALB) {
4657 printk(KERN_NOTICE DRV_NAME
4658 ": In ALB mode you might experience client "
4659 "disconnections upon reconnection of a link if the "
4660 "bonding module updelay parameter (%d msec) is "
4661 "incompatible with the forwarding delay time of the "
4667 if (updelay || downdelay) {
4668 /* just warn the user the up/down delay will have
4669 * no effect since miimon is zero...
4671 printk(KERN_WARNING DRV_NAME
4672 ": Warning: miimon module parameter not set "
4673 "and updelay (%d) or downdelay (%d) module "
4674 "parameter is set; updelay and downdelay have "
4675 "no effect unless miimon is set\n",
4676 updelay, downdelay);
4679 /* don't allow arp monitoring */
4681 printk(KERN_WARNING DRV_NAME
4682 ": Warning: miimon (%d) and arp_interval (%d) "
4683 "can't be used simultaneously, disabling ARP "
4685 miimon, arp_interval);
4689 if ((updelay % miimon) != 0) {
4690 printk(KERN_WARNING DRV_NAME
4691 ": Warning: updelay (%d) is not a multiple "
4692 "of miimon (%d), updelay rounded to %d ms\n",
4693 updelay, miimon, (updelay / miimon) * miimon);
4698 if ((downdelay % miimon) != 0) {
4699 printk(KERN_WARNING DRV_NAME
4700 ": Warning: downdelay (%d) is not a multiple "
4701 "of miimon (%d), downdelay rounded to %d ms\n",
4703 (downdelay / miimon) * miimon);
4706 downdelay /= miimon;
4709 if (arp_interval < 0) {
4710 printk(KERN_WARNING DRV_NAME
4711 ": Warning: arp_interval module parameter (%d) "
4712 ", not in range 0-%d, so it was reset to %d\n",
4713 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4714 arp_interval = BOND_LINK_ARP_INTERV;
4717 for (arp_ip_count = 0;
4718 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4720 /* not complete check, but should be good enough to
4722 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4723 printk(KERN_WARNING DRV_NAME
4724 ": Warning: bad arp_ip_target module parameter "
4725 "(%s), ARP monitoring will not be performed\n",
4726 arp_ip_target[arp_ip_count]);
4729 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4730 arp_target[arp_ip_count] = ip;
4734 if (arp_interval && !arp_ip_count) {
4735 /* don't allow arping if no arp_ip_target given... */
4736 printk(KERN_WARNING DRV_NAME
4737 ": Warning: arp_interval module parameter (%d) "
4738 "specified without providing an arp_ip_target "
4739 "parameter, arp_interval was reset to 0\n",
4745 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4746 printk(KERN_ERR DRV_NAME
4747 ": arp_validate only supported in active-backup mode\n");
4750 if (!arp_interval) {
4751 printk(KERN_ERR DRV_NAME
4752 ": arp_validate requires arp_interval\n");
4756 arp_validate_value = bond_parse_parm(arp_validate,
4758 if (arp_validate_value == -1) {
4759 printk(KERN_ERR DRV_NAME
4760 ": Error: invalid arp_validate \"%s\"\n",
4761 arp_validate == NULL ? "NULL" : arp_validate);
4765 arp_validate_value = 0;
4768 printk(KERN_INFO DRV_NAME
4769 ": MII link monitoring set to %d ms\n",
4771 } else if (arp_interval) {
4774 printk(KERN_INFO DRV_NAME
4775 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4777 arp_validate_tbl[arp_validate_value].modename,
4780 for (i = 0; i < arp_ip_count; i++)
4781 printk (" %s", arp_ip_target[i]);
4786 /* miimon and arp_interval not set, we need one so things
4787 * work as expected, see bonding.txt for details
4789 printk(KERN_WARNING DRV_NAME
4790 ": Warning: either miimon or arp_interval and "
4791 "arp_ip_target module parameters must be specified, "
4792 "otherwise bonding will not detect link failures! see "
4793 "bonding.txt for details.\n");
4796 if (primary && !USES_PRIMARY(bond_mode)) {
4797 /* currently, using a primary only makes sense
4798 * in active backup, TLB or ALB modes
4800 printk(KERN_WARNING DRV_NAME
4801 ": Warning: %s primary device specified but has no "
4802 "effect in %s mode\n",
4803 primary, bond_mode_name(bond_mode));
4807 if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
4808 printk(KERN_WARNING DRV_NAME
4809 ": Warning: fail_over_mac only affects "
4810 "active-backup mode.\n");
4812 /* fill params struct with the proper values */
4813 params->mode = bond_mode;
4814 params->xmit_policy = xmit_hashtype;
4815 params->miimon = miimon;
4816 params->arp_interval = arp_interval;
4817 params->arp_validate = arp_validate_value;
4818 params->updelay = updelay;
4819 params->downdelay = downdelay;
4820 params->use_carrier = use_carrier;
4821 params->lacp_fast = lacp_fast;
4822 params->primary[0] = 0;
4823 params->fail_over_mac = fail_over_mac;
4826 strncpy(params->primary, primary, IFNAMSIZ);
4827 params->primary[IFNAMSIZ - 1] = 0;
4830 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4835 static struct lock_class_key bonding_netdev_xmit_lock_key;
4837 /* Create a new bond based on the specified name and bonding parameters.
4838 * If name is NULL, obtain a suitable "bond%d" name for us.
4839 * Caller must NOT hold rtnl_lock; we need to release it here before we
4840 * set up our sysfs entries.
4842 int bond_create(char *name, struct bond_params *params)
4844 struct net_device *bond_dev;
4845 struct bonding *bond;
4849 down_write(&bonding_rwsem);
4851 /* Check to see if the bond already exists. */
4853 list_for_each_entry(bond, &bond_dev_list, bond_list)
4854 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
4855 printk(KERN_ERR DRV_NAME
4856 ": cannot add bond %s; it already exists\n",
4863 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4866 printk(KERN_ERR DRV_NAME
4867 ": %s: eek! can't alloc netdev!\n",
4874 res = dev_alloc_name(bond_dev, "bond%d");
4879 /* bond_init() must be called after dev_alloc_name() (for the
4880 * /proc files), but before register_netdevice(), because we
4881 * need to set function pointers.
4884 res = bond_init(bond_dev, params);
4889 res = register_netdevice(bond_dev);
4894 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4896 netif_carrier_off(bond_dev);
4898 up_write(&bonding_rwsem);
4899 rtnl_unlock(); /* allows sysfs registration of net device */
4900 res = bond_create_sysfs_entry(bond_dev->priv);
4903 down_write(&bonding_rwsem);
4904 bond_deinit(bond_dev);
4905 unregister_netdevice(bond_dev);
4912 bond_deinit(bond_dev);
4914 free_netdev(bond_dev);
4916 up_write(&bonding_rwsem);
4921 static int __init bonding_init(void)
4925 struct bonding *bond;
4927 printk(KERN_INFO "%s", version);
4929 res = bond_check_params(&bonding_defaults);
4934 #ifdef CONFIG_PROC_FS
4935 bond_create_proc_dir();
4938 init_rwsem(&bonding_rwsem);
4940 for (i = 0; i < max_bonds; i++) {
4941 res = bond_create(NULL, &bonding_defaults);
4946 res = bond_create_sysfs();
4950 register_netdevice_notifier(&bond_netdev_notifier);
4951 register_inetaddr_notifier(&bond_inetaddr_notifier);
4955 list_for_each_entry(bond, &bond_dev_list, bond_list) {
4956 bond_work_cancel_all(bond);
4957 destroy_workqueue(bond->wq);
4960 bond_destroy_sysfs();
4970 static void __exit bonding_exit(void)
4972 unregister_netdevice_notifier(&bond_netdev_notifier);
4973 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4975 bond_destroy_sysfs();
4982 module_init(bonding_init);
4983 module_exit(bonding_exit);
4984 MODULE_LICENSE("GPL");
4985 MODULE_VERSION(DRV_VERSION);
4986 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4987 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4988 MODULE_SUPPORTED_DEVICE("most ethernet devices");