blob: ac9a10c90a507bb194a12b16a6450ddacce8e534 [file] [log] [blame]
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02001/*
2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -08004 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005 *
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
Jiri Pirko90af2312013-10-18 17:43:38 +020024static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
25 [IFLA_BOND_MODE] = { .type = NLA_U8 },
Jiri Pirkoec76aa42013-10-18 17:43:39 +020026 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080027 [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -080028 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -080029 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -080030 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080031 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -080032 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -080033 [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -080034 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -080035 [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -080036 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -080037 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -080038 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -080039 [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -080040 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -080041 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -080042 [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
Jiri Pirko90af2312013-10-18 17:43:38 +020043};
44
Jiri Pirko0a2a78c2013-10-18 17:43:33 +020045static 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
Jiri Pirko90af2312013-10-18 17:43:38 +020056static int bond_changelink(struct net_device *bond_dev,
57 struct nlattr *tb[], struct nlattr *data[])
58{
59 struct bonding *bond = netdev_priv(bond_dev);
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080060 int miimon = 0;
Jiri Pirko90af2312013-10-18 17:43:38 +020061 int err;
62
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080063 if (!data)
64 return 0;
65
66 if (data[IFLA_BOND_MODE]) {
Jiri Pirko90af2312013-10-18 17:43:38 +020067 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 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080073 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
Jiri Pirkoec76aa42013-10-18 17:43:39 +020074 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 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080089 if (data[IFLA_BOND_MIIMON]) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080090 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080091
92 err = bond_option_miimon_set(bond, miimon);
93 if (err)
94 return err;
95 }
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -080096 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 }
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800103 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 }
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800110 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 }
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800117 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 }
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800130 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) {
Jiri Pirkoe7ef9412013-12-14 12:32:10 +0100136 __be32 target = nla_get_be32(attr);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800137 targets[i++] = target;
138 }
139
140 err = bond_option_arp_ip_targets_set(bond, targets, i);
141 if (err)
142 return err;
143 }
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800144 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 }
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800157 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 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800165 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 }
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800178 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 }
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800186 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 }
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800194 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 }
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -0800202 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 }
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800210 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 }
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -0800218 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 }
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -0800227 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 }
Jiri Pirko90af2312013-10-18 17:43:38 +0200235 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{
Dan Carpentere1398622013-11-01 13:18:44 +0300252 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800253 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
254 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800255 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800256 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800257 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800258 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800259 /* IFLA_BOND_ARP_IP_TARGET */
260 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800261 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800262 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800263 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800264 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800265 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800266 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -0800267 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800268 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -0800269 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -0800270 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800271 0;
Jiri Pirko90af2312013-10-18 17:43:38 +0200272}
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);
Jiri Pirkoec76aa42013-10-18 17:43:39 +0200278 struct net_device *slave_dev = bond_option_active_slave_get(bond);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800279 struct nlattr *targets;
280 int i, targets_added;
Jiri Pirko90af2312013-10-18 17:43:38 +0200281
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800282 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
Jiri Pirko90af2312013-10-18 17:43:38 +0200283 goto nla_put_failure;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800284
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
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800292 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
293 bond->params.updelay * bond->params.miimon))
294 goto nla_put_failure;
295
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800296 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
297 bond->params.downdelay * bond->params.miimon))
298 goto nla_put_failure;
299
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800300 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
301 goto nla_put_failure;
302
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800303 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
304 goto nla_put_failure;
305
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800306 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]) {
Jiri Pirkoe7ef9412013-12-14 12:32:10 +0100313 nla_put_be32(skb, i, bond->params.arp_targets[i]);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800314 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
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800323 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
324 goto nla_put_failure;
325
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800326 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
327 bond->params.arp_all_targets))
328 goto nla_put_failure;
329
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800330 if (bond->primary_slave &&
331 nla_put_u32(skb, IFLA_BOND_PRIMARY,
332 bond->primary_slave->dev->ifindex))
333 goto nla_put_failure;
334
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800335 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
336 bond->params.primary_reselect))
337 goto nla_put_failure;
338
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800339 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
340 bond->params.fail_over_mac))
341 goto nla_put_failure;
342
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800343 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
344 bond->params.xmit_policy))
345 goto nla_put_failure;
346
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -0800347 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
348 bond->params.resend_igmp))
349 goto nla_put_failure;
350
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800351 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
352 bond->params.num_peer_notif))
353 goto nla_put_failure;
354
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -0800355 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
356 bond->params.all_slaves_active))
357 goto nla_put_failure;
358
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -0800359 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
360 bond->params.min_links))
361 goto nla_put_failure;
362
Jiri Pirko90af2312013-10-18 17:43:38 +0200363 return 0;
364
365nla_put_failure:
366 return -EMSGSIZE;
367}
368
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200369struct rtnl_link_ops bond_link_ops __read_mostly = {
370 .kind = "bond",
371 .priv_size = sizeof(struct bonding),
372 .setup = bond_setup,
Jiri Pirko90af2312013-10-18 17:43:38 +0200373 .maxtype = IFLA_BOND_MAX,
374 .policy = bond_policy,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200375 .validate = bond_validate,
Jiri Pirko90af2312013-10-18 17:43:38 +0200376 .newlink = bond_newlink,
377 .changelink = bond_changelink,
378 .get_size = bond_get_size,
379 .fill_info = bond_fill_info,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200380 .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
David S. Millera729e832013-10-19 19:09:18 -0400390void bond_netlink_fini(void)
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200391{
392 rtnl_link_unregister(&bond_link_ops);
393}
394
395MODULE_ALIAS_RTNL_LINK("bond");