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