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>
24 int bond_get_slave(struct net_device *slave_dev, struct sk_buff *skb)
26 struct slave *slave = bond_slave_get_rtnl(slave_dev);
27 const struct aggregator *agg;
29 if (nla_put_u8(skb, IFLA_SLAVE_STATE, bond_slave_state(slave)))
32 if (nla_put_u8(skb, IFLA_SLAVE_MII_STATUS, slave->link))
35 if (nla_put_u32(skb, IFLA_SLAVE_LINK_FAILURE_COUNT,
36 slave->link_failure_count))
39 if (nla_put(skb, IFLA_SLAVE_PERM_HWADDR,
40 slave_dev->addr_len, slave->perm_hwaddr))
43 if (nla_put_u16(skb, IFLA_SLAVE_QUEUE_ID, slave->queue_id))
46 if (slave->bond->params.mode == BOND_MODE_8023AD) {
47 agg = SLAVE_AD_INFO(slave).port.aggregator;
49 if (nla_put_u16(skb, IFLA_SLAVE_AD_AGGREGATOR_ID,
50 agg->aggregator_identifier))
60 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
61 [IFLA_BOND_MODE] = { .type = NLA_U8 },
62 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
63 [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
64 [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
65 [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
66 [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
67 [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
68 [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
69 [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
70 [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
71 [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
72 [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
73 [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
74 [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
75 [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
76 [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
77 [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
78 [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
79 [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
80 [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
81 [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
82 [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
83 [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
86 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
88 if (tb[IFLA_ADDRESS]) {
89 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
91 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
92 return -EADDRNOTAVAIL;
97 static int bond_changelink(struct net_device *bond_dev,
98 struct nlattr *tb[], struct nlattr *data[])
100 struct bonding *bond = netdev_priv(bond_dev);
107 if (data[IFLA_BOND_MODE]) {
108 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
110 err = bond_option_mode_set(bond, mode);
114 if (data[IFLA_BOND_ACTIVE_SLAVE]) {
115 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
116 struct net_device *slave_dev;
121 slave_dev = __dev_get_by_index(dev_net(bond_dev),
126 err = bond_option_active_slave_set(bond, slave_dev);
130 if (data[IFLA_BOND_MIIMON]) {
131 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
133 err = bond_option_miimon_set(bond, miimon);
137 if (data[IFLA_BOND_UPDELAY]) {
138 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
140 err = bond_option_updelay_set(bond, updelay);
144 if (data[IFLA_BOND_DOWNDELAY]) {
145 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
147 err = bond_option_downdelay_set(bond, downdelay);
151 if (data[IFLA_BOND_USE_CARRIER]) {
152 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
154 err = bond_option_use_carrier_set(bond, use_carrier);
158 if (data[IFLA_BOND_ARP_INTERVAL]) {
159 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
161 if (arp_interval && miimon) {
162 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
167 err = bond_option_arp_interval_set(bond, arp_interval);
171 if (data[IFLA_BOND_ARP_IP_TARGET]) {
172 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
176 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
177 __be32 target = nla_get_be32(attr);
178 targets[i++] = target;
181 err = bond_option_arp_ip_targets_set(bond, targets, i);
185 if (data[IFLA_BOND_ARP_VALIDATE]) {
186 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
188 if (arp_validate && miimon) {
189 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
194 err = bond_option_arp_validate_set(bond, arp_validate);
198 if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
199 int arp_all_targets =
200 nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
202 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
206 if (data[IFLA_BOND_PRIMARY]) {
207 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
208 struct net_device *dev;
211 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
215 err = bond_option_primary_set(bond, primary);
219 if (data[IFLA_BOND_PRIMARY_RESELECT]) {
220 int primary_reselect =
221 nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
223 err = bond_option_primary_reselect_set(bond, primary_reselect);
227 if (data[IFLA_BOND_FAIL_OVER_MAC]) {
229 nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
231 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
235 if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
236 int xmit_hash_policy =
237 nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
239 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
243 if (data[IFLA_BOND_RESEND_IGMP]) {
245 nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
247 err = bond_option_resend_igmp_set(bond, resend_igmp);
251 if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
253 nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
255 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
259 if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
260 int all_slaves_active =
261 nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
263 err = bond_option_all_slaves_active_set(bond,
268 if (data[IFLA_BOND_MIN_LINKS]) {
270 nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
272 err = bond_option_min_links_set(bond, min_links);
276 if (data[IFLA_BOND_LP_INTERVAL]) {
278 nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
280 err = bond_option_lp_interval_set(bond, lp_interval);
284 if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
285 int packets_per_slave =
286 nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
288 err = bond_option_packets_per_slave_set(bond,
293 if (data[IFLA_BOND_AD_LACP_RATE]) {
295 nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
297 err = bond_option_lacp_rate_set(bond, lacp_rate);
301 if (data[IFLA_BOND_AD_SELECT]) {
303 nla_get_u8(data[IFLA_BOND_AD_SELECT]);
305 err = bond_option_ad_select_set(bond, ad_select);
312 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
313 struct nlattr *tb[], struct nlattr *data[])
317 err = bond_changelink(bond_dev, tb, data);
321 return register_netdevice(bond_dev);
324 static size_t bond_get_size(const struct net_device *bond_dev)
326 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
327 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
328 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
329 nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
330 nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
331 nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
332 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
333 /* IFLA_BOND_ARP_IP_TARGET */
334 nla_total_size(sizeof(struct nlattr)) +
335 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
336 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
337 nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
338 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
339 nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
340 nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
341 nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
342 nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
343 nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
344 nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
345 nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
346 nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
347 nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
348 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
349 nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
350 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
351 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
352 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
353 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
354 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
355 nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
359 static int bond_fill_info(struct sk_buff *skb,
360 const struct net_device *bond_dev)
362 struct bonding *bond = netdev_priv(bond_dev);
363 struct net_device *slave_dev = bond_option_active_slave_get(bond);
364 struct nlattr *targets;
365 unsigned int packets_per_slave;
366 int i, targets_added;
368 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
369 goto nla_put_failure;
372 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
373 goto nla_put_failure;
375 if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
376 goto nla_put_failure;
378 if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
379 bond->params.updelay * bond->params.miimon))
380 goto nla_put_failure;
382 if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
383 bond->params.downdelay * bond->params.miimon))
384 goto nla_put_failure;
386 if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
387 goto nla_put_failure;
389 if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
390 goto nla_put_failure;
392 targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
394 goto nla_put_failure;
397 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
398 if (bond->params.arp_targets[i]) {
399 nla_put_be32(skb, i, bond->params.arp_targets[i]);
405 nla_nest_end(skb, targets);
407 nla_nest_cancel(skb, targets);
409 if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
410 goto nla_put_failure;
412 if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
413 bond->params.arp_all_targets))
414 goto nla_put_failure;
416 if (bond->primary_slave &&
417 nla_put_u32(skb, IFLA_BOND_PRIMARY,
418 bond->primary_slave->dev->ifindex))
419 goto nla_put_failure;
421 if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
422 bond->params.primary_reselect))
423 goto nla_put_failure;
425 if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
426 bond->params.fail_over_mac))
427 goto nla_put_failure;
429 if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
430 bond->params.xmit_policy))
431 goto nla_put_failure;
433 if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
434 bond->params.resend_igmp))
435 goto nla_put_failure;
437 if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
438 bond->params.num_peer_notif))
439 goto nla_put_failure;
441 if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
442 bond->params.all_slaves_active))
443 goto nla_put_failure;
445 if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
446 bond->params.min_links))
447 goto nla_put_failure;
449 if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
450 bond->params.lp_interval))
451 goto nla_put_failure;
453 packets_per_slave = bond->params.packets_per_slave;
454 if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
456 goto nla_put_failure;
458 if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
459 bond->params.lacp_fast))
460 goto nla_put_failure;
462 if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
463 bond->params.ad_select))
464 goto nla_put_failure;
466 if (bond->params.mode == BOND_MODE_8023AD) {
469 if (!bond_3ad_get_active_agg_info(bond, &info)) {
472 nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
474 goto nla_put_failure;
476 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
478 goto nla_put_failure;
479 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
481 goto nla_put_failure;
482 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
484 goto nla_put_failure;
485 if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
487 goto nla_put_failure;
488 if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
489 sizeof(info.partner_system),
490 &info.partner_system))
491 goto nla_put_failure;
493 nla_nest_end(skb, nest);
503 struct rtnl_link_ops bond_link_ops __read_mostly = {
505 .priv_size = sizeof(struct bonding),
507 .maxtype = IFLA_BOND_MAX,
508 .policy = bond_policy,
509 .validate = bond_validate,
510 .newlink = bond_newlink,
511 .changelink = bond_changelink,
512 .get_size = bond_get_size,
513 .fill_info = bond_fill_info,
514 .get_num_tx_queues = bond_get_num_tx_queues,
515 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
519 int __init bond_netlink_init(void)
521 return rtnl_link_register(&bond_link_ops);
524 void bond_netlink_fini(void)
526 rtnl_link_unregister(&bond_link_ops);
529 MODULE_ALIAS_RTNL_LINK("bond");