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>
21 static bool bond_mode_is_valid(int mode)
25 for (i = 0; bond_mode_tbl[i].modename; i++);
27 return mode >= 0 && mode < i;
30 int bond_option_mode_set(struct bonding *bond, int mode)
32 if (!bond_mode_is_valid(mode)) {
33 pr_err("invalid mode value %d.\n", mode);
37 if (bond->dev->flags & IFF_UP) {
38 pr_err("%s: unable to update mode because interface is up.\n",
43 if (bond_has_slaves(bond)) {
44 pr_err("%s: unable to update mode because bond has slaves.\n",
49 if (BOND_NO_USES_ARP(mode) && bond->params.arp_interval) {
50 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
51 bond->dev->name, bond_mode_tbl[mode].modename);
52 /* disable arp monitoring */
53 bond->params.arp_interval = 0;
54 /* set miimon to default value */
55 bond->params.miimon = BOND_DEFAULT_MIIMON;
56 pr_info("%s: Setting MII monitoring interval to %d.\n",
57 bond->dev->name, bond->params.miimon);
60 /* don't cache arp_validate between modes */
61 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
62 bond->params.mode = mode;
66 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
69 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
72 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
74 struct slave *slave = rcu_dereference(bond->curr_active_slave);
76 return __bond_option_active_slave_get(bond, slave);
79 struct net_device *bond_option_active_slave_get(struct bonding *bond)
81 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
84 int bond_option_active_slave_set(struct bonding *bond,
85 struct net_device *slave_dev)
90 if (!netif_is_bond_slave(slave_dev)) {
91 pr_err("Device %s is not bonding slave.\n",
96 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
97 pr_err("%s: Device %s is not our slave.\n",
98 bond->dev->name, slave_dev->name);
103 if (!USES_PRIMARY(bond->params.mode)) {
104 pr_err("%s: Unable to change active slave; %s is in mode %d\n",
105 bond->dev->name, bond->dev->name, bond->params.mode);
110 write_lock_bh(&bond->curr_slave_lock);
112 /* check to see if we are clearing active */
114 pr_info("%s: Clearing current active slave.\n",
116 rcu_assign_pointer(bond->curr_active_slave, NULL);
117 bond_select_active_slave(bond);
119 struct slave *old_active = bond->curr_active_slave;
120 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
124 if (new_active == old_active) {
126 pr_info("%s: %s is already the current active slave.\n",
127 bond->dev->name, new_active->dev->name);
129 if (old_active && (new_active->link == BOND_LINK_UP) &&
130 IS_UP(new_active->dev)) {
131 pr_info("%s: Setting %s as active slave.\n",
132 bond->dev->name, new_active->dev->name);
133 bond_change_active_slave(bond, new_active);
135 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
136 bond->dev->name, new_active->dev->name,
137 new_active->dev->name);
143 write_unlock_bh(&bond->curr_slave_lock);
144 unblock_netpoll_tx();
148 int bond_option_miimon_set(struct bonding *bond, int miimon)
151 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
152 bond->dev->name, miimon, 0, INT_MAX);
155 pr_info("%s: Setting MII monitoring interval to %d.\n",
156 bond->dev->name, miimon);
157 bond->params.miimon = miimon;
158 if (bond->params.updelay)
159 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
161 bond->params.updelay * bond->params.miimon);
162 if (bond->params.downdelay)
163 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
165 bond->params.downdelay * bond->params.miimon);
166 if (miimon && bond->params.arp_interval) {
167 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
169 bond->params.arp_interval = 0;
170 if (bond->params.arp_validate)
171 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
173 if (bond->dev->flags & IFF_UP) {
174 /* If the interface is up, we may need to fire off
175 * the MII timer. If the interface is down, the
176 * timer will get fired off when the open function
180 cancel_delayed_work_sync(&bond->mii_work);
182 cancel_delayed_work_sync(&bond->arp_work);
183 queue_delayed_work(bond->wq, &bond->mii_work, 0);
189 int bond_option_updelay_set(struct bonding *bond, int updelay)
191 if (!(bond->params.miimon)) {
192 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
198 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
199 bond->dev->name, updelay, 0, INT_MAX);
202 if ((updelay % bond->params.miimon) != 0) {
203 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
204 bond->dev->name, updelay,
206 (updelay / bond->params.miimon) *
207 bond->params.miimon);
209 bond->params.updelay = updelay / bond->params.miimon;
210 pr_info("%s: Setting up delay to %d.\n",
212 bond->params.updelay * bond->params.miimon);
218 int bond_option_downdelay_set(struct bonding *bond, int downdelay)
220 if (!(bond->params.miimon)) {
221 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
227 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
228 bond->dev->name, downdelay, 0, INT_MAX);
231 if ((downdelay % bond->params.miimon) != 0) {
232 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
233 bond->dev->name, downdelay,
235 (downdelay / bond->params.miimon) *
236 bond->params.miimon);
238 bond->params.downdelay = downdelay / bond->params.miimon;
239 pr_info("%s: Setting down delay to %d.\n",
241 bond->params.downdelay * bond->params.miimon);
247 int bond_option_use_carrier_set(struct bonding *bond, int use_carrier)
249 if ((use_carrier == 0) || (use_carrier == 1)) {
250 bond->params.use_carrier = use_carrier;
251 pr_info("%s: Setting use_carrier to %d.\n",
252 bond->dev->name, use_carrier);
254 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
255 bond->dev->name, use_carrier);
261 int bond_option_arp_interval_set(struct bonding *bond, int arp_interval)
263 if (arp_interval < 0) {
264 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
265 bond->dev->name, arp_interval, INT_MAX);
268 if (BOND_NO_USES_ARP(bond->params.mode)) {
269 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
270 bond->dev->name, bond->dev->name);
273 pr_info("%s: Setting ARP monitoring interval to %d.\n",
274 bond->dev->name, arp_interval);
275 bond->params.arp_interval = arp_interval;
277 if (bond->params.miimon) {
278 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
279 bond->dev->name, bond->dev->name);
280 bond->params.miimon = 0;
282 if (!bond->params.arp_targets[0])
283 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
286 if (bond->dev->flags & IFF_UP) {
287 /* If the interface is up, we may need to fire off
288 * the ARP timer. If the interface is down, the
289 * timer will get fired off when the open function
293 if (bond->params.arp_validate)
294 bond->recv_probe = NULL;
295 cancel_delayed_work_sync(&bond->arp_work);
297 /* arp_validate can be set only in active-backup mode */
298 if (bond->params.arp_validate)
299 bond->recv_probe = bond_arp_rcv;
300 cancel_delayed_work_sync(&bond->mii_work);
301 queue_delayed_work(bond->wq, &bond->arp_work, 0);
308 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
310 unsigned long last_rx)
312 __be32 *targets = bond->params.arp_targets;
313 struct list_head *iter;
316 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
317 bond_for_each_slave(bond, slave, iter)
318 slave->target_last_arp_rx[slot] = last_rx;
319 targets[slot] = target;
323 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
325 __be32 *targets = bond->params.arp_targets;
328 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
329 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
330 bond->dev->name, &target);
334 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
335 pr_err("%s: ARP target %pI4 is already present\n",
336 bond->dev->name, &target);
340 ind = bond_get_targets_ip(targets, 0); /* first free slot */
342 pr_err("%s: ARP target table is full!\n",
347 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target);
349 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
354 int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
358 /* not to race with bond_arp_rcv */
359 write_lock_bh(&bond->lock);
360 ret = _bond_option_arp_ip_target_add(bond, target);
361 write_unlock_bh(&bond->lock);
366 int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
368 __be32 *targets = bond->params.arp_targets;
369 struct list_head *iter;
371 unsigned long *targets_rx;
374 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
375 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
376 bond->dev->name, &target);
380 ind = bond_get_targets_ip(targets, target);
382 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
383 bond->dev->name, &target);
387 if (ind == 0 && !targets[1] && bond->params.arp_interval)
388 pr_warn("%s: removing last arp target with arp_interval on\n",
391 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
394 /* not to race with bond_arp_rcv */
395 write_lock_bh(&bond->lock);
397 bond_for_each_slave(bond, slave, iter) {
398 targets_rx = slave->target_last_arp_rx;
399 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
400 targets_rx[i] = targets_rx[i+1];
403 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
404 targets[i] = targets[i+1];
407 write_unlock_bh(&bond->lock);
412 int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets,
417 /* not to race with bond_arp_rcv */
418 write_lock_bh(&bond->lock);
421 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
422 _bond_options_arp_ip_target_set(bond, i, 0, 0);
424 if (count == 0 && bond->params.arp_interval)
425 pr_warn("%s: removing last arp target with arp_interval on\n",
428 for (i = 0; i < count; i++) {
429 ret = _bond_option_arp_ip_target_add(bond, targets[i]);
434 write_unlock_bh(&bond->lock);
438 int bond_option_arp_validate_set(struct bonding *bond, int arp_validate)
440 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
441 pr_err("%s: arp_validate only supported in active-backup mode.\n",
445 pr_info("%s: setting arp_validate to %s (%d).\n",
446 bond->dev->name, arp_validate_tbl[arp_validate].modename,
449 if (bond->dev->flags & IFF_UP) {
451 bond->recv_probe = NULL;
452 else if (bond->params.arp_interval)
453 bond->recv_probe = bond_arp_rcv;
455 bond->params.arp_validate = arp_validate;
460 int bond_option_arp_all_targets_set(struct bonding *bond, int arp_all_targets)
462 pr_info("%s: setting arp_all_targets to %s (%d).\n",
463 bond->dev->name, arp_all_targets_tbl[arp_all_targets].modename,
466 bond->params.arp_all_targets = arp_all_targets;
471 int bond_option_primary_set(struct bonding *bond, const char *primary)
473 struct list_head *iter;
478 read_lock(&bond->lock);
479 write_lock_bh(&bond->curr_slave_lock);
481 if (!USES_PRIMARY(bond->params.mode)) {
482 pr_err("%s: Unable to set primary slave; %s is in mode %d\n",
483 bond->dev->name, bond->dev->name, bond->params.mode);
488 /* check to see if we are clearing primary */
489 if (!strlen(primary)) {
490 pr_info("%s: Setting primary slave to None.\n",
492 bond->primary_slave = NULL;
493 memset(bond->params.primary, 0, sizeof(bond->params.primary));
494 bond_select_active_slave(bond);
498 bond_for_each_slave(bond, slave, iter) {
499 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
500 pr_info("%s: Setting %s as primary slave.\n",
501 bond->dev->name, slave->dev->name);
502 bond->primary_slave = slave;
503 strcpy(bond->params.primary, slave->dev->name);
504 bond_select_active_slave(bond);
509 strncpy(bond->params.primary, primary, IFNAMSIZ);
510 bond->params.primary[IFNAMSIZ - 1] = 0;
512 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet.\n",
513 bond->dev->name, primary, bond->dev->name);
516 write_unlock_bh(&bond->curr_slave_lock);
517 read_unlock(&bond->lock);
518 unblock_netpoll_tx();
523 int bond_option_primary_reselect_set(struct bonding *bond, int primary_reselect)
525 bond->params.primary_reselect = primary_reselect;
526 pr_info("%s: setting primary_reselect to %s (%d).\n",
527 bond->dev->name, pri_reselect_tbl[primary_reselect].modename,
531 write_lock_bh(&bond->curr_slave_lock);
532 bond_select_active_slave(bond);
533 write_unlock_bh(&bond->curr_slave_lock);
534 unblock_netpoll_tx();
539 int bond_option_fail_over_mac_set(struct bonding *bond, int fail_over_mac)
541 if (bond_has_slaves(bond)) {
542 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
547 bond->params.fail_over_mac = fail_over_mac;
548 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
549 bond->dev->name, fail_over_mac_tbl[fail_over_mac].modename,
555 int bond_option_xmit_hash_policy_set(struct bonding *bond, int xmit_hash_policy)
557 bond->params.xmit_policy = xmit_hash_policy;
558 pr_info("%s: setting xmit hash policy to %s (%d).\n",
560 xmit_hashtype_tbl[xmit_hash_policy].modename, xmit_hash_policy);