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