blob: 9445243593fcd6fcb0ea8f29f37e4175de9a7e8d [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 },
Jiri Pirko90af2312013-10-18 17:43:38 +020036};
37
Jiri Pirko0a2a78c2013-10-18 17:43:33 +020038static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
39{
40 if (tb[IFLA_ADDRESS]) {
41 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
42 return -EINVAL;
43 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
44 return -EADDRNOTAVAIL;
45 }
46 return 0;
47}
48
Jiri Pirko90af2312013-10-18 17:43:38 +020049static int bond_changelink(struct net_device *bond_dev,
50 struct nlattr *tb[], struct nlattr *data[])
51{
52 struct bonding *bond = netdev_priv(bond_dev);
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080053 int miimon = 0;
Jiri Pirko90af2312013-10-18 17:43:38 +020054 int err;
55
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080056 if (!data)
57 return 0;
58
59 if (data[IFLA_BOND_MODE]) {
Jiri Pirko90af2312013-10-18 17:43:38 +020060 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
61
62 err = bond_option_mode_set(bond, mode);
63 if (err)
64 return err;
65 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080066 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
Jiri Pirkoec76aa42013-10-18 17:43:39 +020067 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
68 struct net_device *slave_dev;
69
70 if (ifindex == 0) {
71 slave_dev = NULL;
72 } else {
73 slave_dev = __dev_get_by_index(dev_net(bond_dev),
74 ifindex);
75 if (!slave_dev)
76 return -ENODEV;
77 }
78 err = bond_option_active_slave_set(bond, slave_dev);
79 if (err)
80 return err;
81 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080082 if (data[IFLA_BOND_MIIMON]) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -080083 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -080084
85 err = bond_option_miimon_set(bond, miimon);
86 if (err)
87 return err;
88 }
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -080089 if (data[IFLA_BOND_UPDELAY]) {
90 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
91
92 err = bond_option_updelay_set(bond, updelay);
93 if (err)
94 return err;
95 }
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -080096 if (data[IFLA_BOND_DOWNDELAY]) {
97 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
98
99 err = bond_option_downdelay_set(bond, downdelay);
100 if (err)
101 return err;
102 }
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800103 if (data[IFLA_BOND_USE_CARRIER]) {
104 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
105
106 err = bond_option_use_carrier_set(bond, use_carrier);
107 if (err)
108 return err;
109 }
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800110 if (data[IFLA_BOND_ARP_INTERVAL]) {
111 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
112
113 if (arp_interval && miimon) {
114 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
115 bond->dev->name);
116 return -EINVAL;
117 }
118
119 err = bond_option_arp_interval_set(bond, arp_interval);
120 if (err)
121 return err;
122 }
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800123 if (data[IFLA_BOND_ARP_IP_TARGET]) {
124 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
125 struct nlattr *attr;
126 int i = 0, rem;
127
128 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
Jiri Pirkoe7ef9412013-12-14 12:32:10 +0100129 __be32 target = nla_get_be32(attr);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800130 targets[i++] = target;
131 }
132
133 err = bond_option_arp_ip_targets_set(bond, targets, i);
134 if (err)
135 return err;
136 }
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800137 if (data[IFLA_BOND_ARP_VALIDATE]) {
138 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
139
140 if (arp_validate && miimon) {
141 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
142 bond->dev->name);
143 return -EINVAL;
144 }
145
146 err = bond_option_arp_validate_set(bond, arp_validate);
147 if (err)
148 return err;
149 }
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800150 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
151 int arp_all_targets =
152 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
153
154 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
155 if (err)
156 return err;
157 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800158 if (data[IFLA_BOND_PRIMARY]) {
159 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
160 struct net_device *dev;
161 char *primary = "";
162
163 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
164 if (dev)
165 primary = dev->name;
166
167 err = bond_option_primary_set(bond, primary);
168 if (err)
169 return err;
170 }
Jiri Pirko90af2312013-10-18 17:43:38 +0200171 return 0;
172}
173
174static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
175 struct nlattr *tb[], struct nlattr *data[])
176{
177 int err;
178
179 err = bond_changelink(bond_dev, tb, data);
180 if (err < 0)
181 return err;
182
183 return register_netdevice(bond_dev);
184}
185
186static size_t bond_get_size(const struct net_device *bond_dev)
187{
Dan Carpentere1398622013-11-01 13:18:44 +0300188 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800189 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
190 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800191 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800192 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800193 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800194 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800195 /* IFLA_BOND_ARP_IP_TARGET */
196 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800197 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800198 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800199 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800200 0;
Jiri Pirko90af2312013-10-18 17:43:38 +0200201}
202
203static int bond_fill_info(struct sk_buff *skb,
204 const struct net_device *bond_dev)
205{
206 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirkoec76aa42013-10-18 17:43:39 +0200207 struct net_device *slave_dev = bond_option_active_slave_get(bond);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800208 struct nlattr *targets;
209 int i, targets_added;
Jiri Pirko90af2312013-10-18 17:43:38 +0200210
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800211 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
Jiri Pirko90af2312013-10-18 17:43:38 +0200212 goto nla_put_failure;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800213
214 if (slave_dev &&
215 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
216 goto nla_put_failure;
217
218 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
219 goto nla_put_failure;
220
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800221 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
222 bond->params.updelay * bond->params.miimon))
223 goto nla_put_failure;
224
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800225 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
226 bond->params.downdelay * bond->params.miimon))
227 goto nla_put_failure;
228
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800229 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
230 goto nla_put_failure;
231
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800232 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
233 goto nla_put_failure;
234
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800235 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
236 if (!targets)
237 goto nla_put_failure;
238
239 targets_added = 0;
240 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
241 if (bond->params.arp_targets[i]) {
Jiri Pirkoe7ef9412013-12-14 12:32:10 +0100242 nla_put_be32(skb, i, bond->params.arp_targets[i]);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800243 targets_added = 1;
244 }
245 }
246
247 if (targets_added)
248 nla_nest_end(skb, targets);
249 else
250 nla_nest_cancel(skb, targets);
251
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800252 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
253 goto nla_put_failure;
254
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800255 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
256 bond->params.arp_all_targets))
257 goto nla_put_failure;
258
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800259 if (bond->primary_slave &&
260 nla_put_u32(skb, IFLA_BOND_PRIMARY,
261 bond->primary_slave->dev->ifindex))
262 goto nla_put_failure;
263
Jiri Pirko90af2312013-10-18 17:43:38 +0200264 return 0;
265
266nla_put_failure:
267 return -EMSGSIZE;
268}
269
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200270struct rtnl_link_ops bond_link_ops __read_mostly = {
271 .kind = "bond",
272 .priv_size = sizeof(struct bonding),
273 .setup = bond_setup,
Jiri Pirko90af2312013-10-18 17:43:38 +0200274 .maxtype = IFLA_BOND_MAX,
275 .policy = bond_policy,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200276 .validate = bond_validate,
Jiri Pirko90af2312013-10-18 17:43:38 +0200277 .newlink = bond_newlink,
278 .changelink = bond_changelink,
279 .get_size = bond_get_size,
280 .fill_info = bond_fill_info,
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200281 .get_num_tx_queues = bond_get_num_tx_queues,
282 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
283 as for TX queues */
284};
285
286int __init bond_netlink_init(void)
287{
288 return rtnl_link_register(&bond_link_ops);
289}
290
David S. Millera729e832013-10-19 19:09:18 -0400291void bond_netlink_fini(void)
Jiri Pirko0a2a78c2013-10-18 17:43:33 +0200292{
293 rtnl_link_unregister(&bond_link_ops);
294}
295
296MODULE_ALIAS_RTNL_LINK("bond");