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