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