summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/Makefile.am1
-rw-r--r--include/linux-private/linux/if_link.h9
-rw-r--r--include/netlink/route/link/vrf.h32
-rw-r--r--lib/Makefile.am1
-rw-r--r--lib/route/link/vrf.c263
-rw-r--r--libnl-route-3.sym8
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/test-create-vrf.c59
8 files changed, 374 insertions, 0 deletions
diff --git a/include/Makefile.am b/include/Makefile.am
index ffaa9a5..84487a4 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 3d0d613..af4a8f9 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 0000000..0a56d91
--- /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@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 1474c8d..320c511 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 0000000..84d2d8b
--- /dev/null
+++ b/lib/route/link/vrf.c
@@ -0,0 +1,263 @@
+/*
+ * 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@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>
+#include <linux-private/linux/rtnetlink.h>
+
+#define VRF_TABLE_ID_MAX RT_TABLE_MAX
+
+/** @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 (link->l_info) {
+ memset(link->l_info, 0, sizeof (*vi));
+ return 0;
+ }
+
+ 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_VRF_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)
+{
+ free(link->l_info);
+ link->l_info = NULL;
+}
+
+static int vrf_clone(struct rtnl_link *dst, struct rtnl_link *src)
+{
+ struct vrf_info *vdst, *vsrc = src->l_info;
+ int err;
+
+ if ((err = rtnl_link_set_type(dst, "vrf")) < 0)
+ return err;
+ vdst = dst->l_info;
+
+ BUG_ON(!vdst || !vsrc);
+
+ memcpy(vdst, vsrc, sizeof(struct vrf_info));
+
+ 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_NOMEM;
+
+ if (vi->vi_mask & VRF_HAS_TABLE_ID) {
+ NLA_PUT_U32(msg, IFLA_VRF_TABLE, vi->table_id);
+ }
+
+ nla_nest_end(msg, data);
+
+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/libnl-route-3.sym b/libnl-route-3.sym
index c6a7832..23b839a 100644
--- a/libnl-route-3.sym
+++ b/libnl-route-3.sym
@@ -884,3 +884,11 @@ global:
rtnl_link_ipvlan_get_mode;
rtnl_link_set_link_netnsid;
} libnl_3_2_26;
+
+libnl_3_2_28 {
+global:
+ rtnl_link_is_vrf;
+ rtnl_link_vrf_alloc;
+ rtnl_link_vrf_get_tableid;
+ rtnl_link_vrf_set_tableid;
+} libnl_3_2_27;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index edf2ee5..3a8256c 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 0000000..7db6d8a
--- /dev/null
+++ b/tests/test-create-vrf.c
@@ -0,0 +1,59 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/vrf.h>
+
+int main(int argc, char *argv[])
+{
+ struct nl_cache *link_cache;
+ struct rtnl_link *link, *link2;
+ struct nl_sock *sk;
+ uint32_t tb_id;
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ if (!(link = rtnl_link_vrf_alloc())) {
+ fprintf(stderr, "Unable to allocate link");
+ return -1;
+ }
+
+ 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;
+ }
+
+ if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if (!(link2 = rtnl_link_get_by_name(link_cache, "vrf-red"))) {
+ fprintf(stderr, "Unable to lookup vrf-red");
+ return -1;
+ }
+
+ if ((err = rtnl_link_vrf_get_tableid(link2, &tb_id)) < 0) {
+ nl_perror(err, "Unable to get VRF table id");
+ return err;
+ }
+
+ if (tb_id != 10) {
+ fprintf(stderr, "Mismatch with VRF table id\n");
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}