mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / bonding / bond_main.c
blob3a9b876c419c654d111e4271313a7fbb4447ea45
1 /*
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:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
16 * How it works:
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
23 * ifconfig bond0 down
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>
43 #include <linux/in.h>
44 #include <net/ip.h>
45 #include <linux/ip.h>
46 #include <linux/tcp.h>
47 #include <linux/udp.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
50 #include <linux/init.h>
51 #include <linux/timer.h>
52 #include <linux/socket.h>
53 #include <linux/ctype.h>
54 #include <linux/inet.h>
55 #include <linux/bitops.h>
56 #include <linux/io.h>
57 #include <asm/dma.h>
58 #include <linux/uaccess.h>
59 #include <linux/errno.h>
60 #include <linux/netdevice.h>
61 #include <linux/inetdevice.h>
62 #include <linux/igmp.h>
63 #include <linux/etherdevice.h>
64 #include <linux/skbuff.h>
65 #include <net/sock.h>
66 #include <linux/rtnetlink.h>
67 #include <linux/smp.h>
68 #include <linux/if_ether.h>
69 #include <net/arp.h>
70 #include <linux/mii.h>
71 #include <linux/ethtool.h>
72 #include <linux/if_vlan.h>
73 #include <linux/if_bonding.h>
74 #include <linux/jiffies.h>
75 #include <linux/preempt.h>
76 #include <net/route.h>
77 #include <net/net_namespace.h>
78 #include <net/netns/generic.h>
79 #include <net/pkt_sched.h>
80 #include <linux/rculist.h>
81 #include "bonding.h"
82 #include "bond_3ad.h"
83 #include "bond_alb.h"
85 /*---------------------------- Module parameters ----------------------------*/
87 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
88 #define BOND_LINK_MON_INTERV 0
89 #define BOND_LINK_ARP_INTERV 0
91 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
92 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
93 static int num_peer_notif = 1;
94 static int miimon = BOND_LINK_MON_INTERV;
95 static int updelay;
96 static int downdelay;
97 static int use_carrier = 1;
98 static char *mode;
99 static char *primary;
100 static char *primary_reselect;
101 static char *lacp_rate;
102 static int min_links;
103 static char *ad_select;
104 static char *xmit_hash_policy;
105 static int arp_interval = BOND_LINK_ARP_INTERV;
106 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
107 static char *arp_validate;
108 static char *arp_all_targets;
109 static char *fail_over_mac;
110 static int all_slaves_active;
111 static struct bond_params bonding_defaults;
112 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
114 module_param(max_bonds, int, 0);
115 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
116 module_param(tx_queues, int, 0);
117 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
118 module_param_named(num_grat_arp, num_peer_notif, int, 0644);
119 MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
120 "failover event (alias of num_unsol_na)");
121 module_param_named(num_unsol_na, num_peer_notif, int, 0644);
122 MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
123 "failover event (alias of num_grat_arp)");
124 module_param(miimon, int, 0);
125 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
126 module_param(updelay, int, 0);
127 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
128 module_param(downdelay, int, 0);
129 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
130 "in milliseconds");
131 module_param(use_carrier, int, 0);
132 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
133 "0 for off, 1 for on (default)");
134 module_param(mode, charp, 0);
135 MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
136 "1 for active-backup, 2 for balance-xor, "
137 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
138 "6 for balance-alb");
139 module_param(primary, charp, 0);
140 MODULE_PARM_DESC(primary, "Primary network device to use");
141 module_param(primary_reselect, charp, 0);
142 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
143 "once it comes up; "
144 "0 for always (default), "
145 "1 for only if speed of primary is "
146 "better, "
147 "2 for only on active slave "
148 "failure");
149 module_param(lacp_rate, charp, 0);
150 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
151 "0 for slow, 1 for fast");
152 module_param(ad_select, charp, 0);
153 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic; "
154 "0 for stable (default), 1 for bandwidth, "
155 "2 for count");
156 module_param(min_links, int, 0);
157 MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
159 module_param(xmit_hash_policy, charp, 0);
160 MODULE_PARM_DESC(xmit_hash_policy, "balance-xor and 802.3ad hashing method; "
161 "0 for layer 2 (default), 1 for layer 3+4, "
162 "2 for layer 2+3");
163 module_param(arp_interval, int, 0);
164 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
165 module_param_array(arp_ip_target, charp, NULL, 0);
166 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
167 module_param(arp_validate, charp, 0);
168 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
169 "0 for none (default), 1 for active, "
170 "2 for backup, 3 for all");
171 module_param(arp_all_targets, charp, 0);
172 MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
173 module_param(fail_over_mac, charp, 0);
174 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
175 "the same MAC; 0 for none (default), "
176 "1 for active, 2 for follow");
177 module_param(all_slaves_active, int, 0);
178 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
179 "by setting active flag for all slaves; "
180 "0 for never (default), 1 for always.");
181 module_param(resend_igmp, int, 0);
182 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
183 "link failure");
185 /*----------------------------- Global variables ----------------------------*/
187 #ifdef CONFIG_NET_POLL_CONTROLLER
188 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
189 #endif
191 int bond_net_id __read_mostly;
193 static __be32 arp_target[BOND_MAX_ARP_TARGETS];
194 static int arp_ip_count;
195 static int bond_mode = BOND_MODE_ROUNDROBIN;
196 static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
197 static int lacp_fast;
199 const struct bond_parm_tbl bond_lacp_tbl[] = {
200 { "slow", AD_LACP_SLOW},
201 { "fast", AD_LACP_FAST},
202 { NULL, -1},
205 const struct bond_parm_tbl bond_mode_tbl[] = {
206 { "balance-rr", BOND_MODE_ROUNDROBIN},
207 { "active-backup", BOND_MODE_ACTIVEBACKUP},
208 { "balance-xor", BOND_MODE_XOR},
209 { "broadcast", BOND_MODE_BROADCAST},
210 { "802.3ad", BOND_MODE_8023AD},
211 { "balance-tlb", BOND_MODE_TLB},
212 { "balance-alb", BOND_MODE_ALB},
213 { NULL, -1},
216 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
217 { "layer2", BOND_XMIT_POLICY_LAYER2},
218 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
219 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
220 { NULL, -1},
223 const struct bond_parm_tbl arp_all_targets_tbl[] = {
224 { "any", BOND_ARP_TARGETS_ANY},
225 { "all", BOND_ARP_TARGETS_ALL},
226 { NULL, -1},
229 const struct bond_parm_tbl arp_validate_tbl[] = {
230 { "none", BOND_ARP_VALIDATE_NONE},
231 { "active", BOND_ARP_VALIDATE_ACTIVE},
232 { "backup", BOND_ARP_VALIDATE_BACKUP},
233 { "all", BOND_ARP_VALIDATE_ALL},
234 { NULL, -1},
237 const struct bond_parm_tbl fail_over_mac_tbl[] = {
238 { "none", BOND_FOM_NONE},
239 { "active", BOND_FOM_ACTIVE},
240 { "follow", BOND_FOM_FOLLOW},
241 { NULL, -1},
244 const struct bond_parm_tbl pri_reselect_tbl[] = {
245 { "always", BOND_PRI_RESELECT_ALWAYS},
246 { "better", BOND_PRI_RESELECT_BETTER},
247 { "failure", BOND_PRI_RESELECT_FAILURE},
248 { NULL, -1},
251 struct bond_parm_tbl ad_select_tbl[] = {
252 { "stable", BOND_AD_STABLE},
253 { "bandwidth", BOND_AD_BANDWIDTH},
254 { "count", BOND_AD_COUNT},
255 { NULL, -1},
258 /*-------------------------- Forward declarations ---------------------------*/
260 static int bond_init(struct net_device *bond_dev);
261 static void bond_uninit(struct net_device *bond_dev);
262 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
263 int mod);
265 /*---------------------------- General routines -----------------------------*/
267 const char *bond_mode_name(int mode)
269 static const char *names[] = {
270 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
271 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
272 [BOND_MODE_XOR] = "load balancing (xor)",
273 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
274 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
275 [BOND_MODE_TLB] = "transmit load balancing",
276 [BOND_MODE_ALB] = "adaptive load balancing",
279 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
280 return "unknown";
282 return names[mode];
285 /*---------------------------------- VLAN -----------------------------------*/
288 * bond_dev_queue_xmit - Prepare skb for xmit.
290 * @bond: bond device that got this skb for tx.
291 * @skb: hw accel VLAN tagged skb to transmit
292 * @slave_dev: slave that is supposed to xmit this skbuff
294 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
295 struct net_device *slave_dev)
297 skb->dev = slave_dev;
299 BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
300 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
301 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
303 if (unlikely(netpoll_tx_running(bond->dev)))
304 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
305 else
306 dev_queue_xmit(skb);
308 return 0;
312 * In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
313 * We don't protect the slave list iteration with a lock because:
314 * a. This operation is performed in IOCTL context,
315 * b. The operation is protected by the RTNL semaphore in the 8021q code,
316 * c. Holding a lock with BH disabled while directly calling a base driver
317 * entry point is generally a BAD idea.
319 * The design of synchronization/protection for this operation in the 8021q
320 * module is good for one or more VLAN devices over a single physical device
321 * and cannot be extended for a teaming solution like bonding, so there is a
322 * potential race condition here where a net device from the vlan group might
323 * be referenced (either by a base driver or the 8021q code) while it is being
324 * removed from the system. However, it turns out we're not making matters
325 * worse, and if it works for regular VLAN usage it will work here too.
329 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
330 * @bond_dev: bonding net device that got called
331 * @vid: vlan id being added
333 static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
334 __be16 proto, u16 vid)
336 struct bonding *bond = netdev_priv(bond_dev);
337 struct slave *slave;
338 int res;
340 bond_for_each_slave(bond, slave) {
341 res = vlan_vid_add(slave->dev, proto, vid);
342 if (res)
343 goto unwind;
346 return 0;
348 unwind:
349 /* unwind from the slave that failed */
350 bond_for_each_slave_continue_reverse(bond, slave)
351 vlan_vid_del(slave->dev, proto, vid);
353 return res;
357 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
358 * @bond_dev: bonding net device that got called
359 * @vid: vlan id being removed
361 static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
362 __be16 proto, u16 vid)
364 struct bonding *bond = netdev_priv(bond_dev);
365 struct slave *slave;
367 bond_for_each_slave(bond, slave)
368 vlan_vid_del(slave->dev, proto, vid);
370 if (bond_is_lb(bond))
371 bond_alb_clear_vlan(bond, vid);
373 return 0;
376 /*------------------------------- Link status -------------------------------*/
379 * Set the carrier state for the master according to the state of its
380 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
381 * do special 802.3ad magic.
383 * Returns zero if carrier state does not change, nonzero if it does.
385 static int bond_set_carrier(struct bonding *bond)
387 struct slave *slave;
389 if (list_empty(&bond->slave_list))
390 goto down;
392 if (bond->params.mode == BOND_MODE_8023AD)
393 return bond_3ad_set_carrier(bond);
395 bond_for_each_slave(bond, slave) {
396 if (slave->link == BOND_LINK_UP) {
397 if (!netif_carrier_ok(bond->dev)) {
398 netif_carrier_on(bond->dev);
399 return 1;
401 return 0;
405 down:
406 if (netif_carrier_ok(bond->dev)) {
407 netif_carrier_off(bond->dev);
408 return 1;
410 return 0;
414 * Get link speed and duplex from the slave's base driver
415 * using ethtool. If for some reason the call fails or the
416 * values are invalid, set speed and duplex to -1,
417 * and return.
419 static void bond_update_speed_duplex(struct slave *slave)
421 struct net_device *slave_dev = slave->dev;
422 struct ethtool_cmd ecmd;
423 u32 slave_speed;
424 int res;
426 slave->speed = SPEED_UNKNOWN;
427 slave->duplex = DUPLEX_UNKNOWN;
429 res = __ethtool_get_settings(slave_dev, &ecmd);
430 if (res < 0)
431 return;
433 slave_speed = ethtool_cmd_speed(&ecmd);
434 if (slave_speed == 0 || slave_speed == ((__u32) -1))
435 return;
437 switch (ecmd.duplex) {
438 case DUPLEX_FULL:
439 case DUPLEX_HALF:
440 break;
441 default:
442 return;
445 slave->speed = slave_speed;
446 slave->duplex = ecmd.duplex;
448 return;
452 * if <dev> supports MII link status reporting, check its link status.
454 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
455 * depending upon the setting of the use_carrier parameter.
457 * Return either BMSR_LSTATUS, meaning that the link is up (or we
458 * can't tell and just pretend it is), or 0, meaning that the link is
459 * down.
461 * If reporting is non-zero, instead of faking link up, return -1 if
462 * both ETHTOOL and MII ioctls fail (meaning the device does not
463 * support them). If use_carrier is set, return whatever it says.
464 * It'd be nice if there was a good way to tell if a driver supports
465 * netif_carrier, but there really isn't.
467 static int bond_check_dev_link(struct bonding *bond,
468 struct net_device *slave_dev, int reporting)
470 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
471 int (*ioctl)(struct net_device *, struct ifreq *, int);
472 struct ifreq ifr;
473 struct mii_ioctl_data *mii;
475 if (!reporting && !netif_running(slave_dev))
476 return 0;
478 if (bond->params.use_carrier)
479 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
481 /* Try to get link status using Ethtool first. */
482 if (slave_dev->ethtool_ops->get_link)
483 return slave_dev->ethtool_ops->get_link(slave_dev) ?
484 BMSR_LSTATUS : 0;
486 /* Ethtool can't be used, fallback to MII ioctls. */
487 ioctl = slave_ops->ndo_do_ioctl;
488 if (ioctl) {
489 /* TODO: set pointer to correct ioctl on a per team member */
490 /* bases to make this more efficient. that is, once */
491 /* we determine the correct ioctl, we will always */
492 /* call it and not the others for that team */
493 /* member. */
496 * We cannot assume that SIOCGMIIPHY will also read a
497 * register; not all network drivers (e.g., e100)
498 * support that.
501 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
502 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
503 mii = if_mii(&ifr);
504 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
505 mii->reg_num = MII_BMSR;
506 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
507 return mii->val_out & BMSR_LSTATUS;
512 * If reporting, report that either there's no dev->do_ioctl,
513 * or both SIOCGMIIREG and get_link failed (meaning that we
514 * cannot report link status). If not reporting, pretend
515 * we're ok.
517 return reporting ? -1 : BMSR_LSTATUS;
520 /*----------------------------- Multicast list ------------------------------*/
523 * Push the promiscuity flag down to appropriate slaves
525 static int bond_set_promiscuity(struct bonding *bond, int inc)
527 int err = 0;
528 if (USES_PRIMARY(bond->params.mode)) {
529 /* write lock already acquired */
530 if (bond->curr_active_slave) {
531 err = dev_set_promiscuity(bond->curr_active_slave->dev,
532 inc);
534 } else {
535 struct slave *slave;
537 bond_for_each_slave(bond, slave) {
538 err = dev_set_promiscuity(slave->dev, inc);
539 if (err)
540 return err;
543 return err;
547 * Push the allmulti flag down to all slaves
549 static int bond_set_allmulti(struct bonding *bond, int inc)
551 int err = 0;
552 if (USES_PRIMARY(bond->params.mode)) {
553 /* write lock already acquired */
554 if (bond->curr_active_slave) {
555 err = dev_set_allmulti(bond->curr_active_slave->dev,
556 inc);
558 } else {
559 struct slave *slave;
561 bond_for_each_slave(bond, slave) {
562 err = dev_set_allmulti(slave->dev, inc);
563 if (err)
564 return err;
567 return err;
571 * Retrieve the list of registered multicast addresses for the bonding
572 * device and retransmit an IGMP JOIN request to the current active
573 * slave.
575 static void bond_resend_igmp_join_requests(struct bonding *bond)
577 if (!rtnl_trylock()) {
578 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
579 return;
581 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
582 rtnl_unlock();
584 /* We use curr_slave_lock to protect against concurrent access to
585 * igmp_retrans from multiple running instances of this function and
586 * bond_change_active_slave
588 write_lock_bh(&bond->curr_slave_lock);
589 if (bond->igmp_retrans > 1) {
590 bond->igmp_retrans--;
591 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
593 write_unlock_bh(&bond->curr_slave_lock);
596 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
598 struct bonding *bond = container_of(work, struct bonding,
599 mcast_work.work);
601 bond_resend_igmp_join_requests(bond);
604 /* Flush bond's hardware addresses from slave
606 static void bond_hw_addr_flush(struct net_device *bond_dev,
607 struct net_device *slave_dev)
609 struct bonding *bond = netdev_priv(bond_dev);
611 dev_uc_unsync(slave_dev, bond_dev);
612 dev_mc_unsync(slave_dev, bond_dev);
614 if (bond->params.mode == BOND_MODE_8023AD) {
615 /* del lacpdu mc addr from mc list */
616 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
618 dev_mc_del(slave_dev, lacpdu_multicast);
622 /*--------------------------- Active slave change ---------------------------*/
624 /* Update the hardware address list and promisc/allmulti for the new and
625 * old active slaves (if any). Modes that are !USES_PRIMARY keep all
626 * slaves up date at all times; only the USES_PRIMARY modes need to call
627 * this function to swap these settings during a failover.
629 static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
630 struct slave *old_active)
632 ASSERT_RTNL();
634 if (old_active) {
635 if (bond->dev->flags & IFF_PROMISC)
636 dev_set_promiscuity(old_active->dev, -1);
638 if (bond->dev->flags & IFF_ALLMULTI)
639 dev_set_allmulti(old_active->dev, -1);
641 bond_hw_addr_flush(bond->dev, old_active->dev);
644 if (new_active) {
645 /* FIXME: Signal errors upstream. */
646 if (bond->dev->flags & IFF_PROMISC)
647 dev_set_promiscuity(new_active->dev, 1);
649 if (bond->dev->flags & IFF_ALLMULTI)
650 dev_set_allmulti(new_active->dev, 1);
652 netif_addr_lock_bh(bond->dev);
653 dev_uc_sync(new_active->dev, bond->dev);
654 dev_mc_sync(new_active->dev, bond->dev);
655 netif_addr_unlock_bh(bond->dev);
660 * bond_set_dev_addr - clone slave's address to bond
661 * @bond_dev: bond net device
662 * @slave_dev: slave net device
664 * Should be called with RTNL held.
666 static void bond_set_dev_addr(struct net_device *bond_dev,
667 struct net_device *slave_dev)
669 pr_debug("bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
670 bond_dev, slave_dev, slave_dev->addr_len);
671 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
672 bond_dev->addr_assign_type = NET_ADDR_STOLEN;
673 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
676 static struct slave *bond_get_old_active(struct bonding *bond,
677 struct slave *new_active)
679 struct slave *slave;
681 bond_for_each_slave(bond, slave) {
682 if (slave == new_active)
683 continue;
685 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
686 return slave;
689 return NULL;
693 * bond_do_fail_over_mac
695 * Perform special MAC address swapping for fail_over_mac settings
697 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
699 static void bond_do_fail_over_mac(struct bonding *bond,
700 struct slave *new_active,
701 struct slave *old_active)
702 __releases(&bond->curr_slave_lock)
703 __releases(&bond->lock)
704 __acquires(&bond->lock)
705 __acquires(&bond->curr_slave_lock)
707 u8 tmp_mac[ETH_ALEN];
708 struct sockaddr saddr;
709 int rv;
711 switch (bond->params.fail_over_mac) {
712 case BOND_FOM_ACTIVE:
713 if (new_active) {
714 write_unlock_bh(&bond->curr_slave_lock);
715 read_unlock(&bond->lock);
716 bond_set_dev_addr(bond->dev, new_active->dev);
717 read_lock(&bond->lock);
718 write_lock_bh(&bond->curr_slave_lock);
720 break;
721 case BOND_FOM_FOLLOW:
723 * if new_active && old_active, swap them
724 * if just old_active, do nothing (going to no active slave)
725 * if just new_active, set new_active to bond's MAC
727 if (!new_active)
728 return;
730 write_unlock_bh(&bond->curr_slave_lock);
731 read_unlock(&bond->lock);
733 if (!old_active)
734 old_active = bond_get_old_active(bond, new_active);
736 if (old_active) {
737 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
738 memcpy(saddr.sa_data, old_active->dev->dev_addr,
739 ETH_ALEN);
740 saddr.sa_family = new_active->dev->type;
741 } else {
742 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
743 saddr.sa_family = bond->dev->type;
746 rv = dev_set_mac_address(new_active->dev, &saddr);
747 if (rv) {
748 pr_err("%s: Error %d setting MAC of slave %s\n",
749 bond->dev->name, -rv, new_active->dev->name);
750 goto out;
753 if (!old_active)
754 goto out;
756 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
757 saddr.sa_family = old_active->dev->type;
759 rv = dev_set_mac_address(old_active->dev, &saddr);
760 if (rv)
761 pr_err("%s: Error %d setting MAC of slave %s\n",
762 bond->dev->name, -rv, new_active->dev->name);
763 out:
764 read_lock(&bond->lock);
765 write_lock_bh(&bond->curr_slave_lock);
766 break;
767 default:
768 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
769 bond->dev->name, bond->params.fail_over_mac);
770 break;
775 static bool bond_should_change_active(struct bonding *bond)
777 struct slave *prim = bond->primary_slave;
778 struct slave *curr = bond->curr_active_slave;
780 if (!prim || !curr || curr->link != BOND_LINK_UP)
781 return true;
782 if (bond->force_primary) {
783 bond->force_primary = false;
784 return true;
786 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
787 (prim->speed < curr->speed ||
788 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
789 return false;
790 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
791 return false;
792 return true;
796 * find_best_interface - select the best available slave to be the active one
797 * @bond: our bonding struct
799 * Warning: Caller must hold curr_slave_lock for writing.
801 static struct slave *bond_find_best_slave(struct bonding *bond)
803 struct slave *new_active, *old_active;
804 struct slave *bestslave = NULL;
805 int mintime = bond->params.updelay;
806 int i;
808 new_active = bond->curr_active_slave;
810 if (!new_active) { /* there were no active slaves left */
811 new_active = bond_first_slave(bond);
812 if (!new_active)
813 return NULL; /* still no slave, return NULL */
816 if ((bond->primary_slave) &&
817 bond->primary_slave->link == BOND_LINK_UP &&
818 bond_should_change_active(bond)) {
819 new_active = bond->primary_slave;
822 /* remember where to stop iterating over the slaves */
823 old_active = new_active;
825 bond_for_each_slave_from(bond, new_active, i, old_active) {
826 if (new_active->link == BOND_LINK_UP) {
827 return new_active;
828 } else if (new_active->link == BOND_LINK_BACK &&
829 IS_UP(new_active->dev)) {
830 /* link up, but waiting for stabilization */
831 if (new_active->delay < mintime) {
832 mintime = new_active->delay;
833 bestslave = new_active;
838 return bestslave;
841 static bool bond_should_notify_peers(struct bonding *bond)
843 struct slave *slave = bond->curr_active_slave;
845 pr_debug("bond_should_notify_peers: bond %s slave %s\n",
846 bond->dev->name, slave ? slave->dev->name : "NULL");
848 if (!slave || !bond->send_peer_notif ||
849 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
850 return false;
852 return true;
856 * change_active_interface - change the active slave into the specified one
857 * @bond: our bonding struct
858 * @new: the new slave to make the active one
860 * Set the new slave to the bond's settings and unset them on the old
861 * curr_active_slave.
862 * Setting include flags, mc-list, promiscuity, allmulti, etc.
864 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
865 * because it is apparently the best available slave we have, even though its
866 * updelay hasn't timed out yet.
868 * If new_active is not NULL, caller must hold bond->lock for read and
869 * curr_slave_lock for write_bh.
871 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
873 struct slave *old_active = bond->curr_active_slave;
875 if (old_active == new_active)
876 return;
878 if (new_active) {
879 new_active->jiffies = jiffies;
881 if (new_active->link == BOND_LINK_BACK) {
882 if (USES_PRIMARY(bond->params.mode)) {
883 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
884 bond->dev->name, new_active->dev->name,
885 (bond->params.updelay - new_active->delay) * bond->params.miimon);
888 new_active->delay = 0;
889 new_active->link = BOND_LINK_UP;
891 if (bond->params.mode == BOND_MODE_8023AD)
892 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
894 if (bond_is_lb(bond))
895 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
896 } else {
897 if (USES_PRIMARY(bond->params.mode)) {
898 pr_info("%s: making interface %s the new active one.\n",
899 bond->dev->name, new_active->dev->name);
904 if (USES_PRIMARY(bond->params.mode))
905 bond_hw_addr_swap(bond, new_active, old_active);
907 if (bond_is_lb(bond)) {
908 bond_alb_handle_active_change(bond, new_active);
909 if (old_active)
910 bond_set_slave_inactive_flags(old_active);
911 if (new_active)
912 bond_set_slave_active_flags(new_active);
913 } else {
914 rcu_assign_pointer(bond->curr_active_slave, new_active);
917 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
918 if (old_active)
919 bond_set_slave_inactive_flags(old_active);
921 if (new_active) {
922 bool should_notify_peers = false;
924 bond_set_slave_active_flags(new_active);
926 if (bond->params.fail_over_mac)
927 bond_do_fail_over_mac(bond, new_active,
928 old_active);
930 if (netif_running(bond->dev)) {
931 bond->send_peer_notif =
932 bond->params.num_peer_notif;
933 should_notify_peers =
934 bond_should_notify_peers(bond);
937 write_unlock_bh(&bond->curr_slave_lock);
938 read_unlock(&bond->lock);
940 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
941 if (should_notify_peers)
942 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
943 bond->dev);
945 read_lock(&bond->lock);
946 write_lock_bh(&bond->curr_slave_lock);
950 /* resend IGMP joins since active slave has changed or
951 * all were sent on curr_active_slave.
952 * resend only if bond is brought up with the affected
953 * bonding modes and the retransmission is enabled */
954 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
955 ((USES_PRIMARY(bond->params.mode) && new_active) ||
956 bond->params.mode == BOND_MODE_ROUNDROBIN)) {
957 bond->igmp_retrans = bond->params.resend_igmp;
958 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
963 * bond_select_active_slave - select a new active slave, if needed
964 * @bond: our bonding struct
966 * This functions should be called when one of the following occurs:
967 * - The old curr_active_slave has been released or lost its link.
968 * - The primary_slave has got its link back.
969 * - A slave has got its link back and there's no old curr_active_slave.
971 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
973 void bond_select_active_slave(struct bonding *bond)
975 struct slave *best_slave;
976 int rv;
978 best_slave = bond_find_best_slave(bond);
979 if (best_slave != bond->curr_active_slave) {
980 bond_change_active_slave(bond, best_slave);
981 rv = bond_set_carrier(bond);
982 if (!rv)
983 return;
985 if (netif_carrier_ok(bond->dev)) {
986 pr_info("%s: first active interface up!\n",
987 bond->dev->name);
988 } else {
989 pr_info("%s: now running without any active interface !\n",
990 bond->dev->name);
995 /*--------------------------- slave list handling ---------------------------*/
998 * This function attaches the slave to the end of list.
1000 * bond->lock held for writing by caller.
1002 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1004 list_add_tail_rcu(&new_slave->list, &bond->slave_list);
1005 bond->slave_cnt++;
1009 * This function detaches the slave from the list.
1010 * WARNING: no check is made to verify if the slave effectively
1011 * belongs to <bond>.
1012 * Nothing is freed on return, structures are just unchained.
1013 * If any slave pointer in bond was pointing to <slave>,
1014 * it should be changed by the calling function.
1016 * bond->lock held for writing by caller.
1018 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1020 list_del_rcu(&slave->list);
1021 bond->slave_cnt--;
1024 #ifdef CONFIG_NET_POLL_CONTROLLER
1025 static inline int slave_enable_netpoll(struct slave *slave)
1027 struct netpoll *np;
1028 int err = 0;
1030 np = kzalloc(sizeof(*np), GFP_ATOMIC);
1031 err = -ENOMEM;
1032 if (!np)
1033 goto out;
1035 err = __netpoll_setup(np, slave->dev, GFP_ATOMIC);
1036 if (err) {
1037 kfree(np);
1038 goto out;
1040 slave->np = np;
1041 out:
1042 return err;
1044 static inline void slave_disable_netpoll(struct slave *slave)
1046 struct netpoll *np = slave->np;
1048 if (!np)
1049 return;
1051 slave->np = NULL;
1052 __netpoll_free_async(np);
1054 static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1056 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1057 return false;
1058 if (!slave_dev->netdev_ops->ndo_poll_controller)
1059 return false;
1060 return true;
1063 static void bond_poll_controller(struct net_device *bond_dev)
1067 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1069 struct bonding *bond = netdev_priv(bond_dev);
1070 struct slave *slave;
1072 bond_for_each_slave(bond, slave)
1073 if (IS_UP(slave->dev))
1074 slave_disable_netpoll(slave);
1077 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni, gfp_t gfp)
1079 struct bonding *bond = netdev_priv(dev);
1080 struct slave *slave;
1081 int err = 0;
1083 bond_for_each_slave(bond, slave) {
1084 err = slave_enable_netpoll(slave);
1085 if (err) {
1086 bond_netpoll_cleanup(dev);
1087 break;
1090 return err;
1092 #else
1093 static inline int slave_enable_netpoll(struct slave *slave)
1095 return 0;
1097 static inline void slave_disable_netpoll(struct slave *slave)
1100 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1103 #endif
1105 /*---------------------------------- IOCTL ----------------------------------*/
1107 static netdev_features_t bond_fix_features(struct net_device *dev,
1108 netdev_features_t features)
1110 struct bonding *bond = netdev_priv(dev);
1111 netdev_features_t mask;
1112 struct slave *slave;
1114 if (list_empty(&bond->slave_list)) {
1115 /* Disable adding VLANs to empty bond. But why? --mq */
1116 features |= NETIF_F_VLAN_CHALLENGED;
1117 return features;
1120 mask = features;
1121 features &= ~NETIF_F_ONE_FOR_ALL;
1122 features |= NETIF_F_ALL_FOR_ALL;
1124 bond_for_each_slave(bond, slave) {
1125 features = netdev_increment_features(features,
1126 slave->dev->features,
1127 mask);
1129 features = netdev_add_tso_features(features, mask);
1131 return features;
1134 #define BOND_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
1135 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
1136 NETIF_F_HIGHDMA | NETIF_F_LRO)
1138 static void bond_compute_features(struct bonding *bond)
1140 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
1141 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
1142 unsigned short max_hard_header_len = ETH_HLEN;
1143 unsigned int gso_max_size = GSO_MAX_SIZE;
1144 struct net_device *bond_dev = bond->dev;
1145 u16 gso_max_segs = GSO_MAX_SEGS;
1146 struct slave *slave;
1148 if (list_empty(&bond->slave_list))
1149 goto done;
1151 bond_for_each_slave(bond, slave) {
1152 vlan_features = netdev_increment_features(vlan_features,
1153 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1155 dst_release_flag &= slave->dev->priv_flags;
1156 if (slave->dev->hard_header_len > max_hard_header_len)
1157 max_hard_header_len = slave->dev->hard_header_len;
1159 gso_max_size = min(gso_max_size, slave->dev->gso_max_size);
1160 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs);
1163 done:
1164 bond_dev->vlan_features = vlan_features;
1165 bond_dev->hard_header_len = max_hard_header_len;
1166 bond_dev->gso_max_segs = gso_max_segs;
1167 netif_set_gso_max_size(bond_dev, gso_max_size);
1169 flags = bond_dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
1170 bond_dev->priv_flags = flags | dst_release_flag;
1172 netdev_change_features(bond_dev);
1175 static void bond_setup_by_slave(struct net_device *bond_dev,
1176 struct net_device *slave_dev)
1178 bond_dev->header_ops = slave_dev->header_ops;
1180 bond_dev->type = slave_dev->type;
1181 bond_dev->hard_header_len = slave_dev->hard_header_len;
1182 bond_dev->addr_len = slave_dev->addr_len;
1184 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1185 slave_dev->addr_len);
1188 /* On bonding slaves other than the currently active slave, suppress
1189 * duplicates except for alb non-mcast/bcast.
1191 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1192 struct slave *slave,
1193 struct bonding *bond)
1195 if (bond_is_slave_inactive(slave)) {
1196 if (bond->params.mode == BOND_MODE_ALB &&
1197 skb->pkt_type != PACKET_BROADCAST &&
1198 skb->pkt_type != PACKET_MULTICAST)
1199 return false;
1200 return true;
1202 return false;
1205 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1207 struct sk_buff *skb = *pskb;
1208 struct slave *slave;
1209 struct bonding *bond;
1210 int (*recv_probe)(const struct sk_buff *, struct bonding *,
1211 struct slave *);
1212 int ret = RX_HANDLER_ANOTHER;
1214 skb = skb_share_check(skb, GFP_ATOMIC);
1215 if (unlikely(!skb))
1216 return RX_HANDLER_CONSUMED;
1218 *pskb = skb;
1220 slave = bond_slave_get_rcu(skb->dev);
1221 bond = slave->bond;
1223 if (bond->params.arp_interval)
1224 slave->dev->last_rx = jiffies;
1226 recv_probe = ACCESS_ONCE(bond->recv_probe);
1227 if (recv_probe) {
1228 ret = recv_probe(skb, bond, slave);
1229 if (ret == RX_HANDLER_CONSUMED) {
1230 consume_skb(skb);
1231 return ret;
1235 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1236 return RX_HANDLER_EXACT;
1239 skb->dev = bond->dev;
1241 if (bond->params.mode == BOND_MODE_ALB &&
1242 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
1243 skb->pkt_type == PACKET_HOST) {
1245 if (unlikely(skb_cow_head(skb,
1246 skb->data - skb_mac_header(skb)))) {
1247 kfree_skb(skb);
1248 return RX_HANDLER_CONSUMED;
1250 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
1253 return ret;
1256 static int bond_master_upper_dev_link(struct net_device *bond_dev,
1257 struct net_device *slave_dev)
1259 int err;
1261 err = netdev_master_upper_dev_link(slave_dev, bond_dev);
1262 if (err)
1263 return err;
1264 slave_dev->flags |= IFF_SLAVE;
1265 rtmsg_ifinfo(RTM_NEWLINK, slave_dev, IFF_SLAVE);
1266 return 0;
1269 static void bond_upper_dev_unlink(struct net_device *bond_dev,
1270 struct net_device *slave_dev)
1272 netdev_upper_dev_unlink(slave_dev, bond_dev);
1273 slave_dev->flags &= ~IFF_SLAVE;
1274 rtmsg_ifinfo(RTM_NEWLINK, slave_dev, IFF_SLAVE);
1277 /* enslave device <slave> to bond device <master> */
1278 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1280 struct bonding *bond = netdev_priv(bond_dev);
1281 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1282 struct slave *new_slave = NULL;
1283 struct sockaddr addr;
1284 int link_reporting;
1285 int res = 0, i;
1287 if (!bond->params.use_carrier &&
1288 slave_dev->ethtool_ops->get_link == NULL &&
1289 slave_ops->ndo_do_ioctl == NULL) {
1290 pr_warning("%s: Warning: no link monitoring support for %s\n",
1291 bond_dev->name, slave_dev->name);
1294 /* already in-use? */
1295 if (netdev_is_rx_handler_busy(slave_dev)) {
1296 netdev_err(bond_dev,
1297 "Error: Device is in use and cannot be enslaved\n");
1298 return -EBUSY;
1301 /* vlan challenged mutual exclusion */
1302 /* no need to lock since we're protected by rtnl_lock */
1303 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1304 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1305 if (vlan_uses_dev(bond_dev)) {
1306 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1307 bond_dev->name, slave_dev->name, bond_dev->name);
1308 return -EPERM;
1309 } else {
1310 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1311 bond_dev->name, slave_dev->name,
1312 slave_dev->name, bond_dev->name);
1314 } else {
1315 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1319 * Old ifenslave binaries are no longer supported. These can
1320 * be identified with moderate accuracy by the state of the slave:
1321 * the current ifenslave will set the interface down prior to
1322 * enslaving it; the old ifenslave will not.
1324 if ((slave_dev->flags & IFF_UP)) {
1325 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
1326 slave_dev->name);
1327 res = -EPERM;
1328 goto err_undo_flags;
1331 /* set bonding device ether type by slave - bonding netdevices are
1332 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1333 * there is a need to override some of the type dependent attribs/funcs.
1335 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1336 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1338 if (list_empty(&bond->slave_list)) {
1339 if (bond_dev->type != slave_dev->type) {
1340 pr_debug("%s: change device type from %d to %d\n",
1341 bond_dev->name,
1342 bond_dev->type, slave_dev->type);
1344 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1345 bond_dev);
1346 res = notifier_to_errno(res);
1347 if (res) {
1348 pr_err("%s: refused to change device type\n",
1349 bond_dev->name);
1350 res = -EBUSY;
1351 goto err_undo_flags;
1354 /* Flush unicast and multicast addresses */
1355 dev_uc_flush(bond_dev);
1356 dev_mc_flush(bond_dev);
1358 if (slave_dev->type != ARPHRD_ETHER)
1359 bond_setup_by_slave(bond_dev, slave_dev);
1360 else {
1361 ether_setup(bond_dev);
1362 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1365 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1366 bond_dev);
1368 } else if (bond_dev->type != slave_dev->type) {
1369 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1370 slave_dev->name,
1371 slave_dev->type, bond_dev->type);
1372 res = -EINVAL;
1373 goto err_undo_flags;
1376 if (slave_ops->ndo_set_mac_address == NULL) {
1377 if (list_empty(&bond->slave_list)) {
1378 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1379 bond_dev->name);
1380 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1381 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1382 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",
1383 bond_dev->name);
1384 res = -EOPNOTSUPP;
1385 goto err_undo_flags;
1389 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1391 /* If this is the first slave, then we need to set the master's hardware
1392 * address to be the same as the slave's. */
1393 if (list_empty(&bond->slave_list) &&
1394 bond->dev->addr_assign_type == NET_ADDR_RANDOM)
1395 bond_set_dev_addr(bond->dev, slave_dev);
1397 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1398 if (!new_slave) {
1399 res = -ENOMEM;
1400 goto err_undo_flags;
1402 INIT_LIST_HEAD(&new_slave->list);
1404 * Set the new_slave's queue_id to be zero. Queue ID mapping
1405 * is set via sysfs or module option if desired.
1407 new_slave->queue_id = 0;
1409 /* Save slave's original mtu and then set it to match the bond */
1410 new_slave->original_mtu = slave_dev->mtu;
1411 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1412 if (res) {
1413 pr_debug("Error %d calling dev_set_mtu\n", res);
1414 goto err_free;
1418 * Save slave's original ("permanent") mac address for modes
1419 * that need it, and for restoring it upon release, and then
1420 * set it to the master's address
1422 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1424 if (!bond->params.fail_over_mac) {
1426 * Set slave to master's mac address. The application already
1427 * set the master's mac address to that of the first slave
1429 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1430 addr.sa_family = slave_dev->type;
1431 res = dev_set_mac_address(slave_dev, &addr);
1432 if (res) {
1433 pr_debug("Error %d calling set_mac_address\n", res);
1434 goto err_restore_mtu;
1438 res = bond_master_upper_dev_link(bond_dev, slave_dev);
1439 if (res) {
1440 pr_debug("Error %d calling bond_master_upper_dev_link\n", res);
1441 goto err_restore_mac;
1444 /* open the slave since the application closed it */
1445 res = dev_open(slave_dev);
1446 if (res) {
1447 pr_debug("Opening slave %s failed\n", slave_dev->name);
1448 goto err_unset_master;
1451 new_slave->bond = bond;
1452 new_slave->dev = slave_dev;
1453 slave_dev->priv_flags |= IFF_BONDING;
1455 if (bond_is_lb(bond)) {
1456 /* bond_alb_init_slave() must be called before all other stages since
1457 * it might fail and we do not want to have to undo everything
1459 res = bond_alb_init_slave(bond, new_slave);
1460 if (res)
1461 goto err_close;
1464 /* If the mode USES_PRIMARY, then the following is handled by
1465 * bond_change_active_slave().
1467 if (!USES_PRIMARY(bond->params.mode)) {
1468 /* set promiscuity level to new slave */
1469 if (bond_dev->flags & IFF_PROMISC) {
1470 res = dev_set_promiscuity(slave_dev, 1);
1471 if (res)
1472 goto err_close;
1475 /* set allmulti level to new slave */
1476 if (bond_dev->flags & IFF_ALLMULTI) {
1477 res = dev_set_allmulti(slave_dev, 1);
1478 if (res)
1479 goto err_close;
1482 netif_addr_lock_bh(bond_dev);
1484 dev_mc_sync_multiple(slave_dev, bond_dev);
1485 dev_uc_sync_multiple(slave_dev, bond_dev);
1487 netif_addr_unlock_bh(bond_dev);
1490 if (bond->params.mode == BOND_MODE_8023AD) {
1491 /* add lacpdu mc addr to mc list */
1492 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1494 dev_mc_add(slave_dev, lacpdu_multicast);
1497 res = vlan_vids_add_by_dev(slave_dev, bond_dev);
1498 if (res) {
1499 pr_err("%s: Error: Couldn't add bond vlan ids to %s\n",
1500 bond_dev->name, slave_dev->name);
1501 goto err_close;
1504 write_lock_bh(&bond->lock);
1506 bond_attach_slave(bond, new_slave);
1508 new_slave->delay = 0;
1509 new_slave->link_failure_count = 0;
1511 write_unlock_bh(&bond->lock);
1513 bond_compute_features(bond);
1515 bond_update_speed_duplex(new_slave);
1517 read_lock(&bond->lock);
1519 new_slave->last_arp_rx = jiffies -
1520 (msecs_to_jiffies(bond->params.arp_interval) + 1);
1521 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1522 new_slave->target_last_arp_rx[i] = new_slave->last_arp_rx;
1524 if (bond->params.miimon && !bond->params.use_carrier) {
1525 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1527 if ((link_reporting == -1) && !bond->params.arp_interval) {
1529 * miimon is set but a bonded network driver
1530 * does not support ETHTOOL/MII and
1531 * arp_interval is not set. Note: if
1532 * use_carrier is enabled, we will never go
1533 * here (because netif_carrier is always
1534 * supported); thus, we don't need to change
1535 * the messages for netif_carrier.
1537 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",
1538 bond_dev->name, slave_dev->name);
1539 } else if (link_reporting == -1) {
1540 /* unable get link status using mii/ethtool */
1541 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",
1542 bond_dev->name, slave_dev->name);
1546 /* check for initial state */
1547 if (bond->params.miimon) {
1548 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
1549 if (bond->params.updelay) {
1550 new_slave->link = BOND_LINK_BACK;
1551 new_slave->delay = bond->params.updelay;
1552 } else {
1553 new_slave->link = BOND_LINK_UP;
1555 } else {
1556 new_slave->link = BOND_LINK_DOWN;
1558 } else if (bond->params.arp_interval) {
1559 new_slave->link = (netif_carrier_ok(slave_dev) ?
1560 BOND_LINK_UP : BOND_LINK_DOWN);
1561 } else {
1562 new_slave->link = BOND_LINK_UP;
1565 if (new_slave->link != BOND_LINK_DOWN)
1566 new_slave->jiffies = jiffies;
1567 pr_debug("Initial state of slave_dev is BOND_LINK_%s\n",
1568 new_slave->link == BOND_LINK_DOWN ? "DOWN" :
1569 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
1571 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1572 /* if there is a primary slave, remember it */
1573 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1574 bond->primary_slave = new_slave;
1575 bond->force_primary = true;
1579 write_lock_bh(&bond->curr_slave_lock);
1581 switch (bond->params.mode) {
1582 case BOND_MODE_ACTIVEBACKUP:
1583 bond_set_slave_inactive_flags(new_slave);
1584 bond_select_active_slave(bond);
1585 break;
1586 case BOND_MODE_8023AD:
1587 /* in 802.3ad mode, the internal mechanism
1588 * will activate the slaves in the selected
1589 * aggregator
1591 bond_set_slave_inactive_flags(new_slave);
1592 /* if this is the first slave */
1593 if (bond_first_slave(bond) == new_slave) {
1594 SLAVE_AD_INFO(new_slave).id = 1;
1595 /* Initialize AD with the number of times that the AD timer is called in 1 second
1596 * can be called only after the mac address of the bond is set
1598 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
1599 } else {
1600 struct slave *prev_slave;
1602 prev_slave = bond_prev_slave(bond, new_slave);
1603 SLAVE_AD_INFO(new_slave).id =
1604 SLAVE_AD_INFO(prev_slave).id + 1;
1607 bond_3ad_bind_slave(new_slave);
1608 break;
1609 case BOND_MODE_TLB:
1610 case BOND_MODE_ALB:
1611 bond_set_active_slave(new_slave);
1612 bond_set_slave_inactive_flags(new_slave);
1613 bond_select_active_slave(bond);
1614 break;
1615 default:
1616 pr_debug("This slave is always active in trunk mode\n");
1618 /* always active in trunk mode */
1619 bond_set_active_slave(new_slave);
1621 /* In trunking mode there is little meaning to curr_active_slave
1622 * anyway (it holds no special properties of the bond device),
1623 * so we can change it without calling change_active_interface()
1625 if (!bond->curr_active_slave && new_slave->link == BOND_LINK_UP)
1626 rcu_assign_pointer(bond->curr_active_slave, new_slave);
1628 break;
1629 } /* switch(bond_mode) */
1631 write_unlock_bh(&bond->curr_slave_lock);
1633 bond_set_carrier(bond);
1635 #ifdef CONFIG_NET_POLL_CONTROLLER
1636 slave_dev->npinfo = bond->dev->npinfo;
1637 if (slave_dev->npinfo) {
1638 if (slave_enable_netpoll(new_slave)) {
1639 read_unlock(&bond->lock);
1640 pr_info("Error, %s: master_dev is using netpoll, "
1641 "but new slave device does not support netpoll.\n",
1642 bond_dev->name);
1643 res = -EBUSY;
1644 goto err_detach;
1647 #endif
1649 read_unlock(&bond->lock);
1651 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1652 if (res)
1653 goto err_detach;
1655 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1656 new_slave);
1657 if (res) {
1658 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1659 goto err_dest_symlinks;
1662 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1663 bond_dev->name, slave_dev->name,
1664 bond_is_active_slave(new_slave) ? "n active" : " backup",
1665 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1667 /* enslave is successful */
1668 return 0;
1670 /* Undo stages on error */
1671 err_dest_symlinks:
1672 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1674 err_detach:
1675 if (!USES_PRIMARY(bond->params.mode))
1676 bond_hw_addr_flush(bond_dev, slave_dev);
1678 vlan_vids_del_by_dev(slave_dev, bond_dev);
1679 write_lock_bh(&bond->lock);
1680 bond_detach_slave(bond, new_slave);
1681 if (bond->primary_slave == new_slave)
1682 bond->primary_slave = NULL;
1683 if (bond->curr_active_slave == new_slave) {
1684 bond_change_active_slave(bond, NULL);
1685 write_unlock_bh(&bond->lock);
1686 read_lock(&bond->lock);
1687 write_lock_bh(&bond->curr_slave_lock);
1688 bond_select_active_slave(bond);
1689 write_unlock_bh(&bond->curr_slave_lock);
1690 read_unlock(&bond->lock);
1691 } else {
1692 write_unlock_bh(&bond->lock);
1694 slave_disable_netpoll(new_slave);
1696 err_close:
1697 slave_dev->priv_flags &= ~IFF_BONDING;
1698 dev_close(slave_dev);
1700 err_unset_master:
1701 bond_upper_dev_unlink(bond_dev, slave_dev);
1703 err_restore_mac:
1704 if (!bond->params.fail_over_mac) {
1705 /* XXX TODO - fom follow mode needs to change master's
1706 * MAC if this slave's MAC is in use by the bond, or at
1707 * least print a warning.
1709 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1710 addr.sa_family = slave_dev->type;
1711 dev_set_mac_address(slave_dev, &addr);
1714 err_restore_mtu:
1715 dev_set_mtu(slave_dev, new_slave->original_mtu);
1717 err_free:
1718 kfree(new_slave);
1720 err_undo_flags:
1721 bond_compute_features(bond);
1722 /* Enslave of first slave has failed and we need to fix master's mac */
1723 if (list_empty(&bond->slave_list) &&
1724 ether_addr_equal(bond_dev->dev_addr, slave_dev->dev_addr))
1725 eth_hw_addr_random(bond_dev);
1727 return res;
1731 * Try to release the slave device <slave> from the bond device <master>
1732 * It is legal to access curr_active_slave without a lock because all the function
1733 * is write-locked. If "all" is true it means that the function is being called
1734 * while destroying a bond interface and all slaves are being released.
1736 * The rules for slave state should be:
1737 * for Active/Backup:
1738 * Active stays on all backups go down
1739 * for Bonded connections:
1740 * The first up interface should be left on and all others downed.
1742 static int __bond_release_one(struct net_device *bond_dev,
1743 struct net_device *slave_dev,
1744 bool all)
1746 struct bonding *bond = netdev_priv(bond_dev);
1747 struct slave *slave, *oldcurrent;
1748 struct sockaddr addr;
1749 int old_flags = bond_dev->flags;
1750 netdev_features_t old_features = bond_dev->features;
1752 /* slave is not a slave or master is not master of this slave */
1753 if (!(slave_dev->flags & IFF_SLAVE) ||
1754 !netdev_has_upper_dev(slave_dev, bond_dev)) {
1755 pr_err("%s: Error: cannot release %s.\n",
1756 bond_dev->name, slave_dev->name);
1757 return -EINVAL;
1760 block_netpoll_tx();
1761 write_lock_bh(&bond->lock);
1763 slave = bond_get_slave_by_dev(bond, slave_dev);
1764 if (!slave) {
1765 /* not a slave of this bond */
1766 pr_info("%s: %s not enslaved\n",
1767 bond_dev->name, slave_dev->name);
1768 write_unlock_bh(&bond->lock);
1769 unblock_netpoll_tx();
1770 return -EINVAL;
1773 write_unlock_bh(&bond->lock);
1774 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1775 * for this slave anymore.
1777 netdev_rx_handler_unregister(slave_dev);
1778 write_lock_bh(&bond->lock);
1780 /* Inform AD package of unbinding of slave. */
1781 if (bond->params.mode == BOND_MODE_8023AD) {
1782 /* must be called before the slave is
1783 * detached from the list
1785 bond_3ad_unbind_slave(slave);
1788 pr_info("%s: releasing %s interface %s\n",
1789 bond_dev->name,
1790 bond_is_active_slave(slave) ? "active" : "backup",
1791 slave_dev->name);
1793 oldcurrent = bond->curr_active_slave;
1795 bond->current_arp_slave = NULL;
1797 /* release the slave from its bond */
1798 bond_detach_slave(bond, slave);
1800 if (!all && !bond->params.fail_over_mac) {
1801 if (ether_addr_equal(bond_dev->dev_addr, slave->perm_hwaddr) &&
1802 !list_empty(&bond->slave_list))
1803 pr_warn("%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",
1804 bond_dev->name, slave_dev->name,
1805 slave->perm_hwaddr,
1806 bond_dev->name, slave_dev->name);
1809 if (bond->primary_slave == slave)
1810 bond->primary_slave = NULL;
1812 if (oldcurrent == slave)
1813 bond_change_active_slave(bond, NULL);
1815 if (bond_is_lb(bond)) {
1816 /* Must be called only after the slave has been
1817 * detached from the list and the curr_active_slave
1818 * has been cleared (if our_slave == old_current),
1819 * but before a new active slave is selected.
1821 write_unlock_bh(&bond->lock);
1822 bond_alb_deinit_slave(bond, slave);
1823 write_lock_bh(&bond->lock);
1826 if (all) {
1827 rcu_assign_pointer(bond->curr_active_slave, NULL);
1828 } else if (oldcurrent == slave) {
1830 * Note that we hold RTNL over this sequence, so there
1831 * is no concern that another slave add/remove event
1832 * will interfere.
1834 write_unlock_bh(&bond->lock);
1835 read_lock(&bond->lock);
1836 write_lock_bh(&bond->curr_slave_lock);
1838 bond_select_active_slave(bond);
1840 write_unlock_bh(&bond->curr_slave_lock);
1841 read_unlock(&bond->lock);
1842 write_lock_bh(&bond->lock);
1845 if (list_empty(&bond->slave_list)) {
1846 bond_set_carrier(bond);
1847 eth_hw_addr_random(bond_dev);
1849 if (vlan_uses_dev(bond_dev)) {
1850 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
1851 bond_dev->name, bond_dev->name);
1852 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
1853 bond_dev->name);
1857 write_unlock_bh(&bond->lock);
1858 unblock_netpoll_tx();
1859 synchronize_rcu();
1861 if (list_empty(&bond->slave_list)) {
1862 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
1863 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
1866 bond_compute_features(bond);
1867 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1868 (old_features & NETIF_F_VLAN_CHALLENGED))
1869 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
1870 bond_dev->name, slave_dev->name, bond_dev->name);
1872 /* must do this from outside any spinlocks */
1873 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1875 vlan_vids_del_by_dev(slave_dev, bond_dev);
1877 /* If the mode USES_PRIMARY, then this cases was handled above by
1878 * bond_change_active_slave(..., NULL)
1880 if (!USES_PRIMARY(bond->params.mode)) {
1881 /* unset promiscuity level from slave
1882 * NOTE: The NETDEV_CHANGEADDR call above may change the value
1883 * of the IFF_PROMISC flag in the bond_dev, but we need the
1884 * value of that flag before that change, as that was the value
1885 * when this slave was attached, so we cache at the start of the
1886 * function and use it here. Same goes for ALLMULTI below
1888 if (old_flags & IFF_PROMISC)
1889 dev_set_promiscuity(slave_dev, -1);
1891 /* unset allmulti level from slave */
1892 if (old_flags & IFF_ALLMULTI)
1893 dev_set_allmulti(slave_dev, -1);
1895 bond_hw_addr_flush(bond_dev, slave_dev);
1898 bond_upper_dev_unlink(bond_dev, slave_dev);
1900 slave_disable_netpoll(slave);
1902 /* close slave before restoring its mac address */
1903 dev_close(slave_dev);
1905 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1906 /* restore original ("permanent") mac address */
1907 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1908 addr.sa_family = slave_dev->type;
1909 dev_set_mac_address(slave_dev, &addr);
1912 dev_set_mtu(slave_dev, slave->original_mtu);
1914 slave_dev->priv_flags &= ~IFF_BONDING;
1916 kfree(slave);
1918 return 0; /* deletion OK */
1921 /* A wrapper used because of ndo_del_link */
1922 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1924 return __bond_release_one(bond_dev, slave_dev, false);
1928 * First release a slave and then destroy the bond if no more slaves are left.
1929 * Must be under rtnl_lock when this function is called.
1931 static int bond_release_and_destroy(struct net_device *bond_dev,
1932 struct net_device *slave_dev)
1934 struct bonding *bond = netdev_priv(bond_dev);
1935 int ret;
1937 ret = bond_release(bond_dev, slave_dev);
1938 if (ret == 0 && list_empty(&bond->slave_list)) {
1939 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
1940 pr_info("%s: destroying bond %s.\n",
1941 bond_dev->name, bond_dev->name);
1942 bond_remove_proc_entry(bond);
1943 unregister_netdevice(bond_dev);
1945 return ret;
1949 * This function changes the active slave to slave <slave_dev>.
1950 * It returns -EINVAL in the following cases.
1951 * - <slave_dev> is not found in the list.
1952 * - There is not active slave now.
1953 * - <slave_dev> is already active.
1954 * - The link state of <slave_dev> is not BOND_LINK_UP.
1955 * - <slave_dev> is not running.
1956 * In these cases, this function does nothing.
1957 * In the other cases, current_slave pointer is changed and 0 is returned.
1959 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1961 struct bonding *bond = netdev_priv(bond_dev);
1962 struct slave *old_active = NULL;
1963 struct slave *new_active = NULL;
1964 int res = 0;
1966 if (!USES_PRIMARY(bond->params.mode))
1967 return -EINVAL;
1969 /* Verify that bond_dev is indeed the master of slave_dev */
1970 if (!(slave_dev->flags & IFF_SLAVE) ||
1971 !netdev_has_upper_dev(slave_dev, bond_dev))
1972 return -EINVAL;
1974 read_lock(&bond->lock);
1976 old_active = bond->curr_active_slave;
1977 new_active = bond_get_slave_by_dev(bond, slave_dev);
1979 * Changing to the current active: do nothing; return success.
1981 if (new_active && new_active == old_active) {
1982 read_unlock(&bond->lock);
1983 return 0;
1986 if (new_active &&
1987 old_active &&
1988 new_active->link == BOND_LINK_UP &&
1989 IS_UP(new_active->dev)) {
1990 block_netpoll_tx();
1991 write_lock_bh(&bond->curr_slave_lock);
1992 bond_change_active_slave(bond, new_active);
1993 write_unlock_bh(&bond->curr_slave_lock);
1994 unblock_netpoll_tx();
1995 } else
1996 res = -EINVAL;
1998 read_unlock(&bond->lock);
2000 return res;
2003 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2005 struct bonding *bond = netdev_priv(bond_dev);
2007 info->bond_mode = bond->params.mode;
2008 info->miimon = bond->params.miimon;
2010 read_lock(&bond->lock);
2011 info->num_slaves = bond->slave_cnt;
2012 read_unlock(&bond->lock);
2014 return 0;
2017 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2019 struct bonding *bond = netdev_priv(bond_dev);
2020 int i = 0, res = -ENODEV;
2021 struct slave *slave;
2023 read_lock(&bond->lock);
2024 bond_for_each_slave(bond, slave) {
2025 if (i++ == (int)info->slave_id) {
2026 res = 0;
2027 strcpy(info->slave_name, slave->dev->name);
2028 info->link = slave->link;
2029 info->state = bond_slave_state(slave);
2030 info->link_failure_count = slave->link_failure_count;
2031 break;
2034 read_unlock(&bond->lock);
2036 return res;
2039 /*-------------------------------- Monitoring -------------------------------*/
2042 static int bond_miimon_inspect(struct bonding *bond)
2044 int link_state, commit = 0;
2045 struct slave *slave;
2046 bool ignore_updelay;
2048 ignore_updelay = !bond->curr_active_slave ? true : false;
2050 bond_for_each_slave(bond, slave) {
2051 slave->new_link = BOND_LINK_NOCHANGE;
2053 link_state = bond_check_dev_link(bond, slave->dev, 0);
2055 switch (slave->link) {
2056 case BOND_LINK_UP:
2057 if (link_state)
2058 continue;
2060 slave->link = BOND_LINK_FAIL;
2061 slave->delay = bond->params.downdelay;
2062 if (slave->delay) {
2063 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2064 bond->dev->name,
2065 (bond->params.mode ==
2066 BOND_MODE_ACTIVEBACKUP) ?
2067 (bond_is_active_slave(slave) ?
2068 "active " : "backup ") : "",
2069 slave->dev->name,
2070 bond->params.downdelay * bond->params.miimon);
2072 /*FALLTHRU*/
2073 case BOND_LINK_FAIL:
2074 if (link_state) {
2076 * recovered before downdelay expired
2078 slave->link = BOND_LINK_UP;
2079 slave->jiffies = jiffies;
2080 pr_info("%s: link status up again after %d ms for interface %s.\n",
2081 bond->dev->name,
2082 (bond->params.downdelay - slave->delay) *
2083 bond->params.miimon,
2084 slave->dev->name);
2085 continue;
2088 if (slave->delay <= 0) {
2089 slave->new_link = BOND_LINK_DOWN;
2090 commit++;
2091 continue;
2094 slave->delay--;
2095 break;
2097 case BOND_LINK_DOWN:
2098 if (!link_state)
2099 continue;
2101 slave->link = BOND_LINK_BACK;
2102 slave->delay = bond->params.updelay;
2104 if (slave->delay) {
2105 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2106 bond->dev->name, slave->dev->name,
2107 ignore_updelay ? 0 :
2108 bond->params.updelay *
2109 bond->params.miimon);
2111 /*FALLTHRU*/
2112 case BOND_LINK_BACK:
2113 if (!link_state) {
2114 slave->link = BOND_LINK_DOWN;
2115 pr_info("%s: link status down again after %d ms for interface %s.\n",
2116 bond->dev->name,
2117 (bond->params.updelay - slave->delay) *
2118 bond->params.miimon,
2119 slave->dev->name);
2121 continue;
2124 if (ignore_updelay)
2125 slave->delay = 0;
2127 if (slave->delay <= 0) {
2128 slave->new_link = BOND_LINK_UP;
2129 commit++;
2130 ignore_updelay = false;
2131 continue;
2134 slave->delay--;
2135 break;
2139 return commit;
2142 static void bond_miimon_commit(struct bonding *bond)
2144 struct slave *slave;
2146 bond_for_each_slave(bond, slave) {
2147 switch (slave->new_link) {
2148 case BOND_LINK_NOCHANGE:
2149 continue;
2151 case BOND_LINK_UP:
2152 slave->link = BOND_LINK_UP;
2153 slave->jiffies = jiffies;
2155 if (bond->params.mode == BOND_MODE_8023AD) {
2156 /* prevent it from being the active one */
2157 bond_set_backup_slave(slave);
2158 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2159 /* make it immediately active */
2160 bond_set_active_slave(slave);
2161 } else if (slave != bond->primary_slave) {
2162 /* prevent it from being the active one */
2163 bond_set_backup_slave(slave);
2166 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
2167 bond->dev->name, slave->dev->name,
2168 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2169 slave->duplex ? "full" : "half");
2171 /* notify ad that the link status has changed */
2172 if (bond->params.mode == BOND_MODE_8023AD)
2173 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2175 if (bond_is_lb(bond))
2176 bond_alb_handle_link_change(bond, slave,
2177 BOND_LINK_UP);
2179 if (!bond->curr_active_slave ||
2180 (slave == bond->primary_slave))
2181 goto do_failover;
2183 continue;
2185 case BOND_LINK_DOWN:
2186 if (slave->link_failure_count < UINT_MAX)
2187 slave->link_failure_count++;
2189 slave->link = BOND_LINK_DOWN;
2191 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2192 bond->params.mode == BOND_MODE_8023AD)
2193 bond_set_slave_inactive_flags(slave);
2195 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2196 bond->dev->name, slave->dev->name);
2198 if (bond->params.mode == BOND_MODE_8023AD)
2199 bond_3ad_handle_link_change(slave,
2200 BOND_LINK_DOWN);
2202 if (bond_is_lb(bond))
2203 bond_alb_handle_link_change(bond, slave,
2204 BOND_LINK_DOWN);
2206 if (slave == bond->curr_active_slave)
2207 goto do_failover;
2209 continue;
2211 default:
2212 pr_err("%s: invalid new link %d on slave %s\n",
2213 bond->dev->name, slave->new_link,
2214 slave->dev->name);
2215 slave->new_link = BOND_LINK_NOCHANGE;
2217 continue;
2220 do_failover:
2221 ASSERT_RTNL();
2222 block_netpoll_tx();
2223 write_lock_bh(&bond->curr_slave_lock);
2224 bond_select_active_slave(bond);
2225 write_unlock_bh(&bond->curr_slave_lock);
2226 unblock_netpoll_tx();
2229 bond_set_carrier(bond);
2233 * bond_mii_monitor
2235 * Really a wrapper that splits the mii monitor into two phases: an
2236 * inspection, then (if inspection indicates something needs to be done)
2237 * an acquisition of appropriate locks followed by a commit phase to
2238 * implement whatever link state changes are indicated.
2240 void bond_mii_monitor(struct work_struct *work)
2242 struct bonding *bond = container_of(work, struct bonding,
2243 mii_work.work);
2244 bool should_notify_peers = false;
2245 unsigned long delay;
2247 read_lock(&bond->lock);
2249 delay = msecs_to_jiffies(bond->params.miimon);
2251 if (list_empty(&bond->slave_list))
2252 goto re_arm;
2254 should_notify_peers = bond_should_notify_peers(bond);
2256 if (bond_miimon_inspect(bond)) {
2257 read_unlock(&bond->lock);
2259 /* Race avoidance with bond_close cancel of workqueue */
2260 if (!rtnl_trylock()) {
2261 read_lock(&bond->lock);
2262 delay = 1;
2263 should_notify_peers = false;
2264 goto re_arm;
2267 read_lock(&bond->lock);
2269 bond_miimon_commit(bond);
2271 read_unlock(&bond->lock);
2272 rtnl_unlock(); /* might sleep, hold no other locks */
2273 read_lock(&bond->lock);
2276 re_arm:
2277 if (bond->params.miimon)
2278 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2280 read_unlock(&bond->lock);
2282 if (should_notify_peers) {
2283 if (!rtnl_trylock())
2284 return;
2285 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
2286 rtnl_unlock();
2290 static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2292 struct net_device *upper;
2293 struct list_head *iter;
2294 bool ret = false;
2296 if (ip == bond_confirm_addr(bond->dev, 0, ip))
2297 return true;
2299 rcu_read_lock();
2300 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) {
2301 if (ip == bond_confirm_addr(upper, 0, ip)) {
2302 ret = true;
2303 break;
2306 rcu_read_unlock();
2308 return ret;
2312 * We go to the (large) trouble of VLAN tagging ARP frames because
2313 * switches in VLAN mode (especially if ports are configured as
2314 * "native" to a VLAN) might not pass non-tagged frames.
2316 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2318 struct sk_buff *skb;
2320 pr_debug("arp %d on slave %s: dst %pI4 src %pI4 vid %d\n", arp_op,
2321 slave_dev->name, &dest_ip, &src_ip, vlan_id);
2323 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2324 NULL, slave_dev->dev_addr, NULL);
2326 if (!skb) {
2327 pr_err("ARP packet allocation failed\n");
2328 return;
2330 if (vlan_id) {
2331 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), vlan_id);
2332 if (!skb) {
2333 pr_err("failed to insert VLAN tag\n");
2334 return;
2337 arp_xmit(skb);
2341 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2343 struct net_device *upper, *vlan_upper;
2344 struct list_head *iter, *vlan_iter;
2345 struct rtable *rt;
2346 __be32 *targets = bond->params.arp_targets, addr;
2347 int i, vlan_id;
2349 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
2350 pr_debug("basa: target %pI4\n", &targets[i]);
2352 /* Find out through which dev should the packet go */
2353 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2354 RTO_ONLINK, 0);
2355 if (IS_ERR(rt)) {
2356 pr_debug("%s: no route to arp_ip_target %pI4\n",
2357 bond->dev->name, &targets[i]);
2358 continue;
2361 vlan_id = 0;
2363 /* bond device itself */
2364 if (rt->dst.dev == bond->dev)
2365 goto found;
2367 rcu_read_lock();
2368 /* first we search only for vlan devices. for every vlan
2369 * found we verify its upper dev list, searching for the
2370 * rt->dst.dev. If found we save the tag of the vlan and
2371 * proceed to send the packet.
2373 * TODO: QinQ?
2375 netdev_for_each_upper_dev_rcu(bond->dev, vlan_upper, vlan_iter) {
2376 if (!is_vlan_dev(vlan_upper))
2377 continue;
2378 netdev_for_each_upper_dev_rcu(vlan_upper, upper, iter) {
2379 if (upper == rt->dst.dev) {
2380 vlan_id = vlan_dev_vlan_id(vlan_upper);
2381 rcu_read_unlock();
2382 goto found;
2387 /* if the device we're looking for is not on top of any of
2388 * our upper vlans, then just search for any dev that
2389 * matches, and in case it's a vlan - save the id
2391 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) {
2392 if (upper == rt->dst.dev) {
2393 /* if it's a vlan - get its VID */
2394 if (is_vlan_dev(upper))
2395 vlan_id = vlan_dev_vlan_id(upper);
2397 rcu_read_unlock();
2398 goto found;
2401 rcu_read_unlock();
2403 /* Not our device - skip */
2404 pr_debug("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2405 bond->dev->name, &targets[i],
2406 rt->dst.dev ? rt->dst.dev->name : "NULL");
2408 ip_rt_put(rt);
2409 continue;
2411 found:
2412 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
2413 ip_rt_put(rt);
2414 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2415 addr, vlan_id);
2419 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2421 int i;
2423 if (!sip || !bond_has_this_ip(bond, tip)) {
2424 pr_debug("bva: sip %pI4 tip %pI4 not found\n", &sip, &tip);
2425 return;
2428 i = bond_get_targets_ip(bond->params.arp_targets, sip);
2429 if (i == -1) {
2430 pr_debug("bva: sip %pI4 not found in targets\n", &sip);
2431 return;
2433 slave->last_arp_rx = jiffies;
2434 slave->target_last_arp_rx[i] = jiffies;
2437 int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
2438 struct slave *slave)
2440 struct arphdr *arp = (struct arphdr *)skb->data;
2441 struct slave *curr_active_slave, *curr_arp_slave;
2442 unsigned char *arp_ptr;
2443 __be32 sip, tip;
2444 int alen;
2446 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2447 return RX_HANDLER_ANOTHER;
2449 read_lock(&bond->lock);
2451 if (!slave_do_arp_validate(bond, slave))
2452 goto out_unlock;
2454 alen = arp_hdr_len(bond->dev);
2456 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2457 bond->dev->name, skb->dev->name);
2459 if (alen > skb_headlen(skb)) {
2460 arp = kmalloc(alen, GFP_ATOMIC);
2461 if (!arp)
2462 goto out_unlock;
2463 if (skb_copy_bits(skb, 0, arp, alen) < 0)
2464 goto out_unlock;
2467 if (arp->ar_hln != bond->dev->addr_len ||
2468 skb->pkt_type == PACKET_OTHERHOST ||
2469 skb->pkt_type == PACKET_LOOPBACK ||
2470 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2471 arp->ar_pro != htons(ETH_P_IP) ||
2472 arp->ar_pln != 4)
2473 goto out_unlock;
2475 arp_ptr = (unsigned char *)(arp + 1);
2476 arp_ptr += bond->dev->addr_len;
2477 memcpy(&sip, arp_ptr, 4);
2478 arp_ptr += 4 + bond->dev->addr_len;
2479 memcpy(&tip, arp_ptr, 4);
2481 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2482 bond->dev->name, slave->dev->name, bond_slave_state(slave),
2483 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2484 &sip, &tip);
2486 curr_active_slave = rcu_dereference(bond->curr_active_slave);
2487 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
2489 /* We 'trust' the received ARP enough to validate it if:
2491 * (a) the slave receiving the ARP is active (which includes the
2492 * current ARP slave, if any), or
2494 * (b) the receiving slave isn't active, but there is a currently
2495 * active slave and it received valid arp reply(s) after it became
2496 * the currently active slave, or
2498 * (c) there is an ARP slave that sent an ARP during the prior ARP
2499 * interval, and we receive an ARP reply on any slave. We accept
2500 * these because switch FDB update delays may deliver the ARP
2501 * reply to a slave other than the sender of the ARP request.
2503 * Note: for (b), backup slaves are receiving the broadcast ARP
2504 * request, not a reply. This request passes from the sending
2505 * slave through the L2 switch(es) to the receiving slave. Since
2506 * this is checking the request, sip/tip are swapped for
2507 * validation.
2509 * This is done to avoid endless looping when we can't reach the
2510 * arp_ip_target and fool ourselves with our own arp requests.
2512 if (bond_is_active_slave(slave))
2513 bond_validate_arp(bond, slave, sip, tip);
2514 else if (curr_active_slave &&
2515 time_after(slave_last_rx(bond, curr_active_slave),
2516 curr_active_slave->jiffies))
2517 bond_validate_arp(bond, slave, tip, sip);
2518 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
2519 bond_time_in_interval(bond,
2520 dev_trans_start(curr_arp_slave->dev), 1))
2521 bond_validate_arp(bond, slave, sip, tip);
2523 out_unlock:
2524 read_unlock(&bond->lock);
2525 if (arp != (struct arphdr *)skb->data)
2526 kfree(arp);
2527 return RX_HANDLER_ANOTHER;
2530 /* function to verify if we're in the arp_interval timeslice, returns true if
2531 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
2532 * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
2534 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
2535 int mod)
2537 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2539 return time_in_range(jiffies,
2540 last_act - delta_in_ticks,
2541 last_act + mod * delta_in_ticks + delta_in_ticks/2);
2545 * this function is called regularly to monitor each slave's link
2546 * ensuring that traffic is being sent and received when arp monitoring
2547 * is used in load-balancing mode. if the adapter has been dormant, then an
2548 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2549 * arp monitoring in active backup mode.
2551 void bond_loadbalance_arp_mon(struct work_struct *work)
2553 struct bonding *bond = container_of(work, struct bonding,
2554 arp_work.work);
2555 struct slave *slave, *oldcurrent;
2556 int do_failover = 0;
2558 read_lock(&bond->lock);
2560 if (list_empty(&bond->slave_list))
2561 goto re_arm;
2563 oldcurrent = bond->curr_active_slave;
2564 /* see if any of the previous devices are up now (i.e. they have
2565 * xmt and rcv traffic). the curr_active_slave does not come into
2566 * the picture unless it is null. also, slave->jiffies is not needed
2567 * here because we send an arp on each slave and give a slave as
2568 * long as it needs to get the tx/rx within the delta.
2569 * TODO: what about up/down delay in arp mode? it wasn't here before
2570 * so it can wait
2572 bond_for_each_slave(bond, slave) {
2573 unsigned long trans_start = dev_trans_start(slave->dev);
2575 if (slave->link != BOND_LINK_UP) {
2576 if (bond_time_in_interval(bond, trans_start, 1) &&
2577 bond_time_in_interval(bond, slave->dev->last_rx, 1)) {
2579 slave->link = BOND_LINK_UP;
2580 bond_set_active_slave(slave);
2582 /* primary_slave has no meaning in round-robin
2583 * mode. the window of a slave being up and
2584 * curr_active_slave being null after enslaving
2585 * is closed.
2587 if (!oldcurrent) {
2588 pr_info("%s: link status definitely up for interface %s, ",
2589 bond->dev->name,
2590 slave->dev->name);
2591 do_failover = 1;
2592 } else {
2593 pr_info("%s: interface %s is now up\n",
2594 bond->dev->name,
2595 slave->dev->name);
2598 } else {
2599 /* slave->link == BOND_LINK_UP */
2601 /* not all switches will respond to an arp request
2602 * when the source ip is 0, so don't take the link down
2603 * if we don't know our ip yet
2605 if (!bond_time_in_interval(bond, trans_start, 2) ||
2606 !bond_time_in_interval(bond, slave->dev->last_rx, 2)) {
2608 slave->link = BOND_LINK_DOWN;
2609 bond_set_backup_slave(slave);
2611 if (slave->link_failure_count < UINT_MAX)
2612 slave->link_failure_count++;
2614 pr_info("%s: interface %s is now down.\n",
2615 bond->dev->name,
2616 slave->dev->name);
2618 if (slave == oldcurrent)
2619 do_failover = 1;
2623 /* note: if switch is in round-robin mode, all links
2624 * must tx arp to ensure all links rx an arp - otherwise
2625 * links may oscillate or not come up at all; if switch is
2626 * in something like xor mode, there is nothing we can
2627 * do - all replies will be rx'ed on same link causing slaves
2628 * to be unstable during low/no traffic periods
2630 if (IS_UP(slave->dev))
2631 bond_arp_send_all(bond, slave);
2634 if (do_failover) {
2635 block_netpoll_tx();
2636 write_lock_bh(&bond->curr_slave_lock);
2638 bond_select_active_slave(bond);
2640 write_unlock_bh(&bond->curr_slave_lock);
2641 unblock_netpoll_tx();
2644 re_arm:
2645 if (bond->params.arp_interval)
2646 queue_delayed_work(bond->wq, &bond->arp_work,
2647 msecs_to_jiffies(bond->params.arp_interval));
2649 read_unlock(&bond->lock);
2653 * Called to inspect slaves for active-backup mode ARP monitor link state
2654 * changes. Sets new_link in slaves to specify what action should take
2655 * place for the slave. Returns 0 if no changes are found, >0 if changes
2656 * to link states must be committed.
2658 * Called with bond->lock held for read.
2660 static int bond_ab_arp_inspect(struct bonding *bond)
2662 unsigned long trans_start, last_rx;
2663 struct slave *slave;
2664 int commit = 0;
2666 bond_for_each_slave(bond, slave) {
2667 slave->new_link = BOND_LINK_NOCHANGE;
2668 last_rx = slave_last_rx(bond, slave);
2670 if (slave->link != BOND_LINK_UP) {
2671 if (bond_time_in_interval(bond, last_rx, 1)) {
2672 slave->new_link = BOND_LINK_UP;
2673 commit++;
2675 continue;
2679 * Give slaves 2*delta after being enslaved or made
2680 * active. This avoids bouncing, as the last receive
2681 * times need a full ARP monitor cycle to be updated.
2683 if (bond_time_in_interval(bond, slave->jiffies, 2))
2684 continue;
2687 * Backup slave is down if:
2688 * - No current_arp_slave AND
2689 * - more than 3*delta since last receive AND
2690 * - the bond has an IP address
2692 * Note: a non-null current_arp_slave indicates
2693 * the curr_active_slave went down and we are
2694 * searching for a new one; under this condition
2695 * we only take the curr_active_slave down - this
2696 * gives each slave a chance to tx/rx traffic
2697 * before being taken out
2699 if (!bond_is_active_slave(slave) &&
2700 !bond->current_arp_slave &&
2701 !bond_time_in_interval(bond, last_rx, 3)) {
2702 slave->new_link = BOND_LINK_DOWN;
2703 commit++;
2707 * Active slave is down if:
2708 * - more than 2*delta since transmitting OR
2709 * - (more than 2*delta since receive AND
2710 * the bond has an IP address)
2712 trans_start = dev_trans_start(slave->dev);
2713 if (bond_is_active_slave(slave) &&
2714 (!bond_time_in_interval(bond, trans_start, 2) ||
2715 !bond_time_in_interval(bond, last_rx, 2))) {
2716 slave->new_link = BOND_LINK_DOWN;
2717 commit++;
2721 return commit;
2725 * Called to commit link state changes noted by inspection step of
2726 * active-backup mode ARP monitor.
2728 * Called with RTNL and bond->lock for read.
2730 static void bond_ab_arp_commit(struct bonding *bond)
2732 unsigned long trans_start;
2733 struct slave *slave;
2735 bond_for_each_slave(bond, slave) {
2736 switch (slave->new_link) {
2737 case BOND_LINK_NOCHANGE:
2738 continue;
2740 case BOND_LINK_UP:
2741 trans_start = dev_trans_start(slave->dev);
2742 if (bond->curr_active_slave != slave ||
2743 (!bond->curr_active_slave &&
2744 bond_time_in_interval(bond, trans_start, 1))) {
2745 slave->link = BOND_LINK_UP;
2746 if (bond->current_arp_slave) {
2747 bond_set_slave_inactive_flags(
2748 bond->current_arp_slave);
2749 bond->current_arp_slave = NULL;
2752 pr_info("%s: link status definitely up for interface %s.\n",
2753 bond->dev->name, slave->dev->name);
2755 if (!bond->curr_active_slave ||
2756 (slave == bond->primary_slave))
2757 goto do_failover;
2761 continue;
2763 case BOND_LINK_DOWN:
2764 if (slave->link_failure_count < UINT_MAX)
2765 slave->link_failure_count++;
2767 slave->link = BOND_LINK_DOWN;
2768 bond_set_slave_inactive_flags(slave);
2770 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2771 bond->dev->name, slave->dev->name);
2773 if (slave == bond->curr_active_slave) {
2774 bond->current_arp_slave = NULL;
2775 goto do_failover;
2778 continue;
2780 default:
2781 pr_err("%s: impossible: new_link %d on slave %s\n",
2782 bond->dev->name, slave->new_link,
2783 slave->dev->name);
2784 continue;
2787 do_failover:
2788 ASSERT_RTNL();
2789 block_netpoll_tx();
2790 write_lock_bh(&bond->curr_slave_lock);
2791 bond_select_active_slave(bond);
2792 write_unlock_bh(&bond->curr_slave_lock);
2793 unblock_netpoll_tx();
2796 bond_set_carrier(bond);
2800 * Send ARP probes for active-backup mode ARP monitor.
2802 * Called with bond->lock held for read.
2804 static void bond_ab_arp_probe(struct bonding *bond)
2806 struct slave *slave, *next_slave;
2807 int i;
2809 read_lock(&bond->curr_slave_lock);
2811 if (bond->current_arp_slave && bond->curr_active_slave)
2812 pr_info("PROBE: c_arp %s && cas %s BAD\n",
2813 bond->current_arp_slave->dev->name,
2814 bond->curr_active_slave->dev->name);
2816 if (bond->curr_active_slave) {
2817 bond_arp_send_all(bond, bond->curr_active_slave);
2818 read_unlock(&bond->curr_slave_lock);
2819 return;
2822 read_unlock(&bond->curr_slave_lock);
2824 /* if we don't have a curr_active_slave, search for the next available
2825 * backup slave from the current_arp_slave and make it the candidate
2826 * for becoming the curr_active_slave
2829 if (!bond->current_arp_slave) {
2830 bond->current_arp_slave = bond_first_slave(bond);
2831 if (!bond->current_arp_slave)
2832 return;
2835 bond_set_slave_inactive_flags(bond->current_arp_slave);
2837 /* search for next candidate */
2838 next_slave = bond_next_slave(bond, bond->current_arp_slave);
2839 bond_for_each_slave_from(bond, slave, i, next_slave) {
2840 if (IS_UP(slave->dev)) {
2841 slave->link = BOND_LINK_BACK;
2842 bond_set_slave_active_flags(slave);
2843 bond_arp_send_all(bond, slave);
2844 slave->jiffies = jiffies;
2845 bond->current_arp_slave = slave;
2846 break;
2849 /* if the link state is up at this point, we
2850 * mark it down - this can happen if we have
2851 * simultaneous link failures and
2852 * reselect_active_interface doesn't make this
2853 * one the current slave so it is still marked
2854 * up when it is actually down
2856 if (slave->link == BOND_LINK_UP) {
2857 slave->link = BOND_LINK_DOWN;
2858 if (slave->link_failure_count < UINT_MAX)
2859 slave->link_failure_count++;
2861 bond_set_slave_inactive_flags(slave);
2863 pr_info("%s: backup interface %s is now down.\n",
2864 bond->dev->name, slave->dev->name);
2869 void bond_activebackup_arp_mon(struct work_struct *work)
2871 struct bonding *bond = container_of(work, struct bonding,
2872 arp_work.work);
2873 bool should_notify_peers = false;
2874 int delta_in_ticks;
2876 read_lock(&bond->lock);
2878 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2880 if (list_empty(&bond->slave_list))
2881 goto re_arm;
2883 should_notify_peers = bond_should_notify_peers(bond);
2885 if (bond_ab_arp_inspect(bond)) {
2886 read_unlock(&bond->lock);
2888 /* Race avoidance with bond_close flush of workqueue */
2889 if (!rtnl_trylock()) {
2890 read_lock(&bond->lock);
2891 delta_in_ticks = 1;
2892 should_notify_peers = false;
2893 goto re_arm;
2896 read_lock(&bond->lock);
2898 bond_ab_arp_commit(bond);
2900 read_unlock(&bond->lock);
2901 rtnl_unlock();
2902 read_lock(&bond->lock);
2905 bond_ab_arp_probe(bond);
2907 re_arm:
2908 if (bond->params.arp_interval)
2909 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2911 read_unlock(&bond->lock);
2913 if (should_notify_peers) {
2914 if (!rtnl_trylock())
2915 return;
2916 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
2917 rtnl_unlock();
2921 /*-------------------------- netdev event handling --------------------------*/
2924 * Change device name
2926 static int bond_event_changename(struct bonding *bond)
2928 bond_remove_proc_entry(bond);
2929 bond_create_proc_entry(bond);
2931 bond_debug_reregister(bond);
2933 return NOTIFY_DONE;
2936 static int bond_master_netdev_event(unsigned long event,
2937 struct net_device *bond_dev)
2939 struct bonding *event_bond = netdev_priv(bond_dev);
2941 switch (event) {
2942 case NETDEV_CHANGENAME:
2943 return bond_event_changename(event_bond);
2944 case NETDEV_UNREGISTER:
2945 bond_remove_proc_entry(event_bond);
2946 break;
2947 case NETDEV_REGISTER:
2948 bond_create_proc_entry(event_bond);
2949 break;
2950 case NETDEV_NOTIFY_PEERS:
2951 if (event_bond->send_peer_notif)
2952 event_bond->send_peer_notif--;
2953 break;
2954 default:
2955 break;
2958 return NOTIFY_DONE;
2961 static int bond_slave_netdev_event(unsigned long event,
2962 struct net_device *slave_dev)
2964 struct slave *slave = bond_slave_get_rtnl(slave_dev);
2965 struct bonding *bond;
2966 struct net_device *bond_dev;
2967 u32 old_speed;
2968 u8 old_duplex;
2970 /* A netdev event can be generated while enslaving a device
2971 * before netdev_rx_handler_register is called in which case
2972 * slave will be NULL
2974 if (!slave)
2975 return NOTIFY_DONE;
2976 bond_dev = slave->bond->dev;
2977 bond = slave->bond;
2979 switch (event) {
2980 case NETDEV_UNREGISTER:
2981 if (bond_dev->type != ARPHRD_ETHER)
2982 bond_release_and_destroy(bond_dev, slave_dev);
2983 else
2984 bond_release(bond_dev, slave_dev);
2985 break;
2986 case NETDEV_UP:
2987 case NETDEV_CHANGE:
2988 old_speed = slave->speed;
2989 old_duplex = slave->duplex;
2991 bond_update_speed_duplex(slave);
2993 if (bond->params.mode == BOND_MODE_8023AD) {
2994 if (old_speed != slave->speed)
2995 bond_3ad_adapter_speed_changed(slave);
2996 if (old_duplex != slave->duplex)
2997 bond_3ad_adapter_duplex_changed(slave);
2999 break;
3000 case NETDEV_DOWN:
3002 * ... Or is it this?
3004 break;
3005 case NETDEV_CHANGEMTU:
3007 * TODO: Should slaves be allowed to
3008 * independently alter their MTU? For
3009 * an active-backup bond, slaves need
3010 * not be the same type of device, so
3011 * MTUs may vary. For other modes,
3012 * slaves arguably should have the
3013 * same MTUs. To do this, we'd need to
3014 * take over the slave's change_mtu
3015 * function for the duration of their
3016 * servitude.
3018 break;
3019 case NETDEV_CHANGENAME:
3021 * TODO: handle changing the primary's name
3023 break;
3024 case NETDEV_FEAT_CHANGE:
3025 bond_compute_features(bond);
3026 break;
3027 case NETDEV_RESEND_IGMP:
3028 /* Propagate to master device */
3029 call_netdevice_notifiers(event, slave->bond->dev);
3030 break;
3031 default:
3032 break;
3035 return NOTIFY_DONE;
3039 * bond_netdev_event: handle netdev notifier chain events.
3041 * This function receives events for the netdev chain. The caller (an
3042 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3043 * locks for us to safely manipulate the slave devices (RTNL lock,
3044 * dev_probe_lock).
3046 static int bond_netdev_event(struct notifier_block *this,
3047 unsigned long event, void *ptr)
3049 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
3051 pr_debug("event_dev: %s, event: %lx\n",
3052 event_dev ? event_dev->name : "None",
3053 event);
3055 if (!(event_dev->priv_flags & IFF_BONDING))
3056 return NOTIFY_DONE;
3058 if (event_dev->flags & IFF_MASTER) {
3059 pr_debug("IFF_MASTER\n");
3060 return bond_master_netdev_event(event, event_dev);
3063 if (event_dev->flags & IFF_SLAVE) {
3064 pr_debug("IFF_SLAVE\n");
3065 return bond_slave_netdev_event(event, event_dev);
3068 return NOTIFY_DONE;
3071 static struct notifier_block bond_netdev_notifier = {
3072 .notifier_call = bond_netdev_event,
3075 /*---------------------------- Hashing Policies -----------------------------*/
3078 * Hash for the output device based upon layer 2 data
3080 static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
3082 struct ethhdr *data = (struct ethhdr *)skb->data;
3084 if (skb_headlen(skb) >= offsetof(struct ethhdr, h_proto))
3085 return (data->h_dest[5] ^ data->h_source[5]) % count;
3087 return 0;
3091 * Hash for the output device based upon layer 2 and layer 3 data. If
3092 * the packet is not IP, fall back on bond_xmit_hash_policy_l2()
3094 static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
3096 const struct ethhdr *data;
3097 const struct iphdr *iph;
3098 const struct ipv6hdr *ipv6h;
3099 u32 v6hash;
3100 const __be32 *s, *d;
3102 if (skb->protocol == htons(ETH_P_IP) &&
3103 pskb_network_may_pull(skb, sizeof(*iph))) {
3104 iph = ip_hdr(skb);
3105 data = (struct ethhdr *)skb->data;
3106 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3107 (data->h_dest[5] ^ data->h_source[5])) % count;
3108 } else if (skb->protocol == htons(ETH_P_IPV6) &&
3109 pskb_network_may_pull(skb, sizeof(*ipv6h))) {
3110 ipv6h = ipv6_hdr(skb);
3111 data = (struct ethhdr *)skb->data;
3112 s = &ipv6h->saddr.s6_addr32[0];
3113 d = &ipv6h->daddr.s6_addr32[0];
3114 v6hash = (s[1] ^ d[1]) ^ (s[2] ^ d[2]) ^ (s[3] ^ d[3]);
3115 v6hash ^= (v6hash >> 24) ^ (v6hash >> 16) ^ (v6hash >> 8);
3116 return (v6hash ^ data->h_dest[5] ^ data->h_source[5]) % count;
3119 return bond_xmit_hash_policy_l2(skb, count);
3123 * Hash for the output device based upon layer 3 and layer 4 data. If
3124 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3125 * altogether not IP, fall back on bond_xmit_hash_policy_l2()
3127 static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
3129 u32 layer4_xor = 0;
3130 const struct iphdr *iph;
3131 const struct ipv6hdr *ipv6h;
3132 const __be32 *s, *d;
3133 const __be16 *l4 = NULL;
3134 __be16 _l4[2];
3135 int noff = skb_network_offset(skb);
3136 int poff;
3138 if (skb->protocol == htons(ETH_P_IP) &&
3139 pskb_may_pull(skb, noff + sizeof(*iph))) {
3140 iph = ip_hdr(skb);
3141 poff = proto_ports_offset(iph->protocol);
3143 if (!ip_is_fragment(iph) && poff >= 0) {
3144 l4 = skb_header_pointer(skb, noff + (iph->ihl << 2) + poff,
3145 sizeof(_l4), &_l4);
3146 if (l4)
3147 layer4_xor = ntohs(l4[0] ^ l4[1]);
3149 return (layer4_xor ^
3150 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3151 } else if (skb->protocol == htons(ETH_P_IPV6) &&
3152 pskb_may_pull(skb, noff + sizeof(*ipv6h))) {
3153 ipv6h = ipv6_hdr(skb);
3154 poff = proto_ports_offset(ipv6h->nexthdr);
3155 if (poff >= 0) {
3156 l4 = skb_header_pointer(skb, noff + sizeof(*ipv6h) + poff,
3157 sizeof(_l4), &_l4);
3158 if (l4)
3159 layer4_xor = ntohs(l4[0] ^ l4[1]);
3161 s = &ipv6h->saddr.s6_addr32[0];
3162 d = &ipv6h->daddr.s6_addr32[0];
3163 layer4_xor ^= (s[1] ^ d[1]) ^ (s[2] ^ d[2]) ^ (s[3] ^ d[3]);
3164 layer4_xor ^= (layer4_xor >> 24) ^ (layer4_xor >> 16) ^
3165 (layer4_xor >> 8);
3166 return layer4_xor % count;
3169 return bond_xmit_hash_policy_l2(skb, count);
3172 /*-------------------------- Device entry points ----------------------------*/
3174 static void bond_work_init_all(struct bonding *bond)
3176 INIT_DELAYED_WORK(&bond->mcast_work,
3177 bond_resend_igmp_join_requests_delayed);
3178 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3179 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3180 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3181 INIT_DELAYED_WORK(&bond->arp_work, bond_activebackup_arp_mon);
3182 else
3183 INIT_DELAYED_WORK(&bond->arp_work, bond_loadbalance_arp_mon);
3184 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3187 static void bond_work_cancel_all(struct bonding *bond)
3189 cancel_delayed_work_sync(&bond->mii_work);
3190 cancel_delayed_work_sync(&bond->arp_work);
3191 cancel_delayed_work_sync(&bond->alb_work);
3192 cancel_delayed_work_sync(&bond->ad_work);
3193 cancel_delayed_work_sync(&bond->mcast_work);
3196 static int bond_open(struct net_device *bond_dev)
3198 struct bonding *bond = netdev_priv(bond_dev);
3199 struct slave *slave;
3201 /* reset slave->backup and slave->inactive */
3202 read_lock(&bond->lock);
3203 if (!list_empty(&bond->slave_list)) {
3204 read_lock(&bond->curr_slave_lock);
3205 bond_for_each_slave(bond, slave) {
3206 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3207 && (slave != bond->curr_active_slave)) {
3208 bond_set_slave_inactive_flags(slave);
3209 } else {
3210 bond_set_slave_active_flags(slave);
3213 read_unlock(&bond->curr_slave_lock);
3215 read_unlock(&bond->lock);
3217 bond_work_init_all(bond);
3219 if (bond_is_lb(bond)) {
3220 /* bond_alb_initialize must be called before the timer
3221 * is started.
3223 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB)))
3224 return -ENOMEM;
3225 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3228 if (bond->params.miimon) /* link check interval, in milliseconds. */
3229 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3231 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3232 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3233 if (bond->params.arp_validate)
3234 bond->recv_probe = bond_arp_rcv;
3237 if (bond->params.mode == BOND_MODE_8023AD) {
3238 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3239 /* register to receive LACPDUs */
3240 bond->recv_probe = bond_3ad_lacpdu_recv;
3241 bond_3ad_initiate_agg_selection(bond, 1);
3244 return 0;
3247 static int bond_close(struct net_device *bond_dev)
3249 struct bonding *bond = netdev_priv(bond_dev);
3251 bond_work_cancel_all(bond);
3252 bond->send_peer_notif = 0;
3253 if (bond_is_lb(bond))
3254 bond_alb_deinitialize(bond);
3255 bond->recv_probe = NULL;
3257 return 0;
3260 static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3261 struct rtnl_link_stats64 *stats)
3263 struct bonding *bond = netdev_priv(bond_dev);
3264 struct rtnl_link_stats64 temp;
3265 struct slave *slave;
3267 memset(stats, 0, sizeof(*stats));
3269 read_lock_bh(&bond->lock);
3270 bond_for_each_slave(bond, slave) {
3271 const struct rtnl_link_stats64 *sstats =
3272 dev_get_stats(slave->dev, &temp);
3274 stats->rx_packets += sstats->rx_packets;
3275 stats->rx_bytes += sstats->rx_bytes;
3276 stats->rx_errors += sstats->rx_errors;
3277 stats->rx_dropped += sstats->rx_dropped;
3279 stats->tx_packets += sstats->tx_packets;
3280 stats->tx_bytes += sstats->tx_bytes;
3281 stats->tx_errors += sstats->tx_errors;
3282 stats->tx_dropped += sstats->tx_dropped;
3284 stats->multicast += sstats->multicast;
3285 stats->collisions += sstats->collisions;
3287 stats->rx_length_errors += sstats->rx_length_errors;
3288 stats->rx_over_errors += sstats->rx_over_errors;
3289 stats->rx_crc_errors += sstats->rx_crc_errors;
3290 stats->rx_frame_errors += sstats->rx_frame_errors;
3291 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3292 stats->rx_missed_errors += sstats->rx_missed_errors;
3294 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3295 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3296 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3297 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3298 stats->tx_window_errors += sstats->tx_window_errors;
3300 read_unlock_bh(&bond->lock);
3302 return stats;
3305 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3307 struct net_device *slave_dev = NULL;
3308 struct ifbond k_binfo;
3309 struct ifbond __user *u_binfo = NULL;
3310 struct ifslave k_sinfo;
3311 struct ifslave __user *u_sinfo = NULL;
3312 struct mii_ioctl_data *mii = NULL;
3313 struct net *net;
3314 int res = 0;
3316 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
3318 switch (cmd) {
3319 case SIOCGMIIPHY:
3320 mii = if_mii(ifr);
3321 if (!mii)
3322 return -EINVAL;
3324 mii->phy_id = 0;
3325 /* Fall Through */
3326 case SIOCGMIIREG:
3328 * We do this again just in case we were called by SIOCGMIIREG
3329 * instead of SIOCGMIIPHY.
3331 mii = if_mii(ifr);
3332 if (!mii)
3333 return -EINVAL;
3336 if (mii->reg_num == 1) {
3337 struct bonding *bond = netdev_priv(bond_dev);
3338 mii->val_out = 0;
3339 read_lock(&bond->lock);
3340 read_lock(&bond->curr_slave_lock);
3341 if (netif_carrier_ok(bond->dev))
3342 mii->val_out = BMSR_LSTATUS;
3344 read_unlock(&bond->curr_slave_lock);
3345 read_unlock(&bond->lock);
3348 return 0;
3349 case BOND_INFO_QUERY_OLD:
3350 case SIOCBONDINFOQUERY:
3351 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3353 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
3354 return -EFAULT;
3356 res = bond_info_query(bond_dev, &k_binfo);
3357 if (res == 0 &&
3358 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3359 return -EFAULT;
3361 return res;
3362 case BOND_SLAVE_INFO_QUERY_OLD:
3363 case SIOCBONDSLAVEINFOQUERY:
3364 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3366 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
3367 return -EFAULT;
3369 res = bond_slave_info_query(bond_dev, &k_sinfo);
3370 if (res == 0 &&
3371 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3372 return -EFAULT;
3374 return res;
3375 default:
3376 /* Go on */
3377 break;
3380 net = dev_net(bond_dev);
3382 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3383 return -EPERM;
3385 slave_dev = dev_get_by_name(net, ifr->ifr_slave);
3387 pr_debug("slave_dev=%p:\n", slave_dev);
3389 if (!slave_dev)
3390 res = -ENODEV;
3391 else {
3392 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
3393 switch (cmd) {
3394 case BOND_ENSLAVE_OLD:
3395 case SIOCBONDENSLAVE:
3396 res = bond_enslave(bond_dev, slave_dev);
3397 break;
3398 case BOND_RELEASE_OLD:
3399 case SIOCBONDRELEASE:
3400 res = bond_release(bond_dev, slave_dev);
3401 break;
3402 case BOND_SETHWADDR_OLD:
3403 case SIOCBONDSETHWADDR:
3404 bond_set_dev_addr(bond_dev, slave_dev);
3405 res = 0;
3406 break;
3407 case BOND_CHANGE_ACTIVE_OLD:
3408 case SIOCBONDCHANGEACTIVE:
3409 res = bond_ioctl_change_active(bond_dev, slave_dev);
3410 break;
3411 default:
3412 res = -EOPNOTSUPP;
3415 dev_put(slave_dev);
3418 return res;
3421 static void bond_change_rx_flags(struct net_device *bond_dev, int change)
3423 struct bonding *bond = netdev_priv(bond_dev);
3425 if (change & IFF_PROMISC)
3426 bond_set_promiscuity(bond,
3427 bond_dev->flags & IFF_PROMISC ? 1 : -1);
3429 if (change & IFF_ALLMULTI)
3430 bond_set_allmulti(bond,
3431 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
3434 static void bond_set_rx_mode(struct net_device *bond_dev)
3436 struct bonding *bond = netdev_priv(bond_dev);
3437 struct slave *slave;
3439 rcu_read_lock();
3440 if (USES_PRIMARY(bond->params.mode)) {
3441 slave = rcu_dereference(bond->curr_active_slave);
3442 if (slave) {
3443 dev_uc_sync(slave->dev, bond_dev);
3444 dev_mc_sync(slave->dev, bond_dev);
3446 } else {
3447 bond_for_each_slave_rcu(bond, slave) {
3448 dev_uc_sync_multiple(slave->dev, bond_dev);
3449 dev_mc_sync_multiple(slave->dev, bond_dev);
3452 rcu_read_unlock();
3455 static int bond_neigh_init(struct neighbour *n)
3457 struct bonding *bond = netdev_priv(n->dev);
3458 const struct net_device_ops *slave_ops;
3459 struct neigh_parms parms;
3460 struct slave *slave;
3461 int ret;
3463 slave = bond_first_slave(bond);
3464 if (!slave)
3465 return 0;
3466 slave_ops = slave->dev->netdev_ops;
3467 if (!slave_ops->ndo_neigh_setup)
3468 return 0;
3470 parms.neigh_setup = NULL;
3471 parms.neigh_cleanup = NULL;
3472 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
3473 if (ret)
3474 return ret;
3477 * Assign slave's neigh_cleanup to neighbour in case cleanup is called
3478 * after the last slave has been detached. Assumes that all slaves
3479 * utilize the same neigh_cleanup (true at this writing as only user
3480 * is ipoib).
3482 n->parms->neigh_cleanup = parms.neigh_cleanup;
3484 if (!parms.neigh_setup)
3485 return 0;
3487 return parms.neigh_setup(n);
3491 * The bonding ndo_neigh_setup is called at init time beofre any
3492 * slave exists. So we must declare proxy setup function which will
3493 * be used at run time to resolve the actual slave neigh param setup.
3495 * It's also called by master devices (such as vlans) to setup their
3496 * underlying devices. In that case - do nothing, we're already set up from
3497 * our init.
3499 static int bond_neigh_setup(struct net_device *dev,
3500 struct neigh_parms *parms)
3502 /* modify only our neigh_parms */
3503 if (parms->dev == dev)
3504 parms->neigh_setup = bond_neigh_init;
3506 return 0;
3510 * Change the MTU of all of a master's slaves to match the master
3512 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3514 struct bonding *bond = netdev_priv(bond_dev);
3515 struct slave *slave;
3516 int res = 0;
3518 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
3519 (bond_dev ? bond_dev->name : "None"), new_mtu);
3521 /* Can't hold bond->lock with bh disabled here since
3522 * some base drivers panic. On the other hand we can't
3523 * hold bond->lock without bh disabled because we'll
3524 * deadlock. The only solution is to rely on the fact
3525 * that we're under rtnl_lock here, and the slaves
3526 * list won't change. This doesn't solve the problem
3527 * of setting the slave's MTU while it is
3528 * transmitting, but the assumption is that the base
3529 * driver can handle that.
3531 * TODO: figure out a way to safely iterate the slaves
3532 * list, but without holding a lock around the actual
3533 * call to the base driver.
3536 bond_for_each_slave(bond, slave) {
3537 pr_debug("s %p s->p %p c_m %p\n",
3538 slave,
3539 bond_prev_slave(bond, slave),
3540 slave->dev->netdev_ops->ndo_change_mtu);
3542 res = dev_set_mtu(slave->dev, new_mtu);
3544 if (res) {
3545 /* If we failed to set the slave's mtu to the new value
3546 * we must abort the operation even in ACTIVE_BACKUP
3547 * mode, because if we allow the backup slaves to have
3548 * different mtu values than the active slave we'll
3549 * need to change their mtu when doing a failover. That
3550 * means changing their mtu from timer context, which
3551 * is probably not a good idea.
3553 pr_debug("err %d %s\n", res, slave->dev->name);
3554 goto unwind;
3558 bond_dev->mtu = new_mtu;
3560 return 0;
3562 unwind:
3563 /* unwind from head to the slave that failed */
3564 bond_for_each_slave_continue_reverse(bond, slave) {
3565 int tmp_res;
3567 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3568 if (tmp_res) {
3569 pr_debug("unwind err %d dev %s\n",
3570 tmp_res, slave->dev->name);
3574 return res;
3578 * Change HW address
3580 * Note that many devices must be down to change the HW address, and
3581 * downing the master releases all slaves. We can make bonds full of
3582 * bonding devices to test this, however.
3584 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3586 struct bonding *bond = netdev_priv(bond_dev);
3587 struct sockaddr *sa = addr, tmp_sa;
3588 struct slave *slave;
3589 int res = 0;
3591 if (bond->params.mode == BOND_MODE_ALB)
3592 return bond_alb_set_mac_address(bond_dev, addr);
3595 pr_debug("bond=%p, name=%s\n",
3596 bond, bond_dev ? bond_dev->name : "None");
3598 /* If fail_over_mac is enabled, do nothing and return success.
3599 * Returning an error causes ifenslave to fail.
3601 if (bond->params.fail_over_mac)
3602 return 0;
3604 if (!is_valid_ether_addr(sa->sa_data))
3605 return -EADDRNOTAVAIL;
3607 /* Can't hold bond->lock with bh disabled here since
3608 * some base drivers panic. On the other hand we can't
3609 * hold bond->lock without bh disabled because we'll
3610 * deadlock. The only solution is to rely on the fact
3611 * that we're under rtnl_lock here, and the slaves
3612 * list won't change. This doesn't solve the problem
3613 * of setting the slave's hw address while it is
3614 * transmitting, but the assumption is that the base
3615 * driver can handle that.
3617 * TODO: figure out a way to safely iterate the slaves
3618 * list, but without holding a lock around the actual
3619 * call to the base driver.
3622 bond_for_each_slave(bond, slave) {
3623 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
3624 pr_debug("slave %p %s\n", slave, slave->dev->name);
3626 if (slave_ops->ndo_set_mac_address == NULL) {
3627 res = -EOPNOTSUPP;
3628 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
3629 goto unwind;
3632 res = dev_set_mac_address(slave->dev, addr);
3633 if (res) {
3634 /* TODO: consider downing the slave
3635 * and retry ?
3636 * User should expect communications
3637 * breakage anyway until ARP finish
3638 * updating, so...
3640 pr_debug("err %d %s\n", res, slave->dev->name);
3641 goto unwind;
3645 /* success */
3646 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3647 return 0;
3649 unwind:
3650 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3651 tmp_sa.sa_family = bond_dev->type;
3653 /* unwind from head to the slave that failed */
3654 bond_for_each_slave_continue_reverse(bond, slave) {
3655 int tmp_res;
3657 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3658 if (tmp_res) {
3659 pr_debug("unwind err %d dev %s\n",
3660 tmp_res, slave->dev->name);
3664 return res;
3668 * bond_xmit_slave_id - transmit skb through slave with slave_id
3669 * @bond: bonding device that is transmitting
3670 * @skb: buffer to transmit
3671 * @slave_id: slave id up to slave_cnt-1 through which to transmit
3673 * This function tries to transmit through slave with slave_id but in case
3674 * it fails, it tries to find the first available slave for transmission.
3675 * The skb is consumed in all cases, thus the function is void.
3677 void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id)
3679 struct slave *slave;
3680 int i = slave_id;
3682 /* Here we start from the slave with slave_id */
3683 bond_for_each_slave_rcu(bond, slave) {
3684 if (--i < 0) {
3685 if (slave_can_tx(slave)) {
3686 bond_dev_queue_xmit(bond, skb, slave->dev);
3687 return;
3692 /* Here we start from the first slave up to slave_id */
3693 i = slave_id;
3694 bond_for_each_slave_rcu(bond, slave) {
3695 if (--i < 0)
3696 break;
3697 if (slave_can_tx(slave)) {
3698 bond_dev_queue_xmit(bond, skb, slave->dev);
3699 return;
3702 /* no slave that can tx has been found */
3703 dev_kfree_skb_any(skb);
3706 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3708 struct bonding *bond = netdev_priv(bond_dev);
3709 struct iphdr *iph = ip_hdr(skb);
3710 struct slave *slave;
3713 * Start with the curr_active_slave that joined the bond as the
3714 * default for sending IGMP traffic. For failover purposes one
3715 * needs to maintain some consistency for the interface that will
3716 * send the join/membership reports. The curr_active_slave found
3717 * will send all of this type of traffic.
3719 if (iph->protocol == IPPROTO_IGMP && skb->protocol == htons(ETH_P_IP)) {
3720 slave = rcu_dereference(bond->curr_active_slave);
3721 if (slave && slave_can_tx(slave))
3722 bond_dev_queue_xmit(bond, skb, slave->dev);
3723 else
3724 bond_xmit_slave_id(bond, skb, 0);
3725 } else {
3726 bond_xmit_slave_id(bond, skb,
3727 bond->rr_tx_counter++ % bond->slave_cnt);
3730 return NETDEV_TX_OK;
3734 * in active-backup mode, we know that bond->curr_active_slave is always valid if
3735 * the bond has a usable interface.
3737 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
3739 struct bonding *bond = netdev_priv(bond_dev);
3740 struct slave *slave;
3742 slave = rcu_dereference(bond->curr_active_slave);
3743 if (slave)
3744 bond_dev_queue_xmit(bond, skb, slave->dev);
3745 else
3746 dev_kfree_skb_any(skb);
3748 return NETDEV_TX_OK;
3752 * In bond_xmit_xor() , we determine the output device by using a pre-
3753 * determined xmit_hash_policy(), If the selected device is not enabled,
3754 * find the next active slave.
3756 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
3758 struct bonding *bond = netdev_priv(bond_dev);
3760 bond_xmit_slave_id(bond, skb,
3761 bond->xmit_hash_policy(skb, bond->slave_cnt));
3763 return NETDEV_TX_OK;
3766 /* in broadcast mode, we send everything to all usable interfaces. */
3767 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
3769 struct bonding *bond = netdev_priv(bond_dev);
3770 struct slave *slave = NULL;
3772 bond_for_each_slave_rcu(bond, slave) {
3773 if (bond_is_last_slave(bond, slave))
3774 break;
3775 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP) {
3776 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
3778 if (!skb2) {
3779 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
3780 bond_dev->name);
3781 continue;
3783 /* bond_dev_queue_xmit always returns 0 */
3784 bond_dev_queue_xmit(bond, skb2, slave->dev);
3787 if (slave && IS_UP(slave->dev) && slave->link == BOND_LINK_UP)
3788 bond_dev_queue_xmit(bond, skb, slave->dev);
3789 else
3790 dev_kfree_skb_any(skb);
3792 return NETDEV_TX_OK;
3795 /*------------------------- Device initialization ---------------------------*/
3797 static void bond_set_xmit_hash_policy(struct bonding *bond)
3799 switch (bond->params.xmit_policy) {
3800 case BOND_XMIT_POLICY_LAYER23:
3801 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
3802 break;
3803 case BOND_XMIT_POLICY_LAYER34:
3804 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
3805 break;
3806 case BOND_XMIT_POLICY_LAYER2:
3807 default:
3808 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
3809 break;
3814 * Lookup the slave that corresponds to a qid
3816 static inline int bond_slave_override(struct bonding *bond,
3817 struct sk_buff *skb)
3819 struct slave *slave = NULL;
3820 struct slave *check_slave;
3821 int res = 1;
3823 if (!skb->queue_mapping)
3824 return 1;
3826 /* Find out if any slaves have the same mapping as this skb. */
3827 bond_for_each_slave_rcu(bond, check_slave) {
3828 if (check_slave->queue_id == skb->queue_mapping) {
3829 slave = check_slave;
3830 break;
3834 /* If the slave isn't UP, use default transmit policy. */
3835 if (slave && slave->queue_id && IS_UP(slave->dev) &&
3836 (slave->link == BOND_LINK_UP)) {
3837 res = bond_dev_queue_xmit(bond, skb, slave->dev);
3840 return res;
3844 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
3847 * This helper function exists to help dev_pick_tx get the correct
3848 * destination queue. Using a helper function skips a call to
3849 * skb_tx_hash and will put the skbs in the queue we expect on their
3850 * way down to the bonding driver.
3852 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
3855 * Save the original txq to restore before passing to the driver
3857 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
3859 if (unlikely(txq >= dev->real_num_tx_queues)) {
3860 do {
3861 txq -= dev->real_num_tx_queues;
3862 } while (txq >= dev->real_num_tx_queues);
3864 return txq;
3867 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
3869 struct bonding *bond = netdev_priv(dev);
3871 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
3872 if (!bond_slave_override(bond, skb))
3873 return NETDEV_TX_OK;
3876 switch (bond->params.mode) {
3877 case BOND_MODE_ROUNDROBIN:
3878 return bond_xmit_roundrobin(skb, dev);
3879 case BOND_MODE_ACTIVEBACKUP:
3880 return bond_xmit_activebackup(skb, dev);
3881 case BOND_MODE_XOR:
3882 return bond_xmit_xor(skb, dev);
3883 case BOND_MODE_BROADCAST:
3884 return bond_xmit_broadcast(skb, dev);
3885 case BOND_MODE_8023AD:
3886 return bond_3ad_xmit_xor(skb, dev);
3887 case BOND_MODE_ALB:
3888 case BOND_MODE_TLB:
3889 return bond_alb_xmit(skb, dev);
3890 default:
3891 /* Should never happen, mode already checked */
3892 pr_err("%s: Error: Unknown bonding mode %d\n",
3893 dev->name, bond->params.mode);
3894 WARN_ON_ONCE(1);
3895 dev_kfree_skb_any(skb);
3896 return NETDEV_TX_OK;
3900 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
3902 struct bonding *bond = netdev_priv(dev);
3903 netdev_tx_t ret = NETDEV_TX_OK;
3906 * If we risk deadlock from transmitting this in the
3907 * netpoll path, tell netpoll to queue the frame for later tx
3909 if (is_netpoll_tx_blocked(dev))
3910 return NETDEV_TX_BUSY;
3912 rcu_read_lock();
3913 if (!list_empty(&bond->slave_list))
3914 ret = __bond_start_xmit(skb, dev);
3915 else
3916 dev_kfree_skb_any(skb);
3917 rcu_read_unlock();
3919 return ret;
3923 * set bond mode specific net device operations
3925 void bond_set_mode_ops(struct bonding *bond, int mode)
3927 struct net_device *bond_dev = bond->dev;
3929 switch (mode) {
3930 case BOND_MODE_ROUNDROBIN:
3931 break;
3932 case BOND_MODE_ACTIVEBACKUP:
3933 break;
3934 case BOND_MODE_XOR:
3935 bond_set_xmit_hash_policy(bond);
3936 break;
3937 case BOND_MODE_BROADCAST:
3938 break;
3939 case BOND_MODE_8023AD:
3940 bond_set_xmit_hash_policy(bond);
3941 break;
3942 case BOND_MODE_ALB:
3943 /* FALLTHRU */
3944 case BOND_MODE_TLB:
3945 break;
3946 default:
3947 /* Should never happen, mode already checked */
3948 pr_err("%s: Error: Unknown bonding mode %d\n",
3949 bond_dev->name, mode);
3950 break;
3954 static int bond_ethtool_get_settings(struct net_device *bond_dev,
3955 struct ethtool_cmd *ecmd)
3957 struct bonding *bond = netdev_priv(bond_dev);
3958 unsigned long speed = 0;
3959 struct slave *slave;
3961 ecmd->duplex = DUPLEX_UNKNOWN;
3962 ecmd->port = PORT_OTHER;
3964 /* Since SLAVE_IS_OK returns false for all inactive or down slaves, we
3965 * do not need to check mode. Though link speed might not represent
3966 * the true receive or transmit bandwidth (not all modes are symmetric)
3967 * this is an accurate maximum.
3969 read_lock(&bond->lock);
3970 bond_for_each_slave(bond, slave) {
3971 if (SLAVE_IS_OK(slave)) {
3972 if (slave->speed != SPEED_UNKNOWN)
3973 speed += slave->speed;
3974 if (ecmd->duplex == DUPLEX_UNKNOWN &&
3975 slave->duplex != DUPLEX_UNKNOWN)
3976 ecmd->duplex = slave->duplex;
3979 ethtool_cmd_speed_set(ecmd, speed ? : SPEED_UNKNOWN);
3980 read_unlock(&bond->lock);
3982 return 0;
3985 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
3986 struct ethtool_drvinfo *drvinfo)
3988 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
3989 strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
3990 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
3991 BOND_ABI_VERSION);
3994 static const struct ethtool_ops bond_ethtool_ops = {
3995 .get_drvinfo = bond_ethtool_get_drvinfo,
3996 .get_settings = bond_ethtool_get_settings,
3997 .get_link = ethtool_op_get_link,
4000 static const struct net_device_ops bond_netdev_ops = {
4001 .ndo_init = bond_init,
4002 .ndo_uninit = bond_uninit,
4003 .ndo_open = bond_open,
4004 .ndo_stop = bond_close,
4005 .ndo_start_xmit = bond_start_xmit,
4006 .ndo_select_queue = bond_select_queue,
4007 .ndo_get_stats64 = bond_get_stats,
4008 .ndo_do_ioctl = bond_do_ioctl,
4009 .ndo_change_rx_flags = bond_change_rx_flags,
4010 .ndo_set_rx_mode = bond_set_rx_mode,
4011 .ndo_change_mtu = bond_change_mtu,
4012 .ndo_set_mac_address = bond_set_mac_address,
4013 .ndo_neigh_setup = bond_neigh_setup,
4014 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4015 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4016 #ifdef CONFIG_NET_POLL_CONTROLLER
4017 .ndo_netpoll_setup = bond_netpoll_setup,
4018 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4019 .ndo_poll_controller = bond_poll_controller,
4020 #endif
4021 .ndo_add_slave = bond_enslave,
4022 .ndo_del_slave = bond_release,
4023 .ndo_fix_features = bond_fix_features,
4026 static const struct device_type bond_type = {
4027 .name = "bond",
4030 static void bond_destructor(struct net_device *bond_dev)
4032 struct bonding *bond = netdev_priv(bond_dev);
4033 if (bond->wq)
4034 destroy_workqueue(bond->wq);
4035 free_netdev(bond_dev);
4038 static void bond_setup(struct net_device *bond_dev)
4040 struct bonding *bond = netdev_priv(bond_dev);
4042 /* initialize rwlocks */
4043 rwlock_init(&bond->lock);
4044 rwlock_init(&bond->curr_slave_lock);
4045 INIT_LIST_HEAD(&bond->slave_list);
4046 bond->params = bonding_defaults;
4048 /* Initialize pointers */
4049 bond->dev = bond_dev;
4051 /* Initialize the device entry points */
4052 ether_setup(bond_dev);
4053 bond_dev->netdev_ops = &bond_netdev_ops;
4054 bond_dev->ethtool_ops = &bond_ethtool_ops;
4055 bond_set_mode_ops(bond, bond->params.mode);
4057 bond_dev->destructor = bond_destructor;
4059 SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
4061 /* Initialize the device options */
4062 bond_dev->tx_queue_len = 0;
4063 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4064 bond_dev->priv_flags |= IFF_BONDING;
4065 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
4067 /* At first, we block adding VLANs. That's the only way to
4068 * prevent problems that occur when adding VLANs over an
4069 * empty bond. The block will be removed once non-challenged
4070 * slaves are enslaved.
4072 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4074 /* don't acquire bond device's netif_tx_lock when
4075 * transmitting */
4076 bond_dev->features |= NETIF_F_LLTX;
4078 /* By default, we declare the bond to be fully
4079 * VLAN hardware accelerated capable. Special
4080 * care is taken in the various xmit functions
4081 * when there are slaves that are not hw accel
4082 * capable
4085 bond_dev->hw_features = BOND_VLAN_FEATURES |
4086 NETIF_F_HW_VLAN_CTAG_TX |
4087 NETIF_F_HW_VLAN_CTAG_RX |
4088 NETIF_F_HW_VLAN_CTAG_FILTER;
4090 bond_dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
4091 bond_dev->features |= bond_dev->hw_features;
4095 * Destroy a bonding device.
4096 * Must be under rtnl_lock when this function is called.
4098 static void bond_uninit(struct net_device *bond_dev)
4100 struct bonding *bond = netdev_priv(bond_dev);
4101 struct slave *slave, *tmp_slave;
4103 bond_netpoll_cleanup(bond_dev);
4105 /* Release the bonded slaves */
4106 list_for_each_entry_safe(slave, tmp_slave, &bond->slave_list, list)
4107 __bond_release_one(bond_dev, slave->dev, true);
4108 pr_info("%s: released all slaves\n", bond_dev->name);
4110 list_del(&bond->bond_list);
4112 bond_debug_unregister(bond);
4115 /*------------------------- Module initialization ---------------------------*/
4118 * Convert string input module parms. Accept either the
4119 * number of the mode or its string name. A bit complicated because
4120 * some mode names are substrings of other names, and calls from sysfs
4121 * may have whitespace in the name (trailing newlines, for example).
4123 int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
4125 int modeint = -1, i, rv;
4126 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4128 for (p = (char *)buf; *p; p++)
4129 if (!(isdigit(*p) || isspace(*p)))
4130 break;
4132 if (*p)
4133 rv = sscanf(buf, "%20s", modestr);
4134 else
4135 rv = sscanf(buf, "%d", &modeint);
4137 if (!rv)
4138 return -1;
4140 for (i = 0; tbl[i].modename; i++) {
4141 if (modeint == tbl[i].mode)
4142 return tbl[i].mode;
4143 if (strcmp(modestr, tbl[i].modename) == 0)
4144 return tbl[i].mode;
4147 return -1;
4150 static int bond_check_params(struct bond_params *params)
4152 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
4153 int arp_all_targets_value;
4156 * Convert string parameters.
4158 if (mode) {
4159 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4160 if (bond_mode == -1) {
4161 pr_err("Error: Invalid bonding mode \"%s\"\n",
4162 mode == NULL ? "NULL" : mode);
4163 return -EINVAL;
4167 if (xmit_hash_policy) {
4168 if ((bond_mode != BOND_MODE_XOR) &&
4169 (bond_mode != BOND_MODE_8023AD)) {
4170 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4171 bond_mode_name(bond_mode));
4172 } else {
4173 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4174 xmit_hashtype_tbl);
4175 if (xmit_hashtype == -1) {
4176 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4177 xmit_hash_policy == NULL ? "NULL" :
4178 xmit_hash_policy);
4179 return -EINVAL;
4184 if (lacp_rate) {
4185 if (bond_mode != BOND_MODE_8023AD) {
4186 pr_info("lacp_rate param is irrelevant in mode %s\n",
4187 bond_mode_name(bond_mode));
4188 } else {
4189 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4190 if (lacp_fast == -1) {
4191 pr_err("Error: Invalid lacp rate \"%s\"\n",
4192 lacp_rate == NULL ? "NULL" : lacp_rate);
4193 return -EINVAL;
4198 if (ad_select) {
4199 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4200 if (params->ad_select == -1) {
4201 pr_err("Error: Invalid ad_select \"%s\"\n",
4202 ad_select == NULL ? "NULL" : ad_select);
4203 return -EINVAL;
4206 if (bond_mode != BOND_MODE_8023AD) {
4207 pr_warning("ad_select param only affects 802.3ad mode\n");
4209 } else {
4210 params->ad_select = BOND_AD_STABLE;
4213 if (max_bonds < 0) {
4214 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4215 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4216 max_bonds = BOND_DEFAULT_MAX_BONDS;
4219 if (miimon < 0) {
4220 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4221 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4222 miimon = BOND_LINK_MON_INTERV;
4225 if (updelay < 0) {
4226 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4227 updelay, INT_MAX);
4228 updelay = 0;
4231 if (downdelay < 0) {
4232 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4233 downdelay, INT_MAX);
4234 downdelay = 0;
4237 if ((use_carrier != 0) && (use_carrier != 1)) {
4238 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4239 use_carrier);
4240 use_carrier = 1;
4243 if (num_peer_notif < 0 || num_peer_notif > 255) {
4244 pr_warning("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4245 num_peer_notif);
4246 num_peer_notif = 1;
4249 /* reset values for 802.3ad */
4250 if (bond_mode == BOND_MODE_8023AD) {
4251 if (!miimon) {
4252 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");
4253 pr_warning("Forcing miimon to 100msec\n");
4254 miimon = 100;
4258 if (tx_queues < 1 || tx_queues > 255) {
4259 pr_warning("Warning: tx_queues (%d) should be between "
4260 "1 and 255, resetting to %d\n",
4261 tx_queues, BOND_DEFAULT_TX_QUEUES);
4262 tx_queues = BOND_DEFAULT_TX_QUEUES;
4265 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4266 pr_warning("Warning: all_slaves_active module parameter (%d), "
4267 "not of valid value (0/1), so it was set to "
4268 "0\n", all_slaves_active);
4269 all_slaves_active = 0;
4272 if (resend_igmp < 0 || resend_igmp > 255) {
4273 pr_warning("Warning: resend_igmp (%d) should be between "
4274 "0 and 255, resetting to %d\n",
4275 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4276 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4279 /* reset values for TLB/ALB */
4280 if ((bond_mode == BOND_MODE_TLB) ||
4281 (bond_mode == BOND_MODE_ALB)) {
4282 if (!miimon) {
4283 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");
4284 pr_warning("Forcing miimon to 100msec\n");
4285 miimon = 100;
4289 if (bond_mode == BOND_MODE_ALB) {
4290 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",
4291 updelay);
4294 if (!miimon) {
4295 if (updelay || downdelay) {
4296 /* just warn the user the up/down delay will have
4297 * no effect since miimon is zero...
4299 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",
4300 updelay, downdelay);
4302 } else {
4303 /* don't allow arp monitoring */
4304 if (arp_interval) {
4305 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4306 miimon, arp_interval);
4307 arp_interval = 0;
4310 if ((updelay % miimon) != 0) {
4311 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4312 updelay, miimon,
4313 (updelay / miimon) * miimon);
4316 updelay /= miimon;
4318 if ((downdelay % miimon) != 0) {
4319 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4320 downdelay, miimon,
4321 (downdelay / miimon) * miimon);
4324 downdelay /= miimon;
4327 if (arp_interval < 0) {
4328 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4329 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4330 arp_interval = BOND_LINK_ARP_INTERV;
4333 for (arp_ip_count = 0, i = 0;
4334 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
4335 /* not complete check, but should be good enough to
4336 catch mistakes */
4337 __be32 ip = in_aton(arp_ip_target[i]);
4338 if (!isdigit(arp_ip_target[i][0]) || ip == 0 ||
4339 ip == htonl(INADDR_BROADCAST)) {
4340 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4341 arp_ip_target[i]);
4342 arp_interval = 0;
4343 } else {
4344 if (bond_get_targets_ip(arp_target, ip) == -1)
4345 arp_target[arp_ip_count++] = ip;
4346 else
4347 pr_warning("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
4348 &ip);
4352 if (arp_interval && !arp_ip_count) {
4353 /* don't allow arping if no arp_ip_target given... */
4354 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4355 arp_interval);
4356 arp_interval = 0;
4359 if (arp_validate) {
4360 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4361 pr_err("arp_validate only supported in active-backup mode\n");
4362 return -EINVAL;
4364 if (!arp_interval) {
4365 pr_err("arp_validate requires arp_interval\n");
4366 return -EINVAL;
4369 arp_validate_value = bond_parse_parm(arp_validate,
4370 arp_validate_tbl);
4371 if (arp_validate_value == -1) {
4372 pr_err("Error: invalid arp_validate \"%s\"\n",
4373 arp_validate == NULL ? "NULL" : arp_validate);
4374 return -EINVAL;
4376 } else
4377 arp_validate_value = 0;
4379 arp_all_targets_value = 0;
4380 if (arp_all_targets) {
4381 arp_all_targets_value = bond_parse_parm(arp_all_targets,
4382 arp_all_targets_tbl);
4384 if (arp_all_targets_value == -1) {
4385 pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
4386 arp_all_targets);
4387 arp_all_targets_value = 0;
4391 if (miimon) {
4392 pr_info("MII link monitoring set to %d ms\n", miimon);
4393 } else if (arp_interval) {
4394 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4395 arp_interval,
4396 arp_validate_tbl[arp_validate_value].modename,
4397 arp_ip_count);
4399 for (i = 0; i < arp_ip_count; i++)
4400 pr_info(" %s", arp_ip_target[i]);
4402 pr_info("\n");
4404 } else if (max_bonds) {
4405 /* miimon and arp_interval not set, we need one so things
4406 * work as expected, see bonding.txt for details
4408 pr_debug("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");
4411 if (primary && !USES_PRIMARY(bond_mode)) {
4412 /* currently, using a primary only makes sense
4413 * in active backup, TLB or ALB modes
4415 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4416 primary, bond_mode_name(bond_mode));
4417 primary = NULL;
4420 if (primary && primary_reselect) {
4421 primary_reselect_value = bond_parse_parm(primary_reselect,
4422 pri_reselect_tbl);
4423 if (primary_reselect_value == -1) {
4424 pr_err("Error: Invalid primary_reselect \"%s\"\n",
4425 primary_reselect ==
4426 NULL ? "NULL" : primary_reselect);
4427 return -EINVAL;
4429 } else {
4430 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4433 if (fail_over_mac) {
4434 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4435 fail_over_mac_tbl);
4436 if (fail_over_mac_value == -1) {
4437 pr_err("Error: invalid fail_over_mac \"%s\"\n",
4438 arp_validate == NULL ? "NULL" : arp_validate);
4439 return -EINVAL;
4442 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
4443 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
4444 } else {
4445 fail_over_mac_value = BOND_FOM_NONE;
4448 /* fill params struct with the proper values */
4449 params->mode = bond_mode;
4450 params->xmit_policy = xmit_hashtype;
4451 params->miimon = miimon;
4452 params->num_peer_notif = num_peer_notif;
4453 params->arp_interval = arp_interval;
4454 params->arp_validate = arp_validate_value;
4455 params->arp_all_targets = arp_all_targets_value;
4456 params->updelay = updelay;
4457 params->downdelay = downdelay;
4458 params->use_carrier = use_carrier;
4459 params->lacp_fast = lacp_fast;
4460 params->primary[0] = 0;
4461 params->primary_reselect = primary_reselect_value;
4462 params->fail_over_mac = fail_over_mac_value;
4463 params->tx_queues = tx_queues;
4464 params->all_slaves_active = all_slaves_active;
4465 params->resend_igmp = resend_igmp;
4466 params->min_links = min_links;
4467 params->lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
4469 if (primary) {
4470 strncpy(params->primary, primary, IFNAMSIZ);
4471 params->primary[IFNAMSIZ - 1] = 0;
4474 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4476 return 0;
4479 static struct lock_class_key bonding_netdev_xmit_lock_key;
4480 static struct lock_class_key bonding_netdev_addr_lock_key;
4481 static struct lock_class_key bonding_tx_busylock_key;
4483 static void bond_set_lockdep_class_one(struct net_device *dev,
4484 struct netdev_queue *txq,
4485 void *_unused)
4487 lockdep_set_class(&txq->_xmit_lock,
4488 &bonding_netdev_xmit_lock_key);
4491 static void bond_set_lockdep_class(struct net_device *dev)
4493 lockdep_set_class(&dev->addr_list_lock,
4494 &bonding_netdev_addr_lock_key);
4495 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
4496 dev->qdisc_tx_busylock = &bonding_tx_busylock_key;
4500 * Called from registration process
4502 static int bond_init(struct net_device *bond_dev)
4504 struct bonding *bond = netdev_priv(bond_dev);
4505 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
4506 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
4508 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4511 * Initialize locks that may be required during
4512 * en/deslave operations. All of the bond_open work
4513 * (of which this is part) should really be moved to
4514 * a phase prior to dev_open
4516 spin_lock_init(&(bond_info->tx_hashtbl_lock));
4517 spin_lock_init(&(bond_info->rx_hashtbl_lock));
4519 bond->wq = create_singlethread_workqueue(bond_dev->name);
4520 if (!bond->wq)
4521 return -ENOMEM;
4523 bond_set_lockdep_class(bond_dev);
4525 list_add_tail(&bond->bond_list, &bn->dev_list);
4527 bond_prepare_sysfs_group(bond);
4529 bond_debug_register(bond);
4531 /* Ensure valid dev_addr */
4532 if (is_zero_ether_addr(bond_dev->dev_addr) &&
4533 bond_dev->addr_assign_type == NET_ADDR_PERM)
4534 eth_hw_addr_random(bond_dev);
4536 return 0;
4539 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4541 if (tb[IFLA_ADDRESS]) {
4542 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4543 return -EINVAL;
4544 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4545 return -EADDRNOTAVAIL;
4547 return 0;
4550 static unsigned int bond_get_num_tx_queues(void)
4552 return tx_queues;
4555 static struct rtnl_link_ops bond_link_ops __read_mostly = {
4556 .kind = "bond",
4557 .priv_size = sizeof(struct bonding),
4558 .setup = bond_setup,
4559 .validate = bond_validate,
4560 .get_num_tx_queues = bond_get_num_tx_queues,
4561 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
4562 as for TX queues */
4565 /* Create a new bond based on the specified name and bonding parameters.
4566 * If name is NULL, obtain a suitable "bond%d" name for us.
4567 * Caller must NOT hold rtnl_lock; we need to release it here before we
4568 * set up our sysfs entries.
4570 int bond_create(struct net *net, const char *name)
4572 struct net_device *bond_dev;
4573 int res;
4575 rtnl_lock();
4577 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
4578 name ? name : "bond%d",
4579 bond_setup, tx_queues);
4580 if (!bond_dev) {
4581 pr_err("%s: eek! can't alloc netdev!\n", name);
4582 rtnl_unlock();
4583 return -ENOMEM;
4586 dev_net_set(bond_dev, net);
4587 bond_dev->rtnl_link_ops = &bond_link_ops;
4589 res = register_netdevice(bond_dev);
4591 netif_carrier_off(bond_dev);
4593 rtnl_unlock();
4594 if (res < 0)
4595 bond_destructor(bond_dev);
4596 return res;
4599 static int __net_init bond_net_init(struct net *net)
4601 struct bond_net *bn = net_generic(net, bond_net_id);
4603 bn->net = net;
4604 INIT_LIST_HEAD(&bn->dev_list);
4606 bond_create_proc_dir(bn);
4607 bond_create_sysfs(bn);
4609 return 0;
4612 static void __net_exit bond_net_exit(struct net *net)
4614 struct bond_net *bn = net_generic(net, bond_net_id);
4615 struct bonding *bond, *tmp_bond;
4616 LIST_HEAD(list);
4618 bond_destroy_sysfs(bn);
4619 bond_destroy_proc_dir(bn);
4621 /* Kill off any bonds created after unregistering bond rtnl ops */
4622 rtnl_lock();
4623 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
4624 unregister_netdevice_queue(bond->dev, &list);
4625 unregister_netdevice_many(&list);
4626 rtnl_unlock();
4629 static struct pernet_operations bond_net_ops = {
4630 .init = bond_net_init,
4631 .exit = bond_net_exit,
4632 .id = &bond_net_id,
4633 .size = sizeof(struct bond_net),
4636 static int __init bonding_init(void)
4638 int i;
4639 int res;
4641 pr_info("%s", bond_version);
4643 res = bond_check_params(&bonding_defaults);
4644 if (res)
4645 goto out;
4647 res = register_pernet_subsys(&bond_net_ops);
4648 if (res)
4649 goto out;
4651 res = rtnl_link_register(&bond_link_ops);
4652 if (res)
4653 goto err_link;
4655 bond_create_debugfs();
4657 for (i = 0; i < max_bonds; i++) {
4658 res = bond_create(&init_net, NULL);
4659 if (res)
4660 goto err;
4663 register_netdevice_notifier(&bond_netdev_notifier);
4664 out:
4665 return res;
4666 err:
4667 bond_destroy_debugfs();
4668 rtnl_link_unregister(&bond_link_ops);
4669 err_link:
4670 unregister_pernet_subsys(&bond_net_ops);
4671 goto out;
4675 static void __exit bonding_exit(void)
4677 unregister_netdevice_notifier(&bond_netdev_notifier);
4679 bond_destroy_debugfs();
4681 rtnl_link_unregister(&bond_link_ops);
4682 unregister_pernet_subsys(&bond_net_ops);
4684 #ifdef CONFIG_NET_POLL_CONTROLLER
4686 * Make sure we don't have an imbalance on our netpoll blocking
4688 WARN_ON(atomic_read(&netpoll_block_tx));
4689 #endif
4692 module_init(bonding_init);
4693 module_exit(bonding_exit);
4694 MODULE_LICENSE("GPL");
4695 MODULE_VERSION(DRV_VERSION);
4696 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4697 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4698 MODULE_ALIAS_RTNL_LINK("bond");