mISDN: timerdev: use __user for mISDN_read's buffer argument
[linux-2.6/cjktty.git] / drivers / net / bonding / bond_main.c
bloba34c186b08bbf4a3ace550262ae377bd9139bc13
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 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
41 #include <linux/in.h>
42 #include <net/ip.h>
43 #include <linux/ip.h>
44 #include <linux/tcp.h>
45 #include <linux/udp.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/init.h>
49 #include <linux/timer.h>
50 #include <linux/socket.h>
51 #include <linux/ctype.h>
52 #include <linux/inet.h>
53 #include <linux/bitops.h>
54 #include <asm/system.h>
55 #include <asm/io.h>
56 #include <asm/dma.h>
57 #include <asm/uaccess.h>
58 #include <linux/errno.h>
59 #include <linux/netdevice.h>
60 #include <linux/inetdevice.h>
61 #include <linux/igmp.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <net/sock.h>
65 #include <linux/rtnetlink.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/smp.h>
69 #include <linux/if_ether.h>
70 #include <net/arp.h>
71 #include <linux/mii.h>
72 #include <linux/ethtool.h>
73 #include <linux/if_vlan.h>
74 #include <linux/if_bonding.h>
75 #include <linux/jiffies.h>
76 #include <net/route.h>
77 #include <net/net_namespace.h>
78 #include "bonding.h"
79 #include "bond_3ad.h"
80 #include "bond_alb.h"
82 /*---------------------------- Module parameters ----------------------------*/
84 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
85 #define BOND_LINK_MON_INTERV 0
86 #define BOND_LINK_ARP_INTERV 0
88 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
89 static int num_grat_arp = 1;
90 static int num_unsol_na = 1;
91 static int miimon = BOND_LINK_MON_INTERV;
92 static int updelay = 0;
93 static int downdelay = 0;
94 static int use_carrier = 1;
95 static char *mode = NULL;
96 static char *primary = NULL;
97 static char *lacp_rate = NULL;
98 static char *ad_select = NULL;
99 static char *xmit_hash_policy = NULL;
100 static int arp_interval = BOND_LINK_ARP_INTERV;
101 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
102 static char *arp_validate = NULL;
103 static char *fail_over_mac = NULL;
104 struct bond_params bonding_defaults;
106 module_param(max_bonds, int, 0);
107 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
108 module_param(num_grat_arp, int, 0644);
109 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
110 module_param(num_unsol_na, int, 0644);
111 MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
112 module_param(miimon, int, 0);
113 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
114 module_param(updelay, int, 0);
115 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
116 module_param(downdelay, int, 0);
117 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
118 "in milliseconds");
119 module_param(use_carrier, int, 0);
120 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
121 "0 for off, 1 for on (default)");
122 module_param(mode, charp, 0);
123 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
124 "1 for active-backup, 2 for balance-xor, "
125 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
126 "6 for balance-alb");
127 module_param(primary, charp, 0);
128 MODULE_PARM_DESC(primary, "Primary network device to use");
129 module_param(lacp_rate, charp, 0);
130 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
131 "(slow/fast)");
132 module_param(ad_select, charp, 0);
133 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
134 module_param(xmit_hash_policy, charp, 0);
135 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
136 ", 1 for layer 3+4");
137 module_param(arp_interval, int, 0);
138 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
139 module_param_array(arp_ip_target, charp, NULL, 0);
140 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
141 module_param(arp_validate, charp, 0);
142 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
143 module_param(fail_over_mac, charp, 0);
144 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
146 /*----------------------------- Global variables ----------------------------*/
148 static const char * const version =
149 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
151 LIST_HEAD(bond_dev_list);
153 #ifdef CONFIG_PROC_FS
154 static struct proc_dir_entry *bond_proc_dir = NULL;
155 #endif
157 extern struct rw_semaphore bonding_rwsem;
158 static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
159 static int arp_ip_count = 0;
160 static int bond_mode = BOND_MODE_ROUNDROBIN;
161 static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
162 static int lacp_fast = 0;
165 const struct bond_parm_tbl bond_lacp_tbl[] = {
166 { "slow", AD_LACP_SLOW},
167 { "fast", AD_LACP_FAST},
168 { NULL, -1},
171 const struct bond_parm_tbl bond_mode_tbl[] = {
172 { "balance-rr", BOND_MODE_ROUNDROBIN},
173 { "active-backup", BOND_MODE_ACTIVEBACKUP},
174 { "balance-xor", BOND_MODE_XOR},
175 { "broadcast", BOND_MODE_BROADCAST},
176 { "802.3ad", BOND_MODE_8023AD},
177 { "balance-tlb", BOND_MODE_TLB},
178 { "balance-alb", BOND_MODE_ALB},
179 { NULL, -1},
182 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
183 { "layer2", BOND_XMIT_POLICY_LAYER2},
184 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
185 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
186 { NULL, -1},
189 const struct bond_parm_tbl arp_validate_tbl[] = {
190 { "none", BOND_ARP_VALIDATE_NONE},
191 { "active", BOND_ARP_VALIDATE_ACTIVE},
192 { "backup", BOND_ARP_VALIDATE_BACKUP},
193 { "all", BOND_ARP_VALIDATE_ALL},
194 { NULL, -1},
197 const struct bond_parm_tbl fail_over_mac_tbl[] = {
198 { "none", BOND_FOM_NONE},
199 { "active", BOND_FOM_ACTIVE},
200 { "follow", BOND_FOM_FOLLOW},
201 { NULL, -1},
204 struct bond_parm_tbl ad_select_tbl[] = {
205 { "stable", BOND_AD_STABLE},
206 { "bandwidth", BOND_AD_BANDWIDTH},
207 { "count", BOND_AD_COUNT},
208 { NULL, -1},
211 /*-------------------------- Forward declarations ---------------------------*/
213 static void bond_send_gratuitous_arp(struct bonding *bond);
214 static void bond_deinit(struct net_device *bond_dev);
216 /*---------------------------- General routines -----------------------------*/
218 static const char *bond_mode_name(int mode)
220 static const char *names[] = {
221 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
222 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
223 [BOND_MODE_XOR] = "load balancing (xor)",
224 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
225 [BOND_MODE_8023AD]= "IEEE 802.3ad Dynamic link aggregation",
226 [BOND_MODE_TLB] = "transmit load balancing",
227 [BOND_MODE_ALB] = "adaptive load balancing",
230 if (mode < 0 || mode > BOND_MODE_ALB)
231 return "unknown";
233 return names[mode];
236 /*---------------------------------- VLAN -----------------------------------*/
239 * bond_add_vlan - add a new vlan id on bond
240 * @bond: bond that got the notification
241 * @vlan_id: the vlan id to add
243 * Returns -ENOMEM if allocation failed.
245 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
247 struct vlan_entry *vlan;
249 pr_debug("bond: %s, vlan id %d\n",
250 (bond ? bond->dev->name: "None"), vlan_id);
252 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
253 if (!vlan) {
254 return -ENOMEM;
257 INIT_LIST_HEAD(&vlan->vlan_list);
258 vlan->vlan_id = vlan_id;
260 write_lock_bh(&bond->lock);
262 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
264 write_unlock_bh(&bond->lock);
266 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
268 return 0;
272 * bond_del_vlan - delete a vlan id from bond
273 * @bond: bond that got the notification
274 * @vlan_id: the vlan id to delete
276 * returns -ENODEV if @vlan_id was not found in @bond.
278 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
280 struct vlan_entry *vlan;
281 int res = -ENODEV;
283 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
285 write_lock_bh(&bond->lock);
287 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
288 if (vlan->vlan_id == vlan_id) {
289 list_del(&vlan->vlan_list);
291 if (bond_is_lb(bond))
292 bond_alb_clear_vlan(bond, vlan_id);
294 pr_debug("removed VLAN ID %d from bond %s\n", vlan_id,
295 bond->dev->name);
297 kfree(vlan);
299 if (list_empty(&bond->vlan_list) &&
300 (bond->slave_cnt == 0)) {
301 /* Last VLAN removed and no slaves, so
302 * restore block on adding VLANs. This will
303 * be removed once new slaves that are not
304 * VLAN challenged will be added.
306 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
309 res = 0;
310 goto out;
314 pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id,
315 bond->dev->name);
317 out:
318 write_unlock_bh(&bond->lock);
319 return res;
323 * bond_has_challenged_slaves
324 * @bond: the bond we're working on
326 * Searches the slave list. Returns 1 if a vlan challenged slave
327 * was found, 0 otherwise.
329 * Assumes bond->lock is held.
331 static int bond_has_challenged_slaves(struct bonding *bond)
333 struct slave *slave;
334 int i;
336 bond_for_each_slave(bond, slave, i) {
337 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
338 pr_debug("found VLAN challenged slave - %s\n",
339 slave->dev->name);
340 return 1;
344 pr_debug("no VLAN challenged slaves found\n");
345 return 0;
349 * bond_next_vlan - safely skip to the next item in the vlans list.
350 * @bond: the bond we're working on
351 * @curr: item we're advancing from
353 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
354 * or @curr->next otherwise (even if it is @curr itself again).
356 * Caller must hold bond->lock
358 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
360 struct vlan_entry *next, *last;
362 if (list_empty(&bond->vlan_list)) {
363 return NULL;
366 if (!curr) {
367 next = list_entry(bond->vlan_list.next,
368 struct vlan_entry, vlan_list);
369 } else {
370 last = list_entry(bond->vlan_list.prev,
371 struct vlan_entry, vlan_list);
372 if (last == curr) {
373 next = list_entry(bond->vlan_list.next,
374 struct vlan_entry, vlan_list);
375 } else {
376 next = list_entry(curr->vlan_list.next,
377 struct vlan_entry, vlan_list);
381 return next;
385 * bond_dev_queue_xmit - Prepare skb for xmit.
387 * @bond: bond device that got this skb for tx.
388 * @skb: hw accel VLAN tagged skb to transmit
389 * @slave_dev: slave that is supposed to xmit this skbuff
391 * When the bond gets an skb to transmit that is
392 * already hardware accelerated VLAN tagged, and it
393 * needs to relay this skb to a slave that is not
394 * hw accel capable, the skb needs to be "unaccelerated",
395 * i.e. strip the hwaccel tag and re-insert it as part
396 * of the payload.
398 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
400 unsigned short uninitialized_var(vlan_id);
402 if (!list_empty(&bond->vlan_list) &&
403 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
404 vlan_get_tag(skb, &vlan_id) == 0) {
405 skb->dev = slave_dev;
406 skb = vlan_put_tag(skb, vlan_id);
407 if (!skb) {
408 /* vlan_put_tag() frees the skb in case of error,
409 * so return success here so the calling functions
410 * won't attempt to free is again.
412 return 0;
414 } else {
415 skb->dev = slave_dev;
418 skb->priority = 1;
419 dev_queue_xmit(skb);
421 return 0;
425 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
426 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
427 * lock because:
428 * a. This operation is performed in IOCTL context,
429 * b. The operation is protected by the RTNL semaphore in the 8021q code,
430 * c. Holding a lock with BH disabled while directly calling a base driver
431 * entry point is generally a BAD idea.
433 * The design of synchronization/protection for this operation in the 8021q
434 * module is good for one or more VLAN devices over a single physical device
435 * and cannot be extended for a teaming solution like bonding, so there is a
436 * potential race condition here where a net device from the vlan group might
437 * be referenced (either by a base driver or the 8021q code) while it is being
438 * removed from the system. However, it turns out we're not making matters
439 * worse, and if it works for regular VLAN usage it will work here too.
443 * bond_vlan_rx_register - Propagates registration to slaves
444 * @bond_dev: bonding net device that got called
445 * @grp: vlan group being registered
447 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
449 struct bonding *bond = netdev_priv(bond_dev);
450 struct slave *slave;
451 int i;
453 bond->vlgrp = grp;
455 bond_for_each_slave(bond, slave, i) {
456 struct net_device *slave_dev = slave->dev;
457 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
459 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
460 slave_ops->ndo_vlan_rx_register) {
461 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
467 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
468 * @bond_dev: bonding net device that got called
469 * @vid: vlan id being added
471 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
473 struct bonding *bond = netdev_priv(bond_dev);
474 struct slave *slave;
475 int i, res;
477 bond_for_each_slave(bond, slave, i) {
478 struct net_device *slave_dev = slave->dev;
479 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
481 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
482 slave_ops->ndo_vlan_rx_add_vid) {
483 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
487 res = bond_add_vlan(bond, vid);
488 if (res) {
489 printk(KERN_ERR DRV_NAME
490 ": %s: Error: Failed to add vlan id %d\n",
491 bond_dev->name, vid);
496 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
497 * @bond_dev: bonding net device that got called
498 * @vid: vlan id being removed
500 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
502 struct bonding *bond = netdev_priv(bond_dev);
503 struct slave *slave;
504 struct net_device *vlan_dev;
505 int i, res;
507 bond_for_each_slave(bond, slave, i) {
508 struct net_device *slave_dev = slave->dev;
509 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
511 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
512 slave_ops->ndo_vlan_rx_kill_vid) {
513 /* Save and then restore vlan_dev in the grp array,
514 * since the slave's driver might clear it.
516 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
517 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
518 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
522 res = bond_del_vlan(bond, vid);
523 if (res) {
524 printk(KERN_ERR DRV_NAME
525 ": %s: Error: Failed to remove vlan id %d\n",
526 bond_dev->name, vid);
530 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
532 struct vlan_entry *vlan;
533 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
535 write_lock_bh(&bond->lock);
537 if (list_empty(&bond->vlan_list))
538 goto out;
540 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
541 slave_ops->ndo_vlan_rx_register)
542 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
544 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
545 !(slave_ops->ndo_vlan_rx_add_vid))
546 goto out;
548 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
549 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
551 out:
552 write_unlock_bh(&bond->lock);
555 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
557 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
558 struct vlan_entry *vlan;
559 struct net_device *vlan_dev;
561 write_lock_bh(&bond->lock);
563 if (list_empty(&bond->vlan_list))
564 goto out;
566 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
567 !(slave_ops->ndo_vlan_rx_kill_vid))
568 goto unreg;
570 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
571 /* Save and then restore vlan_dev in the grp array,
572 * since the slave's driver might clear it.
574 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
575 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
576 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
579 unreg:
580 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
581 slave_ops->ndo_vlan_rx_register)
582 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
584 out:
585 write_unlock_bh(&bond->lock);
588 /*------------------------------- Link status -------------------------------*/
591 * Set the carrier state for the master according to the state of its
592 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
593 * do special 802.3ad magic.
595 * Returns zero if carrier state does not change, nonzero if it does.
597 static int bond_set_carrier(struct bonding *bond)
599 struct slave *slave;
600 int i;
602 if (bond->slave_cnt == 0)
603 goto down;
605 if (bond->params.mode == BOND_MODE_8023AD)
606 return bond_3ad_set_carrier(bond);
608 bond_for_each_slave(bond, slave, i) {
609 if (slave->link == BOND_LINK_UP) {
610 if (!netif_carrier_ok(bond->dev)) {
611 netif_carrier_on(bond->dev);
612 return 1;
614 return 0;
618 down:
619 if (netif_carrier_ok(bond->dev)) {
620 netif_carrier_off(bond->dev);
621 return 1;
623 return 0;
627 * Get link speed and duplex from the slave's base driver
628 * using ethtool. If for some reason the call fails or the
629 * values are invalid, fake speed and duplex to 100/Full
630 * and return error.
632 static int bond_update_speed_duplex(struct slave *slave)
634 struct net_device *slave_dev = slave->dev;
635 struct ethtool_cmd etool;
636 int res;
638 /* Fake speed and duplex */
639 slave->speed = SPEED_100;
640 slave->duplex = DUPLEX_FULL;
642 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
643 return -1;
645 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
646 if (res < 0)
647 return -1;
649 switch (etool.speed) {
650 case SPEED_10:
651 case SPEED_100:
652 case SPEED_1000:
653 case SPEED_10000:
654 break;
655 default:
656 return -1;
659 switch (etool.duplex) {
660 case DUPLEX_FULL:
661 case DUPLEX_HALF:
662 break;
663 default:
664 return -1;
667 slave->speed = etool.speed;
668 slave->duplex = etool.duplex;
670 return 0;
674 * if <dev> supports MII link status reporting, check its link status.
676 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
677 * depening upon the setting of the use_carrier parameter.
679 * Return either BMSR_LSTATUS, meaning that the link is up (or we
680 * can't tell and just pretend it is), or 0, meaning that the link is
681 * down.
683 * If reporting is non-zero, instead of faking link up, return -1 if
684 * both ETHTOOL and MII ioctls fail (meaning the device does not
685 * support them). If use_carrier is set, return whatever it says.
686 * It'd be nice if there was a good way to tell if a driver supports
687 * netif_carrier, but there really isn't.
689 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
691 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
692 static int (* ioctl)(struct net_device *, struct ifreq *, int);
693 struct ifreq ifr;
694 struct mii_ioctl_data *mii;
696 if (bond->params.use_carrier)
697 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
699 ioctl = slave_ops->ndo_do_ioctl;
700 if (ioctl) {
701 /* TODO: set pointer to correct ioctl on a per team member */
702 /* bases to make this more efficient. that is, once */
703 /* we determine the correct ioctl, we will always */
704 /* call it and not the others for that team */
705 /* member. */
708 * We cannot assume that SIOCGMIIPHY will also read a
709 * register; not all network drivers (e.g., e100)
710 * support that.
713 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
714 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
715 mii = if_mii(&ifr);
716 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
717 mii->reg_num = MII_BMSR;
718 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
719 return (mii->val_out & BMSR_LSTATUS);
725 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
726 * attempt to get link status from it if the above MII ioctls fail.
728 if (slave_dev->ethtool_ops) {
729 if (slave_dev->ethtool_ops->get_link) {
730 u32 link;
732 link = slave_dev->ethtool_ops->get_link(slave_dev);
734 return link ? BMSR_LSTATUS : 0;
739 * If reporting, report that either there's no dev->do_ioctl,
740 * or both SIOCGMIIREG and get_link failed (meaning that we
741 * cannot report link status). If not reporting, pretend
742 * we're ok.
744 return (reporting ? -1 : BMSR_LSTATUS);
747 /*----------------------------- Multicast list ------------------------------*/
750 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
752 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
754 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
755 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
759 * returns dmi entry if found, NULL otherwise
761 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
763 struct dev_mc_list *idmi;
765 for (idmi = mc_list; idmi; idmi = idmi->next) {
766 if (bond_is_dmi_same(dmi, idmi)) {
767 return idmi;
771 return NULL;
775 * Push the promiscuity flag down to appropriate slaves
777 static int bond_set_promiscuity(struct bonding *bond, int inc)
779 int err = 0;
780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
783 err = dev_set_promiscuity(bond->curr_active_slave->dev,
784 inc);
786 } else {
787 struct slave *slave;
788 int i;
789 bond_for_each_slave(bond, slave, i) {
790 err = dev_set_promiscuity(slave->dev, inc);
791 if (err)
792 return err;
795 return err;
799 * Push the allmulti flag down to all slaves
801 static int bond_set_allmulti(struct bonding *bond, int inc)
803 int err = 0;
804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
807 err = dev_set_allmulti(bond->curr_active_slave->dev,
808 inc);
810 } else {
811 struct slave *slave;
812 int i;
813 bond_for_each_slave(bond, slave, i) {
814 err = dev_set_allmulti(slave->dev, inc);
815 if (err)
816 return err;
819 return err;
823 * Add a Multicast address to slaves
824 * according to mode
826 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
828 if (USES_PRIMARY(bond->params.mode)) {
829 /* write lock already acquired */
830 if (bond->curr_active_slave) {
831 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
833 } else {
834 struct slave *slave;
835 int i;
836 bond_for_each_slave(bond, slave, i) {
837 dev_mc_add(slave->dev, addr, alen, 0);
843 * Remove a multicast address from slave
844 * according to mode
846 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
848 if (USES_PRIMARY(bond->params.mode)) {
849 /* write lock already acquired */
850 if (bond->curr_active_slave) {
851 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
853 } else {
854 struct slave *slave;
855 int i;
856 bond_for_each_slave(bond, slave, i) {
857 dev_mc_delete(slave->dev, addr, alen, 0);
864 * Retrieve the list of registered multicast addresses for the bonding
865 * device and retransmit an IGMP JOIN request to the current active
866 * slave.
868 static void bond_resend_igmp_join_requests(struct bonding *bond)
870 struct in_device *in_dev;
871 struct ip_mc_list *im;
873 rcu_read_lock();
874 in_dev = __in_dev_get_rcu(bond->dev);
875 if (in_dev) {
876 for (im = in_dev->mc_list; im; im = im->next) {
877 ip_mc_rejoin_group(im);
881 rcu_read_unlock();
885 * Totally destroys the mc_list in bond
887 static void bond_mc_list_destroy(struct bonding *bond)
889 struct dev_mc_list *dmi;
891 dmi = bond->mc_list;
892 while (dmi) {
893 bond->mc_list = dmi->next;
894 kfree(dmi);
895 dmi = bond->mc_list;
897 bond->mc_list = NULL;
901 * Copy all the Multicast addresses from src to the bonding device dst
903 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
904 gfp_t gfp_flag)
906 struct dev_mc_list *dmi, *new_dmi;
908 for (dmi = mc_list; dmi; dmi = dmi->next) {
909 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
911 if (!new_dmi) {
912 /* FIXME: Potential memory leak !!! */
913 return -ENOMEM;
916 new_dmi->next = bond->mc_list;
917 bond->mc_list = new_dmi;
918 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
919 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
920 new_dmi->dmi_users = dmi->dmi_users;
921 new_dmi->dmi_gusers = dmi->dmi_gusers;
924 return 0;
928 * flush all members of flush->mc_list from device dev->mc_list
930 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
932 struct bonding *bond = netdev_priv(bond_dev);
933 struct dev_mc_list *dmi;
935 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
936 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
939 if (bond->params.mode == BOND_MODE_8023AD) {
940 /* del lacpdu mc addr from mc list */
941 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
943 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
947 /*--------------------------- Active slave change ---------------------------*/
950 * Update the mc list and multicast-related flags for the new and
951 * old active slaves (if any) according to the multicast mode, and
952 * promiscuous flags unconditionally.
954 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
956 struct dev_mc_list *dmi;
958 if (!USES_PRIMARY(bond->params.mode)) {
959 /* nothing to do - mc list is already up-to-date on
960 * all slaves
962 return;
965 if (old_active) {
966 if (bond->dev->flags & IFF_PROMISC) {
967 dev_set_promiscuity(old_active->dev, -1);
970 if (bond->dev->flags & IFF_ALLMULTI) {
971 dev_set_allmulti(old_active->dev, -1);
974 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
975 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
979 if (new_active) {
980 /* FIXME: Signal errors upstream. */
981 if (bond->dev->flags & IFF_PROMISC) {
982 dev_set_promiscuity(new_active->dev, 1);
985 if (bond->dev->flags & IFF_ALLMULTI) {
986 dev_set_allmulti(new_active->dev, 1);
989 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
990 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
992 bond_resend_igmp_join_requests(bond);
997 * bond_do_fail_over_mac
999 * Perform special MAC address swapping for fail_over_mac settings
1001 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1003 static void bond_do_fail_over_mac(struct bonding *bond,
1004 struct slave *new_active,
1005 struct slave *old_active)
1007 u8 tmp_mac[ETH_ALEN];
1008 struct sockaddr saddr;
1009 int rv;
1011 switch (bond->params.fail_over_mac) {
1012 case BOND_FOM_ACTIVE:
1013 if (new_active)
1014 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1015 new_active->dev->addr_len);
1016 break;
1017 case BOND_FOM_FOLLOW:
1019 * if new_active && old_active, swap them
1020 * if just old_active, do nothing (going to no active slave)
1021 * if just new_active, set new_active to bond's MAC
1023 if (!new_active)
1024 return;
1026 write_unlock_bh(&bond->curr_slave_lock);
1027 read_unlock(&bond->lock);
1029 if (old_active) {
1030 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1031 memcpy(saddr.sa_data, old_active->dev->dev_addr,
1032 ETH_ALEN);
1033 saddr.sa_family = new_active->dev->type;
1034 } else {
1035 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1036 saddr.sa_family = bond->dev->type;
1039 rv = dev_set_mac_address(new_active->dev, &saddr);
1040 if (rv) {
1041 printk(KERN_ERR DRV_NAME
1042 ": %s: Error %d setting MAC of slave %s\n",
1043 bond->dev->name, -rv, new_active->dev->name);
1044 goto out;
1047 if (!old_active)
1048 goto out;
1050 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1051 saddr.sa_family = old_active->dev->type;
1053 rv = dev_set_mac_address(old_active->dev, &saddr);
1054 if (rv)
1055 printk(KERN_ERR DRV_NAME
1056 ": %s: Error %d setting MAC of slave %s\n",
1057 bond->dev->name, -rv, new_active->dev->name);
1058 out:
1059 read_lock(&bond->lock);
1060 write_lock_bh(&bond->curr_slave_lock);
1061 break;
1062 default:
1063 printk(KERN_ERR DRV_NAME
1064 ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1065 bond->dev->name, bond->params.fail_over_mac);
1066 break;
1073 * find_best_interface - select the best available slave to be the active one
1074 * @bond: our bonding struct
1076 * Warning: Caller must hold curr_slave_lock for writing.
1078 static struct slave *bond_find_best_slave(struct bonding *bond)
1080 struct slave *new_active, *old_active;
1081 struct slave *bestslave = NULL;
1082 int mintime = bond->params.updelay;
1083 int i;
1085 new_active = old_active = bond->curr_active_slave;
1087 if (!new_active) { /* there were no active slaves left */
1088 if (bond->slave_cnt > 0) { /* found one slave */
1089 new_active = bond->first_slave;
1090 } else {
1091 return NULL; /* still no slave, return NULL */
1095 /* first try the primary link; if arping, a link must tx/rx traffic
1096 * before it can be considered the curr_active_slave - also, we would skip
1097 * slaves between the curr_active_slave and primary_slave that may be up
1098 * and able to arp
1100 if ((bond->primary_slave) &&
1101 (!bond->params.arp_interval) &&
1102 (IS_UP(bond->primary_slave->dev))) {
1103 new_active = bond->primary_slave;
1106 /* remember where to stop iterating over the slaves */
1107 old_active = new_active;
1109 bond_for_each_slave_from(bond, new_active, i, old_active) {
1110 if (IS_UP(new_active->dev)) {
1111 if (new_active->link == BOND_LINK_UP) {
1112 return new_active;
1113 } else if (new_active->link == BOND_LINK_BACK) {
1114 /* link up, but waiting for stabilization */
1115 if (new_active->delay < mintime) {
1116 mintime = new_active->delay;
1117 bestslave = new_active;
1123 return bestslave;
1127 * change_active_interface - change the active slave into the specified one
1128 * @bond: our bonding struct
1129 * @new: the new slave to make the active one
1131 * Set the new slave to the bond's settings and unset them on the old
1132 * curr_active_slave.
1133 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1135 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1136 * because it is apparently the best available slave we have, even though its
1137 * updelay hasn't timed out yet.
1139 * If new_active is not NULL, caller must hold bond->lock for read and
1140 * curr_slave_lock for write_bh.
1142 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1144 struct slave *old_active = bond->curr_active_slave;
1146 if (old_active == new_active) {
1147 return;
1150 if (new_active) {
1151 new_active->jiffies = jiffies;
1153 if (new_active->link == BOND_LINK_BACK) {
1154 if (USES_PRIMARY(bond->params.mode)) {
1155 printk(KERN_INFO DRV_NAME
1156 ": %s: making interface %s the new "
1157 "active one %d ms earlier.\n",
1158 bond->dev->name, new_active->dev->name,
1159 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1162 new_active->delay = 0;
1163 new_active->link = BOND_LINK_UP;
1165 if (bond->params.mode == BOND_MODE_8023AD) {
1166 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1169 if (bond_is_lb(bond))
1170 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1171 } else {
1172 if (USES_PRIMARY(bond->params.mode)) {
1173 printk(KERN_INFO DRV_NAME
1174 ": %s: making interface %s the new "
1175 "active one.\n",
1176 bond->dev->name, new_active->dev->name);
1181 if (USES_PRIMARY(bond->params.mode)) {
1182 bond_mc_swap(bond, new_active, old_active);
1185 if (bond_is_lb(bond)) {
1186 bond_alb_handle_active_change(bond, new_active);
1187 if (old_active)
1188 bond_set_slave_inactive_flags(old_active);
1189 if (new_active)
1190 bond_set_slave_active_flags(new_active);
1191 } else {
1192 bond->curr_active_slave = new_active;
1195 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1196 if (old_active) {
1197 bond_set_slave_inactive_flags(old_active);
1200 if (new_active) {
1201 bond_set_slave_active_flags(new_active);
1203 if (bond->params.fail_over_mac)
1204 bond_do_fail_over_mac(bond, new_active,
1205 old_active);
1207 bond->send_grat_arp = bond->params.num_grat_arp;
1208 bond_send_gratuitous_arp(bond);
1210 bond->send_unsol_na = bond->params.num_unsol_na;
1211 bond_send_unsolicited_na(bond);
1213 write_unlock_bh(&bond->curr_slave_lock);
1214 read_unlock(&bond->lock);
1216 netdev_bonding_change(bond->dev);
1218 read_lock(&bond->lock);
1219 write_lock_bh(&bond->curr_slave_lock);
1225 * bond_select_active_slave - select a new active slave, if needed
1226 * @bond: our bonding struct
1228 * This functions shoud be called when one of the following occurs:
1229 * - The old curr_active_slave has been released or lost its link.
1230 * - The primary_slave has got its link back.
1231 * - A slave has got its link back and there's no old curr_active_slave.
1233 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1235 void bond_select_active_slave(struct bonding *bond)
1237 struct slave *best_slave;
1238 int rv;
1240 best_slave = bond_find_best_slave(bond);
1241 if (best_slave != bond->curr_active_slave) {
1242 bond_change_active_slave(bond, best_slave);
1243 rv = bond_set_carrier(bond);
1244 if (!rv)
1245 return;
1247 if (netif_carrier_ok(bond->dev)) {
1248 printk(KERN_INFO DRV_NAME
1249 ": %s: first active interface up!\n",
1250 bond->dev->name);
1251 } else {
1252 printk(KERN_INFO DRV_NAME ": %s: "
1253 "now running without any active interface !\n",
1254 bond->dev->name);
1259 /*--------------------------- slave list handling ---------------------------*/
1262 * This function attaches the slave to the end of list.
1264 * bond->lock held for writing by caller.
1266 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1268 if (bond->first_slave == NULL) { /* attaching the first slave */
1269 new_slave->next = new_slave;
1270 new_slave->prev = new_slave;
1271 bond->first_slave = new_slave;
1272 } else {
1273 new_slave->next = bond->first_slave;
1274 new_slave->prev = bond->first_slave->prev;
1275 new_slave->next->prev = new_slave;
1276 new_slave->prev->next = new_slave;
1279 bond->slave_cnt++;
1283 * This function detaches the slave from the list.
1284 * WARNING: no check is made to verify if the slave effectively
1285 * belongs to <bond>.
1286 * Nothing is freed on return, structures are just unchained.
1287 * If any slave pointer in bond was pointing to <slave>,
1288 * it should be changed by the calling function.
1290 * bond->lock held for writing by caller.
1292 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1294 if (slave->next) {
1295 slave->next->prev = slave->prev;
1298 if (slave->prev) {
1299 slave->prev->next = slave->next;
1302 if (bond->first_slave == slave) { /* slave is the first slave */
1303 if (bond->slave_cnt > 1) { /* there are more slave */
1304 bond->first_slave = slave->next;
1305 } else {
1306 bond->first_slave = NULL; /* slave was the last one */
1310 slave->next = NULL;
1311 slave->prev = NULL;
1312 bond->slave_cnt--;
1315 /*---------------------------------- IOCTL ----------------------------------*/
1317 static int bond_sethwaddr(struct net_device *bond_dev,
1318 struct net_device *slave_dev)
1320 pr_debug("bond_dev=%p\n", bond_dev);
1321 pr_debug("slave_dev=%p\n", slave_dev);
1322 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1323 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1324 return 0;
1327 #define BOND_VLAN_FEATURES \
1328 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1329 NETIF_F_HW_VLAN_FILTER)
1332 * Compute the common dev->feature set available to all slaves. Some
1333 * feature bits are managed elsewhere, so preserve those feature bits
1334 * on the master device.
1336 static int bond_compute_features(struct bonding *bond)
1338 struct slave *slave;
1339 struct net_device *bond_dev = bond->dev;
1340 unsigned long features = bond_dev->features;
1341 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1342 bond_dev->hard_header_len);
1343 int i;
1345 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1346 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1348 if (!bond->first_slave)
1349 goto done;
1351 features &= ~NETIF_F_ONE_FOR_ALL;
1353 bond_for_each_slave(bond, slave, i) {
1354 features = netdev_increment_features(features,
1355 slave->dev->features,
1356 NETIF_F_ONE_FOR_ALL);
1357 if (slave->dev->hard_header_len > max_hard_header_len)
1358 max_hard_header_len = slave->dev->hard_header_len;
1361 done:
1362 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1363 bond_dev->features = netdev_fix_features(features, NULL);
1364 bond_dev->hard_header_len = max_hard_header_len;
1366 return 0;
1369 static void bond_setup_by_slave(struct net_device *bond_dev,
1370 struct net_device *slave_dev)
1372 struct bonding *bond = netdev_priv(bond_dev);
1374 bond_dev->header_ops = slave_dev->header_ops;
1376 bond_dev->type = slave_dev->type;
1377 bond_dev->hard_header_len = slave_dev->hard_header_len;
1378 bond_dev->addr_len = slave_dev->addr_len;
1380 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1381 slave_dev->addr_len);
1382 bond->setup_by_slave = 1;
1385 /* enslave device <slave> to bond device <master> */
1386 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1388 struct bonding *bond = netdev_priv(bond_dev);
1389 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1390 struct slave *new_slave = NULL;
1391 struct dev_mc_list *dmi;
1392 struct sockaddr addr;
1393 int link_reporting;
1394 int old_features = bond_dev->features;
1395 int res = 0;
1397 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1398 slave_ops->ndo_do_ioctl == NULL) {
1399 printk(KERN_WARNING DRV_NAME
1400 ": %s: Warning: no link monitoring support for %s\n",
1401 bond_dev->name, slave_dev->name);
1404 /* bond must be initialized by bond_open() before enslaving */
1405 if (!(bond_dev->flags & IFF_UP)) {
1406 printk(KERN_WARNING DRV_NAME
1407 " %s: master_dev is not up in bond_enslave\n",
1408 bond_dev->name);
1411 /* already enslaved */
1412 if (slave_dev->flags & IFF_SLAVE) {
1413 pr_debug("Error, Device was already enslaved\n");
1414 return -EBUSY;
1417 /* vlan challenged mutual exclusion */
1418 /* no need to lock since we're protected by rtnl_lock */
1419 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1420 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1421 if (!list_empty(&bond->vlan_list)) {
1422 printk(KERN_ERR DRV_NAME
1423 ": %s: Error: cannot enslave VLAN "
1424 "challenged slave %s on VLAN enabled "
1425 "bond %s\n", bond_dev->name, slave_dev->name,
1426 bond_dev->name);
1427 return -EPERM;
1428 } else {
1429 printk(KERN_WARNING DRV_NAME
1430 ": %s: Warning: enslaved VLAN challenged "
1431 "slave %s. Adding VLANs will be blocked as "
1432 "long as %s is part of bond %s\n",
1433 bond_dev->name, slave_dev->name, slave_dev->name,
1434 bond_dev->name);
1435 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1437 } else {
1438 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1439 if (bond->slave_cnt == 0) {
1440 /* First slave, and it is not VLAN challenged,
1441 * so remove the block of adding VLANs over the bond.
1443 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1448 * Old ifenslave binaries are no longer supported. These can
1449 * be identified with moderate accurary by the state of the slave:
1450 * the current ifenslave will set the interface down prior to
1451 * enslaving it; the old ifenslave will not.
1453 if ((slave_dev->flags & IFF_UP)) {
1454 printk(KERN_ERR DRV_NAME ": %s is up. "
1455 "This may be due to an out of date ifenslave.\n",
1456 slave_dev->name);
1457 res = -EPERM;
1458 goto err_undo_flags;
1461 /* set bonding device ether type by slave - bonding netdevices are
1462 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1463 * there is a need to override some of the type dependent attribs/funcs.
1465 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1466 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1468 if (bond->slave_cnt == 0) {
1469 if (slave_dev->type != ARPHRD_ETHER)
1470 bond_setup_by_slave(bond_dev, slave_dev);
1471 } else if (bond_dev->type != slave_dev->type) {
1472 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1473 "from other slaves (%d), can not enslave it.\n",
1474 slave_dev->name,
1475 slave_dev->type, bond_dev->type);
1476 res = -EINVAL;
1477 goto err_undo_flags;
1480 if (slave_ops->ndo_set_mac_address == NULL) {
1481 if (bond->slave_cnt == 0) {
1482 printk(KERN_WARNING DRV_NAME
1483 ": %s: Warning: The first slave device "
1484 "specified does not support setting the MAC "
1485 "address. Setting fail_over_mac to active.",
1486 bond_dev->name);
1487 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1488 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1489 printk(KERN_ERR DRV_NAME
1490 ": %s: Error: The slave device specified "
1491 "does not support setting the MAC address, "
1492 "but fail_over_mac is not set to active.\n"
1493 , bond_dev->name);
1494 res = -EOPNOTSUPP;
1495 goto err_undo_flags;
1499 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1500 if (!new_slave) {
1501 res = -ENOMEM;
1502 goto err_undo_flags;
1505 /* save slave's original flags before calling
1506 * netdev_set_master and dev_open
1508 new_slave->original_flags = slave_dev->flags;
1511 * Save slave's original ("permanent") mac address for modes
1512 * that need it, and for restoring it upon release, and then
1513 * set it to the master's address
1515 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1517 if (!bond->params.fail_over_mac) {
1519 * Set slave to master's mac address. The application already
1520 * set the master's mac address to that of the first slave
1522 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1523 addr.sa_family = slave_dev->type;
1524 res = dev_set_mac_address(slave_dev, &addr);
1525 if (res) {
1526 pr_debug("Error %d calling set_mac_address\n", res);
1527 goto err_free;
1531 res = netdev_set_master(slave_dev, bond_dev);
1532 if (res) {
1533 pr_debug("Error %d calling netdev_set_master\n", res);
1534 goto err_restore_mac;
1536 /* open the slave since the application closed it */
1537 res = dev_open(slave_dev);
1538 if (res) {
1539 pr_debug("Openning slave %s failed\n", slave_dev->name);
1540 goto err_unset_master;
1543 new_slave->dev = slave_dev;
1544 slave_dev->priv_flags |= IFF_BONDING;
1546 if (bond_is_lb(bond)) {
1547 /* bond_alb_init_slave() must be called before all other stages since
1548 * it might fail and we do not want to have to undo everything
1550 res = bond_alb_init_slave(bond, new_slave);
1551 if (res) {
1552 goto err_close;
1556 /* If the mode USES_PRIMARY, then the new slave gets the
1557 * master's promisc (and mc) settings only if it becomes the
1558 * curr_active_slave, and that is taken care of later when calling
1559 * bond_change_active()
1561 if (!USES_PRIMARY(bond->params.mode)) {
1562 /* set promiscuity level to new slave */
1563 if (bond_dev->flags & IFF_PROMISC) {
1564 res = dev_set_promiscuity(slave_dev, 1);
1565 if (res)
1566 goto err_close;
1569 /* set allmulti level to new slave */
1570 if (bond_dev->flags & IFF_ALLMULTI) {
1571 res = dev_set_allmulti(slave_dev, 1);
1572 if (res)
1573 goto err_close;
1576 netif_addr_lock_bh(bond_dev);
1577 /* upload master's mc_list to new slave */
1578 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1579 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1581 netif_addr_unlock_bh(bond_dev);
1584 if (bond->params.mode == BOND_MODE_8023AD) {
1585 /* add lacpdu mc addr to mc list */
1586 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1588 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1591 bond_add_vlans_on_slave(bond, slave_dev);
1593 write_lock_bh(&bond->lock);
1595 bond_attach_slave(bond, new_slave);
1597 new_slave->delay = 0;
1598 new_slave->link_failure_count = 0;
1600 bond_compute_features(bond);
1602 write_unlock_bh(&bond->lock);
1604 read_lock(&bond->lock);
1606 new_slave->last_arp_rx = jiffies;
1608 if (bond->params.miimon && !bond->params.use_carrier) {
1609 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1611 if ((link_reporting == -1) && !bond->params.arp_interval) {
1613 * miimon is set but a bonded network driver
1614 * does not support ETHTOOL/MII and
1615 * arp_interval is not set. Note: if
1616 * use_carrier is enabled, we will never go
1617 * here (because netif_carrier is always
1618 * supported); thus, we don't need to change
1619 * the messages for netif_carrier.
1621 printk(KERN_WARNING DRV_NAME
1622 ": %s: Warning: MII and ETHTOOL support not "
1623 "available for interface %s, and "
1624 "arp_interval/arp_ip_target module parameters "
1625 "not specified, thus bonding will not detect "
1626 "link failures! see bonding.txt for details.\n",
1627 bond_dev->name, slave_dev->name);
1628 } else if (link_reporting == -1) {
1629 /* unable get link status using mii/ethtool */
1630 printk(KERN_WARNING DRV_NAME
1631 ": %s: Warning: can't get link status from "
1632 "interface %s; the network driver associated "
1633 "with this interface does not support MII or "
1634 "ETHTOOL link status reporting, thus miimon "
1635 "has no effect on this interface.\n",
1636 bond_dev->name, slave_dev->name);
1640 /* check for initial state */
1641 if (!bond->params.miimon ||
1642 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1643 if (bond->params.updelay) {
1644 pr_debug("Initial state of slave_dev is "
1645 "BOND_LINK_BACK\n");
1646 new_slave->link = BOND_LINK_BACK;
1647 new_slave->delay = bond->params.updelay;
1648 } else {
1649 pr_debug("Initial state of slave_dev is "
1650 "BOND_LINK_UP\n");
1651 new_slave->link = BOND_LINK_UP;
1653 new_slave->jiffies = jiffies;
1654 } else {
1655 pr_debug("Initial state of slave_dev is "
1656 "BOND_LINK_DOWN\n");
1657 new_slave->link = BOND_LINK_DOWN;
1660 if (bond_update_speed_duplex(new_slave) &&
1661 (new_slave->link != BOND_LINK_DOWN)) {
1662 printk(KERN_WARNING DRV_NAME
1663 ": %s: Warning: failed to get speed and duplex from %s, "
1664 "assumed to be 100Mb/sec and Full.\n",
1665 bond_dev->name, new_slave->dev->name);
1667 if (bond->params.mode == BOND_MODE_8023AD) {
1668 printk(KERN_WARNING DRV_NAME
1669 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1670 "support in base driver for proper aggregator "
1671 "selection.\n", bond_dev->name);
1675 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1676 /* if there is a primary slave, remember it */
1677 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1678 bond->primary_slave = new_slave;
1682 write_lock_bh(&bond->curr_slave_lock);
1684 switch (bond->params.mode) {
1685 case BOND_MODE_ACTIVEBACKUP:
1686 bond_set_slave_inactive_flags(new_slave);
1687 bond_select_active_slave(bond);
1688 break;
1689 case BOND_MODE_8023AD:
1690 /* in 802.3ad mode, the internal mechanism
1691 * will activate the slaves in the selected
1692 * aggregator
1694 bond_set_slave_inactive_flags(new_slave);
1695 /* if this is the first slave */
1696 if (bond->slave_cnt == 1) {
1697 SLAVE_AD_INFO(new_slave).id = 1;
1698 /* Initialize AD with the number of times that the AD timer is called in 1 second
1699 * can be called only after the mac address of the bond is set
1701 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1702 bond->params.lacp_fast);
1703 } else {
1704 SLAVE_AD_INFO(new_slave).id =
1705 SLAVE_AD_INFO(new_slave->prev).id + 1;
1708 bond_3ad_bind_slave(new_slave);
1709 break;
1710 case BOND_MODE_TLB:
1711 case BOND_MODE_ALB:
1712 new_slave->state = BOND_STATE_ACTIVE;
1713 bond_set_slave_inactive_flags(new_slave);
1714 break;
1715 default:
1716 pr_debug("This slave is always active in trunk mode\n");
1718 /* always active in trunk mode */
1719 new_slave->state = BOND_STATE_ACTIVE;
1721 /* In trunking mode there is little meaning to curr_active_slave
1722 * anyway (it holds no special properties of the bond device),
1723 * so we can change it without calling change_active_interface()
1725 if (!bond->curr_active_slave) {
1726 bond->curr_active_slave = new_slave;
1728 break;
1729 } /* switch(bond_mode) */
1731 write_unlock_bh(&bond->curr_slave_lock);
1733 bond_set_carrier(bond);
1735 read_unlock(&bond->lock);
1737 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1738 if (res)
1739 goto err_close;
1741 printk(KERN_INFO DRV_NAME
1742 ": %s: enslaving %s as a%s interface with a%s link.\n",
1743 bond_dev->name, slave_dev->name,
1744 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1745 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1747 /* enslave is successful */
1748 return 0;
1750 /* Undo stages on error */
1751 err_close:
1752 dev_close(slave_dev);
1754 err_unset_master:
1755 netdev_set_master(slave_dev, NULL);
1757 err_restore_mac:
1758 if (!bond->params.fail_over_mac) {
1759 /* XXX TODO - fom follow mode needs to change master's
1760 * MAC if this slave's MAC is in use by the bond, or at
1761 * least print a warning.
1763 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1764 addr.sa_family = slave_dev->type;
1765 dev_set_mac_address(slave_dev, &addr);
1768 err_free:
1769 kfree(new_slave);
1771 err_undo_flags:
1772 bond_dev->features = old_features;
1774 return res;
1778 * Try to release the slave device <slave> from the bond device <master>
1779 * It is legal to access curr_active_slave without a lock because all the function
1780 * is write-locked.
1782 * The rules for slave state should be:
1783 * for Active/Backup:
1784 * Active stays on all backups go down
1785 * for Bonded connections:
1786 * The first up interface should be left on and all others downed.
1788 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1790 struct bonding *bond = netdev_priv(bond_dev);
1791 struct slave *slave, *oldcurrent;
1792 struct sockaddr addr;
1793 int mac_addr_differ;
1795 /* slave is not a slave or master is not master of this slave */
1796 if (!(slave_dev->flags & IFF_SLAVE) ||
1797 (slave_dev->master != bond_dev)) {
1798 printk(KERN_ERR DRV_NAME
1799 ": %s: Error: cannot release %s.\n",
1800 bond_dev->name, slave_dev->name);
1801 return -EINVAL;
1804 write_lock_bh(&bond->lock);
1806 slave = bond_get_slave_by_dev(bond, slave_dev);
1807 if (!slave) {
1808 /* not a slave of this bond */
1809 printk(KERN_INFO DRV_NAME
1810 ": %s: %s not enslaved\n",
1811 bond_dev->name, slave_dev->name);
1812 write_unlock_bh(&bond->lock);
1813 return -EINVAL;
1816 if (!bond->params.fail_over_mac) {
1817 mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
1818 ETH_ALEN);
1819 if (!mac_addr_differ && (bond->slave_cnt > 1))
1820 printk(KERN_WARNING DRV_NAME
1821 ": %s: Warning: the permanent HWaddr of %s - "
1822 "%pM - is still in use by %s. "
1823 "Set the HWaddr of %s to a different address "
1824 "to avoid conflicts.\n",
1825 bond_dev->name, slave_dev->name,
1826 slave->perm_hwaddr,
1827 bond_dev->name, slave_dev->name);
1830 /* Inform AD package of unbinding of slave. */
1831 if (bond->params.mode == BOND_MODE_8023AD) {
1832 /* must be called before the slave is
1833 * detached from the list
1835 bond_3ad_unbind_slave(slave);
1838 printk(KERN_INFO DRV_NAME
1839 ": %s: releasing %s interface %s\n",
1840 bond_dev->name,
1841 (slave->state == BOND_STATE_ACTIVE)
1842 ? "active" : "backup",
1843 slave_dev->name);
1845 oldcurrent = bond->curr_active_slave;
1847 bond->current_arp_slave = NULL;
1849 /* release the slave from its bond */
1850 bond_detach_slave(bond, slave);
1852 bond_compute_features(bond);
1854 if (bond->primary_slave == slave) {
1855 bond->primary_slave = NULL;
1858 if (oldcurrent == slave) {
1859 bond_change_active_slave(bond, NULL);
1862 if (bond_is_lb(bond)) {
1863 /* Must be called only after the slave has been
1864 * detached from the list and the curr_active_slave
1865 * has been cleared (if our_slave == old_current),
1866 * but before a new active slave is selected.
1868 write_unlock_bh(&bond->lock);
1869 bond_alb_deinit_slave(bond, slave);
1870 write_lock_bh(&bond->lock);
1873 if (oldcurrent == slave) {
1875 * Note that we hold RTNL over this sequence, so there
1876 * is no concern that another slave add/remove event
1877 * will interfere.
1879 write_unlock_bh(&bond->lock);
1880 read_lock(&bond->lock);
1881 write_lock_bh(&bond->curr_slave_lock);
1883 bond_select_active_slave(bond);
1885 write_unlock_bh(&bond->curr_slave_lock);
1886 read_unlock(&bond->lock);
1887 write_lock_bh(&bond->lock);
1890 if (bond->slave_cnt == 0) {
1891 bond_set_carrier(bond);
1893 /* if the last slave was removed, zero the mac address
1894 * of the master so it will be set by the application
1895 * to the mac address of the first slave
1897 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1899 if (list_empty(&bond->vlan_list)) {
1900 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1901 } else {
1902 printk(KERN_WARNING DRV_NAME
1903 ": %s: Warning: clearing HW address of %s while it "
1904 "still has VLANs.\n",
1905 bond_dev->name, bond_dev->name);
1906 printk(KERN_WARNING DRV_NAME
1907 ": %s: When re-adding slaves, make sure the bond's "
1908 "HW address matches its VLANs'.\n",
1909 bond_dev->name);
1911 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1912 !bond_has_challenged_slaves(bond)) {
1913 printk(KERN_INFO DRV_NAME
1914 ": %s: last VLAN challenged slave %s "
1915 "left bond %s. VLAN blocking is removed\n",
1916 bond_dev->name, slave_dev->name, bond_dev->name);
1917 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1920 write_unlock_bh(&bond->lock);
1922 /* must do this from outside any spinlocks */
1923 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1925 bond_del_vlans_from_slave(bond, slave_dev);
1927 /* If the mode USES_PRIMARY, then we should only remove its
1928 * promisc and mc settings if it was the curr_active_slave, but that was
1929 * already taken care of above when we detached the slave
1931 if (!USES_PRIMARY(bond->params.mode)) {
1932 /* unset promiscuity level from slave */
1933 if (bond_dev->flags & IFF_PROMISC) {
1934 dev_set_promiscuity(slave_dev, -1);
1937 /* unset allmulti level from slave */
1938 if (bond_dev->flags & IFF_ALLMULTI) {
1939 dev_set_allmulti(slave_dev, -1);
1942 /* flush master's mc_list from slave */
1943 netif_addr_lock_bh(bond_dev);
1944 bond_mc_list_flush(bond_dev, slave_dev);
1945 netif_addr_unlock_bh(bond_dev);
1948 netdev_set_master(slave_dev, NULL);
1950 /* close slave before restoring its mac address */
1951 dev_close(slave_dev);
1953 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1954 /* restore original ("permanent") mac address */
1955 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1956 addr.sa_family = slave_dev->type;
1957 dev_set_mac_address(slave_dev, &addr);
1960 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1961 IFF_SLAVE_INACTIVE | IFF_BONDING |
1962 IFF_SLAVE_NEEDARP);
1964 kfree(slave);
1966 return 0; /* deletion OK */
1970 * Destroy a bonding device.
1971 * Must be under rtnl_lock when this function is called.
1973 void bond_destroy(struct bonding *bond)
1975 bond_deinit(bond->dev);
1976 bond_destroy_sysfs_entry(bond);
1977 unregister_netdevice(bond->dev);
1980 static void bond_destructor(struct net_device *bond_dev)
1982 struct bonding *bond = netdev_priv(bond_dev);
1984 if (bond->wq)
1985 destroy_workqueue(bond->wq);
1987 netif_addr_lock_bh(bond_dev);
1988 bond_mc_list_destroy(bond);
1989 netif_addr_unlock_bh(bond_dev);
1991 free_netdev(bond_dev);
1995 * First release a slave and than destroy the bond if no more slaves iare left.
1996 * Must be under rtnl_lock when this function is called.
1998 int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
2000 struct bonding *bond = netdev_priv(bond_dev);
2001 int ret;
2003 ret = bond_release(bond_dev, slave_dev);
2004 if ((ret == 0) && (bond->slave_cnt == 0)) {
2005 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
2006 bond_dev->name, bond_dev->name);
2007 bond_destroy(bond);
2009 return ret;
2013 * This function releases all slaves.
2015 static int bond_release_all(struct net_device *bond_dev)
2017 struct bonding *bond = netdev_priv(bond_dev);
2018 struct slave *slave;
2019 struct net_device *slave_dev;
2020 struct sockaddr addr;
2022 write_lock_bh(&bond->lock);
2024 netif_carrier_off(bond_dev);
2026 if (bond->slave_cnt == 0) {
2027 goto out;
2030 bond->current_arp_slave = NULL;
2031 bond->primary_slave = NULL;
2032 bond_change_active_slave(bond, NULL);
2034 while ((slave = bond->first_slave) != NULL) {
2035 /* Inform AD package of unbinding of slave
2036 * before slave is detached from the list.
2038 if (bond->params.mode == BOND_MODE_8023AD) {
2039 bond_3ad_unbind_slave(slave);
2042 slave_dev = slave->dev;
2043 bond_detach_slave(bond, slave);
2045 /* now that the slave is detached, unlock and perform
2046 * all the undo steps that should not be called from
2047 * within a lock.
2049 write_unlock_bh(&bond->lock);
2051 if (bond_is_lb(bond)) {
2052 /* must be called only after the slave
2053 * has been detached from the list
2055 bond_alb_deinit_slave(bond, slave);
2058 bond_compute_features(bond);
2060 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2061 bond_del_vlans_from_slave(bond, slave_dev);
2063 /* If the mode USES_PRIMARY, then we should only remove its
2064 * promisc and mc settings if it was the curr_active_slave, but that was
2065 * already taken care of above when we detached the slave
2067 if (!USES_PRIMARY(bond->params.mode)) {
2068 /* unset promiscuity level from slave */
2069 if (bond_dev->flags & IFF_PROMISC) {
2070 dev_set_promiscuity(slave_dev, -1);
2073 /* unset allmulti level from slave */
2074 if (bond_dev->flags & IFF_ALLMULTI) {
2075 dev_set_allmulti(slave_dev, -1);
2078 /* flush master's mc_list from slave */
2079 netif_addr_lock_bh(bond_dev);
2080 bond_mc_list_flush(bond_dev, slave_dev);
2081 netif_addr_unlock_bh(bond_dev);
2084 netdev_set_master(slave_dev, NULL);
2086 /* close slave before restoring its mac address */
2087 dev_close(slave_dev);
2089 if (!bond->params.fail_over_mac) {
2090 /* restore original ("permanent") mac address*/
2091 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2092 addr.sa_family = slave_dev->type;
2093 dev_set_mac_address(slave_dev, &addr);
2096 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2097 IFF_SLAVE_INACTIVE);
2099 kfree(slave);
2101 /* re-acquire the lock before getting the next slave */
2102 write_lock_bh(&bond->lock);
2105 /* zero the mac address of the master so it will be
2106 * set by the application to the mac address of the
2107 * first slave
2109 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2111 if (list_empty(&bond->vlan_list)) {
2112 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2113 } else {
2114 printk(KERN_WARNING DRV_NAME
2115 ": %s: Warning: clearing HW address of %s while it "
2116 "still has VLANs.\n",
2117 bond_dev->name, bond_dev->name);
2118 printk(KERN_WARNING DRV_NAME
2119 ": %s: When re-adding slaves, make sure the bond's "
2120 "HW address matches its VLANs'.\n",
2121 bond_dev->name);
2124 printk(KERN_INFO DRV_NAME
2125 ": %s: released all slaves\n",
2126 bond_dev->name);
2128 out:
2129 write_unlock_bh(&bond->lock);
2131 return 0;
2135 * This function changes the active slave to slave <slave_dev>.
2136 * It returns -EINVAL in the following cases.
2137 * - <slave_dev> is not found in the list.
2138 * - There is not active slave now.
2139 * - <slave_dev> is already active.
2140 * - The link state of <slave_dev> is not BOND_LINK_UP.
2141 * - <slave_dev> is not running.
2142 * In these cases, this fuction does nothing.
2143 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2145 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2147 struct bonding *bond = netdev_priv(bond_dev);
2148 struct slave *old_active = NULL;
2149 struct slave *new_active = NULL;
2150 int res = 0;
2152 if (!USES_PRIMARY(bond->params.mode)) {
2153 return -EINVAL;
2156 /* Verify that master_dev is indeed the master of slave_dev */
2157 if (!(slave_dev->flags & IFF_SLAVE) ||
2158 (slave_dev->master != bond_dev)) {
2159 return -EINVAL;
2162 read_lock(&bond->lock);
2164 read_lock(&bond->curr_slave_lock);
2165 old_active = bond->curr_active_slave;
2166 read_unlock(&bond->curr_slave_lock);
2168 new_active = bond_get_slave_by_dev(bond, slave_dev);
2171 * Changing to the current active: do nothing; return success.
2173 if (new_active && (new_active == old_active)) {
2174 read_unlock(&bond->lock);
2175 return 0;
2178 if ((new_active) &&
2179 (old_active) &&
2180 (new_active->link == BOND_LINK_UP) &&
2181 IS_UP(new_active->dev)) {
2182 write_lock_bh(&bond->curr_slave_lock);
2183 bond_change_active_slave(bond, new_active);
2184 write_unlock_bh(&bond->curr_slave_lock);
2185 } else {
2186 res = -EINVAL;
2189 read_unlock(&bond->lock);
2191 return res;
2194 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2196 struct bonding *bond = netdev_priv(bond_dev);
2198 info->bond_mode = bond->params.mode;
2199 info->miimon = bond->params.miimon;
2201 read_lock(&bond->lock);
2202 info->num_slaves = bond->slave_cnt;
2203 read_unlock(&bond->lock);
2205 return 0;
2208 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2210 struct bonding *bond = netdev_priv(bond_dev);
2211 struct slave *slave;
2212 int i, found = 0;
2214 if (info->slave_id < 0) {
2215 return -ENODEV;
2218 read_lock(&bond->lock);
2220 bond_for_each_slave(bond, slave, i) {
2221 if (i == (int)info->slave_id) {
2222 found = 1;
2223 break;
2227 read_unlock(&bond->lock);
2229 if (found) {
2230 strcpy(info->slave_name, slave->dev->name);
2231 info->link = slave->link;
2232 info->state = slave->state;
2233 info->link_failure_count = slave->link_failure_count;
2234 } else {
2235 return -ENODEV;
2238 return 0;
2241 /*-------------------------------- Monitoring -------------------------------*/
2244 static int bond_miimon_inspect(struct bonding *bond)
2246 struct slave *slave;
2247 int i, link_state, commit = 0;
2249 bond_for_each_slave(bond, slave, i) {
2250 slave->new_link = BOND_LINK_NOCHANGE;
2252 link_state = bond_check_dev_link(bond, slave->dev, 0);
2254 switch (slave->link) {
2255 case BOND_LINK_UP:
2256 if (link_state)
2257 continue;
2259 slave->link = BOND_LINK_FAIL;
2260 slave->delay = bond->params.downdelay;
2261 if (slave->delay) {
2262 printk(KERN_INFO DRV_NAME
2263 ": %s: link status down for %s"
2264 "interface %s, disabling it in %d ms.\n",
2265 bond->dev->name,
2266 (bond->params.mode ==
2267 BOND_MODE_ACTIVEBACKUP) ?
2268 ((slave->state == BOND_STATE_ACTIVE) ?
2269 "active " : "backup ") : "",
2270 slave->dev->name,
2271 bond->params.downdelay * bond->params.miimon);
2273 /*FALLTHRU*/
2274 case BOND_LINK_FAIL:
2275 if (link_state) {
2277 * recovered before downdelay expired
2279 slave->link = BOND_LINK_UP;
2280 slave->jiffies = jiffies;
2281 printk(KERN_INFO DRV_NAME
2282 ": %s: link status up again after %d "
2283 "ms for interface %s.\n",
2284 bond->dev->name,
2285 (bond->params.downdelay - slave->delay) *
2286 bond->params.miimon,
2287 slave->dev->name);
2288 continue;
2291 if (slave->delay <= 0) {
2292 slave->new_link = BOND_LINK_DOWN;
2293 commit++;
2294 continue;
2297 slave->delay--;
2298 break;
2300 case BOND_LINK_DOWN:
2301 if (!link_state)
2302 continue;
2304 slave->link = BOND_LINK_BACK;
2305 slave->delay = bond->params.updelay;
2307 if (slave->delay) {
2308 printk(KERN_INFO DRV_NAME
2309 ": %s: link status up for "
2310 "interface %s, enabling it in %d ms.\n",
2311 bond->dev->name, slave->dev->name,
2312 bond->params.updelay *
2313 bond->params.miimon);
2315 /*FALLTHRU*/
2316 case BOND_LINK_BACK:
2317 if (!link_state) {
2318 slave->link = BOND_LINK_DOWN;
2319 printk(KERN_INFO DRV_NAME
2320 ": %s: link status down again after %d "
2321 "ms for interface %s.\n",
2322 bond->dev->name,
2323 (bond->params.updelay - slave->delay) *
2324 bond->params.miimon,
2325 slave->dev->name);
2327 continue;
2330 if (slave->delay <= 0) {
2331 slave->new_link = BOND_LINK_UP;
2332 commit++;
2333 continue;
2336 slave->delay--;
2337 break;
2341 return commit;
2344 static void bond_miimon_commit(struct bonding *bond)
2346 struct slave *slave;
2347 int i;
2349 bond_for_each_slave(bond, slave, i) {
2350 switch (slave->new_link) {
2351 case BOND_LINK_NOCHANGE:
2352 continue;
2354 case BOND_LINK_UP:
2355 slave->link = BOND_LINK_UP;
2356 slave->jiffies = jiffies;
2358 if (bond->params.mode == BOND_MODE_8023AD) {
2359 /* prevent it from being the active one */
2360 slave->state = BOND_STATE_BACKUP;
2361 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2362 /* make it immediately active */
2363 slave->state = BOND_STATE_ACTIVE;
2364 } else if (slave != bond->primary_slave) {
2365 /* prevent it from being the active one */
2366 slave->state = BOND_STATE_BACKUP;
2369 printk(KERN_INFO DRV_NAME
2370 ": %s: link status definitely "
2371 "up for interface %s.\n",
2372 bond->dev->name, slave->dev->name);
2374 /* notify ad that the link status has changed */
2375 if (bond->params.mode == BOND_MODE_8023AD)
2376 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2378 if (bond_is_lb(bond))
2379 bond_alb_handle_link_change(bond, slave,
2380 BOND_LINK_UP);
2382 if (!bond->curr_active_slave ||
2383 (slave == bond->primary_slave))
2384 goto do_failover;
2386 continue;
2388 case BOND_LINK_DOWN:
2389 if (slave->link_failure_count < UINT_MAX)
2390 slave->link_failure_count++;
2392 slave->link = BOND_LINK_DOWN;
2394 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2395 bond->params.mode == BOND_MODE_8023AD)
2396 bond_set_slave_inactive_flags(slave);
2398 printk(KERN_INFO DRV_NAME
2399 ": %s: link status definitely down for "
2400 "interface %s, disabling it\n",
2401 bond->dev->name, slave->dev->name);
2403 if (bond->params.mode == BOND_MODE_8023AD)
2404 bond_3ad_handle_link_change(slave,
2405 BOND_LINK_DOWN);
2407 if (bond->params.mode == BOND_MODE_TLB ||
2408 bond->params.mode == BOND_MODE_ALB)
2409 bond_alb_handle_link_change(bond, slave,
2410 BOND_LINK_DOWN);
2412 if (slave == bond->curr_active_slave)
2413 goto do_failover;
2415 continue;
2417 default:
2418 printk(KERN_ERR DRV_NAME
2419 ": %s: invalid new link %d on slave %s\n",
2420 bond->dev->name, slave->new_link,
2421 slave->dev->name);
2422 slave->new_link = BOND_LINK_NOCHANGE;
2424 continue;
2427 do_failover:
2428 ASSERT_RTNL();
2429 write_lock_bh(&bond->curr_slave_lock);
2430 bond_select_active_slave(bond);
2431 write_unlock_bh(&bond->curr_slave_lock);
2434 bond_set_carrier(bond);
2438 * bond_mii_monitor
2440 * Really a wrapper that splits the mii monitor into two phases: an
2441 * inspection, then (if inspection indicates something needs to be done)
2442 * an acquisition of appropriate locks followed by a commit phase to
2443 * implement whatever link state changes are indicated.
2445 void bond_mii_monitor(struct work_struct *work)
2447 struct bonding *bond = container_of(work, struct bonding,
2448 mii_work.work);
2450 read_lock(&bond->lock);
2451 if (bond->kill_timers)
2452 goto out;
2454 if (bond->slave_cnt == 0)
2455 goto re_arm;
2457 if (bond->send_grat_arp) {
2458 read_lock(&bond->curr_slave_lock);
2459 bond_send_gratuitous_arp(bond);
2460 read_unlock(&bond->curr_slave_lock);
2463 if (bond->send_unsol_na) {
2464 read_lock(&bond->curr_slave_lock);
2465 bond_send_unsolicited_na(bond);
2466 read_unlock(&bond->curr_slave_lock);
2469 if (bond_miimon_inspect(bond)) {
2470 read_unlock(&bond->lock);
2471 rtnl_lock();
2472 read_lock(&bond->lock);
2474 bond_miimon_commit(bond);
2476 read_unlock(&bond->lock);
2477 rtnl_unlock(); /* might sleep, hold no other locks */
2478 read_lock(&bond->lock);
2481 re_arm:
2482 if (bond->params.miimon)
2483 queue_delayed_work(bond->wq, &bond->mii_work,
2484 msecs_to_jiffies(bond->params.miimon));
2485 out:
2486 read_unlock(&bond->lock);
2489 static __be32 bond_glean_dev_ip(struct net_device *dev)
2491 struct in_device *idev;
2492 struct in_ifaddr *ifa;
2493 __be32 addr = 0;
2495 if (!dev)
2496 return 0;
2498 rcu_read_lock();
2499 idev = __in_dev_get_rcu(dev);
2500 if (!idev)
2501 goto out;
2503 ifa = idev->ifa_list;
2504 if (!ifa)
2505 goto out;
2507 addr = ifa->ifa_local;
2508 out:
2509 rcu_read_unlock();
2510 return addr;
2513 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2515 struct vlan_entry *vlan;
2517 if (ip == bond->master_ip)
2518 return 1;
2520 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2521 if (ip == vlan->vlan_ip)
2522 return 1;
2525 return 0;
2529 * We go to the (large) trouble of VLAN tagging ARP frames because
2530 * switches in VLAN mode (especially if ports are configured as
2531 * "native" to a VLAN) might not pass non-tagged frames.
2533 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2535 struct sk_buff *skb;
2537 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2538 slave_dev->name, dest_ip, src_ip, vlan_id);
2540 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2541 NULL, slave_dev->dev_addr, NULL);
2543 if (!skb) {
2544 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2545 return;
2547 if (vlan_id) {
2548 skb = vlan_put_tag(skb, vlan_id);
2549 if (!skb) {
2550 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2551 return;
2554 arp_xmit(skb);
2558 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2560 int i, vlan_id, rv;
2561 __be32 *targets = bond->params.arp_targets;
2562 struct vlan_entry *vlan;
2563 struct net_device *vlan_dev;
2564 struct flowi fl;
2565 struct rtable *rt;
2567 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2568 if (!targets[i])
2569 continue;
2570 pr_debug("basa: target %x\n", targets[i]);
2571 if (list_empty(&bond->vlan_list)) {
2572 pr_debug("basa: empty vlan: arp_send\n");
2573 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2574 bond->master_ip, 0);
2575 continue;
2579 * If VLANs are configured, we do a route lookup to
2580 * determine which VLAN interface would be used, so we
2581 * can tag the ARP with the proper VLAN tag.
2583 memset(&fl, 0, sizeof(fl));
2584 fl.fl4_dst = targets[i];
2585 fl.fl4_tos = RTO_ONLINK;
2587 rv = ip_route_output_key(&init_net, &rt, &fl);
2588 if (rv) {
2589 if (net_ratelimit()) {
2590 printk(KERN_WARNING DRV_NAME
2591 ": %s: no route to arp_ip_target %pI4\n",
2592 bond->dev->name, &fl.fl4_dst);
2594 continue;
2598 * This target is not on a VLAN
2600 if (rt->u.dst.dev == bond->dev) {
2601 ip_rt_put(rt);
2602 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2603 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2604 bond->master_ip, 0);
2605 continue;
2608 vlan_id = 0;
2609 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2610 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2611 if (vlan_dev == rt->u.dst.dev) {
2612 vlan_id = vlan->vlan_id;
2613 pr_debug("basa: vlan match on %s %d\n",
2614 vlan_dev->name, vlan_id);
2615 break;
2619 if (vlan_id) {
2620 ip_rt_put(rt);
2621 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2622 vlan->vlan_ip, vlan_id);
2623 continue;
2626 if (net_ratelimit()) {
2627 printk(KERN_WARNING DRV_NAME
2628 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2629 bond->dev->name, &fl.fl4_dst,
2630 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2632 ip_rt_put(rt);
2637 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2638 * for each VLAN above us.
2640 * Caller must hold curr_slave_lock for read or better
2642 static void bond_send_gratuitous_arp(struct bonding *bond)
2644 struct slave *slave = bond->curr_active_slave;
2645 struct vlan_entry *vlan;
2646 struct net_device *vlan_dev;
2648 pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2649 slave ? slave->dev->name : "NULL");
2651 if (!slave || !bond->send_grat_arp ||
2652 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
2653 return;
2655 bond->send_grat_arp--;
2657 if (bond->master_ip) {
2658 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2659 bond->master_ip, 0);
2662 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2663 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2664 if (vlan->vlan_ip) {
2665 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2666 vlan->vlan_ip, vlan->vlan_id);
2671 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2673 int i;
2674 __be32 *targets = bond->params.arp_targets;
2676 targets = bond->params.arp_targets;
2677 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2678 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2679 &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
2680 if (sip == targets[i]) {
2681 if (bond_has_this_ip(bond, tip))
2682 slave->last_arp_rx = jiffies;
2683 return;
2688 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2690 struct arphdr *arp;
2691 struct slave *slave;
2692 struct bonding *bond;
2693 unsigned char *arp_ptr;
2694 __be32 sip, tip;
2696 if (dev_net(dev) != &init_net)
2697 goto out;
2699 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2700 goto out;
2702 bond = netdev_priv(dev);
2703 read_lock(&bond->lock);
2705 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2706 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2707 orig_dev ? orig_dev->name : "NULL");
2709 slave = bond_get_slave_by_dev(bond, orig_dev);
2710 if (!slave || !slave_do_arp_validate(bond, slave))
2711 goto out_unlock;
2713 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2714 goto out_unlock;
2716 arp = arp_hdr(skb);
2717 if (arp->ar_hln != dev->addr_len ||
2718 skb->pkt_type == PACKET_OTHERHOST ||
2719 skb->pkt_type == PACKET_LOOPBACK ||
2720 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2721 arp->ar_pro != htons(ETH_P_IP) ||
2722 arp->ar_pln != 4)
2723 goto out_unlock;
2725 arp_ptr = (unsigned char *)(arp + 1);
2726 arp_ptr += dev->addr_len;
2727 memcpy(&sip, arp_ptr, 4);
2728 arp_ptr += 4 + dev->addr_len;
2729 memcpy(&tip, arp_ptr, 4);
2731 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2732 bond->dev->name, slave->dev->name, slave->state,
2733 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2734 &sip, &tip);
2737 * Backup slaves won't see the ARP reply, but do come through
2738 * here for each ARP probe (so we swap the sip/tip to validate
2739 * the probe). In a "redundant switch, common router" type of
2740 * configuration, the ARP probe will (hopefully) travel from
2741 * the active, through one switch, the router, then the other
2742 * switch before reaching the backup.
2744 if (slave->state == BOND_STATE_ACTIVE)
2745 bond_validate_arp(bond, slave, sip, tip);
2746 else
2747 bond_validate_arp(bond, slave, tip, sip);
2749 out_unlock:
2750 read_unlock(&bond->lock);
2751 out:
2752 dev_kfree_skb(skb);
2753 return NET_RX_SUCCESS;
2757 * this function is called regularly to monitor each slave's link
2758 * ensuring that traffic is being sent and received when arp monitoring
2759 * is used in load-balancing mode. if the adapter has been dormant, then an
2760 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2761 * arp monitoring in active backup mode.
2763 void bond_loadbalance_arp_mon(struct work_struct *work)
2765 struct bonding *bond = container_of(work, struct bonding,
2766 arp_work.work);
2767 struct slave *slave, *oldcurrent;
2768 int do_failover = 0;
2769 int delta_in_ticks;
2770 int i;
2772 read_lock(&bond->lock);
2774 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2776 if (bond->kill_timers) {
2777 goto out;
2780 if (bond->slave_cnt == 0) {
2781 goto re_arm;
2784 read_lock(&bond->curr_slave_lock);
2785 oldcurrent = bond->curr_active_slave;
2786 read_unlock(&bond->curr_slave_lock);
2788 /* see if any of the previous devices are up now (i.e. they have
2789 * xmt and rcv traffic). the curr_active_slave does not come into
2790 * the picture unless it is null. also, slave->jiffies is not needed
2791 * here because we send an arp on each slave and give a slave as
2792 * long as it needs to get the tx/rx within the delta.
2793 * TODO: what about up/down delay in arp mode? it wasn't here before
2794 * so it can wait
2796 bond_for_each_slave(bond, slave, i) {
2797 if (slave->link != BOND_LINK_UP) {
2798 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2799 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
2801 slave->link = BOND_LINK_UP;
2802 slave->state = BOND_STATE_ACTIVE;
2804 /* primary_slave has no meaning in round-robin
2805 * mode. the window of a slave being up and
2806 * curr_active_slave being null after enslaving
2807 * is closed.
2809 if (!oldcurrent) {
2810 printk(KERN_INFO DRV_NAME
2811 ": %s: link status definitely "
2812 "up for interface %s, ",
2813 bond->dev->name,
2814 slave->dev->name);
2815 do_failover = 1;
2816 } else {
2817 printk(KERN_INFO DRV_NAME
2818 ": %s: interface %s is now up\n",
2819 bond->dev->name,
2820 slave->dev->name);
2823 } else {
2824 /* slave->link == BOND_LINK_UP */
2826 /* not all switches will respond to an arp request
2827 * when the source ip is 0, so don't take the link down
2828 * if we don't know our ip yet
2830 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2831 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
2833 slave->link = BOND_LINK_DOWN;
2834 slave->state = BOND_STATE_BACKUP;
2836 if (slave->link_failure_count < UINT_MAX) {
2837 slave->link_failure_count++;
2840 printk(KERN_INFO DRV_NAME
2841 ": %s: interface %s is now down.\n",
2842 bond->dev->name,
2843 slave->dev->name);
2845 if (slave == oldcurrent) {
2846 do_failover = 1;
2851 /* note: if switch is in round-robin mode, all links
2852 * must tx arp to ensure all links rx an arp - otherwise
2853 * links may oscillate or not come up at all; if switch is
2854 * in something like xor mode, there is nothing we can
2855 * do - all replies will be rx'ed on same link causing slaves
2856 * to be unstable during low/no traffic periods
2858 if (IS_UP(slave->dev)) {
2859 bond_arp_send_all(bond, slave);
2863 if (do_failover) {
2864 write_lock_bh(&bond->curr_slave_lock);
2866 bond_select_active_slave(bond);
2868 write_unlock_bh(&bond->curr_slave_lock);
2871 re_arm:
2872 if (bond->params.arp_interval)
2873 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2874 out:
2875 read_unlock(&bond->lock);
2879 * Called to inspect slaves for active-backup mode ARP monitor link state
2880 * changes. Sets new_link in slaves to specify what action should take
2881 * place for the slave. Returns 0 if no changes are found, >0 if changes
2882 * to link states must be committed.
2884 * Called with bond->lock held for read.
2886 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
2888 struct slave *slave;
2889 int i, commit = 0;
2891 bond_for_each_slave(bond, slave, i) {
2892 slave->new_link = BOND_LINK_NOCHANGE;
2894 if (slave->link != BOND_LINK_UP) {
2895 if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2896 delta_in_ticks)) {
2897 slave->new_link = BOND_LINK_UP;
2898 commit++;
2901 continue;
2905 * Give slaves 2*delta after being enslaved or made
2906 * active. This avoids bouncing, as the last receive
2907 * times need a full ARP monitor cycle to be updated.
2909 if (!time_after_eq(jiffies, slave->jiffies +
2910 2 * delta_in_ticks))
2911 continue;
2914 * Backup slave is down if:
2915 * - No current_arp_slave AND
2916 * - more than 3*delta since last receive AND
2917 * - the bond has an IP address
2919 * Note: a non-null current_arp_slave indicates
2920 * the curr_active_slave went down and we are
2921 * searching for a new one; under this condition
2922 * we only take the curr_active_slave down - this
2923 * gives each slave a chance to tx/rx traffic
2924 * before being taken out
2926 if (slave->state == BOND_STATE_BACKUP &&
2927 !bond->current_arp_slave &&
2928 time_after(jiffies, slave_last_rx(bond, slave) +
2929 3 * delta_in_ticks)) {
2930 slave->new_link = BOND_LINK_DOWN;
2931 commit++;
2935 * Active slave is down if:
2936 * - more than 2*delta since transmitting OR
2937 * - (more than 2*delta since receive AND
2938 * the bond has an IP address)
2940 if ((slave->state == BOND_STATE_ACTIVE) &&
2941 (time_after_eq(jiffies, slave->dev->trans_start +
2942 2 * delta_in_ticks) ||
2943 (time_after_eq(jiffies, slave_last_rx(bond, slave)
2944 + 2 * delta_in_ticks)))) {
2945 slave->new_link = BOND_LINK_DOWN;
2946 commit++;
2950 read_lock(&bond->curr_slave_lock);
2953 * Trigger a commit if the primary option setting has changed.
2955 if (bond->primary_slave &&
2956 (bond->primary_slave != bond->curr_active_slave) &&
2957 (bond->primary_slave->link == BOND_LINK_UP))
2958 commit++;
2960 read_unlock(&bond->curr_slave_lock);
2962 return commit;
2966 * Called to commit link state changes noted by inspection step of
2967 * active-backup mode ARP monitor.
2969 * Called with RTNL and bond->lock for read.
2971 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2973 struct slave *slave;
2974 int i;
2976 bond_for_each_slave(bond, slave, i) {
2977 switch (slave->new_link) {
2978 case BOND_LINK_NOCHANGE:
2979 continue;
2981 case BOND_LINK_UP:
2982 write_lock_bh(&bond->curr_slave_lock);
2984 if (!bond->curr_active_slave &&
2985 time_before_eq(jiffies, slave->dev->trans_start +
2986 delta_in_ticks)) {
2987 slave->link = BOND_LINK_UP;
2988 bond_change_active_slave(bond, slave);
2989 bond->current_arp_slave = NULL;
2991 printk(KERN_INFO DRV_NAME
2992 ": %s: %s is up and now the "
2993 "active interface\n",
2994 bond->dev->name, slave->dev->name);
2996 } else if (bond->curr_active_slave != slave) {
2997 /* this slave has just come up but we
2998 * already have a current slave; this can
2999 * also happen if bond_enslave adds a new
3000 * slave that is up while we are searching
3001 * for a new slave
3003 slave->link = BOND_LINK_UP;
3004 bond_set_slave_inactive_flags(slave);
3005 bond->current_arp_slave = NULL;
3007 printk(KERN_INFO DRV_NAME
3008 ": %s: backup interface %s is now up\n",
3009 bond->dev->name, slave->dev->name);
3012 write_unlock_bh(&bond->curr_slave_lock);
3014 break;
3016 case BOND_LINK_DOWN:
3017 if (slave->link_failure_count < UINT_MAX)
3018 slave->link_failure_count++;
3020 slave->link = BOND_LINK_DOWN;
3022 if (slave == bond->curr_active_slave) {
3023 printk(KERN_INFO DRV_NAME
3024 ": %s: link status down for active "
3025 "interface %s, disabling it\n",
3026 bond->dev->name, slave->dev->name);
3028 bond_set_slave_inactive_flags(slave);
3030 write_lock_bh(&bond->curr_slave_lock);
3032 bond_select_active_slave(bond);
3033 if (bond->curr_active_slave)
3034 bond->curr_active_slave->jiffies =
3035 jiffies;
3037 write_unlock_bh(&bond->curr_slave_lock);
3039 bond->current_arp_slave = NULL;
3041 } else if (slave->state == BOND_STATE_BACKUP) {
3042 printk(KERN_INFO DRV_NAME
3043 ": %s: backup interface %s is now down\n",
3044 bond->dev->name, slave->dev->name);
3046 bond_set_slave_inactive_flags(slave);
3048 break;
3050 default:
3051 printk(KERN_ERR DRV_NAME
3052 ": %s: impossible: new_link %d on slave %s\n",
3053 bond->dev->name, slave->new_link,
3054 slave->dev->name);
3059 * No race with changes to primary via sysfs, as we hold rtnl.
3061 if (bond->primary_slave &&
3062 (bond->primary_slave != bond->curr_active_slave) &&
3063 (bond->primary_slave->link == BOND_LINK_UP)) {
3064 write_lock_bh(&bond->curr_slave_lock);
3065 bond_change_active_slave(bond, bond->primary_slave);
3066 write_unlock_bh(&bond->curr_slave_lock);
3069 bond_set_carrier(bond);
3073 * Send ARP probes for active-backup mode ARP monitor.
3075 * Called with bond->lock held for read.
3077 static void bond_ab_arp_probe(struct bonding *bond)
3079 struct slave *slave;
3080 int i;
3082 read_lock(&bond->curr_slave_lock);
3084 if (bond->current_arp_slave && bond->curr_active_slave)
3085 printk("PROBE: c_arp %s && cas %s BAD\n",
3086 bond->current_arp_slave->dev->name,
3087 bond->curr_active_slave->dev->name);
3089 if (bond->curr_active_slave) {
3090 bond_arp_send_all(bond, bond->curr_active_slave);
3091 read_unlock(&bond->curr_slave_lock);
3092 return;
3095 read_unlock(&bond->curr_slave_lock);
3097 /* if we don't have a curr_active_slave, search for the next available
3098 * backup slave from the current_arp_slave and make it the candidate
3099 * for becoming the curr_active_slave
3102 if (!bond->current_arp_slave) {
3103 bond->current_arp_slave = bond->first_slave;
3104 if (!bond->current_arp_slave)
3105 return;
3108 bond_set_slave_inactive_flags(bond->current_arp_slave);
3110 /* search for next candidate */
3111 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3112 if (IS_UP(slave->dev)) {
3113 slave->link = BOND_LINK_BACK;
3114 bond_set_slave_active_flags(slave);
3115 bond_arp_send_all(bond, slave);
3116 slave->jiffies = jiffies;
3117 bond->current_arp_slave = slave;
3118 break;
3121 /* if the link state is up at this point, we
3122 * mark it down - this can happen if we have
3123 * simultaneous link failures and
3124 * reselect_active_interface doesn't make this
3125 * one the current slave so it is still marked
3126 * up when it is actually down
3128 if (slave->link == BOND_LINK_UP) {
3129 slave->link = BOND_LINK_DOWN;
3130 if (slave->link_failure_count < UINT_MAX)
3131 slave->link_failure_count++;
3133 bond_set_slave_inactive_flags(slave);
3135 printk(KERN_INFO DRV_NAME
3136 ": %s: backup interface %s is now down.\n",
3137 bond->dev->name, slave->dev->name);
3142 void bond_activebackup_arp_mon(struct work_struct *work)
3144 struct bonding *bond = container_of(work, struct bonding,
3145 arp_work.work);
3146 int delta_in_ticks;
3148 read_lock(&bond->lock);
3150 if (bond->kill_timers)
3151 goto out;
3153 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3155 if (bond->slave_cnt == 0)
3156 goto re_arm;
3158 if (bond->send_grat_arp) {
3159 read_lock(&bond->curr_slave_lock);
3160 bond_send_gratuitous_arp(bond);
3161 read_unlock(&bond->curr_slave_lock);
3164 if (bond->send_unsol_na) {
3165 read_lock(&bond->curr_slave_lock);
3166 bond_send_unsolicited_na(bond);
3167 read_unlock(&bond->curr_slave_lock);
3170 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3171 read_unlock(&bond->lock);
3172 rtnl_lock();
3173 read_lock(&bond->lock);
3175 bond_ab_arp_commit(bond, delta_in_ticks);
3177 read_unlock(&bond->lock);
3178 rtnl_unlock();
3179 read_lock(&bond->lock);
3182 bond_ab_arp_probe(bond);
3184 re_arm:
3185 if (bond->params.arp_interval) {
3186 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3188 out:
3189 read_unlock(&bond->lock);
3192 /*------------------------------ proc/seq_file-------------------------------*/
3194 #ifdef CONFIG_PROC_FS
3196 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3198 struct bonding *bond = seq->private;
3199 loff_t off = 0;
3200 struct slave *slave;
3201 int i;
3203 /* make sure the bond won't be taken away */
3204 read_lock(&dev_base_lock);
3205 read_lock(&bond->lock);
3207 if (*pos == 0) {
3208 return SEQ_START_TOKEN;
3211 bond_for_each_slave(bond, slave, i) {
3212 if (++off == *pos) {
3213 return slave;
3217 return NULL;
3220 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3222 struct bonding *bond = seq->private;
3223 struct slave *slave = v;
3225 ++*pos;
3226 if (v == SEQ_START_TOKEN) {
3227 return bond->first_slave;
3230 slave = slave->next;
3232 return (slave == bond->first_slave) ? NULL : slave;
3235 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3237 struct bonding *bond = seq->private;
3239 read_unlock(&bond->lock);
3240 read_unlock(&dev_base_lock);
3243 static void bond_info_show_master(struct seq_file *seq)
3245 struct bonding *bond = seq->private;
3246 struct slave *curr;
3247 int i;
3249 read_lock(&bond->curr_slave_lock);
3250 curr = bond->curr_active_slave;
3251 read_unlock(&bond->curr_slave_lock);
3253 seq_printf(seq, "Bonding Mode: %s",
3254 bond_mode_name(bond->params.mode));
3256 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3257 bond->params.fail_over_mac)
3258 seq_printf(seq, " (fail_over_mac %s)",
3259 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
3261 seq_printf(seq, "\n");
3263 if (bond->params.mode == BOND_MODE_XOR ||
3264 bond->params.mode == BOND_MODE_8023AD) {
3265 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3266 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3267 bond->params.xmit_policy);
3270 if (USES_PRIMARY(bond->params.mode)) {
3271 seq_printf(seq, "Primary Slave: %s\n",
3272 (bond->primary_slave) ?
3273 bond->primary_slave->dev->name : "None");
3275 seq_printf(seq, "Currently Active Slave: %s\n",
3276 (curr) ? curr->dev->name : "None");
3279 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3280 "up" : "down");
3281 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3282 seq_printf(seq, "Up Delay (ms): %d\n",
3283 bond->params.updelay * bond->params.miimon);
3284 seq_printf(seq, "Down Delay (ms): %d\n",
3285 bond->params.downdelay * bond->params.miimon);
3288 /* ARP information */
3289 if(bond->params.arp_interval > 0) {
3290 int printed=0;
3291 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3292 bond->params.arp_interval);
3294 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3296 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3297 if (!bond->params.arp_targets[i])
3298 continue;
3299 if (printed)
3300 seq_printf(seq, ",");
3301 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
3302 printed = 1;
3304 seq_printf(seq, "\n");
3307 if (bond->params.mode == BOND_MODE_8023AD) {
3308 struct ad_info ad_info;
3310 seq_puts(seq, "\n802.3ad info\n");
3311 seq_printf(seq, "LACP rate: %s\n",
3312 (bond->params.lacp_fast) ? "fast" : "slow");
3313 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3314 ad_select_tbl[bond->params.ad_select].modename);
3316 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3317 seq_printf(seq, "bond %s has no active aggregator\n",
3318 bond->dev->name);
3319 } else {
3320 seq_printf(seq, "Active Aggregator Info:\n");
3322 seq_printf(seq, "\tAggregator ID: %d\n",
3323 ad_info.aggregator_id);
3324 seq_printf(seq, "\tNumber of ports: %d\n",
3325 ad_info.ports);
3326 seq_printf(seq, "\tActor Key: %d\n",
3327 ad_info.actor_key);
3328 seq_printf(seq, "\tPartner Key: %d\n",
3329 ad_info.partner_key);
3330 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3331 ad_info.partner_system);
3336 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3338 struct bonding *bond = seq->private;
3340 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3341 seq_printf(seq, "MII Status: %s\n",
3342 (slave->link == BOND_LINK_UP) ? "up" : "down");
3343 seq_printf(seq, "Link Failure Count: %u\n",
3344 slave->link_failure_count);
3346 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
3348 if (bond->params.mode == BOND_MODE_8023AD) {
3349 const struct aggregator *agg
3350 = SLAVE_AD_INFO(slave).port.aggregator;
3352 if (agg) {
3353 seq_printf(seq, "Aggregator ID: %d\n",
3354 agg->aggregator_identifier);
3355 } else {
3356 seq_puts(seq, "Aggregator ID: N/A\n");
3361 static int bond_info_seq_show(struct seq_file *seq, void *v)
3363 if (v == SEQ_START_TOKEN) {
3364 seq_printf(seq, "%s\n", version);
3365 bond_info_show_master(seq);
3366 } else {
3367 bond_info_show_slave(seq, v);
3370 return 0;
3373 static struct seq_operations bond_info_seq_ops = {
3374 .start = bond_info_seq_start,
3375 .next = bond_info_seq_next,
3376 .stop = bond_info_seq_stop,
3377 .show = bond_info_seq_show,
3380 static int bond_info_open(struct inode *inode, struct file *file)
3382 struct seq_file *seq;
3383 struct proc_dir_entry *proc;
3384 int res;
3386 res = seq_open(file, &bond_info_seq_ops);
3387 if (!res) {
3388 /* recover the pointer buried in proc_dir_entry data */
3389 seq = file->private_data;
3390 proc = PDE(inode);
3391 seq->private = proc->data;
3394 return res;
3397 static const struct file_operations bond_info_fops = {
3398 .owner = THIS_MODULE,
3399 .open = bond_info_open,
3400 .read = seq_read,
3401 .llseek = seq_lseek,
3402 .release = seq_release,
3405 static int bond_create_proc_entry(struct bonding *bond)
3407 struct net_device *bond_dev = bond->dev;
3409 if (bond_proc_dir) {
3410 bond->proc_entry = proc_create_data(bond_dev->name,
3411 S_IRUGO, bond_proc_dir,
3412 &bond_info_fops, bond);
3413 if (bond->proc_entry == NULL) {
3414 printk(KERN_WARNING DRV_NAME
3415 ": Warning: Cannot create /proc/net/%s/%s\n",
3416 DRV_NAME, bond_dev->name);
3417 } else {
3418 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3422 return 0;
3425 static void bond_remove_proc_entry(struct bonding *bond)
3427 if (bond_proc_dir && bond->proc_entry) {
3428 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3429 memset(bond->proc_file_name, 0, IFNAMSIZ);
3430 bond->proc_entry = NULL;
3434 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3435 * Caller must hold rtnl_lock.
3437 static void bond_create_proc_dir(void)
3439 int len = strlen(DRV_NAME);
3441 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
3442 bond_proc_dir = bond_proc_dir->next) {
3443 if ((bond_proc_dir->namelen == len) &&
3444 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3445 break;
3449 if (!bond_proc_dir) {
3450 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
3451 if (bond_proc_dir) {
3452 bond_proc_dir->owner = THIS_MODULE;
3453 } else {
3454 printk(KERN_WARNING DRV_NAME
3455 ": Warning: cannot create /proc/net/%s\n",
3456 DRV_NAME);
3461 /* Destroy the bonding directory under /proc/net, if empty.
3462 * Caller must hold rtnl_lock.
3464 static void bond_destroy_proc_dir(void)
3466 struct proc_dir_entry *de;
3468 if (!bond_proc_dir) {
3469 return;
3472 /* verify that the /proc dir is empty */
3473 for (de = bond_proc_dir->subdir; de; de = de->next) {
3474 /* ignore . and .. */
3475 if (*(de->name) != '.') {
3476 break;
3480 if (de) {
3481 if (bond_proc_dir->owner == THIS_MODULE) {
3482 bond_proc_dir->owner = NULL;
3484 } else {
3485 remove_proc_entry(DRV_NAME, init_net.proc_net);
3486 bond_proc_dir = NULL;
3489 #endif /* CONFIG_PROC_FS */
3491 /*-------------------------- netdev event handling --------------------------*/
3494 * Change device name
3496 static int bond_event_changename(struct bonding *bond)
3498 #ifdef CONFIG_PROC_FS
3499 bond_remove_proc_entry(bond);
3500 bond_create_proc_entry(bond);
3501 #endif
3502 down_write(&(bonding_rwsem));
3503 bond_destroy_sysfs_entry(bond);
3504 bond_create_sysfs_entry(bond);
3505 up_write(&(bonding_rwsem));
3506 return NOTIFY_DONE;
3509 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3511 struct bonding *event_bond = netdev_priv(bond_dev);
3513 switch (event) {
3514 case NETDEV_CHANGENAME:
3515 return bond_event_changename(event_bond);
3516 case NETDEV_UNREGISTER:
3517 bond_release_all(event_bond->dev);
3518 break;
3519 default:
3520 break;
3523 return NOTIFY_DONE;
3526 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3528 struct net_device *bond_dev = slave_dev->master;
3529 struct bonding *bond = netdev_priv(bond_dev);
3531 switch (event) {
3532 case NETDEV_UNREGISTER:
3533 if (bond_dev) {
3534 if (bond->setup_by_slave)
3535 bond_release_and_destroy(bond_dev, slave_dev);
3536 else
3537 bond_release(bond_dev, slave_dev);
3539 break;
3540 case NETDEV_CHANGE:
3542 * TODO: is this what we get if somebody
3543 * sets up a hierarchical bond, then rmmod's
3544 * one of the slave bonding devices?
3546 break;
3547 case NETDEV_DOWN:
3549 * ... Or is it this?
3551 break;
3552 case NETDEV_CHANGEMTU:
3554 * TODO: Should slaves be allowed to
3555 * independently alter their MTU? For
3556 * an active-backup bond, slaves need
3557 * not be the same type of device, so
3558 * MTUs may vary. For other modes,
3559 * slaves arguably should have the
3560 * same MTUs. To do this, we'd need to
3561 * take over the slave's change_mtu
3562 * function for the duration of their
3563 * servitude.
3565 break;
3566 case NETDEV_CHANGENAME:
3568 * TODO: handle changing the primary's name
3570 break;
3571 case NETDEV_FEAT_CHANGE:
3572 bond_compute_features(bond);
3573 break;
3574 default:
3575 break;
3578 return NOTIFY_DONE;
3582 * bond_netdev_event: handle netdev notifier chain events.
3584 * This function receives events for the netdev chain. The caller (an
3585 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3586 * locks for us to safely manipulate the slave devices (RTNL lock,
3587 * dev_probe_lock).
3589 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3591 struct net_device *event_dev = (struct net_device *)ptr;
3593 if (dev_net(event_dev) != &init_net)
3594 return NOTIFY_DONE;
3596 pr_debug("event_dev: %s, event: %lx\n",
3597 (event_dev ? event_dev->name : "None"),
3598 event);
3600 if (!(event_dev->priv_flags & IFF_BONDING))
3601 return NOTIFY_DONE;
3603 if (event_dev->flags & IFF_MASTER) {
3604 pr_debug("IFF_MASTER\n");
3605 return bond_master_netdev_event(event, event_dev);
3608 if (event_dev->flags & IFF_SLAVE) {
3609 pr_debug("IFF_SLAVE\n");
3610 return bond_slave_netdev_event(event, event_dev);
3613 return NOTIFY_DONE;
3617 * bond_inetaddr_event: handle inetaddr notifier chain events.
3619 * We keep track of device IPs primarily to use as source addresses in
3620 * ARP monitor probes (rather than spewing out broadcasts all the time).
3622 * We track one IP for the main device (if it has one), plus one per VLAN.
3624 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3626 struct in_ifaddr *ifa = ptr;
3627 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3628 struct bonding *bond;
3629 struct vlan_entry *vlan;
3631 if (dev_net(ifa->ifa_dev->dev) != &init_net)
3632 return NOTIFY_DONE;
3634 list_for_each_entry(bond, &bond_dev_list, bond_list) {
3635 if (bond->dev == event_dev) {
3636 switch (event) {
3637 case NETDEV_UP:
3638 bond->master_ip = ifa->ifa_local;
3639 return NOTIFY_OK;
3640 case NETDEV_DOWN:
3641 bond->master_ip = bond_glean_dev_ip(bond->dev);
3642 return NOTIFY_OK;
3643 default:
3644 return NOTIFY_DONE;
3648 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3649 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3650 if (vlan_dev == event_dev) {
3651 switch (event) {
3652 case NETDEV_UP:
3653 vlan->vlan_ip = ifa->ifa_local;
3654 return NOTIFY_OK;
3655 case NETDEV_DOWN:
3656 vlan->vlan_ip =
3657 bond_glean_dev_ip(vlan_dev);
3658 return NOTIFY_OK;
3659 default:
3660 return NOTIFY_DONE;
3665 return NOTIFY_DONE;
3668 static struct notifier_block bond_netdev_notifier = {
3669 .notifier_call = bond_netdev_event,
3672 static struct notifier_block bond_inetaddr_notifier = {
3673 .notifier_call = bond_inetaddr_event,
3676 /*-------------------------- Packet type handling ---------------------------*/
3678 /* register to receive lacpdus on a bond */
3679 static void bond_register_lacpdu(struct bonding *bond)
3681 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3683 /* initialize packet type */
3684 pk_type->type = PKT_TYPE_LACPDU;
3685 pk_type->dev = bond->dev;
3686 pk_type->func = bond_3ad_lacpdu_recv;
3688 dev_add_pack(pk_type);
3691 /* unregister to receive lacpdus on a bond */
3692 static void bond_unregister_lacpdu(struct bonding *bond)
3694 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3697 void bond_register_arp(struct bonding *bond)
3699 struct packet_type *pt = &bond->arp_mon_pt;
3701 if (pt->type)
3702 return;
3704 pt->type = htons(ETH_P_ARP);
3705 pt->dev = bond->dev;
3706 pt->func = bond_arp_rcv;
3707 dev_add_pack(pt);
3710 void bond_unregister_arp(struct bonding *bond)
3712 struct packet_type *pt = &bond->arp_mon_pt;
3714 dev_remove_pack(pt);
3715 pt->type = 0;
3718 /*---------------------------- Hashing Policies -----------------------------*/
3721 * Hash for the output device based upon layer 2 and layer 3 data. If
3722 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3724 static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3725 struct net_device *bond_dev, int count)
3727 struct ethhdr *data = (struct ethhdr *)skb->data;
3728 struct iphdr *iph = ip_hdr(skb);
3730 if (skb->protocol == htons(ETH_P_IP)) {
3731 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3732 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3735 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3739 * Hash for the output device based upon layer 3 and layer 4 data. If
3740 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3741 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3743 static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3744 struct net_device *bond_dev, int count)
3746 struct ethhdr *data = (struct ethhdr *)skb->data;
3747 struct iphdr *iph = ip_hdr(skb);
3748 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3749 int layer4_xor = 0;
3751 if (skb->protocol == htons(ETH_P_IP)) {
3752 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
3753 (iph->protocol == IPPROTO_TCP ||
3754 iph->protocol == IPPROTO_UDP)) {
3755 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3757 return (layer4_xor ^
3758 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3762 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3766 * Hash for the output device based upon layer 2 data
3768 static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3769 struct net_device *bond_dev, int count)
3771 struct ethhdr *data = (struct ethhdr *)skb->data;
3773 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3776 /*-------------------------- Device entry points ----------------------------*/
3778 static int bond_open(struct net_device *bond_dev)
3780 struct bonding *bond = netdev_priv(bond_dev);
3782 bond->kill_timers = 0;
3784 if (bond_is_lb(bond)) {
3785 /* bond_alb_initialize must be called before the timer
3786 * is started.
3788 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3789 /* something went wrong - fail the open operation */
3790 return -1;
3793 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3794 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3797 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3798 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3799 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3802 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3803 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3804 INIT_DELAYED_WORK(&bond->arp_work,
3805 bond_activebackup_arp_mon);
3806 else
3807 INIT_DELAYED_WORK(&bond->arp_work,
3808 bond_loadbalance_arp_mon);
3810 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3811 if (bond->params.arp_validate)
3812 bond_register_arp(bond);
3815 if (bond->params.mode == BOND_MODE_8023AD) {
3816 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3817 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3818 /* register to receive LACPDUs */
3819 bond_register_lacpdu(bond);
3820 bond_3ad_initiate_agg_selection(bond, 1);
3823 return 0;
3826 static int bond_close(struct net_device *bond_dev)
3828 struct bonding *bond = netdev_priv(bond_dev);
3830 if (bond->params.mode == BOND_MODE_8023AD) {
3831 /* Unregister the receive of LACPDUs */
3832 bond_unregister_lacpdu(bond);
3835 if (bond->params.arp_validate)
3836 bond_unregister_arp(bond);
3838 write_lock_bh(&bond->lock);
3840 bond->send_grat_arp = 0;
3841 bond->send_unsol_na = 0;
3843 /* signal timers not to re-arm */
3844 bond->kill_timers = 1;
3846 write_unlock_bh(&bond->lock);
3848 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3849 cancel_delayed_work(&bond->mii_work);
3852 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3853 cancel_delayed_work(&bond->arp_work);
3856 switch (bond->params.mode) {
3857 case BOND_MODE_8023AD:
3858 cancel_delayed_work(&bond->ad_work);
3859 break;
3860 case BOND_MODE_TLB:
3861 case BOND_MODE_ALB:
3862 cancel_delayed_work(&bond->alb_work);
3863 break;
3864 default:
3865 break;
3869 if (bond_is_lb(bond)) {
3870 /* Must be called only after all
3871 * slaves have been released
3873 bond_alb_deinitialize(bond);
3876 return 0;
3879 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3881 struct bonding *bond = netdev_priv(bond_dev);
3882 struct net_device_stats *stats = &bond->stats;
3883 struct net_device_stats local_stats;
3884 struct slave *slave;
3885 int i;
3887 memset(&local_stats, 0, sizeof(struct net_device_stats));
3889 read_lock_bh(&bond->lock);
3891 bond_for_each_slave(bond, slave, i) {
3892 const struct net_device_stats *sstats = dev_get_stats(slave->dev);
3894 local_stats.rx_packets += sstats->rx_packets;
3895 local_stats.rx_bytes += sstats->rx_bytes;
3896 local_stats.rx_errors += sstats->rx_errors;
3897 local_stats.rx_dropped += sstats->rx_dropped;
3899 local_stats.tx_packets += sstats->tx_packets;
3900 local_stats.tx_bytes += sstats->tx_bytes;
3901 local_stats.tx_errors += sstats->tx_errors;
3902 local_stats.tx_dropped += sstats->tx_dropped;
3904 local_stats.multicast += sstats->multicast;
3905 local_stats.collisions += sstats->collisions;
3907 local_stats.rx_length_errors += sstats->rx_length_errors;
3908 local_stats.rx_over_errors += sstats->rx_over_errors;
3909 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3910 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3911 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3912 local_stats.rx_missed_errors += sstats->rx_missed_errors;
3914 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3915 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3916 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3917 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3918 local_stats.tx_window_errors += sstats->tx_window_errors;
3921 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3923 read_unlock_bh(&bond->lock);
3925 return stats;
3928 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3930 struct net_device *slave_dev = NULL;
3931 struct ifbond k_binfo;
3932 struct ifbond __user *u_binfo = NULL;
3933 struct ifslave k_sinfo;
3934 struct ifslave __user *u_sinfo = NULL;
3935 struct mii_ioctl_data *mii = NULL;
3936 int res = 0;
3938 pr_debug("bond_ioctl: master=%s, cmd=%d\n",
3939 bond_dev->name, cmd);
3941 switch (cmd) {
3942 case SIOCGMIIPHY:
3943 mii = if_mii(ifr);
3944 if (!mii) {
3945 return -EINVAL;
3947 mii->phy_id = 0;
3948 /* Fall Through */
3949 case SIOCGMIIREG:
3951 * We do this again just in case we were called by SIOCGMIIREG
3952 * instead of SIOCGMIIPHY.
3954 mii = if_mii(ifr);
3955 if (!mii) {
3956 return -EINVAL;
3959 if (mii->reg_num == 1) {
3960 struct bonding *bond = netdev_priv(bond_dev);
3961 mii->val_out = 0;
3962 read_lock(&bond->lock);
3963 read_lock(&bond->curr_slave_lock);
3964 if (netif_carrier_ok(bond->dev)) {
3965 mii->val_out = BMSR_LSTATUS;
3967 read_unlock(&bond->curr_slave_lock);
3968 read_unlock(&bond->lock);
3971 return 0;
3972 case BOND_INFO_QUERY_OLD:
3973 case SIOCBONDINFOQUERY:
3974 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3976 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3977 return -EFAULT;
3980 res = bond_info_query(bond_dev, &k_binfo);
3981 if (res == 0) {
3982 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3983 return -EFAULT;
3987 return res;
3988 case BOND_SLAVE_INFO_QUERY_OLD:
3989 case SIOCBONDSLAVEINFOQUERY:
3990 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3992 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3993 return -EFAULT;
3996 res = bond_slave_info_query(bond_dev, &k_sinfo);
3997 if (res == 0) {
3998 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3999 return -EFAULT;
4003 return res;
4004 default:
4005 /* Go on */
4006 break;
4009 if (!capable(CAP_NET_ADMIN)) {
4010 return -EPERM;
4013 down_write(&(bonding_rwsem));
4014 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
4016 pr_debug("slave_dev=%p: \n", slave_dev);
4018 if (!slave_dev) {
4019 res = -ENODEV;
4020 } else {
4021 pr_debug("slave_dev->name=%s: \n", slave_dev->name);
4022 switch (cmd) {
4023 case BOND_ENSLAVE_OLD:
4024 case SIOCBONDENSLAVE:
4025 res = bond_enslave(bond_dev, slave_dev);
4026 break;
4027 case BOND_RELEASE_OLD:
4028 case SIOCBONDRELEASE:
4029 res = bond_release(bond_dev, slave_dev);
4030 break;
4031 case BOND_SETHWADDR_OLD:
4032 case SIOCBONDSETHWADDR:
4033 res = bond_sethwaddr(bond_dev, slave_dev);
4034 break;
4035 case BOND_CHANGE_ACTIVE_OLD:
4036 case SIOCBONDCHANGEACTIVE:
4037 res = bond_ioctl_change_active(bond_dev, slave_dev);
4038 break;
4039 default:
4040 res = -EOPNOTSUPP;
4043 dev_put(slave_dev);
4046 up_write(&(bonding_rwsem));
4047 return res;
4050 static void bond_set_multicast_list(struct net_device *bond_dev)
4052 struct bonding *bond = netdev_priv(bond_dev);
4053 struct dev_mc_list *dmi;
4056 * Do promisc before checking multicast_mode
4058 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
4060 * FIXME: Need to handle the error when one of the multi-slaves
4061 * encounters error.
4063 bond_set_promiscuity(bond, 1);
4066 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
4067 bond_set_promiscuity(bond, -1);
4070 /* set allmulti flag to slaves */
4071 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
4073 * FIXME: Need to handle the error when one of the multi-slaves
4074 * encounters error.
4076 bond_set_allmulti(bond, 1);
4079 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
4080 bond_set_allmulti(bond, -1);
4083 read_lock(&bond->lock);
4085 bond->flags = bond_dev->flags;
4087 /* looking for addresses to add to slaves' mc list */
4088 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
4089 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
4090 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4094 /* looking for addresses to delete from slaves' list */
4095 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
4096 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
4097 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4101 /* save master's multicast list */
4102 bond_mc_list_destroy(bond);
4103 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4105 read_unlock(&bond->lock);
4108 static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4110 struct bonding *bond = netdev_priv(dev);
4111 struct slave *slave = bond->first_slave;
4113 if (slave) {
4114 const struct net_device_ops *slave_ops
4115 = slave->dev->netdev_ops;
4116 if (slave_ops->ndo_neigh_setup)
4117 return slave_ops->ndo_neigh_setup(dev, parms);
4119 return 0;
4123 * Change the MTU of all of a master's slaves to match the master
4125 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4127 struct bonding *bond = netdev_priv(bond_dev);
4128 struct slave *slave, *stop_at;
4129 int res = 0;
4130 int i;
4132 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
4133 (bond_dev ? bond_dev->name : "None"), new_mtu);
4135 /* Can't hold bond->lock with bh disabled here since
4136 * some base drivers panic. On the other hand we can't
4137 * hold bond->lock without bh disabled because we'll
4138 * deadlock. The only solution is to rely on the fact
4139 * that we're under rtnl_lock here, and the slaves
4140 * list won't change. This doesn't solve the problem
4141 * of setting the slave's MTU while it is
4142 * transmitting, but the assumption is that the base
4143 * driver can handle that.
4145 * TODO: figure out a way to safely iterate the slaves
4146 * list, but without holding a lock around the actual
4147 * call to the base driver.
4150 bond_for_each_slave(bond, slave, i) {
4151 pr_debug("s %p s->p %p c_m %p\n", slave,
4152 slave->prev, slave->dev->change_mtu);
4154 res = dev_set_mtu(slave->dev, new_mtu);
4156 if (res) {
4157 /* If we failed to set the slave's mtu to the new value
4158 * we must abort the operation even in ACTIVE_BACKUP
4159 * mode, because if we allow the backup slaves to have
4160 * different mtu values than the active slave we'll
4161 * need to change their mtu when doing a failover. That
4162 * means changing their mtu from timer context, which
4163 * is probably not a good idea.
4165 pr_debug("err %d %s\n", res, slave->dev->name);
4166 goto unwind;
4170 bond_dev->mtu = new_mtu;
4172 return 0;
4174 unwind:
4175 /* unwind from head to the slave that failed */
4176 stop_at = slave;
4177 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4178 int tmp_res;
4180 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4181 if (tmp_res) {
4182 pr_debug("unwind err %d dev %s\n", tmp_res,
4183 slave->dev->name);
4187 return res;
4191 * Change HW address
4193 * Note that many devices must be down to change the HW address, and
4194 * downing the master releases all slaves. We can make bonds full of
4195 * bonding devices to test this, however.
4197 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4199 struct bonding *bond = netdev_priv(bond_dev);
4200 struct sockaddr *sa = addr, tmp_sa;
4201 struct slave *slave, *stop_at;
4202 int res = 0;
4203 int i;
4205 if (bond->params.mode == BOND_MODE_ALB)
4206 return bond_alb_set_mac_address(bond_dev, addr);
4209 pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4212 * If fail_over_mac is set to active, do nothing and return
4213 * success. Returning an error causes ifenslave to fail.
4215 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
4216 return 0;
4218 if (!is_valid_ether_addr(sa->sa_data)) {
4219 return -EADDRNOTAVAIL;
4222 /* Can't hold bond->lock with bh disabled here since
4223 * some base drivers panic. On the other hand we can't
4224 * hold bond->lock without bh disabled because we'll
4225 * deadlock. The only solution is to rely on the fact
4226 * that we're under rtnl_lock here, and the slaves
4227 * list won't change. This doesn't solve the problem
4228 * of setting the slave's hw address while it is
4229 * transmitting, but the assumption is that the base
4230 * driver can handle that.
4232 * TODO: figure out a way to safely iterate the slaves
4233 * list, but without holding a lock around the actual
4234 * call to the base driver.
4237 bond_for_each_slave(bond, slave, i) {
4238 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
4239 pr_debug("slave %p %s\n", slave, slave->dev->name);
4241 if (slave_ops->ndo_set_mac_address == NULL) {
4242 res = -EOPNOTSUPP;
4243 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
4244 goto unwind;
4247 res = dev_set_mac_address(slave->dev, addr);
4248 if (res) {
4249 /* TODO: consider downing the slave
4250 * and retry ?
4251 * User should expect communications
4252 * breakage anyway until ARP finish
4253 * updating, so...
4255 pr_debug("err %d %s\n", res, slave->dev->name);
4256 goto unwind;
4260 /* success */
4261 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4262 return 0;
4264 unwind:
4265 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4266 tmp_sa.sa_family = bond_dev->type;
4268 /* unwind from head to the slave that failed */
4269 stop_at = slave;
4270 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4271 int tmp_res;
4273 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4274 if (tmp_res) {
4275 pr_debug("unwind err %d dev %s\n", tmp_res,
4276 slave->dev->name);
4280 return res;
4283 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4285 struct bonding *bond = netdev_priv(bond_dev);
4286 struct slave *slave, *start_at;
4287 int i, slave_no, res = 1;
4289 read_lock(&bond->lock);
4291 if (!BOND_IS_OK(bond)) {
4292 goto out;
4296 * Concurrent TX may collide on rr_tx_counter; we accept that
4297 * as being rare enough not to justify using an atomic op here
4299 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4301 bond_for_each_slave(bond, slave, i) {
4302 slave_no--;
4303 if (slave_no < 0) {
4304 break;
4308 start_at = slave;
4309 bond_for_each_slave_from(bond, slave, i, start_at) {
4310 if (IS_UP(slave->dev) &&
4311 (slave->link == BOND_LINK_UP) &&
4312 (slave->state == BOND_STATE_ACTIVE)) {
4313 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4314 break;
4318 out:
4319 if (res) {
4320 /* no suitable interface, frame not sent */
4321 dev_kfree_skb(skb);
4323 read_unlock(&bond->lock);
4324 return 0;
4329 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4330 * the bond has a usable interface.
4332 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4334 struct bonding *bond = netdev_priv(bond_dev);
4335 int res = 1;
4337 read_lock(&bond->lock);
4338 read_lock(&bond->curr_slave_lock);
4340 if (!BOND_IS_OK(bond)) {
4341 goto out;
4344 if (!bond->curr_active_slave)
4345 goto out;
4347 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4349 out:
4350 if (res) {
4351 /* no suitable interface, frame not sent */
4352 dev_kfree_skb(skb);
4354 read_unlock(&bond->curr_slave_lock);
4355 read_unlock(&bond->lock);
4356 return 0;
4360 * In bond_xmit_xor() , we determine the output device by using a pre-
4361 * determined xmit_hash_policy(), If the selected device is not enabled,
4362 * find the next active slave.
4364 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4366 struct bonding *bond = netdev_priv(bond_dev);
4367 struct slave *slave, *start_at;
4368 int slave_no;
4369 int i;
4370 int res = 1;
4372 read_lock(&bond->lock);
4374 if (!BOND_IS_OK(bond)) {
4375 goto out;
4378 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
4380 bond_for_each_slave(bond, slave, i) {
4381 slave_no--;
4382 if (slave_no < 0) {
4383 break;
4387 start_at = slave;
4389 bond_for_each_slave_from(bond, slave, i, start_at) {
4390 if (IS_UP(slave->dev) &&
4391 (slave->link == BOND_LINK_UP) &&
4392 (slave->state == BOND_STATE_ACTIVE)) {
4393 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4394 break;
4398 out:
4399 if (res) {
4400 /* no suitable interface, frame not sent */
4401 dev_kfree_skb(skb);
4403 read_unlock(&bond->lock);
4404 return 0;
4408 * in broadcast mode, we send everything to all usable interfaces.
4410 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4412 struct bonding *bond = netdev_priv(bond_dev);
4413 struct slave *slave, *start_at;
4414 struct net_device *tx_dev = NULL;
4415 int i;
4416 int res = 1;
4418 read_lock(&bond->lock);
4420 if (!BOND_IS_OK(bond)) {
4421 goto out;
4424 read_lock(&bond->curr_slave_lock);
4425 start_at = bond->curr_active_slave;
4426 read_unlock(&bond->curr_slave_lock);
4428 if (!start_at) {
4429 goto out;
4432 bond_for_each_slave_from(bond, slave, i, start_at) {
4433 if (IS_UP(slave->dev) &&
4434 (slave->link == BOND_LINK_UP) &&
4435 (slave->state == BOND_STATE_ACTIVE)) {
4436 if (tx_dev) {
4437 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4438 if (!skb2) {
4439 printk(KERN_ERR DRV_NAME
4440 ": %s: Error: bond_xmit_broadcast(): "
4441 "skb_clone() failed\n",
4442 bond_dev->name);
4443 continue;
4446 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4447 if (res) {
4448 dev_kfree_skb(skb2);
4449 continue;
4452 tx_dev = slave->dev;
4456 if (tx_dev) {
4457 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4460 out:
4461 if (res) {
4462 /* no suitable interface, frame not sent */
4463 dev_kfree_skb(skb);
4465 /* frame sent to all suitable interfaces */
4466 read_unlock(&bond->lock);
4467 return 0;
4470 /*------------------------- Device initialization ---------------------------*/
4472 static void bond_set_xmit_hash_policy(struct bonding *bond)
4474 switch (bond->params.xmit_policy) {
4475 case BOND_XMIT_POLICY_LAYER23:
4476 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4477 break;
4478 case BOND_XMIT_POLICY_LAYER34:
4479 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4480 break;
4481 case BOND_XMIT_POLICY_LAYER2:
4482 default:
4483 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4484 break;
4488 static int bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4490 const struct bonding *bond = netdev_priv(dev);
4492 switch (bond->params.mode) {
4493 case BOND_MODE_ROUNDROBIN:
4494 return bond_xmit_roundrobin(skb, dev);
4495 case BOND_MODE_ACTIVEBACKUP:
4496 return bond_xmit_activebackup(skb, dev);
4497 case BOND_MODE_XOR:
4498 return bond_xmit_xor(skb, dev);
4499 case BOND_MODE_BROADCAST:
4500 return bond_xmit_broadcast(skb, dev);
4501 case BOND_MODE_8023AD:
4502 return bond_3ad_xmit_xor(skb, dev);
4503 case BOND_MODE_ALB:
4504 case BOND_MODE_TLB:
4505 return bond_alb_xmit(skb, dev);
4506 default:
4507 /* Should never happen, mode already checked */
4508 printk(KERN_ERR DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
4509 dev->name, bond->params.mode);
4510 WARN_ON_ONCE(1);
4511 dev_kfree_skb(skb);
4512 return NETDEV_TX_OK;
4518 * set bond mode specific net device operations
4520 void bond_set_mode_ops(struct bonding *bond, int mode)
4522 struct net_device *bond_dev = bond->dev;
4524 switch (mode) {
4525 case BOND_MODE_ROUNDROBIN:
4526 break;
4527 case BOND_MODE_ACTIVEBACKUP:
4528 break;
4529 case BOND_MODE_XOR:
4530 bond_set_xmit_hash_policy(bond);
4531 break;
4532 case BOND_MODE_BROADCAST:
4533 break;
4534 case BOND_MODE_8023AD:
4535 bond_set_master_3ad_flags(bond);
4536 bond_set_xmit_hash_policy(bond);
4537 break;
4538 case BOND_MODE_ALB:
4539 bond_set_master_alb_flags(bond);
4540 /* FALLTHRU */
4541 case BOND_MODE_TLB:
4542 break;
4543 default:
4544 /* Should never happen, mode already checked */
4545 printk(KERN_ERR DRV_NAME
4546 ": %s: Error: Unknown bonding mode %d\n",
4547 bond_dev->name,
4548 mode);
4549 break;
4553 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4554 struct ethtool_drvinfo *drvinfo)
4556 strncpy(drvinfo->driver, DRV_NAME, 32);
4557 strncpy(drvinfo->version, DRV_VERSION, 32);
4558 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4561 static const struct ethtool_ops bond_ethtool_ops = {
4562 .get_drvinfo = bond_ethtool_get_drvinfo,
4563 .get_link = ethtool_op_get_link,
4564 .get_tx_csum = ethtool_op_get_tx_csum,
4565 .get_sg = ethtool_op_get_sg,
4566 .get_tso = ethtool_op_get_tso,
4567 .get_ufo = ethtool_op_get_ufo,
4568 .get_flags = ethtool_op_get_flags,
4571 static const struct net_device_ops bond_netdev_ops = {
4572 .ndo_open = bond_open,
4573 .ndo_stop = bond_close,
4574 .ndo_start_xmit = bond_start_xmit,
4575 .ndo_get_stats = bond_get_stats,
4576 .ndo_do_ioctl = bond_do_ioctl,
4577 .ndo_set_multicast_list = bond_set_multicast_list,
4578 .ndo_change_mtu = bond_change_mtu,
4579 .ndo_set_mac_address = bond_set_mac_address,
4580 .ndo_neigh_setup = bond_neigh_setup,
4581 .ndo_vlan_rx_register = bond_vlan_rx_register,
4582 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4583 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4587 * Does not allocate but creates a /proc entry.
4588 * Allowed to fail.
4590 static int bond_init(struct net_device *bond_dev, struct bond_params *params)
4592 struct bonding *bond = netdev_priv(bond_dev);
4594 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4596 /* initialize rwlocks */
4597 rwlock_init(&bond->lock);
4598 rwlock_init(&bond->curr_slave_lock);
4600 bond->params = *params; /* copy params struct */
4602 bond->wq = create_singlethread_workqueue(bond_dev->name);
4603 if (!bond->wq)
4604 return -ENOMEM;
4606 /* Initialize pointers */
4607 bond->first_slave = NULL;
4608 bond->curr_active_slave = NULL;
4609 bond->current_arp_slave = NULL;
4610 bond->primary_slave = NULL;
4611 bond->dev = bond_dev;
4612 bond->send_grat_arp = 0;
4613 bond->send_unsol_na = 0;
4614 bond->setup_by_slave = 0;
4615 INIT_LIST_HEAD(&bond->vlan_list);
4617 /* Initialize the device entry points */
4618 bond_dev->netdev_ops = &bond_netdev_ops;
4619 bond_dev->ethtool_ops = &bond_ethtool_ops;
4620 bond_set_mode_ops(bond, bond->params.mode);
4622 bond_dev->destructor = bond_destructor;
4624 /* Initialize the device options */
4625 bond_dev->tx_queue_len = 0;
4626 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4627 bond_dev->priv_flags |= IFF_BONDING;
4628 if (bond->params.arp_interval)
4629 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
4631 /* At first, we block adding VLANs. That's the only way to
4632 * prevent problems that occur when adding VLANs over an
4633 * empty bond. The block will be removed once non-challenged
4634 * slaves are enslaved.
4636 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4638 /* don't acquire bond device's netif_tx_lock when
4639 * transmitting */
4640 bond_dev->features |= NETIF_F_LLTX;
4642 /* By default, we declare the bond to be fully
4643 * VLAN hardware accelerated capable. Special
4644 * care is taken in the various xmit functions
4645 * when there are slaves that are not hw accel
4646 * capable
4648 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4649 NETIF_F_HW_VLAN_RX |
4650 NETIF_F_HW_VLAN_FILTER);
4652 #ifdef CONFIG_PROC_FS
4653 bond_create_proc_entry(bond);
4654 #endif
4655 list_add_tail(&bond->bond_list, &bond_dev_list);
4657 return 0;
4660 static void bond_work_cancel_all(struct bonding *bond)
4662 write_lock_bh(&bond->lock);
4663 bond->kill_timers = 1;
4664 write_unlock_bh(&bond->lock);
4666 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4667 cancel_delayed_work(&bond->mii_work);
4669 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4670 cancel_delayed_work(&bond->arp_work);
4672 if (bond->params.mode == BOND_MODE_ALB &&
4673 delayed_work_pending(&bond->alb_work))
4674 cancel_delayed_work(&bond->alb_work);
4676 if (bond->params.mode == BOND_MODE_8023AD &&
4677 delayed_work_pending(&bond->ad_work))
4678 cancel_delayed_work(&bond->ad_work);
4681 /* De-initialize device specific data.
4682 * Caller must hold rtnl_lock.
4684 static void bond_deinit(struct net_device *bond_dev)
4686 struct bonding *bond = netdev_priv(bond_dev);
4688 list_del(&bond->bond_list);
4690 bond_work_cancel_all(bond);
4692 #ifdef CONFIG_PROC_FS
4693 bond_remove_proc_entry(bond);
4694 #endif
4697 /* Unregister and free all bond devices.
4698 * Caller must hold rtnl_lock.
4700 static void bond_free_all(void)
4702 struct bonding *bond, *nxt;
4704 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4705 struct net_device *bond_dev = bond->dev;
4707 bond_work_cancel_all(bond);
4708 /* Release the bonded slaves */
4709 bond_release_all(bond_dev);
4710 bond_destroy(bond);
4713 #ifdef CONFIG_PROC_FS
4714 bond_destroy_proc_dir();
4715 #endif
4718 /*------------------------- Module initialization ---------------------------*/
4721 * Convert string input module parms. Accept either the
4722 * number of the mode or its string name. A bit complicated because
4723 * some mode names are substrings of other names, and calls from sysfs
4724 * may have whitespace in the name (trailing newlines, for example).
4726 int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
4728 int mode = -1, i, rv;
4729 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4731 for (p = (char *)buf; *p; p++)
4732 if (!(isdigit(*p) || isspace(*p)))
4733 break;
4735 if (*p)
4736 rv = sscanf(buf, "%20s", modestr);
4737 else
4738 rv = sscanf(buf, "%d", &mode);
4740 if (!rv)
4741 return -1;
4743 for (i = 0; tbl[i].modename; i++) {
4744 if (mode == tbl[i].mode)
4745 return tbl[i].mode;
4746 if (strcmp(modestr, tbl[i].modename) == 0)
4747 return tbl[i].mode;
4750 return -1;
4753 static int bond_check_params(struct bond_params *params)
4755 int arp_validate_value, fail_over_mac_value;
4758 * Convert string parameters.
4760 if (mode) {
4761 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4762 if (bond_mode == -1) {
4763 printk(KERN_ERR DRV_NAME
4764 ": Error: Invalid bonding mode \"%s\"\n",
4765 mode == NULL ? "NULL" : mode);
4766 return -EINVAL;
4770 if (xmit_hash_policy) {
4771 if ((bond_mode != BOND_MODE_XOR) &&
4772 (bond_mode != BOND_MODE_8023AD)) {
4773 printk(KERN_INFO DRV_NAME
4774 ": xor_mode param is irrelevant in mode %s\n",
4775 bond_mode_name(bond_mode));
4776 } else {
4777 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4778 xmit_hashtype_tbl);
4779 if (xmit_hashtype == -1) {
4780 printk(KERN_ERR DRV_NAME
4781 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4782 xmit_hash_policy == NULL ? "NULL" :
4783 xmit_hash_policy);
4784 return -EINVAL;
4789 if (lacp_rate) {
4790 if (bond_mode != BOND_MODE_8023AD) {
4791 printk(KERN_INFO DRV_NAME
4792 ": lacp_rate param is irrelevant in mode %s\n",
4793 bond_mode_name(bond_mode));
4794 } else {
4795 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4796 if (lacp_fast == -1) {
4797 printk(KERN_ERR DRV_NAME
4798 ": Error: Invalid lacp rate \"%s\"\n",
4799 lacp_rate == NULL ? "NULL" : lacp_rate);
4800 return -EINVAL;
4805 if (ad_select) {
4806 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4807 if (params->ad_select == -1) {
4808 printk(KERN_ERR DRV_NAME
4809 ": Error: Invalid ad_select \"%s\"\n",
4810 ad_select == NULL ? "NULL" : ad_select);
4811 return -EINVAL;
4814 if (bond_mode != BOND_MODE_8023AD) {
4815 printk(KERN_WARNING DRV_NAME
4816 ": ad_select param only affects 802.3ad mode\n");
4818 } else {
4819 params->ad_select = BOND_AD_STABLE;
4822 if (max_bonds < 0 || max_bonds > INT_MAX) {
4823 printk(KERN_WARNING DRV_NAME
4824 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4825 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4826 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4827 max_bonds = BOND_DEFAULT_MAX_BONDS;
4830 if (miimon < 0) {
4831 printk(KERN_WARNING DRV_NAME
4832 ": Warning: miimon module parameter (%d), "
4833 "not in range 0-%d, so it was reset to %d\n",
4834 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4835 miimon = BOND_LINK_MON_INTERV;
4838 if (updelay < 0) {
4839 printk(KERN_WARNING DRV_NAME
4840 ": Warning: updelay module parameter (%d), "
4841 "not in range 0-%d, so it was reset to 0\n",
4842 updelay, INT_MAX);
4843 updelay = 0;
4846 if (downdelay < 0) {
4847 printk(KERN_WARNING DRV_NAME
4848 ": Warning: downdelay module parameter (%d), "
4849 "not in range 0-%d, so it was reset to 0\n",
4850 downdelay, INT_MAX);
4851 downdelay = 0;
4854 if ((use_carrier != 0) && (use_carrier != 1)) {
4855 printk(KERN_WARNING DRV_NAME
4856 ": Warning: use_carrier module parameter (%d), "
4857 "not of valid value (0/1), so it was set to 1\n",
4858 use_carrier);
4859 use_carrier = 1;
4862 if (num_grat_arp < 0 || num_grat_arp > 255) {
4863 printk(KERN_WARNING DRV_NAME
4864 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4865 "was reset to 1 \n", num_grat_arp);
4866 num_grat_arp = 1;
4869 if (num_unsol_na < 0 || num_unsol_na > 255) {
4870 printk(KERN_WARNING DRV_NAME
4871 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4872 "was reset to 1 \n", num_unsol_na);
4873 num_unsol_na = 1;
4876 /* reset values for 802.3ad */
4877 if (bond_mode == BOND_MODE_8023AD) {
4878 if (!miimon) {
4879 printk(KERN_WARNING DRV_NAME
4880 ": Warning: miimon must be specified, "
4881 "otherwise bonding will not detect link "
4882 "failure, speed and duplex which are "
4883 "essential for 802.3ad operation\n");
4884 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4885 miimon = 100;
4889 /* reset values for TLB/ALB */
4890 if ((bond_mode == BOND_MODE_TLB) ||
4891 (bond_mode == BOND_MODE_ALB)) {
4892 if (!miimon) {
4893 printk(KERN_WARNING DRV_NAME
4894 ": Warning: miimon must be specified, "
4895 "otherwise bonding will not detect link "
4896 "failure and link speed which are essential "
4897 "for TLB/ALB load balancing\n");
4898 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4899 miimon = 100;
4903 if (bond_mode == BOND_MODE_ALB) {
4904 printk(KERN_NOTICE DRV_NAME
4905 ": In ALB mode you might experience client "
4906 "disconnections upon reconnection of a link if the "
4907 "bonding module updelay parameter (%d msec) is "
4908 "incompatible with the forwarding delay time of the "
4909 "switch\n",
4910 updelay);
4913 if (!miimon) {
4914 if (updelay || downdelay) {
4915 /* just warn the user the up/down delay will have
4916 * no effect since miimon is zero...
4918 printk(KERN_WARNING DRV_NAME
4919 ": Warning: miimon module parameter not set "
4920 "and updelay (%d) or downdelay (%d) module "
4921 "parameter is set; updelay and downdelay have "
4922 "no effect unless miimon is set\n",
4923 updelay, downdelay);
4925 } else {
4926 /* don't allow arp monitoring */
4927 if (arp_interval) {
4928 printk(KERN_WARNING DRV_NAME
4929 ": Warning: miimon (%d) and arp_interval (%d) "
4930 "can't be used simultaneously, disabling ARP "
4931 "monitoring\n",
4932 miimon, arp_interval);
4933 arp_interval = 0;
4936 if ((updelay % miimon) != 0) {
4937 printk(KERN_WARNING DRV_NAME
4938 ": Warning: updelay (%d) is not a multiple "
4939 "of miimon (%d), updelay rounded to %d ms\n",
4940 updelay, miimon, (updelay / miimon) * miimon);
4943 updelay /= miimon;
4945 if ((downdelay % miimon) != 0) {
4946 printk(KERN_WARNING DRV_NAME
4947 ": Warning: downdelay (%d) is not a multiple "
4948 "of miimon (%d), downdelay rounded to %d ms\n",
4949 downdelay, miimon,
4950 (downdelay / miimon) * miimon);
4953 downdelay /= miimon;
4956 if (arp_interval < 0) {
4957 printk(KERN_WARNING DRV_NAME
4958 ": Warning: arp_interval module parameter (%d) "
4959 ", not in range 0-%d, so it was reset to %d\n",
4960 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4961 arp_interval = BOND_LINK_ARP_INTERV;
4964 for (arp_ip_count = 0;
4965 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4966 arp_ip_count++) {
4967 /* not complete check, but should be good enough to
4968 catch mistakes */
4969 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4970 printk(KERN_WARNING DRV_NAME
4971 ": Warning: bad arp_ip_target module parameter "
4972 "(%s), ARP monitoring will not be performed\n",
4973 arp_ip_target[arp_ip_count]);
4974 arp_interval = 0;
4975 } else {
4976 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4977 arp_target[arp_ip_count] = ip;
4981 if (arp_interval && !arp_ip_count) {
4982 /* don't allow arping if no arp_ip_target given... */
4983 printk(KERN_WARNING DRV_NAME
4984 ": Warning: arp_interval module parameter (%d) "
4985 "specified without providing an arp_ip_target "
4986 "parameter, arp_interval was reset to 0\n",
4987 arp_interval);
4988 arp_interval = 0;
4991 if (arp_validate) {
4992 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4993 printk(KERN_ERR DRV_NAME
4994 ": arp_validate only supported in active-backup mode\n");
4995 return -EINVAL;
4997 if (!arp_interval) {
4998 printk(KERN_ERR DRV_NAME
4999 ": arp_validate requires arp_interval\n");
5000 return -EINVAL;
5003 arp_validate_value = bond_parse_parm(arp_validate,
5004 arp_validate_tbl);
5005 if (arp_validate_value == -1) {
5006 printk(KERN_ERR DRV_NAME
5007 ": Error: invalid arp_validate \"%s\"\n",
5008 arp_validate == NULL ? "NULL" : arp_validate);
5009 return -EINVAL;
5011 } else
5012 arp_validate_value = 0;
5014 if (miimon) {
5015 printk(KERN_INFO DRV_NAME
5016 ": MII link monitoring set to %d ms\n",
5017 miimon);
5018 } else if (arp_interval) {
5019 int i;
5021 printk(KERN_INFO DRV_NAME
5022 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
5023 arp_interval,
5024 arp_validate_tbl[arp_validate_value].modename,
5025 arp_ip_count);
5027 for (i = 0; i < arp_ip_count; i++)
5028 printk (" %s", arp_ip_target[i]);
5030 printk("\n");
5032 } else if (max_bonds) {
5033 /* miimon and arp_interval not set, we need one so things
5034 * work as expected, see bonding.txt for details
5036 printk(KERN_WARNING DRV_NAME
5037 ": Warning: either miimon or arp_interval and "
5038 "arp_ip_target module parameters must be specified, "
5039 "otherwise bonding will not detect link failures! see "
5040 "bonding.txt for details.\n");
5043 if (primary && !USES_PRIMARY(bond_mode)) {
5044 /* currently, using a primary only makes sense
5045 * in active backup, TLB or ALB modes
5047 printk(KERN_WARNING DRV_NAME
5048 ": Warning: %s primary device specified but has no "
5049 "effect in %s mode\n",
5050 primary, bond_mode_name(bond_mode));
5051 primary = NULL;
5054 if (fail_over_mac) {
5055 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5056 fail_over_mac_tbl);
5057 if (fail_over_mac_value == -1) {
5058 printk(KERN_ERR DRV_NAME
5059 ": Error: invalid fail_over_mac \"%s\"\n",
5060 arp_validate == NULL ? "NULL" : arp_validate);
5061 return -EINVAL;
5064 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5065 printk(KERN_WARNING DRV_NAME
5066 ": Warning: fail_over_mac only affects "
5067 "active-backup mode.\n");
5068 } else {
5069 fail_over_mac_value = BOND_FOM_NONE;
5072 /* fill params struct with the proper values */
5073 params->mode = bond_mode;
5074 params->xmit_policy = xmit_hashtype;
5075 params->miimon = miimon;
5076 params->num_grat_arp = num_grat_arp;
5077 params->num_unsol_na = num_unsol_na;
5078 params->arp_interval = arp_interval;
5079 params->arp_validate = arp_validate_value;
5080 params->updelay = updelay;
5081 params->downdelay = downdelay;
5082 params->use_carrier = use_carrier;
5083 params->lacp_fast = lacp_fast;
5084 params->primary[0] = 0;
5085 params->fail_over_mac = fail_over_mac_value;
5087 if (primary) {
5088 strncpy(params->primary, primary, IFNAMSIZ);
5089 params->primary[IFNAMSIZ - 1] = 0;
5092 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5094 return 0;
5097 static struct lock_class_key bonding_netdev_xmit_lock_key;
5098 static struct lock_class_key bonding_netdev_addr_lock_key;
5100 static void bond_set_lockdep_class_one(struct net_device *dev,
5101 struct netdev_queue *txq,
5102 void *_unused)
5104 lockdep_set_class(&txq->_xmit_lock,
5105 &bonding_netdev_xmit_lock_key);
5108 static void bond_set_lockdep_class(struct net_device *dev)
5110 lockdep_set_class(&dev->addr_list_lock,
5111 &bonding_netdev_addr_lock_key);
5112 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
5115 /* Create a new bond based on the specified name and bonding parameters.
5116 * If name is NULL, obtain a suitable "bond%d" name for us.
5117 * Caller must NOT hold rtnl_lock; we need to release it here before we
5118 * set up our sysfs entries.
5120 int bond_create(char *name, struct bond_params *params)
5122 struct net_device *bond_dev;
5123 struct bonding *bond;
5124 int res;
5126 rtnl_lock();
5127 down_write(&bonding_rwsem);
5129 /* Check to see if the bond already exists. */
5130 if (name) {
5131 list_for_each_entry(bond, &bond_dev_list, bond_list)
5132 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
5133 printk(KERN_ERR DRV_NAME
5134 ": cannot add bond %s; it already exists\n",
5135 name);
5136 res = -EPERM;
5137 goto out_rtnl;
5141 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
5142 ether_setup);
5143 if (!bond_dev) {
5144 printk(KERN_ERR DRV_NAME
5145 ": %s: eek! can't alloc netdev!\n",
5146 name);
5147 res = -ENOMEM;
5148 goto out_rtnl;
5151 if (!name) {
5152 res = dev_alloc_name(bond_dev, "bond%d");
5153 if (res < 0)
5154 goto out_netdev;
5157 /* bond_init() must be called after dev_alloc_name() (for the
5158 * /proc files), but before register_netdevice(), because we
5159 * need to set function pointers.
5162 res = bond_init(bond_dev, params);
5163 if (res < 0) {
5164 goto out_netdev;
5167 res = register_netdevice(bond_dev);
5168 if (res < 0) {
5169 goto out_bond;
5172 bond_set_lockdep_class(bond_dev);
5174 netif_carrier_off(bond_dev);
5176 up_write(&bonding_rwsem);
5177 rtnl_unlock(); /* allows sysfs registration of net device */
5178 res = bond_create_sysfs_entry(netdev_priv(bond_dev));
5179 if (res < 0) {
5180 rtnl_lock();
5181 down_write(&bonding_rwsem);
5182 bond_deinit(bond_dev);
5183 unregister_netdevice(bond_dev);
5184 goto out_rtnl;
5187 return 0;
5189 out_bond:
5190 bond_deinit(bond_dev);
5191 out_netdev:
5192 free_netdev(bond_dev);
5193 out_rtnl:
5194 up_write(&bonding_rwsem);
5195 rtnl_unlock();
5196 return res;
5199 static int __init bonding_init(void)
5201 int i;
5202 int res;
5203 struct bonding *bond;
5205 printk(KERN_INFO "%s", version);
5207 res = bond_check_params(&bonding_defaults);
5208 if (res) {
5209 goto out;
5212 #ifdef CONFIG_PROC_FS
5213 bond_create_proc_dir();
5214 #endif
5216 init_rwsem(&bonding_rwsem);
5218 for (i = 0; i < max_bonds; i++) {
5219 res = bond_create(NULL, &bonding_defaults);
5220 if (res)
5221 goto err;
5224 res = bond_create_sysfs();
5225 if (res)
5226 goto err;
5228 register_netdevice_notifier(&bond_netdev_notifier);
5229 register_inetaddr_notifier(&bond_inetaddr_notifier);
5230 bond_register_ipv6_notifier();
5232 goto out;
5233 err:
5234 list_for_each_entry(bond, &bond_dev_list, bond_list) {
5235 bond_work_cancel_all(bond);
5236 destroy_workqueue(bond->wq);
5239 bond_destroy_sysfs();
5241 rtnl_lock();
5242 bond_free_all();
5243 rtnl_unlock();
5244 out:
5245 return res;
5249 static void __exit bonding_exit(void)
5251 unregister_netdevice_notifier(&bond_netdev_notifier);
5252 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
5253 bond_unregister_ipv6_notifier();
5255 bond_destroy_sysfs();
5257 rtnl_lock();
5258 bond_free_all();
5259 rtnl_unlock();
5262 module_init(bonding_init);
5263 module_exit(bonding_exit);
5264 MODULE_LICENSE("GPL");
5265 MODULE_VERSION(DRV_VERSION);
5266 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5267 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5268 MODULE_SUPPORTED_DEVICE("most ethernet devices");
5271 * Local variables:
5272 * c-indent-level: 8
5273 * c-basic-offset: 8
5274 * tab-width: 8
5275 * End: