bonding: add ad_info attribute netlink support
[deliverable/linux.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>
c13ab3ff 22#include <linux/reciprocal_div.h>
0a2a78c4
JP
23#include "bonding.h"
24
90af2311
JP
25static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
26 [IFLA_BOND_MODE] = { .type = NLA_U8 },
ec76aa49 27 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
eecdaa6e 28 [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
25852e29 29 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
c7461f9b 30 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
9f53e14e 31 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
06151dbc 32 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
7f28fa10 33 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
29c49482 34 [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
d5c84254 35 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
0a98a0d1 36 [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
8a41ae44 37 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
89901972 38 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
f70161c6 39 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
d8838de7 40 [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
2c9839c1 41 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
1cc0b1e3 42 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
7d101008 43 [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
8d836d09 44 [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
c13ab3ff 45 [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
998e40bb 46 [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
ec029fac 47 [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
4ee7ac75 48 [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
90af2311
JP
49};
50
0a2a78c4
JP
51static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
52{
53 if (tb[IFLA_ADDRESS]) {
54 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
55 return -EINVAL;
56 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
57 return -EADDRNOTAVAIL;
58 }
59 return 0;
60}
61
90af2311
JP
62static int bond_changelink(struct net_device *bond_dev,
63 struct nlattr *tb[], struct nlattr *data[])
64{
65 struct bonding *bond = netdev_priv(bond_dev);
06151dbc 66 int miimon = 0;
90af2311
JP
67 int err;
68
eecdaa6e 69 if (!data)
70 return 0;
71
72 if (data[IFLA_BOND_MODE]) {
90af2311
JP
73 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
74
75 err = bond_option_mode_set(bond, mode);
76 if (err)
77 return err;
78 }
eecdaa6e 79 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
ec76aa49
JP
80 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
81 struct net_device *slave_dev;
82
83 if (ifindex == 0) {
84 slave_dev = NULL;
85 } else {
86 slave_dev = __dev_get_by_index(dev_net(bond_dev),
87 ifindex);
88 if (!slave_dev)
89 return -ENODEV;
90 }
91 err = bond_option_active_slave_set(bond, slave_dev);
92 if (err)
93 return err;
94 }
eecdaa6e 95 if (data[IFLA_BOND_MIIMON]) {
06151dbc 96 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
eecdaa6e 97
98 err = bond_option_miimon_set(bond, miimon);
99 if (err)
100 return err;
101 }
25852e29 102 if (data[IFLA_BOND_UPDELAY]) {
103 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
104
105 err = bond_option_updelay_set(bond, updelay);
106 if (err)
107 return err;
108 }
c7461f9b 109 if (data[IFLA_BOND_DOWNDELAY]) {
110 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
111
112 err = bond_option_downdelay_set(bond, downdelay);
113 if (err)
114 return err;
115 }
9f53e14e 116 if (data[IFLA_BOND_USE_CARRIER]) {
117 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
118
119 err = bond_option_use_carrier_set(bond, use_carrier);
120 if (err)
121 return err;
122 }
06151dbc 123 if (data[IFLA_BOND_ARP_INTERVAL]) {
124 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
125
126 if (arp_interval && miimon) {
127 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
128 bond->dev->name);
129 return -EINVAL;
130 }
131
132 err = bond_option_arp_interval_set(bond, arp_interval);
133 if (err)
134 return err;
135 }
7f28fa10 136 if (data[IFLA_BOND_ARP_IP_TARGET]) {
137 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
138 struct nlattr *attr;
139 int i = 0, rem;
140
141 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
e7ef941d 142 __be32 target = nla_get_be32(attr);
7f28fa10 143 targets[i++] = target;
144 }
145
146 err = bond_option_arp_ip_targets_set(bond, targets, i);
147 if (err)
148 return err;
149 }
29c49482 150 if (data[IFLA_BOND_ARP_VALIDATE]) {
151 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
152
153 if (arp_validate && miimon) {
154 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
155 bond->dev->name);
156 return -EINVAL;
157 }
158
159 err = bond_option_arp_validate_set(bond, arp_validate);
160 if (err)
161 return err;
162 }
d5c84254 163 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
164 int arp_all_targets =
165 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
166
167 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
168 if (err)
169 return err;
170 }
0a98a0d1 171 if (data[IFLA_BOND_PRIMARY]) {
172 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
173 struct net_device *dev;
174 char *primary = "";
175
176 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
177 if (dev)
178 primary = dev->name;
179
180 err = bond_option_primary_set(bond, primary);
181 if (err)
182 return err;
183 }
8a41ae44 184 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
185 int primary_reselect =
186 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
187
188 err = bond_option_primary_reselect_set(bond, primary_reselect);
189 if (err)
190 return err;
191 }
89901972 192 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
193 int fail_over_mac =
194 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
195
196 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
197 if (err)
198 return err;
199 }
f70161c6 200 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
201 int xmit_hash_policy =
202 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
203
204 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
205 if (err)
206 return err;
207 }
d8838de7 208 if (data[IFLA_BOND_RESEND_IGMP]) {
209 int resend_igmp =
210 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
211
212 err = bond_option_resend_igmp_set(bond, resend_igmp);
213 if (err)
214 return err;
215 }
2c9839c1 216 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
217 int num_peer_notif =
218 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
219
220 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
221 if (err)
222 return err;
223 }
1cc0b1e3 224 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
225 int all_slaves_active =
226 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
227
228 err = bond_option_all_slaves_active_set(bond,
229 all_slaves_active);
230 if (err)
231 return err;
232 }
7d101008 233 if (data[IFLA_BOND_MIN_LINKS]) {
234 int min_links =
235 nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
236
237 err = bond_option_min_links_set(bond, min_links);
238 if (err)
239 return err;
240 }
8d836d09 241 if (data[IFLA_BOND_LP_INTERVAL]) {
242 int lp_interval =
243 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
244
245 err = bond_option_lp_interval_set(bond, lp_interval);
246 if (err)
247 return err;
248 }
c13ab3ff 249 if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
250 int packets_per_slave =
251 nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
252
253 err = bond_option_packets_per_slave_set(bond,
254 packets_per_slave);
255 if (err)
256 return err;
257 }
998e40bb 258 if (data[IFLA_BOND_AD_LACP_RATE]) {
259 int lacp_rate =
260 nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
261
262 err = bond_option_lacp_rate_set(bond, lacp_rate);
263 if (err)
264 return err;
265 }
ec029fac 266 if (data[IFLA_BOND_AD_SELECT]) {
267 int ad_select =
268 nla_get_u8(data[IFLA_BOND_AD_SELECT]);
269
270 err = bond_option_ad_select_set(bond, ad_select);
271 if (err)
272 return err;
273 }
90af2311
JP
274 return 0;
275}
276
277static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
278 struct nlattr *tb[], struct nlattr *data[])
279{
280 int err;
281
282 err = bond_changelink(bond_dev, tb, data);
283 if (err < 0)
284 return err;
285
286 return register_netdevice(bond_dev);
287}
288
289static size_t bond_get_size(const struct net_device *bond_dev)
290{
e139862e 291 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
eecdaa6e 292 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
293 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
25852e29 294 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
c7461f9b 295 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
9f53e14e 296 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
06151dbc 297 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
7f28fa10 298 /* IFLA_BOND_ARP_IP_TARGET */
299 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
29c49482 300 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
d5c84254 301 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
0a98a0d1 302 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
8a41ae44 303 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
89901972 304 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
f70161c6 305 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
d8838de7 306 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
2c9839c1 307 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
1cc0b1e3 308 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
7d101008 309 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
8d836d09 310 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
c13ab3ff 311 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
998e40bb 312 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
ec029fac 313 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
4ee7ac75 314 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
315 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
316 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
317 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
318 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
319 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
eecdaa6e 320 0;
90af2311
JP
321}
322
323static int bond_fill_info(struct sk_buff *skb,
324 const struct net_device *bond_dev)
325{
326 struct bonding *bond = netdev_priv(bond_dev);
ec76aa49 327 struct net_device *slave_dev = bond_option_active_slave_get(bond);
7f28fa10 328 struct nlattr *targets;
c13ab3ff 329 unsigned int packets_per_slave;
7f28fa10 330 int i, targets_added;
90af2311 331
eecdaa6e 332 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
90af2311 333 goto nla_put_failure;
eecdaa6e 334
335 if (slave_dev &&
336 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
337 goto nla_put_failure;
338
339 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
340 goto nla_put_failure;
341
25852e29 342 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
343 bond->params.updelay * bond->params.miimon))
344 goto nla_put_failure;
345
c7461f9b 346 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
347 bond->params.downdelay * bond->params.miimon))
348 goto nla_put_failure;
349
9f53e14e 350 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
351 goto nla_put_failure;
352
06151dbc 353 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
354 goto nla_put_failure;
355
7f28fa10 356 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
357 if (!targets)
358 goto nla_put_failure;
359
360 targets_added = 0;
361 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
362 if (bond->params.arp_targets[i]) {
e7ef941d 363 nla_put_be32(skb, i, bond->params.arp_targets[i]);
7f28fa10 364 targets_added = 1;
365 }
366 }
367
368 if (targets_added)
369 nla_nest_end(skb, targets);
370 else
371 nla_nest_cancel(skb, targets);
372
29c49482 373 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
374 goto nla_put_failure;
375
d5c84254 376 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
377 bond->params.arp_all_targets))
378 goto nla_put_failure;
379
0a98a0d1 380 if (bond->primary_slave &&
381 nla_put_u32(skb, IFLA_BOND_PRIMARY,
382 bond->primary_slave->dev->ifindex))
383 goto nla_put_failure;
384
8a41ae44 385 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
386 bond->params.primary_reselect))
387 goto nla_put_failure;
388
89901972 389 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
390 bond->params.fail_over_mac))
391 goto nla_put_failure;
392
f70161c6 393 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
394 bond->params.xmit_policy))
395 goto nla_put_failure;
396
d8838de7 397 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
398 bond->params.resend_igmp))
399 goto nla_put_failure;
400
2c9839c1 401 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
402 bond->params.num_peer_notif))
403 goto nla_put_failure;
404
1cc0b1e3 405 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
406 bond->params.all_slaves_active))
407 goto nla_put_failure;
408
7d101008 409 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
410 bond->params.min_links))
411 goto nla_put_failure;
412
8d836d09 413 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
414 bond->params.lp_interval))
415 goto nla_put_failure;
416
c13ab3ff 417 packets_per_slave = bond->params.packets_per_slave;
418 if (packets_per_slave > 1)
419 packets_per_slave = reciprocal_value(packets_per_slave);
420
421 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
422 packets_per_slave))
423 goto nla_put_failure;
424
998e40bb 425 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
426 bond->params.lacp_fast))
427 goto nla_put_failure;
428
ec029fac 429 if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
430 bond->params.ad_select))
431 goto nla_put_failure;
432
4ee7ac75 433 if (bond->params.mode == BOND_MODE_8023AD) {
434 struct ad_info info;
435
436 if (!bond_3ad_get_active_agg_info(bond, &info)) {
437 struct nlattr *nest;
438
439 nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
440 if (!nest)
441 goto nla_put_failure;
442
443 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
444 info.aggregator_id))
445 goto nla_put_failure;
446 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
447 info.ports))
448 goto nla_put_failure;
449 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
450 info.actor_key))
451 goto nla_put_failure;
452 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
453 info.partner_key))
454 goto nla_put_failure;
455 if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
456 sizeof(info.partner_system),
457 &info.partner_system))
458 goto nla_put_failure;
459
460 nla_nest_end(skb, nest);
461 }
462 }
463
90af2311
JP
464 return 0;
465
466nla_put_failure:
467 return -EMSGSIZE;
468}
469
0a2a78c4
JP
470struct rtnl_link_ops bond_link_ops __read_mostly = {
471 .kind = "bond",
472 .priv_size = sizeof(struct bonding),
473 .setup = bond_setup,
90af2311
JP
474 .maxtype = IFLA_BOND_MAX,
475 .policy = bond_policy,
0a2a78c4 476 .validate = bond_validate,
90af2311
JP
477 .newlink = bond_newlink,
478 .changelink = bond_changelink,
479 .get_size = bond_get_size,
480 .fill_info = bond_fill_info,
0a2a78c4
JP
481 .get_num_tx_queues = bond_get_num_tx_queues,
482 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
483 as for TX queues */
484};
485
486int __init bond_netlink_init(void)
487{
488 return rtnl_link_register(&bond_link_ops);
489}
490
a729e83a 491void bond_netlink_fini(void)
0a2a78c4
JP
492{
493 rtnl_link_unregister(&bond_link_ops);
494}
495
496MODULE_ALIAS_RTNL_LINK("bond");
This page took 0.064547 seconds and 5 git commands to generate.