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