libnl: /data/src/libnl3/lib/route/link/vrf.c Source File

libnl 3.10.0

vrf.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2015 Cumulus Networks. All rights reserved.
4 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
5 */
6
7/**
8 * @ingroup link
9 * @defgroup vrf VRF
10 * Virtual Routing and Forwarding link module
11 *
12 * @details
13 * \b Link Type Name: "vrf"
14 *
15 * @route_doc{link_vrf, VRF Documentation}
16 *
17 * @{
18 */
19
20#include "nl-default.h"
21
22#include <linux/if_link.h>
23#include <linux/rtnetlink.h>
24
25#include <netlink/netlink.h>
26#include <netlink/attr.h>
27#include <netlink/utils.h>
28#include <netlink/object.h>
29#include <netlink/route/rtnl.h>
30#include <netlink/route/link/vrf.h>
31
32#include "nl-route.h"
33#include "link-api.h"
34
35#define VRF_TABLE_ID_MAX RT_TABLE_MAX
36
37/** @cond SKIP */
38#define VRF_HAS_TABLE_ID (1<<0)
39
40struct vrf_info {
41 uint32_t table_id;
42 uint32_t vi_mask;
43};
44
45/** @endcond */
46
47static struct nla_policy vrf_policy[IFLA_VRF_MAX + 1] = {
48 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
49};
50
51static int vrf_alloc(struct rtnl_link *link)
52{
53 struct vrf_info *vi;
54
55 if (link->l_info) {
56 memset(link->l_info, 0, sizeof (*vi));
57 return 0;
58 }
59
60 if ((vi = calloc(1, sizeof(*vi))) == NULL)
61 return -NLE_NOMEM;
62
63 link->l_info = vi;
64
65 return 0;
66}
67
68static int vrf_parse(struct rtnl_link *link, struct nlattr *data,
69 struct nlattr *xstats)
70{
71 struct nlattr *tb[IFLA_VRF_MAX+1];
72 struct vrf_info *vi;
73 int err;
74
75 NL_DBG(3, "Parsing VRF link info");
76
77 if ((err = nla_parse_nested(tb, IFLA_VRF_MAX, data, vrf_policy)) < 0)
78 goto errout;
79
80 if ((err = vrf_alloc(link)) < 0)
81 goto errout;
82
83 vi = link->l_info;
84
85 if (tb[IFLA_VRF_TABLE]) {
86 vi->table_id = nla_get_u32(tb[IFLA_VRF_TABLE]);
87 vi->vi_mask |= VRF_HAS_TABLE_ID;
88 }
89
90 err = 0;
91
92errout:
93 return err;
94}
95
96static void vrf_free(struct rtnl_link *link)
97{
98 free(link->l_info);
99 link->l_info = NULL;
100}
101
102static int vrf_clone(struct rtnl_link *dst, struct rtnl_link *src)
103{
104 struct vrf_info *vdst, *vsrc = src->l_info;
105 int err;
106
107 dst->l_info = NULL;
108 if ((err = rtnl_link_set_type(dst, "vrf")) < 0)
109 return err;
110 vdst = dst->l_info;
111
112 BUG_ON(!vdst || !vsrc);
113
114 memcpy(vdst, vsrc, sizeof(struct vrf_info));
115
116 return 0;
117}
118
119static int vrf_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
120{
121 struct vrf_info *vi = link->l_info;
122 struct nlattr *data;
123
124 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
125 return -NLE_NOMEM;
126
127 if (vi->vi_mask & VRF_HAS_TABLE_ID) {
128 NLA_PUT_U32(msg, IFLA_VRF_TABLE, vi->table_id);
129 }
130
131 nla_nest_end(msg, data);
132
133nla_put_failure:
134
135 return 0;
136}
137
138static void vrf_dump(struct rtnl_link *link, struct nl_dump_params *p)
139{
140 struct vrf_info *vi = link->l_info;
141
142 if (vi->vi_mask & VRF_HAS_TABLE_ID) {
143 nl_dump(p, "table-id %u", vi->table_id);
144 }
145}
146
147static struct rtnl_link_info_ops vrf_info_ops = {
148 .io_name = "vrf",
149 .io_alloc = vrf_alloc,
150 .io_parse = vrf_parse,
151 .io_dump = {
152 [NL_DUMP_LINE] = vrf_dump,
153 [NL_DUMP_DETAILS] = vrf_dump,
154 },
155 .io_clone = vrf_clone,
156 .io_put_attrs = vrf_put_attrs,
157 .io_free = vrf_free,
158};
159
160/** @cond SKIP */
161#define IS_VRF_LINK_ASSERT(link) \
162 if ((link)->l_info_ops != &vrf_info_ops) { \
163 APPBUG("Link is not a VRF link. set type \"vrf\" first."); \
164 return -NLE_OPNOTSUPP; \
165 }
166/** @endcond */
167
168/**
169 * @name VRF Object
170 * @{
171 */
172
173/**
174 * Allocate link object of type VRF
175 *
176 * @return Allocated link object or NULL.
177 */
178 struct rtnl_link *rtnl_link_vrf_alloc(void)
179{
180 struct rtnl_link *link;
181
182 if (!(link = rtnl_link_alloc()))
183 return NULL;
184
185 if (rtnl_link_set_type(link, "vrf") < 0) {
186 rtnl_link_put(link);
187 return NULL;
188 }
189
190 return link;
191}
192
193/**
194 * Check if link is a VRF link
195 * @arg link Link object
196 *
197 * @return True if link is a VRF link, otherwise false is returned.
198 */
199 int rtnl_link_is_vrf(struct rtnl_link *link)
200{
201 return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vrf");
202}
203
204/**
205 * Get VRF table id
206 * @arg link Link object
207 * @arg id Pointer to store table identifier
208 *
209 * @return 0 on success or a negative error code
210 */
211 int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id)
212{
213 struct vrf_info *vi = link->l_info;
214
215 IS_VRF_LINK_ASSERT(link);
216 if(!id)
217 return -NLE_INVAL;
218
219 if (vi->vi_mask & VRF_HAS_TABLE_ID)
220 *id = vi->table_id;
221 else
222 return -NLE_AGAIN;
223
224 return 0;
225}
226
227/**
228 * Set VRF table id
229 * @arg link Link object
230 * @arg id Table identifier associated with VRF link
231 *
232 * @return 0 on success or a negative error code
233 */
234 int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id)
235{
236 struct vrf_info *vi = link->l_info;
237
238 IS_VRF_LINK_ASSERT(link);
239
240 _NL_STATIC_ASSERT(VRF_TABLE_ID_MAX == UINT32_MAX);
241
242 vi->table_id = id;
243 vi->vi_mask |= VRF_HAS_TABLE_ID;
244
245 return 0;
246}
247
248/** @} */
249
250static void _nl_init vrf_init(void)
251{
252 rtnl_link_register_info(&vrf_info_ops);
253}
254
255static void _nl_exit vrf_exit(void)
256{
257 rtnl_link_unregister_info(&vrf_info_ops);
258}
259
260/** @} */
nla_get_u32
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition attr.c:712
NLA_PUT_U32
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition attr.h:230
nla_nest_start
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition attr.c:908
nla_parse_nested
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition attr.c:1035
nla_nest_end
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition attr.c:971
NLA_U32
@ NLA_U32
32 bit integer
Definition attr.h:37
rtnl_link_register_info
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
Definition api.c:127
rtnl_link_unregister_info
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
Definition api.c:162
rtnl_link_alloc
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
Definition link.c:1920
rtnl_link_put
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
Definition link.c:1929
rtnl_link_set_type
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
Definition link.c:2558
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
rtnl_link_is_vrf
int rtnl_link_is_vrf(struct rtnl_link *link)
Check if link is a VRF link.
Definition vrf.c:199
rtnl_link_vrf_set_tableid
int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id)
Set VRF table id.
Definition vrf.c:234
rtnl_link_vrf_get_tableid
int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id)
Get VRF table id.
Definition vrf.c:211
rtnl_link_vrf_alloc
struct rtnl_link * rtnl_link_vrf_alloc(void)
Allocate link object of type VRF.
Definition vrf.c:178
nl_dump_params
Dumping parameters.
Definition types.h:32
nla_policy
Attribute validation policy.
Definition attr.h:63
nla_policy::type
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition attr.h:65
rtnl_link_info_ops
Available operations to modules implementing a link info type.
Definition link-api.h:20
rtnl_link_info_ops::io_name
char * io_name
Name of link info type, must match name on kernel side.
Definition link-api.h:22
rtnl_link
Link object.
Definition nl-route.h:29