blob: 65c7ebafaacd7e0caa734bef1f6d0cbfe5631962 [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;
91static int miimon = BOND_LINK_MON_INTERV;
92static int updelay = 0;
93static int downdelay = 0;
94static int use_carrier = 1;
95static char *mode = NULL;
96static char *primary = NULL;
97static char *lacp_rate = NULL;
Jay Vosburgh169a3e62005-06-26 17:54:11 -040098static char *xmit_hash_policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int arp_interval = BOND_LINK_ARP_INTERV;
100static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700101static char *arp_validate = NULL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700102static int fail_over_mac = 0;
Mitch Williams12479f92005-11-09 10:35:44 -0800103struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105module_param(max_bonds, int, 0);
106MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
107module_param(miimon, int, 0);
108MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
109module_param(updelay, int, 0);
110MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
111module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800112MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
113 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800115MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
116 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800118MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
119 "1 for active-backup, 2 for balance-xor, "
120 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
121 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param(primary, charp, 0);
123MODULE_PARM_DESC(primary, "Primary network device to use");
124module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800125MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
126 "(slow/fast)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400127module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800128MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
129 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130module_param(arp_interval, int, 0);
131MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
132module_param_array(arp_ip_target, charp, NULL, 0);
133MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700134module_param(arp_validate, charp, 0);
135MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburghdd957c52007-10-09 19:57:24 -0700136module_param(fail_over_mac, int, 0);
137MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/*----------------------------- Global variables ----------------------------*/
140
Arjan van de Venf71e1302006-03-03 21:33:57 -0500141static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
143
Mitch Williams12479f92005-11-09 10:35:44 -0800144LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#ifdef CONFIG_PROC_FS
147static struct proc_dir_entry *bond_proc_dir = NULL;
148#endif
149
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150extern struct rw_semaphore bonding_rwsem;
Al Virod3bb52b2007-08-22 20:06:58 -0400151static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int arp_ip_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int bond_mode = BOND_MODE_ROUNDROBIN;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400154static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int lacp_fast = 0;
Jay Vosburgh217df672005-09-26 16:11:50 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Mitch Williams12479f92005-11-09 10:35:44 -0800158struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{ "slow", AD_LACP_SLOW},
160{ "fast", AD_LACP_FAST},
161{ NULL, -1},
162};
163
Mitch Williams12479f92005-11-09 10:35:44 -0800164struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{ "balance-rr", BOND_MODE_ROUNDROBIN},
166{ "active-backup", BOND_MODE_ACTIVEBACKUP},
167{ "balance-xor", BOND_MODE_XOR},
168{ "broadcast", BOND_MODE_BROADCAST},
169{ "802.3ad", BOND_MODE_8023AD},
170{ "balance-tlb", BOND_MODE_TLB},
171{ "balance-alb", BOND_MODE_ALB},
172{ NULL, -1},
173};
174
Mitch Williams12479f92005-11-09 10:35:44 -0800175struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400176{ "layer2", BOND_XMIT_POLICY_LAYER2},
177{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800178{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400179{ NULL, -1},
180};
181
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700182struct bond_parm_tbl arp_validate_tbl[] = {
183{ "none", BOND_ARP_VALIDATE_NONE},
184{ "active", BOND_ARP_VALIDATE_ACTIVE},
185{ "backup", BOND_ARP_VALIDATE_BACKUP},
186{ "all", BOND_ARP_VALIDATE_ALL},
187{ NULL, -1},
188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*-------------------------- Forward declarations ---------------------------*/
191
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400192static void bond_send_gratuitous_arp(struct bonding *bond);
Adrian Bunkc50b85d2007-10-24 18:23:17 +0200193static void bond_deinit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*---------------------------- General routines -----------------------------*/
196
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700197static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 switch (mode) {
200 case BOND_MODE_ROUNDROBIN :
201 return "load balancing (round-robin)";
202 case BOND_MODE_ACTIVEBACKUP :
203 return "fault-tolerance (active-backup)";
204 case BOND_MODE_XOR :
205 return "load balancing (xor)";
206 case BOND_MODE_BROADCAST :
207 return "fault-tolerance (broadcast)";
208 case BOND_MODE_8023AD:
209 return "IEEE 802.3ad Dynamic link aggregation";
210 case BOND_MODE_TLB:
211 return "transmit load balancing";
212 case BOND_MODE_ALB:
213 return "adaptive load balancing";
214 default:
215 return "unknown";
216 }
217}
218
219/*---------------------------------- VLAN -----------------------------------*/
220
221/**
222 * bond_add_vlan - add a new vlan id on bond
223 * @bond: bond that got the notification
224 * @vlan_id: the vlan id to add
225 *
226 * Returns -ENOMEM if allocation failed.
227 */
228static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
229{
230 struct vlan_entry *vlan;
231
232 dprintk("bond: %s, vlan id %d\n",
233 (bond ? bond->dev->name: "None"), vlan_id);
234
235 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
236 if (!vlan) {
237 return -ENOMEM;
238 }
239
240 INIT_LIST_HEAD(&vlan->vlan_list);
241 vlan->vlan_id = vlan_id;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400242 vlan->vlan_ip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 write_lock_bh(&bond->lock);
245
246 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
247
248 write_unlock_bh(&bond->lock);
249
250 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
251
252 return 0;
253}
254
255/**
256 * bond_del_vlan - delete a vlan id from bond
257 * @bond: bond that got the notification
258 * @vlan_id: the vlan id to delete
259 *
260 * returns -ENODEV if @vlan_id was not found in @bond.
261 */
262static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
263{
264 struct vlan_entry *vlan, *next;
265 int res = -ENODEV;
266
267 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
268
269 write_lock_bh(&bond->lock);
270
271 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
272 if (vlan->vlan_id == vlan_id) {
273 list_del(&vlan->vlan_list);
274
275 if ((bond->params.mode == BOND_MODE_TLB) ||
276 (bond->params.mode == BOND_MODE_ALB)) {
277 bond_alb_clear_vlan(bond, vlan_id);
278 }
279
280 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
281 bond->dev->name);
282
283 kfree(vlan);
284
285 if (list_empty(&bond->vlan_list) &&
286 (bond->slave_cnt == 0)) {
287 /* Last VLAN removed and no slaves, so
288 * restore block on adding VLANs. This will
289 * be removed once new slaves that are not
290 * VLAN challenged will be added.
291 */
292 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
293 }
294
295 res = 0;
296 goto out;
297 }
298 }
299
300 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
301 bond->dev->name);
302
303out:
304 write_unlock_bh(&bond->lock);
305 return res;
306}
307
308/**
309 * bond_has_challenged_slaves
310 * @bond: the bond we're working on
311 *
312 * Searches the slave list. Returns 1 if a vlan challenged slave
313 * was found, 0 otherwise.
314 *
315 * Assumes bond->lock is held.
316 */
317static int bond_has_challenged_slaves(struct bonding *bond)
318{
319 struct slave *slave;
320 int i;
321
322 bond_for_each_slave(bond, slave, i) {
323 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
324 dprintk("found VLAN challenged slave - %s\n",
325 slave->dev->name);
326 return 1;
327 }
328 }
329
330 dprintk("no VLAN challenged slaves found\n");
331 return 0;
332}
333
334/**
335 * bond_next_vlan - safely skip to the next item in the vlans list.
336 * @bond: the bond we're working on
337 * @curr: item we're advancing from
338 *
339 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
340 * or @curr->next otherwise (even if it is @curr itself again).
341 *
342 * Caller must hold bond->lock
343 */
344struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
345{
346 struct vlan_entry *next, *last;
347
348 if (list_empty(&bond->vlan_list)) {
349 return NULL;
350 }
351
352 if (!curr) {
353 next = list_entry(bond->vlan_list.next,
354 struct vlan_entry, vlan_list);
355 } else {
356 last = list_entry(bond->vlan_list.prev,
357 struct vlan_entry, vlan_list);
358 if (last == curr) {
359 next = list_entry(bond->vlan_list.next,
360 struct vlan_entry, vlan_list);
361 } else {
362 next = list_entry(curr->vlan_list.next,
363 struct vlan_entry, vlan_list);
364 }
365 }
366
367 return next;
368}
369
370/**
371 * bond_dev_queue_xmit - Prepare skb for xmit.
372 *
373 * @bond: bond device that got this skb for tx.
374 * @skb: hw accel VLAN tagged skb to transmit
375 * @slave_dev: slave that is supposed to xmit this skbuff
376 *
377 * When the bond gets an skb to transmit that is
378 * already hardware accelerated VLAN tagged, and it
379 * needs to relay this skb to a slave that is not
380 * hw accel capable, the skb needs to be "unaccelerated",
381 * i.e. strip the hwaccel tag and re-insert it as part
382 * of the payload.
383 */
384int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
385{
386 unsigned short vlan_id;
387
388 if (!list_empty(&bond->vlan_list) &&
389 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
390 vlan_get_tag(skb, &vlan_id) == 0) {
391 skb->dev = slave_dev;
392 skb = vlan_put_tag(skb, vlan_id);
393 if (!skb) {
394 /* vlan_put_tag() frees the skb in case of error,
395 * so return success here so the calling functions
396 * won't attempt to free is again.
397 */
398 return 0;
399 }
400 } else {
401 skb->dev = slave_dev;
402 }
403
404 skb->priority = 1;
405 dev_queue_xmit(skb);
406
407 return 0;
408}
409
410/*
411 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
412 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
413 * lock because:
414 * a. This operation is performed in IOCTL context,
415 * b. The operation is protected by the RTNL semaphore in the 8021q code,
416 * c. Holding a lock with BH disabled while directly calling a base driver
417 * entry point is generally a BAD idea.
418 *
419 * The design of synchronization/protection for this operation in the 8021q
420 * module is good for one or more VLAN devices over a single physical device
421 * and cannot be extended for a teaming solution like bonding, so there is a
422 * potential race condition here where a net device from the vlan group might
423 * be referenced (either by a base driver or the 8021q code) while it is being
424 * removed from the system. However, it turns out we're not making matters
425 * worse, and if it works for regular VLAN usage it will work here too.
426*/
427
428/**
429 * bond_vlan_rx_register - Propagates registration to slaves
430 * @bond_dev: bonding net device that got called
431 * @grp: vlan group being registered
432 */
433static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
434{
435 struct bonding *bond = bond_dev->priv;
436 struct slave *slave;
437 int i;
438
439 bond->vlgrp = grp;
440
441 bond_for_each_slave(bond, slave, i) {
442 struct net_device *slave_dev = slave->dev;
443
444 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
445 slave_dev->vlan_rx_register) {
446 slave_dev->vlan_rx_register(slave_dev, grp);
447 }
448 }
449}
450
451/**
452 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
453 * @bond_dev: bonding net device that got called
454 * @vid: vlan id being added
455 */
456static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
457{
458 struct bonding *bond = bond_dev->priv;
459 struct slave *slave;
460 int i, res;
461
462 bond_for_each_slave(bond, slave, i) {
463 struct net_device *slave_dev = slave->dev;
464
465 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
466 slave_dev->vlan_rx_add_vid) {
467 slave_dev->vlan_rx_add_vid(slave_dev, vid);
468 }
469 }
470
471 res = bond_add_vlan(bond, vid);
472 if (res) {
473 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800474 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 bond_dev->name, vid);
476 }
477}
478
479/**
480 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
481 * @bond_dev: bonding net device that got called
482 * @vid: vlan id being removed
483 */
484static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
485{
486 struct bonding *bond = bond_dev->priv;
487 struct slave *slave;
488 struct net_device *vlan_dev;
489 int i, res;
490
491 bond_for_each_slave(bond, slave, i) {
492 struct net_device *slave_dev = slave->dev;
493
494 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
495 slave_dev->vlan_rx_kill_vid) {
496 /* Save and then restore vlan_dev in the grp array,
497 * since the slave's driver might clear it.
498 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800499 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800501 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504
505 res = bond_del_vlan(bond, vid);
506 if (res) {
507 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800508 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 bond_dev->name, vid);
510 }
511}
512
513static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
514{
515 struct vlan_entry *vlan;
516
517 write_lock_bh(&bond->lock);
518
519 if (list_empty(&bond->vlan_list)) {
520 goto out;
521 }
522
523 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
524 slave_dev->vlan_rx_register) {
525 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
526 }
527
528 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
529 !(slave_dev->vlan_rx_add_vid)) {
530 goto out;
531 }
532
533 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
534 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
535 }
536
537out:
538 write_unlock_bh(&bond->lock);
539}
540
541static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
542{
543 struct vlan_entry *vlan;
544 struct net_device *vlan_dev;
545
546 write_lock_bh(&bond->lock);
547
548 if (list_empty(&bond->vlan_list)) {
549 goto out;
550 }
551
552 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
553 !(slave_dev->vlan_rx_kill_vid)) {
554 goto unreg;
555 }
556
557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
558 /* Save and then restore vlan_dev in the grp array,
559 * since the slave's driver might clear it.
560 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800561 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800563 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566unreg:
567 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
568 slave_dev->vlan_rx_register) {
569 slave_dev->vlan_rx_register(slave_dev, NULL);
570 }
571
572out:
573 write_unlock_bh(&bond->lock);
574}
575
576/*------------------------------- Link status -------------------------------*/
577
578/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800579 * Set the carrier state for the master according to the state of its
580 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
581 * do special 802.3ad magic.
582 *
583 * Returns zero if carrier state does not change, nonzero if it does.
584 */
585static int bond_set_carrier(struct bonding *bond)
586{
587 struct slave *slave;
588 int i;
589
590 if (bond->slave_cnt == 0)
591 goto down;
592
593 if (bond->params.mode == BOND_MODE_8023AD)
594 return bond_3ad_set_carrier(bond);
595
596 bond_for_each_slave(bond, slave, i) {
597 if (slave->link == BOND_LINK_UP) {
598 if (!netif_carrier_ok(bond->dev)) {
599 netif_carrier_on(bond->dev);
600 return 1;
601 }
602 return 0;
603 }
604 }
605
606down:
607 if (netif_carrier_ok(bond->dev)) {
608 netif_carrier_off(bond->dev);
609 return 1;
610 }
611 return 0;
612}
613
614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * Get link speed and duplex from the slave's base driver
616 * using ethtool. If for some reason the call fails or the
617 * values are invalid, fake speed and duplex to 100/Full
618 * and return error.
619 */
620static int bond_update_speed_duplex(struct slave *slave)
621{
622 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700624 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* Fake speed and duplex */
627 slave->speed = SPEED_100;
628 slave->duplex = DUPLEX_FULL;
629
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700630 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700633 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
634 if (res < 0)
635 return -1;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 switch (etool.speed) {
638 case SPEED_10:
639 case SPEED_100:
640 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700641 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 default:
644 return -1;
645 }
646
647 switch (etool.duplex) {
648 case DUPLEX_FULL:
649 case DUPLEX_HALF:
650 break;
651 default:
652 return -1;
653 }
654
655 slave->speed = etool.speed;
656 slave->duplex = etool.duplex;
657
658 return 0;
659}
660
661/*
662 * if <dev> supports MII link status reporting, check its link status.
663 *
664 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
665 * depening upon the setting of the use_carrier parameter.
666 *
667 * Return either BMSR_LSTATUS, meaning that the link is up (or we
668 * can't tell and just pretend it is), or 0, meaning that the link is
669 * down.
670 *
671 * If reporting is non-zero, instead of faking link up, return -1 if
672 * both ETHTOOL and MII ioctls fail (meaning the device does not
673 * support them). If use_carrier is set, return whatever it says.
674 * It'd be nice if there was a good way to tell if a driver supports
675 * netif_carrier, but there really isn't.
676 */
677static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
678{
679 static int (* ioctl)(struct net_device *, struct ifreq *, int);
680 struct ifreq ifr;
681 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (bond->params.use_carrier) {
684 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
685 }
686
687 ioctl = slave_dev->do_ioctl;
688 if (ioctl) {
689 /* TODO: set pointer to correct ioctl on a per team member */
690 /* bases to make this more efficient. that is, once */
691 /* we determine the correct ioctl, we will always */
692 /* call it and not the others for that team */
693 /* member. */
694
695 /*
696 * We cannot assume that SIOCGMIIPHY will also read a
697 * register; not all network drivers (e.g., e100)
698 * support that.
699 */
700
701 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
702 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
703 mii = if_mii(&ifr);
704 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
705 mii->reg_num = MII_BMSR;
706 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
707 return (mii->val_out & BMSR_LSTATUS);
708 }
709 }
710 }
711
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700712 /*
713 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
714 * attempt to get link status from it if the above MII ioctls fail.
715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (slave_dev->ethtool_ops) {
717 if (slave_dev->ethtool_ops->get_link) {
718 u32 link;
719
720 link = slave_dev->ethtool_ops->get_link(slave_dev);
721
722 return link ? BMSR_LSTATUS : 0;
723 }
724 }
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700728 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * cannot report link status). If not reporting, pretend
730 * we're ok.
731 */
732 return (reporting ? -1 : BMSR_LSTATUS);
733}
734
735/*----------------------------- Multicast list ------------------------------*/
736
737/*
738 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
739 */
740static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
741{
742 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
743 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
744}
745
746/*
747 * returns dmi entry if found, NULL otherwise
748 */
749static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
750{
751 struct dev_mc_list *idmi;
752
753 for (idmi = mc_list; idmi; idmi = idmi->next) {
754 if (bond_is_dmi_same(dmi, idmi)) {
755 return idmi;
756 }
757 }
758
759 return NULL;
760}
761
762/*
763 * Push the promiscuity flag down to appropriate slaves
764 */
765static void bond_set_promiscuity(struct bonding *bond, int inc)
766{
767 if (USES_PRIMARY(bond->params.mode)) {
768 /* write lock already acquired */
769 if (bond->curr_active_slave) {
770 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
771 }
772 } else {
773 struct slave *slave;
774 int i;
775 bond_for_each_slave(bond, slave, i) {
776 dev_set_promiscuity(slave->dev, inc);
777 }
778 }
779}
780
781/*
782 * Push the allmulti flag down to all slaves
783 */
784static void bond_set_allmulti(struct bonding *bond, int inc)
785{
786 if (USES_PRIMARY(bond->params.mode)) {
787 /* write lock already acquired */
788 if (bond->curr_active_slave) {
789 dev_set_allmulti(bond->curr_active_slave->dev, inc);
790 }
791 } else {
792 struct slave *slave;
793 int i;
794 bond_for_each_slave(bond, slave, i) {
795 dev_set_allmulti(slave->dev, inc);
796 }
797 }
798}
799
800/*
801 * Add a Multicast address to slaves
802 * according to mode
803 */
804static void bond_mc_add(struct bonding *bond, void *addr, int alen)
805{
806 if (USES_PRIMARY(bond->params.mode)) {
807 /* write lock already acquired */
808 if (bond->curr_active_slave) {
809 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
810 }
811 } else {
812 struct slave *slave;
813 int i;
814 bond_for_each_slave(bond, slave, i) {
815 dev_mc_add(slave->dev, addr, alen, 0);
816 }
817 }
818}
819
820/*
821 * Remove a multicast address from slave
822 * according to mode
823 */
824static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
825{
826 if (USES_PRIMARY(bond->params.mode)) {
827 /* write lock already acquired */
828 if (bond->curr_active_slave) {
829 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
830 }
831 } else {
832 struct slave *slave;
833 int i;
834 bond_for_each_slave(bond, slave, i) {
835 dev_mc_delete(slave->dev, addr, alen, 0);
836 }
837 }
838}
839
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800840
841/*
842 * Retrieve the list of registered multicast addresses for the bonding
843 * device and retransmit an IGMP JOIN request to the current active
844 * slave.
845 */
846static void bond_resend_igmp_join_requests(struct bonding *bond)
847{
848 struct in_device *in_dev;
849 struct ip_mc_list *im;
850
851 rcu_read_lock();
852 in_dev = __in_dev_get_rcu(bond->dev);
853 if (in_dev) {
854 for (im = in_dev->mc_list; im; im = im->next) {
855 ip_mc_rejoin_group(im);
856 }
857 }
858
859 rcu_read_unlock();
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
863 * Totally destroys the mc_list in bond
864 */
865static void bond_mc_list_destroy(struct bonding *bond)
866{
867 struct dev_mc_list *dmi;
868
869 dmi = bond->mc_list;
870 while (dmi) {
871 bond->mc_list = dmi->next;
872 kfree(dmi);
873 dmi = bond->mc_list;
874 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800875 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/*
879 * Copy all the Multicast addresses from src to the bonding device dst
880 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700881static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100882 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct dev_mc_list *dmi, *new_dmi;
885
886 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700887 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (!new_dmi) {
890 /* FIXME: Potential memory leak !!! */
891 return -ENOMEM;
892 }
893
894 new_dmi->next = bond->mc_list;
895 bond->mc_list = new_dmi;
896 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
897 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
898 new_dmi->dmi_users = dmi->dmi_users;
899 new_dmi->dmi_gusers = dmi->dmi_gusers;
900 }
901
902 return 0;
903}
904
905/*
906 * flush all members of flush->mc_list from device dev->mc_list
907 */
908static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
909{
910 struct bonding *bond = bond_dev->priv;
911 struct dev_mc_list *dmi;
912
913 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
914 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
915 }
916
917 if (bond->params.mode == BOND_MODE_8023AD) {
918 /* del lacpdu mc addr from mc list */
919 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
920
921 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
922 }
923}
924
925/*--------------------------- Active slave change ---------------------------*/
926
927/*
928 * Update the mc list and multicast-related flags for the new and
929 * old active slaves (if any) according to the multicast mode, and
930 * promiscuous flags unconditionally.
931 */
932static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
933{
934 struct dev_mc_list *dmi;
935
936 if (!USES_PRIMARY(bond->params.mode)) {
937 /* nothing to do - mc list is already up-to-date on
938 * all slaves
939 */
940 return;
941 }
942
943 if (old_active) {
944 if (bond->dev->flags & IFF_PROMISC) {
945 dev_set_promiscuity(old_active->dev, -1);
946 }
947
948 if (bond->dev->flags & IFF_ALLMULTI) {
949 dev_set_allmulti(old_active->dev, -1);
950 }
951
952 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
953 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
954 }
955 }
956
957 if (new_active) {
958 if (bond->dev->flags & IFF_PROMISC) {
959 dev_set_promiscuity(new_active->dev, 1);
960 }
961
962 if (bond->dev->flags & IFF_ALLMULTI) {
963 dev_set_allmulti(new_active->dev, 1);
964 }
965
966 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
967 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
968 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800969 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971}
972
973/**
974 * find_best_interface - select the best available slave to be the active one
975 * @bond: our bonding struct
976 *
977 * Warning: Caller must hold curr_slave_lock for writing.
978 */
979static struct slave *bond_find_best_slave(struct bonding *bond)
980{
981 struct slave *new_active, *old_active;
982 struct slave *bestslave = NULL;
983 int mintime = bond->params.updelay;
984 int i;
985
986 new_active = old_active = bond->curr_active_slave;
987
988 if (!new_active) { /* there were no active slaves left */
989 if (bond->slave_cnt > 0) { /* found one slave */
990 new_active = bond->first_slave;
991 } else {
992 return NULL; /* still no slave, return NULL */
993 }
994 }
995
996 /* first try the primary link; if arping, a link must tx/rx traffic
997 * before it can be considered the curr_active_slave - also, we would skip
998 * slaves between the curr_active_slave and primary_slave that may be up
999 * and able to arp
1000 */
1001 if ((bond->primary_slave) &&
1002 (!bond->params.arp_interval) &&
1003 (IS_UP(bond->primary_slave->dev))) {
1004 new_active = bond->primary_slave;
1005 }
1006
1007 /* remember where to stop iterating over the slaves */
1008 old_active = new_active;
1009
1010 bond_for_each_slave_from(bond, new_active, i, old_active) {
1011 if (IS_UP(new_active->dev)) {
1012 if (new_active->link == BOND_LINK_UP) {
1013 return new_active;
1014 } else if (new_active->link == BOND_LINK_BACK) {
1015 /* link up, but waiting for stabilization */
1016 if (new_active->delay < mintime) {
1017 mintime = new_active->delay;
1018 bestslave = new_active;
1019 }
1020 }
1021 }
1022 }
1023
1024 return bestslave;
1025}
1026
1027/**
1028 * change_active_interface - change the active slave into the specified one
1029 * @bond: our bonding struct
1030 * @new: the new slave to make the active one
1031 *
1032 * Set the new slave to the bond's settings and unset them on the old
1033 * curr_active_slave.
1034 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1035 *
1036 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1037 * because it is apparently the best available slave we have, even though its
1038 * updelay hasn't timed out yet.
1039 *
1040 * Warning: Caller must hold curr_slave_lock for writing.
1041 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001042void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct slave *old_active = bond->curr_active_slave;
1045
1046 if (old_active == new_active) {
1047 return;
1048 }
1049
1050 if (new_active) {
1051 if (new_active->link == BOND_LINK_BACK) {
1052 if (USES_PRIMARY(bond->params.mode)) {
1053 printk(KERN_INFO DRV_NAME
1054 ": %s: making interface %s the new "
1055 "active one %d ms earlier.\n",
1056 bond->dev->name, new_active->dev->name,
1057 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1058 }
1059
1060 new_active->delay = 0;
1061 new_active->link = BOND_LINK_UP;
1062 new_active->jiffies = jiffies;
1063
1064 if (bond->params.mode == BOND_MODE_8023AD) {
1065 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1066 }
1067
1068 if ((bond->params.mode == BOND_MODE_TLB) ||
1069 (bond->params.mode == BOND_MODE_ALB)) {
1070 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1071 }
1072 } else {
1073 if (USES_PRIMARY(bond->params.mode)) {
1074 printk(KERN_INFO DRV_NAME
1075 ": %s: making interface %s the new "
1076 "active one.\n",
1077 bond->dev->name, new_active->dev->name);
1078 }
1079 }
1080 }
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (USES_PRIMARY(bond->params.mode)) {
1083 bond_mc_swap(bond, new_active, old_active);
1084 }
1085
1086 if ((bond->params.mode == BOND_MODE_TLB) ||
1087 (bond->params.mode == BOND_MODE_ALB)) {
1088 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001089 if (old_active)
1090 bond_set_slave_inactive_flags(old_active);
1091 if (new_active)
1092 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 } else {
1094 bond->curr_active_slave = new_active;
1095 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001096
1097 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1098 if (old_active) {
1099 bond_set_slave_inactive_flags(old_active);
1100 }
1101
1102 if (new_active) {
1103 bond_set_slave_active_flags(new_active);
1104 }
Moni Shoua2ab82852007-10-09 19:43:39 -07001105
1106 /* when bonding does not set the slave MAC address, the bond MAC
1107 * address is the one of the active slave.
1108 */
Jay Vosburghdd957c52007-10-09 19:57:24 -07001109 if (new_active && bond->params.fail_over_mac)
Moni Shoua2ab82852007-10-09 19:43:39 -07001110 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1111 new_active->dev->addr_len);
Moni Shoua1053f622007-10-09 19:43:42 -07001112 if (bond->curr_active_slave &&
1113 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1114 &bond->curr_active_slave->dev->state)) {
1115 dprintk("delaying gratuitous arp on %s\n",
1116 bond->curr_active_slave->dev->name);
1117 bond->send_grat_arp = 1;
1118 } else
1119 bond_send_gratuitous_arp(bond);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123/**
1124 * bond_select_active_slave - select a new active slave, if needed
1125 * @bond: our bonding struct
1126 *
1127 * This functions shoud be called when one of the following occurs:
1128 * - The old curr_active_slave has been released or lost its link.
1129 * - The primary_slave has got its link back.
1130 * - A slave has got its link back and there's no old curr_active_slave.
1131 *
1132 * Warning: Caller must hold curr_slave_lock for writing.
1133 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001134void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001137 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 best_slave = bond_find_best_slave(bond);
1140 if (best_slave != bond->curr_active_slave) {
1141 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001142 rv = bond_set_carrier(bond);
1143 if (!rv)
1144 return;
1145
1146 if (netif_carrier_ok(bond->dev)) {
1147 printk(KERN_INFO DRV_NAME
1148 ": %s: first active interface up!\n",
1149 bond->dev->name);
1150 } else {
1151 printk(KERN_INFO DRV_NAME ": %s: "
1152 "now running without any active interface !\n",
1153 bond->dev->name);
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156}
1157
1158/*--------------------------- slave list handling ---------------------------*/
1159
1160/*
1161 * This function attaches the slave to the end of list.
1162 *
1163 * bond->lock held for writing by caller.
1164 */
1165static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1166{
1167 if (bond->first_slave == NULL) { /* attaching the first slave */
1168 new_slave->next = new_slave;
1169 new_slave->prev = new_slave;
1170 bond->first_slave = new_slave;
1171 } else {
1172 new_slave->next = bond->first_slave;
1173 new_slave->prev = bond->first_slave->prev;
1174 new_slave->next->prev = new_slave;
1175 new_slave->prev->next = new_slave;
1176 }
1177
1178 bond->slave_cnt++;
1179}
1180
1181/*
1182 * This function detaches the slave from the list.
1183 * WARNING: no check is made to verify if the slave effectively
1184 * belongs to <bond>.
1185 * Nothing is freed on return, structures are just unchained.
1186 * If any slave pointer in bond was pointing to <slave>,
1187 * it should be changed by the calling function.
1188 *
1189 * bond->lock held for writing by caller.
1190 */
1191static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1192{
1193 if (slave->next) {
1194 slave->next->prev = slave->prev;
1195 }
1196
1197 if (slave->prev) {
1198 slave->prev->next = slave->next;
1199 }
1200
1201 if (bond->first_slave == slave) { /* slave is the first slave */
1202 if (bond->slave_cnt > 1) { /* there are more slave */
1203 bond->first_slave = slave->next;
1204 } else {
1205 bond->first_slave = NULL; /* slave was the last one */
1206 }
1207 }
1208
1209 slave->next = NULL;
1210 slave->prev = NULL;
1211 bond->slave_cnt--;
1212}
1213
1214/*---------------------------------- IOCTL ----------------------------------*/
1215
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001216static int bond_sethwaddr(struct net_device *bond_dev,
1217 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 dprintk("bond_dev=%p\n", bond_dev);
1220 dprintk("slave_dev=%p\n", slave_dev);
1221 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1222 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1223 return 0;
1224}
1225
Herbert Xu7f353bf2007-08-10 15:47:58 -07001226#define BOND_VLAN_FEATURES \
1227 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1228 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001229
1230/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001231 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001232 * feature bits are managed elsewhere, so preserve those feature bits
1233 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001234 */
1235static int bond_compute_features(struct bonding *bond)
1236{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001237 struct slave *slave;
1238 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001239 unsigned long features = bond_dev->features;
Moni Shoua3158bf72007-10-09 19:43:41 -07001240 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1241 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001242 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001243
Herbert Xu7f353bf2007-08-10 15:47:58 -07001244 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1245 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1246 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1247
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001248 bond_for_each_slave(bond, slave, i) {
Herbert Xu7f353bf2007-08-10 15:47:58 -07001249 features = netdev_compute_features(features,
1250 slave->dev->features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001251 if (slave->dev->hard_header_len > max_hard_header_len)
1252 max_hard_header_len = slave->dev->hard_header_len;
1253 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001254
Herbert Xu7f353bf2007-08-10 15:47:58 -07001255 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001256 bond_dev->features = features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001257 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001258
1259 return 0;
1260}
1261
Moni Shoua872254d2007-10-09 19:43:38 -07001262
1263static void bond_setup_by_slave(struct net_device *bond_dev,
1264 struct net_device *slave_dev)
1265{
Moni Shouad90a1622007-10-09 19:43:43 -07001266 struct bonding *bond = bond_dev->priv;
1267
Moni Shoua872254d2007-10-09 19:43:38 -07001268 bond_dev->neigh_setup = slave_dev->neigh_setup;
Jay Vosburgh1284cd32007-10-15 16:44:27 -07001269 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001270
1271 bond_dev->type = slave_dev->type;
1272 bond_dev->hard_header_len = slave_dev->hard_header_len;
1273 bond_dev->addr_len = slave_dev->addr_len;
1274
1275 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1276 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001277 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001281int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct bonding *bond = bond_dev->priv;
1284 struct slave *new_slave = NULL;
1285 struct dev_mc_list *dmi;
1286 struct sockaddr addr;
1287 int link_reporting;
1288 int old_features = bond_dev->features;
1289 int res = 0;
1290
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001291 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1292 slave_dev->do_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001294 ": %s: Warning: no link monitoring support for %s\n",
1295 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
1298 /* bond must be initialized by bond_open() before enslaving */
1299 if (!(bond_dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -07001300 printk(KERN_WARNING DRV_NAME
1301 " %s: master_dev is not up in bond_enslave\n",
1302 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
1305 /* already enslaved */
1306 if (slave_dev->flags & IFF_SLAVE) {
1307 dprintk("Error, Device was already enslaved\n");
1308 return -EBUSY;
1309 }
1310
1311 /* vlan challenged mutual exclusion */
1312 /* no need to lock since we're protected by rtnl_lock */
1313 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1314 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1315 if (!list_empty(&bond->vlan_list)) {
1316 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001317 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001319 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 bond_dev->name);
1321 return -EPERM;
1322 } else {
1323 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001324 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 "slave %s. Adding VLANs will be blocked as "
1326 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001327 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 bond_dev->name);
1329 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1330 }
1331 } else {
1332 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1333 if (bond->slave_cnt == 0) {
1334 /* First slave, and it is not VLAN challenged,
1335 * so remove the block of adding VLANs over the bond.
1336 */
1337 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1338 }
1339 }
1340
Jay Vosburgh217df672005-09-26 16:11:50 -07001341 /*
1342 * Old ifenslave binaries are no longer supported. These can
1343 * be identified with moderate accurary by the state of the slave:
1344 * the current ifenslave will set the interface down prior to
1345 * enslaving it; the old ifenslave will not.
1346 */
1347 if ((slave_dev->flags & IFF_UP)) {
1348 printk(KERN_ERR DRV_NAME ": %s is up. "
1349 "This may be due to an out of date ifenslave.\n",
1350 slave_dev->name);
1351 res = -EPERM;
1352 goto err_undo_flags;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Moni Shoua872254d2007-10-09 19:43:38 -07001355 /* set bonding device ether type by slave - bonding netdevices are
1356 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1357 * there is a need to override some of the type dependent attribs/funcs.
1358 *
1359 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1360 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1361 */
1362 if (bond->slave_cnt == 0) {
1363 if (slave_dev->type != ARPHRD_ETHER)
1364 bond_setup_by_slave(bond_dev, slave_dev);
1365 } else if (bond_dev->type != slave_dev->type) {
1366 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1367 "from other slaves (%d), can not enslave it.\n",
1368 slave_dev->name,
1369 slave_dev->type, bond_dev->type);
1370 res = -EINVAL;
1371 goto err_undo_flags;
1372 }
1373
Jay Vosburgh217df672005-09-26 16:11:50 -07001374 if (slave_dev->set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001375 if (bond->slave_cnt == 0) {
1376 printk(KERN_WARNING DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001377 ": %s: Warning: The first slave device "
1378 "specified does not support setting the MAC "
1379 "address. Enabling the fail_over_mac option.",
1380 bond_dev->name);
1381 bond->params.fail_over_mac = 1;
1382 } else if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001383 printk(KERN_ERR DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001384 ": %s: Error: The slave device specified "
1385 "does not support setting the MAC address, "
1386 "but fail_over_mac is not enabled.\n"
Moni Shoua2ab82852007-10-09 19:43:39 -07001387 , bond_dev->name);
1388 res = -EOPNOTSUPP;
1389 goto err_undo_flags;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
Joe Jin243cb4e2007-02-06 14:16:40 -08001393 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (!new_slave) {
1395 res = -ENOMEM;
1396 goto err_undo_flags;
1397 }
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* save slave's original flags before calling
1400 * netdev_set_master and dev_open
1401 */
1402 new_slave->original_flags = slave_dev->flags;
1403
Jay Vosburgh217df672005-09-26 16:11:50 -07001404 /*
1405 * Save slave's original ("permanent") mac address for modes
1406 * that need it, and for restoring it upon release, and then
1407 * set it to the master's address
1408 */
1409 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jay Vosburghdd957c52007-10-09 19:57:24 -07001411 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001412 /*
1413 * Set slave to master's mac address. The application already
1414 * set the master's mac address to that of the first slave
1415 */
1416 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1417 addr.sa_family = slave_dev->type;
1418 res = dev_set_mac_address(slave_dev, &addr);
1419 if (res) {
1420 dprintk("Error %d calling set_mac_address\n", res);
1421 goto err_free;
1422 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001425 res = netdev_set_master(slave_dev, bond_dev);
1426 if (res) {
1427 dprintk("Error %d calling netdev_set_master\n", res);
1428 goto err_close;
1429 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001430 /* open the slave since the application closed it */
1431 res = dev_open(slave_dev);
1432 if (res) {
1433 dprintk("Openning slave %s failed\n", slave_dev->name);
1434 goto err_restore_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001438 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if ((bond->params.mode == BOND_MODE_TLB) ||
1441 (bond->params.mode == BOND_MODE_ALB)) {
1442 /* bond_alb_init_slave() must be called before all other stages since
1443 * it might fail and we do not want to have to undo everything
1444 */
1445 res = bond_alb_init_slave(bond, new_slave);
1446 if (res) {
1447 goto err_unset_master;
1448 }
1449 }
1450
1451 /* If the mode USES_PRIMARY, then the new slave gets the
1452 * master's promisc (and mc) settings only if it becomes the
1453 * curr_active_slave, and that is taken care of later when calling
1454 * bond_change_active()
1455 */
1456 if (!USES_PRIMARY(bond->params.mode)) {
1457 /* set promiscuity level to new slave */
1458 if (bond_dev->flags & IFF_PROMISC) {
1459 dev_set_promiscuity(slave_dev, 1);
1460 }
1461
1462 /* set allmulti level to new slave */
1463 if (bond_dev->flags & IFF_ALLMULTI) {
1464 dev_set_allmulti(slave_dev, 1);
1465 }
1466
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001467 netif_tx_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /* upload master's mc_list to new slave */
1469 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1470 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1471 }
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001472 netif_tx_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
1475 if (bond->params.mode == BOND_MODE_8023AD) {
1476 /* add lacpdu mc addr to mc list */
1477 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1478
1479 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1480 }
1481
1482 bond_add_vlans_on_slave(bond, slave_dev);
1483
1484 write_lock_bh(&bond->lock);
1485
1486 bond_attach_slave(bond, new_slave);
1487
1488 new_slave->delay = 0;
1489 new_slave->link_failure_count = 0;
1490
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001491 bond_compute_features(bond);
1492
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001493 new_slave->last_arp_rx = jiffies;
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (bond->params.miimon && !bond->params.use_carrier) {
1496 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1497
1498 if ((link_reporting == -1) && !bond->params.arp_interval) {
1499 /*
1500 * miimon is set but a bonded network driver
1501 * does not support ETHTOOL/MII and
1502 * arp_interval is not set. Note: if
1503 * use_carrier is enabled, we will never go
1504 * here (because netif_carrier is always
1505 * supported); thus, we don't need to change
1506 * the messages for netif_carrier.
1507 */
1508 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001509 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 "available for interface %s, and "
1511 "arp_interval/arp_ip_target module parameters "
1512 "not specified, thus bonding will not detect "
1513 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001514 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 } else if (link_reporting == -1) {
1516 /* unable get link status using mii/ethtool */
1517 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001518 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 "interface %s; the network driver associated "
1520 "with this interface does not support MII or "
1521 "ETHTOOL link status reporting, thus miimon "
1522 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001523 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525 }
1526
1527 /* check for initial state */
1528 if (!bond->params.miimon ||
1529 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1530 if (bond->params.updelay) {
1531 dprintk("Initial state of slave_dev is "
1532 "BOND_LINK_BACK\n");
1533 new_slave->link = BOND_LINK_BACK;
1534 new_slave->delay = bond->params.updelay;
1535 } else {
1536 dprintk("Initial state of slave_dev is "
1537 "BOND_LINK_UP\n");
1538 new_slave->link = BOND_LINK_UP;
1539 }
1540 new_slave->jiffies = jiffies;
1541 } else {
1542 dprintk("Initial state of slave_dev is "
1543 "BOND_LINK_DOWN\n");
1544 new_slave->link = BOND_LINK_DOWN;
1545 }
1546
1547 if (bond_update_speed_duplex(new_slave) &&
1548 (new_slave->link != BOND_LINK_DOWN)) {
1549 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001550 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001552 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 if (bond->params.mode == BOND_MODE_8023AD) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001555 printk(KERN_WARNING DRV_NAME
1556 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001558 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 }
1561
1562 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1563 /* if there is a primary slave, remember it */
1564 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1565 bond->primary_slave = new_slave;
1566 }
1567 }
1568
1569 switch (bond->params.mode) {
1570 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001571 bond_set_slave_inactive_flags(new_slave);
1572 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 break;
1574 case BOND_MODE_8023AD:
1575 /* in 802.3ad mode, the internal mechanism
1576 * will activate the slaves in the selected
1577 * aggregator
1578 */
1579 bond_set_slave_inactive_flags(new_slave);
1580 /* if this is the first slave */
1581 if (bond->slave_cnt == 1) {
1582 SLAVE_AD_INFO(new_slave).id = 1;
1583 /* Initialize AD with the number of times that the AD timer is called in 1 second
1584 * can be called only after the mac address of the bond is set
1585 */
1586 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1587 bond->params.lacp_fast);
1588 } else {
1589 SLAVE_AD_INFO(new_slave).id =
1590 SLAVE_AD_INFO(new_slave->prev).id + 1;
1591 }
1592
1593 bond_3ad_bind_slave(new_slave);
1594 break;
1595 case BOND_MODE_TLB:
1596 case BOND_MODE_ALB:
1597 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001598 bond_set_slave_inactive_flags(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600 default:
1601 dprintk("This slave is always active in trunk mode\n");
1602
1603 /* always active in trunk mode */
1604 new_slave->state = BOND_STATE_ACTIVE;
1605
1606 /* In trunking mode there is little meaning to curr_active_slave
1607 * anyway (it holds no special properties of the bond device),
1608 * so we can change it without calling change_active_interface()
1609 */
1610 if (!bond->curr_active_slave) {
1611 bond->curr_active_slave = new_slave;
1612 }
1613 break;
1614 } /* switch(bond_mode) */
1615
Jay Vosburghff59c452006-03-27 13:27:43 -08001616 bond_set_carrier(bond);
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 write_unlock_bh(&bond->lock);
1619
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001620 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1621 if (res)
1622 goto err_unset_master;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 printk(KERN_INFO DRV_NAME
1625 ": %s: enslaving %s as a%s interface with a%s link.\n",
1626 bond_dev->name, slave_dev->name,
1627 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1628 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1629
1630 /* enslave is successful */
1631 return 0;
1632
1633/* Undo stages on error */
1634err_unset_master:
1635 netdev_set_master(slave_dev, NULL);
1636
1637err_close:
1638 dev_close(slave_dev);
1639
1640err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001641 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001642 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1643 addr.sa_family = slave_dev->type;
1644 dev_set_mac_address(slave_dev, &addr);
1645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647err_free:
1648 kfree(new_slave);
1649
1650err_undo_flags:
1651 bond_dev->features = old_features;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return res;
1654}
1655
1656/*
1657 * Try to release the slave device <slave> from the bond device <master>
1658 * It is legal to access curr_active_slave without a lock because all the function
1659 * is write-locked.
1660 *
1661 * The rules for slave state should be:
1662 * for Active/Backup:
1663 * Active stays on all backups go down
1664 * for Bonded connections:
1665 * The first up interface should be left on and all others downed.
1666 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001667int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 struct bonding *bond = bond_dev->priv;
1670 struct slave *slave, *oldcurrent;
1671 struct sockaddr addr;
1672 int mac_addr_differ;
Joe Perches0795af52007-10-03 17:59:30 -07001673 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* slave is not a slave or master is not master of this slave */
1676 if (!(slave_dev->flags & IFF_SLAVE) ||
1677 (slave_dev->master != bond_dev)) {
1678 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001679 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 bond_dev->name, slave_dev->name);
1681 return -EINVAL;
1682 }
1683
1684 write_lock_bh(&bond->lock);
1685
1686 slave = bond_get_slave_by_dev(bond, slave_dev);
1687 if (!slave) {
1688 /* not a slave of this bond */
1689 printk(KERN_INFO DRV_NAME
1690 ": %s: %s not enslaved\n",
1691 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001692 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return -EINVAL;
1694 }
1695
1696 mac_addr_differ = memcmp(bond_dev->dev_addr,
1697 slave->perm_hwaddr,
1698 ETH_ALEN);
1699 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1700 printk(KERN_WARNING DRV_NAME
Joe Perches0795af52007-10-03 17:59:30 -07001701 ": %s: Warning: the permanent HWaddr of %s - "
1702 "%s - is still in use by %s. "
1703 "Set the HWaddr of %s to a different address "
1704 "to avoid conflicts.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001705 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 slave_dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001707 print_mac(mac, slave->perm_hwaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 bond_dev->name,
1709 slave_dev->name);
1710 }
1711
1712 /* Inform AD package of unbinding of slave. */
1713 if (bond->params.mode == BOND_MODE_8023AD) {
1714 /* must be called before the slave is
1715 * detached from the list
1716 */
1717 bond_3ad_unbind_slave(slave);
1718 }
1719
1720 printk(KERN_INFO DRV_NAME
1721 ": %s: releasing %s interface %s\n",
1722 bond_dev->name,
1723 (slave->state == BOND_STATE_ACTIVE)
1724 ? "active" : "backup",
1725 slave_dev->name);
1726
1727 oldcurrent = bond->curr_active_slave;
1728
1729 bond->current_arp_slave = NULL;
1730
1731 /* release the slave from its bond */
1732 bond_detach_slave(bond, slave);
1733
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001734 bond_compute_features(bond);
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (bond->primary_slave == slave) {
1737 bond->primary_slave = NULL;
1738 }
1739
1740 if (oldcurrent == slave) {
1741 bond_change_active_slave(bond, NULL);
1742 }
1743
1744 if ((bond->params.mode == BOND_MODE_TLB) ||
1745 (bond->params.mode == BOND_MODE_ALB)) {
1746 /* Must be called only after the slave has been
1747 * detached from the list and the curr_active_slave
1748 * has been cleared (if our_slave == old_current),
1749 * but before a new active slave is selected.
1750 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001751 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001753 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001756 if (oldcurrent == slave) {
1757 /*
1758 * Note that we hold RTNL over this sequence, so there
1759 * is no concern that another slave add/remove event
1760 * will interfere.
1761 */
1762 write_unlock_bh(&bond->lock);
1763 read_lock(&bond->lock);
1764 write_lock_bh(&bond->curr_slave_lock);
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 bond_select_active_slave(bond);
1767
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001768 write_unlock_bh(&bond->curr_slave_lock);
1769 read_unlock(&bond->lock);
1770 write_lock_bh(&bond->lock);
1771 }
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001774 bond_set_carrier(bond);
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /* if the last slave was removed, zero the mac address
1777 * of the master so it will be set by the application
1778 * to the mac address of the first slave
1779 */
1780 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1781
1782 if (list_empty(&bond->vlan_list)) {
1783 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1784 } else {
1785 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001786 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001788 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001790 ": %s: When re-adding slaves, make sure the bond's "
1791 "HW address matches its VLANs'.\n",
1792 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1795 !bond_has_challenged_slaves(bond)) {
1796 printk(KERN_INFO DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001797 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001799 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1801 }
1802
1803 write_unlock_bh(&bond->lock);
1804
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001805 /* must do this from outside any spinlocks */
1806 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 bond_del_vlans_from_slave(bond, slave_dev);
1809
1810 /* If the mode USES_PRIMARY, then we should only remove its
1811 * promisc and mc settings if it was the curr_active_slave, but that was
1812 * already taken care of above when we detached the slave
1813 */
1814 if (!USES_PRIMARY(bond->params.mode)) {
1815 /* unset promiscuity level from slave */
1816 if (bond_dev->flags & IFF_PROMISC) {
1817 dev_set_promiscuity(slave_dev, -1);
1818 }
1819
1820 /* unset allmulti level from slave */
1821 if (bond_dev->flags & IFF_ALLMULTI) {
1822 dev_set_allmulti(slave_dev, -1);
1823 }
1824
1825 /* flush master's mc_list from slave */
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001826 netif_tx_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 bond_mc_list_flush(bond_dev, slave_dev);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001828 netif_tx_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830
1831 netdev_set_master(slave_dev, NULL);
1832
1833 /* close slave before restoring its mac address */
1834 dev_close(slave_dev);
1835
Jay Vosburghdd957c52007-10-09 19:57:24 -07001836 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001837 /* restore original ("permanent") mac address */
1838 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1839 addr.sa_family = slave_dev->type;
1840 dev_set_mac_address(slave_dev, &addr);
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001843 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001844 IFF_SLAVE_INACTIVE | IFF_BONDING |
1845 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 kfree(slave);
1848
1849 return 0; /* deletion OK */
1850}
1851
1852/*
Moni Shouad90a1622007-10-09 19:43:43 -07001853* Destroy a bonding device.
1854* Must be under rtnl_lock when this function is called.
1855*/
1856void bond_destroy(struct bonding *bond)
1857{
1858 bond_deinit(bond->dev);
1859 bond_destroy_sysfs_entry(bond);
Jay Vosburgh8cbdeec2007-11-13 21:16:29 -08001860 unregister_netdevice(bond->dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001861}
1862
1863/*
1864* First release a slave and than destroy the bond if no more slaves iare left.
1865* Must be under rtnl_lock when this function is called.
1866*/
1867int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1868{
1869 struct bonding *bond = bond_dev->priv;
1870 int ret;
1871
1872 ret = bond_release(bond_dev, slave_dev);
1873 if ((ret == 0) && (bond->slave_cnt == 0)) {
1874 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1875 bond_dev->name, bond_dev->name);
1876 bond_destroy(bond);
1877 }
1878 return ret;
1879}
1880
1881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 * This function releases all slaves.
1883 */
1884static int bond_release_all(struct net_device *bond_dev)
1885{
1886 struct bonding *bond = bond_dev->priv;
1887 struct slave *slave;
1888 struct net_device *slave_dev;
1889 struct sockaddr addr;
1890
1891 write_lock_bh(&bond->lock);
1892
Jay Vosburghff59c452006-03-27 13:27:43 -08001893 netif_carrier_off(bond_dev);
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (bond->slave_cnt == 0) {
1896 goto out;
1897 }
1898
1899 bond->current_arp_slave = NULL;
1900 bond->primary_slave = NULL;
1901 bond_change_active_slave(bond, NULL);
1902
1903 while ((slave = bond->first_slave) != NULL) {
1904 /* Inform AD package of unbinding of slave
1905 * before slave is detached from the list.
1906 */
1907 if (bond->params.mode == BOND_MODE_8023AD) {
1908 bond_3ad_unbind_slave(slave);
1909 }
1910
1911 slave_dev = slave->dev;
1912 bond_detach_slave(bond, slave);
1913
Jay Vosburgh25433312008-01-17 16:24:59 -08001914 /* now that the slave is detached, unlock and perform
1915 * all the undo steps that should not be called from
1916 * within a lock.
1917 */
1918 write_unlock_bh(&bond->lock);
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if ((bond->params.mode == BOND_MODE_TLB) ||
1921 (bond->params.mode == BOND_MODE_ALB)) {
1922 /* must be called only after the slave
1923 * has been detached from the list
1924 */
1925 bond_alb_deinit_slave(bond, slave);
1926 }
1927
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001928 bond_compute_features(bond);
1929
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001930 bond_destroy_slave_symlinks(bond_dev, slave_dev);
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 */
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001949 netif_tx_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 bond_mc_list_flush(bond_dev, slave_dev);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08001951 netif_tx_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 Vosburghdd957c52007-10-09 19:57:24 -07001959 if (!bond->params.fail_over_mac) {
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 |
1967 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 kfree(slave);
1970
1971 /* re-acquire the lock before getting the next slave */
1972 write_lock_bh(&bond->lock);
1973 }
1974
1975 /* zero the mac address of the master so it will be
1976 * set by the application to the mac address of the
1977 * first slave
1978 */
1979 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1980
1981 if (list_empty(&bond->vlan_list)) {
1982 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1983 } else {
1984 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001985 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001987 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001989 ": %s: When re-adding slaves, make sure the bond's "
1990 "HW address matches its VLANs'.\n",
1991 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
1994 printk(KERN_INFO DRV_NAME
1995 ": %s: released all slaves\n",
1996 bond_dev->name);
1997
1998out:
1999 write_unlock_bh(&bond->lock);
2000
2001 return 0;
2002}
2003
2004/*
2005 * This function changes the active slave to slave <slave_dev>.
2006 * It returns -EINVAL in the following cases.
2007 * - <slave_dev> is not found in the list.
2008 * - There is not active slave now.
2009 * - <slave_dev> is already active.
2010 * - The link state of <slave_dev> is not BOND_LINK_UP.
2011 * - <slave_dev> is not running.
2012 * In these cases, this fuction does nothing.
2013 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2014 */
2015static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2016{
2017 struct bonding *bond = bond_dev->priv;
2018 struct slave *old_active = NULL;
2019 struct slave *new_active = NULL;
2020 int res = 0;
2021
2022 if (!USES_PRIMARY(bond->params.mode)) {
2023 return -EINVAL;
2024 }
2025
2026 /* Verify that master_dev is indeed the master of slave_dev */
2027 if (!(slave_dev->flags & IFF_SLAVE) ||
2028 (slave_dev->master != bond_dev)) {
2029 return -EINVAL;
2030 }
2031
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002032 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002034 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002036 read_unlock(&bond->curr_slave_lock);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 new_active = bond_get_slave_by_dev(bond, slave_dev);
2039
2040 /*
2041 * Changing to the current active: do nothing; return success.
2042 */
2043 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002044 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return 0;
2046 }
2047
2048 if ((new_active) &&
2049 (old_active) &&
2050 (new_active->link == BOND_LINK_UP) &&
2051 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002052 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002054 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 } else {
2056 res = -EINVAL;
2057 }
2058
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002059 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 return res;
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2065{
2066 struct bonding *bond = bond_dev->priv;
2067
2068 info->bond_mode = bond->params.mode;
2069 info->miimon = bond->params.miimon;
2070
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002071 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002073 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 return 0;
2076}
2077
2078static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2079{
2080 struct bonding *bond = bond_dev->priv;
2081 struct slave *slave;
2082 int i, found = 0;
2083
2084 if (info->slave_id < 0) {
2085 return -ENODEV;
2086 }
2087
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002088 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 bond_for_each_slave(bond, slave, i) {
2091 if (i == (int)info->slave_id) {
2092 found = 1;
2093 break;
2094 }
2095 }
2096
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002097 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 if (found) {
2100 strcpy(info->slave_name, slave->dev->name);
2101 info->link = slave->link;
2102 info->state = slave->state;
2103 info->link_failure_count = slave->link_failure_count;
2104 } else {
2105 return -ENODEV;
2106 }
2107
2108 return 0;
2109}
2110
2111/*-------------------------------- Monitoring -------------------------------*/
2112
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002113/*
2114 * if !have_locks, return nonzero if a failover is necessary. if
2115 * have_locks, do whatever failover activities are needed.
2116 *
2117 * This is to separate the inspection and failover steps for locking
2118 * purposes; failover requires rtnl, but acquiring it for every
2119 * inspection is undesirable, so a wrapper first does inspection, and
2120 * the acquires the necessary locks and calls again to perform
2121 * failover if needed. Since all locks are dropped, a complete
2122 * restart is needed between calls.
2123 */
2124static int __bond_mii_monitor(struct bonding *bond, int have_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 struct slave *slave, *oldcurrent;
2127 int do_failover = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 int i;
2129
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002130 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 /* we will try to read the link status of each of our slaves, and
2134 * set their IFF_RUNNING flag appropriately. For each slave not
2135 * supporting MII status, we won't do anything so that a user-space
2136 * program could monitor the link itself if needed.
2137 */
2138
Moni Shoua1053f622007-10-09 19:43:42 -07002139 if (bond->send_grat_arp) {
2140 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2141 &bond->curr_active_slave->dev->state))
2142 dprintk("Needs to send gratuitous arp but not yet\n");
2143 else {
2144 dprintk("sending delayed gratuitous arp on on %s\n",
2145 bond->curr_active_slave->dev->name);
2146 bond_send_gratuitous_arp(bond);
2147 bond->send_grat_arp = 0;
2148 }
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 read_lock(&bond->curr_slave_lock);
2151 oldcurrent = bond->curr_active_slave;
2152 read_unlock(&bond->curr_slave_lock);
2153
2154 bond_for_each_slave(bond, slave, i) {
2155 struct net_device *slave_dev = slave->dev;
2156 int link_state;
2157 u16 old_speed = slave->speed;
2158 u8 old_duplex = slave->duplex;
2159
2160 link_state = bond_check_dev_link(bond, slave_dev, 0);
2161
2162 switch (slave->link) {
2163 case BOND_LINK_UP: /* the link was up */
2164 if (link_state == BMSR_LSTATUS) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002165 if (!oldcurrent) {
2166 if (!have_locks)
2167 return 1;
2168 do_failover = 1;
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 break;
2171 } else { /* link going down */
2172 slave->link = BOND_LINK_FAIL;
2173 slave->delay = bond->params.downdelay;
2174
2175 if (slave->link_failure_count < UINT_MAX) {
2176 slave->link_failure_count++;
2177 }
2178
2179 if (bond->params.downdelay) {
2180 printk(KERN_INFO DRV_NAME
2181 ": %s: link status down for %s "
2182 "interface %s, disabling it in "
2183 "%d ms.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002184 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 IS_UP(slave_dev)
2186 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2187 ? ((slave == oldcurrent)
2188 ? "active " : "backup ")
2189 : "")
2190 : "idle ",
2191 slave_dev->name,
2192 bond->params.downdelay * bond->params.miimon);
2193 }
2194 }
2195 /* no break ! fall through the BOND_LINK_FAIL test to
2196 ensure proper action to be taken
2197 */
2198 case BOND_LINK_FAIL: /* the link has just gone down */
2199 if (link_state != BMSR_LSTATUS) {
2200 /* link stays down */
2201 if (slave->delay <= 0) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002202 if (!have_locks)
2203 return 1;
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 /* link down for too long time */
2206 slave->link = BOND_LINK_DOWN;
2207
2208 /* in active/backup mode, we must
2209 * completely disable this interface
2210 */
2211 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2212 (bond->params.mode == BOND_MODE_8023AD)) {
2213 bond_set_slave_inactive_flags(slave);
2214 }
2215
2216 printk(KERN_INFO DRV_NAME
2217 ": %s: link status definitely "
2218 "down for interface %s, "
2219 "disabling it\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002220 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 slave_dev->name);
2222
2223 /* notify ad that the link status has changed */
2224 if (bond->params.mode == BOND_MODE_8023AD) {
2225 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2226 }
2227
2228 if ((bond->params.mode == BOND_MODE_TLB) ||
2229 (bond->params.mode == BOND_MODE_ALB)) {
2230 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2231 }
2232
2233 if (slave == oldcurrent) {
2234 do_failover = 1;
2235 }
2236 } else {
2237 slave->delay--;
2238 }
2239 } else {
2240 /* link up again */
2241 slave->link = BOND_LINK_UP;
2242 slave->jiffies = jiffies;
2243 printk(KERN_INFO DRV_NAME
2244 ": %s: link status up again after %d "
2245 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002246 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2248 slave_dev->name);
2249 }
2250 break;
2251 case BOND_LINK_DOWN: /* the link was down */
2252 if (link_state != BMSR_LSTATUS) {
2253 /* the link stays down, nothing more to do */
2254 break;
2255 } else { /* link going up */
2256 slave->link = BOND_LINK_BACK;
2257 slave->delay = bond->params.updelay;
2258
2259 if (bond->params.updelay) {
2260 /* if updelay == 0, no need to
2261 advertise about a 0 ms delay */
2262 printk(KERN_INFO DRV_NAME
2263 ": %s: link status up for "
2264 "interface %s, enabling it "
2265 "in %d ms.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002266 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 slave_dev->name,
2268 bond->params.updelay * bond->params.miimon);
2269 }
2270 }
2271 /* no break ! fall through the BOND_LINK_BACK state in
2272 case there's something to do.
2273 */
2274 case BOND_LINK_BACK: /* the link has just come back */
2275 if (link_state != BMSR_LSTATUS) {
2276 /* link down again */
2277 slave->link = BOND_LINK_DOWN;
2278
2279 printk(KERN_INFO DRV_NAME
2280 ": %s: link status down again after %d "
2281 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002282 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 (bond->params.updelay - slave->delay) * bond->params.miimon,
2284 slave_dev->name);
2285 } else {
2286 /* link stays up */
2287 if (slave->delay == 0) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002288 if (!have_locks)
2289 return 1;
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 /* now the link has been up for long time enough */
2292 slave->link = BOND_LINK_UP;
2293 slave->jiffies = jiffies;
2294
2295 if (bond->params.mode == BOND_MODE_8023AD) {
2296 /* prevent it from being the active one */
2297 slave->state = BOND_STATE_BACKUP;
2298 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2299 /* make it immediately active */
2300 slave->state = BOND_STATE_ACTIVE;
2301 } else if (slave != bond->primary_slave) {
2302 /* prevent it from being the active one */
2303 slave->state = BOND_STATE_BACKUP;
2304 }
2305
2306 printk(KERN_INFO DRV_NAME
2307 ": %s: link status definitely "
2308 "up for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002309 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 slave_dev->name);
2311
2312 /* notify ad that the link status has changed */
2313 if (bond->params.mode == BOND_MODE_8023AD) {
2314 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2315 }
2316
2317 if ((bond->params.mode == BOND_MODE_TLB) ||
2318 (bond->params.mode == BOND_MODE_ALB)) {
2319 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2320 }
2321
2322 if ((!oldcurrent) ||
2323 (slave == bond->primary_slave)) {
2324 do_failover = 1;
2325 }
2326 } else {
2327 slave->delay--;
2328 }
2329 }
2330 break;
2331 default:
2332 /* Should not happen */
Mitch Williams4e0952c2005-11-09 10:34:57 -08002333 printk(KERN_ERR DRV_NAME
2334 ": %s: Error: %s Illegal value (link=%d)\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002335 bond->dev->name,
Mitch Williams4e0952c2005-11-09 10:34:57 -08002336 slave->dev->name,
2337 slave->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto out;
2339 } /* end of switch (slave->link) */
2340
2341 bond_update_speed_duplex(slave);
2342
2343 if (bond->params.mode == BOND_MODE_8023AD) {
2344 if (old_speed != slave->speed) {
2345 bond_3ad_adapter_speed_changed(slave);
2346 }
2347
2348 if (old_duplex != slave->duplex) {
2349 bond_3ad_adapter_duplex_changed(slave);
2350 }
2351 }
2352
2353 } /* end of for */
2354
2355 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002356 ASSERT_RTNL();
2357
2358 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 bond_select_active_slave(bond);
2361
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002362 write_unlock_bh(&bond->curr_slave_lock);
2363
Jay Vosburghff59c452006-03-27 13:27:43 -08002364 } else
2365 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002371/*
2372 * bond_mii_monitor
2373 *
2374 * Really a wrapper that splits the mii monitor into two phases: an
2375 * inspection, then (if inspection indicates something needs to be
2376 * done) an acquisition of appropriate locks followed by another pass
2377 * to implement whatever link state changes are indicated.
2378 */
2379void bond_mii_monitor(struct work_struct *work)
2380{
2381 struct bonding *bond = container_of(work, struct bonding,
2382 mii_work.work);
2383 unsigned long delay;
2384
2385 read_lock(&bond->lock);
2386 if (bond->kill_timers) {
2387 read_unlock(&bond->lock);
2388 return;
2389 }
2390 if (__bond_mii_monitor(bond, 0)) {
2391 read_unlock(&bond->lock);
2392 rtnl_lock();
2393 read_lock(&bond->lock);
2394 __bond_mii_monitor(bond, 1);
Jay Vosburgh56556622008-01-17 16:25:03 -08002395 read_unlock(&bond->lock);
2396 rtnl_unlock(); /* might sleep, hold no other locks */
2397 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002398 }
2399
2400 delay = ((bond->params.miimon * HZ) / 1000) ? : 1;
2401 read_unlock(&bond->lock);
2402 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2403}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002404
Al Virod3bb52b2007-08-22 20:06:58 -04002405static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002406{
2407 struct in_device *idev;
2408 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002409 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002410
2411 if (!dev)
2412 return 0;
2413
2414 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002415 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002416 if (!idev)
2417 goto out;
2418
2419 ifa = idev->ifa_list;
2420 if (!ifa)
2421 goto out;
2422
2423 addr = ifa->ifa_local;
2424out:
2425 rcu_read_unlock();
2426 return addr;
2427}
2428
2429static int bond_has_ip(struct bonding *bond)
2430{
2431 struct vlan_entry *vlan, *vlan_next;
2432
2433 if (bond->master_ip)
2434 return 1;
2435
2436 if (list_empty(&bond->vlan_list))
2437 return 0;
2438
2439 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2440 vlan_list) {
2441 if (vlan->vlan_ip)
2442 return 1;
2443 }
2444
2445 return 0;
2446}
2447
Al Virod3bb52b2007-08-22 20:06:58 -04002448static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002449{
2450 struct vlan_entry *vlan, *vlan_next;
2451
2452 if (ip == bond->master_ip)
2453 return 1;
2454
2455 if (list_empty(&bond->vlan_list))
2456 return 0;
2457
2458 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2459 vlan_list) {
2460 if (ip == vlan->vlan_ip)
2461 return 1;
2462 }
2463
2464 return 0;
2465}
2466
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002467/*
2468 * We go to the (large) trouble of VLAN tagging ARP frames because
2469 * switches in VLAN mode (especially if ports are configured as
2470 * "native" to a VLAN) might not pass non-tagged frames.
2471 */
Al Virod3bb52b2007-08-22 20:06:58 -04002472static 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 -04002473{
2474 struct sk_buff *skb;
2475
2476 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2477 slave_dev->name, dest_ip, src_ip, vlan_id);
2478
2479 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2480 NULL, slave_dev->dev_addr, NULL);
2481
2482 if (!skb) {
2483 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2484 return;
2485 }
2486 if (vlan_id) {
2487 skb = vlan_put_tag(skb, vlan_id);
2488 if (!skb) {
2489 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2490 return;
2491 }
2492 }
2493 arp_xmit(skb);
2494}
2495
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2498{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002499 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002500 __be32 *targets = bond->params.arp_targets;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002501 struct vlan_entry *vlan, *vlan_next;
2502 struct net_device *vlan_dev;
2503 struct flowi fl;
2504 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Mitch Williams6b780562005-11-09 10:36:19 -08002506 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2507 if (!targets[i])
2508 continue;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002509 dprintk("basa: target %x\n", targets[i]);
2510 if (list_empty(&bond->vlan_list)) {
2511 dprintk("basa: empty vlan: arp_send\n");
2512 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2513 bond->master_ip, 0);
2514 continue;
2515 }
2516
2517 /*
2518 * If VLANs are configured, we do a route lookup to
2519 * determine which VLAN interface would be used, so we
2520 * can tag the ARP with the proper VLAN tag.
2521 */
2522 memset(&fl, 0, sizeof(fl));
2523 fl.fl4_dst = targets[i];
2524 fl.fl4_tos = RTO_ONLINK;
2525
Denis V. Lunevf2063512008-01-22 22:07:34 -08002526 rv = ip_route_output_key(&init_net, &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002527 if (rv) {
2528 if (net_ratelimit()) {
2529 printk(KERN_WARNING DRV_NAME
2530 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2531 bond->dev->name, NIPQUAD(fl.fl4_dst));
2532 }
2533 continue;
2534 }
2535
2536 /*
2537 * This target is not on a VLAN
2538 */
2539 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002540 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002541 dprintk("basa: rtdev == bond->dev: arp_send\n");
2542 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2543 bond->master_ip, 0);
2544 continue;
2545 }
2546
2547 vlan_id = 0;
2548 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2549 vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002550 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002551 if (vlan_dev == rt->u.dst.dev) {
2552 vlan_id = vlan->vlan_id;
2553 dprintk("basa: vlan match on %s %d\n",
2554 vlan_dev->name, vlan_id);
2555 break;
2556 }
2557 }
2558
2559 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002560 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002561 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2562 vlan->vlan_ip, vlan_id);
2563 continue;
2564 }
2565
2566 if (net_ratelimit()) {
2567 printk(KERN_WARNING DRV_NAME
2568 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2569 bond->dev->name, NIPQUAD(fl.fl4_dst),
2570 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2571 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002572 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002573 }
2574}
2575
2576/*
2577 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2578 * for each VLAN above us.
2579 */
2580static void bond_send_gratuitous_arp(struct bonding *bond)
2581{
2582 struct slave *slave = bond->curr_active_slave;
2583 struct vlan_entry *vlan;
2584 struct net_device *vlan_dev;
2585
2586 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2587 slave ? slave->dev->name : "NULL");
2588 if (!slave)
2589 return;
2590
2591 if (bond->master_ip) {
2592 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002593 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002594 }
2595
2596 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002597 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002598 if (vlan->vlan_ip) {
2599 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2600 vlan->vlan_ip, vlan->vlan_id);
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603}
2604
Al Virod3bb52b2007-08-22 20:06:58 -04002605static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002606{
2607 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002608 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002609
2610 targets = bond->params.arp_targets;
2611 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2612 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2613 "%u.%u.%u.%u bhti(tip) %d\n",
2614 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2615 bond_has_this_ip(bond, tip));
2616 if (sip == targets[i]) {
2617 if (bond_has_this_ip(bond, tip))
2618 slave->last_arp_rx = jiffies;
2619 return;
2620 }
2621 }
2622}
2623
2624static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2625{
2626 struct arphdr *arp;
2627 struct slave *slave;
2628 struct bonding *bond;
2629 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002630 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002631
Eric W. Biedermane730c152007-09-17 11:53:39 -07002632 if (dev->nd_net != &init_net)
2633 goto out;
2634
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002635 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2636 goto out;
2637
2638 bond = dev->priv;
2639 read_lock(&bond->lock);
2640
2641 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2642 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2643 orig_dev ? orig_dev->name : "NULL");
2644
2645 slave = bond_get_slave_by_dev(bond, orig_dev);
2646 if (!slave || !slave_do_arp_validate(bond, slave))
2647 goto out_unlock;
2648
2649 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2650 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2651 (2 * dev->addr_len) +
2652 (2 * sizeof(u32)))))
2653 goto out_unlock;
2654
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002655 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002656 if (arp->ar_hln != dev->addr_len ||
2657 skb->pkt_type == PACKET_OTHERHOST ||
2658 skb->pkt_type == PACKET_LOOPBACK ||
2659 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2660 arp->ar_pro != htons(ETH_P_IP) ||
2661 arp->ar_pln != 4)
2662 goto out_unlock;
2663
2664 arp_ptr = (unsigned char *)(arp + 1);
2665 arp_ptr += dev->addr_len;
2666 memcpy(&sip, arp_ptr, 4);
2667 arp_ptr += 4 + dev->addr_len;
2668 memcpy(&tip, arp_ptr, 4);
2669
2670 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2671 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2672 slave->state, bond->params.arp_validate,
2673 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2674
2675 /*
2676 * Backup slaves won't see the ARP reply, but do come through
2677 * here for each ARP probe (so we swap the sip/tip to validate
2678 * the probe). In a "redundant switch, common router" type of
2679 * configuration, the ARP probe will (hopefully) travel from
2680 * the active, through one switch, the router, then the other
2681 * switch before reaching the backup.
2682 */
2683 if (slave->state == BOND_STATE_ACTIVE)
2684 bond_validate_arp(bond, slave, sip, tip);
2685 else
2686 bond_validate_arp(bond, slave, tip, sip);
2687
2688out_unlock:
2689 read_unlock(&bond->lock);
2690out:
2691 dev_kfree_skb(skb);
2692 return NET_RX_SUCCESS;
2693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695/*
2696 * this function is called regularly to monitor each slave's link
2697 * ensuring that traffic is being sent and received when arp monitoring
2698 * is used in load-balancing mode. if the adapter has been dormant, then an
2699 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2700 * arp monitoring in active backup mode.
2701 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002702void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002704 struct bonding *bond = container_of(work, struct bonding,
2705 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 struct slave *slave, *oldcurrent;
2707 int do_failover = 0;
2708 int delta_in_ticks;
2709 int i;
2710
2711 read_lock(&bond->lock);
2712
2713 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2714
2715 if (bond->kill_timers) {
2716 goto out;
2717 }
2718
2719 if (bond->slave_cnt == 0) {
2720 goto re_arm;
2721 }
2722
2723 read_lock(&bond->curr_slave_lock);
2724 oldcurrent = bond->curr_active_slave;
2725 read_unlock(&bond->curr_slave_lock);
2726
2727 /* see if any of the previous devices are up now (i.e. they have
2728 * xmt and rcv traffic). the curr_active_slave does not come into
2729 * the picture unless it is null. also, slave->jiffies is not needed
2730 * here because we send an arp on each slave and give a slave as
2731 * long as it needs to get the tx/rx within the delta.
2732 * TODO: what about up/down delay in arp mode? it wasn't here before
2733 * so it can wait
2734 */
2735 bond_for_each_slave(bond, slave, i) {
2736 if (slave->link != BOND_LINK_UP) {
David Sterbab63bb732007-12-06 23:40:33 -08002737 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2738 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 slave->link = BOND_LINK_UP;
2741 slave->state = BOND_STATE_ACTIVE;
2742
2743 /* primary_slave has no meaning in round-robin
2744 * mode. the window of a slave being up and
2745 * curr_active_slave being null after enslaving
2746 * is closed.
2747 */
2748 if (!oldcurrent) {
2749 printk(KERN_INFO DRV_NAME
2750 ": %s: link status definitely "
2751 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002752 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 slave->dev->name);
2754 do_failover = 1;
2755 } else {
2756 printk(KERN_INFO DRV_NAME
2757 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002758 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 slave->dev->name);
2760 }
2761 }
2762 } else {
2763 /* slave->link == BOND_LINK_UP */
2764
2765 /* not all switches will respond to an arp request
2766 * when the source ip is 0, so don't take the link down
2767 * if we don't know our ip yet
2768 */
David Sterbab63bb732007-12-06 23:40:33 -08002769 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2770 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002771 bond_has_ip(bond))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 slave->link = BOND_LINK_DOWN;
2774 slave->state = BOND_STATE_BACKUP;
2775
2776 if (slave->link_failure_count < UINT_MAX) {
2777 slave->link_failure_count++;
2778 }
2779
2780 printk(KERN_INFO DRV_NAME
2781 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002782 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 slave->dev->name);
2784
2785 if (slave == oldcurrent) {
2786 do_failover = 1;
2787 }
2788 }
2789 }
2790
2791 /* note: if switch is in round-robin mode, all links
2792 * must tx arp to ensure all links rx an arp - otherwise
2793 * links may oscillate or not come up at all; if switch is
2794 * in something like xor mode, there is nothing we can
2795 * do - all replies will be rx'ed on same link causing slaves
2796 * to be unstable during low/no traffic periods
2797 */
2798 if (IS_UP(slave->dev)) {
2799 bond_arp_send_all(bond, slave);
2800 }
2801 }
2802
2803 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002804 rtnl_lock();
2805 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 bond_select_active_slave(bond);
2808
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002809 write_unlock_bh(&bond->curr_slave_lock);
2810 rtnl_unlock();
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813
2814re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002815 if (bond->params.arp_interval)
2816 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817out:
2818 read_unlock(&bond->lock);
2819}
2820
2821/*
2822 * When using arp monitoring in active-backup mode, this function is
2823 * called to determine if any backup slaves have went down or a new
2824 * current slave needs to be found.
2825 * The backup slaves never generate traffic, they are considered up by merely
2826 * receiving traffic. If the current slave goes down, each backup slave will
2827 * be given the opportunity to tx/rx an arp before being taken down - this
2828 * prevents all slaves from being taken down due to the current slave not
2829 * sending any traffic for the backups to receive. The arps are not necessarily
2830 * necessary, any tx and rx traffic will keep the current slave up. While any
2831 * rx traffic will keep the backup slaves up, the current slave is responsible
2832 * for generating traffic to keep them up regardless of any other traffic they
2833 * may have received.
2834 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2835 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002836void bond_activebackup_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002838 struct bonding *bond = container_of(work, struct bonding,
2839 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 struct slave *slave;
2841 int delta_in_ticks;
2842 int i;
2843
2844 read_lock(&bond->lock);
2845
2846 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2847
2848 if (bond->kill_timers) {
2849 goto out;
2850 }
2851
2852 if (bond->slave_cnt == 0) {
2853 goto re_arm;
2854 }
2855
2856 /* determine if any slave has come up or any backup slave has
2857 * gone down
2858 * TODO: what about up/down delay in arp mode? it wasn't here before
2859 * so it can wait
2860 */
2861 bond_for_each_slave(bond, slave, i) {
2862 if (slave->link != BOND_LINK_UP) {
David Sterbab63bb732007-12-06 23:40:33 -08002863 if (time_before_eq(jiffies,
2864 slave_last_rx(bond, slave) + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 slave->link = BOND_LINK_UP;
2867
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002868 rtnl_lock();
2869
2870 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 if ((!bond->curr_active_slave) &&
David Sterbab63bb732007-12-06 23:40:33 -08002873 time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 bond_change_active_slave(bond, slave);
2875 bond->current_arp_slave = NULL;
2876 } else if (bond->curr_active_slave != slave) {
2877 /* this slave has just come up but we
2878 * already have a current slave; this
2879 * can also happen if bond_enslave adds
2880 * a new slave that is up while we are
2881 * searching for a new slave
2882 */
2883 bond_set_slave_inactive_flags(slave);
2884 bond->current_arp_slave = NULL;
2885 }
2886
Jay Vosburghff59c452006-03-27 13:27:43 -08002887 bond_set_carrier(bond);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (slave == bond->curr_active_slave) {
2890 printk(KERN_INFO DRV_NAME
2891 ": %s: %s is up and now the "
2892 "active interface\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002893 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 slave->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08002895 netif_carrier_on(bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 } else {
2897 printk(KERN_INFO DRV_NAME
2898 ": %s: backup interface %s is "
2899 "now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002900 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 slave->dev->name);
2902 }
2903
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002904 write_unlock_bh(&bond->curr_slave_lock);
2905 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907 } else {
2908 read_lock(&bond->curr_slave_lock);
2909
2910 if ((slave != bond->curr_active_slave) &&
2911 (!bond->current_arp_slave) &&
David Sterbab63bb732007-12-06 23:40:33 -08002912 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 3*delta_in_ticks) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002913 bond_has_ip(bond))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /* a backup slave has gone down; three times
2915 * the delta allows the current slave to be
2916 * taken out before the backup slave.
2917 * note: a non-null current_arp_slave indicates
2918 * the curr_active_slave went down and we are
2919 * searching for a new one; under this
2920 * condition we only take the curr_active_slave
2921 * down - this gives each slave a chance to
2922 * tx/rx traffic before being taken out
2923 */
2924
2925 read_unlock(&bond->curr_slave_lock);
2926
2927 slave->link = BOND_LINK_DOWN;
2928
2929 if (slave->link_failure_count < UINT_MAX) {
2930 slave->link_failure_count++;
2931 }
2932
2933 bond_set_slave_inactive_flags(slave);
2934
2935 printk(KERN_INFO DRV_NAME
2936 ": %s: backup interface %s is now down\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002937 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 slave->dev->name);
2939 } else {
2940 read_unlock(&bond->curr_slave_lock);
2941 }
2942 }
2943 }
2944
2945 read_lock(&bond->curr_slave_lock);
2946 slave = bond->curr_active_slave;
2947 read_unlock(&bond->curr_slave_lock);
2948
2949 if (slave) {
2950 /* if we have sent traffic in the past 2*arp_intervals but
2951 * haven't xmit and rx traffic in that time interval, select
2952 * a different slave. slave->jiffies is only updated when
2953 * a slave first becomes the curr_active_slave - not necessarily
2954 * after every arp; this ensures the slave has a full 2*delta
2955 * before being taken out. if a primary is being used, check
2956 * if it is up and needs to take over as the curr_active_slave
2957 */
David Sterbab63bb732007-12-06 23:40:33 -08002958 if ((time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
2959 (time_after_eq(jiffies, slave_last_rx(bond, slave) + 2*delta_in_ticks) &&
2960 bond_has_ip(bond))) &&
2961 time_after_eq(jiffies, slave->jiffies + 2*delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 slave->link = BOND_LINK_DOWN;
2964
2965 if (slave->link_failure_count < UINT_MAX) {
2966 slave->link_failure_count++;
2967 }
2968
2969 printk(KERN_INFO DRV_NAME
2970 ": %s: link status down for active interface "
2971 "%s, disabling it\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002972 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 slave->dev->name);
2974
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002975 rtnl_lock();
2976 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 bond_select_active_slave(bond);
2979 slave = bond->curr_active_slave;
2980
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002981 write_unlock_bh(&bond->curr_slave_lock);
2982
2983 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 bond->current_arp_slave = slave;
2986
2987 if (slave) {
2988 slave->jiffies = jiffies;
2989 }
2990 } else if ((bond->primary_slave) &&
2991 (bond->primary_slave != slave) &&
2992 (bond->primary_slave->link == BOND_LINK_UP)) {
2993 /* at this point, slave is the curr_active_slave */
2994 printk(KERN_INFO DRV_NAME
2995 ": %s: changing from interface %s to primary "
2996 "interface %s\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002997 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 slave->dev->name,
2999 bond->primary_slave->dev->name);
3000
3001 /* primary is up so switch to it */
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003002 rtnl_lock();
3003 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 bond_change_active_slave(bond, bond->primary_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003005 write_unlock_bh(&bond->curr_slave_lock);
3006
3007 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 slave = bond->primary_slave;
3010 slave->jiffies = jiffies;
3011 } else {
3012 bond->current_arp_slave = NULL;
3013 }
3014
3015 /* the current slave must tx an arp to ensure backup slaves
3016 * rx traffic
3017 */
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003018 if (slave && bond_has_ip(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 bond_arp_send_all(bond, slave);
3020 }
3021 }
3022
3023 /* if we don't have a curr_active_slave, search for the next available
3024 * backup slave from the current_arp_slave and make it the candidate
3025 * for becoming the curr_active_slave
3026 */
3027 if (!slave) {
3028 if (!bond->current_arp_slave) {
3029 bond->current_arp_slave = bond->first_slave;
3030 }
3031
3032 if (bond->current_arp_slave) {
3033 bond_set_slave_inactive_flags(bond->current_arp_slave);
3034
3035 /* search for next candidate */
Jay Vosburgh2f872f02005-05-26 12:56:59 -07003036 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (IS_UP(slave->dev)) {
3038 slave->link = BOND_LINK_BACK;
3039 bond_set_slave_active_flags(slave);
3040 bond_arp_send_all(bond, slave);
3041 slave->jiffies = jiffies;
3042 bond->current_arp_slave = slave;
3043 break;
3044 }
3045
3046 /* if the link state is up at this point, we
3047 * mark it down - this can happen if we have
3048 * simultaneous link failures and
3049 * reselect_active_interface doesn't make this
3050 * one the current slave so it is still marked
3051 * up when it is actually down
3052 */
3053 if (slave->link == BOND_LINK_UP) {
3054 slave->link = BOND_LINK_DOWN;
3055 if (slave->link_failure_count < UINT_MAX) {
3056 slave->link_failure_count++;
3057 }
3058
3059 bond_set_slave_inactive_flags(slave);
3060
3061 printk(KERN_INFO DRV_NAME
3062 ": %s: backup interface %s is "
3063 "now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003064 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 slave->dev->name);
3066 }
3067 }
3068 }
3069 }
3070
3071re_arm:
3072 if (bond->params.arp_interval) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003073 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075out:
3076 read_unlock(&bond->lock);
3077}
3078
3079/*------------------------------ proc/seq_file-------------------------------*/
3080
3081#ifdef CONFIG_PROC_FS
3082
3083#define SEQ_START_TOKEN ((void *)1)
3084
3085static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3086{
3087 struct bonding *bond = seq->private;
3088 loff_t off = 0;
3089 struct slave *slave;
3090 int i;
3091
3092 /* make sure the bond won't be taken away */
3093 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003094 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 if (*pos == 0) {
3097 return SEQ_START_TOKEN;
3098 }
3099
3100 bond_for_each_slave(bond, slave, i) {
3101 if (++off == *pos) {
3102 return slave;
3103 }
3104 }
3105
3106 return NULL;
3107}
3108
3109static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3110{
3111 struct bonding *bond = seq->private;
3112 struct slave *slave = v;
3113
3114 ++*pos;
3115 if (v == SEQ_START_TOKEN) {
3116 return bond->first_slave;
3117 }
3118
3119 slave = slave->next;
3120
3121 return (slave == bond->first_slave) ? NULL : slave;
3122}
3123
3124static void bond_info_seq_stop(struct seq_file *seq, void *v)
3125{
3126 struct bonding *bond = seq->private;
3127
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003128 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 read_unlock(&dev_base_lock);
3130}
3131
3132static void bond_info_show_master(struct seq_file *seq)
3133{
3134 struct bonding *bond = seq->private;
3135 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003136 int i;
3137 u32 target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 read_lock(&bond->curr_slave_lock);
3140 curr = bond->curr_active_slave;
3141 read_unlock(&bond->curr_slave_lock);
3142
Jay Vosburghdd957c52007-10-09 19:57:24 -07003143 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 bond_mode_name(bond->params.mode));
3145
Jay Vosburghdd957c52007-10-09 19:57:24 -07003146 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3147 bond->params.fail_over_mac)
3148 seq_printf(seq, " (fail_over_mac)");
3149
3150 seq_printf(seq, "\n");
3151
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003152 if (bond->params.mode == BOND_MODE_XOR ||
3153 bond->params.mode == BOND_MODE_8023AD) {
3154 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3155 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3156 bond->params.xmit_policy);
3157 }
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (USES_PRIMARY(bond->params.mode)) {
3160 seq_printf(seq, "Primary Slave: %s\n",
Mitch Williams0f418b22005-11-09 10:35:21 -08003161 (bond->primary_slave) ?
3162 bond->primary_slave->dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 seq_printf(seq, "Currently Active Slave: %s\n",
3165 (curr) ? curr->dev->name : "None");
3166 }
3167
Jay Vosburghff59c452006-03-27 13:27:43 -08003168 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3169 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3171 seq_printf(seq, "Up Delay (ms): %d\n",
3172 bond->params.updelay * bond->params.miimon);
3173 seq_printf(seq, "Down Delay (ms): %d\n",
3174 bond->params.downdelay * bond->params.miimon);
3175
Mitch Williams4756b022005-11-09 10:36:25 -08003176
3177 /* ARP information */
3178 if(bond->params.arp_interval > 0) {
3179 int printed=0;
3180 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3181 bond->params.arp_interval);
3182
3183 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3184
3185 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3186 if (!bond->params.arp_targets[i])
3187 continue;
3188 if (printed)
3189 seq_printf(seq, ",");
3190 target = ntohl(bond->params.arp_targets[i]);
3191 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3192 printed = 1;
3193 }
3194 seq_printf(seq, "\n");
3195 }
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 if (bond->params.mode == BOND_MODE_8023AD) {
3198 struct ad_info ad_info;
Joe Perches0795af52007-10-03 17:59:30 -07003199 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 seq_puts(seq, "\n802.3ad info\n");
3202 seq_printf(seq, "LACP rate: %s\n",
3203 (bond->params.lacp_fast) ? "fast" : "slow");
3204
3205 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3206 seq_printf(seq, "bond %s has no active aggregator\n",
3207 bond->dev->name);
3208 } else {
3209 seq_printf(seq, "Active Aggregator Info:\n");
3210
3211 seq_printf(seq, "\tAggregator ID: %d\n",
3212 ad_info.aggregator_id);
3213 seq_printf(seq, "\tNumber of ports: %d\n",
3214 ad_info.ports);
3215 seq_printf(seq, "\tActor Key: %d\n",
3216 ad_info.actor_key);
3217 seq_printf(seq, "\tPartner Key: %d\n",
3218 ad_info.partner_key);
Joe Perches0795af52007-10-03 17:59:30 -07003219 seq_printf(seq, "\tPartner Mac Address: %s\n",
3220 print_mac(mac, ad_info.partner_system));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
3222 }
3223}
3224
3225static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3226{
3227 struct bonding *bond = seq->private;
Joe Perches0795af52007-10-03 17:59:30 -07003228 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3231 seq_printf(seq, "MII Status: %s\n",
3232 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003233 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 slave->link_failure_count);
3235
Jay Vosburgh217df672005-09-26 16:11:50 -07003236 seq_printf(seq,
Joe Perches0795af52007-10-03 17:59:30 -07003237 "Permanent HW addr: %s\n",
3238 print_mac(mac, slave->perm_hwaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 if (bond->params.mode == BOND_MODE_8023AD) {
3241 const struct aggregator *agg
3242 = SLAVE_AD_INFO(slave).port.aggregator;
3243
3244 if (agg) {
3245 seq_printf(seq, "Aggregator ID: %d\n",
3246 agg->aggregator_identifier);
3247 } else {
3248 seq_puts(seq, "Aggregator ID: N/A\n");
3249 }
3250 }
3251}
3252
3253static int bond_info_seq_show(struct seq_file *seq, void *v)
3254{
3255 if (v == SEQ_START_TOKEN) {
3256 seq_printf(seq, "%s\n", version);
3257 bond_info_show_master(seq);
3258 } else {
3259 bond_info_show_slave(seq, v);
3260 }
3261
3262 return 0;
3263}
3264
3265static struct seq_operations bond_info_seq_ops = {
3266 .start = bond_info_seq_start,
3267 .next = bond_info_seq_next,
3268 .stop = bond_info_seq_stop,
3269 .show = bond_info_seq_show,
3270};
3271
3272static int bond_info_open(struct inode *inode, struct file *file)
3273{
3274 struct seq_file *seq;
3275 struct proc_dir_entry *proc;
3276 int res;
3277
3278 res = seq_open(file, &bond_info_seq_ops);
3279 if (!res) {
3280 /* recover the pointer buried in proc_dir_entry data */
3281 seq = file->private_data;
3282 proc = PDE(inode);
3283 seq->private = proc->data;
3284 }
3285
3286 return res;
3287}
3288
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003289static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 .owner = THIS_MODULE,
3291 .open = bond_info_open,
3292 .read = seq_read,
3293 .llseek = seq_lseek,
3294 .release = seq_release,
3295};
3296
3297static int bond_create_proc_entry(struct bonding *bond)
3298{
3299 struct net_device *bond_dev = bond->dev;
3300
3301 if (bond_proc_dir) {
3302 bond->proc_entry = create_proc_entry(bond_dev->name,
3303 S_IRUGO,
3304 bond_proc_dir);
3305 if (bond->proc_entry == NULL) {
3306 printk(KERN_WARNING DRV_NAME
3307 ": Warning: Cannot create /proc/net/%s/%s\n",
3308 DRV_NAME, bond_dev->name);
3309 } else {
3310 bond->proc_entry->data = bond;
3311 bond->proc_entry->proc_fops = &bond_info_fops;
3312 bond->proc_entry->owner = THIS_MODULE;
3313 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3314 }
3315 }
3316
3317 return 0;
3318}
3319
3320static void bond_remove_proc_entry(struct bonding *bond)
3321{
3322 if (bond_proc_dir && bond->proc_entry) {
3323 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3324 memset(bond->proc_file_name, 0, IFNAMSIZ);
3325 bond->proc_entry = NULL;
3326 }
3327}
3328
3329/* Create the bonding directory under /proc/net, if doesn't exist yet.
3330 * Caller must hold rtnl_lock.
3331 */
3332static void bond_create_proc_dir(void)
3333{
3334 int len = strlen(DRV_NAME);
3335
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003336 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 bond_proc_dir = bond_proc_dir->next) {
3338 if ((bond_proc_dir->namelen == len) &&
3339 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3340 break;
3341 }
3342 }
3343
3344 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003345 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (bond_proc_dir) {
3347 bond_proc_dir->owner = THIS_MODULE;
3348 } else {
3349 printk(KERN_WARNING DRV_NAME
3350 ": Warning: cannot create /proc/net/%s\n",
3351 DRV_NAME);
3352 }
3353 }
3354}
3355
3356/* Destroy the bonding directory under /proc/net, if empty.
3357 * Caller must hold rtnl_lock.
3358 */
3359static void bond_destroy_proc_dir(void)
3360{
3361 struct proc_dir_entry *de;
3362
3363 if (!bond_proc_dir) {
3364 return;
3365 }
3366
3367 /* verify that the /proc dir is empty */
3368 for (de = bond_proc_dir->subdir; de; de = de->next) {
3369 /* ignore . and .. */
3370 if (*(de->name) != '.') {
3371 break;
3372 }
3373 }
3374
3375 if (de) {
3376 if (bond_proc_dir->owner == THIS_MODULE) {
3377 bond_proc_dir->owner = NULL;
3378 }
3379 } else {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003380 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 bond_proc_dir = NULL;
3382 }
3383}
3384#endif /* CONFIG_PROC_FS */
3385
3386/*-------------------------- netdev event handling --------------------------*/
3387
3388/*
3389 * Change device name
3390 */
3391static int bond_event_changename(struct bonding *bond)
3392{
3393#ifdef CONFIG_PROC_FS
3394 bond_remove_proc_entry(bond);
3395 bond_create_proc_entry(bond);
3396#endif
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003397 down_write(&(bonding_rwsem));
3398 bond_destroy_sysfs_entry(bond);
3399 bond_create_sysfs_entry(bond);
3400 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return NOTIFY_DONE;
3402}
3403
3404static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3405{
3406 struct bonding *event_bond = bond_dev->priv;
3407
3408 switch (event) {
3409 case NETDEV_CHANGENAME:
3410 return bond_event_changename(event_bond);
3411 case NETDEV_UNREGISTER:
Jay Vosburgh3b96c852008-01-17 16:25:00 -08003412 bond_release_all(event_bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 break;
3414 default:
3415 break;
3416 }
3417
3418 return NOTIFY_DONE;
3419}
3420
3421static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3422{
3423 struct net_device *bond_dev = slave_dev->master;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003424 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 switch (event) {
3427 case NETDEV_UNREGISTER:
3428 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003429 if (bond->setup_by_slave)
3430 bond_release_and_destroy(bond_dev, slave_dev);
3431 else
3432 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
3434 break;
3435 case NETDEV_CHANGE:
3436 /*
3437 * TODO: is this what we get if somebody
3438 * sets up a hierarchical bond, then rmmod's
3439 * one of the slave bonding devices?
3440 */
3441 break;
3442 case NETDEV_DOWN:
3443 /*
3444 * ... Or is it this?
3445 */
3446 break;
3447 case NETDEV_CHANGEMTU:
3448 /*
3449 * TODO: Should slaves be allowed to
3450 * independently alter their MTU? For
3451 * an active-backup bond, slaves need
3452 * not be the same type of device, so
3453 * MTUs may vary. For other modes,
3454 * slaves arguably should have the
3455 * same MTUs. To do this, we'd need to
3456 * take over the slave's change_mtu
3457 * function for the duration of their
3458 * servitude.
3459 */
3460 break;
3461 case NETDEV_CHANGENAME:
3462 /*
3463 * TODO: handle changing the primary's name
3464 */
3465 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003466 case NETDEV_FEAT_CHANGE:
3467 bond_compute_features(bond);
3468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 default:
3470 break;
3471 }
3472
3473 return NOTIFY_DONE;
3474}
3475
3476/*
3477 * bond_netdev_event: handle netdev notifier chain events.
3478 *
3479 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003480 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 * locks for us to safely manipulate the slave devices (RTNL lock,
3482 * dev_probe_lock).
3483 */
3484static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3485{
3486 struct net_device *event_dev = (struct net_device *)ptr;
3487
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003488 if (event_dev->nd_net != &init_net)
3489 return NOTIFY_DONE;
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 dprintk("event_dev: %s, event: %lx\n",
3492 (event_dev ? event_dev->name : "None"),
3493 event);
3494
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003495 if (!(event_dev->priv_flags & IFF_BONDING))
3496 return NOTIFY_DONE;
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 if (event_dev->flags & IFF_MASTER) {
3499 dprintk("IFF_MASTER\n");
3500 return bond_master_netdev_event(event, event_dev);
3501 }
3502
3503 if (event_dev->flags & IFF_SLAVE) {
3504 dprintk("IFF_SLAVE\n");
3505 return bond_slave_netdev_event(event, event_dev);
3506 }
3507
3508 return NOTIFY_DONE;
3509}
3510
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003511/*
3512 * bond_inetaddr_event: handle inetaddr notifier chain events.
3513 *
3514 * We keep track of device IPs primarily to use as source addresses in
3515 * ARP monitor probes (rather than spewing out broadcasts all the time).
3516 *
3517 * We track one IP for the main device (if it has one), plus one per VLAN.
3518 */
3519static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3520{
3521 struct in_ifaddr *ifa = ptr;
3522 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3523 struct bonding *bond, *bond_next;
3524 struct vlan_entry *vlan, *vlan_next;
3525
3526 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3527 if (bond->dev == event_dev) {
3528 switch (event) {
3529 case NETDEV_UP:
3530 bond->master_ip = ifa->ifa_local;
3531 return NOTIFY_OK;
3532 case NETDEV_DOWN:
3533 bond->master_ip = bond_glean_dev_ip(bond->dev);
3534 return NOTIFY_OK;
3535 default:
3536 return NOTIFY_DONE;
3537 }
3538 }
3539
3540 if (list_empty(&bond->vlan_list))
3541 continue;
3542
3543 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3544 vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003545 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003546 if (vlan_dev == event_dev) {
3547 switch (event) {
3548 case NETDEV_UP:
3549 vlan->vlan_ip = ifa->ifa_local;
3550 return NOTIFY_OK;
3551 case NETDEV_DOWN:
3552 vlan->vlan_ip =
3553 bond_glean_dev_ip(vlan_dev);
3554 return NOTIFY_OK;
3555 default:
3556 return NOTIFY_DONE;
3557 }
3558 }
3559 }
3560 }
3561 return NOTIFY_DONE;
3562}
3563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564static struct notifier_block bond_netdev_notifier = {
3565 .notifier_call = bond_netdev_event,
3566};
3567
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003568static struct notifier_block bond_inetaddr_notifier = {
3569 .notifier_call = bond_inetaddr_event,
3570};
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572/*-------------------------- Packet type handling ---------------------------*/
3573
3574/* register to receive lacpdus on a bond */
3575static void bond_register_lacpdu(struct bonding *bond)
3576{
3577 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3578
3579 /* initialize packet type */
3580 pk_type->type = PKT_TYPE_LACPDU;
3581 pk_type->dev = bond->dev;
3582 pk_type->func = bond_3ad_lacpdu_recv;
3583
3584 dev_add_pack(pk_type);
3585}
3586
3587/* unregister to receive lacpdus on a bond */
3588static void bond_unregister_lacpdu(struct bonding *bond)
3589{
3590 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3591}
3592
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003593void bond_register_arp(struct bonding *bond)
3594{
3595 struct packet_type *pt = &bond->arp_mon_pt;
3596
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003597 if (pt->type)
3598 return;
3599
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003600 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003601 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003602 pt->func = bond_arp_rcv;
3603 dev_add_pack(pt);
3604}
3605
3606void bond_unregister_arp(struct bonding *bond)
3607{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003608 struct packet_type *pt = &bond->arp_mon_pt;
3609
3610 dev_remove_pack(pt);
3611 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003612}
3613
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003614/*---------------------------- Hashing Policies -----------------------------*/
3615
3616/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003617 * Hash for the output device based upon layer 2 and layer 3 data. If
3618 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3619 */
3620static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3621 struct net_device *bond_dev, int count)
3622{
3623 struct ethhdr *data = (struct ethhdr *)skb->data;
3624 struct iphdr *iph = ip_hdr(skb);
3625
3626 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3627 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3628 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3629 }
3630
3631 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3632}
3633
3634/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003635 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003636 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3637 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3638 */
3639static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3640 struct net_device *bond_dev, int count)
3641{
3642 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003643 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003644 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003645 int layer4_xor = 0;
3646
3647 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3648 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3649 (iph->protocol == IPPROTO_TCP ||
3650 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003651 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003652 }
3653 return (layer4_xor ^
3654 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3655
3656 }
3657
3658 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3659}
3660
3661/*
3662 * Hash for the output device based upon layer 2 data
3663 */
3664static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3665 struct net_device *bond_dev, int count)
3666{
3667 struct ethhdr *data = (struct ethhdr *)skb->data;
3668
3669 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3670}
3671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672/*-------------------------- Device entry points ----------------------------*/
3673
3674static int bond_open(struct net_device *bond_dev)
3675{
3676 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 bond->kill_timers = 0;
3679
3680 if ((bond->params.mode == BOND_MODE_TLB) ||
3681 (bond->params.mode == BOND_MODE_ALB)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 /* bond_alb_initialize must be called before the timer
3683 * is started.
3684 */
3685 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3686 /* something went wrong - fail the open operation */
3687 return -1;
3688 }
3689
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003690 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3691 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
3693
3694 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003695 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3696 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
3698
3699 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003700 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3701 INIT_DELAYED_WORK(&bond->arp_work,
3702 bond_activebackup_arp_mon);
3703 else
3704 INIT_DELAYED_WORK(&bond->arp_work,
3705 bond_loadbalance_arp_mon);
3706
3707 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003708 if (bond->params.arp_validate)
3709 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
3711
3712 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003713 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003714 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 /* register to receive LACPDUs */
3716 bond_register_lacpdu(bond);
3717 }
3718
3719 return 0;
3720}
3721
3722static int bond_close(struct net_device *bond_dev)
3723{
3724 struct bonding *bond = bond_dev->priv;
3725
3726 if (bond->params.mode == BOND_MODE_8023AD) {
3727 /* Unregister the receive of LACPDUs */
3728 bond_unregister_lacpdu(bond);
3729 }
3730
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003731 if (bond->params.arp_validate)
3732 bond_unregister_arp(bond);
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 write_lock_bh(&bond->lock);
3735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
3737 /* signal timers not to re-arm */
3738 bond->kill_timers = 1;
3739
3740 write_unlock_bh(&bond->lock);
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003743 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745
3746 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003747 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
3749
3750 switch (bond->params.mode) {
3751 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003752 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 break;
3754 case BOND_MODE_TLB:
3755 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003756 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 break;
3758 default:
3759 break;
3760 }
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 if ((bond->params.mode == BOND_MODE_TLB) ||
3764 (bond->params.mode == BOND_MODE_ALB)) {
3765 /* Must be called only after all
3766 * slaves have been released
3767 */
3768 bond_alb_deinitialize(bond);
3769 }
3770
3771 return 0;
3772}
3773
3774static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3775{
3776 struct bonding *bond = bond_dev->priv;
3777 struct net_device_stats *stats = &(bond->stats), *sstats;
3778 struct slave *slave;
3779 int i;
3780
3781 memset(stats, 0, sizeof(struct net_device_stats));
3782
3783 read_lock_bh(&bond->lock);
3784
3785 bond_for_each_slave(bond, slave, i) {
Rusty Russellc45d2862007-03-28 14:29:08 -07003786 sstats = slave->dev->get_stats(slave->dev);
Rusty Russell5a1b5892007-04-28 21:04:03 -07003787 stats->rx_packets += sstats->rx_packets;
3788 stats->rx_bytes += sstats->rx_bytes;
3789 stats->rx_errors += sstats->rx_errors;
3790 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Rusty Russell5a1b5892007-04-28 21:04:03 -07003792 stats->tx_packets += sstats->tx_packets;
3793 stats->tx_bytes += sstats->tx_bytes;
3794 stats->tx_errors += sstats->tx_errors;
3795 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Rusty Russell5a1b5892007-04-28 21:04:03 -07003797 stats->multicast += sstats->multicast;
3798 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Rusty Russell5a1b5892007-04-28 21:04:03 -07003800 stats->rx_length_errors += sstats->rx_length_errors;
3801 stats->rx_over_errors += sstats->rx_over_errors;
3802 stats->rx_crc_errors += sstats->rx_crc_errors;
3803 stats->rx_frame_errors += sstats->rx_frame_errors;
3804 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3805 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Rusty Russell5a1b5892007-04-28 21:04:03 -07003807 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3808 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3809 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3810 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3811 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 }
3813
3814 read_unlock_bh(&bond->lock);
3815
3816 return stats;
3817}
3818
3819static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3820{
3821 struct net_device *slave_dev = NULL;
3822 struct ifbond k_binfo;
3823 struct ifbond __user *u_binfo = NULL;
3824 struct ifslave k_sinfo;
3825 struct ifslave __user *u_sinfo = NULL;
3826 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 int res = 0;
3828
3829 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3830 bond_dev->name, cmd);
3831
3832 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 case SIOCGMIIPHY:
3834 mii = if_mii(ifr);
3835 if (!mii) {
3836 return -EINVAL;
3837 }
3838 mii->phy_id = 0;
3839 /* Fall Through */
3840 case SIOCGMIIREG:
3841 /*
3842 * We do this again just in case we were called by SIOCGMIIREG
3843 * instead of SIOCGMIIPHY.
3844 */
3845 mii = if_mii(ifr);
3846 if (!mii) {
3847 return -EINVAL;
3848 }
3849
3850 if (mii->reg_num == 1) {
3851 struct bonding *bond = bond_dev->priv;
3852 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003853 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 read_lock(&bond->curr_slave_lock);
Andy Gospodarek4e140072006-12-04 15:04:54 -08003855 if (netif_carrier_ok(bond->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 mii->val_out = BMSR_LSTATUS;
3857 }
3858 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003859 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 }
3861
3862 return 0;
3863 case BOND_INFO_QUERY_OLD:
3864 case SIOCBONDINFOQUERY:
3865 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3866
3867 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3868 return -EFAULT;
3869 }
3870
3871 res = bond_info_query(bond_dev, &k_binfo);
3872 if (res == 0) {
3873 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3874 return -EFAULT;
3875 }
3876 }
3877
3878 return res;
3879 case BOND_SLAVE_INFO_QUERY_OLD:
3880 case SIOCBONDSLAVEINFOQUERY:
3881 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3882
3883 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3884 return -EFAULT;
3885 }
3886
3887 res = bond_slave_info_query(bond_dev, &k_sinfo);
3888 if (res == 0) {
3889 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3890 return -EFAULT;
3891 }
3892 }
3893
3894 return res;
3895 default:
3896 /* Go on */
3897 break;
3898 }
3899
3900 if (!capable(CAP_NET_ADMIN)) {
3901 return -EPERM;
3902 }
3903
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003904 down_write(&(bonding_rwsem));
Eric W. Biederman881d9662007-09-17 11:56:21 -07003905 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
3907 dprintk("slave_dev=%p: \n", slave_dev);
3908
3909 if (!slave_dev) {
3910 res = -ENODEV;
3911 } else {
3912 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3913 switch (cmd) {
3914 case BOND_ENSLAVE_OLD:
3915 case SIOCBONDENSLAVE:
3916 res = bond_enslave(bond_dev, slave_dev);
3917 break;
3918 case BOND_RELEASE_OLD:
3919 case SIOCBONDRELEASE:
3920 res = bond_release(bond_dev, slave_dev);
3921 break;
3922 case BOND_SETHWADDR_OLD:
3923 case SIOCBONDSETHWADDR:
3924 res = bond_sethwaddr(bond_dev, slave_dev);
3925 break;
3926 case BOND_CHANGE_ACTIVE_OLD:
3927 case SIOCBONDCHANGEACTIVE:
3928 res = bond_ioctl_change_active(bond_dev, slave_dev);
3929 break;
3930 default:
3931 res = -EOPNOTSUPP;
3932 }
3933
3934 dev_put(slave_dev);
3935 }
3936
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003937 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return res;
3939}
3940
3941static void bond_set_multicast_list(struct net_device *bond_dev)
3942{
3943 struct bonding *bond = bond_dev->priv;
3944 struct dev_mc_list *dmi;
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 /*
3947 * Do promisc before checking multicast_mode
3948 */
3949 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3950 bond_set_promiscuity(bond, 1);
3951 }
3952
3953 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3954 bond_set_promiscuity(bond, -1);
3955 }
3956
3957 /* set allmulti flag to slaves */
3958 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3959 bond_set_allmulti(bond, 1);
3960 }
3961
3962 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3963 bond_set_allmulti(bond, -1);
3964 }
3965
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003966 read_lock(&bond->lock);
3967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 bond->flags = bond_dev->flags;
3969
3970 /* looking for addresses to add to slaves' mc list */
3971 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3972 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3973 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3974 }
3975 }
3976
3977 /* looking for addresses to delete from slaves' list */
3978 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3979 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3980 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3981 }
3982 }
3983
3984 /* save master's multicast list */
3985 bond_mc_list_destroy(bond);
3986 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3987
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003988 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
3991/*
3992 * Change the MTU of all of a master's slaves to match the master
3993 */
3994static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3995{
3996 struct bonding *bond = bond_dev->priv;
3997 struct slave *slave, *stop_at;
3998 int res = 0;
3999 int i;
4000
4001 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
4002 (bond_dev ? bond_dev->name : "None"), new_mtu);
4003
4004 /* Can't hold bond->lock with bh disabled here since
4005 * some base drivers panic. On the other hand we can't
4006 * hold bond->lock without bh disabled because we'll
4007 * deadlock. The only solution is to rely on the fact
4008 * that we're under rtnl_lock here, and the slaves
4009 * list won't change. This doesn't solve the problem
4010 * of setting the slave's MTU while it is
4011 * transmitting, but the assumption is that the base
4012 * driver can handle that.
4013 *
4014 * TODO: figure out a way to safely iterate the slaves
4015 * list, but without holding a lock around the actual
4016 * call to the base driver.
4017 */
4018
4019 bond_for_each_slave(bond, slave, i) {
4020 dprintk("s %p s->p %p c_m %p\n", slave,
4021 slave->prev, slave->dev->change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 res = dev_set_mtu(slave->dev, new_mtu);
4024
4025 if (res) {
4026 /* If we failed to set the slave's mtu to the new value
4027 * we must abort the operation even in ACTIVE_BACKUP
4028 * mode, because if we allow the backup slaves to have
4029 * different mtu values than the active slave we'll
4030 * need to change their mtu when doing a failover. That
4031 * means changing their mtu from timer context, which
4032 * is probably not a good idea.
4033 */
4034 dprintk("err %d %s\n", res, slave->dev->name);
4035 goto unwind;
4036 }
4037 }
4038
4039 bond_dev->mtu = new_mtu;
4040
4041 return 0;
4042
4043unwind:
4044 /* unwind from head to the slave that failed */
4045 stop_at = slave;
4046 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4047 int tmp_res;
4048
4049 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4050 if (tmp_res) {
4051 dprintk("unwind err %d dev %s\n", tmp_res,
4052 slave->dev->name);
4053 }
4054 }
4055
4056 return res;
4057}
4058
4059/*
4060 * Change HW address
4061 *
4062 * Note that many devices must be down to change the HW address, and
4063 * downing the master releases all slaves. We can make bonds full of
4064 * bonding devices to test this, however.
4065 */
4066static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4067{
4068 struct bonding *bond = bond_dev->priv;
4069 struct sockaddr *sa = addr, tmp_sa;
4070 struct slave *slave, *stop_at;
4071 int res = 0;
4072 int i;
4073
4074 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4075
Jay Vosburghdd957c52007-10-09 19:57:24 -07004076 /*
4077 * If fail_over_mac is enabled, do nothing and return success.
4078 * Returning an error causes ifenslave to fail.
4079 */
4080 if (bond->params.fail_over_mac)
4081 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (!is_valid_ether_addr(sa->sa_data)) {
4084 return -EADDRNOTAVAIL;
4085 }
4086
4087 /* Can't hold bond->lock with bh disabled here since
4088 * some base drivers panic. On the other hand we can't
4089 * hold bond->lock without bh disabled because we'll
4090 * deadlock. The only solution is to rely on the fact
4091 * that we're under rtnl_lock here, and the slaves
4092 * list won't change. This doesn't solve the problem
4093 * of setting the slave's hw address while it is
4094 * transmitting, but the assumption is that the base
4095 * driver can handle that.
4096 *
4097 * TODO: figure out a way to safely iterate the slaves
4098 * list, but without holding a lock around the actual
4099 * call to the base driver.
4100 */
4101
4102 bond_for_each_slave(bond, slave, i) {
4103 dprintk("slave %p %s\n", slave, slave->dev->name);
4104
4105 if (slave->dev->set_mac_address == NULL) {
4106 res = -EOPNOTSUPP;
4107 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4108 goto unwind;
4109 }
4110
4111 res = dev_set_mac_address(slave->dev, addr);
4112 if (res) {
4113 /* TODO: consider downing the slave
4114 * and retry ?
4115 * User should expect communications
4116 * breakage anyway until ARP finish
4117 * updating, so...
4118 */
4119 dprintk("err %d %s\n", res, slave->dev->name);
4120 goto unwind;
4121 }
4122 }
4123
4124 /* success */
4125 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4126 return 0;
4127
4128unwind:
4129 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4130 tmp_sa.sa_family = bond_dev->type;
4131
4132 /* unwind from head to the slave that failed */
4133 stop_at = slave;
4134 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4135 int tmp_res;
4136
4137 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4138 if (tmp_res) {
4139 dprintk("unwind err %d dev %s\n", tmp_res,
4140 slave->dev->name);
4141 }
4142 }
4143
4144 return res;
4145}
4146
4147static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4148{
4149 struct bonding *bond = bond_dev->priv;
4150 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004151 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 read_lock(&bond->lock);
4154
4155 if (!BOND_IS_OK(bond)) {
4156 goto out;
4157 }
4158
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004159 /*
4160 * Concurrent TX may collide on rr_tx_counter; we accept that
4161 * as being rare enough not to justify using an atomic op here
4162 */
4163 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004165 bond_for_each_slave(bond, slave, i) {
4166 slave_no--;
4167 if (slave_no < 0) {
4168 break;
4169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
4171
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004172 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 bond_for_each_slave_from(bond, slave, i, start_at) {
4174 if (IS_UP(slave->dev) &&
4175 (slave->link == BOND_LINK_UP) &&
4176 (slave->state == BOND_STATE_ACTIVE)) {
4177 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 break;
4179 }
4180 }
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182out:
4183 if (res) {
4184 /* no suitable interface, frame not sent */
4185 dev_kfree_skb(skb);
4186 }
4187 read_unlock(&bond->lock);
4188 return 0;
4189}
4190
John W. Linville075897c2005-09-28 17:50:53 -04004191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192/*
4193 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4194 * the bond has a usable interface.
4195 */
4196static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4197{
4198 struct bonding *bond = bond_dev->priv;
4199 int res = 1;
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 read_lock(&bond->lock);
4202 read_lock(&bond->curr_slave_lock);
4203
4204 if (!BOND_IS_OK(bond)) {
4205 goto out;
4206 }
4207
John W. Linville075897c2005-09-28 17:50:53 -04004208 if (!bond->curr_active_slave)
4209 goto out;
4210
John W. Linville075897c2005-09-28 17:50:53 -04004211 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213out:
4214 if (res) {
4215 /* no suitable interface, frame not sent */
4216 dev_kfree_skb(skb);
4217 }
4218 read_unlock(&bond->curr_slave_lock);
4219 read_unlock(&bond->lock);
4220 return 0;
4221}
4222
4223/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004224 * In bond_xmit_xor() , we determine the output device by using a pre-
4225 * determined xmit_hash_policy(), If the selected device is not enabled,
4226 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 */
4228static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4229{
4230 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 struct slave *slave, *start_at;
4232 int slave_no;
4233 int i;
4234 int res = 1;
4235
4236 read_lock(&bond->lock);
4237
4238 if (!BOND_IS_OK(bond)) {
4239 goto out;
4240 }
4241
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004242 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 bond_for_each_slave(bond, slave, i) {
4245 slave_no--;
4246 if (slave_no < 0) {
4247 break;
4248 }
4249 }
4250
4251 start_at = slave;
4252
4253 bond_for_each_slave_from(bond, slave, i, start_at) {
4254 if (IS_UP(slave->dev) &&
4255 (slave->link == BOND_LINK_UP) &&
4256 (slave->state == BOND_STATE_ACTIVE)) {
4257 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4258 break;
4259 }
4260 }
4261
4262out:
4263 if (res) {
4264 /* no suitable interface, frame not sent */
4265 dev_kfree_skb(skb);
4266 }
4267 read_unlock(&bond->lock);
4268 return 0;
4269}
4270
4271/*
4272 * in broadcast mode, we send everything to all usable interfaces.
4273 */
4274static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4275{
4276 struct bonding *bond = bond_dev->priv;
4277 struct slave *slave, *start_at;
4278 struct net_device *tx_dev = NULL;
4279 int i;
4280 int res = 1;
4281
4282 read_lock(&bond->lock);
4283
4284 if (!BOND_IS_OK(bond)) {
4285 goto out;
4286 }
4287
4288 read_lock(&bond->curr_slave_lock);
4289 start_at = bond->curr_active_slave;
4290 read_unlock(&bond->curr_slave_lock);
4291
4292 if (!start_at) {
4293 goto out;
4294 }
4295
4296 bond_for_each_slave_from(bond, slave, i, start_at) {
4297 if (IS_UP(slave->dev) &&
4298 (slave->link == BOND_LINK_UP) &&
4299 (slave->state == BOND_STATE_ACTIVE)) {
4300 if (tx_dev) {
4301 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4302 if (!skb2) {
4303 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004304 ": %s: Error: bond_xmit_broadcast(): "
4305 "skb_clone() failed\n",
4306 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 continue;
4308 }
4309
4310 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4311 if (res) {
4312 dev_kfree_skb(skb2);
4313 continue;
4314 }
4315 }
4316 tx_dev = slave->dev;
4317 }
4318 }
4319
4320 if (tx_dev) {
4321 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4322 }
4323
4324out:
4325 if (res) {
4326 /* no suitable interface, frame not sent */
4327 dev_kfree_skb(skb);
4328 }
4329 /* frame sent to all suitable interfaces */
4330 read_unlock(&bond->lock);
4331 return 0;
4332}
4333
4334/*------------------------- Device initialization ---------------------------*/
4335
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004336static void bond_set_xmit_hash_policy(struct bonding *bond)
4337{
4338 switch (bond->params.xmit_policy) {
4339 case BOND_XMIT_POLICY_LAYER23:
4340 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4341 break;
4342 case BOND_XMIT_POLICY_LAYER34:
4343 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4344 break;
4345 case BOND_XMIT_POLICY_LAYER2:
4346 default:
4347 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4348 break;
4349 }
4350}
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352/*
4353 * set bond mode specific net device operations
4354 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004355void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004357 struct net_device *bond_dev = bond->dev;
4358
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 switch (mode) {
4360 case BOND_MODE_ROUNDROBIN:
4361 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4362 break;
4363 case BOND_MODE_ACTIVEBACKUP:
4364 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4365 break;
4366 case BOND_MODE_XOR:
4367 bond_dev->hard_start_xmit = bond_xmit_xor;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004368 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 break;
4370 case BOND_MODE_BROADCAST:
4371 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4372 break;
4373 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004374 bond_set_master_3ad_flags(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004376 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004379 bond_set_master_alb_flags(bond);
4380 /* FALLTHRU */
4381 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 bond_dev->hard_start_xmit = bond_alb_xmit;
4383 bond_dev->set_mac_address = bond_alb_set_mac_address;
4384 break;
4385 default:
4386 /* Should never happen, mode already checked */
4387 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004388 ": %s: Error: Unknown bonding mode %d\n",
4389 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 mode);
4391 break;
4392 }
4393}
4394
Jay Vosburgh217df672005-09-26 16:11:50 -07004395static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4396 struct ethtool_drvinfo *drvinfo)
4397{
4398 strncpy(drvinfo->driver, DRV_NAME, 32);
4399 strncpy(drvinfo->version, DRV_VERSION, 32);
4400 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4401}
4402
Jeff Garzik7282d492006-09-13 14:30:00 -04004403static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004404 .get_drvinfo = bond_ethtool_get_drvinfo,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004405};
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407/*
4408 * Does not allocate but creates a /proc entry.
4409 * Allowed to fail.
4410 */
Mitch Williams3c535952005-11-09 10:36:11 -08004411static int bond_init(struct net_device *bond_dev, struct bond_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
4413 struct bonding *bond = bond_dev->priv;
4414
4415 dprintk("Begin bond_init for %s\n", bond_dev->name);
4416
4417 /* initialize rwlocks */
4418 rwlock_init(&bond->lock);
4419 rwlock_init(&bond->curr_slave_lock);
4420
4421 bond->params = *params; /* copy params struct */
4422
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004423 bond->wq = create_singlethread_workqueue(bond_dev->name);
4424 if (!bond->wq)
4425 return -ENOMEM;
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 /* Initialize pointers */
4428 bond->first_slave = NULL;
4429 bond->curr_active_slave = NULL;
4430 bond->current_arp_slave = NULL;
4431 bond->primary_slave = NULL;
4432 bond->dev = bond_dev;
Moni Shoua1053f622007-10-09 19:43:42 -07004433 bond->send_grat_arp = 0;
Moni Shouad90a1622007-10-09 19:43:43 -07004434 bond->setup_by_slave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 INIT_LIST_HEAD(&bond->vlan_list);
4436
4437 /* Initialize the device entry points */
4438 bond_dev->open = bond_open;
4439 bond_dev->stop = bond_close;
4440 bond_dev->get_stats = bond_get_stats;
4441 bond_dev->do_ioctl = bond_do_ioctl;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004442 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 bond_dev->set_multicast_list = bond_set_multicast_list;
4444 bond_dev->change_mtu = bond_change_mtu;
4445 bond_dev->set_mac_address = bond_set_mac_address;
Jay Vosburgh3a1521b2007-11-06 13:33:29 -08004446 bond_dev->validate_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004448 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
4450 bond_dev->destructor = free_netdev;
4451
4452 /* Initialize the device options */
4453 bond_dev->tx_queue_len = 0;
4454 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004455 bond_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 /* At first, we block adding VLANs. That's the only way to
4458 * prevent problems that occur when adding VLANs over an
4459 * empty bond. The block will be removed once non-challenged
4460 * slaves are enslaved.
4461 */
4462 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4463
Herbert Xu932ff272006-06-09 12:20:56 -07004464 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 * transmitting */
4466 bond_dev->features |= NETIF_F_LLTX;
4467
4468 /* By default, we declare the bond to be fully
4469 * VLAN hardware accelerated capable. Special
4470 * care is taken in the various xmit functions
4471 * when there are slaves that are not hw accel
4472 * capable
4473 */
4474 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4475 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4476 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4477 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4478 NETIF_F_HW_VLAN_RX |
4479 NETIF_F_HW_VLAN_FILTER);
4480
4481#ifdef CONFIG_PROC_FS
4482 bond_create_proc_entry(bond);
4483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 list_add_tail(&bond->bond_list, &bond_dev_list);
4485
4486 return 0;
4487}
4488
4489/* De-initialize device specific data.
4490 * Caller must hold rtnl_lock.
4491 */
Adrian Bunkc50b85d2007-10-24 18:23:17 +02004492static void bond_deinit(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
4494 struct bonding *bond = bond_dev->priv;
4495
4496 list_del(&bond->bond_list);
4497
4498#ifdef CONFIG_PROC_FS
4499 bond_remove_proc_entry(bond);
4500#endif
4501}
4502
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004503static void bond_work_cancel_all(struct bonding *bond)
4504{
4505 write_lock_bh(&bond->lock);
4506 bond->kill_timers = 1;
4507 write_unlock_bh(&bond->lock);
4508
4509 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4510 cancel_delayed_work(&bond->mii_work);
4511
4512 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4513 cancel_delayed_work(&bond->arp_work);
4514
4515 if (bond->params.mode == BOND_MODE_ALB &&
4516 delayed_work_pending(&bond->alb_work))
4517 cancel_delayed_work(&bond->alb_work);
4518
4519 if (bond->params.mode == BOND_MODE_8023AD &&
4520 delayed_work_pending(&bond->ad_work))
4521 cancel_delayed_work(&bond->ad_work);
4522}
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524/* Unregister and free all bond devices.
4525 * Caller must hold rtnl_lock.
4526 */
4527static void bond_free_all(void)
4528{
4529 struct bonding *bond, *nxt;
4530
4531 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4532 struct net_device *bond_dev = bond->dev;
4533
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004534 bond_work_cancel_all(bond);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004535 netif_tx_lock_bh(bond_dev);
jamal702987052006-09-22 21:54:37 -07004536 bond_mc_list_destroy(bond);
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004537 netif_tx_unlock_bh(bond_dev);
jamal702987052006-09-22 21:54:37 -07004538 /* Release the bonded slaves */
4539 bond_release_all(bond_dev);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004540 bond_deinit(bond_dev);
Jay Vosburgh8cbdeec2007-11-13 21:16:29 -08004541 unregister_netdevice(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 }
4543
4544#ifdef CONFIG_PROC_FS
4545 bond_destroy_proc_dir();
4546#endif
4547}
4548
4549/*------------------------- Module initialization ---------------------------*/
4550
4551/*
4552 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004553 * number of the mode or its string name. A bit complicated because
4554 * some mode names are substrings of other names, and calls from sysfs
4555 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 */
Jay Vosburghece95f72008-01-17 16:25:01 -08004557int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
Jay Vosburghece95f72008-01-17 16:25:01 -08004559 int mode = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004560 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004561
Jay Vosburgha42e5342008-01-29 18:07:43 -08004562 for (p = (char *)buf; *p; p++)
4563 if (!(isdigit(*p) || isspace(*p)))
4564 break;
4565
4566 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004567 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004568 else
4569 rv = sscanf(buf, "%d", &mode);
4570
4571 if (!rv)
4572 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
4574 for (i = 0; tbl[i].modename; i++) {
Jay Vosburghece95f72008-01-17 16:25:01 -08004575 if (mode == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004577 if (strcmp(modestr, tbl[i].modename) == 0)
4578 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 }
4580
4581 return -1;
4582}
4583
4584static int bond_check_params(struct bond_params *params)
4585{
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004586 int arp_validate_value;
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 /*
4589 * Convert string parameters.
4590 */
4591 if (mode) {
4592 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4593 if (bond_mode == -1) {
4594 printk(KERN_ERR DRV_NAME
4595 ": Error: Invalid bonding mode \"%s\"\n",
4596 mode == NULL ? "NULL" : mode);
4597 return -EINVAL;
4598 }
4599 }
4600
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004601 if (xmit_hash_policy) {
4602 if ((bond_mode != BOND_MODE_XOR) &&
4603 (bond_mode != BOND_MODE_8023AD)) {
4604 printk(KERN_INFO DRV_NAME
4605 ": xor_mode param is irrelevant in mode %s\n",
4606 bond_mode_name(bond_mode));
4607 } else {
4608 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4609 xmit_hashtype_tbl);
4610 if (xmit_hashtype == -1) {
4611 printk(KERN_ERR DRV_NAME
4612 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4613 xmit_hash_policy == NULL ? "NULL" :
4614 xmit_hash_policy);
4615 return -EINVAL;
4616 }
4617 }
4618 }
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 if (lacp_rate) {
4621 if (bond_mode != BOND_MODE_8023AD) {
4622 printk(KERN_INFO DRV_NAME
4623 ": lacp_rate param is irrelevant in mode %s\n",
4624 bond_mode_name(bond_mode));
4625 } else {
4626 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4627 if (lacp_fast == -1) {
4628 printk(KERN_ERR DRV_NAME
4629 ": Error: Invalid lacp rate \"%s\"\n",
4630 lacp_rate == NULL ? "NULL" : lacp_rate);
4631 return -EINVAL;
4632 }
4633 }
4634 }
4635
4636 if (max_bonds < 1 || max_bonds > INT_MAX) {
4637 printk(KERN_WARNING DRV_NAME
4638 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004639 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4641 max_bonds = BOND_DEFAULT_MAX_BONDS;
4642 }
4643
4644 if (miimon < 0) {
4645 printk(KERN_WARNING DRV_NAME
4646 ": Warning: miimon module parameter (%d), "
4647 "not in range 0-%d, so it was reset to %d\n",
4648 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4649 miimon = BOND_LINK_MON_INTERV;
4650 }
4651
4652 if (updelay < 0) {
4653 printk(KERN_WARNING DRV_NAME
4654 ": Warning: updelay module parameter (%d), "
4655 "not in range 0-%d, so it was reset to 0\n",
4656 updelay, INT_MAX);
4657 updelay = 0;
4658 }
4659
4660 if (downdelay < 0) {
4661 printk(KERN_WARNING DRV_NAME
4662 ": Warning: downdelay module parameter (%d), "
4663 "not in range 0-%d, so it was reset to 0\n",
4664 downdelay, INT_MAX);
4665 downdelay = 0;
4666 }
4667
4668 if ((use_carrier != 0) && (use_carrier != 1)) {
4669 printk(KERN_WARNING DRV_NAME
4670 ": Warning: use_carrier module parameter (%d), "
4671 "not of valid value (0/1), so it was set to 1\n",
4672 use_carrier);
4673 use_carrier = 1;
4674 }
4675
4676 /* reset values for 802.3ad */
4677 if (bond_mode == BOND_MODE_8023AD) {
4678 if (!miimon) {
4679 printk(KERN_WARNING DRV_NAME
4680 ": Warning: miimon must be specified, "
4681 "otherwise bonding will not detect link "
4682 "failure, speed and duplex which are "
4683 "essential for 802.3ad operation\n");
4684 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4685 miimon = 100;
4686 }
4687 }
4688
4689 /* reset values for TLB/ALB */
4690 if ((bond_mode == BOND_MODE_TLB) ||
4691 (bond_mode == BOND_MODE_ALB)) {
4692 if (!miimon) {
4693 printk(KERN_WARNING DRV_NAME
4694 ": Warning: miimon must be specified, "
4695 "otherwise bonding will not detect link "
4696 "failure and link speed which are essential "
4697 "for TLB/ALB load balancing\n");
4698 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4699 miimon = 100;
4700 }
4701 }
4702
4703 if (bond_mode == BOND_MODE_ALB) {
4704 printk(KERN_NOTICE DRV_NAME
4705 ": In ALB mode you might experience client "
4706 "disconnections upon reconnection of a link if the "
4707 "bonding module updelay parameter (%d msec) is "
4708 "incompatible with the forwarding delay time of the "
4709 "switch\n",
4710 updelay);
4711 }
4712
4713 if (!miimon) {
4714 if (updelay || downdelay) {
4715 /* just warn the user the up/down delay will have
4716 * no effect since miimon is zero...
4717 */
4718 printk(KERN_WARNING DRV_NAME
4719 ": Warning: miimon module parameter not set "
4720 "and updelay (%d) or downdelay (%d) module "
4721 "parameter is set; updelay and downdelay have "
4722 "no effect unless miimon is set\n",
4723 updelay, downdelay);
4724 }
4725 } else {
4726 /* don't allow arp monitoring */
4727 if (arp_interval) {
4728 printk(KERN_WARNING DRV_NAME
4729 ": Warning: miimon (%d) and arp_interval (%d) "
4730 "can't be used simultaneously, disabling ARP "
4731 "monitoring\n",
4732 miimon, arp_interval);
4733 arp_interval = 0;
4734 }
4735
4736 if ((updelay % miimon) != 0) {
4737 printk(KERN_WARNING DRV_NAME
4738 ": Warning: updelay (%d) is not a multiple "
4739 "of miimon (%d), updelay rounded to %d ms\n",
4740 updelay, miimon, (updelay / miimon) * miimon);
4741 }
4742
4743 updelay /= miimon;
4744
4745 if ((downdelay % miimon) != 0) {
4746 printk(KERN_WARNING DRV_NAME
4747 ": Warning: downdelay (%d) is not a multiple "
4748 "of miimon (%d), downdelay rounded to %d ms\n",
4749 downdelay, miimon,
4750 (downdelay / miimon) * miimon);
4751 }
4752
4753 downdelay /= miimon;
4754 }
4755
4756 if (arp_interval < 0) {
4757 printk(KERN_WARNING DRV_NAME
4758 ": Warning: arp_interval module parameter (%d) "
4759 ", not in range 0-%d, so it was reset to %d\n",
4760 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4761 arp_interval = BOND_LINK_ARP_INTERV;
4762 }
4763
4764 for (arp_ip_count = 0;
4765 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4766 arp_ip_count++) {
4767 /* not complete check, but should be good enough to
4768 catch mistakes */
4769 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4770 printk(KERN_WARNING DRV_NAME
4771 ": Warning: bad arp_ip_target module parameter "
4772 "(%s), ARP monitoring will not be performed\n",
4773 arp_ip_target[arp_ip_count]);
4774 arp_interval = 0;
4775 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004776 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 arp_target[arp_ip_count] = ip;
4778 }
4779 }
4780
4781 if (arp_interval && !arp_ip_count) {
4782 /* don't allow arping if no arp_ip_target given... */
4783 printk(KERN_WARNING DRV_NAME
4784 ": Warning: arp_interval module parameter (%d) "
4785 "specified without providing an arp_ip_target "
4786 "parameter, arp_interval was reset to 0\n",
4787 arp_interval);
4788 arp_interval = 0;
4789 }
4790
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004791 if (arp_validate) {
4792 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4793 printk(KERN_ERR DRV_NAME
4794 ": arp_validate only supported in active-backup mode\n");
4795 return -EINVAL;
4796 }
4797 if (!arp_interval) {
4798 printk(KERN_ERR DRV_NAME
4799 ": arp_validate requires arp_interval\n");
4800 return -EINVAL;
4801 }
4802
4803 arp_validate_value = bond_parse_parm(arp_validate,
4804 arp_validate_tbl);
4805 if (arp_validate_value == -1) {
4806 printk(KERN_ERR DRV_NAME
4807 ": Error: invalid arp_validate \"%s\"\n",
4808 arp_validate == NULL ? "NULL" : arp_validate);
4809 return -EINVAL;
4810 }
4811 } else
4812 arp_validate_value = 0;
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 if (miimon) {
4815 printk(KERN_INFO DRV_NAME
4816 ": MII link monitoring set to %d ms\n",
4817 miimon);
4818 } else if (arp_interval) {
4819 int i;
4820
4821 printk(KERN_INFO DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004822 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4823 arp_interval,
4824 arp_validate_tbl[arp_validate_value].modename,
4825 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 for (i = 0; i < arp_ip_count; i++)
4828 printk (" %s", arp_ip_target[i]);
4829
4830 printk("\n");
4831
4832 } else {
4833 /* miimon and arp_interval not set, we need one so things
4834 * work as expected, see bonding.txt for details
4835 */
4836 printk(KERN_WARNING DRV_NAME
4837 ": Warning: either miimon or arp_interval and "
4838 "arp_ip_target module parameters must be specified, "
4839 "otherwise bonding will not detect link failures! see "
4840 "bonding.txt for details.\n");
4841 }
4842
4843 if (primary && !USES_PRIMARY(bond_mode)) {
4844 /* currently, using a primary only makes sense
4845 * in active backup, TLB or ALB modes
4846 */
4847 printk(KERN_WARNING DRV_NAME
4848 ": Warning: %s primary device specified but has no "
4849 "effect in %s mode\n",
4850 primary, bond_mode_name(bond_mode));
4851 primary = NULL;
4852 }
4853
Jay Vosburghdd957c52007-10-09 19:57:24 -07004854 if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
4855 printk(KERN_WARNING DRV_NAME
4856 ": Warning: fail_over_mac only affects "
4857 "active-backup mode.\n");
4858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 /* fill params struct with the proper values */
4860 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004861 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 params->miimon = miimon;
4863 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004864 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 params->updelay = updelay;
4866 params->downdelay = downdelay;
4867 params->use_carrier = use_carrier;
4868 params->lacp_fast = lacp_fast;
4869 params->primary[0] = 0;
Jay Vosburghdd957c52007-10-09 19:57:24 -07004870 params->fail_over_mac = fail_over_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 if (primary) {
4873 strncpy(params->primary, primary, IFNAMSIZ);
4874 params->primary[IFNAMSIZ - 1] = 0;
4875 }
4876
4877 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4878
4879 return 0;
4880}
4881
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004882static struct lock_class_key bonding_netdev_xmit_lock_key;
4883
Mitch Williamsdfe60392005-11-09 10:36:04 -08004884/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004885 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004886 * Caller must NOT hold rtnl_lock; we need to release it here before we
4887 * set up our sysfs entries.
4888 */
4889int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4890{
4891 struct net_device *bond_dev;
Jay Vosburgh027ea042008-01-17 16:25:02 -08004892 struct bonding *bond, *nxt;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004893 int res;
4894
4895 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004896 down_write(&bonding_rwsem);
4897
4898 /* Check to see if the bond already exists. */
4899 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
4900 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
4901 printk(KERN_ERR DRV_NAME
4902 ": cannot add bond %s; it already exists\n",
4903 name);
4904 res = -EPERM;
4905 goto out_rtnl;
4906 }
4907
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004908 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4909 ether_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004910 if (!bond_dev) {
4911 printk(KERN_ERR DRV_NAME
4912 ": %s: eek! can't alloc netdev!\n",
4913 name);
4914 res = -ENOMEM;
4915 goto out_rtnl;
4916 }
4917
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004918 if (!name) {
4919 res = dev_alloc_name(bond_dev, "bond%d");
4920 if (res < 0)
4921 goto out_netdev;
4922 }
4923
Mitch Williamsdfe60392005-11-09 10:36:04 -08004924 /* bond_init() must be called after dev_alloc_name() (for the
4925 * /proc files), but before register_netdevice(), because we
4926 * need to set function pointers.
4927 */
4928
4929 res = bond_init(bond_dev, params);
4930 if (res < 0) {
4931 goto out_netdev;
4932 }
4933
Mitch Williamsdfe60392005-11-09 10:36:04 -08004934 res = register_netdevice(bond_dev);
4935 if (res < 0) {
4936 goto out_bond;
4937 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004938
4939 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4940
Mitch Williamsdfe60392005-11-09 10:36:04 -08004941 if (newbond)
4942 *newbond = bond_dev->priv;
4943
Jay Vosburghff59c452006-03-27 13:27:43 -08004944 netif_carrier_off(bond_dev);
4945
Jay Vosburgh027ea042008-01-17 16:25:02 -08004946 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004947 rtnl_unlock(); /* allows sysfs registration of net device */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004948 res = bond_create_sysfs_entry(bond_dev->priv);
Jay Vosburgh09c89272007-01-19 18:15:38 -08004949 if (res < 0) {
4950 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004951 down_write(&bonding_rwsem);
Jay Vosburgh09c89272007-01-19 18:15:38 -08004952 goto out_bond;
4953 }
4954
4955 return 0;
4956
Mitch Williamsdfe60392005-11-09 10:36:04 -08004957out_bond:
4958 bond_deinit(bond_dev);
4959out_netdev:
4960 free_netdev(bond_dev);
4961out_rtnl:
Jay Vosburgh027ea042008-01-17 16:25:02 -08004962 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004963 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004964 return res;
4965}
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967static int __init bonding_init(void)
4968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 int i;
4970 int res;
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004971 struct bonding *bond, *nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 printk(KERN_INFO "%s", version);
4974
Mitch Williamsdfe60392005-11-09 10:36:04 -08004975 res = bond_check_params(&bonding_defaults);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 if (res) {
Mitch Williamsdfe60392005-11-09 10:36:04 -08004977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980#ifdef CONFIG_PROC_FS
4981 bond_create_proc_dir();
4982#endif
Jay Vosburgh027ea042008-01-17 16:25:02 -08004983
4984 init_rwsem(&bonding_rwsem);
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 for (i = 0; i < max_bonds; i++) {
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004987 res = bond_create(NULL, &bonding_defaults, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004988 if (res)
4989 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 }
4991
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004992 res = bond_create_sysfs();
4993 if (res)
4994 goto err;
4995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004997 register_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Mitch Williamsdfe60392005-11-09 10:36:04 -08004999 goto out;
5000err:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07005001 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
5002 bond_work_cancel_all(bond);
5003 destroy_workqueue(bond->wq);
5004 }
5005
Florin Malita40abc272005-09-18 00:24:12 -07005006 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 bond_free_all();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005008 bond_destroy_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005010out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
5015static void __exit bonding_exit(void)
5016{
5017 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005018 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 rtnl_lock();
5021 bond_free_all();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005022 bond_destroy_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 rtnl_unlock();
5024}
5025
5026module_init(bonding_init);
5027module_exit(bonding_exit);
5028MODULE_LICENSE("GPL");
5029MODULE_VERSION(DRV_VERSION);
5030MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5031MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5032MODULE_SUPPORTED_DEVICE("most ethernet devices");
5033
5034/*
5035 * Local variables:
5036 * c-indent-level: 8
5037 * c-basic-offset: 8
5038 * tab-width: 8
5039 * End:
5040 */
5041