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