summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2015-10-05 10:55:38 +0200
committerThomas Haller <thaller@redhat.com>2015-11-19 17:20:28 +0100
commit3f231213c7c586b5a5e8ce6b1ea9c1d3b24d74c0 (patch)
treec617e155435b2d95abf27eb59070c105975fd242
parente6b39c9e2355febfe16555b738ccdcf945497245 (diff)
downloadlibnl-3f231213c7c586b5a5e8ce6b1ea9c1d3b24d74c0.tar.gz
route/link: add macvtap support
This adds support for MAC-VLAN based tap interfaces (macvtap). http://lists.infradead.org/pipermail/libnl/2015-October/001976.html Signed-off-by: Beniamino Galvani <bgalvani@redhat.com> Signed-off-by: Thomas Haller <thaller@redhat.com>
-rw-r--r--doc/route.txt57
-rw-r--r--include/Makefile.am1
-rw-r--r--include/netlink/route/link/macvtap.h46
-rw-r--r--lib/route/link/macvlan.c201
-rw-r--r--libnl-route-3.sym12
-rw-r--r--tests/.gitignore1
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/test-create-macvtap.c50
8 files changed, 357 insertions, 12 deletions
diff --git a/doc/route.txt b/doc/route.txt
index d9f88e13..e1e15f1f 100644
--- a/doc/route.txt
+++ b/doc/route.txt
@@ -763,6 +763,63 @@ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0)
rtnl_link_put(link);
-----
+[[link_macvtap]]
+==== MACVTAP
+
+[source,c]
+-----
+extern struct rtnl_link *rtnl_link_macvtap_alloc(void);
+
+extern int rtnl_link_is_macvtap(struct rtnl_link *);
+
+extern char * rtnl_link_macvtap_mode2str(int, char *, size_t);
+extern int rtnl_link_macvtap_str2mode(const char *);
+
+extern char * rtnl_link_macvtap_flags2str(int, char *, size_t);
+extern int rtnl_link_macvtap_str2flags(const char *);
+
+extern int rtnl_link_macvtap_set_mode(struct rtnl_link *,
+ uint32_t);
+extern uint32_t rtnl_link_macvtap_get_mode(struct rtnl_link *);
+
+extern int rtnl_link_macvtap_set_flags(struct rtnl_link *,
+ uint16_t);
+extern int rtnl_link_macvtap_unset_flags(struct rtnl_link *,
+ uint16_t);
+extern uint16_t rtnl_link_macvtap_get_flags(struct rtnl_link *);
+-----
+
+.Example: Add a MACVTAP device
+[source,c]
+-----
+struct rtnl_link *link;
+int master_index;
+struct nl_addr* addr;
+
+/* lookup interface index of eth0 */
+if (!(master_index = rtnl_link_name2i(link_cache, "eth0")))
+ /* error */
+
+/* allocate new link object of type macvtap */
+link = rtnl_link_macvtap_alloc();
+
+/* set eth0 to be our master device */
+rtnl_link_set_link(link, master_index);
+
+/* set address of virtual interface */
+addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN);
+rtnl_link_set_addr(link, addr);
+nl_addr_put(addr);
+
+/* set mode of virtual interface */
+rtnl_link_macvtap_set_mode(link, rtnl_link_macvtap_str2mode("bridge"));
+
+if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0)
+ /* error */
+
+rtnl_link_put(link);
+-----
+
[[link_vxlan]]
==== VXLAN
diff --git a/include/Makefile.am b/include/Makefile.am
index 84487a47..5fa2dff7 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -52,6 +52,7 @@ nobase_libnlinclude_HEADERS = \
netlink/route/link/inet6.h \
netlink/route/link/info-api.h \
netlink/route/link/macvlan.h \
+ netlink/route/link/macvtap.h \
netlink/route/link/vlan.h \
netlink/route/link/vxlan.h \
netlink/route/link/veth.h \
diff --git a/include/netlink/route/link/macvtap.h b/include/netlink/route/link/macvtap.h
new file mode 100644
index 00000000..affcddc7
--- /dev/null
+++ b/include/netlink/route/link/macvtap.h
@@ -0,0 +1,46 @@
+/*
+ * netlink/route/link/macvtap.h MACVTAP interface
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Copyright (c) 2015 Beniamino Galvani <bgalvani@redhat.com>
+ */
+
+#ifndef NETLINK_LINK_MACVTAP_H_
+#define NETLINK_LINK_MACVTAP_H_
+
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern struct rtnl_link *rtnl_link_macvtap_alloc(void);
+
+extern int rtnl_link_is_macvtap(struct rtnl_link *);
+
+extern char * rtnl_link_macvtap_mode2str(int, char *, size_t);
+extern int rtnl_link_macvtap_str2mode(const char *);
+
+extern char * rtnl_link_macvtap_flags2str(int, char *, size_t);
+extern int rtnl_link_macvtap_str2flags(const char *);
+
+extern int rtnl_link_macvtap_set_mode(struct rtnl_link *,
+ uint32_t);
+extern uint32_t rtnl_link_macvtap_get_mode(struct rtnl_link *);
+
+extern int rtnl_link_macvtap_set_flags(struct rtnl_link *,
+ uint16_t);
+extern int rtnl_link_macvtap_unset_flags(struct rtnl_link *,
+ uint16_t);
+extern uint16_t rtnl_link_macvtap_get_flags(struct rtnl_link *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/lib/route/link/macvlan.c b/lib/route/link/macvlan.c
index 4e61d92d..3553cbe5 100644
--- a/lib/route/link/macvlan.c
+++ b/lib/route/link/macvlan.c
@@ -11,13 +11,14 @@
/**
* @ingroup link
- * @defgroup macvlan MACVLAN
+ * @defgroup macvlan MACVLAN/MACVTAP
* MAC-based Virtual LAN link module
*
* @details
* \b Link Type Name: "macvlan"
*
* @route_doc{link_macvlan, MACVLAN Documentation}
+ * @route_doc{link_macvtap, MACVTAP Documentation}
*
* @{
*/
@@ -30,6 +31,7 @@
#include <netlink/route/rtnl.h>
#include <netlink-private/route/link/api.h>
#include <netlink/route/link/macvlan.h>
+#include <netlink/route/link/macvtap.h>
#include <linux/if_link.h>
@@ -74,7 +76,7 @@ static int macvlan_parse(struct rtnl_link *link, struct nlattr *data,
struct macvlan_info *mvi;
int err;
- NL_DBG(3, "Parsing MACVLAN link info\n");
+ NL_DBG(3, "Parsing %s link info", link->l_info_ops->io_name);
if ((err = nla_parse_nested(tb, IFLA_MACVLAN_MAX, data, macvlan_policy)) < 0)
goto errout;
@@ -112,12 +114,12 @@ static void macvlan_dump(struct rtnl_link *link, struct nl_dump_params *p)
if (mvi->mvi_mask & MACVLAN_HAS_MODE) {
rtnl_link_macvlan_mode2str(mvi->mvi_mode, buf, sizeof(buf));
- nl_dump(p, "macvlan-mode %s", buf);
+ nl_dump(p, "%s-mode %s", link->l_info_ops->io_name, buf);
}
if (mvi->mvi_mask & MACVLAN_HAS_FLAGS) {
rtnl_link_macvlan_flags2str(mvi->mvi_flags, buf, sizeof(buf));
- nl_dump(p, "macvlan-flags %s", buf);
+ nl_dump(p, "%s-flags %s", link->l_info_ops->io_name, buf);
}
}
@@ -161,16 +163,29 @@ nla_put_failure:
}
static struct rtnl_link_info_ops macvlan_info_ops = {
- .io_name = "macvlan",
- .io_alloc = macvlan_alloc,
- .io_parse = macvlan_parse,
+ .io_name = "macvlan",
+ .io_alloc = macvlan_alloc,
+ .io_parse = macvlan_parse,
.io_dump = {
- [NL_DUMP_LINE] = macvlan_dump,
- [NL_DUMP_DETAILS] = macvlan_dump,
+ [NL_DUMP_LINE] = macvlan_dump,
+ [NL_DUMP_DETAILS] = macvlan_dump,
},
- .io_clone = macvlan_clone,
- .io_put_attrs = macvlan_put_attrs,
- .io_free = macvlan_free,
+ .io_clone = macvlan_clone,
+ .io_put_attrs = macvlan_put_attrs,
+ .io_free = macvlan_free,
+};
+
+static struct rtnl_link_info_ops macvtap_info_ops = {
+ .io_name = "macvtap",
+ .io_alloc = macvlan_alloc,
+ .io_parse = macvlan_parse,
+ .io_dump = {
+ [NL_DUMP_LINE] = macvlan_dump,
+ [NL_DUMP_DETAILS] = macvlan_dump,
+ },
+ .io_clone = macvlan_clone,
+ .io_put_attrs = macvlan_put_attrs,
+ .io_free = macvlan_free,
};
/** @cond SKIP */
@@ -179,6 +194,12 @@ static struct rtnl_link_info_ops macvlan_info_ops = {
APPBUG("Link is not a macvlan link. set type \"macvlan\" first."); \
return -NLE_OPNOTSUPP; \
}
+
+#define IS_MACVTAP_LINK_ASSERT(link) \
+ if ((link)->l_info_ops != &macvtap_info_ops) { \
+ APPBUG("Link is not a macvtap link. set type \"macvtap\" first."); \
+ return -NLE_OPNOTSUPP; \
+ }
/** @endcond */
/**
@@ -313,6 +334,140 @@ uint16_t rtnl_link_macvlan_get_flags(struct rtnl_link *link)
/** @} */
+
+/**
+ * @name MACVTAP Object
+ * @{
+ */
+
+/**
+ * Allocate link object of type MACVTAP
+ *
+ * @return Allocated link object or NULL.
+ */
+struct rtnl_link *rtnl_link_macvtap_alloc(void)
+{
+ struct rtnl_link *link;
+ int err;
+
+ if (!(link = rtnl_link_alloc()))
+ return NULL;
+
+ if ((err = rtnl_link_set_type(link, "macvtap")) < 0) {
+ rtnl_link_put(link);
+ return NULL;
+ }
+
+ return link;
+}
+
+/**
+ * Check if link is a MACVTAP link
+ * @arg link Link object
+ *
+ * @return True if link is a MACVTAP link, otherwise false is returned.
+ */
+int rtnl_link_is_macvtap(struct rtnl_link *link)
+{
+ return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "macvtap");
+}
+
+/**
+ * Set MACVTAP MODE
+ * @arg link Link object
+ * @arg mode MACVTAP mode
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_link_macvtap_set_mode(struct rtnl_link *link, uint32_t mode)
+{
+ struct macvlan_info *mvi = link->l_info;
+
+ IS_MACVTAP_LINK_ASSERT(link);
+
+ mvi->mvi_mode = mode;
+ mvi->mvi_mask |= MACVLAN_HAS_MODE;
+
+ return 0;
+}
+
+/**
+ * Get MACVTAP Mode
+ * @arg link Link object
+ *
+ * @return MACVTAP mode, 0 if not set or a negative error code.
+ */
+uint32_t rtnl_link_macvtap_get_mode(struct rtnl_link *link)
+{
+ struct macvlan_info *mvi = link->l_info;
+
+ IS_MACVTAP_LINK_ASSERT(link);
+
+ if (mvi->mvi_mask & MACVLAN_HAS_MODE)
+ return mvi->mvi_mode;
+ else
+ return 0;
+}
+
+/**
+ * Set MACVTAP flags
+ * @arg link Link object
+ * @arg flags MACVTAP flags
+ *
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_link_macvtap_set_flags(struct rtnl_link *link, uint16_t flags)
+{
+ struct macvlan_info *mvi = link->l_info;
+
+ IS_MACVTAP_LINK_ASSERT(link);
+
+ mvi->mvi_flags |= flags;
+ mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
+
+ return 0;
+}
+
+/**
+ * Unset MACVTAP flags
+ * @arg link Link object
+ * @arg flags MACVTAP flags
+ *
+ * Note: kernel currently only has a single flag and lacks flags_mask to
+ * indicate which flags shall be changed (it always all).
+ *
+ * @return 0 on success or a negative error code.
+ */
+int rtnl_link_macvtap_unset_flags(struct rtnl_link *link, uint16_t flags)
+{
+ struct macvlan_info *mvi = link->l_info;
+
+ IS_MACVTAP_LINK_ASSERT(link);
+
+ mvi->mvi_flags &= ~flags;
+ mvi->mvi_mask |= MACVLAN_HAS_FLAGS;
+
+ return 0;
+}
+
+/**
+ * Get MACVTAP flags
+ * @arg link Link object
+ *
+ * @return MACVTAP flags, 0 if none set, or a negative error code.
+ */
+uint16_t rtnl_link_macvtap_get_flags(struct rtnl_link *link)
+{
+ struct macvlan_info *mvi = link->l_info;
+
+ IS_MACVTAP_LINK_ASSERT(link);
+
+ return mvi->mvi_flags;
+}
+
+/** @} */
+
+
static const struct trans_tbl macvlan_flags[] = {
__ADD(MACVLAN_FLAG_NOPROMISC, nopromisc),
};
@@ -339,6 +494,16 @@ int rtnl_link_macvlan_str2flags(const char *name)
return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
}
+char *rtnl_link_macvtap_flags2str(int flags, char *buf, size_t len)
+{
+ return __flags2str(flags, buf, len, macvlan_flags, ARRAY_SIZE(macvlan_flags));
+}
+
+int rtnl_link_macvtap_str2flags(const char *name)
+{
+ return __str2flags(name, macvlan_flags, ARRAY_SIZE(macvlan_flags));
+}
+
/** @} */
/**
@@ -356,16 +521,28 @@ int rtnl_link_macvlan_str2mode(const char *name)
return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
}
+char *rtnl_link_macvtap_mode2str(int mode, char *buf, size_t len)
+{
+ return __type2str(mode, buf, len, macvlan_modes, ARRAY_SIZE(macvlan_modes));
+}
+
+int rtnl_link_macvtap_str2mode(const char *name)
+{
+ return __str2type(name, macvlan_modes, ARRAY_SIZE(macvlan_modes));
+}
+
/** @} */
static void __init macvlan_init(void)
{
rtnl_link_register_info(&macvlan_info_ops);
+ rtnl_link_register_info(&macvtap_info_ops);
}
static void __exit macvlan_exit(void)
{
rtnl_link_unregister_info(&macvlan_info_ops);
+ rtnl_link_unregister_info(&macvtap_info_ops);
}
/** @} */
diff --git a/libnl-route-3.sym b/libnl-route-3.sym
index f18a11d8..627cb432 100644
--- a/libnl-route-3.sym
+++ b/libnl-route-3.sym
@@ -888,9 +888,21 @@ global:
libnl_3_2_28 {
global:
rtnl_link_alloc_cache_flags;
+ rtnl_link_is_macvtap;
rtnl_link_is_vrf;
+ rtnl_link_macvtap_alloc;
+ rtnl_link_macvtap_flags2str;
+ rtnl_link_macvtap_get_flags;
+ rtnl_link_macvtap_get_mode;
+ rtnl_link_macvtap_mode2str;
+ rtnl_link_macvtap_set_flags;
+ rtnl_link_macvtap_set_mode;
+ rtnl_link_macvtap_str2flags;
+ rtnl_link_macvtap_str2mode;
+ rtnl_link_macvtap_unset_flags;
rtnl_link_vrf_alloc;
rtnl_link_vrf_get_tableid;
rtnl_link_vrf_set_tableid;
rtnl_neigh_alloc_cache_flags;
} libnl_3_2_27;
+
diff --git a/tests/.gitignore b/tests/.gitignore
index 0e6006c7..d11450ba 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -14,6 +14,7 @@
/test-create-ipvlan
/test-create-ipvti
/test-create-macvlan
+/test-create-macvtap
/test-create-sit
/test-create-veth
/test-create-vlan
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3a8256c2..e7646357 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -35,6 +35,7 @@ check_PROGRAMS = \
test-create-ipip \
test-create-ipvti \
test-create-macvlan \
+ test-create-macvtap \
test-create-ipvlan \
test-create-vrf \
test-create-sit \
diff --git a/tests/test-create-macvtap.c b/tests/test-create-macvtap.c
new file mode 100644
index 00000000..dc7df952
--- /dev/null
+++ b/tests/test-create-macvtap.c
@@ -0,0 +1,50 @@
+#include <netinet/ether.h>
+
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/macvtap.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_cache *link_cache;
+ struct nl_sock *sk;
+ struct nl_addr* addr;
+ int err, master_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
+ fprintf(stderr, "Unable to lookup eth0");
+ return -1;
+ }
+
+ link = rtnl_link_macvtap_alloc();
+
+ rtnl_link_set_link(link, master_index);
+
+ addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN);
+ rtnl_link_set_addr(link, addr);
+ nl_addr_put(addr);
+
+ rtnl_link_macvtap_set_mode(link, rtnl_link_macvtap_str2mode("bridge"));
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}