3 * Copyright (C) 2014 6WIND S.A.
5 * This file is part of GNU Zebra.
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.
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.
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
37 #include "nexthop_group.h"
38 #include "lib_errors.h"
39 #include "northbound.h"
40 #include "northbound_cli.h"
42 /* default VRF name value used when VRF backend is not NETNS */
43 #define VRF_DEFAULT_NAME_INTERNAL "default"
45 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF");
46 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map");
48 DEFINE_QOBJ_TYPE(vrf
);
50 static __inline
int vrf_id_compare(const struct vrf
*, const struct vrf
*);
51 static __inline
int vrf_name_compare(const struct vrf
*, const struct vrf
*);
53 RB_GENERATE(vrf_id_head
, vrf
, id_entry
, vrf_id_compare
);
54 RB_GENERATE(vrf_name_head
, vrf
, name_entry
, vrf_name_compare
);
56 struct vrf_id_head vrfs_by_id
= RB_INITIALIZER(&vrfs_by_id
);
57 struct vrf_name_head vrfs_by_name
= RB_INITIALIZER(&vrfs_by_name
);
59 static int vrf_backend
;
60 static int vrf_backend_configured
;
61 static char vrf_default_name
[VRF_NAMSIZ
] = VRF_DEFAULT_NAME_INTERNAL
;
64 * Turn on/off debug code
67 static int debug_vrf
= 0;
69 /* Holding VRF hooks */
70 static struct vrf_master
{
71 int (*vrf_new_hook
)(struct vrf
*);
72 int (*vrf_delete_hook
)(struct vrf
*);
73 int (*vrf_enable_hook
)(struct vrf
*);
74 int (*vrf_disable_hook
)(struct vrf
*);
75 int (*vrf_update_name_hook
)(struct vrf
*vrf
);
80 static int vrf_is_enabled(struct vrf
*vrf
);
82 /* VRF list existance check by name. */
83 struct vrf
*vrf_lookup_by_name(const char *name
)
86 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
87 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
90 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
92 return (a
->vrf_id
- b
->vrf_id
);
95 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
97 return strcmp(a
->name
, b
->name
);
100 /* if ns_id is different and not VRF_UNKNOWN,
101 * then update vrf identifier, and enable VRF
103 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
105 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
107 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
111 old_vrf_id
= vrf
->vrf_id
;
112 if (vrf_id
== vrf
->vrf_id
)
114 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
115 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
116 vrf
->vrf_id
= vrf_id
;
117 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
118 if (old_vrf_id
== VRF_UNKNOWN
)
122 int vrf_switch_to_netns(vrf_id_t vrf_id
)
125 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
127 /* VRF is default VRF. silently ignore */
128 if (!vrf
|| vrf
->vrf_id
== VRF_DEFAULT
)
129 return 1; /* 1 = default */
130 /* VRF has no NETNS backend. silently ignore */
131 if (vrf
->data
.l
.netns_name
[0] == '\0')
132 return 2; /* 2 = no netns */
133 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
135 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
136 return ns_switch_to_netns(name
);
139 int vrf_switchback_to_initial(void)
141 int ret
= ns_switchback_to_initial();
143 if (ret
== 0 && debug_vrf
)
144 zlog_debug("VRF_SWITCHBACK");
148 /* Get a VRF. If not found, create one.
150 * name - The name of the vrf. May be NULL if unknown.
151 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
152 * Description: Please note that this routine can be called with just the name
155 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
157 struct vrf
*vrf
= NULL
;
160 /* Nothing to see, move along here */
161 if (!name
&& vrf_id
== VRF_UNKNOWN
)
164 /* attempt to find already available VRF
167 vrf
= vrf_lookup_by_name(name
);
168 if (vrf
&& vrf_id
!= VRF_UNKNOWN
169 && vrf
->vrf_id
!= VRF_UNKNOWN
170 && vrf
->vrf_id
!= vrf_id
) {
171 zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)",
172 name
, vrf_id
, vrf
->vrf_id
);
175 /* Try to find VRF both by ID and name */
176 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
177 vrf
= vrf_lookup_by_id(vrf_id
);
180 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
181 vrf
->vrf_id
= VRF_UNKNOWN
;
186 zlog_debug("VRF(%u) %s is created.", vrf_id
,
187 (name
) ? name
: "(NULL)");
191 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
192 vrf
->vrf_id
= vrf_id
;
193 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
197 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
198 /* update the vrf name */
199 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
200 strlcpy(vrf
->data
.l
.netns_name
,
202 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
203 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
204 if (vrf
->vrf_id
== VRF_DEFAULT
)
205 vrf_set_default_name(vrf
->name
, false);
206 } else if (name
&& vrf
->name
[0] == '\0') {
207 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
208 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
210 if (new &&vrf_master
.vrf_new_hook
)
211 (*vrf_master
.vrf_new_hook
)(vrf
);
216 /* Update a VRF. If not found, create one.
218 * name - The name of the vrf.
219 * vrf_id - The vrf_id of the vrf.
220 * Description: This function first finds the vrf using its name. If the vrf is
221 * found and the vrf-id of the existing vrf does not match the new vrf id, it
222 * will disable the existing vrf and update it with new vrf-id. If the vrf is
223 * not found, it will create the vrf with given name and the new vrf id.
225 struct vrf
*vrf_update(vrf_id_t new_vrf_id
, const char *name
)
227 struct vrf
*vrf
= NULL
;
229 /*Treat VRF add for existing vrf as update
230 * Update VRF ID and also update in VRF ID table
233 vrf
= vrf_lookup_by_name(name
);
234 if (vrf
&& new_vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
!= VRF_UNKNOWN
235 && vrf
->vrf_id
!= new_vrf_id
) {
238 "Vrf Update event: %s old id: %u, new id: %u",
239 name
, vrf
->vrf_id
, new_vrf_id
);
242 /*Disable the vrf to simulate implicit delete
243 * so that all stale routes are deleted
244 * This vrf will be enabled down the line
249 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
250 vrf
->vrf_id
= new_vrf_id
;
251 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
256 * vrf_get is implied creation if it does not exist
258 vrf
= vrf_get(new_vrf_id
, name
);
263 /* Delete a VRF. This is called when the underlying VRF goes away, a
264 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
266 void vrf_delete(struct vrf
*vrf
)
269 zlog_debug("VRF %s(%u) is to be deleted.", vrf
->name
,
272 if (vrf_is_enabled(vrf
))
275 /* If the VRF is user configured, it'll stick around, just remove
276 * the ID mapping. Interfaces assigned to this VRF should've been
277 * removed already as part of the VRF going down.
279 if (vrf_is_user_cfged(vrf
)) {
280 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
281 /* Delete any VRF interfaces - should be only
282 * the VRF itself, other interfaces should've
283 * been moved out of the VRF.
286 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
287 vrf
->vrf_id
= VRF_UNKNOWN
;
293 if (vrf_master
.vrf_delete_hook
)
294 (*vrf_master
.vrf_delete_hook
)(vrf
);
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
);
304 XFREE(MTYPE_VRF
, vrf
);
307 /* Look up a VRF by identifier. */
308 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
312 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
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.
320 * RETURN: 1 - enabled successfully; otherwise, 0.
322 int vrf_enable(struct vrf
*vrf
)
324 if (vrf_is_enabled(vrf
))
328 zlog_debug("VRF %s(%u) is enabled.", vrf
->name
, vrf
->vrf_id
);
330 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
332 if (vrf_master
.vrf_enable_hook
)
333 (*vrf_master
.vrf_enable_hook
)(vrf
);
336 * If we have any nexthop group entries that
337 * are awaiting vrf initialization then
338 * let's let people know about it
340 nexthop_group_enable_vrf(vrf
);
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.
350 void vrf_disable(struct vrf
*vrf
)
352 if (!vrf_is_enabled(vrf
))
355 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
358 zlog_debug("VRF %s(%u) is to be disabled.", vrf
->name
,
361 /* Till now, nothing to be done for the default VRF. */
362 // Pending: see why this statement.
366 * When the vrf is disabled let's
367 * handle all nexthop-groups associated
370 nexthop_group_disable_vrf(vrf
);
372 if (vrf_master
.vrf_disable_hook
)
373 (*vrf_master
.vrf_disable_hook
)(vrf
);
376 const char *vrf_id_to_name(vrf_id_t vrf_id
)
380 if (vrf_id
== VRF_DEFAULT
)
381 return VRF_DEFAULT_NAME
;
383 vrf
= vrf_lookup_by_id(vrf_id
);
384 return VRF_LOGNAME(vrf
);
387 vrf_id_t
vrf_name_to_id(const char *name
)
390 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
391 // id/ routine not used.
395 vrf
= vrf_lookup_by_name(name
);
397 vrf_id
= vrf
->vrf_id
;
402 /* Get the data pointer of the specified VRF. If not found, create one. */
403 void *vrf_info_get(vrf_id_t vrf_id
)
405 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
409 /* Look up the data pointer of the specified VRF. */
410 void *vrf_info_lookup(vrf_id_t vrf_id
)
412 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
413 return vrf
? vrf
->info
: NULL
;
417 * VRF hash for storing set or not.
424 static unsigned int vrf_hash_bitmap_key(const void *data
)
426 const struct vrf_bit_set
*bit
= data
;
431 static bool vrf_hash_bitmap_cmp(const void *a
, const void *b
)
433 const struct vrf_bit_set
*bit1
= a
;
434 const struct vrf_bit_set
*bit2
= b
;
436 return bit1
->vrf_id
== bit2
->vrf_id
;
439 static void *vrf_hash_bitmap_alloc(void *data
)
441 struct vrf_bit_set
*copy
= data
;
442 struct vrf_bit_set
*bit
;
444 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
445 bit
->vrf_id
= copy
->vrf_id
;
450 static void vrf_hash_bitmap_free(void *data
)
452 struct vrf_bit_set
*bit
= data
;
454 XFREE(MTYPE_VRF_BITMAP
, bit
);
457 vrf_bitmap_t
vrf_bitmap_init(void)
459 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
463 void vrf_bitmap_free(vrf_bitmap_t bmap
)
465 struct hash
*vrf_hash
= bmap
;
467 if (vrf_hash
== NULL
)
470 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
474 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
476 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
477 struct hash
*vrf_hash
= bmap
;
478 struct vrf_bit_set
*bit
;
480 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
483 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
487 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
489 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
490 struct hash
*vrf_hash
= bmap
;
491 struct vrf_bit_set
*bit
;
493 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
496 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
500 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
502 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
503 struct hash
*vrf_hash
= bmap
;
504 struct vrf_bit_set
*bit
;
506 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
509 bit
= hash_lookup(vrf_hash
, &lookup
);
516 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
518 struct vrf
*vrf
= NULL
;
520 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
521 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
524 static const struct cmd_variable_handler vrf_var_handlers
[] = {
527 .completions
= vrf_autocomplete
,
530 .varname
= "vrf_name",
531 .completions
= vrf_autocomplete
,
534 .varname
= "nexthop_vrf",
535 .completions
= vrf_autocomplete
,
537 {.completions
= NULL
},
540 /* Initialize VRF module. */
541 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
542 int (*disable
)(struct vrf
*), int (*destroy
)(struct vrf
*),
543 int ((*update
)(struct vrf
*)))
545 struct vrf
*default_vrf
;
547 /* initialise NS, in case VRF backend if NETNS */
550 zlog_debug("%s: Initializing VRF subsystem", __func__
);
552 vrf_master
.vrf_new_hook
= create
;
553 vrf_master
.vrf_enable_hook
= enable
;
554 vrf_master
.vrf_disable_hook
= disable
;
555 vrf_master
.vrf_delete_hook
= destroy
;
556 vrf_master
.vrf_update_name_hook
= update
;
558 /* The default VRF always exists. */
559 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
561 flog_err(EC_LIB_VRF_START
,
562 "vrf_init: failed to create the default VRF!");
565 if (vrf_is_backend_netns()) {
568 strlcpy(default_vrf
->data
.l
.netns_name
,
569 VRF_DEFAULT_NAME
, NS_NAMSIZ
);
570 ns
= ns_lookup(NS_DEFAULT
);
571 ns
->vrf_ctxt
= default_vrf
;
572 default_vrf
->ns_ctxt
= ns
;
575 /* Enable the default VRF. */
576 if (!vrf_enable(default_vrf
)) {
577 flog_err(EC_LIB_VRF_START
,
578 "vrf_init: failed to enable the default VRF!");
582 cmd_variable_handler_register(vrf_var_handlers
);
585 static void vrf_terminate_single(struct vrf
*vrf
)
587 /* Clear configured flag and invoke delete. */
588 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
592 /* Terminate VRF module. */
593 void vrf_terminate(void)
595 struct vrf
*vrf
, *tmp
;
598 zlog_debug("%s: Shutting down vrf subsystem", __func__
);
600 RB_FOREACH_SAFE (vrf
, vrf_id_head
, &vrfs_by_id
, tmp
) {
601 if (vrf
->vrf_id
== VRF_DEFAULT
)
604 vrf_terminate_single(vrf
);
607 RB_FOREACH_SAFE (vrf
, vrf_name_head
, &vrfs_by_name
, tmp
) {
608 if (vrf
->vrf_id
== VRF_DEFAULT
)
611 vrf_terminate_single(vrf
);
614 /* Finally terminate default VRF */
615 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
616 vrf_terminate_single(vrf
);
619 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
620 const char *interfacename
)
622 int ret
, save_errno
, ret2
;
624 ret
= vrf_switch_to_netns(vrf_id
);
626 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
627 __func__
, vrf_id
, safe_strerror(errno
));
629 ret
= socket(domain
, type
, protocol
);
631 ret2
= vrf_switchback_to_initial();
633 flog_err_sys(EC_LIB_SOCKET
,
634 "%s: Can't switchback from VRF %u (%s)", __func__
,
635 vrf_id
, safe_strerror(errno
));
639 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
647 int vrf_is_backend_netns(void)
649 return (vrf_backend
== VRF_BACKEND_NETNS
);
652 int vrf_get_backend(void)
654 if (!vrf_backend_configured
)
655 return VRF_BACKEND_UNKNOWN
;
659 int vrf_configure_backend(enum vrf_backend_type backend
)
661 /* Work around issue in old gcc */
663 case VRF_BACKEND_UNKNOWN
:
664 case VRF_BACKEND_NETNS
:
665 case VRF_BACKEND_VRF_LITE
:
671 vrf_backend
= backend
;
672 vrf_backend_configured
= 1;
677 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
681 char xpath_list
[XPATH_MAXLEN
];
684 if (strlen(vrfname
) > VRF_NAMSIZ
) {
687 "%% VRF name %s invalid: length exceeds %d bytes\n",
688 vrfname
, VRF_NAMSIZ
);
692 "%% VRF name %s invalid: length exceeds %d bytes",
693 vrfname
, VRF_NAMSIZ
);
694 return CMD_WARNING_CONFIG_FAILED
;
698 snprintf(xpath_list
, sizeof(xpath_list
), FRR_VRF_KEY_XPATH
,
701 nb_cli_enqueue_change(vty
, xpath_list
, NB_OP_CREATE
, NULL
);
702 ret
= nb_cli_apply_changes_clear_pending(vty
, xpath_list
);
703 if (ret
== CMD_SUCCESS
) {
704 VTY_PUSH_XPATH(VRF_NODE
, xpath_list
);
705 vrfp
= vrf_lookup_by_name(vrfname
);
707 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
710 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
718 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
719 ns_id_t ns_id
, ns_id_t internal_ns_id
,
720 ns_id_t rel_def_ns_id
)
722 struct ns
*ns
= NULL
;
725 return CMD_WARNING_CONFIG_FAILED
;
726 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
729 "VRF %u is already configured with VRF %s\n",
730 vrf
->vrf_id
, vrf
->name
);
732 zlog_info("VRF %u is already configured with VRF %s",
733 vrf
->vrf_id
, vrf
->name
);
734 return CMD_WARNING_CONFIG_FAILED
;
736 if (vrf
->ns_ctxt
!= NULL
) {
737 ns
= (struct ns
*)vrf
->ns_ctxt
;
738 if (!strcmp(ns
->name
, pathname
)) {
741 "VRF %u already configured with NETNS %s\n",
742 vrf
->vrf_id
, ns
->name
);
745 "VRF %u already configured with NETNS %s",
746 vrf
->vrf_id
, ns
->name
);
747 return CMD_WARNING_CONFIG_FAILED
;
750 ns
= ns_lookup_name(pathname
);
751 if (ns
&& ns
->vrf_ctxt
) {
752 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
758 "NS %s is already configured with VRF %u(%s)\n",
759 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
761 zlog_info("NS %s is already configured with VRF %u(%s)",
762 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
763 return CMD_WARNING_CONFIG_FAILED
;
765 ns
= ns_get_created(ns
, pathname
, ns_id
);
766 ns
->internal_ns_id
= internal_ns_id
;
767 ns
->relative_default_ns
= rel_def_ns_id
;
768 ns
->vrf_ctxt
= (void *)vrf
;
769 vrf
->ns_ctxt
= (void *)ns
;
770 /* update VRF netns NAME */
771 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
773 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
775 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
776 ns
->ns_id
, ns
->name
);
778 zlog_info("Can not associate NS %u with NETNS %s",
779 ns
->ns_id
, ns
->name
);
780 return CMD_WARNING_CONFIG_FAILED
;
786 /* vrf CLI commands */
790 "Exit current mode and down to previous mode\n")
796 DEFUN_YANG_NOSH (vrf
,
799 "Select a VRF to configure\n"
803 const char *vrfname
= argv
[idx_name
]->arg
;
805 return vrf_handler_create(vty
, vrfname
, NULL
);
812 "Delete a pseudo VRF's configuration\n"
815 const char *vrfname
= argv
[2]->arg
;
816 char xpath_list
[XPATH_MAXLEN
];
820 vrfp
= vrf_lookup_by_name(vrfname
);
825 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
826 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
827 return CMD_WARNING_CONFIG_FAILED
;
830 if (vrf_get_backend() == VRF_BACKEND_VRF_LITE
) {
832 * Remove the VRF interface config. Currently, we allow to
833 * remove only inactive VRFs, so we use VRF_DEFAULT_NAME here,
834 * because when the VRF is removed from kernel, the interface
835 * is moved to the default VRF. If we ever allow removing
836 * active VRFs, this code have to be updated accordingly.
838 snprintf(xpath_list
, sizeof(xpath_list
),
839 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
840 vrfname
, VRF_DEFAULT_NAME
);
841 nb_cli_enqueue_change(vty
, xpath_list
, NB_OP_DESTROY
, NULL
);
844 snprintf(xpath_list
, sizeof(xpath_list
), FRR_VRF_KEY_XPATH
, vrfname
);
846 nb_cli_enqueue_change(vty
, xpath_list
, NB_OP_DESTROY
, NULL
);
847 return nb_cli_apply_changes(vty
, NULL
);
851 static struct cmd_node vrf_node
= {
854 .parent_node
= CONFIG_NODE
,
855 .prompt
= "%s(config-vrf)# ",
859 * Debug CLI for vrf's
884 static int vrf_write_host(struct vty
*vty
)
887 vty_out(vty
, "debug vrf\n");
892 static int vrf_write_host(struct vty
*vty
);
893 static struct cmd_node vrf_debug_node
= {
895 .node
= VRF_DEBUG_NODE
,
897 .config_write
= vrf_write_host
,
900 void vrf_install_commands(void)
902 install_node(&vrf_debug_node
);
904 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
905 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
906 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
907 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
910 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
))
912 install_element(CONFIG_NODE
, &vrf_cmd
);
913 install_element(CONFIG_NODE
, &no_vrf_cmd
);
914 vrf_node
.config_write
= writefunc
;
915 install_node(&vrf_node
);
916 install_default(VRF_NODE
);
917 install_element(VRF_NODE
, &vrf_exit_cmd
);
920 void vrf_set_default_name(const char *default_name
, bool force
)
923 static bool def_vrf_forced
;
925 def_vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
926 assert(default_name
);
927 if (def_vrf
&& !force
&& def_vrf_forced
) {
928 zlog_debug("VRF: %s, avoid changing name to %s, previously forced (%u)",
929 def_vrf
->name
, default_name
,
933 if (strmatch(vrf_default_name
, default_name
))
935 snprintf(vrf_default_name
, VRF_NAMSIZ
, "%s", default_name
);
938 def_vrf_forced
= true;
939 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, def_vrf
);
940 strlcpy(def_vrf
->data
.l
.netns_name
,
941 vrf_default_name
, NS_NAMSIZ
);
942 strlcpy(def_vrf
->name
, vrf_default_name
, sizeof(def_vrf
->name
));
943 RB_INSERT(vrf_name_head
, &vrfs_by_name
, def_vrf
);
944 if (vrf_master
.vrf_update_name_hook
)
945 (*vrf_master
.vrf_update_name_hook
)(def_vrf
);
949 const char *vrf_get_default_name(void)
951 return vrf_default_name
;
954 int vrf_bind(vrf_id_t vrf_id
, int fd
, const char *ifname
)
957 struct interface
*ifp
;
963 if (vrf_id
== VRF_UNKNOWN
)
966 /* can't bind to a VRF that doesn't exist */
967 vrf
= vrf_lookup_by_id(vrf_id
);
968 if (!vrf_is_enabled(vrf
))
971 if (ifname
&& strcmp(ifname
, vrf
->name
)) {
972 /* binding to a regular interface */
974 /* can't bind to an interface that doesn't exist */
975 ifp
= if_lookup_by_name(ifname
, vrf_id
);
979 /* binding to a VRF device */
981 /* nothing to do for netns */
982 if (vrf_is_backend_netns())
985 /* nothing to do for default vrf */
986 if (vrf_id
== VRF_DEFAULT
)
992 #ifdef SO_BINDTODEVICE
993 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, ifname
,
996 zlog_err("bind to interface %s failed, errno=%d", ifname
,
998 #endif /* SO_BINDTODEVICE */
1001 int vrf_getaddrinfo(const char *node
, const char *service
,
1002 const struct addrinfo
*hints
, struct addrinfo
**res
,
1005 int ret
, ret2
, save_errno
;
1007 ret
= vrf_switch_to_netns(vrf_id
);
1009 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
1010 __func__
, vrf_id
, safe_strerror(errno
));
1011 ret
= getaddrinfo(node
, service
, hints
, res
);
1013 ret2
= vrf_switchback_to_initial();
1015 flog_err_sys(EC_LIB_SOCKET
,
1016 "%s: Can't switchback from VRF %u (%s)", __func__
,
1017 vrf_id
, safe_strerror(errno
));
1022 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
1024 int ret
, saved_errno
, rc
;
1026 ret
= vrf_switch_to_netns(vrf_id
);
1028 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
1029 __func__
, vrf_id
, safe_strerror(errno
));
1032 rc
= ioctl(d
, request
, params
);
1033 saved_errno
= errno
;
1034 ret
= vrf_switchback_to_initial();
1036 flog_err_sys(EC_LIB_SOCKET
,
1037 "%s: Can't switchback from VRF %u (%s)", __func__
,
1038 vrf_id
, safe_strerror(errno
));
1039 errno
= saved_errno
;
1043 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
1044 const char *interfacename
)
1046 int ret
, save_errno
, ret2
;
1048 ret
= vrf_switch_to_netns(vrf_id
);
1050 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
1051 __func__
, vrf_id
, safe_strerror(errno
));
1052 ret
= sockunion_socket(su
);
1054 ret2
= vrf_switchback_to_initial();
1056 flog_err_sys(EC_LIB_SOCKET
,
1057 "%s: Can't switchback from VRF %u (%s)", __func__
,
1058 vrf_id
, safe_strerror(errno
));
1063 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
1071 vrf_id_t
vrf_generate_id(void)
1073 static int vrf_id_local
;
1075 return ++vrf_id_local
;
1078 /* ------- Northbound callbacks ------- */
1081 * XPath: /frr-vrf:lib/vrf
1083 static int lib_vrf_create(struct nb_cb_create_args
*args
)
1085 const char *vrfname
;
1088 vrfname
= yang_dnode_get_string(args
->dnode
, "./name");
1090 if (args
->event
!= NB_EV_APPLY
)
1093 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
1095 SET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
1096 nb_running_set_entry(args
->dnode
, vrfp
);
1101 static int lib_vrf_destroy(struct nb_cb_destroy_args
*args
)
1105 switch (args
->event
) {
1106 case NB_EV_VALIDATE
:
1107 vrfp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1108 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
1109 snprintf(args
->errmsg
, args
->errmsg_len
,
1110 "Only inactive VRFs can be deleted");
1111 return NB_ERR_VALIDATION
;
1118 vrfp
= nb_running_unset_entry(args
->dnode
);
1120 /* Clear configured flag and invoke delete. */
1121 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
1129 static const void *lib_vrf_get_next(struct nb_cb_get_next_args
*args
)
1131 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1133 if (args
->list_entry
== NULL
) {
1134 vrfp
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1136 vrfp
= RB_NEXT(vrf_name_head
, vrfp
);
1142 static int lib_vrf_get_keys(struct nb_cb_get_keys_args
*args
)
1144 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1146 args
->keys
->num
= 1;
1147 strlcpy(args
->keys
->key
[0], vrfp
->name
, sizeof(args
->keys
->key
[0]));
1152 static const void *lib_vrf_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1154 const char *vrfname
= args
->keys
->key
[0];
1156 struct vrf
*vrf
= vrf_lookup_by_name(vrfname
);
1162 * XPath: /frr-vrf:lib/vrf/id
1164 static struct yang_data
*
1165 lib_vrf_state_id_get_elem(struct nb_cb_get_elem_args
*args
)
1167 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1169 return yang_data_new_uint32(args
->xpath
, vrfp
->vrf_id
);
1173 * XPath: /frr-vrf:lib/vrf/active
1175 static struct yang_data
*
1176 lib_vrf_state_active_get_elem(struct nb_cb_get_elem_args
*args
)
1178 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1180 if (vrfp
->status
== VRF_ACTIVE
)
1181 return yang_data_new_bool(
1182 args
->xpath
, vrfp
->status
== VRF_ACTIVE
? true : false);
1187 /* clang-format off */
1188 const struct frr_yang_module_info frr_vrf_info
= {
1192 .xpath
= "/frr-vrf:lib/vrf",
1194 .create
= lib_vrf_create
,
1195 .destroy
= lib_vrf_destroy
,
1196 .get_next
= lib_vrf_get_next
,
1197 .get_keys
= lib_vrf_get_keys
,
1198 .lookup_entry
= lib_vrf_lookup_entry
,
1200 .priority
= NB_DFLT_PRIORITY
- 2,
1203 .xpath
= "/frr-vrf:lib/vrf/state/id",
1205 .get_elem
= lib_vrf_state_id_get_elem
,
1209 .xpath
= "/frr-vrf:lib/vrf/state/active",
1211 .get_elem
= lib_vrf_state_active_get_elem
,