2 * originally based on the dummy device.
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
7 * bonding.c: an Ethernet Bonding driver
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
14 * and probably many L2 switches ...
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.
24 * will release all slaves, marking them as down.
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.
34 //#define BONDING_DEBUG 1
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>
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>
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>
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>
73 #include <linux/mii.h>
74 #include <linux/ethtool.h>
75 #include <linux/if_vlan.h>
76 #include <linux/if_bonding.h>
77 #include <linux/jiffies.h>
78 #include <net/route.h>
79 #include <net/net_namespace.h>
84 /*---------------------------- Module parameters ----------------------------*/
86 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
87 #define BOND_LINK_MON_INTERV 0
88 #define BOND_LINK_ARP_INTERV 0
90 static int max_bonds
= BOND_DEFAULT_MAX_BONDS
;
91 static int miimon
= BOND_LINK_MON_INTERV
;
92 static int updelay
= 0;
93 static int downdelay
= 0;
94 static int use_carrier
= 1;
95 static char *mode
= NULL
;
96 static char *primary
= NULL
;
97 static char *lacp_rate
= NULL
;
98 static char *xmit_hash_policy
= NULL
;
99 static int arp_interval
= BOND_LINK_ARP_INTERV
;
100 static char *arp_ip_target
[BOND_MAX_ARP_TARGETS
] = { NULL
, };
101 static char *arp_validate
= NULL
;
102 static int fail_over_mac
= 0;
103 struct bond_params bonding_defaults
;
105 module_param(max_bonds
, int, 0);
106 MODULE_PARM_DESC(max_bonds
, "Max number of bonded devices");
107 module_param(miimon
, int, 0);
108 MODULE_PARM_DESC(miimon
, "Link check interval in milliseconds");
109 module_param(updelay
, int, 0);
110 MODULE_PARM_DESC(updelay
, "Delay before considering link up, in milliseconds");
111 module_param(downdelay
, int, 0);
112 MODULE_PARM_DESC(downdelay
, "Delay before considering link down, "
114 module_param(use_carrier
, int, 0);
115 MODULE_PARM_DESC(use_carrier
, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
116 "0 for off, 1 for on (default)");
117 module_param(mode
, charp
, 0);
118 MODULE_PARM_DESC(mode
, "Mode of operation : 0 for balance-rr, "
119 "1 for active-backup, 2 for balance-xor, "
120 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
121 "6 for balance-alb");
122 module_param(primary
, charp
, 0);
123 MODULE_PARM_DESC(primary
, "Primary network device to use");
124 module_param(lacp_rate
, charp
, 0);
125 MODULE_PARM_DESC(lacp_rate
, "LACPDU tx rate to request from 802.3ad partner "
127 module_param(xmit_hash_policy
, charp
, 0);
128 MODULE_PARM_DESC(xmit_hash_policy
, "XOR hashing method: 0 for layer 2 (default)"
129 ", 1 for layer 3+4");
130 module_param(arp_interval
, int, 0);
131 MODULE_PARM_DESC(arp_interval
, "arp interval in milliseconds");
132 module_param_array(arp_ip_target
, charp
, NULL
, 0);
133 MODULE_PARM_DESC(arp_ip_target
, "arp targets in n.n.n.n form");
134 module_param(arp_validate
, charp
, 0);
135 MODULE_PARM_DESC(arp_validate
, "validate src/dst of ARP probes: none (default), active, backup or all");
136 module_param(fail_over_mac
, int, 0);
137 MODULE_PARM_DESC(fail_over_mac
, "For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on.");
139 /*----------------------------- Global variables ----------------------------*/
141 static const char * const version
=
142 DRV_DESCRIPTION
": v" DRV_VERSION
" (" DRV_RELDATE
")\n";
144 LIST_HEAD(bond_dev_list
);
146 #ifdef CONFIG_PROC_FS
147 static struct proc_dir_entry
*bond_proc_dir
= NULL
;
150 extern struct rw_semaphore bonding_rwsem
;
151 static __be32 arp_target
[BOND_MAX_ARP_TARGETS
] = { 0, } ;
152 static int arp_ip_count
= 0;
153 static int bond_mode
= BOND_MODE_ROUNDROBIN
;
154 static int xmit_hashtype
= BOND_XMIT_POLICY_LAYER2
;
155 static int lacp_fast
= 0;
158 struct bond_parm_tbl bond_lacp_tbl
[] = {
159 { "slow", AD_LACP_SLOW
},
160 { "fast", AD_LACP_FAST
},
164 struct bond_parm_tbl bond_mode_tbl
[] = {
165 { "balance-rr", BOND_MODE_ROUNDROBIN
},
166 { "active-backup", BOND_MODE_ACTIVEBACKUP
},
167 { "balance-xor", BOND_MODE_XOR
},
168 { "broadcast", BOND_MODE_BROADCAST
},
169 { "802.3ad", BOND_MODE_8023AD
},
170 { "balance-tlb", BOND_MODE_TLB
},
171 { "balance-alb", BOND_MODE_ALB
},
175 struct bond_parm_tbl xmit_hashtype_tbl
[] = {
176 { "layer2", BOND_XMIT_POLICY_LAYER2
},
177 { "layer3+4", BOND_XMIT_POLICY_LAYER34
},
181 struct bond_parm_tbl arp_validate_tbl
[] = {
182 { "none", BOND_ARP_VALIDATE_NONE
},
183 { "active", BOND_ARP_VALIDATE_ACTIVE
},
184 { "backup", BOND_ARP_VALIDATE_BACKUP
},
185 { "all", BOND_ARP_VALIDATE_ALL
},
189 /*-------------------------- Forward declarations ---------------------------*/
191 static void bond_send_gratuitous_arp(struct bonding
*bond
);
192 static void bond_deinit(struct net_device
*bond_dev
);
194 /*---------------------------- General routines -----------------------------*/
196 static const char *bond_mode_name(int mode
)
199 case BOND_MODE_ROUNDROBIN
:
200 return "load balancing (round-robin)";
201 case BOND_MODE_ACTIVEBACKUP
:
202 return "fault-tolerance (active-backup)";
204 return "load balancing (xor)";
205 case BOND_MODE_BROADCAST
:
206 return "fault-tolerance (broadcast)";
207 case BOND_MODE_8023AD
:
208 return "IEEE 802.3ad Dynamic link aggregation";
210 return "transmit load balancing";
212 return "adaptive load balancing";
218 /*---------------------------------- VLAN -----------------------------------*/
221 * bond_add_vlan - add a new vlan id on bond
222 * @bond: bond that got the notification
223 * @vlan_id: the vlan id to add
225 * Returns -ENOMEM if allocation failed.
227 static int bond_add_vlan(struct bonding
*bond
, unsigned short vlan_id
)
229 struct vlan_entry
*vlan
;
231 dprintk("bond: %s, vlan id %d\n",
232 (bond
? bond
->dev
->name
: "None"), vlan_id
);
234 vlan
= kmalloc(sizeof(struct vlan_entry
), GFP_KERNEL
);
239 INIT_LIST_HEAD(&vlan
->vlan_list
);
240 vlan
->vlan_id
= vlan_id
;
243 write_lock_bh(&bond
->lock
);
245 list_add_tail(&vlan
->vlan_list
, &bond
->vlan_list
);
247 write_unlock_bh(&bond
->lock
);
249 dprintk("added VLAN ID %d on bond %s\n", vlan_id
, bond
->dev
->name
);
255 * bond_del_vlan - delete a vlan id from bond
256 * @bond: bond that got the notification
257 * @vlan_id: the vlan id to delete
259 * returns -ENODEV if @vlan_id was not found in @bond.
261 static int bond_del_vlan(struct bonding
*bond
, unsigned short vlan_id
)
263 struct vlan_entry
*vlan
, *next
;
266 dprintk("bond: %s, vlan id %d\n", bond
->dev
->name
, vlan_id
);
268 write_lock_bh(&bond
->lock
);
270 list_for_each_entry_safe(vlan
, next
, &bond
->vlan_list
, vlan_list
) {
271 if (vlan
->vlan_id
== vlan_id
) {
272 list_del(&vlan
->vlan_list
);
274 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
275 (bond
->params
.mode
== BOND_MODE_ALB
)) {
276 bond_alb_clear_vlan(bond
, vlan_id
);
279 dprintk("removed VLAN ID %d from bond %s\n", vlan_id
,
284 if (list_empty(&bond
->vlan_list
) &&
285 (bond
->slave_cnt
== 0)) {
286 /* Last VLAN removed and no slaves, so
287 * restore block on adding VLANs. This will
288 * be removed once new slaves that are not
289 * VLAN challenged will be added.
291 bond
->dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
299 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id
,
303 write_unlock_bh(&bond
->lock
);
308 * bond_has_challenged_slaves
309 * @bond: the bond we're working on
311 * Searches the slave list. Returns 1 if a vlan challenged slave
312 * was found, 0 otherwise.
314 * Assumes bond->lock is held.
316 static int bond_has_challenged_slaves(struct bonding
*bond
)
321 bond_for_each_slave(bond
, slave
, i
) {
322 if (slave
->dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
323 dprintk("found VLAN challenged slave - %s\n",
329 dprintk("no VLAN challenged slaves found\n");
334 * bond_next_vlan - safely skip to the next item in the vlans list.
335 * @bond: the bond we're working on
336 * @curr: item we're advancing from
338 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
339 * or @curr->next otherwise (even if it is @curr itself again).
341 * Caller must hold bond->lock
343 struct vlan_entry
*bond_next_vlan(struct bonding
*bond
, struct vlan_entry
*curr
)
345 struct vlan_entry
*next
, *last
;
347 if (list_empty(&bond
->vlan_list
)) {
352 next
= list_entry(bond
->vlan_list
.next
,
353 struct vlan_entry
, vlan_list
);
355 last
= list_entry(bond
->vlan_list
.prev
,
356 struct vlan_entry
, vlan_list
);
358 next
= list_entry(bond
->vlan_list
.next
,
359 struct vlan_entry
, vlan_list
);
361 next
= list_entry(curr
->vlan_list
.next
,
362 struct vlan_entry
, vlan_list
);
370 * bond_dev_queue_xmit - Prepare skb for xmit.
372 * @bond: bond device that got this skb for tx.
373 * @skb: hw accel VLAN tagged skb to transmit
374 * @slave_dev: slave that is supposed to xmit this skbuff
376 * When the bond gets an skb to transmit that is
377 * already hardware accelerated VLAN tagged, and it
378 * needs to relay this skb to a slave that is not
379 * hw accel capable, the skb needs to be "unaccelerated",
380 * i.e. strip the hwaccel tag and re-insert it as part
383 int bond_dev_queue_xmit(struct bonding
*bond
, struct sk_buff
*skb
, struct net_device
*slave_dev
)
385 unsigned short vlan_id
;
387 if (!list_empty(&bond
->vlan_list
) &&
388 !(slave_dev
->features
& NETIF_F_HW_VLAN_TX
) &&
389 vlan_get_tag(skb
, &vlan_id
) == 0) {
390 skb
->dev
= slave_dev
;
391 skb
= vlan_put_tag(skb
, vlan_id
);
393 /* vlan_put_tag() frees the skb in case of error,
394 * so return success here so the calling functions
395 * won't attempt to free is again.
400 skb
->dev
= slave_dev
;
410 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
411 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
413 * a. This operation is performed in IOCTL context,
414 * b. The operation is protected by the RTNL semaphore in the 8021q code,
415 * c. Holding a lock with BH disabled while directly calling a base driver
416 * entry point is generally a BAD idea.
418 * The design of synchronization/protection for this operation in the 8021q
419 * module is good for one or more VLAN devices over a single physical device
420 * and cannot be extended for a teaming solution like bonding, so there is a
421 * potential race condition here where a net device from the vlan group might
422 * be referenced (either by a base driver or the 8021q code) while it is being
423 * removed from the system. However, it turns out we're not making matters
424 * worse, and if it works for regular VLAN usage it will work here too.
428 * bond_vlan_rx_register - Propagates registration to slaves
429 * @bond_dev: bonding net device that got called
430 * @grp: vlan group being registered
432 static void bond_vlan_rx_register(struct net_device
*bond_dev
, struct vlan_group
*grp
)
434 struct bonding
*bond
= bond_dev
->priv
;
440 bond_for_each_slave(bond
, slave
, i
) {
441 struct net_device
*slave_dev
= slave
->dev
;
443 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
444 slave_dev
->vlan_rx_register
) {
445 slave_dev
->vlan_rx_register(slave_dev
, grp
);
451 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
452 * @bond_dev: bonding net device that got called
453 * @vid: vlan id being added
455 static void bond_vlan_rx_add_vid(struct net_device
*bond_dev
, uint16_t vid
)
457 struct bonding
*bond
= bond_dev
->priv
;
461 bond_for_each_slave(bond
, slave
, i
) {
462 struct net_device
*slave_dev
= slave
->dev
;
464 if ((slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
465 slave_dev
->vlan_rx_add_vid
) {
466 slave_dev
->vlan_rx_add_vid(slave_dev
, vid
);
470 res
= bond_add_vlan(bond
, vid
);
472 printk(KERN_ERR DRV_NAME
473 ": %s: Error: Failed to add vlan id %d\n",
474 bond_dev
->name
, vid
);
479 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
480 * @bond_dev: bonding net device that got called
481 * @vid: vlan id being removed
483 static void bond_vlan_rx_kill_vid(struct net_device
*bond_dev
, uint16_t vid
)
485 struct bonding
*bond
= bond_dev
->priv
;
487 struct net_device
*vlan_dev
;
490 bond_for_each_slave(bond
, slave
, i
) {
491 struct net_device
*slave_dev
= slave
->dev
;
493 if ((slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
494 slave_dev
->vlan_rx_kill_vid
) {
495 /* Save and then restore vlan_dev in the grp array,
496 * since the slave's driver might clear it.
498 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vid
);
499 slave_dev
->vlan_rx_kill_vid(slave_dev
, vid
);
500 vlan_group_set_device(bond
->vlgrp
, vid
, vlan_dev
);
504 res
= bond_del_vlan(bond
, vid
);
506 printk(KERN_ERR DRV_NAME
507 ": %s: Error: Failed to remove vlan id %d\n",
508 bond_dev
->name
, vid
);
512 static void bond_add_vlans_on_slave(struct bonding
*bond
, struct net_device
*slave_dev
)
514 struct vlan_entry
*vlan
;
516 write_lock_bh(&bond
->lock
);
518 if (list_empty(&bond
->vlan_list
)) {
522 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
523 slave_dev
->vlan_rx_register
) {
524 slave_dev
->vlan_rx_register(slave_dev
, bond
->vlgrp
);
527 if (!(slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) ||
528 !(slave_dev
->vlan_rx_add_vid
)) {
532 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
533 slave_dev
->vlan_rx_add_vid(slave_dev
, vlan
->vlan_id
);
537 write_unlock_bh(&bond
->lock
);
540 static void bond_del_vlans_from_slave(struct bonding
*bond
, struct net_device
*slave_dev
)
542 struct vlan_entry
*vlan
;
543 struct net_device
*vlan_dev
;
545 write_lock_bh(&bond
->lock
);
547 if (list_empty(&bond
->vlan_list
)) {
551 if (!(slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) ||
552 !(slave_dev
->vlan_rx_kill_vid
)) {
556 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
557 /* Save and then restore vlan_dev in the grp array,
558 * since the slave's driver might clear it.
560 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
561 slave_dev
->vlan_rx_kill_vid(slave_dev
, vlan
->vlan_id
);
562 vlan_group_set_device(bond
->vlgrp
, vlan
->vlan_id
, vlan_dev
);
566 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
567 slave_dev
->vlan_rx_register
) {
568 slave_dev
->vlan_rx_register(slave_dev
, NULL
);
572 write_unlock_bh(&bond
->lock
);
575 /*------------------------------- Link status -------------------------------*/
578 * Set the carrier state for the master according to the state of its
579 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
580 * do special 802.3ad magic.
582 * Returns zero if carrier state does not change, nonzero if it does.
584 static int bond_set_carrier(struct bonding
*bond
)
589 if (bond
->slave_cnt
== 0)
592 if (bond
->params
.mode
== BOND_MODE_8023AD
)
593 return bond_3ad_set_carrier(bond
);
595 bond_for_each_slave(bond
, slave
, i
) {
596 if (slave
->link
== BOND_LINK_UP
) {
597 if (!netif_carrier_ok(bond
->dev
)) {
598 netif_carrier_on(bond
->dev
);
606 if (netif_carrier_ok(bond
->dev
)) {
607 netif_carrier_off(bond
->dev
);
614 * Get link speed and duplex from the slave's base driver
615 * using ethtool. If for some reason the call fails or the
616 * values are invalid, fake speed and duplex to 100/Full
619 static int bond_update_speed_duplex(struct slave
*slave
)
621 struct net_device
*slave_dev
= slave
->dev
;
622 struct ethtool_cmd etool
;
625 /* Fake speed and duplex */
626 slave
->speed
= SPEED_100
;
627 slave
->duplex
= DUPLEX_FULL
;
629 if (!slave_dev
->ethtool_ops
|| !slave_dev
->ethtool_ops
->get_settings
)
632 res
= slave_dev
->ethtool_ops
->get_settings(slave_dev
, &etool
);
636 switch (etool
.speed
) {
646 switch (etool
.duplex
) {
654 slave
->speed
= etool
.speed
;
655 slave
->duplex
= etool
.duplex
;
661 * if <dev> supports MII link status reporting, check its link status.
663 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
664 * depening upon the setting of the use_carrier parameter.
666 * Return either BMSR_LSTATUS, meaning that the link is up (or we
667 * can't tell and just pretend it is), or 0, meaning that the link is
670 * If reporting is non-zero, instead of faking link up, return -1 if
671 * both ETHTOOL and MII ioctls fail (meaning the device does not
672 * support them). If use_carrier is set, return whatever it says.
673 * It'd be nice if there was a good way to tell if a driver supports
674 * netif_carrier, but there really isn't.
676 static int bond_check_dev_link(struct bonding
*bond
, struct net_device
*slave_dev
, int reporting
)
678 static int (* ioctl
)(struct net_device
*, struct ifreq
*, int);
680 struct mii_ioctl_data
*mii
;
682 if (bond
->params
.use_carrier
) {
683 return netif_carrier_ok(slave_dev
) ? BMSR_LSTATUS
: 0;
686 ioctl
= slave_dev
->do_ioctl
;
688 /* TODO: set pointer to correct ioctl on a per team member */
689 /* bases to make this more efficient. that is, once */
690 /* we determine the correct ioctl, we will always */
691 /* call it and not the others for that team */
695 * We cannot assume that SIOCGMIIPHY will also read a
696 * register; not all network drivers (e.g., e100)
700 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
701 strncpy(ifr
.ifr_name
, slave_dev
->name
, IFNAMSIZ
);
703 if (IOCTL(slave_dev
, &ifr
, SIOCGMIIPHY
) == 0) {
704 mii
->reg_num
= MII_BMSR
;
705 if (IOCTL(slave_dev
, &ifr
, SIOCGMIIREG
) == 0) {
706 return (mii
->val_out
& BMSR_LSTATUS
);
712 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
713 * attempt to get link status from it if the above MII ioctls fail.
715 if (slave_dev
->ethtool_ops
) {
716 if (slave_dev
->ethtool_ops
->get_link
) {
719 link
= slave_dev
->ethtool_ops
->get_link(slave_dev
);
721 return link
? BMSR_LSTATUS
: 0;
726 * If reporting, report that either there's no dev->do_ioctl,
727 * or both SIOCGMIIREG and get_link failed (meaning that we
728 * cannot report link status). If not reporting, pretend
731 return (reporting
? -1 : BMSR_LSTATUS
);
734 /*----------------------------- Multicast list ------------------------------*/
737 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
739 static inline int bond_is_dmi_same(struct dev_mc_list
*dmi1
, struct dev_mc_list
*dmi2
)
741 return memcmp(dmi1
->dmi_addr
, dmi2
->dmi_addr
, dmi1
->dmi_addrlen
) == 0 &&
742 dmi1
->dmi_addrlen
== dmi2
->dmi_addrlen
;
746 * returns dmi entry if found, NULL otherwise
748 static struct dev_mc_list
*bond_mc_list_find_dmi(struct dev_mc_list
*dmi
, struct dev_mc_list
*mc_list
)
750 struct dev_mc_list
*idmi
;
752 for (idmi
= mc_list
; idmi
; idmi
= idmi
->next
) {
753 if (bond_is_dmi_same(dmi
, idmi
)) {
762 * Push the promiscuity flag down to appropriate slaves
764 static void bond_set_promiscuity(struct bonding
*bond
, int inc
)
766 if (USES_PRIMARY(bond
->params
.mode
)) {
767 /* write lock already acquired */
768 if (bond
->curr_active_slave
) {
769 dev_set_promiscuity(bond
->curr_active_slave
->dev
, inc
);
774 bond_for_each_slave(bond
, slave
, i
) {
775 dev_set_promiscuity(slave
->dev
, inc
);
781 * Push the allmulti flag down to all slaves
783 static void bond_set_allmulti(struct bonding
*bond
, int inc
)
785 if (USES_PRIMARY(bond
->params
.mode
)) {
786 /* write lock already acquired */
787 if (bond
->curr_active_slave
) {
788 dev_set_allmulti(bond
->curr_active_slave
->dev
, inc
);
793 bond_for_each_slave(bond
, slave
, i
) {
794 dev_set_allmulti(slave
->dev
, inc
);
800 * Add a Multicast address to slaves
803 static void bond_mc_add(struct bonding
*bond
, void *addr
, int alen
)
805 if (USES_PRIMARY(bond
->params
.mode
)) {
806 /* write lock already acquired */
807 if (bond
->curr_active_slave
) {
808 dev_mc_add(bond
->curr_active_slave
->dev
, addr
, alen
, 0);
813 bond_for_each_slave(bond
, slave
, i
) {
814 dev_mc_add(slave
->dev
, addr
, alen
, 0);
820 * Remove a multicast address from slave
823 static void bond_mc_delete(struct bonding
*bond
, void *addr
, int alen
)
825 if (USES_PRIMARY(bond
->params
.mode
)) {
826 /* write lock already acquired */
827 if (bond
->curr_active_slave
) {
828 dev_mc_delete(bond
->curr_active_slave
->dev
, addr
, alen
, 0);
833 bond_for_each_slave(bond
, slave
, i
) {
834 dev_mc_delete(slave
->dev
, addr
, alen
, 0);
841 * Retrieve the list of registered multicast addresses for the bonding
842 * device and retransmit an IGMP JOIN request to the current active
845 static void bond_resend_igmp_join_requests(struct bonding
*bond
)
847 struct in_device
*in_dev
;
848 struct ip_mc_list
*im
;
851 in_dev
= __in_dev_get_rcu(bond
->dev
);
853 for (im
= in_dev
->mc_list
; im
; im
= im
->next
) {
854 ip_mc_rejoin_group(im
);
862 * Totally destroys the mc_list in bond
864 static void bond_mc_list_destroy(struct bonding
*bond
)
866 struct dev_mc_list
*dmi
;
870 bond
->mc_list
= dmi
->next
;
874 bond
->mc_list
= NULL
;
878 * Copy all the Multicast addresses from src to the bonding device dst
880 static int bond_mc_list_copy(struct dev_mc_list
*mc_list
, struct bonding
*bond
,
883 struct dev_mc_list
*dmi
, *new_dmi
;
885 for (dmi
= mc_list
; dmi
; dmi
= dmi
->next
) {
886 new_dmi
= kmalloc(sizeof(struct dev_mc_list
), gfp_flag
);
889 /* FIXME: Potential memory leak !!! */
893 new_dmi
->next
= bond
->mc_list
;
894 bond
->mc_list
= new_dmi
;
895 new_dmi
->dmi_addrlen
= dmi
->dmi_addrlen
;
896 memcpy(new_dmi
->dmi_addr
, dmi
->dmi_addr
, dmi
->dmi_addrlen
);
897 new_dmi
->dmi_users
= dmi
->dmi_users
;
898 new_dmi
->dmi_gusers
= dmi
->dmi_gusers
;
905 * flush all members of flush->mc_list from device dev->mc_list
907 static void bond_mc_list_flush(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
909 struct bonding
*bond
= bond_dev
->priv
;
910 struct dev_mc_list
*dmi
;
912 for (dmi
= bond_dev
->mc_list
; dmi
; dmi
= dmi
->next
) {
913 dev_mc_delete(slave_dev
, dmi
->dmi_addr
, dmi
->dmi_addrlen
, 0);
916 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
917 /* del lacpdu mc addr from mc list */
918 u8 lacpdu_multicast
[ETH_ALEN
] = MULTICAST_LACPDU_ADDR
;
920 dev_mc_delete(slave_dev
, lacpdu_multicast
, ETH_ALEN
, 0);
924 /*--------------------------- Active slave change ---------------------------*/
927 * Update the mc list and multicast-related flags for the new and
928 * old active slaves (if any) according to the multicast mode, and
929 * promiscuous flags unconditionally.
931 static void bond_mc_swap(struct bonding
*bond
, struct slave
*new_active
, struct slave
*old_active
)
933 struct dev_mc_list
*dmi
;
935 if (!USES_PRIMARY(bond
->params
.mode
)) {
936 /* nothing to do - mc list is already up-to-date on
943 if (bond
->dev
->flags
& IFF_PROMISC
) {
944 dev_set_promiscuity(old_active
->dev
, -1);
947 if (bond
->dev
->flags
& IFF_ALLMULTI
) {
948 dev_set_allmulti(old_active
->dev
, -1);
951 for (dmi
= bond
->dev
->mc_list
; dmi
; dmi
= dmi
->next
) {
952 dev_mc_delete(old_active
->dev
, dmi
->dmi_addr
, dmi
->dmi_addrlen
, 0);
957 if (bond
->dev
->flags
& IFF_PROMISC
) {
958 dev_set_promiscuity(new_active
->dev
, 1);
961 if (bond
->dev
->flags
& IFF_ALLMULTI
) {
962 dev_set_allmulti(new_active
->dev
, 1);
965 for (dmi
= bond
->dev
->mc_list
; dmi
; dmi
= dmi
->next
) {
966 dev_mc_add(new_active
->dev
, dmi
->dmi_addr
, dmi
->dmi_addrlen
, 0);
968 bond_resend_igmp_join_requests(bond
);
973 * find_best_interface - select the best available slave to be the active one
974 * @bond: our bonding struct
976 * Warning: Caller must hold curr_slave_lock for writing.
978 static struct slave
*bond_find_best_slave(struct bonding
*bond
)
980 struct slave
*new_active
, *old_active
;
981 struct slave
*bestslave
= NULL
;
982 int mintime
= bond
->params
.updelay
;
985 new_active
= old_active
= bond
->curr_active_slave
;
987 if (!new_active
) { /* there were no active slaves left */
988 if (bond
->slave_cnt
> 0) { /* found one slave */
989 new_active
= bond
->first_slave
;
991 return NULL
; /* still no slave, return NULL */
995 /* first try the primary link; if arping, a link must tx/rx traffic
996 * before it can be considered the curr_active_slave - also, we would skip
997 * slaves between the curr_active_slave and primary_slave that may be up
1000 if ((bond
->primary_slave
) &&
1001 (!bond
->params
.arp_interval
) &&
1002 (IS_UP(bond
->primary_slave
->dev
))) {
1003 new_active
= bond
->primary_slave
;
1006 /* remember where to stop iterating over the slaves */
1007 old_active
= new_active
;
1009 bond_for_each_slave_from(bond
, new_active
, i
, old_active
) {
1010 if (IS_UP(new_active
->dev
)) {
1011 if (new_active
->link
== BOND_LINK_UP
) {
1013 } else if (new_active
->link
== BOND_LINK_BACK
) {
1014 /* link up, but waiting for stabilization */
1015 if (new_active
->delay
< mintime
) {
1016 mintime
= new_active
->delay
;
1017 bestslave
= new_active
;
1027 * change_active_interface - change the active slave into the specified one
1028 * @bond: our bonding struct
1029 * @new: the new slave to make the active one
1031 * Set the new slave to the bond's settings and unset them on the old
1032 * curr_active_slave.
1033 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1035 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1036 * because it is apparently the best available slave we have, even though its
1037 * updelay hasn't timed out yet.
1039 * Warning: Caller must hold curr_slave_lock for writing.
1041 void bond_change_active_slave(struct bonding
*bond
, struct slave
*new_active
)
1043 struct slave
*old_active
= bond
->curr_active_slave
;
1045 if (old_active
== new_active
) {
1050 if (new_active
->link
== BOND_LINK_BACK
) {
1051 if (USES_PRIMARY(bond
->params
.mode
)) {
1052 printk(KERN_INFO DRV_NAME
1053 ": %s: making interface %s the new "
1054 "active one %d ms earlier.\n",
1055 bond
->dev
->name
, new_active
->dev
->name
,
1056 (bond
->params
.updelay
- new_active
->delay
) * bond
->params
.miimon
);
1059 new_active
->delay
= 0;
1060 new_active
->link
= BOND_LINK_UP
;
1061 new_active
->jiffies
= jiffies
;
1063 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1064 bond_3ad_handle_link_change(new_active
, BOND_LINK_UP
);
1067 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
1068 (bond
->params
.mode
== BOND_MODE_ALB
)) {
1069 bond_alb_handle_link_change(bond
, new_active
, BOND_LINK_UP
);
1072 if (USES_PRIMARY(bond
->params
.mode
)) {
1073 printk(KERN_INFO DRV_NAME
1074 ": %s: making interface %s the new "
1076 bond
->dev
->name
, new_active
->dev
->name
);
1081 if (USES_PRIMARY(bond
->params
.mode
)) {
1082 bond_mc_swap(bond
, new_active
, old_active
);
1085 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
1086 (bond
->params
.mode
== BOND_MODE_ALB
)) {
1087 bond_alb_handle_active_change(bond
, new_active
);
1089 bond_set_slave_inactive_flags(old_active
);
1091 bond_set_slave_active_flags(new_active
);
1093 bond
->curr_active_slave
= new_active
;
1096 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
) {
1098 bond_set_slave_inactive_flags(old_active
);
1102 bond_set_slave_active_flags(new_active
);
1105 /* when bonding does not set the slave MAC address, the bond MAC
1106 * address is the one of the active slave.
1108 if (new_active
&& bond
->params
.fail_over_mac
)
1109 memcpy(bond
->dev
->dev_addr
, new_active
->dev
->dev_addr
,
1110 new_active
->dev
->addr_len
);
1111 if (bond
->curr_active_slave
&&
1112 test_bit(__LINK_STATE_LINKWATCH_PENDING
,
1113 &bond
->curr_active_slave
->dev
->state
)) {
1114 dprintk("delaying gratuitous arp on %s\n",
1115 bond
->curr_active_slave
->dev
->name
);
1116 bond
->send_grat_arp
= 1;
1118 bond_send_gratuitous_arp(bond
);
1123 * bond_select_active_slave - select a new active slave, if needed
1124 * @bond: our bonding struct
1126 * This functions shoud be called when one of the following occurs:
1127 * - The old curr_active_slave has been released or lost its link.
1128 * - The primary_slave has got its link back.
1129 * - A slave has got its link back and there's no old curr_active_slave.
1131 * Warning: Caller must hold curr_slave_lock for writing.
1133 void bond_select_active_slave(struct bonding
*bond
)
1135 struct slave
*best_slave
;
1138 best_slave
= bond_find_best_slave(bond
);
1139 if (best_slave
!= bond
->curr_active_slave
) {
1140 bond_change_active_slave(bond
, best_slave
);
1141 rv
= bond_set_carrier(bond
);
1145 if (netif_carrier_ok(bond
->dev
)) {
1146 printk(KERN_INFO DRV_NAME
1147 ": %s: first active interface up!\n",
1150 printk(KERN_INFO DRV_NAME
": %s: "
1151 "now running without any active interface !\n",
1157 /*--------------------------- slave list handling ---------------------------*/
1160 * This function attaches the slave to the end of list.
1162 * bond->lock held for writing by caller.
1164 static void bond_attach_slave(struct bonding
*bond
, struct slave
*new_slave
)
1166 if (bond
->first_slave
== NULL
) { /* attaching the first slave */
1167 new_slave
->next
= new_slave
;
1168 new_slave
->prev
= new_slave
;
1169 bond
->first_slave
= new_slave
;
1171 new_slave
->next
= bond
->first_slave
;
1172 new_slave
->prev
= bond
->first_slave
->prev
;
1173 new_slave
->next
->prev
= new_slave
;
1174 new_slave
->prev
->next
= new_slave
;
1181 * This function detaches the slave from the list.
1182 * WARNING: no check is made to verify if the slave effectively
1183 * belongs to <bond>.
1184 * Nothing is freed on return, structures are just unchained.
1185 * If any slave pointer in bond was pointing to <slave>,
1186 * it should be changed by the calling function.
1188 * bond->lock held for writing by caller.
1190 static void bond_detach_slave(struct bonding
*bond
, struct slave
*slave
)
1193 slave
->next
->prev
= slave
->prev
;
1197 slave
->prev
->next
= slave
->next
;
1200 if (bond
->first_slave
== slave
) { /* slave is the first slave */
1201 if (bond
->slave_cnt
> 1) { /* there are more slave */
1202 bond
->first_slave
= slave
->next
;
1204 bond
->first_slave
= NULL
; /* slave was the last one */
1213 /*---------------------------------- IOCTL ----------------------------------*/
1215 static int bond_sethwaddr(struct net_device
*bond_dev
,
1216 struct net_device
*slave_dev
)
1218 dprintk("bond_dev=%p\n", bond_dev
);
1219 dprintk("slave_dev=%p\n", slave_dev
);
1220 dprintk("slave_dev->addr_len=%d\n", slave_dev
->addr_len
);
1221 memcpy(bond_dev
->dev_addr
, slave_dev
->dev_addr
, slave_dev
->addr_len
);
1225 #define BOND_VLAN_FEATURES \
1226 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1227 NETIF_F_HW_VLAN_FILTER)
1230 * Compute the common dev->feature set available to all slaves. Some
1231 * feature bits are managed elsewhere, so preserve those feature bits
1232 * on the master device.
1234 static int bond_compute_features(struct bonding
*bond
)
1236 struct slave
*slave
;
1237 struct net_device
*bond_dev
= bond
->dev
;
1238 unsigned long features
= bond_dev
->features
;
1239 unsigned short max_hard_header_len
= max((u16
)ETH_HLEN
,
1240 bond_dev
->hard_header_len
);
1243 features
&= ~(NETIF_F_ALL_CSUM
| BOND_VLAN_FEATURES
);
1244 features
|= NETIF_F_SG
| NETIF_F_FRAGLIST
| NETIF_F_HIGHDMA
|
1245 NETIF_F_GSO_MASK
| NETIF_F_NO_CSUM
;
1247 bond_for_each_slave(bond
, slave
, i
) {
1248 features
= netdev_compute_features(features
,
1249 slave
->dev
->features
);
1250 if (slave
->dev
->hard_header_len
> max_hard_header_len
)
1251 max_hard_header_len
= slave
->dev
->hard_header_len
;
1254 features
|= (bond_dev
->features
& BOND_VLAN_FEATURES
);
1255 bond_dev
->features
= features
;
1256 bond_dev
->hard_header_len
= max_hard_header_len
;
1262 static void bond_setup_by_slave(struct net_device
*bond_dev
,
1263 struct net_device
*slave_dev
)
1265 struct bonding
*bond
= bond_dev
->priv
;
1267 bond_dev
->neigh_setup
= slave_dev
->neigh_setup
;
1268 bond_dev
->header_ops
= slave_dev
->header_ops
;
1270 bond_dev
->type
= slave_dev
->type
;
1271 bond_dev
->hard_header_len
= slave_dev
->hard_header_len
;
1272 bond_dev
->addr_len
= slave_dev
->addr_len
;
1274 memcpy(bond_dev
->broadcast
, slave_dev
->broadcast
,
1275 slave_dev
->addr_len
);
1276 bond
->setup_by_slave
= 1;
1279 /* enslave device <slave> to bond device <master> */
1280 int bond_enslave(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1282 struct bonding
*bond
= bond_dev
->priv
;
1283 struct slave
*new_slave
= NULL
;
1284 struct dev_mc_list
*dmi
;
1285 struct sockaddr addr
;
1287 int old_features
= bond_dev
->features
;
1290 if (!bond
->params
.use_carrier
&& slave_dev
->ethtool_ops
== NULL
&&
1291 slave_dev
->do_ioctl
== NULL
) {
1292 printk(KERN_WARNING DRV_NAME
1293 ": %s: Warning: no link monitoring support for %s\n",
1294 bond_dev
->name
, slave_dev
->name
);
1297 /* bond must be initialized by bond_open() before enslaving */
1298 if (!(bond_dev
->flags
& IFF_UP
)) {
1299 printk(KERN_WARNING DRV_NAME
1300 " %s: master_dev is not up in bond_enslave\n",
1304 /* already enslaved */
1305 if (slave_dev
->flags
& IFF_SLAVE
) {
1306 dprintk("Error, Device was already enslaved\n");
1310 /* vlan challenged mutual exclusion */
1311 /* no need to lock since we're protected by rtnl_lock */
1312 if (slave_dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
1313 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev
->name
);
1314 if (!list_empty(&bond
->vlan_list
)) {
1315 printk(KERN_ERR DRV_NAME
1316 ": %s: Error: cannot enslave VLAN "
1317 "challenged slave %s on VLAN enabled "
1318 "bond %s\n", bond_dev
->name
, slave_dev
->name
,
1322 printk(KERN_WARNING DRV_NAME
1323 ": %s: Warning: enslaved VLAN challenged "
1324 "slave %s. Adding VLANs will be blocked as "
1325 "long as %s is part of bond %s\n",
1326 bond_dev
->name
, slave_dev
->name
, slave_dev
->name
,
1328 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1331 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev
->name
);
1332 if (bond
->slave_cnt
== 0) {
1333 /* First slave, and it is not VLAN challenged,
1334 * so remove the block of adding VLANs over the bond.
1336 bond_dev
->features
&= ~NETIF_F_VLAN_CHALLENGED
;
1341 * Old ifenslave binaries are no longer supported. These can
1342 * be identified with moderate accurary by the state of the slave:
1343 * the current ifenslave will set the interface down prior to
1344 * enslaving it; the old ifenslave will not.
1346 if ((slave_dev
->flags
& IFF_UP
)) {
1347 printk(KERN_ERR DRV_NAME
": %s is up. "
1348 "This may be due to an out of date ifenslave.\n",
1351 goto err_undo_flags
;
1354 /* set bonding device ether type by slave - bonding netdevices are
1355 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1356 * there is a need to override some of the type dependent attribs/funcs.
1358 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1359 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1361 if (bond
->slave_cnt
== 0) {
1362 if (slave_dev
->type
!= ARPHRD_ETHER
)
1363 bond_setup_by_slave(bond_dev
, slave_dev
);
1364 } else if (bond_dev
->type
!= slave_dev
->type
) {
1365 printk(KERN_ERR DRV_NAME
": %s ether type (%d) is different "
1366 "from other slaves (%d), can not enslave it.\n",
1368 slave_dev
->type
, bond_dev
->type
);
1370 goto err_undo_flags
;
1373 if (slave_dev
->set_mac_address
== NULL
) {
1374 if (bond
->slave_cnt
== 0) {
1375 printk(KERN_WARNING DRV_NAME
1376 ": %s: Warning: The first slave device "
1377 "specified does not support setting the MAC "
1378 "address. Enabling the fail_over_mac option.",
1380 bond
->params
.fail_over_mac
= 1;
1381 } else if (!bond
->params
.fail_over_mac
) {
1382 printk(KERN_ERR DRV_NAME
1383 ": %s: Error: The slave device specified "
1384 "does not support setting the MAC address, "
1385 "but fail_over_mac is not enabled.\n"
1388 goto err_undo_flags
;
1392 new_slave
= kzalloc(sizeof(struct slave
), GFP_KERNEL
);
1395 goto err_undo_flags
;
1398 /* save slave's original flags before calling
1399 * netdev_set_master and dev_open
1401 new_slave
->original_flags
= slave_dev
->flags
;
1404 * Save slave's original ("permanent") mac address for modes
1405 * that need it, and for restoring it upon release, and then
1406 * set it to the master's address
1408 memcpy(new_slave
->perm_hwaddr
, slave_dev
->dev_addr
, ETH_ALEN
);
1410 if (!bond
->params
.fail_over_mac
) {
1412 * Set slave to master's mac address. The application already
1413 * set the master's mac address to that of the first slave
1415 memcpy(addr
.sa_data
, bond_dev
->dev_addr
, bond_dev
->addr_len
);
1416 addr
.sa_family
= slave_dev
->type
;
1417 res
= dev_set_mac_address(slave_dev
, &addr
);
1419 dprintk("Error %d calling set_mac_address\n", res
);
1424 res
= netdev_set_master(slave_dev
, bond_dev
);
1426 dprintk("Error %d calling netdev_set_master\n", res
);
1429 /* open the slave since the application closed it */
1430 res
= dev_open(slave_dev
);
1432 dprintk("Openning slave %s failed\n", slave_dev
->name
);
1433 goto err_restore_mac
;
1436 new_slave
->dev
= slave_dev
;
1437 slave_dev
->priv_flags
|= IFF_BONDING
;
1439 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
1440 (bond
->params
.mode
== BOND_MODE_ALB
)) {
1441 /* bond_alb_init_slave() must be called before all other stages since
1442 * it might fail and we do not want to have to undo everything
1444 res
= bond_alb_init_slave(bond
, new_slave
);
1446 goto err_unset_master
;
1450 /* If the mode USES_PRIMARY, then the new slave gets the
1451 * master's promisc (and mc) settings only if it becomes the
1452 * curr_active_slave, and that is taken care of later when calling
1453 * bond_change_active()
1455 if (!USES_PRIMARY(bond
->params
.mode
)) {
1456 /* set promiscuity level to new slave */
1457 if (bond_dev
->flags
& IFF_PROMISC
) {
1458 dev_set_promiscuity(slave_dev
, 1);
1461 /* set allmulti level to new slave */
1462 if (bond_dev
->flags
& IFF_ALLMULTI
) {
1463 dev_set_allmulti(slave_dev
, 1);
1466 /* upload master's mc_list to new slave */
1467 for (dmi
= bond_dev
->mc_list
; dmi
; dmi
= dmi
->next
) {
1468 dev_mc_add (slave_dev
, dmi
->dmi_addr
, dmi
->dmi_addrlen
, 0);
1472 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1473 /* add lacpdu mc addr to mc list */
1474 u8 lacpdu_multicast
[ETH_ALEN
] = MULTICAST_LACPDU_ADDR
;
1476 dev_mc_add(slave_dev
, lacpdu_multicast
, ETH_ALEN
, 0);
1479 bond_add_vlans_on_slave(bond
, slave_dev
);
1481 write_lock_bh(&bond
->lock
);
1483 bond_attach_slave(bond
, new_slave
);
1485 new_slave
->delay
= 0;
1486 new_slave
->link_failure_count
= 0;
1488 bond_compute_features(bond
);
1490 new_slave
->last_arp_rx
= jiffies
;
1492 if (bond
->params
.miimon
&& !bond
->params
.use_carrier
) {
1493 link_reporting
= bond_check_dev_link(bond
, slave_dev
, 1);
1495 if ((link_reporting
== -1) && !bond
->params
.arp_interval
) {
1497 * miimon is set but a bonded network driver
1498 * does not support ETHTOOL/MII and
1499 * arp_interval is not set. Note: if
1500 * use_carrier is enabled, we will never go
1501 * here (because netif_carrier is always
1502 * supported); thus, we don't need to change
1503 * the messages for netif_carrier.
1505 printk(KERN_WARNING DRV_NAME
1506 ": %s: Warning: MII and ETHTOOL support not "
1507 "available for interface %s, and "
1508 "arp_interval/arp_ip_target module parameters "
1509 "not specified, thus bonding will not detect "
1510 "link failures! see bonding.txt for details.\n",
1511 bond_dev
->name
, slave_dev
->name
);
1512 } else if (link_reporting
== -1) {
1513 /* unable get link status using mii/ethtool */
1514 printk(KERN_WARNING DRV_NAME
1515 ": %s: Warning: can't get link status from "
1516 "interface %s; the network driver associated "
1517 "with this interface does not support MII or "
1518 "ETHTOOL link status reporting, thus miimon "
1519 "has no effect on this interface.\n",
1520 bond_dev
->name
, slave_dev
->name
);
1524 /* check for initial state */
1525 if (!bond
->params
.miimon
||
1526 (bond_check_dev_link(bond
, slave_dev
, 0) == BMSR_LSTATUS
)) {
1527 if (bond
->params
.updelay
) {
1528 dprintk("Initial state of slave_dev is "
1529 "BOND_LINK_BACK\n");
1530 new_slave
->link
= BOND_LINK_BACK
;
1531 new_slave
->delay
= bond
->params
.updelay
;
1533 dprintk("Initial state of slave_dev is "
1535 new_slave
->link
= BOND_LINK_UP
;
1537 new_slave
->jiffies
= jiffies
;
1539 dprintk("Initial state of slave_dev is "
1540 "BOND_LINK_DOWN\n");
1541 new_slave
->link
= BOND_LINK_DOWN
;
1544 if (bond_update_speed_duplex(new_slave
) &&
1545 (new_slave
->link
!= BOND_LINK_DOWN
)) {
1546 printk(KERN_WARNING DRV_NAME
1547 ": %s: Warning: failed to get speed and duplex from %s, "
1548 "assumed to be 100Mb/sec and Full.\n",
1549 bond_dev
->name
, new_slave
->dev
->name
);
1551 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1552 printk(KERN_WARNING DRV_NAME
1553 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1554 "support in base driver for proper aggregator "
1555 "selection.\n", bond_dev
->name
);
1559 if (USES_PRIMARY(bond
->params
.mode
) && bond
->params
.primary
[0]) {
1560 /* if there is a primary slave, remember it */
1561 if (strcmp(bond
->params
.primary
, new_slave
->dev
->name
) == 0) {
1562 bond
->primary_slave
= new_slave
;
1566 switch (bond
->params
.mode
) {
1567 case BOND_MODE_ACTIVEBACKUP
:
1568 bond_set_slave_inactive_flags(new_slave
);
1569 bond_select_active_slave(bond
);
1571 case BOND_MODE_8023AD
:
1572 /* in 802.3ad mode, the internal mechanism
1573 * will activate the slaves in the selected
1576 bond_set_slave_inactive_flags(new_slave
);
1577 /* if this is the first slave */
1578 if (bond
->slave_cnt
== 1) {
1579 SLAVE_AD_INFO(new_slave
).id
= 1;
1580 /* Initialize AD with the number of times that the AD timer is called in 1 second
1581 * can be called only after the mac address of the bond is set
1583 bond_3ad_initialize(bond
, 1000/AD_TIMER_INTERVAL
,
1584 bond
->params
.lacp_fast
);
1586 SLAVE_AD_INFO(new_slave
).id
=
1587 SLAVE_AD_INFO(new_slave
->prev
).id
+ 1;
1590 bond_3ad_bind_slave(new_slave
);
1594 new_slave
->state
= BOND_STATE_ACTIVE
;
1595 bond_set_slave_inactive_flags(new_slave
);
1598 dprintk("This slave is always active in trunk mode\n");
1600 /* always active in trunk mode */
1601 new_slave
->state
= BOND_STATE_ACTIVE
;
1603 /* In trunking mode there is little meaning to curr_active_slave
1604 * anyway (it holds no special properties of the bond device),
1605 * so we can change it without calling change_active_interface()
1607 if (!bond
->curr_active_slave
) {
1608 bond
->curr_active_slave
= new_slave
;
1611 } /* switch(bond_mode) */
1613 bond_set_carrier(bond
);
1615 write_unlock_bh(&bond
->lock
);
1617 res
= bond_create_slave_symlinks(bond_dev
, slave_dev
);
1619 goto err_unset_master
;
1621 printk(KERN_INFO DRV_NAME
1622 ": %s: enslaving %s as a%s interface with a%s link.\n",
1623 bond_dev
->name
, slave_dev
->name
,
1624 new_slave
->state
== BOND_STATE_ACTIVE
? "n active" : " backup",
1625 new_slave
->link
!= BOND_LINK_DOWN
? "n up" : " down");
1627 /* enslave is successful */
1630 /* Undo stages on error */
1632 netdev_set_master(slave_dev
, NULL
);
1635 dev_close(slave_dev
);
1638 if (!bond
->params
.fail_over_mac
) {
1639 memcpy(addr
.sa_data
, new_slave
->perm_hwaddr
, ETH_ALEN
);
1640 addr
.sa_family
= slave_dev
->type
;
1641 dev_set_mac_address(slave_dev
, &addr
);
1648 bond_dev
->features
= old_features
;
1654 * Try to release the slave device <slave> from the bond device <master>
1655 * It is legal to access curr_active_slave without a lock because all the function
1658 * The rules for slave state should be:
1659 * for Active/Backup:
1660 * Active stays on all backups go down
1661 * for Bonded connections:
1662 * The first up interface should be left on and all others downed.
1664 int bond_release(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1666 struct bonding
*bond
= bond_dev
->priv
;
1667 struct slave
*slave
, *oldcurrent
;
1668 struct sockaddr addr
;
1669 int mac_addr_differ
;
1670 DECLARE_MAC_BUF(mac
);
1672 /* slave is not a slave or master is not master of this slave */
1673 if (!(slave_dev
->flags
& IFF_SLAVE
) ||
1674 (slave_dev
->master
!= bond_dev
)) {
1675 printk(KERN_ERR DRV_NAME
1676 ": %s: Error: cannot release %s.\n",
1677 bond_dev
->name
, slave_dev
->name
);
1681 write_lock_bh(&bond
->lock
);
1683 slave
= bond_get_slave_by_dev(bond
, slave_dev
);
1685 /* not a slave of this bond */
1686 printk(KERN_INFO DRV_NAME
1687 ": %s: %s not enslaved\n",
1688 bond_dev
->name
, slave_dev
->name
);
1689 write_unlock_bh(&bond
->lock
);
1693 mac_addr_differ
= memcmp(bond_dev
->dev_addr
,
1696 if (!mac_addr_differ
&& (bond
->slave_cnt
> 1)) {
1697 printk(KERN_WARNING DRV_NAME
1698 ": %s: Warning: the permanent HWaddr of %s - "
1699 "%s - is still in use by %s. "
1700 "Set the HWaddr of %s to a different address "
1701 "to avoid conflicts.\n",
1704 print_mac(mac
, slave
->perm_hwaddr
),
1709 /* Inform AD package of unbinding of slave. */
1710 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1711 /* must be called before the slave is
1712 * detached from the list
1714 bond_3ad_unbind_slave(slave
);
1717 printk(KERN_INFO DRV_NAME
1718 ": %s: releasing %s interface %s\n",
1720 (slave
->state
== BOND_STATE_ACTIVE
)
1721 ? "active" : "backup",
1724 oldcurrent
= bond
->curr_active_slave
;
1726 bond
->current_arp_slave
= NULL
;
1728 /* release the slave from its bond */
1729 bond_detach_slave(bond
, slave
);
1731 bond_compute_features(bond
);
1733 if (bond
->primary_slave
== slave
) {
1734 bond
->primary_slave
= NULL
;
1737 if (oldcurrent
== slave
) {
1738 bond_change_active_slave(bond
, NULL
);
1741 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
1742 (bond
->params
.mode
== BOND_MODE_ALB
)) {
1743 /* Must be called only after the slave has been
1744 * detached from the list and the curr_active_slave
1745 * has been cleared (if our_slave == old_current),
1746 * but before a new active slave is selected.
1748 bond_alb_deinit_slave(bond
, slave
);
1751 if (oldcurrent
== slave
) {
1753 * Note that we hold RTNL over this sequence, so there
1754 * is no concern that another slave add/remove event
1757 write_unlock_bh(&bond
->lock
);
1758 read_lock(&bond
->lock
);
1759 write_lock_bh(&bond
->curr_slave_lock
);
1761 bond_select_active_slave(bond
);
1763 write_unlock_bh(&bond
->curr_slave_lock
);
1764 read_unlock(&bond
->lock
);
1765 write_lock_bh(&bond
->lock
);
1768 if (bond
->slave_cnt
== 0) {
1769 bond_set_carrier(bond
);
1771 /* if the last slave was removed, zero the mac address
1772 * of the master so it will be set by the application
1773 * to the mac address of the first slave
1775 memset(bond_dev
->dev_addr
, 0, bond_dev
->addr_len
);
1777 if (list_empty(&bond
->vlan_list
)) {
1778 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1780 printk(KERN_WARNING DRV_NAME
1781 ": %s: Warning: clearing HW address of %s while it "
1782 "still has VLANs.\n",
1783 bond_dev
->name
, bond_dev
->name
);
1784 printk(KERN_WARNING DRV_NAME
1785 ": %s: When re-adding slaves, make sure the bond's "
1786 "HW address matches its VLANs'.\n",
1789 } else if ((bond_dev
->features
& NETIF_F_VLAN_CHALLENGED
) &&
1790 !bond_has_challenged_slaves(bond
)) {
1791 printk(KERN_INFO DRV_NAME
1792 ": %s: last VLAN challenged slave %s "
1793 "left bond %s. VLAN blocking is removed\n",
1794 bond_dev
->name
, slave_dev
->name
, bond_dev
->name
);
1795 bond_dev
->features
&= ~NETIF_F_VLAN_CHALLENGED
;
1798 write_unlock_bh(&bond
->lock
);
1800 /* must do this from outside any spinlocks */
1801 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
1803 bond_del_vlans_from_slave(bond
, slave_dev
);
1805 /* If the mode USES_PRIMARY, then we should only remove its
1806 * promisc and mc settings if it was the curr_active_slave, but that was
1807 * already taken care of above when we detached the slave
1809 if (!USES_PRIMARY(bond
->params
.mode
)) {
1810 /* unset promiscuity level from slave */
1811 if (bond_dev
->flags
& IFF_PROMISC
) {
1812 dev_set_promiscuity(slave_dev
, -1);
1815 /* unset allmulti level from slave */
1816 if (bond_dev
->flags
& IFF_ALLMULTI
) {
1817 dev_set_allmulti(slave_dev
, -1);
1820 /* flush master's mc_list from slave */
1821 bond_mc_list_flush(bond_dev
, slave_dev
);
1824 netdev_set_master(slave_dev
, NULL
);
1826 /* close slave before restoring its mac address */
1827 dev_close(slave_dev
);
1829 if (!bond
->params
.fail_over_mac
) {
1830 /* restore original ("permanent") mac address */
1831 memcpy(addr
.sa_data
, slave
->perm_hwaddr
, ETH_ALEN
);
1832 addr
.sa_family
= slave_dev
->type
;
1833 dev_set_mac_address(slave_dev
, &addr
);
1836 slave_dev
->priv_flags
&= ~(IFF_MASTER_8023AD
| IFF_MASTER_ALB
|
1837 IFF_SLAVE_INACTIVE
| IFF_BONDING
|
1842 return 0; /* deletion OK */
1846 * Destroy a bonding device.
1847 * Must be under rtnl_lock when this function is called.
1849 void bond_destroy(struct bonding
*bond
)
1851 bond_deinit(bond
->dev
);
1852 bond_destroy_sysfs_entry(bond
);
1853 unregister_netdevice(bond
->dev
);
1857 * First release a slave and than destroy the bond if no more slaves iare left.
1858 * Must be under rtnl_lock when this function is called.
1860 int bond_release_and_destroy(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1862 struct bonding
*bond
= bond_dev
->priv
;
1865 ret
= bond_release(bond_dev
, slave_dev
);
1866 if ((ret
== 0) && (bond
->slave_cnt
== 0)) {
1867 printk(KERN_INFO DRV_NAME
": %s: destroying bond %s.\n",
1868 bond_dev
->name
, bond_dev
->name
);
1875 * This function releases all slaves.
1877 static int bond_release_all(struct net_device
*bond_dev
)
1879 struct bonding
*bond
= bond_dev
->priv
;
1880 struct slave
*slave
;
1881 struct net_device
*slave_dev
;
1882 struct sockaddr addr
;
1884 write_lock_bh(&bond
->lock
);
1886 netif_carrier_off(bond_dev
);
1888 if (bond
->slave_cnt
== 0) {
1892 bond
->current_arp_slave
= NULL
;
1893 bond
->primary_slave
= NULL
;
1894 bond_change_active_slave(bond
, NULL
);
1896 while ((slave
= bond
->first_slave
) != NULL
) {
1897 /* Inform AD package of unbinding of slave
1898 * before slave is detached from the list.
1900 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1901 bond_3ad_unbind_slave(slave
);
1904 slave_dev
= slave
->dev
;
1905 bond_detach_slave(bond
, slave
);
1907 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
1908 (bond
->params
.mode
== BOND_MODE_ALB
)) {
1909 /* must be called only after the slave
1910 * has been detached from the list
1912 bond_alb_deinit_slave(bond
, slave
);
1915 bond_compute_features(bond
);
1917 /* now that the slave is detached, unlock and perform
1918 * all the undo steps that should not be called from
1921 write_unlock_bh(&bond
->lock
);
1923 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
1924 bond_del_vlans_from_slave(bond
, slave_dev
);
1926 /* If the mode USES_PRIMARY, then we should only remove its
1927 * promisc and mc settings if it was the curr_active_slave, but that was
1928 * already taken care of above when we detached the slave
1930 if (!USES_PRIMARY(bond
->params
.mode
)) {
1931 /* unset promiscuity level from slave */
1932 if (bond_dev
->flags
& IFF_PROMISC
) {
1933 dev_set_promiscuity(slave_dev
, -1);
1936 /* unset allmulti level from slave */
1937 if (bond_dev
->flags
& IFF_ALLMULTI
) {
1938 dev_set_allmulti(slave_dev
, -1);
1941 /* flush master's mc_list from slave */
1942 bond_mc_list_flush(bond_dev
, slave_dev
);
1945 netdev_set_master(slave_dev
, NULL
);
1947 /* close slave before restoring its mac address */
1948 dev_close(slave_dev
);
1950 if (!bond
->params
.fail_over_mac
) {
1951 /* restore original ("permanent") mac address*/
1952 memcpy(addr
.sa_data
, slave
->perm_hwaddr
, ETH_ALEN
);
1953 addr
.sa_family
= slave_dev
->type
;
1954 dev_set_mac_address(slave_dev
, &addr
);
1957 slave_dev
->priv_flags
&= ~(IFF_MASTER_8023AD
| IFF_MASTER_ALB
|
1958 IFF_SLAVE_INACTIVE
);
1962 /* re-acquire the lock before getting the next slave */
1963 write_lock_bh(&bond
->lock
);
1966 /* zero the mac address of the master so it will be
1967 * set by the application to the mac address of the
1970 memset(bond_dev
->dev_addr
, 0, bond_dev
->addr_len
);
1972 if (list_empty(&bond
->vlan_list
)) {
1973 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1975 printk(KERN_WARNING DRV_NAME
1976 ": %s: Warning: clearing HW address of %s while it "
1977 "still has VLANs.\n",
1978 bond_dev
->name
, bond_dev
->name
);
1979 printk(KERN_WARNING DRV_NAME
1980 ": %s: When re-adding slaves, make sure the bond's "
1981 "HW address matches its VLANs'.\n",
1985 printk(KERN_INFO DRV_NAME
1986 ": %s: released all slaves\n",
1990 write_unlock_bh(&bond
->lock
);
1996 * This function changes the active slave to slave <slave_dev>.
1997 * It returns -EINVAL in the following cases.
1998 * - <slave_dev> is not found in the list.
1999 * - There is not active slave now.
2000 * - <slave_dev> is already active.
2001 * - The link state of <slave_dev> is not BOND_LINK_UP.
2002 * - <slave_dev> is not running.
2003 * In these cases, this fuction does nothing.
2004 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2006 static int bond_ioctl_change_active(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
2008 struct bonding
*bond
= bond_dev
->priv
;
2009 struct slave
*old_active
= NULL
;
2010 struct slave
*new_active
= NULL
;
2013 if (!USES_PRIMARY(bond
->params
.mode
)) {
2017 /* Verify that master_dev is indeed the master of slave_dev */
2018 if (!(slave_dev
->flags
& IFF_SLAVE
) ||
2019 (slave_dev
->master
!= bond_dev
)) {
2023 read_lock(&bond
->lock
);
2025 read_lock(&bond
->curr_slave_lock
);
2026 old_active
= bond
->curr_active_slave
;
2027 read_unlock(&bond
->curr_slave_lock
);
2029 new_active
= bond_get_slave_by_dev(bond
, slave_dev
);
2032 * Changing to the current active: do nothing; return success.
2034 if (new_active
&& (new_active
== old_active
)) {
2035 read_unlock(&bond
->lock
);
2041 (new_active
->link
== BOND_LINK_UP
) &&
2042 IS_UP(new_active
->dev
)) {
2043 write_lock_bh(&bond
->curr_slave_lock
);
2044 bond_change_active_slave(bond
, new_active
);
2045 write_unlock_bh(&bond
->curr_slave_lock
);
2050 read_unlock(&bond
->lock
);
2055 static int bond_info_query(struct net_device
*bond_dev
, struct ifbond
*info
)
2057 struct bonding
*bond
= bond_dev
->priv
;
2059 info
->bond_mode
= bond
->params
.mode
;
2060 info
->miimon
= bond
->params
.miimon
;
2062 read_lock(&bond
->lock
);
2063 info
->num_slaves
= bond
->slave_cnt
;
2064 read_unlock(&bond
->lock
);
2069 static int bond_slave_info_query(struct net_device
*bond_dev
, struct ifslave
*info
)
2071 struct bonding
*bond
= bond_dev
->priv
;
2072 struct slave
*slave
;
2075 if (info
->slave_id
< 0) {
2079 read_lock(&bond
->lock
);
2081 bond_for_each_slave(bond
, slave
, i
) {
2082 if (i
== (int)info
->slave_id
) {
2088 read_unlock(&bond
->lock
);
2091 strcpy(info
->slave_name
, slave
->dev
->name
);
2092 info
->link
= slave
->link
;
2093 info
->state
= slave
->state
;
2094 info
->link_failure_count
= slave
->link_failure_count
;
2102 /*-------------------------------- Monitoring -------------------------------*/
2105 * if !have_locks, return nonzero if a failover is necessary. if
2106 * have_locks, do whatever failover activities are needed.
2108 * This is to separate the inspection and failover steps for locking
2109 * purposes; failover requires rtnl, but acquiring it for every
2110 * inspection is undesirable, so a wrapper first does inspection, and
2111 * the acquires the necessary locks and calls again to perform
2112 * failover if needed. Since all locks are dropped, a complete
2113 * restart is needed between calls.
2115 static int __bond_mii_monitor(struct bonding
*bond
, int have_locks
)
2117 struct slave
*slave
, *oldcurrent
;
2118 int do_failover
= 0;
2121 if (bond
->slave_cnt
== 0)
2124 /* we will try to read the link status of each of our slaves, and
2125 * set their IFF_RUNNING flag appropriately. For each slave not
2126 * supporting MII status, we won't do anything so that a user-space
2127 * program could monitor the link itself if needed.
2130 if (bond
->send_grat_arp
) {
2131 if (bond
->curr_active_slave
&& test_bit(__LINK_STATE_LINKWATCH_PENDING
,
2132 &bond
->curr_active_slave
->dev
->state
))
2133 dprintk("Needs to send gratuitous arp but not yet\n");
2135 dprintk("sending delayed gratuitous arp on on %s\n",
2136 bond
->curr_active_slave
->dev
->name
);
2137 bond_send_gratuitous_arp(bond
);
2138 bond
->send_grat_arp
= 0;
2141 read_lock(&bond
->curr_slave_lock
);
2142 oldcurrent
= bond
->curr_active_slave
;
2143 read_unlock(&bond
->curr_slave_lock
);
2145 bond_for_each_slave(bond
, slave
, i
) {
2146 struct net_device
*slave_dev
= slave
->dev
;
2148 u16 old_speed
= slave
->speed
;
2149 u8 old_duplex
= slave
->duplex
;
2151 link_state
= bond_check_dev_link(bond
, slave_dev
, 0);
2153 switch (slave
->link
) {
2154 case BOND_LINK_UP
: /* the link was up */
2155 if (link_state
== BMSR_LSTATUS
) {
2162 } else { /* link going down */
2163 slave
->link
= BOND_LINK_FAIL
;
2164 slave
->delay
= bond
->params
.downdelay
;
2166 if (slave
->link_failure_count
< UINT_MAX
) {
2167 slave
->link_failure_count
++;
2170 if (bond
->params
.downdelay
) {
2171 printk(KERN_INFO DRV_NAME
2172 ": %s: link status down for %s "
2173 "interface %s, disabling it in "
2177 ? ((bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
)
2178 ? ((slave
== oldcurrent
)
2179 ? "active " : "backup ")
2183 bond
->params
.downdelay
* bond
->params
.miimon
);
2186 /* no break ! fall through the BOND_LINK_FAIL test to
2187 ensure proper action to be taken
2189 case BOND_LINK_FAIL
: /* the link has just gone down */
2190 if (link_state
!= BMSR_LSTATUS
) {
2191 /* link stays down */
2192 if (slave
->delay
<= 0) {
2196 /* link down for too long time */
2197 slave
->link
= BOND_LINK_DOWN
;
2199 /* in active/backup mode, we must
2200 * completely disable this interface
2202 if ((bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
) ||
2203 (bond
->params
.mode
== BOND_MODE_8023AD
)) {
2204 bond_set_slave_inactive_flags(slave
);
2207 printk(KERN_INFO DRV_NAME
2208 ": %s: link status definitely "
2209 "down for interface %s, "
2214 /* notify ad that the link status has changed */
2215 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
2216 bond_3ad_handle_link_change(slave
, BOND_LINK_DOWN
);
2219 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
2220 (bond
->params
.mode
== BOND_MODE_ALB
)) {
2221 bond_alb_handle_link_change(bond
, slave
, BOND_LINK_DOWN
);
2224 if (slave
== oldcurrent
) {
2232 slave
->link
= BOND_LINK_UP
;
2233 slave
->jiffies
= jiffies
;
2234 printk(KERN_INFO DRV_NAME
2235 ": %s: link status up again after %d "
2236 "ms for interface %s.\n",
2238 (bond
->params
.downdelay
- slave
->delay
) * bond
->params
.miimon
,
2242 case BOND_LINK_DOWN
: /* the link was down */
2243 if (link_state
!= BMSR_LSTATUS
) {
2244 /* the link stays down, nothing more to do */
2246 } else { /* link going up */
2247 slave
->link
= BOND_LINK_BACK
;
2248 slave
->delay
= bond
->params
.updelay
;
2250 if (bond
->params
.updelay
) {
2251 /* if updelay == 0, no need to
2252 advertise about a 0 ms delay */
2253 printk(KERN_INFO DRV_NAME
2254 ": %s: link status up for "
2255 "interface %s, enabling it "
2259 bond
->params
.updelay
* bond
->params
.miimon
);
2262 /* no break ! fall through the BOND_LINK_BACK state in
2263 case there's something to do.
2265 case BOND_LINK_BACK
: /* the link has just come back */
2266 if (link_state
!= BMSR_LSTATUS
) {
2267 /* link down again */
2268 slave
->link
= BOND_LINK_DOWN
;
2270 printk(KERN_INFO DRV_NAME
2271 ": %s: link status down again after %d "
2272 "ms for interface %s.\n",
2274 (bond
->params
.updelay
- slave
->delay
) * bond
->params
.miimon
,
2278 if (slave
->delay
== 0) {
2282 /* now the link has been up for long time enough */
2283 slave
->link
= BOND_LINK_UP
;
2284 slave
->jiffies
= jiffies
;
2286 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
2287 /* prevent it from being the active one */
2288 slave
->state
= BOND_STATE_BACKUP
;
2289 } else if (bond
->params
.mode
!= BOND_MODE_ACTIVEBACKUP
) {
2290 /* make it immediately active */
2291 slave
->state
= BOND_STATE_ACTIVE
;
2292 } else if (slave
!= bond
->primary_slave
) {
2293 /* prevent it from being the active one */
2294 slave
->state
= BOND_STATE_BACKUP
;
2297 printk(KERN_INFO DRV_NAME
2298 ": %s: link status definitely "
2299 "up for interface %s.\n",
2303 /* notify ad that the link status has changed */
2304 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
2305 bond_3ad_handle_link_change(slave
, BOND_LINK_UP
);
2308 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
2309 (bond
->params
.mode
== BOND_MODE_ALB
)) {
2310 bond_alb_handle_link_change(bond
, slave
, BOND_LINK_UP
);
2313 if ((!oldcurrent
) ||
2314 (slave
== bond
->primary_slave
)) {
2323 /* Should not happen */
2324 printk(KERN_ERR DRV_NAME
2325 ": %s: Error: %s Illegal value (link=%d)\n",
2330 } /* end of switch (slave->link) */
2332 bond_update_speed_duplex(slave
);
2334 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
2335 if (old_speed
!= slave
->speed
) {
2336 bond_3ad_adapter_speed_changed(slave
);
2339 if (old_duplex
!= slave
->duplex
) {
2340 bond_3ad_adapter_duplex_changed(slave
);
2349 write_lock_bh(&bond
->curr_slave_lock
);
2351 bond_select_active_slave(bond
);
2353 write_unlock_bh(&bond
->curr_slave_lock
);
2356 bond_set_carrier(bond
);
2365 * Really a wrapper that splits the mii monitor into two phases: an
2366 * inspection, then (if inspection indicates something needs to be
2367 * done) an acquisition of appropriate locks followed by another pass
2368 * to implement whatever link state changes are indicated.
2370 void bond_mii_monitor(struct work_struct
*work
)
2372 struct bonding
*bond
= container_of(work
, struct bonding
,
2374 unsigned long delay
;
2376 read_lock(&bond
->lock
);
2377 if (bond
->kill_timers
) {
2378 read_unlock(&bond
->lock
);
2381 if (__bond_mii_monitor(bond
, 0)) {
2382 read_unlock(&bond
->lock
);
2384 read_lock(&bond
->lock
);
2385 __bond_mii_monitor(bond
, 1);
2389 delay
= ((bond
->params
.miimon
* HZ
) / 1000) ? : 1;
2390 read_unlock(&bond
->lock
);
2391 queue_delayed_work(bond
->wq
, &bond
->mii_work
, delay
);
2394 static __be32
bond_glean_dev_ip(struct net_device
*dev
)
2396 struct in_device
*idev
;
2397 struct in_ifaddr
*ifa
;
2404 idev
= __in_dev_get_rcu(dev
);
2408 ifa
= idev
->ifa_list
;
2412 addr
= ifa
->ifa_local
;
2418 static int bond_has_ip(struct bonding
*bond
)
2420 struct vlan_entry
*vlan
, *vlan_next
;
2422 if (bond
->master_ip
)
2425 if (list_empty(&bond
->vlan_list
))
2428 list_for_each_entry_safe(vlan
, vlan_next
, &bond
->vlan_list
,
2437 static int bond_has_this_ip(struct bonding
*bond
, __be32 ip
)
2439 struct vlan_entry
*vlan
, *vlan_next
;
2441 if (ip
== bond
->master_ip
)
2444 if (list_empty(&bond
->vlan_list
))
2447 list_for_each_entry_safe(vlan
, vlan_next
, &bond
->vlan_list
,
2449 if (ip
== vlan
->vlan_ip
)
2457 * We go to the (large) trouble of VLAN tagging ARP frames because
2458 * switches in VLAN mode (especially if ports are configured as
2459 * "native" to a VLAN) might not pass non-tagged frames.
2461 static void bond_arp_send(struct net_device
*slave_dev
, int arp_op
, __be32 dest_ip
, __be32 src_ip
, unsigned short vlan_id
)
2463 struct sk_buff
*skb
;
2465 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op
,
2466 slave_dev
->name
, dest_ip
, src_ip
, vlan_id
);
2468 skb
= arp_create(arp_op
, ETH_P_ARP
, dest_ip
, slave_dev
, src_ip
,
2469 NULL
, slave_dev
->dev_addr
, NULL
);
2472 printk(KERN_ERR DRV_NAME
": ARP packet allocation failed\n");
2476 skb
= vlan_put_tag(skb
, vlan_id
);
2478 printk(KERN_ERR DRV_NAME
": failed to insert VLAN tag\n");
2486 static void bond_arp_send_all(struct bonding
*bond
, struct slave
*slave
)
2489 __be32
*targets
= bond
->params
.arp_targets
;
2490 struct vlan_entry
*vlan
, *vlan_next
;
2491 struct net_device
*vlan_dev
;
2495 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
); i
++) {
2498 dprintk("basa: target %x\n", targets
[i
]);
2499 if (list_empty(&bond
->vlan_list
)) {
2500 dprintk("basa: empty vlan: arp_send\n");
2501 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2502 bond
->master_ip
, 0);
2507 * If VLANs are configured, we do a route lookup to
2508 * determine which VLAN interface would be used, so we
2509 * can tag the ARP with the proper VLAN tag.
2511 memset(&fl
, 0, sizeof(fl
));
2512 fl
.fl4_dst
= targets
[i
];
2513 fl
.fl4_tos
= RTO_ONLINK
;
2515 rv
= ip_route_output_key(&rt
, &fl
);
2517 if (net_ratelimit()) {
2518 printk(KERN_WARNING DRV_NAME
2519 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2520 bond
->dev
->name
, NIPQUAD(fl
.fl4_dst
));
2526 * This target is not on a VLAN
2528 if (rt
->u
.dst
.dev
== bond
->dev
) {
2530 dprintk("basa: rtdev == bond->dev: arp_send\n");
2531 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2532 bond
->master_ip
, 0);
2537 list_for_each_entry_safe(vlan
, vlan_next
, &bond
->vlan_list
,
2539 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
2540 if (vlan_dev
== rt
->u
.dst
.dev
) {
2541 vlan_id
= vlan
->vlan_id
;
2542 dprintk("basa: vlan match on %s %d\n",
2543 vlan_dev
->name
, vlan_id
);
2550 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2551 vlan
->vlan_ip
, vlan_id
);
2555 if (net_ratelimit()) {
2556 printk(KERN_WARNING DRV_NAME
2557 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2558 bond
->dev
->name
, NIPQUAD(fl
.fl4_dst
),
2559 rt
->u
.dst
.dev
? rt
->u
.dst
.dev
->name
: "NULL");
2566 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2567 * for each VLAN above us.
2569 static void bond_send_gratuitous_arp(struct bonding
*bond
)
2571 struct slave
*slave
= bond
->curr_active_slave
;
2572 struct vlan_entry
*vlan
;
2573 struct net_device
*vlan_dev
;
2575 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond
->dev
->name
,
2576 slave
? slave
->dev
->name
: "NULL");
2580 if (bond
->master_ip
) {
2581 bond_arp_send(slave
->dev
, ARPOP_REPLY
, bond
->master_ip
,
2582 bond
->master_ip
, 0);
2585 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
2586 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
2587 if (vlan
->vlan_ip
) {
2588 bond_arp_send(slave
->dev
, ARPOP_REPLY
, vlan
->vlan_ip
,
2589 vlan
->vlan_ip
, vlan
->vlan_id
);
2594 static void bond_validate_arp(struct bonding
*bond
, struct slave
*slave
, __be32 sip
, __be32 tip
)
2597 __be32
*targets
= bond
->params
.arp_targets
;
2599 targets
= bond
->params
.arp_targets
;
2600 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
) && targets
[i
]; i
++) {
2601 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2602 "%u.%u.%u.%u bhti(tip) %d\n",
2603 NIPQUAD(sip
), NIPQUAD(tip
), i
, NIPQUAD(targets
[i
]),
2604 bond_has_this_ip(bond
, tip
));
2605 if (sip
== targets
[i
]) {
2606 if (bond_has_this_ip(bond
, tip
))
2607 slave
->last_arp_rx
= jiffies
;
2613 static int bond_arp_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
2616 struct slave
*slave
;
2617 struct bonding
*bond
;
2618 unsigned char *arp_ptr
;
2621 if (dev
->nd_net
!= &init_net
)
2624 if (!(dev
->priv_flags
& IFF_BONDING
) || !(dev
->flags
& IFF_MASTER
))
2628 read_lock(&bond
->lock
);
2630 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2631 bond
->dev
->name
, skb
->dev
? skb
->dev
->name
: "NULL",
2632 orig_dev
? orig_dev
->name
: "NULL");
2634 slave
= bond_get_slave_by_dev(bond
, orig_dev
);
2635 if (!slave
|| !slave_do_arp_validate(bond
, slave
))
2638 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2639 if (!pskb_may_pull(skb
, (sizeof(struct arphdr
) +
2640 (2 * dev
->addr_len
) +
2641 (2 * sizeof(u32
)))))
2645 if (arp
->ar_hln
!= dev
->addr_len
||
2646 skb
->pkt_type
== PACKET_OTHERHOST
||
2647 skb
->pkt_type
== PACKET_LOOPBACK
||
2648 arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
2649 arp
->ar_pro
!= htons(ETH_P_IP
) ||
2653 arp_ptr
= (unsigned char *)(arp
+ 1);
2654 arp_ptr
+= dev
->addr_len
;
2655 memcpy(&sip
, arp_ptr
, 4);
2656 arp_ptr
+= 4 + dev
->addr_len
;
2657 memcpy(&tip
, arp_ptr
, 4);
2659 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2660 " tip %u.%u.%u.%u\n", bond
->dev
->name
, slave
->dev
->name
,
2661 slave
->state
, bond
->params
.arp_validate
,
2662 slave_do_arp_validate(bond
, slave
), NIPQUAD(sip
), NIPQUAD(tip
));
2665 * Backup slaves won't see the ARP reply, but do come through
2666 * here for each ARP probe (so we swap the sip/tip to validate
2667 * the probe). In a "redundant switch, common router" type of
2668 * configuration, the ARP probe will (hopefully) travel from
2669 * the active, through one switch, the router, then the other
2670 * switch before reaching the backup.
2672 if (slave
->state
== BOND_STATE_ACTIVE
)
2673 bond_validate_arp(bond
, slave
, sip
, tip
);
2675 bond_validate_arp(bond
, slave
, tip
, sip
);
2678 read_unlock(&bond
->lock
);
2681 return NET_RX_SUCCESS
;
2685 * this function is called regularly to monitor each slave's link
2686 * ensuring that traffic is being sent and received when arp monitoring
2687 * is used in load-balancing mode. if the adapter has been dormant, then an
2688 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2689 * arp monitoring in active backup mode.
2691 void bond_loadbalance_arp_mon(struct work_struct
*work
)
2693 struct bonding
*bond
= container_of(work
, struct bonding
,
2695 struct slave
*slave
, *oldcurrent
;
2696 int do_failover
= 0;
2700 read_lock(&bond
->lock
);
2702 delta_in_ticks
= (bond
->params
.arp_interval
* HZ
) / 1000;
2704 if (bond
->kill_timers
) {
2708 if (bond
->slave_cnt
== 0) {
2712 read_lock(&bond
->curr_slave_lock
);
2713 oldcurrent
= bond
->curr_active_slave
;
2714 read_unlock(&bond
->curr_slave_lock
);
2716 /* see if any of the previous devices are up now (i.e. they have
2717 * xmt and rcv traffic). the curr_active_slave does not come into
2718 * the picture unless it is null. also, slave->jiffies is not needed
2719 * here because we send an arp on each slave and give a slave as
2720 * long as it needs to get the tx/rx within the delta.
2721 * TODO: what about up/down delay in arp mode? it wasn't here before
2724 bond_for_each_slave(bond
, slave
, i
) {
2725 if (slave
->link
!= BOND_LINK_UP
) {
2726 if (time_before_eq(jiffies
, slave
->dev
->trans_start
+ delta_in_ticks
) &&
2727 time_before_eq(jiffies
, slave
->dev
->last_rx
+ delta_in_ticks
)) {
2729 slave
->link
= BOND_LINK_UP
;
2730 slave
->state
= BOND_STATE_ACTIVE
;
2732 /* primary_slave has no meaning in round-robin
2733 * mode. the window of a slave being up and
2734 * curr_active_slave being null after enslaving
2738 printk(KERN_INFO DRV_NAME
2739 ": %s: link status definitely "
2740 "up for interface %s, ",
2745 printk(KERN_INFO DRV_NAME
2746 ": %s: interface %s is now up\n",
2752 /* slave->link == BOND_LINK_UP */
2754 /* not all switches will respond to an arp request
2755 * when the source ip is 0, so don't take the link down
2756 * if we don't know our ip yet
2758 if (time_after_eq(jiffies
, slave
->dev
->trans_start
+ 2*delta_in_ticks
) ||
2759 (time_after_eq(jiffies
, slave
->dev
->last_rx
+ 2*delta_in_ticks
) &&
2760 bond_has_ip(bond
))) {
2762 slave
->link
= BOND_LINK_DOWN
;
2763 slave
->state
= BOND_STATE_BACKUP
;
2765 if (slave
->link_failure_count
< UINT_MAX
) {
2766 slave
->link_failure_count
++;
2769 printk(KERN_INFO DRV_NAME
2770 ": %s: interface %s is now down.\n",
2774 if (slave
== oldcurrent
) {
2780 /* note: if switch is in round-robin mode, all links
2781 * must tx arp to ensure all links rx an arp - otherwise
2782 * links may oscillate or not come up at all; if switch is
2783 * in something like xor mode, there is nothing we can
2784 * do - all replies will be rx'ed on same link causing slaves
2785 * to be unstable during low/no traffic periods
2787 if (IS_UP(slave
->dev
)) {
2788 bond_arp_send_all(bond
, slave
);
2794 write_lock_bh(&bond
->curr_slave_lock
);
2796 bond_select_active_slave(bond
);
2798 write_unlock_bh(&bond
->curr_slave_lock
);
2804 if (bond
->params
.arp_interval
)
2805 queue_delayed_work(bond
->wq
, &bond
->arp_work
, delta_in_ticks
);
2807 read_unlock(&bond
->lock
);
2811 * When using arp monitoring in active-backup mode, this function is
2812 * called to determine if any backup slaves have went down or a new
2813 * current slave needs to be found.
2814 * The backup slaves never generate traffic, they are considered up by merely
2815 * receiving traffic. If the current slave goes down, each backup slave will
2816 * be given the opportunity to tx/rx an arp before being taken down - this
2817 * prevents all slaves from being taken down due to the current slave not
2818 * sending any traffic for the backups to receive. The arps are not necessarily
2819 * necessary, any tx and rx traffic will keep the current slave up. While any
2820 * rx traffic will keep the backup slaves up, the current slave is responsible
2821 * for generating traffic to keep them up regardless of any other traffic they
2822 * may have received.
2823 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2825 void bond_activebackup_arp_mon(struct work_struct
*work
)
2827 struct bonding
*bond
= container_of(work
, struct bonding
,
2829 struct slave
*slave
;
2833 read_lock(&bond
->lock
);
2835 delta_in_ticks
= (bond
->params
.arp_interval
* HZ
) / 1000;
2837 if (bond
->kill_timers
) {
2841 if (bond
->slave_cnt
== 0) {
2845 /* determine if any slave has come up or any backup slave has
2847 * TODO: what about up/down delay in arp mode? it wasn't here before
2850 bond_for_each_slave(bond
, slave
, i
) {
2851 if (slave
->link
!= BOND_LINK_UP
) {
2852 if (time_before_eq(jiffies
,
2853 slave_last_rx(bond
, slave
) + delta_in_ticks
)) {
2855 slave
->link
= BOND_LINK_UP
;
2859 write_lock_bh(&bond
->curr_slave_lock
);
2861 if ((!bond
->curr_active_slave
) &&
2862 time_before_eq(jiffies
, slave
->dev
->trans_start
+ delta_in_ticks
)) {
2863 bond_change_active_slave(bond
, slave
);
2864 bond
->current_arp_slave
= NULL
;
2865 } else if (bond
->curr_active_slave
!= slave
) {
2866 /* this slave has just come up but we
2867 * already have a current slave; this
2868 * can also happen if bond_enslave adds
2869 * a new slave that is up while we are
2870 * searching for a new slave
2872 bond_set_slave_inactive_flags(slave
);
2873 bond
->current_arp_slave
= NULL
;
2876 bond_set_carrier(bond
);
2878 if (slave
== bond
->curr_active_slave
) {
2879 printk(KERN_INFO DRV_NAME
2880 ": %s: %s is up and now the "
2881 "active interface\n",
2884 netif_carrier_on(bond
->dev
);
2886 printk(KERN_INFO DRV_NAME
2887 ": %s: backup interface %s is "
2893 write_unlock_bh(&bond
->curr_slave_lock
);
2897 read_lock(&bond
->curr_slave_lock
);
2899 if ((slave
!= bond
->curr_active_slave
) &&
2900 (!bond
->current_arp_slave
) &&
2901 (time_after_eq(jiffies
, slave_last_rx(bond
, slave
) + 3*delta_in_ticks
) &&
2902 bond_has_ip(bond
))) {
2903 /* a backup slave has gone down; three times
2904 * the delta allows the current slave to be
2905 * taken out before the backup slave.
2906 * note: a non-null current_arp_slave indicates
2907 * the curr_active_slave went down and we are
2908 * searching for a new one; under this
2909 * condition we only take the curr_active_slave
2910 * down - this gives each slave a chance to
2911 * tx/rx traffic before being taken out
2914 read_unlock(&bond
->curr_slave_lock
);
2916 slave
->link
= BOND_LINK_DOWN
;
2918 if (slave
->link_failure_count
< UINT_MAX
) {
2919 slave
->link_failure_count
++;
2922 bond_set_slave_inactive_flags(slave
);
2924 printk(KERN_INFO DRV_NAME
2925 ": %s: backup interface %s is now down\n",
2929 read_unlock(&bond
->curr_slave_lock
);
2934 read_lock(&bond
->curr_slave_lock
);
2935 slave
= bond
->curr_active_slave
;
2936 read_unlock(&bond
->curr_slave_lock
);
2939 /* if we have sent traffic in the past 2*arp_intervals but
2940 * haven't xmit and rx traffic in that time interval, select
2941 * a different slave. slave->jiffies is only updated when
2942 * a slave first becomes the curr_active_slave - not necessarily
2943 * after every arp; this ensures the slave has a full 2*delta
2944 * before being taken out. if a primary is being used, check
2945 * if it is up and needs to take over as the curr_active_slave
2947 if ((time_after_eq(jiffies
, slave
->dev
->trans_start
+ 2*delta_in_ticks
) ||
2948 (time_after_eq(jiffies
, slave_last_rx(bond
, slave
) + 2*delta_in_ticks
) &&
2949 bond_has_ip(bond
))) &&
2950 time_after_eq(jiffies
, slave
->jiffies
+ 2*delta_in_ticks
)) {
2952 slave
->link
= BOND_LINK_DOWN
;
2954 if (slave
->link_failure_count
< UINT_MAX
) {
2955 slave
->link_failure_count
++;
2958 printk(KERN_INFO DRV_NAME
2959 ": %s: link status down for active interface "
2960 "%s, disabling it\n",
2965 write_lock_bh(&bond
->curr_slave_lock
);
2967 bond_select_active_slave(bond
);
2968 slave
= bond
->curr_active_slave
;
2970 write_unlock_bh(&bond
->curr_slave_lock
);
2974 bond
->current_arp_slave
= slave
;
2977 slave
->jiffies
= jiffies
;
2979 } else if ((bond
->primary_slave
) &&
2980 (bond
->primary_slave
!= slave
) &&
2981 (bond
->primary_slave
->link
== BOND_LINK_UP
)) {
2982 /* at this point, slave is the curr_active_slave */
2983 printk(KERN_INFO DRV_NAME
2984 ": %s: changing from interface %s to primary "
2988 bond
->primary_slave
->dev
->name
);
2990 /* primary is up so switch to it */
2992 write_lock_bh(&bond
->curr_slave_lock
);
2993 bond_change_active_slave(bond
, bond
->primary_slave
);
2994 write_unlock_bh(&bond
->curr_slave_lock
);
2998 slave
= bond
->primary_slave
;
2999 slave
->jiffies
= jiffies
;
3001 bond
->current_arp_slave
= NULL
;
3004 /* the current slave must tx an arp to ensure backup slaves
3007 if (slave
&& bond_has_ip(bond
)) {
3008 bond_arp_send_all(bond
, slave
);
3012 /* if we don't have a curr_active_slave, search for the next available
3013 * backup slave from the current_arp_slave and make it the candidate
3014 * for becoming the curr_active_slave
3017 if (!bond
->current_arp_slave
) {
3018 bond
->current_arp_slave
= bond
->first_slave
;
3021 if (bond
->current_arp_slave
) {
3022 bond_set_slave_inactive_flags(bond
->current_arp_slave
);
3024 /* search for next candidate */
3025 bond_for_each_slave_from(bond
, slave
, i
, bond
->current_arp_slave
->next
) {
3026 if (IS_UP(slave
->dev
)) {
3027 slave
->link
= BOND_LINK_BACK
;
3028 bond_set_slave_active_flags(slave
);
3029 bond_arp_send_all(bond
, slave
);
3030 slave
->jiffies
= jiffies
;
3031 bond
->current_arp_slave
= slave
;
3035 /* if the link state is up at this point, we
3036 * mark it down - this can happen if we have
3037 * simultaneous link failures and
3038 * reselect_active_interface doesn't make this
3039 * one the current slave so it is still marked
3040 * up when it is actually down
3042 if (slave
->link
== BOND_LINK_UP
) {
3043 slave
->link
= BOND_LINK_DOWN
;
3044 if (slave
->link_failure_count
< UINT_MAX
) {
3045 slave
->link_failure_count
++;
3048 bond_set_slave_inactive_flags(slave
);
3050 printk(KERN_INFO DRV_NAME
3051 ": %s: backup interface %s is "
3061 if (bond
->params
.arp_interval
) {
3062 queue_delayed_work(bond
->wq
, &bond
->arp_work
, delta_in_ticks
);
3065 read_unlock(&bond
->lock
);
3068 /*------------------------------ proc/seq_file-------------------------------*/
3070 #ifdef CONFIG_PROC_FS
3072 #define SEQ_START_TOKEN ((void *)1)
3074 static void *bond_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3076 struct bonding
*bond
= seq
->private;
3078 struct slave
*slave
;
3081 /* make sure the bond won't be taken away */
3082 read_lock(&dev_base_lock
);
3083 read_lock(&bond
->lock
);
3086 return SEQ_START_TOKEN
;
3089 bond_for_each_slave(bond
, slave
, i
) {
3090 if (++off
== *pos
) {
3098 static void *bond_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3100 struct bonding
*bond
= seq
->private;
3101 struct slave
*slave
= v
;
3104 if (v
== SEQ_START_TOKEN
) {
3105 return bond
->first_slave
;
3108 slave
= slave
->next
;
3110 return (slave
== bond
->first_slave
) ? NULL
: slave
;
3113 static void bond_info_seq_stop(struct seq_file
*seq
, void *v
)
3115 struct bonding
*bond
= seq
->private;
3117 read_unlock(&bond
->lock
);
3118 read_unlock(&dev_base_lock
);
3121 static void bond_info_show_master(struct seq_file
*seq
)
3123 struct bonding
*bond
= seq
->private;
3128 read_lock(&bond
->curr_slave_lock
);
3129 curr
= bond
->curr_active_slave
;
3130 read_unlock(&bond
->curr_slave_lock
);
3132 seq_printf(seq
, "Bonding Mode: %s",
3133 bond_mode_name(bond
->params
.mode
));
3135 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
&&
3136 bond
->params
.fail_over_mac
)
3137 seq_printf(seq
, " (fail_over_mac)");
3139 seq_printf(seq
, "\n");
3141 if (bond
->params
.mode
== BOND_MODE_XOR
||
3142 bond
->params
.mode
== BOND_MODE_8023AD
) {
3143 seq_printf(seq
, "Transmit Hash Policy: %s (%d)\n",
3144 xmit_hashtype_tbl
[bond
->params
.xmit_policy
].modename
,
3145 bond
->params
.xmit_policy
);
3148 if (USES_PRIMARY(bond
->params
.mode
)) {
3149 seq_printf(seq
, "Primary Slave: %s\n",
3150 (bond
->primary_slave
) ?
3151 bond
->primary_slave
->dev
->name
: "None");
3153 seq_printf(seq
, "Currently Active Slave: %s\n",
3154 (curr
) ? curr
->dev
->name
: "None");
3157 seq_printf(seq
, "MII Status: %s\n", netif_carrier_ok(bond
->dev
) ?
3159 seq_printf(seq
, "MII Polling Interval (ms): %d\n", bond
->params
.miimon
);
3160 seq_printf(seq
, "Up Delay (ms): %d\n",
3161 bond
->params
.updelay
* bond
->params
.miimon
);
3162 seq_printf(seq
, "Down Delay (ms): %d\n",
3163 bond
->params
.downdelay
* bond
->params
.miimon
);
3166 /* ARP information */
3167 if(bond
->params
.arp_interval
> 0) {
3169 seq_printf(seq
, "ARP Polling Interval (ms): %d\n",
3170 bond
->params
.arp_interval
);
3172 seq_printf(seq
, "ARP IP target/s (n.n.n.n form):");
3174 for(i
= 0; (i
< BOND_MAX_ARP_TARGETS
) ;i
++) {
3175 if (!bond
->params
.arp_targets
[i
])
3178 seq_printf(seq
, ",");
3179 target
= ntohl(bond
->params
.arp_targets
[i
]);
3180 seq_printf(seq
, " %d.%d.%d.%d", HIPQUAD(target
));
3183 seq_printf(seq
, "\n");
3186 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3187 struct ad_info ad_info
;
3188 DECLARE_MAC_BUF(mac
);
3190 seq_puts(seq
, "\n802.3ad info\n");
3191 seq_printf(seq
, "LACP rate: %s\n",
3192 (bond
->params
.lacp_fast
) ? "fast" : "slow");
3194 if (bond_3ad_get_active_agg_info(bond
, &ad_info
)) {
3195 seq_printf(seq
, "bond %s has no active aggregator\n",
3198 seq_printf(seq
, "Active Aggregator Info:\n");
3200 seq_printf(seq
, "\tAggregator ID: %d\n",
3201 ad_info
.aggregator_id
);
3202 seq_printf(seq
, "\tNumber of ports: %d\n",
3204 seq_printf(seq
, "\tActor Key: %d\n",
3206 seq_printf(seq
, "\tPartner Key: %d\n",
3207 ad_info
.partner_key
);
3208 seq_printf(seq
, "\tPartner Mac Address: %s\n",
3209 print_mac(mac
, ad_info
.partner_system
));
3214 static void bond_info_show_slave(struct seq_file
*seq
, const struct slave
*slave
)
3216 struct bonding
*bond
= seq
->private;
3217 DECLARE_MAC_BUF(mac
);
3219 seq_printf(seq
, "\nSlave Interface: %s\n", slave
->dev
->name
);
3220 seq_printf(seq
, "MII Status: %s\n",
3221 (slave
->link
== BOND_LINK_UP
) ? "up" : "down");
3222 seq_printf(seq
, "Link Failure Count: %u\n",
3223 slave
->link_failure_count
);
3226 "Permanent HW addr: %s\n",
3227 print_mac(mac
, slave
->perm_hwaddr
));
3229 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3230 const struct aggregator
*agg
3231 = SLAVE_AD_INFO(slave
).port
.aggregator
;
3234 seq_printf(seq
, "Aggregator ID: %d\n",
3235 agg
->aggregator_identifier
);
3237 seq_puts(seq
, "Aggregator ID: N/A\n");
3242 static int bond_info_seq_show(struct seq_file
*seq
, void *v
)
3244 if (v
== SEQ_START_TOKEN
) {
3245 seq_printf(seq
, "%s\n", version
);
3246 bond_info_show_master(seq
);
3248 bond_info_show_slave(seq
, v
);
3254 static struct seq_operations bond_info_seq_ops
= {
3255 .start
= bond_info_seq_start
,
3256 .next
= bond_info_seq_next
,
3257 .stop
= bond_info_seq_stop
,
3258 .show
= bond_info_seq_show
,
3261 static int bond_info_open(struct inode
*inode
, struct file
*file
)
3263 struct seq_file
*seq
;
3264 struct proc_dir_entry
*proc
;
3267 res
= seq_open(file
, &bond_info_seq_ops
);
3269 /* recover the pointer buried in proc_dir_entry data */
3270 seq
= file
->private_data
;
3272 seq
->private = proc
->data
;
3278 static const struct file_operations bond_info_fops
= {
3279 .owner
= THIS_MODULE
,
3280 .open
= bond_info_open
,
3282 .llseek
= seq_lseek
,
3283 .release
= seq_release
,
3286 static int bond_create_proc_entry(struct bonding
*bond
)
3288 struct net_device
*bond_dev
= bond
->dev
;
3290 if (bond_proc_dir
) {
3291 bond
->proc_entry
= create_proc_entry(bond_dev
->name
,
3294 if (bond
->proc_entry
== NULL
) {
3295 printk(KERN_WARNING DRV_NAME
3296 ": Warning: Cannot create /proc/net/%s/%s\n",
3297 DRV_NAME
, bond_dev
->name
);
3299 bond
->proc_entry
->data
= bond
;
3300 bond
->proc_entry
->proc_fops
= &bond_info_fops
;
3301 bond
->proc_entry
->owner
= THIS_MODULE
;
3302 memcpy(bond
->proc_file_name
, bond_dev
->name
, IFNAMSIZ
);
3309 static void bond_remove_proc_entry(struct bonding
*bond
)
3311 if (bond_proc_dir
&& bond
->proc_entry
) {
3312 remove_proc_entry(bond
->proc_file_name
, bond_proc_dir
);
3313 memset(bond
->proc_file_name
, 0, IFNAMSIZ
);
3314 bond
->proc_entry
= NULL
;
3318 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3319 * Caller must hold rtnl_lock.
3321 static void bond_create_proc_dir(void)
3323 int len
= strlen(DRV_NAME
);
3325 for (bond_proc_dir
= init_net
.proc_net
->subdir
; bond_proc_dir
;
3326 bond_proc_dir
= bond_proc_dir
->next
) {
3327 if ((bond_proc_dir
->namelen
== len
) &&
3328 !memcmp(bond_proc_dir
->name
, DRV_NAME
, len
)) {
3333 if (!bond_proc_dir
) {
3334 bond_proc_dir
= proc_mkdir(DRV_NAME
, init_net
.proc_net
);
3335 if (bond_proc_dir
) {
3336 bond_proc_dir
->owner
= THIS_MODULE
;
3338 printk(KERN_WARNING DRV_NAME
3339 ": Warning: cannot create /proc/net/%s\n",
3345 /* Destroy the bonding directory under /proc/net, if empty.
3346 * Caller must hold rtnl_lock.
3348 static void bond_destroy_proc_dir(void)
3350 struct proc_dir_entry
*de
;
3352 if (!bond_proc_dir
) {
3356 /* verify that the /proc dir is empty */
3357 for (de
= bond_proc_dir
->subdir
; de
; de
= de
->next
) {
3358 /* ignore . and .. */
3359 if (*(de
->name
) != '.') {
3365 if (bond_proc_dir
->owner
== THIS_MODULE
) {
3366 bond_proc_dir
->owner
= NULL
;
3369 remove_proc_entry(DRV_NAME
, init_net
.proc_net
);
3370 bond_proc_dir
= NULL
;
3373 #endif /* CONFIG_PROC_FS */
3375 /*-------------------------- netdev event handling --------------------------*/
3378 * Change device name
3380 static int bond_event_changename(struct bonding
*bond
)
3382 #ifdef CONFIG_PROC_FS
3383 bond_remove_proc_entry(bond
);
3384 bond_create_proc_entry(bond
);
3386 down_write(&(bonding_rwsem
));
3387 bond_destroy_sysfs_entry(bond
);
3388 bond_create_sysfs_entry(bond
);
3389 up_write(&(bonding_rwsem
));
3393 static int bond_master_netdev_event(unsigned long event
, struct net_device
*bond_dev
)
3395 struct bonding
*event_bond
= bond_dev
->priv
;
3398 case NETDEV_CHANGENAME
:
3399 return bond_event_changename(event_bond
);
3400 case NETDEV_UNREGISTER
:
3402 * TODO: remove a bond from the list?
3412 static int bond_slave_netdev_event(unsigned long event
, struct net_device
*slave_dev
)
3414 struct net_device
*bond_dev
= slave_dev
->master
;
3415 struct bonding
*bond
= bond_dev
->priv
;
3418 case NETDEV_UNREGISTER
:
3420 if (bond
->setup_by_slave
)
3421 bond_release_and_destroy(bond_dev
, slave_dev
);
3423 bond_release(bond_dev
, slave_dev
);
3428 * TODO: is this what we get if somebody
3429 * sets up a hierarchical bond, then rmmod's
3430 * one of the slave bonding devices?
3435 * ... Or is it this?
3438 case NETDEV_CHANGEMTU
:
3440 * TODO: Should slaves be allowed to
3441 * independently alter their MTU? For
3442 * an active-backup bond, slaves need
3443 * not be the same type of device, so
3444 * MTUs may vary. For other modes,
3445 * slaves arguably should have the
3446 * same MTUs. To do this, we'd need to
3447 * take over the slave's change_mtu
3448 * function for the duration of their
3452 case NETDEV_CHANGENAME
:
3454 * TODO: handle changing the primary's name
3457 case NETDEV_FEAT_CHANGE
:
3458 bond_compute_features(bond
);
3468 * bond_netdev_event: handle netdev notifier chain events.
3470 * This function receives events for the netdev chain. The caller (an
3471 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3472 * locks for us to safely manipulate the slave devices (RTNL lock,
3475 static int bond_netdev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3477 struct net_device
*event_dev
= (struct net_device
*)ptr
;
3479 if (event_dev
->nd_net
!= &init_net
)
3482 dprintk("event_dev: %s, event: %lx\n",
3483 (event_dev
? event_dev
->name
: "None"),
3486 if (!(event_dev
->priv_flags
& IFF_BONDING
))
3489 if (event_dev
->flags
& IFF_MASTER
) {
3490 dprintk("IFF_MASTER\n");
3491 return bond_master_netdev_event(event
, event_dev
);
3494 if (event_dev
->flags
& IFF_SLAVE
) {
3495 dprintk("IFF_SLAVE\n");
3496 return bond_slave_netdev_event(event
, event_dev
);
3503 * bond_inetaddr_event: handle inetaddr notifier chain events.
3505 * We keep track of device IPs primarily to use as source addresses in
3506 * ARP monitor probes (rather than spewing out broadcasts all the time).
3508 * We track one IP for the main device (if it has one), plus one per VLAN.
3510 static int bond_inetaddr_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3512 struct in_ifaddr
*ifa
= ptr
;
3513 struct net_device
*vlan_dev
, *event_dev
= ifa
->ifa_dev
->dev
;
3514 struct bonding
*bond
, *bond_next
;
3515 struct vlan_entry
*vlan
, *vlan_next
;
3517 list_for_each_entry_safe(bond
, bond_next
, &bond_dev_list
, bond_list
) {
3518 if (bond
->dev
== event_dev
) {
3521 bond
->master_ip
= ifa
->ifa_local
;
3524 bond
->master_ip
= bond_glean_dev_ip(bond
->dev
);
3531 if (list_empty(&bond
->vlan_list
))
3534 list_for_each_entry_safe(vlan
, vlan_next
, &bond
->vlan_list
,
3536 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
3537 if (vlan_dev
== event_dev
) {
3540 vlan
->vlan_ip
= ifa
->ifa_local
;
3544 bond_glean_dev_ip(vlan_dev
);
3555 static struct notifier_block bond_netdev_notifier
= {
3556 .notifier_call
= bond_netdev_event
,
3559 static struct notifier_block bond_inetaddr_notifier
= {
3560 .notifier_call
= bond_inetaddr_event
,
3563 /*-------------------------- Packet type handling ---------------------------*/
3565 /* register to receive lacpdus on a bond */
3566 static void bond_register_lacpdu(struct bonding
*bond
)
3568 struct packet_type
*pk_type
= &(BOND_AD_INFO(bond
).ad_pkt_type
);
3570 /* initialize packet type */
3571 pk_type
->type
= PKT_TYPE_LACPDU
;
3572 pk_type
->dev
= bond
->dev
;
3573 pk_type
->func
= bond_3ad_lacpdu_recv
;
3575 dev_add_pack(pk_type
);
3578 /* unregister to receive lacpdus on a bond */
3579 static void bond_unregister_lacpdu(struct bonding
*bond
)
3581 dev_remove_pack(&(BOND_AD_INFO(bond
).ad_pkt_type
));
3584 void bond_register_arp(struct bonding
*bond
)
3586 struct packet_type
*pt
= &bond
->arp_mon_pt
;
3591 pt
->type
= htons(ETH_P_ARP
);
3592 pt
->dev
= bond
->dev
;
3593 pt
->func
= bond_arp_rcv
;
3597 void bond_unregister_arp(struct bonding
*bond
)
3599 struct packet_type
*pt
= &bond
->arp_mon_pt
;
3601 dev_remove_pack(pt
);
3605 /*---------------------------- Hashing Policies -----------------------------*/
3608 * Hash for the output device based upon layer 3 and layer 4 data. If
3609 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3610 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3612 static int bond_xmit_hash_policy_l34(struct sk_buff
*skb
,
3613 struct net_device
*bond_dev
, int count
)
3615 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3616 struct iphdr
*iph
= ip_hdr(skb
);
3617 __be16
*layer4hdr
= (__be16
*)((u32
*)iph
+ iph
->ihl
);
3620 if (skb
->protocol
== __constant_htons(ETH_P_IP
)) {
3621 if (!(iph
->frag_off
& __constant_htons(IP_MF
|IP_OFFSET
)) &&
3622 (iph
->protocol
== IPPROTO_TCP
||
3623 iph
->protocol
== IPPROTO_UDP
)) {
3624 layer4_xor
= ntohs((*layer4hdr
^ *(layer4hdr
+ 1)));
3626 return (layer4_xor
^
3627 ((ntohl(iph
->saddr
^ iph
->daddr
)) & 0xffff)) % count
;
3631 return (data
->h_dest
[5] ^ bond_dev
->dev_addr
[5]) % count
;
3635 * Hash for the output device based upon layer 2 data
3637 static int bond_xmit_hash_policy_l2(struct sk_buff
*skb
,
3638 struct net_device
*bond_dev
, int count
)
3640 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3642 return (data
->h_dest
[5] ^ bond_dev
->dev_addr
[5]) % count
;
3645 /*-------------------------- Device entry points ----------------------------*/
3647 static int bond_open(struct net_device
*bond_dev
)
3649 struct bonding
*bond
= bond_dev
->priv
;
3651 bond
->kill_timers
= 0;
3653 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
3654 (bond
->params
.mode
== BOND_MODE_ALB
)) {
3655 /* bond_alb_initialize must be called before the timer
3658 if (bond_alb_initialize(bond
, (bond
->params
.mode
== BOND_MODE_ALB
))) {
3659 /* something went wrong - fail the open operation */
3663 INIT_DELAYED_WORK(&bond
->alb_work
, bond_alb_monitor
);
3664 queue_delayed_work(bond
->wq
, &bond
->alb_work
, 0);
3667 if (bond
->params
.miimon
) { /* link check interval, in milliseconds. */
3668 INIT_DELAYED_WORK(&bond
->mii_work
, bond_mii_monitor
);
3669 queue_delayed_work(bond
->wq
, &bond
->mii_work
, 0);
3672 if (bond
->params
.arp_interval
) { /* arp interval, in milliseconds. */
3673 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
)
3674 INIT_DELAYED_WORK(&bond
->arp_work
,
3675 bond_activebackup_arp_mon
);
3677 INIT_DELAYED_WORK(&bond
->arp_work
,
3678 bond_loadbalance_arp_mon
);
3680 queue_delayed_work(bond
->wq
, &bond
->arp_work
, 0);
3681 if (bond
->params
.arp_validate
)
3682 bond_register_arp(bond
);
3685 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3686 INIT_DELAYED_WORK(&bond
->ad_work
, bond_3ad_state_machine_handler
);
3687 queue_delayed_work(bond
->wq
, &bond
->ad_work
, 0);
3688 /* register to receive LACPDUs */
3689 bond_register_lacpdu(bond
);
3695 static int bond_close(struct net_device
*bond_dev
)
3697 struct bonding
*bond
= bond_dev
->priv
;
3699 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3700 /* Unregister the receive of LACPDUs */
3701 bond_unregister_lacpdu(bond
);
3704 if (bond
->params
.arp_validate
)
3705 bond_unregister_arp(bond
);
3707 write_lock_bh(&bond
->lock
);
3710 /* signal timers not to re-arm */
3711 bond
->kill_timers
= 1;
3713 write_unlock_bh(&bond
->lock
);
3715 if (bond
->params
.miimon
) { /* link check interval, in milliseconds. */
3716 cancel_delayed_work(&bond
->mii_work
);
3719 if (bond
->params
.arp_interval
) { /* arp interval, in milliseconds. */
3720 cancel_delayed_work(&bond
->arp_work
);
3723 switch (bond
->params
.mode
) {
3724 case BOND_MODE_8023AD
:
3725 cancel_delayed_work(&bond
->ad_work
);
3729 cancel_delayed_work(&bond
->alb_work
);
3736 if ((bond
->params
.mode
== BOND_MODE_TLB
) ||
3737 (bond
->params
.mode
== BOND_MODE_ALB
)) {
3738 /* Must be called only after all
3739 * slaves have been released
3741 bond_alb_deinitialize(bond
);
3747 static struct net_device_stats
*bond_get_stats(struct net_device
*bond_dev
)
3749 struct bonding
*bond
= bond_dev
->priv
;
3750 struct net_device_stats
*stats
= &(bond
->stats
), *sstats
;
3751 struct slave
*slave
;
3754 memset(stats
, 0, sizeof(struct net_device_stats
));
3756 read_lock_bh(&bond
->lock
);
3758 bond_for_each_slave(bond
, slave
, i
) {
3759 sstats
= slave
->dev
->get_stats(slave
->dev
);
3760 stats
->rx_packets
+= sstats
->rx_packets
;
3761 stats
->rx_bytes
+= sstats
->rx_bytes
;
3762 stats
->rx_errors
+= sstats
->rx_errors
;
3763 stats
->rx_dropped
+= sstats
->rx_dropped
;
3765 stats
->tx_packets
+= sstats
->tx_packets
;
3766 stats
->tx_bytes
+= sstats
->tx_bytes
;
3767 stats
->tx_errors
+= sstats
->tx_errors
;
3768 stats
->tx_dropped
+= sstats
->tx_dropped
;
3770 stats
->multicast
+= sstats
->multicast
;
3771 stats
->collisions
+= sstats
->collisions
;
3773 stats
->rx_length_errors
+= sstats
->rx_length_errors
;
3774 stats
->rx_over_errors
+= sstats
->rx_over_errors
;
3775 stats
->rx_crc_errors
+= sstats
->rx_crc_errors
;
3776 stats
->rx_frame_errors
+= sstats
->rx_frame_errors
;
3777 stats
->rx_fifo_errors
+= sstats
->rx_fifo_errors
;
3778 stats
->rx_missed_errors
+= sstats
->rx_missed_errors
;
3780 stats
->tx_aborted_errors
+= sstats
->tx_aborted_errors
;
3781 stats
->tx_carrier_errors
+= sstats
->tx_carrier_errors
;
3782 stats
->tx_fifo_errors
+= sstats
->tx_fifo_errors
;
3783 stats
->tx_heartbeat_errors
+= sstats
->tx_heartbeat_errors
;
3784 stats
->tx_window_errors
+= sstats
->tx_window_errors
;
3787 read_unlock_bh(&bond
->lock
);
3792 static int bond_do_ioctl(struct net_device
*bond_dev
, struct ifreq
*ifr
, int cmd
)
3794 struct net_device
*slave_dev
= NULL
;
3795 struct ifbond k_binfo
;
3796 struct ifbond __user
*u_binfo
= NULL
;
3797 struct ifslave k_sinfo
;
3798 struct ifslave __user
*u_sinfo
= NULL
;
3799 struct mii_ioctl_data
*mii
= NULL
;
3802 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3803 bond_dev
->name
, cmd
);
3815 * We do this again just in case we were called by SIOCGMIIREG
3816 * instead of SIOCGMIIPHY.
3823 if (mii
->reg_num
== 1) {
3824 struct bonding
*bond
= bond_dev
->priv
;
3826 read_lock(&bond
->lock
);
3827 read_lock(&bond
->curr_slave_lock
);
3828 if (netif_carrier_ok(bond
->dev
)) {
3829 mii
->val_out
= BMSR_LSTATUS
;
3831 read_unlock(&bond
->curr_slave_lock
);
3832 read_unlock(&bond
->lock
);
3836 case BOND_INFO_QUERY_OLD
:
3837 case SIOCBONDINFOQUERY
:
3838 u_binfo
= (struct ifbond __user
*)ifr
->ifr_data
;
3840 if (copy_from_user(&k_binfo
, u_binfo
, sizeof(ifbond
))) {
3844 res
= bond_info_query(bond_dev
, &k_binfo
);
3846 if (copy_to_user(u_binfo
, &k_binfo
, sizeof(ifbond
))) {
3852 case BOND_SLAVE_INFO_QUERY_OLD
:
3853 case SIOCBONDSLAVEINFOQUERY
:
3854 u_sinfo
= (struct ifslave __user
*)ifr
->ifr_data
;
3856 if (copy_from_user(&k_sinfo
, u_sinfo
, sizeof(ifslave
))) {
3860 res
= bond_slave_info_query(bond_dev
, &k_sinfo
);
3862 if (copy_to_user(u_sinfo
, &k_sinfo
, sizeof(ifslave
))) {
3873 if (!capable(CAP_NET_ADMIN
)) {
3877 down_write(&(bonding_rwsem
));
3878 slave_dev
= dev_get_by_name(&init_net
, ifr
->ifr_slave
);
3880 dprintk("slave_dev=%p: \n", slave_dev
);
3885 dprintk("slave_dev->name=%s: \n", slave_dev
->name
);
3887 case BOND_ENSLAVE_OLD
:
3888 case SIOCBONDENSLAVE
:
3889 res
= bond_enslave(bond_dev
, slave_dev
);
3891 case BOND_RELEASE_OLD
:
3892 case SIOCBONDRELEASE
:
3893 res
= bond_release(bond_dev
, slave_dev
);
3895 case BOND_SETHWADDR_OLD
:
3896 case SIOCBONDSETHWADDR
:
3897 res
= bond_sethwaddr(bond_dev
, slave_dev
);
3899 case BOND_CHANGE_ACTIVE_OLD
:
3900 case SIOCBONDCHANGEACTIVE
:
3901 res
= bond_ioctl_change_active(bond_dev
, slave_dev
);
3910 up_write(&(bonding_rwsem
));
3914 static void bond_set_multicast_list(struct net_device
*bond_dev
)
3916 struct bonding
*bond
= bond_dev
->priv
;
3917 struct dev_mc_list
*dmi
;
3919 write_lock_bh(&bond
->lock
);
3922 * Do promisc before checking multicast_mode
3924 if ((bond_dev
->flags
& IFF_PROMISC
) && !(bond
->flags
& IFF_PROMISC
)) {
3925 bond_set_promiscuity(bond
, 1);
3928 if (!(bond_dev
->flags
& IFF_PROMISC
) && (bond
->flags
& IFF_PROMISC
)) {
3929 bond_set_promiscuity(bond
, -1);
3932 /* set allmulti flag to slaves */
3933 if ((bond_dev
->flags
& IFF_ALLMULTI
) && !(bond
->flags
& IFF_ALLMULTI
)) {
3934 bond_set_allmulti(bond
, 1);
3937 if (!(bond_dev
->flags
& IFF_ALLMULTI
) && (bond
->flags
& IFF_ALLMULTI
)) {
3938 bond_set_allmulti(bond
, -1);
3941 bond
->flags
= bond_dev
->flags
;
3943 /* looking for addresses to add to slaves' mc list */
3944 for (dmi
= bond_dev
->mc_list
; dmi
; dmi
= dmi
->next
) {
3945 if (!bond_mc_list_find_dmi(dmi
, bond
->mc_list
)) {
3946 bond_mc_add(bond
, dmi
->dmi_addr
, dmi
->dmi_addrlen
);
3950 /* looking for addresses to delete from slaves' list */
3951 for (dmi
= bond
->mc_list
; dmi
; dmi
= dmi
->next
) {
3952 if (!bond_mc_list_find_dmi(dmi
, bond_dev
->mc_list
)) {
3953 bond_mc_delete(bond
, dmi
->dmi_addr
, dmi
->dmi_addrlen
);
3957 /* save master's multicast list */
3958 bond_mc_list_destroy(bond
);
3959 bond_mc_list_copy(bond_dev
->mc_list
, bond
, GFP_ATOMIC
);
3961 write_unlock_bh(&bond
->lock
);
3965 * Change the MTU of all of a master's slaves to match the master
3967 static int bond_change_mtu(struct net_device
*bond_dev
, int new_mtu
)
3969 struct bonding
*bond
= bond_dev
->priv
;
3970 struct slave
*slave
, *stop_at
;
3974 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond
,
3975 (bond_dev
? bond_dev
->name
: "None"), new_mtu
);
3977 /* Can't hold bond->lock with bh disabled here since
3978 * some base drivers panic. On the other hand we can't
3979 * hold bond->lock without bh disabled because we'll
3980 * deadlock. The only solution is to rely on the fact
3981 * that we're under rtnl_lock here, and the slaves
3982 * list won't change. This doesn't solve the problem
3983 * of setting the slave's MTU while it is
3984 * transmitting, but the assumption is that the base
3985 * driver can handle that.
3987 * TODO: figure out a way to safely iterate the slaves
3988 * list, but without holding a lock around the actual
3989 * call to the base driver.
3992 bond_for_each_slave(bond
, slave
, i
) {
3993 dprintk("s %p s->p %p c_m %p\n", slave
,
3994 slave
->prev
, slave
->dev
->change_mtu
);
3996 res
= dev_set_mtu(slave
->dev
, new_mtu
);
3999 /* If we failed to set the slave's mtu to the new value
4000 * we must abort the operation even in ACTIVE_BACKUP
4001 * mode, because if we allow the backup slaves to have
4002 * different mtu values than the active slave we'll
4003 * need to change their mtu when doing a failover. That
4004 * means changing their mtu from timer context, which
4005 * is probably not a good idea.
4007 dprintk("err %d %s\n", res
, slave
->dev
->name
);
4012 bond_dev
->mtu
= new_mtu
;
4017 /* unwind from head to the slave that failed */
4019 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
4022 tmp_res
= dev_set_mtu(slave
->dev
, bond_dev
->mtu
);
4024 dprintk("unwind err %d dev %s\n", tmp_res
,
4035 * Note that many devices must be down to change the HW address, and
4036 * downing the master releases all slaves. We can make bonds full of
4037 * bonding devices to test this, however.
4039 static int bond_set_mac_address(struct net_device
*bond_dev
, void *addr
)
4041 struct bonding
*bond
= bond_dev
->priv
;
4042 struct sockaddr
*sa
= addr
, tmp_sa
;
4043 struct slave
*slave
, *stop_at
;
4047 dprintk("bond=%p, name=%s\n", bond
, (bond_dev
? bond_dev
->name
: "None"));
4050 * If fail_over_mac is enabled, do nothing and return success.
4051 * Returning an error causes ifenslave to fail.
4053 if (bond
->params
.fail_over_mac
)
4056 if (!is_valid_ether_addr(sa
->sa_data
)) {
4057 return -EADDRNOTAVAIL
;
4060 /* Can't hold bond->lock with bh disabled here since
4061 * some base drivers panic. On the other hand we can't
4062 * hold bond->lock without bh disabled because we'll
4063 * deadlock. The only solution is to rely on the fact
4064 * that we're under rtnl_lock here, and the slaves
4065 * list won't change. This doesn't solve the problem
4066 * of setting the slave's hw address while it is
4067 * transmitting, but the assumption is that the base
4068 * driver can handle that.
4070 * TODO: figure out a way to safely iterate the slaves
4071 * list, but without holding a lock around the actual
4072 * call to the base driver.
4075 bond_for_each_slave(bond
, slave
, i
) {
4076 dprintk("slave %p %s\n", slave
, slave
->dev
->name
);
4078 if (slave
->dev
->set_mac_address
== NULL
) {
4080 dprintk("EOPNOTSUPP %s\n", slave
->dev
->name
);
4084 res
= dev_set_mac_address(slave
->dev
, addr
);
4086 /* TODO: consider downing the slave
4088 * User should expect communications
4089 * breakage anyway until ARP finish
4092 dprintk("err %d %s\n", res
, slave
->dev
->name
);
4098 memcpy(bond_dev
->dev_addr
, sa
->sa_data
, bond_dev
->addr_len
);
4102 memcpy(tmp_sa
.sa_data
, bond_dev
->dev_addr
, bond_dev
->addr_len
);
4103 tmp_sa
.sa_family
= bond_dev
->type
;
4105 /* unwind from head to the slave that failed */
4107 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
4110 tmp_res
= dev_set_mac_address(slave
->dev
, &tmp_sa
);
4112 dprintk("unwind err %d dev %s\n", tmp_res
,
4120 static int bond_xmit_roundrobin(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4122 struct bonding
*bond
= bond_dev
->priv
;
4123 struct slave
*slave
, *start_at
;
4124 int i
, slave_no
, res
= 1;
4126 read_lock(&bond
->lock
);
4128 if (!BOND_IS_OK(bond
)) {
4133 * Concurrent TX may collide on rr_tx_counter; we accept that
4134 * as being rare enough not to justify using an atomic op here
4136 slave_no
= bond
->rr_tx_counter
++ % bond
->slave_cnt
;
4138 bond_for_each_slave(bond
, slave
, i
) {
4146 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4147 if (IS_UP(slave
->dev
) &&
4148 (slave
->link
== BOND_LINK_UP
) &&
4149 (slave
->state
== BOND_STATE_ACTIVE
)) {
4150 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4157 /* no suitable interface, frame not sent */
4160 read_unlock(&bond
->lock
);
4166 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4167 * the bond has a usable interface.
4169 static int bond_xmit_activebackup(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4171 struct bonding
*bond
= bond_dev
->priv
;
4174 read_lock(&bond
->lock
);
4175 read_lock(&bond
->curr_slave_lock
);
4177 if (!BOND_IS_OK(bond
)) {
4181 if (!bond
->curr_active_slave
)
4184 res
= bond_dev_queue_xmit(bond
, skb
, bond
->curr_active_slave
->dev
);
4188 /* no suitable interface, frame not sent */
4191 read_unlock(&bond
->curr_slave_lock
);
4192 read_unlock(&bond
->lock
);
4197 * In bond_xmit_xor() , we determine the output device by using a pre-
4198 * determined xmit_hash_policy(), If the selected device is not enabled,
4199 * find the next active slave.
4201 static int bond_xmit_xor(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4203 struct bonding
*bond
= bond_dev
->priv
;
4204 struct slave
*slave
, *start_at
;
4209 read_lock(&bond
->lock
);
4211 if (!BOND_IS_OK(bond
)) {
4215 slave_no
= bond
->xmit_hash_policy(skb
, bond_dev
, bond
->slave_cnt
);
4217 bond_for_each_slave(bond
, slave
, i
) {
4226 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4227 if (IS_UP(slave
->dev
) &&
4228 (slave
->link
== BOND_LINK_UP
) &&
4229 (slave
->state
== BOND_STATE_ACTIVE
)) {
4230 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4237 /* no suitable interface, frame not sent */
4240 read_unlock(&bond
->lock
);
4245 * in broadcast mode, we send everything to all usable interfaces.
4247 static int bond_xmit_broadcast(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4249 struct bonding
*bond
= bond_dev
->priv
;
4250 struct slave
*slave
, *start_at
;
4251 struct net_device
*tx_dev
= NULL
;
4255 read_lock(&bond
->lock
);
4257 if (!BOND_IS_OK(bond
)) {
4261 read_lock(&bond
->curr_slave_lock
);
4262 start_at
= bond
->curr_active_slave
;
4263 read_unlock(&bond
->curr_slave_lock
);
4269 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4270 if (IS_UP(slave
->dev
) &&
4271 (slave
->link
== BOND_LINK_UP
) &&
4272 (slave
->state
== BOND_STATE_ACTIVE
)) {
4274 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
4276 printk(KERN_ERR DRV_NAME
4277 ": %s: Error: bond_xmit_broadcast(): "
4278 "skb_clone() failed\n",
4283 res
= bond_dev_queue_xmit(bond
, skb2
, tx_dev
);
4285 dev_kfree_skb(skb2
);
4289 tx_dev
= slave
->dev
;
4294 res
= bond_dev_queue_xmit(bond
, skb
, tx_dev
);
4299 /* no suitable interface, frame not sent */
4302 /* frame sent to all suitable interfaces */
4303 read_unlock(&bond
->lock
);
4307 /*------------------------- Device initialization ---------------------------*/
4310 * set bond mode specific net device operations
4312 void bond_set_mode_ops(struct bonding
*bond
, int mode
)
4314 struct net_device
*bond_dev
= bond
->dev
;
4317 case BOND_MODE_ROUNDROBIN
:
4318 bond_dev
->hard_start_xmit
= bond_xmit_roundrobin
;
4320 case BOND_MODE_ACTIVEBACKUP
:
4321 bond_dev
->hard_start_xmit
= bond_xmit_activebackup
;
4324 bond_dev
->hard_start_xmit
= bond_xmit_xor
;
4325 if (bond
->params
.xmit_policy
== BOND_XMIT_POLICY_LAYER34
)
4326 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l34
;
4328 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l2
;
4330 case BOND_MODE_BROADCAST
:
4331 bond_dev
->hard_start_xmit
= bond_xmit_broadcast
;
4333 case BOND_MODE_8023AD
:
4334 bond_set_master_3ad_flags(bond
);
4335 bond_dev
->hard_start_xmit
= bond_3ad_xmit_xor
;
4336 if (bond
->params
.xmit_policy
== BOND_XMIT_POLICY_LAYER34
)
4337 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l34
;
4339 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l2
;
4342 bond_set_master_alb_flags(bond
);
4345 bond_dev
->hard_start_xmit
= bond_alb_xmit
;
4346 bond_dev
->set_mac_address
= bond_alb_set_mac_address
;
4349 /* Should never happen, mode already checked */
4350 printk(KERN_ERR DRV_NAME
4351 ": %s: Error: Unknown bonding mode %d\n",
4358 static void bond_ethtool_get_drvinfo(struct net_device
*bond_dev
,
4359 struct ethtool_drvinfo
*drvinfo
)
4361 strncpy(drvinfo
->driver
, DRV_NAME
, 32);
4362 strncpy(drvinfo
->version
, DRV_VERSION
, 32);
4363 snprintf(drvinfo
->fw_version
, 32, "%d", BOND_ABI_VERSION
);
4366 static const struct ethtool_ops bond_ethtool_ops
= {
4367 .get_drvinfo
= bond_ethtool_get_drvinfo
,
4371 * Does not allocate but creates a /proc entry.
4374 static int bond_init(struct net_device
*bond_dev
, struct bond_params
*params
)
4376 struct bonding
*bond
= bond_dev
->priv
;
4378 dprintk("Begin bond_init for %s\n", bond_dev
->name
);
4380 /* initialize rwlocks */
4381 rwlock_init(&bond
->lock
);
4382 rwlock_init(&bond
->curr_slave_lock
);
4384 bond
->params
= *params
; /* copy params struct */
4386 bond
->wq
= create_singlethread_workqueue(bond_dev
->name
);
4390 /* Initialize pointers */
4391 bond
->first_slave
= NULL
;
4392 bond
->curr_active_slave
= NULL
;
4393 bond
->current_arp_slave
= NULL
;
4394 bond
->primary_slave
= NULL
;
4395 bond
->dev
= bond_dev
;
4396 bond
->send_grat_arp
= 0;
4397 bond
->setup_by_slave
= 0;
4398 INIT_LIST_HEAD(&bond
->vlan_list
);
4400 /* Initialize the device entry points */
4401 bond_dev
->open
= bond_open
;
4402 bond_dev
->stop
= bond_close
;
4403 bond_dev
->get_stats
= bond_get_stats
;
4404 bond_dev
->do_ioctl
= bond_do_ioctl
;
4405 bond_dev
->ethtool_ops
= &bond_ethtool_ops
;
4406 bond_dev
->set_multicast_list
= bond_set_multicast_list
;
4407 bond_dev
->change_mtu
= bond_change_mtu
;
4408 bond_dev
->set_mac_address
= bond_set_mac_address
;
4409 bond_dev
->validate_addr
= NULL
;
4411 bond_set_mode_ops(bond
, bond
->params
.mode
);
4413 bond_dev
->destructor
= free_netdev
;
4415 /* Initialize the device options */
4416 bond_dev
->tx_queue_len
= 0;
4417 bond_dev
->flags
|= IFF_MASTER
|IFF_MULTICAST
;
4418 bond_dev
->priv_flags
|= IFF_BONDING
;
4420 /* At first, we block adding VLANs. That's the only way to
4421 * prevent problems that occur when adding VLANs over an
4422 * empty bond. The block will be removed once non-challenged
4423 * slaves are enslaved.
4425 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
4427 /* don't acquire bond device's netif_tx_lock when
4429 bond_dev
->features
|= NETIF_F_LLTX
;
4431 /* By default, we declare the bond to be fully
4432 * VLAN hardware accelerated capable. Special
4433 * care is taken in the various xmit functions
4434 * when there are slaves that are not hw accel
4437 bond_dev
->vlan_rx_register
= bond_vlan_rx_register
;
4438 bond_dev
->vlan_rx_add_vid
= bond_vlan_rx_add_vid
;
4439 bond_dev
->vlan_rx_kill_vid
= bond_vlan_rx_kill_vid
;
4440 bond_dev
->features
|= (NETIF_F_HW_VLAN_TX
|
4441 NETIF_F_HW_VLAN_RX
|
4442 NETIF_F_HW_VLAN_FILTER
);
4444 #ifdef CONFIG_PROC_FS
4445 bond_create_proc_entry(bond
);
4447 list_add_tail(&bond
->bond_list
, &bond_dev_list
);
4452 /* De-initialize device specific data.
4453 * Caller must hold rtnl_lock.
4455 static void bond_deinit(struct net_device
*bond_dev
)
4457 struct bonding
*bond
= bond_dev
->priv
;
4459 list_del(&bond
->bond_list
);
4461 #ifdef CONFIG_PROC_FS
4462 bond_remove_proc_entry(bond
);
4466 /* Unregister and free all bond devices.
4467 * Caller must hold rtnl_lock.
4469 static void bond_free_all(void)
4471 struct bonding
*bond
, *nxt
;
4473 list_for_each_entry_safe(bond
, nxt
, &bond_dev_list
, bond_list
) {
4474 struct net_device
*bond_dev
= bond
->dev
;
4476 bond_mc_list_destroy(bond
);
4477 /* Release the bonded slaves */
4478 bond_release_all(bond_dev
);
4479 bond_deinit(bond_dev
);
4480 unregister_netdevice(bond_dev
);
4483 #ifdef CONFIG_PROC_FS
4484 bond_destroy_proc_dir();
4488 /*------------------------- Module initialization ---------------------------*/
4491 * Convert string input module parms. Accept either the
4492 * number of the mode or its string name.
4494 int bond_parse_parm(char *mode_arg
, struct bond_parm_tbl
*tbl
)
4498 for (i
= 0; tbl
[i
].modename
; i
++) {
4499 if ((isdigit(*mode_arg
) &&
4500 tbl
[i
].mode
== simple_strtol(mode_arg
, NULL
, 0)) ||
4501 (strncmp(mode_arg
, tbl
[i
].modename
,
4502 strlen(tbl
[i
].modename
)) == 0)) {
4510 static int bond_check_params(struct bond_params
*params
)
4512 int arp_validate_value
;
4515 * Convert string parameters.
4518 bond_mode
= bond_parse_parm(mode
, bond_mode_tbl
);
4519 if (bond_mode
== -1) {
4520 printk(KERN_ERR DRV_NAME
4521 ": Error: Invalid bonding mode \"%s\"\n",
4522 mode
== NULL
? "NULL" : mode
);
4527 if (xmit_hash_policy
) {
4528 if ((bond_mode
!= BOND_MODE_XOR
) &&
4529 (bond_mode
!= BOND_MODE_8023AD
)) {
4530 printk(KERN_INFO DRV_NAME
4531 ": xor_mode param is irrelevant in mode %s\n",
4532 bond_mode_name(bond_mode
));
4534 xmit_hashtype
= bond_parse_parm(xmit_hash_policy
,
4536 if (xmit_hashtype
== -1) {
4537 printk(KERN_ERR DRV_NAME
4538 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4539 xmit_hash_policy
== NULL
? "NULL" :
4547 if (bond_mode
!= BOND_MODE_8023AD
) {
4548 printk(KERN_INFO DRV_NAME
4549 ": lacp_rate param is irrelevant in mode %s\n",
4550 bond_mode_name(bond_mode
));
4552 lacp_fast
= bond_parse_parm(lacp_rate
, bond_lacp_tbl
);
4553 if (lacp_fast
== -1) {
4554 printk(KERN_ERR DRV_NAME
4555 ": Error: Invalid lacp rate \"%s\"\n",
4556 lacp_rate
== NULL
? "NULL" : lacp_rate
);
4562 if (max_bonds
< 1 || max_bonds
> INT_MAX
) {
4563 printk(KERN_WARNING DRV_NAME
4564 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4565 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4566 max_bonds
, 1, INT_MAX
, BOND_DEFAULT_MAX_BONDS
);
4567 max_bonds
= BOND_DEFAULT_MAX_BONDS
;
4571 printk(KERN_WARNING DRV_NAME
4572 ": Warning: miimon module parameter (%d), "
4573 "not in range 0-%d, so it was reset to %d\n",
4574 miimon
, INT_MAX
, BOND_LINK_MON_INTERV
);
4575 miimon
= BOND_LINK_MON_INTERV
;
4579 printk(KERN_WARNING DRV_NAME
4580 ": Warning: updelay module parameter (%d), "
4581 "not in range 0-%d, so it was reset to 0\n",
4586 if (downdelay
< 0) {
4587 printk(KERN_WARNING DRV_NAME
4588 ": Warning: downdelay module parameter (%d), "
4589 "not in range 0-%d, so it was reset to 0\n",
4590 downdelay
, INT_MAX
);
4594 if ((use_carrier
!= 0) && (use_carrier
!= 1)) {
4595 printk(KERN_WARNING DRV_NAME
4596 ": Warning: use_carrier module parameter (%d), "
4597 "not of valid value (0/1), so it was set to 1\n",
4602 /* reset values for 802.3ad */
4603 if (bond_mode
== BOND_MODE_8023AD
) {
4605 printk(KERN_WARNING DRV_NAME
4606 ": Warning: miimon must be specified, "
4607 "otherwise bonding will not detect link "
4608 "failure, speed and duplex which are "
4609 "essential for 802.3ad operation\n");
4610 printk(KERN_WARNING
"Forcing miimon to 100msec\n");
4615 /* reset values for TLB/ALB */
4616 if ((bond_mode
== BOND_MODE_TLB
) ||
4617 (bond_mode
== BOND_MODE_ALB
)) {
4619 printk(KERN_WARNING DRV_NAME
4620 ": Warning: miimon must be specified, "
4621 "otherwise bonding will not detect link "
4622 "failure and link speed which are essential "
4623 "for TLB/ALB load balancing\n");
4624 printk(KERN_WARNING
"Forcing miimon to 100msec\n");
4629 if (bond_mode
== BOND_MODE_ALB
) {
4630 printk(KERN_NOTICE DRV_NAME
4631 ": In ALB mode you might experience client "
4632 "disconnections upon reconnection of a link if the "
4633 "bonding module updelay parameter (%d msec) is "
4634 "incompatible with the forwarding delay time of the "
4640 if (updelay
|| downdelay
) {
4641 /* just warn the user the up/down delay will have
4642 * no effect since miimon is zero...
4644 printk(KERN_WARNING DRV_NAME
4645 ": Warning: miimon module parameter not set "
4646 "and updelay (%d) or downdelay (%d) module "
4647 "parameter is set; updelay and downdelay have "
4648 "no effect unless miimon is set\n",
4649 updelay
, downdelay
);
4652 /* don't allow arp monitoring */
4654 printk(KERN_WARNING DRV_NAME
4655 ": Warning: miimon (%d) and arp_interval (%d) "
4656 "can't be used simultaneously, disabling ARP "
4658 miimon
, arp_interval
);
4662 if ((updelay
% miimon
) != 0) {
4663 printk(KERN_WARNING DRV_NAME
4664 ": Warning: updelay (%d) is not a multiple "
4665 "of miimon (%d), updelay rounded to %d ms\n",
4666 updelay
, miimon
, (updelay
/ miimon
) * miimon
);
4671 if ((downdelay
% miimon
) != 0) {
4672 printk(KERN_WARNING DRV_NAME
4673 ": Warning: downdelay (%d) is not a multiple "
4674 "of miimon (%d), downdelay rounded to %d ms\n",
4676 (downdelay
/ miimon
) * miimon
);
4679 downdelay
/= miimon
;
4682 if (arp_interval
< 0) {
4683 printk(KERN_WARNING DRV_NAME
4684 ": Warning: arp_interval module parameter (%d) "
4685 ", not in range 0-%d, so it was reset to %d\n",
4686 arp_interval
, INT_MAX
, BOND_LINK_ARP_INTERV
);
4687 arp_interval
= BOND_LINK_ARP_INTERV
;
4690 for (arp_ip_count
= 0;
4691 (arp_ip_count
< BOND_MAX_ARP_TARGETS
) && arp_ip_target
[arp_ip_count
];
4693 /* not complete check, but should be good enough to
4695 if (!isdigit(arp_ip_target
[arp_ip_count
][0])) {
4696 printk(KERN_WARNING DRV_NAME
4697 ": Warning: bad arp_ip_target module parameter "
4698 "(%s), ARP monitoring will not be performed\n",
4699 arp_ip_target
[arp_ip_count
]);
4702 __be32 ip
= in_aton(arp_ip_target
[arp_ip_count
]);
4703 arp_target
[arp_ip_count
] = ip
;
4707 if (arp_interval
&& !arp_ip_count
) {
4708 /* don't allow arping if no arp_ip_target given... */
4709 printk(KERN_WARNING DRV_NAME
4710 ": Warning: arp_interval module parameter (%d) "
4711 "specified without providing an arp_ip_target "
4712 "parameter, arp_interval was reset to 0\n",
4718 if (bond_mode
!= BOND_MODE_ACTIVEBACKUP
) {
4719 printk(KERN_ERR DRV_NAME
4720 ": arp_validate only supported in active-backup mode\n");
4723 if (!arp_interval
) {
4724 printk(KERN_ERR DRV_NAME
4725 ": arp_validate requires arp_interval\n");
4729 arp_validate_value
= bond_parse_parm(arp_validate
,
4731 if (arp_validate_value
== -1) {
4732 printk(KERN_ERR DRV_NAME
4733 ": Error: invalid arp_validate \"%s\"\n",
4734 arp_validate
== NULL
? "NULL" : arp_validate
);
4738 arp_validate_value
= 0;
4741 printk(KERN_INFO DRV_NAME
4742 ": MII link monitoring set to %d ms\n",
4744 } else if (arp_interval
) {
4747 printk(KERN_INFO DRV_NAME
4748 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4750 arp_validate_tbl
[arp_validate_value
].modename
,
4753 for (i
= 0; i
< arp_ip_count
; i
++)
4754 printk (" %s", arp_ip_target
[i
]);
4759 /* miimon and arp_interval not set, we need one so things
4760 * work as expected, see bonding.txt for details
4762 printk(KERN_WARNING DRV_NAME
4763 ": Warning: either miimon or arp_interval and "
4764 "arp_ip_target module parameters must be specified, "
4765 "otherwise bonding will not detect link failures! see "
4766 "bonding.txt for details.\n");
4769 if (primary
&& !USES_PRIMARY(bond_mode
)) {
4770 /* currently, using a primary only makes sense
4771 * in active backup, TLB or ALB modes
4773 printk(KERN_WARNING DRV_NAME
4774 ": Warning: %s primary device specified but has no "
4775 "effect in %s mode\n",
4776 primary
, bond_mode_name(bond_mode
));
4780 if (fail_over_mac
&& (bond_mode
!= BOND_MODE_ACTIVEBACKUP
))
4781 printk(KERN_WARNING DRV_NAME
4782 ": Warning: fail_over_mac only affects "
4783 "active-backup mode.\n");
4785 /* fill params struct with the proper values */
4786 params
->mode
= bond_mode
;
4787 params
->xmit_policy
= xmit_hashtype
;
4788 params
->miimon
= miimon
;
4789 params
->arp_interval
= arp_interval
;
4790 params
->arp_validate
= arp_validate_value
;
4791 params
->updelay
= updelay
;
4792 params
->downdelay
= downdelay
;
4793 params
->use_carrier
= use_carrier
;
4794 params
->lacp_fast
= lacp_fast
;
4795 params
->primary
[0] = 0;
4796 params
->fail_over_mac
= fail_over_mac
;
4799 strncpy(params
->primary
, primary
, IFNAMSIZ
);
4800 params
->primary
[IFNAMSIZ
- 1] = 0;
4803 memcpy(params
->arp_targets
, arp_target
, sizeof(arp_target
));
4808 static struct lock_class_key bonding_netdev_xmit_lock_key
;
4810 /* Create a new bond based on the specified name and bonding parameters.
4811 * If name is NULL, obtain a suitable "bond%d" name for us.
4812 * Caller must NOT hold rtnl_lock; we need to release it here before we
4813 * set up our sysfs entries.
4815 int bond_create(char *name
, struct bond_params
*params
, struct bonding
**newbond
)
4817 struct net_device
*bond_dev
;
4821 bond_dev
= alloc_netdev(sizeof(struct bonding
), name
? name
: "",
4824 printk(KERN_ERR DRV_NAME
4825 ": %s: eek! can't alloc netdev!\n",
4832 res
= dev_alloc_name(bond_dev
, "bond%d");
4837 /* bond_init() must be called after dev_alloc_name() (for the
4838 * /proc files), but before register_netdevice(), because we
4839 * need to set function pointers.
4842 res
= bond_init(bond_dev
, params
);
4847 res
= register_netdevice(bond_dev
);
4852 lockdep_set_class(&bond_dev
->_xmit_lock
, &bonding_netdev_xmit_lock_key
);
4855 *newbond
= bond_dev
->priv
;
4857 netif_carrier_off(bond_dev
);
4859 rtnl_unlock(); /* allows sysfs registration of net device */
4860 res
= bond_create_sysfs_entry(bond_dev
->priv
);
4869 bond_deinit(bond_dev
);
4871 free_netdev(bond_dev
);
4877 static void bond_work_cancel_all(struct bonding
*bond
)
4879 write_lock_bh(&bond
->lock
);
4880 bond
->kill_timers
= 1;
4881 write_unlock_bh(&bond
->lock
);
4883 if (bond
->params
.miimon
&& delayed_work_pending(&bond
->mii_work
))
4884 cancel_delayed_work(&bond
->mii_work
);
4886 if (bond
->params
.arp_interval
&& delayed_work_pending(&bond
->arp_work
))
4887 cancel_delayed_work(&bond
->arp_work
);
4889 if (bond
->params
.mode
== BOND_MODE_ALB
&&
4890 delayed_work_pending(&bond
->alb_work
))
4891 cancel_delayed_work(&bond
->alb_work
);
4893 if (bond
->params
.mode
== BOND_MODE_8023AD
&&
4894 delayed_work_pending(&bond
->ad_work
))
4895 cancel_delayed_work(&bond
->ad_work
);
4898 static int __init
bonding_init(void)
4902 struct bonding
*bond
, *nxt
;
4904 printk(KERN_INFO
"%s", version
);
4906 res
= bond_check_params(&bonding_defaults
);
4911 #ifdef CONFIG_PROC_FS
4912 bond_create_proc_dir();
4914 for (i
= 0; i
< max_bonds
; i
++) {
4915 res
= bond_create(NULL
, &bonding_defaults
, NULL
);
4920 res
= bond_create_sysfs();
4924 register_netdevice_notifier(&bond_netdev_notifier
);
4925 register_inetaddr_notifier(&bond_inetaddr_notifier
);
4929 list_for_each_entry_safe(bond
, nxt
, &bond_dev_list
, bond_list
) {
4930 bond_work_cancel_all(bond
);
4931 destroy_workqueue(bond
->wq
);
4936 bond_destroy_sysfs();
4943 static void __exit
bonding_exit(void)
4945 unregister_netdevice_notifier(&bond_netdev_notifier
);
4946 unregister_inetaddr_notifier(&bond_inetaddr_notifier
);
4950 bond_destroy_sysfs();
4954 module_init(bonding_init
);
4955 module_exit(bonding_exit
);
4956 MODULE_LICENSE("GPL");
4957 MODULE_VERSION(DRV_VERSION
);
4958 MODULE_DESCRIPTION(DRV_DESCRIPTION
", v" DRV_VERSION
);
4959 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4960 MODULE_SUPPORTED_DEVICE("most ethernet devices");