net/bonding: Enable bonding to enslave non ARPHRD_ETHER
[GitHub/mt8127/android_kernel_alcatel_ttab.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 }
1099 bond_send_gratuitous_arp(bond);
1100 }
1da177e4
LT
1101}
1102
1103/**
1104 * bond_select_active_slave - select a new active slave, if needed
1105 * @bond: our bonding struct
1106 *
1107 * This functions shoud be called when one of the following occurs:
1108 * - The old curr_active_slave has been released or lost its link.
1109 * - The primary_slave has got its link back.
1110 * - A slave has got its link back and there's no old curr_active_slave.
1111 *
1112 * Warning: Caller must hold curr_slave_lock for writing.
1113 */
a77b5325 1114void bond_select_active_slave(struct bonding *bond)
1da177e4
LT
1115{
1116 struct slave *best_slave;
ff59c456 1117 int rv;
1da177e4
LT
1118
1119 best_slave = bond_find_best_slave(bond);
1120 if (best_slave != bond->curr_active_slave) {
1121 bond_change_active_slave(bond, best_slave);
ff59c456
JV
1122 rv = bond_set_carrier(bond);
1123 if (!rv)
1124 return;
1125
1126 if (netif_carrier_ok(bond->dev)) {
1127 printk(KERN_INFO DRV_NAME
1128 ": %s: first active interface up!\n",
1129 bond->dev->name);
1130 } else {
1131 printk(KERN_INFO DRV_NAME ": %s: "
1132 "now running without any active interface !\n",
1133 bond->dev->name);
1134 }
1da177e4
LT
1135 }
1136}
1137
1138/*--------------------------- slave list handling ---------------------------*/
1139
1140/*
1141 * This function attaches the slave to the end of list.
1142 *
1143 * bond->lock held for writing by caller.
1144 */
1145static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1146{
1147 if (bond->first_slave == NULL) { /* attaching the first slave */
1148 new_slave->next = new_slave;
1149 new_slave->prev = new_slave;
1150 bond->first_slave = new_slave;
1151 } else {
1152 new_slave->next = bond->first_slave;
1153 new_slave->prev = bond->first_slave->prev;
1154 new_slave->next->prev = new_slave;
1155 new_slave->prev->next = new_slave;
1156 }
1157
1158 bond->slave_cnt++;
1159}
1160
1161/*
1162 * This function detaches the slave from the list.
1163 * WARNING: no check is made to verify if the slave effectively
1164 * belongs to <bond>.
1165 * Nothing is freed on return, structures are just unchained.
1166 * If any slave pointer in bond was pointing to <slave>,
1167 * it should be changed by the calling function.
1168 *
1169 * bond->lock held for writing by caller.
1170 */
1171static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1172{
1173 if (slave->next) {
1174 slave->next->prev = slave->prev;
1175 }
1176
1177 if (slave->prev) {
1178 slave->prev->next = slave->next;
1179 }
1180
1181 if (bond->first_slave == slave) { /* slave is the first slave */
1182 if (bond->slave_cnt > 1) { /* there are more slave */
1183 bond->first_slave = slave->next;
1184 } else {
1185 bond->first_slave = NULL; /* slave was the last one */
1186 }
1187 }
1188
1189 slave->next = NULL;
1190 slave->prev = NULL;
1191 bond->slave_cnt--;
1192}
1193
1194/*---------------------------------- IOCTL ----------------------------------*/
1195
4ad072c9
AB
1196static int bond_sethwaddr(struct net_device *bond_dev,
1197 struct net_device *slave_dev)
1da177e4
LT
1198{
1199 dprintk("bond_dev=%p\n", bond_dev);
1200 dprintk("slave_dev=%p\n", slave_dev);
1201 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1202 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1203 return 0;
1204}
1205
7f353bf2
HX
1206#define BOND_VLAN_FEATURES \
1207 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1208 NETIF_F_HW_VLAN_FILTER)
8531c5ff
AK
1209
1210/*
8e3babcd 1211 * Compute the common dev->feature set available to all slaves. Some
7f353bf2
HX
1212 * feature bits are managed elsewhere, so preserve those feature bits
1213 * on the master device.
8531c5ff
AK
1214 */
1215static int bond_compute_features(struct bonding *bond)
1216{
8531c5ff
AK
1217 struct slave *slave;
1218 struct net_device *bond_dev = bond->dev;
7f353bf2 1219 unsigned long features = bond_dev->features;
54ef3137 1220 unsigned short max_hard_header_len = ETH_HLEN;
8e3babcd 1221 int i;
8531c5ff 1222
7f353bf2
HX
1223 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1224 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1225 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1226
54ef3137 1227 bond_for_each_slave(bond, slave, i) {
7f353bf2
HX
1228 features = netdev_compute_features(features,
1229 slave->dev->features);
54ef3137
JV
1230 if (slave->dev->hard_header_len > max_hard_header_len)
1231 max_hard_header_len = slave->dev->hard_header_len;
1232 }
8531c5ff 1233
7f353bf2 1234 features |= (bond_dev->features & BOND_VLAN_FEATURES);
8531c5ff 1235 bond_dev->features = features;
54ef3137 1236 bond_dev->hard_header_len = max_hard_header_len;
8531c5ff
AK
1237
1238 return 0;
1239}
1240
872254dd
MS
1241
1242static void bond_setup_by_slave(struct net_device *bond_dev,
1243 struct net_device *slave_dev)
1244{
1245 bond_dev->neigh_setup = slave_dev->neigh_setup;
1246
1247 bond_dev->type = slave_dev->type;
1248 bond_dev->hard_header_len = slave_dev->hard_header_len;
1249 bond_dev->addr_len = slave_dev->addr_len;
1250
1251 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1252 slave_dev->addr_len);
1253}
1254
1da177e4 1255/* enslave device <slave> to bond device <master> */
a77b5325 1256int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1257{
1258 struct bonding *bond = bond_dev->priv;
1259 struct slave *new_slave = NULL;
1260 struct dev_mc_list *dmi;
1261 struct sockaddr addr;
1262 int link_reporting;
1263 int old_features = bond_dev->features;
1264 int res = 0;
1265
552709d5 1266 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1267 slave_dev->do_ioctl == NULL) {
1da177e4 1268 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1269 ": %s: Warning: no link monitoring support for %s\n",
1270 bond_dev->name, slave_dev->name);
1da177e4
LT
1271 }
1272
1273 /* bond must be initialized by bond_open() before enslaving */
1274 if (!(bond_dev->flags & IFF_UP)) {
1275 dprintk("Error, master_dev is not up\n");
1276 return -EPERM;
1277 }
1278
1279 /* already enslaved */
1280 if (slave_dev->flags & IFF_SLAVE) {
1281 dprintk("Error, Device was already enslaved\n");
1282 return -EBUSY;
1283 }
1284
1285 /* vlan challenged mutual exclusion */
1286 /* no need to lock since we're protected by rtnl_lock */
1287 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1288 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1289 if (!list_empty(&bond->vlan_list)) {
1290 printk(KERN_ERR DRV_NAME
4e0952c7 1291 ": %s: Error: cannot enslave VLAN "
1da177e4 1292 "challenged slave %s on VLAN enabled "
4e0952c7 1293 "bond %s\n", bond_dev->name, slave_dev->name,
1da177e4
LT
1294 bond_dev->name);
1295 return -EPERM;
1296 } else {
1297 printk(KERN_WARNING DRV_NAME
4e0952c7 1298 ": %s: Warning: enslaved VLAN challenged "
1da177e4
LT
1299 "slave %s. Adding VLANs will be blocked as "
1300 "long as %s is part of bond %s\n",
4e0952c7 1301 bond_dev->name, slave_dev->name, slave_dev->name,
1da177e4
LT
1302 bond_dev->name);
1303 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1304 }
1305 } else {
1306 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1307 if (bond->slave_cnt == 0) {
1308 /* First slave, and it is not VLAN challenged,
1309 * so remove the block of adding VLANs over the bond.
1310 */
1311 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1312 }
1313 }
1314
217df670
JV
1315 /*
1316 * Old ifenslave binaries are no longer supported. These can
1317 * be identified with moderate accurary by the state of the slave:
1318 * the current ifenslave will set the interface down prior to
1319 * enslaving it; the old ifenslave will not.
1320 */
1321 if ((slave_dev->flags & IFF_UP)) {
1322 printk(KERN_ERR DRV_NAME ": %s is up. "
1323 "This may be due to an out of date ifenslave.\n",
1324 slave_dev->name);
1325 res = -EPERM;
1326 goto err_undo_flags;
1327 }
1da177e4 1328
872254dd
MS
1329 /* set bonding device ether type by slave - bonding netdevices are
1330 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1331 * there is a need to override some of the type dependent attribs/funcs.
1332 *
1333 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1334 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1335 */
1336 if (bond->slave_cnt == 0) {
1337 if (slave_dev->type != ARPHRD_ETHER)
1338 bond_setup_by_slave(bond_dev, slave_dev);
1339 } else if (bond_dev->type != slave_dev->type) {
1340 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1341 "from other slaves (%d), can not enslave it.\n",
1342 slave_dev->name,
1343 slave_dev->type, bond_dev->type);
1344 res = -EINVAL;
1345 goto err_undo_flags;
1346 }
1347
217df670
JV
1348 if (slave_dev->set_mac_address == NULL) {
1349 printk(KERN_ERR DRV_NAME
4e0952c7
MW
1350 ": %s: Error: The slave device you specified does "
1351 "not support setting the MAC address. "
1352 "Your kernel likely does not support slave "
1353 "devices.\n", bond_dev->name);
1354 res = -EOPNOTSUPP;
217df670 1355 goto err_undo_flags;
1da177e4
LT
1356 }
1357
243cb4e5 1358 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1da177e4
LT
1359 if (!new_slave) {
1360 res = -ENOMEM;
1361 goto err_undo_flags;
1362 }
1363
1da177e4
LT
1364 /* save slave's original flags before calling
1365 * netdev_set_master and dev_open
1366 */
1367 new_slave->original_flags = slave_dev->flags;
1368
217df670
JV
1369 /*
1370 * Save slave's original ("permanent") mac address for modes
1371 * that need it, and for restoring it upon release, and then
1372 * set it to the master's address
1373 */
1374 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1da177e4 1375
217df670
JV
1376 /*
1377 * Set slave to master's mac address. The application already
1378 * set the master's mac address to that of the first slave
1379 */
1380 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1381 addr.sa_family = slave_dev->type;
1382 res = dev_set_mac_address(slave_dev, &addr);
1383 if (res) {
1384 dprintk("Error %d calling set_mac_address\n", res);
1385 goto err_free;
1386 }
1da177e4 1387
c2edacf8
JV
1388 res = netdev_set_master(slave_dev, bond_dev);
1389 if (res) {
1390 dprintk("Error %d calling netdev_set_master\n", res);
1391 goto err_close;
1392 }
217df670
JV
1393 /* open the slave since the application closed it */
1394 res = dev_open(slave_dev);
1395 if (res) {
1396 dprintk("Openning slave %s failed\n", slave_dev->name);
1397 goto err_restore_mac;
1da177e4
LT
1398 }
1399
1da177e4 1400 new_slave->dev = slave_dev;
0b680e75 1401 slave_dev->priv_flags |= IFF_BONDING;
1da177e4
LT
1402
1403 if ((bond->params.mode == BOND_MODE_TLB) ||
1404 (bond->params.mode == BOND_MODE_ALB)) {
1405 /* bond_alb_init_slave() must be called before all other stages since
1406 * it might fail and we do not want to have to undo everything
1407 */
1408 res = bond_alb_init_slave(bond, new_slave);
1409 if (res) {
1410 goto err_unset_master;
1411 }
1412 }
1413
1414 /* If the mode USES_PRIMARY, then the new slave gets the
1415 * master's promisc (and mc) settings only if it becomes the
1416 * curr_active_slave, and that is taken care of later when calling
1417 * bond_change_active()
1418 */
1419 if (!USES_PRIMARY(bond->params.mode)) {
1420 /* set promiscuity level to new slave */
1421 if (bond_dev->flags & IFF_PROMISC) {
1422 dev_set_promiscuity(slave_dev, 1);
1423 }
1424
1425 /* set allmulti level to new slave */
1426 if (bond_dev->flags & IFF_ALLMULTI) {
1427 dev_set_allmulti(slave_dev, 1);
1428 }
1429
1430 /* upload master's mc_list to new slave */
1431 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1432 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1433 }
1434 }
1435
1436 if (bond->params.mode == BOND_MODE_8023AD) {
1437 /* add lacpdu mc addr to mc list */
1438 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1439
1440 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1441 }
1442
1443 bond_add_vlans_on_slave(bond, slave_dev);
1444
1445 write_lock_bh(&bond->lock);
1446
1447 bond_attach_slave(bond, new_slave);
1448
1449 new_slave->delay = 0;
1450 new_slave->link_failure_count = 0;
1451
8531c5ff
AK
1452 bond_compute_features(bond);
1453
f5b2b966
JV
1454 new_slave->last_arp_rx = jiffies;
1455
1da177e4
LT
1456 if (bond->params.miimon && !bond->params.use_carrier) {
1457 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1458
1459 if ((link_reporting == -1) && !bond->params.arp_interval) {
1460 /*
1461 * miimon is set but a bonded network driver
1462 * does not support ETHTOOL/MII and
1463 * arp_interval is not set. Note: if
1464 * use_carrier is enabled, we will never go
1465 * here (because netif_carrier is always
1466 * supported); thus, we don't need to change
1467 * the messages for netif_carrier.
1468 */
1469 printk(KERN_WARNING DRV_NAME
4e0952c7 1470 ": %s: Warning: MII and ETHTOOL support not "
1da177e4
LT
1471 "available for interface %s, and "
1472 "arp_interval/arp_ip_target module parameters "
1473 "not specified, thus bonding will not detect "
1474 "link failures! see bonding.txt for details.\n",
4e0952c7 1475 bond_dev->name, slave_dev->name);
1da177e4
LT
1476 } else if (link_reporting == -1) {
1477 /* unable get link status using mii/ethtool */
1478 printk(KERN_WARNING DRV_NAME
4e0952c7 1479 ": %s: Warning: can't get link status from "
1da177e4
LT
1480 "interface %s; the network driver associated "
1481 "with this interface does not support MII or "
1482 "ETHTOOL link status reporting, thus miimon "
1483 "has no effect on this interface.\n",
4e0952c7 1484 bond_dev->name, slave_dev->name);
1da177e4
LT
1485 }
1486 }
1487
1488 /* check for initial state */
1489 if (!bond->params.miimon ||
1490 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1491 if (bond->params.updelay) {
1492 dprintk("Initial state of slave_dev is "
1493 "BOND_LINK_BACK\n");
1494 new_slave->link = BOND_LINK_BACK;
1495 new_slave->delay = bond->params.updelay;
1496 } else {
1497 dprintk("Initial state of slave_dev is "
1498 "BOND_LINK_UP\n");
1499 new_slave->link = BOND_LINK_UP;
1500 }
1501 new_slave->jiffies = jiffies;
1502 } else {
1503 dprintk("Initial state of slave_dev is "
1504 "BOND_LINK_DOWN\n");
1505 new_slave->link = BOND_LINK_DOWN;
1506 }
1507
1508 if (bond_update_speed_duplex(new_slave) &&
1509 (new_slave->link != BOND_LINK_DOWN)) {
1510 printk(KERN_WARNING DRV_NAME
4e0952c7 1511 ": %s: Warning: failed to get speed and duplex from %s, "
1da177e4 1512 "assumed to be 100Mb/sec and Full.\n",
4e0952c7 1513 bond_dev->name, new_slave->dev->name);
1da177e4
LT
1514
1515 if (bond->params.mode == BOND_MODE_8023AD) {
4e0952c7
MW
1516 printk(KERN_WARNING DRV_NAME
1517 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1da177e4 1518 "support in base driver for proper aggregator "
4e0952c7 1519 "selection.\n", bond_dev->name);
1da177e4
LT
1520 }
1521 }
1522
1523 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1524 /* if there is a primary slave, remember it */
1525 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1526 bond->primary_slave = new_slave;
1527 }
1528 }
1529
1530 switch (bond->params.mode) {
1531 case BOND_MODE_ACTIVEBACKUP:
8a8e447b
JV
1532 bond_set_slave_inactive_flags(new_slave);
1533 bond_select_active_slave(bond);
1da177e4
LT
1534 break;
1535 case BOND_MODE_8023AD:
1536 /* in 802.3ad mode, the internal mechanism
1537 * will activate the slaves in the selected
1538 * aggregator
1539 */
1540 bond_set_slave_inactive_flags(new_slave);
1541 /* if this is the first slave */
1542 if (bond->slave_cnt == 1) {
1543 SLAVE_AD_INFO(new_slave).id = 1;
1544 /* Initialize AD with the number of times that the AD timer is called in 1 second
1545 * can be called only after the mac address of the bond is set
1546 */
1547 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1548 bond->params.lacp_fast);
1549 } else {
1550 SLAVE_AD_INFO(new_slave).id =
1551 SLAVE_AD_INFO(new_slave->prev).id + 1;
1552 }
1553
1554 bond_3ad_bind_slave(new_slave);
1555 break;
1556 case BOND_MODE_TLB:
1557 case BOND_MODE_ALB:
1558 new_slave->state = BOND_STATE_ACTIVE;
1559 if ((!bond->curr_active_slave) &&
1560 (new_slave->link != BOND_LINK_DOWN)) {
1561 /* first slave or no active slave yet, and this link
1562 * is OK, so make this interface the active one
1563 */
1564 bond_change_active_slave(bond, new_slave);
8f903c70
JV
1565 } else {
1566 bond_set_slave_inactive_flags(new_slave);
1da177e4
LT
1567 }
1568 break;
1569 default:
1570 dprintk("This slave is always active in trunk mode\n");
1571
1572 /* always active in trunk mode */
1573 new_slave->state = BOND_STATE_ACTIVE;
1574
1575 /* In trunking mode there is little meaning to curr_active_slave
1576 * anyway (it holds no special properties of the bond device),
1577 * so we can change it without calling change_active_interface()
1578 */
1579 if (!bond->curr_active_slave) {
1580 bond->curr_active_slave = new_slave;
1581 }
1582 break;
1583 } /* switch(bond_mode) */
1584
ff59c456
JV
1585 bond_set_carrier(bond);
1586
1da177e4
LT
1587 write_unlock_bh(&bond->lock);
1588
b76cdba9
MW
1589 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1590 if (res)
1591 goto err_unset_master;
1592
1da177e4
LT
1593 printk(KERN_INFO DRV_NAME
1594 ": %s: enslaving %s as a%s interface with a%s link.\n",
1595 bond_dev->name, slave_dev->name,
1596 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1597 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1598
1599 /* enslave is successful */
1600 return 0;
1601
1602/* Undo stages on error */
1603err_unset_master:
1604 netdev_set_master(slave_dev, NULL);
1605
1606err_close:
1607 dev_close(slave_dev);
1608
1609err_restore_mac:
1610 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1611 addr.sa_family = slave_dev->type;
1612 dev_set_mac_address(slave_dev, &addr);
1613
1614err_free:
1615 kfree(new_slave);
1616
1617err_undo_flags:
1618 bond_dev->features = old_features;
8531c5ff 1619
1da177e4
LT
1620 return res;
1621}
1622
1623/*
1624 * Try to release the slave device <slave> from the bond device <master>
1625 * It is legal to access curr_active_slave without a lock because all the function
1626 * is write-locked.
1627 *
1628 * The rules for slave state should be:
1629 * for Active/Backup:
1630 * Active stays on all backups go down
1631 * for Bonded connections:
1632 * The first up interface should be left on and all others downed.
1633 */
a77b5325 1634int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1635{
1636 struct bonding *bond = bond_dev->priv;
1637 struct slave *slave, *oldcurrent;
1638 struct sockaddr addr;
1639 int mac_addr_differ;
0795af57 1640 DECLARE_MAC_BUF(mac);
1da177e4
LT
1641
1642 /* slave is not a slave or master is not master of this slave */
1643 if (!(slave_dev->flags & IFF_SLAVE) ||
1644 (slave_dev->master != bond_dev)) {
1645 printk(KERN_ERR DRV_NAME
4e0952c7 1646 ": %s: Error: cannot release %s.\n",
1da177e4
LT
1647 bond_dev->name, slave_dev->name);
1648 return -EINVAL;
1649 }
1650
1651 write_lock_bh(&bond->lock);
1652
1653 slave = bond_get_slave_by_dev(bond, slave_dev);
1654 if (!slave) {
1655 /* not a slave of this bond */
1656 printk(KERN_INFO DRV_NAME
1657 ": %s: %s not enslaved\n",
1658 bond_dev->name, slave_dev->name);
f5e2a7b2 1659 write_unlock_bh(&bond->lock);
1da177e4
LT
1660 return -EINVAL;
1661 }
1662
1663 mac_addr_differ = memcmp(bond_dev->dev_addr,
1664 slave->perm_hwaddr,
1665 ETH_ALEN);
1666 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1667 printk(KERN_WARNING DRV_NAME
0795af57
JP
1668 ": %s: Warning: the permanent HWaddr of %s - "
1669 "%s - is still in use by %s. "
1670 "Set the HWaddr of %s to a different address "
1671 "to avoid conflicts.\n",
4e0952c7 1672 bond_dev->name,
1da177e4 1673 slave_dev->name,
0795af57 1674 print_mac(mac, slave->perm_hwaddr),
1da177e4
LT
1675 bond_dev->name,
1676 slave_dev->name);
1677 }
1678
1679 /* Inform AD package of unbinding of slave. */
1680 if (bond->params.mode == BOND_MODE_8023AD) {
1681 /* must be called before the slave is
1682 * detached from the list
1683 */
1684 bond_3ad_unbind_slave(slave);
1685 }
1686
1687 printk(KERN_INFO DRV_NAME
1688 ": %s: releasing %s interface %s\n",
1689 bond_dev->name,
1690 (slave->state == BOND_STATE_ACTIVE)
1691 ? "active" : "backup",
1692 slave_dev->name);
1693
1694 oldcurrent = bond->curr_active_slave;
1695
1696 bond->current_arp_slave = NULL;
1697
1698 /* release the slave from its bond */
1699 bond_detach_slave(bond, slave);
1700
8531c5ff
AK
1701 bond_compute_features(bond);
1702
1da177e4
LT
1703 if (bond->primary_slave == slave) {
1704 bond->primary_slave = NULL;
1705 }
1706
1707 if (oldcurrent == slave) {
1708 bond_change_active_slave(bond, NULL);
1709 }
1710
1711 if ((bond->params.mode == BOND_MODE_TLB) ||
1712 (bond->params.mode == BOND_MODE_ALB)) {
1713 /* Must be called only after the slave has been
1714 * detached from the list and the curr_active_slave
1715 * has been cleared (if our_slave == old_current),
1716 * but before a new active slave is selected.
1717 */
1718 bond_alb_deinit_slave(bond, slave);
1719 }
1720
ff59c456 1721 if (oldcurrent == slave)
1da177e4
LT
1722 bond_select_active_slave(bond);
1723
1da177e4 1724 if (bond->slave_cnt == 0) {
ff59c456
JV
1725 bond_set_carrier(bond);
1726
1da177e4
LT
1727 /* if the last slave was removed, zero the mac address
1728 * of the master so it will be set by the application
1729 * to the mac address of the first slave
1730 */
1731 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1732
1733 if (list_empty(&bond->vlan_list)) {
1734 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1735 } else {
1736 printk(KERN_WARNING DRV_NAME
4e0952c7 1737 ": %s: Warning: clearing HW address of %s while it "
1da177e4 1738 "still has VLANs.\n",
4e0952c7 1739 bond_dev->name, bond_dev->name);
1da177e4 1740 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1741 ": %s: When re-adding slaves, make sure the bond's "
1742 "HW address matches its VLANs'.\n",
1743 bond_dev->name);
1da177e4
LT
1744 }
1745 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1746 !bond_has_challenged_slaves(bond)) {
1747 printk(KERN_INFO DRV_NAME
4e0952c7 1748 ": %s: last VLAN challenged slave %s "
1da177e4 1749 "left bond %s. VLAN blocking is removed\n",
4e0952c7 1750 bond_dev->name, slave_dev->name, bond_dev->name);
1da177e4
LT
1751 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1752 }
1753
1754 write_unlock_bh(&bond->lock);
1755
b76cdba9
MW
1756 /* must do this from outside any spinlocks */
1757 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1758
1da177e4
LT
1759 bond_del_vlans_from_slave(bond, slave_dev);
1760
1761 /* If the mode USES_PRIMARY, then we should only remove its
1762 * promisc and mc settings if it was the curr_active_slave, but that was
1763 * already taken care of above when we detached the slave
1764 */
1765 if (!USES_PRIMARY(bond->params.mode)) {
1766 /* unset promiscuity level from slave */
1767 if (bond_dev->flags & IFF_PROMISC) {
1768 dev_set_promiscuity(slave_dev, -1);
1769 }
1770
1771 /* unset allmulti level from slave */
1772 if (bond_dev->flags & IFF_ALLMULTI) {
1773 dev_set_allmulti(slave_dev, -1);
1774 }
1775
1776 /* flush master's mc_list from slave */
1777 bond_mc_list_flush(bond_dev, slave_dev);
1778 }
1779
1780 netdev_set_master(slave_dev, NULL);
1781
1782 /* close slave before restoring its mac address */
1783 dev_close(slave_dev);
1784
217df670
JV
1785 /* restore original ("permanent") mac address */
1786 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1787 addr.sa_family = slave_dev->type;
1788 dev_set_mac_address(slave_dev, &addr);
1da177e4 1789
8f903c70 1790 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
f5b2b966
JV
1791 IFF_SLAVE_INACTIVE | IFF_BONDING |
1792 IFF_SLAVE_NEEDARP);
1da177e4
LT
1793
1794 kfree(slave);
1795
1796 return 0; /* deletion OK */
1797}
1798
1799/*
1800 * This function releases all slaves.
1801 */
1802static int bond_release_all(struct net_device *bond_dev)
1803{
1804 struct bonding *bond = bond_dev->priv;
1805 struct slave *slave;
1806 struct net_device *slave_dev;
1807 struct sockaddr addr;
1808
1809 write_lock_bh(&bond->lock);
1810
ff59c456
JV
1811 netif_carrier_off(bond_dev);
1812
1da177e4
LT
1813 if (bond->slave_cnt == 0) {
1814 goto out;
1815 }
1816
1817 bond->current_arp_slave = NULL;
1818 bond->primary_slave = NULL;
1819 bond_change_active_slave(bond, NULL);
1820
1821 while ((slave = bond->first_slave) != NULL) {
1822 /* Inform AD package of unbinding of slave
1823 * before slave is detached from the list.
1824 */
1825 if (bond->params.mode == BOND_MODE_8023AD) {
1826 bond_3ad_unbind_slave(slave);
1827 }
1828
1829 slave_dev = slave->dev;
1830 bond_detach_slave(bond, slave);
1831
1832 if ((bond->params.mode == BOND_MODE_TLB) ||
1833 (bond->params.mode == BOND_MODE_ALB)) {
1834 /* must be called only after the slave
1835 * has been detached from the list
1836 */
1837 bond_alb_deinit_slave(bond, slave);
1838 }
1839
8531c5ff
AK
1840 bond_compute_features(bond);
1841
1da177e4
LT
1842 /* now that the slave is detached, unlock and perform
1843 * all the undo steps that should not be called from
1844 * within a lock.
1845 */
1846 write_unlock_bh(&bond->lock);
1847
b76cdba9 1848 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1da177e4
LT
1849 bond_del_vlans_from_slave(bond, slave_dev);
1850
1851 /* If the mode USES_PRIMARY, then we should only remove its
1852 * promisc and mc settings if it was the curr_active_slave, but that was
1853 * already taken care of above when we detached the slave
1854 */
1855 if (!USES_PRIMARY(bond->params.mode)) {
1856 /* unset promiscuity level from slave */
1857 if (bond_dev->flags & IFF_PROMISC) {
1858 dev_set_promiscuity(slave_dev, -1);
1859 }
1860
1861 /* unset allmulti level from slave */
1862 if (bond_dev->flags & IFF_ALLMULTI) {
1863 dev_set_allmulti(slave_dev, -1);
1864 }
1865
1866 /* flush master's mc_list from slave */
1867 bond_mc_list_flush(bond_dev, slave_dev);
1868 }
1869
1870 netdev_set_master(slave_dev, NULL);
1871
1872 /* close slave before restoring its mac address */
1873 dev_close(slave_dev);
1874
217df670
JV
1875 /* restore original ("permanent") mac address*/
1876 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1877 addr.sa_family = slave_dev->type;
1878 dev_set_mac_address(slave_dev, &addr);
1da177e4 1879
8f903c70
JV
1880 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1881 IFF_SLAVE_INACTIVE);
1da177e4
LT
1882
1883 kfree(slave);
1884
1885 /* re-acquire the lock before getting the next slave */
1886 write_lock_bh(&bond->lock);
1887 }
1888
1889 /* zero the mac address of the master so it will be
1890 * set by the application to the mac address of the
1891 * first slave
1892 */
1893 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1894
1895 if (list_empty(&bond->vlan_list)) {
1896 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1897 } else {
1898 printk(KERN_WARNING DRV_NAME
4e0952c7 1899 ": %s: Warning: clearing HW address of %s while it "
1da177e4 1900 "still has VLANs.\n",
4e0952c7 1901 bond_dev->name, bond_dev->name);
1da177e4 1902 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1903 ": %s: When re-adding slaves, make sure the bond's "
1904 "HW address matches its VLANs'.\n",
1905 bond_dev->name);
1da177e4
LT
1906 }
1907
1908 printk(KERN_INFO DRV_NAME
1909 ": %s: released all slaves\n",
1910 bond_dev->name);
1911
1912out:
1913 write_unlock_bh(&bond->lock);
1914
1915 return 0;
1916}
1917
1918/*
1919 * This function changes the active slave to slave <slave_dev>.
1920 * It returns -EINVAL in the following cases.
1921 * - <slave_dev> is not found in the list.
1922 * - There is not active slave now.
1923 * - <slave_dev> is already active.
1924 * - The link state of <slave_dev> is not BOND_LINK_UP.
1925 * - <slave_dev> is not running.
1926 * In these cases, this fuction does nothing.
1927 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1928 */
1929static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1930{
1931 struct bonding *bond = bond_dev->priv;
1932 struct slave *old_active = NULL;
1933 struct slave *new_active = NULL;
1934 int res = 0;
1935
1936 if (!USES_PRIMARY(bond->params.mode)) {
1937 return -EINVAL;
1938 }
1939
1940 /* Verify that master_dev is indeed the master of slave_dev */
1941 if (!(slave_dev->flags & IFF_SLAVE) ||
1942 (slave_dev->master != bond_dev)) {
1943 return -EINVAL;
1944 }
1945
1946 write_lock_bh(&bond->lock);
1947
1948 old_active = bond->curr_active_slave;
1949 new_active = bond_get_slave_by_dev(bond, slave_dev);
1950
1951 /*
1952 * Changing to the current active: do nothing; return success.
1953 */
1954 if (new_active && (new_active == old_active)) {
1955 write_unlock_bh(&bond->lock);
1956 return 0;
1957 }
1958
1959 if ((new_active) &&
1960 (old_active) &&
1961 (new_active->link == BOND_LINK_UP) &&
1962 IS_UP(new_active->dev)) {
1963 bond_change_active_slave(bond, new_active);
1964 } else {
1965 res = -EINVAL;
1966 }
1967
1968 write_unlock_bh(&bond->lock);
1969
1970 return res;
1971}
1972
1da177e4
LT
1973static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1974{
1975 struct bonding *bond = bond_dev->priv;
1976
1977 info->bond_mode = bond->params.mode;
1978 info->miimon = bond->params.miimon;
1979
1980 read_lock_bh(&bond->lock);
1981 info->num_slaves = bond->slave_cnt;
1982 read_unlock_bh(&bond->lock);
1983
1984 return 0;
1985}
1986
1987static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
1988{
1989 struct bonding *bond = bond_dev->priv;
1990 struct slave *slave;
1991 int i, found = 0;
1992
1993 if (info->slave_id < 0) {
1994 return -ENODEV;
1995 }
1996
1997 read_lock_bh(&bond->lock);
1998
1999 bond_for_each_slave(bond, slave, i) {
2000 if (i == (int)info->slave_id) {
2001 found = 1;
2002 break;
2003 }
2004 }
2005
2006 read_unlock_bh(&bond->lock);
2007
2008 if (found) {
2009 strcpy(info->slave_name, slave->dev->name);
2010 info->link = slave->link;
2011 info->state = slave->state;
2012 info->link_failure_count = slave->link_failure_count;
2013 } else {
2014 return -ENODEV;
2015 }
2016
2017 return 0;
2018}
2019
2020/*-------------------------------- Monitoring -------------------------------*/
2021
2022/* this function is called regularly to monitor each slave's link. */
a77b5325 2023void bond_mii_monitor(struct net_device *bond_dev)
1da177e4
LT
2024{
2025 struct bonding *bond = bond_dev->priv;
2026 struct slave *slave, *oldcurrent;
2027 int do_failover = 0;
2028 int delta_in_ticks;
2029 int i;
2030
2031 read_lock(&bond->lock);
2032
2033 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2034
2035 if (bond->kill_timers) {
2036 goto out;
2037 }
2038
2039 if (bond->slave_cnt == 0) {
2040 goto re_arm;
2041 }
2042
2043 /* we will try to read the link status of each of our slaves, and
2044 * set their IFF_RUNNING flag appropriately. For each slave not
2045 * supporting MII status, we won't do anything so that a user-space
2046 * program could monitor the link itself if needed.
2047 */
2048
2049 read_lock(&bond->curr_slave_lock);
2050 oldcurrent = bond->curr_active_slave;
2051 read_unlock(&bond->curr_slave_lock);
2052
2053 bond_for_each_slave(bond, slave, i) {
2054 struct net_device *slave_dev = slave->dev;
2055 int link_state;
2056 u16 old_speed = slave->speed;
2057 u8 old_duplex = slave->duplex;
2058
2059 link_state = bond_check_dev_link(bond, slave_dev, 0);
2060
2061 switch (slave->link) {
2062 case BOND_LINK_UP: /* the link was up */
2063 if (link_state == BMSR_LSTATUS) {
2064 /* link stays up, nothing more to do */
2065 break;
2066 } else { /* link going down */
2067 slave->link = BOND_LINK_FAIL;
2068 slave->delay = bond->params.downdelay;
2069
2070 if (slave->link_failure_count < UINT_MAX) {
2071 slave->link_failure_count++;
2072 }
2073
2074 if (bond->params.downdelay) {
2075 printk(KERN_INFO DRV_NAME
2076 ": %s: link status down for %s "
2077 "interface %s, disabling it in "
2078 "%d ms.\n",
2079 bond_dev->name,
2080 IS_UP(slave_dev)
2081 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2082 ? ((slave == oldcurrent)
2083 ? "active " : "backup ")
2084 : "")
2085 : "idle ",
2086 slave_dev->name,
2087 bond->params.downdelay * bond->params.miimon);
2088 }
2089 }
2090 /* no break ! fall through the BOND_LINK_FAIL test to
2091 ensure proper action to be taken
2092 */
2093 case BOND_LINK_FAIL: /* the link has just gone down */
2094 if (link_state != BMSR_LSTATUS) {
2095 /* link stays down */
2096 if (slave->delay <= 0) {
2097 /* link down for too long time */
2098 slave->link = BOND_LINK_DOWN;
2099
2100 /* in active/backup mode, we must
2101 * completely disable this interface
2102 */
2103 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2104 (bond->params.mode == BOND_MODE_8023AD)) {
2105 bond_set_slave_inactive_flags(slave);
2106 }
2107
2108 printk(KERN_INFO DRV_NAME
2109 ": %s: link status definitely "
2110 "down for interface %s, "
2111 "disabling it\n",
2112 bond_dev->name,
2113 slave_dev->name);
2114
2115 /* notify ad that the link status has changed */
2116 if (bond->params.mode == BOND_MODE_8023AD) {
2117 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2118 }
2119
2120 if ((bond->params.mode == BOND_MODE_TLB) ||
2121 (bond->params.mode == BOND_MODE_ALB)) {
2122 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2123 }
2124
2125 if (slave == oldcurrent) {
2126 do_failover = 1;
2127 }
2128 } else {
2129 slave->delay--;
2130 }
2131 } else {
2132 /* link up again */
2133 slave->link = BOND_LINK_UP;
2134 slave->jiffies = jiffies;
2135 printk(KERN_INFO DRV_NAME
2136 ": %s: link status up again after %d "
2137 "ms for interface %s.\n",
2138 bond_dev->name,
2139 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2140 slave_dev->name);
2141 }
2142 break;
2143 case BOND_LINK_DOWN: /* the link was down */
2144 if (link_state != BMSR_LSTATUS) {
2145 /* the link stays down, nothing more to do */
2146 break;
2147 } else { /* link going up */
2148 slave->link = BOND_LINK_BACK;
2149 slave->delay = bond->params.updelay;
2150
2151 if (bond->params.updelay) {
2152 /* if updelay == 0, no need to
2153 advertise about a 0 ms delay */
2154 printk(KERN_INFO DRV_NAME
2155 ": %s: link status up for "
2156 "interface %s, enabling it "
2157 "in %d ms.\n",
2158 bond_dev->name,
2159 slave_dev->name,
2160 bond->params.updelay * bond->params.miimon);
2161 }
2162 }
2163 /* no break ! fall through the BOND_LINK_BACK state in
2164 case there's something to do.
2165 */
2166 case BOND_LINK_BACK: /* the link has just come back */
2167 if (link_state != BMSR_LSTATUS) {
2168 /* link down again */
2169 slave->link = BOND_LINK_DOWN;
2170
2171 printk(KERN_INFO DRV_NAME
2172 ": %s: link status down again after %d "
2173 "ms for interface %s.\n",
2174 bond_dev->name,
2175 (bond->params.updelay - slave->delay) * bond->params.miimon,
2176 slave_dev->name);
2177 } else {
2178 /* link stays up */
2179 if (slave->delay == 0) {
2180 /* now the link has been up for long time enough */
2181 slave->link = BOND_LINK_UP;
2182 slave->jiffies = jiffies;
2183
2184 if (bond->params.mode == BOND_MODE_8023AD) {
2185 /* prevent it from being the active one */
2186 slave->state = BOND_STATE_BACKUP;
2187 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2188 /* make it immediately active */
2189 slave->state = BOND_STATE_ACTIVE;
2190 } else if (slave != bond->primary_slave) {
2191 /* prevent it from being the active one */
2192 slave->state = BOND_STATE_BACKUP;
2193 }
2194
2195 printk(KERN_INFO DRV_NAME
2196 ": %s: link status definitely "
2197 "up for interface %s.\n",
2198 bond_dev->name,
2199 slave_dev->name);
2200
2201 /* notify ad that the link status has changed */
2202 if (bond->params.mode == BOND_MODE_8023AD) {
2203 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2204 }
2205
2206 if ((bond->params.mode == BOND_MODE_TLB) ||
2207 (bond->params.mode == BOND_MODE_ALB)) {
2208 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2209 }
2210
2211 if ((!oldcurrent) ||
2212 (slave == bond->primary_slave)) {
2213 do_failover = 1;
2214 }
2215 } else {
2216 slave->delay--;
2217 }
2218 }
2219 break;
2220 default:
2221 /* Should not happen */
4e0952c7
MW
2222 printk(KERN_ERR DRV_NAME
2223 ": %s: Error: %s Illegal value (link=%d)\n",
2224 bond_dev->name,
2225 slave->dev->name,
2226 slave->link);
1da177e4
LT
2227 goto out;
2228 } /* end of switch (slave->link) */
2229
2230 bond_update_speed_duplex(slave);
2231
2232 if (bond->params.mode == BOND_MODE_8023AD) {
2233 if (old_speed != slave->speed) {
2234 bond_3ad_adapter_speed_changed(slave);
2235 }
2236
2237 if (old_duplex != slave->duplex) {
2238 bond_3ad_adapter_duplex_changed(slave);
2239 }
2240 }
2241
2242 } /* end of for */
2243
2244 if (do_failover) {
2245 write_lock(&bond->curr_slave_lock);
2246
2247 bond_select_active_slave(bond);
2248
1da177e4 2249 write_unlock(&bond->curr_slave_lock);
ff59c456
JV
2250 } else
2251 bond_set_carrier(bond);
1da177e4
LT
2252
2253re_arm:
2254 if (bond->params.miimon) {
2255 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2256 }
2257out:
2258 read_unlock(&bond->lock);
2259}
2260
c3ade5ca 2261
d3bb52b0 2262static __be32 bond_glean_dev_ip(struct net_device *dev)
c3ade5ca
JV
2263{
2264 struct in_device *idev;
2265 struct in_ifaddr *ifa;
a144ea4b 2266 __be32 addr = 0;
c3ade5ca
JV
2267
2268 if (!dev)
2269 return 0;
2270
2271 rcu_read_lock();
e5ed6399 2272 idev = __in_dev_get_rcu(dev);
c3ade5ca
JV
2273 if (!idev)
2274 goto out;
2275
2276 ifa = idev->ifa_list;
2277 if (!ifa)
2278 goto out;
2279
2280 addr = ifa->ifa_local;
2281out:
2282 rcu_read_unlock();
2283 return addr;
2284}
2285
2286static int bond_has_ip(struct bonding *bond)
2287{
2288 struct vlan_entry *vlan, *vlan_next;
2289
2290 if (bond->master_ip)
2291 return 1;
2292
2293 if (list_empty(&bond->vlan_list))
2294 return 0;
2295
2296 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2297 vlan_list) {
2298 if (vlan->vlan_ip)
2299 return 1;
2300 }
2301
2302 return 0;
2303}
2304
d3bb52b0 2305static int bond_has_this_ip(struct bonding *bond, __be32 ip)
f5b2b966
JV
2306{
2307 struct vlan_entry *vlan, *vlan_next;
2308
2309 if (ip == bond->master_ip)
2310 return 1;
2311
2312 if (list_empty(&bond->vlan_list))
2313 return 0;
2314
2315 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2316 vlan_list) {
2317 if (ip == vlan->vlan_ip)
2318 return 1;
2319 }
2320
2321 return 0;
2322}
2323
c3ade5ca
JV
2324/*
2325 * We go to the (large) trouble of VLAN tagging ARP frames because
2326 * switches in VLAN mode (especially if ports are configured as
2327 * "native" to a VLAN) might not pass non-tagged frames.
2328 */
d3bb52b0 2329static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
c3ade5ca
JV
2330{
2331 struct sk_buff *skb;
2332
2333 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2334 slave_dev->name, dest_ip, src_ip, vlan_id);
2335
2336 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2337 NULL, slave_dev->dev_addr, NULL);
2338
2339 if (!skb) {
2340 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2341 return;
2342 }
2343 if (vlan_id) {
2344 skb = vlan_put_tag(skb, vlan_id);
2345 if (!skb) {
2346 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2347 return;
2348 }
2349 }
2350 arp_xmit(skb);
2351}
2352
2353
1da177e4
LT
2354static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2355{
c3ade5ca 2356 int i, vlan_id, rv;
d3bb52b0 2357 __be32 *targets = bond->params.arp_targets;
c3ade5ca
JV
2358 struct vlan_entry *vlan, *vlan_next;
2359 struct net_device *vlan_dev;
2360 struct flowi fl;
2361 struct rtable *rt;
1da177e4 2362
6b780567
MW
2363 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2364 if (!targets[i])
2365 continue;
c3ade5ca
JV
2366 dprintk("basa: target %x\n", targets[i]);
2367 if (list_empty(&bond->vlan_list)) {
2368 dprintk("basa: empty vlan: arp_send\n");
2369 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2370 bond->master_ip, 0);
2371 continue;
2372 }
2373
2374 /*
2375 * If VLANs are configured, we do a route lookup to
2376 * determine which VLAN interface would be used, so we
2377 * can tag the ARP with the proper VLAN tag.
2378 */
2379 memset(&fl, 0, sizeof(fl));
2380 fl.fl4_dst = targets[i];
2381 fl.fl4_tos = RTO_ONLINK;
2382
2383 rv = ip_route_output_key(&rt, &fl);
2384 if (rv) {
2385 if (net_ratelimit()) {
2386 printk(KERN_WARNING DRV_NAME
2387 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2388 bond->dev->name, NIPQUAD(fl.fl4_dst));
2389 }
2390 continue;
2391 }
2392
2393 /*
2394 * This target is not on a VLAN
2395 */
2396 if (rt->u.dst.dev == bond->dev) {
ed4b9f80 2397 ip_rt_put(rt);
c3ade5ca
JV
2398 dprintk("basa: rtdev == bond->dev: arp_send\n");
2399 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2400 bond->master_ip, 0);
2401 continue;
2402 }
2403
2404 vlan_id = 0;
2405 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2406 vlan_list) {
5c15bdec 2407 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
c3ade5ca
JV
2408 if (vlan_dev == rt->u.dst.dev) {
2409 vlan_id = vlan->vlan_id;
2410 dprintk("basa: vlan match on %s %d\n",
2411 vlan_dev->name, vlan_id);
2412 break;
2413 }
2414 }
2415
2416 if (vlan_id) {
ed4b9f80 2417 ip_rt_put(rt);
c3ade5ca
JV
2418 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2419 vlan->vlan_ip, vlan_id);
2420 continue;
2421 }
2422
2423 if (net_ratelimit()) {
2424 printk(KERN_WARNING DRV_NAME
2425 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2426 bond->dev->name, NIPQUAD(fl.fl4_dst),
2427 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2428 }
ed4b9f80 2429 ip_rt_put(rt);
c3ade5ca
JV
2430 }
2431}
2432
2433/*
2434 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2435 * for each VLAN above us.
2436 */
2437static void bond_send_gratuitous_arp(struct bonding *bond)
2438{
2439 struct slave *slave = bond->curr_active_slave;
2440 struct vlan_entry *vlan;
2441 struct net_device *vlan_dev;
2442
2443 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2444 slave ? slave->dev->name : "NULL");
2445 if (!slave)
2446 return;
2447
2448 if (bond->master_ip) {
2449 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2450 bond->master_ip, 0);
2451 }
2452
2453 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
5c15bdec 2454 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
c3ade5ca
JV
2455 if (vlan->vlan_ip) {
2456 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2457 vlan->vlan_ip, vlan->vlan_id);
2458 }
1da177e4
LT
2459 }
2460}
2461
d3bb52b0 2462static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
f5b2b966
JV
2463{
2464 int i;
d3bb52b0 2465 __be32 *targets = bond->params.arp_targets;
f5b2b966
JV
2466
2467 targets = bond->params.arp_targets;
2468 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2469 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2470 "%u.%u.%u.%u bhti(tip) %d\n",
2471 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2472 bond_has_this_ip(bond, tip));
2473 if (sip == targets[i]) {
2474 if (bond_has_this_ip(bond, tip))
2475 slave->last_arp_rx = jiffies;
2476 return;
2477 }
2478 }
2479}
2480
2481static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2482{
2483 struct arphdr *arp;
2484 struct slave *slave;
2485 struct bonding *bond;
2486 unsigned char *arp_ptr;
d3bb52b0 2487 __be32 sip, tip;
f5b2b966 2488
e730c155
EB
2489 if (dev->nd_net != &init_net)
2490 goto out;
2491
f5b2b966
JV
2492 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2493 goto out;
2494
2495 bond = dev->priv;
2496 read_lock(&bond->lock);
2497
2498 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2499 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2500 orig_dev ? orig_dev->name : "NULL");
2501
2502 slave = bond_get_slave_by_dev(bond, orig_dev);
2503 if (!slave || !slave_do_arp_validate(bond, slave))
2504 goto out_unlock;
2505
2506 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2507 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2508 (2 * dev->addr_len) +
2509 (2 * sizeof(u32)))))
2510 goto out_unlock;
2511
d0a92be0 2512 arp = arp_hdr(skb);
f5b2b966
JV
2513 if (arp->ar_hln != dev->addr_len ||
2514 skb->pkt_type == PACKET_OTHERHOST ||
2515 skb->pkt_type == PACKET_LOOPBACK ||
2516 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2517 arp->ar_pro != htons(ETH_P_IP) ||
2518 arp->ar_pln != 4)
2519 goto out_unlock;
2520
2521 arp_ptr = (unsigned char *)(arp + 1);
2522 arp_ptr += dev->addr_len;
2523 memcpy(&sip, arp_ptr, 4);
2524 arp_ptr += 4 + dev->addr_len;
2525 memcpy(&tip, arp_ptr, 4);
2526
2527 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2528 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2529 slave->state, bond->params.arp_validate,
2530 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2531
2532 /*
2533 * Backup slaves won't see the ARP reply, but do come through
2534 * here for each ARP probe (so we swap the sip/tip to validate
2535 * the probe). In a "redundant switch, common router" type of
2536 * configuration, the ARP probe will (hopefully) travel from
2537 * the active, through one switch, the router, then the other
2538 * switch before reaching the backup.
2539 */
2540 if (slave->state == BOND_STATE_ACTIVE)
2541 bond_validate_arp(bond, slave, sip, tip);
2542 else
2543 bond_validate_arp(bond, slave, tip, sip);
2544
2545out_unlock:
2546 read_unlock(&bond->lock);
2547out:
2548 dev_kfree_skb(skb);
2549 return NET_RX_SUCCESS;
2550}
2551
1da177e4
LT
2552/*
2553 * this function is called regularly to monitor each slave's link
2554 * ensuring that traffic is being sent and received when arp monitoring
2555 * is used in load-balancing mode. if the adapter has been dormant, then an
2556 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2557 * arp monitoring in active backup mode.
2558 */
a77b5325 2559void bond_loadbalance_arp_mon(struct net_device *bond_dev)
1da177e4
LT
2560{
2561 struct bonding *bond = bond_dev->priv;
2562 struct slave *slave, *oldcurrent;
2563 int do_failover = 0;
2564 int delta_in_ticks;
2565 int i;
2566
2567 read_lock(&bond->lock);
2568
2569 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2570
2571 if (bond->kill_timers) {
2572 goto out;
2573 }
2574
2575 if (bond->slave_cnt == 0) {
2576 goto re_arm;
2577 }
2578
2579 read_lock(&bond->curr_slave_lock);
2580 oldcurrent = bond->curr_active_slave;
2581 read_unlock(&bond->curr_slave_lock);
2582
2583 /* see if any of the previous devices are up now (i.e. they have
2584 * xmt and rcv traffic). the curr_active_slave does not come into
2585 * the picture unless it is null. also, slave->jiffies is not needed
2586 * here because we send an arp on each slave and give a slave as
2587 * long as it needs to get the tx/rx within the delta.
2588 * TODO: what about up/down delay in arp mode? it wasn't here before
2589 * so it can wait
2590 */
2591 bond_for_each_slave(bond, slave, i) {
2592 if (slave->link != BOND_LINK_UP) {
2593 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2594 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2595
2596 slave->link = BOND_LINK_UP;
2597 slave->state = BOND_STATE_ACTIVE;
2598
2599 /* primary_slave has no meaning in round-robin
2600 * mode. the window of a slave being up and
2601 * curr_active_slave being null after enslaving
2602 * is closed.
2603 */
2604 if (!oldcurrent) {
2605 printk(KERN_INFO DRV_NAME
2606 ": %s: link status definitely "
2607 "up for interface %s, ",
2608 bond_dev->name,
2609 slave->dev->name);
2610 do_failover = 1;
2611 } else {
2612 printk(KERN_INFO DRV_NAME
2613 ": %s: interface %s is now up\n",
2614 bond_dev->name,
2615 slave->dev->name);
2616 }
2617 }
2618 } else {
2619 /* slave->link == BOND_LINK_UP */
2620
2621 /* not all switches will respond to an arp request
2622 * when the source ip is 0, so don't take the link down
2623 * if we don't know our ip yet
2624 */
2625 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2626 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
c3ade5ca 2627 bond_has_ip(bond))) {
1da177e4
LT
2628
2629 slave->link = BOND_LINK_DOWN;
2630 slave->state = BOND_STATE_BACKUP;
2631
2632 if (slave->link_failure_count < UINT_MAX) {
2633 slave->link_failure_count++;
2634 }
2635
2636 printk(KERN_INFO DRV_NAME
2637 ": %s: interface %s is now down.\n",
2638 bond_dev->name,
2639 slave->dev->name);
2640
2641 if (slave == oldcurrent) {
2642 do_failover = 1;
2643 }
2644 }
2645 }
2646
2647 /* note: if switch is in round-robin mode, all links
2648 * must tx arp to ensure all links rx an arp - otherwise
2649 * links may oscillate or not come up at all; if switch is
2650 * in something like xor mode, there is nothing we can
2651 * do - all replies will be rx'ed on same link causing slaves
2652 * to be unstable during low/no traffic periods
2653 */
2654 if (IS_UP(slave->dev)) {
2655 bond_arp_send_all(bond, slave);
2656 }
2657 }
2658
2659 if (do_failover) {
2660 write_lock(&bond->curr_slave_lock);
2661
2662 bond_select_active_slave(bond);
2663
1da177e4
LT
2664 write_unlock(&bond->curr_slave_lock);
2665 }
2666
2667re_arm:
2668 if (bond->params.arp_interval) {
2669 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2670 }
2671out:
2672 read_unlock(&bond->lock);
2673}
2674
2675/*
2676 * When using arp monitoring in active-backup mode, this function is
2677 * called to determine if any backup slaves have went down or a new
2678 * current slave needs to be found.
2679 * The backup slaves never generate traffic, they are considered up by merely
2680 * receiving traffic. If the current slave goes down, each backup slave will
2681 * be given the opportunity to tx/rx an arp before being taken down - this
2682 * prevents all slaves from being taken down due to the current slave not
2683 * sending any traffic for the backups to receive. The arps are not necessarily
2684 * necessary, any tx and rx traffic will keep the current slave up. While any
2685 * rx traffic will keep the backup slaves up, the current slave is responsible
2686 * for generating traffic to keep them up regardless of any other traffic they
2687 * may have received.
2688 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2689 */
a77b5325 2690void bond_activebackup_arp_mon(struct net_device *bond_dev)
1da177e4
LT
2691{
2692 struct bonding *bond = bond_dev->priv;
2693 struct slave *slave;
2694 int delta_in_ticks;
2695 int i;
2696
2697 read_lock(&bond->lock);
2698
2699 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2700
2701 if (bond->kill_timers) {
2702 goto out;
2703 }
2704
2705 if (bond->slave_cnt == 0) {
2706 goto re_arm;
2707 }
2708
2709 /* determine if any slave has come up or any backup slave has
2710 * gone down
2711 * TODO: what about up/down delay in arp mode? it wasn't here before
2712 * so it can wait
2713 */
2714 bond_for_each_slave(bond, slave, i) {
2715 if (slave->link != BOND_LINK_UP) {
f5b2b966
JV
2716 if ((jiffies - slave_last_rx(bond, slave)) <=
2717 delta_in_ticks) {
1da177e4
LT
2718
2719 slave->link = BOND_LINK_UP;
2720
2721 write_lock(&bond->curr_slave_lock);
2722
2723 if ((!bond->curr_active_slave) &&
2724 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2725 bond_change_active_slave(bond, slave);
2726 bond->current_arp_slave = NULL;
2727 } else if (bond->curr_active_slave != slave) {
2728 /* this slave has just come up but we
2729 * already have a current slave; this
2730 * can also happen if bond_enslave adds
2731 * a new slave that is up while we are
2732 * searching for a new slave
2733 */
2734 bond_set_slave_inactive_flags(slave);
2735 bond->current_arp_slave = NULL;
2736 }
2737
ff59c456
JV
2738 bond_set_carrier(bond);
2739
1da177e4
LT
2740 if (slave == bond->curr_active_slave) {
2741 printk(KERN_INFO DRV_NAME
2742 ": %s: %s is up and now the "
2743 "active interface\n",
2744 bond_dev->name,
2745 slave->dev->name);
ff59c456 2746 netif_carrier_on(bond->dev);
1da177e4
LT
2747 } else {
2748 printk(KERN_INFO DRV_NAME
2749 ": %s: backup interface %s is "
2750 "now up\n",
2751 bond_dev->name,
2752 slave->dev->name);
2753 }
2754
2755 write_unlock(&bond->curr_slave_lock);
2756 }
2757 } else {
2758 read_lock(&bond->curr_slave_lock);
2759
2760 if ((slave != bond->curr_active_slave) &&
2761 (!bond->current_arp_slave) &&
f5b2b966 2762 (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
c3ade5ca 2763 bond_has_ip(bond))) {
1da177e4
LT
2764 /* a backup slave has gone down; three times
2765 * the delta allows the current slave to be
2766 * taken out before the backup slave.
2767 * note: a non-null current_arp_slave indicates
2768 * the curr_active_slave went down and we are
2769 * searching for a new one; under this
2770 * condition we only take the curr_active_slave
2771 * down - this gives each slave a chance to
2772 * tx/rx traffic before being taken out
2773 */
2774
2775 read_unlock(&bond->curr_slave_lock);
2776
2777 slave->link = BOND_LINK_DOWN;
2778
2779 if (slave->link_failure_count < UINT_MAX) {
2780 slave->link_failure_count++;
2781 }
2782
2783 bond_set_slave_inactive_flags(slave);
2784
2785 printk(KERN_INFO DRV_NAME
2786 ": %s: backup interface %s is now down\n",
2787 bond_dev->name,
2788 slave->dev->name);
2789 } else {
2790 read_unlock(&bond->curr_slave_lock);
2791 }
2792 }
2793 }
2794
2795 read_lock(&bond->curr_slave_lock);
2796 slave = bond->curr_active_slave;
2797 read_unlock(&bond->curr_slave_lock);
2798
2799 if (slave) {
2800 /* if we have sent traffic in the past 2*arp_intervals but
2801 * haven't xmit and rx traffic in that time interval, select
2802 * a different slave. slave->jiffies is only updated when
2803 * a slave first becomes the curr_active_slave - not necessarily
2804 * after every arp; this ensures the slave has a full 2*delta
2805 * before being taken out. if a primary is being used, check
2806 * if it is up and needs to take over as the curr_active_slave
2807 */
2808 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
f5b2b966 2809 (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
c3ade5ca 2810 bond_has_ip(bond))) &&
1da177e4
LT
2811 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2812
2813 slave->link = BOND_LINK_DOWN;
2814
2815 if (slave->link_failure_count < UINT_MAX) {
2816 slave->link_failure_count++;
2817 }
2818
2819 printk(KERN_INFO DRV_NAME
2820 ": %s: link status down for active interface "
2821 "%s, disabling it\n",
2822 bond_dev->name,
2823 slave->dev->name);
2824
2825 write_lock(&bond->curr_slave_lock);
2826
2827 bond_select_active_slave(bond);
2828 slave = bond->curr_active_slave;
2829
2830 write_unlock(&bond->curr_slave_lock);
2831
2832 bond->current_arp_slave = slave;
2833
2834 if (slave) {
2835 slave->jiffies = jiffies;
2836 }
2837 } else if ((bond->primary_slave) &&
2838 (bond->primary_slave != slave) &&
2839 (bond->primary_slave->link == BOND_LINK_UP)) {
2840 /* at this point, slave is the curr_active_slave */
2841 printk(KERN_INFO DRV_NAME
2842 ": %s: changing from interface %s to primary "
2843 "interface %s\n",
2844 bond_dev->name,
2845 slave->dev->name,
2846 bond->primary_slave->dev->name);
2847
2848 /* primary is up so switch to it */
2849 write_lock(&bond->curr_slave_lock);
2850 bond_change_active_slave(bond, bond->primary_slave);
2851 write_unlock(&bond->curr_slave_lock);
2852
2853 slave = bond->primary_slave;
2854 slave->jiffies = jiffies;
2855 } else {
2856 bond->current_arp_slave = NULL;
2857 }
2858
2859 /* the current slave must tx an arp to ensure backup slaves
2860 * rx traffic
2861 */
c3ade5ca 2862 if (slave && bond_has_ip(bond)) {
1da177e4
LT
2863 bond_arp_send_all(bond, slave);
2864 }
2865 }
2866
2867 /* if we don't have a curr_active_slave, search for the next available
2868 * backup slave from the current_arp_slave and make it the candidate
2869 * for becoming the curr_active_slave
2870 */
2871 if (!slave) {
2872 if (!bond->current_arp_slave) {
2873 bond->current_arp_slave = bond->first_slave;
2874 }
2875
2876 if (bond->current_arp_slave) {
2877 bond_set_slave_inactive_flags(bond->current_arp_slave);
2878
2879 /* search for next candidate */
2f872f04 2880 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
1da177e4
LT
2881 if (IS_UP(slave->dev)) {
2882 slave->link = BOND_LINK_BACK;
2883 bond_set_slave_active_flags(slave);
2884 bond_arp_send_all(bond, slave);
2885 slave->jiffies = jiffies;
2886 bond->current_arp_slave = slave;
2887 break;
2888 }
2889
2890 /* if the link state is up at this point, we
2891 * mark it down - this can happen if we have
2892 * simultaneous link failures and
2893 * reselect_active_interface doesn't make this
2894 * one the current slave so it is still marked
2895 * up when it is actually down
2896 */
2897 if (slave->link == BOND_LINK_UP) {
2898 slave->link = BOND_LINK_DOWN;
2899 if (slave->link_failure_count < UINT_MAX) {
2900 slave->link_failure_count++;
2901 }
2902
2903 bond_set_slave_inactive_flags(slave);
2904
2905 printk(KERN_INFO DRV_NAME
2906 ": %s: backup interface %s is "
2907 "now down.\n",
2908 bond_dev->name,
2909 slave->dev->name);
2910 }
2911 }
2912 }
2913 }
2914
2915re_arm:
2916 if (bond->params.arp_interval) {
2917 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2918 }
2919out:
2920 read_unlock(&bond->lock);
2921}
2922
2923/*------------------------------ proc/seq_file-------------------------------*/
2924
2925#ifdef CONFIG_PROC_FS
2926
2927#define SEQ_START_TOKEN ((void *)1)
2928
2929static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
2930{
2931 struct bonding *bond = seq->private;
2932 loff_t off = 0;
2933 struct slave *slave;
2934 int i;
2935
2936 /* make sure the bond won't be taken away */
2937 read_lock(&dev_base_lock);
2938 read_lock_bh(&bond->lock);
2939
2940 if (*pos == 0) {
2941 return SEQ_START_TOKEN;
2942 }
2943
2944 bond_for_each_slave(bond, slave, i) {
2945 if (++off == *pos) {
2946 return slave;
2947 }
2948 }
2949
2950 return NULL;
2951}
2952
2953static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2954{
2955 struct bonding *bond = seq->private;
2956 struct slave *slave = v;
2957
2958 ++*pos;
2959 if (v == SEQ_START_TOKEN) {
2960 return bond->first_slave;
2961 }
2962
2963 slave = slave->next;
2964
2965 return (slave == bond->first_slave) ? NULL : slave;
2966}
2967
2968static void bond_info_seq_stop(struct seq_file *seq, void *v)
2969{
2970 struct bonding *bond = seq->private;
2971
2972 read_unlock_bh(&bond->lock);
2973 read_unlock(&dev_base_lock);
2974}
2975
2976static void bond_info_show_master(struct seq_file *seq)
2977{
2978 struct bonding *bond = seq->private;
2979 struct slave *curr;
4756b02f
MW
2980 int i;
2981 u32 target;
1da177e4
LT
2982
2983 read_lock(&bond->curr_slave_lock);
2984 curr = bond->curr_active_slave;
2985 read_unlock(&bond->curr_slave_lock);
2986
2987 seq_printf(seq, "Bonding Mode: %s\n",
2988 bond_mode_name(bond->params.mode));
2989
c61b75ad
MW
2990 if (bond->params.mode == BOND_MODE_XOR ||
2991 bond->params.mode == BOND_MODE_8023AD) {
2992 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
2993 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
2994 bond->params.xmit_policy);
2995 }
2996
1da177e4
LT
2997 if (USES_PRIMARY(bond->params.mode)) {
2998 seq_printf(seq, "Primary Slave: %s\n",
0f418b2a
MW
2999 (bond->primary_slave) ?
3000 bond->primary_slave->dev->name : "None");
1da177e4
LT
3001
3002 seq_printf(seq, "Currently Active Slave: %s\n",
3003 (curr) ? curr->dev->name : "None");
3004 }
3005
ff59c456
JV
3006 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3007 "up" : "down");
1da177e4
LT
3008 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3009 seq_printf(seq, "Up Delay (ms): %d\n",
3010 bond->params.updelay * bond->params.miimon);
3011 seq_printf(seq, "Down Delay (ms): %d\n",
3012 bond->params.downdelay * bond->params.miimon);
3013
4756b02f
MW
3014
3015 /* ARP information */
3016 if(bond->params.arp_interval > 0) {
3017 int printed=0;
3018 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3019 bond->params.arp_interval);
3020
3021 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3022
3023 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3024 if (!bond->params.arp_targets[i])
3025 continue;
3026 if (printed)
3027 seq_printf(seq, ",");
3028 target = ntohl(bond->params.arp_targets[i]);
3029 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3030 printed = 1;
3031 }
3032 seq_printf(seq, "\n");
3033 }
3034
1da177e4
LT
3035 if (bond->params.mode == BOND_MODE_8023AD) {
3036 struct ad_info ad_info;
0795af57 3037 DECLARE_MAC_BUF(mac);
1da177e4
LT
3038
3039 seq_puts(seq, "\n802.3ad info\n");
3040 seq_printf(seq, "LACP rate: %s\n",
3041 (bond->params.lacp_fast) ? "fast" : "slow");
3042
3043 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3044 seq_printf(seq, "bond %s has no active aggregator\n",
3045 bond->dev->name);
3046 } else {
3047 seq_printf(seq, "Active Aggregator Info:\n");
3048
3049 seq_printf(seq, "\tAggregator ID: %d\n",
3050 ad_info.aggregator_id);
3051 seq_printf(seq, "\tNumber of ports: %d\n",
3052 ad_info.ports);
3053 seq_printf(seq, "\tActor Key: %d\n",
3054 ad_info.actor_key);
3055 seq_printf(seq, "\tPartner Key: %d\n",
3056 ad_info.partner_key);
0795af57
JP
3057 seq_printf(seq, "\tPartner Mac Address: %s\n",
3058 print_mac(mac, ad_info.partner_system));
1da177e4
LT
3059 }
3060 }
3061}
3062
3063static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3064{
3065 struct bonding *bond = seq->private;
0795af57 3066 DECLARE_MAC_BUF(mac);
1da177e4
LT
3067
3068 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3069 seq_printf(seq, "MII Status: %s\n",
3070 (slave->link == BOND_LINK_UP) ? "up" : "down");
65509645 3071 seq_printf(seq, "Link Failure Count: %u\n",
1da177e4
LT
3072 slave->link_failure_count);
3073
217df670 3074 seq_printf(seq,
0795af57
JP
3075 "Permanent HW addr: %s\n",
3076 print_mac(mac, slave->perm_hwaddr));
1da177e4
LT
3077
3078 if (bond->params.mode == BOND_MODE_8023AD) {
3079 const struct aggregator *agg
3080 = SLAVE_AD_INFO(slave).port.aggregator;
3081
3082 if (agg) {
3083 seq_printf(seq, "Aggregator ID: %d\n",
3084 agg->aggregator_identifier);
3085 } else {
3086 seq_puts(seq, "Aggregator ID: N/A\n");
3087 }
3088 }
3089}
3090
3091static int bond_info_seq_show(struct seq_file *seq, void *v)
3092{
3093 if (v == SEQ_START_TOKEN) {
3094 seq_printf(seq, "%s\n", version);
3095 bond_info_show_master(seq);
3096 } else {
3097 bond_info_show_slave(seq, v);
3098 }
3099
3100 return 0;
3101}
3102
3103static struct seq_operations bond_info_seq_ops = {
3104 .start = bond_info_seq_start,
3105 .next = bond_info_seq_next,
3106 .stop = bond_info_seq_stop,
3107 .show = bond_info_seq_show,
3108};
3109
3110static int bond_info_open(struct inode *inode, struct file *file)
3111{
3112 struct seq_file *seq;
3113 struct proc_dir_entry *proc;
3114 int res;
3115
3116 res = seq_open(file, &bond_info_seq_ops);
3117 if (!res) {
3118 /* recover the pointer buried in proc_dir_entry data */
3119 seq = file->private_data;
3120 proc = PDE(inode);
3121 seq->private = proc->data;
3122 }
3123
3124 return res;
3125}
3126
d54b1fdb 3127static const struct file_operations bond_info_fops = {
1da177e4
LT
3128 .owner = THIS_MODULE,
3129 .open = bond_info_open,
3130 .read = seq_read,
3131 .llseek = seq_lseek,
3132 .release = seq_release,
3133};
3134
3135static int bond_create_proc_entry(struct bonding *bond)
3136{
3137 struct net_device *bond_dev = bond->dev;
3138
3139 if (bond_proc_dir) {
3140 bond->proc_entry = create_proc_entry(bond_dev->name,
3141 S_IRUGO,
3142 bond_proc_dir);
3143 if (bond->proc_entry == NULL) {
3144 printk(KERN_WARNING DRV_NAME
3145 ": Warning: Cannot create /proc/net/%s/%s\n",
3146 DRV_NAME, bond_dev->name);
3147 } else {
3148 bond->proc_entry->data = bond;
3149 bond->proc_entry->proc_fops = &bond_info_fops;
3150 bond->proc_entry->owner = THIS_MODULE;
3151 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3152 }
3153 }
3154
3155 return 0;
3156}
3157
3158static void bond_remove_proc_entry(struct bonding *bond)
3159{
3160 if (bond_proc_dir && bond->proc_entry) {
3161 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3162 memset(bond->proc_file_name, 0, IFNAMSIZ);
3163 bond->proc_entry = NULL;
3164 }
3165}
3166
3167/* Create the bonding directory under /proc/net, if doesn't exist yet.
3168 * Caller must hold rtnl_lock.
3169 */
3170static void bond_create_proc_dir(void)
3171{
3172 int len = strlen(DRV_NAME);
3173
457c4cbc 3174 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
1da177e4
LT
3175 bond_proc_dir = bond_proc_dir->next) {
3176 if ((bond_proc_dir->namelen == len) &&
3177 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3178 break;
3179 }
3180 }
3181
3182 if (!bond_proc_dir) {
457c4cbc 3183 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
1da177e4
LT
3184 if (bond_proc_dir) {
3185 bond_proc_dir->owner = THIS_MODULE;
3186 } else {
3187 printk(KERN_WARNING DRV_NAME
3188 ": Warning: cannot create /proc/net/%s\n",
3189 DRV_NAME);
3190 }
3191 }
3192}
3193
3194/* Destroy the bonding directory under /proc/net, if empty.
3195 * Caller must hold rtnl_lock.
3196 */
3197static void bond_destroy_proc_dir(void)
3198{
3199 struct proc_dir_entry *de;
3200
3201 if (!bond_proc_dir) {
3202 return;
3203 }
3204
3205 /* verify that the /proc dir is empty */
3206 for (de = bond_proc_dir->subdir; de; de = de->next) {
3207 /* ignore . and .. */
3208 if (*(de->name) != '.') {
3209 break;
3210 }
3211 }
3212
3213 if (de) {
3214 if (bond_proc_dir->owner == THIS_MODULE) {
3215 bond_proc_dir->owner = NULL;
3216 }
3217 } else {
457c4cbc 3218 remove_proc_entry(DRV_NAME, init_net.proc_net);
1da177e4
LT
3219 bond_proc_dir = NULL;
3220 }
3221}
3222#endif /* CONFIG_PROC_FS */
3223
3224/*-------------------------- netdev event handling --------------------------*/
3225
3226/*
3227 * Change device name
3228 */
3229static int bond_event_changename(struct bonding *bond)
3230{
3231#ifdef CONFIG_PROC_FS
3232 bond_remove_proc_entry(bond);
3233 bond_create_proc_entry(bond);
3234#endif
b76cdba9
MW
3235 down_write(&(bonding_rwsem));
3236 bond_destroy_sysfs_entry(bond);
3237 bond_create_sysfs_entry(bond);
3238 up_write(&(bonding_rwsem));
1da177e4
LT
3239 return NOTIFY_DONE;
3240}
3241
3242static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3243{
3244 struct bonding *event_bond = bond_dev->priv;
3245
3246 switch (event) {
3247 case NETDEV_CHANGENAME:
3248 return bond_event_changename(event_bond);
3249 case NETDEV_UNREGISTER:
3250 /*
3251 * TODO: remove a bond from the list?
3252 */
3253 break;
3254 default:
3255 break;
3256 }
3257
3258 return NOTIFY_DONE;
3259}
3260
3261static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3262{
3263 struct net_device *bond_dev = slave_dev->master;
8531c5ff 3264 struct bonding *bond = bond_dev->priv;
1da177e4
LT
3265
3266 switch (event) {
3267 case NETDEV_UNREGISTER:
3268 if (bond_dev) {
3269 bond_release(bond_dev, slave_dev);
3270 }
3271 break;
3272 case NETDEV_CHANGE:
3273 /*
3274 * TODO: is this what we get if somebody
3275 * sets up a hierarchical bond, then rmmod's
3276 * one of the slave bonding devices?
3277 */
3278 break;
3279 case NETDEV_DOWN:
3280 /*
3281 * ... Or is it this?
3282 */
3283 break;
3284 case NETDEV_CHANGEMTU:
3285 /*
3286 * TODO: Should slaves be allowed to
3287 * independently alter their MTU? For
3288 * an active-backup bond, slaves need
3289 * not be the same type of device, so
3290 * MTUs may vary. For other modes,
3291 * slaves arguably should have the
3292 * same MTUs. To do this, we'd need to
3293 * take over the slave's change_mtu
3294 * function for the duration of their
3295 * servitude.
3296 */
3297 break;
3298 case NETDEV_CHANGENAME:
3299 /*
3300 * TODO: handle changing the primary's name
3301 */
3302 break;
8531c5ff
AK
3303 case NETDEV_FEAT_CHANGE:
3304 bond_compute_features(bond);
3305 break;
1da177e4
LT
3306 default:
3307 break;
3308 }
3309
3310 return NOTIFY_DONE;
3311}
3312
3313/*
3314 * bond_netdev_event: handle netdev notifier chain events.
3315 *
3316 * This function receives events for the netdev chain. The caller (an
e041c683 3317 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
1da177e4
LT
3318 * locks for us to safely manipulate the slave devices (RTNL lock,
3319 * dev_probe_lock).
3320 */
3321static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3322{
3323 struct net_device *event_dev = (struct net_device *)ptr;
3324
e9dc8653
EB
3325 if (event_dev->nd_net != &init_net)
3326 return NOTIFY_DONE;
3327
1da177e4
LT
3328 dprintk("event_dev: %s, event: %lx\n",
3329 (event_dev ? event_dev->name : "None"),
3330 event);
3331
0b680e75
JV
3332 if (!(event_dev->priv_flags & IFF_BONDING))
3333 return NOTIFY_DONE;
3334
1da177e4
LT
3335 if (event_dev->flags & IFF_MASTER) {
3336 dprintk("IFF_MASTER\n");
3337 return bond_master_netdev_event(event, event_dev);
3338 }
3339
3340 if (event_dev->flags & IFF_SLAVE) {
3341 dprintk("IFF_SLAVE\n");
3342 return bond_slave_netdev_event(event, event_dev);
3343 }
3344
3345 return NOTIFY_DONE;
3346}
3347
c3ade5ca
JV
3348/*
3349 * bond_inetaddr_event: handle inetaddr notifier chain events.
3350 *
3351 * We keep track of device IPs primarily to use as source addresses in
3352 * ARP monitor probes (rather than spewing out broadcasts all the time).
3353 *
3354 * We track one IP for the main device (if it has one), plus one per VLAN.
3355 */
3356static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3357{
3358 struct in_ifaddr *ifa = ptr;
3359 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3360 struct bonding *bond, *bond_next;
3361 struct vlan_entry *vlan, *vlan_next;
3362
3363 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3364 if (bond->dev == event_dev) {
3365 switch (event) {
3366 case NETDEV_UP:
3367 bond->master_ip = ifa->ifa_local;
3368 return NOTIFY_OK;
3369 case NETDEV_DOWN:
3370 bond->master_ip = bond_glean_dev_ip(bond->dev);
3371 return NOTIFY_OK;
3372 default:
3373 return NOTIFY_DONE;
3374 }
3375 }
3376
3377 if (list_empty(&bond->vlan_list))
3378 continue;
3379
3380 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3381 vlan_list) {
5c15bdec 3382 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
c3ade5ca
JV
3383 if (vlan_dev == event_dev) {
3384 switch (event) {
3385 case NETDEV_UP:
3386 vlan->vlan_ip = ifa->ifa_local;
3387 return NOTIFY_OK;
3388 case NETDEV_DOWN:
3389 vlan->vlan_ip =
3390 bond_glean_dev_ip(vlan_dev);
3391 return NOTIFY_OK;
3392 default:
3393 return NOTIFY_DONE;
3394 }
3395 }
3396 }
3397 }
3398 return NOTIFY_DONE;
3399}
3400
1da177e4
LT
3401static struct notifier_block bond_netdev_notifier = {
3402 .notifier_call = bond_netdev_event,
3403};
3404
c3ade5ca
JV
3405static struct notifier_block bond_inetaddr_notifier = {
3406 .notifier_call = bond_inetaddr_event,
3407};
3408
1da177e4
LT
3409/*-------------------------- Packet type handling ---------------------------*/
3410
3411/* register to receive lacpdus on a bond */
3412static void bond_register_lacpdu(struct bonding *bond)
3413{
3414 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3415
3416 /* initialize packet type */
3417 pk_type->type = PKT_TYPE_LACPDU;
3418 pk_type->dev = bond->dev;
3419 pk_type->func = bond_3ad_lacpdu_recv;
3420
3421 dev_add_pack(pk_type);
3422}
3423
3424/* unregister to receive lacpdus on a bond */
3425static void bond_unregister_lacpdu(struct bonding *bond)
3426{
3427 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3428}
3429
f5b2b966
JV
3430void bond_register_arp(struct bonding *bond)
3431{
3432 struct packet_type *pt = &bond->arp_mon_pt;
3433
c4f283b1
JV
3434 if (pt->type)
3435 return;
3436
f5b2b966 3437 pt->type = htons(ETH_P_ARP);
e245cb71 3438 pt->dev = bond->dev;
f5b2b966
JV
3439 pt->func = bond_arp_rcv;
3440 dev_add_pack(pt);
3441}
3442
3443void bond_unregister_arp(struct bonding *bond)
3444{
c4f283b1
JV
3445 struct packet_type *pt = &bond->arp_mon_pt;
3446
3447 dev_remove_pack(pt);
3448 pt->type = 0;
f5b2b966
JV
3449}
3450
169a3e66
JV
3451/*---------------------------- Hashing Policies -----------------------------*/
3452
3453/*
59c51591 3454 * Hash for the output device based upon layer 3 and layer 4 data. If
169a3e66
JV
3455 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3456 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3457 */
3458static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3459 struct net_device *bond_dev, int count)
3460{
3461 struct ethhdr *data = (struct ethhdr *)skb->data;
eddc9ec5 3462 struct iphdr *iph = ip_hdr(skb);
d3bb52b0 3463 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
169a3e66
JV
3464 int layer4_xor = 0;
3465
3466 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3467 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3468 (iph->protocol == IPPROTO_TCP ||
3469 iph->protocol == IPPROTO_UDP)) {
d3bb52b0 3470 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
169a3e66
JV
3471 }
3472 return (layer4_xor ^
3473 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3474
3475 }
3476
3477 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3478}
3479
3480/*
3481 * Hash for the output device based upon layer 2 data
3482 */
3483static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3484 struct net_device *bond_dev, int count)
3485{
3486 struct ethhdr *data = (struct ethhdr *)skb->data;
3487
3488 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3489}
3490
1da177e4
LT
3491/*-------------------------- Device entry points ----------------------------*/
3492
3493static int bond_open(struct net_device *bond_dev)
3494{
3495 struct bonding *bond = bond_dev->priv;
3496 struct timer_list *mii_timer = &bond->mii_timer;
3497 struct timer_list *arp_timer = &bond->arp_timer;
3498
3499 bond->kill_timers = 0;
3500
3501 if ((bond->params.mode == BOND_MODE_TLB) ||
3502 (bond->params.mode == BOND_MODE_ALB)) {
3503 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3504
3505 /* bond_alb_initialize must be called before the timer
3506 * is started.
3507 */
3508 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3509 /* something went wrong - fail the open operation */
3510 return -1;
3511 }
3512
3513 init_timer(alb_timer);
3514 alb_timer->expires = jiffies + 1;
3515 alb_timer->data = (unsigned long)bond;
3516 alb_timer->function = (void *)&bond_alb_monitor;
3517 add_timer(alb_timer);
3518 }
3519
3520 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3521 init_timer(mii_timer);
3522 mii_timer->expires = jiffies + 1;
3523 mii_timer->data = (unsigned long)bond_dev;
3524 mii_timer->function = (void *)&bond_mii_monitor;
3525 add_timer(mii_timer);
3526 }
3527
3528 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3529 init_timer(arp_timer);
3530 arp_timer->expires = jiffies + 1;
3531 arp_timer->data = (unsigned long)bond_dev;
3532 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3533 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3534 } else {
3535 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3536 }
f5b2b966
JV
3537 if (bond->params.arp_validate)
3538 bond_register_arp(bond);
3539
1da177e4
LT
3540 add_timer(arp_timer);
3541 }
3542
3543 if (bond->params.mode == BOND_MODE_8023AD) {
3544 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3545 init_timer(ad_timer);
3546 ad_timer->expires = jiffies + 1;
3547 ad_timer->data = (unsigned long)bond;
3548 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3549 add_timer(ad_timer);
3550
3551 /* register to receive LACPDUs */
3552 bond_register_lacpdu(bond);
3553 }
3554
3555 return 0;
3556}
3557
3558static int bond_close(struct net_device *bond_dev)
3559{
3560 struct bonding *bond = bond_dev->priv;
3561
3562 if (bond->params.mode == BOND_MODE_8023AD) {
3563 /* Unregister the receive of LACPDUs */
3564 bond_unregister_lacpdu(bond);
3565 }
3566
f5b2b966
JV
3567 if (bond->params.arp_validate)
3568 bond_unregister_arp(bond);
3569
1da177e4
LT
3570 write_lock_bh(&bond->lock);
3571
1da177e4
LT
3572
3573 /* signal timers not to re-arm */
3574 bond->kill_timers = 1;
3575
3576 write_unlock_bh(&bond->lock);
3577
3578 /* del_timer_sync must run without holding the bond->lock
3579 * because a running timer might be trying to hold it too
3580 */
3581
3582 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3583 del_timer_sync(&bond->mii_timer);
3584 }
3585
3586 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3587 del_timer_sync(&bond->arp_timer);
3588 }
3589
3590 switch (bond->params.mode) {
3591 case BOND_MODE_8023AD:
3592 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3593 break;
3594 case BOND_MODE_TLB:
3595 case BOND_MODE_ALB:
3596 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3597 break;
3598 default:
3599 break;
3600 }
3601
1da177e4
LT
3602
3603 if ((bond->params.mode == BOND_MODE_TLB) ||
3604 (bond->params.mode == BOND_MODE_ALB)) {
3605 /* Must be called only after all
3606 * slaves have been released
3607 */
3608 bond_alb_deinitialize(bond);
3609 }
3610
3611 return 0;
3612}
3613
3614static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3615{
3616 struct bonding *bond = bond_dev->priv;
3617 struct net_device_stats *stats = &(bond->stats), *sstats;
3618 struct slave *slave;
3619 int i;
3620
3621 memset(stats, 0, sizeof(struct net_device_stats));
3622
3623 read_lock_bh(&bond->lock);
3624
3625 bond_for_each_slave(bond, slave, i) {
c45d286e 3626 sstats = slave->dev->get_stats(slave->dev);
5a1b5898
RR
3627 stats->rx_packets += sstats->rx_packets;
3628 stats->rx_bytes += sstats->rx_bytes;
3629 stats->rx_errors += sstats->rx_errors;
3630 stats->rx_dropped += sstats->rx_dropped;
3631
3632 stats->tx_packets += sstats->tx_packets;
3633 stats->tx_bytes += sstats->tx_bytes;
3634 stats->tx_errors += sstats->tx_errors;
3635 stats->tx_dropped += sstats->tx_dropped;
3636
3637 stats->multicast += sstats->multicast;
3638 stats->collisions += sstats->collisions;
3639
3640 stats->rx_length_errors += sstats->rx_length_errors;
3641 stats->rx_over_errors += sstats->rx_over_errors;
3642 stats->rx_crc_errors += sstats->rx_crc_errors;
3643 stats->rx_frame_errors += sstats->rx_frame_errors;
3644 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3645 stats->rx_missed_errors += sstats->rx_missed_errors;
3646
3647 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3648 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3649 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3650 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3651 stats->tx_window_errors += sstats->tx_window_errors;
1da177e4
LT
3652 }
3653
3654 read_unlock_bh(&bond->lock);
3655
3656 return stats;
3657}
3658
3659static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3660{
3661 struct net_device *slave_dev = NULL;
3662 struct ifbond k_binfo;
3663 struct ifbond __user *u_binfo = NULL;
3664 struct ifslave k_sinfo;
3665 struct ifslave __user *u_sinfo = NULL;
3666 struct mii_ioctl_data *mii = NULL;
1da177e4
LT
3667 int res = 0;
3668
3669 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3670 bond_dev->name, cmd);
3671
3672 switch (cmd) {
1da177e4
LT
3673 case SIOCGMIIPHY:
3674 mii = if_mii(ifr);
3675 if (!mii) {
3676 return -EINVAL;
3677 }
3678 mii->phy_id = 0;
3679 /* Fall Through */
3680 case SIOCGMIIREG:
3681 /*
3682 * We do this again just in case we were called by SIOCGMIIREG
3683 * instead of SIOCGMIIPHY.
3684 */
3685 mii = if_mii(ifr);
3686 if (!mii) {
3687 return -EINVAL;
3688 }
3689
3690 if (mii->reg_num == 1) {
3691 struct bonding *bond = bond_dev->priv;
3692 mii->val_out = 0;
3693 read_lock_bh(&bond->lock);
3694 read_lock(&bond->curr_slave_lock);
4e140079 3695 if (netif_carrier_ok(bond->dev)) {
1da177e4
LT
3696 mii->val_out = BMSR_LSTATUS;
3697 }
3698 read_unlock(&bond->curr_slave_lock);
3699 read_unlock_bh(&bond->lock);
3700 }
3701
3702 return 0;
3703 case BOND_INFO_QUERY_OLD:
3704 case SIOCBONDINFOQUERY:
3705 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3706
3707 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3708 return -EFAULT;
3709 }
3710
3711 res = bond_info_query(bond_dev, &k_binfo);
3712 if (res == 0) {
3713 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3714 return -EFAULT;
3715 }
3716 }
3717
3718 return res;
3719 case BOND_SLAVE_INFO_QUERY_OLD:
3720 case SIOCBONDSLAVEINFOQUERY:
3721 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3722
3723 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3724 return -EFAULT;
3725 }
3726
3727 res = bond_slave_info_query(bond_dev, &k_sinfo);
3728 if (res == 0) {
3729 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3730 return -EFAULT;
3731 }
3732 }
3733
3734 return res;
3735 default:
3736 /* Go on */
3737 break;
3738 }
3739
3740 if (!capable(CAP_NET_ADMIN)) {
3741 return -EPERM;
3742 }
3743
b76cdba9 3744 down_write(&(bonding_rwsem));
881d966b 3745 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
1da177e4
LT
3746
3747 dprintk("slave_dev=%p: \n", slave_dev);
3748
3749 if (!slave_dev) {
3750 res = -ENODEV;
3751 } else {
3752 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3753 switch (cmd) {
3754 case BOND_ENSLAVE_OLD:
3755 case SIOCBONDENSLAVE:
3756 res = bond_enslave(bond_dev, slave_dev);
3757 break;
3758 case BOND_RELEASE_OLD:
3759 case SIOCBONDRELEASE:
3760 res = bond_release(bond_dev, slave_dev);
3761 break;
3762 case BOND_SETHWADDR_OLD:
3763 case SIOCBONDSETHWADDR:
3764 res = bond_sethwaddr(bond_dev, slave_dev);
3765 break;
3766 case BOND_CHANGE_ACTIVE_OLD:
3767 case SIOCBONDCHANGEACTIVE:
3768 res = bond_ioctl_change_active(bond_dev, slave_dev);
3769 break;
3770 default:
3771 res = -EOPNOTSUPP;
3772 }
3773
3774 dev_put(slave_dev);
3775 }
3776
b76cdba9 3777 up_write(&(bonding_rwsem));
1da177e4
LT
3778 return res;
3779}
3780
3781static void bond_set_multicast_list(struct net_device *bond_dev)
3782{
3783 struct bonding *bond = bond_dev->priv;
3784 struct dev_mc_list *dmi;
3785
3786 write_lock_bh(&bond->lock);
3787
3788 /*
3789 * Do promisc before checking multicast_mode
3790 */
3791 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3792 bond_set_promiscuity(bond, 1);
3793 }
3794
3795 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3796 bond_set_promiscuity(bond, -1);
3797 }
3798
3799 /* set allmulti flag to slaves */
3800 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3801 bond_set_allmulti(bond, 1);
3802 }
3803
3804 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3805 bond_set_allmulti(bond, -1);
3806 }
3807
3808 bond->flags = bond_dev->flags;
3809
3810 /* looking for addresses to add to slaves' mc list */
3811 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3812 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3813 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3814 }
3815 }
3816
3817 /* looking for addresses to delete from slaves' list */
3818 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3819 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3820 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3821 }
3822 }
3823
3824 /* save master's multicast list */
3825 bond_mc_list_destroy(bond);
3826 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3827
3828 write_unlock_bh(&bond->lock);
3829}
3830
3831/*
3832 * Change the MTU of all of a master's slaves to match the master
3833 */
3834static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3835{
3836 struct bonding *bond = bond_dev->priv;
3837 struct slave *slave, *stop_at;
3838 int res = 0;
3839 int i;
3840
3841 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3842 (bond_dev ? bond_dev->name : "None"), new_mtu);
3843
3844 /* Can't hold bond->lock with bh disabled here since
3845 * some base drivers panic. On the other hand we can't
3846 * hold bond->lock without bh disabled because we'll
3847 * deadlock. The only solution is to rely on the fact
3848 * that we're under rtnl_lock here, and the slaves
3849 * list won't change. This doesn't solve the problem
3850 * of setting the slave's MTU while it is
3851 * transmitting, but the assumption is that the base
3852 * driver can handle that.
3853 *
3854 * TODO: figure out a way to safely iterate the slaves
3855 * list, but without holding a lock around the actual
3856 * call to the base driver.
3857 */
3858
3859 bond_for_each_slave(bond, slave, i) {
3860 dprintk("s %p s->p %p c_m %p\n", slave,
3861 slave->prev, slave->dev->change_mtu);
e944ef79 3862
1da177e4
LT
3863 res = dev_set_mtu(slave->dev, new_mtu);
3864
3865 if (res) {
3866 /* If we failed to set the slave's mtu to the new value
3867 * we must abort the operation even in ACTIVE_BACKUP
3868 * mode, because if we allow the backup slaves to have
3869 * different mtu values than the active slave we'll
3870 * need to change their mtu when doing a failover. That
3871 * means changing their mtu from timer context, which
3872 * is probably not a good idea.
3873 */
3874 dprintk("err %d %s\n", res, slave->dev->name);
3875 goto unwind;
3876 }
3877 }
3878
3879 bond_dev->mtu = new_mtu;
3880
3881 return 0;
3882
3883unwind:
3884 /* unwind from head to the slave that failed */
3885 stop_at = slave;
3886 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3887 int tmp_res;
3888
3889 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3890 if (tmp_res) {
3891 dprintk("unwind err %d dev %s\n", tmp_res,
3892 slave->dev->name);
3893 }
3894 }
3895
3896 return res;
3897}
3898
3899/*
3900 * Change HW address
3901 *
3902 * Note that many devices must be down to change the HW address, and
3903 * downing the master releases all slaves. We can make bonds full of
3904 * bonding devices to test this, however.
3905 */
3906static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3907{
3908 struct bonding *bond = bond_dev->priv;
3909 struct sockaddr *sa = addr, tmp_sa;
3910 struct slave *slave, *stop_at;
3911 int res = 0;
3912 int i;
3913
3914 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3915
3916 if (!is_valid_ether_addr(sa->sa_data)) {
3917 return -EADDRNOTAVAIL;
3918 }
3919
3920 /* Can't hold bond->lock with bh disabled here since
3921 * some base drivers panic. On the other hand we can't
3922 * hold bond->lock without bh disabled because we'll
3923 * deadlock. The only solution is to rely on the fact
3924 * that we're under rtnl_lock here, and the slaves
3925 * list won't change. This doesn't solve the problem
3926 * of setting the slave's hw address while it is
3927 * transmitting, but the assumption is that the base
3928 * driver can handle that.
3929 *
3930 * TODO: figure out a way to safely iterate the slaves
3931 * list, but without holding a lock around the actual
3932 * call to the base driver.
3933 */
3934
3935 bond_for_each_slave(bond, slave, i) {
3936 dprintk("slave %p %s\n", slave, slave->dev->name);
3937
3938 if (slave->dev->set_mac_address == NULL) {
3939 res = -EOPNOTSUPP;
3940 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
3941 goto unwind;
3942 }
3943
3944 res = dev_set_mac_address(slave->dev, addr);
3945 if (res) {
3946 /* TODO: consider downing the slave
3947 * and retry ?
3948 * User should expect communications
3949 * breakage anyway until ARP finish
3950 * updating, so...
3951 */
3952 dprintk("err %d %s\n", res, slave->dev->name);
3953 goto unwind;
3954 }
3955 }
3956
3957 /* success */
3958 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3959 return 0;
3960
3961unwind:
3962 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3963 tmp_sa.sa_family = bond_dev->type;
3964
3965 /* unwind from head to the slave that failed */
3966 stop_at = slave;
3967 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3968 int tmp_res;
3969
3970 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3971 if (tmp_res) {
3972 dprintk("unwind err %d dev %s\n", tmp_res,
3973 slave->dev->name);
3974 }
3975 }
3976
3977 return res;
3978}
3979
3980static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3981{
3982 struct bonding *bond = bond_dev->priv;
3983 struct slave *slave, *start_at;
3984 int i;
3985 int res = 1;
3986
3987 read_lock(&bond->lock);
3988
3989 if (!BOND_IS_OK(bond)) {
3990 goto out;
3991 }
3992
3993 read_lock(&bond->curr_slave_lock);
3994 slave = start_at = bond->curr_active_slave;
3995 read_unlock(&bond->curr_slave_lock);
3996
3997 if (!slave) {
3998 goto out;
3999 }
4000
4001 bond_for_each_slave_from(bond, slave, i, start_at) {
4002 if (IS_UP(slave->dev) &&
4003 (slave->link == BOND_LINK_UP) &&
4004 (slave->state == BOND_STATE_ACTIVE)) {
4005 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4006
4007 write_lock(&bond->curr_slave_lock);
4008 bond->curr_active_slave = slave->next;
4009 write_unlock(&bond->curr_slave_lock);
4010
4011 break;
4012 }
4013 }
4014
4015
4016out:
4017 if (res) {
4018 /* no suitable interface, frame not sent */
4019 dev_kfree_skb(skb);
4020 }
4021 read_unlock(&bond->lock);
4022 return 0;
4023}
4024
075897ce 4025
1da177e4
LT
4026/*
4027 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4028 * the bond has a usable interface.
4029 */
4030static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4031{
4032 struct bonding *bond = bond_dev->priv;
4033 int res = 1;
4034
1da177e4
LT
4035 read_lock(&bond->lock);
4036 read_lock(&bond->curr_slave_lock);
4037
4038 if (!BOND_IS_OK(bond)) {
4039 goto out;
4040 }
4041
075897ce
JL
4042 if (!bond->curr_active_slave)
4043 goto out;
4044
075897ce
JL
4045 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4046
1da177e4
LT
4047out:
4048 if (res) {
4049 /* no suitable interface, frame not sent */
4050 dev_kfree_skb(skb);
4051 }
4052 read_unlock(&bond->curr_slave_lock);
4053 read_unlock(&bond->lock);
4054 return 0;
4055}
4056
4057/*
169a3e66
JV
4058 * In bond_xmit_xor() , we determine the output device by using a pre-
4059 * determined xmit_hash_policy(), If the selected device is not enabled,
4060 * find the next active slave.
1da177e4
LT
4061 */
4062static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4063{
4064 struct bonding *bond = bond_dev->priv;
1da177e4
LT
4065 struct slave *slave, *start_at;
4066 int slave_no;
4067 int i;
4068 int res = 1;
4069
4070 read_lock(&bond->lock);
4071
4072 if (!BOND_IS_OK(bond)) {
4073 goto out;
4074 }
4075
169a3e66 4076 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
1da177e4
LT
4077
4078 bond_for_each_slave(bond, slave, i) {
4079 slave_no--;
4080 if (slave_no < 0) {
4081 break;
4082 }
4083 }
4084
4085 start_at = slave;
4086
4087 bond_for_each_slave_from(bond, slave, i, start_at) {
4088 if (IS_UP(slave->dev) &&
4089 (slave->link == BOND_LINK_UP) &&
4090 (slave->state == BOND_STATE_ACTIVE)) {
4091 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4092 break;
4093 }
4094 }
4095
4096out:
4097 if (res) {
4098 /* no suitable interface, frame not sent */
4099 dev_kfree_skb(skb);
4100 }
4101 read_unlock(&bond->lock);
4102 return 0;
4103}
4104
4105/*
4106 * in broadcast mode, we send everything to all usable interfaces.
4107 */
4108static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4109{
4110 struct bonding *bond = bond_dev->priv;
4111 struct slave *slave, *start_at;
4112 struct net_device *tx_dev = NULL;
4113 int i;
4114 int res = 1;
4115
4116 read_lock(&bond->lock);
4117
4118 if (!BOND_IS_OK(bond)) {
4119 goto out;
4120 }
4121
4122 read_lock(&bond->curr_slave_lock);
4123 start_at = bond->curr_active_slave;
4124 read_unlock(&bond->curr_slave_lock);
4125
4126 if (!start_at) {
4127 goto out;
4128 }
4129
4130 bond_for_each_slave_from(bond, slave, i, start_at) {
4131 if (IS_UP(slave->dev) &&
4132 (slave->link == BOND_LINK_UP) &&
4133 (slave->state == BOND_STATE_ACTIVE)) {
4134 if (tx_dev) {
4135 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4136 if (!skb2) {
4137 printk(KERN_ERR DRV_NAME
4e0952c7
MW
4138 ": %s: Error: bond_xmit_broadcast(): "
4139 "skb_clone() failed\n",
4140 bond_dev->name);
1da177e4
LT
4141 continue;
4142 }
4143
4144 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4145 if (res) {
4146 dev_kfree_skb(skb2);
4147 continue;
4148 }
4149 }
4150 tx_dev = slave->dev;
4151 }
4152 }
4153
4154 if (tx_dev) {
4155 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4156 }
4157
4158out:
4159 if (res) {
4160 /* no suitable interface, frame not sent */
4161 dev_kfree_skb(skb);
4162 }
4163 /* frame sent to all suitable interfaces */
4164 read_unlock(&bond->lock);
4165 return 0;
4166}
4167
4168/*------------------------- Device initialization ---------------------------*/
4169
4170/*
4171 * set bond mode specific net device operations
4172 */
a77b5325 4173void bond_set_mode_ops(struct bonding *bond, int mode)
1da177e4 4174{
169a3e66
JV
4175 struct net_device *bond_dev = bond->dev;
4176
1da177e4
LT
4177 switch (mode) {
4178 case BOND_MODE_ROUNDROBIN:
4179 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4180 break;
4181 case BOND_MODE_ACTIVEBACKUP:
4182 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4183 break;
4184 case BOND_MODE_XOR:
4185 bond_dev->hard_start_xmit = bond_xmit_xor;
169a3e66
JV
4186 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4187 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4188 else
4189 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
1da177e4
LT
4190 break;
4191 case BOND_MODE_BROADCAST:
4192 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4193 break;
4194 case BOND_MODE_8023AD:
8f903c70 4195 bond_set_master_3ad_flags(bond);
1da177e4 4196 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
169a3e66
JV
4197 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4198 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4199 else
4200 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
1da177e4 4201 break;
1da177e4 4202 case BOND_MODE_ALB:
8f903c70
JV
4203 bond_set_master_alb_flags(bond);
4204 /* FALLTHRU */
4205 case BOND_MODE_TLB:
1da177e4
LT
4206 bond_dev->hard_start_xmit = bond_alb_xmit;
4207 bond_dev->set_mac_address = bond_alb_set_mac_address;
4208 break;
4209 default:
4210 /* Should never happen, mode already checked */
4211 printk(KERN_ERR DRV_NAME
4e0952c7
MW
4212 ": %s: Error: Unknown bonding mode %d\n",
4213 bond_dev->name,
1da177e4
LT
4214 mode);
4215 break;
4216 }
4217}
4218
217df670
JV
4219static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4220 struct ethtool_drvinfo *drvinfo)
4221{
4222 strncpy(drvinfo->driver, DRV_NAME, 32);
4223 strncpy(drvinfo->version, DRV_VERSION, 32);
4224 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4225}
4226
7282d491 4227static const struct ethtool_ops bond_ethtool_ops = {
217df670 4228 .get_drvinfo = bond_ethtool_get_drvinfo,
8531c5ff
AK
4229};
4230
1da177e4
LT
4231/*
4232 * Does not allocate but creates a /proc entry.
4233 * Allowed to fail.
4234 */
3c535952 4235static int bond_init(struct net_device *bond_dev, struct bond_params *params)
1da177e4
LT
4236{
4237 struct bonding *bond = bond_dev->priv;
4238
4239 dprintk("Begin bond_init for %s\n", bond_dev->name);
4240
4241 /* initialize rwlocks */
4242 rwlock_init(&bond->lock);
4243 rwlock_init(&bond->curr_slave_lock);
4244
4245 bond->params = *params; /* copy params struct */
4246
4247 /* Initialize pointers */
4248 bond->first_slave = NULL;
4249 bond->curr_active_slave = NULL;
4250 bond->current_arp_slave = NULL;
4251 bond->primary_slave = NULL;
4252 bond->dev = bond_dev;
4253 INIT_LIST_HEAD(&bond->vlan_list);
4254
4255 /* Initialize the device entry points */
4256 bond_dev->open = bond_open;
4257 bond_dev->stop = bond_close;
4258 bond_dev->get_stats = bond_get_stats;
4259 bond_dev->do_ioctl = bond_do_ioctl;
8531c5ff 4260 bond_dev->ethtool_ops = &bond_ethtool_ops;
1da177e4
LT
4261 bond_dev->set_multicast_list = bond_set_multicast_list;
4262 bond_dev->change_mtu = bond_change_mtu;
4263 bond_dev->set_mac_address = bond_set_mac_address;
4264
169a3e66 4265 bond_set_mode_ops(bond, bond->params.mode);
1da177e4
LT
4266
4267 bond_dev->destructor = free_netdev;
4268
4269 /* Initialize the device options */
4270 bond_dev->tx_queue_len = 0;
4271 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
0b680e75 4272 bond_dev->priv_flags |= IFF_BONDING;
1da177e4
LT
4273
4274 /* At first, we block adding VLANs. That's the only way to
4275 * prevent problems that occur when adding VLANs over an
4276 * empty bond. The block will be removed once non-challenged
4277 * slaves are enslaved.
4278 */
4279 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4280
932ff279 4281 /* don't acquire bond device's netif_tx_lock when
1da177e4
LT
4282 * transmitting */
4283 bond_dev->features |= NETIF_F_LLTX;
4284
4285 /* By default, we declare the bond to be fully
4286 * VLAN hardware accelerated capable. Special
4287 * care is taken in the various xmit functions
4288 * when there are slaves that are not hw accel
4289 * capable
4290 */
4291 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4292 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4293 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4294 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4295 NETIF_F_HW_VLAN_RX |
4296 NETIF_F_HW_VLAN_FILTER);
4297
4298#ifdef CONFIG_PROC_FS
4299 bond_create_proc_entry(bond);
4300#endif
4301
4302 list_add_tail(&bond->bond_list, &bond_dev_list);
4303
4304 return 0;
4305}
4306
4307/* De-initialize device specific data.
4308 * Caller must hold rtnl_lock.
4309 */
a77b5325 4310void bond_deinit(struct net_device *bond_dev)
1da177e4
LT
4311{
4312 struct bonding *bond = bond_dev->priv;
4313
4314 list_del(&bond->bond_list);
4315
4316#ifdef CONFIG_PROC_FS
4317 bond_remove_proc_entry(bond);
4318#endif
4319}
4320
4321/* Unregister and free all bond devices.
4322 * Caller must hold rtnl_lock.
4323 */
4324static void bond_free_all(void)
4325{
4326 struct bonding *bond, *nxt;
4327
4328 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4329 struct net_device *bond_dev = bond->dev;
4330
70298705 4331 bond_mc_list_destroy(bond);
4332 /* Release the bonded slaves */
4333 bond_release_all(bond_dev);
1da177e4 4334 bond_deinit(bond_dev);
3201e656 4335 unregister_netdevice(bond_dev);
1da177e4
LT
4336 }
4337
4338#ifdef CONFIG_PROC_FS
4339 bond_destroy_proc_dir();
4340#endif
4341}
4342
4343/*------------------------- Module initialization ---------------------------*/
4344
4345/*
4346 * Convert string input module parms. Accept either the
4347 * number of the mode or its string name.
4348 */
a77b5325 4349int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
1da177e4
LT
4350{
4351 int i;
4352
4353 for (i = 0; tbl[i].modename; i++) {
4354 if ((isdigit(*mode_arg) &&
4355 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4356 (strncmp(mode_arg, tbl[i].modename,
4357 strlen(tbl[i].modename)) == 0)) {
4358 return tbl[i].mode;
4359 }
4360 }
4361
4362 return -1;
4363}
4364
4365static int bond_check_params(struct bond_params *params)
4366{
f5b2b966
JV
4367 int arp_validate_value;
4368
1da177e4
LT
4369 /*
4370 * Convert string parameters.
4371 */
4372 if (mode) {
4373 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4374 if (bond_mode == -1) {
4375 printk(KERN_ERR DRV_NAME
4376 ": Error: Invalid bonding mode \"%s\"\n",
4377 mode == NULL ? "NULL" : mode);
4378 return -EINVAL;
4379 }
4380 }
4381
169a3e66
JV
4382 if (xmit_hash_policy) {
4383 if ((bond_mode != BOND_MODE_XOR) &&
4384 (bond_mode != BOND_MODE_8023AD)) {
4385 printk(KERN_INFO DRV_NAME
4386 ": xor_mode param is irrelevant in mode %s\n",
4387 bond_mode_name(bond_mode));
4388 } else {
4389 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4390 xmit_hashtype_tbl);
4391 if (xmit_hashtype == -1) {
4392 printk(KERN_ERR DRV_NAME
4393 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4394 xmit_hash_policy == NULL ? "NULL" :
4395 xmit_hash_policy);
4396 return -EINVAL;
4397 }
4398 }
4399 }
4400
1da177e4
LT
4401 if (lacp_rate) {
4402 if (bond_mode != BOND_MODE_8023AD) {
4403 printk(KERN_INFO DRV_NAME
4404 ": lacp_rate param is irrelevant in mode %s\n",
4405 bond_mode_name(bond_mode));
4406 } else {
4407 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4408 if (lacp_fast == -1) {
4409 printk(KERN_ERR DRV_NAME
4410 ": Error: Invalid lacp rate \"%s\"\n",
4411 lacp_rate == NULL ? "NULL" : lacp_rate);
4412 return -EINVAL;
4413 }
4414 }
4415 }
4416
4417 if (max_bonds < 1 || max_bonds > INT_MAX) {
4418 printk(KERN_WARNING DRV_NAME
4419 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4e0952c7 4420 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
1da177e4
LT
4421 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4422 max_bonds = BOND_DEFAULT_MAX_BONDS;
4423 }
4424
4425 if (miimon < 0) {
4426 printk(KERN_WARNING DRV_NAME
4427 ": Warning: miimon module parameter (%d), "
4428 "not in range 0-%d, so it was reset to %d\n",
4429 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4430 miimon = BOND_LINK_MON_INTERV;
4431 }
4432
4433 if (updelay < 0) {
4434 printk(KERN_WARNING DRV_NAME
4435 ": Warning: updelay module parameter (%d), "
4436 "not in range 0-%d, so it was reset to 0\n",
4437 updelay, INT_MAX);
4438 updelay = 0;
4439 }
4440
4441 if (downdelay < 0) {
4442 printk(KERN_WARNING DRV_NAME
4443 ": Warning: downdelay module parameter (%d), "
4444 "not in range 0-%d, so it was reset to 0\n",
4445 downdelay, INT_MAX);
4446 downdelay = 0;
4447 }
4448
4449 if ((use_carrier != 0) && (use_carrier != 1)) {
4450 printk(KERN_WARNING DRV_NAME
4451 ": Warning: use_carrier module parameter (%d), "
4452 "not of valid value (0/1), so it was set to 1\n",
4453 use_carrier);
4454 use_carrier = 1;
4455 }
4456
4457 /* reset values for 802.3ad */
4458 if (bond_mode == BOND_MODE_8023AD) {
4459 if (!miimon) {
4460 printk(KERN_WARNING DRV_NAME
4461 ": Warning: miimon must be specified, "
4462 "otherwise bonding will not detect link "
4463 "failure, speed and duplex which are "
4464 "essential for 802.3ad operation\n");
4465 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4466 miimon = 100;
4467 }
4468 }
4469
4470 /* reset values for TLB/ALB */
4471 if ((bond_mode == BOND_MODE_TLB) ||
4472 (bond_mode == BOND_MODE_ALB)) {
4473 if (!miimon) {
4474 printk(KERN_WARNING DRV_NAME
4475 ": Warning: miimon must be specified, "
4476 "otherwise bonding will not detect link "
4477 "failure and link speed which are essential "
4478 "for TLB/ALB load balancing\n");
4479 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4480 miimon = 100;
4481 }
4482 }
4483
4484 if (bond_mode == BOND_MODE_ALB) {
4485 printk(KERN_NOTICE DRV_NAME
4486 ": In ALB mode you might experience client "
4487 "disconnections upon reconnection of a link if the "
4488 "bonding module updelay parameter (%d msec) is "
4489 "incompatible with the forwarding delay time of the "
4490 "switch\n",
4491 updelay);
4492 }
4493
4494 if (!miimon) {
4495 if (updelay || downdelay) {
4496 /* just warn the user the up/down delay will have
4497 * no effect since miimon is zero...
4498 */
4499 printk(KERN_WARNING DRV_NAME
4500 ": Warning: miimon module parameter not set "
4501 "and updelay (%d) or downdelay (%d) module "
4502 "parameter is set; updelay and downdelay have "
4503 "no effect unless miimon is set\n",
4504 updelay, downdelay);
4505 }
4506 } else {
4507 /* don't allow arp monitoring */
4508 if (arp_interval) {
4509 printk(KERN_WARNING DRV_NAME
4510 ": Warning: miimon (%d) and arp_interval (%d) "
4511 "can't be used simultaneously, disabling ARP "
4512 "monitoring\n",
4513 miimon, arp_interval);
4514 arp_interval = 0;
4515 }
4516
4517 if ((updelay % miimon) != 0) {
4518 printk(KERN_WARNING DRV_NAME
4519 ": Warning: updelay (%d) is not a multiple "
4520 "of miimon (%d), updelay rounded to %d ms\n",
4521 updelay, miimon, (updelay / miimon) * miimon);
4522 }
4523
4524 updelay /= miimon;
4525
4526 if ((downdelay % miimon) != 0) {
4527 printk(KERN_WARNING DRV_NAME
4528 ": Warning: downdelay (%d) is not a multiple "
4529 "of miimon (%d), downdelay rounded to %d ms\n",
4530 downdelay, miimon,
4531 (downdelay / miimon) * miimon);
4532 }
4533
4534 downdelay /= miimon;
4535 }
4536
4537 if (arp_interval < 0) {
4538 printk(KERN_WARNING DRV_NAME
4539 ": Warning: arp_interval module parameter (%d) "
4540 ", not in range 0-%d, so it was reset to %d\n",
4541 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4542 arp_interval = BOND_LINK_ARP_INTERV;
4543 }
4544
4545 for (arp_ip_count = 0;
4546 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4547 arp_ip_count++) {
4548 /* not complete check, but should be good enough to
4549 catch mistakes */
4550 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4551 printk(KERN_WARNING DRV_NAME
4552 ": Warning: bad arp_ip_target module parameter "
4553 "(%s), ARP monitoring will not be performed\n",
4554 arp_ip_target[arp_ip_count]);
4555 arp_interval = 0;
4556 } else {
d3bb52b0 4557 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
1da177e4
LT
4558 arp_target[arp_ip_count] = ip;
4559 }
4560 }
4561
4562 if (arp_interval && !arp_ip_count) {
4563 /* don't allow arping if no arp_ip_target given... */
4564 printk(KERN_WARNING DRV_NAME
4565 ": Warning: arp_interval module parameter (%d) "
4566 "specified without providing an arp_ip_target "
4567 "parameter, arp_interval was reset to 0\n",
4568 arp_interval);
4569 arp_interval = 0;
4570 }
4571
f5b2b966
JV
4572 if (arp_validate) {
4573 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4574 printk(KERN_ERR DRV_NAME
4575 ": arp_validate only supported in active-backup mode\n");
4576 return -EINVAL;
4577 }
4578 if (!arp_interval) {
4579 printk(KERN_ERR DRV_NAME
4580 ": arp_validate requires arp_interval\n");
4581 return -EINVAL;
4582 }
4583
4584 arp_validate_value = bond_parse_parm(arp_validate,
4585 arp_validate_tbl);
4586 if (arp_validate_value == -1) {
4587 printk(KERN_ERR DRV_NAME
4588 ": Error: invalid arp_validate \"%s\"\n",
4589 arp_validate == NULL ? "NULL" : arp_validate);
4590 return -EINVAL;
4591 }
4592 } else
4593 arp_validate_value = 0;
4594
1da177e4
LT
4595 if (miimon) {
4596 printk(KERN_INFO DRV_NAME
4597 ": MII link monitoring set to %d ms\n",
4598 miimon);
4599 } else if (arp_interval) {
4600 int i;
4601
4602 printk(KERN_INFO DRV_NAME
f5b2b966
JV
4603 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4604 arp_interval,
4605 arp_validate_tbl[arp_validate_value].modename,
4606 arp_ip_count);
1da177e4
LT
4607
4608 for (i = 0; i < arp_ip_count; i++)
4609 printk (" %s", arp_ip_target[i]);
4610
4611 printk("\n");
4612
4613 } else {
4614 /* miimon and arp_interval not set, we need one so things
4615 * work as expected, see bonding.txt for details
4616 */
4617 printk(KERN_WARNING DRV_NAME
4618 ": Warning: either miimon or arp_interval and "
4619 "arp_ip_target module parameters must be specified, "
4620 "otherwise bonding will not detect link failures! see "
4621 "bonding.txt for details.\n");
4622 }
4623
4624 if (primary && !USES_PRIMARY(bond_mode)) {
4625 /* currently, using a primary only makes sense
4626 * in active backup, TLB or ALB modes
4627 */
4628 printk(KERN_WARNING DRV_NAME
4629 ": Warning: %s primary device specified but has no "
4630 "effect in %s mode\n",
4631 primary, bond_mode_name(bond_mode));
4632 primary = NULL;
4633 }
4634
4635 /* fill params struct with the proper values */
4636 params->mode = bond_mode;
169a3e66 4637 params->xmit_policy = xmit_hashtype;
1da177e4
LT
4638 params->miimon = miimon;
4639 params->arp_interval = arp_interval;
f5b2b966 4640 params->arp_validate = arp_validate_value;
1da177e4
LT
4641 params->updelay = updelay;
4642 params->downdelay = downdelay;
4643 params->use_carrier = use_carrier;
4644 params->lacp_fast = lacp_fast;
4645 params->primary[0] = 0;
4646
4647 if (primary) {
4648 strncpy(params->primary, primary, IFNAMSIZ);
4649 params->primary[IFNAMSIZ - 1] = 0;
4650 }
4651
4652 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4653
4654 return 0;
4655}
4656
0daa2303
PZ
4657static struct lock_class_key bonding_netdev_xmit_lock_key;
4658
dfe60397 4659/* Create a new bond based on the specified name and bonding parameters.
e4b91c48 4660 * If name is NULL, obtain a suitable "bond%d" name for us.
dfe60397
MW
4661 * Caller must NOT hold rtnl_lock; we need to release it here before we
4662 * set up our sysfs entries.
4663 */
4664int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4665{
4666 struct net_device *bond_dev;
4667 int res;
4668
4669 rtnl_lock();
e4b91c48
JV
4670 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4671 ether_setup);
dfe60397
MW
4672 if (!bond_dev) {
4673 printk(KERN_ERR DRV_NAME
4674 ": %s: eek! can't alloc netdev!\n",
4675 name);
4676 res = -ENOMEM;
4677 goto out_rtnl;
4678 }
4679
e4b91c48
JV
4680 if (!name) {
4681 res = dev_alloc_name(bond_dev, "bond%d");
4682 if (res < 0)
4683 goto out_netdev;
4684 }
4685
dfe60397
MW
4686 /* bond_init() must be called after dev_alloc_name() (for the
4687 * /proc files), but before register_netdevice(), because we
4688 * need to set function pointers.
4689 */
4690
4691 res = bond_init(bond_dev, params);
4692 if (res < 0) {
4693 goto out_netdev;
4694 }
4695
dfe60397
MW
4696 res = register_netdevice(bond_dev);
4697 if (res < 0) {
4698 goto out_bond;
4699 }
0daa2303
PZ
4700
4701 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4702
dfe60397
MW
4703 if (newbond)
4704 *newbond = bond_dev->priv;
4705
ff59c456
JV
4706 netif_carrier_off(bond_dev);
4707
dfe60397 4708 rtnl_unlock(); /* allows sysfs registration of net device */
b76cdba9 4709 res = bond_create_sysfs_entry(bond_dev->priv);
09c89279
JV
4710 if (res < 0) {
4711 rtnl_lock();
4712 goto out_bond;
4713 }
4714
4715 return 0;
4716
dfe60397
MW
4717out_bond:
4718 bond_deinit(bond_dev);
4719out_netdev:
4720 free_netdev(bond_dev);
4721out_rtnl:
4722 rtnl_unlock();
dfe60397
MW
4723 return res;
4724}
4725
1da177e4
LT
4726static int __init bonding_init(void)
4727{
1da177e4
LT
4728 int i;
4729 int res;
4730
4731 printk(KERN_INFO "%s", version);
4732
dfe60397 4733 res = bond_check_params(&bonding_defaults);
1da177e4 4734 if (res) {
dfe60397 4735 goto out;
1da177e4
LT
4736 }
4737
1da177e4
LT
4738#ifdef CONFIG_PROC_FS
4739 bond_create_proc_dir();
4740#endif
1da177e4 4741 for (i = 0; i < max_bonds; i++) {
e4b91c48 4742 res = bond_create(NULL, &bonding_defaults, NULL);
dfe60397
MW
4743 if (res)
4744 goto err;
1da177e4
LT
4745 }
4746
b76cdba9
MW
4747 res = bond_create_sysfs();
4748 if (res)
4749 goto err;
4750
1da177e4 4751 register_netdevice_notifier(&bond_netdev_notifier);
c3ade5ca 4752 register_inetaddr_notifier(&bond_inetaddr_notifier);
1da177e4 4753
dfe60397
MW
4754 goto out;
4755err:
40abc270 4756 rtnl_lock();
1da177e4 4757 bond_free_all();
b76cdba9 4758 bond_destroy_sysfs();
1da177e4 4759 rtnl_unlock();
dfe60397 4760out:
1da177e4 4761 return res;
dfe60397 4762
1da177e4
LT
4763}
4764
4765static void __exit bonding_exit(void)
4766{
4767 unregister_netdevice_notifier(&bond_netdev_notifier);
c3ade5ca 4768 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
1da177e4
LT
4769
4770 rtnl_lock();
4771 bond_free_all();
b76cdba9 4772 bond_destroy_sysfs();
1da177e4
LT
4773 rtnl_unlock();
4774}
4775
4776module_init(bonding_init);
4777module_exit(bonding_exit);
4778MODULE_LICENSE("GPL");
4779MODULE_VERSION(DRV_VERSION);
4780MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4781MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4782MODULE_SUPPORTED_DEVICE("most ethernet devices");
4783
4784/*
4785 * Local variables:
4786 * c-indent-level: 8
4787 * c-basic-offset: 8
4788 * tab-width: 8
4789 * End:
4790 */
4791