]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/bonding/bond_main.c
Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/linville...
[net-next-2.6.git] / drivers / net / bonding / bond_main.c
CommitLineData
1da177e4
LT
1/*
2 * originally based on the dummy device.
3 *
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6 *
7 * bonding.c: an Ethernet Bonding driver
8 *
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 ...
15 *
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.
22 *
23 * ifconfig bond0 down
24 * will release all slaves, marking them as down.
25 *
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.
31 *
1da177e4
LT
32 */
33
34//#define BONDING_DEBUG 1
35
36#include <linux/config.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/sched.h>
40#include <linux/types.h>
41#include <linux/fcntl.h>
42#include <linux/interrupt.h>
43#include <linux/ptrace.h>
44#include <linux/ioport.h>
45#include <linux/in.h>
169a3e66 46#include <net/ip.h>
1da177e4 47#include <linux/ip.h>
169a3e66
JV
48#include <linux/tcp.h>
49#include <linux/udp.h>
1da177e4
LT
50#include <linux/slab.h>
51#include <linux/string.h>
52#include <linux/init.h>
53#include <linux/timer.h>
54#include <linux/socket.h>
55#include <linux/ctype.h>
56#include <linux/inet.h>
57#include <linux/bitops.h>
58#include <asm/system.h>
59#include <asm/io.h>
60#include <asm/dma.h>
61#include <asm/uaccess.h>
62#include <linux/errno.h>
63#include <linux/netdevice.h>
64#include <linux/inetdevice.h>
65#include <linux/etherdevice.h>
66#include <linux/skbuff.h>
67#include <net/sock.h>
68#include <linux/rtnetlink.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
71#include <linux/smp.h>
72#include <linux/if_ether.h>
73#include <net/arp.h>
74#include <linux/mii.h>
75#include <linux/ethtool.h>
76#include <linux/if_vlan.h>
77#include <linux/if_bonding.h>
c3ade5ca 78#include <net/route.h>
1da177e4
LT
79#include "bonding.h"
80#include "bond_3ad.h"
81#include "bond_alb.h"
82
83/*---------------------------- Module parameters ----------------------------*/
84
85/* monitor all links that often (in milliseconds). <=0 disables monitoring */
86#define BOND_LINK_MON_INTERV 0
87#define BOND_LINK_ARP_INTERV 0
88
89static int max_bonds = BOND_DEFAULT_MAX_BONDS;
90static int miimon = BOND_LINK_MON_INTERV;
91static int updelay = 0;
92static int downdelay = 0;
93static int use_carrier = 1;
94static char *mode = NULL;
95static char *primary = NULL;
96static char *lacp_rate = NULL;
169a3e66 97static char *xmit_hash_policy = NULL;
1da177e4
LT
98static int arp_interval = BOND_LINK_ARP_INTERV;
99static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
12479f9a 100struct bond_params bonding_defaults;
1da177e4
LT
101
102module_param(max_bonds, int, 0);
103MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
104module_param(miimon, int, 0);
105MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
106module_param(updelay, int, 0);
107MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
108module_param(downdelay, int, 0);
2ac47660
MW
109MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
110 "in milliseconds");
1da177e4 111module_param(use_carrier, int, 0);
2ac47660
MW
112MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
113 "0 for off, 1 for on (default)");
1da177e4 114module_param(mode, charp, 0);
2ac47660
MW
115MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
116 "1 for active-backup, 2 for balance-xor, "
117 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
118 "6 for balance-alb");
1da177e4
LT
119module_param(primary, charp, 0);
120MODULE_PARM_DESC(primary, "Primary network device to use");
121module_param(lacp_rate, charp, 0);
2ac47660
MW
122MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
123 "(slow/fast)");
169a3e66 124module_param(xmit_hash_policy, charp, 0);
2ac47660
MW
125MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
126 ", 1 for layer 3+4");
1da177e4
LT
127module_param(arp_interval, int, 0);
128MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
129module_param_array(arp_ip_target, charp, NULL, 0);
130MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
131
132/*----------------------------- Global variables ----------------------------*/
133
f71e1309 134static const char * const version =
1da177e4
LT
135 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
136
12479f9a 137LIST_HEAD(bond_dev_list);
1da177e4
LT
138
139#ifdef CONFIG_PROC_FS
140static struct proc_dir_entry *bond_proc_dir = NULL;
141#endif
142
b76cdba9 143extern struct rw_semaphore bonding_rwsem;
1da177e4
LT
144static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
145static int arp_ip_count = 0;
1da177e4 146static int bond_mode = BOND_MODE_ROUNDROBIN;
169a3e66 147static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
1da177e4 148static int lacp_fast = 0;
217df670 149
1da177e4 150
12479f9a 151struct bond_parm_tbl bond_lacp_tbl[] = {
1da177e4
LT
152{ "slow", AD_LACP_SLOW},
153{ "fast", AD_LACP_FAST},
154{ NULL, -1},
155};
156
12479f9a 157struct bond_parm_tbl bond_mode_tbl[] = {
1da177e4
LT
158{ "balance-rr", BOND_MODE_ROUNDROBIN},
159{ "active-backup", BOND_MODE_ACTIVEBACKUP},
160{ "balance-xor", BOND_MODE_XOR},
161{ "broadcast", BOND_MODE_BROADCAST},
162{ "802.3ad", BOND_MODE_8023AD},
163{ "balance-tlb", BOND_MODE_TLB},
164{ "balance-alb", BOND_MODE_ALB},
165{ NULL, -1},
166};
167
12479f9a 168struct bond_parm_tbl xmit_hashtype_tbl[] = {
169a3e66
JV
169{ "layer2", BOND_XMIT_POLICY_LAYER2},
170{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
171{ NULL, -1},
172};
173
1da177e4
LT
174/*-------------------------- Forward declarations ---------------------------*/
175
c3ade5ca 176static void bond_send_gratuitous_arp(struct bonding *bond);
1da177e4
LT
177
178/*---------------------------- General routines -----------------------------*/
179
a77b5325 180const char *bond_mode_name(int mode)
1da177e4
LT
181{
182 switch (mode) {
183 case BOND_MODE_ROUNDROBIN :
184 return "load balancing (round-robin)";
185 case BOND_MODE_ACTIVEBACKUP :
186 return "fault-tolerance (active-backup)";
187 case BOND_MODE_XOR :
188 return "load balancing (xor)";
189 case BOND_MODE_BROADCAST :
190 return "fault-tolerance (broadcast)";
191 case BOND_MODE_8023AD:
192 return "IEEE 802.3ad Dynamic link aggregation";
193 case BOND_MODE_TLB:
194 return "transmit load balancing";
195 case BOND_MODE_ALB:
196 return "adaptive load balancing";
197 default:
198 return "unknown";
199 }
200}
201
202/*---------------------------------- VLAN -----------------------------------*/
203
204/**
205 * bond_add_vlan - add a new vlan id on bond
206 * @bond: bond that got the notification
207 * @vlan_id: the vlan id to add
208 *
209 * Returns -ENOMEM if allocation failed.
210 */
211static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
212{
213 struct vlan_entry *vlan;
214
215 dprintk("bond: %s, vlan id %d\n",
216 (bond ? bond->dev->name: "None"), vlan_id);
217
218 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
219 if (!vlan) {
220 return -ENOMEM;
221 }
222
223 INIT_LIST_HEAD(&vlan->vlan_list);
224 vlan->vlan_id = vlan_id;
c3ade5ca 225 vlan->vlan_ip = 0;
1da177e4
LT
226
227 write_lock_bh(&bond->lock);
228
229 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
230
231 write_unlock_bh(&bond->lock);
232
233 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
234
235 return 0;
236}
237
238/**
239 * bond_del_vlan - delete a vlan id from bond
240 * @bond: bond that got the notification
241 * @vlan_id: the vlan id to delete
242 *
243 * returns -ENODEV if @vlan_id was not found in @bond.
244 */
245static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
246{
247 struct vlan_entry *vlan, *next;
248 int res = -ENODEV;
249
250 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
251
252 write_lock_bh(&bond->lock);
253
254 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
255 if (vlan->vlan_id == vlan_id) {
256 list_del(&vlan->vlan_list);
257
258 if ((bond->params.mode == BOND_MODE_TLB) ||
259 (bond->params.mode == BOND_MODE_ALB)) {
260 bond_alb_clear_vlan(bond, vlan_id);
261 }
262
263 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
264 bond->dev->name);
265
266 kfree(vlan);
267
268 if (list_empty(&bond->vlan_list) &&
269 (bond->slave_cnt == 0)) {
270 /* Last VLAN removed and no slaves, so
271 * restore block on adding VLANs. This will
272 * be removed once new slaves that are not
273 * VLAN challenged will be added.
274 */
275 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
276 }
277
278 res = 0;
279 goto out;
280 }
281 }
282
283 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
284 bond->dev->name);
285
286out:
287 write_unlock_bh(&bond->lock);
288 return res;
289}
290
291/**
292 * bond_has_challenged_slaves
293 * @bond: the bond we're working on
294 *
295 * Searches the slave list. Returns 1 if a vlan challenged slave
296 * was found, 0 otherwise.
297 *
298 * Assumes bond->lock is held.
299 */
300static int bond_has_challenged_slaves(struct bonding *bond)
301{
302 struct slave *slave;
303 int i;
304
305 bond_for_each_slave(bond, slave, i) {
306 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
307 dprintk("found VLAN challenged slave - %s\n",
308 slave->dev->name);
309 return 1;
310 }
311 }
312
313 dprintk("no VLAN challenged slaves found\n");
314 return 0;
315}
316
317/**
318 * bond_next_vlan - safely skip to the next item in the vlans list.
319 * @bond: the bond we're working on
320 * @curr: item we're advancing from
321 *
322 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
323 * or @curr->next otherwise (even if it is @curr itself again).
324 *
325 * Caller must hold bond->lock
326 */
327struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
328{
329 struct vlan_entry *next, *last;
330
331 if (list_empty(&bond->vlan_list)) {
332 return NULL;
333 }
334
335 if (!curr) {
336 next = list_entry(bond->vlan_list.next,
337 struct vlan_entry, vlan_list);
338 } else {
339 last = list_entry(bond->vlan_list.prev,
340 struct vlan_entry, vlan_list);
341 if (last == curr) {
342 next = list_entry(bond->vlan_list.next,
343 struct vlan_entry, vlan_list);
344 } else {
345 next = list_entry(curr->vlan_list.next,
346 struct vlan_entry, vlan_list);
347 }
348 }
349
350 return next;
351}
352
353/**
354 * bond_dev_queue_xmit - Prepare skb for xmit.
355 *
356 * @bond: bond device that got this skb for tx.
357 * @skb: hw accel VLAN tagged skb to transmit
358 * @slave_dev: slave that is supposed to xmit this skbuff
359 *
360 * When the bond gets an skb to transmit that is
361 * already hardware accelerated VLAN tagged, and it
362 * needs to relay this skb to a slave that is not
363 * hw accel capable, the skb needs to be "unaccelerated",
364 * i.e. strip the hwaccel tag and re-insert it as part
365 * of the payload.
366 */
367int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
368{
369 unsigned short vlan_id;
370
371 if (!list_empty(&bond->vlan_list) &&
372 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
373 vlan_get_tag(skb, &vlan_id) == 0) {
374 skb->dev = slave_dev;
375 skb = vlan_put_tag(skb, vlan_id);
376 if (!skb) {
377 /* vlan_put_tag() frees the skb in case of error,
378 * so return success here so the calling functions
379 * won't attempt to free is again.
380 */
381 return 0;
382 }
383 } else {
384 skb->dev = slave_dev;
385 }
386
387 skb->priority = 1;
388 dev_queue_xmit(skb);
389
390 return 0;
391}
392
393/*
394 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
395 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
396 * lock because:
397 * a. This operation is performed in IOCTL context,
398 * b. The operation is protected by the RTNL semaphore in the 8021q code,
399 * c. Holding a lock with BH disabled while directly calling a base driver
400 * entry point is generally a BAD idea.
401 *
402 * The design of synchronization/protection for this operation in the 8021q
403 * module is good for one or more VLAN devices over a single physical device
404 * and cannot be extended for a teaming solution like bonding, so there is a
405 * potential race condition here where a net device from the vlan group might
406 * be referenced (either by a base driver or the 8021q code) while it is being
407 * removed from the system. However, it turns out we're not making matters
408 * worse, and if it works for regular VLAN usage it will work here too.
409*/
410
411/**
412 * bond_vlan_rx_register - Propagates registration to slaves
413 * @bond_dev: bonding net device that got called
414 * @grp: vlan group being registered
415 */
416static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
417{
418 struct bonding *bond = bond_dev->priv;
419 struct slave *slave;
420 int i;
421
422 bond->vlgrp = grp;
423
424 bond_for_each_slave(bond, slave, i) {
425 struct net_device *slave_dev = slave->dev;
426
427 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
428 slave_dev->vlan_rx_register) {
429 slave_dev->vlan_rx_register(slave_dev, grp);
430 }
431 }
432}
433
434/**
435 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
436 * @bond_dev: bonding net device that got called
437 * @vid: vlan id being added
438 */
439static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
440{
441 struct bonding *bond = bond_dev->priv;
442 struct slave *slave;
443 int i, res;
444
445 bond_for_each_slave(bond, slave, i) {
446 struct net_device *slave_dev = slave->dev;
447
448 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
449 slave_dev->vlan_rx_add_vid) {
450 slave_dev->vlan_rx_add_vid(slave_dev, vid);
451 }
452 }
453
454 res = bond_add_vlan(bond, vid);
455 if (res) {
456 printk(KERN_ERR DRV_NAME
4e0952c7 457 ": %s: Error: Failed to add vlan id %d\n",
1da177e4
LT
458 bond_dev->name, vid);
459 }
460}
461
462/**
463 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
464 * @bond_dev: bonding net device that got called
465 * @vid: vlan id being removed
466 */
467static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
468{
469 struct bonding *bond = bond_dev->priv;
470 struct slave *slave;
471 struct net_device *vlan_dev;
472 int i, res;
473
474 bond_for_each_slave(bond, slave, i) {
475 struct net_device *slave_dev = slave->dev;
476
477 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
478 slave_dev->vlan_rx_kill_vid) {
479 /* Save and then restore vlan_dev in the grp array,
480 * since the slave's driver might clear it.
481 */
482 vlan_dev = bond->vlgrp->vlan_devices[vid];
483 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
484 bond->vlgrp->vlan_devices[vid] = vlan_dev;
485 }
486 }
487
488 res = bond_del_vlan(bond, vid);
489 if (res) {
490 printk(KERN_ERR DRV_NAME
4e0952c7 491 ": %s: Error: Failed to remove vlan id %d\n",
1da177e4
LT
492 bond_dev->name, vid);
493 }
494}
495
496static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
497{
498 struct vlan_entry *vlan;
499
500 write_lock_bh(&bond->lock);
501
502 if (list_empty(&bond->vlan_list)) {
503 goto out;
504 }
505
506 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
507 slave_dev->vlan_rx_register) {
508 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
509 }
510
511 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
512 !(slave_dev->vlan_rx_add_vid)) {
513 goto out;
514 }
515
516 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
517 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
518 }
519
520out:
521 write_unlock_bh(&bond->lock);
522}
523
524static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
525{
526 struct vlan_entry *vlan;
527 struct net_device *vlan_dev;
528
529 write_lock_bh(&bond->lock);
530
531 if (list_empty(&bond->vlan_list)) {
532 goto out;
533 }
534
535 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
536 !(slave_dev->vlan_rx_kill_vid)) {
537 goto unreg;
538 }
539
540 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
541 /* Save and then restore vlan_dev in the grp array,
542 * since the slave's driver might clear it.
543 */
544 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
545 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
546 bond->vlgrp->vlan_devices[vlan->vlan_id] = vlan_dev;
547 }
548
549unreg:
550 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
551 slave_dev->vlan_rx_register) {
552 slave_dev->vlan_rx_register(slave_dev, NULL);
553 }
554
555out:
556 write_unlock_bh(&bond->lock);
557}
558
559/*------------------------------- Link status -------------------------------*/
560
561/*
562 * Get link speed and duplex from the slave's base driver
563 * using ethtool. If for some reason the call fails or the
564 * values are invalid, fake speed and duplex to 100/Full
565 * and return error.
566 */
567static int bond_update_speed_duplex(struct slave *slave)
568{
569 struct net_device *slave_dev = slave->dev;
570 static int (* ioctl)(struct net_device *, struct ifreq *, int);
571 struct ifreq ifr;
572 struct ethtool_cmd etool;
573
574 /* Fake speed and duplex */
575 slave->speed = SPEED_100;
576 slave->duplex = DUPLEX_FULL;
577
578 if (slave_dev->ethtool_ops) {
6a986ce4 579 int res;
1da177e4
LT
580
581 if (!slave_dev->ethtool_ops->get_settings) {
582 return -1;
583 }
584
585 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
586 if (res < 0) {
587 return -1;
588 }
589
590 goto verify;
591 }
592
593 ioctl = slave_dev->do_ioctl;
594 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
595 etool.cmd = ETHTOOL_GSET;
596 ifr.ifr_data = (char*)&etool;
597 if (!ioctl || (IOCTL(slave_dev, &ifr, SIOCETHTOOL) < 0)) {
598 return -1;
599 }
600
601verify:
602 switch (etool.speed) {
603 case SPEED_10:
604 case SPEED_100:
605 case SPEED_1000:
606 break;
607 default:
608 return -1;
609 }
610
611 switch (etool.duplex) {
612 case DUPLEX_FULL:
613 case DUPLEX_HALF:
614 break;
615 default:
616 return -1;
617 }
618
619 slave->speed = etool.speed;
620 slave->duplex = etool.duplex;
621
622 return 0;
623}
624
625/*
626 * if <dev> supports MII link status reporting, check its link status.
627 *
628 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
629 * depening upon the setting of the use_carrier parameter.
630 *
631 * Return either BMSR_LSTATUS, meaning that the link is up (or we
632 * can't tell and just pretend it is), or 0, meaning that the link is
633 * down.
634 *
635 * If reporting is non-zero, instead of faking link up, return -1 if
636 * both ETHTOOL and MII ioctls fail (meaning the device does not
637 * support them). If use_carrier is set, return whatever it says.
638 * It'd be nice if there was a good way to tell if a driver supports
639 * netif_carrier, but there really isn't.
640 */
641static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
642{
643 static int (* ioctl)(struct net_device *, struct ifreq *, int);
644 struct ifreq ifr;
645 struct mii_ioctl_data *mii;
646 struct ethtool_value etool;
647
648 if (bond->params.use_carrier) {
649 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
650 }
651
652 ioctl = slave_dev->do_ioctl;
653 if (ioctl) {
654 /* TODO: set pointer to correct ioctl on a per team member */
655 /* bases to make this more efficient. that is, once */
656 /* we determine the correct ioctl, we will always */
657 /* call it and not the others for that team */
658 /* member. */
659
660 /*
661 * We cannot assume that SIOCGMIIPHY will also read a
662 * register; not all network drivers (e.g., e100)
663 * support that.
664 */
665
666 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
667 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
668 mii = if_mii(&ifr);
669 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
670 mii->reg_num = MII_BMSR;
671 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
672 return (mii->val_out & BMSR_LSTATUS);
673 }
674 }
675 }
676
677 /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
678 /* for a period of time so we attempt to get link status */
679 /* from it last if the above MII ioctls fail... */
680 if (slave_dev->ethtool_ops) {
681 if (slave_dev->ethtool_ops->get_link) {
682 u32 link;
683
684 link = slave_dev->ethtool_ops->get_link(slave_dev);
685
686 return link ? BMSR_LSTATUS : 0;
687 }
688 }
689
690 if (ioctl) {
691 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
692 etool.cmd = ETHTOOL_GLINK;
693 ifr.ifr_data = (char*)&etool;
694 if (IOCTL(slave_dev, &ifr, SIOCETHTOOL) == 0) {
695 if (etool.data == 1) {
696 return BMSR_LSTATUS;
697 } else {
698 dprintk("SIOCETHTOOL shows link down\n");
699 return 0;
700 }
701 }
702 }
703
704 /*
705 * If reporting, report that either there's no dev->do_ioctl,
706 * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
707 * cannot report link status). If not reporting, pretend
708 * we're ok.
709 */
710 return (reporting ? -1 : BMSR_LSTATUS);
711}
712
713/*----------------------------- Multicast list ------------------------------*/
714
715/*
716 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
717 */
718static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
719{
720 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
721 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
722}
723
724/*
725 * returns dmi entry if found, NULL otherwise
726 */
727static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
728{
729 struct dev_mc_list *idmi;
730
731 for (idmi = mc_list; idmi; idmi = idmi->next) {
732 if (bond_is_dmi_same(dmi, idmi)) {
733 return idmi;
734 }
735 }
736
737 return NULL;
738}
739
740/*
741 * Push the promiscuity flag down to appropriate slaves
742 */
743static void bond_set_promiscuity(struct bonding *bond, int inc)
744{
745 if (USES_PRIMARY(bond->params.mode)) {
746 /* write lock already acquired */
747 if (bond->curr_active_slave) {
748 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
749 }
750 } else {
751 struct slave *slave;
752 int i;
753 bond_for_each_slave(bond, slave, i) {
754 dev_set_promiscuity(slave->dev, inc);
755 }
756 }
757}
758
759/*
760 * Push the allmulti flag down to all slaves
761 */
762static void bond_set_allmulti(struct bonding *bond, int inc)
763{
764 if (USES_PRIMARY(bond->params.mode)) {
765 /* write lock already acquired */
766 if (bond->curr_active_slave) {
767 dev_set_allmulti(bond->curr_active_slave->dev, inc);
768 }
769 } else {
770 struct slave *slave;
771 int i;
772 bond_for_each_slave(bond, slave, i) {
773 dev_set_allmulti(slave->dev, inc);
774 }
775 }
776}
777
778/*
779 * Add a Multicast address to slaves
780 * according to mode
781 */
782static void bond_mc_add(struct bonding *bond, void *addr, int alen)
783{
784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
787 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
788 }
789 } else {
790 struct slave *slave;
791 int i;
792 bond_for_each_slave(bond, slave, i) {
793 dev_mc_add(slave->dev, addr, alen, 0);
794 }
795 }
796}
797
798/*
799 * Remove a multicast address from slave
800 * according to mode
801 */
802static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
803{
804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
807 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
808 }
809 } else {
810 struct slave *slave;
811 int i;
812 bond_for_each_slave(bond, slave, i) {
813 dev_mc_delete(slave->dev, addr, alen, 0);
814 }
815 }
816}
817
818/*
819 * Totally destroys the mc_list in bond
820 */
821static void bond_mc_list_destroy(struct bonding *bond)
822{
823 struct dev_mc_list *dmi;
824
825 dmi = bond->mc_list;
826 while (dmi) {
827 bond->mc_list = dmi->next;
828 kfree(dmi);
829 dmi = bond->mc_list;
830 }
831}
832
833/*
834 * Copy all the Multicast addresses from src to the bonding device dst
835 */
de54f390 836static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
dd0fc66f 837 gfp_t gfp_flag)
1da177e4
LT
838{
839 struct dev_mc_list *dmi, *new_dmi;
840
841 for (dmi = mc_list; dmi; dmi = dmi->next) {
de54f390 842 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
1da177e4
LT
843
844 if (!new_dmi) {
845 /* FIXME: Potential memory leak !!! */
846 return -ENOMEM;
847 }
848
849 new_dmi->next = bond->mc_list;
850 bond->mc_list = new_dmi;
851 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
852 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
853 new_dmi->dmi_users = dmi->dmi_users;
854 new_dmi->dmi_gusers = dmi->dmi_gusers;
855 }
856
857 return 0;
858}
859
860/*
861 * flush all members of flush->mc_list from device dev->mc_list
862 */
863static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
864{
865 struct bonding *bond = bond_dev->priv;
866 struct dev_mc_list *dmi;
867
868 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
869 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
870 }
871
872 if (bond->params.mode == BOND_MODE_8023AD) {
873 /* del lacpdu mc addr from mc list */
874 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
875
876 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
877 }
878}
879
880/*--------------------------- Active slave change ---------------------------*/
881
882/*
883 * Update the mc list and multicast-related flags for the new and
884 * old active slaves (if any) according to the multicast mode, and
885 * promiscuous flags unconditionally.
886 */
887static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
888{
889 struct dev_mc_list *dmi;
890
891 if (!USES_PRIMARY(bond->params.mode)) {
892 /* nothing to do - mc list is already up-to-date on
893 * all slaves
894 */
895 return;
896 }
897
898 if (old_active) {
899 if (bond->dev->flags & IFF_PROMISC) {
900 dev_set_promiscuity(old_active->dev, -1);
901 }
902
903 if (bond->dev->flags & IFF_ALLMULTI) {
904 dev_set_allmulti(old_active->dev, -1);
905 }
906
907 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
908 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
909 }
910 }
911
912 if (new_active) {
913 if (bond->dev->flags & IFF_PROMISC) {
914 dev_set_promiscuity(new_active->dev, 1);
915 }
916
917 if (bond->dev->flags & IFF_ALLMULTI) {
918 dev_set_allmulti(new_active->dev, 1);
919 }
920
921 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
922 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
923 }
924 }
925}
926
927/**
928 * find_best_interface - select the best available slave to be the active one
929 * @bond: our bonding struct
930 *
931 * Warning: Caller must hold curr_slave_lock for writing.
932 */
933static struct slave *bond_find_best_slave(struct bonding *bond)
934{
935 struct slave *new_active, *old_active;
936 struct slave *bestslave = NULL;
937 int mintime = bond->params.updelay;
938 int i;
939
940 new_active = old_active = bond->curr_active_slave;
941
942 if (!new_active) { /* there were no active slaves left */
943 if (bond->slave_cnt > 0) { /* found one slave */
944 new_active = bond->first_slave;
945 } else {
946 return NULL; /* still no slave, return NULL */
947 }
948 }
949
950 /* first try the primary link; if arping, a link must tx/rx traffic
951 * before it can be considered the curr_active_slave - also, we would skip
952 * slaves between the curr_active_slave and primary_slave that may be up
953 * and able to arp
954 */
955 if ((bond->primary_slave) &&
956 (!bond->params.arp_interval) &&
957 (IS_UP(bond->primary_slave->dev))) {
958 new_active = bond->primary_slave;
959 }
960
961 /* remember where to stop iterating over the slaves */
962 old_active = new_active;
963
964 bond_for_each_slave_from(bond, new_active, i, old_active) {
965 if (IS_UP(new_active->dev)) {
966 if (new_active->link == BOND_LINK_UP) {
967 return new_active;
968 } else if (new_active->link == BOND_LINK_BACK) {
969 /* link up, but waiting for stabilization */
970 if (new_active->delay < mintime) {
971 mintime = new_active->delay;
972 bestslave = new_active;
973 }
974 }
975 }
976 }
977
978 return bestslave;
979}
980
981/**
982 * change_active_interface - change the active slave into the specified one
983 * @bond: our bonding struct
984 * @new: the new slave to make the active one
985 *
986 * Set the new slave to the bond's settings and unset them on the old
987 * curr_active_slave.
988 * Setting include flags, mc-list, promiscuity, allmulti, etc.
989 *
990 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
991 * because it is apparently the best available slave we have, even though its
992 * updelay hasn't timed out yet.
993 *
994 * Warning: Caller must hold curr_slave_lock for writing.
995 */
a77b5325 996void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1da177e4
LT
997{
998 struct slave *old_active = bond->curr_active_slave;
999
1000 if (old_active == new_active) {
1001 return;
1002 }
1003
1004 if (new_active) {
1005 if (new_active->link == BOND_LINK_BACK) {
1006 if (USES_PRIMARY(bond->params.mode)) {
1007 printk(KERN_INFO DRV_NAME
1008 ": %s: making interface %s the new "
1009 "active one %d ms earlier.\n",
1010 bond->dev->name, new_active->dev->name,
1011 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1012 }
1013
1014 new_active->delay = 0;
1015 new_active->link = BOND_LINK_UP;
1016 new_active->jiffies = jiffies;
1017
1018 if (bond->params.mode == BOND_MODE_8023AD) {
1019 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1020 }
1021
1022 if ((bond->params.mode == BOND_MODE_TLB) ||
1023 (bond->params.mode == BOND_MODE_ALB)) {
1024 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1025 }
1026 } else {
1027 if (USES_PRIMARY(bond->params.mode)) {
1028 printk(KERN_INFO DRV_NAME
1029 ": %s: making interface %s the new "
1030 "active one.\n",
1031 bond->dev->name, new_active->dev->name);
1032 }
1033 }
1034 }
1035
1da177e4
LT
1036 if (USES_PRIMARY(bond->params.mode)) {
1037 bond_mc_swap(bond, new_active, old_active);
1038 }
1039
1040 if ((bond->params.mode == BOND_MODE_TLB) ||
1041 (bond->params.mode == BOND_MODE_ALB)) {
1042 bond_alb_handle_active_change(bond, new_active);
8f903c70
JV
1043 if (old_active)
1044 bond_set_slave_inactive_flags(old_active);
1045 if (new_active)
1046 bond_set_slave_active_flags(new_active);
1da177e4
LT
1047 } else {
1048 bond->curr_active_slave = new_active;
1049 }
c3ade5ca
JV
1050
1051 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1052 if (old_active) {
1053 bond_set_slave_inactive_flags(old_active);
1054 }
1055
1056 if (new_active) {
1057 bond_set_slave_active_flags(new_active);
1058 }
1059 bond_send_gratuitous_arp(bond);
1060 }
1da177e4
LT
1061}
1062
1063/**
1064 * bond_select_active_slave - select a new active slave, if needed
1065 * @bond: our bonding struct
1066 *
1067 * This functions shoud be called when one of the following occurs:
1068 * - The old curr_active_slave has been released or lost its link.
1069 * - The primary_slave has got its link back.
1070 * - A slave has got its link back and there's no old curr_active_slave.
1071 *
1072 * Warning: Caller must hold curr_slave_lock for writing.
1073 */
a77b5325 1074void bond_select_active_slave(struct bonding *bond)
1da177e4
LT
1075{
1076 struct slave *best_slave;
1077
1078 best_slave = bond_find_best_slave(bond);
1079 if (best_slave != bond->curr_active_slave) {
1080 bond_change_active_slave(bond, best_slave);
1081 }
1082}
1083
1084/*--------------------------- slave list handling ---------------------------*/
1085
1086/*
1087 * This function attaches the slave to the end of list.
1088 *
1089 * bond->lock held for writing by caller.
1090 */
1091static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1092{
1093 if (bond->first_slave == NULL) { /* attaching the first slave */
1094 new_slave->next = new_slave;
1095 new_slave->prev = new_slave;
1096 bond->first_slave = new_slave;
1097 } else {
1098 new_slave->next = bond->first_slave;
1099 new_slave->prev = bond->first_slave->prev;
1100 new_slave->next->prev = new_slave;
1101 new_slave->prev->next = new_slave;
1102 }
1103
1104 bond->slave_cnt++;
1105}
1106
1107/*
1108 * This function detaches the slave from the list.
1109 * WARNING: no check is made to verify if the slave effectively
1110 * belongs to <bond>.
1111 * Nothing is freed on return, structures are just unchained.
1112 * If any slave pointer in bond was pointing to <slave>,
1113 * it should be changed by the calling function.
1114 *
1115 * bond->lock held for writing by caller.
1116 */
1117static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1118{
1119 if (slave->next) {
1120 slave->next->prev = slave->prev;
1121 }
1122
1123 if (slave->prev) {
1124 slave->prev->next = slave->next;
1125 }
1126
1127 if (bond->first_slave == slave) { /* slave is the first slave */
1128 if (bond->slave_cnt > 1) { /* there are more slave */
1129 bond->first_slave = slave->next;
1130 } else {
1131 bond->first_slave = NULL; /* slave was the last one */
1132 }
1133 }
1134
1135 slave->next = NULL;
1136 slave->prev = NULL;
1137 bond->slave_cnt--;
1138}
1139
1140/*---------------------------------- IOCTL ----------------------------------*/
1141
a77b5325 1142int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1143{
1144 dprintk("bond_dev=%p\n", bond_dev);
1145 dprintk("slave_dev=%p\n", slave_dev);
1146 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1147 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1148 return 0;
1149}
1150
8531c5ff 1151#define BOND_INTERSECT_FEATURES \
a0de3adf
JV
1152 (NETIF_F_SG|NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM|\
1153 NETIF_F_TSO|NETIF_F_UFO)
8531c5ff
AK
1154
1155/*
8e3babcd
JV
1156 * Compute the common dev->feature set available to all slaves. Some
1157 * feature bits are managed elsewhere, so preserve feature bits set on
1158 * master device that are not part of the examined set.
8531c5ff
AK
1159 */
1160static int bond_compute_features(struct bonding *bond)
1161{
8e3babcd 1162 unsigned long features = BOND_INTERSECT_FEATURES;
8531c5ff
AK
1163 struct slave *slave;
1164 struct net_device *bond_dev = bond->dev;
8e3babcd 1165 int i;
8531c5ff 1166
8e3babcd
JV
1167 bond_for_each_slave(bond, slave, i)
1168 features &= (slave->dev->features & BOND_INTERSECT_FEATURES);
8531c5ff 1169
8531c5ff 1170 if ((features & NETIF_F_SG) &&
8e3babcd
JV
1171 !(features & (NETIF_F_IP_CSUM |
1172 NETIF_F_NO_CSUM |
1173 NETIF_F_HW_CSUM)))
8531c5ff 1174 features &= ~NETIF_F_SG;
8531c5ff 1175
a0de3adf
JV
1176 /*
1177 * features will include NETIF_F_TSO (NETIF_F_UFO) iff all
1178 * slave devices support NETIF_F_TSO (NETIF_F_UFO), which
1179 * implies that all slaves also support scatter-gather
1180 * (NETIF_F_SG), which implies that features also includes
1181 * NETIF_F_SG. So no need to check whether we have an
1182 * illegal combination of NETIF_F_{TSO,UFO} and
1183 * !NETIF_F_SG
1184 */
1185
8e3babcd 1186 features |= (bond_dev->features & ~BOND_INTERSECT_FEATURES);
8531c5ff
AK
1187 bond_dev->features = features;
1188
1189 return 0;
1190}
1191
1da177e4 1192/* enslave device <slave> to bond device <master> */
a77b5325 1193int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1194{
1195 struct bonding *bond = bond_dev->priv;
1196 struct slave *new_slave = NULL;
1197 struct dev_mc_list *dmi;
1198 struct sockaddr addr;
1199 int link_reporting;
1200 int old_features = bond_dev->features;
1201 int res = 0;
1202
552709d5 1203 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1204 slave_dev->do_ioctl == NULL) {
1da177e4 1205 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1206 ": %s: Warning: no link monitoring support for %s\n",
1207 bond_dev->name, slave_dev->name);
1da177e4
LT
1208 }
1209
1210 /* bond must be initialized by bond_open() before enslaving */
1211 if (!(bond_dev->flags & IFF_UP)) {
1212 dprintk("Error, master_dev is not up\n");
1213 return -EPERM;
1214 }
1215
1216 /* already enslaved */
1217 if (slave_dev->flags & IFF_SLAVE) {
1218 dprintk("Error, Device was already enslaved\n");
1219 return -EBUSY;
1220 }
1221
1222 /* vlan challenged mutual exclusion */
1223 /* no need to lock since we're protected by rtnl_lock */
1224 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1225 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1226 if (!list_empty(&bond->vlan_list)) {
1227 printk(KERN_ERR DRV_NAME
4e0952c7 1228 ": %s: Error: cannot enslave VLAN "
1da177e4 1229 "challenged slave %s on VLAN enabled "
4e0952c7 1230 "bond %s\n", bond_dev->name, slave_dev->name,
1da177e4
LT
1231 bond_dev->name);
1232 return -EPERM;
1233 } else {
1234 printk(KERN_WARNING DRV_NAME
4e0952c7 1235 ": %s: Warning: enslaved VLAN challenged "
1da177e4
LT
1236 "slave %s. Adding VLANs will be blocked as "
1237 "long as %s is part of bond %s\n",
4e0952c7 1238 bond_dev->name, slave_dev->name, slave_dev->name,
1da177e4
LT
1239 bond_dev->name);
1240 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1241 }
1242 } else {
1243 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1244 if (bond->slave_cnt == 0) {
1245 /* First slave, and it is not VLAN challenged,
1246 * so remove the block of adding VLANs over the bond.
1247 */
1248 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1249 }
1250 }
1251
217df670
JV
1252 /*
1253 * Old ifenslave binaries are no longer supported. These can
1254 * be identified with moderate accurary by the state of the slave:
1255 * the current ifenslave will set the interface down prior to
1256 * enslaving it; the old ifenslave will not.
1257 */
1258 if ((slave_dev->flags & IFF_UP)) {
1259 printk(KERN_ERR DRV_NAME ": %s is up. "
1260 "This may be due to an out of date ifenslave.\n",
1261 slave_dev->name);
1262 res = -EPERM;
1263 goto err_undo_flags;
1264 }
1da177e4 1265
217df670
JV
1266 if (slave_dev->set_mac_address == NULL) {
1267 printk(KERN_ERR DRV_NAME
4e0952c7
MW
1268 ": %s: Error: The slave device you specified does "
1269 "not support setting the MAC address. "
1270 "Your kernel likely does not support slave "
1271 "devices.\n", bond_dev->name);
1272 res = -EOPNOTSUPP;
217df670 1273 goto err_undo_flags;
1da177e4
LT
1274 }
1275
1276 new_slave = kmalloc(sizeof(struct slave), GFP_KERNEL);
1277 if (!new_slave) {
1278 res = -ENOMEM;
1279 goto err_undo_flags;
1280 }
1281
1282 memset(new_slave, 0, sizeof(struct slave));
1283
1284 /* save slave's original flags before calling
1285 * netdev_set_master and dev_open
1286 */
1287 new_slave->original_flags = slave_dev->flags;
1288
217df670
JV
1289 /*
1290 * Save slave's original ("permanent") mac address for modes
1291 * that need it, and for restoring it upon release, and then
1292 * set it to the master's address
1293 */
1294 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1da177e4 1295
217df670
JV
1296 /*
1297 * Set slave to master's mac address. The application already
1298 * set the master's mac address to that of the first slave
1299 */
1300 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1301 addr.sa_family = slave_dev->type;
1302 res = dev_set_mac_address(slave_dev, &addr);
1303 if (res) {
1304 dprintk("Error %d calling set_mac_address\n", res);
1305 goto err_free;
1306 }
1da177e4 1307
217df670
JV
1308 /* open the slave since the application closed it */
1309 res = dev_open(slave_dev);
1310 if (res) {
1311 dprintk("Openning slave %s failed\n", slave_dev->name);
1312 goto err_restore_mac;
1da177e4
LT
1313 }
1314
1315 res = netdev_set_master(slave_dev, bond_dev);
1316 if (res) {
1317 dprintk("Error %d calling netdev_set_master\n", res);
217df670 1318 goto err_close;
1da177e4
LT
1319 }
1320
1321 new_slave->dev = slave_dev;
1322
1323 if ((bond->params.mode == BOND_MODE_TLB) ||
1324 (bond->params.mode == BOND_MODE_ALB)) {
1325 /* bond_alb_init_slave() must be called before all other stages since
1326 * it might fail and we do not want to have to undo everything
1327 */
1328 res = bond_alb_init_slave(bond, new_slave);
1329 if (res) {
1330 goto err_unset_master;
1331 }
1332 }
1333
1334 /* If the mode USES_PRIMARY, then the new slave gets the
1335 * master's promisc (and mc) settings only if it becomes the
1336 * curr_active_slave, and that is taken care of later when calling
1337 * bond_change_active()
1338 */
1339 if (!USES_PRIMARY(bond->params.mode)) {
1340 /* set promiscuity level to new slave */
1341 if (bond_dev->flags & IFF_PROMISC) {
1342 dev_set_promiscuity(slave_dev, 1);
1343 }
1344
1345 /* set allmulti level to new slave */
1346 if (bond_dev->flags & IFF_ALLMULTI) {
1347 dev_set_allmulti(slave_dev, 1);
1348 }
1349
1350 /* upload master's mc_list to new slave */
1351 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1352 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1353 }
1354 }
1355
1356 if (bond->params.mode == BOND_MODE_8023AD) {
1357 /* add lacpdu mc addr to mc list */
1358 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1359
1360 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1361 }
1362
1363 bond_add_vlans_on_slave(bond, slave_dev);
1364
1365 write_lock_bh(&bond->lock);
1366
1367 bond_attach_slave(bond, new_slave);
1368
1369 new_slave->delay = 0;
1370 new_slave->link_failure_count = 0;
1371
8531c5ff
AK
1372 bond_compute_features(bond);
1373
1da177e4
LT
1374 if (bond->params.miimon && !bond->params.use_carrier) {
1375 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1376
1377 if ((link_reporting == -1) && !bond->params.arp_interval) {
1378 /*
1379 * miimon is set but a bonded network driver
1380 * does not support ETHTOOL/MII and
1381 * arp_interval is not set. Note: if
1382 * use_carrier is enabled, we will never go
1383 * here (because netif_carrier is always
1384 * supported); thus, we don't need to change
1385 * the messages for netif_carrier.
1386 */
1387 printk(KERN_WARNING DRV_NAME
4e0952c7 1388 ": %s: Warning: MII and ETHTOOL support not "
1da177e4
LT
1389 "available for interface %s, and "
1390 "arp_interval/arp_ip_target module parameters "
1391 "not specified, thus bonding will not detect "
1392 "link failures! see bonding.txt for details.\n",
4e0952c7 1393 bond_dev->name, slave_dev->name);
1da177e4
LT
1394 } else if (link_reporting == -1) {
1395 /* unable get link status using mii/ethtool */
1396 printk(KERN_WARNING DRV_NAME
4e0952c7 1397 ": %s: Warning: can't get link status from "
1da177e4
LT
1398 "interface %s; the network driver associated "
1399 "with this interface does not support MII or "
1400 "ETHTOOL link status reporting, thus miimon "
1401 "has no effect on this interface.\n",
4e0952c7 1402 bond_dev->name, slave_dev->name);
1da177e4
LT
1403 }
1404 }
1405
1406 /* check for initial state */
1407 if (!bond->params.miimon ||
1408 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1409 if (bond->params.updelay) {
1410 dprintk("Initial state of slave_dev is "
1411 "BOND_LINK_BACK\n");
1412 new_slave->link = BOND_LINK_BACK;
1413 new_slave->delay = bond->params.updelay;
1414 } else {
1415 dprintk("Initial state of slave_dev is "
1416 "BOND_LINK_UP\n");
1417 new_slave->link = BOND_LINK_UP;
1418 }
1419 new_slave->jiffies = jiffies;
1420 } else {
1421 dprintk("Initial state of slave_dev is "
1422 "BOND_LINK_DOWN\n");
1423 new_slave->link = BOND_LINK_DOWN;
1424 }
1425
1426 if (bond_update_speed_duplex(new_slave) &&
1427 (new_slave->link != BOND_LINK_DOWN)) {
1428 printk(KERN_WARNING DRV_NAME
4e0952c7 1429 ": %s: Warning: failed to get speed and duplex from %s, "
1da177e4 1430 "assumed to be 100Mb/sec and Full.\n",
4e0952c7 1431 bond_dev->name, new_slave->dev->name);
1da177e4
LT
1432
1433 if (bond->params.mode == BOND_MODE_8023AD) {
4e0952c7
MW
1434 printk(KERN_WARNING DRV_NAME
1435 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1da177e4 1436 "support in base driver for proper aggregator "
4e0952c7 1437 "selection.\n", bond_dev->name);
1da177e4
LT
1438 }
1439 }
1440
1441 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1442 /* if there is a primary slave, remember it */
1443 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1444 bond->primary_slave = new_slave;
1445 }
1446 }
1447
1448 switch (bond->params.mode) {
1449 case BOND_MODE_ACTIVEBACKUP:
8f903c70
JV
1450 /* if we're in active-backup mode, we need one and
1451 * only one active interface. The backup interfaces
1452 * will have their SLAVE_INACTIVE flag set because we
1453 * need them to be drop all packets. Thus, since we
1454 * guarantee that curr_active_slave always point to
1455 * the last usable interface, we just have to verify
1456 * this interface's flag.
1da177e4
LT
1457 */
1458 if (((!bond->curr_active_slave) ||
8f903c70 1459 (bond->curr_active_slave->dev->priv_flags & IFF_SLAVE_INACTIVE)) &&
1da177e4
LT
1460 (new_slave->link != BOND_LINK_DOWN)) {
1461 dprintk("This is the first active slave\n");
1462 /* first slave or no active slave yet, and this link
1463 is OK, so make this interface the active one */
1464 bond_change_active_slave(bond, new_slave);
1465 } else {
1466 dprintk("This is just a backup slave\n");
1467 bond_set_slave_inactive_flags(new_slave);
1468 }
1469 break;
1470 case BOND_MODE_8023AD:
1471 /* in 802.3ad mode, the internal mechanism
1472 * will activate the slaves in the selected
1473 * aggregator
1474 */
1475 bond_set_slave_inactive_flags(new_slave);
1476 /* if this is the first slave */
1477 if (bond->slave_cnt == 1) {
1478 SLAVE_AD_INFO(new_slave).id = 1;
1479 /* Initialize AD with the number of times that the AD timer is called in 1 second
1480 * can be called only after the mac address of the bond is set
1481 */
1482 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1483 bond->params.lacp_fast);
1484 } else {
1485 SLAVE_AD_INFO(new_slave).id =
1486 SLAVE_AD_INFO(new_slave->prev).id + 1;
1487 }
1488
1489 bond_3ad_bind_slave(new_slave);
1490 break;
1491 case BOND_MODE_TLB:
1492 case BOND_MODE_ALB:
1493 new_slave->state = BOND_STATE_ACTIVE;
1494 if ((!bond->curr_active_slave) &&
1495 (new_slave->link != BOND_LINK_DOWN)) {
1496 /* first slave or no active slave yet, and this link
1497 * is OK, so make this interface the active one
1498 */
1499 bond_change_active_slave(bond, new_slave);
8f903c70
JV
1500 } else {
1501 bond_set_slave_inactive_flags(new_slave);
1da177e4
LT
1502 }
1503 break;
1504 default:
1505 dprintk("This slave is always active in trunk mode\n");
1506
1507 /* always active in trunk mode */
1508 new_slave->state = BOND_STATE_ACTIVE;
1509
1510 /* In trunking mode there is little meaning to curr_active_slave
1511 * anyway (it holds no special properties of the bond device),
1512 * so we can change it without calling change_active_interface()
1513 */
1514 if (!bond->curr_active_slave) {
1515 bond->curr_active_slave = new_slave;
1516 }
1517 break;
1518 } /* switch(bond_mode) */
1519
1520 write_unlock_bh(&bond->lock);
1521
b76cdba9
MW
1522 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1523 if (res)
1524 goto err_unset_master;
1525
1da177e4
LT
1526 printk(KERN_INFO DRV_NAME
1527 ": %s: enslaving %s as a%s interface with a%s link.\n",
1528 bond_dev->name, slave_dev->name,
1529 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1530 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1531
1532 /* enslave is successful */
1533 return 0;
1534
1535/* Undo stages on error */
1536err_unset_master:
1537 netdev_set_master(slave_dev, NULL);
1538
1539err_close:
1540 dev_close(slave_dev);
1541
1542err_restore_mac:
1543 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1544 addr.sa_family = slave_dev->type;
1545 dev_set_mac_address(slave_dev, &addr);
1546
1547err_free:
1548 kfree(new_slave);
1549
1550err_undo_flags:
1551 bond_dev->features = old_features;
8531c5ff 1552
1da177e4
LT
1553 return res;
1554}
1555
1556/*
1557 * Try to release the slave device <slave> from the bond device <master>
1558 * It is legal to access curr_active_slave without a lock because all the function
1559 * is write-locked.
1560 *
1561 * The rules for slave state should be:
1562 * for Active/Backup:
1563 * Active stays on all backups go down
1564 * for Bonded connections:
1565 * The first up interface should be left on and all others downed.
1566 */
a77b5325 1567int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1568{
1569 struct bonding *bond = bond_dev->priv;
1570 struct slave *slave, *oldcurrent;
1571 struct sockaddr addr;
1572 int mac_addr_differ;
1573
1574 /* slave is not a slave or master is not master of this slave */
1575 if (!(slave_dev->flags & IFF_SLAVE) ||
1576 (slave_dev->master != bond_dev)) {
1577 printk(KERN_ERR DRV_NAME
4e0952c7 1578 ": %s: Error: cannot release %s.\n",
1da177e4
LT
1579 bond_dev->name, slave_dev->name);
1580 return -EINVAL;
1581 }
1582
1583 write_lock_bh(&bond->lock);
1584
1585 slave = bond_get_slave_by_dev(bond, slave_dev);
1586 if (!slave) {
1587 /* not a slave of this bond */
1588 printk(KERN_INFO DRV_NAME
1589 ": %s: %s not enslaved\n",
1590 bond_dev->name, slave_dev->name);
f5e2a7b2 1591 write_unlock_bh(&bond->lock);
1da177e4
LT
1592 return -EINVAL;
1593 }
1594
1595 mac_addr_differ = memcmp(bond_dev->dev_addr,
1596 slave->perm_hwaddr,
1597 ETH_ALEN);
1598 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1599 printk(KERN_WARNING DRV_NAME
4e0952c7 1600 ": %s: Warning: the permanent HWaddr of %s "
1da177e4
LT
1601 "- %02X:%02X:%02X:%02X:%02X:%02X - is "
1602 "still in use by %s. Set the HWaddr of "
1603 "%s to a different address to avoid "
1604 "conflicts.\n",
4e0952c7 1605 bond_dev->name,
1da177e4
LT
1606 slave_dev->name,
1607 slave->perm_hwaddr[0],
1608 slave->perm_hwaddr[1],
1609 slave->perm_hwaddr[2],
1610 slave->perm_hwaddr[3],
1611 slave->perm_hwaddr[4],
1612 slave->perm_hwaddr[5],
1613 bond_dev->name,
1614 slave_dev->name);
1615 }
1616
1617 /* Inform AD package of unbinding of slave. */
1618 if (bond->params.mode == BOND_MODE_8023AD) {
1619 /* must be called before the slave is
1620 * detached from the list
1621 */
1622 bond_3ad_unbind_slave(slave);
1623 }
1624
1625 printk(KERN_INFO DRV_NAME
1626 ": %s: releasing %s interface %s\n",
1627 bond_dev->name,
1628 (slave->state == BOND_STATE_ACTIVE)
1629 ? "active" : "backup",
1630 slave_dev->name);
1631
1632 oldcurrent = bond->curr_active_slave;
1633
1634 bond->current_arp_slave = NULL;
1635
1636 /* release the slave from its bond */
1637 bond_detach_slave(bond, slave);
1638
8531c5ff
AK
1639 bond_compute_features(bond);
1640
1da177e4
LT
1641 if (bond->primary_slave == slave) {
1642 bond->primary_slave = NULL;
1643 }
1644
1645 if (oldcurrent == slave) {
1646 bond_change_active_slave(bond, NULL);
1647 }
1648
1649 if ((bond->params.mode == BOND_MODE_TLB) ||
1650 (bond->params.mode == BOND_MODE_ALB)) {
1651 /* Must be called only after the slave has been
1652 * detached from the list and the curr_active_slave
1653 * has been cleared (if our_slave == old_current),
1654 * but before a new active slave is selected.
1655 */
1656 bond_alb_deinit_slave(bond, slave);
1657 }
1658
1659 if (oldcurrent == slave) {
1660 bond_select_active_slave(bond);
1661
1662 if (!bond->curr_active_slave) {
1663 printk(KERN_INFO DRV_NAME
1664 ": %s: now running without any active "
1665 "interface !\n",
1666 bond_dev->name);
1667 }
1668 }
1669
1670 if (bond->slave_cnt == 0) {
1671 /* if the last slave was removed, zero the mac address
1672 * of the master so it will be set by the application
1673 * to the mac address of the first slave
1674 */
1675 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1676
1677 if (list_empty(&bond->vlan_list)) {
1678 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1679 } else {
1680 printk(KERN_WARNING DRV_NAME
4e0952c7 1681 ": %s: Warning: clearing HW address of %s while it "
1da177e4 1682 "still has VLANs.\n",
4e0952c7 1683 bond_dev->name, bond_dev->name);
1da177e4 1684 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1685 ": %s: When re-adding slaves, make sure the bond's "
1686 "HW address matches its VLANs'.\n",
1687 bond_dev->name);
1da177e4
LT
1688 }
1689 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1690 !bond_has_challenged_slaves(bond)) {
1691 printk(KERN_INFO DRV_NAME
4e0952c7 1692 ": %s: last VLAN challenged slave %s "
1da177e4 1693 "left bond %s. VLAN blocking is removed\n",
4e0952c7 1694 bond_dev->name, slave_dev->name, bond_dev->name);
1da177e4
LT
1695 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1696 }
1697
1698 write_unlock_bh(&bond->lock);
1699
b76cdba9
MW
1700 /* must do this from outside any spinlocks */
1701 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1702
1da177e4
LT
1703 bond_del_vlans_from_slave(bond, slave_dev);
1704
1705 /* If the mode USES_PRIMARY, then we should only remove its
1706 * promisc and mc settings if it was the curr_active_slave, but that was
1707 * already taken care of above when we detached the slave
1708 */
1709 if (!USES_PRIMARY(bond->params.mode)) {
1710 /* unset promiscuity level from slave */
1711 if (bond_dev->flags & IFF_PROMISC) {
1712 dev_set_promiscuity(slave_dev, -1);
1713 }
1714
1715 /* unset allmulti level from slave */
1716 if (bond_dev->flags & IFF_ALLMULTI) {
1717 dev_set_allmulti(slave_dev, -1);
1718 }
1719
1720 /* flush master's mc_list from slave */
1721 bond_mc_list_flush(bond_dev, slave_dev);
1722 }
1723
1724 netdev_set_master(slave_dev, NULL);
1725
1726 /* close slave before restoring its mac address */
1727 dev_close(slave_dev);
1728
217df670
JV
1729 /* restore original ("permanent") mac address */
1730 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1731 addr.sa_family = slave_dev->type;
1732 dev_set_mac_address(slave_dev, &addr);
1da177e4 1733
8f903c70
JV
1734 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1735 IFF_SLAVE_INACTIVE);
1da177e4
LT
1736
1737 kfree(slave);
1738
1739 return 0; /* deletion OK */
1740}
1741
1742/*
1743 * This function releases all slaves.
1744 */
1745static int bond_release_all(struct net_device *bond_dev)
1746{
1747 struct bonding *bond = bond_dev->priv;
1748 struct slave *slave;
1749 struct net_device *slave_dev;
1750 struct sockaddr addr;
1751
1752 write_lock_bh(&bond->lock);
1753
1754 if (bond->slave_cnt == 0) {
1755 goto out;
1756 }
1757
1758 bond->current_arp_slave = NULL;
1759 bond->primary_slave = NULL;
1760 bond_change_active_slave(bond, NULL);
1761
1762 while ((slave = bond->first_slave) != NULL) {
1763 /* Inform AD package of unbinding of slave
1764 * before slave is detached from the list.
1765 */
1766 if (bond->params.mode == BOND_MODE_8023AD) {
1767 bond_3ad_unbind_slave(slave);
1768 }
1769
1770 slave_dev = slave->dev;
1771 bond_detach_slave(bond, slave);
1772
1773 if ((bond->params.mode == BOND_MODE_TLB) ||
1774 (bond->params.mode == BOND_MODE_ALB)) {
1775 /* must be called only after the slave
1776 * has been detached from the list
1777 */
1778 bond_alb_deinit_slave(bond, slave);
1779 }
1780
8531c5ff
AK
1781 bond_compute_features(bond);
1782
1da177e4
LT
1783 /* now that the slave is detached, unlock and perform
1784 * all the undo steps that should not be called from
1785 * within a lock.
1786 */
1787 write_unlock_bh(&bond->lock);
1788
b76cdba9 1789 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1da177e4
LT
1790 bond_del_vlans_from_slave(bond, slave_dev);
1791
1792 /* If the mode USES_PRIMARY, then we should only remove its
1793 * promisc and mc settings if it was the curr_active_slave, but that was
1794 * already taken care of above when we detached the slave
1795 */
1796 if (!USES_PRIMARY(bond->params.mode)) {
1797 /* unset promiscuity level from slave */
1798 if (bond_dev->flags & IFF_PROMISC) {
1799 dev_set_promiscuity(slave_dev, -1);
1800 }
1801
1802 /* unset allmulti level from slave */
1803 if (bond_dev->flags & IFF_ALLMULTI) {
1804 dev_set_allmulti(slave_dev, -1);
1805 }
1806
1807 /* flush master's mc_list from slave */
1808 bond_mc_list_flush(bond_dev, slave_dev);
1809 }
1810
1811 netdev_set_master(slave_dev, NULL);
1812
1813 /* close slave before restoring its mac address */
1814 dev_close(slave_dev);
1815
217df670
JV
1816 /* restore original ("permanent") mac address*/
1817 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1818 addr.sa_family = slave_dev->type;
1819 dev_set_mac_address(slave_dev, &addr);
1da177e4 1820
8f903c70
JV
1821 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1822 IFF_SLAVE_INACTIVE);
1da177e4
LT
1823
1824 kfree(slave);
1825
1826 /* re-acquire the lock before getting the next slave */
1827 write_lock_bh(&bond->lock);
1828 }
1829
1830 /* zero the mac address of the master so it will be
1831 * set by the application to the mac address of the
1832 * first slave
1833 */
1834 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1835
1836 if (list_empty(&bond->vlan_list)) {
1837 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1838 } else {
1839 printk(KERN_WARNING DRV_NAME
4e0952c7 1840 ": %s: Warning: clearing HW address of %s while it "
1da177e4 1841 "still has VLANs.\n",
4e0952c7 1842 bond_dev->name, bond_dev->name);
1da177e4 1843 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1844 ": %s: When re-adding slaves, make sure the bond's "
1845 "HW address matches its VLANs'.\n",
1846 bond_dev->name);
1da177e4
LT
1847 }
1848
1849 printk(KERN_INFO DRV_NAME
1850 ": %s: released all slaves\n",
1851 bond_dev->name);
1852
1853out:
1854 write_unlock_bh(&bond->lock);
1855
1856 return 0;
1857}
1858
1859/*
1860 * This function changes the active slave to slave <slave_dev>.
1861 * It returns -EINVAL in the following cases.
1862 * - <slave_dev> is not found in the list.
1863 * - There is not active slave now.
1864 * - <slave_dev> is already active.
1865 * - The link state of <slave_dev> is not BOND_LINK_UP.
1866 * - <slave_dev> is not running.
1867 * In these cases, this fuction does nothing.
1868 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1869 */
1870static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1871{
1872 struct bonding *bond = bond_dev->priv;
1873 struct slave *old_active = NULL;
1874 struct slave *new_active = NULL;
1875 int res = 0;
1876
1877 if (!USES_PRIMARY(bond->params.mode)) {
1878 return -EINVAL;
1879 }
1880
1881 /* Verify that master_dev is indeed the master of slave_dev */
1882 if (!(slave_dev->flags & IFF_SLAVE) ||
1883 (slave_dev->master != bond_dev)) {
1884 return -EINVAL;
1885 }
1886
1887 write_lock_bh(&bond->lock);
1888
1889 old_active = bond->curr_active_slave;
1890 new_active = bond_get_slave_by_dev(bond, slave_dev);
1891
1892 /*
1893 * Changing to the current active: do nothing; return success.
1894 */
1895 if (new_active && (new_active == old_active)) {
1896 write_unlock_bh(&bond->lock);
1897 return 0;
1898 }
1899
1900 if ((new_active) &&
1901 (old_active) &&
1902 (new_active->link == BOND_LINK_UP) &&
1903 IS_UP(new_active->dev)) {
1904 bond_change_active_slave(bond, new_active);
1905 } else {
1906 res = -EINVAL;
1907 }
1908
1909 write_unlock_bh(&bond->lock);
1910
1911 return res;
1912}
1913
1da177e4
LT
1914static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1915{
1916 struct bonding *bond = bond_dev->priv;
1917
1918 info->bond_mode = bond->params.mode;
1919 info->miimon = bond->params.miimon;
1920
1921 read_lock_bh(&bond->lock);
1922 info->num_slaves = bond->slave_cnt;
1923 read_unlock_bh(&bond->lock);
1924
1925 return 0;
1926}
1927
1928static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
1929{
1930 struct bonding *bond = bond_dev->priv;
1931 struct slave *slave;
1932 int i, found = 0;
1933
1934 if (info->slave_id < 0) {
1935 return -ENODEV;
1936 }
1937
1938 read_lock_bh(&bond->lock);
1939
1940 bond_for_each_slave(bond, slave, i) {
1941 if (i == (int)info->slave_id) {
1942 found = 1;
1943 break;
1944 }
1945 }
1946
1947 read_unlock_bh(&bond->lock);
1948
1949 if (found) {
1950 strcpy(info->slave_name, slave->dev->name);
1951 info->link = slave->link;
1952 info->state = slave->state;
1953 info->link_failure_count = slave->link_failure_count;
1954 } else {
1955 return -ENODEV;
1956 }
1957
1958 return 0;
1959}
1960
1961/*-------------------------------- Monitoring -------------------------------*/
1962
1963/* this function is called regularly to monitor each slave's link. */
a77b5325 1964void bond_mii_monitor(struct net_device *bond_dev)
1da177e4
LT
1965{
1966 struct bonding *bond = bond_dev->priv;
1967 struct slave *slave, *oldcurrent;
1968 int do_failover = 0;
1969 int delta_in_ticks;
1970 int i;
1971
1972 read_lock(&bond->lock);
1973
1974 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
1975
1976 if (bond->kill_timers) {
1977 goto out;
1978 }
1979
1980 if (bond->slave_cnt == 0) {
1981 goto re_arm;
1982 }
1983
1984 /* we will try to read the link status of each of our slaves, and
1985 * set their IFF_RUNNING flag appropriately. For each slave not
1986 * supporting MII status, we won't do anything so that a user-space
1987 * program could monitor the link itself if needed.
1988 */
1989
1990 read_lock(&bond->curr_slave_lock);
1991 oldcurrent = bond->curr_active_slave;
1992 read_unlock(&bond->curr_slave_lock);
1993
1994 bond_for_each_slave(bond, slave, i) {
1995 struct net_device *slave_dev = slave->dev;
1996 int link_state;
1997 u16 old_speed = slave->speed;
1998 u8 old_duplex = slave->duplex;
1999
2000 link_state = bond_check_dev_link(bond, slave_dev, 0);
2001
2002 switch (slave->link) {
2003 case BOND_LINK_UP: /* the link was up */
2004 if (link_state == BMSR_LSTATUS) {
2005 /* link stays up, nothing more to do */
2006 break;
2007 } else { /* link going down */
2008 slave->link = BOND_LINK_FAIL;
2009 slave->delay = bond->params.downdelay;
2010
2011 if (slave->link_failure_count < UINT_MAX) {
2012 slave->link_failure_count++;
2013 }
2014
2015 if (bond->params.downdelay) {
2016 printk(KERN_INFO DRV_NAME
2017 ": %s: link status down for %s "
2018 "interface %s, disabling it in "
2019 "%d ms.\n",
2020 bond_dev->name,
2021 IS_UP(slave_dev)
2022 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2023 ? ((slave == oldcurrent)
2024 ? "active " : "backup ")
2025 : "")
2026 : "idle ",
2027 slave_dev->name,
2028 bond->params.downdelay * bond->params.miimon);
2029 }
2030 }
2031 /* no break ! fall through the BOND_LINK_FAIL test to
2032 ensure proper action to be taken
2033 */
2034 case BOND_LINK_FAIL: /* the link has just gone down */
2035 if (link_state != BMSR_LSTATUS) {
2036 /* link stays down */
2037 if (slave->delay <= 0) {
2038 /* link down for too long time */
2039 slave->link = BOND_LINK_DOWN;
2040
2041 /* in active/backup mode, we must
2042 * completely disable this interface
2043 */
2044 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2045 (bond->params.mode == BOND_MODE_8023AD)) {
2046 bond_set_slave_inactive_flags(slave);
2047 }
2048
2049 printk(KERN_INFO DRV_NAME
2050 ": %s: link status definitely "
2051 "down for interface %s, "
2052 "disabling it\n",
2053 bond_dev->name,
2054 slave_dev->name);
2055
2056 /* notify ad that the link status has changed */
2057 if (bond->params.mode == BOND_MODE_8023AD) {
2058 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2059 }
2060
2061 if ((bond->params.mode == BOND_MODE_TLB) ||
2062 (bond->params.mode == BOND_MODE_ALB)) {
2063 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2064 }
2065
2066 if (slave == oldcurrent) {
2067 do_failover = 1;
2068 }
2069 } else {
2070 slave->delay--;
2071 }
2072 } else {
2073 /* link up again */
2074 slave->link = BOND_LINK_UP;
2075 slave->jiffies = jiffies;
2076 printk(KERN_INFO DRV_NAME
2077 ": %s: link status up again after %d "
2078 "ms for interface %s.\n",
2079 bond_dev->name,
2080 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2081 slave_dev->name);
2082 }
2083 break;
2084 case BOND_LINK_DOWN: /* the link was down */
2085 if (link_state != BMSR_LSTATUS) {
2086 /* the link stays down, nothing more to do */
2087 break;
2088 } else { /* link going up */
2089 slave->link = BOND_LINK_BACK;
2090 slave->delay = bond->params.updelay;
2091
2092 if (bond->params.updelay) {
2093 /* if updelay == 0, no need to
2094 advertise about a 0 ms delay */
2095 printk(KERN_INFO DRV_NAME
2096 ": %s: link status up for "
2097 "interface %s, enabling it "
2098 "in %d ms.\n",
2099 bond_dev->name,
2100 slave_dev->name,
2101 bond->params.updelay * bond->params.miimon);
2102 }
2103 }
2104 /* no break ! fall through the BOND_LINK_BACK state in
2105 case there's something to do.
2106 */
2107 case BOND_LINK_BACK: /* the link has just come back */
2108 if (link_state != BMSR_LSTATUS) {
2109 /* link down again */
2110 slave->link = BOND_LINK_DOWN;
2111
2112 printk(KERN_INFO DRV_NAME
2113 ": %s: link status down again after %d "
2114 "ms for interface %s.\n",
2115 bond_dev->name,
2116 (bond->params.updelay - slave->delay) * bond->params.miimon,
2117 slave_dev->name);
2118 } else {
2119 /* link stays up */
2120 if (slave->delay == 0) {
2121 /* now the link has been up for long time enough */
2122 slave->link = BOND_LINK_UP;
2123 slave->jiffies = jiffies;
2124
2125 if (bond->params.mode == BOND_MODE_8023AD) {
2126 /* prevent it from being the active one */
2127 slave->state = BOND_STATE_BACKUP;
2128 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2129 /* make it immediately active */
2130 slave->state = BOND_STATE_ACTIVE;
2131 } else if (slave != bond->primary_slave) {
2132 /* prevent it from being the active one */
2133 slave->state = BOND_STATE_BACKUP;
2134 }
2135
2136 printk(KERN_INFO DRV_NAME
2137 ": %s: link status definitely "
2138 "up for interface %s.\n",
2139 bond_dev->name,
2140 slave_dev->name);
2141
2142 /* notify ad that the link status has changed */
2143 if (bond->params.mode == BOND_MODE_8023AD) {
2144 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2145 }
2146
2147 if ((bond->params.mode == BOND_MODE_TLB) ||
2148 (bond->params.mode == BOND_MODE_ALB)) {
2149 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2150 }
2151
2152 if ((!oldcurrent) ||
2153 (slave == bond->primary_slave)) {
2154 do_failover = 1;
2155 }
2156 } else {
2157 slave->delay--;
2158 }
2159 }
2160 break;
2161 default:
2162 /* Should not happen */
4e0952c7
MW
2163 printk(KERN_ERR DRV_NAME
2164 ": %s: Error: %s Illegal value (link=%d)\n",
2165 bond_dev->name,
2166 slave->dev->name,
2167 slave->link);
1da177e4
LT
2168 goto out;
2169 } /* end of switch (slave->link) */
2170
2171 bond_update_speed_duplex(slave);
2172
2173 if (bond->params.mode == BOND_MODE_8023AD) {
2174 if (old_speed != slave->speed) {
2175 bond_3ad_adapter_speed_changed(slave);
2176 }
2177
2178 if (old_duplex != slave->duplex) {
2179 bond_3ad_adapter_duplex_changed(slave);
2180 }
2181 }
2182
2183 } /* end of for */
2184
2185 if (do_failover) {
2186 write_lock(&bond->curr_slave_lock);
2187
2188 bond_select_active_slave(bond);
2189
2190 if (oldcurrent && !bond->curr_active_slave) {
2191 printk(KERN_INFO DRV_NAME
2192 ": %s: now running without any active "
2193 "interface !\n",
2194 bond_dev->name);
2195 }
2196
2197 write_unlock(&bond->curr_slave_lock);
2198 }
2199
2200re_arm:
2201 if (bond->params.miimon) {
2202 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2203 }
2204out:
2205 read_unlock(&bond->lock);
2206}
2207
c3ade5ca
JV
2208
2209static u32 bond_glean_dev_ip(struct net_device *dev)
2210{
2211 struct in_device *idev;
2212 struct in_ifaddr *ifa;
2213 u32 addr = 0;
2214
2215 if (!dev)
2216 return 0;
2217
2218 rcu_read_lock();
e5ed6399 2219 idev = __in_dev_get_rcu(dev);
c3ade5ca
JV
2220 if (!idev)
2221 goto out;
2222
2223 ifa = idev->ifa_list;
2224 if (!ifa)
2225 goto out;
2226
2227 addr = ifa->ifa_local;
2228out:
2229 rcu_read_unlock();
2230 return addr;
2231}
2232
2233static int bond_has_ip(struct bonding *bond)
2234{
2235 struct vlan_entry *vlan, *vlan_next;
2236
2237 if (bond->master_ip)
2238 return 1;
2239
2240 if (list_empty(&bond->vlan_list))
2241 return 0;
2242
2243 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2244 vlan_list) {
2245 if (vlan->vlan_ip)
2246 return 1;
2247 }
2248
2249 return 0;
2250}
2251
2252/*
2253 * We go to the (large) trouble of VLAN tagging ARP frames because
2254 * switches in VLAN mode (especially if ports are configured as
2255 * "native" to a VLAN) might not pass non-tagged frames.
2256 */
2257static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip, u32 src_ip, unsigned short vlan_id)
2258{
2259 struct sk_buff *skb;
2260
2261 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2262 slave_dev->name, dest_ip, src_ip, vlan_id);
2263
2264 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2265 NULL, slave_dev->dev_addr, NULL);
2266
2267 if (!skb) {
2268 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2269 return;
2270 }
2271 if (vlan_id) {
2272 skb = vlan_put_tag(skb, vlan_id);
2273 if (!skb) {
2274 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2275 return;
2276 }
2277 }
2278 arp_xmit(skb);
2279}
2280
2281
1da177e4
LT
2282static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2283{
c3ade5ca 2284 int i, vlan_id, rv;
1da177e4 2285 u32 *targets = bond->params.arp_targets;
c3ade5ca
JV
2286 struct vlan_entry *vlan, *vlan_next;
2287 struct net_device *vlan_dev;
2288 struct flowi fl;
2289 struct rtable *rt;
1da177e4 2290
6b780567
MW
2291 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2292 if (!targets[i])
2293 continue;
c3ade5ca
JV
2294 dprintk("basa: target %x\n", targets[i]);
2295 if (list_empty(&bond->vlan_list)) {
2296 dprintk("basa: empty vlan: arp_send\n");
2297 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2298 bond->master_ip, 0);
2299 continue;
2300 }
2301
2302 /*
2303 * If VLANs are configured, we do a route lookup to
2304 * determine which VLAN interface would be used, so we
2305 * can tag the ARP with the proper VLAN tag.
2306 */
2307 memset(&fl, 0, sizeof(fl));
2308 fl.fl4_dst = targets[i];
2309 fl.fl4_tos = RTO_ONLINK;
2310
2311 rv = ip_route_output_key(&rt, &fl);
2312 if (rv) {
2313 if (net_ratelimit()) {
2314 printk(KERN_WARNING DRV_NAME
2315 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2316 bond->dev->name, NIPQUAD(fl.fl4_dst));
2317 }
2318 continue;
2319 }
2320
2321 /*
2322 * This target is not on a VLAN
2323 */
2324 if (rt->u.dst.dev == bond->dev) {
ed4b9f80 2325 ip_rt_put(rt);
c3ade5ca
JV
2326 dprintk("basa: rtdev == bond->dev: arp_send\n");
2327 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2328 bond->master_ip, 0);
2329 continue;
2330 }
2331
2332 vlan_id = 0;
2333 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2334 vlan_list) {
2335 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2336 if (vlan_dev == rt->u.dst.dev) {
2337 vlan_id = vlan->vlan_id;
2338 dprintk("basa: vlan match on %s %d\n",
2339 vlan_dev->name, vlan_id);
2340 break;
2341 }
2342 }
2343
2344 if (vlan_id) {
ed4b9f80 2345 ip_rt_put(rt);
c3ade5ca
JV
2346 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2347 vlan->vlan_ip, vlan_id);
2348 continue;
2349 }
2350
2351 if (net_ratelimit()) {
2352 printk(KERN_WARNING DRV_NAME
2353 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2354 bond->dev->name, NIPQUAD(fl.fl4_dst),
2355 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2356 }
ed4b9f80 2357 ip_rt_put(rt);
c3ade5ca
JV
2358 }
2359}
2360
2361/*
2362 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2363 * for each VLAN above us.
2364 */
2365static void bond_send_gratuitous_arp(struct bonding *bond)
2366{
2367 struct slave *slave = bond->curr_active_slave;
2368 struct vlan_entry *vlan;
2369 struct net_device *vlan_dev;
2370
2371 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2372 slave ? slave->dev->name : "NULL");
2373 if (!slave)
2374 return;
2375
2376 if (bond->master_ip) {
2377 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2378 bond->master_ip, 0);
2379 }
2380
2381 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2382 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2383 if (vlan->vlan_ip) {
2384 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2385 vlan->vlan_ip, vlan->vlan_id);
2386 }
1da177e4
LT
2387 }
2388}
2389
2390/*
2391 * this function is called regularly to monitor each slave's link
2392 * ensuring that traffic is being sent and received when arp monitoring
2393 * is used in load-balancing mode. if the adapter has been dormant, then an
2394 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2395 * arp monitoring in active backup mode.
2396 */
a77b5325 2397void bond_loadbalance_arp_mon(struct net_device *bond_dev)
1da177e4
LT
2398{
2399 struct bonding *bond = bond_dev->priv;
2400 struct slave *slave, *oldcurrent;
2401 int do_failover = 0;
2402 int delta_in_ticks;
2403 int i;
2404
2405 read_lock(&bond->lock);
2406
2407 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2408
2409 if (bond->kill_timers) {
2410 goto out;
2411 }
2412
2413 if (bond->slave_cnt == 0) {
2414 goto re_arm;
2415 }
2416
2417 read_lock(&bond->curr_slave_lock);
2418 oldcurrent = bond->curr_active_slave;
2419 read_unlock(&bond->curr_slave_lock);
2420
2421 /* see if any of the previous devices are up now (i.e. they have
2422 * xmt and rcv traffic). the curr_active_slave does not come into
2423 * the picture unless it is null. also, slave->jiffies is not needed
2424 * here because we send an arp on each slave and give a slave as
2425 * long as it needs to get the tx/rx within the delta.
2426 * TODO: what about up/down delay in arp mode? it wasn't here before
2427 * so it can wait
2428 */
2429 bond_for_each_slave(bond, slave, i) {
2430 if (slave->link != BOND_LINK_UP) {
2431 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2432 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2433
2434 slave->link = BOND_LINK_UP;
2435 slave->state = BOND_STATE_ACTIVE;
2436
2437 /* primary_slave has no meaning in round-robin
2438 * mode. the window of a slave being up and
2439 * curr_active_slave being null after enslaving
2440 * is closed.
2441 */
2442 if (!oldcurrent) {
2443 printk(KERN_INFO DRV_NAME
2444 ": %s: link status definitely "
2445 "up for interface %s, ",
2446 bond_dev->name,
2447 slave->dev->name);
2448 do_failover = 1;
2449 } else {
2450 printk(KERN_INFO DRV_NAME
2451 ": %s: interface %s is now up\n",
2452 bond_dev->name,
2453 slave->dev->name);
2454 }
2455 }
2456 } else {
2457 /* slave->link == BOND_LINK_UP */
2458
2459 /* not all switches will respond to an arp request
2460 * when the source ip is 0, so don't take the link down
2461 * if we don't know our ip yet
2462 */
2463 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2464 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
c3ade5ca 2465 bond_has_ip(bond))) {
1da177e4
LT
2466
2467 slave->link = BOND_LINK_DOWN;
2468 slave->state = BOND_STATE_BACKUP;
2469
2470 if (slave->link_failure_count < UINT_MAX) {
2471 slave->link_failure_count++;
2472 }
2473
2474 printk(KERN_INFO DRV_NAME
2475 ": %s: interface %s is now down.\n",
2476 bond_dev->name,
2477 slave->dev->name);
2478
2479 if (slave == oldcurrent) {
2480 do_failover = 1;
2481 }
2482 }
2483 }
2484
2485 /* note: if switch is in round-robin mode, all links
2486 * must tx arp to ensure all links rx an arp - otherwise
2487 * links may oscillate or not come up at all; if switch is
2488 * in something like xor mode, there is nothing we can
2489 * do - all replies will be rx'ed on same link causing slaves
2490 * to be unstable during low/no traffic periods
2491 */
2492 if (IS_UP(slave->dev)) {
2493 bond_arp_send_all(bond, slave);
2494 }
2495 }
2496
2497 if (do_failover) {
2498 write_lock(&bond->curr_slave_lock);
2499
2500 bond_select_active_slave(bond);
2501
2502 if (oldcurrent && !bond->curr_active_slave) {
2503 printk(KERN_INFO DRV_NAME
2504 ": %s: now running without any active "
2505 "interface !\n",
2506 bond_dev->name);
2507 }
2508
2509 write_unlock(&bond->curr_slave_lock);
2510 }
2511
2512re_arm:
2513 if (bond->params.arp_interval) {
2514 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2515 }
2516out:
2517 read_unlock(&bond->lock);
2518}
2519
2520/*
2521 * When using arp monitoring in active-backup mode, this function is
2522 * called to determine if any backup slaves have went down or a new
2523 * current slave needs to be found.
2524 * The backup slaves never generate traffic, they are considered up by merely
2525 * receiving traffic. If the current slave goes down, each backup slave will
2526 * be given the opportunity to tx/rx an arp before being taken down - this
2527 * prevents all slaves from being taken down due to the current slave not
2528 * sending any traffic for the backups to receive. The arps are not necessarily
2529 * necessary, any tx and rx traffic will keep the current slave up. While any
2530 * rx traffic will keep the backup slaves up, the current slave is responsible
2531 * for generating traffic to keep them up regardless of any other traffic they
2532 * may have received.
2533 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2534 */
a77b5325 2535void bond_activebackup_arp_mon(struct net_device *bond_dev)
1da177e4
LT
2536{
2537 struct bonding *bond = bond_dev->priv;
2538 struct slave *slave;
2539 int delta_in_ticks;
2540 int i;
2541
2542 read_lock(&bond->lock);
2543
2544 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2545
2546 if (bond->kill_timers) {
2547 goto out;
2548 }
2549
2550 if (bond->slave_cnt == 0) {
2551 goto re_arm;
2552 }
2553
2554 /* determine if any slave has come up or any backup slave has
2555 * gone down
2556 * TODO: what about up/down delay in arp mode? it wasn't here before
2557 * so it can wait
2558 */
2559 bond_for_each_slave(bond, slave, i) {
2560 if (slave->link != BOND_LINK_UP) {
2561 if ((jiffies - slave->dev->last_rx) <= delta_in_ticks) {
2562
2563 slave->link = BOND_LINK_UP;
2564
2565 write_lock(&bond->curr_slave_lock);
2566
2567 if ((!bond->curr_active_slave) &&
2568 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2569 bond_change_active_slave(bond, slave);
2570 bond->current_arp_slave = NULL;
2571 } else if (bond->curr_active_slave != slave) {
2572 /* this slave has just come up but we
2573 * already have a current slave; this
2574 * can also happen if bond_enslave adds
2575 * a new slave that is up while we are
2576 * searching for a new slave
2577 */
2578 bond_set_slave_inactive_flags(slave);
2579 bond->current_arp_slave = NULL;
2580 }
2581
2582 if (slave == bond->curr_active_slave) {
2583 printk(KERN_INFO DRV_NAME
2584 ": %s: %s is up and now the "
2585 "active interface\n",
2586 bond_dev->name,
2587 slave->dev->name);
2588 } else {
2589 printk(KERN_INFO DRV_NAME
2590 ": %s: backup interface %s is "
2591 "now up\n",
2592 bond_dev->name,
2593 slave->dev->name);
2594 }
2595
2596 write_unlock(&bond->curr_slave_lock);
2597 }
2598 } else {
2599 read_lock(&bond->curr_slave_lock);
2600
2601 if ((slave != bond->curr_active_slave) &&
2602 (!bond->current_arp_slave) &&
2603 (((jiffies - slave->dev->last_rx) >= 3*delta_in_ticks) &&
c3ade5ca 2604 bond_has_ip(bond))) {
1da177e4
LT
2605 /* a backup slave has gone down; three times
2606 * the delta allows the current slave to be
2607 * taken out before the backup slave.
2608 * note: a non-null current_arp_slave indicates
2609 * the curr_active_slave went down and we are
2610 * searching for a new one; under this
2611 * condition we only take the curr_active_slave
2612 * down - this gives each slave a chance to
2613 * tx/rx traffic before being taken out
2614 */
2615
2616 read_unlock(&bond->curr_slave_lock);
2617
2618 slave->link = BOND_LINK_DOWN;
2619
2620 if (slave->link_failure_count < UINT_MAX) {
2621 slave->link_failure_count++;
2622 }
2623
2624 bond_set_slave_inactive_flags(slave);
2625
2626 printk(KERN_INFO DRV_NAME
2627 ": %s: backup interface %s is now down\n",
2628 bond_dev->name,
2629 slave->dev->name);
2630 } else {
2631 read_unlock(&bond->curr_slave_lock);
2632 }
2633 }
2634 }
2635
2636 read_lock(&bond->curr_slave_lock);
2637 slave = bond->curr_active_slave;
2638 read_unlock(&bond->curr_slave_lock);
2639
2640 if (slave) {
2641 /* if we have sent traffic in the past 2*arp_intervals but
2642 * haven't xmit and rx traffic in that time interval, select
2643 * a different slave. slave->jiffies is only updated when
2644 * a slave first becomes the curr_active_slave - not necessarily
2645 * after every arp; this ensures the slave has a full 2*delta
2646 * before being taken out. if a primary is being used, check
2647 * if it is up and needs to take over as the curr_active_slave
2648 */
2649 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
c3ade5ca
JV
2650 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2651 bond_has_ip(bond))) &&
1da177e4
LT
2652 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2653
2654 slave->link = BOND_LINK_DOWN;
2655
2656 if (slave->link_failure_count < UINT_MAX) {
2657 slave->link_failure_count++;
2658 }
2659
2660 printk(KERN_INFO DRV_NAME
2661 ": %s: link status down for active interface "
2662 "%s, disabling it\n",
2663 bond_dev->name,
2664 slave->dev->name);
2665
2666 write_lock(&bond->curr_slave_lock);
2667
2668 bond_select_active_slave(bond);
2669 slave = bond->curr_active_slave;
2670
2671 write_unlock(&bond->curr_slave_lock);
2672
2673 bond->current_arp_slave = slave;
2674
2675 if (slave) {
2676 slave->jiffies = jiffies;
2677 }
2678 } else if ((bond->primary_slave) &&
2679 (bond->primary_slave != slave) &&
2680 (bond->primary_slave->link == BOND_LINK_UP)) {
2681 /* at this point, slave is the curr_active_slave */
2682 printk(KERN_INFO DRV_NAME
2683 ": %s: changing from interface %s to primary "
2684 "interface %s\n",
2685 bond_dev->name,
2686 slave->dev->name,
2687 bond->primary_slave->dev->name);
2688
2689 /* primary is up so switch to it */
2690 write_lock(&bond->curr_slave_lock);
2691 bond_change_active_slave(bond, bond->primary_slave);
2692 write_unlock(&bond->curr_slave_lock);
2693
2694 slave = bond->primary_slave;
2695 slave->jiffies = jiffies;
2696 } else {
2697 bond->current_arp_slave = NULL;
2698 }
2699
2700 /* the current slave must tx an arp to ensure backup slaves
2701 * rx traffic
2702 */
c3ade5ca 2703 if (slave && bond_has_ip(bond)) {
1da177e4
LT
2704 bond_arp_send_all(bond, slave);
2705 }
2706 }
2707
2708 /* if we don't have a curr_active_slave, search for the next available
2709 * backup slave from the current_arp_slave and make it the candidate
2710 * for becoming the curr_active_slave
2711 */
2712 if (!slave) {
2713 if (!bond->current_arp_slave) {
2714 bond->current_arp_slave = bond->first_slave;
2715 }
2716
2717 if (bond->current_arp_slave) {
2718 bond_set_slave_inactive_flags(bond->current_arp_slave);
2719
2720 /* search for next candidate */
2f872f04 2721 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
1da177e4
LT
2722 if (IS_UP(slave->dev)) {
2723 slave->link = BOND_LINK_BACK;
2724 bond_set_slave_active_flags(slave);
2725 bond_arp_send_all(bond, slave);
2726 slave->jiffies = jiffies;
2727 bond->current_arp_slave = slave;
2728 break;
2729 }
2730
2731 /* if the link state is up at this point, we
2732 * mark it down - this can happen if we have
2733 * simultaneous link failures and
2734 * reselect_active_interface doesn't make this
2735 * one the current slave so it is still marked
2736 * up when it is actually down
2737 */
2738 if (slave->link == BOND_LINK_UP) {
2739 slave->link = BOND_LINK_DOWN;
2740 if (slave->link_failure_count < UINT_MAX) {
2741 slave->link_failure_count++;
2742 }
2743
2744 bond_set_slave_inactive_flags(slave);
2745
2746 printk(KERN_INFO DRV_NAME
2747 ": %s: backup interface %s is "
2748 "now down.\n",
2749 bond_dev->name,
2750 slave->dev->name);
2751 }
2752 }
2753 }
2754 }
2755
2756re_arm:
2757 if (bond->params.arp_interval) {
2758 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2759 }
2760out:
2761 read_unlock(&bond->lock);
2762}
2763
2764/*------------------------------ proc/seq_file-------------------------------*/
2765
2766#ifdef CONFIG_PROC_FS
2767
2768#define SEQ_START_TOKEN ((void *)1)
2769
2770static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
2771{
2772 struct bonding *bond = seq->private;
2773 loff_t off = 0;
2774 struct slave *slave;
2775 int i;
2776
2777 /* make sure the bond won't be taken away */
2778 read_lock(&dev_base_lock);
2779 read_lock_bh(&bond->lock);
2780
2781 if (*pos == 0) {
2782 return SEQ_START_TOKEN;
2783 }
2784
2785 bond_for_each_slave(bond, slave, i) {
2786 if (++off == *pos) {
2787 return slave;
2788 }
2789 }
2790
2791 return NULL;
2792}
2793
2794static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2795{
2796 struct bonding *bond = seq->private;
2797 struct slave *slave = v;
2798
2799 ++*pos;
2800 if (v == SEQ_START_TOKEN) {
2801 return bond->first_slave;
2802 }
2803
2804 slave = slave->next;
2805
2806 return (slave == bond->first_slave) ? NULL : slave;
2807}
2808
2809static void bond_info_seq_stop(struct seq_file *seq, void *v)
2810{
2811 struct bonding *bond = seq->private;
2812
2813 read_unlock_bh(&bond->lock);
2814 read_unlock(&dev_base_lock);
2815}
2816
2817static void bond_info_show_master(struct seq_file *seq)
2818{
2819 struct bonding *bond = seq->private;
2820 struct slave *curr;
4756b02f
MW
2821 int i;
2822 u32 target;
1da177e4
LT
2823
2824 read_lock(&bond->curr_slave_lock);
2825 curr = bond->curr_active_slave;
2826 read_unlock(&bond->curr_slave_lock);
2827
2828 seq_printf(seq, "Bonding Mode: %s\n",
2829 bond_mode_name(bond->params.mode));
2830
c61b75ad
MW
2831 if (bond->params.mode == BOND_MODE_XOR ||
2832 bond->params.mode == BOND_MODE_8023AD) {
2833 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
2834 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
2835 bond->params.xmit_policy);
2836 }
2837
1da177e4
LT
2838 if (USES_PRIMARY(bond->params.mode)) {
2839 seq_printf(seq, "Primary Slave: %s\n",
0f418b2a
MW
2840 (bond->primary_slave) ?
2841 bond->primary_slave->dev->name : "None");
1da177e4
LT
2842
2843 seq_printf(seq, "Currently Active Slave: %s\n",
2844 (curr) ? curr->dev->name : "None");
2845 }
2846
2847 seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
2848 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
2849 seq_printf(seq, "Up Delay (ms): %d\n",
2850 bond->params.updelay * bond->params.miimon);
2851 seq_printf(seq, "Down Delay (ms): %d\n",
2852 bond->params.downdelay * bond->params.miimon);
2853
4756b02f
MW
2854
2855 /* ARP information */
2856 if(bond->params.arp_interval > 0) {
2857 int printed=0;
2858 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
2859 bond->params.arp_interval);
2860
2861 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
2862
2863 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
2864 if (!bond->params.arp_targets[i])
2865 continue;
2866 if (printed)
2867 seq_printf(seq, ",");
2868 target = ntohl(bond->params.arp_targets[i]);
2869 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
2870 printed = 1;
2871 }
2872 seq_printf(seq, "\n");
2873 }
2874
1da177e4
LT
2875 if (bond->params.mode == BOND_MODE_8023AD) {
2876 struct ad_info ad_info;
2877
2878 seq_puts(seq, "\n802.3ad info\n");
2879 seq_printf(seq, "LACP rate: %s\n",
2880 (bond->params.lacp_fast) ? "fast" : "slow");
2881
2882 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
2883 seq_printf(seq, "bond %s has no active aggregator\n",
2884 bond->dev->name);
2885 } else {
2886 seq_printf(seq, "Active Aggregator Info:\n");
2887
2888 seq_printf(seq, "\tAggregator ID: %d\n",
2889 ad_info.aggregator_id);
2890 seq_printf(seq, "\tNumber of ports: %d\n",
2891 ad_info.ports);
2892 seq_printf(seq, "\tActor Key: %d\n",
2893 ad_info.actor_key);
2894 seq_printf(seq, "\tPartner Key: %d\n",
2895 ad_info.partner_key);
2896 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
2897 ad_info.partner_system[0],
2898 ad_info.partner_system[1],
2899 ad_info.partner_system[2],
2900 ad_info.partner_system[3],
2901 ad_info.partner_system[4],
2902 ad_info.partner_system[5]);
2903 }
2904 }
2905}
2906
2907static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
2908{
2909 struct bonding *bond = seq->private;
2910
2911 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
2912 seq_printf(seq, "MII Status: %s\n",
2913 (slave->link == BOND_LINK_UP) ? "up" : "down");
2914 seq_printf(seq, "Link Failure Count: %d\n",
2915 slave->link_failure_count);
2916
217df670
JV
2917 seq_printf(seq,
2918 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
2919 slave->perm_hwaddr[0], slave->perm_hwaddr[1],
2920 slave->perm_hwaddr[2], slave->perm_hwaddr[3],
2921 slave->perm_hwaddr[4], slave->perm_hwaddr[5]);
1da177e4
LT
2922
2923 if (bond->params.mode == BOND_MODE_8023AD) {
2924 const struct aggregator *agg
2925 = SLAVE_AD_INFO(slave).port.aggregator;
2926
2927 if (agg) {
2928 seq_printf(seq, "Aggregator ID: %d\n",
2929 agg->aggregator_identifier);
2930 } else {
2931 seq_puts(seq, "Aggregator ID: N/A\n");
2932 }
2933 }
2934}
2935
2936static int bond_info_seq_show(struct seq_file *seq, void *v)
2937{
2938 if (v == SEQ_START_TOKEN) {
2939 seq_printf(seq, "%s\n", version);
2940 bond_info_show_master(seq);
2941 } else {
2942 bond_info_show_slave(seq, v);
2943 }
2944
2945 return 0;
2946}
2947
2948static struct seq_operations bond_info_seq_ops = {
2949 .start = bond_info_seq_start,
2950 .next = bond_info_seq_next,
2951 .stop = bond_info_seq_stop,
2952 .show = bond_info_seq_show,
2953};
2954
2955static int bond_info_open(struct inode *inode, struct file *file)
2956{
2957 struct seq_file *seq;
2958 struct proc_dir_entry *proc;
2959 int res;
2960
2961 res = seq_open(file, &bond_info_seq_ops);
2962 if (!res) {
2963 /* recover the pointer buried in proc_dir_entry data */
2964 seq = file->private_data;
2965 proc = PDE(inode);
2966 seq->private = proc->data;
2967 }
2968
2969 return res;
2970}
2971
2972static struct file_operations bond_info_fops = {
2973 .owner = THIS_MODULE,
2974 .open = bond_info_open,
2975 .read = seq_read,
2976 .llseek = seq_lseek,
2977 .release = seq_release,
2978};
2979
2980static int bond_create_proc_entry(struct bonding *bond)
2981{
2982 struct net_device *bond_dev = bond->dev;
2983
2984 if (bond_proc_dir) {
2985 bond->proc_entry = create_proc_entry(bond_dev->name,
2986 S_IRUGO,
2987 bond_proc_dir);
2988 if (bond->proc_entry == NULL) {
2989 printk(KERN_WARNING DRV_NAME
2990 ": Warning: Cannot create /proc/net/%s/%s\n",
2991 DRV_NAME, bond_dev->name);
2992 } else {
2993 bond->proc_entry->data = bond;
2994 bond->proc_entry->proc_fops = &bond_info_fops;
2995 bond->proc_entry->owner = THIS_MODULE;
2996 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
2997 }
2998 }
2999
3000 return 0;
3001}
3002
3003static void bond_remove_proc_entry(struct bonding *bond)
3004{
3005 if (bond_proc_dir && bond->proc_entry) {
3006 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3007 memset(bond->proc_file_name, 0, IFNAMSIZ);
3008 bond->proc_entry = NULL;
3009 }
3010}
3011
3012/* Create the bonding directory under /proc/net, if doesn't exist yet.
3013 * Caller must hold rtnl_lock.
3014 */
3015static void bond_create_proc_dir(void)
3016{
3017 int len = strlen(DRV_NAME);
3018
3019 for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3020 bond_proc_dir = bond_proc_dir->next) {
3021 if ((bond_proc_dir->namelen == len) &&
3022 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3023 break;
3024 }
3025 }
3026
3027 if (!bond_proc_dir) {
3028 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3029 if (bond_proc_dir) {
3030 bond_proc_dir->owner = THIS_MODULE;
3031 } else {
3032 printk(KERN_WARNING DRV_NAME
3033 ": Warning: cannot create /proc/net/%s\n",
3034 DRV_NAME);
3035 }
3036 }
3037}
3038
3039/* Destroy the bonding directory under /proc/net, if empty.
3040 * Caller must hold rtnl_lock.
3041 */
3042static void bond_destroy_proc_dir(void)
3043{
3044 struct proc_dir_entry *de;
3045
3046 if (!bond_proc_dir) {
3047 return;
3048 }
3049
3050 /* verify that the /proc dir is empty */
3051 for (de = bond_proc_dir->subdir; de; de = de->next) {
3052 /* ignore . and .. */
3053 if (*(de->name) != '.') {
3054 break;
3055 }
3056 }
3057
3058 if (de) {
3059 if (bond_proc_dir->owner == THIS_MODULE) {
3060 bond_proc_dir->owner = NULL;
3061 }
3062 } else {
3063 remove_proc_entry(DRV_NAME, proc_net);
3064 bond_proc_dir = NULL;
3065 }
3066}
3067#endif /* CONFIG_PROC_FS */
3068
3069/*-------------------------- netdev event handling --------------------------*/
3070
3071/*
3072 * Change device name
3073 */
3074static int bond_event_changename(struct bonding *bond)
3075{
3076#ifdef CONFIG_PROC_FS
3077 bond_remove_proc_entry(bond);
3078 bond_create_proc_entry(bond);
3079#endif
b76cdba9
MW
3080 down_write(&(bonding_rwsem));
3081 bond_destroy_sysfs_entry(bond);
3082 bond_create_sysfs_entry(bond);
3083 up_write(&(bonding_rwsem));
1da177e4
LT
3084 return NOTIFY_DONE;
3085}
3086
3087static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3088{
3089 struct bonding *event_bond = bond_dev->priv;
3090
3091 switch (event) {
3092 case NETDEV_CHANGENAME:
3093 return bond_event_changename(event_bond);
3094 case NETDEV_UNREGISTER:
3095 /*
3096 * TODO: remove a bond from the list?
3097 */
3098 break;
3099 default:
3100 break;
3101 }
3102
3103 return NOTIFY_DONE;
3104}
3105
3106static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3107{
3108 struct net_device *bond_dev = slave_dev->master;
8531c5ff 3109 struct bonding *bond = bond_dev->priv;
1da177e4
LT
3110
3111 switch (event) {
3112 case NETDEV_UNREGISTER:
3113 if (bond_dev) {
3114 bond_release(bond_dev, slave_dev);
3115 }
3116 break;
3117 case NETDEV_CHANGE:
3118 /*
3119 * TODO: is this what we get if somebody
3120 * sets up a hierarchical bond, then rmmod's
3121 * one of the slave bonding devices?
3122 */
3123 break;
3124 case NETDEV_DOWN:
3125 /*
3126 * ... Or is it this?
3127 */
3128 break;
3129 case NETDEV_CHANGEMTU:
3130 /*
3131 * TODO: Should slaves be allowed to
3132 * independently alter their MTU? For
3133 * an active-backup bond, slaves need
3134 * not be the same type of device, so
3135 * MTUs may vary. For other modes,
3136 * slaves arguably should have the
3137 * same MTUs. To do this, we'd need to
3138 * take over the slave's change_mtu
3139 * function for the duration of their
3140 * servitude.
3141 */
3142 break;
3143 case NETDEV_CHANGENAME:
3144 /*
3145 * TODO: handle changing the primary's name
3146 */
3147 break;
8531c5ff
AK
3148 case NETDEV_FEAT_CHANGE:
3149 bond_compute_features(bond);
3150 break;
1da177e4
LT
3151 default:
3152 break;
3153 }
3154
3155 return NOTIFY_DONE;
3156}
3157
3158/*
3159 * bond_netdev_event: handle netdev notifier chain events.
3160 *
3161 * This function receives events for the netdev chain. The caller (an
e041c683 3162 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
1da177e4
LT
3163 * locks for us to safely manipulate the slave devices (RTNL lock,
3164 * dev_probe_lock).
3165 */
3166static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3167{
3168 struct net_device *event_dev = (struct net_device *)ptr;
3169
3170 dprintk("event_dev: %s, event: %lx\n",
3171 (event_dev ? event_dev->name : "None"),
3172 event);
3173
3174 if (event_dev->flags & IFF_MASTER) {
3175 dprintk("IFF_MASTER\n");
3176 return bond_master_netdev_event(event, event_dev);
3177 }
3178
3179 if (event_dev->flags & IFF_SLAVE) {
3180 dprintk("IFF_SLAVE\n");
3181 return bond_slave_netdev_event(event, event_dev);
3182 }
3183
3184 return NOTIFY_DONE;
3185}
3186
c3ade5ca
JV
3187/*
3188 * bond_inetaddr_event: handle inetaddr notifier chain events.
3189 *
3190 * We keep track of device IPs primarily to use as source addresses in
3191 * ARP monitor probes (rather than spewing out broadcasts all the time).
3192 *
3193 * We track one IP for the main device (if it has one), plus one per VLAN.
3194 */
3195static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3196{
3197 struct in_ifaddr *ifa = ptr;
3198 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3199 struct bonding *bond, *bond_next;
3200 struct vlan_entry *vlan, *vlan_next;
3201
3202 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3203 if (bond->dev == event_dev) {
3204 switch (event) {
3205 case NETDEV_UP:
3206 bond->master_ip = ifa->ifa_local;
3207 return NOTIFY_OK;
3208 case NETDEV_DOWN:
3209 bond->master_ip = bond_glean_dev_ip(bond->dev);
3210 return NOTIFY_OK;
3211 default:
3212 return NOTIFY_DONE;
3213 }
3214 }
3215
3216 if (list_empty(&bond->vlan_list))
3217 continue;
3218
3219 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3220 vlan_list) {
3221 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
3222 if (vlan_dev == event_dev) {
3223 switch (event) {
3224 case NETDEV_UP:
3225 vlan->vlan_ip = ifa->ifa_local;
3226 return NOTIFY_OK;
3227 case NETDEV_DOWN:
3228 vlan->vlan_ip =
3229 bond_glean_dev_ip(vlan_dev);
3230 return NOTIFY_OK;
3231 default:
3232 return NOTIFY_DONE;
3233 }
3234 }
3235 }
3236 }
3237 return NOTIFY_DONE;
3238}
3239
1da177e4
LT
3240static struct notifier_block bond_netdev_notifier = {
3241 .notifier_call = bond_netdev_event,
3242};
3243
c3ade5ca
JV
3244static struct notifier_block bond_inetaddr_notifier = {
3245 .notifier_call = bond_inetaddr_event,
3246};
3247
1da177e4
LT
3248/*-------------------------- Packet type handling ---------------------------*/
3249
3250/* register to receive lacpdus on a bond */
3251static void bond_register_lacpdu(struct bonding *bond)
3252{
3253 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3254
3255 /* initialize packet type */
3256 pk_type->type = PKT_TYPE_LACPDU;
3257 pk_type->dev = bond->dev;
3258 pk_type->func = bond_3ad_lacpdu_recv;
3259
3260 dev_add_pack(pk_type);
3261}
3262
3263/* unregister to receive lacpdus on a bond */
3264static void bond_unregister_lacpdu(struct bonding *bond)
3265{
3266 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3267}
3268
169a3e66
JV
3269/*---------------------------- Hashing Policies -----------------------------*/
3270
3271/*
3272 * Hash for the the output device based upon layer 3 and layer 4 data. If
3273 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3274 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3275 */
3276static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3277 struct net_device *bond_dev, int count)
3278{
3279 struct ethhdr *data = (struct ethhdr *)skb->data;
3280 struct iphdr *iph = skb->nh.iph;
3281 u16 *layer4hdr = (u16 *)((u32 *)iph + iph->ihl);
3282 int layer4_xor = 0;
3283
3284 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3285 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3286 (iph->protocol == IPPROTO_TCP ||
3287 iph->protocol == IPPROTO_UDP)) {
3288 layer4_xor = htons((*layer4hdr ^ *(layer4hdr + 1)));
3289 }
3290 return (layer4_xor ^
3291 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3292
3293 }
3294
3295 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3296}
3297
3298/*
3299 * Hash for the output device based upon layer 2 data
3300 */
3301static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3302 struct net_device *bond_dev, int count)
3303{
3304 struct ethhdr *data = (struct ethhdr *)skb->data;
3305
3306 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3307}
3308
1da177e4
LT
3309/*-------------------------- Device entry points ----------------------------*/
3310
3311static int bond_open(struct net_device *bond_dev)
3312{
3313 struct bonding *bond = bond_dev->priv;
3314 struct timer_list *mii_timer = &bond->mii_timer;
3315 struct timer_list *arp_timer = &bond->arp_timer;
3316
3317 bond->kill_timers = 0;
3318
3319 if ((bond->params.mode == BOND_MODE_TLB) ||
3320 (bond->params.mode == BOND_MODE_ALB)) {
3321 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3322
3323 /* bond_alb_initialize must be called before the timer
3324 * is started.
3325 */
3326 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3327 /* something went wrong - fail the open operation */
3328 return -1;
3329 }
3330
3331 init_timer(alb_timer);
3332 alb_timer->expires = jiffies + 1;
3333 alb_timer->data = (unsigned long)bond;
3334 alb_timer->function = (void *)&bond_alb_monitor;
3335 add_timer(alb_timer);
3336 }
3337
3338 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3339 init_timer(mii_timer);
3340 mii_timer->expires = jiffies + 1;
3341 mii_timer->data = (unsigned long)bond_dev;
3342 mii_timer->function = (void *)&bond_mii_monitor;
3343 add_timer(mii_timer);
3344 }
3345
3346 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3347 init_timer(arp_timer);
3348 arp_timer->expires = jiffies + 1;
3349 arp_timer->data = (unsigned long)bond_dev;
3350 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3351 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3352 } else {
3353 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3354 }
3355 add_timer(arp_timer);
3356 }
3357
3358 if (bond->params.mode == BOND_MODE_8023AD) {
3359 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3360 init_timer(ad_timer);
3361 ad_timer->expires = jiffies + 1;
3362 ad_timer->data = (unsigned long)bond;
3363 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3364 add_timer(ad_timer);
3365
3366 /* register to receive LACPDUs */
3367 bond_register_lacpdu(bond);
3368 }
3369
3370 return 0;
3371}
3372
3373static int bond_close(struct net_device *bond_dev)
3374{
3375 struct bonding *bond = bond_dev->priv;
3376
3377 if (bond->params.mode == BOND_MODE_8023AD) {
3378 /* Unregister the receive of LACPDUs */
3379 bond_unregister_lacpdu(bond);
3380 }
3381
3382 write_lock_bh(&bond->lock);
3383
3384 bond_mc_list_destroy(bond);
3385
3386 /* signal timers not to re-arm */
3387 bond->kill_timers = 1;
3388
3389 write_unlock_bh(&bond->lock);
3390
3391 /* del_timer_sync must run without holding the bond->lock
3392 * because a running timer might be trying to hold it too
3393 */
3394
3395 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3396 del_timer_sync(&bond->mii_timer);
3397 }
3398
3399 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3400 del_timer_sync(&bond->arp_timer);
3401 }
3402
3403 switch (bond->params.mode) {
3404 case BOND_MODE_8023AD:
3405 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3406 break;
3407 case BOND_MODE_TLB:
3408 case BOND_MODE_ALB:
3409 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3410 break;
3411 default:
3412 break;
3413 }
3414
3415 /* Release the bonded slaves */
3416 bond_release_all(bond_dev);
3417
3418 if ((bond->params.mode == BOND_MODE_TLB) ||
3419 (bond->params.mode == BOND_MODE_ALB)) {
3420 /* Must be called only after all
3421 * slaves have been released
3422 */
3423 bond_alb_deinitialize(bond);
3424 }
3425
3426 return 0;
3427}
3428
3429static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3430{
3431 struct bonding *bond = bond_dev->priv;
3432 struct net_device_stats *stats = &(bond->stats), *sstats;
3433 struct slave *slave;
3434 int i;
3435
3436 memset(stats, 0, sizeof(struct net_device_stats));
3437
3438 read_lock_bh(&bond->lock);
3439
3440 bond_for_each_slave(bond, slave, i) {
3441 sstats = slave->dev->get_stats(slave->dev);
3442
3443 stats->rx_packets += sstats->rx_packets;
3444 stats->rx_bytes += sstats->rx_bytes;
3445 stats->rx_errors += sstats->rx_errors;
3446 stats->rx_dropped += sstats->rx_dropped;
3447
3448 stats->tx_packets += sstats->tx_packets;
3449 stats->tx_bytes += sstats->tx_bytes;
3450 stats->tx_errors += sstats->tx_errors;
3451 stats->tx_dropped += sstats->tx_dropped;
3452
3453 stats->multicast += sstats->multicast;
3454 stats->collisions += sstats->collisions;
3455
3456 stats->rx_length_errors += sstats->rx_length_errors;
3457 stats->rx_over_errors += sstats->rx_over_errors;
3458 stats->rx_crc_errors += sstats->rx_crc_errors;
3459 stats->rx_frame_errors += sstats->rx_frame_errors;
3460 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3461 stats->rx_missed_errors += sstats->rx_missed_errors;
3462
3463 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3464 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3465 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3466 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3467 stats->tx_window_errors += sstats->tx_window_errors;
3468 }
3469
3470 read_unlock_bh(&bond->lock);
3471
3472 return stats;
3473}
3474
3475static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3476{
3477 struct net_device *slave_dev = NULL;
3478 struct ifbond k_binfo;
3479 struct ifbond __user *u_binfo = NULL;
3480 struct ifslave k_sinfo;
3481 struct ifslave __user *u_sinfo = NULL;
3482 struct mii_ioctl_data *mii = NULL;
1da177e4
LT
3483 int res = 0;
3484
3485 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3486 bond_dev->name, cmd);
3487
3488 switch (cmd) {
1da177e4
LT
3489 case SIOCGMIIPHY:
3490 mii = if_mii(ifr);
3491 if (!mii) {
3492 return -EINVAL;
3493 }
3494 mii->phy_id = 0;
3495 /* Fall Through */
3496 case SIOCGMIIREG:
3497 /*
3498 * We do this again just in case we were called by SIOCGMIIREG
3499 * instead of SIOCGMIIPHY.
3500 */
3501 mii = if_mii(ifr);
3502 if (!mii) {
3503 return -EINVAL;
3504 }
3505
3506 if (mii->reg_num == 1) {
3507 struct bonding *bond = bond_dev->priv;
3508 mii->val_out = 0;
3509 read_lock_bh(&bond->lock);
3510 read_lock(&bond->curr_slave_lock);
3511 if (bond->curr_active_slave) {
3512 mii->val_out = BMSR_LSTATUS;
3513 }
3514 read_unlock(&bond->curr_slave_lock);
3515 read_unlock_bh(&bond->lock);
3516 }
3517
3518 return 0;
3519 case BOND_INFO_QUERY_OLD:
3520 case SIOCBONDINFOQUERY:
3521 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3522
3523 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3524 return -EFAULT;
3525 }
3526
3527 res = bond_info_query(bond_dev, &k_binfo);
3528 if (res == 0) {
3529 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3530 return -EFAULT;
3531 }
3532 }
3533
3534 return res;
3535 case BOND_SLAVE_INFO_QUERY_OLD:
3536 case SIOCBONDSLAVEINFOQUERY:
3537 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3538
3539 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3540 return -EFAULT;
3541 }
3542
3543 res = bond_slave_info_query(bond_dev, &k_sinfo);
3544 if (res == 0) {
3545 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3546 return -EFAULT;
3547 }
3548 }
3549
3550 return res;
3551 default:
3552 /* Go on */
3553 break;
3554 }
3555
3556 if (!capable(CAP_NET_ADMIN)) {
3557 return -EPERM;
3558 }
3559
b76cdba9 3560 down_write(&(bonding_rwsem));
1da177e4
LT
3561 slave_dev = dev_get_by_name(ifr->ifr_slave);
3562
3563 dprintk("slave_dev=%p: \n", slave_dev);
3564
3565 if (!slave_dev) {
3566 res = -ENODEV;
3567 } else {
3568 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3569 switch (cmd) {
3570 case BOND_ENSLAVE_OLD:
3571 case SIOCBONDENSLAVE:
3572 res = bond_enslave(bond_dev, slave_dev);
3573 break;
3574 case BOND_RELEASE_OLD:
3575 case SIOCBONDRELEASE:
3576 res = bond_release(bond_dev, slave_dev);
3577 break;
3578 case BOND_SETHWADDR_OLD:
3579 case SIOCBONDSETHWADDR:
3580 res = bond_sethwaddr(bond_dev, slave_dev);
3581 break;
3582 case BOND_CHANGE_ACTIVE_OLD:
3583 case SIOCBONDCHANGEACTIVE:
3584 res = bond_ioctl_change_active(bond_dev, slave_dev);
3585 break;
3586 default:
3587 res = -EOPNOTSUPP;
3588 }
3589
3590 dev_put(slave_dev);
3591 }
3592
b76cdba9 3593 up_write(&(bonding_rwsem));
1da177e4
LT
3594 return res;
3595}
3596
3597static void bond_set_multicast_list(struct net_device *bond_dev)
3598{
3599 struct bonding *bond = bond_dev->priv;
3600 struct dev_mc_list *dmi;
3601
3602 write_lock_bh(&bond->lock);
3603
3604 /*
3605 * Do promisc before checking multicast_mode
3606 */
3607 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3608 bond_set_promiscuity(bond, 1);
3609 }
3610
3611 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3612 bond_set_promiscuity(bond, -1);
3613 }
3614
3615 /* set allmulti flag to slaves */
3616 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3617 bond_set_allmulti(bond, 1);
3618 }
3619
3620 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3621 bond_set_allmulti(bond, -1);
3622 }
3623
3624 bond->flags = bond_dev->flags;
3625
3626 /* looking for addresses to add to slaves' mc list */
3627 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3628 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3629 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3630 }
3631 }
3632
3633 /* looking for addresses to delete from slaves' list */
3634 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3635 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3636 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3637 }
3638 }
3639
3640 /* save master's multicast list */
3641 bond_mc_list_destroy(bond);
3642 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3643
3644 write_unlock_bh(&bond->lock);
3645}
3646
3647/*
3648 * Change the MTU of all of a master's slaves to match the master
3649 */
3650static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3651{
3652 struct bonding *bond = bond_dev->priv;
3653 struct slave *slave, *stop_at;
3654 int res = 0;
3655 int i;
3656
3657 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3658 (bond_dev ? bond_dev->name : "None"), new_mtu);
3659
3660 /* Can't hold bond->lock with bh disabled here since
3661 * some base drivers panic. On the other hand we can't
3662 * hold bond->lock without bh disabled because we'll
3663 * deadlock. The only solution is to rely on the fact
3664 * that we're under rtnl_lock here, and the slaves
3665 * list won't change. This doesn't solve the problem
3666 * of setting the slave's MTU while it is
3667 * transmitting, but the assumption is that the base
3668 * driver can handle that.
3669 *
3670 * TODO: figure out a way to safely iterate the slaves
3671 * list, but without holding a lock around the actual
3672 * call to the base driver.
3673 */
3674
3675 bond_for_each_slave(bond, slave, i) {
3676 dprintk("s %p s->p %p c_m %p\n", slave,
3677 slave->prev, slave->dev->change_mtu);
e944ef79 3678
1da177e4
LT
3679 res = dev_set_mtu(slave->dev, new_mtu);
3680
3681 if (res) {
3682 /* If we failed to set the slave's mtu to the new value
3683 * we must abort the operation even in ACTIVE_BACKUP
3684 * mode, because if we allow the backup slaves to have
3685 * different mtu values than the active slave we'll
3686 * need to change their mtu when doing a failover. That
3687 * means changing their mtu from timer context, which
3688 * is probably not a good idea.
3689 */
3690 dprintk("err %d %s\n", res, slave->dev->name);
3691 goto unwind;
3692 }
3693 }
3694
3695 bond_dev->mtu = new_mtu;
3696
3697 return 0;
3698
3699unwind:
3700 /* unwind from head to the slave that failed */
3701 stop_at = slave;
3702 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3703 int tmp_res;
3704
3705 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3706 if (tmp_res) {
3707 dprintk("unwind err %d dev %s\n", tmp_res,
3708 slave->dev->name);
3709 }
3710 }
3711
3712 return res;
3713}
3714
3715/*
3716 * Change HW address
3717 *
3718 * Note that many devices must be down to change the HW address, and
3719 * downing the master releases all slaves. We can make bonds full of
3720 * bonding devices to test this, however.
3721 */
3722static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3723{
3724 struct bonding *bond = bond_dev->priv;
3725 struct sockaddr *sa = addr, tmp_sa;
3726 struct slave *slave, *stop_at;
3727 int res = 0;
3728 int i;
3729
3730 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3731
3732 if (!is_valid_ether_addr(sa->sa_data)) {
3733 return -EADDRNOTAVAIL;
3734 }
3735
3736 /* Can't hold bond->lock with bh disabled here since
3737 * some base drivers panic. On the other hand we can't
3738 * hold bond->lock without bh disabled because we'll
3739 * deadlock. The only solution is to rely on the fact
3740 * that we're under rtnl_lock here, and the slaves
3741 * list won't change. This doesn't solve the problem
3742 * of setting the slave's hw address while it is
3743 * transmitting, but the assumption is that the base
3744 * driver can handle that.
3745 *
3746 * TODO: figure out a way to safely iterate the slaves
3747 * list, but without holding a lock around the actual
3748 * call to the base driver.
3749 */
3750
3751 bond_for_each_slave(bond, slave, i) {
3752 dprintk("slave %p %s\n", slave, slave->dev->name);
3753
3754 if (slave->dev->set_mac_address == NULL) {
3755 res = -EOPNOTSUPP;
3756 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
3757 goto unwind;
3758 }
3759
3760 res = dev_set_mac_address(slave->dev, addr);
3761 if (res) {
3762 /* TODO: consider downing the slave
3763 * and retry ?
3764 * User should expect communications
3765 * breakage anyway until ARP finish
3766 * updating, so...
3767 */
3768 dprintk("err %d %s\n", res, slave->dev->name);
3769 goto unwind;
3770 }
3771 }
3772
3773 /* success */
3774 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3775 return 0;
3776
3777unwind:
3778 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3779 tmp_sa.sa_family = bond_dev->type;
3780
3781 /* unwind from head to the slave that failed */
3782 stop_at = slave;
3783 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3784 int tmp_res;
3785
3786 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3787 if (tmp_res) {
3788 dprintk("unwind err %d dev %s\n", tmp_res,
3789 slave->dev->name);
3790 }
3791 }
3792
3793 return res;
3794}
3795
3796static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3797{
3798 struct bonding *bond = bond_dev->priv;
3799 struct slave *slave, *start_at;
3800 int i;
3801 int res = 1;
3802
3803 read_lock(&bond->lock);
3804
3805 if (!BOND_IS_OK(bond)) {
3806 goto out;
3807 }
3808
3809 read_lock(&bond->curr_slave_lock);
3810 slave = start_at = bond->curr_active_slave;
3811 read_unlock(&bond->curr_slave_lock);
3812
3813 if (!slave) {
3814 goto out;
3815 }
3816
3817 bond_for_each_slave_from(bond, slave, i, start_at) {
3818 if (IS_UP(slave->dev) &&
3819 (slave->link == BOND_LINK_UP) &&
3820 (slave->state == BOND_STATE_ACTIVE)) {
3821 res = bond_dev_queue_xmit(bond, skb, slave->dev);
3822
3823 write_lock(&bond->curr_slave_lock);
3824 bond->curr_active_slave = slave->next;
3825 write_unlock(&bond->curr_slave_lock);
3826
3827 break;
3828 }
3829 }
3830
3831
3832out:
3833 if (res) {
3834 /* no suitable interface, frame not sent */
3835 dev_kfree_skb(skb);
3836 }
3837 read_unlock(&bond->lock);
3838 return 0;
3839}
3840
075897ce 3841static void bond_activebackup_xmit_copy(struct sk_buff *skb,
df49898a
JL
3842 struct bonding *bond,
3843 struct slave *slave)
075897ce
JL
3844{
3845 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
3846 struct ethhdr *eth_data;
3847 u8 *hwaddr;
3848 int res;
3849
3850 if (!skb2) {
3851 printk(KERN_ERR DRV_NAME ": Error: "
3852 "bond_activebackup_xmit_copy(): skb_copy() failed\n");
3853 return;
3854 }
3855
3856 skb2->mac.raw = (unsigned char *)skb2->data;
3857 eth_data = eth_hdr(skb2);
3858
df49898a
JL
3859 /* Pick an appropriate source MAC address
3860 * -- use slave's perm MAC addr, unless used by bond
3861 * -- otherwise, borrow active slave's perm MAC addr
3862 * since that will not be used
3863 */
075897ce
JL
3864 hwaddr = slave->perm_hwaddr;
3865 if (!memcmp(eth_data->h_source, hwaddr, ETH_ALEN))
3866 hwaddr = bond->curr_active_slave->perm_hwaddr;
3867
3868 /* Set source MAC address appropriately */
3869 memcpy(eth_data->h_source, hwaddr, ETH_ALEN);
3870
3871 res = bond_dev_queue_xmit(bond, skb2, slave->dev);
3872 if (res)
3873 dev_kfree_skb(skb2);
3874
3875 return;
3876}
3877
1da177e4
LT
3878/*
3879 * in active-backup mode, we know that bond->curr_active_slave is always valid if
3880 * the bond has a usable interface.
3881 */
3882static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
3883{
3884 struct bonding *bond = bond_dev->priv;
3885 int res = 1;
3886
1da177e4
LT
3887 read_lock(&bond->lock);
3888 read_lock(&bond->curr_slave_lock);
3889
3890 if (!BOND_IS_OK(bond)) {
3891 goto out;
3892 }
3893
075897ce
JL
3894 if (!bond->curr_active_slave)
3895 goto out;
3896
3897 /* Xmit IGMP frames on all slaves to ensure rapid fail-over
3898 for multicast traffic on snooping switches */
3899 if (skb->protocol == __constant_htons(ETH_P_IP) &&
3900 skb->nh.iph->protocol == IPPROTO_IGMP) {
3901 struct slave *slave, *active_slave;
3902 int i;
3903
3904 active_slave = bond->curr_active_slave;
3905 bond_for_each_slave_from_to(bond, slave, i, active_slave->next,
3906 active_slave->prev)
3907 if (IS_UP(slave->dev) &&
3908 (slave->link == BOND_LINK_UP))
3909 bond_activebackup_xmit_copy(skb, bond, slave);
1da177e4
LT
3910 }
3911
075897ce
JL
3912 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
3913
1da177e4
LT
3914out:
3915 if (res) {
3916 /* no suitable interface, frame not sent */
3917 dev_kfree_skb(skb);
3918 }
3919 read_unlock(&bond->curr_slave_lock);
3920 read_unlock(&bond->lock);
3921 return 0;
3922}
3923
3924/*
169a3e66
JV
3925 * In bond_xmit_xor() , we determine the output device by using a pre-
3926 * determined xmit_hash_policy(), If the selected device is not enabled,
3927 * find the next active slave.
1da177e4
LT
3928 */
3929static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
3930{
3931 struct bonding *bond = bond_dev->priv;
1da177e4
LT
3932 struct slave *slave, *start_at;
3933 int slave_no;
3934 int i;
3935 int res = 1;
3936
3937 read_lock(&bond->lock);
3938
3939 if (!BOND_IS_OK(bond)) {
3940 goto out;
3941 }
3942
169a3e66 3943 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
1da177e4
LT
3944
3945 bond_for_each_slave(bond, slave, i) {
3946 slave_no--;
3947 if (slave_no < 0) {
3948 break;
3949 }
3950 }
3951
3952 start_at = slave;
3953
3954 bond_for_each_slave_from(bond, slave, i, start_at) {
3955 if (IS_UP(slave->dev) &&
3956 (slave->link == BOND_LINK_UP) &&
3957 (slave->state == BOND_STATE_ACTIVE)) {
3958 res = bond_dev_queue_xmit(bond, skb, slave->dev);
3959 break;
3960 }
3961 }
3962
3963out:
3964 if (res) {
3965 /* no suitable interface, frame not sent */
3966 dev_kfree_skb(skb);
3967 }
3968 read_unlock(&bond->lock);
3969 return 0;
3970}
3971
3972/*
3973 * in broadcast mode, we send everything to all usable interfaces.
3974 */
3975static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
3976{
3977 struct bonding *bond = bond_dev->priv;
3978 struct slave *slave, *start_at;
3979 struct net_device *tx_dev = NULL;
3980 int i;
3981 int res = 1;
3982
3983 read_lock(&bond->lock);
3984
3985 if (!BOND_IS_OK(bond)) {
3986 goto out;
3987 }
3988
3989 read_lock(&bond->curr_slave_lock);
3990 start_at = bond->curr_active_slave;
3991 read_unlock(&bond->curr_slave_lock);
3992
3993 if (!start_at) {
3994 goto out;
3995 }
3996
3997 bond_for_each_slave_from(bond, slave, i, start_at) {
3998 if (IS_UP(slave->dev) &&
3999 (slave->link == BOND_LINK_UP) &&
4000 (slave->state == BOND_STATE_ACTIVE)) {
4001 if (tx_dev) {
4002 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4003 if (!skb2) {
4004 printk(KERN_ERR DRV_NAME
4e0952c7
MW
4005 ": %s: Error: bond_xmit_broadcast(): "
4006 "skb_clone() failed\n",
4007 bond_dev->name);
1da177e4
LT
4008 continue;
4009 }
4010
4011 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4012 if (res) {
4013 dev_kfree_skb(skb2);
4014 continue;
4015 }
4016 }
4017 tx_dev = slave->dev;
4018 }
4019 }
4020
4021 if (tx_dev) {
4022 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4023 }
4024
4025out:
4026 if (res) {
4027 /* no suitable interface, frame not sent */
4028 dev_kfree_skb(skb);
4029 }
4030 /* frame sent to all suitable interfaces */
4031 read_unlock(&bond->lock);
4032 return 0;
4033}
4034
4035/*------------------------- Device initialization ---------------------------*/
4036
4037/*
4038 * set bond mode specific net device operations
4039 */
a77b5325 4040void bond_set_mode_ops(struct bonding *bond, int mode)
1da177e4 4041{
169a3e66
JV
4042 struct net_device *bond_dev = bond->dev;
4043
1da177e4
LT
4044 switch (mode) {
4045 case BOND_MODE_ROUNDROBIN:
4046 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4047 break;
4048 case BOND_MODE_ACTIVEBACKUP:
4049 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4050 break;
4051 case BOND_MODE_XOR:
4052 bond_dev->hard_start_xmit = bond_xmit_xor;
169a3e66
JV
4053 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4054 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4055 else
4056 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
1da177e4
LT
4057 break;
4058 case BOND_MODE_BROADCAST:
4059 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4060 break;
4061 case BOND_MODE_8023AD:
8f903c70 4062 bond_set_master_3ad_flags(bond);
1da177e4 4063 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
169a3e66
JV
4064 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4065 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4066 else
4067 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
1da177e4 4068 break;
1da177e4 4069 case BOND_MODE_ALB:
8f903c70
JV
4070 bond_set_master_alb_flags(bond);
4071 /* FALLTHRU */
4072 case BOND_MODE_TLB:
1da177e4
LT
4073 bond_dev->hard_start_xmit = bond_alb_xmit;
4074 bond_dev->set_mac_address = bond_alb_set_mac_address;
4075 break;
4076 default:
4077 /* Should never happen, mode already checked */
4078 printk(KERN_ERR DRV_NAME
4e0952c7
MW
4079 ": %s: Error: Unknown bonding mode %d\n",
4080 bond_dev->name,
1da177e4
LT
4081 mode);
4082 break;
4083 }
4084}
4085
217df670
JV
4086static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4087 struct ethtool_drvinfo *drvinfo)
4088{
4089 strncpy(drvinfo->driver, DRV_NAME, 32);
4090 strncpy(drvinfo->version, DRV_VERSION, 32);
4091 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4092}
4093
8531c5ff
AK
4094static struct ethtool_ops bond_ethtool_ops = {
4095 .get_tx_csum = ethtool_op_get_tx_csum,
a0de3adf
JV
4096 .get_tso = ethtool_op_get_tso,
4097 .get_ufo = ethtool_op_get_ufo,
8531c5ff 4098 .get_sg = ethtool_op_get_sg,
217df670 4099 .get_drvinfo = bond_ethtool_get_drvinfo,
8531c5ff
AK
4100};
4101
1da177e4
LT
4102/*
4103 * Does not allocate but creates a /proc entry.
4104 * Allowed to fail.
4105 */
3c535952 4106static int bond_init(struct net_device *bond_dev, struct bond_params *params)
1da177e4
LT
4107{
4108 struct bonding *bond = bond_dev->priv;
4109
4110 dprintk("Begin bond_init for %s\n", bond_dev->name);
4111
4112 /* initialize rwlocks */
4113 rwlock_init(&bond->lock);
4114 rwlock_init(&bond->curr_slave_lock);
4115
4116 bond->params = *params; /* copy params struct */
4117
4118 /* Initialize pointers */
4119 bond->first_slave = NULL;
4120 bond->curr_active_slave = NULL;
4121 bond->current_arp_slave = NULL;
4122 bond->primary_slave = NULL;
4123 bond->dev = bond_dev;
4124 INIT_LIST_HEAD(&bond->vlan_list);
4125
4126 /* Initialize the device entry points */
4127 bond_dev->open = bond_open;
4128 bond_dev->stop = bond_close;
4129 bond_dev->get_stats = bond_get_stats;
4130 bond_dev->do_ioctl = bond_do_ioctl;
8531c5ff 4131 bond_dev->ethtool_ops = &bond_ethtool_ops;
1da177e4
LT
4132 bond_dev->set_multicast_list = bond_set_multicast_list;
4133 bond_dev->change_mtu = bond_change_mtu;
4134 bond_dev->set_mac_address = bond_set_mac_address;
4135
169a3e66 4136 bond_set_mode_ops(bond, bond->params.mode);
1da177e4
LT
4137
4138 bond_dev->destructor = free_netdev;
4139
4140 /* Initialize the device options */
4141 bond_dev->tx_queue_len = 0;
4142 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4143
4144 /* At first, we block adding VLANs. That's the only way to
4145 * prevent problems that occur when adding VLANs over an
4146 * empty bond. The block will be removed once non-challenged
4147 * slaves are enslaved.
4148 */
4149 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4150
4151 /* don't acquire bond device's xmit_lock when
4152 * transmitting */
4153 bond_dev->features |= NETIF_F_LLTX;
4154
4155 /* By default, we declare the bond to be fully
4156 * VLAN hardware accelerated capable. Special
4157 * care is taken in the various xmit functions
4158 * when there are slaves that are not hw accel
4159 * capable
4160 */
4161 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4162 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4163 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4164 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4165 NETIF_F_HW_VLAN_RX |
4166 NETIF_F_HW_VLAN_FILTER);
4167
4168#ifdef CONFIG_PROC_FS
4169 bond_create_proc_entry(bond);
4170#endif
4171
4172 list_add_tail(&bond->bond_list, &bond_dev_list);
4173
4174 return 0;
4175}
4176
4177/* De-initialize device specific data.
4178 * Caller must hold rtnl_lock.
4179 */
a77b5325 4180void bond_deinit(struct net_device *bond_dev)
1da177e4
LT
4181{
4182 struct bonding *bond = bond_dev->priv;
4183
4184 list_del(&bond->bond_list);
4185
4186#ifdef CONFIG_PROC_FS
4187 bond_remove_proc_entry(bond);
4188#endif
4189}
4190
4191/* Unregister and free all bond devices.
4192 * Caller must hold rtnl_lock.
4193 */
4194static void bond_free_all(void)
4195{
4196 struct bonding *bond, *nxt;
4197
4198 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4199 struct net_device *bond_dev = bond->dev;
4200
4201 unregister_netdevice(bond_dev);
4202 bond_deinit(bond_dev);
4203 }
4204
4205#ifdef CONFIG_PROC_FS
4206 bond_destroy_proc_dir();
4207#endif
4208}
4209
4210/*------------------------- Module initialization ---------------------------*/
4211
4212/*
4213 * Convert string input module parms. Accept either the
4214 * number of the mode or its string name.
4215 */
a77b5325 4216int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
1da177e4
LT
4217{
4218 int i;
4219
4220 for (i = 0; tbl[i].modename; i++) {
4221 if ((isdigit(*mode_arg) &&
4222 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4223 (strncmp(mode_arg, tbl[i].modename,
4224 strlen(tbl[i].modename)) == 0)) {
4225 return tbl[i].mode;
4226 }
4227 }
4228
4229 return -1;
4230}
4231
4232static int bond_check_params(struct bond_params *params)
4233{
4234 /*
4235 * Convert string parameters.
4236 */
4237 if (mode) {
4238 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4239 if (bond_mode == -1) {
4240 printk(KERN_ERR DRV_NAME
4241 ": Error: Invalid bonding mode \"%s\"\n",
4242 mode == NULL ? "NULL" : mode);
4243 return -EINVAL;
4244 }
4245 }
4246
169a3e66
JV
4247 if (xmit_hash_policy) {
4248 if ((bond_mode != BOND_MODE_XOR) &&
4249 (bond_mode != BOND_MODE_8023AD)) {
4250 printk(KERN_INFO DRV_NAME
4251 ": xor_mode param is irrelevant in mode %s\n",
4252 bond_mode_name(bond_mode));
4253 } else {
4254 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4255 xmit_hashtype_tbl);
4256 if (xmit_hashtype == -1) {
4257 printk(KERN_ERR DRV_NAME
4258 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4259 xmit_hash_policy == NULL ? "NULL" :
4260 xmit_hash_policy);
4261 return -EINVAL;
4262 }
4263 }
4264 }
4265
1da177e4
LT
4266 if (lacp_rate) {
4267 if (bond_mode != BOND_MODE_8023AD) {
4268 printk(KERN_INFO DRV_NAME
4269 ": lacp_rate param is irrelevant in mode %s\n",
4270 bond_mode_name(bond_mode));
4271 } else {
4272 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4273 if (lacp_fast == -1) {
4274 printk(KERN_ERR DRV_NAME
4275 ": Error: Invalid lacp rate \"%s\"\n",
4276 lacp_rate == NULL ? "NULL" : lacp_rate);
4277 return -EINVAL;
4278 }
4279 }
4280 }
4281
4282 if (max_bonds < 1 || max_bonds > INT_MAX) {
4283 printk(KERN_WARNING DRV_NAME
4284 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4e0952c7 4285 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
1da177e4
LT
4286 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4287 max_bonds = BOND_DEFAULT_MAX_BONDS;
4288 }
4289
4290 if (miimon < 0) {
4291 printk(KERN_WARNING DRV_NAME
4292 ": Warning: miimon module parameter (%d), "
4293 "not in range 0-%d, so it was reset to %d\n",
4294 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4295 miimon = BOND_LINK_MON_INTERV;
4296 }
4297
4298 if (updelay < 0) {
4299 printk(KERN_WARNING DRV_NAME
4300 ": Warning: updelay module parameter (%d), "
4301 "not in range 0-%d, so it was reset to 0\n",
4302 updelay, INT_MAX);
4303 updelay = 0;
4304 }
4305
4306 if (downdelay < 0) {
4307 printk(KERN_WARNING DRV_NAME
4308 ": Warning: downdelay module parameter (%d), "
4309 "not in range 0-%d, so it was reset to 0\n",
4310 downdelay, INT_MAX);
4311 downdelay = 0;
4312 }
4313
4314 if ((use_carrier != 0) && (use_carrier != 1)) {
4315 printk(KERN_WARNING DRV_NAME
4316 ": Warning: use_carrier module parameter (%d), "
4317 "not of valid value (0/1), so it was set to 1\n",
4318 use_carrier);
4319 use_carrier = 1;
4320 }
4321
4322 /* reset values for 802.3ad */
4323 if (bond_mode == BOND_MODE_8023AD) {
4324 if (!miimon) {
4325 printk(KERN_WARNING DRV_NAME
4326 ": Warning: miimon must be specified, "
4327 "otherwise bonding will not detect link "
4328 "failure, speed and duplex which are "
4329 "essential for 802.3ad operation\n");
4330 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4331 miimon = 100;
4332 }
4333 }
4334
4335 /* reset values for TLB/ALB */
4336 if ((bond_mode == BOND_MODE_TLB) ||
4337 (bond_mode == BOND_MODE_ALB)) {
4338 if (!miimon) {
4339 printk(KERN_WARNING DRV_NAME
4340 ": Warning: miimon must be specified, "
4341 "otherwise bonding will not detect link "
4342 "failure and link speed which are essential "
4343 "for TLB/ALB load balancing\n");
4344 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4345 miimon = 100;
4346 }
4347 }
4348
4349 if (bond_mode == BOND_MODE_ALB) {
4350 printk(KERN_NOTICE DRV_NAME
4351 ": In ALB mode you might experience client "
4352 "disconnections upon reconnection of a link if the "
4353 "bonding module updelay parameter (%d msec) is "
4354 "incompatible with the forwarding delay time of the "
4355 "switch\n",
4356 updelay);
4357 }
4358
4359 if (!miimon) {
4360 if (updelay || downdelay) {
4361 /* just warn the user the up/down delay will have
4362 * no effect since miimon is zero...
4363 */
4364 printk(KERN_WARNING DRV_NAME
4365 ": Warning: miimon module parameter not set "
4366 "and updelay (%d) or downdelay (%d) module "
4367 "parameter is set; updelay and downdelay have "
4368 "no effect unless miimon is set\n",
4369 updelay, downdelay);
4370 }
4371 } else {
4372 /* don't allow arp monitoring */
4373 if (arp_interval) {
4374 printk(KERN_WARNING DRV_NAME
4375 ": Warning: miimon (%d) and arp_interval (%d) "
4376 "can't be used simultaneously, disabling ARP "
4377 "monitoring\n",
4378 miimon, arp_interval);
4379 arp_interval = 0;
4380 }
4381
4382 if ((updelay % miimon) != 0) {
4383 printk(KERN_WARNING DRV_NAME
4384 ": Warning: updelay (%d) is not a multiple "
4385 "of miimon (%d), updelay rounded to %d ms\n",
4386 updelay, miimon, (updelay / miimon) * miimon);
4387 }
4388
4389 updelay /= miimon;
4390
4391 if ((downdelay % miimon) != 0) {
4392 printk(KERN_WARNING DRV_NAME
4393 ": Warning: downdelay (%d) is not a multiple "
4394 "of miimon (%d), downdelay rounded to %d ms\n",
4395 downdelay, miimon,
4396 (downdelay / miimon) * miimon);
4397 }
4398
4399 downdelay /= miimon;
4400 }
4401
4402 if (arp_interval < 0) {
4403 printk(KERN_WARNING DRV_NAME
4404 ": Warning: arp_interval module parameter (%d) "
4405 ", not in range 0-%d, so it was reset to %d\n",
4406 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4407 arp_interval = BOND_LINK_ARP_INTERV;
4408 }
4409
4410 for (arp_ip_count = 0;
4411 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4412 arp_ip_count++) {
4413 /* not complete check, but should be good enough to
4414 catch mistakes */
4415 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4416 printk(KERN_WARNING DRV_NAME
4417 ": Warning: bad arp_ip_target module parameter "
4418 "(%s), ARP monitoring will not be performed\n",
4419 arp_ip_target[arp_ip_count]);
4420 arp_interval = 0;
4421 } else {
4422 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4423 arp_target[arp_ip_count] = ip;
4424 }
4425 }
4426
4427 if (arp_interval && !arp_ip_count) {
4428 /* don't allow arping if no arp_ip_target given... */
4429 printk(KERN_WARNING DRV_NAME
4430 ": Warning: arp_interval module parameter (%d) "
4431 "specified without providing an arp_ip_target "
4432 "parameter, arp_interval was reset to 0\n",
4433 arp_interval);
4434 arp_interval = 0;
4435 }
4436
4437 if (miimon) {
4438 printk(KERN_INFO DRV_NAME
4439 ": MII link monitoring set to %d ms\n",
4440 miimon);
4441 } else if (arp_interval) {
4442 int i;
4443
4444 printk(KERN_INFO DRV_NAME
4445 ": ARP monitoring set to %d ms with %d target(s):",
4446 arp_interval, arp_ip_count);
4447
4448 for (i = 0; i < arp_ip_count; i++)
4449 printk (" %s", arp_ip_target[i]);
4450
4451 printk("\n");
4452
4453 } else {
4454 /* miimon and arp_interval not set, we need one so things
4455 * work as expected, see bonding.txt for details
4456 */
4457 printk(KERN_WARNING DRV_NAME
4458 ": Warning: either miimon or arp_interval and "
4459 "arp_ip_target module parameters must be specified, "
4460 "otherwise bonding will not detect link failures! see "
4461 "bonding.txt for details.\n");
4462 }
4463
4464 if (primary && !USES_PRIMARY(bond_mode)) {
4465 /* currently, using a primary only makes sense
4466 * in active backup, TLB or ALB modes
4467 */
4468 printk(KERN_WARNING DRV_NAME
4469 ": Warning: %s primary device specified but has no "
4470 "effect in %s mode\n",
4471 primary, bond_mode_name(bond_mode));
4472 primary = NULL;
4473 }
4474
4475 /* fill params struct with the proper values */
4476 params->mode = bond_mode;
169a3e66 4477 params->xmit_policy = xmit_hashtype;
1da177e4
LT
4478 params->miimon = miimon;
4479 params->arp_interval = arp_interval;
4480 params->updelay = updelay;
4481 params->downdelay = downdelay;
4482 params->use_carrier = use_carrier;
4483 params->lacp_fast = lacp_fast;
4484 params->primary[0] = 0;
4485
4486 if (primary) {
4487 strncpy(params->primary, primary, IFNAMSIZ);
4488 params->primary[IFNAMSIZ - 1] = 0;
4489 }
4490
4491 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4492
4493 return 0;
4494}
4495
dfe60397
MW
4496/* Create a new bond based on the specified name and bonding parameters.
4497 * Caller must NOT hold rtnl_lock; we need to release it here before we
4498 * set up our sysfs entries.
4499 */
4500int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4501{
4502 struct net_device *bond_dev;
4503 int res;
4504
4505 rtnl_lock();
4506 bond_dev = alloc_netdev(sizeof(struct bonding), name, ether_setup);
4507 if (!bond_dev) {
4508 printk(KERN_ERR DRV_NAME
4509 ": %s: eek! can't alloc netdev!\n",
4510 name);
4511 res = -ENOMEM;
4512 goto out_rtnl;
4513 }
4514
4515 /* bond_init() must be called after dev_alloc_name() (for the
4516 * /proc files), but before register_netdevice(), because we
4517 * need to set function pointers.
4518 */
4519
4520 res = bond_init(bond_dev, params);
4521 if (res < 0) {
4522 goto out_netdev;
4523 }
4524
4525 SET_MODULE_OWNER(bond_dev);
4526
4527 res = register_netdevice(bond_dev);
4528 if (res < 0) {
4529 goto out_bond;
4530 }
4531 if (newbond)
4532 *newbond = bond_dev->priv;
4533
4534 rtnl_unlock(); /* allows sysfs registration of net device */
b76cdba9 4535 res = bond_create_sysfs_entry(bond_dev->priv);
dfe60397
MW
4536 goto done;
4537out_bond:
4538 bond_deinit(bond_dev);
4539out_netdev:
4540 free_netdev(bond_dev);
4541out_rtnl:
4542 rtnl_unlock();
4543done:
4544 return res;
4545}
4546
1da177e4
LT
4547static int __init bonding_init(void)
4548{
1da177e4
LT
4549 int i;
4550 int res;
dfe60397 4551 char new_bond_name[8]; /* Enough room for 999 bonds at init. */
1da177e4
LT
4552
4553 printk(KERN_INFO "%s", version);
4554
dfe60397 4555 res = bond_check_params(&bonding_defaults);
1da177e4 4556 if (res) {
dfe60397 4557 goto out;
1da177e4
LT
4558 }
4559
1da177e4
LT
4560#ifdef CONFIG_PROC_FS
4561 bond_create_proc_dir();
4562#endif
1da177e4 4563 for (i = 0; i < max_bonds; i++) {
dfe60397
MW
4564 sprintf(new_bond_name, "bond%d",i);
4565 res = bond_create(new_bond_name,&bonding_defaults, NULL);
4566 if (res)
4567 goto err;
1da177e4
LT
4568 }
4569
b76cdba9
MW
4570 res = bond_create_sysfs();
4571 if (res)
4572 goto err;
4573
1da177e4 4574 register_netdevice_notifier(&bond_netdev_notifier);
c3ade5ca 4575 register_inetaddr_notifier(&bond_inetaddr_notifier);
1da177e4 4576
dfe60397
MW
4577 goto out;
4578err:
40abc270 4579 rtnl_lock();
1da177e4 4580 bond_free_all();
b76cdba9 4581 bond_destroy_sysfs();
1da177e4 4582 rtnl_unlock();
dfe60397 4583out:
1da177e4 4584 return res;
dfe60397 4585
1da177e4
LT
4586}
4587
4588static void __exit bonding_exit(void)
4589{
4590 unregister_netdevice_notifier(&bond_netdev_notifier);
c3ade5ca 4591 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
1da177e4
LT
4592
4593 rtnl_lock();
4594 bond_free_all();
b76cdba9 4595 bond_destroy_sysfs();
1da177e4
LT
4596 rtnl_unlock();
4597}
4598
4599module_init(bonding_init);
4600module_exit(bonding_exit);
4601MODULE_LICENSE("GPL");
4602MODULE_VERSION(DRV_VERSION);
4603MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4604MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4605MODULE_SUPPORTED_DEVICE("most ethernet devices");
4606
4607/*
4608 * Local variables:
4609 * c-indent-level: 8
4610 * c-basic-offset: 8
4611 * tab-width: 8
4612 * End:
4613 */
4614