blob: 798d98ce2d977db787586284ac15b8193365f1fb [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;
Brian Haley305d5522008-11-04 17:51:14 -080092static int num_unsol_na = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int miimon = BOND_LINK_MON_INTERV;
94static int updelay = 0;
95static int downdelay = 0;
96static int use_carrier = 1;
97static char *mode = NULL;
98static char *primary = NULL;
99static char *lacp_rate = NULL;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400100static char *xmit_hash_policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int arp_interval = BOND_LINK_ARP_INTERV;
102static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700103static char *arp_validate = NULL;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700104static char *fail_over_mac = NULL;
Mitch Williams12479f92005-11-09 10:35:44 -0800105struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107module_param(max_bonds, int, 0);
108MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Moni Shoua7893b242008-05-17 21:10:12 -0700109module_param(num_grat_arp, int, 0644);
110MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800111module_param(num_unsol_na, int, 0644);
112MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(miimon, int, 0);
114MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
115module_param(updelay, int, 0);
116MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
117module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800118MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
119 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800121MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
122 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800124MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
125 "1 for active-backup, 2 for balance-xor, "
126 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
127 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(primary, charp, 0);
129MODULE_PARM_DESC(primary, "Primary network device to use");
130module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800131MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
132 "(slow/fast)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400133module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800134MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
135 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136module_param(arp_interval, int, 0);
137MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
138module_param_array(arp_ip_target, charp, NULL, 0);
139MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700140module_param(arp_validate, charp, 0);
141MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700142module_param(fail_over_mac, charp, 0);
143MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/*----------------------------- Global variables ----------------------------*/
146
Arjan van de Venf71e1302006-03-03 21:33:57 -0500147static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
149
Mitch Williams12479f92005-11-09 10:35:44 -0800150LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#ifdef CONFIG_PROC_FS
153static struct proc_dir_entry *bond_proc_dir = NULL;
154#endif
155
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800156extern struct rw_semaphore bonding_rwsem;
Al Virod3bb52b2007-08-22 20:06:58 -0400157static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int arp_ip_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int bond_mode = BOND_MODE_ROUNDROBIN;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400160static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int lacp_fast = 0;
Jay Vosburgh217df672005-09-26 16:11:50 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Mitch Williams12479f92005-11-09 10:35:44 -0800164struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{ "slow", AD_LACP_SLOW},
166{ "fast", AD_LACP_FAST},
167{ NULL, -1},
168};
169
Mitch Williams12479f92005-11-09 10:35:44 -0800170struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{ "balance-rr", BOND_MODE_ROUNDROBIN},
172{ "active-backup", BOND_MODE_ACTIVEBACKUP},
173{ "balance-xor", BOND_MODE_XOR},
174{ "broadcast", BOND_MODE_BROADCAST},
175{ "802.3ad", BOND_MODE_8023AD},
176{ "balance-tlb", BOND_MODE_TLB},
177{ "balance-alb", BOND_MODE_ALB},
178{ NULL, -1},
179};
180
Mitch Williams12479f92005-11-09 10:35:44 -0800181struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400182{ "layer2", BOND_XMIT_POLICY_LAYER2},
183{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800184{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400185{ NULL, -1},
186};
187
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700188struct bond_parm_tbl arp_validate_tbl[] = {
189{ "none", BOND_ARP_VALIDATE_NONE},
190{ "active", BOND_ARP_VALIDATE_ACTIVE},
191{ "backup", BOND_ARP_VALIDATE_BACKUP},
192{ "all", BOND_ARP_VALIDATE_ALL},
193{ NULL, -1},
194};
195
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700196struct bond_parm_tbl fail_over_mac_tbl[] = {
197{ "none", BOND_FOM_NONE},
198{ "active", BOND_FOM_ACTIVE},
199{ "follow", BOND_FOM_FOLLOW},
200{ NULL, -1},
201};
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*-------------------------- Forward declarations ---------------------------*/
204
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400205static void bond_send_gratuitous_arp(struct bonding *bond);
Adrian Bunkc50b85d2007-10-24 18:23:17 +0200206static void bond_deinit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/*---------------------------- General routines -----------------------------*/
209
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700210static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 switch (mode) {
213 case BOND_MODE_ROUNDROBIN :
214 return "load balancing (round-robin)";
215 case BOND_MODE_ACTIVEBACKUP :
216 return "fault-tolerance (active-backup)";
217 case BOND_MODE_XOR :
218 return "load balancing (xor)";
219 case BOND_MODE_BROADCAST :
220 return "fault-tolerance (broadcast)";
221 case BOND_MODE_8023AD:
222 return "IEEE 802.3ad Dynamic link aggregation";
223 case BOND_MODE_TLB:
224 return "transmit load balancing";
225 case BOND_MODE_ALB:
226 return "adaptive load balancing";
227 default:
228 return "unknown";
229 }
230}
231
232/*---------------------------------- VLAN -----------------------------------*/
233
234/**
235 * bond_add_vlan - add a new vlan id on bond
236 * @bond: bond that got the notification
237 * @vlan_id: the vlan id to add
238 *
239 * Returns -ENOMEM if allocation failed.
240 */
241static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
242{
243 struct vlan_entry *vlan;
244
245 dprintk("bond: %s, vlan id %d\n",
246 (bond ? bond->dev->name: "None"), vlan_id);
247
Brian Haley305d5522008-11-04 17:51:14 -0800248 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!vlan) {
250 return -ENOMEM;
251 }
252
253 INIT_LIST_HEAD(&vlan->vlan_list);
254 vlan->vlan_id = vlan_id;
255
256 write_lock_bh(&bond->lock);
257
258 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
259
260 write_unlock_bh(&bond->lock);
261
262 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
263
264 return 0;
265}
266
267/**
268 * bond_del_vlan - delete a vlan id from bond
269 * @bond: bond that got the notification
270 * @vlan_id: the vlan id to delete
271 *
272 * returns -ENODEV if @vlan_id was not found in @bond.
273 */
274static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
275{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700276 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int res = -ENODEV;
278
279 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
280
281 write_lock_bh(&bond->lock);
282
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700283 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (vlan->vlan_id == vlan_id) {
285 list_del(&vlan->vlan_list);
286
287 if ((bond->params.mode == BOND_MODE_TLB) ||
288 (bond->params.mode == BOND_MODE_ALB)) {
289 bond_alb_clear_vlan(bond, vlan_id);
290 }
291
292 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
293 bond->dev->name);
294
295 kfree(vlan);
296
297 if (list_empty(&bond->vlan_list) &&
298 (bond->slave_cnt == 0)) {
299 /* Last VLAN removed and no slaves, so
300 * restore block on adding VLANs. This will
301 * be removed once new slaves that are not
302 * VLAN challenged will be added.
303 */
304 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
305 }
306
307 res = 0;
308 goto out;
309 }
310 }
311
312 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
313 bond->dev->name);
314
315out:
316 write_unlock_bh(&bond->lock);
317 return res;
318}
319
320/**
321 * bond_has_challenged_slaves
322 * @bond: the bond we're working on
323 *
324 * Searches the slave list. Returns 1 if a vlan challenged slave
325 * was found, 0 otherwise.
326 *
327 * Assumes bond->lock is held.
328 */
329static int bond_has_challenged_slaves(struct bonding *bond)
330{
331 struct slave *slave;
332 int i;
333
334 bond_for_each_slave(bond, slave, i) {
335 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
336 dprintk("found VLAN challenged slave - %s\n",
337 slave->dev->name);
338 return 1;
339 }
340 }
341
342 dprintk("no VLAN challenged slaves found\n");
343 return 0;
344}
345
346/**
347 * bond_next_vlan - safely skip to the next item in the vlans list.
348 * @bond: the bond we're working on
349 * @curr: item we're advancing from
350 *
351 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
352 * or @curr->next otherwise (even if it is @curr itself again).
353 *
354 * Caller must hold bond->lock
355 */
356struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
357{
358 struct vlan_entry *next, *last;
359
360 if (list_empty(&bond->vlan_list)) {
361 return NULL;
362 }
363
364 if (!curr) {
365 next = list_entry(bond->vlan_list.next,
366 struct vlan_entry, vlan_list);
367 } else {
368 last = list_entry(bond->vlan_list.prev,
369 struct vlan_entry, vlan_list);
370 if (last == curr) {
371 next = list_entry(bond->vlan_list.next,
372 struct vlan_entry, vlan_list);
373 } else {
374 next = list_entry(curr->vlan_list.next,
375 struct vlan_entry, vlan_list);
376 }
377 }
378
379 return next;
380}
381
382/**
383 * bond_dev_queue_xmit - Prepare skb for xmit.
384 *
385 * @bond: bond device that got this skb for tx.
386 * @skb: hw accel VLAN tagged skb to transmit
387 * @slave_dev: slave that is supposed to xmit this skbuff
388 *
389 * When the bond gets an skb to transmit that is
390 * already hardware accelerated VLAN tagged, and it
391 * needs to relay this skb to a slave that is not
392 * hw accel capable, the skb needs to be "unaccelerated",
393 * i.e. strip the hwaccel tag and re-insert it as part
394 * of the payload.
395 */
396int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
397{
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700398 unsigned short uninitialized_var(vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (!list_empty(&bond->vlan_list) &&
401 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
402 vlan_get_tag(skb, &vlan_id) == 0) {
403 skb->dev = slave_dev;
404 skb = vlan_put_tag(skb, vlan_id);
405 if (!skb) {
406 /* vlan_put_tag() frees the skb in case of error,
407 * so return success here so the calling functions
408 * won't attempt to free is again.
409 */
410 return 0;
411 }
412 } else {
413 skb->dev = slave_dev;
414 }
415
416 skb->priority = 1;
417 dev_queue_xmit(skb);
418
419 return 0;
420}
421
422/*
423 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
424 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
425 * lock because:
426 * a. This operation is performed in IOCTL context,
427 * b. The operation is protected by the RTNL semaphore in the 8021q code,
428 * c. Holding a lock with BH disabled while directly calling a base driver
429 * entry point is generally a BAD idea.
430 *
431 * The design of synchronization/protection for this operation in the 8021q
432 * module is good for one or more VLAN devices over a single physical device
433 * and cannot be extended for a teaming solution like bonding, so there is a
434 * potential race condition here where a net device from the vlan group might
435 * be referenced (either by a base driver or the 8021q code) while it is being
436 * removed from the system. However, it turns out we're not making matters
437 * worse, and if it works for regular VLAN usage it will work here too.
438*/
439
440/**
441 * bond_vlan_rx_register - Propagates registration to slaves
442 * @bond_dev: bonding net device that got called
443 * @grp: vlan group being registered
444 */
445static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
446{
447 struct bonding *bond = bond_dev->priv;
448 struct slave *slave;
449 int i;
450
451 bond->vlgrp = grp;
452
453 bond_for_each_slave(bond, slave, i) {
454 struct net_device *slave_dev = slave->dev;
455
456 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
457 slave_dev->vlan_rx_register) {
458 slave_dev->vlan_rx_register(slave_dev, grp);
459 }
460 }
461}
462
463/**
464 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
465 * @bond_dev: bonding net device that got called
466 * @vid: vlan id being added
467 */
468static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
469{
470 struct bonding *bond = bond_dev->priv;
471 struct slave *slave;
472 int i, res;
473
474 bond_for_each_slave(bond, slave, i) {
475 struct net_device *slave_dev = slave->dev;
476
477 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
478 slave_dev->vlan_rx_add_vid) {
479 slave_dev->vlan_rx_add_vid(slave_dev, vid);
480 }
481 }
482
483 res = bond_add_vlan(bond, vid);
484 if (res) {
485 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800486 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 bond_dev->name, vid);
488 }
489}
490
491/**
492 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
493 * @bond_dev: bonding net device that got called
494 * @vid: vlan id being removed
495 */
496static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
497{
498 struct bonding *bond = bond_dev->priv;
499 struct slave *slave;
500 struct net_device *vlan_dev;
501 int i, res;
502
503 bond_for_each_slave(bond, slave, i) {
504 struct net_device *slave_dev = slave->dev;
505
506 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
507 slave_dev->vlan_rx_kill_vid) {
508 /* Save and then restore vlan_dev in the grp array,
509 * since the slave's driver might clear it.
510 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800511 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800513 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 }
516
517 res = bond_del_vlan(bond, vid);
518 if (res) {
519 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800520 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 bond_dev->name, vid);
522 }
523}
524
525static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
526{
527 struct vlan_entry *vlan;
528
529 write_lock_bh(&bond->lock);
530
531 if (list_empty(&bond->vlan_list)) {
532 goto out;
533 }
534
535 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
536 slave_dev->vlan_rx_register) {
537 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
538 }
539
540 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
541 !(slave_dev->vlan_rx_add_vid)) {
542 goto out;
543 }
544
545 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
546 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
547 }
548
549out:
550 write_unlock_bh(&bond->lock);
551}
552
553static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
554{
555 struct vlan_entry *vlan;
556 struct net_device *vlan_dev;
557
558 write_lock_bh(&bond->lock);
559
560 if (list_empty(&bond->vlan_list)) {
561 goto out;
562 }
563
564 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
565 !(slave_dev->vlan_rx_kill_vid)) {
566 goto unreg;
567 }
568
569 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
570 /* Save and then restore vlan_dev in the grp array,
571 * since the slave's driver might clear it.
572 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800573 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800575 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578unreg:
579 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
580 slave_dev->vlan_rx_register) {
581 slave_dev->vlan_rx_register(slave_dev, NULL);
582 }
583
584out:
585 write_unlock_bh(&bond->lock);
586}
587
588/*------------------------------- Link status -------------------------------*/
589
590/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800591 * Set the carrier state for the master according to the state of its
592 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
593 * do special 802.3ad magic.
594 *
595 * Returns zero if carrier state does not change, nonzero if it does.
596 */
597static int bond_set_carrier(struct bonding *bond)
598{
599 struct slave *slave;
600 int i;
601
602 if (bond->slave_cnt == 0)
603 goto down;
604
605 if (bond->params.mode == BOND_MODE_8023AD)
606 return bond_3ad_set_carrier(bond);
607
608 bond_for_each_slave(bond, slave, i) {
609 if (slave->link == BOND_LINK_UP) {
610 if (!netif_carrier_ok(bond->dev)) {
611 netif_carrier_on(bond->dev);
612 return 1;
613 }
614 return 0;
615 }
616 }
617
618down:
619 if (netif_carrier_ok(bond->dev)) {
620 netif_carrier_off(bond->dev);
621 return 1;
622 }
623 return 0;
624}
625
626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * Get link speed and duplex from the slave's base driver
628 * using ethtool. If for some reason the call fails or the
629 * values are invalid, fake speed and duplex to 100/Full
630 * and return error.
631 */
632static int bond_update_speed_duplex(struct slave *slave)
633{
634 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700636 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* Fake speed and duplex */
639 slave->speed = SPEED_100;
640 slave->duplex = DUPLEX_FULL;
641
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700642 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700645 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
646 if (res < 0)
647 return -1;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 switch (etool.speed) {
650 case SPEED_10:
651 case SPEED_100:
652 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700653 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 break;
655 default:
656 return -1;
657 }
658
659 switch (etool.duplex) {
660 case DUPLEX_FULL:
661 case DUPLEX_HALF:
662 break;
663 default:
664 return -1;
665 }
666
667 slave->speed = etool.speed;
668 slave->duplex = etool.duplex;
669
670 return 0;
671}
672
673/*
674 * if <dev> supports MII link status reporting, check its link status.
675 *
676 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
677 * depening upon the setting of the use_carrier parameter.
678 *
679 * Return either BMSR_LSTATUS, meaning that the link is up (or we
680 * can't tell and just pretend it is), or 0, meaning that the link is
681 * down.
682 *
683 * If reporting is non-zero, instead of faking link up, return -1 if
684 * both ETHTOOL and MII ioctls fail (meaning the device does not
685 * support them). If use_carrier is set, return whatever it says.
686 * It'd be nice if there was a good way to tell if a driver supports
687 * netif_carrier, but there really isn't.
688 */
689static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
690{
691 static int (* ioctl)(struct net_device *, struct ifreq *, int);
692 struct ifreq ifr;
693 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (bond->params.use_carrier) {
696 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
697 }
698
699 ioctl = slave_dev->do_ioctl;
700 if (ioctl) {
701 /* TODO: set pointer to correct ioctl on a per team member */
702 /* bases to make this more efficient. that is, once */
703 /* we determine the correct ioctl, we will always */
704 /* call it and not the others for that team */
705 /* member. */
706
707 /*
708 * We cannot assume that SIOCGMIIPHY will also read a
709 * register; not all network drivers (e.g., e100)
710 * support that.
711 */
712
713 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
714 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
715 mii = if_mii(&ifr);
716 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
717 mii->reg_num = MII_BMSR;
718 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
719 return (mii->val_out & BMSR_LSTATUS);
720 }
721 }
722 }
723
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700724 /*
725 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
726 * attempt to get link status from it if the above MII ioctls fail.
727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (slave_dev->ethtool_ops) {
729 if (slave_dev->ethtool_ops->get_link) {
730 u32 link;
731
732 link = slave_dev->ethtool_ops->get_link(slave_dev);
733
734 return link ? BMSR_LSTATUS : 0;
735 }
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700740 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * cannot report link status). If not reporting, pretend
742 * we're ok.
743 */
744 return (reporting ? -1 : BMSR_LSTATUS);
745}
746
747/*----------------------------- Multicast list ------------------------------*/
748
749/*
750 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
751 */
752static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
753{
754 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
755 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
756}
757
758/*
759 * returns dmi entry if found, NULL otherwise
760 */
761static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
762{
763 struct dev_mc_list *idmi;
764
765 for (idmi = mc_list; idmi; idmi = idmi->next) {
766 if (bond_is_dmi_same(dmi, idmi)) {
767 return idmi;
768 }
769 }
770
771 return NULL;
772}
773
774/*
775 * Push the promiscuity flag down to appropriate slaves
776 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700777static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700779 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700783 err = dev_set_promiscuity(bond->curr_active_slave->dev,
784 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 } else {
787 struct slave *slave;
788 int i;
789 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700790 err = dev_set_promiscuity(slave->dev, inc);
791 if (err)
792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700795 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798/*
799 * Push the allmulti flag down to all slaves
800 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700801static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700803 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700807 err = dev_set_allmulti(bond->curr_active_slave->dev,
808 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 } else {
811 struct slave *slave;
812 int i;
813 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700814 err = dev_set_allmulti(slave->dev, inc);
815 if (err)
816 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700819 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822/*
823 * Add a Multicast address to slaves
824 * according to mode
825 */
826static void bond_mc_add(struct bonding *bond, void *addr, int alen)
827{
828 if (USES_PRIMARY(bond->params.mode)) {
829 /* write lock already acquired */
830 if (bond->curr_active_slave) {
831 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
832 }
833 } else {
834 struct slave *slave;
835 int i;
836 bond_for_each_slave(bond, slave, i) {
837 dev_mc_add(slave->dev, addr, alen, 0);
838 }
839 }
840}
841
842/*
843 * Remove a multicast address from slave
844 * according to mode
845 */
846static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
847{
848 if (USES_PRIMARY(bond->params.mode)) {
849 /* write lock already acquired */
850 if (bond->curr_active_slave) {
851 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
852 }
853 } else {
854 struct slave *slave;
855 int i;
856 bond_for_each_slave(bond, slave, i) {
857 dev_mc_delete(slave->dev, addr, alen, 0);
858 }
859 }
860}
861
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800862
863/*
864 * Retrieve the list of registered multicast addresses for the bonding
865 * device and retransmit an IGMP JOIN request to the current active
866 * slave.
867 */
868static void bond_resend_igmp_join_requests(struct bonding *bond)
869{
870 struct in_device *in_dev;
871 struct ip_mc_list *im;
872
873 rcu_read_lock();
874 in_dev = __in_dev_get_rcu(bond->dev);
875 if (in_dev) {
876 for (im = in_dev->mc_list; im; im = im->next) {
877 ip_mc_rejoin_group(im);
878 }
879 }
880
881 rcu_read_unlock();
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
885 * Totally destroys the mc_list in bond
886 */
887static void bond_mc_list_destroy(struct bonding *bond)
888{
889 struct dev_mc_list *dmi;
890
891 dmi = bond->mc_list;
892 while (dmi) {
893 bond->mc_list = dmi->next;
894 kfree(dmi);
895 dmi = bond->mc_list;
896 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800897 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
901 * Copy all the Multicast addresses from src to the bonding device dst
902 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700903static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100904 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct dev_mc_list *dmi, *new_dmi;
907
908 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700909 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (!new_dmi) {
912 /* FIXME: Potential memory leak !!! */
913 return -ENOMEM;
914 }
915
916 new_dmi->next = bond->mc_list;
917 bond->mc_list = new_dmi;
918 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
919 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
920 new_dmi->dmi_users = dmi->dmi_users;
921 new_dmi->dmi_gusers = dmi->dmi_gusers;
922 }
923
924 return 0;
925}
926
927/*
928 * flush all members of flush->mc_list from device dev->mc_list
929 */
930static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
931{
932 struct bonding *bond = bond_dev->priv;
933 struct dev_mc_list *dmi;
934
935 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
936 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
937 }
938
939 if (bond->params.mode == BOND_MODE_8023AD) {
940 /* del lacpdu mc addr from mc list */
941 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
942
943 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
944 }
945}
946
947/*--------------------------- Active slave change ---------------------------*/
948
949/*
950 * Update the mc list and multicast-related flags for the new and
951 * old active slaves (if any) according to the multicast mode, and
952 * promiscuous flags unconditionally.
953 */
954static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
955{
956 struct dev_mc_list *dmi;
957
958 if (!USES_PRIMARY(bond->params.mode)) {
959 /* nothing to do - mc list is already up-to-date on
960 * all slaves
961 */
962 return;
963 }
964
965 if (old_active) {
966 if (bond->dev->flags & IFF_PROMISC) {
967 dev_set_promiscuity(old_active->dev, -1);
968 }
969
970 if (bond->dev->flags & IFF_ALLMULTI) {
971 dev_set_allmulti(old_active->dev, -1);
972 }
973
974 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
975 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
976 }
977 }
978
979 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700980 /* FIXME: Signal errors upstream. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (bond->dev->flags & IFF_PROMISC) {
982 dev_set_promiscuity(new_active->dev, 1);
983 }
984
985 if (bond->dev->flags & IFF_ALLMULTI) {
986 dev_set_allmulti(new_active->dev, 1);
987 }
988
989 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
990 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
991 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800992 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994}
995
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700996/*
997 * bond_do_fail_over_mac
998 *
999 * Perform special MAC address swapping for fail_over_mac settings
1000 *
1001 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1002 */
1003static void bond_do_fail_over_mac(struct bonding *bond,
1004 struct slave *new_active,
1005 struct slave *old_active)
1006{
1007 u8 tmp_mac[ETH_ALEN];
1008 struct sockaddr saddr;
1009 int rv;
1010
1011 switch (bond->params.fail_over_mac) {
1012 case BOND_FOM_ACTIVE:
1013 if (new_active)
1014 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1015 new_active->dev->addr_len);
1016 break;
1017 case BOND_FOM_FOLLOW:
1018 /*
1019 * if new_active && old_active, swap them
1020 * if just old_active, do nothing (going to no active slave)
1021 * if just new_active, set new_active to bond's MAC
1022 */
1023 if (!new_active)
1024 return;
1025
1026 write_unlock_bh(&bond->curr_slave_lock);
1027 read_unlock(&bond->lock);
1028
1029 if (old_active) {
1030 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1031 memcpy(saddr.sa_data, old_active->dev->dev_addr,
1032 ETH_ALEN);
1033 saddr.sa_family = new_active->dev->type;
1034 } else {
1035 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1036 saddr.sa_family = bond->dev->type;
1037 }
1038
1039 rv = dev_set_mac_address(new_active->dev, &saddr);
1040 if (rv) {
1041 printk(KERN_ERR DRV_NAME
1042 ": %s: Error %d setting MAC of slave %s\n",
1043 bond->dev->name, -rv, new_active->dev->name);
1044 goto out;
1045 }
1046
1047 if (!old_active)
1048 goto out;
1049
1050 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1051 saddr.sa_family = old_active->dev->type;
1052
1053 rv = dev_set_mac_address(old_active->dev, &saddr);
1054 if (rv)
1055 printk(KERN_ERR DRV_NAME
1056 ": %s: Error %d setting MAC of slave %s\n",
1057 bond->dev->name, -rv, new_active->dev->name);
1058out:
1059 read_lock(&bond->lock);
1060 write_lock_bh(&bond->curr_slave_lock);
1061 break;
1062 default:
1063 printk(KERN_ERR DRV_NAME
1064 ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1065 bond->dev->name, bond->params.fail_over_mac);
1066 break;
1067 }
1068
1069}
1070
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/**
1073 * find_best_interface - select the best available slave to be the active one
1074 * @bond: our bonding struct
1075 *
1076 * Warning: Caller must hold curr_slave_lock for writing.
1077 */
1078static struct slave *bond_find_best_slave(struct bonding *bond)
1079{
1080 struct slave *new_active, *old_active;
1081 struct slave *bestslave = NULL;
1082 int mintime = bond->params.updelay;
1083 int i;
1084
1085 new_active = old_active = bond->curr_active_slave;
1086
1087 if (!new_active) { /* there were no active slaves left */
1088 if (bond->slave_cnt > 0) { /* found one slave */
1089 new_active = bond->first_slave;
1090 } else {
1091 return NULL; /* still no slave, return NULL */
1092 }
1093 }
1094
1095 /* first try the primary link; if arping, a link must tx/rx traffic
1096 * before it can be considered the curr_active_slave - also, we would skip
1097 * slaves between the curr_active_slave and primary_slave that may be up
1098 * and able to arp
1099 */
1100 if ((bond->primary_slave) &&
1101 (!bond->params.arp_interval) &&
1102 (IS_UP(bond->primary_slave->dev))) {
1103 new_active = bond->primary_slave;
1104 }
1105
1106 /* remember where to stop iterating over the slaves */
1107 old_active = new_active;
1108
1109 bond_for_each_slave_from(bond, new_active, i, old_active) {
1110 if (IS_UP(new_active->dev)) {
1111 if (new_active->link == BOND_LINK_UP) {
1112 return new_active;
1113 } else if (new_active->link == BOND_LINK_BACK) {
1114 /* link up, but waiting for stabilization */
1115 if (new_active->delay < mintime) {
1116 mintime = new_active->delay;
1117 bestslave = new_active;
1118 }
1119 }
1120 }
1121 }
1122
1123 return bestslave;
1124}
1125
1126/**
1127 * change_active_interface - change the active slave into the specified one
1128 * @bond: our bonding struct
1129 * @new: the new slave to make the active one
1130 *
1131 * Set the new slave to the bond's settings and unset them on the old
1132 * curr_active_slave.
1133 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1134 *
1135 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1136 * because it is apparently the best available slave we have, even though its
1137 * updelay hasn't timed out yet.
1138 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001139 * If new_active is not NULL, caller must hold bond->lock for read and
1140 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001142void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct slave *old_active = bond->curr_active_slave;
1145
1146 if (old_active == new_active) {
1147 return;
1148 }
1149
1150 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001151 new_active->jiffies = jiffies;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (new_active->link == BOND_LINK_BACK) {
1154 if (USES_PRIMARY(bond->params.mode)) {
1155 printk(KERN_INFO DRV_NAME
1156 ": %s: making interface %s the new "
1157 "active one %d ms earlier.\n",
1158 bond->dev->name, new_active->dev->name,
1159 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1160 }
1161
1162 new_active->delay = 0;
1163 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (bond->params.mode == BOND_MODE_8023AD) {
1166 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1167 }
1168
1169 if ((bond->params.mode == BOND_MODE_TLB) ||
1170 (bond->params.mode == BOND_MODE_ALB)) {
1171 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1172 }
1173 } else {
1174 if (USES_PRIMARY(bond->params.mode)) {
1175 printk(KERN_INFO DRV_NAME
1176 ": %s: making interface %s the new "
1177 "active one.\n",
1178 bond->dev->name, new_active->dev->name);
1179 }
1180 }
1181 }
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (USES_PRIMARY(bond->params.mode)) {
1184 bond_mc_swap(bond, new_active, old_active);
1185 }
1186
1187 if ((bond->params.mode == BOND_MODE_TLB) ||
1188 (bond->params.mode == BOND_MODE_ALB)) {
1189 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001190 if (old_active)
1191 bond_set_slave_inactive_flags(old_active);
1192 if (new_active)
1193 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 } else {
1195 bond->curr_active_slave = new_active;
1196 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001197
1198 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1199 if (old_active) {
1200 bond_set_slave_inactive_flags(old_active);
1201 }
1202
1203 if (new_active) {
1204 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001205
Or Gerlitz709f8a42008-06-13 18:12:01 -07001206 if (bond->params.fail_over_mac)
1207 bond_do_fail_over_mac(bond, new_active,
1208 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001209
Or Gerlitz709f8a42008-06-13 18:12:01 -07001210 bond->send_grat_arp = bond->params.num_grat_arp;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07001211 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001212
Brian Haley305d5522008-11-04 17:51:14 -08001213 bond->send_unsol_na = bond->params.num_unsol_na;
1214 bond_send_unsolicited_na(bond);
1215
Or Gerlitz01f31092008-06-13 18:12:02 -07001216 write_unlock_bh(&bond->curr_slave_lock);
1217 read_unlock(&bond->lock);
1218
1219 netdev_bonding_change(bond->dev);
1220
1221 read_lock(&bond->lock);
1222 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001223 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227/**
1228 * bond_select_active_slave - select a new active slave, if needed
1229 * @bond: our bonding struct
1230 *
1231 * This functions shoud be called when one of the following occurs:
1232 * - The old curr_active_slave has been released or lost its link.
1233 * - The primary_slave has got its link back.
1234 * - A slave has got its link back and there's no old curr_active_slave.
1235 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001236 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001238void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001241 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 best_slave = bond_find_best_slave(bond);
1244 if (best_slave != bond->curr_active_slave) {
1245 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001246 rv = bond_set_carrier(bond);
1247 if (!rv)
1248 return;
1249
1250 if (netif_carrier_ok(bond->dev)) {
1251 printk(KERN_INFO DRV_NAME
1252 ": %s: first active interface up!\n",
1253 bond->dev->name);
1254 } else {
1255 printk(KERN_INFO DRV_NAME ": %s: "
1256 "now running without any active interface !\n",
1257 bond->dev->name);
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260}
1261
1262/*--------------------------- slave list handling ---------------------------*/
1263
1264/*
1265 * This function attaches the slave to the end of list.
1266 *
1267 * bond->lock held for writing by caller.
1268 */
1269static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1270{
1271 if (bond->first_slave == NULL) { /* attaching the first slave */
1272 new_slave->next = new_slave;
1273 new_slave->prev = new_slave;
1274 bond->first_slave = new_slave;
1275 } else {
1276 new_slave->next = bond->first_slave;
1277 new_slave->prev = bond->first_slave->prev;
1278 new_slave->next->prev = new_slave;
1279 new_slave->prev->next = new_slave;
1280 }
1281
1282 bond->slave_cnt++;
1283}
1284
1285/*
1286 * This function detaches the slave from the list.
1287 * WARNING: no check is made to verify if the slave effectively
1288 * belongs to <bond>.
1289 * Nothing is freed on return, structures are just unchained.
1290 * If any slave pointer in bond was pointing to <slave>,
1291 * it should be changed by the calling function.
1292 *
1293 * bond->lock held for writing by caller.
1294 */
1295static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1296{
1297 if (slave->next) {
1298 slave->next->prev = slave->prev;
1299 }
1300
1301 if (slave->prev) {
1302 slave->prev->next = slave->next;
1303 }
1304
1305 if (bond->first_slave == slave) { /* slave is the first slave */
1306 if (bond->slave_cnt > 1) { /* there are more slave */
1307 bond->first_slave = slave->next;
1308 } else {
1309 bond->first_slave = NULL; /* slave was the last one */
1310 }
1311 }
1312
1313 slave->next = NULL;
1314 slave->prev = NULL;
1315 bond->slave_cnt--;
1316}
1317
1318/*---------------------------------- IOCTL ----------------------------------*/
1319
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001320static int bond_sethwaddr(struct net_device *bond_dev,
1321 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 dprintk("bond_dev=%p\n", bond_dev);
1324 dprintk("slave_dev=%p\n", slave_dev);
1325 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1326 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1327 return 0;
1328}
1329
Herbert Xu7f353bf2007-08-10 15:47:58 -07001330#define BOND_VLAN_FEATURES \
1331 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1332 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001333
1334/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001335 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001336 * feature bits are managed elsewhere, so preserve those feature bits
1337 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001338 */
1339static int bond_compute_features(struct bonding *bond)
1340{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001341 struct slave *slave;
1342 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001343 unsigned long features = bond_dev->features;
Moni Shoua3158bf72007-10-09 19:43:41 -07001344 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1345 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001346 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001347
Herbert Xu7f353bf2007-08-10 15:47:58 -07001348 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001349 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1350
1351 if (!bond->first_slave)
1352 goto done;
1353
1354 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001355
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001356 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001357 features = netdev_increment_features(features,
1358 slave->dev->features,
1359 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001360 if (slave->dev->hard_header_len > max_hard_header_len)
1361 max_hard_header_len = slave->dev->hard_header_len;
1362 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001363
Herbert Xub63365a2008-10-23 01:11:29 -07001364done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001365 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001366 bond_dev->features = netdev_fix_features(features, NULL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001367 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001368
1369 return 0;
1370}
1371
Moni Shoua872254d2007-10-09 19:43:38 -07001372
1373static void bond_setup_by_slave(struct net_device *bond_dev,
1374 struct net_device *slave_dev)
1375{
Moni Shouad90a1622007-10-09 19:43:43 -07001376 struct bonding *bond = bond_dev->priv;
1377
Moni Shoua872254d2007-10-09 19:43:38 -07001378 bond_dev->neigh_setup = slave_dev->neigh_setup;
Jay Vosburgh1284cd32007-10-15 16:44:27 -07001379 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001380
1381 bond_dev->type = slave_dev->type;
1382 bond_dev->hard_header_len = slave_dev->hard_header_len;
1383 bond_dev->addr_len = slave_dev->addr_len;
1384
1385 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1386 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001387 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001391int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct bonding *bond = bond_dev->priv;
1394 struct slave *new_slave = NULL;
1395 struct dev_mc_list *dmi;
1396 struct sockaddr addr;
1397 int link_reporting;
1398 int old_features = bond_dev->features;
1399 int res = 0;
1400
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001401 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1402 slave_dev->do_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001404 ": %s: Warning: no link monitoring support for %s\n",
1405 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 /* bond must be initialized by bond_open() before enslaving */
1409 if (!(bond_dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -07001410 printk(KERN_WARNING DRV_NAME
1411 " %s: master_dev is not up in bond_enslave\n",
1412 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 /* already enslaved */
1416 if (slave_dev->flags & IFF_SLAVE) {
1417 dprintk("Error, Device was already enslaved\n");
1418 return -EBUSY;
1419 }
1420
1421 /* vlan challenged mutual exclusion */
1422 /* no need to lock since we're protected by rtnl_lock */
1423 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1424 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1425 if (!list_empty(&bond->vlan_list)) {
1426 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001427 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001429 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 bond_dev->name);
1431 return -EPERM;
1432 } else {
1433 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001434 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 "slave %s. Adding VLANs will be blocked as "
1436 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001437 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 bond_dev->name);
1439 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1440 }
1441 } else {
1442 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1443 if (bond->slave_cnt == 0) {
1444 /* First slave, and it is not VLAN challenged,
1445 * so remove the block of adding VLANs over the bond.
1446 */
1447 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1448 }
1449 }
1450
Jay Vosburgh217df672005-09-26 16:11:50 -07001451 /*
1452 * Old ifenslave binaries are no longer supported. These can
1453 * be identified with moderate accurary by the state of the slave:
1454 * the current ifenslave will set the interface down prior to
1455 * enslaving it; the old ifenslave will not.
1456 */
1457 if ((slave_dev->flags & IFF_UP)) {
1458 printk(KERN_ERR DRV_NAME ": %s is up. "
1459 "This may be due to an out of date ifenslave.\n",
1460 slave_dev->name);
1461 res = -EPERM;
1462 goto err_undo_flags;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Moni Shoua872254d2007-10-09 19:43:38 -07001465 /* set bonding device ether type by slave - bonding netdevices are
1466 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1467 * there is a need to override some of the type dependent attribs/funcs.
1468 *
1469 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1470 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1471 */
1472 if (bond->slave_cnt == 0) {
1473 if (slave_dev->type != ARPHRD_ETHER)
1474 bond_setup_by_slave(bond_dev, slave_dev);
1475 } else if (bond_dev->type != slave_dev->type) {
1476 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1477 "from other slaves (%d), can not enslave it.\n",
1478 slave_dev->name,
1479 slave_dev->type, bond_dev->type);
1480 res = -EINVAL;
1481 goto err_undo_flags;
1482 }
1483
Jay Vosburgh217df672005-09-26 16:11:50 -07001484 if (slave_dev->set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001485 if (bond->slave_cnt == 0) {
1486 printk(KERN_WARNING DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001487 ": %s: Warning: The first slave device "
1488 "specified does not support setting the MAC "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001489 "address. Setting fail_over_mac to active.",
Jay Vosburghdd957c52007-10-09 19:57:24 -07001490 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001491 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1492 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001493 printk(KERN_ERR DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001494 ": %s: Error: The slave device specified "
1495 "does not support setting the MAC address, "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001496 "but fail_over_mac is not set to active.\n"
Moni Shoua2ab82852007-10-09 19:43:39 -07001497 , bond_dev->name);
1498 res = -EOPNOTSUPP;
1499 goto err_undo_flags;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502
Joe Jin243cb4e2007-02-06 14:16:40 -08001503 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (!new_slave) {
1505 res = -ENOMEM;
1506 goto err_undo_flags;
1507 }
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* save slave's original flags before calling
1510 * netdev_set_master and dev_open
1511 */
1512 new_slave->original_flags = slave_dev->flags;
1513
Jay Vosburgh217df672005-09-26 16:11:50 -07001514 /*
1515 * Save slave's original ("permanent") mac address for modes
1516 * that need it, and for restoring it upon release, and then
1517 * set it to the master's address
1518 */
1519 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Jay Vosburghdd957c52007-10-09 19:57:24 -07001521 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001522 /*
1523 * Set slave to master's mac address. The application already
1524 * set the master's mac address to that of the first slave
1525 */
1526 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1527 addr.sa_family = slave_dev->type;
1528 res = dev_set_mac_address(slave_dev, &addr);
1529 if (res) {
1530 dprintk("Error %d calling set_mac_address\n", res);
1531 goto err_free;
1532 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001535 res = netdev_set_master(slave_dev, bond_dev);
1536 if (res) {
1537 dprintk("Error %d calling netdev_set_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001538 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001539 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001540 /* open the slave since the application closed it */
1541 res = dev_open(slave_dev);
1542 if (res) {
1543 dprintk("Openning slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001544 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001548 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if ((bond->params.mode == BOND_MODE_TLB) ||
1551 (bond->params.mode == BOND_MODE_ALB)) {
1552 /* bond_alb_init_slave() must be called before all other stages since
1553 * it might fail and we do not want to have to undo everything
1554 */
1555 res = bond_alb_init_slave(bond, new_slave);
1556 if (res) {
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001557 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 }
1560
1561 /* If the mode USES_PRIMARY, then the new slave gets the
1562 * master's promisc (and mc) settings only if it becomes the
1563 * curr_active_slave, and that is taken care of later when calling
1564 * bond_change_active()
1565 */
1566 if (!USES_PRIMARY(bond->params.mode)) {
1567 /* set promiscuity level to new slave */
1568 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001569 res = dev_set_promiscuity(slave_dev, 1);
1570 if (res)
1571 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
1574 /* set allmulti level to new slave */
1575 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001576 res = dev_set_allmulti(slave_dev, 1);
1577 if (res)
1578 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
David S. Millerb9e40852008-07-15 00:15:08 -07001581 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /* upload master's mc_list to new slave */
1583 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1584 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1585 }
David S. Millerb9e40852008-07-15 00:15:08 -07001586 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588
1589 if (bond->params.mode == BOND_MODE_8023AD) {
1590 /* add lacpdu mc addr to mc list */
1591 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1592
1593 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1594 }
1595
1596 bond_add_vlans_on_slave(bond, slave_dev);
1597
1598 write_lock_bh(&bond->lock);
1599
1600 bond_attach_slave(bond, new_slave);
1601
1602 new_slave->delay = 0;
1603 new_slave->link_failure_count = 0;
1604
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001605 bond_compute_features(bond);
1606
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001607 write_unlock_bh(&bond->lock);
1608
1609 read_lock(&bond->lock);
1610
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001611 new_slave->last_arp_rx = jiffies;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (bond->params.miimon && !bond->params.use_carrier) {
1614 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1615
1616 if ((link_reporting == -1) && !bond->params.arp_interval) {
1617 /*
1618 * miimon is set but a bonded network driver
1619 * does not support ETHTOOL/MII and
1620 * arp_interval is not set. Note: if
1621 * use_carrier is enabled, we will never go
1622 * here (because netif_carrier is always
1623 * supported); thus, we don't need to change
1624 * the messages for netif_carrier.
1625 */
1626 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001627 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 "available for interface %s, and "
1629 "arp_interval/arp_ip_target module parameters "
1630 "not specified, thus bonding will not detect "
1631 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001632 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 } else if (link_reporting == -1) {
1634 /* unable get link status using mii/ethtool */
1635 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001636 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 "interface %s; the network driver associated "
1638 "with this interface does not support MII or "
1639 "ETHTOOL link status reporting, thus miimon "
1640 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001641 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 }
1644
1645 /* check for initial state */
1646 if (!bond->params.miimon ||
1647 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1648 if (bond->params.updelay) {
1649 dprintk("Initial state of slave_dev is "
1650 "BOND_LINK_BACK\n");
1651 new_slave->link = BOND_LINK_BACK;
1652 new_slave->delay = bond->params.updelay;
1653 } else {
1654 dprintk("Initial state of slave_dev is "
1655 "BOND_LINK_UP\n");
1656 new_slave->link = BOND_LINK_UP;
1657 }
1658 new_slave->jiffies = jiffies;
1659 } else {
1660 dprintk("Initial state of slave_dev is "
1661 "BOND_LINK_DOWN\n");
1662 new_slave->link = BOND_LINK_DOWN;
1663 }
1664
1665 if (bond_update_speed_duplex(new_slave) &&
1666 (new_slave->link != BOND_LINK_DOWN)) {
1667 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001668 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001670 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (bond->params.mode == BOND_MODE_8023AD) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001673 printk(KERN_WARNING DRV_NAME
1674 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001676 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 }
1679
1680 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1681 /* if there is a primary slave, remember it */
1682 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1683 bond->primary_slave = new_slave;
1684 }
1685 }
1686
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001687 write_lock_bh(&bond->curr_slave_lock);
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 switch (bond->params.mode) {
1690 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001691 bond_set_slave_inactive_flags(new_slave);
1692 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
1694 case BOND_MODE_8023AD:
1695 /* in 802.3ad mode, the internal mechanism
1696 * will activate the slaves in the selected
1697 * aggregator
1698 */
1699 bond_set_slave_inactive_flags(new_slave);
1700 /* if this is the first slave */
1701 if (bond->slave_cnt == 1) {
1702 SLAVE_AD_INFO(new_slave).id = 1;
1703 /* Initialize AD with the number of times that the AD timer is called in 1 second
1704 * can be called only after the mac address of the bond is set
1705 */
1706 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1707 bond->params.lacp_fast);
1708 } else {
1709 SLAVE_AD_INFO(new_slave).id =
1710 SLAVE_AD_INFO(new_slave->prev).id + 1;
1711 }
1712
1713 bond_3ad_bind_slave(new_slave);
1714 break;
1715 case BOND_MODE_TLB:
1716 case BOND_MODE_ALB:
1717 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001718 bond_set_slave_inactive_flags(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 break;
1720 default:
1721 dprintk("This slave is always active in trunk mode\n");
1722
1723 /* always active in trunk mode */
1724 new_slave->state = BOND_STATE_ACTIVE;
1725
1726 /* In trunking mode there is little meaning to curr_active_slave
1727 * anyway (it holds no special properties of the bond device),
1728 * so we can change it without calling change_active_interface()
1729 */
1730 if (!bond->curr_active_slave) {
1731 bond->curr_active_slave = new_slave;
1732 }
1733 break;
1734 } /* switch(bond_mode) */
1735
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001736 write_unlock_bh(&bond->curr_slave_lock);
1737
Jay Vosburghff59c452006-03-27 13:27:43 -08001738 bond_set_carrier(bond);
1739
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001740 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001742 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1743 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001744 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 printk(KERN_INFO DRV_NAME
1747 ": %s: enslaving %s as a%s interface with a%s link.\n",
1748 bond_dev->name, slave_dev->name,
1749 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1750 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1751
1752 /* enslave is successful */
1753 return 0;
1754
1755/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756err_close:
1757 dev_close(slave_dev);
1758
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001759err_unset_master:
1760 netdev_set_master(slave_dev, NULL);
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001763 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001764 /* XXX TODO - fom follow mode needs to change master's
1765 * MAC if this slave's MAC is in use by the bond, or at
1766 * least print a warning.
1767 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001768 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1769 addr.sa_family = slave_dev->type;
1770 dev_set_mac_address(slave_dev, &addr);
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773err_free:
1774 kfree(new_slave);
1775
1776err_undo_flags:
1777 bond_dev->features = old_features;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return res;
1780}
1781
1782/*
1783 * Try to release the slave device <slave> from the bond device <master>
1784 * It is legal to access curr_active_slave without a lock because all the function
1785 * is write-locked.
1786 *
1787 * The rules for slave state should be:
1788 * for Active/Backup:
1789 * Active stays on all backups go down
1790 * for Bonded connections:
1791 * The first up interface should be left on and all others downed.
1792 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001793int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct bonding *bond = bond_dev->priv;
1796 struct slave *slave, *oldcurrent;
1797 struct sockaddr addr;
1798 int mac_addr_differ;
1799
1800 /* slave is not a slave or master is not master of this slave */
1801 if (!(slave_dev->flags & IFF_SLAVE) ||
1802 (slave_dev->master != bond_dev)) {
1803 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001804 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 bond_dev->name, slave_dev->name);
1806 return -EINVAL;
1807 }
1808
1809 write_lock_bh(&bond->lock);
1810
1811 slave = bond_get_slave_by_dev(bond, slave_dev);
1812 if (!slave) {
1813 /* not a slave of this bond */
1814 printk(KERN_INFO DRV_NAME
1815 ": %s: %s not enslaved\n",
1816 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001817 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return -EINVAL;
1819 }
1820
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001821 if (!bond->params.fail_over_mac) {
1822 mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
1823 ETH_ALEN);
1824 if (!mac_addr_differ && (bond->slave_cnt > 1))
1825 printk(KERN_WARNING DRV_NAME
1826 ": %s: Warning: the permanent HWaddr of %s - "
Johannes Berge1749612008-10-27 15:59:26 -07001827 "%pM - is still in use by %s. "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001828 "Set the HWaddr of %s to a different address "
1829 "to avoid conflicts.\n",
1830 bond_dev->name, slave_dev->name,
Johannes Berge1749612008-10-27 15:59:26 -07001831 slave->perm_hwaddr,
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001832 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834
1835 /* Inform AD package of unbinding of slave. */
1836 if (bond->params.mode == BOND_MODE_8023AD) {
1837 /* must be called before the slave is
1838 * detached from the list
1839 */
1840 bond_3ad_unbind_slave(slave);
1841 }
1842
1843 printk(KERN_INFO DRV_NAME
1844 ": %s: releasing %s interface %s\n",
1845 bond_dev->name,
1846 (slave->state == BOND_STATE_ACTIVE)
1847 ? "active" : "backup",
1848 slave_dev->name);
1849
1850 oldcurrent = bond->curr_active_slave;
1851
1852 bond->current_arp_slave = NULL;
1853
1854 /* release the slave from its bond */
1855 bond_detach_slave(bond, slave);
1856
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001857 bond_compute_features(bond);
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (bond->primary_slave == slave) {
1860 bond->primary_slave = NULL;
1861 }
1862
1863 if (oldcurrent == slave) {
1864 bond_change_active_slave(bond, NULL);
1865 }
1866
1867 if ((bond->params.mode == BOND_MODE_TLB) ||
1868 (bond->params.mode == BOND_MODE_ALB)) {
1869 /* Must be called only after the slave has been
1870 * detached from the list and the curr_active_slave
1871 * has been cleared (if our_slave == old_current),
1872 * but before a new active slave is selected.
1873 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001874 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001876 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001879 if (oldcurrent == slave) {
1880 /*
1881 * Note that we hold RTNL over this sequence, so there
1882 * is no concern that another slave add/remove event
1883 * will interfere.
1884 */
1885 write_unlock_bh(&bond->lock);
1886 read_lock(&bond->lock);
1887 write_lock_bh(&bond->curr_slave_lock);
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 bond_select_active_slave(bond);
1890
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001891 write_unlock_bh(&bond->curr_slave_lock);
1892 read_unlock(&bond->lock);
1893 write_lock_bh(&bond->lock);
1894 }
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001897 bond_set_carrier(bond);
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* if the last slave was removed, zero the mac address
1900 * of the master so it will be set by the application
1901 * to the mac address of the first slave
1902 */
1903 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1904
1905 if (list_empty(&bond->vlan_list)) {
1906 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1907 } else {
1908 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001909 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001911 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001913 ": %s: When re-adding slaves, make sure the bond's "
1914 "HW address matches its VLANs'.\n",
1915 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1918 !bond_has_challenged_slaves(bond)) {
1919 printk(KERN_INFO DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001920 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001922 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1924 }
1925
1926 write_unlock_bh(&bond->lock);
1927
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001928 /* must do this from outside any spinlocks */
1929 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 bond_del_vlans_from_slave(bond, slave_dev);
1932
1933 /* If the mode USES_PRIMARY, then we should only remove its
1934 * promisc and mc settings if it was the curr_active_slave, but that was
1935 * already taken care of above when we detached the slave
1936 */
1937 if (!USES_PRIMARY(bond->params.mode)) {
1938 /* unset promiscuity level from slave */
1939 if (bond_dev->flags & IFF_PROMISC) {
1940 dev_set_promiscuity(slave_dev, -1);
1941 }
1942
1943 /* unset allmulti level from slave */
1944 if (bond_dev->flags & IFF_ALLMULTI) {
1945 dev_set_allmulti(slave_dev, -1);
1946 }
1947
1948 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07001949 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07001951 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
1954 netdev_set_master(slave_dev, NULL);
1955
1956 /* close slave before restoring its mac address */
1957 dev_close(slave_dev);
1958
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001959 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001960 /* restore original ("permanent") mac address */
1961 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1962 addr.sa_family = slave_dev->type;
1963 dev_set_mac_address(slave_dev, &addr);
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001966 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001967 IFF_SLAVE_INACTIVE | IFF_BONDING |
1968 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 kfree(slave);
1971
1972 return 0; /* deletion OK */
1973}
1974
1975/*
Moni Shouad90a1622007-10-09 19:43:43 -07001976* Destroy a bonding device.
1977* Must be under rtnl_lock when this function is called.
1978*/
1979void bond_destroy(struct bonding *bond)
1980{
1981 bond_deinit(bond->dev);
1982 bond_destroy_sysfs_entry(bond);
Jay Vosburgh8cbdeec2007-11-13 21:16:29 -08001983 unregister_netdevice(bond->dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001984}
1985
Jay Vosburgha434e432008-10-30 17:41:15 -07001986static void bond_destructor(struct net_device *bond_dev)
1987{
1988 struct bonding *bond = bond_dev->priv;
1989
1990 if (bond->wq)
1991 destroy_workqueue(bond->wq);
1992
1993 netif_addr_lock_bh(bond_dev);
1994 bond_mc_list_destroy(bond);
1995 netif_addr_unlock_bh(bond_dev);
1996
1997 free_netdev(bond_dev);
1998}
1999
Moni Shouad90a1622007-10-09 19:43:43 -07002000/*
2001* First release a slave and than destroy the bond if no more slaves iare left.
2002* Must be under rtnl_lock when this function is called.
2003*/
2004int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
2005{
2006 struct bonding *bond = bond_dev->priv;
2007 int ret;
2008
2009 ret = bond_release(bond_dev, slave_dev);
2010 if ((ret == 0) && (bond->slave_cnt == 0)) {
2011 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
2012 bond_dev->name, bond_dev->name);
2013 bond_destroy(bond);
2014 }
2015 return ret;
2016}
2017
2018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * This function releases all slaves.
2020 */
2021static int bond_release_all(struct net_device *bond_dev)
2022{
2023 struct bonding *bond = bond_dev->priv;
2024 struct slave *slave;
2025 struct net_device *slave_dev;
2026 struct sockaddr addr;
2027
2028 write_lock_bh(&bond->lock);
2029
Jay Vosburghff59c452006-03-27 13:27:43 -08002030 netif_carrier_off(bond_dev);
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (bond->slave_cnt == 0) {
2033 goto out;
2034 }
2035
2036 bond->current_arp_slave = NULL;
2037 bond->primary_slave = NULL;
2038 bond_change_active_slave(bond, NULL);
2039
2040 while ((slave = bond->first_slave) != NULL) {
2041 /* Inform AD package of unbinding of slave
2042 * before slave is detached from the list.
2043 */
2044 if (bond->params.mode == BOND_MODE_8023AD) {
2045 bond_3ad_unbind_slave(slave);
2046 }
2047
2048 slave_dev = slave->dev;
2049 bond_detach_slave(bond, slave);
2050
Jay Vosburgh25433312008-01-17 16:24:59 -08002051 /* now that the slave is detached, unlock and perform
2052 * all the undo steps that should not be called from
2053 * within a lock.
2054 */
2055 write_unlock_bh(&bond->lock);
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if ((bond->params.mode == BOND_MODE_TLB) ||
2058 (bond->params.mode == BOND_MODE_ALB)) {
2059 /* must be called only after the slave
2060 * has been detached from the list
2061 */
2062 bond_alb_deinit_slave(bond, slave);
2063 }
2064
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002065 bond_compute_features(bond);
2066
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002067 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 bond_del_vlans_from_slave(bond, slave_dev);
2069
2070 /* If the mode USES_PRIMARY, then we should only remove its
2071 * promisc and mc settings if it was the curr_active_slave, but that was
2072 * already taken care of above when we detached the slave
2073 */
2074 if (!USES_PRIMARY(bond->params.mode)) {
2075 /* unset promiscuity level from slave */
2076 if (bond_dev->flags & IFF_PROMISC) {
2077 dev_set_promiscuity(slave_dev, -1);
2078 }
2079
2080 /* unset allmulti level from slave */
2081 if (bond_dev->flags & IFF_ALLMULTI) {
2082 dev_set_allmulti(slave_dev, -1);
2083 }
2084
2085 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002086 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002088 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090
2091 netdev_set_master(slave_dev, NULL);
2092
2093 /* close slave before restoring its mac address */
2094 dev_close(slave_dev);
2095
Jay Vosburghdd957c52007-10-09 19:57:24 -07002096 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002097 /* restore original ("permanent") mac address*/
2098 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2099 addr.sa_family = slave_dev->type;
2100 dev_set_mac_address(slave_dev, &addr);
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002103 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2104 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 kfree(slave);
2107
2108 /* re-acquire the lock before getting the next slave */
2109 write_lock_bh(&bond->lock);
2110 }
2111
2112 /* zero the mac address of the master so it will be
2113 * set by the application to the mac address of the
2114 * first slave
2115 */
2116 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2117
2118 if (list_empty(&bond->vlan_list)) {
2119 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2120 } else {
2121 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002122 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08002124 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002126 ": %s: When re-adding slaves, make sure the bond's "
2127 "HW address matches its VLANs'.\n",
2128 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130
2131 printk(KERN_INFO DRV_NAME
2132 ": %s: released all slaves\n",
2133 bond_dev->name);
2134
2135out:
2136 write_unlock_bh(&bond->lock);
2137
2138 return 0;
2139}
2140
2141/*
2142 * This function changes the active slave to slave <slave_dev>.
2143 * It returns -EINVAL in the following cases.
2144 * - <slave_dev> is not found in the list.
2145 * - There is not active slave now.
2146 * - <slave_dev> is already active.
2147 * - The link state of <slave_dev> is not BOND_LINK_UP.
2148 * - <slave_dev> is not running.
2149 * In these cases, this fuction does nothing.
2150 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2151 */
2152static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2153{
2154 struct bonding *bond = bond_dev->priv;
2155 struct slave *old_active = NULL;
2156 struct slave *new_active = NULL;
2157 int res = 0;
2158
2159 if (!USES_PRIMARY(bond->params.mode)) {
2160 return -EINVAL;
2161 }
2162
2163 /* Verify that master_dev is indeed the master of slave_dev */
2164 if (!(slave_dev->flags & IFF_SLAVE) ||
2165 (slave_dev->master != bond_dev)) {
2166 return -EINVAL;
2167 }
2168
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002169 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002171 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002173 read_unlock(&bond->curr_slave_lock);
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 new_active = bond_get_slave_by_dev(bond, slave_dev);
2176
2177 /*
2178 * Changing to the current active: do nothing; return success.
2179 */
2180 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002181 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return 0;
2183 }
2184
2185 if ((new_active) &&
2186 (old_active) &&
2187 (new_active->link == BOND_LINK_UP) &&
2188 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002189 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002191 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 } else {
2193 res = -EINVAL;
2194 }
2195
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002196 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 return res;
2199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2202{
2203 struct bonding *bond = bond_dev->priv;
2204
2205 info->bond_mode = bond->params.mode;
2206 info->miimon = bond->params.miimon;
2207
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002208 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002210 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 return 0;
2213}
2214
2215static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2216{
2217 struct bonding *bond = bond_dev->priv;
2218 struct slave *slave;
2219 int i, found = 0;
2220
2221 if (info->slave_id < 0) {
2222 return -ENODEV;
2223 }
2224
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002225 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 bond_for_each_slave(bond, slave, i) {
2228 if (i == (int)info->slave_id) {
2229 found = 1;
2230 break;
2231 }
2232 }
2233
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002234 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if (found) {
2237 strcpy(info->slave_name, slave->dev->name);
2238 info->link = slave->link;
2239 info->state = slave->state;
2240 info->link_failure_count = slave->link_failure_count;
2241 } else {
2242 return -ENODEV;
2243 }
2244
2245 return 0;
2246}
2247
2248/*-------------------------------- Monitoring -------------------------------*/
2249
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002250
2251static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002253 struct slave *slave;
2254 int i, link_state, commit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002257 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002259 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002262 case BOND_LINK_UP:
2263 if (link_state)
2264 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002266 slave->link = BOND_LINK_FAIL;
2267 slave->delay = bond->params.downdelay;
2268 if (slave->delay) {
2269 printk(KERN_INFO DRV_NAME
2270 ": %s: link status down for %s"
2271 "interface %s, disabling it in %d ms.\n",
2272 bond->dev->name,
2273 (bond->params.mode ==
2274 BOND_MODE_ACTIVEBACKUP) ?
2275 ((slave->state == BOND_STATE_ACTIVE) ?
2276 "active " : "backup ") : "",
2277 slave->dev->name,
2278 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002280 /*FALLTHRU*/
2281 case BOND_LINK_FAIL:
2282 if (link_state) {
2283 /*
2284 * recovered before downdelay expired
2285 */
2286 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 slave->jiffies = jiffies;
2288 printk(KERN_INFO DRV_NAME
2289 ": %s: link status up again after %d "
2290 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002291 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002292 (bond->params.downdelay - slave->delay) *
2293 bond->params.miimon,
2294 slave->dev->name);
2295 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002297
2298 if (slave->delay <= 0) {
2299 slave->new_link = BOND_LINK_DOWN;
2300 commit++;
2301 continue;
2302 }
2303
2304 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002307 case BOND_LINK_DOWN:
2308 if (!link_state)
2309 continue;
2310
2311 slave->link = BOND_LINK_BACK;
2312 slave->delay = bond->params.updelay;
2313
2314 if (slave->delay) {
2315 printk(KERN_INFO DRV_NAME
2316 ": %s: link status up for "
2317 "interface %s, enabling it in %d ms.\n",
2318 bond->dev->name, slave->dev->name,
2319 bond->params.updelay *
2320 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002322 /*FALLTHRU*/
2323 case BOND_LINK_BACK:
2324 if (!link_state) {
2325 slave->link = BOND_LINK_DOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 printk(KERN_INFO DRV_NAME
2327 ": %s: link status down again after %d "
2328 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002329 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002330 (bond->params.updelay - slave->delay) *
2331 bond->params.miimon,
2332 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002333
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002334 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002336
2337 if (slave->delay <= 0) {
2338 slave->new_link = BOND_LINK_UP;
2339 commit++;
2340 continue;
2341 }
2342
2343 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002345 }
2346 }
2347
2348 return commit;
2349}
2350
2351static void bond_miimon_commit(struct bonding *bond)
2352{
2353 struct slave *slave;
2354 int i;
2355
2356 bond_for_each_slave(bond, slave, i) {
2357 switch (slave->new_link) {
2358 case BOND_LINK_NOCHANGE:
2359 continue;
2360
2361 case BOND_LINK_UP:
2362 slave->link = BOND_LINK_UP;
2363 slave->jiffies = jiffies;
2364
2365 if (bond->params.mode == BOND_MODE_8023AD) {
2366 /* prevent it from being the active one */
2367 slave->state = BOND_STATE_BACKUP;
2368 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2369 /* make it immediately active */
2370 slave->state = BOND_STATE_ACTIVE;
2371 } else if (slave != bond->primary_slave) {
2372 /* prevent it from being the active one */
2373 slave->state = BOND_STATE_BACKUP;
2374 }
2375
2376 printk(KERN_INFO DRV_NAME
2377 ": %s: link status definitely "
2378 "up for interface %s.\n",
2379 bond->dev->name, slave->dev->name);
2380
2381 /* notify ad that the link status has changed */
2382 if (bond->params.mode == BOND_MODE_8023AD)
2383 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2384
2385 if ((bond->params.mode == BOND_MODE_TLB) ||
2386 (bond->params.mode == BOND_MODE_ALB))
2387 bond_alb_handle_link_change(bond, slave,
2388 BOND_LINK_UP);
2389
2390 if (!bond->curr_active_slave ||
2391 (slave == bond->primary_slave))
2392 goto do_failover;
2393
2394 continue;
2395
2396 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002397 if (slave->link_failure_count < UINT_MAX)
2398 slave->link_failure_count++;
2399
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002400 slave->link = BOND_LINK_DOWN;
2401
2402 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2403 bond->params.mode == BOND_MODE_8023AD)
2404 bond_set_slave_inactive_flags(slave);
2405
2406 printk(KERN_INFO DRV_NAME
2407 ": %s: link status definitely down for "
2408 "interface %s, disabling it\n",
2409 bond->dev->name, slave->dev->name);
2410
2411 if (bond->params.mode == BOND_MODE_8023AD)
2412 bond_3ad_handle_link_change(slave,
2413 BOND_LINK_DOWN);
2414
2415 if (bond->params.mode == BOND_MODE_TLB ||
2416 bond->params.mode == BOND_MODE_ALB)
2417 bond_alb_handle_link_change(bond, slave,
2418 BOND_LINK_DOWN);
2419
2420 if (slave == bond->curr_active_slave)
2421 goto do_failover;
2422
2423 continue;
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 default:
Mitch Williams4e0952c2005-11-09 10:34:57 -08002426 printk(KERN_ERR DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002427 ": %s: invalid new link %d on slave %s\n",
2428 bond->dev->name, slave->new_link,
2429 slave->dev->name);
2430 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002432 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002435do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002436 ASSERT_RTNL();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002437 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002439 write_unlock_bh(&bond->curr_slave_lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002440 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002441
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002442 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002445/*
2446 * bond_mii_monitor
2447 *
2448 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002449 * inspection, then (if inspection indicates something needs to be done)
2450 * an acquisition of appropriate locks followed by a commit phase to
2451 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002452 */
2453void bond_mii_monitor(struct work_struct *work)
2454{
2455 struct bonding *bond = container_of(work, struct bonding,
2456 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002457
2458 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002459 if (bond->kill_timers)
2460 goto out;
2461
2462 if (bond->slave_cnt == 0)
2463 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002464
2465 if (bond->send_grat_arp) {
2466 read_lock(&bond->curr_slave_lock);
2467 bond_send_gratuitous_arp(bond);
2468 read_unlock(&bond->curr_slave_lock);
2469 }
2470
Brian Haley305d5522008-11-04 17:51:14 -08002471 if (bond->send_unsol_na) {
2472 read_lock(&bond->curr_slave_lock);
2473 bond_send_unsolicited_na(bond);
2474 read_unlock(&bond->curr_slave_lock);
2475 }
2476
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002477 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002478 read_unlock(&bond->lock);
2479 rtnl_lock();
2480 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002481
2482 bond_miimon_commit(bond);
2483
Jay Vosburgh56556622008-01-17 16:25:03 -08002484 read_unlock(&bond->lock);
2485 rtnl_unlock(); /* might sleep, hold no other locks */
2486 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002487 }
2488
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002489re_arm:
2490 if (bond->params.miimon)
2491 queue_delayed_work(bond->wq, &bond->mii_work,
2492 msecs_to_jiffies(bond->params.miimon));
2493out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002494 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002495}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002496
Al Virod3bb52b2007-08-22 20:06:58 -04002497static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002498{
2499 struct in_device *idev;
2500 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002501 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002502
2503 if (!dev)
2504 return 0;
2505
2506 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002507 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002508 if (!idev)
2509 goto out;
2510
2511 ifa = idev->ifa_list;
2512 if (!ifa)
2513 goto out;
2514
2515 addr = ifa->ifa_local;
2516out:
2517 rcu_read_unlock();
2518 return addr;
2519}
2520
Al Virod3bb52b2007-08-22 20:06:58 -04002521static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002522{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002523 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002524
2525 if (ip == bond->master_ip)
2526 return 1;
2527
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002528 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002529 if (ip == vlan->vlan_ip)
2530 return 1;
2531 }
2532
2533 return 0;
2534}
2535
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002536/*
2537 * We go to the (large) trouble of VLAN tagging ARP frames because
2538 * switches in VLAN mode (especially if ports are configured as
2539 * "native" to a VLAN) might not pass non-tagged frames.
2540 */
Al Virod3bb52b2007-08-22 20:06:58 -04002541static 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 -04002542{
2543 struct sk_buff *skb;
2544
2545 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2546 slave_dev->name, dest_ip, src_ip, vlan_id);
2547
2548 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2549 NULL, slave_dev->dev_addr, NULL);
2550
2551 if (!skb) {
2552 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2553 return;
2554 }
2555 if (vlan_id) {
2556 skb = vlan_put_tag(skb, vlan_id);
2557 if (!skb) {
2558 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2559 return;
2560 }
2561 }
2562 arp_xmit(skb);
2563}
2564
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2567{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002568 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002569 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002570 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002571 struct net_device *vlan_dev;
2572 struct flowi fl;
2573 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Mitch Williams6b780562005-11-09 10:36:19 -08002575 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2576 if (!targets[i])
2577 continue;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002578 dprintk("basa: target %x\n", targets[i]);
2579 if (list_empty(&bond->vlan_list)) {
2580 dprintk("basa: empty vlan: arp_send\n");
2581 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2582 bond->master_ip, 0);
2583 continue;
2584 }
2585
2586 /*
2587 * If VLANs are configured, we do a route lookup to
2588 * determine which VLAN interface would be used, so we
2589 * can tag the ARP with the proper VLAN tag.
2590 */
2591 memset(&fl, 0, sizeof(fl));
2592 fl.fl4_dst = targets[i];
2593 fl.fl4_tos = RTO_ONLINK;
2594
Denis V. Lunevf2063512008-01-22 22:07:34 -08002595 rv = ip_route_output_key(&init_net, &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002596 if (rv) {
2597 if (net_ratelimit()) {
2598 printk(KERN_WARNING DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002599 ": %s: no route to arp_ip_target %pI4\n",
2600 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002601 }
2602 continue;
2603 }
2604
2605 /*
2606 * This target is not on a VLAN
2607 */
2608 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002609 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002610 dprintk("basa: rtdev == bond->dev: arp_send\n");
2611 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2612 bond->master_ip, 0);
2613 continue;
2614 }
2615
2616 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002617 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002618 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002619 if (vlan_dev == rt->u.dst.dev) {
2620 vlan_id = vlan->vlan_id;
2621 dprintk("basa: vlan match on %s %d\n",
2622 vlan_dev->name, vlan_id);
2623 break;
2624 }
2625 }
2626
2627 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002628 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002629 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2630 vlan->vlan_ip, vlan_id);
2631 continue;
2632 }
2633
2634 if (net_ratelimit()) {
2635 printk(KERN_WARNING DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002636 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2637 bond->dev->name, &fl.fl4_dst,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002638 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2639 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002640 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002641 }
2642}
2643
2644/*
2645 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2646 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002647 *
2648 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002649 */
2650static void bond_send_gratuitous_arp(struct bonding *bond)
2651{
2652 struct slave *slave = bond->curr_active_slave;
2653 struct vlan_entry *vlan;
2654 struct net_device *vlan_dev;
2655
2656 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2657 slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002658
2659 if (!slave || !bond->send_grat_arp ||
2660 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002661 return;
2662
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002663 bond->send_grat_arp--;
2664
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665 if (bond->master_ip) {
2666 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002667 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002668 }
2669
2670 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002671 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002672 if (vlan->vlan_ip) {
2673 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2674 vlan->vlan_ip, vlan->vlan_id);
2675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677}
2678
Al Virod3bb52b2007-08-22 20:06:58 -04002679static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002680{
2681 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002682 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002683
2684 targets = bond->params.arp_targets;
2685 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Harvey Harrison63779432008-10-31 00:56:00 -07002686 dprintk("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2687 &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002688 if (sip == targets[i]) {
2689 if (bond_has_this_ip(bond, tip))
2690 slave->last_arp_rx = jiffies;
2691 return;
2692 }
2693 }
2694}
2695
2696static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2697{
2698 struct arphdr *arp;
2699 struct slave *slave;
2700 struct bonding *bond;
2701 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002702 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002703
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002704 if (dev_net(dev) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -07002705 goto out;
2706
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002707 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2708 goto out;
2709
2710 bond = dev->priv;
2711 read_lock(&bond->lock);
2712
2713 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2714 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2715 orig_dev ? orig_dev->name : "NULL");
2716
2717 slave = bond_get_slave_by_dev(bond, orig_dev);
2718 if (!slave || !slave_do_arp_validate(bond, slave))
2719 goto out_unlock;
2720
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002721 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002722 goto out_unlock;
2723
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002724 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002725 if (arp->ar_hln != dev->addr_len ||
2726 skb->pkt_type == PACKET_OTHERHOST ||
2727 skb->pkt_type == PACKET_LOOPBACK ||
2728 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2729 arp->ar_pro != htons(ETH_P_IP) ||
2730 arp->ar_pln != 4)
2731 goto out_unlock;
2732
2733 arp_ptr = (unsigned char *)(arp + 1);
2734 arp_ptr += dev->addr_len;
2735 memcpy(&sip, arp_ptr, 4);
2736 arp_ptr += 4 + dev->addr_len;
2737 memcpy(&tip, arp_ptr, 4);
2738
Harvey Harrison63779432008-10-31 00:56:00 -07002739 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2740 bond->dev->name, slave->dev->name, slave->state,
2741 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2742 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002743
2744 /*
2745 * Backup slaves won't see the ARP reply, but do come through
2746 * here for each ARP probe (so we swap the sip/tip to validate
2747 * the probe). In a "redundant switch, common router" type of
2748 * configuration, the ARP probe will (hopefully) travel from
2749 * the active, through one switch, the router, then the other
2750 * switch before reaching the backup.
2751 */
2752 if (slave->state == BOND_STATE_ACTIVE)
2753 bond_validate_arp(bond, slave, sip, tip);
2754 else
2755 bond_validate_arp(bond, slave, tip, sip);
2756
2757out_unlock:
2758 read_unlock(&bond->lock);
2759out:
2760 dev_kfree_skb(skb);
2761 return NET_RX_SUCCESS;
2762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764/*
2765 * this function is called regularly to monitor each slave's link
2766 * ensuring that traffic is being sent and received when arp monitoring
2767 * is used in load-balancing mode. if the adapter has been dormant, then an
2768 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2769 * arp monitoring in active backup mode.
2770 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002771void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002773 struct bonding *bond = container_of(work, struct bonding,
2774 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct slave *slave, *oldcurrent;
2776 int do_failover = 0;
2777 int delta_in_ticks;
2778 int i;
2779
2780 read_lock(&bond->lock);
2781
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002782 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 if (bond->kill_timers) {
2785 goto out;
2786 }
2787
2788 if (bond->slave_cnt == 0) {
2789 goto re_arm;
2790 }
2791
2792 read_lock(&bond->curr_slave_lock);
2793 oldcurrent = bond->curr_active_slave;
2794 read_unlock(&bond->curr_slave_lock);
2795
2796 /* see if any of the previous devices are up now (i.e. they have
2797 * xmt and rcv traffic). the curr_active_slave does not come into
2798 * the picture unless it is null. also, slave->jiffies is not needed
2799 * here because we send an arp on each slave and give a slave as
2800 * long as it needs to get the tx/rx within the delta.
2801 * TODO: what about up/down delay in arp mode? it wasn't here before
2802 * so it can wait
2803 */
2804 bond_for_each_slave(bond, slave, i) {
2805 if (slave->link != BOND_LINK_UP) {
David Sterbab63bb732007-12-06 23:40:33 -08002806 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2807 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 slave->link = BOND_LINK_UP;
2810 slave->state = BOND_STATE_ACTIVE;
2811
2812 /* primary_slave has no meaning in round-robin
2813 * mode. the window of a slave being up and
2814 * curr_active_slave being null after enslaving
2815 * is closed.
2816 */
2817 if (!oldcurrent) {
2818 printk(KERN_INFO DRV_NAME
2819 ": %s: link status definitely "
2820 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002821 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 slave->dev->name);
2823 do_failover = 1;
2824 } else {
2825 printk(KERN_INFO DRV_NAME
2826 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002827 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 slave->dev->name);
2829 }
2830 }
2831 } else {
2832 /* slave->link == BOND_LINK_UP */
2833
2834 /* not all switches will respond to an arp request
2835 * when the source ip is 0, so don't take the link down
2836 * if we don't know our ip yet
2837 */
David Sterbab63bb732007-12-06 23:40:33 -08002838 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002839 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 slave->link = BOND_LINK_DOWN;
2842 slave->state = BOND_STATE_BACKUP;
2843
2844 if (slave->link_failure_count < UINT_MAX) {
2845 slave->link_failure_count++;
2846 }
2847
2848 printk(KERN_INFO DRV_NAME
2849 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002850 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 slave->dev->name);
2852
2853 if (slave == oldcurrent) {
2854 do_failover = 1;
2855 }
2856 }
2857 }
2858
2859 /* note: if switch is in round-robin mode, all links
2860 * must tx arp to ensure all links rx an arp - otherwise
2861 * links may oscillate or not come up at all; if switch is
2862 * in something like xor mode, there is nothing we can
2863 * do - all replies will be rx'ed on same link causing slaves
2864 * to be unstable during low/no traffic periods
2865 */
2866 if (IS_UP(slave->dev)) {
2867 bond_arp_send_all(bond, slave);
2868 }
2869 }
2870
2871 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002872 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 bond_select_active_slave(bond);
2875
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002876 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
2879re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002880 if (bond->params.arp_interval)
2881 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882out:
2883 read_unlock(&bond->lock);
2884}
2885
2886/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002887 * Called to inspect slaves for active-backup mode ARP monitor link state
2888 * changes. Sets new_link in slaves to specify what action should take
2889 * place for the slave. Returns 0 if no changes are found, >0 if changes
2890 * to link states must be committed.
2891 *
2892 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002894static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002897 int i, commit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002900 slave->new_link = BOND_LINK_NOCHANGE;
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (slave->link != BOND_LINK_UP) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002903 if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2904 delta_in_ticks)) {
2905 slave->new_link = BOND_LINK_UP;
2906 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002909 continue;
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002912 /*
2913 * Give slaves 2*delta after being enslaved or made
2914 * active. This avoids bouncing, as the last receive
2915 * times need a full ARP monitor cycle to be updated.
2916 */
2917 if (!time_after_eq(jiffies, slave->jiffies +
2918 2 * delta_in_ticks))
2919 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002921 /*
2922 * Backup slave is down if:
2923 * - No current_arp_slave AND
2924 * - more than 3*delta since last receive AND
2925 * - the bond has an IP address
2926 *
2927 * Note: a non-null current_arp_slave indicates
2928 * the curr_active_slave went down and we are
2929 * searching for a new one; under this condition
2930 * we only take the curr_active_slave down - this
2931 * gives each slave a chance to tx/rx traffic
2932 * before being taken out
2933 */
2934 if (slave->state == BOND_STATE_BACKUP &&
2935 !bond->current_arp_slave &&
2936 time_after(jiffies, slave_last_rx(bond, slave) +
2937 3 * delta_in_ticks)) {
2938 slave->new_link = BOND_LINK_DOWN;
2939 commit++;
2940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002942 /*
2943 * Active slave is down if:
2944 * - more than 2*delta since transmitting OR
2945 * - (more than 2*delta since receive AND
2946 * the bond has an IP address)
2947 */
2948 if ((slave->state == BOND_STATE_ACTIVE) &&
2949 (time_after_eq(jiffies, slave->dev->trans_start +
2950 2 * delta_in_ticks) ||
2951 (time_after_eq(jiffies, slave_last_rx(bond, slave)
2952 + 2 * delta_in_ticks)))) {
2953 slave->new_link = BOND_LINK_DOWN;
2954 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956 }
2957
2958 read_lock(&bond->curr_slave_lock);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002959
2960 /*
2961 * Trigger a commit if the primary option setting has changed.
2962 */
2963 if (bond->primary_slave &&
2964 (bond->primary_slave != bond->curr_active_slave) &&
2965 (bond->primary_slave->link == BOND_LINK_UP))
2966 commit++;
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 read_unlock(&bond->curr_slave_lock);
2969
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002970 return commit;
2971}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002973/*
2974 * Called to commit link state changes noted by inspection step of
2975 * active-backup mode ARP monitor.
2976 *
2977 * Called with RTNL and bond->lock for read.
2978 */
2979static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2980{
2981 struct slave *slave;
2982 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002984 bond_for_each_slave(bond, slave, i) {
2985 switch (slave->new_link) {
2986 case BOND_LINK_NOCHANGE:
2987 continue;
2988
2989 case BOND_LINK_UP:
2990 write_lock_bh(&bond->curr_slave_lock);
2991
2992 if (!bond->curr_active_slave &&
2993 time_before_eq(jiffies, slave->dev->trans_start +
2994 delta_in_ticks)) {
2995 slave->link = BOND_LINK_UP;
2996 bond_change_active_slave(bond, slave);
2997 bond->current_arp_slave = NULL;
2998
2999 printk(KERN_INFO DRV_NAME
3000 ": %s: %s is up and now the "
3001 "active interface\n",
3002 bond->dev->name, slave->dev->name);
3003
3004 } else if (bond->curr_active_slave != slave) {
3005 /* this slave has just come up but we
3006 * already have a current slave; this can
3007 * also happen if bond_enslave adds a new
3008 * slave that is up while we are searching
3009 * for a new slave
3010 */
3011 slave->link = BOND_LINK_UP;
3012 bond_set_slave_inactive_flags(slave);
3013 bond->current_arp_slave = NULL;
3014
3015 printk(KERN_INFO DRV_NAME
3016 ": %s: backup interface %s is now up\n",
3017 bond->dev->name, slave->dev->name);
3018 }
3019
3020 write_unlock_bh(&bond->curr_slave_lock);
3021
3022 break;
3023
3024 case BOND_LINK_DOWN:
3025 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003028 slave->link = BOND_LINK_DOWN;
3029
3030 if (slave == bond->curr_active_slave) {
3031 printk(KERN_INFO DRV_NAME
3032 ": %s: link status down for active "
3033 "interface %s, disabling it\n",
3034 bond->dev->name, slave->dev->name);
3035
3036 bond_set_slave_inactive_flags(slave);
3037
3038 write_lock_bh(&bond->curr_slave_lock);
3039
3040 bond_select_active_slave(bond);
3041 if (bond->curr_active_slave)
3042 bond->curr_active_slave->jiffies =
3043 jiffies;
3044
3045 write_unlock_bh(&bond->curr_slave_lock);
3046
3047 bond->current_arp_slave = NULL;
3048
3049 } else if (slave->state == BOND_STATE_BACKUP) {
3050 printk(KERN_INFO DRV_NAME
3051 ": %s: backup interface %s is now down\n",
3052 bond->dev->name, slave->dev->name);
3053
3054 bond_set_slave_inactive_flags(slave);
3055 }
3056 break;
3057
3058 default:
3059 printk(KERN_ERR DRV_NAME
3060 ": %s: impossible: new_link %d on slave %s\n",
3061 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003066 /*
3067 * No race with changes to primary via sysfs, as we hold rtnl.
3068 */
3069 if (bond->primary_slave &&
3070 (bond->primary_slave != bond->curr_active_slave) &&
3071 (bond->primary_slave->link == BOND_LINK_UP)) {
3072 write_lock_bh(&bond->curr_slave_lock);
3073 bond_change_active_slave(bond, bond->primary_slave);
3074 write_unlock_bh(&bond->curr_slave_lock);
3075 }
3076
3077 bond_set_carrier(bond);
3078}
3079
3080/*
3081 * Send ARP probes for active-backup mode ARP monitor.
3082 *
3083 * Called with bond->lock held for read.
3084 */
3085static void bond_ab_arp_probe(struct bonding *bond)
3086{
3087 struct slave *slave;
3088 int i;
3089
3090 read_lock(&bond->curr_slave_lock);
3091
3092 if (bond->current_arp_slave && bond->curr_active_slave)
3093 printk("PROBE: c_arp %s && cas %s BAD\n",
3094 bond->current_arp_slave->dev->name,
3095 bond->curr_active_slave->dev->name);
3096
3097 if (bond->curr_active_slave) {
3098 bond_arp_send_all(bond, bond->curr_active_slave);
3099 read_unlock(&bond->curr_slave_lock);
3100 return;
3101 }
3102
3103 read_unlock(&bond->curr_slave_lock);
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 /* if we don't have a curr_active_slave, search for the next available
3106 * backup slave from the current_arp_slave and make it the candidate
3107 * for becoming the curr_active_slave
3108 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003109
3110 if (!bond->current_arp_slave) {
3111 bond->current_arp_slave = bond->first_slave;
3112 if (!bond->current_arp_slave)
3113 return;
3114 }
3115
3116 bond_set_slave_inactive_flags(bond->current_arp_slave);
3117
3118 /* search for next candidate */
3119 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3120 if (IS_UP(slave->dev)) {
3121 slave->link = BOND_LINK_BACK;
3122 bond_set_slave_active_flags(slave);
3123 bond_arp_send_all(bond, slave);
3124 slave->jiffies = jiffies;
3125 bond->current_arp_slave = slave;
3126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003129 /* if the link state is up at this point, we
3130 * mark it down - this can happen if we have
3131 * simultaneous link failures and
3132 * reselect_active_interface doesn't make this
3133 * one the current slave so it is still marked
3134 * up when it is actually down
3135 */
3136 if (slave->link == BOND_LINK_UP) {
3137 slave->link = BOND_LINK_DOWN;
3138 if (slave->link_failure_count < UINT_MAX)
3139 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003141 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003143 printk(KERN_INFO DRV_NAME
3144 ": %s: backup interface %s is now down.\n",
3145 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003148}
3149
3150void bond_activebackup_arp_mon(struct work_struct *work)
3151{
3152 struct bonding *bond = container_of(work, struct bonding,
3153 arp_work.work);
3154 int delta_in_ticks;
3155
3156 read_lock(&bond->lock);
3157
3158 if (bond->kill_timers)
3159 goto out;
3160
3161 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3162
3163 if (bond->slave_cnt == 0)
3164 goto re_arm;
3165
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003166 if (bond->send_grat_arp) {
3167 read_lock(&bond->curr_slave_lock);
3168 bond_send_gratuitous_arp(bond);
3169 read_unlock(&bond->curr_slave_lock);
3170 }
3171
Brian Haley305d5522008-11-04 17:51:14 -08003172 if (bond->send_unsol_na) {
3173 read_lock(&bond->curr_slave_lock);
3174 bond_send_unsolicited_na(bond);
3175 read_unlock(&bond->curr_slave_lock);
3176 }
3177
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003178 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3179 read_unlock(&bond->lock);
3180 rtnl_lock();
3181 read_lock(&bond->lock);
3182
3183 bond_ab_arp_commit(bond, delta_in_ticks);
3184
3185 read_unlock(&bond->lock);
3186 rtnl_unlock();
3187 read_lock(&bond->lock);
3188 }
3189
3190 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192re_arm:
3193 if (bond->params.arp_interval) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003194 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196out:
3197 read_unlock(&bond->lock);
3198}
3199
3200/*------------------------------ proc/seq_file-------------------------------*/
3201
3202#ifdef CONFIG_PROC_FS
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3205{
3206 struct bonding *bond = seq->private;
3207 loff_t off = 0;
3208 struct slave *slave;
3209 int i;
3210
3211 /* make sure the bond won't be taken away */
3212 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003213 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 if (*pos == 0) {
3216 return SEQ_START_TOKEN;
3217 }
3218
3219 bond_for_each_slave(bond, slave, i) {
3220 if (++off == *pos) {
3221 return slave;
3222 }
3223 }
3224
3225 return NULL;
3226}
3227
3228static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3229{
3230 struct bonding *bond = seq->private;
3231 struct slave *slave = v;
3232
3233 ++*pos;
3234 if (v == SEQ_START_TOKEN) {
3235 return bond->first_slave;
3236 }
3237
3238 slave = slave->next;
3239
3240 return (slave == bond->first_slave) ? NULL : slave;
3241}
3242
3243static void bond_info_seq_stop(struct seq_file *seq, void *v)
3244{
3245 struct bonding *bond = seq->private;
3246
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003247 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 read_unlock(&dev_base_lock);
3249}
3250
3251static void bond_info_show_master(struct seq_file *seq)
3252{
3253 struct bonding *bond = seq->private;
3254 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003255 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 read_lock(&bond->curr_slave_lock);
3258 curr = bond->curr_active_slave;
3259 read_unlock(&bond->curr_slave_lock);
3260
Jay Vosburghdd957c52007-10-09 19:57:24 -07003261 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 bond_mode_name(bond->params.mode));
3263
Jay Vosburghdd957c52007-10-09 19:57:24 -07003264 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3265 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003266 seq_printf(seq, " (fail_over_mac %s)",
3267 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003268
3269 seq_printf(seq, "\n");
3270
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003271 if (bond->params.mode == BOND_MODE_XOR ||
3272 bond->params.mode == BOND_MODE_8023AD) {
3273 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3274 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3275 bond->params.xmit_policy);
3276 }
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (USES_PRIMARY(bond->params.mode)) {
3279 seq_printf(seq, "Primary Slave: %s\n",
Mitch Williams0f418b22005-11-09 10:35:21 -08003280 (bond->primary_slave) ?
3281 bond->primary_slave->dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 seq_printf(seq, "Currently Active Slave: %s\n",
3284 (curr) ? curr->dev->name : "None");
3285 }
3286
Jay Vosburghff59c452006-03-27 13:27:43 -08003287 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3288 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3290 seq_printf(seq, "Up Delay (ms): %d\n",
3291 bond->params.updelay * bond->params.miimon);
3292 seq_printf(seq, "Down Delay (ms): %d\n",
3293 bond->params.downdelay * bond->params.miimon);
3294
Mitch Williams4756b022005-11-09 10:36:25 -08003295
3296 /* ARP information */
3297 if(bond->params.arp_interval > 0) {
3298 int printed=0;
3299 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3300 bond->params.arp_interval);
3301
3302 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3303
3304 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3305 if (!bond->params.arp_targets[i])
3306 continue;
3307 if (printed)
3308 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003309 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003310 printed = 1;
3311 }
3312 seq_printf(seq, "\n");
3313 }
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (bond->params.mode == BOND_MODE_8023AD) {
3316 struct ad_info ad_info;
3317
3318 seq_puts(seq, "\n802.3ad info\n");
3319 seq_printf(seq, "LACP rate: %s\n",
3320 (bond->params.lacp_fast) ? "fast" : "slow");
3321
3322 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3323 seq_printf(seq, "bond %s has no active aggregator\n",
3324 bond->dev->name);
3325 } else {
3326 seq_printf(seq, "Active Aggregator Info:\n");
3327
3328 seq_printf(seq, "\tAggregator ID: %d\n",
3329 ad_info.aggregator_id);
3330 seq_printf(seq, "\tNumber of ports: %d\n",
3331 ad_info.ports);
3332 seq_printf(seq, "\tActor Key: %d\n",
3333 ad_info.actor_key);
3334 seq_printf(seq, "\tPartner Key: %d\n",
3335 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003336 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3337 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
3339 }
3340}
3341
3342static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3343{
3344 struct bonding *bond = seq->private;
3345
3346 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3347 seq_printf(seq, "MII Status: %s\n",
3348 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003349 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 slave->link_failure_count);
3351
Johannes Berge1749612008-10-27 15:59:26 -07003352 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 if (bond->params.mode == BOND_MODE_8023AD) {
3355 const struct aggregator *agg
3356 = SLAVE_AD_INFO(slave).port.aggregator;
3357
3358 if (agg) {
3359 seq_printf(seq, "Aggregator ID: %d\n",
3360 agg->aggregator_identifier);
3361 } else {
3362 seq_puts(seq, "Aggregator ID: N/A\n");
3363 }
3364 }
3365}
3366
3367static int bond_info_seq_show(struct seq_file *seq, void *v)
3368{
3369 if (v == SEQ_START_TOKEN) {
3370 seq_printf(seq, "%s\n", version);
3371 bond_info_show_master(seq);
3372 } else {
3373 bond_info_show_slave(seq, v);
3374 }
3375
3376 return 0;
3377}
3378
3379static struct seq_operations bond_info_seq_ops = {
3380 .start = bond_info_seq_start,
3381 .next = bond_info_seq_next,
3382 .stop = bond_info_seq_stop,
3383 .show = bond_info_seq_show,
3384};
3385
3386static int bond_info_open(struct inode *inode, struct file *file)
3387{
3388 struct seq_file *seq;
3389 struct proc_dir_entry *proc;
3390 int res;
3391
3392 res = seq_open(file, &bond_info_seq_ops);
3393 if (!res) {
3394 /* recover the pointer buried in proc_dir_entry data */
3395 seq = file->private_data;
3396 proc = PDE(inode);
3397 seq->private = proc->data;
3398 }
3399
3400 return res;
3401}
3402
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003403static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 .owner = THIS_MODULE,
3405 .open = bond_info_open,
3406 .read = seq_read,
3407 .llseek = seq_lseek,
3408 .release = seq_release,
3409};
3410
3411static int bond_create_proc_entry(struct bonding *bond)
3412{
3413 struct net_device *bond_dev = bond->dev;
3414
3415 if (bond_proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003416 bond->proc_entry = proc_create_data(bond_dev->name,
3417 S_IRUGO, bond_proc_dir,
3418 &bond_info_fops, bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 if (bond->proc_entry == NULL) {
3420 printk(KERN_WARNING DRV_NAME
3421 ": Warning: Cannot create /proc/net/%s/%s\n",
3422 DRV_NAME, bond_dev->name);
3423 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3425 }
3426 }
3427
3428 return 0;
3429}
3430
3431static void bond_remove_proc_entry(struct bonding *bond)
3432{
3433 if (bond_proc_dir && bond->proc_entry) {
3434 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3435 memset(bond->proc_file_name, 0, IFNAMSIZ);
3436 bond->proc_entry = NULL;
3437 }
3438}
3439
3440/* Create the bonding directory under /proc/net, if doesn't exist yet.
3441 * Caller must hold rtnl_lock.
3442 */
3443static void bond_create_proc_dir(void)
3444{
3445 int len = strlen(DRV_NAME);
3446
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003447 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 bond_proc_dir = bond_proc_dir->next) {
3449 if ((bond_proc_dir->namelen == len) &&
3450 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3451 break;
3452 }
3453 }
3454
3455 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003456 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (bond_proc_dir) {
3458 bond_proc_dir->owner = THIS_MODULE;
3459 } else {
3460 printk(KERN_WARNING DRV_NAME
3461 ": Warning: cannot create /proc/net/%s\n",
3462 DRV_NAME);
3463 }
3464 }
3465}
3466
3467/* Destroy the bonding directory under /proc/net, if empty.
3468 * Caller must hold rtnl_lock.
3469 */
3470static void bond_destroy_proc_dir(void)
3471{
3472 struct proc_dir_entry *de;
3473
3474 if (!bond_proc_dir) {
3475 return;
3476 }
3477
3478 /* verify that the /proc dir is empty */
3479 for (de = bond_proc_dir->subdir; de; de = de->next) {
3480 /* ignore . and .. */
3481 if (*(de->name) != '.') {
3482 break;
3483 }
3484 }
3485
3486 if (de) {
3487 if (bond_proc_dir->owner == THIS_MODULE) {
3488 bond_proc_dir->owner = NULL;
3489 }
3490 } else {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003491 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 bond_proc_dir = NULL;
3493 }
3494}
3495#endif /* CONFIG_PROC_FS */
3496
3497/*-------------------------- netdev event handling --------------------------*/
3498
3499/*
3500 * Change device name
3501 */
3502static int bond_event_changename(struct bonding *bond)
3503{
3504#ifdef CONFIG_PROC_FS
3505 bond_remove_proc_entry(bond);
3506 bond_create_proc_entry(bond);
3507#endif
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003508 down_write(&(bonding_rwsem));
3509 bond_destroy_sysfs_entry(bond);
3510 bond_create_sysfs_entry(bond);
3511 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return NOTIFY_DONE;
3513}
3514
3515static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3516{
3517 struct bonding *event_bond = bond_dev->priv;
3518
3519 switch (event) {
3520 case NETDEV_CHANGENAME:
3521 return bond_event_changename(event_bond);
3522 case NETDEV_UNREGISTER:
Jay Vosburgh3b96c852008-01-17 16:25:00 -08003523 bond_release_all(event_bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 break;
3525 default:
3526 break;
3527 }
3528
3529 return NOTIFY_DONE;
3530}
3531
3532static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3533{
3534 struct net_device *bond_dev = slave_dev->master;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003535 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 switch (event) {
3538 case NETDEV_UNREGISTER:
3539 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003540 if (bond->setup_by_slave)
3541 bond_release_and_destroy(bond_dev, slave_dev);
3542 else
3543 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
3545 break;
3546 case NETDEV_CHANGE:
3547 /*
3548 * TODO: is this what we get if somebody
3549 * sets up a hierarchical bond, then rmmod's
3550 * one of the slave bonding devices?
3551 */
3552 break;
3553 case NETDEV_DOWN:
3554 /*
3555 * ... Or is it this?
3556 */
3557 break;
3558 case NETDEV_CHANGEMTU:
3559 /*
3560 * TODO: Should slaves be allowed to
3561 * independently alter their MTU? For
3562 * an active-backup bond, slaves need
3563 * not be the same type of device, so
3564 * MTUs may vary. For other modes,
3565 * slaves arguably should have the
3566 * same MTUs. To do this, we'd need to
3567 * take over the slave's change_mtu
3568 * function for the duration of their
3569 * servitude.
3570 */
3571 break;
3572 case NETDEV_CHANGENAME:
3573 /*
3574 * TODO: handle changing the primary's name
3575 */
3576 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003577 case NETDEV_FEAT_CHANGE:
3578 bond_compute_features(bond);
3579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 default:
3581 break;
3582 }
3583
3584 return NOTIFY_DONE;
3585}
3586
3587/*
3588 * bond_netdev_event: handle netdev notifier chain events.
3589 *
3590 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003591 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 * locks for us to safely manipulate the slave devices (RTNL lock,
3593 * dev_probe_lock).
3594 */
3595static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3596{
3597 struct net_device *event_dev = (struct net_device *)ptr;
3598
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003599 if (dev_net(event_dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003600 return NOTIFY_DONE;
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 dprintk("event_dev: %s, event: %lx\n",
3603 (event_dev ? event_dev->name : "None"),
3604 event);
3605
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003606 if (!(event_dev->priv_flags & IFF_BONDING))
3607 return NOTIFY_DONE;
3608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (event_dev->flags & IFF_MASTER) {
3610 dprintk("IFF_MASTER\n");
3611 return bond_master_netdev_event(event, event_dev);
3612 }
3613
3614 if (event_dev->flags & IFF_SLAVE) {
3615 dprintk("IFF_SLAVE\n");
3616 return bond_slave_netdev_event(event, event_dev);
3617 }
3618
3619 return NOTIFY_DONE;
3620}
3621
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003622/*
3623 * bond_inetaddr_event: handle inetaddr notifier chain events.
3624 *
3625 * We keep track of device IPs primarily to use as source addresses in
3626 * ARP monitor probes (rather than spewing out broadcasts all the time).
3627 *
3628 * We track one IP for the main device (if it has one), plus one per VLAN.
3629 */
3630static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3631{
3632 struct in_ifaddr *ifa = ptr;
3633 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003634 struct bonding *bond;
3635 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003636
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003637 if (dev_net(ifa->ifa_dev->dev) != &init_net)
Denis V. Lunev6133fb12008-02-28 20:46:17 -08003638 return NOTIFY_DONE;
3639
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003640 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003641 if (bond->dev == event_dev) {
3642 switch (event) {
3643 case NETDEV_UP:
3644 bond->master_ip = ifa->ifa_local;
3645 return NOTIFY_OK;
3646 case NETDEV_DOWN:
3647 bond->master_ip = bond_glean_dev_ip(bond->dev);
3648 return NOTIFY_OK;
3649 default:
3650 return NOTIFY_DONE;
3651 }
3652 }
3653
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003654 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003655 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003656 if (vlan_dev == event_dev) {
3657 switch (event) {
3658 case NETDEV_UP:
3659 vlan->vlan_ip = ifa->ifa_local;
3660 return NOTIFY_OK;
3661 case NETDEV_DOWN:
3662 vlan->vlan_ip =
3663 bond_glean_dev_ip(vlan_dev);
3664 return NOTIFY_OK;
3665 default:
3666 return NOTIFY_DONE;
3667 }
3668 }
3669 }
3670 }
3671 return NOTIFY_DONE;
3672}
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674static struct notifier_block bond_netdev_notifier = {
3675 .notifier_call = bond_netdev_event,
3676};
3677
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003678static struct notifier_block bond_inetaddr_notifier = {
3679 .notifier_call = bond_inetaddr_event,
3680};
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682/*-------------------------- Packet type handling ---------------------------*/
3683
3684/* register to receive lacpdus on a bond */
3685static void bond_register_lacpdu(struct bonding *bond)
3686{
3687 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3688
3689 /* initialize packet type */
3690 pk_type->type = PKT_TYPE_LACPDU;
3691 pk_type->dev = bond->dev;
3692 pk_type->func = bond_3ad_lacpdu_recv;
3693
3694 dev_add_pack(pk_type);
3695}
3696
3697/* unregister to receive lacpdus on a bond */
3698static void bond_unregister_lacpdu(struct bonding *bond)
3699{
3700 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3701}
3702
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003703void bond_register_arp(struct bonding *bond)
3704{
3705 struct packet_type *pt = &bond->arp_mon_pt;
3706
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003707 if (pt->type)
3708 return;
3709
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003710 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003711 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003712 pt->func = bond_arp_rcv;
3713 dev_add_pack(pt);
3714}
3715
3716void bond_unregister_arp(struct bonding *bond)
3717{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003718 struct packet_type *pt = &bond->arp_mon_pt;
3719
3720 dev_remove_pack(pt);
3721 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003722}
3723
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003724/*---------------------------- Hashing Policies -----------------------------*/
3725
3726/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003727 * Hash for the output device based upon layer 2 and layer 3 data. If
3728 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3729 */
3730static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3731 struct net_device *bond_dev, int count)
3732{
3733 struct ethhdr *data = (struct ethhdr *)skb->data;
3734 struct iphdr *iph = ip_hdr(skb);
3735
Brian Haleyf14c4e42008-09-02 10:08:08 -04003736 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003737 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3738 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3739 }
3740
3741 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3742}
3743
3744/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003745 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003746 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3747 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3748 */
3749static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3750 struct net_device *bond_dev, int count)
3751{
3752 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003753 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003754 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003755 int layer4_xor = 0;
3756
Brian Haleyf14c4e42008-09-02 10:08:08 -04003757 if (skb->protocol == htons(ETH_P_IP)) {
3758 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003759 (iph->protocol == IPPROTO_TCP ||
3760 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003761 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003762 }
3763 return (layer4_xor ^
3764 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3765
3766 }
3767
3768 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3769}
3770
3771/*
3772 * Hash for the output device based upon layer 2 data
3773 */
3774static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3775 struct net_device *bond_dev, int count)
3776{
3777 struct ethhdr *data = (struct ethhdr *)skb->data;
3778
3779 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3780}
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782/*-------------------------- Device entry points ----------------------------*/
3783
3784static int bond_open(struct net_device *bond_dev)
3785{
3786 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 bond->kill_timers = 0;
3789
3790 if ((bond->params.mode == BOND_MODE_TLB) ||
3791 (bond->params.mode == BOND_MODE_ALB)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /* bond_alb_initialize must be called before the timer
3793 * is started.
3794 */
3795 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3796 /* something went wrong - fail the open operation */
3797 return -1;
3798 }
3799
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003800 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3801 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803
3804 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003805 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3806 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808
3809 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003810 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3811 INIT_DELAYED_WORK(&bond->arp_work,
3812 bond_activebackup_arp_mon);
3813 else
3814 INIT_DELAYED_WORK(&bond->arp_work,
3815 bond_loadbalance_arp_mon);
3816
3817 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003818 if (bond->params.arp_validate)
3819 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 }
3821
3822 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003823 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003824 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 /* register to receive LACPDUs */
3826 bond_register_lacpdu(bond);
3827 }
3828
3829 return 0;
3830}
3831
3832static int bond_close(struct net_device *bond_dev)
3833{
3834 struct bonding *bond = bond_dev->priv;
3835
3836 if (bond->params.mode == BOND_MODE_8023AD) {
3837 /* Unregister the receive of LACPDUs */
3838 bond_unregister_lacpdu(bond);
3839 }
3840
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003841 if (bond->params.arp_validate)
3842 bond_unregister_arp(bond);
3843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 write_lock_bh(&bond->lock);
3845
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003846 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003847 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
3849 /* signal timers not to re-arm */
3850 bond->kill_timers = 1;
3851
3852 write_unlock_bh(&bond->lock);
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003855 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857
3858 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003859 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 }
3861
3862 switch (bond->params.mode) {
3863 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003864 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 break;
3866 case BOND_MODE_TLB:
3867 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003868 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 break;
3870 default:
3871 break;
3872 }
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 if ((bond->params.mode == BOND_MODE_TLB) ||
3876 (bond->params.mode == BOND_MODE_ALB)) {
3877 /* Must be called only after all
3878 * slaves have been released
3879 */
3880 bond_alb_deinitialize(bond);
3881 }
3882
3883 return 0;
3884}
3885
3886static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3887{
3888 struct bonding *bond = bond_dev->priv;
3889 struct net_device_stats *stats = &(bond->stats), *sstats;
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003890 struct net_device_stats local_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 struct slave *slave;
3892 int i;
3893
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003894 memset(&local_stats, 0, sizeof(struct net_device_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 read_lock_bh(&bond->lock);
3897
3898 bond_for_each_slave(bond, slave, i) {
Rusty Russellc45d2862007-03-28 14:29:08 -07003899 sstats = slave->dev->get_stats(slave->dev);
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003900 local_stats.rx_packets += sstats->rx_packets;
3901 local_stats.rx_bytes += sstats->rx_bytes;
3902 local_stats.rx_errors += sstats->rx_errors;
3903 local_stats.rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003905 local_stats.tx_packets += sstats->tx_packets;
3906 local_stats.tx_bytes += sstats->tx_bytes;
3907 local_stats.tx_errors += sstats->tx_errors;
3908 local_stats.tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003910 local_stats.multicast += sstats->multicast;
3911 local_stats.collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003913 local_stats.rx_length_errors += sstats->rx_length_errors;
3914 local_stats.rx_over_errors += sstats->rx_over_errors;
3915 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3916 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3917 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3918 local_stats.rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003920 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3921 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3922 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3923 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3924 local_stats.tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
3926
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003927 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 read_unlock_bh(&bond->lock);
3930
3931 return stats;
3932}
3933
3934static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3935{
3936 struct net_device *slave_dev = NULL;
3937 struct ifbond k_binfo;
3938 struct ifbond __user *u_binfo = NULL;
3939 struct ifslave k_sinfo;
3940 struct ifslave __user *u_sinfo = NULL;
3941 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 int res = 0;
3943
3944 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3945 bond_dev->name, cmd);
3946
3947 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 case SIOCGMIIPHY:
3949 mii = if_mii(ifr);
3950 if (!mii) {
3951 return -EINVAL;
3952 }
3953 mii->phy_id = 0;
3954 /* Fall Through */
3955 case SIOCGMIIREG:
3956 /*
3957 * We do this again just in case we were called by SIOCGMIIREG
3958 * instead of SIOCGMIIPHY.
3959 */
3960 mii = if_mii(ifr);
3961 if (!mii) {
3962 return -EINVAL;
3963 }
3964
3965 if (mii->reg_num == 1) {
3966 struct bonding *bond = bond_dev->priv;
3967 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003968 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 read_lock(&bond->curr_slave_lock);
Andy Gospodarek4e140072006-12-04 15:04:54 -08003970 if (netif_carrier_ok(bond->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 mii->val_out = BMSR_LSTATUS;
3972 }
3973 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003974 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976
3977 return 0;
3978 case BOND_INFO_QUERY_OLD:
3979 case SIOCBONDINFOQUERY:
3980 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3981
3982 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3983 return -EFAULT;
3984 }
3985
3986 res = bond_info_query(bond_dev, &k_binfo);
3987 if (res == 0) {
3988 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3989 return -EFAULT;
3990 }
3991 }
3992
3993 return res;
3994 case BOND_SLAVE_INFO_QUERY_OLD:
3995 case SIOCBONDSLAVEINFOQUERY:
3996 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3997
3998 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3999 return -EFAULT;
4000 }
4001
4002 res = bond_slave_info_query(bond_dev, &k_sinfo);
4003 if (res == 0) {
4004 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
4005 return -EFAULT;
4006 }
4007 }
4008
4009 return res;
4010 default:
4011 /* Go on */
4012 break;
4013 }
4014
4015 if (!capable(CAP_NET_ADMIN)) {
4016 return -EPERM;
4017 }
4018
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004019 down_write(&(bonding_rwsem));
Eric W. Biederman881d9662007-09-17 11:56:21 -07004020 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 dprintk("slave_dev=%p: \n", slave_dev);
4023
4024 if (!slave_dev) {
4025 res = -ENODEV;
4026 } else {
4027 dprintk("slave_dev->name=%s: \n", slave_dev->name);
4028 switch (cmd) {
4029 case BOND_ENSLAVE_OLD:
4030 case SIOCBONDENSLAVE:
4031 res = bond_enslave(bond_dev, slave_dev);
4032 break;
4033 case BOND_RELEASE_OLD:
4034 case SIOCBONDRELEASE:
4035 res = bond_release(bond_dev, slave_dev);
4036 break;
4037 case BOND_SETHWADDR_OLD:
4038 case SIOCBONDSETHWADDR:
4039 res = bond_sethwaddr(bond_dev, slave_dev);
4040 break;
4041 case BOND_CHANGE_ACTIVE_OLD:
4042 case SIOCBONDCHANGEACTIVE:
4043 res = bond_ioctl_change_active(bond_dev, slave_dev);
4044 break;
4045 default:
4046 res = -EOPNOTSUPP;
4047 }
4048
4049 dev_put(slave_dev);
4050 }
4051
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004052 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return res;
4054}
4055
4056static void bond_set_multicast_list(struct net_device *bond_dev)
4057{
4058 struct bonding *bond = bond_dev->priv;
4059 struct dev_mc_list *dmi;
4060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 /*
4062 * Do promisc before checking multicast_mode
4063 */
4064 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004065 /*
4066 * FIXME: Need to handle the error when one of the multi-slaves
4067 * encounters error.
4068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 bond_set_promiscuity(bond, 1);
4070 }
4071
4072 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
4073 bond_set_promiscuity(bond, -1);
4074 }
4075
4076 /* set allmulti flag to slaves */
4077 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004078 /*
4079 * FIXME: Need to handle the error when one of the multi-slaves
4080 * encounters error.
4081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 bond_set_allmulti(bond, 1);
4083 }
4084
4085 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
4086 bond_set_allmulti(bond, -1);
4087 }
4088
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004089 read_lock(&bond->lock);
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 bond->flags = bond_dev->flags;
4092
4093 /* looking for addresses to add to slaves' mc list */
4094 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
4095 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
4096 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4097 }
4098 }
4099
4100 /* looking for addresses to delete from slaves' list */
4101 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
4102 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
4103 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4104 }
4105 }
4106
4107 /* save master's multicast list */
4108 bond_mc_list_destroy(bond);
4109 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4110
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004111 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
4114/*
4115 * Change the MTU of all of a master's slaves to match the master
4116 */
4117static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4118{
4119 struct bonding *bond = bond_dev->priv;
4120 struct slave *slave, *stop_at;
4121 int res = 0;
4122 int i;
4123
4124 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
4125 (bond_dev ? bond_dev->name : "None"), new_mtu);
4126
4127 /* Can't hold bond->lock with bh disabled here since
4128 * some base drivers panic. On the other hand we can't
4129 * hold bond->lock without bh disabled because we'll
4130 * deadlock. The only solution is to rely on the fact
4131 * that we're under rtnl_lock here, and the slaves
4132 * list won't change. This doesn't solve the problem
4133 * of setting the slave's MTU while it is
4134 * transmitting, but the assumption is that the base
4135 * driver can handle that.
4136 *
4137 * TODO: figure out a way to safely iterate the slaves
4138 * list, but without holding a lock around the actual
4139 * call to the base driver.
4140 */
4141
4142 bond_for_each_slave(bond, slave, i) {
4143 dprintk("s %p s->p %p c_m %p\n", slave,
4144 slave->prev, slave->dev->change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 res = dev_set_mtu(slave->dev, new_mtu);
4147
4148 if (res) {
4149 /* If we failed to set the slave's mtu to the new value
4150 * we must abort the operation even in ACTIVE_BACKUP
4151 * mode, because if we allow the backup slaves to have
4152 * different mtu values than the active slave we'll
4153 * need to change their mtu when doing a failover. That
4154 * means changing their mtu from timer context, which
4155 * is probably not a good idea.
4156 */
4157 dprintk("err %d %s\n", res, slave->dev->name);
4158 goto unwind;
4159 }
4160 }
4161
4162 bond_dev->mtu = new_mtu;
4163
4164 return 0;
4165
4166unwind:
4167 /* unwind from head to the slave that failed */
4168 stop_at = slave;
4169 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4170 int tmp_res;
4171
4172 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4173 if (tmp_res) {
4174 dprintk("unwind err %d dev %s\n", tmp_res,
4175 slave->dev->name);
4176 }
4177 }
4178
4179 return res;
4180}
4181
4182/*
4183 * Change HW address
4184 *
4185 * Note that many devices must be down to change the HW address, and
4186 * downing the master releases all slaves. We can make bonds full of
4187 * bonding devices to test this, however.
4188 */
4189static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4190{
4191 struct bonding *bond = bond_dev->priv;
4192 struct sockaddr *sa = addr, tmp_sa;
4193 struct slave *slave, *stop_at;
4194 int res = 0;
4195 int i;
4196
4197 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4198
Jay Vosburghdd957c52007-10-09 19:57:24 -07004199 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004200 * If fail_over_mac is set to active, do nothing and return
4201 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004202 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004203 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004204 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 if (!is_valid_ether_addr(sa->sa_data)) {
4207 return -EADDRNOTAVAIL;
4208 }
4209
4210 /* Can't hold bond->lock with bh disabled here since
4211 * some base drivers panic. On the other hand we can't
4212 * hold bond->lock without bh disabled because we'll
4213 * deadlock. The only solution is to rely on the fact
4214 * that we're under rtnl_lock here, and the slaves
4215 * list won't change. This doesn't solve the problem
4216 * of setting the slave's hw address while it is
4217 * transmitting, but the assumption is that the base
4218 * driver can handle that.
4219 *
4220 * TODO: figure out a way to safely iterate the slaves
4221 * list, but without holding a lock around the actual
4222 * call to the base driver.
4223 */
4224
4225 bond_for_each_slave(bond, slave, i) {
4226 dprintk("slave %p %s\n", slave, slave->dev->name);
4227
4228 if (slave->dev->set_mac_address == NULL) {
4229 res = -EOPNOTSUPP;
4230 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4231 goto unwind;
4232 }
4233
4234 res = dev_set_mac_address(slave->dev, addr);
4235 if (res) {
4236 /* TODO: consider downing the slave
4237 * and retry ?
4238 * User should expect communications
4239 * breakage anyway until ARP finish
4240 * updating, so...
4241 */
4242 dprintk("err %d %s\n", res, slave->dev->name);
4243 goto unwind;
4244 }
4245 }
4246
4247 /* success */
4248 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4249 return 0;
4250
4251unwind:
4252 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4253 tmp_sa.sa_family = bond_dev->type;
4254
4255 /* unwind from head to the slave that failed */
4256 stop_at = slave;
4257 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4258 int tmp_res;
4259
4260 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4261 if (tmp_res) {
4262 dprintk("unwind err %d dev %s\n", tmp_res,
4263 slave->dev->name);
4264 }
4265 }
4266
4267 return res;
4268}
4269
4270static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4271{
4272 struct bonding *bond = bond_dev->priv;
4273 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004274 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 read_lock(&bond->lock);
4277
4278 if (!BOND_IS_OK(bond)) {
4279 goto out;
4280 }
4281
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004282 /*
4283 * Concurrent TX may collide on rr_tx_counter; we accept that
4284 * as being rare enough not to justify using an atomic op here
4285 */
4286 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004288 bond_for_each_slave(bond, slave, i) {
4289 slave_no--;
4290 if (slave_no < 0) {
4291 break;
4292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004295 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 bond_for_each_slave_from(bond, slave, i, start_at) {
4297 if (IS_UP(slave->dev) &&
4298 (slave->link == BOND_LINK_UP) &&
4299 (slave->state == BOND_STATE_ACTIVE)) {
4300 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 break;
4302 }
4303 }
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305out:
4306 if (res) {
4307 /* no suitable interface, frame not sent */
4308 dev_kfree_skb(skb);
4309 }
4310 read_unlock(&bond->lock);
4311 return 0;
4312}
4313
John W. Linville075897c2005-09-28 17:50:53 -04004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315/*
4316 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4317 * the bond has a usable interface.
4318 */
4319static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4320{
4321 struct bonding *bond = bond_dev->priv;
4322 int res = 1;
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 read_lock(&bond->lock);
4325 read_lock(&bond->curr_slave_lock);
4326
4327 if (!BOND_IS_OK(bond)) {
4328 goto out;
4329 }
4330
John W. Linville075897c2005-09-28 17:50:53 -04004331 if (!bond->curr_active_slave)
4332 goto out;
4333
John W. Linville075897c2005-09-28 17:50:53 -04004334 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336out:
4337 if (res) {
4338 /* no suitable interface, frame not sent */
4339 dev_kfree_skb(skb);
4340 }
4341 read_unlock(&bond->curr_slave_lock);
4342 read_unlock(&bond->lock);
4343 return 0;
4344}
4345
4346/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004347 * In bond_xmit_xor() , we determine the output device by using a pre-
4348 * determined xmit_hash_policy(), If the selected device is not enabled,
4349 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 */
4351static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4352{
4353 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 struct slave *slave, *start_at;
4355 int slave_no;
4356 int i;
4357 int res = 1;
4358
4359 read_lock(&bond->lock);
4360
4361 if (!BOND_IS_OK(bond)) {
4362 goto out;
4363 }
4364
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004365 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
4367 bond_for_each_slave(bond, slave, i) {
4368 slave_no--;
4369 if (slave_no < 0) {
4370 break;
4371 }
4372 }
4373
4374 start_at = slave;
4375
4376 bond_for_each_slave_from(bond, slave, i, start_at) {
4377 if (IS_UP(slave->dev) &&
4378 (slave->link == BOND_LINK_UP) &&
4379 (slave->state == BOND_STATE_ACTIVE)) {
4380 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4381 break;
4382 }
4383 }
4384
4385out:
4386 if (res) {
4387 /* no suitable interface, frame not sent */
4388 dev_kfree_skb(skb);
4389 }
4390 read_unlock(&bond->lock);
4391 return 0;
4392}
4393
4394/*
4395 * in broadcast mode, we send everything to all usable interfaces.
4396 */
4397static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4398{
4399 struct bonding *bond = bond_dev->priv;
4400 struct slave *slave, *start_at;
4401 struct net_device *tx_dev = NULL;
4402 int i;
4403 int res = 1;
4404
4405 read_lock(&bond->lock);
4406
4407 if (!BOND_IS_OK(bond)) {
4408 goto out;
4409 }
4410
4411 read_lock(&bond->curr_slave_lock);
4412 start_at = bond->curr_active_slave;
4413 read_unlock(&bond->curr_slave_lock);
4414
4415 if (!start_at) {
4416 goto out;
4417 }
4418
4419 bond_for_each_slave_from(bond, slave, i, start_at) {
4420 if (IS_UP(slave->dev) &&
4421 (slave->link == BOND_LINK_UP) &&
4422 (slave->state == BOND_STATE_ACTIVE)) {
4423 if (tx_dev) {
4424 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4425 if (!skb2) {
4426 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004427 ": %s: Error: bond_xmit_broadcast(): "
4428 "skb_clone() failed\n",
4429 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 continue;
4431 }
4432
4433 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4434 if (res) {
4435 dev_kfree_skb(skb2);
4436 continue;
4437 }
4438 }
4439 tx_dev = slave->dev;
4440 }
4441 }
4442
4443 if (tx_dev) {
4444 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4445 }
4446
4447out:
4448 if (res) {
4449 /* no suitable interface, frame not sent */
4450 dev_kfree_skb(skb);
4451 }
4452 /* frame sent to all suitable interfaces */
4453 read_unlock(&bond->lock);
4454 return 0;
4455}
4456
4457/*------------------------- Device initialization ---------------------------*/
4458
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004459static void bond_set_xmit_hash_policy(struct bonding *bond)
4460{
4461 switch (bond->params.xmit_policy) {
4462 case BOND_XMIT_POLICY_LAYER23:
4463 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4464 break;
4465 case BOND_XMIT_POLICY_LAYER34:
4466 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4467 break;
4468 case BOND_XMIT_POLICY_LAYER2:
4469 default:
4470 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4471 break;
4472 }
4473}
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475/*
4476 * set bond mode specific net device operations
4477 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004478void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004480 struct net_device *bond_dev = bond->dev;
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 switch (mode) {
4483 case BOND_MODE_ROUNDROBIN:
4484 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4485 break;
4486 case BOND_MODE_ACTIVEBACKUP:
4487 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4488 break;
4489 case BOND_MODE_XOR:
4490 bond_dev->hard_start_xmit = bond_xmit_xor;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004491 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 break;
4493 case BOND_MODE_BROADCAST:
4494 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4495 break;
4496 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004497 bond_set_master_3ad_flags(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004499 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004502 bond_set_master_alb_flags(bond);
4503 /* FALLTHRU */
4504 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 bond_dev->hard_start_xmit = bond_alb_xmit;
4506 bond_dev->set_mac_address = bond_alb_set_mac_address;
4507 break;
4508 default:
4509 /* Should never happen, mode already checked */
4510 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004511 ": %s: Error: Unknown bonding mode %d\n",
4512 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 mode);
4514 break;
4515 }
4516}
4517
Jay Vosburgh217df672005-09-26 16:11:50 -07004518static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4519 struct ethtool_drvinfo *drvinfo)
4520{
4521 strncpy(drvinfo->driver, DRV_NAME, 32);
4522 strncpy(drvinfo->version, DRV_VERSION, 32);
4523 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4524}
4525
Jeff Garzik7282d492006-09-13 14:30:00 -04004526static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004527 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004528 .get_link = ethtool_op_get_link,
4529 .get_tx_csum = ethtool_op_get_tx_csum,
4530 .get_sg = ethtool_op_get_sg,
4531 .get_tso = ethtool_op_get_tso,
4532 .get_ufo = ethtool_op_get_ufo,
4533 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004534};
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536/*
4537 * Does not allocate but creates a /proc entry.
4538 * Allowed to fail.
4539 */
Mitch Williams3c535952005-11-09 10:36:11 -08004540static int bond_init(struct net_device *bond_dev, struct bond_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
4542 struct bonding *bond = bond_dev->priv;
4543
4544 dprintk("Begin bond_init for %s\n", bond_dev->name);
4545
4546 /* initialize rwlocks */
4547 rwlock_init(&bond->lock);
4548 rwlock_init(&bond->curr_slave_lock);
4549
4550 bond->params = *params; /* copy params struct */
4551
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004552 bond->wq = create_singlethread_workqueue(bond_dev->name);
4553 if (!bond->wq)
4554 return -ENOMEM;
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /* Initialize pointers */
4557 bond->first_slave = NULL;
4558 bond->curr_active_slave = NULL;
4559 bond->current_arp_slave = NULL;
4560 bond->primary_slave = NULL;
4561 bond->dev = bond_dev;
Moni Shoua1053f622007-10-09 19:43:42 -07004562 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08004563 bond->send_unsol_na = 0;
Moni Shouad90a1622007-10-09 19:43:43 -07004564 bond->setup_by_slave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 INIT_LIST_HEAD(&bond->vlan_list);
4566
4567 /* Initialize the device entry points */
4568 bond_dev->open = bond_open;
4569 bond_dev->stop = bond_close;
4570 bond_dev->get_stats = bond_get_stats;
4571 bond_dev->do_ioctl = bond_do_ioctl;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004572 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 bond_dev->set_multicast_list = bond_set_multicast_list;
4574 bond_dev->change_mtu = bond_change_mtu;
4575 bond_dev->set_mac_address = bond_set_mac_address;
Jay Vosburgh3a1521b2007-11-06 13:33:29 -08004576 bond_dev->validate_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004578 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Jay Vosburgha434e432008-10-30 17:41:15 -07004580 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 /* Initialize the device options */
4583 bond_dev->tx_queue_len = 0;
4584 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004585 bond_dev->priv_flags |= IFF_BONDING;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004586 if (bond->params.arp_interval)
4587 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
4589 /* At first, we block adding VLANs. That's the only way to
4590 * prevent problems that occur when adding VLANs over an
4591 * empty bond. The block will be removed once non-challenged
4592 * slaves are enslaved.
4593 */
4594 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4595
Herbert Xu932ff272006-06-09 12:20:56 -07004596 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 * transmitting */
4598 bond_dev->features |= NETIF_F_LLTX;
4599
4600 /* By default, we declare the bond to be fully
4601 * VLAN hardware accelerated capable. Special
4602 * care is taken in the various xmit functions
4603 * when there are slaves that are not hw accel
4604 * capable
4605 */
4606 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4607 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4608 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4609 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4610 NETIF_F_HW_VLAN_RX |
4611 NETIF_F_HW_VLAN_FILTER);
4612
4613#ifdef CONFIG_PROC_FS
4614 bond_create_proc_entry(bond);
4615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 list_add_tail(&bond->bond_list, &bond_dev_list);
4617
4618 return 0;
4619}
4620
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004621static void bond_work_cancel_all(struct bonding *bond)
4622{
4623 write_lock_bh(&bond->lock);
4624 bond->kill_timers = 1;
4625 write_unlock_bh(&bond->lock);
4626
4627 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4628 cancel_delayed_work(&bond->mii_work);
4629
4630 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4631 cancel_delayed_work(&bond->arp_work);
4632
4633 if (bond->params.mode == BOND_MODE_ALB &&
4634 delayed_work_pending(&bond->alb_work))
4635 cancel_delayed_work(&bond->alb_work);
4636
4637 if (bond->params.mode == BOND_MODE_8023AD &&
4638 delayed_work_pending(&bond->ad_work))
4639 cancel_delayed_work(&bond->ad_work);
4640}
4641
Jay Vosburgha434e432008-10-30 17:41:15 -07004642/* De-initialize device specific data.
4643 * Caller must hold rtnl_lock.
4644 */
4645static void bond_deinit(struct net_device *bond_dev)
4646{
4647 struct bonding *bond = bond_dev->priv;
4648
4649 list_del(&bond->bond_list);
4650
4651 bond_work_cancel_all(bond);
4652
4653#ifdef CONFIG_PROC_FS
4654 bond_remove_proc_entry(bond);
4655#endif
4656}
4657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658/* Unregister and free all bond devices.
4659 * Caller must hold rtnl_lock.
4660 */
4661static void bond_free_all(void)
4662{
4663 struct bonding *bond, *nxt;
4664
4665 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4666 struct net_device *bond_dev = bond->dev;
4667
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004668 bond_work_cancel_all(bond);
jamal702987052006-09-22 21:54:37 -07004669 /* Release the bonded slaves */
4670 bond_release_all(bond_dev);
Libor Pechacek92b41da2008-03-21 22:29:35 -07004671 bond_destroy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 }
4673
4674#ifdef CONFIG_PROC_FS
4675 bond_destroy_proc_dir();
4676#endif
4677}
4678
4679/*------------------------- Module initialization ---------------------------*/
4680
4681/*
4682 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004683 * number of the mode or its string name. A bit complicated because
4684 * some mode names are substrings of other names, and calls from sysfs
4685 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 */
Jay Vosburghece95f72008-01-17 16:25:01 -08004687int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Jay Vosburghece95f72008-01-17 16:25:01 -08004689 int mode = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004690 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004691
Jay Vosburgha42e5342008-01-29 18:07:43 -08004692 for (p = (char *)buf; *p; p++)
4693 if (!(isdigit(*p) || isspace(*p)))
4694 break;
4695
4696 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004697 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004698 else
4699 rv = sscanf(buf, "%d", &mode);
4700
4701 if (!rv)
4702 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 for (i = 0; tbl[i].modename; i++) {
Jay Vosburghece95f72008-01-17 16:25:01 -08004705 if (mode == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004707 if (strcmp(modestr, tbl[i].modename) == 0)
4708 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
4710
4711 return -1;
4712}
4713
4714static int bond_check_params(struct bond_params *params)
4715{
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004716 int arp_validate_value, fail_over_mac_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 /*
4719 * Convert string parameters.
4720 */
4721 if (mode) {
4722 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4723 if (bond_mode == -1) {
4724 printk(KERN_ERR DRV_NAME
4725 ": Error: Invalid bonding mode \"%s\"\n",
4726 mode == NULL ? "NULL" : mode);
4727 return -EINVAL;
4728 }
4729 }
4730
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004731 if (xmit_hash_policy) {
4732 if ((bond_mode != BOND_MODE_XOR) &&
4733 (bond_mode != BOND_MODE_8023AD)) {
4734 printk(KERN_INFO DRV_NAME
4735 ": xor_mode param is irrelevant in mode %s\n",
4736 bond_mode_name(bond_mode));
4737 } else {
4738 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4739 xmit_hashtype_tbl);
4740 if (xmit_hashtype == -1) {
4741 printk(KERN_ERR DRV_NAME
4742 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4743 xmit_hash_policy == NULL ? "NULL" :
4744 xmit_hash_policy);
4745 return -EINVAL;
4746 }
4747 }
4748 }
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 if (lacp_rate) {
4751 if (bond_mode != BOND_MODE_8023AD) {
4752 printk(KERN_INFO DRV_NAME
4753 ": lacp_rate param is irrelevant in mode %s\n",
4754 bond_mode_name(bond_mode));
4755 } else {
4756 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4757 if (lacp_fast == -1) {
4758 printk(KERN_ERR DRV_NAME
4759 ": Error: Invalid lacp rate \"%s\"\n",
4760 lacp_rate == NULL ? "NULL" : lacp_rate);
4761 return -EINVAL;
4762 }
4763 }
4764 }
4765
Jay Vosburghb8a97872008-06-13 18:12:04 -07004766 if (max_bonds < 0 || max_bonds > INT_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 printk(KERN_WARNING DRV_NAME
4768 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004769 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Jay Vosburghb8a97872008-06-13 18:12:04 -07004770 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 max_bonds = BOND_DEFAULT_MAX_BONDS;
4772 }
4773
4774 if (miimon < 0) {
4775 printk(KERN_WARNING DRV_NAME
4776 ": Warning: miimon module parameter (%d), "
4777 "not in range 0-%d, so it was reset to %d\n",
4778 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4779 miimon = BOND_LINK_MON_INTERV;
4780 }
4781
4782 if (updelay < 0) {
4783 printk(KERN_WARNING DRV_NAME
4784 ": Warning: updelay module parameter (%d), "
4785 "not in range 0-%d, so it was reset to 0\n",
4786 updelay, INT_MAX);
4787 updelay = 0;
4788 }
4789
4790 if (downdelay < 0) {
4791 printk(KERN_WARNING DRV_NAME
4792 ": Warning: downdelay module parameter (%d), "
4793 "not in range 0-%d, so it was reset to 0\n",
4794 downdelay, INT_MAX);
4795 downdelay = 0;
4796 }
4797
4798 if ((use_carrier != 0) && (use_carrier != 1)) {
4799 printk(KERN_WARNING DRV_NAME
4800 ": Warning: use_carrier module parameter (%d), "
4801 "not of valid value (0/1), so it was set to 1\n",
4802 use_carrier);
4803 use_carrier = 1;
4804 }
4805
Moni Shoua7893b242008-05-17 21:10:12 -07004806 if (num_grat_arp < 0 || num_grat_arp > 255) {
4807 printk(KERN_WARNING DRV_NAME
4808 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4809 "was reset to 1 \n", num_grat_arp);
4810 num_grat_arp = 1;
4811 }
4812
Brian Haley305d5522008-11-04 17:51:14 -08004813 if (num_unsol_na < 0 || num_unsol_na > 255) {
4814 printk(KERN_WARNING DRV_NAME
4815 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4816 "was reset to 1 \n", num_unsol_na);
4817 num_unsol_na = 1;
4818 }
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 /* reset values for 802.3ad */
4821 if (bond_mode == BOND_MODE_8023AD) {
4822 if (!miimon) {
4823 printk(KERN_WARNING DRV_NAME
4824 ": Warning: miimon must be specified, "
4825 "otherwise bonding will not detect link "
4826 "failure, speed and duplex which are "
4827 "essential for 802.3ad operation\n");
4828 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4829 miimon = 100;
4830 }
4831 }
4832
4833 /* reset values for TLB/ALB */
4834 if ((bond_mode == BOND_MODE_TLB) ||
4835 (bond_mode == BOND_MODE_ALB)) {
4836 if (!miimon) {
4837 printk(KERN_WARNING DRV_NAME
4838 ": Warning: miimon must be specified, "
4839 "otherwise bonding will not detect link "
4840 "failure and link speed which are essential "
4841 "for TLB/ALB load balancing\n");
4842 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4843 miimon = 100;
4844 }
4845 }
4846
4847 if (bond_mode == BOND_MODE_ALB) {
4848 printk(KERN_NOTICE DRV_NAME
4849 ": In ALB mode you might experience client "
4850 "disconnections upon reconnection of a link if the "
4851 "bonding module updelay parameter (%d msec) is "
4852 "incompatible with the forwarding delay time of the "
4853 "switch\n",
4854 updelay);
4855 }
4856
4857 if (!miimon) {
4858 if (updelay || downdelay) {
4859 /* just warn the user the up/down delay will have
4860 * no effect since miimon is zero...
4861 */
4862 printk(KERN_WARNING DRV_NAME
4863 ": Warning: miimon module parameter not set "
4864 "and updelay (%d) or downdelay (%d) module "
4865 "parameter is set; updelay and downdelay have "
4866 "no effect unless miimon is set\n",
4867 updelay, downdelay);
4868 }
4869 } else {
4870 /* don't allow arp monitoring */
4871 if (arp_interval) {
4872 printk(KERN_WARNING DRV_NAME
4873 ": Warning: miimon (%d) and arp_interval (%d) "
4874 "can't be used simultaneously, disabling ARP "
4875 "monitoring\n",
4876 miimon, arp_interval);
4877 arp_interval = 0;
4878 }
4879
4880 if ((updelay % miimon) != 0) {
4881 printk(KERN_WARNING DRV_NAME
4882 ": Warning: updelay (%d) is not a multiple "
4883 "of miimon (%d), updelay rounded to %d ms\n",
4884 updelay, miimon, (updelay / miimon) * miimon);
4885 }
4886
4887 updelay /= miimon;
4888
4889 if ((downdelay % miimon) != 0) {
4890 printk(KERN_WARNING DRV_NAME
4891 ": Warning: downdelay (%d) is not a multiple "
4892 "of miimon (%d), downdelay rounded to %d ms\n",
4893 downdelay, miimon,
4894 (downdelay / miimon) * miimon);
4895 }
4896
4897 downdelay /= miimon;
4898 }
4899
4900 if (arp_interval < 0) {
4901 printk(KERN_WARNING DRV_NAME
4902 ": Warning: arp_interval module parameter (%d) "
4903 ", not in range 0-%d, so it was reset to %d\n",
4904 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4905 arp_interval = BOND_LINK_ARP_INTERV;
4906 }
4907
4908 for (arp_ip_count = 0;
4909 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4910 arp_ip_count++) {
4911 /* not complete check, but should be good enough to
4912 catch mistakes */
4913 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4914 printk(KERN_WARNING DRV_NAME
4915 ": Warning: bad arp_ip_target module parameter "
4916 "(%s), ARP monitoring will not be performed\n",
4917 arp_ip_target[arp_ip_count]);
4918 arp_interval = 0;
4919 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004920 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 arp_target[arp_ip_count] = ip;
4922 }
4923 }
4924
4925 if (arp_interval && !arp_ip_count) {
4926 /* don't allow arping if no arp_ip_target given... */
4927 printk(KERN_WARNING DRV_NAME
4928 ": Warning: arp_interval module parameter (%d) "
4929 "specified without providing an arp_ip_target "
4930 "parameter, arp_interval was reset to 0\n",
4931 arp_interval);
4932 arp_interval = 0;
4933 }
4934
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004935 if (arp_validate) {
4936 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4937 printk(KERN_ERR DRV_NAME
4938 ": arp_validate only supported in active-backup mode\n");
4939 return -EINVAL;
4940 }
4941 if (!arp_interval) {
4942 printk(KERN_ERR DRV_NAME
4943 ": arp_validate requires arp_interval\n");
4944 return -EINVAL;
4945 }
4946
4947 arp_validate_value = bond_parse_parm(arp_validate,
4948 arp_validate_tbl);
4949 if (arp_validate_value == -1) {
4950 printk(KERN_ERR DRV_NAME
4951 ": Error: invalid arp_validate \"%s\"\n",
4952 arp_validate == NULL ? "NULL" : arp_validate);
4953 return -EINVAL;
4954 }
4955 } else
4956 arp_validate_value = 0;
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 if (miimon) {
4959 printk(KERN_INFO DRV_NAME
4960 ": MII link monitoring set to %d ms\n",
4961 miimon);
4962 } else if (arp_interval) {
4963 int i;
4964
4965 printk(KERN_INFO DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004966 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4967 arp_interval,
4968 arp_validate_tbl[arp_validate_value].modename,
4969 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 for (i = 0; i < arp_ip_count; i++)
4972 printk (" %s", arp_ip_target[i]);
4973
4974 printk("\n");
4975
Jay Vosburghb8a97872008-06-13 18:12:04 -07004976 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 /* miimon and arp_interval not set, we need one so things
4978 * work as expected, see bonding.txt for details
4979 */
4980 printk(KERN_WARNING DRV_NAME
4981 ": Warning: either miimon or arp_interval and "
4982 "arp_ip_target module parameters must be specified, "
4983 "otherwise bonding will not detect link failures! see "
4984 "bonding.txt for details.\n");
4985 }
4986
4987 if (primary && !USES_PRIMARY(bond_mode)) {
4988 /* currently, using a primary only makes sense
4989 * in active backup, TLB or ALB modes
4990 */
4991 printk(KERN_WARNING DRV_NAME
4992 ": Warning: %s primary device specified but has no "
4993 "effect in %s mode\n",
4994 primary, bond_mode_name(bond_mode));
4995 primary = NULL;
4996 }
4997
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004998 if (fail_over_mac) {
4999 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5000 fail_over_mac_tbl);
5001 if (fail_over_mac_value == -1) {
5002 printk(KERN_ERR DRV_NAME
5003 ": Error: invalid fail_over_mac \"%s\"\n",
5004 arp_validate == NULL ? "NULL" : arp_validate);
5005 return -EINVAL;
5006 }
5007
5008 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5009 printk(KERN_WARNING DRV_NAME
5010 ": Warning: fail_over_mac only affects "
5011 "active-backup mode.\n");
5012 } else {
5013 fail_over_mac_value = BOND_FOM_NONE;
5014 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 /* fill params struct with the proper values */
5017 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005018 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005020 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005021 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005023 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 params->updelay = updelay;
5025 params->downdelay = downdelay;
5026 params->use_carrier = use_carrier;
5027 params->lacp_fast = lacp_fast;
5028 params->primary[0] = 0;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005029 params->fail_over_mac = fail_over_mac_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
5031 if (primary) {
5032 strncpy(params->primary, primary, IFNAMSIZ);
5033 params->primary[IFNAMSIZ - 1] = 0;
5034 }
5035
5036 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5037
5038 return 0;
5039}
5040
Peter Zijlstra0daa2302006-11-08 19:51:01 -08005041static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005042static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa2302006-11-08 19:51:01 -08005043
David S. Millere8a04642008-07-17 00:34:19 -07005044static void bond_set_lockdep_class_one(struct net_device *dev,
5045 struct netdev_queue *txq,
5046 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005047{
5048 lockdep_set_class(&txq->_xmit_lock,
5049 &bonding_netdev_xmit_lock_key);
5050}
5051
5052static void bond_set_lockdep_class(struct net_device *dev)
5053{
David S. Millercf508b12008-07-22 14:16:42 -07005054 lockdep_set_class(&dev->addr_list_lock,
5055 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005056 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005057}
5058
Mitch Williamsdfe60392005-11-09 10:36:04 -08005059/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005060 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005061 * Caller must NOT hold rtnl_lock; we need to release it here before we
5062 * set up our sysfs entries.
5063 */
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -07005064int bond_create(char *name, struct bond_params *params)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005065{
5066 struct net_device *bond_dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005067 struct bonding *bond;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005068 int res;
5069
5070 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005071 down_write(&bonding_rwsem);
5072
5073 /* Check to see if the bond already exists. */
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005074 if (name) {
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005075 list_for_each_entry(bond, &bond_dev_list, bond_list)
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005076 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
5077 printk(KERN_ERR DRV_NAME
Jay Vosburgh027ea042008-01-17 16:25:02 -08005078 ": cannot add bond %s; it already exists\n",
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005079 name);
5080 res = -EPERM;
5081 goto out_rtnl;
5082 }
5083 }
Jay Vosburgh027ea042008-01-17 16:25:02 -08005084
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005085 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
5086 ether_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005087 if (!bond_dev) {
5088 printk(KERN_ERR DRV_NAME
5089 ": %s: eek! can't alloc netdev!\n",
5090 name);
5091 res = -ENOMEM;
5092 goto out_rtnl;
5093 }
5094
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005095 if (!name) {
5096 res = dev_alloc_name(bond_dev, "bond%d");
5097 if (res < 0)
5098 goto out_netdev;
5099 }
5100
Mitch Williamsdfe60392005-11-09 10:36:04 -08005101 /* bond_init() must be called after dev_alloc_name() (for the
5102 * /proc files), but before register_netdevice(), because we
5103 * need to set function pointers.
5104 */
5105
5106 res = bond_init(bond_dev, params);
5107 if (res < 0) {
5108 goto out_netdev;
5109 }
5110
Mitch Williamsdfe60392005-11-09 10:36:04 -08005111 res = register_netdevice(bond_dev);
5112 if (res < 0) {
5113 goto out_bond;
5114 }
Peter Zijlstra0daa2302006-11-08 19:51:01 -08005115
David S. Millerc773e842008-07-08 23:13:53 -07005116 bond_set_lockdep_class(bond_dev);
Peter Zijlstra0daa2302006-11-08 19:51:01 -08005117
Jay Vosburghff59c452006-03-27 13:27:43 -08005118 netif_carrier_off(bond_dev);
5119
Jay Vosburgh027ea042008-01-17 16:25:02 -08005120 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005121 rtnl_unlock(); /* allows sysfs registration of net device */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005122 res = bond_create_sysfs_entry(bond_dev->priv);
Jay Vosburgh09c89272007-01-19 18:15:38 -08005123 if (res < 0) {
5124 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005125 down_write(&bonding_rwsem);
Pavel Emelyanov822973b2008-05-02 17:49:37 -07005126 bond_deinit(bond_dev);
5127 unregister_netdevice(bond_dev);
5128 goto out_rtnl;
Jay Vosburgh09c89272007-01-19 18:15:38 -08005129 }
5130
5131 return 0;
5132
Mitch Williamsdfe60392005-11-09 10:36:04 -08005133out_bond:
5134 bond_deinit(bond_dev);
5135out_netdev:
5136 free_netdev(bond_dev);
5137out_rtnl:
Jay Vosburgh027ea042008-01-17 16:25:02 -08005138 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005139 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005140 return res;
5141}
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143static int __init bonding_init(void)
5144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 int i;
5146 int res;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005147 struct bonding *bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 printk(KERN_INFO "%s", version);
5150
Mitch Williamsdfe60392005-11-09 10:36:04 -08005151 res = bond_check_params(&bonding_defaults);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if (res) {
Mitch Williamsdfe60392005-11-09 10:36:04 -08005153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 }
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156#ifdef CONFIG_PROC_FS
5157 bond_create_proc_dir();
5158#endif
Jay Vosburgh027ea042008-01-17 16:25:02 -08005159
5160 init_rwsem(&bonding_rwsem);
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 for (i = 0; i < max_bonds; i++) {
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -07005163 res = bond_create(NULL, &bonding_defaults);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005164 if (res)
5165 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 }
5167
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005168 res = bond_create_sysfs();
5169 if (res)
5170 goto err;
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005173 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005174 bond_register_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
Mitch Williamsdfe60392005-11-09 10:36:04 -08005176 goto out;
5177err:
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005178 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07005179 bond_work_cancel_all(bond);
5180 destroy_workqueue(bond->wq);
5181 }
5182
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005183 bond_destroy_sysfs();
5184
Florin Malita40abc272005-09-18 00:24:12 -07005185 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005188out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005190
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
5193static void __exit bonding_exit(void)
5194{
5195 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005196 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005197 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005199 bond_destroy_sysfs();
5200
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 rtnl_lock();
5202 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 rtnl_unlock();
5204}
5205
5206module_init(bonding_init);
5207module_exit(bonding_exit);
5208MODULE_LICENSE("GPL");
5209MODULE_VERSION(DRV_VERSION);
5210MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5211MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5212MODULE_SUPPORTED_DEVICE("most ethernet devices");
5213
5214/*
5215 * Local variables:
5216 * c-indent-level: 8
5217 * c-basic-offset: 8
5218 * tab-width: 8
5219 * End:
5220 */
5221