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