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