SLUB: Fix memory leak by not reusing cpu_slab
[pv_ops_mirror.git] / drivers / net / bonding / bond_main.c
blob6937ef0e7275f40a14b227055f0f65886b24a052
1 /*
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:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
16 * How it works:
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.
23 * ifconfig bond0 down
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>
43 #include <linux/in.h>
44 #include <net/ip.h>
45 #include <linux/ip.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>
57 #include <asm/io.h>
58 #include <asm/dma.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>
66 #include <net/sock.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>
72 #include <net/arp.h>
73 #include <linux/mii.h>
74 #include <linux/ethtool.h>
75 #include <linux/if_vlan.h>
76 #include <linux/if_bonding.h>
77 #include <net/route.h>
78 #include <net/net_namespace.h>
79 #include "bonding.h"
80 #include "bond_3ad.h"
81 #include "bond_alb.h"
83 /*---------------------------- Module parameters ----------------------------*/
85 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
86 #define BOND_LINK_MON_INTERV 0
87 #define BOND_LINK_ARP_INTERV 0
89 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
90 static int miimon = BOND_LINK_MON_INTERV;
91 static int updelay = 0;
92 static int downdelay = 0;
93 static int use_carrier = 1;
94 static char *mode = NULL;
95 static char *primary = NULL;
96 static char *lacp_rate = NULL;
97 static char *xmit_hash_policy = NULL;
98 static int arp_interval = BOND_LINK_ARP_INTERV;
99 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
100 static char *arp_validate = NULL;
101 static int fail_over_mac = 0;
102 struct bond_params bonding_defaults;
104 module_param(max_bonds, int, 0);
105 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
106 module_param(miimon, int, 0);
107 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
108 module_param(updelay, int, 0);
109 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
110 module_param(downdelay, int, 0);
111 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
112 "in milliseconds");
113 module_param(use_carrier, int, 0);
114 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
115 "0 for off, 1 for on (default)");
116 module_param(mode, charp, 0);
117 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
118 "1 for active-backup, 2 for balance-xor, "
119 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
120 "6 for balance-alb");
121 module_param(primary, charp, 0);
122 MODULE_PARM_DESC(primary, "Primary network device to use");
123 module_param(lacp_rate, charp, 0);
124 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
125 "(slow/fast)");
126 module_param(xmit_hash_policy, charp, 0);
127 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
128 ", 1 for layer 3+4");
129 module_param(arp_interval, int, 0);
130 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
131 module_param_array(arp_ip_target, charp, NULL, 0);
132 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
133 module_param(arp_validate, charp, 0);
134 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
135 module_param(fail_over_mac, int, 0);
136 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.");
138 /*----------------------------- Global variables ----------------------------*/
140 static const char * const version =
141 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
143 LIST_HEAD(bond_dev_list);
145 #ifdef CONFIG_PROC_FS
146 static struct proc_dir_entry *bond_proc_dir = NULL;
147 #endif
149 extern struct rw_semaphore bonding_rwsem;
150 static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
151 static int arp_ip_count = 0;
152 static int bond_mode = BOND_MODE_ROUNDROBIN;
153 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
154 static int lacp_fast = 0;
157 struct bond_parm_tbl bond_lacp_tbl[] = {
158 { "slow", AD_LACP_SLOW},
159 { "fast", AD_LACP_FAST},
160 { NULL, -1},
163 struct bond_parm_tbl bond_mode_tbl[] = {
164 { "balance-rr", BOND_MODE_ROUNDROBIN},
165 { "active-backup", BOND_MODE_ACTIVEBACKUP},
166 { "balance-xor", BOND_MODE_XOR},
167 { "broadcast", BOND_MODE_BROADCAST},
168 { "802.3ad", BOND_MODE_8023AD},
169 { "balance-tlb", BOND_MODE_TLB},
170 { "balance-alb", BOND_MODE_ALB},
171 { NULL, -1},
174 struct bond_parm_tbl xmit_hashtype_tbl[] = {
175 { "layer2", BOND_XMIT_POLICY_LAYER2},
176 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
177 { NULL, -1},
180 struct bond_parm_tbl arp_validate_tbl[] = {
181 { "none", BOND_ARP_VALIDATE_NONE},
182 { "active", BOND_ARP_VALIDATE_ACTIVE},
183 { "backup", BOND_ARP_VALIDATE_BACKUP},
184 { "all", BOND_ARP_VALIDATE_ALL},
185 { NULL, -1},
188 /*-------------------------- Forward declarations ---------------------------*/
190 static void bond_send_gratuitous_arp(struct bonding *bond);
191 static void bond_deinit(struct net_device *bond_dev);
193 /*---------------------------- General routines -----------------------------*/
195 static const char *bond_mode_name(int mode)
197 switch (mode) {
198 case BOND_MODE_ROUNDROBIN :
199 return "load balancing (round-robin)";
200 case BOND_MODE_ACTIVEBACKUP :
201 return "fault-tolerance (active-backup)";
202 case BOND_MODE_XOR :
203 return "load balancing (xor)";
204 case BOND_MODE_BROADCAST :
205 return "fault-tolerance (broadcast)";
206 case BOND_MODE_8023AD:
207 return "IEEE 802.3ad Dynamic link aggregation";
208 case BOND_MODE_TLB:
209 return "transmit load balancing";
210 case BOND_MODE_ALB:
211 return "adaptive load balancing";
212 default:
213 return "unknown";
217 /*---------------------------------- VLAN -----------------------------------*/
220 * bond_add_vlan - add a new vlan id on bond
221 * @bond: bond that got the notification
222 * @vlan_id: the vlan id to add
224 * Returns -ENOMEM if allocation failed.
226 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
228 struct vlan_entry *vlan;
230 dprintk("bond: %s, vlan id %d\n",
231 (bond ? bond->dev->name: "None"), vlan_id);
233 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
234 if (!vlan) {
235 return -ENOMEM;
238 INIT_LIST_HEAD(&vlan->vlan_list);
239 vlan->vlan_id = vlan_id;
240 vlan->vlan_ip = 0;
242 write_lock_bh(&bond->lock);
244 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
246 write_unlock_bh(&bond->lock);
248 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
250 return 0;
254 * bond_del_vlan - delete a vlan id from bond
255 * @bond: bond that got the notification
256 * @vlan_id: the vlan id to delete
258 * returns -ENODEV if @vlan_id was not found in @bond.
260 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
262 struct vlan_entry *vlan, *next;
263 int res = -ENODEV;
265 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
267 write_lock_bh(&bond->lock);
269 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
270 if (vlan->vlan_id == vlan_id) {
271 list_del(&vlan->vlan_list);
273 if ((bond->params.mode == BOND_MODE_TLB) ||
274 (bond->params.mode == BOND_MODE_ALB)) {
275 bond_alb_clear_vlan(bond, vlan_id);
278 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
279 bond->dev->name);
281 kfree(vlan);
283 if (list_empty(&bond->vlan_list) &&
284 (bond->slave_cnt == 0)) {
285 /* Last VLAN removed and no slaves, so
286 * restore block on adding VLANs. This will
287 * be removed once new slaves that are not
288 * VLAN challenged will be added.
290 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
293 res = 0;
294 goto out;
298 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
299 bond->dev->name);
301 out:
302 write_unlock_bh(&bond->lock);
303 return res;
307 * bond_has_challenged_slaves
308 * @bond: the bond we're working on
310 * Searches the slave list. Returns 1 if a vlan challenged slave
311 * was found, 0 otherwise.
313 * Assumes bond->lock is held.
315 static int bond_has_challenged_slaves(struct bonding *bond)
317 struct slave *slave;
318 int i;
320 bond_for_each_slave(bond, slave, i) {
321 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
322 dprintk("found VLAN challenged slave - %s\n",
323 slave->dev->name);
324 return 1;
328 dprintk("no VLAN challenged slaves found\n");
329 return 0;
333 * bond_next_vlan - safely skip to the next item in the vlans list.
334 * @bond: the bond we're working on
335 * @curr: item we're advancing from
337 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
338 * or @curr->next otherwise (even if it is @curr itself again).
340 * Caller must hold bond->lock
342 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
344 struct vlan_entry *next, *last;
346 if (list_empty(&bond->vlan_list)) {
347 return NULL;
350 if (!curr) {
351 next = list_entry(bond->vlan_list.next,
352 struct vlan_entry, vlan_list);
353 } else {
354 last = list_entry(bond->vlan_list.prev,
355 struct vlan_entry, vlan_list);
356 if (last == curr) {
357 next = list_entry(bond->vlan_list.next,
358 struct vlan_entry, vlan_list);
359 } else {
360 next = list_entry(curr->vlan_list.next,
361 struct vlan_entry, vlan_list);
365 return next;
369 * bond_dev_queue_xmit - Prepare skb for xmit.
371 * @bond: bond device that got this skb for tx.
372 * @skb: hw accel VLAN tagged skb to transmit
373 * @slave_dev: slave that is supposed to xmit this skbuff
375 * When the bond gets an skb to transmit that is
376 * already hardware accelerated VLAN tagged, and it
377 * needs to relay this skb to a slave that is not
378 * hw accel capable, the skb needs to be "unaccelerated",
379 * i.e. strip the hwaccel tag and re-insert it as part
380 * of the payload.
382 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
384 unsigned short vlan_id;
386 if (!list_empty(&bond->vlan_list) &&
387 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
388 vlan_get_tag(skb, &vlan_id) == 0) {
389 skb->dev = slave_dev;
390 skb = vlan_put_tag(skb, vlan_id);
391 if (!skb) {
392 /* vlan_put_tag() frees the skb in case of error,
393 * so return success here so the calling functions
394 * won't attempt to free is again.
396 return 0;
398 } else {
399 skb->dev = slave_dev;
402 skb->priority = 1;
403 dev_queue_xmit(skb);
405 return 0;
409 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
410 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
411 * lock because:
412 * a. This operation is performed in IOCTL context,
413 * b. The operation is protected by the RTNL semaphore in the 8021q code,
414 * c. Holding a lock with BH disabled while directly calling a base driver
415 * entry point is generally a BAD idea.
417 * The design of synchronization/protection for this operation in the 8021q
418 * module is good for one or more VLAN devices over a single physical device
419 * and cannot be extended for a teaming solution like bonding, so there is a
420 * potential race condition here where a net device from the vlan group might
421 * be referenced (either by a base driver or the 8021q code) while it is being
422 * removed from the system. However, it turns out we're not making matters
423 * worse, and if it works for regular VLAN usage it will work here too.
427 * bond_vlan_rx_register - Propagates registration to slaves
428 * @bond_dev: bonding net device that got called
429 * @grp: vlan group being registered
431 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
433 struct bonding *bond = bond_dev->priv;
434 struct slave *slave;
435 int i;
437 bond->vlgrp = grp;
439 bond_for_each_slave(bond, slave, i) {
440 struct net_device *slave_dev = slave->dev;
442 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
443 slave_dev->vlan_rx_register) {
444 slave_dev->vlan_rx_register(slave_dev, grp);
450 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
451 * @bond_dev: bonding net device that got called
452 * @vid: vlan id being added
454 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
456 struct bonding *bond = bond_dev->priv;
457 struct slave *slave;
458 int i, res;
460 bond_for_each_slave(bond, slave, i) {
461 struct net_device *slave_dev = slave->dev;
463 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
464 slave_dev->vlan_rx_add_vid) {
465 slave_dev->vlan_rx_add_vid(slave_dev, vid);
469 res = bond_add_vlan(bond, vid);
470 if (res) {
471 printk(KERN_ERR DRV_NAME
472 ": %s: Error: Failed to add vlan id %d\n",
473 bond_dev->name, vid);
478 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
479 * @bond_dev: bonding net device that got called
480 * @vid: vlan id being removed
482 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
484 struct bonding *bond = bond_dev->priv;
485 struct slave *slave;
486 struct net_device *vlan_dev;
487 int i, res;
489 bond_for_each_slave(bond, slave, i) {
490 struct net_device *slave_dev = slave->dev;
492 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
493 slave_dev->vlan_rx_kill_vid) {
494 /* Save and then restore vlan_dev in the grp array,
495 * since the slave's driver might clear it.
497 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
498 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
499 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
503 res = bond_del_vlan(bond, vid);
504 if (res) {
505 printk(KERN_ERR DRV_NAME
506 ": %s: Error: Failed to remove vlan id %d\n",
507 bond_dev->name, vid);
511 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
513 struct vlan_entry *vlan;
515 write_lock_bh(&bond->lock);
517 if (list_empty(&bond->vlan_list)) {
518 goto out;
521 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
522 slave_dev->vlan_rx_register) {
523 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
526 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
527 !(slave_dev->vlan_rx_add_vid)) {
528 goto out;
531 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
532 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
535 out:
536 write_unlock_bh(&bond->lock);
539 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
541 struct vlan_entry *vlan;
542 struct net_device *vlan_dev;
544 write_lock_bh(&bond->lock);
546 if (list_empty(&bond->vlan_list)) {
547 goto out;
550 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
551 !(slave_dev->vlan_rx_kill_vid)) {
552 goto unreg;
555 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
556 /* Save and then restore vlan_dev in the grp array,
557 * since the slave's driver might clear it.
559 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
560 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
561 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
564 unreg:
565 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
566 slave_dev->vlan_rx_register) {
567 slave_dev->vlan_rx_register(slave_dev, NULL);
570 out:
571 write_unlock_bh(&bond->lock);
574 /*------------------------------- Link status -------------------------------*/
577 * Set the carrier state for the master according to the state of its
578 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
579 * do special 802.3ad magic.
581 * Returns zero if carrier state does not change, nonzero if it does.
583 static int bond_set_carrier(struct bonding *bond)
585 struct slave *slave;
586 int i;
588 if (bond->slave_cnt == 0)
589 goto down;
591 if (bond->params.mode == BOND_MODE_8023AD)
592 return bond_3ad_set_carrier(bond);
594 bond_for_each_slave(bond, slave, i) {
595 if (slave->link == BOND_LINK_UP) {
596 if (!netif_carrier_ok(bond->dev)) {
597 netif_carrier_on(bond->dev);
598 return 1;
600 return 0;
604 down:
605 if (netif_carrier_ok(bond->dev)) {
606 netif_carrier_off(bond->dev);
607 return 1;
609 return 0;
613 * Get link speed and duplex from the slave's base driver
614 * using ethtool. If for some reason the call fails or the
615 * values are invalid, fake speed and duplex to 100/Full
616 * and return error.
618 static int bond_update_speed_duplex(struct slave *slave)
620 struct net_device *slave_dev = slave->dev;
621 struct ethtool_cmd etool;
622 int res;
624 /* Fake speed and duplex */
625 slave->speed = SPEED_100;
626 slave->duplex = DUPLEX_FULL;
628 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
629 return -1;
631 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
632 if (res < 0)
633 return -1;
635 switch (etool.speed) {
636 case SPEED_10:
637 case SPEED_100:
638 case SPEED_1000:
639 case SPEED_10000:
640 break;
641 default:
642 return -1;
645 switch (etool.duplex) {
646 case DUPLEX_FULL:
647 case DUPLEX_HALF:
648 break;
649 default:
650 return -1;
653 slave->speed = etool.speed;
654 slave->duplex = etool.duplex;
656 return 0;
660 * if <dev> supports MII link status reporting, check its link status.
662 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
663 * depening upon the setting of the use_carrier parameter.
665 * Return either BMSR_LSTATUS, meaning that the link is up (or we
666 * can't tell and just pretend it is), or 0, meaning that the link is
667 * down.
669 * If reporting is non-zero, instead of faking link up, return -1 if
670 * both ETHTOOL and MII ioctls fail (meaning the device does not
671 * support them). If use_carrier is set, return whatever it says.
672 * It'd be nice if there was a good way to tell if a driver supports
673 * netif_carrier, but there really isn't.
675 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
677 static int (* ioctl)(struct net_device *, struct ifreq *, int);
678 struct ifreq ifr;
679 struct mii_ioctl_data *mii;
681 if (bond->params.use_carrier) {
682 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
685 ioctl = slave_dev->do_ioctl;
686 if (ioctl) {
687 /* TODO: set pointer to correct ioctl on a per team member */
688 /* bases to make this more efficient. that is, once */
689 /* we determine the correct ioctl, we will always */
690 /* call it and not the others for that team */
691 /* member. */
694 * We cannot assume that SIOCGMIIPHY will also read a
695 * register; not all network drivers (e.g., e100)
696 * support that.
699 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
700 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
701 mii = if_mii(&ifr);
702 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
703 mii->reg_num = MII_BMSR;
704 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
705 return (mii->val_out & BMSR_LSTATUS);
711 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
712 * attempt to get link status from it if the above MII ioctls fail.
714 if (slave_dev->ethtool_ops) {
715 if (slave_dev->ethtool_ops->get_link) {
716 u32 link;
718 link = slave_dev->ethtool_ops->get_link(slave_dev);
720 return link ? BMSR_LSTATUS : 0;
725 * If reporting, report that either there's no dev->do_ioctl,
726 * or both SIOCGMIIREG and get_link failed (meaning that we
727 * cannot report link status). If not reporting, pretend
728 * we're ok.
730 return (reporting ? -1 : BMSR_LSTATUS);
733 /*----------------------------- Multicast list ------------------------------*/
736 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
738 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
740 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
741 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
745 * returns dmi entry if found, NULL otherwise
747 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
749 struct dev_mc_list *idmi;
751 for (idmi = mc_list; idmi; idmi = idmi->next) {
752 if (bond_is_dmi_same(dmi, idmi)) {
753 return idmi;
757 return NULL;
761 * Push the promiscuity flag down to appropriate slaves
763 static void bond_set_promiscuity(struct bonding *bond, int inc)
765 if (USES_PRIMARY(bond->params.mode)) {
766 /* write lock already acquired */
767 if (bond->curr_active_slave) {
768 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
770 } else {
771 struct slave *slave;
772 int i;
773 bond_for_each_slave(bond, slave, i) {
774 dev_set_promiscuity(slave->dev, inc);
780 * Push the allmulti flag down to all slaves
782 static void bond_set_allmulti(struct bonding *bond, int inc)
784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
787 dev_set_allmulti(bond->curr_active_slave->dev, inc);
789 } else {
790 struct slave *slave;
791 int i;
792 bond_for_each_slave(bond, slave, i) {
793 dev_set_allmulti(slave->dev, inc);
799 * Add a Multicast address to slaves
800 * according to mode
802 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
807 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
809 } else {
810 struct slave *slave;
811 int i;
812 bond_for_each_slave(bond, slave, i) {
813 dev_mc_add(slave->dev, addr, alen, 0);
819 * Remove a multicast address from slave
820 * according to mode
822 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
824 if (USES_PRIMARY(bond->params.mode)) {
825 /* write lock already acquired */
826 if (bond->curr_active_slave) {
827 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
829 } else {
830 struct slave *slave;
831 int i;
832 bond_for_each_slave(bond, slave, i) {
833 dev_mc_delete(slave->dev, addr, alen, 0);
840 * Retrieve the list of registered multicast addresses for the bonding
841 * device and retransmit an IGMP JOIN request to the current active
842 * slave.
844 static void bond_resend_igmp_join_requests(struct bonding *bond)
846 struct in_device *in_dev;
847 struct ip_mc_list *im;
849 rcu_read_lock();
850 in_dev = __in_dev_get_rcu(bond->dev);
851 if (in_dev) {
852 for (im = in_dev->mc_list; im; im = im->next) {
853 ip_mc_rejoin_group(im);
857 rcu_read_unlock();
861 * Totally destroys the mc_list in bond
863 static void bond_mc_list_destroy(struct bonding *bond)
865 struct dev_mc_list *dmi;
867 dmi = bond->mc_list;
868 while (dmi) {
869 bond->mc_list = dmi->next;
870 kfree(dmi);
871 dmi = bond->mc_list;
873 bond->mc_list = NULL;
877 * Copy all the Multicast addresses from src to the bonding device dst
879 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
880 gfp_t gfp_flag)
882 struct dev_mc_list *dmi, *new_dmi;
884 for (dmi = mc_list; dmi; dmi = dmi->next) {
885 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
887 if (!new_dmi) {
888 /* FIXME: Potential memory leak !!! */
889 return -ENOMEM;
892 new_dmi->next = bond->mc_list;
893 bond->mc_list = new_dmi;
894 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
895 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
896 new_dmi->dmi_users = dmi->dmi_users;
897 new_dmi->dmi_gusers = dmi->dmi_gusers;
900 return 0;
904 * flush all members of flush->mc_list from device dev->mc_list
906 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
908 struct bonding *bond = bond_dev->priv;
909 struct dev_mc_list *dmi;
911 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
912 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
915 if (bond->params.mode == BOND_MODE_8023AD) {
916 /* del lacpdu mc addr from mc list */
917 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
919 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
923 /*--------------------------- Active slave change ---------------------------*/
926 * Update the mc list and multicast-related flags for the new and
927 * old active slaves (if any) according to the multicast mode, and
928 * promiscuous flags unconditionally.
930 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
932 struct dev_mc_list *dmi;
934 if (!USES_PRIMARY(bond->params.mode)) {
935 /* nothing to do - mc list is already up-to-date on
936 * all slaves
938 return;
941 if (old_active) {
942 if (bond->dev->flags & IFF_PROMISC) {
943 dev_set_promiscuity(old_active->dev, -1);
946 if (bond->dev->flags & IFF_ALLMULTI) {
947 dev_set_allmulti(old_active->dev, -1);
950 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
951 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
955 if (new_active) {
956 if (bond->dev->flags & IFF_PROMISC) {
957 dev_set_promiscuity(new_active->dev, 1);
960 if (bond->dev->flags & IFF_ALLMULTI) {
961 dev_set_allmulti(new_active->dev, 1);
964 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
965 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
967 bond_resend_igmp_join_requests(bond);
972 * find_best_interface - select the best available slave to be the active one
973 * @bond: our bonding struct
975 * Warning: Caller must hold curr_slave_lock for writing.
977 static struct slave *bond_find_best_slave(struct bonding *bond)
979 struct slave *new_active, *old_active;
980 struct slave *bestslave = NULL;
981 int mintime = bond->params.updelay;
982 int i;
984 new_active = old_active = bond->curr_active_slave;
986 if (!new_active) { /* there were no active slaves left */
987 if (bond->slave_cnt > 0) { /* found one slave */
988 new_active = bond->first_slave;
989 } else {
990 return NULL; /* still no slave, return NULL */
994 /* first try the primary link; if arping, a link must tx/rx traffic
995 * before it can be considered the curr_active_slave - also, we would skip
996 * slaves between the curr_active_slave and primary_slave that may be up
997 * and able to arp
999 if ((bond->primary_slave) &&
1000 (!bond->params.arp_interval) &&
1001 (IS_UP(bond->primary_slave->dev))) {
1002 new_active = bond->primary_slave;
1005 /* remember where to stop iterating over the slaves */
1006 old_active = new_active;
1008 bond_for_each_slave_from(bond, new_active, i, old_active) {
1009 if (IS_UP(new_active->dev)) {
1010 if (new_active->link == BOND_LINK_UP) {
1011 return new_active;
1012 } else if (new_active->link == BOND_LINK_BACK) {
1013 /* link up, but waiting for stabilization */
1014 if (new_active->delay < mintime) {
1015 mintime = new_active->delay;
1016 bestslave = new_active;
1022 return bestslave;
1026 * change_active_interface - change the active slave into the specified one
1027 * @bond: our bonding struct
1028 * @new: the new slave to make the active one
1030 * Set the new slave to the bond's settings and unset them on the old
1031 * curr_active_slave.
1032 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1034 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1035 * because it is apparently the best available slave we have, even though its
1036 * updelay hasn't timed out yet.
1038 * Warning: Caller must hold curr_slave_lock for writing.
1040 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1042 struct slave *old_active = bond->curr_active_slave;
1044 if (old_active == new_active) {
1045 return;
1048 if (new_active) {
1049 if (new_active->link == BOND_LINK_BACK) {
1050 if (USES_PRIMARY(bond->params.mode)) {
1051 printk(KERN_INFO DRV_NAME
1052 ": %s: making interface %s the new "
1053 "active one %d ms earlier.\n",
1054 bond->dev->name, new_active->dev->name,
1055 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1058 new_active->delay = 0;
1059 new_active->link = BOND_LINK_UP;
1060 new_active->jiffies = jiffies;
1062 if (bond->params.mode == BOND_MODE_8023AD) {
1063 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1066 if ((bond->params.mode == BOND_MODE_TLB) ||
1067 (bond->params.mode == BOND_MODE_ALB)) {
1068 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1070 } else {
1071 if (USES_PRIMARY(bond->params.mode)) {
1072 printk(KERN_INFO DRV_NAME
1073 ": %s: making interface %s the new "
1074 "active one.\n",
1075 bond->dev->name, new_active->dev->name);
1080 if (USES_PRIMARY(bond->params.mode)) {
1081 bond_mc_swap(bond, new_active, old_active);
1084 if ((bond->params.mode == BOND_MODE_TLB) ||
1085 (bond->params.mode == BOND_MODE_ALB)) {
1086 bond_alb_handle_active_change(bond, new_active);
1087 if (old_active)
1088 bond_set_slave_inactive_flags(old_active);
1089 if (new_active)
1090 bond_set_slave_active_flags(new_active);
1091 } else {
1092 bond->curr_active_slave = new_active;
1095 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1096 if (old_active) {
1097 bond_set_slave_inactive_flags(old_active);
1100 if (new_active) {
1101 bond_set_slave_active_flags(new_active);
1104 /* when bonding does not set the slave MAC address, the bond MAC
1105 * address is the one of the active slave.
1107 if (new_active && bond->params.fail_over_mac)
1108 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1109 new_active->dev->addr_len);
1110 if (bond->curr_active_slave &&
1111 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1112 &bond->curr_active_slave->dev->state)) {
1113 dprintk("delaying gratuitous arp on %s\n",
1114 bond->curr_active_slave->dev->name);
1115 bond->send_grat_arp = 1;
1116 } else
1117 bond_send_gratuitous_arp(bond);
1122 * bond_select_active_slave - select a new active slave, if needed
1123 * @bond: our bonding struct
1125 * This functions shoud be called when one of the following occurs:
1126 * - The old curr_active_slave has been released or lost its link.
1127 * - The primary_slave has got its link back.
1128 * - A slave has got its link back and there's no old curr_active_slave.
1130 * Warning: Caller must hold curr_slave_lock for writing.
1132 void bond_select_active_slave(struct bonding *bond)
1134 struct slave *best_slave;
1135 int rv;
1137 best_slave = bond_find_best_slave(bond);
1138 if (best_slave != bond->curr_active_slave) {
1139 bond_change_active_slave(bond, best_slave);
1140 rv = bond_set_carrier(bond);
1141 if (!rv)
1142 return;
1144 if (netif_carrier_ok(bond->dev)) {
1145 printk(KERN_INFO DRV_NAME
1146 ": %s: first active interface up!\n",
1147 bond->dev->name);
1148 } else {
1149 printk(KERN_INFO DRV_NAME ": %s: "
1150 "now running without any active interface !\n",
1151 bond->dev->name);
1156 /*--------------------------- slave list handling ---------------------------*/
1159 * This function attaches the slave to the end of list.
1161 * bond->lock held for writing by caller.
1163 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1165 if (bond->first_slave == NULL) { /* attaching the first slave */
1166 new_slave->next = new_slave;
1167 new_slave->prev = new_slave;
1168 bond->first_slave = new_slave;
1169 } else {
1170 new_slave->next = bond->first_slave;
1171 new_slave->prev = bond->first_slave->prev;
1172 new_slave->next->prev = new_slave;
1173 new_slave->prev->next = new_slave;
1176 bond->slave_cnt++;
1180 * This function detaches the slave from the list.
1181 * WARNING: no check is made to verify if the slave effectively
1182 * belongs to <bond>.
1183 * Nothing is freed on return, structures are just unchained.
1184 * If any slave pointer in bond was pointing to <slave>,
1185 * it should be changed by the calling function.
1187 * bond->lock held for writing by caller.
1189 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1191 if (slave->next) {
1192 slave->next->prev = slave->prev;
1195 if (slave->prev) {
1196 slave->prev->next = slave->next;
1199 if (bond->first_slave == slave) { /* slave is the first slave */
1200 if (bond->slave_cnt > 1) { /* there are more slave */
1201 bond->first_slave = slave->next;
1202 } else {
1203 bond->first_slave = NULL; /* slave was the last one */
1207 slave->next = NULL;
1208 slave->prev = NULL;
1209 bond->slave_cnt--;
1212 /*---------------------------------- IOCTL ----------------------------------*/
1214 static int bond_sethwaddr(struct net_device *bond_dev,
1215 struct net_device *slave_dev)
1217 dprintk("bond_dev=%p\n", bond_dev);
1218 dprintk("slave_dev=%p\n", slave_dev);
1219 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1220 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1221 return 0;
1224 #define BOND_VLAN_FEATURES \
1225 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1226 NETIF_F_HW_VLAN_FILTER)
1229 * Compute the common dev->feature set available to all slaves. Some
1230 * feature bits are managed elsewhere, so preserve those feature bits
1231 * on the master device.
1233 static int bond_compute_features(struct bonding *bond)
1235 struct slave *slave;
1236 struct net_device *bond_dev = bond->dev;
1237 unsigned long features = bond_dev->features;
1238 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1239 bond_dev->hard_header_len);
1240 int i;
1242 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1243 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1244 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1246 bond_for_each_slave(bond, slave, i) {
1247 features = netdev_compute_features(features,
1248 slave->dev->features);
1249 if (slave->dev->hard_header_len > max_hard_header_len)
1250 max_hard_header_len = slave->dev->hard_header_len;
1253 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1254 bond_dev->features = features;
1255 bond_dev->hard_header_len = max_hard_header_len;
1257 return 0;
1261 static void bond_setup_by_slave(struct net_device *bond_dev,
1262 struct net_device *slave_dev)
1264 struct bonding *bond = bond_dev->priv;
1266 bond_dev->neigh_setup = slave_dev->neigh_setup;
1267 bond_dev->header_ops = slave_dev->header_ops;
1269 bond_dev->type = slave_dev->type;
1270 bond_dev->hard_header_len = slave_dev->hard_header_len;
1271 bond_dev->addr_len = slave_dev->addr_len;
1273 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1274 slave_dev->addr_len);
1275 bond->setup_by_slave = 1;
1278 /* enslave device <slave> to bond device <master> */
1279 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1281 struct bonding *bond = bond_dev->priv;
1282 struct slave *new_slave = NULL;
1283 struct dev_mc_list *dmi;
1284 struct sockaddr addr;
1285 int link_reporting;
1286 int old_features = bond_dev->features;
1287 int res = 0;
1289 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1290 slave_dev->do_ioctl == NULL) {
1291 printk(KERN_WARNING DRV_NAME
1292 ": %s: Warning: no link monitoring support for %s\n",
1293 bond_dev->name, slave_dev->name);
1296 /* bond must be initialized by bond_open() before enslaving */
1297 if (!(bond_dev->flags & IFF_UP)) {
1298 printk(KERN_WARNING DRV_NAME
1299 " %s: master_dev is not up in bond_enslave\n",
1300 bond_dev->name);
1303 /* already enslaved */
1304 if (slave_dev->flags & IFF_SLAVE) {
1305 dprintk("Error, Device was already enslaved\n");
1306 return -EBUSY;
1309 /* vlan challenged mutual exclusion */
1310 /* no need to lock since we're protected by rtnl_lock */
1311 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1312 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1313 if (!list_empty(&bond->vlan_list)) {
1314 printk(KERN_ERR DRV_NAME
1315 ": %s: Error: cannot enslave VLAN "
1316 "challenged slave %s on VLAN enabled "
1317 "bond %s\n", bond_dev->name, slave_dev->name,
1318 bond_dev->name);
1319 return -EPERM;
1320 } else {
1321 printk(KERN_WARNING DRV_NAME
1322 ": %s: Warning: enslaved VLAN challenged "
1323 "slave %s. Adding VLANs will be blocked as "
1324 "long as %s is part of bond %s\n",
1325 bond_dev->name, slave_dev->name, slave_dev->name,
1326 bond_dev->name);
1327 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1329 } else {
1330 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1331 if (bond->slave_cnt == 0) {
1332 /* First slave, and it is not VLAN challenged,
1333 * so remove the block of adding VLANs over the bond.
1335 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1340 * Old ifenslave binaries are no longer supported. These can
1341 * be identified with moderate accurary by the state of the slave:
1342 * the current ifenslave will set the interface down prior to
1343 * enslaving it; the old ifenslave will not.
1345 if ((slave_dev->flags & IFF_UP)) {
1346 printk(KERN_ERR DRV_NAME ": %s is up. "
1347 "This may be due to an out of date ifenslave.\n",
1348 slave_dev->name);
1349 res = -EPERM;
1350 goto err_undo_flags;
1353 /* set bonding device ether type by slave - bonding netdevices are
1354 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1355 * there is a need to override some of the type dependent attribs/funcs.
1357 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1358 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1360 if (bond->slave_cnt == 0) {
1361 if (slave_dev->type != ARPHRD_ETHER)
1362 bond_setup_by_slave(bond_dev, slave_dev);
1363 } else if (bond_dev->type != slave_dev->type) {
1364 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1365 "from other slaves (%d), can not enslave it.\n",
1366 slave_dev->name,
1367 slave_dev->type, bond_dev->type);
1368 res = -EINVAL;
1369 goto err_undo_flags;
1372 if (slave_dev->set_mac_address == NULL) {
1373 if (bond->slave_cnt == 0) {
1374 printk(KERN_WARNING DRV_NAME
1375 ": %s: Warning: The first slave device "
1376 "specified does not support setting the MAC "
1377 "address. Enabling the fail_over_mac option.",
1378 bond_dev->name);
1379 bond->params.fail_over_mac = 1;
1380 } else if (!bond->params.fail_over_mac) {
1381 printk(KERN_ERR DRV_NAME
1382 ": %s: Error: The slave device specified "
1383 "does not support setting the MAC address, "
1384 "but fail_over_mac is not enabled.\n"
1385 , bond_dev->name);
1386 res = -EOPNOTSUPP;
1387 goto err_undo_flags;
1391 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1392 if (!new_slave) {
1393 res = -ENOMEM;
1394 goto err_undo_flags;
1397 /* save slave's original flags before calling
1398 * netdev_set_master and dev_open
1400 new_slave->original_flags = slave_dev->flags;
1403 * Save slave's original ("permanent") mac address for modes
1404 * that need it, and for restoring it upon release, and then
1405 * set it to the master's address
1407 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1409 if (!bond->params.fail_over_mac) {
1411 * Set slave to master's mac address. The application already
1412 * set the master's mac address to that of the first slave
1414 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1415 addr.sa_family = slave_dev->type;
1416 res = dev_set_mac_address(slave_dev, &addr);
1417 if (res) {
1418 dprintk("Error %d calling set_mac_address\n", res);
1419 goto err_free;
1423 res = netdev_set_master(slave_dev, bond_dev);
1424 if (res) {
1425 dprintk("Error %d calling netdev_set_master\n", res);
1426 goto err_close;
1428 /* open the slave since the application closed it */
1429 res = dev_open(slave_dev);
1430 if (res) {
1431 dprintk("Openning slave %s failed\n", slave_dev->name);
1432 goto err_restore_mac;
1435 new_slave->dev = slave_dev;
1436 slave_dev->priv_flags |= IFF_BONDING;
1438 if ((bond->params.mode == BOND_MODE_TLB) ||
1439 (bond->params.mode == BOND_MODE_ALB)) {
1440 /* bond_alb_init_slave() must be called before all other stages since
1441 * it might fail and we do not want to have to undo everything
1443 res = bond_alb_init_slave(bond, new_slave);
1444 if (res) {
1445 goto err_unset_master;
1449 /* If the mode USES_PRIMARY, then the new slave gets the
1450 * master's promisc (and mc) settings only if it becomes the
1451 * curr_active_slave, and that is taken care of later when calling
1452 * bond_change_active()
1454 if (!USES_PRIMARY(bond->params.mode)) {
1455 /* set promiscuity level to new slave */
1456 if (bond_dev->flags & IFF_PROMISC) {
1457 dev_set_promiscuity(slave_dev, 1);
1460 /* set allmulti level to new slave */
1461 if (bond_dev->flags & IFF_ALLMULTI) {
1462 dev_set_allmulti(slave_dev, 1);
1465 /* upload master's mc_list to new slave */
1466 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1467 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1471 if (bond->params.mode == BOND_MODE_8023AD) {
1472 /* add lacpdu mc addr to mc list */
1473 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1475 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1478 bond_add_vlans_on_slave(bond, slave_dev);
1480 write_lock_bh(&bond->lock);
1482 bond_attach_slave(bond, new_slave);
1484 new_slave->delay = 0;
1485 new_slave->link_failure_count = 0;
1487 bond_compute_features(bond);
1489 new_slave->last_arp_rx = jiffies;
1491 if (bond->params.miimon && !bond->params.use_carrier) {
1492 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1494 if ((link_reporting == -1) && !bond->params.arp_interval) {
1496 * miimon is set but a bonded network driver
1497 * does not support ETHTOOL/MII and
1498 * arp_interval is not set. Note: if
1499 * use_carrier is enabled, we will never go
1500 * here (because netif_carrier is always
1501 * supported); thus, we don't need to change
1502 * the messages for netif_carrier.
1504 printk(KERN_WARNING DRV_NAME
1505 ": %s: Warning: MII and ETHTOOL support not "
1506 "available for interface %s, and "
1507 "arp_interval/arp_ip_target module parameters "
1508 "not specified, thus bonding will not detect "
1509 "link failures! see bonding.txt for details.\n",
1510 bond_dev->name, slave_dev->name);
1511 } else if (link_reporting == -1) {
1512 /* unable get link status using mii/ethtool */
1513 printk(KERN_WARNING DRV_NAME
1514 ": %s: Warning: can't get link status from "
1515 "interface %s; the network driver associated "
1516 "with this interface does not support MII or "
1517 "ETHTOOL link status reporting, thus miimon "
1518 "has no effect on this interface.\n",
1519 bond_dev->name, slave_dev->name);
1523 /* check for initial state */
1524 if (!bond->params.miimon ||
1525 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1526 if (bond->params.updelay) {
1527 dprintk("Initial state of slave_dev is "
1528 "BOND_LINK_BACK\n");
1529 new_slave->link = BOND_LINK_BACK;
1530 new_slave->delay = bond->params.updelay;
1531 } else {
1532 dprintk("Initial state of slave_dev is "
1533 "BOND_LINK_UP\n");
1534 new_slave->link = BOND_LINK_UP;
1536 new_slave->jiffies = jiffies;
1537 } else {
1538 dprintk("Initial state of slave_dev is "
1539 "BOND_LINK_DOWN\n");
1540 new_slave->link = BOND_LINK_DOWN;
1543 if (bond_update_speed_duplex(new_slave) &&
1544 (new_slave->link != BOND_LINK_DOWN)) {
1545 printk(KERN_WARNING DRV_NAME
1546 ": %s: Warning: failed to get speed and duplex from %s, "
1547 "assumed to be 100Mb/sec and Full.\n",
1548 bond_dev->name, new_slave->dev->name);
1550 if (bond->params.mode == BOND_MODE_8023AD) {
1551 printk(KERN_WARNING DRV_NAME
1552 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1553 "support in base driver for proper aggregator "
1554 "selection.\n", bond_dev->name);
1558 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1559 /* if there is a primary slave, remember it */
1560 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1561 bond->primary_slave = new_slave;
1565 switch (bond->params.mode) {
1566 case BOND_MODE_ACTIVEBACKUP:
1567 bond_set_slave_inactive_flags(new_slave);
1568 bond_select_active_slave(bond);
1569 break;
1570 case BOND_MODE_8023AD:
1571 /* in 802.3ad mode, the internal mechanism
1572 * will activate the slaves in the selected
1573 * aggregator
1575 bond_set_slave_inactive_flags(new_slave);
1576 /* if this is the first slave */
1577 if (bond->slave_cnt == 1) {
1578 SLAVE_AD_INFO(new_slave).id = 1;
1579 /* Initialize AD with the number of times that the AD timer is called in 1 second
1580 * can be called only after the mac address of the bond is set
1582 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1583 bond->params.lacp_fast);
1584 } else {
1585 SLAVE_AD_INFO(new_slave).id =
1586 SLAVE_AD_INFO(new_slave->prev).id + 1;
1589 bond_3ad_bind_slave(new_slave);
1590 break;
1591 case BOND_MODE_TLB:
1592 case BOND_MODE_ALB:
1593 new_slave->state = BOND_STATE_ACTIVE;
1594 bond_set_slave_inactive_flags(new_slave);
1595 break;
1596 default:
1597 dprintk("This slave is always active in trunk mode\n");
1599 /* always active in trunk mode */
1600 new_slave->state = BOND_STATE_ACTIVE;
1602 /* In trunking mode there is little meaning to curr_active_slave
1603 * anyway (it holds no special properties of the bond device),
1604 * so we can change it without calling change_active_interface()
1606 if (!bond->curr_active_slave) {
1607 bond->curr_active_slave = new_slave;
1609 break;
1610 } /* switch(bond_mode) */
1612 bond_set_carrier(bond);
1614 write_unlock_bh(&bond->lock);
1616 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1617 if (res)
1618 goto err_unset_master;
1620 printk(KERN_INFO DRV_NAME
1621 ": %s: enslaving %s as a%s interface with a%s link.\n",
1622 bond_dev->name, slave_dev->name,
1623 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1624 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1626 /* enslave is successful */
1627 return 0;
1629 /* Undo stages on error */
1630 err_unset_master:
1631 netdev_set_master(slave_dev, NULL);
1633 err_close:
1634 dev_close(slave_dev);
1636 err_restore_mac:
1637 if (!bond->params.fail_over_mac) {
1638 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1639 addr.sa_family = slave_dev->type;
1640 dev_set_mac_address(slave_dev, &addr);
1643 err_free:
1644 kfree(new_slave);
1646 err_undo_flags:
1647 bond_dev->features = old_features;
1649 return res;
1653 * Try to release the slave device <slave> from the bond device <master>
1654 * It is legal to access curr_active_slave without a lock because all the function
1655 * is write-locked.
1657 * The rules for slave state should be:
1658 * for Active/Backup:
1659 * Active stays on all backups go down
1660 * for Bonded connections:
1661 * The first up interface should be left on and all others downed.
1663 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1665 struct bonding *bond = bond_dev->priv;
1666 struct slave *slave, *oldcurrent;
1667 struct sockaddr addr;
1668 int mac_addr_differ;
1669 DECLARE_MAC_BUF(mac);
1671 /* slave is not a slave or master is not master of this slave */
1672 if (!(slave_dev->flags & IFF_SLAVE) ||
1673 (slave_dev->master != bond_dev)) {
1674 printk(KERN_ERR DRV_NAME
1675 ": %s: Error: cannot release %s.\n",
1676 bond_dev->name, slave_dev->name);
1677 return -EINVAL;
1680 write_lock_bh(&bond->lock);
1682 slave = bond_get_slave_by_dev(bond, slave_dev);
1683 if (!slave) {
1684 /* not a slave of this bond */
1685 printk(KERN_INFO DRV_NAME
1686 ": %s: %s not enslaved\n",
1687 bond_dev->name, slave_dev->name);
1688 write_unlock_bh(&bond->lock);
1689 return -EINVAL;
1692 mac_addr_differ = memcmp(bond_dev->dev_addr,
1693 slave->perm_hwaddr,
1694 ETH_ALEN);
1695 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1696 printk(KERN_WARNING DRV_NAME
1697 ": %s: Warning: the permanent HWaddr of %s - "
1698 "%s - is still in use by %s. "
1699 "Set the HWaddr of %s to a different address "
1700 "to avoid conflicts.\n",
1701 bond_dev->name,
1702 slave_dev->name,
1703 print_mac(mac, slave->perm_hwaddr),
1704 bond_dev->name,
1705 slave_dev->name);
1708 /* Inform AD package of unbinding of slave. */
1709 if (bond->params.mode == BOND_MODE_8023AD) {
1710 /* must be called before the slave is
1711 * detached from the list
1713 bond_3ad_unbind_slave(slave);
1716 printk(KERN_INFO DRV_NAME
1717 ": %s: releasing %s interface %s\n",
1718 bond_dev->name,
1719 (slave->state == BOND_STATE_ACTIVE)
1720 ? "active" : "backup",
1721 slave_dev->name);
1723 oldcurrent = bond->curr_active_slave;
1725 bond->current_arp_slave = NULL;
1727 /* release the slave from its bond */
1728 bond_detach_slave(bond, slave);
1730 bond_compute_features(bond);
1732 if (bond->primary_slave == slave) {
1733 bond->primary_slave = NULL;
1736 if (oldcurrent == slave) {
1737 bond_change_active_slave(bond, NULL);
1740 if ((bond->params.mode == BOND_MODE_TLB) ||
1741 (bond->params.mode == BOND_MODE_ALB)) {
1742 /* Must be called only after the slave has been
1743 * detached from the list and the curr_active_slave
1744 * has been cleared (if our_slave == old_current),
1745 * but before a new active slave is selected.
1747 bond_alb_deinit_slave(bond, slave);
1750 if (oldcurrent == slave) {
1752 * Note that we hold RTNL over this sequence, so there
1753 * is no concern that another slave add/remove event
1754 * will interfere.
1756 write_unlock_bh(&bond->lock);
1757 read_lock(&bond->lock);
1758 write_lock_bh(&bond->curr_slave_lock);
1760 bond_select_active_slave(bond);
1762 write_unlock_bh(&bond->curr_slave_lock);
1763 read_unlock(&bond->lock);
1764 write_lock_bh(&bond->lock);
1767 if (bond->slave_cnt == 0) {
1768 bond_set_carrier(bond);
1770 /* if the last slave was removed, zero the mac address
1771 * of the master so it will be set by the application
1772 * to the mac address of the first slave
1774 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1776 if (list_empty(&bond->vlan_list)) {
1777 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1778 } else {
1779 printk(KERN_WARNING DRV_NAME
1780 ": %s: Warning: clearing HW address of %s while it "
1781 "still has VLANs.\n",
1782 bond_dev->name, bond_dev->name);
1783 printk(KERN_WARNING DRV_NAME
1784 ": %s: When re-adding slaves, make sure the bond's "
1785 "HW address matches its VLANs'.\n",
1786 bond_dev->name);
1788 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1789 !bond_has_challenged_slaves(bond)) {
1790 printk(KERN_INFO DRV_NAME
1791 ": %s: last VLAN challenged slave %s "
1792 "left bond %s. VLAN blocking is removed\n",
1793 bond_dev->name, slave_dev->name, bond_dev->name);
1794 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1797 write_unlock_bh(&bond->lock);
1799 /* must do this from outside any spinlocks */
1800 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1802 bond_del_vlans_from_slave(bond, slave_dev);
1804 /* If the mode USES_PRIMARY, then we should only remove its
1805 * promisc and mc settings if it was the curr_active_slave, but that was
1806 * already taken care of above when we detached the slave
1808 if (!USES_PRIMARY(bond->params.mode)) {
1809 /* unset promiscuity level from slave */
1810 if (bond_dev->flags & IFF_PROMISC) {
1811 dev_set_promiscuity(slave_dev, -1);
1814 /* unset allmulti level from slave */
1815 if (bond_dev->flags & IFF_ALLMULTI) {
1816 dev_set_allmulti(slave_dev, -1);
1819 /* flush master's mc_list from slave */
1820 bond_mc_list_flush(bond_dev, slave_dev);
1823 netdev_set_master(slave_dev, NULL);
1825 /* close slave before restoring its mac address */
1826 dev_close(slave_dev);
1828 if (!bond->params.fail_over_mac) {
1829 /* restore original ("permanent") mac address */
1830 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1831 addr.sa_family = slave_dev->type;
1832 dev_set_mac_address(slave_dev, &addr);
1835 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1836 IFF_SLAVE_INACTIVE | IFF_BONDING |
1837 IFF_SLAVE_NEEDARP);
1839 kfree(slave);
1841 return 0; /* deletion OK */
1845 * Destroy a bonding device.
1846 * Must be under rtnl_lock when this function is called.
1848 void bond_destroy(struct bonding *bond)
1850 unregister_netdevice(bond->dev);
1851 bond_deinit(bond->dev);
1852 bond_destroy_sysfs_entry(bond);
1856 * First release a slave and than destroy the bond if no more slaves iare left.
1857 * Must be under rtnl_lock when this function is called.
1859 int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1861 struct bonding *bond = bond_dev->priv;
1862 int ret;
1864 ret = bond_release(bond_dev, slave_dev);
1865 if ((ret == 0) && (bond->slave_cnt == 0)) {
1866 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1867 bond_dev->name, bond_dev->name);
1868 bond_destroy(bond);
1870 return ret;
1874 * This function releases all slaves.
1876 static int bond_release_all(struct net_device *bond_dev)
1878 struct bonding *bond = bond_dev->priv;
1879 struct slave *slave;
1880 struct net_device *slave_dev;
1881 struct sockaddr addr;
1883 write_lock_bh(&bond->lock);
1885 netif_carrier_off(bond_dev);
1887 if (bond->slave_cnt == 0) {
1888 goto out;
1891 bond->current_arp_slave = NULL;
1892 bond->primary_slave = NULL;
1893 bond_change_active_slave(bond, NULL);
1895 while ((slave = bond->first_slave) != NULL) {
1896 /* Inform AD package of unbinding of slave
1897 * before slave is detached from the list.
1899 if (bond->params.mode == BOND_MODE_8023AD) {
1900 bond_3ad_unbind_slave(slave);
1903 slave_dev = slave->dev;
1904 bond_detach_slave(bond, slave);
1906 if ((bond->params.mode == BOND_MODE_TLB) ||
1907 (bond->params.mode == BOND_MODE_ALB)) {
1908 /* must be called only after the slave
1909 * has been detached from the list
1911 bond_alb_deinit_slave(bond, slave);
1914 bond_compute_features(bond);
1916 /* now that the slave is detached, unlock and perform
1917 * all the undo steps that should not be called from
1918 * within a lock.
1920 write_unlock_bh(&bond->lock);
1922 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1923 bond_del_vlans_from_slave(bond, slave_dev);
1925 /* If the mode USES_PRIMARY, then we should only remove its
1926 * promisc and mc settings if it was the curr_active_slave, but that was
1927 * already taken care of above when we detached the slave
1929 if (!USES_PRIMARY(bond->params.mode)) {
1930 /* unset promiscuity level from slave */
1931 if (bond_dev->flags & IFF_PROMISC) {
1932 dev_set_promiscuity(slave_dev, -1);
1935 /* unset allmulti level from slave */
1936 if (bond_dev->flags & IFF_ALLMULTI) {
1937 dev_set_allmulti(slave_dev, -1);
1940 /* flush master's mc_list from slave */
1941 bond_mc_list_flush(bond_dev, slave_dev);
1944 netdev_set_master(slave_dev, NULL);
1946 /* close slave before restoring its mac address */
1947 dev_close(slave_dev);
1949 if (!bond->params.fail_over_mac) {
1950 /* restore original ("permanent") mac address*/
1951 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1952 addr.sa_family = slave_dev->type;
1953 dev_set_mac_address(slave_dev, &addr);
1956 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1957 IFF_SLAVE_INACTIVE);
1959 kfree(slave);
1961 /* re-acquire the lock before getting the next slave */
1962 write_lock_bh(&bond->lock);
1965 /* zero the mac address of the master so it will be
1966 * set by the application to the mac address of the
1967 * first slave
1969 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1971 if (list_empty(&bond->vlan_list)) {
1972 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1973 } else {
1974 printk(KERN_WARNING DRV_NAME
1975 ": %s: Warning: clearing HW address of %s while it "
1976 "still has VLANs.\n",
1977 bond_dev->name, bond_dev->name);
1978 printk(KERN_WARNING DRV_NAME
1979 ": %s: When re-adding slaves, make sure the bond's "
1980 "HW address matches its VLANs'.\n",
1981 bond_dev->name);
1984 printk(KERN_INFO DRV_NAME
1985 ": %s: released all slaves\n",
1986 bond_dev->name);
1988 out:
1989 write_unlock_bh(&bond->lock);
1991 return 0;
1995 * This function changes the active slave to slave <slave_dev>.
1996 * It returns -EINVAL in the following cases.
1997 * - <slave_dev> is not found in the list.
1998 * - There is not active slave now.
1999 * - <slave_dev> is already active.
2000 * - The link state of <slave_dev> is not BOND_LINK_UP.
2001 * - <slave_dev> is not running.
2002 * In these cases, this fuction does nothing.
2003 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2005 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2007 struct bonding *bond = bond_dev->priv;
2008 struct slave *old_active = NULL;
2009 struct slave *new_active = NULL;
2010 int res = 0;
2012 if (!USES_PRIMARY(bond->params.mode)) {
2013 return -EINVAL;
2016 /* Verify that master_dev is indeed the master of slave_dev */
2017 if (!(slave_dev->flags & IFF_SLAVE) ||
2018 (slave_dev->master != bond_dev)) {
2019 return -EINVAL;
2022 read_lock(&bond->lock);
2024 read_lock(&bond->curr_slave_lock);
2025 old_active = bond->curr_active_slave;
2026 read_unlock(&bond->curr_slave_lock);
2028 new_active = bond_get_slave_by_dev(bond, slave_dev);
2031 * Changing to the current active: do nothing; return success.
2033 if (new_active && (new_active == old_active)) {
2034 read_unlock(&bond->lock);
2035 return 0;
2038 if ((new_active) &&
2039 (old_active) &&
2040 (new_active->link == BOND_LINK_UP) &&
2041 IS_UP(new_active->dev)) {
2042 write_lock_bh(&bond->curr_slave_lock);
2043 bond_change_active_slave(bond, new_active);
2044 write_unlock_bh(&bond->curr_slave_lock);
2045 } else {
2046 res = -EINVAL;
2049 read_unlock(&bond->lock);
2051 return res;
2054 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2056 struct bonding *bond = bond_dev->priv;
2058 info->bond_mode = bond->params.mode;
2059 info->miimon = bond->params.miimon;
2061 read_lock(&bond->lock);
2062 info->num_slaves = bond->slave_cnt;
2063 read_unlock(&bond->lock);
2065 return 0;
2068 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2070 struct bonding *bond = bond_dev->priv;
2071 struct slave *slave;
2072 int i, found = 0;
2074 if (info->slave_id < 0) {
2075 return -ENODEV;
2078 read_lock(&bond->lock);
2080 bond_for_each_slave(bond, slave, i) {
2081 if (i == (int)info->slave_id) {
2082 found = 1;
2083 break;
2087 read_unlock(&bond->lock);
2089 if (found) {
2090 strcpy(info->slave_name, slave->dev->name);
2091 info->link = slave->link;
2092 info->state = slave->state;
2093 info->link_failure_count = slave->link_failure_count;
2094 } else {
2095 return -ENODEV;
2098 return 0;
2101 /*-------------------------------- Monitoring -------------------------------*/
2104 * if !have_locks, return nonzero if a failover is necessary. if
2105 * have_locks, do whatever failover activities are needed.
2107 * This is to separate the inspection and failover steps for locking
2108 * purposes; failover requires rtnl, but acquiring it for every
2109 * inspection is undesirable, so a wrapper first does inspection, and
2110 * the acquires the necessary locks and calls again to perform
2111 * failover if needed. Since all locks are dropped, a complete
2112 * restart is needed between calls.
2114 static int __bond_mii_monitor(struct bonding *bond, int have_locks)
2116 struct slave *slave, *oldcurrent;
2117 int do_failover = 0;
2118 int i;
2120 if (bond->slave_cnt == 0)
2121 goto out;
2123 /* we will try to read the link status of each of our slaves, and
2124 * set their IFF_RUNNING flag appropriately. For each slave not
2125 * supporting MII status, we won't do anything so that a user-space
2126 * program could monitor the link itself if needed.
2129 if (bond->send_grat_arp) {
2130 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2131 &bond->curr_active_slave->dev->state))
2132 dprintk("Needs to send gratuitous arp but not yet\n");
2133 else {
2134 dprintk("sending delayed gratuitous arp on on %s\n",
2135 bond->curr_active_slave->dev->name);
2136 bond_send_gratuitous_arp(bond);
2137 bond->send_grat_arp = 0;
2140 read_lock(&bond->curr_slave_lock);
2141 oldcurrent = bond->curr_active_slave;
2142 read_unlock(&bond->curr_slave_lock);
2144 bond_for_each_slave(bond, slave, i) {
2145 struct net_device *slave_dev = slave->dev;
2146 int link_state;
2147 u16 old_speed = slave->speed;
2148 u8 old_duplex = slave->duplex;
2150 link_state = bond_check_dev_link(bond, slave_dev, 0);
2152 switch (slave->link) {
2153 case BOND_LINK_UP: /* the link was up */
2154 if (link_state == BMSR_LSTATUS) {
2155 if (!oldcurrent) {
2156 if (!have_locks)
2157 return 1;
2158 do_failover = 1;
2160 break;
2161 } else { /* link going down */
2162 slave->link = BOND_LINK_FAIL;
2163 slave->delay = bond->params.downdelay;
2165 if (slave->link_failure_count < UINT_MAX) {
2166 slave->link_failure_count++;
2169 if (bond->params.downdelay) {
2170 printk(KERN_INFO DRV_NAME
2171 ": %s: link status down for %s "
2172 "interface %s, disabling it in "
2173 "%d ms.\n",
2174 bond->dev->name,
2175 IS_UP(slave_dev)
2176 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2177 ? ((slave == oldcurrent)
2178 ? "active " : "backup ")
2179 : "")
2180 : "idle ",
2181 slave_dev->name,
2182 bond->params.downdelay * bond->params.miimon);
2185 /* no break ! fall through the BOND_LINK_FAIL test to
2186 ensure proper action to be taken
2188 case BOND_LINK_FAIL: /* the link has just gone down */
2189 if (link_state != BMSR_LSTATUS) {
2190 /* link stays down */
2191 if (slave->delay <= 0) {
2192 if (!have_locks)
2193 return 1;
2195 /* link down for too long time */
2196 slave->link = BOND_LINK_DOWN;
2198 /* in active/backup mode, we must
2199 * completely disable this interface
2201 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2202 (bond->params.mode == BOND_MODE_8023AD)) {
2203 bond_set_slave_inactive_flags(slave);
2206 printk(KERN_INFO DRV_NAME
2207 ": %s: link status definitely "
2208 "down for interface %s, "
2209 "disabling it\n",
2210 bond->dev->name,
2211 slave_dev->name);
2213 /* notify ad that the link status has changed */
2214 if (bond->params.mode == BOND_MODE_8023AD) {
2215 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2218 if ((bond->params.mode == BOND_MODE_TLB) ||
2219 (bond->params.mode == BOND_MODE_ALB)) {
2220 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2223 if (slave == oldcurrent) {
2224 do_failover = 1;
2226 } else {
2227 slave->delay--;
2229 } else {
2230 /* link up again */
2231 slave->link = BOND_LINK_UP;
2232 slave->jiffies = jiffies;
2233 printk(KERN_INFO DRV_NAME
2234 ": %s: link status up again after %d "
2235 "ms for interface %s.\n",
2236 bond->dev->name,
2237 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2238 slave_dev->name);
2240 break;
2241 case BOND_LINK_DOWN: /* the link was down */
2242 if (link_state != BMSR_LSTATUS) {
2243 /* the link stays down, nothing more to do */
2244 break;
2245 } else { /* link going up */
2246 slave->link = BOND_LINK_BACK;
2247 slave->delay = bond->params.updelay;
2249 if (bond->params.updelay) {
2250 /* if updelay == 0, no need to
2251 advertise about a 0 ms delay */
2252 printk(KERN_INFO DRV_NAME
2253 ": %s: link status up for "
2254 "interface %s, enabling it "
2255 "in %d ms.\n",
2256 bond->dev->name,
2257 slave_dev->name,
2258 bond->params.updelay * bond->params.miimon);
2261 /* no break ! fall through the BOND_LINK_BACK state in
2262 case there's something to do.
2264 case BOND_LINK_BACK: /* the link has just come back */
2265 if (link_state != BMSR_LSTATUS) {
2266 /* link down again */
2267 slave->link = BOND_LINK_DOWN;
2269 printk(KERN_INFO DRV_NAME
2270 ": %s: link status down again after %d "
2271 "ms for interface %s.\n",
2272 bond->dev->name,
2273 (bond->params.updelay - slave->delay) * bond->params.miimon,
2274 slave_dev->name);
2275 } else {
2276 /* link stays up */
2277 if (slave->delay == 0) {
2278 if (!have_locks)
2279 return 1;
2281 /* now the link has been up for long time enough */
2282 slave->link = BOND_LINK_UP;
2283 slave->jiffies = jiffies;
2285 if (bond->params.mode == BOND_MODE_8023AD) {
2286 /* prevent it from being the active one */
2287 slave->state = BOND_STATE_BACKUP;
2288 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2289 /* make it immediately active */
2290 slave->state = BOND_STATE_ACTIVE;
2291 } else if (slave != bond->primary_slave) {
2292 /* prevent it from being the active one */
2293 slave->state = BOND_STATE_BACKUP;
2296 printk(KERN_INFO DRV_NAME
2297 ": %s: link status definitely "
2298 "up for interface %s.\n",
2299 bond->dev->name,
2300 slave_dev->name);
2302 /* notify ad that the link status has changed */
2303 if (bond->params.mode == BOND_MODE_8023AD) {
2304 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2307 if ((bond->params.mode == BOND_MODE_TLB) ||
2308 (bond->params.mode == BOND_MODE_ALB)) {
2309 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2312 if ((!oldcurrent) ||
2313 (slave == bond->primary_slave)) {
2314 do_failover = 1;
2316 } else {
2317 slave->delay--;
2320 break;
2321 default:
2322 /* Should not happen */
2323 printk(KERN_ERR DRV_NAME
2324 ": %s: Error: %s Illegal value (link=%d)\n",
2325 bond->dev->name,
2326 slave->dev->name,
2327 slave->link);
2328 goto out;
2329 } /* end of switch (slave->link) */
2331 bond_update_speed_duplex(slave);
2333 if (bond->params.mode == BOND_MODE_8023AD) {
2334 if (old_speed != slave->speed) {
2335 bond_3ad_adapter_speed_changed(slave);
2338 if (old_duplex != slave->duplex) {
2339 bond_3ad_adapter_duplex_changed(slave);
2343 } /* end of for */
2345 if (do_failover) {
2346 ASSERT_RTNL();
2348 write_lock_bh(&bond->curr_slave_lock);
2350 bond_select_active_slave(bond);
2352 write_unlock_bh(&bond->curr_slave_lock);
2354 } else
2355 bond_set_carrier(bond);
2357 out:
2358 return 0;
2362 * bond_mii_monitor
2364 * Really a wrapper that splits the mii monitor into two phases: an
2365 * inspection, then (if inspection indicates something needs to be
2366 * done) an acquisition of appropriate locks followed by another pass
2367 * to implement whatever link state changes are indicated.
2369 void bond_mii_monitor(struct work_struct *work)
2371 struct bonding *bond = container_of(work, struct bonding,
2372 mii_work.work);
2373 unsigned long delay;
2375 read_lock(&bond->lock);
2376 if (bond->kill_timers) {
2377 read_unlock(&bond->lock);
2378 return;
2380 if (__bond_mii_monitor(bond, 0)) {
2381 read_unlock(&bond->lock);
2382 rtnl_lock();
2383 read_lock(&bond->lock);
2384 __bond_mii_monitor(bond, 1);
2385 rtnl_unlock();
2388 delay = ((bond->params.miimon * HZ) / 1000) ? : 1;
2389 read_unlock(&bond->lock);
2390 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2393 static __be32 bond_glean_dev_ip(struct net_device *dev)
2395 struct in_device *idev;
2396 struct in_ifaddr *ifa;
2397 __be32 addr = 0;
2399 if (!dev)
2400 return 0;
2402 rcu_read_lock();
2403 idev = __in_dev_get_rcu(dev);
2404 if (!idev)
2405 goto out;
2407 ifa = idev->ifa_list;
2408 if (!ifa)
2409 goto out;
2411 addr = ifa->ifa_local;
2412 out:
2413 rcu_read_unlock();
2414 return addr;
2417 static int bond_has_ip(struct bonding *bond)
2419 struct vlan_entry *vlan, *vlan_next;
2421 if (bond->master_ip)
2422 return 1;
2424 if (list_empty(&bond->vlan_list))
2425 return 0;
2427 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2428 vlan_list) {
2429 if (vlan->vlan_ip)
2430 return 1;
2433 return 0;
2436 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2438 struct vlan_entry *vlan, *vlan_next;
2440 if (ip == bond->master_ip)
2441 return 1;
2443 if (list_empty(&bond->vlan_list))
2444 return 0;
2446 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2447 vlan_list) {
2448 if (ip == vlan->vlan_ip)
2449 return 1;
2452 return 0;
2456 * We go to the (large) trouble of VLAN tagging ARP frames because
2457 * switches in VLAN mode (especially if ports are configured as
2458 * "native" to a VLAN) might not pass non-tagged frames.
2460 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2462 struct sk_buff *skb;
2464 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2465 slave_dev->name, dest_ip, src_ip, vlan_id);
2467 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2468 NULL, slave_dev->dev_addr, NULL);
2470 if (!skb) {
2471 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2472 return;
2474 if (vlan_id) {
2475 skb = vlan_put_tag(skb, vlan_id);
2476 if (!skb) {
2477 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2478 return;
2481 arp_xmit(skb);
2485 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2487 int i, vlan_id, rv;
2488 __be32 *targets = bond->params.arp_targets;
2489 struct vlan_entry *vlan, *vlan_next;
2490 struct net_device *vlan_dev;
2491 struct flowi fl;
2492 struct rtable *rt;
2494 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2495 if (!targets[i])
2496 continue;
2497 dprintk("basa: target %x\n", targets[i]);
2498 if (list_empty(&bond->vlan_list)) {
2499 dprintk("basa: empty vlan: arp_send\n");
2500 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2501 bond->master_ip, 0);
2502 continue;
2506 * If VLANs are configured, we do a route lookup to
2507 * determine which VLAN interface would be used, so we
2508 * can tag the ARP with the proper VLAN tag.
2510 memset(&fl, 0, sizeof(fl));
2511 fl.fl4_dst = targets[i];
2512 fl.fl4_tos = RTO_ONLINK;
2514 rv = ip_route_output_key(&rt, &fl);
2515 if (rv) {
2516 if (net_ratelimit()) {
2517 printk(KERN_WARNING DRV_NAME
2518 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2519 bond->dev->name, NIPQUAD(fl.fl4_dst));
2521 continue;
2525 * This target is not on a VLAN
2527 if (rt->u.dst.dev == bond->dev) {
2528 ip_rt_put(rt);
2529 dprintk("basa: rtdev == bond->dev: arp_send\n");
2530 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2531 bond->master_ip, 0);
2532 continue;
2535 vlan_id = 0;
2536 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2537 vlan_list) {
2538 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2539 if (vlan_dev == rt->u.dst.dev) {
2540 vlan_id = vlan->vlan_id;
2541 dprintk("basa: vlan match on %s %d\n",
2542 vlan_dev->name, vlan_id);
2543 break;
2547 if (vlan_id) {
2548 ip_rt_put(rt);
2549 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2550 vlan->vlan_ip, vlan_id);
2551 continue;
2554 if (net_ratelimit()) {
2555 printk(KERN_WARNING DRV_NAME
2556 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2557 bond->dev->name, NIPQUAD(fl.fl4_dst),
2558 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2560 ip_rt_put(rt);
2565 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2566 * for each VLAN above us.
2568 static void bond_send_gratuitous_arp(struct bonding *bond)
2570 struct slave *slave = bond->curr_active_slave;
2571 struct vlan_entry *vlan;
2572 struct net_device *vlan_dev;
2574 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2575 slave ? slave->dev->name : "NULL");
2576 if (!slave)
2577 return;
2579 if (bond->master_ip) {
2580 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2581 bond->master_ip, 0);
2584 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2585 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2586 if (vlan->vlan_ip) {
2587 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2588 vlan->vlan_ip, vlan->vlan_id);
2593 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2595 int i;
2596 __be32 *targets = bond->params.arp_targets;
2598 targets = bond->params.arp_targets;
2599 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2600 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2601 "%u.%u.%u.%u bhti(tip) %d\n",
2602 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2603 bond_has_this_ip(bond, tip));
2604 if (sip == targets[i]) {
2605 if (bond_has_this_ip(bond, tip))
2606 slave->last_arp_rx = jiffies;
2607 return;
2612 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2614 struct arphdr *arp;
2615 struct slave *slave;
2616 struct bonding *bond;
2617 unsigned char *arp_ptr;
2618 __be32 sip, tip;
2620 if (dev->nd_net != &init_net)
2621 goto out;
2623 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2624 goto out;
2626 bond = dev->priv;
2627 read_lock(&bond->lock);
2629 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2630 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2631 orig_dev ? orig_dev->name : "NULL");
2633 slave = bond_get_slave_by_dev(bond, orig_dev);
2634 if (!slave || !slave_do_arp_validate(bond, slave))
2635 goto out_unlock;
2637 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2638 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2639 (2 * dev->addr_len) +
2640 (2 * sizeof(u32)))))
2641 goto out_unlock;
2643 arp = arp_hdr(skb);
2644 if (arp->ar_hln != dev->addr_len ||
2645 skb->pkt_type == PACKET_OTHERHOST ||
2646 skb->pkt_type == PACKET_LOOPBACK ||
2647 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2648 arp->ar_pro != htons(ETH_P_IP) ||
2649 arp->ar_pln != 4)
2650 goto out_unlock;
2652 arp_ptr = (unsigned char *)(arp + 1);
2653 arp_ptr += dev->addr_len;
2654 memcpy(&sip, arp_ptr, 4);
2655 arp_ptr += 4 + dev->addr_len;
2656 memcpy(&tip, arp_ptr, 4);
2658 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2659 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2660 slave->state, bond->params.arp_validate,
2661 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2664 * Backup slaves won't see the ARP reply, but do come through
2665 * here for each ARP probe (so we swap the sip/tip to validate
2666 * the probe). In a "redundant switch, common router" type of
2667 * configuration, the ARP probe will (hopefully) travel from
2668 * the active, through one switch, the router, then the other
2669 * switch before reaching the backup.
2671 if (slave->state == BOND_STATE_ACTIVE)
2672 bond_validate_arp(bond, slave, sip, tip);
2673 else
2674 bond_validate_arp(bond, slave, tip, sip);
2676 out_unlock:
2677 read_unlock(&bond->lock);
2678 out:
2679 dev_kfree_skb(skb);
2680 return NET_RX_SUCCESS;
2684 * this function is called regularly to monitor each slave's link
2685 * ensuring that traffic is being sent and received when arp monitoring
2686 * is used in load-balancing mode. if the adapter has been dormant, then an
2687 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2688 * arp monitoring in active backup mode.
2690 void bond_loadbalance_arp_mon(struct work_struct *work)
2692 struct bonding *bond = container_of(work, struct bonding,
2693 arp_work.work);
2694 struct slave *slave, *oldcurrent;
2695 int do_failover = 0;
2696 int delta_in_ticks;
2697 int i;
2699 read_lock(&bond->lock);
2701 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2703 if (bond->kill_timers) {
2704 goto out;
2707 if (bond->slave_cnt == 0) {
2708 goto re_arm;
2711 read_lock(&bond->curr_slave_lock);
2712 oldcurrent = bond->curr_active_slave;
2713 read_unlock(&bond->curr_slave_lock);
2715 /* see if any of the previous devices are up now (i.e. they have
2716 * xmt and rcv traffic). the curr_active_slave does not come into
2717 * the picture unless it is null. also, slave->jiffies is not needed
2718 * here because we send an arp on each slave and give a slave as
2719 * long as it needs to get the tx/rx within the delta.
2720 * TODO: what about up/down delay in arp mode? it wasn't here before
2721 * so it can wait
2723 bond_for_each_slave(bond, slave, i) {
2724 if (slave->link != BOND_LINK_UP) {
2725 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2726 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2728 slave->link = BOND_LINK_UP;
2729 slave->state = BOND_STATE_ACTIVE;
2731 /* primary_slave has no meaning in round-robin
2732 * mode. the window of a slave being up and
2733 * curr_active_slave being null after enslaving
2734 * is closed.
2736 if (!oldcurrent) {
2737 printk(KERN_INFO DRV_NAME
2738 ": %s: link status definitely "
2739 "up for interface %s, ",
2740 bond->dev->name,
2741 slave->dev->name);
2742 do_failover = 1;
2743 } else {
2744 printk(KERN_INFO DRV_NAME
2745 ": %s: interface %s is now up\n",
2746 bond->dev->name,
2747 slave->dev->name);
2750 } else {
2751 /* slave->link == BOND_LINK_UP */
2753 /* not all switches will respond to an arp request
2754 * when the source ip is 0, so don't take the link down
2755 * if we don't know our ip yet
2757 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2758 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2759 bond_has_ip(bond))) {
2761 slave->link = BOND_LINK_DOWN;
2762 slave->state = BOND_STATE_BACKUP;
2764 if (slave->link_failure_count < UINT_MAX) {
2765 slave->link_failure_count++;
2768 printk(KERN_INFO DRV_NAME
2769 ": %s: interface %s is now down.\n",
2770 bond->dev->name,
2771 slave->dev->name);
2773 if (slave == oldcurrent) {
2774 do_failover = 1;
2779 /* note: if switch is in round-robin mode, all links
2780 * must tx arp to ensure all links rx an arp - otherwise
2781 * links may oscillate or not come up at all; if switch is
2782 * in something like xor mode, there is nothing we can
2783 * do - all replies will be rx'ed on same link causing slaves
2784 * to be unstable during low/no traffic periods
2786 if (IS_UP(slave->dev)) {
2787 bond_arp_send_all(bond, slave);
2791 if (do_failover) {
2792 rtnl_lock();
2793 write_lock_bh(&bond->curr_slave_lock);
2795 bond_select_active_slave(bond);
2797 write_unlock_bh(&bond->curr_slave_lock);
2798 rtnl_unlock();
2802 re_arm:
2803 if (bond->params.arp_interval)
2804 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2805 out:
2806 read_unlock(&bond->lock);
2810 * When using arp monitoring in active-backup mode, this function is
2811 * called to determine if any backup slaves have went down or a new
2812 * current slave needs to be found.
2813 * The backup slaves never generate traffic, they are considered up by merely
2814 * receiving traffic. If the current slave goes down, each backup slave will
2815 * be given the opportunity to tx/rx an arp before being taken down - this
2816 * prevents all slaves from being taken down due to the current slave not
2817 * sending any traffic for the backups to receive. The arps are not necessarily
2818 * necessary, any tx and rx traffic will keep the current slave up. While any
2819 * rx traffic will keep the backup slaves up, the current slave is responsible
2820 * for generating traffic to keep them up regardless of any other traffic they
2821 * may have received.
2822 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2824 void bond_activebackup_arp_mon(struct work_struct *work)
2826 struct bonding *bond = container_of(work, struct bonding,
2827 arp_work.work);
2828 struct slave *slave;
2829 int delta_in_ticks;
2830 int i;
2832 read_lock(&bond->lock);
2834 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2836 if (bond->kill_timers) {
2837 goto out;
2840 if (bond->slave_cnt == 0) {
2841 goto re_arm;
2844 /* determine if any slave has come up or any backup slave has
2845 * gone down
2846 * TODO: what about up/down delay in arp mode? it wasn't here before
2847 * so it can wait
2849 bond_for_each_slave(bond, slave, i) {
2850 if (slave->link != BOND_LINK_UP) {
2851 if ((jiffies - slave_last_rx(bond, slave)) <=
2852 delta_in_ticks) {
2854 slave->link = BOND_LINK_UP;
2856 rtnl_lock();
2858 write_lock_bh(&bond->curr_slave_lock);
2860 if ((!bond->curr_active_slave) &&
2861 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2862 bond_change_active_slave(bond, slave);
2863 bond->current_arp_slave = NULL;
2864 } else if (bond->curr_active_slave != slave) {
2865 /* this slave has just come up but we
2866 * already have a current slave; this
2867 * can also happen if bond_enslave adds
2868 * a new slave that is up while we are
2869 * searching for a new slave
2871 bond_set_slave_inactive_flags(slave);
2872 bond->current_arp_slave = NULL;
2875 bond_set_carrier(bond);
2877 if (slave == bond->curr_active_slave) {
2878 printk(KERN_INFO DRV_NAME
2879 ": %s: %s is up and now the "
2880 "active interface\n",
2881 bond->dev->name,
2882 slave->dev->name);
2883 netif_carrier_on(bond->dev);
2884 } else {
2885 printk(KERN_INFO DRV_NAME
2886 ": %s: backup interface %s is "
2887 "now up\n",
2888 bond->dev->name,
2889 slave->dev->name);
2892 write_unlock_bh(&bond->curr_slave_lock);
2893 rtnl_unlock();
2895 } else {
2896 read_lock(&bond->curr_slave_lock);
2898 if ((slave != bond->curr_active_slave) &&
2899 (!bond->current_arp_slave) &&
2900 (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
2901 bond_has_ip(bond))) {
2902 /* a backup slave has gone down; three times
2903 * the delta allows the current slave to be
2904 * taken out before the backup slave.
2905 * note: a non-null current_arp_slave indicates
2906 * the curr_active_slave went down and we are
2907 * searching for a new one; under this
2908 * condition we only take the curr_active_slave
2909 * down - this gives each slave a chance to
2910 * tx/rx traffic before being taken out
2913 read_unlock(&bond->curr_slave_lock);
2915 slave->link = BOND_LINK_DOWN;
2917 if (slave->link_failure_count < UINT_MAX) {
2918 slave->link_failure_count++;
2921 bond_set_slave_inactive_flags(slave);
2923 printk(KERN_INFO DRV_NAME
2924 ": %s: backup interface %s is now down\n",
2925 bond->dev->name,
2926 slave->dev->name);
2927 } else {
2928 read_unlock(&bond->curr_slave_lock);
2933 read_lock(&bond->curr_slave_lock);
2934 slave = bond->curr_active_slave;
2935 read_unlock(&bond->curr_slave_lock);
2937 if (slave) {
2938 /* if we have sent traffic in the past 2*arp_intervals but
2939 * haven't xmit and rx traffic in that time interval, select
2940 * a different slave. slave->jiffies is only updated when
2941 * a slave first becomes the curr_active_slave - not necessarily
2942 * after every arp; this ensures the slave has a full 2*delta
2943 * before being taken out. if a primary is being used, check
2944 * if it is up and needs to take over as the curr_active_slave
2946 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2947 (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
2948 bond_has_ip(bond))) &&
2949 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2951 slave->link = BOND_LINK_DOWN;
2953 if (slave->link_failure_count < UINT_MAX) {
2954 slave->link_failure_count++;
2957 printk(KERN_INFO DRV_NAME
2958 ": %s: link status down for active interface "
2959 "%s, disabling it\n",
2960 bond->dev->name,
2961 slave->dev->name);
2963 rtnl_lock();
2964 write_lock_bh(&bond->curr_slave_lock);
2966 bond_select_active_slave(bond);
2967 slave = bond->curr_active_slave;
2969 write_unlock_bh(&bond->curr_slave_lock);
2971 rtnl_unlock();
2973 bond->current_arp_slave = slave;
2975 if (slave) {
2976 slave->jiffies = jiffies;
2978 } else if ((bond->primary_slave) &&
2979 (bond->primary_slave != slave) &&
2980 (bond->primary_slave->link == BOND_LINK_UP)) {
2981 /* at this point, slave is the curr_active_slave */
2982 printk(KERN_INFO DRV_NAME
2983 ": %s: changing from interface %s to primary "
2984 "interface %s\n",
2985 bond->dev->name,
2986 slave->dev->name,
2987 bond->primary_slave->dev->name);
2989 /* primary is up so switch to it */
2990 rtnl_lock();
2991 write_lock_bh(&bond->curr_slave_lock);
2992 bond_change_active_slave(bond, bond->primary_slave);
2993 write_unlock_bh(&bond->curr_slave_lock);
2995 rtnl_unlock();
2997 slave = bond->primary_slave;
2998 slave->jiffies = jiffies;
2999 } else {
3000 bond->current_arp_slave = NULL;
3003 /* the current slave must tx an arp to ensure backup slaves
3004 * rx traffic
3006 if (slave && bond_has_ip(bond)) {
3007 bond_arp_send_all(bond, slave);
3011 /* if we don't have a curr_active_slave, search for the next available
3012 * backup slave from the current_arp_slave and make it the candidate
3013 * for becoming the curr_active_slave
3015 if (!slave) {
3016 if (!bond->current_arp_slave) {
3017 bond->current_arp_slave = bond->first_slave;
3020 if (bond->current_arp_slave) {
3021 bond_set_slave_inactive_flags(bond->current_arp_slave);
3023 /* search for next candidate */
3024 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3025 if (IS_UP(slave->dev)) {
3026 slave->link = BOND_LINK_BACK;
3027 bond_set_slave_active_flags(slave);
3028 bond_arp_send_all(bond, slave);
3029 slave->jiffies = jiffies;
3030 bond->current_arp_slave = slave;
3031 break;
3034 /* if the link state is up at this point, we
3035 * mark it down - this can happen if we have
3036 * simultaneous link failures and
3037 * reselect_active_interface doesn't make this
3038 * one the current slave so it is still marked
3039 * up when it is actually down
3041 if (slave->link == BOND_LINK_UP) {
3042 slave->link = BOND_LINK_DOWN;
3043 if (slave->link_failure_count < UINT_MAX) {
3044 slave->link_failure_count++;
3047 bond_set_slave_inactive_flags(slave);
3049 printk(KERN_INFO DRV_NAME
3050 ": %s: backup interface %s is "
3051 "now down.\n",
3052 bond->dev->name,
3053 slave->dev->name);
3059 re_arm:
3060 if (bond->params.arp_interval) {
3061 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3063 out:
3064 read_unlock(&bond->lock);
3067 /*------------------------------ proc/seq_file-------------------------------*/
3069 #ifdef CONFIG_PROC_FS
3071 #define SEQ_START_TOKEN ((void *)1)
3073 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3075 struct bonding *bond = seq->private;
3076 loff_t off = 0;
3077 struct slave *slave;
3078 int i;
3080 /* make sure the bond won't be taken away */
3081 read_lock(&dev_base_lock);
3082 read_lock(&bond->lock);
3084 if (*pos == 0) {
3085 return SEQ_START_TOKEN;
3088 bond_for_each_slave(bond, slave, i) {
3089 if (++off == *pos) {
3090 return slave;
3094 return NULL;
3097 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3099 struct bonding *bond = seq->private;
3100 struct slave *slave = v;
3102 ++*pos;
3103 if (v == SEQ_START_TOKEN) {
3104 return bond->first_slave;
3107 slave = slave->next;
3109 return (slave == bond->first_slave) ? NULL : slave;
3112 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3114 struct bonding *bond = seq->private;
3116 read_unlock(&bond->lock);
3117 read_unlock(&dev_base_lock);
3120 static void bond_info_show_master(struct seq_file *seq)
3122 struct bonding *bond = seq->private;
3123 struct slave *curr;
3124 int i;
3125 u32 target;
3127 read_lock(&bond->curr_slave_lock);
3128 curr = bond->curr_active_slave;
3129 read_unlock(&bond->curr_slave_lock);
3131 seq_printf(seq, "Bonding Mode: %s",
3132 bond_mode_name(bond->params.mode));
3134 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3135 bond->params.fail_over_mac)
3136 seq_printf(seq, " (fail_over_mac)");
3138 seq_printf(seq, "\n");
3140 if (bond->params.mode == BOND_MODE_XOR ||
3141 bond->params.mode == BOND_MODE_8023AD) {
3142 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3143 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3144 bond->params.xmit_policy);
3147 if (USES_PRIMARY(bond->params.mode)) {
3148 seq_printf(seq, "Primary Slave: %s\n",
3149 (bond->primary_slave) ?
3150 bond->primary_slave->dev->name : "None");
3152 seq_printf(seq, "Currently Active Slave: %s\n",
3153 (curr) ? curr->dev->name : "None");
3156 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3157 "up" : "down");
3158 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3159 seq_printf(seq, "Up Delay (ms): %d\n",
3160 bond->params.updelay * bond->params.miimon);
3161 seq_printf(seq, "Down Delay (ms): %d\n",
3162 bond->params.downdelay * bond->params.miimon);
3165 /* ARP information */
3166 if(bond->params.arp_interval > 0) {
3167 int printed=0;
3168 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3169 bond->params.arp_interval);
3171 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3173 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3174 if (!bond->params.arp_targets[i])
3175 continue;
3176 if (printed)
3177 seq_printf(seq, ",");
3178 target = ntohl(bond->params.arp_targets[i]);
3179 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3180 printed = 1;
3182 seq_printf(seq, "\n");
3185 if (bond->params.mode == BOND_MODE_8023AD) {
3186 struct ad_info ad_info;
3187 DECLARE_MAC_BUF(mac);
3189 seq_puts(seq, "\n802.3ad info\n");
3190 seq_printf(seq, "LACP rate: %s\n",
3191 (bond->params.lacp_fast) ? "fast" : "slow");
3193 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3194 seq_printf(seq, "bond %s has no active aggregator\n",
3195 bond->dev->name);
3196 } else {
3197 seq_printf(seq, "Active Aggregator Info:\n");
3199 seq_printf(seq, "\tAggregator ID: %d\n",
3200 ad_info.aggregator_id);
3201 seq_printf(seq, "\tNumber of ports: %d\n",
3202 ad_info.ports);
3203 seq_printf(seq, "\tActor Key: %d\n",
3204 ad_info.actor_key);
3205 seq_printf(seq, "\tPartner Key: %d\n",
3206 ad_info.partner_key);
3207 seq_printf(seq, "\tPartner Mac Address: %s\n",
3208 print_mac(mac, ad_info.partner_system));
3213 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3215 struct bonding *bond = seq->private;
3216 DECLARE_MAC_BUF(mac);
3218 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3219 seq_printf(seq, "MII Status: %s\n",
3220 (slave->link == BOND_LINK_UP) ? "up" : "down");
3221 seq_printf(seq, "Link Failure Count: %u\n",
3222 slave->link_failure_count);
3224 seq_printf(seq,
3225 "Permanent HW addr: %s\n",
3226 print_mac(mac, slave->perm_hwaddr));
3228 if (bond->params.mode == BOND_MODE_8023AD) {
3229 const struct aggregator *agg
3230 = SLAVE_AD_INFO(slave).port.aggregator;
3232 if (agg) {
3233 seq_printf(seq, "Aggregator ID: %d\n",
3234 agg->aggregator_identifier);
3235 } else {
3236 seq_puts(seq, "Aggregator ID: N/A\n");
3241 static int bond_info_seq_show(struct seq_file *seq, void *v)
3243 if (v == SEQ_START_TOKEN) {
3244 seq_printf(seq, "%s\n", version);
3245 bond_info_show_master(seq);
3246 } else {
3247 bond_info_show_slave(seq, v);
3250 return 0;
3253 static struct seq_operations bond_info_seq_ops = {
3254 .start = bond_info_seq_start,
3255 .next = bond_info_seq_next,
3256 .stop = bond_info_seq_stop,
3257 .show = bond_info_seq_show,
3260 static int bond_info_open(struct inode *inode, struct file *file)
3262 struct seq_file *seq;
3263 struct proc_dir_entry *proc;
3264 int res;
3266 res = seq_open(file, &bond_info_seq_ops);
3267 if (!res) {
3268 /* recover the pointer buried in proc_dir_entry data */
3269 seq = file->private_data;
3270 proc = PDE(inode);
3271 seq->private = proc->data;
3274 return res;
3277 static const struct file_operations bond_info_fops = {
3278 .owner = THIS_MODULE,
3279 .open = bond_info_open,
3280 .read = seq_read,
3281 .llseek = seq_lseek,
3282 .release = seq_release,
3285 static int bond_create_proc_entry(struct bonding *bond)
3287 struct net_device *bond_dev = bond->dev;
3289 if (bond_proc_dir) {
3290 bond->proc_entry = create_proc_entry(bond_dev->name,
3291 S_IRUGO,
3292 bond_proc_dir);
3293 if (bond->proc_entry == NULL) {
3294 printk(KERN_WARNING DRV_NAME
3295 ": Warning: Cannot create /proc/net/%s/%s\n",
3296 DRV_NAME, bond_dev->name);
3297 } else {
3298 bond->proc_entry->data = bond;
3299 bond->proc_entry->proc_fops = &bond_info_fops;
3300 bond->proc_entry->owner = THIS_MODULE;
3301 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3305 return 0;
3308 static void bond_remove_proc_entry(struct bonding *bond)
3310 if (bond_proc_dir && bond->proc_entry) {
3311 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3312 memset(bond->proc_file_name, 0, IFNAMSIZ);
3313 bond->proc_entry = NULL;
3317 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3318 * Caller must hold rtnl_lock.
3320 static void bond_create_proc_dir(void)
3322 int len = strlen(DRV_NAME);
3324 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3325 bond_proc_dir = bond_proc_dir->next) {
3326 if ((bond_proc_dir->namelen == len) &&
3327 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3328 break;
3332 if (!bond_proc_dir) {
3333 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3334 if (bond_proc_dir) {
3335 bond_proc_dir->owner = THIS_MODULE;
3336 } else {
3337 printk(KERN_WARNING DRV_NAME
3338 ": Warning: cannot create /proc/net/%s\n",
3339 DRV_NAME);
3344 /* Destroy the bonding directory under /proc/net, if empty.
3345 * Caller must hold rtnl_lock.
3347 static void bond_destroy_proc_dir(void)
3349 struct proc_dir_entry *de;
3351 if (!bond_proc_dir) {
3352 return;
3355 /* verify that the /proc dir is empty */
3356 for (de = bond_proc_dir->subdir; de; de = de->next) {
3357 /* ignore . and .. */
3358 if (*(de->name) != '.') {
3359 break;
3363 if (de) {
3364 if (bond_proc_dir->owner == THIS_MODULE) {
3365 bond_proc_dir->owner = NULL;
3367 } else {
3368 remove_proc_entry(DRV_NAME, init_net.proc_net);
3369 bond_proc_dir = NULL;
3372 #endif /* CONFIG_PROC_FS */
3374 /*-------------------------- netdev event handling --------------------------*/
3377 * Change device name
3379 static int bond_event_changename(struct bonding *bond)
3381 #ifdef CONFIG_PROC_FS
3382 bond_remove_proc_entry(bond);
3383 bond_create_proc_entry(bond);
3384 #endif
3385 down_write(&(bonding_rwsem));
3386 bond_destroy_sysfs_entry(bond);
3387 bond_create_sysfs_entry(bond);
3388 up_write(&(bonding_rwsem));
3389 return NOTIFY_DONE;
3392 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3394 struct bonding *event_bond = bond_dev->priv;
3396 switch (event) {
3397 case NETDEV_CHANGENAME:
3398 return bond_event_changename(event_bond);
3399 case NETDEV_UNREGISTER:
3401 * TODO: remove a bond from the list?
3403 break;
3404 default:
3405 break;
3408 return NOTIFY_DONE;
3411 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3413 struct net_device *bond_dev = slave_dev->master;
3414 struct bonding *bond = bond_dev->priv;
3416 switch (event) {
3417 case NETDEV_UNREGISTER:
3418 if (bond_dev) {
3419 if (bond->setup_by_slave)
3420 bond_release_and_destroy(bond_dev, slave_dev);
3421 else
3422 bond_release(bond_dev, slave_dev);
3424 break;
3425 case NETDEV_CHANGE:
3427 * TODO: is this what we get if somebody
3428 * sets up a hierarchical bond, then rmmod's
3429 * one of the slave bonding devices?
3431 break;
3432 case NETDEV_DOWN:
3434 * ... Or is it this?
3436 break;
3437 case NETDEV_CHANGEMTU:
3439 * TODO: Should slaves be allowed to
3440 * independently alter their MTU? For
3441 * an active-backup bond, slaves need
3442 * not be the same type of device, so
3443 * MTUs may vary. For other modes,
3444 * slaves arguably should have the
3445 * same MTUs. To do this, we'd need to
3446 * take over the slave's change_mtu
3447 * function for the duration of their
3448 * servitude.
3450 break;
3451 case NETDEV_CHANGENAME:
3453 * TODO: handle changing the primary's name
3455 break;
3456 case NETDEV_FEAT_CHANGE:
3457 bond_compute_features(bond);
3458 break;
3459 default:
3460 break;
3463 return NOTIFY_DONE;
3467 * bond_netdev_event: handle netdev notifier chain events.
3469 * This function receives events for the netdev chain. The caller (an
3470 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3471 * locks for us to safely manipulate the slave devices (RTNL lock,
3472 * dev_probe_lock).
3474 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3476 struct net_device *event_dev = (struct net_device *)ptr;
3478 if (event_dev->nd_net != &init_net)
3479 return NOTIFY_DONE;
3481 dprintk("event_dev: %s, event: %lx\n",
3482 (event_dev ? event_dev->name : "None"),
3483 event);
3485 if (!(event_dev->priv_flags & IFF_BONDING))
3486 return NOTIFY_DONE;
3488 if (event_dev->flags & IFF_MASTER) {
3489 dprintk("IFF_MASTER\n");
3490 return bond_master_netdev_event(event, event_dev);
3493 if (event_dev->flags & IFF_SLAVE) {
3494 dprintk("IFF_SLAVE\n");
3495 return bond_slave_netdev_event(event, event_dev);
3498 return NOTIFY_DONE;
3502 * bond_inetaddr_event: handle inetaddr notifier chain events.
3504 * We keep track of device IPs primarily to use as source addresses in
3505 * ARP monitor probes (rather than spewing out broadcasts all the time).
3507 * We track one IP for the main device (if it has one), plus one per VLAN.
3509 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3511 struct in_ifaddr *ifa = ptr;
3512 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3513 struct bonding *bond, *bond_next;
3514 struct vlan_entry *vlan, *vlan_next;
3516 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3517 if (bond->dev == event_dev) {
3518 switch (event) {
3519 case NETDEV_UP:
3520 bond->master_ip = ifa->ifa_local;
3521 return NOTIFY_OK;
3522 case NETDEV_DOWN:
3523 bond->master_ip = bond_glean_dev_ip(bond->dev);
3524 return NOTIFY_OK;
3525 default:
3526 return NOTIFY_DONE;
3530 if (list_empty(&bond->vlan_list))
3531 continue;
3533 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3534 vlan_list) {
3535 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3536 if (vlan_dev == event_dev) {
3537 switch (event) {
3538 case NETDEV_UP:
3539 vlan->vlan_ip = ifa->ifa_local;
3540 return NOTIFY_OK;
3541 case NETDEV_DOWN:
3542 vlan->vlan_ip =
3543 bond_glean_dev_ip(vlan_dev);
3544 return NOTIFY_OK;
3545 default:
3546 return NOTIFY_DONE;
3551 return NOTIFY_DONE;
3554 static struct notifier_block bond_netdev_notifier = {
3555 .notifier_call = bond_netdev_event,
3558 static struct notifier_block bond_inetaddr_notifier = {
3559 .notifier_call = bond_inetaddr_event,
3562 /*-------------------------- Packet type handling ---------------------------*/
3564 /* register to receive lacpdus on a bond */
3565 static void bond_register_lacpdu(struct bonding *bond)
3567 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3569 /* initialize packet type */
3570 pk_type->type = PKT_TYPE_LACPDU;
3571 pk_type->dev = bond->dev;
3572 pk_type->func = bond_3ad_lacpdu_recv;
3574 dev_add_pack(pk_type);
3577 /* unregister to receive lacpdus on a bond */
3578 static void bond_unregister_lacpdu(struct bonding *bond)
3580 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3583 void bond_register_arp(struct bonding *bond)
3585 struct packet_type *pt = &bond->arp_mon_pt;
3587 if (pt->type)
3588 return;
3590 pt->type = htons(ETH_P_ARP);
3591 pt->dev = bond->dev;
3592 pt->func = bond_arp_rcv;
3593 dev_add_pack(pt);
3596 void bond_unregister_arp(struct bonding *bond)
3598 struct packet_type *pt = &bond->arp_mon_pt;
3600 dev_remove_pack(pt);
3601 pt->type = 0;
3604 /*---------------------------- Hashing Policies -----------------------------*/
3607 * Hash for the output device based upon layer 3 and layer 4 data. If
3608 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3609 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3611 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3612 struct net_device *bond_dev, int count)
3614 struct ethhdr *data = (struct ethhdr *)skb->data;
3615 struct iphdr *iph = ip_hdr(skb);
3616 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3617 int layer4_xor = 0;
3619 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3620 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3621 (iph->protocol == IPPROTO_TCP ||
3622 iph->protocol == IPPROTO_UDP)) {
3623 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3625 return (layer4_xor ^
3626 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3630 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3634 * Hash for the output device based upon layer 2 data
3636 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3637 struct net_device *bond_dev, int count)
3639 struct ethhdr *data = (struct ethhdr *)skb->data;
3641 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3644 /*-------------------------- Device entry points ----------------------------*/
3646 static int bond_open(struct net_device *bond_dev)
3648 struct bonding *bond = bond_dev->priv;
3650 bond->kill_timers = 0;
3652 if ((bond->params.mode == BOND_MODE_TLB) ||
3653 (bond->params.mode == BOND_MODE_ALB)) {
3654 /* bond_alb_initialize must be called before the timer
3655 * is started.
3657 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3658 /* something went wrong - fail the open operation */
3659 return -1;
3662 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3663 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3666 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3667 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3668 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3671 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3672 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3673 INIT_DELAYED_WORK(&bond->arp_work,
3674 bond_activebackup_arp_mon);
3675 else
3676 INIT_DELAYED_WORK(&bond->arp_work,
3677 bond_loadbalance_arp_mon);
3679 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3680 if (bond->params.arp_validate)
3681 bond_register_arp(bond);
3684 if (bond->params.mode == BOND_MODE_8023AD) {
3685 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3686 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3687 /* register to receive LACPDUs */
3688 bond_register_lacpdu(bond);
3691 return 0;
3694 static int bond_close(struct net_device *bond_dev)
3696 struct bonding *bond = bond_dev->priv;
3698 if (bond->params.mode == BOND_MODE_8023AD) {
3699 /* Unregister the receive of LACPDUs */
3700 bond_unregister_lacpdu(bond);
3703 if (bond->params.arp_validate)
3704 bond_unregister_arp(bond);
3706 write_lock_bh(&bond->lock);
3709 /* signal timers not to re-arm */
3710 bond->kill_timers = 1;
3712 write_unlock_bh(&bond->lock);
3714 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3715 cancel_delayed_work(&bond->mii_work);
3718 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3719 cancel_delayed_work(&bond->arp_work);
3722 switch (bond->params.mode) {
3723 case BOND_MODE_8023AD:
3724 cancel_delayed_work(&bond->ad_work);
3725 break;
3726 case BOND_MODE_TLB:
3727 case BOND_MODE_ALB:
3728 cancel_delayed_work(&bond->alb_work);
3729 break;
3730 default:
3731 break;
3735 if ((bond->params.mode == BOND_MODE_TLB) ||
3736 (bond->params.mode == BOND_MODE_ALB)) {
3737 /* Must be called only after all
3738 * slaves have been released
3740 bond_alb_deinitialize(bond);
3743 return 0;
3746 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3748 struct bonding *bond = bond_dev->priv;
3749 struct net_device_stats *stats = &(bond->stats), *sstats;
3750 struct slave *slave;
3751 int i;
3753 memset(stats, 0, sizeof(struct net_device_stats));
3755 read_lock_bh(&bond->lock);
3757 bond_for_each_slave(bond, slave, i) {
3758 sstats = slave->dev->get_stats(slave->dev);
3759 stats->rx_packets += sstats->rx_packets;
3760 stats->rx_bytes += sstats->rx_bytes;
3761 stats->rx_errors += sstats->rx_errors;
3762 stats->rx_dropped += sstats->rx_dropped;
3764 stats->tx_packets += sstats->tx_packets;
3765 stats->tx_bytes += sstats->tx_bytes;
3766 stats->tx_errors += sstats->tx_errors;
3767 stats->tx_dropped += sstats->tx_dropped;
3769 stats->multicast += sstats->multicast;
3770 stats->collisions += sstats->collisions;
3772 stats->rx_length_errors += sstats->rx_length_errors;
3773 stats->rx_over_errors += sstats->rx_over_errors;
3774 stats->rx_crc_errors += sstats->rx_crc_errors;
3775 stats->rx_frame_errors += sstats->rx_frame_errors;
3776 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3777 stats->rx_missed_errors += sstats->rx_missed_errors;
3779 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3780 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3781 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3782 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3783 stats->tx_window_errors += sstats->tx_window_errors;
3786 read_unlock_bh(&bond->lock);
3788 return stats;
3791 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3793 struct net_device *slave_dev = NULL;
3794 struct ifbond k_binfo;
3795 struct ifbond __user *u_binfo = NULL;
3796 struct ifslave k_sinfo;
3797 struct ifslave __user *u_sinfo = NULL;
3798 struct mii_ioctl_data *mii = NULL;
3799 int res = 0;
3801 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3802 bond_dev->name, cmd);
3804 switch (cmd) {
3805 case SIOCGMIIPHY:
3806 mii = if_mii(ifr);
3807 if (!mii) {
3808 return -EINVAL;
3810 mii->phy_id = 0;
3811 /* Fall Through */
3812 case SIOCGMIIREG:
3814 * We do this again just in case we were called by SIOCGMIIREG
3815 * instead of SIOCGMIIPHY.
3817 mii = if_mii(ifr);
3818 if (!mii) {
3819 return -EINVAL;
3822 if (mii->reg_num == 1) {
3823 struct bonding *bond = bond_dev->priv;
3824 mii->val_out = 0;
3825 read_lock(&bond->lock);
3826 read_lock(&bond->curr_slave_lock);
3827 if (netif_carrier_ok(bond->dev)) {
3828 mii->val_out = BMSR_LSTATUS;
3830 read_unlock(&bond->curr_slave_lock);
3831 read_unlock(&bond->lock);
3834 return 0;
3835 case BOND_INFO_QUERY_OLD:
3836 case SIOCBONDINFOQUERY:
3837 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3839 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3840 return -EFAULT;
3843 res = bond_info_query(bond_dev, &k_binfo);
3844 if (res == 0) {
3845 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3846 return -EFAULT;
3850 return res;
3851 case BOND_SLAVE_INFO_QUERY_OLD:
3852 case SIOCBONDSLAVEINFOQUERY:
3853 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3855 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3856 return -EFAULT;
3859 res = bond_slave_info_query(bond_dev, &k_sinfo);
3860 if (res == 0) {
3861 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3862 return -EFAULT;
3866 return res;
3867 default:
3868 /* Go on */
3869 break;
3872 if (!capable(CAP_NET_ADMIN)) {
3873 return -EPERM;
3876 down_write(&(bonding_rwsem));
3877 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
3879 dprintk("slave_dev=%p: \n", slave_dev);
3881 if (!slave_dev) {
3882 res = -ENODEV;
3883 } else {
3884 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3885 switch (cmd) {
3886 case BOND_ENSLAVE_OLD:
3887 case SIOCBONDENSLAVE:
3888 res = bond_enslave(bond_dev, slave_dev);
3889 break;
3890 case BOND_RELEASE_OLD:
3891 case SIOCBONDRELEASE:
3892 res = bond_release(bond_dev, slave_dev);
3893 break;
3894 case BOND_SETHWADDR_OLD:
3895 case SIOCBONDSETHWADDR:
3896 res = bond_sethwaddr(bond_dev, slave_dev);
3897 break;
3898 case BOND_CHANGE_ACTIVE_OLD:
3899 case SIOCBONDCHANGEACTIVE:
3900 res = bond_ioctl_change_active(bond_dev, slave_dev);
3901 break;
3902 default:
3903 res = -EOPNOTSUPP;
3906 dev_put(slave_dev);
3909 up_write(&(bonding_rwsem));
3910 return res;
3913 static void bond_set_multicast_list(struct net_device *bond_dev)
3915 struct bonding *bond = bond_dev->priv;
3916 struct dev_mc_list *dmi;
3918 write_lock_bh(&bond->lock);
3921 * Do promisc before checking multicast_mode
3923 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3924 bond_set_promiscuity(bond, 1);
3927 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3928 bond_set_promiscuity(bond, -1);
3931 /* set allmulti flag to slaves */
3932 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3933 bond_set_allmulti(bond, 1);
3936 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3937 bond_set_allmulti(bond, -1);
3940 bond->flags = bond_dev->flags;
3942 /* looking for addresses to add to slaves' mc list */
3943 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3944 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3945 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3949 /* looking for addresses to delete from slaves' list */
3950 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3951 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3952 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3956 /* save master's multicast list */
3957 bond_mc_list_destroy(bond);
3958 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3960 write_unlock_bh(&bond->lock);
3964 * Change the MTU of all of a master's slaves to match the master
3966 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3968 struct bonding *bond = bond_dev->priv;
3969 struct slave *slave, *stop_at;
3970 int res = 0;
3971 int i;
3973 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3974 (bond_dev ? bond_dev->name : "None"), new_mtu);
3976 /* Can't hold bond->lock with bh disabled here since
3977 * some base drivers panic. On the other hand we can't
3978 * hold bond->lock without bh disabled because we'll
3979 * deadlock. The only solution is to rely on the fact
3980 * that we're under rtnl_lock here, and the slaves
3981 * list won't change. This doesn't solve the problem
3982 * of setting the slave's MTU while it is
3983 * transmitting, but the assumption is that the base
3984 * driver can handle that.
3986 * TODO: figure out a way to safely iterate the slaves
3987 * list, but without holding a lock around the actual
3988 * call to the base driver.
3991 bond_for_each_slave(bond, slave, i) {
3992 dprintk("s %p s->p %p c_m %p\n", slave,
3993 slave->prev, slave->dev->change_mtu);
3995 res = dev_set_mtu(slave->dev, new_mtu);
3997 if (res) {
3998 /* If we failed to set the slave's mtu to the new value
3999 * we must abort the operation even in ACTIVE_BACKUP
4000 * mode, because if we allow the backup slaves to have
4001 * different mtu values than the active slave we'll
4002 * need to change their mtu when doing a failover. That
4003 * means changing their mtu from timer context, which
4004 * is probably not a good idea.
4006 dprintk("err %d %s\n", res, slave->dev->name);
4007 goto unwind;
4011 bond_dev->mtu = new_mtu;
4013 return 0;
4015 unwind:
4016 /* unwind from head to the slave that failed */
4017 stop_at = slave;
4018 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4019 int tmp_res;
4021 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4022 if (tmp_res) {
4023 dprintk("unwind err %d dev %s\n", tmp_res,
4024 slave->dev->name);
4028 return res;
4032 * Change HW address
4034 * Note that many devices must be down to change the HW address, and
4035 * downing the master releases all slaves. We can make bonds full of
4036 * bonding devices to test this, however.
4038 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4040 struct bonding *bond = bond_dev->priv;
4041 struct sockaddr *sa = addr, tmp_sa;
4042 struct slave *slave, *stop_at;
4043 int res = 0;
4044 int i;
4046 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4049 * If fail_over_mac is enabled, do nothing and return success.
4050 * Returning an error causes ifenslave to fail.
4052 if (bond->params.fail_over_mac)
4053 return 0;
4055 if (!is_valid_ether_addr(sa->sa_data)) {
4056 return -EADDRNOTAVAIL;
4059 /* Can't hold bond->lock with bh disabled here since
4060 * some base drivers panic. On the other hand we can't
4061 * hold bond->lock without bh disabled because we'll
4062 * deadlock. The only solution is to rely on the fact
4063 * that we're under rtnl_lock here, and the slaves
4064 * list won't change. This doesn't solve the problem
4065 * of setting the slave's hw address while it is
4066 * transmitting, but the assumption is that the base
4067 * driver can handle that.
4069 * TODO: figure out a way to safely iterate the slaves
4070 * list, but without holding a lock around the actual
4071 * call to the base driver.
4074 bond_for_each_slave(bond, slave, i) {
4075 dprintk("slave %p %s\n", slave, slave->dev->name);
4077 if (slave->dev->set_mac_address == NULL) {
4078 res = -EOPNOTSUPP;
4079 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4080 goto unwind;
4083 res = dev_set_mac_address(slave->dev, addr);
4084 if (res) {
4085 /* TODO: consider downing the slave
4086 * and retry ?
4087 * User should expect communications
4088 * breakage anyway until ARP finish
4089 * updating, so...
4091 dprintk("err %d %s\n", res, slave->dev->name);
4092 goto unwind;
4096 /* success */
4097 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4098 return 0;
4100 unwind:
4101 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4102 tmp_sa.sa_family = bond_dev->type;
4104 /* unwind from head to the slave that failed */
4105 stop_at = slave;
4106 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4107 int tmp_res;
4109 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4110 if (tmp_res) {
4111 dprintk("unwind err %d dev %s\n", tmp_res,
4112 slave->dev->name);
4116 return res;
4119 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4121 struct bonding *bond = bond_dev->priv;
4122 struct slave *slave, *start_at;
4123 int i, slave_no, res = 1;
4125 read_lock(&bond->lock);
4127 if (!BOND_IS_OK(bond)) {
4128 goto out;
4132 * Concurrent TX may collide on rr_tx_counter; we accept that
4133 * as being rare enough not to justify using an atomic op here
4135 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4137 bond_for_each_slave(bond, slave, i) {
4138 slave_no--;
4139 if (slave_no < 0) {
4140 break;
4144 start_at = slave;
4145 bond_for_each_slave_from(bond, slave, i, start_at) {
4146 if (IS_UP(slave->dev) &&
4147 (slave->link == BOND_LINK_UP) &&
4148 (slave->state == BOND_STATE_ACTIVE)) {
4149 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4150 break;
4154 out:
4155 if (res) {
4156 /* no suitable interface, frame not sent */
4157 dev_kfree_skb(skb);
4159 read_unlock(&bond->lock);
4160 return 0;
4165 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4166 * the bond has a usable interface.
4168 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4170 struct bonding *bond = bond_dev->priv;
4171 int res = 1;
4173 read_lock(&bond->lock);
4174 read_lock(&bond->curr_slave_lock);
4176 if (!BOND_IS_OK(bond)) {
4177 goto out;
4180 if (!bond->curr_active_slave)
4181 goto out;
4183 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4185 out:
4186 if (res) {
4187 /* no suitable interface, frame not sent */
4188 dev_kfree_skb(skb);
4190 read_unlock(&bond->curr_slave_lock);
4191 read_unlock(&bond->lock);
4192 return 0;
4196 * In bond_xmit_xor() , we determine the output device by using a pre-
4197 * determined xmit_hash_policy(), If the selected device is not enabled,
4198 * find the next active slave.
4200 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4202 struct bonding *bond = bond_dev->priv;
4203 struct slave *slave, *start_at;
4204 int slave_no;
4205 int i;
4206 int res = 1;
4208 read_lock(&bond->lock);
4210 if (!BOND_IS_OK(bond)) {
4211 goto out;
4214 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4216 bond_for_each_slave(bond, slave, i) {
4217 slave_no--;
4218 if (slave_no < 0) {
4219 break;
4223 start_at = slave;
4225 bond_for_each_slave_from(bond, slave, i, start_at) {
4226 if (IS_UP(slave->dev) &&
4227 (slave->link == BOND_LINK_UP) &&
4228 (slave->state == BOND_STATE_ACTIVE)) {
4229 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4230 break;
4234 out:
4235 if (res) {
4236 /* no suitable interface, frame not sent */
4237 dev_kfree_skb(skb);
4239 read_unlock(&bond->lock);
4240 return 0;
4244 * in broadcast mode, we send everything to all usable interfaces.
4246 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4248 struct bonding *bond = bond_dev->priv;
4249 struct slave *slave, *start_at;
4250 struct net_device *tx_dev = NULL;
4251 int i;
4252 int res = 1;
4254 read_lock(&bond->lock);
4256 if (!BOND_IS_OK(bond)) {
4257 goto out;
4260 read_lock(&bond->curr_slave_lock);
4261 start_at = bond->curr_active_slave;
4262 read_unlock(&bond->curr_slave_lock);
4264 if (!start_at) {
4265 goto out;
4268 bond_for_each_slave_from(bond, slave, i, start_at) {
4269 if (IS_UP(slave->dev) &&
4270 (slave->link == BOND_LINK_UP) &&
4271 (slave->state == BOND_STATE_ACTIVE)) {
4272 if (tx_dev) {
4273 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4274 if (!skb2) {
4275 printk(KERN_ERR DRV_NAME
4276 ": %s: Error: bond_xmit_broadcast(): "
4277 "skb_clone() failed\n",
4278 bond_dev->name);
4279 continue;
4282 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4283 if (res) {
4284 dev_kfree_skb(skb2);
4285 continue;
4288 tx_dev = slave->dev;
4292 if (tx_dev) {
4293 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4296 out:
4297 if (res) {
4298 /* no suitable interface, frame not sent */
4299 dev_kfree_skb(skb);
4301 /* frame sent to all suitable interfaces */
4302 read_unlock(&bond->lock);
4303 return 0;
4306 /*------------------------- Device initialization ---------------------------*/
4309 * set bond mode specific net device operations
4311 void bond_set_mode_ops(struct bonding *bond, int mode)
4313 struct net_device *bond_dev = bond->dev;
4315 switch (mode) {
4316 case BOND_MODE_ROUNDROBIN:
4317 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4318 break;
4319 case BOND_MODE_ACTIVEBACKUP:
4320 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4321 break;
4322 case BOND_MODE_XOR:
4323 bond_dev->hard_start_xmit = bond_xmit_xor;
4324 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4325 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4326 else
4327 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4328 break;
4329 case BOND_MODE_BROADCAST:
4330 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4331 break;
4332 case BOND_MODE_8023AD:
4333 bond_set_master_3ad_flags(bond);
4334 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4335 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4336 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4337 else
4338 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4339 break;
4340 case BOND_MODE_ALB:
4341 bond_set_master_alb_flags(bond);
4342 /* FALLTHRU */
4343 case BOND_MODE_TLB:
4344 bond_dev->hard_start_xmit = bond_alb_xmit;
4345 bond_dev->set_mac_address = bond_alb_set_mac_address;
4346 break;
4347 default:
4348 /* Should never happen, mode already checked */
4349 printk(KERN_ERR DRV_NAME
4350 ": %s: Error: Unknown bonding mode %d\n",
4351 bond_dev->name,
4352 mode);
4353 break;
4357 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4358 struct ethtool_drvinfo *drvinfo)
4360 strncpy(drvinfo->driver, DRV_NAME, 32);
4361 strncpy(drvinfo->version, DRV_VERSION, 32);
4362 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4365 static const struct ethtool_ops bond_ethtool_ops = {
4366 .get_drvinfo = bond_ethtool_get_drvinfo,
4370 * Does not allocate but creates a /proc entry.
4371 * Allowed to fail.
4373 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4375 struct bonding *bond = bond_dev->priv;
4377 dprintk("Begin bond_init for %s\n", bond_dev->name);
4379 /* initialize rwlocks */
4380 rwlock_init(&bond->lock);
4381 rwlock_init(&bond->curr_slave_lock);
4383 bond->params = *params; /* copy params struct */
4385 bond->wq = create_singlethread_workqueue(bond_dev->name);
4386 if (!bond->wq)
4387 return -ENOMEM;
4389 /* Initialize pointers */
4390 bond->first_slave = NULL;
4391 bond->curr_active_slave = NULL;
4392 bond->current_arp_slave = NULL;
4393 bond->primary_slave = NULL;
4394 bond->dev = bond_dev;
4395 bond->send_grat_arp = 0;
4396 bond->setup_by_slave = 0;
4397 INIT_LIST_HEAD(&bond->vlan_list);
4399 /* Initialize the device entry points */
4400 bond_dev->open = bond_open;
4401 bond_dev->stop = bond_close;
4402 bond_dev->get_stats = bond_get_stats;
4403 bond_dev->do_ioctl = bond_do_ioctl;
4404 bond_dev->ethtool_ops = &bond_ethtool_ops;
4405 bond_dev->set_multicast_list = bond_set_multicast_list;
4406 bond_dev->change_mtu = bond_change_mtu;
4407 bond_dev->set_mac_address = bond_set_mac_address;
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
4426 * transmitting */
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
4433 * capable
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);
4444 #endif
4445 list_add_tail(&bond->bond_list, &bond_dev_list);
4447 return 0;
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);
4461 #endif
4464 /* Unregister and free all bond devices.
4465 * Caller must hold rtnl_lock.
4467 static void bond_free_all(void)
4469 struct bonding *bond, *nxt;
4471 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4472 struct net_device *bond_dev = bond->dev;
4474 bond_mc_list_destroy(bond);
4475 /* Release the bonded slaves */
4476 bond_release_all(bond_dev);
4477 unregister_netdevice(bond_dev);
4478 bond_deinit(bond_dev);
4481 #ifdef CONFIG_PROC_FS
4482 bond_destroy_proc_dir();
4483 #endif
4486 /*------------------------- Module initialization ---------------------------*/
4489 * Convert string input module parms. Accept either the
4490 * number of the mode or its string name.
4492 int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4494 int i;
4496 for (i = 0; tbl[i].modename; i++) {
4497 if ((isdigit(*mode_arg) &&
4498 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4499 (strncmp(mode_arg, tbl[i].modename,
4500 strlen(tbl[i].modename)) == 0)) {
4501 return tbl[i].mode;
4505 return -1;
4508 static int bond_check_params(struct bond_params *params)
4510 int arp_validate_value;
4513 * Convert string parameters.
4515 if (mode) {
4516 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4517 if (bond_mode == -1) {
4518 printk(KERN_ERR DRV_NAME
4519 ": Error: Invalid bonding mode \"%s\"\n",
4520 mode == NULL ? "NULL" : mode);
4521 return -EINVAL;
4525 if (xmit_hash_policy) {
4526 if ((bond_mode != BOND_MODE_XOR) &&
4527 (bond_mode != BOND_MODE_8023AD)) {
4528 printk(KERN_INFO DRV_NAME
4529 ": xor_mode param is irrelevant in mode %s\n",
4530 bond_mode_name(bond_mode));
4531 } else {
4532 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4533 xmit_hashtype_tbl);
4534 if (xmit_hashtype == -1) {
4535 printk(KERN_ERR DRV_NAME
4536 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4537 xmit_hash_policy == NULL ? "NULL" :
4538 xmit_hash_policy);
4539 return -EINVAL;
4544 if (lacp_rate) {
4545 if (bond_mode != BOND_MODE_8023AD) {
4546 printk(KERN_INFO DRV_NAME
4547 ": lacp_rate param is irrelevant in mode %s\n",
4548 bond_mode_name(bond_mode));
4549 } else {
4550 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4551 if (lacp_fast == -1) {
4552 printk(KERN_ERR DRV_NAME
4553 ": Error: Invalid lacp rate \"%s\"\n",
4554 lacp_rate == NULL ? "NULL" : lacp_rate);
4555 return -EINVAL;
4560 if (max_bonds < 1 || max_bonds > INT_MAX) {
4561 printk(KERN_WARNING DRV_NAME
4562 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4563 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4564 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4565 max_bonds = BOND_DEFAULT_MAX_BONDS;
4568 if (miimon < 0) {
4569 printk(KERN_WARNING DRV_NAME
4570 ": Warning: miimon module parameter (%d), "
4571 "not in range 0-%d, so it was reset to %d\n",
4572 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4573 miimon = BOND_LINK_MON_INTERV;
4576 if (updelay < 0) {
4577 printk(KERN_WARNING DRV_NAME
4578 ": Warning: updelay module parameter (%d), "
4579 "not in range 0-%d, so it was reset to 0\n",
4580 updelay, INT_MAX);
4581 updelay = 0;
4584 if (downdelay < 0) {
4585 printk(KERN_WARNING DRV_NAME
4586 ": Warning: downdelay module parameter (%d), "
4587 "not in range 0-%d, so it was reset to 0\n",
4588 downdelay, INT_MAX);
4589 downdelay = 0;
4592 if ((use_carrier != 0) && (use_carrier != 1)) {
4593 printk(KERN_WARNING DRV_NAME
4594 ": Warning: use_carrier module parameter (%d), "
4595 "not of valid value (0/1), so it was set to 1\n",
4596 use_carrier);
4597 use_carrier = 1;
4600 /* reset values for 802.3ad */
4601 if (bond_mode == BOND_MODE_8023AD) {
4602 if (!miimon) {
4603 printk(KERN_WARNING DRV_NAME
4604 ": Warning: miimon must be specified, "
4605 "otherwise bonding will not detect link "
4606 "failure, speed and duplex which are "
4607 "essential for 802.3ad operation\n");
4608 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4609 miimon = 100;
4613 /* reset values for TLB/ALB */
4614 if ((bond_mode == BOND_MODE_TLB) ||
4615 (bond_mode == BOND_MODE_ALB)) {
4616 if (!miimon) {
4617 printk(KERN_WARNING DRV_NAME
4618 ": Warning: miimon must be specified, "
4619 "otherwise bonding will not detect link "
4620 "failure and link speed which are essential "
4621 "for TLB/ALB load balancing\n");
4622 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4623 miimon = 100;
4627 if (bond_mode == BOND_MODE_ALB) {
4628 printk(KERN_NOTICE DRV_NAME
4629 ": In ALB mode you might experience client "
4630 "disconnections upon reconnection of a link if the "
4631 "bonding module updelay parameter (%d msec) is "
4632 "incompatible with the forwarding delay time of the "
4633 "switch\n",
4634 updelay);
4637 if (!miimon) {
4638 if (updelay || downdelay) {
4639 /* just warn the user the up/down delay will have
4640 * no effect since miimon is zero...
4642 printk(KERN_WARNING DRV_NAME
4643 ": Warning: miimon module parameter not set "
4644 "and updelay (%d) or downdelay (%d) module "
4645 "parameter is set; updelay and downdelay have "
4646 "no effect unless miimon is set\n",
4647 updelay, downdelay);
4649 } else {
4650 /* don't allow arp monitoring */
4651 if (arp_interval) {
4652 printk(KERN_WARNING DRV_NAME
4653 ": Warning: miimon (%d) and arp_interval (%d) "
4654 "can't be used simultaneously, disabling ARP "
4655 "monitoring\n",
4656 miimon, arp_interval);
4657 arp_interval = 0;
4660 if ((updelay % miimon) != 0) {
4661 printk(KERN_WARNING DRV_NAME
4662 ": Warning: updelay (%d) is not a multiple "
4663 "of miimon (%d), updelay rounded to %d ms\n",
4664 updelay, miimon, (updelay / miimon) * miimon);
4667 updelay /= miimon;
4669 if ((downdelay % miimon) != 0) {
4670 printk(KERN_WARNING DRV_NAME
4671 ": Warning: downdelay (%d) is not a multiple "
4672 "of miimon (%d), downdelay rounded to %d ms\n",
4673 downdelay, miimon,
4674 (downdelay / miimon) * miimon);
4677 downdelay /= miimon;
4680 if (arp_interval < 0) {
4681 printk(KERN_WARNING DRV_NAME
4682 ": Warning: arp_interval module parameter (%d) "
4683 ", not in range 0-%d, so it was reset to %d\n",
4684 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4685 arp_interval = BOND_LINK_ARP_INTERV;
4688 for (arp_ip_count = 0;
4689 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4690 arp_ip_count++) {
4691 /* not complete check, but should be good enough to
4692 catch mistakes */
4693 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4694 printk(KERN_WARNING DRV_NAME
4695 ": Warning: bad arp_ip_target module parameter "
4696 "(%s), ARP monitoring will not be performed\n",
4697 arp_ip_target[arp_ip_count]);
4698 arp_interval = 0;
4699 } else {
4700 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4701 arp_target[arp_ip_count] = ip;
4705 if (arp_interval && !arp_ip_count) {
4706 /* don't allow arping if no arp_ip_target given... */
4707 printk(KERN_WARNING DRV_NAME
4708 ": Warning: arp_interval module parameter (%d) "
4709 "specified without providing an arp_ip_target "
4710 "parameter, arp_interval was reset to 0\n",
4711 arp_interval);
4712 arp_interval = 0;
4715 if (arp_validate) {
4716 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4717 printk(KERN_ERR DRV_NAME
4718 ": arp_validate only supported in active-backup mode\n");
4719 return -EINVAL;
4721 if (!arp_interval) {
4722 printk(KERN_ERR DRV_NAME
4723 ": arp_validate requires arp_interval\n");
4724 return -EINVAL;
4727 arp_validate_value = bond_parse_parm(arp_validate,
4728 arp_validate_tbl);
4729 if (arp_validate_value == -1) {
4730 printk(KERN_ERR DRV_NAME
4731 ": Error: invalid arp_validate \"%s\"\n",
4732 arp_validate == NULL ? "NULL" : arp_validate);
4733 return -EINVAL;
4735 } else
4736 arp_validate_value = 0;
4738 if (miimon) {
4739 printk(KERN_INFO DRV_NAME
4740 ": MII link monitoring set to %d ms\n",
4741 miimon);
4742 } else if (arp_interval) {
4743 int i;
4745 printk(KERN_INFO DRV_NAME
4746 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4747 arp_interval,
4748 arp_validate_tbl[arp_validate_value].modename,
4749 arp_ip_count);
4751 for (i = 0; i < arp_ip_count; i++)
4752 printk (" %s", arp_ip_target[i]);
4754 printk("\n");
4756 } else {
4757 /* miimon and arp_interval not set, we need one so things
4758 * work as expected, see bonding.txt for details
4760 printk(KERN_WARNING DRV_NAME
4761 ": Warning: either miimon or arp_interval and "
4762 "arp_ip_target module parameters must be specified, "
4763 "otherwise bonding will not detect link failures! see "
4764 "bonding.txt for details.\n");
4767 if (primary && !USES_PRIMARY(bond_mode)) {
4768 /* currently, using a primary only makes sense
4769 * in active backup, TLB or ALB modes
4771 printk(KERN_WARNING DRV_NAME
4772 ": Warning: %s primary device specified but has no "
4773 "effect in %s mode\n",
4774 primary, bond_mode_name(bond_mode));
4775 primary = NULL;
4778 if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
4779 printk(KERN_WARNING DRV_NAME
4780 ": Warning: fail_over_mac only affects "
4781 "active-backup mode.\n");
4783 /* fill params struct with the proper values */
4784 params->mode = bond_mode;
4785 params->xmit_policy = xmit_hashtype;
4786 params->miimon = miimon;
4787 params->arp_interval = arp_interval;
4788 params->arp_validate = arp_validate_value;
4789 params->updelay = updelay;
4790 params->downdelay = downdelay;
4791 params->use_carrier = use_carrier;
4792 params->lacp_fast = lacp_fast;
4793 params->primary[0] = 0;
4794 params->fail_over_mac = fail_over_mac;
4796 if (primary) {
4797 strncpy(params->primary, primary, IFNAMSIZ);
4798 params->primary[IFNAMSIZ - 1] = 0;
4801 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4803 return 0;
4806 static struct lock_class_key bonding_netdev_xmit_lock_key;
4808 /* Create a new bond based on the specified name and bonding parameters.
4809 * If name is NULL, obtain a suitable "bond%d" name for us.
4810 * Caller must NOT hold rtnl_lock; we need to release it here before we
4811 * set up our sysfs entries.
4813 int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4815 struct net_device *bond_dev;
4816 int res;
4818 rtnl_lock();
4819 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4820 ether_setup);
4821 if (!bond_dev) {
4822 printk(KERN_ERR DRV_NAME
4823 ": %s: eek! can't alloc netdev!\n",
4824 name);
4825 res = -ENOMEM;
4826 goto out_rtnl;
4829 if (!name) {
4830 res = dev_alloc_name(bond_dev, "bond%d");
4831 if (res < 0)
4832 goto out_netdev;
4835 /* bond_init() must be called after dev_alloc_name() (for the
4836 * /proc files), but before register_netdevice(), because we
4837 * need to set function pointers.
4840 res = bond_init(bond_dev, params);
4841 if (res < 0) {
4842 goto out_netdev;
4845 res = register_netdevice(bond_dev);
4846 if (res < 0) {
4847 goto out_bond;
4850 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4852 if (newbond)
4853 *newbond = bond_dev->priv;
4855 netif_carrier_off(bond_dev);
4857 rtnl_unlock(); /* allows sysfs registration of net device */
4858 res = bond_create_sysfs_entry(bond_dev->priv);
4859 if (res < 0) {
4860 rtnl_lock();
4861 goto out_bond;
4864 return 0;
4866 out_bond:
4867 bond_deinit(bond_dev);
4868 out_netdev:
4869 free_netdev(bond_dev);
4870 out_rtnl:
4871 rtnl_unlock();
4872 return res;
4875 static void bond_work_cancel_all(struct bonding *bond)
4877 write_lock_bh(&bond->lock);
4878 bond->kill_timers = 1;
4879 write_unlock_bh(&bond->lock);
4881 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4882 cancel_delayed_work(&bond->mii_work);
4884 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4885 cancel_delayed_work(&bond->arp_work);
4887 if (bond->params.mode == BOND_MODE_ALB &&
4888 delayed_work_pending(&bond->alb_work))
4889 cancel_delayed_work(&bond->alb_work);
4891 if (bond->params.mode == BOND_MODE_8023AD &&
4892 delayed_work_pending(&bond->ad_work))
4893 cancel_delayed_work(&bond->ad_work);
4896 static int __init bonding_init(void)
4898 int i;
4899 int res;
4900 struct bonding *bond, *nxt;
4902 printk(KERN_INFO "%s", version);
4904 res = bond_check_params(&bonding_defaults);
4905 if (res) {
4906 goto out;
4909 #ifdef CONFIG_PROC_FS
4910 bond_create_proc_dir();
4911 #endif
4912 for (i = 0; i < max_bonds; i++) {
4913 res = bond_create(NULL, &bonding_defaults, NULL);
4914 if (res)
4915 goto err;
4918 res = bond_create_sysfs();
4919 if (res)
4920 goto err;
4922 register_netdevice_notifier(&bond_netdev_notifier);
4923 register_inetaddr_notifier(&bond_inetaddr_notifier);
4925 goto out;
4926 err:
4927 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4928 bond_work_cancel_all(bond);
4929 destroy_workqueue(bond->wq);
4932 rtnl_lock();
4933 bond_free_all();
4934 bond_destroy_sysfs();
4935 rtnl_unlock();
4936 out:
4937 return res;
4941 static void __exit bonding_exit(void)
4943 unregister_netdevice_notifier(&bond_netdev_notifier);
4944 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4946 rtnl_lock();
4947 bond_free_all();
4948 bond_destroy_sysfs();
4949 rtnl_unlock();
4952 module_init(bonding_init);
4953 module_exit(bonding_exit);
4954 MODULE_LICENSE("GPL");
4955 MODULE_VERSION(DRV_VERSION);
4956 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4957 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4958 MODULE_SUPPORTED_DEVICE("most ethernet devices");
4961 * Local variables:
4962 * c-indent-level: 8
4963 * c-basic-offset: 8
4964 * tab-width: 8
4965 * End: