[PATCH] Add VRF support
David Ahern
dsa at cumulusnetworks.com
Tue Oct 20 16:55:00 PDT 2015
Signed-off-by: David Ahern <dsa at cumulusnetworks.com>
---
include/Makefile.am | 1 +
include/linux-private/linux/if_link.h | 9 ++
include/netlink/route/link/vrf.h | 32 +++++
lib/Makefile.am | 1 +
lib/route/link/vrf.c | 244 ++++++++++++++++++++++++++++++++++
tests/Makefile.am | 1 +
tests/test-create-vrf.c | 35 +++++
7 files changed, 323 insertions(+)
create mode 100644 include/netlink/route/link/vrf.h
create mode 100644 lib/route/link/vrf.c
create mode 100644 tests/test-create-vrf.c
diff --git a/include/Makefile.am b/include/Makefile.am
index ffaa9a5f3d1d..84487a47dadb 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -61,6 +61,7 @@ nobase_libnlinclude_HEADERS = \
netlink/route/link/ipvti.h \
netlink/route/link/sit.h \
netlink/route/link/ipvlan.h \
+ netlink/route/link/vrf.h \
netlink/route/qdisc/cbq.h \
netlink/route/qdisc/dsmark.h \
netlink/route/qdisc/fifo.h \
diff --git a/include/linux-private/linux/if_link.h b/include/linux-private/linux/if_link.h
index 3d0d61317733..f8eb94a43b53 100644
--- a/include/linux-private/linux/if_link.h
+++ b/include/linux-private/linux/if_link.h
@@ -337,6 +337,15 @@ enum macvlan_macaddr_mode {
#define MACVLAN_FLAG_NOPROMISC 1
+/* VRF section */
+enum {
+ IFLA_VRF_UNSPEC,
+ IFLA_VRF_TABLE,
+ __IFLA_VRF_MAX
+};
+
+#define IFLA_VRF_MAX (__IFLA_VRF_MAX - 1)
+
/* IPVLAN section */
enum {
IFLA_IPVLAN_UNSPEC,
diff --git a/include/netlink/route/link/vrf.h b/include/netlink/route/link/vrf.h
new file mode 100644
index 000000000000..0a56d91e203c
--- /dev/null
+++ b/include/netlink/route/link/vrf.h
@@ -0,0 +1,32 @@
+/*
+ * netlink/route/link/vrf.h VRF 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 Cumulus Networks. All rights reserved.
+ * Copyright (c) 2015 David Ahern <dsa at cumulusnetworks.com>
+ */
+
+#ifndef NETLINK_LINK_VRF_H_
+#define NETLINK_LINK_VRF_H_
+
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern struct rtnl_link *rtnl_link_vrf_alloc(void);
+extern int rtnl_link_is_vrf(struct rtnl_link *link);
+extern int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id);
+extern int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1474c8df6e3d..320c511c68eb 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -111,6 +111,7 @@ libnl_route_3_la_SOURCES = \
route/link/vxlan.c route/link/veth.c route/link/ipip.c \
route/link/ipgre.c route/link/sit.c route/link/ipvti.c \
route/link/ip6tnl.c route/link/ifb.c route/link/ipvlan.c \
+ route/link/vrf.c \
\
route/qdisc/blackhole.c route/qdisc/cbq.c route/qdisc/dsmark.c \
route/qdisc/fifo.c route/qdisc/htb.c route/qdisc/netem.c \
diff --git a/lib/route/link/vrf.c b/lib/route/link/vrf.c
new file mode 100644
index 000000000000..698c12f8cc19
--- /dev/null
+++ b/lib/route/link/vrf.c
@@ -0,0 +1,244 @@
+/*
+ * lib/route/link/vrf.c VRF Link Info
+ *
+ * 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 Cumulus Networks. All rights reserved.
+ * Copyright (c) 2015 David Ahern <dsa at cumulusnetworks.com>
+ */
+
+/**
+ * @ingroup link
+ * @defgroup vrf VRF
+ * Virtual Routing and Forwarding link module
+ *
+ * @details
+ * \b Link Type Name: "vrf"
+ *
+ * @route_doc{link_vrf, VRF Documentation}
+ *
+ * @{
+ */
+
+#include <netlink-private/netlink.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+#include <netlink/utils.h>
+#include <netlink/object.h>
+#include <netlink/route/rtnl.h>
+#include <netlink-private/route/link/api.h>
+#include <netlink/route/link/vrf.h>
+
+#include <linux/if_link.h>
+
+#define VRF_TABLE_ID_MAX (uint32_t) -1
+
+/** @cond SKIP */
+#define VRF_HAS_TABLE_ID (1<<0)
+
+struct vrf_info {
+ uint32_t table_id;
+ uint32_t vi_mask;
+};
+
+/** @endcond */
+
+static struct nla_policy vrf_policy[IFLA_VRF_MAX + 1] = {
+ [IFLA_VRF_TABLE] = { .type = NLA_U32 },
+};
+
+static int vrf_alloc(struct rtnl_link *link)
+{
+ struct vrf_info *vi;
+
+ if ((vi = calloc(1, sizeof(*vi))) == NULL)
+ return -NLE_NOMEM;
+
+ link->l_info = vi;
+
+ return 0;
+}
+
+static int vrf_parse(struct rtnl_link *link, struct nlattr *data,
+ struct nlattr *xstats)
+{
+ struct nlattr *tb[IFLA_VXLAN_MAX+1];
+ struct vrf_info *vi;
+ int err;
+
+ NL_DBG(3, "Parsing VRF link info");
+
+ if ((err = nla_parse_nested(tb, IFLA_VRF_MAX, data, vrf_policy))< 0)
+ goto errout;
+
+ if ((err = vrf_alloc(link)) < 0)
+ goto errout;
+
+ vi = link->l_info;
+
+ if (tb[IFLA_VRF_TABLE]) {
+ vi->table_id = nla_get_u32(tb[IFLA_VRF_TABLE]);
+ vi->vi_mask |= VRF_HAS_TABLE_ID;
+ }
+
+ err = 0;
+
+errout:
+ return err;
+}
+
+static void vrf_free(struct rtnl_link *link)
+{
+ struct vrf_info *vi = link->l_info;
+
+ free(vi);
+ link->l_info = NULL;
+}
+
+static int vrf_clone(struct rtnl_link *dst, struct rtnl_link *src)
+{
+ return 0;
+}
+
+static int vrf_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
+{
+ struct vrf_info *vi = link->l_info;
+ struct nlattr *data;
+
+ if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
+ return -NLE_MSGSIZE;
+
+ NLA_PUT_U32(msg, IFLA_VRF_TABLE, vi->table_id);
+
+nla_put_failure:
+
+ return 0;
+}
+
+static void vrf_dump(struct rtnl_link *link, struct nl_dump_params *p)
+{
+ struct vrf_info *vi = link->l_info;
+
+ if (vi->vi_mask & VRF_HAS_TABLE_ID) {
+ nl_dump(p, "table-id %u", vi->table_id);
+ }
+}
+
+static struct rtnl_link_info_ops vrf_info_ops = {
+ .io_name = "vrf",
+ .io_alloc = vrf_alloc,
+ .io_parse = vrf_parse,
+ .io_dump = {
+ [NL_DUMP_LINE] = vrf_dump,
+ [NL_DUMP_DETAILS] = vrf_dump,
+ },
+ .io_clone = vrf_clone,
+ .io_put_attrs = vrf_put_attrs,
+ .io_free = vrf_free,
+};
+
+/** @cond SKIP */
+#define IS_VRF_LINK_ASSERT(link) \
+ if ((link)->l_info_ops != &vrf_info_ops) { \
+ APPBUG("Link is not a VRF link. set type \"vrf\" first."); \
+ return -NLE_OPNOTSUPP; \
+ }
+/** @endcond */
+
+/**
+ * @name VRF Object
+ * @{
+ */
+
+/**
+ * Allocate link object of type VRF
+ *
+ * @return Allocated link object or NULL.
+ */
+struct rtnl_link *rtnl_link_vrf_alloc(void)
+{
+ struct rtnl_link *link;
+ int err;
+
+ if (!(link = rtnl_link_alloc()))
+ return NULL;
+
+ if ((err = rtnl_link_set_type(link, "vrf")) < 0) {
+ rtnl_link_put(link);
+ return NULL;
+ }
+
+ return link;
+}
+
+/**
+ * Check if link is a VRF link
+ * @arg link Link object
+ *
+ * @return True if link is a VRF link, otherwise false is returned.
+ */
+int rtnl_link_is_vrf(struct rtnl_link *link)
+{
+ return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vrf");
+}
+
+/**
+ * Get VRF table id
+ * @arg link Link object
+ * @arg id Pointer to store table identifier
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id)
+{
+ struct vrf_info *vi = link->l_info;
+
+ IS_VRF_LINK_ASSERT(link);
+ if(!id)
+ return -NLE_INVAL;
+
+ if (vi->vi_mask & VRF_HAS_TABLE_ID)
+ *id = vi->table_id;
+ else
+ return -NLE_AGAIN;
+
+ return 0;
+}
+
+/**
+ * Set VRF table id
+ * @arg link Link object
+ * @arg id Table identifier associated with VRF link
+ *
+ * @return 0 on success or a negative error code
+ */
+int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id)
+{
+ struct vrf_info *vi = link->l_info;
+
+ IS_VRF_LINK_ASSERT(link);
+ if(id > VRF_TABLE_ID_MAX)
+ return -NLE_INVAL;
+
+ vi->table_id = id;
+ vi->vi_mask |= VRF_HAS_TABLE_ID;
+
+ return 0;
+}
+
+/** @} */
+
+static void __init vrf_init(void)
+{
+ rtnl_link_register_info(&vrf_info_ops);
+}
+
+static void __exit vrf_exit(void)
+{
+ rtnl_link_unregister_info(&vrf_info_ops);
+}
+
+/** @} */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index edf2ee58930d..3a8256c2db22 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -36,6 +36,7 @@ check_PROGRAMS = \
test-create-ipvti \
test-create-macvlan \
test-create-ipvlan \
+ test-create-vrf \
test-create-sit \
test-create-ifb \
test-delete-link \
diff --git a/tests/test-create-vrf.c b/tests/test-create-vrf.c
new file mode 100644
index 000000000000..acfd19cc8a2d
--- /dev/null
+++ b/tests/test-create-vrf.c
@@ -0,0 +1,35 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/vrf.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_sock *sk;
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ link = rtnl_link_vrf_alloc();
+
+ rtnl_link_set_name(link, "vrf-red");
+
+ if ((err = rtnl_link_vrf_set_tableid(link, 10)) < 0) {
+ nl_perror(err, "Unable to set VRF table id");
+ return err;
+ }
+
+ 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;
+}
--
1.9.1
More information about the libnl
mailing list