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