bonding: COW before overwriting the destination MAC address
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / bonding / bond_main.c
blob7b7ca971672ff259773a6270fddfbc810dcffff9
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/system.h>
58 #include <asm/dma.h>
59 #include <linux/uaccess.h>
60 #include <linux/errno.h>
61 #include <linux/netdevice.h>
62 #include <linux/inetdevice.h>
63 #include <linux/igmp.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
66 #include <net/sock.h>
67 #include <linux/rtnetlink.h>
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
70 #include <linux/smp.h>
71 #include <linux/if_ether.h>
72 #include <net/arp.h>
73 #include <linux/mii.h>
74 #include <linux/ethtool.h>
75 #include <linux/if_vlan.h>
76 #include <linux/if_bonding.h>
77 #include <linux/jiffies.h>
78 #include <linux/preempt.h>
79 #include <net/route.h>
80 #include <net/net_namespace.h>
81 #include <net/netns/generic.h>
82 #include "bonding.h"
83 #include "bond_3ad.h"
84 #include "bond_alb.h"
86 /*---------------------------- Module parameters ----------------------------*/
88 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
89 #define BOND_LINK_MON_INTERV 0
90 #define BOND_LINK_ARP_INTERV 0
92 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
93 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
94 static int num_grat_arp = 1;
95 static int num_unsol_na = 1;
96 static int miimon = BOND_LINK_MON_INTERV;
97 static int updelay;
98 static int downdelay;
99 static int use_carrier = 1;
100 static char *mode;
101 static char *primary;
102 static char *primary_reselect;
103 static char *lacp_rate;
104 static char *ad_select;
105 static char *xmit_hash_policy;
106 static int arp_interval = BOND_LINK_ARP_INTERV;
107 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
108 static char *arp_validate;
109 static char *fail_over_mac;
110 static int all_slaves_active = 0;
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(num_grat_arp, int, 0644);
119 MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
120 module_param(num_unsol_na, int, 0644);
121 MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
122 module_param(miimon, int, 0);
123 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
124 module_param(updelay, int, 0);
125 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
126 module_param(downdelay, int, 0);
127 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
128 "in milliseconds");
129 module_param(use_carrier, int, 0);
130 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
131 "0 for off, 1 for on (default)");
132 module_param(mode, charp, 0);
133 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
134 "1 for active-backup, 2 for balance-xor, "
135 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
136 "6 for balance-alb");
137 module_param(primary, charp, 0);
138 MODULE_PARM_DESC(primary, "Primary network device to use");
139 module_param(primary_reselect, charp, 0);
140 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
141 "once it comes up; "
142 "0 for always (default), "
143 "1 for only if speed of primary is "
144 "better, "
145 "2 for only on active slave "
146 "failure");
147 module_param(lacp_rate, charp, 0);
148 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
149 "(slow/fast)");
150 module_param(ad_select, charp, 0);
151 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
152 module_param(xmit_hash_policy, charp, 0);
153 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
154 ", 1 for layer 3+4");
155 module_param(arp_interval, int, 0);
156 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
157 module_param_array(arp_ip_target, charp, NULL, 0);
158 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
159 module_param(arp_validate, charp, 0);
160 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
161 module_param(fail_over_mac, charp, 0);
162 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
163 module_param(all_slaves_active, int, 0);
164 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
165 "by setting active flag for all slaves. "
166 "0 for never (default), 1 for always.");
167 module_param(resend_igmp, int, 0);
168 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
170 /*----------------------------- Global variables ----------------------------*/
172 #ifdef CONFIG_NET_POLL_CONTROLLER
173 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
174 #endif
176 static const char * const version =
177 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
179 int bond_net_id __read_mostly;
181 static __be32 arp_target[BOND_MAX_ARP_TARGETS];
182 static int arp_ip_count;
183 static int bond_mode = BOND_MODE_ROUNDROBIN;
184 static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
185 static int lacp_fast;
187 const struct bond_parm_tbl bond_lacp_tbl[] = {
188 { "slow", AD_LACP_SLOW},
189 { "fast", AD_LACP_FAST},
190 { NULL, -1},
193 const struct bond_parm_tbl bond_mode_tbl[] = {
194 { "balance-rr", BOND_MODE_ROUNDROBIN},
195 { "active-backup", BOND_MODE_ACTIVEBACKUP},
196 { "balance-xor", BOND_MODE_XOR},
197 { "broadcast", BOND_MODE_BROADCAST},
198 { "802.3ad", BOND_MODE_8023AD},
199 { "balance-tlb", BOND_MODE_TLB},
200 { "balance-alb", BOND_MODE_ALB},
201 { NULL, -1},
204 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
205 { "layer2", BOND_XMIT_POLICY_LAYER2},
206 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
207 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
208 { NULL, -1},
211 const struct bond_parm_tbl arp_validate_tbl[] = {
212 { "none", BOND_ARP_VALIDATE_NONE},
213 { "active", BOND_ARP_VALIDATE_ACTIVE},
214 { "backup", BOND_ARP_VALIDATE_BACKUP},
215 { "all", BOND_ARP_VALIDATE_ALL},
216 { NULL, -1},
219 const struct bond_parm_tbl fail_over_mac_tbl[] = {
220 { "none", BOND_FOM_NONE},
221 { "active", BOND_FOM_ACTIVE},
222 { "follow", BOND_FOM_FOLLOW},
223 { NULL, -1},
226 const struct bond_parm_tbl pri_reselect_tbl[] = {
227 { "always", BOND_PRI_RESELECT_ALWAYS},
228 { "better", BOND_PRI_RESELECT_BETTER},
229 { "failure", BOND_PRI_RESELECT_FAILURE},
230 { NULL, -1},
233 struct bond_parm_tbl ad_select_tbl[] = {
234 { "stable", BOND_AD_STABLE},
235 { "bandwidth", BOND_AD_BANDWIDTH},
236 { "count", BOND_AD_COUNT},
237 { NULL, -1},
240 /*-------------------------- Forward declarations ---------------------------*/
242 static void bond_send_gratuitous_arp(struct bonding *bond);
243 static int bond_init(struct net_device *bond_dev);
244 static void bond_uninit(struct net_device *bond_dev);
246 /*---------------------------- General routines -----------------------------*/
248 static const char *bond_mode_name(int mode)
250 static const char *names[] = {
251 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
252 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
253 [BOND_MODE_XOR] = "load balancing (xor)",
254 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
255 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
256 [BOND_MODE_TLB] = "transmit load balancing",
257 [BOND_MODE_ALB] = "adaptive load balancing",
260 if (mode < 0 || mode > BOND_MODE_ALB)
261 return "unknown";
263 return names[mode];
266 /*---------------------------------- VLAN -----------------------------------*/
269 * bond_add_vlan - add a new vlan id on bond
270 * @bond: bond that got the notification
271 * @vlan_id: the vlan id to add
273 * Returns -ENOMEM if allocation failed.
275 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
277 struct vlan_entry *vlan;
279 pr_debug("bond: %s, vlan id %d\n",
280 (bond ? bond->dev->name : "None"), vlan_id);
282 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
283 if (!vlan)
284 return -ENOMEM;
286 INIT_LIST_HEAD(&vlan->vlan_list);
287 vlan->vlan_id = vlan_id;
289 write_lock_bh(&bond->lock);
291 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
293 write_unlock_bh(&bond->lock);
295 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
297 return 0;
301 * bond_del_vlan - delete a vlan id from bond
302 * @bond: bond that got the notification
303 * @vlan_id: the vlan id to delete
305 * returns -ENODEV if @vlan_id was not found in @bond.
307 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
309 struct vlan_entry *vlan;
310 int res = -ENODEV;
312 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
314 block_netpoll_tx();
315 write_lock_bh(&bond->lock);
317 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
318 if (vlan->vlan_id == vlan_id) {
319 list_del(&vlan->vlan_list);
321 if (bond_is_lb(bond))
322 bond_alb_clear_vlan(bond, vlan_id);
324 pr_debug("removed VLAN ID %d from bond %s\n",
325 vlan_id, bond->dev->name);
327 kfree(vlan);
329 if (list_empty(&bond->vlan_list) &&
330 (bond->slave_cnt == 0)) {
331 /* Last VLAN removed and no slaves, so
332 * restore block on adding VLANs. This will
333 * be removed once new slaves that are not
334 * VLAN challenged will be added.
336 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
339 res = 0;
340 goto out;
344 pr_debug("couldn't find VLAN ID %d in bond %s\n",
345 vlan_id, bond->dev->name);
347 out:
348 write_unlock_bh(&bond->lock);
349 unblock_netpoll_tx();
350 return res;
354 * bond_has_challenged_slaves
355 * @bond: the bond we're working on
357 * Searches the slave list. Returns 1 if a vlan challenged slave
358 * was found, 0 otherwise.
360 * Assumes bond->lock is held.
362 static int bond_has_challenged_slaves(struct bonding *bond)
364 struct slave *slave;
365 int i;
367 bond_for_each_slave(bond, slave, i) {
368 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
369 pr_debug("found VLAN challenged slave - %s\n",
370 slave->dev->name);
371 return 1;
375 pr_debug("no VLAN challenged slaves found\n");
376 return 0;
380 * bond_next_vlan - safely skip to the next item in the vlans list.
381 * @bond: the bond we're working on
382 * @curr: item we're advancing from
384 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
385 * or @curr->next otherwise (even if it is @curr itself again).
387 * Caller must hold bond->lock
389 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
391 struct vlan_entry *next, *last;
393 if (list_empty(&bond->vlan_list))
394 return NULL;
396 if (!curr) {
397 next = list_entry(bond->vlan_list.next,
398 struct vlan_entry, vlan_list);
399 } else {
400 last = list_entry(bond->vlan_list.prev,
401 struct vlan_entry, vlan_list);
402 if (last == curr) {
403 next = list_entry(bond->vlan_list.next,
404 struct vlan_entry, vlan_list);
405 } else {
406 next = list_entry(curr->vlan_list.next,
407 struct vlan_entry, vlan_list);
411 return next;
415 * bond_dev_queue_xmit - Prepare skb for xmit.
417 * @bond: bond device that got this skb for tx.
418 * @skb: hw accel VLAN tagged skb to transmit
419 * @slave_dev: slave that is supposed to xmit this skbuff
421 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
422 struct net_device *slave_dev)
424 skb->dev = slave_dev;
425 skb->priority = 1;
426 if (unlikely(netpoll_tx_running(slave_dev)))
427 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
428 else
429 dev_queue_xmit(skb);
431 return 0;
435 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
436 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
437 * lock because:
438 * a. This operation is performed in IOCTL context,
439 * b. The operation is protected by the RTNL semaphore in the 8021q code,
440 * c. Holding a lock with BH disabled while directly calling a base driver
441 * entry point is generally a BAD idea.
443 * The design of synchronization/protection for this operation in the 8021q
444 * module is good for one or more VLAN devices over a single physical device
445 * and cannot be extended for a teaming solution like bonding, so there is a
446 * potential race condition here where a net device from the vlan group might
447 * be referenced (either by a base driver or the 8021q code) while it is being
448 * removed from the system. However, it turns out we're not making matters
449 * worse, and if it works for regular VLAN usage it will work here too.
453 * bond_vlan_rx_register - Propagates registration to slaves
454 * @bond_dev: bonding net device that got called
455 * @grp: vlan group being registered
457 static void bond_vlan_rx_register(struct net_device *bond_dev,
458 struct vlan_group *grp)
460 struct bonding *bond = netdev_priv(bond_dev);
461 struct slave *slave;
462 int i;
464 write_lock_bh(&bond->lock);
465 bond->vlgrp = grp;
466 write_unlock_bh(&bond->lock);
468 bond_for_each_slave(bond, slave, i) {
469 struct net_device *slave_dev = slave->dev;
470 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
472 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
473 slave_ops->ndo_vlan_rx_register) {
474 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
480 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
481 * @bond_dev: bonding net device that got called
482 * @vid: vlan id being added
484 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
486 struct bonding *bond = netdev_priv(bond_dev);
487 struct slave *slave;
488 int i, res;
490 bond_for_each_slave(bond, slave, i) {
491 struct net_device *slave_dev = slave->dev;
492 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
494 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
495 slave_ops->ndo_vlan_rx_add_vid) {
496 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
500 res = bond_add_vlan(bond, vid);
501 if (res) {
502 pr_err("%s: Error: Failed to add vlan id %d\n",
503 bond_dev->name, vid);
508 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
509 * @bond_dev: bonding net device that got called
510 * @vid: vlan id being removed
512 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
514 struct bonding *bond = netdev_priv(bond_dev);
515 struct slave *slave;
516 struct net_device *vlan_dev;
517 int i, res;
519 bond_for_each_slave(bond, slave, i) {
520 struct net_device *slave_dev = slave->dev;
521 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
523 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
524 slave_ops->ndo_vlan_rx_kill_vid) {
525 /* Save and then restore vlan_dev in the grp array,
526 * since the slave's driver might clear it.
528 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
529 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
530 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
534 res = bond_del_vlan(bond, vid);
535 if (res) {
536 pr_err("%s: Error: Failed to remove vlan id %d\n",
537 bond_dev->name, vid);
541 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
543 struct vlan_entry *vlan;
544 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
546 if (!bond->vlgrp)
547 return;
549 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
550 slave_ops->ndo_vlan_rx_register)
551 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
553 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
554 !(slave_ops->ndo_vlan_rx_add_vid))
555 return;
557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
558 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
561 static void bond_del_vlans_from_slave(struct bonding *bond,
562 struct net_device *slave_dev)
564 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
565 struct vlan_entry *vlan;
566 struct net_device *vlan_dev;
568 if (!bond->vlgrp)
569 return;
571 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
572 !(slave_ops->ndo_vlan_rx_kill_vid))
573 goto unreg;
575 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
576 if (!vlan->vlan_id)
577 continue;
578 /* Save and then restore vlan_dev in the grp array,
579 * since the slave's driver might clear it.
581 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
582 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
583 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
586 unreg:
587 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
588 slave_ops->ndo_vlan_rx_register)
589 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
592 /*------------------------------- Link status -------------------------------*/
595 * Set the carrier state for the master according to the state of its
596 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
597 * do special 802.3ad magic.
599 * Returns zero if carrier state does not change, nonzero if it does.
601 static int bond_set_carrier(struct bonding *bond)
603 struct slave *slave;
604 int i;
606 if (bond->slave_cnt == 0)
607 goto down;
609 if (bond->params.mode == BOND_MODE_8023AD)
610 return bond_3ad_set_carrier(bond);
612 bond_for_each_slave(bond, slave, i) {
613 if (slave->link == BOND_LINK_UP) {
614 if (!netif_carrier_ok(bond->dev)) {
615 netif_carrier_on(bond->dev);
616 return 1;
618 return 0;
622 down:
623 if (netif_carrier_ok(bond->dev)) {
624 netif_carrier_off(bond->dev);
625 return 1;
627 return 0;
631 * Get link speed and duplex from the slave's base driver
632 * using ethtool. If for some reason the call fails or the
633 * values are invalid, fake speed and duplex to 100/Full
634 * and return error.
636 static int bond_update_speed_duplex(struct slave *slave)
638 struct net_device *slave_dev = slave->dev;
639 struct ethtool_cmd etool;
640 int res;
642 /* Fake speed and duplex */
643 slave->speed = SPEED_100;
644 slave->duplex = DUPLEX_FULL;
646 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
647 return -1;
649 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
650 if (res < 0)
651 return -1;
653 switch (etool.speed) {
654 case SPEED_10:
655 case SPEED_100:
656 case SPEED_1000:
657 case SPEED_10000:
658 break;
659 default:
660 return -1;
663 switch (etool.duplex) {
664 case DUPLEX_FULL:
665 case DUPLEX_HALF:
666 break;
667 default:
668 return -1;
671 slave->speed = etool.speed;
672 slave->duplex = etool.duplex;
674 return 0;
678 * if <dev> supports MII link status reporting, check its link status.
680 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
681 * depending upon the setting of the use_carrier parameter.
683 * Return either BMSR_LSTATUS, meaning that the link is up (or we
684 * can't tell and just pretend it is), or 0, meaning that the link is
685 * down.
687 * If reporting is non-zero, instead of faking link up, return -1 if
688 * both ETHTOOL and MII ioctls fail (meaning the device does not
689 * support them). If use_carrier is set, return whatever it says.
690 * It'd be nice if there was a good way to tell if a driver supports
691 * netif_carrier, but there really isn't.
693 static int bond_check_dev_link(struct bonding *bond,
694 struct net_device *slave_dev, int reporting)
696 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
697 int (*ioctl)(struct net_device *, struct ifreq *, int);
698 struct ifreq ifr;
699 struct mii_ioctl_data *mii;
701 if (!reporting && !netif_running(slave_dev))
702 return 0;
704 if (bond->params.use_carrier)
705 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
707 /* Try to get link status using Ethtool first. */
708 if (slave_dev->ethtool_ops) {
709 if (slave_dev->ethtool_ops->get_link) {
710 u32 link;
712 link = slave_dev->ethtool_ops->get_link(slave_dev);
714 return link ? BMSR_LSTATUS : 0;
718 /* Ethtool can't be used, fallback to MII ioctls. */
719 ioctl = slave_ops->ndo_do_ioctl;
720 if (ioctl) {
721 /* TODO: set pointer to correct ioctl on a per team member */
722 /* bases to make this more efficient. that is, once */
723 /* we determine the correct ioctl, we will always */
724 /* call it and not the others for that team */
725 /* member. */
728 * We cannot assume that SIOCGMIIPHY will also read a
729 * register; not all network drivers (e.g., e100)
730 * support that.
733 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
734 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
735 mii = if_mii(&ifr);
736 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
737 mii->reg_num = MII_BMSR;
738 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
739 return mii->val_out & BMSR_LSTATUS;
744 * If reporting, report that either there's no dev->do_ioctl,
745 * or both SIOCGMIIREG and get_link failed (meaning that we
746 * cannot report link status). If not reporting, pretend
747 * we're ok.
749 return reporting ? -1 : BMSR_LSTATUS;
752 /*----------------------------- Multicast list ------------------------------*/
755 * Push the promiscuity flag down to appropriate slaves
757 static int bond_set_promiscuity(struct bonding *bond, int inc)
759 int err = 0;
760 if (USES_PRIMARY(bond->params.mode)) {
761 /* write lock already acquired */
762 if (bond->curr_active_slave) {
763 err = dev_set_promiscuity(bond->curr_active_slave->dev,
764 inc);
766 } else {
767 struct slave *slave;
768 int i;
769 bond_for_each_slave(bond, slave, i) {
770 err = dev_set_promiscuity(slave->dev, inc);
771 if (err)
772 return err;
775 return err;
779 * Push the allmulti flag down to all slaves
781 static int bond_set_allmulti(struct bonding *bond, int inc)
783 int err = 0;
784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
787 err = dev_set_allmulti(bond->curr_active_slave->dev,
788 inc);
790 } else {
791 struct slave *slave;
792 int i;
793 bond_for_each_slave(bond, slave, i) {
794 err = dev_set_allmulti(slave->dev, inc);
795 if (err)
796 return err;
799 return err;
803 * Add a Multicast address to slaves
804 * according to mode
806 static void bond_mc_add(struct bonding *bond, void *addr)
808 if (USES_PRIMARY(bond->params.mode)) {
809 /* write lock already acquired */
810 if (bond->curr_active_slave)
811 dev_mc_add(bond->curr_active_slave->dev, addr);
812 } else {
813 struct slave *slave;
814 int i;
816 bond_for_each_slave(bond, slave, i)
817 dev_mc_add(slave->dev, addr);
822 * Remove a multicast address from slave
823 * according to mode
825 static void bond_mc_del(struct bonding *bond, void *addr)
827 if (USES_PRIMARY(bond->params.mode)) {
828 /* write lock already acquired */
829 if (bond->curr_active_slave)
830 dev_mc_del(bond->curr_active_slave->dev, addr);
831 } else {
832 struct slave *slave;
833 int i;
834 bond_for_each_slave(bond, slave, i) {
835 dev_mc_del(slave->dev, addr);
841 static void __bond_resend_igmp_join_requests(struct net_device *dev)
843 struct in_device *in_dev;
845 rcu_read_lock();
846 in_dev = __in_dev_get_rcu(dev);
847 if (in_dev)
848 ip_mc_rejoin_groups(in_dev);
849 rcu_read_unlock();
853 * Retrieve the list of registered multicast addresses for the bonding
854 * device and retransmit an IGMP JOIN request to the current active
855 * slave.
857 static void bond_resend_igmp_join_requests(struct bonding *bond)
859 struct net_device *vlan_dev;
860 struct vlan_entry *vlan;
862 read_lock(&bond->lock);
864 /* rejoin all groups on bond device */
865 __bond_resend_igmp_join_requests(bond->dev);
867 /* rejoin all groups on vlan devices */
868 if (bond->vlgrp) {
869 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
870 vlan_dev = vlan_group_get_device(bond->vlgrp,
871 vlan->vlan_id);
872 if (vlan_dev)
873 __bond_resend_igmp_join_requests(vlan_dev);
877 if (--bond->igmp_retrans > 0)
878 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
880 read_unlock(&bond->lock);
883 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
885 struct bonding *bond = container_of(work, struct bonding,
886 mcast_work.work);
887 bond_resend_igmp_join_requests(bond);
891 * flush all members of flush->mc_list from device dev->mc_list
893 static void bond_mc_list_flush(struct net_device *bond_dev,
894 struct net_device *slave_dev)
896 struct bonding *bond = netdev_priv(bond_dev);
897 struct netdev_hw_addr *ha;
899 netdev_for_each_mc_addr(ha, bond_dev)
900 dev_mc_del(slave_dev, ha->addr);
902 if (bond->params.mode == BOND_MODE_8023AD) {
903 /* del lacpdu mc addr from mc list */
904 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
906 dev_mc_del(slave_dev, lacpdu_multicast);
910 /*--------------------------- Active slave change ---------------------------*/
913 * Update the mc list and multicast-related flags for the new and
914 * old active slaves (if any) according to the multicast mode, and
915 * promiscuous flags unconditionally.
917 static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
918 struct slave *old_active)
920 struct netdev_hw_addr *ha;
922 if (!USES_PRIMARY(bond->params.mode))
923 /* nothing to do - mc list is already up-to-date on
924 * all slaves
926 return;
928 if (old_active) {
929 if (bond->dev->flags & IFF_PROMISC)
930 dev_set_promiscuity(old_active->dev, -1);
932 if (bond->dev->flags & IFF_ALLMULTI)
933 dev_set_allmulti(old_active->dev, -1);
935 netdev_for_each_mc_addr(ha, bond->dev)
936 dev_mc_del(old_active->dev, ha->addr);
939 if (new_active) {
940 /* FIXME: Signal errors upstream. */
941 if (bond->dev->flags & IFF_PROMISC)
942 dev_set_promiscuity(new_active->dev, 1);
944 if (bond->dev->flags & IFF_ALLMULTI)
945 dev_set_allmulti(new_active->dev, 1);
947 netdev_for_each_mc_addr(ha, bond->dev)
948 dev_mc_add(new_active->dev, ha->addr);
953 * bond_do_fail_over_mac
955 * Perform special MAC address swapping for fail_over_mac settings
957 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
959 static void bond_do_fail_over_mac(struct bonding *bond,
960 struct slave *new_active,
961 struct slave *old_active)
962 __releases(&bond->curr_slave_lock)
963 __releases(&bond->lock)
964 __acquires(&bond->lock)
965 __acquires(&bond->curr_slave_lock)
967 u8 tmp_mac[ETH_ALEN];
968 struct sockaddr saddr;
969 int rv;
971 switch (bond->params.fail_over_mac) {
972 case BOND_FOM_ACTIVE:
973 if (new_active)
974 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
975 new_active->dev->addr_len);
976 break;
977 case BOND_FOM_FOLLOW:
979 * if new_active && old_active, swap them
980 * if just old_active, do nothing (going to no active slave)
981 * if just new_active, set new_active to bond's MAC
983 if (!new_active)
984 return;
986 write_unlock_bh(&bond->curr_slave_lock);
987 read_unlock(&bond->lock);
989 if (old_active) {
990 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
991 memcpy(saddr.sa_data, old_active->dev->dev_addr,
992 ETH_ALEN);
993 saddr.sa_family = new_active->dev->type;
994 } else {
995 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
996 saddr.sa_family = bond->dev->type;
999 rv = dev_set_mac_address(new_active->dev, &saddr);
1000 if (rv) {
1001 pr_err("%s: Error %d setting MAC of slave %s\n",
1002 bond->dev->name, -rv, new_active->dev->name);
1003 goto out;
1006 if (!old_active)
1007 goto out;
1009 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1010 saddr.sa_family = old_active->dev->type;
1012 rv = dev_set_mac_address(old_active->dev, &saddr);
1013 if (rv)
1014 pr_err("%s: Error %d setting MAC of slave %s\n",
1015 bond->dev->name, -rv, new_active->dev->name);
1016 out:
1017 read_lock(&bond->lock);
1018 write_lock_bh(&bond->curr_slave_lock);
1019 break;
1020 default:
1021 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
1022 bond->dev->name, bond->params.fail_over_mac);
1023 break;
1028 static bool bond_should_change_active(struct bonding *bond)
1030 struct slave *prim = bond->primary_slave;
1031 struct slave *curr = bond->curr_active_slave;
1033 if (!prim || !curr || curr->link != BOND_LINK_UP)
1034 return true;
1035 if (bond->force_primary) {
1036 bond->force_primary = false;
1037 return true;
1039 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1040 (prim->speed < curr->speed ||
1041 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1042 return false;
1043 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1044 return false;
1045 return true;
1049 * find_best_interface - select the best available slave to be the active one
1050 * @bond: our bonding struct
1052 * Warning: Caller must hold curr_slave_lock for writing.
1054 static struct slave *bond_find_best_slave(struct bonding *bond)
1056 struct slave *new_active, *old_active;
1057 struct slave *bestslave = NULL;
1058 int mintime = bond->params.updelay;
1059 int i;
1061 new_active = bond->curr_active_slave;
1063 if (!new_active) { /* there were no active slaves left */
1064 if (bond->slave_cnt > 0) /* found one slave */
1065 new_active = bond->first_slave;
1066 else
1067 return NULL; /* still no slave, return NULL */
1070 if ((bond->primary_slave) &&
1071 bond->primary_slave->link == BOND_LINK_UP &&
1072 bond_should_change_active(bond)) {
1073 new_active = bond->primary_slave;
1076 /* remember where to stop iterating over the slaves */
1077 old_active = new_active;
1079 bond_for_each_slave_from(bond, new_active, i, old_active) {
1080 if (new_active->link == BOND_LINK_UP) {
1081 return new_active;
1082 } else if (new_active->link == BOND_LINK_BACK &&
1083 IS_UP(new_active->dev)) {
1084 /* link up, but waiting for stabilization */
1085 if (new_active->delay < mintime) {
1086 mintime = new_active->delay;
1087 bestslave = new_active;
1092 return bestslave;
1096 * change_active_interface - change the active slave into the specified one
1097 * @bond: our bonding struct
1098 * @new: the new slave to make the active one
1100 * Set the new slave to the bond's settings and unset them on the old
1101 * curr_active_slave.
1102 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1104 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1105 * because it is apparently the best available slave we have, even though its
1106 * updelay hasn't timed out yet.
1108 * If new_active is not NULL, caller must hold bond->lock for read and
1109 * curr_slave_lock for write_bh.
1111 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1113 struct slave *old_active = bond->curr_active_slave;
1115 if (old_active == new_active)
1116 return;
1118 if (new_active) {
1119 new_active->jiffies = jiffies;
1121 if (new_active->link == BOND_LINK_BACK) {
1122 if (USES_PRIMARY(bond->params.mode)) {
1123 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1124 bond->dev->name, new_active->dev->name,
1125 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1128 new_active->delay = 0;
1129 new_active->link = BOND_LINK_UP;
1131 if (bond->params.mode == BOND_MODE_8023AD)
1132 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1134 if (bond_is_lb(bond))
1135 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1136 } else {
1137 if (USES_PRIMARY(bond->params.mode)) {
1138 pr_info("%s: making interface %s the new active one.\n",
1139 bond->dev->name, new_active->dev->name);
1144 if (USES_PRIMARY(bond->params.mode))
1145 bond_mc_swap(bond, new_active, old_active);
1147 if (bond_is_lb(bond)) {
1148 bond_alb_handle_active_change(bond, new_active);
1149 if (old_active)
1150 bond_set_slave_inactive_flags(old_active);
1151 if (new_active)
1152 bond_set_slave_active_flags(new_active);
1153 } else {
1154 bond->curr_active_slave = new_active;
1157 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1158 if (old_active)
1159 bond_set_slave_inactive_flags(old_active);
1161 if (new_active) {
1162 bond_set_slave_active_flags(new_active);
1164 if (bond->params.fail_over_mac)
1165 bond_do_fail_over_mac(bond, new_active,
1166 old_active);
1168 if (netif_running(bond->dev)) {
1169 bond->send_grat_arp = bond->params.num_grat_arp;
1170 bond_send_gratuitous_arp(bond);
1172 bond->send_unsol_na = bond->params.num_unsol_na;
1173 bond_send_unsolicited_na(bond);
1176 write_unlock_bh(&bond->curr_slave_lock);
1177 read_unlock(&bond->lock);
1179 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
1181 read_lock(&bond->lock);
1182 write_lock_bh(&bond->curr_slave_lock);
1186 /* resend IGMP joins since active slave has changed or
1187 * all were sent on curr_active_slave */
1188 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1189 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1190 netif_running(bond->dev)) {
1191 bond->igmp_retrans = bond->params.resend_igmp;
1192 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
1197 * bond_select_active_slave - select a new active slave, if needed
1198 * @bond: our bonding struct
1200 * This functions should be called when one of the following occurs:
1201 * - The old curr_active_slave has been released or lost its link.
1202 * - The primary_slave has got its link back.
1203 * - A slave has got its link back and there's no old curr_active_slave.
1205 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1207 void bond_select_active_slave(struct bonding *bond)
1209 struct slave *best_slave;
1210 int rv;
1212 best_slave = bond_find_best_slave(bond);
1213 if (best_slave != bond->curr_active_slave) {
1214 bond_change_active_slave(bond, best_slave);
1215 rv = bond_set_carrier(bond);
1216 if (!rv)
1217 return;
1219 if (netif_carrier_ok(bond->dev)) {
1220 pr_info("%s: first active interface up!\n",
1221 bond->dev->name);
1222 } else {
1223 pr_info("%s: now running without any active interface !\n",
1224 bond->dev->name);
1229 /*--------------------------- slave list handling ---------------------------*/
1232 * This function attaches the slave to the end of list.
1234 * bond->lock held for writing by caller.
1236 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1238 if (bond->first_slave == NULL) { /* attaching the first slave */
1239 new_slave->next = new_slave;
1240 new_slave->prev = new_slave;
1241 bond->first_slave = new_slave;
1242 } else {
1243 new_slave->next = bond->first_slave;
1244 new_slave->prev = bond->first_slave->prev;
1245 new_slave->next->prev = new_slave;
1246 new_slave->prev->next = new_slave;
1249 bond->slave_cnt++;
1253 * This function detaches the slave from the list.
1254 * WARNING: no check is made to verify if the slave effectively
1255 * belongs to <bond>.
1256 * Nothing is freed on return, structures are just unchained.
1257 * If any slave pointer in bond was pointing to <slave>,
1258 * it should be changed by the calling function.
1260 * bond->lock held for writing by caller.
1262 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1264 if (slave->next)
1265 slave->next->prev = slave->prev;
1267 if (slave->prev)
1268 slave->prev->next = slave->next;
1270 if (bond->first_slave == slave) { /* slave is the first slave */
1271 if (bond->slave_cnt > 1) { /* there are more slave */
1272 bond->first_slave = slave->next;
1273 } else {
1274 bond->first_slave = NULL; /* slave was the last one */
1278 slave->next = NULL;
1279 slave->prev = NULL;
1280 bond->slave_cnt--;
1283 #ifdef CONFIG_NET_POLL_CONTROLLER
1284 static inline int slave_enable_netpoll(struct slave *slave)
1286 struct netpoll *np;
1287 int err = 0;
1289 np = kzalloc(sizeof(*np), GFP_KERNEL);
1290 err = -ENOMEM;
1291 if (!np)
1292 goto out;
1294 np->dev = slave->dev;
1295 err = __netpoll_setup(np);
1296 if (err) {
1297 kfree(np);
1298 goto out;
1300 slave->np = np;
1301 out:
1302 return err;
1304 static inline void slave_disable_netpoll(struct slave *slave)
1306 struct netpoll *np = slave->np;
1308 if (!np)
1309 return;
1311 slave->np = NULL;
1312 synchronize_rcu_bh();
1313 __netpoll_cleanup(np);
1314 kfree(np);
1316 static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1318 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1319 return false;
1320 if (!slave_dev->netdev_ops->ndo_poll_controller)
1321 return false;
1322 return true;
1325 static void bond_poll_controller(struct net_device *bond_dev)
1329 static void __bond_netpoll_cleanup(struct bonding *bond)
1331 struct slave *slave;
1332 int i;
1334 bond_for_each_slave(bond, slave, i)
1335 if (IS_UP(slave->dev))
1336 slave_disable_netpoll(slave);
1338 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1340 struct bonding *bond = netdev_priv(bond_dev);
1342 read_lock(&bond->lock);
1343 __bond_netpoll_cleanup(bond);
1344 read_unlock(&bond->lock);
1347 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1349 struct bonding *bond = netdev_priv(dev);
1350 struct slave *slave;
1351 int i, err = 0;
1353 read_lock(&bond->lock);
1354 bond_for_each_slave(bond, slave, i) {
1355 if (!IS_UP(slave->dev))
1356 continue;
1357 err = slave_enable_netpoll(slave);
1358 if (err) {
1359 __bond_netpoll_cleanup(bond);
1360 break;
1363 read_unlock(&bond->lock);
1364 return err;
1367 static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1369 return bond->dev->npinfo;
1372 #else
1373 static inline int slave_enable_netpoll(struct slave *slave)
1375 return 0;
1377 static inline void slave_disable_netpoll(struct slave *slave)
1380 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1383 #endif
1385 /*---------------------------------- IOCTL ----------------------------------*/
1387 static int bond_sethwaddr(struct net_device *bond_dev,
1388 struct net_device *slave_dev)
1390 pr_debug("bond_dev=%p\n", bond_dev);
1391 pr_debug("slave_dev=%p\n", slave_dev);
1392 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1393 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1394 return 0;
1397 #define BOND_VLAN_FEATURES \
1398 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1399 NETIF_F_HW_VLAN_FILTER)
1402 * Compute the common dev->feature set available to all slaves. Some
1403 * feature bits are managed elsewhere, so preserve those feature bits
1404 * on the master device.
1406 static int bond_compute_features(struct bonding *bond)
1408 struct slave *slave;
1409 struct net_device *bond_dev = bond->dev;
1410 u32 features = bond_dev->features;
1411 u32 vlan_features = 0;
1412 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1413 bond_dev->hard_header_len);
1414 int i;
1416 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1417 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1419 if (!bond->first_slave)
1420 goto done;
1422 features &= ~NETIF_F_ONE_FOR_ALL;
1424 vlan_features = bond->first_slave->dev->vlan_features;
1425 bond_for_each_slave(bond, slave, i) {
1426 features = netdev_increment_features(features,
1427 slave->dev->features,
1428 NETIF_F_ONE_FOR_ALL);
1429 vlan_features = netdev_increment_features(vlan_features,
1430 slave->dev->vlan_features,
1431 NETIF_F_ONE_FOR_ALL);
1432 if (slave->dev->hard_header_len > max_hard_header_len)
1433 max_hard_header_len = slave->dev->hard_header_len;
1436 done:
1437 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1438 bond_dev->features = netdev_fix_features(bond_dev, features);
1439 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
1440 bond_dev->hard_header_len = max_hard_header_len;
1442 return 0;
1445 static void bond_setup_by_slave(struct net_device *bond_dev,
1446 struct net_device *slave_dev)
1448 struct bonding *bond = netdev_priv(bond_dev);
1450 bond_dev->header_ops = slave_dev->header_ops;
1452 bond_dev->type = slave_dev->type;
1453 bond_dev->hard_header_len = slave_dev->hard_header_len;
1454 bond_dev->addr_len = slave_dev->addr_len;
1456 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1457 slave_dev->addr_len);
1458 bond->setup_by_slave = 1;
1461 /* On bonding slaves other than the currently active slave, suppress
1462 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
1463 * ARP on active-backup slaves with arp_validate enabled.
1465 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1466 struct net_device *slave_dev,
1467 struct net_device *bond_dev)
1469 if (slave_dev->priv_flags & IFF_SLAVE_INACTIVE) {
1470 if (slave_dev->priv_flags & IFF_SLAVE_NEEDARP &&
1471 skb->protocol == __cpu_to_be16(ETH_P_ARP))
1472 return false;
1474 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1475 skb->pkt_type != PACKET_BROADCAST &&
1476 skb->pkt_type != PACKET_MULTICAST)
1477 return false;
1479 if (bond_dev->priv_flags & IFF_MASTER_8023AD &&
1480 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
1481 return false;
1483 return true;
1485 return false;
1488 static struct sk_buff *bond_handle_frame(struct sk_buff *skb)
1490 struct net_device *slave_dev;
1491 struct net_device *bond_dev;
1493 skb = skb_share_check(skb, GFP_ATOMIC);
1494 if (unlikely(!skb))
1495 return NULL;
1496 slave_dev = skb->dev;
1497 bond_dev = ACCESS_ONCE(slave_dev->master);
1498 if (unlikely(!bond_dev))
1499 return skb;
1501 if (bond_dev->priv_flags & IFF_MASTER_ARPMON)
1502 slave_dev->last_rx = jiffies;
1504 if (bond_should_deliver_exact_match(skb, slave_dev, bond_dev)) {
1505 skb->deliver_no_wcard = 1;
1506 return skb;
1509 skb->dev = bond_dev;
1511 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1512 bond_dev->priv_flags & IFF_BRIDGE_PORT &&
1513 skb->pkt_type == PACKET_HOST) {
1515 if (unlikely(skb_cow_head(skb,
1516 skb->data - skb_mac_header(skb)))) {
1517 kfree_skb(skb);
1518 return NULL;
1520 memcpy(eth_hdr(skb)->h_dest, bond_dev->dev_addr, ETH_ALEN);
1523 return skb;
1526 /* enslave device <slave> to bond device <master> */
1527 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1529 struct bonding *bond = netdev_priv(bond_dev);
1530 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1531 struct slave *new_slave = NULL;
1532 struct netdev_hw_addr *ha;
1533 struct sockaddr addr;
1534 int link_reporting;
1535 int old_features = bond_dev->features;
1536 int res = 0;
1538 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1539 slave_ops->ndo_do_ioctl == NULL) {
1540 pr_warning("%s: Warning: no link monitoring support for %s\n",
1541 bond_dev->name, slave_dev->name);
1544 /* bond must be initialized by bond_open() before enslaving */
1545 if (!(bond_dev->flags & IFF_UP)) {
1546 pr_warning("%s: master_dev is not up in bond_enslave\n",
1547 bond_dev->name);
1550 /* already enslaved */
1551 if (slave_dev->flags & IFF_SLAVE) {
1552 pr_debug("Error, Device was already enslaved\n");
1553 return -EBUSY;
1556 /* vlan challenged mutual exclusion */
1557 /* no need to lock since we're protected by rtnl_lock */
1558 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1559 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1560 if (bond->vlgrp) {
1561 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1562 bond_dev->name, slave_dev->name, bond_dev->name);
1563 return -EPERM;
1564 } else {
1565 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1566 bond_dev->name, slave_dev->name,
1567 slave_dev->name, bond_dev->name);
1568 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1570 } else {
1571 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1572 if (bond->slave_cnt == 0) {
1573 /* First slave, and it is not VLAN challenged,
1574 * so remove the block of adding VLANs over the bond.
1576 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1581 * Old ifenslave binaries are no longer supported. These can
1582 * be identified with moderate accuracy by the state of the slave:
1583 * the current ifenslave will set the interface down prior to
1584 * enslaving it; the old ifenslave will not.
1586 if ((slave_dev->flags & IFF_UP)) {
1587 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
1588 slave_dev->name);
1589 res = -EPERM;
1590 goto err_undo_flags;
1593 /* set bonding device ether type by slave - bonding netdevices are
1594 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1595 * there is a need to override some of the type dependent attribs/funcs.
1597 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1598 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1600 if (bond->slave_cnt == 0) {
1601 if (bond_dev->type != slave_dev->type) {
1602 pr_debug("%s: change device type from %d to %d\n",
1603 bond_dev->name,
1604 bond_dev->type, slave_dev->type);
1606 res = netdev_bonding_change(bond_dev,
1607 NETDEV_PRE_TYPE_CHANGE);
1608 res = notifier_to_errno(res);
1609 if (res) {
1610 pr_err("%s: refused to change device type\n",
1611 bond_dev->name);
1612 res = -EBUSY;
1613 goto err_undo_flags;
1616 /* Flush unicast and multicast addresses */
1617 dev_uc_flush(bond_dev);
1618 dev_mc_flush(bond_dev);
1620 if (slave_dev->type != ARPHRD_ETHER)
1621 bond_setup_by_slave(bond_dev, slave_dev);
1622 else
1623 ether_setup(bond_dev);
1625 netdev_bonding_change(bond_dev,
1626 NETDEV_POST_TYPE_CHANGE);
1628 } else if (bond_dev->type != slave_dev->type) {
1629 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1630 slave_dev->name,
1631 slave_dev->type, bond_dev->type);
1632 res = -EINVAL;
1633 goto err_undo_flags;
1636 if (slave_ops->ndo_set_mac_address == NULL) {
1637 if (bond->slave_cnt == 0) {
1638 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1639 bond_dev->name);
1640 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1641 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1642 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",
1643 bond_dev->name);
1644 res = -EOPNOTSUPP;
1645 goto err_undo_flags;
1649 /* If this is the first slave, then we need to set the master's hardware
1650 * address to be the same as the slave's. */
1651 if (is_zero_ether_addr(bond->dev->dev_addr))
1652 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1653 slave_dev->addr_len);
1656 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1657 if (!new_slave) {
1658 res = -ENOMEM;
1659 goto err_undo_flags;
1663 * Set the new_slave's queue_id to be zero. Queue ID mapping
1664 * is set via sysfs or module option if desired.
1666 new_slave->queue_id = 0;
1668 /* Save slave's original mtu and then set it to match the bond */
1669 new_slave->original_mtu = slave_dev->mtu;
1670 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1671 if (res) {
1672 pr_debug("Error %d calling dev_set_mtu\n", res);
1673 goto err_free;
1677 * Save slave's original ("permanent") mac address for modes
1678 * that need it, and for restoring it upon release, and then
1679 * set it to the master's address
1681 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1683 if (!bond->params.fail_over_mac) {
1685 * Set slave to master's mac address. The application already
1686 * set the master's mac address to that of the first slave
1688 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1689 addr.sa_family = slave_dev->type;
1690 res = dev_set_mac_address(slave_dev, &addr);
1691 if (res) {
1692 pr_debug("Error %d calling set_mac_address\n", res);
1693 goto err_restore_mtu;
1697 res = netdev_set_bond_master(slave_dev, bond_dev);
1698 if (res) {
1699 pr_debug("Error %d calling netdev_set_bond_master\n", res);
1700 goto err_restore_mac;
1702 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, NULL);
1703 if (res) {
1704 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1705 goto err_unset_master;
1708 /* open the slave since the application closed it */
1709 res = dev_open(slave_dev);
1710 if (res) {
1711 pr_debug("Opening slave %s failed\n", slave_dev->name);
1712 goto err_unreg_rxhandler;
1715 new_slave->dev = slave_dev;
1716 slave_dev->priv_flags |= IFF_BONDING;
1718 if (bond_is_lb(bond)) {
1719 /* bond_alb_init_slave() must be called before all other stages since
1720 * it might fail and we do not want to have to undo everything
1722 res = bond_alb_init_slave(bond, new_slave);
1723 if (res)
1724 goto err_close;
1727 /* If the mode USES_PRIMARY, then the new slave gets the
1728 * master's promisc (and mc) settings only if it becomes the
1729 * curr_active_slave, and that is taken care of later when calling
1730 * bond_change_active()
1732 if (!USES_PRIMARY(bond->params.mode)) {
1733 /* set promiscuity level to new slave */
1734 if (bond_dev->flags & IFF_PROMISC) {
1735 res = dev_set_promiscuity(slave_dev, 1);
1736 if (res)
1737 goto err_close;
1740 /* set allmulti level to new slave */
1741 if (bond_dev->flags & IFF_ALLMULTI) {
1742 res = dev_set_allmulti(slave_dev, 1);
1743 if (res)
1744 goto err_close;
1747 netif_addr_lock_bh(bond_dev);
1748 /* upload master's mc_list to new slave */
1749 netdev_for_each_mc_addr(ha, bond_dev)
1750 dev_mc_add(slave_dev, ha->addr);
1751 netif_addr_unlock_bh(bond_dev);
1754 if (bond->params.mode == BOND_MODE_8023AD) {
1755 /* add lacpdu mc addr to mc list */
1756 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1758 dev_mc_add(slave_dev, lacpdu_multicast);
1761 bond_add_vlans_on_slave(bond, slave_dev);
1763 write_lock_bh(&bond->lock);
1765 bond_attach_slave(bond, new_slave);
1767 new_slave->delay = 0;
1768 new_slave->link_failure_count = 0;
1770 bond_compute_features(bond);
1772 write_unlock_bh(&bond->lock);
1774 read_lock(&bond->lock);
1776 new_slave->last_arp_rx = jiffies;
1778 if (bond->params.miimon && !bond->params.use_carrier) {
1779 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1781 if ((link_reporting == -1) && !bond->params.arp_interval) {
1783 * miimon is set but a bonded network driver
1784 * does not support ETHTOOL/MII and
1785 * arp_interval is not set. Note: if
1786 * use_carrier is enabled, we will never go
1787 * here (because netif_carrier is always
1788 * supported); thus, we don't need to change
1789 * the messages for netif_carrier.
1791 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",
1792 bond_dev->name, slave_dev->name);
1793 } else if (link_reporting == -1) {
1794 /* unable get link status using mii/ethtool */
1795 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",
1796 bond_dev->name, slave_dev->name);
1800 /* check for initial state */
1801 if (!bond->params.miimon ||
1802 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1803 if (bond->params.updelay) {
1804 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
1805 new_slave->link = BOND_LINK_BACK;
1806 new_slave->delay = bond->params.updelay;
1807 } else {
1808 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
1809 new_slave->link = BOND_LINK_UP;
1811 new_slave->jiffies = jiffies;
1812 } else {
1813 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
1814 new_slave->link = BOND_LINK_DOWN;
1817 if (bond_update_speed_duplex(new_slave) &&
1818 (new_slave->link != BOND_LINK_DOWN)) {
1819 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1820 bond_dev->name, new_slave->dev->name);
1822 if (bond->params.mode == BOND_MODE_8023AD) {
1823 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1824 bond_dev->name);
1828 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1829 /* if there is a primary slave, remember it */
1830 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1831 bond->primary_slave = new_slave;
1832 bond->force_primary = true;
1836 write_lock_bh(&bond->curr_slave_lock);
1838 switch (bond->params.mode) {
1839 case BOND_MODE_ACTIVEBACKUP:
1840 bond_set_slave_inactive_flags(new_slave);
1841 bond_select_active_slave(bond);
1842 break;
1843 case BOND_MODE_8023AD:
1844 /* in 802.3ad mode, the internal mechanism
1845 * will activate the slaves in the selected
1846 * aggregator
1848 bond_set_slave_inactive_flags(new_slave);
1849 /* if this is the first slave */
1850 if (bond->slave_cnt == 1) {
1851 SLAVE_AD_INFO(new_slave).id = 1;
1852 /* Initialize AD with the number of times that the AD timer is called in 1 second
1853 * can be called only after the mac address of the bond is set
1855 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1856 bond->params.lacp_fast);
1857 } else {
1858 SLAVE_AD_INFO(new_slave).id =
1859 SLAVE_AD_INFO(new_slave->prev).id + 1;
1862 bond_3ad_bind_slave(new_slave);
1863 break;
1864 case BOND_MODE_TLB:
1865 case BOND_MODE_ALB:
1866 new_slave->state = BOND_STATE_ACTIVE;
1867 bond_set_slave_inactive_flags(new_slave);
1868 bond_select_active_slave(bond);
1869 break;
1870 default:
1871 pr_debug("This slave is always active in trunk mode\n");
1873 /* always active in trunk mode */
1874 new_slave->state = BOND_STATE_ACTIVE;
1876 /* In trunking mode there is little meaning to curr_active_slave
1877 * anyway (it holds no special properties of the bond device),
1878 * so we can change it without calling change_active_interface()
1880 if (!bond->curr_active_slave)
1881 bond->curr_active_slave = new_slave;
1883 break;
1884 } /* switch(bond_mode) */
1886 write_unlock_bh(&bond->curr_slave_lock);
1888 bond_set_carrier(bond);
1890 #ifdef CONFIG_NET_POLL_CONTROLLER
1891 slave_dev->npinfo = bond_netpoll_info(bond);
1892 if (slave_dev->npinfo) {
1893 if (slave_enable_netpoll(new_slave)) {
1894 read_unlock(&bond->lock);
1895 pr_info("Error, %s: master_dev is using netpoll, "
1896 "but new slave device does not support netpoll.\n",
1897 bond_dev->name);
1898 res = -EBUSY;
1899 goto err_close;
1902 #endif
1904 read_unlock(&bond->lock);
1906 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1907 if (res)
1908 goto err_close;
1910 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1911 bond_dev->name, slave_dev->name,
1912 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1913 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1915 /* enslave is successful */
1916 return 0;
1918 /* Undo stages on error */
1919 err_close:
1920 dev_close(slave_dev);
1922 err_unreg_rxhandler:
1923 netdev_rx_handler_unregister(slave_dev);
1925 err_unset_master:
1926 netdev_set_bond_master(slave_dev, NULL);
1928 err_restore_mac:
1929 if (!bond->params.fail_over_mac) {
1930 /* XXX TODO - fom follow mode needs to change master's
1931 * MAC if this slave's MAC is in use by the bond, or at
1932 * least print a warning.
1934 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1935 addr.sa_family = slave_dev->type;
1936 dev_set_mac_address(slave_dev, &addr);
1939 err_restore_mtu:
1940 dev_set_mtu(slave_dev, new_slave->original_mtu);
1942 err_free:
1943 kfree(new_slave);
1945 err_undo_flags:
1946 bond_dev->features = old_features;
1948 return res;
1952 * Try to release the slave device <slave> from the bond device <master>
1953 * It is legal to access curr_active_slave without a lock because all the function
1954 * is write-locked.
1956 * The rules for slave state should be:
1957 * for Active/Backup:
1958 * Active stays on all backups go down
1959 * for Bonded connections:
1960 * The first up interface should be left on and all others downed.
1962 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1964 struct bonding *bond = netdev_priv(bond_dev);
1965 struct slave *slave, *oldcurrent;
1966 struct sockaddr addr;
1968 /* slave is not a slave or master is not master of this slave */
1969 if (!(slave_dev->flags & IFF_SLAVE) ||
1970 (slave_dev->master != bond_dev)) {
1971 pr_err("%s: Error: cannot release %s.\n",
1972 bond_dev->name, slave_dev->name);
1973 return -EINVAL;
1976 block_netpoll_tx();
1977 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
1978 write_lock_bh(&bond->lock);
1980 slave = bond_get_slave_by_dev(bond, slave_dev);
1981 if (!slave) {
1982 /* not a slave of this bond */
1983 pr_info("%s: %s not enslaved\n",
1984 bond_dev->name, slave_dev->name);
1985 write_unlock_bh(&bond->lock);
1986 unblock_netpoll_tx();
1987 return -EINVAL;
1990 if (!bond->params.fail_over_mac) {
1991 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1992 bond->slave_cnt > 1)
1993 pr_warning("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n",
1994 bond_dev->name, slave_dev->name,
1995 slave->perm_hwaddr,
1996 bond_dev->name, slave_dev->name);
1999 /* Inform AD package of unbinding of slave. */
2000 if (bond->params.mode == BOND_MODE_8023AD) {
2001 /* must be called before the slave is
2002 * detached from the list
2004 bond_3ad_unbind_slave(slave);
2007 pr_info("%s: releasing %s interface %s\n",
2008 bond_dev->name,
2009 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2010 slave_dev->name);
2012 oldcurrent = bond->curr_active_slave;
2014 bond->current_arp_slave = NULL;
2016 /* release the slave from its bond */
2017 bond_detach_slave(bond, slave);
2019 bond_compute_features(bond);
2021 if (bond->primary_slave == slave)
2022 bond->primary_slave = NULL;
2024 if (oldcurrent == slave)
2025 bond_change_active_slave(bond, NULL);
2027 if (bond_is_lb(bond)) {
2028 /* Must be called only after the slave has been
2029 * detached from the list and the curr_active_slave
2030 * has been cleared (if our_slave == old_current),
2031 * but before a new active slave is selected.
2033 write_unlock_bh(&bond->lock);
2034 bond_alb_deinit_slave(bond, slave);
2035 write_lock_bh(&bond->lock);
2038 if (oldcurrent == slave) {
2040 * Note that we hold RTNL over this sequence, so there
2041 * is no concern that another slave add/remove event
2042 * will interfere.
2044 write_unlock_bh(&bond->lock);
2045 read_lock(&bond->lock);
2046 write_lock_bh(&bond->curr_slave_lock);
2048 bond_select_active_slave(bond);
2050 write_unlock_bh(&bond->curr_slave_lock);
2051 read_unlock(&bond->lock);
2052 write_lock_bh(&bond->lock);
2055 if (bond->slave_cnt == 0) {
2056 bond_set_carrier(bond);
2058 /* if the last slave was removed, zero the mac address
2059 * of the master so it will be set by the application
2060 * to the mac address of the first slave
2062 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2064 if (!bond->vlgrp) {
2065 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2066 } else {
2067 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2068 bond_dev->name, bond_dev->name);
2069 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2070 bond_dev->name);
2072 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2073 !bond_has_challenged_slaves(bond)) {
2074 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2075 bond_dev->name, slave_dev->name, bond_dev->name);
2076 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2079 write_unlock_bh(&bond->lock);
2080 unblock_netpoll_tx();
2082 /* must do this from outside any spinlocks */
2083 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2085 bond_del_vlans_from_slave(bond, slave_dev);
2087 /* If the mode USES_PRIMARY, then we should only remove its
2088 * promisc and mc settings if it was the curr_active_slave, but that was
2089 * already taken care of above when we detached the slave
2091 if (!USES_PRIMARY(bond->params.mode)) {
2092 /* unset promiscuity level from slave */
2093 if (bond_dev->flags & IFF_PROMISC)
2094 dev_set_promiscuity(slave_dev, -1);
2096 /* unset allmulti level from slave */
2097 if (bond_dev->flags & IFF_ALLMULTI)
2098 dev_set_allmulti(slave_dev, -1);
2100 /* flush master's mc_list from slave */
2101 netif_addr_lock_bh(bond_dev);
2102 bond_mc_list_flush(bond_dev, slave_dev);
2103 netif_addr_unlock_bh(bond_dev);
2106 netdev_rx_handler_unregister(slave_dev);
2107 netdev_set_bond_master(slave_dev, NULL);
2109 slave_disable_netpoll(slave);
2111 /* close slave before restoring its mac address */
2112 dev_close(slave_dev);
2114 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
2115 /* restore original ("permanent") mac address */
2116 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2117 addr.sa_family = slave_dev->type;
2118 dev_set_mac_address(slave_dev, &addr);
2121 dev_set_mtu(slave_dev, slave->original_mtu);
2123 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2124 IFF_SLAVE_INACTIVE | IFF_BONDING |
2125 IFF_SLAVE_NEEDARP);
2127 kfree(slave);
2129 return 0; /* deletion OK */
2133 * First release a slave and than destroy the bond if no more slaves are left.
2134 * Must be under rtnl_lock when this function is called.
2136 static int bond_release_and_destroy(struct net_device *bond_dev,
2137 struct net_device *slave_dev)
2139 struct bonding *bond = netdev_priv(bond_dev);
2140 int ret;
2142 ret = bond_release(bond_dev, slave_dev);
2143 if ((ret == 0) && (bond->slave_cnt == 0)) {
2144 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2145 pr_info("%s: destroying bond %s.\n",
2146 bond_dev->name, bond_dev->name);
2147 unregister_netdevice(bond_dev);
2149 return ret;
2153 * This function releases all slaves.
2155 static int bond_release_all(struct net_device *bond_dev)
2157 struct bonding *bond = netdev_priv(bond_dev);
2158 struct slave *slave;
2159 struct net_device *slave_dev;
2160 struct sockaddr addr;
2162 write_lock_bh(&bond->lock);
2164 netif_carrier_off(bond_dev);
2166 if (bond->slave_cnt == 0)
2167 goto out;
2169 bond->current_arp_slave = NULL;
2170 bond->primary_slave = NULL;
2171 bond_change_active_slave(bond, NULL);
2173 while ((slave = bond->first_slave) != NULL) {
2174 /* Inform AD package of unbinding of slave
2175 * before slave is detached from the list.
2177 if (bond->params.mode == BOND_MODE_8023AD)
2178 bond_3ad_unbind_slave(slave);
2180 slave_dev = slave->dev;
2181 bond_detach_slave(bond, slave);
2183 /* now that the slave is detached, unlock and perform
2184 * all the undo steps that should not be called from
2185 * within a lock.
2187 write_unlock_bh(&bond->lock);
2189 if (bond_is_lb(bond)) {
2190 /* must be called only after the slave
2191 * has been detached from the list
2193 bond_alb_deinit_slave(bond, slave);
2196 bond_compute_features(bond);
2198 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2199 bond_del_vlans_from_slave(bond, slave_dev);
2201 /* If the mode USES_PRIMARY, then we should only remove its
2202 * promisc and mc settings if it was the curr_active_slave, but that was
2203 * already taken care of above when we detached the slave
2205 if (!USES_PRIMARY(bond->params.mode)) {
2206 /* unset promiscuity level from slave */
2207 if (bond_dev->flags & IFF_PROMISC)
2208 dev_set_promiscuity(slave_dev, -1);
2210 /* unset allmulti level from slave */
2211 if (bond_dev->flags & IFF_ALLMULTI)
2212 dev_set_allmulti(slave_dev, -1);
2214 /* flush master's mc_list from slave */
2215 netif_addr_lock_bh(bond_dev);
2216 bond_mc_list_flush(bond_dev, slave_dev);
2217 netif_addr_unlock_bh(bond_dev);
2220 netdev_rx_handler_unregister(slave_dev);
2221 netdev_set_bond_master(slave_dev, NULL);
2223 slave_disable_netpoll(slave);
2225 /* close slave before restoring its mac address */
2226 dev_close(slave_dev);
2228 if (!bond->params.fail_over_mac) {
2229 /* restore original ("permanent") mac address*/
2230 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2231 addr.sa_family = slave_dev->type;
2232 dev_set_mac_address(slave_dev, &addr);
2235 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2236 IFF_SLAVE_INACTIVE);
2238 kfree(slave);
2240 /* re-acquire the lock before getting the next slave */
2241 write_lock_bh(&bond->lock);
2244 /* zero the mac address of the master so it will be
2245 * set by the application to the mac address of the
2246 * first slave
2248 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2250 if (!bond->vlgrp) {
2251 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2252 } else {
2253 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2254 bond_dev->name, bond_dev->name);
2255 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2256 bond_dev->name);
2259 pr_info("%s: released all slaves\n", bond_dev->name);
2261 out:
2262 write_unlock_bh(&bond->lock);
2263 return 0;
2267 * This function changes the active slave to slave <slave_dev>.
2268 * It returns -EINVAL in the following cases.
2269 * - <slave_dev> is not found in the list.
2270 * - There is not active slave now.
2271 * - <slave_dev> is already active.
2272 * - The link state of <slave_dev> is not BOND_LINK_UP.
2273 * - <slave_dev> is not running.
2274 * In these cases, this function does nothing.
2275 * In the other cases, current_slave pointer is changed and 0 is returned.
2277 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2279 struct bonding *bond = netdev_priv(bond_dev);
2280 struct slave *old_active = NULL;
2281 struct slave *new_active = NULL;
2282 int res = 0;
2284 if (!USES_PRIMARY(bond->params.mode))
2285 return -EINVAL;
2287 /* Verify that master_dev is indeed the master of slave_dev */
2288 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
2289 return -EINVAL;
2291 read_lock(&bond->lock);
2293 read_lock(&bond->curr_slave_lock);
2294 old_active = bond->curr_active_slave;
2295 read_unlock(&bond->curr_slave_lock);
2297 new_active = bond_get_slave_by_dev(bond, slave_dev);
2300 * Changing to the current active: do nothing; return success.
2302 if (new_active && (new_active == old_active)) {
2303 read_unlock(&bond->lock);
2304 return 0;
2307 if ((new_active) &&
2308 (old_active) &&
2309 (new_active->link == BOND_LINK_UP) &&
2310 IS_UP(new_active->dev)) {
2311 block_netpoll_tx();
2312 write_lock_bh(&bond->curr_slave_lock);
2313 bond_change_active_slave(bond, new_active);
2314 write_unlock_bh(&bond->curr_slave_lock);
2315 unblock_netpoll_tx();
2316 } else
2317 res = -EINVAL;
2319 read_unlock(&bond->lock);
2321 return res;
2324 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2326 struct bonding *bond = netdev_priv(bond_dev);
2328 info->bond_mode = bond->params.mode;
2329 info->miimon = bond->params.miimon;
2331 read_lock(&bond->lock);
2332 info->num_slaves = bond->slave_cnt;
2333 read_unlock(&bond->lock);
2335 return 0;
2338 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2340 struct bonding *bond = netdev_priv(bond_dev);
2341 struct slave *slave;
2342 int i, res = -ENODEV;
2344 read_lock(&bond->lock);
2346 bond_for_each_slave(bond, slave, i) {
2347 if (i == (int)info->slave_id) {
2348 res = 0;
2349 strcpy(info->slave_name, slave->dev->name);
2350 info->link = slave->link;
2351 info->state = slave->state;
2352 info->link_failure_count = slave->link_failure_count;
2353 break;
2357 read_unlock(&bond->lock);
2359 return res;
2362 /*-------------------------------- Monitoring -------------------------------*/
2365 static int bond_miimon_inspect(struct bonding *bond)
2367 struct slave *slave;
2368 int i, link_state, commit = 0;
2369 bool ignore_updelay;
2371 ignore_updelay = !bond->curr_active_slave ? true : false;
2373 bond_for_each_slave(bond, slave, i) {
2374 slave->new_link = BOND_LINK_NOCHANGE;
2376 link_state = bond_check_dev_link(bond, slave->dev, 0);
2378 switch (slave->link) {
2379 case BOND_LINK_UP:
2380 if (link_state)
2381 continue;
2383 slave->link = BOND_LINK_FAIL;
2384 slave->delay = bond->params.downdelay;
2385 if (slave->delay) {
2386 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2387 bond->dev->name,
2388 (bond->params.mode ==
2389 BOND_MODE_ACTIVEBACKUP) ?
2390 ((slave->state == BOND_STATE_ACTIVE) ?
2391 "active " : "backup ") : "",
2392 slave->dev->name,
2393 bond->params.downdelay * bond->params.miimon);
2395 /*FALLTHRU*/
2396 case BOND_LINK_FAIL:
2397 if (link_state) {
2399 * recovered before downdelay expired
2401 slave->link = BOND_LINK_UP;
2402 slave->jiffies = jiffies;
2403 pr_info("%s: link status up again after %d ms for interface %s.\n",
2404 bond->dev->name,
2405 (bond->params.downdelay - slave->delay) *
2406 bond->params.miimon,
2407 slave->dev->name);
2408 continue;
2411 if (slave->delay <= 0) {
2412 slave->new_link = BOND_LINK_DOWN;
2413 commit++;
2414 continue;
2417 slave->delay--;
2418 break;
2420 case BOND_LINK_DOWN:
2421 if (!link_state)
2422 continue;
2424 slave->link = BOND_LINK_BACK;
2425 slave->delay = bond->params.updelay;
2427 if (slave->delay) {
2428 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2429 bond->dev->name, slave->dev->name,
2430 ignore_updelay ? 0 :
2431 bond->params.updelay *
2432 bond->params.miimon);
2434 /*FALLTHRU*/
2435 case BOND_LINK_BACK:
2436 if (!link_state) {
2437 slave->link = BOND_LINK_DOWN;
2438 pr_info("%s: link status down again after %d ms for interface %s.\n",
2439 bond->dev->name,
2440 (bond->params.updelay - slave->delay) *
2441 bond->params.miimon,
2442 slave->dev->name);
2444 continue;
2447 if (ignore_updelay)
2448 slave->delay = 0;
2450 if (slave->delay <= 0) {
2451 slave->new_link = BOND_LINK_UP;
2452 commit++;
2453 ignore_updelay = false;
2454 continue;
2457 slave->delay--;
2458 break;
2462 return commit;
2465 static void bond_miimon_commit(struct bonding *bond)
2467 struct slave *slave;
2468 int i;
2470 bond_for_each_slave(bond, slave, i) {
2471 switch (slave->new_link) {
2472 case BOND_LINK_NOCHANGE:
2473 continue;
2475 case BOND_LINK_UP:
2476 slave->link = BOND_LINK_UP;
2477 slave->jiffies = jiffies;
2479 if (bond->params.mode == BOND_MODE_8023AD) {
2480 /* prevent it from being the active one */
2481 slave->state = BOND_STATE_BACKUP;
2482 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2483 /* make it immediately active */
2484 slave->state = BOND_STATE_ACTIVE;
2485 } else if (slave != bond->primary_slave) {
2486 /* prevent it from being the active one */
2487 slave->state = BOND_STATE_BACKUP;
2490 bond_update_speed_duplex(slave);
2492 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2493 bond->dev->name, slave->dev->name,
2494 slave->speed, slave->duplex ? "full" : "half");
2496 /* notify ad that the link status has changed */
2497 if (bond->params.mode == BOND_MODE_8023AD)
2498 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2500 if (bond_is_lb(bond))
2501 bond_alb_handle_link_change(bond, slave,
2502 BOND_LINK_UP);
2504 if (!bond->curr_active_slave ||
2505 (slave == bond->primary_slave))
2506 goto do_failover;
2508 continue;
2510 case BOND_LINK_DOWN:
2511 if (slave->link_failure_count < UINT_MAX)
2512 slave->link_failure_count++;
2514 slave->link = BOND_LINK_DOWN;
2516 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2517 bond->params.mode == BOND_MODE_8023AD)
2518 bond_set_slave_inactive_flags(slave);
2520 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2521 bond->dev->name, slave->dev->name);
2523 if (bond->params.mode == BOND_MODE_8023AD)
2524 bond_3ad_handle_link_change(slave,
2525 BOND_LINK_DOWN);
2527 if (bond_is_lb(bond))
2528 bond_alb_handle_link_change(bond, slave,
2529 BOND_LINK_DOWN);
2531 if (slave == bond->curr_active_slave)
2532 goto do_failover;
2534 continue;
2536 default:
2537 pr_err("%s: invalid new link %d on slave %s\n",
2538 bond->dev->name, slave->new_link,
2539 slave->dev->name);
2540 slave->new_link = BOND_LINK_NOCHANGE;
2542 continue;
2545 do_failover:
2546 ASSERT_RTNL();
2547 block_netpoll_tx();
2548 write_lock_bh(&bond->curr_slave_lock);
2549 bond_select_active_slave(bond);
2550 write_unlock_bh(&bond->curr_slave_lock);
2551 unblock_netpoll_tx();
2554 bond_set_carrier(bond);
2558 * bond_mii_monitor
2560 * Really a wrapper that splits the mii monitor into two phases: an
2561 * inspection, then (if inspection indicates something needs to be done)
2562 * an acquisition of appropriate locks followed by a commit phase to
2563 * implement whatever link state changes are indicated.
2565 void bond_mii_monitor(struct work_struct *work)
2567 struct bonding *bond = container_of(work, struct bonding,
2568 mii_work.work);
2570 read_lock(&bond->lock);
2571 if (bond->kill_timers)
2572 goto out;
2574 if (bond->slave_cnt == 0)
2575 goto re_arm;
2577 if (bond->send_grat_arp) {
2578 read_lock(&bond->curr_slave_lock);
2579 bond_send_gratuitous_arp(bond);
2580 read_unlock(&bond->curr_slave_lock);
2583 if (bond->send_unsol_na) {
2584 read_lock(&bond->curr_slave_lock);
2585 bond_send_unsolicited_na(bond);
2586 read_unlock(&bond->curr_slave_lock);
2589 if (bond_miimon_inspect(bond)) {
2590 read_unlock(&bond->lock);
2591 rtnl_lock();
2592 read_lock(&bond->lock);
2594 bond_miimon_commit(bond);
2596 read_unlock(&bond->lock);
2597 rtnl_unlock(); /* might sleep, hold no other locks */
2598 read_lock(&bond->lock);
2601 re_arm:
2602 if (bond->params.miimon)
2603 queue_delayed_work(bond->wq, &bond->mii_work,
2604 msecs_to_jiffies(bond->params.miimon));
2605 out:
2606 read_unlock(&bond->lock);
2609 static __be32 bond_glean_dev_ip(struct net_device *dev)
2611 struct in_device *idev;
2612 struct in_ifaddr *ifa;
2613 __be32 addr = 0;
2615 if (!dev)
2616 return 0;
2618 rcu_read_lock();
2619 idev = __in_dev_get_rcu(dev);
2620 if (!idev)
2621 goto out;
2623 ifa = idev->ifa_list;
2624 if (!ifa)
2625 goto out;
2627 addr = ifa->ifa_local;
2628 out:
2629 rcu_read_unlock();
2630 return addr;
2633 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2635 struct vlan_entry *vlan;
2637 if (ip == bond->master_ip)
2638 return 1;
2640 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2641 if (ip == vlan->vlan_ip)
2642 return 1;
2645 return 0;
2649 * We go to the (large) trouble of VLAN tagging ARP frames because
2650 * switches in VLAN mode (especially if ports are configured as
2651 * "native" to a VLAN) might not pass non-tagged frames.
2653 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2655 struct sk_buff *skb;
2657 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2658 slave_dev->name, dest_ip, src_ip, vlan_id);
2660 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2661 NULL, slave_dev->dev_addr, NULL);
2663 if (!skb) {
2664 pr_err("ARP packet allocation failed\n");
2665 return;
2667 if (vlan_id) {
2668 skb = vlan_put_tag(skb, vlan_id);
2669 if (!skb) {
2670 pr_err("failed to insert VLAN tag\n");
2671 return;
2674 arp_xmit(skb);
2678 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2680 int i, vlan_id;
2681 __be32 *targets = bond->params.arp_targets;
2682 struct vlan_entry *vlan;
2683 struct net_device *vlan_dev;
2684 struct flowi fl;
2685 struct rtable *rt;
2687 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2688 if (!targets[i])
2689 break;
2690 pr_debug("basa: target %x\n", targets[i]);
2691 if (!bond->vlgrp) {
2692 pr_debug("basa: empty vlan: arp_send\n");
2693 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2694 bond->master_ip, 0);
2695 continue;
2699 * If VLANs are configured, we do a route lookup to
2700 * determine which VLAN interface would be used, so we
2701 * can tag the ARP with the proper VLAN tag.
2703 memset(&fl, 0, sizeof(fl));
2704 fl.fl4_dst = targets[i];
2705 fl.fl4_tos = RTO_ONLINK;
2707 rt = ip_route_output_key(dev_net(bond->dev), &fl);
2708 if (IS_ERR(rt)) {
2709 if (net_ratelimit()) {
2710 pr_warning("%s: no route to arp_ip_target %pI4\n",
2711 bond->dev->name, &fl.fl4_dst);
2713 continue;
2717 * This target is not on a VLAN
2719 if (rt->dst.dev == bond->dev) {
2720 ip_rt_put(rt);
2721 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2722 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2723 bond->master_ip, 0);
2724 continue;
2727 vlan_id = 0;
2728 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2729 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2730 if (vlan_dev == rt->dst.dev) {
2731 vlan_id = vlan->vlan_id;
2732 pr_debug("basa: vlan match on %s %d\n",
2733 vlan_dev->name, vlan_id);
2734 break;
2738 if (vlan_id) {
2739 ip_rt_put(rt);
2740 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2741 vlan->vlan_ip, vlan_id);
2742 continue;
2745 if (net_ratelimit()) {
2746 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2747 bond->dev->name, &fl.fl4_dst,
2748 rt->dst.dev ? rt->dst.dev->name : "NULL");
2750 ip_rt_put(rt);
2755 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2756 * for each VLAN above us.
2758 * Caller must hold curr_slave_lock for read or better
2760 static void bond_send_gratuitous_arp(struct bonding *bond)
2762 struct slave *slave = bond->curr_active_slave;
2763 struct vlan_entry *vlan;
2764 struct net_device *vlan_dev;
2766 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2767 bond->dev->name, slave ? slave->dev->name : "NULL");
2769 if (!slave || !bond->send_grat_arp ||
2770 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
2771 return;
2773 bond->send_grat_arp--;
2775 if (bond->master_ip) {
2776 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2777 bond->master_ip, 0);
2780 if (!bond->vlgrp)
2781 return;
2783 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2784 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2785 if (vlan->vlan_ip) {
2786 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2787 vlan->vlan_ip, vlan->vlan_id);
2792 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2794 int i;
2795 __be32 *targets = bond->params.arp_targets;
2797 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2798 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2799 &sip, &tip, i, &targets[i],
2800 bond_has_this_ip(bond, tip));
2801 if (sip == targets[i]) {
2802 if (bond_has_this_ip(bond, tip))
2803 slave->last_arp_rx = jiffies;
2804 return;
2809 static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2811 struct arphdr *arp;
2812 struct slave *slave;
2813 struct bonding *bond;
2814 unsigned char *arp_ptr;
2815 __be32 sip, tip;
2817 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2819 * When using VLANS and bonding, dev and oriv_dev may be
2820 * incorrect if the physical interface supports VLAN
2821 * acceleration. With this change ARP validation now
2822 * works for hosts only reachable on the VLAN interface.
2824 dev = vlan_dev_real_dev(dev);
2825 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2828 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2829 goto out;
2831 bond = netdev_priv(dev);
2832 read_lock(&bond->lock);
2834 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2835 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2836 orig_dev ? orig_dev->name : "NULL");
2838 slave = bond_get_slave_by_dev(bond, orig_dev);
2839 if (!slave || !slave_do_arp_validate(bond, slave))
2840 goto out_unlock;
2842 skb = skb_share_check(skb, GFP_ATOMIC);
2843 if (!skb)
2844 goto out_unlock;
2846 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
2847 goto out_unlock;
2849 arp = arp_hdr(skb);
2850 if (arp->ar_hln != dev->addr_len ||
2851 skb->pkt_type == PACKET_OTHERHOST ||
2852 skb->pkt_type == PACKET_LOOPBACK ||
2853 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2854 arp->ar_pro != htons(ETH_P_IP) ||
2855 arp->ar_pln != 4)
2856 goto out_unlock;
2858 arp_ptr = (unsigned char *)(arp + 1);
2859 arp_ptr += dev->addr_len;
2860 memcpy(&sip, arp_ptr, 4);
2861 arp_ptr += 4 + dev->addr_len;
2862 memcpy(&tip, arp_ptr, 4);
2864 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2865 bond->dev->name, slave->dev->name, slave->state,
2866 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2867 &sip, &tip);
2870 * Backup slaves won't see the ARP reply, but do come through
2871 * here for each ARP probe (so we swap the sip/tip to validate
2872 * the probe). In a "redundant switch, common router" type of
2873 * configuration, the ARP probe will (hopefully) travel from
2874 * the active, through one switch, the router, then the other
2875 * switch before reaching the backup.
2877 if (slave->state == BOND_STATE_ACTIVE)
2878 bond_validate_arp(bond, slave, sip, tip);
2879 else
2880 bond_validate_arp(bond, slave, tip, sip);
2882 out_unlock:
2883 read_unlock(&bond->lock);
2884 out:
2885 dev_kfree_skb(skb);
2886 return NET_RX_SUCCESS;
2890 * this function is called regularly to monitor each slave's link
2891 * ensuring that traffic is being sent and received when arp monitoring
2892 * is used in load-balancing mode. if the adapter has been dormant, then an
2893 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2894 * arp monitoring in active backup mode.
2896 void bond_loadbalance_arp_mon(struct work_struct *work)
2898 struct bonding *bond = container_of(work, struct bonding,
2899 arp_work.work);
2900 struct slave *slave, *oldcurrent;
2901 int do_failover = 0;
2902 int delta_in_ticks;
2903 int i;
2905 read_lock(&bond->lock);
2907 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2909 if (bond->kill_timers)
2910 goto out;
2912 if (bond->slave_cnt == 0)
2913 goto re_arm;
2915 read_lock(&bond->curr_slave_lock);
2916 oldcurrent = bond->curr_active_slave;
2917 read_unlock(&bond->curr_slave_lock);
2919 /* see if any of the previous devices are up now (i.e. they have
2920 * xmt and rcv traffic). the curr_active_slave does not come into
2921 * the picture unless it is null. also, slave->jiffies is not needed
2922 * here because we send an arp on each slave and give a slave as
2923 * long as it needs to get the tx/rx within the delta.
2924 * TODO: what about up/down delay in arp mode? it wasn't here before
2925 * so it can wait
2927 bond_for_each_slave(bond, slave, i) {
2928 unsigned long trans_start = dev_trans_start(slave->dev);
2930 if (slave->link != BOND_LINK_UP) {
2931 if (time_in_range(jiffies,
2932 trans_start - delta_in_ticks,
2933 trans_start + delta_in_ticks) &&
2934 time_in_range(jiffies,
2935 slave->dev->last_rx - delta_in_ticks,
2936 slave->dev->last_rx + delta_in_ticks)) {
2938 slave->link = BOND_LINK_UP;
2939 slave->state = BOND_STATE_ACTIVE;
2941 /* primary_slave has no meaning in round-robin
2942 * mode. the window of a slave being up and
2943 * curr_active_slave being null after enslaving
2944 * is closed.
2946 if (!oldcurrent) {
2947 pr_info("%s: link status definitely up for interface %s, ",
2948 bond->dev->name,
2949 slave->dev->name);
2950 do_failover = 1;
2951 } else {
2952 pr_info("%s: interface %s is now up\n",
2953 bond->dev->name,
2954 slave->dev->name);
2957 } else {
2958 /* slave->link == BOND_LINK_UP */
2960 /* not all switches will respond to an arp request
2961 * when the source ip is 0, so don't take the link down
2962 * if we don't know our ip yet
2964 if (!time_in_range(jiffies,
2965 trans_start - delta_in_ticks,
2966 trans_start + 2 * delta_in_ticks) ||
2967 !time_in_range(jiffies,
2968 slave->dev->last_rx - delta_in_ticks,
2969 slave->dev->last_rx + 2 * delta_in_ticks)) {
2971 slave->link = BOND_LINK_DOWN;
2972 slave->state = BOND_STATE_BACKUP;
2974 if (slave->link_failure_count < UINT_MAX)
2975 slave->link_failure_count++;
2977 pr_info("%s: interface %s is now down.\n",
2978 bond->dev->name,
2979 slave->dev->name);
2981 if (slave == oldcurrent)
2982 do_failover = 1;
2986 /* note: if switch is in round-robin mode, all links
2987 * must tx arp to ensure all links rx an arp - otherwise
2988 * links may oscillate or not come up at all; if switch is
2989 * in something like xor mode, there is nothing we can
2990 * do - all replies will be rx'ed on same link causing slaves
2991 * to be unstable during low/no traffic periods
2993 if (IS_UP(slave->dev))
2994 bond_arp_send_all(bond, slave);
2997 if (do_failover) {
2998 block_netpoll_tx();
2999 write_lock_bh(&bond->curr_slave_lock);
3001 bond_select_active_slave(bond);
3003 write_unlock_bh(&bond->curr_slave_lock);
3004 unblock_netpoll_tx();
3007 re_arm:
3008 if (bond->params.arp_interval)
3009 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3010 out:
3011 read_unlock(&bond->lock);
3015 * Called to inspect slaves for active-backup mode ARP monitor link state
3016 * changes. Sets new_link in slaves to specify what action should take
3017 * place for the slave. Returns 0 if no changes are found, >0 if changes
3018 * to link states must be committed.
3020 * Called with bond->lock held for read.
3022 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
3024 struct slave *slave;
3025 int i, commit = 0;
3026 unsigned long trans_start;
3028 bond_for_each_slave(bond, slave, i) {
3029 slave->new_link = BOND_LINK_NOCHANGE;
3031 if (slave->link != BOND_LINK_UP) {
3032 if (time_in_range(jiffies,
3033 slave_last_rx(bond, slave) - delta_in_ticks,
3034 slave_last_rx(bond, slave) + delta_in_ticks)) {
3036 slave->new_link = BOND_LINK_UP;
3037 commit++;
3040 continue;
3044 * Give slaves 2*delta after being enslaved or made
3045 * active. This avoids bouncing, as the last receive
3046 * times need a full ARP monitor cycle to be updated.
3048 if (time_in_range(jiffies,
3049 slave->jiffies - delta_in_ticks,
3050 slave->jiffies + 2 * delta_in_ticks))
3051 continue;
3054 * Backup slave is down if:
3055 * - No current_arp_slave AND
3056 * - more than 3*delta since last receive AND
3057 * - the bond has an IP address
3059 * Note: a non-null current_arp_slave indicates
3060 * the curr_active_slave went down and we are
3061 * searching for a new one; under this condition
3062 * we only take the curr_active_slave down - this
3063 * gives each slave a chance to tx/rx traffic
3064 * before being taken out
3066 if (slave->state == BOND_STATE_BACKUP &&
3067 !bond->current_arp_slave &&
3068 !time_in_range(jiffies,
3069 slave_last_rx(bond, slave) - delta_in_ticks,
3070 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3072 slave->new_link = BOND_LINK_DOWN;
3073 commit++;
3077 * Active slave is down if:
3078 * - more than 2*delta since transmitting OR
3079 * - (more than 2*delta since receive AND
3080 * the bond has an IP address)
3082 trans_start = dev_trans_start(slave->dev);
3083 if ((slave->state == BOND_STATE_ACTIVE) &&
3084 (!time_in_range(jiffies,
3085 trans_start - delta_in_ticks,
3086 trans_start + 2 * delta_in_ticks) ||
3087 !time_in_range(jiffies,
3088 slave_last_rx(bond, slave) - delta_in_ticks,
3089 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3091 slave->new_link = BOND_LINK_DOWN;
3092 commit++;
3096 return commit;
3100 * Called to commit link state changes noted by inspection step of
3101 * active-backup mode ARP monitor.
3103 * Called with RTNL and bond->lock for read.
3105 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3107 struct slave *slave;
3108 int i;
3109 unsigned long trans_start;
3111 bond_for_each_slave(bond, slave, i) {
3112 switch (slave->new_link) {
3113 case BOND_LINK_NOCHANGE:
3114 continue;
3116 case BOND_LINK_UP:
3117 trans_start = dev_trans_start(slave->dev);
3118 if ((!bond->curr_active_slave &&
3119 time_in_range(jiffies,
3120 trans_start - delta_in_ticks,
3121 trans_start + delta_in_ticks)) ||
3122 bond->curr_active_slave != slave) {
3123 slave->link = BOND_LINK_UP;
3124 bond->current_arp_slave = NULL;
3126 pr_info("%s: link status definitely up for interface %s.\n",
3127 bond->dev->name, slave->dev->name);
3129 if (!bond->curr_active_slave ||
3130 (slave == bond->primary_slave))
3131 goto do_failover;
3135 continue;
3137 case BOND_LINK_DOWN:
3138 if (slave->link_failure_count < UINT_MAX)
3139 slave->link_failure_count++;
3141 slave->link = BOND_LINK_DOWN;
3142 bond_set_slave_inactive_flags(slave);
3144 pr_info("%s: link status definitely down for interface %s, disabling it\n",
3145 bond->dev->name, slave->dev->name);
3147 if (slave == bond->curr_active_slave) {
3148 bond->current_arp_slave = NULL;
3149 goto do_failover;
3152 continue;
3154 default:
3155 pr_err("%s: impossible: new_link %d on slave %s\n",
3156 bond->dev->name, slave->new_link,
3157 slave->dev->name);
3158 continue;
3161 do_failover:
3162 ASSERT_RTNL();
3163 block_netpoll_tx();
3164 write_lock_bh(&bond->curr_slave_lock);
3165 bond_select_active_slave(bond);
3166 write_unlock_bh(&bond->curr_slave_lock);
3167 unblock_netpoll_tx();
3170 bond_set_carrier(bond);
3174 * Send ARP probes for active-backup mode ARP monitor.
3176 * Called with bond->lock held for read.
3178 static void bond_ab_arp_probe(struct bonding *bond)
3180 struct slave *slave;
3181 int i;
3183 read_lock(&bond->curr_slave_lock);
3185 if (bond->current_arp_slave && bond->curr_active_slave)
3186 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3187 bond->current_arp_slave->dev->name,
3188 bond->curr_active_slave->dev->name);
3190 if (bond->curr_active_slave) {
3191 bond_arp_send_all(bond, bond->curr_active_slave);
3192 read_unlock(&bond->curr_slave_lock);
3193 return;
3196 read_unlock(&bond->curr_slave_lock);
3198 /* if we don't have a curr_active_slave, search for the next available
3199 * backup slave from the current_arp_slave and make it the candidate
3200 * for becoming the curr_active_slave
3203 if (!bond->current_arp_slave) {
3204 bond->current_arp_slave = bond->first_slave;
3205 if (!bond->current_arp_slave)
3206 return;
3209 bond_set_slave_inactive_flags(bond->current_arp_slave);
3211 /* search for next candidate */
3212 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3213 if (IS_UP(slave->dev)) {
3214 slave->link = BOND_LINK_BACK;
3215 bond_set_slave_active_flags(slave);
3216 bond_arp_send_all(bond, slave);
3217 slave->jiffies = jiffies;
3218 bond->current_arp_slave = slave;
3219 break;
3222 /* if the link state is up at this point, we
3223 * mark it down - this can happen if we have
3224 * simultaneous link failures and
3225 * reselect_active_interface doesn't make this
3226 * one the current slave so it is still marked
3227 * up when it is actually down
3229 if (slave->link == BOND_LINK_UP) {
3230 slave->link = BOND_LINK_DOWN;
3231 if (slave->link_failure_count < UINT_MAX)
3232 slave->link_failure_count++;
3234 bond_set_slave_inactive_flags(slave);
3236 pr_info("%s: backup interface %s is now down.\n",
3237 bond->dev->name, slave->dev->name);
3242 void bond_activebackup_arp_mon(struct work_struct *work)
3244 struct bonding *bond = container_of(work, struct bonding,
3245 arp_work.work);
3246 int delta_in_ticks;
3248 read_lock(&bond->lock);
3250 if (bond->kill_timers)
3251 goto out;
3253 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3255 if (bond->slave_cnt == 0)
3256 goto re_arm;
3258 if (bond->send_grat_arp) {
3259 read_lock(&bond->curr_slave_lock);
3260 bond_send_gratuitous_arp(bond);
3261 read_unlock(&bond->curr_slave_lock);
3264 if (bond->send_unsol_na) {
3265 read_lock(&bond->curr_slave_lock);
3266 bond_send_unsolicited_na(bond);
3267 read_unlock(&bond->curr_slave_lock);
3270 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3271 read_unlock(&bond->lock);
3272 rtnl_lock();
3273 read_lock(&bond->lock);
3275 bond_ab_arp_commit(bond, delta_in_ticks);
3277 read_unlock(&bond->lock);
3278 rtnl_unlock();
3279 read_lock(&bond->lock);
3282 bond_ab_arp_probe(bond);
3284 re_arm:
3285 if (bond->params.arp_interval)
3286 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3287 out:
3288 read_unlock(&bond->lock);
3291 /*------------------------------ proc/seq_file-------------------------------*/
3293 #ifdef CONFIG_PROC_FS
3295 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3296 __acquires(RCU)
3297 __acquires(&bond->lock)
3299 struct bonding *bond = seq->private;
3300 loff_t off = 0;
3301 struct slave *slave;
3302 int i;
3304 /* make sure the bond won't be taken away */
3305 rcu_read_lock();
3306 read_lock(&bond->lock);
3308 if (*pos == 0)
3309 return SEQ_START_TOKEN;
3311 bond_for_each_slave(bond, slave, i) {
3312 if (++off == *pos)
3313 return slave;
3316 return NULL;
3319 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3321 struct bonding *bond = seq->private;
3322 struct slave *slave = v;
3324 ++*pos;
3325 if (v == SEQ_START_TOKEN)
3326 return bond->first_slave;
3328 slave = slave->next;
3330 return (slave == bond->first_slave) ? NULL : slave;
3333 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3334 __releases(&bond->lock)
3335 __releases(RCU)
3337 struct bonding *bond = seq->private;
3339 read_unlock(&bond->lock);
3340 rcu_read_unlock();
3343 static void bond_info_show_master(struct seq_file *seq)
3345 struct bonding *bond = seq->private;
3346 struct slave *curr;
3347 int i;
3349 read_lock(&bond->curr_slave_lock);
3350 curr = bond->curr_active_slave;
3351 read_unlock(&bond->curr_slave_lock);
3353 seq_printf(seq, "Bonding Mode: %s",
3354 bond_mode_name(bond->params.mode));
3356 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3357 bond->params.fail_over_mac)
3358 seq_printf(seq, " (fail_over_mac %s)",
3359 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
3361 seq_printf(seq, "\n");
3363 if (bond->params.mode == BOND_MODE_XOR ||
3364 bond->params.mode == BOND_MODE_8023AD) {
3365 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3366 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3367 bond->params.xmit_policy);
3370 if (USES_PRIMARY(bond->params.mode)) {
3371 seq_printf(seq, "Primary Slave: %s",
3372 (bond->primary_slave) ?
3373 bond->primary_slave->dev->name : "None");
3374 if (bond->primary_slave)
3375 seq_printf(seq, " (primary_reselect %s)",
3376 pri_reselect_tbl[bond->params.primary_reselect].modename);
3378 seq_printf(seq, "\nCurrently Active Slave: %s\n",
3379 (curr) ? curr->dev->name : "None");
3382 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3383 "up" : "down");
3384 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3385 seq_printf(seq, "Up Delay (ms): %d\n",
3386 bond->params.updelay * bond->params.miimon);
3387 seq_printf(seq, "Down Delay (ms): %d\n",
3388 bond->params.downdelay * bond->params.miimon);
3391 /* ARP information */
3392 if (bond->params.arp_interval > 0) {
3393 int printed = 0;
3394 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3395 bond->params.arp_interval);
3397 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3399 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
3400 if (!bond->params.arp_targets[i])
3401 break;
3402 if (printed)
3403 seq_printf(seq, ",");
3404 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
3405 printed = 1;
3407 seq_printf(seq, "\n");
3410 if (bond->params.mode == BOND_MODE_8023AD) {
3411 struct ad_info ad_info;
3413 seq_puts(seq, "\n802.3ad info\n");
3414 seq_printf(seq, "LACP rate: %s\n",
3415 (bond->params.lacp_fast) ? "fast" : "slow");
3416 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3417 ad_select_tbl[bond->params.ad_select].modename);
3419 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3420 seq_printf(seq, "bond %s has no active aggregator\n",
3421 bond->dev->name);
3422 } else {
3423 seq_printf(seq, "Active Aggregator Info:\n");
3425 seq_printf(seq, "\tAggregator ID: %d\n",
3426 ad_info.aggregator_id);
3427 seq_printf(seq, "\tNumber of ports: %d\n",
3428 ad_info.ports);
3429 seq_printf(seq, "\tActor Key: %d\n",
3430 ad_info.actor_key);
3431 seq_printf(seq, "\tPartner Key: %d\n",
3432 ad_info.partner_key);
3433 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3434 ad_info.partner_system);
3439 static void bond_info_show_slave(struct seq_file *seq,
3440 const struct slave *slave)
3442 struct bonding *bond = seq->private;
3444 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3445 seq_printf(seq, "MII Status: %s\n",
3446 (slave->link == BOND_LINK_UP) ? "up" : "down");
3447 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3448 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
3449 seq_printf(seq, "Link Failure Count: %u\n",
3450 slave->link_failure_count);
3452 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
3454 if (bond->params.mode == BOND_MODE_8023AD) {
3455 const struct aggregator *agg
3456 = SLAVE_AD_INFO(slave).port.aggregator;
3458 if (agg)
3459 seq_printf(seq, "Aggregator ID: %d\n",
3460 agg->aggregator_identifier);
3461 else
3462 seq_puts(seq, "Aggregator ID: N/A\n");
3464 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
3467 static int bond_info_seq_show(struct seq_file *seq, void *v)
3469 if (v == SEQ_START_TOKEN) {
3470 seq_printf(seq, "%s\n", version);
3471 bond_info_show_master(seq);
3472 } else
3473 bond_info_show_slave(seq, v);
3475 return 0;
3478 static const struct seq_operations bond_info_seq_ops = {
3479 .start = bond_info_seq_start,
3480 .next = bond_info_seq_next,
3481 .stop = bond_info_seq_stop,
3482 .show = bond_info_seq_show,
3485 static int bond_info_open(struct inode *inode, struct file *file)
3487 struct seq_file *seq;
3488 struct proc_dir_entry *proc;
3489 int res;
3491 res = seq_open(file, &bond_info_seq_ops);
3492 if (!res) {
3493 /* recover the pointer buried in proc_dir_entry data */
3494 seq = file->private_data;
3495 proc = PDE(inode);
3496 seq->private = proc->data;
3499 return res;
3502 static const struct file_operations bond_info_fops = {
3503 .owner = THIS_MODULE,
3504 .open = bond_info_open,
3505 .read = seq_read,
3506 .llseek = seq_lseek,
3507 .release = seq_release,
3510 static void bond_create_proc_entry(struct bonding *bond)
3512 struct net_device *bond_dev = bond->dev;
3513 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3515 if (bn->proc_dir) {
3516 bond->proc_entry = proc_create_data(bond_dev->name,
3517 S_IRUGO, bn->proc_dir,
3518 &bond_info_fops, bond);
3519 if (bond->proc_entry == NULL)
3520 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3521 DRV_NAME, bond_dev->name);
3522 else
3523 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3527 static void bond_remove_proc_entry(struct bonding *bond)
3529 struct net_device *bond_dev = bond->dev;
3530 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3532 if (bn->proc_dir && bond->proc_entry) {
3533 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
3534 memset(bond->proc_file_name, 0, IFNAMSIZ);
3535 bond->proc_entry = NULL;
3539 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3540 * Caller must hold rtnl_lock.
3542 static void __net_init bond_create_proc_dir(struct bond_net *bn)
3544 if (!bn->proc_dir) {
3545 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3546 if (!bn->proc_dir)
3547 pr_warning("Warning: cannot create /proc/net/%s\n",
3548 DRV_NAME);
3552 /* Destroy the bonding directory under /proc/net, if empty.
3553 * Caller must hold rtnl_lock.
3555 static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
3557 if (bn->proc_dir) {
3558 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3559 bn->proc_dir = NULL;
3563 #else /* !CONFIG_PROC_FS */
3565 static void bond_create_proc_entry(struct bonding *bond)
3569 static void bond_remove_proc_entry(struct bonding *bond)
3573 static inline void bond_create_proc_dir(struct bond_net *bn)
3577 static inline void bond_destroy_proc_dir(struct bond_net *bn)
3581 #endif /* CONFIG_PROC_FS */
3584 /*-------------------------- netdev event handling --------------------------*/
3587 * Change device name
3589 static int bond_event_changename(struct bonding *bond)
3591 bond_remove_proc_entry(bond);
3592 bond_create_proc_entry(bond);
3594 bond_debug_reregister(bond);
3596 return NOTIFY_DONE;
3599 static int bond_master_netdev_event(unsigned long event,
3600 struct net_device *bond_dev)
3602 struct bonding *event_bond = netdev_priv(bond_dev);
3604 switch (event) {
3605 case NETDEV_CHANGENAME:
3606 return bond_event_changename(event_bond);
3607 default:
3608 break;
3611 return NOTIFY_DONE;
3614 static int bond_slave_netdev_event(unsigned long event,
3615 struct net_device *slave_dev)
3617 struct net_device *bond_dev = slave_dev->master;
3618 struct bonding *bond = netdev_priv(bond_dev);
3620 switch (event) {
3621 case NETDEV_UNREGISTER:
3622 if (bond_dev) {
3623 if (bond->setup_by_slave)
3624 bond_release_and_destroy(bond_dev, slave_dev);
3625 else
3626 bond_release(bond_dev, slave_dev);
3628 break;
3629 case NETDEV_CHANGE:
3630 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3631 struct slave *slave;
3633 slave = bond_get_slave_by_dev(bond, slave_dev);
3634 if (slave) {
3635 u16 old_speed = slave->speed;
3636 u16 old_duplex = slave->duplex;
3638 bond_update_speed_duplex(slave);
3640 if (bond_is_lb(bond))
3641 break;
3643 if (old_speed != slave->speed)
3644 bond_3ad_adapter_speed_changed(slave);
3645 if (old_duplex != slave->duplex)
3646 bond_3ad_adapter_duplex_changed(slave);
3650 break;
3651 case NETDEV_DOWN:
3653 * ... Or is it this?
3655 break;
3656 case NETDEV_CHANGEMTU:
3658 * TODO: Should slaves be allowed to
3659 * independently alter their MTU? For
3660 * an active-backup bond, slaves need
3661 * not be the same type of device, so
3662 * MTUs may vary. For other modes,
3663 * slaves arguably should have the
3664 * same MTUs. To do this, we'd need to
3665 * take over the slave's change_mtu
3666 * function for the duration of their
3667 * servitude.
3669 break;
3670 case NETDEV_CHANGENAME:
3672 * TODO: handle changing the primary's name
3674 break;
3675 case NETDEV_FEAT_CHANGE:
3676 bond_compute_features(bond);
3677 break;
3678 default:
3679 break;
3682 return NOTIFY_DONE;
3686 * bond_netdev_event: handle netdev notifier chain events.
3688 * This function receives events for the netdev chain. The caller (an
3689 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3690 * locks for us to safely manipulate the slave devices (RTNL lock,
3691 * dev_probe_lock).
3693 static int bond_netdev_event(struct notifier_block *this,
3694 unsigned long event, void *ptr)
3696 struct net_device *event_dev = (struct net_device *)ptr;
3698 pr_debug("event_dev: %s, event: %lx\n",
3699 event_dev ? event_dev->name : "None",
3700 event);
3702 if (!(event_dev->priv_flags & IFF_BONDING))
3703 return NOTIFY_DONE;
3705 if (event_dev->flags & IFF_MASTER) {
3706 pr_debug("IFF_MASTER\n");
3707 return bond_master_netdev_event(event, event_dev);
3710 if (event_dev->flags & IFF_SLAVE) {
3711 pr_debug("IFF_SLAVE\n");
3712 return bond_slave_netdev_event(event, event_dev);
3715 return NOTIFY_DONE;
3719 * bond_inetaddr_event: handle inetaddr notifier chain events.
3721 * We keep track of device IPs primarily to use as source addresses in
3722 * ARP monitor probes (rather than spewing out broadcasts all the time).
3724 * We track one IP for the main device (if it has one), plus one per VLAN.
3726 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3728 struct in_ifaddr *ifa = ptr;
3729 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3730 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
3731 struct bonding *bond;
3732 struct vlan_entry *vlan;
3734 list_for_each_entry(bond, &bn->dev_list, bond_list) {
3735 if (bond->dev == event_dev) {
3736 switch (event) {
3737 case NETDEV_UP:
3738 bond->master_ip = ifa->ifa_local;
3739 return NOTIFY_OK;
3740 case NETDEV_DOWN:
3741 bond->master_ip = bond_glean_dev_ip(bond->dev);
3742 return NOTIFY_OK;
3743 default:
3744 return NOTIFY_DONE;
3748 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3749 if (!bond->vlgrp)
3750 continue;
3751 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3752 if (vlan_dev == event_dev) {
3753 switch (event) {
3754 case NETDEV_UP:
3755 vlan->vlan_ip = ifa->ifa_local;
3756 return NOTIFY_OK;
3757 case NETDEV_DOWN:
3758 vlan->vlan_ip =
3759 bond_glean_dev_ip(vlan_dev);
3760 return NOTIFY_OK;
3761 default:
3762 return NOTIFY_DONE;
3767 return NOTIFY_DONE;
3770 static struct notifier_block bond_netdev_notifier = {
3771 .notifier_call = bond_netdev_event,
3774 static struct notifier_block bond_inetaddr_notifier = {
3775 .notifier_call = bond_inetaddr_event,
3778 /*-------------------------- Packet type handling ---------------------------*/
3780 /* register to receive lacpdus on a bond */
3781 static void bond_register_lacpdu(struct bonding *bond)
3783 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3785 /* initialize packet type */
3786 pk_type->type = PKT_TYPE_LACPDU;
3787 pk_type->dev = bond->dev;
3788 pk_type->func = bond_3ad_lacpdu_recv;
3790 dev_add_pack(pk_type);
3793 /* unregister to receive lacpdus on a bond */
3794 static void bond_unregister_lacpdu(struct bonding *bond)
3796 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3799 void bond_register_arp(struct bonding *bond)
3801 struct packet_type *pt = &bond->arp_mon_pt;
3803 if (pt->type)
3804 return;
3806 pt->type = htons(ETH_P_ARP);
3807 pt->dev = bond->dev;
3808 pt->func = bond_arp_rcv;
3809 dev_add_pack(pt);
3812 void bond_unregister_arp(struct bonding *bond)
3814 struct packet_type *pt = &bond->arp_mon_pt;
3816 dev_remove_pack(pt);
3817 pt->type = 0;
3820 /*---------------------------- Hashing Policies -----------------------------*/
3823 * Hash for the output device based upon layer 2 and layer 3 data. If
3824 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3826 static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
3828 struct ethhdr *data = (struct ethhdr *)skb->data;
3829 struct iphdr *iph = ip_hdr(skb);
3831 if (skb->protocol == htons(ETH_P_IP)) {
3832 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3833 (data->h_dest[5] ^ data->h_source[5])) % count;
3836 return (data->h_dest[5] ^ data->h_source[5]) % count;
3840 * Hash for the output device based upon layer 3 and layer 4 data. If
3841 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3842 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3844 static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
3846 struct ethhdr *data = (struct ethhdr *)skb->data;
3847 struct iphdr *iph = ip_hdr(skb);
3848 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3849 int layer4_xor = 0;
3851 if (skb->protocol == htons(ETH_P_IP)) {
3852 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
3853 (iph->protocol == IPPROTO_TCP ||
3854 iph->protocol == IPPROTO_UDP)) {
3855 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3857 return (layer4_xor ^
3858 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3862 return (data->h_dest[5] ^ data->h_source[5]) % count;
3866 * Hash for the output device based upon layer 2 data
3868 static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
3870 struct ethhdr *data = (struct ethhdr *)skb->data;
3872 return (data->h_dest[5] ^ data->h_source[5]) % count;
3875 /*-------------------------- Device entry points ----------------------------*/
3877 static int bond_open(struct net_device *bond_dev)
3879 struct bonding *bond = netdev_priv(bond_dev);
3881 bond->kill_timers = 0;
3883 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3885 if (bond_is_lb(bond)) {
3886 /* bond_alb_initialize must be called before the timer
3887 * is started.
3889 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3890 /* something went wrong - fail the open operation */
3891 return -ENOMEM;
3894 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3895 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3898 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3899 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3900 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3903 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3904 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3905 INIT_DELAYED_WORK(&bond->arp_work,
3906 bond_activebackup_arp_mon);
3907 else
3908 INIT_DELAYED_WORK(&bond->arp_work,
3909 bond_loadbalance_arp_mon);
3911 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3912 if (bond->params.arp_validate)
3913 bond_register_arp(bond);
3916 if (bond->params.mode == BOND_MODE_8023AD) {
3917 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3918 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3919 /* register to receive LACPDUs */
3920 bond_register_lacpdu(bond);
3921 bond_3ad_initiate_agg_selection(bond, 1);
3924 return 0;
3927 static int bond_close(struct net_device *bond_dev)
3929 struct bonding *bond = netdev_priv(bond_dev);
3931 if (bond->params.mode == BOND_MODE_8023AD) {
3932 /* Unregister the receive of LACPDUs */
3933 bond_unregister_lacpdu(bond);
3936 if (bond->params.arp_validate)
3937 bond_unregister_arp(bond);
3939 write_lock_bh(&bond->lock);
3941 bond->send_grat_arp = 0;
3942 bond->send_unsol_na = 0;
3944 /* signal timers not to re-arm */
3945 bond->kill_timers = 1;
3947 write_unlock_bh(&bond->lock);
3949 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3950 cancel_delayed_work(&bond->mii_work);
3953 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3954 cancel_delayed_work(&bond->arp_work);
3957 switch (bond->params.mode) {
3958 case BOND_MODE_8023AD:
3959 cancel_delayed_work(&bond->ad_work);
3960 break;
3961 case BOND_MODE_TLB:
3962 case BOND_MODE_ALB:
3963 cancel_delayed_work(&bond->alb_work);
3964 break;
3965 default:
3966 break;
3969 if (delayed_work_pending(&bond->mcast_work))
3970 cancel_delayed_work(&bond->mcast_work);
3972 if (bond_is_lb(bond)) {
3973 /* Must be called only after all
3974 * slaves have been released
3976 bond_alb_deinitialize(bond);
3979 return 0;
3982 static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3983 struct rtnl_link_stats64 *stats)
3985 struct bonding *bond = netdev_priv(bond_dev);
3986 struct rtnl_link_stats64 temp;
3987 struct slave *slave;
3988 int i;
3990 memset(stats, 0, sizeof(*stats));
3992 read_lock_bh(&bond->lock);
3994 bond_for_each_slave(bond, slave, i) {
3995 const struct rtnl_link_stats64 *sstats =
3996 dev_get_stats(slave->dev, &temp);
3998 stats->rx_packets += sstats->rx_packets;
3999 stats->rx_bytes += sstats->rx_bytes;
4000 stats->rx_errors += sstats->rx_errors;
4001 stats->rx_dropped += sstats->rx_dropped;
4003 stats->tx_packets += sstats->tx_packets;
4004 stats->tx_bytes += sstats->tx_bytes;
4005 stats->tx_errors += sstats->tx_errors;
4006 stats->tx_dropped += sstats->tx_dropped;
4008 stats->multicast += sstats->multicast;
4009 stats->collisions += sstats->collisions;
4011 stats->rx_length_errors += sstats->rx_length_errors;
4012 stats->rx_over_errors += sstats->rx_over_errors;
4013 stats->rx_crc_errors += sstats->rx_crc_errors;
4014 stats->rx_frame_errors += sstats->rx_frame_errors;
4015 stats->rx_fifo_errors += sstats->rx_fifo_errors;
4016 stats->rx_missed_errors += sstats->rx_missed_errors;
4018 stats->tx_aborted_errors += sstats->tx_aborted_errors;
4019 stats->tx_carrier_errors += sstats->tx_carrier_errors;
4020 stats->tx_fifo_errors += sstats->tx_fifo_errors;
4021 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
4022 stats->tx_window_errors += sstats->tx_window_errors;
4025 read_unlock_bh(&bond->lock);
4027 return stats;
4030 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4032 struct net_device *slave_dev = NULL;
4033 struct ifbond k_binfo;
4034 struct ifbond __user *u_binfo = NULL;
4035 struct ifslave k_sinfo;
4036 struct ifslave __user *u_sinfo = NULL;
4037 struct mii_ioctl_data *mii = NULL;
4038 int res = 0;
4040 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
4042 switch (cmd) {
4043 case SIOCGMIIPHY:
4044 mii = if_mii(ifr);
4045 if (!mii)
4046 return -EINVAL;
4048 mii->phy_id = 0;
4049 /* Fall Through */
4050 case SIOCGMIIREG:
4052 * We do this again just in case we were called by SIOCGMIIREG
4053 * instead of SIOCGMIIPHY.
4055 mii = if_mii(ifr);
4056 if (!mii)
4057 return -EINVAL;
4060 if (mii->reg_num == 1) {
4061 struct bonding *bond = netdev_priv(bond_dev);
4062 mii->val_out = 0;
4063 read_lock(&bond->lock);
4064 read_lock(&bond->curr_slave_lock);
4065 if (netif_carrier_ok(bond->dev))
4066 mii->val_out = BMSR_LSTATUS;
4068 read_unlock(&bond->curr_slave_lock);
4069 read_unlock(&bond->lock);
4072 return 0;
4073 case BOND_INFO_QUERY_OLD:
4074 case SIOCBONDINFOQUERY:
4075 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4077 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
4078 return -EFAULT;
4080 res = bond_info_query(bond_dev, &k_binfo);
4081 if (res == 0 &&
4082 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4083 return -EFAULT;
4085 return res;
4086 case BOND_SLAVE_INFO_QUERY_OLD:
4087 case SIOCBONDSLAVEINFOQUERY:
4088 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4090 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
4091 return -EFAULT;
4093 res = bond_slave_info_query(bond_dev, &k_sinfo);
4094 if (res == 0 &&
4095 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4096 return -EFAULT;
4098 return res;
4099 default:
4100 /* Go on */
4101 break;
4104 if (!capable(CAP_NET_ADMIN))
4105 return -EPERM;
4107 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
4109 pr_debug("slave_dev=%p:\n", slave_dev);
4111 if (!slave_dev)
4112 res = -ENODEV;
4113 else {
4114 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
4115 switch (cmd) {
4116 case BOND_ENSLAVE_OLD:
4117 case SIOCBONDENSLAVE:
4118 res = bond_enslave(bond_dev, slave_dev);
4119 break;
4120 case BOND_RELEASE_OLD:
4121 case SIOCBONDRELEASE:
4122 res = bond_release(bond_dev, slave_dev);
4123 break;
4124 case BOND_SETHWADDR_OLD:
4125 case SIOCBONDSETHWADDR:
4126 res = bond_sethwaddr(bond_dev, slave_dev);
4127 break;
4128 case BOND_CHANGE_ACTIVE_OLD:
4129 case SIOCBONDCHANGEACTIVE:
4130 res = bond_ioctl_change_active(bond_dev, slave_dev);
4131 break;
4132 default:
4133 res = -EOPNOTSUPP;
4136 dev_put(slave_dev);
4139 return res;
4142 static bool bond_addr_in_mc_list(unsigned char *addr,
4143 struct netdev_hw_addr_list *list,
4144 int addrlen)
4146 struct netdev_hw_addr *ha;
4148 netdev_hw_addr_list_for_each(ha, list)
4149 if (!memcmp(ha->addr, addr, addrlen))
4150 return true;
4152 return false;
4155 static void bond_set_multicast_list(struct net_device *bond_dev)
4157 struct bonding *bond = netdev_priv(bond_dev);
4158 struct netdev_hw_addr *ha;
4159 bool found;
4162 * Do promisc before checking multicast_mode
4164 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
4166 * FIXME: Need to handle the error when one of the multi-slaves
4167 * encounters error.
4169 bond_set_promiscuity(bond, 1);
4172 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
4173 bond_set_promiscuity(bond, -1);
4176 /* set allmulti flag to slaves */
4177 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
4179 * FIXME: Need to handle the error when one of the multi-slaves
4180 * encounters error.
4182 bond_set_allmulti(bond, 1);
4185 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
4186 bond_set_allmulti(bond, -1);
4189 read_lock(&bond->lock);
4191 bond->flags = bond_dev->flags;
4193 /* looking for addresses to add to slaves' mc list */
4194 netdev_for_each_mc_addr(ha, bond_dev) {
4195 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4196 bond_dev->addr_len);
4197 if (!found)
4198 bond_mc_add(bond, ha->addr);
4201 /* looking for addresses to delete from slaves' list */
4202 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4203 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4204 bond_dev->addr_len);
4205 if (!found)
4206 bond_mc_del(bond, ha->addr);
4209 /* save master's multicast list */
4210 __hw_addr_flush(&bond->mc_list);
4211 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4212 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
4214 read_unlock(&bond->lock);
4217 static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4219 struct bonding *bond = netdev_priv(dev);
4220 struct slave *slave = bond->first_slave;
4222 if (slave) {
4223 const struct net_device_ops *slave_ops
4224 = slave->dev->netdev_ops;
4225 if (slave_ops->ndo_neigh_setup)
4226 return slave_ops->ndo_neigh_setup(slave->dev, parms);
4228 return 0;
4232 * Change the MTU of all of a master's slaves to match the master
4234 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4236 struct bonding *bond = netdev_priv(bond_dev);
4237 struct slave *slave, *stop_at;
4238 int res = 0;
4239 int i;
4241 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
4242 (bond_dev ? bond_dev->name : "None"), new_mtu);
4244 /* Can't hold bond->lock with bh disabled here since
4245 * some base drivers panic. On the other hand we can't
4246 * hold bond->lock without bh disabled because we'll
4247 * deadlock. The only solution is to rely on the fact
4248 * that we're under rtnl_lock here, and the slaves
4249 * list won't change. This doesn't solve the problem
4250 * of setting the slave's MTU while it is
4251 * transmitting, but the assumption is that the base
4252 * driver can handle that.
4254 * TODO: figure out a way to safely iterate the slaves
4255 * list, but without holding a lock around the actual
4256 * call to the base driver.
4259 bond_for_each_slave(bond, slave, i) {
4260 pr_debug("s %p s->p %p c_m %p\n",
4261 slave,
4262 slave->prev,
4263 slave->dev->netdev_ops->ndo_change_mtu);
4265 res = dev_set_mtu(slave->dev, new_mtu);
4267 if (res) {
4268 /* If we failed to set the slave's mtu to the new value
4269 * we must abort the operation even in ACTIVE_BACKUP
4270 * mode, because if we allow the backup slaves to have
4271 * different mtu values than the active slave we'll
4272 * need to change their mtu when doing a failover. That
4273 * means changing their mtu from timer context, which
4274 * is probably not a good idea.
4276 pr_debug("err %d %s\n", res, slave->dev->name);
4277 goto unwind;
4281 bond_dev->mtu = new_mtu;
4283 return 0;
4285 unwind:
4286 /* unwind from head to the slave that failed */
4287 stop_at = slave;
4288 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4289 int tmp_res;
4291 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4292 if (tmp_res) {
4293 pr_debug("unwind err %d dev %s\n",
4294 tmp_res, slave->dev->name);
4298 return res;
4302 * Change HW address
4304 * Note that many devices must be down to change the HW address, and
4305 * downing the master releases all slaves. We can make bonds full of
4306 * bonding devices to test this, however.
4308 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4310 struct bonding *bond = netdev_priv(bond_dev);
4311 struct sockaddr *sa = addr, tmp_sa;
4312 struct slave *slave, *stop_at;
4313 int res = 0;
4314 int i;
4316 if (bond->params.mode == BOND_MODE_ALB)
4317 return bond_alb_set_mac_address(bond_dev, addr);
4320 pr_debug("bond=%p, name=%s\n",
4321 bond, bond_dev ? bond_dev->name : "None");
4324 * If fail_over_mac is set to active, do nothing and return
4325 * success. Returning an error causes ifenslave to fail.
4327 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
4328 return 0;
4330 if (!is_valid_ether_addr(sa->sa_data))
4331 return -EADDRNOTAVAIL;
4333 /* Can't hold bond->lock with bh disabled here since
4334 * some base drivers panic. On the other hand we can't
4335 * hold bond->lock without bh disabled because we'll
4336 * deadlock. The only solution is to rely on the fact
4337 * that we're under rtnl_lock here, and the slaves
4338 * list won't change. This doesn't solve the problem
4339 * of setting the slave's hw address while it is
4340 * transmitting, but the assumption is that the base
4341 * driver can handle that.
4343 * TODO: figure out a way to safely iterate the slaves
4344 * list, but without holding a lock around the actual
4345 * call to the base driver.
4348 bond_for_each_slave(bond, slave, i) {
4349 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
4350 pr_debug("slave %p %s\n", slave, slave->dev->name);
4352 if (slave_ops->ndo_set_mac_address == NULL) {
4353 res = -EOPNOTSUPP;
4354 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
4355 goto unwind;
4358 res = dev_set_mac_address(slave->dev, addr);
4359 if (res) {
4360 /* TODO: consider downing the slave
4361 * and retry ?
4362 * User should expect communications
4363 * breakage anyway until ARP finish
4364 * updating, so...
4366 pr_debug("err %d %s\n", res, slave->dev->name);
4367 goto unwind;
4371 /* success */
4372 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4373 return 0;
4375 unwind:
4376 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4377 tmp_sa.sa_family = bond_dev->type;
4379 /* unwind from head to the slave that failed */
4380 stop_at = slave;
4381 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4382 int tmp_res;
4384 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4385 if (tmp_res) {
4386 pr_debug("unwind err %d dev %s\n",
4387 tmp_res, slave->dev->name);
4391 return res;
4394 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4396 struct bonding *bond = netdev_priv(bond_dev);
4397 struct slave *slave, *start_at;
4398 int i, slave_no, res = 1;
4399 struct iphdr *iph = ip_hdr(skb);
4401 read_lock(&bond->lock);
4403 if (!BOND_IS_OK(bond))
4404 goto out;
4406 * Start with the curr_active_slave that joined the bond as the
4407 * default for sending IGMP traffic. For failover purposes one
4408 * needs to maintain some consistency for the interface that will
4409 * send the join/membership reports. The curr_active_slave found
4410 * will send all of this type of traffic.
4412 if ((iph->protocol == IPPROTO_IGMP) &&
4413 (skb->protocol == htons(ETH_P_IP))) {
4415 read_lock(&bond->curr_slave_lock);
4416 slave = bond->curr_active_slave;
4417 read_unlock(&bond->curr_slave_lock);
4419 if (!slave)
4420 goto out;
4421 } else {
4423 * Concurrent TX may collide on rr_tx_counter; we accept
4424 * that as being rare enough not to justify using an
4425 * atomic op here.
4427 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4429 bond_for_each_slave(bond, slave, i) {
4430 slave_no--;
4431 if (slave_no < 0)
4432 break;
4436 start_at = slave;
4437 bond_for_each_slave_from(bond, slave, i, start_at) {
4438 if (IS_UP(slave->dev) &&
4439 (slave->link == BOND_LINK_UP) &&
4440 (slave->state == BOND_STATE_ACTIVE)) {
4441 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4442 break;
4446 out:
4447 if (res) {
4448 /* no suitable interface, frame not sent */
4449 dev_kfree_skb(skb);
4451 read_unlock(&bond->lock);
4452 return NETDEV_TX_OK;
4457 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4458 * the bond has a usable interface.
4460 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4462 struct bonding *bond = netdev_priv(bond_dev);
4463 int res = 1;
4465 read_lock(&bond->lock);
4466 read_lock(&bond->curr_slave_lock);
4468 if (!BOND_IS_OK(bond))
4469 goto out;
4471 if (!bond->curr_active_slave)
4472 goto out;
4474 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4476 out:
4477 if (res)
4478 /* no suitable interface, frame not sent */
4479 dev_kfree_skb(skb);
4481 read_unlock(&bond->curr_slave_lock);
4482 read_unlock(&bond->lock);
4483 return NETDEV_TX_OK;
4487 * In bond_xmit_xor() , we determine the output device by using a pre-
4488 * determined xmit_hash_policy(), If the selected device is not enabled,
4489 * find the next active slave.
4491 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4493 struct bonding *bond = netdev_priv(bond_dev);
4494 struct slave *slave, *start_at;
4495 int slave_no;
4496 int i;
4497 int res = 1;
4499 read_lock(&bond->lock);
4501 if (!BOND_IS_OK(bond))
4502 goto out;
4504 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
4506 bond_for_each_slave(bond, slave, i) {
4507 slave_no--;
4508 if (slave_no < 0)
4509 break;
4512 start_at = slave;
4514 bond_for_each_slave_from(bond, slave, i, start_at) {
4515 if (IS_UP(slave->dev) &&
4516 (slave->link == BOND_LINK_UP) &&
4517 (slave->state == BOND_STATE_ACTIVE)) {
4518 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4519 break;
4523 out:
4524 if (res) {
4525 /* no suitable interface, frame not sent */
4526 dev_kfree_skb(skb);
4528 read_unlock(&bond->lock);
4529 return NETDEV_TX_OK;
4533 * in broadcast mode, we send everything to all usable interfaces.
4535 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4537 struct bonding *bond = netdev_priv(bond_dev);
4538 struct slave *slave, *start_at;
4539 struct net_device *tx_dev = NULL;
4540 int i;
4541 int res = 1;
4543 read_lock(&bond->lock);
4545 if (!BOND_IS_OK(bond))
4546 goto out;
4548 read_lock(&bond->curr_slave_lock);
4549 start_at = bond->curr_active_slave;
4550 read_unlock(&bond->curr_slave_lock);
4552 if (!start_at)
4553 goto out;
4555 bond_for_each_slave_from(bond, slave, i, start_at) {
4556 if (IS_UP(slave->dev) &&
4557 (slave->link == BOND_LINK_UP) &&
4558 (slave->state == BOND_STATE_ACTIVE)) {
4559 if (tx_dev) {
4560 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4561 if (!skb2) {
4562 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
4563 bond_dev->name);
4564 continue;
4567 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4568 if (res) {
4569 dev_kfree_skb(skb2);
4570 continue;
4573 tx_dev = slave->dev;
4577 if (tx_dev)
4578 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4580 out:
4581 if (res)
4582 /* no suitable interface, frame not sent */
4583 dev_kfree_skb(skb);
4585 /* frame sent to all suitable interfaces */
4586 read_unlock(&bond->lock);
4587 return NETDEV_TX_OK;
4590 /*------------------------- Device initialization ---------------------------*/
4592 static void bond_set_xmit_hash_policy(struct bonding *bond)
4594 switch (bond->params.xmit_policy) {
4595 case BOND_XMIT_POLICY_LAYER23:
4596 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4597 break;
4598 case BOND_XMIT_POLICY_LAYER34:
4599 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4600 break;
4601 case BOND_XMIT_POLICY_LAYER2:
4602 default:
4603 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4604 break;
4609 * Lookup the slave that corresponds to a qid
4611 static inline int bond_slave_override(struct bonding *bond,
4612 struct sk_buff *skb)
4614 int i, res = 1;
4615 struct slave *slave = NULL;
4616 struct slave *check_slave;
4618 read_lock(&bond->lock);
4620 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4621 goto out;
4623 /* Find out if any slaves have the same mapping as this skb. */
4624 bond_for_each_slave(bond, check_slave, i) {
4625 if (check_slave->queue_id == skb->queue_mapping) {
4626 slave = check_slave;
4627 break;
4631 /* If the slave isn't UP, use default transmit policy. */
4632 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4633 (slave->link == BOND_LINK_UP)) {
4634 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4637 out:
4638 read_unlock(&bond->lock);
4639 return res;
4642 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4645 * This helper function exists to help dev_pick_tx get the correct
4646 * destination queue. Using a helper function skips the a call to
4647 * skb_tx_hash and will put the skbs in the queue we expect on their
4648 * way down to the bonding driver.
4650 return skb->queue_mapping;
4653 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4655 struct bonding *bond = netdev_priv(dev);
4658 * If we risk deadlock from transmitting this in the
4659 * netpoll path, tell netpoll to queue the frame for later tx
4661 if (is_netpoll_tx_blocked(dev))
4662 return NETDEV_TX_BUSY;
4664 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4665 if (!bond_slave_override(bond, skb))
4666 return NETDEV_TX_OK;
4669 switch (bond->params.mode) {
4670 case BOND_MODE_ROUNDROBIN:
4671 return bond_xmit_roundrobin(skb, dev);
4672 case BOND_MODE_ACTIVEBACKUP:
4673 return bond_xmit_activebackup(skb, dev);
4674 case BOND_MODE_XOR:
4675 return bond_xmit_xor(skb, dev);
4676 case BOND_MODE_BROADCAST:
4677 return bond_xmit_broadcast(skb, dev);
4678 case BOND_MODE_8023AD:
4679 return bond_3ad_xmit_xor(skb, dev);
4680 case BOND_MODE_ALB:
4681 case BOND_MODE_TLB:
4682 return bond_alb_xmit(skb, dev);
4683 default:
4684 /* Should never happen, mode already checked */
4685 pr_err("%s: Error: Unknown bonding mode %d\n",
4686 dev->name, bond->params.mode);
4687 WARN_ON_ONCE(1);
4688 dev_kfree_skb(skb);
4689 return NETDEV_TX_OK;
4695 * set bond mode specific net device operations
4697 void bond_set_mode_ops(struct bonding *bond, int mode)
4699 struct net_device *bond_dev = bond->dev;
4701 switch (mode) {
4702 case BOND_MODE_ROUNDROBIN:
4703 break;
4704 case BOND_MODE_ACTIVEBACKUP:
4705 break;
4706 case BOND_MODE_XOR:
4707 bond_set_xmit_hash_policy(bond);
4708 break;
4709 case BOND_MODE_BROADCAST:
4710 break;
4711 case BOND_MODE_8023AD:
4712 bond_set_master_3ad_flags(bond);
4713 bond_set_xmit_hash_policy(bond);
4714 break;
4715 case BOND_MODE_ALB:
4716 bond_set_master_alb_flags(bond);
4717 /* FALLTHRU */
4718 case BOND_MODE_TLB:
4719 break;
4720 default:
4721 /* Should never happen, mode already checked */
4722 pr_err("%s: Error: Unknown bonding mode %d\n",
4723 bond_dev->name, mode);
4724 break;
4728 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4729 struct ethtool_drvinfo *drvinfo)
4731 strncpy(drvinfo->driver, DRV_NAME, 32);
4732 strncpy(drvinfo->version, DRV_VERSION, 32);
4733 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4736 static const struct ethtool_ops bond_ethtool_ops = {
4737 .get_drvinfo = bond_ethtool_get_drvinfo,
4738 .get_link = ethtool_op_get_link,
4739 .get_tx_csum = ethtool_op_get_tx_csum,
4740 .get_sg = ethtool_op_get_sg,
4741 .get_tso = ethtool_op_get_tso,
4742 .get_ufo = ethtool_op_get_ufo,
4743 .get_flags = ethtool_op_get_flags,
4746 static const struct net_device_ops bond_netdev_ops = {
4747 .ndo_init = bond_init,
4748 .ndo_uninit = bond_uninit,
4749 .ndo_open = bond_open,
4750 .ndo_stop = bond_close,
4751 .ndo_start_xmit = bond_start_xmit,
4752 .ndo_select_queue = bond_select_queue,
4753 .ndo_get_stats64 = bond_get_stats,
4754 .ndo_do_ioctl = bond_do_ioctl,
4755 .ndo_set_multicast_list = bond_set_multicast_list,
4756 .ndo_change_mtu = bond_change_mtu,
4757 .ndo_set_mac_address = bond_set_mac_address,
4758 .ndo_neigh_setup = bond_neigh_setup,
4759 .ndo_vlan_rx_register = bond_vlan_rx_register,
4760 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4761 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4762 #ifdef CONFIG_NET_POLL_CONTROLLER
4763 .ndo_netpoll_setup = bond_netpoll_setup,
4764 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4765 .ndo_poll_controller = bond_poll_controller,
4766 #endif
4767 .ndo_add_slave = bond_enslave,
4768 .ndo_del_slave = bond_release,
4771 static void bond_destructor(struct net_device *bond_dev)
4773 struct bonding *bond = netdev_priv(bond_dev);
4774 if (bond->wq)
4775 destroy_workqueue(bond->wq);
4776 free_netdev(bond_dev);
4779 static void bond_setup(struct net_device *bond_dev)
4781 struct bonding *bond = netdev_priv(bond_dev);
4783 /* initialize rwlocks */
4784 rwlock_init(&bond->lock);
4785 rwlock_init(&bond->curr_slave_lock);
4787 bond->params = bonding_defaults;
4789 /* Initialize pointers */
4790 bond->dev = bond_dev;
4791 INIT_LIST_HEAD(&bond->vlan_list);
4793 /* Initialize the device entry points */
4794 ether_setup(bond_dev);
4795 bond_dev->netdev_ops = &bond_netdev_ops;
4796 bond_dev->ethtool_ops = &bond_ethtool_ops;
4797 bond_set_mode_ops(bond, bond->params.mode);
4799 bond_dev->destructor = bond_destructor;
4801 /* Initialize the device options */
4802 bond_dev->tx_queue_len = 0;
4803 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4804 bond_dev->priv_flags |= IFF_BONDING;
4805 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4807 if (bond->params.arp_interval)
4808 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
4810 /* At first, we block adding VLANs. That's the only way to
4811 * prevent problems that occur when adding VLANs over an
4812 * empty bond. The block will be removed once non-challenged
4813 * slaves are enslaved.
4815 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4817 /* don't acquire bond device's netif_tx_lock when
4818 * transmitting */
4819 bond_dev->features |= NETIF_F_LLTX;
4821 /* By default, we declare the bond to be fully
4822 * VLAN hardware accelerated capable. Special
4823 * care is taken in the various xmit functions
4824 * when there are slaves that are not hw accel
4825 * capable
4827 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4828 NETIF_F_HW_VLAN_RX |
4829 NETIF_F_HW_VLAN_FILTER);
4831 /* By default, we enable GRO on bonding devices.
4832 * Actual support requires lowlevel drivers are GRO ready.
4834 bond_dev->features |= NETIF_F_GRO;
4837 static void bond_work_cancel_all(struct bonding *bond)
4839 write_lock_bh(&bond->lock);
4840 bond->kill_timers = 1;
4841 write_unlock_bh(&bond->lock);
4843 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4844 cancel_delayed_work(&bond->mii_work);
4846 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4847 cancel_delayed_work(&bond->arp_work);
4849 if (bond->params.mode == BOND_MODE_ALB &&
4850 delayed_work_pending(&bond->alb_work))
4851 cancel_delayed_work(&bond->alb_work);
4853 if (bond->params.mode == BOND_MODE_8023AD &&
4854 delayed_work_pending(&bond->ad_work))
4855 cancel_delayed_work(&bond->ad_work);
4857 if (delayed_work_pending(&bond->mcast_work))
4858 cancel_delayed_work(&bond->mcast_work);
4862 * Destroy a bonding device.
4863 * Must be under rtnl_lock when this function is called.
4865 static void bond_uninit(struct net_device *bond_dev)
4867 struct bonding *bond = netdev_priv(bond_dev);
4868 struct vlan_entry *vlan, *tmp;
4870 bond_netpoll_cleanup(bond_dev);
4872 /* Release the bonded slaves */
4873 bond_release_all(bond_dev);
4875 list_del(&bond->bond_list);
4877 bond_work_cancel_all(bond);
4879 bond_remove_proc_entry(bond);
4881 bond_debug_unregister(bond);
4883 __hw_addr_flush(&bond->mc_list);
4885 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4886 list_del(&vlan->vlan_list);
4887 kfree(vlan);
4891 /*------------------------- Module initialization ---------------------------*/
4894 * Convert string input module parms. Accept either the
4895 * number of the mode or its string name. A bit complicated because
4896 * some mode names are substrings of other names, and calls from sysfs
4897 * may have whitespace in the name (trailing newlines, for example).
4899 int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
4901 int modeint = -1, i, rv;
4902 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4904 for (p = (char *)buf; *p; p++)
4905 if (!(isdigit(*p) || isspace(*p)))
4906 break;
4908 if (*p)
4909 rv = sscanf(buf, "%20s", modestr);
4910 else
4911 rv = sscanf(buf, "%d", &modeint);
4913 if (!rv)
4914 return -1;
4916 for (i = 0; tbl[i].modename; i++) {
4917 if (modeint == tbl[i].mode)
4918 return tbl[i].mode;
4919 if (strcmp(modestr, tbl[i].modename) == 0)
4920 return tbl[i].mode;
4923 return -1;
4926 static int bond_check_params(struct bond_params *params)
4928 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
4931 * Convert string parameters.
4933 if (mode) {
4934 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4935 if (bond_mode == -1) {
4936 pr_err("Error: Invalid bonding mode \"%s\"\n",
4937 mode == NULL ? "NULL" : mode);
4938 return -EINVAL;
4942 if (xmit_hash_policy) {
4943 if ((bond_mode != BOND_MODE_XOR) &&
4944 (bond_mode != BOND_MODE_8023AD)) {
4945 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4946 bond_mode_name(bond_mode));
4947 } else {
4948 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4949 xmit_hashtype_tbl);
4950 if (xmit_hashtype == -1) {
4951 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4952 xmit_hash_policy == NULL ? "NULL" :
4953 xmit_hash_policy);
4954 return -EINVAL;
4959 if (lacp_rate) {
4960 if (bond_mode != BOND_MODE_8023AD) {
4961 pr_info("lacp_rate param is irrelevant in mode %s\n",
4962 bond_mode_name(bond_mode));
4963 } else {
4964 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4965 if (lacp_fast == -1) {
4966 pr_err("Error: Invalid lacp rate \"%s\"\n",
4967 lacp_rate == NULL ? "NULL" : lacp_rate);
4968 return -EINVAL;
4973 if (ad_select) {
4974 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4975 if (params->ad_select == -1) {
4976 pr_err("Error: Invalid ad_select \"%s\"\n",
4977 ad_select == NULL ? "NULL" : ad_select);
4978 return -EINVAL;
4981 if (bond_mode != BOND_MODE_8023AD) {
4982 pr_warning("ad_select param only affects 802.3ad mode\n");
4984 } else {
4985 params->ad_select = BOND_AD_STABLE;
4988 if (max_bonds < 0) {
4989 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4990 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4991 max_bonds = BOND_DEFAULT_MAX_BONDS;
4994 if (miimon < 0) {
4995 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4996 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4997 miimon = BOND_LINK_MON_INTERV;
5000 if (updelay < 0) {
5001 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5002 updelay, INT_MAX);
5003 updelay = 0;
5006 if (downdelay < 0) {
5007 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5008 downdelay, INT_MAX);
5009 downdelay = 0;
5012 if ((use_carrier != 0) && (use_carrier != 1)) {
5013 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
5014 use_carrier);
5015 use_carrier = 1;
5018 if (num_grat_arp < 0 || num_grat_arp > 255) {
5019 pr_warning("Warning: num_grat_arp (%d) not in range 0-255 so it was reset to 1\n",
5020 num_grat_arp);
5021 num_grat_arp = 1;
5024 if (num_unsol_na < 0 || num_unsol_na > 255) {
5025 pr_warning("Warning: num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
5026 num_unsol_na);
5027 num_unsol_na = 1;
5030 /* reset values for 802.3ad */
5031 if (bond_mode == BOND_MODE_8023AD) {
5032 if (!miimon) {
5033 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");
5034 pr_warning("Forcing miimon to 100msec\n");
5035 miimon = 100;
5039 if (tx_queues < 1 || tx_queues > 255) {
5040 pr_warning("Warning: tx_queues (%d) should be between "
5041 "1 and 255, resetting to %d\n",
5042 tx_queues, BOND_DEFAULT_TX_QUEUES);
5043 tx_queues = BOND_DEFAULT_TX_QUEUES;
5046 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
5047 pr_warning("Warning: all_slaves_active module parameter (%d), "
5048 "not of valid value (0/1), so it was set to "
5049 "0\n", all_slaves_active);
5050 all_slaves_active = 0;
5053 if (resend_igmp < 0 || resend_igmp > 255) {
5054 pr_warning("Warning: resend_igmp (%d) should be between "
5055 "0 and 255, resetting to %d\n",
5056 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
5057 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
5060 /* reset values for TLB/ALB */
5061 if ((bond_mode == BOND_MODE_TLB) ||
5062 (bond_mode == BOND_MODE_ALB)) {
5063 if (!miimon) {
5064 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");
5065 pr_warning("Forcing miimon to 100msec\n");
5066 miimon = 100;
5070 if (bond_mode == BOND_MODE_ALB) {
5071 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",
5072 updelay);
5075 if (!miimon) {
5076 if (updelay || downdelay) {
5077 /* just warn the user the up/down delay will have
5078 * no effect since miimon is zero...
5080 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",
5081 updelay, downdelay);
5083 } else {
5084 /* don't allow arp monitoring */
5085 if (arp_interval) {
5086 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5087 miimon, arp_interval);
5088 arp_interval = 0;
5091 if ((updelay % miimon) != 0) {
5092 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5093 updelay, miimon,
5094 (updelay / miimon) * miimon);
5097 updelay /= miimon;
5099 if ((downdelay % miimon) != 0) {
5100 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5101 downdelay, miimon,
5102 (downdelay / miimon) * miimon);
5105 downdelay /= miimon;
5108 if (arp_interval < 0) {
5109 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5110 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
5111 arp_interval = BOND_LINK_ARP_INTERV;
5114 for (arp_ip_count = 0;
5115 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5116 arp_ip_count++) {
5117 /* not complete check, but should be good enough to
5118 catch mistakes */
5119 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
5120 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5121 arp_ip_target[arp_ip_count]);
5122 arp_interval = 0;
5123 } else {
5124 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
5125 arp_target[arp_ip_count] = ip;
5129 if (arp_interval && !arp_ip_count) {
5130 /* don't allow arping if no arp_ip_target given... */
5131 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5132 arp_interval);
5133 arp_interval = 0;
5136 if (arp_validate) {
5137 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
5138 pr_err("arp_validate only supported in active-backup mode\n");
5139 return -EINVAL;
5141 if (!arp_interval) {
5142 pr_err("arp_validate requires arp_interval\n");
5143 return -EINVAL;
5146 arp_validate_value = bond_parse_parm(arp_validate,
5147 arp_validate_tbl);
5148 if (arp_validate_value == -1) {
5149 pr_err("Error: invalid arp_validate \"%s\"\n",
5150 arp_validate == NULL ? "NULL" : arp_validate);
5151 return -EINVAL;
5153 } else
5154 arp_validate_value = 0;
5156 if (miimon) {
5157 pr_info("MII link monitoring set to %d ms\n", miimon);
5158 } else if (arp_interval) {
5159 int i;
5161 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5162 arp_interval,
5163 arp_validate_tbl[arp_validate_value].modename,
5164 arp_ip_count);
5166 for (i = 0; i < arp_ip_count; i++)
5167 pr_info(" %s", arp_ip_target[i]);
5169 pr_info("\n");
5171 } else if (max_bonds) {
5172 /* miimon and arp_interval not set, we need one so things
5173 * work as expected, see bonding.txt for details
5175 pr_warning("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details.\n");
5178 if (primary && !USES_PRIMARY(bond_mode)) {
5179 /* currently, using a primary only makes sense
5180 * in active backup, TLB or ALB modes
5182 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5183 primary, bond_mode_name(bond_mode));
5184 primary = NULL;
5187 if (primary && primary_reselect) {
5188 primary_reselect_value = bond_parse_parm(primary_reselect,
5189 pri_reselect_tbl);
5190 if (primary_reselect_value == -1) {
5191 pr_err("Error: Invalid primary_reselect \"%s\"\n",
5192 primary_reselect ==
5193 NULL ? "NULL" : primary_reselect);
5194 return -EINVAL;
5196 } else {
5197 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5200 if (fail_over_mac) {
5201 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5202 fail_over_mac_tbl);
5203 if (fail_over_mac_value == -1) {
5204 pr_err("Error: invalid fail_over_mac \"%s\"\n",
5205 arp_validate == NULL ? "NULL" : arp_validate);
5206 return -EINVAL;
5209 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5210 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
5211 } else {
5212 fail_over_mac_value = BOND_FOM_NONE;
5215 /* fill params struct with the proper values */
5216 params->mode = bond_mode;
5217 params->xmit_policy = xmit_hashtype;
5218 params->miimon = miimon;
5219 params->num_grat_arp = num_grat_arp;
5220 params->num_unsol_na = num_unsol_na;
5221 params->arp_interval = arp_interval;
5222 params->arp_validate = arp_validate_value;
5223 params->updelay = updelay;
5224 params->downdelay = downdelay;
5225 params->use_carrier = use_carrier;
5226 params->lacp_fast = lacp_fast;
5227 params->primary[0] = 0;
5228 params->primary_reselect = primary_reselect_value;
5229 params->fail_over_mac = fail_over_mac_value;
5230 params->tx_queues = tx_queues;
5231 params->all_slaves_active = all_slaves_active;
5232 params->resend_igmp = resend_igmp;
5234 if (primary) {
5235 strncpy(params->primary, primary, IFNAMSIZ);
5236 params->primary[IFNAMSIZ - 1] = 0;
5239 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5241 return 0;
5244 static struct lock_class_key bonding_netdev_xmit_lock_key;
5245 static struct lock_class_key bonding_netdev_addr_lock_key;
5247 static void bond_set_lockdep_class_one(struct net_device *dev,
5248 struct netdev_queue *txq,
5249 void *_unused)
5251 lockdep_set_class(&txq->_xmit_lock,
5252 &bonding_netdev_xmit_lock_key);
5255 static void bond_set_lockdep_class(struct net_device *dev)
5257 lockdep_set_class(&dev->addr_list_lock,
5258 &bonding_netdev_addr_lock_key);
5259 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
5263 * Called from registration process
5265 static int bond_init(struct net_device *bond_dev)
5267 struct bonding *bond = netdev_priv(bond_dev);
5268 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
5270 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5272 bond->wq = create_singlethread_workqueue(bond_dev->name);
5273 if (!bond->wq)
5274 return -ENOMEM;
5276 bond_set_lockdep_class(bond_dev);
5278 netif_carrier_off(bond_dev);
5280 bond_create_proc_entry(bond);
5281 list_add_tail(&bond->bond_list, &bn->dev_list);
5283 bond_prepare_sysfs_group(bond);
5285 bond_debug_register(bond);
5287 __hw_addr_init(&bond->mc_list);
5288 return 0;
5291 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5293 if (tb[IFLA_ADDRESS]) {
5294 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5295 return -EINVAL;
5296 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5297 return -EADDRNOTAVAIL;
5299 return 0;
5302 static struct rtnl_link_ops bond_link_ops __read_mostly = {
5303 .kind = "bond",
5304 .priv_size = sizeof(struct bonding),
5305 .setup = bond_setup,
5306 .validate = bond_validate,
5309 /* Create a new bond based on the specified name and bonding parameters.
5310 * If name is NULL, obtain a suitable "bond%d" name for us.
5311 * Caller must NOT hold rtnl_lock; we need to release it here before we
5312 * set up our sysfs entries.
5314 int bond_create(struct net *net, const char *name)
5316 struct net_device *bond_dev;
5317 int res;
5319 rtnl_lock();
5321 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5322 bond_setup, tx_queues);
5323 if (!bond_dev) {
5324 pr_err("%s: eek! can't alloc netdev!\n", name);
5325 rtnl_unlock();
5326 return -ENOMEM;
5329 dev_net_set(bond_dev, net);
5330 bond_dev->rtnl_link_ops = &bond_link_ops;
5332 if (!name) {
5333 res = dev_alloc_name(bond_dev, "bond%d");
5334 if (res < 0)
5335 goto out;
5336 } else {
5338 * If we're given a name to register
5339 * we need to ensure that its not already
5340 * registered
5342 res = -EEXIST;
5343 if (__dev_get_by_name(net, name) != NULL)
5344 goto out;
5347 res = register_netdevice(bond_dev);
5349 out:
5350 rtnl_unlock();
5351 if (res < 0)
5352 bond_destructor(bond_dev);
5353 return res;
5356 static int __net_init bond_net_init(struct net *net)
5358 struct bond_net *bn = net_generic(net, bond_net_id);
5360 bn->net = net;
5361 INIT_LIST_HEAD(&bn->dev_list);
5363 bond_create_proc_dir(bn);
5365 return 0;
5368 static void __net_exit bond_net_exit(struct net *net)
5370 struct bond_net *bn = net_generic(net, bond_net_id);
5372 bond_destroy_proc_dir(bn);
5375 static struct pernet_operations bond_net_ops = {
5376 .init = bond_net_init,
5377 .exit = bond_net_exit,
5378 .id = &bond_net_id,
5379 .size = sizeof(struct bond_net),
5382 static int __init bonding_init(void)
5384 int i;
5385 int res;
5387 pr_info("%s", version);
5389 res = bond_check_params(&bonding_defaults);
5390 if (res)
5391 goto out;
5393 res = register_pernet_subsys(&bond_net_ops);
5394 if (res)
5395 goto out;
5397 res = rtnl_link_register(&bond_link_ops);
5398 if (res)
5399 goto err_link;
5401 bond_create_debugfs();
5403 for (i = 0; i < max_bonds; i++) {
5404 res = bond_create(&init_net, NULL);
5405 if (res)
5406 goto err;
5409 res = bond_create_sysfs();
5410 if (res)
5411 goto err;
5413 register_netdevice_notifier(&bond_netdev_notifier);
5414 register_inetaddr_notifier(&bond_inetaddr_notifier);
5415 bond_register_ipv6_notifier();
5416 out:
5417 return res;
5418 err:
5419 rtnl_link_unregister(&bond_link_ops);
5420 err_link:
5421 unregister_pernet_subsys(&bond_net_ops);
5422 goto out;
5426 static void __exit bonding_exit(void)
5428 unregister_netdevice_notifier(&bond_netdev_notifier);
5429 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
5430 bond_unregister_ipv6_notifier();
5432 bond_destroy_sysfs();
5433 bond_destroy_debugfs();
5435 rtnl_link_unregister(&bond_link_ops);
5436 unregister_pernet_subsys(&bond_net_ops);
5438 #ifdef CONFIG_NET_POLL_CONTROLLER
5440 * Make sure we don't have an imbalance on our netpoll blocking
5442 WARN_ON(atomic_read(&netpoll_block_tx));
5443 #endif
5446 module_init(bonding_init);
5447 module_exit(bonding_exit);
5448 MODULE_LICENSE("GPL");
5449 MODULE_VERSION(DRV_VERSION);
5450 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5451 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5452 MODULE_ALIAS_RTNL_LINK("bond");