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