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