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 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
44 #include <linux/tcp.h>
45 #include <linux/udp.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/init.h>
49 #include <linux/timer.h>
50 #include <linux/socket.h>
51 #include <linux/ctype.h>
52 #include <linux/inet.h>
53 #include <linux/bitops.h>
55 #include <asm/system.h>
57 #include <linux/uaccess.h>
58 #include <linux/errno.h>
59 #include <linux/netdevice.h>
60 #include <linux/inetdevice.h>
61 #include <linux/igmp.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
65 #include <linux/rtnetlink.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/smp.h>
69 #include <linux/if_ether.h>
71 #include <linux/mii.h>
72 #include <linux/ethtool.h>
73 #include <linux/if_vlan.h>
74 #include <linux/if_bonding.h>
75 #include <linux/jiffies.h>
76 #include <net/route.h>
77 #include <net/net_namespace.h>
82 /*---------------------------- Module parameters ----------------------------*/
84 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
85 #define BOND_LINK_MON_INTERV 0
86 #define BOND_LINK_ARP_INTERV 0
88 static int max_bonds
= BOND_DEFAULT_MAX_BONDS
;
89 static int num_grat_arp
= 1;
90 static int num_unsol_na
= 1;
91 static int miimon
= BOND_LINK_MON_INTERV
;
94 static int use_carrier
= 1;
97 static char *primary_reselect
;
98 static char *lacp_rate
;
99 static char *ad_select
;
100 static char *xmit_hash_policy
;
101 static int arp_interval
= BOND_LINK_ARP_INTERV
;
102 static char *arp_ip_target
[BOND_MAX_ARP_TARGETS
];
103 static char *arp_validate
;
104 static char *fail_over_mac
;
105 static struct bond_params bonding_defaults
;
107 module_param(max_bonds
, int, 0);
108 MODULE_PARM_DESC(max_bonds
, "Max number of bonded devices");
109 module_param(num_grat_arp
, int, 0644);
110 MODULE_PARM_DESC(num_grat_arp
, "Number of gratuitous ARP packets to send on failover event");
111 module_param(num_unsol_na
, int, 0644);
112 MODULE_PARM_DESC(num_unsol_na
, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
113 module_param(miimon
, int, 0);
114 MODULE_PARM_DESC(miimon
, "Link check interval in milliseconds");
115 module_param(updelay
, int, 0);
116 MODULE_PARM_DESC(updelay
, "Delay before considering link up, in milliseconds");
117 module_param(downdelay
, int, 0);
118 MODULE_PARM_DESC(downdelay
, "Delay before considering link down, "
120 module_param(use_carrier
, int, 0);
121 MODULE_PARM_DESC(use_carrier
, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
122 "0 for off, 1 for on (default)");
123 module_param(mode
, charp
, 0);
124 MODULE_PARM_DESC(mode
, "Mode of operation : 0 for balance-rr, "
125 "1 for active-backup, 2 for balance-xor, "
126 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
127 "6 for balance-alb");
128 module_param(primary
, charp
, 0);
129 MODULE_PARM_DESC(primary
, "Primary network device to use");
130 module_param(primary_reselect
, charp
, 0);
131 MODULE_PARM_DESC(primary_reselect
, "Reselect primary slave "
133 "0 for always (default), "
134 "1 for only if speed of primary is "
136 "2 for only on active slave "
138 module_param(lacp_rate
, charp
, 0);
139 MODULE_PARM_DESC(lacp_rate
, "LACPDU tx rate to request from 802.3ad partner "
141 module_param(ad_select
, charp
, 0);
142 MODULE_PARM_DESC(ad_select
, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
143 module_param(xmit_hash_policy
, charp
, 0);
144 MODULE_PARM_DESC(xmit_hash_policy
, "XOR hashing method: 0 for layer 2 (default)"
145 ", 1 for layer 3+4");
146 module_param(arp_interval
, int, 0);
147 MODULE_PARM_DESC(arp_interval
, "arp interval in milliseconds");
148 module_param_array(arp_ip_target
, charp
, NULL
, 0);
149 MODULE_PARM_DESC(arp_ip_target
, "arp targets in n.n.n.n form");
150 module_param(arp_validate
, charp
, 0);
151 MODULE_PARM_DESC(arp_validate
, "validate src/dst of ARP probes: none (default), active, backup or all");
152 module_param(fail_over_mac
, charp
, 0);
153 MODULE_PARM_DESC(fail_over_mac
, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
155 /*----------------------------- Global variables ----------------------------*/
157 static const char * const version
=
158 DRV_DESCRIPTION
": v" DRV_VERSION
" (" DRV_RELDATE
")\n";
160 LIST_HEAD(bond_dev_list
);
162 #ifdef CONFIG_PROC_FS
163 static struct proc_dir_entry
*bond_proc_dir
;
166 static __be32 arp_target
[BOND_MAX_ARP_TARGETS
];
167 static int arp_ip_count
;
168 static int bond_mode
= BOND_MODE_ROUNDROBIN
;
169 static int xmit_hashtype
= BOND_XMIT_POLICY_LAYER2
;
170 static int lacp_fast
;
173 const struct bond_parm_tbl bond_lacp_tbl
[] = {
174 { "slow", AD_LACP_SLOW
},
175 { "fast", AD_LACP_FAST
},
179 const struct bond_parm_tbl bond_mode_tbl
[] = {
180 { "balance-rr", BOND_MODE_ROUNDROBIN
},
181 { "active-backup", BOND_MODE_ACTIVEBACKUP
},
182 { "balance-xor", BOND_MODE_XOR
},
183 { "broadcast", BOND_MODE_BROADCAST
},
184 { "802.3ad", BOND_MODE_8023AD
},
185 { "balance-tlb", BOND_MODE_TLB
},
186 { "balance-alb", BOND_MODE_ALB
},
190 const struct bond_parm_tbl xmit_hashtype_tbl
[] = {
191 { "layer2", BOND_XMIT_POLICY_LAYER2
},
192 { "layer3+4", BOND_XMIT_POLICY_LAYER34
},
193 { "layer2+3", BOND_XMIT_POLICY_LAYER23
},
197 const struct bond_parm_tbl arp_validate_tbl
[] = {
198 { "none", BOND_ARP_VALIDATE_NONE
},
199 { "active", BOND_ARP_VALIDATE_ACTIVE
},
200 { "backup", BOND_ARP_VALIDATE_BACKUP
},
201 { "all", BOND_ARP_VALIDATE_ALL
},
205 const struct bond_parm_tbl fail_over_mac_tbl
[] = {
206 { "none", BOND_FOM_NONE
},
207 { "active", BOND_FOM_ACTIVE
},
208 { "follow", BOND_FOM_FOLLOW
},
212 const struct bond_parm_tbl pri_reselect_tbl
[] = {
213 { "always", BOND_PRI_RESELECT_ALWAYS
},
214 { "better", BOND_PRI_RESELECT_BETTER
},
215 { "failure", BOND_PRI_RESELECT_FAILURE
},
219 struct bond_parm_tbl ad_select_tbl
[] = {
220 { "stable", BOND_AD_STABLE
},
221 { "bandwidth", BOND_AD_BANDWIDTH
},
222 { "count", BOND_AD_COUNT
},
226 /*-------------------------- Forward declarations ---------------------------*/
228 static void bond_send_gratuitous_arp(struct bonding
*bond
);
229 static int bond_init(struct net_device
*bond_dev
);
230 static void bond_uninit(struct net_device
*bond_dev
);
232 /*---------------------------- General routines -----------------------------*/
234 static const char *bond_mode_name(int mode
)
236 static const char *names
[] = {
237 [BOND_MODE_ROUNDROBIN
] = "load balancing (round-robin)",
238 [BOND_MODE_ACTIVEBACKUP
] = "fault-tolerance (active-backup)",
239 [BOND_MODE_XOR
] = "load balancing (xor)",
240 [BOND_MODE_BROADCAST
] = "fault-tolerance (broadcast)",
241 [BOND_MODE_8023AD
] = "IEEE 802.3ad Dynamic link aggregation",
242 [BOND_MODE_TLB
] = "transmit load balancing",
243 [BOND_MODE_ALB
] = "adaptive load balancing",
246 if (mode
< 0 || mode
> BOND_MODE_ALB
)
252 /*---------------------------------- VLAN -----------------------------------*/
255 * bond_add_vlan - add a new vlan id on bond
256 * @bond: bond that got the notification
257 * @vlan_id: the vlan id to add
259 * Returns -ENOMEM if allocation failed.
261 static int bond_add_vlan(struct bonding
*bond
, unsigned short vlan_id
)
263 struct vlan_entry
*vlan
;
265 pr_debug("bond: %s, vlan id %d\n",
266 (bond
? bond
->dev
->name
: "None"), vlan_id
);
268 vlan
= kzalloc(sizeof(struct vlan_entry
), GFP_KERNEL
);
272 INIT_LIST_HEAD(&vlan
->vlan_list
);
273 vlan
->vlan_id
= vlan_id
;
275 write_lock_bh(&bond
->lock
);
277 list_add_tail(&vlan
->vlan_list
, &bond
->vlan_list
);
279 write_unlock_bh(&bond
->lock
);
281 pr_debug("added VLAN ID %d on bond %s\n", vlan_id
, bond
->dev
->name
);
287 * bond_del_vlan - delete a vlan id from bond
288 * @bond: bond that got the notification
289 * @vlan_id: the vlan id to delete
291 * returns -ENODEV if @vlan_id was not found in @bond.
293 static int bond_del_vlan(struct bonding
*bond
, unsigned short vlan_id
)
295 struct vlan_entry
*vlan
;
298 pr_debug("bond: %s, vlan id %d\n", bond
->dev
->name
, vlan_id
);
300 write_lock_bh(&bond
->lock
);
302 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
303 if (vlan
->vlan_id
== vlan_id
) {
304 list_del(&vlan
->vlan_list
);
306 if (bond_is_lb(bond
))
307 bond_alb_clear_vlan(bond
, vlan_id
);
309 pr_debug("removed VLAN ID %d from bond %s\n", vlan_id
,
314 if (list_empty(&bond
->vlan_list
) &&
315 (bond
->slave_cnt
== 0)) {
316 /* Last VLAN removed and no slaves, so
317 * restore block on adding VLANs. This will
318 * be removed once new slaves that are not
319 * VLAN challenged will be added.
321 bond
->dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
329 pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id
,
333 write_unlock_bh(&bond
->lock
);
338 * bond_has_challenged_slaves
339 * @bond: the bond we're working on
341 * Searches the slave list. Returns 1 if a vlan challenged slave
342 * was found, 0 otherwise.
344 * Assumes bond->lock is held.
346 static int bond_has_challenged_slaves(struct bonding
*bond
)
351 bond_for_each_slave(bond
, slave
, i
) {
352 if (slave
->dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
353 pr_debug("found VLAN challenged slave - %s\n",
359 pr_debug("no VLAN challenged slaves found\n");
364 * bond_next_vlan - safely skip to the next item in the vlans list.
365 * @bond: the bond we're working on
366 * @curr: item we're advancing from
368 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
369 * or @curr->next otherwise (even if it is @curr itself again).
371 * Caller must hold bond->lock
373 struct vlan_entry
*bond_next_vlan(struct bonding
*bond
, struct vlan_entry
*curr
)
375 struct vlan_entry
*next
, *last
;
377 if (list_empty(&bond
->vlan_list
))
381 next
= list_entry(bond
->vlan_list
.next
,
382 struct vlan_entry
, vlan_list
);
384 last
= list_entry(bond
->vlan_list
.prev
,
385 struct vlan_entry
, vlan_list
);
387 next
= list_entry(bond
->vlan_list
.next
,
388 struct vlan_entry
, vlan_list
);
390 next
= list_entry(curr
->vlan_list
.next
,
391 struct vlan_entry
, vlan_list
);
399 * bond_dev_queue_xmit - Prepare skb for xmit.
401 * @bond: bond device that got this skb for tx.
402 * @skb: hw accel VLAN tagged skb to transmit
403 * @slave_dev: slave that is supposed to xmit this skbuff
405 * When the bond gets an skb to transmit that is
406 * already hardware accelerated VLAN tagged, and it
407 * needs to relay this skb to a slave that is not
408 * hw accel capable, the skb needs to be "unaccelerated",
409 * i.e. strip the hwaccel tag and re-insert it as part
412 int bond_dev_queue_xmit(struct bonding
*bond
, struct sk_buff
*skb
,
413 struct net_device
*slave_dev
)
415 unsigned short uninitialized_var(vlan_id
);
417 if (!list_empty(&bond
->vlan_list
) &&
418 !(slave_dev
->features
& NETIF_F_HW_VLAN_TX
) &&
419 vlan_get_tag(skb
, &vlan_id
) == 0) {
420 skb
->dev
= slave_dev
;
421 skb
= vlan_put_tag(skb
, vlan_id
);
423 /* vlan_put_tag() frees the skb in case of error,
424 * so return success here so the calling functions
425 * won't attempt to free is again.
430 skb
->dev
= slave_dev
;
440 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
441 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
443 * a. This operation is performed in IOCTL context,
444 * b. The operation is protected by the RTNL semaphore in the 8021q code,
445 * c. Holding a lock with BH disabled while directly calling a base driver
446 * entry point is generally a BAD idea.
448 * The design of synchronization/protection for this operation in the 8021q
449 * module is good for one or more VLAN devices over a single physical device
450 * and cannot be extended for a teaming solution like bonding, so there is a
451 * potential race condition here where a net device from the vlan group might
452 * be referenced (either by a base driver or the 8021q code) while it is being
453 * removed from the system. However, it turns out we're not making matters
454 * worse, and if it works for regular VLAN usage it will work here too.
458 * bond_vlan_rx_register - Propagates registration to slaves
459 * @bond_dev: bonding net device that got called
460 * @grp: vlan group being registered
462 static void bond_vlan_rx_register(struct net_device
*bond_dev
,
463 struct vlan_group
*grp
)
465 struct bonding
*bond
= netdev_priv(bond_dev
);
471 bond_for_each_slave(bond
, slave
, i
) {
472 struct net_device
*slave_dev
= slave
->dev
;
473 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
475 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
476 slave_ops
->ndo_vlan_rx_register
) {
477 slave_ops
->ndo_vlan_rx_register(slave_dev
, grp
);
483 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
484 * @bond_dev: bonding net device that got called
485 * @vid: vlan id being added
487 static void bond_vlan_rx_add_vid(struct net_device
*bond_dev
, uint16_t vid
)
489 struct bonding
*bond
= netdev_priv(bond_dev
);
493 bond_for_each_slave(bond
, slave
, i
) {
494 struct net_device
*slave_dev
= slave
->dev
;
495 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
497 if ((slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
498 slave_ops
->ndo_vlan_rx_add_vid
) {
499 slave_ops
->ndo_vlan_rx_add_vid(slave_dev
, vid
);
503 res
= bond_add_vlan(bond
, vid
);
506 ": %s: Error: Failed to add vlan id %d\n",
507 bond_dev
->name
, vid
);
512 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
513 * @bond_dev: bonding net device that got called
514 * @vid: vlan id being removed
516 static void bond_vlan_rx_kill_vid(struct net_device
*bond_dev
, uint16_t vid
)
518 struct bonding
*bond
= netdev_priv(bond_dev
);
520 struct net_device
*vlan_dev
;
523 bond_for_each_slave(bond
, slave
, i
) {
524 struct net_device
*slave_dev
= slave
->dev
;
525 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
527 if ((slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
528 slave_ops
->ndo_vlan_rx_kill_vid
) {
529 /* Save and then restore vlan_dev in the grp array,
530 * since the slave's driver might clear it.
532 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vid
);
533 slave_ops
->ndo_vlan_rx_kill_vid(slave_dev
, vid
);
534 vlan_group_set_device(bond
->vlgrp
, vid
, vlan_dev
);
538 res
= bond_del_vlan(bond
, vid
);
541 ": %s: Error: Failed to remove vlan id %d\n",
542 bond_dev
->name
, vid
);
546 static void bond_add_vlans_on_slave(struct bonding
*bond
, struct net_device
*slave_dev
)
548 struct vlan_entry
*vlan
;
549 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
551 write_lock_bh(&bond
->lock
);
553 if (list_empty(&bond
->vlan_list
))
556 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
557 slave_ops
->ndo_vlan_rx_register
)
558 slave_ops
->ndo_vlan_rx_register(slave_dev
, bond
->vlgrp
);
560 if (!(slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) ||
561 !(slave_ops
->ndo_vlan_rx_add_vid
))
564 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
)
565 slave_ops
->ndo_vlan_rx_add_vid(slave_dev
, vlan
->vlan_id
);
568 write_unlock_bh(&bond
->lock
);
571 static void bond_del_vlans_from_slave(struct bonding
*bond
,
572 struct net_device
*slave_dev
)
574 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
575 struct vlan_entry
*vlan
;
576 struct net_device
*vlan_dev
;
578 write_lock_bh(&bond
->lock
);
580 if (list_empty(&bond
->vlan_list
))
583 if (!(slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) ||
584 !(slave_ops
->ndo_vlan_rx_kill_vid
))
587 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
588 /* Save and then restore vlan_dev in the grp array,
589 * since the slave's driver might clear it.
591 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
592 slave_ops
->ndo_vlan_rx_kill_vid(slave_dev
, vlan
->vlan_id
);
593 vlan_group_set_device(bond
->vlgrp
, vlan
->vlan_id
, vlan_dev
);
597 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
598 slave_ops
->ndo_vlan_rx_register
)
599 slave_ops
->ndo_vlan_rx_register(slave_dev
, NULL
);
602 write_unlock_bh(&bond
->lock
);
605 /*------------------------------- Link status -------------------------------*/
608 * Set the carrier state for the master according to the state of its
609 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
610 * do special 802.3ad magic.
612 * Returns zero if carrier state does not change, nonzero if it does.
614 static int bond_set_carrier(struct bonding
*bond
)
619 if (bond
->slave_cnt
== 0)
622 if (bond
->params
.mode
== BOND_MODE_8023AD
)
623 return bond_3ad_set_carrier(bond
);
625 bond_for_each_slave(bond
, slave
, i
) {
626 if (slave
->link
== BOND_LINK_UP
) {
627 if (!netif_carrier_ok(bond
->dev
)) {
628 netif_carrier_on(bond
->dev
);
636 if (netif_carrier_ok(bond
->dev
)) {
637 netif_carrier_off(bond
->dev
);
644 * Get link speed and duplex from the slave's base driver
645 * using ethtool. If for some reason the call fails or the
646 * values are invalid, fake speed and duplex to 100/Full
649 static int bond_update_speed_duplex(struct slave
*slave
)
651 struct net_device
*slave_dev
= slave
->dev
;
652 struct ethtool_cmd etool
;
655 /* Fake speed and duplex */
656 slave
->speed
= SPEED_100
;
657 slave
->duplex
= DUPLEX_FULL
;
659 if (!slave_dev
->ethtool_ops
|| !slave_dev
->ethtool_ops
->get_settings
)
662 res
= slave_dev
->ethtool_ops
->get_settings(slave_dev
, &etool
);
666 switch (etool
.speed
) {
676 switch (etool
.duplex
) {
684 slave
->speed
= etool
.speed
;
685 slave
->duplex
= etool
.duplex
;
691 * if <dev> supports MII link status reporting, check its link status.
693 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
694 * depending upon the setting of the use_carrier parameter.
696 * Return either BMSR_LSTATUS, meaning that the link is up (or we
697 * can't tell and just pretend it is), or 0, meaning that the link is
700 * If reporting is non-zero, instead of faking link up, return -1 if
701 * both ETHTOOL and MII ioctls fail (meaning the device does not
702 * support them). If use_carrier is set, return whatever it says.
703 * It'd be nice if there was a good way to tell if a driver supports
704 * netif_carrier, but there really isn't.
706 static int bond_check_dev_link(struct bonding
*bond
,
707 struct net_device
*slave_dev
, int reporting
)
709 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
710 int (*ioctl
)(struct net_device
*, struct ifreq
*, int);
712 struct mii_ioctl_data
*mii
;
714 if (!reporting
&& !netif_running(slave_dev
))
717 if (bond
->params
.use_carrier
)
718 return netif_carrier_ok(slave_dev
) ? BMSR_LSTATUS
: 0;
720 /* Try to get link status using Ethtool first. */
721 if (slave_dev
->ethtool_ops
) {
722 if (slave_dev
->ethtool_ops
->get_link
) {
725 link
= slave_dev
->ethtool_ops
->get_link(slave_dev
);
727 return link
? BMSR_LSTATUS
: 0;
731 /* Ethtool can't be used, fallback to MII ioctls. */
732 ioctl
= slave_ops
->ndo_do_ioctl
;
734 /* TODO: set pointer to correct ioctl on a per team member */
735 /* bases to make this more efficient. that is, once */
736 /* we determine the correct ioctl, we will always */
737 /* call it and not the others for that team */
741 * We cannot assume that SIOCGMIIPHY will also read a
742 * register; not all network drivers (e.g., e100)
746 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
747 strncpy(ifr
.ifr_name
, slave_dev
->name
, IFNAMSIZ
);
749 if (IOCTL(slave_dev
, &ifr
, SIOCGMIIPHY
) == 0) {
750 mii
->reg_num
= MII_BMSR
;
751 if (IOCTL(slave_dev
, &ifr
, SIOCGMIIREG
) == 0)
752 return mii
->val_out
& BMSR_LSTATUS
;
757 * If reporting, report that either there's no dev->do_ioctl,
758 * or both SIOCGMIIREG and get_link failed (meaning that we
759 * cannot report link status). If not reporting, pretend
762 return reporting
? -1 : BMSR_LSTATUS
;
765 /*----------------------------- Multicast list ------------------------------*/
768 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
770 static inline int bond_is_dmi_same(const struct dev_mc_list
*dmi1
,
771 const struct dev_mc_list
*dmi2
)
773 return memcmp(dmi1
->dmi_addr
, dmi2
->dmi_addr
, dmi1
->dmi_addrlen
) == 0 &&
774 dmi1
->dmi_addrlen
== dmi2
->dmi_addrlen
;
778 * returns dmi entry if found, NULL otherwise
780 static struct dev_mc_list
*bond_mc_list_find_dmi(struct dev_mc_list
*dmi
,
781 struct dev_mc_list
*mc_list
)
783 struct dev_mc_list
*idmi
;
785 for (idmi
= mc_list
; idmi
; idmi
= idmi
->next
) {
786 if (bond_is_dmi_same(dmi
, idmi
))
794 * Push the promiscuity flag down to appropriate slaves
796 static int bond_set_promiscuity(struct bonding
*bond
, int inc
)
799 if (USES_PRIMARY(bond
->params
.mode
)) {
800 /* write lock already acquired */
801 if (bond
->curr_active_slave
) {
802 err
= dev_set_promiscuity(bond
->curr_active_slave
->dev
,
808 bond_for_each_slave(bond
, slave
, i
) {
809 err
= dev_set_promiscuity(slave
->dev
, inc
);
818 * Push the allmulti flag down to all slaves
820 static int bond_set_allmulti(struct bonding
*bond
, int inc
)
823 if (USES_PRIMARY(bond
->params
.mode
)) {
824 /* write lock already acquired */
825 if (bond
->curr_active_slave
) {
826 err
= dev_set_allmulti(bond
->curr_active_slave
->dev
,
832 bond_for_each_slave(bond
, slave
, i
) {
833 err
= dev_set_allmulti(slave
->dev
, inc
);
842 * Add a Multicast address to slaves
845 static void bond_mc_add(struct bonding
*bond
, void *addr
, int alen
)
847 if (USES_PRIMARY(bond
->params
.mode
)) {
848 /* write lock already acquired */
849 if (bond
->curr_active_slave
)
850 dev_mc_add(bond
->curr_active_slave
->dev
, addr
, alen
, 0);
855 bond_for_each_slave(bond
, slave
, i
)
856 dev_mc_add(slave
->dev
, addr
, alen
, 0);
861 * Remove a multicast address from slave
864 static void bond_mc_delete(struct bonding
*bond
, void *addr
, int alen
)
866 if (USES_PRIMARY(bond
->params
.mode
)) {
867 /* write lock already acquired */
868 if (bond
->curr_active_slave
)
869 dev_mc_delete(bond
->curr_active_slave
->dev
, addr
,
874 bond_for_each_slave(bond
, slave
, i
) {
875 dev_mc_delete(slave
->dev
, addr
, alen
, 0);
882 * Retrieve the list of registered multicast addresses for the bonding
883 * device and retransmit an IGMP JOIN request to the current active
886 static void bond_resend_igmp_join_requests(struct bonding
*bond
)
888 struct in_device
*in_dev
;
889 struct ip_mc_list
*im
;
892 in_dev
= __in_dev_get_rcu(bond
->dev
);
894 for (im
= in_dev
->mc_list
; im
; im
= im
->next
)
895 ip_mc_rejoin_group(im
);
902 * Totally destroys the mc_list in bond
904 static void bond_mc_list_destroy(struct bonding
*bond
)
906 struct dev_mc_list
*dmi
;
910 bond
->mc_list
= dmi
->next
;
915 bond
->mc_list
= NULL
;
919 * Copy all the Multicast addresses from src to the bonding device dst
921 static int bond_mc_list_copy(struct dev_mc_list
*mc_list
, struct bonding
*bond
,
924 struct dev_mc_list
*dmi
, *new_dmi
;
926 for (dmi
= mc_list
; dmi
; dmi
= dmi
->next
) {
927 new_dmi
= kmalloc(sizeof(struct dev_mc_list
), gfp_flag
);
930 /* FIXME: Potential memory leak !!! */
934 new_dmi
->next
= bond
->mc_list
;
935 bond
->mc_list
= new_dmi
;
936 new_dmi
->dmi_addrlen
= dmi
->dmi_addrlen
;
937 memcpy(new_dmi
->dmi_addr
, dmi
->dmi_addr
, dmi
->dmi_addrlen
);
938 new_dmi
->dmi_users
= dmi
->dmi_users
;
939 new_dmi
->dmi_gusers
= dmi
->dmi_gusers
;
946 * flush all members of flush->mc_list from device dev->mc_list
948 static void bond_mc_list_flush(struct net_device
*bond_dev
,
949 struct net_device
*slave_dev
)
951 struct bonding
*bond
= netdev_priv(bond_dev
);
952 struct dev_mc_list
*dmi
;
954 for (dmi
= bond_dev
->mc_list
; dmi
; dmi
= dmi
->next
)
955 dev_mc_delete(slave_dev
, dmi
->dmi_addr
, dmi
->dmi_addrlen
, 0);
957 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
958 /* del lacpdu mc addr from mc list */
959 u8 lacpdu_multicast
[ETH_ALEN
] = MULTICAST_LACPDU_ADDR
;
961 dev_mc_delete(slave_dev
, lacpdu_multicast
, ETH_ALEN
, 0);
965 /*--------------------------- Active slave change ---------------------------*/
968 * Update the mc list and multicast-related flags for the new and
969 * old active slaves (if any) according to the multicast mode, and
970 * promiscuous flags unconditionally.
972 static void bond_mc_swap(struct bonding
*bond
, struct slave
*new_active
,
973 struct slave
*old_active
)
975 struct dev_mc_list
*dmi
;
977 if (!USES_PRIMARY(bond
->params
.mode
))
978 /* nothing to do - mc list is already up-to-date on
984 if (bond
->dev
->flags
& IFF_PROMISC
)
985 dev_set_promiscuity(old_active
->dev
, -1);
987 if (bond
->dev
->flags
& IFF_ALLMULTI
)
988 dev_set_allmulti(old_active
->dev
, -1);
990 for (dmi
= bond
->dev
->mc_list
; dmi
; dmi
= dmi
->next
)
991 dev_mc_delete(old_active
->dev
, dmi
->dmi_addr
,
992 dmi
->dmi_addrlen
, 0);
996 /* FIXME: Signal errors upstream. */
997 if (bond
->dev
->flags
& IFF_PROMISC
)
998 dev_set_promiscuity(new_active
->dev
, 1);
1000 if (bond
->dev
->flags
& IFF_ALLMULTI
)
1001 dev_set_allmulti(new_active
->dev
, 1);
1003 for (dmi
= bond
->dev
->mc_list
; dmi
; dmi
= dmi
->next
)
1004 dev_mc_add(new_active
->dev
, dmi
->dmi_addr
,
1005 dmi
->dmi_addrlen
, 0);
1006 bond_resend_igmp_join_requests(bond
);
1011 * bond_do_fail_over_mac
1013 * Perform special MAC address swapping for fail_over_mac settings
1015 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1017 static void bond_do_fail_over_mac(struct bonding
*bond
,
1018 struct slave
*new_active
,
1019 struct slave
*old_active
)
1020 __releases(&bond
->curr_slave_lock
)
1021 __releases(&bond
->lock
)
1022 __acquires(&bond
->lock
)
1023 __acquires(&bond
->curr_slave_lock
)
1025 u8 tmp_mac
[ETH_ALEN
];
1026 struct sockaddr saddr
;
1029 switch (bond
->params
.fail_over_mac
) {
1030 case BOND_FOM_ACTIVE
:
1032 memcpy(bond
->dev
->dev_addr
, new_active
->dev
->dev_addr
,
1033 new_active
->dev
->addr_len
);
1035 case BOND_FOM_FOLLOW
:
1037 * if new_active && old_active, swap them
1038 * if just old_active, do nothing (going to no active slave)
1039 * if just new_active, set new_active to bond's MAC
1044 write_unlock_bh(&bond
->curr_slave_lock
);
1045 read_unlock(&bond
->lock
);
1048 memcpy(tmp_mac
, new_active
->dev
->dev_addr
, ETH_ALEN
);
1049 memcpy(saddr
.sa_data
, old_active
->dev
->dev_addr
,
1051 saddr
.sa_family
= new_active
->dev
->type
;
1053 memcpy(saddr
.sa_data
, bond
->dev
->dev_addr
, ETH_ALEN
);
1054 saddr
.sa_family
= bond
->dev
->type
;
1057 rv
= dev_set_mac_address(new_active
->dev
, &saddr
);
1060 ": %s: Error %d setting MAC of slave %s\n",
1061 bond
->dev
->name
, -rv
, new_active
->dev
->name
);
1068 memcpy(saddr
.sa_data
, tmp_mac
, ETH_ALEN
);
1069 saddr
.sa_family
= old_active
->dev
->type
;
1071 rv
= dev_set_mac_address(old_active
->dev
, &saddr
);
1074 ": %s: Error %d setting MAC of slave %s\n",
1075 bond
->dev
->name
, -rv
, new_active
->dev
->name
);
1077 read_lock(&bond
->lock
);
1078 write_lock_bh(&bond
->curr_slave_lock
);
1082 ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1083 bond
->dev
->name
, bond
->params
.fail_over_mac
);
1089 static bool bond_should_change_active(struct bonding
*bond
)
1091 struct slave
*prim
= bond
->primary_slave
;
1092 struct slave
*curr
= bond
->curr_active_slave
;
1094 if (!prim
|| !curr
|| curr
->link
!= BOND_LINK_UP
)
1096 if (bond
->force_primary
) {
1097 bond
->force_primary
= false;
1100 if (bond
->params
.primary_reselect
== BOND_PRI_RESELECT_BETTER
&&
1101 (prim
->speed
< curr
->speed
||
1102 (prim
->speed
== curr
->speed
&& prim
->duplex
<= curr
->duplex
)))
1104 if (bond
->params
.primary_reselect
== BOND_PRI_RESELECT_FAILURE
)
1110 * find_best_interface - select the best available slave to be the active one
1111 * @bond: our bonding struct
1113 * Warning: Caller must hold curr_slave_lock for writing.
1115 static struct slave
*bond_find_best_slave(struct bonding
*bond
)
1117 struct slave
*new_active
, *old_active
;
1118 struct slave
*bestslave
= NULL
;
1119 int mintime
= bond
->params
.updelay
;
1122 new_active
= bond
->curr_active_slave
;
1124 if (!new_active
) { /* there were no active slaves left */
1125 if (bond
->slave_cnt
> 0) /* found one slave */
1126 new_active
= bond
->first_slave
;
1128 return NULL
; /* still no slave, return NULL */
1131 if ((bond
->primary_slave
) &&
1132 bond
->primary_slave
->link
== BOND_LINK_UP
&&
1133 bond_should_change_active(bond
)) {
1134 new_active
= bond
->primary_slave
;
1137 /* remember where to stop iterating over the slaves */
1138 old_active
= new_active
;
1140 bond_for_each_slave_from(bond
, new_active
, i
, old_active
) {
1141 if (new_active
->link
== BOND_LINK_UP
) {
1143 } else if (new_active
->link
== BOND_LINK_BACK
&&
1144 IS_UP(new_active
->dev
)) {
1145 /* link up, but waiting for stabilization */
1146 if (new_active
->delay
< mintime
) {
1147 mintime
= new_active
->delay
;
1148 bestslave
= new_active
;
1157 * change_active_interface - change the active slave into the specified one
1158 * @bond: our bonding struct
1159 * @new: the new slave to make the active one
1161 * Set the new slave to the bond's settings and unset them on the old
1162 * curr_active_slave.
1163 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1165 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1166 * because it is apparently the best available slave we have, even though its
1167 * updelay hasn't timed out yet.
1169 * If new_active is not NULL, caller must hold bond->lock for read and
1170 * curr_slave_lock for write_bh.
1172 void bond_change_active_slave(struct bonding
*bond
, struct slave
*new_active
)
1174 struct slave
*old_active
= bond
->curr_active_slave
;
1176 if (old_active
== new_active
)
1180 new_active
->jiffies
= jiffies
;
1182 if (new_active
->link
== BOND_LINK_BACK
) {
1183 if (USES_PRIMARY(bond
->params
.mode
)) {
1185 ": %s: making interface %s the new "
1186 "active one %d ms earlier.\n",
1187 bond
->dev
->name
, new_active
->dev
->name
,
1188 (bond
->params
.updelay
- new_active
->delay
) * bond
->params
.miimon
);
1191 new_active
->delay
= 0;
1192 new_active
->link
= BOND_LINK_UP
;
1194 if (bond
->params
.mode
== BOND_MODE_8023AD
)
1195 bond_3ad_handle_link_change(new_active
, BOND_LINK_UP
);
1197 if (bond_is_lb(bond
))
1198 bond_alb_handle_link_change(bond
, new_active
, BOND_LINK_UP
);
1200 if (USES_PRIMARY(bond
->params
.mode
)) {
1202 ": %s: making interface %s the new "
1204 bond
->dev
->name
, new_active
->dev
->name
);
1209 if (USES_PRIMARY(bond
->params
.mode
))
1210 bond_mc_swap(bond
, new_active
, old_active
);
1212 if (bond_is_lb(bond
)) {
1213 bond_alb_handle_active_change(bond
, new_active
);
1215 bond_set_slave_inactive_flags(old_active
);
1217 bond_set_slave_active_flags(new_active
);
1219 bond
->curr_active_slave
= new_active
;
1222 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
) {
1224 bond_set_slave_inactive_flags(old_active
);
1227 bond_set_slave_active_flags(new_active
);
1229 if (bond
->params
.fail_over_mac
)
1230 bond_do_fail_over_mac(bond
, new_active
,
1233 bond
->send_grat_arp
= bond
->params
.num_grat_arp
;
1234 bond_send_gratuitous_arp(bond
);
1236 bond
->send_unsol_na
= bond
->params
.num_unsol_na
;
1237 bond_send_unsolicited_na(bond
);
1239 write_unlock_bh(&bond
->curr_slave_lock
);
1240 read_unlock(&bond
->lock
);
1242 netdev_bonding_change(bond
->dev
, NETDEV_BONDING_FAILOVER
);
1244 read_lock(&bond
->lock
);
1245 write_lock_bh(&bond
->curr_slave_lock
);
1251 * bond_select_active_slave - select a new active slave, if needed
1252 * @bond: our bonding struct
1254 * This functions should be called when one of the following occurs:
1255 * - The old curr_active_slave has been released or lost its link.
1256 * - The primary_slave has got its link back.
1257 * - A slave has got its link back and there's no old curr_active_slave.
1259 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1261 void bond_select_active_slave(struct bonding
*bond
)
1263 struct slave
*best_slave
;
1266 best_slave
= bond_find_best_slave(bond
);
1267 if (best_slave
!= bond
->curr_active_slave
) {
1268 bond_change_active_slave(bond
, best_slave
);
1269 rv
= bond_set_carrier(bond
);
1273 if (netif_carrier_ok(bond
->dev
)) {
1275 ": %s: first active interface up!\n",
1278 pr_info(DRV_NAME
": %s: "
1279 "now running without any active interface !\n",
1285 /*--------------------------- slave list handling ---------------------------*/
1288 * This function attaches the slave to the end of list.
1290 * bond->lock held for writing by caller.
1292 static void bond_attach_slave(struct bonding
*bond
, struct slave
*new_slave
)
1294 if (bond
->first_slave
== NULL
) { /* attaching the first slave */
1295 new_slave
->next
= new_slave
;
1296 new_slave
->prev
= new_slave
;
1297 bond
->first_slave
= new_slave
;
1299 new_slave
->next
= bond
->first_slave
;
1300 new_slave
->prev
= bond
->first_slave
->prev
;
1301 new_slave
->next
->prev
= new_slave
;
1302 new_slave
->prev
->next
= new_slave
;
1309 * This function detaches the slave from the list.
1310 * WARNING: no check is made to verify if the slave effectively
1311 * belongs to <bond>.
1312 * Nothing is freed on return, structures are just unchained.
1313 * If any slave pointer in bond was pointing to <slave>,
1314 * it should be changed by the calling function.
1316 * bond->lock held for writing by caller.
1318 static void bond_detach_slave(struct bonding
*bond
, struct slave
*slave
)
1321 slave
->next
->prev
= slave
->prev
;
1324 slave
->prev
->next
= slave
->next
;
1326 if (bond
->first_slave
== slave
) { /* slave is the first slave */
1327 if (bond
->slave_cnt
> 1) { /* there are more slave */
1328 bond
->first_slave
= slave
->next
;
1330 bond
->first_slave
= NULL
; /* slave was the last one */
1339 /*---------------------------------- IOCTL ----------------------------------*/
1341 static int bond_sethwaddr(struct net_device
*bond_dev
,
1342 struct net_device
*slave_dev
)
1344 pr_debug("bond_dev=%p\n", bond_dev
);
1345 pr_debug("slave_dev=%p\n", slave_dev
);
1346 pr_debug("slave_dev->addr_len=%d\n", slave_dev
->addr_len
);
1347 memcpy(bond_dev
->dev_addr
, slave_dev
->dev_addr
, slave_dev
->addr_len
);
1351 #define BOND_VLAN_FEATURES \
1352 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1353 NETIF_F_HW_VLAN_FILTER)
1356 * Compute the common dev->feature set available to all slaves. Some
1357 * feature bits are managed elsewhere, so preserve those feature bits
1358 * on the master device.
1360 static int bond_compute_features(struct bonding
*bond
)
1362 struct slave
*slave
;
1363 struct net_device
*bond_dev
= bond
->dev
;
1364 unsigned long features
= bond_dev
->features
;
1365 unsigned long vlan_features
= 0;
1366 unsigned short max_hard_header_len
= max((u16
)ETH_HLEN
,
1367 bond_dev
->hard_header_len
);
1370 features
&= ~(NETIF_F_ALL_CSUM
| BOND_VLAN_FEATURES
);
1371 features
|= NETIF_F_GSO_MASK
| NETIF_F_NO_CSUM
;
1373 if (!bond
->first_slave
)
1376 features
&= ~NETIF_F_ONE_FOR_ALL
;
1378 vlan_features
= bond
->first_slave
->dev
->vlan_features
;
1379 bond_for_each_slave(bond
, slave
, i
) {
1380 features
= netdev_increment_features(features
,
1381 slave
->dev
->features
,
1382 NETIF_F_ONE_FOR_ALL
);
1383 vlan_features
= netdev_increment_features(vlan_features
,
1384 slave
->dev
->vlan_features
,
1385 NETIF_F_ONE_FOR_ALL
);
1386 if (slave
->dev
->hard_header_len
> max_hard_header_len
)
1387 max_hard_header_len
= slave
->dev
->hard_header_len
;
1391 features
|= (bond_dev
->features
& BOND_VLAN_FEATURES
);
1392 bond_dev
->features
= netdev_fix_features(features
, NULL
);
1393 bond_dev
->vlan_features
= netdev_fix_features(vlan_features
, NULL
);
1394 bond_dev
->hard_header_len
= max_hard_header_len
;
1399 static void bond_setup_by_slave(struct net_device
*bond_dev
,
1400 struct net_device
*slave_dev
)
1402 struct bonding
*bond
= netdev_priv(bond_dev
);
1404 bond_dev
->header_ops
= slave_dev
->header_ops
;
1406 bond_dev
->type
= slave_dev
->type
;
1407 bond_dev
->hard_header_len
= slave_dev
->hard_header_len
;
1408 bond_dev
->addr_len
= slave_dev
->addr_len
;
1410 memcpy(bond_dev
->broadcast
, slave_dev
->broadcast
,
1411 slave_dev
->addr_len
);
1412 bond
->setup_by_slave
= 1;
1415 /* enslave device <slave> to bond device <master> */
1416 int bond_enslave(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1418 struct bonding
*bond
= netdev_priv(bond_dev
);
1419 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
1420 struct slave
*new_slave
= NULL
;
1421 struct dev_mc_list
*dmi
;
1422 struct sockaddr addr
;
1424 int old_features
= bond_dev
->features
;
1427 if (!bond
->params
.use_carrier
&& slave_dev
->ethtool_ops
== NULL
&&
1428 slave_ops
->ndo_do_ioctl
== NULL
) {
1430 ": %s: Warning: no link monitoring support for %s\n",
1431 bond_dev
->name
, slave_dev
->name
);
1434 /* bond must be initialized by bond_open() before enslaving */
1435 if (!(bond_dev
->flags
& IFF_UP
)) {
1437 " %s: master_dev is not up in bond_enslave\n",
1441 /* already enslaved */
1442 if (slave_dev
->flags
& IFF_SLAVE
) {
1443 pr_debug("Error, Device was already enslaved\n");
1447 /* vlan challenged mutual exclusion */
1448 /* no need to lock since we're protected by rtnl_lock */
1449 if (slave_dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
1450 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev
->name
);
1451 if (!list_empty(&bond
->vlan_list
)) {
1453 ": %s: Error: cannot enslave VLAN "
1454 "challenged slave %s on VLAN enabled "
1455 "bond %s\n", bond_dev
->name
, slave_dev
->name
,
1460 ": %s: Warning: enslaved VLAN challenged "
1461 "slave %s. Adding VLANs will be blocked as "
1462 "long as %s is part of bond %s\n",
1463 bond_dev
->name
, slave_dev
->name
, slave_dev
->name
,
1465 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1468 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev
->name
);
1469 if (bond
->slave_cnt
== 0) {
1470 /* First slave, and it is not VLAN challenged,
1471 * so remove the block of adding VLANs over the bond.
1473 bond_dev
->features
&= ~NETIF_F_VLAN_CHALLENGED
;
1478 * Old ifenslave binaries are no longer supported. These can
1479 * be identified with moderate accuracy by the state of the slave:
1480 * the current ifenslave will set the interface down prior to
1481 * enslaving it; the old ifenslave will not.
1483 if ((slave_dev
->flags
& IFF_UP
)) {
1484 pr_err(DRV_NAME
": %s is up. "
1485 "This may be due to an out of date ifenslave.\n",
1488 goto err_undo_flags
;
1491 /* set bonding device ether type by slave - bonding netdevices are
1492 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1493 * there is a need to override some of the type dependent attribs/funcs.
1495 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1496 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1498 if (bond
->slave_cnt
== 0) {
1499 if (bond_dev
->type
!= slave_dev
->type
) {
1500 pr_debug("%s: change device type from %d to %d\n",
1501 bond_dev
->name
, bond_dev
->type
, slave_dev
->type
);
1503 netdev_bonding_change(bond_dev
, NETDEV_BONDING_OLDTYPE
);
1505 if (slave_dev
->type
!= ARPHRD_ETHER
)
1506 bond_setup_by_slave(bond_dev
, slave_dev
);
1508 ether_setup(bond_dev
);
1510 netdev_bonding_change(bond_dev
, NETDEV_BONDING_NEWTYPE
);
1512 } else if (bond_dev
->type
!= slave_dev
->type
) {
1513 pr_err(DRV_NAME
": %s ether type (%d) is different "
1514 "from other slaves (%d), can not enslave it.\n",
1516 slave_dev
->type
, bond_dev
->type
);
1518 goto err_undo_flags
;
1521 if (slave_ops
->ndo_set_mac_address
== NULL
) {
1522 if (bond
->slave_cnt
== 0) {
1524 ": %s: Warning: The first slave device "
1525 "specified does not support setting the MAC "
1526 "address. Setting fail_over_mac to active.",
1528 bond
->params
.fail_over_mac
= BOND_FOM_ACTIVE
;
1529 } else if (bond
->params
.fail_over_mac
!= BOND_FOM_ACTIVE
) {
1531 ": %s: Error: The slave device specified "
1532 "does not support setting the MAC address, "
1533 "but fail_over_mac is not set to active.\n"
1536 goto err_undo_flags
;
1540 new_slave
= kzalloc(sizeof(struct slave
), GFP_KERNEL
);
1543 goto err_undo_flags
;
1546 /* save slave's original flags before calling
1547 * netdev_set_master and dev_open
1549 new_slave
->original_flags
= slave_dev
->flags
;
1552 * Save slave's original ("permanent") mac address for modes
1553 * that need it, and for restoring it upon release, and then
1554 * set it to the master's address
1556 memcpy(new_slave
->perm_hwaddr
, slave_dev
->dev_addr
, ETH_ALEN
);
1558 if (!bond
->params
.fail_over_mac
) {
1560 * Set slave to master's mac address. The application already
1561 * set the master's mac address to that of the first slave
1563 memcpy(addr
.sa_data
, bond_dev
->dev_addr
, bond_dev
->addr_len
);
1564 addr
.sa_family
= slave_dev
->type
;
1565 res
= dev_set_mac_address(slave_dev
, &addr
);
1567 pr_debug("Error %d calling set_mac_address\n", res
);
1572 res
= netdev_set_master(slave_dev
, bond_dev
);
1574 pr_debug("Error %d calling netdev_set_master\n", res
);
1575 goto err_restore_mac
;
1577 /* open the slave since the application closed it */
1578 res
= dev_open(slave_dev
);
1580 pr_debug("Opening slave %s failed\n", slave_dev
->name
);
1581 goto err_unset_master
;
1584 new_slave
->dev
= slave_dev
;
1585 slave_dev
->priv_flags
|= IFF_BONDING
;
1587 if (bond_is_lb(bond
)) {
1588 /* bond_alb_init_slave() must be called before all other stages since
1589 * it might fail and we do not want to have to undo everything
1591 res
= bond_alb_init_slave(bond
, new_slave
);
1596 /* If the mode USES_PRIMARY, then the new slave gets the
1597 * master's promisc (and mc) settings only if it becomes the
1598 * curr_active_slave, and that is taken care of later when calling
1599 * bond_change_active()
1601 if (!USES_PRIMARY(bond
->params
.mode
)) {
1602 /* set promiscuity level to new slave */
1603 if (bond_dev
->flags
& IFF_PROMISC
) {
1604 res
= dev_set_promiscuity(slave_dev
, 1);
1609 /* set allmulti level to new slave */
1610 if (bond_dev
->flags
& IFF_ALLMULTI
) {
1611 res
= dev_set_allmulti(slave_dev
, 1);
1616 netif_addr_lock_bh(bond_dev
);
1617 /* upload master's mc_list to new slave */
1618 for (dmi
= bond_dev
->mc_list
; dmi
; dmi
= dmi
->next
)
1619 dev_mc_add(slave_dev
, dmi
->dmi_addr
,
1620 dmi
->dmi_addrlen
, 0);
1621 netif_addr_unlock_bh(bond_dev
);
1624 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1625 /* add lacpdu mc addr to mc list */
1626 u8 lacpdu_multicast
[ETH_ALEN
] = MULTICAST_LACPDU_ADDR
;
1628 dev_mc_add(slave_dev
, lacpdu_multicast
, ETH_ALEN
, 0);
1631 bond_add_vlans_on_slave(bond
, slave_dev
);
1633 write_lock_bh(&bond
->lock
);
1635 bond_attach_slave(bond
, new_slave
);
1637 new_slave
->delay
= 0;
1638 new_slave
->link_failure_count
= 0;
1640 bond_compute_features(bond
);
1642 write_unlock_bh(&bond
->lock
);
1644 read_lock(&bond
->lock
);
1646 new_slave
->last_arp_rx
= jiffies
;
1648 if (bond
->params
.miimon
&& !bond
->params
.use_carrier
) {
1649 link_reporting
= bond_check_dev_link(bond
, slave_dev
, 1);
1651 if ((link_reporting
== -1) && !bond
->params
.arp_interval
) {
1653 * miimon is set but a bonded network driver
1654 * does not support ETHTOOL/MII and
1655 * arp_interval is not set. Note: if
1656 * use_carrier is enabled, we will never go
1657 * here (because netif_carrier is always
1658 * supported); thus, we don't need to change
1659 * the messages for netif_carrier.
1662 ": %s: Warning: MII and ETHTOOL support not "
1663 "available for interface %s, and "
1664 "arp_interval/arp_ip_target module parameters "
1665 "not specified, thus bonding will not detect "
1666 "link failures! see bonding.txt for details.\n",
1667 bond_dev
->name
, slave_dev
->name
);
1668 } else if (link_reporting
== -1) {
1669 /* unable get link status using mii/ethtool */
1671 ": %s: Warning: can't get link status from "
1672 "interface %s; the network driver associated "
1673 "with this interface does not support MII or "
1674 "ETHTOOL link status reporting, thus miimon "
1675 "has no effect on this interface.\n",
1676 bond_dev
->name
, slave_dev
->name
);
1680 /* check for initial state */
1681 if (!bond
->params
.miimon
||
1682 (bond_check_dev_link(bond
, slave_dev
, 0) == BMSR_LSTATUS
)) {
1683 if (bond
->params
.updelay
) {
1684 pr_debug("Initial state of slave_dev is "
1685 "BOND_LINK_BACK\n");
1686 new_slave
->link
= BOND_LINK_BACK
;
1687 new_slave
->delay
= bond
->params
.updelay
;
1689 pr_debug("Initial state of slave_dev is "
1691 new_slave
->link
= BOND_LINK_UP
;
1693 new_slave
->jiffies
= jiffies
;
1695 pr_debug("Initial state of slave_dev is "
1696 "BOND_LINK_DOWN\n");
1697 new_slave
->link
= BOND_LINK_DOWN
;
1700 if (bond_update_speed_duplex(new_slave
) &&
1701 (new_slave
->link
!= BOND_LINK_DOWN
)) {
1703 ": %s: Warning: failed to get speed and duplex from %s, "
1704 "assumed to be 100Mb/sec and Full.\n",
1705 bond_dev
->name
, new_slave
->dev
->name
);
1707 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1709 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1710 "support in base driver for proper aggregator "
1711 "selection.\n", bond_dev
->name
);
1715 if (USES_PRIMARY(bond
->params
.mode
) && bond
->params
.primary
[0]) {
1716 /* if there is a primary slave, remember it */
1717 if (strcmp(bond
->params
.primary
, new_slave
->dev
->name
) == 0) {
1718 bond
->primary_slave
= new_slave
;
1719 bond
->force_primary
= true;
1723 write_lock_bh(&bond
->curr_slave_lock
);
1725 switch (bond
->params
.mode
) {
1726 case BOND_MODE_ACTIVEBACKUP
:
1727 bond_set_slave_inactive_flags(new_slave
);
1728 bond_select_active_slave(bond
);
1730 case BOND_MODE_8023AD
:
1731 /* in 802.3ad mode, the internal mechanism
1732 * will activate the slaves in the selected
1735 bond_set_slave_inactive_flags(new_slave
);
1736 /* if this is the first slave */
1737 if (bond
->slave_cnt
== 1) {
1738 SLAVE_AD_INFO(new_slave
).id
= 1;
1739 /* Initialize AD with the number of times that the AD timer is called in 1 second
1740 * can be called only after the mac address of the bond is set
1742 bond_3ad_initialize(bond
, 1000/AD_TIMER_INTERVAL
,
1743 bond
->params
.lacp_fast
);
1745 SLAVE_AD_INFO(new_slave
).id
=
1746 SLAVE_AD_INFO(new_slave
->prev
).id
+ 1;
1749 bond_3ad_bind_slave(new_slave
);
1753 new_slave
->state
= BOND_STATE_ACTIVE
;
1754 bond_set_slave_inactive_flags(new_slave
);
1755 bond_select_active_slave(bond
);
1758 pr_debug("This slave is always active in trunk mode\n");
1760 /* always active in trunk mode */
1761 new_slave
->state
= BOND_STATE_ACTIVE
;
1763 /* In trunking mode there is little meaning to curr_active_slave
1764 * anyway (it holds no special properties of the bond device),
1765 * so we can change it without calling change_active_interface()
1767 if (!bond
->curr_active_slave
)
1768 bond
->curr_active_slave
= new_slave
;
1771 } /* switch(bond_mode) */
1773 write_unlock_bh(&bond
->curr_slave_lock
);
1775 bond_set_carrier(bond
);
1777 read_unlock(&bond
->lock
);
1779 res
= bond_create_slave_symlinks(bond_dev
, slave_dev
);
1784 ": %s: enslaving %s as a%s interface with a%s link.\n",
1785 bond_dev
->name
, slave_dev
->name
,
1786 new_slave
->state
== BOND_STATE_ACTIVE
? "n active" : " backup",
1787 new_slave
->link
!= BOND_LINK_DOWN
? "n up" : " down");
1789 /* enslave is successful */
1792 /* Undo stages on error */
1794 dev_close(slave_dev
);
1797 netdev_set_master(slave_dev
, NULL
);
1800 if (!bond
->params
.fail_over_mac
) {
1801 /* XXX TODO - fom follow mode needs to change master's
1802 * MAC if this slave's MAC is in use by the bond, or at
1803 * least print a warning.
1805 memcpy(addr
.sa_data
, new_slave
->perm_hwaddr
, ETH_ALEN
);
1806 addr
.sa_family
= slave_dev
->type
;
1807 dev_set_mac_address(slave_dev
, &addr
);
1814 bond_dev
->features
= old_features
;
1820 * Try to release the slave device <slave> from the bond device <master>
1821 * It is legal to access curr_active_slave without a lock because all the function
1824 * The rules for slave state should be:
1825 * for Active/Backup:
1826 * Active stays on all backups go down
1827 * for Bonded connections:
1828 * The first up interface should be left on and all others downed.
1830 int bond_release(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1832 struct bonding
*bond
= netdev_priv(bond_dev
);
1833 struct slave
*slave
, *oldcurrent
;
1834 struct sockaddr addr
;
1836 /* slave is not a slave or master is not master of this slave */
1837 if (!(slave_dev
->flags
& IFF_SLAVE
) ||
1838 (slave_dev
->master
!= bond_dev
)) {
1840 ": %s: Error: cannot release %s.\n",
1841 bond_dev
->name
, slave_dev
->name
);
1845 write_lock_bh(&bond
->lock
);
1847 slave
= bond_get_slave_by_dev(bond
, slave_dev
);
1849 /* not a slave of this bond */
1851 ": %s: %s not enslaved\n",
1852 bond_dev
->name
, slave_dev
->name
);
1853 write_unlock_bh(&bond
->lock
);
1857 if (!bond
->params
.fail_over_mac
) {
1858 if (!compare_ether_addr(bond_dev
->dev_addr
, slave
->perm_hwaddr
)
1859 && bond
->slave_cnt
> 1)
1861 ": %s: Warning: the permanent HWaddr of %s - "
1862 "%pM - is still in use by %s. "
1863 "Set the HWaddr of %s to a different address "
1864 "to avoid conflicts.\n",
1865 bond_dev
->name
, slave_dev
->name
,
1867 bond_dev
->name
, slave_dev
->name
);
1870 /* Inform AD package of unbinding of slave. */
1871 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1872 /* must be called before the slave is
1873 * detached from the list
1875 bond_3ad_unbind_slave(slave
);
1879 ": %s: releasing %s interface %s\n",
1881 (slave
->state
== BOND_STATE_ACTIVE
)
1882 ? "active" : "backup",
1885 oldcurrent
= bond
->curr_active_slave
;
1887 bond
->current_arp_slave
= NULL
;
1889 /* release the slave from its bond */
1890 bond_detach_slave(bond
, slave
);
1892 bond_compute_features(bond
);
1894 if (bond
->primary_slave
== slave
)
1895 bond
->primary_slave
= NULL
;
1897 if (oldcurrent
== slave
)
1898 bond_change_active_slave(bond
, NULL
);
1900 if (bond_is_lb(bond
)) {
1901 /* Must be called only after the slave has been
1902 * detached from the list and the curr_active_slave
1903 * has been cleared (if our_slave == old_current),
1904 * but before a new active slave is selected.
1906 write_unlock_bh(&bond
->lock
);
1907 bond_alb_deinit_slave(bond
, slave
);
1908 write_lock_bh(&bond
->lock
);
1911 if (oldcurrent
== slave
) {
1913 * Note that we hold RTNL over this sequence, so there
1914 * is no concern that another slave add/remove event
1917 write_unlock_bh(&bond
->lock
);
1918 read_lock(&bond
->lock
);
1919 write_lock_bh(&bond
->curr_slave_lock
);
1921 bond_select_active_slave(bond
);
1923 write_unlock_bh(&bond
->curr_slave_lock
);
1924 read_unlock(&bond
->lock
);
1925 write_lock_bh(&bond
->lock
);
1928 if (bond
->slave_cnt
== 0) {
1929 bond_set_carrier(bond
);
1931 /* if the last slave was removed, zero the mac address
1932 * of the master so it will be set by the application
1933 * to the mac address of the first slave
1935 memset(bond_dev
->dev_addr
, 0, bond_dev
->addr_len
);
1937 if (list_empty(&bond
->vlan_list
)) {
1938 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1941 ": %s: Warning: clearing HW address of %s while it "
1942 "still has VLANs.\n",
1943 bond_dev
->name
, bond_dev
->name
);
1945 ": %s: When re-adding slaves, make sure the bond's "
1946 "HW address matches its VLANs'.\n",
1949 } else if ((bond_dev
->features
& NETIF_F_VLAN_CHALLENGED
) &&
1950 !bond_has_challenged_slaves(bond
)) {
1952 ": %s: last VLAN challenged slave %s "
1953 "left bond %s. VLAN blocking is removed\n",
1954 bond_dev
->name
, slave_dev
->name
, bond_dev
->name
);
1955 bond_dev
->features
&= ~NETIF_F_VLAN_CHALLENGED
;
1958 write_unlock_bh(&bond
->lock
);
1960 /* must do this from outside any spinlocks */
1961 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
1963 bond_del_vlans_from_slave(bond
, slave_dev
);
1965 /* If the mode USES_PRIMARY, then we should only remove its
1966 * promisc and mc settings if it was the curr_active_slave, but that was
1967 * already taken care of above when we detached the slave
1969 if (!USES_PRIMARY(bond
->params
.mode
)) {
1970 /* unset promiscuity level from slave */
1971 if (bond_dev
->flags
& IFF_PROMISC
)
1972 dev_set_promiscuity(slave_dev
, -1);
1974 /* unset allmulti level from slave */
1975 if (bond_dev
->flags
& IFF_ALLMULTI
)
1976 dev_set_allmulti(slave_dev
, -1);
1978 /* flush master's mc_list from slave */
1979 netif_addr_lock_bh(bond_dev
);
1980 bond_mc_list_flush(bond_dev
, slave_dev
);
1981 netif_addr_unlock_bh(bond_dev
);
1984 netdev_set_master(slave_dev
, NULL
);
1986 /* close slave before restoring its mac address */
1987 dev_close(slave_dev
);
1989 if (bond
->params
.fail_over_mac
!= BOND_FOM_ACTIVE
) {
1990 /* restore original ("permanent") mac address */
1991 memcpy(addr
.sa_data
, slave
->perm_hwaddr
, ETH_ALEN
);
1992 addr
.sa_family
= slave_dev
->type
;
1993 dev_set_mac_address(slave_dev
, &addr
);
1996 slave_dev
->priv_flags
&= ~(IFF_MASTER_8023AD
| IFF_MASTER_ALB
|
1997 IFF_SLAVE_INACTIVE
| IFF_BONDING
|
2002 return 0; /* deletion OK */
2006 * First release a slave and than destroy the bond if no more slaves are left.
2007 * Must be under rtnl_lock when this function is called.
2009 int bond_release_and_destroy(struct net_device
*bond_dev
,
2010 struct net_device
*slave_dev
)
2012 struct bonding
*bond
= netdev_priv(bond_dev
);
2015 ret
= bond_release(bond_dev
, slave_dev
);
2016 if ((ret
== 0) && (bond
->slave_cnt
== 0)) {
2017 pr_info(DRV_NAME
": %s: destroying bond %s.\n",
2018 bond_dev
->name
, bond_dev
->name
);
2019 unregister_netdevice(bond_dev
);
2025 * This function releases all slaves.
2027 static int bond_release_all(struct net_device
*bond_dev
)
2029 struct bonding
*bond
= netdev_priv(bond_dev
);
2030 struct slave
*slave
;
2031 struct net_device
*slave_dev
;
2032 struct sockaddr addr
;
2034 write_lock_bh(&bond
->lock
);
2036 netif_carrier_off(bond_dev
);
2038 if (bond
->slave_cnt
== 0)
2041 bond
->current_arp_slave
= NULL
;
2042 bond
->primary_slave
= NULL
;
2043 bond_change_active_slave(bond
, NULL
);
2045 while ((slave
= bond
->first_slave
) != NULL
) {
2046 /* Inform AD package of unbinding of slave
2047 * before slave is detached from the list.
2049 if (bond
->params
.mode
== BOND_MODE_8023AD
)
2050 bond_3ad_unbind_slave(slave
);
2052 slave_dev
= slave
->dev
;
2053 bond_detach_slave(bond
, slave
);
2055 /* now that the slave is detached, unlock and perform
2056 * all the undo steps that should not be called from
2059 write_unlock_bh(&bond
->lock
);
2061 if (bond_is_lb(bond
)) {
2062 /* must be called only after the slave
2063 * has been detached from the list
2065 bond_alb_deinit_slave(bond
, slave
);
2068 bond_compute_features(bond
);
2070 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
2071 bond_del_vlans_from_slave(bond
, slave_dev
);
2073 /* If the mode USES_PRIMARY, then we should only remove its
2074 * promisc and mc settings if it was the curr_active_slave, but that was
2075 * already taken care of above when we detached the slave
2077 if (!USES_PRIMARY(bond
->params
.mode
)) {
2078 /* unset promiscuity level from slave */
2079 if (bond_dev
->flags
& IFF_PROMISC
)
2080 dev_set_promiscuity(slave_dev
, -1);
2082 /* unset allmulti level from slave */
2083 if (bond_dev
->flags
& IFF_ALLMULTI
)
2084 dev_set_allmulti(slave_dev
, -1);
2086 /* flush master's mc_list from slave */
2087 netif_addr_lock_bh(bond_dev
);
2088 bond_mc_list_flush(bond_dev
, slave_dev
);
2089 netif_addr_unlock_bh(bond_dev
);
2092 netdev_set_master(slave_dev
, NULL
);
2094 /* close slave before restoring its mac address */
2095 dev_close(slave_dev
);
2097 if (!bond
->params
.fail_over_mac
) {
2098 /* restore original ("permanent") mac address*/
2099 memcpy(addr
.sa_data
, slave
->perm_hwaddr
, ETH_ALEN
);
2100 addr
.sa_family
= slave_dev
->type
;
2101 dev_set_mac_address(slave_dev
, &addr
);
2104 slave_dev
->priv_flags
&= ~(IFF_MASTER_8023AD
| IFF_MASTER_ALB
|
2105 IFF_SLAVE_INACTIVE
);
2109 /* re-acquire the lock before getting the next slave */
2110 write_lock_bh(&bond
->lock
);
2113 /* zero the mac address of the master so it will be
2114 * set by the application to the mac address of the
2117 memset(bond_dev
->dev_addr
, 0, bond_dev
->addr_len
);
2119 if (list_empty(&bond
->vlan_list
))
2120 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
2123 ": %s: Warning: clearing HW address of %s while it "
2124 "still has VLANs.\n",
2125 bond_dev
->name
, bond_dev
->name
);
2127 ": %s: When re-adding slaves, make sure the bond's "
2128 "HW address matches its VLANs'.\n",
2133 ": %s: released all slaves\n",
2137 write_unlock_bh(&bond
->lock
);
2143 * This function changes the active slave to slave <slave_dev>.
2144 * It returns -EINVAL in the following cases.
2145 * - <slave_dev> is not found in the list.
2146 * - There is not active slave now.
2147 * - <slave_dev> is already active.
2148 * - The link state of <slave_dev> is not BOND_LINK_UP.
2149 * - <slave_dev> is not running.
2150 * In these cases, this function does nothing.
2151 * In the other cases, current_slave pointer is changed and 0 is returned.
2153 static int bond_ioctl_change_active(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
2155 struct bonding
*bond
= netdev_priv(bond_dev
);
2156 struct slave
*old_active
= NULL
;
2157 struct slave
*new_active
= NULL
;
2160 if (!USES_PRIMARY(bond
->params
.mode
))
2163 /* Verify that master_dev is indeed the master of slave_dev */
2164 if (!(slave_dev
->flags
& IFF_SLAVE
) || (slave_dev
->master
!= bond_dev
))
2167 read_lock(&bond
->lock
);
2169 read_lock(&bond
->curr_slave_lock
);
2170 old_active
= bond
->curr_active_slave
;
2171 read_unlock(&bond
->curr_slave_lock
);
2173 new_active
= bond_get_slave_by_dev(bond
, slave_dev
);
2176 * Changing to the current active: do nothing; return success.
2178 if (new_active
&& (new_active
== old_active
)) {
2179 read_unlock(&bond
->lock
);
2185 (new_active
->link
== BOND_LINK_UP
) &&
2186 IS_UP(new_active
->dev
)) {
2187 write_lock_bh(&bond
->curr_slave_lock
);
2188 bond_change_active_slave(bond
, new_active
);
2189 write_unlock_bh(&bond
->curr_slave_lock
);
2193 read_unlock(&bond
->lock
);
2198 static int bond_info_query(struct net_device
*bond_dev
, struct ifbond
*info
)
2200 struct bonding
*bond
= netdev_priv(bond_dev
);
2202 info
->bond_mode
= bond
->params
.mode
;
2203 info
->miimon
= bond
->params
.miimon
;
2205 read_lock(&bond
->lock
);
2206 info
->num_slaves
= bond
->slave_cnt
;
2207 read_unlock(&bond
->lock
);
2212 static int bond_slave_info_query(struct net_device
*bond_dev
, struct ifslave
*info
)
2214 struct bonding
*bond
= netdev_priv(bond_dev
);
2215 struct slave
*slave
;
2216 int i
, res
= -ENODEV
;
2218 read_lock(&bond
->lock
);
2220 bond_for_each_slave(bond
, slave
, i
) {
2221 if (i
== (int)info
->slave_id
) {
2223 strcpy(info
->slave_name
, slave
->dev
->name
);
2224 info
->link
= slave
->link
;
2225 info
->state
= slave
->state
;
2226 info
->link_failure_count
= slave
->link_failure_count
;
2231 read_unlock(&bond
->lock
);
2236 /*-------------------------------- Monitoring -------------------------------*/
2239 static int bond_miimon_inspect(struct bonding
*bond
)
2241 struct slave
*slave
;
2242 int i
, link_state
, commit
= 0;
2243 bool ignore_updelay
;
2245 ignore_updelay
= !bond
->curr_active_slave
? true : false;
2247 bond_for_each_slave(bond
, slave
, i
) {
2248 slave
->new_link
= BOND_LINK_NOCHANGE
;
2250 link_state
= bond_check_dev_link(bond
, slave
->dev
, 0);
2252 switch (slave
->link
) {
2257 slave
->link
= BOND_LINK_FAIL
;
2258 slave
->delay
= bond
->params
.downdelay
;
2261 ": %s: link status down for %s"
2262 "interface %s, disabling it in %d ms.\n",
2264 (bond
->params
.mode
==
2265 BOND_MODE_ACTIVEBACKUP
) ?
2266 ((slave
->state
== BOND_STATE_ACTIVE
) ?
2267 "active " : "backup ") : "",
2269 bond
->params
.downdelay
* bond
->params
.miimon
);
2272 case BOND_LINK_FAIL
:
2275 * recovered before downdelay expired
2277 slave
->link
= BOND_LINK_UP
;
2278 slave
->jiffies
= jiffies
;
2280 ": %s: link status up again after %d "
2281 "ms for interface %s.\n",
2283 (bond
->params
.downdelay
- slave
->delay
) *
2284 bond
->params
.miimon
,
2289 if (slave
->delay
<= 0) {
2290 slave
->new_link
= BOND_LINK_DOWN
;
2298 case BOND_LINK_DOWN
:
2302 slave
->link
= BOND_LINK_BACK
;
2303 slave
->delay
= bond
->params
.updelay
;
2307 ": %s: link status up for "
2308 "interface %s, enabling it in %d ms.\n",
2309 bond
->dev
->name
, slave
->dev
->name
,
2310 ignore_updelay
? 0 :
2311 bond
->params
.updelay
*
2312 bond
->params
.miimon
);
2315 case BOND_LINK_BACK
:
2317 slave
->link
= BOND_LINK_DOWN
;
2319 ": %s: link status down again after %d "
2320 "ms for interface %s.\n",
2322 (bond
->params
.updelay
- slave
->delay
) *
2323 bond
->params
.miimon
,
2332 if (slave
->delay
<= 0) {
2333 slave
->new_link
= BOND_LINK_UP
;
2335 ignore_updelay
= false;
2347 static void bond_miimon_commit(struct bonding
*bond
)
2349 struct slave
*slave
;
2352 bond_for_each_slave(bond
, slave
, i
) {
2353 switch (slave
->new_link
) {
2354 case BOND_LINK_NOCHANGE
:
2358 slave
->link
= BOND_LINK_UP
;
2359 slave
->jiffies
= jiffies
;
2361 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
2362 /* prevent it from being the active one */
2363 slave
->state
= BOND_STATE_BACKUP
;
2364 } else if (bond
->params
.mode
!= BOND_MODE_ACTIVEBACKUP
) {
2365 /* make it immediately active */
2366 slave
->state
= BOND_STATE_ACTIVE
;
2367 } else if (slave
!= bond
->primary_slave
) {
2368 /* prevent it from being the active one */
2369 slave
->state
= BOND_STATE_BACKUP
;
2373 ": %s: link status definitely "
2374 "up for interface %s.\n",
2375 bond
->dev
->name
, slave
->dev
->name
);
2377 /* notify ad that the link status has changed */
2378 if (bond
->params
.mode
== BOND_MODE_8023AD
)
2379 bond_3ad_handle_link_change(slave
, BOND_LINK_UP
);
2381 if (bond_is_lb(bond
))
2382 bond_alb_handle_link_change(bond
, slave
,
2385 if (!bond
->curr_active_slave
||
2386 (slave
== bond
->primary_slave
))
2391 case BOND_LINK_DOWN
:
2392 if (slave
->link_failure_count
< UINT_MAX
)
2393 slave
->link_failure_count
++;
2395 slave
->link
= BOND_LINK_DOWN
;
2397 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
||
2398 bond
->params
.mode
== BOND_MODE_8023AD
)
2399 bond_set_slave_inactive_flags(slave
);
2402 ": %s: link status definitely down for "
2403 "interface %s, disabling it\n",
2404 bond
->dev
->name
, slave
->dev
->name
);
2406 if (bond
->params
.mode
== BOND_MODE_8023AD
)
2407 bond_3ad_handle_link_change(slave
,
2410 if (bond_is_lb(bond
))
2411 bond_alb_handle_link_change(bond
, slave
,
2414 if (slave
== bond
->curr_active_slave
)
2421 ": %s: invalid new link %d on slave %s\n",
2422 bond
->dev
->name
, slave
->new_link
,
2424 slave
->new_link
= BOND_LINK_NOCHANGE
;
2431 write_lock_bh(&bond
->curr_slave_lock
);
2432 bond_select_active_slave(bond
);
2433 write_unlock_bh(&bond
->curr_slave_lock
);
2436 bond_set_carrier(bond
);
2442 * Really a wrapper that splits the mii monitor into two phases: an
2443 * inspection, then (if inspection indicates something needs to be done)
2444 * an acquisition of appropriate locks followed by a commit phase to
2445 * implement whatever link state changes are indicated.
2447 void bond_mii_monitor(struct work_struct
*work
)
2449 struct bonding
*bond
= container_of(work
, struct bonding
,
2452 read_lock(&bond
->lock
);
2453 if (bond
->kill_timers
)
2456 if (bond
->slave_cnt
== 0)
2459 if (bond
->send_grat_arp
) {
2460 read_lock(&bond
->curr_slave_lock
);
2461 bond_send_gratuitous_arp(bond
);
2462 read_unlock(&bond
->curr_slave_lock
);
2465 if (bond
->send_unsol_na
) {
2466 read_lock(&bond
->curr_slave_lock
);
2467 bond_send_unsolicited_na(bond
);
2468 read_unlock(&bond
->curr_slave_lock
);
2471 if (bond_miimon_inspect(bond
)) {
2472 read_unlock(&bond
->lock
);
2474 read_lock(&bond
->lock
);
2476 bond_miimon_commit(bond
);
2478 read_unlock(&bond
->lock
);
2479 rtnl_unlock(); /* might sleep, hold no other locks */
2480 read_lock(&bond
->lock
);
2484 if (bond
->params
.miimon
)
2485 queue_delayed_work(bond
->wq
, &bond
->mii_work
,
2486 msecs_to_jiffies(bond
->params
.miimon
));
2488 read_unlock(&bond
->lock
);
2491 static __be32
bond_glean_dev_ip(struct net_device
*dev
)
2493 struct in_device
*idev
;
2494 struct in_ifaddr
*ifa
;
2501 idev
= __in_dev_get_rcu(dev
);
2505 ifa
= idev
->ifa_list
;
2509 addr
= ifa
->ifa_local
;
2515 static int bond_has_this_ip(struct bonding
*bond
, __be32 ip
)
2517 struct vlan_entry
*vlan
;
2519 if (ip
== bond
->master_ip
)
2522 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
2523 if (ip
== vlan
->vlan_ip
)
2531 * We go to the (large) trouble of VLAN tagging ARP frames because
2532 * switches in VLAN mode (especially if ports are configured as
2533 * "native" to a VLAN) might not pass non-tagged frames.
2535 static void bond_arp_send(struct net_device
*slave_dev
, int arp_op
, __be32 dest_ip
, __be32 src_ip
, unsigned short vlan_id
)
2537 struct sk_buff
*skb
;
2539 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op
,
2540 slave_dev
->name
, dest_ip
, src_ip
, vlan_id
);
2542 skb
= arp_create(arp_op
, ETH_P_ARP
, dest_ip
, slave_dev
, src_ip
,
2543 NULL
, slave_dev
->dev_addr
, NULL
);
2546 pr_err(DRV_NAME
": ARP packet allocation failed\n");
2550 skb
= vlan_put_tag(skb
, vlan_id
);
2552 pr_err(DRV_NAME
": failed to insert VLAN tag\n");
2560 static void bond_arp_send_all(struct bonding
*bond
, struct slave
*slave
)
2563 __be32
*targets
= bond
->params
.arp_targets
;
2564 struct vlan_entry
*vlan
;
2565 struct net_device
*vlan_dev
;
2569 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
); i
++) {
2572 pr_debug("basa: target %x\n", targets
[i
]);
2573 if (list_empty(&bond
->vlan_list
)) {
2574 pr_debug("basa: empty vlan: arp_send\n");
2575 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2576 bond
->master_ip
, 0);
2581 * If VLANs are configured, we do a route lookup to
2582 * determine which VLAN interface would be used, so we
2583 * can tag the ARP with the proper VLAN tag.
2585 memset(&fl
, 0, sizeof(fl
));
2586 fl
.fl4_dst
= targets
[i
];
2587 fl
.fl4_tos
= RTO_ONLINK
;
2589 rv
= ip_route_output_key(&init_net
, &rt
, &fl
);
2591 if (net_ratelimit()) {
2593 ": %s: no route to arp_ip_target %pI4\n",
2594 bond
->dev
->name
, &fl
.fl4_dst
);
2600 * This target is not on a VLAN
2602 if (rt
->u
.dst
.dev
== bond
->dev
) {
2604 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2605 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2606 bond
->master_ip
, 0);
2611 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
2612 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
2613 if (vlan_dev
== rt
->u
.dst
.dev
) {
2614 vlan_id
= vlan
->vlan_id
;
2615 pr_debug("basa: vlan match on %s %d\n",
2616 vlan_dev
->name
, vlan_id
);
2623 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2624 vlan
->vlan_ip
, vlan_id
);
2628 if (net_ratelimit()) {
2630 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2631 bond
->dev
->name
, &fl
.fl4_dst
,
2632 rt
->u
.dst
.dev
? rt
->u
.dst
.dev
->name
: "NULL");
2639 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2640 * for each VLAN above us.
2642 * Caller must hold curr_slave_lock for read or better
2644 static void bond_send_gratuitous_arp(struct bonding
*bond
)
2646 struct slave
*slave
= bond
->curr_active_slave
;
2647 struct vlan_entry
*vlan
;
2648 struct net_device
*vlan_dev
;
2650 pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond
->dev
->name
,
2651 slave
? slave
->dev
->name
: "NULL");
2653 if (!slave
|| !bond
->send_grat_arp
||
2654 test_bit(__LINK_STATE_LINKWATCH_PENDING
, &slave
->dev
->state
))
2657 bond
->send_grat_arp
--;
2659 if (bond
->master_ip
) {
2660 bond_arp_send(slave
->dev
, ARPOP_REPLY
, bond
->master_ip
,
2661 bond
->master_ip
, 0);
2664 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
2665 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
2666 if (vlan
->vlan_ip
) {
2667 bond_arp_send(slave
->dev
, ARPOP_REPLY
, vlan
->vlan_ip
,
2668 vlan
->vlan_ip
, vlan
->vlan_id
);
2673 static void bond_validate_arp(struct bonding
*bond
, struct slave
*slave
, __be32 sip
, __be32 tip
)
2676 __be32
*targets
= bond
->params
.arp_targets
;
2678 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
) && targets
[i
]; i
++) {
2679 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2680 &sip
, &tip
, i
, &targets
[i
], bond_has_this_ip(bond
, tip
));
2681 if (sip
== targets
[i
]) {
2682 if (bond_has_this_ip(bond
, tip
))
2683 slave
->last_arp_rx
= jiffies
;
2689 static int bond_arp_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
2692 struct slave
*slave
;
2693 struct bonding
*bond
;
2694 unsigned char *arp_ptr
;
2697 if (dev_net(dev
) != &init_net
)
2700 if (!(dev
->priv_flags
& IFF_BONDING
) || !(dev
->flags
& IFF_MASTER
))
2703 bond
= netdev_priv(dev
);
2704 read_lock(&bond
->lock
);
2706 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2707 bond
->dev
->name
, skb
->dev
? skb
->dev
->name
: "NULL",
2708 orig_dev
? orig_dev
->name
: "NULL");
2710 slave
= bond_get_slave_by_dev(bond
, orig_dev
);
2711 if (!slave
|| !slave_do_arp_validate(bond
, slave
))
2714 if (!pskb_may_pull(skb
, arp_hdr_len(dev
)))
2718 if (arp
->ar_hln
!= dev
->addr_len
||
2719 skb
->pkt_type
== PACKET_OTHERHOST
||
2720 skb
->pkt_type
== PACKET_LOOPBACK
||
2721 arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
2722 arp
->ar_pro
!= htons(ETH_P_IP
) ||
2726 arp_ptr
= (unsigned char *)(arp
+ 1);
2727 arp_ptr
+= dev
->addr_len
;
2728 memcpy(&sip
, arp_ptr
, 4);
2729 arp_ptr
+= 4 + dev
->addr_len
;
2730 memcpy(&tip
, arp_ptr
, 4);
2732 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2733 bond
->dev
->name
, slave
->dev
->name
, slave
->state
,
2734 bond
->params
.arp_validate
, slave_do_arp_validate(bond
, slave
),
2738 * Backup slaves won't see the ARP reply, but do come through
2739 * here for each ARP probe (so we swap the sip/tip to validate
2740 * the probe). In a "redundant switch, common router" type of
2741 * configuration, the ARP probe will (hopefully) travel from
2742 * the active, through one switch, the router, then the other
2743 * switch before reaching the backup.
2745 if (slave
->state
== BOND_STATE_ACTIVE
)
2746 bond_validate_arp(bond
, slave
, sip
, tip
);
2748 bond_validate_arp(bond
, slave
, tip
, sip
);
2751 read_unlock(&bond
->lock
);
2754 return NET_RX_SUCCESS
;
2758 * this function is called regularly to monitor each slave's link
2759 * ensuring that traffic is being sent and received when arp monitoring
2760 * is used in load-balancing mode. if the adapter has been dormant, then an
2761 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2762 * arp monitoring in active backup mode.
2764 void bond_loadbalance_arp_mon(struct work_struct
*work
)
2766 struct bonding
*bond
= container_of(work
, struct bonding
,
2768 struct slave
*slave
, *oldcurrent
;
2769 int do_failover
= 0;
2773 read_lock(&bond
->lock
);
2775 delta_in_ticks
= msecs_to_jiffies(bond
->params
.arp_interval
);
2777 if (bond
->kill_timers
)
2780 if (bond
->slave_cnt
== 0)
2783 read_lock(&bond
->curr_slave_lock
);
2784 oldcurrent
= bond
->curr_active_slave
;
2785 read_unlock(&bond
->curr_slave_lock
);
2787 /* see if any of the previous devices are up now (i.e. they have
2788 * xmt and rcv traffic). the curr_active_slave does not come into
2789 * the picture unless it is null. also, slave->jiffies is not needed
2790 * here because we send an arp on each slave and give a slave as
2791 * long as it needs to get the tx/rx within the delta.
2792 * TODO: what about up/down delay in arp mode? it wasn't here before
2795 bond_for_each_slave(bond
, slave
, i
) {
2796 if (slave
->link
!= BOND_LINK_UP
) {
2797 if (time_before_eq(jiffies
, dev_trans_start(slave
->dev
) + delta_in_ticks
) &&
2798 time_before_eq(jiffies
, slave
->dev
->last_rx
+ delta_in_ticks
)) {
2800 slave
->link
= BOND_LINK_UP
;
2801 slave
->state
= BOND_STATE_ACTIVE
;
2803 /* primary_slave has no meaning in round-robin
2804 * mode. the window of a slave being up and
2805 * curr_active_slave being null after enslaving
2810 ": %s: link status definitely "
2811 "up for interface %s, ",
2817 ": %s: interface %s is now up\n",
2823 /* slave->link == BOND_LINK_UP */
2825 /* not all switches will respond to an arp request
2826 * when the source ip is 0, so don't take the link down
2827 * if we don't know our ip yet
2829 if (time_after_eq(jiffies
, dev_trans_start(slave
->dev
) + 2*delta_in_ticks
) ||
2830 (time_after_eq(jiffies
, slave
->dev
->last_rx
+ 2*delta_in_ticks
))) {
2832 slave
->link
= BOND_LINK_DOWN
;
2833 slave
->state
= BOND_STATE_BACKUP
;
2835 if (slave
->link_failure_count
< UINT_MAX
)
2836 slave
->link_failure_count
++;
2839 ": %s: interface %s is now down.\n",
2843 if (slave
== oldcurrent
)
2848 /* note: if switch is in round-robin mode, all links
2849 * must tx arp to ensure all links rx an arp - otherwise
2850 * links may oscillate or not come up at all; if switch is
2851 * in something like xor mode, there is nothing we can
2852 * do - all replies will be rx'ed on same link causing slaves
2853 * to be unstable during low/no traffic periods
2855 if (IS_UP(slave
->dev
))
2856 bond_arp_send_all(bond
, slave
);
2860 write_lock_bh(&bond
->curr_slave_lock
);
2862 bond_select_active_slave(bond
);
2864 write_unlock_bh(&bond
->curr_slave_lock
);
2868 if (bond
->params
.arp_interval
)
2869 queue_delayed_work(bond
->wq
, &bond
->arp_work
, delta_in_ticks
);
2871 read_unlock(&bond
->lock
);
2875 * Called to inspect slaves for active-backup mode ARP monitor link state
2876 * changes. Sets new_link in slaves to specify what action should take
2877 * place for the slave. Returns 0 if no changes are found, >0 if changes
2878 * to link states must be committed.
2880 * Called with bond->lock held for read.
2882 static int bond_ab_arp_inspect(struct bonding
*bond
, int delta_in_ticks
)
2884 struct slave
*slave
;
2887 bond_for_each_slave(bond
, slave
, i
) {
2888 slave
->new_link
= BOND_LINK_NOCHANGE
;
2890 if (slave
->link
!= BOND_LINK_UP
) {
2891 if (time_before_eq(jiffies
, slave_last_rx(bond
, slave
) +
2893 slave
->new_link
= BOND_LINK_UP
;
2901 * Give slaves 2*delta after being enslaved or made
2902 * active. This avoids bouncing, as the last receive
2903 * times need a full ARP monitor cycle to be updated.
2905 if (!time_after_eq(jiffies
, slave
->jiffies
+
2906 2 * delta_in_ticks
))
2910 * Backup slave is down if:
2911 * - No current_arp_slave AND
2912 * - more than 3*delta since last receive AND
2913 * - the bond has an IP address
2915 * Note: a non-null current_arp_slave indicates
2916 * the curr_active_slave went down and we are
2917 * searching for a new one; under this condition
2918 * we only take the curr_active_slave down - this
2919 * gives each slave a chance to tx/rx traffic
2920 * before being taken out
2922 if (slave
->state
== BOND_STATE_BACKUP
&&
2923 !bond
->current_arp_slave
&&
2924 time_after(jiffies
, slave_last_rx(bond
, slave
) +
2925 3 * delta_in_ticks
)) {
2926 slave
->new_link
= BOND_LINK_DOWN
;
2931 * Active slave is down if:
2932 * - more than 2*delta since transmitting OR
2933 * - (more than 2*delta since receive AND
2934 * the bond has an IP address)
2936 if ((slave
->state
== BOND_STATE_ACTIVE
) &&
2937 (time_after_eq(jiffies
, dev_trans_start(slave
->dev
) +
2938 2 * delta_in_ticks
) ||
2939 (time_after_eq(jiffies
, slave_last_rx(bond
, slave
)
2940 + 2 * delta_in_ticks
)))) {
2941 slave
->new_link
= BOND_LINK_DOWN
;
2950 * Called to commit link state changes noted by inspection step of
2951 * active-backup mode ARP monitor.
2953 * Called with RTNL and bond->lock for read.
2955 static void bond_ab_arp_commit(struct bonding
*bond
, int delta_in_ticks
)
2957 struct slave
*slave
;
2960 bond_for_each_slave(bond
, slave
, i
) {
2961 switch (slave
->new_link
) {
2962 case BOND_LINK_NOCHANGE
:
2966 if ((!bond
->curr_active_slave
&&
2967 time_before_eq(jiffies
,
2968 dev_trans_start(slave
->dev
) +
2970 bond
->curr_active_slave
!= slave
) {
2971 slave
->link
= BOND_LINK_UP
;
2972 bond
->current_arp_slave
= NULL
;
2975 ": %s: link status definitely "
2976 "up for interface %s.\n",
2977 bond
->dev
->name
, slave
->dev
->name
);
2979 if (!bond
->curr_active_slave
||
2980 (slave
== bond
->primary_slave
))
2987 case BOND_LINK_DOWN
:
2988 if (slave
->link_failure_count
< UINT_MAX
)
2989 slave
->link_failure_count
++;
2991 slave
->link
= BOND_LINK_DOWN
;
2992 bond_set_slave_inactive_flags(slave
);
2995 ": %s: link status definitely down for "
2996 "interface %s, disabling it\n",
2997 bond
->dev
->name
, slave
->dev
->name
);
2999 if (slave
== bond
->curr_active_slave
) {
3000 bond
->current_arp_slave
= NULL
;
3008 ": %s: impossible: new_link %d on slave %s\n",
3009 bond
->dev
->name
, slave
->new_link
,
3016 write_lock_bh(&bond
->curr_slave_lock
);
3017 bond_select_active_slave(bond
);
3018 write_unlock_bh(&bond
->curr_slave_lock
);
3021 bond_set_carrier(bond
);
3025 * Send ARP probes for active-backup mode ARP monitor.
3027 * Called with bond->lock held for read.
3029 static void bond_ab_arp_probe(struct bonding
*bond
)
3031 struct slave
*slave
;
3034 read_lock(&bond
->curr_slave_lock
);
3036 if (bond
->current_arp_slave
&& bond
->curr_active_slave
)
3037 pr_info(DRV_NAME
"PROBE: c_arp %s && cas %s BAD\n",
3038 bond
->current_arp_slave
->dev
->name
,
3039 bond
->curr_active_slave
->dev
->name
);
3041 if (bond
->curr_active_slave
) {
3042 bond_arp_send_all(bond
, bond
->curr_active_slave
);
3043 read_unlock(&bond
->curr_slave_lock
);
3047 read_unlock(&bond
->curr_slave_lock
);
3049 /* if we don't have a curr_active_slave, search for the next available
3050 * backup slave from the current_arp_slave and make it the candidate
3051 * for becoming the curr_active_slave
3054 if (!bond
->current_arp_slave
) {
3055 bond
->current_arp_slave
= bond
->first_slave
;
3056 if (!bond
->current_arp_slave
)
3060 bond_set_slave_inactive_flags(bond
->current_arp_slave
);
3062 /* search for next candidate */
3063 bond_for_each_slave_from(bond
, slave
, i
, bond
->current_arp_slave
->next
) {
3064 if (IS_UP(slave
->dev
)) {
3065 slave
->link
= BOND_LINK_BACK
;
3066 bond_set_slave_active_flags(slave
);
3067 bond_arp_send_all(bond
, slave
);
3068 slave
->jiffies
= jiffies
;
3069 bond
->current_arp_slave
= slave
;
3073 /* if the link state is up at this point, we
3074 * mark it down - this can happen if we have
3075 * simultaneous link failures and
3076 * reselect_active_interface doesn't make this
3077 * one the current slave so it is still marked
3078 * up when it is actually down
3080 if (slave
->link
== BOND_LINK_UP
) {
3081 slave
->link
= BOND_LINK_DOWN
;
3082 if (slave
->link_failure_count
< UINT_MAX
)
3083 slave
->link_failure_count
++;
3085 bond_set_slave_inactive_flags(slave
);
3088 ": %s: backup interface %s is now down.\n",
3089 bond
->dev
->name
, slave
->dev
->name
);
3094 void bond_activebackup_arp_mon(struct work_struct
*work
)
3096 struct bonding
*bond
= container_of(work
, struct bonding
,
3100 read_lock(&bond
->lock
);
3102 if (bond
->kill_timers
)
3105 delta_in_ticks
= msecs_to_jiffies(bond
->params
.arp_interval
);
3107 if (bond
->slave_cnt
== 0)
3110 if (bond
->send_grat_arp
) {
3111 read_lock(&bond
->curr_slave_lock
);
3112 bond_send_gratuitous_arp(bond
);
3113 read_unlock(&bond
->curr_slave_lock
);
3116 if (bond
->send_unsol_na
) {
3117 read_lock(&bond
->curr_slave_lock
);
3118 bond_send_unsolicited_na(bond
);
3119 read_unlock(&bond
->curr_slave_lock
);
3122 if (bond_ab_arp_inspect(bond
, delta_in_ticks
)) {
3123 read_unlock(&bond
->lock
);
3125 read_lock(&bond
->lock
);
3127 bond_ab_arp_commit(bond
, delta_in_ticks
);
3129 read_unlock(&bond
->lock
);
3131 read_lock(&bond
->lock
);
3134 bond_ab_arp_probe(bond
);
3137 if (bond
->params
.arp_interval
)
3138 queue_delayed_work(bond
->wq
, &bond
->arp_work
, delta_in_ticks
);
3140 read_unlock(&bond
->lock
);
3143 /*------------------------------ proc/seq_file-------------------------------*/
3145 #ifdef CONFIG_PROC_FS
3147 static void *bond_info_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3148 __acquires(&dev_base_lock
)
3149 __acquires(&bond
->lock
)
3151 struct bonding
*bond
= seq
->private;
3153 struct slave
*slave
;
3156 /* make sure the bond won't be taken away */
3157 read_lock(&dev_base_lock
);
3158 read_lock(&bond
->lock
);
3161 return SEQ_START_TOKEN
;
3163 bond_for_each_slave(bond
, slave
, i
) {
3171 static void *bond_info_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3173 struct bonding
*bond
= seq
->private;
3174 struct slave
*slave
= v
;
3177 if (v
== SEQ_START_TOKEN
)
3178 return bond
->first_slave
;
3180 slave
= slave
->next
;
3182 return (slave
== bond
->first_slave
) ? NULL
: slave
;
3185 static void bond_info_seq_stop(struct seq_file
*seq
, void *v
)
3186 __releases(&bond
->lock
)
3187 __releases(&dev_base_lock
)
3189 struct bonding
*bond
= seq
->private;
3191 read_unlock(&bond
->lock
);
3192 read_unlock(&dev_base_lock
);
3195 static void bond_info_show_master(struct seq_file
*seq
)
3197 struct bonding
*bond
= seq
->private;
3201 read_lock(&bond
->curr_slave_lock
);
3202 curr
= bond
->curr_active_slave
;
3203 read_unlock(&bond
->curr_slave_lock
);
3205 seq_printf(seq
, "Bonding Mode: %s",
3206 bond_mode_name(bond
->params
.mode
));
3208 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
&&
3209 bond
->params
.fail_over_mac
)
3210 seq_printf(seq
, " (fail_over_mac %s)",
3211 fail_over_mac_tbl
[bond
->params
.fail_over_mac
].modename
);
3213 seq_printf(seq
, "\n");
3215 if (bond
->params
.mode
== BOND_MODE_XOR
||
3216 bond
->params
.mode
== BOND_MODE_8023AD
) {
3217 seq_printf(seq
, "Transmit Hash Policy: %s (%d)\n",
3218 xmit_hashtype_tbl
[bond
->params
.xmit_policy
].modename
,
3219 bond
->params
.xmit_policy
);
3222 if (USES_PRIMARY(bond
->params
.mode
)) {
3223 seq_printf(seq
, "Primary Slave: %s",
3224 (bond
->primary_slave
) ?
3225 bond
->primary_slave
->dev
->name
: "None");
3226 if (bond
->primary_slave
)
3227 seq_printf(seq
, " (primary_reselect %s)",
3228 pri_reselect_tbl
[bond
->params
.primary_reselect
].modename
);
3230 seq_printf(seq
, "\nCurrently Active Slave: %s\n",
3231 (curr
) ? curr
->dev
->name
: "None");
3234 seq_printf(seq
, "MII Status: %s\n", netif_carrier_ok(bond
->dev
) ?
3236 seq_printf(seq
, "MII Polling Interval (ms): %d\n", bond
->params
.miimon
);
3237 seq_printf(seq
, "Up Delay (ms): %d\n",
3238 bond
->params
.updelay
* bond
->params
.miimon
);
3239 seq_printf(seq
, "Down Delay (ms): %d\n",
3240 bond
->params
.downdelay
* bond
->params
.miimon
);
3243 /* ARP information */
3244 if (bond
->params
.arp_interval
> 0) {
3246 seq_printf(seq
, "ARP Polling Interval (ms): %d\n",
3247 bond
->params
.arp_interval
);
3249 seq_printf(seq
, "ARP IP target/s (n.n.n.n form):");
3251 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
); i
++) {
3252 if (!bond
->params
.arp_targets
[i
])
3255 seq_printf(seq
, ",");
3256 seq_printf(seq
, " %pI4", &bond
->params
.arp_targets
[i
]);
3259 seq_printf(seq
, "\n");
3262 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3263 struct ad_info ad_info
;
3265 seq_puts(seq
, "\n802.3ad info\n");
3266 seq_printf(seq
, "LACP rate: %s\n",
3267 (bond
->params
.lacp_fast
) ? "fast" : "slow");
3268 seq_printf(seq
, "Aggregator selection policy (ad_select): %s\n",
3269 ad_select_tbl
[bond
->params
.ad_select
].modename
);
3271 if (bond_3ad_get_active_agg_info(bond
, &ad_info
)) {
3272 seq_printf(seq
, "bond %s has no active aggregator\n",
3275 seq_printf(seq
, "Active Aggregator Info:\n");
3277 seq_printf(seq
, "\tAggregator ID: %d\n",
3278 ad_info
.aggregator_id
);
3279 seq_printf(seq
, "\tNumber of ports: %d\n",
3281 seq_printf(seq
, "\tActor Key: %d\n",
3283 seq_printf(seq
, "\tPartner Key: %d\n",
3284 ad_info
.partner_key
);
3285 seq_printf(seq
, "\tPartner Mac Address: %pM\n",
3286 ad_info
.partner_system
);
3291 static void bond_info_show_slave(struct seq_file
*seq
,
3292 const struct slave
*slave
)
3294 struct bonding
*bond
= seq
->private;
3296 seq_printf(seq
, "\nSlave Interface: %s\n", slave
->dev
->name
);
3297 seq_printf(seq
, "MII Status: %s\n",
3298 (slave
->link
== BOND_LINK_UP
) ? "up" : "down");
3299 seq_printf(seq
, "Link Failure Count: %u\n",
3300 slave
->link_failure_count
);
3302 seq_printf(seq
, "Permanent HW addr: %pM\n", slave
->perm_hwaddr
);
3304 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3305 const struct aggregator
*agg
3306 = SLAVE_AD_INFO(slave
).port
.aggregator
;
3309 seq_printf(seq
, "Aggregator ID: %d\n",
3310 agg
->aggregator_identifier
);
3312 seq_puts(seq
, "Aggregator ID: N/A\n");
3316 static int bond_info_seq_show(struct seq_file
*seq
, void *v
)
3318 if (v
== SEQ_START_TOKEN
) {
3319 seq_printf(seq
, "%s\n", version
);
3320 bond_info_show_master(seq
);
3322 bond_info_show_slave(seq
, v
);
3327 static const struct seq_operations bond_info_seq_ops
= {
3328 .start
= bond_info_seq_start
,
3329 .next
= bond_info_seq_next
,
3330 .stop
= bond_info_seq_stop
,
3331 .show
= bond_info_seq_show
,
3334 static int bond_info_open(struct inode
*inode
, struct file
*file
)
3336 struct seq_file
*seq
;
3337 struct proc_dir_entry
*proc
;
3340 res
= seq_open(file
, &bond_info_seq_ops
);
3342 /* recover the pointer buried in proc_dir_entry data */
3343 seq
= file
->private_data
;
3345 seq
->private = proc
->data
;
3351 static const struct file_operations bond_info_fops
= {
3352 .owner
= THIS_MODULE
,
3353 .open
= bond_info_open
,
3355 .llseek
= seq_lseek
,
3356 .release
= seq_release
,
3359 static void bond_create_proc_entry(struct bonding
*bond
)
3361 struct net_device
*bond_dev
= bond
->dev
;
3363 if (bond_proc_dir
) {
3364 bond
->proc_entry
= proc_create_data(bond_dev
->name
,
3365 S_IRUGO
, bond_proc_dir
,
3366 &bond_info_fops
, bond
);
3367 if (bond
->proc_entry
== NULL
)
3369 ": Warning: Cannot create /proc/net/%s/%s\n",
3370 DRV_NAME
, bond_dev
->name
);
3372 memcpy(bond
->proc_file_name
, bond_dev
->name
, IFNAMSIZ
);
3376 static void bond_remove_proc_entry(struct bonding
*bond
)
3378 if (bond_proc_dir
&& bond
->proc_entry
) {
3379 remove_proc_entry(bond
->proc_file_name
, bond_proc_dir
);
3380 memset(bond
->proc_file_name
, 0, IFNAMSIZ
);
3381 bond
->proc_entry
= NULL
;
3385 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3386 * Caller must hold rtnl_lock.
3388 static void bond_create_proc_dir(void)
3390 if (!bond_proc_dir
) {
3391 bond_proc_dir
= proc_mkdir(DRV_NAME
, init_net
.proc_net
);
3394 ": Warning: cannot create /proc/net/%s\n",
3399 /* Destroy the bonding directory under /proc/net, if empty.
3400 * Caller must hold rtnl_lock.
3402 static void bond_destroy_proc_dir(void)
3404 if (bond_proc_dir
) {
3405 remove_proc_entry(DRV_NAME
, init_net
.proc_net
);
3406 bond_proc_dir
= NULL
;
3410 #else /* !CONFIG_PROC_FS */
3412 static void bond_create_proc_entry(struct bonding
*bond
)
3416 static void bond_remove_proc_entry(struct bonding
*bond
)
3420 static void bond_create_proc_dir(void)
3424 static void bond_destroy_proc_dir(void)
3428 #endif /* CONFIG_PROC_FS */
3431 /*-------------------------- netdev event handling --------------------------*/
3434 * Change device name
3436 static int bond_event_changename(struct bonding
*bond
)
3438 bond_remove_proc_entry(bond
);
3439 bond_create_proc_entry(bond
);
3444 static int bond_master_netdev_event(unsigned long event
,
3445 struct net_device
*bond_dev
)
3447 struct bonding
*event_bond
= netdev_priv(bond_dev
);
3450 case NETDEV_CHANGENAME
:
3451 return bond_event_changename(event_bond
);
3459 static int bond_slave_netdev_event(unsigned long event
,
3460 struct net_device
*slave_dev
)
3462 struct net_device
*bond_dev
= slave_dev
->master
;
3463 struct bonding
*bond
= netdev_priv(bond_dev
);
3466 case NETDEV_UNREGISTER
:
3468 if (bond
->setup_by_slave
)
3469 bond_release_and_destroy(bond_dev
, slave_dev
);
3471 bond_release(bond_dev
, slave_dev
);
3475 if (bond
->params
.mode
== BOND_MODE_8023AD
|| bond_is_lb(bond
)) {
3476 struct slave
*slave
;
3478 slave
= bond_get_slave_by_dev(bond
, slave_dev
);
3480 u16 old_speed
= slave
->speed
;
3481 u16 old_duplex
= slave
->duplex
;
3483 bond_update_speed_duplex(slave
);
3485 if (bond_is_lb(bond
))
3488 if (old_speed
!= slave
->speed
)
3489 bond_3ad_adapter_speed_changed(slave
);
3490 if (old_duplex
!= slave
->duplex
)
3491 bond_3ad_adapter_duplex_changed(slave
);
3498 * ... Or is it this?
3501 case NETDEV_CHANGEMTU
:
3503 * TODO: Should slaves be allowed to
3504 * independently alter their MTU? For
3505 * an active-backup bond, slaves need
3506 * not be the same type of device, so
3507 * MTUs may vary. For other modes,
3508 * slaves arguably should have the
3509 * same MTUs. To do this, we'd need to
3510 * take over the slave's change_mtu
3511 * function for the duration of their
3515 case NETDEV_CHANGENAME
:
3517 * TODO: handle changing the primary's name
3520 case NETDEV_FEAT_CHANGE
:
3521 bond_compute_features(bond
);
3531 * bond_netdev_event: handle netdev notifier chain events.
3533 * This function receives events for the netdev chain. The caller (an
3534 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3535 * locks for us to safely manipulate the slave devices (RTNL lock,
3538 static int bond_netdev_event(struct notifier_block
*this,
3539 unsigned long event
, void *ptr
)
3541 struct net_device
*event_dev
= (struct net_device
*)ptr
;
3543 if (dev_net(event_dev
) != &init_net
)
3546 pr_debug("event_dev: %s, event: %lx\n",
3547 (event_dev
? event_dev
->name
: "None"),
3550 if (!(event_dev
->priv_flags
& IFF_BONDING
))
3553 if (event_dev
->flags
& IFF_MASTER
) {
3554 pr_debug("IFF_MASTER\n");
3555 return bond_master_netdev_event(event
, event_dev
);
3558 if (event_dev
->flags
& IFF_SLAVE
) {
3559 pr_debug("IFF_SLAVE\n");
3560 return bond_slave_netdev_event(event
, event_dev
);
3567 * bond_inetaddr_event: handle inetaddr notifier chain events.
3569 * We keep track of device IPs primarily to use as source addresses in
3570 * ARP monitor probes (rather than spewing out broadcasts all the time).
3572 * We track one IP for the main device (if it has one), plus one per VLAN.
3574 static int bond_inetaddr_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3576 struct in_ifaddr
*ifa
= ptr
;
3577 struct net_device
*vlan_dev
, *event_dev
= ifa
->ifa_dev
->dev
;
3578 struct bonding
*bond
;
3579 struct vlan_entry
*vlan
;
3581 if (dev_net(ifa
->ifa_dev
->dev
) != &init_net
)
3584 list_for_each_entry(bond
, &bond_dev_list
, bond_list
) {
3585 if (bond
->dev
== event_dev
) {
3588 bond
->master_ip
= ifa
->ifa_local
;
3591 bond
->master_ip
= bond_glean_dev_ip(bond
->dev
);
3598 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
3599 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
3600 if (vlan_dev
== event_dev
) {
3603 vlan
->vlan_ip
= ifa
->ifa_local
;
3607 bond_glean_dev_ip(vlan_dev
);
3618 static struct notifier_block bond_netdev_notifier
= {
3619 .notifier_call
= bond_netdev_event
,
3622 static struct notifier_block bond_inetaddr_notifier
= {
3623 .notifier_call
= bond_inetaddr_event
,
3626 /*-------------------------- Packet type handling ---------------------------*/
3628 /* register to receive lacpdus on a bond */
3629 static void bond_register_lacpdu(struct bonding
*bond
)
3631 struct packet_type
*pk_type
= &(BOND_AD_INFO(bond
).ad_pkt_type
);
3633 /* initialize packet type */
3634 pk_type
->type
= PKT_TYPE_LACPDU
;
3635 pk_type
->dev
= bond
->dev
;
3636 pk_type
->func
= bond_3ad_lacpdu_recv
;
3638 dev_add_pack(pk_type
);
3641 /* unregister to receive lacpdus on a bond */
3642 static void bond_unregister_lacpdu(struct bonding
*bond
)
3644 dev_remove_pack(&(BOND_AD_INFO(bond
).ad_pkt_type
));
3647 void bond_register_arp(struct bonding
*bond
)
3649 struct packet_type
*pt
= &bond
->arp_mon_pt
;
3654 pt
->type
= htons(ETH_P_ARP
);
3655 pt
->dev
= bond
->dev
;
3656 pt
->func
= bond_arp_rcv
;
3660 void bond_unregister_arp(struct bonding
*bond
)
3662 struct packet_type
*pt
= &bond
->arp_mon_pt
;
3664 dev_remove_pack(pt
);
3668 /*---------------------------- Hashing Policies -----------------------------*/
3671 * Hash for the output device based upon layer 2 and layer 3 data. If
3672 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3674 static int bond_xmit_hash_policy_l23(struct sk_buff
*skb
, int count
)
3676 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3677 struct iphdr
*iph
= ip_hdr(skb
);
3679 if (skb
->protocol
== htons(ETH_P_IP
)) {
3680 return ((ntohl(iph
->saddr
^ iph
->daddr
) & 0xffff) ^
3681 (data
->h_dest
[5] ^ data
->h_source
[5])) % count
;
3684 return (data
->h_dest
[5] ^ data
->h_source
[5]) % count
;
3688 * Hash for the output device based upon layer 3 and layer 4 data. If
3689 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3690 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3692 static int bond_xmit_hash_policy_l34(struct sk_buff
*skb
, int count
)
3694 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3695 struct iphdr
*iph
= ip_hdr(skb
);
3696 __be16
*layer4hdr
= (__be16
*)((u32
*)iph
+ iph
->ihl
);
3699 if (skb
->protocol
== htons(ETH_P_IP
)) {
3700 if (!(iph
->frag_off
& htons(IP_MF
|IP_OFFSET
)) &&
3701 (iph
->protocol
== IPPROTO_TCP
||
3702 iph
->protocol
== IPPROTO_UDP
)) {
3703 layer4_xor
= ntohs((*layer4hdr
^ *(layer4hdr
+ 1)));
3705 return (layer4_xor
^
3706 ((ntohl(iph
->saddr
^ iph
->daddr
)) & 0xffff)) % count
;
3710 return (data
->h_dest
[5] ^ data
->h_source
[5]) % count
;
3714 * Hash for the output device based upon layer 2 data
3716 static int bond_xmit_hash_policy_l2(struct sk_buff
*skb
, int count
)
3718 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3720 return (data
->h_dest
[5] ^ data
->h_source
[5]) % count
;
3723 /*-------------------------- Device entry points ----------------------------*/
3725 static int bond_open(struct net_device
*bond_dev
)
3727 struct bonding
*bond
= netdev_priv(bond_dev
);
3729 bond
->kill_timers
= 0;
3731 if (bond_is_lb(bond
)) {
3732 /* bond_alb_initialize must be called before the timer
3735 if (bond_alb_initialize(bond
, (bond
->params
.mode
== BOND_MODE_ALB
))) {
3736 /* something went wrong - fail the open operation */
3740 INIT_DELAYED_WORK(&bond
->alb_work
, bond_alb_monitor
);
3741 queue_delayed_work(bond
->wq
, &bond
->alb_work
, 0);
3744 if (bond
->params
.miimon
) { /* link check interval, in milliseconds. */
3745 INIT_DELAYED_WORK(&bond
->mii_work
, bond_mii_monitor
);
3746 queue_delayed_work(bond
->wq
, &bond
->mii_work
, 0);
3749 if (bond
->params
.arp_interval
) { /* arp interval, in milliseconds. */
3750 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
)
3751 INIT_DELAYED_WORK(&bond
->arp_work
,
3752 bond_activebackup_arp_mon
);
3754 INIT_DELAYED_WORK(&bond
->arp_work
,
3755 bond_loadbalance_arp_mon
);
3757 queue_delayed_work(bond
->wq
, &bond
->arp_work
, 0);
3758 if (bond
->params
.arp_validate
)
3759 bond_register_arp(bond
);
3762 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3763 INIT_DELAYED_WORK(&bond
->ad_work
, bond_3ad_state_machine_handler
);
3764 queue_delayed_work(bond
->wq
, &bond
->ad_work
, 0);
3765 /* register to receive LACPDUs */
3766 bond_register_lacpdu(bond
);
3767 bond_3ad_initiate_agg_selection(bond
, 1);
3773 static int bond_close(struct net_device
*bond_dev
)
3775 struct bonding
*bond
= netdev_priv(bond_dev
);
3777 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3778 /* Unregister the receive of LACPDUs */
3779 bond_unregister_lacpdu(bond
);
3782 if (bond
->params
.arp_validate
)
3783 bond_unregister_arp(bond
);
3785 write_lock_bh(&bond
->lock
);
3787 bond
->send_grat_arp
= 0;
3788 bond
->send_unsol_na
= 0;
3790 /* signal timers not to re-arm */
3791 bond
->kill_timers
= 1;
3793 write_unlock_bh(&bond
->lock
);
3795 if (bond
->params
.miimon
) { /* link check interval, in milliseconds. */
3796 cancel_delayed_work(&bond
->mii_work
);
3799 if (bond
->params
.arp_interval
) { /* arp interval, in milliseconds. */
3800 cancel_delayed_work(&bond
->arp_work
);
3803 switch (bond
->params
.mode
) {
3804 case BOND_MODE_8023AD
:
3805 cancel_delayed_work(&bond
->ad_work
);
3809 cancel_delayed_work(&bond
->alb_work
);
3816 if (bond_is_lb(bond
)) {
3817 /* Must be called only after all
3818 * slaves have been released
3820 bond_alb_deinitialize(bond
);
3826 static struct net_device_stats
*bond_get_stats(struct net_device
*bond_dev
)
3828 struct bonding
*bond
= netdev_priv(bond_dev
);
3829 struct net_device_stats
*stats
= &bond
->stats
;
3830 struct net_device_stats local_stats
;
3831 struct slave
*slave
;
3834 memset(&local_stats
, 0, sizeof(struct net_device_stats
));
3836 read_lock_bh(&bond
->lock
);
3838 bond_for_each_slave(bond
, slave
, i
) {
3839 const struct net_device_stats
*sstats
= dev_get_stats(slave
->dev
);
3841 local_stats
.rx_packets
+= sstats
->rx_packets
;
3842 local_stats
.rx_bytes
+= sstats
->rx_bytes
;
3843 local_stats
.rx_errors
+= sstats
->rx_errors
;
3844 local_stats
.rx_dropped
+= sstats
->rx_dropped
;
3846 local_stats
.tx_packets
+= sstats
->tx_packets
;
3847 local_stats
.tx_bytes
+= sstats
->tx_bytes
;
3848 local_stats
.tx_errors
+= sstats
->tx_errors
;
3849 local_stats
.tx_dropped
+= sstats
->tx_dropped
;
3851 local_stats
.multicast
+= sstats
->multicast
;
3852 local_stats
.collisions
+= sstats
->collisions
;
3854 local_stats
.rx_length_errors
+= sstats
->rx_length_errors
;
3855 local_stats
.rx_over_errors
+= sstats
->rx_over_errors
;
3856 local_stats
.rx_crc_errors
+= sstats
->rx_crc_errors
;
3857 local_stats
.rx_frame_errors
+= sstats
->rx_frame_errors
;
3858 local_stats
.rx_fifo_errors
+= sstats
->rx_fifo_errors
;
3859 local_stats
.rx_missed_errors
+= sstats
->rx_missed_errors
;
3861 local_stats
.tx_aborted_errors
+= sstats
->tx_aborted_errors
;
3862 local_stats
.tx_carrier_errors
+= sstats
->tx_carrier_errors
;
3863 local_stats
.tx_fifo_errors
+= sstats
->tx_fifo_errors
;
3864 local_stats
.tx_heartbeat_errors
+= sstats
->tx_heartbeat_errors
;
3865 local_stats
.tx_window_errors
+= sstats
->tx_window_errors
;
3868 memcpy(stats
, &local_stats
, sizeof(struct net_device_stats
));
3870 read_unlock_bh(&bond
->lock
);
3875 static int bond_do_ioctl(struct net_device
*bond_dev
, struct ifreq
*ifr
, int cmd
)
3877 struct net_device
*slave_dev
= NULL
;
3878 struct ifbond k_binfo
;
3879 struct ifbond __user
*u_binfo
= NULL
;
3880 struct ifslave k_sinfo
;
3881 struct ifslave __user
*u_sinfo
= NULL
;
3882 struct mii_ioctl_data
*mii
= NULL
;
3885 pr_debug("bond_ioctl: master=%s, cmd=%d\n",
3886 bond_dev
->name
, cmd
);
3898 * We do this again just in case we were called by SIOCGMIIREG
3899 * instead of SIOCGMIIPHY.
3906 if (mii
->reg_num
== 1) {
3907 struct bonding
*bond
= netdev_priv(bond_dev
);
3909 read_lock(&bond
->lock
);
3910 read_lock(&bond
->curr_slave_lock
);
3911 if (netif_carrier_ok(bond
->dev
))
3912 mii
->val_out
= BMSR_LSTATUS
;
3914 read_unlock(&bond
->curr_slave_lock
);
3915 read_unlock(&bond
->lock
);
3919 case BOND_INFO_QUERY_OLD
:
3920 case SIOCBONDINFOQUERY
:
3921 u_binfo
= (struct ifbond __user
*)ifr
->ifr_data
;
3923 if (copy_from_user(&k_binfo
, u_binfo
, sizeof(ifbond
)))
3926 res
= bond_info_query(bond_dev
, &k_binfo
);
3928 copy_to_user(u_binfo
, &k_binfo
, sizeof(ifbond
)))
3932 case BOND_SLAVE_INFO_QUERY_OLD
:
3933 case SIOCBONDSLAVEINFOQUERY
:
3934 u_sinfo
= (struct ifslave __user
*)ifr
->ifr_data
;
3936 if (copy_from_user(&k_sinfo
, u_sinfo
, sizeof(ifslave
)))
3939 res
= bond_slave_info_query(bond_dev
, &k_sinfo
);
3941 copy_to_user(u_sinfo
, &k_sinfo
, sizeof(ifslave
)))
3950 if (!capable(CAP_NET_ADMIN
))
3953 slave_dev
= dev_get_by_name(&init_net
, ifr
->ifr_slave
);
3955 pr_debug("slave_dev=%p: \n", slave_dev
);
3960 pr_debug("slave_dev->name=%s: \n", slave_dev
->name
);
3962 case BOND_ENSLAVE_OLD
:
3963 case SIOCBONDENSLAVE
:
3964 res
= bond_enslave(bond_dev
, slave_dev
);
3966 case BOND_RELEASE_OLD
:
3967 case SIOCBONDRELEASE
:
3968 res
= bond_release(bond_dev
, slave_dev
);
3970 case BOND_SETHWADDR_OLD
:
3971 case SIOCBONDSETHWADDR
:
3972 res
= bond_sethwaddr(bond_dev
, slave_dev
);
3974 case BOND_CHANGE_ACTIVE_OLD
:
3975 case SIOCBONDCHANGEACTIVE
:
3976 res
= bond_ioctl_change_active(bond_dev
, slave_dev
);
3988 static void bond_set_multicast_list(struct net_device
*bond_dev
)
3990 struct bonding
*bond
= netdev_priv(bond_dev
);
3991 struct dev_mc_list
*dmi
;
3994 * Do promisc before checking multicast_mode
3996 if ((bond_dev
->flags
& IFF_PROMISC
) && !(bond
->flags
& IFF_PROMISC
))
3998 * FIXME: Need to handle the error when one of the multi-slaves
4001 bond_set_promiscuity(bond
, 1);
4004 if (!(bond_dev
->flags
& IFF_PROMISC
) && (bond
->flags
& IFF_PROMISC
))
4005 bond_set_promiscuity(bond
, -1);
4008 /* set allmulti flag to slaves */
4009 if ((bond_dev
->flags
& IFF_ALLMULTI
) && !(bond
->flags
& IFF_ALLMULTI
))
4011 * FIXME: Need to handle the error when one of the multi-slaves
4014 bond_set_allmulti(bond
, 1);
4017 if (!(bond_dev
->flags
& IFF_ALLMULTI
) && (bond
->flags
& IFF_ALLMULTI
))
4018 bond_set_allmulti(bond
, -1);
4021 read_lock(&bond
->lock
);
4023 bond
->flags
= bond_dev
->flags
;
4025 /* looking for addresses to add to slaves' mc list */
4026 for (dmi
= bond_dev
->mc_list
; dmi
; dmi
= dmi
->next
) {
4027 if (!bond_mc_list_find_dmi(dmi
, bond
->mc_list
))
4028 bond_mc_add(bond
, dmi
->dmi_addr
, dmi
->dmi_addrlen
);
4031 /* looking for addresses to delete from slaves' list */
4032 for (dmi
= bond
->mc_list
; dmi
; dmi
= dmi
->next
) {
4033 if (!bond_mc_list_find_dmi(dmi
, bond_dev
->mc_list
))
4034 bond_mc_delete(bond
, dmi
->dmi_addr
, dmi
->dmi_addrlen
);
4037 /* save master's multicast list */
4038 bond_mc_list_destroy(bond
);
4039 bond_mc_list_copy(bond_dev
->mc_list
, bond
, GFP_ATOMIC
);
4041 read_unlock(&bond
->lock
);
4044 static int bond_neigh_setup(struct net_device
*dev
, struct neigh_parms
*parms
)
4046 struct bonding
*bond
= netdev_priv(dev
);
4047 struct slave
*slave
= bond
->first_slave
;
4050 const struct net_device_ops
*slave_ops
4051 = slave
->dev
->netdev_ops
;
4052 if (slave_ops
->ndo_neigh_setup
)
4053 return slave_ops
->ndo_neigh_setup(slave
->dev
, parms
);
4059 * Change the MTU of all of a master's slaves to match the master
4061 static int bond_change_mtu(struct net_device
*bond_dev
, int new_mtu
)
4063 struct bonding
*bond
= netdev_priv(bond_dev
);
4064 struct slave
*slave
, *stop_at
;
4068 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond
,
4069 (bond_dev
? bond_dev
->name
: "None"), new_mtu
);
4071 /* Can't hold bond->lock with bh disabled here since
4072 * some base drivers panic. On the other hand we can't
4073 * hold bond->lock without bh disabled because we'll
4074 * deadlock. The only solution is to rely on the fact
4075 * that we're under rtnl_lock here, and the slaves
4076 * list won't change. This doesn't solve the problem
4077 * of setting the slave's MTU while it is
4078 * transmitting, but the assumption is that the base
4079 * driver can handle that.
4081 * TODO: figure out a way to safely iterate the slaves
4082 * list, but without holding a lock around the actual
4083 * call to the base driver.
4086 bond_for_each_slave(bond
, slave
, i
) {
4087 pr_debug("s %p s->p %p c_m %p\n", slave
,
4088 slave
->prev
, slave
->dev
->netdev_ops
->ndo_change_mtu
);
4090 res
= dev_set_mtu(slave
->dev
, new_mtu
);
4093 /* If we failed to set the slave's mtu to the new value
4094 * we must abort the operation even in ACTIVE_BACKUP
4095 * mode, because if we allow the backup slaves to have
4096 * different mtu values than the active slave we'll
4097 * need to change their mtu when doing a failover. That
4098 * means changing their mtu from timer context, which
4099 * is probably not a good idea.
4101 pr_debug("err %d %s\n", res
, slave
->dev
->name
);
4106 bond_dev
->mtu
= new_mtu
;
4111 /* unwind from head to the slave that failed */
4113 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
4116 tmp_res
= dev_set_mtu(slave
->dev
, bond_dev
->mtu
);
4118 pr_debug("unwind err %d dev %s\n", tmp_res
,
4129 * Note that many devices must be down to change the HW address, and
4130 * downing the master releases all slaves. We can make bonds full of
4131 * bonding devices to test this, however.
4133 static int bond_set_mac_address(struct net_device
*bond_dev
, void *addr
)
4135 struct bonding
*bond
= netdev_priv(bond_dev
);
4136 struct sockaddr
*sa
= addr
, tmp_sa
;
4137 struct slave
*slave
, *stop_at
;
4141 if (bond
->params
.mode
== BOND_MODE_ALB
)
4142 return bond_alb_set_mac_address(bond_dev
, addr
);
4145 pr_debug("bond=%p, name=%s\n", bond
, (bond_dev
? bond_dev
->name
: "None"));
4148 * If fail_over_mac is set to active, do nothing and return
4149 * success. Returning an error causes ifenslave to fail.
4151 if (bond
->params
.fail_over_mac
== BOND_FOM_ACTIVE
)
4154 if (!is_valid_ether_addr(sa
->sa_data
))
4155 return -EADDRNOTAVAIL
;
4157 /* Can't hold bond->lock with bh disabled here since
4158 * some base drivers panic. On the other hand we can't
4159 * hold bond->lock without bh disabled because we'll
4160 * deadlock. The only solution is to rely on the fact
4161 * that we're under rtnl_lock here, and the slaves
4162 * list won't change. This doesn't solve the problem
4163 * of setting the slave's hw address while it is
4164 * transmitting, but the assumption is that the base
4165 * driver can handle that.
4167 * TODO: figure out a way to safely iterate the slaves
4168 * list, but without holding a lock around the actual
4169 * call to the base driver.
4172 bond_for_each_slave(bond
, slave
, i
) {
4173 const struct net_device_ops
*slave_ops
= slave
->dev
->netdev_ops
;
4174 pr_debug("slave %p %s\n", slave
, slave
->dev
->name
);
4176 if (slave_ops
->ndo_set_mac_address
== NULL
) {
4178 pr_debug("EOPNOTSUPP %s\n", slave
->dev
->name
);
4182 res
= dev_set_mac_address(slave
->dev
, addr
);
4184 /* TODO: consider downing the slave
4186 * User should expect communications
4187 * breakage anyway until ARP finish
4190 pr_debug("err %d %s\n", res
, slave
->dev
->name
);
4196 memcpy(bond_dev
->dev_addr
, sa
->sa_data
, bond_dev
->addr_len
);
4200 memcpy(tmp_sa
.sa_data
, bond_dev
->dev_addr
, bond_dev
->addr_len
);
4201 tmp_sa
.sa_family
= bond_dev
->type
;
4203 /* unwind from head to the slave that failed */
4205 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
4208 tmp_res
= dev_set_mac_address(slave
->dev
, &tmp_sa
);
4210 pr_debug("unwind err %d dev %s\n", tmp_res
,
4218 static int bond_xmit_roundrobin(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4220 struct bonding
*bond
= netdev_priv(bond_dev
);
4221 struct slave
*slave
, *start_at
;
4222 int i
, slave_no
, res
= 1;
4224 read_lock(&bond
->lock
);
4226 if (!BOND_IS_OK(bond
))
4230 * Concurrent TX may collide on rr_tx_counter; we accept that
4231 * as being rare enough not to justify using an atomic op here
4233 slave_no
= bond
->rr_tx_counter
++ % bond
->slave_cnt
;
4235 bond_for_each_slave(bond
, slave
, i
) {
4242 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4243 if (IS_UP(slave
->dev
) &&
4244 (slave
->link
== BOND_LINK_UP
) &&
4245 (slave
->state
== BOND_STATE_ACTIVE
)) {
4246 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4253 /* no suitable interface, frame not sent */
4256 read_unlock(&bond
->lock
);
4257 return NETDEV_TX_OK
;
4262 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4263 * the bond has a usable interface.
4265 static int bond_xmit_activebackup(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4267 struct bonding
*bond
= netdev_priv(bond_dev
);
4270 read_lock(&bond
->lock
);
4271 read_lock(&bond
->curr_slave_lock
);
4273 if (!BOND_IS_OK(bond
))
4276 if (!bond
->curr_active_slave
)
4279 res
= bond_dev_queue_xmit(bond
, skb
, bond
->curr_active_slave
->dev
);
4283 /* no suitable interface, frame not sent */
4286 read_unlock(&bond
->curr_slave_lock
);
4287 read_unlock(&bond
->lock
);
4288 return NETDEV_TX_OK
;
4292 * In bond_xmit_xor() , we determine the output device by using a pre-
4293 * determined xmit_hash_policy(), If the selected device is not enabled,
4294 * find the next active slave.
4296 static int bond_xmit_xor(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4298 struct bonding
*bond
= netdev_priv(bond_dev
);
4299 struct slave
*slave
, *start_at
;
4304 read_lock(&bond
->lock
);
4306 if (!BOND_IS_OK(bond
))
4309 slave_no
= bond
->xmit_hash_policy(skb
, bond
->slave_cnt
);
4311 bond_for_each_slave(bond
, slave
, i
) {
4319 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4320 if (IS_UP(slave
->dev
) &&
4321 (slave
->link
== BOND_LINK_UP
) &&
4322 (slave
->state
== BOND_STATE_ACTIVE
)) {
4323 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4330 /* no suitable interface, frame not sent */
4333 read_unlock(&bond
->lock
);
4334 return NETDEV_TX_OK
;
4338 * in broadcast mode, we send everything to all usable interfaces.
4340 static int bond_xmit_broadcast(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4342 struct bonding
*bond
= netdev_priv(bond_dev
);
4343 struct slave
*slave
, *start_at
;
4344 struct net_device
*tx_dev
= NULL
;
4348 read_lock(&bond
->lock
);
4350 if (!BOND_IS_OK(bond
))
4353 read_lock(&bond
->curr_slave_lock
);
4354 start_at
= bond
->curr_active_slave
;
4355 read_unlock(&bond
->curr_slave_lock
);
4360 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4361 if (IS_UP(slave
->dev
) &&
4362 (slave
->link
== BOND_LINK_UP
) &&
4363 (slave
->state
== BOND_STATE_ACTIVE
)) {
4365 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
4368 ": %s: Error: bond_xmit_broadcast(): "
4369 "skb_clone() failed\n",
4374 res
= bond_dev_queue_xmit(bond
, skb2
, tx_dev
);
4376 dev_kfree_skb(skb2
);
4380 tx_dev
= slave
->dev
;
4385 res
= bond_dev_queue_xmit(bond
, skb
, tx_dev
);
4389 /* no suitable interface, frame not sent */
4392 /* frame sent to all suitable interfaces */
4393 read_unlock(&bond
->lock
);
4394 return NETDEV_TX_OK
;
4397 /*------------------------- Device initialization ---------------------------*/
4399 static void bond_set_xmit_hash_policy(struct bonding
*bond
)
4401 switch (bond
->params
.xmit_policy
) {
4402 case BOND_XMIT_POLICY_LAYER23
:
4403 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l23
;
4405 case BOND_XMIT_POLICY_LAYER34
:
4406 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l34
;
4408 case BOND_XMIT_POLICY_LAYER2
:
4410 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l2
;
4415 static netdev_tx_t
bond_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
4417 const struct bonding
*bond
= netdev_priv(dev
);
4419 switch (bond
->params
.mode
) {
4420 case BOND_MODE_ROUNDROBIN
:
4421 return bond_xmit_roundrobin(skb
, dev
);
4422 case BOND_MODE_ACTIVEBACKUP
:
4423 return bond_xmit_activebackup(skb
, dev
);
4425 return bond_xmit_xor(skb
, dev
);
4426 case BOND_MODE_BROADCAST
:
4427 return bond_xmit_broadcast(skb
, dev
);
4428 case BOND_MODE_8023AD
:
4429 return bond_3ad_xmit_xor(skb
, dev
);
4432 return bond_alb_xmit(skb
, dev
);
4434 /* Should never happen, mode already checked */
4435 pr_err(DRV_NAME
": %s: Error: Unknown bonding mode %d\n",
4436 dev
->name
, bond
->params
.mode
);
4439 return NETDEV_TX_OK
;
4445 * set bond mode specific net device operations
4447 void bond_set_mode_ops(struct bonding
*bond
, int mode
)
4449 struct net_device
*bond_dev
= bond
->dev
;
4452 case BOND_MODE_ROUNDROBIN
:
4454 case BOND_MODE_ACTIVEBACKUP
:
4457 bond_set_xmit_hash_policy(bond
);
4459 case BOND_MODE_BROADCAST
:
4461 case BOND_MODE_8023AD
:
4462 bond_set_master_3ad_flags(bond
);
4463 bond_set_xmit_hash_policy(bond
);
4466 bond_set_master_alb_flags(bond
);
4471 /* Should never happen, mode already checked */
4473 ": %s: Error: Unknown bonding mode %d\n",
4480 static void bond_ethtool_get_drvinfo(struct net_device
*bond_dev
,
4481 struct ethtool_drvinfo
*drvinfo
)
4483 strncpy(drvinfo
->driver
, DRV_NAME
, 32);
4484 strncpy(drvinfo
->version
, DRV_VERSION
, 32);
4485 snprintf(drvinfo
->fw_version
, 32, "%d", BOND_ABI_VERSION
);
4488 static const struct ethtool_ops bond_ethtool_ops
= {
4489 .get_drvinfo
= bond_ethtool_get_drvinfo
,
4490 .get_link
= ethtool_op_get_link
,
4491 .get_tx_csum
= ethtool_op_get_tx_csum
,
4492 .get_sg
= ethtool_op_get_sg
,
4493 .get_tso
= ethtool_op_get_tso
,
4494 .get_ufo
= ethtool_op_get_ufo
,
4495 .get_flags
= ethtool_op_get_flags
,
4498 static const struct net_device_ops bond_netdev_ops
= {
4499 .ndo_init
= bond_init
,
4500 .ndo_uninit
= bond_uninit
,
4501 .ndo_open
= bond_open
,
4502 .ndo_stop
= bond_close
,
4503 .ndo_start_xmit
= bond_start_xmit
,
4504 .ndo_get_stats
= bond_get_stats
,
4505 .ndo_do_ioctl
= bond_do_ioctl
,
4506 .ndo_set_multicast_list
= bond_set_multicast_list
,
4507 .ndo_change_mtu
= bond_change_mtu
,
4508 .ndo_set_mac_address
= bond_set_mac_address
,
4509 .ndo_neigh_setup
= bond_neigh_setup
,
4510 .ndo_vlan_rx_register
= bond_vlan_rx_register
,
4511 .ndo_vlan_rx_add_vid
= bond_vlan_rx_add_vid
,
4512 .ndo_vlan_rx_kill_vid
= bond_vlan_rx_kill_vid
,
4515 static void bond_setup(struct net_device
*bond_dev
)
4517 struct bonding
*bond
= netdev_priv(bond_dev
);
4519 /* initialize rwlocks */
4520 rwlock_init(&bond
->lock
);
4521 rwlock_init(&bond
->curr_slave_lock
);
4523 bond
->params
= bonding_defaults
;
4525 /* Initialize pointers */
4526 bond
->dev
= bond_dev
;
4527 INIT_LIST_HEAD(&bond
->vlan_list
);
4529 /* Initialize the device entry points */
4530 ether_setup(bond_dev
);
4531 bond_dev
->netdev_ops
= &bond_netdev_ops
;
4532 bond_dev
->ethtool_ops
= &bond_ethtool_ops
;
4533 bond_set_mode_ops(bond
, bond
->params
.mode
);
4535 bond_dev
->destructor
= free_netdev
;
4537 /* Initialize the device options */
4538 bond_dev
->tx_queue_len
= 0;
4539 bond_dev
->flags
|= IFF_MASTER
|IFF_MULTICAST
;
4540 bond_dev
->priv_flags
|= IFF_BONDING
;
4541 bond_dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
4543 if (bond
->params
.arp_interval
)
4544 bond_dev
->priv_flags
|= IFF_MASTER_ARPMON
;
4546 /* At first, we block adding VLANs. That's the only way to
4547 * prevent problems that occur when adding VLANs over an
4548 * empty bond. The block will be removed once non-challenged
4549 * slaves are enslaved.
4551 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
4553 /* don't acquire bond device's netif_tx_lock when
4555 bond_dev
->features
|= NETIF_F_LLTX
;
4557 /* By default, we declare the bond to be fully
4558 * VLAN hardware accelerated capable. Special
4559 * care is taken in the various xmit functions
4560 * when there are slaves that are not hw accel
4563 bond_dev
->features
|= (NETIF_F_HW_VLAN_TX
|
4564 NETIF_F_HW_VLAN_RX
|
4565 NETIF_F_HW_VLAN_FILTER
);
4569 static void bond_work_cancel_all(struct bonding
*bond
)
4571 write_lock_bh(&bond
->lock
);
4572 bond
->kill_timers
= 1;
4573 write_unlock_bh(&bond
->lock
);
4575 if (bond
->params
.miimon
&& delayed_work_pending(&bond
->mii_work
))
4576 cancel_delayed_work(&bond
->mii_work
);
4578 if (bond
->params
.arp_interval
&& delayed_work_pending(&bond
->arp_work
))
4579 cancel_delayed_work(&bond
->arp_work
);
4581 if (bond
->params
.mode
== BOND_MODE_ALB
&&
4582 delayed_work_pending(&bond
->alb_work
))
4583 cancel_delayed_work(&bond
->alb_work
);
4585 if (bond
->params
.mode
== BOND_MODE_8023AD
&&
4586 delayed_work_pending(&bond
->ad_work
))
4587 cancel_delayed_work(&bond
->ad_work
);
4591 * Destroy a bonding device.
4592 * Must be under rtnl_lock when this function is called.
4594 static void bond_uninit(struct net_device
*bond_dev
)
4596 struct bonding
*bond
= netdev_priv(bond_dev
);
4598 /* Release the bonded slaves */
4599 bond_release_all(bond_dev
);
4601 list_del(&bond
->bond_list
);
4603 bond_work_cancel_all(bond
);
4605 bond_remove_proc_entry(bond
);
4608 destroy_workqueue(bond
->wq
);
4610 netif_addr_lock_bh(bond_dev
);
4611 bond_mc_list_destroy(bond
);
4612 netif_addr_unlock_bh(bond_dev
);
4615 /*------------------------- Module initialization ---------------------------*/
4618 * Convert string input module parms. Accept either the
4619 * number of the mode or its string name. A bit complicated because
4620 * some mode names are substrings of other names, and calls from sysfs
4621 * may have whitespace in the name (trailing newlines, for example).
4623 int bond_parse_parm(const char *buf
, const struct bond_parm_tbl
*tbl
)
4625 int modeint
= -1, i
, rv
;
4626 char *p
, modestr
[BOND_MAX_MODENAME_LEN
+ 1] = { 0, };
4628 for (p
= (char *)buf
; *p
; p
++)
4629 if (!(isdigit(*p
) || isspace(*p
)))
4633 rv
= sscanf(buf
, "%20s", modestr
);
4635 rv
= sscanf(buf
, "%d", &modeint
);
4640 for (i
= 0; tbl
[i
].modename
; i
++) {
4641 if (modeint
== tbl
[i
].mode
)
4643 if (strcmp(modestr
, tbl
[i
].modename
) == 0)
4650 static int bond_check_params(struct bond_params
*params
)
4652 int arp_validate_value
, fail_over_mac_value
, primary_reselect_value
;
4655 * Convert string parameters.
4658 bond_mode
= bond_parse_parm(mode
, bond_mode_tbl
);
4659 if (bond_mode
== -1) {
4661 ": Error: Invalid bonding mode \"%s\"\n",
4662 mode
== NULL
? "NULL" : mode
);
4667 if (xmit_hash_policy
) {
4668 if ((bond_mode
!= BOND_MODE_XOR
) &&
4669 (bond_mode
!= BOND_MODE_8023AD
)) {
4671 ": xmit_hash_policy param is irrelevant in"
4673 bond_mode_name(bond_mode
));
4675 xmit_hashtype
= bond_parse_parm(xmit_hash_policy
,
4677 if (xmit_hashtype
== -1) {
4679 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4680 xmit_hash_policy
== NULL
? "NULL" :
4688 if (bond_mode
!= BOND_MODE_8023AD
) {
4690 ": lacp_rate param is irrelevant in mode %s\n",
4691 bond_mode_name(bond_mode
));
4693 lacp_fast
= bond_parse_parm(lacp_rate
, bond_lacp_tbl
);
4694 if (lacp_fast
== -1) {
4696 ": Error: Invalid lacp rate \"%s\"\n",
4697 lacp_rate
== NULL
? "NULL" : lacp_rate
);
4704 params
->ad_select
= bond_parse_parm(ad_select
, ad_select_tbl
);
4705 if (params
->ad_select
== -1) {
4707 ": Error: Invalid ad_select \"%s\"\n",
4708 ad_select
== NULL
? "NULL" : ad_select
);
4712 if (bond_mode
!= BOND_MODE_8023AD
) {
4714 ": ad_select param only affects 802.3ad mode\n");
4717 params
->ad_select
= BOND_AD_STABLE
;
4720 if (max_bonds
< 0) {
4722 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4723 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4724 max_bonds
, 0, INT_MAX
, BOND_DEFAULT_MAX_BONDS
);
4725 max_bonds
= BOND_DEFAULT_MAX_BONDS
;
4730 ": Warning: miimon module parameter (%d), "
4731 "not in range 0-%d, so it was reset to %d\n",
4732 miimon
, INT_MAX
, BOND_LINK_MON_INTERV
);
4733 miimon
= BOND_LINK_MON_INTERV
;
4738 ": Warning: updelay module parameter (%d), "
4739 "not in range 0-%d, so it was reset to 0\n",
4744 if (downdelay
< 0) {
4746 ": Warning: downdelay module parameter (%d), "
4747 "not in range 0-%d, so it was reset to 0\n",
4748 downdelay
, INT_MAX
);
4752 if ((use_carrier
!= 0) && (use_carrier
!= 1)) {
4754 ": Warning: use_carrier module parameter (%d), "
4755 "not of valid value (0/1), so it was set to 1\n",
4760 if (num_grat_arp
< 0 || num_grat_arp
> 255) {
4762 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4763 "was reset to 1 \n", num_grat_arp
);
4767 if (num_unsol_na
< 0 || num_unsol_na
> 255) {
4769 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4770 "was reset to 1 \n", num_unsol_na
);
4774 /* reset values for 802.3ad */
4775 if (bond_mode
== BOND_MODE_8023AD
) {
4778 ": Warning: miimon must be specified, "
4779 "otherwise bonding will not detect link "
4780 "failure, speed and duplex which are "
4781 "essential for 802.3ad operation\n");
4782 pr_warning("Forcing miimon to 100msec\n");
4787 /* reset values for TLB/ALB */
4788 if ((bond_mode
== BOND_MODE_TLB
) ||
4789 (bond_mode
== BOND_MODE_ALB
)) {
4792 ": Warning: miimon must be specified, "
4793 "otherwise bonding will not detect link "
4794 "failure and link speed which are essential "
4795 "for TLB/ALB load balancing\n");
4796 pr_warning("Forcing miimon to 100msec\n");
4801 if (bond_mode
== BOND_MODE_ALB
) {
4803 ": In ALB mode you might experience client "
4804 "disconnections upon reconnection of a link if the "
4805 "bonding module updelay parameter (%d msec) is "
4806 "incompatible with the forwarding delay time of the "
4812 if (updelay
|| downdelay
) {
4813 /* just warn the user the up/down delay will have
4814 * no effect since miimon is zero...
4817 ": Warning: miimon module parameter not set "
4818 "and updelay (%d) or downdelay (%d) module "
4819 "parameter is set; updelay and downdelay have "
4820 "no effect unless miimon is set\n",
4821 updelay
, downdelay
);
4824 /* don't allow arp monitoring */
4827 ": Warning: miimon (%d) and arp_interval (%d) "
4828 "can't be used simultaneously, disabling ARP "
4830 miimon
, arp_interval
);
4834 if ((updelay
% miimon
) != 0) {
4836 ": Warning: updelay (%d) is not a multiple "
4837 "of miimon (%d), updelay rounded to %d ms\n",
4838 updelay
, miimon
, (updelay
/ miimon
) * miimon
);
4843 if ((downdelay
% miimon
) != 0) {
4845 ": Warning: downdelay (%d) is not a multiple "
4846 "of miimon (%d), downdelay rounded to %d ms\n",
4848 (downdelay
/ miimon
) * miimon
);
4851 downdelay
/= miimon
;
4854 if (arp_interval
< 0) {
4856 ": Warning: arp_interval module parameter (%d) "
4857 ", not in range 0-%d, so it was reset to %d\n",
4858 arp_interval
, INT_MAX
, BOND_LINK_ARP_INTERV
);
4859 arp_interval
= BOND_LINK_ARP_INTERV
;
4862 for (arp_ip_count
= 0;
4863 (arp_ip_count
< BOND_MAX_ARP_TARGETS
) && arp_ip_target
[arp_ip_count
];
4865 /* not complete check, but should be good enough to
4867 if (!isdigit(arp_ip_target
[arp_ip_count
][0])) {
4869 ": Warning: bad arp_ip_target module parameter "
4870 "(%s), ARP monitoring will not be performed\n",
4871 arp_ip_target
[arp_ip_count
]);
4874 __be32 ip
= in_aton(arp_ip_target
[arp_ip_count
]);
4875 arp_target
[arp_ip_count
] = ip
;
4879 if (arp_interval
&& !arp_ip_count
) {
4880 /* don't allow arping if no arp_ip_target given... */
4882 ": Warning: arp_interval module parameter (%d) "
4883 "specified without providing an arp_ip_target "
4884 "parameter, arp_interval was reset to 0\n",
4890 if (bond_mode
!= BOND_MODE_ACTIVEBACKUP
) {
4892 ": arp_validate only supported in active-backup mode\n");
4895 if (!arp_interval
) {
4897 ": arp_validate requires arp_interval\n");
4901 arp_validate_value
= bond_parse_parm(arp_validate
,
4903 if (arp_validate_value
== -1) {
4905 ": Error: invalid arp_validate \"%s\"\n",
4906 arp_validate
== NULL
? "NULL" : arp_validate
);
4910 arp_validate_value
= 0;
4914 ": MII link monitoring set to %d ms\n",
4916 } else if (arp_interval
) {
4919 pr_info(DRV_NAME
": ARP monitoring set to %d ms,"
4920 " validate %s, with %d target(s):",
4922 arp_validate_tbl
[arp_validate_value
].modename
,
4925 for (i
= 0; i
< arp_ip_count
; i
++)
4926 pr_info(" %s", arp_ip_target
[i
]);
4930 } else if (max_bonds
) {
4931 /* miimon and arp_interval not set, we need one so things
4932 * work as expected, see bonding.txt for details
4935 ": Warning: either miimon or arp_interval and "
4936 "arp_ip_target module parameters must be specified, "
4937 "otherwise bonding will not detect link failures! see "
4938 "bonding.txt for details.\n");
4941 if (primary
&& !USES_PRIMARY(bond_mode
)) {
4942 /* currently, using a primary only makes sense
4943 * in active backup, TLB or ALB modes
4946 ": Warning: %s primary device specified but has no "
4947 "effect in %s mode\n",
4948 primary
, bond_mode_name(bond_mode
));
4952 if (primary
&& primary_reselect
) {
4953 primary_reselect_value
= bond_parse_parm(primary_reselect
,
4955 if (primary_reselect_value
== -1) {
4957 ": Error: Invalid primary_reselect \"%s\"\n",
4959 NULL
? "NULL" : primary_reselect
);
4963 primary_reselect_value
= BOND_PRI_RESELECT_ALWAYS
;
4966 if (fail_over_mac
) {
4967 fail_over_mac_value
= bond_parse_parm(fail_over_mac
,
4969 if (fail_over_mac_value
== -1) {
4971 ": Error: invalid fail_over_mac \"%s\"\n",
4972 arp_validate
== NULL
? "NULL" : arp_validate
);
4976 if (bond_mode
!= BOND_MODE_ACTIVEBACKUP
)
4978 ": Warning: fail_over_mac only affects "
4979 "active-backup mode.\n");
4981 fail_over_mac_value
= BOND_FOM_NONE
;
4984 /* fill params struct with the proper values */
4985 params
->mode
= bond_mode
;
4986 params
->xmit_policy
= xmit_hashtype
;
4987 params
->miimon
= miimon
;
4988 params
->num_grat_arp
= num_grat_arp
;
4989 params
->num_unsol_na
= num_unsol_na
;
4990 params
->arp_interval
= arp_interval
;
4991 params
->arp_validate
= arp_validate_value
;
4992 params
->updelay
= updelay
;
4993 params
->downdelay
= downdelay
;
4994 params
->use_carrier
= use_carrier
;
4995 params
->lacp_fast
= lacp_fast
;
4996 params
->primary
[0] = 0;
4997 params
->primary_reselect
= primary_reselect_value
;
4998 params
->fail_over_mac
= fail_over_mac_value
;
5001 strncpy(params
->primary
, primary
, IFNAMSIZ
);
5002 params
->primary
[IFNAMSIZ
- 1] = 0;
5005 memcpy(params
->arp_targets
, arp_target
, sizeof(arp_target
));
5010 static struct lock_class_key bonding_netdev_xmit_lock_key
;
5011 static struct lock_class_key bonding_netdev_addr_lock_key
;
5013 static void bond_set_lockdep_class_one(struct net_device
*dev
,
5014 struct netdev_queue
*txq
,
5017 lockdep_set_class(&txq
->_xmit_lock
,
5018 &bonding_netdev_xmit_lock_key
);
5021 static void bond_set_lockdep_class(struct net_device
*dev
)
5023 lockdep_set_class(&dev
->addr_list_lock
,
5024 &bonding_netdev_addr_lock_key
);
5025 netdev_for_each_tx_queue(dev
, bond_set_lockdep_class_one
, NULL
);
5029 * Called from registration process
5031 static int bond_init(struct net_device
*bond_dev
)
5033 struct bonding
*bond
= netdev_priv(bond_dev
);
5035 pr_debug("Begin bond_init for %s\n", bond_dev
->name
);
5037 bond
->wq
= create_singlethread_workqueue(bond_dev
->name
);
5041 bond_set_lockdep_class(bond_dev
);
5043 netif_carrier_off(bond_dev
);
5045 bond_create_proc_entry(bond
);
5046 list_add_tail(&bond
->bond_list
, &bond_dev_list
);
5048 bond_prepare_sysfs_group(bond
);
5052 static int bond_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
5054 if (tb
[IFLA_ADDRESS
]) {
5055 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
5057 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
5058 return -EADDRNOTAVAIL
;
5063 static struct rtnl_link_ops bond_link_ops __read_mostly
= {
5065 .setup
= bond_setup
,
5066 .validate
= bond_validate
,
5069 /* Create a new bond based on the specified name and bonding parameters.
5070 * If name is NULL, obtain a suitable "bond%d" name for us.
5071 * Caller must NOT hold rtnl_lock; we need to release it here before we
5072 * set up our sysfs entries.
5074 int bond_create(const char *name
)
5076 struct net_device
*bond_dev
;
5081 bond_dev
= alloc_netdev(sizeof(struct bonding
), name
? name
: "",
5084 pr_err(DRV_NAME
": %s: eek! can't alloc netdev!\n",
5090 bond_dev
->rtnl_link_ops
= &bond_link_ops
;
5093 res
= dev_alloc_name(bond_dev
, "bond%d");
5098 res
= register_netdevice(bond_dev
);
5104 free_netdev(bond_dev
);
5108 static int __init
bonding_init(void)
5113 pr_info("%s", version
);
5115 res
= bond_check_params(&bonding_defaults
);
5119 bond_create_proc_dir();
5121 res
= rtnl_link_register(&bond_link_ops
);
5125 for (i
= 0; i
< max_bonds
; i
++) {
5126 res
= bond_create(NULL
);
5131 res
= bond_create_sysfs();
5135 register_netdevice_notifier(&bond_netdev_notifier
);
5136 register_inetaddr_notifier(&bond_inetaddr_notifier
);
5137 bond_register_ipv6_notifier();
5141 rtnl_link_unregister(&bond_link_ops
);
5142 bond_destroy_proc_dir();
5147 static void __exit
bonding_exit(void)
5149 unregister_netdevice_notifier(&bond_netdev_notifier
);
5150 unregister_inetaddr_notifier(&bond_inetaddr_notifier
);
5151 bond_unregister_ipv6_notifier();
5153 bond_destroy_sysfs();
5155 rtnl_link_unregister(&bond_link_ops
);
5156 bond_destroy_proc_dir();
5159 module_init(bonding_init
);
5160 module_exit(bonding_exit
);
5161 MODULE_LICENSE("GPL");
5162 MODULE_VERSION(DRV_VERSION
);
5163 MODULE_DESCRIPTION(DRV_DESCRIPTION
", v" DRV_VERSION
);
5164 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5165 MODULE_ALIAS_RTNL_LINK("bond");