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