25 #include <netlink-private/netlink.h>
26 #include <netlink/netlink.h>
27 #include <netlink/attr.h>
28 #include <netlink/utils.h>
29 #include <netlink/object.h>
30 #include <netlink/route/rtnl.h>
31 #include <netlink-private/route/link/api.h>
32 #include <netlink/route/link/macvlan.h>
34 #include <linux/if_link.h>
37 #define MACVLAN_HAS_MODE (1<<0)
38 #define MACVLAN_HAS_FLAGS (1<<1)
49 static struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX+1] = {
51 [IFLA_MACVLAN_FLAGS] = { .type =
NLA_U16 },
54 static int macvlan_alloc(
struct rtnl_link *link)
56 struct macvlan_info *mvi;
59 memset(link->l_info, 0,
sizeof(*mvi));
61 if ((mvi = calloc(1,
sizeof(*mvi))) == NULL)
70 static int macvlan_parse(
struct rtnl_link *link,
struct nlattr *data,
71 struct nlattr *xstats)
73 struct nlattr *tb[IFLA_MACVLAN_MAX+1];
74 struct macvlan_info *mvi;
77 NL_DBG(3,
"Parsing MACVLAN link info\n");
79 if ((err =
nla_parse_nested(tb, IFLA_MACVLAN_MAX, data, macvlan_policy)) < 0)
82 if ((err = macvlan_alloc(link)) < 0)
87 if (tb[IFLA_MACVLAN_MODE]) {
89 mvi->mvi_mask |= MACVLAN_HAS_MODE;
92 if (tb[IFLA_MACVLAN_FLAGS]) {
93 mvi->mvi_mode =
nla_get_u16(tb[IFLA_MACVLAN_FLAGS]);
94 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
102 static void macvlan_free(
struct rtnl_link *link)
111 struct macvlan_info *mvi = link->l_info;
113 if (mvi->mvi_mask & MACVLAN_HAS_MODE) {
114 rtnl_link_macvlan_mode2str(mvi->mvi_mode, buf,
sizeof(buf));
115 nl_dump(p,
"macvlan-mode %s", buf);
118 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS) {
119 rtnl_link_macvlan_flags2str(mvi->mvi_flags, buf,
sizeof(buf));
120 nl_dump(p,
"macvlan-flags %s", buf);
126 struct macvlan_info *vdst, *vsrc = src->l_info;
137 memcpy(vdst, vsrc,
sizeof(
struct macvlan_info));
142 static int macvlan_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
144 struct macvlan_info *mvi = link->l_info;
150 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
151 NLA_PUT_U32(msg, IFLA_MACVLAN_MODE, mvi->mvi_mode);
153 if (mvi->mvi_mask & MACVLAN_HAS_FLAGS)
154 NLA_PUT_U16(msg, IFLA_MACVLAN_FLAGS, mvi->mvi_flags);
163 static struct rtnl_link_info_ops macvlan_info_ops = {
164 .io_name =
"macvlan",
165 .io_alloc = macvlan_alloc,
166 .io_parse = macvlan_parse,
171 .io_clone = macvlan_clone,
172 .io_put_attrs = macvlan_put_attrs,
173 .io_free = macvlan_free,
177 #define IS_MACVLAN_LINK_ASSERT(link) \
178 if ((link)->l_info_ops != &macvlan_info_ops) { \
179 APPBUG("Link is not a macvlan link. set type \"macvlan\" first."); \
180 return -NLE_OPNOTSUPP; \
218 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"macvlan");
230 struct macvlan_info *mvi = link->l_info;
232 IS_MACVLAN_LINK_ASSERT(link);
234 mvi->mvi_mode = mode;
235 mvi->mvi_mask |= MACVLAN_HAS_MODE;
248 struct macvlan_info *mvi = link->l_info;
250 IS_MACVLAN_LINK_ASSERT(link);
252 if (mvi->mvi_mask & MACVLAN_HAS_MODE)
253 return mvi->mvi_mode;
267 struct macvlan_info *mvi = link->l_info;
269 IS_MACVLAN_LINK_ASSERT(link);
271 mvi->mvi_flags |= flags;
272 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
289 struct macvlan_info *mvi = link->l_info;
291 IS_MACVLAN_LINK_ASSERT(link);
293 mvi->mvi_flags &= ~flags;
294 mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
307 struct macvlan_info *mvi = link->l_info;
309 IS_MACVLAN_LINK_ASSERT(link);
311 return mvi->mvi_flags;
316 static const struct trans_tbl macvlan_flags[] = {
317 __ADD(MACVLAN_FLAG_NOPROMISC, nopromisc),
320 static const struct trans_tbl macvlan_modes[] = {
321 __ADD(MACVLAN_MODE_PRIVATE,
private),
322 __ADD(MACVLAN_MODE_VEPA, vepa),
323 __ADD(MACVLAN_MODE_BRIDGE, bridge),
324 __ADD(MACVLAN_MODE_PASSTHRU, passthru),
332 char *rtnl_link_macvlan_flags2str(
int flags,
char *buf,
size_t len)
334 return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
337 int rtnl_link_macvlan_str2flags(
const char *name)
339 return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
349 char *rtnl_link_macvlan_mode2str(
int mode,
char *buf,
size_t len)
351 return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
354 int rtnl_link_macvlan_str2mode(
const char *name)
356 return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
361 static void __init macvlan_init(
void)
366 static void __exit macvlan_exit(
void)
Dump object briefly on one line.
int rtnl_link_is_macvlan(struct rtnl_link *link)
Check if link is a MACVLAN link.
int rtnl_link_macvlan_unset_flags(struct rtnl_link *link, uint16_t flags)
Unset MACVLAN flags.
uint16_t rtnl_link_macvlan_get_flags(struct rtnl_link *link)
Get MACVLAN flags.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
Attribute validation policy.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
uint32_t rtnl_link_macvlan_get_mode(struct rtnl_link *link)
Get MACVLAN Mode.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Dump all attributes but no statistics.
int rtnl_link_macvlan_set_flags(struct rtnl_link *link, uint16_t flags)
Set MACVLAN flags.
int rtnl_link_macvlan_set_mode(struct rtnl_link *link, uint32_t mode)
Set MACVLAN MODE.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
uint16_t type
Type of attribute or NLA_UNSPEC.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
struct rtnl_link * rtnl_link_macvlan_alloc(void)
Allocate link object of type MACVLAN.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.