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