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