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