2 * originally based on the dummy device.
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
7 * bonding.c: an Ethernet Bonding driver
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
14 * and probably many L2 switches ...
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
24 * will release all slaves, marking them as down.
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
34 //#define BONDING_DEBUG 1
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
46 #include <linux/tcp.h>
47 #include <linux/udp.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <linux/timer.h>
52 #include <linux/socket.h>
53 #include <linux/ctype.h>
54 #include <linux/inet.h>
55 #include <linux/bitops.h>
56 #include <asm/system.h>
59 #include <asm/uaccess.h>
60 #include <linux/errno.h>
61 #include <linux/netdevice.h>
62 #include <linux/inetdevice.h>
63 #include <linux/igmp.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
67 #include <linux/rtnetlink.h>
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
70 #include <linux/smp.h>
71 #include <linux/if_ether.h>
73 #include <linux/mii.h>
74 #include <linux/ethtool.h>
75 #include <linux/if_vlan.h>
76 #include <linux/if_bonding.h>
77 #include <net/route.h>
78 #include <net/net_namespace.h>
83 /*---------------------------- Module parameters ----------------------------*/
85 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
86 #define BOND_LINK_MON_INTERV 0
87 #define BOND_LINK_ARP_INTERV 0
89 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
90 static int miimon = BOND_LINK_MON_INTERV;
91 static int updelay = 0;
92 static int downdelay = 0;
93 static int use_carrier = 1;
94 static char *mode = NULL;
95 static char *primary = NULL;
96 static char *lacp_rate = NULL;
97 static char *xmit_hash_policy = NULL;
98 static int arp_interval = BOND_LINK_ARP_INTERV;
99 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
100 static char *arp_validate = NULL;
101 struct bond_params bonding_defaults;
103 module_param(max_bonds, int, 0);
104 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
105 module_param(miimon, int, 0);
106 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
107 module_param(updelay, int, 0);
108 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
109 module_param(downdelay, int, 0);
110 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
112 module_param(use_carrier, int, 0);
113 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
114 "0 for off, 1 for on (default)");
115 module_param(mode, charp, 0);
116 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
117 "1 for active-backup, 2 for balance-xor, "
118 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
119 "6 for balance-alb");
120 module_param(primary, charp, 0);
121 MODULE_PARM_DESC(primary, "Primary network device to use");
122 module_param(lacp_rate, charp, 0);
123 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
125 module_param(xmit_hash_policy, charp, 0);
126 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
127 ", 1 for layer 3+4");
128 module_param(arp_interval, int, 0);
129 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
130 module_param_array(arp_ip_target, charp, NULL, 0);
131 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
132 module_param(arp_validate, charp, 0);
133 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
135 /*----------------------------- Global variables ----------------------------*/
137 static const char * const version =
138 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
140 LIST_HEAD(bond_dev_list);
142 #ifdef CONFIG_PROC_FS
143 static struct proc_dir_entry *bond_proc_dir = NULL;
146 extern struct rw_semaphore bonding_rwsem;
147 static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
148 static int arp_ip_count = 0;
149 static int bond_mode = BOND_MODE_ROUNDROBIN;
150 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
151 static int lacp_fast = 0;
154 struct bond_parm_tbl bond_lacp_tbl[] = {
155 { "slow", AD_LACP_SLOW},
156 { "fast", AD_LACP_FAST},
160 struct bond_parm_tbl bond_mode_tbl[] = {
161 { "balance-rr", BOND_MODE_ROUNDROBIN},
162 { "active-backup", BOND_MODE_ACTIVEBACKUP},
163 { "balance-xor", BOND_MODE_XOR},
164 { "broadcast", BOND_MODE_BROADCAST},
165 { "802.3ad", BOND_MODE_8023AD},
166 { "balance-tlb", BOND_MODE_TLB},
167 { "balance-alb", BOND_MODE_ALB},
171 struct bond_parm_tbl xmit_hashtype_tbl[] = {
172 { "layer2", BOND_XMIT_POLICY_LAYER2},
173 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
177 struct bond_parm_tbl arp_validate_tbl[] = {
178 { "none", BOND_ARP_VALIDATE_NONE},
179 { "active", BOND_ARP_VALIDATE_ACTIVE},
180 { "backup", BOND_ARP_VALIDATE_BACKUP},
181 { "all", BOND_ARP_VALIDATE_ALL},
185 /*-------------------------- Forward declarations ---------------------------*/
187 static void bond_send_gratuitous_arp(struct bonding *bond);
189 /*---------------------------- General routines -----------------------------*/
191 static const char *bond_mode_name(int mode)
194 case BOND_MODE_ROUNDROBIN :
195 return "load balancing (round-robin)";
196 case BOND_MODE_ACTIVEBACKUP :
197 return "fault-tolerance (active-backup)";
199 return "load balancing (xor)";
200 case BOND_MODE_BROADCAST :
201 return "fault-tolerance (broadcast)";
202 case BOND_MODE_8023AD:
203 return "IEEE 802.3ad Dynamic link aggregation";
205 return "transmit load balancing";
207 return "adaptive load balancing";
213 /*---------------------------------- VLAN -----------------------------------*/
216 * bond_add_vlan - add a new vlan id on bond
217 * @bond: bond that got the notification
218 * @vlan_id: the vlan id to add
220 * Returns -ENOMEM if allocation failed.
222 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
224 struct vlan_entry *vlan;
226 dprintk("bond: %s, vlan id %d\n",
227 (bond ? bond->dev->name: "None"), vlan_id);
229 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
234 INIT_LIST_HEAD(&vlan->vlan_list);
235 vlan->vlan_id = vlan_id;
238 write_lock_bh(&bond->lock);
240 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
242 write_unlock_bh(&bond->lock);
244 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
250 * bond_del_vlan - delete a vlan id from bond
251 * @bond: bond that got the notification
252 * @vlan_id: the vlan id to delete
254 * returns -ENODEV if @vlan_id was not found in @bond.
256 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
258 struct vlan_entry *vlan, *next;
261 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
263 write_lock_bh(&bond->lock);
265 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
266 if (vlan->vlan_id == vlan_id) {
267 list_del(&vlan->vlan_list);
269 if ((bond->params.mode == BOND_MODE_TLB) ||
270 (bond->params.mode == BOND_MODE_ALB)) {
271 bond_alb_clear_vlan(bond, vlan_id);
274 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
279 if (list_empty(&bond->vlan_list) &&
280 (bond->slave_cnt == 0)) {
281 /* Last VLAN removed and no slaves, so
282 * restore block on adding VLANs. This will
283 * be removed once new slaves that are not
284 * VLAN challenged will be added.
286 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
294 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
298 write_unlock_bh(&bond->lock);
303 * bond_has_challenged_slaves
304 * @bond: the bond we're working on
306 * Searches the slave list. Returns 1 if a vlan challenged slave
307 * was found, 0 otherwise.
309 * Assumes bond->lock is held.
311 static int bond_has_challenged_slaves(struct bonding *bond)
316 bond_for_each_slave(bond, slave, i) {
317 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
318 dprintk("found VLAN challenged slave - %s\n",
324 dprintk("no VLAN challenged slaves found\n");
329 * bond_next_vlan - safely skip to the next item in the vlans list.
330 * @bond: the bond we're working on
331 * @curr: item we're advancing from
333 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
334 * or @curr->next otherwise (even if it is @curr itself again).
336 * Caller must hold bond->lock
338 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
340 struct vlan_entry *next, *last;
342 if (list_empty(&bond->vlan_list)) {
347 next = list_entry(bond->vlan_list.next,
348 struct vlan_entry, vlan_list);
350 last = list_entry(bond->vlan_list.prev,
351 struct vlan_entry, vlan_list);
353 next = list_entry(bond->vlan_list.next,
354 struct vlan_entry, vlan_list);
356 next = list_entry(curr->vlan_list.next,
357 struct vlan_entry, vlan_list);
365 * bond_dev_queue_xmit - Prepare skb for xmit.
367 * @bond: bond device that got this skb for tx.
368 * @skb: hw accel VLAN tagged skb to transmit
369 * @slave_dev: slave that is supposed to xmit this skbuff
371 * When the bond gets an skb to transmit that is
372 * already hardware accelerated VLAN tagged, and it
373 * needs to relay this skb to a slave that is not
374 * hw accel capable, the skb needs to be "unaccelerated",
375 * i.e. strip the hwaccel tag and re-insert it as part
378 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
380 unsigned short vlan_id;
382 if (!list_empty(&bond->vlan_list) &&
383 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
384 vlan_get_tag(skb, &vlan_id) == 0) {
385 skb->dev = slave_dev;
386 skb = vlan_put_tag(skb, vlan_id);
388 /* vlan_put_tag() frees the skb in case of error,
389 * so return success here so the calling functions
390 * won't attempt to free is again.
395 skb->dev = slave_dev;
405 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
406 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
408 * a. This operation is performed in IOCTL context,
409 * b. The operation is protected by the RTNL semaphore in the 8021q code,
410 * c. Holding a lock with BH disabled while directly calling a base driver
411 * entry point is generally a BAD idea.
413 * The design of synchronization/protection for this operation in the 8021q
414 * module is good for one or more VLAN devices over a single physical device
415 * and cannot be extended for a teaming solution like bonding, so there is a
416 * potential race condition here where a net device from the vlan group might
417 * be referenced (either by a base driver or the 8021q code) while it is being
418 * removed from the system. However, it turns out we're not making matters
419 * worse, and if it works for regular VLAN usage it will work here too.
423 * bond_vlan_rx_register - Propagates registration to slaves
424 * @bond_dev: bonding net device that got called
425 * @grp: vlan group being registered
427 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
429 struct bonding *bond = bond_dev->priv;
435 bond_for_each_slave(bond, slave, i) {
436 struct net_device *slave_dev = slave->dev;
438 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
439 slave_dev->vlan_rx_register) {
440 slave_dev->vlan_rx_register(slave_dev, grp);
446 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
447 * @bond_dev: bonding net device that got called
448 * @vid: vlan id being added
450 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
452 struct bonding *bond = bond_dev->priv;
456 bond_for_each_slave(bond, slave, i) {
457 struct net_device *slave_dev = slave->dev;
459 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
460 slave_dev->vlan_rx_add_vid) {
461 slave_dev->vlan_rx_add_vid(slave_dev, vid);
465 res = bond_add_vlan(bond, vid);
467 printk(KERN_ERR DRV_NAME
468 ": %s: Error: Failed to add vlan id %d\n",
469 bond_dev->name, vid);
474 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
475 * @bond_dev: bonding net device that got called
476 * @vid: vlan id being removed
478 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
480 struct bonding *bond = bond_dev->priv;
482 struct net_device *vlan_dev;
485 bond_for_each_slave(bond, slave, i) {
486 struct net_device *slave_dev = slave->dev;
488 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
489 slave_dev->vlan_rx_kill_vid) {
490 /* Save and then restore vlan_dev in the grp array,
491 * since the slave's driver might clear it.
493 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
494 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
495 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
499 res = bond_del_vlan(bond, vid);
501 printk(KERN_ERR DRV_NAME
502 ": %s: Error: Failed to remove vlan id %d\n",
503 bond_dev->name, vid);
507 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
509 struct vlan_entry *vlan;
511 write_lock_bh(&bond->lock);
513 if (list_empty(&bond->vlan_list)) {
517 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
518 slave_dev->vlan_rx_register) {
519 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
522 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
523 !(slave_dev->vlan_rx_add_vid)) {
527 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
528 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
532 write_unlock_bh(&bond->lock);
535 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
537 struct vlan_entry *vlan;
538 struct net_device *vlan_dev;
540 write_lock_bh(&bond->lock);
542 if (list_empty(&bond->vlan_list)) {
546 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
547 !(slave_dev->vlan_rx_kill_vid)) {
551 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
552 /* Save and then restore vlan_dev in the grp array,
553 * since the slave's driver might clear it.
555 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
556 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
557 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
561 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
562 slave_dev->vlan_rx_register) {
563 slave_dev->vlan_rx_register(slave_dev, NULL);
567 write_unlock_bh(&bond->lock);
570 /*------------------------------- Link status -------------------------------*/
573 * Set the carrier state for the master according to the state of its
574 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
575 * do special 802.3ad magic.
577 * Returns zero if carrier state does not change, nonzero if it does.
579 static int bond_set_carrier(struct bonding *bond)
584 if (bond->slave_cnt == 0)
587 if (bond->params.mode == BOND_MODE_8023AD)
588 return bond_3ad_set_carrier(bond);
590 bond_for_each_slave(bond, slave, i) {
591 if (slave->link == BOND_LINK_UP) {
592 if (!netif_carrier_ok(bond->dev)) {
593 netif_carrier_on(bond->dev);
601 if (netif_carrier_ok(bond->dev)) {
602 netif_carrier_off(bond->dev);
609 * Get link speed and duplex from the slave's base driver
610 * using ethtool. If for some reason the call fails or the
611 * values are invalid, fake speed and duplex to 100/Full
614 static int bond_update_speed_duplex(struct slave *slave)
616 struct net_device *slave_dev = slave->dev;
617 struct ethtool_cmd etool;
620 /* Fake speed and duplex */
621 slave->speed = SPEED_100;
622 slave->duplex = DUPLEX_FULL;
624 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
627 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
631 switch (etool.speed) {
641 switch (etool.duplex) {
649 slave->speed = etool.speed;
650 slave->duplex = etool.duplex;
656 * if <dev> supports MII link status reporting, check its link status.
658 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
659 * depening upon the setting of the use_carrier parameter.
661 * Return either BMSR_LSTATUS, meaning that the link is up (or we
662 * can't tell and just pretend it is), or 0, meaning that the link is
665 * If reporting is non-zero, instead of faking link up, return -1 if
666 * both ETHTOOL and MII ioctls fail (meaning the device does not
667 * support them). If use_carrier is set, return whatever it says.
668 * It'd be nice if there was a good way to tell if a driver supports
669 * netif_carrier, but there really isn't.
671 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
673 static int (* ioctl)(struct net_device *, struct ifreq *, int);
675 struct mii_ioctl_data *mii;
677 if (bond->params.use_carrier) {
678 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
681 ioctl = slave_dev->do_ioctl;
683 /* TODO: set pointer to correct ioctl on a per team member */
684 /* bases to make this more efficient. that is, once */
685 /* we determine the correct ioctl, we will always */
686 /* call it and not the others for that team */
690 * We cannot assume that SIOCGMIIPHY will also read a
691 * register; not all network drivers (e.g., e100)
695 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
696 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
698 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
699 mii->reg_num = MII_BMSR;
700 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
701 return (mii->val_out & BMSR_LSTATUS);
707 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
708 * attempt to get link status from it if the above MII ioctls fail.
710 if (slave_dev->ethtool_ops) {
711 if (slave_dev->ethtool_ops->get_link) {
714 link = slave_dev->ethtool_ops->get_link(slave_dev);
716 return link ? BMSR_LSTATUS : 0;
721 * If reporting, report that either there's no dev->do_ioctl,
722 * or both SIOCGMIIREG and get_link failed (meaning that we
723 * cannot report link status). If not reporting, pretend
726 return (reporting ? -1 : BMSR_LSTATUS);
729 /*----------------------------- Multicast list ------------------------------*/
732 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
734 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
736 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
737 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
741 * returns dmi entry if found, NULL otherwise
743 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
745 struct dev_mc_list *idmi;
747 for (idmi = mc_list; idmi; idmi = idmi->next) {
748 if (bond_is_dmi_same(dmi, idmi)) {
757 * Push the promiscuity flag down to appropriate slaves
759 static void bond_set_promiscuity(struct bonding *bond, int inc)
761 if (USES_PRIMARY(bond->params.mode)) {
762 /* write lock already acquired */
763 if (bond->curr_active_slave) {
764 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
769 bond_for_each_slave(bond, slave, i) {
770 dev_set_promiscuity(slave->dev, inc);
776 * Push the allmulti flag down to all slaves
778 static void bond_set_allmulti(struct bonding *bond, int inc)
780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
783 dev_set_allmulti(bond->curr_active_slave->dev, inc);
788 bond_for_each_slave(bond, slave, i) {
789 dev_set_allmulti(slave->dev, inc);
795 * Add a Multicast address to slaves
798 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
800 if (USES_PRIMARY(bond->params.mode)) {
801 /* write lock already acquired */
802 if (bond->curr_active_slave) {
803 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
808 bond_for_each_slave(bond, slave, i) {
809 dev_mc_add(slave->dev, addr, alen, 0);
815 * Remove a multicast address from slave
818 static void bond_mc_delete(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_delete(bond->curr_active_slave->dev, addr, alen, 0);
828 bond_for_each_slave(bond, slave, i) {
829 dev_mc_delete(slave->dev, addr, alen, 0);
836 * Retrieve the list of registered multicast addresses for the bonding
837 * device and retransmit an IGMP JOIN request to the current active
840 static void bond_resend_igmp_join_requests(struct bonding *bond)
842 struct in_device *in_dev;
843 struct ip_mc_list *im;
846 in_dev = __in_dev_get_rcu(bond->dev);
848 for (im = in_dev->mc_list; im; im = im->next) {
849 ip_mc_rejoin_group(im);
857 * Totally destroys the mc_list in bond
859 static void bond_mc_list_destroy(struct bonding *bond)
861 struct dev_mc_list *dmi;
865 bond->mc_list = dmi->next;
869 bond->mc_list = NULL;
873 * Copy all the Multicast addresses from src to the bonding device dst
875 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
878 struct dev_mc_list *dmi, *new_dmi;
880 for (dmi = mc_list; dmi; dmi = dmi->next) {
881 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
884 /* FIXME: Potential memory leak !!! */
888 new_dmi->next = bond->mc_list;
889 bond->mc_list = new_dmi;
890 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
891 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
892 new_dmi->dmi_users = dmi->dmi_users;
893 new_dmi->dmi_gusers = dmi->dmi_gusers;
900 * flush all members of flush->mc_list from device dev->mc_list
902 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
904 struct bonding *bond = bond_dev->priv;
905 struct dev_mc_list *dmi;
907 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
908 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
911 if (bond->params.mode == BOND_MODE_8023AD) {
912 /* del lacpdu mc addr from mc list */
913 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
915 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
919 /*--------------------------- Active slave change ---------------------------*/
922 * Update the mc list and multicast-related flags for the new and
923 * old active slaves (if any) according to the multicast mode, and
924 * promiscuous flags unconditionally.
926 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
928 struct dev_mc_list *dmi;
930 if (!USES_PRIMARY(bond->params.mode)) {
931 /* nothing to do - mc list is already up-to-date on
938 if (bond->dev->flags & IFF_PROMISC) {
939 dev_set_promiscuity(old_active->dev, -1);
942 if (bond->dev->flags & IFF_ALLMULTI) {
943 dev_set_allmulti(old_active->dev, -1);
946 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
947 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
952 if (bond->dev->flags & IFF_PROMISC) {
953 dev_set_promiscuity(new_active->dev, 1);
956 if (bond->dev->flags & IFF_ALLMULTI) {
957 dev_set_allmulti(new_active->dev, 1);
960 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
961 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
963 bond_resend_igmp_join_requests(bond);
968 * find_best_interface - select the best available slave to be the active one
969 * @bond: our bonding struct
971 * Warning: Caller must hold curr_slave_lock for writing.
973 static struct slave *bond_find_best_slave(struct bonding *bond)
975 struct slave *new_active, *old_active;
976 struct slave *bestslave = NULL;
977 int mintime = bond->params.updelay;
980 new_active = old_active = bond->curr_active_slave;
982 if (!new_active) { /* there were no active slaves left */
983 if (bond->slave_cnt > 0) { /* found one slave */
984 new_active = bond->first_slave;
986 return NULL; /* still no slave, return NULL */
990 /* first try the primary link; if arping, a link must tx/rx traffic
991 * before it can be considered the curr_active_slave - also, we would skip
992 * slaves between the curr_active_slave and primary_slave that may be up
995 if ((bond->primary_slave) &&
996 (!bond->params.arp_interval) &&
997 (IS_UP(bond->primary_slave->dev))) {
998 new_active = bond->primary_slave;
1001 /* remember where to stop iterating over the slaves */
1002 old_active = new_active;
1004 bond_for_each_slave_from(bond, new_active, i, old_active) {
1005 if (IS_UP(new_active->dev)) {
1006 if (new_active->link == BOND_LINK_UP) {
1008 } else if (new_active->link == BOND_LINK_BACK) {
1009 /* link up, but waiting for stabilization */
1010 if (new_active->delay < mintime) {
1011 mintime = new_active->delay;
1012 bestslave = new_active;
1022 * change_active_interface - change the active slave into the specified one
1023 * @bond: our bonding struct
1024 * @new: the new slave to make the active one
1026 * Set the new slave to the bond's settings and unset them on the old
1027 * curr_active_slave.
1028 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1030 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1031 * because it is apparently the best available slave we have, even though its
1032 * updelay hasn't timed out yet.
1034 * Warning: Caller must hold curr_slave_lock for writing.
1036 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1038 struct slave *old_active = bond->curr_active_slave;
1040 if (old_active == new_active) {
1045 if (new_active->link == BOND_LINK_BACK) {
1046 if (USES_PRIMARY(bond->params.mode)) {
1047 printk(KERN_INFO DRV_NAME
1048 ": %s: making interface %s the new "
1049 "active one %d ms earlier.\n",
1050 bond->dev->name, new_active->dev->name,
1051 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1054 new_active->delay = 0;
1055 new_active->link = BOND_LINK_UP;
1056 new_active->jiffies = jiffies;
1058 if (bond->params.mode == BOND_MODE_8023AD) {
1059 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1062 if ((bond->params.mode == BOND_MODE_TLB) ||
1063 (bond->params.mode == BOND_MODE_ALB)) {
1064 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1067 if (USES_PRIMARY(bond->params.mode)) {
1068 printk(KERN_INFO DRV_NAME
1069 ": %s: making interface %s the new "
1071 bond->dev->name, new_active->dev->name);
1076 if (USES_PRIMARY(bond->params.mode)) {
1077 bond_mc_swap(bond, new_active, old_active);
1080 if ((bond->params.mode == BOND_MODE_TLB) ||
1081 (bond->params.mode == BOND_MODE_ALB)) {
1082 bond_alb_handle_active_change(bond, new_active);
1084 bond_set_slave_inactive_flags(old_active);
1086 bond_set_slave_active_flags(new_active);
1088 bond->curr_active_slave = new_active;
1091 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1093 bond_set_slave_inactive_flags(old_active);
1097 bond_set_slave_active_flags(new_active);
1100 /* when bonding does not set the slave MAC address, the bond MAC
1101 * address is the one of the active slave.
1103 if (new_active && !bond->do_set_mac_addr)
1104 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1105 new_active->dev->addr_len);
1106 if (bond->curr_active_slave &&
1107 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1108 &bond->curr_active_slave->dev->state)) {
1109 dprintk("delaying gratuitous arp on %s\n",
1110 bond->curr_active_slave->dev->name);
1111 bond->send_grat_arp = 1;
1113 bond_send_gratuitous_arp(bond);
1118 * bond_select_active_slave - select a new active slave, if needed
1119 * @bond: our bonding struct
1121 * This functions shoud be called when one of the following occurs:
1122 * - The old curr_active_slave has been released or lost its link.
1123 * - The primary_slave has got its link back.
1124 * - A slave has got its link back and there's no old curr_active_slave.
1126 * Warning: Caller must hold curr_slave_lock for writing.
1128 void bond_select_active_slave(struct bonding *bond)
1130 struct slave *best_slave;
1133 best_slave = bond_find_best_slave(bond);
1134 if (best_slave != bond->curr_active_slave) {
1135 bond_change_active_slave(bond, best_slave);
1136 rv = bond_set_carrier(bond);
1140 if (netif_carrier_ok(bond->dev)) {
1141 printk(KERN_INFO DRV_NAME
1142 ": %s: first active interface up!\n",
1145 printk(KERN_INFO DRV_NAME ": %s: "
1146 "now running without any active interface !\n",
1152 /*--------------------------- slave list handling ---------------------------*/
1155 * This function attaches the slave to the end of list.
1157 * bond->lock held for writing by caller.
1159 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1161 if (bond->first_slave == NULL) { /* attaching the first slave */
1162 new_slave->next = new_slave;
1163 new_slave->prev = new_slave;
1164 bond->first_slave = new_slave;
1166 new_slave->next = bond->first_slave;
1167 new_slave->prev = bond->first_slave->prev;
1168 new_slave->next->prev = new_slave;
1169 new_slave->prev->next = new_slave;
1176 * This function detaches the slave from the list.
1177 * WARNING: no check is made to verify if the slave effectively
1178 * belongs to <bond>.
1179 * Nothing is freed on return, structures are just unchained.
1180 * If any slave pointer in bond was pointing to <slave>,
1181 * it should be changed by the calling function.
1183 * bond->lock held for writing by caller.
1185 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1188 slave->next->prev = slave->prev;
1192 slave->prev->next = slave->next;
1195 if (bond->first_slave == slave) { /* slave is the first slave */
1196 if (bond->slave_cnt > 1) { /* there are more slave */
1197 bond->first_slave = slave->next;
1199 bond->first_slave = NULL; /* slave was the last one */
1208 /*---------------------------------- IOCTL ----------------------------------*/
1210 static int bond_sethwaddr(struct net_device *bond_dev,
1211 struct net_device *slave_dev)
1213 dprintk("bond_dev=%p\n", bond_dev);
1214 dprintk("slave_dev=%p\n", slave_dev);
1215 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1216 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1220 #define BOND_VLAN_FEATURES \
1221 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1222 NETIF_F_HW_VLAN_FILTER)
1225 * Compute the common dev->feature set available to all slaves. Some
1226 * feature bits are managed elsewhere, so preserve those feature bits
1227 * on the master device.
1229 static int bond_compute_features(struct bonding *bond)
1231 struct slave *slave;
1232 struct net_device *bond_dev = bond->dev;
1233 unsigned long features = bond_dev->features;
1234 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1235 bond_dev->hard_header_len);
1238 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1239 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1240 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1242 bond_for_each_slave(bond, slave, i) {
1243 features = netdev_compute_features(features,
1244 slave->dev->features);
1245 if (slave->dev->hard_header_len > max_hard_header_len)
1246 max_hard_header_len = slave->dev->hard_header_len;
1249 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1250 bond_dev->features = features;
1251 bond_dev->hard_header_len = max_hard_header_len;
1257 static void bond_setup_by_slave(struct net_device *bond_dev,
1258 struct net_device *slave_dev)
1260 struct bonding *bond = bond_dev->priv;
1262 bond_dev->neigh_setup = slave_dev->neigh_setup;
1264 bond_dev->type = slave_dev->type;
1265 bond_dev->hard_header_len = slave_dev->hard_header_len;
1266 bond_dev->addr_len = slave_dev->addr_len;
1268 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1269 slave_dev->addr_len);
1270 bond->setup_by_slave = 1;
1273 /* enslave device <slave> to bond device <master> */
1274 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1276 struct bonding *bond = bond_dev->priv;
1277 struct slave *new_slave = NULL;
1278 struct dev_mc_list *dmi;
1279 struct sockaddr addr;
1281 int old_features = bond_dev->features;
1284 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1285 slave_dev->do_ioctl == NULL) {
1286 printk(KERN_WARNING DRV_NAME
1287 ": %s: Warning: no link monitoring support for %s\n",
1288 bond_dev->name, slave_dev->name);
1291 /* bond must be initialized by bond_open() before enslaving */
1292 if (!(bond_dev->flags & IFF_UP)) {
1293 printk(KERN_WARNING DRV_NAME
1294 " %s: master_dev is not up in bond_enslave\n",
1298 /* already enslaved */
1299 if (slave_dev->flags & IFF_SLAVE) {
1300 dprintk("Error, Device was already enslaved\n");
1304 /* vlan challenged mutual exclusion */
1305 /* no need to lock since we're protected by rtnl_lock */
1306 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1307 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1308 if (!list_empty(&bond->vlan_list)) {
1309 printk(KERN_ERR DRV_NAME
1310 ": %s: Error: cannot enslave VLAN "
1311 "challenged slave %s on VLAN enabled "
1312 "bond %s\n", bond_dev->name, slave_dev->name,
1316 printk(KERN_WARNING DRV_NAME
1317 ": %s: Warning: enslaved VLAN challenged "
1318 "slave %s. Adding VLANs will be blocked as "
1319 "long as %s is part of bond %s\n",
1320 bond_dev->name, slave_dev->name, slave_dev->name,
1322 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1325 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1326 if (bond->slave_cnt == 0) {
1327 /* First slave, and it is not VLAN challenged,
1328 * so remove the block of adding VLANs over the bond.
1330 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1335 * Old ifenslave binaries are no longer supported. These can
1336 * be identified with moderate accurary by the state of the slave:
1337 * the current ifenslave will set the interface down prior to
1338 * enslaving it; the old ifenslave will not.
1340 if ((slave_dev->flags & IFF_UP)) {
1341 printk(KERN_ERR DRV_NAME ": %s is up. "
1342 "This may be due to an out of date ifenslave.\n",
1345 goto err_undo_flags;
1348 /* set bonding device ether type by slave - bonding netdevices are
1349 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1350 * there is a need to override some of the type dependent attribs/funcs.
1352 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1353 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1355 if (bond->slave_cnt == 0) {
1356 if (slave_dev->type != ARPHRD_ETHER)
1357 bond_setup_by_slave(bond_dev, slave_dev);
1358 } else if (bond_dev->type != slave_dev->type) {
1359 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1360 "from other slaves (%d), can not enslave it.\n",
1362 slave_dev->type, bond_dev->type);
1364 goto err_undo_flags;
1367 if (slave_dev->set_mac_address == NULL) {
1368 if (bond->slave_cnt == 0) {
1369 printk(KERN_WARNING DRV_NAME
1370 ": %s: Warning: The first slave device you "
1371 "specified does not support setting the MAC "
1372 "address. This bond MAC address would be that "
1373 "of the active slave.\n", bond_dev->name);
1374 bond->do_set_mac_addr = 0;
1375 } else if (bond->do_set_mac_addr) {
1376 printk(KERN_ERR DRV_NAME
1377 ": %s: Error: The slave device you specified "
1378 "does not support setting the MAC addres,."
1379 "but this bond uses this practice. \n"
1382 goto err_undo_flags;
1386 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1389 goto err_undo_flags;
1392 /* save slave's original flags before calling
1393 * netdev_set_master and dev_open
1395 new_slave->original_flags = slave_dev->flags;
1398 * Save slave's original ("permanent") mac address for modes
1399 * that need it, and for restoring it upon release, and then
1400 * set it to the master's address
1402 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1404 if (bond->do_set_mac_addr) {
1406 * Set slave to master's mac address. The application already
1407 * set the master's mac address to that of the first slave
1409 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1410 addr.sa_family = slave_dev->type;
1411 res = dev_set_mac_address(slave_dev, &addr);
1413 dprintk("Error %d calling set_mac_address\n", res);
1418 res = netdev_set_master(slave_dev, bond_dev);
1420 dprintk("Error %d calling netdev_set_master\n", res);
1423 /* open the slave since the application closed it */
1424 res = dev_open(slave_dev);
1426 dprintk("Openning slave %s failed\n", slave_dev->name);
1427 goto err_restore_mac;
1430 new_slave->dev = slave_dev;
1431 slave_dev->priv_flags |= IFF_BONDING;
1433 if ((bond->params.mode == BOND_MODE_TLB) ||
1434 (bond->params.mode == BOND_MODE_ALB)) {
1435 /* bond_alb_init_slave() must be called before all other stages since
1436 * it might fail and we do not want to have to undo everything
1438 res = bond_alb_init_slave(bond, new_slave);
1440 goto err_unset_master;
1444 /* If the mode USES_PRIMARY, then the new slave gets the
1445 * master's promisc (and mc) settings only if it becomes the
1446 * curr_active_slave, and that is taken care of later when calling
1447 * bond_change_active()
1449 if (!USES_PRIMARY(bond->params.mode)) {
1450 /* set promiscuity level to new slave */
1451 if (bond_dev->flags & IFF_PROMISC) {
1452 dev_set_promiscuity(slave_dev, 1);
1455 /* set allmulti level to new slave */
1456 if (bond_dev->flags & IFF_ALLMULTI) {
1457 dev_set_allmulti(slave_dev, 1);
1460 /* upload master's mc_list to new slave */
1461 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1462 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1466 if (bond->params.mode == BOND_MODE_8023AD) {
1467 /* add lacpdu mc addr to mc list */
1468 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1470 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1473 bond_add_vlans_on_slave(bond, slave_dev);
1475 write_lock_bh(&bond->lock);
1477 bond_attach_slave(bond, new_slave);
1479 new_slave->delay = 0;
1480 new_slave->link_failure_count = 0;
1482 bond_compute_features(bond);
1484 new_slave->last_arp_rx = jiffies;
1486 if (bond->params.miimon && !bond->params.use_carrier) {
1487 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1489 if ((link_reporting == -1) && !bond->params.arp_interval) {
1491 * miimon is set but a bonded network driver
1492 * does not support ETHTOOL/MII and
1493 * arp_interval is not set. Note: if
1494 * use_carrier is enabled, we will never go
1495 * here (because netif_carrier is always
1496 * supported); thus, we don't need to change
1497 * the messages for netif_carrier.
1499 printk(KERN_WARNING DRV_NAME
1500 ": %s: Warning: MII and ETHTOOL support not "
1501 "available for interface %s, and "
1502 "arp_interval/arp_ip_target module parameters "
1503 "not specified, thus bonding will not detect "
1504 "link failures! see bonding.txt for details.\n",
1505 bond_dev->name, slave_dev->name);
1506 } else if (link_reporting == -1) {
1507 /* unable get link status using mii/ethtool */
1508 printk(KERN_WARNING DRV_NAME
1509 ": %s: Warning: can't get link status from "
1510 "interface %s; the network driver associated "
1511 "with this interface does not support MII or "
1512 "ETHTOOL link status reporting, thus miimon "
1513 "has no effect on this interface.\n",
1514 bond_dev->name, slave_dev->name);
1518 /* check for initial state */
1519 if (!bond->params.miimon ||
1520 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1521 if (bond->params.updelay) {
1522 dprintk("Initial state of slave_dev is "
1523 "BOND_LINK_BACK\n");
1524 new_slave->link = BOND_LINK_BACK;
1525 new_slave->delay = bond->params.updelay;
1527 dprintk("Initial state of slave_dev is "
1529 new_slave->link = BOND_LINK_UP;
1531 new_slave->jiffies = jiffies;
1533 dprintk("Initial state of slave_dev is "
1534 "BOND_LINK_DOWN\n");
1535 new_slave->link = BOND_LINK_DOWN;
1538 if (bond_update_speed_duplex(new_slave) &&
1539 (new_slave->link != BOND_LINK_DOWN)) {
1540 printk(KERN_WARNING DRV_NAME
1541 ": %s: Warning: failed to get speed and duplex from %s, "
1542 "assumed to be 100Mb/sec and Full.\n",
1543 bond_dev->name, new_slave->dev->name);
1545 if (bond->params.mode == BOND_MODE_8023AD) {
1546 printk(KERN_WARNING DRV_NAME
1547 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1548 "support in base driver for proper aggregator "
1549 "selection.\n", bond_dev->name);
1553 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1554 /* if there is a primary slave, remember it */
1555 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1556 bond->primary_slave = new_slave;
1560 switch (bond->params.mode) {
1561 case BOND_MODE_ACTIVEBACKUP:
1562 bond_set_slave_inactive_flags(new_slave);
1563 bond_select_active_slave(bond);
1565 case BOND_MODE_8023AD:
1566 /* in 802.3ad mode, the internal mechanism
1567 * will activate the slaves in the selected
1570 bond_set_slave_inactive_flags(new_slave);
1571 /* if this is the first slave */
1572 if (bond->slave_cnt == 1) {
1573 SLAVE_AD_INFO(new_slave).id = 1;
1574 /* Initialize AD with the number of times that the AD timer is called in 1 second
1575 * can be called only after the mac address of the bond is set
1577 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1578 bond->params.lacp_fast);
1580 SLAVE_AD_INFO(new_slave).id =
1581 SLAVE_AD_INFO(new_slave->prev).id + 1;
1584 bond_3ad_bind_slave(new_slave);
1588 new_slave->state = BOND_STATE_ACTIVE;
1589 if ((!bond->curr_active_slave) &&
1590 (new_slave->link != BOND_LINK_DOWN)) {
1591 /* first slave or no active slave yet, and this link
1592 * is OK, so make this interface the active one
1594 bond_change_active_slave(bond, new_slave);
1596 bond_set_slave_inactive_flags(new_slave);
1600 dprintk("This slave is always active in trunk mode\n");
1602 /* always active in trunk mode */
1603 new_slave->state = BOND_STATE_ACTIVE;
1605 /* In trunking mode there is little meaning to curr_active_slave
1606 * anyway (it holds no special properties of the bond device),
1607 * so we can change it without calling change_active_interface()
1609 if (!bond->curr_active_slave) {
1610 bond->curr_active_slave = new_slave;
1613 } /* switch(bond_mode) */
1615 bond_set_carrier(bond);
1617 write_unlock_bh(&bond->lock);
1619 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1621 goto err_unset_master;
1623 printk(KERN_INFO DRV_NAME
1624 ": %s: enslaving %s as a%s interface with a%s link.\n",
1625 bond_dev->name, slave_dev->name,
1626 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1627 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1629 /* enslave is successful */
1632 /* Undo stages on error */
1634 netdev_set_master(slave_dev, NULL);
1637 dev_close(slave_dev);
1640 if (bond->do_set_mac_addr) {
1641 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1642 addr.sa_family = slave_dev->type;
1643 dev_set_mac_address(slave_dev, &addr);
1650 bond_dev->features = old_features;
1656 * Try to release the slave device <slave> from the bond device <master>
1657 * It is legal to access curr_active_slave without a lock because all the function
1660 * The rules for slave state should be:
1661 * for Active/Backup:
1662 * Active stays on all backups go down
1663 * for Bonded connections:
1664 * The first up interface should be left on and all others downed.
1666 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1668 struct bonding *bond = bond_dev->priv;
1669 struct slave *slave, *oldcurrent;
1670 struct sockaddr addr;
1671 int mac_addr_differ;
1672 DECLARE_MAC_BUF(mac);
1674 /* slave is not a slave or master is not master of this slave */
1675 if (!(slave_dev->flags & IFF_SLAVE) ||
1676 (slave_dev->master != bond_dev)) {
1677 printk(KERN_ERR DRV_NAME
1678 ": %s: Error: cannot release %s.\n",
1679 bond_dev->name, slave_dev->name);
1683 write_lock_bh(&bond->lock);
1685 slave = bond_get_slave_by_dev(bond, slave_dev);
1687 /* not a slave of this bond */
1688 printk(KERN_INFO DRV_NAME
1689 ": %s: %s not enslaved\n",
1690 bond_dev->name, slave_dev->name);
1691 write_unlock_bh(&bond->lock);
1695 mac_addr_differ = memcmp(bond_dev->dev_addr,
1698 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1699 printk(KERN_WARNING DRV_NAME
1700 ": %s: Warning: the permanent HWaddr of %s - "
1701 "%s - is still in use by %s. "
1702 "Set the HWaddr of %s to a different address "
1703 "to avoid conflicts.\n",
1706 print_mac(mac, slave->perm_hwaddr),
1711 /* Inform AD package of unbinding of slave. */
1712 if (bond->params.mode == BOND_MODE_8023AD) {
1713 /* must be called before the slave is
1714 * detached from the list
1716 bond_3ad_unbind_slave(slave);
1719 printk(KERN_INFO DRV_NAME
1720 ": %s: releasing %s interface %s\n",
1722 (slave->state == BOND_STATE_ACTIVE)
1723 ? "active" : "backup",
1726 oldcurrent = bond->curr_active_slave;
1728 bond->current_arp_slave = NULL;
1730 /* release the slave from its bond */
1731 bond_detach_slave(bond, slave);
1733 bond_compute_features(bond);
1735 if (bond->primary_slave == slave) {
1736 bond->primary_slave = NULL;
1739 if (oldcurrent == slave) {
1740 bond_change_active_slave(bond, NULL);
1743 if ((bond->params.mode == BOND_MODE_TLB) ||
1744 (bond->params.mode == BOND_MODE_ALB)) {
1745 /* Must be called only after the slave has been
1746 * detached from the list and the curr_active_slave
1747 * has been cleared (if our_slave == old_current),
1748 * but before a new active slave is selected.
1750 bond_alb_deinit_slave(bond, slave);
1753 if (oldcurrent == slave)
1754 bond_select_active_slave(bond);
1756 if (bond->slave_cnt == 0) {
1757 bond_set_carrier(bond);
1759 /* if the last slave was removed, zero the mac address
1760 * of the master so it will be set by the application
1761 * to the mac address of the first slave
1763 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1765 if (list_empty(&bond->vlan_list)) {
1766 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1768 printk(KERN_WARNING DRV_NAME
1769 ": %s: Warning: clearing HW address of %s while it "
1770 "still has VLANs.\n",
1771 bond_dev->name, bond_dev->name);
1772 printk(KERN_WARNING DRV_NAME
1773 ": %s: When re-adding slaves, make sure the bond's "
1774 "HW address matches its VLANs'.\n",
1777 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1778 !bond_has_challenged_slaves(bond)) {
1779 printk(KERN_INFO DRV_NAME
1780 ": %s: last VLAN challenged slave %s "
1781 "left bond %s. VLAN blocking is removed\n",
1782 bond_dev->name, slave_dev->name, bond_dev->name);
1783 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1786 write_unlock_bh(&bond->lock);
1788 /* must do this from outside any spinlocks */
1789 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1791 bond_del_vlans_from_slave(bond, slave_dev);
1793 /* If the mode USES_PRIMARY, then we should only remove its
1794 * promisc and mc settings if it was the curr_active_slave, but that was
1795 * already taken care of above when we detached the slave
1797 if (!USES_PRIMARY(bond->params.mode)) {
1798 /* unset promiscuity level from slave */
1799 if (bond_dev->flags & IFF_PROMISC) {
1800 dev_set_promiscuity(slave_dev, -1);
1803 /* unset allmulti level from slave */
1804 if (bond_dev->flags & IFF_ALLMULTI) {
1805 dev_set_allmulti(slave_dev, -1);
1808 /* flush master's mc_list from slave */
1809 bond_mc_list_flush(bond_dev, slave_dev);
1812 netdev_set_master(slave_dev, NULL);
1814 /* close slave before restoring its mac address */
1815 dev_close(slave_dev);
1817 if (bond->do_set_mac_addr) {
1818 /* restore original ("permanent") mac address */
1819 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1820 addr.sa_family = slave_dev->type;
1821 dev_set_mac_address(slave_dev, &addr);
1824 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1825 IFF_SLAVE_INACTIVE | IFF_BONDING |
1830 return 0; /* deletion OK */
1834 * Destroy a bonding device.
1835 * Must be under rtnl_lock when this function is called.
1837 void bond_destroy(struct bonding *bond)
1839 bond_deinit(bond->dev);
1840 bond_destroy_sysfs_entry(bond);
1841 unregister_netdevice(bond->dev);
1845 * First release a slave and than destroy the bond if no more slaves iare left.
1846 * Must be under rtnl_lock when this function is called.
1848 int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1850 struct bonding *bond = bond_dev->priv;
1853 ret = bond_release(bond_dev, slave_dev);
1854 if ((ret == 0) && (bond->slave_cnt == 0)) {
1855 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1856 bond_dev->name, bond_dev->name);
1863 * This function releases all slaves.
1865 static int bond_release_all(struct net_device *bond_dev)
1867 struct bonding *bond = bond_dev->priv;
1868 struct slave *slave;
1869 struct net_device *slave_dev;
1870 struct sockaddr addr;
1872 write_lock_bh(&bond->lock);
1874 netif_carrier_off(bond_dev);
1876 if (bond->slave_cnt == 0) {
1880 bond->current_arp_slave = NULL;
1881 bond->primary_slave = NULL;
1882 bond_change_active_slave(bond, NULL);
1884 while ((slave = bond->first_slave) != NULL) {
1885 /* Inform AD package of unbinding of slave
1886 * before slave is detached from the list.
1888 if (bond->params.mode == BOND_MODE_8023AD) {
1889 bond_3ad_unbind_slave(slave);
1892 slave_dev = slave->dev;
1893 bond_detach_slave(bond, slave);
1895 if ((bond->params.mode == BOND_MODE_TLB) ||
1896 (bond->params.mode == BOND_MODE_ALB)) {
1897 /* must be called only after the slave
1898 * has been detached from the list
1900 bond_alb_deinit_slave(bond, slave);
1903 bond_compute_features(bond);
1905 /* now that the slave is detached, unlock and perform
1906 * all the undo steps that should not be called from
1909 write_unlock_bh(&bond->lock);
1911 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1912 bond_del_vlans_from_slave(bond, slave_dev);
1914 /* If the mode USES_PRIMARY, then we should only remove its
1915 * promisc and mc settings if it was the curr_active_slave, but that was
1916 * already taken care of above when we detached the slave
1918 if (!USES_PRIMARY(bond->params.mode)) {
1919 /* unset promiscuity level from slave */
1920 if (bond_dev->flags & IFF_PROMISC) {
1921 dev_set_promiscuity(slave_dev, -1);
1924 /* unset allmulti level from slave */
1925 if (bond_dev->flags & IFF_ALLMULTI) {
1926 dev_set_allmulti(slave_dev, -1);
1929 /* flush master's mc_list from slave */
1930 bond_mc_list_flush(bond_dev, slave_dev);
1933 netdev_set_master(slave_dev, NULL);
1935 /* close slave before restoring its mac address */
1936 dev_close(slave_dev);
1938 if (bond->do_set_mac_addr) {
1939 /* restore original ("permanent") mac address*/
1940 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1941 addr.sa_family = slave_dev->type;
1942 dev_set_mac_address(slave_dev, &addr);
1945 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1946 IFF_SLAVE_INACTIVE);
1950 /* re-acquire the lock before getting the next slave */
1951 write_lock_bh(&bond->lock);
1954 /* zero the mac address of the master so it will be
1955 * set by the application to the mac address of the
1958 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1960 if (list_empty(&bond->vlan_list)) {
1961 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1963 printk(KERN_WARNING DRV_NAME
1964 ": %s: Warning: clearing HW address of %s while it "
1965 "still has VLANs.\n",
1966 bond_dev->name, bond_dev->name);
1967 printk(KERN_WARNING DRV_NAME
1968 ": %s: When re-adding slaves, make sure the bond's "
1969 "HW address matches its VLANs'.\n",
1973 printk(KERN_INFO DRV_NAME
1974 ": %s: released all slaves\n",
1978 write_unlock_bh(&bond->lock);
1984 * This function changes the active slave to slave <slave_dev>.
1985 * It returns -EINVAL in the following cases.
1986 * - <slave_dev> is not found in the list.
1987 * - There is not active slave now.
1988 * - <slave_dev> is already active.
1989 * - The link state of <slave_dev> is not BOND_LINK_UP.
1990 * - <slave_dev> is not running.
1991 * In these cases, this fuction does nothing.
1992 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1994 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1996 struct bonding *bond = bond_dev->priv;
1997 struct slave *old_active = NULL;
1998 struct slave *new_active = NULL;
2001 if (!USES_PRIMARY(bond->params.mode)) {
2005 /* Verify that master_dev is indeed the master of slave_dev */
2006 if (!(slave_dev->flags & IFF_SLAVE) ||
2007 (slave_dev->master != bond_dev)) {
2011 write_lock_bh(&bond->lock);
2013 old_active = bond->curr_active_slave;
2014 new_active = bond_get_slave_by_dev(bond, slave_dev);
2017 * Changing to the current active: do nothing; return success.
2019 if (new_active && (new_active == old_active)) {
2020 write_unlock_bh(&bond->lock);
2026 (new_active->link == BOND_LINK_UP) &&
2027 IS_UP(new_active->dev)) {
2028 bond_change_active_slave(bond, new_active);
2033 write_unlock_bh(&bond->lock);
2038 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2040 struct bonding *bond = bond_dev->priv;
2042 info->bond_mode = bond->params.mode;
2043 info->miimon = bond->params.miimon;
2045 read_lock_bh(&bond->lock);
2046 info->num_slaves = bond->slave_cnt;
2047 read_unlock_bh(&bond->lock);
2052 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2054 struct bonding *bond = bond_dev->priv;
2055 struct slave *slave;
2058 if (info->slave_id < 0) {
2062 read_lock_bh(&bond->lock);
2064 bond_for_each_slave(bond, slave, i) {
2065 if (i == (int)info->slave_id) {
2071 read_unlock_bh(&bond->lock);
2074 strcpy(info->slave_name, slave->dev->name);
2075 info->link = slave->link;
2076 info->state = slave->state;
2077 info->link_failure_count = slave->link_failure_count;
2085 /*-------------------------------- Monitoring -------------------------------*/
2087 /* this function is called regularly to monitor each slave's link. */
2088 void bond_mii_monitor(struct net_device *bond_dev)
2090 struct bonding *bond = bond_dev->priv;
2091 struct slave *slave, *oldcurrent;
2092 int do_failover = 0;
2096 read_lock(&bond->lock);
2098 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2100 if (bond->kill_timers) {
2104 if (bond->slave_cnt == 0) {
2108 /* we will try to read the link status of each of our slaves, and
2109 * set their IFF_RUNNING flag appropriately. For each slave not
2110 * supporting MII status, we won't do anything so that a user-space
2111 * program could monitor the link itself if needed.
2114 if (bond->send_grat_arp) {
2115 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2116 &bond->curr_active_slave->dev->state))
2117 dprintk("Needs to send gratuitous arp but not yet\n");
2119 dprintk("sending delayed gratuitous arp on on %s\n",
2120 bond->curr_active_slave->dev->name);
2121 bond_send_gratuitous_arp(bond);
2122 bond->send_grat_arp = 0;
2125 read_lock(&bond->curr_slave_lock);
2126 oldcurrent = bond->curr_active_slave;
2127 read_unlock(&bond->curr_slave_lock);
2129 bond_for_each_slave(bond, slave, i) {
2130 struct net_device *slave_dev = slave->dev;
2132 u16 old_speed = slave->speed;
2133 u8 old_duplex = slave->duplex;
2135 link_state = bond_check_dev_link(bond, slave_dev, 0);
2137 switch (slave->link) {
2138 case BOND_LINK_UP: /* the link was up */
2139 if (link_state == BMSR_LSTATUS) {
2140 /* link stays up, nothing more to do */
2142 } else { /* link going down */
2143 slave->link = BOND_LINK_FAIL;
2144 slave->delay = bond->params.downdelay;
2146 if (slave->link_failure_count < UINT_MAX) {
2147 slave->link_failure_count++;
2150 if (bond->params.downdelay) {
2151 printk(KERN_INFO DRV_NAME
2152 ": %s: link status down for %s "
2153 "interface %s, disabling it in "
2157 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2158 ? ((slave == oldcurrent)
2159 ? "active " : "backup ")
2163 bond->params.downdelay * bond->params.miimon);
2166 /* no break ! fall through the BOND_LINK_FAIL test to
2167 ensure proper action to be taken
2169 case BOND_LINK_FAIL: /* the link has just gone down */
2170 if (link_state != BMSR_LSTATUS) {
2171 /* link stays down */
2172 if (slave->delay <= 0) {
2173 /* link down for too long time */
2174 slave->link = BOND_LINK_DOWN;
2176 /* in active/backup mode, we must
2177 * completely disable this interface
2179 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2180 (bond->params.mode == BOND_MODE_8023AD)) {
2181 bond_set_slave_inactive_flags(slave);
2184 printk(KERN_INFO DRV_NAME
2185 ": %s: link status definitely "
2186 "down for interface %s, "
2191 /* notify ad that the link status has changed */
2192 if (bond->params.mode == BOND_MODE_8023AD) {
2193 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2196 if ((bond->params.mode == BOND_MODE_TLB) ||
2197 (bond->params.mode == BOND_MODE_ALB)) {
2198 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2201 if (slave == oldcurrent) {
2209 slave->link = BOND_LINK_UP;
2210 slave->jiffies = jiffies;
2211 printk(KERN_INFO DRV_NAME
2212 ": %s: link status up again after %d "
2213 "ms for interface %s.\n",
2215 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2219 case BOND_LINK_DOWN: /* the link was down */
2220 if (link_state != BMSR_LSTATUS) {
2221 /* the link stays down, nothing more to do */
2223 } else { /* link going up */
2224 slave->link = BOND_LINK_BACK;
2225 slave->delay = bond->params.updelay;
2227 if (bond->params.updelay) {
2228 /* if updelay == 0, no need to
2229 advertise about a 0 ms delay */
2230 printk(KERN_INFO DRV_NAME
2231 ": %s: link status up for "
2232 "interface %s, enabling it "
2236 bond->params.updelay * bond->params.miimon);
2239 /* no break ! fall through the BOND_LINK_BACK state in
2240 case there's something to do.
2242 case BOND_LINK_BACK: /* the link has just come back */
2243 if (link_state != BMSR_LSTATUS) {
2244 /* link down again */
2245 slave->link = BOND_LINK_DOWN;
2247 printk(KERN_INFO DRV_NAME
2248 ": %s: link status down again after %d "
2249 "ms for interface %s.\n",
2251 (bond->params.updelay - slave->delay) * bond->params.miimon,
2255 if (slave->delay == 0) {
2256 /* now the link has been up for long time enough */
2257 slave->link = BOND_LINK_UP;
2258 slave->jiffies = jiffies;
2260 if (bond->params.mode == BOND_MODE_8023AD) {
2261 /* prevent it from being the active one */
2262 slave->state = BOND_STATE_BACKUP;
2263 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2264 /* make it immediately active */
2265 slave->state = BOND_STATE_ACTIVE;
2266 } else if (slave != bond->primary_slave) {
2267 /* prevent it from being the active one */
2268 slave->state = BOND_STATE_BACKUP;
2271 printk(KERN_INFO DRV_NAME
2272 ": %s: link status definitely "
2273 "up for interface %s.\n",
2277 /* notify ad that the link status has changed */
2278 if (bond->params.mode == BOND_MODE_8023AD) {
2279 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2282 if ((bond->params.mode == BOND_MODE_TLB) ||
2283 (bond->params.mode == BOND_MODE_ALB)) {
2284 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2287 if ((!oldcurrent) ||
2288 (slave == bond->primary_slave)) {
2297 /* Should not happen */
2298 printk(KERN_ERR DRV_NAME
2299 ": %s: Error: %s Illegal value (link=%d)\n",
2304 } /* end of switch (slave->link) */
2306 bond_update_speed_duplex(slave);
2308 if (bond->params.mode == BOND_MODE_8023AD) {
2309 if (old_speed != slave->speed) {
2310 bond_3ad_adapter_speed_changed(slave);
2313 if (old_duplex != slave->duplex) {
2314 bond_3ad_adapter_duplex_changed(slave);
2321 write_lock(&bond->curr_slave_lock);
2323 bond_select_active_slave(bond);
2325 write_unlock(&bond->curr_slave_lock);
2327 bond_set_carrier(bond);
2330 if (bond->params.miimon) {
2331 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2334 read_unlock(&bond->lock);
2338 static __be32 bond_glean_dev_ip(struct net_device *dev)
2340 struct in_device *idev;
2341 struct in_ifaddr *ifa;
2348 idev = __in_dev_get_rcu(dev);
2352 ifa = idev->ifa_list;
2356 addr = ifa->ifa_local;
2362 static int bond_has_ip(struct bonding *bond)
2364 struct vlan_entry *vlan, *vlan_next;
2366 if (bond->master_ip)
2369 if (list_empty(&bond->vlan_list))
2372 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2381 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2383 struct vlan_entry *vlan, *vlan_next;
2385 if (ip == bond->master_ip)
2388 if (list_empty(&bond->vlan_list))
2391 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2393 if (ip == vlan->vlan_ip)
2401 * We go to the (large) trouble of VLAN tagging ARP frames because
2402 * switches in VLAN mode (especially if ports are configured as
2403 * "native" to a VLAN) might not pass non-tagged frames.
2405 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2407 struct sk_buff *skb;
2409 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2410 slave_dev->name, dest_ip, src_ip, vlan_id);
2412 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2413 NULL, slave_dev->dev_addr, NULL);
2416 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2420 skb = vlan_put_tag(skb, vlan_id);
2422 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2430 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2433 __be32 *targets = bond->params.arp_targets;
2434 struct vlan_entry *vlan, *vlan_next;
2435 struct net_device *vlan_dev;
2439 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2442 dprintk("basa: target %x\n", targets[i]);
2443 if (list_empty(&bond->vlan_list)) {
2444 dprintk("basa: empty vlan: arp_send\n");
2445 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2446 bond->master_ip, 0);
2451 * If VLANs are configured, we do a route lookup to
2452 * determine which VLAN interface would be used, so we
2453 * can tag the ARP with the proper VLAN tag.
2455 memset(&fl, 0, sizeof(fl));
2456 fl.fl4_dst = targets[i];
2457 fl.fl4_tos = RTO_ONLINK;
2459 rv = ip_route_output_key(&rt, &fl);
2461 if (net_ratelimit()) {
2462 printk(KERN_WARNING DRV_NAME
2463 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2464 bond->dev->name, NIPQUAD(fl.fl4_dst));
2470 * This target is not on a VLAN
2472 if (rt->u.dst.dev == bond->dev) {
2474 dprintk("basa: rtdev == bond->dev: arp_send\n");
2475 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2476 bond->master_ip, 0);
2481 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2483 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2484 if (vlan_dev == rt->u.dst.dev) {
2485 vlan_id = vlan->vlan_id;
2486 dprintk("basa: vlan match on %s %d\n",
2487 vlan_dev->name, vlan_id);
2494 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2495 vlan->vlan_ip, vlan_id);
2499 if (net_ratelimit()) {
2500 printk(KERN_WARNING DRV_NAME
2501 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2502 bond->dev->name, NIPQUAD(fl.fl4_dst),
2503 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2510 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2511 * for each VLAN above us.
2513 static void bond_send_gratuitous_arp(struct bonding *bond)
2515 struct slave *slave = bond->curr_active_slave;
2516 struct vlan_entry *vlan;
2517 struct net_device *vlan_dev;
2519 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2520 slave ? slave->dev->name : "NULL");
2524 if (bond->master_ip) {
2525 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2526 bond->master_ip, 0);
2529 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2530 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2531 if (vlan->vlan_ip) {
2532 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2533 vlan->vlan_ip, vlan->vlan_id);
2538 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2541 __be32 *targets = bond->params.arp_targets;
2543 targets = bond->params.arp_targets;
2544 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2545 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2546 "%u.%u.%u.%u bhti(tip) %d\n",
2547 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2548 bond_has_this_ip(bond, tip));
2549 if (sip == targets[i]) {
2550 if (bond_has_this_ip(bond, tip))
2551 slave->last_arp_rx = jiffies;
2557 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2560 struct slave *slave;
2561 struct bonding *bond;
2562 unsigned char *arp_ptr;
2565 if (dev->nd_net != &init_net)
2568 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2572 read_lock(&bond->lock);
2574 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2575 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2576 orig_dev ? orig_dev->name : "NULL");
2578 slave = bond_get_slave_by_dev(bond, orig_dev);
2579 if (!slave || !slave_do_arp_validate(bond, slave))
2582 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2583 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2584 (2 * dev->addr_len) +
2585 (2 * sizeof(u32)))))
2589 if (arp->ar_hln != dev->addr_len ||
2590 skb->pkt_type == PACKET_OTHERHOST ||
2591 skb->pkt_type == PACKET_LOOPBACK ||
2592 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2593 arp->ar_pro != htons(ETH_P_IP) ||
2597 arp_ptr = (unsigned char *)(arp + 1);
2598 arp_ptr += dev->addr_len;
2599 memcpy(&sip, arp_ptr, 4);
2600 arp_ptr += 4 + dev->addr_len;
2601 memcpy(&tip, arp_ptr, 4);
2603 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2604 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2605 slave->state, bond->params.arp_validate,
2606 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2609 * Backup slaves won't see the ARP reply, but do come through
2610 * here for each ARP probe (so we swap the sip/tip to validate
2611 * the probe). In a "redundant switch, common router" type of
2612 * configuration, the ARP probe will (hopefully) travel from
2613 * the active, through one switch, the router, then the other
2614 * switch before reaching the backup.
2616 if (slave->state == BOND_STATE_ACTIVE)
2617 bond_validate_arp(bond, slave, sip, tip);
2619 bond_validate_arp(bond, slave, tip, sip);
2622 read_unlock(&bond->lock);
2625 return NET_RX_SUCCESS;
2629 * this function is called regularly to monitor each slave's link
2630 * ensuring that traffic is being sent and received when arp monitoring
2631 * is used in load-balancing mode. if the adapter has been dormant, then an
2632 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2633 * arp monitoring in active backup mode.
2635 void bond_loadbalance_arp_mon(struct net_device *bond_dev)
2637 struct bonding *bond = bond_dev->priv;
2638 struct slave *slave, *oldcurrent;
2639 int do_failover = 0;
2643 read_lock(&bond->lock);
2645 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2647 if (bond->kill_timers) {
2651 if (bond->slave_cnt == 0) {
2655 read_lock(&bond->curr_slave_lock);
2656 oldcurrent = bond->curr_active_slave;
2657 read_unlock(&bond->curr_slave_lock);
2659 /* see if any of the previous devices are up now (i.e. they have
2660 * xmt and rcv traffic). the curr_active_slave does not come into
2661 * the picture unless it is null. also, slave->jiffies is not needed
2662 * here because we send an arp on each slave and give a slave as
2663 * long as it needs to get the tx/rx within the delta.
2664 * TODO: what about up/down delay in arp mode? it wasn't here before
2667 bond_for_each_slave(bond, slave, i) {
2668 if (slave->link != BOND_LINK_UP) {
2669 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2670 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2672 slave->link = BOND_LINK_UP;
2673 slave->state = BOND_STATE_ACTIVE;
2675 /* primary_slave has no meaning in round-robin
2676 * mode. the window of a slave being up and
2677 * curr_active_slave being null after enslaving
2681 printk(KERN_INFO DRV_NAME
2682 ": %s: link status definitely "
2683 "up for interface %s, ",
2688 printk(KERN_INFO DRV_NAME
2689 ": %s: interface %s is now up\n",
2695 /* slave->link == BOND_LINK_UP */
2697 /* not all switches will respond to an arp request
2698 * when the source ip is 0, so don't take the link down
2699 * if we don't know our ip yet
2701 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2702 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2703 bond_has_ip(bond))) {
2705 slave->link = BOND_LINK_DOWN;
2706 slave->state = BOND_STATE_BACKUP;
2708 if (slave->link_failure_count < UINT_MAX) {
2709 slave->link_failure_count++;
2712 printk(KERN_INFO DRV_NAME
2713 ": %s: interface %s is now down.\n",
2717 if (slave == oldcurrent) {
2723 /* note: if switch is in round-robin mode, all links
2724 * must tx arp to ensure all links rx an arp - otherwise
2725 * links may oscillate or not come up at all; if switch is
2726 * in something like xor mode, there is nothing we can
2727 * do - all replies will be rx'ed on same link causing slaves
2728 * to be unstable during low/no traffic periods
2730 if (IS_UP(slave->dev)) {
2731 bond_arp_send_all(bond, slave);
2736 write_lock(&bond->curr_slave_lock);
2738 bond_select_active_slave(bond);
2740 write_unlock(&bond->curr_slave_lock);
2744 if (bond->params.arp_interval) {
2745 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2748 read_unlock(&bond->lock);
2752 * When using arp monitoring in active-backup mode, this function is
2753 * called to determine if any backup slaves have went down or a new
2754 * current slave needs to be found.
2755 * The backup slaves never generate traffic, they are considered up by merely
2756 * receiving traffic. If the current slave goes down, each backup slave will
2757 * be given the opportunity to tx/rx an arp before being taken down - this
2758 * prevents all slaves from being taken down due to the current slave not
2759 * sending any traffic for the backups to receive. The arps are not necessarily
2760 * necessary, any tx and rx traffic will keep the current slave up. While any
2761 * rx traffic will keep the backup slaves up, the current slave is responsible
2762 * for generating traffic to keep them up regardless of any other traffic they
2763 * may have received.
2764 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2766 void bond_activebackup_arp_mon(struct net_device *bond_dev)
2768 struct bonding *bond = bond_dev->priv;
2769 struct slave *slave;
2773 read_lock(&bond->lock);
2775 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2777 if (bond->kill_timers) {
2781 if (bond->slave_cnt == 0) {
2785 /* determine if any slave has come up or any backup slave has
2787 * TODO: what about up/down delay in arp mode? it wasn't here before
2790 bond_for_each_slave(bond, slave, i) {
2791 if (slave->link != BOND_LINK_UP) {
2792 if ((jiffies - slave_last_rx(bond, slave)) <=
2795 slave->link = BOND_LINK_UP;
2797 write_lock(&bond->curr_slave_lock);
2799 if ((!bond->curr_active_slave) &&
2800 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2801 bond_change_active_slave(bond, slave);
2802 bond->current_arp_slave = NULL;
2803 } else if (bond->curr_active_slave != slave) {
2804 /* this slave has just come up but we
2805 * already have a current slave; this
2806 * can also happen if bond_enslave adds
2807 * a new slave that is up while we are
2808 * searching for a new slave
2810 bond_set_slave_inactive_flags(slave);
2811 bond->current_arp_slave = NULL;
2814 bond_set_carrier(bond);
2816 if (slave == bond->curr_active_slave) {
2817 printk(KERN_INFO DRV_NAME
2818 ": %s: %s is up and now the "
2819 "active interface\n",
2822 netif_carrier_on(bond->dev);
2824 printk(KERN_INFO DRV_NAME
2825 ": %s: backup interface %s is "
2831 write_unlock(&bond->curr_slave_lock);
2834 read_lock(&bond->curr_slave_lock);
2836 if ((slave != bond->curr_active_slave) &&
2837 (!bond->current_arp_slave) &&
2838 (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
2839 bond_has_ip(bond))) {
2840 /* a backup slave has gone down; three times
2841 * the delta allows the current slave to be
2842 * taken out before the backup slave.
2843 * note: a non-null current_arp_slave indicates
2844 * the curr_active_slave went down and we are
2845 * searching for a new one; under this
2846 * condition we only take the curr_active_slave
2847 * down - this gives each slave a chance to
2848 * tx/rx traffic before being taken out
2851 read_unlock(&bond->curr_slave_lock);
2853 slave->link = BOND_LINK_DOWN;
2855 if (slave->link_failure_count < UINT_MAX) {
2856 slave->link_failure_count++;
2859 bond_set_slave_inactive_flags(slave);
2861 printk(KERN_INFO DRV_NAME
2862 ": %s: backup interface %s is now down\n",
2866 read_unlock(&bond->curr_slave_lock);
2871 read_lock(&bond->curr_slave_lock);
2872 slave = bond->curr_active_slave;
2873 read_unlock(&bond->curr_slave_lock);
2876 /* if we have sent traffic in the past 2*arp_intervals but
2877 * haven't xmit and rx traffic in that time interval, select
2878 * a different slave. slave->jiffies is only updated when
2879 * a slave first becomes the curr_active_slave - not necessarily
2880 * after every arp; this ensures the slave has a full 2*delta
2881 * before being taken out. if a primary is being used, check
2882 * if it is up and needs to take over as the curr_active_slave
2884 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2885 (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
2886 bond_has_ip(bond))) &&
2887 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2889 slave->link = BOND_LINK_DOWN;
2891 if (slave->link_failure_count < UINT_MAX) {
2892 slave->link_failure_count++;
2895 printk(KERN_INFO DRV_NAME
2896 ": %s: link status down for active interface "
2897 "%s, disabling it\n",
2901 write_lock(&bond->curr_slave_lock);
2903 bond_select_active_slave(bond);
2904 slave = bond->curr_active_slave;
2906 write_unlock(&bond->curr_slave_lock);
2908 bond->current_arp_slave = slave;
2911 slave->jiffies = jiffies;
2913 } else if ((bond->primary_slave) &&
2914 (bond->primary_slave != slave) &&
2915 (bond->primary_slave->link == BOND_LINK_UP)) {
2916 /* at this point, slave is the curr_active_slave */
2917 printk(KERN_INFO DRV_NAME
2918 ": %s: changing from interface %s to primary "
2922 bond->primary_slave->dev->name);
2924 /* primary is up so switch to it */
2925 write_lock(&bond->curr_slave_lock);
2926 bond_change_active_slave(bond, bond->primary_slave);
2927 write_unlock(&bond->curr_slave_lock);
2929 slave = bond->primary_slave;
2930 slave->jiffies = jiffies;
2932 bond->current_arp_slave = NULL;
2935 /* the current slave must tx an arp to ensure backup slaves
2938 if (slave && bond_has_ip(bond)) {
2939 bond_arp_send_all(bond, slave);
2943 /* if we don't have a curr_active_slave, search for the next available
2944 * backup slave from the current_arp_slave and make it the candidate
2945 * for becoming the curr_active_slave
2948 if (!bond->current_arp_slave) {
2949 bond->current_arp_slave = bond->first_slave;
2952 if (bond->current_arp_slave) {
2953 bond_set_slave_inactive_flags(bond->current_arp_slave);
2955 /* search for next candidate */
2956 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
2957 if (IS_UP(slave->dev)) {
2958 slave->link = BOND_LINK_BACK;
2959 bond_set_slave_active_flags(slave);
2960 bond_arp_send_all(bond, slave);
2961 slave->jiffies = jiffies;
2962 bond->current_arp_slave = slave;
2966 /* if the link state is up at this point, we
2967 * mark it down - this can happen if we have
2968 * simultaneous link failures and
2969 * reselect_active_interface doesn't make this
2970 * one the current slave so it is still marked
2971 * up when it is actually down
2973 if (slave->link == BOND_LINK_UP) {
2974 slave->link = BOND_LINK_DOWN;
2975 if (slave->link_failure_count < UINT_MAX) {
2976 slave->link_failure_count++;
2979 bond_set_slave_inactive_flags(slave);
2981 printk(KERN_INFO DRV_NAME
2982 ": %s: backup interface %s is "
2992 if (bond->params.arp_interval) {
2993 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2996 read_unlock(&bond->lock);
2999 /*------------------------------ proc/seq_file-------------------------------*/
3001 #ifdef CONFIG_PROC_FS
3003 #define SEQ_START_TOKEN ((void *)1)
3005 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3007 struct bonding *bond = seq->private;
3009 struct slave *slave;
3012 /* make sure the bond won't be taken away */
3013 read_lock(&dev_base_lock);
3014 read_lock_bh(&bond->lock);
3017 return SEQ_START_TOKEN;
3020 bond_for_each_slave(bond, slave, i) {
3021 if (++off == *pos) {
3029 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3031 struct bonding *bond = seq->private;
3032 struct slave *slave = v;
3035 if (v == SEQ_START_TOKEN) {
3036 return bond->first_slave;
3039 slave = slave->next;
3041 return (slave == bond->first_slave) ? NULL : slave;
3044 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3046 struct bonding *bond = seq->private;
3048 read_unlock_bh(&bond->lock);
3049 read_unlock(&dev_base_lock);
3052 static void bond_info_show_master(struct seq_file *seq)
3054 struct bonding *bond = seq->private;
3059 read_lock(&bond->curr_slave_lock);
3060 curr = bond->curr_active_slave;
3061 read_unlock(&bond->curr_slave_lock);
3063 seq_printf(seq, "Bonding Mode: %s\n",
3064 bond_mode_name(bond->params.mode));
3066 if (bond->params.mode == BOND_MODE_XOR ||
3067 bond->params.mode == BOND_MODE_8023AD) {
3068 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3069 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3070 bond->params.xmit_policy);
3073 if (USES_PRIMARY(bond->params.mode)) {
3074 seq_printf(seq, "Primary Slave: %s\n",
3075 (bond->primary_slave) ?
3076 bond->primary_slave->dev->name : "None");
3078 seq_printf(seq, "Currently Active Slave: %s\n",
3079 (curr) ? curr->dev->name : "None");
3082 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3084 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3085 seq_printf(seq, "Up Delay (ms): %d\n",
3086 bond->params.updelay * bond->params.miimon);
3087 seq_printf(seq, "Down Delay (ms): %d\n",
3088 bond->params.downdelay * bond->params.miimon);
3091 /* ARP information */
3092 if(bond->params.arp_interval > 0) {
3094 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3095 bond->params.arp_interval);
3097 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3099 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3100 if (!bond->params.arp_targets[i])
3103 seq_printf(seq, ",");
3104 target = ntohl(bond->params.arp_targets[i]);
3105 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3108 seq_printf(seq, "\n");
3111 if (bond->params.mode == BOND_MODE_8023AD) {
3112 struct ad_info ad_info;
3113 DECLARE_MAC_BUF(mac);
3115 seq_puts(seq, "\n802.3ad info\n");
3116 seq_printf(seq, "LACP rate: %s\n",
3117 (bond->params.lacp_fast) ? "fast" : "slow");
3119 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3120 seq_printf(seq, "bond %s has no active aggregator\n",
3123 seq_printf(seq, "Active Aggregator Info:\n");
3125 seq_printf(seq, "\tAggregator ID: %d\n",
3126 ad_info.aggregator_id);
3127 seq_printf(seq, "\tNumber of ports: %d\n",
3129 seq_printf(seq, "\tActor Key: %d\n",
3131 seq_printf(seq, "\tPartner Key: %d\n",
3132 ad_info.partner_key);
3133 seq_printf(seq, "\tPartner Mac Address: %s\n",
3134 print_mac(mac, ad_info.partner_system));
3139 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3141 struct bonding *bond = seq->private;
3142 DECLARE_MAC_BUF(mac);
3144 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3145 seq_printf(seq, "MII Status: %s\n",
3146 (slave->link == BOND_LINK_UP) ? "up" : "down");
3147 seq_printf(seq, "Link Failure Count: %u\n",
3148 slave->link_failure_count);
3151 "Permanent HW addr: %s\n",
3152 print_mac(mac, slave->perm_hwaddr));
3154 if (bond->params.mode == BOND_MODE_8023AD) {
3155 const struct aggregator *agg
3156 = SLAVE_AD_INFO(slave).port.aggregator;
3159 seq_printf(seq, "Aggregator ID: %d\n",
3160 agg->aggregator_identifier);
3162 seq_puts(seq, "Aggregator ID: N/A\n");
3167 static int bond_info_seq_show(struct seq_file *seq, void *v)
3169 if (v == SEQ_START_TOKEN) {
3170 seq_printf(seq, "%s\n", version);
3171 bond_info_show_master(seq);
3173 bond_info_show_slave(seq, v);
3179 static struct seq_operations bond_info_seq_ops = {
3180 .start = bond_info_seq_start,
3181 .next = bond_info_seq_next,
3182 .stop = bond_info_seq_stop,
3183 .show = bond_info_seq_show,
3186 static int bond_info_open(struct inode *inode, struct file *file)
3188 struct seq_file *seq;
3189 struct proc_dir_entry *proc;
3192 res = seq_open(file, &bond_info_seq_ops);
3194 /* recover the pointer buried in proc_dir_entry data */
3195 seq = file->private_data;
3197 seq->private = proc->data;
3203 static const struct file_operations bond_info_fops = {
3204 .owner = THIS_MODULE,
3205 .open = bond_info_open,
3207 .llseek = seq_lseek,
3208 .release = seq_release,
3211 static int bond_create_proc_entry(struct bonding *bond)
3213 struct net_device *bond_dev = bond->dev;
3215 if (bond_proc_dir) {
3216 bond->proc_entry = create_proc_entry(bond_dev->name,
3219 if (bond->proc_entry == NULL) {
3220 printk(KERN_WARNING DRV_NAME
3221 ": Warning: Cannot create /proc/net/%s/%s\n",
3222 DRV_NAME, bond_dev->name);
3224 bond->proc_entry->data = bond;
3225 bond->proc_entry->proc_fops = &bond_info_fops;
3226 bond->proc_entry->owner = THIS_MODULE;
3227 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3234 static void bond_remove_proc_entry(struct bonding *bond)
3236 if (bond_proc_dir && bond->proc_entry) {
3237 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3238 memset(bond->proc_file_name, 0, IFNAMSIZ);
3239 bond->proc_entry = NULL;
3243 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3244 * Caller must hold rtnl_lock.
3246 static void bond_create_proc_dir(void)
3248 int len = strlen(DRV_NAME);
3250 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3251 bond_proc_dir = bond_proc_dir->next) {
3252 if ((bond_proc_dir->namelen == len) &&
3253 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3258 if (!bond_proc_dir) {
3259 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3260 if (bond_proc_dir) {
3261 bond_proc_dir->owner = THIS_MODULE;
3263 printk(KERN_WARNING DRV_NAME
3264 ": Warning: cannot create /proc/net/%s\n",
3270 /* Destroy the bonding directory under /proc/net, if empty.
3271 * Caller must hold rtnl_lock.
3273 static void bond_destroy_proc_dir(void)
3275 struct proc_dir_entry *de;
3277 if (!bond_proc_dir) {
3281 /* verify that the /proc dir is empty */
3282 for (de = bond_proc_dir->subdir; de; de = de->next) {
3283 /* ignore . and .. */
3284 if (*(de->name) != '.') {
3290 if (bond_proc_dir->owner == THIS_MODULE) {
3291 bond_proc_dir->owner = NULL;
3294 remove_proc_entry(DRV_NAME, init_net.proc_net);
3295 bond_proc_dir = NULL;
3298 #endif /* CONFIG_PROC_FS */
3300 /*-------------------------- netdev event handling --------------------------*/
3303 * Change device name
3305 static int bond_event_changename(struct bonding *bond)
3307 #ifdef CONFIG_PROC_FS
3308 bond_remove_proc_entry(bond);
3309 bond_create_proc_entry(bond);
3311 down_write(&(bonding_rwsem));
3312 bond_destroy_sysfs_entry(bond);
3313 bond_create_sysfs_entry(bond);
3314 up_write(&(bonding_rwsem));
3318 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3320 struct bonding *event_bond = bond_dev->priv;
3323 case NETDEV_CHANGENAME:
3324 return bond_event_changename(event_bond);
3325 case NETDEV_UNREGISTER:
3327 * TODO: remove a bond from the list?
3337 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3339 struct net_device *bond_dev = slave_dev->master;
3340 struct bonding *bond = bond_dev->priv;
3343 case NETDEV_UNREGISTER:
3345 bond_release(bond_dev, slave_dev);
3350 * TODO: is this what we get if somebody
3351 * sets up a hierarchical bond, then rmmod's
3352 * one of the slave bonding devices?
3357 * ... Or is it this?
3360 case NETDEV_GOING_DOWN:
3361 dprintk("slave %s is going down\n", slave_dev->name);
3362 if (bond->setup_by_slave)
3363 bond_release_and_destroy(bond_dev, slave_dev);
3365 case NETDEV_CHANGEMTU:
3367 * TODO: Should slaves be allowed to
3368 * independently alter their MTU? For
3369 * an active-backup bond, slaves need
3370 * not be the same type of device, so
3371 * MTUs may vary. For other modes,
3372 * slaves arguably should have the
3373 * same MTUs. To do this, we'd need to
3374 * take over the slave's change_mtu
3375 * function for the duration of their
3379 case NETDEV_CHANGENAME:
3381 * TODO: handle changing the primary's name
3384 case NETDEV_FEAT_CHANGE:
3385 bond_compute_features(bond);
3395 * bond_netdev_event: handle netdev notifier chain events.
3397 * This function receives events for the netdev chain. The caller (an
3398 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3399 * locks for us to safely manipulate the slave devices (RTNL lock,
3402 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3404 struct net_device *event_dev = (struct net_device *)ptr;
3406 if (event_dev->nd_net != &init_net)
3409 dprintk("event_dev: %s, event: %lx\n",
3410 (event_dev ? event_dev->name : "None"),
3413 if (!(event_dev->priv_flags & IFF_BONDING))
3416 if (event_dev->flags & IFF_MASTER) {
3417 dprintk("IFF_MASTER\n");
3418 return bond_master_netdev_event(event, event_dev);
3421 if (event_dev->flags & IFF_SLAVE) {
3422 dprintk("IFF_SLAVE\n");
3423 return bond_slave_netdev_event(event, event_dev);
3430 * bond_inetaddr_event: handle inetaddr notifier chain events.
3432 * We keep track of device IPs primarily to use as source addresses in
3433 * ARP monitor probes (rather than spewing out broadcasts all the time).
3435 * We track one IP for the main device (if it has one), plus one per VLAN.
3437 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3439 struct in_ifaddr *ifa = ptr;
3440 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3441 struct bonding *bond, *bond_next;
3442 struct vlan_entry *vlan, *vlan_next;
3444 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3445 if (bond->dev == event_dev) {
3448 bond->master_ip = ifa->ifa_local;
3451 bond->master_ip = bond_glean_dev_ip(bond->dev);
3458 if (list_empty(&bond->vlan_list))
3461 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3463 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3464 if (vlan_dev == event_dev) {
3467 vlan->vlan_ip = ifa->ifa_local;
3471 bond_glean_dev_ip(vlan_dev);
3482 static struct notifier_block bond_netdev_notifier = {
3483 .notifier_call = bond_netdev_event,
3486 static struct notifier_block bond_inetaddr_notifier = {
3487 .notifier_call = bond_inetaddr_event,
3490 /*-------------------------- Packet type handling ---------------------------*/
3492 /* register to receive lacpdus on a bond */
3493 static void bond_register_lacpdu(struct bonding *bond)
3495 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3497 /* initialize packet type */
3498 pk_type->type = PKT_TYPE_LACPDU;
3499 pk_type->dev = bond->dev;
3500 pk_type->func = bond_3ad_lacpdu_recv;
3502 dev_add_pack(pk_type);
3505 /* unregister to receive lacpdus on a bond */
3506 static void bond_unregister_lacpdu(struct bonding *bond)
3508 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3511 void bond_register_arp(struct bonding *bond)
3513 struct packet_type *pt = &bond->arp_mon_pt;
3518 pt->type = htons(ETH_P_ARP);
3519 pt->dev = bond->dev;
3520 pt->func = bond_arp_rcv;
3524 void bond_unregister_arp(struct bonding *bond)
3526 struct packet_type *pt = &bond->arp_mon_pt;
3528 dev_remove_pack(pt);
3532 /*---------------------------- Hashing Policies -----------------------------*/
3535 * Hash for the output device based upon layer 3 and layer 4 data. If
3536 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3537 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3539 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3540 struct net_device *bond_dev, int count)
3542 struct ethhdr *data = (struct ethhdr *)skb->data;
3543 struct iphdr *iph = ip_hdr(skb);
3544 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3547 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3548 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3549 (iph->protocol == IPPROTO_TCP ||
3550 iph->protocol == IPPROTO_UDP)) {
3551 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3553 return (layer4_xor ^
3554 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3558 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3562 * Hash for the output device based upon layer 2 data
3564 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3565 struct net_device *bond_dev, int count)
3567 struct ethhdr *data = (struct ethhdr *)skb->data;
3569 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3572 /*-------------------------- Device entry points ----------------------------*/
3574 static int bond_open(struct net_device *bond_dev)
3576 struct bonding *bond = bond_dev->priv;
3577 struct timer_list *mii_timer = &bond->mii_timer;
3578 struct timer_list *arp_timer = &bond->arp_timer;
3580 bond->kill_timers = 0;
3582 if ((bond->params.mode == BOND_MODE_TLB) ||
3583 (bond->params.mode == BOND_MODE_ALB)) {
3584 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3586 /* bond_alb_initialize must be called before the timer
3589 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3590 /* something went wrong - fail the open operation */
3594 init_timer(alb_timer);
3595 alb_timer->expires = jiffies + 1;
3596 alb_timer->data = (unsigned long)bond;
3597 alb_timer->function = (void *)&bond_alb_monitor;
3598 add_timer(alb_timer);
3601 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3602 init_timer(mii_timer);
3603 mii_timer->expires = jiffies + 1;
3604 mii_timer->data = (unsigned long)bond_dev;
3605 mii_timer->function = (void *)&bond_mii_monitor;
3606 add_timer(mii_timer);
3609 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3610 init_timer(arp_timer);
3611 arp_timer->expires = jiffies + 1;
3612 arp_timer->data = (unsigned long)bond_dev;
3613 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3614 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3616 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3618 if (bond->params.arp_validate)
3619 bond_register_arp(bond);
3621 add_timer(arp_timer);
3624 if (bond->params.mode == BOND_MODE_8023AD) {
3625 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3626 init_timer(ad_timer);
3627 ad_timer->expires = jiffies + 1;
3628 ad_timer->data = (unsigned long)bond;
3629 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3630 add_timer(ad_timer);
3632 /* register to receive LACPDUs */
3633 bond_register_lacpdu(bond);
3639 static int bond_close(struct net_device *bond_dev)
3641 struct bonding *bond = bond_dev->priv;
3643 if (bond->params.mode == BOND_MODE_8023AD) {
3644 /* Unregister the receive of LACPDUs */
3645 bond_unregister_lacpdu(bond);
3648 if (bond->params.arp_validate)
3649 bond_unregister_arp(bond);
3651 write_lock_bh(&bond->lock);
3654 /* signal timers not to re-arm */
3655 bond->kill_timers = 1;
3657 write_unlock_bh(&bond->lock);
3659 /* del_timer_sync must run without holding the bond->lock
3660 * because a running timer might be trying to hold it too
3663 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3664 del_timer_sync(&bond->mii_timer);
3667 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3668 del_timer_sync(&bond->arp_timer);
3671 switch (bond->params.mode) {
3672 case BOND_MODE_8023AD:
3673 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3677 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3684 if ((bond->params.mode == BOND_MODE_TLB) ||
3685 (bond->params.mode == BOND_MODE_ALB)) {
3686 /* Must be called only after all
3687 * slaves have been released
3689 bond_alb_deinitialize(bond);
3695 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3697 struct bonding *bond = bond_dev->priv;
3698 struct net_device_stats *stats = &(bond->stats), *sstats;
3699 struct slave *slave;
3702 memset(stats, 0, sizeof(struct net_device_stats));
3704 read_lock_bh(&bond->lock);
3706 bond_for_each_slave(bond, slave, i) {
3707 sstats = slave->dev->get_stats(slave->dev);
3708 stats->rx_packets += sstats->rx_packets;
3709 stats->rx_bytes += sstats->rx_bytes;
3710 stats->rx_errors += sstats->rx_errors;
3711 stats->rx_dropped += sstats->rx_dropped;
3713 stats->tx_packets += sstats->tx_packets;
3714 stats->tx_bytes += sstats->tx_bytes;
3715 stats->tx_errors += sstats->tx_errors;
3716 stats->tx_dropped += sstats->tx_dropped;
3718 stats->multicast += sstats->multicast;
3719 stats->collisions += sstats->collisions;
3721 stats->rx_length_errors += sstats->rx_length_errors;
3722 stats->rx_over_errors += sstats->rx_over_errors;
3723 stats->rx_crc_errors += sstats->rx_crc_errors;
3724 stats->rx_frame_errors += sstats->rx_frame_errors;
3725 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3726 stats->rx_missed_errors += sstats->rx_missed_errors;
3728 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3729 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3730 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3731 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3732 stats->tx_window_errors += sstats->tx_window_errors;
3735 read_unlock_bh(&bond->lock);
3740 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3742 struct net_device *slave_dev = NULL;
3743 struct ifbond k_binfo;
3744 struct ifbond __user *u_binfo = NULL;
3745 struct ifslave k_sinfo;
3746 struct ifslave __user *u_sinfo = NULL;
3747 struct mii_ioctl_data *mii = NULL;
3750 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3751 bond_dev->name, cmd);
3763 * We do this again just in case we were called by SIOCGMIIREG
3764 * instead of SIOCGMIIPHY.
3771 if (mii->reg_num == 1) {
3772 struct bonding *bond = bond_dev->priv;
3774 read_lock_bh(&bond->lock);
3775 read_lock(&bond->curr_slave_lock);
3776 if (netif_carrier_ok(bond->dev)) {
3777 mii->val_out = BMSR_LSTATUS;
3779 read_unlock(&bond->curr_slave_lock);
3780 read_unlock_bh(&bond->lock);
3784 case BOND_INFO_QUERY_OLD:
3785 case SIOCBONDINFOQUERY:
3786 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3788 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3792 res = bond_info_query(bond_dev, &k_binfo);
3794 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3800 case BOND_SLAVE_INFO_QUERY_OLD:
3801 case SIOCBONDSLAVEINFOQUERY:
3802 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3804 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3808 res = bond_slave_info_query(bond_dev, &k_sinfo);
3810 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3821 if (!capable(CAP_NET_ADMIN)) {
3825 down_write(&(bonding_rwsem));
3826 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
3828 dprintk("slave_dev=%p: \n", slave_dev);
3833 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3835 case BOND_ENSLAVE_OLD:
3836 case SIOCBONDENSLAVE:
3837 res = bond_enslave(bond_dev, slave_dev);
3839 case BOND_RELEASE_OLD:
3840 case SIOCBONDRELEASE:
3841 res = bond_release(bond_dev, slave_dev);
3843 case BOND_SETHWADDR_OLD:
3844 case SIOCBONDSETHWADDR:
3845 res = bond_sethwaddr(bond_dev, slave_dev);
3847 case BOND_CHANGE_ACTIVE_OLD:
3848 case SIOCBONDCHANGEACTIVE:
3849 res = bond_ioctl_change_active(bond_dev, slave_dev);
3858 up_write(&(bonding_rwsem));
3862 static void bond_set_multicast_list(struct net_device *bond_dev)
3864 struct bonding *bond = bond_dev->priv;
3865 struct dev_mc_list *dmi;
3867 write_lock_bh(&bond->lock);
3870 * Do promisc before checking multicast_mode
3872 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3873 bond_set_promiscuity(bond, 1);
3876 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3877 bond_set_promiscuity(bond, -1);
3880 /* set allmulti flag to slaves */
3881 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3882 bond_set_allmulti(bond, 1);
3885 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3886 bond_set_allmulti(bond, -1);
3889 bond->flags = bond_dev->flags;
3891 /* looking for addresses to add to slaves' mc list */
3892 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3893 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3894 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3898 /* looking for addresses to delete from slaves' list */
3899 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3900 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3901 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3905 /* save master's multicast list */
3906 bond_mc_list_destroy(bond);
3907 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3909 write_unlock_bh(&bond->lock);
3913 * Change the MTU of all of a master's slaves to match the master
3915 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3917 struct bonding *bond = bond_dev->priv;
3918 struct slave *slave, *stop_at;
3922 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3923 (bond_dev ? bond_dev->name : "None"), new_mtu);
3925 /* Can't hold bond->lock with bh disabled here since
3926 * some base drivers panic. On the other hand we can't
3927 * hold bond->lock without bh disabled because we'll
3928 * deadlock. The only solution is to rely on the fact
3929 * that we're under rtnl_lock here, and the slaves
3930 * list won't change. This doesn't solve the problem
3931 * of setting the slave's MTU while it is
3932 * transmitting, but the assumption is that the base
3933 * driver can handle that.
3935 * TODO: figure out a way to safely iterate the slaves
3936 * list, but without holding a lock around the actual
3937 * call to the base driver.
3940 bond_for_each_slave(bond, slave, i) {
3941 dprintk("s %p s->p %p c_m %p\n", slave,
3942 slave->prev, slave->dev->change_mtu);
3944 res = dev_set_mtu(slave->dev, new_mtu);
3947 /* If we failed to set the slave's mtu to the new value
3948 * we must abort the operation even in ACTIVE_BACKUP
3949 * mode, because if we allow the backup slaves to have
3950 * different mtu values than the active slave we'll
3951 * need to change their mtu when doing a failover. That
3952 * means changing their mtu from timer context, which
3953 * is probably not a good idea.
3955 dprintk("err %d %s\n", res, slave->dev->name);
3960 bond_dev->mtu = new_mtu;
3965 /* unwind from head to the slave that failed */
3967 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3970 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3972 dprintk("unwind err %d dev %s\n", tmp_res,
3983 * Note that many devices must be down to change the HW address, and
3984 * downing the master releases all slaves. We can make bonds full of
3985 * bonding devices to test this, however.
3987 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3989 struct bonding *bond = bond_dev->priv;
3990 struct sockaddr *sa = addr, tmp_sa;
3991 struct slave *slave, *stop_at;
3995 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3997 if (!bond->do_set_mac_addr)
4000 if (!is_valid_ether_addr(sa->sa_data)) {
4001 return -EADDRNOTAVAIL;
4004 /* Can't hold bond->lock with bh disabled here since
4005 * some base drivers panic. On the other hand we can't
4006 * hold bond->lock without bh disabled because we'll
4007 * deadlock. The only solution is to rely on the fact
4008 * that we're under rtnl_lock here, and the slaves
4009 * list won't change. This doesn't solve the problem
4010 * of setting the slave's hw address while it is
4011 * transmitting, but the assumption is that the base
4012 * driver can handle that.
4014 * TODO: figure out a way to safely iterate the slaves
4015 * list, but without holding a lock around the actual
4016 * call to the base driver.
4019 bond_for_each_slave(bond, slave, i) {
4020 dprintk("slave %p %s\n", slave, slave->dev->name);
4022 if (slave->dev->set_mac_address == NULL) {
4024 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4028 res = dev_set_mac_address(slave->dev, addr);
4030 /* TODO: consider downing the slave
4032 * User should expect communications
4033 * breakage anyway until ARP finish
4036 dprintk("err %d %s\n", res, slave->dev->name);
4042 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4046 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4047 tmp_sa.sa_family = bond_dev->type;
4049 /* unwind from head to the slave that failed */
4051 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4054 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4056 dprintk("unwind err %d dev %s\n", tmp_res,
4064 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4066 struct bonding *bond = bond_dev->priv;
4067 struct slave *slave, *start_at;
4071 read_lock(&bond->lock);
4073 if (!BOND_IS_OK(bond)) {
4077 read_lock(&bond->curr_slave_lock);
4078 slave = start_at = bond->curr_active_slave;
4079 read_unlock(&bond->curr_slave_lock);
4085 bond_for_each_slave_from(bond, slave, i, start_at) {
4086 if (IS_UP(slave->dev) &&
4087 (slave->link == BOND_LINK_UP) &&
4088 (slave->state == BOND_STATE_ACTIVE)) {
4089 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4091 write_lock(&bond->curr_slave_lock);
4092 bond->curr_active_slave = slave->next;
4093 write_unlock(&bond->curr_slave_lock);
4102 /* no suitable interface, frame not sent */
4105 read_unlock(&bond->lock);
4111 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4112 * the bond has a usable interface.
4114 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4116 struct bonding *bond = bond_dev->priv;
4119 read_lock(&bond->lock);
4120 read_lock(&bond->curr_slave_lock);
4122 if (!BOND_IS_OK(bond)) {
4126 if (!bond->curr_active_slave)
4129 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4133 /* no suitable interface, frame not sent */
4136 read_unlock(&bond->curr_slave_lock);
4137 read_unlock(&bond->lock);
4142 * In bond_xmit_xor() , we determine the output device by using a pre-
4143 * determined xmit_hash_policy(), If the selected device is not enabled,
4144 * find the next active slave.
4146 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4148 struct bonding *bond = bond_dev->priv;
4149 struct slave *slave, *start_at;
4154 read_lock(&bond->lock);
4156 if (!BOND_IS_OK(bond)) {
4160 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4162 bond_for_each_slave(bond, slave, i) {
4171 bond_for_each_slave_from(bond, slave, i, start_at) {
4172 if (IS_UP(slave->dev) &&
4173 (slave->link == BOND_LINK_UP) &&
4174 (slave->state == BOND_STATE_ACTIVE)) {
4175 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4182 /* no suitable interface, frame not sent */
4185 read_unlock(&bond->lock);
4190 * in broadcast mode, we send everything to all usable interfaces.
4192 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4194 struct bonding *bond = bond_dev->priv;
4195 struct slave *slave, *start_at;
4196 struct net_device *tx_dev = NULL;
4200 read_lock(&bond->lock);
4202 if (!BOND_IS_OK(bond)) {
4206 read_lock(&bond->curr_slave_lock);
4207 start_at = bond->curr_active_slave;
4208 read_unlock(&bond->curr_slave_lock);
4214 bond_for_each_slave_from(bond, slave, i, start_at) {
4215 if (IS_UP(slave->dev) &&
4216 (slave->link == BOND_LINK_UP) &&
4217 (slave->state == BOND_STATE_ACTIVE)) {
4219 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4221 printk(KERN_ERR DRV_NAME
4222 ": %s: Error: bond_xmit_broadcast(): "
4223 "skb_clone() failed\n",
4228 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4230 dev_kfree_skb(skb2);
4234 tx_dev = slave->dev;
4239 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4244 /* no suitable interface, frame not sent */
4247 /* frame sent to all suitable interfaces */
4248 read_unlock(&bond->lock);
4252 /*------------------------- Device initialization ---------------------------*/
4255 * set bond mode specific net device operations
4257 void bond_set_mode_ops(struct bonding *bond, int mode)
4259 struct net_device *bond_dev = bond->dev;
4262 case BOND_MODE_ROUNDROBIN:
4263 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4265 case BOND_MODE_ACTIVEBACKUP:
4266 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4269 bond_dev->hard_start_xmit = bond_xmit_xor;
4270 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4271 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4273 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4275 case BOND_MODE_BROADCAST:
4276 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4278 case BOND_MODE_8023AD:
4279 bond_set_master_3ad_flags(bond);
4280 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4281 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4282 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4284 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4287 bond_set_master_alb_flags(bond);
4290 bond_dev->hard_start_xmit = bond_alb_xmit;
4291 bond_dev->set_mac_address = bond_alb_set_mac_address;
4294 /* Should never happen, mode already checked */
4295 printk(KERN_ERR DRV_NAME
4296 ": %s: Error: Unknown bonding mode %d\n",
4303 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4304 struct ethtool_drvinfo *drvinfo)
4306 strncpy(drvinfo->driver, DRV_NAME, 32);
4307 strncpy(drvinfo->version, DRV_VERSION, 32);
4308 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4311 static const struct ethtool_ops bond_ethtool_ops = {
4312 .get_drvinfo = bond_ethtool_get_drvinfo,
4316 * Does not allocate but creates a /proc entry.
4319 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4321 struct bonding *bond = bond_dev->priv;
4323 dprintk("Begin bond_init for %s\n", bond_dev->name);
4325 /* initialize rwlocks */
4326 rwlock_init(&bond->lock);
4327 rwlock_init(&bond->curr_slave_lock);
4329 bond->params = *params; /* copy params struct */
4331 /* Initialize pointers */
4332 bond->first_slave = NULL;
4333 bond->curr_active_slave = NULL;
4334 bond->current_arp_slave = NULL;
4335 bond->primary_slave = NULL;
4336 bond->dev = bond_dev;
4337 bond->send_grat_arp = 0;
4338 bond->setup_by_slave = 0;
4339 INIT_LIST_HEAD(&bond->vlan_list);
4341 /* Initialize the device entry points */
4342 bond_dev->open = bond_open;
4343 bond_dev->stop = bond_close;
4344 bond_dev->get_stats = bond_get_stats;
4345 bond_dev->do_ioctl = bond_do_ioctl;
4346 bond_dev->ethtool_ops = &bond_ethtool_ops;
4347 bond_dev->set_multicast_list = bond_set_multicast_list;
4348 bond_dev->change_mtu = bond_change_mtu;
4349 bond_dev->set_mac_address = bond_set_mac_address;
4351 bond_set_mode_ops(bond, bond->params.mode);
4353 bond_dev->destructor = free_netdev;
4355 /* Initialize the device options */
4356 bond_dev->tx_queue_len = 0;
4357 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4358 bond_dev->priv_flags |= IFF_BONDING;
4360 /* At first, we block adding VLANs. That's the only way to
4361 * prevent problems that occur when adding VLANs over an
4362 * empty bond. The block will be removed once non-challenged
4363 * slaves are enslaved.
4365 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4367 /* don't acquire bond device's netif_tx_lock when
4369 bond_dev->features |= NETIF_F_LLTX;
4371 /* By default, we declare the bond to be fully
4372 * VLAN hardware accelerated capable. Special
4373 * care is taken in the various xmit functions
4374 * when there are slaves that are not hw accel
4377 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4378 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4379 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4380 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4381 NETIF_F_HW_VLAN_RX |
4382 NETIF_F_HW_VLAN_FILTER);
4384 #ifdef CONFIG_PROC_FS
4385 bond_create_proc_entry(bond);
4388 /* set do_set_mac_addr to true on startup */
4389 bond->do_set_mac_addr = 1;
4391 list_add_tail(&bond->bond_list, &bond_dev_list);
4396 /* De-initialize device specific data.
4397 * Caller must hold rtnl_lock.
4399 void bond_deinit(struct net_device *bond_dev)
4401 struct bonding *bond = bond_dev->priv;
4403 list_del(&bond->bond_list);
4405 #ifdef CONFIG_PROC_FS
4406 bond_remove_proc_entry(bond);
4410 /* Unregister and free all bond devices.
4411 * Caller must hold rtnl_lock.
4413 static void bond_free_all(void)
4415 struct bonding *bond, *nxt;
4417 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4418 struct net_device *bond_dev = bond->dev;
4420 bond_mc_list_destroy(bond);
4421 /* Release the bonded slaves */
4422 bond_release_all(bond_dev);
4423 bond_deinit(bond_dev);
4424 unregister_netdevice(bond_dev);
4427 #ifdef CONFIG_PROC_FS
4428 bond_destroy_proc_dir();
4432 /*------------------------- Module initialization ---------------------------*/
4435 * Convert string input module parms. Accept either the
4436 * number of the mode or its string name.
4438 int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4442 for (i = 0; tbl[i].modename; i++) {
4443 if ((isdigit(*mode_arg) &&
4444 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4445 (strncmp(mode_arg, tbl[i].modename,
4446 strlen(tbl[i].modename)) == 0)) {
4454 static int bond_check_params(struct bond_params *params)
4456 int arp_validate_value;
4459 * Convert string parameters.
4462 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4463 if (bond_mode == -1) {
4464 printk(KERN_ERR DRV_NAME
4465 ": Error: Invalid bonding mode \"%s\"\n",
4466 mode == NULL ? "NULL" : mode);
4471 if (xmit_hash_policy) {
4472 if ((bond_mode != BOND_MODE_XOR) &&
4473 (bond_mode != BOND_MODE_8023AD)) {
4474 printk(KERN_INFO DRV_NAME
4475 ": xor_mode param is irrelevant in mode %s\n",
4476 bond_mode_name(bond_mode));
4478 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4480 if (xmit_hashtype == -1) {
4481 printk(KERN_ERR DRV_NAME
4482 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4483 xmit_hash_policy == NULL ? "NULL" :
4491 if (bond_mode != BOND_MODE_8023AD) {
4492 printk(KERN_INFO DRV_NAME
4493 ": lacp_rate param is irrelevant in mode %s\n",
4494 bond_mode_name(bond_mode));
4496 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4497 if (lacp_fast == -1) {
4498 printk(KERN_ERR DRV_NAME
4499 ": Error: Invalid lacp rate \"%s\"\n",
4500 lacp_rate == NULL ? "NULL" : lacp_rate);
4506 if (max_bonds < 1 || max_bonds > INT_MAX) {
4507 printk(KERN_WARNING DRV_NAME
4508 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4509 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4510 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4511 max_bonds = BOND_DEFAULT_MAX_BONDS;
4515 printk(KERN_WARNING DRV_NAME
4516 ": Warning: miimon module parameter (%d), "
4517 "not in range 0-%d, so it was reset to %d\n",
4518 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4519 miimon = BOND_LINK_MON_INTERV;
4523 printk(KERN_WARNING DRV_NAME
4524 ": Warning: updelay module parameter (%d), "
4525 "not in range 0-%d, so it was reset to 0\n",
4530 if (downdelay < 0) {
4531 printk(KERN_WARNING DRV_NAME
4532 ": Warning: downdelay module parameter (%d), "
4533 "not in range 0-%d, so it was reset to 0\n",
4534 downdelay, INT_MAX);
4538 if ((use_carrier != 0) && (use_carrier != 1)) {
4539 printk(KERN_WARNING DRV_NAME
4540 ": Warning: use_carrier module parameter (%d), "
4541 "not of valid value (0/1), so it was set to 1\n",
4546 /* reset values for 802.3ad */
4547 if (bond_mode == BOND_MODE_8023AD) {
4549 printk(KERN_WARNING DRV_NAME
4550 ": Warning: miimon must be specified, "
4551 "otherwise bonding will not detect link "
4552 "failure, speed and duplex which are "
4553 "essential for 802.3ad operation\n");
4554 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4559 /* reset values for TLB/ALB */
4560 if ((bond_mode == BOND_MODE_TLB) ||
4561 (bond_mode == BOND_MODE_ALB)) {
4563 printk(KERN_WARNING DRV_NAME
4564 ": Warning: miimon must be specified, "
4565 "otherwise bonding will not detect link "
4566 "failure and link speed which are essential "
4567 "for TLB/ALB load balancing\n");
4568 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4573 if (bond_mode == BOND_MODE_ALB) {
4574 printk(KERN_NOTICE DRV_NAME
4575 ": In ALB mode you might experience client "
4576 "disconnections upon reconnection of a link if the "
4577 "bonding module updelay parameter (%d msec) is "
4578 "incompatible with the forwarding delay time of the "
4584 if (updelay || downdelay) {
4585 /* just warn the user the up/down delay will have
4586 * no effect since miimon is zero...
4588 printk(KERN_WARNING DRV_NAME
4589 ": Warning: miimon module parameter not set "
4590 "and updelay (%d) or downdelay (%d) module "
4591 "parameter is set; updelay and downdelay have "
4592 "no effect unless miimon is set\n",
4593 updelay, downdelay);
4596 /* don't allow arp monitoring */
4598 printk(KERN_WARNING DRV_NAME
4599 ": Warning: miimon (%d) and arp_interval (%d) "
4600 "can't be used simultaneously, disabling ARP "
4602 miimon, arp_interval);
4606 if ((updelay % miimon) != 0) {
4607 printk(KERN_WARNING DRV_NAME
4608 ": Warning: updelay (%d) is not a multiple "
4609 "of miimon (%d), updelay rounded to %d ms\n",
4610 updelay, miimon, (updelay / miimon) * miimon);
4615 if ((downdelay % miimon) != 0) {
4616 printk(KERN_WARNING DRV_NAME
4617 ": Warning: downdelay (%d) is not a multiple "
4618 "of miimon (%d), downdelay rounded to %d ms\n",
4620 (downdelay / miimon) * miimon);
4623 downdelay /= miimon;
4626 if (arp_interval < 0) {
4627 printk(KERN_WARNING DRV_NAME
4628 ": Warning: arp_interval module parameter (%d) "
4629 ", not in range 0-%d, so it was reset to %d\n",
4630 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4631 arp_interval = BOND_LINK_ARP_INTERV;
4634 for (arp_ip_count = 0;
4635 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4637 /* not complete check, but should be good enough to
4639 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4640 printk(KERN_WARNING DRV_NAME
4641 ": Warning: bad arp_ip_target module parameter "
4642 "(%s), ARP monitoring will not be performed\n",
4643 arp_ip_target[arp_ip_count]);
4646 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4647 arp_target[arp_ip_count] = ip;
4651 if (arp_interval && !arp_ip_count) {
4652 /* don't allow arping if no arp_ip_target given... */
4653 printk(KERN_WARNING DRV_NAME
4654 ": Warning: arp_interval module parameter (%d) "
4655 "specified without providing an arp_ip_target "
4656 "parameter, arp_interval was reset to 0\n",
4662 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4663 printk(KERN_ERR DRV_NAME
4664 ": arp_validate only supported in active-backup mode\n");
4667 if (!arp_interval) {
4668 printk(KERN_ERR DRV_NAME
4669 ": arp_validate requires arp_interval\n");
4673 arp_validate_value = bond_parse_parm(arp_validate,
4675 if (arp_validate_value == -1) {
4676 printk(KERN_ERR DRV_NAME
4677 ": Error: invalid arp_validate \"%s\"\n",
4678 arp_validate == NULL ? "NULL" : arp_validate);
4682 arp_validate_value = 0;
4685 printk(KERN_INFO DRV_NAME
4686 ": MII link monitoring set to %d ms\n",
4688 } else if (arp_interval) {
4691 printk(KERN_INFO DRV_NAME
4692 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4694 arp_validate_tbl[arp_validate_value].modename,
4697 for (i = 0; i < arp_ip_count; i++)
4698 printk (" %s", arp_ip_target[i]);
4703 /* miimon and arp_interval not set, we need one so things
4704 * work as expected, see bonding.txt for details
4706 printk(KERN_WARNING DRV_NAME
4707 ": Warning: either miimon or arp_interval and "
4708 "arp_ip_target module parameters must be specified, "
4709 "otherwise bonding will not detect link failures! see "
4710 "bonding.txt for details.\n");
4713 if (primary && !USES_PRIMARY(bond_mode)) {
4714 /* currently, using a primary only makes sense
4715 * in active backup, TLB or ALB modes
4717 printk(KERN_WARNING DRV_NAME
4718 ": Warning: %s primary device specified but has no "
4719 "effect in %s mode\n",
4720 primary, bond_mode_name(bond_mode));
4724 /* fill params struct with the proper values */
4725 params->mode = bond_mode;
4726 params->xmit_policy = xmit_hashtype;
4727 params->miimon = miimon;
4728 params->arp_interval = arp_interval;
4729 params->arp_validate = arp_validate_value;
4730 params->updelay = updelay;
4731 params->downdelay = downdelay;
4732 params->use_carrier = use_carrier;
4733 params->lacp_fast = lacp_fast;
4734 params->primary[0] = 0;
4737 strncpy(params->primary, primary, IFNAMSIZ);
4738 params->primary[IFNAMSIZ - 1] = 0;
4741 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4746 static struct lock_class_key bonding_netdev_xmit_lock_key;
4748 /* Create a new bond based on the specified name and bonding parameters.
4749 * If name is NULL, obtain a suitable "bond%d" name for us.
4750 * Caller must NOT hold rtnl_lock; we need to release it here before we
4751 * set up our sysfs entries.
4753 int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4755 struct net_device *bond_dev;
4759 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4762 printk(KERN_ERR DRV_NAME
4763 ": %s: eek! can't alloc netdev!\n",
4770 res = dev_alloc_name(bond_dev, "bond%d");
4775 /* bond_init() must be called after dev_alloc_name() (for the
4776 * /proc files), but before register_netdevice(), because we
4777 * need to set function pointers.
4780 res = bond_init(bond_dev, params);
4785 res = register_netdevice(bond_dev);
4790 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4793 *newbond = bond_dev->priv;
4795 netif_carrier_off(bond_dev);
4797 rtnl_unlock(); /* allows sysfs registration of net device */
4798 res = bond_create_sysfs_entry(bond_dev->priv);
4807 bond_deinit(bond_dev);
4809 free_netdev(bond_dev);
4815 static int __init bonding_init(void)
4820 printk(KERN_INFO "%s", version);
4822 res = bond_check_params(&bonding_defaults);
4827 #ifdef CONFIG_PROC_FS
4828 bond_create_proc_dir();
4830 for (i = 0; i < max_bonds; i++) {
4831 res = bond_create(NULL, &bonding_defaults, NULL);
4836 res = bond_create_sysfs();
4840 register_netdevice_notifier(&bond_netdev_notifier);
4841 register_inetaddr_notifier(&bond_inetaddr_notifier);
4847 bond_destroy_sysfs();
4854 static void __exit bonding_exit(void)
4856 unregister_netdevice_notifier(&bond_netdev_notifier);
4857 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4861 bond_destroy_sysfs();
4865 module_init(bonding_init);
4866 module_exit(bonding_exit);
4867 MODULE_LICENSE("GPL");
4868 MODULE_VERSION(DRV_VERSION);
4869 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4870 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4871 MODULE_SUPPORTED_DEVICE("most ethernet devices");