17#include "nl-default.h"
19#include <netlink/netlink.h>
20#include <netlink/route/link/bonding.h>
25#define BOND_HAS_MODE (1 << 0)
26#define BOND_HAS_ACTIVE_SLAVE (1 << 1)
27#define BOND_HAS_HASHING_TYPE (1 << 2)
28#define BOND_HAS_MIIMON (1 << 3)
29#define BOND_HAS_MIN_LINKS (1 << 4)
40static int bond_info_alloc(
struct rtnl_link *link)
45 memset(link->l_info, 0,
sizeof(*bn));
47 bn = calloc(1,
sizeof(*bn));
68 struct nlattr *tb[IFLA_BOND_MAX + 1];
76 err = bond_info_alloc(link);
82 if (tb[IFLA_BOND_MODE]) {
84 bn->ce_mask |= BOND_HAS_MODE;
87 if (tb[IFLA_BOND_ACTIVE_SLAVE]) {
88 bn->ifindex =
nla_get_u32(tb[IFLA_BOND_ACTIVE_SLAVE]);
89 bn->ce_mask |= BOND_HAS_ACTIVE_SLAVE;
92 if (tb[IFLA_BOND_MIIMON]) {
93 bn->hashing_type =
nla_get_u32(tb[IFLA_BOND_MIIMON]);
94 bn->ce_mask |= BOND_HAS_MIIMON;
97 if (tb[IFLA_BOND_XMIT_HASH_POLICY]) {
98 bn->hashing_type =
nla_get_u8(tb[IFLA_BOND_XMIT_HASH_POLICY]);
99 bn->ce_mask |= BOND_HAS_HASHING_TYPE;
102 if (tb[IFLA_BOND_MIN_LINKS]) {
103 bn->hashing_type =
nla_get_u32(tb[IFLA_BOND_MIN_LINKS]);
104 bn->ce_mask |= BOND_HAS_MIN_LINKS;
110static void bond_info_free(
struct rtnl_link *link)
112 _nl_clear_free(&link->l_info);
117 struct bond_info *bond_dst, *bond_src = src->l_info;
120 _nl_assert(bond_src);
122 err = bond_info_alloc(dst);
126 bond_dst = dst->l_info;
128 _nl_assert(bond_dst);
130 *bond_dst = *bond_src;
135static int bond_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
143 if (bn->ce_mask & BOND_HAS_MODE)
146 if (bn->ce_mask & BOND_HAS_ACTIVE_SLAVE)
147 NLA_PUT_U32(msg, IFLA_BOND_ACTIVE_SLAVE, bn->ifindex);
149 if (bn->ce_mask & BOND_HAS_HASHING_TYPE)
150 NLA_PUT_U8(msg, IFLA_BOND_XMIT_HASH_POLICY, bn->hashing_type);
152 if (bn->ce_mask & BOND_HAS_MIIMON)
155 if (bn->ce_mask & BOND_HAS_MIN_LINKS)
156 NLA_PUT_U32(msg, IFLA_BOND_MIN_LINKS, bn->min_links);
171 uint32_t attrs = flags & LOOSE_COMPARISON ? b->ce_mask : ~0u;
174#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
175 diff |= _DIFF(BOND_HAS_MODE, a->bn_mode != b->bn_mode);
176 diff |= _DIFF(BOND_HAS_ACTIVE_SLAVE, a->ifindex != b->ifindex);
177 diff |= _DIFF(BOND_HAS_HASHING_TYPE,
178 a->hashing_type != b->hashing_type);
179 diff |= _DIFF(BOND_HAS_MIIMON, a->miimon != b->miimon);
180 diff |= _DIFF(BOND_HAS_MIN_LINKS, a->min_links != b->min_links);
188 .io_alloc = bond_info_alloc,
189 .io_parse = bond_info_parse,
190 .io_clone = bond_info_clone,
191 .io_put_attrs = bond_put_attrs,
192 .io_free = bond_info_free,
193 .io_compare = bond_info_compare,
196#define IS_BOND_INFO_ASSERT(link) \
198 if (link->l_info_ops != &bonding_info_ops) { \
199 APPBUG("Link is not a bond link. Set type \"bond\" first."); \
214 IS_BOND_INFO_ASSERT(link);
216 bn->ifindex = active_slave;
218 bn->ce_mask |= BOND_HAS_ACTIVE_SLAVE;
234 IS_BOND_INFO_ASSERT(link);
236 if (!(bn->ce_mask & BOND_HAS_ACTIVE_SLAVE))
242 *active_slave = bn->ifindex;
258 IS_BOND_INFO_ASSERT(link);
262 bn->ce_mask |= BOND_HAS_MODE;
278 IS_BOND_INFO_ASSERT(link);
280 if (!(bn->ce_mask & BOND_HAS_MODE))
302 IS_BOND_INFO_ASSERT(link);
304 bn->hashing_type = type;
306 bn->ce_mask |= BOND_HAS_HASHING_TYPE;
322 IS_BOND_INFO_ASSERT(link);
324 if (!(bn->ce_mask & BOND_HAS_HASHING_TYPE))
330 *type = bn->hashing_type;
346 IS_BOND_INFO_ASSERT(link);
350 bn->ce_mask |= BOND_HAS_MIIMON;
366 IS_BOND_INFO_ASSERT(link);
368 if (!(bn->ce_mask & BOND_HAS_MIIMON))
374 *miimon = bn->miimon;
391 IS_BOND_INFO_ASSERT(link);
393 bn->min_links = min_links;
395 bn->ce_mask |= BOND_HAS_MIN_LINKS;
411 IS_BOND_INFO_ASSERT(link);
413 if (!(bn->ce_mask & BOND_HAS_MIN_LINKS))
419 *min_links = bn->min_links;
531 err = -NLE_OPNOTSUPP;
609static void _nl_init bonding_init(
void)
614static void _nl_exit bonding_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
void nla_nest_cancel(struct nl_msg *msg, const struct nlattr *attr)
Cancel the addition of a nested attribute.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
void rtnl_link_bond_set_mode(struct rtnl_link *link, uint8_t mode)
Set bond mode.
void rtnl_link_bond_set_min_links(struct rtnl_link *link, uint32_t min_links)
Set the minimum number of member ports that must be up before marking the bond device as up.
int rtnl_link_bond_get_min_links(struct rtnl_link *link, uint32_t *min_links)
Get the minimum number of member ports that must be up before marking the bond device as up.
int rtnl_link_bond_get_miimon(struct rtnl_link *link, uint32_t *miimon)
Get MII monitoring interval.
void rtnl_link_bond_set_activeslave(struct rtnl_link *link, int active_slave)
Set active slave for bond.
int rtnl_link_bond_get_mode(struct rtnl_link *link, uint8_t *mode)
Get bond mode.
int rtnl_link_bond_get_hashing_type(struct rtnl_link *link, uint8_t *type)
Get hashing type.
int rtnl_link_bond_enslave_ifindex(struct nl_sock *sock, int master, int slave)
Add a link to a bond (enslave)
int rtnl_link_bond_add(struct nl_sock *sock, const char *name, struct rtnl_link *opts)
Create a new kernel bonding device.
struct rtnl_link * rtnl_link_bond_alloc(void)
Allocate link object of type bond.
int rtnl_link_bond_enslave(struct nl_sock *sock, struct rtnl_link *master, struct rtnl_link *slave)
Add a link to a bond (enslave)
int rtnl_link_bond_release(struct nl_sock *sock, struct rtnl_link *slave)
Release a link from a bond.
void rtnl_link_bond_set_hashing_type(struct rtnl_link *link, uint8_t type)
Set hashing type.
int rtnl_link_bond_release_ifindex(struct nl_sock *sock, int slave)
Release a link from a bond.
void rtnl_link_bond_set_miimon(struct rtnl_link *link, uint32_t miimon)
Set MII monitoring interval.
int rtnl_link_bond_get_activeslave(struct rtnl_link *link, int *active_slave)
Get active slave for bond.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
int rtnl_link_get_master(struct rtnl_link *link)
Return master link of link object.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
int rtnl_link_get_kernel(struct nl_sock *sk, int ifindex, const char *name, struct rtnl_link **result)
Get a link object directly from kernel.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
void rtnl_link_set_master(struct rtnl_link *link, int ifindex)
Set master link of link object.
void rtnl_link_set_ifindex(struct rtnl_link *link, int ifindex)
Set interface index of link object.
int rtnl_link_get_ifindex(struct rtnl_link *link)
Return interface index of link object.
int rtnl_link_change(struct nl_sock *sk, struct rtnl_link *orig, struct rtnl_link *changes, int flags)
Change link.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.
Available operations to modules implementing a link info type.
char * io_name
Name of link info type, must match name on kernel side.