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")
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 struct zebra_privs_t
*vrf_daemon_privs
;
62 static char vrf_default_name
[VRF_NAMSIZ
] = VRF_DEFAULT_NAME_INTERNAL
;
65 * Turn on/off debug code
68 static int debug_vrf
= 0;
70 /* Holding VRF hooks */
71 static struct vrf_master
{
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
*);
76 int (*vrf_update_name_hook
)(struct vrf
*vrf
);
81 static int vrf_is_enabled(struct vrf
*vrf
);
83 /* VRF list existance check by name. */
84 struct vrf
*vrf_lookup_by_name(const char *name
)
87 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
88 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
91 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
93 return (a
->vrf_id
- b
->vrf_id
);
96 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
98 return strcmp(a
->name
, b
->name
);
101 /* if ns_id is different and not VRF_UNKNOWN,
102 * then update vrf identifier, and enable VRF
104 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
106 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
108 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
112 old_vrf_id
= vrf
->vrf_id
;
113 if (vrf_id
== vrf
->vrf_id
)
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
)
123 int vrf_switch_to_netns(vrf_id_t vrf_id
)
126 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
128 /* VRF is default VRF. silently ignore */
129 if (!vrf
|| vrf
->vrf_id
== VRF_DEFAULT
)
130 return 1; /* 1 = default */
131 /* VRF has no NETNS backend. silently ignore */
132 if (vrf
->data
.l
.netns_name
[0] == '\0')
133 return 2; /* 2 = no netns */
134 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
136 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
137 return ns_switch_to_netns(name
);
140 int vrf_switchback_to_initial(void)
142 int ret
= ns_switchback_to_initial();
144 if (ret
== 0 && debug_vrf
)
145 zlog_debug("VRF_SWITCHBACK");
149 /* Get a VRF. If not found, create one.
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
153 * Description: Please note that this routine can be called with just the name
156 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
158 struct vrf
*vrf
= NULL
;
161 /* Nothing to see, move along here */
162 if (!name
&& vrf_id
== VRF_UNKNOWN
)
165 /* attempt to find already available VRF
168 vrf
= vrf_lookup_by_name(name
);
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
);
176 /* Try to find VRF both by ID and name */
177 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
178 vrf
= vrf_lookup_by_id(vrf_id
);
181 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
182 vrf
->vrf_id
= VRF_UNKNOWN
;
187 zlog_debug("VRF(%u) %s is created.", vrf_id
,
188 (name
) ? name
: "(NULL)");
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
);
198 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
199 /* update the vrf name */
200 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
201 strlcpy(vrf
->data
.l
.netns_name
,
203 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
204 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
205 if (vrf
->vrf_id
== VRF_DEFAULT
)
206 vrf_set_default_name(vrf
->name
, false);
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
);
211 if (new &&vrf_master
.vrf_new_hook
)
212 (*vrf_master
.vrf_new_hook
)(vrf
);
217 /* Delete a VRF. This is called when the underlying VRF goes away, a
218 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
220 void vrf_delete(struct vrf
*vrf
)
223 zlog_debug("VRF %s(%u) is to be deleted.", vrf
->name
,
226 if (vrf_is_enabled(vrf
))
229 /* If the VRF is user configured, it'll stick around, just remove
230 * the ID mapping. Interfaces assigned to this VRF should've been
231 * removed already as part of the VRF going down.
233 if (vrf_is_user_cfged(vrf
)) {
234 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
235 /* Delete any VRF interfaces - should be only
236 * the VRF itself, other interfaces should've
237 * been moved out of the VRF.
240 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
241 vrf
->vrf_id
= VRF_UNKNOWN
;
246 if (vrf_master
.vrf_delete_hook
)
247 (*vrf_master
.vrf_delete_hook
)(vrf
);
252 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
253 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
254 if (vrf
->name
[0] != '\0')
255 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
257 XFREE(MTYPE_VRF
, vrf
);
260 /* Look up a VRF by identifier. */
261 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
265 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
269 * Enable a VRF - that is, let the VRF be ready to use.
270 * The VRF_ENABLE_HOOK callback will be called to inform
271 * that they can allocate resources in this VRF.
273 * RETURN: 1 - enabled successfully; otherwise, 0.
275 int vrf_enable(struct vrf
*vrf
)
277 if (vrf_is_enabled(vrf
))
281 zlog_debug("VRF %s(%u) is enabled.", vrf
->name
, vrf
->vrf_id
);
283 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
285 if (vrf_master
.vrf_enable_hook
)
286 (*vrf_master
.vrf_enable_hook
)(vrf
);
289 * If we have any nexthop group entries that
290 * are awaiting vrf initialization then
291 * let's let people know about it
293 nexthop_group_enable_vrf(vrf
);
299 * Disable a VRF - that is, let the VRF be unusable.
300 * The VRF_DELETE_HOOK callback will be called to inform
301 * that they must release the resources in the VRF.
303 void vrf_disable(struct vrf
*vrf
)
305 if (!vrf_is_enabled(vrf
))
308 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
311 zlog_debug("VRF %s(%u) is to be disabled.", vrf
->name
,
314 /* Till now, nothing to be done for the default VRF. */
315 // Pending: see why this statement.
319 * When the vrf is disabled let's
320 * handle all nexthop-groups associated
323 nexthop_group_disable_vrf(vrf
);
325 if (vrf_master
.vrf_disable_hook
)
326 (*vrf_master
.vrf_disable_hook
)(vrf
);
329 const char *vrf_id_to_name(vrf_id_t vrf_id
)
333 if (vrf_id
== VRF_DEFAULT
)
334 return VRF_DEFAULT_NAME
;
336 vrf
= vrf_lookup_by_id(vrf_id
);
337 return VRF_LOGNAME(vrf
);
340 vrf_id_t
vrf_name_to_id(const char *name
)
343 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
344 // id/ routine not used.
348 vrf
= vrf_lookup_by_name(name
);
350 vrf_id
= vrf
->vrf_id
;
355 /* Get the data pointer of the specified VRF. If not found, create one. */
356 void *vrf_info_get(vrf_id_t vrf_id
)
358 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
362 /* Look up the data pointer of the specified VRF. */
363 void *vrf_info_lookup(vrf_id_t vrf_id
)
365 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
366 return vrf
? vrf
->info
: NULL
;
370 * VRF hash for storing set or not.
377 static unsigned int vrf_hash_bitmap_key(const void *data
)
379 const struct vrf_bit_set
*bit
= data
;
384 static bool vrf_hash_bitmap_cmp(const void *a
, const void *b
)
386 const struct vrf_bit_set
*bit1
= a
;
387 const struct vrf_bit_set
*bit2
= b
;
389 return bit1
->vrf_id
== bit2
->vrf_id
;
392 static void *vrf_hash_bitmap_alloc(void *data
)
394 struct vrf_bit_set
*copy
= data
;
395 struct vrf_bit_set
*bit
;
397 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
398 bit
->vrf_id
= copy
->vrf_id
;
403 static void vrf_hash_bitmap_free(void *data
)
405 struct vrf_bit_set
*bit
= data
;
407 XFREE(MTYPE_VRF_BITMAP
, bit
);
410 vrf_bitmap_t
vrf_bitmap_init(void)
412 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
416 void vrf_bitmap_free(vrf_bitmap_t bmap
)
418 struct hash
*vrf_hash
= bmap
;
420 if (vrf_hash
== NULL
)
423 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
427 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
429 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
430 struct hash
*vrf_hash
= bmap
;
431 struct vrf_bit_set
*bit
;
433 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
436 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
440 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
442 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
443 struct hash
*vrf_hash
= bmap
;
444 struct vrf_bit_set
*bit
;
446 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
449 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
453 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
455 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
456 struct hash
*vrf_hash
= bmap
;
457 struct vrf_bit_set
*bit
;
459 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
462 bit
= hash_lookup(vrf_hash
, &lookup
);
469 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
471 struct vrf
*vrf
= NULL
;
473 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
474 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
477 static const struct cmd_variable_handler vrf_var_handlers
[] = {
480 .completions
= vrf_autocomplete
,
483 .varname
= "vrf_name",
484 .completions
= vrf_autocomplete
,
487 .varname
= "nexthop_vrf",
488 .completions
= vrf_autocomplete
,
490 {.completions
= NULL
},
493 /* Initialize VRF module. */
494 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
495 int (*disable
)(struct vrf
*), int (*destroy
)(struct vrf
*),
496 int ((*update
)(struct vrf
*)))
498 struct vrf
*default_vrf
;
500 /* initialise NS, in case VRF backend if NETNS */
503 zlog_debug("%s: Initializing VRF subsystem", __func__
);
505 vrf_master
.vrf_new_hook
= create
;
506 vrf_master
.vrf_enable_hook
= enable
;
507 vrf_master
.vrf_disable_hook
= disable
;
508 vrf_master
.vrf_delete_hook
= destroy
;
509 vrf_master
.vrf_update_name_hook
= update
;
511 /* The default VRF always exists. */
512 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
514 flog_err(EC_LIB_VRF_START
,
515 "vrf_init: failed to create the default VRF!");
518 if (vrf_is_backend_netns()) {
521 strlcpy(default_vrf
->data
.l
.netns_name
,
522 VRF_DEFAULT_NAME
, NS_NAMSIZ
);
523 ns
= ns_lookup(NS_DEFAULT
);
524 ns
->vrf_ctxt
= default_vrf
;
525 default_vrf
->ns_ctxt
= ns
;
528 /* Enable the default VRF. */
529 if (!vrf_enable(default_vrf
)) {
530 flog_err(EC_LIB_VRF_START
,
531 "vrf_init: failed to enable the default VRF!");
535 cmd_variable_handler_register(vrf_var_handlers
);
538 /* Terminate VRF module. */
539 void vrf_terminate(void)
544 zlog_debug("%s: Shutting down vrf subsystem", __func__
);
546 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
547 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
549 /* Clear configured flag and invoke delete. */
550 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
554 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
555 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
557 /* Clear configured flag and invoke delete. */
558 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
563 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
564 const char *interfacename
)
566 int ret
, save_errno
, ret2
;
568 ret
= vrf_switch_to_netns(vrf_id
);
570 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
571 __func__
, vrf_id
, safe_strerror(errno
));
573 ret
= socket(domain
, type
, protocol
);
575 ret2
= vrf_switchback_to_initial();
577 flog_err_sys(EC_LIB_SOCKET
,
578 "%s: Can't switchback from VRF %u (%s)", __func__
,
579 vrf_id
, safe_strerror(errno
));
583 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
591 int vrf_is_backend_netns(void)
593 return (vrf_backend
== VRF_BACKEND_NETNS
);
596 int vrf_get_backend(void)
598 if (!vrf_backend_configured
)
599 return VRF_BACKEND_UNKNOWN
;
603 int vrf_configure_backend(enum vrf_backend_type backend
)
605 /* Work around issue in old gcc */
607 case VRF_BACKEND_UNKNOWN
:
608 case VRF_BACKEND_NETNS
:
609 case VRF_BACKEND_VRF_LITE
:
615 vrf_backend
= backend
;
616 vrf_backend_configured
= 1;
621 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
625 char xpath_list
[XPATH_MAXLEN
];
628 if (strlen(vrfname
) > VRF_NAMSIZ
) {
631 "%% VRF name %s invalid: length exceeds %d bytes\n",
632 vrfname
, VRF_NAMSIZ
);
636 "%% VRF name %s invalid: length exceeds %d bytes\n",
637 vrfname
, VRF_NAMSIZ
);
638 return CMD_WARNING_CONFIG_FAILED
;
642 snprintf(xpath_list
, sizeof(xpath_list
),
643 "/frr-vrf:lib/vrf[name='%s']", vrfname
);
645 nb_cli_enqueue_change(vty
, xpath_list
, NB_OP_CREATE
, NULL
);
646 ret
= nb_cli_apply_changes(vty
, xpath_list
);
647 if (ret
== CMD_SUCCESS
) {
648 VTY_PUSH_XPATH(VRF_NODE
, xpath_list
);
649 nb_cli_pending_commit_check(vty
);
650 vrfp
= vrf_lookup_by_name(vrfname
);
652 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
655 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
663 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
664 ns_id_t ns_id
, ns_id_t internal_ns_id
,
665 ns_id_t rel_def_ns_id
)
667 struct ns
*ns
= NULL
;
670 return CMD_WARNING_CONFIG_FAILED
;
671 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
674 "VRF %u is already configured with VRF %s\n",
675 vrf
->vrf_id
, vrf
->name
);
677 zlog_info("VRF %u is already configured with VRF %s",
678 vrf
->vrf_id
, vrf
->name
);
679 return CMD_WARNING_CONFIG_FAILED
;
681 if (vrf
->ns_ctxt
!= NULL
) {
682 ns
= (struct ns
*)vrf
->ns_ctxt
;
683 if (!strcmp(ns
->name
, pathname
)) {
686 "VRF %u already configured with NETNS %s\n",
687 vrf
->vrf_id
, ns
->name
);
690 "VRF %u already configured with NETNS %s",
691 vrf
->vrf_id
, ns
->name
);
692 return CMD_WARNING_CONFIG_FAILED
;
695 ns
= ns_lookup_name(pathname
);
696 if (ns
&& ns
->vrf_ctxt
) {
697 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
703 "NS %s is already configured with VRF %u(%s)\n",
704 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
706 zlog_info("NS %s is already configured with VRF %u(%s)",
707 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
708 return CMD_WARNING_CONFIG_FAILED
;
710 ns
= ns_get_created(ns
, pathname
, ns_id
);
711 ns
->internal_ns_id
= internal_ns_id
;
712 ns
->relative_default_ns
= rel_def_ns_id
;
713 ns
->vrf_ctxt
= (void *)vrf
;
714 vrf
->ns_ctxt
= (void *)ns
;
715 /* update VRF netns NAME */
716 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
718 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
720 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
721 ns
->ns_id
, ns
->name
);
723 zlog_info("Can not associate NS %u with NETNS %s",
724 ns
->ns_id
, ns
->name
);
725 return CMD_WARNING_CONFIG_FAILED
;
731 /* vrf CLI commands */
735 "Exit current mode and down to previous mode\n")
737 /* We have to set vrf context to default vrf */
738 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
743 DEFUN_YANG_NOSH (vrf
,
746 "Select a VRF to configure\n"
750 const char *vrfname
= argv
[idx_name
]->arg
;
752 return vrf_handler_create(vty
, vrfname
, NULL
);
759 "Delete a pseudo VRF's configuration\n"
762 const char *vrfname
= argv
[2]->arg
;
763 char xpath_list
[XPATH_MAXLEN
];
767 vrfp
= vrf_lookup_by_name(vrfname
);
772 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
773 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
774 return CMD_WARNING_CONFIG_FAILED
;
777 snprintf(xpath_list
, sizeof(xpath_list
), "/frr-vrf:lib/vrf[name='%s']",
780 nb_cli_enqueue_change(vty
, xpath_list
, NB_OP_DESTROY
, NULL
);
781 return nb_cli_apply_changes(vty
, xpath_list
);
785 static struct cmd_node vrf_node
= {
788 .parent_node
= CONFIG_NODE
,
789 .prompt
= "%s(config-vrf)# ",
792 DEFUN_NOSH (vrf_netns
,
795 "Attach VRF to a Namespace\n"
796 "The file name in " NS_RUN_DIR
", or a full pathname\n")
798 int idx_name
= 1, ret
;
799 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
801 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
804 return CMD_WARNING_CONFIG_FAILED
;
806 frr_with_privs(vrf_daemon_privs
) {
807 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
815 DEFUN_NOSH (no_vrf_netns
,
819 "Detach VRF from a Namespace\n"
820 "The file name in " NS_RUN_DIR
", or a full pathname\n")
822 struct ns
*ns
= NULL
;
824 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
826 if (!vrf_is_backend_netns()) {
827 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
828 return CMD_WARNING_CONFIG_FAILED
;
831 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
832 vrf
->name
, vrf
->vrf_id
);
833 return CMD_WARNING_CONFIG_FAILED
;
836 ns
= (struct ns
*)vrf
->ns_ctxt
;
840 /* vrf ID from VRF is necessary for Zebra
841 * so that propagate to other clients is done
849 * Debug CLI for vrf's
874 static int vrf_write_host(struct vty
*vty
)
877 vty_out(vty
, "debug vrf\n");
882 static int vrf_write_host(struct vty
*vty
);
883 static struct cmd_node vrf_debug_node
= {
885 .node
= VRF_DEBUG_NODE
,
887 .config_write
= vrf_write_host
,
890 void vrf_install_commands(void)
892 install_node(&vrf_debug_node
);
894 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
895 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
896 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
897 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
900 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
901 struct zebra_privs_t
*daemon_privs
)
903 install_element(CONFIG_NODE
, &vrf_cmd
);
904 install_element(CONFIG_NODE
, &no_vrf_cmd
);
905 vrf_node
.config_write
= writefunc
;
906 install_node(&vrf_node
);
907 install_default(VRF_NODE
);
908 install_element(VRF_NODE
, &vrf_exit_cmd
);
909 if (vrf_is_backend_netns() && ns_have_netns()) {
910 /* Install NS commands. */
911 vrf_daemon_privs
= daemon_privs
;
912 install_element(VRF_NODE
, &vrf_netns_cmd
);
913 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
917 void vrf_set_default_name(const char *default_name
, bool force
)
920 static bool def_vrf_forced
;
922 def_vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
923 assert(default_name
);
924 if (def_vrf
&& !force
&& def_vrf_forced
) {
925 zlog_debug("VRF: %s, avoid changing name to %s, previously forced (%u)",
926 def_vrf
->name
, default_name
,
930 if (strmatch(vrf_default_name
, default_name
))
932 snprintf(vrf_default_name
, VRF_NAMSIZ
, "%s", default_name
);
935 def_vrf_forced
= true;
936 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, def_vrf
);
937 strlcpy(def_vrf
->data
.l
.netns_name
,
938 vrf_default_name
, NS_NAMSIZ
);
939 strlcpy(def_vrf
->name
, vrf_default_name
, sizeof(def_vrf
->name
));
940 RB_INSERT(vrf_name_head
, &vrfs_by_name
, def_vrf
);
941 if (vrf_master
.vrf_update_name_hook
)
942 (*vrf_master
.vrf_update_name_hook
)(def_vrf
);
946 const char *vrf_get_default_name(void)
948 return vrf_default_name
;
951 int vrf_bind(vrf_id_t vrf_id
, int fd
, const char *name
)
954 struct interface
*ifp
;
956 if (fd
< 0 || name
== NULL
)
958 /* the device should exist
959 * otherwise we should return
960 * case ifname = vrf in netns mode => return
962 ifp
= if_lookup_by_name(name
, vrf_id
);
965 #ifdef SO_BINDTODEVICE
966 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
968 zlog_debug("bind to interface %s failed, errno=%d", name
,
970 #endif /* SO_BINDTODEVICE */
973 int vrf_getaddrinfo(const char *node
, const char *service
,
974 const struct addrinfo
*hints
, struct addrinfo
**res
,
977 int ret
, ret2
, save_errno
;
979 ret
= vrf_switch_to_netns(vrf_id
);
981 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
982 __func__
, vrf_id
, safe_strerror(errno
));
983 ret
= getaddrinfo(node
, service
, hints
, res
);
985 ret2
= vrf_switchback_to_initial();
987 flog_err_sys(EC_LIB_SOCKET
,
988 "%s: Can't switchback from VRF %u (%s)", __func__
,
989 vrf_id
, safe_strerror(errno
));
994 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
996 int ret
, saved_errno
, rc
;
998 ret
= vrf_switch_to_netns(vrf_id
);
1000 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
1001 __func__
, vrf_id
, safe_strerror(errno
));
1004 rc
= ioctl(d
, request
, params
);
1005 saved_errno
= errno
;
1006 ret
= vrf_switchback_to_initial();
1008 flog_err_sys(EC_LIB_SOCKET
,
1009 "%s: Can't switchback from VRF %u (%s)", __func__
,
1010 vrf_id
, safe_strerror(errno
));
1011 errno
= saved_errno
;
1015 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
1016 const char *interfacename
)
1018 int ret
, save_errno
, ret2
;
1020 ret
= vrf_switch_to_netns(vrf_id
);
1022 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
1023 __func__
, vrf_id
, safe_strerror(errno
));
1024 ret
= sockunion_socket(su
);
1026 ret2
= vrf_switchback_to_initial();
1028 flog_err_sys(EC_LIB_SOCKET
,
1029 "%s: Can't switchback from VRF %u (%s)", __func__
,
1030 vrf_id
, safe_strerror(errno
));
1035 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
1043 vrf_id_t
vrf_generate_id(void)
1045 static int vrf_id_local
;
1047 return ++vrf_id_local
;
1050 /* ------- Northbound callbacks ------- */
1053 * XPath: /frr-vrf:lib/vrf
1055 static int lib_vrf_create(struct nb_cb_create_args
*args
)
1057 const char *vrfname
;
1060 vrfname
= yang_dnode_get_string(args
->dnode
, "./name");
1062 if (args
->event
!= NB_EV_APPLY
)
1065 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
1067 nb_running_set_entry(args
->dnode
, vrfp
);
1072 static int lib_vrf_destroy(struct nb_cb_destroy_args
*args
)
1076 switch (args
->event
) {
1077 case NB_EV_VALIDATE
:
1078 vrfp
= nb_running_get_entry(args
->dnode
, NULL
, true);
1079 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
1080 snprintf(args
->errmsg
, args
->errmsg_len
,
1081 "Only inactive VRFs can be deleted");
1082 return NB_ERR_VALIDATION
;
1089 vrfp
= nb_running_unset_entry(args
->dnode
);
1091 /* Clear configured flag and invoke delete. */
1092 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
1100 static const void *lib_vrf_get_next(struct nb_cb_get_next_args
*args
)
1102 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1104 if (args
->list_entry
== NULL
) {
1105 vrfp
= RB_MIN(vrf_name_head
, &vrfs_by_name
);
1107 vrfp
= RB_NEXT(vrf_name_head
, vrfp
);
1113 static int lib_vrf_get_keys(struct nb_cb_get_keys_args
*args
)
1115 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1117 args
->keys
->num
= 1;
1118 strlcpy(args
->keys
->key
[0], vrfp
->name
, sizeof(args
->keys
->key
[0]));
1123 static const void *lib_vrf_lookup_entry(struct nb_cb_lookup_entry_args
*args
)
1125 const char *vrfname
= args
->keys
->key
[0];
1127 struct vrf
*vrf
= vrf_lookup_by_name(vrfname
);
1133 * XPath: /frr-vrf:lib/vrf/id
1135 static struct yang_data
*
1136 lib_vrf_state_id_get_elem(struct nb_cb_get_elem_args
*args
)
1138 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1140 return yang_data_new_uint32(args
->xpath
, vrfp
->vrf_id
);
1144 * XPath: /frr-vrf:lib/vrf/active
1146 static struct yang_data
*
1147 lib_vrf_state_active_get_elem(struct nb_cb_get_elem_args
*args
)
1149 struct vrf
*vrfp
= (struct vrf
*)args
->list_entry
;
1151 if (vrfp
->status
== VRF_ACTIVE
)
1152 return yang_data_new_bool(
1153 args
->xpath
, vrfp
->status
== VRF_ACTIVE
? true : false);
1158 /* clang-format off */
1159 const struct frr_yang_module_info frr_vrf_info
= {
1163 .xpath
= "/frr-vrf:lib/vrf",
1165 .create
= lib_vrf_create
,
1166 .destroy
= lib_vrf_destroy
,
1167 .get_next
= lib_vrf_get_next
,
1168 .get_keys
= lib_vrf_get_keys
,
1169 .lookup_entry
= lib_vrf_lookup_entry
,
1173 .xpath
= "/frr-vrf:lib/vrf/state/id",
1175 .get_elem
= lib_vrf_state_id_get_elem
,
1179 .xpath
= "/frr-vrf:lib/vrf/state/active",
1181 .get_elem
= lib_vrf_state_active_get_elem
,