[PATCH 1/2] Add macvtap support

Beniamino Galvani bgalvani at redhat.com
Mon Oct 5 01:55:38 PDT 2015


This adds support for MAC-VLAN based tap interfaces (macvtap).

Signed-off-by: Beniamino Galvani <bgalvani at redhat.com>
---
 doc/route.txt                        |  57 +++++++++++
 include/Makefile.am                  |   1 +
 include/netlink/route/link/macvtap.h |  46 +++++++++
 lib/route/link/macvlan.c             | 185 ++++++++++++++++++++++++++++++++++-
 libnl-route-3.sym                    |  15 +++
 tests/.gitignore                     |   1 +
 tests/Makefile.am                    |   1 +
 tests/test-create-macvtap.c          |  50 ++++++++++
 8 files changed, 352 insertions(+), 4 deletions(-)
 create mode 100644 include/netlink/route/link/macvtap.h
 create mode 100644 tests/test-create-macvtap.c

diff --git a/doc/route.txt b/doc/route.txt
index d9f88e1..e1e15f1 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 ffaa9a5..6b7d357 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 0000000..76ae1db
--- /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 at 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 b8380aa..f422431 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");
+	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);
 	}
 }
 
@@ -173,12 +175,31 @@ static struct rtnl_link_info_ops macvlan_info_ops = {
 	.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 */
 #define IS_MACVLAN_LINK_ASSERT(link) \
 	if ((link)->l_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 c6a7832..3bc129b 100644
--- a/libnl-route-3.sym
+++ b/libnl-route-3.sym
@@ -884,3 +884,18 @@ global:
 	rtnl_link_ipvlan_get_mode;
 	rtnl_link_set_link_netnsid;
 } libnl_3_2_26;
+
+libnl_3_2_28 {
+global:
+	rtnl_link_is_macvtap;
+	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;
+} libnl_3_2_27;
diff --git a/tests/.gitignore b/tests/.gitignore
index 0f55108..a587239 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -12,6 +12,7 @@
 /test-create-ipip
 /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 edf2ee5..f579335 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-sit \
 	test-create-ifb \
diff --git a/tests/test-create-macvtap.c b/tests/test-create-macvtap.c
new file mode 100644
index 0000000..7539185
--- /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;
+}
-- 
2.4.3




More information about the libnl mailing list