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
);
101 struct bond_opt_value newval
;
108 if (data
[IFLA_BOND_MODE
]) {
109 int mode
= nla_get_u8(data
[IFLA_BOND_MODE
]);
111 bond_opt_initval(&newval
, mode
);
112 err
= __bond_opt_set(bond
, BOND_OPT_MODE
, &newval
);
116 if (data
[IFLA_BOND_ACTIVE_SLAVE
]) {
117 int ifindex
= nla_get_u32(data
[IFLA_BOND_ACTIVE_SLAVE
]);
118 struct net_device
*slave_dev
;
123 slave_dev
= __dev_get_by_index(dev_net(bond_dev
),
128 err
= bond_option_active_slave_set(bond
, slave_dev
);
132 if (data
[IFLA_BOND_MIIMON
]) {
133 miimon
= nla_get_u32(data
[IFLA_BOND_MIIMON
]);
135 err
= bond_option_miimon_set(bond
, miimon
);
139 if (data
[IFLA_BOND_UPDELAY
]) {
140 int updelay
= nla_get_u32(data
[IFLA_BOND_UPDELAY
]);
142 bond_opt_initval(&newval
, updelay
);
143 err
= __bond_opt_set(bond
, BOND_OPT_UPDELAY
, &newval
);
147 if (data
[IFLA_BOND_DOWNDELAY
]) {
148 int downdelay
= nla_get_u32(data
[IFLA_BOND_DOWNDELAY
]);
150 bond_opt_initval(&newval
, downdelay
);
151 err
= __bond_opt_set(bond
, BOND_OPT_DOWNDELAY
, &newval
);
155 if (data
[IFLA_BOND_USE_CARRIER
]) {
156 int use_carrier
= nla_get_u8(data
[IFLA_BOND_USE_CARRIER
]);
158 err
= bond_option_use_carrier_set(bond
, use_carrier
);
162 if (data
[IFLA_BOND_ARP_INTERVAL
]) {
163 int arp_interval
= nla_get_u32(data
[IFLA_BOND_ARP_INTERVAL
]);
165 if (arp_interval
&& miimon
) {
166 pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
171 bond_opt_initval(&newval
, arp_interval
);
172 err
= __bond_opt_set(bond
, BOND_OPT_ARP_INTERVAL
, &newval
);
176 if (data
[IFLA_BOND_ARP_IP_TARGET
]) {
180 bond_option_arp_ip_targets_clear(bond
);
181 nla_for_each_nested(attr
, data
[IFLA_BOND_ARP_IP_TARGET
], rem
) {
182 __be32 target
= nla_get_be32(attr
);
184 bond_opt_initval(&newval
, target
);
185 err
= __bond_opt_set(bond
, BOND_OPT_ARP_TARGETS
,
191 if (i
== 0 && bond
->params
.arp_interval
)
192 pr_warn("%s: removing last arp target with arp_interval on\n",
197 if (data
[IFLA_BOND_ARP_VALIDATE
]) {
198 int arp_validate
= nla_get_u32(data
[IFLA_BOND_ARP_VALIDATE
]);
200 if (arp_validate
&& miimon
) {
201 pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
206 bond_opt_initval(&newval
, arp_validate
);
207 err
= __bond_opt_set(bond
, BOND_OPT_ARP_VALIDATE
, &newval
);
211 if (data
[IFLA_BOND_ARP_ALL_TARGETS
]) {
212 int arp_all_targets
=
213 nla_get_u32(data
[IFLA_BOND_ARP_ALL_TARGETS
]);
215 bond_opt_initval(&newval
, arp_all_targets
);
216 err
= __bond_opt_set(bond
, BOND_OPT_ARP_ALL_TARGETS
, &newval
);
220 if (data
[IFLA_BOND_PRIMARY
]) {
221 int ifindex
= nla_get_u32(data
[IFLA_BOND_PRIMARY
]);
222 struct net_device
*dev
;
225 dev
= __dev_get_by_index(dev_net(bond_dev
), ifindex
);
229 err
= bond_option_primary_set(bond
, primary
);
233 if (data
[IFLA_BOND_PRIMARY_RESELECT
]) {
234 int primary_reselect
=
235 nla_get_u8(data
[IFLA_BOND_PRIMARY_RESELECT
]);
237 err
= bond_option_primary_reselect_set(bond
, primary_reselect
);
241 if (data
[IFLA_BOND_FAIL_OVER_MAC
]) {
243 nla_get_u8(data
[IFLA_BOND_FAIL_OVER_MAC
]);
245 bond_opt_initval(&newval
, fail_over_mac
);
246 err
= __bond_opt_set(bond
, BOND_OPT_FAIL_OVER_MAC
, &newval
);
250 if (data
[IFLA_BOND_XMIT_HASH_POLICY
]) {
251 int xmit_hash_policy
=
252 nla_get_u8(data
[IFLA_BOND_XMIT_HASH_POLICY
]);
254 bond_opt_initval(&newval
, xmit_hash_policy
);
255 err
= __bond_opt_set(bond
, BOND_OPT_XMIT_HASH
, &newval
);
259 if (data
[IFLA_BOND_RESEND_IGMP
]) {
261 nla_get_u32(data
[IFLA_BOND_RESEND_IGMP
]);
263 err
= bond_option_resend_igmp_set(bond
, resend_igmp
);
267 if (data
[IFLA_BOND_NUM_PEER_NOTIF
]) {
269 nla_get_u8(data
[IFLA_BOND_NUM_PEER_NOTIF
]);
271 bond_opt_initval(&newval
, num_peer_notif
);
272 err
= __bond_opt_set(bond
, BOND_OPT_NUM_PEER_NOTIF
, &newval
);
276 if (data
[IFLA_BOND_ALL_SLAVES_ACTIVE
]) {
277 int all_slaves_active
=
278 nla_get_u8(data
[IFLA_BOND_ALL_SLAVES_ACTIVE
]);
280 err
= bond_option_all_slaves_active_set(bond
,
285 if (data
[IFLA_BOND_MIN_LINKS
]) {
287 nla_get_u32(data
[IFLA_BOND_MIN_LINKS
]);
289 bond_opt_initval(&newval
, min_links
);
290 err
= __bond_opt_set(bond
, BOND_OPT_MINLINKS
, &newval
);
294 if (data
[IFLA_BOND_LP_INTERVAL
]) {
296 nla_get_u32(data
[IFLA_BOND_LP_INTERVAL
]);
298 err
= bond_option_lp_interval_set(bond
, lp_interval
);
302 if (data
[IFLA_BOND_PACKETS_PER_SLAVE
]) {
303 int packets_per_slave
=
304 nla_get_u32(data
[IFLA_BOND_PACKETS_PER_SLAVE
]);
306 bond_opt_initval(&newval
, packets_per_slave
);
307 err
= __bond_opt_set(bond
, BOND_OPT_PACKETS_PER_SLAVE
, &newval
);
311 if (data
[IFLA_BOND_AD_LACP_RATE
]) {
313 nla_get_u8(data
[IFLA_BOND_AD_LACP_RATE
]);
315 bond_opt_initval(&newval
, lacp_rate
);
316 err
= __bond_opt_set(bond
, BOND_OPT_LACP_RATE
, &newval
);
320 if (data
[IFLA_BOND_AD_SELECT
]) {
322 nla_get_u8(data
[IFLA_BOND_AD_SELECT
]);
324 bond_opt_initval(&newval
, ad_select
);
325 err
= __bond_opt_set(bond
, BOND_OPT_AD_SELECT
, &newval
);
332 static int bond_newlink(struct net
*src_net
, struct net_device
*bond_dev
,
333 struct nlattr
*tb
[], struct nlattr
*data
[])
337 err
= bond_changelink(bond_dev
, tb
, data
);
341 return register_netdevice(bond_dev
);
344 static size_t bond_get_size(const struct net_device
*bond_dev
)
346 return nla_total_size(sizeof(u8
)) + /* IFLA_BOND_MODE */
347 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_ACTIVE_SLAVE */
348 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_MIIMON */
349 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_UPDELAY */
350 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_DOWNDELAY */
351 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_USE_CARRIER */
352 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_ARP_INTERVAL */
353 /* IFLA_BOND_ARP_IP_TARGET */
354 nla_total_size(sizeof(struct nlattr
)) +
355 nla_total_size(sizeof(u32
)) * BOND_MAX_ARP_TARGETS
+
356 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_ARP_VALIDATE */
357 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_ARP_ALL_TARGETS */
358 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_PRIMARY */
359 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_PRIMARY_RESELECT */
360 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_FAIL_OVER_MAC */
361 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_XMIT_HASH_POLICY */
362 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_RESEND_IGMP */
363 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_NUM_PEER_NOTIF */
364 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
365 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_MIN_LINKS */
366 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_LP_INTERVAL */
367 nla_total_size(sizeof(u32
)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
368 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_AD_LACP_RATE */
369 nla_total_size(sizeof(u8
)) + /* IFLA_BOND_AD_SELECT */
370 nla_total_size(sizeof(struct nlattr
)) + /* IFLA_BOND_AD_INFO */
371 nla_total_size(sizeof(u16
)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
372 nla_total_size(sizeof(u16
)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
373 nla_total_size(sizeof(u16
)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
374 nla_total_size(sizeof(u16
)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
375 nla_total_size(ETH_ALEN
) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
379 static int bond_fill_info(struct sk_buff
*skb
,
380 const struct net_device
*bond_dev
)
382 struct bonding
*bond
= netdev_priv(bond_dev
);
383 struct net_device
*slave_dev
= bond_option_active_slave_get(bond
);
384 struct nlattr
*targets
;
385 unsigned int packets_per_slave
;
386 int i
, targets_added
;
388 if (nla_put_u8(skb
, IFLA_BOND_MODE
, bond
->params
.mode
))
389 goto nla_put_failure
;
392 nla_put_u32(skb
, IFLA_BOND_ACTIVE_SLAVE
, slave_dev
->ifindex
))
393 goto nla_put_failure
;
395 if (nla_put_u32(skb
, IFLA_BOND_MIIMON
, bond
->params
.miimon
))
396 goto nla_put_failure
;
398 if (nla_put_u32(skb
, IFLA_BOND_UPDELAY
,
399 bond
->params
.updelay
* bond
->params
.miimon
))
400 goto nla_put_failure
;
402 if (nla_put_u32(skb
, IFLA_BOND_DOWNDELAY
,
403 bond
->params
.downdelay
* bond
->params
.miimon
))
404 goto nla_put_failure
;
406 if (nla_put_u8(skb
, IFLA_BOND_USE_CARRIER
, bond
->params
.use_carrier
))
407 goto nla_put_failure
;
409 if (nla_put_u32(skb
, IFLA_BOND_ARP_INTERVAL
, bond
->params
.arp_interval
))
410 goto nla_put_failure
;
412 targets
= nla_nest_start(skb
, IFLA_BOND_ARP_IP_TARGET
);
414 goto nla_put_failure
;
417 for (i
= 0; i
< BOND_MAX_ARP_TARGETS
; i
++) {
418 if (bond
->params
.arp_targets
[i
]) {
419 nla_put_be32(skb
, i
, bond
->params
.arp_targets
[i
]);
425 nla_nest_end(skb
, targets
);
427 nla_nest_cancel(skb
, targets
);
429 if (nla_put_u32(skb
, IFLA_BOND_ARP_VALIDATE
, bond
->params
.arp_validate
))
430 goto nla_put_failure
;
432 if (nla_put_u32(skb
, IFLA_BOND_ARP_ALL_TARGETS
,
433 bond
->params
.arp_all_targets
))
434 goto nla_put_failure
;
436 if (bond
->primary_slave
&&
437 nla_put_u32(skb
, IFLA_BOND_PRIMARY
,
438 bond
->primary_slave
->dev
->ifindex
))
439 goto nla_put_failure
;
441 if (nla_put_u8(skb
, IFLA_BOND_PRIMARY_RESELECT
,
442 bond
->params
.primary_reselect
))
443 goto nla_put_failure
;
445 if (nla_put_u8(skb
, IFLA_BOND_FAIL_OVER_MAC
,
446 bond
->params
.fail_over_mac
))
447 goto nla_put_failure
;
449 if (nla_put_u8(skb
, IFLA_BOND_XMIT_HASH_POLICY
,
450 bond
->params
.xmit_policy
))
451 goto nla_put_failure
;
453 if (nla_put_u32(skb
, IFLA_BOND_RESEND_IGMP
,
454 bond
->params
.resend_igmp
))
455 goto nla_put_failure
;
457 if (nla_put_u8(skb
, IFLA_BOND_NUM_PEER_NOTIF
,
458 bond
->params
.num_peer_notif
))
459 goto nla_put_failure
;
461 if (nla_put_u8(skb
, IFLA_BOND_ALL_SLAVES_ACTIVE
,
462 bond
->params
.all_slaves_active
))
463 goto nla_put_failure
;
465 if (nla_put_u32(skb
, IFLA_BOND_MIN_LINKS
,
466 bond
->params
.min_links
))
467 goto nla_put_failure
;
469 if (nla_put_u32(skb
, IFLA_BOND_LP_INTERVAL
,
470 bond
->params
.lp_interval
))
471 goto nla_put_failure
;
473 packets_per_slave
= bond
->params
.packets_per_slave
;
474 if (nla_put_u32(skb
, IFLA_BOND_PACKETS_PER_SLAVE
,
476 goto nla_put_failure
;
478 if (nla_put_u8(skb
, IFLA_BOND_AD_LACP_RATE
,
479 bond
->params
.lacp_fast
))
480 goto nla_put_failure
;
482 if (nla_put_u8(skb
, IFLA_BOND_AD_SELECT
,
483 bond
->params
.ad_select
))
484 goto nla_put_failure
;
486 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
489 if (!bond_3ad_get_active_agg_info(bond
, &info
)) {
492 nest
= nla_nest_start(skb
, IFLA_BOND_AD_INFO
);
494 goto nla_put_failure
;
496 if (nla_put_u16(skb
, IFLA_BOND_AD_INFO_AGGREGATOR
,
498 goto nla_put_failure
;
499 if (nla_put_u16(skb
, IFLA_BOND_AD_INFO_NUM_PORTS
,
501 goto nla_put_failure
;
502 if (nla_put_u16(skb
, IFLA_BOND_AD_INFO_ACTOR_KEY
,
504 goto nla_put_failure
;
505 if (nla_put_u16(skb
, IFLA_BOND_AD_INFO_PARTNER_KEY
,
507 goto nla_put_failure
;
508 if (nla_put(skb
, IFLA_BOND_AD_INFO_PARTNER_MAC
,
509 sizeof(info
.partner_system
),
510 &info
.partner_system
))
511 goto nla_put_failure
;
513 nla_nest_end(skb
, nest
);
523 struct rtnl_link_ops bond_link_ops __read_mostly
= {
525 .priv_size
= sizeof(struct bonding
),
527 .maxtype
= IFLA_BOND_MAX
,
528 .policy
= bond_policy
,
529 .validate
= bond_validate
,
530 .newlink
= bond_newlink
,
531 .changelink
= bond_changelink
,
532 .get_size
= bond_get_size
,
533 .fill_info
= bond_fill_info
,
534 .get_num_tx_queues
= bond_get_num_tx_queues
,
535 .get_num_rx_queues
= bond_get_num_tx_queues
, /* Use the same number
539 int __init
bond_netlink_init(void)
541 return rtnl_link_register(&bond_link_ops
);
544 void bond_netlink_fini(void)
546 rtnl_link_unregister(&bond_link_ops
);
549 MODULE_ALIAS_RTNL_LINK("bond");