Merge branch 'for-davem' of git://git.kernel.org/pub/scm/linux/kernel/git/linville...
[deliverable/linux.git] / drivers / net / bonding / bond_main.c
1 /*
2 * originally based on the dummy device.
3 *
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6 *
7 * bonding.c: an Ethernet Bonding driver
8 *
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 ...
15 *
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.
22 *
23 * ifconfig bond0 down
24 * will release all slaves, marking them as down.
25 *
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.
31 *
32 */
33
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
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/smp.h>
69 #include <linux/if_ether.h>
70 #include <net/arp.h>
71 #include <linux/mii.h>
72 #include <linux/ethtool.h>
73 #include <linux/if_vlan.h>
74 #include <linux/if_bonding.h>
75 #include <linux/jiffies.h>
76 #include <linux/preempt.h>
77 #include <net/route.h>
78 #include <net/net_namespace.h>
79 #include <net/netns/generic.h>
80 #include "bonding.h"
81 #include "bond_3ad.h"
82 #include "bond_alb.h"
83
84 /*---------------------------- Module parameters ----------------------------*/
85
86 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
87 #define BOND_LINK_MON_INTERV 0
88 #define BOND_LINK_ARP_INTERV 0
89
90 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
91 static int tx_queues = BOND_DEFAULT_TX_QUEUES;
92 static int miimon = BOND_LINK_MON_INTERV;
93 static int updelay;
94 static int downdelay;
95 static int use_carrier = 1;
96 static char *mode;
97 static char *primary;
98 static char *primary_reselect;
99 static char *lacp_rate;
100 static char *ad_select;
101 static char *xmit_hash_policy;
102 static int arp_interval = BOND_LINK_ARP_INTERV;
103 static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
104 static char *arp_validate;
105 static char *fail_over_mac;
106 static int all_slaves_active = 0;
107 static struct bond_params bonding_defaults;
108 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
109
110 module_param(max_bonds, int, 0);
111 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
112 module_param(tx_queues, int, 0);
113 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
114 module_param(miimon, int, 0);
115 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
116 module_param(updelay, int, 0);
117 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
118 module_param(downdelay, int, 0);
119 MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
120 "in milliseconds");
121 module_param(use_carrier, int, 0);
122 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
123 "0 for off, 1 for on (default)");
124 module_param(mode, charp, 0);
125 MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
126 "1 for active-backup, 2 for balance-xor, "
127 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
128 "6 for balance-alb");
129 module_param(primary, charp, 0);
130 MODULE_PARM_DESC(primary, "Primary network device to use");
131 module_param(primary_reselect, charp, 0);
132 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
133 "once it comes up; "
134 "0 for always (default), "
135 "1 for only if speed of primary is "
136 "better, "
137 "2 for only on active slave "
138 "failure");
139 module_param(lacp_rate, charp, 0);
140 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
141 "(slow/fast)");
142 module_param(ad_select, charp, 0);
143 MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
144 module_param(xmit_hash_policy, charp, 0);
145 MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
146 ", 1 for layer 3+4");
147 module_param(arp_interval, int, 0);
148 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
149 module_param_array(arp_ip_target, charp, NULL, 0);
150 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
151 module_param(arp_validate, charp, 0);
152 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
153 module_param(fail_over_mac, charp, 0);
154 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
155 module_param(all_slaves_active, int, 0);
156 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
157 "by setting active flag for all slaves. "
158 "0 for never (default), 1 for always.");
159 module_param(resend_igmp, int, 0);
160 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
161
162 /*----------------------------- Global variables ----------------------------*/
163
164 #ifdef CONFIG_NET_POLL_CONTROLLER
165 atomic_t netpoll_block_tx = ATOMIC_INIT(0);
166 #endif
167
168 int bond_net_id __read_mostly;
169
170 static __be32 arp_target[BOND_MAX_ARP_TARGETS];
171 static int arp_ip_count;
172 static int bond_mode = BOND_MODE_ROUNDROBIN;
173 static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
174 static int lacp_fast;
175
176 const struct bond_parm_tbl bond_lacp_tbl[] = {
177 { "slow", AD_LACP_SLOW},
178 { "fast", AD_LACP_FAST},
179 { NULL, -1},
180 };
181
182 const struct bond_parm_tbl bond_mode_tbl[] = {
183 { "balance-rr", BOND_MODE_ROUNDROBIN},
184 { "active-backup", BOND_MODE_ACTIVEBACKUP},
185 { "balance-xor", BOND_MODE_XOR},
186 { "broadcast", BOND_MODE_BROADCAST},
187 { "802.3ad", BOND_MODE_8023AD},
188 { "balance-tlb", BOND_MODE_TLB},
189 { "balance-alb", BOND_MODE_ALB},
190 { NULL, -1},
191 };
192
193 const struct bond_parm_tbl xmit_hashtype_tbl[] = {
194 { "layer2", BOND_XMIT_POLICY_LAYER2},
195 { "layer3+4", BOND_XMIT_POLICY_LAYER34},
196 { "layer2+3", BOND_XMIT_POLICY_LAYER23},
197 { NULL, -1},
198 };
199
200 const struct bond_parm_tbl arp_validate_tbl[] = {
201 { "none", BOND_ARP_VALIDATE_NONE},
202 { "active", BOND_ARP_VALIDATE_ACTIVE},
203 { "backup", BOND_ARP_VALIDATE_BACKUP},
204 { "all", BOND_ARP_VALIDATE_ALL},
205 { NULL, -1},
206 };
207
208 const struct bond_parm_tbl fail_over_mac_tbl[] = {
209 { "none", BOND_FOM_NONE},
210 { "active", BOND_FOM_ACTIVE},
211 { "follow", BOND_FOM_FOLLOW},
212 { NULL, -1},
213 };
214
215 const struct bond_parm_tbl pri_reselect_tbl[] = {
216 { "always", BOND_PRI_RESELECT_ALWAYS},
217 { "better", BOND_PRI_RESELECT_BETTER},
218 { "failure", BOND_PRI_RESELECT_FAILURE},
219 { NULL, -1},
220 };
221
222 struct bond_parm_tbl ad_select_tbl[] = {
223 { "stable", BOND_AD_STABLE},
224 { "bandwidth", BOND_AD_BANDWIDTH},
225 { "count", BOND_AD_COUNT},
226 { NULL, -1},
227 };
228
229 /*-------------------------- Forward declarations ---------------------------*/
230
231 static int bond_init(struct net_device *bond_dev);
232 static void bond_uninit(struct net_device *bond_dev);
233
234 /*---------------------------- General routines -----------------------------*/
235
236 const char *bond_mode_name(int mode)
237 {
238 static const char *names[] = {
239 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
240 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
241 [BOND_MODE_XOR] = "load balancing (xor)",
242 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
243 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
244 [BOND_MODE_TLB] = "transmit load balancing",
245 [BOND_MODE_ALB] = "adaptive load balancing",
246 };
247
248 if (mode < 0 || mode > BOND_MODE_ALB)
249 return "unknown";
250
251 return names[mode];
252 }
253
254 /*---------------------------------- VLAN -----------------------------------*/
255
256 /**
257 * bond_add_vlan - add a new vlan id on bond
258 * @bond: bond that got the notification
259 * @vlan_id: the vlan id to add
260 *
261 * Returns -ENOMEM if allocation failed.
262 */
263 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
264 {
265 struct vlan_entry *vlan;
266
267 pr_debug("bond: %s, vlan id %d\n",
268 (bond ? bond->dev->name : "None"), vlan_id);
269
270 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
271 if (!vlan)
272 return -ENOMEM;
273
274 INIT_LIST_HEAD(&vlan->vlan_list);
275 vlan->vlan_id = vlan_id;
276
277 write_lock_bh(&bond->lock);
278
279 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
280
281 write_unlock_bh(&bond->lock);
282
283 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
284
285 return 0;
286 }
287
288 /**
289 * bond_del_vlan - delete a vlan id from bond
290 * @bond: bond that got the notification
291 * @vlan_id: the vlan id to delete
292 *
293 * returns -ENODEV if @vlan_id was not found in @bond.
294 */
295 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
296 {
297 struct vlan_entry *vlan;
298 int res = -ENODEV;
299
300 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
301
302 block_netpoll_tx();
303 write_lock_bh(&bond->lock);
304
305 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
306 if (vlan->vlan_id == vlan_id) {
307 list_del(&vlan->vlan_list);
308
309 if (bond_is_lb(bond))
310 bond_alb_clear_vlan(bond, vlan_id);
311
312 pr_debug("removed VLAN ID %d from bond %s\n",
313 vlan_id, bond->dev->name);
314
315 kfree(vlan);
316
317 if (list_empty(&bond->vlan_list) &&
318 (bond->slave_cnt == 0)) {
319 /* Last VLAN removed and no slaves, so
320 * restore block on adding VLANs. This will
321 * be removed once new slaves that are not
322 * VLAN challenged will be added.
323 */
324 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
325 }
326
327 res = 0;
328 goto out;
329 }
330 }
331
332 pr_debug("couldn't find VLAN ID %d in bond %s\n",
333 vlan_id, bond->dev->name);
334
335 out:
336 write_unlock_bh(&bond->lock);
337 unblock_netpoll_tx();
338 return res;
339 }
340
341 /**
342 * bond_has_challenged_slaves
343 * @bond: the bond we're working on
344 *
345 * Searches the slave list. Returns 1 if a vlan challenged slave
346 * was found, 0 otherwise.
347 *
348 * Assumes bond->lock is held.
349 */
350 static int bond_has_challenged_slaves(struct bonding *bond)
351 {
352 struct slave *slave;
353 int i;
354
355 bond_for_each_slave(bond, slave, i) {
356 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
357 pr_debug("found VLAN challenged slave - %s\n",
358 slave->dev->name);
359 return 1;
360 }
361 }
362
363 pr_debug("no VLAN challenged slaves found\n");
364 return 0;
365 }
366
367 /**
368 * bond_next_vlan - safely skip to the next item in the vlans list.
369 * @bond: the bond we're working on
370 * @curr: item we're advancing from
371 *
372 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
373 * or @curr->next otherwise (even if it is @curr itself again).
374 *
375 * Caller must hold bond->lock
376 */
377 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
378 {
379 struct vlan_entry *next, *last;
380
381 if (list_empty(&bond->vlan_list))
382 return NULL;
383
384 if (!curr) {
385 next = list_entry(bond->vlan_list.next,
386 struct vlan_entry, vlan_list);
387 } else {
388 last = list_entry(bond->vlan_list.prev,
389 struct vlan_entry, vlan_list);
390 if (last == curr) {
391 next = list_entry(bond->vlan_list.next,
392 struct vlan_entry, vlan_list);
393 } else {
394 next = list_entry(curr->vlan_list.next,
395 struct vlan_entry, vlan_list);
396 }
397 }
398
399 return next;
400 }
401
402 /**
403 * bond_dev_queue_xmit - Prepare skb for xmit.
404 *
405 * @bond: bond device that got this skb for tx.
406 * @skb: hw accel VLAN tagged skb to transmit
407 * @slave_dev: slave that is supposed to xmit this skbuff
408 */
409 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
410 struct net_device *slave_dev)
411 {
412 skb->dev = slave_dev;
413 skb->priority = 1;
414 if (unlikely(netpoll_tx_running(slave_dev)))
415 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
416 else
417 dev_queue_xmit(skb);
418
419 return 0;
420 }
421
422 /*
423 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
424 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
425 * lock because:
426 * a. This operation is performed in IOCTL context,
427 * b. The operation is protected by the RTNL semaphore in the 8021q code,
428 * c. Holding a lock with BH disabled while directly calling a base driver
429 * entry point is generally a BAD idea.
430 *
431 * The design of synchronization/protection for this operation in the 8021q
432 * module is good for one or more VLAN devices over a single physical device
433 * and cannot be extended for a teaming solution like bonding, so there is a
434 * potential race condition here where a net device from the vlan group might
435 * be referenced (either by a base driver or the 8021q code) while it is being
436 * removed from the system. However, it turns out we're not making matters
437 * worse, and if it works for regular VLAN usage it will work here too.
438 */
439
440 /**
441 * bond_vlan_rx_register - Propagates registration to slaves
442 * @bond_dev: bonding net device that got called
443 * @grp: vlan group being registered
444 */
445 static void bond_vlan_rx_register(struct net_device *bond_dev,
446 struct vlan_group *grp)
447 {
448 struct bonding *bond = netdev_priv(bond_dev);
449 struct slave *slave;
450 int i;
451
452 write_lock_bh(&bond->lock);
453 bond->vlgrp = grp;
454 write_unlock_bh(&bond->lock);
455
456 bond_for_each_slave(bond, slave, i) {
457 struct net_device *slave_dev = slave->dev;
458 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
459
460 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
461 slave_ops->ndo_vlan_rx_register) {
462 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
463 }
464 }
465 }
466
467 /**
468 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
469 * @bond_dev: bonding net device that got called
470 * @vid: vlan id being added
471 */
472 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
473 {
474 struct bonding *bond = netdev_priv(bond_dev);
475 struct slave *slave;
476 int i, res;
477
478 bond_for_each_slave(bond, slave, i) {
479 struct net_device *slave_dev = slave->dev;
480 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
481
482 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
483 slave_ops->ndo_vlan_rx_add_vid) {
484 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
485 }
486 }
487
488 res = bond_add_vlan(bond, vid);
489 if (res) {
490 pr_err("%s: Error: Failed to add vlan id %d\n",
491 bond_dev->name, vid);
492 }
493 }
494
495 /**
496 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
497 * @bond_dev: bonding net device that got called
498 * @vid: vlan id being removed
499 */
500 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
501 {
502 struct bonding *bond = netdev_priv(bond_dev);
503 struct slave *slave;
504 struct net_device *vlan_dev;
505 int i, res;
506
507 bond_for_each_slave(bond, slave, i) {
508 struct net_device *slave_dev = slave->dev;
509 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
510
511 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
512 slave_ops->ndo_vlan_rx_kill_vid) {
513 /* Save and then restore vlan_dev in the grp array,
514 * since the slave's driver might clear it.
515 */
516 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
517 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
518 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
519 }
520 }
521
522 res = bond_del_vlan(bond, vid);
523 if (res) {
524 pr_err("%s: Error: Failed to remove vlan id %d\n",
525 bond_dev->name, vid);
526 }
527 }
528
529 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
530 {
531 struct vlan_entry *vlan;
532 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
533
534 if (!bond->vlgrp)
535 return;
536
537 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
538 slave_ops->ndo_vlan_rx_register)
539 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
540
541 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
542 !(slave_ops->ndo_vlan_rx_add_vid))
543 return;
544
545 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
546 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
547 }
548
549 static void bond_del_vlans_from_slave(struct bonding *bond,
550 struct net_device *slave_dev)
551 {
552 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
553 struct vlan_entry *vlan;
554 struct net_device *vlan_dev;
555
556 if (!bond->vlgrp)
557 return;
558
559 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
560 !(slave_ops->ndo_vlan_rx_kill_vid))
561 goto unreg;
562
563 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
564 if (!vlan->vlan_id)
565 continue;
566 /* Save and then restore vlan_dev in the grp array,
567 * since the slave's driver might clear it.
568 */
569 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
570 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
571 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
572 }
573
574 unreg:
575 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
576 slave_ops->ndo_vlan_rx_register)
577 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
578 }
579
580 /*------------------------------- Link status -------------------------------*/
581
582 /*
583 * Set the carrier state for the master according to the state of its
584 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
585 * do special 802.3ad magic.
586 *
587 * Returns zero if carrier state does not change, nonzero if it does.
588 */
589 static int bond_set_carrier(struct bonding *bond)
590 {
591 struct slave *slave;
592 int i;
593
594 if (bond->slave_cnt == 0)
595 goto down;
596
597 if (bond->params.mode == BOND_MODE_8023AD)
598 return bond_3ad_set_carrier(bond);
599
600 bond_for_each_slave(bond, slave, i) {
601 if (slave->link == BOND_LINK_UP) {
602 if (!netif_carrier_ok(bond->dev)) {
603 netif_carrier_on(bond->dev);
604 return 1;
605 }
606 return 0;
607 }
608 }
609
610 down:
611 if (netif_carrier_ok(bond->dev)) {
612 netif_carrier_off(bond->dev);
613 return 1;
614 }
615 return 0;
616 }
617
618 /*
619 * Get link speed and duplex from the slave's base driver
620 * using ethtool. If for some reason the call fails or the
621 * values are invalid, fake speed and duplex to 100/Full
622 * and return error.
623 */
624 static int bond_update_speed_duplex(struct slave *slave)
625 {
626 struct net_device *slave_dev = slave->dev;
627 struct ethtool_cmd etool = { .cmd = ETHTOOL_GSET };
628 u32 slave_speed;
629 int res;
630
631 /* Fake speed and duplex */
632 slave->speed = SPEED_100;
633 slave->duplex = DUPLEX_FULL;
634
635 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
636 return -1;
637
638 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
639 if (res < 0)
640 return -1;
641
642 slave_speed = ethtool_cmd_speed(&etool);
643 switch (slave_speed) {
644 case SPEED_10:
645 case SPEED_100:
646 case SPEED_1000:
647 case SPEED_10000:
648 break;
649 default:
650 return -1;
651 }
652
653 switch (etool.duplex) {
654 case DUPLEX_FULL:
655 case DUPLEX_HALF:
656 break;
657 default:
658 return -1;
659 }
660
661 slave->speed = slave_speed;
662 slave->duplex = etool.duplex;
663
664 return 0;
665 }
666
667 /*
668 * if <dev> supports MII link status reporting, check its link status.
669 *
670 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
671 * depending upon the setting of the use_carrier parameter.
672 *
673 * Return either BMSR_LSTATUS, meaning that the link is up (or we
674 * can't tell and just pretend it is), or 0, meaning that the link is
675 * down.
676 *
677 * If reporting is non-zero, instead of faking link up, return -1 if
678 * both ETHTOOL and MII ioctls fail (meaning the device does not
679 * support them). If use_carrier is set, return whatever it says.
680 * It'd be nice if there was a good way to tell if a driver supports
681 * netif_carrier, but there really isn't.
682 */
683 static int bond_check_dev_link(struct bonding *bond,
684 struct net_device *slave_dev, int reporting)
685 {
686 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
687 int (*ioctl)(struct net_device *, struct ifreq *, int);
688 struct ifreq ifr;
689 struct mii_ioctl_data *mii;
690
691 if (!reporting && !netif_running(slave_dev))
692 return 0;
693
694 if (bond->params.use_carrier)
695 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
696
697 /* Try to get link status using Ethtool first. */
698 if (slave_dev->ethtool_ops) {
699 if (slave_dev->ethtool_ops->get_link) {
700 u32 link;
701
702 link = slave_dev->ethtool_ops->get_link(slave_dev);
703
704 return link ? BMSR_LSTATUS : 0;
705 }
706 }
707
708 /* Ethtool can't be used, fallback to MII ioctls. */
709 ioctl = slave_ops->ndo_do_ioctl;
710 if (ioctl) {
711 /* TODO: set pointer to correct ioctl on a per team member */
712 /* bases to make this more efficient. that is, once */
713 /* we determine the correct ioctl, we will always */
714 /* call it and not the others for that team */
715 /* member. */
716
717 /*
718 * We cannot assume that SIOCGMIIPHY will also read a
719 * register; not all network drivers (e.g., e100)
720 * support that.
721 */
722
723 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
724 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
725 mii = if_mii(&ifr);
726 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
727 mii->reg_num = MII_BMSR;
728 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
729 return mii->val_out & BMSR_LSTATUS;
730 }
731 }
732
733 /*
734 * If reporting, report that either there's no dev->do_ioctl,
735 * or both SIOCGMIIREG and get_link failed (meaning that we
736 * cannot report link status). If not reporting, pretend
737 * we're ok.
738 */
739 return reporting ? -1 : BMSR_LSTATUS;
740 }
741
742 /*----------------------------- Multicast list ------------------------------*/
743
744 /*
745 * Push the promiscuity flag down to appropriate slaves
746 */
747 static int bond_set_promiscuity(struct bonding *bond, int inc)
748 {
749 int err = 0;
750 if (USES_PRIMARY(bond->params.mode)) {
751 /* write lock already acquired */
752 if (bond->curr_active_slave) {
753 err = dev_set_promiscuity(bond->curr_active_slave->dev,
754 inc);
755 }
756 } else {
757 struct slave *slave;
758 int i;
759 bond_for_each_slave(bond, slave, i) {
760 err = dev_set_promiscuity(slave->dev, inc);
761 if (err)
762 return err;
763 }
764 }
765 return err;
766 }
767
768 /*
769 * Push the allmulti flag down to all slaves
770 */
771 static int bond_set_allmulti(struct bonding *bond, int inc)
772 {
773 int err = 0;
774 if (USES_PRIMARY(bond->params.mode)) {
775 /* write lock already acquired */
776 if (bond->curr_active_slave) {
777 err = dev_set_allmulti(bond->curr_active_slave->dev,
778 inc);
779 }
780 } else {
781 struct slave *slave;
782 int i;
783 bond_for_each_slave(bond, slave, i) {
784 err = dev_set_allmulti(slave->dev, inc);
785 if (err)
786 return err;
787 }
788 }
789 return err;
790 }
791
792 /*
793 * Add a Multicast address to slaves
794 * according to mode
795 */
796 static void bond_mc_add(struct bonding *bond, void *addr)
797 {
798 if (USES_PRIMARY(bond->params.mode)) {
799 /* write lock already acquired */
800 if (bond->curr_active_slave)
801 dev_mc_add(bond->curr_active_slave->dev, addr);
802 } else {
803 struct slave *slave;
804 int i;
805
806 bond_for_each_slave(bond, slave, i)
807 dev_mc_add(slave->dev, addr);
808 }
809 }
810
811 /*
812 * Remove a multicast address from slave
813 * according to mode
814 */
815 static void bond_mc_del(struct bonding *bond, void *addr)
816 {
817 if (USES_PRIMARY(bond->params.mode)) {
818 /* write lock already acquired */
819 if (bond->curr_active_slave)
820 dev_mc_del(bond->curr_active_slave->dev, addr);
821 } else {
822 struct slave *slave;
823 int i;
824 bond_for_each_slave(bond, slave, i) {
825 dev_mc_del(slave->dev, addr);
826 }
827 }
828 }
829
830
831 static void __bond_resend_igmp_join_requests(struct net_device *dev)
832 {
833 struct in_device *in_dev;
834
835 rcu_read_lock();
836 in_dev = __in_dev_get_rcu(dev);
837 if (in_dev)
838 ip_mc_rejoin_groups(in_dev);
839 rcu_read_unlock();
840 }
841
842 /*
843 * Retrieve the list of registered multicast addresses for the bonding
844 * device and retransmit an IGMP JOIN request to the current active
845 * slave.
846 */
847 static void bond_resend_igmp_join_requests(struct bonding *bond)
848 {
849 struct net_device *vlan_dev;
850 struct vlan_entry *vlan;
851
852 read_lock(&bond->lock);
853
854 /* rejoin all groups on bond device */
855 __bond_resend_igmp_join_requests(bond->dev);
856
857 /* rejoin all groups on vlan devices */
858 if (bond->vlgrp) {
859 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
860 vlan_dev = vlan_group_get_device(bond->vlgrp,
861 vlan->vlan_id);
862 if (vlan_dev)
863 __bond_resend_igmp_join_requests(vlan_dev);
864 }
865 }
866
867 if (--bond->igmp_retrans > 0)
868 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
869
870 read_unlock(&bond->lock);
871 }
872
873 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
874 {
875 struct bonding *bond = container_of(work, struct bonding,
876 mcast_work.work);
877 bond_resend_igmp_join_requests(bond);
878 }
879
880 /*
881 * flush all members of flush->mc_list from device dev->mc_list
882 */
883 static void bond_mc_list_flush(struct net_device *bond_dev,
884 struct net_device *slave_dev)
885 {
886 struct bonding *bond = netdev_priv(bond_dev);
887 struct netdev_hw_addr *ha;
888
889 netdev_for_each_mc_addr(ha, bond_dev)
890 dev_mc_del(slave_dev, ha->addr);
891
892 if (bond->params.mode == BOND_MODE_8023AD) {
893 /* del lacpdu mc addr from mc list */
894 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
895
896 dev_mc_del(slave_dev, lacpdu_multicast);
897 }
898 }
899
900 /*--------------------------- Active slave change ---------------------------*/
901
902 /*
903 * Update the mc list and multicast-related flags for the new and
904 * old active slaves (if any) according to the multicast mode, and
905 * promiscuous flags unconditionally.
906 */
907 static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
908 struct slave *old_active)
909 {
910 struct netdev_hw_addr *ha;
911
912 if (!USES_PRIMARY(bond->params.mode))
913 /* nothing to do - mc list is already up-to-date on
914 * all slaves
915 */
916 return;
917
918 if (old_active) {
919 if (bond->dev->flags & IFF_PROMISC)
920 dev_set_promiscuity(old_active->dev, -1);
921
922 if (bond->dev->flags & IFF_ALLMULTI)
923 dev_set_allmulti(old_active->dev, -1);
924
925 netdev_for_each_mc_addr(ha, bond->dev)
926 dev_mc_del(old_active->dev, ha->addr);
927 }
928
929 if (new_active) {
930 /* FIXME: Signal errors upstream. */
931 if (bond->dev->flags & IFF_PROMISC)
932 dev_set_promiscuity(new_active->dev, 1);
933
934 if (bond->dev->flags & IFF_ALLMULTI)
935 dev_set_allmulti(new_active->dev, 1);
936
937 netdev_for_each_mc_addr(ha, bond->dev)
938 dev_mc_add(new_active->dev, ha->addr);
939 }
940 }
941
942 /*
943 * bond_do_fail_over_mac
944 *
945 * Perform special MAC address swapping for fail_over_mac settings
946 *
947 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
948 */
949 static void bond_do_fail_over_mac(struct bonding *bond,
950 struct slave *new_active,
951 struct slave *old_active)
952 __releases(&bond->curr_slave_lock)
953 __releases(&bond->lock)
954 __acquires(&bond->lock)
955 __acquires(&bond->curr_slave_lock)
956 {
957 u8 tmp_mac[ETH_ALEN];
958 struct sockaddr saddr;
959 int rv;
960
961 switch (bond->params.fail_over_mac) {
962 case BOND_FOM_ACTIVE:
963 if (new_active)
964 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
965 new_active->dev->addr_len);
966 break;
967 case BOND_FOM_FOLLOW:
968 /*
969 * if new_active && old_active, swap them
970 * if just old_active, do nothing (going to no active slave)
971 * if just new_active, set new_active to bond's MAC
972 */
973 if (!new_active)
974 return;
975
976 write_unlock_bh(&bond->curr_slave_lock);
977 read_unlock(&bond->lock);
978
979 if (old_active) {
980 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
981 memcpy(saddr.sa_data, old_active->dev->dev_addr,
982 ETH_ALEN);
983 saddr.sa_family = new_active->dev->type;
984 } else {
985 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
986 saddr.sa_family = bond->dev->type;
987 }
988
989 rv = dev_set_mac_address(new_active->dev, &saddr);
990 if (rv) {
991 pr_err("%s: Error %d setting MAC of slave %s\n",
992 bond->dev->name, -rv, new_active->dev->name);
993 goto out;
994 }
995
996 if (!old_active)
997 goto out;
998
999 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1000 saddr.sa_family = old_active->dev->type;
1001
1002 rv = dev_set_mac_address(old_active->dev, &saddr);
1003 if (rv)
1004 pr_err("%s: Error %d setting MAC of slave %s\n",
1005 bond->dev->name, -rv, new_active->dev->name);
1006 out:
1007 read_lock(&bond->lock);
1008 write_lock_bh(&bond->curr_slave_lock);
1009 break;
1010 default:
1011 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
1012 bond->dev->name, bond->params.fail_over_mac);
1013 break;
1014 }
1015
1016 }
1017
1018 static bool bond_should_change_active(struct bonding *bond)
1019 {
1020 struct slave *prim = bond->primary_slave;
1021 struct slave *curr = bond->curr_active_slave;
1022
1023 if (!prim || !curr || curr->link != BOND_LINK_UP)
1024 return true;
1025 if (bond->force_primary) {
1026 bond->force_primary = false;
1027 return true;
1028 }
1029 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1030 (prim->speed < curr->speed ||
1031 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1032 return false;
1033 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1034 return false;
1035 return true;
1036 }
1037
1038 /**
1039 * find_best_interface - select the best available slave to be the active one
1040 * @bond: our bonding struct
1041 *
1042 * Warning: Caller must hold curr_slave_lock for writing.
1043 */
1044 static struct slave *bond_find_best_slave(struct bonding *bond)
1045 {
1046 struct slave *new_active, *old_active;
1047 struct slave *bestslave = NULL;
1048 int mintime = bond->params.updelay;
1049 int i;
1050
1051 new_active = bond->curr_active_slave;
1052
1053 if (!new_active) { /* there were no active slaves left */
1054 if (bond->slave_cnt > 0) /* found one slave */
1055 new_active = bond->first_slave;
1056 else
1057 return NULL; /* still no slave, return NULL */
1058 }
1059
1060 if ((bond->primary_slave) &&
1061 bond->primary_slave->link == BOND_LINK_UP &&
1062 bond_should_change_active(bond)) {
1063 new_active = bond->primary_slave;
1064 }
1065
1066 /* remember where to stop iterating over the slaves */
1067 old_active = new_active;
1068
1069 bond_for_each_slave_from(bond, new_active, i, old_active) {
1070 if (new_active->link == BOND_LINK_UP) {
1071 return new_active;
1072 } else if (new_active->link == BOND_LINK_BACK &&
1073 IS_UP(new_active->dev)) {
1074 /* link up, but waiting for stabilization */
1075 if (new_active->delay < mintime) {
1076 mintime = new_active->delay;
1077 bestslave = new_active;
1078 }
1079 }
1080 }
1081
1082 return bestslave;
1083 }
1084
1085 /**
1086 * change_active_interface - change the active slave into the specified one
1087 * @bond: our bonding struct
1088 * @new: the new slave to make the active one
1089 *
1090 * Set the new slave to the bond's settings and unset them on the old
1091 * curr_active_slave.
1092 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1093 *
1094 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1095 * because it is apparently the best available slave we have, even though its
1096 * updelay hasn't timed out yet.
1097 *
1098 * If new_active is not NULL, caller must hold bond->lock for read and
1099 * curr_slave_lock for write_bh.
1100 */
1101 void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1102 {
1103 struct slave *old_active = bond->curr_active_slave;
1104
1105 if (old_active == new_active)
1106 return;
1107
1108 if (new_active) {
1109 new_active->jiffies = jiffies;
1110
1111 if (new_active->link == BOND_LINK_BACK) {
1112 if (USES_PRIMARY(bond->params.mode)) {
1113 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1114 bond->dev->name, new_active->dev->name,
1115 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1116 }
1117
1118 new_active->delay = 0;
1119 new_active->link = BOND_LINK_UP;
1120
1121 if (bond->params.mode == BOND_MODE_8023AD)
1122 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1123
1124 if (bond_is_lb(bond))
1125 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1126 } else {
1127 if (USES_PRIMARY(bond->params.mode)) {
1128 pr_info("%s: making interface %s the new active one.\n",
1129 bond->dev->name, new_active->dev->name);
1130 }
1131 }
1132 }
1133
1134 if (USES_PRIMARY(bond->params.mode))
1135 bond_mc_swap(bond, new_active, old_active);
1136
1137 if (bond_is_lb(bond)) {
1138 bond_alb_handle_active_change(bond, new_active);
1139 if (old_active)
1140 bond_set_slave_inactive_flags(old_active);
1141 if (new_active)
1142 bond_set_slave_active_flags(new_active);
1143 } else {
1144 bond->curr_active_slave = new_active;
1145 }
1146
1147 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1148 if (old_active)
1149 bond_set_slave_inactive_flags(old_active);
1150
1151 if (new_active) {
1152 bond_set_slave_active_flags(new_active);
1153
1154 if (bond->params.fail_over_mac)
1155 bond_do_fail_over_mac(bond, new_active,
1156 old_active);
1157
1158 write_unlock_bh(&bond->curr_slave_lock);
1159 read_unlock(&bond->lock);
1160
1161 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
1162
1163 read_lock(&bond->lock);
1164 write_lock_bh(&bond->curr_slave_lock);
1165 }
1166 }
1167
1168 /* resend IGMP joins since active slave has changed or
1169 * all were sent on curr_active_slave */
1170 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1171 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1172 netif_running(bond->dev)) {
1173 bond->igmp_retrans = bond->params.resend_igmp;
1174 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
1175 }
1176 }
1177
1178 /**
1179 * bond_select_active_slave - select a new active slave, if needed
1180 * @bond: our bonding struct
1181 *
1182 * This functions should be called when one of the following occurs:
1183 * - The old curr_active_slave has been released or lost its link.
1184 * - The primary_slave has got its link back.
1185 * - A slave has got its link back and there's no old curr_active_slave.
1186 *
1187 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
1188 */
1189 void bond_select_active_slave(struct bonding *bond)
1190 {
1191 struct slave *best_slave;
1192 int rv;
1193
1194 best_slave = bond_find_best_slave(bond);
1195 if (best_slave != bond->curr_active_slave) {
1196 bond_change_active_slave(bond, best_slave);
1197 rv = bond_set_carrier(bond);
1198 if (!rv)
1199 return;
1200
1201 if (netif_carrier_ok(bond->dev)) {
1202 pr_info("%s: first active interface up!\n",
1203 bond->dev->name);
1204 } else {
1205 pr_info("%s: now running without any active interface !\n",
1206 bond->dev->name);
1207 }
1208 }
1209 }
1210
1211 /*--------------------------- slave list handling ---------------------------*/
1212
1213 /*
1214 * This function attaches the slave to the end of list.
1215 *
1216 * bond->lock held for writing by caller.
1217 */
1218 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1219 {
1220 if (bond->first_slave == NULL) { /* attaching the first slave */
1221 new_slave->next = new_slave;
1222 new_slave->prev = new_slave;
1223 bond->first_slave = new_slave;
1224 } else {
1225 new_slave->next = bond->first_slave;
1226 new_slave->prev = bond->first_slave->prev;
1227 new_slave->next->prev = new_slave;
1228 new_slave->prev->next = new_slave;
1229 }
1230
1231 bond->slave_cnt++;
1232 }
1233
1234 /*
1235 * This function detaches the slave from the list.
1236 * WARNING: no check is made to verify if the slave effectively
1237 * belongs to <bond>.
1238 * Nothing is freed on return, structures are just unchained.
1239 * If any slave pointer in bond was pointing to <slave>,
1240 * it should be changed by the calling function.
1241 *
1242 * bond->lock held for writing by caller.
1243 */
1244 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1245 {
1246 if (slave->next)
1247 slave->next->prev = slave->prev;
1248
1249 if (slave->prev)
1250 slave->prev->next = slave->next;
1251
1252 if (bond->first_slave == slave) { /* slave is the first slave */
1253 if (bond->slave_cnt > 1) { /* there are more slave */
1254 bond->first_slave = slave->next;
1255 } else {
1256 bond->first_slave = NULL; /* slave was the last one */
1257 }
1258 }
1259
1260 slave->next = NULL;
1261 slave->prev = NULL;
1262 bond->slave_cnt--;
1263 }
1264
1265 #ifdef CONFIG_NET_POLL_CONTROLLER
1266 static inline int slave_enable_netpoll(struct slave *slave)
1267 {
1268 struct netpoll *np;
1269 int err = 0;
1270
1271 np = kzalloc(sizeof(*np), GFP_KERNEL);
1272 err = -ENOMEM;
1273 if (!np)
1274 goto out;
1275
1276 np->dev = slave->dev;
1277 err = __netpoll_setup(np);
1278 if (err) {
1279 kfree(np);
1280 goto out;
1281 }
1282 slave->np = np;
1283 out:
1284 return err;
1285 }
1286 static inline void slave_disable_netpoll(struct slave *slave)
1287 {
1288 struct netpoll *np = slave->np;
1289
1290 if (!np)
1291 return;
1292
1293 slave->np = NULL;
1294 synchronize_rcu_bh();
1295 __netpoll_cleanup(np);
1296 kfree(np);
1297 }
1298 static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1299 {
1300 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1301 return false;
1302 if (!slave_dev->netdev_ops->ndo_poll_controller)
1303 return false;
1304 return true;
1305 }
1306
1307 static void bond_poll_controller(struct net_device *bond_dev)
1308 {
1309 }
1310
1311 static void __bond_netpoll_cleanup(struct bonding *bond)
1312 {
1313 struct slave *slave;
1314 int i;
1315
1316 bond_for_each_slave(bond, slave, i)
1317 if (IS_UP(slave->dev))
1318 slave_disable_netpoll(slave);
1319 }
1320 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1321 {
1322 struct bonding *bond = netdev_priv(bond_dev);
1323
1324 read_lock(&bond->lock);
1325 __bond_netpoll_cleanup(bond);
1326 read_unlock(&bond->lock);
1327 }
1328
1329 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1330 {
1331 struct bonding *bond = netdev_priv(dev);
1332 struct slave *slave;
1333 int i, err = 0;
1334
1335 read_lock(&bond->lock);
1336 bond_for_each_slave(bond, slave, i) {
1337 err = slave_enable_netpoll(slave);
1338 if (err) {
1339 __bond_netpoll_cleanup(bond);
1340 break;
1341 }
1342 }
1343 read_unlock(&bond->lock);
1344 return err;
1345 }
1346
1347 static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1348 {
1349 return bond->dev->npinfo;
1350 }
1351
1352 #else
1353 static inline int slave_enable_netpoll(struct slave *slave)
1354 {
1355 return 0;
1356 }
1357 static inline void slave_disable_netpoll(struct slave *slave)
1358 {
1359 }
1360 static void bond_netpoll_cleanup(struct net_device *bond_dev)
1361 {
1362 }
1363 #endif
1364
1365 /*---------------------------------- IOCTL ----------------------------------*/
1366
1367 static int bond_sethwaddr(struct net_device *bond_dev,
1368 struct net_device *slave_dev)
1369 {
1370 pr_debug("bond_dev=%p\n", bond_dev);
1371 pr_debug("slave_dev=%p\n", slave_dev);
1372 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1373 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1374 return 0;
1375 }
1376
1377 #define BOND_VLAN_FEATURES \
1378 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1379 NETIF_F_HW_VLAN_FILTER)
1380
1381 /*
1382 * Compute the common dev->feature set available to all slaves. Some
1383 * feature bits are managed elsewhere, so preserve those feature bits
1384 * on the master device.
1385 */
1386 static int bond_compute_features(struct bonding *bond)
1387 {
1388 struct slave *slave;
1389 struct net_device *bond_dev = bond->dev;
1390 u32 features = bond_dev->features;
1391 u32 vlan_features = 0;
1392 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1393 bond_dev->hard_header_len);
1394 int i;
1395
1396 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1397 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_NOCACHE_COPY;
1398
1399 if (!bond->first_slave)
1400 goto done;
1401
1402 features &= ~NETIF_F_ONE_FOR_ALL;
1403
1404 vlan_features = bond->first_slave->dev->vlan_features;
1405 bond_for_each_slave(bond, slave, i) {
1406 features = netdev_increment_features(features,
1407 slave->dev->features,
1408 NETIF_F_ONE_FOR_ALL);
1409 vlan_features = netdev_increment_features(vlan_features,
1410 slave->dev->vlan_features,
1411 NETIF_F_ONE_FOR_ALL);
1412 if (slave->dev->hard_header_len > max_hard_header_len)
1413 max_hard_header_len = slave->dev->hard_header_len;
1414 }
1415
1416 done:
1417 features |= (bond_dev->features & BOND_VLAN_FEATURES);
1418 bond_dev->features = netdev_fix_features(bond_dev, features);
1419 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
1420 bond_dev->hard_header_len = max_hard_header_len;
1421
1422 return 0;
1423 }
1424
1425 static void bond_setup_by_slave(struct net_device *bond_dev,
1426 struct net_device *slave_dev)
1427 {
1428 struct bonding *bond = netdev_priv(bond_dev);
1429
1430 bond_dev->header_ops = slave_dev->header_ops;
1431
1432 bond_dev->type = slave_dev->type;
1433 bond_dev->hard_header_len = slave_dev->hard_header_len;
1434 bond_dev->addr_len = slave_dev->addr_len;
1435
1436 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1437 slave_dev->addr_len);
1438 bond->setup_by_slave = 1;
1439 }
1440
1441 /* On bonding slaves other than the currently active slave, suppress
1442 * duplicates except for alb non-mcast/bcast.
1443 */
1444 static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1445 struct slave *slave,
1446 struct bonding *bond)
1447 {
1448 if (bond_is_slave_inactive(slave)) {
1449 if (bond->params.mode == BOND_MODE_ALB &&
1450 skb->pkt_type != PACKET_BROADCAST &&
1451 skb->pkt_type != PACKET_MULTICAST)
1452 return false;
1453 return true;
1454 }
1455 return false;
1456 }
1457
1458 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1459 {
1460 struct sk_buff *skb = *pskb;
1461 struct slave *slave;
1462 struct bonding *bond;
1463
1464 skb = skb_share_check(skb, GFP_ATOMIC);
1465 if (unlikely(!skb))
1466 return RX_HANDLER_CONSUMED;
1467
1468 *pskb = skb;
1469
1470 slave = bond_slave_get_rcu(skb->dev);
1471 bond = slave->bond;
1472
1473 if (bond->params.arp_interval)
1474 slave->dev->last_rx = jiffies;
1475
1476 if (bond->recv_probe) {
1477 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1478
1479 if (likely(nskb)) {
1480 bond->recv_probe(nskb, bond, slave);
1481 dev_kfree_skb(nskb);
1482 }
1483 }
1484
1485 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1486 return RX_HANDLER_EXACT;
1487 }
1488
1489 skb->dev = bond->dev;
1490
1491 if (bond->params.mode == BOND_MODE_ALB &&
1492 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
1493 skb->pkt_type == PACKET_HOST) {
1494
1495 if (unlikely(skb_cow_head(skb,
1496 skb->data - skb_mac_header(skb)))) {
1497 kfree_skb(skb);
1498 return RX_HANDLER_CONSUMED;
1499 }
1500 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
1501 }
1502
1503 return RX_HANDLER_ANOTHER;
1504 }
1505
1506 /* enslave device <slave> to bond device <master> */
1507 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1508 {
1509 struct bonding *bond = netdev_priv(bond_dev);
1510 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1511 struct slave *new_slave = NULL;
1512 struct netdev_hw_addr *ha;
1513 struct sockaddr addr;
1514 int link_reporting;
1515 int old_features = bond_dev->features;
1516 int res = 0;
1517
1518 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1519 slave_ops->ndo_do_ioctl == NULL) {
1520 pr_warning("%s: Warning: no link monitoring support for %s\n",
1521 bond_dev->name, slave_dev->name);
1522 }
1523
1524 /* bond must be initialized by bond_open() before enslaving */
1525 if (!(bond_dev->flags & IFF_UP)) {
1526 pr_warning("%s: master_dev is not up in bond_enslave\n",
1527 bond_dev->name);
1528 }
1529
1530 /* already enslaved */
1531 if (slave_dev->flags & IFF_SLAVE) {
1532 pr_debug("Error, Device was already enslaved\n");
1533 return -EBUSY;
1534 }
1535
1536 /* vlan challenged mutual exclusion */
1537 /* no need to lock since we're protected by rtnl_lock */
1538 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1539 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1540 if (bond->vlgrp) {
1541 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1542 bond_dev->name, slave_dev->name, bond_dev->name);
1543 return -EPERM;
1544 } else {
1545 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1546 bond_dev->name, slave_dev->name,
1547 slave_dev->name, bond_dev->name);
1548 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1549 }
1550 } else {
1551 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1552 if (bond->slave_cnt == 0) {
1553 /* First slave, and it is not VLAN challenged,
1554 * so remove the block of adding VLANs over the bond.
1555 */
1556 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1557 }
1558 }
1559
1560 /*
1561 * Old ifenslave binaries are no longer supported. These can
1562 * be identified with moderate accuracy by the state of the slave:
1563 * the current ifenslave will set the interface down prior to
1564 * enslaving it; the old ifenslave will not.
1565 */
1566 if ((slave_dev->flags & IFF_UP)) {
1567 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
1568 slave_dev->name);
1569 res = -EPERM;
1570 goto err_undo_flags;
1571 }
1572
1573 /* set bonding device ether type by slave - bonding netdevices are
1574 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1575 * there is a need to override some of the type dependent attribs/funcs.
1576 *
1577 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1578 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1579 */
1580 if (bond->slave_cnt == 0) {
1581 if (bond_dev->type != slave_dev->type) {
1582 pr_debug("%s: change device type from %d to %d\n",
1583 bond_dev->name,
1584 bond_dev->type, slave_dev->type);
1585
1586 res = netdev_bonding_change(bond_dev,
1587 NETDEV_PRE_TYPE_CHANGE);
1588 res = notifier_to_errno(res);
1589 if (res) {
1590 pr_err("%s: refused to change device type\n",
1591 bond_dev->name);
1592 res = -EBUSY;
1593 goto err_undo_flags;
1594 }
1595
1596 /* Flush unicast and multicast addresses */
1597 dev_uc_flush(bond_dev);
1598 dev_mc_flush(bond_dev);
1599
1600 if (slave_dev->type != ARPHRD_ETHER)
1601 bond_setup_by_slave(bond_dev, slave_dev);
1602 else
1603 ether_setup(bond_dev);
1604
1605 netdev_bonding_change(bond_dev,
1606 NETDEV_POST_TYPE_CHANGE);
1607 }
1608 } else if (bond_dev->type != slave_dev->type) {
1609 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1610 slave_dev->name,
1611 slave_dev->type, bond_dev->type);
1612 res = -EINVAL;
1613 goto err_undo_flags;
1614 }
1615
1616 if (slave_ops->ndo_set_mac_address == NULL) {
1617 if (bond->slave_cnt == 0) {
1618 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1619 bond_dev->name);
1620 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1621 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1622 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1623 bond_dev->name);
1624 res = -EOPNOTSUPP;
1625 goto err_undo_flags;
1626 }
1627 }
1628
1629 /* If this is the first slave, then we need to set the master's hardware
1630 * address to be the same as the slave's. */
1631 if (is_zero_ether_addr(bond->dev->dev_addr))
1632 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1633 slave_dev->addr_len);
1634
1635
1636 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
1637 if (!new_slave) {
1638 res = -ENOMEM;
1639 goto err_undo_flags;
1640 }
1641
1642 /*
1643 * Set the new_slave's queue_id to be zero. Queue ID mapping
1644 * is set via sysfs or module option if desired.
1645 */
1646 new_slave->queue_id = 0;
1647
1648 /* Save slave's original mtu and then set it to match the bond */
1649 new_slave->original_mtu = slave_dev->mtu;
1650 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1651 if (res) {
1652 pr_debug("Error %d calling dev_set_mtu\n", res);
1653 goto err_free;
1654 }
1655
1656 /*
1657 * Save slave's original ("permanent") mac address for modes
1658 * that need it, and for restoring it upon release, and then
1659 * set it to the master's address
1660 */
1661 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1662
1663 if (!bond->params.fail_over_mac) {
1664 /*
1665 * Set slave to master's mac address. The application already
1666 * set the master's mac address to that of the first slave
1667 */
1668 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1669 addr.sa_family = slave_dev->type;
1670 res = dev_set_mac_address(slave_dev, &addr);
1671 if (res) {
1672 pr_debug("Error %d calling set_mac_address\n", res);
1673 goto err_restore_mtu;
1674 }
1675 }
1676
1677 res = netdev_set_bond_master(slave_dev, bond_dev);
1678 if (res) {
1679 pr_debug("Error %d calling netdev_set_bond_master\n", res);
1680 goto err_restore_mac;
1681 }
1682
1683 /* open the slave since the application closed it */
1684 res = dev_open(slave_dev);
1685 if (res) {
1686 pr_debug("Opening slave %s failed\n", slave_dev->name);
1687 goto err_unset_master;
1688 }
1689
1690 new_slave->bond = bond;
1691 new_slave->dev = slave_dev;
1692 slave_dev->priv_flags |= IFF_BONDING;
1693
1694 if (bond_is_lb(bond)) {
1695 /* bond_alb_init_slave() must be called before all other stages since
1696 * it might fail and we do not want to have to undo everything
1697 */
1698 res = bond_alb_init_slave(bond, new_slave);
1699 if (res)
1700 goto err_close;
1701 }
1702
1703 /* If the mode USES_PRIMARY, then the new slave gets the
1704 * master's promisc (and mc) settings only if it becomes the
1705 * curr_active_slave, and that is taken care of later when calling
1706 * bond_change_active()
1707 */
1708 if (!USES_PRIMARY(bond->params.mode)) {
1709 /* set promiscuity level to new slave */
1710 if (bond_dev->flags & IFF_PROMISC) {
1711 res = dev_set_promiscuity(slave_dev, 1);
1712 if (res)
1713 goto err_close;
1714 }
1715
1716 /* set allmulti level to new slave */
1717 if (bond_dev->flags & IFF_ALLMULTI) {
1718 res = dev_set_allmulti(slave_dev, 1);
1719 if (res)
1720 goto err_close;
1721 }
1722
1723 netif_addr_lock_bh(bond_dev);
1724 /* upload master's mc_list to new slave */
1725 netdev_for_each_mc_addr(ha, bond_dev)
1726 dev_mc_add(slave_dev, ha->addr);
1727 netif_addr_unlock_bh(bond_dev);
1728 }
1729
1730 if (bond->params.mode == BOND_MODE_8023AD) {
1731 /* add lacpdu mc addr to mc list */
1732 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1733
1734 dev_mc_add(slave_dev, lacpdu_multicast);
1735 }
1736
1737 bond_add_vlans_on_slave(bond, slave_dev);
1738
1739 write_lock_bh(&bond->lock);
1740
1741 bond_attach_slave(bond, new_slave);
1742
1743 new_slave->delay = 0;
1744 new_slave->link_failure_count = 0;
1745
1746 bond_compute_features(bond);
1747
1748 write_unlock_bh(&bond->lock);
1749
1750 read_lock(&bond->lock);
1751
1752 new_slave->last_arp_rx = jiffies;
1753
1754 if (bond->params.miimon && !bond->params.use_carrier) {
1755 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1756
1757 if ((link_reporting == -1) && !bond->params.arp_interval) {
1758 /*
1759 * miimon is set but a bonded network driver
1760 * does not support ETHTOOL/MII and
1761 * arp_interval is not set. Note: if
1762 * use_carrier is enabled, we will never go
1763 * here (because netif_carrier is always
1764 * supported); thus, we don't need to change
1765 * the messages for netif_carrier.
1766 */
1767 pr_warning("%s: Warning: MII and ETHTOOL support not available for interface %s, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details.\n",
1768 bond_dev->name, slave_dev->name);
1769 } else if (link_reporting == -1) {
1770 /* unable get link status using mii/ethtool */
1771 pr_warning("%s: Warning: can't get link status from interface %s; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface.\n",
1772 bond_dev->name, slave_dev->name);
1773 }
1774 }
1775
1776 /* check for initial state */
1777 if (!bond->params.miimon ||
1778 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1779 if (bond->params.updelay) {
1780 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
1781 new_slave->link = BOND_LINK_BACK;
1782 new_slave->delay = bond->params.updelay;
1783 } else {
1784 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
1785 new_slave->link = BOND_LINK_UP;
1786 }
1787 new_slave->jiffies = jiffies;
1788 } else {
1789 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
1790 new_slave->link = BOND_LINK_DOWN;
1791 }
1792
1793 if (bond_update_speed_duplex(new_slave) &&
1794 (new_slave->link != BOND_LINK_DOWN)) {
1795 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1796 bond_dev->name, new_slave->dev->name);
1797
1798 if (bond->params.mode == BOND_MODE_8023AD) {
1799 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1800 bond_dev->name);
1801 }
1802 }
1803
1804 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1805 /* if there is a primary slave, remember it */
1806 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1807 bond->primary_slave = new_slave;
1808 bond->force_primary = true;
1809 }
1810 }
1811
1812 write_lock_bh(&bond->curr_slave_lock);
1813
1814 switch (bond->params.mode) {
1815 case BOND_MODE_ACTIVEBACKUP:
1816 bond_set_slave_inactive_flags(new_slave);
1817 bond_select_active_slave(bond);
1818 break;
1819 case BOND_MODE_8023AD:
1820 /* in 802.3ad mode, the internal mechanism
1821 * will activate the slaves in the selected
1822 * aggregator
1823 */
1824 bond_set_slave_inactive_flags(new_slave);
1825 /* if this is the first slave */
1826 if (bond->slave_cnt == 1) {
1827 SLAVE_AD_INFO(new_slave).id = 1;
1828 /* Initialize AD with the number of times that the AD timer is called in 1 second
1829 * can be called only after the mac address of the bond is set
1830 */
1831 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1832 bond->params.lacp_fast);
1833 } else {
1834 SLAVE_AD_INFO(new_slave).id =
1835 SLAVE_AD_INFO(new_slave->prev).id + 1;
1836 }
1837
1838 bond_3ad_bind_slave(new_slave);
1839 break;
1840 case BOND_MODE_TLB:
1841 case BOND_MODE_ALB:
1842 bond_set_active_slave(new_slave);
1843 bond_set_slave_inactive_flags(new_slave);
1844 bond_select_active_slave(bond);
1845 break;
1846 default:
1847 pr_debug("This slave is always active in trunk mode\n");
1848
1849 /* always active in trunk mode */
1850 bond_set_active_slave(new_slave);
1851
1852 /* In trunking mode there is little meaning to curr_active_slave
1853 * anyway (it holds no special properties of the bond device),
1854 * so we can change it without calling change_active_interface()
1855 */
1856 if (!bond->curr_active_slave)
1857 bond->curr_active_slave = new_slave;
1858
1859 break;
1860 } /* switch(bond_mode) */
1861
1862 write_unlock_bh(&bond->curr_slave_lock);
1863
1864 bond_set_carrier(bond);
1865
1866 #ifdef CONFIG_NET_POLL_CONTROLLER
1867 slave_dev->npinfo = bond_netpoll_info(bond);
1868 if (slave_dev->npinfo) {
1869 if (slave_enable_netpoll(new_slave)) {
1870 read_unlock(&bond->lock);
1871 pr_info("Error, %s: master_dev is using netpoll, "
1872 "but new slave device does not support netpoll.\n",
1873 bond_dev->name);
1874 res = -EBUSY;
1875 goto err_close;
1876 }
1877 }
1878 #endif
1879
1880 read_unlock(&bond->lock);
1881
1882 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1883 if (res)
1884 goto err_close;
1885
1886 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1887 new_slave);
1888 if (res) {
1889 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1890 goto err_dest_symlinks;
1891 }
1892
1893 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1894 bond_dev->name, slave_dev->name,
1895 bond_is_active_slave(new_slave) ? "n active" : " backup",
1896 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1897
1898 /* enslave is successful */
1899 return 0;
1900
1901 /* Undo stages on error */
1902 err_dest_symlinks:
1903 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1904
1905 err_close:
1906 dev_close(slave_dev);
1907
1908 err_unset_master:
1909 netdev_set_bond_master(slave_dev, NULL);
1910
1911 err_restore_mac:
1912 if (!bond->params.fail_over_mac) {
1913 /* XXX TODO - fom follow mode needs to change master's
1914 * MAC if this slave's MAC is in use by the bond, or at
1915 * least print a warning.
1916 */
1917 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1918 addr.sa_family = slave_dev->type;
1919 dev_set_mac_address(slave_dev, &addr);
1920 }
1921
1922 err_restore_mtu:
1923 dev_set_mtu(slave_dev, new_slave->original_mtu);
1924
1925 err_free:
1926 kfree(new_slave);
1927
1928 err_undo_flags:
1929 bond_dev->features = old_features;
1930
1931 return res;
1932 }
1933
1934 /*
1935 * Try to release the slave device <slave> from the bond device <master>
1936 * It is legal to access curr_active_slave without a lock because all the function
1937 * is write-locked.
1938 *
1939 * The rules for slave state should be:
1940 * for Active/Backup:
1941 * Active stays on all backups go down
1942 * for Bonded connections:
1943 * The first up interface should be left on and all others downed.
1944 */
1945 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1946 {
1947 struct bonding *bond = netdev_priv(bond_dev);
1948 struct slave *slave, *oldcurrent;
1949 struct sockaddr addr;
1950
1951 /* slave is not a slave or master is not master of this slave */
1952 if (!(slave_dev->flags & IFF_SLAVE) ||
1953 (slave_dev->master != bond_dev)) {
1954 pr_err("%s: Error: cannot release %s.\n",
1955 bond_dev->name, slave_dev->name);
1956 return -EINVAL;
1957 }
1958
1959 block_netpoll_tx();
1960 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
1961 write_lock_bh(&bond->lock);
1962
1963 slave = bond_get_slave_by_dev(bond, slave_dev);
1964 if (!slave) {
1965 /* not a slave of this bond */
1966 pr_info("%s: %s not enslaved\n",
1967 bond_dev->name, slave_dev->name);
1968 write_unlock_bh(&bond->lock);
1969 unblock_netpoll_tx();
1970 return -EINVAL;
1971 }
1972
1973 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1974 * for this slave anymore.
1975 */
1976 netdev_rx_handler_unregister(slave_dev);
1977 write_unlock_bh(&bond->lock);
1978 synchronize_net();
1979 write_lock_bh(&bond->lock);
1980
1981 if (!bond->params.fail_over_mac) {
1982 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1983 bond->slave_cnt > 1)
1984 pr_warning("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n",
1985 bond_dev->name, slave_dev->name,
1986 slave->perm_hwaddr,
1987 bond_dev->name, slave_dev->name);
1988 }
1989
1990 /* Inform AD package of unbinding of slave. */
1991 if (bond->params.mode == BOND_MODE_8023AD) {
1992 /* must be called before the slave is
1993 * detached from the list
1994 */
1995 bond_3ad_unbind_slave(slave);
1996 }
1997
1998 pr_info("%s: releasing %s interface %s\n",
1999 bond_dev->name,
2000 bond_is_active_slave(slave) ? "active" : "backup",
2001 slave_dev->name);
2002
2003 oldcurrent = bond->curr_active_slave;
2004
2005 bond->current_arp_slave = NULL;
2006
2007 /* release the slave from its bond */
2008 bond_detach_slave(bond, slave);
2009
2010 bond_compute_features(bond);
2011
2012 if (bond->primary_slave == slave)
2013 bond->primary_slave = NULL;
2014
2015 if (oldcurrent == slave)
2016 bond_change_active_slave(bond, NULL);
2017
2018 if (bond_is_lb(bond)) {
2019 /* Must be called only after the slave has been
2020 * detached from the list and the curr_active_slave
2021 * has been cleared (if our_slave == old_current),
2022 * but before a new active slave is selected.
2023 */
2024 write_unlock_bh(&bond->lock);
2025 bond_alb_deinit_slave(bond, slave);
2026 write_lock_bh(&bond->lock);
2027 }
2028
2029 if (oldcurrent == slave) {
2030 /*
2031 * Note that we hold RTNL over this sequence, so there
2032 * is no concern that another slave add/remove event
2033 * will interfere.
2034 */
2035 write_unlock_bh(&bond->lock);
2036 read_lock(&bond->lock);
2037 write_lock_bh(&bond->curr_slave_lock);
2038
2039 bond_select_active_slave(bond);
2040
2041 write_unlock_bh(&bond->curr_slave_lock);
2042 read_unlock(&bond->lock);
2043 write_lock_bh(&bond->lock);
2044 }
2045
2046 if (bond->slave_cnt == 0) {
2047 bond_set_carrier(bond);
2048
2049 /* if the last slave was removed, zero the mac address
2050 * of the master so it will be set by the application
2051 * to the mac address of the first slave
2052 */
2053 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2054
2055 if (!bond->vlgrp) {
2056 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2057 } else {
2058 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2059 bond_dev->name, bond_dev->name);
2060 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2061 bond_dev->name);
2062 }
2063 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2064 !bond_has_challenged_slaves(bond)) {
2065 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2066 bond_dev->name, slave_dev->name, bond_dev->name);
2067 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2068 }
2069
2070 write_unlock_bh(&bond->lock);
2071 unblock_netpoll_tx();
2072
2073 /* must do this from outside any spinlocks */
2074 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2075
2076 bond_del_vlans_from_slave(bond, slave_dev);
2077
2078 /* If the mode USES_PRIMARY, then we should only remove its
2079 * promisc and mc settings if it was the curr_active_slave, but that was
2080 * already taken care of above when we detached the slave
2081 */
2082 if (!USES_PRIMARY(bond->params.mode)) {
2083 /* unset promiscuity level from slave */
2084 if (bond_dev->flags & IFF_PROMISC)
2085 dev_set_promiscuity(slave_dev, -1);
2086
2087 /* unset allmulti level from slave */
2088 if (bond_dev->flags & IFF_ALLMULTI)
2089 dev_set_allmulti(slave_dev, -1);
2090
2091 /* flush master's mc_list from slave */
2092 netif_addr_lock_bh(bond_dev);
2093 bond_mc_list_flush(bond_dev, slave_dev);
2094 netif_addr_unlock_bh(bond_dev);
2095 }
2096
2097 netdev_set_bond_master(slave_dev, NULL);
2098
2099 slave_disable_netpoll(slave);
2100
2101 /* close slave before restoring its mac address */
2102 dev_close(slave_dev);
2103
2104 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
2105 /* restore original ("permanent") mac address */
2106 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2107 addr.sa_family = slave_dev->type;
2108 dev_set_mac_address(slave_dev, &addr);
2109 }
2110
2111 dev_set_mtu(slave_dev, slave->original_mtu);
2112
2113 slave_dev->priv_flags &= ~IFF_BONDING;
2114
2115 kfree(slave);
2116
2117 return 0; /* deletion OK */
2118 }
2119
2120 /*
2121 * First release a slave and then destroy the bond if no more slaves are left.
2122 * Must be under rtnl_lock when this function is called.
2123 */
2124 static int bond_release_and_destroy(struct net_device *bond_dev,
2125 struct net_device *slave_dev)
2126 {
2127 struct bonding *bond = netdev_priv(bond_dev);
2128 int ret;
2129
2130 ret = bond_release(bond_dev, slave_dev);
2131 if ((ret == 0) && (bond->slave_cnt == 0)) {
2132 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2133 pr_info("%s: destroying bond %s.\n",
2134 bond_dev->name, bond_dev->name);
2135 unregister_netdevice(bond_dev);
2136 }
2137 return ret;
2138 }
2139
2140 /*
2141 * This function releases all slaves.
2142 */
2143 static int bond_release_all(struct net_device *bond_dev)
2144 {
2145 struct bonding *bond = netdev_priv(bond_dev);
2146 struct slave *slave;
2147 struct net_device *slave_dev;
2148 struct sockaddr addr;
2149
2150 write_lock_bh(&bond->lock);
2151
2152 netif_carrier_off(bond_dev);
2153
2154 if (bond->slave_cnt == 0)
2155 goto out;
2156
2157 bond->current_arp_slave = NULL;
2158 bond->primary_slave = NULL;
2159 bond_change_active_slave(bond, NULL);
2160
2161 while ((slave = bond->first_slave) != NULL) {
2162 /* Inform AD package of unbinding of slave
2163 * before slave is detached from the list.
2164 */
2165 if (bond->params.mode == BOND_MODE_8023AD)
2166 bond_3ad_unbind_slave(slave);
2167
2168 slave_dev = slave->dev;
2169 bond_detach_slave(bond, slave);
2170
2171 /* now that the slave is detached, unlock and perform
2172 * all the undo steps that should not be called from
2173 * within a lock.
2174 */
2175 write_unlock_bh(&bond->lock);
2176
2177 /* unregister rx_handler early so bond_handle_frame wouldn't
2178 * be called for this slave anymore.
2179 */
2180 netdev_rx_handler_unregister(slave_dev);
2181 synchronize_net();
2182
2183 if (bond_is_lb(bond)) {
2184 /* must be called only after the slave
2185 * has been detached from the list
2186 */
2187 bond_alb_deinit_slave(bond, slave);
2188 }
2189
2190 bond_compute_features(bond);
2191
2192 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2193 bond_del_vlans_from_slave(bond, slave_dev);
2194
2195 /* If the mode USES_PRIMARY, then we should only remove its
2196 * promisc and mc settings if it was the curr_active_slave, but that was
2197 * already taken care of above when we detached the slave
2198 */
2199 if (!USES_PRIMARY(bond->params.mode)) {
2200 /* unset promiscuity level from slave */
2201 if (bond_dev->flags & IFF_PROMISC)
2202 dev_set_promiscuity(slave_dev, -1);
2203
2204 /* unset allmulti level from slave */
2205 if (bond_dev->flags & IFF_ALLMULTI)
2206 dev_set_allmulti(slave_dev, -1);
2207
2208 /* flush master's mc_list from slave */
2209 netif_addr_lock_bh(bond_dev);
2210 bond_mc_list_flush(bond_dev, slave_dev);
2211 netif_addr_unlock_bh(bond_dev);
2212 }
2213
2214 netdev_set_bond_master(slave_dev, NULL);
2215
2216 slave_disable_netpoll(slave);
2217
2218 /* close slave before restoring its mac address */
2219 dev_close(slave_dev);
2220
2221 if (!bond->params.fail_over_mac) {
2222 /* restore original ("permanent") mac address*/
2223 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2224 addr.sa_family = slave_dev->type;
2225 dev_set_mac_address(slave_dev, &addr);
2226 }
2227
2228 kfree(slave);
2229
2230 /* re-acquire the lock before getting the next slave */
2231 write_lock_bh(&bond->lock);
2232 }
2233
2234 /* zero the mac address of the master so it will be
2235 * set by the application to the mac address of the
2236 * first slave
2237 */
2238 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2239
2240 if (!bond->vlgrp) {
2241 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2242 } else {
2243 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2244 bond_dev->name, bond_dev->name);
2245 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2246 bond_dev->name);
2247 }
2248
2249 pr_info("%s: released all slaves\n", bond_dev->name);
2250
2251 out:
2252 write_unlock_bh(&bond->lock);
2253 return 0;
2254 }
2255
2256 /*
2257 * This function changes the active slave to slave <slave_dev>.
2258 * It returns -EINVAL in the following cases.
2259 * - <slave_dev> is not found in the list.
2260 * - There is not active slave now.
2261 * - <slave_dev> is already active.
2262 * - The link state of <slave_dev> is not BOND_LINK_UP.
2263 * - <slave_dev> is not running.
2264 * In these cases, this function does nothing.
2265 * In the other cases, current_slave pointer is changed and 0 is returned.
2266 */
2267 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2268 {
2269 struct bonding *bond = netdev_priv(bond_dev);
2270 struct slave *old_active = NULL;
2271 struct slave *new_active = NULL;
2272 int res = 0;
2273
2274 if (!USES_PRIMARY(bond->params.mode))
2275 return -EINVAL;
2276
2277 /* Verify that master_dev is indeed the master of slave_dev */
2278 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
2279 return -EINVAL;
2280
2281 read_lock(&bond->lock);
2282
2283 read_lock(&bond->curr_slave_lock);
2284 old_active = bond->curr_active_slave;
2285 read_unlock(&bond->curr_slave_lock);
2286
2287 new_active = bond_get_slave_by_dev(bond, slave_dev);
2288
2289 /*
2290 * Changing to the current active: do nothing; return success.
2291 */
2292 if (new_active && (new_active == old_active)) {
2293 read_unlock(&bond->lock);
2294 return 0;
2295 }
2296
2297 if ((new_active) &&
2298 (old_active) &&
2299 (new_active->link == BOND_LINK_UP) &&
2300 IS_UP(new_active->dev)) {
2301 block_netpoll_tx();
2302 write_lock_bh(&bond->curr_slave_lock);
2303 bond_change_active_slave(bond, new_active);
2304 write_unlock_bh(&bond->curr_slave_lock);
2305 unblock_netpoll_tx();
2306 } else
2307 res = -EINVAL;
2308
2309 read_unlock(&bond->lock);
2310
2311 return res;
2312 }
2313
2314 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2315 {
2316 struct bonding *bond = netdev_priv(bond_dev);
2317
2318 info->bond_mode = bond->params.mode;
2319 info->miimon = bond->params.miimon;
2320
2321 read_lock(&bond->lock);
2322 info->num_slaves = bond->slave_cnt;
2323 read_unlock(&bond->lock);
2324
2325 return 0;
2326 }
2327
2328 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2329 {
2330 struct bonding *bond = netdev_priv(bond_dev);
2331 struct slave *slave;
2332 int i, res = -ENODEV;
2333
2334 read_lock(&bond->lock);
2335
2336 bond_for_each_slave(bond, slave, i) {
2337 if (i == (int)info->slave_id) {
2338 res = 0;
2339 strcpy(info->slave_name, slave->dev->name);
2340 info->link = slave->link;
2341 info->state = bond_slave_state(slave);
2342 info->link_failure_count = slave->link_failure_count;
2343 break;
2344 }
2345 }
2346
2347 read_unlock(&bond->lock);
2348
2349 return res;
2350 }
2351
2352 /*-------------------------------- Monitoring -------------------------------*/
2353
2354
2355 static int bond_miimon_inspect(struct bonding *bond)
2356 {
2357 struct slave *slave;
2358 int i, link_state, commit = 0;
2359 bool ignore_updelay;
2360
2361 ignore_updelay = !bond->curr_active_slave ? true : false;
2362
2363 bond_for_each_slave(bond, slave, i) {
2364 slave->new_link = BOND_LINK_NOCHANGE;
2365
2366 link_state = bond_check_dev_link(bond, slave->dev, 0);
2367
2368 switch (slave->link) {
2369 case BOND_LINK_UP:
2370 if (link_state)
2371 continue;
2372
2373 slave->link = BOND_LINK_FAIL;
2374 slave->delay = bond->params.downdelay;
2375 if (slave->delay) {
2376 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2377 bond->dev->name,
2378 (bond->params.mode ==
2379 BOND_MODE_ACTIVEBACKUP) ?
2380 (bond_is_active_slave(slave) ?
2381 "active " : "backup ") : "",
2382 slave->dev->name,
2383 bond->params.downdelay * bond->params.miimon);
2384 }
2385 /*FALLTHRU*/
2386 case BOND_LINK_FAIL:
2387 if (link_state) {
2388 /*
2389 * recovered before downdelay expired
2390 */
2391 slave->link = BOND_LINK_UP;
2392 slave->jiffies = jiffies;
2393 pr_info("%s: link status up again after %d ms for interface %s.\n",
2394 bond->dev->name,
2395 (bond->params.downdelay - slave->delay) *
2396 bond->params.miimon,
2397 slave->dev->name);
2398 continue;
2399 }
2400
2401 if (slave->delay <= 0) {
2402 slave->new_link = BOND_LINK_DOWN;
2403 commit++;
2404 continue;
2405 }
2406
2407 slave->delay--;
2408 break;
2409
2410 case BOND_LINK_DOWN:
2411 if (!link_state)
2412 continue;
2413
2414 slave->link = BOND_LINK_BACK;
2415 slave->delay = bond->params.updelay;
2416
2417 if (slave->delay) {
2418 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2419 bond->dev->name, slave->dev->name,
2420 ignore_updelay ? 0 :
2421 bond->params.updelay *
2422 bond->params.miimon);
2423 }
2424 /*FALLTHRU*/
2425 case BOND_LINK_BACK:
2426 if (!link_state) {
2427 slave->link = BOND_LINK_DOWN;
2428 pr_info("%s: link status down again after %d ms for interface %s.\n",
2429 bond->dev->name,
2430 (bond->params.updelay - slave->delay) *
2431 bond->params.miimon,
2432 slave->dev->name);
2433
2434 continue;
2435 }
2436
2437 if (ignore_updelay)
2438 slave->delay = 0;
2439
2440 if (slave->delay <= 0) {
2441 slave->new_link = BOND_LINK_UP;
2442 commit++;
2443 ignore_updelay = false;
2444 continue;
2445 }
2446
2447 slave->delay--;
2448 break;
2449 }
2450 }
2451
2452 return commit;
2453 }
2454
2455 static void bond_miimon_commit(struct bonding *bond)
2456 {
2457 struct slave *slave;
2458 int i;
2459
2460 bond_for_each_slave(bond, slave, i) {
2461 switch (slave->new_link) {
2462 case BOND_LINK_NOCHANGE:
2463 continue;
2464
2465 case BOND_LINK_UP:
2466 slave->link = BOND_LINK_UP;
2467 slave->jiffies = jiffies;
2468
2469 if (bond->params.mode == BOND_MODE_8023AD) {
2470 /* prevent it from being the active one */
2471 bond_set_backup_slave(slave);
2472 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2473 /* make it immediately active */
2474 bond_set_active_slave(slave);
2475 } else if (slave != bond->primary_slave) {
2476 /* prevent it from being the active one */
2477 bond_set_backup_slave(slave);
2478 }
2479
2480 bond_update_speed_duplex(slave);
2481
2482 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
2483 bond->dev->name, slave->dev->name,
2484 slave->speed, slave->duplex ? "full" : "half");
2485
2486 /* notify ad that the link status has changed */
2487 if (bond->params.mode == BOND_MODE_8023AD)
2488 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2489
2490 if (bond_is_lb(bond))
2491 bond_alb_handle_link_change(bond, slave,
2492 BOND_LINK_UP);
2493
2494 if (!bond->curr_active_slave ||
2495 (slave == bond->primary_slave))
2496 goto do_failover;
2497
2498 continue;
2499
2500 case BOND_LINK_DOWN:
2501 if (slave->link_failure_count < UINT_MAX)
2502 slave->link_failure_count++;
2503
2504 slave->link = BOND_LINK_DOWN;
2505
2506 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2507 bond->params.mode == BOND_MODE_8023AD)
2508 bond_set_slave_inactive_flags(slave);
2509
2510 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2511 bond->dev->name, slave->dev->name);
2512
2513 if (bond->params.mode == BOND_MODE_8023AD)
2514 bond_3ad_handle_link_change(slave,
2515 BOND_LINK_DOWN);
2516
2517 if (bond_is_lb(bond))
2518 bond_alb_handle_link_change(bond, slave,
2519 BOND_LINK_DOWN);
2520
2521 if (slave == bond->curr_active_slave)
2522 goto do_failover;
2523
2524 continue;
2525
2526 default:
2527 pr_err("%s: invalid new link %d on slave %s\n",
2528 bond->dev->name, slave->new_link,
2529 slave->dev->name);
2530 slave->new_link = BOND_LINK_NOCHANGE;
2531
2532 continue;
2533 }
2534
2535 do_failover:
2536 ASSERT_RTNL();
2537 block_netpoll_tx();
2538 write_lock_bh(&bond->curr_slave_lock);
2539 bond_select_active_slave(bond);
2540 write_unlock_bh(&bond->curr_slave_lock);
2541 unblock_netpoll_tx();
2542 }
2543
2544 bond_set_carrier(bond);
2545 }
2546
2547 /*
2548 * bond_mii_monitor
2549 *
2550 * Really a wrapper that splits the mii monitor into two phases: an
2551 * inspection, then (if inspection indicates something needs to be done)
2552 * an acquisition of appropriate locks followed by a commit phase to
2553 * implement whatever link state changes are indicated.
2554 */
2555 void bond_mii_monitor(struct work_struct *work)
2556 {
2557 struct bonding *bond = container_of(work, struct bonding,
2558 mii_work.work);
2559
2560 read_lock(&bond->lock);
2561 if (bond->kill_timers)
2562 goto out;
2563
2564 if (bond->slave_cnt == 0)
2565 goto re_arm;
2566
2567 if (bond_miimon_inspect(bond)) {
2568 read_unlock(&bond->lock);
2569 rtnl_lock();
2570 read_lock(&bond->lock);
2571
2572 bond_miimon_commit(bond);
2573
2574 read_unlock(&bond->lock);
2575 rtnl_unlock(); /* might sleep, hold no other locks */
2576 read_lock(&bond->lock);
2577 }
2578
2579 re_arm:
2580 if (bond->params.miimon)
2581 queue_delayed_work(bond->wq, &bond->mii_work,
2582 msecs_to_jiffies(bond->params.miimon));
2583 out:
2584 read_unlock(&bond->lock);
2585 }
2586
2587 static __be32 bond_glean_dev_ip(struct net_device *dev)
2588 {
2589 struct in_device *idev;
2590 struct in_ifaddr *ifa;
2591 __be32 addr = 0;
2592
2593 if (!dev)
2594 return 0;
2595
2596 rcu_read_lock();
2597 idev = __in_dev_get_rcu(dev);
2598 if (!idev)
2599 goto out;
2600
2601 ifa = idev->ifa_list;
2602 if (!ifa)
2603 goto out;
2604
2605 addr = ifa->ifa_local;
2606 out:
2607 rcu_read_unlock();
2608 return addr;
2609 }
2610
2611 static int bond_has_this_ip(struct bonding *bond, __be32 ip)
2612 {
2613 struct vlan_entry *vlan;
2614
2615 if (ip == bond->master_ip)
2616 return 1;
2617
2618 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2619 if (ip == vlan->vlan_ip)
2620 return 1;
2621 }
2622
2623 return 0;
2624 }
2625
2626 /*
2627 * We go to the (large) trouble of VLAN tagging ARP frames because
2628 * switches in VLAN mode (especially if ports are configured as
2629 * "native" to a VLAN) might not pass non-tagged frames.
2630 */
2631 static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
2632 {
2633 struct sk_buff *skb;
2634
2635 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2636 slave_dev->name, dest_ip, src_ip, vlan_id);
2637
2638 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2639 NULL, slave_dev->dev_addr, NULL);
2640
2641 if (!skb) {
2642 pr_err("ARP packet allocation failed\n");
2643 return;
2644 }
2645 if (vlan_id) {
2646 skb = vlan_put_tag(skb, vlan_id);
2647 if (!skb) {
2648 pr_err("failed to insert VLAN tag\n");
2649 return;
2650 }
2651 }
2652 arp_xmit(skb);
2653 }
2654
2655
2656 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2657 {
2658 int i, vlan_id;
2659 __be32 *targets = bond->params.arp_targets;
2660 struct vlan_entry *vlan;
2661 struct net_device *vlan_dev;
2662 struct rtable *rt;
2663
2664 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2665 if (!targets[i])
2666 break;
2667 pr_debug("basa: target %x\n", targets[i]);
2668 if (!bond->vlgrp) {
2669 pr_debug("basa: empty vlan: arp_send\n");
2670 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2671 bond->master_ip, 0);
2672 continue;
2673 }
2674
2675 /*
2676 * If VLANs are configured, we do a route lookup to
2677 * determine which VLAN interface would be used, so we
2678 * can tag the ARP with the proper VLAN tag.
2679 */
2680 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2681 RTO_ONLINK, 0);
2682 if (IS_ERR(rt)) {
2683 if (net_ratelimit()) {
2684 pr_warning("%s: no route to arp_ip_target %pI4\n",
2685 bond->dev->name, &targets[i]);
2686 }
2687 continue;
2688 }
2689
2690 /*
2691 * This target is not on a VLAN
2692 */
2693 if (rt->dst.dev == bond->dev) {
2694 ip_rt_put(rt);
2695 pr_debug("basa: rtdev == bond->dev: arp_send\n");
2696 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2697 bond->master_ip, 0);
2698 continue;
2699 }
2700
2701 vlan_id = 0;
2702 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2703 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
2704 if (vlan_dev == rt->dst.dev) {
2705 vlan_id = vlan->vlan_id;
2706 pr_debug("basa: vlan match on %s %d\n",
2707 vlan_dev->name, vlan_id);
2708 break;
2709 }
2710 }
2711
2712 if (vlan_id) {
2713 ip_rt_put(rt);
2714 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2715 vlan->vlan_ip, vlan_id);
2716 continue;
2717 }
2718
2719 if (net_ratelimit()) {
2720 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2721 bond->dev->name, &targets[i],
2722 rt->dst.dev ? rt->dst.dev->name : "NULL");
2723 }
2724 ip_rt_put(rt);
2725 }
2726 }
2727
2728 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
2729 {
2730 int i;
2731 __be32 *targets = bond->params.arp_targets;
2732
2733 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2734 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
2735 &sip, &tip, i, &targets[i],
2736 bond_has_this_ip(bond, tip));
2737 if (sip == targets[i]) {
2738 if (bond_has_this_ip(bond, tip))
2739 slave->last_arp_rx = jiffies;
2740 return;
2741 }
2742 }
2743 }
2744
2745 static void bond_arp_rcv(struct sk_buff *skb, struct bonding *bond,
2746 struct slave *slave)
2747 {
2748 struct arphdr *arp;
2749 unsigned char *arp_ptr;
2750 __be32 sip, tip;
2751
2752 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2753 return;
2754
2755 read_lock(&bond->lock);
2756
2757 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2758 bond->dev->name, skb->dev->name);
2759
2760 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
2761 goto out_unlock;
2762
2763 arp = arp_hdr(skb);
2764 if (arp->ar_hln != bond->dev->addr_len ||
2765 skb->pkt_type == PACKET_OTHERHOST ||
2766 skb->pkt_type == PACKET_LOOPBACK ||
2767 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2768 arp->ar_pro != htons(ETH_P_IP) ||
2769 arp->ar_pln != 4)
2770 goto out_unlock;
2771
2772 arp_ptr = (unsigned char *)(arp + 1);
2773 arp_ptr += bond->dev->addr_len;
2774 memcpy(&sip, arp_ptr, 4);
2775 arp_ptr += 4 + bond->dev->addr_len;
2776 memcpy(&tip, arp_ptr, 4);
2777
2778 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2779 bond->dev->name, slave->dev->name, bond_slave_state(slave),
2780 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2781 &sip, &tip);
2782
2783 /*
2784 * Backup slaves won't see the ARP reply, but do come through
2785 * here for each ARP probe (so we swap the sip/tip to validate
2786 * the probe). In a "redundant switch, common router" type of
2787 * configuration, the ARP probe will (hopefully) travel from
2788 * the active, through one switch, the router, then the other
2789 * switch before reaching the backup.
2790 */
2791 if (bond_is_active_slave(slave))
2792 bond_validate_arp(bond, slave, sip, tip);
2793 else
2794 bond_validate_arp(bond, slave, tip, sip);
2795
2796 out_unlock:
2797 read_unlock(&bond->lock);
2798 }
2799
2800 /*
2801 * this function is called regularly to monitor each slave's link
2802 * ensuring that traffic is being sent and received when arp monitoring
2803 * is used in load-balancing mode. if the adapter has been dormant, then an
2804 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2805 * arp monitoring in active backup mode.
2806 */
2807 void bond_loadbalance_arp_mon(struct work_struct *work)
2808 {
2809 struct bonding *bond = container_of(work, struct bonding,
2810 arp_work.work);
2811 struct slave *slave, *oldcurrent;
2812 int do_failover = 0;
2813 int delta_in_ticks;
2814 int i;
2815
2816 read_lock(&bond->lock);
2817
2818 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2819
2820 if (bond->kill_timers)
2821 goto out;
2822
2823 if (bond->slave_cnt == 0)
2824 goto re_arm;
2825
2826 read_lock(&bond->curr_slave_lock);
2827 oldcurrent = bond->curr_active_slave;
2828 read_unlock(&bond->curr_slave_lock);
2829
2830 /* see if any of the previous devices are up now (i.e. they have
2831 * xmt and rcv traffic). the curr_active_slave does not come into
2832 * the picture unless it is null. also, slave->jiffies is not needed
2833 * here because we send an arp on each slave and give a slave as
2834 * long as it needs to get the tx/rx within the delta.
2835 * TODO: what about up/down delay in arp mode? it wasn't here before
2836 * so it can wait
2837 */
2838 bond_for_each_slave(bond, slave, i) {
2839 unsigned long trans_start = dev_trans_start(slave->dev);
2840
2841 if (slave->link != BOND_LINK_UP) {
2842 if (time_in_range(jiffies,
2843 trans_start - delta_in_ticks,
2844 trans_start + delta_in_ticks) &&
2845 time_in_range(jiffies,
2846 slave->dev->last_rx - delta_in_ticks,
2847 slave->dev->last_rx + delta_in_ticks)) {
2848
2849 slave->link = BOND_LINK_UP;
2850 bond_set_active_slave(slave);
2851
2852 /* primary_slave has no meaning in round-robin
2853 * mode. the window of a slave being up and
2854 * curr_active_slave being null after enslaving
2855 * is closed.
2856 */
2857 if (!oldcurrent) {
2858 pr_info("%s: link status definitely up for interface %s, ",
2859 bond->dev->name,
2860 slave->dev->name);
2861 do_failover = 1;
2862 } else {
2863 pr_info("%s: interface %s is now up\n",
2864 bond->dev->name,
2865 slave->dev->name);
2866 }
2867 }
2868 } else {
2869 /* slave->link == BOND_LINK_UP */
2870
2871 /* not all switches will respond to an arp request
2872 * when the source ip is 0, so don't take the link down
2873 * if we don't know our ip yet
2874 */
2875 if (!time_in_range(jiffies,
2876 trans_start - delta_in_ticks,
2877 trans_start + 2 * delta_in_ticks) ||
2878 !time_in_range(jiffies,
2879 slave->dev->last_rx - delta_in_ticks,
2880 slave->dev->last_rx + 2 * delta_in_ticks)) {
2881
2882 slave->link = BOND_LINK_DOWN;
2883 bond_set_backup_slave(slave);
2884
2885 if (slave->link_failure_count < UINT_MAX)
2886 slave->link_failure_count++;
2887
2888 pr_info("%s: interface %s is now down.\n",
2889 bond->dev->name,
2890 slave->dev->name);
2891
2892 if (slave == oldcurrent)
2893 do_failover = 1;
2894 }
2895 }
2896
2897 /* note: if switch is in round-robin mode, all links
2898 * must tx arp to ensure all links rx an arp - otherwise
2899 * links may oscillate or not come up at all; if switch is
2900 * in something like xor mode, there is nothing we can
2901 * do - all replies will be rx'ed on same link causing slaves
2902 * to be unstable during low/no traffic periods
2903 */
2904 if (IS_UP(slave->dev))
2905 bond_arp_send_all(bond, slave);
2906 }
2907
2908 if (do_failover) {
2909 block_netpoll_tx();
2910 write_lock_bh(&bond->curr_slave_lock);
2911
2912 bond_select_active_slave(bond);
2913
2914 write_unlock_bh(&bond->curr_slave_lock);
2915 unblock_netpoll_tx();
2916 }
2917
2918 re_arm:
2919 if (bond->params.arp_interval)
2920 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
2921 out:
2922 read_unlock(&bond->lock);
2923 }
2924
2925 /*
2926 * Called to inspect slaves for active-backup mode ARP monitor link state
2927 * changes. Sets new_link in slaves to specify what action should take
2928 * place for the slave. Returns 0 if no changes are found, >0 if changes
2929 * to link states must be committed.
2930 *
2931 * Called with bond->lock held for read.
2932 */
2933 static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
2934 {
2935 struct slave *slave;
2936 int i, commit = 0;
2937 unsigned long trans_start;
2938
2939 bond_for_each_slave(bond, slave, i) {
2940 slave->new_link = BOND_LINK_NOCHANGE;
2941
2942 if (slave->link != BOND_LINK_UP) {
2943 if (time_in_range(jiffies,
2944 slave_last_rx(bond, slave) - delta_in_ticks,
2945 slave_last_rx(bond, slave) + delta_in_ticks)) {
2946
2947 slave->new_link = BOND_LINK_UP;
2948 commit++;
2949 }
2950
2951 continue;
2952 }
2953
2954 /*
2955 * Give slaves 2*delta after being enslaved or made
2956 * active. This avoids bouncing, as the last receive
2957 * times need a full ARP monitor cycle to be updated.
2958 */
2959 if (time_in_range(jiffies,
2960 slave->jiffies - delta_in_ticks,
2961 slave->jiffies + 2 * delta_in_ticks))
2962 continue;
2963
2964 /*
2965 * Backup slave is down if:
2966 * - No current_arp_slave AND
2967 * - more than 3*delta since last receive AND
2968 * - the bond has an IP address
2969 *
2970 * Note: a non-null current_arp_slave indicates
2971 * the curr_active_slave went down and we are
2972 * searching for a new one; under this condition
2973 * we only take the curr_active_slave down - this
2974 * gives each slave a chance to tx/rx traffic
2975 * before being taken out
2976 */
2977 if (!bond_is_active_slave(slave) &&
2978 !bond->current_arp_slave &&
2979 !time_in_range(jiffies,
2980 slave_last_rx(bond, slave) - delta_in_ticks,
2981 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
2982
2983 slave->new_link = BOND_LINK_DOWN;
2984 commit++;
2985 }
2986
2987 /*
2988 * Active slave is down if:
2989 * - more than 2*delta since transmitting OR
2990 * - (more than 2*delta since receive AND
2991 * the bond has an IP address)
2992 */
2993 trans_start = dev_trans_start(slave->dev);
2994 if (bond_is_active_slave(slave) &&
2995 (!time_in_range(jiffies,
2996 trans_start - delta_in_ticks,
2997 trans_start + 2 * delta_in_ticks) ||
2998 !time_in_range(jiffies,
2999 slave_last_rx(bond, slave) - delta_in_ticks,
3000 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3001
3002 slave->new_link = BOND_LINK_DOWN;
3003 commit++;
3004 }
3005 }
3006
3007 return commit;
3008 }
3009
3010 /*
3011 * Called to commit link state changes noted by inspection step of
3012 * active-backup mode ARP monitor.
3013 *
3014 * Called with RTNL and bond->lock for read.
3015 */
3016 static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3017 {
3018 struct slave *slave;
3019 int i;
3020 unsigned long trans_start;
3021
3022 bond_for_each_slave(bond, slave, i) {
3023 switch (slave->new_link) {
3024 case BOND_LINK_NOCHANGE:
3025 continue;
3026
3027 case BOND_LINK_UP:
3028 trans_start = dev_trans_start(slave->dev);
3029 if ((!bond->curr_active_slave &&
3030 time_in_range(jiffies,
3031 trans_start - delta_in_ticks,
3032 trans_start + delta_in_ticks)) ||
3033 bond->curr_active_slave != slave) {
3034 slave->link = BOND_LINK_UP;
3035 bond->current_arp_slave = NULL;
3036
3037 pr_info("%s: link status definitely up for interface %s.\n",
3038 bond->dev->name, slave->dev->name);
3039
3040 if (!bond->curr_active_slave ||
3041 (slave == bond->primary_slave))
3042 goto do_failover;
3043
3044 }
3045
3046 continue;
3047
3048 case BOND_LINK_DOWN:
3049 if (slave->link_failure_count < UINT_MAX)
3050 slave->link_failure_count++;
3051
3052 slave->link = BOND_LINK_DOWN;
3053 bond_set_slave_inactive_flags(slave);
3054
3055 pr_info("%s: link status definitely down for interface %s, disabling it\n",
3056 bond->dev->name, slave->dev->name);
3057
3058 if (slave == bond->curr_active_slave) {
3059 bond->current_arp_slave = NULL;
3060 goto do_failover;
3061 }
3062
3063 continue;
3064
3065 default:
3066 pr_err("%s: impossible: new_link %d on slave %s\n",
3067 bond->dev->name, slave->new_link,
3068 slave->dev->name);
3069 continue;
3070 }
3071
3072 do_failover:
3073 ASSERT_RTNL();
3074 block_netpoll_tx();
3075 write_lock_bh(&bond->curr_slave_lock);
3076 bond_select_active_slave(bond);
3077 write_unlock_bh(&bond->curr_slave_lock);
3078 unblock_netpoll_tx();
3079 }
3080
3081 bond_set_carrier(bond);
3082 }
3083
3084 /*
3085 * Send ARP probes for active-backup mode ARP monitor.
3086 *
3087 * Called with bond->lock held for read.
3088 */
3089 static void bond_ab_arp_probe(struct bonding *bond)
3090 {
3091 struct slave *slave;
3092 int i;
3093
3094 read_lock(&bond->curr_slave_lock);
3095
3096 if (bond->current_arp_slave && bond->curr_active_slave)
3097 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3098 bond->current_arp_slave->dev->name,
3099 bond->curr_active_slave->dev->name);
3100
3101 if (bond->curr_active_slave) {
3102 bond_arp_send_all(bond, bond->curr_active_slave);
3103 read_unlock(&bond->curr_slave_lock);
3104 return;
3105 }
3106
3107 read_unlock(&bond->curr_slave_lock);
3108
3109 /* if we don't have a curr_active_slave, search for the next available
3110 * backup slave from the current_arp_slave and make it the candidate
3111 * for becoming the curr_active_slave
3112 */
3113
3114 if (!bond->current_arp_slave) {
3115 bond->current_arp_slave = bond->first_slave;
3116 if (!bond->current_arp_slave)
3117 return;
3118 }
3119
3120 bond_set_slave_inactive_flags(bond->current_arp_slave);
3121
3122 /* search for next candidate */
3123 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3124 if (IS_UP(slave->dev)) {
3125 slave->link = BOND_LINK_BACK;
3126 bond_set_slave_active_flags(slave);
3127 bond_arp_send_all(bond, slave);
3128 slave->jiffies = jiffies;
3129 bond->current_arp_slave = slave;
3130 break;
3131 }
3132
3133 /* if the link state is up at this point, we
3134 * mark it down - this can happen if we have
3135 * simultaneous link failures and
3136 * reselect_active_interface doesn't make this
3137 * one the current slave so it is still marked
3138 * up when it is actually down
3139 */
3140 if (slave->link == BOND_LINK_UP) {
3141 slave->link = BOND_LINK_DOWN;
3142 if (slave->link_failure_count < UINT_MAX)
3143 slave->link_failure_count++;
3144
3145 bond_set_slave_inactive_flags(slave);
3146
3147 pr_info("%s: backup interface %s is now down.\n",
3148 bond->dev->name, slave->dev->name);
3149 }
3150 }
3151 }
3152
3153 void bond_activebackup_arp_mon(struct work_struct *work)
3154 {
3155 struct bonding *bond = container_of(work, struct bonding,
3156 arp_work.work);
3157 int delta_in_ticks;
3158
3159 read_lock(&bond->lock);
3160
3161 if (bond->kill_timers)
3162 goto out;
3163
3164 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3165
3166 if (bond->slave_cnt == 0)
3167 goto re_arm;
3168
3169 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3170 read_unlock(&bond->lock);
3171 rtnl_lock();
3172 read_lock(&bond->lock);
3173
3174 bond_ab_arp_commit(bond, delta_in_ticks);
3175
3176 read_unlock(&bond->lock);
3177 rtnl_unlock();
3178 read_lock(&bond->lock);
3179 }
3180
3181 bond_ab_arp_probe(bond);
3182
3183 re_arm:
3184 if (bond->params.arp_interval)
3185 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3186 out:
3187 read_unlock(&bond->lock);
3188 }
3189
3190 /*-------------------------- netdev event handling --------------------------*/
3191
3192 /*
3193 * Change device name
3194 */
3195 static int bond_event_changename(struct bonding *bond)
3196 {
3197 bond_remove_proc_entry(bond);
3198 bond_create_proc_entry(bond);
3199
3200 bond_debug_reregister(bond);
3201
3202 return NOTIFY_DONE;
3203 }
3204
3205 static int bond_master_netdev_event(unsigned long event,
3206 struct net_device *bond_dev)
3207 {
3208 struct bonding *event_bond = netdev_priv(bond_dev);
3209
3210 switch (event) {
3211 case NETDEV_CHANGENAME:
3212 return bond_event_changename(event_bond);
3213 default:
3214 break;
3215 }
3216
3217 return NOTIFY_DONE;
3218 }
3219
3220 static int bond_slave_netdev_event(unsigned long event,
3221 struct net_device *slave_dev)
3222 {
3223 struct net_device *bond_dev = slave_dev->master;
3224 struct bonding *bond = netdev_priv(bond_dev);
3225
3226 switch (event) {
3227 case NETDEV_UNREGISTER:
3228 if (bond_dev) {
3229 if (bond->setup_by_slave)
3230 bond_release_and_destroy(bond_dev, slave_dev);
3231 else
3232 bond_release(bond_dev, slave_dev);
3233 }
3234 break;
3235 case NETDEV_CHANGE:
3236 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3237 struct slave *slave;
3238
3239 slave = bond_get_slave_by_dev(bond, slave_dev);
3240 if (slave) {
3241 u32 old_speed = slave->speed;
3242 u8 old_duplex = slave->duplex;
3243
3244 bond_update_speed_duplex(slave);
3245
3246 if (bond_is_lb(bond))
3247 break;
3248
3249 if (old_speed != slave->speed)
3250 bond_3ad_adapter_speed_changed(slave);
3251 if (old_duplex != slave->duplex)
3252 bond_3ad_adapter_duplex_changed(slave);
3253 }
3254 }
3255
3256 break;
3257 case NETDEV_DOWN:
3258 /*
3259 * ... Or is it this?
3260 */
3261 break;
3262 case NETDEV_CHANGEMTU:
3263 /*
3264 * TODO: Should slaves be allowed to
3265 * independently alter their MTU? For
3266 * an active-backup bond, slaves need
3267 * not be the same type of device, so
3268 * MTUs may vary. For other modes,
3269 * slaves arguably should have the
3270 * same MTUs. To do this, we'd need to
3271 * take over the slave's change_mtu
3272 * function for the duration of their
3273 * servitude.
3274 */
3275 break;
3276 case NETDEV_CHANGENAME:
3277 /*
3278 * TODO: handle changing the primary's name
3279 */
3280 break;
3281 case NETDEV_FEAT_CHANGE:
3282 bond_compute_features(bond);
3283 break;
3284 default:
3285 break;
3286 }
3287
3288 return NOTIFY_DONE;
3289 }
3290
3291 /*
3292 * bond_netdev_event: handle netdev notifier chain events.
3293 *
3294 * This function receives events for the netdev chain. The caller (an
3295 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
3296 * locks for us to safely manipulate the slave devices (RTNL lock,
3297 * dev_probe_lock).
3298 */
3299 static int bond_netdev_event(struct notifier_block *this,
3300 unsigned long event, void *ptr)
3301 {
3302 struct net_device *event_dev = (struct net_device *)ptr;
3303
3304 pr_debug("event_dev: %s, event: %lx\n",
3305 event_dev ? event_dev->name : "None",
3306 event);
3307
3308 if (!(event_dev->priv_flags & IFF_BONDING))
3309 return NOTIFY_DONE;
3310
3311 if (event_dev->flags & IFF_MASTER) {
3312 pr_debug("IFF_MASTER\n");
3313 return bond_master_netdev_event(event, event_dev);
3314 }
3315
3316 if (event_dev->flags & IFF_SLAVE) {
3317 pr_debug("IFF_SLAVE\n");
3318 return bond_slave_netdev_event(event, event_dev);
3319 }
3320
3321 return NOTIFY_DONE;
3322 }
3323
3324 /*
3325 * bond_inetaddr_event: handle inetaddr notifier chain events.
3326 *
3327 * We keep track of device IPs primarily to use as source addresses in
3328 * ARP monitor probes (rather than spewing out broadcasts all the time).
3329 *
3330 * We track one IP for the main device (if it has one), plus one per VLAN.
3331 */
3332 static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3333 {
3334 struct in_ifaddr *ifa = ptr;
3335 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3336 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
3337 struct bonding *bond;
3338 struct vlan_entry *vlan;
3339
3340 list_for_each_entry(bond, &bn->dev_list, bond_list) {
3341 if (bond->dev == event_dev) {
3342 switch (event) {
3343 case NETDEV_UP:
3344 bond->master_ip = ifa->ifa_local;
3345 return NOTIFY_OK;
3346 case NETDEV_DOWN:
3347 bond->master_ip = bond_glean_dev_ip(bond->dev);
3348 return NOTIFY_OK;
3349 default:
3350 return NOTIFY_DONE;
3351 }
3352 }
3353
3354 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
3355 if (!bond->vlgrp)
3356 continue;
3357 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
3358 if (vlan_dev == event_dev) {
3359 switch (event) {
3360 case NETDEV_UP:
3361 vlan->vlan_ip = ifa->ifa_local;
3362 return NOTIFY_OK;
3363 case NETDEV_DOWN:
3364 vlan->vlan_ip =
3365 bond_glean_dev_ip(vlan_dev);
3366 return NOTIFY_OK;
3367 default:
3368 return NOTIFY_DONE;
3369 }
3370 }
3371 }
3372 }
3373 return NOTIFY_DONE;
3374 }
3375
3376 static struct notifier_block bond_netdev_notifier = {
3377 .notifier_call = bond_netdev_event,
3378 };
3379
3380 static struct notifier_block bond_inetaddr_notifier = {
3381 .notifier_call = bond_inetaddr_event,
3382 };
3383
3384 /*---------------------------- Hashing Policies -----------------------------*/
3385
3386 /*
3387 * Hash for the output device based upon layer 2 and layer 3 data. If
3388 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3389 */
3390 static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
3391 {
3392 struct ethhdr *data = (struct ethhdr *)skb->data;
3393 struct iphdr *iph = ip_hdr(skb);
3394
3395 if (skb->protocol == htons(ETH_P_IP)) {
3396 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3397 (data->h_dest[5] ^ data->h_source[5])) % count;
3398 }
3399
3400 return (data->h_dest[5] ^ data->h_source[5]) % count;
3401 }
3402
3403 /*
3404 * Hash for the output device based upon layer 3 and layer 4 data. If
3405 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3406 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3407 */
3408 static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
3409 {
3410 struct ethhdr *data = (struct ethhdr *)skb->data;
3411 struct iphdr *iph = ip_hdr(skb);
3412 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
3413 int layer4_xor = 0;
3414
3415 if (skb->protocol == htons(ETH_P_IP)) {
3416 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
3417 (iph->protocol == IPPROTO_TCP ||
3418 iph->protocol == IPPROTO_UDP)) {
3419 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
3420 }
3421 return (layer4_xor ^
3422 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3423
3424 }
3425
3426 return (data->h_dest[5] ^ data->h_source[5]) % count;
3427 }
3428
3429 /*
3430 * Hash for the output device based upon layer 2 data
3431 */
3432 static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
3433 {
3434 struct ethhdr *data = (struct ethhdr *)skb->data;
3435
3436 return (data->h_dest[5] ^ data->h_source[5]) % count;
3437 }
3438
3439 /*-------------------------- Device entry points ----------------------------*/
3440
3441 static int bond_open(struct net_device *bond_dev)
3442 {
3443 struct bonding *bond = netdev_priv(bond_dev);
3444
3445 bond->kill_timers = 0;
3446
3447 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3448
3449 if (bond_is_lb(bond)) {
3450 /* bond_alb_initialize must be called before the timer
3451 * is started.
3452 */
3453 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3454 /* something went wrong - fail the open operation */
3455 return -ENOMEM;
3456 }
3457
3458 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3459 queue_delayed_work(bond->wq, &bond->alb_work, 0);
3460 }
3461
3462 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3463 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3464 queue_delayed_work(bond->wq, &bond->mii_work, 0);
3465 }
3466
3467 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3468 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3469 INIT_DELAYED_WORK(&bond->arp_work,
3470 bond_activebackup_arp_mon);
3471 else
3472 INIT_DELAYED_WORK(&bond->arp_work,
3473 bond_loadbalance_arp_mon);
3474
3475 queue_delayed_work(bond->wq, &bond->arp_work, 0);
3476 if (bond->params.arp_validate)
3477 bond->recv_probe = bond_arp_rcv;
3478 }
3479
3480 if (bond->params.mode == BOND_MODE_8023AD) {
3481 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3482 queue_delayed_work(bond->wq, &bond->ad_work, 0);
3483 /* register to receive LACPDUs */
3484 bond->recv_probe = bond_3ad_lacpdu_recv;
3485 bond_3ad_initiate_agg_selection(bond, 1);
3486 }
3487
3488 return 0;
3489 }
3490
3491 static int bond_close(struct net_device *bond_dev)
3492 {
3493 struct bonding *bond = netdev_priv(bond_dev);
3494
3495 write_lock_bh(&bond->lock);
3496
3497 /* signal timers not to re-arm */
3498 bond->kill_timers = 1;
3499
3500 write_unlock_bh(&bond->lock);
3501
3502 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3503 cancel_delayed_work(&bond->mii_work);
3504 }
3505
3506 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3507 cancel_delayed_work(&bond->arp_work);
3508 }
3509
3510 switch (bond->params.mode) {
3511 case BOND_MODE_8023AD:
3512 cancel_delayed_work(&bond->ad_work);
3513 break;
3514 case BOND_MODE_TLB:
3515 case BOND_MODE_ALB:
3516 cancel_delayed_work(&bond->alb_work);
3517 break;
3518 default:
3519 break;
3520 }
3521
3522 if (delayed_work_pending(&bond->mcast_work))
3523 cancel_delayed_work(&bond->mcast_work);
3524
3525 if (bond_is_lb(bond)) {
3526 /* Must be called only after all
3527 * slaves have been released
3528 */
3529 bond_alb_deinitialize(bond);
3530 }
3531 bond->recv_probe = NULL;
3532
3533 return 0;
3534 }
3535
3536 static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3537 struct rtnl_link_stats64 *stats)
3538 {
3539 struct bonding *bond = netdev_priv(bond_dev);
3540 struct rtnl_link_stats64 temp;
3541 struct slave *slave;
3542 int i;
3543
3544 memset(stats, 0, sizeof(*stats));
3545
3546 read_lock_bh(&bond->lock);
3547
3548 bond_for_each_slave(bond, slave, i) {
3549 const struct rtnl_link_stats64 *sstats =
3550 dev_get_stats(slave->dev, &temp);
3551
3552 stats->rx_packets += sstats->rx_packets;
3553 stats->rx_bytes += sstats->rx_bytes;
3554 stats->rx_errors += sstats->rx_errors;
3555 stats->rx_dropped += sstats->rx_dropped;
3556
3557 stats->tx_packets += sstats->tx_packets;
3558 stats->tx_bytes += sstats->tx_bytes;
3559 stats->tx_errors += sstats->tx_errors;
3560 stats->tx_dropped += sstats->tx_dropped;
3561
3562 stats->multicast += sstats->multicast;
3563 stats->collisions += sstats->collisions;
3564
3565 stats->rx_length_errors += sstats->rx_length_errors;
3566 stats->rx_over_errors += sstats->rx_over_errors;
3567 stats->rx_crc_errors += sstats->rx_crc_errors;
3568 stats->rx_frame_errors += sstats->rx_frame_errors;
3569 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3570 stats->rx_missed_errors += sstats->rx_missed_errors;
3571
3572 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3573 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3574 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3575 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3576 stats->tx_window_errors += sstats->tx_window_errors;
3577 }
3578
3579 read_unlock_bh(&bond->lock);
3580
3581 return stats;
3582 }
3583
3584 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3585 {
3586 struct net_device *slave_dev = NULL;
3587 struct ifbond k_binfo;
3588 struct ifbond __user *u_binfo = NULL;
3589 struct ifslave k_sinfo;
3590 struct ifslave __user *u_sinfo = NULL;
3591 struct mii_ioctl_data *mii = NULL;
3592 int res = 0;
3593
3594 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
3595
3596 switch (cmd) {
3597 case SIOCGMIIPHY:
3598 mii = if_mii(ifr);
3599 if (!mii)
3600 return -EINVAL;
3601
3602 mii->phy_id = 0;
3603 /* Fall Through */
3604 case SIOCGMIIREG:
3605 /*
3606 * We do this again just in case we were called by SIOCGMIIREG
3607 * instead of SIOCGMIIPHY.
3608 */
3609 mii = if_mii(ifr);
3610 if (!mii)
3611 return -EINVAL;
3612
3613
3614 if (mii->reg_num == 1) {
3615 struct bonding *bond = netdev_priv(bond_dev);
3616 mii->val_out = 0;
3617 read_lock(&bond->lock);
3618 read_lock(&bond->curr_slave_lock);
3619 if (netif_carrier_ok(bond->dev))
3620 mii->val_out = BMSR_LSTATUS;
3621
3622 read_unlock(&bond->curr_slave_lock);
3623 read_unlock(&bond->lock);
3624 }
3625
3626 return 0;
3627 case BOND_INFO_QUERY_OLD:
3628 case SIOCBONDINFOQUERY:
3629 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3630
3631 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
3632 return -EFAULT;
3633
3634 res = bond_info_query(bond_dev, &k_binfo);
3635 if (res == 0 &&
3636 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3637 return -EFAULT;
3638
3639 return res;
3640 case BOND_SLAVE_INFO_QUERY_OLD:
3641 case SIOCBONDSLAVEINFOQUERY:
3642 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3643
3644 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
3645 return -EFAULT;
3646
3647 res = bond_slave_info_query(bond_dev, &k_sinfo);
3648 if (res == 0 &&
3649 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3650 return -EFAULT;
3651
3652 return res;
3653 default:
3654 /* Go on */
3655 break;
3656 }
3657
3658 if (!capable(CAP_NET_ADMIN))
3659 return -EPERM;
3660
3661 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
3662
3663 pr_debug("slave_dev=%p:\n", slave_dev);
3664
3665 if (!slave_dev)
3666 res = -ENODEV;
3667 else {
3668 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
3669 switch (cmd) {
3670 case BOND_ENSLAVE_OLD:
3671 case SIOCBONDENSLAVE:
3672 res = bond_enslave(bond_dev, slave_dev);
3673 break;
3674 case BOND_RELEASE_OLD:
3675 case SIOCBONDRELEASE:
3676 res = bond_release(bond_dev, slave_dev);
3677 break;
3678 case BOND_SETHWADDR_OLD:
3679 case SIOCBONDSETHWADDR:
3680 res = bond_sethwaddr(bond_dev, slave_dev);
3681 break;
3682 case BOND_CHANGE_ACTIVE_OLD:
3683 case SIOCBONDCHANGEACTIVE:
3684 res = bond_ioctl_change_active(bond_dev, slave_dev);
3685 break;
3686 default:
3687 res = -EOPNOTSUPP;
3688 }
3689
3690 dev_put(slave_dev);
3691 }
3692
3693 return res;
3694 }
3695
3696 static bool bond_addr_in_mc_list(unsigned char *addr,
3697 struct netdev_hw_addr_list *list,
3698 int addrlen)
3699 {
3700 struct netdev_hw_addr *ha;
3701
3702 netdev_hw_addr_list_for_each(ha, list)
3703 if (!memcmp(ha->addr, addr, addrlen))
3704 return true;
3705
3706 return false;
3707 }
3708
3709 static void bond_set_multicast_list(struct net_device *bond_dev)
3710 {
3711 struct bonding *bond = netdev_priv(bond_dev);
3712 struct netdev_hw_addr *ha;
3713 bool found;
3714
3715 /*
3716 * Do promisc before checking multicast_mode
3717 */
3718 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
3719 /*
3720 * FIXME: Need to handle the error when one of the multi-slaves
3721 * encounters error.
3722 */
3723 bond_set_promiscuity(bond, 1);
3724
3725
3726 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
3727 bond_set_promiscuity(bond, -1);
3728
3729
3730 /* set allmulti flag to slaves */
3731 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
3732 /*
3733 * FIXME: Need to handle the error when one of the multi-slaves
3734 * encounters error.
3735 */
3736 bond_set_allmulti(bond, 1);
3737
3738
3739 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
3740 bond_set_allmulti(bond, -1);
3741
3742
3743 read_lock(&bond->lock);
3744
3745 bond->flags = bond_dev->flags;
3746
3747 /* looking for addresses to add to slaves' mc list */
3748 netdev_for_each_mc_addr(ha, bond_dev) {
3749 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3750 bond_dev->addr_len);
3751 if (!found)
3752 bond_mc_add(bond, ha->addr);
3753 }
3754
3755 /* looking for addresses to delete from slaves' list */
3756 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3757 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3758 bond_dev->addr_len);
3759 if (!found)
3760 bond_mc_del(bond, ha->addr);
3761 }
3762
3763 /* save master's multicast list */
3764 __hw_addr_flush(&bond->mc_list);
3765 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3766 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
3767
3768 read_unlock(&bond->lock);
3769 }
3770
3771 static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3772 {
3773 struct bonding *bond = netdev_priv(dev);
3774 struct slave *slave = bond->first_slave;
3775
3776 if (slave) {
3777 const struct net_device_ops *slave_ops
3778 = slave->dev->netdev_ops;
3779 if (slave_ops->ndo_neigh_setup)
3780 return slave_ops->ndo_neigh_setup(slave->dev, parms);
3781 }
3782 return 0;
3783 }
3784
3785 /*
3786 * Change the MTU of all of a master's slaves to match the master
3787 */
3788 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3789 {
3790 struct bonding *bond = netdev_priv(bond_dev);
3791 struct slave *slave, *stop_at;
3792 int res = 0;
3793 int i;
3794
3795 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
3796 (bond_dev ? bond_dev->name : "None"), new_mtu);
3797
3798 /* Can't hold bond->lock with bh disabled here since
3799 * some base drivers panic. On the other hand we can't
3800 * hold bond->lock without bh disabled because we'll
3801 * deadlock. The only solution is to rely on the fact
3802 * that we're under rtnl_lock here, and the slaves
3803 * list won't change. This doesn't solve the problem
3804 * of setting the slave's MTU while it is
3805 * transmitting, but the assumption is that the base
3806 * driver can handle that.
3807 *
3808 * TODO: figure out a way to safely iterate the slaves
3809 * list, but without holding a lock around the actual
3810 * call to the base driver.
3811 */
3812
3813 bond_for_each_slave(bond, slave, i) {
3814 pr_debug("s %p s->p %p c_m %p\n",
3815 slave,
3816 slave->prev,
3817 slave->dev->netdev_ops->ndo_change_mtu);
3818
3819 res = dev_set_mtu(slave->dev, new_mtu);
3820
3821 if (res) {
3822 /* If we failed to set the slave's mtu to the new value
3823 * we must abort the operation even in ACTIVE_BACKUP
3824 * mode, because if we allow the backup slaves to have
3825 * different mtu values than the active slave we'll
3826 * need to change their mtu when doing a failover. That
3827 * means changing their mtu from timer context, which
3828 * is probably not a good idea.
3829 */
3830 pr_debug("err %d %s\n", res, slave->dev->name);
3831 goto unwind;
3832 }
3833 }
3834
3835 bond_dev->mtu = new_mtu;
3836
3837 return 0;
3838
3839 unwind:
3840 /* unwind from head to the slave that failed */
3841 stop_at = slave;
3842 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3843 int tmp_res;
3844
3845 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3846 if (tmp_res) {
3847 pr_debug("unwind err %d dev %s\n",
3848 tmp_res, slave->dev->name);
3849 }
3850 }
3851
3852 return res;
3853 }
3854
3855 /*
3856 * Change HW address
3857 *
3858 * Note that many devices must be down to change the HW address, and
3859 * downing the master releases all slaves. We can make bonds full of
3860 * bonding devices to test this, however.
3861 */
3862 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3863 {
3864 struct bonding *bond = netdev_priv(bond_dev);
3865 struct sockaddr *sa = addr, tmp_sa;
3866 struct slave *slave, *stop_at;
3867 int res = 0;
3868 int i;
3869
3870 if (bond->params.mode == BOND_MODE_ALB)
3871 return bond_alb_set_mac_address(bond_dev, addr);
3872
3873
3874 pr_debug("bond=%p, name=%s\n",
3875 bond, bond_dev ? bond_dev->name : "None");
3876
3877 /*
3878 * If fail_over_mac is set to active, do nothing and return
3879 * success. Returning an error causes ifenslave to fail.
3880 */
3881 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
3882 return 0;
3883
3884 if (!is_valid_ether_addr(sa->sa_data))
3885 return -EADDRNOTAVAIL;
3886
3887 /* Can't hold bond->lock with bh disabled here since
3888 * some base drivers panic. On the other hand we can't
3889 * hold bond->lock without bh disabled because we'll
3890 * deadlock. The only solution is to rely on the fact
3891 * that we're under rtnl_lock here, and the slaves
3892 * list won't change. This doesn't solve the problem
3893 * of setting the slave's hw address while it is
3894 * transmitting, but the assumption is that the base
3895 * driver can handle that.
3896 *
3897 * TODO: figure out a way to safely iterate the slaves
3898 * list, but without holding a lock around the actual
3899 * call to the base driver.
3900 */
3901
3902 bond_for_each_slave(bond, slave, i) {
3903 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
3904 pr_debug("slave %p %s\n", slave, slave->dev->name);
3905
3906 if (slave_ops->ndo_set_mac_address == NULL) {
3907 res = -EOPNOTSUPP;
3908 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
3909 goto unwind;
3910 }
3911
3912 res = dev_set_mac_address(slave->dev, addr);
3913 if (res) {
3914 /* TODO: consider downing the slave
3915 * and retry ?
3916 * User should expect communications
3917 * breakage anyway until ARP finish
3918 * updating, so...
3919 */
3920 pr_debug("err %d %s\n", res, slave->dev->name);
3921 goto unwind;
3922 }
3923 }
3924
3925 /* success */
3926 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3927 return 0;
3928
3929 unwind:
3930 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3931 tmp_sa.sa_family = bond_dev->type;
3932
3933 /* unwind from head to the slave that failed */
3934 stop_at = slave;
3935 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3936 int tmp_res;
3937
3938 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3939 if (tmp_res) {
3940 pr_debug("unwind err %d dev %s\n",
3941 tmp_res, slave->dev->name);
3942 }
3943 }
3944
3945 return res;
3946 }
3947
3948 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3949 {
3950 struct bonding *bond = netdev_priv(bond_dev);
3951 struct slave *slave, *start_at;
3952 int i, slave_no, res = 1;
3953 struct iphdr *iph = ip_hdr(skb);
3954
3955 read_lock(&bond->lock);
3956
3957 if (!BOND_IS_OK(bond))
3958 goto out;
3959 /*
3960 * Start with the curr_active_slave that joined the bond as the
3961 * default for sending IGMP traffic. For failover purposes one
3962 * needs to maintain some consistency for the interface that will
3963 * send the join/membership reports. The curr_active_slave found
3964 * will send all of this type of traffic.
3965 */
3966 if ((iph->protocol == IPPROTO_IGMP) &&
3967 (skb->protocol == htons(ETH_P_IP))) {
3968
3969 read_lock(&bond->curr_slave_lock);
3970 slave = bond->curr_active_slave;
3971 read_unlock(&bond->curr_slave_lock);
3972
3973 if (!slave)
3974 goto out;
3975 } else {
3976 /*
3977 * Concurrent TX may collide on rr_tx_counter; we accept
3978 * that as being rare enough not to justify using an
3979 * atomic op here.
3980 */
3981 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
3982
3983 bond_for_each_slave(bond, slave, i) {
3984 slave_no--;
3985 if (slave_no < 0)
3986 break;
3987 }
3988 }
3989
3990 start_at = slave;
3991 bond_for_each_slave_from(bond, slave, i, start_at) {
3992 if (IS_UP(slave->dev) &&
3993 (slave->link == BOND_LINK_UP) &&
3994 bond_is_active_slave(slave)) {
3995 res = bond_dev_queue_xmit(bond, skb, slave->dev);
3996 break;
3997 }
3998 }
3999
4000 out:
4001 if (res) {
4002 /* no suitable interface, frame not sent */
4003 dev_kfree_skb(skb);
4004 }
4005 read_unlock(&bond->lock);
4006 return NETDEV_TX_OK;
4007 }
4008
4009
4010 /*
4011 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4012 * the bond has a usable interface.
4013 */
4014 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4015 {
4016 struct bonding *bond = netdev_priv(bond_dev);
4017 int res = 1;
4018
4019 read_lock(&bond->lock);
4020 read_lock(&bond->curr_slave_lock);
4021
4022 if (!BOND_IS_OK(bond))
4023 goto out;
4024
4025 if (!bond->curr_active_slave)
4026 goto out;
4027
4028 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4029
4030 out:
4031 if (res)
4032 /* no suitable interface, frame not sent */
4033 dev_kfree_skb(skb);
4034
4035 read_unlock(&bond->curr_slave_lock);
4036 read_unlock(&bond->lock);
4037 return NETDEV_TX_OK;
4038 }
4039
4040 /*
4041 * In bond_xmit_xor() , we determine the output device by using a pre-
4042 * determined xmit_hash_policy(), If the selected device is not enabled,
4043 * find the next active slave.
4044 */
4045 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4046 {
4047 struct bonding *bond = netdev_priv(bond_dev);
4048 struct slave *slave, *start_at;
4049 int slave_no;
4050 int i;
4051 int res = 1;
4052
4053 read_lock(&bond->lock);
4054
4055 if (!BOND_IS_OK(bond))
4056 goto out;
4057
4058 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
4059
4060 bond_for_each_slave(bond, slave, i) {
4061 slave_no--;
4062 if (slave_no < 0)
4063 break;
4064 }
4065
4066 start_at = slave;
4067
4068 bond_for_each_slave_from(bond, slave, i, start_at) {
4069 if (IS_UP(slave->dev) &&
4070 (slave->link == BOND_LINK_UP) &&
4071 bond_is_active_slave(slave)) {
4072 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4073 break;
4074 }
4075 }
4076
4077 out:
4078 if (res) {
4079 /* no suitable interface, frame not sent */
4080 dev_kfree_skb(skb);
4081 }
4082 read_unlock(&bond->lock);
4083 return NETDEV_TX_OK;
4084 }
4085
4086 /*
4087 * in broadcast mode, we send everything to all usable interfaces.
4088 */
4089 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4090 {
4091 struct bonding *bond = netdev_priv(bond_dev);
4092 struct slave *slave, *start_at;
4093 struct net_device *tx_dev = NULL;
4094 int i;
4095 int res = 1;
4096
4097 read_lock(&bond->lock);
4098
4099 if (!BOND_IS_OK(bond))
4100 goto out;
4101
4102 read_lock(&bond->curr_slave_lock);
4103 start_at = bond->curr_active_slave;
4104 read_unlock(&bond->curr_slave_lock);
4105
4106 if (!start_at)
4107 goto out;
4108
4109 bond_for_each_slave_from(bond, slave, i, start_at) {
4110 if (IS_UP(slave->dev) &&
4111 (slave->link == BOND_LINK_UP) &&
4112 bond_is_active_slave(slave)) {
4113 if (tx_dev) {
4114 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4115 if (!skb2) {
4116 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
4117 bond_dev->name);
4118 continue;
4119 }
4120
4121 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4122 if (res) {
4123 dev_kfree_skb(skb2);
4124 continue;
4125 }
4126 }
4127 tx_dev = slave->dev;
4128 }
4129 }
4130
4131 if (tx_dev)
4132 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4133
4134 out:
4135 if (res)
4136 /* no suitable interface, frame not sent */
4137 dev_kfree_skb(skb);
4138
4139 /* frame sent to all suitable interfaces */
4140 read_unlock(&bond->lock);
4141 return NETDEV_TX_OK;
4142 }
4143
4144 /*------------------------- Device initialization ---------------------------*/
4145
4146 static void bond_set_xmit_hash_policy(struct bonding *bond)
4147 {
4148 switch (bond->params.xmit_policy) {
4149 case BOND_XMIT_POLICY_LAYER23:
4150 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4151 break;
4152 case BOND_XMIT_POLICY_LAYER34:
4153 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4154 break;
4155 case BOND_XMIT_POLICY_LAYER2:
4156 default:
4157 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4158 break;
4159 }
4160 }
4161
4162 /*
4163 * Lookup the slave that corresponds to a qid
4164 */
4165 static inline int bond_slave_override(struct bonding *bond,
4166 struct sk_buff *skb)
4167 {
4168 int i, res = 1;
4169 struct slave *slave = NULL;
4170 struct slave *check_slave;
4171
4172 read_lock(&bond->lock);
4173
4174 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4175 goto out;
4176
4177 /* Find out if any slaves have the same mapping as this skb. */
4178 bond_for_each_slave(bond, check_slave, i) {
4179 if (check_slave->queue_id == skb->queue_mapping) {
4180 slave = check_slave;
4181 break;
4182 }
4183 }
4184
4185 /* If the slave isn't UP, use default transmit policy. */
4186 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4187 (slave->link == BOND_LINK_UP)) {
4188 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4189 }
4190
4191 out:
4192 read_unlock(&bond->lock);
4193 return res;
4194 }
4195
4196 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4197 {
4198 /*
4199 * This helper function exists to help dev_pick_tx get the correct
4200 * destination queue. Using a helper function skips a call to
4201 * skb_tx_hash and will put the skbs in the queue we expect on their
4202 * way down to the bonding driver.
4203 */
4204 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4205
4206 if (unlikely(txq >= dev->real_num_tx_queues)) {
4207 do {
4208 txq -= dev->real_num_tx_queues;
4209 } while (txq >= dev->real_num_tx_queues);
4210 }
4211 return txq;
4212 }
4213
4214 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4215 {
4216 struct bonding *bond = netdev_priv(dev);
4217
4218 /*
4219 * If we risk deadlock from transmitting this in the
4220 * netpoll path, tell netpoll to queue the frame for later tx
4221 */
4222 if (is_netpoll_tx_blocked(dev))
4223 return NETDEV_TX_BUSY;
4224
4225 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4226 if (!bond_slave_override(bond, skb))
4227 return NETDEV_TX_OK;
4228 }
4229
4230 switch (bond->params.mode) {
4231 case BOND_MODE_ROUNDROBIN:
4232 return bond_xmit_roundrobin(skb, dev);
4233 case BOND_MODE_ACTIVEBACKUP:
4234 return bond_xmit_activebackup(skb, dev);
4235 case BOND_MODE_XOR:
4236 return bond_xmit_xor(skb, dev);
4237 case BOND_MODE_BROADCAST:
4238 return bond_xmit_broadcast(skb, dev);
4239 case BOND_MODE_8023AD:
4240 return bond_3ad_xmit_xor(skb, dev);
4241 case BOND_MODE_ALB:
4242 case BOND_MODE_TLB:
4243 return bond_alb_xmit(skb, dev);
4244 default:
4245 /* Should never happen, mode already checked */
4246 pr_err("%s: Error: Unknown bonding mode %d\n",
4247 dev->name, bond->params.mode);
4248 WARN_ON_ONCE(1);
4249 dev_kfree_skb(skb);
4250 return NETDEV_TX_OK;
4251 }
4252 }
4253
4254
4255 /*
4256 * set bond mode specific net device operations
4257 */
4258 void bond_set_mode_ops(struct bonding *bond, int mode)
4259 {
4260 struct net_device *bond_dev = bond->dev;
4261
4262 switch (mode) {
4263 case BOND_MODE_ROUNDROBIN:
4264 break;
4265 case BOND_MODE_ACTIVEBACKUP:
4266 break;
4267 case BOND_MODE_XOR:
4268 bond_set_xmit_hash_policy(bond);
4269 break;
4270 case BOND_MODE_BROADCAST:
4271 break;
4272 case BOND_MODE_8023AD:
4273 bond_set_xmit_hash_policy(bond);
4274 break;
4275 case BOND_MODE_ALB:
4276 /* FALLTHRU */
4277 case BOND_MODE_TLB:
4278 break;
4279 default:
4280 /* Should never happen, mode already checked */
4281 pr_err("%s: Error: Unknown bonding mode %d\n",
4282 bond_dev->name, mode);
4283 break;
4284 }
4285 }
4286
4287 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4288 struct ethtool_drvinfo *drvinfo)
4289 {
4290 strncpy(drvinfo->driver, DRV_NAME, 32);
4291 strncpy(drvinfo->version, DRV_VERSION, 32);
4292 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4293 }
4294
4295 static const struct ethtool_ops bond_ethtool_ops = {
4296 .get_drvinfo = bond_ethtool_get_drvinfo,
4297 .get_link = ethtool_op_get_link,
4298 .get_tx_csum = ethtool_op_get_tx_csum,
4299 .get_sg = ethtool_op_get_sg,
4300 .get_tso = ethtool_op_get_tso,
4301 .get_ufo = ethtool_op_get_ufo,
4302 .get_flags = ethtool_op_get_flags,
4303 };
4304
4305 static const struct net_device_ops bond_netdev_ops = {
4306 .ndo_init = bond_init,
4307 .ndo_uninit = bond_uninit,
4308 .ndo_open = bond_open,
4309 .ndo_stop = bond_close,
4310 .ndo_start_xmit = bond_start_xmit,
4311 .ndo_select_queue = bond_select_queue,
4312 .ndo_get_stats64 = bond_get_stats,
4313 .ndo_do_ioctl = bond_do_ioctl,
4314 .ndo_set_multicast_list = bond_set_multicast_list,
4315 .ndo_change_mtu = bond_change_mtu,
4316 .ndo_set_mac_address = bond_set_mac_address,
4317 .ndo_neigh_setup = bond_neigh_setup,
4318 .ndo_vlan_rx_register = bond_vlan_rx_register,
4319 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4320 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4321 #ifdef CONFIG_NET_POLL_CONTROLLER
4322 .ndo_netpoll_setup = bond_netpoll_setup,
4323 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4324 .ndo_poll_controller = bond_poll_controller,
4325 #endif
4326 .ndo_add_slave = bond_enslave,
4327 .ndo_del_slave = bond_release,
4328 };
4329
4330 static void bond_destructor(struct net_device *bond_dev)
4331 {
4332 struct bonding *bond = netdev_priv(bond_dev);
4333 if (bond->wq)
4334 destroy_workqueue(bond->wq);
4335 free_netdev(bond_dev);
4336 }
4337
4338 static void bond_setup(struct net_device *bond_dev)
4339 {
4340 struct bonding *bond = netdev_priv(bond_dev);
4341
4342 /* initialize rwlocks */
4343 rwlock_init(&bond->lock);
4344 rwlock_init(&bond->curr_slave_lock);
4345
4346 bond->params = bonding_defaults;
4347
4348 /* Initialize pointers */
4349 bond->dev = bond_dev;
4350 INIT_LIST_HEAD(&bond->vlan_list);
4351
4352 /* Initialize the device entry points */
4353 ether_setup(bond_dev);
4354 bond_dev->netdev_ops = &bond_netdev_ops;
4355 bond_dev->ethtool_ops = &bond_ethtool_ops;
4356 bond_set_mode_ops(bond, bond->params.mode);
4357
4358 bond_dev->destructor = bond_destructor;
4359
4360 /* Initialize the device options */
4361 bond_dev->tx_queue_len = 0;
4362 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4363 bond_dev->priv_flags |= IFF_BONDING;
4364 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4365
4366 /* At first, we block adding VLANs. That's the only way to
4367 * prevent problems that occur when adding VLANs over an
4368 * empty bond. The block will be removed once non-challenged
4369 * slaves are enslaved.
4370 */
4371 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4372
4373 /* don't acquire bond device's netif_tx_lock when
4374 * transmitting */
4375 bond_dev->features |= NETIF_F_LLTX;
4376
4377 /* By default, we declare the bond to be fully
4378 * VLAN hardware accelerated capable. Special
4379 * care is taken in the various xmit functions
4380 * when there are slaves that are not hw accel
4381 * capable
4382 */
4383 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4384 NETIF_F_HW_VLAN_RX |
4385 NETIF_F_HW_VLAN_FILTER);
4386
4387 /* By default, we enable GRO on bonding devices.
4388 * Actual support requires lowlevel drivers are GRO ready.
4389 */
4390 bond_dev->features |= NETIF_F_GRO;
4391 }
4392
4393 static void bond_work_cancel_all(struct bonding *bond)
4394 {
4395 write_lock_bh(&bond->lock);
4396 bond->kill_timers = 1;
4397 write_unlock_bh(&bond->lock);
4398
4399 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4400 cancel_delayed_work(&bond->mii_work);
4401
4402 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4403 cancel_delayed_work(&bond->arp_work);
4404
4405 if (bond->params.mode == BOND_MODE_ALB &&
4406 delayed_work_pending(&bond->alb_work))
4407 cancel_delayed_work(&bond->alb_work);
4408
4409 if (bond->params.mode == BOND_MODE_8023AD &&
4410 delayed_work_pending(&bond->ad_work))
4411 cancel_delayed_work(&bond->ad_work);
4412
4413 if (delayed_work_pending(&bond->mcast_work))
4414 cancel_delayed_work(&bond->mcast_work);
4415 }
4416
4417 /*
4418 * Destroy a bonding device.
4419 * Must be under rtnl_lock when this function is called.
4420 */
4421 static void bond_uninit(struct net_device *bond_dev)
4422 {
4423 struct bonding *bond = netdev_priv(bond_dev);
4424 struct vlan_entry *vlan, *tmp;
4425
4426 bond_netpoll_cleanup(bond_dev);
4427
4428 /* Release the bonded slaves */
4429 bond_release_all(bond_dev);
4430
4431 list_del(&bond->bond_list);
4432
4433 bond_work_cancel_all(bond);
4434
4435 bond_remove_proc_entry(bond);
4436
4437 bond_debug_unregister(bond);
4438
4439 __hw_addr_flush(&bond->mc_list);
4440
4441 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4442 list_del(&vlan->vlan_list);
4443 kfree(vlan);
4444 }
4445 }
4446
4447 /*------------------------- Module initialization ---------------------------*/
4448
4449 /*
4450 * Convert string input module parms. Accept either the
4451 * number of the mode or its string name. A bit complicated because
4452 * some mode names are substrings of other names, and calls from sysfs
4453 * may have whitespace in the name (trailing newlines, for example).
4454 */
4455 int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
4456 {
4457 int modeint = -1, i, rv;
4458 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
4459
4460 for (p = (char *)buf; *p; p++)
4461 if (!(isdigit(*p) || isspace(*p)))
4462 break;
4463
4464 if (*p)
4465 rv = sscanf(buf, "%20s", modestr);
4466 else
4467 rv = sscanf(buf, "%d", &modeint);
4468
4469 if (!rv)
4470 return -1;
4471
4472 for (i = 0; tbl[i].modename; i++) {
4473 if (modeint == tbl[i].mode)
4474 return tbl[i].mode;
4475 if (strcmp(modestr, tbl[i].modename) == 0)
4476 return tbl[i].mode;
4477 }
4478
4479 return -1;
4480 }
4481
4482 static int bond_check_params(struct bond_params *params)
4483 {
4484 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
4485
4486 /*
4487 * Convert string parameters.
4488 */
4489 if (mode) {
4490 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4491 if (bond_mode == -1) {
4492 pr_err("Error: Invalid bonding mode \"%s\"\n",
4493 mode == NULL ? "NULL" : mode);
4494 return -EINVAL;
4495 }
4496 }
4497
4498 if (xmit_hash_policy) {
4499 if ((bond_mode != BOND_MODE_XOR) &&
4500 (bond_mode != BOND_MODE_8023AD)) {
4501 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
4502 bond_mode_name(bond_mode));
4503 } else {
4504 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4505 xmit_hashtype_tbl);
4506 if (xmit_hashtype == -1) {
4507 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
4508 xmit_hash_policy == NULL ? "NULL" :
4509 xmit_hash_policy);
4510 return -EINVAL;
4511 }
4512 }
4513 }
4514
4515 if (lacp_rate) {
4516 if (bond_mode != BOND_MODE_8023AD) {
4517 pr_info("lacp_rate param is irrelevant in mode %s\n",
4518 bond_mode_name(bond_mode));
4519 } else {
4520 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4521 if (lacp_fast == -1) {
4522 pr_err("Error: Invalid lacp rate \"%s\"\n",
4523 lacp_rate == NULL ? "NULL" : lacp_rate);
4524 return -EINVAL;
4525 }
4526 }
4527 }
4528
4529 if (ad_select) {
4530 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4531 if (params->ad_select == -1) {
4532 pr_err("Error: Invalid ad_select \"%s\"\n",
4533 ad_select == NULL ? "NULL" : ad_select);
4534 return -EINVAL;
4535 }
4536
4537 if (bond_mode != BOND_MODE_8023AD) {
4538 pr_warning("ad_select param only affects 802.3ad mode\n");
4539 }
4540 } else {
4541 params->ad_select = BOND_AD_STABLE;
4542 }
4543
4544 if (max_bonds < 0) {
4545 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4546 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4547 max_bonds = BOND_DEFAULT_MAX_BONDS;
4548 }
4549
4550 if (miimon < 0) {
4551 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4552 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4553 miimon = BOND_LINK_MON_INTERV;
4554 }
4555
4556 if (updelay < 0) {
4557 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4558 updelay, INT_MAX);
4559 updelay = 0;
4560 }
4561
4562 if (downdelay < 0) {
4563 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4564 downdelay, INT_MAX);
4565 downdelay = 0;
4566 }
4567
4568 if ((use_carrier != 0) && (use_carrier != 1)) {
4569 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4570 use_carrier);
4571 use_carrier = 1;
4572 }
4573
4574 /* reset values for 802.3ad */
4575 if (bond_mode == BOND_MODE_8023AD) {
4576 if (!miimon) {
4577 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
4578 pr_warning("Forcing miimon to 100msec\n");
4579 miimon = 100;
4580 }
4581 }
4582
4583 if (tx_queues < 1 || tx_queues > 255) {
4584 pr_warning("Warning: tx_queues (%d) should be between "
4585 "1 and 255, resetting to %d\n",
4586 tx_queues, BOND_DEFAULT_TX_QUEUES);
4587 tx_queues = BOND_DEFAULT_TX_QUEUES;
4588 }
4589
4590 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4591 pr_warning("Warning: all_slaves_active module parameter (%d), "
4592 "not of valid value (0/1), so it was set to "
4593 "0\n", all_slaves_active);
4594 all_slaves_active = 0;
4595 }
4596
4597 if (resend_igmp < 0 || resend_igmp > 255) {
4598 pr_warning("Warning: resend_igmp (%d) should be between "
4599 "0 and 255, resetting to %d\n",
4600 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4601 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4602 }
4603
4604 /* reset values for TLB/ALB */
4605 if ((bond_mode == BOND_MODE_TLB) ||
4606 (bond_mode == BOND_MODE_ALB)) {
4607 if (!miimon) {
4608 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure and link speed which are essential for TLB/ALB load balancing\n");
4609 pr_warning("Forcing miimon to 100msec\n");
4610 miimon = 100;
4611 }
4612 }
4613
4614 if (bond_mode == BOND_MODE_ALB) {
4615 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
4616 updelay);
4617 }
4618
4619 if (!miimon) {
4620 if (updelay || downdelay) {
4621 /* just warn the user the up/down delay will have
4622 * no effect since miimon is zero...
4623 */
4624 pr_warning("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
4625 updelay, downdelay);
4626 }
4627 } else {
4628 /* don't allow arp monitoring */
4629 if (arp_interval) {
4630 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4631 miimon, arp_interval);
4632 arp_interval = 0;
4633 }
4634
4635 if ((updelay % miimon) != 0) {
4636 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4637 updelay, miimon,
4638 (updelay / miimon) * miimon);
4639 }
4640
4641 updelay /= miimon;
4642
4643 if ((downdelay % miimon) != 0) {
4644 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4645 downdelay, miimon,
4646 (downdelay / miimon) * miimon);
4647 }
4648
4649 downdelay /= miimon;
4650 }
4651
4652 if (arp_interval < 0) {
4653 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4654 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4655 arp_interval = BOND_LINK_ARP_INTERV;
4656 }
4657
4658 for (arp_ip_count = 0;
4659 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4660 arp_ip_count++) {
4661 /* not complete check, but should be good enough to
4662 catch mistakes */
4663 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4664 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4665 arp_ip_target[arp_ip_count]);
4666 arp_interval = 0;
4667 } else {
4668 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
4669 arp_target[arp_ip_count] = ip;
4670 }
4671 }
4672
4673 if (arp_interval && !arp_ip_count) {
4674 /* don't allow arping if no arp_ip_target given... */
4675 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4676 arp_interval);
4677 arp_interval = 0;
4678 }
4679
4680 if (arp_validate) {
4681 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4682 pr_err("arp_validate only supported in active-backup mode\n");
4683 return -EINVAL;
4684 }
4685 if (!arp_interval) {
4686 pr_err("arp_validate requires arp_interval\n");
4687 return -EINVAL;
4688 }
4689
4690 arp_validate_value = bond_parse_parm(arp_validate,
4691 arp_validate_tbl);
4692 if (arp_validate_value == -1) {
4693 pr_err("Error: invalid arp_validate \"%s\"\n",
4694 arp_validate == NULL ? "NULL" : arp_validate);
4695 return -EINVAL;
4696 }
4697 } else
4698 arp_validate_value = 0;
4699
4700 if (miimon) {
4701 pr_info("MII link monitoring set to %d ms\n", miimon);
4702 } else if (arp_interval) {
4703 int i;
4704
4705 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4706 arp_interval,
4707 arp_validate_tbl[arp_validate_value].modename,
4708 arp_ip_count);
4709
4710 for (i = 0; i < arp_ip_count; i++)
4711 pr_info(" %s", arp_ip_target[i]);
4712
4713 pr_info("\n");
4714
4715 } else if (max_bonds) {
4716 /* miimon and arp_interval not set, we need one so things
4717 * work as expected, see bonding.txt for details
4718 */
4719 pr_warning("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details.\n");
4720 }
4721
4722 if (primary && !USES_PRIMARY(bond_mode)) {
4723 /* currently, using a primary only makes sense
4724 * in active backup, TLB or ALB modes
4725 */
4726 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4727 primary, bond_mode_name(bond_mode));
4728 primary = NULL;
4729 }
4730
4731 if (primary && primary_reselect) {
4732 primary_reselect_value = bond_parse_parm(primary_reselect,
4733 pri_reselect_tbl);
4734 if (primary_reselect_value == -1) {
4735 pr_err("Error: Invalid primary_reselect \"%s\"\n",
4736 primary_reselect ==
4737 NULL ? "NULL" : primary_reselect);
4738 return -EINVAL;
4739 }
4740 } else {
4741 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4742 }
4743
4744 if (fail_over_mac) {
4745 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4746 fail_over_mac_tbl);
4747 if (fail_over_mac_value == -1) {
4748 pr_err("Error: invalid fail_over_mac \"%s\"\n",
4749 arp_validate == NULL ? "NULL" : arp_validate);
4750 return -EINVAL;
4751 }
4752
4753 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
4754 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
4755 } else {
4756 fail_over_mac_value = BOND_FOM_NONE;
4757 }
4758
4759 /* fill params struct with the proper values */
4760 params->mode = bond_mode;
4761 params->xmit_policy = xmit_hashtype;
4762 params->miimon = miimon;
4763 params->arp_interval = arp_interval;
4764 params->arp_validate = arp_validate_value;
4765 params->updelay = updelay;
4766 params->downdelay = downdelay;
4767 params->use_carrier = use_carrier;
4768 params->lacp_fast = lacp_fast;
4769 params->primary[0] = 0;
4770 params->primary_reselect = primary_reselect_value;
4771 params->fail_over_mac = fail_over_mac_value;
4772 params->tx_queues = tx_queues;
4773 params->all_slaves_active = all_slaves_active;
4774 params->resend_igmp = resend_igmp;
4775
4776 if (primary) {
4777 strncpy(params->primary, primary, IFNAMSIZ);
4778 params->primary[IFNAMSIZ - 1] = 0;
4779 }
4780
4781 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4782
4783 return 0;
4784 }
4785
4786 static struct lock_class_key bonding_netdev_xmit_lock_key;
4787 static struct lock_class_key bonding_netdev_addr_lock_key;
4788
4789 static void bond_set_lockdep_class_one(struct net_device *dev,
4790 struct netdev_queue *txq,
4791 void *_unused)
4792 {
4793 lockdep_set_class(&txq->_xmit_lock,
4794 &bonding_netdev_xmit_lock_key);
4795 }
4796
4797 static void bond_set_lockdep_class(struct net_device *dev)
4798 {
4799 lockdep_set_class(&dev->addr_list_lock,
4800 &bonding_netdev_addr_lock_key);
4801 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
4802 }
4803
4804 /*
4805 * Called from registration process
4806 */
4807 static int bond_init(struct net_device *bond_dev)
4808 {
4809 struct bonding *bond = netdev_priv(bond_dev);
4810 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
4811
4812 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4813
4814 bond->wq = create_singlethread_workqueue(bond_dev->name);
4815 if (!bond->wq)
4816 return -ENOMEM;
4817
4818 bond_set_lockdep_class(bond_dev);
4819
4820 bond_create_proc_entry(bond);
4821 list_add_tail(&bond->bond_list, &bn->dev_list);
4822
4823 bond_prepare_sysfs_group(bond);
4824
4825 bond_debug_register(bond);
4826
4827 __hw_addr_init(&bond->mc_list);
4828 return 0;
4829 }
4830
4831 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4832 {
4833 if (tb[IFLA_ADDRESS]) {
4834 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4835 return -EINVAL;
4836 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4837 return -EADDRNOTAVAIL;
4838 }
4839 return 0;
4840 }
4841
4842 static struct rtnl_link_ops bond_link_ops __read_mostly = {
4843 .kind = "bond",
4844 .priv_size = sizeof(struct bonding),
4845 .setup = bond_setup,
4846 .validate = bond_validate,
4847 };
4848
4849 /* Create a new bond based on the specified name and bonding parameters.
4850 * If name is NULL, obtain a suitable "bond%d" name for us.
4851 * Caller must NOT hold rtnl_lock; we need to release it here before we
4852 * set up our sysfs entries.
4853 */
4854 int bond_create(struct net *net, const char *name)
4855 {
4856 struct net_device *bond_dev;
4857 int res;
4858
4859 rtnl_lock();
4860
4861 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
4862 bond_setup, tx_queues);
4863 if (!bond_dev) {
4864 pr_err("%s: eek! can't alloc netdev!\n", name);
4865 rtnl_unlock();
4866 return -ENOMEM;
4867 }
4868
4869 dev_net_set(bond_dev, net);
4870 bond_dev->rtnl_link_ops = &bond_link_ops;
4871
4872 if (!name) {
4873 res = dev_alloc_name(bond_dev, "bond%d");
4874 if (res < 0)
4875 goto out;
4876 } else {
4877 /*
4878 * If we're given a name to register
4879 * we need to ensure that its not already
4880 * registered
4881 */
4882 res = -EEXIST;
4883 if (__dev_get_by_name(net, name) != NULL)
4884 goto out;
4885 }
4886
4887 res = register_netdevice(bond_dev);
4888
4889 netif_carrier_off(bond_dev);
4890
4891 out:
4892 rtnl_unlock();
4893 if (res < 0)
4894 bond_destructor(bond_dev);
4895 return res;
4896 }
4897
4898 static int __net_init bond_net_init(struct net *net)
4899 {
4900 struct bond_net *bn = net_generic(net, bond_net_id);
4901
4902 bn->net = net;
4903 INIT_LIST_HEAD(&bn->dev_list);
4904
4905 bond_create_proc_dir(bn);
4906
4907 return 0;
4908 }
4909
4910 static void __net_exit bond_net_exit(struct net *net)
4911 {
4912 struct bond_net *bn = net_generic(net, bond_net_id);
4913
4914 bond_destroy_proc_dir(bn);
4915 }
4916
4917 static struct pernet_operations bond_net_ops = {
4918 .init = bond_net_init,
4919 .exit = bond_net_exit,
4920 .id = &bond_net_id,
4921 .size = sizeof(struct bond_net),
4922 };
4923
4924 static int __init bonding_init(void)
4925 {
4926 int i;
4927 int res;
4928
4929 pr_info("%s", bond_version);
4930
4931 res = bond_check_params(&bonding_defaults);
4932 if (res)
4933 goto out;
4934
4935 res = register_pernet_subsys(&bond_net_ops);
4936 if (res)
4937 goto out;
4938
4939 res = rtnl_link_register(&bond_link_ops);
4940 if (res)
4941 goto err_link;
4942
4943 bond_create_debugfs();
4944
4945 for (i = 0; i < max_bonds; i++) {
4946 res = bond_create(&init_net, NULL);
4947 if (res)
4948 goto err;
4949 }
4950
4951 res = bond_create_sysfs();
4952 if (res)
4953 goto err;
4954
4955 register_netdevice_notifier(&bond_netdev_notifier);
4956 register_inetaddr_notifier(&bond_inetaddr_notifier);
4957 out:
4958 return res;
4959 err:
4960 rtnl_link_unregister(&bond_link_ops);
4961 err_link:
4962 unregister_pernet_subsys(&bond_net_ops);
4963 goto out;
4964
4965 }
4966
4967 static void __exit bonding_exit(void)
4968 {
4969 unregister_netdevice_notifier(&bond_netdev_notifier);
4970 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
4971
4972 bond_destroy_sysfs();
4973 bond_destroy_debugfs();
4974
4975 rtnl_link_unregister(&bond_link_ops);
4976 unregister_pernet_subsys(&bond_net_ops);
4977
4978 #ifdef CONFIG_NET_POLL_CONTROLLER
4979 /*
4980 * Make sure we don't have an imbalance on our netpoll blocking
4981 */
4982 WARN_ON(atomic_read(&netpoll_block_tx));
4983 #endif
4984 }
4985
4986 module_init(bonding_init);
4987 module_exit(bonding_exit);
4988 MODULE_LICENSE("GPL");
4989 MODULE_VERSION(DRV_VERSION);
4990 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4991 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4992 MODULE_ALIAS_RTNL_LINK("bond");
This page took 0.231576 seconds and 6 git commands to generate.