2 * originally based on the dummy device.
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
7 * bonding.c: an Ethernet Bonding driver
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
14 * and probably many L2 switches ...
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
24 * will release all slaves, marking them as down.
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
34 //#define BONDING_DEBUG 1
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
46 #include <linux/tcp.h>
47 #include <linux/udp.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <linux/timer.h>
52 #include <linux/socket.h>
53 #include <linux/ctype.h>
54 #include <linux/inet.h>
55 #include <linux/bitops.h>
56 #include <asm/system.h>
59 #include <asm/uaccess.h>
60 #include <linux/errno.h>
61 #include <linux/netdevice.h>
62 #include <linux/inetdevice.h>
63 #include <linux/igmp.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
67 #include <linux/rtnetlink.h>
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
70 #include <linux/smp.h>
71 #include <linux/if_ether.h>
73 #include <linux/mii.h>
74 #include <linux/ethtool.h>
75 #include <linux/if_vlan.h>
76 #include <linux/if_bonding.h>
77 #include <linux/jiffies.h>
78 #include <net/route.h>
79 #include <net/net_namespace.h>
84 /*---------------------------- Module parameters ----------------------------*/
86 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
87 #define BOND_LINK_MON_INTERV 0
88 #define BOND_LINK_ARP_INTERV 0
90 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
91 static int num_grat_arp = 1;
92 static int num_unsol_na = 1;
93 static int miimon = BOND_LINK_MON_INTERV;
94 static int updelay = 0;
95 static int downdelay = 0;
96 static int use_carrier = 1;
97 static char *mode = NULL;
98 static char *primary = NULL;
99 static char *lacp_rate = NULL;
100 static char *xmit_hash_policy = NULL;
101 static int arp_interval = BOND_LINK_ARP_INTERV;
102 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
103 static char *arp_validate = NULL;
104 static char *fail_over_mac = NULL;
105 struct bond_params bonding_defaults;
107 module_param(max_bonds, int, 0);
108 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
109 module_param(num_grat_arp, int, 0644);
110 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
111 module_param(num_unsol_na, int, 0644);
112 MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
113 module_param(miimon, int, 0);
114 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
115 module_param(updelay, int, 0);
116 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
117 module_param(downdelay, int, 0);
118 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
120 module_param(use_carrier, int, 0);
121 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
122 "0 for off, 1 for on (default)");
123 module_param(mode, charp, 0);
124 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
125 "1 for active-backup, 2 for balance-xor, "
126 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
127 "6 for balance-alb");
128 module_param(primary, charp, 0);
129 MODULE_PARM_DESC(primary, "Primary network device to use");
130 module_param(lacp_rate, charp, 0);
131 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
133 module_param(xmit_hash_policy, charp, 0);
134 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
135 ", 1 for layer 3+4");
136 module_param(arp_interval, int, 0);
137 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
138 module_param_array(arp_ip_target, charp, NULL, 0);
139 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
140 module_param(arp_validate, charp, 0);
141 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
142 module_param(fail_over_mac, charp, 0);
143 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
145 /*----------------------------- Global variables ----------------------------*/
147 static const char * const version =
148 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
150 LIST_HEAD(bond_dev_list);
152 #ifdef CONFIG_PROC_FS
153 static struct proc_dir_entry *bond_proc_dir = NULL;
156 extern struct rw_semaphore bonding_rwsem;
157 static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
158 static int arp_ip_count = 0;
159 static int bond_mode = BOND_MODE_ROUNDROBIN;
160 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
161 static int lacp_fast = 0;
164 struct bond_parm_tbl bond_lacp_tbl[] = {
165 { "slow", AD_LACP_SLOW},
166 { "fast", AD_LACP_FAST},
170 struct bond_parm_tbl bond_mode_tbl[] = {
171 { "balance-rr", BOND_MODE_ROUNDROBIN},
172 { "active-backup", BOND_MODE_ACTIVEBACKUP},
173 { "balance-xor", BOND_MODE_XOR},
174 { "broadcast", BOND_MODE_BROADCAST},
175 { "802.3ad", BOND_MODE_8023AD},
176 { "balance-tlb", BOND_MODE_TLB},
177 { "balance-alb", BOND_MODE_ALB},
181 struct bond_parm_tbl xmit_hashtype_tbl[] = {
182 { "layer2", BOND_XMIT_POLICY_LAYER2},
183 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
184 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
188 struct bond_parm_tbl arp_validate_tbl[] = {
189 { "none", BOND_ARP_VALIDATE_NONE},
190 { "active", BOND_ARP_VALIDATE_ACTIVE},
191 { "backup", BOND_ARP_VALIDATE_BACKUP},
192 { "all", BOND_ARP_VALIDATE_ALL},
196 struct bond_parm_tbl fail_over_mac_tbl[] = {
197 { "none", BOND_FOM_NONE},
198 { "active", BOND_FOM_ACTIVE},
199 { "follow", BOND_FOM_FOLLOW},
203 /*-------------------------- Forward declarations ---------------------------*/
205 static void bond_send_gratuitous_arp(struct bonding *bond);
206 static void bond_deinit(struct net_device *bond_dev);
208 /*---------------------------- General routines -----------------------------*/
210 static const char *bond_mode_name(int mode)
213 case BOND_MODE_ROUNDROBIN :
214 return "load balancing (round-robin)";
215 case BOND_MODE_ACTIVEBACKUP :
216 return "fault-tolerance (active-backup)";
218 return "load balancing (xor)";
219 case BOND_MODE_BROADCAST :
220 return "fault-tolerance (broadcast)";
221 case BOND_MODE_8023AD:
222 return "IEEE 802.3ad Dynamic link aggregation";
224 return "transmit load balancing";
226 return "adaptive load balancing";
232 /*---------------------------------- VLAN -----------------------------------*/
235 * bond_add_vlan - add a new vlan id on bond
236 * @bond: bond that got the notification
237 * @vlan_id: the vlan id to add
239 * Returns -ENOMEM if allocation failed.
241 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
243 struct vlan_entry *vlan;
245 dprintk("bond: %s, vlan id %d\n",
246 (bond ? bond->dev->name: "None"), vlan_id);
248 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
253 INIT_LIST_HEAD(&vlan->vlan_list);
254 vlan->vlan_id = vlan_id;
256 write_lock_bh(&bond->lock);
258 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
260 write_unlock_bh(&bond->lock);
262 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
268 * bond_del_vlan - delete a vlan id from bond
269 * @bond: bond that got the notification
270 * @vlan_id: the vlan id to delete
272 * returns -ENODEV if @vlan_id was not found in @bond.
274 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
276 struct vlan_entry *vlan;
279 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
281 write_lock_bh(&bond->lock);
283 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
284 if (vlan->vlan_id == vlan_id) {
285 list_del(&vlan->vlan_list);
287 if ((bond->params.mode == BOND_MODE_TLB) ||
288 (bond->params.mode == BOND_MODE_ALB)) {
289 bond_alb_clear_vlan(bond, vlan_id);
292 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
297 if (list_empty(&bond->vlan_list) &&
298 (bond->slave_cnt == 0)) {
299 /* Last VLAN removed and no slaves, so
300 * restore block on adding VLANs. This will
301 * be removed once new slaves that are not
302 * VLAN challenged will be added.
304 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
312 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
316 write_unlock_bh(&bond->lock);
321 * bond_has_challenged_slaves
322 * @bond: the bond we're working on
324 * Searches the slave list. Returns 1 if a vlan challenged slave
325 * was found, 0 otherwise.
327 * Assumes bond->lock is held.
329 static int bond_has_challenged_slaves(struct bonding *bond)
334 bond_for_each_slave(bond, slave, i) {
335 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
336 dprintk("found VLAN challenged slave - %s\n",
342 dprintk("no VLAN challenged slaves found\n");
347 * bond_next_vlan - safely skip to the next item in the vlans list.
348 * @bond: the bond we're working on
349 * @curr: item we're advancing from
351 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
352 * or @curr->next otherwise (even if it is @curr itself again).
354 * Caller must hold bond->lock
356 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
358 struct vlan_entry *next, *last;
360 if (list_empty(&bond->vlan_list)) {
365 next = list_entry(bond->vlan_list.next,
366 struct vlan_entry, vlan_list);
368 last = list_entry(bond->vlan_list.prev,
369 struct vlan_entry, vlan_list);
371 next = list_entry(bond->vlan_list.next,
372 struct vlan_entry, vlan_list);
374 next = list_entry(curr->vlan_list.next,
375 struct vlan_entry, vlan_list);
383 * bond_dev_queue_xmit - Prepare skb for xmit.
385 * @bond: bond device that got this skb for tx.
386 * @skb: hw accel VLAN tagged skb to transmit
387 * @slave_dev: slave that is supposed to xmit this skbuff
389 * When the bond gets an skb to transmit that is
390 * already hardware accelerated VLAN tagged, and it
391 * needs to relay this skb to a slave that is not
392 * hw accel capable, the skb needs to be "unaccelerated",
393 * i.e. strip the hwaccel tag and re-insert it as part
396 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
398 unsigned short uninitialized_var(vlan_id);
400 if (!list_empty(&bond->vlan_list) &&
401 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
402 vlan_get_tag(skb, &vlan_id) == 0) {
403 skb->dev = slave_dev;
404 skb = vlan_put_tag(skb, vlan_id);
406 /* vlan_put_tag() frees the skb in case of error,
407 * so return success here so the calling functions
408 * won't attempt to free is again.
413 skb->dev = slave_dev;
423 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
424 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
426 * a. This operation is performed in IOCTL context,
427 * b. The operation is protected by the RTNL semaphore in the 8021q code,
428 * c. Holding a lock with BH disabled while directly calling a base driver
429 * entry point is generally a BAD idea.
431 * The design of synchronization/protection for this operation in the 8021q
432 * module is good for one or more VLAN devices over a single physical device
433 * and cannot be extended for a teaming solution like bonding, so there is a
434 * potential race condition here where a net device from the vlan group might
435 * be referenced (either by a base driver or the 8021q code) while it is being
436 * removed from the system. However, it turns out we're not making matters
437 * worse, and if it works for regular VLAN usage it will work here too.
441 * bond_vlan_rx_register - Propagates registration to slaves
442 * @bond_dev: bonding net device that got called
443 * @grp: vlan group being registered
445 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
447 struct bonding *bond = bond_dev->priv;
453 bond_for_each_slave(bond, slave, i) {
454 struct net_device *slave_dev = slave->dev;
456 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
457 slave_dev->vlan_rx_register) {
458 slave_dev->vlan_rx_register(slave_dev, grp);
464 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
465 * @bond_dev: bonding net device that got called
466 * @vid: vlan id being added
468 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
470 struct bonding *bond = bond_dev->priv;
474 bond_for_each_slave(bond, slave, i) {
475 struct net_device *slave_dev = slave->dev;
477 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
478 slave_dev->vlan_rx_add_vid) {
479 slave_dev->vlan_rx_add_vid(slave_dev, vid);
483 res = bond_add_vlan(bond, vid);
485 printk(KERN_ERR DRV_NAME
486 ": %s: Error: Failed to add vlan id %d\n",
487 bond_dev->name, vid);
492 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
493 * @bond_dev: bonding net device that got called
494 * @vid: vlan id being removed
496 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
498 struct bonding *bond = bond_dev->priv;
500 struct net_device *vlan_dev;
503 bond_for_each_slave(bond, slave, i) {
504 struct net_device *slave_dev = slave->dev;
506 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
507 slave_dev->vlan_rx_kill_vid) {
508 /* Save and then restore vlan_dev in the grp array,
509 * since the slave's driver might clear it.
511 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
512 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
513 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
517 res = bond_del_vlan(bond, vid);
519 printk(KERN_ERR DRV_NAME
520 ": %s: Error: Failed to remove vlan id %d\n",
521 bond_dev->name, vid);
525 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
527 struct vlan_entry *vlan;
529 write_lock_bh(&bond->lock);
531 if (list_empty(&bond->vlan_list)) {
535 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
536 slave_dev->vlan_rx_register) {
537 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
540 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
541 !(slave_dev->vlan_rx_add_vid)) {
545 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
546 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
550 write_unlock_bh(&bond->lock);
553 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
555 struct vlan_entry *vlan;
556 struct net_device *vlan_dev;
558 write_lock_bh(&bond->lock);
560 if (list_empty(&bond->vlan_list)) {
564 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
565 !(slave_dev->vlan_rx_kill_vid)) {
569 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
570 /* Save and then restore vlan_dev in the grp array,
571 * since the slave's driver might clear it.
573 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
574 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
575 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
579 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
580 slave_dev->vlan_rx_register) {
581 slave_dev->vlan_rx_register(slave_dev, NULL);
585 write_unlock_bh(&bond->lock);
588 /*------------------------------- Link status -------------------------------*/
591 * Set the carrier state for the master according to the state of its
592 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
593 * do special 802.3ad magic.
595 * Returns zero if carrier state does not change, nonzero if it does.
597 static int bond_set_carrier(struct bonding *bond)
602 if (bond->slave_cnt == 0)
605 if (bond->params.mode == BOND_MODE_8023AD)
606 return bond_3ad_set_carrier(bond);
608 bond_for_each_slave(bond, slave, i) {
609 if (slave->link == BOND_LINK_UP) {
610 if (!netif_carrier_ok(bond->dev)) {
611 netif_carrier_on(bond->dev);
619 if (netif_carrier_ok(bond->dev)) {
620 netif_carrier_off(bond->dev);
627 * Get link speed and duplex from the slave's base driver
628 * using ethtool. If for some reason the call fails or the
629 * values are invalid, fake speed and duplex to 100/Full
632 static int bond_update_speed_duplex(struct slave *slave)
634 struct net_device *slave_dev = slave->dev;
635 struct ethtool_cmd etool;
638 /* Fake speed and duplex */
639 slave->speed = SPEED_100;
640 slave->duplex = DUPLEX_FULL;
642 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
645 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
649 switch (etool.speed) {
659 switch (etool.duplex) {
667 slave->speed = etool.speed;
668 slave->duplex = etool.duplex;
674 * if <dev> supports MII link status reporting, check its link status.
676 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
677 * depening upon the setting of the use_carrier parameter.
679 * Return either BMSR_LSTATUS, meaning that the link is up (or we
680 * can't tell and just pretend it is), or 0, meaning that the link is
683 * If reporting is non-zero, instead of faking link up, return -1 if
684 * both ETHTOOL and MII ioctls fail (meaning the device does not
685 * support them). If use_carrier is set, return whatever it says.
686 * It'd be nice if there was a good way to tell if a driver supports
687 * netif_carrier, but there really isn't.
689 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
691 static int (* ioctl)(struct net_device *, struct ifreq *, int);
693 struct mii_ioctl_data *mii;
695 if (bond->params.use_carrier) {
696 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
699 ioctl = slave_dev->do_ioctl;
701 /* TODO: set pointer to correct ioctl on a per team member */
702 /* bases to make this more efficient. that is, once */
703 /* we determine the correct ioctl, we will always */
704 /* call it and not the others for that team */
708 * We cannot assume that SIOCGMIIPHY will also read a
709 * register; not all network drivers (e.g., e100)
713 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
714 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
716 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
717 mii->reg_num = MII_BMSR;
718 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
719 return (mii->val_out & BMSR_LSTATUS);
725 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
726 * attempt to get link status from it if the above MII ioctls fail.
728 if (slave_dev->ethtool_ops) {
729 if (slave_dev->ethtool_ops->get_link) {
732 link = slave_dev->ethtool_ops->get_link(slave_dev);
734 return link ? BMSR_LSTATUS : 0;
739 * If reporting, report that either there's no dev->do_ioctl,
740 * or both SIOCGMIIREG and get_link failed (meaning that we
741 * cannot report link status). If not reporting, pretend
744 return (reporting ? -1 : BMSR_LSTATUS);
747 /*----------------------------- Multicast list ------------------------------*/
750 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
752 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
754 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
755 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
759 * returns dmi entry if found, NULL otherwise
761 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
763 struct dev_mc_list *idmi;
765 for (idmi = mc_list; idmi; idmi = idmi->next) {
766 if (bond_is_dmi_same(dmi, idmi)) {
775 * Push the promiscuity flag down to appropriate slaves
777 static int bond_set_promiscuity(struct bonding *bond, int inc)
780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
783 err = dev_set_promiscuity(bond->curr_active_slave->dev,
789 bond_for_each_slave(bond, slave, i) {
790 err = dev_set_promiscuity(slave->dev, inc);
799 * Push the allmulti flag down to all slaves
801 static int bond_set_allmulti(struct bonding *bond, int inc)
804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
807 err = dev_set_allmulti(bond->curr_active_slave->dev,
813 bond_for_each_slave(bond, slave, i) {
814 err = dev_set_allmulti(slave->dev, inc);
823 * Add a Multicast address to slaves
826 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
828 if (USES_PRIMARY(bond->params.mode)) {
829 /* write lock already acquired */
830 if (bond->curr_active_slave) {
831 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
836 bond_for_each_slave(bond, slave, i) {
837 dev_mc_add(slave->dev, addr, alen, 0);
843 * Remove a multicast address from slave
846 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
848 if (USES_PRIMARY(bond->params.mode)) {
849 /* write lock already acquired */
850 if (bond->curr_active_slave) {
851 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
856 bond_for_each_slave(bond, slave, i) {
857 dev_mc_delete(slave->dev, addr, alen, 0);
864 * Retrieve the list of registered multicast addresses for the bonding
865 * device and retransmit an IGMP JOIN request to the current active
868 static void bond_resend_igmp_join_requests(struct bonding *bond)
870 struct in_device *in_dev;
871 struct ip_mc_list *im;
874 in_dev = __in_dev_get_rcu(bond->dev);
876 for (im = in_dev->mc_list; im; im = im->next) {
877 ip_mc_rejoin_group(im);
885 * Totally destroys the mc_list in bond
887 static void bond_mc_list_destroy(struct bonding *bond)
889 struct dev_mc_list *dmi;
893 bond->mc_list = dmi->next;
897 bond->mc_list = NULL;
901 * Copy all the Multicast addresses from src to the bonding device dst
903 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
906 struct dev_mc_list *dmi, *new_dmi;
908 for (dmi = mc_list; dmi; dmi = dmi->next) {
909 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
912 /* FIXME: Potential memory leak !!! */
916 new_dmi->next = bond->mc_list;
917 bond->mc_list = new_dmi;
918 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
919 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
920 new_dmi->dmi_users = dmi->dmi_users;
921 new_dmi->dmi_gusers = dmi->dmi_gusers;
928 * flush all members of flush->mc_list from device dev->mc_list
930 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
932 struct bonding *bond = bond_dev->priv;
933 struct dev_mc_list *dmi;
935 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
936 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
939 if (bond->params.mode == BOND_MODE_8023AD) {
940 /* del lacpdu mc addr from mc list */
941 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
943 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
947 /*--------------------------- Active slave change ---------------------------*/
950 * Update the mc list and multicast-related flags for the new and
951 * old active slaves (if any) according to the multicast mode, and
952 * promiscuous flags unconditionally.
954 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
956 struct dev_mc_list *dmi;
958 if (!USES_PRIMARY(bond->params.mode)) {
959 /* nothing to do - mc list is already up-to-date on
966 if (bond->dev->flags & IFF_PROMISC) {
967 dev_set_promiscuity(old_active->dev, -1);
970 if (bond->dev->flags & IFF_ALLMULTI) {
971 dev_set_allmulti(old_active->dev, -1);
974 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
975 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
980 /* FIXME: Signal errors upstream. */
981 if (bond->dev->flags & IFF_PROMISC) {
982 dev_set_promiscuity(new_active->dev, 1);
985 if (bond->dev->flags & IFF_ALLMULTI) {
986 dev_set_allmulti(new_active->dev, 1);
989 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
990 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
992 bond_resend_igmp_join_requests(bond);
997 * bond_do_fail_over_mac
999 * Perform special MAC address swapping for fail_over_mac settings
1001 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1003 static void bond_do_fail_over_mac(struct bonding *bond,
1004 struct slave *new_active,
1005 struct slave *old_active)
1007 u8 tmp_mac[ETH_ALEN];
1008 struct sockaddr saddr;
1011 switch (bond->params.fail_over_mac) {
1012 case BOND_FOM_ACTIVE:
1014 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1015 new_active->dev->addr_len);
1017 case BOND_FOM_FOLLOW:
1019 * if new_active && old_active, swap them
1020 * if just old_active, do nothing (going to no active slave)
1021 * if just new_active, set new_active to bond's MAC
1026 write_unlock_bh(&bond->curr_slave_lock);
1027 read_unlock(&bond->lock);
1030 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1031 memcpy(saddr.sa_data, old_active->dev->dev_addr,
1033 saddr.sa_family = new_active->dev->type;
1035 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1036 saddr.sa_family = bond->dev->type;
1039 rv = dev_set_mac_address(new_active->dev, &saddr);
1041 printk(KERN_ERR DRV_NAME
1042 ": %s: Error %d setting MAC of slave %s\n",
1043 bond->dev->name, -rv, new_active->dev->name);
1050 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1051 saddr.sa_family = old_active->dev->type;
1053 rv = dev_set_mac_address(old_active->dev, &saddr);
1055 printk(KERN_ERR DRV_NAME
1056 ": %s: Error %d setting MAC of slave %s\n",
1057 bond->dev->name, -rv, new_active->dev->name);
1059 read_lock(&bond->lock);
1060 write_lock_bh(&bond->curr_slave_lock);
1063 printk(KERN_ERR DRV_NAME
1064 ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1065 bond->dev->name, bond->params.fail_over_mac);
1073 * find_best_interface - select the best available slave to be the active one
1074 * @bond: our bonding struct
1076 * Warning: Caller must hold curr_slave_lock for writing.
1078 static struct slave *bond_find_best_slave(struct bonding *bond)
1080 struct slave *new_active, *old_active;
1081 struct slave *bestslave = NULL;
1082 int mintime = bond->params.updelay;
1085 new_active = old_active = bond->curr_active_slave;
1087 if (!new_active) { /* there were no active slaves left */
1088 if (bond->slave_cnt > 0) { /* found one slave */
1089 new_active = bond->first_slave;
1091 return NULL; /* still no slave, return NULL */
1095 /* first try the primary link; if arping, a link must tx/rx traffic
1096 * before it can be considered the curr_active_slave - also, we would skip
1097 * slaves between the curr_active_slave and primary_slave that may be up
1100 if ((bond->primary_slave) &&
1101 (!bond->params.arp_interval) &&
1102 (IS_UP(bond->primary_slave->dev))) {
1103 new_active = bond->primary_slave;
1106 /* remember where to stop iterating over the slaves */
1107 old_active = new_active;
1109 bond_for_each_slave_from(bond, new_active, i, old_active) {
1110 if (IS_UP(new_active->dev)) {
1111 if (new_active->link == BOND_LINK_UP) {
1113 } else if (new_active->link == BOND_LINK_BACK) {
1114 /* link up, but waiting for stabilization */
1115 if (new_active->delay < mintime) {
1116 mintime = new_active->delay;
1117 bestslave = new_active;
1127 * change_active_interface - change the active slave into the specified one
1128 * @bond: our bonding struct
1129 * @new: the new slave to make the active one
1131 * Set the new slave to the bond's settings and unset them on the old
1132 * curr_active_slave.
1133 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1135 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1136 * because it is apparently the best available slave we have, even though its
1137 * updelay hasn't timed out yet.
1139 * If new_active is not NULL, caller must hold bond->lock for read and
1140 * curr_slave_lock for write_bh.
1142 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1144 struct slave *old_active = bond->curr_active_slave;
1146 if (old_active == new_active) {
1151 new_active->jiffies = jiffies;
1153 if (new_active->link == BOND_LINK_BACK) {
1154 if (USES_PRIMARY(bond->params.mode)) {
1155 printk(KERN_INFO DRV_NAME
1156 ": %s: making interface %s the new "
1157 "active one %d ms earlier.\n",
1158 bond->dev->name, new_active->dev->name,
1159 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1162 new_active->delay = 0;
1163 new_active->link = BOND_LINK_UP;
1165 if (bond->params.mode == BOND_MODE_8023AD) {
1166 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1169 if ((bond->params.mode == BOND_MODE_TLB) ||
1170 (bond->params.mode == BOND_MODE_ALB)) {
1171 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1174 if (USES_PRIMARY(bond->params.mode)) {
1175 printk(KERN_INFO DRV_NAME
1176 ": %s: making interface %s the new "
1178 bond->dev->name, new_active->dev->name);
1183 if (USES_PRIMARY(bond->params.mode)) {
1184 bond_mc_swap(bond, new_active, old_active);
1187 if ((bond->params.mode == BOND_MODE_TLB) ||
1188 (bond->params.mode == BOND_MODE_ALB)) {
1189 bond_alb_handle_active_change(bond, new_active);
1191 bond_set_slave_inactive_flags(old_active);
1193 bond_set_slave_active_flags(new_active);
1195 bond->curr_active_slave = new_active;
1198 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1200 bond_set_slave_inactive_flags(old_active);
1204 bond_set_slave_active_flags(new_active);
1206 if (bond->params.fail_over_mac)
1207 bond_do_fail_over_mac(bond, new_active,
1210 bond->send_grat_arp = bond->params.num_grat_arp;
1211 bond_send_gratuitous_arp(bond);
1213 bond->send_unsol_na = bond->params.num_unsol_na;
1214 bond_send_unsolicited_na(bond);
1216 write_unlock_bh(&bond->curr_slave_lock);
1217 read_unlock(&bond->lock);
1219 netdev_bonding_change(bond->dev);
1221 read_lock(&bond->lock);
1222 write_lock_bh(&bond->curr_slave_lock);
1228 * bond_select_active_slave - select a new active slave, if needed
1229 * @bond: our bonding struct
1231 * This functions shoud be called when one of the following occurs:
1232 * - The old curr_active_slave has been released or lost its link.
1233 * - The primary_slave has got its link back.
1234 * - A slave has got its link back and there's no old curr_active_slave.
1236 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1238 void bond_select_active_slave(struct bonding *bond)
1240 struct slave *best_slave;
1243 best_slave = bond_find_best_slave(bond);
1244 if (best_slave != bond->curr_active_slave) {
1245 bond_change_active_slave(bond, best_slave);
1246 rv = bond_set_carrier(bond);
1250 if (netif_carrier_ok(bond->dev)) {
1251 printk(KERN_INFO DRV_NAME
1252 ": %s: first active interface up!\n",
1255 printk(KERN_INFO DRV_NAME ": %s: "
1256 "now running without any active interface !\n",
1262 /*--------------------------- slave list handling ---------------------------*/
1265 * This function attaches the slave to the end of list.
1267 * bond->lock held for writing by caller.
1269 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1271 if (bond->first_slave == NULL) { /* attaching the first slave */
1272 new_slave->next = new_slave;
1273 new_slave->prev = new_slave;
1274 bond->first_slave = new_slave;
1276 new_slave->next = bond->first_slave;
1277 new_slave->prev = bond->first_slave->prev;
1278 new_slave->next->prev = new_slave;
1279 new_slave->prev->next = new_slave;
1286 * This function detaches the slave from the list.
1287 * WARNING: no check is made to verify if the slave effectively
1288 * belongs to <bond>.
1289 * Nothing is freed on return, structures are just unchained.
1290 * If any slave pointer in bond was pointing to <slave>,
1291 * it should be changed by the calling function.
1293 * bond->lock held for writing by caller.
1295 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1298 slave->next->prev = slave->prev;
1302 slave->prev->next = slave->next;
1305 if (bond->first_slave == slave) { /* slave is the first slave */
1306 if (bond->slave_cnt > 1) { /* there are more slave */
1307 bond->first_slave = slave->next;
1309 bond->first_slave = NULL; /* slave was the last one */
1318 /*---------------------------------- IOCTL ----------------------------------*/
1320 static int bond_sethwaddr(struct net_device *bond_dev,
1321 struct net_device *slave_dev)
1323 dprintk("bond_dev=%p\n", bond_dev);
1324 dprintk("slave_dev=%p\n", slave_dev);
1325 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1326 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1330 #define BOND_VLAN_FEATURES \
1331 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1332 NETIF_F_HW_VLAN_FILTER)
1335 * Compute the common dev->feature set available to all slaves. Some
1336 * feature bits are managed elsewhere, so preserve those feature bits
1337 * on the master device.
1339 static int bond_compute_features(struct bonding *bond)
1341 struct slave *slave;
1342 struct net_device *bond_dev = bond->dev;
1343 unsigned long features = bond_dev->features;
1344 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1345 bond_dev->hard_header_len);
1348 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1349 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1351 if (!bond->first_slave)
1354 features &= ~NETIF_F_ONE_FOR_ALL;
1356 bond_for_each_slave(bond, slave, i) {
1357 features = netdev_increment_features(features,
1358 slave->dev->features,
1359 NETIF_F_ONE_FOR_ALL);
1360 if (slave->dev->hard_header_len > max_hard_header_len)
1361 max_hard_header_len = slave->dev->hard_header_len;
1365 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1366 bond_dev->features = netdev_fix_features(features, NULL);
1367 bond_dev->hard_header_len = max_hard_header_len;
1373 static void bond_setup_by_slave(struct net_device *bond_dev,
1374 struct net_device *slave_dev)
1376 struct bonding *bond = bond_dev->priv;
1378 bond_dev->neigh_setup = slave_dev->neigh_setup;
1379 bond_dev->header_ops = slave_dev->header_ops;
1381 bond_dev->type = slave_dev->type;
1382 bond_dev->hard_header_len = slave_dev->hard_header_len;
1383 bond_dev->addr_len = slave_dev->addr_len;
1385 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1386 slave_dev->addr_len);
1387 bond->setup_by_slave = 1;
1390 /* enslave device <slave> to bond device <master> */
1391 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1393 struct bonding *bond = bond_dev->priv;
1394 struct slave *new_slave = NULL;
1395 struct dev_mc_list *dmi;
1396 struct sockaddr addr;
1398 int old_features = bond_dev->features;
1401 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1402 slave_dev->do_ioctl == NULL) {
1403 printk(KERN_WARNING DRV_NAME
1404 ": %s: Warning: no link monitoring support for %s\n",
1405 bond_dev->name, slave_dev->name);
1408 /* bond must be initialized by bond_open() before enslaving */
1409 if (!(bond_dev->flags & IFF_UP)) {
1410 printk(KERN_WARNING DRV_NAME
1411 " %s: master_dev is not up in bond_enslave\n",
1415 /* already enslaved */
1416 if (slave_dev->flags & IFF_SLAVE) {
1417 dprintk("Error, Device was already enslaved\n");
1421 /* vlan challenged mutual exclusion */
1422 /* no need to lock since we're protected by rtnl_lock */
1423 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1424 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1425 if (!list_empty(&bond->vlan_list)) {
1426 printk(KERN_ERR DRV_NAME
1427 ": %s: Error: cannot enslave VLAN "
1428 "challenged slave %s on VLAN enabled "
1429 "bond %s\n", bond_dev->name, slave_dev->name,
1433 printk(KERN_WARNING DRV_NAME
1434 ": %s: Warning: enslaved VLAN challenged "
1435 "slave %s. Adding VLANs will be blocked as "
1436 "long as %s is part of bond %s\n",
1437 bond_dev->name, slave_dev->name, slave_dev->name,
1439 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1442 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1443 if (bond->slave_cnt == 0) {
1444 /* First slave, and it is not VLAN challenged,
1445 * so remove the block of adding VLANs over the bond.
1447 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1452 * Old ifenslave binaries are no longer supported. These can
1453 * be identified with moderate accurary by the state of the slave:
1454 * the current ifenslave will set the interface down prior to
1455 * enslaving it; the old ifenslave will not.
1457 if ((slave_dev->flags & IFF_UP)) {
1458 printk(KERN_ERR DRV_NAME ": %s is up. "
1459 "This may be due to an out of date ifenslave.\n",
1462 goto err_undo_flags;
1465 /* set bonding device ether type by slave - bonding netdevices are
1466 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1467 * there is a need to override some of the type dependent attribs/funcs.
1469 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1470 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1472 if (bond->slave_cnt == 0) {
1473 if (slave_dev->type != ARPHRD_ETHER)
1474 bond_setup_by_slave(bond_dev, slave_dev);
1475 } else if (bond_dev->type != slave_dev->type) {
1476 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1477 "from other slaves (%d), can not enslave it.\n",
1479 slave_dev->type, bond_dev->type);
1481 goto err_undo_flags;
1484 if (slave_dev->set_mac_address == NULL) {
1485 if (bond->slave_cnt == 0) {
1486 printk(KERN_WARNING DRV_NAME
1487 ": %s: Warning: The first slave device "
1488 "specified does not support setting the MAC "
1489 "address. Setting fail_over_mac to active.",
1491 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1492 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1493 printk(KERN_ERR DRV_NAME
1494 ": %s: Error: The slave device specified "
1495 "does not support setting the MAC address, "
1496 "but fail_over_mac is not set to active.\n"
1499 goto err_undo_flags;
1503 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1506 goto err_undo_flags;
1509 /* save slave's original flags before calling
1510 * netdev_set_master and dev_open
1512 new_slave->original_flags = slave_dev->flags;
1515 * Save slave's original ("permanent") mac address for modes
1516 * that need it, and for restoring it upon release, and then
1517 * set it to the master's address
1519 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1521 if (!bond->params.fail_over_mac) {
1523 * Set slave to master's mac address. The application already
1524 * set the master's mac address to that of the first slave
1526 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1527 addr.sa_family = slave_dev->type;
1528 res = dev_set_mac_address(slave_dev, &addr);
1530 dprintk("Error %d calling set_mac_address\n", res);
1535 res = netdev_set_master(slave_dev, bond_dev);
1537 dprintk("Error %d calling netdev_set_master\n", res);
1538 goto err_restore_mac;
1540 /* open the slave since the application closed it */
1541 res = dev_open(slave_dev);
1543 dprintk("Openning slave %s failed\n", slave_dev->name);
1544 goto err_unset_master;
1547 new_slave->dev = slave_dev;
1548 slave_dev->priv_flags |= IFF_BONDING;
1550 if ((bond->params.mode == BOND_MODE_TLB) ||
1551 (bond->params.mode == BOND_MODE_ALB)) {
1552 /* bond_alb_init_slave() must be called before all other stages since
1553 * it might fail and we do not want to have to undo everything
1555 res = bond_alb_init_slave(bond, new_slave);
1561 /* If the mode USES_PRIMARY, then the new slave gets the
1562 * master's promisc (and mc) settings only if it becomes the
1563 * curr_active_slave, and that is taken care of later when calling
1564 * bond_change_active()
1566 if (!USES_PRIMARY(bond->params.mode)) {
1567 /* set promiscuity level to new slave */
1568 if (bond_dev->flags & IFF_PROMISC) {
1569 res = dev_set_promiscuity(slave_dev, 1);
1574 /* set allmulti level to new slave */
1575 if (bond_dev->flags & IFF_ALLMULTI) {
1576 res = dev_set_allmulti(slave_dev, 1);
1581 netif_addr_lock_bh(bond_dev);
1582 /* upload master's mc_list to new slave */
1583 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1584 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1586 netif_addr_unlock_bh(bond_dev);
1589 if (bond->params.mode == BOND_MODE_8023AD) {
1590 /* add lacpdu mc addr to mc list */
1591 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1593 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1596 bond_add_vlans_on_slave(bond, slave_dev);
1598 write_lock_bh(&bond->lock);
1600 bond_attach_slave(bond, new_slave);
1602 new_slave->delay = 0;
1603 new_slave->link_failure_count = 0;
1605 bond_compute_features(bond);
1607 write_unlock_bh(&bond->lock);
1609 read_lock(&bond->lock);
1611 new_slave->last_arp_rx = jiffies;
1613 if (bond->params.miimon && !bond->params.use_carrier) {
1614 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1616 if ((link_reporting == -1) && !bond->params.arp_interval) {
1618 * miimon is set but a bonded network driver
1619 * does not support ETHTOOL/MII and
1620 * arp_interval is not set. Note: if
1621 * use_carrier is enabled, we will never go
1622 * here (because netif_carrier is always
1623 * supported); thus, we don't need to change
1624 * the messages for netif_carrier.
1626 printk(KERN_WARNING DRV_NAME
1627 ": %s: Warning: MII and ETHTOOL support not "
1628 "available for interface %s, and "
1629 "arp_interval/arp_ip_target module parameters "
1630 "not specified, thus bonding will not detect "
1631 "link failures! see bonding.txt for details.\n",
1632 bond_dev->name, slave_dev->name);
1633 } else if (link_reporting == -1) {
1634 /* unable get link status using mii/ethtool */
1635 printk(KERN_WARNING DRV_NAME
1636 ": %s: Warning: can't get link status from "
1637 "interface %s; the network driver associated "
1638 "with this interface does not support MII or "
1639 "ETHTOOL link status reporting, thus miimon "
1640 "has no effect on this interface.\n",
1641 bond_dev->name, slave_dev->name);
1645 /* check for initial state */
1646 if (!bond->params.miimon ||
1647 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1648 if (bond->params.updelay) {
1649 dprintk("Initial state of slave_dev is "
1650 "BOND_LINK_BACK\n");
1651 new_slave->link = BOND_LINK_BACK;
1652 new_slave->delay = bond->params.updelay;
1654 dprintk("Initial state of slave_dev is "
1656 new_slave->link = BOND_LINK_UP;
1658 new_slave->jiffies = jiffies;
1660 dprintk("Initial state of slave_dev is "
1661 "BOND_LINK_DOWN\n");
1662 new_slave->link = BOND_LINK_DOWN;
1665 if (bond_update_speed_duplex(new_slave) &&
1666 (new_slave->link != BOND_LINK_DOWN)) {
1667 printk(KERN_WARNING DRV_NAME
1668 ": %s: Warning: failed to get speed and duplex from %s, "
1669 "assumed to be 100Mb/sec and Full.\n",
1670 bond_dev->name, new_slave->dev->name);
1672 if (bond->params.mode == BOND_MODE_8023AD) {
1673 printk(KERN_WARNING DRV_NAME
1674 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1675 "support in base driver for proper aggregator "
1676 "selection.\n", bond_dev->name);
1680 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1681 /* if there is a primary slave, remember it */
1682 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1683 bond->primary_slave = new_slave;
1687 write_lock_bh(&bond->curr_slave_lock);
1689 switch (bond->params.mode) {
1690 case BOND_MODE_ACTIVEBACKUP:
1691 bond_set_slave_inactive_flags(new_slave);
1692 bond_select_active_slave(bond);
1694 case BOND_MODE_8023AD:
1695 /* in 802.3ad mode, the internal mechanism
1696 * will activate the slaves in the selected
1699 bond_set_slave_inactive_flags(new_slave);
1700 /* if this is the first slave */
1701 if (bond->slave_cnt == 1) {
1702 SLAVE_AD_INFO(new_slave).id = 1;
1703 /* Initialize AD with the number of times that the AD timer is called in 1 second
1704 * can be called only after the mac address of the bond is set
1706 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1707 bond->params.lacp_fast);
1709 SLAVE_AD_INFO(new_slave).id =
1710 SLAVE_AD_INFO(new_slave->prev).id + 1;
1713 bond_3ad_bind_slave(new_slave);
1717 new_slave->state = BOND_STATE_ACTIVE;
1718 bond_set_slave_inactive_flags(new_slave);
1721 dprintk("This slave is always active in trunk mode\n");
1723 /* always active in trunk mode */
1724 new_slave->state = BOND_STATE_ACTIVE;
1726 /* In trunking mode there is little meaning to curr_active_slave
1727 * anyway (it holds no special properties of the bond device),
1728 * so we can change it without calling change_active_interface()
1730 if (!bond->curr_active_slave) {
1731 bond->curr_active_slave = new_slave;
1734 } /* switch(bond_mode) */
1736 write_unlock_bh(&bond->curr_slave_lock);
1738 bond_set_carrier(bond);
1740 read_unlock(&bond->lock);
1742 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1746 printk(KERN_INFO DRV_NAME
1747 ": %s: enslaving %s as a%s interface with a%s link.\n",
1748 bond_dev->name, slave_dev->name,
1749 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1750 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1752 /* enslave is successful */
1755 /* Undo stages on error */
1757 dev_close(slave_dev);
1760 netdev_set_master(slave_dev, NULL);
1763 if (!bond->params.fail_over_mac) {
1764 /* XXX TODO - fom follow mode needs to change master's
1765 * MAC if this slave's MAC is in use by the bond, or at
1766 * least print a warning.
1768 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1769 addr.sa_family = slave_dev->type;
1770 dev_set_mac_address(slave_dev, &addr);
1777 bond_dev->features = old_features;
1783 * Try to release the slave device <slave> from the bond device <master>
1784 * It is legal to access curr_active_slave without a lock because all the function
1787 * The rules for slave state should be:
1788 * for Active/Backup:
1789 * Active stays on all backups go down
1790 * for Bonded connections:
1791 * The first up interface should be left on and all others downed.
1793 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1795 struct bonding *bond = bond_dev->priv;
1796 struct slave *slave, *oldcurrent;
1797 struct sockaddr addr;
1798 int mac_addr_differ;
1800 /* slave is not a slave or master is not master of this slave */
1801 if (!(slave_dev->flags & IFF_SLAVE) ||
1802 (slave_dev->master != bond_dev)) {
1803 printk(KERN_ERR DRV_NAME
1804 ": %s: Error: cannot release %s.\n",
1805 bond_dev->name, slave_dev->name);
1809 write_lock_bh(&bond->lock);
1811 slave = bond_get_slave_by_dev(bond, slave_dev);
1813 /* not a slave of this bond */
1814 printk(KERN_INFO DRV_NAME
1815 ": %s: %s not enslaved\n",
1816 bond_dev->name, slave_dev->name);
1817 write_unlock_bh(&bond->lock);
1821 if (!bond->params.fail_over_mac) {
1822 mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
1824 if (!mac_addr_differ && (bond->slave_cnt > 1))
1825 printk(KERN_WARNING DRV_NAME
1826 ": %s: Warning: the permanent HWaddr of %s - "
1827 "%pM - is still in use by %s. "
1828 "Set the HWaddr of %s to a different address "
1829 "to avoid conflicts.\n",
1830 bond_dev->name, slave_dev->name,
1832 bond_dev->name, slave_dev->name);
1835 /* Inform AD package of unbinding of slave. */
1836 if (bond->params.mode == BOND_MODE_8023AD) {
1837 /* must be called before the slave is
1838 * detached from the list
1840 bond_3ad_unbind_slave(slave);
1843 printk(KERN_INFO DRV_NAME
1844 ": %s: releasing %s interface %s\n",
1846 (slave->state == BOND_STATE_ACTIVE)
1847 ? "active" : "backup",
1850 oldcurrent = bond->curr_active_slave;
1852 bond->current_arp_slave = NULL;
1854 /* release the slave from its bond */
1855 bond_detach_slave(bond, slave);
1857 bond_compute_features(bond);
1859 if (bond->primary_slave == slave) {
1860 bond->primary_slave = NULL;
1863 if (oldcurrent == slave) {
1864 bond_change_active_slave(bond, NULL);
1867 if ((bond->params.mode == BOND_MODE_TLB) ||
1868 (bond->params.mode == BOND_MODE_ALB)) {
1869 /* Must be called only after the slave has been
1870 * detached from the list and the curr_active_slave
1871 * has been cleared (if our_slave == old_current),
1872 * but before a new active slave is selected.
1874 write_unlock_bh(&bond->lock);
1875 bond_alb_deinit_slave(bond, slave);
1876 write_lock_bh(&bond->lock);
1879 if (oldcurrent == slave) {
1881 * Note that we hold RTNL over this sequence, so there
1882 * is no concern that another slave add/remove event
1885 write_unlock_bh(&bond->lock);
1886 read_lock(&bond->lock);
1887 write_lock_bh(&bond->curr_slave_lock);
1889 bond_select_active_slave(bond);
1891 write_unlock_bh(&bond->curr_slave_lock);
1892 read_unlock(&bond->lock);
1893 write_lock_bh(&bond->lock);
1896 if (bond->slave_cnt == 0) {
1897 bond_set_carrier(bond);
1899 /* if the last slave was removed, zero the mac address
1900 * of the master so it will be set by the application
1901 * to the mac address of the first slave
1903 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1905 if (list_empty(&bond->vlan_list)) {
1906 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1908 printk(KERN_WARNING DRV_NAME
1909 ": %s: Warning: clearing HW address of %s while it "
1910 "still has VLANs.\n",
1911 bond_dev->name, bond_dev->name);
1912 printk(KERN_WARNING DRV_NAME
1913 ": %s: When re-adding slaves, make sure the bond's "
1914 "HW address matches its VLANs'.\n",
1917 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1918 !bond_has_challenged_slaves(bond)) {
1919 printk(KERN_INFO DRV_NAME
1920 ": %s: last VLAN challenged slave %s "
1921 "left bond %s. VLAN blocking is removed\n",
1922 bond_dev->name, slave_dev->name, bond_dev->name);
1923 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1926 write_unlock_bh(&bond->lock);
1928 /* must do this from outside any spinlocks */
1929 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1931 bond_del_vlans_from_slave(bond, slave_dev);
1933 /* If the mode USES_PRIMARY, then we should only remove its
1934 * promisc and mc settings if it was the curr_active_slave, but that was
1935 * already taken care of above when we detached the slave
1937 if (!USES_PRIMARY(bond->params.mode)) {
1938 /* unset promiscuity level from slave */
1939 if (bond_dev->flags & IFF_PROMISC) {
1940 dev_set_promiscuity(slave_dev, -1);
1943 /* unset allmulti level from slave */
1944 if (bond_dev->flags & IFF_ALLMULTI) {
1945 dev_set_allmulti(slave_dev, -1);
1948 /* flush master's mc_list from slave */
1949 netif_addr_lock_bh(bond_dev);
1950 bond_mc_list_flush(bond_dev, slave_dev);
1951 netif_addr_unlock_bh(bond_dev);
1954 netdev_set_master(slave_dev, NULL);
1956 /* close slave before restoring its mac address */
1957 dev_close(slave_dev);
1959 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1960 /* restore original ("permanent") mac address */
1961 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1962 addr.sa_family = slave_dev->type;
1963 dev_set_mac_address(slave_dev, &addr);
1966 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1967 IFF_SLAVE_INACTIVE | IFF_BONDING |
1972 return 0; /* deletion OK */
1976 * Destroy a bonding device.
1977 * Must be under rtnl_lock when this function is called.
1979 void bond_destroy(struct bonding *bond)
1981 bond_deinit(bond->dev);
1982 bond_destroy_sysfs_entry(bond);
1983 unregister_netdevice(bond->dev);
1986 static void bond_destructor(struct net_device *bond_dev)
1988 struct bonding *bond = bond_dev->priv;
1991 destroy_workqueue(bond->wq);
1993 netif_addr_lock_bh(bond_dev);
1994 bond_mc_list_destroy(bond);
1995 netif_addr_unlock_bh(bond_dev);
1997 free_netdev(bond_dev);
2001 * First release a slave and than destroy the bond if no more slaves iare left.
2002 * Must be under rtnl_lock when this function is called.
2004 int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
2006 struct bonding *bond = bond_dev->priv;
2009 ret = bond_release(bond_dev, slave_dev);
2010 if ((ret == 0) && (bond->slave_cnt == 0)) {
2011 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
2012 bond_dev->name, bond_dev->name);
2019 * This function releases all slaves.
2021 static int bond_release_all(struct net_device *bond_dev)
2023 struct bonding *bond = bond_dev->priv;
2024 struct slave *slave;
2025 struct net_device *slave_dev;
2026 struct sockaddr addr;
2028 write_lock_bh(&bond->lock);
2030 netif_carrier_off(bond_dev);
2032 if (bond->slave_cnt == 0) {
2036 bond->current_arp_slave = NULL;
2037 bond->primary_slave = NULL;
2038 bond_change_active_slave(bond, NULL);
2040 while ((slave = bond->first_slave) != NULL) {
2041 /* Inform AD package of unbinding of slave
2042 * before slave is detached from the list.
2044 if (bond->params.mode == BOND_MODE_8023AD) {
2045 bond_3ad_unbind_slave(slave);
2048 slave_dev = slave->dev;
2049 bond_detach_slave(bond, slave);
2051 /* now that the slave is detached, unlock and perform
2052 * all the undo steps that should not be called from
2055 write_unlock_bh(&bond->lock);
2057 if ((bond->params.mode == BOND_MODE_TLB) ||
2058 (bond->params.mode == BOND_MODE_ALB)) {
2059 /* must be called only after the slave
2060 * has been detached from the list
2062 bond_alb_deinit_slave(bond, slave);
2065 bond_compute_features(bond);
2067 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2068 bond_del_vlans_from_slave(bond, slave_dev);
2070 /* If the mode USES_PRIMARY, then we should only remove its
2071 * promisc and mc settings if it was the curr_active_slave, but that was
2072 * already taken care of above when we detached the slave
2074 if (!USES_PRIMARY(bond->params.mode)) {
2075 /* unset promiscuity level from slave */
2076 if (bond_dev->flags & IFF_PROMISC) {
2077 dev_set_promiscuity(slave_dev, -1);
2080 /* unset allmulti level from slave */
2081 if (bond_dev->flags & IFF_ALLMULTI) {
2082 dev_set_allmulti(slave_dev, -1);
2085 /* flush master's mc_list from slave */
2086 netif_addr_lock_bh(bond_dev);
2087 bond_mc_list_flush(bond_dev, slave_dev);
2088 netif_addr_unlock_bh(bond_dev);
2091 netdev_set_master(slave_dev, NULL);
2093 /* close slave before restoring its mac address */
2094 dev_close(slave_dev);
2096 if (!bond->params.fail_over_mac) {
2097 /* restore original ("permanent") mac address*/
2098 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2099 addr.sa_family = slave_dev->type;
2100 dev_set_mac_address(slave_dev, &addr);
2103 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2104 IFF_SLAVE_INACTIVE);
2108 /* re-acquire the lock before getting the next slave */
2109 write_lock_bh(&bond->lock);
2112 /* zero the mac address of the master so it will be
2113 * set by the application to the mac address of the
2116 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2118 if (list_empty(&bond->vlan_list)) {
2119 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2121 printk(KERN_WARNING DRV_NAME
2122 ": %s: Warning: clearing HW address of %s while it "
2123 "still has VLANs.\n",
2124 bond_dev->name, bond_dev->name);
2125 printk(KERN_WARNING DRV_NAME
2126 ": %s: When re-adding slaves, make sure the bond's "
2127 "HW address matches its VLANs'.\n",
2131 printk(KERN_INFO DRV_NAME
2132 ": %s: released all slaves\n",
2136 write_unlock_bh(&bond->lock);
2142 * This function changes the active slave to slave <slave_dev>.
2143 * It returns -EINVAL in the following cases.
2144 * - <slave_dev> is not found in the list.
2145 * - There is not active slave now.
2146 * - <slave_dev> is already active.
2147 * - The link state of <slave_dev> is not BOND_LINK_UP.
2148 * - <slave_dev> is not running.
2149 * In these cases, this fuction does nothing.
2150 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2152 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2154 struct bonding *bond = bond_dev->priv;
2155 struct slave *old_active = NULL;
2156 struct slave *new_active = NULL;
2159 if (!USES_PRIMARY(bond->params.mode)) {
2163 /* Verify that master_dev is indeed the master of slave_dev */
2164 if (!(slave_dev->flags & IFF_SLAVE) ||
2165 (slave_dev->master != bond_dev)) {
2169 read_lock(&bond->lock);
2171 read_lock(&bond->curr_slave_lock);
2172 old_active = bond->curr_active_slave;
2173 read_unlock(&bond->curr_slave_lock);
2175 new_active = bond_get_slave_by_dev(bond, slave_dev);
2178 * Changing to the current active: do nothing; return success.
2180 if (new_active && (new_active == old_active)) {
2181 read_unlock(&bond->lock);
2187 (new_active->link == BOND_LINK_UP) &&
2188 IS_UP(new_active->dev)) {
2189 write_lock_bh(&bond->curr_slave_lock);
2190 bond_change_active_slave(bond, new_active);
2191 write_unlock_bh(&bond->curr_slave_lock);
2196 read_unlock(&bond->lock);
2201 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2203 struct bonding *bond = bond_dev->priv;
2205 info->bond_mode = bond->params.mode;
2206 info->miimon = bond->params.miimon;
2208 read_lock(&bond->lock);
2209 info->num_slaves = bond->slave_cnt;
2210 read_unlock(&bond->lock);
2215 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2217 struct bonding *bond = bond_dev->priv;
2218 struct slave *slave;
2221 if (info->slave_id < 0) {
2225 read_lock(&bond->lock);
2227 bond_for_each_slave(bond, slave, i) {
2228 if (i == (int)info->slave_id) {
2234 read_unlock(&bond->lock);
2237 strcpy(info->slave_name, slave->dev->name);
2238 info->link = slave->link;
2239 info->state = slave->state;
2240 info->link_failure_count = slave->link_failure_count;
2248 /*-------------------------------- Monitoring -------------------------------*/
2251 static int bond_miimon_inspect(struct bonding *bond)
2253 struct slave *slave;
2254 int i, link_state, commit = 0;
2256 bond_for_each_slave(bond, slave, i) {
2257 slave->new_link = BOND_LINK_NOCHANGE;
2259 link_state = bond_check_dev_link(bond, slave->dev, 0);
2261 switch (slave->link) {
2266 slave->link = BOND_LINK_FAIL;
2267 slave->delay = bond->params.downdelay;
2269 printk(KERN_INFO DRV_NAME
2270 ": %s: link status down for %s"
2271 "interface %s, disabling it in %d ms.\n",
2273 (bond->params.mode ==
2274 BOND_MODE_ACTIVEBACKUP) ?
2275 ((slave->state == BOND_STATE_ACTIVE) ?
2276 "active " : "backup ") : "",
2278 bond->params.downdelay * bond->params.miimon);
2281 case BOND_LINK_FAIL:
2284 * recovered before downdelay expired
2286 slave->link = BOND_LINK_UP;
2287 slave->jiffies = jiffies;
2288 printk(KERN_INFO DRV_NAME
2289 ": %s: link status up again after %d "
2290 "ms for interface %s.\n",
2292 (bond->params.downdelay - slave->delay) *
2293 bond->params.miimon,
2298 if (slave->delay <= 0) {
2299 slave->new_link = BOND_LINK_DOWN;
2307 case BOND_LINK_DOWN:
2311 slave->link = BOND_LINK_BACK;
2312 slave->delay = bond->params.updelay;
2315 printk(KERN_INFO DRV_NAME
2316 ": %s: link status up for "
2317 "interface %s, enabling it in %d ms.\n",
2318 bond->dev->name, slave->dev->name,
2319 bond->params.updelay *
2320 bond->params.miimon);
2323 case BOND_LINK_BACK:
2325 slave->link = BOND_LINK_DOWN;
2326 printk(KERN_INFO DRV_NAME
2327 ": %s: link status down again after %d "
2328 "ms for interface %s.\n",
2330 (bond->params.updelay - slave->delay) *
2331 bond->params.miimon,
2337 if (slave->delay <= 0) {
2338 slave->new_link = BOND_LINK_UP;
2351 static void bond_miimon_commit(struct bonding *bond)
2353 struct slave *slave;
2356 bond_for_each_slave(bond, slave, i) {
2357 switch (slave->new_link) {
2358 case BOND_LINK_NOCHANGE:
2362 slave->link = BOND_LINK_UP;
2363 slave->jiffies = jiffies;
2365 if (bond->params.mode == BOND_MODE_8023AD) {
2366 /* prevent it from being the active one */
2367 slave->state = BOND_STATE_BACKUP;
2368 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2369 /* make it immediately active */
2370 slave->state = BOND_STATE_ACTIVE;
2371 } else if (slave != bond->primary_slave) {
2372 /* prevent it from being the active one */
2373 slave->state = BOND_STATE_BACKUP;
2376 printk(KERN_INFO DRV_NAME
2377 ": %s: link status definitely "
2378 "up for interface %s.\n",
2379 bond->dev->name, slave->dev->name);
2381 /* notify ad that the link status has changed */
2382 if (bond->params.mode == BOND_MODE_8023AD)
2383 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2385 if ((bond->params.mode == BOND_MODE_TLB) ||
2386 (bond->params.mode == BOND_MODE_ALB))
2387 bond_alb_handle_link_change(bond, slave,
2390 if (!bond->curr_active_slave ||
2391 (slave == bond->primary_slave))
2396 case BOND_LINK_DOWN:
2397 if (slave->link_failure_count < UINT_MAX)
2398 slave->link_failure_count++;
2400 slave->link = BOND_LINK_DOWN;
2402 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2403 bond->params.mode == BOND_MODE_8023AD)
2404 bond_set_slave_inactive_flags(slave);
2406 printk(KERN_INFO DRV_NAME
2407 ": %s: link status definitely down for "
2408 "interface %s, disabling it\n",
2409 bond->dev->name, slave->dev->name);
2411 if (bond->params.mode == BOND_MODE_8023AD)
2412 bond_3ad_handle_link_change(slave,
2415 if (bond->params.mode == BOND_MODE_TLB ||
2416 bond->params.mode == BOND_MODE_ALB)
2417 bond_alb_handle_link_change(bond, slave,
2420 if (slave == bond->curr_active_slave)
2426 printk(KERN_ERR DRV_NAME
2427 ": %s: invalid new link %d on slave %s\n",
2428 bond->dev->name, slave->new_link,
2430 slave->new_link = BOND_LINK_NOCHANGE;
2437 write_lock_bh(&bond->curr_slave_lock);
2438 bond_select_active_slave(bond);
2439 write_unlock_bh(&bond->curr_slave_lock);
2442 bond_set_carrier(bond);
2448 * Really a wrapper that splits the mii monitor into two phases: an
2449 * inspection, then (if inspection indicates something needs to be done)
2450 * an acquisition of appropriate locks followed by a commit phase to
2451 * implement whatever link state changes are indicated.
2453 void bond_mii_monitor(struct work_struct *work)
2455 struct bonding *bond = container_of(work, struct bonding,
2458 read_lock(&bond->lock);
2459 if (bond->kill_timers)
2462 if (bond->slave_cnt == 0)
2465 if (bond->send_grat_arp) {
2466 read_lock(&bond->curr_slave_lock);
2467 bond_send_gratuitous_arp(bond);
2468 read_unlock(&bond->curr_slave_lock);
2471 if (bond->send_unsol_na) {
2472 read_lock(&bond->curr_slave_lock);
2473 bond_send_unsolicited_na(bond);
2474 read_unlock(&bond->curr_slave_lock);
2477 if (bond_miimon_inspect(bond)) {
2478 read_unlock(&bond->lock);
2480 read_lock(&bond->lock);
2482 bond_miimon_commit(bond);
2484 read_unlock(&bond->lock);
2485 rtnl_unlock(); /* might sleep, hold no other locks */
2486 read_lock(&bond->lock);
2490 if (bond->params.miimon)
2491 queue_delayed_work(bond->wq, &bond->mii_work,
2492 msecs_to_jiffies(bond->params.miimon));
2494 read_unlock(&bond->lock);
2497 static __be32 bond_glean_dev_ip(struct net_device *dev)
2499 struct in_device *idev;
2500 struct in_ifaddr *ifa;
2507 idev = __in_dev_get_rcu(dev);
2511 ifa = idev->ifa_list;
2515 addr = ifa->ifa_local;
2521 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2523 struct vlan_entry *vlan;
2525 if (ip == bond->master_ip)
2528 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2529 if (ip == vlan->vlan_ip)
2537 * We go to the (large) trouble of VLAN tagging ARP frames because
2538 * switches in VLAN mode (especially if ports are configured as
2539 * "native" to a VLAN) might not pass non-tagged frames.
2541 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2543 struct sk_buff *skb;
2545 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2546 slave_dev->name, dest_ip, src_ip, vlan_id);
2548 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2549 NULL, slave_dev->dev_addr, NULL);
2552 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2556 skb = vlan_put_tag(skb, vlan_id);
2558 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2566 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2569 __be32 *targets = bond->params.arp_targets;
2570 struct vlan_entry *vlan;
2571 struct net_device *vlan_dev;
2575 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2578 dprintk("basa: target %x\n", targets[i]);
2579 if (list_empty(&bond->vlan_list)) {
2580 dprintk("basa: empty vlan: arp_send\n");
2581 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2582 bond->master_ip, 0);
2587 * If VLANs are configured, we do a route lookup to
2588 * determine which VLAN interface would be used, so we
2589 * can tag the ARP with the proper VLAN tag.
2591 memset(&fl, 0, sizeof(fl));
2592 fl.fl4_dst = targets[i];
2593 fl.fl4_tos = RTO_ONLINK;
2595 rv = ip_route_output_key(&init_net, &rt, &fl);
2597 if (net_ratelimit()) {
2598 printk(KERN_WARNING DRV_NAME
2599 ": %s: no route to arp_ip_target %pI4\n",
2600 bond->dev->name, &fl.fl4_dst);
2606 * This target is not on a VLAN
2608 if (rt->u.dst.dev == bond->dev) {
2610 dprintk("basa: rtdev == bond->dev: arp_send\n");
2611 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2612 bond->master_ip, 0);
2617 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2618 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2619 if (vlan_dev == rt->u.dst.dev) {
2620 vlan_id = vlan->vlan_id;
2621 dprintk("basa: vlan match on %s %d\n",
2622 vlan_dev->name, vlan_id);
2629 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2630 vlan->vlan_ip, vlan_id);
2634 if (net_ratelimit()) {
2635 printk(KERN_WARNING DRV_NAME
2636 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2637 bond->dev->name, &fl.fl4_dst,
2638 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2645 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2646 * for each VLAN above us.
2648 * Caller must hold curr_slave_lock for read or better
2650 static void bond_send_gratuitous_arp(struct bonding *bond)
2652 struct slave *slave = bond->curr_active_slave;
2653 struct vlan_entry *vlan;
2654 struct net_device *vlan_dev;
2656 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2657 slave ? slave->dev->name : "NULL");
2659 if (!slave || !bond->send_grat_arp ||
2660 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
2663 bond->send_grat_arp--;
2665 if (bond->master_ip) {
2666 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2667 bond->master_ip, 0);
2670 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2671 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2672 if (vlan->vlan_ip) {
2673 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2674 vlan->vlan_ip, vlan->vlan_id);
2679 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2682 __be32 *targets = bond->params.arp_targets;
2684 targets = bond->params.arp_targets;
2685 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2686 dprintk("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2687 &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
2688 if (sip == targets[i]) {
2689 if (bond_has_this_ip(bond, tip))
2690 slave->last_arp_rx = jiffies;
2696 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2699 struct slave *slave;
2700 struct bonding *bond;
2701 unsigned char *arp_ptr;
2704 if (dev_net(dev) != &init_net)
2707 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2711 read_lock(&bond->lock);
2713 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2714 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2715 orig_dev ? orig_dev->name : "NULL");
2717 slave = bond_get_slave_by_dev(bond, orig_dev);
2718 if (!slave || !slave_do_arp_validate(bond, slave))
2721 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2725 if (arp->ar_hln != dev->addr_len ||
2726 skb->pkt_type == PACKET_OTHERHOST ||
2727 skb->pkt_type == PACKET_LOOPBACK ||
2728 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2729 arp->ar_pro != htons(ETH_P_IP) ||
2733 arp_ptr = (unsigned char *)(arp + 1);
2734 arp_ptr += dev->addr_len;
2735 memcpy(&sip, arp_ptr, 4);
2736 arp_ptr += 4 + dev->addr_len;
2737 memcpy(&tip, arp_ptr, 4);
2739 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2740 bond->dev->name, slave->dev->name, slave->state,
2741 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2745 * Backup slaves won't see the ARP reply, but do come through
2746 * here for each ARP probe (so we swap the sip/tip to validate
2747 * the probe). In a "redundant switch, common router" type of
2748 * configuration, the ARP probe will (hopefully) travel from
2749 * the active, through one switch, the router, then the other
2750 * switch before reaching the backup.
2752 if (slave->state == BOND_STATE_ACTIVE)
2753 bond_validate_arp(bond, slave, sip, tip);
2755 bond_validate_arp(bond, slave, tip, sip);
2758 read_unlock(&bond->lock);
2761 return NET_RX_SUCCESS;
2765 * this function is called regularly to monitor each slave's link
2766 * ensuring that traffic is being sent and received when arp monitoring
2767 * is used in load-balancing mode. if the adapter has been dormant, then an
2768 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2769 * arp monitoring in active backup mode.
2771 void bond_loadbalance_arp_mon(struct work_struct *work)
2773 struct bonding *bond = container_of(work, struct bonding,
2775 struct slave *slave, *oldcurrent;
2776 int do_failover = 0;
2780 read_lock(&bond->lock);
2782 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2784 if (bond->kill_timers) {
2788 if (bond->slave_cnt == 0) {
2792 read_lock(&bond->curr_slave_lock);
2793 oldcurrent = bond->curr_active_slave;
2794 read_unlock(&bond->curr_slave_lock);
2796 /* see if any of the previous devices are up now (i.e. they have
2797 * xmt and rcv traffic). the curr_active_slave does not come into
2798 * the picture unless it is null. also, slave->jiffies is not needed
2799 * here because we send an arp on each slave and give a slave as
2800 * long as it needs to get the tx/rx within the delta.
2801 * TODO: what about up/down delay in arp mode? it wasn't here before
2804 bond_for_each_slave(bond, slave, i) {
2805 if (slave->link != BOND_LINK_UP) {
2806 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2807 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
2809 slave->link = BOND_LINK_UP;
2810 slave->state = BOND_STATE_ACTIVE;
2812 /* primary_slave has no meaning in round-robin
2813 * mode. the window of a slave being up and
2814 * curr_active_slave being null after enslaving
2818 printk(KERN_INFO DRV_NAME
2819 ": %s: link status definitely "
2820 "up for interface %s, ",
2825 printk(KERN_INFO DRV_NAME
2826 ": %s: interface %s is now up\n",
2832 /* slave->link == BOND_LINK_UP */
2834 /* not all switches will respond to an arp request
2835 * when the source ip is 0, so don't take the link down
2836 * if we don't know our ip yet
2838 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2839 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
2841 slave->link = BOND_LINK_DOWN;
2842 slave->state = BOND_STATE_BACKUP;
2844 if (slave->link_failure_count < UINT_MAX) {
2845 slave->link_failure_count++;
2848 printk(KERN_INFO DRV_NAME
2849 ": %s: interface %s is now down.\n",
2853 if (slave == oldcurrent) {
2859 /* note: if switch is in round-robin mode, all links
2860 * must tx arp to ensure all links rx an arp - otherwise
2861 * links may oscillate or not come up at all; if switch is
2862 * in something like xor mode, there is nothing we can
2863 * do - all replies will be rx'ed on same link causing slaves
2864 * to be unstable during low/no traffic periods
2866 if (IS_UP(slave->dev)) {
2867 bond_arp_send_all(bond, slave);
2872 write_lock_bh(&bond->curr_slave_lock);
2874 bond_select_active_slave(bond);
2876 write_unlock_bh(&bond->curr_slave_lock);
2880 if (bond->params.arp_interval)
2881 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2883 read_unlock(&bond->lock);
2887 * Called to inspect slaves for active-backup mode ARP monitor link state
2888 * changes. Sets new_link in slaves to specify what action should take
2889 * place for the slave. Returns 0 if no changes are found, >0 if changes
2890 * to link states must be committed.
2892 * Called with bond->lock held for read.
2894 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
2896 struct slave *slave;
2899 bond_for_each_slave(bond, slave, i) {
2900 slave->new_link = BOND_LINK_NOCHANGE;
2902 if (slave->link != BOND_LINK_UP) {
2903 if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2905 slave->new_link = BOND_LINK_UP;
2913 * Give slaves 2*delta after being enslaved or made
2914 * active. This avoids bouncing, as the last receive
2915 * times need a full ARP monitor cycle to be updated.
2917 if (!time_after_eq(jiffies, slave->jiffies +
2918 2 * delta_in_ticks))
2922 * Backup slave is down if:
2923 * - No current_arp_slave AND
2924 * - more than 3*delta since last receive AND
2925 * - the bond has an IP address
2927 * Note: a non-null current_arp_slave indicates
2928 * the curr_active_slave went down and we are
2929 * searching for a new one; under this condition
2930 * we only take the curr_active_slave down - this
2931 * gives each slave a chance to tx/rx traffic
2932 * before being taken out
2934 if (slave->state == BOND_STATE_BACKUP &&
2935 !bond->current_arp_slave &&
2936 time_after(jiffies, slave_last_rx(bond, slave) +
2937 3 * delta_in_ticks)) {
2938 slave->new_link = BOND_LINK_DOWN;
2943 * Active slave is down if:
2944 * - more than 2*delta since transmitting OR
2945 * - (more than 2*delta since receive AND
2946 * the bond has an IP address)
2948 if ((slave->state == BOND_STATE_ACTIVE) &&
2949 (time_after_eq(jiffies, slave->dev->trans_start +
2950 2 * delta_in_ticks) ||
2951 (time_after_eq(jiffies, slave_last_rx(bond, slave)
2952 + 2 * delta_in_ticks)))) {
2953 slave->new_link = BOND_LINK_DOWN;
2958 read_lock(&bond->curr_slave_lock);
2961 * Trigger a commit if the primary option setting has changed.
2963 if (bond->primary_slave &&
2964 (bond->primary_slave != bond->curr_active_slave) &&
2965 (bond->primary_slave->link == BOND_LINK_UP))
2968 read_unlock(&bond->curr_slave_lock);
2974 * Called to commit link state changes noted by inspection step of
2975 * active-backup mode ARP monitor.
2977 * Called with RTNL and bond->lock for read.
2979 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2981 struct slave *slave;
2984 bond_for_each_slave(bond, slave, i) {
2985 switch (slave->new_link) {
2986 case BOND_LINK_NOCHANGE:
2990 write_lock_bh(&bond->curr_slave_lock);
2992 if (!bond->curr_active_slave &&
2993 time_before_eq(jiffies, slave->dev->trans_start +
2995 slave->link = BOND_LINK_UP;
2996 bond_change_active_slave(bond, slave);
2997 bond->current_arp_slave = NULL;
2999 printk(KERN_INFO DRV_NAME
3000 ": %s: %s is up and now the "
3001 "active interface\n",
3002 bond->dev->name, slave->dev->name);
3004 } else if (bond->curr_active_slave != slave) {
3005 /* this slave has just come up but we
3006 * already have a current slave; this can
3007 * also happen if bond_enslave adds a new
3008 * slave that is up while we are searching
3011 slave->link = BOND_LINK_UP;
3012 bond_set_slave_inactive_flags(slave);
3013 bond->current_arp_slave = NULL;
3015 printk(KERN_INFO DRV_NAME
3016 ": %s: backup interface %s is now up\n",
3017 bond->dev->name, slave->dev->name);
3020 write_unlock_bh(&bond->curr_slave_lock);
3024 case BOND_LINK_DOWN:
3025 if (slave->link_failure_count < UINT_MAX)
3026 slave->link_failure_count++;
3028 slave->link = BOND_LINK_DOWN;
3030 if (slave == bond->curr_active_slave) {
3031 printk(KERN_INFO DRV_NAME
3032 ": %s: link status down for active "
3033 "interface %s, disabling it\n",
3034 bond->dev->name, slave->dev->name);
3036 bond_set_slave_inactive_flags(slave);
3038 write_lock_bh(&bond->curr_slave_lock);
3040 bond_select_active_slave(bond);
3041 if (bond->curr_active_slave)
3042 bond->curr_active_slave->jiffies =
3045 write_unlock_bh(&bond->curr_slave_lock);
3047 bond->current_arp_slave = NULL;
3049 } else if (slave->state == BOND_STATE_BACKUP) {
3050 printk(KERN_INFO DRV_NAME
3051 ": %s: backup interface %s is now down\n",
3052 bond->dev->name, slave->dev->name);
3054 bond_set_slave_inactive_flags(slave);
3059 printk(KERN_ERR DRV_NAME
3060 ": %s: impossible: new_link %d on slave %s\n",
3061 bond->dev->name, slave->new_link,
3067 * No race with changes to primary via sysfs, as we hold rtnl.
3069 if (bond->primary_slave &&
3070 (bond->primary_slave != bond->curr_active_slave) &&
3071 (bond->primary_slave->link == BOND_LINK_UP)) {
3072 write_lock_bh(&bond->curr_slave_lock);
3073 bond_change_active_slave(bond, bond->primary_slave);
3074 write_unlock_bh(&bond->curr_slave_lock);
3077 bond_set_carrier(bond);
3081 * Send ARP probes for active-backup mode ARP monitor.
3083 * Called with bond->lock held for read.
3085 static void bond_ab_arp_probe(struct bonding *bond)
3087 struct slave *slave;
3090 read_lock(&bond->curr_slave_lock);
3092 if (bond->current_arp_slave && bond->curr_active_slave)
3093 printk("PROBE: c_arp %s && cas %s BAD\n",
3094 bond->current_arp_slave->dev->name,
3095 bond->curr_active_slave->dev->name);
3097 if (bond->curr_active_slave) {
3098 bond_arp_send_all(bond, bond->curr_active_slave);
3099 read_unlock(&bond->curr_slave_lock);
3103 read_unlock(&bond->curr_slave_lock);
3105 /* if we don't have a curr_active_slave, search for the next available
3106 * backup slave from the current_arp_slave and make it the candidate
3107 * for becoming the curr_active_slave
3110 if (!bond->current_arp_slave) {
3111 bond->current_arp_slave = bond->first_slave;
3112 if (!bond->current_arp_slave)
3116 bond_set_slave_inactive_flags(bond->current_arp_slave);
3118 /* search for next candidate */
3119 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3120 if (IS_UP(slave->dev)) {
3121 slave->link = BOND_LINK_BACK;
3122 bond_set_slave_active_flags(slave);
3123 bond_arp_send_all(bond, slave);
3124 slave->jiffies = jiffies;
3125 bond->current_arp_slave = slave;
3129 /* if the link state is up at this point, we
3130 * mark it down - this can happen if we have
3131 * simultaneous link failures and
3132 * reselect_active_interface doesn't make this
3133 * one the current slave so it is still marked
3134 * up when it is actually down
3136 if (slave->link == BOND_LINK_UP) {
3137 slave->link = BOND_LINK_DOWN;
3138 if (slave->link_failure_count < UINT_MAX)
3139 slave->link_failure_count++;
3141 bond_set_slave_inactive_flags(slave);
3143 printk(KERN_INFO DRV_NAME
3144 ": %s: backup interface %s is now down.\n",
3145 bond->dev->name, slave->dev->name);
3150 void bond_activebackup_arp_mon(struct work_struct *work)
3152 struct bonding *bond = container_of(work, struct bonding,
3156 read_lock(&bond->lock);
3158 if (bond->kill_timers)
3161 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3163 if (bond->slave_cnt == 0)
3166 if (bond->send_grat_arp) {
3167 read_lock(&bond->curr_slave_lock);
3168 bond_send_gratuitous_arp(bond);
3169 read_unlock(&bond->curr_slave_lock);
3172 if (bond->send_unsol_na) {
3173 read_lock(&bond->curr_slave_lock);
3174 bond_send_unsolicited_na(bond);
3175 read_unlock(&bond->curr_slave_lock);
3178 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3179 read_unlock(&bond->lock);
3181 read_lock(&bond->lock);
3183 bond_ab_arp_commit(bond, delta_in_ticks);
3185 read_unlock(&bond->lock);
3187 read_lock(&bond->lock);
3190 bond_ab_arp_probe(bond);
3193 if (bond->params.arp_interval) {
3194 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3197 read_unlock(&bond->lock);
3200 /*------------------------------ proc/seq_file-------------------------------*/
3202 #ifdef CONFIG_PROC_FS
3204 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3206 struct bonding *bond = seq->private;
3208 struct slave *slave;
3211 /* make sure the bond won't be taken away */
3212 read_lock(&dev_base_lock);
3213 read_lock(&bond->lock);
3216 return SEQ_START_TOKEN;
3219 bond_for_each_slave(bond, slave, i) {
3220 if (++off == *pos) {
3228 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3230 struct bonding *bond = seq->private;
3231 struct slave *slave = v;
3234 if (v == SEQ_START_TOKEN) {
3235 return bond->first_slave;
3238 slave = slave->next;
3240 return (slave == bond->first_slave) ? NULL : slave;
3243 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3245 struct bonding *bond = seq->private;
3247 read_unlock(&bond->lock);
3248 read_unlock(&dev_base_lock);
3251 static void bond_info_show_master(struct seq_file *seq)
3253 struct bonding *bond = seq->private;
3257 read_lock(&bond->curr_slave_lock);
3258 curr = bond->curr_active_slave;
3259 read_unlock(&bond->curr_slave_lock);
3261 seq_printf(seq, "Bonding Mode: %s",
3262 bond_mode_name(bond->params.mode));
3264 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3265 bond->params.fail_over_mac)
3266 seq_printf(seq, " (fail_over_mac %s)",
3267 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
3269 seq_printf(seq, "\n");
3271 if (bond->params.mode == BOND_MODE_XOR ||
3272 bond->params.mode == BOND_MODE_8023AD) {
3273 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3274 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3275 bond->params.xmit_policy);
3278 if (USES_PRIMARY(bond->params.mode)) {
3279 seq_printf(seq, "Primary Slave: %s\n",
3280 (bond->primary_slave) ?
3281 bond->primary_slave->dev->name : "None");
3283 seq_printf(seq, "Currently Active Slave: %s\n",
3284 (curr) ? curr->dev->name : "None");
3287 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3289 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3290 seq_printf(seq, "Up Delay (ms): %d\n",
3291 bond->params.updelay * bond->params.miimon);
3292 seq_printf(seq, "Down Delay (ms): %d\n",
3293 bond->params.downdelay * bond->params.miimon);
3296 /* ARP information */
3297 if(bond->params.arp_interval > 0) {
3299 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3300 bond->params.arp_interval);
3302 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3304 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3305 if (!bond->params.arp_targets[i])
3308 seq_printf(seq, ",");
3309 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
3312 seq_printf(seq, "\n");
3315 if (bond->params.mode == BOND_MODE_8023AD) {
3316 struct ad_info ad_info;
3318 seq_puts(seq, "\n802.3ad info\n");
3319 seq_printf(seq, "LACP rate: %s\n",
3320 (bond->params.lacp_fast) ? "fast" : "slow");
3322 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3323 seq_printf(seq, "bond %s has no active aggregator\n",
3326 seq_printf(seq, "Active Aggregator Info:\n");
3328 seq_printf(seq, "\tAggregator ID: %d\n",
3329 ad_info.aggregator_id);
3330 seq_printf(seq, "\tNumber of ports: %d\n",
3332 seq_printf(seq, "\tActor Key: %d\n",
3334 seq_printf(seq, "\tPartner Key: %d\n",
3335 ad_info.partner_key);
3336 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3337 ad_info.partner_system);
3342 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3344 struct bonding *bond = seq->private;
3346 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3347 seq_printf(seq, "MII Status: %s\n",
3348 (slave->link == BOND_LINK_UP) ? "up" : "down");
3349 seq_printf(seq, "Link Failure Count: %u\n",
3350 slave->link_failure_count);
3352 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
3354 if (bond->params.mode == BOND_MODE_8023AD) {
3355 const struct aggregator *agg
3356 = SLAVE_AD_INFO(slave).port.aggregator;
3359 seq_printf(seq, "Aggregator ID: %d\n",
3360 agg->aggregator_identifier);
3362 seq_puts(seq, "Aggregator ID: N/A\n");
3367 static int bond_info_seq_show(struct seq_file *seq, void *v)
3369 if (v == SEQ_START_TOKEN) {
3370 seq_printf(seq, "%s\n", version);
3371 bond_info_show_master(seq);
3373 bond_info_show_slave(seq, v);
3379 static struct seq_operations bond_info_seq_ops = {
3380 .start = bond_info_seq_start,
3381 .next = bond_info_seq_next,
3382 .stop = bond_info_seq_stop,
3383 .show = bond_info_seq_show,
3386 static int bond_info_open(struct inode *inode, struct file *file)
3388 struct seq_file *seq;
3389 struct proc_dir_entry *proc;
3392 res = seq_open(file, &bond_info_seq_ops);
3394 /* recover the pointer buried in proc_dir_entry data */
3395 seq = file->private_data;
3397 seq->private = proc->data;
3403 static const struct file_operations bond_info_fops = {
3404 .owner = THIS_MODULE,
3405 .open = bond_info_open,
3407 .llseek = seq_lseek,
3408 .release = seq_release,
3411 static int bond_create_proc_entry(struct bonding *bond)
3413 struct net_device *bond_dev = bond->dev;
3415 if (bond_proc_dir) {
3416 bond->proc_entry = proc_create_data(bond_dev->name,
3417 S_IRUGO, bond_proc_dir,
3418 &bond_info_fops, bond);
3419 if (bond->proc_entry == NULL) {
3420 printk(KERN_WARNING DRV_NAME
3421 ": Warning: Cannot create /proc/net/%s/%s\n",
3422 DRV_NAME, bond_dev->name);
3424 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3431 static void bond_remove_proc_entry(struct bonding *bond)
3433 if (bond_proc_dir && bond->proc_entry) {
3434 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3435 memset(bond->proc_file_name, 0, IFNAMSIZ);
3436 bond->proc_entry = NULL;
3440 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3441 * Caller must hold rtnl_lock.
3443 static void bond_create_proc_dir(void)
3445 int len = strlen(DRV_NAME);
3447 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3448 bond_proc_dir = bond_proc_dir->next) {
3449 if ((bond_proc_dir->namelen == len) &&
3450 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3455 if (!bond_proc_dir) {
3456 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3457 if (bond_proc_dir) {
3458 bond_proc_dir->owner = THIS_MODULE;
3460 printk(KERN_WARNING DRV_NAME
3461 ": Warning: cannot create /proc/net/%s\n",
3467 /* Destroy the bonding directory under /proc/net, if empty.
3468 * Caller must hold rtnl_lock.
3470 static void bond_destroy_proc_dir(void)
3472 struct proc_dir_entry *de;
3474 if (!bond_proc_dir) {
3478 /* verify that the /proc dir is empty */
3479 for (de = bond_proc_dir->subdir; de; de = de->next) {
3480 /* ignore . and .. */
3481 if (*(de->name) != '.') {
3487 if (bond_proc_dir->owner == THIS_MODULE) {
3488 bond_proc_dir->owner = NULL;
3491 remove_proc_entry(DRV_NAME, init_net.proc_net);
3492 bond_proc_dir = NULL;
3495 #endif /* CONFIG_PROC_FS */
3497 /*-------------------------- netdev event handling --------------------------*/
3500 * Change device name
3502 static int bond_event_changename(struct bonding *bond)
3504 #ifdef CONFIG_PROC_FS
3505 bond_remove_proc_entry(bond);
3506 bond_create_proc_entry(bond);
3508 down_write(&(bonding_rwsem));
3509 bond_destroy_sysfs_entry(bond);
3510 bond_create_sysfs_entry(bond);
3511 up_write(&(bonding_rwsem));
3515 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3517 struct bonding *event_bond = bond_dev->priv;
3520 case NETDEV_CHANGENAME:
3521 return bond_event_changename(event_bond);
3522 case NETDEV_UNREGISTER:
3523 bond_release_all(event_bond->dev);
3532 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3534 struct net_device *bond_dev = slave_dev->master;
3535 struct bonding *bond = bond_dev->priv;
3538 case NETDEV_UNREGISTER:
3540 if (bond->setup_by_slave)
3541 bond_release_and_destroy(bond_dev, slave_dev);
3543 bond_release(bond_dev, slave_dev);
3548 * TODO: is this what we get if somebody
3549 * sets up a hierarchical bond, then rmmod's
3550 * one of the slave bonding devices?
3555 * ... Or is it this?
3558 case NETDEV_CHANGEMTU:
3560 * TODO: Should slaves be allowed to
3561 * independently alter their MTU? For
3562 * an active-backup bond, slaves need
3563 * not be the same type of device, so
3564 * MTUs may vary. For other modes,
3565 * slaves arguably should have the
3566 * same MTUs. To do this, we'd need to
3567 * take over the slave's change_mtu
3568 * function for the duration of their
3572 case NETDEV_CHANGENAME:
3574 * TODO: handle changing the primary's name
3577 case NETDEV_FEAT_CHANGE:
3578 bond_compute_features(bond);
3588 * bond_netdev_event: handle netdev notifier chain events.
3590 * This function receives events for the netdev chain. The caller (an
3591 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3592 * locks for us to safely manipulate the slave devices (RTNL lock,
3595 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3597 struct net_device *event_dev = (struct net_device *)ptr;
3599 if (dev_net(event_dev) != &init_net)
3602 dprintk("event_dev: %s, event: %lx\n",
3603 (event_dev ? event_dev->name : "None"),
3606 if (!(event_dev->priv_flags & IFF_BONDING))
3609 if (event_dev->flags & IFF_MASTER) {
3610 dprintk("IFF_MASTER\n");
3611 return bond_master_netdev_event(event, event_dev);
3614 if (event_dev->flags & IFF_SLAVE) {
3615 dprintk("IFF_SLAVE\n");
3616 return bond_slave_netdev_event(event, event_dev);
3623 * bond_inetaddr_event: handle inetaddr notifier chain events.
3625 * We keep track of device IPs primarily to use as source addresses in
3626 * ARP monitor probes (rather than spewing out broadcasts all the time).
3628 * We track one IP for the main device (if it has one), plus one per VLAN.
3630 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3632 struct in_ifaddr *ifa = ptr;
3633 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3634 struct bonding *bond;
3635 struct vlan_entry *vlan;
3637 if (dev_net(ifa->ifa_dev->dev) != &init_net)
3640 list_for_each_entry(bond, &bond_dev_list, bond_list) {
3641 if (bond->dev == event_dev) {
3644 bond->master_ip = ifa->ifa_local;
3647 bond->master_ip = bond_glean_dev_ip(bond->dev);
3654 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3655 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3656 if (vlan_dev == event_dev) {
3659 vlan->vlan_ip = ifa->ifa_local;
3663 bond_glean_dev_ip(vlan_dev);
3674 static struct notifier_block bond_netdev_notifier = {
3675 .notifier_call = bond_netdev_event,
3678 static struct notifier_block bond_inetaddr_notifier = {
3679 .notifier_call = bond_inetaddr_event,
3682 /*-------------------------- Packet type handling ---------------------------*/
3684 /* register to receive lacpdus on a bond */
3685 static void bond_register_lacpdu(struct bonding *bond)
3687 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3689 /* initialize packet type */
3690 pk_type->type = PKT_TYPE_LACPDU;
3691 pk_type->dev = bond->dev;
3692 pk_type->func = bond_3ad_lacpdu_recv;
3694 dev_add_pack(pk_type);
3697 /* unregister to receive lacpdus on a bond */
3698 static void bond_unregister_lacpdu(struct bonding *bond)
3700 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3703 void bond_register_arp(struct bonding *bond)
3705 struct packet_type *pt = &bond->arp_mon_pt;
3710 pt->type = htons(ETH_P_ARP);
3711 pt->dev = bond->dev;
3712 pt->func = bond_arp_rcv;
3716 void bond_unregister_arp(struct bonding *bond)
3718 struct packet_type *pt = &bond->arp_mon_pt;
3720 dev_remove_pack(pt);
3724 /*---------------------------- Hashing Policies -----------------------------*/
3727 * Hash for the output device based upon layer 2 and layer 3 data. If
3728 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3730 static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3731 struct net_device *bond_dev, int count)
3733 struct ethhdr *data = (struct ethhdr *)skb->data;
3734 struct iphdr *iph = ip_hdr(skb);
3736 if (skb->protocol == htons(ETH_P_IP)) {
3737 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3738 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3741 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3745 * Hash for the output device based upon layer 3 and layer 4 data. If
3746 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3747 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3749 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3750 struct net_device *bond_dev, int count)
3752 struct ethhdr *data = (struct ethhdr *)skb->data;
3753 struct iphdr *iph = ip_hdr(skb);
3754 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3757 if (skb->protocol == htons(ETH_P_IP)) {
3758 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
3759 (iph->protocol == IPPROTO_TCP ||
3760 iph->protocol == IPPROTO_UDP)) {
3761 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3763 return (layer4_xor ^
3764 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3768 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3772 * Hash for the output device based upon layer 2 data
3774 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3775 struct net_device *bond_dev, int count)
3777 struct ethhdr *data = (struct ethhdr *)skb->data;
3779 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3782 /*-------------------------- Device entry points ----------------------------*/
3784 static int bond_open(struct net_device *bond_dev)
3786 struct bonding *bond = bond_dev->priv;
3788 bond->kill_timers = 0;
3790 if ((bond->params.mode == BOND_MODE_TLB) ||
3791 (bond->params.mode == BOND_MODE_ALB)) {
3792 /* bond_alb_initialize must be called before the timer
3795 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3796 /* something went wrong - fail the open operation */
3800 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3801 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3804 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3805 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3806 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3809 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3810 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3811 INIT_DELAYED_WORK(&bond->arp_work,
3812 bond_activebackup_arp_mon);
3814 INIT_DELAYED_WORK(&bond->arp_work,
3815 bond_loadbalance_arp_mon);
3817 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3818 if (bond->params.arp_validate)
3819 bond_register_arp(bond);
3822 if (bond->params.mode == BOND_MODE_8023AD) {
3823 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3824 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3825 /* register to receive LACPDUs */
3826 bond_register_lacpdu(bond);
3832 static int bond_close(struct net_device *bond_dev)
3834 struct bonding *bond = bond_dev->priv;
3836 if (bond->params.mode == BOND_MODE_8023AD) {
3837 /* Unregister the receive of LACPDUs */
3838 bond_unregister_lacpdu(bond);
3841 if (bond->params.arp_validate)
3842 bond_unregister_arp(bond);
3844 write_lock_bh(&bond->lock);
3846 bond->send_grat_arp = 0;
3847 bond->send_unsol_na = 0;
3849 /* signal timers not to re-arm */
3850 bond->kill_timers = 1;
3852 write_unlock_bh(&bond->lock);
3854 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3855 cancel_delayed_work(&bond->mii_work);
3858 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3859 cancel_delayed_work(&bond->arp_work);
3862 switch (bond->params.mode) {
3863 case BOND_MODE_8023AD:
3864 cancel_delayed_work(&bond->ad_work);
3868 cancel_delayed_work(&bond->alb_work);
3875 if ((bond->params.mode == BOND_MODE_TLB) ||
3876 (bond->params.mode == BOND_MODE_ALB)) {
3877 /* Must be called only after all
3878 * slaves have been released
3880 bond_alb_deinitialize(bond);
3886 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3888 struct bonding *bond = bond_dev->priv;
3889 struct net_device_stats *stats = &(bond->stats), *sstats;
3890 struct net_device_stats local_stats;
3891 struct slave *slave;
3894 memset(&local_stats, 0, sizeof(struct net_device_stats));
3896 read_lock_bh(&bond->lock);
3898 bond_for_each_slave(bond, slave, i) {
3899 sstats = slave->dev->get_stats(slave->dev);
3900 local_stats.rx_packets += sstats->rx_packets;
3901 local_stats.rx_bytes += sstats->rx_bytes;
3902 local_stats.rx_errors += sstats->rx_errors;
3903 local_stats.rx_dropped += sstats->rx_dropped;
3905 local_stats.tx_packets += sstats->tx_packets;
3906 local_stats.tx_bytes += sstats->tx_bytes;
3907 local_stats.tx_errors += sstats->tx_errors;
3908 local_stats.tx_dropped += sstats->tx_dropped;
3910 local_stats.multicast += sstats->multicast;
3911 local_stats.collisions += sstats->collisions;
3913 local_stats.rx_length_errors += sstats->rx_length_errors;
3914 local_stats.rx_over_errors += sstats->rx_over_errors;
3915 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3916 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3917 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3918 local_stats.rx_missed_errors += sstats->rx_missed_errors;
3920 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3921 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3922 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3923 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3924 local_stats.tx_window_errors += sstats->tx_window_errors;
3927 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3929 read_unlock_bh(&bond->lock);
3934 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3936 struct net_device *slave_dev = NULL;
3937 struct ifbond k_binfo;
3938 struct ifbond __user *u_binfo = NULL;
3939 struct ifslave k_sinfo;
3940 struct ifslave __user *u_sinfo = NULL;
3941 struct mii_ioctl_data *mii = NULL;
3944 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3945 bond_dev->name, cmd);
3957 * We do this again just in case we were called by SIOCGMIIREG
3958 * instead of SIOCGMIIPHY.
3965 if (mii->reg_num == 1) {
3966 struct bonding *bond = bond_dev->priv;
3968 read_lock(&bond->lock);
3969 read_lock(&bond->curr_slave_lock);
3970 if (netif_carrier_ok(bond->dev)) {
3971 mii->val_out = BMSR_LSTATUS;
3973 read_unlock(&bond->curr_slave_lock);
3974 read_unlock(&bond->lock);
3978 case BOND_INFO_QUERY_OLD:
3979 case SIOCBONDINFOQUERY:
3980 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3982 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3986 res = bond_info_query(bond_dev, &k_binfo);
3988 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3994 case BOND_SLAVE_INFO_QUERY_OLD:
3995 case SIOCBONDSLAVEINFOQUERY:
3996 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3998 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
4002 res = bond_slave_info_query(bond_dev, &k_sinfo);
4004 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
4015 if (!capable(CAP_NET_ADMIN)) {
4019 down_write(&(bonding_rwsem));
4020 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
4022 dprintk("slave_dev=%p: \n", slave_dev);
4027 dprintk("slave_dev->name=%s: \n", slave_dev->name);
4029 case BOND_ENSLAVE_OLD:
4030 case SIOCBONDENSLAVE:
4031 res = bond_enslave(bond_dev, slave_dev);
4033 case BOND_RELEASE_OLD:
4034 case SIOCBONDRELEASE:
4035 res = bond_release(bond_dev, slave_dev);
4037 case BOND_SETHWADDR_OLD:
4038 case SIOCBONDSETHWADDR:
4039 res = bond_sethwaddr(bond_dev, slave_dev);
4041 case BOND_CHANGE_ACTIVE_OLD:
4042 case SIOCBONDCHANGEACTIVE:
4043 res = bond_ioctl_change_active(bond_dev, slave_dev);
4052 up_write(&(bonding_rwsem));
4056 static void bond_set_multicast_list(struct net_device *bond_dev)
4058 struct bonding *bond = bond_dev->priv;
4059 struct dev_mc_list *dmi;
4062 * Do promisc before checking multicast_mode
4064 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
4066 * FIXME: Need to handle the error when one of the multi-slaves
4069 bond_set_promiscuity(bond, 1);
4072 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
4073 bond_set_promiscuity(bond, -1);
4076 /* set allmulti flag to slaves */
4077 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
4079 * FIXME: Need to handle the error when one of the multi-slaves
4082 bond_set_allmulti(bond, 1);
4085 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
4086 bond_set_allmulti(bond, -1);
4089 read_lock(&bond->lock);
4091 bond->flags = bond_dev->flags;
4093 /* looking for addresses to add to slaves' mc list */
4094 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
4095 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
4096 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4100 /* looking for addresses to delete from slaves' list */
4101 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
4102 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
4103 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4107 /* save master's multicast list */
4108 bond_mc_list_destroy(bond);
4109 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4111 read_unlock(&bond->lock);
4115 * Change the MTU of all of a master's slaves to match the master
4117 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4119 struct bonding *bond = bond_dev->priv;
4120 struct slave *slave, *stop_at;
4124 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
4125 (bond_dev ? bond_dev->name : "None"), new_mtu);
4127 /* Can't hold bond->lock with bh disabled here since
4128 * some base drivers panic. On the other hand we can't
4129 * hold bond->lock without bh disabled because we'll
4130 * deadlock. The only solution is to rely on the fact
4131 * that we're under rtnl_lock here, and the slaves
4132 * list won't change. This doesn't solve the problem
4133 * of setting the slave's MTU while it is
4134 * transmitting, but the assumption is that the base
4135 * driver can handle that.
4137 * TODO: figure out a way to safely iterate the slaves
4138 * list, but without holding a lock around the actual
4139 * call to the base driver.
4142 bond_for_each_slave(bond, slave, i) {
4143 dprintk("s %p s->p %p c_m %p\n", slave,
4144 slave->prev, slave->dev->change_mtu);
4146 res = dev_set_mtu(slave->dev, new_mtu);
4149 /* If we failed to set the slave's mtu to the new value
4150 * we must abort the operation even in ACTIVE_BACKUP
4151 * mode, because if we allow the backup slaves to have
4152 * different mtu values than the active slave we'll
4153 * need to change their mtu when doing a failover. That
4154 * means changing their mtu from timer context, which
4155 * is probably not a good idea.
4157 dprintk("err %d %s\n", res, slave->dev->name);
4162 bond_dev->mtu = new_mtu;
4167 /* unwind from head to the slave that failed */
4169 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4172 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4174 dprintk("unwind err %d dev %s\n", tmp_res,
4185 * Note that many devices must be down to change the HW address, and
4186 * downing the master releases all slaves. We can make bonds full of
4187 * bonding devices to test this, however.
4189 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4191 struct bonding *bond = bond_dev->priv;
4192 struct sockaddr *sa = addr, tmp_sa;
4193 struct slave *slave, *stop_at;
4197 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4200 * If fail_over_mac is set to active, do nothing and return
4201 * success. Returning an error causes ifenslave to fail.
4203 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
4206 if (!is_valid_ether_addr(sa->sa_data)) {
4207 return -EADDRNOTAVAIL;
4210 /* Can't hold bond->lock with bh disabled here since
4211 * some base drivers panic. On the other hand we can't
4212 * hold bond->lock without bh disabled because we'll
4213 * deadlock. The only solution is to rely on the fact
4214 * that we're under rtnl_lock here, and the slaves
4215 * list won't change. This doesn't solve the problem
4216 * of setting the slave's hw address while it is
4217 * transmitting, but the assumption is that the base
4218 * driver can handle that.
4220 * TODO: figure out a way to safely iterate the slaves
4221 * list, but without holding a lock around the actual
4222 * call to the base driver.
4225 bond_for_each_slave(bond, slave, i) {
4226 dprintk("slave %p %s\n", slave, slave->dev->name);
4228 if (slave->dev->set_mac_address == NULL) {
4230 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4234 res = dev_set_mac_address(slave->dev, addr);
4236 /* TODO: consider downing the slave
4238 * User should expect communications
4239 * breakage anyway until ARP finish
4242 dprintk("err %d %s\n", res, slave->dev->name);
4248 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4252 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4253 tmp_sa.sa_family = bond_dev->type;
4255 /* unwind from head to the slave that failed */
4257 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4260 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4262 dprintk("unwind err %d dev %s\n", tmp_res,
4270 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4272 struct bonding *bond = bond_dev->priv;
4273 struct slave *slave, *start_at;
4274 int i, slave_no, res = 1;
4276 read_lock(&bond->lock);
4278 if (!BOND_IS_OK(bond)) {
4283 * Concurrent TX may collide on rr_tx_counter; we accept that
4284 * as being rare enough not to justify using an atomic op here
4286 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4288 bond_for_each_slave(bond, slave, i) {
4296 bond_for_each_slave_from(bond, slave, i, start_at) {
4297 if (IS_UP(slave->dev) &&
4298 (slave->link == BOND_LINK_UP) &&
4299 (slave->state == BOND_STATE_ACTIVE)) {
4300 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4307 /* no suitable interface, frame not sent */
4310 read_unlock(&bond->lock);
4316 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4317 * the bond has a usable interface.
4319 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4321 struct bonding *bond = bond_dev->priv;
4324 read_lock(&bond->lock);
4325 read_lock(&bond->curr_slave_lock);
4327 if (!BOND_IS_OK(bond)) {
4331 if (!bond->curr_active_slave)
4334 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4338 /* no suitable interface, frame not sent */
4341 read_unlock(&bond->curr_slave_lock);
4342 read_unlock(&bond->lock);
4347 * In bond_xmit_xor() , we determine the output device by using a pre-
4348 * determined xmit_hash_policy(), If the selected device is not enabled,
4349 * find the next active slave.
4351 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4353 struct bonding *bond = bond_dev->priv;
4354 struct slave *slave, *start_at;
4359 read_lock(&bond->lock);
4361 if (!BOND_IS_OK(bond)) {
4365 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4367 bond_for_each_slave(bond, slave, i) {
4376 bond_for_each_slave_from(bond, slave, i, start_at) {
4377 if (IS_UP(slave->dev) &&
4378 (slave->link == BOND_LINK_UP) &&
4379 (slave->state == BOND_STATE_ACTIVE)) {
4380 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4387 /* no suitable interface, frame not sent */
4390 read_unlock(&bond->lock);
4395 * in broadcast mode, we send everything to all usable interfaces.
4397 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4399 struct bonding *bond = bond_dev->priv;
4400 struct slave *slave, *start_at;
4401 struct net_device *tx_dev = NULL;
4405 read_lock(&bond->lock);
4407 if (!BOND_IS_OK(bond)) {
4411 read_lock(&bond->curr_slave_lock);
4412 start_at = bond->curr_active_slave;
4413 read_unlock(&bond->curr_slave_lock);
4419 bond_for_each_slave_from(bond, slave, i, start_at) {
4420 if (IS_UP(slave->dev) &&
4421 (slave->link == BOND_LINK_UP) &&
4422 (slave->state == BOND_STATE_ACTIVE)) {
4424 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4426 printk(KERN_ERR DRV_NAME
4427 ": %s: Error: bond_xmit_broadcast(): "
4428 "skb_clone() failed\n",
4433 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4435 dev_kfree_skb(skb2);
4439 tx_dev = slave->dev;
4444 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4449 /* no suitable interface, frame not sent */
4452 /* frame sent to all suitable interfaces */
4453 read_unlock(&bond->lock);
4457 /*------------------------- Device initialization ---------------------------*/
4459 static void bond_set_xmit_hash_policy(struct bonding *bond)
4461 switch (bond->params.xmit_policy) {
4462 case BOND_XMIT_POLICY_LAYER23:
4463 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4465 case BOND_XMIT_POLICY_LAYER34:
4466 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4468 case BOND_XMIT_POLICY_LAYER2:
4470 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4476 * set bond mode specific net device operations
4478 void bond_set_mode_ops(struct bonding *bond, int mode)
4480 struct net_device *bond_dev = bond->dev;
4483 case BOND_MODE_ROUNDROBIN:
4484 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4486 case BOND_MODE_ACTIVEBACKUP:
4487 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4490 bond_dev->hard_start_xmit = bond_xmit_xor;
4491 bond_set_xmit_hash_policy(bond);
4493 case BOND_MODE_BROADCAST:
4494 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4496 case BOND_MODE_8023AD:
4497 bond_set_master_3ad_flags(bond);
4498 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4499 bond_set_xmit_hash_policy(bond);
4502 bond_set_master_alb_flags(bond);
4505 bond_dev->hard_start_xmit = bond_alb_xmit;
4506 bond_dev->set_mac_address = bond_alb_set_mac_address;
4509 /* Should never happen, mode already checked */
4510 printk(KERN_ERR DRV_NAME
4511 ": %s: Error: Unknown bonding mode %d\n",
4518 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4519 struct ethtool_drvinfo *drvinfo)
4521 strncpy(drvinfo->driver, DRV_NAME, 32);
4522 strncpy(drvinfo->version, DRV_VERSION, 32);
4523 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4526 static const struct ethtool_ops bond_ethtool_ops = {
4527 .get_drvinfo = bond_ethtool_get_drvinfo,
4528 .get_link = ethtool_op_get_link,
4529 .get_tx_csum = ethtool_op_get_tx_csum,
4530 .get_sg = ethtool_op_get_sg,
4531 .get_tso = ethtool_op_get_tso,
4532 .get_ufo = ethtool_op_get_ufo,
4533 .get_flags = ethtool_op_get_flags,
4537 * Does not allocate but creates a /proc entry.
4540 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4542 struct bonding *bond = bond_dev->priv;
4544 dprintk("Begin bond_init for %s\n", bond_dev->name);
4546 /* initialize rwlocks */
4547 rwlock_init(&bond->lock);
4548 rwlock_init(&bond->curr_slave_lock);
4550 bond->params = *params; /* copy params struct */
4552 bond->wq = create_singlethread_workqueue(bond_dev->name);
4556 /* Initialize pointers */
4557 bond->first_slave = NULL;
4558 bond->curr_active_slave = NULL;
4559 bond->current_arp_slave = NULL;
4560 bond->primary_slave = NULL;
4561 bond->dev = bond_dev;
4562 bond->send_grat_arp = 0;
4563 bond->send_unsol_na = 0;
4564 bond->setup_by_slave = 0;
4565 INIT_LIST_HEAD(&bond->vlan_list);
4567 /* Initialize the device entry points */
4568 bond_dev->open = bond_open;
4569 bond_dev->stop = bond_close;
4570 bond_dev->get_stats = bond_get_stats;
4571 bond_dev->do_ioctl = bond_do_ioctl;
4572 bond_dev->ethtool_ops = &bond_ethtool_ops;
4573 bond_dev->set_multicast_list = bond_set_multicast_list;
4574 bond_dev->change_mtu = bond_change_mtu;
4575 bond_dev->set_mac_address = bond_set_mac_address;
4576 bond_dev->validate_addr = NULL;
4578 bond_set_mode_ops(bond, bond->params.mode);
4580 bond_dev->destructor = bond_destructor;
4582 /* Initialize the device options */
4583 bond_dev->tx_queue_len = 0;
4584 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4585 bond_dev->priv_flags |= IFF_BONDING;
4586 if (bond->params.arp_interval)
4587 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
4589 /* At first, we block adding VLANs. That's the only way to
4590 * prevent problems that occur when adding VLANs over an
4591 * empty bond. The block will be removed once non-challenged
4592 * slaves are enslaved.
4594 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4596 /* don't acquire bond device's netif_tx_lock when
4598 bond_dev->features |= NETIF_F_LLTX;
4600 /* By default, we declare the bond to be fully
4601 * VLAN hardware accelerated capable. Special
4602 * care is taken in the various xmit functions
4603 * when there are slaves that are not hw accel
4606 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4607 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4608 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4609 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4610 NETIF_F_HW_VLAN_RX |
4611 NETIF_F_HW_VLAN_FILTER);
4613 #ifdef CONFIG_PROC_FS
4614 bond_create_proc_entry(bond);
4616 list_add_tail(&bond->bond_list, &bond_dev_list);
4621 static void bond_work_cancel_all(struct bonding *bond)
4623 write_lock_bh(&bond->lock);
4624 bond->kill_timers = 1;
4625 write_unlock_bh(&bond->lock);
4627 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4628 cancel_delayed_work(&bond->mii_work);
4630 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4631 cancel_delayed_work(&bond->arp_work);
4633 if (bond->params.mode == BOND_MODE_ALB &&
4634 delayed_work_pending(&bond->alb_work))
4635 cancel_delayed_work(&bond->alb_work);
4637 if (bond->params.mode == BOND_MODE_8023AD &&
4638 delayed_work_pending(&bond->ad_work))
4639 cancel_delayed_work(&bond->ad_work);
4642 /* De-initialize device specific data.
4643 * Caller must hold rtnl_lock.
4645 static void bond_deinit(struct net_device *bond_dev)
4647 struct bonding *bond = bond_dev->priv;
4649 list_del(&bond->bond_list);
4651 bond_work_cancel_all(bond);
4653 #ifdef CONFIG_PROC_FS
4654 bond_remove_proc_entry(bond);
4658 /* Unregister and free all bond devices.
4659 * Caller must hold rtnl_lock.
4661 static void bond_free_all(void)
4663 struct bonding *bond, *nxt;
4665 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4666 struct net_device *bond_dev = bond->dev;
4668 bond_work_cancel_all(bond);
4669 /* Release the bonded slaves */
4670 bond_release_all(bond_dev);
4674 #ifdef CONFIG_PROC_FS
4675 bond_destroy_proc_dir();
4679 /*------------------------- Module initialization ---------------------------*/
4682 * Convert string input module parms. Accept either the
4683 * number of the mode or its string name. A bit complicated because
4684 * some mode names are substrings of other names, and calls from sysfs
4685 * may have whitespace in the name (trailing newlines, for example).
4687 int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
4689 int mode = -1, i, rv;
4690 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4692 for (p = (char *)buf; *p; p++)
4693 if (!(isdigit(*p) || isspace(*p)))
4697 rv = sscanf(buf, "%20s", modestr);
4699 rv = sscanf(buf, "%d", &mode);
4704 for (i = 0; tbl[i].modename; i++) {
4705 if (mode == tbl[i].mode)
4707 if (strcmp(modestr, tbl[i].modename) == 0)
4714 static int bond_check_params(struct bond_params *params)
4716 int arp_validate_value, fail_over_mac_value;
4719 * Convert string parameters.
4722 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4723 if (bond_mode == -1) {
4724 printk(KERN_ERR DRV_NAME
4725 ": Error: Invalid bonding mode \"%s\"\n",
4726 mode == NULL ? "NULL" : mode);
4731 if (xmit_hash_policy) {
4732 if ((bond_mode != BOND_MODE_XOR) &&
4733 (bond_mode != BOND_MODE_8023AD)) {
4734 printk(KERN_INFO DRV_NAME
4735 ": xor_mode param is irrelevant in mode %s\n",
4736 bond_mode_name(bond_mode));
4738 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4740 if (xmit_hashtype == -1) {
4741 printk(KERN_ERR DRV_NAME
4742 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4743 xmit_hash_policy == NULL ? "NULL" :
4751 if (bond_mode != BOND_MODE_8023AD) {
4752 printk(KERN_INFO DRV_NAME
4753 ": lacp_rate param is irrelevant in mode %s\n",
4754 bond_mode_name(bond_mode));
4756 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4757 if (lacp_fast == -1) {
4758 printk(KERN_ERR DRV_NAME
4759 ": Error: Invalid lacp rate \"%s\"\n",
4760 lacp_rate == NULL ? "NULL" : lacp_rate);
4766 if (max_bonds < 0 || max_bonds > INT_MAX) {
4767 printk(KERN_WARNING DRV_NAME
4768 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4769 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4770 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4771 max_bonds = BOND_DEFAULT_MAX_BONDS;
4775 printk(KERN_WARNING DRV_NAME
4776 ": Warning: miimon module parameter (%d), "
4777 "not in range 0-%d, so it was reset to %d\n",
4778 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4779 miimon = BOND_LINK_MON_INTERV;
4783 printk(KERN_WARNING DRV_NAME
4784 ": Warning: updelay module parameter (%d), "
4785 "not in range 0-%d, so it was reset to 0\n",
4790 if (downdelay < 0) {
4791 printk(KERN_WARNING DRV_NAME
4792 ": Warning: downdelay module parameter (%d), "
4793 "not in range 0-%d, so it was reset to 0\n",
4794 downdelay, INT_MAX);
4798 if ((use_carrier != 0) && (use_carrier != 1)) {
4799 printk(KERN_WARNING DRV_NAME
4800 ": Warning: use_carrier module parameter (%d), "
4801 "not of valid value (0/1), so it was set to 1\n",
4806 if (num_grat_arp < 0 || num_grat_arp > 255) {
4807 printk(KERN_WARNING DRV_NAME
4808 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4809 "was reset to 1 \n", num_grat_arp);
4813 if (num_unsol_na < 0 || num_unsol_na > 255) {
4814 printk(KERN_WARNING DRV_NAME
4815 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4816 "was reset to 1 \n", num_unsol_na);
4820 /* reset values for 802.3ad */
4821 if (bond_mode == BOND_MODE_8023AD) {
4823 printk(KERN_WARNING DRV_NAME
4824 ": Warning: miimon must be specified, "
4825 "otherwise bonding will not detect link "
4826 "failure, speed and duplex which are "
4827 "essential for 802.3ad operation\n");
4828 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4833 /* reset values for TLB/ALB */
4834 if ((bond_mode == BOND_MODE_TLB) ||
4835 (bond_mode == BOND_MODE_ALB)) {
4837 printk(KERN_WARNING DRV_NAME
4838 ": Warning: miimon must be specified, "
4839 "otherwise bonding will not detect link "
4840 "failure and link speed which are essential "
4841 "for TLB/ALB load balancing\n");
4842 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4847 if (bond_mode == BOND_MODE_ALB) {
4848 printk(KERN_NOTICE DRV_NAME
4849 ": In ALB mode you might experience client "
4850 "disconnections upon reconnection of a link if the "
4851 "bonding module updelay parameter (%d msec) is "
4852 "incompatible with the forwarding delay time of the "
4858 if (updelay || downdelay) {
4859 /* just warn the user the up/down delay will have
4860 * no effect since miimon is zero...
4862 printk(KERN_WARNING DRV_NAME
4863 ": Warning: miimon module parameter not set "
4864 "and updelay (%d) or downdelay (%d) module "
4865 "parameter is set; updelay and downdelay have "
4866 "no effect unless miimon is set\n",
4867 updelay, downdelay);
4870 /* don't allow arp monitoring */
4872 printk(KERN_WARNING DRV_NAME
4873 ": Warning: miimon (%d) and arp_interval (%d) "
4874 "can't be used simultaneously, disabling ARP "
4876 miimon, arp_interval);
4880 if ((updelay % miimon) != 0) {
4881 printk(KERN_WARNING DRV_NAME
4882 ": Warning: updelay (%d) is not a multiple "
4883 "of miimon (%d), updelay rounded to %d ms\n",
4884 updelay, miimon, (updelay / miimon) * miimon);
4889 if ((downdelay % miimon) != 0) {
4890 printk(KERN_WARNING DRV_NAME
4891 ": Warning: downdelay (%d) is not a multiple "
4892 "of miimon (%d), downdelay rounded to %d ms\n",
4894 (downdelay / miimon) * miimon);
4897 downdelay /= miimon;
4900 if (arp_interval < 0) {
4901 printk(KERN_WARNING DRV_NAME
4902 ": Warning: arp_interval module parameter (%d) "
4903 ", not in range 0-%d, so it was reset to %d\n",
4904 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4905 arp_interval = BOND_LINK_ARP_INTERV;
4908 for (arp_ip_count = 0;
4909 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4911 /* not complete check, but should be good enough to
4913 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4914 printk(KERN_WARNING DRV_NAME
4915 ": Warning: bad arp_ip_target module parameter "
4916 "(%s), ARP monitoring will not be performed\n",
4917 arp_ip_target[arp_ip_count]);
4920 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4921 arp_target[arp_ip_count] = ip;
4925 if (arp_interval && !arp_ip_count) {
4926 /* don't allow arping if no arp_ip_target given... */
4927 printk(KERN_WARNING DRV_NAME
4928 ": Warning: arp_interval module parameter (%d) "
4929 "specified without providing an arp_ip_target "
4930 "parameter, arp_interval was reset to 0\n",
4936 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4937 printk(KERN_ERR DRV_NAME
4938 ": arp_validate only supported in active-backup mode\n");
4941 if (!arp_interval) {
4942 printk(KERN_ERR DRV_NAME
4943 ": arp_validate requires arp_interval\n");
4947 arp_validate_value = bond_parse_parm(arp_validate,
4949 if (arp_validate_value == -1) {
4950 printk(KERN_ERR DRV_NAME
4951 ": Error: invalid arp_validate \"%s\"\n",
4952 arp_validate == NULL ? "NULL" : arp_validate);
4956 arp_validate_value = 0;
4959 printk(KERN_INFO DRV_NAME
4960 ": MII link monitoring set to %d ms\n",
4962 } else if (arp_interval) {
4965 printk(KERN_INFO DRV_NAME
4966 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4968 arp_validate_tbl[arp_validate_value].modename,
4971 for (i = 0; i < arp_ip_count; i++)
4972 printk (" %s", arp_ip_target[i]);
4976 } else if (max_bonds) {
4977 /* miimon and arp_interval not set, we need one so things
4978 * work as expected, see bonding.txt for details
4980 printk(KERN_WARNING DRV_NAME
4981 ": Warning: either miimon or arp_interval and "
4982 "arp_ip_target module parameters must be specified, "
4983 "otherwise bonding will not detect link failures! see "
4984 "bonding.txt for details.\n");
4987 if (primary && !USES_PRIMARY(bond_mode)) {
4988 /* currently, using a primary only makes sense
4989 * in active backup, TLB or ALB modes
4991 printk(KERN_WARNING DRV_NAME
4992 ": Warning: %s primary device specified but has no "
4993 "effect in %s mode\n",
4994 primary, bond_mode_name(bond_mode));
4998 if (fail_over_mac) {
4999 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5001 if (fail_over_mac_value == -1) {
5002 printk(KERN_ERR DRV_NAME
5003 ": Error: invalid fail_over_mac \"%s\"\n",
5004 arp_validate == NULL ? "NULL" : arp_validate);
5008 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5009 printk(KERN_WARNING DRV_NAME
5010 ": Warning: fail_over_mac only affects "
5011 "active-backup mode.\n");
5013 fail_over_mac_value = BOND_FOM_NONE;
5016 /* fill params struct with the proper values */
5017 params->mode = bond_mode;
5018 params->xmit_policy = xmit_hashtype;
5019 params->miimon = miimon;
5020 params->num_grat_arp = num_grat_arp;
5021 params->num_unsol_na = num_unsol_na;
5022 params->arp_interval = arp_interval;
5023 params->arp_validate = arp_validate_value;
5024 params->updelay = updelay;
5025 params->downdelay = downdelay;
5026 params->use_carrier = use_carrier;
5027 params->lacp_fast = lacp_fast;
5028 params->primary[0] = 0;
5029 params->fail_over_mac = fail_over_mac_value;
5032 strncpy(params->primary, primary, IFNAMSIZ);
5033 params->primary[IFNAMSIZ - 1] = 0;
5036 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5041 static struct lock_class_key bonding_netdev_xmit_lock_key;
5042 static struct lock_class_key bonding_netdev_addr_lock_key;
5044 static void bond_set_lockdep_class_one(struct net_device *dev,
5045 struct netdev_queue *txq,
5048 lockdep_set_class(&txq->_xmit_lock,
5049 &bonding_netdev_xmit_lock_key);
5052 static void bond_set_lockdep_class(struct net_device *dev)
5054 lockdep_set_class(&dev->addr_list_lock,
5055 &bonding_netdev_addr_lock_key);
5056 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
5059 /* Create a new bond based on the specified name and bonding parameters.
5060 * If name is NULL, obtain a suitable "bond%d" name for us.
5061 * Caller must NOT hold rtnl_lock; we need to release it here before we
5062 * set up our sysfs entries.
5064 int bond_create(char *name, struct bond_params *params)
5066 struct net_device *bond_dev;
5067 struct bonding *bond;
5071 down_write(&bonding_rwsem);
5073 /* Check to see if the bond already exists. */
5075 list_for_each_entry(bond, &bond_dev_list, bond_list)
5076 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
5077 printk(KERN_ERR DRV_NAME
5078 ": cannot add bond %s; it already exists\n",
5085 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
5088 printk(KERN_ERR DRV_NAME
5089 ": %s: eek! can't alloc netdev!\n",
5096 res = dev_alloc_name(bond_dev, "bond%d");
5101 /* bond_init() must be called after dev_alloc_name() (for the
5102 * /proc files), but before register_netdevice(), because we
5103 * need to set function pointers.
5106 res = bond_init(bond_dev, params);
5111 res = register_netdevice(bond_dev);
5116 bond_set_lockdep_class(bond_dev);
5118 netif_carrier_off(bond_dev);
5120 up_write(&bonding_rwsem);
5121 rtnl_unlock(); /* allows sysfs registration of net device */
5122 res = bond_create_sysfs_entry(bond_dev->priv);
5125 down_write(&bonding_rwsem);
5126 bond_deinit(bond_dev);
5127 unregister_netdevice(bond_dev);
5134 bond_deinit(bond_dev);
5136 free_netdev(bond_dev);
5138 up_write(&bonding_rwsem);
5143 static int __init bonding_init(void)
5147 struct bonding *bond;
5149 printk(KERN_INFO "%s", version);
5151 res = bond_check_params(&bonding_defaults);
5156 #ifdef CONFIG_PROC_FS
5157 bond_create_proc_dir();
5160 init_rwsem(&bonding_rwsem);
5162 for (i = 0; i < max_bonds; i++) {
5163 res = bond_create(NULL, &bonding_defaults);
5168 res = bond_create_sysfs();
5172 register_netdevice_notifier(&bond_netdev_notifier);
5173 register_inetaddr_notifier(&bond_inetaddr_notifier);
5174 bond_register_ipv6_notifier();
5178 list_for_each_entry(bond, &bond_dev_list, bond_list) {
5179 bond_work_cancel_all(bond);
5180 destroy_workqueue(bond->wq);
5183 bond_destroy_sysfs();
5193 static void __exit bonding_exit(void)
5195 unregister_netdevice_notifier(&bond_netdev_notifier);
5196 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
5197 bond_unregister_ipv6_notifier();
5199 bond_destroy_sysfs();
5206 module_init(bonding_init);
5207 module_exit(bonding_exit);
5208 MODULE_LICENSE("GPL");
5209 MODULE_VERSION(DRV_VERSION);
5210 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5211 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5212 MODULE_SUPPORTED_DEVICE("most ethernet devices");