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