source: src/linux/xscale/linux-2.6.23/drivers/net/bonding/bond_main.c @ 10891

Last change on this file since 10891 was 10891, checked in by BrainSlayer, 5 years ago

two duplex modes, one for master, one for slave

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