blob: 517521569adfcd0b0cb0bb5b9a2c24cfe8a228e0 [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 },
Jiri Pirko90af2312013-10-18 17:43:38 +020042};
43
Jiri Pirko0a2a78c2013-10-18 17:43:33 +020044static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
45{
46 if (tb[IFLA_ADDRESS]) {
47 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
48 return -EINVAL;
49 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
50 return -EADDRNOTAVAIL;
51 }
52 return 0;
53}
54
Jiri Pirko90af2312013-10-18 17:43:38 +020055static int bond_changelink(struct net_device *bond_dev,
56 struct nlattr *tb[], struct nlattr *data[])
57{
58 struct bonding *bond = netdev_priv(bond_dev);
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080059 int miimon = 0;
Jiri Pirko90af2312013-10-18 17:43:38 +020060 int err;
61
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080062 if (!data)
63 return 0;
64
65 if (data[IFLA_BOND_MODE]) {
Jiri Pirko90af2312013-10-18 17:43:38 +020066 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
67
68 err = bond_option_mode_set(bond, mode);
69 if (err)
70 return err;
71 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080072 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
Jiri Pirkoec76aa42013-10-18 17:43:39 +020073 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
74 struct net_device *slave_dev;
75
76 if (ifindex == 0) {
77 slave_dev = NULL;
78 } else {
79 slave_dev = __dev_get_by_index(dev_net(bond_dev),
80 ifindex);
81 if (!slave_dev)
82 return -ENODEV;
83 }
84 err = bond_option_active_slave_set(bond, slave_dev);
85 if (err)
86 return err;
87 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080088 if (data[IFLA_BOND_MIIMON]) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080089 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080090
91 err = bond_option_miimon_set(bond, miimon);
92 if (err)
93 return err;
94 }
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -080095 if (data[IFLA_BOND_UPDELAY]) {
96 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
97
98 err = bond_option_updelay_set(bond, updelay);
99 if (err)
100 return err;
101 }
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800102 if (data[IFLA_BOND_DOWNDELAY]) {
103 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
104
105 err = bond_option_downdelay_set(bond, downdelay);
106 if (err)
107 return err;
108 }
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800109 if (data[IFLA_BOND_USE_CARRIER]) {
110 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
111
112 err = bond_option_use_carrier_set(bond, use_carrier);
113 if (err)
114 return err;
115 }
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800116 if (data[IFLA_BOND_ARP_INTERVAL]) {
117 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
118
119 if (arp_interval && miimon) {
120 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
121 bond->dev->name);
122 return -EINVAL;
123 }
124
125 err = bond_option_arp_interval_set(bond, arp_interval);
126 if (err)
127 return err;
128 }
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800129 if (data[IFLA_BOND_ARP_IP_TARGET]) {
130 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
131 struct nlattr *attr;
132 int i = 0, rem;
133
134 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
Jiri Pirkoe7ef9412013-12-14 12:32:10 +0100135 __be32 target = nla_get_be32(attr);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800136 targets[i++] = target;
137 }
138
139 err = bond_option_arp_ip_targets_set(bond, targets, i);
140 if (err)
141 return err;
142 }
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800143 if (data[IFLA_BOND_ARP_VALIDATE]) {
144 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
145
146 if (arp_validate && miimon) {
147 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
148 bond->dev->name);
149 return -EINVAL;
150 }
151
152 err = bond_option_arp_validate_set(bond, arp_validate);
153 if (err)
154 return err;
155 }
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800156 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
157 int arp_all_targets =
158 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
159
160 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
161 if (err)
162 return err;
163 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800164 if (data[IFLA_BOND_PRIMARY]) {
165 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
166 struct net_device *dev;
167 char *primary = "";
168
169 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
170 if (dev)
171 primary = dev->name;
172
173 err = bond_option_primary_set(bond, primary);
174 if (err)
175 return err;
176 }
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800177 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
178 int primary_reselect =
179 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
180
181 err = bond_option_primary_reselect_set(bond, primary_reselect);
182 if (err)
183 return err;
184 }
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800185 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
186 int fail_over_mac =
187 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
188
189 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
190 if (err)
191 return err;
192 }
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800193 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
194 int xmit_hash_policy =
195 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
196
197 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
198 if (err)
199 return err;
200 }
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -0800201 if (data[IFLA_BOND_RESEND_IGMP]) {
202 int resend_igmp =
203 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
204
205 err = bond_option_resend_igmp_set(bond, resend_igmp);
206 if (err)
207 return err;
208 }
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800209 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
210 int num_peer_notif =
211 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
212
213 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
214 if (err)
215 return err;
216 }
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -0800217 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
218 int all_slaves_active =
219 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
220
221 err = bond_option_all_slaves_active_set(bond,
222 all_slaves_active);
223 if (err)
224 return err;
225 }
Jiri Pirko90af2312013-10-18 17:43:38 +0200226 return 0;
227}
228
229static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
230 struct nlattr *tb[], struct nlattr *data[])
231{
232 int err;
233
234 err = bond_changelink(bond_dev, tb, data);
235 if (err < 0)
236 return err;
237
238 return register_netdevice(bond_dev);
239}
240
241static size_t bond_get_size(const struct net_device *bond_dev)
242{
Dan Carpentere1398622013-11-01 13:18:44 +0300243 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800244 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
245 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800246 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800247 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800248 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800249 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800250 /* IFLA_BOND_ARP_IP_TARGET */
251 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800252 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800253 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800254 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800255 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800256 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800257 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -0800258 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800259 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -0800260 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800261 0;
Jiri Pirko90af2312013-10-18 17:43:38 +0200262}
263
264static int bond_fill_info(struct sk_buff *skb,
265 const struct net_device *bond_dev)
266{
267 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirkoec76aa42013-10-18 17:43:39 +0200268 struct net_device *slave_dev = bond_option_active_slave_get(bond);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800269 struct nlattr *targets;
270 int i, targets_added;
Jiri Pirko90af2312013-10-18 17:43:38 +0200271
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800272 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
Jiri Pirko90af2312013-10-18 17:43:38 +0200273 goto nla_put_failure;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800274
275 if (slave_dev &&
276 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
277 goto nla_put_failure;
278
279 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
280 goto nla_put_failure;
281
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800282 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
283 bond->params.updelay * bond->params.miimon))
284 goto nla_put_failure;
285
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800286 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
287 bond->params.downdelay * bond->params.miimon))
288 goto nla_put_failure;
289
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800290 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
291 goto nla_put_failure;
292
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800293 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
294 goto nla_put_failure;
295
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800296 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
297 if (!targets)
298 goto nla_put_failure;
299
300 targets_added = 0;
301 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
302 if (bond->params.arp_targets[i]) {
Jiri Pirkoe7ef9412013-12-14 12:32:10 +0100303 nla_put_be32(skb, i, bond->params.arp_targets[i]);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800304 targets_added = 1;
305 }
306 }
307
308 if (targets_added)
309 nla_nest_end(skb, targets);
310 else
311 nla_nest_cancel(skb, targets);
312
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800313 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
314 goto nla_put_failure;
315
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800316 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
317 bond->params.arp_all_targets))
318 goto nla_put_failure;
319
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800320 if (bond->primary_slave &&
321 nla_put_u32(skb, IFLA_BOND_PRIMARY,
322 bond->primary_slave->dev->ifindex))
323 goto nla_put_failure;
324
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800325 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
326 bond->params.primary_reselect))
327 goto nla_put_failure;
328
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800329 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
330 bond->params.fail_over_mac))
331 goto nla_put_failure;
332
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800333 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
334 bond->params.xmit_policy))
335 goto nla_put_failure;
336
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -0800337 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
338 bond->params.resend_igmp))
339 goto nla_put_failure;
340
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800341 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
342 bond->params.num_peer_notif))
343 goto nla_put_failure;
344
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -0800345 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
346 bond->params.all_slaves_active))
347 goto nla_put_failure;
348
Jiri Pirko90af2312013-10-18 17:43:38 +0200349 return 0;
350
351nla_put_failure:
352 return -EMSGSIZE;
353}
354
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200355struct rtnl_link_ops bond_link_ops __read_mostly = {
356 .kind = "bond",
357 .priv_size = sizeof(struct bonding),
358 .setup = bond_setup,
Jiri Pirko90af2312013-10-18 17:43:38 +0200359 .maxtype = IFLA_BOND_MAX,
360 .policy = bond_policy,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200361 .validate = bond_validate,
Jiri Pirko90af2312013-10-18 17:43:38 +0200362 .newlink = bond_newlink,
363 .changelink = bond_changelink,
364 .get_size = bond_get_size,
365 .fill_info = bond_fill_info,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200366 .get_num_tx_queues = bond_get_num_tx_queues,
367 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
368 as for TX queues */
369};
370
371int __init bond_netlink_init(void)
372{
373 return rtnl_link_register(&bond_link_ops);
374}
375
David S. Millera729e832013-10-19 19:09:18 -0400376void bond_netlink_fini(void)
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200377{
378 rtnl_link_unregister(&bond_link_ops);
379}
380
381MODULE_ALIAS_RTNL_LINK("bond");