diff options
author | David Ahern <dsa@cumulusnetworks.com> | 2015-10-21 17:44:29 (GMT) |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-11-01 16:54:03 (GMT) |
commit | 493bbd13e6d96c61e844dd1c04aa7218d0b7da08 (patch) | |
tree | cb47114e902a28be329940f65fa59296c1dc4e4a /lib/route/link/vrf.c | |
parent | f25138d3214cefba0dc992ce7a408de580fd3dcd (diff) | |
download | libnl-493bbd13e6d96c61e844dd1c04aa7218d0b7da08.zip libnl-493bbd13e6d96c61e844dd1c04aa7218d0b7da08.tar.gz libnl-493bbd13e6d96c61e844dd1c04aa7218d0b7da08.tar.bz2 |
route/vrf: add VRF support
http://lists.infradead.org/pipermail/libnl/2015-October/001991.html
Signed-off-by: David Ahern <dsa@cumulusnetworks.com>
[thaller@redhat.com: slightly modified original patch]
Signed-off-by: Thomas Haller <thaller@redhat.com>
Diffstat (limited to 'lib/route/link/vrf.c')
-rw-r--r-- | lib/route/link/vrf.c | 263 |
1 files changed, 263 insertions, 0 deletions
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); +} + +/** @} */ |