]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/bonding/bond_options.c
bonding: add arp_interval netlink support
[mirror_ubuntu-artful-kernel.git] / drivers / net / bonding / bond_options.c
CommitLineData
72be35fe
JP
1/*
2 * drivers/net/bond/bond_options.c - bonding options
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
eecdaa6e 4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
72be35fe
JP
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>
d9e32b21
JP
16#include <linux/netdevice.h>
17#include <linux/rwlock.h>
18#include <linux/rcupdate.h>
72be35fe
JP
19#include "bonding.h"
20
21static 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
30int 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
fe9d04af 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);
72be35fe
JP
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}
d9e32b21 65
752d48b5
JP
66static 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
72struct 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
79struct 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
d9e32b21
JP
84int 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 read_lock(&bond->lock);
111 write_lock_bh(&bond->curr_slave_lock);
112
113 /* check to see if we are clearing active */
114 if (!slave_dev) {
115 pr_info("%s: Clearing current active slave.\n",
116 bond->dev->name);
117 rcu_assign_pointer(bond->curr_active_slave, NULL);
118 bond_select_active_slave(bond);
119 } else {
120 struct slave *old_active = bond->curr_active_slave;
121 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
122
123 BUG_ON(!new_active);
124
125 if (new_active == old_active) {
126 /* do nothing */
127 pr_info("%s: %s is already the current active slave.\n",
128 bond->dev->name, new_active->dev->name);
129 } else {
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);
135 } else {
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);
139 ret = -EINVAL;
140 }
141 }
142 }
143
144 write_unlock_bh(&bond->curr_slave_lock);
145 read_unlock(&bond->lock);
146 unblock_netpoll_tx();
147 return ret;
148}
eecdaa6e 149
150int bond_option_miimon_set(struct bonding *bond, int miimon)
151{
152 if (miimon < 0) {
153 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
154 bond->dev->name, miimon, 0, INT_MAX);
155 return -EINVAL;
156 }
157 pr_info("%s: Setting MII monitoring interval to %d.\n",
158 bond->dev->name, miimon);
159 bond->params.miimon = miimon;
160 if (bond->params.updelay)
161 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
162 bond->dev->name,
163 bond->params.updelay * bond->params.miimon);
164 if (bond->params.downdelay)
165 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
166 bond->dev->name,
167 bond->params.downdelay * bond->params.miimon);
168 if (miimon && bond->params.arp_interval) {
169 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
170 bond->dev->name);
171 bond->params.arp_interval = 0;
172 if (bond->params.arp_validate)
173 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
174 }
175 if (bond->dev->flags & IFF_UP) {
176 /* If the interface is up, we may need to fire off
177 * the MII timer. If the interface is down, the
178 * timer will get fired off when the open function
179 * is called.
180 */
181 if (!miimon) {
182 cancel_delayed_work_sync(&bond->mii_work);
183 } else {
184 cancel_delayed_work_sync(&bond->arp_work);
185 queue_delayed_work(bond->wq, &bond->mii_work, 0);
186 }
187 }
188 return 0;
189}
25852e29 190
191int bond_option_updelay_set(struct bonding *bond, int updelay)
192{
193 if (!(bond->params.miimon)) {
194 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
195 bond->dev->name);
196 return -EPERM;
197 }
198
199 if (updelay < 0) {
200 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
201 bond->dev->name, updelay, 0, INT_MAX);
202 return -EINVAL;
203 } else {
204 if ((updelay % bond->params.miimon) != 0) {
205 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
206 bond->dev->name, updelay,
207 bond->params.miimon,
208 (updelay / bond->params.miimon) *
209 bond->params.miimon);
210 }
211 bond->params.updelay = updelay / bond->params.miimon;
212 pr_info("%s: Setting up delay to %d.\n",
213 bond->dev->name,
214 bond->params.updelay * bond->params.miimon);
215 }
216
217 return 0;
218}
c7461f9b 219
220int bond_option_downdelay_set(struct bonding *bond, int downdelay)
221{
222 if (!(bond->params.miimon)) {
223 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
224 bond->dev->name);
225 return -EPERM;
226 }
227
228 if (downdelay < 0) {
229 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
230 bond->dev->name, downdelay, 0, INT_MAX);
231 return -EINVAL;
232 } else {
233 if ((downdelay % bond->params.miimon) != 0) {
234 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
235 bond->dev->name, downdelay,
236 bond->params.miimon,
237 (downdelay / bond->params.miimon) *
238 bond->params.miimon);
239 }
240 bond->params.downdelay = downdelay / bond->params.miimon;
241 pr_info("%s: Setting down delay to %d.\n",
242 bond->dev->name,
243 bond->params.downdelay * bond->params.miimon);
244 }
245
246 return 0;
247}
9f53e14e 248
249int bond_option_use_carrier_set(struct bonding *bond, int use_carrier)
250{
251 if ((use_carrier == 0) || (use_carrier == 1)) {
252 bond->params.use_carrier = use_carrier;
253 pr_info("%s: Setting use_carrier to %d.\n",
254 bond->dev->name, use_carrier);
255 } else {
256 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
257 bond->dev->name, use_carrier);
258 }
259
260 return 0;
261}
06151dbc 262
263int bond_option_arp_interval_set(struct bonding *bond, int arp_interval)
264{
265 if (arp_interval < 0) {
266 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
267 bond->dev->name, arp_interval, INT_MAX);
268 return -EINVAL;
269 }
270 if (BOND_NO_USES_ARP(bond->params.mode)) {
271 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
272 bond->dev->name, bond->dev->name);
273 return -EINVAL;
274 }
275 pr_info("%s: Setting ARP monitoring interval to %d.\n",
276 bond->dev->name, arp_interval);
277 bond->params.arp_interval = arp_interval;
278 if (arp_interval) {
279 if (bond->params.miimon) {
280 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
281 bond->dev->name, bond->dev->name);
282 bond->params.miimon = 0;
283 }
284 if (!bond->params.arp_targets[0])
285 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
286 bond->dev->name);
287 }
288 if (bond->dev->flags & IFF_UP) {
289 /* If the interface is up, we may need to fire off
290 * the ARP timer. If the interface is down, the
291 * timer will get fired off when the open function
292 * is called.
293 */
294 if (!arp_interval) {
295 if (bond->params.arp_validate)
296 bond->recv_probe = NULL;
297 cancel_delayed_work_sync(&bond->arp_work);
298 } else {
299 /* arp_validate can be set only in active-backup mode */
300 if (bond->params.arp_validate)
301 bond->recv_probe = bond_arp_rcv;
302 cancel_delayed_work_sync(&bond->mii_work);
303 queue_delayed_work(bond->wq, &bond->arp_work, 0);
304 }
305 }
306
307 return 0;
308}