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