[NET]: Make /proc/net per network namespace
[linux-2.6/kmemtrace.git] / drivers / net / bonding / bond_main.c
blob5de648f90a458106e406bf6881ab3b03344263e2
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 struct bond_params bonding_defaults;
103 module_param(max_bonds, int, 0);
104 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
105 module_param(miimon, int, 0);
106 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
107 module_param(updelay, int, 0);
108 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
109 module_param(downdelay, int, 0);
110 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
111 "in milliseconds");
112 module_param(use_carrier, int, 0);
113 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
114 "0 for off, 1 for on (default)");
115 module_param(mode, charp, 0);
116 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
117 "1 for active-backup, 2 for balance-xor, "
118 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
119 "6 for balance-alb");
120 module_param(primary, charp, 0);
121 MODULE_PARM_DESC(primary, "Primary network device to use");
122 module_param(lacp_rate, charp, 0);
123 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
124 "(slow/fast)");
125 module_param(xmit_hash_policy, charp, 0);
126 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
127 ", 1 for layer 3+4");
128 module_param(arp_interval, int, 0);
129 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
130 module_param_array(arp_ip_target, charp, NULL, 0);
131 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
132 module_param(arp_validate, charp, 0);
133 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
135 /*----------------------------- Global variables ----------------------------*/
137 static const char * const version =
138 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
140 LIST_HEAD(bond_dev_list);
142 #ifdef CONFIG_PROC_FS
143 static struct proc_dir_entry *bond_proc_dir = NULL;
144 #endif
146 extern struct rw_semaphore bonding_rwsem;
147 static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
148 static int arp_ip_count = 0;
149 static int bond_mode = BOND_MODE_ROUNDROBIN;
150 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
151 static int lacp_fast = 0;
154 struct bond_parm_tbl bond_lacp_tbl[] = {
155 { "slow", AD_LACP_SLOW},
156 { "fast", AD_LACP_FAST},
157 { NULL, -1},
160 struct bond_parm_tbl bond_mode_tbl[] = {
161 { "balance-rr", BOND_MODE_ROUNDROBIN},
162 { "active-backup", BOND_MODE_ACTIVEBACKUP},
163 { "balance-xor", BOND_MODE_XOR},
164 { "broadcast", BOND_MODE_BROADCAST},
165 { "802.3ad", BOND_MODE_8023AD},
166 { "balance-tlb", BOND_MODE_TLB},
167 { "balance-alb", BOND_MODE_ALB},
168 { NULL, -1},
171 struct bond_parm_tbl xmit_hashtype_tbl[] = {
172 { "layer2", BOND_XMIT_POLICY_LAYER2},
173 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
174 { NULL, -1},
177 struct bond_parm_tbl arp_validate_tbl[] = {
178 { "none", BOND_ARP_VALIDATE_NONE},
179 { "active", BOND_ARP_VALIDATE_ACTIVE},
180 { "backup", BOND_ARP_VALIDATE_BACKUP},
181 { "all", BOND_ARP_VALIDATE_ALL},
182 { NULL, -1},
185 /*-------------------------- Forward declarations ---------------------------*/
187 static void bond_send_gratuitous_arp(struct bonding *bond);
189 /*---------------------------- General routines -----------------------------*/
191 static const char *bond_mode_name(int mode)
193 switch (mode) {
194 case BOND_MODE_ROUNDROBIN :
195 return "load balancing (round-robin)";
196 case BOND_MODE_ACTIVEBACKUP :
197 return "fault-tolerance (active-backup)";
198 case BOND_MODE_XOR :
199 return "load balancing (xor)";
200 case BOND_MODE_BROADCAST :
201 return "fault-tolerance (broadcast)";
202 case BOND_MODE_8023AD:
203 return "IEEE 802.3ad Dynamic link aggregation";
204 case BOND_MODE_TLB:
205 return "transmit load balancing";
206 case BOND_MODE_ALB:
207 return "adaptive load balancing";
208 default:
209 return "unknown";
213 /*---------------------------------- VLAN -----------------------------------*/
216 * bond_add_vlan - add a new vlan id on bond
217 * @bond: bond that got the notification
218 * @vlan_id: the vlan id to add
220 * Returns -ENOMEM if allocation failed.
222 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
224 struct vlan_entry *vlan;
226 dprintk("bond: %s, vlan id %d\n",
227 (bond ? bond->dev->name: "None"), vlan_id);
229 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
230 if (!vlan) {
231 return -ENOMEM;
234 INIT_LIST_HEAD(&vlan->vlan_list);
235 vlan->vlan_id = vlan_id;
236 vlan->vlan_ip = 0;
238 write_lock_bh(&bond->lock);
240 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
242 write_unlock_bh(&bond->lock);
244 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
246 return 0;
250 * bond_del_vlan - delete a vlan id from bond
251 * @bond: bond that got the notification
252 * @vlan_id: the vlan id to delete
254 * returns -ENODEV if @vlan_id was not found in @bond.
256 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
258 struct vlan_entry *vlan, *next;
259 int res = -ENODEV;
261 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
263 write_lock_bh(&bond->lock);
265 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
266 if (vlan->vlan_id == vlan_id) {
267 list_del(&vlan->vlan_list);
269 if ((bond->params.mode == BOND_MODE_TLB) ||
270 (bond->params.mode == BOND_MODE_ALB)) {
271 bond_alb_clear_vlan(bond, vlan_id);
274 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
275 bond->dev->name);
277 kfree(vlan);
279 if (list_empty(&bond->vlan_list) &&
280 (bond->slave_cnt == 0)) {
281 /* Last VLAN removed and no slaves, so
282 * restore block on adding VLANs. This will
283 * be removed once new slaves that are not
284 * VLAN challenged will be added.
286 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
289 res = 0;
290 goto out;
294 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
295 bond->dev->name);
297 out:
298 write_unlock_bh(&bond->lock);
299 return res;
303 * bond_has_challenged_slaves
304 * @bond: the bond we're working on
306 * Searches the slave list. Returns 1 if a vlan challenged slave
307 * was found, 0 otherwise.
309 * Assumes bond->lock is held.
311 static int bond_has_challenged_slaves(struct bonding *bond)
313 struct slave *slave;
314 int i;
316 bond_for_each_slave(bond, slave, i) {
317 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
318 dprintk("found VLAN challenged slave - %s\n",
319 slave->dev->name);
320 return 1;
324 dprintk("no VLAN challenged slaves found\n");
325 return 0;
329 * bond_next_vlan - safely skip to the next item in the vlans list.
330 * @bond: the bond we're working on
331 * @curr: item we're advancing from
333 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
334 * or @curr->next otherwise (even if it is @curr itself again).
336 * Caller must hold bond->lock
338 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
340 struct vlan_entry *next, *last;
342 if (list_empty(&bond->vlan_list)) {
343 return NULL;
346 if (!curr) {
347 next = list_entry(bond->vlan_list.next,
348 struct vlan_entry, vlan_list);
349 } else {
350 last = list_entry(bond->vlan_list.prev,
351 struct vlan_entry, vlan_list);
352 if (last == curr) {
353 next = list_entry(bond->vlan_list.next,
354 struct vlan_entry, vlan_list);
355 } else {
356 next = list_entry(curr->vlan_list.next,
357 struct vlan_entry, vlan_list);
361 return next;
365 * bond_dev_queue_xmit - Prepare skb for xmit.
367 * @bond: bond device that got this skb for tx.
368 * @skb: hw accel VLAN tagged skb to transmit
369 * @slave_dev: slave that is supposed to xmit this skbuff
371 * When the bond gets an skb to transmit that is
372 * already hardware accelerated VLAN tagged, and it
373 * needs to relay this skb to a slave that is not
374 * hw accel capable, the skb needs to be "unaccelerated",
375 * i.e. strip the hwaccel tag and re-insert it as part
376 * of the payload.
378 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
380 unsigned short vlan_id;
382 if (!list_empty(&bond->vlan_list) &&
383 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
384 vlan_get_tag(skb, &vlan_id) == 0) {
385 skb->dev = slave_dev;
386 skb = vlan_put_tag(skb, vlan_id);
387 if (!skb) {
388 /* vlan_put_tag() frees the skb in case of error,
389 * so return success here so the calling functions
390 * won't attempt to free is again.
392 return 0;
394 } else {
395 skb->dev = slave_dev;
398 skb->priority = 1;
399 dev_queue_xmit(skb);
401 return 0;
405 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
406 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
407 * lock because:
408 * a. This operation is performed in IOCTL context,
409 * b. The operation is protected by the RTNL semaphore in the 8021q code,
410 * c. Holding a lock with BH disabled while directly calling a base driver
411 * entry point is generally a BAD idea.
413 * The design of synchronization/protection for this operation in the 8021q
414 * module is good for one or more VLAN devices over a single physical device
415 * and cannot be extended for a teaming solution like bonding, so there is a
416 * potential race condition here where a net device from the vlan group might
417 * be referenced (either by a base driver or the 8021q code) while it is being
418 * removed from the system. However, it turns out we're not making matters
419 * worse, and if it works for regular VLAN usage it will work here too.
423 * bond_vlan_rx_register - Propagates registration to slaves
424 * @bond_dev: bonding net device that got called
425 * @grp: vlan group being registered
427 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
429 struct bonding *bond = bond_dev->priv;
430 struct slave *slave;
431 int i;
433 bond->vlgrp = grp;
435 bond_for_each_slave(bond, slave, i) {
436 struct net_device *slave_dev = slave->dev;
438 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
439 slave_dev->vlan_rx_register) {
440 slave_dev->vlan_rx_register(slave_dev, grp);
446 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
447 * @bond_dev: bonding net device that got called
448 * @vid: vlan id being added
450 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
452 struct bonding *bond = bond_dev->priv;
453 struct slave *slave;
454 int i, res;
456 bond_for_each_slave(bond, slave, i) {
457 struct net_device *slave_dev = slave->dev;
459 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
460 slave_dev->vlan_rx_add_vid) {
461 slave_dev->vlan_rx_add_vid(slave_dev, vid);
465 res = bond_add_vlan(bond, vid);
466 if (res) {
467 printk(KERN_ERR DRV_NAME
468 ": %s: Error: Failed to add vlan id %d\n",
469 bond_dev->name, vid);
474 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
475 * @bond_dev: bonding net device that got called
476 * @vid: vlan id being removed
478 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
480 struct bonding *bond = bond_dev->priv;
481 struct slave *slave;
482 struct net_device *vlan_dev;
483 int i, res;
485 bond_for_each_slave(bond, slave, i) {
486 struct net_device *slave_dev = slave->dev;
488 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
489 slave_dev->vlan_rx_kill_vid) {
490 /* Save and then restore vlan_dev in the grp array,
491 * since the slave's driver might clear it.
493 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
494 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
495 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
499 res = bond_del_vlan(bond, vid);
500 if (res) {
501 printk(KERN_ERR DRV_NAME
502 ": %s: Error: Failed to remove vlan id %d\n",
503 bond_dev->name, vid);
507 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
509 struct vlan_entry *vlan;
511 write_lock_bh(&bond->lock);
513 if (list_empty(&bond->vlan_list)) {
514 goto out;
517 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
518 slave_dev->vlan_rx_register) {
519 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
522 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
523 !(slave_dev->vlan_rx_add_vid)) {
524 goto out;
527 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
528 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
531 out:
532 write_unlock_bh(&bond->lock);
535 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
537 struct vlan_entry *vlan;
538 struct net_device *vlan_dev;
540 write_lock_bh(&bond->lock);
542 if (list_empty(&bond->vlan_list)) {
543 goto out;
546 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
547 !(slave_dev->vlan_rx_kill_vid)) {
548 goto unreg;
551 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
552 /* Save and then restore vlan_dev in the grp array,
553 * since the slave's driver might clear it.
555 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
556 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
557 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
560 unreg:
561 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
562 slave_dev->vlan_rx_register) {
563 slave_dev->vlan_rx_register(slave_dev, NULL);
566 out:
567 write_unlock_bh(&bond->lock);
570 /*------------------------------- Link status -------------------------------*/
573 * Set the carrier state for the master according to the state of its
574 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
575 * do special 802.3ad magic.
577 * Returns zero if carrier state does not change, nonzero if it does.
579 static int bond_set_carrier(struct bonding *bond)
581 struct slave *slave;
582 int i;
584 if (bond->slave_cnt == 0)
585 goto down;
587 if (bond->params.mode == BOND_MODE_8023AD)
588 return bond_3ad_set_carrier(bond);
590 bond_for_each_slave(bond, slave, i) {
591 if (slave->link == BOND_LINK_UP) {
592 if (!netif_carrier_ok(bond->dev)) {
593 netif_carrier_on(bond->dev);
594 return 1;
596 return 0;
600 down:
601 if (netif_carrier_ok(bond->dev)) {
602 netif_carrier_off(bond->dev);
603 return 1;
605 return 0;
609 * Get link speed and duplex from the slave's base driver
610 * using ethtool. If for some reason the call fails or the
611 * values are invalid, fake speed and duplex to 100/Full
612 * and return error.
614 static int bond_update_speed_duplex(struct slave *slave)
616 struct net_device *slave_dev = slave->dev;
617 struct ethtool_cmd etool;
618 int res;
620 /* Fake speed and duplex */
621 slave->speed = SPEED_100;
622 slave->duplex = DUPLEX_FULL;
624 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
625 return -1;
627 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
628 if (res < 0)
629 return -1;
631 switch (etool.speed) {
632 case SPEED_10:
633 case SPEED_100:
634 case SPEED_1000:
635 case SPEED_10000:
636 break;
637 default:
638 return -1;
641 switch (etool.duplex) {
642 case DUPLEX_FULL:
643 case DUPLEX_HALF:
644 break;
645 default:
646 return -1;
649 slave->speed = etool.speed;
650 slave->duplex = etool.duplex;
652 return 0;
656 * if <dev> supports MII link status reporting, check its link status.
658 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
659 * depening upon the setting of the use_carrier parameter.
661 * Return either BMSR_LSTATUS, meaning that the link is up (or we
662 * can't tell and just pretend it is), or 0, meaning that the link is
663 * down.
665 * If reporting is non-zero, instead of faking link up, return -1 if
666 * both ETHTOOL and MII ioctls fail (meaning the device does not
667 * support them). If use_carrier is set, return whatever it says.
668 * It'd be nice if there was a good way to tell if a driver supports
669 * netif_carrier, but there really isn't.
671 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
673 static int (* ioctl)(struct net_device *, struct ifreq *, int);
674 struct ifreq ifr;
675 struct mii_ioctl_data *mii;
677 if (bond->params.use_carrier) {
678 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
681 ioctl = slave_dev->do_ioctl;
682 if (ioctl) {
683 /* TODO: set pointer to correct ioctl on a per team member */
684 /* bases to make this more efficient. that is, once */
685 /* we determine the correct ioctl, we will always */
686 /* call it and not the others for that team */
687 /* member. */
690 * We cannot assume that SIOCGMIIPHY will also read a
691 * register; not all network drivers (e.g., e100)
692 * support that.
695 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
696 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
697 mii = if_mii(&ifr);
698 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
699 mii->reg_num = MII_BMSR;
700 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
701 return (mii->val_out & BMSR_LSTATUS);
707 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
708 * attempt to get link status from it if the above MII ioctls fail.
710 if (slave_dev->ethtool_ops) {
711 if (slave_dev->ethtool_ops->get_link) {
712 u32 link;
714 link = slave_dev->ethtool_ops->get_link(slave_dev);
716 return link ? BMSR_LSTATUS : 0;
721 * If reporting, report that either there's no dev->do_ioctl,
722 * or both SIOCGMIIREG and get_link failed (meaning that we
723 * cannot report link status). If not reporting, pretend
724 * we're ok.
726 return (reporting ? -1 : BMSR_LSTATUS);
729 /*----------------------------- Multicast list ------------------------------*/
732 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
734 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
736 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
737 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
741 * returns dmi entry if found, NULL otherwise
743 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
745 struct dev_mc_list *idmi;
747 for (idmi = mc_list; idmi; idmi = idmi->next) {
748 if (bond_is_dmi_same(dmi, idmi)) {
749 return idmi;
753 return NULL;
757 * Push the promiscuity flag down to appropriate slaves
759 static void bond_set_promiscuity(struct bonding *bond, int inc)
761 if (USES_PRIMARY(bond->params.mode)) {
762 /* write lock already acquired */
763 if (bond->curr_active_slave) {
764 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
766 } else {
767 struct slave *slave;
768 int i;
769 bond_for_each_slave(bond, slave, i) {
770 dev_set_promiscuity(slave->dev, inc);
776 * Push the allmulti flag down to all slaves
778 static void bond_set_allmulti(struct bonding *bond, int inc)
780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
783 dev_set_allmulti(bond->curr_active_slave->dev, inc);
785 } else {
786 struct slave *slave;
787 int i;
788 bond_for_each_slave(bond, slave, i) {
789 dev_set_allmulti(slave->dev, inc);
795 * Add a Multicast address to slaves
796 * according to mode
798 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
800 if (USES_PRIMARY(bond->params.mode)) {
801 /* write lock already acquired */
802 if (bond->curr_active_slave) {
803 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
805 } else {
806 struct slave *slave;
807 int i;
808 bond_for_each_slave(bond, slave, i) {
809 dev_mc_add(slave->dev, addr, alen, 0);
815 * Remove a multicast address from slave
816 * according to mode
818 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
820 if (USES_PRIMARY(bond->params.mode)) {
821 /* write lock already acquired */
822 if (bond->curr_active_slave) {
823 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
825 } else {
826 struct slave *slave;
827 int i;
828 bond_for_each_slave(bond, slave, i) {
829 dev_mc_delete(slave->dev, addr, alen, 0);
836 * Retrieve the list of registered multicast addresses for the bonding
837 * device and retransmit an IGMP JOIN request to the current active
838 * slave.
840 static void bond_resend_igmp_join_requests(struct bonding *bond)
842 struct in_device *in_dev;
843 struct ip_mc_list *im;
845 rcu_read_lock();
846 in_dev = __in_dev_get_rcu(bond->dev);
847 if (in_dev) {
848 for (im = in_dev->mc_list; im; im = im->next) {
849 ip_mc_rejoin_group(im);
853 rcu_read_unlock();
857 * Totally destroys the mc_list in bond
859 static void bond_mc_list_destroy(struct bonding *bond)
861 struct dev_mc_list *dmi;
863 dmi = bond->mc_list;
864 while (dmi) {
865 bond->mc_list = dmi->next;
866 kfree(dmi);
867 dmi = bond->mc_list;
869 bond->mc_list = NULL;
873 * Copy all the Multicast addresses from src to the bonding device dst
875 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
876 gfp_t gfp_flag)
878 struct dev_mc_list *dmi, *new_dmi;
880 for (dmi = mc_list; dmi; dmi = dmi->next) {
881 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
883 if (!new_dmi) {
884 /* FIXME: Potential memory leak !!! */
885 return -ENOMEM;
888 new_dmi->next = bond->mc_list;
889 bond->mc_list = new_dmi;
890 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
891 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
892 new_dmi->dmi_users = dmi->dmi_users;
893 new_dmi->dmi_gusers = dmi->dmi_gusers;
896 return 0;
900 * flush all members of flush->mc_list from device dev->mc_list
902 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
904 struct bonding *bond = bond_dev->priv;
905 struct dev_mc_list *dmi;
907 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
908 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
911 if (bond->params.mode == BOND_MODE_8023AD) {
912 /* del lacpdu mc addr from mc list */
913 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
915 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
919 /*--------------------------- Active slave change ---------------------------*/
922 * Update the mc list and multicast-related flags for the new and
923 * old active slaves (if any) according to the multicast mode, and
924 * promiscuous flags unconditionally.
926 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
928 struct dev_mc_list *dmi;
930 if (!USES_PRIMARY(bond->params.mode)) {
931 /* nothing to do - mc list is already up-to-date on
932 * all slaves
934 return;
937 if (old_active) {
938 if (bond->dev->flags & IFF_PROMISC) {
939 dev_set_promiscuity(old_active->dev, -1);
942 if (bond->dev->flags & IFF_ALLMULTI) {
943 dev_set_allmulti(old_active->dev, -1);
946 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
947 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
951 if (new_active) {
952 if (bond->dev->flags & IFF_PROMISC) {
953 dev_set_promiscuity(new_active->dev, 1);
956 if (bond->dev->flags & IFF_ALLMULTI) {
957 dev_set_allmulti(new_active->dev, 1);
960 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
961 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
963 bond_resend_igmp_join_requests(bond);
968 * find_best_interface - select the best available slave to be the active one
969 * @bond: our bonding struct
971 * Warning: Caller must hold curr_slave_lock for writing.
973 static struct slave *bond_find_best_slave(struct bonding *bond)
975 struct slave *new_active, *old_active;
976 struct slave *bestslave = NULL;
977 int mintime = bond->params.updelay;
978 int i;
980 new_active = old_active = bond->curr_active_slave;
982 if (!new_active) { /* there were no active slaves left */
983 if (bond->slave_cnt > 0) { /* found one slave */
984 new_active = bond->first_slave;
985 } else {
986 return NULL; /* still no slave, return NULL */
990 /* first try the primary link; if arping, a link must tx/rx traffic
991 * before it can be considered the curr_active_slave - also, we would skip
992 * slaves between the curr_active_slave and primary_slave that may be up
993 * and able to arp
995 if ((bond->primary_slave) &&
996 (!bond->params.arp_interval) &&
997 (IS_UP(bond->primary_slave->dev))) {
998 new_active = bond->primary_slave;
1001 /* remember where to stop iterating over the slaves */
1002 old_active = new_active;
1004 bond_for_each_slave_from(bond, new_active, i, old_active) {
1005 if (IS_UP(new_active->dev)) {
1006 if (new_active->link == BOND_LINK_UP) {
1007 return new_active;
1008 } else if (new_active->link == BOND_LINK_BACK) {
1009 /* link up, but waiting for stabilization */
1010 if (new_active->delay < mintime) {
1011 mintime = new_active->delay;
1012 bestslave = new_active;
1018 return bestslave;
1022 * change_active_interface - change the active slave into the specified one
1023 * @bond: our bonding struct
1024 * @new: the new slave to make the active one
1026 * Set the new slave to the bond's settings and unset them on the old
1027 * curr_active_slave.
1028 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1030 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1031 * because it is apparently the best available slave we have, even though its
1032 * updelay hasn't timed out yet.
1034 * Warning: Caller must hold curr_slave_lock for writing.
1036 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1038 struct slave *old_active = bond->curr_active_slave;
1040 if (old_active == new_active) {
1041 return;
1044 if (new_active) {
1045 if (new_active->link == BOND_LINK_BACK) {
1046 if (USES_PRIMARY(bond->params.mode)) {
1047 printk(KERN_INFO DRV_NAME
1048 ": %s: making interface %s the new "
1049 "active one %d ms earlier.\n",
1050 bond->dev->name, new_active->dev->name,
1051 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1054 new_active->delay = 0;
1055 new_active->link = BOND_LINK_UP;
1056 new_active->jiffies = jiffies;
1058 if (bond->params.mode == BOND_MODE_8023AD) {
1059 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1062 if ((bond->params.mode == BOND_MODE_TLB) ||
1063 (bond->params.mode == BOND_MODE_ALB)) {
1064 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1066 } else {
1067 if (USES_PRIMARY(bond->params.mode)) {
1068 printk(KERN_INFO DRV_NAME
1069 ": %s: making interface %s the new "
1070 "active one.\n",
1071 bond->dev->name, new_active->dev->name);
1076 if (USES_PRIMARY(bond->params.mode)) {
1077 bond_mc_swap(bond, new_active, old_active);
1080 if ((bond->params.mode == BOND_MODE_TLB) ||
1081 (bond->params.mode == BOND_MODE_ALB)) {
1082 bond_alb_handle_active_change(bond, new_active);
1083 if (old_active)
1084 bond_set_slave_inactive_flags(old_active);
1085 if (new_active)
1086 bond_set_slave_active_flags(new_active);
1087 } else {
1088 bond->curr_active_slave = new_active;
1091 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1092 if (old_active) {
1093 bond_set_slave_inactive_flags(old_active);
1096 if (new_active) {
1097 bond_set_slave_active_flags(new_active);
1099 bond_send_gratuitous_arp(bond);
1104 * bond_select_active_slave - select a new active slave, if needed
1105 * @bond: our bonding struct
1107 * This functions shoud be called when one of the following occurs:
1108 * - The old curr_active_slave has been released or lost its link.
1109 * - The primary_slave has got its link back.
1110 * - A slave has got its link back and there's no old curr_active_slave.
1112 * Warning: Caller must hold curr_slave_lock for writing.
1114 void bond_select_active_slave(struct bonding *bond)
1116 struct slave *best_slave;
1117 int rv;
1119 best_slave = bond_find_best_slave(bond);
1120 if (best_slave != bond->curr_active_slave) {
1121 bond_change_active_slave(bond, best_slave);
1122 rv = bond_set_carrier(bond);
1123 if (!rv)
1124 return;
1126 if (netif_carrier_ok(bond->dev)) {
1127 printk(KERN_INFO DRV_NAME
1128 ": %s: first active interface up!\n",
1129 bond->dev->name);
1130 } else {
1131 printk(KERN_INFO DRV_NAME ": %s: "
1132 "now running without any active interface !\n",
1133 bond->dev->name);
1138 /*--------------------------- slave list handling ---------------------------*/
1141 * This function attaches the slave to the end of list.
1143 * bond->lock held for writing by caller.
1145 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1147 if (bond->first_slave == NULL) { /* attaching the first slave */
1148 new_slave->next = new_slave;
1149 new_slave->prev = new_slave;
1150 bond->first_slave = new_slave;
1151 } else {
1152 new_slave->next = bond->first_slave;
1153 new_slave->prev = bond->first_slave->prev;
1154 new_slave->next->prev = new_slave;
1155 new_slave->prev->next = new_slave;
1158 bond->slave_cnt++;
1162 * This function detaches the slave from the list.
1163 * WARNING: no check is made to verify if the slave effectively
1164 * belongs to <bond>.
1165 * Nothing is freed on return, structures are just unchained.
1166 * If any slave pointer in bond was pointing to <slave>,
1167 * it should be changed by the calling function.
1169 * bond->lock held for writing by caller.
1171 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1173 if (slave->next) {
1174 slave->next->prev = slave->prev;
1177 if (slave->prev) {
1178 slave->prev->next = slave->next;
1181 if (bond->first_slave == slave) { /* slave is the first slave */
1182 if (bond->slave_cnt > 1) { /* there are more slave */
1183 bond->first_slave = slave->next;
1184 } else {
1185 bond->first_slave = NULL; /* slave was the last one */
1189 slave->next = NULL;
1190 slave->prev = NULL;
1191 bond->slave_cnt--;
1194 /*---------------------------------- IOCTL ----------------------------------*/
1196 static int bond_sethwaddr(struct net_device *bond_dev,
1197 struct net_device *slave_dev)
1199 dprintk("bond_dev=%p\n", bond_dev);
1200 dprintk("slave_dev=%p\n", slave_dev);
1201 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1202 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1203 return 0;
1206 #define BOND_VLAN_FEATURES \
1207 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1208 NETIF_F_HW_VLAN_FILTER)
1211 * Compute the common dev->feature set available to all slaves. Some
1212 * feature bits are managed elsewhere, so preserve those feature bits
1213 * on the master device.
1215 static int bond_compute_features(struct bonding *bond)
1217 struct slave *slave;
1218 struct net_device *bond_dev = bond->dev;
1219 unsigned long features = bond_dev->features;
1220 unsigned short max_hard_header_len = ETH_HLEN;
1221 int i;
1223 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1224 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1225 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1227 bond_for_each_slave(bond, slave, i) {
1228 features = netdev_compute_features(features,
1229 slave->dev->features);
1230 if (slave->dev->hard_header_len > max_hard_header_len)
1231 max_hard_header_len = slave->dev->hard_header_len;
1234 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1235 bond_dev->features = features;
1236 bond_dev->hard_header_len = max_hard_header_len;
1238 return 0;
1241 /* enslave device <slave> to bond device <master> */
1242 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1244 struct bonding *bond = bond_dev->priv;
1245 struct slave *new_slave = NULL;
1246 struct dev_mc_list *dmi;
1247 struct sockaddr addr;
1248 int link_reporting;
1249 int old_features = bond_dev->features;
1250 int res = 0;
1252 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1253 slave_dev->do_ioctl == NULL) {
1254 printk(KERN_WARNING DRV_NAME
1255 ": %s: Warning: no link monitoring support for %s\n",
1256 bond_dev->name, slave_dev->name);
1259 /* bond must be initialized by bond_open() before enslaving */
1260 if (!(bond_dev->flags & IFF_UP)) {
1261 dprintk("Error, master_dev is not up\n");
1262 return -EPERM;
1265 /* already enslaved */
1266 if (slave_dev->flags & IFF_SLAVE) {
1267 dprintk("Error, Device was already enslaved\n");
1268 return -EBUSY;
1271 /* vlan challenged mutual exclusion */
1272 /* no need to lock since we're protected by rtnl_lock */
1273 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1274 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1275 if (!list_empty(&bond->vlan_list)) {
1276 printk(KERN_ERR DRV_NAME
1277 ": %s: Error: cannot enslave VLAN "
1278 "challenged slave %s on VLAN enabled "
1279 "bond %s\n", bond_dev->name, slave_dev->name,
1280 bond_dev->name);
1281 return -EPERM;
1282 } else {
1283 printk(KERN_WARNING DRV_NAME
1284 ": %s: Warning: enslaved VLAN challenged "
1285 "slave %s. Adding VLANs will be blocked as "
1286 "long as %s is part of bond %s\n",
1287 bond_dev->name, slave_dev->name, slave_dev->name,
1288 bond_dev->name);
1289 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1291 } else {
1292 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1293 if (bond->slave_cnt == 0) {
1294 /* First slave, and it is not VLAN challenged,
1295 * so remove the block of adding VLANs over the bond.
1297 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1302 * Old ifenslave binaries are no longer supported. These can
1303 * be identified with moderate accurary by the state of the slave:
1304 * the current ifenslave will set the interface down prior to
1305 * enslaving it; the old ifenslave will not.
1307 if ((slave_dev->flags & IFF_UP)) {
1308 printk(KERN_ERR DRV_NAME ": %s is up. "
1309 "This may be due to an out of date ifenslave.\n",
1310 slave_dev->name);
1311 res = -EPERM;
1312 goto err_undo_flags;
1315 if (slave_dev->set_mac_address == NULL) {
1316 printk(KERN_ERR DRV_NAME
1317 ": %s: Error: The slave device you specified does "
1318 "not support setting the MAC address. "
1319 "Your kernel likely does not support slave "
1320 "devices.\n", bond_dev->name);
1321 res = -EOPNOTSUPP;
1322 goto err_undo_flags;
1325 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1326 if (!new_slave) {
1327 res = -ENOMEM;
1328 goto err_undo_flags;
1331 /* save slave's original flags before calling
1332 * netdev_set_master and dev_open
1334 new_slave->original_flags = slave_dev->flags;
1337 * Save slave's original ("permanent") mac address for modes
1338 * that need it, and for restoring it upon release, and then
1339 * set it to the master's address
1341 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1344 * Set slave to master's mac address. The application already
1345 * set the master's mac address to that of the first slave
1347 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1348 addr.sa_family = slave_dev->type;
1349 res = dev_set_mac_address(slave_dev, &addr);
1350 if (res) {
1351 dprintk("Error %d calling set_mac_address\n", res);
1352 goto err_free;
1355 res = netdev_set_master(slave_dev, bond_dev);
1356 if (res) {
1357 dprintk("Error %d calling netdev_set_master\n", res);
1358 goto err_close;
1360 /* open the slave since the application closed it */
1361 res = dev_open(slave_dev);
1362 if (res) {
1363 dprintk("Openning slave %s failed\n", slave_dev->name);
1364 goto err_restore_mac;
1367 new_slave->dev = slave_dev;
1368 slave_dev->priv_flags |= IFF_BONDING;
1370 if ((bond->params.mode == BOND_MODE_TLB) ||
1371 (bond->params.mode == BOND_MODE_ALB)) {
1372 /* bond_alb_init_slave() must be called before all other stages since
1373 * it might fail and we do not want to have to undo everything
1375 res = bond_alb_init_slave(bond, new_slave);
1376 if (res) {
1377 goto err_unset_master;
1381 /* If the mode USES_PRIMARY, then the new slave gets the
1382 * master's promisc (and mc) settings only if it becomes the
1383 * curr_active_slave, and that is taken care of later when calling
1384 * bond_change_active()
1386 if (!USES_PRIMARY(bond->params.mode)) {
1387 /* set promiscuity level to new slave */
1388 if (bond_dev->flags & IFF_PROMISC) {
1389 dev_set_promiscuity(slave_dev, 1);
1392 /* set allmulti level to new slave */
1393 if (bond_dev->flags & IFF_ALLMULTI) {
1394 dev_set_allmulti(slave_dev, 1);
1397 /* upload master's mc_list to new slave */
1398 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1399 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1403 if (bond->params.mode == BOND_MODE_8023AD) {
1404 /* add lacpdu mc addr to mc list */
1405 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1407 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1410 bond_add_vlans_on_slave(bond, slave_dev);
1412 write_lock_bh(&bond->lock);
1414 bond_attach_slave(bond, new_slave);
1416 new_slave->delay = 0;
1417 new_slave->link_failure_count = 0;
1419 bond_compute_features(bond);
1421 new_slave->last_arp_rx = jiffies;
1423 if (bond->params.miimon && !bond->params.use_carrier) {
1424 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1426 if ((link_reporting == -1) && !bond->params.arp_interval) {
1428 * miimon is set but a bonded network driver
1429 * does not support ETHTOOL/MII and
1430 * arp_interval is not set. Note: if
1431 * use_carrier is enabled, we will never go
1432 * here (because netif_carrier is always
1433 * supported); thus, we don't need to change
1434 * the messages for netif_carrier.
1436 printk(KERN_WARNING DRV_NAME
1437 ": %s: Warning: MII and ETHTOOL support not "
1438 "available for interface %s, and "
1439 "arp_interval/arp_ip_target module parameters "
1440 "not specified, thus bonding will not detect "
1441 "link failures! see bonding.txt for details.\n",
1442 bond_dev->name, slave_dev->name);
1443 } else if (link_reporting == -1) {
1444 /* unable get link status using mii/ethtool */
1445 printk(KERN_WARNING DRV_NAME
1446 ": %s: Warning: can't get link status from "
1447 "interface %s; the network driver associated "
1448 "with this interface does not support MII or "
1449 "ETHTOOL link status reporting, thus miimon "
1450 "has no effect on this interface.\n",
1451 bond_dev->name, slave_dev->name);
1455 /* check for initial state */
1456 if (!bond->params.miimon ||
1457 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1458 if (bond->params.updelay) {
1459 dprintk("Initial state of slave_dev is "
1460 "BOND_LINK_BACK\n");
1461 new_slave->link = BOND_LINK_BACK;
1462 new_slave->delay = bond->params.updelay;
1463 } else {
1464 dprintk("Initial state of slave_dev is "
1465 "BOND_LINK_UP\n");
1466 new_slave->link = BOND_LINK_UP;
1468 new_slave->jiffies = jiffies;
1469 } else {
1470 dprintk("Initial state of slave_dev is "
1471 "BOND_LINK_DOWN\n");
1472 new_slave->link = BOND_LINK_DOWN;
1475 if (bond_update_speed_duplex(new_slave) &&
1476 (new_slave->link != BOND_LINK_DOWN)) {
1477 printk(KERN_WARNING DRV_NAME
1478 ": %s: Warning: failed to get speed and duplex from %s, "
1479 "assumed to be 100Mb/sec and Full.\n",
1480 bond_dev->name, new_slave->dev->name);
1482 if (bond->params.mode == BOND_MODE_8023AD) {
1483 printk(KERN_WARNING DRV_NAME
1484 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1485 "support in base driver for proper aggregator "
1486 "selection.\n", bond_dev->name);
1490 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1491 /* if there is a primary slave, remember it */
1492 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1493 bond->primary_slave = new_slave;
1497 switch (bond->params.mode) {
1498 case BOND_MODE_ACTIVEBACKUP:
1499 bond_set_slave_inactive_flags(new_slave);
1500 bond_select_active_slave(bond);
1501 break;
1502 case BOND_MODE_8023AD:
1503 /* in 802.3ad mode, the internal mechanism
1504 * will activate the slaves in the selected
1505 * aggregator
1507 bond_set_slave_inactive_flags(new_slave);
1508 /* if this is the first slave */
1509 if (bond->slave_cnt == 1) {
1510 SLAVE_AD_INFO(new_slave).id = 1;
1511 /* Initialize AD with the number of times that the AD timer is called in 1 second
1512 * can be called only after the mac address of the bond is set
1514 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1515 bond->params.lacp_fast);
1516 } else {
1517 SLAVE_AD_INFO(new_slave).id =
1518 SLAVE_AD_INFO(new_slave->prev).id + 1;
1521 bond_3ad_bind_slave(new_slave);
1522 break;
1523 case BOND_MODE_TLB:
1524 case BOND_MODE_ALB:
1525 new_slave->state = BOND_STATE_ACTIVE;
1526 if ((!bond->curr_active_slave) &&
1527 (new_slave->link != BOND_LINK_DOWN)) {
1528 /* first slave or no active slave yet, and this link
1529 * is OK, so make this interface the active one
1531 bond_change_active_slave(bond, new_slave);
1532 } else {
1533 bond_set_slave_inactive_flags(new_slave);
1535 break;
1536 default:
1537 dprintk("This slave is always active in trunk mode\n");
1539 /* always active in trunk mode */
1540 new_slave->state = BOND_STATE_ACTIVE;
1542 /* In trunking mode there is little meaning to curr_active_slave
1543 * anyway (it holds no special properties of the bond device),
1544 * so we can change it without calling change_active_interface()
1546 if (!bond->curr_active_slave) {
1547 bond->curr_active_slave = new_slave;
1549 break;
1550 } /* switch(bond_mode) */
1552 bond_set_carrier(bond);
1554 write_unlock_bh(&bond->lock);
1556 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1557 if (res)
1558 goto err_unset_master;
1560 printk(KERN_INFO DRV_NAME
1561 ": %s: enslaving %s as a%s interface with a%s link.\n",
1562 bond_dev->name, slave_dev->name,
1563 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1564 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1566 /* enslave is successful */
1567 return 0;
1569 /* Undo stages on error */
1570 err_unset_master:
1571 netdev_set_master(slave_dev, NULL);
1573 err_close:
1574 dev_close(slave_dev);
1576 err_restore_mac:
1577 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1578 addr.sa_family = slave_dev->type;
1579 dev_set_mac_address(slave_dev, &addr);
1581 err_free:
1582 kfree(new_slave);
1584 err_undo_flags:
1585 bond_dev->features = old_features;
1587 return res;
1591 * Try to release the slave device <slave> from the bond device <master>
1592 * It is legal to access curr_active_slave without a lock because all the function
1593 * is write-locked.
1595 * The rules for slave state should be:
1596 * for Active/Backup:
1597 * Active stays on all backups go down
1598 * for Bonded connections:
1599 * The first up interface should be left on and all others downed.
1601 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1603 struct bonding *bond = bond_dev->priv;
1604 struct slave *slave, *oldcurrent;
1605 struct sockaddr addr;
1606 int mac_addr_differ;
1608 /* slave is not a slave or master is not master of this slave */
1609 if (!(slave_dev->flags & IFF_SLAVE) ||
1610 (slave_dev->master != bond_dev)) {
1611 printk(KERN_ERR DRV_NAME
1612 ": %s: Error: cannot release %s.\n",
1613 bond_dev->name, slave_dev->name);
1614 return -EINVAL;
1617 write_lock_bh(&bond->lock);
1619 slave = bond_get_slave_by_dev(bond, slave_dev);
1620 if (!slave) {
1621 /* not a slave of this bond */
1622 printk(KERN_INFO DRV_NAME
1623 ": %s: %s not enslaved\n",
1624 bond_dev->name, slave_dev->name);
1625 write_unlock_bh(&bond->lock);
1626 return -EINVAL;
1629 mac_addr_differ = memcmp(bond_dev->dev_addr,
1630 slave->perm_hwaddr,
1631 ETH_ALEN);
1632 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1633 printk(KERN_WARNING DRV_NAME
1634 ": %s: Warning: the permanent HWaddr of %s "
1635 "- %02X:%02X:%02X:%02X:%02X:%02X - is "
1636 "still in use by %s. Set the HWaddr of "
1637 "%s to a different address to avoid "
1638 "conflicts.\n",
1639 bond_dev->name,
1640 slave_dev->name,
1641 slave->perm_hwaddr[0],
1642 slave->perm_hwaddr[1],
1643 slave->perm_hwaddr[2],
1644 slave->perm_hwaddr[3],
1645 slave->perm_hwaddr[4],
1646 slave->perm_hwaddr[5],
1647 bond_dev->name,
1648 slave_dev->name);
1651 /* Inform AD package of unbinding of slave. */
1652 if (bond->params.mode == BOND_MODE_8023AD) {
1653 /* must be called before the slave is
1654 * detached from the list
1656 bond_3ad_unbind_slave(slave);
1659 printk(KERN_INFO DRV_NAME
1660 ": %s: releasing %s interface %s\n",
1661 bond_dev->name,
1662 (slave->state == BOND_STATE_ACTIVE)
1663 ? "active" : "backup",
1664 slave_dev->name);
1666 oldcurrent = bond->curr_active_slave;
1668 bond->current_arp_slave = NULL;
1670 /* release the slave from its bond */
1671 bond_detach_slave(bond, slave);
1673 bond_compute_features(bond);
1675 if (bond->primary_slave == slave) {
1676 bond->primary_slave = NULL;
1679 if (oldcurrent == slave) {
1680 bond_change_active_slave(bond, NULL);
1683 if ((bond->params.mode == BOND_MODE_TLB) ||
1684 (bond->params.mode == BOND_MODE_ALB)) {
1685 /* Must be called only after the slave has been
1686 * detached from the list and the curr_active_slave
1687 * has been cleared (if our_slave == old_current),
1688 * but before a new active slave is selected.
1690 bond_alb_deinit_slave(bond, slave);
1693 if (oldcurrent == slave)
1694 bond_select_active_slave(bond);
1696 if (bond->slave_cnt == 0) {
1697 bond_set_carrier(bond);
1699 /* if the last slave was removed, zero the mac address
1700 * of the master so it will be set by the application
1701 * to the mac address of the first slave
1703 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1705 if (list_empty(&bond->vlan_list)) {
1706 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1707 } else {
1708 printk(KERN_WARNING DRV_NAME
1709 ": %s: Warning: clearing HW address of %s while it "
1710 "still has VLANs.\n",
1711 bond_dev->name, bond_dev->name);
1712 printk(KERN_WARNING DRV_NAME
1713 ": %s: When re-adding slaves, make sure the bond's "
1714 "HW address matches its VLANs'.\n",
1715 bond_dev->name);
1717 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1718 !bond_has_challenged_slaves(bond)) {
1719 printk(KERN_INFO DRV_NAME
1720 ": %s: last VLAN challenged slave %s "
1721 "left bond %s. VLAN blocking is removed\n",
1722 bond_dev->name, slave_dev->name, bond_dev->name);
1723 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1726 write_unlock_bh(&bond->lock);
1728 /* must do this from outside any spinlocks */
1729 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1731 bond_del_vlans_from_slave(bond, slave_dev);
1733 /* If the mode USES_PRIMARY, then we should only remove its
1734 * promisc and mc settings if it was the curr_active_slave, but that was
1735 * already taken care of above when we detached the slave
1737 if (!USES_PRIMARY(bond->params.mode)) {
1738 /* unset promiscuity level from slave */
1739 if (bond_dev->flags & IFF_PROMISC) {
1740 dev_set_promiscuity(slave_dev, -1);
1743 /* unset allmulti level from slave */
1744 if (bond_dev->flags & IFF_ALLMULTI) {
1745 dev_set_allmulti(slave_dev, -1);
1748 /* flush master's mc_list from slave */
1749 bond_mc_list_flush(bond_dev, slave_dev);
1752 netdev_set_master(slave_dev, NULL);
1754 /* close slave before restoring its mac address */
1755 dev_close(slave_dev);
1757 /* restore original ("permanent") mac address */
1758 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1759 addr.sa_family = slave_dev->type;
1760 dev_set_mac_address(slave_dev, &addr);
1762 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1763 IFF_SLAVE_INACTIVE | IFF_BONDING |
1764 IFF_SLAVE_NEEDARP);
1766 kfree(slave);
1768 return 0; /* deletion OK */
1772 * This function releases all slaves.
1774 static int bond_release_all(struct net_device *bond_dev)
1776 struct bonding *bond = bond_dev->priv;
1777 struct slave *slave;
1778 struct net_device *slave_dev;
1779 struct sockaddr addr;
1781 write_lock_bh(&bond->lock);
1783 netif_carrier_off(bond_dev);
1785 if (bond->slave_cnt == 0) {
1786 goto out;
1789 bond->current_arp_slave = NULL;
1790 bond->primary_slave = NULL;
1791 bond_change_active_slave(bond, NULL);
1793 while ((slave = bond->first_slave) != NULL) {
1794 /* Inform AD package of unbinding of slave
1795 * before slave is detached from the list.
1797 if (bond->params.mode == BOND_MODE_8023AD) {
1798 bond_3ad_unbind_slave(slave);
1801 slave_dev = slave->dev;
1802 bond_detach_slave(bond, slave);
1804 if ((bond->params.mode == BOND_MODE_TLB) ||
1805 (bond->params.mode == BOND_MODE_ALB)) {
1806 /* must be called only after the slave
1807 * has been detached from the list
1809 bond_alb_deinit_slave(bond, slave);
1812 bond_compute_features(bond);
1814 /* now that the slave is detached, unlock and perform
1815 * all the undo steps that should not be called from
1816 * within a lock.
1818 write_unlock_bh(&bond->lock);
1820 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1821 bond_del_vlans_from_slave(bond, slave_dev);
1823 /* If the mode USES_PRIMARY, then we should only remove its
1824 * promisc and mc settings if it was the curr_active_slave, but that was
1825 * already taken care of above when we detached the slave
1827 if (!USES_PRIMARY(bond->params.mode)) {
1828 /* unset promiscuity level from slave */
1829 if (bond_dev->flags & IFF_PROMISC) {
1830 dev_set_promiscuity(slave_dev, -1);
1833 /* unset allmulti level from slave */
1834 if (bond_dev->flags & IFF_ALLMULTI) {
1835 dev_set_allmulti(slave_dev, -1);
1838 /* flush master's mc_list from slave */
1839 bond_mc_list_flush(bond_dev, slave_dev);
1842 netdev_set_master(slave_dev, NULL);
1844 /* close slave before restoring its mac address */
1845 dev_close(slave_dev);
1847 /* restore original ("permanent") mac address*/
1848 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1849 addr.sa_family = slave_dev->type;
1850 dev_set_mac_address(slave_dev, &addr);
1852 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1853 IFF_SLAVE_INACTIVE);
1855 kfree(slave);
1857 /* re-acquire the lock before getting the next slave */
1858 write_lock_bh(&bond->lock);
1861 /* zero the mac address of the master so it will be
1862 * set by the application to the mac address of the
1863 * first slave
1865 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1867 if (list_empty(&bond->vlan_list)) {
1868 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1869 } else {
1870 printk(KERN_WARNING DRV_NAME
1871 ": %s: Warning: clearing HW address of %s while it "
1872 "still has VLANs.\n",
1873 bond_dev->name, bond_dev->name);
1874 printk(KERN_WARNING DRV_NAME
1875 ": %s: When re-adding slaves, make sure the bond's "
1876 "HW address matches its VLANs'.\n",
1877 bond_dev->name);
1880 printk(KERN_INFO DRV_NAME
1881 ": %s: released all slaves\n",
1882 bond_dev->name);
1884 out:
1885 write_unlock_bh(&bond->lock);
1887 return 0;
1891 * This function changes the active slave to slave <slave_dev>.
1892 * It returns -EINVAL in the following cases.
1893 * - <slave_dev> is not found in the list.
1894 * - There is not active slave now.
1895 * - <slave_dev> is already active.
1896 * - The link state of <slave_dev> is not BOND_LINK_UP.
1897 * - <slave_dev> is not running.
1898 * In these cases, this fuction does nothing.
1899 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1901 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1903 struct bonding *bond = bond_dev->priv;
1904 struct slave *old_active = NULL;
1905 struct slave *new_active = NULL;
1906 int res = 0;
1908 if (!USES_PRIMARY(bond->params.mode)) {
1909 return -EINVAL;
1912 /* Verify that master_dev is indeed the master of slave_dev */
1913 if (!(slave_dev->flags & IFF_SLAVE) ||
1914 (slave_dev->master != bond_dev)) {
1915 return -EINVAL;
1918 write_lock_bh(&bond->lock);
1920 old_active = bond->curr_active_slave;
1921 new_active = bond_get_slave_by_dev(bond, slave_dev);
1924 * Changing to the current active: do nothing; return success.
1926 if (new_active && (new_active == old_active)) {
1927 write_unlock_bh(&bond->lock);
1928 return 0;
1931 if ((new_active) &&
1932 (old_active) &&
1933 (new_active->link == BOND_LINK_UP) &&
1934 IS_UP(new_active->dev)) {
1935 bond_change_active_slave(bond, new_active);
1936 } else {
1937 res = -EINVAL;
1940 write_unlock_bh(&bond->lock);
1942 return res;
1945 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1947 struct bonding *bond = bond_dev->priv;
1949 info->bond_mode = bond->params.mode;
1950 info->miimon = bond->params.miimon;
1952 read_lock_bh(&bond->lock);
1953 info->num_slaves = bond->slave_cnt;
1954 read_unlock_bh(&bond->lock);
1956 return 0;
1959 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
1961 struct bonding *bond = bond_dev->priv;
1962 struct slave *slave;
1963 int i, found = 0;
1965 if (info->slave_id < 0) {
1966 return -ENODEV;
1969 read_lock_bh(&bond->lock);
1971 bond_for_each_slave(bond, slave, i) {
1972 if (i == (int)info->slave_id) {
1973 found = 1;
1974 break;
1978 read_unlock_bh(&bond->lock);
1980 if (found) {
1981 strcpy(info->slave_name, slave->dev->name);
1982 info->link = slave->link;
1983 info->state = slave->state;
1984 info->link_failure_count = slave->link_failure_count;
1985 } else {
1986 return -ENODEV;
1989 return 0;
1992 /*-------------------------------- Monitoring -------------------------------*/
1994 /* this function is called regularly to monitor each slave's link. */
1995 void bond_mii_monitor(struct net_device *bond_dev)
1997 struct bonding *bond = bond_dev->priv;
1998 struct slave *slave, *oldcurrent;
1999 int do_failover = 0;
2000 int delta_in_ticks;
2001 int i;
2003 read_lock(&bond->lock);
2005 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2007 if (bond->kill_timers) {
2008 goto out;
2011 if (bond->slave_cnt == 0) {
2012 goto re_arm;
2015 /* we will try to read the link status of each of our slaves, and
2016 * set their IFF_RUNNING flag appropriately. For each slave not
2017 * supporting MII status, we won't do anything so that a user-space
2018 * program could monitor the link itself if needed.
2021 read_lock(&bond->curr_slave_lock);
2022 oldcurrent = bond->curr_active_slave;
2023 read_unlock(&bond->curr_slave_lock);
2025 bond_for_each_slave(bond, slave, i) {
2026 struct net_device *slave_dev = slave->dev;
2027 int link_state;
2028 u16 old_speed = slave->speed;
2029 u8 old_duplex = slave->duplex;
2031 link_state = bond_check_dev_link(bond, slave_dev, 0);
2033 switch (slave->link) {
2034 case BOND_LINK_UP: /* the link was up */
2035 if (link_state == BMSR_LSTATUS) {
2036 /* link stays up, nothing more to do */
2037 break;
2038 } else { /* link going down */
2039 slave->link = BOND_LINK_FAIL;
2040 slave->delay = bond->params.downdelay;
2042 if (slave->link_failure_count < UINT_MAX) {
2043 slave->link_failure_count++;
2046 if (bond->params.downdelay) {
2047 printk(KERN_INFO DRV_NAME
2048 ": %s: link status down for %s "
2049 "interface %s, disabling it in "
2050 "%d ms.\n",
2051 bond_dev->name,
2052 IS_UP(slave_dev)
2053 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2054 ? ((slave == oldcurrent)
2055 ? "active " : "backup ")
2056 : "")
2057 : "idle ",
2058 slave_dev->name,
2059 bond->params.downdelay * bond->params.miimon);
2062 /* no break ! fall through the BOND_LINK_FAIL test to
2063 ensure proper action to be taken
2065 case BOND_LINK_FAIL: /* the link has just gone down */
2066 if (link_state != BMSR_LSTATUS) {
2067 /* link stays down */
2068 if (slave->delay <= 0) {
2069 /* link down for too long time */
2070 slave->link = BOND_LINK_DOWN;
2072 /* in active/backup mode, we must
2073 * completely disable this interface
2075 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2076 (bond->params.mode == BOND_MODE_8023AD)) {
2077 bond_set_slave_inactive_flags(slave);
2080 printk(KERN_INFO DRV_NAME
2081 ": %s: link status definitely "
2082 "down for interface %s, "
2083 "disabling it\n",
2084 bond_dev->name,
2085 slave_dev->name);
2087 /* notify ad that the link status has changed */
2088 if (bond->params.mode == BOND_MODE_8023AD) {
2089 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2092 if ((bond->params.mode == BOND_MODE_TLB) ||
2093 (bond->params.mode == BOND_MODE_ALB)) {
2094 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2097 if (slave == oldcurrent) {
2098 do_failover = 1;
2100 } else {
2101 slave->delay--;
2103 } else {
2104 /* link up again */
2105 slave->link = BOND_LINK_UP;
2106 slave->jiffies = jiffies;
2107 printk(KERN_INFO DRV_NAME
2108 ": %s: link status up again after %d "
2109 "ms for interface %s.\n",
2110 bond_dev->name,
2111 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2112 slave_dev->name);
2114 break;
2115 case BOND_LINK_DOWN: /* the link was down */
2116 if (link_state != BMSR_LSTATUS) {
2117 /* the link stays down, nothing more to do */
2118 break;
2119 } else { /* link going up */
2120 slave->link = BOND_LINK_BACK;
2121 slave->delay = bond->params.updelay;
2123 if (bond->params.updelay) {
2124 /* if updelay == 0, no need to
2125 advertise about a 0 ms delay */
2126 printk(KERN_INFO DRV_NAME
2127 ": %s: link status up for "
2128 "interface %s, enabling it "
2129 "in %d ms.\n",
2130 bond_dev->name,
2131 slave_dev->name,
2132 bond->params.updelay * bond->params.miimon);
2135 /* no break ! fall through the BOND_LINK_BACK state in
2136 case there's something to do.
2138 case BOND_LINK_BACK: /* the link has just come back */
2139 if (link_state != BMSR_LSTATUS) {
2140 /* link down again */
2141 slave->link = BOND_LINK_DOWN;
2143 printk(KERN_INFO DRV_NAME
2144 ": %s: link status down again after %d "
2145 "ms for interface %s.\n",
2146 bond_dev->name,
2147 (bond->params.updelay - slave->delay) * bond->params.miimon,
2148 slave_dev->name);
2149 } else {
2150 /* link stays up */
2151 if (slave->delay == 0) {
2152 /* now the link has been up for long time enough */
2153 slave->link = BOND_LINK_UP;
2154 slave->jiffies = jiffies;
2156 if (bond->params.mode == BOND_MODE_8023AD) {
2157 /* prevent it from being the active one */
2158 slave->state = BOND_STATE_BACKUP;
2159 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2160 /* make it immediately active */
2161 slave->state = BOND_STATE_ACTIVE;
2162 } else if (slave != bond->primary_slave) {
2163 /* prevent it from being the active one */
2164 slave->state = BOND_STATE_BACKUP;
2167 printk(KERN_INFO DRV_NAME
2168 ": %s: link status definitely "
2169 "up for interface %s.\n",
2170 bond_dev->name,
2171 slave_dev->name);
2173 /* notify ad that the link status has changed */
2174 if (bond->params.mode == BOND_MODE_8023AD) {
2175 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2178 if ((bond->params.mode == BOND_MODE_TLB) ||
2179 (bond->params.mode == BOND_MODE_ALB)) {
2180 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2183 if ((!oldcurrent) ||
2184 (slave == bond->primary_slave)) {
2185 do_failover = 1;
2187 } else {
2188 slave->delay--;
2191 break;
2192 default:
2193 /* Should not happen */
2194 printk(KERN_ERR DRV_NAME
2195 ": %s: Error: %s Illegal value (link=%d)\n",
2196 bond_dev->name,
2197 slave->dev->name,
2198 slave->link);
2199 goto out;
2200 } /* end of switch (slave->link) */
2202 bond_update_speed_duplex(slave);
2204 if (bond->params.mode == BOND_MODE_8023AD) {
2205 if (old_speed != slave->speed) {
2206 bond_3ad_adapter_speed_changed(slave);
2209 if (old_duplex != slave->duplex) {
2210 bond_3ad_adapter_duplex_changed(slave);
2214 } /* end of for */
2216 if (do_failover) {
2217 write_lock(&bond->curr_slave_lock);
2219 bond_select_active_slave(bond);
2221 write_unlock(&bond->curr_slave_lock);
2222 } else
2223 bond_set_carrier(bond);
2225 re_arm:
2226 if (bond->params.miimon) {
2227 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2229 out:
2230 read_unlock(&bond->lock);
2234 static u32 bond_glean_dev_ip(struct net_device *dev)
2236 struct in_device *idev;
2237 struct in_ifaddr *ifa;
2238 __be32 addr = 0;
2240 if (!dev)
2241 return 0;
2243 rcu_read_lock();
2244 idev = __in_dev_get_rcu(dev);
2245 if (!idev)
2246 goto out;
2248 ifa = idev->ifa_list;
2249 if (!ifa)
2250 goto out;
2252 addr = ifa->ifa_local;
2253 out:
2254 rcu_read_unlock();
2255 return addr;
2258 static int bond_has_ip(struct bonding *bond)
2260 struct vlan_entry *vlan, *vlan_next;
2262 if (bond->master_ip)
2263 return 1;
2265 if (list_empty(&bond->vlan_list))
2266 return 0;
2268 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2269 vlan_list) {
2270 if (vlan->vlan_ip)
2271 return 1;
2274 return 0;
2277 static int bond_has_this_ip(struct bonding *bond, u32 ip)
2279 struct vlan_entry *vlan, *vlan_next;
2281 if (ip == bond->master_ip)
2282 return 1;
2284 if (list_empty(&bond->vlan_list))
2285 return 0;
2287 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2288 vlan_list) {
2289 if (ip == vlan->vlan_ip)
2290 return 1;
2293 return 0;
2297 * We go to the (large) trouble of VLAN tagging ARP frames because
2298 * switches in VLAN mode (especially if ports are configured as
2299 * "native" to a VLAN) might not pass non-tagged frames.
2301 static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip, u32 src_ip, unsigned short vlan_id)
2303 struct sk_buff *skb;
2305 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2306 slave_dev->name, dest_ip, src_ip, vlan_id);
2308 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2309 NULL, slave_dev->dev_addr, NULL);
2311 if (!skb) {
2312 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2313 return;
2315 if (vlan_id) {
2316 skb = vlan_put_tag(skb, vlan_id);
2317 if (!skb) {
2318 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2319 return;
2322 arp_xmit(skb);
2326 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2328 int i, vlan_id, rv;
2329 u32 *targets = bond->params.arp_targets;
2330 struct vlan_entry *vlan, *vlan_next;
2331 struct net_device *vlan_dev;
2332 struct flowi fl;
2333 struct rtable *rt;
2335 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2336 if (!targets[i])
2337 continue;
2338 dprintk("basa: target %x\n", targets[i]);
2339 if (list_empty(&bond->vlan_list)) {
2340 dprintk("basa: empty vlan: arp_send\n");
2341 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2342 bond->master_ip, 0);
2343 continue;
2347 * If VLANs are configured, we do a route lookup to
2348 * determine which VLAN interface would be used, so we
2349 * can tag the ARP with the proper VLAN tag.
2351 memset(&fl, 0, sizeof(fl));
2352 fl.fl4_dst = targets[i];
2353 fl.fl4_tos = RTO_ONLINK;
2355 rv = ip_route_output_key(&rt, &fl);
2356 if (rv) {
2357 if (net_ratelimit()) {
2358 printk(KERN_WARNING DRV_NAME
2359 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2360 bond->dev->name, NIPQUAD(fl.fl4_dst));
2362 continue;
2366 * This target is not on a VLAN
2368 if (rt->u.dst.dev == bond->dev) {
2369 ip_rt_put(rt);
2370 dprintk("basa: rtdev == bond->dev: arp_send\n");
2371 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2372 bond->master_ip, 0);
2373 continue;
2376 vlan_id = 0;
2377 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2378 vlan_list) {
2379 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2380 if (vlan_dev == rt->u.dst.dev) {
2381 vlan_id = vlan->vlan_id;
2382 dprintk("basa: vlan match on %s %d\n",
2383 vlan_dev->name, vlan_id);
2384 break;
2388 if (vlan_id) {
2389 ip_rt_put(rt);
2390 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2391 vlan->vlan_ip, vlan_id);
2392 continue;
2395 if (net_ratelimit()) {
2396 printk(KERN_WARNING DRV_NAME
2397 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2398 bond->dev->name, NIPQUAD(fl.fl4_dst),
2399 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2401 ip_rt_put(rt);
2406 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2407 * for each VLAN above us.
2409 static void bond_send_gratuitous_arp(struct bonding *bond)
2411 struct slave *slave = bond->curr_active_slave;
2412 struct vlan_entry *vlan;
2413 struct net_device *vlan_dev;
2415 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2416 slave ? slave->dev->name : "NULL");
2417 if (!slave)
2418 return;
2420 if (bond->master_ip) {
2421 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2422 bond->master_ip, 0);
2425 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2426 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2427 if (vlan->vlan_ip) {
2428 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2429 vlan->vlan_ip, vlan->vlan_id);
2434 static void bond_validate_arp(struct bonding *bond, struct slave *slave, u32 sip, u32 tip)
2436 int i;
2437 u32 *targets = bond->params.arp_targets;
2439 targets = bond->params.arp_targets;
2440 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2441 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2442 "%u.%u.%u.%u bhti(tip) %d\n",
2443 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2444 bond_has_this_ip(bond, tip));
2445 if (sip == targets[i]) {
2446 if (bond_has_this_ip(bond, tip))
2447 slave->last_arp_rx = jiffies;
2448 return;
2453 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2455 struct arphdr *arp;
2456 struct slave *slave;
2457 struct bonding *bond;
2458 unsigned char *arp_ptr;
2459 u32 sip, tip;
2461 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2462 goto out;
2464 bond = dev->priv;
2465 read_lock(&bond->lock);
2467 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2468 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2469 orig_dev ? orig_dev->name : "NULL");
2471 slave = bond_get_slave_by_dev(bond, orig_dev);
2472 if (!slave || !slave_do_arp_validate(bond, slave))
2473 goto out_unlock;
2475 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2476 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2477 (2 * dev->addr_len) +
2478 (2 * sizeof(u32)))))
2479 goto out_unlock;
2481 arp = arp_hdr(skb);
2482 if (arp->ar_hln != dev->addr_len ||
2483 skb->pkt_type == PACKET_OTHERHOST ||
2484 skb->pkt_type == PACKET_LOOPBACK ||
2485 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2486 arp->ar_pro != htons(ETH_P_IP) ||
2487 arp->ar_pln != 4)
2488 goto out_unlock;
2490 arp_ptr = (unsigned char *)(arp + 1);
2491 arp_ptr += dev->addr_len;
2492 memcpy(&sip, arp_ptr, 4);
2493 arp_ptr += 4 + dev->addr_len;
2494 memcpy(&tip, arp_ptr, 4);
2496 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2497 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2498 slave->state, bond->params.arp_validate,
2499 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2502 * Backup slaves won't see the ARP reply, but do come through
2503 * here for each ARP probe (so we swap the sip/tip to validate
2504 * the probe). In a "redundant switch, common router" type of
2505 * configuration, the ARP probe will (hopefully) travel from
2506 * the active, through one switch, the router, then the other
2507 * switch before reaching the backup.
2509 if (slave->state == BOND_STATE_ACTIVE)
2510 bond_validate_arp(bond, slave, sip, tip);
2511 else
2512 bond_validate_arp(bond, slave, tip, sip);
2514 out_unlock:
2515 read_unlock(&bond->lock);
2516 out:
2517 dev_kfree_skb(skb);
2518 return NET_RX_SUCCESS;
2522 * this function is called regularly to monitor each slave's link
2523 * ensuring that traffic is being sent and received when arp monitoring
2524 * is used in load-balancing mode. if the adapter has been dormant, then an
2525 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2526 * arp monitoring in active backup mode.
2528 void bond_loadbalance_arp_mon(struct net_device *bond_dev)
2530 struct bonding *bond = bond_dev->priv;
2531 struct slave *slave, *oldcurrent;
2532 int do_failover = 0;
2533 int delta_in_ticks;
2534 int i;
2536 read_lock(&bond->lock);
2538 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2540 if (bond->kill_timers) {
2541 goto out;
2544 if (bond->slave_cnt == 0) {
2545 goto re_arm;
2548 read_lock(&bond->curr_slave_lock);
2549 oldcurrent = bond->curr_active_slave;
2550 read_unlock(&bond->curr_slave_lock);
2552 /* see if any of the previous devices are up now (i.e. they have
2553 * xmt and rcv traffic). the curr_active_slave does not come into
2554 * the picture unless it is null. also, slave->jiffies is not needed
2555 * here because we send an arp on each slave and give a slave as
2556 * long as it needs to get the tx/rx within the delta.
2557 * TODO: what about up/down delay in arp mode? it wasn't here before
2558 * so it can wait
2560 bond_for_each_slave(bond, slave, i) {
2561 if (slave->link != BOND_LINK_UP) {
2562 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2563 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2565 slave->link = BOND_LINK_UP;
2566 slave->state = BOND_STATE_ACTIVE;
2568 /* primary_slave has no meaning in round-robin
2569 * mode. the window of a slave being up and
2570 * curr_active_slave being null after enslaving
2571 * is closed.
2573 if (!oldcurrent) {
2574 printk(KERN_INFO DRV_NAME
2575 ": %s: link status definitely "
2576 "up for interface %s, ",
2577 bond_dev->name,
2578 slave->dev->name);
2579 do_failover = 1;
2580 } else {
2581 printk(KERN_INFO DRV_NAME
2582 ": %s: interface %s is now up\n",
2583 bond_dev->name,
2584 slave->dev->name);
2587 } else {
2588 /* slave->link == BOND_LINK_UP */
2590 /* not all switches will respond to an arp request
2591 * when the source ip is 0, so don't take the link down
2592 * if we don't know our ip yet
2594 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2595 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2596 bond_has_ip(bond))) {
2598 slave->link = BOND_LINK_DOWN;
2599 slave->state = BOND_STATE_BACKUP;
2601 if (slave->link_failure_count < UINT_MAX) {
2602 slave->link_failure_count++;
2605 printk(KERN_INFO DRV_NAME
2606 ": %s: interface %s is now down.\n",
2607 bond_dev->name,
2608 slave->dev->name);
2610 if (slave == oldcurrent) {
2611 do_failover = 1;
2616 /* note: if switch is in round-robin mode, all links
2617 * must tx arp to ensure all links rx an arp - otherwise
2618 * links may oscillate or not come up at all; if switch is
2619 * in something like xor mode, there is nothing we can
2620 * do - all replies will be rx'ed on same link causing slaves
2621 * to be unstable during low/no traffic periods
2623 if (IS_UP(slave->dev)) {
2624 bond_arp_send_all(bond, slave);
2628 if (do_failover) {
2629 write_lock(&bond->curr_slave_lock);
2631 bond_select_active_slave(bond);
2633 write_unlock(&bond->curr_slave_lock);
2636 re_arm:
2637 if (bond->params.arp_interval) {
2638 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2640 out:
2641 read_unlock(&bond->lock);
2645 * When using arp monitoring in active-backup mode, this function is
2646 * called to determine if any backup slaves have went down or a new
2647 * current slave needs to be found.
2648 * The backup slaves never generate traffic, they are considered up by merely
2649 * receiving traffic. If the current slave goes down, each backup slave will
2650 * be given the opportunity to tx/rx an arp before being taken down - this
2651 * prevents all slaves from being taken down due to the current slave not
2652 * sending any traffic for the backups to receive. The arps are not necessarily
2653 * necessary, any tx and rx traffic will keep the current slave up. While any
2654 * rx traffic will keep the backup slaves up, the current slave is responsible
2655 * for generating traffic to keep them up regardless of any other traffic they
2656 * may have received.
2657 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2659 void bond_activebackup_arp_mon(struct net_device *bond_dev)
2661 struct bonding *bond = bond_dev->priv;
2662 struct slave *slave;
2663 int delta_in_ticks;
2664 int i;
2666 read_lock(&bond->lock);
2668 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2670 if (bond->kill_timers) {
2671 goto out;
2674 if (bond->slave_cnt == 0) {
2675 goto re_arm;
2678 /* determine if any slave has come up or any backup slave has
2679 * gone down
2680 * TODO: what about up/down delay in arp mode? it wasn't here before
2681 * so it can wait
2683 bond_for_each_slave(bond, slave, i) {
2684 if (slave->link != BOND_LINK_UP) {
2685 if ((jiffies - slave_last_rx(bond, slave)) <=
2686 delta_in_ticks) {
2688 slave->link = BOND_LINK_UP;
2690 write_lock(&bond->curr_slave_lock);
2692 if ((!bond->curr_active_slave) &&
2693 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2694 bond_change_active_slave(bond, slave);
2695 bond->current_arp_slave = NULL;
2696 } else if (bond->curr_active_slave != slave) {
2697 /* this slave has just come up but we
2698 * already have a current slave; this
2699 * can also happen if bond_enslave adds
2700 * a new slave that is up while we are
2701 * searching for a new slave
2703 bond_set_slave_inactive_flags(slave);
2704 bond->current_arp_slave = NULL;
2707 bond_set_carrier(bond);
2709 if (slave == bond->curr_active_slave) {
2710 printk(KERN_INFO DRV_NAME
2711 ": %s: %s is up and now the "
2712 "active interface\n",
2713 bond_dev->name,
2714 slave->dev->name);
2715 netif_carrier_on(bond->dev);
2716 } else {
2717 printk(KERN_INFO DRV_NAME
2718 ": %s: backup interface %s is "
2719 "now up\n",
2720 bond_dev->name,
2721 slave->dev->name);
2724 write_unlock(&bond->curr_slave_lock);
2726 } else {
2727 read_lock(&bond->curr_slave_lock);
2729 if ((slave != bond->curr_active_slave) &&
2730 (!bond->current_arp_slave) &&
2731 (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
2732 bond_has_ip(bond))) {
2733 /* a backup slave has gone down; three times
2734 * the delta allows the current slave to be
2735 * taken out before the backup slave.
2736 * note: a non-null current_arp_slave indicates
2737 * the curr_active_slave went down and we are
2738 * searching for a new one; under this
2739 * condition we only take the curr_active_slave
2740 * down - this gives each slave a chance to
2741 * tx/rx traffic before being taken out
2744 read_unlock(&bond->curr_slave_lock);
2746 slave->link = BOND_LINK_DOWN;
2748 if (slave->link_failure_count < UINT_MAX) {
2749 slave->link_failure_count++;
2752 bond_set_slave_inactive_flags(slave);
2754 printk(KERN_INFO DRV_NAME
2755 ": %s: backup interface %s is now down\n",
2756 bond_dev->name,
2757 slave->dev->name);
2758 } else {
2759 read_unlock(&bond->curr_slave_lock);
2764 read_lock(&bond->curr_slave_lock);
2765 slave = bond->curr_active_slave;
2766 read_unlock(&bond->curr_slave_lock);
2768 if (slave) {
2769 /* if we have sent traffic in the past 2*arp_intervals but
2770 * haven't xmit and rx traffic in that time interval, select
2771 * a different slave. slave->jiffies is only updated when
2772 * a slave first becomes the curr_active_slave - not necessarily
2773 * after every arp; this ensures the slave has a full 2*delta
2774 * before being taken out. if a primary is being used, check
2775 * if it is up and needs to take over as the curr_active_slave
2777 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2778 (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
2779 bond_has_ip(bond))) &&
2780 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2782 slave->link = BOND_LINK_DOWN;
2784 if (slave->link_failure_count < UINT_MAX) {
2785 slave->link_failure_count++;
2788 printk(KERN_INFO DRV_NAME
2789 ": %s: link status down for active interface "
2790 "%s, disabling it\n",
2791 bond_dev->name,
2792 slave->dev->name);
2794 write_lock(&bond->curr_slave_lock);
2796 bond_select_active_slave(bond);
2797 slave = bond->curr_active_slave;
2799 write_unlock(&bond->curr_slave_lock);
2801 bond->current_arp_slave = slave;
2803 if (slave) {
2804 slave->jiffies = jiffies;
2806 } else if ((bond->primary_slave) &&
2807 (bond->primary_slave != slave) &&
2808 (bond->primary_slave->link == BOND_LINK_UP)) {
2809 /* at this point, slave is the curr_active_slave */
2810 printk(KERN_INFO DRV_NAME
2811 ": %s: changing from interface %s to primary "
2812 "interface %s\n",
2813 bond_dev->name,
2814 slave->dev->name,
2815 bond->primary_slave->dev->name);
2817 /* primary is up so switch to it */
2818 write_lock(&bond->curr_slave_lock);
2819 bond_change_active_slave(bond, bond->primary_slave);
2820 write_unlock(&bond->curr_slave_lock);
2822 slave = bond->primary_slave;
2823 slave->jiffies = jiffies;
2824 } else {
2825 bond->current_arp_slave = NULL;
2828 /* the current slave must tx an arp to ensure backup slaves
2829 * rx traffic
2831 if (slave && bond_has_ip(bond)) {
2832 bond_arp_send_all(bond, slave);
2836 /* if we don't have a curr_active_slave, search for the next available
2837 * backup slave from the current_arp_slave and make it the candidate
2838 * for becoming the curr_active_slave
2840 if (!slave) {
2841 if (!bond->current_arp_slave) {
2842 bond->current_arp_slave = bond->first_slave;
2845 if (bond->current_arp_slave) {
2846 bond_set_slave_inactive_flags(bond->current_arp_slave);
2848 /* search for next candidate */
2849 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
2850 if (IS_UP(slave->dev)) {
2851 slave->link = BOND_LINK_BACK;
2852 bond_set_slave_active_flags(slave);
2853 bond_arp_send_all(bond, slave);
2854 slave->jiffies = jiffies;
2855 bond->current_arp_slave = slave;
2856 break;
2859 /* if the link state is up at this point, we
2860 * mark it down - this can happen if we have
2861 * simultaneous link failures and
2862 * reselect_active_interface doesn't make this
2863 * one the current slave so it is still marked
2864 * up when it is actually down
2866 if (slave->link == BOND_LINK_UP) {
2867 slave->link = BOND_LINK_DOWN;
2868 if (slave->link_failure_count < UINT_MAX) {
2869 slave->link_failure_count++;
2872 bond_set_slave_inactive_flags(slave);
2874 printk(KERN_INFO DRV_NAME
2875 ": %s: backup interface %s is "
2876 "now down.\n",
2877 bond_dev->name,
2878 slave->dev->name);
2884 re_arm:
2885 if (bond->params.arp_interval) {
2886 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2888 out:
2889 read_unlock(&bond->lock);
2892 /*------------------------------ proc/seq_file-------------------------------*/
2894 #ifdef CONFIG_PROC_FS
2896 #define SEQ_START_TOKEN ((void *)1)
2898 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
2900 struct bonding *bond = seq->private;
2901 loff_t off = 0;
2902 struct slave *slave;
2903 int i;
2905 /* make sure the bond won't be taken away */
2906 read_lock(&dev_base_lock);
2907 read_lock_bh(&bond->lock);
2909 if (*pos == 0) {
2910 return SEQ_START_TOKEN;
2913 bond_for_each_slave(bond, slave, i) {
2914 if (++off == *pos) {
2915 return slave;
2919 return NULL;
2922 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2924 struct bonding *bond = seq->private;
2925 struct slave *slave = v;
2927 ++*pos;
2928 if (v == SEQ_START_TOKEN) {
2929 return bond->first_slave;
2932 slave = slave->next;
2934 return (slave == bond->first_slave) ? NULL : slave;
2937 static void bond_info_seq_stop(struct seq_file *seq, void *v)
2939 struct bonding *bond = seq->private;
2941 read_unlock_bh(&bond->lock);
2942 read_unlock(&dev_base_lock);
2945 static void bond_info_show_master(struct seq_file *seq)
2947 struct bonding *bond = seq->private;
2948 struct slave *curr;
2949 int i;
2950 u32 target;
2952 read_lock(&bond->curr_slave_lock);
2953 curr = bond->curr_active_slave;
2954 read_unlock(&bond->curr_slave_lock);
2956 seq_printf(seq, "Bonding Mode: %s\n",
2957 bond_mode_name(bond->params.mode));
2959 if (bond->params.mode == BOND_MODE_XOR ||
2960 bond->params.mode == BOND_MODE_8023AD) {
2961 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
2962 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
2963 bond->params.xmit_policy);
2966 if (USES_PRIMARY(bond->params.mode)) {
2967 seq_printf(seq, "Primary Slave: %s\n",
2968 (bond->primary_slave) ?
2969 bond->primary_slave->dev->name : "None");
2971 seq_printf(seq, "Currently Active Slave: %s\n",
2972 (curr) ? curr->dev->name : "None");
2975 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
2976 "up" : "down");
2977 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
2978 seq_printf(seq, "Up Delay (ms): %d\n",
2979 bond->params.updelay * bond->params.miimon);
2980 seq_printf(seq, "Down Delay (ms): %d\n",
2981 bond->params.downdelay * bond->params.miimon);
2984 /* ARP information */
2985 if(bond->params.arp_interval > 0) {
2986 int printed=0;
2987 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
2988 bond->params.arp_interval);
2990 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
2992 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
2993 if (!bond->params.arp_targets[i])
2994 continue;
2995 if (printed)
2996 seq_printf(seq, ",");
2997 target = ntohl(bond->params.arp_targets[i]);
2998 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
2999 printed = 1;
3001 seq_printf(seq, "\n");
3004 if (bond->params.mode == BOND_MODE_8023AD) {
3005 struct ad_info ad_info;
3007 seq_puts(seq, "\n802.3ad info\n");
3008 seq_printf(seq, "LACP rate: %s\n",
3009 (bond->params.lacp_fast) ? "fast" : "slow");
3011 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3012 seq_printf(seq, "bond %s has no active aggregator\n",
3013 bond->dev->name);
3014 } else {
3015 seq_printf(seq, "Active Aggregator Info:\n");
3017 seq_printf(seq, "\tAggregator ID: %d\n",
3018 ad_info.aggregator_id);
3019 seq_printf(seq, "\tNumber of ports: %d\n",
3020 ad_info.ports);
3021 seq_printf(seq, "\tActor Key: %d\n",
3022 ad_info.actor_key);
3023 seq_printf(seq, "\tPartner Key: %d\n",
3024 ad_info.partner_key);
3025 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3026 ad_info.partner_system[0],
3027 ad_info.partner_system[1],
3028 ad_info.partner_system[2],
3029 ad_info.partner_system[3],
3030 ad_info.partner_system[4],
3031 ad_info.partner_system[5]);
3036 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3038 struct bonding *bond = seq->private;
3040 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3041 seq_printf(seq, "MII Status: %s\n",
3042 (slave->link == BOND_LINK_UP) ? "up" : "down");
3043 seq_printf(seq, "Link Failure Count: %u\n",
3044 slave->link_failure_count);
3046 seq_printf(seq,
3047 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3048 slave->perm_hwaddr[0], slave->perm_hwaddr[1],
3049 slave->perm_hwaddr[2], slave->perm_hwaddr[3],
3050 slave->perm_hwaddr[4], slave->perm_hwaddr[5]);
3052 if (bond->params.mode == BOND_MODE_8023AD) {
3053 const struct aggregator *agg
3054 = SLAVE_AD_INFO(slave).port.aggregator;
3056 if (agg) {
3057 seq_printf(seq, "Aggregator ID: %d\n",
3058 agg->aggregator_identifier);
3059 } else {
3060 seq_puts(seq, "Aggregator ID: N/A\n");
3065 static int bond_info_seq_show(struct seq_file *seq, void *v)
3067 if (v == SEQ_START_TOKEN) {
3068 seq_printf(seq, "%s\n", version);
3069 bond_info_show_master(seq);
3070 } else {
3071 bond_info_show_slave(seq, v);
3074 return 0;
3077 static struct seq_operations bond_info_seq_ops = {
3078 .start = bond_info_seq_start,
3079 .next = bond_info_seq_next,
3080 .stop = bond_info_seq_stop,
3081 .show = bond_info_seq_show,
3084 static int bond_info_open(struct inode *inode, struct file *file)
3086 struct seq_file *seq;
3087 struct proc_dir_entry *proc;
3088 int res;
3090 res = seq_open(file, &bond_info_seq_ops);
3091 if (!res) {
3092 /* recover the pointer buried in proc_dir_entry data */
3093 seq = file->private_data;
3094 proc = PDE(inode);
3095 seq->private = proc->data;
3098 return res;
3101 static const struct file_operations bond_info_fops = {
3102 .owner = THIS_MODULE,
3103 .open = bond_info_open,
3104 .read = seq_read,
3105 .llseek = seq_lseek,
3106 .release = seq_release,
3109 static int bond_create_proc_entry(struct bonding *bond)
3111 struct net_device *bond_dev = bond->dev;
3113 if (bond_proc_dir) {
3114 bond->proc_entry = create_proc_entry(bond_dev->name,
3115 S_IRUGO,
3116 bond_proc_dir);
3117 if (bond->proc_entry == NULL) {
3118 printk(KERN_WARNING DRV_NAME
3119 ": Warning: Cannot create /proc/net/%s/%s\n",
3120 DRV_NAME, bond_dev->name);
3121 } else {
3122 bond->proc_entry->data = bond;
3123 bond->proc_entry->proc_fops = &bond_info_fops;
3124 bond->proc_entry->owner = THIS_MODULE;
3125 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3129 return 0;
3132 static void bond_remove_proc_entry(struct bonding *bond)
3134 if (bond_proc_dir && bond->proc_entry) {
3135 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3136 memset(bond->proc_file_name, 0, IFNAMSIZ);
3137 bond->proc_entry = NULL;
3141 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3142 * Caller must hold rtnl_lock.
3144 static void bond_create_proc_dir(void)
3146 int len = strlen(DRV_NAME);
3148 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3149 bond_proc_dir = bond_proc_dir->next) {
3150 if ((bond_proc_dir->namelen == len) &&
3151 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3152 break;
3156 if (!bond_proc_dir) {
3157 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3158 if (bond_proc_dir) {
3159 bond_proc_dir->owner = THIS_MODULE;
3160 } else {
3161 printk(KERN_WARNING DRV_NAME
3162 ": Warning: cannot create /proc/net/%s\n",
3163 DRV_NAME);
3168 /* Destroy the bonding directory under /proc/net, if empty.
3169 * Caller must hold rtnl_lock.
3171 static void bond_destroy_proc_dir(void)
3173 struct proc_dir_entry *de;
3175 if (!bond_proc_dir) {
3176 return;
3179 /* verify that the /proc dir is empty */
3180 for (de = bond_proc_dir->subdir; de; de = de->next) {
3181 /* ignore . and .. */
3182 if (*(de->name) != '.') {
3183 break;
3187 if (de) {
3188 if (bond_proc_dir->owner == THIS_MODULE) {
3189 bond_proc_dir->owner = NULL;
3191 } else {
3192 remove_proc_entry(DRV_NAME, init_net.proc_net);
3193 bond_proc_dir = NULL;
3196 #endif /* CONFIG_PROC_FS */
3198 /*-------------------------- netdev event handling --------------------------*/
3201 * Change device name
3203 static int bond_event_changename(struct bonding *bond)
3205 #ifdef CONFIG_PROC_FS
3206 bond_remove_proc_entry(bond);
3207 bond_create_proc_entry(bond);
3208 #endif
3209 down_write(&(bonding_rwsem));
3210 bond_destroy_sysfs_entry(bond);
3211 bond_create_sysfs_entry(bond);
3212 up_write(&(bonding_rwsem));
3213 return NOTIFY_DONE;
3216 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3218 struct bonding *event_bond = bond_dev->priv;
3220 switch (event) {
3221 case NETDEV_CHANGENAME:
3222 return bond_event_changename(event_bond);
3223 case NETDEV_UNREGISTER:
3225 * TODO: remove a bond from the list?
3227 break;
3228 default:
3229 break;
3232 return NOTIFY_DONE;
3235 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3237 struct net_device *bond_dev = slave_dev->master;
3238 struct bonding *bond = bond_dev->priv;
3240 switch (event) {
3241 case NETDEV_UNREGISTER:
3242 if (bond_dev) {
3243 bond_release(bond_dev, slave_dev);
3245 break;
3246 case NETDEV_CHANGE:
3248 * TODO: is this what we get if somebody
3249 * sets up a hierarchical bond, then rmmod's
3250 * one of the slave bonding devices?
3252 break;
3253 case NETDEV_DOWN:
3255 * ... Or is it this?
3257 break;
3258 case NETDEV_CHANGEMTU:
3260 * TODO: Should slaves be allowed to
3261 * independently alter their MTU? For
3262 * an active-backup bond, slaves need
3263 * not be the same type of device, so
3264 * MTUs may vary. For other modes,
3265 * slaves arguably should have the
3266 * same MTUs. To do this, we'd need to
3267 * take over the slave's change_mtu
3268 * function for the duration of their
3269 * servitude.
3271 break;
3272 case NETDEV_CHANGENAME:
3274 * TODO: handle changing the primary's name
3276 break;
3277 case NETDEV_FEAT_CHANGE:
3278 bond_compute_features(bond);
3279 break;
3280 default:
3281 break;
3284 return NOTIFY_DONE;
3288 * bond_netdev_event: handle netdev notifier chain events.
3290 * This function receives events for the netdev chain. The caller (an
3291 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3292 * locks for us to safely manipulate the slave devices (RTNL lock,
3293 * dev_probe_lock).
3295 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3297 struct net_device *event_dev = (struct net_device *)ptr;
3299 dprintk("event_dev: %s, event: %lx\n",
3300 (event_dev ? event_dev->name : "None"),
3301 event);
3303 if (!(event_dev->priv_flags & IFF_BONDING))
3304 return NOTIFY_DONE;
3306 if (event_dev->flags & IFF_MASTER) {
3307 dprintk("IFF_MASTER\n");
3308 return bond_master_netdev_event(event, event_dev);
3311 if (event_dev->flags & IFF_SLAVE) {
3312 dprintk("IFF_SLAVE\n");
3313 return bond_slave_netdev_event(event, event_dev);
3316 return NOTIFY_DONE;
3320 * bond_inetaddr_event: handle inetaddr notifier chain events.
3322 * We keep track of device IPs primarily to use as source addresses in
3323 * ARP monitor probes (rather than spewing out broadcasts all the time).
3325 * We track one IP for the main device (if it has one), plus one per VLAN.
3327 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3329 struct in_ifaddr *ifa = ptr;
3330 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3331 struct bonding *bond, *bond_next;
3332 struct vlan_entry *vlan, *vlan_next;
3334 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3335 if (bond->dev == event_dev) {
3336 switch (event) {
3337 case NETDEV_UP:
3338 bond->master_ip = ifa->ifa_local;
3339 return NOTIFY_OK;
3340 case NETDEV_DOWN:
3341 bond->master_ip = bond_glean_dev_ip(bond->dev);
3342 return NOTIFY_OK;
3343 default:
3344 return NOTIFY_DONE;
3348 if (list_empty(&bond->vlan_list))
3349 continue;
3351 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3352 vlan_list) {
3353 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3354 if (vlan_dev == event_dev) {
3355 switch (event) {
3356 case NETDEV_UP:
3357 vlan->vlan_ip = ifa->ifa_local;
3358 return NOTIFY_OK;
3359 case NETDEV_DOWN:
3360 vlan->vlan_ip =
3361 bond_glean_dev_ip(vlan_dev);
3362 return NOTIFY_OK;
3363 default:
3364 return NOTIFY_DONE;
3369 return NOTIFY_DONE;
3372 static struct notifier_block bond_netdev_notifier = {
3373 .notifier_call = bond_netdev_event,
3376 static struct notifier_block bond_inetaddr_notifier = {
3377 .notifier_call = bond_inetaddr_event,
3380 /*-------------------------- Packet type handling ---------------------------*/
3382 /* register to receive lacpdus on a bond */
3383 static void bond_register_lacpdu(struct bonding *bond)
3385 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3387 /* initialize packet type */
3388 pk_type->type = PKT_TYPE_LACPDU;
3389 pk_type->dev = bond->dev;
3390 pk_type->func = bond_3ad_lacpdu_recv;
3392 dev_add_pack(pk_type);
3395 /* unregister to receive lacpdus on a bond */
3396 static void bond_unregister_lacpdu(struct bonding *bond)
3398 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3401 void bond_register_arp(struct bonding *bond)
3403 struct packet_type *pt = &bond->arp_mon_pt;
3405 if (pt->type)
3406 return;
3408 pt->type = htons(ETH_P_ARP);
3409 pt->dev = bond->dev;
3410 pt->func = bond_arp_rcv;
3411 dev_add_pack(pt);
3414 void bond_unregister_arp(struct bonding *bond)
3416 struct packet_type *pt = &bond->arp_mon_pt;
3418 dev_remove_pack(pt);
3419 pt->type = 0;
3422 /*---------------------------- Hashing Policies -----------------------------*/
3425 * Hash for the output device based upon layer 3 and layer 4 data. If
3426 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3427 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3429 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3430 struct net_device *bond_dev, int count)
3432 struct ethhdr *data = (struct ethhdr *)skb->data;
3433 struct iphdr *iph = ip_hdr(skb);
3434 u16 *layer4hdr = (u16 *)((u32 *)iph + iph->ihl);
3435 int layer4_xor = 0;
3437 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3438 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3439 (iph->protocol == IPPROTO_TCP ||
3440 iph->protocol == IPPROTO_UDP)) {
3441 layer4_xor = htons((*layer4hdr ^ *(layer4hdr + 1)));
3443 return (layer4_xor ^
3444 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3448 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3452 * Hash for the output device based upon layer 2 data
3454 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3455 struct net_device *bond_dev, int count)
3457 struct ethhdr *data = (struct ethhdr *)skb->data;
3459 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3462 /*-------------------------- Device entry points ----------------------------*/
3464 static int bond_open(struct net_device *bond_dev)
3466 struct bonding *bond = bond_dev->priv;
3467 struct timer_list *mii_timer = &bond->mii_timer;
3468 struct timer_list *arp_timer = &bond->arp_timer;
3470 bond->kill_timers = 0;
3472 if ((bond->params.mode == BOND_MODE_TLB) ||
3473 (bond->params.mode == BOND_MODE_ALB)) {
3474 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3476 /* bond_alb_initialize must be called before the timer
3477 * is started.
3479 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3480 /* something went wrong - fail the open operation */
3481 return -1;
3484 init_timer(alb_timer);
3485 alb_timer->expires = jiffies + 1;
3486 alb_timer->data = (unsigned long)bond;
3487 alb_timer->function = (void *)&bond_alb_monitor;
3488 add_timer(alb_timer);
3491 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3492 init_timer(mii_timer);
3493 mii_timer->expires = jiffies + 1;
3494 mii_timer->data = (unsigned long)bond_dev;
3495 mii_timer->function = (void *)&bond_mii_monitor;
3496 add_timer(mii_timer);
3499 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3500 init_timer(arp_timer);
3501 arp_timer->expires = jiffies + 1;
3502 arp_timer->data = (unsigned long)bond_dev;
3503 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3504 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3505 } else {
3506 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3508 if (bond->params.arp_validate)
3509 bond_register_arp(bond);
3511 add_timer(arp_timer);
3514 if (bond->params.mode == BOND_MODE_8023AD) {
3515 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3516 init_timer(ad_timer);
3517 ad_timer->expires = jiffies + 1;
3518 ad_timer->data = (unsigned long)bond;
3519 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3520 add_timer(ad_timer);
3522 /* register to receive LACPDUs */
3523 bond_register_lacpdu(bond);
3526 return 0;
3529 static int bond_close(struct net_device *bond_dev)
3531 struct bonding *bond = bond_dev->priv;
3533 if (bond->params.mode == BOND_MODE_8023AD) {
3534 /* Unregister the receive of LACPDUs */
3535 bond_unregister_lacpdu(bond);
3538 if (bond->params.arp_validate)
3539 bond_unregister_arp(bond);
3541 write_lock_bh(&bond->lock);
3544 /* signal timers not to re-arm */
3545 bond->kill_timers = 1;
3547 write_unlock_bh(&bond->lock);
3549 /* del_timer_sync must run without holding the bond->lock
3550 * because a running timer might be trying to hold it too
3553 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3554 del_timer_sync(&bond->mii_timer);
3557 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3558 del_timer_sync(&bond->arp_timer);
3561 switch (bond->params.mode) {
3562 case BOND_MODE_8023AD:
3563 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3564 break;
3565 case BOND_MODE_TLB:
3566 case BOND_MODE_ALB:
3567 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3568 break;
3569 default:
3570 break;
3574 if ((bond->params.mode == BOND_MODE_TLB) ||
3575 (bond->params.mode == BOND_MODE_ALB)) {
3576 /* Must be called only after all
3577 * slaves have been released
3579 bond_alb_deinitialize(bond);
3582 return 0;
3585 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3587 struct bonding *bond = bond_dev->priv;
3588 struct net_device_stats *stats = &(bond->stats), *sstats;
3589 struct slave *slave;
3590 int i;
3592 memset(stats, 0, sizeof(struct net_device_stats));
3594 read_lock_bh(&bond->lock);
3596 bond_for_each_slave(bond, slave, i) {
3597 sstats = slave->dev->get_stats(slave->dev);
3598 stats->rx_packets += sstats->rx_packets;
3599 stats->rx_bytes += sstats->rx_bytes;
3600 stats->rx_errors += sstats->rx_errors;
3601 stats->rx_dropped += sstats->rx_dropped;
3603 stats->tx_packets += sstats->tx_packets;
3604 stats->tx_bytes += sstats->tx_bytes;
3605 stats->tx_errors += sstats->tx_errors;
3606 stats->tx_dropped += sstats->tx_dropped;
3608 stats->multicast += sstats->multicast;
3609 stats->collisions += sstats->collisions;
3611 stats->rx_length_errors += sstats->rx_length_errors;
3612 stats->rx_over_errors += sstats->rx_over_errors;
3613 stats->rx_crc_errors += sstats->rx_crc_errors;
3614 stats->rx_frame_errors += sstats->rx_frame_errors;
3615 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3616 stats->rx_missed_errors += sstats->rx_missed_errors;
3618 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3619 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3620 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3621 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3622 stats->tx_window_errors += sstats->tx_window_errors;
3625 read_unlock_bh(&bond->lock);
3627 return stats;
3630 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3632 struct net_device *slave_dev = NULL;
3633 struct ifbond k_binfo;
3634 struct ifbond __user *u_binfo = NULL;
3635 struct ifslave k_sinfo;
3636 struct ifslave __user *u_sinfo = NULL;
3637 struct mii_ioctl_data *mii = NULL;
3638 int res = 0;
3640 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3641 bond_dev->name, cmd);
3643 switch (cmd) {
3644 case SIOCGMIIPHY:
3645 mii = if_mii(ifr);
3646 if (!mii) {
3647 return -EINVAL;
3649 mii->phy_id = 0;
3650 /* Fall Through */
3651 case SIOCGMIIREG:
3653 * We do this again just in case we were called by SIOCGMIIREG
3654 * instead of SIOCGMIIPHY.
3656 mii = if_mii(ifr);
3657 if (!mii) {
3658 return -EINVAL;
3661 if (mii->reg_num == 1) {
3662 struct bonding *bond = bond_dev->priv;
3663 mii->val_out = 0;
3664 read_lock_bh(&bond->lock);
3665 read_lock(&bond->curr_slave_lock);
3666 if (netif_carrier_ok(bond->dev)) {
3667 mii->val_out = BMSR_LSTATUS;
3669 read_unlock(&bond->curr_slave_lock);
3670 read_unlock_bh(&bond->lock);
3673 return 0;
3674 case BOND_INFO_QUERY_OLD:
3675 case SIOCBONDINFOQUERY:
3676 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3678 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3679 return -EFAULT;
3682 res = bond_info_query(bond_dev, &k_binfo);
3683 if (res == 0) {
3684 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3685 return -EFAULT;
3689 return res;
3690 case BOND_SLAVE_INFO_QUERY_OLD:
3691 case SIOCBONDSLAVEINFOQUERY:
3692 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3694 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3695 return -EFAULT;
3698 res = bond_slave_info_query(bond_dev, &k_sinfo);
3699 if (res == 0) {
3700 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3701 return -EFAULT;
3705 return res;
3706 default:
3707 /* Go on */
3708 break;
3711 if (!capable(CAP_NET_ADMIN)) {
3712 return -EPERM;
3715 down_write(&(bonding_rwsem));
3716 slave_dev = dev_get_by_name(ifr->ifr_slave);
3718 dprintk("slave_dev=%p: \n", slave_dev);
3720 if (!slave_dev) {
3721 res = -ENODEV;
3722 } else {
3723 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3724 switch (cmd) {
3725 case BOND_ENSLAVE_OLD:
3726 case SIOCBONDENSLAVE:
3727 res = bond_enslave(bond_dev, slave_dev);
3728 break;
3729 case BOND_RELEASE_OLD:
3730 case SIOCBONDRELEASE:
3731 res = bond_release(bond_dev, slave_dev);
3732 break;
3733 case BOND_SETHWADDR_OLD:
3734 case SIOCBONDSETHWADDR:
3735 res = bond_sethwaddr(bond_dev, slave_dev);
3736 break;
3737 case BOND_CHANGE_ACTIVE_OLD:
3738 case SIOCBONDCHANGEACTIVE:
3739 res = bond_ioctl_change_active(bond_dev, slave_dev);
3740 break;
3741 default:
3742 res = -EOPNOTSUPP;
3745 dev_put(slave_dev);
3748 up_write(&(bonding_rwsem));
3749 return res;
3752 static void bond_set_multicast_list(struct net_device *bond_dev)
3754 struct bonding *bond = bond_dev->priv;
3755 struct dev_mc_list *dmi;
3757 write_lock_bh(&bond->lock);
3760 * Do promisc before checking multicast_mode
3762 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3763 bond_set_promiscuity(bond, 1);
3766 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3767 bond_set_promiscuity(bond, -1);
3770 /* set allmulti flag to slaves */
3771 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3772 bond_set_allmulti(bond, 1);
3775 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3776 bond_set_allmulti(bond, -1);
3779 bond->flags = bond_dev->flags;
3781 /* looking for addresses to add to slaves' mc list */
3782 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3783 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3784 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3788 /* looking for addresses to delete from slaves' list */
3789 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3790 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3791 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3795 /* save master's multicast list */
3796 bond_mc_list_destroy(bond);
3797 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3799 write_unlock_bh(&bond->lock);
3803 * Change the MTU of all of a master's slaves to match the master
3805 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3807 struct bonding *bond = bond_dev->priv;
3808 struct slave *slave, *stop_at;
3809 int res = 0;
3810 int i;
3812 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3813 (bond_dev ? bond_dev->name : "None"), new_mtu);
3815 /* Can't hold bond->lock with bh disabled here since
3816 * some base drivers panic. On the other hand we can't
3817 * hold bond->lock without bh disabled because we'll
3818 * deadlock. The only solution is to rely on the fact
3819 * that we're under rtnl_lock here, and the slaves
3820 * list won't change. This doesn't solve the problem
3821 * of setting the slave's MTU while it is
3822 * transmitting, but the assumption is that the base
3823 * driver can handle that.
3825 * TODO: figure out a way to safely iterate the slaves
3826 * list, but without holding a lock around the actual
3827 * call to the base driver.
3830 bond_for_each_slave(bond, slave, i) {
3831 dprintk("s %p s->p %p c_m %p\n", slave,
3832 slave->prev, slave->dev->change_mtu);
3834 res = dev_set_mtu(slave->dev, new_mtu);
3836 if (res) {
3837 /* If we failed to set the slave's mtu to the new value
3838 * we must abort the operation even in ACTIVE_BACKUP
3839 * mode, because if we allow the backup slaves to have
3840 * different mtu values than the active slave we'll
3841 * need to change their mtu when doing a failover. That
3842 * means changing their mtu from timer context, which
3843 * is probably not a good idea.
3845 dprintk("err %d %s\n", res, slave->dev->name);
3846 goto unwind;
3850 bond_dev->mtu = new_mtu;
3852 return 0;
3854 unwind:
3855 /* unwind from head to the slave that failed */
3856 stop_at = slave;
3857 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3858 int tmp_res;
3860 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3861 if (tmp_res) {
3862 dprintk("unwind err %d dev %s\n", tmp_res,
3863 slave->dev->name);
3867 return res;
3871 * Change HW address
3873 * Note that many devices must be down to change the HW address, and
3874 * downing the master releases all slaves. We can make bonds full of
3875 * bonding devices to test this, however.
3877 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3879 struct bonding *bond = bond_dev->priv;
3880 struct sockaddr *sa = addr, tmp_sa;
3881 struct slave *slave, *stop_at;
3882 int res = 0;
3883 int i;
3885 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3887 if (!is_valid_ether_addr(sa->sa_data)) {
3888 return -EADDRNOTAVAIL;
3891 /* Can't hold bond->lock with bh disabled here since
3892 * some base drivers panic. On the other hand we can't
3893 * hold bond->lock without bh disabled because we'll
3894 * deadlock. The only solution is to rely on the fact
3895 * that we're under rtnl_lock here, and the slaves
3896 * list won't change. This doesn't solve the problem
3897 * of setting the slave's hw address while it is
3898 * transmitting, but the assumption is that the base
3899 * driver can handle that.
3901 * TODO: figure out a way to safely iterate the slaves
3902 * list, but without holding a lock around the actual
3903 * call to the base driver.
3906 bond_for_each_slave(bond, slave, i) {
3907 dprintk("slave %p %s\n", slave, slave->dev->name);
3909 if (slave->dev->set_mac_address == NULL) {
3910 res = -EOPNOTSUPP;
3911 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
3912 goto unwind;
3915 res = dev_set_mac_address(slave->dev, addr);
3916 if (res) {
3917 /* TODO: consider downing the slave
3918 * and retry ?
3919 * User should expect communications
3920 * breakage anyway until ARP finish
3921 * updating, so...
3923 dprintk("err %d %s\n", res, slave->dev->name);
3924 goto unwind;
3928 /* success */
3929 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3930 return 0;
3932 unwind:
3933 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3934 tmp_sa.sa_family = bond_dev->type;
3936 /* unwind from head to the slave that failed */
3937 stop_at = slave;
3938 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3939 int tmp_res;
3941 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3942 if (tmp_res) {
3943 dprintk("unwind err %d dev %s\n", tmp_res,
3944 slave->dev->name);
3948 return res;
3951 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3953 struct bonding *bond = bond_dev->priv;
3954 struct slave *slave, *start_at;
3955 int i;
3956 int res = 1;
3958 read_lock(&bond->lock);
3960 if (!BOND_IS_OK(bond)) {
3961 goto out;
3964 read_lock(&bond->curr_slave_lock);
3965 slave = start_at = bond->curr_active_slave;
3966 read_unlock(&bond->curr_slave_lock);
3968 if (!slave) {
3969 goto out;
3972 bond_for_each_slave_from(bond, slave, i, start_at) {
3973 if (IS_UP(slave->dev) &&
3974 (slave->link == BOND_LINK_UP) &&
3975 (slave->state == BOND_STATE_ACTIVE)) {
3976 res = bond_dev_queue_xmit(bond, skb, slave->dev);
3978 write_lock(&bond->curr_slave_lock);
3979 bond->curr_active_slave = slave->next;
3980 write_unlock(&bond->curr_slave_lock);
3982 break;
3987 out:
3988 if (res) {
3989 /* no suitable interface, frame not sent */
3990 dev_kfree_skb(skb);
3992 read_unlock(&bond->lock);
3993 return 0;
3998 * in active-backup mode, we know that bond->curr_active_slave is always valid if
3999 * the bond has a usable interface.
4001 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4003 struct bonding *bond = bond_dev->priv;
4004 int res = 1;
4006 read_lock(&bond->lock);
4007 read_lock(&bond->curr_slave_lock);
4009 if (!BOND_IS_OK(bond)) {
4010 goto out;
4013 if (!bond->curr_active_slave)
4014 goto out;
4016 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4018 out:
4019 if (res) {
4020 /* no suitable interface, frame not sent */
4021 dev_kfree_skb(skb);
4023 read_unlock(&bond->curr_slave_lock);
4024 read_unlock(&bond->lock);
4025 return 0;
4029 * In bond_xmit_xor() , we determine the output device by using a pre-
4030 * determined xmit_hash_policy(), If the selected device is not enabled,
4031 * find the next active slave.
4033 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4035 struct bonding *bond = bond_dev->priv;
4036 struct slave *slave, *start_at;
4037 int slave_no;
4038 int i;
4039 int res = 1;
4041 read_lock(&bond->lock);
4043 if (!BOND_IS_OK(bond)) {
4044 goto out;
4047 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4049 bond_for_each_slave(bond, slave, i) {
4050 slave_no--;
4051 if (slave_no < 0) {
4052 break;
4056 start_at = slave;
4058 bond_for_each_slave_from(bond, slave, i, start_at) {
4059 if (IS_UP(slave->dev) &&
4060 (slave->link == BOND_LINK_UP) &&
4061 (slave->state == BOND_STATE_ACTIVE)) {
4062 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4063 break;
4067 out:
4068 if (res) {
4069 /* no suitable interface, frame not sent */
4070 dev_kfree_skb(skb);
4072 read_unlock(&bond->lock);
4073 return 0;
4077 * in broadcast mode, we send everything to all usable interfaces.
4079 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4081 struct bonding *bond = bond_dev->priv;
4082 struct slave *slave, *start_at;
4083 struct net_device *tx_dev = NULL;
4084 int i;
4085 int res = 1;
4087 read_lock(&bond->lock);
4089 if (!BOND_IS_OK(bond)) {
4090 goto out;
4093 read_lock(&bond->curr_slave_lock);
4094 start_at = bond->curr_active_slave;
4095 read_unlock(&bond->curr_slave_lock);
4097 if (!start_at) {
4098 goto out;
4101 bond_for_each_slave_from(bond, slave, i, start_at) {
4102 if (IS_UP(slave->dev) &&
4103 (slave->link == BOND_LINK_UP) &&
4104 (slave->state == BOND_STATE_ACTIVE)) {
4105 if (tx_dev) {
4106 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4107 if (!skb2) {
4108 printk(KERN_ERR DRV_NAME
4109 ": %s: Error: bond_xmit_broadcast(): "
4110 "skb_clone() failed\n",
4111 bond_dev->name);
4112 continue;
4115 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4116 if (res) {
4117 dev_kfree_skb(skb2);
4118 continue;
4121 tx_dev = slave->dev;
4125 if (tx_dev) {
4126 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4129 out:
4130 if (res) {
4131 /* no suitable interface, frame not sent */
4132 dev_kfree_skb(skb);
4134 /* frame sent to all suitable interfaces */
4135 read_unlock(&bond->lock);
4136 return 0;
4139 /*------------------------- Device initialization ---------------------------*/
4142 * set bond mode specific net device operations
4144 void bond_set_mode_ops(struct bonding *bond, int mode)
4146 struct net_device *bond_dev = bond->dev;
4148 switch (mode) {
4149 case BOND_MODE_ROUNDROBIN:
4150 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4151 break;
4152 case BOND_MODE_ACTIVEBACKUP:
4153 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4154 break;
4155 case BOND_MODE_XOR:
4156 bond_dev->hard_start_xmit = bond_xmit_xor;
4157 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4158 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4159 else
4160 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4161 break;
4162 case BOND_MODE_BROADCAST:
4163 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4164 break;
4165 case BOND_MODE_8023AD:
4166 bond_set_master_3ad_flags(bond);
4167 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4168 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4169 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4170 else
4171 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4172 break;
4173 case BOND_MODE_ALB:
4174 bond_set_master_alb_flags(bond);
4175 /* FALLTHRU */
4176 case BOND_MODE_TLB:
4177 bond_dev->hard_start_xmit = bond_alb_xmit;
4178 bond_dev->set_mac_address = bond_alb_set_mac_address;
4179 break;
4180 default:
4181 /* Should never happen, mode already checked */
4182 printk(KERN_ERR DRV_NAME
4183 ": %s: Error: Unknown bonding mode %d\n",
4184 bond_dev->name,
4185 mode);
4186 break;
4190 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4191 struct ethtool_drvinfo *drvinfo)
4193 strncpy(drvinfo->driver, DRV_NAME, 32);
4194 strncpy(drvinfo->version, DRV_VERSION, 32);
4195 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4198 static const struct ethtool_ops bond_ethtool_ops = {
4199 .get_tx_csum = ethtool_op_get_tx_csum,
4200 .get_tso = ethtool_op_get_tso,
4201 .get_ufo = ethtool_op_get_ufo,
4202 .get_sg = ethtool_op_get_sg,
4203 .get_drvinfo = bond_ethtool_get_drvinfo,
4207 * Does not allocate but creates a /proc entry.
4208 * Allowed to fail.
4210 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4212 struct bonding *bond = bond_dev->priv;
4214 dprintk("Begin bond_init for %s\n", bond_dev->name);
4216 /* initialize rwlocks */
4217 rwlock_init(&bond->lock);
4218 rwlock_init(&bond->curr_slave_lock);
4220 bond->params = *params; /* copy params struct */
4222 /* Initialize pointers */
4223 bond->first_slave = NULL;
4224 bond->curr_active_slave = NULL;
4225 bond->current_arp_slave = NULL;
4226 bond->primary_slave = NULL;
4227 bond->dev = bond_dev;
4228 INIT_LIST_HEAD(&bond->vlan_list);
4230 /* Initialize the device entry points */
4231 bond_dev->open = bond_open;
4232 bond_dev->stop = bond_close;
4233 bond_dev->get_stats = bond_get_stats;
4234 bond_dev->do_ioctl = bond_do_ioctl;
4235 bond_dev->ethtool_ops = &bond_ethtool_ops;
4236 bond_dev->set_multicast_list = bond_set_multicast_list;
4237 bond_dev->change_mtu = bond_change_mtu;
4238 bond_dev->set_mac_address = bond_set_mac_address;
4240 bond_set_mode_ops(bond, bond->params.mode);
4242 bond_dev->destructor = free_netdev;
4244 /* Initialize the device options */
4245 bond_dev->tx_queue_len = 0;
4246 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4247 bond_dev->priv_flags |= IFF_BONDING;
4249 /* At first, we block adding VLANs. That's the only way to
4250 * prevent problems that occur when adding VLANs over an
4251 * empty bond. The block will be removed once non-challenged
4252 * slaves are enslaved.
4254 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4256 /* don't acquire bond device's netif_tx_lock when
4257 * transmitting */
4258 bond_dev->features |= NETIF_F_LLTX;
4260 /* By default, we declare the bond to be fully
4261 * VLAN hardware accelerated capable. Special
4262 * care is taken in the various xmit functions
4263 * when there are slaves that are not hw accel
4264 * capable
4266 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4267 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4268 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4269 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4270 NETIF_F_HW_VLAN_RX |
4271 NETIF_F_HW_VLAN_FILTER);
4273 #ifdef CONFIG_PROC_FS
4274 bond_create_proc_entry(bond);
4275 #endif
4277 list_add_tail(&bond->bond_list, &bond_dev_list);
4279 return 0;
4282 /* De-initialize device specific data.
4283 * Caller must hold rtnl_lock.
4285 void bond_deinit(struct net_device *bond_dev)
4287 struct bonding *bond = bond_dev->priv;
4289 list_del(&bond->bond_list);
4291 #ifdef CONFIG_PROC_FS
4292 bond_remove_proc_entry(bond);
4293 #endif
4296 /* Unregister and free all bond devices.
4297 * Caller must hold rtnl_lock.
4299 static void bond_free_all(void)
4301 struct bonding *bond, *nxt;
4303 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4304 struct net_device *bond_dev = bond->dev;
4306 bond_mc_list_destroy(bond);
4307 /* Release the bonded slaves */
4308 bond_release_all(bond_dev);
4309 bond_deinit(bond_dev);
4310 unregister_netdevice(bond_dev);
4313 #ifdef CONFIG_PROC_FS
4314 bond_destroy_proc_dir();
4315 #endif
4318 /*------------------------- Module initialization ---------------------------*/
4321 * Convert string input module parms. Accept either the
4322 * number of the mode or its string name.
4324 int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4326 int i;
4328 for (i = 0; tbl[i].modename; i++) {
4329 if ((isdigit(*mode_arg) &&
4330 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4331 (strncmp(mode_arg, tbl[i].modename,
4332 strlen(tbl[i].modename)) == 0)) {
4333 return tbl[i].mode;
4337 return -1;
4340 static int bond_check_params(struct bond_params *params)
4342 int arp_validate_value;
4345 * Convert string parameters.
4347 if (mode) {
4348 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4349 if (bond_mode == -1) {
4350 printk(KERN_ERR DRV_NAME
4351 ": Error: Invalid bonding mode \"%s\"\n",
4352 mode == NULL ? "NULL" : mode);
4353 return -EINVAL;
4357 if (xmit_hash_policy) {
4358 if ((bond_mode != BOND_MODE_XOR) &&
4359 (bond_mode != BOND_MODE_8023AD)) {
4360 printk(KERN_INFO DRV_NAME
4361 ": xor_mode param is irrelevant in mode %s\n",
4362 bond_mode_name(bond_mode));
4363 } else {
4364 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4365 xmit_hashtype_tbl);
4366 if (xmit_hashtype == -1) {
4367 printk(KERN_ERR DRV_NAME
4368 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4369 xmit_hash_policy == NULL ? "NULL" :
4370 xmit_hash_policy);
4371 return -EINVAL;
4376 if (lacp_rate) {
4377 if (bond_mode != BOND_MODE_8023AD) {
4378 printk(KERN_INFO DRV_NAME
4379 ": lacp_rate param is irrelevant in mode %s\n",
4380 bond_mode_name(bond_mode));
4381 } else {
4382 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4383 if (lacp_fast == -1) {
4384 printk(KERN_ERR DRV_NAME
4385 ": Error: Invalid lacp rate \"%s\"\n",
4386 lacp_rate == NULL ? "NULL" : lacp_rate);
4387 return -EINVAL;
4392 if (max_bonds < 1 || max_bonds > INT_MAX) {
4393 printk(KERN_WARNING DRV_NAME
4394 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4395 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4396 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4397 max_bonds = BOND_DEFAULT_MAX_BONDS;
4400 if (miimon < 0) {
4401 printk(KERN_WARNING DRV_NAME
4402 ": Warning: miimon module parameter (%d), "
4403 "not in range 0-%d, so it was reset to %d\n",
4404 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4405 miimon = BOND_LINK_MON_INTERV;
4408 if (updelay < 0) {
4409 printk(KERN_WARNING DRV_NAME
4410 ": Warning: updelay module parameter (%d), "
4411 "not in range 0-%d, so it was reset to 0\n",
4412 updelay, INT_MAX);
4413 updelay = 0;
4416 if (downdelay < 0) {
4417 printk(KERN_WARNING DRV_NAME
4418 ": Warning: downdelay module parameter (%d), "
4419 "not in range 0-%d, so it was reset to 0\n",
4420 downdelay, INT_MAX);
4421 downdelay = 0;
4424 if ((use_carrier != 0) && (use_carrier != 1)) {
4425 printk(KERN_WARNING DRV_NAME
4426 ": Warning: use_carrier module parameter (%d), "
4427 "not of valid value (0/1), so it was set to 1\n",
4428 use_carrier);
4429 use_carrier = 1;
4432 /* reset values for 802.3ad */
4433 if (bond_mode == BOND_MODE_8023AD) {
4434 if (!miimon) {
4435 printk(KERN_WARNING DRV_NAME
4436 ": Warning: miimon must be specified, "
4437 "otherwise bonding will not detect link "
4438 "failure, speed and duplex which are "
4439 "essential for 802.3ad operation\n");
4440 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4441 miimon = 100;
4445 /* reset values for TLB/ALB */
4446 if ((bond_mode == BOND_MODE_TLB) ||
4447 (bond_mode == BOND_MODE_ALB)) {
4448 if (!miimon) {
4449 printk(KERN_WARNING DRV_NAME
4450 ": Warning: miimon must be specified, "
4451 "otherwise bonding will not detect link "
4452 "failure and link speed which are essential "
4453 "for TLB/ALB load balancing\n");
4454 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4455 miimon = 100;
4459 if (bond_mode == BOND_MODE_ALB) {
4460 printk(KERN_NOTICE DRV_NAME
4461 ": In ALB mode you might experience client "
4462 "disconnections upon reconnection of a link if the "
4463 "bonding module updelay parameter (%d msec) is "
4464 "incompatible with the forwarding delay time of the "
4465 "switch\n",
4466 updelay);
4469 if (!miimon) {
4470 if (updelay || downdelay) {
4471 /* just warn the user the up/down delay will have
4472 * no effect since miimon is zero...
4474 printk(KERN_WARNING DRV_NAME
4475 ": Warning: miimon module parameter not set "
4476 "and updelay (%d) or downdelay (%d) module "
4477 "parameter is set; updelay and downdelay have "
4478 "no effect unless miimon is set\n",
4479 updelay, downdelay);
4481 } else {
4482 /* don't allow arp monitoring */
4483 if (arp_interval) {
4484 printk(KERN_WARNING DRV_NAME
4485 ": Warning: miimon (%d) and arp_interval (%d) "
4486 "can't be used simultaneously, disabling ARP "
4487 "monitoring\n",
4488 miimon, arp_interval);
4489 arp_interval = 0;
4492 if ((updelay % miimon) != 0) {
4493 printk(KERN_WARNING DRV_NAME
4494 ": Warning: updelay (%d) is not a multiple "
4495 "of miimon (%d), updelay rounded to %d ms\n",
4496 updelay, miimon, (updelay / miimon) * miimon);
4499 updelay /= miimon;
4501 if ((downdelay % miimon) != 0) {
4502 printk(KERN_WARNING DRV_NAME
4503 ": Warning: downdelay (%d) is not a multiple "
4504 "of miimon (%d), downdelay rounded to %d ms\n",
4505 downdelay, miimon,
4506 (downdelay / miimon) * miimon);
4509 downdelay /= miimon;
4512 if (arp_interval < 0) {
4513 printk(KERN_WARNING DRV_NAME
4514 ": Warning: arp_interval module parameter (%d) "
4515 ", not in range 0-%d, so it was reset to %d\n",
4516 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4517 arp_interval = BOND_LINK_ARP_INTERV;
4520 for (arp_ip_count = 0;
4521 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4522 arp_ip_count++) {
4523 /* not complete check, but should be good enough to
4524 catch mistakes */
4525 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4526 printk(KERN_WARNING DRV_NAME
4527 ": Warning: bad arp_ip_target module parameter "
4528 "(%s), ARP monitoring will not be performed\n",
4529 arp_ip_target[arp_ip_count]);
4530 arp_interval = 0;
4531 } else {
4532 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4533 arp_target[arp_ip_count] = ip;
4537 if (arp_interval && !arp_ip_count) {
4538 /* don't allow arping if no arp_ip_target given... */
4539 printk(KERN_WARNING DRV_NAME
4540 ": Warning: arp_interval module parameter (%d) "
4541 "specified without providing an arp_ip_target "
4542 "parameter, arp_interval was reset to 0\n",
4543 arp_interval);
4544 arp_interval = 0;
4547 if (arp_validate) {
4548 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4549 printk(KERN_ERR DRV_NAME
4550 ": arp_validate only supported in active-backup mode\n");
4551 return -EINVAL;
4553 if (!arp_interval) {
4554 printk(KERN_ERR DRV_NAME
4555 ": arp_validate requires arp_interval\n");
4556 return -EINVAL;
4559 arp_validate_value = bond_parse_parm(arp_validate,
4560 arp_validate_tbl);
4561 if (arp_validate_value == -1) {
4562 printk(KERN_ERR DRV_NAME
4563 ": Error: invalid arp_validate \"%s\"\n",
4564 arp_validate == NULL ? "NULL" : arp_validate);
4565 return -EINVAL;
4567 } else
4568 arp_validate_value = 0;
4570 if (miimon) {
4571 printk(KERN_INFO DRV_NAME
4572 ": MII link monitoring set to %d ms\n",
4573 miimon);
4574 } else if (arp_interval) {
4575 int i;
4577 printk(KERN_INFO DRV_NAME
4578 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4579 arp_interval,
4580 arp_validate_tbl[arp_validate_value].modename,
4581 arp_ip_count);
4583 for (i = 0; i < arp_ip_count; i++)
4584 printk (" %s", arp_ip_target[i]);
4586 printk("\n");
4588 } else {
4589 /* miimon and arp_interval not set, we need one so things
4590 * work as expected, see bonding.txt for details
4592 printk(KERN_WARNING DRV_NAME
4593 ": Warning: either miimon or arp_interval and "
4594 "arp_ip_target module parameters must be specified, "
4595 "otherwise bonding will not detect link failures! see "
4596 "bonding.txt for details.\n");
4599 if (primary && !USES_PRIMARY(bond_mode)) {
4600 /* currently, using a primary only makes sense
4601 * in active backup, TLB or ALB modes
4603 printk(KERN_WARNING DRV_NAME
4604 ": Warning: %s primary device specified but has no "
4605 "effect in %s mode\n",
4606 primary, bond_mode_name(bond_mode));
4607 primary = NULL;
4610 /* fill params struct with the proper values */
4611 params->mode = bond_mode;
4612 params->xmit_policy = xmit_hashtype;
4613 params->miimon = miimon;
4614 params->arp_interval = arp_interval;
4615 params->arp_validate = arp_validate_value;
4616 params->updelay = updelay;
4617 params->downdelay = downdelay;
4618 params->use_carrier = use_carrier;
4619 params->lacp_fast = lacp_fast;
4620 params->primary[0] = 0;
4622 if (primary) {
4623 strncpy(params->primary, primary, IFNAMSIZ);
4624 params->primary[IFNAMSIZ - 1] = 0;
4627 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4629 return 0;
4632 static struct lock_class_key bonding_netdev_xmit_lock_key;
4634 /* Create a new bond based on the specified name and bonding parameters.
4635 * If name is NULL, obtain a suitable "bond%d" name for us.
4636 * Caller must NOT hold rtnl_lock; we need to release it here before we
4637 * set up our sysfs entries.
4639 int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4641 struct net_device *bond_dev;
4642 int res;
4644 rtnl_lock();
4645 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4646 ether_setup);
4647 if (!bond_dev) {
4648 printk(KERN_ERR DRV_NAME
4649 ": %s: eek! can't alloc netdev!\n",
4650 name);
4651 res = -ENOMEM;
4652 goto out_rtnl;
4655 if (!name) {
4656 res = dev_alloc_name(bond_dev, "bond%d");
4657 if (res < 0)
4658 goto out_netdev;
4661 /* bond_init() must be called after dev_alloc_name() (for the
4662 * /proc files), but before register_netdevice(), because we
4663 * need to set function pointers.
4666 res = bond_init(bond_dev, params);
4667 if (res < 0) {
4668 goto out_netdev;
4671 SET_MODULE_OWNER(bond_dev);
4673 res = register_netdevice(bond_dev);
4674 if (res < 0) {
4675 goto out_bond;
4678 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4680 if (newbond)
4681 *newbond = bond_dev->priv;
4683 netif_carrier_off(bond_dev);
4685 rtnl_unlock(); /* allows sysfs registration of net device */
4686 res = bond_create_sysfs_entry(bond_dev->priv);
4687 if (res < 0) {
4688 rtnl_lock();
4689 goto out_bond;
4692 return 0;
4694 out_bond:
4695 bond_deinit(bond_dev);
4696 out_netdev:
4697 free_netdev(bond_dev);
4698 out_rtnl:
4699 rtnl_unlock();
4700 return res;
4703 static int __init bonding_init(void)
4705 int i;
4706 int res;
4708 printk(KERN_INFO "%s", version);
4710 res = bond_check_params(&bonding_defaults);
4711 if (res) {
4712 goto out;
4715 #ifdef CONFIG_PROC_FS
4716 bond_create_proc_dir();
4717 #endif
4718 for (i = 0; i < max_bonds; i++) {
4719 res = bond_create(NULL, &bonding_defaults, NULL);
4720 if (res)
4721 goto err;
4724 res = bond_create_sysfs();
4725 if (res)
4726 goto err;
4728 register_netdevice_notifier(&bond_netdev_notifier);
4729 register_inetaddr_notifier(&bond_inetaddr_notifier);
4731 goto out;
4732 err:
4733 rtnl_lock();
4734 bond_free_all();
4735 bond_destroy_sysfs();
4736 rtnl_unlock();
4737 out:
4738 return res;
4742 static void __exit bonding_exit(void)
4744 unregister_netdevice_notifier(&bond_netdev_notifier);
4745 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4747 rtnl_lock();
4748 bond_free_all();
4749 bond_destroy_sysfs();
4750 rtnl_unlock();
4753 module_init(bonding_init);
4754 module_exit(bonding_exit);
4755 MODULE_LICENSE("GPL");
4756 MODULE_VERSION(DRV_VERSION);
4757 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4758 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4759 MODULE_SUPPORTED_DEVICE("most ethernet devices");
4762 * Local variables:
4763 * c-indent-level: 8
4764 * c-basic-offset: 8
4765 * tab-width: 8
4766 * End: