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