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