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