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 miimon = BOND_LINK_MON_INTERV;
93 static int updelay = 0;
94 static int downdelay = 0;
95 static int use_carrier = 1;
96 static char *mode = NULL;
97 static char *primary = NULL;
98 static char *lacp_rate = NULL;
99 static char *xmit_hash_policy = NULL;
100 static int arp_interval = BOND_LINK_ARP_INTERV;
101 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
102 static char *arp_validate = NULL;
103 static int fail_over_mac = 0;
104 struct bond_params bonding_defaults;
106 module_param(max_bonds, int, 0);
107 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
108 module_param(num_grat_arp, int, 0644);
109 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
110 module_param(miimon, int, 0);
111 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
112 module_param(updelay, int, 0);
113 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
114 module_param(downdelay, int, 0);
115 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
117 module_param(use_carrier, int, 0);
118 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
119 "0 for off, 1 for on (default)");
120 module_param(mode, charp, 0);
121 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
122 "1 for active-backup, 2 for balance-xor, "
123 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
124 "6 for balance-alb");
125 module_param(primary, charp, 0);
126 MODULE_PARM_DESC(primary, "Primary network device to use");
127 module_param(lacp_rate, charp, 0);
128 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
130 module_param(xmit_hash_policy, charp, 0);
131 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
132 ", 1 for layer 3+4");
133 module_param(arp_interval, int, 0);
134 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
135 module_param_array(arp_ip_target, charp, NULL, 0);
136 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
137 module_param(arp_validate, charp, 0);
138 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
139 module_param(fail_over_mac, int, 0);
140 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on.");
142 /*----------------------------- Global variables ----------------------------*/
144 static const char * const version =
145 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
147 LIST_HEAD(bond_dev_list);
149 #ifdef CONFIG_PROC_FS
150 static struct proc_dir_entry *bond_proc_dir = NULL;
153 extern struct rw_semaphore bonding_rwsem;
154 static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
155 static int arp_ip_count = 0;
156 static int bond_mode = BOND_MODE_ROUNDROBIN;
157 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
158 static int lacp_fast = 0;
161 struct bond_parm_tbl bond_lacp_tbl[] = {
162 { "slow", AD_LACP_SLOW},
163 { "fast", AD_LACP_FAST},
167 struct bond_parm_tbl bond_mode_tbl[] = {
168 { "balance-rr", BOND_MODE_ROUNDROBIN},
169 { "active-backup", BOND_MODE_ACTIVEBACKUP},
170 { "balance-xor", BOND_MODE_XOR},
171 { "broadcast", BOND_MODE_BROADCAST},
172 { "802.3ad", BOND_MODE_8023AD},
173 { "balance-tlb", BOND_MODE_TLB},
174 { "balance-alb", BOND_MODE_ALB},
178 struct bond_parm_tbl xmit_hashtype_tbl[] = {
179 { "layer2", BOND_XMIT_POLICY_LAYER2},
180 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
181 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
185 struct bond_parm_tbl arp_validate_tbl[] = {
186 { "none", BOND_ARP_VALIDATE_NONE},
187 { "active", BOND_ARP_VALIDATE_ACTIVE},
188 { "backup", BOND_ARP_VALIDATE_BACKUP},
189 { "all", BOND_ARP_VALIDATE_ALL},
193 /*-------------------------- Forward declarations ---------------------------*/
195 static void bond_send_gratuitous_arp(struct bonding *bond);
196 static void bond_deinit(struct net_device *bond_dev);
198 /*---------------------------- General routines -----------------------------*/
200 static const char *bond_mode_name(int mode)
203 case BOND_MODE_ROUNDROBIN :
204 return "load balancing (round-robin)";
205 case BOND_MODE_ACTIVEBACKUP :
206 return "fault-tolerance (active-backup)";
208 return "load balancing (xor)";
209 case BOND_MODE_BROADCAST :
210 return "fault-tolerance (broadcast)";
211 case BOND_MODE_8023AD:
212 return "IEEE 802.3ad Dynamic link aggregation";
214 return "transmit load balancing";
216 return "adaptive load balancing";
222 /*---------------------------------- VLAN -----------------------------------*/
225 * bond_add_vlan - add a new vlan id on bond
226 * @bond: bond that got the notification
227 * @vlan_id: the vlan id to add
229 * Returns -ENOMEM if allocation failed.
231 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
233 struct vlan_entry *vlan;
235 dprintk("bond: %s, vlan id %d\n",
236 (bond ? bond->dev->name: "None"), vlan_id);
238 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
243 INIT_LIST_HEAD(&vlan->vlan_list);
244 vlan->vlan_id = vlan_id;
247 write_lock_bh(&bond->lock);
249 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
251 write_unlock_bh(&bond->lock);
253 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
259 * bond_del_vlan - delete a vlan id from bond
260 * @bond: bond that got the notification
261 * @vlan_id: the vlan id to delete
263 * returns -ENODEV if @vlan_id was not found in @bond.
265 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
267 struct vlan_entry *vlan;
270 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
272 write_lock_bh(&bond->lock);
274 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
275 if (vlan->vlan_id == vlan_id) {
276 list_del(&vlan->vlan_list);
278 if ((bond->params.mode == BOND_MODE_TLB) ||
279 (bond->params.mode == BOND_MODE_ALB)) {
280 bond_alb_clear_vlan(bond, vlan_id);
283 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
288 if (list_empty(&bond->vlan_list) &&
289 (bond->slave_cnt == 0)) {
290 /* Last VLAN removed and no slaves, so
291 * restore block on adding VLANs. This will
292 * be removed once new slaves that are not
293 * VLAN challenged will be added.
295 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
303 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
307 write_unlock_bh(&bond->lock);
312 * bond_has_challenged_slaves
313 * @bond: the bond we're working on
315 * Searches the slave list. Returns 1 if a vlan challenged slave
316 * was found, 0 otherwise.
318 * Assumes bond->lock is held.
320 static int bond_has_challenged_slaves(struct bonding *bond)
325 bond_for_each_slave(bond, slave, i) {
326 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
327 dprintk("found VLAN challenged slave - %s\n",
333 dprintk("no VLAN challenged slaves found\n");
338 * bond_next_vlan - safely skip to the next item in the vlans list.
339 * @bond: the bond we're working on
340 * @curr: item we're advancing from
342 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
343 * or @curr->next otherwise (even if it is @curr itself again).
345 * Caller must hold bond->lock
347 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
349 struct vlan_entry *next, *last;
351 if (list_empty(&bond->vlan_list)) {
356 next = list_entry(bond->vlan_list.next,
357 struct vlan_entry, vlan_list);
359 last = list_entry(bond->vlan_list.prev,
360 struct vlan_entry, vlan_list);
362 next = list_entry(bond->vlan_list.next,
363 struct vlan_entry, vlan_list);
365 next = list_entry(curr->vlan_list.next,
366 struct vlan_entry, vlan_list);
374 * bond_dev_queue_xmit - Prepare skb for xmit.
376 * @bond: bond device that got this skb for tx.
377 * @skb: hw accel VLAN tagged skb to transmit
378 * @slave_dev: slave that is supposed to xmit this skbuff
380 * When the bond gets an skb to transmit that is
381 * already hardware accelerated VLAN tagged, and it
382 * needs to relay this skb to a slave that is not
383 * hw accel capable, the skb needs to be "unaccelerated",
384 * i.e. strip the hwaccel tag and re-insert it as part
387 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
389 unsigned short uninitialized_var(vlan_id);
391 if (!list_empty(&bond->vlan_list) &&
392 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
393 vlan_get_tag(skb, &vlan_id) == 0) {
394 skb->dev = slave_dev;
395 skb = vlan_put_tag(skb, vlan_id);
397 /* vlan_put_tag() frees the skb in case of error,
398 * so return success here so the calling functions
399 * won't attempt to free is again.
404 skb->dev = slave_dev;
414 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
415 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
417 * a. This operation is performed in IOCTL context,
418 * b. The operation is protected by the RTNL semaphore in the 8021q code,
419 * c. Holding a lock with BH disabled while directly calling a base driver
420 * entry point is generally a BAD idea.
422 * The design of synchronization/protection for this operation in the 8021q
423 * module is good for one or more VLAN devices over a single physical device
424 * and cannot be extended for a teaming solution like bonding, so there is a
425 * potential race condition here where a net device from the vlan group might
426 * be referenced (either by a base driver or the 8021q code) while it is being
427 * removed from the system. However, it turns out we're not making matters
428 * worse, and if it works for regular VLAN usage it will work here too.
432 * bond_vlan_rx_register - Propagates registration to slaves
433 * @bond_dev: bonding net device that got called
434 * @grp: vlan group being registered
436 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
438 struct bonding *bond = bond_dev->priv;
444 bond_for_each_slave(bond, slave, i) {
445 struct net_device *slave_dev = slave->dev;
447 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
448 slave_dev->vlan_rx_register) {
449 slave_dev->vlan_rx_register(slave_dev, grp);
455 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
456 * @bond_dev: bonding net device that got called
457 * @vid: vlan id being added
459 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
461 struct bonding *bond = bond_dev->priv;
465 bond_for_each_slave(bond, slave, i) {
466 struct net_device *slave_dev = slave->dev;
468 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
469 slave_dev->vlan_rx_add_vid) {
470 slave_dev->vlan_rx_add_vid(slave_dev, vid);
474 res = bond_add_vlan(bond, vid);
476 printk(KERN_ERR DRV_NAME
477 ": %s: Error: Failed to add vlan id %d\n",
478 bond_dev->name, vid);
483 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
484 * @bond_dev: bonding net device that got called
485 * @vid: vlan id being removed
487 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
489 struct bonding *bond = bond_dev->priv;
491 struct net_device *vlan_dev;
494 bond_for_each_slave(bond, slave, i) {
495 struct net_device *slave_dev = slave->dev;
497 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
498 slave_dev->vlan_rx_kill_vid) {
499 /* Save and then restore vlan_dev in the grp array,
500 * since the slave's driver might clear it.
502 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
503 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
504 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
508 res = bond_del_vlan(bond, vid);
510 printk(KERN_ERR DRV_NAME
511 ": %s: Error: Failed to remove vlan id %d\n",
512 bond_dev->name, vid);
516 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
518 struct vlan_entry *vlan;
520 write_lock_bh(&bond->lock);
522 if (list_empty(&bond->vlan_list)) {
526 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
527 slave_dev->vlan_rx_register) {
528 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
531 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
532 !(slave_dev->vlan_rx_add_vid)) {
536 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
537 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
541 write_unlock_bh(&bond->lock);
544 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
546 struct vlan_entry *vlan;
547 struct net_device *vlan_dev;
549 write_lock_bh(&bond->lock);
551 if (list_empty(&bond->vlan_list)) {
555 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
556 !(slave_dev->vlan_rx_kill_vid)) {
560 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
561 /* Save and then restore vlan_dev in the grp array,
562 * since the slave's driver might clear it.
564 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
565 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
566 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
570 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
571 slave_dev->vlan_rx_register) {
572 slave_dev->vlan_rx_register(slave_dev, NULL);
576 write_unlock_bh(&bond->lock);
579 /*------------------------------- Link status -------------------------------*/
582 * Set the carrier state for the master according to the state of its
583 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
584 * do special 802.3ad magic.
586 * Returns zero if carrier state does not change, nonzero if it does.
588 static int bond_set_carrier(struct bonding *bond)
593 if (bond->slave_cnt == 0)
596 if (bond->params.mode == BOND_MODE_8023AD)
597 return bond_3ad_set_carrier(bond);
599 bond_for_each_slave(bond, slave, i) {
600 if (slave->link == BOND_LINK_UP) {
601 if (!netif_carrier_ok(bond->dev)) {
602 netif_carrier_on(bond->dev);
610 if (netif_carrier_ok(bond->dev)) {
611 netif_carrier_off(bond->dev);
618 * Get link speed and duplex from the slave's base driver
619 * using ethtool. If for some reason the call fails or the
620 * values are invalid, fake speed and duplex to 100/Full
623 static int bond_update_speed_duplex(struct slave *slave)
625 struct net_device *slave_dev = slave->dev;
626 struct ethtool_cmd etool;
629 /* Fake speed and duplex */
630 slave->speed = SPEED_100;
631 slave->duplex = DUPLEX_FULL;
633 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
636 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
640 switch (etool.speed) {
650 switch (etool.duplex) {
658 slave->speed = etool.speed;
659 slave->duplex = etool.duplex;
665 * if <dev> supports MII link status reporting, check its link status.
667 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
668 * depening upon the setting of the use_carrier parameter.
670 * Return either BMSR_LSTATUS, meaning that the link is up (or we
671 * can't tell and just pretend it is), or 0, meaning that the link is
674 * If reporting is non-zero, instead of faking link up, return -1 if
675 * both ETHTOOL and MII ioctls fail (meaning the device does not
676 * support them). If use_carrier is set, return whatever it says.
677 * It'd be nice if there was a good way to tell if a driver supports
678 * netif_carrier, but there really isn't.
680 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
682 static int (* ioctl)(struct net_device *, struct ifreq *, int);
684 struct mii_ioctl_data *mii;
686 if (bond->params.use_carrier) {
687 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
690 ioctl = slave_dev->do_ioctl;
692 /* TODO: set pointer to correct ioctl on a per team member */
693 /* bases to make this more efficient. that is, once */
694 /* we determine the correct ioctl, we will always */
695 /* call it and not the others for that team */
699 * We cannot assume that SIOCGMIIPHY will also read a
700 * register; not all network drivers (e.g., e100)
704 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
705 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
707 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
708 mii->reg_num = MII_BMSR;
709 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
710 return (mii->val_out & BMSR_LSTATUS);
716 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
717 * attempt to get link status from it if the above MII ioctls fail.
719 if (slave_dev->ethtool_ops) {
720 if (slave_dev->ethtool_ops->get_link) {
723 link = slave_dev->ethtool_ops->get_link(slave_dev);
725 return link ? BMSR_LSTATUS : 0;
730 * If reporting, report that either there's no dev->do_ioctl,
731 * or both SIOCGMIIREG and get_link failed (meaning that we
732 * cannot report link status). If not reporting, pretend
735 return (reporting ? -1 : BMSR_LSTATUS);
738 /*----------------------------- Multicast list ------------------------------*/
741 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
743 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
745 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
746 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
750 * returns dmi entry if found, NULL otherwise
752 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
754 struct dev_mc_list *idmi;
756 for (idmi = mc_list; idmi; idmi = idmi->next) {
757 if (bond_is_dmi_same(dmi, idmi)) {
766 * Push the promiscuity flag down to appropriate slaves
768 static void bond_set_promiscuity(struct bonding *bond, int inc)
770 if (USES_PRIMARY(bond->params.mode)) {
771 /* write lock already acquired */
772 if (bond->curr_active_slave) {
773 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
778 bond_for_each_slave(bond, slave, i) {
779 dev_set_promiscuity(slave->dev, inc);
785 * Push the allmulti flag down to all slaves
787 static void bond_set_allmulti(struct bonding *bond, int inc)
789 if (USES_PRIMARY(bond->params.mode)) {
790 /* write lock already acquired */
791 if (bond->curr_active_slave) {
792 dev_set_allmulti(bond->curr_active_slave->dev, inc);
797 bond_for_each_slave(bond, slave, i) {
798 dev_set_allmulti(slave->dev, inc);
804 * Add a Multicast address to slaves
807 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
809 if (USES_PRIMARY(bond->params.mode)) {
810 /* write lock already acquired */
811 if (bond->curr_active_slave) {
812 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
817 bond_for_each_slave(bond, slave, i) {
818 dev_mc_add(slave->dev, addr, alen, 0);
824 * Remove a multicast address from slave
827 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
829 if (USES_PRIMARY(bond->params.mode)) {
830 /* write lock already acquired */
831 if (bond->curr_active_slave) {
832 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
837 bond_for_each_slave(bond, slave, i) {
838 dev_mc_delete(slave->dev, addr, alen, 0);
845 * Retrieve the list of registered multicast addresses for the bonding
846 * device and retransmit an IGMP JOIN request to the current active
849 static void bond_resend_igmp_join_requests(struct bonding *bond)
851 struct in_device *in_dev;
852 struct ip_mc_list *im;
855 in_dev = __in_dev_get_rcu(bond->dev);
857 for (im = in_dev->mc_list; im; im = im->next) {
858 ip_mc_rejoin_group(im);
866 * Totally destroys the mc_list in bond
868 static void bond_mc_list_destroy(struct bonding *bond)
870 struct dev_mc_list *dmi;
874 bond->mc_list = dmi->next;
878 bond->mc_list = NULL;
882 * Copy all the Multicast addresses from src to the bonding device dst
884 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
887 struct dev_mc_list *dmi, *new_dmi;
889 for (dmi = mc_list; dmi; dmi = dmi->next) {
890 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
893 /* FIXME: Potential memory leak !!! */
897 new_dmi->next = bond->mc_list;
898 bond->mc_list = new_dmi;
899 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
900 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
901 new_dmi->dmi_users = dmi->dmi_users;
902 new_dmi->dmi_gusers = dmi->dmi_gusers;
909 * flush all members of flush->mc_list from device dev->mc_list
911 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
913 struct bonding *bond = bond_dev->priv;
914 struct dev_mc_list *dmi;
916 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
917 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
920 if (bond->params.mode == BOND_MODE_8023AD) {
921 /* del lacpdu mc addr from mc list */
922 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
924 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
928 /*--------------------------- Active slave change ---------------------------*/
931 * Update the mc list and multicast-related flags for the new and
932 * old active slaves (if any) according to the multicast mode, and
933 * promiscuous flags unconditionally.
935 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
937 struct dev_mc_list *dmi;
939 if (!USES_PRIMARY(bond->params.mode)) {
940 /* nothing to do - mc list is already up-to-date on
947 if (bond->dev->flags & IFF_PROMISC) {
948 dev_set_promiscuity(old_active->dev, -1);
951 if (bond->dev->flags & IFF_ALLMULTI) {
952 dev_set_allmulti(old_active->dev, -1);
955 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
956 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
961 if (bond->dev->flags & IFF_PROMISC) {
962 dev_set_promiscuity(new_active->dev, 1);
965 if (bond->dev->flags & IFF_ALLMULTI) {
966 dev_set_allmulti(new_active->dev, 1);
969 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
970 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
972 bond_resend_igmp_join_requests(bond);
977 * find_best_interface - select the best available slave to be the active one
978 * @bond: our bonding struct
980 * Warning: Caller must hold curr_slave_lock for writing.
982 static struct slave *bond_find_best_slave(struct bonding *bond)
984 struct slave *new_active, *old_active;
985 struct slave *bestslave = NULL;
986 int mintime = bond->params.updelay;
989 new_active = old_active = bond->curr_active_slave;
991 if (!new_active) { /* there were no active slaves left */
992 if (bond->slave_cnt > 0) { /* found one slave */
993 new_active = bond->first_slave;
995 return NULL; /* still no slave, return NULL */
999 /* first try the primary link; if arping, a link must tx/rx traffic
1000 * before it can be considered the curr_active_slave - also, we would skip
1001 * slaves between the curr_active_slave and primary_slave that may be up
1004 if ((bond->primary_slave) &&
1005 (!bond->params.arp_interval) &&
1006 (IS_UP(bond->primary_slave->dev))) {
1007 new_active = bond->primary_slave;
1010 /* remember where to stop iterating over the slaves */
1011 old_active = new_active;
1013 bond_for_each_slave_from(bond, new_active, i, old_active) {
1014 if (IS_UP(new_active->dev)) {
1015 if (new_active->link == BOND_LINK_UP) {
1017 } else if (new_active->link == BOND_LINK_BACK) {
1018 /* link up, but waiting for stabilization */
1019 if (new_active->delay < mintime) {
1020 mintime = new_active->delay;
1021 bestslave = new_active;
1031 * change_active_interface - change the active slave into the specified one
1032 * @bond: our bonding struct
1033 * @new: the new slave to make the active one
1035 * Set the new slave to the bond's settings and unset them on the old
1036 * curr_active_slave.
1037 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1039 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1040 * because it is apparently the best available slave we have, even though its
1041 * updelay hasn't timed out yet.
1043 * Warning: Caller must hold curr_slave_lock for writing.
1045 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1047 struct slave *old_active = bond->curr_active_slave;
1049 if (old_active == new_active) {
1054 if (new_active->link == BOND_LINK_BACK) {
1055 if (USES_PRIMARY(bond->params.mode)) {
1056 printk(KERN_INFO DRV_NAME
1057 ": %s: making interface %s the new "
1058 "active one %d ms earlier.\n",
1059 bond->dev->name, new_active->dev->name,
1060 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1063 new_active->delay = 0;
1064 new_active->link = BOND_LINK_UP;
1065 new_active->jiffies = jiffies;
1067 if (bond->params.mode == BOND_MODE_8023AD) {
1068 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1071 if ((bond->params.mode == BOND_MODE_TLB) ||
1072 (bond->params.mode == BOND_MODE_ALB)) {
1073 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1076 if (USES_PRIMARY(bond->params.mode)) {
1077 printk(KERN_INFO DRV_NAME
1078 ": %s: making interface %s the new "
1080 bond->dev->name, new_active->dev->name);
1085 if (USES_PRIMARY(bond->params.mode)) {
1086 bond_mc_swap(bond, new_active, old_active);
1089 if ((bond->params.mode == BOND_MODE_TLB) ||
1090 (bond->params.mode == BOND_MODE_ALB)) {
1091 bond_alb_handle_active_change(bond, new_active);
1093 bond_set_slave_inactive_flags(old_active);
1095 bond_set_slave_active_flags(new_active);
1097 bond->curr_active_slave = new_active;
1100 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1102 bond_set_slave_inactive_flags(old_active);
1106 bond_set_slave_active_flags(new_active);
1109 /* when bonding does not set the slave MAC address, the bond MAC
1110 * address is the one of the active slave.
1112 if (new_active && bond->params.fail_over_mac)
1113 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1114 new_active->dev->addr_len);
1115 bond->send_grat_arp = bond->params.num_grat_arp;
1116 if (bond->curr_active_slave &&
1117 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1118 &bond->curr_active_slave->dev->state)) {
1119 dprintk("delaying gratuitous arp on %s\n",
1120 bond->curr_active_slave->dev->name);
1122 if (bond->send_grat_arp > 0) {
1123 bond_send_gratuitous_arp(bond);
1124 bond->send_grat_arp--;
1131 * bond_select_active_slave - select a new active slave, if needed
1132 * @bond: our bonding struct
1134 * This functions shoud be called when one of the following occurs:
1135 * - The old curr_active_slave has been released or lost its link.
1136 * - The primary_slave has got its link back.
1137 * - A slave has got its link back and there's no old curr_active_slave.
1139 * Warning: Caller must hold curr_slave_lock for writing.
1141 void bond_select_active_slave(struct bonding *bond)
1143 struct slave *best_slave;
1146 best_slave = bond_find_best_slave(bond);
1147 if (best_slave != bond->curr_active_slave) {
1148 bond_change_active_slave(bond, best_slave);
1149 rv = bond_set_carrier(bond);
1153 if (netif_carrier_ok(bond->dev)) {
1154 printk(KERN_INFO DRV_NAME
1155 ": %s: first active interface up!\n",
1158 printk(KERN_INFO DRV_NAME ": %s: "
1159 "now running without any active interface !\n",
1165 /*--------------------------- slave list handling ---------------------------*/
1168 * This function attaches the slave to the end of list.
1170 * bond->lock held for writing by caller.
1172 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1174 if (bond->first_slave == NULL) { /* attaching the first slave */
1175 new_slave->next = new_slave;
1176 new_slave->prev = new_slave;
1177 bond->first_slave = new_slave;
1179 new_slave->next = bond->first_slave;
1180 new_slave->prev = bond->first_slave->prev;
1181 new_slave->next->prev = new_slave;
1182 new_slave->prev->next = new_slave;
1189 * This function detaches the slave from the list.
1190 * WARNING: no check is made to verify if the slave effectively
1191 * belongs to <bond>.
1192 * Nothing is freed on return, structures are just unchained.
1193 * If any slave pointer in bond was pointing to <slave>,
1194 * it should be changed by the calling function.
1196 * bond->lock held for writing by caller.
1198 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1201 slave->next->prev = slave->prev;
1205 slave->prev->next = slave->next;
1208 if (bond->first_slave == slave) { /* slave is the first slave */
1209 if (bond->slave_cnt > 1) { /* there are more slave */
1210 bond->first_slave = slave->next;
1212 bond->first_slave = NULL; /* slave was the last one */
1221 /*---------------------------------- IOCTL ----------------------------------*/
1223 static int bond_sethwaddr(struct net_device *bond_dev,
1224 struct net_device *slave_dev)
1226 dprintk("bond_dev=%p\n", bond_dev);
1227 dprintk("slave_dev=%p\n", slave_dev);
1228 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1229 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1233 #define BOND_VLAN_FEATURES \
1234 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1235 NETIF_F_HW_VLAN_FILTER)
1238 * Compute the common dev->feature set available to all slaves. Some
1239 * feature bits are managed elsewhere, so preserve those feature bits
1240 * on the master device.
1242 static int bond_compute_features(struct bonding *bond)
1244 struct slave *slave;
1245 struct net_device *bond_dev = bond->dev;
1246 unsigned long features = bond_dev->features;
1247 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1248 bond_dev->hard_header_len);
1251 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1252 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1253 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1255 bond_for_each_slave(bond, slave, i) {
1256 features = netdev_compute_features(features,
1257 slave->dev->features);
1258 if (slave->dev->hard_header_len > max_hard_header_len)
1259 max_hard_header_len = slave->dev->hard_header_len;
1262 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1263 bond_dev->features = features;
1264 bond_dev->hard_header_len = max_hard_header_len;
1270 static void bond_setup_by_slave(struct net_device *bond_dev,
1271 struct net_device *slave_dev)
1273 struct bonding *bond = bond_dev->priv;
1275 bond_dev->neigh_setup = slave_dev->neigh_setup;
1276 bond_dev->header_ops = slave_dev->header_ops;
1278 bond_dev->type = slave_dev->type;
1279 bond_dev->hard_header_len = slave_dev->hard_header_len;
1280 bond_dev->addr_len = slave_dev->addr_len;
1282 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1283 slave_dev->addr_len);
1284 bond->setup_by_slave = 1;
1287 /* enslave device <slave> to bond device <master> */
1288 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1290 struct bonding *bond = bond_dev->priv;
1291 struct slave *new_slave = NULL;
1292 struct dev_mc_list *dmi;
1293 struct sockaddr addr;
1295 int old_features = bond_dev->features;
1298 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1299 slave_dev->do_ioctl == NULL) {
1300 printk(KERN_WARNING DRV_NAME
1301 ": %s: Warning: no link monitoring support for %s\n",
1302 bond_dev->name, slave_dev->name);
1305 /* bond must be initialized by bond_open() before enslaving */
1306 if (!(bond_dev->flags & IFF_UP)) {
1307 printk(KERN_WARNING DRV_NAME
1308 " %s: master_dev is not up in bond_enslave\n",
1312 /* already enslaved */
1313 if (slave_dev->flags & IFF_SLAVE) {
1314 dprintk("Error, Device was already enslaved\n");
1318 /* vlan challenged mutual exclusion */
1319 /* no need to lock since we're protected by rtnl_lock */
1320 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1321 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1322 if (!list_empty(&bond->vlan_list)) {
1323 printk(KERN_ERR DRV_NAME
1324 ": %s: Error: cannot enslave VLAN "
1325 "challenged slave %s on VLAN enabled "
1326 "bond %s\n", bond_dev->name, slave_dev->name,
1330 printk(KERN_WARNING DRV_NAME
1331 ": %s: Warning: enslaved VLAN challenged "
1332 "slave %s. Adding VLANs will be blocked as "
1333 "long as %s is part of bond %s\n",
1334 bond_dev->name, slave_dev->name, slave_dev->name,
1336 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1339 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1340 if (bond->slave_cnt == 0) {
1341 /* First slave, and it is not VLAN challenged,
1342 * so remove the block of adding VLANs over the bond.
1344 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1349 * Old ifenslave binaries are no longer supported. These can
1350 * be identified with moderate accurary by the state of the slave:
1351 * the current ifenslave will set the interface down prior to
1352 * enslaving it; the old ifenslave will not.
1354 if ((slave_dev->flags & IFF_UP)) {
1355 printk(KERN_ERR DRV_NAME ": %s is up. "
1356 "This may be due to an out of date ifenslave.\n",
1359 goto err_undo_flags;
1362 /* set bonding device ether type by slave - bonding netdevices are
1363 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1364 * there is a need to override some of the type dependent attribs/funcs.
1366 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1367 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1369 if (bond->slave_cnt == 0) {
1370 if (slave_dev->type != ARPHRD_ETHER)
1371 bond_setup_by_slave(bond_dev, slave_dev);
1372 } else if (bond_dev->type != slave_dev->type) {
1373 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1374 "from other slaves (%d), can not enslave it.\n",
1376 slave_dev->type, bond_dev->type);
1378 goto err_undo_flags;
1381 if (slave_dev->set_mac_address == NULL) {
1382 if (bond->slave_cnt == 0) {
1383 printk(KERN_WARNING DRV_NAME
1384 ": %s: Warning: The first slave device "
1385 "specified does not support setting the MAC "
1386 "address. Enabling the fail_over_mac option.",
1388 bond->params.fail_over_mac = 1;
1389 } else if (!bond->params.fail_over_mac) {
1390 printk(KERN_ERR DRV_NAME
1391 ": %s: Error: The slave device specified "
1392 "does not support setting the MAC address, "
1393 "but fail_over_mac is not enabled.\n"
1396 goto err_undo_flags;
1400 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1403 goto err_undo_flags;
1406 /* save slave's original flags before calling
1407 * netdev_set_master and dev_open
1409 new_slave->original_flags = slave_dev->flags;
1412 * Save slave's original ("permanent") mac address for modes
1413 * that need it, and for restoring it upon release, and then
1414 * set it to the master's address
1416 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1418 if (!bond->params.fail_over_mac) {
1420 * Set slave to master's mac address. The application already
1421 * set the master's mac address to that of the first slave
1423 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1424 addr.sa_family = slave_dev->type;
1425 res = dev_set_mac_address(slave_dev, &addr);
1427 dprintk("Error %d calling set_mac_address\n", res);
1432 res = netdev_set_master(slave_dev, bond_dev);
1434 dprintk("Error %d calling netdev_set_master\n", res);
1435 goto err_restore_mac;
1437 /* open the slave since the application closed it */
1438 res = dev_open(slave_dev);
1440 dprintk("Openning slave %s failed\n", slave_dev->name);
1441 goto err_unset_master;
1444 new_slave->dev = slave_dev;
1445 slave_dev->priv_flags |= IFF_BONDING;
1447 if ((bond->params.mode == BOND_MODE_TLB) ||
1448 (bond->params.mode == BOND_MODE_ALB)) {
1449 /* bond_alb_init_slave() must be called before all other stages since
1450 * it might fail and we do not want to have to undo everything
1452 res = bond_alb_init_slave(bond, new_slave);
1458 /* If the mode USES_PRIMARY, then the new slave gets the
1459 * master's promisc (and mc) settings only if it becomes the
1460 * curr_active_slave, and that is taken care of later when calling
1461 * bond_change_active()
1463 if (!USES_PRIMARY(bond->params.mode)) {
1464 /* set promiscuity level to new slave */
1465 if (bond_dev->flags & IFF_PROMISC) {
1466 dev_set_promiscuity(slave_dev, 1);
1469 /* set allmulti level to new slave */
1470 if (bond_dev->flags & IFF_ALLMULTI) {
1471 dev_set_allmulti(slave_dev, 1);
1474 netif_tx_lock_bh(bond_dev);
1475 /* upload master's mc_list to new slave */
1476 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1477 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1479 netif_tx_unlock_bh(bond_dev);
1482 if (bond->params.mode == BOND_MODE_8023AD) {
1483 /* add lacpdu mc addr to mc list */
1484 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1486 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1489 bond_add_vlans_on_slave(bond, slave_dev);
1491 write_lock_bh(&bond->lock);
1493 bond_attach_slave(bond, new_slave);
1495 new_slave->delay = 0;
1496 new_slave->link_failure_count = 0;
1498 bond_compute_features(bond);
1500 new_slave->last_arp_rx = jiffies;
1502 if (bond->params.miimon && !bond->params.use_carrier) {
1503 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1505 if ((link_reporting == -1) && !bond->params.arp_interval) {
1507 * miimon is set but a bonded network driver
1508 * does not support ETHTOOL/MII and
1509 * arp_interval is not set. Note: if
1510 * use_carrier is enabled, we will never go
1511 * here (because netif_carrier is always
1512 * supported); thus, we don't need to change
1513 * the messages for netif_carrier.
1515 printk(KERN_WARNING DRV_NAME
1516 ": %s: Warning: MII and ETHTOOL support not "
1517 "available for interface %s, and "
1518 "arp_interval/arp_ip_target module parameters "
1519 "not specified, thus bonding will not detect "
1520 "link failures! see bonding.txt for details.\n",
1521 bond_dev->name, slave_dev->name);
1522 } else if (link_reporting == -1) {
1523 /* unable get link status using mii/ethtool */
1524 printk(KERN_WARNING DRV_NAME
1525 ": %s: Warning: can't get link status from "
1526 "interface %s; the network driver associated "
1527 "with this interface does not support MII or "
1528 "ETHTOOL link status reporting, thus miimon "
1529 "has no effect on this interface.\n",
1530 bond_dev->name, slave_dev->name);
1534 /* check for initial state */
1535 if (!bond->params.miimon ||
1536 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1537 if (bond->params.updelay) {
1538 dprintk("Initial state of slave_dev is "
1539 "BOND_LINK_BACK\n");
1540 new_slave->link = BOND_LINK_BACK;
1541 new_slave->delay = bond->params.updelay;
1543 dprintk("Initial state of slave_dev is "
1545 new_slave->link = BOND_LINK_UP;
1547 new_slave->jiffies = jiffies;
1549 dprintk("Initial state of slave_dev is "
1550 "BOND_LINK_DOWN\n");
1551 new_slave->link = BOND_LINK_DOWN;
1554 if (bond_update_speed_duplex(new_slave) &&
1555 (new_slave->link != BOND_LINK_DOWN)) {
1556 printk(KERN_WARNING DRV_NAME
1557 ": %s: Warning: failed to get speed and duplex from %s, "
1558 "assumed to be 100Mb/sec and Full.\n",
1559 bond_dev->name, new_slave->dev->name);
1561 if (bond->params.mode == BOND_MODE_8023AD) {
1562 printk(KERN_WARNING DRV_NAME
1563 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1564 "support in base driver for proper aggregator "
1565 "selection.\n", bond_dev->name);
1569 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1570 /* if there is a primary slave, remember it */
1571 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1572 bond->primary_slave = new_slave;
1576 switch (bond->params.mode) {
1577 case BOND_MODE_ACTIVEBACKUP:
1578 bond_set_slave_inactive_flags(new_slave);
1579 bond_select_active_slave(bond);
1581 case BOND_MODE_8023AD:
1582 /* in 802.3ad mode, the internal mechanism
1583 * will activate the slaves in the selected
1586 bond_set_slave_inactive_flags(new_slave);
1587 /* if this is the first slave */
1588 if (bond->slave_cnt == 1) {
1589 SLAVE_AD_INFO(new_slave).id = 1;
1590 /* Initialize AD with the number of times that the AD timer is called in 1 second
1591 * can be called only after the mac address of the bond is set
1593 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1594 bond->params.lacp_fast);
1596 SLAVE_AD_INFO(new_slave).id =
1597 SLAVE_AD_INFO(new_slave->prev).id + 1;
1600 bond_3ad_bind_slave(new_slave);
1604 new_slave->state = BOND_STATE_ACTIVE;
1605 bond_set_slave_inactive_flags(new_slave);
1608 dprintk("This slave is always active in trunk mode\n");
1610 /* always active in trunk mode */
1611 new_slave->state = BOND_STATE_ACTIVE;
1613 /* In trunking mode there is little meaning to curr_active_slave
1614 * anyway (it holds no special properties of the bond device),
1615 * so we can change it without calling change_active_interface()
1617 if (!bond->curr_active_slave) {
1618 bond->curr_active_slave = new_slave;
1621 } /* switch(bond_mode) */
1623 bond_set_carrier(bond);
1625 write_unlock_bh(&bond->lock);
1627 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1631 printk(KERN_INFO DRV_NAME
1632 ": %s: enslaving %s as a%s interface with a%s link.\n",
1633 bond_dev->name, slave_dev->name,
1634 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1635 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1637 /* enslave is successful */
1640 /* Undo stages on error */
1642 dev_close(slave_dev);
1645 netdev_set_master(slave_dev, NULL);
1648 if (!bond->params.fail_over_mac) {
1649 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1650 addr.sa_family = slave_dev->type;
1651 dev_set_mac_address(slave_dev, &addr);
1658 bond_dev->features = old_features;
1664 * Try to release the slave device <slave> from the bond device <master>
1665 * It is legal to access curr_active_slave without a lock because all the function
1668 * The rules for slave state should be:
1669 * for Active/Backup:
1670 * Active stays on all backups go down
1671 * for Bonded connections:
1672 * The first up interface should be left on and all others downed.
1674 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1676 struct bonding *bond = bond_dev->priv;
1677 struct slave *slave, *oldcurrent;
1678 struct sockaddr addr;
1679 int mac_addr_differ;
1680 DECLARE_MAC_BUF(mac);
1682 /* slave is not a slave or master is not master of this slave */
1683 if (!(slave_dev->flags & IFF_SLAVE) ||
1684 (slave_dev->master != bond_dev)) {
1685 printk(KERN_ERR DRV_NAME
1686 ": %s: Error: cannot release %s.\n",
1687 bond_dev->name, slave_dev->name);
1691 write_lock_bh(&bond->lock);
1693 slave = bond_get_slave_by_dev(bond, slave_dev);
1695 /* not a slave of this bond */
1696 printk(KERN_INFO DRV_NAME
1697 ": %s: %s not enslaved\n",
1698 bond_dev->name, slave_dev->name);
1699 write_unlock_bh(&bond->lock);
1703 mac_addr_differ = memcmp(bond_dev->dev_addr,
1706 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1707 printk(KERN_WARNING DRV_NAME
1708 ": %s: Warning: the permanent HWaddr of %s - "
1709 "%s - is still in use by %s. "
1710 "Set the HWaddr of %s to a different address "
1711 "to avoid conflicts.\n",
1714 print_mac(mac, slave->perm_hwaddr),
1719 /* Inform AD package of unbinding of slave. */
1720 if (bond->params.mode == BOND_MODE_8023AD) {
1721 /* must be called before the slave is
1722 * detached from the list
1724 bond_3ad_unbind_slave(slave);
1727 printk(KERN_INFO DRV_NAME
1728 ": %s: releasing %s interface %s\n",
1730 (slave->state == BOND_STATE_ACTIVE)
1731 ? "active" : "backup",
1734 oldcurrent = bond->curr_active_slave;
1736 bond->current_arp_slave = NULL;
1738 /* release the slave from its bond */
1739 bond_detach_slave(bond, slave);
1741 bond_compute_features(bond);
1743 if (bond->primary_slave == slave) {
1744 bond->primary_slave = NULL;
1747 if (oldcurrent == slave) {
1748 bond_change_active_slave(bond, NULL);
1751 if ((bond->params.mode == BOND_MODE_TLB) ||
1752 (bond->params.mode == BOND_MODE_ALB)) {
1753 /* Must be called only after the slave has been
1754 * detached from the list and the curr_active_slave
1755 * has been cleared (if our_slave == old_current),
1756 * but before a new active slave is selected.
1758 write_unlock_bh(&bond->lock);
1759 bond_alb_deinit_slave(bond, slave);
1760 write_lock_bh(&bond->lock);
1763 if (oldcurrent == slave) {
1765 * Note that we hold RTNL over this sequence, so there
1766 * is no concern that another slave add/remove event
1769 write_unlock_bh(&bond->lock);
1770 read_lock(&bond->lock);
1771 write_lock_bh(&bond->curr_slave_lock);
1773 bond_select_active_slave(bond);
1775 write_unlock_bh(&bond->curr_slave_lock);
1776 read_unlock(&bond->lock);
1777 write_lock_bh(&bond->lock);
1780 if (bond->slave_cnt == 0) {
1781 bond_set_carrier(bond);
1783 /* if the last slave was removed, zero the mac address
1784 * of the master so it will be set by the application
1785 * to the mac address of the first slave
1787 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1789 if (list_empty(&bond->vlan_list)) {
1790 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1792 printk(KERN_WARNING DRV_NAME
1793 ": %s: Warning: clearing HW address of %s while it "
1794 "still has VLANs.\n",
1795 bond_dev->name, bond_dev->name);
1796 printk(KERN_WARNING DRV_NAME
1797 ": %s: When re-adding slaves, make sure the bond's "
1798 "HW address matches its VLANs'.\n",
1801 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1802 !bond_has_challenged_slaves(bond)) {
1803 printk(KERN_INFO DRV_NAME
1804 ": %s: last VLAN challenged slave %s "
1805 "left bond %s. VLAN blocking is removed\n",
1806 bond_dev->name, slave_dev->name, bond_dev->name);
1807 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1810 write_unlock_bh(&bond->lock);
1812 /* must do this from outside any spinlocks */
1813 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1815 bond_del_vlans_from_slave(bond, slave_dev);
1817 /* If the mode USES_PRIMARY, then we should only remove its
1818 * promisc and mc settings if it was the curr_active_slave, but that was
1819 * already taken care of above when we detached the slave
1821 if (!USES_PRIMARY(bond->params.mode)) {
1822 /* unset promiscuity level from slave */
1823 if (bond_dev->flags & IFF_PROMISC) {
1824 dev_set_promiscuity(slave_dev, -1);
1827 /* unset allmulti level from slave */
1828 if (bond_dev->flags & IFF_ALLMULTI) {
1829 dev_set_allmulti(slave_dev, -1);
1832 /* flush master's mc_list from slave */
1833 netif_tx_lock_bh(bond_dev);
1834 bond_mc_list_flush(bond_dev, slave_dev);
1835 netif_tx_unlock_bh(bond_dev);
1838 netdev_set_master(slave_dev, NULL);
1840 /* close slave before restoring its mac address */
1841 dev_close(slave_dev);
1843 if (!bond->params.fail_over_mac) {
1844 /* restore original ("permanent") mac address */
1845 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1846 addr.sa_family = slave_dev->type;
1847 dev_set_mac_address(slave_dev, &addr);
1850 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1851 IFF_SLAVE_INACTIVE | IFF_BONDING |
1856 return 0; /* deletion OK */
1860 * Destroy a bonding device.
1861 * Must be under rtnl_lock when this function is called.
1863 void bond_destroy(struct bonding *bond)
1865 bond_deinit(bond->dev);
1866 bond_destroy_sysfs_entry(bond);
1867 unregister_netdevice(bond->dev);
1871 * First release a slave and than destroy the bond if no more slaves iare left.
1872 * Must be under rtnl_lock when this function is called.
1874 int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1876 struct bonding *bond = bond_dev->priv;
1879 ret = bond_release(bond_dev, slave_dev);
1880 if ((ret == 0) && (bond->slave_cnt == 0)) {
1881 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1882 bond_dev->name, bond_dev->name);
1889 * This function releases all slaves.
1891 static int bond_release_all(struct net_device *bond_dev)
1893 struct bonding *bond = bond_dev->priv;
1894 struct slave *slave;
1895 struct net_device *slave_dev;
1896 struct sockaddr addr;
1898 write_lock_bh(&bond->lock);
1900 netif_carrier_off(bond_dev);
1902 if (bond->slave_cnt == 0) {
1906 bond->current_arp_slave = NULL;
1907 bond->primary_slave = NULL;
1908 bond_change_active_slave(bond, NULL);
1910 while ((slave = bond->first_slave) != NULL) {
1911 /* Inform AD package of unbinding of slave
1912 * before slave is detached from the list.
1914 if (bond->params.mode == BOND_MODE_8023AD) {
1915 bond_3ad_unbind_slave(slave);
1918 slave_dev = slave->dev;
1919 bond_detach_slave(bond, slave);
1921 /* now that the slave is detached, unlock and perform
1922 * all the undo steps that should not be called from
1925 write_unlock_bh(&bond->lock);
1927 if ((bond->params.mode == BOND_MODE_TLB) ||
1928 (bond->params.mode == BOND_MODE_ALB)) {
1929 /* must be called only after the slave
1930 * has been detached from the list
1932 bond_alb_deinit_slave(bond, slave);
1935 bond_compute_features(bond);
1937 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1938 bond_del_vlans_from_slave(bond, slave_dev);
1940 /* If the mode USES_PRIMARY, then we should only remove its
1941 * promisc and mc settings if it was the curr_active_slave, but that was
1942 * already taken care of above when we detached the slave
1944 if (!USES_PRIMARY(bond->params.mode)) {
1945 /* unset promiscuity level from slave */
1946 if (bond_dev->flags & IFF_PROMISC) {
1947 dev_set_promiscuity(slave_dev, -1);
1950 /* unset allmulti level from slave */
1951 if (bond_dev->flags & IFF_ALLMULTI) {
1952 dev_set_allmulti(slave_dev, -1);
1955 /* flush master's mc_list from slave */
1956 netif_tx_lock_bh(bond_dev);
1957 bond_mc_list_flush(bond_dev, slave_dev);
1958 netif_tx_unlock_bh(bond_dev);
1961 netdev_set_master(slave_dev, NULL);
1963 /* close slave before restoring its mac address */
1964 dev_close(slave_dev);
1966 if (!bond->params.fail_over_mac) {
1967 /* restore original ("permanent") mac address*/
1968 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1969 addr.sa_family = slave_dev->type;
1970 dev_set_mac_address(slave_dev, &addr);
1973 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1974 IFF_SLAVE_INACTIVE);
1978 /* re-acquire the lock before getting the next slave */
1979 write_lock_bh(&bond->lock);
1982 /* zero the mac address of the master so it will be
1983 * set by the application to the mac address of the
1986 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1988 if (list_empty(&bond->vlan_list)) {
1989 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1991 printk(KERN_WARNING DRV_NAME
1992 ": %s: Warning: clearing HW address of %s while it "
1993 "still has VLANs.\n",
1994 bond_dev->name, bond_dev->name);
1995 printk(KERN_WARNING DRV_NAME
1996 ": %s: When re-adding slaves, make sure the bond's "
1997 "HW address matches its VLANs'.\n",
2001 printk(KERN_INFO DRV_NAME
2002 ": %s: released all slaves\n",
2006 write_unlock_bh(&bond->lock);
2012 * This function changes the active slave to slave <slave_dev>.
2013 * It returns -EINVAL in the following cases.
2014 * - <slave_dev> is not found in the list.
2015 * - There is not active slave now.
2016 * - <slave_dev> is already active.
2017 * - The link state of <slave_dev> is not BOND_LINK_UP.
2018 * - <slave_dev> is not running.
2019 * In these cases, this fuction does nothing.
2020 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2022 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2024 struct bonding *bond = bond_dev->priv;
2025 struct slave *old_active = NULL;
2026 struct slave *new_active = NULL;
2029 if (!USES_PRIMARY(bond->params.mode)) {
2033 /* Verify that master_dev is indeed the master of slave_dev */
2034 if (!(slave_dev->flags & IFF_SLAVE) ||
2035 (slave_dev->master != bond_dev)) {
2039 read_lock(&bond->lock);
2041 read_lock(&bond->curr_slave_lock);
2042 old_active = bond->curr_active_slave;
2043 read_unlock(&bond->curr_slave_lock);
2045 new_active = bond_get_slave_by_dev(bond, slave_dev);
2048 * Changing to the current active: do nothing; return success.
2050 if (new_active && (new_active == old_active)) {
2051 read_unlock(&bond->lock);
2057 (new_active->link == BOND_LINK_UP) &&
2058 IS_UP(new_active->dev)) {
2059 write_lock_bh(&bond->curr_slave_lock);
2060 bond_change_active_slave(bond, new_active);
2061 write_unlock_bh(&bond->curr_slave_lock);
2066 read_unlock(&bond->lock);
2071 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2073 struct bonding *bond = bond_dev->priv;
2075 info->bond_mode = bond->params.mode;
2076 info->miimon = bond->params.miimon;
2078 read_lock(&bond->lock);
2079 info->num_slaves = bond->slave_cnt;
2080 read_unlock(&bond->lock);
2085 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2087 struct bonding *bond = bond_dev->priv;
2088 struct slave *slave;
2091 if (info->slave_id < 0) {
2095 read_lock(&bond->lock);
2097 bond_for_each_slave(bond, slave, i) {
2098 if (i == (int)info->slave_id) {
2104 read_unlock(&bond->lock);
2107 strcpy(info->slave_name, slave->dev->name);
2108 info->link = slave->link;
2109 info->state = slave->state;
2110 info->link_failure_count = slave->link_failure_count;
2118 /*-------------------------------- Monitoring -------------------------------*/
2121 * if !have_locks, return nonzero if a failover is necessary. if
2122 * have_locks, do whatever failover activities are needed.
2124 * This is to separate the inspection and failover steps for locking
2125 * purposes; failover requires rtnl, but acquiring it for every
2126 * inspection is undesirable, so a wrapper first does inspection, and
2127 * the acquires the necessary locks and calls again to perform
2128 * failover if needed. Since all locks are dropped, a complete
2129 * restart is needed between calls.
2131 static int __bond_mii_monitor(struct bonding *bond, int have_locks)
2133 struct slave *slave, *oldcurrent;
2134 int do_failover = 0;
2137 if (bond->slave_cnt == 0)
2140 /* we will try to read the link status of each of our slaves, and
2141 * set their IFF_RUNNING flag appropriately. For each slave not
2142 * supporting MII status, we won't do anything so that a user-space
2143 * program could monitor the link itself if needed.
2146 if (bond->send_grat_arp) {
2147 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2148 &bond->curr_active_slave->dev->state))
2149 dprintk("Needs to send gratuitous arp but not yet\n");
2151 dprintk("sending delayed gratuitous arp on on %s\n",
2152 bond->curr_active_slave->dev->name);
2153 bond_send_gratuitous_arp(bond);
2154 bond->send_grat_arp--;
2157 read_lock(&bond->curr_slave_lock);
2158 oldcurrent = bond->curr_active_slave;
2159 read_unlock(&bond->curr_slave_lock);
2161 bond_for_each_slave(bond, slave, i) {
2162 struct net_device *slave_dev = slave->dev;
2164 u16 old_speed = slave->speed;
2165 u8 old_duplex = slave->duplex;
2167 link_state = bond_check_dev_link(bond, slave_dev, 0);
2169 switch (slave->link) {
2170 case BOND_LINK_UP: /* the link was up */
2171 if (link_state == BMSR_LSTATUS) {
2178 } else { /* link going down */
2179 slave->link = BOND_LINK_FAIL;
2180 slave->delay = bond->params.downdelay;
2182 if (slave->link_failure_count < UINT_MAX) {
2183 slave->link_failure_count++;
2186 if (bond->params.downdelay) {
2187 printk(KERN_INFO DRV_NAME
2188 ": %s: link status down for %s "
2189 "interface %s, disabling it in "
2193 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2194 ? ((slave == oldcurrent)
2195 ? "active " : "backup ")
2199 bond->params.downdelay * bond->params.miimon);
2202 /* no break ! fall through the BOND_LINK_FAIL test to
2203 ensure proper action to be taken
2205 case BOND_LINK_FAIL: /* the link has just gone down */
2206 if (link_state != BMSR_LSTATUS) {
2207 /* link stays down */
2208 if (slave->delay <= 0) {
2212 /* link down for too long time */
2213 slave->link = BOND_LINK_DOWN;
2215 /* in active/backup mode, we must
2216 * completely disable this interface
2218 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2219 (bond->params.mode == BOND_MODE_8023AD)) {
2220 bond_set_slave_inactive_flags(slave);
2223 printk(KERN_INFO DRV_NAME
2224 ": %s: link status definitely "
2225 "down for interface %s, "
2230 /* notify ad that the link status has changed */
2231 if (bond->params.mode == BOND_MODE_8023AD) {
2232 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2235 if ((bond->params.mode == BOND_MODE_TLB) ||
2236 (bond->params.mode == BOND_MODE_ALB)) {
2237 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2240 if (slave == oldcurrent) {
2248 slave->link = BOND_LINK_UP;
2249 slave->jiffies = jiffies;
2250 printk(KERN_INFO DRV_NAME
2251 ": %s: link status up again after %d "
2252 "ms for interface %s.\n",
2254 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2258 case BOND_LINK_DOWN: /* the link was down */
2259 if (link_state != BMSR_LSTATUS) {
2260 /* the link stays down, nothing more to do */
2262 } else { /* link going up */
2263 slave->link = BOND_LINK_BACK;
2264 slave->delay = bond->params.updelay;
2266 if (bond->params.updelay) {
2267 /* if updelay == 0, no need to
2268 advertise about a 0 ms delay */
2269 printk(KERN_INFO DRV_NAME
2270 ": %s: link status up for "
2271 "interface %s, enabling it "
2275 bond->params.updelay * bond->params.miimon);
2278 /* no break ! fall through the BOND_LINK_BACK state in
2279 case there's something to do.
2281 case BOND_LINK_BACK: /* the link has just come back */
2282 if (link_state != BMSR_LSTATUS) {
2283 /* link down again */
2284 slave->link = BOND_LINK_DOWN;
2286 printk(KERN_INFO DRV_NAME
2287 ": %s: link status down again after %d "
2288 "ms for interface %s.\n",
2290 (bond->params.updelay - slave->delay) * bond->params.miimon,
2294 if (slave->delay == 0) {
2298 /* now the link has been up for long time enough */
2299 slave->link = BOND_LINK_UP;
2300 slave->jiffies = jiffies;
2302 if (bond->params.mode == BOND_MODE_8023AD) {
2303 /* prevent it from being the active one */
2304 slave->state = BOND_STATE_BACKUP;
2305 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2306 /* make it immediately active */
2307 slave->state = BOND_STATE_ACTIVE;
2308 } else if (slave != bond->primary_slave) {
2309 /* prevent it from being the active one */
2310 slave->state = BOND_STATE_BACKUP;
2313 printk(KERN_INFO DRV_NAME
2314 ": %s: link status definitely "
2315 "up for interface %s.\n",
2319 /* notify ad that the link status has changed */
2320 if (bond->params.mode == BOND_MODE_8023AD) {
2321 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2324 if ((bond->params.mode == BOND_MODE_TLB) ||
2325 (bond->params.mode == BOND_MODE_ALB)) {
2326 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2329 if ((!oldcurrent) ||
2330 (slave == bond->primary_slave)) {
2339 /* Should not happen */
2340 printk(KERN_ERR DRV_NAME
2341 ": %s: Error: %s Illegal value (link=%d)\n",
2346 } /* end of switch (slave->link) */
2348 bond_update_speed_duplex(slave);
2350 if (bond->params.mode == BOND_MODE_8023AD) {
2351 if (old_speed != slave->speed) {
2352 bond_3ad_adapter_speed_changed(slave);
2355 if (old_duplex != slave->duplex) {
2356 bond_3ad_adapter_duplex_changed(slave);
2365 write_lock_bh(&bond->curr_slave_lock);
2367 bond_select_active_slave(bond);
2369 write_unlock_bh(&bond->curr_slave_lock);
2372 bond_set_carrier(bond);
2381 * Really a wrapper that splits the mii monitor into two phases: an
2382 * inspection, then (if inspection indicates something needs to be
2383 * done) an acquisition of appropriate locks followed by another pass
2384 * to implement whatever link state changes are indicated.
2386 void bond_mii_monitor(struct work_struct *work)
2388 struct bonding *bond = container_of(work, struct bonding,
2390 unsigned long delay;
2392 read_lock(&bond->lock);
2393 if (bond->kill_timers) {
2394 read_unlock(&bond->lock);
2397 if (__bond_mii_monitor(bond, 0)) {
2398 read_unlock(&bond->lock);
2400 read_lock(&bond->lock);
2401 __bond_mii_monitor(bond, 1);
2402 read_unlock(&bond->lock);
2403 rtnl_unlock(); /* might sleep, hold no other locks */
2404 read_lock(&bond->lock);
2407 delay = msecs_to_jiffies(bond->params.miimon);
2408 read_unlock(&bond->lock);
2409 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2412 static __be32 bond_glean_dev_ip(struct net_device *dev)
2414 struct in_device *idev;
2415 struct in_ifaddr *ifa;
2422 idev = __in_dev_get_rcu(dev);
2426 ifa = idev->ifa_list;
2430 addr = ifa->ifa_local;
2436 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2438 struct vlan_entry *vlan;
2440 if (ip == bond->master_ip)
2443 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2444 if (ip == vlan->vlan_ip)
2452 * We go to the (large) trouble of VLAN tagging ARP frames because
2453 * switches in VLAN mode (especially if ports are configured as
2454 * "native" to a VLAN) might not pass non-tagged frames.
2456 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2458 struct sk_buff *skb;
2460 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2461 slave_dev->name, dest_ip, src_ip, vlan_id);
2463 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2464 NULL, slave_dev->dev_addr, NULL);
2467 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2471 skb = vlan_put_tag(skb, vlan_id);
2473 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2481 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2484 __be32 *targets = bond->params.arp_targets;
2485 struct vlan_entry *vlan;
2486 struct net_device *vlan_dev;
2490 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2493 dprintk("basa: target %x\n", targets[i]);
2494 if (list_empty(&bond->vlan_list)) {
2495 dprintk("basa: empty vlan: arp_send\n");
2496 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2497 bond->master_ip, 0);
2502 * If VLANs are configured, we do a route lookup to
2503 * determine which VLAN interface would be used, so we
2504 * can tag the ARP with the proper VLAN tag.
2506 memset(&fl, 0, sizeof(fl));
2507 fl.fl4_dst = targets[i];
2508 fl.fl4_tos = RTO_ONLINK;
2510 rv = ip_route_output_key(&init_net, &rt, &fl);
2512 if (net_ratelimit()) {
2513 printk(KERN_WARNING DRV_NAME
2514 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2515 bond->dev->name, NIPQUAD(fl.fl4_dst));
2521 * This target is not on a VLAN
2523 if (rt->u.dst.dev == bond->dev) {
2525 dprintk("basa: rtdev == bond->dev: arp_send\n");
2526 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2527 bond->master_ip, 0);
2532 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2533 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2534 if (vlan_dev == rt->u.dst.dev) {
2535 vlan_id = vlan->vlan_id;
2536 dprintk("basa: vlan match on %s %d\n",
2537 vlan_dev->name, vlan_id);
2544 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2545 vlan->vlan_ip, vlan_id);
2549 if (net_ratelimit()) {
2550 printk(KERN_WARNING DRV_NAME
2551 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2552 bond->dev->name, NIPQUAD(fl.fl4_dst),
2553 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2560 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2561 * for each VLAN above us.
2563 static void bond_send_gratuitous_arp(struct bonding *bond)
2565 struct slave *slave = bond->curr_active_slave;
2566 struct vlan_entry *vlan;
2567 struct net_device *vlan_dev;
2569 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2570 slave ? slave->dev->name : "NULL");
2574 if (bond->master_ip) {
2575 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2576 bond->master_ip, 0);
2579 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2580 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2581 if (vlan->vlan_ip) {
2582 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2583 vlan->vlan_ip, vlan->vlan_id);
2588 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2591 __be32 *targets = bond->params.arp_targets;
2593 targets = bond->params.arp_targets;
2594 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2595 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2596 "%u.%u.%u.%u bhti(tip) %d\n",
2597 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2598 bond_has_this_ip(bond, tip));
2599 if (sip == targets[i]) {
2600 if (bond_has_this_ip(bond, tip))
2601 slave->last_arp_rx = jiffies;
2607 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2610 struct slave *slave;
2611 struct bonding *bond;
2612 unsigned char *arp_ptr;
2615 if (dev_net(dev) != &init_net)
2618 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2622 read_lock(&bond->lock);
2624 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2625 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2626 orig_dev ? orig_dev->name : "NULL");
2628 slave = bond_get_slave_by_dev(bond, orig_dev);
2629 if (!slave || !slave_do_arp_validate(bond, slave))
2632 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2636 if (arp->ar_hln != dev->addr_len ||
2637 skb->pkt_type == PACKET_OTHERHOST ||
2638 skb->pkt_type == PACKET_LOOPBACK ||
2639 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2640 arp->ar_pro != htons(ETH_P_IP) ||
2644 arp_ptr = (unsigned char *)(arp + 1);
2645 arp_ptr += dev->addr_len;
2646 memcpy(&sip, arp_ptr, 4);
2647 arp_ptr += 4 + dev->addr_len;
2648 memcpy(&tip, arp_ptr, 4);
2650 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2651 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2652 slave->state, bond->params.arp_validate,
2653 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2656 * Backup slaves won't see the ARP reply, but do come through
2657 * here for each ARP probe (so we swap the sip/tip to validate
2658 * the probe). In a "redundant switch, common router" type of
2659 * configuration, the ARP probe will (hopefully) travel from
2660 * the active, through one switch, the router, then the other
2661 * switch before reaching the backup.
2663 if (slave->state == BOND_STATE_ACTIVE)
2664 bond_validate_arp(bond, slave, sip, tip);
2666 bond_validate_arp(bond, slave, tip, sip);
2669 read_unlock(&bond->lock);
2672 return NET_RX_SUCCESS;
2676 * this function is called regularly to monitor each slave's link
2677 * ensuring that traffic is being sent and received when arp monitoring
2678 * is used in load-balancing mode. if the adapter has been dormant, then an
2679 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2680 * arp monitoring in active backup mode.
2682 void bond_loadbalance_arp_mon(struct work_struct *work)
2684 struct bonding *bond = container_of(work, struct bonding,
2686 struct slave *slave, *oldcurrent;
2687 int do_failover = 0;
2691 read_lock(&bond->lock);
2693 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2695 if (bond->kill_timers) {
2699 if (bond->slave_cnt == 0) {
2703 read_lock(&bond->curr_slave_lock);
2704 oldcurrent = bond->curr_active_slave;
2705 read_unlock(&bond->curr_slave_lock);
2707 /* see if any of the previous devices are up now (i.e. they have
2708 * xmt and rcv traffic). the curr_active_slave does not come into
2709 * the picture unless it is null. also, slave->jiffies is not needed
2710 * here because we send an arp on each slave and give a slave as
2711 * long as it needs to get the tx/rx within the delta.
2712 * TODO: what about up/down delay in arp mode? it wasn't here before
2715 bond_for_each_slave(bond, slave, i) {
2716 if (slave->link != BOND_LINK_UP) {
2717 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2718 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
2720 slave->link = BOND_LINK_UP;
2721 slave->state = BOND_STATE_ACTIVE;
2723 /* primary_slave has no meaning in round-robin
2724 * mode. the window of a slave being up and
2725 * curr_active_slave being null after enslaving
2729 printk(KERN_INFO DRV_NAME
2730 ": %s: link status definitely "
2731 "up for interface %s, ",
2736 printk(KERN_INFO DRV_NAME
2737 ": %s: interface %s is now up\n",
2743 /* slave->link == BOND_LINK_UP */
2745 /* not all switches will respond to an arp request
2746 * when the source ip is 0, so don't take the link down
2747 * if we don't know our ip yet
2749 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2750 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
2752 slave->link = BOND_LINK_DOWN;
2753 slave->state = BOND_STATE_BACKUP;
2755 if (slave->link_failure_count < UINT_MAX) {
2756 slave->link_failure_count++;
2759 printk(KERN_INFO DRV_NAME
2760 ": %s: interface %s is now down.\n",
2764 if (slave == oldcurrent) {
2770 /* note: if switch is in round-robin mode, all links
2771 * must tx arp to ensure all links rx an arp - otherwise
2772 * links may oscillate or not come up at all; if switch is
2773 * in something like xor mode, there is nothing we can
2774 * do - all replies will be rx'ed on same link causing slaves
2775 * to be unstable during low/no traffic periods
2777 if (IS_UP(slave->dev)) {
2778 bond_arp_send_all(bond, slave);
2783 write_lock_bh(&bond->curr_slave_lock);
2785 bond_select_active_slave(bond);
2787 write_unlock_bh(&bond->curr_slave_lock);
2791 if (bond->params.arp_interval)
2792 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2794 read_unlock(&bond->lock);
2798 * When using arp monitoring in active-backup mode, this function is
2799 * called to determine if any backup slaves have went down or a new
2800 * current slave needs to be found.
2801 * The backup slaves never generate traffic, they are considered up by merely
2802 * receiving traffic. If the current slave goes down, each backup slave will
2803 * be given the opportunity to tx/rx an arp before being taken down - this
2804 * prevents all slaves from being taken down due to the current slave not
2805 * sending any traffic for the backups to receive. The arps are not necessarily
2806 * necessary, any tx and rx traffic will keep the current slave up. While any
2807 * rx traffic will keep the backup slaves up, the current slave is responsible
2808 * for generating traffic to keep them up regardless of any other traffic they
2809 * may have received.
2810 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2812 void bond_activebackup_arp_mon(struct work_struct *work)
2814 struct bonding *bond = container_of(work, struct bonding,
2816 struct slave *slave;
2820 read_lock(&bond->lock);
2822 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2824 if (bond->kill_timers) {
2828 if (bond->slave_cnt == 0) {
2832 /* determine if any slave has come up or any backup slave has
2834 * TODO: what about up/down delay in arp mode? it wasn't here before
2837 bond_for_each_slave(bond, slave, i) {
2838 if (slave->link != BOND_LINK_UP) {
2839 if (time_before_eq(jiffies,
2840 slave_last_rx(bond, slave) + delta_in_ticks)) {
2842 slave->link = BOND_LINK_UP;
2844 write_lock_bh(&bond->curr_slave_lock);
2846 if ((!bond->curr_active_slave) &&
2847 time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks)) {
2848 bond_change_active_slave(bond, slave);
2849 bond->current_arp_slave = NULL;
2850 } else if (bond->curr_active_slave != slave) {
2851 /* this slave has just come up but we
2852 * already have a current slave; this
2853 * can also happen if bond_enslave adds
2854 * a new slave that is up while we are
2855 * searching for a new slave
2857 bond_set_slave_inactive_flags(slave);
2858 bond->current_arp_slave = NULL;
2861 bond_set_carrier(bond);
2863 if (slave == bond->curr_active_slave) {
2864 printk(KERN_INFO DRV_NAME
2865 ": %s: %s is up and now the "
2866 "active interface\n",
2869 netif_carrier_on(bond->dev);
2871 printk(KERN_INFO DRV_NAME
2872 ": %s: backup interface %s is "
2878 write_unlock_bh(&bond->curr_slave_lock);
2881 read_lock(&bond->curr_slave_lock);
2883 if ((slave != bond->curr_active_slave) &&
2884 (!bond->current_arp_slave) &&
2885 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 3*delta_in_ticks))) {
2886 /* a backup slave has gone down; three times
2887 * the delta allows the current slave to be
2888 * taken out before the backup slave.
2889 * note: a non-null current_arp_slave indicates
2890 * the curr_active_slave went down and we are
2891 * searching for a new one; under this
2892 * condition we only take the curr_active_slave
2893 * down - this gives each slave a chance to
2894 * tx/rx traffic before being taken out
2897 read_unlock(&bond->curr_slave_lock);
2899 slave->link = BOND_LINK_DOWN;
2901 if (slave->link_failure_count < UINT_MAX) {
2902 slave->link_failure_count++;
2905 bond_set_slave_inactive_flags(slave);
2907 printk(KERN_INFO DRV_NAME
2908 ": %s: backup interface %s is now down\n",
2912 read_unlock(&bond->curr_slave_lock);
2917 read_lock(&bond->curr_slave_lock);
2918 slave = bond->curr_active_slave;
2919 read_unlock(&bond->curr_slave_lock);
2922 /* if we have sent traffic in the past 2*arp_intervals but
2923 * haven't xmit and rx traffic in that time interval, select
2924 * a different slave. slave->jiffies is only updated when
2925 * a slave first becomes the curr_active_slave - not necessarily
2926 * after every arp; this ensures the slave has a full 2*delta
2927 * before being taken out. if a primary is being used, check
2928 * if it is up and needs to take over as the curr_active_slave
2930 if ((time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2931 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 2*delta_in_ticks))) &&
2932 time_after_eq(jiffies, slave->jiffies + 2*delta_in_ticks)) {
2934 slave->link = BOND_LINK_DOWN;
2936 if (slave->link_failure_count < UINT_MAX) {
2937 slave->link_failure_count++;
2940 printk(KERN_INFO DRV_NAME
2941 ": %s: link status down for active interface "
2942 "%s, disabling it\n",
2946 write_lock_bh(&bond->curr_slave_lock);
2948 bond_select_active_slave(bond);
2949 slave = bond->curr_active_slave;
2951 write_unlock_bh(&bond->curr_slave_lock);
2953 bond->current_arp_slave = slave;
2956 slave->jiffies = jiffies;
2958 } else if ((bond->primary_slave) &&
2959 (bond->primary_slave != slave) &&
2960 (bond->primary_slave->link == BOND_LINK_UP)) {
2961 /* at this point, slave is the curr_active_slave */
2962 printk(KERN_INFO DRV_NAME
2963 ": %s: changing from interface %s to primary "
2967 bond->primary_slave->dev->name);
2969 /* primary is up so switch to it */
2970 write_lock_bh(&bond->curr_slave_lock);
2971 bond_change_active_slave(bond, bond->primary_slave);
2972 write_unlock_bh(&bond->curr_slave_lock);
2974 slave = bond->primary_slave;
2975 slave->jiffies = jiffies;
2977 bond->current_arp_slave = NULL;
2980 /* the current slave must tx an arp to ensure backup slaves
2983 if (slave && IS_UP(slave->dev))
2984 bond_arp_send_all(bond, slave);
2987 /* if we don't have a curr_active_slave, search for the next available
2988 * backup slave from the current_arp_slave and make it the candidate
2989 * for becoming the curr_active_slave
2992 if (!bond->current_arp_slave) {
2993 bond->current_arp_slave = bond->first_slave;
2996 if (bond->current_arp_slave) {
2997 bond_set_slave_inactive_flags(bond->current_arp_slave);
2999 /* search for next candidate */
3000 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3001 if (IS_UP(slave->dev)) {
3002 slave->link = BOND_LINK_BACK;
3003 bond_set_slave_active_flags(slave);
3004 bond_arp_send_all(bond, slave);
3005 slave->jiffies = jiffies;
3006 bond->current_arp_slave = slave;
3010 /* if the link state is up at this point, we
3011 * mark it down - this can happen if we have
3012 * simultaneous link failures and
3013 * reselect_active_interface doesn't make this
3014 * one the current slave so it is still marked
3015 * up when it is actually down
3017 if (slave->link == BOND_LINK_UP) {
3018 slave->link = BOND_LINK_DOWN;
3019 if (slave->link_failure_count < UINT_MAX) {
3020 slave->link_failure_count++;
3023 bond_set_slave_inactive_flags(slave);
3025 printk(KERN_INFO DRV_NAME
3026 ": %s: backup interface %s is "
3036 if (bond->params.arp_interval) {
3037 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3040 read_unlock(&bond->lock);
3043 /*------------------------------ proc/seq_file-------------------------------*/
3045 #ifdef CONFIG_PROC_FS
3047 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3049 struct bonding *bond = seq->private;
3051 struct slave *slave;
3054 /* make sure the bond won't be taken away */
3055 read_lock(&dev_base_lock);
3056 read_lock(&bond->lock);
3059 return SEQ_START_TOKEN;
3062 bond_for_each_slave(bond, slave, i) {
3063 if (++off == *pos) {
3071 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3073 struct bonding *bond = seq->private;
3074 struct slave *slave = v;
3077 if (v == SEQ_START_TOKEN) {
3078 return bond->first_slave;
3081 slave = slave->next;
3083 return (slave == bond->first_slave) ? NULL : slave;
3086 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3088 struct bonding *bond = seq->private;
3090 read_unlock(&bond->lock);
3091 read_unlock(&dev_base_lock);
3094 static void bond_info_show_master(struct seq_file *seq)
3096 struct bonding *bond = seq->private;
3101 read_lock(&bond->curr_slave_lock);
3102 curr = bond->curr_active_slave;
3103 read_unlock(&bond->curr_slave_lock);
3105 seq_printf(seq, "Bonding Mode: %s",
3106 bond_mode_name(bond->params.mode));
3108 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3109 bond->params.fail_over_mac)
3110 seq_printf(seq, " (fail_over_mac)");
3112 seq_printf(seq, "\n");
3114 if (bond->params.mode == BOND_MODE_XOR ||
3115 bond->params.mode == BOND_MODE_8023AD) {
3116 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3117 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3118 bond->params.xmit_policy);
3121 if (USES_PRIMARY(bond->params.mode)) {
3122 seq_printf(seq, "Primary Slave: %s\n",
3123 (bond->primary_slave) ?
3124 bond->primary_slave->dev->name : "None");
3126 seq_printf(seq, "Currently Active Slave: %s\n",
3127 (curr) ? curr->dev->name : "None");
3130 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3132 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3133 seq_printf(seq, "Up Delay (ms): %d\n",
3134 bond->params.updelay * bond->params.miimon);
3135 seq_printf(seq, "Down Delay (ms): %d\n",
3136 bond->params.downdelay * bond->params.miimon);
3139 /* ARP information */
3140 if(bond->params.arp_interval > 0) {
3142 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3143 bond->params.arp_interval);
3145 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3147 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3148 if (!bond->params.arp_targets[i])
3151 seq_printf(seq, ",");
3152 target = ntohl(bond->params.arp_targets[i]);
3153 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3156 seq_printf(seq, "\n");
3159 if (bond->params.mode == BOND_MODE_8023AD) {
3160 struct ad_info ad_info;
3161 DECLARE_MAC_BUF(mac);
3163 seq_puts(seq, "\n802.3ad info\n");
3164 seq_printf(seq, "LACP rate: %s\n",
3165 (bond->params.lacp_fast) ? "fast" : "slow");
3167 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3168 seq_printf(seq, "bond %s has no active aggregator\n",
3171 seq_printf(seq, "Active Aggregator Info:\n");
3173 seq_printf(seq, "\tAggregator ID: %d\n",
3174 ad_info.aggregator_id);
3175 seq_printf(seq, "\tNumber of ports: %d\n",
3177 seq_printf(seq, "\tActor Key: %d\n",
3179 seq_printf(seq, "\tPartner Key: %d\n",
3180 ad_info.partner_key);
3181 seq_printf(seq, "\tPartner Mac Address: %s\n",
3182 print_mac(mac, ad_info.partner_system));
3187 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3189 struct bonding *bond = seq->private;
3190 DECLARE_MAC_BUF(mac);
3192 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3193 seq_printf(seq, "MII Status: %s\n",
3194 (slave->link == BOND_LINK_UP) ? "up" : "down");
3195 seq_printf(seq, "Link Failure Count: %u\n",
3196 slave->link_failure_count);
3199 "Permanent HW addr: %s\n",
3200 print_mac(mac, slave->perm_hwaddr));
3202 if (bond->params.mode == BOND_MODE_8023AD) {
3203 const struct aggregator *agg
3204 = SLAVE_AD_INFO(slave).port.aggregator;
3207 seq_printf(seq, "Aggregator ID: %d\n",
3208 agg->aggregator_identifier);
3210 seq_puts(seq, "Aggregator ID: N/A\n");
3215 static int bond_info_seq_show(struct seq_file *seq, void *v)
3217 if (v == SEQ_START_TOKEN) {
3218 seq_printf(seq, "%s\n", version);
3219 bond_info_show_master(seq);
3221 bond_info_show_slave(seq, v);
3227 static struct seq_operations bond_info_seq_ops = {
3228 .start = bond_info_seq_start,
3229 .next = bond_info_seq_next,
3230 .stop = bond_info_seq_stop,
3231 .show = bond_info_seq_show,
3234 static int bond_info_open(struct inode *inode, struct file *file)
3236 struct seq_file *seq;
3237 struct proc_dir_entry *proc;
3240 res = seq_open(file, &bond_info_seq_ops);
3242 /* recover the pointer buried in proc_dir_entry data */
3243 seq = file->private_data;
3245 seq->private = proc->data;
3251 static const struct file_operations bond_info_fops = {
3252 .owner = THIS_MODULE,
3253 .open = bond_info_open,
3255 .llseek = seq_lseek,
3256 .release = seq_release,
3259 static int bond_create_proc_entry(struct bonding *bond)
3261 struct net_device *bond_dev = bond->dev;
3263 if (bond_proc_dir) {
3264 bond->proc_entry = proc_create_data(bond_dev->name,
3265 S_IRUGO, bond_proc_dir,
3266 &bond_info_fops, bond);
3267 if (bond->proc_entry == NULL) {
3268 printk(KERN_WARNING DRV_NAME
3269 ": Warning: Cannot create /proc/net/%s/%s\n",
3270 DRV_NAME, bond_dev->name);
3272 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3279 static void bond_remove_proc_entry(struct bonding *bond)
3281 if (bond_proc_dir && bond->proc_entry) {
3282 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3283 memset(bond->proc_file_name, 0, IFNAMSIZ);
3284 bond->proc_entry = NULL;
3288 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3289 * Caller must hold rtnl_lock.
3291 static void bond_create_proc_dir(void)
3293 int len = strlen(DRV_NAME);
3295 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3296 bond_proc_dir = bond_proc_dir->next) {
3297 if ((bond_proc_dir->namelen == len) &&
3298 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3303 if (!bond_proc_dir) {
3304 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3305 if (bond_proc_dir) {
3306 bond_proc_dir->owner = THIS_MODULE;
3308 printk(KERN_WARNING DRV_NAME
3309 ": Warning: cannot create /proc/net/%s\n",
3315 /* Destroy the bonding directory under /proc/net, if empty.
3316 * Caller must hold rtnl_lock.
3318 static void bond_destroy_proc_dir(void)
3320 struct proc_dir_entry *de;
3322 if (!bond_proc_dir) {
3326 /* verify that the /proc dir is empty */
3327 for (de = bond_proc_dir->subdir; de; de = de->next) {
3328 /* ignore . and .. */
3329 if (*(de->name) != '.') {
3335 if (bond_proc_dir->owner == THIS_MODULE) {
3336 bond_proc_dir->owner = NULL;
3339 remove_proc_entry(DRV_NAME, init_net.proc_net);
3340 bond_proc_dir = NULL;
3343 #endif /* CONFIG_PROC_FS */
3345 /*-------------------------- netdev event handling --------------------------*/
3348 * Change device name
3350 static int bond_event_changename(struct bonding *bond)
3352 #ifdef CONFIG_PROC_FS
3353 bond_remove_proc_entry(bond);
3354 bond_create_proc_entry(bond);
3356 down_write(&(bonding_rwsem));
3357 bond_destroy_sysfs_entry(bond);
3358 bond_create_sysfs_entry(bond);
3359 up_write(&(bonding_rwsem));
3363 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3365 struct bonding *event_bond = bond_dev->priv;
3368 case NETDEV_CHANGENAME:
3369 return bond_event_changename(event_bond);
3370 case NETDEV_UNREGISTER:
3371 bond_release_all(event_bond->dev);
3380 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3382 struct net_device *bond_dev = slave_dev->master;
3383 struct bonding *bond = bond_dev->priv;
3386 case NETDEV_UNREGISTER:
3388 if (bond->setup_by_slave)
3389 bond_release_and_destroy(bond_dev, slave_dev);
3391 bond_release(bond_dev, slave_dev);
3396 * TODO: is this what we get if somebody
3397 * sets up a hierarchical bond, then rmmod's
3398 * one of the slave bonding devices?
3403 * ... Or is it this?
3406 case NETDEV_CHANGEMTU:
3408 * TODO: Should slaves be allowed to
3409 * independently alter their MTU? For
3410 * an active-backup bond, slaves need
3411 * not be the same type of device, so
3412 * MTUs may vary. For other modes,
3413 * slaves arguably should have the
3414 * same MTUs. To do this, we'd need to
3415 * take over the slave's change_mtu
3416 * function for the duration of their
3420 case NETDEV_CHANGENAME:
3422 * TODO: handle changing the primary's name
3425 case NETDEV_FEAT_CHANGE:
3426 bond_compute_features(bond);
3436 * bond_netdev_event: handle netdev notifier chain events.
3438 * This function receives events for the netdev chain. The caller (an
3439 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3440 * locks for us to safely manipulate the slave devices (RTNL lock,
3443 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3445 struct net_device *event_dev = (struct net_device *)ptr;
3447 if (dev_net(event_dev) != &init_net)
3450 dprintk("event_dev: %s, event: %lx\n",
3451 (event_dev ? event_dev->name : "None"),
3454 if (!(event_dev->priv_flags & IFF_BONDING))
3457 if (event_dev->flags & IFF_MASTER) {
3458 dprintk("IFF_MASTER\n");
3459 return bond_master_netdev_event(event, event_dev);
3462 if (event_dev->flags & IFF_SLAVE) {
3463 dprintk("IFF_SLAVE\n");
3464 return bond_slave_netdev_event(event, event_dev);
3471 * bond_inetaddr_event: handle inetaddr notifier chain events.
3473 * We keep track of device IPs primarily to use as source addresses in
3474 * ARP monitor probes (rather than spewing out broadcasts all the time).
3476 * We track one IP for the main device (if it has one), plus one per VLAN.
3478 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3480 struct in_ifaddr *ifa = ptr;
3481 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3482 struct bonding *bond;
3483 struct vlan_entry *vlan;
3485 if (dev_net(ifa->ifa_dev->dev) != &init_net)
3488 list_for_each_entry(bond, &bond_dev_list, bond_list) {
3489 if (bond->dev == event_dev) {
3492 bond->master_ip = ifa->ifa_local;
3495 bond->master_ip = bond_glean_dev_ip(bond->dev);
3502 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3503 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3504 if (vlan_dev == event_dev) {
3507 vlan->vlan_ip = ifa->ifa_local;
3511 bond_glean_dev_ip(vlan_dev);
3522 static struct notifier_block bond_netdev_notifier = {
3523 .notifier_call = bond_netdev_event,
3526 static struct notifier_block bond_inetaddr_notifier = {
3527 .notifier_call = bond_inetaddr_event,
3530 /*-------------------------- Packet type handling ---------------------------*/
3532 /* register to receive lacpdus on a bond */
3533 static void bond_register_lacpdu(struct bonding *bond)
3535 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3537 /* initialize packet type */
3538 pk_type->type = PKT_TYPE_LACPDU;
3539 pk_type->dev = bond->dev;
3540 pk_type->func = bond_3ad_lacpdu_recv;
3542 dev_add_pack(pk_type);
3545 /* unregister to receive lacpdus on a bond */
3546 static void bond_unregister_lacpdu(struct bonding *bond)
3548 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3551 void bond_register_arp(struct bonding *bond)
3553 struct packet_type *pt = &bond->arp_mon_pt;
3558 pt->type = htons(ETH_P_ARP);
3559 pt->dev = bond->dev;
3560 pt->func = bond_arp_rcv;
3564 void bond_unregister_arp(struct bonding *bond)
3566 struct packet_type *pt = &bond->arp_mon_pt;
3568 dev_remove_pack(pt);
3572 /*---------------------------- Hashing Policies -----------------------------*/
3575 * Hash for the output device based upon layer 2 and layer 3 data. If
3576 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3578 static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3579 struct net_device *bond_dev, int count)
3581 struct ethhdr *data = (struct ethhdr *)skb->data;
3582 struct iphdr *iph = ip_hdr(skb);
3584 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3585 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3586 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3589 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3593 * Hash for the output device based upon layer 3 and layer 4 data. If
3594 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3595 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3597 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3598 struct net_device *bond_dev, int count)
3600 struct ethhdr *data = (struct ethhdr *)skb->data;
3601 struct iphdr *iph = ip_hdr(skb);
3602 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3605 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3606 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3607 (iph->protocol == IPPROTO_TCP ||
3608 iph->protocol == IPPROTO_UDP)) {
3609 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3611 return (layer4_xor ^
3612 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3616 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3620 * Hash for the output device based upon layer 2 data
3622 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3623 struct net_device *bond_dev, int count)
3625 struct ethhdr *data = (struct ethhdr *)skb->data;
3627 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3630 /*-------------------------- Device entry points ----------------------------*/
3632 static int bond_open(struct net_device *bond_dev)
3634 struct bonding *bond = bond_dev->priv;
3636 bond->kill_timers = 0;
3638 if ((bond->params.mode == BOND_MODE_TLB) ||
3639 (bond->params.mode == BOND_MODE_ALB)) {
3640 /* bond_alb_initialize must be called before the timer
3643 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3644 /* something went wrong - fail the open operation */
3648 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3649 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3652 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3653 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3654 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3657 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3658 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3659 INIT_DELAYED_WORK(&bond->arp_work,
3660 bond_activebackup_arp_mon);
3662 INIT_DELAYED_WORK(&bond->arp_work,
3663 bond_loadbalance_arp_mon);
3665 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3666 if (bond->params.arp_validate)
3667 bond_register_arp(bond);
3670 if (bond->params.mode == BOND_MODE_8023AD) {
3671 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3672 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3673 /* register to receive LACPDUs */
3674 bond_register_lacpdu(bond);
3680 static int bond_close(struct net_device *bond_dev)
3682 struct bonding *bond = bond_dev->priv;
3684 if (bond->params.mode == BOND_MODE_8023AD) {
3685 /* Unregister the receive of LACPDUs */
3686 bond_unregister_lacpdu(bond);
3689 if (bond->params.arp_validate)
3690 bond_unregister_arp(bond);
3692 write_lock_bh(&bond->lock);
3695 /* signal timers not to re-arm */
3696 bond->kill_timers = 1;
3698 write_unlock_bh(&bond->lock);
3700 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3701 cancel_delayed_work(&bond->mii_work);
3704 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3705 cancel_delayed_work(&bond->arp_work);
3708 switch (bond->params.mode) {
3709 case BOND_MODE_8023AD:
3710 cancel_delayed_work(&bond->ad_work);
3714 cancel_delayed_work(&bond->alb_work);
3721 if ((bond->params.mode == BOND_MODE_TLB) ||
3722 (bond->params.mode == BOND_MODE_ALB)) {
3723 /* Must be called only after all
3724 * slaves have been released
3726 bond_alb_deinitialize(bond);
3732 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3734 struct bonding *bond = bond_dev->priv;
3735 struct net_device_stats *stats = &(bond->stats), *sstats;
3736 struct net_device_stats local_stats;
3737 struct slave *slave;
3740 memset(&local_stats, 0, sizeof(struct net_device_stats));
3742 read_lock_bh(&bond->lock);
3744 bond_for_each_slave(bond, slave, i) {
3745 sstats = slave->dev->get_stats(slave->dev);
3746 local_stats.rx_packets += sstats->rx_packets;
3747 local_stats.rx_bytes += sstats->rx_bytes;
3748 local_stats.rx_errors += sstats->rx_errors;
3749 local_stats.rx_dropped += sstats->rx_dropped;
3751 local_stats.tx_packets += sstats->tx_packets;
3752 local_stats.tx_bytes += sstats->tx_bytes;
3753 local_stats.tx_errors += sstats->tx_errors;
3754 local_stats.tx_dropped += sstats->tx_dropped;
3756 local_stats.multicast += sstats->multicast;
3757 local_stats.collisions += sstats->collisions;
3759 local_stats.rx_length_errors += sstats->rx_length_errors;
3760 local_stats.rx_over_errors += sstats->rx_over_errors;
3761 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3762 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3763 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3764 local_stats.rx_missed_errors += sstats->rx_missed_errors;
3766 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3767 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3768 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3769 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3770 local_stats.tx_window_errors += sstats->tx_window_errors;
3773 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3775 read_unlock_bh(&bond->lock);
3780 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3782 struct net_device *slave_dev = NULL;
3783 struct ifbond k_binfo;
3784 struct ifbond __user *u_binfo = NULL;
3785 struct ifslave k_sinfo;
3786 struct ifslave __user *u_sinfo = NULL;
3787 struct mii_ioctl_data *mii = NULL;
3790 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3791 bond_dev->name, cmd);
3803 * We do this again just in case we were called by SIOCGMIIREG
3804 * instead of SIOCGMIIPHY.
3811 if (mii->reg_num == 1) {
3812 struct bonding *bond = bond_dev->priv;
3814 read_lock(&bond->lock);
3815 read_lock(&bond->curr_slave_lock);
3816 if (netif_carrier_ok(bond->dev)) {
3817 mii->val_out = BMSR_LSTATUS;
3819 read_unlock(&bond->curr_slave_lock);
3820 read_unlock(&bond->lock);
3824 case BOND_INFO_QUERY_OLD:
3825 case SIOCBONDINFOQUERY:
3826 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3828 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3832 res = bond_info_query(bond_dev, &k_binfo);
3834 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3840 case BOND_SLAVE_INFO_QUERY_OLD:
3841 case SIOCBONDSLAVEINFOQUERY:
3842 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3844 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3848 res = bond_slave_info_query(bond_dev, &k_sinfo);
3850 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3861 if (!capable(CAP_NET_ADMIN)) {
3865 down_write(&(bonding_rwsem));
3866 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
3868 dprintk("slave_dev=%p: \n", slave_dev);
3873 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3875 case BOND_ENSLAVE_OLD:
3876 case SIOCBONDENSLAVE:
3877 res = bond_enslave(bond_dev, slave_dev);
3879 case BOND_RELEASE_OLD:
3880 case SIOCBONDRELEASE:
3881 res = bond_release(bond_dev, slave_dev);
3883 case BOND_SETHWADDR_OLD:
3884 case SIOCBONDSETHWADDR:
3885 res = bond_sethwaddr(bond_dev, slave_dev);
3887 case BOND_CHANGE_ACTIVE_OLD:
3888 case SIOCBONDCHANGEACTIVE:
3889 res = bond_ioctl_change_active(bond_dev, slave_dev);
3898 up_write(&(bonding_rwsem));
3902 static void bond_set_multicast_list(struct net_device *bond_dev)
3904 struct bonding *bond = bond_dev->priv;
3905 struct dev_mc_list *dmi;
3908 * Do promisc before checking multicast_mode
3910 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3911 bond_set_promiscuity(bond, 1);
3914 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3915 bond_set_promiscuity(bond, -1);
3918 /* set allmulti flag to slaves */
3919 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3920 bond_set_allmulti(bond, 1);
3923 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3924 bond_set_allmulti(bond, -1);
3927 read_lock(&bond->lock);
3929 bond->flags = bond_dev->flags;
3931 /* looking for addresses to add to slaves' mc list */
3932 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3933 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3934 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3938 /* looking for addresses to delete from slaves' list */
3939 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3940 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3941 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3945 /* save master's multicast list */
3946 bond_mc_list_destroy(bond);
3947 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3949 read_unlock(&bond->lock);
3953 * Change the MTU of all of a master's slaves to match the master
3955 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3957 struct bonding *bond = bond_dev->priv;
3958 struct slave *slave, *stop_at;
3962 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3963 (bond_dev ? bond_dev->name : "None"), new_mtu);
3965 /* Can't hold bond->lock with bh disabled here since
3966 * some base drivers panic. On the other hand we can't
3967 * hold bond->lock without bh disabled because we'll
3968 * deadlock. The only solution is to rely on the fact
3969 * that we're under rtnl_lock here, and the slaves
3970 * list won't change. This doesn't solve the problem
3971 * of setting the slave's MTU while it is
3972 * transmitting, but the assumption is that the base
3973 * driver can handle that.
3975 * TODO: figure out a way to safely iterate the slaves
3976 * list, but without holding a lock around the actual
3977 * call to the base driver.
3980 bond_for_each_slave(bond, slave, i) {
3981 dprintk("s %p s->p %p c_m %p\n", slave,
3982 slave->prev, slave->dev->change_mtu);
3984 res = dev_set_mtu(slave->dev, new_mtu);
3987 /* If we failed to set the slave's mtu to the new value
3988 * we must abort the operation even in ACTIVE_BACKUP
3989 * mode, because if we allow the backup slaves to have
3990 * different mtu values than the active slave we'll
3991 * need to change their mtu when doing a failover. That
3992 * means changing their mtu from timer context, which
3993 * is probably not a good idea.
3995 dprintk("err %d %s\n", res, slave->dev->name);
4000 bond_dev->mtu = new_mtu;
4005 /* unwind from head to the slave that failed */
4007 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4010 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4012 dprintk("unwind err %d dev %s\n", tmp_res,
4023 * Note that many devices must be down to change the HW address, and
4024 * downing the master releases all slaves. We can make bonds full of
4025 * bonding devices to test this, however.
4027 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4029 struct bonding *bond = bond_dev->priv;
4030 struct sockaddr *sa = addr, tmp_sa;
4031 struct slave *slave, *stop_at;
4035 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4038 * If fail_over_mac is enabled, do nothing and return success.
4039 * Returning an error causes ifenslave to fail.
4041 if (bond->params.fail_over_mac)
4044 if (!is_valid_ether_addr(sa->sa_data)) {
4045 return -EADDRNOTAVAIL;
4048 /* Can't hold bond->lock with bh disabled here since
4049 * some base drivers panic. On the other hand we can't
4050 * hold bond->lock without bh disabled because we'll
4051 * deadlock. The only solution is to rely on the fact
4052 * that we're under rtnl_lock here, and the slaves
4053 * list won't change. This doesn't solve the problem
4054 * of setting the slave's hw address while it is
4055 * transmitting, but the assumption is that the base
4056 * driver can handle that.
4058 * TODO: figure out a way to safely iterate the slaves
4059 * list, but without holding a lock around the actual
4060 * call to the base driver.
4063 bond_for_each_slave(bond, slave, i) {
4064 dprintk("slave %p %s\n", slave, slave->dev->name);
4066 if (slave->dev->set_mac_address == NULL) {
4068 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4072 res = dev_set_mac_address(slave->dev, addr);
4074 /* TODO: consider downing the slave
4076 * User should expect communications
4077 * breakage anyway until ARP finish
4080 dprintk("err %d %s\n", res, slave->dev->name);
4086 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4090 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4091 tmp_sa.sa_family = bond_dev->type;
4093 /* unwind from head to the slave that failed */
4095 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4098 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4100 dprintk("unwind err %d dev %s\n", tmp_res,
4108 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4110 struct bonding *bond = bond_dev->priv;
4111 struct slave *slave, *start_at;
4112 int i, slave_no, res = 1;
4114 read_lock(&bond->lock);
4116 if (!BOND_IS_OK(bond)) {
4121 * Concurrent TX may collide on rr_tx_counter; we accept that
4122 * as being rare enough not to justify using an atomic op here
4124 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4126 bond_for_each_slave(bond, slave, i) {
4134 bond_for_each_slave_from(bond, slave, i, start_at) {
4135 if (IS_UP(slave->dev) &&
4136 (slave->link == BOND_LINK_UP) &&
4137 (slave->state == BOND_STATE_ACTIVE)) {
4138 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4145 /* no suitable interface, frame not sent */
4148 read_unlock(&bond->lock);
4154 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4155 * the bond has a usable interface.
4157 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4159 struct bonding *bond = bond_dev->priv;
4162 read_lock(&bond->lock);
4163 read_lock(&bond->curr_slave_lock);
4165 if (!BOND_IS_OK(bond)) {
4169 if (!bond->curr_active_slave)
4172 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4176 /* no suitable interface, frame not sent */
4179 read_unlock(&bond->curr_slave_lock);
4180 read_unlock(&bond->lock);
4185 * In bond_xmit_xor() , we determine the output device by using a pre-
4186 * determined xmit_hash_policy(), If the selected device is not enabled,
4187 * find the next active slave.
4189 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4191 struct bonding *bond = bond_dev->priv;
4192 struct slave *slave, *start_at;
4197 read_lock(&bond->lock);
4199 if (!BOND_IS_OK(bond)) {
4203 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4205 bond_for_each_slave(bond, slave, i) {
4214 bond_for_each_slave_from(bond, slave, i, start_at) {
4215 if (IS_UP(slave->dev) &&
4216 (slave->link == BOND_LINK_UP) &&
4217 (slave->state == BOND_STATE_ACTIVE)) {
4218 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4225 /* no suitable interface, frame not sent */
4228 read_unlock(&bond->lock);
4233 * in broadcast mode, we send everything to all usable interfaces.
4235 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4237 struct bonding *bond = bond_dev->priv;
4238 struct slave *slave, *start_at;
4239 struct net_device *tx_dev = NULL;
4243 read_lock(&bond->lock);
4245 if (!BOND_IS_OK(bond)) {
4249 read_lock(&bond->curr_slave_lock);
4250 start_at = bond->curr_active_slave;
4251 read_unlock(&bond->curr_slave_lock);
4257 bond_for_each_slave_from(bond, slave, i, start_at) {
4258 if (IS_UP(slave->dev) &&
4259 (slave->link == BOND_LINK_UP) &&
4260 (slave->state == BOND_STATE_ACTIVE)) {
4262 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4264 printk(KERN_ERR DRV_NAME
4265 ": %s: Error: bond_xmit_broadcast(): "
4266 "skb_clone() failed\n",
4271 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4273 dev_kfree_skb(skb2);
4277 tx_dev = slave->dev;
4282 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4287 /* no suitable interface, frame not sent */
4290 /* frame sent to all suitable interfaces */
4291 read_unlock(&bond->lock);
4295 /*------------------------- Device initialization ---------------------------*/
4297 static void bond_set_xmit_hash_policy(struct bonding *bond)
4299 switch (bond->params.xmit_policy) {
4300 case BOND_XMIT_POLICY_LAYER23:
4301 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4303 case BOND_XMIT_POLICY_LAYER34:
4304 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4306 case BOND_XMIT_POLICY_LAYER2:
4308 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4314 * set bond mode specific net device operations
4316 void bond_set_mode_ops(struct bonding *bond, int mode)
4318 struct net_device *bond_dev = bond->dev;
4321 case BOND_MODE_ROUNDROBIN:
4322 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4324 case BOND_MODE_ACTIVEBACKUP:
4325 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4328 bond_dev->hard_start_xmit = bond_xmit_xor;
4329 bond_set_xmit_hash_policy(bond);
4331 case BOND_MODE_BROADCAST:
4332 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4334 case BOND_MODE_8023AD:
4335 bond_set_master_3ad_flags(bond);
4336 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4337 bond_set_xmit_hash_policy(bond);
4340 bond_set_master_alb_flags(bond);
4343 bond_dev->hard_start_xmit = bond_alb_xmit;
4344 bond_dev->set_mac_address = bond_alb_set_mac_address;
4347 /* Should never happen, mode already checked */
4348 printk(KERN_ERR DRV_NAME
4349 ": %s: Error: Unknown bonding mode %d\n",
4356 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4357 struct ethtool_drvinfo *drvinfo)
4359 strncpy(drvinfo->driver, DRV_NAME, 32);
4360 strncpy(drvinfo->version, DRV_VERSION, 32);
4361 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4364 static const struct ethtool_ops bond_ethtool_ops = {
4365 .get_drvinfo = bond_ethtool_get_drvinfo,
4369 * Does not allocate but creates a /proc entry.
4372 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4374 struct bonding *bond = bond_dev->priv;
4376 dprintk("Begin bond_init for %s\n", bond_dev->name);
4378 /* initialize rwlocks */
4379 rwlock_init(&bond->lock);
4380 rwlock_init(&bond->curr_slave_lock);
4382 bond->params = *params; /* copy params struct */
4384 bond->wq = create_singlethread_workqueue(bond_dev->name);
4388 /* Initialize pointers */
4389 bond->first_slave = NULL;
4390 bond->curr_active_slave = NULL;
4391 bond->current_arp_slave = NULL;
4392 bond->primary_slave = NULL;
4393 bond->dev = bond_dev;
4394 bond->send_grat_arp = 0;
4395 bond->setup_by_slave = 0;
4396 INIT_LIST_HEAD(&bond->vlan_list);
4398 /* Initialize the device entry points */
4399 bond_dev->open = bond_open;
4400 bond_dev->stop = bond_close;
4401 bond_dev->get_stats = bond_get_stats;
4402 bond_dev->do_ioctl = bond_do_ioctl;
4403 bond_dev->ethtool_ops = &bond_ethtool_ops;
4404 bond_dev->set_multicast_list = bond_set_multicast_list;
4405 bond_dev->change_mtu = bond_change_mtu;
4406 bond_dev->set_mac_address = bond_set_mac_address;
4407 bond_dev->validate_addr = NULL;
4409 bond_set_mode_ops(bond, bond->params.mode);
4411 bond_dev->destructor = free_netdev;
4413 /* Initialize the device options */
4414 bond_dev->tx_queue_len = 0;
4415 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4416 bond_dev->priv_flags |= IFF_BONDING;
4418 /* At first, we block adding VLANs. That's the only way to
4419 * prevent problems that occur when adding VLANs over an
4420 * empty bond. The block will be removed once non-challenged
4421 * slaves are enslaved.
4423 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4425 /* don't acquire bond device's netif_tx_lock when
4427 bond_dev->features |= NETIF_F_LLTX;
4429 /* By default, we declare the bond to be fully
4430 * VLAN hardware accelerated capable. Special
4431 * care is taken in the various xmit functions
4432 * when there are slaves that are not hw accel
4435 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4436 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4437 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4438 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4439 NETIF_F_HW_VLAN_RX |
4440 NETIF_F_HW_VLAN_FILTER);
4442 #ifdef CONFIG_PROC_FS
4443 bond_create_proc_entry(bond);
4445 list_add_tail(&bond->bond_list, &bond_dev_list);
4450 /* De-initialize device specific data.
4451 * Caller must hold rtnl_lock.
4453 static void bond_deinit(struct net_device *bond_dev)
4455 struct bonding *bond = bond_dev->priv;
4457 list_del(&bond->bond_list);
4459 #ifdef CONFIG_PROC_FS
4460 bond_remove_proc_entry(bond);
4464 static void bond_work_cancel_all(struct bonding *bond)
4466 write_lock_bh(&bond->lock);
4467 bond->kill_timers = 1;
4468 write_unlock_bh(&bond->lock);
4470 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4471 cancel_delayed_work(&bond->mii_work);
4473 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4474 cancel_delayed_work(&bond->arp_work);
4476 if (bond->params.mode == BOND_MODE_ALB &&
4477 delayed_work_pending(&bond->alb_work))
4478 cancel_delayed_work(&bond->alb_work);
4480 if (bond->params.mode == BOND_MODE_8023AD &&
4481 delayed_work_pending(&bond->ad_work))
4482 cancel_delayed_work(&bond->ad_work);
4485 /* Unregister and free all bond devices.
4486 * Caller must hold rtnl_lock.
4488 static void bond_free_all(void)
4490 struct bonding *bond, *nxt;
4492 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4493 struct net_device *bond_dev = bond->dev;
4495 bond_work_cancel_all(bond);
4496 netif_tx_lock_bh(bond_dev);
4497 bond_mc_list_destroy(bond);
4498 netif_tx_unlock_bh(bond_dev);
4499 /* Release the bonded slaves */
4500 bond_release_all(bond_dev);
4504 #ifdef CONFIG_PROC_FS
4505 bond_destroy_proc_dir();
4509 /*------------------------- Module initialization ---------------------------*/
4512 * Convert string input module parms. Accept either the
4513 * number of the mode or its string name. A bit complicated because
4514 * some mode names are substrings of other names, and calls from sysfs
4515 * may have whitespace in the name (trailing newlines, for example).
4517 int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
4519 int mode = -1, i, rv;
4520 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4522 for (p = (char *)buf; *p; p++)
4523 if (!(isdigit(*p) || isspace(*p)))
4527 rv = sscanf(buf, "%20s", modestr);
4529 rv = sscanf(buf, "%d", &mode);
4534 for (i = 0; tbl[i].modename; i++) {
4535 if (mode == tbl[i].mode)
4537 if (strcmp(modestr, tbl[i].modename) == 0)
4544 static int bond_check_params(struct bond_params *params)
4546 int arp_validate_value;
4549 * Convert string parameters.
4552 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4553 if (bond_mode == -1) {
4554 printk(KERN_ERR DRV_NAME
4555 ": Error: Invalid bonding mode \"%s\"\n",
4556 mode == NULL ? "NULL" : mode);
4561 if (xmit_hash_policy) {
4562 if ((bond_mode != BOND_MODE_XOR) &&
4563 (bond_mode != BOND_MODE_8023AD)) {
4564 printk(KERN_INFO DRV_NAME
4565 ": xor_mode param is irrelevant in mode %s\n",
4566 bond_mode_name(bond_mode));
4568 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4570 if (xmit_hashtype == -1) {
4571 printk(KERN_ERR DRV_NAME
4572 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4573 xmit_hash_policy == NULL ? "NULL" :
4581 if (bond_mode != BOND_MODE_8023AD) {
4582 printk(KERN_INFO DRV_NAME
4583 ": lacp_rate param is irrelevant in mode %s\n",
4584 bond_mode_name(bond_mode));
4586 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4587 if (lacp_fast == -1) {
4588 printk(KERN_ERR DRV_NAME
4589 ": Error: Invalid lacp rate \"%s\"\n",
4590 lacp_rate == NULL ? "NULL" : lacp_rate);
4596 if (max_bonds < 1 || max_bonds > INT_MAX) {
4597 printk(KERN_WARNING DRV_NAME
4598 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4599 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4600 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4601 max_bonds = BOND_DEFAULT_MAX_BONDS;
4605 printk(KERN_WARNING DRV_NAME
4606 ": Warning: miimon module parameter (%d), "
4607 "not in range 0-%d, so it was reset to %d\n",
4608 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4609 miimon = BOND_LINK_MON_INTERV;
4613 printk(KERN_WARNING DRV_NAME
4614 ": Warning: updelay module parameter (%d), "
4615 "not in range 0-%d, so it was reset to 0\n",
4620 if (downdelay < 0) {
4621 printk(KERN_WARNING DRV_NAME
4622 ": Warning: downdelay module parameter (%d), "
4623 "not in range 0-%d, so it was reset to 0\n",
4624 downdelay, INT_MAX);
4628 if ((use_carrier != 0) && (use_carrier != 1)) {
4629 printk(KERN_WARNING DRV_NAME
4630 ": Warning: use_carrier module parameter (%d), "
4631 "not of valid value (0/1), so it was set to 1\n",
4636 if (num_grat_arp < 0 || num_grat_arp > 255) {
4637 printk(KERN_WARNING DRV_NAME
4638 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4639 "was reset to 1 \n", num_grat_arp);
4643 /* reset values for 802.3ad */
4644 if (bond_mode == BOND_MODE_8023AD) {
4646 printk(KERN_WARNING DRV_NAME
4647 ": Warning: miimon must be specified, "
4648 "otherwise bonding will not detect link "
4649 "failure, speed and duplex which are "
4650 "essential for 802.3ad operation\n");
4651 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4656 /* reset values for TLB/ALB */
4657 if ((bond_mode == BOND_MODE_TLB) ||
4658 (bond_mode == BOND_MODE_ALB)) {
4660 printk(KERN_WARNING DRV_NAME
4661 ": Warning: miimon must be specified, "
4662 "otherwise bonding will not detect link "
4663 "failure and link speed which are essential "
4664 "for TLB/ALB load balancing\n");
4665 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4670 if (bond_mode == BOND_MODE_ALB) {
4671 printk(KERN_NOTICE DRV_NAME
4672 ": In ALB mode you might experience client "
4673 "disconnections upon reconnection of a link if the "
4674 "bonding module updelay parameter (%d msec) is "
4675 "incompatible with the forwarding delay time of the "
4681 if (updelay || downdelay) {
4682 /* just warn the user the up/down delay will have
4683 * no effect since miimon is zero...
4685 printk(KERN_WARNING DRV_NAME
4686 ": Warning: miimon module parameter not set "
4687 "and updelay (%d) or downdelay (%d) module "
4688 "parameter is set; updelay and downdelay have "
4689 "no effect unless miimon is set\n",
4690 updelay, downdelay);
4693 /* don't allow arp monitoring */
4695 printk(KERN_WARNING DRV_NAME
4696 ": Warning: miimon (%d) and arp_interval (%d) "
4697 "can't be used simultaneously, disabling ARP "
4699 miimon, arp_interval);
4703 if ((updelay % miimon) != 0) {
4704 printk(KERN_WARNING DRV_NAME
4705 ": Warning: updelay (%d) is not a multiple "
4706 "of miimon (%d), updelay rounded to %d ms\n",
4707 updelay, miimon, (updelay / miimon) * miimon);
4712 if ((downdelay % miimon) != 0) {
4713 printk(KERN_WARNING DRV_NAME
4714 ": Warning: downdelay (%d) is not a multiple "
4715 "of miimon (%d), downdelay rounded to %d ms\n",
4717 (downdelay / miimon) * miimon);
4720 downdelay /= miimon;
4723 if (arp_interval < 0) {
4724 printk(KERN_WARNING DRV_NAME
4725 ": Warning: arp_interval module parameter (%d) "
4726 ", not in range 0-%d, so it was reset to %d\n",
4727 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4728 arp_interval = BOND_LINK_ARP_INTERV;
4731 for (arp_ip_count = 0;
4732 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4734 /* not complete check, but should be good enough to
4736 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4737 printk(KERN_WARNING DRV_NAME
4738 ": Warning: bad arp_ip_target module parameter "
4739 "(%s), ARP monitoring will not be performed\n",
4740 arp_ip_target[arp_ip_count]);
4743 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4744 arp_target[arp_ip_count] = ip;
4748 if (arp_interval && !arp_ip_count) {
4749 /* don't allow arping if no arp_ip_target given... */
4750 printk(KERN_WARNING DRV_NAME
4751 ": Warning: arp_interval module parameter (%d) "
4752 "specified without providing an arp_ip_target "
4753 "parameter, arp_interval was reset to 0\n",
4759 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4760 printk(KERN_ERR DRV_NAME
4761 ": arp_validate only supported in active-backup mode\n");
4764 if (!arp_interval) {
4765 printk(KERN_ERR DRV_NAME
4766 ": arp_validate requires arp_interval\n");
4770 arp_validate_value = bond_parse_parm(arp_validate,
4772 if (arp_validate_value == -1) {
4773 printk(KERN_ERR DRV_NAME
4774 ": Error: invalid arp_validate \"%s\"\n",
4775 arp_validate == NULL ? "NULL" : arp_validate);
4779 arp_validate_value = 0;
4782 printk(KERN_INFO DRV_NAME
4783 ": MII link monitoring set to %d ms\n",
4785 } else if (arp_interval) {
4788 printk(KERN_INFO DRV_NAME
4789 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4791 arp_validate_tbl[arp_validate_value].modename,
4794 for (i = 0; i < arp_ip_count; i++)
4795 printk (" %s", arp_ip_target[i]);
4800 /* miimon and arp_interval not set, we need one so things
4801 * work as expected, see bonding.txt for details
4803 printk(KERN_WARNING DRV_NAME
4804 ": Warning: either miimon or arp_interval and "
4805 "arp_ip_target module parameters must be specified, "
4806 "otherwise bonding will not detect link failures! see "
4807 "bonding.txt for details.\n");
4810 if (primary && !USES_PRIMARY(bond_mode)) {
4811 /* currently, using a primary only makes sense
4812 * in active backup, TLB or ALB modes
4814 printk(KERN_WARNING DRV_NAME
4815 ": Warning: %s primary device specified but has no "
4816 "effect in %s mode\n",
4817 primary, bond_mode_name(bond_mode));
4821 if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
4822 printk(KERN_WARNING DRV_NAME
4823 ": Warning: fail_over_mac only affects "
4824 "active-backup mode.\n");
4826 /* fill params struct with the proper values */
4827 params->mode = bond_mode;
4828 params->xmit_policy = xmit_hashtype;
4829 params->miimon = miimon;
4830 params->num_grat_arp = num_grat_arp;
4831 params->arp_interval = arp_interval;
4832 params->arp_validate = arp_validate_value;
4833 params->updelay = updelay;
4834 params->downdelay = downdelay;
4835 params->use_carrier = use_carrier;
4836 params->lacp_fast = lacp_fast;
4837 params->primary[0] = 0;
4838 params->fail_over_mac = fail_over_mac;
4841 strncpy(params->primary, primary, IFNAMSIZ);
4842 params->primary[IFNAMSIZ - 1] = 0;
4845 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4850 static struct lock_class_key bonding_netdev_xmit_lock_key;
4852 /* Create a new bond based on the specified name and bonding parameters.
4853 * If name is NULL, obtain a suitable "bond%d" name for us.
4854 * Caller must NOT hold rtnl_lock; we need to release it here before we
4855 * set up our sysfs entries.
4857 int bond_create(char *name, struct bond_params *params)
4859 struct net_device *bond_dev;
4860 struct bonding *bond;
4864 down_write(&bonding_rwsem);
4866 /* Check to see if the bond already exists. */
4868 list_for_each_entry(bond, &bond_dev_list, bond_list)
4869 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
4870 printk(KERN_ERR DRV_NAME
4871 ": cannot add bond %s; it already exists\n",
4878 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4881 printk(KERN_ERR DRV_NAME
4882 ": %s: eek! can't alloc netdev!\n",
4889 res = dev_alloc_name(bond_dev, "bond%d");
4894 /* bond_init() must be called after dev_alloc_name() (for the
4895 * /proc files), but before register_netdevice(), because we
4896 * need to set function pointers.
4899 res = bond_init(bond_dev, params);
4904 res = register_netdevice(bond_dev);
4909 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4911 netif_carrier_off(bond_dev);
4913 up_write(&bonding_rwsem);
4914 rtnl_unlock(); /* allows sysfs registration of net device */
4915 res = bond_create_sysfs_entry(bond_dev->priv);
4918 down_write(&bonding_rwsem);
4919 bond_deinit(bond_dev);
4920 unregister_netdevice(bond_dev);
4927 bond_deinit(bond_dev);
4929 free_netdev(bond_dev);
4931 up_write(&bonding_rwsem);
4936 static int __init bonding_init(void)
4940 struct bonding *bond;
4942 printk(KERN_INFO "%s", version);
4944 res = bond_check_params(&bonding_defaults);
4949 #ifdef CONFIG_PROC_FS
4950 bond_create_proc_dir();
4953 init_rwsem(&bonding_rwsem);
4955 for (i = 0; i < max_bonds; i++) {
4956 res = bond_create(NULL, &bonding_defaults);
4961 res = bond_create_sysfs();
4965 register_netdevice_notifier(&bond_netdev_notifier);
4966 register_inetaddr_notifier(&bond_inetaddr_notifier);
4970 list_for_each_entry(bond, &bond_dev_list, bond_list) {
4971 bond_work_cancel_all(bond);
4972 destroy_workqueue(bond->wq);
4975 bond_destroy_sysfs();
4985 static void __exit bonding_exit(void)
4987 unregister_netdevice_notifier(&bond_netdev_notifier);
4988 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4990 bond_destroy_sysfs();
4997 module_init(bonding_init);
4998 module_exit(bonding_exit);
4999 MODULE_LICENSE("GPL");
5000 MODULE_VERSION(DRV_VERSION);
5001 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5002 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5003 MODULE_SUPPORTED_DEVICE("most ethernet devices");