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/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/ioport.h>
47 #include <linux/tcp.h>
48 #include <linux/udp.h>
49 #include <linux/slab.h>
50 #include <linux/string.h>
51 #include <linux/init.h>
52 #include <linux/timer.h>
53 #include <linux/socket.h>
54 #include <linux/ctype.h>
55 #include <linux/inet.h>
56 #include <linux/bitops.h>
57 #include <asm/system.h>
60 #include <asm/uaccess.h>
61 #include <linux/errno.h>
62 #include <linux/netdevice.h>
63 #include <linux/inetdevice.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 <net/route.h>
82 /*---------------------------- Module parameters ----------------------------*/
84 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
85 #define BOND_LINK_MON_INTERV 0
86 #define BOND_LINK_ARP_INTERV 0
88 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
89 static int miimon = BOND_LINK_MON_INTERV;
90 static int updelay = 0;
91 static int downdelay = 0;
92 static int use_carrier = 1;
93 static char *mode = NULL;
94 static char *primary = NULL;
95 static char *lacp_rate = NULL;
96 static char *xmit_hash_policy = NULL;
97 static int arp_interval = BOND_LINK_ARP_INTERV;
98 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
99 struct bond_params bonding_defaults;
101 module_param(max_bonds, int, 0);
102 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
103 module_param(miimon, int, 0);
104 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
105 module_param(updelay, int, 0);
106 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
107 module_param(downdelay, int, 0);
108 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
110 module_param(use_carrier, int, 0);
111 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
112 "0 for off, 1 for on (default)");
113 module_param(mode, charp, 0);
114 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
115 "1 for active-backup, 2 for balance-xor, "
116 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
117 "6 for balance-alb");
118 module_param(primary, charp, 0);
119 MODULE_PARM_DESC(primary, "Primary network device to use");
120 module_param(lacp_rate, charp, 0);
121 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
123 module_param(xmit_hash_policy, charp, 0);
124 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
125 ", 1 for layer 3+4");
126 module_param(arp_interval, int, 0);
127 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
128 module_param_array(arp_ip_target, charp, NULL, 0);
129 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
131 /*----------------------------- Global variables ----------------------------*/
133 static const char * const version =
134 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
136 LIST_HEAD(bond_dev_list);
138 #ifdef CONFIG_PROC_FS
139 static struct proc_dir_entry *bond_proc_dir = NULL;
142 extern struct rw_semaphore bonding_rwsem;
143 static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
144 static int arp_ip_count = 0;
145 static int bond_mode = BOND_MODE_ROUNDROBIN;
146 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
147 static int lacp_fast = 0;
150 struct bond_parm_tbl bond_lacp_tbl[] = {
151 { "slow", AD_LACP_SLOW},
152 { "fast", AD_LACP_FAST},
156 struct bond_parm_tbl bond_mode_tbl[] = {
157 { "balance-rr", BOND_MODE_ROUNDROBIN},
158 { "active-backup", BOND_MODE_ACTIVEBACKUP},
159 { "balance-xor", BOND_MODE_XOR},
160 { "broadcast", BOND_MODE_BROADCAST},
161 { "802.3ad", BOND_MODE_8023AD},
162 { "balance-tlb", BOND_MODE_TLB},
163 { "balance-alb", BOND_MODE_ALB},
167 struct bond_parm_tbl xmit_hashtype_tbl[] = {
168 { "layer2", BOND_XMIT_POLICY_LAYER2},
169 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
173 /*-------------------------- Forward declarations ---------------------------*/
175 static void bond_send_gratuitous_arp(struct bonding *bond);
177 /*---------------------------- General routines -----------------------------*/
179 const char *bond_mode_name(int mode)
182 case BOND_MODE_ROUNDROBIN :
183 return "load balancing (round-robin)";
184 case BOND_MODE_ACTIVEBACKUP :
185 return "fault-tolerance (active-backup)";
187 return "load balancing (xor)";
188 case BOND_MODE_BROADCAST :
189 return "fault-tolerance (broadcast)";
190 case BOND_MODE_8023AD:
191 return "IEEE 802.3ad Dynamic link aggregation";
193 return "transmit load balancing";
195 return "adaptive load balancing";
201 /*---------------------------------- VLAN -----------------------------------*/
204 * bond_add_vlan - add a new vlan id on bond
205 * @bond: bond that got the notification
206 * @vlan_id: the vlan id to add
208 * Returns -ENOMEM if allocation failed.
210 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
212 struct vlan_entry *vlan;
214 dprintk("bond: %s, vlan id %d\n",
215 (bond ? bond->dev->name: "None"), vlan_id);
217 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
222 INIT_LIST_HEAD(&vlan->vlan_list);
223 vlan->vlan_id = vlan_id;
226 write_lock_bh(&bond->lock);
228 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
230 write_unlock_bh(&bond->lock);
232 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
238 * bond_del_vlan - delete a vlan id from bond
239 * @bond: bond that got the notification
240 * @vlan_id: the vlan id to delete
242 * returns -ENODEV if @vlan_id was not found in @bond.
244 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
246 struct vlan_entry *vlan, *next;
249 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
251 write_lock_bh(&bond->lock);
253 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
254 if (vlan->vlan_id == vlan_id) {
255 list_del(&vlan->vlan_list);
257 if ((bond->params.mode == BOND_MODE_TLB) ||
258 (bond->params.mode == BOND_MODE_ALB)) {
259 bond_alb_clear_vlan(bond, vlan_id);
262 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
267 if (list_empty(&bond->vlan_list) &&
268 (bond->slave_cnt == 0)) {
269 /* Last VLAN removed and no slaves, so
270 * restore block on adding VLANs. This will
271 * be removed once new slaves that are not
272 * VLAN challenged will be added.
274 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
282 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
286 write_unlock_bh(&bond->lock);
291 * bond_has_challenged_slaves
292 * @bond: the bond we're working on
294 * Searches the slave list. Returns 1 if a vlan challenged slave
295 * was found, 0 otherwise.
297 * Assumes bond->lock is held.
299 static int bond_has_challenged_slaves(struct bonding *bond)
304 bond_for_each_slave(bond, slave, i) {
305 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
306 dprintk("found VLAN challenged slave - %s\n",
312 dprintk("no VLAN challenged slaves found\n");
317 * bond_next_vlan - safely skip to the next item in the vlans list.
318 * @bond: the bond we're working on
319 * @curr: item we're advancing from
321 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
322 * or @curr->next otherwise (even if it is @curr itself again).
324 * Caller must hold bond->lock
326 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
328 struct vlan_entry *next, *last;
330 if (list_empty(&bond->vlan_list)) {
335 next = list_entry(bond->vlan_list.next,
336 struct vlan_entry, vlan_list);
338 last = list_entry(bond->vlan_list.prev,
339 struct vlan_entry, vlan_list);
341 next = list_entry(bond->vlan_list.next,
342 struct vlan_entry, vlan_list);
344 next = list_entry(curr->vlan_list.next,
345 struct vlan_entry, vlan_list);
353 * bond_dev_queue_xmit - Prepare skb for xmit.
355 * @bond: bond device that got this skb for tx.
356 * @skb: hw accel VLAN tagged skb to transmit
357 * @slave_dev: slave that is supposed to xmit this skbuff
359 * When the bond gets an skb to transmit that is
360 * already hardware accelerated VLAN tagged, and it
361 * needs to relay this skb to a slave that is not
362 * hw accel capable, the skb needs to be "unaccelerated",
363 * i.e. strip the hwaccel tag and re-insert it as part
366 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
368 unsigned short vlan_id;
370 if (!list_empty(&bond->vlan_list) &&
371 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
372 vlan_get_tag(skb, &vlan_id) == 0) {
373 skb->dev = slave_dev;
374 skb = vlan_put_tag(skb, vlan_id);
376 /* vlan_put_tag() frees the skb in case of error,
377 * so return success here so the calling functions
378 * won't attempt to free is again.
383 skb->dev = slave_dev;
393 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
394 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
396 * a. This operation is performed in IOCTL context,
397 * b. The operation is protected by the RTNL semaphore in the 8021q code,
398 * c. Holding a lock with BH disabled while directly calling a base driver
399 * entry point is generally a BAD idea.
401 * The design of synchronization/protection for this operation in the 8021q
402 * module is good for one or more VLAN devices over a single physical device
403 * and cannot be extended for a teaming solution like bonding, so there is a
404 * potential race condition here where a net device from the vlan group might
405 * be referenced (either by a base driver or the 8021q code) while it is being
406 * removed from the system. However, it turns out we're not making matters
407 * worse, and if it works for regular VLAN usage it will work here too.
411 * bond_vlan_rx_register - Propagates registration to slaves
412 * @bond_dev: bonding net device that got called
413 * @grp: vlan group being registered
415 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
417 struct bonding *bond = bond_dev->priv;
423 bond_for_each_slave(bond, slave, i) {
424 struct net_device *slave_dev = slave->dev;
426 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
427 slave_dev->vlan_rx_register) {
428 slave_dev->vlan_rx_register(slave_dev, grp);
434 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
435 * @bond_dev: bonding net device that got called
436 * @vid: vlan id being added
438 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
440 struct bonding *bond = bond_dev->priv;
444 bond_for_each_slave(bond, slave, i) {
445 struct net_device *slave_dev = slave->dev;
447 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
448 slave_dev->vlan_rx_add_vid) {
449 slave_dev->vlan_rx_add_vid(slave_dev, vid);
453 res = bond_add_vlan(bond, vid);
455 printk(KERN_ERR DRV_NAME
456 ": %s: Error: Failed to add vlan id %d\n",
457 bond_dev->name, vid);
462 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
463 * @bond_dev: bonding net device that got called
464 * @vid: vlan id being removed
466 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
468 struct bonding *bond = bond_dev->priv;
470 struct net_device *vlan_dev;
473 bond_for_each_slave(bond, slave, i) {
474 struct net_device *slave_dev = slave->dev;
476 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
477 slave_dev->vlan_rx_kill_vid) {
478 /* Save and then restore vlan_dev in the grp array,
479 * since the slave's driver might clear it.
481 vlan_dev = bond->vlgrp->vlan_devices[vid];
482 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
483 bond->vlgrp->vlan_devices[vid] = vlan_dev;
487 res = bond_del_vlan(bond, vid);
489 printk(KERN_ERR DRV_NAME
490 ": %s: Error: Failed to remove vlan id %d\n",
491 bond_dev->name, vid);
495 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
497 struct vlan_entry *vlan;
499 write_lock_bh(&bond->lock);
501 if (list_empty(&bond->vlan_list)) {
505 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
506 slave_dev->vlan_rx_register) {
507 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
510 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
511 !(slave_dev->vlan_rx_add_vid)) {
515 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
516 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
520 write_unlock_bh(&bond->lock);
523 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
525 struct vlan_entry *vlan;
526 struct net_device *vlan_dev;
528 write_lock_bh(&bond->lock);
530 if (list_empty(&bond->vlan_list)) {
534 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
535 !(slave_dev->vlan_rx_kill_vid)) {
539 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
540 /* Save and then restore vlan_dev in the grp array,
541 * since the slave's driver might clear it.
543 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
544 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
545 bond->vlgrp->vlan_devices[vlan->vlan_id] = vlan_dev;
549 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
550 slave_dev->vlan_rx_register) {
551 slave_dev->vlan_rx_register(slave_dev, NULL);
555 write_unlock_bh(&bond->lock);
558 /*------------------------------- Link status -------------------------------*/
561 * Set the carrier state for the master according to the state of its
562 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
563 * do special 802.3ad magic.
565 * Returns zero if carrier state does not change, nonzero if it does.
567 static int bond_set_carrier(struct bonding *bond)
572 if (bond->slave_cnt == 0)
575 if (bond->params.mode == BOND_MODE_8023AD)
576 return bond_3ad_set_carrier(bond);
578 bond_for_each_slave(bond, slave, i) {
579 if (slave->link == BOND_LINK_UP) {
580 if (!netif_carrier_ok(bond->dev)) {
581 netif_carrier_on(bond->dev);
589 if (netif_carrier_ok(bond->dev)) {
590 netif_carrier_off(bond->dev);
597 * Get link speed and duplex from the slave's base driver
598 * using ethtool. If for some reason the call fails or the
599 * values are invalid, fake speed and duplex to 100/Full
602 static int bond_update_speed_duplex(struct slave *slave)
604 struct net_device *slave_dev = slave->dev;
605 static int (* ioctl)(struct net_device *, struct ifreq *, int);
607 struct ethtool_cmd etool;
609 /* Fake speed and duplex */
610 slave->speed = SPEED_100;
611 slave->duplex = DUPLEX_FULL;
613 if (slave_dev->ethtool_ops) {
616 if (!slave_dev->ethtool_ops->get_settings) {
620 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
628 ioctl = slave_dev->do_ioctl;
629 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
630 etool.cmd = ETHTOOL_GSET;
631 ifr.ifr_data = (char*)&etool;
632 if (!ioctl || (IOCTL(slave_dev, &ifr, SIOCETHTOOL) < 0)) {
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;
682 struct ethtool_value etool;
684 if (bond->params.use_carrier) {
685 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
688 ioctl = slave_dev->do_ioctl;
690 /* TODO: set pointer to correct ioctl on a per team member */
691 /* bases to make this more efficient. that is, once */
692 /* we determine the correct ioctl, we will always */
693 /* call it and not the others for that team */
697 * We cannot assume that SIOCGMIIPHY will also read a
698 * register; not all network drivers (e.g., e100)
702 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
703 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
705 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
706 mii->reg_num = MII_BMSR;
707 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
708 return (mii->val_out & BMSR_LSTATUS);
713 /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
714 /* for a period of time so we attempt to get link status */
715 /* from it last 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 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
728 etool.cmd = ETHTOOL_GLINK;
729 ifr.ifr_data = (char*)&etool;
730 if (IOCTL(slave_dev, &ifr, SIOCETHTOOL) == 0) {
731 if (etool.data == 1) {
734 dprintk("SIOCETHTOOL shows link down\n");
741 * If reporting, report that either there's no dev->do_ioctl,
742 * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
743 * cannot report link status). If not reporting, pretend
746 return (reporting ? -1 : BMSR_LSTATUS);
749 /*----------------------------- Multicast list ------------------------------*/
752 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
754 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
756 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
757 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
761 * returns dmi entry if found, NULL otherwise
763 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
765 struct dev_mc_list *idmi;
767 for (idmi = mc_list; idmi; idmi = idmi->next) {
768 if (bond_is_dmi_same(dmi, idmi)) {
777 * Push the promiscuity flag down to appropriate slaves
779 static void bond_set_promiscuity(struct bonding *bond, int inc)
781 if (USES_PRIMARY(bond->params.mode)) {
782 /* write lock already acquired */
783 if (bond->curr_active_slave) {
784 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
789 bond_for_each_slave(bond, slave, i) {
790 dev_set_promiscuity(slave->dev, inc);
796 * Push the allmulti flag down to all slaves
798 static void bond_set_allmulti(struct bonding *bond, int inc)
800 if (USES_PRIMARY(bond->params.mode)) {
801 /* write lock already acquired */
802 if (bond->curr_active_slave) {
803 dev_set_allmulti(bond->curr_active_slave->dev, inc);
808 bond_for_each_slave(bond, slave, i) {
809 dev_set_allmulti(slave->dev, inc);
815 * Add a Multicast address to slaves
818 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
820 if (USES_PRIMARY(bond->params.mode)) {
821 /* write lock already acquired */
822 if (bond->curr_active_slave) {
823 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
828 bond_for_each_slave(bond, slave, i) {
829 dev_mc_add(slave->dev, addr, alen, 0);
835 * Remove a multicast address from slave
838 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
840 if (USES_PRIMARY(bond->params.mode)) {
841 /* write lock already acquired */
842 if (bond->curr_active_slave) {
843 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
848 bond_for_each_slave(bond, slave, i) {
849 dev_mc_delete(slave->dev, addr, alen, 0);
855 * Totally destroys the mc_list in bond
857 static void bond_mc_list_destroy(struct bonding *bond)
859 struct dev_mc_list *dmi;
863 bond->mc_list = dmi->next;
870 * Copy all the Multicast addresses from src to the bonding device dst
872 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
875 struct dev_mc_list *dmi, *new_dmi;
877 for (dmi = mc_list; dmi; dmi = dmi->next) {
878 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
881 /* FIXME: Potential memory leak !!! */
885 new_dmi->next = bond->mc_list;
886 bond->mc_list = new_dmi;
887 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
888 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
889 new_dmi->dmi_users = dmi->dmi_users;
890 new_dmi->dmi_gusers = dmi->dmi_gusers;
897 * flush all members of flush->mc_list from device dev->mc_list
899 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
901 struct bonding *bond = bond_dev->priv;
902 struct dev_mc_list *dmi;
904 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
905 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
908 if (bond->params.mode == BOND_MODE_8023AD) {
909 /* del lacpdu mc addr from mc list */
910 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
912 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
916 /*--------------------------- Active slave change ---------------------------*/
919 * Update the mc list and multicast-related flags for the new and
920 * old active slaves (if any) according to the multicast mode, and
921 * promiscuous flags unconditionally.
923 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
925 struct dev_mc_list *dmi;
927 if (!USES_PRIMARY(bond->params.mode)) {
928 /* nothing to do - mc list is already up-to-date on
935 if (bond->dev->flags & IFF_PROMISC) {
936 dev_set_promiscuity(old_active->dev, -1);
939 if (bond->dev->flags & IFF_ALLMULTI) {
940 dev_set_allmulti(old_active->dev, -1);
943 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
944 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
949 if (bond->dev->flags & IFF_PROMISC) {
950 dev_set_promiscuity(new_active->dev, 1);
953 if (bond->dev->flags & IFF_ALLMULTI) {
954 dev_set_allmulti(new_active->dev, 1);
957 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
958 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
964 * find_best_interface - select the best available slave to be the active one
965 * @bond: our bonding struct
967 * Warning: Caller must hold curr_slave_lock for writing.
969 static struct slave *bond_find_best_slave(struct bonding *bond)
971 struct slave *new_active, *old_active;
972 struct slave *bestslave = NULL;
973 int mintime = bond->params.updelay;
976 new_active = old_active = bond->curr_active_slave;
978 if (!new_active) { /* there were no active slaves left */
979 if (bond->slave_cnt > 0) { /* found one slave */
980 new_active = bond->first_slave;
982 return NULL; /* still no slave, return NULL */
986 /* first try the primary link; if arping, a link must tx/rx traffic
987 * before it can be considered the curr_active_slave - also, we would skip
988 * slaves between the curr_active_slave and primary_slave that may be up
991 if ((bond->primary_slave) &&
992 (!bond->params.arp_interval) &&
993 (IS_UP(bond->primary_slave->dev))) {
994 new_active = bond->primary_slave;
997 /* remember where to stop iterating over the slaves */
998 old_active = new_active;
1000 bond_for_each_slave_from(bond, new_active, i, old_active) {
1001 if (IS_UP(new_active->dev)) {
1002 if (new_active->link == BOND_LINK_UP) {
1004 } else if (new_active->link == BOND_LINK_BACK) {
1005 /* link up, but waiting for stabilization */
1006 if (new_active->delay < mintime) {
1007 mintime = new_active->delay;
1008 bestslave = new_active;
1018 * change_active_interface - change the active slave into the specified one
1019 * @bond: our bonding struct
1020 * @new: the new slave to make the active one
1022 * Set the new slave to the bond's settings and unset them on the old
1023 * curr_active_slave.
1024 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1026 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1027 * because it is apparently the best available slave we have, even though its
1028 * updelay hasn't timed out yet.
1030 * Warning: Caller must hold curr_slave_lock for writing.
1032 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1034 struct slave *old_active = bond->curr_active_slave;
1036 if (old_active == new_active) {
1041 if (new_active->link == BOND_LINK_BACK) {
1042 if (USES_PRIMARY(bond->params.mode)) {
1043 printk(KERN_INFO DRV_NAME
1044 ": %s: making interface %s the new "
1045 "active one %d ms earlier.\n",
1046 bond->dev->name, new_active->dev->name,
1047 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1050 new_active->delay = 0;
1051 new_active->link = BOND_LINK_UP;
1052 new_active->jiffies = jiffies;
1054 if (bond->params.mode == BOND_MODE_8023AD) {
1055 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1058 if ((bond->params.mode == BOND_MODE_TLB) ||
1059 (bond->params.mode == BOND_MODE_ALB)) {
1060 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1063 if (USES_PRIMARY(bond->params.mode)) {
1064 printk(KERN_INFO DRV_NAME
1065 ": %s: making interface %s the new "
1067 bond->dev->name, new_active->dev->name);
1072 if (USES_PRIMARY(bond->params.mode)) {
1073 bond_mc_swap(bond, new_active, old_active);
1076 if ((bond->params.mode == BOND_MODE_TLB) ||
1077 (bond->params.mode == BOND_MODE_ALB)) {
1078 bond_alb_handle_active_change(bond, new_active);
1080 bond_set_slave_inactive_flags(old_active);
1082 bond_set_slave_active_flags(new_active);
1084 bond->curr_active_slave = new_active;
1087 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1089 bond_set_slave_inactive_flags(old_active);
1093 bond_set_slave_active_flags(new_active);
1095 bond_send_gratuitous_arp(bond);
1100 * bond_select_active_slave - select a new active slave, if needed
1101 * @bond: our bonding struct
1103 * This functions shoud be called when one of the following occurs:
1104 * - The old curr_active_slave has been released or lost its link.
1105 * - The primary_slave has got its link back.
1106 * - A slave has got its link back and there's no old curr_active_slave.
1108 * Warning: Caller must hold curr_slave_lock for writing.
1110 void bond_select_active_slave(struct bonding *bond)
1112 struct slave *best_slave;
1115 best_slave = bond_find_best_slave(bond);
1116 if (best_slave != bond->curr_active_slave) {
1117 bond_change_active_slave(bond, best_slave);
1118 rv = bond_set_carrier(bond);
1122 if (netif_carrier_ok(bond->dev)) {
1123 printk(KERN_INFO DRV_NAME
1124 ": %s: first active interface up!\n",
1127 printk(KERN_INFO DRV_NAME ": %s: "
1128 "now running without any active interface !\n",
1134 /*--------------------------- slave list handling ---------------------------*/
1137 * This function attaches the slave to the end of list.
1139 * bond->lock held for writing by caller.
1141 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1143 if (bond->first_slave == NULL) { /* attaching the first slave */
1144 new_slave->next = new_slave;
1145 new_slave->prev = new_slave;
1146 bond->first_slave = new_slave;
1148 new_slave->next = bond->first_slave;
1149 new_slave->prev = bond->first_slave->prev;
1150 new_slave->next->prev = new_slave;
1151 new_slave->prev->next = new_slave;
1158 * This function detaches the slave from the list.
1159 * WARNING: no check is made to verify if the slave effectively
1160 * belongs to <bond>.
1161 * Nothing is freed on return, structures are just unchained.
1162 * If any slave pointer in bond was pointing to <slave>,
1163 * it should be changed by the calling function.
1165 * bond->lock held for writing by caller.
1167 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1170 slave->next->prev = slave->prev;
1174 slave->prev->next = slave->next;
1177 if (bond->first_slave == slave) { /* slave is the first slave */
1178 if (bond->slave_cnt > 1) { /* there are more slave */
1179 bond->first_slave = slave->next;
1181 bond->first_slave = NULL; /* slave was the last one */
1190 /*---------------------------------- IOCTL ----------------------------------*/
1192 int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
1194 dprintk("bond_dev=%p\n", bond_dev);
1195 dprintk("slave_dev=%p\n", slave_dev);
1196 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1197 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1201 #define BOND_INTERSECT_FEATURES \
1202 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_TSO | NETIF_F_UFO)
1205 * Compute the common dev->feature set available to all slaves. Some
1206 * feature bits are managed elsewhere, so preserve feature bits set on
1207 * master device that are not part of the examined set.
1209 static int bond_compute_features(struct bonding *bond)
1211 unsigned long features = BOND_INTERSECT_FEATURES;
1212 struct slave *slave;
1213 struct net_device *bond_dev = bond->dev;
1214 unsigned short max_hard_header_len = ETH_HLEN;
1217 bond_for_each_slave(bond, slave, i) {
1218 features &= (slave->dev->features & BOND_INTERSECT_FEATURES);
1219 if (slave->dev->hard_header_len > max_hard_header_len)
1220 max_hard_header_len = slave->dev->hard_header_len;
1223 if ((features & NETIF_F_SG) &&
1224 !(features & NETIF_F_ALL_CSUM))
1225 features &= ~NETIF_F_SG;
1228 * features will include NETIF_F_TSO (NETIF_F_UFO) iff all
1229 * slave devices support NETIF_F_TSO (NETIF_F_UFO), which
1230 * implies that all slaves also support scatter-gather
1231 * (NETIF_F_SG), which implies that features also includes
1232 * NETIF_F_SG. So no need to check whether we have an
1233 * illegal combination of NETIF_F_{TSO,UFO} and
1237 features |= (bond_dev->features & ~BOND_INTERSECT_FEATURES);
1238 bond_dev->features = features;
1239 bond_dev->hard_header_len = max_hard_header_len;
1244 /* enslave device <slave> to bond device <master> */
1245 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1247 struct bonding *bond = bond_dev->priv;
1248 struct slave *new_slave = NULL;
1249 struct dev_mc_list *dmi;
1250 struct sockaddr addr;
1252 int old_features = bond_dev->features;
1255 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1256 slave_dev->do_ioctl == NULL) {
1257 printk(KERN_WARNING DRV_NAME
1258 ": %s: Warning: no link monitoring support for %s\n",
1259 bond_dev->name, slave_dev->name);
1262 /* bond must be initialized by bond_open() before enslaving */
1263 if (!(bond_dev->flags & IFF_UP)) {
1264 dprintk("Error, master_dev is not up\n");
1268 /* already enslaved */
1269 if (slave_dev->flags & IFF_SLAVE) {
1270 dprintk("Error, Device was already enslaved\n");
1274 /* vlan challenged mutual exclusion */
1275 /* no need to lock since we're protected by rtnl_lock */
1276 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1277 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1278 if (!list_empty(&bond->vlan_list)) {
1279 printk(KERN_ERR DRV_NAME
1280 ": %s: Error: cannot enslave VLAN "
1281 "challenged slave %s on VLAN enabled "
1282 "bond %s\n", bond_dev->name, slave_dev->name,
1286 printk(KERN_WARNING DRV_NAME
1287 ": %s: Warning: enslaved VLAN challenged "
1288 "slave %s. Adding VLANs will be blocked as "
1289 "long as %s is part of bond %s\n",
1290 bond_dev->name, slave_dev->name, slave_dev->name,
1292 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1295 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1296 if (bond->slave_cnt == 0) {
1297 /* First slave, and it is not VLAN challenged,
1298 * so remove the block of adding VLANs over the bond.
1300 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1305 * Old ifenslave binaries are no longer supported. These can
1306 * be identified with moderate accurary by the state of the slave:
1307 * the current ifenslave will set the interface down prior to
1308 * enslaving it; the old ifenslave will not.
1310 if ((slave_dev->flags & IFF_UP)) {
1311 printk(KERN_ERR DRV_NAME ": %s is up. "
1312 "This may be due to an out of date ifenslave.\n",
1315 goto err_undo_flags;
1318 if (slave_dev->set_mac_address == NULL) {
1319 printk(KERN_ERR DRV_NAME
1320 ": %s: Error: The slave device you specified does "
1321 "not support setting the MAC address. "
1322 "Your kernel likely does not support slave "
1323 "devices.\n", bond_dev->name);
1325 goto err_undo_flags;
1328 new_slave = kmalloc(sizeof(struct slave), GFP_KERNEL);
1331 goto err_undo_flags;
1334 memset(new_slave, 0, sizeof(struct slave));
1336 /* save slave's original flags before calling
1337 * netdev_set_master and dev_open
1339 new_slave->original_flags = slave_dev->flags;
1342 * Save slave's original ("permanent") mac address for modes
1343 * that need it, and for restoring it upon release, and then
1344 * set it to the master's address
1346 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1349 * Set slave to master's mac address. The application already
1350 * set the master's mac address to that of the first slave
1352 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1353 addr.sa_family = slave_dev->type;
1354 res = dev_set_mac_address(slave_dev, &addr);
1356 dprintk("Error %d calling set_mac_address\n", res);
1360 /* open the slave since the application closed it */
1361 res = dev_open(slave_dev);
1363 dprintk("Openning slave %s failed\n", slave_dev->name);
1364 goto err_restore_mac;
1367 res = netdev_set_master(slave_dev, bond_dev);
1369 dprintk("Error %d calling netdev_set_master\n", res);
1373 new_slave->dev = slave_dev;
1374 slave_dev->priv_flags |= IFF_BONDING;
1376 if ((bond->params.mode == BOND_MODE_TLB) ||
1377 (bond->params.mode == BOND_MODE_ALB)) {
1378 /* bond_alb_init_slave() must be called before all other stages since
1379 * it might fail and we do not want to have to undo everything
1381 res = bond_alb_init_slave(bond, new_slave);
1383 goto err_unset_master;
1387 /* If the mode USES_PRIMARY, then the new slave gets the
1388 * master's promisc (and mc) settings only if it becomes the
1389 * curr_active_slave, and that is taken care of later when calling
1390 * bond_change_active()
1392 if (!USES_PRIMARY(bond->params.mode)) {
1393 /* set promiscuity level to new slave */
1394 if (bond_dev->flags & IFF_PROMISC) {
1395 dev_set_promiscuity(slave_dev, 1);
1398 /* set allmulti level to new slave */
1399 if (bond_dev->flags & IFF_ALLMULTI) {
1400 dev_set_allmulti(slave_dev, 1);
1403 /* upload master's mc_list to new slave */
1404 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1405 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1409 if (bond->params.mode == BOND_MODE_8023AD) {
1410 /* add lacpdu mc addr to mc list */
1411 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1413 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1416 bond_add_vlans_on_slave(bond, slave_dev);
1418 write_lock_bh(&bond->lock);
1420 bond_attach_slave(bond, new_slave);
1422 new_slave->delay = 0;
1423 new_slave->link_failure_count = 0;
1425 bond_compute_features(bond);
1427 if (bond->params.miimon && !bond->params.use_carrier) {
1428 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1430 if ((link_reporting == -1) && !bond->params.arp_interval) {
1432 * miimon is set but a bonded network driver
1433 * does not support ETHTOOL/MII and
1434 * arp_interval is not set. Note: if
1435 * use_carrier is enabled, we will never go
1436 * here (because netif_carrier is always
1437 * supported); thus, we don't need to change
1438 * the messages for netif_carrier.
1440 printk(KERN_WARNING DRV_NAME
1441 ": %s: Warning: MII and ETHTOOL support not "
1442 "available for interface %s, and "
1443 "arp_interval/arp_ip_target module parameters "
1444 "not specified, thus bonding will not detect "
1445 "link failures! see bonding.txt for details.\n",
1446 bond_dev->name, slave_dev->name);
1447 } else if (link_reporting == -1) {
1448 /* unable get link status using mii/ethtool */
1449 printk(KERN_WARNING DRV_NAME
1450 ": %s: Warning: can't get link status from "
1451 "interface %s; the network driver associated "
1452 "with this interface does not support MII or "
1453 "ETHTOOL link status reporting, thus miimon "
1454 "has no effect on this interface.\n",
1455 bond_dev->name, slave_dev->name);
1459 /* check for initial state */
1460 if (!bond->params.miimon ||
1461 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1462 if (bond->params.updelay) {
1463 dprintk("Initial state of slave_dev is "
1464 "BOND_LINK_BACK\n");
1465 new_slave->link = BOND_LINK_BACK;
1466 new_slave->delay = bond->params.updelay;
1468 dprintk("Initial state of slave_dev is "
1470 new_slave->link = BOND_LINK_UP;
1472 new_slave->jiffies = jiffies;
1474 dprintk("Initial state of slave_dev is "
1475 "BOND_LINK_DOWN\n");
1476 new_slave->link = BOND_LINK_DOWN;
1479 if (bond_update_speed_duplex(new_slave) &&
1480 (new_slave->link != BOND_LINK_DOWN)) {
1481 printk(KERN_WARNING DRV_NAME
1482 ": %s: Warning: failed to get speed and duplex from %s, "
1483 "assumed to be 100Mb/sec and Full.\n",
1484 bond_dev->name, new_slave->dev->name);
1486 if (bond->params.mode == BOND_MODE_8023AD) {
1487 printk(KERN_WARNING DRV_NAME
1488 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1489 "support in base driver for proper aggregator "
1490 "selection.\n", bond_dev->name);
1494 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1495 /* if there is a primary slave, remember it */
1496 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1497 bond->primary_slave = new_slave;
1501 switch (bond->params.mode) {
1502 case BOND_MODE_ACTIVEBACKUP:
1503 /* if we're in active-backup mode, we need one and
1504 * only one active interface. The backup interfaces
1505 * will have their SLAVE_INACTIVE flag set because we
1506 * need them to be drop all packets. Thus, since we
1507 * guarantee that curr_active_slave always point to
1508 * the last usable interface, we just have to verify
1509 * this interface's flag.
1511 if (((!bond->curr_active_slave) ||
1512 (bond->curr_active_slave->dev->priv_flags & IFF_SLAVE_INACTIVE)) &&
1513 (new_slave->link != BOND_LINK_DOWN)) {
1514 /* first slave or no active slave yet, and this link
1515 is OK, so make this interface the active one */
1516 bond_change_active_slave(bond, new_slave);
1517 printk(KERN_INFO DRV_NAME
1518 ": %s: first active interface up!\n",
1520 netif_carrier_on(bond->dev);
1523 dprintk("This is just a backup slave\n");
1524 bond_set_slave_inactive_flags(new_slave);
1527 case BOND_MODE_8023AD:
1528 /* in 802.3ad mode, the internal mechanism
1529 * will activate the slaves in the selected
1532 bond_set_slave_inactive_flags(new_slave);
1533 /* if this is the first slave */
1534 if (bond->slave_cnt == 1) {
1535 SLAVE_AD_INFO(new_slave).id = 1;
1536 /* Initialize AD with the number of times that the AD timer is called in 1 second
1537 * can be called only after the mac address of the bond is set
1539 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1540 bond->params.lacp_fast);
1542 SLAVE_AD_INFO(new_slave).id =
1543 SLAVE_AD_INFO(new_slave->prev).id + 1;
1546 bond_3ad_bind_slave(new_slave);
1550 new_slave->state = BOND_STATE_ACTIVE;
1551 if ((!bond->curr_active_slave) &&
1552 (new_slave->link != BOND_LINK_DOWN)) {
1553 /* first slave or no active slave yet, and this link
1554 * is OK, so make this interface the active one
1556 bond_change_active_slave(bond, new_slave);
1558 bond_set_slave_inactive_flags(new_slave);
1562 dprintk("This slave is always active in trunk mode\n");
1564 /* always active in trunk mode */
1565 new_slave->state = BOND_STATE_ACTIVE;
1567 /* In trunking mode there is little meaning to curr_active_slave
1568 * anyway (it holds no special properties of the bond device),
1569 * so we can change it without calling change_active_interface()
1571 if (!bond->curr_active_slave) {
1572 bond->curr_active_slave = new_slave;
1575 } /* switch(bond_mode) */
1577 bond_set_carrier(bond);
1579 write_unlock_bh(&bond->lock);
1581 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1583 goto err_unset_master;
1585 printk(KERN_INFO DRV_NAME
1586 ": %s: enslaving %s as a%s interface with a%s link.\n",
1587 bond_dev->name, slave_dev->name,
1588 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1589 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1591 /* enslave is successful */
1594 /* Undo stages on error */
1596 netdev_set_master(slave_dev, NULL);
1599 dev_close(slave_dev);
1602 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1603 addr.sa_family = slave_dev->type;
1604 dev_set_mac_address(slave_dev, &addr);
1610 bond_dev->features = old_features;
1616 * Try to release the slave device <slave> from the bond device <master>
1617 * It is legal to access curr_active_slave without a lock because all the function
1620 * The rules for slave state should be:
1621 * for Active/Backup:
1622 * Active stays on all backups go down
1623 * for Bonded connections:
1624 * The first up interface should be left on and all others downed.
1626 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1628 struct bonding *bond = bond_dev->priv;
1629 struct slave *slave, *oldcurrent;
1630 struct sockaddr addr;
1631 int mac_addr_differ;
1633 /* slave is not a slave or master is not master of this slave */
1634 if (!(slave_dev->flags & IFF_SLAVE) ||
1635 (slave_dev->master != bond_dev)) {
1636 printk(KERN_ERR DRV_NAME
1637 ": %s: Error: cannot release %s.\n",
1638 bond_dev->name, slave_dev->name);
1642 write_lock_bh(&bond->lock);
1644 slave = bond_get_slave_by_dev(bond, slave_dev);
1646 /* not a slave of this bond */
1647 printk(KERN_INFO DRV_NAME
1648 ": %s: %s not enslaved\n",
1649 bond_dev->name, slave_dev->name);
1650 write_unlock_bh(&bond->lock);
1654 mac_addr_differ = memcmp(bond_dev->dev_addr,
1657 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1658 printk(KERN_WARNING DRV_NAME
1659 ": %s: Warning: the permanent HWaddr of %s "
1660 "- %02X:%02X:%02X:%02X:%02X:%02X - is "
1661 "still in use by %s. Set the HWaddr of "
1662 "%s to a different address to avoid "
1666 slave->perm_hwaddr[0],
1667 slave->perm_hwaddr[1],
1668 slave->perm_hwaddr[2],
1669 slave->perm_hwaddr[3],
1670 slave->perm_hwaddr[4],
1671 slave->perm_hwaddr[5],
1676 /* Inform AD package of unbinding of slave. */
1677 if (bond->params.mode == BOND_MODE_8023AD) {
1678 /* must be called before the slave is
1679 * detached from the list
1681 bond_3ad_unbind_slave(slave);
1684 printk(KERN_INFO DRV_NAME
1685 ": %s: releasing %s interface %s\n",
1687 (slave->state == BOND_STATE_ACTIVE)
1688 ? "active" : "backup",
1691 oldcurrent = bond->curr_active_slave;
1693 bond->current_arp_slave = NULL;
1695 /* release the slave from its bond */
1696 bond_detach_slave(bond, slave);
1698 bond_compute_features(bond);
1700 if (bond->primary_slave == slave) {
1701 bond->primary_slave = NULL;
1704 if (oldcurrent == slave) {
1705 bond_change_active_slave(bond, NULL);
1708 if ((bond->params.mode == BOND_MODE_TLB) ||
1709 (bond->params.mode == BOND_MODE_ALB)) {
1710 /* Must be called only after the slave has been
1711 * detached from the list and the curr_active_slave
1712 * has been cleared (if our_slave == old_current),
1713 * but before a new active slave is selected.
1715 bond_alb_deinit_slave(bond, slave);
1718 if (oldcurrent == slave)
1719 bond_select_active_slave(bond);
1721 if (bond->slave_cnt == 0) {
1722 bond_set_carrier(bond);
1724 /* if the last slave was removed, zero the mac address
1725 * of the master so it will be set by the application
1726 * to the mac address of the first slave
1728 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1730 if (list_empty(&bond->vlan_list)) {
1731 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1733 printk(KERN_WARNING DRV_NAME
1734 ": %s: Warning: clearing HW address of %s while it "
1735 "still has VLANs.\n",
1736 bond_dev->name, bond_dev->name);
1737 printk(KERN_WARNING DRV_NAME
1738 ": %s: When re-adding slaves, make sure the bond's "
1739 "HW address matches its VLANs'.\n",
1742 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1743 !bond_has_challenged_slaves(bond)) {
1744 printk(KERN_INFO DRV_NAME
1745 ": %s: last VLAN challenged slave %s "
1746 "left bond %s. VLAN blocking is removed\n",
1747 bond_dev->name, slave_dev->name, bond_dev->name);
1748 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1751 write_unlock_bh(&bond->lock);
1753 /* must do this from outside any spinlocks */
1754 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1756 bond_del_vlans_from_slave(bond, slave_dev);
1758 /* If the mode USES_PRIMARY, then we should only remove its
1759 * promisc and mc settings if it was the curr_active_slave, but that was
1760 * already taken care of above when we detached the slave
1762 if (!USES_PRIMARY(bond->params.mode)) {
1763 /* unset promiscuity level from slave */
1764 if (bond_dev->flags & IFF_PROMISC) {
1765 dev_set_promiscuity(slave_dev, -1);
1768 /* unset allmulti level from slave */
1769 if (bond_dev->flags & IFF_ALLMULTI) {
1770 dev_set_allmulti(slave_dev, -1);
1773 /* flush master's mc_list from slave */
1774 bond_mc_list_flush(bond_dev, slave_dev);
1777 netdev_set_master(slave_dev, NULL);
1779 /* close slave before restoring its mac address */
1780 dev_close(slave_dev);
1782 /* restore original ("permanent") mac address */
1783 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1784 addr.sa_family = slave_dev->type;
1785 dev_set_mac_address(slave_dev, &addr);
1787 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1788 IFF_SLAVE_INACTIVE | IFF_BONDING);
1792 return 0; /* deletion OK */
1796 * This function releases all slaves.
1798 static int bond_release_all(struct net_device *bond_dev)
1800 struct bonding *bond = bond_dev->priv;
1801 struct slave *slave;
1802 struct net_device *slave_dev;
1803 struct sockaddr addr;
1805 write_lock_bh(&bond->lock);
1807 netif_carrier_off(bond_dev);
1809 if (bond->slave_cnt == 0) {
1813 bond->current_arp_slave = NULL;
1814 bond->primary_slave = NULL;
1815 bond_change_active_slave(bond, NULL);
1817 while ((slave = bond->first_slave) != NULL) {
1818 /* Inform AD package of unbinding of slave
1819 * before slave is detached from the list.
1821 if (bond->params.mode == BOND_MODE_8023AD) {
1822 bond_3ad_unbind_slave(slave);
1825 slave_dev = slave->dev;
1826 bond_detach_slave(bond, slave);
1828 if ((bond->params.mode == BOND_MODE_TLB) ||
1829 (bond->params.mode == BOND_MODE_ALB)) {
1830 /* must be called only after the slave
1831 * has been detached from the list
1833 bond_alb_deinit_slave(bond, slave);
1836 bond_compute_features(bond);
1838 /* now that the slave is detached, unlock and perform
1839 * all the undo steps that should not be called from
1842 write_unlock_bh(&bond->lock);
1844 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1845 bond_del_vlans_from_slave(bond, slave_dev);
1847 /* If the mode USES_PRIMARY, then we should only remove its
1848 * promisc and mc settings if it was the curr_active_slave, but that was
1849 * already taken care of above when we detached the slave
1851 if (!USES_PRIMARY(bond->params.mode)) {
1852 /* unset promiscuity level from slave */
1853 if (bond_dev->flags & IFF_PROMISC) {
1854 dev_set_promiscuity(slave_dev, -1);
1857 /* unset allmulti level from slave */
1858 if (bond_dev->flags & IFF_ALLMULTI) {
1859 dev_set_allmulti(slave_dev, -1);
1862 /* flush master's mc_list from slave */
1863 bond_mc_list_flush(bond_dev, slave_dev);
1866 netdev_set_master(slave_dev, NULL);
1868 /* close slave before restoring its mac address */
1869 dev_close(slave_dev);
1871 /* restore original ("permanent") mac address*/
1872 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1873 addr.sa_family = slave_dev->type;
1874 dev_set_mac_address(slave_dev, &addr);
1876 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1877 IFF_SLAVE_INACTIVE);
1881 /* re-acquire the lock before getting the next slave */
1882 write_lock_bh(&bond->lock);
1885 /* zero the mac address of the master so it will be
1886 * set by the application to the mac address of the
1889 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1891 if (list_empty(&bond->vlan_list)) {
1892 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1894 printk(KERN_WARNING DRV_NAME
1895 ": %s: Warning: clearing HW address of %s while it "
1896 "still has VLANs.\n",
1897 bond_dev->name, bond_dev->name);
1898 printk(KERN_WARNING DRV_NAME
1899 ": %s: When re-adding slaves, make sure the bond's "
1900 "HW address matches its VLANs'.\n",
1904 printk(KERN_INFO DRV_NAME
1905 ": %s: released all slaves\n",
1909 write_unlock_bh(&bond->lock);
1915 * This function changes the active slave to slave <slave_dev>.
1916 * It returns -EINVAL in the following cases.
1917 * - <slave_dev> is not found in the list.
1918 * - There is not active slave now.
1919 * - <slave_dev> is already active.
1920 * - The link state of <slave_dev> is not BOND_LINK_UP.
1921 * - <slave_dev> is not running.
1922 * In these cases, this fuction does nothing.
1923 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1925 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1927 struct bonding *bond = bond_dev->priv;
1928 struct slave *old_active = NULL;
1929 struct slave *new_active = NULL;
1932 if (!USES_PRIMARY(bond->params.mode)) {
1936 /* Verify that master_dev is indeed the master of slave_dev */
1937 if (!(slave_dev->flags & IFF_SLAVE) ||
1938 (slave_dev->master != bond_dev)) {
1942 write_lock_bh(&bond->lock);
1944 old_active = bond->curr_active_slave;
1945 new_active = bond_get_slave_by_dev(bond, slave_dev);
1948 * Changing to the current active: do nothing; return success.
1950 if (new_active && (new_active == old_active)) {
1951 write_unlock_bh(&bond->lock);
1957 (new_active->link == BOND_LINK_UP) &&
1958 IS_UP(new_active->dev)) {
1959 bond_change_active_slave(bond, new_active);
1964 write_unlock_bh(&bond->lock);
1969 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1971 struct bonding *bond = bond_dev->priv;
1973 info->bond_mode = bond->params.mode;
1974 info->miimon = bond->params.miimon;
1976 read_lock_bh(&bond->lock);
1977 info->num_slaves = bond->slave_cnt;
1978 read_unlock_bh(&bond->lock);
1983 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
1985 struct bonding *bond = bond_dev->priv;
1986 struct slave *slave;
1989 if (info->slave_id < 0) {
1993 read_lock_bh(&bond->lock);
1995 bond_for_each_slave(bond, slave, i) {
1996 if (i == (int)info->slave_id) {
2002 read_unlock_bh(&bond->lock);
2005 strcpy(info->slave_name, slave->dev->name);
2006 info->link = slave->link;
2007 info->state = slave->state;
2008 info->link_failure_count = slave->link_failure_count;
2016 /*-------------------------------- Monitoring -------------------------------*/
2018 /* this function is called regularly to monitor each slave's link. */
2019 void bond_mii_monitor(struct net_device *bond_dev)
2021 struct bonding *bond = bond_dev->priv;
2022 struct slave *slave, *oldcurrent;
2023 int do_failover = 0;
2027 read_lock(&bond->lock);
2029 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2031 if (bond->kill_timers) {
2035 if (bond->slave_cnt == 0) {
2039 /* we will try to read the link status of each of our slaves, and
2040 * set their IFF_RUNNING flag appropriately. For each slave not
2041 * supporting MII status, we won't do anything so that a user-space
2042 * program could monitor the link itself if needed.
2045 read_lock(&bond->curr_slave_lock);
2046 oldcurrent = bond->curr_active_slave;
2047 read_unlock(&bond->curr_slave_lock);
2049 bond_for_each_slave(bond, slave, i) {
2050 struct net_device *slave_dev = slave->dev;
2052 u16 old_speed = slave->speed;
2053 u8 old_duplex = slave->duplex;
2055 link_state = bond_check_dev_link(bond, slave_dev, 0);
2057 switch (slave->link) {
2058 case BOND_LINK_UP: /* the link was up */
2059 if (link_state == BMSR_LSTATUS) {
2060 /* link stays up, nothing more to do */
2062 } else { /* link going down */
2063 slave->link = BOND_LINK_FAIL;
2064 slave->delay = bond->params.downdelay;
2066 if (slave->link_failure_count < UINT_MAX) {
2067 slave->link_failure_count++;
2070 if (bond->params.downdelay) {
2071 printk(KERN_INFO DRV_NAME
2072 ": %s: link status down for %s "
2073 "interface %s, disabling it in "
2077 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2078 ? ((slave == oldcurrent)
2079 ? "active " : "backup ")
2083 bond->params.downdelay * bond->params.miimon);
2086 /* no break ! fall through the BOND_LINK_FAIL test to
2087 ensure proper action to be taken
2089 case BOND_LINK_FAIL: /* the link has just gone down */
2090 if (link_state != BMSR_LSTATUS) {
2091 /* link stays down */
2092 if (slave->delay <= 0) {
2093 /* link down for too long time */
2094 slave->link = BOND_LINK_DOWN;
2096 /* in active/backup mode, we must
2097 * completely disable this interface
2099 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2100 (bond->params.mode == BOND_MODE_8023AD)) {
2101 bond_set_slave_inactive_flags(slave);
2104 printk(KERN_INFO DRV_NAME
2105 ": %s: link status definitely "
2106 "down for interface %s, "
2111 /* notify ad that the link status has changed */
2112 if (bond->params.mode == BOND_MODE_8023AD) {
2113 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2116 if ((bond->params.mode == BOND_MODE_TLB) ||
2117 (bond->params.mode == BOND_MODE_ALB)) {
2118 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2121 if (slave == oldcurrent) {
2129 slave->link = BOND_LINK_UP;
2130 slave->jiffies = jiffies;
2131 printk(KERN_INFO DRV_NAME
2132 ": %s: link status up again after %d "
2133 "ms for interface %s.\n",
2135 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2139 case BOND_LINK_DOWN: /* the link was down */
2140 if (link_state != BMSR_LSTATUS) {
2141 /* the link stays down, nothing more to do */
2143 } else { /* link going up */
2144 slave->link = BOND_LINK_BACK;
2145 slave->delay = bond->params.updelay;
2147 if (bond->params.updelay) {
2148 /* if updelay == 0, no need to
2149 advertise about a 0 ms delay */
2150 printk(KERN_INFO DRV_NAME
2151 ": %s: link status up for "
2152 "interface %s, enabling it "
2156 bond->params.updelay * bond->params.miimon);
2159 /* no break ! fall through the BOND_LINK_BACK state in
2160 case there's something to do.
2162 case BOND_LINK_BACK: /* the link has just come back */
2163 if (link_state != BMSR_LSTATUS) {
2164 /* link down again */
2165 slave->link = BOND_LINK_DOWN;
2167 printk(KERN_INFO DRV_NAME
2168 ": %s: link status down again after %d "
2169 "ms for interface %s.\n",
2171 (bond->params.updelay - slave->delay) * bond->params.miimon,
2175 if (slave->delay == 0) {
2176 /* now the link has been up for long time enough */
2177 slave->link = BOND_LINK_UP;
2178 slave->jiffies = jiffies;
2180 if (bond->params.mode == BOND_MODE_8023AD) {
2181 /* prevent it from being the active one */
2182 slave->state = BOND_STATE_BACKUP;
2183 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2184 /* make it immediately active */
2185 slave->state = BOND_STATE_ACTIVE;
2186 } else if (slave != bond->primary_slave) {
2187 /* prevent it from being the active one */
2188 slave->state = BOND_STATE_BACKUP;
2191 printk(KERN_INFO DRV_NAME
2192 ": %s: link status definitely "
2193 "up for interface %s.\n",
2197 /* notify ad that the link status has changed */
2198 if (bond->params.mode == BOND_MODE_8023AD) {
2199 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2202 if ((bond->params.mode == BOND_MODE_TLB) ||
2203 (bond->params.mode == BOND_MODE_ALB)) {
2204 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2207 if ((!oldcurrent) ||
2208 (slave == bond->primary_slave)) {
2217 /* Should not happen */
2218 printk(KERN_ERR DRV_NAME
2219 ": %s: Error: %s Illegal value (link=%d)\n",
2224 } /* end of switch (slave->link) */
2226 bond_update_speed_duplex(slave);
2228 if (bond->params.mode == BOND_MODE_8023AD) {
2229 if (old_speed != slave->speed) {
2230 bond_3ad_adapter_speed_changed(slave);
2233 if (old_duplex != slave->duplex) {
2234 bond_3ad_adapter_duplex_changed(slave);
2241 write_lock(&bond->curr_slave_lock);
2243 bond_select_active_slave(bond);
2245 write_unlock(&bond->curr_slave_lock);
2247 bond_set_carrier(bond);
2250 if (bond->params.miimon) {
2251 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2254 read_unlock(&bond->lock);
2258 static u32 bond_glean_dev_ip(struct net_device *dev)
2260 struct in_device *idev;
2261 struct in_ifaddr *ifa;
2268 idev = __in_dev_get_rcu(dev);
2272 ifa = idev->ifa_list;
2276 addr = ifa->ifa_local;
2282 static int bond_has_ip(struct bonding *bond)
2284 struct vlan_entry *vlan, *vlan_next;
2286 if (bond->master_ip)
2289 if (list_empty(&bond->vlan_list))
2292 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2302 * We go to the (large) trouble of VLAN tagging ARP frames because
2303 * switches in VLAN mode (especially if ports are configured as
2304 * "native" to a VLAN) might not pass non-tagged frames.
2306 static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip, u32 src_ip, unsigned short vlan_id)
2308 struct sk_buff *skb;
2310 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2311 slave_dev->name, dest_ip, src_ip, vlan_id);
2313 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2314 NULL, slave_dev->dev_addr, NULL);
2317 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2321 skb = vlan_put_tag(skb, vlan_id);
2323 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2331 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2334 u32 *targets = bond->params.arp_targets;
2335 struct vlan_entry *vlan, *vlan_next;
2336 struct net_device *vlan_dev;
2340 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2343 dprintk("basa: target %x\n", targets[i]);
2344 if (list_empty(&bond->vlan_list)) {
2345 dprintk("basa: empty vlan: arp_send\n");
2346 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2347 bond->master_ip, 0);
2352 * If VLANs are configured, we do a route lookup to
2353 * determine which VLAN interface would be used, so we
2354 * can tag the ARP with the proper VLAN tag.
2356 memset(&fl, 0, sizeof(fl));
2357 fl.fl4_dst = targets[i];
2358 fl.fl4_tos = RTO_ONLINK;
2360 rv = ip_route_output_key(&rt, &fl);
2362 if (net_ratelimit()) {
2363 printk(KERN_WARNING DRV_NAME
2364 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2365 bond->dev->name, NIPQUAD(fl.fl4_dst));
2371 * This target is not on a VLAN
2373 if (rt->u.dst.dev == bond->dev) {
2375 dprintk("basa: rtdev == bond->dev: arp_send\n");
2376 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2377 bond->master_ip, 0);
2382 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2384 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2385 if (vlan_dev == rt->u.dst.dev) {
2386 vlan_id = vlan->vlan_id;
2387 dprintk("basa: vlan match on %s %d\n",
2388 vlan_dev->name, vlan_id);
2395 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2396 vlan->vlan_ip, vlan_id);
2400 if (net_ratelimit()) {
2401 printk(KERN_WARNING DRV_NAME
2402 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2403 bond->dev->name, NIPQUAD(fl.fl4_dst),
2404 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2411 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2412 * for each VLAN above us.
2414 static void bond_send_gratuitous_arp(struct bonding *bond)
2416 struct slave *slave = bond->curr_active_slave;
2417 struct vlan_entry *vlan;
2418 struct net_device *vlan_dev;
2420 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2421 slave ? slave->dev->name : "NULL");
2425 if (bond->master_ip) {
2426 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2427 bond->master_ip, 0);
2430 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2431 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2432 if (vlan->vlan_ip) {
2433 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2434 vlan->vlan_ip, vlan->vlan_id);
2440 * this function is called regularly to monitor each slave's link
2441 * ensuring that traffic is being sent and received when arp monitoring
2442 * is used in load-balancing mode. if the adapter has been dormant, then an
2443 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2444 * arp monitoring in active backup mode.
2446 void bond_loadbalance_arp_mon(struct net_device *bond_dev)
2448 struct bonding *bond = bond_dev->priv;
2449 struct slave *slave, *oldcurrent;
2450 int do_failover = 0;
2454 read_lock(&bond->lock);
2456 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2458 if (bond->kill_timers) {
2462 if (bond->slave_cnt == 0) {
2466 read_lock(&bond->curr_slave_lock);
2467 oldcurrent = bond->curr_active_slave;
2468 read_unlock(&bond->curr_slave_lock);
2470 /* see if any of the previous devices are up now (i.e. they have
2471 * xmt and rcv traffic). the curr_active_slave does not come into
2472 * the picture unless it is null. also, slave->jiffies is not needed
2473 * here because we send an arp on each slave and give a slave as
2474 * long as it needs to get the tx/rx within the delta.
2475 * TODO: what about up/down delay in arp mode? it wasn't here before
2478 bond_for_each_slave(bond, slave, i) {
2479 if (slave->link != BOND_LINK_UP) {
2480 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2481 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2483 slave->link = BOND_LINK_UP;
2484 slave->state = BOND_STATE_ACTIVE;
2486 /* primary_slave has no meaning in round-robin
2487 * mode. the window of a slave being up and
2488 * curr_active_slave being null after enslaving
2492 printk(KERN_INFO DRV_NAME
2493 ": %s: link status definitely "
2494 "up for interface %s, ",
2499 printk(KERN_INFO DRV_NAME
2500 ": %s: interface %s is now up\n",
2506 /* slave->link == BOND_LINK_UP */
2508 /* not all switches will respond to an arp request
2509 * when the source ip is 0, so don't take the link down
2510 * if we don't know our ip yet
2512 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2513 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2514 bond_has_ip(bond))) {
2516 slave->link = BOND_LINK_DOWN;
2517 slave->state = BOND_STATE_BACKUP;
2519 if (slave->link_failure_count < UINT_MAX) {
2520 slave->link_failure_count++;
2523 printk(KERN_INFO DRV_NAME
2524 ": %s: interface %s is now down.\n",
2528 if (slave == oldcurrent) {
2534 /* note: if switch is in round-robin mode, all links
2535 * must tx arp to ensure all links rx an arp - otherwise
2536 * links may oscillate or not come up at all; if switch is
2537 * in something like xor mode, there is nothing we can
2538 * do - all replies will be rx'ed on same link causing slaves
2539 * to be unstable during low/no traffic periods
2541 if (IS_UP(slave->dev)) {
2542 bond_arp_send_all(bond, slave);
2547 write_lock(&bond->curr_slave_lock);
2549 bond_select_active_slave(bond);
2551 write_unlock(&bond->curr_slave_lock);
2555 if (bond->params.arp_interval) {
2556 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2559 read_unlock(&bond->lock);
2563 * When using arp monitoring in active-backup mode, this function is
2564 * called to determine if any backup slaves have went down or a new
2565 * current slave needs to be found.
2566 * The backup slaves never generate traffic, they are considered up by merely
2567 * receiving traffic. If the current slave goes down, each backup slave will
2568 * be given the opportunity to tx/rx an arp before being taken down - this
2569 * prevents all slaves from being taken down due to the current slave not
2570 * sending any traffic for the backups to receive. The arps are not necessarily
2571 * necessary, any tx and rx traffic will keep the current slave up. While any
2572 * rx traffic will keep the backup slaves up, the current slave is responsible
2573 * for generating traffic to keep them up regardless of any other traffic they
2574 * may have received.
2575 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2577 void bond_activebackup_arp_mon(struct net_device *bond_dev)
2579 struct bonding *bond = bond_dev->priv;
2580 struct slave *slave;
2584 read_lock(&bond->lock);
2586 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2588 if (bond->kill_timers) {
2592 if (bond->slave_cnt == 0) {
2596 /* determine if any slave has come up or any backup slave has
2598 * TODO: what about up/down delay in arp mode? it wasn't here before
2601 bond_for_each_slave(bond, slave, i) {
2602 if (slave->link != BOND_LINK_UP) {
2603 if ((jiffies - slave->dev->last_rx) <= delta_in_ticks) {
2605 slave->link = BOND_LINK_UP;
2607 write_lock(&bond->curr_slave_lock);
2609 if ((!bond->curr_active_slave) &&
2610 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2611 bond_change_active_slave(bond, slave);
2612 bond->current_arp_slave = NULL;
2613 } else if (bond->curr_active_slave != slave) {
2614 /* this slave has just come up but we
2615 * already have a current slave; this
2616 * can also happen if bond_enslave adds
2617 * a new slave that is up while we are
2618 * searching for a new slave
2620 bond_set_slave_inactive_flags(slave);
2621 bond->current_arp_slave = NULL;
2624 bond_set_carrier(bond);
2626 if (slave == bond->curr_active_slave) {
2627 printk(KERN_INFO DRV_NAME
2628 ": %s: %s is up and now the "
2629 "active interface\n",
2632 netif_carrier_on(bond->dev);
2634 printk(KERN_INFO DRV_NAME
2635 ": %s: backup interface %s is "
2641 write_unlock(&bond->curr_slave_lock);
2644 read_lock(&bond->curr_slave_lock);
2646 if ((slave != bond->curr_active_slave) &&
2647 (!bond->current_arp_slave) &&
2648 (((jiffies - slave->dev->last_rx) >= 3*delta_in_ticks) &&
2649 bond_has_ip(bond))) {
2650 /* a backup slave has gone down; three times
2651 * the delta allows the current slave to be
2652 * taken out before the backup slave.
2653 * note: a non-null current_arp_slave indicates
2654 * the curr_active_slave went down and we are
2655 * searching for a new one; under this
2656 * condition we only take the curr_active_slave
2657 * down - this gives each slave a chance to
2658 * tx/rx traffic before being taken out
2661 read_unlock(&bond->curr_slave_lock);
2663 slave->link = BOND_LINK_DOWN;
2665 if (slave->link_failure_count < UINT_MAX) {
2666 slave->link_failure_count++;
2669 bond_set_slave_inactive_flags(slave);
2671 printk(KERN_INFO DRV_NAME
2672 ": %s: backup interface %s is now down\n",
2676 read_unlock(&bond->curr_slave_lock);
2681 read_lock(&bond->curr_slave_lock);
2682 slave = bond->curr_active_slave;
2683 read_unlock(&bond->curr_slave_lock);
2686 /* if we have sent traffic in the past 2*arp_intervals but
2687 * haven't xmit and rx traffic in that time interval, select
2688 * a different slave. slave->jiffies is only updated when
2689 * a slave first becomes the curr_active_slave - not necessarily
2690 * after every arp; this ensures the slave has a full 2*delta
2691 * before being taken out. if a primary is being used, check
2692 * if it is up and needs to take over as the curr_active_slave
2694 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2695 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2696 bond_has_ip(bond))) &&
2697 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2699 slave->link = BOND_LINK_DOWN;
2701 if (slave->link_failure_count < UINT_MAX) {
2702 slave->link_failure_count++;
2705 printk(KERN_INFO DRV_NAME
2706 ": %s: link status down for active interface "
2707 "%s, disabling it\n",
2711 write_lock(&bond->curr_slave_lock);
2713 bond_select_active_slave(bond);
2714 slave = bond->curr_active_slave;
2716 write_unlock(&bond->curr_slave_lock);
2718 bond->current_arp_slave = slave;
2721 slave->jiffies = jiffies;
2723 } else if ((bond->primary_slave) &&
2724 (bond->primary_slave != slave) &&
2725 (bond->primary_slave->link == BOND_LINK_UP)) {
2726 /* at this point, slave is the curr_active_slave */
2727 printk(KERN_INFO DRV_NAME
2728 ": %s: changing from interface %s to primary "
2732 bond->primary_slave->dev->name);
2734 /* primary is up so switch to it */
2735 write_lock(&bond->curr_slave_lock);
2736 bond_change_active_slave(bond, bond->primary_slave);
2737 write_unlock(&bond->curr_slave_lock);
2739 slave = bond->primary_slave;
2740 slave->jiffies = jiffies;
2742 bond->current_arp_slave = NULL;
2745 /* the current slave must tx an arp to ensure backup slaves
2748 if (slave && bond_has_ip(bond)) {
2749 bond_arp_send_all(bond, slave);
2753 /* if we don't have a curr_active_slave, search for the next available
2754 * backup slave from the current_arp_slave and make it the candidate
2755 * for becoming the curr_active_slave
2758 if (!bond->current_arp_slave) {
2759 bond->current_arp_slave = bond->first_slave;
2762 if (bond->current_arp_slave) {
2763 bond_set_slave_inactive_flags(bond->current_arp_slave);
2765 /* search for next candidate */
2766 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
2767 if (IS_UP(slave->dev)) {
2768 slave->link = BOND_LINK_BACK;
2769 bond_set_slave_active_flags(slave);
2770 bond_arp_send_all(bond, slave);
2771 slave->jiffies = jiffies;
2772 bond->current_arp_slave = slave;
2776 /* if the link state is up at this point, we
2777 * mark it down - this can happen if we have
2778 * simultaneous link failures and
2779 * reselect_active_interface doesn't make this
2780 * one the current slave so it is still marked
2781 * up when it is actually down
2783 if (slave->link == BOND_LINK_UP) {
2784 slave->link = BOND_LINK_DOWN;
2785 if (slave->link_failure_count < UINT_MAX) {
2786 slave->link_failure_count++;
2789 bond_set_slave_inactive_flags(slave);
2791 printk(KERN_INFO DRV_NAME
2792 ": %s: backup interface %s is "
2802 if (bond->params.arp_interval) {
2803 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2806 read_unlock(&bond->lock);
2809 /*------------------------------ proc/seq_file-------------------------------*/
2811 #ifdef CONFIG_PROC_FS
2813 #define SEQ_START_TOKEN ((void *)1)
2815 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
2817 struct bonding *bond = seq->private;
2819 struct slave *slave;
2822 /* make sure the bond won't be taken away */
2823 read_lock(&dev_base_lock);
2824 read_lock_bh(&bond->lock);
2827 return SEQ_START_TOKEN;
2830 bond_for_each_slave(bond, slave, i) {
2831 if (++off == *pos) {
2839 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2841 struct bonding *bond = seq->private;
2842 struct slave *slave = v;
2845 if (v == SEQ_START_TOKEN) {
2846 return bond->first_slave;
2849 slave = slave->next;
2851 return (slave == bond->first_slave) ? NULL : slave;
2854 static void bond_info_seq_stop(struct seq_file *seq, void *v)
2856 struct bonding *bond = seq->private;
2858 read_unlock_bh(&bond->lock);
2859 read_unlock(&dev_base_lock);
2862 static void bond_info_show_master(struct seq_file *seq)
2864 struct bonding *bond = seq->private;
2869 read_lock(&bond->curr_slave_lock);
2870 curr = bond->curr_active_slave;
2871 read_unlock(&bond->curr_slave_lock);
2873 seq_printf(seq, "Bonding Mode: %s\n",
2874 bond_mode_name(bond->params.mode));
2876 if (bond->params.mode == BOND_MODE_XOR ||
2877 bond->params.mode == BOND_MODE_8023AD) {
2878 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
2879 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
2880 bond->params.xmit_policy);
2883 if (USES_PRIMARY(bond->params.mode)) {
2884 seq_printf(seq, "Primary Slave: %s\n",
2885 (bond->primary_slave) ?
2886 bond->primary_slave->dev->name : "None");
2888 seq_printf(seq, "Currently Active Slave: %s\n",
2889 (curr) ? curr->dev->name : "None");
2892 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
2894 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
2895 seq_printf(seq, "Up Delay (ms): %d\n",
2896 bond->params.updelay * bond->params.miimon);
2897 seq_printf(seq, "Down Delay (ms): %d\n",
2898 bond->params.downdelay * bond->params.miimon);
2901 /* ARP information */
2902 if(bond->params.arp_interval > 0) {
2904 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
2905 bond->params.arp_interval);
2907 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
2909 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
2910 if (!bond->params.arp_targets[i])
2913 seq_printf(seq, ",");
2914 target = ntohl(bond->params.arp_targets[i]);
2915 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
2918 seq_printf(seq, "\n");
2921 if (bond->params.mode == BOND_MODE_8023AD) {
2922 struct ad_info ad_info;
2924 seq_puts(seq, "\n802.3ad info\n");
2925 seq_printf(seq, "LACP rate: %s\n",
2926 (bond->params.lacp_fast) ? "fast" : "slow");
2928 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
2929 seq_printf(seq, "bond %s has no active aggregator\n",
2932 seq_printf(seq, "Active Aggregator Info:\n");
2934 seq_printf(seq, "\tAggregator ID: %d\n",
2935 ad_info.aggregator_id);
2936 seq_printf(seq, "\tNumber of ports: %d\n",
2938 seq_printf(seq, "\tActor Key: %d\n",
2940 seq_printf(seq, "\tPartner Key: %d\n",
2941 ad_info.partner_key);
2942 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
2943 ad_info.partner_system[0],
2944 ad_info.partner_system[1],
2945 ad_info.partner_system[2],
2946 ad_info.partner_system[3],
2947 ad_info.partner_system[4],
2948 ad_info.partner_system[5]);
2953 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
2955 struct bonding *bond = seq->private;
2957 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
2958 seq_printf(seq, "MII Status: %s\n",
2959 (slave->link == BOND_LINK_UP) ? "up" : "down");
2960 seq_printf(seq, "Link Failure Count: %u\n",
2961 slave->link_failure_count);
2964 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
2965 slave->perm_hwaddr[0], slave->perm_hwaddr[1],
2966 slave->perm_hwaddr[2], slave->perm_hwaddr[3],
2967 slave->perm_hwaddr[4], slave->perm_hwaddr[5]);
2969 if (bond->params.mode == BOND_MODE_8023AD) {
2970 const struct aggregator *agg
2971 = SLAVE_AD_INFO(slave).port.aggregator;
2974 seq_printf(seq, "Aggregator ID: %d\n",
2975 agg->aggregator_identifier);
2977 seq_puts(seq, "Aggregator ID: N/A\n");
2982 static int bond_info_seq_show(struct seq_file *seq, void *v)
2984 if (v == SEQ_START_TOKEN) {
2985 seq_printf(seq, "%s\n", version);
2986 bond_info_show_master(seq);
2988 bond_info_show_slave(seq, v);
2994 static struct seq_operations bond_info_seq_ops = {
2995 .start = bond_info_seq_start,
2996 .next = bond_info_seq_next,
2997 .stop = bond_info_seq_stop,
2998 .show = bond_info_seq_show,
3001 static int bond_info_open(struct inode *inode, struct file *file)
3003 struct seq_file *seq;
3004 struct proc_dir_entry *proc;
3007 res = seq_open(file, &bond_info_seq_ops);
3009 /* recover the pointer buried in proc_dir_entry data */
3010 seq = file->private_data;
3012 seq->private = proc->data;
3018 static struct file_operations bond_info_fops = {
3019 .owner = THIS_MODULE,
3020 .open = bond_info_open,
3022 .llseek = seq_lseek,
3023 .release = seq_release,
3026 static int bond_create_proc_entry(struct bonding *bond)
3028 struct net_device *bond_dev = bond->dev;
3030 if (bond_proc_dir) {
3031 bond->proc_entry = create_proc_entry(bond_dev->name,
3034 if (bond->proc_entry == NULL) {
3035 printk(KERN_WARNING DRV_NAME
3036 ": Warning: Cannot create /proc/net/%s/%s\n",
3037 DRV_NAME, bond_dev->name);
3039 bond->proc_entry->data = bond;
3040 bond->proc_entry->proc_fops = &bond_info_fops;
3041 bond->proc_entry->owner = THIS_MODULE;
3042 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3049 static void bond_remove_proc_entry(struct bonding *bond)
3051 if (bond_proc_dir && bond->proc_entry) {
3052 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3053 memset(bond->proc_file_name, 0, IFNAMSIZ);
3054 bond->proc_entry = NULL;
3058 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3059 * Caller must hold rtnl_lock.
3061 static void bond_create_proc_dir(void)
3063 int len = strlen(DRV_NAME);
3065 for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3066 bond_proc_dir = bond_proc_dir->next) {
3067 if ((bond_proc_dir->namelen == len) &&
3068 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3073 if (!bond_proc_dir) {
3074 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3075 if (bond_proc_dir) {
3076 bond_proc_dir->owner = THIS_MODULE;
3078 printk(KERN_WARNING DRV_NAME
3079 ": Warning: cannot create /proc/net/%s\n",
3085 /* Destroy the bonding directory under /proc/net, if empty.
3086 * Caller must hold rtnl_lock.
3088 static void bond_destroy_proc_dir(void)
3090 struct proc_dir_entry *de;
3092 if (!bond_proc_dir) {
3096 /* verify that the /proc dir is empty */
3097 for (de = bond_proc_dir->subdir; de; de = de->next) {
3098 /* ignore . and .. */
3099 if (*(de->name) != '.') {
3105 if (bond_proc_dir->owner == THIS_MODULE) {
3106 bond_proc_dir->owner = NULL;
3109 remove_proc_entry(DRV_NAME, proc_net);
3110 bond_proc_dir = NULL;
3113 #endif /* CONFIG_PROC_FS */
3115 /*-------------------------- netdev event handling --------------------------*/
3118 * Change device name
3120 static int bond_event_changename(struct bonding *bond)
3122 #ifdef CONFIG_PROC_FS
3123 bond_remove_proc_entry(bond);
3124 bond_create_proc_entry(bond);
3126 down_write(&(bonding_rwsem));
3127 bond_destroy_sysfs_entry(bond);
3128 bond_create_sysfs_entry(bond);
3129 up_write(&(bonding_rwsem));
3133 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3135 struct bonding *event_bond = bond_dev->priv;
3138 case NETDEV_CHANGENAME:
3139 return bond_event_changename(event_bond);
3140 case NETDEV_UNREGISTER:
3142 * TODO: remove a bond from the list?
3152 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3154 struct net_device *bond_dev = slave_dev->master;
3155 struct bonding *bond = bond_dev->priv;
3158 case NETDEV_UNREGISTER:
3160 bond_release(bond_dev, slave_dev);
3165 * TODO: is this what we get if somebody
3166 * sets up a hierarchical bond, then rmmod's
3167 * one of the slave bonding devices?
3172 * ... Or is it this?
3175 case NETDEV_CHANGEMTU:
3177 * TODO: Should slaves be allowed to
3178 * independently alter their MTU? For
3179 * an active-backup bond, slaves need
3180 * not be the same type of device, so
3181 * MTUs may vary. For other modes,
3182 * slaves arguably should have the
3183 * same MTUs. To do this, we'd need to
3184 * take over the slave's change_mtu
3185 * function for the duration of their
3189 case NETDEV_CHANGENAME:
3191 * TODO: handle changing the primary's name
3194 case NETDEV_FEAT_CHANGE:
3195 bond_compute_features(bond);
3205 * bond_netdev_event: handle netdev notifier chain events.
3207 * This function receives events for the netdev chain. The caller (an
3208 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3209 * locks for us to safely manipulate the slave devices (RTNL lock,
3212 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3214 struct net_device *event_dev = (struct net_device *)ptr;
3216 dprintk("event_dev: %s, event: %lx\n",
3217 (event_dev ? event_dev->name : "None"),
3220 if (!(event_dev->priv_flags & IFF_BONDING))
3223 if (event_dev->flags & IFF_MASTER) {
3224 dprintk("IFF_MASTER\n");
3225 return bond_master_netdev_event(event, event_dev);
3228 if (event_dev->flags & IFF_SLAVE) {
3229 dprintk("IFF_SLAVE\n");
3230 return bond_slave_netdev_event(event, event_dev);
3237 * bond_inetaddr_event: handle inetaddr notifier chain events.
3239 * We keep track of device IPs primarily to use as source addresses in
3240 * ARP monitor probes (rather than spewing out broadcasts all the time).
3242 * We track one IP for the main device (if it has one), plus one per VLAN.
3244 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3246 struct in_ifaddr *ifa = ptr;
3247 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3248 struct bonding *bond, *bond_next;
3249 struct vlan_entry *vlan, *vlan_next;
3251 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3252 if (bond->dev == event_dev) {
3255 bond->master_ip = ifa->ifa_local;
3258 bond->master_ip = bond_glean_dev_ip(bond->dev);
3265 if (list_empty(&bond->vlan_list))
3268 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3270 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
3271 if (vlan_dev == event_dev) {
3274 vlan->vlan_ip = ifa->ifa_local;
3278 bond_glean_dev_ip(vlan_dev);
3289 static struct notifier_block bond_netdev_notifier = {
3290 .notifier_call = bond_netdev_event,
3293 static struct notifier_block bond_inetaddr_notifier = {
3294 .notifier_call = bond_inetaddr_event,
3297 /*-------------------------- Packet type handling ---------------------------*/
3299 /* register to receive lacpdus on a bond */
3300 static void bond_register_lacpdu(struct bonding *bond)
3302 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3304 /* initialize packet type */
3305 pk_type->type = PKT_TYPE_LACPDU;
3306 pk_type->dev = bond->dev;
3307 pk_type->func = bond_3ad_lacpdu_recv;
3309 dev_add_pack(pk_type);
3312 /* unregister to receive lacpdus on a bond */
3313 static void bond_unregister_lacpdu(struct bonding *bond)
3315 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3318 /*---------------------------- Hashing Policies -----------------------------*/
3321 * Hash for the the output device based upon layer 3 and layer 4 data. If
3322 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3323 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3325 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3326 struct net_device *bond_dev, int count)
3328 struct ethhdr *data = (struct ethhdr *)skb->data;
3329 struct iphdr *iph = skb->nh.iph;
3330 u16 *layer4hdr = (u16 *)((u32 *)iph + iph->ihl);
3333 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3334 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3335 (iph->protocol == IPPROTO_TCP ||
3336 iph->protocol == IPPROTO_UDP)) {
3337 layer4_xor = htons((*layer4hdr ^ *(layer4hdr + 1)));
3339 return (layer4_xor ^
3340 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3344 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3348 * Hash for the output device based upon layer 2 data
3350 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3351 struct net_device *bond_dev, int count)
3353 struct ethhdr *data = (struct ethhdr *)skb->data;
3355 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3358 /*-------------------------- Device entry points ----------------------------*/
3360 static int bond_open(struct net_device *bond_dev)
3362 struct bonding *bond = bond_dev->priv;
3363 struct timer_list *mii_timer = &bond->mii_timer;
3364 struct timer_list *arp_timer = &bond->arp_timer;
3366 bond->kill_timers = 0;
3368 if ((bond->params.mode == BOND_MODE_TLB) ||
3369 (bond->params.mode == BOND_MODE_ALB)) {
3370 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3372 /* bond_alb_initialize must be called before the timer
3375 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3376 /* something went wrong - fail the open operation */
3380 init_timer(alb_timer);
3381 alb_timer->expires = jiffies + 1;
3382 alb_timer->data = (unsigned long)bond;
3383 alb_timer->function = (void *)&bond_alb_monitor;
3384 add_timer(alb_timer);
3387 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3388 init_timer(mii_timer);
3389 mii_timer->expires = jiffies + 1;
3390 mii_timer->data = (unsigned long)bond_dev;
3391 mii_timer->function = (void *)&bond_mii_monitor;
3392 add_timer(mii_timer);
3395 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3396 init_timer(arp_timer);
3397 arp_timer->expires = jiffies + 1;
3398 arp_timer->data = (unsigned long)bond_dev;
3399 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3400 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3402 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3404 add_timer(arp_timer);
3407 if (bond->params.mode == BOND_MODE_8023AD) {
3408 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3409 init_timer(ad_timer);
3410 ad_timer->expires = jiffies + 1;
3411 ad_timer->data = (unsigned long)bond;
3412 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3413 add_timer(ad_timer);
3415 /* register to receive LACPDUs */
3416 bond_register_lacpdu(bond);
3422 static int bond_close(struct net_device *bond_dev)
3424 struct bonding *bond = bond_dev->priv;
3426 if (bond->params.mode == BOND_MODE_8023AD) {
3427 /* Unregister the receive of LACPDUs */
3428 bond_unregister_lacpdu(bond);
3431 write_lock_bh(&bond->lock);
3434 /* signal timers not to re-arm */
3435 bond->kill_timers = 1;
3437 write_unlock_bh(&bond->lock);
3439 /* del_timer_sync must run without holding the bond->lock
3440 * because a running timer might be trying to hold it too
3443 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3444 del_timer_sync(&bond->mii_timer);
3447 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3448 del_timer_sync(&bond->arp_timer);
3451 switch (bond->params.mode) {
3452 case BOND_MODE_8023AD:
3453 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3457 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3464 if ((bond->params.mode == BOND_MODE_TLB) ||
3465 (bond->params.mode == BOND_MODE_ALB)) {
3466 /* Must be called only after all
3467 * slaves have been released
3469 bond_alb_deinitialize(bond);
3475 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3477 struct bonding *bond = bond_dev->priv;
3478 struct net_device_stats *stats = &(bond->stats), *sstats;
3479 struct slave *slave;
3482 memset(stats, 0, sizeof(struct net_device_stats));
3484 read_lock_bh(&bond->lock);
3486 bond_for_each_slave(bond, slave, i) {
3487 sstats = slave->dev->get_stats(slave->dev);
3489 stats->rx_packets += sstats->rx_packets;
3490 stats->rx_bytes += sstats->rx_bytes;
3491 stats->rx_errors += sstats->rx_errors;
3492 stats->rx_dropped += sstats->rx_dropped;
3494 stats->tx_packets += sstats->tx_packets;
3495 stats->tx_bytes += sstats->tx_bytes;
3496 stats->tx_errors += sstats->tx_errors;
3497 stats->tx_dropped += sstats->tx_dropped;
3499 stats->multicast += sstats->multicast;
3500 stats->collisions += sstats->collisions;
3502 stats->rx_length_errors += sstats->rx_length_errors;
3503 stats->rx_over_errors += sstats->rx_over_errors;
3504 stats->rx_crc_errors += sstats->rx_crc_errors;
3505 stats->rx_frame_errors += sstats->rx_frame_errors;
3506 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3507 stats->rx_missed_errors += sstats->rx_missed_errors;
3509 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3510 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3511 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3512 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3513 stats->tx_window_errors += sstats->tx_window_errors;
3516 read_unlock_bh(&bond->lock);
3521 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3523 struct net_device *slave_dev = NULL;
3524 struct ifbond k_binfo;
3525 struct ifbond __user *u_binfo = NULL;
3526 struct ifslave k_sinfo;
3527 struct ifslave __user *u_sinfo = NULL;
3528 struct mii_ioctl_data *mii = NULL;
3531 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3532 bond_dev->name, cmd);
3544 * We do this again just in case we were called by SIOCGMIIREG
3545 * instead of SIOCGMIIPHY.
3552 if (mii->reg_num == 1) {
3553 struct bonding *bond = bond_dev->priv;
3555 read_lock_bh(&bond->lock);
3556 read_lock(&bond->curr_slave_lock);
3557 if (bond->curr_active_slave) {
3558 mii->val_out = BMSR_LSTATUS;
3560 read_unlock(&bond->curr_slave_lock);
3561 read_unlock_bh(&bond->lock);
3565 case BOND_INFO_QUERY_OLD:
3566 case SIOCBONDINFOQUERY:
3567 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3569 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3573 res = bond_info_query(bond_dev, &k_binfo);
3575 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3581 case BOND_SLAVE_INFO_QUERY_OLD:
3582 case SIOCBONDSLAVEINFOQUERY:
3583 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3585 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3589 res = bond_slave_info_query(bond_dev, &k_sinfo);
3591 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3602 if (!capable(CAP_NET_ADMIN)) {
3606 down_write(&(bonding_rwsem));
3607 slave_dev = dev_get_by_name(ifr->ifr_slave);
3609 dprintk("slave_dev=%p: \n", slave_dev);
3614 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3616 case BOND_ENSLAVE_OLD:
3617 case SIOCBONDENSLAVE:
3618 res = bond_enslave(bond_dev, slave_dev);
3620 case BOND_RELEASE_OLD:
3621 case SIOCBONDRELEASE:
3622 res = bond_release(bond_dev, slave_dev);
3624 case BOND_SETHWADDR_OLD:
3625 case SIOCBONDSETHWADDR:
3626 res = bond_sethwaddr(bond_dev, slave_dev);
3628 case BOND_CHANGE_ACTIVE_OLD:
3629 case SIOCBONDCHANGEACTIVE:
3630 res = bond_ioctl_change_active(bond_dev, slave_dev);
3639 up_write(&(bonding_rwsem));
3643 static void bond_set_multicast_list(struct net_device *bond_dev)
3645 struct bonding *bond = bond_dev->priv;
3646 struct dev_mc_list *dmi;
3648 write_lock_bh(&bond->lock);
3651 * Do promisc before checking multicast_mode
3653 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3654 bond_set_promiscuity(bond, 1);
3657 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3658 bond_set_promiscuity(bond, -1);
3661 /* set allmulti flag to slaves */
3662 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3663 bond_set_allmulti(bond, 1);
3666 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3667 bond_set_allmulti(bond, -1);
3670 bond->flags = bond_dev->flags;
3672 /* looking for addresses to add to slaves' mc list */
3673 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3674 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3675 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3679 /* looking for addresses to delete from slaves' list */
3680 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3681 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3682 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3686 /* save master's multicast list */
3687 bond_mc_list_destroy(bond);
3688 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3690 write_unlock_bh(&bond->lock);
3694 * Change the MTU of all of a master's slaves to match the master
3696 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3698 struct bonding *bond = bond_dev->priv;
3699 struct slave *slave, *stop_at;
3703 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3704 (bond_dev ? bond_dev->name : "None"), new_mtu);
3706 /* Can't hold bond->lock with bh disabled here since
3707 * some base drivers panic. On the other hand we can't
3708 * hold bond->lock without bh disabled because we'll
3709 * deadlock. The only solution is to rely on the fact
3710 * that we're under rtnl_lock here, and the slaves
3711 * list won't change. This doesn't solve the problem
3712 * of setting the slave's MTU while it is
3713 * transmitting, but the assumption is that the base
3714 * driver can handle that.
3716 * TODO: figure out a way to safely iterate the slaves
3717 * list, but without holding a lock around the actual
3718 * call to the base driver.
3721 bond_for_each_slave(bond, slave, i) {
3722 dprintk("s %p s->p %p c_m %p\n", slave,
3723 slave->prev, slave->dev->change_mtu);
3725 res = dev_set_mtu(slave->dev, new_mtu);
3728 /* If we failed to set the slave's mtu to the new value
3729 * we must abort the operation even in ACTIVE_BACKUP
3730 * mode, because if we allow the backup slaves to have
3731 * different mtu values than the active slave we'll
3732 * need to change their mtu when doing a failover. That
3733 * means changing their mtu from timer context, which
3734 * is probably not a good idea.
3736 dprintk("err %d %s\n", res, slave->dev->name);
3741 bond_dev->mtu = new_mtu;
3746 /* unwind from head to the slave that failed */
3748 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3751 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3753 dprintk("unwind err %d dev %s\n", tmp_res,
3764 * Note that many devices must be down to change the HW address, and
3765 * downing the master releases all slaves. We can make bonds full of
3766 * bonding devices to test this, however.
3768 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3770 struct bonding *bond = bond_dev->priv;
3771 struct sockaddr *sa = addr, tmp_sa;
3772 struct slave *slave, *stop_at;
3776 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3778 if (!is_valid_ether_addr(sa->sa_data)) {
3779 return -EADDRNOTAVAIL;
3782 /* Can't hold bond->lock with bh disabled here since
3783 * some base drivers panic. On the other hand we can't
3784 * hold bond->lock without bh disabled because we'll
3785 * deadlock. The only solution is to rely on the fact
3786 * that we're under rtnl_lock here, and the slaves
3787 * list won't change. This doesn't solve the problem
3788 * of setting the slave's hw address while it is
3789 * transmitting, but the assumption is that the base
3790 * driver can handle that.
3792 * TODO: figure out a way to safely iterate the slaves
3793 * list, but without holding a lock around the actual
3794 * call to the base driver.
3797 bond_for_each_slave(bond, slave, i) {
3798 dprintk("slave %p %s\n", slave, slave->dev->name);
3800 if (slave->dev->set_mac_address == NULL) {
3802 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
3806 res = dev_set_mac_address(slave->dev, addr);
3808 /* TODO: consider downing the slave
3810 * User should expect communications
3811 * breakage anyway until ARP finish
3814 dprintk("err %d %s\n", res, slave->dev->name);
3820 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3824 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3825 tmp_sa.sa_family = bond_dev->type;
3827 /* unwind from head to the slave that failed */
3829 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3832 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3834 dprintk("unwind err %d dev %s\n", tmp_res,
3842 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3844 struct bonding *bond = bond_dev->priv;
3845 struct slave *slave, *start_at;
3849 read_lock(&bond->lock);
3851 if (!BOND_IS_OK(bond)) {
3855 read_lock(&bond->curr_slave_lock);
3856 slave = start_at = bond->curr_active_slave;
3857 read_unlock(&bond->curr_slave_lock);
3863 bond_for_each_slave_from(bond, slave, i, start_at) {
3864 if (IS_UP(slave->dev) &&
3865 (slave->link == BOND_LINK_UP) &&
3866 (slave->state == BOND_STATE_ACTIVE)) {
3867 res = bond_dev_queue_xmit(bond, skb, slave->dev);
3869 write_lock(&bond->curr_slave_lock);
3870 bond->curr_active_slave = slave->next;
3871 write_unlock(&bond->curr_slave_lock);
3880 /* no suitable interface, frame not sent */
3883 read_unlock(&bond->lock);
3887 static void bond_activebackup_xmit_copy(struct sk_buff *skb,
3888 struct bonding *bond,
3889 struct slave *slave)
3891 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
3892 struct ethhdr *eth_data;
3897 printk(KERN_ERR DRV_NAME ": Error: "
3898 "bond_activebackup_xmit_copy(): skb_copy() failed\n");
3902 skb2->mac.raw = (unsigned char *)skb2->data;
3903 eth_data = eth_hdr(skb2);
3905 /* Pick an appropriate source MAC address
3906 * -- use slave's perm MAC addr, unless used by bond
3907 * -- otherwise, borrow active slave's perm MAC addr
3908 * since that will not be used
3910 hwaddr = slave->perm_hwaddr;
3911 if (!memcmp(eth_data->h_source, hwaddr, ETH_ALEN))
3912 hwaddr = bond->curr_active_slave->perm_hwaddr;
3914 /* Set source MAC address appropriately */
3915 memcpy(eth_data->h_source, hwaddr, ETH_ALEN);
3917 res = bond_dev_queue_xmit(bond, skb2, slave->dev);
3919 dev_kfree_skb(skb2);
3925 * in active-backup mode, we know that bond->curr_active_slave is always valid if
3926 * the bond has a usable interface.
3928 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
3930 struct bonding *bond = bond_dev->priv;
3933 read_lock(&bond->lock);
3934 read_lock(&bond->curr_slave_lock);
3936 if (!BOND_IS_OK(bond)) {
3940 if (!bond->curr_active_slave)
3943 /* Xmit IGMP frames on all slaves to ensure rapid fail-over
3944 for multicast traffic on snooping switches */
3945 if (skb->protocol == __constant_htons(ETH_P_IP) &&
3946 skb->nh.iph->protocol == IPPROTO_IGMP) {
3947 struct slave *slave, *active_slave;
3950 active_slave = bond->curr_active_slave;
3951 bond_for_each_slave_from_to(bond, slave, i, active_slave->next,
3953 if (IS_UP(slave->dev) &&
3954 (slave->link == BOND_LINK_UP))
3955 bond_activebackup_xmit_copy(skb, bond, slave);
3958 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
3962 /* no suitable interface, frame not sent */
3965 read_unlock(&bond->curr_slave_lock);
3966 read_unlock(&bond->lock);
3971 * In bond_xmit_xor() , we determine the output device by using a pre-
3972 * determined xmit_hash_policy(), If the selected device is not enabled,
3973 * find the next active slave.
3975 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
3977 struct bonding *bond = bond_dev->priv;
3978 struct slave *slave, *start_at;
3983 read_lock(&bond->lock);
3985 if (!BOND_IS_OK(bond)) {
3989 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
3991 bond_for_each_slave(bond, slave, i) {
4000 bond_for_each_slave_from(bond, slave, i, start_at) {
4001 if (IS_UP(slave->dev) &&
4002 (slave->link == BOND_LINK_UP) &&
4003 (slave->state == BOND_STATE_ACTIVE)) {
4004 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4011 /* no suitable interface, frame not sent */
4014 read_unlock(&bond->lock);
4019 * in broadcast mode, we send everything to all usable interfaces.
4021 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4023 struct bonding *bond = bond_dev->priv;
4024 struct slave *slave, *start_at;
4025 struct net_device *tx_dev = NULL;
4029 read_lock(&bond->lock);
4031 if (!BOND_IS_OK(bond)) {
4035 read_lock(&bond->curr_slave_lock);
4036 start_at = bond->curr_active_slave;
4037 read_unlock(&bond->curr_slave_lock);
4043 bond_for_each_slave_from(bond, slave, i, start_at) {
4044 if (IS_UP(slave->dev) &&
4045 (slave->link == BOND_LINK_UP) &&
4046 (slave->state == BOND_STATE_ACTIVE)) {
4048 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4050 printk(KERN_ERR DRV_NAME
4051 ": %s: Error: bond_xmit_broadcast(): "
4052 "skb_clone() failed\n",
4057 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4059 dev_kfree_skb(skb2);
4063 tx_dev = slave->dev;
4068 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4073 /* no suitable interface, frame not sent */
4076 /* frame sent to all suitable interfaces */
4077 read_unlock(&bond->lock);
4081 /*------------------------- Device initialization ---------------------------*/
4084 * set bond mode specific net device operations
4086 void bond_set_mode_ops(struct bonding *bond, int mode)
4088 struct net_device *bond_dev = bond->dev;
4091 case BOND_MODE_ROUNDROBIN:
4092 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4094 case BOND_MODE_ACTIVEBACKUP:
4095 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4098 bond_dev->hard_start_xmit = bond_xmit_xor;
4099 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4100 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4102 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4104 case BOND_MODE_BROADCAST:
4105 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4107 case BOND_MODE_8023AD:
4108 bond_set_master_3ad_flags(bond);
4109 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4110 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4111 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4113 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4116 bond_set_master_alb_flags(bond);
4119 bond_dev->hard_start_xmit = bond_alb_xmit;
4120 bond_dev->set_mac_address = bond_alb_set_mac_address;
4123 /* Should never happen, mode already checked */
4124 printk(KERN_ERR DRV_NAME
4125 ": %s: Error: Unknown bonding mode %d\n",
4132 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4133 struct ethtool_drvinfo *drvinfo)
4135 strncpy(drvinfo->driver, DRV_NAME, 32);
4136 strncpy(drvinfo->version, DRV_VERSION, 32);
4137 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4140 static const struct ethtool_ops bond_ethtool_ops = {
4141 .get_tx_csum = ethtool_op_get_tx_csum,
4142 .get_tso = ethtool_op_get_tso,
4143 .get_ufo = ethtool_op_get_ufo,
4144 .get_sg = ethtool_op_get_sg,
4145 .get_drvinfo = bond_ethtool_get_drvinfo,
4149 * Does not allocate but creates a /proc entry.
4152 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4154 struct bonding *bond = bond_dev->priv;
4156 dprintk("Begin bond_init for %s\n", bond_dev->name);
4158 /* initialize rwlocks */
4159 rwlock_init(&bond->lock);
4160 rwlock_init(&bond->curr_slave_lock);
4162 bond->params = *params; /* copy params struct */
4164 /* Initialize pointers */
4165 bond->first_slave = NULL;
4166 bond->curr_active_slave = NULL;
4167 bond->current_arp_slave = NULL;
4168 bond->primary_slave = NULL;
4169 bond->dev = bond_dev;
4170 INIT_LIST_HEAD(&bond->vlan_list);
4172 /* Initialize the device entry points */
4173 bond_dev->open = bond_open;
4174 bond_dev->stop = bond_close;
4175 bond_dev->get_stats = bond_get_stats;
4176 bond_dev->do_ioctl = bond_do_ioctl;
4177 bond_dev->ethtool_ops = &bond_ethtool_ops;
4178 bond_dev->set_multicast_list = bond_set_multicast_list;
4179 bond_dev->change_mtu = bond_change_mtu;
4180 bond_dev->set_mac_address = bond_set_mac_address;
4182 bond_set_mode_ops(bond, bond->params.mode);
4184 bond_dev->destructor = free_netdev;
4186 /* Initialize the device options */
4187 bond_dev->tx_queue_len = 0;
4188 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4189 bond_dev->priv_flags |= IFF_BONDING;
4191 /* At first, we block adding VLANs. That's the only way to
4192 * prevent problems that occur when adding VLANs over an
4193 * empty bond. The block will be removed once non-challenged
4194 * slaves are enslaved.
4196 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4198 /* don't acquire bond device's netif_tx_lock when
4200 bond_dev->features |= NETIF_F_LLTX;
4202 /* By default, we declare the bond to be fully
4203 * VLAN hardware accelerated capable. Special
4204 * care is taken in the various xmit functions
4205 * when there are slaves that are not hw accel
4208 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4209 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4210 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4211 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4212 NETIF_F_HW_VLAN_RX |
4213 NETIF_F_HW_VLAN_FILTER);
4215 #ifdef CONFIG_PROC_FS
4216 bond_create_proc_entry(bond);
4219 list_add_tail(&bond->bond_list, &bond_dev_list);
4224 /* De-initialize device specific data.
4225 * Caller must hold rtnl_lock.
4227 void bond_deinit(struct net_device *bond_dev)
4229 struct bonding *bond = bond_dev->priv;
4231 list_del(&bond->bond_list);
4233 #ifdef CONFIG_PROC_FS
4234 bond_remove_proc_entry(bond);
4238 /* Unregister and free all bond devices.
4239 * Caller must hold rtnl_lock.
4241 static void bond_free_all(void)
4243 struct bonding *bond, *nxt;
4245 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4246 struct net_device *bond_dev = bond->dev;
4248 bond_mc_list_destroy(bond);
4249 /* Release the bonded slaves */
4250 bond_release_all(bond_dev);
4251 unregister_netdevice(bond_dev);
4252 bond_deinit(bond_dev);
4255 #ifdef CONFIG_PROC_FS
4256 bond_destroy_proc_dir();
4260 /*------------------------- Module initialization ---------------------------*/
4263 * Convert string input module parms. Accept either the
4264 * number of the mode or its string name.
4266 int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4270 for (i = 0; tbl[i].modename; i++) {
4271 if ((isdigit(*mode_arg) &&
4272 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4273 (strncmp(mode_arg, tbl[i].modename,
4274 strlen(tbl[i].modename)) == 0)) {
4282 static int bond_check_params(struct bond_params *params)
4285 * Convert string parameters.
4288 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4289 if (bond_mode == -1) {
4290 printk(KERN_ERR DRV_NAME
4291 ": Error: Invalid bonding mode \"%s\"\n",
4292 mode == NULL ? "NULL" : mode);
4297 if (xmit_hash_policy) {
4298 if ((bond_mode != BOND_MODE_XOR) &&
4299 (bond_mode != BOND_MODE_8023AD)) {
4300 printk(KERN_INFO DRV_NAME
4301 ": xor_mode param is irrelevant in mode %s\n",
4302 bond_mode_name(bond_mode));
4304 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4306 if (xmit_hashtype == -1) {
4307 printk(KERN_ERR DRV_NAME
4308 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4309 xmit_hash_policy == NULL ? "NULL" :
4317 if (bond_mode != BOND_MODE_8023AD) {
4318 printk(KERN_INFO DRV_NAME
4319 ": lacp_rate param is irrelevant in mode %s\n",
4320 bond_mode_name(bond_mode));
4322 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4323 if (lacp_fast == -1) {
4324 printk(KERN_ERR DRV_NAME
4325 ": Error: Invalid lacp rate \"%s\"\n",
4326 lacp_rate == NULL ? "NULL" : lacp_rate);
4332 if (max_bonds < 1 || max_bonds > INT_MAX) {
4333 printk(KERN_WARNING DRV_NAME
4334 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4335 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4336 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4337 max_bonds = BOND_DEFAULT_MAX_BONDS;
4341 printk(KERN_WARNING DRV_NAME
4342 ": Warning: miimon module parameter (%d), "
4343 "not in range 0-%d, so it was reset to %d\n",
4344 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4345 miimon = BOND_LINK_MON_INTERV;
4349 printk(KERN_WARNING DRV_NAME
4350 ": Warning: updelay module parameter (%d), "
4351 "not in range 0-%d, so it was reset to 0\n",
4356 if (downdelay < 0) {
4357 printk(KERN_WARNING DRV_NAME
4358 ": Warning: downdelay module parameter (%d), "
4359 "not in range 0-%d, so it was reset to 0\n",
4360 downdelay, INT_MAX);
4364 if ((use_carrier != 0) && (use_carrier != 1)) {
4365 printk(KERN_WARNING DRV_NAME
4366 ": Warning: use_carrier module parameter (%d), "
4367 "not of valid value (0/1), so it was set to 1\n",
4372 /* reset values for 802.3ad */
4373 if (bond_mode == BOND_MODE_8023AD) {
4375 printk(KERN_WARNING DRV_NAME
4376 ": Warning: miimon must be specified, "
4377 "otherwise bonding will not detect link "
4378 "failure, speed and duplex which are "
4379 "essential for 802.3ad operation\n");
4380 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4385 /* reset values for TLB/ALB */
4386 if ((bond_mode == BOND_MODE_TLB) ||
4387 (bond_mode == BOND_MODE_ALB)) {
4389 printk(KERN_WARNING DRV_NAME
4390 ": Warning: miimon must be specified, "
4391 "otherwise bonding will not detect link "
4392 "failure and link speed which are essential "
4393 "for TLB/ALB load balancing\n");
4394 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4399 if (bond_mode == BOND_MODE_ALB) {
4400 printk(KERN_NOTICE DRV_NAME
4401 ": In ALB mode you might experience client "
4402 "disconnections upon reconnection of a link if the "
4403 "bonding module updelay parameter (%d msec) is "
4404 "incompatible with the forwarding delay time of the "
4410 if (updelay || downdelay) {
4411 /* just warn the user the up/down delay will have
4412 * no effect since miimon is zero...
4414 printk(KERN_WARNING DRV_NAME
4415 ": Warning: miimon module parameter not set "
4416 "and updelay (%d) or downdelay (%d) module "
4417 "parameter is set; updelay and downdelay have "
4418 "no effect unless miimon is set\n",
4419 updelay, downdelay);
4422 /* don't allow arp monitoring */
4424 printk(KERN_WARNING DRV_NAME
4425 ": Warning: miimon (%d) and arp_interval (%d) "
4426 "can't be used simultaneously, disabling ARP "
4428 miimon, arp_interval);
4432 if ((updelay % miimon) != 0) {
4433 printk(KERN_WARNING DRV_NAME
4434 ": Warning: updelay (%d) is not a multiple "
4435 "of miimon (%d), updelay rounded to %d ms\n",
4436 updelay, miimon, (updelay / miimon) * miimon);
4441 if ((downdelay % miimon) != 0) {
4442 printk(KERN_WARNING DRV_NAME
4443 ": Warning: downdelay (%d) is not a multiple "
4444 "of miimon (%d), downdelay rounded to %d ms\n",
4446 (downdelay / miimon) * miimon);
4449 downdelay /= miimon;
4452 if (arp_interval < 0) {
4453 printk(KERN_WARNING DRV_NAME
4454 ": Warning: arp_interval module parameter (%d) "
4455 ", not in range 0-%d, so it was reset to %d\n",
4456 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4457 arp_interval = BOND_LINK_ARP_INTERV;
4460 for (arp_ip_count = 0;
4461 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4463 /* not complete check, but should be good enough to
4465 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4466 printk(KERN_WARNING DRV_NAME
4467 ": Warning: bad arp_ip_target module parameter "
4468 "(%s), ARP monitoring will not be performed\n",
4469 arp_ip_target[arp_ip_count]);
4472 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4473 arp_target[arp_ip_count] = ip;
4477 if (arp_interval && !arp_ip_count) {
4478 /* don't allow arping if no arp_ip_target given... */
4479 printk(KERN_WARNING DRV_NAME
4480 ": Warning: arp_interval module parameter (%d) "
4481 "specified without providing an arp_ip_target "
4482 "parameter, arp_interval was reset to 0\n",
4488 printk(KERN_INFO DRV_NAME
4489 ": MII link monitoring set to %d ms\n",
4491 } else if (arp_interval) {
4494 printk(KERN_INFO DRV_NAME
4495 ": ARP monitoring set to %d ms with %d target(s):",
4496 arp_interval, arp_ip_count);
4498 for (i = 0; i < arp_ip_count; i++)
4499 printk (" %s", arp_ip_target[i]);
4504 /* miimon and arp_interval not set, we need one so things
4505 * work as expected, see bonding.txt for details
4507 printk(KERN_WARNING DRV_NAME
4508 ": Warning: either miimon or arp_interval and "
4509 "arp_ip_target module parameters must be specified, "
4510 "otherwise bonding will not detect link failures! see "
4511 "bonding.txt for details.\n");
4514 if (primary && !USES_PRIMARY(bond_mode)) {
4515 /* currently, using a primary only makes sense
4516 * in active backup, TLB or ALB modes
4518 printk(KERN_WARNING DRV_NAME
4519 ": Warning: %s primary device specified but has no "
4520 "effect in %s mode\n",
4521 primary, bond_mode_name(bond_mode));
4525 /* fill params struct with the proper values */
4526 params->mode = bond_mode;
4527 params->xmit_policy = xmit_hashtype;
4528 params->miimon = miimon;
4529 params->arp_interval = arp_interval;
4530 params->updelay = updelay;
4531 params->downdelay = downdelay;
4532 params->use_carrier = use_carrier;
4533 params->lacp_fast = lacp_fast;
4534 params->primary[0] = 0;
4537 strncpy(params->primary, primary, IFNAMSIZ);
4538 params->primary[IFNAMSIZ - 1] = 0;
4541 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4546 /* Create a new bond based on the specified name and bonding parameters.
4547 * Caller must NOT hold rtnl_lock; we need to release it here before we
4548 * set up our sysfs entries.
4550 int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4552 struct net_device *bond_dev;
4556 bond_dev = alloc_netdev(sizeof(struct bonding), name, ether_setup);
4558 printk(KERN_ERR DRV_NAME
4559 ": %s: eek! can't alloc netdev!\n",
4565 /* bond_init() must be called after dev_alloc_name() (for the
4566 * /proc files), but before register_netdevice(), because we
4567 * need to set function pointers.
4570 res = bond_init(bond_dev, params);
4575 SET_MODULE_OWNER(bond_dev);
4577 res = register_netdevice(bond_dev);
4582 *newbond = bond_dev->priv;
4584 netif_carrier_off(bond_dev);
4586 rtnl_unlock(); /* allows sysfs registration of net device */
4587 res = bond_create_sysfs_entry(bond_dev->priv);
4590 bond_deinit(bond_dev);
4592 free_netdev(bond_dev);
4599 static int __init bonding_init(void)
4603 char new_bond_name[8]; /* Enough room for 999 bonds at init. */
4605 printk(KERN_INFO "%s", version);
4607 res = bond_check_params(&bonding_defaults);
4612 #ifdef CONFIG_PROC_FS
4613 bond_create_proc_dir();
4615 for (i = 0; i < max_bonds; i++) {
4616 sprintf(new_bond_name, "bond%d",i);
4617 res = bond_create(new_bond_name,&bonding_defaults, NULL);
4622 res = bond_create_sysfs();
4626 register_netdevice_notifier(&bond_netdev_notifier);
4627 register_inetaddr_notifier(&bond_inetaddr_notifier);
4633 bond_destroy_sysfs();
4640 static void __exit bonding_exit(void)
4642 unregister_netdevice_notifier(&bond_netdev_notifier);
4643 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4647 bond_destroy_sysfs();
4651 module_init(bonding_init);
4652 module_exit(bonding_exit);
4653 MODULE_LICENSE("GPL");
4654 MODULE_VERSION(DRV_VERSION);
4655 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4656 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4657 MODULE_SUPPORTED_DEVICE("most ethernet devices");