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