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 },
46 [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
47 [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
48 [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
51 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
53 if (tb[IFLA_ADDRESS]) {
54 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
56 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
57 return -EADDRNOTAVAIL;
62 static int bond_changelink(struct net_device *bond_dev,
63 struct nlattr *tb[], struct nlattr *data[])
65 struct bonding *bond = netdev_priv(bond_dev);
72 if (data[IFLA_BOND_MODE]) {
73 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
75 err = bond_option_mode_set(bond, mode);
79 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
80 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
81 struct net_device *slave_dev;
86 slave_dev = __dev_get_by_index(dev_net(bond_dev),
91 err = bond_option_active_slave_set(bond, slave_dev);
95 if (data[IFLA_BOND_MIIMON]) {
96 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
98 err = bond_option_miimon_set(bond, miimon);
102 if (data[IFLA_BOND_UPDELAY]) {
103 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
105 err = bond_option_updelay_set(bond, updelay);
109 if (data[IFLA_BOND_DOWNDELAY]) {
110 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
112 err = bond_option_downdelay_set(bond, downdelay);
116 if (data[IFLA_BOND_USE_CARRIER]) {
117 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
119 err = bond_option_use_carrier_set(bond, use_carrier);
123 if (data[IFLA_BOND_ARP_INTERVAL]) {
124 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
126 if (arp_interval && miimon) {
127 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
132 err = bond_option_arp_interval_set(bond, arp_interval);
136 if (data[IFLA_BOND_ARP_IP_TARGET]) {
137 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
141 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
142 __be32 target = nla_get_be32(attr);
143 targets[i++] = target;
146 err = bond_option_arp_ip_targets_set(bond, targets, i);
150 if (data[IFLA_BOND_ARP_VALIDATE]) {
151 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
153 if (arp_validate && miimon) {
154 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
159 err = bond_option_arp_validate_set(bond, arp_validate);
163 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
164 int arp_all_targets =
165 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
167 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
171 if (data[IFLA_BOND_PRIMARY]) {
172 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
173 struct net_device *dev;
176 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
180 err = bond_option_primary_set(bond, primary);
184 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
185 int primary_reselect =
186 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
188 err = bond_option_primary_reselect_set(bond, primary_reselect);
192 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
194 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
196 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
200 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
201 int xmit_hash_policy =
202 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
204 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
208 if (data[IFLA_BOND_RESEND_IGMP]) {
210 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
212 err = bond_option_resend_igmp_set(bond, resend_igmp);
216 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
218 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
220 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
224 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
225 int all_slaves_active =
226 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
228 err = bond_option_all_slaves_active_set(bond,
233 if (data[IFLA_BOND_MIN_LINKS]) {
235 nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
237 err = bond_option_min_links_set(bond, min_links);
241 if (data[IFLA_BOND_LP_INTERVAL]) {
243 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
245 err = bond_option_lp_interval_set(bond, lp_interval);
249 if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
250 int packets_per_slave =
251 nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
253 err = bond_option_packets_per_slave_set(bond,
258 if (data[IFLA_BOND_AD_LACP_RATE]) {
260 nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
262 err = bond_option_lacp_rate_set(bond, lacp_rate);
266 if (data[IFLA_BOND_AD_SELECT]) {
268 nla_get_u8(data[IFLA_BOND_AD_SELECT]);
270 err = bond_option_ad_select_set(bond, ad_select);
277 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
278 struct nlattr *tb[], struct nlattr *data[])
282 err = bond_changelink(bond_dev, tb, data);
286 return register_netdevice(bond_dev);
289 static size_t bond_get_size(const struct net_device *bond_dev)
291 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
292 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
293 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
294 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
295 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
296 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
297 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
298 /* IFLA_BOND_ARP_IP_TARGET */
299 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
300 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
301 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
302 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
303 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
304 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
305 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
306 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
307 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
308 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
309 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
310 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
311 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
312 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
313 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
314 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
315 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
316 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
317 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
318 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
319 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
323 static int bond_fill_info(struct sk_buff *skb,
324 const struct net_device *bond_dev)
326 struct bonding *bond = netdev_priv(bond_dev);
327 struct net_device *slave_dev = bond_option_active_slave_get(bond);
328 struct nlattr *targets;
329 unsigned int packets_per_slave;
330 int i, targets_added;
332 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
333 goto nla_put_failure;
336 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
337 goto nla_put_failure;
339 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
340 goto nla_put_failure;
342 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
343 bond->params.updelay * bond->params.miimon))
344 goto nla_put_failure;
346 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
347 bond->params.downdelay * bond->params.miimon))
348 goto nla_put_failure;
350 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
351 goto nla_put_failure;
353 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
354 goto nla_put_failure;
356 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
358 goto nla_put_failure;
361 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
362 if (bond->params.arp_targets[i]) {
363 nla_put_be32(skb, i, bond->params.arp_targets[i]);
369 nla_nest_end(skb, targets);
371 nla_nest_cancel(skb, targets);
373 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
374 goto nla_put_failure;
376 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
377 bond->params.arp_all_targets))
378 goto nla_put_failure;
380 if (bond->primary_slave &&
381 nla_put_u32(skb, IFLA_BOND_PRIMARY,
382 bond->primary_slave->dev->ifindex))
383 goto nla_put_failure;
385 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
386 bond->params.primary_reselect))
387 goto nla_put_failure;
389 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
390 bond->params.fail_over_mac))
391 goto nla_put_failure;
393 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
394 bond->params.xmit_policy))
395 goto nla_put_failure;
397 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
398 bond->params.resend_igmp))
399 goto nla_put_failure;
401 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
402 bond->params.num_peer_notif))
403 goto nla_put_failure;
405 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
406 bond->params.all_slaves_active))
407 goto nla_put_failure;
409 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
410 bond->params.min_links))
411 goto nla_put_failure;
413 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
414 bond->params.lp_interval))
415 goto nla_put_failure;
417 packets_per_slave = bond->params.packets_per_slave;
418 if (packets_per_slave > 1)
419 packets_per_slave = reciprocal_value(packets_per_slave);
421 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
423 goto nla_put_failure;
425 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
426 bond->params.lacp_fast))
427 goto nla_put_failure;
429 if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
430 bond->params.ad_select))
431 goto nla_put_failure;
433 if (bond->params.mode == BOND_MODE_8023AD) {
436 if (!bond_3ad_get_active_agg_info(bond, &info)) {
439 nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
441 goto nla_put_failure;
443 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
445 goto nla_put_failure;
446 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
448 goto nla_put_failure;
449 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
451 goto nla_put_failure;
452 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
454 goto nla_put_failure;
455 if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
456 sizeof(info.partner_system),
457 &info.partner_system))
458 goto nla_put_failure;
460 nla_nest_end(skb, nest);
470 struct rtnl_link_ops bond_link_ops __read_mostly = {
472 .priv_size = sizeof(struct bonding),
474 .maxtype = IFLA_BOND_MAX,
475 .policy = bond_policy,
476 .validate = bond_validate,
477 .newlink = bond_newlink,
478 .changelink = bond_changelink,
479 .get_size = bond_get_size,
480 .fill_info = bond_fill_info,
481 .get_num_tx_queues = bond_get_num_tx_queues,
482 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
486 int __init bond_netlink_init(void)
488 return rtnl_link_register(&bond_link_ops);
491 void bond_netlink_fini(void)
493 rtnl_link_unregister(&bond_link_ops);
496 MODULE_ALIAS_RTNL_LINK("bond");