]> git.proxmox.com Git - mirror_frr.git/blame - lib/vrf.c
zebra: always display vrf in show ip route json
[mirror_frr.git] / lib / vrf.c
CommitLineData
b72ede27
FL
1/*
2 * VRF functions.
3 * Copyright (C) 2014 6WIND S.A.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b72ede27
FL
20 */
21
22#include <zebra.h>
23
4691b65a
PG
24/* for basename */
25#include <libgen.h>
26
6a69b354 27#include "if.h"
b72ede27 28#include "vrf.h"
7922fc65 29#include "vrf_int.h"
b72ede27
FL
30#include "prefix.h"
31#include "table.h"
32#include "log.h"
33#include "memory.h"
19dc275e 34#include "command.h"
b95c1883 35#include "ns.h"
3bc34908 36#include "privs.h"
98cbbaea 37#include "nexthop_group.h"
b66d022e 38#include "lib_errors.h"
bc867a5d 39#include "northbound.h"
8b4cb7a6 40#include "northbound_cli.h"
19dc275e 41
ec31f30d
PG
42/* default VRF ID value used when VRF backend is not NETNS */
43#define VRF_DEFAULT_INTERNAL 0
dd114702 44#define VRF_DEFAULT_NAME_INTERNAL "default"
ec31f30d 45
d62a17ae 46DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
4a1ab8e4
DL
47DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
48
e80e7cce
DL
49DEFINE_QOBJ_TYPE(vrf)
50
d62a17ae 51static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
52static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
1a1a7065 53
d62a17ae 54RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
55RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
1a1a7065 56
d62a17ae 57struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
58struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
1a1a7065 59
78dd30b2 60static int vrf_backend;
72261ecd 61static int vrf_backend_configured;
3bc34908 62static struct zebra_privs_t *vrf_daemon_privs;
c200f5e1 63static char vrf_default_name[VRF_NAMSIZ] = VRF_DEFAULT_NAME_INTERNAL;
78dd30b2 64
19dc275e
DS
65/*
66 * Turn on/off debug code
67 * for vrf.
68 */
c17faa4b 69static int debug_vrf = 0;
b72ede27 70
b72ede27 71/* Holding VRF hooks */
1b3e9a21 72static struct vrf_master {
d62a17ae 73 int (*vrf_new_hook)(struct vrf *);
74 int (*vrf_delete_hook)(struct vrf *);
75 int (*vrf_enable_hook)(struct vrf *);
76 int (*vrf_disable_hook)(struct vrf *);
ecbc5a37 77 int (*vrf_update_name_hook)(struct vrf *vrf);
d62a17ae 78} vrf_master = {
79 0,
80};
b72ede27 81
d62a17ae 82static int vrf_is_enabled(struct vrf *vrf);
e5bf3e1e 83
216b18ef 84/* VRF list existance check by name. */
d62a17ae 85struct vrf *vrf_lookup_by_name(const char *name)
216b18ef 86{
d62a17ae 87 struct vrf vrf;
88 strlcpy(vrf.name, name, sizeof(vrf.name));
89 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
216b18ef 90}
216b18ef 91
d62a17ae 92static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
b72ede27 93{
d62a17ae 94 return (a->vrf_id - b->vrf_id);
216b18ef
DS
95}
96
d62a17ae 97static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
b72ede27 98{
d62a17ae 99 return strcmp(a->name, b->name);
b72ede27
FL
100}
101
e26aedbe
PG
102/* if ns_id is different and not VRF_UNKNOWN,
103 * then update vrf identifier, and enable VRF
104 */
105static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr)
106{
107 ns_id_t vrf_id = (vrf_id_t)ns_id;
108 vrf_id_t old_vrf_id;
109 struct vrf *vrf = (struct vrf *)opaqueptr;
110
111 if (!vrf)
112 return;
113 old_vrf_id = vrf->vrf_id;
114 if (vrf_id == vrf->vrf_id)
115 return;
116 if (vrf->vrf_id != VRF_UNKNOWN)
117 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
118 vrf->vrf_id = vrf_id;
119 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
120 if (old_vrf_id == VRF_UNKNOWN)
c4efd0f4 121 vrf_enable(vrf);
e26aedbe
PG
122}
123
ce1be369
PG
124int vrf_switch_to_netns(vrf_id_t vrf_id)
125{
126 char *name;
127 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
128
ce1be369 129 /* VRF is default VRF. silently ignore */
e26aedbe 130 if (!vrf || vrf->vrf_id == VRF_DEFAULT)
9dff1132 131 return 1; /* 1 = default */
e26aedbe
PG
132 /* VRF has no NETNS backend. silently ignore */
133 if (vrf->data.l.netns_name[0] == '\0')
9dff1132 134 return 2; /* 2 = no netns */
ce1be369
PG
135 name = ns_netns_pathname(NULL, vrf->data.l.netns_name);
136 if (debug_vrf)
137 zlog_debug("VRF_SWITCH: %s(%u)", name, vrf->vrf_id);
138 return ns_switch_to_netns(name);
139}
140
141int vrf_switchback_to_initial(void)
142{
143 int ret = ns_switchback_to_initial();
144
145 if (ret == 0 && debug_vrf)
146 zlog_debug("VRF_SWITCHBACK");
147 return ret;
148}
149
216b18ef 150/* Get a VRF. If not found, create one.
34f8e6af
DS
151 * Arg:
152 * name - The name of the vrf. May be NULL if unknown.
153 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
216b18ef 154 * Description: Please note that this routine can be called with just the name
34f8e6af
DS
155 * and 0 vrf-id
156 */
d62a17ae 157struct vrf *vrf_get(vrf_id_t vrf_id, const char *name)
158{
159 struct vrf *vrf = NULL;
160 int new = 0;
161
d62a17ae 162 /* Nothing to see, move along here */
163 if (!name && vrf_id == VRF_UNKNOWN)
164 return NULL;
165
0c2bac38
PG
166 /* attempt to find already available VRF
167 */
168 if (name)
169 vrf = vrf_lookup_by_name(name);
dd114702
PG
170 if (vrf && vrf_id != VRF_UNKNOWN
171 && vrf->vrf_id != VRF_UNKNOWN
172 && vrf->vrf_id != vrf_id) {
173 zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)",
174 name, vrf_id, vrf->vrf_id);
175 return NULL;
176 }
d62a17ae 177 /* Try to find VRF both by ID and name */
0c2bac38 178 if (!vrf && vrf_id != VRF_UNKNOWN)
d62a17ae 179 vrf = vrf_lookup_by_id(vrf_id);
d62a17ae 180
181 if (vrf == NULL) {
182 vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf));
183 vrf->vrf_id = VRF_UNKNOWN;
d62a17ae 184 QOBJ_REG(vrf, vrf);
185 new = 1;
186
187 if (debug_vrf)
188 zlog_debug("VRF(%u) %s is created.", vrf_id,
189 (name) ? name : "(NULL)");
190 }
191
192 /* Set identifier */
193 if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) {
194 vrf->vrf_id = vrf_id;
195 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
196 }
197
198 /* Set name */
199 if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) {
87272aff 200 /* update the vrf name */
d62a17ae 201 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
87272aff
PG
202 strlcpy(vrf->data.l.netns_name,
203 name, NS_NAMSIZ);
d62a17ae 204 strlcpy(vrf->name, name, sizeof(vrf->name));
205 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
87272aff
PG
206 if (vrf->vrf_id == VRF_DEFAULT)
207 vrf_set_default_name(vrf->name, false);
d62a17ae 208 } else if (name && vrf->name[0] == '\0') {
209 strlcpy(vrf->name, name, sizeof(vrf->name));
210 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
211 }
d62a17ae 212 if (new &&vrf_master.vrf_new_hook)
213 (*vrf_master.vrf_new_hook)(vrf);
214
215 return vrf;
b72ede27
FL
216}
217
84915b0a 218/* Delete a VRF. This is called when the underlying VRF goes away, a
219 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
220 */
d62a17ae 221void vrf_delete(struct vrf *vrf)
b72ede27 222{
d62a17ae 223 if (debug_vrf)
c7384cf8
DS
224 zlog_debug("VRF %s(%u) is to be deleted.", vrf->name,
225 vrf->vrf_id);
b72ede27 226
d62a17ae 227 if (vrf_is_enabled(vrf))
228 vrf_disable(vrf);
e5bf3e1e 229
84915b0a 230 /* If the VRF is user configured, it'll stick around, just remove
231 * the ID mapping. Interfaces assigned to this VRF should've been
232 * removed already as part of the VRF going down.
233 */
234 if (vrf_is_user_cfged(vrf)) {
235 if (vrf->vrf_id != VRF_UNKNOWN) {
236 /* Delete any VRF interfaces - should be only
237 * the VRF itself, other interfaces should've
238 * been moved out of the VRF.
239 */
240 if_terminate(vrf);
241 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
242 vrf->vrf_id = VRF_UNKNOWN;
243 }
244 return;
245 }
246
d62a17ae 247 if (vrf_master.vrf_delete_hook)
248 (*vrf_master.vrf_delete_hook)(vrf);
216b18ef 249
d62a17ae 250 QOBJ_UNREG(vrf);
f4e14fdb 251 if_terminate(vrf);
b72ede27 252
d62a17ae 253 if (vrf->vrf_id != VRF_UNKNOWN)
254 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
255 if (vrf->name[0] != '\0')
256 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
b72ede27 257
d62a17ae 258 XFREE(MTYPE_VRF, vrf);
b72ede27
FL
259}
260
261/* Look up a VRF by identifier. */
d62a17ae 262struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
b72ede27 263{
d62a17ae 264 struct vrf vrf;
265 vrf.vrf_id = vrf_id;
266 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
b72ede27
FL
267}
268
e5bf3e1e
FL
269/*
270 * Enable a VRF - that is, let the VRF be ready to use.
271 * The VRF_ENABLE_HOOK callback will be called to inform
272 * that they can allocate resources in this VRF.
273 *
274 * RETURN: 1 - enabled successfully; otherwise, 0.
275 */
d62a17ae 276int vrf_enable(struct vrf *vrf)
e5bf3e1e 277{
d62a17ae 278 if (vrf_is_enabled(vrf))
279 return 1;
05e8e11e 280
d62a17ae 281 if (debug_vrf)
c7384cf8 282 zlog_debug("VRF %s(%u) is enabled.", vrf->name, vrf->vrf_id);
e5bf3e1e 283
d62a17ae 284 SET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 285
d62a17ae 286 if (vrf_master.vrf_enable_hook)
287 (*vrf_master.vrf_enable_hook)(vrf);
e5bf3e1e 288
98cbbaea
DS
289 /*
290 * If we have any nexthop group entries that
291 * are awaiting vrf initialization then
292 * let's let people know about it
293 */
294 nexthop_group_enable_vrf(vrf);
295
d62a17ae 296 return 1;
e5bf3e1e
FL
297}
298
299/*
300 * Disable a VRF - that is, let the VRF be unusable.
301 * The VRF_DELETE_HOOK callback will be called to inform
302 * that they must release the resources in the VRF.
303 */
697d3ec7 304void vrf_disable(struct vrf *vrf)
e5bf3e1e 305{
d62a17ae 306 if (!vrf_is_enabled(vrf))
307 return;
a647bfa8 308
d62a17ae 309 UNSET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 310
d62a17ae 311 if (debug_vrf)
c7384cf8
DS
312 zlog_debug("VRF %s(%u) is to be disabled.", vrf->name,
313 vrf->vrf_id);
e5bf3e1e 314
d62a17ae 315 /* Till now, nothing to be done for the default VRF. */
316 // Pending: see why this statement.
e74f14fc 317
0cbee799
DS
318
319 /*
320 * When the vrf is disabled let's
321 * handle all nexthop-groups associated
322 * with this vrf
323 */
324 nexthop_group_disable_vrf(vrf);
325
d62a17ae 326 if (vrf_master.vrf_disable_hook)
327 (*vrf_master.vrf_disable_hook)(vrf);
e5bf3e1e
FL
328}
329
b7cfce93
MK
330const char *vrf_id_to_name(vrf_id_t vrf_id)
331{
332 struct vrf *vrf;
333
7c1119cb
CG
334 if (vrf_id == VRF_DEFAULT)
335 return VRF_DEFAULT_NAME;
336
b7cfce93 337 vrf = vrf_lookup_by_id(vrf_id);
bd47f3a3 338 return VRF_LOGNAME(vrf);
b7cfce93
MK
339}
340
d62a17ae 341vrf_id_t vrf_name_to_id(const char *name)
216b18ef 342{
d62a17ae 343 struct vrf *vrf;
344 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
345 // id/ routine not used.
216b18ef 346
2569910b
PG
347 if (!name)
348 return vrf_id;
d62a17ae 349 vrf = vrf_lookup_by_name(name);
350 if (vrf)
351 vrf_id = vrf->vrf_id;
216b18ef 352
d62a17ae 353 return vrf_id;
216b18ef
DS
354}
355
b72ede27 356/* Get the data pointer of the specified VRF. If not found, create one. */
d62a17ae 357void *vrf_info_get(vrf_id_t vrf_id)
b72ede27 358{
d62a17ae 359 struct vrf *vrf = vrf_get(vrf_id, NULL);
360 return vrf->info;
b72ede27
FL
361}
362
363/* Look up the data pointer of the specified VRF. */
d62a17ae 364void *vrf_info_lookup(vrf_id_t vrf_id)
b72ede27 365{
d62a17ae 366 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
367 return vrf ? vrf->info : NULL;
b72ede27
FL
368}
369
7076bb2f 370/*
4a8bf858 371 * VRF hash for storing set or not.
7076bb2f 372 */
4a8bf858
DS
373struct vrf_bit_set {
374 vrf_id_t vrf_id;
375 bool set;
376};
7076bb2f 377
d8b87afe 378static unsigned int vrf_hash_bitmap_key(const void *data)
4a8bf858 379{
d8b87afe 380 const struct vrf_bit_set *bit = data;
d62a17ae 381
4a8bf858
DS
382 return bit->vrf_id;
383}
d62a17ae 384
74df8d6d 385static bool vrf_hash_bitmap_cmp(const void *a, const void *b)
4a8bf858
DS
386{
387 const struct vrf_bit_set *bit1 = a;
388 const struct vrf_bit_set *bit2 = b;
d62a17ae 389
4a8bf858
DS
390 return bit1->vrf_id == bit2->vrf_id;
391}
392
393static void *vrf_hash_bitmap_alloc(void *data)
394{
395 struct vrf_bit_set *copy = data;
396 struct vrf_bit_set *bit;
397
398 bit = XMALLOC(MTYPE_VRF_BITMAP, sizeof(*bit));
399 bit->vrf_id = copy->vrf_id;
400
401 return bit;
402}
403
404static void vrf_hash_bitmap_free(void *data)
405{
406 struct vrf_bit_set *bit = data;
407
408 XFREE(MTYPE_VRF_BITMAP, bit);
409}
7076bb2f 410
d62a17ae 411vrf_bitmap_t vrf_bitmap_init(void)
7076bb2f 412{
4a8bf858
DS
413 return hash_create_size(32, vrf_hash_bitmap_key, vrf_hash_bitmap_cmp,
414 "VRF BIT HASH");
7076bb2f
FL
415}
416
d62a17ae 417void vrf_bitmap_free(vrf_bitmap_t bmap)
7076bb2f 418{
4a8bf858 419 struct hash *vrf_hash = bmap;
7076bb2f 420
4a8bf858 421 if (vrf_hash == NULL)
d62a17ae 422 return;
7076bb2f 423
4a8bf858
DS
424 hash_clean(vrf_hash, vrf_hash_bitmap_free);
425 hash_free(vrf_hash);
7076bb2f
FL
426}
427
d62a17ae 428void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 429{
4a8bf858
DS
430 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
431 struct hash *vrf_hash = bmap;
432 struct vrf_bit_set *bit;
7076bb2f 433
4a8bf858 434 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
d62a17ae 435 return;
7076bb2f 436
4a8bf858
DS
437 bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc);
438 bit->set = true;
7076bb2f
FL
439}
440
d62a17ae 441void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 442{
4a8bf858
DS
443 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
444 struct hash *vrf_hash = bmap;
445 struct vrf_bit_set *bit;
7076bb2f 446
4a8bf858 447 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
d62a17ae 448 return;
7076bb2f 449
4a8bf858
DS
450 bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc);
451 bit->set = false;
7076bb2f
FL
452}
453
d62a17ae 454int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 455{
4a8bf858
DS
456 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
457 struct hash *vrf_hash = bmap;
458 struct vrf_bit_set *bit;
7076bb2f 459
4a8bf858 460 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
d62a17ae 461 return 0;
7076bb2f 462
4a8bf858
DS
463 bit = hash_lookup(vrf_hash, &lookup);
464 if (bit)
465 return bit->set;
466
467 return 0;
7076bb2f
FL
468}
469
d62a17ae 470static void vrf_autocomplete(vector comps, struct cmd_token *token)
d617d5fe 471{
d62a17ae 472 struct vrf *vrf = NULL;
d617d5fe 473
723001fc
PG
474 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
475 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
d617d5fe
DS
476}
477
478static const struct cmd_variable_handler vrf_var_handlers[] = {
d62a17ae 479 {
480 .varname = "vrf",
481 .completions = vrf_autocomplete,
482 },
e429a2a0
IR
483 {
484 .varname = "vrf_name",
485 .completions = vrf_autocomplete,
486 },
487 {
488 .varname = "nexthop_vrf",
489 .completions = vrf_autocomplete,
490 },
d62a17ae 491 {.completions = NULL},
d617d5fe
DS
492};
493
b72ede27 494/* Initialize VRF module. */
d62a17ae 495void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
d01b92fd 496 int (*disable)(struct vrf *), int (*destroy)(struct vrf *),
ecbc5a37 497 int ((*update)(struct vrf *)))
d62a17ae 498{
499 struct vrf *default_vrf;
500
e26aedbe
PG
501 /* initialise NS, in case VRF backend if NETNS */
502 ns_init();
d62a17ae 503 if (debug_vrf)
15569c58 504 zlog_debug("%s: Initializing VRF subsystem", __func__);
d62a17ae 505
506 vrf_master.vrf_new_hook = create;
507 vrf_master.vrf_enable_hook = enable;
508 vrf_master.vrf_disable_hook = disable;
d01b92fd 509 vrf_master.vrf_delete_hook = destroy;
ecbc5a37 510 vrf_master.vrf_update_name_hook = update;
d62a17ae 511
512 /* The default VRF always exists. */
eb6934d5 513 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
d62a17ae 514 if (!default_vrf) {
450971aa 515 flog_err(EC_LIB_VRF_START,
1c50c1c0 516 "vrf_init: failed to create the default VRF!");
d62a17ae 517 exit(1);
518 }
20c87e98
TC
519 if (vrf_is_backend_netns()) {
520 struct ns *ns;
521
fdafe17e 522 strlcpy(default_vrf->data.l.netns_name,
eb6934d5 523 VRF_DEFAULT_NAME, NS_NAMSIZ);
20c87e98 524 ns = ns_lookup(ns_get_default_id());
c3568c4d
TC
525 ns->vrf_ctxt = default_vrf;
526 default_vrf->ns_ctxt = ns;
20c87e98 527 }
d62a17ae 528
529 /* Enable the default VRF. */
530 if (!vrf_enable(default_vrf)) {
450971aa 531 flog_err(EC_LIB_VRF_START,
1c50c1c0 532 "vrf_init: failed to enable the default VRF!");
d62a17ae 533 exit(1);
534 }
535
536 cmd_variable_handler_register(vrf_var_handlers);
b72ede27
FL
537}
538
539/* Terminate VRF module. */
d62a17ae 540void vrf_terminate(void)
b72ede27 541{
d62a17ae 542 struct vrf *vrf;
b72ede27 543
d62a17ae 544 if (debug_vrf)
15569c58 545 zlog_debug("%s: Shutting down vrf subsystem", __func__);
19dc275e 546
55cd0f61
DS
547 while (!RB_EMPTY(vrf_id_head, &vrfs_by_id)) {
548 vrf = RB_ROOT(vrf_id_head, &vrfs_by_id);
549
65c3a7c4 550 /* Clear configured flag and invoke delete. */
551 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
d62a17ae 552 vrf_delete(vrf);
65c3a7c4 553 }
55cd0f61
DS
554
555 while (!RB_EMPTY(vrf_name_head, &vrfs_by_name)) {
556 vrf = RB_ROOT(vrf_name_head, &vrfs_by_name);
557
65c3a7c4 558 /* Clear configured flag and invoke delete. */
559 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
d62a17ae 560 vrf_delete(vrf);
65c3a7c4 561 }
b72ede27
FL
562}
563
0f4977c6 564int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id,
02fe07c7 565 const char *interfacename)
e5bf3e1e 566{
2e0d2b3d 567 int ret, save_errno, ret2;
e5bf3e1e 568
2e0d2b3d
PG
569 ret = vrf_switch_to_netns(vrf_id);
570 if (ret < 0)
450971aa 571 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 572 __func__, vrf_id, safe_strerror(errno));
b66d022e 573
d62a17ae 574 ret = socket(domain, type, protocol);
2e0d2b3d
PG
575 save_errno = errno;
576 ret2 = vrf_switchback_to_initial();
577 if (ret2 < 0)
450971aa 578 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
579 "%s: Can't switchback from VRF %u (%s)", __func__,
580 vrf_id, safe_strerror(errno));
2e0d2b3d 581 errno = save_errno;
0f4977c6
PG
582 if (ret <= 0)
583 return ret;
584 ret2 = vrf_bind(vrf_id, ret, interfacename);
585 if (ret2 < 0) {
586 close(ret);
587 ret = ret2;
588 }
d62a17ae 589 return ret;
e5bf3e1e
FL
590}
591
78dd30b2
PG
592int vrf_is_backend_netns(void)
593{
594 return (vrf_backend == VRF_BACKEND_NETNS);
595}
596
597int vrf_get_backend(void)
598{
72261ecd
PG
599 if (!vrf_backend_configured)
600 return VRF_BACKEND_UNKNOWN;
78dd30b2
PG
601 return vrf_backend;
602}
603
7239d3d9 604int vrf_configure_backend(enum vrf_backend_type backend)
78dd30b2 605{
f7d45925
QY
606 /* Work around issue in old gcc */
607 switch (backend) {
608 case VRF_BACKEND_UNKNOWN:
609 case VRF_BACKEND_NETNS:
610 case VRF_BACKEND_VRF_LITE:
611 break;
612 default:
7239d3d9 613 return -1;
f7d45925 614 }
7239d3d9
QY
615
616 vrf_backend = backend;
72261ecd 617 vrf_backend_configured = 1;
7239d3d9
QY
618
619 return 0;
78dd30b2
PG
620}
621
03aff2d8
PG
622int vrf_handler_create(struct vty *vty, const char *vrfname,
623 struct vrf **vrf)
f30c50b9 624{
d62a17ae 625 struct vrf *vrfp;
8b4cb7a6
CS
626 char xpath_list[XPATH_MAXLEN];
627 int ret;
f30c50b9 628
d62a17ae 629 if (strlen(vrfname) > VRF_NAMSIZ) {
697d3ec7
PG
630 if (vty)
631 vty_out(vty,
996c9314
LB
632 "%% VRF name %s invalid: length exceeds %d bytes\n",
633 vrfname, VRF_NAMSIZ);
697d3ec7 634 else
0351a28f 635 flog_warn(
450971aa 636 EC_LIB_VRF_LENGTH,
996c9314
LB
637 "%% VRF name %s invalid: length exceeds %d bytes\n",
638 vrfname, VRF_NAMSIZ);
d62a17ae 639 return CMD_WARNING_CONFIG_FAILED;
640 }
f30c50b9 641
8b4cb7a6
CS
642 if (vty) {
643 snprintf(xpath_list, sizeof(xpath_list),
644 "/frr-vrf:lib/vrf[name='%s']", vrfname);
645
646 nb_cli_enqueue_change(vty, xpath_list, NB_OP_CREATE, NULL);
647 ret = nb_cli_apply_changes(vty, xpath_list);
648 if (ret == CMD_SUCCESS) {
649 VTY_PUSH_XPATH(VRF_NODE, xpath_list);
b855e95f 650 nb_cli_pending_commit_check(vty);
8b4cb7a6
CS
651 vrfp = vrf_lookup_by_name(vrfname);
652 if (vrfp)
653 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
654 }
655 } else {
656 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
f30c50b9 657
8b4cb7a6
CS
658 if (vrf)
659 *vrf = vrfp;
660 }
d62a17ae 661 return CMD_SUCCESS;
f30c50b9
RW
662}
663
996c9314 664int vrf_netns_handler_create(struct vty *vty, struct vrf *vrf, char *pathname,
20f4b2b0
PG
665 ns_id_t ns_id, ns_id_t internal_ns_id,
666 ns_id_t rel_def_ns_id)
e26aedbe
PG
667{
668 struct ns *ns = NULL;
669
670 if (!vrf)
671 return CMD_WARNING_CONFIG_FAILED;
672 if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
673 if (vty)
674 vty_out(vty,
675 "VRF %u is already configured with VRF %s\n",
676 vrf->vrf_id, vrf->name);
677 else
9165c5f5 678 zlog_info("VRF %u is already configured with VRF %s",
e26aedbe
PG
679 vrf->vrf_id, vrf->name);
680 return CMD_WARNING_CONFIG_FAILED;
681 }
682 if (vrf->ns_ctxt != NULL) {
996c9314 683 ns = (struct ns *)vrf->ns_ctxt;
2e1cc436 684 if (!strcmp(ns->name, pathname)) {
e26aedbe
PG
685 if (vty)
686 vty_out(vty,
996c9314
LB
687 "VRF %u already configured with NETNS %s\n",
688 vrf->vrf_id, ns->name);
e26aedbe 689 else
0351a28f 690 zlog_info(
ade6974d
QY
691 "VRF %u already configured with NETNS %s",
692 vrf->vrf_id, ns->name);
e26aedbe
PG
693 return CMD_WARNING_CONFIG_FAILED;
694 }
695 }
696 ns = ns_lookup_name(pathname);
697 if (ns && ns->vrf_ctxt) {
698 struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;
699
700 if (vrf2 == vrf)
701 return CMD_SUCCESS;
702 if (vty)
996c9314 703 vty_out(vty,
3efd0893 704 "NS %s is already configured with VRF %u(%s)\n",
996c9314 705 ns->name, vrf2->vrf_id, vrf2->name);
e26aedbe 706 else
0351a28f 707 zlog_info("NS %s is already configured with VRF %u(%s)",
e26aedbe
PG
708 ns->name, vrf2->vrf_id, vrf2->name);
709 return CMD_WARNING_CONFIG_FAILED;
710 }
711 ns = ns_get_created(ns, pathname, ns_id);
03aff2d8 712 ns->internal_ns_id = internal_ns_id;
20f4b2b0 713 ns->relative_default_ns = rel_def_ns_id;
e26aedbe
PG
714 ns->vrf_ctxt = (void *)vrf;
715 vrf->ns_ctxt = (void *)ns;
716 /* update VRF netns NAME */
2e1cc436 717 strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ);
e26aedbe
PG
718
719 if (!ns_enable(ns, vrf_update_vrf_id)) {
720 if (vty)
721 vty_out(vty, "Can not associate NS %u with NETNS %s\n",
996c9314 722 ns->ns_id, ns->name);
e26aedbe 723 else
0351a28f 724 zlog_info("Can not associate NS %u with NETNS %s",
e26aedbe
PG
725 ns->ns_id, ns->name);
726 return CMD_WARNING_CONFIG_FAILED;
727 }
728
729 return CMD_SUCCESS;
730}
731
697d3ec7 732/* vrf CLI commands */
16d6ea59
QY
733DEFUN_NOSH(vrf_exit,
734 vrf_exit_cmd,
735 "exit-vrf",
736 "Exit current mode and down to previous mode\n")
737{
738 /* We have to set vrf context to default vrf */
739 VTY_PUSH_CONTEXT(VRF_NODE, vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME));
799a81df 740 cmd_exit(vty);
16d6ea59
QY
741 return CMD_SUCCESS;
742}
743
ca77b518 744DEFUN_YANG_NOSH (vrf,
697d3ec7
PG
745 vrf_cmd,
746 "vrf NAME",
747 "Select a VRF to configure\n"
748 "VRF's name\n")
749{
750 int idx_name = 1;
751 const char *vrfname = argv[idx_name]->arg;
752
753 return vrf_handler_create(vty, vrfname, NULL);
754}
755
ca77b518 756DEFUN_YANG (no_vrf,
34c46274
RW
757 no_vrf_cmd,
758 "no vrf NAME",
759 NO_STR
760 "Delete a pseudo VRF's configuration\n"
761 "VRF's name\n")
f30c50b9 762{
d62a17ae 763 const char *vrfname = argv[2]->arg;
8b4cb7a6 764 char xpath_list[XPATH_MAXLEN];
53dc2b05 765
d62a17ae 766 struct vrf *vrfp;
f30c50b9 767
d62a17ae 768 vrfp = vrf_lookup_by_name(vrfname);
f30c50b9 769
cd980d03
IR
770 if (vrfp == NULL)
771 return CMD_SUCCESS;
f30c50b9 772
d62a17ae 773 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
774 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
775 return CMD_WARNING_CONFIG_FAILED;
776 }
f30c50b9 777
8b4cb7a6
CS
778 snprintf(xpath_list, sizeof(xpath_list), "/frr-vrf:lib/vrf[name='%s']",
779 vrfname);
f30c50b9 780
8b4cb7a6
CS
781 nb_cli_enqueue_change(vty, xpath_list, NB_OP_DESTROY, NULL);
782 return nb_cli_apply_changes(vty, xpath_list);
f30c50b9
RW
783}
784
53dc2b05 785
62b346ee 786static struct cmd_node vrf_node = {
f4b8291f 787 .name = "vrf",
62b346ee 788 .node = VRF_NODE,
24389580 789 .parent_node = CONFIG_NODE,
62b346ee 790 .prompt = "%s(config-vrf)# ",
62b346ee 791};
7ddcfca4 792
34c46274 793DEFUN_NOSH (vrf_netns,
4a541e8c
PG
794 vrf_netns_cmd,
795 "netns NAME",
796 "Attach VRF to a Namespace\n"
797 "The file name in " NS_RUN_DIR ", or a full pathname\n")
e26aedbe 798{
3bc34908 799 int idx_name = 1, ret;
e26aedbe
PG
800 char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg);
801
802 VTY_DECLVAR_CONTEXT(vrf, vrf);
803
804 if (!pathname)
805 return CMD_WARNING_CONFIG_FAILED;
3bc34908 806
0cf6db21 807 frr_with_privs(vrf_daemon_privs) {
6bb30c2c 808 ret = vrf_netns_handler_create(vty, vrf, pathname,
20f4b2b0
PG
809 NS_UNKNOWN,
810 NS_UNKNOWN,
811 NS_UNKNOWN);
6bb30c2c 812 }
3bc34908 813 return ret;
e26aedbe
PG
814}
815
34c46274 816DEFUN_NOSH (no_vrf_netns,
e26aedbe
PG
817 no_vrf_netns_cmd,
818 "no netns [NAME]",
819 NO_STR
820 "Detach VRF from a Namespace\n"
821 "The file name in " NS_RUN_DIR ", or a full pathname\n")
822{
823 struct ns *ns = NULL;
824
825 VTY_DECLVAR_CONTEXT(vrf, vrf);
826
827 if (!vrf_is_backend_netns()) {
828 vty_out(vty, "VRF backend is not Netns. Aborting\n");
829 return CMD_WARNING_CONFIG_FAILED;
830 }
831 if (!vrf->ns_ctxt) {
832 vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
833 vrf->name, vrf->vrf_id);
834 return CMD_WARNING_CONFIG_FAILED;
835 }
836
837 ns = (struct ns *)vrf->ns_ctxt;
838
839 ns->vrf_ctxt = NULL;
840 vrf_disable(vrf);
841 /* vrf ID from VRF is necessary for Zebra
842 * so that propagate to other clients is done
843 */
844 ns_delete(ns);
845 vrf->ns_ctxt = NULL;
846 return CMD_SUCCESS;
847}
848
19dc275e
DS
849/*
850 * Debug CLI for vrf's
851 */
852DEFUN (vrf_debug,
853 vrf_debug_cmd,
854 "debug vrf",
855 DEBUG_STR
856 "VRF Debugging\n")
857{
d62a17ae 858 debug_vrf = 1;
19dc275e 859
d62a17ae 860 return CMD_SUCCESS;
19dc275e
DS
861}
862
863DEFUN (no_vrf_debug,
864 no_vrf_debug_cmd,
865 "no debug vrf",
866 NO_STR
867 DEBUG_STR
868 "VRF Debugging\n")
869{
d62a17ae 870 debug_vrf = 0;
19dc275e 871
d62a17ae 872 return CMD_SUCCESS;
19dc275e
DS
873}
874
d62a17ae 875static int vrf_write_host(struct vty *vty)
19dc275e 876{
d62a17ae 877 if (debug_vrf)
878 vty_out(vty, "debug vrf\n");
19dc275e 879
d62a17ae 880 return 1;
19dc275e
DS
881}
882
612c2c15 883static int vrf_write_host(struct vty *vty);
62b346ee 884static struct cmd_node vrf_debug_node = {
f4b8291f 885 .name = "vrf debug",
62b346ee
DL
886 .node = VRF_DEBUG_NODE,
887 .prompt = "",
612c2c15 888 .config_write = vrf_write_host,
62b346ee 889};
19dc275e 890
d62a17ae 891void vrf_install_commands(void)
19dc275e 892{
612c2c15 893 install_node(&vrf_debug_node);
19dc275e 894
d62a17ae 895 install_element(CONFIG_NODE, &vrf_debug_cmd);
896 install_element(ENABLE_NODE, &vrf_debug_cmd);
897 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
898 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
19dc275e 899}
53dc2b05 900
3bc34908
PG
901void vrf_cmd_init(int (*writefunc)(struct vty *vty),
902 struct zebra_privs_t *daemon_privs)
7ddcfca4 903{
d62a17ae 904 install_element(CONFIG_NODE, &vrf_cmd);
905 install_element(CONFIG_NODE, &no_vrf_cmd);
612c2c15
DL
906 vrf_node.config_write = writefunc;
907 install_node(&vrf_node);
d62a17ae 908 install_default(VRF_NODE);
16d6ea59 909 install_element(VRF_NODE, &vrf_exit_cmd);
e26aedbe
PG
910 if (vrf_is_backend_netns() && ns_have_netns()) {
911 /* Install NS commands. */
3bc34908 912 vrf_daemon_privs = daemon_privs;
e26aedbe
PG
913 install_element(VRF_NODE, &vrf_netns_cmd);
914 install_element(VRF_NODE, &no_vrf_netns_cmd);
915 }
19dc275e 916}
ec31f30d 917
4fe52e76 918void vrf_set_default_name(const char *default_name, bool force)
ec31f30d 919{
c200f5e1 920 struct vrf *def_vrf;
4fe52e76 921 static bool def_vrf_forced;
ec31f30d 922
c200f5e1
PG
923 def_vrf = vrf_lookup_by_id(VRF_DEFAULT);
924 assert(default_name);
4fe52e76
PG
925 if (def_vrf && !force && def_vrf_forced) {
926 zlog_debug("VRF: %s, avoid changing name to %s, previously forced (%u)",
927 def_vrf->name, default_name,
928 def_vrf->vrf_id);
929 return;
930 }
87272aff
PG
931 if (strmatch(vrf_default_name, default_name))
932 return;
c200f5e1
PG
933 snprintf(vrf_default_name, VRF_NAMSIZ, "%s", default_name);
934 if (def_vrf) {
4fe52e76
PG
935 if (force)
936 def_vrf_forced = true;
c200f5e1
PG
937 RB_REMOVE(vrf_name_head, &vrfs_by_name, def_vrf);
938 strlcpy(def_vrf->data.l.netns_name,
939 vrf_default_name, NS_NAMSIZ);
940 strlcpy(def_vrf->name, vrf_default_name, sizeof(def_vrf->name));
941 RB_INSERT(vrf_name_head, &vrfs_by_name, def_vrf);
ecbc5a37
PG
942 if (vrf_master.vrf_update_name_hook)
943 (*vrf_master.vrf_update_name_hook)(def_vrf);
c200f5e1
PG
944 }
945}
946
947const char *vrf_get_default_name(void)
948{
949 return vrf_default_name;
950}
951
952vrf_id_t vrf_get_default_id(void)
953{
03aff2d8
PG
954 /* backend netns is only known by zebra
955 * for other daemons, we return VRF_DEFAULT_INTERNAL
956 */
ec31f30d
PG
957 if (vrf_is_backend_netns())
958 return ns_get_default_id();
959 else
960 return VRF_DEFAULT_INTERNAL;
961}
2e0d2b3d 962
02fe07c7 963int vrf_bind(vrf_id_t vrf_id, int fd, const char *name)
0f4977c6
PG
964{
965 int ret = 0;
91f854f6 966 struct interface *ifp;
0f4977c6 967
a36898e7 968 if (fd < 0 || name == NULL)
0f4977c6 969 return fd;
91f854f6
PG
970 /* the device should exist
971 * otherwise we should return
972 * case ifname = vrf in netns mode => return
973 */
a36898e7 974 ifp = if_lookup_by_name(name, vrf_id);
91f854f6 975 if (!ifp)
0f4977c6
PG
976 return fd;
977#ifdef SO_BINDTODEVICE
c9c70dd1 978 ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name)+1);
0f4977c6 979 if (ret < 0)
996c9314
LB
980 zlog_debug("bind to interface %s failed, errno=%d", name,
981 errno);
0f4977c6
PG
982#endif /* SO_BINDTODEVICE */
983 return ret;
984}
2e0d2b3d 985int vrf_getaddrinfo(const char *node, const char *service,
996c9314
LB
986 const struct addrinfo *hints, struct addrinfo **res,
987 vrf_id_t vrf_id)
2e0d2b3d
PG
988{
989 int ret, ret2, save_errno;
990
991 ret = vrf_switch_to_netns(vrf_id);
992 if (ret < 0)
450971aa 993 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 994 __func__, vrf_id, safe_strerror(errno));
2e0d2b3d
PG
995 ret = getaddrinfo(node, service, hints, res);
996 save_errno = errno;
997 ret2 = vrf_switchback_to_initial();
998 if (ret2 < 0)
450971aa 999 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1000 "%s: Can't switchback from VRF %u (%s)", __func__,
1001 vrf_id, safe_strerror(errno));
2e0d2b3d
PG
1002 errno = save_errno;
1003 return ret;
1004}
1005
516d7591
PG
1006int vrf_ioctl(vrf_id_t vrf_id, int d, unsigned long request, char *params)
1007{
1008 int ret, saved_errno, rc;
1009
1010 ret = vrf_switch_to_netns(vrf_id);
1011 if (ret < 0) {
450971aa 1012 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 1013 __func__, vrf_id, safe_strerror(errno));
516d7591
PG
1014 return 0;
1015 }
1016 rc = ioctl(d, request, params);
1017 saved_errno = errno;
1018 ret = vrf_switchback_to_initial();
1019 if (ret < 0)
450971aa 1020 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1021 "%s: Can't switchback from VRF %u (%s)", __func__,
1022 vrf_id, safe_strerror(errno));
516d7591
PG
1023 errno = saved_errno;
1024 return rc;
1025}
1026
0f4977c6 1027int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id,
02fe07c7 1028 const char *interfacename)
2e0d2b3d
PG
1029{
1030 int ret, save_errno, ret2;
1031
1032 ret = vrf_switch_to_netns(vrf_id);
1033 if (ret < 0)
450971aa 1034 flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)",
09c866e3 1035 __func__, vrf_id, safe_strerror(errno));
2e0d2b3d
PG
1036 ret = sockunion_socket(su);
1037 save_errno = errno;
1038 ret2 = vrf_switchback_to_initial();
1039 if (ret2 < 0)
450971aa 1040 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1041 "%s: Can't switchback from VRF %u (%s)", __func__,
1042 vrf_id, safe_strerror(errno));
2e0d2b3d 1043 errno = save_errno;
0f4977c6
PG
1044
1045 if (ret <= 0)
1046 return ret;
1047 ret2 = vrf_bind(vrf_id, ret, interfacename);
1048 if (ret2 < 0) {
1049 close(ret);
1050 ret = ret2;
1051 }
2e0d2b3d
PG
1052 return ret;
1053}
0b014ea6
PG
1054
1055vrf_id_t vrf_generate_id(void)
1056{
1057 static int vrf_id_local;
1058
1059 return ++vrf_id_local;
1060}
bc867a5d
CS
1061
1062/* ------- Northbound callbacks ------- */
1063
1064/*
1065 * XPath: /frr-vrf:lib/vrf
1066 */
60ee8be1 1067static int lib_vrf_create(struct nb_cb_create_args *args)
bc867a5d
CS
1068{
1069 const char *vrfname;
1070 struct vrf *vrfp;
1071
60ee8be1 1072 vrfname = yang_dnode_get_string(args->dnode, "./name");
bc867a5d 1073
60ee8be1 1074 if (args->event != NB_EV_APPLY)
bc867a5d
CS
1075 return NB_OK;
1076
1077 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
1078
60ee8be1 1079 nb_running_set_entry(args->dnode, vrfp);
bc867a5d
CS
1080
1081 return NB_OK;
1082}
1083
60ee8be1 1084static int lib_vrf_destroy(struct nb_cb_destroy_args *args)
bc867a5d
CS
1085{
1086 struct vrf *vrfp;
1087
60ee8be1 1088 switch (args->event) {
bc867a5d 1089 case NB_EV_VALIDATE:
60ee8be1 1090 vrfp = nb_running_get_entry(args->dnode, NULL, true);
bc867a5d 1091 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
10bdc68f
RW
1092 snprintf(args->errmsg, args->errmsg_len,
1093 "Only inactive VRFs can be deleted");
bc867a5d
CS
1094 return NB_ERR_VALIDATION;
1095 }
1096 break;
1097 case NB_EV_PREPARE:
1098 case NB_EV_ABORT:
1099 break;
1100 case NB_EV_APPLY:
60ee8be1 1101 vrfp = nb_running_unset_entry(args->dnode);
8b4cb7a6 1102
bc867a5d
CS
1103 /* Clear configured flag and invoke delete. */
1104 UNSET_FLAG(vrfp->status, VRF_CONFIGURED);
1105 vrf_delete(vrfp);
1106 break;
1107 }
1108
1109 return NB_OK;
1110}
1111
60ee8be1 1112static const void *lib_vrf_get_next(struct nb_cb_get_next_args *args)
bc867a5d 1113{
60ee8be1 1114 struct vrf *vrfp = (struct vrf *)args->list_entry;
bc867a5d 1115
60ee8be1 1116 if (args->list_entry == NULL) {
bc867a5d
CS
1117 vrfp = RB_MIN(vrf_name_head, &vrfs_by_name);
1118 } else {
1119 vrfp = RB_NEXT(vrf_name_head, vrfp);
1120 }
1121
1122 return vrfp;
1123}
1124
60ee8be1 1125static int lib_vrf_get_keys(struct nb_cb_get_keys_args *args)
bc867a5d 1126{
60ee8be1 1127 struct vrf *vrfp = (struct vrf *)args->list_entry;
bc867a5d 1128
60ee8be1
RW
1129 args->keys->num = 1;
1130 strlcpy(args->keys->key[0], vrfp->name, sizeof(args->keys->key[0]));
bc867a5d
CS
1131
1132 return NB_OK;
1133}
1134
60ee8be1 1135static const void *lib_vrf_lookup_entry(struct nb_cb_lookup_entry_args *args)
bc867a5d 1136{
60ee8be1 1137 const char *vrfname = args->keys->key[0];
bc867a5d
CS
1138
1139 struct vrf *vrf = vrf_lookup_by_name(vrfname);
1140
1141 return vrf;
1142}
1143
1144/*
1145 * XPath: /frr-vrf:lib/vrf/id
1146 */
60ee8be1
RW
1147static struct yang_data *
1148lib_vrf_state_id_get_elem(struct nb_cb_get_elem_args *args)
bc867a5d 1149{
60ee8be1 1150 struct vrf *vrfp = (struct vrf *)args->list_entry;
bc867a5d 1151
60ee8be1 1152 return yang_data_new_uint32(args->xpath, vrfp->vrf_id);
bc867a5d
CS
1153}
1154
1155/*
1156 * XPath: /frr-vrf:lib/vrf/active
1157 */
60ee8be1
RW
1158static struct yang_data *
1159lib_vrf_state_active_get_elem(struct nb_cb_get_elem_args *args)
bc867a5d 1160{
60ee8be1 1161 struct vrf *vrfp = (struct vrf *)args->list_entry;
bc867a5d
CS
1162
1163 if (vrfp->status == VRF_ACTIVE)
1164 return yang_data_new_bool(
60ee8be1 1165 args->xpath, vrfp->status == VRF_ACTIVE ? true : false);
bc867a5d
CS
1166
1167 return NULL;
1168}
1169
1170/* clang-format off */
1171const struct frr_yang_module_info frr_vrf_info = {
1172 .name = "frr-vrf",
1173 .nodes = {
1174 {
1175 .xpath = "/frr-vrf:lib/vrf",
1176 .cbs = {
1177 .create = lib_vrf_create,
1178 .destroy = lib_vrf_destroy,
1179 .get_next = lib_vrf_get_next,
1180 .get_keys = lib_vrf_get_keys,
1181 .lookup_entry = lib_vrf_lookup_entry,
1182 }
1183 },
1184 {
1185 .xpath = "/frr-vrf:lib/vrf/state/id",
1186 .cbs = {
1187 .get_elem = lib_vrf_state_id_get_elem,
1188 }
1189 },
1190 {
1191 .xpath = "/frr-vrf:lib/vrf/state/active",
1192 .cbs = {
1193 .get_elem = lib_vrf_state_active_get_elem,
1194 }
1195 },
1196 {
1197 .xpath = NULL,
1198 },
1199 }
1200};
1201