2 * originally based on the dummy device.
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
7 * bonding.c: an Ethernet Bonding driver
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
14 * and probably many L2 switches ...
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
24 * will release all slaves, marking them as down.
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
46 #include <linux/tcp.h>
47 #include <linux/udp.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <linux/timer.h>
52 #include <linux/socket.h>
53 #include <linux/ctype.h>
54 #include <linux/inet.h>
55 #include <linux/bitops.h>
57 #include <asm/system.h>
59 #include <linux/uaccess.h>
60 #include <linux/errno.h>
61 #include <linux/netdevice.h>
62 #include <linux/inetdevice.h>
63 #include <linux/igmp.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
67 #include <linux/rtnetlink.h>
68 #include <linux/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 <linux/preempt.h>
77 #include <net/route.h>
78 #include <net/net_namespace.h>
79 #include <net/netns/generic.h>
84 /*---------------------------- Module parameters ----------------------------*/
86 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
87 #define BOND_LINK_MON_INTERV 0
88 #define BOND_LINK_ARP_INTERV 0
90 static int max_bonds
= BOND_DEFAULT_MAX_BONDS
;
91 static int tx_queues
= BOND_DEFAULT_TX_QUEUES
;
92 static int miimon
= BOND_LINK_MON_INTERV
;
95 static int use_carrier
= 1;
98 static char *primary_reselect
;
99 static char *lacp_rate
;
100 static char *ad_select
;
101 static char *xmit_hash_policy
;
102 static int arp_interval
= BOND_LINK_ARP_INTERV
;
103 static char *arp_ip_target
[BOND_MAX_ARP_TARGETS
];
104 static char *arp_validate
;
105 static char *fail_over_mac
;
106 static int all_slaves_active
= 0;
107 static struct bond_params bonding_defaults
;
108 static int resend_igmp
= BOND_DEFAULT_RESEND_IGMP
;
110 module_param(max_bonds
, int, 0);
111 MODULE_PARM_DESC(max_bonds
, "Max number of bonded devices");
112 module_param(tx_queues
, int, 0);
113 MODULE_PARM_DESC(tx_queues
, "Max number of transmit queues (default = 16)");
114 module_param(miimon
, int, 0);
115 MODULE_PARM_DESC(miimon
, "Link check interval in milliseconds");
116 module_param(updelay
, int, 0);
117 MODULE_PARM_DESC(updelay
, "Delay before considering link up, in milliseconds");
118 module_param(downdelay
, int, 0);
119 MODULE_PARM_DESC(downdelay
, "Delay before considering link down, "
121 module_param(use_carrier
, int, 0);
122 MODULE_PARM_DESC(use_carrier
, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
123 "0 for off, 1 for on (default)");
124 module_param(mode
, charp
, 0);
125 MODULE_PARM_DESC(mode
, "Mode of operation : 0 for balance-rr, "
126 "1 for active-backup, 2 for balance-xor, "
127 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
128 "6 for balance-alb");
129 module_param(primary
, charp
, 0);
130 MODULE_PARM_DESC(primary
, "Primary network device to use");
131 module_param(primary_reselect
, charp
, 0);
132 MODULE_PARM_DESC(primary_reselect
, "Reselect primary slave "
134 "0 for always (default), "
135 "1 for only if speed of primary is "
137 "2 for only on active slave "
139 module_param(lacp_rate
, charp
, 0);
140 MODULE_PARM_DESC(lacp_rate
, "LACPDU tx rate to request from 802.3ad partner "
142 module_param(ad_select
, charp
, 0);
143 MODULE_PARM_DESC(ad_select
, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
144 module_param(xmit_hash_policy
, charp
, 0);
145 MODULE_PARM_DESC(xmit_hash_policy
, "XOR hashing method: 0 for layer 2 (default)"
146 ", 1 for layer 3+4");
147 module_param(arp_interval
, int, 0);
148 MODULE_PARM_DESC(arp_interval
, "arp interval in milliseconds");
149 module_param_array(arp_ip_target
, charp
, NULL
, 0);
150 MODULE_PARM_DESC(arp_ip_target
, "arp targets in n.n.n.n form");
151 module_param(arp_validate
, charp
, 0);
152 MODULE_PARM_DESC(arp_validate
, "validate src/dst of ARP probes: none (default), active, backup or all");
153 module_param(fail_over_mac
, charp
, 0);
154 MODULE_PARM_DESC(fail_over_mac
, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
155 module_param(all_slaves_active
, int, 0);
156 MODULE_PARM_DESC(all_slaves_active
, "Keep all frames received on an interface"
157 "by setting active flag for all slaves. "
158 "0 for never (default), 1 for always.");
159 module_param(resend_igmp
, int, 0);
160 MODULE_PARM_DESC(resend_igmp
, "Number of IGMP membership reports to send on link failure");
162 /*----------------------------- Global variables ----------------------------*/
164 #ifdef CONFIG_NET_POLL_CONTROLLER
165 atomic_t netpoll_block_tx
= ATOMIC_INIT(0);
168 int bond_net_id __read_mostly
;
170 static __be32 arp_target
[BOND_MAX_ARP_TARGETS
];
171 static int arp_ip_count
;
172 static int bond_mode
= BOND_MODE_ROUNDROBIN
;
173 static int xmit_hashtype
= BOND_XMIT_POLICY_LAYER2
;
174 static int lacp_fast
;
176 const struct bond_parm_tbl bond_lacp_tbl
[] = {
177 { "slow", AD_LACP_SLOW
},
178 { "fast", AD_LACP_FAST
},
182 const struct bond_parm_tbl bond_mode_tbl
[] = {
183 { "balance-rr", BOND_MODE_ROUNDROBIN
},
184 { "active-backup", BOND_MODE_ACTIVEBACKUP
},
185 { "balance-xor", BOND_MODE_XOR
},
186 { "broadcast", BOND_MODE_BROADCAST
},
187 { "802.3ad", BOND_MODE_8023AD
},
188 { "balance-tlb", BOND_MODE_TLB
},
189 { "balance-alb", BOND_MODE_ALB
},
193 const struct bond_parm_tbl xmit_hashtype_tbl
[] = {
194 { "layer2", BOND_XMIT_POLICY_LAYER2
},
195 { "layer3+4", BOND_XMIT_POLICY_LAYER34
},
196 { "layer2+3", BOND_XMIT_POLICY_LAYER23
},
200 const struct bond_parm_tbl arp_validate_tbl
[] = {
201 { "none", BOND_ARP_VALIDATE_NONE
},
202 { "active", BOND_ARP_VALIDATE_ACTIVE
},
203 { "backup", BOND_ARP_VALIDATE_BACKUP
},
204 { "all", BOND_ARP_VALIDATE_ALL
},
208 const struct bond_parm_tbl fail_over_mac_tbl
[] = {
209 { "none", BOND_FOM_NONE
},
210 { "active", BOND_FOM_ACTIVE
},
211 { "follow", BOND_FOM_FOLLOW
},
215 const struct bond_parm_tbl pri_reselect_tbl
[] = {
216 { "always", BOND_PRI_RESELECT_ALWAYS
},
217 { "better", BOND_PRI_RESELECT_BETTER
},
218 { "failure", BOND_PRI_RESELECT_FAILURE
},
222 struct bond_parm_tbl ad_select_tbl
[] = {
223 { "stable", BOND_AD_STABLE
},
224 { "bandwidth", BOND_AD_BANDWIDTH
},
225 { "count", BOND_AD_COUNT
},
229 /*-------------------------- Forward declarations ---------------------------*/
231 static int bond_init(struct net_device
*bond_dev
);
232 static void bond_uninit(struct net_device
*bond_dev
);
234 /*---------------------------- General routines -----------------------------*/
236 const char *bond_mode_name(int mode
)
238 static const char *names
[] = {
239 [BOND_MODE_ROUNDROBIN
] = "load balancing (round-robin)",
240 [BOND_MODE_ACTIVEBACKUP
] = "fault-tolerance (active-backup)",
241 [BOND_MODE_XOR
] = "load balancing (xor)",
242 [BOND_MODE_BROADCAST
] = "fault-tolerance (broadcast)",
243 [BOND_MODE_8023AD
] = "IEEE 802.3ad Dynamic link aggregation",
244 [BOND_MODE_TLB
] = "transmit load balancing",
245 [BOND_MODE_ALB
] = "adaptive load balancing",
248 if (mode
< 0 || mode
> BOND_MODE_ALB
)
254 /*---------------------------------- VLAN -----------------------------------*/
257 * bond_add_vlan - add a new vlan id on bond
258 * @bond: bond that got the notification
259 * @vlan_id: the vlan id to add
261 * Returns -ENOMEM if allocation failed.
263 static int bond_add_vlan(struct bonding
*bond
, unsigned short vlan_id
)
265 struct vlan_entry
*vlan
;
267 pr_debug("bond: %s, vlan id %d\n",
268 (bond
? bond
->dev
->name
: "None"), vlan_id
);
270 vlan
= kzalloc(sizeof(struct vlan_entry
), GFP_KERNEL
);
274 INIT_LIST_HEAD(&vlan
->vlan_list
);
275 vlan
->vlan_id
= vlan_id
;
277 write_lock_bh(&bond
->lock
);
279 list_add_tail(&vlan
->vlan_list
, &bond
->vlan_list
);
281 write_unlock_bh(&bond
->lock
);
283 pr_debug("added VLAN ID %d on bond %s\n", vlan_id
, bond
->dev
->name
);
289 * bond_del_vlan - delete a vlan id from bond
290 * @bond: bond that got the notification
291 * @vlan_id: the vlan id to delete
293 * returns -ENODEV if @vlan_id was not found in @bond.
295 static int bond_del_vlan(struct bonding
*bond
, unsigned short vlan_id
)
297 struct vlan_entry
*vlan
;
300 pr_debug("bond: %s, vlan id %d\n", bond
->dev
->name
, vlan_id
);
303 write_lock_bh(&bond
->lock
);
305 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
306 if (vlan
->vlan_id
== vlan_id
) {
307 list_del(&vlan
->vlan_list
);
309 if (bond_is_lb(bond
))
310 bond_alb_clear_vlan(bond
, vlan_id
);
312 pr_debug("removed VLAN ID %d from bond %s\n",
313 vlan_id
, bond
->dev
->name
);
317 if (list_empty(&bond
->vlan_list
) &&
318 (bond
->slave_cnt
== 0)) {
319 /* Last VLAN removed and no slaves, so
320 * restore block on adding VLANs. This will
321 * be removed once new slaves that are not
322 * VLAN challenged will be added.
324 bond
->dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
332 pr_debug("couldn't find VLAN ID %d in bond %s\n",
333 vlan_id
, bond
->dev
->name
);
336 write_unlock_bh(&bond
->lock
);
337 unblock_netpoll_tx();
342 * bond_has_challenged_slaves
343 * @bond: the bond we're working on
345 * Searches the slave list. Returns 1 if a vlan challenged slave
346 * was found, 0 otherwise.
348 * Assumes bond->lock is held.
350 static int bond_has_challenged_slaves(struct bonding
*bond
)
355 bond_for_each_slave(bond
, slave
, i
) {
356 if (slave
->dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
357 pr_debug("found VLAN challenged slave - %s\n",
363 pr_debug("no VLAN challenged slaves found\n");
368 * bond_next_vlan - safely skip to the next item in the vlans list.
369 * @bond: the bond we're working on
370 * @curr: item we're advancing from
372 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
373 * or @curr->next otherwise (even if it is @curr itself again).
375 * Caller must hold bond->lock
377 struct vlan_entry
*bond_next_vlan(struct bonding
*bond
, struct vlan_entry
*curr
)
379 struct vlan_entry
*next
, *last
;
381 if (list_empty(&bond
->vlan_list
))
385 next
= list_entry(bond
->vlan_list
.next
,
386 struct vlan_entry
, vlan_list
);
388 last
= list_entry(bond
->vlan_list
.prev
,
389 struct vlan_entry
, vlan_list
);
391 next
= list_entry(bond
->vlan_list
.next
,
392 struct vlan_entry
, vlan_list
);
394 next
= list_entry(curr
->vlan_list
.next
,
395 struct vlan_entry
, vlan_list
);
403 * bond_dev_queue_xmit - Prepare skb for xmit.
405 * @bond: bond device that got this skb for tx.
406 * @skb: hw accel VLAN tagged skb to transmit
407 * @slave_dev: slave that is supposed to xmit this skbuff
409 int bond_dev_queue_xmit(struct bonding
*bond
, struct sk_buff
*skb
,
410 struct net_device
*slave_dev
)
412 skb
->dev
= slave_dev
;
414 if (unlikely(netpoll_tx_running(slave_dev
)))
415 bond_netpoll_send_skb(bond_get_slave_by_dev(bond
, slave_dev
), skb
);
423 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
424 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
426 * a. This operation is performed in IOCTL context,
427 * b. The operation is protected by the RTNL semaphore in the 8021q code,
428 * c. Holding a lock with BH disabled while directly calling a base driver
429 * entry point is generally a BAD idea.
431 * The design of synchronization/protection for this operation in the 8021q
432 * module is good for one or more VLAN devices over a single physical device
433 * and cannot be extended for a teaming solution like bonding, so there is a
434 * potential race condition here where a net device from the vlan group might
435 * be referenced (either by a base driver or the 8021q code) while it is being
436 * removed from the system. However, it turns out we're not making matters
437 * worse, and if it works for regular VLAN usage it will work here too.
441 * bond_vlan_rx_register - Propagates registration to slaves
442 * @bond_dev: bonding net device that got called
443 * @grp: vlan group being registered
445 static void bond_vlan_rx_register(struct net_device
*bond_dev
,
446 struct vlan_group
*grp
)
448 struct bonding
*bond
= netdev_priv(bond_dev
);
452 write_lock_bh(&bond
->lock
);
454 write_unlock_bh(&bond
->lock
);
456 bond_for_each_slave(bond
, slave
, i
) {
457 struct net_device
*slave_dev
= slave
->dev
;
458 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
460 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
461 slave_ops
->ndo_vlan_rx_register
) {
462 slave_ops
->ndo_vlan_rx_register(slave_dev
, grp
);
468 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
469 * @bond_dev: bonding net device that got called
470 * @vid: vlan id being added
472 static void bond_vlan_rx_add_vid(struct net_device
*bond_dev
, uint16_t vid
)
474 struct bonding
*bond
= netdev_priv(bond_dev
);
478 bond_for_each_slave(bond
, slave
, i
) {
479 struct net_device
*slave_dev
= slave
->dev
;
480 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
482 if ((slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
483 slave_ops
->ndo_vlan_rx_add_vid
) {
484 slave_ops
->ndo_vlan_rx_add_vid(slave_dev
, vid
);
488 res
= bond_add_vlan(bond
, vid
);
490 pr_err("%s: Error: Failed to add vlan id %d\n",
491 bond_dev
->name
, vid
);
496 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
497 * @bond_dev: bonding net device that got called
498 * @vid: vlan id being removed
500 static void bond_vlan_rx_kill_vid(struct net_device
*bond_dev
, uint16_t vid
)
502 struct bonding
*bond
= netdev_priv(bond_dev
);
504 struct net_device
*vlan_dev
;
507 bond_for_each_slave(bond
, slave
, i
) {
508 struct net_device
*slave_dev
= slave
->dev
;
509 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
511 if ((slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
512 slave_ops
->ndo_vlan_rx_kill_vid
) {
513 /* Save and then restore vlan_dev in the grp array,
514 * since the slave's driver might clear it.
516 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vid
);
517 slave_ops
->ndo_vlan_rx_kill_vid(slave_dev
, vid
);
518 vlan_group_set_device(bond
->vlgrp
, vid
, vlan_dev
);
522 res
= bond_del_vlan(bond
, vid
);
524 pr_err("%s: Error: Failed to remove vlan id %d\n",
525 bond_dev
->name
, vid
);
529 static void bond_add_vlans_on_slave(struct bonding
*bond
, struct net_device
*slave_dev
)
531 struct vlan_entry
*vlan
;
532 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
537 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
538 slave_ops
->ndo_vlan_rx_register
)
539 slave_ops
->ndo_vlan_rx_register(slave_dev
, bond
->vlgrp
);
541 if (!(slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) ||
542 !(slave_ops
->ndo_vlan_rx_add_vid
))
545 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
)
546 slave_ops
->ndo_vlan_rx_add_vid(slave_dev
, vlan
->vlan_id
);
549 static void bond_del_vlans_from_slave(struct bonding
*bond
,
550 struct net_device
*slave_dev
)
552 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
553 struct vlan_entry
*vlan
;
554 struct net_device
*vlan_dev
;
559 if (!(slave_dev
->features
& NETIF_F_HW_VLAN_FILTER
) ||
560 !(slave_ops
->ndo_vlan_rx_kill_vid
))
563 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
566 /* Save and then restore vlan_dev in the grp array,
567 * since the slave's driver might clear it.
569 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
570 slave_ops
->ndo_vlan_rx_kill_vid(slave_dev
, vlan
->vlan_id
);
571 vlan_group_set_device(bond
->vlgrp
, vlan
->vlan_id
, vlan_dev
);
575 if ((slave_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
576 slave_ops
->ndo_vlan_rx_register
)
577 slave_ops
->ndo_vlan_rx_register(slave_dev
, NULL
);
580 /*------------------------------- Link status -------------------------------*/
583 * Set the carrier state for the master according to the state of its
584 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
585 * do special 802.3ad magic.
587 * Returns zero if carrier state does not change, nonzero if it does.
589 static int bond_set_carrier(struct bonding
*bond
)
594 if (bond
->slave_cnt
== 0)
597 if (bond
->params
.mode
== BOND_MODE_8023AD
)
598 return bond_3ad_set_carrier(bond
);
600 bond_for_each_slave(bond
, slave
, i
) {
601 if (slave
->link
== BOND_LINK_UP
) {
602 if (!netif_carrier_ok(bond
->dev
)) {
603 netif_carrier_on(bond
->dev
);
611 if (netif_carrier_ok(bond
->dev
)) {
612 netif_carrier_off(bond
->dev
);
619 * Get link speed and duplex from the slave's base driver
620 * using ethtool. If for some reason the call fails or the
621 * values are invalid, fake speed and duplex to 100/Full
624 static int bond_update_speed_duplex(struct slave
*slave
)
626 struct net_device
*slave_dev
= slave
->dev
;
627 struct ethtool_cmd etool
= { .cmd
= ETHTOOL_GSET
};
631 /* Fake speed and duplex */
632 slave
->speed
= SPEED_100
;
633 slave
->duplex
= DUPLEX_FULL
;
635 if (!slave_dev
->ethtool_ops
|| !slave_dev
->ethtool_ops
->get_settings
)
638 res
= slave_dev
->ethtool_ops
->get_settings(slave_dev
, &etool
);
642 slave_speed
= ethtool_cmd_speed(&etool
);
643 switch (slave_speed
) {
653 switch (etool
.duplex
) {
661 slave
->speed
= slave_speed
;
662 slave
->duplex
= etool
.duplex
;
668 * if <dev> supports MII link status reporting, check its link status.
670 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
671 * depending upon the setting of the use_carrier parameter.
673 * Return either BMSR_LSTATUS, meaning that the link is up (or we
674 * can't tell and just pretend it is), or 0, meaning that the link is
677 * If reporting is non-zero, instead of faking link up, return -1 if
678 * both ETHTOOL and MII ioctls fail (meaning the device does not
679 * support them). If use_carrier is set, return whatever it says.
680 * It'd be nice if there was a good way to tell if a driver supports
681 * netif_carrier, but there really isn't.
683 static int bond_check_dev_link(struct bonding
*bond
,
684 struct net_device
*slave_dev
, int reporting
)
686 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
687 int (*ioctl
)(struct net_device
*, struct ifreq
*, int);
689 struct mii_ioctl_data
*mii
;
691 if (!reporting
&& !netif_running(slave_dev
))
694 if (bond
->params
.use_carrier
)
695 return netif_carrier_ok(slave_dev
) ? BMSR_LSTATUS
: 0;
697 /* Try to get link status using Ethtool first. */
698 if (slave_dev
->ethtool_ops
) {
699 if (slave_dev
->ethtool_ops
->get_link
) {
702 link
= slave_dev
->ethtool_ops
->get_link(slave_dev
);
704 return link
? BMSR_LSTATUS
: 0;
708 /* Ethtool can't be used, fallback to MII ioctls. */
709 ioctl
= slave_ops
->ndo_do_ioctl
;
711 /* TODO: set pointer to correct ioctl on a per team member */
712 /* bases to make this more efficient. that is, once */
713 /* we determine the correct ioctl, we will always */
714 /* call it and not the others for that team */
718 * We cannot assume that SIOCGMIIPHY will also read a
719 * register; not all network drivers (e.g., e100)
723 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
724 strncpy(ifr
.ifr_name
, slave_dev
->name
, IFNAMSIZ
);
726 if (IOCTL(slave_dev
, &ifr
, SIOCGMIIPHY
) == 0) {
727 mii
->reg_num
= MII_BMSR
;
728 if (IOCTL(slave_dev
, &ifr
, SIOCGMIIREG
) == 0)
729 return mii
->val_out
& BMSR_LSTATUS
;
734 * If reporting, report that either there's no dev->do_ioctl,
735 * or both SIOCGMIIREG and get_link failed (meaning that we
736 * cannot report link status). If not reporting, pretend
739 return reporting
? -1 : BMSR_LSTATUS
;
742 /*----------------------------- Multicast list ------------------------------*/
745 * Push the promiscuity flag down to appropriate slaves
747 static int bond_set_promiscuity(struct bonding
*bond
, int inc
)
750 if (USES_PRIMARY(bond
->params
.mode
)) {
751 /* write lock already acquired */
752 if (bond
->curr_active_slave
) {
753 err
= dev_set_promiscuity(bond
->curr_active_slave
->dev
,
759 bond_for_each_slave(bond
, slave
, i
) {
760 err
= dev_set_promiscuity(slave
->dev
, inc
);
769 * Push the allmulti flag down to all slaves
771 static int bond_set_allmulti(struct bonding
*bond
, int inc
)
774 if (USES_PRIMARY(bond
->params
.mode
)) {
775 /* write lock already acquired */
776 if (bond
->curr_active_slave
) {
777 err
= dev_set_allmulti(bond
->curr_active_slave
->dev
,
783 bond_for_each_slave(bond
, slave
, i
) {
784 err
= dev_set_allmulti(slave
->dev
, inc
);
793 * Add a Multicast address to slaves
796 static void bond_mc_add(struct bonding
*bond
, void *addr
)
798 if (USES_PRIMARY(bond
->params
.mode
)) {
799 /* write lock already acquired */
800 if (bond
->curr_active_slave
)
801 dev_mc_add(bond
->curr_active_slave
->dev
, addr
);
806 bond_for_each_slave(bond
, slave
, i
)
807 dev_mc_add(slave
->dev
, addr
);
812 * Remove a multicast address from slave
815 static void bond_mc_del(struct bonding
*bond
, void *addr
)
817 if (USES_PRIMARY(bond
->params
.mode
)) {
818 /* write lock already acquired */
819 if (bond
->curr_active_slave
)
820 dev_mc_del(bond
->curr_active_slave
->dev
, addr
);
824 bond_for_each_slave(bond
, slave
, i
) {
825 dev_mc_del(slave
->dev
, addr
);
831 static void __bond_resend_igmp_join_requests(struct net_device
*dev
)
833 struct in_device
*in_dev
;
836 in_dev
= __in_dev_get_rcu(dev
);
838 ip_mc_rejoin_groups(in_dev
);
843 * Retrieve the list of registered multicast addresses for the bonding
844 * device and retransmit an IGMP JOIN request to the current active
847 static void bond_resend_igmp_join_requests(struct bonding
*bond
)
849 struct net_device
*vlan_dev
;
850 struct vlan_entry
*vlan
;
852 read_lock(&bond
->lock
);
854 /* rejoin all groups on bond device */
855 __bond_resend_igmp_join_requests(bond
->dev
);
857 /* rejoin all groups on vlan devices */
859 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
860 vlan_dev
= vlan_group_get_device(bond
->vlgrp
,
863 __bond_resend_igmp_join_requests(vlan_dev
);
867 if (--bond
->igmp_retrans
> 0)
868 queue_delayed_work(bond
->wq
, &bond
->mcast_work
, HZ
/5);
870 read_unlock(&bond
->lock
);
873 static void bond_resend_igmp_join_requests_delayed(struct work_struct
*work
)
875 struct bonding
*bond
= container_of(work
, struct bonding
,
877 bond_resend_igmp_join_requests(bond
);
881 * flush all members of flush->mc_list from device dev->mc_list
883 static void bond_mc_list_flush(struct net_device
*bond_dev
,
884 struct net_device
*slave_dev
)
886 struct bonding
*bond
= netdev_priv(bond_dev
);
887 struct netdev_hw_addr
*ha
;
889 netdev_for_each_mc_addr(ha
, bond_dev
)
890 dev_mc_del(slave_dev
, ha
->addr
);
892 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
893 /* del lacpdu mc addr from mc list */
894 u8 lacpdu_multicast
[ETH_ALEN
] = MULTICAST_LACPDU_ADDR
;
896 dev_mc_del(slave_dev
, lacpdu_multicast
);
900 /*--------------------------- Active slave change ---------------------------*/
903 * Update the mc list and multicast-related flags for the new and
904 * old active slaves (if any) according to the multicast mode, and
905 * promiscuous flags unconditionally.
907 static void bond_mc_swap(struct bonding
*bond
, struct slave
*new_active
,
908 struct slave
*old_active
)
910 struct netdev_hw_addr
*ha
;
912 if (!USES_PRIMARY(bond
->params
.mode
))
913 /* nothing to do - mc list is already up-to-date on
919 if (bond
->dev
->flags
& IFF_PROMISC
)
920 dev_set_promiscuity(old_active
->dev
, -1);
922 if (bond
->dev
->flags
& IFF_ALLMULTI
)
923 dev_set_allmulti(old_active
->dev
, -1);
925 netdev_for_each_mc_addr(ha
, bond
->dev
)
926 dev_mc_del(old_active
->dev
, ha
->addr
);
930 /* FIXME: Signal errors upstream. */
931 if (bond
->dev
->flags
& IFF_PROMISC
)
932 dev_set_promiscuity(new_active
->dev
, 1);
934 if (bond
->dev
->flags
& IFF_ALLMULTI
)
935 dev_set_allmulti(new_active
->dev
, 1);
937 netdev_for_each_mc_addr(ha
, bond
->dev
)
938 dev_mc_add(new_active
->dev
, ha
->addr
);
943 * bond_do_fail_over_mac
945 * Perform special MAC address swapping for fail_over_mac settings
947 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
949 static void bond_do_fail_over_mac(struct bonding
*bond
,
950 struct slave
*new_active
,
951 struct slave
*old_active
)
952 __releases(&bond
->curr_slave_lock
)
953 __releases(&bond
->lock
)
954 __acquires(&bond
->lock
)
955 __acquires(&bond
->curr_slave_lock
)
957 u8 tmp_mac
[ETH_ALEN
];
958 struct sockaddr saddr
;
961 switch (bond
->params
.fail_over_mac
) {
962 case BOND_FOM_ACTIVE
:
964 memcpy(bond
->dev
->dev_addr
, new_active
->dev
->dev_addr
,
965 new_active
->dev
->addr_len
);
967 case BOND_FOM_FOLLOW
:
969 * if new_active && old_active, swap them
970 * if just old_active, do nothing (going to no active slave)
971 * if just new_active, set new_active to bond's MAC
976 write_unlock_bh(&bond
->curr_slave_lock
);
977 read_unlock(&bond
->lock
);
980 memcpy(tmp_mac
, new_active
->dev
->dev_addr
, ETH_ALEN
);
981 memcpy(saddr
.sa_data
, old_active
->dev
->dev_addr
,
983 saddr
.sa_family
= new_active
->dev
->type
;
985 memcpy(saddr
.sa_data
, bond
->dev
->dev_addr
, ETH_ALEN
);
986 saddr
.sa_family
= bond
->dev
->type
;
989 rv
= dev_set_mac_address(new_active
->dev
, &saddr
);
991 pr_err("%s: Error %d setting MAC of slave %s\n",
992 bond
->dev
->name
, -rv
, new_active
->dev
->name
);
999 memcpy(saddr
.sa_data
, tmp_mac
, ETH_ALEN
);
1000 saddr
.sa_family
= old_active
->dev
->type
;
1002 rv
= dev_set_mac_address(old_active
->dev
, &saddr
);
1004 pr_err("%s: Error %d setting MAC of slave %s\n",
1005 bond
->dev
->name
, -rv
, new_active
->dev
->name
);
1007 read_lock(&bond
->lock
);
1008 write_lock_bh(&bond
->curr_slave_lock
);
1011 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
1012 bond
->dev
->name
, bond
->params
.fail_over_mac
);
1018 static bool bond_should_change_active(struct bonding
*bond
)
1020 struct slave
*prim
= bond
->primary_slave
;
1021 struct slave
*curr
= bond
->curr_active_slave
;
1023 if (!prim
|| !curr
|| curr
->link
!= BOND_LINK_UP
)
1025 if (bond
->force_primary
) {
1026 bond
->force_primary
= false;
1029 if (bond
->params
.primary_reselect
== BOND_PRI_RESELECT_BETTER
&&
1030 (prim
->speed
< curr
->speed
||
1031 (prim
->speed
== curr
->speed
&& prim
->duplex
<= curr
->duplex
)))
1033 if (bond
->params
.primary_reselect
== BOND_PRI_RESELECT_FAILURE
)
1039 * find_best_interface - select the best available slave to be the active one
1040 * @bond: our bonding struct
1042 * Warning: Caller must hold curr_slave_lock for writing.
1044 static struct slave
*bond_find_best_slave(struct bonding
*bond
)
1046 struct slave
*new_active
, *old_active
;
1047 struct slave
*bestslave
= NULL
;
1048 int mintime
= bond
->params
.updelay
;
1051 new_active
= bond
->curr_active_slave
;
1053 if (!new_active
) { /* there were no active slaves left */
1054 if (bond
->slave_cnt
> 0) /* found one slave */
1055 new_active
= bond
->first_slave
;
1057 return NULL
; /* still no slave, return NULL */
1060 if ((bond
->primary_slave
) &&
1061 bond
->primary_slave
->link
== BOND_LINK_UP
&&
1062 bond_should_change_active(bond
)) {
1063 new_active
= bond
->primary_slave
;
1066 /* remember where to stop iterating over the slaves */
1067 old_active
= new_active
;
1069 bond_for_each_slave_from(bond
, new_active
, i
, old_active
) {
1070 if (new_active
->link
== BOND_LINK_UP
) {
1072 } else if (new_active
->link
== BOND_LINK_BACK
&&
1073 IS_UP(new_active
->dev
)) {
1074 /* link up, but waiting for stabilization */
1075 if (new_active
->delay
< mintime
) {
1076 mintime
= new_active
->delay
;
1077 bestslave
= new_active
;
1086 * change_active_interface - change the active slave into the specified one
1087 * @bond: our bonding struct
1088 * @new: the new slave to make the active one
1090 * Set the new slave to the bond's settings and unset them on the old
1091 * curr_active_slave.
1092 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1094 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1095 * because it is apparently the best available slave we have, even though its
1096 * updelay hasn't timed out yet.
1098 * If new_active is not NULL, caller must hold bond->lock for read and
1099 * curr_slave_lock for write_bh.
1101 void bond_change_active_slave(struct bonding
*bond
, struct slave
*new_active
)
1103 struct slave
*old_active
= bond
->curr_active_slave
;
1105 if (old_active
== new_active
)
1109 new_active
->jiffies
= jiffies
;
1111 if (new_active
->link
== BOND_LINK_BACK
) {
1112 if (USES_PRIMARY(bond
->params
.mode
)) {
1113 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1114 bond
->dev
->name
, new_active
->dev
->name
,
1115 (bond
->params
.updelay
- new_active
->delay
) * bond
->params
.miimon
);
1118 new_active
->delay
= 0;
1119 new_active
->link
= BOND_LINK_UP
;
1121 if (bond
->params
.mode
== BOND_MODE_8023AD
)
1122 bond_3ad_handle_link_change(new_active
, BOND_LINK_UP
);
1124 if (bond_is_lb(bond
))
1125 bond_alb_handle_link_change(bond
, new_active
, BOND_LINK_UP
);
1127 if (USES_PRIMARY(bond
->params
.mode
)) {
1128 pr_info("%s: making interface %s the new active one.\n",
1129 bond
->dev
->name
, new_active
->dev
->name
);
1134 if (USES_PRIMARY(bond
->params
.mode
))
1135 bond_mc_swap(bond
, new_active
, old_active
);
1137 if (bond_is_lb(bond
)) {
1138 bond_alb_handle_active_change(bond
, new_active
);
1140 bond_set_slave_inactive_flags(old_active
);
1142 bond_set_slave_active_flags(new_active
);
1144 bond
->curr_active_slave
= new_active
;
1147 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
) {
1149 bond_set_slave_inactive_flags(old_active
);
1152 bond_set_slave_active_flags(new_active
);
1154 if (bond
->params
.fail_over_mac
)
1155 bond_do_fail_over_mac(bond
, new_active
,
1158 write_unlock_bh(&bond
->curr_slave_lock
);
1159 read_unlock(&bond
->lock
);
1161 netdev_bonding_change(bond
->dev
, NETDEV_BONDING_FAILOVER
);
1163 read_lock(&bond
->lock
);
1164 write_lock_bh(&bond
->curr_slave_lock
);
1168 /* resend IGMP joins since active slave has changed or
1169 * all were sent on curr_active_slave */
1170 if (((USES_PRIMARY(bond
->params
.mode
) && new_active
) ||
1171 bond
->params
.mode
== BOND_MODE_ROUNDROBIN
) &&
1172 netif_running(bond
->dev
)) {
1173 bond
->igmp_retrans
= bond
->params
.resend_igmp
;
1174 queue_delayed_work(bond
->wq
, &bond
->mcast_work
, 0);
1179 * bond_select_active_slave - select a new active slave, if needed
1180 * @bond: our bonding struct
1182 * This functions should be called when one of the following occurs:
1183 * - The old curr_active_slave has been released or lost its link.
1184 * - The primary_slave has got its link back.
1185 * - A slave has got its link back and there's no old curr_active_slave.
1187 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1189 void bond_select_active_slave(struct bonding
*bond
)
1191 struct slave
*best_slave
;
1194 best_slave
= bond_find_best_slave(bond
);
1195 if (best_slave
!= bond
->curr_active_slave
) {
1196 bond_change_active_slave(bond
, best_slave
);
1197 rv
= bond_set_carrier(bond
);
1201 if (netif_carrier_ok(bond
->dev
)) {
1202 pr_info("%s: first active interface up!\n",
1205 pr_info("%s: now running without any active interface !\n",
1211 /*--------------------------- slave list handling ---------------------------*/
1214 * This function attaches the slave to the end of list.
1216 * bond->lock held for writing by caller.
1218 static void bond_attach_slave(struct bonding
*bond
, struct slave
*new_slave
)
1220 if (bond
->first_slave
== NULL
) { /* attaching the first slave */
1221 new_slave
->next
= new_slave
;
1222 new_slave
->prev
= new_slave
;
1223 bond
->first_slave
= new_slave
;
1225 new_slave
->next
= bond
->first_slave
;
1226 new_slave
->prev
= bond
->first_slave
->prev
;
1227 new_slave
->next
->prev
= new_slave
;
1228 new_slave
->prev
->next
= new_slave
;
1235 * This function detaches the slave from the list.
1236 * WARNING: no check is made to verify if the slave effectively
1237 * belongs to <bond>.
1238 * Nothing is freed on return, structures are just unchained.
1239 * If any slave pointer in bond was pointing to <slave>,
1240 * it should be changed by the calling function.
1242 * bond->lock held for writing by caller.
1244 static void bond_detach_slave(struct bonding
*bond
, struct slave
*slave
)
1247 slave
->next
->prev
= slave
->prev
;
1250 slave
->prev
->next
= slave
->next
;
1252 if (bond
->first_slave
== slave
) { /* slave is the first slave */
1253 if (bond
->slave_cnt
> 1) { /* there are more slave */
1254 bond
->first_slave
= slave
->next
;
1256 bond
->first_slave
= NULL
; /* slave was the last one */
1265 #ifdef CONFIG_NET_POLL_CONTROLLER
1266 static inline int slave_enable_netpoll(struct slave
*slave
)
1271 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
1276 np
->dev
= slave
->dev
;
1277 err
= __netpoll_setup(np
);
1286 static inline void slave_disable_netpoll(struct slave
*slave
)
1288 struct netpoll
*np
= slave
->np
;
1294 synchronize_rcu_bh();
1295 __netpoll_cleanup(np
);
1298 static inline bool slave_dev_support_netpoll(struct net_device
*slave_dev
)
1300 if (slave_dev
->priv_flags
& IFF_DISABLE_NETPOLL
)
1302 if (!slave_dev
->netdev_ops
->ndo_poll_controller
)
1307 static void bond_poll_controller(struct net_device
*bond_dev
)
1311 static void __bond_netpoll_cleanup(struct bonding
*bond
)
1313 struct slave
*slave
;
1316 bond_for_each_slave(bond
, slave
, i
)
1317 if (IS_UP(slave
->dev
))
1318 slave_disable_netpoll(slave
);
1320 static void bond_netpoll_cleanup(struct net_device
*bond_dev
)
1322 struct bonding
*bond
= netdev_priv(bond_dev
);
1324 read_lock(&bond
->lock
);
1325 __bond_netpoll_cleanup(bond
);
1326 read_unlock(&bond
->lock
);
1329 static int bond_netpoll_setup(struct net_device
*dev
, struct netpoll_info
*ni
)
1331 struct bonding
*bond
= netdev_priv(dev
);
1332 struct slave
*slave
;
1335 read_lock(&bond
->lock
);
1336 bond_for_each_slave(bond
, slave
, i
) {
1337 err
= slave_enable_netpoll(slave
);
1339 __bond_netpoll_cleanup(bond
);
1343 read_unlock(&bond
->lock
);
1347 static struct netpoll_info
*bond_netpoll_info(struct bonding
*bond
)
1349 return bond
->dev
->npinfo
;
1353 static inline int slave_enable_netpoll(struct slave
*slave
)
1357 static inline void slave_disable_netpoll(struct slave
*slave
)
1360 static void bond_netpoll_cleanup(struct net_device
*bond_dev
)
1365 /*---------------------------------- IOCTL ----------------------------------*/
1367 static int bond_sethwaddr(struct net_device
*bond_dev
,
1368 struct net_device
*slave_dev
)
1370 pr_debug("bond_dev=%p\n", bond_dev
);
1371 pr_debug("slave_dev=%p\n", slave_dev
);
1372 pr_debug("slave_dev->addr_len=%d\n", slave_dev
->addr_len
);
1373 memcpy(bond_dev
->dev_addr
, slave_dev
->dev_addr
, slave_dev
->addr_len
);
1377 #define BOND_VLAN_FEATURES \
1378 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1379 NETIF_F_HW_VLAN_FILTER)
1382 * Compute the common dev->feature set available to all slaves. Some
1383 * feature bits are managed elsewhere, so preserve those feature bits
1384 * on the master device.
1386 static int bond_compute_features(struct bonding
*bond
)
1388 struct slave
*slave
;
1389 struct net_device
*bond_dev
= bond
->dev
;
1390 u32 features
= bond_dev
->features
;
1391 u32 vlan_features
= 0;
1392 unsigned short max_hard_header_len
= max((u16
)ETH_HLEN
,
1393 bond_dev
->hard_header_len
);
1396 features
&= ~(NETIF_F_ALL_CSUM
| BOND_VLAN_FEATURES
);
1397 features
|= NETIF_F_GSO_MASK
| NETIF_F_NO_CSUM
| NETIF_F_NOCACHE_COPY
;
1399 if (!bond
->first_slave
)
1402 features
&= ~NETIF_F_ONE_FOR_ALL
;
1404 vlan_features
= bond
->first_slave
->dev
->vlan_features
;
1405 bond_for_each_slave(bond
, slave
, i
) {
1406 features
= netdev_increment_features(features
,
1407 slave
->dev
->features
,
1408 NETIF_F_ONE_FOR_ALL
);
1409 vlan_features
= netdev_increment_features(vlan_features
,
1410 slave
->dev
->vlan_features
,
1411 NETIF_F_ONE_FOR_ALL
);
1412 if (slave
->dev
->hard_header_len
> max_hard_header_len
)
1413 max_hard_header_len
= slave
->dev
->hard_header_len
;
1417 features
|= (bond_dev
->features
& BOND_VLAN_FEATURES
);
1418 bond_dev
->features
= netdev_fix_features(bond_dev
, features
);
1419 bond_dev
->vlan_features
= netdev_fix_features(bond_dev
, vlan_features
);
1420 bond_dev
->hard_header_len
= max_hard_header_len
;
1425 static void bond_setup_by_slave(struct net_device
*bond_dev
,
1426 struct net_device
*slave_dev
)
1428 struct bonding
*bond
= netdev_priv(bond_dev
);
1430 bond_dev
->header_ops
= slave_dev
->header_ops
;
1432 bond_dev
->type
= slave_dev
->type
;
1433 bond_dev
->hard_header_len
= slave_dev
->hard_header_len
;
1434 bond_dev
->addr_len
= slave_dev
->addr_len
;
1436 memcpy(bond_dev
->broadcast
, slave_dev
->broadcast
,
1437 slave_dev
->addr_len
);
1438 bond
->setup_by_slave
= 1;
1441 /* On bonding slaves other than the currently active slave, suppress
1442 * duplicates except for alb non-mcast/bcast.
1444 static bool bond_should_deliver_exact_match(struct sk_buff
*skb
,
1445 struct slave
*slave
,
1446 struct bonding
*bond
)
1448 if (bond_is_slave_inactive(slave
)) {
1449 if (bond
->params
.mode
== BOND_MODE_ALB
&&
1450 skb
->pkt_type
!= PACKET_BROADCAST
&&
1451 skb
->pkt_type
!= PACKET_MULTICAST
)
1458 static rx_handler_result_t
bond_handle_frame(struct sk_buff
**pskb
)
1460 struct sk_buff
*skb
= *pskb
;
1461 struct slave
*slave
;
1462 struct bonding
*bond
;
1464 skb
= skb_share_check(skb
, GFP_ATOMIC
);
1466 return RX_HANDLER_CONSUMED
;
1470 slave
= bond_slave_get_rcu(skb
->dev
);
1473 if (bond
->params
.arp_interval
)
1474 slave
->dev
->last_rx
= jiffies
;
1476 if (bond
->recv_probe
) {
1477 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
1480 bond
->recv_probe(nskb
, bond
, slave
);
1481 dev_kfree_skb(nskb
);
1485 if (bond_should_deliver_exact_match(skb
, slave
, bond
)) {
1486 return RX_HANDLER_EXACT
;
1489 skb
->dev
= bond
->dev
;
1491 if (bond
->params
.mode
== BOND_MODE_ALB
&&
1492 bond
->dev
->priv_flags
& IFF_BRIDGE_PORT
&&
1493 skb
->pkt_type
== PACKET_HOST
) {
1495 if (unlikely(skb_cow_head(skb
,
1496 skb
->data
- skb_mac_header(skb
)))) {
1498 return RX_HANDLER_CONSUMED
;
1500 memcpy(eth_hdr(skb
)->h_dest
, bond
->dev
->dev_addr
, ETH_ALEN
);
1503 return RX_HANDLER_ANOTHER
;
1506 /* enslave device <slave> to bond device <master> */
1507 int bond_enslave(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1509 struct bonding
*bond
= netdev_priv(bond_dev
);
1510 const struct net_device_ops
*slave_ops
= slave_dev
->netdev_ops
;
1511 struct slave
*new_slave
= NULL
;
1512 struct netdev_hw_addr
*ha
;
1513 struct sockaddr addr
;
1515 int old_features
= bond_dev
->features
;
1518 if (!bond
->params
.use_carrier
&& slave_dev
->ethtool_ops
== NULL
&&
1519 slave_ops
->ndo_do_ioctl
== NULL
) {
1520 pr_warning("%s: Warning: no link monitoring support for %s\n",
1521 bond_dev
->name
, slave_dev
->name
);
1524 /* bond must be initialized by bond_open() before enslaving */
1525 if (!(bond_dev
->flags
& IFF_UP
)) {
1526 pr_warning("%s: master_dev is not up in bond_enslave\n",
1530 /* already enslaved */
1531 if (slave_dev
->flags
& IFF_SLAVE
) {
1532 pr_debug("Error, Device was already enslaved\n");
1536 /* vlan challenged mutual exclusion */
1537 /* no need to lock since we're protected by rtnl_lock */
1538 if (slave_dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
1539 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev
->name
);
1541 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1542 bond_dev
->name
, slave_dev
->name
, bond_dev
->name
);
1545 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1546 bond_dev
->name
, slave_dev
->name
,
1547 slave_dev
->name
, bond_dev
->name
);
1548 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1551 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev
->name
);
1552 if (bond
->slave_cnt
== 0) {
1553 /* First slave, and it is not VLAN challenged,
1554 * so remove the block of adding VLANs over the bond.
1556 bond_dev
->features
&= ~NETIF_F_VLAN_CHALLENGED
;
1561 * Old ifenslave binaries are no longer supported. These can
1562 * be identified with moderate accuracy by the state of the slave:
1563 * the current ifenslave will set the interface down prior to
1564 * enslaving it; the old ifenslave will not.
1566 if ((slave_dev
->flags
& IFF_UP
)) {
1567 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
1570 goto err_undo_flags
;
1573 /* set bonding device ether type by slave - bonding netdevices are
1574 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1575 * there is a need to override some of the type dependent attribs/funcs.
1577 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1578 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1580 if (bond
->slave_cnt
== 0) {
1581 if (bond_dev
->type
!= slave_dev
->type
) {
1582 pr_debug("%s: change device type from %d to %d\n",
1584 bond_dev
->type
, slave_dev
->type
);
1586 res
= netdev_bonding_change(bond_dev
,
1587 NETDEV_PRE_TYPE_CHANGE
);
1588 res
= notifier_to_errno(res
);
1590 pr_err("%s: refused to change device type\n",
1593 goto err_undo_flags
;
1596 /* Flush unicast and multicast addresses */
1597 dev_uc_flush(bond_dev
);
1598 dev_mc_flush(bond_dev
);
1600 if (slave_dev
->type
!= ARPHRD_ETHER
)
1601 bond_setup_by_slave(bond_dev
, slave_dev
);
1603 ether_setup(bond_dev
);
1605 netdev_bonding_change(bond_dev
,
1606 NETDEV_POST_TYPE_CHANGE
);
1608 } else if (bond_dev
->type
!= slave_dev
->type
) {
1609 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1611 slave_dev
->type
, bond_dev
->type
);
1613 goto err_undo_flags
;
1616 if (slave_ops
->ndo_set_mac_address
== NULL
) {
1617 if (bond
->slave_cnt
== 0) {
1618 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1620 bond
->params
.fail_over_mac
= BOND_FOM_ACTIVE
;
1621 } else if (bond
->params
.fail_over_mac
!= BOND_FOM_ACTIVE
) {
1622 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1625 goto err_undo_flags
;
1629 /* If this is the first slave, then we need to set the master's hardware
1630 * address to be the same as the slave's. */
1631 if (is_zero_ether_addr(bond
->dev
->dev_addr
))
1632 memcpy(bond
->dev
->dev_addr
, slave_dev
->dev_addr
,
1633 slave_dev
->addr_len
);
1636 new_slave
= kzalloc(sizeof(struct slave
), GFP_KERNEL
);
1639 goto err_undo_flags
;
1643 * Set the new_slave's queue_id to be zero. Queue ID mapping
1644 * is set via sysfs or module option if desired.
1646 new_slave
->queue_id
= 0;
1648 /* Save slave's original mtu and then set it to match the bond */
1649 new_slave
->original_mtu
= slave_dev
->mtu
;
1650 res
= dev_set_mtu(slave_dev
, bond
->dev
->mtu
);
1652 pr_debug("Error %d calling dev_set_mtu\n", res
);
1657 * Save slave's original ("permanent") mac address for modes
1658 * that need it, and for restoring it upon release, and then
1659 * set it to the master's address
1661 memcpy(new_slave
->perm_hwaddr
, slave_dev
->dev_addr
, ETH_ALEN
);
1663 if (!bond
->params
.fail_over_mac
) {
1665 * Set slave to master's mac address. The application already
1666 * set the master's mac address to that of the first slave
1668 memcpy(addr
.sa_data
, bond_dev
->dev_addr
, bond_dev
->addr_len
);
1669 addr
.sa_family
= slave_dev
->type
;
1670 res
= dev_set_mac_address(slave_dev
, &addr
);
1672 pr_debug("Error %d calling set_mac_address\n", res
);
1673 goto err_restore_mtu
;
1677 res
= netdev_set_bond_master(slave_dev
, bond_dev
);
1679 pr_debug("Error %d calling netdev_set_bond_master\n", res
);
1680 goto err_restore_mac
;
1683 /* open the slave since the application closed it */
1684 res
= dev_open(slave_dev
);
1686 pr_debug("Opening slave %s failed\n", slave_dev
->name
);
1687 goto err_unset_master
;
1690 new_slave
->bond
= bond
;
1691 new_slave
->dev
= slave_dev
;
1692 slave_dev
->priv_flags
|= IFF_BONDING
;
1694 if (bond_is_lb(bond
)) {
1695 /* bond_alb_init_slave() must be called before all other stages since
1696 * it might fail and we do not want to have to undo everything
1698 res
= bond_alb_init_slave(bond
, new_slave
);
1703 /* If the mode USES_PRIMARY, then the new slave gets the
1704 * master's promisc (and mc) settings only if it becomes the
1705 * curr_active_slave, and that is taken care of later when calling
1706 * bond_change_active()
1708 if (!USES_PRIMARY(bond
->params
.mode
)) {
1709 /* set promiscuity level to new slave */
1710 if (bond_dev
->flags
& IFF_PROMISC
) {
1711 res
= dev_set_promiscuity(slave_dev
, 1);
1716 /* set allmulti level to new slave */
1717 if (bond_dev
->flags
& IFF_ALLMULTI
) {
1718 res
= dev_set_allmulti(slave_dev
, 1);
1723 netif_addr_lock_bh(bond_dev
);
1724 /* upload master's mc_list to new slave */
1725 netdev_for_each_mc_addr(ha
, bond_dev
)
1726 dev_mc_add(slave_dev
, ha
->addr
);
1727 netif_addr_unlock_bh(bond_dev
);
1730 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1731 /* add lacpdu mc addr to mc list */
1732 u8 lacpdu_multicast
[ETH_ALEN
] = MULTICAST_LACPDU_ADDR
;
1734 dev_mc_add(slave_dev
, lacpdu_multicast
);
1737 bond_add_vlans_on_slave(bond
, slave_dev
);
1739 write_lock_bh(&bond
->lock
);
1741 bond_attach_slave(bond
, new_slave
);
1743 new_slave
->delay
= 0;
1744 new_slave
->link_failure_count
= 0;
1746 bond_compute_features(bond
);
1748 write_unlock_bh(&bond
->lock
);
1750 read_lock(&bond
->lock
);
1752 new_slave
->last_arp_rx
= jiffies
;
1754 if (bond
->params
.miimon
&& !bond
->params
.use_carrier
) {
1755 link_reporting
= bond_check_dev_link(bond
, slave_dev
, 1);
1757 if ((link_reporting
== -1) && !bond
->params
.arp_interval
) {
1759 * miimon is set but a bonded network driver
1760 * does not support ETHTOOL/MII and
1761 * arp_interval is not set. Note: if
1762 * use_carrier is enabled, we will never go
1763 * here (because netif_carrier is always
1764 * supported); thus, we don't need to change
1765 * the messages for netif_carrier.
1767 pr_warning("%s: Warning: MII and ETHTOOL support not available for interface %s, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details.\n",
1768 bond_dev
->name
, slave_dev
->name
);
1769 } else if (link_reporting
== -1) {
1770 /* unable get link status using mii/ethtool */
1771 pr_warning("%s: Warning: can't get link status from interface %s; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface.\n",
1772 bond_dev
->name
, slave_dev
->name
);
1776 /* check for initial state */
1777 if (!bond
->params
.miimon
||
1778 (bond_check_dev_link(bond
, slave_dev
, 0) == BMSR_LSTATUS
)) {
1779 if (bond
->params
.updelay
) {
1780 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
1781 new_slave
->link
= BOND_LINK_BACK
;
1782 new_slave
->delay
= bond
->params
.updelay
;
1784 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
1785 new_slave
->link
= BOND_LINK_UP
;
1787 new_slave
->jiffies
= jiffies
;
1789 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
1790 new_slave
->link
= BOND_LINK_DOWN
;
1793 if (bond_update_speed_duplex(new_slave
) &&
1794 (new_slave
->link
!= BOND_LINK_DOWN
)) {
1795 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1796 bond_dev
->name
, new_slave
->dev
->name
);
1798 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1799 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1804 if (USES_PRIMARY(bond
->params
.mode
) && bond
->params
.primary
[0]) {
1805 /* if there is a primary slave, remember it */
1806 if (strcmp(bond
->params
.primary
, new_slave
->dev
->name
) == 0) {
1807 bond
->primary_slave
= new_slave
;
1808 bond
->force_primary
= true;
1812 write_lock_bh(&bond
->curr_slave_lock
);
1814 switch (bond
->params
.mode
) {
1815 case BOND_MODE_ACTIVEBACKUP
:
1816 bond_set_slave_inactive_flags(new_slave
);
1817 bond_select_active_slave(bond
);
1819 case BOND_MODE_8023AD
:
1820 /* in 802.3ad mode, the internal mechanism
1821 * will activate the slaves in the selected
1824 bond_set_slave_inactive_flags(new_slave
);
1825 /* if this is the first slave */
1826 if (bond
->slave_cnt
== 1) {
1827 SLAVE_AD_INFO(new_slave
).id
= 1;
1828 /* Initialize AD with the number of times that the AD timer is called in 1 second
1829 * can be called only after the mac address of the bond is set
1831 bond_3ad_initialize(bond
, 1000/AD_TIMER_INTERVAL
,
1832 bond
->params
.lacp_fast
);
1834 SLAVE_AD_INFO(new_slave
).id
=
1835 SLAVE_AD_INFO(new_slave
->prev
).id
+ 1;
1838 bond_3ad_bind_slave(new_slave
);
1842 bond_set_active_slave(new_slave
);
1843 bond_set_slave_inactive_flags(new_slave
);
1844 bond_select_active_slave(bond
);
1847 pr_debug("This slave is always active in trunk mode\n");
1849 /* always active in trunk mode */
1850 bond_set_active_slave(new_slave
);
1852 /* In trunking mode there is little meaning to curr_active_slave
1853 * anyway (it holds no special properties of the bond device),
1854 * so we can change it without calling change_active_interface()
1856 if (!bond
->curr_active_slave
)
1857 bond
->curr_active_slave
= new_slave
;
1860 } /* switch(bond_mode) */
1862 write_unlock_bh(&bond
->curr_slave_lock
);
1864 bond_set_carrier(bond
);
1866 #ifdef CONFIG_NET_POLL_CONTROLLER
1867 slave_dev
->npinfo
= bond_netpoll_info(bond
);
1868 if (slave_dev
->npinfo
) {
1869 if (slave_enable_netpoll(new_slave
)) {
1870 read_unlock(&bond
->lock
);
1871 pr_info("Error, %s: master_dev is using netpoll, "
1872 "but new slave device does not support netpoll.\n",
1880 read_unlock(&bond
->lock
);
1882 res
= bond_create_slave_symlinks(bond_dev
, slave_dev
);
1886 res
= netdev_rx_handler_register(slave_dev
, bond_handle_frame
,
1889 pr_debug("Error %d calling netdev_rx_handler_register\n", res
);
1890 goto err_dest_symlinks
;
1893 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1894 bond_dev
->name
, slave_dev
->name
,
1895 bond_is_active_slave(new_slave
) ? "n active" : " backup",
1896 new_slave
->link
!= BOND_LINK_DOWN
? "n up" : " down");
1898 /* enslave is successful */
1901 /* Undo stages on error */
1903 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
1906 dev_close(slave_dev
);
1909 netdev_set_bond_master(slave_dev
, NULL
);
1912 if (!bond
->params
.fail_over_mac
) {
1913 /* XXX TODO - fom follow mode needs to change master's
1914 * MAC if this slave's MAC is in use by the bond, or at
1915 * least print a warning.
1917 memcpy(addr
.sa_data
, new_slave
->perm_hwaddr
, ETH_ALEN
);
1918 addr
.sa_family
= slave_dev
->type
;
1919 dev_set_mac_address(slave_dev
, &addr
);
1923 dev_set_mtu(slave_dev
, new_slave
->original_mtu
);
1929 bond_dev
->features
= old_features
;
1935 * Try to release the slave device <slave> from the bond device <master>
1936 * It is legal to access curr_active_slave without a lock because all the function
1939 * The rules for slave state should be:
1940 * for Active/Backup:
1941 * Active stays on all backups go down
1942 * for Bonded connections:
1943 * The first up interface should be left on and all others downed.
1945 int bond_release(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
1947 struct bonding
*bond
= netdev_priv(bond_dev
);
1948 struct slave
*slave
, *oldcurrent
;
1949 struct sockaddr addr
;
1951 /* slave is not a slave or master is not master of this slave */
1952 if (!(slave_dev
->flags
& IFF_SLAVE
) ||
1953 (slave_dev
->master
!= bond_dev
)) {
1954 pr_err("%s: Error: cannot release %s.\n",
1955 bond_dev
->name
, slave_dev
->name
);
1960 netdev_bonding_change(bond_dev
, NETDEV_BONDING_DESLAVE
);
1961 write_lock_bh(&bond
->lock
);
1963 slave
= bond_get_slave_by_dev(bond
, slave_dev
);
1965 /* not a slave of this bond */
1966 pr_info("%s: %s not enslaved\n",
1967 bond_dev
->name
, slave_dev
->name
);
1968 write_unlock_bh(&bond
->lock
);
1969 unblock_netpoll_tx();
1973 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1974 * for this slave anymore.
1976 netdev_rx_handler_unregister(slave_dev
);
1977 write_unlock_bh(&bond
->lock
);
1979 write_lock_bh(&bond
->lock
);
1981 if (!bond
->params
.fail_over_mac
) {
1982 if (!compare_ether_addr(bond_dev
->dev_addr
, slave
->perm_hwaddr
) &&
1983 bond
->slave_cnt
> 1)
1984 pr_warning("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n",
1985 bond_dev
->name
, slave_dev
->name
,
1987 bond_dev
->name
, slave_dev
->name
);
1990 /* Inform AD package of unbinding of slave. */
1991 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1992 /* must be called before the slave is
1993 * detached from the list
1995 bond_3ad_unbind_slave(slave
);
1998 pr_info("%s: releasing %s interface %s\n",
2000 bond_is_active_slave(slave
) ? "active" : "backup",
2003 oldcurrent
= bond
->curr_active_slave
;
2005 bond
->current_arp_slave
= NULL
;
2007 /* release the slave from its bond */
2008 bond_detach_slave(bond
, slave
);
2010 bond_compute_features(bond
);
2012 if (bond
->primary_slave
== slave
)
2013 bond
->primary_slave
= NULL
;
2015 if (oldcurrent
== slave
)
2016 bond_change_active_slave(bond
, NULL
);
2018 if (bond_is_lb(bond
)) {
2019 /* Must be called only after the slave has been
2020 * detached from the list and the curr_active_slave
2021 * has been cleared (if our_slave == old_current),
2022 * but before a new active slave is selected.
2024 write_unlock_bh(&bond
->lock
);
2025 bond_alb_deinit_slave(bond
, slave
);
2026 write_lock_bh(&bond
->lock
);
2029 if (oldcurrent
== slave
) {
2031 * Note that we hold RTNL over this sequence, so there
2032 * is no concern that another slave add/remove event
2035 write_unlock_bh(&bond
->lock
);
2036 read_lock(&bond
->lock
);
2037 write_lock_bh(&bond
->curr_slave_lock
);
2039 bond_select_active_slave(bond
);
2041 write_unlock_bh(&bond
->curr_slave_lock
);
2042 read_unlock(&bond
->lock
);
2043 write_lock_bh(&bond
->lock
);
2046 if (bond
->slave_cnt
== 0) {
2047 bond_set_carrier(bond
);
2049 /* if the last slave was removed, zero the mac address
2050 * of the master so it will be set by the application
2051 * to the mac address of the first slave
2053 memset(bond_dev
->dev_addr
, 0, bond_dev
->addr_len
);
2056 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
2058 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2059 bond_dev
->name
, bond_dev
->name
);
2060 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2063 } else if ((bond_dev
->features
& NETIF_F_VLAN_CHALLENGED
) &&
2064 !bond_has_challenged_slaves(bond
)) {
2065 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2066 bond_dev
->name
, slave_dev
->name
, bond_dev
->name
);
2067 bond_dev
->features
&= ~NETIF_F_VLAN_CHALLENGED
;
2070 write_unlock_bh(&bond
->lock
);
2071 unblock_netpoll_tx();
2073 /* must do this from outside any spinlocks */
2074 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
2076 bond_del_vlans_from_slave(bond
, slave_dev
);
2078 /* If the mode USES_PRIMARY, then we should only remove its
2079 * promisc and mc settings if it was the curr_active_slave, but that was
2080 * already taken care of above when we detached the slave
2082 if (!USES_PRIMARY(bond
->params
.mode
)) {
2083 /* unset promiscuity level from slave */
2084 if (bond_dev
->flags
& IFF_PROMISC
)
2085 dev_set_promiscuity(slave_dev
, -1);
2087 /* unset allmulti level from slave */
2088 if (bond_dev
->flags
& IFF_ALLMULTI
)
2089 dev_set_allmulti(slave_dev
, -1);
2091 /* flush master's mc_list from slave */
2092 netif_addr_lock_bh(bond_dev
);
2093 bond_mc_list_flush(bond_dev
, slave_dev
);
2094 netif_addr_unlock_bh(bond_dev
);
2097 netdev_set_bond_master(slave_dev
, NULL
);
2099 slave_disable_netpoll(slave
);
2101 /* close slave before restoring its mac address */
2102 dev_close(slave_dev
);
2104 if (bond
->params
.fail_over_mac
!= BOND_FOM_ACTIVE
) {
2105 /* restore original ("permanent") mac address */
2106 memcpy(addr
.sa_data
, slave
->perm_hwaddr
, ETH_ALEN
);
2107 addr
.sa_family
= slave_dev
->type
;
2108 dev_set_mac_address(slave_dev
, &addr
);
2111 dev_set_mtu(slave_dev
, slave
->original_mtu
);
2113 slave_dev
->priv_flags
&= ~IFF_BONDING
;
2117 return 0; /* deletion OK */
2121 * First release a slave and then destroy the bond if no more slaves are left.
2122 * Must be under rtnl_lock when this function is called.
2124 static int bond_release_and_destroy(struct net_device
*bond_dev
,
2125 struct net_device
*slave_dev
)
2127 struct bonding
*bond
= netdev_priv(bond_dev
);
2130 ret
= bond_release(bond_dev
, slave_dev
);
2131 if ((ret
== 0) && (bond
->slave_cnt
== 0)) {
2132 bond_dev
->priv_flags
|= IFF_DISABLE_NETPOLL
;
2133 pr_info("%s: destroying bond %s.\n",
2134 bond_dev
->name
, bond_dev
->name
);
2135 unregister_netdevice(bond_dev
);
2141 * This function releases all slaves.
2143 static int bond_release_all(struct net_device
*bond_dev
)
2145 struct bonding
*bond
= netdev_priv(bond_dev
);
2146 struct slave
*slave
;
2147 struct net_device
*slave_dev
;
2148 struct sockaddr addr
;
2150 write_lock_bh(&bond
->lock
);
2152 netif_carrier_off(bond_dev
);
2154 if (bond
->slave_cnt
== 0)
2157 bond
->current_arp_slave
= NULL
;
2158 bond
->primary_slave
= NULL
;
2159 bond_change_active_slave(bond
, NULL
);
2161 while ((slave
= bond
->first_slave
) != NULL
) {
2162 /* Inform AD package of unbinding of slave
2163 * before slave is detached from the list.
2165 if (bond
->params
.mode
== BOND_MODE_8023AD
)
2166 bond_3ad_unbind_slave(slave
);
2168 slave_dev
= slave
->dev
;
2169 bond_detach_slave(bond
, slave
);
2171 /* now that the slave is detached, unlock and perform
2172 * all the undo steps that should not be called from
2175 write_unlock_bh(&bond
->lock
);
2177 /* unregister rx_handler early so bond_handle_frame wouldn't
2178 * be called for this slave anymore.
2180 netdev_rx_handler_unregister(slave_dev
);
2183 if (bond_is_lb(bond
)) {
2184 /* must be called only after the slave
2185 * has been detached from the list
2187 bond_alb_deinit_slave(bond
, slave
);
2190 bond_compute_features(bond
);
2192 bond_destroy_slave_symlinks(bond_dev
, slave_dev
);
2193 bond_del_vlans_from_slave(bond
, slave_dev
);
2195 /* If the mode USES_PRIMARY, then we should only remove its
2196 * promisc and mc settings if it was the curr_active_slave, but that was
2197 * already taken care of above when we detached the slave
2199 if (!USES_PRIMARY(bond
->params
.mode
)) {
2200 /* unset promiscuity level from slave */
2201 if (bond_dev
->flags
& IFF_PROMISC
)
2202 dev_set_promiscuity(slave_dev
, -1);
2204 /* unset allmulti level from slave */
2205 if (bond_dev
->flags
& IFF_ALLMULTI
)
2206 dev_set_allmulti(slave_dev
, -1);
2208 /* flush master's mc_list from slave */
2209 netif_addr_lock_bh(bond_dev
);
2210 bond_mc_list_flush(bond_dev
, slave_dev
);
2211 netif_addr_unlock_bh(bond_dev
);
2214 netdev_set_bond_master(slave_dev
, NULL
);
2216 slave_disable_netpoll(slave
);
2218 /* close slave before restoring its mac address */
2219 dev_close(slave_dev
);
2221 if (!bond
->params
.fail_over_mac
) {
2222 /* restore original ("permanent") mac address*/
2223 memcpy(addr
.sa_data
, slave
->perm_hwaddr
, ETH_ALEN
);
2224 addr
.sa_family
= slave_dev
->type
;
2225 dev_set_mac_address(slave_dev
, &addr
);
2230 /* re-acquire the lock before getting the next slave */
2231 write_lock_bh(&bond
->lock
);
2234 /* zero the mac address of the master so it will be
2235 * set by the application to the mac address of the
2238 memset(bond_dev
->dev_addr
, 0, bond_dev
->addr_len
);
2241 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
2243 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2244 bond_dev
->name
, bond_dev
->name
);
2245 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2249 pr_info("%s: released all slaves\n", bond_dev
->name
);
2252 write_unlock_bh(&bond
->lock
);
2257 * This function changes the active slave to slave <slave_dev>.
2258 * It returns -EINVAL in the following cases.
2259 * - <slave_dev> is not found in the list.
2260 * - There is not active slave now.
2261 * - <slave_dev> is already active.
2262 * - The link state of <slave_dev> is not BOND_LINK_UP.
2263 * - <slave_dev> is not running.
2264 * In these cases, this function does nothing.
2265 * In the other cases, current_slave pointer is changed and 0 is returned.
2267 static int bond_ioctl_change_active(struct net_device
*bond_dev
, struct net_device
*slave_dev
)
2269 struct bonding
*bond
= netdev_priv(bond_dev
);
2270 struct slave
*old_active
= NULL
;
2271 struct slave
*new_active
= NULL
;
2274 if (!USES_PRIMARY(bond
->params
.mode
))
2277 /* Verify that master_dev is indeed the master of slave_dev */
2278 if (!(slave_dev
->flags
& IFF_SLAVE
) || (slave_dev
->master
!= bond_dev
))
2281 read_lock(&bond
->lock
);
2283 read_lock(&bond
->curr_slave_lock
);
2284 old_active
= bond
->curr_active_slave
;
2285 read_unlock(&bond
->curr_slave_lock
);
2287 new_active
= bond_get_slave_by_dev(bond
, slave_dev
);
2290 * Changing to the current active: do nothing; return success.
2292 if (new_active
&& (new_active
== old_active
)) {
2293 read_unlock(&bond
->lock
);
2299 (new_active
->link
== BOND_LINK_UP
) &&
2300 IS_UP(new_active
->dev
)) {
2302 write_lock_bh(&bond
->curr_slave_lock
);
2303 bond_change_active_slave(bond
, new_active
);
2304 write_unlock_bh(&bond
->curr_slave_lock
);
2305 unblock_netpoll_tx();
2309 read_unlock(&bond
->lock
);
2314 static int bond_info_query(struct net_device
*bond_dev
, struct ifbond
*info
)
2316 struct bonding
*bond
= netdev_priv(bond_dev
);
2318 info
->bond_mode
= bond
->params
.mode
;
2319 info
->miimon
= bond
->params
.miimon
;
2321 read_lock(&bond
->lock
);
2322 info
->num_slaves
= bond
->slave_cnt
;
2323 read_unlock(&bond
->lock
);
2328 static int bond_slave_info_query(struct net_device
*bond_dev
, struct ifslave
*info
)
2330 struct bonding
*bond
= netdev_priv(bond_dev
);
2331 struct slave
*slave
;
2332 int i
, res
= -ENODEV
;
2334 read_lock(&bond
->lock
);
2336 bond_for_each_slave(bond
, slave
, i
) {
2337 if (i
== (int)info
->slave_id
) {
2339 strcpy(info
->slave_name
, slave
->dev
->name
);
2340 info
->link
= slave
->link
;
2341 info
->state
= bond_slave_state(slave
);
2342 info
->link_failure_count
= slave
->link_failure_count
;
2347 read_unlock(&bond
->lock
);
2352 /*-------------------------------- Monitoring -------------------------------*/
2355 static int bond_miimon_inspect(struct bonding
*bond
)
2357 struct slave
*slave
;
2358 int i
, link_state
, commit
= 0;
2359 bool ignore_updelay
;
2361 ignore_updelay
= !bond
->curr_active_slave
? true : false;
2363 bond_for_each_slave(bond
, slave
, i
) {
2364 slave
->new_link
= BOND_LINK_NOCHANGE
;
2366 link_state
= bond_check_dev_link(bond
, slave
->dev
, 0);
2368 switch (slave
->link
) {
2373 slave
->link
= BOND_LINK_FAIL
;
2374 slave
->delay
= bond
->params
.downdelay
;
2376 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2378 (bond
->params
.mode
==
2379 BOND_MODE_ACTIVEBACKUP
) ?
2380 (bond_is_active_slave(slave
) ?
2381 "active " : "backup ") : "",
2383 bond
->params
.downdelay
* bond
->params
.miimon
);
2386 case BOND_LINK_FAIL
:
2389 * recovered before downdelay expired
2391 slave
->link
= BOND_LINK_UP
;
2392 slave
->jiffies
= jiffies
;
2393 pr_info("%s: link status up again after %d ms for interface %s.\n",
2395 (bond
->params
.downdelay
- slave
->delay
) *
2396 bond
->params
.miimon
,
2401 if (slave
->delay
<= 0) {
2402 slave
->new_link
= BOND_LINK_DOWN
;
2410 case BOND_LINK_DOWN
:
2414 slave
->link
= BOND_LINK_BACK
;
2415 slave
->delay
= bond
->params
.updelay
;
2418 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2419 bond
->dev
->name
, slave
->dev
->name
,
2420 ignore_updelay
? 0 :
2421 bond
->params
.updelay
*
2422 bond
->params
.miimon
);
2425 case BOND_LINK_BACK
:
2427 slave
->link
= BOND_LINK_DOWN
;
2428 pr_info("%s: link status down again after %d ms for interface %s.\n",
2430 (bond
->params
.updelay
- slave
->delay
) *
2431 bond
->params
.miimon
,
2440 if (slave
->delay
<= 0) {
2441 slave
->new_link
= BOND_LINK_UP
;
2443 ignore_updelay
= false;
2455 static void bond_miimon_commit(struct bonding
*bond
)
2457 struct slave
*slave
;
2460 bond_for_each_slave(bond
, slave
, i
) {
2461 switch (slave
->new_link
) {
2462 case BOND_LINK_NOCHANGE
:
2466 slave
->link
= BOND_LINK_UP
;
2467 slave
->jiffies
= jiffies
;
2469 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
2470 /* prevent it from being the active one */
2471 bond_set_backup_slave(slave
);
2472 } else if (bond
->params
.mode
!= BOND_MODE_ACTIVEBACKUP
) {
2473 /* make it immediately active */
2474 bond_set_active_slave(slave
);
2475 } else if (slave
!= bond
->primary_slave
) {
2476 /* prevent it from being the active one */
2477 bond_set_backup_slave(slave
);
2480 bond_update_speed_duplex(slave
);
2482 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
2483 bond
->dev
->name
, slave
->dev
->name
,
2484 slave
->speed
, slave
->duplex
? "full" : "half");
2486 /* notify ad that the link status has changed */
2487 if (bond
->params
.mode
== BOND_MODE_8023AD
)
2488 bond_3ad_handle_link_change(slave
, BOND_LINK_UP
);
2490 if (bond_is_lb(bond
))
2491 bond_alb_handle_link_change(bond
, slave
,
2494 if (!bond
->curr_active_slave
||
2495 (slave
== bond
->primary_slave
))
2500 case BOND_LINK_DOWN
:
2501 if (slave
->link_failure_count
< UINT_MAX
)
2502 slave
->link_failure_count
++;
2504 slave
->link
= BOND_LINK_DOWN
;
2506 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
||
2507 bond
->params
.mode
== BOND_MODE_8023AD
)
2508 bond_set_slave_inactive_flags(slave
);
2510 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2511 bond
->dev
->name
, slave
->dev
->name
);
2513 if (bond
->params
.mode
== BOND_MODE_8023AD
)
2514 bond_3ad_handle_link_change(slave
,
2517 if (bond_is_lb(bond
))
2518 bond_alb_handle_link_change(bond
, slave
,
2521 if (slave
== bond
->curr_active_slave
)
2527 pr_err("%s: invalid new link %d on slave %s\n",
2528 bond
->dev
->name
, slave
->new_link
,
2530 slave
->new_link
= BOND_LINK_NOCHANGE
;
2538 write_lock_bh(&bond
->curr_slave_lock
);
2539 bond_select_active_slave(bond
);
2540 write_unlock_bh(&bond
->curr_slave_lock
);
2541 unblock_netpoll_tx();
2544 bond_set_carrier(bond
);
2550 * Really a wrapper that splits the mii monitor into two phases: an
2551 * inspection, then (if inspection indicates something needs to be done)
2552 * an acquisition of appropriate locks followed by a commit phase to
2553 * implement whatever link state changes are indicated.
2555 void bond_mii_monitor(struct work_struct
*work
)
2557 struct bonding
*bond
= container_of(work
, struct bonding
,
2560 read_lock(&bond
->lock
);
2561 if (bond
->kill_timers
)
2564 if (bond
->slave_cnt
== 0)
2567 if (bond_miimon_inspect(bond
)) {
2568 read_unlock(&bond
->lock
);
2570 read_lock(&bond
->lock
);
2572 bond_miimon_commit(bond
);
2574 read_unlock(&bond
->lock
);
2575 rtnl_unlock(); /* might sleep, hold no other locks */
2576 read_lock(&bond
->lock
);
2580 if (bond
->params
.miimon
)
2581 queue_delayed_work(bond
->wq
, &bond
->mii_work
,
2582 msecs_to_jiffies(bond
->params
.miimon
));
2584 read_unlock(&bond
->lock
);
2587 static __be32
bond_glean_dev_ip(struct net_device
*dev
)
2589 struct in_device
*idev
;
2590 struct in_ifaddr
*ifa
;
2597 idev
= __in_dev_get_rcu(dev
);
2601 ifa
= idev
->ifa_list
;
2605 addr
= ifa
->ifa_local
;
2611 static int bond_has_this_ip(struct bonding
*bond
, __be32 ip
)
2613 struct vlan_entry
*vlan
;
2615 if (ip
== bond
->master_ip
)
2618 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
2619 if (ip
== vlan
->vlan_ip
)
2627 * We go to the (large) trouble of VLAN tagging ARP frames because
2628 * switches in VLAN mode (especially if ports are configured as
2629 * "native" to a VLAN) might not pass non-tagged frames.
2631 static void bond_arp_send(struct net_device
*slave_dev
, int arp_op
, __be32 dest_ip
, __be32 src_ip
, unsigned short vlan_id
)
2633 struct sk_buff
*skb
;
2635 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op
,
2636 slave_dev
->name
, dest_ip
, src_ip
, vlan_id
);
2638 skb
= arp_create(arp_op
, ETH_P_ARP
, dest_ip
, slave_dev
, src_ip
,
2639 NULL
, slave_dev
->dev_addr
, NULL
);
2642 pr_err("ARP packet allocation failed\n");
2646 skb
= vlan_put_tag(skb
, vlan_id
);
2648 pr_err("failed to insert VLAN tag\n");
2656 static void bond_arp_send_all(struct bonding
*bond
, struct slave
*slave
)
2659 __be32
*targets
= bond
->params
.arp_targets
;
2660 struct vlan_entry
*vlan
;
2661 struct net_device
*vlan_dev
;
2664 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
); i
++) {
2667 pr_debug("basa: target %x\n", targets
[i
]);
2669 pr_debug("basa: empty vlan: arp_send\n");
2670 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2671 bond
->master_ip
, 0);
2676 * If VLANs are configured, we do a route lookup to
2677 * determine which VLAN interface would be used, so we
2678 * can tag the ARP with the proper VLAN tag.
2680 rt
= ip_route_output(dev_net(bond
->dev
), targets
[i
], 0,
2683 if (net_ratelimit()) {
2684 pr_warning("%s: no route to arp_ip_target %pI4\n",
2685 bond
->dev
->name
, &targets
[i
]);
2691 * This target is not on a VLAN
2693 if (rt
->dst
.dev
== bond
->dev
) {
2695 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2696 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2697 bond
->master_ip
, 0);
2702 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
2703 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
2704 if (vlan_dev
== rt
->dst
.dev
) {
2705 vlan_id
= vlan
->vlan_id
;
2706 pr_debug("basa: vlan match on %s %d\n",
2707 vlan_dev
->name
, vlan_id
);
2714 bond_arp_send(slave
->dev
, ARPOP_REQUEST
, targets
[i
],
2715 vlan
->vlan_ip
, vlan_id
);
2719 if (net_ratelimit()) {
2720 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2721 bond
->dev
->name
, &targets
[i
],
2722 rt
->dst
.dev
? rt
->dst
.dev
->name
: "NULL");
2728 static void bond_validate_arp(struct bonding
*bond
, struct slave
*slave
, __be32 sip
, __be32 tip
)
2731 __be32
*targets
= bond
->params
.arp_targets
;
2733 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
) && targets
[i
]; i
++) {
2734 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2735 &sip
, &tip
, i
, &targets
[i
],
2736 bond_has_this_ip(bond
, tip
));
2737 if (sip
== targets
[i
]) {
2738 if (bond_has_this_ip(bond
, tip
))
2739 slave
->last_arp_rx
= jiffies
;
2745 static void bond_arp_rcv(struct sk_buff
*skb
, struct bonding
*bond
,
2746 struct slave
*slave
)
2749 unsigned char *arp_ptr
;
2752 if (skb
->protocol
!= __cpu_to_be16(ETH_P_ARP
))
2755 read_lock(&bond
->lock
);
2757 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2758 bond
->dev
->name
, skb
->dev
->name
);
2760 if (!pskb_may_pull(skb
, arp_hdr_len(bond
->dev
)))
2764 if (arp
->ar_hln
!= bond
->dev
->addr_len
||
2765 skb
->pkt_type
== PACKET_OTHERHOST
||
2766 skb
->pkt_type
== PACKET_LOOPBACK
||
2767 arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
2768 arp
->ar_pro
!= htons(ETH_P_IP
) ||
2772 arp_ptr
= (unsigned char *)(arp
+ 1);
2773 arp_ptr
+= bond
->dev
->addr_len
;
2774 memcpy(&sip
, arp_ptr
, 4);
2775 arp_ptr
+= 4 + bond
->dev
->addr_len
;
2776 memcpy(&tip
, arp_ptr
, 4);
2778 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2779 bond
->dev
->name
, slave
->dev
->name
, bond_slave_state(slave
),
2780 bond
->params
.arp_validate
, slave_do_arp_validate(bond
, slave
),
2784 * Backup slaves won't see the ARP reply, but do come through
2785 * here for each ARP probe (so we swap the sip/tip to validate
2786 * the probe). In a "redundant switch, common router" type of
2787 * configuration, the ARP probe will (hopefully) travel from
2788 * the active, through one switch, the router, then the other
2789 * switch before reaching the backup.
2791 if (bond_is_active_slave(slave
))
2792 bond_validate_arp(bond
, slave
, sip
, tip
);
2794 bond_validate_arp(bond
, slave
, tip
, sip
);
2797 read_unlock(&bond
->lock
);
2801 * this function is called regularly to monitor each slave's link
2802 * ensuring that traffic is being sent and received when arp monitoring
2803 * is used in load-balancing mode. if the adapter has been dormant, then an
2804 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2805 * arp monitoring in active backup mode.
2807 void bond_loadbalance_arp_mon(struct work_struct
*work
)
2809 struct bonding
*bond
= container_of(work
, struct bonding
,
2811 struct slave
*slave
, *oldcurrent
;
2812 int do_failover
= 0;
2816 read_lock(&bond
->lock
);
2818 delta_in_ticks
= msecs_to_jiffies(bond
->params
.arp_interval
);
2820 if (bond
->kill_timers
)
2823 if (bond
->slave_cnt
== 0)
2826 read_lock(&bond
->curr_slave_lock
);
2827 oldcurrent
= bond
->curr_active_slave
;
2828 read_unlock(&bond
->curr_slave_lock
);
2830 /* see if any of the previous devices are up now (i.e. they have
2831 * xmt and rcv traffic). the curr_active_slave does not come into
2832 * the picture unless it is null. also, slave->jiffies is not needed
2833 * here because we send an arp on each slave and give a slave as
2834 * long as it needs to get the tx/rx within the delta.
2835 * TODO: what about up/down delay in arp mode? it wasn't here before
2838 bond_for_each_slave(bond
, slave
, i
) {
2839 unsigned long trans_start
= dev_trans_start(slave
->dev
);
2841 if (slave
->link
!= BOND_LINK_UP
) {
2842 if (time_in_range(jiffies
,
2843 trans_start
- delta_in_ticks
,
2844 trans_start
+ delta_in_ticks
) &&
2845 time_in_range(jiffies
,
2846 slave
->dev
->last_rx
- delta_in_ticks
,
2847 slave
->dev
->last_rx
+ delta_in_ticks
)) {
2849 slave
->link
= BOND_LINK_UP
;
2850 bond_set_active_slave(slave
);
2852 /* primary_slave has no meaning in round-robin
2853 * mode. the window of a slave being up and
2854 * curr_active_slave being null after enslaving
2858 pr_info("%s: link status definitely up for interface %s, ",
2863 pr_info("%s: interface %s is now up\n",
2869 /* slave->link == BOND_LINK_UP */
2871 /* not all switches will respond to an arp request
2872 * when the source ip is 0, so don't take the link down
2873 * if we don't know our ip yet
2875 if (!time_in_range(jiffies
,
2876 trans_start
- delta_in_ticks
,
2877 trans_start
+ 2 * delta_in_ticks
) ||
2878 !time_in_range(jiffies
,
2879 slave
->dev
->last_rx
- delta_in_ticks
,
2880 slave
->dev
->last_rx
+ 2 * delta_in_ticks
)) {
2882 slave
->link
= BOND_LINK_DOWN
;
2883 bond_set_backup_slave(slave
);
2885 if (slave
->link_failure_count
< UINT_MAX
)
2886 slave
->link_failure_count
++;
2888 pr_info("%s: interface %s is now down.\n",
2892 if (slave
== oldcurrent
)
2897 /* note: if switch is in round-robin mode, all links
2898 * must tx arp to ensure all links rx an arp - otherwise
2899 * links may oscillate or not come up at all; if switch is
2900 * in something like xor mode, there is nothing we can
2901 * do - all replies will be rx'ed on same link causing slaves
2902 * to be unstable during low/no traffic periods
2904 if (IS_UP(slave
->dev
))
2905 bond_arp_send_all(bond
, slave
);
2910 write_lock_bh(&bond
->curr_slave_lock
);
2912 bond_select_active_slave(bond
);
2914 write_unlock_bh(&bond
->curr_slave_lock
);
2915 unblock_netpoll_tx();
2919 if (bond
->params
.arp_interval
)
2920 queue_delayed_work(bond
->wq
, &bond
->arp_work
, delta_in_ticks
);
2922 read_unlock(&bond
->lock
);
2926 * Called to inspect slaves for active-backup mode ARP monitor link state
2927 * changes. Sets new_link in slaves to specify what action should take
2928 * place for the slave. Returns 0 if no changes are found, >0 if changes
2929 * to link states must be committed.
2931 * Called with bond->lock held for read.
2933 static int bond_ab_arp_inspect(struct bonding
*bond
, int delta_in_ticks
)
2935 struct slave
*slave
;
2937 unsigned long trans_start
;
2939 bond_for_each_slave(bond
, slave
, i
) {
2940 slave
->new_link
= BOND_LINK_NOCHANGE
;
2942 if (slave
->link
!= BOND_LINK_UP
) {
2943 if (time_in_range(jiffies
,
2944 slave_last_rx(bond
, slave
) - delta_in_ticks
,
2945 slave_last_rx(bond
, slave
) + delta_in_ticks
)) {
2947 slave
->new_link
= BOND_LINK_UP
;
2955 * Give slaves 2*delta after being enslaved or made
2956 * active. This avoids bouncing, as the last receive
2957 * times need a full ARP monitor cycle to be updated.
2959 if (time_in_range(jiffies
,
2960 slave
->jiffies
- delta_in_ticks
,
2961 slave
->jiffies
+ 2 * delta_in_ticks
))
2965 * Backup slave is down if:
2966 * - No current_arp_slave AND
2967 * - more than 3*delta since last receive AND
2968 * - the bond has an IP address
2970 * Note: a non-null current_arp_slave indicates
2971 * the curr_active_slave went down and we are
2972 * searching for a new one; under this condition
2973 * we only take the curr_active_slave down - this
2974 * gives each slave a chance to tx/rx traffic
2975 * before being taken out
2977 if (!bond_is_active_slave(slave
) &&
2978 !bond
->current_arp_slave
&&
2979 !time_in_range(jiffies
,
2980 slave_last_rx(bond
, slave
) - delta_in_ticks
,
2981 slave_last_rx(bond
, slave
) + 3 * delta_in_ticks
)) {
2983 slave
->new_link
= BOND_LINK_DOWN
;
2988 * Active slave is down if:
2989 * - more than 2*delta since transmitting OR
2990 * - (more than 2*delta since receive AND
2991 * the bond has an IP address)
2993 trans_start
= dev_trans_start(slave
->dev
);
2994 if (bond_is_active_slave(slave
) &&
2995 (!time_in_range(jiffies
,
2996 trans_start
- delta_in_ticks
,
2997 trans_start
+ 2 * delta_in_ticks
) ||
2998 !time_in_range(jiffies
,
2999 slave_last_rx(bond
, slave
) - delta_in_ticks
,
3000 slave_last_rx(bond
, slave
) + 2 * delta_in_ticks
))) {
3002 slave
->new_link
= BOND_LINK_DOWN
;
3011 * Called to commit link state changes noted by inspection step of
3012 * active-backup mode ARP monitor.
3014 * Called with RTNL and bond->lock for read.
3016 static void bond_ab_arp_commit(struct bonding
*bond
, int delta_in_ticks
)
3018 struct slave
*slave
;
3020 unsigned long trans_start
;
3022 bond_for_each_slave(bond
, slave
, i
) {
3023 switch (slave
->new_link
) {
3024 case BOND_LINK_NOCHANGE
:
3028 trans_start
= dev_trans_start(slave
->dev
);
3029 if ((!bond
->curr_active_slave
&&
3030 time_in_range(jiffies
,
3031 trans_start
- delta_in_ticks
,
3032 trans_start
+ delta_in_ticks
)) ||
3033 bond
->curr_active_slave
!= slave
) {
3034 slave
->link
= BOND_LINK_UP
;
3035 bond
->current_arp_slave
= NULL
;
3037 pr_info("%s: link status definitely up for interface %s.\n",
3038 bond
->dev
->name
, slave
->dev
->name
);
3040 if (!bond
->curr_active_slave
||
3041 (slave
== bond
->primary_slave
))
3048 case BOND_LINK_DOWN
:
3049 if (slave
->link_failure_count
< UINT_MAX
)
3050 slave
->link_failure_count
++;
3052 slave
->link
= BOND_LINK_DOWN
;
3053 bond_set_slave_inactive_flags(slave
);
3055 pr_info("%s: link status definitely down for interface %s, disabling it\n",
3056 bond
->dev
->name
, slave
->dev
->name
);
3058 if (slave
== bond
->curr_active_slave
) {
3059 bond
->current_arp_slave
= NULL
;
3066 pr_err("%s: impossible: new_link %d on slave %s\n",
3067 bond
->dev
->name
, slave
->new_link
,
3075 write_lock_bh(&bond
->curr_slave_lock
);
3076 bond_select_active_slave(bond
);
3077 write_unlock_bh(&bond
->curr_slave_lock
);
3078 unblock_netpoll_tx();
3081 bond_set_carrier(bond
);
3085 * Send ARP probes for active-backup mode ARP monitor.
3087 * Called with bond->lock held for read.
3089 static void bond_ab_arp_probe(struct bonding
*bond
)
3091 struct slave
*slave
;
3094 read_lock(&bond
->curr_slave_lock
);
3096 if (bond
->current_arp_slave
&& bond
->curr_active_slave
)
3097 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3098 bond
->current_arp_slave
->dev
->name
,
3099 bond
->curr_active_slave
->dev
->name
);
3101 if (bond
->curr_active_slave
) {
3102 bond_arp_send_all(bond
, bond
->curr_active_slave
);
3103 read_unlock(&bond
->curr_slave_lock
);
3107 read_unlock(&bond
->curr_slave_lock
);
3109 /* if we don't have a curr_active_slave, search for the next available
3110 * backup slave from the current_arp_slave and make it the candidate
3111 * for becoming the curr_active_slave
3114 if (!bond
->current_arp_slave
) {
3115 bond
->current_arp_slave
= bond
->first_slave
;
3116 if (!bond
->current_arp_slave
)
3120 bond_set_slave_inactive_flags(bond
->current_arp_slave
);
3122 /* search for next candidate */
3123 bond_for_each_slave_from(bond
, slave
, i
, bond
->current_arp_slave
->next
) {
3124 if (IS_UP(slave
->dev
)) {
3125 slave
->link
= BOND_LINK_BACK
;
3126 bond_set_slave_active_flags(slave
);
3127 bond_arp_send_all(bond
, slave
);
3128 slave
->jiffies
= jiffies
;
3129 bond
->current_arp_slave
= slave
;
3133 /* if the link state is up at this point, we
3134 * mark it down - this can happen if we have
3135 * simultaneous link failures and
3136 * reselect_active_interface doesn't make this
3137 * one the current slave so it is still marked
3138 * up when it is actually down
3140 if (slave
->link
== BOND_LINK_UP
) {
3141 slave
->link
= BOND_LINK_DOWN
;
3142 if (slave
->link_failure_count
< UINT_MAX
)
3143 slave
->link_failure_count
++;
3145 bond_set_slave_inactive_flags(slave
);
3147 pr_info("%s: backup interface %s is now down.\n",
3148 bond
->dev
->name
, slave
->dev
->name
);
3153 void bond_activebackup_arp_mon(struct work_struct
*work
)
3155 struct bonding
*bond
= container_of(work
, struct bonding
,
3159 read_lock(&bond
->lock
);
3161 if (bond
->kill_timers
)
3164 delta_in_ticks
= msecs_to_jiffies(bond
->params
.arp_interval
);
3166 if (bond
->slave_cnt
== 0)
3169 if (bond_ab_arp_inspect(bond
, delta_in_ticks
)) {
3170 read_unlock(&bond
->lock
);
3172 read_lock(&bond
->lock
);
3174 bond_ab_arp_commit(bond
, delta_in_ticks
);
3176 read_unlock(&bond
->lock
);
3178 read_lock(&bond
->lock
);
3181 bond_ab_arp_probe(bond
);
3184 if (bond
->params
.arp_interval
)
3185 queue_delayed_work(bond
->wq
, &bond
->arp_work
, delta_in_ticks
);
3187 read_unlock(&bond
->lock
);
3190 /*-------------------------- netdev event handling --------------------------*/
3193 * Change device name
3195 static int bond_event_changename(struct bonding
*bond
)
3197 bond_remove_proc_entry(bond
);
3198 bond_create_proc_entry(bond
);
3200 bond_debug_reregister(bond
);
3205 static int bond_master_netdev_event(unsigned long event
,
3206 struct net_device
*bond_dev
)
3208 struct bonding
*event_bond
= netdev_priv(bond_dev
);
3211 case NETDEV_CHANGENAME
:
3212 return bond_event_changename(event_bond
);
3220 static int bond_slave_netdev_event(unsigned long event
,
3221 struct net_device
*slave_dev
)
3223 struct net_device
*bond_dev
= slave_dev
->master
;
3224 struct bonding
*bond
= netdev_priv(bond_dev
);
3227 case NETDEV_UNREGISTER
:
3229 if (bond
->setup_by_slave
)
3230 bond_release_and_destroy(bond_dev
, slave_dev
);
3232 bond_release(bond_dev
, slave_dev
);
3236 if (bond
->params
.mode
== BOND_MODE_8023AD
|| bond_is_lb(bond
)) {
3237 struct slave
*slave
;
3239 slave
= bond_get_slave_by_dev(bond
, slave_dev
);
3241 u32 old_speed
= slave
->speed
;
3242 u8 old_duplex
= slave
->duplex
;
3244 bond_update_speed_duplex(slave
);
3246 if (bond_is_lb(bond
))
3249 if (old_speed
!= slave
->speed
)
3250 bond_3ad_adapter_speed_changed(slave
);
3251 if (old_duplex
!= slave
->duplex
)
3252 bond_3ad_adapter_duplex_changed(slave
);
3259 * ... Or is it this?
3262 case NETDEV_CHANGEMTU
:
3264 * TODO: Should slaves be allowed to
3265 * independently alter their MTU? For
3266 * an active-backup bond, slaves need
3267 * not be the same type of device, so
3268 * MTUs may vary. For other modes,
3269 * slaves arguably should have the
3270 * same MTUs. To do this, we'd need to
3271 * take over the slave's change_mtu
3272 * function for the duration of their
3276 case NETDEV_CHANGENAME
:
3278 * TODO: handle changing the primary's name
3281 case NETDEV_FEAT_CHANGE
:
3282 bond_compute_features(bond
);
3292 * bond_netdev_event: handle netdev notifier chain events.
3294 * This function receives events for the netdev chain. The caller (an
3295 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3296 * locks for us to safely manipulate the slave devices (RTNL lock,
3299 static int bond_netdev_event(struct notifier_block
*this,
3300 unsigned long event
, void *ptr
)
3302 struct net_device
*event_dev
= (struct net_device
*)ptr
;
3304 pr_debug("event_dev: %s, event: %lx\n",
3305 event_dev
? event_dev
->name
: "None",
3308 if (!(event_dev
->priv_flags
& IFF_BONDING
))
3311 if (event_dev
->flags
& IFF_MASTER
) {
3312 pr_debug("IFF_MASTER\n");
3313 return bond_master_netdev_event(event
, event_dev
);
3316 if (event_dev
->flags
& IFF_SLAVE
) {
3317 pr_debug("IFF_SLAVE\n");
3318 return bond_slave_netdev_event(event
, event_dev
);
3325 * bond_inetaddr_event: handle inetaddr notifier chain events.
3327 * We keep track of device IPs primarily to use as source addresses in
3328 * ARP monitor probes (rather than spewing out broadcasts all the time).
3330 * We track one IP for the main device (if it has one), plus one per VLAN.
3332 static int bond_inetaddr_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
3334 struct in_ifaddr
*ifa
= ptr
;
3335 struct net_device
*vlan_dev
, *event_dev
= ifa
->ifa_dev
->dev
;
3336 struct bond_net
*bn
= net_generic(dev_net(event_dev
), bond_net_id
);
3337 struct bonding
*bond
;
3338 struct vlan_entry
*vlan
;
3340 list_for_each_entry(bond
, &bn
->dev_list
, bond_list
) {
3341 if (bond
->dev
== event_dev
) {
3344 bond
->master_ip
= ifa
->ifa_local
;
3347 bond
->master_ip
= bond_glean_dev_ip(bond
->dev
);
3354 list_for_each_entry(vlan
, &bond
->vlan_list
, vlan_list
) {
3357 vlan_dev
= vlan_group_get_device(bond
->vlgrp
, vlan
->vlan_id
);
3358 if (vlan_dev
== event_dev
) {
3361 vlan
->vlan_ip
= ifa
->ifa_local
;
3365 bond_glean_dev_ip(vlan_dev
);
3376 static struct notifier_block bond_netdev_notifier
= {
3377 .notifier_call
= bond_netdev_event
,
3380 static struct notifier_block bond_inetaddr_notifier
= {
3381 .notifier_call
= bond_inetaddr_event
,
3384 /*---------------------------- Hashing Policies -----------------------------*/
3387 * Hash for the output device based upon layer 2 and layer 3 data. If
3388 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3390 static int bond_xmit_hash_policy_l23(struct sk_buff
*skb
, int count
)
3392 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3393 struct iphdr
*iph
= ip_hdr(skb
);
3395 if (skb
->protocol
== htons(ETH_P_IP
)) {
3396 return ((ntohl(iph
->saddr
^ iph
->daddr
) & 0xffff) ^
3397 (data
->h_dest
[5] ^ data
->h_source
[5])) % count
;
3400 return (data
->h_dest
[5] ^ data
->h_source
[5]) % count
;
3404 * Hash for the output device based upon layer 3 and layer 4 data. If
3405 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3406 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3408 static int bond_xmit_hash_policy_l34(struct sk_buff
*skb
, int count
)
3410 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3411 struct iphdr
*iph
= ip_hdr(skb
);
3412 __be16
*layer4hdr
= (__be16
*)((u32
*)iph
+ iph
->ihl
);
3415 if (skb
->protocol
== htons(ETH_P_IP
)) {
3416 if (!(iph
->frag_off
& htons(IP_MF
|IP_OFFSET
)) &&
3417 (iph
->protocol
== IPPROTO_TCP
||
3418 iph
->protocol
== IPPROTO_UDP
)) {
3419 layer4_xor
= ntohs((*layer4hdr
^ *(layer4hdr
+ 1)));
3421 return (layer4_xor
^
3422 ((ntohl(iph
->saddr
^ iph
->daddr
)) & 0xffff)) % count
;
3426 return (data
->h_dest
[5] ^ data
->h_source
[5]) % count
;
3430 * Hash for the output device based upon layer 2 data
3432 static int bond_xmit_hash_policy_l2(struct sk_buff
*skb
, int count
)
3434 struct ethhdr
*data
= (struct ethhdr
*)skb
->data
;
3436 return (data
->h_dest
[5] ^ data
->h_source
[5]) % count
;
3439 /*-------------------------- Device entry points ----------------------------*/
3441 static int bond_open(struct net_device
*bond_dev
)
3443 struct bonding
*bond
= netdev_priv(bond_dev
);
3445 bond
->kill_timers
= 0;
3447 INIT_DELAYED_WORK(&bond
->mcast_work
, bond_resend_igmp_join_requests_delayed
);
3449 if (bond_is_lb(bond
)) {
3450 /* bond_alb_initialize must be called before the timer
3453 if (bond_alb_initialize(bond
, (bond
->params
.mode
== BOND_MODE_ALB
))) {
3454 /* something went wrong - fail the open operation */
3458 INIT_DELAYED_WORK(&bond
->alb_work
, bond_alb_monitor
);
3459 queue_delayed_work(bond
->wq
, &bond
->alb_work
, 0);
3462 if (bond
->params
.miimon
) { /* link check interval, in milliseconds. */
3463 INIT_DELAYED_WORK(&bond
->mii_work
, bond_mii_monitor
);
3464 queue_delayed_work(bond
->wq
, &bond
->mii_work
, 0);
3467 if (bond
->params
.arp_interval
) { /* arp interval, in milliseconds. */
3468 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
)
3469 INIT_DELAYED_WORK(&bond
->arp_work
,
3470 bond_activebackup_arp_mon
);
3472 INIT_DELAYED_WORK(&bond
->arp_work
,
3473 bond_loadbalance_arp_mon
);
3475 queue_delayed_work(bond
->wq
, &bond
->arp_work
, 0);
3476 if (bond
->params
.arp_validate
)
3477 bond
->recv_probe
= bond_arp_rcv
;
3480 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
3481 INIT_DELAYED_WORK(&bond
->ad_work
, bond_3ad_state_machine_handler
);
3482 queue_delayed_work(bond
->wq
, &bond
->ad_work
, 0);
3483 /* register to receive LACPDUs */
3484 bond
->recv_probe
= bond_3ad_lacpdu_recv
;
3485 bond_3ad_initiate_agg_selection(bond
, 1);
3491 static int bond_close(struct net_device
*bond_dev
)
3493 struct bonding
*bond
= netdev_priv(bond_dev
);
3495 write_lock_bh(&bond
->lock
);
3497 /* signal timers not to re-arm */
3498 bond
->kill_timers
= 1;
3500 write_unlock_bh(&bond
->lock
);
3502 if (bond
->params
.miimon
) { /* link check interval, in milliseconds. */
3503 cancel_delayed_work(&bond
->mii_work
);
3506 if (bond
->params
.arp_interval
) { /* arp interval, in milliseconds. */
3507 cancel_delayed_work(&bond
->arp_work
);
3510 switch (bond
->params
.mode
) {
3511 case BOND_MODE_8023AD
:
3512 cancel_delayed_work(&bond
->ad_work
);
3516 cancel_delayed_work(&bond
->alb_work
);
3522 if (delayed_work_pending(&bond
->mcast_work
))
3523 cancel_delayed_work(&bond
->mcast_work
);
3525 if (bond_is_lb(bond
)) {
3526 /* Must be called only after all
3527 * slaves have been released
3529 bond_alb_deinitialize(bond
);
3531 bond
->recv_probe
= NULL
;
3536 static struct rtnl_link_stats64
*bond_get_stats(struct net_device
*bond_dev
,
3537 struct rtnl_link_stats64
*stats
)
3539 struct bonding
*bond
= netdev_priv(bond_dev
);
3540 struct rtnl_link_stats64 temp
;
3541 struct slave
*slave
;
3544 memset(stats
, 0, sizeof(*stats
));
3546 read_lock_bh(&bond
->lock
);
3548 bond_for_each_slave(bond
, slave
, i
) {
3549 const struct rtnl_link_stats64
*sstats
=
3550 dev_get_stats(slave
->dev
, &temp
);
3552 stats
->rx_packets
+= sstats
->rx_packets
;
3553 stats
->rx_bytes
+= sstats
->rx_bytes
;
3554 stats
->rx_errors
+= sstats
->rx_errors
;
3555 stats
->rx_dropped
+= sstats
->rx_dropped
;
3557 stats
->tx_packets
+= sstats
->tx_packets
;
3558 stats
->tx_bytes
+= sstats
->tx_bytes
;
3559 stats
->tx_errors
+= sstats
->tx_errors
;
3560 stats
->tx_dropped
+= sstats
->tx_dropped
;
3562 stats
->multicast
+= sstats
->multicast
;
3563 stats
->collisions
+= sstats
->collisions
;
3565 stats
->rx_length_errors
+= sstats
->rx_length_errors
;
3566 stats
->rx_over_errors
+= sstats
->rx_over_errors
;
3567 stats
->rx_crc_errors
+= sstats
->rx_crc_errors
;
3568 stats
->rx_frame_errors
+= sstats
->rx_frame_errors
;
3569 stats
->rx_fifo_errors
+= sstats
->rx_fifo_errors
;
3570 stats
->rx_missed_errors
+= sstats
->rx_missed_errors
;
3572 stats
->tx_aborted_errors
+= sstats
->tx_aborted_errors
;
3573 stats
->tx_carrier_errors
+= sstats
->tx_carrier_errors
;
3574 stats
->tx_fifo_errors
+= sstats
->tx_fifo_errors
;
3575 stats
->tx_heartbeat_errors
+= sstats
->tx_heartbeat_errors
;
3576 stats
->tx_window_errors
+= sstats
->tx_window_errors
;
3579 read_unlock_bh(&bond
->lock
);
3584 static int bond_do_ioctl(struct net_device
*bond_dev
, struct ifreq
*ifr
, int cmd
)
3586 struct net_device
*slave_dev
= NULL
;
3587 struct ifbond k_binfo
;
3588 struct ifbond __user
*u_binfo
= NULL
;
3589 struct ifslave k_sinfo
;
3590 struct ifslave __user
*u_sinfo
= NULL
;
3591 struct mii_ioctl_data
*mii
= NULL
;
3594 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev
->name
, cmd
);
3606 * We do this again just in case we were called by SIOCGMIIREG
3607 * instead of SIOCGMIIPHY.
3614 if (mii
->reg_num
== 1) {
3615 struct bonding
*bond
= netdev_priv(bond_dev
);
3617 read_lock(&bond
->lock
);
3618 read_lock(&bond
->curr_slave_lock
);
3619 if (netif_carrier_ok(bond
->dev
))
3620 mii
->val_out
= BMSR_LSTATUS
;
3622 read_unlock(&bond
->curr_slave_lock
);
3623 read_unlock(&bond
->lock
);
3627 case BOND_INFO_QUERY_OLD
:
3628 case SIOCBONDINFOQUERY
:
3629 u_binfo
= (struct ifbond __user
*)ifr
->ifr_data
;
3631 if (copy_from_user(&k_binfo
, u_binfo
, sizeof(ifbond
)))
3634 res
= bond_info_query(bond_dev
, &k_binfo
);
3636 copy_to_user(u_binfo
, &k_binfo
, sizeof(ifbond
)))
3640 case BOND_SLAVE_INFO_QUERY_OLD
:
3641 case SIOCBONDSLAVEINFOQUERY
:
3642 u_sinfo
= (struct ifslave __user
*)ifr
->ifr_data
;
3644 if (copy_from_user(&k_sinfo
, u_sinfo
, sizeof(ifslave
)))
3647 res
= bond_slave_info_query(bond_dev
, &k_sinfo
);
3649 copy_to_user(u_sinfo
, &k_sinfo
, sizeof(ifslave
)))
3658 if (!capable(CAP_NET_ADMIN
))
3661 slave_dev
= dev_get_by_name(dev_net(bond_dev
), ifr
->ifr_slave
);
3663 pr_debug("slave_dev=%p:\n", slave_dev
);
3668 pr_debug("slave_dev->name=%s:\n", slave_dev
->name
);
3670 case BOND_ENSLAVE_OLD
:
3671 case SIOCBONDENSLAVE
:
3672 res
= bond_enslave(bond_dev
, slave_dev
);
3674 case BOND_RELEASE_OLD
:
3675 case SIOCBONDRELEASE
:
3676 res
= bond_release(bond_dev
, slave_dev
);
3678 case BOND_SETHWADDR_OLD
:
3679 case SIOCBONDSETHWADDR
:
3680 res
= bond_sethwaddr(bond_dev
, slave_dev
);
3682 case BOND_CHANGE_ACTIVE_OLD
:
3683 case SIOCBONDCHANGEACTIVE
:
3684 res
= bond_ioctl_change_active(bond_dev
, slave_dev
);
3696 static bool bond_addr_in_mc_list(unsigned char *addr
,
3697 struct netdev_hw_addr_list
*list
,
3700 struct netdev_hw_addr
*ha
;
3702 netdev_hw_addr_list_for_each(ha
, list
)
3703 if (!memcmp(ha
->addr
, addr
, addrlen
))
3709 static void bond_set_multicast_list(struct net_device
*bond_dev
)
3711 struct bonding
*bond
= netdev_priv(bond_dev
);
3712 struct netdev_hw_addr
*ha
;
3716 * Do promisc before checking multicast_mode
3718 if ((bond_dev
->flags
& IFF_PROMISC
) && !(bond
->flags
& IFF_PROMISC
))
3720 * FIXME: Need to handle the error when one of the multi-slaves
3723 bond_set_promiscuity(bond
, 1);
3726 if (!(bond_dev
->flags
& IFF_PROMISC
) && (bond
->flags
& IFF_PROMISC
))
3727 bond_set_promiscuity(bond
, -1);
3730 /* set allmulti flag to slaves */
3731 if ((bond_dev
->flags
& IFF_ALLMULTI
) && !(bond
->flags
& IFF_ALLMULTI
))
3733 * FIXME: Need to handle the error when one of the multi-slaves
3736 bond_set_allmulti(bond
, 1);
3739 if (!(bond_dev
->flags
& IFF_ALLMULTI
) && (bond
->flags
& IFF_ALLMULTI
))
3740 bond_set_allmulti(bond
, -1);
3743 read_lock(&bond
->lock
);
3745 bond
->flags
= bond_dev
->flags
;
3747 /* looking for addresses to add to slaves' mc list */
3748 netdev_for_each_mc_addr(ha
, bond_dev
) {
3749 found
= bond_addr_in_mc_list(ha
->addr
, &bond
->mc_list
,
3750 bond_dev
->addr_len
);
3752 bond_mc_add(bond
, ha
->addr
);
3755 /* looking for addresses to delete from slaves' list */
3756 netdev_hw_addr_list_for_each(ha
, &bond
->mc_list
) {
3757 found
= bond_addr_in_mc_list(ha
->addr
, &bond_dev
->mc
,
3758 bond_dev
->addr_len
);
3760 bond_mc_del(bond
, ha
->addr
);
3763 /* save master's multicast list */
3764 __hw_addr_flush(&bond
->mc_list
);
3765 __hw_addr_add_multiple(&bond
->mc_list
, &bond_dev
->mc
,
3766 bond_dev
->addr_len
, NETDEV_HW_ADDR_T_MULTICAST
);
3768 read_unlock(&bond
->lock
);
3771 static int bond_neigh_setup(struct net_device
*dev
, struct neigh_parms
*parms
)
3773 struct bonding
*bond
= netdev_priv(dev
);
3774 struct slave
*slave
= bond
->first_slave
;
3777 const struct net_device_ops
*slave_ops
3778 = slave
->dev
->netdev_ops
;
3779 if (slave_ops
->ndo_neigh_setup
)
3780 return slave_ops
->ndo_neigh_setup(slave
->dev
, parms
);
3786 * Change the MTU of all of a master's slaves to match the master
3788 static int bond_change_mtu(struct net_device
*bond_dev
, int new_mtu
)
3790 struct bonding
*bond
= netdev_priv(bond_dev
);
3791 struct slave
*slave
, *stop_at
;
3795 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond
,
3796 (bond_dev
? bond_dev
->name
: "None"), new_mtu
);
3798 /* Can't hold bond->lock with bh disabled here since
3799 * some base drivers panic. On the other hand we can't
3800 * hold bond->lock without bh disabled because we'll
3801 * deadlock. The only solution is to rely on the fact
3802 * that we're under rtnl_lock here, and the slaves
3803 * list won't change. This doesn't solve the problem
3804 * of setting the slave's MTU while it is
3805 * transmitting, but the assumption is that the base
3806 * driver can handle that.
3808 * TODO: figure out a way to safely iterate the slaves
3809 * list, but without holding a lock around the actual
3810 * call to the base driver.
3813 bond_for_each_slave(bond
, slave
, i
) {
3814 pr_debug("s %p s->p %p c_m %p\n",
3817 slave
->dev
->netdev_ops
->ndo_change_mtu
);
3819 res
= dev_set_mtu(slave
->dev
, new_mtu
);
3822 /* If we failed to set the slave's mtu to the new value
3823 * we must abort the operation even in ACTIVE_BACKUP
3824 * mode, because if we allow the backup slaves to have
3825 * different mtu values than the active slave we'll
3826 * need to change their mtu when doing a failover. That
3827 * means changing their mtu from timer context, which
3828 * is probably not a good idea.
3830 pr_debug("err %d %s\n", res
, slave
->dev
->name
);
3835 bond_dev
->mtu
= new_mtu
;
3840 /* unwind from head to the slave that failed */
3842 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
3845 tmp_res
= dev_set_mtu(slave
->dev
, bond_dev
->mtu
);
3847 pr_debug("unwind err %d dev %s\n",
3848 tmp_res
, slave
->dev
->name
);
3858 * Note that many devices must be down to change the HW address, and
3859 * downing the master releases all slaves. We can make bonds full of
3860 * bonding devices to test this, however.
3862 static int bond_set_mac_address(struct net_device
*bond_dev
, void *addr
)
3864 struct bonding
*bond
= netdev_priv(bond_dev
);
3865 struct sockaddr
*sa
= addr
, tmp_sa
;
3866 struct slave
*slave
, *stop_at
;
3870 if (bond
->params
.mode
== BOND_MODE_ALB
)
3871 return bond_alb_set_mac_address(bond_dev
, addr
);
3874 pr_debug("bond=%p, name=%s\n",
3875 bond
, bond_dev
? bond_dev
->name
: "None");
3878 * If fail_over_mac is set to active, do nothing and return
3879 * success. Returning an error causes ifenslave to fail.
3881 if (bond
->params
.fail_over_mac
== BOND_FOM_ACTIVE
)
3884 if (!is_valid_ether_addr(sa
->sa_data
))
3885 return -EADDRNOTAVAIL
;
3887 /* Can't hold bond->lock with bh disabled here since
3888 * some base drivers panic. On the other hand we can't
3889 * hold bond->lock without bh disabled because we'll
3890 * deadlock. The only solution is to rely on the fact
3891 * that we're under rtnl_lock here, and the slaves
3892 * list won't change. This doesn't solve the problem
3893 * of setting the slave's hw address while it is
3894 * transmitting, but the assumption is that the base
3895 * driver can handle that.
3897 * TODO: figure out a way to safely iterate the slaves
3898 * list, but without holding a lock around the actual
3899 * call to the base driver.
3902 bond_for_each_slave(bond
, slave
, i
) {
3903 const struct net_device_ops
*slave_ops
= slave
->dev
->netdev_ops
;
3904 pr_debug("slave %p %s\n", slave
, slave
->dev
->name
);
3906 if (slave_ops
->ndo_set_mac_address
== NULL
) {
3908 pr_debug("EOPNOTSUPP %s\n", slave
->dev
->name
);
3912 res
= dev_set_mac_address(slave
->dev
, addr
);
3914 /* TODO: consider downing the slave
3916 * User should expect communications
3917 * breakage anyway until ARP finish
3920 pr_debug("err %d %s\n", res
, slave
->dev
->name
);
3926 memcpy(bond_dev
->dev_addr
, sa
->sa_data
, bond_dev
->addr_len
);
3930 memcpy(tmp_sa
.sa_data
, bond_dev
->dev_addr
, bond_dev
->addr_len
);
3931 tmp_sa
.sa_family
= bond_dev
->type
;
3933 /* unwind from head to the slave that failed */
3935 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
3938 tmp_res
= dev_set_mac_address(slave
->dev
, &tmp_sa
);
3940 pr_debug("unwind err %d dev %s\n",
3941 tmp_res
, slave
->dev
->name
);
3948 static int bond_xmit_roundrobin(struct sk_buff
*skb
, struct net_device
*bond_dev
)
3950 struct bonding
*bond
= netdev_priv(bond_dev
);
3951 struct slave
*slave
, *start_at
;
3952 int i
, slave_no
, res
= 1;
3953 struct iphdr
*iph
= ip_hdr(skb
);
3955 read_lock(&bond
->lock
);
3957 if (!BOND_IS_OK(bond
))
3960 * Start with the curr_active_slave that joined the bond as the
3961 * default for sending IGMP traffic. For failover purposes one
3962 * needs to maintain some consistency for the interface that will
3963 * send the join/membership reports. The curr_active_slave found
3964 * will send all of this type of traffic.
3966 if ((iph
->protocol
== IPPROTO_IGMP
) &&
3967 (skb
->protocol
== htons(ETH_P_IP
))) {
3969 read_lock(&bond
->curr_slave_lock
);
3970 slave
= bond
->curr_active_slave
;
3971 read_unlock(&bond
->curr_slave_lock
);
3977 * Concurrent TX may collide on rr_tx_counter; we accept
3978 * that as being rare enough not to justify using an
3981 slave_no
= bond
->rr_tx_counter
++ % bond
->slave_cnt
;
3983 bond_for_each_slave(bond
, slave
, i
) {
3991 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
3992 if (IS_UP(slave
->dev
) &&
3993 (slave
->link
== BOND_LINK_UP
) &&
3994 bond_is_active_slave(slave
)) {
3995 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4002 /* no suitable interface, frame not sent */
4005 read_unlock(&bond
->lock
);
4006 return NETDEV_TX_OK
;
4011 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4012 * the bond has a usable interface.
4014 static int bond_xmit_activebackup(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4016 struct bonding
*bond
= netdev_priv(bond_dev
);
4019 read_lock(&bond
->lock
);
4020 read_lock(&bond
->curr_slave_lock
);
4022 if (!BOND_IS_OK(bond
))
4025 if (!bond
->curr_active_slave
)
4028 res
= bond_dev_queue_xmit(bond
, skb
, bond
->curr_active_slave
->dev
);
4032 /* no suitable interface, frame not sent */
4035 read_unlock(&bond
->curr_slave_lock
);
4036 read_unlock(&bond
->lock
);
4037 return NETDEV_TX_OK
;
4041 * In bond_xmit_xor() , we determine the output device by using a pre-
4042 * determined xmit_hash_policy(), If the selected device is not enabled,
4043 * find the next active slave.
4045 static int bond_xmit_xor(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4047 struct bonding
*bond
= netdev_priv(bond_dev
);
4048 struct slave
*slave
, *start_at
;
4053 read_lock(&bond
->lock
);
4055 if (!BOND_IS_OK(bond
))
4058 slave_no
= bond
->xmit_hash_policy(skb
, bond
->slave_cnt
);
4060 bond_for_each_slave(bond
, slave
, i
) {
4068 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4069 if (IS_UP(slave
->dev
) &&
4070 (slave
->link
== BOND_LINK_UP
) &&
4071 bond_is_active_slave(slave
)) {
4072 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4079 /* no suitable interface, frame not sent */
4082 read_unlock(&bond
->lock
);
4083 return NETDEV_TX_OK
;
4087 * in broadcast mode, we send everything to all usable interfaces.
4089 static int bond_xmit_broadcast(struct sk_buff
*skb
, struct net_device
*bond_dev
)
4091 struct bonding
*bond
= netdev_priv(bond_dev
);
4092 struct slave
*slave
, *start_at
;
4093 struct net_device
*tx_dev
= NULL
;
4097 read_lock(&bond
->lock
);
4099 if (!BOND_IS_OK(bond
))
4102 read_lock(&bond
->curr_slave_lock
);
4103 start_at
= bond
->curr_active_slave
;
4104 read_unlock(&bond
->curr_slave_lock
);
4109 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
4110 if (IS_UP(slave
->dev
) &&
4111 (slave
->link
== BOND_LINK_UP
) &&
4112 bond_is_active_slave(slave
)) {
4114 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
4116 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
4121 res
= bond_dev_queue_xmit(bond
, skb2
, tx_dev
);
4123 dev_kfree_skb(skb2
);
4127 tx_dev
= slave
->dev
;
4132 res
= bond_dev_queue_xmit(bond
, skb
, tx_dev
);
4136 /* no suitable interface, frame not sent */
4139 /* frame sent to all suitable interfaces */
4140 read_unlock(&bond
->lock
);
4141 return NETDEV_TX_OK
;
4144 /*------------------------- Device initialization ---------------------------*/
4146 static void bond_set_xmit_hash_policy(struct bonding
*bond
)
4148 switch (bond
->params
.xmit_policy
) {
4149 case BOND_XMIT_POLICY_LAYER23
:
4150 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l23
;
4152 case BOND_XMIT_POLICY_LAYER34
:
4153 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l34
;
4155 case BOND_XMIT_POLICY_LAYER2
:
4157 bond
->xmit_hash_policy
= bond_xmit_hash_policy_l2
;
4163 * Lookup the slave that corresponds to a qid
4165 static inline int bond_slave_override(struct bonding
*bond
,
4166 struct sk_buff
*skb
)
4169 struct slave
*slave
= NULL
;
4170 struct slave
*check_slave
;
4172 read_lock(&bond
->lock
);
4174 if (!BOND_IS_OK(bond
) || !skb
->queue_mapping
)
4177 /* Find out if any slaves have the same mapping as this skb. */
4178 bond_for_each_slave(bond
, check_slave
, i
) {
4179 if (check_slave
->queue_id
== skb
->queue_mapping
) {
4180 slave
= check_slave
;
4185 /* If the slave isn't UP, use default transmit policy. */
4186 if (slave
&& slave
->queue_id
&& IS_UP(slave
->dev
) &&
4187 (slave
->link
== BOND_LINK_UP
)) {
4188 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
4192 read_unlock(&bond
->lock
);
4196 static u16
bond_select_queue(struct net_device
*dev
, struct sk_buff
*skb
)
4199 * This helper function exists to help dev_pick_tx get the correct
4200 * destination queue. Using a helper function skips a call to
4201 * skb_tx_hash and will put the skbs in the queue we expect on their
4202 * way down to the bonding driver.
4204 u16 txq
= skb_rx_queue_recorded(skb
) ? skb_get_rx_queue(skb
) : 0;
4206 if (unlikely(txq
>= dev
->real_num_tx_queues
)) {
4208 txq
-= dev
->real_num_tx_queues
;
4209 } while (txq
>= dev
->real_num_tx_queues
);
4214 static netdev_tx_t
bond_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
4216 struct bonding
*bond
= netdev_priv(dev
);
4219 * If we risk deadlock from transmitting this in the
4220 * netpoll path, tell netpoll to queue the frame for later tx
4222 if (is_netpoll_tx_blocked(dev
))
4223 return NETDEV_TX_BUSY
;
4225 if (TX_QUEUE_OVERRIDE(bond
->params
.mode
)) {
4226 if (!bond_slave_override(bond
, skb
))
4227 return NETDEV_TX_OK
;
4230 switch (bond
->params
.mode
) {
4231 case BOND_MODE_ROUNDROBIN
:
4232 return bond_xmit_roundrobin(skb
, dev
);
4233 case BOND_MODE_ACTIVEBACKUP
:
4234 return bond_xmit_activebackup(skb
, dev
);
4236 return bond_xmit_xor(skb
, dev
);
4237 case BOND_MODE_BROADCAST
:
4238 return bond_xmit_broadcast(skb
, dev
);
4239 case BOND_MODE_8023AD
:
4240 return bond_3ad_xmit_xor(skb
, dev
);
4243 return bond_alb_xmit(skb
, dev
);
4245 /* Should never happen, mode already checked */
4246 pr_err("%s: Error: Unknown bonding mode %d\n",
4247 dev
->name
, bond
->params
.mode
);
4250 return NETDEV_TX_OK
;
4256 * set bond mode specific net device operations
4258 void bond_set_mode_ops(struct bonding
*bond
, int mode
)
4260 struct net_device
*bond_dev
= bond
->dev
;
4263 case BOND_MODE_ROUNDROBIN
:
4265 case BOND_MODE_ACTIVEBACKUP
:
4268 bond_set_xmit_hash_policy(bond
);
4270 case BOND_MODE_BROADCAST
:
4272 case BOND_MODE_8023AD
:
4273 bond_set_xmit_hash_policy(bond
);
4280 /* Should never happen, mode already checked */
4281 pr_err("%s: Error: Unknown bonding mode %d\n",
4282 bond_dev
->name
, mode
);
4287 static void bond_ethtool_get_drvinfo(struct net_device
*bond_dev
,
4288 struct ethtool_drvinfo
*drvinfo
)
4290 strncpy(drvinfo
->driver
, DRV_NAME
, 32);
4291 strncpy(drvinfo
->version
, DRV_VERSION
, 32);
4292 snprintf(drvinfo
->fw_version
, 32, "%d", BOND_ABI_VERSION
);
4295 static const struct ethtool_ops bond_ethtool_ops
= {
4296 .get_drvinfo
= bond_ethtool_get_drvinfo
,
4297 .get_link
= ethtool_op_get_link
,
4298 .get_tx_csum
= ethtool_op_get_tx_csum
,
4299 .get_sg
= ethtool_op_get_sg
,
4300 .get_tso
= ethtool_op_get_tso
,
4301 .get_ufo
= ethtool_op_get_ufo
,
4302 .get_flags
= ethtool_op_get_flags
,
4305 static const struct net_device_ops bond_netdev_ops
= {
4306 .ndo_init
= bond_init
,
4307 .ndo_uninit
= bond_uninit
,
4308 .ndo_open
= bond_open
,
4309 .ndo_stop
= bond_close
,
4310 .ndo_start_xmit
= bond_start_xmit
,
4311 .ndo_select_queue
= bond_select_queue
,
4312 .ndo_get_stats64
= bond_get_stats
,
4313 .ndo_do_ioctl
= bond_do_ioctl
,
4314 .ndo_set_multicast_list
= bond_set_multicast_list
,
4315 .ndo_change_mtu
= bond_change_mtu
,
4316 .ndo_set_mac_address
= bond_set_mac_address
,
4317 .ndo_neigh_setup
= bond_neigh_setup
,
4318 .ndo_vlan_rx_register
= bond_vlan_rx_register
,
4319 .ndo_vlan_rx_add_vid
= bond_vlan_rx_add_vid
,
4320 .ndo_vlan_rx_kill_vid
= bond_vlan_rx_kill_vid
,
4321 #ifdef CONFIG_NET_POLL_CONTROLLER
4322 .ndo_netpoll_setup
= bond_netpoll_setup
,
4323 .ndo_netpoll_cleanup
= bond_netpoll_cleanup
,
4324 .ndo_poll_controller
= bond_poll_controller
,
4326 .ndo_add_slave
= bond_enslave
,
4327 .ndo_del_slave
= bond_release
,
4330 static void bond_destructor(struct net_device
*bond_dev
)
4332 struct bonding
*bond
= netdev_priv(bond_dev
);
4334 destroy_workqueue(bond
->wq
);
4335 free_netdev(bond_dev
);
4338 static void bond_setup(struct net_device
*bond_dev
)
4340 struct bonding
*bond
= netdev_priv(bond_dev
);
4342 /* initialize rwlocks */
4343 rwlock_init(&bond
->lock
);
4344 rwlock_init(&bond
->curr_slave_lock
);
4346 bond
->params
= bonding_defaults
;
4348 /* Initialize pointers */
4349 bond
->dev
= bond_dev
;
4350 INIT_LIST_HEAD(&bond
->vlan_list
);
4352 /* Initialize the device entry points */
4353 ether_setup(bond_dev
);
4354 bond_dev
->netdev_ops
= &bond_netdev_ops
;
4355 bond_dev
->ethtool_ops
= &bond_ethtool_ops
;
4356 bond_set_mode_ops(bond
, bond
->params
.mode
);
4358 bond_dev
->destructor
= bond_destructor
;
4360 /* Initialize the device options */
4361 bond_dev
->tx_queue_len
= 0;
4362 bond_dev
->flags
|= IFF_MASTER
|IFF_MULTICAST
;
4363 bond_dev
->priv_flags
|= IFF_BONDING
;
4364 bond_dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
4366 /* At first, we block adding VLANs. That's the only way to
4367 * prevent problems that occur when adding VLANs over an
4368 * empty bond. The block will be removed once non-challenged
4369 * slaves are enslaved.
4371 bond_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
4373 /* don't acquire bond device's netif_tx_lock when
4375 bond_dev
->features
|= NETIF_F_LLTX
;
4377 /* By default, we declare the bond to be fully
4378 * VLAN hardware accelerated capable. Special
4379 * care is taken in the various xmit functions
4380 * when there are slaves that are not hw accel
4383 bond_dev
->features
|= (NETIF_F_HW_VLAN_TX
|
4384 NETIF_F_HW_VLAN_RX
|
4385 NETIF_F_HW_VLAN_FILTER
);
4387 /* By default, we enable GRO on bonding devices.
4388 * Actual support requires lowlevel drivers are GRO ready.
4390 bond_dev
->features
|= NETIF_F_GRO
;
4393 static void bond_work_cancel_all(struct bonding
*bond
)
4395 write_lock_bh(&bond
->lock
);
4396 bond
->kill_timers
= 1;
4397 write_unlock_bh(&bond
->lock
);
4399 if (bond
->params
.miimon
&& delayed_work_pending(&bond
->mii_work
))
4400 cancel_delayed_work(&bond
->mii_work
);
4402 if (bond
->params
.arp_interval
&& delayed_work_pending(&bond
->arp_work
))
4403 cancel_delayed_work(&bond
->arp_work
);
4405 if (bond
->params
.mode
== BOND_MODE_ALB
&&
4406 delayed_work_pending(&bond
->alb_work
))
4407 cancel_delayed_work(&bond
->alb_work
);
4409 if (bond
->params
.mode
== BOND_MODE_8023AD
&&
4410 delayed_work_pending(&bond
->ad_work
))
4411 cancel_delayed_work(&bond
->ad_work
);
4413 if (delayed_work_pending(&bond
->mcast_work
))
4414 cancel_delayed_work(&bond
->mcast_work
);
4418 * Destroy a bonding device.
4419 * Must be under rtnl_lock when this function is called.
4421 static void bond_uninit(struct net_device
*bond_dev
)
4423 struct bonding
*bond
= netdev_priv(bond_dev
);
4424 struct vlan_entry
*vlan
, *tmp
;
4426 bond_netpoll_cleanup(bond_dev
);
4428 /* Release the bonded slaves */
4429 bond_release_all(bond_dev
);
4431 list_del(&bond
->bond_list
);
4433 bond_work_cancel_all(bond
);
4435 bond_remove_proc_entry(bond
);
4437 bond_debug_unregister(bond
);
4439 __hw_addr_flush(&bond
->mc_list
);
4441 list_for_each_entry_safe(vlan
, tmp
, &bond
->vlan_list
, vlan_list
) {
4442 list_del(&vlan
->vlan_list
);
4447 /*------------------------- Module initialization ---------------------------*/
4450 * Convert string input module parms. Accept either the
4451 * number of the mode or its string name. A bit complicated because
4452 * some mode names are substrings of other names, and calls from sysfs
4453 * may have whitespace in the name (trailing newlines, for example).
4455 int bond_parse_parm(const char *buf
, const struct bond_parm_tbl
*tbl
)
4457 int modeint
= -1, i
, rv
;
4458 char *p
, modestr
[BOND_MAX_MODENAME_LEN
+ 1] = { 0, };
4460 for (p
= (char *)buf
; *p
; p
++)
4461 if (!(isdigit(*p
) || isspace(*p
)))
4465 rv
= sscanf(buf
, "%20s", modestr
);
4467 rv
= sscanf(buf
, "%d", &modeint
);
4472 for (i
= 0; tbl
[i
].modename
; i
++) {
4473 if (modeint
== tbl
[i
].mode
)
4475 if (strcmp(modestr
, tbl
[i
].modename
) == 0)
4482 static int bond_check_params(struct bond_params
*params
)
4484 int arp_validate_value
, fail_over_mac_value
, primary_reselect_value
;
4487 * Convert string parameters.
4490 bond_mode
= bond_parse_parm(mode
, bond_mode_tbl
);
4491 if (bond_mode
== -1) {
4492 pr_err("Error: Invalid bonding mode \"%s\"\n",
4493 mode
== NULL
? "NULL" : mode
);
4498 if (xmit_hash_policy
) {
4499 if ((bond_mode
!= BOND_MODE_XOR
) &&
4500 (bond_mode
!= BOND_MODE_8023AD
)) {
4501 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4502 bond_mode_name(bond_mode
));
4504 xmit_hashtype
= bond_parse_parm(xmit_hash_policy
,
4506 if (xmit_hashtype
== -1) {
4507 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4508 xmit_hash_policy
== NULL
? "NULL" :
4516 if (bond_mode
!= BOND_MODE_8023AD
) {
4517 pr_info("lacp_rate param is irrelevant in mode %s\n",
4518 bond_mode_name(bond_mode
));
4520 lacp_fast
= bond_parse_parm(lacp_rate
, bond_lacp_tbl
);
4521 if (lacp_fast
== -1) {
4522 pr_err("Error: Invalid lacp rate \"%s\"\n",
4523 lacp_rate
== NULL
? "NULL" : lacp_rate
);
4530 params
->ad_select
= bond_parse_parm(ad_select
, ad_select_tbl
);
4531 if (params
->ad_select
== -1) {
4532 pr_err("Error: Invalid ad_select \"%s\"\n",
4533 ad_select
== NULL
? "NULL" : ad_select
);
4537 if (bond_mode
!= BOND_MODE_8023AD
) {
4538 pr_warning("ad_select param only affects 802.3ad mode\n");
4541 params
->ad_select
= BOND_AD_STABLE
;
4544 if (max_bonds
< 0) {
4545 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4546 max_bonds
, 0, INT_MAX
, BOND_DEFAULT_MAX_BONDS
);
4547 max_bonds
= BOND_DEFAULT_MAX_BONDS
;
4551 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4552 miimon
, INT_MAX
, BOND_LINK_MON_INTERV
);
4553 miimon
= BOND_LINK_MON_INTERV
;
4557 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4562 if (downdelay
< 0) {
4563 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4564 downdelay
, INT_MAX
);
4568 if ((use_carrier
!= 0) && (use_carrier
!= 1)) {
4569 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4574 /* reset values for 802.3ad */
4575 if (bond_mode
== BOND_MODE_8023AD
) {
4577 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
4578 pr_warning("Forcing miimon to 100msec\n");
4583 if (tx_queues
< 1 || tx_queues
> 255) {
4584 pr_warning("Warning: tx_queues (%d) should be between "
4585 "1 and 255, resetting to %d\n",
4586 tx_queues
, BOND_DEFAULT_TX_QUEUES
);
4587 tx_queues
= BOND_DEFAULT_TX_QUEUES
;
4590 if ((all_slaves_active
!= 0) && (all_slaves_active
!= 1)) {
4591 pr_warning("Warning: all_slaves_active module parameter (%d), "
4592 "not of valid value (0/1), so it was set to "
4593 "0\n", all_slaves_active
);
4594 all_slaves_active
= 0;
4597 if (resend_igmp
< 0 || resend_igmp
> 255) {
4598 pr_warning("Warning: resend_igmp (%d) should be between "
4599 "0 and 255, resetting to %d\n",
4600 resend_igmp
, BOND_DEFAULT_RESEND_IGMP
);
4601 resend_igmp
= BOND_DEFAULT_RESEND_IGMP
;
4604 /* reset values for TLB/ALB */
4605 if ((bond_mode
== BOND_MODE_TLB
) ||
4606 (bond_mode
== BOND_MODE_ALB
)) {
4608 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure and link speed which are essential for TLB/ALB load balancing\n");
4609 pr_warning("Forcing miimon to 100msec\n");
4614 if (bond_mode
== BOND_MODE_ALB
) {
4615 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
4620 if (updelay
|| downdelay
) {
4621 /* just warn the user the up/down delay will have
4622 * no effect since miimon is zero...
4624 pr_warning("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
4625 updelay
, downdelay
);
4628 /* don't allow arp monitoring */
4630 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4631 miimon
, arp_interval
);
4635 if ((updelay
% miimon
) != 0) {
4636 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4638 (updelay
/ miimon
) * miimon
);
4643 if ((downdelay
% miimon
) != 0) {
4644 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4646 (downdelay
/ miimon
) * miimon
);
4649 downdelay
/= miimon
;
4652 if (arp_interval
< 0) {
4653 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4654 arp_interval
, INT_MAX
, BOND_LINK_ARP_INTERV
);
4655 arp_interval
= BOND_LINK_ARP_INTERV
;
4658 for (arp_ip_count
= 0;
4659 (arp_ip_count
< BOND_MAX_ARP_TARGETS
) && arp_ip_target
[arp_ip_count
];
4661 /* not complete check, but should be good enough to
4663 if (!isdigit(arp_ip_target
[arp_ip_count
][0])) {
4664 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4665 arp_ip_target
[arp_ip_count
]);
4668 __be32 ip
= in_aton(arp_ip_target
[arp_ip_count
]);
4669 arp_target
[arp_ip_count
] = ip
;
4673 if (arp_interval
&& !arp_ip_count
) {
4674 /* don't allow arping if no arp_ip_target given... */
4675 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4681 if (bond_mode
!= BOND_MODE_ACTIVEBACKUP
) {
4682 pr_err("arp_validate only supported in active-backup mode\n");
4685 if (!arp_interval
) {
4686 pr_err("arp_validate requires arp_interval\n");
4690 arp_validate_value
= bond_parse_parm(arp_validate
,
4692 if (arp_validate_value
== -1) {
4693 pr_err("Error: invalid arp_validate \"%s\"\n",
4694 arp_validate
== NULL
? "NULL" : arp_validate
);
4698 arp_validate_value
= 0;
4701 pr_info("MII link monitoring set to %d ms\n", miimon
);
4702 } else if (arp_interval
) {
4705 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4707 arp_validate_tbl
[arp_validate_value
].modename
,
4710 for (i
= 0; i
< arp_ip_count
; i
++)
4711 pr_info(" %s", arp_ip_target
[i
]);
4715 } else if (max_bonds
) {
4716 /* miimon and arp_interval not set, we need one so things
4717 * work as expected, see bonding.txt for details
4719 pr_warning("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details.\n");
4722 if (primary
&& !USES_PRIMARY(bond_mode
)) {
4723 /* currently, using a primary only makes sense
4724 * in active backup, TLB or ALB modes
4726 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4727 primary
, bond_mode_name(bond_mode
));
4731 if (primary
&& primary_reselect
) {
4732 primary_reselect_value
= bond_parse_parm(primary_reselect
,
4734 if (primary_reselect_value
== -1) {
4735 pr_err("Error: Invalid primary_reselect \"%s\"\n",
4737 NULL
? "NULL" : primary_reselect
);
4741 primary_reselect_value
= BOND_PRI_RESELECT_ALWAYS
;
4744 if (fail_over_mac
) {
4745 fail_over_mac_value
= bond_parse_parm(fail_over_mac
,
4747 if (fail_over_mac_value
== -1) {
4748 pr_err("Error: invalid fail_over_mac \"%s\"\n",
4749 arp_validate
== NULL
? "NULL" : arp_validate
);
4753 if (bond_mode
!= BOND_MODE_ACTIVEBACKUP
)
4754 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
4756 fail_over_mac_value
= BOND_FOM_NONE
;
4759 /* fill params struct with the proper values */
4760 params
->mode
= bond_mode
;
4761 params
->xmit_policy
= xmit_hashtype
;
4762 params
->miimon
= miimon
;
4763 params
->arp_interval
= arp_interval
;
4764 params
->arp_validate
= arp_validate_value
;
4765 params
->updelay
= updelay
;
4766 params
->downdelay
= downdelay
;
4767 params
->use_carrier
= use_carrier
;
4768 params
->lacp_fast
= lacp_fast
;
4769 params
->primary
[0] = 0;
4770 params
->primary_reselect
= primary_reselect_value
;
4771 params
->fail_over_mac
= fail_over_mac_value
;
4772 params
->tx_queues
= tx_queues
;
4773 params
->all_slaves_active
= all_slaves_active
;
4774 params
->resend_igmp
= resend_igmp
;
4777 strncpy(params
->primary
, primary
, IFNAMSIZ
);
4778 params
->primary
[IFNAMSIZ
- 1] = 0;
4781 memcpy(params
->arp_targets
, arp_target
, sizeof(arp_target
));
4786 static struct lock_class_key bonding_netdev_xmit_lock_key
;
4787 static struct lock_class_key bonding_netdev_addr_lock_key
;
4789 static void bond_set_lockdep_class_one(struct net_device
*dev
,
4790 struct netdev_queue
*txq
,
4793 lockdep_set_class(&txq
->_xmit_lock
,
4794 &bonding_netdev_xmit_lock_key
);
4797 static void bond_set_lockdep_class(struct net_device
*dev
)
4799 lockdep_set_class(&dev
->addr_list_lock
,
4800 &bonding_netdev_addr_lock_key
);
4801 netdev_for_each_tx_queue(dev
, bond_set_lockdep_class_one
, NULL
);
4805 * Called from registration process
4807 static int bond_init(struct net_device
*bond_dev
)
4809 struct bonding
*bond
= netdev_priv(bond_dev
);
4810 struct bond_net
*bn
= net_generic(dev_net(bond_dev
), bond_net_id
);
4812 pr_debug("Begin bond_init for %s\n", bond_dev
->name
);
4814 bond
->wq
= create_singlethread_workqueue(bond_dev
->name
);
4818 bond_set_lockdep_class(bond_dev
);
4820 bond_create_proc_entry(bond
);
4821 list_add_tail(&bond
->bond_list
, &bn
->dev_list
);
4823 bond_prepare_sysfs_group(bond
);
4825 bond_debug_register(bond
);
4827 __hw_addr_init(&bond
->mc_list
);
4831 static int bond_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
4833 if (tb
[IFLA_ADDRESS
]) {
4834 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
4836 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
4837 return -EADDRNOTAVAIL
;
4842 static struct rtnl_link_ops bond_link_ops __read_mostly
= {
4844 .priv_size
= sizeof(struct bonding
),
4845 .setup
= bond_setup
,
4846 .validate
= bond_validate
,
4849 /* Create a new bond based on the specified name and bonding parameters.
4850 * If name is NULL, obtain a suitable "bond%d" name for us.
4851 * Caller must NOT hold rtnl_lock; we need to release it here before we
4852 * set up our sysfs entries.
4854 int bond_create(struct net
*net
, const char *name
)
4856 struct net_device
*bond_dev
;
4861 bond_dev
= alloc_netdev_mq(sizeof(struct bonding
), name
? name
: "",
4862 bond_setup
, tx_queues
);
4864 pr_err("%s: eek! can't alloc netdev!\n", name
);
4869 dev_net_set(bond_dev
, net
);
4870 bond_dev
->rtnl_link_ops
= &bond_link_ops
;
4873 res
= dev_alloc_name(bond_dev
, "bond%d");
4878 * If we're given a name to register
4879 * we need to ensure that its not already
4883 if (__dev_get_by_name(net
, name
) != NULL
)
4887 res
= register_netdevice(bond_dev
);
4889 netif_carrier_off(bond_dev
);
4894 bond_destructor(bond_dev
);
4898 static int __net_init
bond_net_init(struct net
*net
)
4900 struct bond_net
*bn
= net_generic(net
, bond_net_id
);
4903 INIT_LIST_HEAD(&bn
->dev_list
);
4905 bond_create_proc_dir(bn
);
4910 static void __net_exit
bond_net_exit(struct net
*net
)
4912 struct bond_net
*bn
= net_generic(net
, bond_net_id
);
4914 bond_destroy_proc_dir(bn
);
4917 static struct pernet_operations bond_net_ops
= {
4918 .init
= bond_net_init
,
4919 .exit
= bond_net_exit
,
4921 .size
= sizeof(struct bond_net
),
4924 static int __init
bonding_init(void)
4929 pr_info("%s", bond_version
);
4931 res
= bond_check_params(&bonding_defaults
);
4935 res
= register_pernet_subsys(&bond_net_ops
);
4939 res
= rtnl_link_register(&bond_link_ops
);
4943 bond_create_debugfs();
4945 for (i
= 0; i
< max_bonds
; i
++) {
4946 res
= bond_create(&init_net
, NULL
);
4951 res
= bond_create_sysfs();
4955 register_netdevice_notifier(&bond_netdev_notifier
);
4956 register_inetaddr_notifier(&bond_inetaddr_notifier
);
4960 rtnl_link_unregister(&bond_link_ops
);
4962 unregister_pernet_subsys(&bond_net_ops
);
4967 static void __exit
bonding_exit(void)
4969 unregister_netdevice_notifier(&bond_netdev_notifier
);
4970 unregister_inetaddr_notifier(&bond_inetaddr_notifier
);
4972 bond_destroy_sysfs();
4973 bond_destroy_debugfs();
4975 rtnl_link_unregister(&bond_link_ops
);
4976 unregister_pernet_subsys(&bond_net_ops
);
4978 #ifdef CONFIG_NET_POLL_CONTROLLER
4980 * Make sure we don't have an imbalance on our netpoll blocking
4982 WARN_ON(atomic_read(&netpoll_block_tx
));
4986 module_init(bonding_init
);
4987 module_exit(bonding_exit
);
4988 MODULE_LICENSE("GPL");
4989 MODULE_VERSION(DRV_VERSION
);
4990 MODULE_DESCRIPTION(DRV_DESCRIPTION
", v" DRV_VERSION
);
4991 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4992 MODULE_ALIAS_RTNL_LINK("bond");