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