blob: 7be23364a9d6f1682ea6a1023c90ba4728846fc4 [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>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040077#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020078#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "bonding.h"
80#include "bond_3ad.h"
81#include "bond_alb.h"
82
83/*---------------------------- Module parameters ----------------------------*/
84
85/* monitor all links that often (in milliseconds). <=0 disables monitoring */
86#define BOND_LINK_MON_INTERV 0
87#define BOND_LINK_ARP_INTERV 0
88
89static int max_bonds = BOND_DEFAULT_MAX_BONDS;
90static int miimon = BOND_LINK_MON_INTERV;
91static int updelay = 0;
92static int downdelay = 0;
93static int use_carrier = 1;
94static char *mode = NULL;
95static char *primary = NULL;
96static char *lacp_rate = NULL;
Jay Vosburgh169a3e62005-06-26 17:54:11 -040097static char *xmit_hash_policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int arp_interval = BOND_LINK_ARP_INTERV;
99static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700100static char *arp_validate = NULL;
Mitch Williams12479f92005-11-09 10:35:44 -0800101struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103module_param(max_bonds, int, 0);
104MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
105module_param(miimon, int, 0);
106MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
107module_param(updelay, int, 0);
108MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
109module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800110MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
111 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800113MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
114 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800116MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
117 "1 for active-backup, 2 for balance-xor, "
118 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
119 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(primary, charp, 0);
121MODULE_PARM_DESC(primary, "Primary network device to use");
122module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800123MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
124 "(slow/fast)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400125module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800126MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
127 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(arp_interval, int, 0);
129MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
130module_param_array(arp_ip_target, charp, NULL, 0);
131MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700132module_param(arp_validate, charp, 0);
133MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*----------------------------- Global variables ----------------------------*/
136
Arjan van de Venf71e1302006-03-03 21:33:57 -0500137static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
139
Mitch Williams12479f92005-11-09 10:35:44 -0800140LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142#ifdef CONFIG_PROC_FS
143static struct proc_dir_entry *bond_proc_dir = NULL;
144#endif
145
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146extern struct rw_semaphore bonding_rwsem;
Al Virod3bb52b2007-08-22 20:06:58 -0400147static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int arp_ip_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int bond_mode = BOND_MODE_ROUNDROBIN;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400150static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int lacp_fast = 0;
Jay Vosburgh217df672005-09-26 16:11:50 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Mitch Williams12479f92005-11-09 10:35:44 -0800154struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{ "slow", AD_LACP_SLOW},
156{ "fast", AD_LACP_FAST},
157{ NULL, -1},
158};
159
Mitch Williams12479f92005-11-09 10:35:44 -0800160struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{ "balance-rr", BOND_MODE_ROUNDROBIN},
162{ "active-backup", BOND_MODE_ACTIVEBACKUP},
163{ "balance-xor", BOND_MODE_XOR},
164{ "broadcast", BOND_MODE_BROADCAST},
165{ "802.3ad", BOND_MODE_8023AD},
166{ "balance-tlb", BOND_MODE_TLB},
167{ "balance-alb", BOND_MODE_ALB},
168{ NULL, -1},
169};
170
Mitch Williams12479f92005-11-09 10:35:44 -0800171struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400172{ "layer2", BOND_XMIT_POLICY_LAYER2},
173{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
174{ NULL, -1},
175};
176
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700177struct bond_parm_tbl arp_validate_tbl[] = {
178{ "none", BOND_ARP_VALIDATE_NONE},
179{ "active", BOND_ARP_VALIDATE_ACTIVE},
180{ "backup", BOND_ARP_VALIDATE_BACKUP},
181{ "all", BOND_ARP_VALIDATE_ALL},
182{ NULL, -1},
183};
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*-------------------------- Forward declarations ---------------------------*/
186
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400187static void bond_send_gratuitous_arp(struct bonding *bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/*---------------------------- General routines -----------------------------*/
190
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700191static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 switch (mode) {
194 case BOND_MODE_ROUNDROBIN :
195 return "load balancing (round-robin)";
196 case BOND_MODE_ACTIVEBACKUP :
197 return "fault-tolerance (active-backup)";
198 case BOND_MODE_XOR :
199 return "load balancing (xor)";
200 case BOND_MODE_BROADCAST :
201 return "fault-tolerance (broadcast)";
202 case BOND_MODE_8023AD:
203 return "IEEE 802.3ad Dynamic link aggregation";
204 case BOND_MODE_TLB:
205 return "transmit load balancing";
206 case BOND_MODE_ALB:
207 return "adaptive load balancing";
208 default:
209 return "unknown";
210 }
211}
212
213/*---------------------------------- VLAN -----------------------------------*/
214
215/**
216 * bond_add_vlan - add a new vlan id on bond
217 * @bond: bond that got the notification
218 * @vlan_id: the vlan id to add
219 *
220 * Returns -ENOMEM if allocation failed.
221 */
222static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
223{
224 struct vlan_entry *vlan;
225
226 dprintk("bond: %s, vlan id %d\n",
227 (bond ? bond->dev->name: "None"), vlan_id);
228
229 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
230 if (!vlan) {
231 return -ENOMEM;
232 }
233
234 INIT_LIST_HEAD(&vlan->vlan_list);
235 vlan->vlan_id = vlan_id;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400236 vlan->vlan_ip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 write_lock_bh(&bond->lock);
239
240 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
241
242 write_unlock_bh(&bond->lock);
243
244 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
245
246 return 0;
247}
248
249/**
250 * bond_del_vlan - delete a vlan id from bond
251 * @bond: bond that got the notification
252 * @vlan_id: the vlan id to delete
253 *
254 * returns -ENODEV if @vlan_id was not found in @bond.
255 */
256static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
257{
258 struct vlan_entry *vlan, *next;
259 int res = -ENODEV;
260
261 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
262
263 write_lock_bh(&bond->lock);
264
265 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
266 if (vlan->vlan_id == vlan_id) {
267 list_del(&vlan->vlan_list);
268
269 if ((bond->params.mode == BOND_MODE_TLB) ||
270 (bond->params.mode == BOND_MODE_ALB)) {
271 bond_alb_clear_vlan(bond, vlan_id);
272 }
273
274 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
275 bond->dev->name);
276
277 kfree(vlan);
278
279 if (list_empty(&bond->vlan_list) &&
280 (bond->slave_cnt == 0)) {
281 /* Last VLAN removed and no slaves, so
282 * restore block on adding VLANs. This will
283 * be removed once new slaves that are not
284 * VLAN challenged will be added.
285 */
286 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
287 }
288
289 res = 0;
290 goto out;
291 }
292 }
293
294 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
295 bond->dev->name);
296
297out:
298 write_unlock_bh(&bond->lock);
299 return res;
300}
301
302/**
303 * bond_has_challenged_slaves
304 * @bond: the bond we're working on
305 *
306 * Searches the slave list. Returns 1 if a vlan challenged slave
307 * was found, 0 otherwise.
308 *
309 * Assumes bond->lock is held.
310 */
311static int bond_has_challenged_slaves(struct bonding *bond)
312{
313 struct slave *slave;
314 int i;
315
316 bond_for_each_slave(bond, slave, i) {
317 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
318 dprintk("found VLAN challenged slave - %s\n",
319 slave->dev->name);
320 return 1;
321 }
322 }
323
324 dprintk("no VLAN challenged slaves found\n");
325 return 0;
326}
327
328/**
329 * bond_next_vlan - safely skip to the next item in the vlans list.
330 * @bond: the bond we're working on
331 * @curr: item we're advancing from
332 *
333 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
334 * or @curr->next otherwise (even if it is @curr itself again).
335 *
336 * Caller must hold bond->lock
337 */
338struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
339{
340 struct vlan_entry *next, *last;
341
342 if (list_empty(&bond->vlan_list)) {
343 return NULL;
344 }
345
346 if (!curr) {
347 next = list_entry(bond->vlan_list.next,
348 struct vlan_entry, vlan_list);
349 } else {
350 last = list_entry(bond->vlan_list.prev,
351 struct vlan_entry, vlan_list);
352 if (last == curr) {
353 next = list_entry(bond->vlan_list.next,
354 struct vlan_entry, vlan_list);
355 } else {
356 next = list_entry(curr->vlan_list.next,
357 struct vlan_entry, vlan_list);
358 }
359 }
360
361 return next;
362}
363
364/**
365 * bond_dev_queue_xmit - Prepare skb for xmit.
366 *
367 * @bond: bond device that got this skb for tx.
368 * @skb: hw accel VLAN tagged skb to transmit
369 * @slave_dev: slave that is supposed to xmit this skbuff
370 *
371 * When the bond gets an skb to transmit that is
372 * already hardware accelerated VLAN tagged, and it
373 * needs to relay this skb to a slave that is not
374 * hw accel capable, the skb needs to be "unaccelerated",
375 * i.e. strip the hwaccel tag and re-insert it as part
376 * of the payload.
377 */
378int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
379{
380 unsigned short vlan_id;
381
382 if (!list_empty(&bond->vlan_list) &&
383 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
384 vlan_get_tag(skb, &vlan_id) == 0) {
385 skb->dev = slave_dev;
386 skb = vlan_put_tag(skb, vlan_id);
387 if (!skb) {
388 /* vlan_put_tag() frees the skb in case of error,
389 * so return success here so the calling functions
390 * won't attempt to free is again.
391 */
392 return 0;
393 }
394 } else {
395 skb->dev = slave_dev;
396 }
397
398 skb->priority = 1;
399 dev_queue_xmit(skb);
400
401 return 0;
402}
403
404/*
405 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
406 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
407 * lock because:
408 * a. This operation is performed in IOCTL context,
409 * b. The operation is protected by the RTNL semaphore in the 8021q code,
410 * c. Holding a lock with BH disabled while directly calling a base driver
411 * entry point is generally a BAD idea.
412 *
413 * The design of synchronization/protection for this operation in the 8021q
414 * module is good for one or more VLAN devices over a single physical device
415 * and cannot be extended for a teaming solution like bonding, so there is a
416 * potential race condition here where a net device from the vlan group might
417 * be referenced (either by a base driver or the 8021q code) while it is being
418 * removed from the system. However, it turns out we're not making matters
419 * worse, and if it works for regular VLAN usage it will work here too.
420*/
421
422/**
423 * bond_vlan_rx_register - Propagates registration to slaves
424 * @bond_dev: bonding net device that got called
425 * @grp: vlan group being registered
426 */
427static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
428{
429 struct bonding *bond = bond_dev->priv;
430 struct slave *slave;
431 int i;
432
433 bond->vlgrp = grp;
434
435 bond_for_each_slave(bond, slave, i) {
436 struct net_device *slave_dev = slave->dev;
437
438 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
439 slave_dev->vlan_rx_register) {
440 slave_dev->vlan_rx_register(slave_dev, grp);
441 }
442 }
443}
444
445/**
446 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
447 * @bond_dev: bonding net device that got called
448 * @vid: vlan id being added
449 */
450static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
451{
452 struct bonding *bond = bond_dev->priv;
453 struct slave *slave;
454 int i, res;
455
456 bond_for_each_slave(bond, slave, i) {
457 struct net_device *slave_dev = slave->dev;
458
459 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
460 slave_dev->vlan_rx_add_vid) {
461 slave_dev->vlan_rx_add_vid(slave_dev, vid);
462 }
463 }
464
465 res = bond_add_vlan(bond, vid);
466 if (res) {
467 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800468 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 bond_dev->name, vid);
470 }
471}
472
473/**
474 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
475 * @bond_dev: bonding net device that got called
476 * @vid: vlan id being removed
477 */
478static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
479{
480 struct bonding *bond = bond_dev->priv;
481 struct slave *slave;
482 struct net_device *vlan_dev;
483 int i, res;
484
485 bond_for_each_slave(bond, slave, i) {
486 struct net_device *slave_dev = slave->dev;
487
488 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
489 slave_dev->vlan_rx_kill_vid) {
490 /* Save and then restore vlan_dev in the grp array,
491 * since the slave's driver might clear it.
492 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800493 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800495 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498
499 res = bond_del_vlan(bond, vid);
500 if (res) {
501 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800502 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 bond_dev->name, vid);
504 }
505}
506
507static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
508{
509 struct vlan_entry *vlan;
510
511 write_lock_bh(&bond->lock);
512
513 if (list_empty(&bond->vlan_list)) {
514 goto out;
515 }
516
517 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
518 slave_dev->vlan_rx_register) {
519 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
520 }
521
522 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
523 !(slave_dev->vlan_rx_add_vid)) {
524 goto out;
525 }
526
527 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
528 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
529 }
530
531out:
532 write_unlock_bh(&bond->lock);
533}
534
535static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
536{
537 struct vlan_entry *vlan;
538 struct net_device *vlan_dev;
539
540 write_lock_bh(&bond->lock);
541
542 if (list_empty(&bond->vlan_list)) {
543 goto out;
544 }
545
546 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
547 !(slave_dev->vlan_rx_kill_vid)) {
548 goto unreg;
549 }
550
551 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
552 /* Save and then restore vlan_dev in the grp array,
553 * since the slave's driver might clear it.
554 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800555 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800557 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560unreg:
561 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
562 slave_dev->vlan_rx_register) {
563 slave_dev->vlan_rx_register(slave_dev, NULL);
564 }
565
566out:
567 write_unlock_bh(&bond->lock);
568}
569
570/*------------------------------- Link status -------------------------------*/
571
572/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800573 * Set the carrier state for the master according to the state of its
574 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
575 * do special 802.3ad magic.
576 *
577 * Returns zero if carrier state does not change, nonzero if it does.
578 */
579static int bond_set_carrier(struct bonding *bond)
580{
581 struct slave *slave;
582 int i;
583
584 if (bond->slave_cnt == 0)
585 goto down;
586
587 if (bond->params.mode == BOND_MODE_8023AD)
588 return bond_3ad_set_carrier(bond);
589
590 bond_for_each_slave(bond, slave, i) {
591 if (slave->link == BOND_LINK_UP) {
592 if (!netif_carrier_ok(bond->dev)) {
593 netif_carrier_on(bond->dev);
594 return 1;
595 }
596 return 0;
597 }
598 }
599
600down:
601 if (netif_carrier_ok(bond->dev)) {
602 netif_carrier_off(bond->dev);
603 return 1;
604 }
605 return 0;
606}
607
608/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * Get link speed and duplex from the slave's base driver
610 * using ethtool. If for some reason the call fails or the
611 * values are invalid, fake speed and duplex to 100/Full
612 * and return error.
613 */
614static int bond_update_speed_duplex(struct slave *slave)
615{
616 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700618 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* Fake speed and duplex */
621 slave->speed = SPEED_100;
622 slave->duplex = DUPLEX_FULL;
623
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700624 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700627 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
628 if (res < 0)
629 return -1;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 switch (etool.speed) {
632 case SPEED_10:
633 case SPEED_100:
634 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700635 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 default:
638 return -1;
639 }
640
641 switch (etool.duplex) {
642 case DUPLEX_FULL:
643 case DUPLEX_HALF:
644 break;
645 default:
646 return -1;
647 }
648
649 slave->speed = etool.speed;
650 slave->duplex = etool.duplex;
651
652 return 0;
653}
654
655/*
656 * if <dev> supports MII link status reporting, check its link status.
657 *
658 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
659 * depening upon the setting of the use_carrier parameter.
660 *
661 * Return either BMSR_LSTATUS, meaning that the link is up (or we
662 * can't tell and just pretend it is), or 0, meaning that the link is
663 * down.
664 *
665 * If reporting is non-zero, instead of faking link up, return -1 if
666 * both ETHTOOL and MII ioctls fail (meaning the device does not
667 * support them). If use_carrier is set, return whatever it says.
668 * It'd be nice if there was a good way to tell if a driver supports
669 * netif_carrier, but there really isn't.
670 */
671static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
672{
673 static int (* ioctl)(struct net_device *, struct ifreq *, int);
674 struct ifreq ifr;
675 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (bond->params.use_carrier) {
678 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
679 }
680
681 ioctl = slave_dev->do_ioctl;
682 if (ioctl) {
683 /* TODO: set pointer to correct ioctl on a per team member */
684 /* bases to make this more efficient. that is, once */
685 /* we determine the correct ioctl, we will always */
686 /* call it and not the others for that team */
687 /* member. */
688
689 /*
690 * We cannot assume that SIOCGMIIPHY will also read a
691 * register; not all network drivers (e.g., e100)
692 * support that.
693 */
694
695 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
696 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
697 mii = if_mii(&ifr);
698 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
699 mii->reg_num = MII_BMSR;
700 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
701 return (mii->val_out & BMSR_LSTATUS);
702 }
703 }
704 }
705
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700706 /*
707 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
708 * attempt to get link status from it if the above MII ioctls fail.
709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (slave_dev->ethtool_ops) {
711 if (slave_dev->ethtool_ops->get_link) {
712 u32 link;
713
714 link = slave_dev->ethtool_ops->get_link(slave_dev);
715
716 return link ? BMSR_LSTATUS : 0;
717 }
718 }
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700722 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * cannot report link status). If not reporting, pretend
724 * we're ok.
725 */
726 return (reporting ? -1 : BMSR_LSTATUS);
727}
728
729/*----------------------------- Multicast list ------------------------------*/
730
731/*
732 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
733 */
734static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
735{
736 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
737 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
738}
739
740/*
741 * returns dmi entry if found, NULL otherwise
742 */
743static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
744{
745 struct dev_mc_list *idmi;
746
747 for (idmi = mc_list; idmi; idmi = idmi->next) {
748 if (bond_is_dmi_same(dmi, idmi)) {
749 return idmi;
750 }
751 }
752
753 return NULL;
754}
755
756/*
757 * Push the promiscuity flag down to appropriate slaves
758 */
759static void bond_set_promiscuity(struct bonding *bond, int inc)
760{
761 if (USES_PRIMARY(bond->params.mode)) {
762 /* write lock already acquired */
763 if (bond->curr_active_slave) {
764 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
765 }
766 } else {
767 struct slave *slave;
768 int i;
769 bond_for_each_slave(bond, slave, i) {
770 dev_set_promiscuity(slave->dev, inc);
771 }
772 }
773}
774
775/*
776 * Push the allmulti flag down to all slaves
777 */
778static void bond_set_allmulti(struct bonding *bond, int inc)
779{
780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
783 dev_set_allmulti(bond->curr_active_slave->dev, inc);
784 }
785 } else {
786 struct slave *slave;
787 int i;
788 bond_for_each_slave(bond, slave, i) {
789 dev_set_allmulti(slave->dev, inc);
790 }
791 }
792}
793
794/*
795 * Add a Multicast address to slaves
796 * according to mode
797 */
798static void bond_mc_add(struct bonding *bond, void *addr, int alen)
799{
800 if (USES_PRIMARY(bond->params.mode)) {
801 /* write lock already acquired */
802 if (bond->curr_active_slave) {
803 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
804 }
805 } else {
806 struct slave *slave;
807 int i;
808 bond_for_each_slave(bond, slave, i) {
809 dev_mc_add(slave->dev, addr, alen, 0);
810 }
811 }
812}
813
814/*
815 * Remove a multicast address from slave
816 * according to mode
817 */
818static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
819{
820 if (USES_PRIMARY(bond->params.mode)) {
821 /* write lock already acquired */
822 if (bond->curr_active_slave) {
823 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
824 }
825 } else {
826 struct slave *slave;
827 int i;
828 bond_for_each_slave(bond, slave, i) {
829 dev_mc_delete(slave->dev, addr, alen, 0);
830 }
831 }
832}
833
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800834
835/*
836 * Retrieve the list of registered multicast addresses for the bonding
837 * device and retransmit an IGMP JOIN request to the current active
838 * slave.
839 */
840static void bond_resend_igmp_join_requests(struct bonding *bond)
841{
842 struct in_device *in_dev;
843 struct ip_mc_list *im;
844
845 rcu_read_lock();
846 in_dev = __in_dev_get_rcu(bond->dev);
847 if (in_dev) {
848 for (im = in_dev->mc_list; im; im = im->next) {
849 ip_mc_rejoin_group(im);
850 }
851 }
852
853 rcu_read_unlock();
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * Totally destroys the mc_list in bond
858 */
859static void bond_mc_list_destroy(struct bonding *bond)
860{
861 struct dev_mc_list *dmi;
862
863 dmi = bond->mc_list;
864 while (dmi) {
865 bond->mc_list = dmi->next;
866 kfree(dmi);
867 dmi = bond->mc_list;
868 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800869 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/*
873 * Copy all the Multicast addresses from src to the bonding device dst
874 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700875static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100876 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct dev_mc_list *dmi, *new_dmi;
879
880 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700881 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (!new_dmi) {
884 /* FIXME: Potential memory leak !!! */
885 return -ENOMEM;
886 }
887
888 new_dmi->next = bond->mc_list;
889 bond->mc_list = new_dmi;
890 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
891 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
892 new_dmi->dmi_users = dmi->dmi_users;
893 new_dmi->dmi_gusers = dmi->dmi_gusers;
894 }
895
896 return 0;
897}
898
899/*
900 * flush all members of flush->mc_list from device dev->mc_list
901 */
902static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
903{
904 struct bonding *bond = bond_dev->priv;
905 struct dev_mc_list *dmi;
906
907 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
908 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
909 }
910
911 if (bond->params.mode == BOND_MODE_8023AD) {
912 /* del lacpdu mc addr from mc list */
913 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
914
915 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
916 }
917}
918
919/*--------------------------- Active slave change ---------------------------*/
920
921/*
922 * Update the mc list and multicast-related flags for the new and
923 * old active slaves (if any) according to the multicast mode, and
924 * promiscuous flags unconditionally.
925 */
926static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
927{
928 struct dev_mc_list *dmi;
929
930 if (!USES_PRIMARY(bond->params.mode)) {
931 /* nothing to do - mc list is already up-to-date on
932 * all slaves
933 */
934 return;
935 }
936
937 if (old_active) {
938 if (bond->dev->flags & IFF_PROMISC) {
939 dev_set_promiscuity(old_active->dev, -1);
940 }
941
942 if (bond->dev->flags & IFF_ALLMULTI) {
943 dev_set_allmulti(old_active->dev, -1);
944 }
945
946 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
947 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
948 }
949 }
950
951 if (new_active) {
952 if (bond->dev->flags & IFF_PROMISC) {
953 dev_set_promiscuity(new_active->dev, 1);
954 }
955
956 if (bond->dev->flags & IFF_ALLMULTI) {
957 dev_set_allmulti(new_active->dev, 1);
958 }
959
960 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
961 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
962 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800963 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965}
966
967/**
968 * find_best_interface - select the best available slave to be the active one
969 * @bond: our bonding struct
970 *
971 * Warning: Caller must hold curr_slave_lock for writing.
972 */
973static struct slave *bond_find_best_slave(struct bonding *bond)
974{
975 struct slave *new_active, *old_active;
976 struct slave *bestslave = NULL;
977 int mintime = bond->params.updelay;
978 int i;
979
980 new_active = old_active = bond->curr_active_slave;
981
982 if (!new_active) { /* there were no active slaves left */
983 if (bond->slave_cnt > 0) { /* found one slave */
984 new_active = bond->first_slave;
985 } else {
986 return NULL; /* still no slave, return NULL */
987 }
988 }
989
990 /* first try the primary link; if arping, a link must tx/rx traffic
991 * before it can be considered the curr_active_slave - also, we would skip
992 * slaves between the curr_active_slave and primary_slave that may be up
993 * and able to arp
994 */
995 if ((bond->primary_slave) &&
996 (!bond->params.arp_interval) &&
997 (IS_UP(bond->primary_slave->dev))) {
998 new_active = bond->primary_slave;
999 }
1000
1001 /* remember where to stop iterating over the slaves */
1002 old_active = new_active;
1003
1004 bond_for_each_slave_from(bond, new_active, i, old_active) {
1005 if (IS_UP(new_active->dev)) {
1006 if (new_active->link == BOND_LINK_UP) {
1007 return new_active;
1008 } else if (new_active->link == BOND_LINK_BACK) {
1009 /* link up, but waiting for stabilization */
1010 if (new_active->delay < mintime) {
1011 mintime = new_active->delay;
1012 bestslave = new_active;
1013 }
1014 }
1015 }
1016 }
1017
1018 return bestslave;
1019}
1020
1021/**
1022 * change_active_interface - change the active slave into the specified one
1023 * @bond: our bonding struct
1024 * @new: the new slave to make the active one
1025 *
1026 * Set the new slave to the bond's settings and unset them on the old
1027 * curr_active_slave.
1028 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1029 *
1030 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1031 * because it is apparently the best available slave we have, even though its
1032 * updelay hasn't timed out yet.
1033 *
1034 * Warning: Caller must hold curr_slave_lock for writing.
1035 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001036void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct slave *old_active = bond->curr_active_slave;
1039
1040 if (old_active == new_active) {
1041 return;
1042 }
1043
1044 if (new_active) {
1045 if (new_active->link == BOND_LINK_BACK) {
1046 if (USES_PRIMARY(bond->params.mode)) {
1047 printk(KERN_INFO DRV_NAME
1048 ": %s: making interface %s the new "
1049 "active one %d ms earlier.\n",
1050 bond->dev->name, new_active->dev->name,
1051 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1052 }
1053
1054 new_active->delay = 0;
1055 new_active->link = BOND_LINK_UP;
1056 new_active->jiffies = jiffies;
1057
1058 if (bond->params.mode == BOND_MODE_8023AD) {
1059 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1060 }
1061
1062 if ((bond->params.mode == BOND_MODE_TLB) ||
1063 (bond->params.mode == BOND_MODE_ALB)) {
1064 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1065 }
1066 } else {
1067 if (USES_PRIMARY(bond->params.mode)) {
1068 printk(KERN_INFO DRV_NAME
1069 ": %s: making interface %s the new "
1070 "active one.\n",
1071 bond->dev->name, new_active->dev->name);
1072 }
1073 }
1074 }
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (USES_PRIMARY(bond->params.mode)) {
1077 bond_mc_swap(bond, new_active, old_active);
1078 }
1079
1080 if ((bond->params.mode == BOND_MODE_TLB) ||
1081 (bond->params.mode == BOND_MODE_ALB)) {
1082 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001083 if (old_active)
1084 bond_set_slave_inactive_flags(old_active);
1085 if (new_active)
1086 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 } else {
1088 bond->curr_active_slave = new_active;
1089 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001090
1091 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1092 if (old_active) {
1093 bond_set_slave_inactive_flags(old_active);
1094 }
1095
1096 if (new_active) {
1097 bond_set_slave_active_flags(new_active);
1098 }
Moni Shoua2ab82852007-10-09 19:43:39 -07001099
1100 /* when bonding does not set the slave MAC address, the bond MAC
1101 * address is the one of the active slave.
1102 */
1103 if (new_active && !bond->do_set_mac_addr)
1104 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1105 new_active->dev->addr_len);
1106
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001107 bond_send_gratuitous_arp(bond);
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111/**
1112 * bond_select_active_slave - select a new active slave, if needed
1113 * @bond: our bonding struct
1114 *
1115 * This functions shoud be called when one of the following occurs:
1116 * - The old curr_active_slave has been released or lost its link.
1117 * - The primary_slave has got its link back.
1118 * - A slave has got its link back and there's no old curr_active_slave.
1119 *
1120 * Warning: Caller must hold curr_slave_lock for writing.
1121 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001122void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001125 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 best_slave = bond_find_best_slave(bond);
1128 if (best_slave != bond->curr_active_slave) {
1129 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001130 rv = bond_set_carrier(bond);
1131 if (!rv)
1132 return;
1133
1134 if (netif_carrier_ok(bond->dev)) {
1135 printk(KERN_INFO DRV_NAME
1136 ": %s: first active interface up!\n",
1137 bond->dev->name);
1138 } else {
1139 printk(KERN_INFO DRV_NAME ": %s: "
1140 "now running without any active interface !\n",
1141 bond->dev->name);
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144}
1145
1146/*--------------------------- slave list handling ---------------------------*/
1147
1148/*
1149 * This function attaches the slave to the end of list.
1150 *
1151 * bond->lock held for writing by caller.
1152 */
1153static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1154{
1155 if (bond->first_slave == NULL) { /* attaching the first slave */
1156 new_slave->next = new_slave;
1157 new_slave->prev = new_slave;
1158 bond->first_slave = new_slave;
1159 } else {
1160 new_slave->next = bond->first_slave;
1161 new_slave->prev = bond->first_slave->prev;
1162 new_slave->next->prev = new_slave;
1163 new_slave->prev->next = new_slave;
1164 }
1165
1166 bond->slave_cnt++;
1167}
1168
1169/*
1170 * This function detaches the slave from the list.
1171 * WARNING: no check is made to verify if the slave effectively
1172 * belongs to <bond>.
1173 * Nothing is freed on return, structures are just unchained.
1174 * If any slave pointer in bond was pointing to <slave>,
1175 * it should be changed by the calling function.
1176 *
1177 * bond->lock held for writing by caller.
1178 */
1179static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1180{
1181 if (slave->next) {
1182 slave->next->prev = slave->prev;
1183 }
1184
1185 if (slave->prev) {
1186 slave->prev->next = slave->next;
1187 }
1188
1189 if (bond->first_slave == slave) { /* slave is the first slave */
1190 if (bond->slave_cnt > 1) { /* there are more slave */
1191 bond->first_slave = slave->next;
1192 } else {
1193 bond->first_slave = NULL; /* slave was the last one */
1194 }
1195 }
1196
1197 slave->next = NULL;
1198 slave->prev = NULL;
1199 bond->slave_cnt--;
1200}
1201
1202/*---------------------------------- IOCTL ----------------------------------*/
1203
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001204static int bond_sethwaddr(struct net_device *bond_dev,
1205 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 dprintk("bond_dev=%p\n", bond_dev);
1208 dprintk("slave_dev=%p\n", slave_dev);
1209 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1210 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1211 return 0;
1212}
1213
Herbert Xu7f353bf2007-08-10 15:47:58 -07001214#define BOND_VLAN_FEATURES \
1215 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1216 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001217
1218/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001219 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001220 * feature bits are managed elsewhere, so preserve those feature bits
1221 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001222 */
1223static int bond_compute_features(struct bonding *bond)
1224{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001225 struct slave *slave;
1226 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001227 unsigned long features = bond_dev->features;
Moni Shoua3158bf72007-10-09 19:43:41 -07001228 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1229 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001230 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001231
Herbert Xu7f353bf2007-08-10 15:47:58 -07001232 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1233 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1234 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1235
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001236 bond_for_each_slave(bond, slave, i) {
Herbert Xu7f353bf2007-08-10 15:47:58 -07001237 features = netdev_compute_features(features,
1238 slave->dev->features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001239 if (slave->dev->hard_header_len > max_hard_header_len)
1240 max_hard_header_len = slave->dev->hard_header_len;
1241 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001242
Herbert Xu7f353bf2007-08-10 15:47:58 -07001243 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001244 bond_dev->features = features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001245 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001246
1247 return 0;
1248}
1249
Moni Shoua872254d2007-10-09 19:43:38 -07001250
1251static void bond_setup_by_slave(struct net_device *bond_dev,
1252 struct net_device *slave_dev)
1253{
1254 bond_dev->neigh_setup = slave_dev->neigh_setup;
1255
1256 bond_dev->type = slave_dev->type;
1257 bond_dev->hard_header_len = slave_dev->hard_header_len;
1258 bond_dev->addr_len = slave_dev->addr_len;
1259
1260 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1261 slave_dev->addr_len);
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001265int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct bonding *bond = bond_dev->priv;
1268 struct slave *new_slave = NULL;
1269 struct dev_mc_list *dmi;
1270 struct sockaddr addr;
1271 int link_reporting;
1272 int old_features = bond_dev->features;
1273 int res = 0;
1274
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001275 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1276 slave_dev->do_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001278 ": %s: Warning: no link monitoring support for %s\n",
1279 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
1282 /* bond must be initialized by bond_open() before enslaving */
1283 if (!(bond_dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -07001284 printk(KERN_WARNING DRV_NAME
1285 " %s: master_dev is not up in bond_enslave\n",
1286 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
1289 /* already enslaved */
1290 if (slave_dev->flags & IFF_SLAVE) {
1291 dprintk("Error, Device was already enslaved\n");
1292 return -EBUSY;
1293 }
1294
1295 /* vlan challenged mutual exclusion */
1296 /* no need to lock since we're protected by rtnl_lock */
1297 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1298 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1299 if (!list_empty(&bond->vlan_list)) {
1300 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001301 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001303 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 bond_dev->name);
1305 return -EPERM;
1306 } else {
1307 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001308 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 "slave %s. Adding VLANs will be blocked as "
1310 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001311 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 bond_dev->name);
1313 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1314 }
1315 } else {
1316 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1317 if (bond->slave_cnt == 0) {
1318 /* First slave, and it is not VLAN challenged,
1319 * so remove the block of adding VLANs over the bond.
1320 */
1321 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1322 }
1323 }
1324
Jay Vosburgh217df672005-09-26 16:11:50 -07001325 /*
1326 * Old ifenslave binaries are no longer supported. These can
1327 * be identified with moderate accurary by the state of the slave:
1328 * the current ifenslave will set the interface down prior to
1329 * enslaving it; the old ifenslave will not.
1330 */
1331 if ((slave_dev->flags & IFF_UP)) {
1332 printk(KERN_ERR DRV_NAME ": %s is up. "
1333 "This may be due to an out of date ifenslave.\n",
1334 slave_dev->name);
1335 res = -EPERM;
1336 goto err_undo_flags;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Moni Shoua872254d2007-10-09 19:43:38 -07001339 /* set bonding device ether type by slave - bonding netdevices are
1340 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1341 * there is a need to override some of the type dependent attribs/funcs.
1342 *
1343 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1344 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1345 */
1346 if (bond->slave_cnt == 0) {
1347 if (slave_dev->type != ARPHRD_ETHER)
1348 bond_setup_by_slave(bond_dev, slave_dev);
1349 } else if (bond_dev->type != slave_dev->type) {
1350 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1351 "from other slaves (%d), can not enslave it.\n",
1352 slave_dev->name,
1353 slave_dev->type, bond_dev->type);
1354 res = -EINVAL;
1355 goto err_undo_flags;
1356 }
1357
Jay Vosburgh217df672005-09-26 16:11:50 -07001358 if (slave_dev->set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001359 if (bond->slave_cnt == 0) {
1360 printk(KERN_WARNING DRV_NAME
1361 ": %s: Warning: The first slave device you "
1362 "specified does not support setting the MAC "
1363 "address. This bond MAC address would be that "
1364 "of the active slave.\n", bond_dev->name);
1365 bond->do_set_mac_addr = 0;
1366 } else if (bond->do_set_mac_addr) {
1367 printk(KERN_ERR DRV_NAME
1368 ": %s: Error: The slave device you specified "
1369 "does not support setting the MAC addres,."
1370 "but this bond uses this practice. \n"
1371 , bond_dev->name);
1372 res = -EOPNOTSUPP;
1373 goto err_undo_flags;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
Joe Jin243cb4e2007-02-06 14:16:40 -08001377 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!new_slave) {
1379 res = -ENOMEM;
1380 goto err_undo_flags;
1381 }
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* save slave's original flags before calling
1384 * netdev_set_master and dev_open
1385 */
1386 new_slave->original_flags = slave_dev->flags;
1387
Jay Vosburgh217df672005-09-26 16:11:50 -07001388 /*
1389 * Save slave's original ("permanent") mac address for modes
1390 * that need it, and for restoring it upon release, and then
1391 * set it to the master's address
1392 */
1393 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Moni Shoua2ab82852007-10-09 19:43:39 -07001395 if (bond->do_set_mac_addr) {
1396 /*
1397 * Set slave to master's mac address. The application already
1398 * set the master's mac address to that of the first slave
1399 */
1400 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1401 addr.sa_family = slave_dev->type;
1402 res = dev_set_mac_address(slave_dev, &addr);
1403 if (res) {
1404 dprintk("Error %d calling set_mac_address\n", res);
1405 goto err_free;
1406 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001409 res = netdev_set_master(slave_dev, bond_dev);
1410 if (res) {
1411 dprintk("Error %d calling netdev_set_master\n", res);
1412 goto err_close;
1413 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001414 /* open the slave since the application closed it */
1415 res = dev_open(slave_dev);
1416 if (res) {
1417 dprintk("Openning slave %s failed\n", slave_dev->name);
1418 goto err_restore_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001422 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if ((bond->params.mode == BOND_MODE_TLB) ||
1425 (bond->params.mode == BOND_MODE_ALB)) {
1426 /* bond_alb_init_slave() must be called before all other stages since
1427 * it might fail and we do not want to have to undo everything
1428 */
1429 res = bond_alb_init_slave(bond, new_slave);
1430 if (res) {
1431 goto err_unset_master;
1432 }
1433 }
1434
1435 /* If the mode USES_PRIMARY, then the new slave gets the
1436 * master's promisc (and mc) settings only if it becomes the
1437 * curr_active_slave, and that is taken care of later when calling
1438 * bond_change_active()
1439 */
1440 if (!USES_PRIMARY(bond->params.mode)) {
1441 /* set promiscuity level to new slave */
1442 if (bond_dev->flags & IFF_PROMISC) {
1443 dev_set_promiscuity(slave_dev, 1);
1444 }
1445
1446 /* set allmulti level to new slave */
1447 if (bond_dev->flags & IFF_ALLMULTI) {
1448 dev_set_allmulti(slave_dev, 1);
1449 }
1450
1451 /* upload master's mc_list to new slave */
1452 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1453 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1454 }
1455 }
1456
1457 if (bond->params.mode == BOND_MODE_8023AD) {
1458 /* add lacpdu mc addr to mc list */
1459 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1460
1461 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1462 }
1463
1464 bond_add_vlans_on_slave(bond, slave_dev);
1465
1466 write_lock_bh(&bond->lock);
1467
1468 bond_attach_slave(bond, new_slave);
1469
1470 new_slave->delay = 0;
1471 new_slave->link_failure_count = 0;
1472
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001473 bond_compute_features(bond);
1474
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001475 new_slave->last_arp_rx = jiffies;
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (bond->params.miimon && !bond->params.use_carrier) {
1478 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1479
1480 if ((link_reporting == -1) && !bond->params.arp_interval) {
1481 /*
1482 * miimon is set but a bonded network driver
1483 * does not support ETHTOOL/MII and
1484 * arp_interval is not set. Note: if
1485 * use_carrier is enabled, we will never go
1486 * here (because netif_carrier is always
1487 * supported); thus, we don't need to change
1488 * the messages for netif_carrier.
1489 */
1490 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001491 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 "available for interface %s, and "
1493 "arp_interval/arp_ip_target module parameters "
1494 "not specified, thus bonding will not detect "
1495 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001496 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 } else if (link_reporting == -1) {
1498 /* unable get link status using mii/ethtool */
1499 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001500 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 "interface %s; the network driver associated "
1502 "with this interface does not support MII or "
1503 "ETHTOOL link status reporting, thus miimon "
1504 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001505 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 }
1508
1509 /* check for initial state */
1510 if (!bond->params.miimon ||
1511 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1512 if (bond->params.updelay) {
1513 dprintk("Initial state of slave_dev is "
1514 "BOND_LINK_BACK\n");
1515 new_slave->link = BOND_LINK_BACK;
1516 new_slave->delay = bond->params.updelay;
1517 } else {
1518 dprintk("Initial state of slave_dev is "
1519 "BOND_LINK_UP\n");
1520 new_slave->link = BOND_LINK_UP;
1521 }
1522 new_slave->jiffies = jiffies;
1523 } else {
1524 dprintk("Initial state of slave_dev is "
1525 "BOND_LINK_DOWN\n");
1526 new_slave->link = BOND_LINK_DOWN;
1527 }
1528
1529 if (bond_update_speed_duplex(new_slave) &&
1530 (new_slave->link != BOND_LINK_DOWN)) {
1531 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001532 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001534 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (bond->params.mode == BOND_MODE_8023AD) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001537 printk(KERN_WARNING DRV_NAME
1538 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001540 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 }
1543
1544 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1545 /* if there is a primary slave, remember it */
1546 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1547 bond->primary_slave = new_slave;
1548 }
1549 }
1550
1551 switch (bond->params.mode) {
1552 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001553 bond_set_slave_inactive_flags(new_slave);
1554 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556 case BOND_MODE_8023AD:
1557 /* in 802.3ad mode, the internal mechanism
1558 * will activate the slaves in the selected
1559 * aggregator
1560 */
1561 bond_set_slave_inactive_flags(new_slave);
1562 /* if this is the first slave */
1563 if (bond->slave_cnt == 1) {
1564 SLAVE_AD_INFO(new_slave).id = 1;
1565 /* Initialize AD with the number of times that the AD timer is called in 1 second
1566 * can be called only after the mac address of the bond is set
1567 */
1568 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1569 bond->params.lacp_fast);
1570 } else {
1571 SLAVE_AD_INFO(new_slave).id =
1572 SLAVE_AD_INFO(new_slave->prev).id + 1;
1573 }
1574
1575 bond_3ad_bind_slave(new_slave);
1576 break;
1577 case BOND_MODE_TLB:
1578 case BOND_MODE_ALB:
1579 new_slave->state = BOND_STATE_ACTIVE;
1580 if ((!bond->curr_active_slave) &&
1581 (new_slave->link != BOND_LINK_DOWN)) {
1582 /* first slave or no active slave yet, and this link
1583 * is OK, so make this interface the active one
1584 */
1585 bond_change_active_slave(bond, new_slave);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001586 } else {
1587 bond_set_slave_inactive_flags(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 break;
1590 default:
1591 dprintk("This slave is always active in trunk mode\n");
1592
1593 /* always active in trunk mode */
1594 new_slave->state = BOND_STATE_ACTIVE;
1595
1596 /* In trunking mode there is little meaning to curr_active_slave
1597 * anyway (it holds no special properties of the bond device),
1598 * so we can change it without calling change_active_interface()
1599 */
1600 if (!bond->curr_active_slave) {
1601 bond->curr_active_slave = new_slave;
1602 }
1603 break;
1604 } /* switch(bond_mode) */
1605
Jay Vosburghff59c452006-03-27 13:27:43 -08001606 bond_set_carrier(bond);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 write_unlock_bh(&bond->lock);
1609
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001610 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1611 if (res)
1612 goto err_unset_master;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 printk(KERN_INFO DRV_NAME
1615 ": %s: enslaving %s as a%s interface with a%s link.\n",
1616 bond_dev->name, slave_dev->name,
1617 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1618 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1619
1620 /* enslave is successful */
1621 return 0;
1622
1623/* Undo stages on error */
1624err_unset_master:
1625 netdev_set_master(slave_dev, NULL);
1626
1627err_close:
1628 dev_close(slave_dev);
1629
1630err_restore_mac:
Moni Shoua2ab82852007-10-09 19:43:39 -07001631 if (bond->do_set_mac_addr) {
1632 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1633 addr.sa_family = slave_dev->type;
1634 dev_set_mac_address(slave_dev, &addr);
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637err_free:
1638 kfree(new_slave);
1639
1640err_undo_flags:
1641 bond_dev->features = old_features;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return res;
1644}
1645
1646/*
1647 * Try to release the slave device <slave> from the bond device <master>
1648 * It is legal to access curr_active_slave without a lock because all the function
1649 * is write-locked.
1650 *
1651 * The rules for slave state should be:
1652 * for Active/Backup:
1653 * Active stays on all backups go down
1654 * for Bonded connections:
1655 * The first up interface should be left on and all others downed.
1656 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001657int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct bonding *bond = bond_dev->priv;
1660 struct slave *slave, *oldcurrent;
1661 struct sockaddr addr;
1662 int mac_addr_differ;
Joe Perches0795af52007-10-03 17:59:30 -07001663 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* slave is not a slave or master is not master of this slave */
1666 if (!(slave_dev->flags & IFF_SLAVE) ||
1667 (slave_dev->master != bond_dev)) {
1668 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001669 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 bond_dev->name, slave_dev->name);
1671 return -EINVAL;
1672 }
1673
1674 write_lock_bh(&bond->lock);
1675
1676 slave = bond_get_slave_by_dev(bond, slave_dev);
1677 if (!slave) {
1678 /* not a slave of this bond */
1679 printk(KERN_INFO DRV_NAME
1680 ": %s: %s not enslaved\n",
1681 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001682 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return -EINVAL;
1684 }
1685
1686 mac_addr_differ = memcmp(bond_dev->dev_addr,
1687 slave->perm_hwaddr,
1688 ETH_ALEN);
1689 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1690 printk(KERN_WARNING DRV_NAME
Joe Perches0795af52007-10-03 17:59:30 -07001691 ": %s: Warning: the permanent HWaddr of %s - "
1692 "%s - is still in use by %s. "
1693 "Set the HWaddr of %s to a different address "
1694 "to avoid conflicts.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001695 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 slave_dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001697 print_mac(mac, slave->perm_hwaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 bond_dev->name,
1699 slave_dev->name);
1700 }
1701
1702 /* Inform AD package of unbinding of slave. */
1703 if (bond->params.mode == BOND_MODE_8023AD) {
1704 /* must be called before the slave is
1705 * detached from the list
1706 */
1707 bond_3ad_unbind_slave(slave);
1708 }
1709
1710 printk(KERN_INFO DRV_NAME
1711 ": %s: releasing %s interface %s\n",
1712 bond_dev->name,
1713 (slave->state == BOND_STATE_ACTIVE)
1714 ? "active" : "backup",
1715 slave_dev->name);
1716
1717 oldcurrent = bond->curr_active_slave;
1718
1719 bond->current_arp_slave = NULL;
1720
1721 /* release the slave from its bond */
1722 bond_detach_slave(bond, slave);
1723
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001724 bond_compute_features(bond);
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (bond->primary_slave == slave) {
1727 bond->primary_slave = NULL;
1728 }
1729
1730 if (oldcurrent == slave) {
1731 bond_change_active_slave(bond, NULL);
1732 }
1733
1734 if ((bond->params.mode == BOND_MODE_TLB) ||
1735 (bond->params.mode == BOND_MODE_ALB)) {
1736 /* Must be called only after the slave has been
1737 * detached from the list and the curr_active_slave
1738 * has been cleared (if our_slave == old_current),
1739 * but before a new active slave is selected.
1740 */
1741 bond_alb_deinit_slave(bond, slave);
1742 }
1743
Jay Vosburghff59c452006-03-27 13:27:43 -08001744 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 bond_select_active_slave(bond);
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001748 bond_set_carrier(bond);
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /* if the last slave was removed, zero the mac address
1751 * of the master so it will be set by the application
1752 * to the mac address of the first slave
1753 */
1754 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1755
1756 if (list_empty(&bond->vlan_list)) {
1757 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1758 } else {
1759 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001760 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001762 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001764 ": %s: When re-adding slaves, make sure the bond's "
1765 "HW address matches its VLANs'.\n",
1766 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1769 !bond_has_challenged_slaves(bond)) {
1770 printk(KERN_INFO DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001771 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001773 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1775 }
1776
1777 write_unlock_bh(&bond->lock);
1778
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001779 /* must do this from outside any spinlocks */
1780 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 bond_del_vlans_from_slave(bond, slave_dev);
1783
1784 /* If the mode USES_PRIMARY, then we should only remove its
1785 * promisc and mc settings if it was the curr_active_slave, but that was
1786 * already taken care of above when we detached the slave
1787 */
1788 if (!USES_PRIMARY(bond->params.mode)) {
1789 /* unset promiscuity level from slave */
1790 if (bond_dev->flags & IFF_PROMISC) {
1791 dev_set_promiscuity(slave_dev, -1);
1792 }
1793
1794 /* unset allmulti level from slave */
1795 if (bond_dev->flags & IFF_ALLMULTI) {
1796 dev_set_allmulti(slave_dev, -1);
1797 }
1798
1799 /* flush master's mc_list from slave */
1800 bond_mc_list_flush(bond_dev, slave_dev);
1801 }
1802
1803 netdev_set_master(slave_dev, NULL);
1804
1805 /* close slave before restoring its mac address */
1806 dev_close(slave_dev);
1807
Moni Shoua2ab82852007-10-09 19:43:39 -07001808 if (bond->do_set_mac_addr) {
1809 /* restore original ("permanent") mac address */
1810 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1811 addr.sa_family = slave_dev->type;
1812 dev_set_mac_address(slave_dev, &addr);
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001815 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001816 IFF_SLAVE_INACTIVE | IFF_BONDING |
1817 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 kfree(slave);
1820
1821 return 0; /* deletion OK */
1822}
1823
1824/*
1825 * This function releases all slaves.
1826 */
1827static int bond_release_all(struct net_device *bond_dev)
1828{
1829 struct bonding *bond = bond_dev->priv;
1830 struct slave *slave;
1831 struct net_device *slave_dev;
1832 struct sockaddr addr;
1833
1834 write_lock_bh(&bond->lock);
1835
Jay Vosburghff59c452006-03-27 13:27:43 -08001836 netif_carrier_off(bond_dev);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (bond->slave_cnt == 0) {
1839 goto out;
1840 }
1841
1842 bond->current_arp_slave = NULL;
1843 bond->primary_slave = NULL;
1844 bond_change_active_slave(bond, NULL);
1845
1846 while ((slave = bond->first_slave) != NULL) {
1847 /* Inform AD package of unbinding of slave
1848 * before slave is detached from the list.
1849 */
1850 if (bond->params.mode == BOND_MODE_8023AD) {
1851 bond_3ad_unbind_slave(slave);
1852 }
1853
1854 slave_dev = slave->dev;
1855 bond_detach_slave(bond, slave);
1856
1857 if ((bond->params.mode == BOND_MODE_TLB) ||
1858 (bond->params.mode == BOND_MODE_ALB)) {
1859 /* must be called only after the slave
1860 * has been detached from the list
1861 */
1862 bond_alb_deinit_slave(bond, slave);
1863 }
1864
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001865 bond_compute_features(bond);
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /* now that the slave is detached, unlock and perform
1868 * all the undo steps that should not be called from
1869 * within a lock.
1870 */
1871 write_unlock_bh(&bond->lock);
1872
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001873 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 bond_del_vlans_from_slave(bond, slave_dev);
1875
1876 /* If the mode USES_PRIMARY, then we should only remove its
1877 * promisc and mc settings if it was the curr_active_slave, but that was
1878 * already taken care of above when we detached the slave
1879 */
1880 if (!USES_PRIMARY(bond->params.mode)) {
1881 /* unset promiscuity level from slave */
1882 if (bond_dev->flags & IFF_PROMISC) {
1883 dev_set_promiscuity(slave_dev, -1);
1884 }
1885
1886 /* unset allmulti level from slave */
1887 if (bond_dev->flags & IFF_ALLMULTI) {
1888 dev_set_allmulti(slave_dev, -1);
1889 }
1890
1891 /* flush master's mc_list from slave */
1892 bond_mc_list_flush(bond_dev, slave_dev);
1893 }
1894
1895 netdev_set_master(slave_dev, NULL);
1896
1897 /* close slave before restoring its mac address */
1898 dev_close(slave_dev);
1899
Moni Shoua2ab82852007-10-09 19:43:39 -07001900 if (bond->do_set_mac_addr) {
1901 /* restore original ("permanent") mac address*/
1902 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1903 addr.sa_family = slave_dev->type;
1904 dev_set_mac_address(slave_dev, &addr);
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001907 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1908 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 kfree(slave);
1911
1912 /* re-acquire the lock before getting the next slave */
1913 write_lock_bh(&bond->lock);
1914 }
1915
1916 /* zero the mac address of the master so it will be
1917 * set by the application to the mac address of the
1918 * first slave
1919 */
1920 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1921
1922 if (list_empty(&bond->vlan_list)) {
1923 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1924 } else {
1925 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001926 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001928 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001930 ": %s: When re-adding slaves, make sure the bond's "
1931 "HW address matches its VLANs'.\n",
1932 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 printk(KERN_INFO DRV_NAME
1936 ": %s: released all slaves\n",
1937 bond_dev->name);
1938
1939out:
1940 write_unlock_bh(&bond->lock);
1941
1942 return 0;
1943}
1944
1945/*
1946 * This function changes the active slave to slave <slave_dev>.
1947 * It returns -EINVAL in the following cases.
1948 * - <slave_dev> is not found in the list.
1949 * - There is not active slave now.
1950 * - <slave_dev> is already active.
1951 * - The link state of <slave_dev> is not BOND_LINK_UP.
1952 * - <slave_dev> is not running.
1953 * In these cases, this fuction does nothing.
1954 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1955 */
1956static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1957{
1958 struct bonding *bond = bond_dev->priv;
1959 struct slave *old_active = NULL;
1960 struct slave *new_active = NULL;
1961 int res = 0;
1962
1963 if (!USES_PRIMARY(bond->params.mode)) {
1964 return -EINVAL;
1965 }
1966
1967 /* Verify that master_dev is indeed the master of slave_dev */
1968 if (!(slave_dev->flags & IFF_SLAVE) ||
1969 (slave_dev->master != bond_dev)) {
1970 return -EINVAL;
1971 }
1972
1973 write_lock_bh(&bond->lock);
1974
1975 old_active = bond->curr_active_slave;
1976 new_active = bond_get_slave_by_dev(bond, slave_dev);
1977
1978 /*
1979 * Changing to the current active: do nothing; return success.
1980 */
1981 if (new_active && (new_active == old_active)) {
1982 write_unlock_bh(&bond->lock);
1983 return 0;
1984 }
1985
1986 if ((new_active) &&
1987 (old_active) &&
1988 (new_active->link == BOND_LINK_UP) &&
1989 IS_UP(new_active->dev)) {
1990 bond_change_active_slave(bond, new_active);
1991 } else {
1992 res = -EINVAL;
1993 }
1994
1995 write_unlock_bh(&bond->lock);
1996
1997 return res;
1998}
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2001{
2002 struct bonding *bond = bond_dev->priv;
2003
2004 info->bond_mode = bond->params.mode;
2005 info->miimon = bond->params.miimon;
2006
2007 read_lock_bh(&bond->lock);
2008 info->num_slaves = bond->slave_cnt;
2009 read_unlock_bh(&bond->lock);
2010
2011 return 0;
2012}
2013
2014static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2015{
2016 struct bonding *bond = bond_dev->priv;
2017 struct slave *slave;
2018 int i, found = 0;
2019
2020 if (info->slave_id < 0) {
2021 return -ENODEV;
2022 }
2023
2024 read_lock_bh(&bond->lock);
2025
2026 bond_for_each_slave(bond, slave, i) {
2027 if (i == (int)info->slave_id) {
2028 found = 1;
2029 break;
2030 }
2031 }
2032
2033 read_unlock_bh(&bond->lock);
2034
2035 if (found) {
2036 strcpy(info->slave_name, slave->dev->name);
2037 info->link = slave->link;
2038 info->state = slave->state;
2039 info->link_failure_count = slave->link_failure_count;
2040 } else {
2041 return -ENODEV;
2042 }
2043
2044 return 0;
2045}
2046
2047/*-------------------------------- Monitoring -------------------------------*/
2048
2049/* this function is called regularly to monitor each slave's link. */
Mitch Williamsa77b5322005-11-09 10:35:51 -08002050void bond_mii_monitor(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 struct bonding *bond = bond_dev->priv;
2053 struct slave *slave, *oldcurrent;
2054 int do_failover = 0;
2055 int delta_in_ticks;
2056 int i;
2057
2058 read_lock(&bond->lock);
2059
2060 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2061
2062 if (bond->kill_timers) {
2063 goto out;
2064 }
2065
2066 if (bond->slave_cnt == 0) {
2067 goto re_arm;
2068 }
2069
2070 /* we will try to read the link status of each of our slaves, and
2071 * set their IFF_RUNNING flag appropriately. For each slave not
2072 * supporting MII status, we won't do anything so that a user-space
2073 * program could monitor the link itself if needed.
2074 */
2075
2076 read_lock(&bond->curr_slave_lock);
2077 oldcurrent = bond->curr_active_slave;
2078 read_unlock(&bond->curr_slave_lock);
2079
2080 bond_for_each_slave(bond, slave, i) {
2081 struct net_device *slave_dev = slave->dev;
2082 int link_state;
2083 u16 old_speed = slave->speed;
2084 u8 old_duplex = slave->duplex;
2085
2086 link_state = bond_check_dev_link(bond, slave_dev, 0);
2087
2088 switch (slave->link) {
2089 case BOND_LINK_UP: /* the link was up */
2090 if (link_state == BMSR_LSTATUS) {
2091 /* link stays up, nothing more to do */
2092 break;
2093 } else { /* link going down */
2094 slave->link = BOND_LINK_FAIL;
2095 slave->delay = bond->params.downdelay;
2096
2097 if (slave->link_failure_count < UINT_MAX) {
2098 slave->link_failure_count++;
2099 }
2100
2101 if (bond->params.downdelay) {
2102 printk(KERN_INFO DRV_NAME
2103 ": %s: link status down for %s "
2104 "interface %s, disabling it in "
2105 "%d ms.\n",
2106 bond_dev->name,
2107 IS_UP(slave_dev)
2108 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2109 ? ((slave == oldcurrent)
2110 ? "active " : "backup ")
2111 : "")
2112 : "idle ",
2113 slave_dev->name,
2114 bond->params.downdelay * bond->params.miimon);
2115 }
2116 }
2117 /* no break ! fall through the BOND_LINK_FAIL test to
2118 ensure proper action to be taken
2119 */
2120 case BOND_LINK_FAIL: /* the link has just gone down */
2121 if (link_state != BMSR_LSTATUS) {
2122 /* link stays down */
2123 if (slave->delay <= 0) {
2124 /* link down for too long time */
2125 slave->link = BOND_LINK_DOWN;
2126
2127 /* in active/backup mode, we must
2128 * completely disable this interface
2129 */
2130 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2131 (bond->params.mode == BOND_MODE_8023AD)) {
2132 bond_set_slave_inactive_flags(slave);
2133 }
2134
2135 printk(KERN_INFO DRV_NAME
2136 ": %s: link status definitely "
2137 "down for interface %s, "
2138 "disabling it\n",
2139 bond_dev->name,
2140 slave_dev->name);
2141
2142 /* notify ad that the link status has changed */
2143 if (bond->params.mode == BOND_MODE_8023AD) {
2144 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2145 }
2146
2147 if ((bond->params.mode == BOND_MODE_TLB) ||
2148 (bond->params.mode == BOND_MODE_ALB)) {
2149 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2150 }
2151
2152 if (slave == oldcurrent) {
2153 do_failover = 1;
2154 }
2155 } else {
2156 slave->delay--;
2157 }
2158 } else {
2159 /* link up again */
2160 slave->link = BOND_LINK_UP;
2161 slave->jiffies = jiffies;
2162 printk(KERN_INFO DRV_NAME
2163 ": %s: link status up again after %d "
2164 "ms for interface %s.\n",
2165 bond_dev->name,
2166 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2167 slave_dev->name);
2168 }
2169 break;
2170 case BOND_LINK_DOWN: /* the link was down */
2171 if (link_state != BMSR_LSTATUS) {
2172 /* the link stays down, nothing more to do */
2173 break;
2174 } else { /* link going up */
2175 slave->link = BOND_LINK_BACK;
2176 slave->delay = bond->params.updelay;
2177
2178 if (bond->params.updelay) {
2179 /* if updelay == 0, no need to
2180 advertise about a 0 ms delay */
2181 printk(KERN_INFO DRV_NAME
2182 ": %s: link status up for "
2183 "interface %s, enabling it "
2184 "in %d ms.\n",
2185 bond_dev->name,
2186 slave_dev->name,
2187 bond->params.updelay * bond->params.miimon);
2188 }
2189 }
2190 /* no break ! fall through the BOND_LINK_BACK state in
2191 case there's something to do.
2192 */
2193 case BOND_LINK_BACK: /* the link has just come back */
2194 if (link_state != BMSR_LSTATUS) {
2195 /* link down again */
2196 slave->link = BOND_LINK_DOWN;
2197
2198 printk(KERN_INFO DRV_NAME
2199 ": %s: link status down again after %d "
2200 "ms for interface %s.\n",
2201 bond_dev->name,
2202 (bond->params.updelay - slave->delay) * bond->params.miimon,
2203 slave_dev->name);
2204 } else {
2205 /* link stays up */
2206 if (slave->delay == 0) {
2207 /* now the link has been up for long time enough */
2208 slave->link = BOND_LINK_UP;
2209 slave->jiffies = jiffies;
2210
2211 if (bond->params.mode == BOND_MODE_8023AD) {
2212 /* prevent it from being the active one */
2213 slave->state = BOND_STATE_BACKUP;
2214 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2215 /* make it immediately active */
2216 slave->state = BOND_STATE_ACTIVE;
2217 } else if (slave != bond->primary_slave) {
2218 /* prevent it from being the active one */
2219 slave->state = BOND_STATE_BACKUP;
2220 }
2221
2222 printk(KERN_INFO DRV_NAME
2223 ": %s: link status definitely "
2224 "up for interface %s.\n",
2225 bond_dev->name,
2226 slave_dev->name);
2227
2228 /* notify ad that the link status has changed */
2229 if (bond->params.mode == BOND_MODE_8023AD) {
2230 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2231 }
2232
2233 if ((bond->params.mode == BOND_MODE_TLB) ||
2234 (bond->params.mode == BOND_MODE_ALB)) {
2235 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2236 }
2237
2238 if ((!oldcurrent) ||
2239 (slave == bond->primary_slave)) {
2240 do_failover = 1;
2241 }
2242 } else {
2243 slave->delay--;
2244 }
2245 }
2246 break;
2247 default:
2248 /* Should not happen */
Mitch Williams4e0952c2005-11-09 10:34:57 -08002249 printk(KERN_ERR DRV_NAME
2250 ": %s: Error: %s Illegal value (link=%d)\n",
2251 bond_dev->name,
2252 slave->dev->name,
2253 slave->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto out;
2255 } /* end of switch (slave->link) */
2256
2257 bond_update_speed_duplex(slave);
2258
2259 if (bond->params.mode == BOND_MODE_8023AD) {
2260 if (old_speed != slave->speed) {
2261 bond_3ad_adapter_speed_changed(slave);
2262 }
2263
2264 if (old_duplex != slave->duplex) {
2265 bond_3ad_adapter_duplex_changed(slave);
2266 }
2267 }
2268
2269 } /* end of for */
2270
2271 if (do_failover) {
2272 write_lock(&bond->curr_slave_lock);
2273
2274 bond_select_active_slave(bond);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 write_unlock(&bond->curr_slave_lock);
Jay Vosburghff59c452006-03-27 13:27:43 -08002277 } else
2278 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280re_arm:
2281 if (bond->params.miimon) {
2282 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2283 }
2284out:
2285 read_unlock(&bond->lock);
2286}
2287
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002288
Al Virod3bb52b2007-08-22 20:06:58 -04002289static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002290{
2291 struct in_device *idev;
2292 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002293 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002294
2295 if (!dev)
2296 return 0;
2297
2298 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002299 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002300 if (!idev)
2301 goto out;
2302
2303 ifa = idev->ifa_list;
2304 if (!ifa)
2305 goto out;
2306
2307 addr = ifa->ifa_local;
2308out:
2309 rcu_read_unlock();
2310 return addr;
2311}
2312
2313static int bond_has_ip(struct bonding *bond)
2314{
2315 struct vlan_entry *vlan, *vlan_next;
2316
2317 if (bond->master_ip)
2318 return 1;
2319
2320 if (list_empty(&bond->vlan_list))
2321 return 0;
2322
2323 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2324 vlan_list) {
2325 if (vlan->vlan_ip)
2326 return 1;
2327 }
2328
2329 return 0;
2330}
2331
Al Virod3bb52b2007-08-22 20:06:58 -04002332static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002333{
2334 struct vlan_entry *vlan, *vlan_next;
2335
2336 if (ip == bond->master_ip)
2337 return 1;
2338
2339 if (list_empty(&bond->vlan_list))
2340 return 0;
2341
2342 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2343 vlan_list) {
2344 if (ip == vlan->vlan_ip)
2345 return 1;
2346 }
2347
2348 return 0;
2349}
2350
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002351/*
2352 * We go to the (large) trouble of VLAN tagging ARP frames because
2353 * switches in VLAN mode (especially if ports are configured as
2354 * "native" to a VLAN) might not pass non-tagged frames.
2355 */
Al Virod3bb52b2007-08-22 20:06:58 -04002356static 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 -04002357{
2358 struct sk_buff *skb;
2359
2360 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2361 slave_dev->name, dest_ip, src_ip, vlan_id);
2362
2363 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2364 NULL, slave_dev->dev_addr, NULL);
2365
2366 if (!skb) {
2367 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2368 return;
2369 }
2370 if (vlan_id) {
2371 skb = vlan_put_tag(skb, vlan_id);
2372 if (!skb) {
2373 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2374 return;
2375 }
2376 }
2377 arp_xmit(skb);
2378}
2379
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2382{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002383 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002384 __be32 *targets = bond->params.arp_targets;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002385 struct vlan_entry *vlan, *vlan_next;
2386 struct net_device *vlan_dev;
2387 struct flowi fl;
2388 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Mitch Williams6b780562005-11-09 10:36:19 -08002390 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2391 if (!targets[i])
2392 continue;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002393 dprintk("basa: target %x\n", targets[i]);
2394 if (list_empty(&bond->vlan_list)) {
2395 dprintk("basa: empty vlan: arp_send\n");
2396 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2397 bond->master_ip, 0);
2398 continue;
2399 }
2400
2401 /*
2402 * If VLANs are configured, we do a route lookup to
2403 * determine which VLAN interface would be used, so we
2404 * can tag the ARP with the proper VLAN tag.
2405 */
2406 memset(&fl, 0, sizeof(fl));
2407 fl.fl4_dst = targets[i];
2408 fl.fl4_tos = RTO_ONLINK;
2409
2410 rv = ip_route_output_key(&rt, &fl);
2411 if (rv) {
2412 if (net_ratelimit()) {
2413 printk(KERN_WARNING DRV_NAME
2414 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2415 bond->dev->name, NIPQUAD(fl.fl4_dst));
2416 }
2417 continue;
2418 }
2419
2420 /*
2421 * This target is not on a VLAN
2422 */
2423 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002424 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002425 dprintk("basa: rtdev == bond->dev: arp_send\n");
2426 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2427 bond->master_ip, 0);
2428 continue;
2429 }
2430
2431 vlan_id = 0;
2432 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2433 vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002434 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002435 if (vlan_dev == rt->u.dst.dev) {
2436 vlan_id = vlan->vlan_id;
2437 dprintk("basa: vlan match on %s %d\n",
2438 vlan_dev->name, vlan_id);
2439 break;
2440 }
2441 }
2442
2443 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002444 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002445 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2446 vlan->vlan_ip, vlan_id);
2447 continue;
2448 }
2449
2450 if (net_ratelimit()) {
2451 printk(KERN_WARNING DRV_NAME
2452 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2453 bond->dev->name, NIPQUAD(fl.fl4_dst),
2454 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2455 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002456 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002457 }
2458}
2459
2460/*
2461 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2462 * for each VLAN above us.
2463 */
2464static void bond_send_gratuitous_arp(struct bonding *bond)
2465{
2466 struct slave *slave = bond->curr_active_slave;
2467 struct vlan_entry *vlan;
2468 struct net_device *vlan_dev;
2469
2470 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2471 slave ? slave->dev->name : "NULL");
2472 if (!slave)
2473 return;
2474
2475 if (bond->master_ip) {
2476 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2477 bond->master_ip, 0);
2478 }
2479
2480 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002481 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002482 if (vlan->vlan_ip) {
2483 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2484 vlan->vlan_ip, vlan->vlan_id);
2485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
2487}
2488
Al Virod3bb52b2007-08-22 20:06:58 -04002489static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002490{
2491 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002492 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002493
2494 targets = bond->params.arp_targets;
2495 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2496 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2497 "%u.%u.%u.%u bhti(tip) %d\n",
2498 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2499 bond_has_this_ip(bond, tip));
2500 if (sip == targets[i]) {
2501 if (bond_has_this_ip(bond, tip))
2502 slave->last_arp_rx = jiffies;
2503 return;
2504 }
2505 }
2506}
2507
2508static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2509{
2510 struct arphdr *arp;
2511 struct slave *slave;
2512 struct bonding *bond;
2513 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002514 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002515
Eric W. Biedermane730c152007-09-17 11:53:39 -07002516 if (dev->nd_net != &init_net)
2517 goto out;
2518
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002519 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2520 goto out;
2521
2522 bond = dev->priv;
2523 read_lock(&bond->lock);
2524
2525 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2526 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2527 orig_dev ? orig_dev->name : "NULL");
2528
2529 slave = bond_get_slave_by_dev(bond, orig_dev);
2530 if (!slave || !slave_do_arp_validate(bond, slave))
2531 goto out_unlock;
2532
2533 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2534 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2535 (2 * dev->addr_len) +
2536 (2 * sizeof(u32)))))
2537 goto out_unlock;
2538
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002539 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002540 if (arp->ar_hln != dev->addr_len ||
2541 skb->pkt_type == PACKET_OTHERHOST ||
2542 skb->pkt_type == PACKET_LOOPBACK ||
2543 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2544 arp->ar_pro != htons(ETH_P_IP) ||
2545 arp->ar_pln != 4)
2546 goto out_unlock;
2547
2548 arp_ptr = (unsigned char *)(arp + 1);
2549 arp_ptr += dev->addr_len;
2550 memcpy(&sip, arp_ptr, 4);
2551 arp_ptr += 4 + dev->addr_len;
2552 memcpy(&tip, arp_ptr, 4);
2553
2554 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2555 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2556 slave->state, bond->params.arp_validate,
2557 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2558
2559 /*
2560 * Backup slaves won't see the ARP reply, but do come through
2561 * here for each ARP probe (so we swap the sip/tip to validate
2562 * the probe). In a "redundant switch, common router" type of
2563 * configuration, the ARP probe will (hopefully) travel from
2564 * the active, through one switch, the router, then the other
2565 * switch before reaching the backup.
2566 */
2567 if (slave->state == BOND_STATE_ACTIVE)
2568 bond_validate_arp(bond, slave, sip, tip);
2569 else
2570 bond_validate_arp(bond, slave, tip, sip);
2571
2572out_unlock:
2573 read_unlock(&bond->lock);
2574out:
2575 dev_kfree_skb(skb);
2576 return NET_RX_SUCCESS;
2577}
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/*
2580 * this function is called regularly to monitor each slave's link
2581 * ensuring that traffic is being sent and received when arp monitoring
2582 * is used in load-balancing mode. if the adapter has been dormant, then an
2583 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2584 * arp monitoring in active backup mode.
2585 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08002586void bond_loadbalance_arp_mon(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 struct bonding *bond = bond_dev->priv;
2589 struct slave *slave, *oldcurrent;
2590 int do_failover = 0;
2591 int delta_in_ticks;
2592 int i;
2593
2594 read_lock(&bond->lock);
2595
2596 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2597
2598 if (bond->kill_timers) {
2599 goto out;
2600 }
2601
2602 if (bond->slave_cnt == 0) {
2603 goto re_arm;
2604 }
2605
2606 read_lock(&bond->curr_slave_lock);
2607 oldcurrent = bond->curr_active_slave;
2608 read_unlock(&bond->curr_slave_lock);
2609
2610 /* see if any of the previous devices are up now (i.e. they have
2611 * xmt and rcv traffic). the curr_active_slave does not come into
2612 * the picture unless it is null. also, slave->jiffies is not needed
2613 * here because we send an arp on each slave and give a slave as
2614 * long as it needs to get the tx/rx within the delta.
2615 * TODO: what about up/down delay in arp mode? it wasn't here before
2616 * so it can wait
2617 */
2618 bond_for_each_slave(bond, slave, i) {
2619 if (slave->link != BOND_LINK_UP) {
2620 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2621 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2622
2623 slave->link = BOND_LINK_UP;
2624 slave->state = BOND_STATE_ACTIVE;
2625
2626 /* primary_slave has no meaning in round-robin
2627 * mode. the window of a slave being up and
2628 * curr_active_slave being null after enslaving
2629 * is closed.
2630 */
2631 if (!oldcurrent) {
2632 printk(KERN_INFO DRV_NAME
2633 ": %s: link status definitely "
2634 "up for interface %s, ",
2635 bond_dev->name,
2636 slave->dev->name);
2637 do_failover = 1;
2638 } else {
2639 printk(KERN_INFO DRV_NAME
2640 ": %s: interface %s is now up\n",
2641 bond_dev->name,
2642 slave->dev->name);
2643 }
2644 }
2645 } else {
2646 /* slave->link == BOND_LINK_UP */
2647
2648 /* not all switches will respond to an arp request
2649 * when the source ip is 0, so don't take the link down
2650 * if we don't know our ip yet
2651 */
2652 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2653 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002654 bond_has_ip(bond))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656 slave->link = BOND_LINK_DOWN;
2657 slave->state = BOND_STATE_BACKUP;
2658
2659 if (slave->link_failure_count < UINT_MAX) {
2660 slave->link_failure_count++;
2661 }
2662
2663 printk(KERN_INFO DRV_NAME
2664 ": %s: interface %s is now down.\n",
2665 bond_dev->name,
2666 slave->dev->name);
2667
2668 if (slave == oldcurrent) {
2669 do_failover = 1;
2670 }
2671 }
2672 }
2673
2674 /* note: if switch is in round-robin mode, all links
2675 * must tx arp to ensure all links rx an arp - otherwise
2676 * links may oscillate or not come up at all; if switch is
2677 * in something like xor mode, there is nothing we can
2678 * do - all replies will be rx'ed on same link causing slaves
2679 * to be unstable during low/no traffic periods
2680 */
2681 if (IS_UP(slave->dev)) {
2682 bond_arp_send_all(bond, slave);
2683 }
2684 }
2685
2686 if (do_failover) {
2687 write_lock(&bond->curr_slave_lock);
2688
2689 bond_select_active_slave(bond);
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 write_unlock(&bond->curr_slave_lock);
2692 }
2693
2694re_arm:
2695 if (bond->params.arp_interval) {
2696 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2697 }
2698out:
2699 read_unlock(&bond->lock);
2700}
2701
2702/*
2703 * When using arp monitoring in active-backup mode, this function is
2704 * called to determine if any backup slaves have went down or a new
2705 * current slave needs to be found.
2706 * The backup slaves never generate traffic, they are considered up by merely
2707 * receiving traffic. If the current slave goes down, each backup slave will
2708 * be given the opportunity to tx/rx an arp before being taken down - this
2709 * prevents all slaves from being taken down due to the current slave not
2710 * sending any traffic for the backups to receive. The arps are not necessarily
2711 * necessary, any tx and rx traffic will keep the current slave up. While any
2712 * rx traffic will keep the backup slaves up, the current slave is responsible
2713 * for generating traffic to keep them up regardless of any other traffic they
2714 * may have received.
2715 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2716 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08002717void bond_activebackup_arp_mon(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 struct bonding *bond = bond_dev->priv;
2720 struct slave *slave;
2721 int delta_in_ticks;
2722 int i;
2723
2724 read_lock(&bond->lock);
2725
2726 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2727
2728 if (bond->kill_timers) {
2729 goto out;
2730 }
2731
2732 if (bond->slave_cnt == 0) {
2733 goto re_arm;
2734 }
2735
2736 /* determine if any slave has come up or any backup slave has
2737 * gone down
2738 * TODO: what about up/down delay in arp mode? it wasn't here before
2739 * so it can wait
2740 */
2741 bond_for_each_slave(bond, slave, i) {
2742 if (slave->link != BOND_LINK_UP) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002743 if ((jiffies - slave_last_rx(bond, slave)) <=
2744 delta_in_ticks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 slave->link = BOND_LINK_UP;
2747
2748 write_lock(&bond->curr_slave_lock);
2749
2750 if ((!bond->curr_active_slave) &&
2751 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2752 bond_change_active_slave(bond, slave);
2753 bond->current_arp_slave = NULL;
2754 } else if (bond->curr_active_slave != slave) {
2755 /* this slave has just come up but we
2756 * already have a current slave; this
2757 * can also happen if bond_enslave adds
2758 * a new slave that is up while we are
2759 * searching for a new slave
2760 */
2761 bond_set_slave_inactive_flags(slave);
2762 bond->current_arp_slave = NULL;
2763 }
2764
Jay Vosburghff59c452006-03-27 13:27:43 -08002765 bond_set_carrier(bond);
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (slave == bond->curr_active_slave) {
2768 printk(KERN_INFO DRV_NAME
2769 ": %s: %s is up and now the "
2770 "active interface\n",
2771 bond_dev->name,
2772 slave->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08002773 netif_carrier_on(bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 } else {
2775 printk(KERN_INFO DRV_NAME
2776 ": %s: backup interface %s is "
2777 "now up\n",
2778 bond_dev->name,
2779 slave->dev->name);
2780 }
2781
2782 write_unlock(&bond->curr_slave_lock);
2783 }
2784 } else {
2785 read_lock(&bond->curr_slave_lock);
2786
2787 if ((slave != bond->curr_active_slave) &&
2788 (!bond->current_arp_slave) &&
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002789 (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002790 bond_has_ip(bond))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /* a backup slave has gone down; three times
2792 * the delta allows the current slave to be
2793 * taken out before the backup slave.
2794 * note: a non-null current_arp_slave indicates
2795 * the curr_active_slave went down and we are
2796 * searching for a new one; under this
2797 * condition we only take the curr_active_slave
2798 * down - this gives each slave a chance to
2799 * tx/rx traffic before being taken out
2800 */
2801
2802 read_unlock(&bond->curr_slave_lock);
2803
2804 slave->link = BOND_LINK_DOWN;
2805
2806 if (slave->link_failure_count < UINT_MAX) {
2807 slave->link_failure_count++;
2808 }
2809
2810 bond_set_slave_inactive_flags(slave);
2811
2812 printk(KERN_INFO DRV_NAME
2813 ": %s: backup interface %s is now down\n",
2814 bond_dev->name,
2815 slave->dev->name);
2816 } else {
2817 read_unlock(&bond->curr_slave_lock);
2818 }
2819 }
2820 }
2821
2822 read_lock(&bond->curr_slave_lock);
2823 slave = bond->curr_active_slave;
2824 read_unlock(&bond->curr_slave_lock);
2825
2826 if (slave) {
2827 /* if we have sent traffic in the past 2*arp_intervals but
2828 * haven't xmit and rx traffic in that time interval, select
2829 * a different slave. slave->jiffies is only updated when
2830 * a slave first becomes the curr_active_slave - not necessarily
2831 * after every arp; this ensures the slave has a full 2*delta
2832 * before being taken out. if a primary is being used, check
2833 * if it is up and needs to take over as the curr_active_slave
2834 */
2835 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002836 (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002837 bond_has_ip(bond))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2839
2840 slave->link = BOND_LINK_DOWN;
2841
2842 if (slave->link_failure_count < UINT_MAX) {
2843 slave->link_failure_count++;
2844 }
2845
2846 printk(KERN_INFO DRV_NAME
2847 ": %s: link status down for active interface "
2848 "%s, disabling it\n",
2849 bond_dev->name,
2850 slave->dev->name);
2851
2852 write_lock(&bond->curr_slave_lock);
2853
2854 bond_select_active_slave(bond);
2855 slave = bond->curr_active_slave;
2856
2857 write_unlock(&bond->curr_slave_lock);
2858
2859 bond->current_arp_slave = slave;
2860
2861 if (slave) {
2862 slave->jiffies = jiffies;
2863 }
2864 } else if ((bond->primary_slave) &&
2865 (bond->primary_slave != slave) &&
2866 (bond->primary_slave->link == BOND_LINK_UP)) {
2867 /* at this point, slave is the curr_active_slave */
2868 printk(KERN_INFO DRV_NAME
2869 ": %s: changing from interface %s to primary "
2870 "interface %s\n",
2871 bond_dev->name,
2872 slave->dev->name,
2873 bond->primary_slave->dev->name);
2874
2875 /* primary is up so switch to it */
2876 write_lock(&bond->curr_slave_lock);
2877 bond_change_active_slave(bond, bond->primary_slave);
2878 write_unlock(&bond->curr_slave_lock);
2879
2880 slave = bond->primary_slave;
2881 slave->jiffies = jiffies;
2882 } else {
2883 bond->current_arp_slave = NULL;
2884 }
2885
2886 /* the current slave must tx an arp to ensure backup slaves
2887 * rx traffic
2888 */
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002889 if (slave && bond_has_ip(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 bond_arp_send_all(bond, slave);
2891 }
2892 }
2893
2894 /* if we don't have a curr_active_slave, search for the next available
2895 * backup slave from the current_arp_slave and make it the candidate
2896 * for becoming the curr_active_slave
2897 */
2898 if (!slave) {
2899 if (!bond->current_arp_slave) {
2900 bond->current_arp_slave = bond->first_slave;
2901 }
2902
2903 if (bond->current_arp_slave) {
2904 bond_set_slave_inactive_flags(bond->current_arp_slave);
2905
2906 /* search for next candidate */
Jay Vosburgh2f872f02005-05-26 12:56:59 -07002907 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (IS_UP(slave->dev)) {
2909 slave->link = BOND_LINK_BACK;
2910 bond_set_slave_active_flags(slave);
2911 bond_arp_send_all(bond, slave);
2912 slave->jiffies = jiffies;
2913 bond->current_arp_slave = slave;
2914 break;
2915 }
2916
2917 /* if the link state is up at this point, we
2918 * mark it down - this can happen if we have
2919 * simultaneous link failures and
2920 * reselect_active_interface doesn't make this
2921 * one the current slave so it is still marked
2922 * up when it is actually down
2923 */
2924 if (slave->link == BOND_LINK_UP) {
2925 slave->link = BOND_LINK_DOWN;
2926 if (slave->link_failure_count < UINT_MAX) {
2927 slave->link_failure_count++;
2928 }
2929
2930 bond_set_slave_inactive_flags(slave);
2931
2932 printk(KERN_INFO DRV_NAME
2933 ": %s: backup interface %s is "
2934 "now down.\n",
2935 bond_dev->name,
2936 slave->dev->name);
2937 }
2938 }
2939 }
2940 }
2941
2942re_arm:
2943 if (bond->params.arp_interval) {
2944 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2945 }
2946out:
2947 read_unlock(&bond->lock);
2948}
2949
2950/*------------------------------ proc/seq_file-------------------------------*/
2951
2952#ifdef CONFIG_PROC_FS
2953
2954#define SEQ_START_TOKEN ((void *)1)
2955
2956static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
2957{
2958 struct bonding *bond = seq->private;
2959 loff_t off = 0;
2960 struct slave *slave;
2961 int i;
2962
2963 /* make sure the bond won't be taken away */
2964 read_lock(&dev_base_lock);
2965 read_lock_bh(&bond->lock);
2966
2967 if (*pos == 0) {
2968 return SEQ_START_TOKEN;
2969 }
2970
2971 bond_for_each_slave(bond, slave, i) {
2972 if (++off == *pos) {
2973 return slave;
2974 }
2975 }
2976
2977 return NULL;
2978}
2979
2980static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2981{
2982 struct bonding *bond = seq->private;
2983 struct slave *slave = v;
2984
2985 ++*pos;
2986 if (v == SEQ_START_TOKEN) {
2987 return bond->first_slave;
2988 }
2989
2990 slave = slave->next;
2991
2992 return (slave == bond->first_slave) ? NULL : slave;
2993}
2994
2995static void bond_info_seq_stop(struct seq_file *seq, void *v)
2996{
2997 struct bonding *bond = seq->private;
2998
2999 read_unlock_bh(&bond->lock);
3000 read_unlock(&dev_base_lock);
3001}
3002
3003static void bond_info_show_master(struct seq_file *seq)
3004{
3005 struct bonding *bond = seq->private;
3006 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003007 int i;
3008 u32 target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 read_lock(&bond->curr_slave_lock);
3011 curr = bond->curr_active_slave;
3012 read_unlock(&bond->curr_slave_lock);
3013
3014 seq_printf(seq, "Bonding Mode: %s\n",
3015 bond_mode_name(bond->params.mode));
3016
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003017 if (bond->params.mode == BOND_MODE_XOR ||
3018 bond->params.mode == BOND_MODE_8023AD) {
3019 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3020 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3021 bond->params.xmit_policy);
3022 }
3023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (USES_PRIMARY(bond->params.mode)) {
3025 seq_printf(seq, "Primary Slave: %s\n",
Mitch Williams0f418b22005-11-09 10:35:21 -08003026 (bond->primary_slave) ?
3027 bond->primary_slave->dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 seq_printf(seq, "Currently Active Slave: %s\n",
3030 (curr) ? curr->dev->name : "None");
3031 }
3032
Jay Vosburghff59c452006-03-27 13:27:43 -08003033 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3034 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3036 seq_printf(seq, "Up Delay (ms): %d\n",
3037 bond->params.updelay * bond->params.miimon);
3038 seq_printf(seq, "Down Delay (ms): %d\n",
3039 bond->params.downdelay * bond->params.miimon);
3040
Mitch Williams4756b022005-11-09 10:36:25 -08003041
3042 /* ARP information */
3043 if(bond->params.arp_interval > 0) {
3044 int printed=0;
3045 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3046 bond->params.arp_interval);
3047
3048 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3049
3050 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3051 if (!bond->params.arp_targets[i])
3052 continue;
3053 if (printed)
3054 seq_printf(seq, ",");
3055 target = ntohl(bond->params.arp_targets[i]);
3056 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3057 printed = 1;
3058 }
3059 seq_printf(seq, "\n");
3060 }
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 if (bond->params.mode == BOND_MODE_8023AD) {
3063 struct ad_info ad_info;
Joe Perches0795af52007-10-03 17:59:30 -07003064 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 seq_puts(seq, "\n802.3ad info\n");
3067 seq_printf(seq, "LACP rate: %s\n",
3068 (bond->params.lacp_fast) ? "fast" : "slow");
3069
3070 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3071 seq_printf(seq, "bond %s has no active aggregator\n",
3072 bond->dev->name);
3073 } else {
3074 seq_printf(seq, "Active Aggregator Info:\n");
3075
3076 seq_printf(seq, "\tAggregator ID: %d\n",
3077 ad_info.aggregator_id);
3078 seq_printf(seq, "\tNumber of ports: %d\n",
3079 ad_info.ports);
3080 seq_printf(seq, "\tActor Key: %d\n",
3081 ad_info.actor_key);
3082 seq_printf(seq, "\tPartner Key: %d\n",
3083 ad_info.partner_key);
Joe Perches0795af52007-10-03 17:59:30 -07003084 seq_printf(seq, "\tPartner Mac Address: %s\n",
3085 print_mac(mac, ad_info.partner_system));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087 }
3088}
3089
3090static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3091{
3092 struct bonding *bond = seq->private;
Joe Perches0795af52007-10-03 17:59:30 -07003093 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3096 seq_printf(seq, "MII Status: %s\n",
3097 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003098 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 slave->link_failure_count);
3100
Jay Vosburgh217df672005-09-26 16:11:50 -07003101 seq_printf(seq,
Joe Perches0795af52007-10-03 17:59:30 -07003102 "Permanent HW addr: %s\n",
3103 print_mac(mac, slave->perm_hwaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 if (bond->params.mode == BOND_MODE_8023AD) {
3106 const struct aggregator *agg
3107 = SLAVE_AD_INFO(slave).port.aggregator;
3108
3109 if (agg) {
3110 seq_printf(seq, "Aggregator ID: %d\n",
3111 agg->aggregator_identifier);
3112 } else {
3113 seq_puts(seq, "Aggregator ID: N/A\n");
3114 }
3115 }
3116}
3117
3118static int bond_info_seq_show(struct seq_file *seq, void *v)
3119{
3120 if (v == SEQ_START_TOKEN) {
3121 seq_printf(seq, "%s\n", version);
3122 bond_info_show_master(seq);
3123 } else {
3124 bond_info_show_slave(seq, v);
3125 }
3126
3127 return 0;
3128}
3129
3130static struct seq_operations bond_info_seq_ops = {
3131 .start = bond_info_seq_start,
3132 .next = bond_info_seq_next,
3133 .stop = bond_info_seq_stop,
3134 .show = bond_info_seq_show,
3135};
3136
3137static int bond_info_open(struct inode *inode, struct file *file)
3138{
3139 struct seq_file *seq;
3140 struct proc_dir_entry *proc;
3141 int res;
3142
3143 res = seq_open(file, &bond_info_seq_ops);
3144 if (!res) {
3145 /* recover the pointer buried in proc_dir_entry data */
3146 seq = file->private_data;
3147 proc = PDE(inode);
3148 seq->private = proc->data;
3149 }
3150
3151 return res;
3152}
3153
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003154static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 .owner = THIS_MODULE,
3156 .open = bond_info_open,
3157 .read = seq_read,
3158 .llseek = seq_lseek,
3159 .release = seq_release,
3160};
3161
3162static int bond_create_proc_entry(struct bonding *bond)
3163{
3164 struct net_device *bond_dev = bond->dev;
3165
3166 if (bond_proc_dir) {
3167 bond->proc_entry = create_proc_entry(bond_dev->name,
3168 S_IRUGO,
3169 bond_proc_dir);
3170 if (bond->proc_entry == NULL) {
3171 printk(KERN_WARNING DRV_NAME
3172 ": Warning: Cannot create /proc/net/%s/%s\n",
3173 DRV_NAME, bond_dev->name);
3174 } else {
3175 bond->proc_entry->data = bond;
3176 bond->proc_entry->proc_fops = &bond_info_fops;
3177 bond->proc_entry->owner = THIS_MODULE;
3178 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3179 }
3180 }
3181
3182 return 0;
3183}
3184
3185static void bond_remove_proc_entry(struct bonding *bond)
3186{
3187 if (bond_proc_dir && bond->proc_entry) {
3188 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3189 memset(bond->proc_file_name, 0, IFNAMSIZ);
3190 bond->proc_entry = NULL;
3191 }
3192}
3193
3194/* Create the bonding directory under /proc/net, if doesn't exist yet.
3195 * Caller must hold rtnl_lock.
3196 */
3197static void bond_create_proc_dir(void)
3198{
3199 int len = strlen(DRV_NAME);
3200
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003201 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 bond_proc_dir = bond_proc_dir->next) {
3203 if ((bond_proc_dir->namelen == len) &&
3204 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3205 break;
3206 }
3207 }
3208
3209 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003210 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (bond_proc_dir) {
3212 bond_proc_dir->owner = THIS_MODULE;
3213 } else {
3214 printk(KERN_WARNING DRV_NAME
3215 ": Warning: cannot create /proc/net/%s\n",
3216 DRV_NAME);
3217 }
3218 }
3219}
3220
3221/* Destroy the bonding directory under /proc/net, if empty.
3222 * Caller must hold rtnl_lock.
3223 */
3224static void bond_destroy_proc_dir(void)
3225{
3226 struct proc_dir_entry *de;
3227
3228 if (!bond_proc_dir) {
3229 return;
3230 }
3231
3232 /* verify that the /proc dir is empty */
3233 for (de = bond_proc_dir->subdir; de; de = de->next) {
3234 /* ignore . and .. */
3235 if (*(de->name) != '.') {
3236 break;
3237 }
3238 }
3239
3240 if (de) {
3241 if (bond_proc_dir->owner == THIS_MODULE) {
3242 bond_proc_dir->owner = NULL;
3243 }
3244 } else {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003245 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 bond_proc_dir = NULL;
3247 }
3248}
3249#endif /* CONFIG_PROC_FS */
3250
3251/*-------------------------- netdev event handling --------------------------*/
3252
3253/*
3254 * Change device name
3255 */
3256static int bond_event_changename(struct bonding *bond)
3257{
3258#ifdef CONFIG_PROC_FS
3259 bond_remove_proc_entry(bond);
3260 bond_create_proc_entry(bond);
3261#endif
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003262 down_write(&(bonding_rwsem));
3263 bond_destroy_sysfs_entry(bond);
3264 bond_create_sysfs_entry(bond);
3265 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return NOTIFY_DONE;
3267}
3268
3269static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3270{
3271 struct bonding *event_bond = bond_dev->priv;
3272
3273 switch (event) {
3274 case NETDEV_CHANGENAME:
3275 return bond_event_changename(event_bond);
3276 case NETDEV_UNREGISTER:
3277 /*
3278 * TODO: remove a bond from the list?
3279 */
3280 break;
3281 default:
3282 break;
3283 }
3284
3285 return NOTIFY_DONE;
3286}
3287
3288static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3289{
3290 struct net_device *bond_dev = slave_dev->master;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003291 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 switch (event) {
3294 case NETDEV_UNREGISTER:
3295 if (bond_dev) {
3296 bond_release(bond_dev, slave_dev);
3297 }
3298 break;
3299 case NETDEV_CHANGE:
3300 /*
3301 * TODO: is this what we get if somebody
3302 * sets up a hierarchical bond, then rmmod's
3303 * one of the slave bonding devices?
3304 */
3305 break;
3306 case NETDEV_DOWN:
3307 /*
3308 * ... Or is it this?
3309 */
3310 break;
3311 case NETDEV_CHANGEMTU:
3312 /*
3313 * TODO: Should slaves be allowed to
3314 * independently alter their MTU? For
3315 * an active-backup bond, slaves need
3316 * not be the same type of device, so
3317 * MTUs may vary. For other modes,
3318 * slaves arguably should have the
3319 * same MTUs. To do this, we'd need to
3320 * take over the slave's change_mtu
3321 * function for the duration of their
3322 * servitude.
3323 */
3324 break;
3325 case NETDEV_CHANGENAME:
3326 /*
3327 * TODO: handle changing the primary's name
3328 */
3329 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003330 case NETDEV_FEAT_CHANGE:
3331 bond_compute_features(bond);
3332 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 default:
3334 break;
3335 }
3336
3337 return NOTIFY_DONE;
3338}
3339
3340/*
3341 * bond_netdev_event: handle netdev notifier chain events.
3342 *
3343 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003344 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 * locks for us to safely manipulate the slave devices (RTNL lock,
3346 * dev_probe_lock).
3347 */
3348static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3349{
3350 struct net_device *event_dev = (struct net_device *)ptr;
3351
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003352 if (event_dev->nd_net != &init_net)
3353 return NOTIFY_DONE;
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 dprintk("event_dev: %s, event: %lx\n",
3356 (event_dev ? event_dev->name : "None"),
3357 event);
3358
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003359 if (!(event_dev->priv_flags & IFF_BONDING))
3360 return NOTIFY_DONE;
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (event_dev->flags & IFF_MASTER) {
3363 dprintk("IFF_MASTER\n");
3364 return bond_master_netdev_event(event, event_dev);
3365 }
3366
3367 if (event_dev->flags & IFF_SLAVE) {
3368 dprintk("IFF_SLAVE\n");
3369 return bond_slave_netdev_event(event, event_dev);
3370 }
3371
3372 return NOTIFY_DONE;
3373}
3374
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003375/*
3376 * bond_inetaddr_event: handle inetaddr notifier chain events.
3377 *
3378 * We keep track of device IPs primarily to use as source addresses in
3379 * ARP monitor probes (rather than spewing out broadcasts all the time).
3380 *
3381 * We track one IP for the main device (if it has one), plus one per VLAN.
3382 */
3383static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3384{
3385 struct in_ifaddr *ifa = ptr;
3386 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3387 struct bonding *bond, *bond_next;
3388 struct vlan_entry *vlan, *vlan_next;
3389
3390 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3391 if (bond->dev == event_dev) {
3392 switch (event) {
3393 case NETDEV_UP:
3394 bond->master_ip = ifa->ifa_local;
3395 return NOTIFY_OK;
3396 case NETDEV_DOWN:
3397 bond->master_ip = bond_glean_dev_ip(bond->dev);
3398 return NOTIFY_OK;
3399 default:
3400 return NOTIFY_DONE;
3401 }
3402 }
3403
3404 if (list_empty(&bond->vlan_list))
3405 continue;
3406
3407 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3408 vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003409 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003410 if (vlan_dev == event_dev) {
3411 switch (event) {
3412 case NETDEV_UP:
3413 vlan->vlan_ip = ifa->ifa_local;
3414 return NOTIFY_OK;
3415 case NETDEV_DOWN:
3416 vlan->vlan_ip =
3417 bond_glean_dev_ip(vlan_dev);
3418 return NOTIFY_OK;
3419 default:
3420 return NOTIFY_DONE;
3421 }
3422 }
3423 }
3424 }
3425 return NOTIFY_DONE;
3426}
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428static struct notifier_block bond_netdev_notifier = {
3429 .notifier_call = bond_netdev_event,
3430};
3431
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003432static struct notifier_block bond_inetaddr_notifier = {
3433 .notifier_call = bond_inetaddr_event,
3434};
3435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436/*-------------------------- Packet type handling ---------------------------*/
3437
3438/* register to receive lacpdus on a bond */
3439static void bond_register_lacpdu(struct bonding *bond)
3440{
3441 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3442
3443 /* initialize packet type */
3444 pk_type->type = PKT_TYPE_LACPDU;
3445 pk_type->dev = bond->dev;
3446 pk_type->func = bond_3ad_lacpdu_recv;
3447
3448 dev_add_pack(pk_type);
3449}
3450
3451/* unregister to receive lacpdus on a bond */
3452static void bond_unregister_lacpdu(struct bonding *bond)
3453{
3454 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3455}
3456
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003457void bond_register_arp(struct bonding *bond)
3458{
3459 struct packet_type *pt = &bond->arp_mon_pt;
3460
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003461 if (pt->type)
3462 return;
3463
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003464 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003465 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003466 pt->func = bond_arp_rcv;
3467 dev_add_pack(pt);
3468}
3469
3470void bond_unregister_arp(struct bonding *bond)
3471{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003472 struct packet_type *pt = &bond->arp_mon_pt;
3473
3474 dev_remove_pack(pt);
3475 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003476}
3477
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003478/*---------------------------- Hashing Policies -----------------------------*/
3479
3480/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003481 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003482 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3483 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3484 */
3485static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3486 struct net_device *bond_dev, int count)
3487{
3488 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003489 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003490 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003491 int layer4_xor = 0;
3492
3493 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3494 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3495 (iph->protocol == IPPROTO_TCP ||
3496 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003497 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003498 }
3499 return (layer4_xor ^
3500 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3501
3502 }
3503
3504 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3505}
3506
3507/*
3508 * Hash for the output device based upon layer 2 data
3509 */
3510static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3511 struct net_device *bond_dev, int count)
3512{
3513 struct ethhdr *data = (struct ethhdr *)skb->data;
3514
3515 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3516}
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518/*-------------------------- Device entry points ----------------------------*/
3519
3520static int bond_open(struct net_device *bond_dev)
3521{
3522 struct bonding *bond = bond_dev->priv;
3523 struct timer_list *mii_timer = &bond->mii_timer;
3524 struct timer_list *arp_timer = &bond->arp_timer;
3525
3526 bond->kill_timers = 0;
3527
3528 if ((bond->params.mode == BOND_MODE_TLB) ||
3529 (bond->params.mode == BOND_MODE_ALB)) {
3530 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3531
3532 /* bond_alb_initialize must be called before the timer
3533 * is started.
3534 */
3535 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3536 /* something went wrong - fail the open operation */
3537 return -1;
3538 }
3539
3540 init_timer(alb_timer);
3541 alb_timer->expires = jiffies + 1;
3542 alb_timer->data = (unsigned long)bond;
3543 alb_timer->function = (void *)&bond_alb_monitor;
3544 add_timer(alb_timer);
3545 }
3546
3547 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3548 init_timer(mii_timer);
3549 mii_timer->expires = jiffies + 1;
3550 mii_timer->data = (unsigned long)bond_dev;
3551 mii_timer->function = (void *)&bond_mii_monitor;
3552 add_timer(mii_timer);
3553 }
3554
3555 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3556 init_timer(arp_timer);
3557 arp_timer->expires = jiffies + 1;
3558 arp_timer->data = (unsigned long)bond_dev;
3559 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3560 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3561 } else {
3562 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3563 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003564 if (bond->params.arp_validate)
3565 bond_register_arp(bond);
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 add_timer(arp_timer);
3568 }
3569
3570 if (bond->params.mode == BOND_MODE_8023AD) {
3571 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3572 init_timer(ad_timer);
3573 ad_timer->expires = jiffies + 1;
3574 ad_timer->data = (unsigned long)bond;
3575 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3576 add_timer(ad_timer);
3577
3578 /* register to receive LACPDUs */
3579 bond_register_lacpdu(bond);
3580 }
3581
3582 return 0;
3583}
3584
3585static int bond_close(struct net_device *bond_dev)
3586{
3587 struct bonding *bond = bond_dev->priv;
3588
3589 if (bond->params.mode == BOND_MODE_8023AD) {
3590 /* Unregister the receive of LACPDUs */
3591 bond_unregister_lacpdu(bond);
3592 }
3593
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003594 if (bond->params.arp_validate)
3595 bond_unregister_arp(bond);
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 write_lock_bh(&bond->lock);
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 /* signal timers not to re-arm */
3601 bond->kill_timers = 1;
3602
3603 write_unlock_bh(&bond->lock);
3604
3605 /* del_timer_sync must run without holding the bond->lock
3606 * because a running timer might be trying to hold it too
3607 */
3608
3609 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3610 del_timer_sync(&bond->mii_timer);
3611 }
3612
3613 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3614 del_timer_sync(&bond->arp_timer);
3615 }
3616
3617 switch (bond->params.mode) {
3618 case BOND_MODE_8023AD:
3619 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3620 break;
3621 case BOND_MODE_TLB:
3622 case BOND_MODE_ALB:
3623 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3624 break;
3625 default:
3626 break;
3627 }
3628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 if ((bond->params.mode == BOND_MODE_TLB) ||
3631 (bond->params.mode == BOND_MODE_ALB)) {
3632 /* Must be called only after all
3633 * slaves have been released
3634 */
3635 bond_alb_deinitialize(bond);
3636 }
3637
3638 return 0;
3639}
3640
3641static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3642{
3643 struct bonding *bond = bond_dev->priv;
3644 struct net_device_stats *stats = &(bond->stats), *sstats;
3645 struct slave *slave;
3646 int i;
3647
3648 memset(stats, 0, sizeof(struct net_device_stats));
3649
3650 read_lock_bh(&bond->lock);
3651
3652 bond_for_each_slave(bond, slave, i) {
Rusty Russellc45d2862007-03-28 14:29:08 -07003653 sstats = slave->dev->get_stats(slave->dev);
Rusty Russell5a1b5892007-04-28 21:04:03 -07003654 stats->rx_packets += sstats->rx_packets;
3655 stats->rx_bytes += sstats->rx_bytes;
3656 stats->rx_errors += sstats->rx_errors;
3657 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Rusty Russell5a1b5892007-04-28 21:04:03 -07003659 stats->tx_packets += sstats->tx_packets;
3660 stats->tx_bytes += sstats->tx_bytes;
3661 stats->tx_errors += sstats->tx_errors;
3662 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Rusty Russell5a1b5892007-04-28 21:04:03 -07003664 stats->multicast += sstats->multicast;
3665 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Rusty Russell5a1b5892007-04-28 21:04:03 -07003667 stats->rx_length_errors += sstats->rx_length_errors;
3668 stats->rx_over_errors += sstats->rx_over_errors;
3669 stats->rx_crc_errors += sstats->rx_crc_errors;
3670 stats->rx_frame_errors += sstats->rx_frame_errors;
3671 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3672 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Rusty Russell5a1b5892007-04-28 21:04:03 -07003674 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3675 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3676 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3677 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3678 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
3680
3681 read_unlock_bh(&bond->lock);
3682
3683 return stats;
3684}
3685
3686static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3687{
3688 struct net_device *slave_dev = NULL;
3689 struct ifbond k_binfo;
3690 struct ifbond __user *u_binfo = NULL;
3691 struct ifslave k_sinfo;
3692 struct ifslave __user *u_sinfo = NULL;
3693 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 int res = 0;
3695
3696 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3697 bond_dev->name, cmd);
3698
3699 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 case SIOCGMIIPHY:
3701 mii = if_mii(ifr);
3702 if (!mii) {
3703 return -EINVAL;
3704 }
3705 mii->phy_id = 0;
3706 /* Fall Through */
3707 case SIOCGMIIREG:
3708 /*
3709 * We do this again just in case we were called by SIOCGMIIREG
3710 * instead of SIOCGMIIPHY.
3711 */
3712 mii = if_mii(ifr);
3713 if (!mii) {
3714 return -EINVAL;
3715 }
3716
3717 if (mii->reg_num == 1) {
3718 struct bonding *bond = bond_dev->priv;
3719 mii->val_out = 0;
3720 read_lock_bh(&bond->lock);
3721 read_lock(&bond->curr_slave_lock);
Andy Gospodarek4e140072006-12-04 15:04:54 -08003722 if (netif_carrier_ok(bond->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 mii->val_out = BMSR_LSTATUS;
3724 }
3725 read_unlock(&bond->curr_slave_lock);
3726 read_unlock_bh(&bond->lock);
3727 }
3728
3729 return 0;
3730 case BOND_INFO_QUERY_OLD:
3731 case SIOCBONDINFOQUERY:
3732 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3733
3734 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3735 return -EFAULT;
3736 }
3737
3738 res = bond_info_query(bond_dev, &k_binfo);
3739 if (res == 0) {
3740 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3741 return -EFAULT;
3742 }
3743 }
3744
3745 return res;
3746 case BOND_SLAVE_INFO_QUERY_OLD:
3747 case SIOCBONDSLAVEINFOQUERY:
3748 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3749
3750 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3751 return -EFAULT;
3752 }
3753
3754 res = bond_slave_info_query(bond_dev, &k_sinfo);
3755 if (res == 0) {
3756 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3757 return -EFAULT;
3758 }
3759 }
3760
3761 return res;
3762 default:
3763 /* Go on */
3764 break;
3765 }
3766
3767 if (!capable(CAP_NET_ADMIN)) {
3768 return -EPERM;
3769 }
3770
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003771 down_write(&(bonding_rwsem));
Eric W. Biederman881d9662007-09-17 11:56:21 -07003772 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
3774 dprintk("slave_dev=%p: \n", slave_dev);
3775
3776 if (!slave_dev) {
3777 res = -ENODEV;
3778 } else {
3779 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3780 switch (cmd) {
3781 case BOND_ENSLAVE_OLD:
3782 case SIOCBONDENSLAVE:
3783 res = bond_enslave(bond_dev, slave_dev);
3784 break;
3785 case BOND_RELEASE_OLD:
3786 case SIOCBONDRELEASE:
3787 res = bond_release(bond_dev, slave_dev);
3788 break;
3789 case BOND_SETHWADDR_OLD:
3790 case SIOCBONDSETHWADDR:
3791 res = bond_sethwaddr(bond_dev, slave_dev);
3792 break;
3793 case BOND_CHANGE_ACTIVE_OLD:
3794 case SIOCBONDCHANGEACTIVE:
3795 res = bond_ioctl_change_active(bond_dev, slave_dev);
3796 break;
3797 default:
3798 res = -EOPNOTSUPP;
3799 }
3800
3801 dev_put(slave_dev);
3802 }
3803
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003804 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return res;
3806}
3807
3808static void bond_set_multicast_list(struct net_device *bond_dev)
3809{
3810 struct bonding *bond = bond_dev->priv;
3811 struct dev_mc_list *dmi;
3812
3813 write_lock_bh(&bond->lock);
3814
3815 /*
3816 * Do promisc before checking multicast_mode
3817 */
3818 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3819 bond_set_promiscuity(bond, 1);
3820 }
3821
3822 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3823 bond_set_promiscuity(bond, -1);
3824 }
3825
3826 /* set allmulti flag to slaves */
3827 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3828 bond_set_allmulti(bond, 1);
3829 }
3830
3831 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3832 bond_set_allmulti(bond, -1);
3833 }
3834
3835 bond->flags = bond_dev->flags;
3836
3837 /* looking for addresses to add to slaves' mc list */
3838 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3839 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3840 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3841 }
3842 }
3843
3844 /* looking for addresses to delete from slaves' list */
3845 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3846 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3847 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3848 }
3849 }
3850
3851 /* save master's multicast list */
3852 bond_mc_list_destroy(bond);
3853 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3854
3855 write_unlock_bh(&bond->lock);
3856}
3857
3858/*
3859 * Change the MTU of all of a master's slaves to match the master
3860 */
3861static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3862{
3863 struct bonding *bond = bond_dev->priv;
3864 struct slave *slave, *stop_at;
3865 int res = 0;
3866 int i;
3867
3868 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3869 (bond_dev ? bond_dev->name : "None"), new_mtu);
3870
3871 /* Can't hold bond->lock with bh disabled here since
3872 * some base drivers panic. On the other hand we can't
3873 * hold bond->lock without bh disabled because we'll
3874 * deadlock. The only solution is to rely on the fact
3875 * that we're under rtnl_lock here, and the slaves
3876 * list won't change. This doesn't solve the problem
3877 * of setting the slave's MTU while it is
3878 * transmitting, but the assumption is that the base
3879 * driver can handle that.
3880 *
3881 * TODO: figure out a way to safely iterate the slaves
3882 * list, but without holding a lock around the actual
3883 * call to the base driver.
3884 */
3885
3886 bond_for_each_slave(bond, slave, i) {
3887 dprintk("s %p s->p %p c_m %p\n", slave,
3888 slave->prev, slave->dev->change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 res = dev_set_mtu(slave->dev, new_mtu);
3891
3892 if (res) {
3893 /* If we failed to set the slave's mtu to the new value
3894 * we must abort the operation even in ACTIVE_BACKUP
3895 * mode, because if we allow the backup slaves to have
3896 * different mtu values than the active slave we'll
3897 * need to change their mtu when doing a failover. That
3898 * means changing their mtu from timer context, which
3899 * is probably not a good idea.
3900 */
3901 dprintk("err %d %s\n", res, slave->dev->name);
3902 goto unwind;
3903 }
3904 }
3905
3906 bond_dev->mtu = new_mtu;
3907
3908 return 0;
3909
3910unwind:
3911 /* unwind from head to the slave that failed */
3912 stop_at = slave;
3913 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3914 int tmp_res;
3915
3916 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3917 if (tmp_res) {
3918 dprintk("unwind err %d dev %s\n", tmp_res,
3919 slave->dev->name);
3920 }
3921 }
3922
3923 return res;
3924}
3925
3926/*
3927 * Change HW address
3928 *
3929 * Note that many devices must be down to change the HW address, and
3930 * downing the master releases all slaves. We can make bonds full of
3931 * bonding devices to test this, however.
3932 */
3933static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3934{
3935 struct bonding *bond = bond_dev->priv;
3936 struct sockaddr *sa = addr, tmp_sa;
3937 struct slave *slave, *stop_at;
3938 int res = 0;
3939 int i;
3940
3941 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3942
Moni Shoua2ab82852007-10-09 19:43:39 -07003943 if (!bond->do_set_mac_addr)
3944 return -EOPNOTSUPP;
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 if (!is_valid_ether_addr(sa->sa_data)) {
3947 return -EADDRNOTAVAIL;
3948 }
3949
3950 /* Can't hold bond->lock with bh disabled here since
3951 * some base drivers panic. On the other hand we can't
3952 * hold bond->lock without bh disabled because we'll
3953 * deadlock. The only solution is to rely on the fact
3954 * that we're under rtnl_lock here, and the slaves
3955 * list won't change. This doesn't solve the problem
3956 * of setting the slave's hw address while it is
3957 * transmitting, but the assumption is that the base
3958 * driver can handle that.
3959 *
3960 * TODO: figure out a way to safely iterate the slaves
3961 * list, but without holding a lock around the actual
3962 * call to the base driver.
3963 */
3964
3965 bond_for_each_slave(bond, slave, i) {
3966 dprintk("slave %p %s\n", slave, slave->dev->name);
3967
3968 if (slave->dev->set_mac_address == NULL) {
3969 res = -EOPNOTSUPP;
3970 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
3971 goto unwind;
3972 }
3973
3974 res = dev_set_mac_address(slave->dev, addr);
3975 if (res) {
3976 /* TODO: consider downing the slave
3977 * and retry ?
3978 * User should expect communications
3979 * breakage anyway until ARP finish
3980 * updating, so...
3981 */
3982 dprintk("err %d %s\n", res, slave->dev->name);
3983 goto unwind;
3984 }
3985 }
3986
3987 /* success */
3988 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3989 return 0;
3990
3991unwind:
3992 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3993 tmp_sa.sa_family = bond_dev->type;
3994
3995 /* unwind from head to the slave that failed */
3996 stop_at = slave;
3997 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3998 int tmp_res;
3999
4000 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4001 if (tmp_res) {
4002 dprintk("unwind err %d dev %s\n", tmp_res,
4003 slave->dev->name);
4004 }
4005 }
4006
4007 return res;
4008}
4009
4010static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4011{
4012 struct bonding *bond = bond_dev->priv;
4013 struct slave *slave, *start_at;
4014 int i;
4015 int res = 1;
4016
4017 read_lock(&bond->lock);
4018
4019 if (!BOND_IS_OK(bond)) {
4020 goto out;
4021 }
4022
4023 read_lock(&bond->curr_slave_lock);
4024 slave = start_at = bond->curr_active_slave;
4025 read_unlock(&bond->curr_slave_lock);
4026
4027 if (!slave) {
4028 goto out;
4029 }
4030
4031 bond_for_each_slave_from(bond, slave, i, start_at) {
4032 if (IS_UP(slave->dev) &&
4033 (slave->link == BOND_LINK_UP) &&
4034 (slave->state == BOND_STATE_ACTIVE)) {
4035 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4036
4037 write_lock(&bond->curr_slave_lock);
4038 bond->curr_active_slave = slave->next;
4039 write_unlock(&bond->curr_slave_lock);
4040
4041 break;
4042 }
4043 }
4044
4045
4046out:
4047 if (res) {
4048 /* no suitable interface, frame not sent */
4049 dev_kfree_skb(skb);
4050 }
4051 read_unlock(&bond->lock);
4052 return 0;
4053}
4054
John W. Linville075897c2005-09-28 17:50:53 -04004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056/*
4057 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4058 * the bond has a usable interface.
4059 */
4060static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4061{
4062 struct bonding *bond = bond_dev->priv;
4063 int res = 1;
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 read_lock(&bond->lock);
4066 read_lock(&bond->curr_slave_lock);
4067
4068 if (!BOND_IS_OK(bond)) {
4069 goto out;
4070 }
4071
John W. Linville075897c2005-09-28 17:50:53 -04004072 if (!bond->curr_active_slave)
4073 goto out;
4074
John W. Linville075897c2005-09-28 17:50:53 -04004075 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077out:
4078 if (res) {
4079 /* no suitable interface, frame not sent */
4080 dev_kfree_skb(skb);
4081 }
4082 read_unlock(&bond->curr_slave_lock);
4083 read_unlock(&bond->lock);
4084 return 0;
4085}
4086
4087/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004088 * In bond_xmit_xor() , we determine the output device by using a pre-
4089 * determined xmit_hash_policy(), If the selected device is not enabled,
4090 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 */
4092static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4093{
4094 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 struct slave *slave, *start_at;
4096 int slave_no;
4097 int i;
4098 int res = 1;
4099
4100 read_lock(&bond->lock);
4101
4102 if (!BOND_IS_OK(bond)) {
4103 goto out;
4104 }
4105
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004106 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 bond_for_each_slave(bond, slave, i) {
4109 slave_no--;
4110 if (slave_no < 0) {
4111 break;
4112 }
4113 }
4114
4115 start_at = slave;
4116
4117 bond_for_each_slave_from(bond, slave, i, start_at) {
4118 if (IS_UP(slave->dev) &&
4119 (slave->link == BOND_LINK_UP) &&
4120 (slave->state == BOND_STATE_ACTIVE)) {
4121 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4122 break;
4123 }
4124 }
4125
4126out:
4127 if (res) {
4128 /* no suitable interface, frame not sent */
4129 dev_kfree_skb(skb);
4130 }
4131 read_unlock(&bond->lock);
4132 return 0;
4133}
4134
4135/*
4136 * in broadcast mode, we send everything to all usable interfaces.
4137 */
4138static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4139{
4140 struct bonding *bond = bond_dev->priv;
4141 struct slave *slave, *start_at;
4142 struct net_device *tx_dev = NULL;
4143 int i;
4144 int res = 1;
4145
4146 read_lock(&bond->lock);
4147
4148 if (!BOND_IS_OK(bond)) {
4149 goto out;
4150 }
4151
4152 read_lock(&bond->curr_slave_lock);
4153 start_at = bond->curr_active_slave;
4154 read_unlock(&bond->curr_slave_lock);
4155
4156 if (!start_at) {
4157 goto out;
4158 }
4159
4160 bond_for_each_slave_from(bond, slave, i, start_at) {
4161 if (IS_UP(slave->dev) &&
4162 (slave->link == BOND_LINK_UP) &&
4163 (slave->state == BOND_STATE_ACTIVE)) {
4164 if (tx_dev) {
4165 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4166 if (!skb2) {
4167 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004168 ": %s: Error: bond_xmit_broadcast(): "
4169 "skb_clone() failed\n",
4170 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 continue;
4172 }
4173
4174 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4175 if (res) {
4176 dev_kfree_skb(skb2);
4177 continue;
4178 }
4179 }
4180 tx_dev = slave->dev;
4181 }
4182 }
4183
4184 if (tx_dev) {
4185 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4186 }
4187
4188out:
4189 if (res) {
4190 /* no suitable interface, frame not sent */
4191 dev_kfree_skb(skb);
4192 }
4193 /* frame sent to all suitable interfaces */
4194 read_unlock(&bond->lock);
4195 return 0;
4196}
4197
4198/*------------------------- Device initialization ---------------------------*/
4199
4200/*
4201 * set bond mode specific net device operations
4202 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004203void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004205 struct net_device *bond_dev = bond->dev;
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 switch (mode) {
4208 case BOND_MODE_ROUNDROBIN:
4209 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4210 break;
4211 case BOND_MODE_ACTIVEBACKUP:
4212 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4213 break;
4214 case BOND_MODE_XOR:
4215 bond_dev->hard_start_xmit = bond_xmit_xor;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004216 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4217 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4218 else
4219 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 break;
4221 case BOND_MODE_BROADCAST:
4222 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4223 break;
4224 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004225 bond_set_master_3ad_flags(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004227 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4228 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4229 else
4230 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004233 bond_set_master_alb_flags(bond);
4234 /* FALLTHRU */
4235 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 bond_dev->hard_start_xmit = bond_alb_xmit;
4237 bond_dev->set_mac_address = bond_alb_set_mac_address;
4238 break;
4239 default:
4240 /* Should never happen, mode already checked */
4241 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004242 ": %s: Error: Unknown bonding mode %d\n",
4243 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 mode);
4245 break;
4246 }
4247}
4248
Jay Vosburgh217df672005-09-26 16:11:50 -07004249static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4250 struct ethtool_drvinfo *drvinfo)
4251{
4252 strncpy(drvinfo->driver, DRV_NAME, 32);
4253 strncpy(drvinfo->version, DRV_VERSION, 32);
4254 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4255}
4256
Jeff Garzik7282d492006-09-13 14:30:00 -04004257static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004258 .get_drvinfo = bond_ethtool_get_drvinfo,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004259};
4260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261/*
4262 * Does not allocate but creates a /proc entry.
4263 * Allowed to fail.
4264 */
Mitch Williams3c535952005-11-09 10:36:11 -08004265static int bond_init(struct net_device *bond_dev, struct bond_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
4267 struct bonding *bond = bond_dev->priv;
4268
4269 dprintk("Begin bond_init for %s\n", bond_dev->name);
4270
4271 /* initialize rwlocks */
4272 rwlock_init(&bond->lock);
4273 rwlock_init(&bond->curr_slave_lock);
4274
4275 bond->params = *params; /* copy params struct */
4276
4277 /* Initialize pointers */
4278 bond->first_slave = NULL;
4279 bond->curr_active_slave = NULL;
4280 bond->current_arp_slave = NULL;
4281 bond->primary_slave = NULL;
4282 bond->dev = bond_dev;
4283 INIT_LIST_HEAD(&bond->vlan_list);
4284
4285 /* Initialize the device entry points */
4286 bond_dev->open = bond_open;
4287 bond_dev->stop = bond_close;
4288 bond_dev->get_stats = bond_get_stats;
4289 bond_dev->do_ioctl = bond_do_ioctl;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004290 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 bond_dev->set_multicast_list = bond_set_multicast_list;
4292 bond_dev->change_mtu = bond_change_mtu;
4293 bond_dev->set_mac_address = bond_set_mac_address;
4294
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004295 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
4297 bond_dev->destructor = free_netdev;
4298
4299 /* Initialize the device options */
4300 bond_dev->tx_queue_len = 0;
4301 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004302 bond_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
4304 /* At first, we block adding VLANs. That's the only way to
4305 * prevent problems that occur when adding VLANs over an
4306 * empty bond. The block will be removed once non-challenged
4307 * slaves are enslaved.
4308 */
4309 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4310
Herbert Xu932ff272006-06-09 12:20:56 -07004311 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 * transmitting */
4313 bond_dev->features |= NETIF_F_LLTX;
4314
4315 /* By default, we declare the bond to be fully
4316 * VLAN hardware accelerated capable. Special
4317 * care is taken in the various xmit functions
4318 * when there are slaves that are not hw accel
4319 * capable
4320 */
4321 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4322 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4323 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4324 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4325 NETIF_F_HW_VLAN_RX |
4326 NETIF_F_HW_VLAN_FILTER);
4327
4328#ifdef CONFIG_PROC_FS
4329 bond_create_proc_entry(bond);
4330#endif
4331
Moni Shoua2ab82852007-10-09 19:43:39 -07004332 /* set do_set_mac_addr to true on startup */
4333 bond->do_set_mac_addr = 1;
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 list_add_tail(&bond->bond_list, &bond_dev_list);
4336
4337 return 0;
4338}
4339
4340/* De-initialize device specific data.
4341 * Caller must hold rtnl_lock.
4342 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004343void bond_deinit(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
4345 struct bonding *bond = bond_dev->priv;
4346
4347 list_del(&bond->bond_list);
4348
4349#ifdef CONFIG_PROC_FS
4350 bond_remove_proc_entry(bond);
4351#endif
4352}
4353
4354/* Unregister and free all bond devices.
4355 * Caller must hold rtnl_lock.
4356 */
4357static void bond_free_all(void)
4358{
4359 struct bonding *bond, *nxt;
4360
4361 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4362 struct net_device *bond_dev = bond->dev;
4363
jamal702987052006-09-22 21:54:37 -07004364 bond_mc_list_destroy(bond);
4365 /* Release the bonded slaves */
4366 bond_release_all(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 bond_deinit(bond_dev);
Jay Vosburgh3201e652007-06-19 11:12:12 -07004368 unregister_netdevice(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 }
4370
4371#ifdef CONFIG_PROC_FS
4372 bond_destroy_proc_dir();
4373#endif
4374}
4375
4376/*------------------------- Module initialization ---------------------------*/
4377
4378/*
4379 * Convert string input module parms. Accept either the
4380 * number of the mode or its string name.
4381 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004382int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
4384 int i;
4385
4386 for (i = 0; tbl[i].modename; i++) {
4387 if ((isdigit(*mode_arg) &&
4388 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4389 (strncmp(mode_arg, tbl[i].modename,
4390 strlen(tbl[i].modename)) == 0)) {
4391 return tbl[i].mode;
4392 }
4393 }
4394
4395 return -1;
4396}
4397
4398static int bond_check_params(struct bond_params *params)
4399{
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004400 int arp_validate_value;
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 /*
4403 * Convert string parameters.
4404 */
4405 if (mode) {
4406 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4407 if (bond_mode == -1) {
4408 printk(KERN_ERR DRV_NAME
4409 ": Error: Invalid bonding mode \"%s\"\n",
4410 mode == NULL ? "NULL" : mode);
4411 return -EINVAL;
4412 }
4413 }
4414
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004415 if (xmit_hash_policy) {
4416 if ((bond_mode != BOND_MODE_XOR) &&
4417 (bond_mode != BOND_MODE_8023AD)) {
4418 printk(KERN_INFO DRV_NAME
4419 ": xor_mode param is irrelevant in mode %s\n",
4420 bond_mode_name(bond_mode));
4421 } else {
4422 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4423 xmit_hashtype_tbl);
4424 if (xmit_hashtype == -1) {
4425 printk(KERN_ERR DRV_NAME
4426 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4427 xmit_hash_policy == NULL ? "NULL" :
4428 xmit_hash_policy);
4429 return -EINVAL;
4430 }
4431 }
4432 }
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 if (lacp_rate) {
4435 if (bond_mode != BOND_MODE_8023AD) {
4436 printk(KERN_INFO DRV_NAME
4437 ": lacp_rate param is irrelevant in mode %s\n",
4438 bond_mode_name(bond_mode));
4439 } else {
4440 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4441 if (lacp_fast == -1) {
4442 printk(KERN_ERR DRV_NAME
4443 ": Error: Invalid lacp rate \"%s\"\n",
4444 lacp_rate == NULL ? "NULL" : lacp_rate);
4445 return -EINVAL;
4446 }
4447 }
4448 }
4449
4450 if (max_bonds < 1 || max_bonds > INT_MAX) {
4451 printk(KERN_WARNING DRV_NAME
4452 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004453 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4455 max_bonds = BOND_DEFAULT_MAX_BONDS;
4456 }
4457
4458 if (miimon < 0) {
4459 printk(KERN_WARNING DRV_NAME
4460 ": Warning: miimon module parameter (%d), "
4461 "not in range 0-%d, so it was reset to %d\n",
4462 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4463 miimon = BOND_LINK_MON_INTERV;
4464 }
4465
4466 if (updelay < 0) {
4467 printk(KERN_WARNING DRV_NAME
4468 ": Warning: updelay module parameter (%d), "
4469 "not in range 0-%d, so it was reset to 0\n",
4470 updelay, INT_MAX);
4471 updelay = 0;
4472 }
4473
4474 if (downdelay < 0) {
4475 printk(KERN_WARNING DRV_NAME
4476 ": Warning: downdelay module parameter (%d), "
4477 "not in range 0-%d, so it was reset to 0\n",
4478 downdelay, INT_MAX);
4479 downdelay = 0;
4480 }
4481
4482 if ((use_carrier != 0) && (use_carrier != 1)) {
4483 printk(KERN_WARNING DRV_NAME
4484 ": Warning: use_carrier module parameter (%d), "
4485 "not of valid value (0/1), so it was set to 1\n",
4486 use_carrier);
4487 use_carrier = 1;
4488 }
4489
4490 /* reset values for 802.3ad */
4491 if (bond_mode == BOND_MODE_8023AD) {
4492 if (!miimon) {
4493 printk(KERN_WARNING DRV_NAME
4494 ": Warning: miimon must be specified, "
4495 "otherwise bonding will not detect link "
4496 "failure, speed and duplex which are "
4497 "essential for 802.3ad operation\n");
4498 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4499 miimon = 100;
4500 }
4501 }
4502
4503 /* reset values for TLB/ALB */
4504 if ((bond_mode == BOND_MODE_TLB) ||
4505 (bond_mode == BOND_MODE_ALB)) {
4506 if (!miimon) {
4507 printk(KERN_WARNING DRV_NAME
4508 ": Warning: miimon must be specified, "
4509 "otherwise bonding will not detect link "
4510 "failure and link speed which are essential "
4511 "for TLB/ALB load balancing\n");
4512 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4513 miimon = 100;
4514 }
4515 }
4516
4517 if (bond_mode == BOND_MODE_ALB) {
4518 printk(KERN_NOTICE DRV_NAME
4519 ": In ALB mode you might experience client "
4520 "disconnections upon reconnection of a link if the "
4521 "bonding module updelay parameter (%d msec) is "
4522 "incompatible with the forwarding delay time of the "
4523 "switch\n",
4524 updelay);
4525 }
4526
4527 if (!miimon) {
4528 if (updelay || downdelay) {
4529 /* just warn the user the up/down delay will have
4530 * no effect since miimon is zero...
4531 */
4532 printk(KERN_WARNING DRV_NAME
4533 ": Warning: miimon module parameter not set "
4534 "and updelay (%d) or downdelay (%d) module "
4535 "parameter is set; updelay and downdelay have "
4536 "no effect unless miimon is set\n",
4537 updelay, downdelay);
4538 }
4539 } else {
4540 /* don't allow arp monitoring */
4541 if (arp_interval) {
4542 printk(KERN_WARNING DRV_NAME
4543 ": Warning: miimon (%d) and arp_interval (%d) "
4544 "can't be used simultaneously, disabling ARP "
4545 "monitoring\n",
4546 miimon, arp_interval);
4547 arp_interval = 0;
4548 }
4549
4550 if ((updelay % miimon) != 0) {
4551 printk(KERN_WARNING DRV_NAME
4552 ": Warning: updelay (%d) is not a multiple "
4553 "of miimon (%d), updelay rounded to %d ms\n",
4554 updelay, miimon, (updelay / miimon) * miimon);
4555 }
4556
4557 updelay /= miimon;
4558
4559 if ((downdelay % miimon) != 0) {
4560 printk(KERN_WARNING DRV_NAME
4561 ": Warning: downdelay (%d) is not a multiple "
4562 "of miimon (%d), downdelay rounded to %d ms\n",
4563 downdelay, miimon,
4564 (downdelay / miimon) * miimon);
4565 }
4566
4567 downdelay /= miimon;
4568 }
4569
4570 if (arp_interval < 0) {
4571 printk(KERN_WARNING DRV_NAME
4572 ": Warning: arp_interval module parameter (%d) "
4573 ", not in range 0-%d, so it was reset to %d\n",
4574 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4575 arp_interval = BOND_LINK_ARP_INTERV;
4576 }
4577
4578 for (arp_ip_count = 0;
4579 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4580 arp_ip_count++) {
4581 /* not complete check, but should be good enough to
4582 catch mistakes */
4583 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4584 printk(KERN_WARNING DRV_NAME
4585 ": Warning: bad arp_ip_target module parameter "
4586 "(%s), ARP monitoring will not be performed\n",
4587 arp_ip_target[arp_ip_count]);
4588 arp_interval = 0;
4589 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004590 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 arp_target[arp_ip_count] = ip;
4592 }
4593 }
4594
4595 if (arp_interval && !arp_ip_count) {
4596 /* don't allow arping if no arp_ip_target given... */
4597 printk(KERN_WARNING DRV_NAME
4598 ": Warning: arp_interval module parameter (%d) "
4599 "specified without providing an arp_ip_target "
4600 "parameter, arp_interval was reset to 0\n",
4601 arp_interval);
4602 arp_interval = 0;
4603 }
4604
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004605 if (arp_validate) {
4606 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4607 printk(KERN_ERR DRV_NAME
4608 ": arp_validate only supported in active-backup mode\n");
4609 return -EINVAL;
4610 }
4611 if (!arp_interval) {
4612 printk(KERN_ERR DRV_NAME
4613 ": arp_validate requires arp_interval\n");
4614 return -EINVAL;
4615 }
4616
4617 arp_validate_value = bond_parse_parm(arp_validate,
4618 arp_validate_tbl);
4619 if (arp_validate_value == -1) {
4620 printk(KERN_ERR DRV_NAME
4621 ": Error: invalid arp_validate \"%s\"\n",
4622 arp_validate == NULL ? "NULL" : arp_validate);
4623 return -EINVAL;
4624 }
4625 } else
4626 arp_validate_value = 0;
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (miimon) {
4629 printk(KERN_INFO DRV_NAME
4630 ": MII link monitoring set to %d ms\n",
4631 miimon);
4632 } else if (arp_interval) {
4633 int i;
4634
4635 printk(KERN_INFO DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004636 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4637 arp_interval,
4638 arp_validate_tbl[arp_validate_value].modename,
4639 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 for (i = 0; i < arp_ip_count; i++)
4642 printk (" %s", arp_ip_target[i]);
4643
4644 printk("\n");
4645
4646 } else {
4647 /* miimon and arp_interval not set, we need one so things
4648 * work as expected, see bonding.txt for details
4649 */
4650 printk(KERN_WARNING DRV_NAME
4651 ": Warning: either miimon or arp_interval and "
4652 "arp_ip_target module parameters must be specified, "
4653 "otherwise bonding will not detect link failures! see "
4654 "bonding.txt for details.\n");
4655 }
4656
4657 if (primary && !USES_PRIMARY(bond_mode)) {
4658 /* currently, using a primary only makes sense
4659 * in active backup, TLB or ALB modes
4660 */
4661 printk(KERN_WARNING DRV_NAME
4662 ": Warning: %s primary device specified but has no "
4663 "effect in %s mode\n",
4664 primary, bond_mode_name(bond_mode));
4665 primary = NULL;
4666 }
4667
4668 /* fill params struct with the proper values */
4669 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004670 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 params->miimon = miimon;
4672 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004673 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 params->updelay = updelay;
4675 params->downdelay = downdelay;
4676 params->use_carrier = use_carrier;
4677 params->lacp_fast = lacp_fast;
4678 params->primary[0] = 0;
4679
4680 if (primary) {
4681 strncpy(params->primary, primary, IFNAMSIZ);
4682 params->primary[IFNAMSIZ - 1] = 0;
4683 }
4684
4685 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4686
4687 return 0;
4688}
4689
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004690static struct lock_class_key bonding_netdev_xmit_lock_key;
4691
Mitch Williamsdfe60392005-11-09 10:36:04 -08004692/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004693 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004694 * Caller must NOT hold rtnl_lock; we need to release it here before we
4695 * set up our sysfs entries.
4696 */
4697int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4698{
4699 struct net_device *bond_dev;
4700 int res;
4701
4702 rtnl_lock();
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004703 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4704 ether_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004705 if (!bond_dev) {
4706 printk(KERN_ERR DRV_NAME
4707 ": %s: eek! can't alloc netdev!\n",
4708 name);
4709 res = -ENOMEM;
4710 goto out_rtnl;
4711 }
4712
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004713 if (!name) {
4714 res = dev_alloc_name(bond_dev, "bond%d");
4715 if (res < 0)
4716 goto out_netdev;
4717 }
4718
Mitch Williamsdfe60392005-11-09 10:36:04 -08004719 /* bond_init() must be called after dev_alloc_name() (for the
4720 * /proc files), but before register_netdevice(), because we
4721 * need to set function pointers.
4722 */
4723
4724 res = bond_init(bond_dev, params);
4725 if (res < 0) {
4726 goto out_netdev;
4727 }
4728
Mitch Williamsdfe60392005-11-09 10:36:04 -08004729 res = register_netdevice(bond_dev);
4730 if (res < 0) {
4731 goto out_bond;
4732 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004733
4734 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4735
Mitch Williamsdfe60392005-11-09 10:36:04 -08004736 if (newbond)
4737 *newbond = bond_dev->priv;
4738
Jay Vosburghff59c452006-03-27 13:27:43 -08004739 netif_carrier_off(bond_dev);
4740
Mitch Williamsdfe60392005-11-09 10:36:04 -08004741 rtnl_unlock(); /* allows sysfs registration of net device */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004742 res = bond_create_sysfs_entry(bond_dev->priv);
Jay Vosburgh09c89272007-01-19 18:15:38 -08004743 if (res < 0) {
4744 rtnl_lock();
4745 goto out_bond;
4746 }
4747
4748 return 0;
4749
Mitch Williamsdfe60392005-11-09 10:36:04 -08004750out_bond:
4751 bond_deinit(bond_dev);
4752out_netdev:
4753 free_netdev(bond_dev);
4754out_rtnl:
4755 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004756 return res;
4757}
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759static int __init bonding_init(void)
4760{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 int i;
4762 int res;
4763
4764 printk(KERN_INFO "%s", version);
4765
Mitch Williamsdfe60392005-11-09 10:36:04 -08004766 res = bond_check_params(&bonding_defaults);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 if (res) {
Mitch Williamsdfe60392005-11-09 10:36:04 -08004768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 }
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771#ifdef CONFIG_PROC_FS
4772 bond_create_proc_dir();
4773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 for (i = 0; i < max_bonds; i++) {
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004775 res = bond_create(NULL, &bonding_defaults, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004776 if (res)
4777 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004780 res = bond_create_sysfs();
4781 if (res)
4782 goto err;
4783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004785 register_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Mitch Williamsdfe60392005-11-09 10:36:04 -08004787 goto out;
4788err:
Florin Malita40abc272005-09-18 00:24:12 -07004789 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 bond_free_all();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004791 bond_destroy_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004793out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004795
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
4798static void __exit bonding_exit(void)
4799{
4800 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004801 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 rtnl_lock();
4804 bond_free_all();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004805 bond_destroy_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 rtnl_unlock();
4807}
4808
4809module_init(bonding_init);
4810module_exit(bonding_exit);
4811MODULE_LICENSE("GPL");
4812MODULE_VERSION(DRV_VERSION);
4813MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4814MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4815MODULE_SUPPORTED_DEVICE("most ethernet devices");
4816
4817/*
4818 * Local variables:
4819 * c-indent-level: 8
4820 * c-basic-offset: 8
4821 * tab-width: 8
4822 * End:
4823 */
4824