bonding: add ad_info attribute netlink support
[cascardo/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 <linux/reciprocal_div.h>
23 #include "bonding.h"
24
25 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
26         [IFLA_BOND_MODE]                = { .type = NLA_U8 },
27         [IFLA_BOND_ACTIVE_SLAVE]        = { .type = NLA_U32 },
28         [IFLA_BOND_MIIMON]              = { .type = NLA_U32 },
29         [IFLA_BOND_UPDELAY]             = { .type = NLA_U32 },
30         [IFLA_BOND_DOWNDELAY]           = { .type = NLA_U32 },
31         [IFLA_BOND_USE_CARRIER]         = { .type = NLA_U8 },
32         [IFLA_BOND_ARP_INTERVAL]        = { .type = NLA_U32 },
33         [IFLA_BOND_ARP_IP_TARGET]       = { .type = NLA_NESTED },
34         [IFLA_BOND_ARP_VALIDATE]        = { .type = NLA_U32 },
35         [IFLA_BOND_ARP_ALL_TARGETS]     = { .type = NLA_U32 },
36         [IFLA_BOND_PRIMARY]             = { .type = NLA_U32 },
37         [IFLA_BOND_PRIMARY_RESELECT]    = { .type = NLA_U8 },
38         [IFLA_BOND_FAIL_OVER_MAC]       = { .type = NLA_U8 },
39         [IFLA_BOND_XMIT_HASH_POLICY]    = { .type = NLA_U8 },
40         [IFLA_BOND_RESEND_IGMP]         = { .type = NLA_U32 },
41         [IFLA_BOND_NUM_PEER_NOTIF]      = { .type = NLA_U8 },
42         [IFLA_BOND_ALL_SLAVES_ACTIVE]   = { .type = NLA_U8 },
43         [IFLA_BOND_MIN_LINKS]           = { .type = NLA_U32 },
44         [IFLA_BOND_LP_INTERVAL]         = { .type = NLA_U32 },
45         [IFLA_BOND_PACKETS_PER_SLAVE]   = { .type = NLA_U32 },
46         [IFLA_BOND_AD_LACP_RATE]        = { .type = NLA_U8 },
47         [IFLA_BOND_AD_SELECT]           = { .type = NLA_U8 },
48         [IFLA_BOND_AD_INFO]             = { .type = NLA_NESTED },
49 };
50
51 static 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
62 static int bond_changelink(struct net_device *bond_dev,
63                            struct nlattr *tb[], struct nlattr *data[])
64 {
65         struct bonding *bond = netdev_priv(bond_dev);
66         int miimon = 0;
67         int err;
68
69         if (!data)
70                 return 0;
71
72         if (data[IFLA_BOND_MODE]) {
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         }
79         if (data[IFLA_BOND_ACTIVE_SLAVE]) {
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         }
95         if (data[IFLA_BOND_MIIMON]) {
96                 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
97
98                 err = bond_option_miimon_set(bond, miimon);
99                 if (err)
100                         return err;
101         }
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         }
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         }
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         }
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         }
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) {
142                         __be32 target = nla_get_be32(attr);
143                         targets[i++] = target;
144                 }
145
146                 err = bond_option_arp_ip_targets_set(bond, targets, i);
147                 if (err)
148                         return err;
149         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
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         }
274         return 0;
275 }
276
277 static 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
289 static size_t bond_get_size(const struct net_device *bond_dev)
290 {
291         return nla_total_size(sizeof(u8)) +     /* IFLA_BOND_MODE */
292                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ACTIVE_SLAVE */
293                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_MIIMON */
294                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_UPDELAY */
295                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_DOWNDELAY */
296                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_USE_CARRIER */
297                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_INTERVAL */
298                                                 /* IFLA_BOND_ARP_IP_TARGET */
299                 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
300                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_VALIDATE */
301                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_ALL_TARGETS */
302                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_PRIMARY */
303                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_PRIMARY_RESELECT */
304                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_FAIL_OVER_MAC */
305                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_XMIT_HASH_POLICY */
306                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_RESEND_IGMP */
307                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_NUM_PEER_NOTIF */
308                 nla_total_size(sizeof(u8)) +   /* IFLA_BOND_ALL_SLAVES_ACTIVE */
309                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_MIN_LINKS */
310                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_LP_INTERVAL */
311                 nla_total_size(sizeof(u32)) +  /* IFLA_BOND_PACKETS_PER_SLAVE */
312                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_LACP_RATE */
313                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_SELECT */
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*/
320                 0;
321 }
322
323 static int bond_fill_info(struct sk_buff *skb,
324                           const struct net_device *bond_dev)
325 {
326         struct bonding *bond = netdev_priv(bond_dev);
327         struct net_device *slave_dev = bond_option_active_slave_get(bond);
328         struct nlattr *targets;
329         unsigned int packets_per_slave;
330         int i, targets_added;
331
332         if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
333                 goto nla_put_failure;
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
342         if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
343                         bond->params.updelay * bond->params.miimon))
344                 goto nla_put_failure;
345
346         if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
347                         bond->params.downdelay * bond->params.miimon))
348                 goto nla_put_failure;
349
350         if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
351                 goto nla_put_failure;
352
353         if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
354                 goto nla_put_failure;
355
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]) {
363                         nla_put_be32(skb, i, bond->params.arp_targets[i]);
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
373         if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
374                 goto nla_put_failure;
375
376         if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
377                         bond->params.arp_all_targets))
378                 goto nla_put_failure;
379
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
385         if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
386                        bond->params.primary_reselect))
387                 goto nla_put_failure;
388
389         if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
390                        bond->params.fail_over_mac))
391                 goto nla_put_failure;
392
393         if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
394                        bond->params.xmit_policy))
395                 goto nla_put_failure;
396
397         if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
398                         bond->params.resend_igmp))
399                 goto nla_put_failure;
400
401         if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
402                        bond->params.num_peer_notif))
403                 goto nla_put_failure;
404
405         if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
406                        bond->params.all_slaves_active))
407                 goto nla_put_failure;
408
409         if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
410                         bond->params.min_links))
411                 goto nla_put_failure;
412
413         if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
414                         bond->params.lp_interval))
415                 goto nla_put_failure;
416
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
425         if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
426                        bond->params.lacp_fast))
427                 goto nla_put_failure;
428
429         if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
430                        bond->params.ad_select))
431                 goto nla_put_failure;
432
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
464         return 0;
465
466 nla_put_failure:
467         return -EMSGSIZE;
468 }
469
470 struct rtnl_link_ops bond_link_ops __read_mostly = {
471         .kind                   = "bond",
472         .priv_size              = sizeof(struct bonding),
473         .setup                  = bond_setup,
474         .maxtype                = IFLA_BOND_MAX,
475         .policy                 = bond_policy,
476         .validate               = bond_validate,
477         .newlink                = bond_newlink,
478         .changelink             = bond_changelink,
479         .get_size               = bond_get_size,
480         .fill_info              = bond_fill_info,
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
486 int __init bond_netlink_init(void)
487 {
488         return rtnl_link_register(&bond_link_ops);
489 }
490
491 void bond_netlink_fini(void)
492 {
493         rtnl_link_unregister(&bond_link_ops);
494 }
495
496 MODULE_ALIAS_RTNL_LINK("bond");