2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 <linux/reciprocal_div.h>
25 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
26 [IFLA_BOND_MODE] = { .type = NLA_U8 },
27 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
28 [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
29 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
30 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
31 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
32 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
33 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
34 [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
35 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
36 [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
37 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
38 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
39 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
40 [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
41 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
42 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
43 [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
44 [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
45 [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
48 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
50 if (tb[IFLA_ADDRESS]) {
51 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
53 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
54 return -EADDRNOTAVAIL;
59 static int bond_changelink(struct net_device *bond_dev,
60 struct nlattr *tb[], struct nlattr *data[])
62 struct bonding *bond = netdev_priv(bond_dev);
69 if (data[IFLA_BOND_MODE]) {
70 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
72 err = bond_option_mode_set(bond, mode);
76 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
77 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
78 struct net_device *slave_dev;
83 slave_dev = __dev_get_by_index(dev_net(bond_dev),
88 err = bond_option_active_slave_set(bond, slave_dev);
92 if (data[IFLA_BOND_MIIMON]) {
93 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
95 err = bond_option_miimon_set(bond, miimon);
99 if (data[IFLA_BOND_UPDELAY]) {
100 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
102 err = bond_option_updelay_set(bond, updelay);
106 if (data[IFLA_BOND_DOWNDELAY]) {
107 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
109 err = bond_option_downdelay_set(bond, downdelay);
113 if (data[IFLA_BOND_USE_CARRIER]) {
114 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
116 err = bond_option_use_carrier_set(bond, use_carrier);
120 if (data[IFLA_BOND_ARP_INTERVAL]) {
121 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
123 if (arp_interval && miimon) {
124 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
129 err = bond_option_arp_interval_set(bond, arp_interval);
133 if (data[IFLA_BOND_ARP_IP_TARGET]) {
134 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
138 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
139 __be32 target = nla_get_be32(attr);
140 targets[i++] = target;
143 err = bond_option_arp_ip_targets_set(bond, targets, i);
147 if (data[IFLA_BOND_ARP_VALIDATE]) {
148 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
150 if (arp_validate && miimon) {
151 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
156 err = bond_option_arp_validate_set(bond, arp_validate);
160 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
161 int arp_all_targets =
162 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
164 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
168 if (data[IFLA_BOND_PRIMARY]) {
169 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
170 struct net_device *dev;
173 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
177 err = bond_option_primary_set(bond, primary);
181 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
182 int primary_reselect =
183 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
185 err = bond_option_primary_reselect_set(bond, primary_reselect);
189 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
191 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
193 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
197 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
198 int xmit_hash_policy =
199 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
201 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
205 if (data[IFLA_BOND_RESEND_IGMP]) {
207 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
209 err = bond_option_resend_igmp_set(bond, resend_igmp);
213 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
215 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
217 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
221 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
222 int all_slaves_active =
223 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
225 err = bond_option_all_slaves_active_set(bond,
230 if (data[IFLA_BOND_MIN_LINKS]) {
232 nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
234 err = bond_option_min_links_set(bond, min_links);
238 if (data[IFLA_BOND_LP_INTERVAL]) {
240 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
242 err = bond_option_lp_interval_set(bond, lp_interval);
246 if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
247 int packets_per_slave =
248 nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
250 err = bond_option_packets_per_slave_set(bond,
258 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
259 struct nlattr *tb[], struct nlattr *data[])
263 err = bond_changelink(bond_dev, tb, data);
267 return register_netdevice(bond_dev);
270 static size_t bond_get_size(const struct net_device *bond_dev)
272 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
273 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
274 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
275 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
276 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
277 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
278 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
279 /* IFLA_BOND_ARP_IP_TARGET */
280 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
281 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
282 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
283 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
284 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
285 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
286 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
287 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
288 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
289 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
290 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
291 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
292 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
296 static int bond_fill_info(struct sk_buff *skb,
297 const struct net_device *bond_dev)
299 struct bonding *bond = netdev_priv(bond_dev);
300 struct net_device *slave_dev = bond_option_active_slave_get(bond);
301 struct nlattr *targets;
302 unsigned int packets_per_slave;
303 int i, targets_added;
305 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
306 goto nla_put_failure;
309 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
310 goto nla_put_failure;
312 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
313 goto nla_put_failure;
315 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
316 bond->params.updelay * bond->params.miimon))
317 goto nla_put_failure;
319 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
320 bond->params.downdelay * bond->params.miimon))
321 goto nla_put_failure;
323 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
324 goto nla_put_failure;
326 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
327 goto nla_put_failure;
329 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
331 goto nla_put_failure;
334 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
335 if (bond->params.arp_targets[i]) {
336 nla_put_be32(skb, i, bond->params.arp_targets[i]);
342 nla_nest_end(skb, targets);
344 nla_nest_cancel(skb, targets);
346 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
347 goto nla_put_failure;
349 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
350 bond->params.arp_all_targets))
351 goto nla_put_failure;
353 if (bond->primary_slave &&
354 nla_put_u32(skb, IFLA_BOND_PRIMARY,
355 bond->primary_slave->dev->ifindex))
356 goto nla_put_failure;
358 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
359 bond->params.primary_reselect))
360 goto nla_put_failure;
362 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
363 bond->params.fail_over_mac))
364 goto nla_put_failure;
366 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
367 bond->params.xmit_policy))
368 goto nla_put_failure;
370 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
371 bond->params.resend_igmp))
372 goto nla_put_failure;
374 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
375 bond->params.num_peer_notif))
376 goto nla_put_failure;
378 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
379 bond->params.all_slaves_active))
380 goto nla_put_failure;
382 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
383 bond->params.min_links))
384 goto nla_put_failure;
386 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
387 bond->params.lp_interval))
388 goto nla_put_failure;
390 packets_per_slave = bond->params.packets_per_slave;
391 if (packets_per_slave > 1)
392 packets_per_slave = reciprocal_value(packets_per_slave);
394 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
396 goto nla_put_failure;
404 struct rtnl_link_ops bond_link_ops __read_mostly = {
406 .priv_size = sizeof(struct bonding),
408 .maxtype = IFLA_BOND_MAX,
409 .policy = bond_policy,
410 .validate = bond_validate,
411 .newlink = bond_newlink,
412 .changelink = bond_changelink,
413 .get_size = bond_get_size,
414 .fill_info = bond_fill_info,
415 .get_num_tx_queues = bond_get_num_tx_queues,
416 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
420 int __init bond_netlink_init(void)
422 return rtnl_link_register(&bond_link_ops);
425 void bond_netlink_fini(void)
427 rtnl_link_unregister(&bond_link_ops);
430 MODULE_ALIAS_RTNL_LINK("bond");