bonding: add lp_interval attribute netlink support
[linux-block.git] / drivers / net / bonding / bond_netlink.c
CommitLineData
0a2a78c4
JP
1/*
2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
eecdaa6e 4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
0a2a78c4
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/module.h>
15#include <linux/errno.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/if_link.h>
19#include <linux/if_ether.h>
20#include <net/netlink.h>
21#include <net/rtnetlink.h>
22#include "bonding.h"
23
90af2311
JP
24static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
25 [IFLA_BOND_MODE] = { .type = NLA_U8 },
ec76aa49 26 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
eecdaa6e 27 [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
25852e29 28 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
c7461f9b 29 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
9f53e14e 30 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
06151dbc 31 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
7f28fa10 32 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
29c49482 33 [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
d5c84254 34 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
0a98a0d1 35 [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
8a41ae44 36 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
89901972 37 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
f70161c6 38 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
d8838de7 39 [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
2c9839c1 40 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
1cc0b1e3 41 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
7d101008 42 [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
8d836d09 43 [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
90af2311
JP
44};
45
0a2a78c4
JP
46static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
47{
48 if (tb[IFLA_ADDRESS]) {
49 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
50 return -EINVAL;
51 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
52 return -EADDRNOTAVAIL;
53 }
54 return 0;
55}
56
90af2311
JP
57static int bond_changelink(struct net_device *bond_dev,
58 struct nlattr *tb[], struct nlattr *data[])
59{
60 struct bonding *bond = netdev_priv(bond_dev);
06151dbc 61 int miimon = 0;
90af2311
JP
62 int err;
63
eecdaa6e 64 if (!data)
65 return 0;
66
67 if (data[IFLA_BOND_MODE]) {
90af2311
JP
68 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
69
70 err = bond_option_mode_set(bond, mode);
71 if (err)
72 return err;
73 }
eecdaa6e 74 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
ec76aa49
JP
75 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
76 struct net_device *slave_dev;
77
78 if (ifindex == 0) {
79 slave_dev = NULL;
80 } else {
81 slave_dev = __dev_get_by_index(dev_net(bond_dev),
82 ifindex);
83 if (!slave_dev)
84 return -ENODEV;
85 }
86 err = bond_option_active_slave_set(bond, slave_dev);
87 if (err)
88 return err;
89 }
eecdaa6e 90 if (data[IFLA_BOND_MIIMON]) {
06151dbc 91 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
eecdaa6e 92
93 err = bond_option_miimon_set(bond, miimon);
94 if (err)
95 return err;
96 }
25852e29 97 if (data[IFLA_BOND_UPDELAY]) {
98 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
99
100 err = bond_option_updelay_set(bond, updelay);
101 if (err)
102 return err;
103 }
c7461f9b 104 if (data[IFLA_BOND_DOWNDELAY]) {
105 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
106
107 err = bond_option_downdelay_set(bond, downdelay);
108 if (err)
109 return err;
110 }
9f53e14e 111 if (data[IFLA_BOND_USE_CARRIER]) {
112 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
113
114 err = bond_option_use_carrier_set(bond, use_carrier);
115 if (err)
116 return err;
117 }
06151dbc 118 if (data[IFLA_BOND_ARP_INTERVAL]) {
119 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
120
121 if (arp_interval && miimon) {
122 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
123 bond->dev->name);
124 return -EINVAL;
125 }
126
127 err = bond_option_arp_interval_set(bond, arp_interval);
128 if (err)
129 return err;
130 }
7f28fa10 131 if (data[IFLA_BOND_ARP_IP_TARGET]) {
132 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
133 struct nlattr *attr;
134 int i = 0, rem;
135
136 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
e7ef941d 137 __be32 target = nla_get_be32(attr);
7f28fa10 138 targets[i++] = target;
139 }
140
141 err = bond_option_arp_ip_targets_set(bond, targets, i);
142 if (err)
143 return err;
144 }
29c49482 145 if (data[IFLA_BOND_ARP_VALIDATE]) {
146 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
147
148 if (arp_validate && miimon) {
149 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
150 bond->dev->name);
151 return -EINVAL;
152 }
153
154 err = bond_option_arp_validate_set(bond, arp_validate);
155 if (err)
156 return err;
157 }
d5c84254 158 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
159 int arp_all_targets =
160 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
161
162 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
163 if (err)
164 return err;
165 }
0a98a0d1 166 if (data[IFLA_BOND_PRIMARY]) {
167 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
168 struct net_device *dev;
169 char *primary = "";
170
171 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
172 if (dev)
173 primary = dev->name;
174
175 err = bond_option_primary_set(bond, primary);
176 if (err)
177 return err;
178 }
8a41ae44 179 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
180 int primary_reselect =
181 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
182
183 err = bond_option_primary_reselect_set(bond, primary_reselect);
184 if (err)
185 return err;
186 }
89901972 187 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
188 int fail_over_mac =
189 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
190
191 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
192 if (err)
193 return err;
194 }
f70161c6 195 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
196 int xmit_hash_policy =
197 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
198
199 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
200 if (err)
201 return err;
202 }
d8838de7 203 if (data[IFLA_BOND_RESEND_IGMP]) {
204 int resend_igmp =
205 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
206
207 err = bond_option_resend_igmp_set(bond, resend_igmp);
208 if (err)
209 return err;
210 }
2c9839c1 211 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
212 int num_peer_notif =
213 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
214
215 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
216 if (err)
217 return err;
218 }
1cc0b1e3 219 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
220 int all_slaves_active =
221 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
222
223 err = bond_option_all_slaves_active_set(bond,
224 all_slaves_active);
225 if (err)
226 return err;
227 }
7d101008 228 if (data[IFLA_BOND_MIN_LINKS]) {
229 int min_links =
230 nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
231
232 err = bond_option_min_links_set(bond, min_links);
233 if (err)
234 return err;
235 }
8d836d09 236 if (data[IFLA_BOND_LP_INTERVAL]) {
237 int lp_interval =
238 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
239
240 err = bond_option_lp_interval_set(bond, lp_interval);
241 if (err)
242 return err;
243 }
90af2311
JP
244 return 0;
245}
246
247static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
248 struct nlattr *tb[], struct nlattr *data[])
249{
250 int err;
251
252 err = bond_changelink(bond_dev, tb, data);
253 if (err < 0)
254 return err;
255
256 return register_netdevice(bond_dev);
257}
258
259static size_t bond_get_size(const struct net_device *bond_dev)
260{
e139862e 261 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
eecdaa6e 262 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
263 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
25852e29 264 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
c7461f9b 265 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
9f53e14e 266 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
06151dbc 267 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
7f28fa10 268 /* IFLA_BOND_ARP_IP_TARGET */
269 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
29c49482 270 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
d5c84254 271 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
0a98a0d1 272 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
8a41ae44 273 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
89901972 274 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
f70161c6 275 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
d8838de7 276 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
2c9839c1 277 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
1cc0b1e3 278 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
7d101008 279 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
8d836d09 280 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
eecdaa6e 281 0;
90af2311
JP
282}
283
284static int bond_fill_info(struct sk_buff *skb,
285 const struct net_device *bond_dev)
286{
287 struct bonding *bond = netdev_priv(bond_dev);
ec76aa49 288 struct net_device *slave_dev = bond_option_active_slave_get(bond);
7f28fa10 289 struct nlattr *targets;
290 int i, targets_added;
90af2311 291
eecdaa6e 292 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
90af2311 293 goto nla_put_failure;
eecdaa6e 294
295 if (slave_dev &&
296 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
297 goto nla_put_failure;
298
299 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
300 goto nla_put_failure;
301
25852e29 302 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
303 bond->params.updelay * bond->params.miimon))
304 goto nla_put_failure;
305
c7461f9b 306 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
307 bond->params.downdelay * bond->params.miimon))
308 goto nla_put_failure;
309
9f53e14e 310 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
311 goto nla_put_failure;
312
06151dbc 313 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
314 goto nla_put_failure;
315
7f28fa10 316 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
317 if (!targets)
318 goto nla_put_failure;
319
320 targets_added = 0;
321 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
322 if (bond->params.arp_targets[i]) {
e7ef941d 323 nla_put_be32(skb, i, bond->params.arp_targets[i]);
7f28fa10 324 targets_added = 1;
325 }
326 }
327
328 if (targets_added)
329 nla_nest_end(skb, targets);
330 else
331 nla_nest_cancel(skb, targets);
332
29c49482 333 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
334 goto nla_put_failure;
335
d5c84254 336 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
337 bond->params.arp_all_targets))
338 goto nla_put_failure;
339
0a98a0d1 340 if (bond->primary_slave &&
341 nla_put_u32(skb, IFLA_BOND_PRIMARY,
342 bond->primary_slave->dev->ifindex))
343 goto nla_put_failure;
344
8a41ae44 345 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
346 bond->params.primary_reselect))
347 goto nla_put_failure;
348
89901972 349 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
350 bond->params.fail_over_mac))
351 goto nla_put_failure;
352
f70161c6 353 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
354 bond->params.xmit_policy))
355 goto nla_put_failure;
356
d8838de7 357 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
358 bond->params.resend_igmp))
359 goto nla_put_failure;
360
2c9839c1 361 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
362 bond->params.num_peer_notif))
363 goto nla_put_failure;
364
1cc0b1e3 365 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
366 bond->params.all_slaves_active))
367 goto nla_put_failure;
368
7d101008 369 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
370 bond->params.min_links))
371 goto nla_put_failure;
372
8d836d09 373 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
374 bond->params.lp_interval))
375 goto nla_put_failure;
376
90af2311
JP
377 return 0;
378
379nla_put_failure:
380 return -EMSGSIZE;
381}
382
0a2a78c4
JP
383struct rtnl_link_ops bond_link_ops __read_mostly = {
384 .kind = "bond",
385 .priv_size = sizeof(struct bonding),
386 .setup = bond_setup,
90af2311
JP
387 .maxtype = IFLA_BOND_MAX,
388 .policy = bond_policy,
0a2a78c4 389 .validate = bond_validate,
90af2311
JP
390 .newlink = bond_newlink,
391 .changelink = bond_changelink,
392 .get_size = bond_get_size,
393 .fill_info = bond_fill_info,
0a2a78c4
JP
394 .get_num_tx_queues = bond_get_num_tx_queues,
395 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
396 as for TX queues */
397};
398
399int __init bond_netlink_init(void)
400{
401 return rtnl_link_register(&bond_link_ops);
402}
403
a729e83a 404void bond_netlink_fini(void)
0a2a78c4
JP
405{
406 rtnl_link_unregister(&bond_link_ops);
407}
408
409MODULE_ALIAS_RTNL_LINK("bond");