2 * drivers/net/bond/bond_options.c - bonding options
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/rwlock.h>
18 #include <linux/rcupdate.h>
19 #include <linux/reciprocal_div.h>
22 static bool bond_mode_is_valid(int mode
)
26 for (i
= 0; bond_mode_tbl
[i
].modename
; i
++);
28 return mode
>= 0 && mode
< i
;
31 int bond_option_mode_set(struct bonding
*bond
, int mode
)
33 if (!bond_mode_is_valid(mode
)) {
34 pr_err("invalid mode value %d.\n", mode
);
38 if (bond
->dev
->flags
& IFF_UP
) {
39 pr_err("%s: unable to update mode because interface is up.\n",
44 if (bond_has_slaves(bond
)) {
45 pr_err("%s: unable to update mode because bond has slaves.\n",
50 if (BOND_NO_USES_ARP(mode
) && bond
->params
.arp_interval
) {
51 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
52 bond
->dev
->name
, bond_mode_tbl
[mode
].modename
);
53 /* disable arp monitoring */
54 bond
->params
.arp_interval
= 0;
55 /* set miimon to default value */
56 bond
->params
.miimon
= BOND_DEFAULT_MIIMON
;
57 pr_info("%s: Setting MII monitoring interval to %d.\n",
58 bond
->dev
->name
, bond
->params
.miimon
);
61 /* don't cache arp_validate between modes */
62 bond
->params
.arp_validate
= BOND_ARP_VALIDATE_NONE
;
63 bond
->params
.mode
= mode
;
67 static struct net_device
*__bond_option_active_slave_get(struct bonding
*bond
,
70 return USES_PRIMARY(bond
->params
.mode
) && slave
? slave
->dev
: NULL
;
73 struct net_device
*bond_option_active_slave_get_rcu(struct bonding
*bond
)
75 struct slave
*slave
= rcu_dereference(bond
->curr_active_slave
);
77 return __bond_option_active_slave_get(bond
, slave
);
80 struct net_device
*bond_option_active_slave_get(struct bonding
*bond
)
82 return __bond_option_active_slave_get(bond
, bond
->curr_active_slave
);
85 int bond_option_active_slave_set(struct bonding
*bond
,
86 struct net_device
*slave_dev
)
91 if (!netif_is_bond_slave(slave_dev
)) {
92 pr_err("Device %s is not bonding slave.\n",
97 if (bond
->dev
!= netdev_master_upper_dev_get(slave_dev
)) {
98 pr_err("%s: Device %s is not our slave.\n",
99 bond
->dev
->name
, slave_dev
->name
);
104 if (!USES_PRIMARY(bond
->params
.mode
)) {
105 pr_err("%s: Unable to change active slave; %s is in mode %d\n",
106 bond
->dev
->name
, bond
->dev
->name
, bond
->params
.mode
);
111 write_lock_bh(&bond
->curr_slave_lock
);
113 /* check to see if we are clearing active */
115 pr_info("%s: Clearing current active slave.\n",
117 rcu_assign_pointer(bond
->curr_active_slave
, NULL
);
118 bond_select_active_slave(bond
);
120 struct slave
*old_active
= bond
->curr_active_slave
;
121 struct slave
*new_active
= bond_slave_get_rtnl(slave_dev
);
125 if (new_active
== old_active
) {
127 pr_info("%s: %s is already the current active slave.\n",
128 bond
->dev
->name
, new_active
->dev
->name
);
130 if (old_active
&& (new_active
->link
== BOND_LINK_UP
) &&
131 IS_UP(new_active
->dev
)) {
132 pr_info("%s: Setting %s as active slave.\n",
133 bond
->dev
->name
, new_active
->dev
->name
);
134 bond_change_active_slave(bond
, new_active
);
136 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
137 bond
->dev
->name
, new_active
->dev
->name
,
138 new_active
->dev
->name
);
144 write_unlock_bh(&bond
->curr_slave_lock
);
145 unblock_netpoll_tx();
149 int bond_option_miimon_set(struct bonding
*bond
, int miimon
)
152 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
153 bond
->dev
->name
, miimon
, 0, INT_MAX
);
156 pr_info("%s: Setting MII monitoring interval to %d.\n",
157 bond
->dev
->name
, miimon
);
158 bond
->params
.miimon
= miimon
;
159 if (bond
->params
.updelay
)
160 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
162 bond
->params
.updelay
* bond
->params
.miimon
);
163 if (bond
->params
.downdelay
)
164 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
166 bond
->params
.downdelay
* bond
->params
.miimon
);
167 if (miimon
&& bond
->params
.arp_interval
) {
168 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
170 bond
->params
.arp_interval
= 0;
171 if (bond
->params
.arp_validate
)
172 bond
->params
.arp_validate
= BOND_ARP_VALIDATE_NONE
;
174 if (bond
->dev
->flags
& IFF_UP
) {
175 /* If the interface is up, we may need to fire off
176 * the MII timer. If the interface is down, the
177 * timer will get fired off when the open function
181 cancel_delayed_work_sync(&bond
->mii_work
);
183 cancel_delayed_work_sync(&bond
->arp_work
);
184 queue_delayed_work(bond
->wq
, &bond
->mii_work
, 0);
190 int bond_option_updelay_set(struct bonding
*bond
, int updelay
)
192 if (!(bond
->params
.miimon
)) {
193 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
199 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
200 bond
->dev
->name
, updelay
, 0, INT_MAX
);
203 if ((updelay
% bond
->params
.miimon
) != 0) {
204 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
205 bond
->dev
->name
, updelay
,
207 (updelay
/ bond
->params
.miimon
) *
208 bond
->params
.miimon
);
210 bond
->params
.updelay
= updelay
/ bond
->params
.miimon
;
211 pr_info("%s: Setting up delay to %d.\n",
213 bond
->params
.updelay
* bond
->params
.miimon
);
219 int bond_option_downdelay_set(struct bonding
*bond
, int downdelay
)
221 if (!(bond
->params
.miimon
)) {
222 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
228 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
229 bond
->dev
->name
, downdelay
, 0, INT_MAX
);
232 if ((downdelay
% bond
->params
.miimon
) != 0) {
233 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
234 bond
->dev
->name
, downdelay
,
236 (downdelay
/ bond
->params
.miimon
) *
237 bond
->params
.miimon
);
239 bond
->params
.downdelay
= downdelay
/ bond
->params
.miimon
;
240 pr_info("%s: Setting down delay to %d.\n",
242 bond
->params
.downdelay
* bond
->params
.miimon
);
248 int bond_option_use_carrier_set(struct bonding
*bond
, int use_carrier
)
250 if ((use_carrier
== 0) || (use_carrier
== 1)) {
251 bond
->params
.use_carrier
= use_carrier
;
252 pr_info("%s: Setting use_carrier to %d.\n",
253 bond
->dev
->name
, use_carrier
);
255 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
256 bond
->dev
->name
, use_carrier
);
262 int bond_option_arp_interval_set(struct bonding
*bond
, int arp_interval
)
264 if (arp_interval
< 0) {
265 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
266 bond
->dev
->name
, arp_interval
, INT_MAX
);
269 if (BOND_NO_USES_ARP(bond
->params
.mode
)) {
270 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
271 bond
->dev
->name
, bond
->dev
->name
);
274 pr_info("%s: Setting ARP monitoring interval to %d.\n",
275 bond
->dev
->name
, arp_interval
);
276 bond
->params
.arp_interval
= arp_interval
;
278 if (bond
->params
.miimon
) {
279 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
280 bond
->dev
->name
, bond
->dev
->name
);
281 bond
->params
.miimon
= 0;
283 if (!bond
->params
.arp_targets
[0])
284 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
287 if (bond
->dev
->flags
& IFF_UP
) {
288 /* If the interface is up, we may need to fire off
289 * the ARP timer. If the interface is down, the
290 * timer will get fired off when the open function
294 if (bond
->params
.arp_validate
)
295 bond
->recv_probe
= NULL
;
296 cancel_delayed_work_sync(&bond
->arp_work
);
298 /* arp_validate can be set only in active-backup mode */
299 if (bond
->params
.arp_validate
)
300 bond
->recv_probe
= bond_arp_rcv
;
301 cancel_delayed_work_sync(&bond
->mii_work
);
302 queue_delayed_work(bond
->wq
, &bond
->arp_work
, 0);
309 static void _bond_options_arp_ip_target_set(struct bonding
*bond
, int slot
,
311 unsigned long last_rx
)
313 __be32
*targets
= bond
->params
.arp_targets
;
314 struct list_head
*iter
;
317 if (slot
>= 0 && slot
< BOND_MAX_ARP_TARGETS
) {
318 bond_for_each_slave(bond
, slave
, iter
)
319 slave
->target_last_arp_rx
[slot
] = last_rx
;
320 targets
[slot
] = target
;
324 static int _bond_option_arp_ip_target_add(struct bonding
*bond
, __be32 target
)
326 __be32
*targets
= bond
->params
.arp_targets
;
329 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target
)) {
330 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
331 bond
->dev
->name
, &target
);
335 if (bond_get_targets_ip(targets
, target
) != -1) { /* dup */
336 pr_err("%s: ARP target %pI4 is already present\n",
337 bond
->dev
->name
, &target
);
341 ind
= bond_get_targets_ip(targets
, 0); /* first free slot */
343 pr_err("%s: ARP target table is full!\n",
348 pr_info("%s: adding ARP target %pI4.\n", bond
->dev
->name
, &target
);
350 _bond_options_arp_ip_target_set(bond
, ind
, target
, jiffies
);
355 int bond_option_arp_ip_target_add(struct bonding
*bond
, __be32 target
)
359 /* not to race with bond_arp_rcv */
360 write_lock_bh(&bond
->lock
);
361 ret
= _bond_option_arp_ip_target_add(bond
, target
);
362 write_unlock_bh(&bond
->lock
);
367 int bond_option_arp_ip_target_rem(struct bonding
*bond
, __be32 target
)
369 __be32
*targets
= bond
->params
.arp_targets
;
370 struct list_head
*iter
;
372 unsigned long *targets_rx
;
375 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target
)) {
376 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
377 bond
->dev
->name
, &target
);
381 ind
= bond_get_targets_ip(targets
, target
);
383 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
384 bond
->dev
->name
, &target
);
388 if (ind
== 0 && !targets
[1] && bond
->params
.arp_interval
)
389 pr_warn("%s: removing last arp target with arp_interval on\n",
392 pr_info("%s: removing ARP target %pI4.\n", bond
->dev
->name
,
395 /* not to race with bond_arp_rcv */
396 write_lock_bh(&bond
->lock
);
398 bond_for_each_slave(bond
, slave
, iter
) {
399 targets_rx
= slave
->target_last_arp_rx
;
400 for (i
= ind
; (i
< BOND_MAX_ARP_TARGETS
-1) && targets
[i
+1]; i
++)
401 targets_rx
[i
] = targets_rx
[i
+1];
404 for (i
= ind
; (i
< BOND_MAX_ARP_TARGETS
-1) && targets
[i
+1]; i
++)
405 targets
[i
] = targets
[i
+1];
408 write_unlock_bh(&bond
->lock
);
413 int bond_option_arp_ip_targets_set(struct bonding
*bond
, __be32
*targets
,
418 /* not to race with bond_arp_rcv */
419 write_lock_bh(&bond
->lock
);
422 for (i
= 0; i
< BOND_MAX_ARP_TARGETS
; i
++)
423 _bond_options_arp_ip_target_set(bond
, i
, 0, 0);
425 if (count
== 0 && bond
->params
.arp_interval
)
426 pr_warn("%s: removing last arp target with arp_interval on\n",
429 for (i
= 0; i
< count
; i
++) {
430 ret
= _bond_option_arp_ip_target_add(bond
, targets
[i
]);
435 write_unlock_bh(&bond
->lock
);
439 int bond_option_arp_validate_set(struct bonding
*bond
, int arp_validate
)
441 if (bond
->params
.mode
!= BOND_MODE_ACTIVEBACKUP
) {
442 pr_err("%s: arp_validate only supported in active-backup mode.\n",
446 pr_info("%s: setting arp_validate to %s (%d).\n",
447 bond
->dev
->name
, arp_validate_tbl
[arp_validate
].modename
,
450 if (bond
->dev
->flags
& IFF_UP
) {
452 bond
->recv_probe
= NULL
;
453 else if (bond
->params
.arp_interval
)
454 bond
->recv_probe
= bond_arp_rcv
;
456 bond
->params
.arp_validate
= arp_validate
;
461 int bond_option_arp_all_targets_set(struct bonding
*bond
, int arp_all_targets
)
463 pr_info("%s: setting arp_all_targets to %s (%d).\n",
464 bond
->dev
->name
, arp_all_targets_tbl
[arp_all_targets
].modename
,
467 bond
->params
.arp_all_targets
= arp_all_targets
;
472 int bond_option_primary_set(struct bonding
*bond
, const char *primary
)
474 struct list_head
*iter
;
479 read_lock(&bond
->lock
);
480 write_lock_bh(&bond
->curr_slave_lock
);
482 if (!USES_PRIMARY(bond
->params
.mode
)) {
483 pr_err("%s: Unable to set primary slave; %s is in mode %d\n",
484 bond
->dev
->name
, bond
->dev
->name
, bond
->params
.mode
);
489 /* check to see if we are clearing primary */
490 if (!strlen(primary
)) {
491 pr_info("%s: Setting primary slave to None.\n",
493 bond
->primary_slave
= NULL
;
494 memset(bond
->params
.primary
, 0, sizeof(bond
->params
.primary
));
495 bond_select_active_slave(bond
);
499 bond_for_each_slave(bond
, slave
, iter
) {
500 if (strncmp(slave
->dev
->name
, primary
, IFNAMSIZ
) == 0) {
501 pr_info("%s: Setting %s as primary slave.\n",
502 bond
->dev
->name
, slave
->dev
->name
);
503 bond
->primary_slave
= slave
;
504 strcpy(bond
->params
.primary
, slave
->dev
->name
);
505 bond_select_active_slave(bond
);
510 strncpy(bond
->params
.primary
, primary
, IFNAMSIZ
);
511 bond
->params
.primary
[IFNAMSIZ
- 1] = 0;
513 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet.\n",
514 bond
->dev
->name
, primary
, bond
->dev
->name
);
517 write_unlock_bh(&bond
->curr_slave_lock
);
518 read_unlock(&bond
->lock
);
519 unblock_netpoll_tx();
524 int bond_option_primary_reselect_set(struct bonding
*bond
, int primary_reselect
)
526 bond
->params
.primary_reselect
= primary_reselect
;
527 pr_info("%s: setting primary_reselect to %s (%d).\n",
528 bond
->dev
->name
, pri_reselect_tbl
[primary_reselect
].modename
,
532 write_lock_bh(&bond
->curr_slave_lock
);
533 bond_select_active_slave(bond
);
534 write_unlock_bh(&bond
->curr_slave_lock
);
535 unblock_netpoll_tx();
540 int bond_option_fail_over_mac_set(struct bonding
*bond
, int fail_over_mac
)
542 if (bond_has_slaves(bond
)) {
543 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
548 bond
->params
.fail_over_mac
= fail_over_mac
;
549 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
550 bond
->dev
->name
, fail_over_mac_tbl
[fail_over_mac
].modename
,
556 int bond_option_xmit_hash_policy_set(struct bonding
*bond
, int xmit_hash_policy
)
558 bond
->params
.xmit_policy
= xmit_hash_policy
;
559 pr_info("%s: setting xmit hash policy to %s (%d).\n",
561 xmit_hashtype_tbl
[xmit_hash_policy
].modename
, xmit_hash_policy
);
566 int bond_option_resend_igmp_set(struct bonding
*bond
, int resend_igmp
)
568 if (resend_igmp
< 0 || resend_igmp
> 255) {
569 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
570 bond
->dev
->name
, resend_igmp
);
574 bond
->params
.resend_igmp
= resend_igmp
;
575 pr_info("%s: Setting resend_igmp to %d.\n",
576 bond
->dev
->name
, resend_igmp
);
581 int bond_option_num_peer_notif_set(struct bonding
*bond
, int num_peer_notif
)
583 bond
->params
.num_peer_notif
= num_peer_notif
;
587 int bond_option_all_slaves_active_set(struct bonding
*bond
,
588 int all_slaves_active
)
590 struct list_head
*iter
;
593 if (all_slaves_active
== bond
->params
.all_slaves_active
)
596 if ((all_slaves_active
== 0) || (all_slaves_active
== 1)) {
597 bond
->params
.all_slaves_active
= all_slaves_active
;
599 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
600 bond
->dev
->name
, all_slaves_active
);
604 bond_for_each_slave(bond
, slave
, iter
) {
605 if (!bond_is_active_slave(slave
)) {
606 if (all_slaves_active
)
616 int bond_option_min_links_set(struct bonding
*bond
, int min_links
)
618 pr_info("%s: Setting min links value to %u\n",
619 bond
->dev
->name
, min_links
);
620 bond
->params
.min_links
= min_links
;
625 int bond_option_lp_interval_set(struct bonding
*bond
, int lp_interval
)
627 if (lp_interval
<= 0) {
628 pr_err("%s: lp_interval must be between 1 and %d\n",
629 bond
->dev
->name
, INT_MAX
);
633 bond
->params
.lp_interval
= lp_interval
;
638 int bond_option_packets_per_slave_set(struct bonding
*bond
,
639 int packets_per_slave
)
641 if (packets_per_slave
< 0 || packets_per_slave
> USHRT_MAX
) {
642 pr_err("%s: packets_per_slave must be between 0 and %u\n",
643 bond
->dev
->name
, USHRT_MAX
);
647 if (bond
->params
.mode
!= BOND_MODE_ROUNDROBIN
)
648 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
651 if (packets_per_slave
> 1)
652 bond
->params
.packets_per_slave
=
653 reciprocal_value(packets_per_slave
);
655 bond
->params
.packets_per_slave
= packets_per_slave
;
660 int bond_option_lacp_rate_set(struct bonding
*bond
, int lacp_rate
)
662 if (bond
->dev
->flags
& IFF_UP
) {
663 pr_err("%s: Unable to update LACP rate because interface is up.\n",
668 if (bond
->params
.mode
!= BOND_MODE_8023AD
) {
669 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
674 if ((lacp_rate
== 1) || (lacp_rate
== 0)) {
675 bond
->params
.lacp_fast
= lacp_rate
;
676 bond_3ad_update_lacp_rate(bond
);
677 pr_info("%s: Setting LACP rate to %s (%d).\n",
678 bond
->dev
->name
, bond_lacp_tbl
[lacp_rate
].modename
,
681 pr_err("%s: Ignoring invalid LACP rate value %d.\n",
682 bond
->dev
->name
, lacp_rate
);
This page took 0.046707 seconds and 5 git commands to generate.