8510c6df115b1d143e5b36928920c60139ba98da
[firefly-linux-kernel-4.4.55.git] / drivers / net / bonding / bond_options.c
1 /*
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>
5  *
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.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/errno.h>
15 #include <linux/if.h>
16 #include <linux/netdevice.h>
17 #include <linux/rwlock.h>
18 #include <linux/rcupdate.h>
19 #include "bonding.h"
20
21 static bool bond_mode_is_valid(int mode)
22 {
23         int i;
24
25         for (i = 0; bond_mode_tbl[i].modename; i++);
26
27         return mode >= 0 && mode < i;
28 }
29
30 int bond_option_mode_set(struct bonding *bond, int mode)
31 {
32         if (!bond_mode_is_valid(mode)) {
33                 pr_err("invalid mode value %d.\n", mode);
34                 return -EINVAL;
35         }
36
37         if (bond->dev->flags & IFF_UP) {
38                 pr_err("%s: unable to update mode because interface is up.\n",
39                        bond->dev->name);
40                 return -EPERM;
41         }
42
43         if (bond_has_slaves(bond)) {
44                 pr_err("%s: unable to update mode because bond has slaves.\n",
45                         bond->dev->name);
46                 return -EPERM;
47         }
48
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);
58         }
59
60         /* don't cache arp_validate between modes */
61         bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
62         bond->params.mode = mode;
63         return 0;
64 }
65
66 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
67                                                          struct slave *slave)
68 {
69         return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
70 }
71
72 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
73 {
74         struct slave *slave = rcu_dereference(bond->curr_active_slave);
75
76         return __bond_option_active_slave_get(bond, slave);
77 }
78
79 struct net_device *bond_option_active_slave_get(struct bonding *bond)
80 {
81         return __bond_option_active_slave_get(bond, bond->curr_active_slave);
82 }
83
84 int bond_option_active_slave_set(struct bonding *bond,
85                                  struct net_device *slave_dev)
86 {
87         int ret = 0;
88
89         if (slave_dev) {
90                 if (!netif_is_bond_slave(slave_dev)) {
91                         pr_err("Device %s is not bonding slave.\n",
92                                slave_dev->name);
93                         return -EINVAL;
94                 }
95
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);
99                         return -EINVAL;
100                 }
101         }
102
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);
106                 return -EINVAL;
107         }
108
109         block_netpoll_tx();
110         write_lock_bh(&bond->curr_slave_lock);
111
112         /* check to see if we are clearing active */
113         if (!slave_dev) {
114                 pr_info("%s: Clearing current active slave.\n",
115                 bond->dev->name);
116                 rcu_assign_pointer(bond->curr_active_slave, NULL);
117                 bond_select_active_slave(bond);
118         } else {
119                 struct slave *old_active = bond->curr_active_slave;
120                 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
121
122                 BUG_ON(!new_active);
123
124                 if (new_active == old_active) {
125                         /* do nothing */
126                         pr_info("%s: %s is already the current active slave.\n",
127                                 bond->dev->name, new_active->dev->name);
128                 } else {
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);
134                         } else {
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);
138                                 ret = -EINVAL;
139                         }
140                 }
141         }
142
143         write_unlock_bh(&bond->curr_slave_lock);
144         unblock_netpoll_tx();
145         return ret;
146 }
147
148 int bond_option_miimon_set(struct bonding *bond, int miimon)
149 {
150         if (miimon < 0) {
151                 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
152                        bond->dev->name, miimon, 0, INT_MAX);
153                 return -EINVAL;
154         }
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",
160                         bond->dev->name,
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",
164                         bond->dev->name,
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",
168                         bond->dev->name);
169                 bond->params.arp_interval = 0;
170                 if (bond->params.arp_validate)
171                         bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
172         }
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
177                  * is called.
178                  */
179                 if (!miimon) {
180                         cancel_delayed_work_sync(&bond->mii_work);
181                 } else {
182                         cancel_delayed_work_sync(&bond->arp_work);
183                         queue_delayed_work(bond->wq, &bond->mii_work, 0);
184                 }
185         }
186         return 0;
187 }
188
189 int bond_option_updelay_set(struct bonding *bond, int updelay)
190 {
191         if (!(bond->params.miimon)) {
192                 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
193                        bond->dev->name);
194                 return -EPERM;
195         }
196
197         if (updelay < 0) {
198                 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
199                        bond->dev->name, updelay, 0, INT_MAX);
200                 return -EINVAL;
201         } else {
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,
205                                 bond->params.miimon,
206                                 (updelay / bond->params.miimon) *
207                                 bond->params.miimon);
208                 }
209                 bond->params.updelay = updelay / bond->params.miimon;
210                 pr_info("%s: Setting up delay to %d.\n",
211                         bond->dev->name,
212                         bond->params.updelay * bond->params.miimon);
213         }
214
215         return 0;
216 }
217
218 int bond_option_downdelay_set(struct bonding *bond, int downdelay)
219 {
220         if (!(bond->params.miimon)) {
221                 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
222                        bond->dev->name);
223                 return -EPERM;
224         }
225
226         if (downdelay < 0) {
227                 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
228                        bond->dev->name, downdelay, 0, INT_MAX);
229                 return -EINVAL;
230         } else {
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,
234                                 bond->params.miimon,
235                                 (downdelay / bond->params.miimon) *
236                                 bond->params.miimon);
237                 }
238                 bond->params.downdelay = downdelay / bond->params.miimon;
239                 pr_info("%s: Setting down delay to %d.\n",
240                         bond->dev->name,
241                         bond->params.downdelay * bond->params.miimon);
242         }
243
244         return 0;
245 }
246
247 int bond_option_use_carrier_set(struct bonding *bond, int use_carrier)
248 {
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);
253         } else {
254                 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
255                         bond->dev->name, use_carrier);
256         }
257
258         return 0;
259 }
260
261 int bond_option_arp_interval_set(struct bonding *bond, int arp_interval)
262 {
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);
266                 return -EINVAL;
267         }
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);
271                 return -EINVAL;
272         }
273         pr_info("%s: Setting ARP monitoring interval to %d.\n",
274                 bond->dev->name, arp_interval);
275         bond->params.arp_interval = arp_interval;
276         if (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;
281                 }
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",
284                                 bond->dev->name);
285         }
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
290                  * is called.
291                  */
292                 if (!arp_interval) {
293                         if (bond->params.arp_validate)
294                                 bond->recv_probe = NULL;
295                         cancel_delayed_work_sync(&bond->arp_work);
296                 } else {
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);
302                 }
303         }
304
305         return 0;
306 }
307
308 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
309                                             __be32 target,
310                                             unsigned long last_rx)
311 {
312         __be32 *targets = bond->params.arp_targets;
313         struct list_head *iter;
314         struct slave *slave;
315
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;
320         }
321 }
322
323 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
324 {
325         __be32 *targets = bond->params.arp_targets;
326         int ind;
327
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);
331                 return -EINVAL;
332         }
333
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);
337                 return -EINVAL;
338         }
339
340         ind = bond_get_targets_ip(targets, 0); /* first free slot */
341         if (ind == -1) {
342                 pr_err("%s: ARP target table is full!\n",
343                        bond->dev->name);
344                 return -EINVAL;
345         }
346
347         pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target);
348
349         _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
350
351         return 0;
352 }
353
354 int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
355 {
356         int ret;
357
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);
362
363         return ret;
364 }
365
366 int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
367 {
368         __be32 *targets = bond->params.arp_targets;
369         struct list_head *iter;
370         struct slave *slave;
371         unsigned long *targets_rx;
372         int ind, i;
373
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);
377                 return -EINVAL;
378         }
379
380         ind = bond_get_targets_ip(targets, target);
381         if (ind == -1) {
382                 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
383                        bond->dev->name, &target);
384                 return -EINVAL;
385         }
386
387         if (ind == 0 && !targets[1] && bond->params.arp_interval)
388                 pr_warn("%s: removing last arp target with arp_interval on\n",
389                         bond->dev->name);
390
391         pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
392                 &target);
393
394         /* not to race with bond_arp_rcv */
395         write_lock_bh(&bond->lock);
396
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];
401                 targets_rx[i] = 0;
402         }
403         for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
404                 targets[i] = targets[i+1];
405         targets[i] = 0;
406
407         write_unlock_bh(&bond->lock);
408
409         return 0;
410 }
411
412 int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets,
413                                    int count)
414 {
415         int i, ret = 0;
416
417         /* not to race with bond_arp_rcv */
418         write_lock_bh(&bond->lock);
419
420         /* clear table */
421         for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
422                 _bond_options_arp_ip_target_set(bond, i, 0, 0);
423
424         if (count == 0 && bond->params.arp_interval)
425                 pr_warn("%s: removing last arp target with arp_interval on\n",
426                         bond->dev->name);
427
428         for (i = 0; i < count; i++) {
429                 ret = _bond_option_arp_ip_target_add(bond, targets[i]);
430                 if (ret)
431                         break;
432         }
433
434         write_unlock_bh(&bond->lock);
435         return ret;
436 }
437
438 int bond_option_arp_validate_set(struct bonding *bond, int arp_validate)
439 {
440         if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
441                 pr_err("%s: arp_validate only supported in active-backup mode.\n",
442                        bond->dev->name);
443                 return -EINVAL;
444         }
445         pr_info("%s: setting arp_validate to %s (%d).\n",
446                 bond->dev->name, arp_validate_tbl[arp_validate].modename,
447                 arp_validate);
448
449         if (bond->dev->flags & IFF_UP) {
450                 if (!arp_validate)
451                         bond->recv_probe = NULL;
452                 else if (bond->params.arp_interval)
453                         bond->recv_probe = bond_arp_rcv;
454         }
455         bond->params.arp_validate = arp_validate;
456
457         return 0;
458 }
459
460 int bond_option_arp_all_targets_set(struct bonding *bond, int arp_all_targets)
461 {
462         pr_info("%s: setting arp_all_targets to %s (%d).\n",
463                 bond->dev->name, arp_all_targets_tbl[arp_all_targets].modename,
464                 arp_all_targets);
465
466         bond->params.arp_all_targets = arp_all_targets;
467
468         return 0;
469 }
470
471 int bond_option_primary_set(struct bonding *bond, const char *primary)
472 {
473         struct list_head *iter;
474         struct slave *slave;
475         int err = 0;
476
477         block_netpoll_tx();
478         read_lock(&bond->lock);
479         write_lock_bh(&bond->curr_slave_lock);
480
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);
484                 err = -EINVAL;
485                 goto out;
486         }
487
488         /* check to see if we are clearing primary */
489         if (!strlen(primary)) {
490                 pr_info("%s: Setting primary slave to None.\n",
491                         bond->dev->name);
492                 bond->primary_slave = NULL;
493                 memset(bond->params.primary, 0, sizeof(bond->params.primary));
494                 bond_select_active_slave(bond);
495                 goto out;
496         }
497
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);
505                         goto out;
506                 }
507         }
508
509         strncpy(bond->params.primary, primary, IFNAMSIZ);
510         bond->params.primary[IFNAMSIZ - 1] = 0;
511
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);
514
515 out:
516         write_unlock_bh(&bond->curr_slave_lock);
517         read_unlock(&bond->lock);
518         unblock_netpoll_tx();
519
520         return err;
521 }
522
523 int bond_option_primary_reselect_set(struct bonding *bond, int primary_reselect)
524 {
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,
528                 primary_reselect);
529
530         block_netpoll_tx();
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();
535
536         return 0;
537 }
538
539 int bond_option_fail_over_mac_set(struct bonding *bond, int fail_over_mac)
540 {
541         if (bond_has_slaves(bond)) {
542                 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
543                        bond->dev->name);
544                 return -EPERM;
545         }
546
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,
550                 fail_over_mac);
551
552         return 0;
553 }
554
555 int bond_option_xmit_hash_policy_set(struct bonding *bond, int xmit_hash_policy)
556 {
557         bond->params.xmit_policy = xmit_hash_policy;
558         pr_info("%s: setting xmit hash policy to %s (%d).\n",
559                 bond->dev->name,
560                 xmit_hashtype_tbl[xmit_hash_policy].modename, xmit_hash_policy);
561
562         return 0;
563 }