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