blob: fa3c2101fe750d48379f0083e37a35a2737f0389 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
34//#define BONDING_DEBUG 1
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/kernel.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040044#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ip.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040046#include <linux/tcp.h>
47#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080063#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#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>
David Sterbab63bb732007-12-06 23:40:33 -080077#include <linux/jiffies.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040078#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020079#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#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;
Moni Shoua7893b242008-05-17 21:10:12 -070091static int num_grat_arp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int miimon = BOND_LINK_MON_INTERV;
93static int updelay = 0;
94static int downdelay = 0;
95static int use_carrier = 1;
96static char *mode = NULL;
97static char *primary = NULL;
98static char *lacp_rate = NULL;
Jay Vosburgh169a3e62005-06-26 17:54:11 -040099static char *xmit_hash_policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int arp_interval = BOND_LINK_ARP_INTERV;
101static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700102static char *arp_validate = NULL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700103static int fail_over_mac = 0;
Mitch Williams12479f92005-11-09 10:35:44 -0800104struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106module_param(max_bonds, int, 0);
107MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Moni Shoua7893b242008-05-17 21:10:12 -0700108module_param(num_grat_arp, int, 0644);
109MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110module_param(miimon, int, 0);
111MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
112module_param(updelay, int, 0);
113MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
114module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800115MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
116 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800118MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
119 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800121MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
122 "1 for active-backup, 2 for balance-xor, "
123 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
124 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125module_param(primary, charp, 0);
126MODULE_PARM_DESC(primary, "Primary network device to use");
127module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800128MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
129 "(slow/fast)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400130module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800131MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
132 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133module_param(arp_interval, int, 0);
134MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
135module_param_array(arp_ip_target, charp, NULL, 0);
136MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700137module_param(arp_validate, charp, 0);
138MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburghdd957c52007-10-09 19:57:24 -0700139module_param(fail_over_mac, int, 0);
140MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/*----------------------------- Global variables ----------------------------*/
143
Arjan van de Venf71e1302006-03-03 21:33:57 -0500144static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
146
Mitch Williams12479f92005-11-09 10:35:44 -0800147LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149#ifdef CONFIG_PROC_FS
150static struct proc_dir_entry *bond_proc_dir = NULL;
151#endif
152
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800153extern struct rw_semaphore bonding_rwsem;
Al Virod3bb52b2007-08-22 20:06:58 -0400154static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int arp_ip_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static int bond_mode = BOND_MODE_ROUNDROBIN;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400157static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int lacp_fast = 0;
Jay Vosburgh217df672005-09-26 16:11:50 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Mitch Williams12479f92005-11-09 10:35:44 -0800161struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{ "slow", AD_LACP_SLOW},
163{ "fast", AD_LACP_FAST},
164{ NULL, -1},
165};
166
Mitch Williams12479f92005-11-09 10:35:44 -0800167struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{ "balance-rr", BOND_MODE_ROUNDROBIN},
169{ "active-backup", BOND_MODE_ACTIVEBACKUP},
170{ "balance-xor", BOND_MODE_XOR},
171{ "broadcast", BOND_MODE_BROADCAST},
172{ "802.3ad", BOND_MODE_8023AD},
173{ "balance-tlb", BOND_MODE_TLB},
174{ "balance-alb", BOND_MODE_ALB},
175{ NULL, -1},
176};
177
Mitch Williams12479f92005-11-09 10:35:44 -0800178struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400179{ "layer2", BOND_XMIT_POLICY_LAYER2},
180{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800181{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400182{ NULL, -1},
183};
184
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700185struct bond_parm_tbl arp_validate_tbl[] = {
186{ "none", BOND_ARP_VALIDATE_NONE},
187{ "active", BOND_ARP_VALIDATE_ACTIVE},
188{ "backup", BOND_ARP_VALIDATE_BACKUP},
189{ "all", BOND_ARP_VALIDATE_ALL},
190{ NULL, -1},
191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*-------------------------- Forward declarations ---------------------------*/
194
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400195static void bond_send_gratuitous_arp(struct bonding *bond);
Adrian Bunkc50b85d2007-10-24 18:23:17 +0200196static void bond_deinit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/*---------------------------- General routines -----------------------------*/
199
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700200static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 switch (mode) {
203 case BOND_MODE_ROUNDROBIN :
204 return "load balancing (round-robin)";
205 case BOND_MODE_ACTIVEBACKUP :
206 return "fault-tolerance (active-backup)";
207 case BOND_MODE_XOR :
208 return "load balancing (xor)";
209 case BOND_MODE_BROADCAST :
210 return "fault-tolerance (broadcast)";
211 case BOND_MODE_8023AD:
212 return "IEEE 802.3ad Dynamic link aggregation";
213 case BOND_MODE_TLB:
214 return "transmit load balancing";
215 case BOND_MODE_ALB:
216 return "adaptive load balancing";
217 default:
218 return "unknown";
219 }
220}
221
222/*---------------------------------- VLAN -----------------------------------*/
223
224/**
225 * bond_add_vlan - add a new vlan id on bond
226 * @bond: bond that got the notification
227 * @vlan_id: the vlan id to add
228 *
229 * Returns -ENOMEM if allocation failed.
230 */
231static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
232{
233 struct vlan_entry *vlan;
234
235 dprintk("bond: %s, vlan id %d\n",
236 (bond ? bond->dev->name: "None"), vlan_id);
237
238 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
239 if (!vlan) {
240 return -ENOMEM;
241 }
242
243 INIT_LIST_HEAD(&vlan->vlan_list);
244 vlan->vlan_id = vlan_id;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400245 vlan->vlan_ip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 write_lock_bh(&bond->lock);
248
249 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
250
251 write_unlock_bh(&bond->lock);
252
253 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
254
255 return 0;
256}
257
258/**
259 * bond_del_vlan - delete a vlan id from bond
260 * @bond: bond that got the notification
261 * @vlan_id: the vlan id to delete
262 *
263 * returns -ENODEV if @vlan_id was not found in @bond.
264 */
265static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
266{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700267 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int res = -ENODEV;
269
270 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
271
272 write_lock_bh(&bond->lock);
273
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700274 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (vlan->vlan_id == vlan_id) {
276 list_del(&vlan->vlan_list);
277
278 if ((bond->params.mode == BOND_MODE_TLB) ||
279 (bond->params.mode == BOND_MODE_ALB)) {
280 bond_alb_clear_vlan(bond, vlan_id);
281 }
282
283 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
284 bond->dev->name);
285
286 kfree(vlan);
287
288 if (list_empty(&bond->vlan_list) &&
289 (bond->slave_cnt == 0)) {
290 /* Last VLAN removed and no slaves, so
291 * restore block on adding VLANs. This will
292 * be removed once new slaves that are not
293 * VLAN challenged will be added.
294 */
295 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
296 }
297
298 res = 0;
299 goto out;
300 }
301 }
302
303 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
304 bond->dev->name);
305
306out:
307 write_unlock_bh(&bond->lock);
308 return res;
309}
310
311/**
312 * bond_has_challenged_slaves
313 * @bond: the bond we're working on
314 *
315 * Searches the slave list. Returns 1 if a vlan challenged slave
316 * was found, 0 otherwise.
317 *
318 * Assumes bond->lock is held.
319 */
320static int bond_has_challenged_slaves(struct bonding *bond)
321{
322 struct slave *slave;
323 int i;
324
325 bond_for_each_slave(bond, slave, i) {
326 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
327 dprintk("found VLAN challenged slave - %s\n",
328 slave->dev->name);
329 return 1;
330 }
331 }
332
333 dprintk("no VLAN challenged slaves found\n");
334 return 0;
335}
336
337/**
338 * bond_next_vlan - safely skip to the next item in the vlans list.
339 * @bond: the bond we're working on
340 * @curr: item we're advancing from
341 *
342 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
343 * or @curr->next otherwise (even if it is @curr itself again).
344 *
345 * Caller must hold bond->lock
346 */
347struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
348{
349 struct vlan_entry *next, *last;
350
351 if (list_empty(&bond->vlan_list)) {
352 return NULL;
353 }
354
355 if (!curr) {
356 next = list_entry(bond->vlan_list.next,
357 struct vlan_entry, vlan_list);
358 } else {
359 last = list_entry(bond->vlan_list.prev,
360 struct vlan_entry, vlan_list);
361 if (last == curr) {
362 next = list_entry(bond->vlan_list.next,
363 struct vlan_entry, vlan_list);
364 } else {
365 next = list_entry(curr->vlan_list.next,
366 struct vlan_entry, vlan_list);
367 }
368 }
369
370 return next;
371}
372
373/**
374 * bond_dev_queue_xmit - Prepare skb for xmit.
375 *
376 * @bond: bond device that got this skb for tx.
377 * @skb: hw accel VLAN tagged skb to transmit
378 * @slave_dev: slave that is supposed to xmit this skbuff
379 *
380 * When the bond gets an skb to transmit that is
381 * already hardware accelerated VLAN tagged, and it
382 * needs to relay this skb to a slave that is not
383 * hw accel capable, the skb needs to be "unaccelerated",
384 * i.e. strip the hwaccel tag and re-insert it as part
385 * of the payload.
386 */
387int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
388{
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700389 unsigned short uninitialized_var(vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!list_empty(&bond->vlan_list) &&
392 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
393 vlan_get_tag(skb, &vlan_id) == 0) {
394 skb->dev = slave_dev;
395 skb = vlan_put_tag(skb, vlan_id);
396 if (!skb) {
397 /* vlan_put_tag() frees the skb in case of error,
398 * so return success here so the calling functions
399 * won't attempt to free is again.
400 */
401 return 0;
402 }
403 } else {
404 skb->dev = slave_dev;
405 }
406
407 skb->priority = 1;
408 dev_queue_xmit(skb);
409
410 return 0;
411}
412
413/*
414 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
415 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
416 * lock because:
417 * a. This operation is performed in IOCTL context,
418 * b. The operation is protected by the RTNL semaphore in the 8021q code,
419 * c. Holding a lock with BH disabled while directly calling a base driver
420 * entry point is generally a BAD idea.
421 *
422 * The design of synchronization/protection for this operation in the 8021q
423 * module is good for one or more VLAN devices over a single physical device
424 * and cannot be extended for a teaming solution like bonding, so there is a
425 * potential race condition here where a net device from the vlan group might
426 * be referenced (either by a base driver or the 8021q code) while it is being
427 * removed from the system. However, it turns out we're not making matters
428 * worse, and if it works for regular VLAN usage it will work here too.
429*/
430
431/**
432 * bond_vlan_rx_register - Propagates registration to slaves
433 * @bond_dev: bonding net device that got called
434 * @grp: vlan group being registered
435 */
436static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
437{
438 struct bonding *bond = bond_dev->priv;
439 struct slave *slave;
440 int i;
441
442 bond->vlgrp = grp;
443
444 bond_for_each_slave(bond, slave, i) {
445 struct net_device *slave_dev = slave->dev;
446
447 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
448 slave_dev->vlan_rx_register) {
449 slave_dev->vlan_rx_register(slave_dev, grp);
450 }
451 }
452}
453
454/**
455 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
456 * @bond_dev: bonding net device that got called
457 * @vid: vlan id being added
458 */
459static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
460{
461 struct bonding *bond = bond_dev->priv;
462 struct slave *slave;
463 int i, res;
464
465 bond_for_each_slave(bond, slave, i) {
466 struct net_device *slave_dev = slave->dev;
467
468 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
469 slave_dev->vlan_rx_add_vid) {
470 slave_dev->vlan_rx_add_vid(slave_dev, vid);
471 }
472 }
473
474 res = bond_add_vlan(bond, vid);
475 if (res) {
476 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800477 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 bond_dev->name, vid);
479 }
480}
481
482/**
483 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
484 * @bond_dev: bonding net device that got called
485 * @vid: vlan id being removed
486 */
487static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
488{
489 struct bonding *bond = bond_dev->priv;
490 struct slave *slave;
491 struct net_device *vlan_dev;
492 int i, res;
493
494 bond_for_each_slave(bond, slave, i) {
495 struct net_device *slave_dev = slave->dev;
496
497 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
498 slave_dev->vlan_rx_kill_vid) {
499 /* Save and then restore vlan_dev in the grp array,
500 * since the slave's driver might clear it.
501 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800502 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800504 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 }
507
508 res = bond_del_vlan(bond, vid);
509 if (res) {
510 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800511 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 bond_dev->name, vid);
513 }
514}
515
516static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
517{
518 struct vlan_entry *vlan;
519
520 write_lock_bh(&bond->lock);
521
522 if (list_empty(&bond->vlan_list)) {
523 goto out;
524 }
525
526 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
527 slave_dev->vlan_rx_register) {
528 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
529 }
530
531 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
532 !(slave_dev->vlan_rx_add_vid)) {
533 goto out;
534 }
535
536 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
537 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
538 }
539
540out:
541 write_unlock_bh(&bond->lock);
542}
543
544static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
545{
546 struct vlan_entry *vlan;
547 struct net_device *vlan_dev;
548
549 write_lock_bh(&bond->lock);
550
551 if (list_empty(&bond->vlan_list)) {
552 goto out;
553 }
554
555 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
556 !(slave_dev->vlan_rx_kill_vid)) {
557 goto unreg;
558 }
559
560 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
561 /* Save and then restore vlan_dev in the grp array,
562 * since the slave's driver might clear it.
563 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800564 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800566 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569unreg:
570 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
571 slave_dev->vlan_rx_register) {
572 slave_dev->vlan_rx_register(slave_dev, NULL);
573 }
574
575out:
576 write_unlock_bh(&bond->lock);
577}
578
579/*------------------------------- Link status -------------------------------*/
580
581/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800582 * Set the carrier state for the master according to the state of its
583 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
584 * do special 802.3ad magic.
585 *
586 * Returns zero if carrier state does not change, nonzero if it does.
587 */
588static int bond_set_carrier(struct bonding *bond)
589{
590 struct slave *slave;
591 int i;
592
593 if (bond->slave_cnt == 0)
594 goto down;
595
596 if (bond->params.mode == BOND_MODE_8023AD)
597 return bond_3ad_set_carrier(bond);
598
599 bond_for_each_slave(bond, slave, i) {
600 if (slave->link == BOND_LINK_UP) {
601 if (!netif_carrier_ok(bond->dev)) {
602 netif_carrier_on(bond->dev);
603 return 1;
604 }
605 return 0;
606 }
607 }
608
609down:
610 if (netif_carrier_ok(bond->dev)) {
611 netif_carrier_off(bond->dev);
612 return 1;
613 }
614 return 0;
615}
616
617/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * Get link speed and duplex from the slave's base driver
619 * using ethtool. If for some reason the call fails or the
620 * values are invalid, fake speed and duplex to 100/Full
621 * and return error.
622 */
623static int bond_update_speed_duplex(struct slave *slave)
624{
625 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700627 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* Fake speed and duplex */
630 slave->speed = SPEED_100;
631 slave->duplex = DUPLEX_FULL;
632
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700633 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700636 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
637 if (res < 0)
638 return -1;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 switch (etool.speed) {
641 case SPEED_10:
642 case SPEED_100:
643 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700644 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 default:
647 return -1;
648 }
649
650 switch (etool.duplex) {
651 case DUPLEX_FULL:
652 case DUPLEX_HALF:
653 break;
654 default:
655 return -1;
656 }
657
658 slave->speed = etool.speed;
659 slave->duplex = etool.duplex;
660
661 return 0;
662}
663
664/*
665 * if <dev> supports MII link status reporting, check its link status.
666 *
667 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
668 * depening upon the setting of the use_carrier parameter.
669 *
670 * Return either BMSR_LSTATUS, meaning that the link is up (or we
671 * can't tell and just pretend it is), or 0, meaning that the link is
672 * down.
673 *
674 * If reporting is non-zero, instead of faking link up, return -1 if
675 * both ETHTOOL and MII ioctls fail (meaning the device does not
676 * support them). If use_carrier is set, return whatever it says.
677 * It'd be nice if there was a good way to tell if a driver supports
678 * netif_carrier, but there really isn't.
679 */
680static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
681{
682 static int (* ioctl)(struct net_device *, struct ifreq *, int);
683 struct ifreq ifr;
684 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (bond->params.use_carrier) {
687 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
688 }
689
690 ioctl = slave_dev->do_ioctl;
691 if (ioctl) {
692 /* TODO: set pointer to correct ioctl on a per team member */
693 /* bases to make this more efficient. that is, once */
694 /* we determine the correct ioctl, we will always */
695 /* call it and not the others for that team */
696 /* member. */
697
698 /*
699 * We cannot assume that SIOCGMIIPHY will also read a
700 * register; not all network drivers (e.g., e100)
701 * support that.
702 */
703
704 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
705 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
706 mii = if_mii(&ifr);
707 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
708 mii->reg_num = MII_BMSR;
709 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
710 return (mii->val_out & BMSR_LSTATUS);
711 }
712 }
713 }
714
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700715 /*
716 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
717 * attempt to get link status from it if the above MII ioctls fail.
718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (slave_dev->ethtool_ops) {
720 if (slave_dev->ethtool_ops->get_link) {
721 u32 link;
722
723 link = slave_dev->ethtool_ops->get_link(slave_dev);
724
725 return link ? BMSR_LSTATUS : 0;
726 }
727 }
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700731 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * cannot report link status). If not reporting, pretend
733 * we're ok.
734 */
735 return (reporting ? -1 : BMSR_LSTATUS);
736}
737
738/*----------------------------- Multicast list ------------------------------*/
739
740/*
741 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
742 */
743static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
744{
745 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
746 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
747}
748
749/*
750 * returns dmi entry if found, NULL otherwise
751 */
752static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
753{
754 struct dev_mc_list *idmi;
755
756 for (idmi = mc_list; idmi; idmi = idmi->next) {
757 if (bond_is_dmi_same(dmi, idmi)) {
758 return idmi;
759 }
760 }
761
762 return NULL;
763}
764
765/*
766 * Push the promiscuity flag down to appropriate slaves
767 */
768static void bond_set_promiscuity(struct bonding *bond, int inc)
769{
770 if (USES_PRIMARY(bond->params.mode)) {
771 /* write lock already acquired */
772 if (bond->curr_active_slave) {
773 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
774 }
775 } else {
776 struct slave *slave;
777 int i;
778 bond_for_each_slave(bond, slave, i) {
779 dev_set_promiscuity(slave->dev, inc);
780 }
781 }
782}
783
784/*
785 * Push the allmulti flag down to all slaves
786 */
787static void bond_set_allmulti(struct bonding *bond, int inc)
788{
789 if (USES_PRIMARY(bond->params.mode)) {
790 /* write lock already acquired */
791 if (bond->curr_active_slave) {
792 dev_set_allmulti(bond->curr_active_slave->dev, inc);
793 }
794 } else {
795 struct slave *slave;
796 int i;
797 bond_for_each_slave(bond, slave, i) {
798 dev_set_allmulti(slave->dev, inc);
799 }
800 }
801}
802
803/*
804 * Add a Multicast address to slaves
805 * according to mode
806 */
807static void bond_mc_add(struct bonding *bond, void *addr, int alen)
808{
809 if (USES_PRIMARY(bond->params.mode)) {
810 /* write lock already acquired */
811 if (bond->curr_active_slave) {
812 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
813 }
814 } else {
815 struct slave *slave;
816 int i;
817 bond_for_each_slave(bond, slave, i) {
818 dev_mc_add(slave->dev, addr, alen, 0);
819 }
820 }
821}
822
823/*
824 * Remove a multicast address from slave
825 * according to mode
826 */
827static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
828{
829 if (USES_PRIMARY(bond->params.mode)) {
830 /* write lock already acquired */
831 if (bond->curr_active_slave) {
832 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
833 }
834 } else {
835 struct slave *slave;
836 int i;
837 bond_for_each_slave(bond, slave, i) {
838 dev_mc_delete(slave->dev, addr, alen, 0);
839 }
840 }
841}
842
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800843
844/*
845 * Retrieve the list of registered multicast addresses for the bonding
846 * device and retransmit an IGMP JOIN request to the current active
847 * slave.
848 */
849static void bond_resend_igmp_join_requests(struct bonding *bond)
850{
851 struct in_device *in_dev;
852 struct ip_mc_list *im;
853
854 rcu_read_lock();
855 in_dev = __in_dev_get_rcu(bond->dev);
856 if (in_dev) {
857 for (im = in_dev->mc_list; im; im = im->next) {
858 ip_mc_rejoin_group(im);
859 }
860 }
861
862 rcu_read_unlock();
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/*
866 * Totally destroys the mc_list in bond
867 */
868static void bond_mc_list_destroy(struct bonding *bond)
869{
870 struct dev_mc_list *dmi;
871
872 dmi = bond->mc_list;
873 while (dmi) {
874 bond->mc_list = dmi->next;
875 kfree(dmi);
876 dmi = bond->mc_list;
877 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800878 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/*
882 * Copy all the Multicast addresses from src to the bonding device dst
883 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700884static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100885 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct dev_mc_list *dmi, *new_dmi;
888
889 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700890 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (!new_dmi) {
893 /* FIXME: Potential memory leak !!! */
894 return -ENOMEM;
895 }
896
897 new_dmi->next = bond->mc_list;
898 bond->mc_list = new_dmi;
899 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
900 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
901 new_dmi->dmi_users = dmi->dmi_users;
902 new_dmi->dmi_gusers = dmi->dmi_gusers;
903 }
904
905 return 0;
906}
907
908/*
909 * flush all members of flush->mc_list from device dev->mc_list
910 */
911static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
912{
913 struct bonding *bond = bond_dev->priv;
914 struct dev_mc_list *dmi;
915
916 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
917 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
918 }
919
920 if (bond->params.mode == BOND_MODE_8023AD) {
921 /* del lacpdu mc addr from mc list */
922 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
923
924 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
925 }
926}
927
928/*--------------------------- Active slave change ---------------------------*/
929
930/*
931 * Update the mc list and multicast-related flags for the new and
932 * old active slaves (if any) according to the multicast mode, and
933 * promiscuous flags unconditionally.
934 */
935static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
936{
937 struct dev_mc_list *dmi;
938
939 if (!USES_PRIMARY(bond->params.mode)) {
940 /* nothing to do - mc list is already up-to-date on
941 * all slaves
942 */
943 return;
944 }
945
946 if (old_active) {
947 if (bond->dev->flags & IFF_PROMISC) {
948 dev_set_promiscuity(old_active->dev, -1);
949 }
950
951 if (bond->dev->flags & IFF_ALLMULTI) {
952 dev_set_allmulti(old_active->dev, -1);
953 }
954
955 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
956 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
957 }
958 }
959
960 if (new_active) {
961 if (bond->dev->flags & IFF_PROMISC) {
962 dev_set_promiscuity(new_active->dev, 1);
963 }
964
965 if (bond->dev->flags & IFF_ALLMULTI) {
966 dev_set_allmulti(new_active->dev, 1);
967 }
968
969 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
970 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
971 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800972 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974}
975
976/**
977 * find_best_interface - select the best available slave to be the active one
978 * @bond: our bonding struct
979 *
980 * Warning: Caller must hold curr_slave_lock for writing.
981 */
982static struct slave *bond_find_best_slave(struct bonding *bond)
983{
984 struct slave *new_active, *old_active;
985 struct slave *bestslave = NULL;
986 int mintime = bond->params.updelay;
987 int i;
988
989 new_active = old_active = bond->curr_active_slave;
990
991 if (!new_active) { /* there were no active slaves left */
992 if (bond->slave_cnt > 0) { /* found one slave */
993 new_active = bond->first_slave;
994 } else {
995 return NULL; /* still no slave, return NULL */
996 }
997 }
998
999 /* first try the primary link; if arping, a link must tx/rx traffic
1000 * before it can be considered the curr_active_slave - also, we would skip
1001 * slaves between the curr_active_slave and primary_slave that may be up
1002 * and able to arp
1003 */
1004 if ((bond->primary_slave) &&
1005 (!bond->params.arp_interval) &&
1006 (IS_UP(bond->primary_slave->dev))) {
1007 new_active = bond->primary_slave;
1008 }
1009
1010 /* remember where to stop iterating over the slaves */
1011 old_active = new_active;
1012
1013 bond_for_each_slave_from(bond, new_active, i, old_active) {
1014 if (IS_UP(new_active->dev)) {
1015 if (new_active->link == BOND_LINK_UP) {
1016 return new_active;
1017 } else if (new_active->link == BOND_LINK_BACK) {
1018 /* link up, but waiting for stabilization */
1019 if (new_active->delay < mintime) {
1020 mintime = new_active->delay;
1021 bestslave = new_active;
1022 }
1023 }
1024 }
1025 }
1026
1027 return bestslave;
1028}
1029
1030/**
1031 * change_active_interface - change the active slave into the specified one
1032 * @bond: our bonding struct
1033 * @new: the new slave to make the active one
1034 *
1035 * Set the new slave to the bond's settings and unset them on the old
1036 * curr_active_slave.
1037 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1038 *
1039 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1040 * because it is apparently the best available slave we have, even though its
1041 * updelay hasn't timed out yet.
1042 *
1043 * Warning: Caller must hold curr_slave_lock for writing.
1044 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001045void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct slave *old_active = bond->curr_active_slave;
1048
1049 if (old_active == new_active) {
1050 return;
1051 }
1052
1053 if (new_active) {
1054 if (new_active->link == BOND_LINK_BACK) {
1055 if (USES_PRIMARY(bond->params.mode)) {
1056 printk(KERN_INFO DRV_NAME
1057 ": %s: making interface %s the new "
1058 "active one %d ms earlier.\n",
1059 bond->dev->name, new_active->dev->name,
1060 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1061 }
1062
1063 new_active->delay = 0;
1064 new_active->link = BOND_LINK_UP;
1065 new_active->jiffies = jiffies;
1066
1067 if (bond->params.mode == BOND_MODE_8023AD) {
1068 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1069 }
1070
1071 if ((bond->params.mode == BOND_MODE_TLB) ||
1072 (bond->params.mode == BOND_MODE_ALB)) {
1073 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1074 }
1075 } else {
1076 if (USES_PRIMARY(bond->params.mode)) {
1077 printk(KERN_INFO DRV_NAME
1078 ": %s: making interface %s the new "
1079 "active one.\n",
1080 bond->dev->name, new_active->dev->name);
1081 }
1082 }
1083 }
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (USES_PRIMARY(bond->params.mode)) {
1086 bond_mc_swap(bond, new_active, old_active);
1087 }
1088
1089 if ((bond->params.mode == BOND_MODE_TLB) ||
1090 (bond->params.mode == BOND_MODE_ALB)) {
1091 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001092 if (old_active)
1093 bond_set_slave_inactive_flags(old_active);
1094 if (new_active)
1095 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else {
1097 bond->curr_active_slave = new_active;
1098 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001099
1100 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1101 if (old_active) {
1102 bond_set_slave_inactive_flags(old_active);
1103 }
1104
1105 if (new_active) {
1106 bond_set_slave_active_flags(new_active);
1107 }
Moni Shoua2ab82852007-10-09 19:43:39 -07001108
1109 /* when bonding does not set the slave MAC address, the bond MAC
1110 * address is the one of the active slave.
1111 */
Jay Vosburghdd957c52007-10-09 19:57:24 -07001112 if (new_active && bond->params.fail_over_mac)
Moni Shoua2ab82852007-10-09 19:43:39 -07001113 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1114 new_active->dev->addr_len);
Moni Shoua7893b242008-05-17 21:10:12 -07001115 bond->send_grat_arp = bond->params.num_grat_arp;
Moni Shoua1053f622007-10-09 19:43:42 -07001116 if (bond->curr_active_slave &&
1117 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1118 &bond->curr_active_slave->dev->state)) {
1119 dprintk("delaying gratuitous arp on %s\n",
1120 bond->curr_active_slave->dev->name);
Moni Shoua7893b242008-05-17 21:10:12 -07001121 } else {
1122 if (bond->send_grat_arp > 0) {
1123 bond_send_gratuitous_arp(bond);
1124 bond->send_grat_arp--;
1125 }
1126 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/**
1131 * bond_select_active_slave - select a new active slave, if needed
1132 * @bond: our bonding struct
1133 *
1134 * This functions shoud be called when one of the following occurs:
1135 * - The old curr_active_slave has been released or lost its link.
1136 * - The primary_slave has got its link back.
1137 * - A slave has got its link back and there's no old curr_active_slave.
1138 *
1139 * Warning: Caller must hold curr_slave_lock for writing.
1140 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001141void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001144 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 best_slave = bond_find_best_slave(bond);
1147 if (best_slave != bond->curr_active_slave) {
1148 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001149 rv = bond_set_carrier(bond);
1150 if (!rv)
1151 return;
1152
1153 if (netif_carrier_ok(bond->dev)) {
1154 printk(KERN_INFO DRV_NAME
1155 ": %s: first active interface up!\n",
1156 bond->dev->name);
1157 } else {
1158 printk(KERN_INFO DRV_NAME ": %s: "
1159 "now running without any active interface !\n",
1160 bond->dev->name);
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163}
1164
1165/*--------------------------- slave list handling ---------------------------*/
1166
1167/*
1168 * This function attaches the slave to the end of list.
1169 *
1170 * bond->lock held for writing by caller.
1171 */
1172static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1173{
1174 if (bond->first_slave == NULL) { /* attaching the first slave */
1175 new_slave->next = new_slave;
1176 new_slave->prev = new_slave;
1177 bond->first_slave = new_slave;
1178 } else {
1179 new_slave->next = bond->first_slave;
1180 new_slave->prev = bond->first_slave->prev;
1181 new_slave->next->prev = new_slave;
1182 new_slave->prev->next = new_slave;
1183 }
1184
1185 bond->slave_cnt++;
1186}
1187
1188/*
1189 * This function detaches the slave from the list.
1190 * WARNING: no check is made to verify if the slave effectively
1191 * belongs to <bond>.
1192 * Nothing is freed on return, structures are just unchained.
1193 * If any slave pointer in bond was pointing to <slave>,
1194 * it should be changed by the calling function.
1195 *
1196 * bond->lock held for writing by caller.
1197 */
1198static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1199{
1200 if (slave->next) {
1201 slave->next->prev = slave->prev;
1202 }
1203
1204 if (slave->prev) {
1205 slave->prev->next = slave->next;
1206 }
1207
1208 if (bond->first_slave == slave) { /* slave is the first slave */
1209 if (bond->slave_cnt > 1) { /* there are more slave */
1210 bond->first_slave = slave->next;
1211 } else {
1212 bond->first_slave = NULL; /* slave was the last one */
1213 }
1214 }
1215
1216 slave->next = NULL;
1217 slave->prev = NULL;
1218 bond->slave_cnt--;
1219}
1220
1221/*---------------------------------- IOCTL ----------------------------------*/
1222
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001223static int bond_sethwaddr(struct net_device *bond_dev,
1224 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 dprintk("bond_dev=%p\n", bond_dev);
1227 dprintk("slave_dev=%p\n", slave_dev);
1228 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1229 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1230 return 0;
1231}
1232
Herbert Xu7f353bf2007-08-10 15:47:58 -07001233#define BOND_VLAN_FEATURES \
1234 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1235 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001236
1237/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001238 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001239 * feature bits are managed elsewhere, so preserve those feature bits
1240 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001241 */
1242static int bond_compute_features(struct bonding *bond)
1243{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001244 struct slave *slave;
1245 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001246 unsigned long features = bond_dev->features;
Moni Shoua3158bf72007-10-09 19:43:41 -07001247 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1248 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001249 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001250
Herbert Xu7f353bf2007-08-10 15:47:58 -07001251 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1252 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1253 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1254
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001255 bond_for_each_slave(bond, slave, i) {
Herbert Xu7f353bf2007-08-10 15:47:58 -07001256 features = netdev_compute_features(features,
1257 slave->dev->features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001258 if (slave->dev->hard_header_len > max_hard_header_len)
1259 max_hard_header_len = slave->dev->hard_header_len;
1260 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001261
Herbert Xu7f353bf2007-08-10 15:47:58 -07001262 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001263 bond_dev->features = features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001264 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001265
1266 return 0;
1267}
1268
Moni Shoua872254d2007-10-09 19:43:38 -07001269
1270static void bond_setup_by_slave(struct net_device *bond_dev,
1271 struct net_device *slave_dev)
1272{
Moni Shouad90a1622007-10-09 19:43:43 -07001273 struct bonding *bond = bond_dev->priv;
1274
Moni Shoua872254d2007-10-09 19:43:38 -07001275 bond_dev->neigh_setup = slave_dev->neigh_setup;
Jay Vosburgh1284cd32007-10-15 16:44:27 -07001276 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001277
1278 bond_dev->type = slave_dev->type;
1279 bond_dev->hard_header_len = slave_dev->hard_header_len;
1280 bond_dev->addr_len = slave_dev->addr_len;
1281
1282 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1283 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001284 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001285}
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001288int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct bonding *bond = bond_dev->priv;
1291 struct slave *new_slave = NULL;
1292 struct dev_mc_list *dmi;
1293 struct sockaddr addr;
1294 int link_reporting;
1295 int old_features = bond_dev->features;
1296 int res = 0;
1297
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001298 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1299 slave_dev->do_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001301 ": %s: Warning: no link monitoring support for %s\n",
1302 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
1305 /* bond must be initialized by bond_open() before enslaving */
1306 if (!(bond_dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -07001307 printk(KERN_WARNING DRV_NAME
1308 " %s: master_dev is not up in bond_enslave\n",
1309 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
1312 /* already enslaved */
1313 if (slave_dev->flags & IFF_SLAVE) {
1314 dprintk("Error, Device was already enslaved\n");
1315 return -EBUSY;
1316 }
1317
1318 /* vlan challenged mutual exclusion */
1319 /* no need to lock since we're protected by rtnl_lock */
1320 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1321 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1322 if (!list_empty(&bond->vlan_list)) {
1323 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001324 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001326 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 bond_dev->name);
1328 return -EPERM;
1329 } else {
1330 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001331 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 "slave %s. Adding VLANs will be blocked as "
1333 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001334 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 bond_dev->name);
1336 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1337 }
1338 } else {
1339 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1340 if (bond->slave_cnt == 0) {
1341 /* First slave, and it is not VLAN challenged,
1342 * so remove the block of adding VLANs over the bond.
1343 */
1344 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1345 }
1346 }
1347
Jay Vosburgh217df672005-09-26 16:11:50 -07001348 /*
1349 * Old ifenslave binaries are no longer supported. These can
1350 * be identified with moderate accurary by the state of the slave:
1351 * the current ifenslave will set the interface down prior to
1352 * enslaving it; the old ifenslave will not.
1353 */
1354 if ((slave_dev->flags & IFF_UP)) {
1355 printk(KERN_ERR DRV_NAME ": %s is up. "
1356 "This may be due to an out of date ifenslave.\n",
1357 slave_dev->name);
1358 res = -EPERM;
1359 goto err_undo_flags;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Moni Shoua872254d2007-10-09 19:43:38 -07001362 /* set bonding device ether type by slave - bonding netdevices are
1363 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1364 * there is a need to override some of the type dependent attribs/funcs.
1365 *
1366 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1367 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1368 */
1369 if (bond->slave_cnt == 0) {
1370 if (slave_dev->type != ARPHRD_ETHER)
1371 bond_setup_by_slave(bond_dev, slave_dev);
1372 } else if (bond_dev->type != slave_dev->type) {
1373 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1374 "from other slaves (%d), can not enslave it.\n",
1375 slave_dev->name,
1376 slave_dev->type, bond_dev->type);
1377 res = -EINVAL;
1378 goto err_undo_flags;
1379 }
1380
Jay Vosburgh217df672005-09-26 16:11:50 -07001381 if (slave_dev->set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001382 if (bond->slave_cnt == 0) {
1383 printk(KERN_WARNING DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001384 ": %s: Warning: The first slave device "
1385 "specified does not support setting the MAC "
1386 "address. Enabling the fail_over_mac option.",
1387 bond_dev->name);
1388 bond->params.fail_over_mac = 1;
1389 } else if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001390 printk(KERN_ERR DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001391 ": %s: Error: The slave device specified "
1392 "does not support setting the MAC address, "
1393 "but fail_over_mac is not enabled.\n"
Moni Shoua2ab82852007-10-09 19:43:39 -07001394 , bond_dev->name);
1395 res = -EOPNOTSUPP;
1396 goto err_undo_flags;
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Joe Jin243cb4e2007-02-06 14:16:40 -08001400 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!new_slave) {
1402 res = -ENOMEM;
1403 goto err_undo_flags;
1404 }
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* save slave's original flags before calling
1407 * netdev_set_master and dev_open
1408 */
1409 new_slave->original_flags = slave_dev->flags;
1410
Jay Vosburgh217df672005-09-26 16:11:50 -07001411 /*
1412 * Save slave's original ("permanent") mac address for modes
1413 * that need it, and for restoring it upon release, and then
1414 * set it to the master's address
1415 */
1416 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Jay Vosburghdd957c52007-10-09 19:57:24 -07001418 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001419 /*
1420 * Set slave to master's mac address. The application already
1421 * set the master's mac address to that of the first slave
1422 */
1423 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1424 addr.sa_family = slave_dev->type;
1425 res = dev_set_mac_address(slave_dev, &addr);
1426 if (res) {
1427 dprintk("Error %d calling set_mac_address\n", res);
1428 goto err_free;
1429 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001432 res = netdev_set_master(slave_dev, bond_dev);
1433 if (res) {
1434 dprintk("Error %d calling netdev_set_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001435 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001436 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001437 /* open the slave since the application closed it */
1438 res = dev_open(slave_dev);
1439 if (res) {
1440 dprintk("Openning slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001441 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001445 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if ((bond->params.mode == BOND_MODE_TLB) ||
1448 (bond->params.mode == BOND_MODE_ALB)) {
1449 /* bond_alb_init_slave() must be called before all other stages since
1450 * it might fail and we do not want to have to undo everything
1451 */
1452 res = bond_alb_init_slave(bond, new_slave);
1453 if (res) {
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001454 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 }
1457
1458 /* If the mode USES_PRIMARY, then the new slave gets the
1459 * master's promisc (and mc) settings only if it becomes the
1460 * curr_active_slave, and that is taken care of later when calling
1461 * bond_change_active()
1462 */
1463 if (!USES_PRIMARY(bond->params.mode)) {
1464 /* set promiscuity level to new slave */
1465 if (bond_dev->flags & IFF_PROMISC) {
1466 dev_set_promiscuity(slave_dev, 1);
1467 }
1468
1469 /* set allmulti level to new slave */
1470 if (bond_dev->flags & IFF_ALLMULTI) {
1471 dev_set_allmulti(slave_dev, 1);
1472 }
1473
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001474 netif_tx_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* upload master's mc_list to new slave */
1476 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1477 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1478 }
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001479 netif_tx_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
1482 if (bond->params.mode == BOND_MODE_8023AD) {
1483 /* add lacpdu mc addr to mc list */
1484 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1485
1486 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1487 }
1488
1489 bond_add_vlans_on_slave(bond, slave_dev);
1490
1491 write_lock_bh(&bond->lock);
1492
1493 bond_attach_slave(bond, new_slave);
1494
1495 new_slave->delay = 0;
1496 new_slave->link_failure_count = 0;
1497
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001498 bond_compute_features(bond);
1499
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001500 new_slave->last_arp_rx = jiffies;
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (bond->params.miimon && !bond->params.use_carrier) {
1503 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1504
1505 if ((link_reporting == -1) && !bond->params.arp_interval) {
1506 /*
1507 * miimon is set but a bonded network driver
1508 * does not support ETHTOOL/MII and
1509 * arp_interval is not set. Note: if
1510 * use_carrier is enabled, we will never go
1511 * here (because netif_carrier is always
1512 * supported); thus, we don't need to change
1513 * the messages for netif_carrier.
1514 */
1515 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001516 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 "available for interface %s, and "
1518 "arp_interval/arp_ip_target module parameters "
1519 "not specified, thus bonding will not detect "
1520 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001521 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 } else if (link_reporting == -1) {
1523 /* unable get link status using mii/ethtool */
1524 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001525 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 "interface %s; the network driver associated "
1527 "with this interface does not support MII or "
1528 "ETHTOOL link status reporting, thus miimon "
1529 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001530 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 }
1533
1534 /* check for initial state */
1535 if (!bond->params.miimon ||
1536 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1537 if (bond->params.updelay) {
1538 dprintk("Initial state of slave_dev is "
1539 "BOND_LINK_BACK\n");
1540 new_slave->link = BOND_LINK_BACK;
1541 new_slave->delay = bond->params.updelay;
1542 } else {
1543 dprintk("Initial state of slave_dev is "
1544 "BOND_LINK_UP\n");
1545 new_slave->link = BOND_LINK_UP;
1546 }
1547 new_slave->jiffies = jiffies;
1548 } else {
1549 dprintk("Initial state of slave_dev is "
1550 "BOND_LINK_DOWN\n");
1551 new_slave->link = BOND_LINK_DOWN;
1552 }
1553
1554 if (bond_update_speed_duplex(new_slave) &&
1555 (new_slave->link != BOND_LINK_DOWN)) {
1556 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001557 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001559 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (bond->params.mode == BOND_MODE_8023AD) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001562 printk(KERN_WARNING DRV_NAME
1563 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001565 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567 }
1568
1569 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1570 /* if there is a primary slave, remember it */
1571 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1572 bond->primary_slave = new_slave;
1573 }
1574 }
1575
1576 switch (bond->params.mode) {
1577 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001578 bond_set_slave_inactive_flags(new_slave);
1579 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 break;
1581 case BOND_MODE_8023AD:
1582 /* in 802.3ad mode, the internal mechanism
1583 * will activate the slaves in the selected
1584 * aggregator
1585 */
1586 bond_set_slave_inactive_flags(new_slave);
1587 /* if this is the first slave */
1588 if (bond->slave_cnt == 1) {
1589 SLAVE_AD_INFO(new_slave).id = 1;
1590 /* Initialize AD with the number of times that the AD timer is called in 1 second
1591 * can be called only after the mac address of the bond is set
1592 */
1593 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1594 bond->params.lacp_fast);
1595 } else {
1596 SLAVE_AD_INFO(new_slave).id =
1597 SLAVE_AD_INFO(new_slave->prev).id + 1;
1598 }
1599
1600 bond_3ad_bind_slave(new_slave);
1601 break;
1602 case BOND_MODE_TLB:
1603 case BOND_MODE_ALB:
1604 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001605 bond_set_slave_inactive_flags(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607 default:
1608 dprintk("This slave is always active in trunk mode\n");
1609
1610 /* always active in trunk mode */
1611 new_slave->state = BOND_STATE_ACTIVE;
1612
1613 /* In trunking mode there is little meaning to curr_active_slave
1614 * anyway (it holds no special properties of the bond device),
1615 * so we can change it without calling change_active_interface()
1616 */
1617 if (!bond->curr_active_slave) {
1618 bond->curr_active_slave = new_slave;
1619 }
1620 break;
1621 } /* switch(bond_mode) */
1622
Jay Vosburghff59c452006-03-27 13:27:43 -08001623 bond_set_carrier(bond);
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 write_unlock_bh(&bond->lock);
1626
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001627 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1628 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001629 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 printk(KERN_INFO DRV_NAME
1632 ": %s: enslaving %s as a%s interface with a%s link.\n",
1633 bond_dev->name, slave_dev->name,
1634 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1635 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1636
1637 /* enslave is successful */
1638 return 0;
1639
1640/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641err_close:
1642 dev_close(slave_dev);
1643
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001644err_unset_master:
1645 netdev_set_master(slave_dev, NULL);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001648 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001649 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1650 addr.sa_family = slave_dev->type;
1651 dev_set_mac_address(slave_dev, &addr);
1652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654err_free:
1655 kfree(new_slave);
1656
1657err_undo_flags:
1658 bond_dev->features = old_features;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return res;
1661}
1662
1663/*
1664 * Try to release the slave device <slave> from the bond device <master>
1665 * It is legal to access curr_active_slave without a lock because all the function
1666 * is write-locked.
1667 *
1668 * The rules for slave state should be:
1669 * for Active/Backup:
1670 * Active stays on all backups go down
1671 * for Bonded connections:
1672 * The first up interface should be left on and all others downed.
1673 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001674int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 struct bonding *bond = bond_dev->priv;
1677 struct slave *slave, *oldcurrent;
1678 struct sockaddr addr;
1679 int mac_addr_differ;
Joe Perches0795af52007-10-03 17:59:30 -07001680 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 /* slave is not a slave or master is not master of this slave */
1683 if (!(slave_dev->flags & IFF_SLAVE) ||
1684 (slave_dev->master != bond_dev)) {
1685 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001686 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 bond_dev->name, slave_dev->name);
1688 return -EINVAL;
1689 }
1690
1691 write_lock_bh(&bond->lock);
1692
1693 slave = bond_get_slave_by_dev(bond, slave_dev);
1694 if (!slave) {
1695 /* not a slave of this bond */
1696 printk(KERN_INFO DRV_NAME
1697 ": %s: %s not enslaved\n",
1698 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001699 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -EINVAL;
1701 }
1702
1703 mac_addr_differ = memcmp(bond_dev->dev_addr,
1704 slave->perm_hwaddr,
1705 ETH_ALEN);
1706 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1707 printk(KERN_WARNING DRV_NAME
Joe Perches0795af52007-10-03 17:59:30 -07001708 ": %s: Warning: the permanent HWaddr of %s - "
1709 "%s - is still in use by %s. "
1710 "Set the HWaddr of %s to a different address "
1711 "to avoid conflicts.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001712 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 slave_dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001714 print_mac(mac, slave->perm_hwaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 bond_dev->name,
1716 slave_dev->name);
1717 }
1718
1719 /* Inform AD package of unbinding of slave. */
1720 if (bond->params.mode == BOND_MODE_8023AD) {
1721 /* must be called before the slave is
1722 * detached from the list
1723 */
1724 bond_3ad_unbind_slave(slave);
1725 }
1726
1727 printk(KERN_INFO DRV_NAME
1728 ": %s: releasing %s interface %s\n",
1729 bond_dev->name,
1730 (slave->state == BOND_STATE_ACTIVE)
1731 ? "active" : "backup",
1732 slave_dev->name);
1733
1734 oldcurrent = bond->curr_active_slave;
1735
1736 bond->current_arp_slave = NULL;
1737
1738 /* release the slave from its bond */
1739 bond_detach_slave(bond, slave);
1740
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001741 bond_compute_features(bond);
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (bond->primary_slave == slave) {
1744 bond->primary_slave = NULL;
1745 }
1746
1747 if (oldcurrent == slave) {
1748 bond_change_active_slave(bond, NULL);
1749 }
1750
1751 if ((bond->params.mode == BOND_MODE_TLB) ||
1752 (bond->params.mode == BOND_MODE_ALB)) {
1753 /* Must be called only after the slave has been
1754 * detached from the list and the curr_active_slave
1755 * has been cleared (if our_slave == old_current),
1756 * but before a new active slave is selected.
1757 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001758 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001760 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001763 if (oldcurrent == slave) {
1764 /*
1765 * Note that we hold RTNL over this sequence, so there
1766 * is no concern that another slave add/remove event
1767 * will interfere.
1768 */
1769 write_unlock_bh(&bond->lock);
1770 read_lock(&bond->lock);
1771 write_lock_bh(&bond->curr_slave_lock);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 bond_select_active_slave(bond);
1774
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001775 write_unlock_bh(&bond->curr_slave_lock);
1776 read_unlock(&bond->lock);
1777 write_lock_bh(&bond->lock);
1778 }
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001781 bond_set_carrier(bond);
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* if the last slave was removed, zero the mac address
1784 * of the master so it will be set by the application
1785 * to the mac address of the first slave
1786 */
1787 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1788
1789 if (list_empty(&bond->vlan_list)) {
1790 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1791 } else {
1792 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001793 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001795 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001797 ": %s: When re-adding slaves, make sure the bond's "
1798 "HW address matches its VLANs'.\n",
1799 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1802 !bond_has_challenged_slaves(bond)) {
1803 printk(KERN_INFO DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001804 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001806 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1808 }
1809
1810 write_unlock_bh(&bond->lock);
1811
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001812 /* must do this from outside any spinlocks */
1813 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 bond_del_vlans_from_slave(bond, slave_dev);
1816
1817 /* If the mode USES_PRIMARY, then we should only remove its
1818 * promisc and mc settings if it was the curr_active_slave, but that was
1819 * already taken care of above when we detached the slave
1820 */
1821 if (!USES_PRIMARY(bond->params.mode)) {
1822 /* unset promiscuity level from slave */
1823 if (bond_dev->flags & IFF_PROMISC) {
1824 dev_set_promiscuity(slave_dev, -1);
1825 }
1826
1827 /* unset allmulti level from slave */
1828 if (bond_dev->flags & IFF_ALLMULTI) {
1829 dev_set_allmulti(slave_dev, -1);
1830 }
1831
1832 /* flush master's mc_list from slave */
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001833 netif_tx_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 bond_mc_list_flush(bond_dev, slave_dev);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001835 netif_tx_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837
1838 netdev_set_master(slave_dev, NULL);
1839
1840 /* close slave before restoring its mac address */
1841 dev_close(slave_dev);
1842
Jay Vosburghdd957c52007-10-09 19:57:24 -07001843 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001844 /* restore original ("permanent") mac address */
1845 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1846 addr.sa_family = slave_dev->type;
1847 dev_set_mac_address(slave_dev, &addr);
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001850 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001851 IFF_SLAVE_INACTIVE | IFF_BONDING |
1852 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 kfree(slave);
1855
1856 return 0; /* deletion OK */
1857}
1858
1859/*
Moni Shouad90a1622007-10-09 19:43:43 -07001860* Destroy a bonding device.
1861* Must be under rtnl_lock when this function is called.
1862*/
1863void bond_destroy(struct bonding *bond)
1864{
1865 bond_deinit(bond->dev);
1866 bond_destroy_sysfs_entry(bond);
Jay Vosburgh8cbdeec2007-11-13 21:16:29 -08001867 unregister_netdevice(bond->dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001868}
1869
1870/*
1871* First release a slave and than destroy the bond if no more slaves iare left.
1872* Must be under rtnl_lock when this function is called.
1873*/
1874int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1875{
1876 struct bonding *bond = bond_dev->priv;
1877 int ret;
1878
1879 ret = bond_release(bond_dev, slave_dev);
1880 if ((ret == 0) && (bond->slave_cnt == 0)) {
1881 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1882 bond_dev->name, bond_dev->name);
1883 bond_destroy(bond);
1884 }
1885 return ret;
1886}
1887
1888/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * This function releases all slaves.
1890 */
1891static int bond_release_all(struct net_device *bond_dev)
1892{
1893 struct bonding *bond = bond_dev->priv;
1894 struct slave *slave;
1895 struct net_device *slave_dev;
1896 struct sockaddr addr;
1897
1898 write_lock_bh(&bond->lock);
1899
Jay Vosburghff59c452006-03-27 13:27:43 -08001900 netif_carrier_off(bond_dev);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (bond->slave_cnt == 0) {
1903 goto out;
1904 }
1905
1906 bond->current_arp_slave = NULL;
1907 bond->primary_slave = NULL;
1908 bond_change_active_slave(bond, NULL);
1909
1910 while ((slave = bond->first_slave) != NULL) {
1911 /* Inform AD package of unbinding of slave
1912 * before slave is detached from the list.
1913 */
1914 if (bond->params.mode == BOND_MODE_8023AD) {
1915 bond_3ad_unbind_slave(slave);
1916 }
1917
1918 slave_dev = slave->dev;
1919 bond_detach_slave(bond, slave);
1920
Jay Vosburgh25433312008-01-17 16:24:59 -08001921 /* now that the slave is detached, unlock and perform
1922 * all the undo steps that should not be called from
1923 * within a lock.
1924 */
1925 write_unlock_bh(&bond->lock);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if ((bond->params.mode == BOND_MODE_TLB) ||
1928 (bond->params.mode == BOND_MODE_ALB)) {
1929 /* must be called only after the slave
1930 * has been detached from the list
1931 */
1932 bond_alb_deinit_slave(bond, slave);
1933 }
1934
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001935 bond_compute_features(bond);
1936
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001937 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 bond_del_vlans_from_slave(bond, slave_dev);
1939
1940 /* If the mode USES_PRIMARY, then we should only remove its
1941 * promisc and mc settings if it was the curr_active_slave, but that was
1942 * already taken care of above when we detached the slave
1943 */
1944 if (!USES_PRIMARY(bond->params.mode)) {
1945 /* unset promiscuity level from slave */
1946 if (bond_dev->flags & IFF_PROMISC) {
1947 dev_set_promiscuity(slave_dev, -1);
1948 }
1949
1950 /* unset allmulti level from slave */
1951 if (bond_dev->flags & IFF_ALLMULTI) {
1952 dev_set_allmulti(slave_dev, -1);
1953 }
1954
1955 /* flush master's mc_list from slave */
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001956 netif_tx_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 bond_mc_list_flush(bond_dev, slave_dev);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001958 netif_tx_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
1961 netdev_set_master(slave_dev, NULL);
1962
1963 /* close slave before restoring its mac address */
1964 dev_close(slave_dev);
1965
Jay Vosburghdd957c52007-10-09 19:57:24 -07001966 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001967 /* restore original ("permanent") mac address*/
1968 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1969 addr.sa_family = slave_dev->type;
1970 dev_set_mac_address(slave_dev, &addr);
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001973 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1974 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 kfree(slave);
1977
1978 /* re-acquire the lock before getting the next slave */
1979 write_lock_bh(&bond->lock);
1980 }
1981
1982 /* zero the mac address of the master so it will be
1983 * set by the application to the mac address of the
1984 * first slave
1985 */
1986 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1987
1988 if (list_empty(&bond->vlan_list)) {
1989 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1990 } else {
1991 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001992 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001994 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001996 ": %s: When re-adding slaves, make sure the bond's "
1997 "HW address matches its VLANs'.\n",
1998 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000
2001 printk(KERN_INFO DRV_NAME
2002 ": %s: released all slaves\n",
2003 bond_dev->name);
2004
2005out:
2006 write_unlock_bh(&bond->lock);
2007
2008 return 0;
2009}
2010
2011/*
2012 * This function changes the active slave to slave <slave_dev>.
2013 * It returns -EINVAL in the following cases.
2014 * - <slave_dev> is not found in the list.
2015 * - There is not active slave now.
2016 * - <slave_dev> is already active.
2017 * - The link state of <slave_dev> is not BOND_LINK_UP.
2018 * - <slave_dev> is not running.
2019 * In these cases, this fuction does nothing.
2020 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2021 */
2022static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2023{
2024 struct bonding *bond = bond_dev->priv;
2025 struct slave *old_active = NULL;
2026 struct slave *new_active = NULL;
2027 int res = 0;
2028
2029 if (!USES_PRIMARY(bond->params.mode)) {
2030 return -EINVAL;
2031 }
2032
2033 /* Verify that master_dev is indeed the master of slave_dev */
2034 if (!(slave_dev->flags & IFF_SLAVE) ||
2035 (slave_dev->master != bond_dev)) {
2036 return -EINVAL;
2037 }
2038
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002039 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002041 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002043 read_unlock(&bond->curr_slave_lock);
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 new_active = bond_get_slave_by_dev(bond, slave_dev);
2046
2047 /*
2048 * Changing to the current active: do nothing; return success.
2049 */
2050 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002051 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return 0;
2053 }
2054
2055 if ((new_active) &&
2056 (old_active) &&
2057 (new_active->link == BOND_LINK_UP) &&
2058 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002059 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002061 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 } else {
2063 res = -EINVAL;
2064 }
2065
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002066 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 return res;
2069}
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2072{
2073 struct bonding *bond = bond_dev->priv;
2074
2075 info->bond_mode = bond->params.mode;
2076 info->miimon = bond->params.miimon;
2077
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002078 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002080 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 return 0;
2083}
2084
2085static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2086{
2087 struct bonding *bond = bond_dev->priv;
2088 struct slave *slave;
2089 int i, found = 0;
2090
2091 if (info->slave_id < 0) {
2092 return -ENODEV;
2093 }
2094
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002095 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 bond_for_each_slave(bond, slave, i) {
2098 if (i == (int)info->slave_id) {
2099 found = 1;
2100 break;
2101 }
2102 }
2103
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002104 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 if (found) {
2107 strcpy(info->slave_name, slave->dev->name);
2108 info->link = slave->link;
2109 info->state = slave->state;
2110 info->link_failure_count = slave->link_failure_count;
2111 } else {
2112 return -ENODEV;
2113 }
2114
2115 return 0;
2116}
2117
2118/*-------------------------------- Monitoring -------------------------------*/
2119
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002120/*
2121 * if !have_locks, return nonzero if a failover is necessary. if
2122 * have_locks, do whatever failover activities are needed.
2123 *
2124 * This is to separate the inspection and failover steps for locking
2125 * purposes; failover requires rtnl, but acquiring it for every
2126 * inspection is undesirable, so a wrapper first does inspection, and
2127 * the acquires the necessary locks and calls again to perform
2128 * failover if needed. Since all locks are dropped, a complete
2129 * restart is needed between calls.
2130 */
2131static int __bond_mii_monitor(struct bonding *bond, int have_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 struct slave *slave, *oldcurrent;
2134 int do_failover = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int i;
2136
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002137 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* we will try to read the link status of each of our slaves, and
2141 * set their IFF_RUNNING flag appropriately. For each slave not
2142 * supporting MII status, we won't do anything so that a user-space
2143 * program could monitor the link itself if needed.
2144 */
2145
Moni Shoua1053f622007-10-09 19:43:42 -07002146 if (bond->send_grat_arp) {
2147 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2148 &bond->curr_active_slave->dev->state))
2149 dprintk("Needs to send gratuitous arp but not yet\n");
2150 else {
2151 dprintk("sending delayed gratuitous arp on on %s\n",
2152 bond->curr_active_slave->dev->name);
2153 bond_send_gratuitous_arp(bond);
Moni Shoua7893b242008-05-17 21:10:12 -07002154 bond->send_grat_arp--;
Moni Shoua1053f622007-10-09 19:43:42 -07002155 }
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 read_lock(&bond->curr_slave_lock);
2158 oldcurrent = bond->curr_active_slave;
2159 read_unlock(&bond->curr_slave_lock);
2160
2161 bond_for_each_slave(bond, slave, i) {
2162 struct net_device *slave_dev = slave->dev;
2163 int link_state;
2164 u16 old_speed = slave->speed;
2165 u8 old_duplex = slave->duplex;
2166
2167 link_state = bond_check_dev_link(bond, slave_dev, 0);
2168
2169 switch (slave->link) {
2170 case BOND_LINK_UP: /* the link was up */
2171 if (link_state == BMSR_LSTATUS) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002172 if (!oldcurrent) {
2173 if (!have_locks)
2174 return 1;
2175 do_failover = 1;
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 break;
2178 } else { /* link going down */
2179 slave->link = BOND_LINK_FAIL;
2180 slave->delay = bond->params.downdelay;
2181
2182 if (slave->link_failure_count < UINT_MAX) {
2183 slave->link_failure_count++;
2184 }
2185
2186 if (bond->params.downdelay) {
2187 printk(KERN_INFO DRV_NAME
2188 ": %s: link status down for %s "
2189 "interface %s, disabling it in "
2190 "%d ms.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002191 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 IS_UP(slave_dev)
2193 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2194 ? ((slave == oldcurrent)
2195 ? "active " : "backup ")
2196 : "")
2197 : "idle ",
2198 slave_dev->name,
2199 bond->params.downdelay * bond->params.miimon);
2200 }
2201 }
2202 /* no break ! fall through the BOND_LINK_FAIL test to
2203 ensure proper action to be taken
2204 */
2205 case BOND_LINK_FAIL: /* the link has just gone down */
2206 if (link_state != BMSR_LSTATUS) {
2207 /* link stays down */
2208 if (slave->delay <= 0) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002209 if (!have_locks)
2210 return 1;
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* link down for too long time */
2213 slave->link = BOND_LINK_DOWN;
2214
2215 /* in active/backup mode, we must
2216 * completely disable this interface
2217 */
2218 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2219 (bond->params.mode == BOND_MODE_8023AD)) {
2220 bond_set_slave_inactive_flags(slave);
2221 }
2222
2223 printk(KERN_INFO DRV_NAME
2224 ": %s: link status definitely "
2225 "down for interface %s, "
2226 "disabling it\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002227 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 slave_dev->name);
2229
2230 /* notify ad that the link status has changed */
2231 if (bond->params.mode == BOND_MODE_8023AD) {
2232 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2233 }
2234
2235 if ((bond->params.mode == BOND_MODE_TLB) ||
2236 (bond->params.mode == BOND_MODE_ALB)) {
2237 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2238 }
2239
2240 if (slave == oldcurrent) {
2241 do_failover = 1;
2242 }
2243 } else {
2244 slave->delay--;
2245 }
2246 } else {
2247 /* link up again */
2248 slave->link = BOND_LINK_UP;
2249 slave->jiffies = jiffies;
2250 printk(KERN_INFO DRV_NAME
2251 ": %s: link status up again after %d "
2252 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002253 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2255 slave_dev->name);
2256 }
2257 break;
2258 case BOND_LINK_DOWN: /* the link was down */
2259 if (link_state != BMSR_LSTATUS) {
2260 /* the link stays down, nothing more to do */
2261 break;
2262 } else { /* link going up */
2263 slave->link = BOND_LINK_BACK;
2264 slave->delay = bond->params.updelay;
2265
2266 if (bond->params.updelay) {
2267 /* if updelay == 0, no need to
2268 advertise about a 0 ms delay */
2269 printk(KERN_INFO DRV_NAME
2270 ": %s: link status up for "
2271 "interface %s, enabling it "
2272 "in %d ms.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002273 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 slave_dev->name,
2275 bond->params.updelay * bond->params.miimon);
2276 }
2277 }
2278 /* no break ! fall through the BOND_LINK_BACK state in
2279 case there's something to do.
2280 */
2281 case BOND_LINK_BACK: /* the link has just come back */
2282 if (link_state != BMSR_LSTATUS) {
2283 /* link down again */
2284 slave->link = BOND_LINK_DOWN;
2285
2286 printk(KERN_INFO DRV_NAME
2287 ": %s: link status down again after %d "
2288 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002289 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 (bond->params.updelay - slave->delay) * bond->params.miimon,
2291 slave_dev->name);
2292 } else {
2293 /* link stays up */
2294 if (slave->delay == 0) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002295 if (!have_locks)
2296 return 1;
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* now the link has been up for long time enough */
2299 slave->link = BOND_LINK_UP;
2300 slave->jiffies = jiffies;
2301
2302 if (bond->params.mode == BOND_MODE_8023AD) {
2303 /* prevent it from being the active one */
2304 slave->state = BOND_STATE_BACKUP;
2305 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2306 /* make it immediately active */
2307 slave->state = BOND_STATE_ACTIVE;
2308 } else if (slave != bond->primary_slave) {
2309 /* prevent it from being the active one */
2310 slave->state = BOND_STATE_BACKUP;
2311 }
2312
2313 printk(KERN_INFO DRV_NAME
2314 ": %s: link status definitely "
2315 "up for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002316 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 slave_dev->name);
2318
2319 /* notify ad that the link status has changed */
2320 if (bond->params.mode == BOND_MODE_8023AD) {
2321 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2322 }
2323
2324 if ((bond->params.mode == BOND_MODE_TLB) ||
2325 (bond->params.mode == BOND_MODE_ALB)) {
2326 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2327 }
2328
2329 if ((!oldcurrent) ||
2330 (slave == bond->primary_slave)) {
2331 do_failover = 1;
2332 }
2333 } else {
2334 slave->delay--;
2335 }
2336 }
2337 break;
2338 default:
2339 /* Should not happen */
Mitch Williams4e0952c2005-11-09 10:34:57 -08002340 printk(KERN_ERR DRV_NAME
2341 ": %s: Error: %s Illegal value (link=%d)\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002342 bond->dev->name,
Mitch Williams4e0952c2005-11-09 10:34:57 -08002343 slave->dev->name,
2344 slave->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 goto out;
2346 } /* end of switch (slave->link) */
2347
2348 bond_update_speed_duplex(slave);
2349
2350 if (bond->params.mode == BOND_MODE_8023AD) {
2351 if (old_speed != slave->speed) {
2352 bond_3ad_adapter_speed_changed(slave);
2353 }
2354
2355 if (old_duplex != slave->duplex) {
2356 bond_3ad_adapter_duplex_changed(slave);
2357 }
2358 }
2359
2360 } /* end of for */
2361
2362 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002363 ASSERT_RTNL();
2364
2365 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 bond_select_active_slave(bond);
2368
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002369 write_unlock_bh(&bond->curr_slave_lock);
2370
Jay Vosburghff59c452006-03-27 13:27:43 -08002371 } else
2372 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002378/*
2379 * bond_mii_monitor
2380 *
2381 * Really a wrapper that splits the mii monitor into two phases: an
2382 * inspection, then (if inspection indicates something needs to be
2383 * done) an acquisition of appropriate locks followed by another pass
2384 * to implement whatever link state changes are indicated.
2385 */
2386void bond_mii_monitor(struct work_struct *work)
2387{
2388 struct bonding *bond = container_of(work, struct bonding,
2389 mii_work.work);
2390 unsigned long delay;
2391
2392 read_lock(&bond->lock);
2393 if (bond->kill_timers) {
2394 read_unlock(&bond->lock);
2395 return;
2396 }
2397 if (__bond_mii_monitor(bond, 0)) {
2398 read_unlock(&bond->lock);
2399 rtnl_lock();
2400 read_lock(&bond->lock);
2401 __bond_mii_monitor(bond, 1);
Jay Vosburgh56556622008-01-17 16:25:03 -08002402 read_unlock(&bond->lock);
2403 rtnl_unlock(); /* might sleep, hold no other locks */
2404 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002405 }
2406
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002407 delay = msecs_to_jiffies(bond->params.miimon);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002408 read_unlock(&bond->lock);
2409 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2410}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002411
Al Virod3bb52b2007-08-22 20:06:58 -04002412static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002413{
2414 struct in_device *idev;
2415 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002416 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002417
2418 if (!dev)
2419 return 0;
2420
2421 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002422 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002423 if (!idev)
2424 goto out;
2425
2426 ifa = idev->ifa_list;
2427 if (!ifa)
2428 goto out;
2429
2430 addr = ifa->ifa_local;
2431out:
2432 rcu_read_unlock();
2433 return addr;
2434}
2435
Al Virod3bb52b2007-08-22 20:06:58 -04002436static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002437{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002438 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002439
2440 if (ip == bond->master_ip)
2441 return 1;
2442
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002443 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002444 if (ip == vlan->vlan_ip)
2445 return 1;
2446 }
2447
2448 return 0;
2449}
2450
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002451/*
2452 * We go to the (large) trouble of VLAN tagging ARP frames because
2453 * switches in VLAN mode (especially if ports are configured as
2454 * "native" to a VLAN) might not pass non-tagged frames.
2455 */
Al Virod3bb52b2007-08-22 20:06:58 -04002456static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002457{
2458 struct sk_buff *skb;
2459
2460 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2461 slave_dev->name, dest_ip, src_ip, vlan_id);
2462
2463 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2464 NULL, slave_dev->dev_addr, NULL);
2465
2466 if (!skb) {
2467 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2468 return;
2469 }
2470 if (vlan_id) {
2471 skb = vlan_put_tag(skb, vlan_id);
2472 if (!skb) {
2473 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2474 return;
2475 }
2476 }
2477 arp_xmit(skb);
2478}
2479
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2482{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002483 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002484 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002485 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002486 struct net_device *vlan_dev;
2487 struct flowi fl;
2488 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Mitch Williams6b780562005-11-09 10:36:19 -08002490 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2491 if (!targets[i])
2492 continue;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002493 dprintk("basa: target %x\n", targets[i]);
2494 if (list_empty(&bond->vlan_list)) {
2495 dprintk("basa: empty vlan: arp_send\n");
2496 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2497 bond->master_ip, 0);
2498 continue;
2499 }
2500
2501 /*
2502 * If VLANs are configured, we do a route lookup to
2503 * determine which VLAN interface would be used, so we
2504 * can tag the ARP with the proper VLAN tag.
2505 */
2506 memset(&fl, 0, sizeof(fl));
2507 fl.fl4_dst = targets[i];
2508 fl.fl4_tos = RTO_ONLINK;
2509
Denis V. Lunevf2063512008-01-22 22:07:34 -08002510 rv = ip_route_output_key(&init_net, &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002511 if (rv) {
2512 if (net_ratelimit()) {
2513 printk(KERN_WARNING DRV_NAME
2514 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2515 bond->dev->name, NIPQUAD(fl.fl4_dst));
2516 }
2517 continue;
2518 }
2519
2520 /*
2521 * This target is not on a VLAN
2522 */
2523 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002524 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002525 dprintk("basa: rtdev == bond->dev: arp_send\n");
2526 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2527 bond->master_ip, 0);
2528 continue;
2529 }
2530
2531 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002532 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002533 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002534 if (vlan_dev == rt->u.dst.dev) {
2535 vlan_id = vlan->vlan_id;
2536 dprintk("basa: vlan match on %s %d\n",
2537 vlan_dev->name, vlan_id);
2538 break;
2539 }
2540 }
2541
2542 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002543 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002544 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2545 vlan->vlan_ip, vlan_id);
2546 continue;
2547 }
2548
2549 if (net_ratelimit()) {
2550 printk(KERN_WARNING DRV_NAME
2551 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2552 bond->dev->name, NIPQUAD(fl.fl4_dst),
2553 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2554 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002555 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002556 }
2557}
2558
2559/*
2560 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2561 * for each VLAN above us.
2562 */
2563static void bond_send_gratuitous_arp(struct bonding *bond)
2564{
2565 struct slave *slave = bond->curr_active_slave;
2566 struct vlan_entry *vlan;
2567 struct net_device *vlan_dev;
2568
2569 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2570 slave ? slave->dev->name : "NULL");
2571 if (!slave)
2572 return;
2573
2574 if (bond->master_ip) {
2575 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002576 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002577 }
2578
2579 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002580 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002581 if (vlan->vlan_ip) {
2582 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2583 vlan->vlan_ip, vlan->vlan_id);
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586}
2587
Al Virod3bb52b2007-08-22 20:06:58 -04002588static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002589{
2590 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002591 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002592
2593 targets = bond->params.arp_targets;
2594 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2595 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2596 "%u.%u.%u.%u bhti(tip) %d\n",
2597 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2598 bond_has_this_ip(bond, tip));
2599 if (sip == targets[i]) {
2600 if (bond_has_this_ip(bond, tip))
2601 slave->last_arp_rx = jiffies;
2602 return;
2603 }
2604 }
2605}
2606
2607static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2608{
2609 struct arphdr *arp;
2610 struct slave *slave;
2611 struct bonding *bond;
2612 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002613 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002614
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002615 if (dev_net(dev) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -07002616 goto out;
2617
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002618 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2619 goto out;
2620
2621 bond = dev->priv;
2622 read_lock(&bond->lock);
2623
2624 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2625 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2626 orig_dev ? orig_dev->name : "NULL");
2627
2628 slave = bond_get_slave_by_dev(bond, orig_dev);
2629 if (!slave || !slave_do_arp_validate(bond, slave))
2630 goto out_unlock;
2631
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002632 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002633 goto out_unlock;
2634
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002635 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002636 if (arp->ar_hln != dev->addr_len ||
2637 skb->pkt_type == PACKET_OTHERHOST ||
2638 skb->pkt_type == PACKET_LOOPBACK ||
2639 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2640 arp->ar_pro != htons(ETH_P_IP) ||
2641 arp->ar_pln != 4)
2642 goto out_unlock;
2643
2644 arp_ptr = (unsigned char *)(arp + 1);
2645 arp_ptr += dev->addr_len;
2646 memcpy(&sip, arp_ptr, 4);
2647 arp_ptr += 4 + dev->addr_len;
2648 memcpy(&tip, arp_ptr, 4);
2649
2650 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2651 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2652 slave->state, bond->params.arp_validate,
2653 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2654
2655 /*
2656 * Backup slaves won't see the ARP reply, but do come through
2657 * here for each ARP probe (so we swap the sip/tip to validate
2658 * the probe). In a "redundant switch, common router" type of
2659 * configuration, the ARP probe will (hopefully) travel from
2660 * the active, through one switch, the router, then the other
2661 * switch before reaching the backup.
2662 */
2663 if (slave->state == BOND_STATE_ACTIVE)
2664 bond_validate_arp(bond, slave, sip, tip);
2665 else
2666 bond_validate_arp(bond, slave, tip, sip);
2667
2668out_unlock:
2669 read_unlock(&bond->lock);
2670out:
2671 dev_kfree_skb(skb);
2672 return NET_RX_SUCCESS;
2673}
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675/*
2676 * this function is called regularly to monitor each slave's link
2677 * ensuring that traffic is being sent and received when arp monitoring
2678 * is used in load-balancing mode. if the adapter has been dormant, then an
2679 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2680 * arp monitoring in active backup mode.
2681 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002682void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002684 struct bonding *bond = container_of(work, struct bonding,
2685 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct slave *slave, *oldcurrent;
2687 int do_failover = 0;
2688 int delta_in_ticks;
2689 int i;
2690
2691 read_lock(&bond->lock);
2692
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002693 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 if (bond->kill_timers) {
2696 goto out;
2697 }
2698
2699 if (bond->slave_cnt == 0) {
2700 goto re_arm;
2701 }
2702
2703 read_lock(&bond->curr_slave_lock);
2704 oldcurrent = bond->curr_active_slave;
2705 read_unlock(&bond->curr_slave_lock);
2706
2707 /* see if any of the previous devices are up now (i.e. they have
2708 * xmt and rcv traffic). the curr_active_slave does not come into
2709 * the picture unless it is null. also, slave->jiffies is not needed
2710 * here because we send an arp on each slave and give a slave as
2711 * long as it needs to get the tx/rx within the delta.
2712 * TODO: what about up/down delay in arp mode? it wasn't here before
2713 * so it can wait
2714 */
2715 bond_for_each_slave(bond, slave, i) {
2716 if (slave->link != BOND_LINK_UP) {
David Sterbab63bb732007-12-06 23:40:33 -08002717 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2718 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 slave->link = BOND_LINK_UP;
2721 slave->state = BOND_STATE_ACTIVE;
2722
2723 /* primary_slave has no meaning in round-robin
2724 * mode. the window of a slave being up and
2725 * curr_active_slave being null after enslaving
2726 * is closed.
2727 */
2728 if (!oldcurrent) {
2729 printk(KERN_INFO DRV_NAME
2730 ": %s: link status definitely "
2731 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002732 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 slave->dev->name);
2734 do_failover = 1;
2735 } else {
2736 printk(KERN_INFO DRV_NAME
2737 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002738 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 slave->dev->name);
2740 }
2741 }
2742 } else {
2743 /* slave->link == BOND_LINK_UP */
2744
2745 /* not all switches will respond to an arp request
2746 * when the source ip is 0, so don't take the link down
2747 * if we don't know our ip yet
2748 */
David Sterbab63bb732007-12-06 23:40:33 -08002749 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002750 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 slave->link = BOND_LINK_DOWN;
2753 slave->state = BOND_STATE_BACKUP;
2754
2755 if (slave->link_failure_count < UINT_MAX) {
2756 slave->link_failure_count++;
2757 }
2758
2759 printk(KERN_INFO DRV_NAME
2760 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002761 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 slave->dev->name);
2763
2764 if (slave == oldcurrent) {
2765 do_failover = 1;
2766 }
2767 }
2768 }
2769
2770 /* note: if switch is in round-robin mode, all links
2771 * must tx arp to ensure all links rx an arp - otherwise
2772 * links may oscillate or not come up at all; if switch is
2773 * in something like xor mode, there is nothing we can
2774 * do - all replies will be rx'ed on same link causing slaves
2775 * to be unstable during low/no traffic periods
2776 */
2777 if (IS_UP(slave->dev)) {
2778 bond_arp_send_all(bond, slave);
2779 }
2780 }
2781
2782 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002783 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 bond_select_active_slave(bond);
2786
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002787 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789
2790re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002791 if (bond->params.arp_interval)
2792 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793out:
2794 read_unlock(&bond->lock);
2795}
2796
2797/*
2798 * When using arp monitoring in active-backup mode, this function is
2799 * called to determine if any backup slaves have went down or a new
2800 * current slave needs to be found.
2801 * The backup slaves never generate traffic, they are considered up by merely
2802 * receiving traffic. If the current slave goes down, each backup slave will
2803 * be given the opportunity to tx/rx an arp before being taken down - this
2804 * prevents all slaves from being taken down due to the current slave not
2805 * sending any traffic for the backups to receive. The arps are not necessarily
2806 * necessary, any tx and rx traffic will keep the current slave up. While any
2807 * rx traffic will keep the backup slaves up, the current slave is responsible
2808 * for generating traffic to keep them up regardless of any other traffic they
2809 * may have received.
2810 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2811 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002812void bond_activebackup_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002814 struct bonding *bond = container_of(work, struct bonding,
2815 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct slave *slave;
2817 int delta_in_ticks;
2818 int i;
2819
2820 read_lock(&bond->lock);
2821
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002822 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 if (bond->kill_timers) {
2825 goto out;
2826 }
2827
2828 if (bond->slave_cnt == 0) {
2829 goto re_arm;
2830 }
2831
2832 /* determine if any slave has come up or any backup slave has
2833 * gone down
2834 * TODO: what about up/down delay in arp mode? it wasn't here before
2835 * so it can wait
2836 */
2837 bond_for_each_slave(bond, slave, i) {
2838 if (slave->link != BOND_LINK_UP) {
David Sterbab63bb732007-12-06 23:40:33 -08002839 if (time_before_eq(jiffies,
2840 slave_last_rx(bond, slave) + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 slave->link = BOND_LINK_UP;
2843
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002844 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 if ((!bond->curr_active_slave) &&
David Sterbab63bb732007-12-06 23:40:33 -08002847 time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 bond_change_active_slave(bond, slave);
2849 bond->current_arp_slave = NULL;
2850 } else if (bond->curr_active_slave != slave) {
2851 /* this slave has just come up but we
2852 * already have a current slave; this
2853 * can also happen if bond_enslave adds
2854 * a new slave that is up while we are
2855 * searching for a new slave
2856 */
2857 bond_set_slave_inactive_flags(slave);
2858 bond->current_arp_slave = NULL;
2859 }
2860
Jay Vosburghff59c452006-03-27 13:27:43 -08002861 bond_set_carrier(bond);
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (slave == bond->curr_active_slave) {
2864 printk(KERN_INFO DRV_NAME
2865 ": %s: %s is up and now the "
2866 "active interface\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002867 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 slave->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08002869 netif_carrier_on(bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 } else {
2871 printk(KERN_INFO DRV_NAME
2872 ": %s: backup interface %s is "
2873 "now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002874 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 slave->dev->name);
2876 }
2877
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002878 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880 } else {
2881 read_lock(&bond->curr_slave_lock);
2882
2883 if ((slave != bond->curr_active_slave) &&
2884 (!bond->current_arp_slave) &&
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002885 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 3*delta_in_ticks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /* a backup slave has gone down; three times
2887 * the delta allows the current slave to be
2888 * taken out before the backup slave.
2889 * note: a non-null current_arp_slave indicates
2890 * the curr_active_slave went down and we are
2891 * searching for a new one; under this
2892 * condition we only take the curr_active_slave
2893 * down - this gives each slave a chance to
2894 * tx/rx traffic before being taken out
2895 */
2896
2897 read_unlock(&bond->curr_slave_lock);
2898
2899 slave->link = BOND_LINK_DOWN;
2900
2901 if (slave->link_failure_count < UINT_MAX) {
2902 slave->link_failure_count++;
2903 }
2904
2905 bond_set_slave_inactive_flags(slave);
2906
2907 printk(KERN_INFO DRV_NAME
2908 ": %s: backup interface %s is now down\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002909 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 slave->dev->name);
2911 } else {
2912 read_unlock(&bond->curr_slave_lock);
2913 }
2914 }
2915 }
2916
2917 read_lock(&bond->curr_slave_lock);
2918 slave = bond->curr_active_slave;
2919 read_unlock(&bond->curr_slave_lock);
2920
2921 if (slave) {
2922 /* if we have sent traffic in the past 2*arp_intervals but
2923 * haven't xmit and rx traffic in that time interval, select
2924 * a different slave. slave->jiffies is only updated when
2925 * a slave first becomes the curr_active_slave - not necessarily
2926 * after every arp; this ensures the slave has a full 2*delta
2927 * before being taken out. if a primary is being used, check
2928 * if it is up and needs to take over as the curr_active_slave
2929 */
David Sterbab63bb732007-12-06 23:40:33 -08002930 if ((time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002931 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 2*delta_in_ticks))) &&
David Sterbab63bb732007-12-06 23:40:33 -08002932 time_after_eq(jiffies, slave->jiffies + 2*delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 slave->link = BOND_LINK_DOWN;
2935
2936 if (slave->link_failure_count < UINT_MAX) {
2937 slave->link_failure_count++;
2938 }
2939
2940 printk(KERN_INFO DRV_NAME
2941 ": %s: link status down for active interface "
2942 "%s, disabling it\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002943 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 slave->dev->name);
2945
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002946 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 bond_select_active_slave(bond);
2949 slave = bond->curr_active_slave;
2950
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002951 write_unlock_bh(&bond->curr_slave_lock);
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 bond->current_arp_slave = slave;
2954
2955 if (slave) {
2956 slave->jiffies = jiffies;
2957 }
2958 } else if ((bond->primary_slave) &&
2959 (bond->primary_slave != slave) &&
2960 (bond->primary_slave->link == BOND_LINK_UP)) {
2961 /* at this point, slave is the curr_active_slave */
2962 printk(KERN_INFO DRV_NAME
2963 ": %s: changing from interface %s to primary "
2964 "interface %s\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002965 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 slave->dev->name,
2967 bond->primary_slave->dev->name);
2968
2969 /* primary is up so switch to it */
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002970 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 bond_change_active_slave(bond, bond->primary_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002972 write_unlock_bh(&bond->curr_slave_lock);
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 slave = bond->primary_slave;
2975 slave->jiffies = jiffies;
2976 } else {
2977 bond->current_arp_slave = NULL;
2978 }
2979
2980 /* the current slave must tx an arp to ensure backup slaves
2981 * rx traffic
2982 */
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002983 if (slave && IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986
2987 /* if we don't have a curr_active_slave, search for the next available
2988 * backup slave from the current_arp_slave and make it the candidate
2989 * for becoming the curr_active_slave
2990 */
2991 if (!slave) {
2992 if (!bond->current_arp_slave) {
2993 bond->current_arp_slave = bond->first_slave;
2994 }
2995
2996 if (bond->current_arp_slave) {
2997 bond_set_slave_inactive_flags(bond->current_arp_slave);
2998
2999 /* search for next candidate */
Jay Vosburgh2f872f02005-05-26 12:56:59 -07003000 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (IS_UP(slave->dev)) {
3002 slave->link = BOND_LINK_BACK;
3003 bond_set_slave_active_flags(slave);
3004 bond_arp_send_all(bond, slave);
3005 slave->jiffies = jiffies;
3006 bond->current_arp_slave = slave;
3007 break;
3008 }
3009
3010 /* if the link state is up at this point, we
3011 * mark it down - this can happen if we have
3012 * simultaneous link failures and
3013 * reselect_active_interface doesn't make this
3014 * one the current slave so it is still marked
3015 * up when it is actually down
3016 */
3017 if (slave->link == BOND_LINK_UP) {
3018 slave->link = BOND_LINK_DOWN;
3019 if (slave->link_failure_count < UINT_MAX) {
3020 slave->link_failure_count++;
3021 }
3022
3023 bond_set_slave_inactive_flags(slave);
3024
3025 printk(KERN_INFO DRV_NAME
3026 ": %s: backup interface %s is "
3027 "now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003028 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 slave->dev->name);
3030 }
3031 }
3032 }
3033 }
3034
3035re_arm:
3036 if (bond->params.arp_interval) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003037 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039out:
3040 read_unlock(&bond->lock);
3041}
3042
3043/*------------------------------ proc/seq_file-------------------------------*/
3044
3045#ifdef CONFIG_PROC_FS
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3048{
3049 struct bonding *bond = seq->private;
3050 loff_t off = 0;
3051 struct slave *slave;
3052 int i;
3053
3054 /* make sure the bond won't be taken away */
3055 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003056 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 if (*pos == 0) {
3059 return SEQ_START_TOKEN;
3060 }
3061
3062 bond_for_each_slave(bond, slave, i) {
3063 if (++off == *pos) {
3064 return slave;
3065 }
3066 }
3067
3068 return NULL;
3069}
3070
3071static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3072{
3073 struct bonding *bond = seq->private;
3074 struct slave *slave = v;
3075
3076 ++*pos;
3077 if (v == SEQ_START_TOKEN) {
3078 return bond->first_slave;
3079 }
3080
3081 slave = slave->next;
3082
3083 return (slave == bond->first_slave) ? NULL : slave;
3084}
3085
3086static void bond_info_seq_stop(struct seq_file *seq, void *v)
3087{
3088 struct bonding *bond = seq->private;
3089
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003090 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 read_unlock(&dev_base_lock);
3092}
3093
3094static void bond_info_show_master(struct seq_file *seq)
3095{
3096 struct bonding *bond = seq->private;
3097 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003098 int i;
3099 u32 target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 read_lock(&bond->curr_slave_lock);
3102 curr = bond->curr_active_slave;
3103 read_unlock(&bond->curr_slave_lock);
3104
Jay Vosburghdd957c52007-10-09 19:57:24 -07003105 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 bond_mode_name(bond->params.mode));
3107
Jay Vosburghdd957c52007-10-09 19:57:24 -07003108 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3109 bond->params.fail_over_mac)
3110 seq_printf(seq, " (fail_over_mac)");
3111
3112 seq_printf(seq, "\n");
3113
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003114 if (bond->params.mode == BOND_MODE_XOR ||
3115 bond->params.mode == BOND_MODE_8023AD) {
3116 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3117 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3118 bond->params.xmit_policy);
3119 }
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (USES_PRIMARY(bond->params.mode)) {
3122 seq_printf(seq, "Primary Slave: %s\n",
Mitch Williams0f418b22005-11-09 10:35:21 -08003123 (bond->primary_slave) ?
3124 bond->primary_slave->dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 seq_printf(seq, "Currently Active Slave: %s\n",
3127 (curr) ? curr->dev->name : "None");
3128 }
3129
Jay Vosburghff59c452006-03-27 13:27:43 -08003130 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3131 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3133 seq_printf(seq, "Up Delay (ms): %d\n",
3134 bond->params.updelay * bond->params.miimon);
3135 seq_printf(seq, "Down Delay (ms): %d\n",
3136 bond->params.downdelay * bond->params.miimon);
3137
Mitch Williams4756b022005-11-09 10:36:25 -08003138
3139 /* ARP information */
3140 if(bond->params.arp_interval > 0) {
3141 int printed=0;
3142 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3143 bond->params.arp_interval);
3144
3145 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3146
3147 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3148 if (!bond->params.arp_targets[i])
3149 continue;
3150 if (printed)
3151 seq_printf(seq, ",");
3152 target = ntohl(bond->params.arp_targets[i]);
3153 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3154 printed = 1;
3155 }
3156 seq_printf(seq, "\n");
3157 }
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (bond->params.mode == BOND_MODE_8023AD) {
3160 struct ad_info ad_info;
Joe Perches0795af52007-10-03 17:59:30 -07003161 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 seq_puts(seq, "\n802.3ad info\n");
3164 seq_printf(seq, "LACP rate: %s\n",
3165 (bond->params.lacp_fast) ? "fast" : "slow");
3166
3167 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3168 seq_printf(seq, "bond %s has no active aggregator\n",
3169 bond->dev->name);
3170 } else {
3171 seq_printf(seq, "Active Aggregator Info:\n");
3172
3173 seq_printf(seq, "\tAggregator ID: %d\n",
3174 ad_info.aggregator_id);
3175 seq_printf(seq, "\tNumber of ports: %d\n",
3176 ad_info.ports);
3177 seq_printf(seq, "\tActor Key: %d\n",
3178 ad_info.actor_key);
3179 seq_printf(seq, "\tPartner Key: %d\n",
3180 ad_info.partner_key);
Joe Perches0795af52007-10-03 17:59:30 -07003181 seq_printf(seq, "\tPartner Mac Address: %s\n",
3182 print_mac(mac, ad_info.partner_system));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184 }
3185}
3186
3187static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3188{
3189 struct bonding *bond = seq->private;
Joe Perches0795af52007-10-03 17:59:30 -07003190 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3193 seq_printf(seq, "MII Status: %s\n",
3194 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003195 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 slave->link_failure_count);
3197
Jay Vosburgh217df672005-09-26 16:11:50 -07003198 seq_printf(seq,
Joe Perches0795af52007-10-03 17:59:30 -07003199 "Permanent HW addr: %s\n",
3200 print_mac(mac, slave->perm_hwaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 if (bond->params.mode == BOND_MODE_8023AD) {
3203 const struct aggregator *agg
3204 = SLAVE_AD_INFO(slave).port.aggregator;
3205
3206 if (agg) {
3207 seq_printf(seq, "Aggregator ID: %d\n",
3208 agg->aggregator_identifier);
3209 } else {
3210 seq_puts(seq, "Aggregator ID: N/A\n");
3211 }
3212 }
3213}
3214
3215static int bond_info_seq_show(struct seq_file *seq, void *v)
3216{
3217 if (v == SEQ_START_TOKEN) {
3218 seq_printf(seq, "%s\n", version);
3219 bond_info_show_master(seq);
3220 } else {
3221 bond_info_show_slave(seq, v);
3222 }
3223
3224 return 0;
3225}
3226
3227static struct seq_operations bond_info_seq_ops = {
3228 .start = bond_info_seq_start,
3229 .next = bond_info_seq_next,
3230 .stop = bond_info_seq_stop,
3231 .show = bond_info_seq_show,
3232};
3233
3234static int bond_info_open(struct inode *inode, struct file *file)
3235{
3236 struct seq_file *seq;
3237 struct proc_dir_entry *proc;
3238 int res;
3239
3240 res = seq_open(file, &bond_info_seq_ops);
3241 if (!res) {
3242 /* recover the pointer buried in proc_dir_entry data */
3243 seq = file->private_data;
3244 proc = PDE(inode);
3245 seq->private = proc->data;
3246 }
3247
3248 return res;
3249}
3250
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003251static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 .owner = THIS_MODULE,
3253 .open = bond_info_open,
3254 .read = seq_read,
3255 .llseek = seq_lseek,
3256 .release = seq_release,
3257};
3258
3259static int bond_create_proc_entry(struct bonding *bond)
3260{
3261 struct net_device *bond_dev = bond->dev;
3262
3263 if (bond_proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003264 bond->proc_entry = proc_create_data(bond_dev->name,
3265 S_IRUGO, bond_proc_dir,
3266 &bond_info_fops, bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (bond->proc_entry == NULL) {
3268 printk(KERN_WARNING DRV_NAME
3269 ": Warning: Cannot create /proc/net/%s/%s\n",
3270 DRV_NAME, bond_dev->name);
3271 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3273 }
3274 }
3275
3276 return 0;
3277}
3278
3279static void bond_remove_proc_entry(struct bonding *bond)
3280{
3281 if (bond_proc_dir && bond->proc_entry) {
3282 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3283 memset(bond->proc_file_name, 0, IFNAMSIZ);
3284 bond->proc_entry = NULL;
3285 }
3286}
3287
3288/* Create the bonding directory under /proc/net, if doesn't exist yet.
3289 * Caller must hold rtnl_lock.
3290 */
3291static void bond_create_proc_dir(void)
3292{
3293 int len = strlen(DRV_NAME);
3294
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003295 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 bond_proc_dir = bond_proc_dir->next) {
3297 if ((bond_proc_dir->namelen == len) &&
3298 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3299 break;
3300 }
3301 }
3302
3303 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003304 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (bond_proc_dir) {
3306 bond_proc_dir->owner = THIS_MODULE;
3307 } else {
3308 printk(KERN_WARNING DRV_NAME
3309 ": Warning: cannot create /proc/net/%s\n",
3310 DRV_NAME);
3311 }
3312 }
3313}
3314
3315/* Destroy the bonding directory under /proc/net, if empty.
3316 * Caller must hold rtnl_lock.
3317 */
3318static void bond_destroy_proc_dir(void)
3319{
3320 struct proc_dir_entry *de;
3321
3322 if (!bond_proc_dir) {
3323 return;
3324 }
3325
3326 /* verify that the /proc dir is empty */
3327 for (de = bond_proc_dir->subdir; de; de = de->next) {
3328 /* ignore . and .. */
3329 if (*(de->name) != '.') {
3330 break;
3331 }
3332 }
3333
3334 if (de) {
3335 if (bond_proc_dir->owner == THIS_MODULE) {
3336 bond_proc_dir->owner = NULL;
3337 }
3338 } else {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003339 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 bond_proc_dir = NULL;
3341 }
3342}
3343#endif /* CONFIG_PROC_FS */
3344
3345/*-------------------------- netdev event handling --------------------------*/
3346
3347/*
3348 * Change device name
3349 */
3350static int bond_event_changename(struct bonding *bond)
3351{
3352#ifdef CONFIG_PROC_FS
3353 bond_remove_proc_entry(bond);
3354 bond_create_proc_entry(bond);
3355#endif
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003356 down_write(&(bonding_rwsem));
3357 bond_destroy_sysfs_entry(bond);
3358 bond_create_sysfs_entry(bond);
3359 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return NOTIFY_DONE;
3361}
3362
3363static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3364{
3365 struct bonding *event_bond = bond_dev->priv;
3366
3367 switch (event) {
3368 case NETDEV_CHANGENAME:
3369 return bond_event_changename(event_bond);
3370 case NETDEV_UNREGISTER:
Jay Vosburgh3b96c852008-01-17 16:25:00 -08003371 bond_release_all(event_bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 break;
3373 default:
3374 break;
3375 }
3376
3377 return NOTIFY_DONE;
3378}
3379
3380static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3381{
3382 struct net_device *bond_dev = slave_dev->master;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003383 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 switch (event) {
3386 case NETDEV_UNREGISTER:
3387 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003388 if (bond->setup_by_slave)
3389 bond_release_and_destroy(bond_dev, slave_dev);
3390 else
3391 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
3393 break;
3394 case NETDEV_CHANGE:
3395 /*
3396 * TODO: is this what we get if somebody
3397 * sets up a hierarchical bond, then rmmod's
3398 * one of the slave bonding devices?
3399 */
3400 break;
3401 case NETDEV_DOWN:
3402 /*
3403 * ... Or is it this?
3404 */
3405 break;
3406 case NETDEV_CHANGEMTU:
3407 /*
3408 * TODO: Should slaves be allowed to
3409 * independently alter their MTU? For
3410 * an active-backup bond, slaves need
3411 * not be the same type of device, so
3412 * MTUs may vary. For other modes,
3413 * slaves arguably should have the
3414 * same MTUs. To do this, we'd need to
3415 * take over the slave's change_mtu
3416 * function for the duration of their
3417 * servitude.
3418 */
3419 break;
3420 case NETDEV_CHANGENAME:
3421 /*
3422 * TODO: handle changing the primary's name
3423 */
3424 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003425 case NETDEV_FEAT_CHANGE:
3426 bond_compute_features(bond);
3427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 default:
3429 break;
3430 }
3431
3432 return NOTIFY_DONE;
3433}
3434
3435/*
3436 * bond_netdev_event: handle netdev notifier chain events.
3437 *
3438 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003439 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 * locks for us to safely manipulate the slave devices (RTNL lock,
3441 * dev_probe_lock).
3442 */
3443static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3444{
3445 struct net_device *event_dev = (struct net_device *)ptr;
3446
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003447 if (dev_net(event_dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003448 return NOTIFY_DONE;
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 dprintk("event_dev: %s, event: %lx\n",
3451 (event_dev ? event_dev->name : "None"),
3452 event);
3453
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003454 if (!(event_dev->priv_flags & IFF_BONDING))
3455 return NOTIFY_DONE;
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (event_dev->flags & IFF_MASTER) {
3458 dprintk("IFF_MASTER\n");
3459 return bond_master_netdev_event(event, event_dev);
3460 }
3461
3462 if (event_dev->flags & IFF_SLAVE) {
3463 dprintk("IFF_SLAVE\n");
3464 return bond_slave_netdev_event(event, event_dev);
3465 }
3466
3467 return NOTIFY_DONE;
3468}
3469
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003470/*
3471 * bond_inetaddr_event: handle inetaddr notifier chain events.
3472 *
3473 * We keep track of device IPs primarily to use as source addresses in
3474 * ARP monitor probes (rather than spewing out broadcasts all the time).
3475 *
3476 * We track one IP for the main device (if it has one), plus one per VLAN.
3477 */
3478static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3479{
3480 struct in_ifaddr *ifa = ptr;
3481 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003482 struct bonding *bond;
3483 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003484
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003485 if (dev_net(ifa->ifa_dev->dev) != &init_net)
Denis V. Lunev6133fb12008-02-28 20:46:17 -08003486 return NOTIFY_DONE;
3487
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003488 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003489 if (bond->dev == event_dev) {
3490 switch (event) {
3491 case NETDEV_UP:
3492 bond->master_ip = ifa->ifa_local;
3493 return NOTIFY_OK;
3494 case NETDEV_DOWN:
3495 bond->master_ip = bond_glean_dev_ip(bond->dev);
3496 return NOTIFY_OK;
3497 default:
3498 return NOTIFY_DONE;
3499 }
3500 }
3501
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003502 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003503 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003504 if (vlan_dev == event_dev) {
3505 switch (event) {
3506 case NETDEV_UP:
3507 vlan->vlan_ip = ifa->ifa_local;
3508 return NOTIFY_OK;
3509 case NETDEV_DOWN:
3510 vlan->vlan_ip =
3511 bond_glean_dev_ip(vlan_dev);
3512 return NOTIFY_OK;
3513 default:
3514 return NOTIFY_DONE;
3515 }
3516 }
3517 }
3518 }
3519 return NOTIFY_DONE;
3520}
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522static struct notifier_block bond_netdev_notifier = {
3523 .notifier_call = bond_netdev_event,
3524};
3525
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003526static struct notifier_block bond_inetaddr_notifier = {
3527 .notifier_call = bond_inetaddr_event,
3528};
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530/*-------------------------- Packet type handling ---------------------------*/
3531
3532/* register to receive lacpdus on a bond */
3533static void bond_register_lacpdu(struct bonding *bond)
3534{
3535 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3536
3537 /* initialize packet type */
3538 pk_type->type = PKT_TYPE_LACPDU;
3539 pk_type->dev = bond->dev;
3540 pk_type->func = bond_3ad_lacpdu_recv;
3541
3542 dev_add_pack(pk_type);
3543}
3544
3545/* unregister to receive lacpdus on a bond */
3546static void bond_unregister_lacpdu(struct bonding *bond)
3547{
3548 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3549}
3550
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003551void bond_register_arp(struct bonding *bond)
3552{
3553 struct packet_type *pt = &bond->arp_mon_pt;
3554
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003555 if (pt->type)
3556 return;
3557
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003558 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003559 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003560 pt->func = bond_arp_rcv;
3561 dev_add_pack(pt);
3562}
3563
3564void bond_unregister_arp(struct bonding *bond)
3565{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003566 struct packet_type *pt = &bond->arp_mon_pt;
3567
3568 dev_remove_pack(pt);
3569 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003570}
3571
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003572/*---------------------------- Hashing Policies -----------------------------*/
3573
3574/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003575 * Hash for the output device based upon layer 2 and layer 3 data. If
3576 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3577 */
3578static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3579 struct net_device *bond_dev, int count)
3580{
3581 struct ethhdr *data = (struct ethhdr *)skb->data;
3582 struct iphdr *iph = ip_hdr(skb);
3583
3584 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3585 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3586 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3587 }
3588
3589 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3590}
3591
3592/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003593 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003594 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3595 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3596 */
3597static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3598 struct net_device *bond_dev, int count)
3599{
3600 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003601 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003602 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003603 int layer4_xor = 0;
3604
3605 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3606 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3607 (iph->protocol == IPPROTO_TCP ||
3608 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003609 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003610 }
3611 return (layer4_xor ^
3612 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3613
3614 }
3615
3616 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3617}
3618
3619/*
3620 * Hash for the output device based upon layer 2 data
3621 */
3622static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3623 struct net_device *bond_dev, int count)
3624{
3625 struct ethhdr *data = (struct ethhdr *)skb->data;
3626
3627 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3628}
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630/*-------------------------- Device entry points ----------------------------*/
3631
3632static int bond_open(struct net_device *bond_dev)
3633{
3634 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636 bond->kill_timers = 0;
3637
3638 if ((bond->params.mode == BOND_MODE_TLB) ||
3639 (bond->params.mode == BOND_MODE_ALB)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 /* bond_alb_initialize must be called before the timer
3641 * is started.
3642 */
3643 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3644 /* something went wrong - fail the open operation */
3645 return -1;
3646 }
3647
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003648 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3649 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651
3652 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003653 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3654 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 }
3656
3657 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003658 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3659 INIT_DELAYED_WORK(&bond->arp_work,
3660 bond_activebackup_arp_mon);
3661 else
3662 INIT_DELAYED_WORK(&bond->arp_work,
3663 bond_loadbalance_arp_mon);
3664
3665 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003666 if (bond->params.arp_validate)
3667 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669
3670 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003671 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003672 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* register to receive LACPDUs */
3674 bond_register_lacpdu(bond);
3675 }
3676
3677 return 0;
3678}
3679
3680static int bond_close(struct net_device *bond_dev)
3681{
3682 struct bonding *bond = bond_dev->priv;
3683
3684 if (bond->params.mode == BOND_MODE_8023AD) {
3685 /* Unregister the receive of LACPDUs */
3686 bond_unregister_lacpdu(bond);
3687 }
3688
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003689 if (bond->params.arp_validate)
3690 bond_unregister_arp(bond);
3691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 write_lock_bh(&bond->lock);
3693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 /* signal timers not to re-arm */
3696 bond->kill_timers = 1;
3697
3698 write_unlock_bh(&bond->lock);
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003701 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 }
3703
3704 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003705 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
3707
3708 switch (bond->params.mode) {
3709 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003710 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 break;
3712 case BOND_MODE_TLB:
3713 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003714 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 break;
3716 default:
3717 break;
3718 }
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 if ((bond->params.mode == BOND_MODE_TLB) ||
3722 (bond->params.mode == BOND_MODE_ALB)) {
3723 /* Must be called only after all
3724 * slaves have been released
3725 */
3726 bond_alb_deinitialize(bond);
3727 }
3728
3729 return 0;
3730}
3731
3732static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3733{
3734 struct bonding *bond = bond_dev->priv;
3735 struct net_device_stats *stats = &(bond->stats), *sstats;
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003736 struct net_device_stats local_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 struct slave *slave;
3738 int i;
3739
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003740 memset(&local_stats, 0, sizeof(struct net_device_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
3742 read_lock_bh(&bond->lock);
3743
3744 bond_for_each_slave(bond, slave, i) {
Rusty Russellc45d2862007-03-28 14:29:08 -07003745 sstats = slave->dev->get_stats(slave->dev);
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003746 local_stats.rx_packets += sstats->rx_packets;
3747 local_stats.rx_bytes += sstats->rx_bytes;
3748 local_stats.rx_errors += sstats->rx_errors;
3749 local_stats.rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003751 local_stats.tx_packets += sstats->tx_packets;
3752 local_stats.tx_bytes += sstats->tx_bytes;
3753 local_stats.tx_errors += sstats->tx_errors;
3754 local_stats.tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003756 local_stats.multicast += sstats->multicast;
3757 local_stats.collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003759 local_stats.rx_length_errors += sstats->rx_length_errors;
3760 local_stats.rx_over_errors += sstats->rx_over_errors;
3761 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3762 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3763 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3764 local_stats.rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003766 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3767 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3768 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3769 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3770 local_stats.tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
3772
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003773 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 read_unlock_bh(&bond->lock);
3776
3777 return stats;
3778}
3779
3780static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3781{
3782 struct net_device *slave_dev = NULL;
3783 struct ifbond k_binfo;
3784 struct ifbond __user *u_binfo = NULL;
3785 struct ifslave k_sinfo;
3786 struct ifslave __user *u_sinfo = NULL;
3787 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 int res = 0;
3789
3790 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3791 bond_dev->name, cmd);
3792
3793 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 case SIOCGMIIPHY:
3795 mii = if_mii(ifr);
3796 if (!mii) {
3797 return -EINVAL;
3798 }
3799 mii->phy_id = 0;
3800 /* Fall Through */
3801 case SIOCGMIIREG:
3802 /*
3803 * We do this again just in case we were called by SIOCGMIIREG
3804 * instead of SIOCGMIIPHY.
3805 */
3806 mii = if_mii(ifr);
3807 if (!mii) {
3808 return -EINVAL;
3809 }
3810
3811 if (mii->reg_num == 1) {
3812 struct bonding *bond = bond_dev->priv;
3813 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003814 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 read_lock(&bond->curr_slave_lock);
Andy Gospodarek4e140072006-12-04 15:04:54 -08003816 if (netif_carrier_ok(bond->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 mii->val_out = BMSR_LSTATUS;
3818 }
3819 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003820 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 }
3822
3823 return 0;
3824 case BOND_INFO_QUERY_OLD:
3825 case SIOCBONDINFOQUERY:
3826 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3827
3828 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3829 return -EFAULT;
3830 }
3831
3832 res = bond_info_query(bond_dev, &k_binfo);
3833 if (res == 0) {
3834 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3835 return -EFAULT;
3836 }
3837 }
3838
3839 return res;
3840 case BOND_SLAVE_INFO_QUERY_OLD:
3841 case SIOCBONDSLAVEINFOQUERY:
3842 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3843
3844 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3845 return -EFAULT;
3846 }
3847
3848 res = bond_slave_info_query(bond_dev, &k_sinfo);
3849 if (res == 0) {
3850 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3851 return -EFAULT;
3852 }
3853 }
3854
3855 return res;
3856 default:
3857 /* Go on */
3858 break;
3859 }
3860
3861 if (!capable(CAP_NET_ADMIN)) {
3862 return -EPERM;
3863 }
3864
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003865 down_write(&(bonding_rwsem));
Eric W. Biederman881d9662007-09-17 11:56:21 -07003866 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 dprintk("slave_dev=%p: \n", slave_dev);
3869
3870 if (!slave_dev) {
3871 res = -ENODEV;
3872 } else {
3873 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3874 switch (cmd) {
3875 case BOND_ENSLAVE_OLD:
3876 case SIOCBONDENSLAVE:
3877 res = bond_enslave(bond_dev, slave_dev);
3878 break;
3879 case BOND_RELEASE_OLD:
3880 case SIOCBONDRELEASE:
3881 res = bond_release(bond_dev, slave_dev);
3882 break;
3883 case BOND_SETHWADDR_OLD:
3884 case SIOCBONDSETHWADDR:
3885 res = bond_sethwaddr(bond_dev, slave_dev);
3886 break;
3887 case BOND_CHANGE_ACTIVE_OLD:
3888 case SIOCBONDCHANGEACTIVE:
3889 res = bond_ioctl_change_active(bond_dev, slave_dev);
3890 break;
3891 default:
3892 res = -EOPNOTSUPP;
3893 }
3894
3895 dev_put(slave_dev);
3896 }
3897
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003898 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return res;
3900}
3901
3902static void bond_set_multicast_list(struct net_device *bond_dev)
3903{
3904 struct bonding *bond = bond_dev->priv;
3905 struct dev_mc_list *dmi;
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 /*
3908 * Do promisc before checking multicast_mode
3909 */
3910 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3911 bond_set_promiscuity(bond, 1);
3912 }
3913
3914 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3915 bond_set_promiscuity(bond, -1);
3916 }
3917
3918 /* set allmulti flag to slaves */
3919 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3920 bond_set_allmulti(bond, 1);
3921 }
3922
3923 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3924 bond_set_allmulti(bond, -1);
3925 }
3926
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003927 read_lock(&bond->lock);
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 bond->flags = bond_dev->flags;
3930
3931 /* looking for addresses to add to slaves' mc list */
3932 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3933 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3934 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3935 }
3936 }
3937
3938 /* looking for addresses to delete from slaves' list */
3939 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3940 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3941 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3942 }
3943 }
3944
3945 /* save master's multicast list */
3946 bond_mc_list_destroy(bond);
3947 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3948
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003949 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
3952/*
3953 * Change the MTU of all of a master's slaves to match the master
3954 */
3955static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3956{
3957 struct bonding *bond = bond_dev->priv;
3958 struct slave *slave, *stop_at;
3959 int res = 0;
3960 int i;
3961
3962 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3963 (bond_dev ? bond_dev->name : "None"), new_mtu);
3964
3965 /* Can't hold bond->lock with bh disabled here since
3966 * some base drivers panic. On the other hand we can't
3967 * hold bond->lock without bh disabled because we'll
3968 * deadlock. The only solution is to rely on the fact
3969 * that we're under rtnl_lock here, and the slaves
3970 * list won't change. This doesn't solve the problem
3971 * of setting the slave's MTU while it is
3972 * transmitting, but the assumption is that the base
3973 * driver can handle that.
3974 *
3975 * TODO: figure out a way to safely iterate the slaves
3976 * list, but without holding a lock around the actual
3977 * call to the base driver.
3978 */
3979
3980 bond_for_each_slave(bond, slave, i) {
3981 dprintk("s %p s->p %p c_m %p\n", slave,
3982 slave->prev, slave->dev->change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 res = dev_set_mtu(slave->dev, new_mtu);
3985
3986 if (res) {
3987 /* If we failed to set the slave's mtu to the new value
3988 * we must abort the operation even in ACTIVE_BACKUP
3989 * mode, because if we allow the backup slaves to have
3990 * different mtu values than the active slave we'll
3991 * need to change their mtu when doing a failover. That
3992 * means changing their mtu from timer context, which
3993 * is probably not a good idea.
3994 */
3995 dprintk("err %d %s\n", res, slave->dev->name);
3996 goto unwind;
3997 }
3998 }
3999
4000 bond_dev->mtu = new_mtu;
4001
4002 return 0;
4003
4004unwind:
4005 /* unwind from head to the slave that failed */
4006 stop_at = slave;
4007 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4008 int tmp_res;
4009
4010 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4011 if (tmp_res) {
4012 dprintk("unwind err %d dev %s\n", tmp_res,
4013 slave->dev->name);
4014 }
4015 }
4016
4017 return res;
4018}
4019
4020/*
4021 * Change HW address
4022 *
4023 * Note that many devices must be down to change the HW address, and
4024 * downing the master releases all slaves. We can make bonds full of
4025 * bonding devices to test this, however.
4026 */
4027static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4028{
4029 struct bonding *bond = bond_dev->priv;
4030 struct sockaddr *sa = addr, tmp_sa;
4031 struct slave *slave, *stop_at;
4032 int res = 0;
4033 int i;
4034
4035 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4036
Jay Vosburghdd957c52007-10-09 19:57:24 -07004037 /*
4038 * If fail_over_mac is enabled, do nothing and return success.
4039 * Returning an error causes ifenslave to fail.
4040 */
4041 if (bond->params.fail_over_mac)
4042 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (!is_valid_ether_addr(sa->sa_data)) {
4045 return -EADDRNOTAVAIL;
4046 }
4047
4048 /* Can't hold bond->lock with bh disabled here since
4049 * some base drivers panic. On the other hand we can't
4050 * hold bond->lock without bh disabled because we'll
4051 * deadlock. The only solution is to rely on the fact
4052 * that we're under rtnl_lock here, and the slaves
4053 * list won't change. This doesn't solve the problem
4054 * of setting the slave's hw address while it is
4055 * transmitting, but the assumption is that the base
4056 * driver can handle that.
4057 *
4058 * TODO: figure out a way to safely iterate the slaves
4059 * list, but without holding a lock around the actual
4060 * call to the base driver.
4061 */
4062
4063 bond_for_each_slave(bond, slave, i) {
4064 dprintk("slave %p %s\n", slave, slave->dev->name);
4065
4066 if (slave->dev->set_mac_address == NULL) {
4067 res = -EOPNOTSUPP;
4068 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4069 goto unwind;
4070 }
4071
4072 res = dev_set_mac_address(slave->dev, addr);
4073 if (res) {
4074 /* TODO: consider downing the slave
4075 * and retry ?
4076 * User should expect communications
4077 * breakage anyway until ARP finish
4078 * updating, so...
4079 */
4080 dprintk("err %d %s\n", res, slave->dev->name);
4081 goto unwind;
4082 }
4083 }
4084
4085 /* success */
4086 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4087 return 0;
4088
4089unwind:
4090 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4091 tmp_sa.sa_family = bond_dev->type;
4092
4093 /* unwind from head to the slave that failed */
4094 stop_at = slave;
4095 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4096 int tmp_res;
4097
4098 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4099 if (tmp_res) {
4100 dprintk("unwind err %d dev %s\n", tmp_res,
4101 slave->dev->name);
4102 }
4103 }
4104
4105 return res;
4106}
4107
4108static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4109{
4110 struct bonding *bond = bond_dev->priv;
4111 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004112 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
4114 read_lock(&bond->lock);
4115
4116 if (!BOND_IS_OK(bond)) {
4117 goto out;
4118 }
4119
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004120 /*
4121 * Concurrent TX may collide on rr_tx_counter; we accept that
4122 * as being rare enough not to justify using an atomic op here
4123 */
4124 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004126 bond_for_each_slave(bond, slave, i) {
4127 slave_no--;
4128 if (slave_no < 0) {
4129 break;
4130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
4132
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004133 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 bond_for_each_slave_from(bond, slave, i, start_at) {
4135 if (IS_UP(slave->dev) &&
4136 (slave->link == BOND_LINK_UP) &&
4137 (slave->state == BOND_STATE_ACTIVE)) {
4138 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 break;
4140 }
4141 }
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143out:
4144 if (res) {
4145 /* no suitable interface, frame not sent */
4146 dev_kfree_skb(skb);
4147 }
4148 read_unlock(&bond->lock);
4149 return 0;
4150}
4151
John W. Linville075897c2005-09-28 17:50:53 -04004152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153/*
4154 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4155 * the bond has a usable interface.
4156 */
4157static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4158{
4159 struct bonding *bond = bond_dev->priv;
4160 int res = 1;
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 read_lock(&bond->lock);
4163 read_lock(&bond->curr_slave_lock);
4164
4165 if (!BOND_IS_OK(bond)) {
4166 goto out;
4167 }
4168
John W. Linville075897c2005-09-28 17:50:53 -04004169 if (!bond->curr_active_slave)
4170 goto out;
4171
John W. Linville075897c2005-09-28 17:50:53 -04004172 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174out:
4175 if (res) {
4176 /* no suitable interface, frame not sent */
4177 dev_kfree_skb(skb);
4178 }
4179 read_unlock(&bond->curr_slave_lock);
4180 read_unlock(&bond->lock);
4181 return 0;
4182}
4183
4184/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004185 * In bond_xmit_xor() , we determine the output device by using a pre-
4186 * determined xmit_hash_policy(), If the selected device is not enabled,
4187 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 */
4189static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4190{
4191 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 struct slave *slave, *start_at;
4193 int slave_no;
4194 int i;
4195 int res = 1;
4196
4197 read_lock(&bond->lock);
4198
4199 if (!BOND_IS_OK(bond)) {
4200 goto out;
4201 }
4202
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004203 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 bond_for_each_slave(bond, slave, i) {
4206 slave_no--;
4207 if (slave_no < 0) {
4208 break;
4209 }
4210 }
4211
4212 start_at = slave;
4213
4214 bond_for_each_slave_from(bond, slave, i, start_at) {
4215 if (IS_UP(slave->dev) &&
4216 (slave->link == BOND_LINK_UP) &&
4217 (slave->state == BOND_STATE_ACTIVE)) {
4218 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4219 break;
4220 }
4221 }
4222
4223out:
4224 if (res) {
4225 /* no suitable interface, frame not sent */
4226 dev_kfree_skb(skb);
4227 }
4228 read_unlock(&bond->lock);
4229 return 0;
4230}
4231
4232/*
4233 * in broadcast mode, we send everything to all usable interfaces.
4234 */
4235static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4236{
4237 struct bonding *bond = bond_dev->priv;
4238 struct slave *slave, *start_at;
4239 struct net_device *tx_dev = NULL;
4240 int i;
4241 int res = 1;
4242
4243 read_lock(&bond->lock);
4244
4245 if (!BOND_IS_OK(bond)) {
4246 goto out;
4247 }
4248
4249 read_lock(&bond->curr_slave_lock);
4250 start_at = bond->curr_active_slave;
4251 read_unlock(&bond->curr_slave_lock);
4252
4253 if (!start_at) {
4254 goto out;
4255 }
4256
4257 bond_for_each_slave_from(bond, slave, i, start_at) {
4258 if (IS_UP(slave->dev) &&
4259 (slave->link == BOND_LINK_UP) &&
4260 (slave->state == BOND_STATE_ACTIVE)) {
4261 if (tx_dev) {
4262 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4263 if (!skb2) {
4264 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004265 ": %s: Error: bond_xmit_broadcast(): "
4266 "skb_clone() failed\n",
4267 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 continue;
4269 }
4270
4271 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4272 if (res) {
4273 dev_kfree_skb(skb2);
4274 continue;
4275 }
4276 }
4277 tx_dev = slave->dev;
4278 }
4279 }
4280
4281 if (tx_dev) {
4282 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4283 }
4284
4285out:
4286 if (res) {
4287 /* no suitable interface, frame not sent */
4288 dev_kfree_skb(skb);
4289 }
4290 /* frame sent to all suitable interfaces */
4291 read_unlock(&bond->lock);
4292 return 0;
4293}
4294
4295/*------------------------- Device initialization ---------------------------*/
4296
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004297static void bond_set_xmit_hash_policy(struct bonding *bond)
4298{
4299 switch (bond->params.xmit_policy) {
4300 case BOND_XMIT_POLICY_LAYER23:
4301 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4302 break;
4303 case BOND_XMIT_POLICY_LAYER34:
4304 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4305 break;
4306 case BOND_XMIT_POLICY_LAYER2:
4307 default:
4308 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4309 break;
4310 }
4311}
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313/*
4314 * set bond mode specific net device operations
4315 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004316void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004318 struct net_device *bond_dev = bond->dev;
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 switch (mode) {
4321 case BOND_MODE_ROUNDROBIN:
4322 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4323 break;
4324 case BOND_MODE_ACTIVEBACKUP:
4325 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4326 break;
4327 case BOND_MODE_XOR:
4328 bond_dev->hard_start_xmit = bond_xmit_xor;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004329 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 break;
4331 case BOND_MODE_BROADCAST:
4332 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4333 break;
4334 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004335 bond_set_master_3ad_flags(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004337 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004340 bond_set_master_alb_flags(bond);
4341 /* FALLTHRU */
4342 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 bond_dev->hard_start_xmit = bond_alb_xmit;
4344 bond_dev->set_mac_address = bond_alb_set_mac_address;
4345 break;
4346 default:
4347 /* Should never happen, mode already checked */
4348 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004349 ": %s: Error: Unknown bonding mode %d\n",
4350 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 mode);
4352 break;
4353 }
4354}
4355
Jay Vosburgh217df672005-09-26 16:11:50 -07004356static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4357 struct ethtool_drvinfo *drvinfo)
4358{
4359 strncpy(drvinfo->driver, DRV_NAME, 32);
4360 strncpy(drvinfo->version, DRV_VERSION, 32);
4361 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4362}
4363
Jeff Garzik7282d492006-09-13 14:30:00 -04004364static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004365 .get_drvinfo = bond_ethtool_get_drvinfo,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004366};
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368/*
4369 * Does not allocate but creates a /proc entry.
4370 * Allowed to fail.
4371 */
Mitch Williams3c535952005-11-09 10:36:11 -08004372static int bond_init(struct net_device *bond_dev, struct bond_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
4374 struct bonding *bond = bond_dev->priv;
4375
4376 dprintk("Begin bond_init for %s\n", bond_dev->name);
4377
4378 /* initialize rwlocks */
4379 rwlock_init(&bond->lock);
4380 rwlock_init(&bond->curr_slave_lock);
4381
4382 bond->params = *params; /* copy params struct */
4383
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004384 bond->wq = create_singlethread_workqueue(bond_dev->name);
4385 if (!bond->wq)
4386 return -ENOMEM;
4387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 /* Initialize pointers */
4389 bond->first_slave = NULL;
4390 bond->curr_active_slave = NULL;
4391 bond->current_arp_slave = NULL;
4392 bond->primary_slave = NULL;
4393 bond->dev = bond_dev;
Moni Shoua1053f622007-10-09 19:43:42 -07004394 bond->send_grat_arp = 0;
Moni Shouad90a1622007-10-09 19:43:43 -07004395 bond->setup_by_slave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 INIT_LIST_HEAD(&bond->vlan_list);
4397
4398 /* Initialize the device entry points */
4399 bond_dev->open = bond_open;
4400 bond_dev->stop = bond_close;
4401 bond_dev->get_stats = bond_get_stats;
4402 bond_dev->do_ioctl = bond_do_ioctl;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004403 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 bond_dev->set_multicast_list = bond_set_multicast_list;
4405 bond_dev->change_mtu = bond_change_mtu;
4406 bond_dev->set_mac_address = bond_set_mac_address;
Jay Vosburgh3a1521b2007-11-06 13:33:29 -08004407 bond_dev->validate_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004409 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 bond_dev->destructor = free_netdev;
4412
4413 /* Initialize the device options */
4414 bond_dev->tx_queue_len = 0;
4415 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004416 bond_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
4418 /* At first, we block adding VLANs. That's the only way to
4419 * prevent problems that occur when adding VLANs over an
4420 * empty bond. The block will be removed once non-challenged
4421 * slaves are enslaved.
4422 */
4423 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4424
Herbert Xu932ff272006-06-09 12:20:56 -07004425 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 * transmitting */
4427 bond_dev->features |= NETIF_F_LLTX;
4428
4429 /* By default, we declare the bond to be fully
4430 * VLAN hardware accelerated capable. Special
4431 * care is taken in the various xmit functions
4432 * when there are slaves that are not hw accel
4433 * capable
4434 */
4435 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4436 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4437 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4438 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4439 NETIF_F_HW_VLAN_RX |
4440 NETIF_F_HW_VLAN_FILTER);
4441
4442#ifdef CONFIG_PROC_FS
4443 bond_create_proc_entry(bond);
4444#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 list_add_tail(&bond->bond_list, &bond_dev_list);
4446
4447 return 0;
4448}
4449
4450/* De-initialize device specific data.
4451 * Caller must hold rtnl_lock.
4452 */
Adrian Bunkc50b85d2007-10-24 18:23:17 +02004453static void bond_deinit(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 struct bonding *bond = bond_dev->priv;
4456
4457 list_del(&bond->bond_list);
4458
4459#ifdef CONFIG_PROC_FS
4460 bond_remove_proc_entry(bond);
4461#endif
4462}
4463
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004464static void bond_work_cancel_all(struct bonding *bond)
4465{
4466 write_lock_bh(&bond->lock);
4467 bond->kill_timers = 1;
4468 write_unlock_bh(&bond->lock);
4469
4470 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4471 cancel_delayed_work(&bond->mii_work);
4472
4473 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4474 cancel_delayed_work(&bond->arp_work);
4475
4476 if (bond->params.mode == BOND_MODE_ALB &&
4477 delayed_work_pending(&bond->alb_work))
4478 cancel_delayed_work(&bond->alb_work);
4479
4480 if (bond->params.mode == BOND_MODE_8023AD &&
4481 delayed_work_pending(&bond->ad_work))
4482 cancel_delayed_work(&bond->ad_work);
4483}
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485/* Unregister and free all bond devices.
4486 * Caller must hold rtnl_lock.
4487 */
4488static void bond_free_all(void)
4489{
4490 struct bonding *bond, *nxt;
4491
4492 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4493 struct net_device *bond_dev = bond->dev;
4494
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004495 bond_work_cancel_all(bond);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004496 netif_tx_lock_bh(bond_dev);
jamal702987052006-09-22 21:54:37 -07004497 bond_mc_list_destroy(bond);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004498 netif_tx_unlock_bh(bond_dev);
jamal702987052006-09-22 21:54:37 -07004499 /* Release the bonded slaves */
4500 bond_release_all(bond_dev);
Libor Pechacek92b41da2008-03-21 22:29:35 -07004501 bond_destroy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
4503
4504#ifdef CONFIG_PROC_FS
4505 bond_destroy_proc_dir();
4506#endif
4507}
4508
4509/*------------------------- Module initialization ---------------------------*/
4510
4511/*
4512 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004513 * number of the mode or its string name. A bit complicated because
4514 * some mode names are substrings of other names, and calls from sysfs
4515 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 */
Jay Vosburghece95f72008-01-17 16:25:01 -08004517int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
Jay Vosburghece95f72008-01-17 16:25:01 -08004519 int mode = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004520 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004521
Jay Vosburgha42e5342008-01-29 18:07:43 -08004522 for (p = (char *)buf; *p; p++)
4523 if (!(isdigit(*p) || isspace(*p)))
4524 break;
4525
4526 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004527 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004528 else
4529 rv = sscanf(buf, "%d", &mode);
4530
4531 if (!rv)
4532 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
4534 for (i = 0; tbl[i].modename; i++) {
Jay Vosburghece95f72008-01-17 16:25:01 -08004535 if (mode == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004537 if (strcmp(modestr, tbl[i].modename) == 0)
4538 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 }
4540
4541 return -1;
4542}
4543
4544static int bond_check_params(struct bond_params *params)
4545{
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004546 int arp_validate_value;
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 /*
4549 * Convert string parameters.
4550 */
4551 if (mode) {
4552 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4553 if (bond_mode == -1) {
4554 printk(KERN_ERR DRV_NAME
4555 ": Error: Invalid bonding mode \"%s\"\n",
4556 mode == NULL ? "NULL" : mode);
4557 return -EINVAL;
4558 }
4559 }
4560
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004561 if (xmit_hash_policy) {
4562 if ((bond_mode != BOND_MODE_XOR) &&
4563 (bond_mode != BOND_MODE_8023AD)) {
4564 printk(KERN_INFO DRV_NAME
4565 ": xor_mode param is irrelevant in mode %s\n",
4566 bond_mode_name(bond_mode));
4567 } else {
4568 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4569 xmit_hashtype_tbl);
4570 if (xmit_hashtype == -1) {
4571 printk(KERN_ERR DRV_NAME
4572 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4573 xmit_hash_policy == NULL ? "NULL" :
4574 xmit_hash_policy);
4575 return -EINVAL;
4576 }
4577 }
4578 }
4579
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 if (lacp_rate) {
4581 if (bond_mode != BOND_MODE_8023AD) {
4582 printk(KERN_INFO DRV_NAME
4583 ": lacp_rate param is irrelevant in mode %s\n",
4584 bond_mode_name(bond_mode));
4585 } else {
4586 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4587 if (lacp_fast == -1) {
4588 printk(KERN_ERR DRV_NAME
4589 ": Error: Invalid lacp rate \"%s\"\n",
4590 lacp_rate == NULL ? "NULL" : lacp_rate);
4591 return -EINVAL;
4592 }
4593 }
4594 }
4595
4596 if (max_bonds < 1 || max_bonds > INT_MAX) {
4597 printk(KERN_WARNING DRV_NAME
4598 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004599 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4601 max_bonds = BOND_DEFAULT_MAX_BONDS;
4602 }
4603
4604 if (miimon < 0) {
4605 printk(KERN_WARNING DRV_NAME
4606 ": Warning: miimon module parameter (%d), "
4607 "not in range 0-%d, so it was reset to %d\n",
4608 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4609 miimon = BOND_LINK_MON_INTERV;
4610 }
4611
4612 if (updelay < 0) {
4613 printk(KERN_WARNING DRV_NAME
4614 ": Warning: updelay module parameter (%d), "
4615 "not in range 0-%d, so it was reset to 0\n",
4616 updelay, INT_MAX);
4617 updelay = 0;
4618 }
4619
4620 if (downdelay < 0) {
4621 printk(KERN_WARNING DRV_NAME
4622 ": Warning: downdelay module parameter (%d), "
4623 "not in range 0-%d, so it was reset to 0\n",
4624 downdelay, INT_MAX);
4625 downdelay = 0;
4626 }
4627
4628 if ((use_carrier != 0) && (use_carrier != 1)) {
4629 printk(KERN_WARNING DRV_NAME
4630 ": Warning: use_carrier module parameter (%d), "
4631 "not of valid value (0/1), so it was set to 1\n",
4632 use_carrier);
4633 use_carrier = 1;
4634 }
4635
Moni Shoua7893b242008-05-17 21:10:12 -07004636 if (num_grat_arp < 0 || num_grat_arp > 255) {
4637 printk(KERN_WARNING DRV_NAME
4638 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4639 "was reset to 1 \n", num_grat_arp);
4640 num_grat_arp = 1;
4641 }
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 /* reset values for 802.3ad */
4644 if (bond_mode == BOND_MODE_8023AD) {
4645 if (!miimon) {
4646 printk(KERN_WARNING DRV_NAME
4647 ": Warning: miimon must be specified, "
4648 "otherwise bonding will not detect link "
4649 "failure, speed and duplex which are "
4650 "essential for 802.3ad operation\n");
4651 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4652 miimon = 100;
4653 }
4654 }
4655
4656 /* reset values for TLB/ALB */
4657 if ((bond_mode == BOND_MODE_TLB) ||
4658 (bond_mode == BOND_MODE_ALB)) {
4659 if (!miimon) {
4660 printk(KERN_WARNING DRV_NAME
4661 ": Warning: miimon must be specified, "
4662 "otherwise bonding will not detect link "
4663 "failure and link speed which are essential "
4664 "for TLB/ALB load balancing\n");
4665 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4666 miimon = 100;
4667 }
4668 }
4669
4670 if (bond_mode == BOND_MODE_ALB) {
4671 printk(KERN_NOTICE DRV_NAME
4672 ": In ALB mode you might experience client "
4673 "disconnections upon reconnection of a link if the "
4674 "bonding module updelay parameter (%d msec) is "
4675 "incompatible with the forwarding delay time of the "
4676 "switch\n",
4677 updelay);
4678 }
4679
4680 if (!miimon) {
4681 if (updelay || downdelay) {
4682 /* just warn the user the up/down delay will have
4683 * no effect since miimon is zero...
4684 */
4685 printk(KERN_WARNING DRV_NAME
4686 ": Warning: miimon module parameter not set "
4687 "and updelay (%d) or downdelay (%d) module "
4688 "parameter is set; updelay and downdelay have "
4689 "no effect unless miimon is set\n",
4690 updelay, downdelay);
4691 }
4692 } else {
4693 /* don't allow arp monitoring */
4694 if (arp_interval) {
4695 printk(KERN_WARNING DRV_NAME
4696 ": Warning: miimon (%d) and arp_interval (%d) "
4697 "can't be used simultaneously, disabling ARP "
4698 "monitoring\n",
4699 miimon, arp_interval);
4700 arp_interval = 0;
4701 }
4702
4703 if ((updelay % miimon) != 0) {
4704 printk(KERN_WARNING DRV_NAME
4705 ": Warning: updelay (%d) is not a multiple "
4706 "of miimon (%d), updelay rounded to %d ms\n",
4707 updelay, miimon, (updelay / miimon) * miimon);
4708 }
4709
4710 updelay /= miimon;
4711
4712 if ((downdelay % miimon) != 0) {
4713 printk(KERN_WARNING DRV_NAME
4714 ": Warning: downdelay (%d) is not a multiple "
4715 "of miimon (%d), downdelay rounded to %d ms\n",
4716 downdelay, miimon,
4717 (downdelay / miimon) * miimon);
4718 }
4719
4720 downdelay /= miimon;
4721 }
4722
4723 if (arp_interval < 0) {
4724 printk(KERN_WARNING DRV_NAME
4725 ": Warning: arp_interval module parameter (%d) "
4726 ", not in range 0-%d, so it was reset to %d\n",
4727 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4728 arp_interval = BOND_LINK_ARP_INTERV;
4729 }
4730
4731 for (arp_ip_count = 0;
4732 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4733 arp_ip_count++) {
4734 /* not complete check, but should be good enough to
4735 catch mistakes */
4736 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4737 printk(KERN_WARNING DRV_NAME
4738 ": Warning: bad arp_ip_target module parameter "
4739 "(%s), ARP monitoring will not be performed\n",
4740 arp_ip_target[arp_ip_count]);
4741 arp_interval = 0;
4742 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004743 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 arp_target[arp_ip_count] = ip;
4745 }
4746 }
4747
4748 if (arp_interval && !arp_ip_count) {
4749 /* don't allow arping if no arp_ip_target given... */
4750 printk(KERN_WARNING DRV_NAME
4751 ": Warning: arp_interval module parameter (%d) "
4752 "specified without providing an arp_ip_target "
4753 "parameter, arp_interval was reset to 0\n",
4754 arp_interval);
4755 arp_interval = 0;
4756 }
4757
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004758 if (arp_validate) {
4759 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4760 printk(KERN_ERR DRV_NAME
4761 ": arp_validate only supported in active-backup mode\n");
4762 return -EINVAL;
4763 }
4764 if (!arp_interval) {
4765 printk(KERN_ERR DRV_NAME
4766 ": arp_validate requires arp_interval\n");
4767 return -EINVAL;
4768 }
4769
4770 arp_validate_value = bond_parse_parm(arp_validate,
4771 arp_validate_tbl);
4772 if (arp_validate_value == -1) {
4773 printk(KERN_ERR DRV_NAME
4774 ": Error: invalid arp_validate \"%s\"\n",
4775 arp_validate == NULL ? "NULL" : arp_validate);
4776 return -EINVAL;
4777 }
4778 } else
4779 arp_validate_value = 0;
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (miimon) {
4782 printk(KERN_INFO DRV_NAME
4783 ": MII link monitoring set to %d ms\n",
4784 miimon);
4785 } else if (arp_interval) {
4786 int i;
4787
4788 printk(KERN_INFO DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004789 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4790 arp_interval,
4791 arp_validate_tbl[arp_validate_value].modename,
4792 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 for (i = 0; i < arp_ip_count; i++)
4795 printk (" %s", arp_ip_target[i]);
4796
4797 printk("\n");
4798
4799 } else {
4800 /* miimon and arp_interval not set, we need one so things
4801 * work as expected, see bonding.txt for details
4802 */
4803 printk(KERN_WARNING DRV_NAME
4804 ": Warning: either miimon or arp_interval and "
4805 "arp_ip_target module parameters must be specified, "
4806 "otherwise bonding will not detect link failures! see "
4807 "bonding.txt for details.\n");
4808 }
4809
4810 if (primary && !USES_PRIMARY(bond_mode)) {
4811 /* currently, using a primary only makes sense
4812 * in active backup, TLB or ALB modes
4813 */
4814 printk(KERN_WARNING DRV_NAME
4815 ": Warning: %s primary device specified but has no "
4816 "effect in %s mode\n",
4817 primary, bond_mode_name(bond_mode));
4818 primary = NULL;
4819 }
4820
Jay Vosburghdd957c52007-10-09 19:57:24 -07004821 if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
4822 printk(KERN_WARNING DRV_NAME
4823 ": Warning: fail_over_mac only affects "
4824 "active-backup mode.\n");
4825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 /* fill params struct with the proper values */
4827 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004828 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07004830 params->num_grat_arp = num_grat_arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004832 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 params->updelay = updelay;
4834 params->downdelay = downdelay;
4835 params->use_carrier = use_carrier;
4836 params->lacp_fast = lacp_fast;
4837 params->primary[0] = 0;
Jay Vosburghdd957c52007-10-09 19:57:24 -07004838 params->fail_over_mac = fail_over_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 if (primary) {
4841 strncpy(params->primary, primary, IFNAMSIZ);
4842 params->primary[IFNAMSIZ - 1] = 0;
4843 }
4844
4845 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4846
4847 return 0;
4848}
4849
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004850static struct lock_class_key bonding_netdev_xmit_lock_key;
4851
Mitch Williamsdfe60392005-11-09 10:36:04 -08004852/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004853 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004854 * Caller must NOT hold rtnl_lock; we need to release it here before we
4855 * set up our sysfs entries.
4856 */
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -07004857int bond_create(char *name, struct bond_params *params)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004858{
4859 struct net_device *bond_dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07004860 struct bonding *bond;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004861 int res;
4862
4863 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004864 down_write(&bonding_rwsem);
4865
4866 /* Check to see if the bond already exists. */
Jay Vosburgh4fe47632008-01-29 18:07:45 -08004867 if (name) {
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07004868 list_for_each_entry(bond, &bond_dev_list, bond_list)
Jay Vosburgh4fe47632008-01-29 18:07:45 -08004869 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
4870 printk(KERN_ERR DRV_NAME
Jay Vosburgh027ea042008-01-17 16:25:02 -08004871 ": cannot add bond %s; it already exists\n",
Jay Vosburgh4fe47632008-01-29 18:07:45 -08004872 name);
4873 res = -EPERM;
4874 goto out_rtnl;
4875 }
4876 }
Jay Vosburgh027ea042008-01-17 16:25:02 -08004877
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004878 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4879 ether_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004880 if (!bond_dev) {
4881 printk(KERN_ERR DRV_NAME
4882 ": %s: eek! can't alloc netdev!\n",
4883 name);
4884 res = -ENOMEM;
4885 goto out_rtnl;
4886 }
4887
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004888 if (!name) {
4889 res = dev_alloc_name(bond_dev, "bond%d");
4890 if (res < 0)
4891 goto out_netdev;
4892 }
4893
Mitch Williamsdfe60392005-11-09 10:36:04 -08004894 /* bond_init() must be called after dev_alloc_name() (for the
4895 * /proc files), but before register_netdevice(), because we
4896 * need to set function pointers.
4897 */
4898
4899 res = bond_init(bond_dev, params);
4900 if (res < 0) {
4901 goto out_netdev;
4902 }
4903
Mitch Williamsdfe60392005-11-09 10:36:04 -08004904 res = register_netdevice(bond_dev);
4905 if (res < 0) {
4906 goto out_bond;
4907 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004908
4909 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4910
Jay Vosburghff59c452006-03-27 13:27:43 -08004911 netif_carrier_off(bond_dev);
4912
Jay Vosburgh027ea042008-01-17 16:25:02 -08004913 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004914 rtnl_unlock(); /* allows sysfs registration of net device */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004915 res = bond_create_sysfs_entry(bond_dev->priv);
Jay Vosburgh09c89272007-01-19 18:15:38 -08004916 if (res < 0) {
4917 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004918 down_write(&bonding_rwsem);
Pavel Emelyanov822973b2008-05-02 17:49:37 -07004919 bond_deinit(bond_dev);
4920 unregister_netdevice(bond_dev);
4921 goto out_rtnl;
Jay Vosburgh09c89272007-01-19 18:15:38 -08004922 }
4923
4924 return 0;
4925
Mitch Williamsdfe60392005-11-09 10:36:04 -08004926out_bond:
4927 bond_deinit(bond_dev);
4928out_netdev:
4929 free_netdev(bond_dev);
4930out_rtnl:
Jay Vosburgh027ea042008-01-17 16:25:02 -08004931 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004932 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004933 return res;
4934}
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936static int __init bonding_init(void)
4937{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 int i;
4939 int res;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07004940 struct bonding *bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
4942 printk(KERN_INFO "%s", version);
4943
Mitch Williamsdfe60392005-11-09 10:36:04 -08004944 res = bond_check_params(&bonding_defaults);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (res) {
Mitch Williamsdfe60392005-11-09 10:36:04 -08004946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949#ifdef CONFIG_PROC_FS
4950 bond_create_proc_dir();
4951#endif
Jay Vosburgh027ea042008-01-17 16:25:02 -08004952
4953 init_rwsem(&bonding_rwsem);
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 for (i = 0; i < max_bonds; i++) {
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -07004956 res = bond_create(NULL, &bonding_defaults);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004957 if (res)
4958 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
4960
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004961 res = bond_create_sysfs();
4962 if (res)
4963 goto err;
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004966 register_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Mitch Williamsdfe60392005-11-09 10:36:04 -08004968 goto out;
4969err:
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07004970 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004971 bond_work_cancel_all(bond);
4972 destroy_workqueue(bond->wq);
4973 }
4974
Pavel Emelyanovae68c392008-05-02 17:49:39 -07004975 bond_destroy_sysfs();
4976
Florin Malita40abc272005-09-18 00:24:12 -07004977 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004980out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
4985static void __exit bonding_exit(void)
4986{
4987 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004988 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Pavel Emelyanovae68c392008-05-02 17:49:39 -07004990 bond_destroy_sysfs();
4991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 rtnl_lock();
4993 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 rtnl_unlock();
4995}
4996
4997module_init(bonding_init);
4998module_exit(bonding_exit);
4999MODULE_LICENSE("GPL");
5000MODULE_VERSION(DRV_VERSION);
5001MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5002MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5003MODULE_SUPPORTED_DEVICE("most ethernet devices");
5004
5005/*
5006 * Local variables:
5007 * c-indent-level: 8
5008 * c-basic-offset: 8
5009 * tab-width: 8
5010 * End:
5011 */
5012