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