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"
40 /* default VRF ID value used when VRF backend is not NETNS */
41 #define VRF_DEFAULT_INTERNAL 0
42 #define VRF_DEFAULT_NAME_INTERNAL "default"
44 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF")
45 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map")
49 static __inline
int vrf_id_compare(const struct vrf
*, const struct vrf
*);
50 static __inline
int vrf_name_compare(const struct vrf
*, const struct vrf
*);
52 RB_GENERATE(vrf_id_head
, vrf
, id_entry
, vrf_id_compare
);
53 RB_GENERATE(vrf_name_head
, vrf
, name_entry
, vrf_name_compare
);
55 struct vrf_id_head vrfs_by_id
= RB_INITIALIZER(&vrfs_by_id
);
56 struct vrf_name_head vrfs_by_name
= RB_INITIALIZER(&vrfs_by_name
);
58 static int vrf_backend
;
59 static int vrf_backend_configured
;
60 static struct zebra_privs_t
*vrf_daemon_privs
;
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 */
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
)
119 vrf_enable((struct vrf
*)vrf
);
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
;
161 zlog_debug("VRF_GET: %s(%u)", name
== NULL
? "(NULL)" : name
,
164 /* Nothing to see, move along here */
165 if (!name
&& vrf_id
== VRF_UNKNOWN
)
168 /* attempt to find already available VRF
171 vrf
= vrf_lookup_by_name(name
);
172 if (vrf
&& vrf_id
!= VRF_UNKNOWN
173 && vrf
->vrf_id
!= VRF_UNKNOWN
174 && vrf
->vrf_id
!= vrf_id
) {
175 zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)",
176 name
, vrf_id
, vrf
->vrf_id
);
179 /* Try to find VRF both by ID and name */
180 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
181 vrf
= vrf_lookup_by_id(vrf_id
);
184 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
185 vrf
->vrf_id
= VRF_UNKNOWN
;
190 zlog_debug("VRF(%u) %s is created.", vrf_id
,
191 (name
) ? name
: "(NULL)");
195 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
196 vrf
->vrf_id
= vrf_id
;
197 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
201 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
202 /* update the vrf name */
203 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
204 strlcpy(vrf
->data
.l
.netns_name
,
206 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
207 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
208 if (vrf
->vrf_id
== VRF_DEFAULT
)
209 vrf_set_default_name(vrf
->name
, false);
210 } else if (name
&& vrf
->name
[0] == '\0') {
211 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
212 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
214 if (new &&vrf_master
.vrf_new_hook
)
215 (*vrf_master
.vrf_new_hook
)(vrf
);
220 /* Delete a VRF. This is called when the underlying VRF goes away, a
221 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
223 void vrf_delete(struct vrf
*vrf
)
226 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
228 if (vrf_is_enabled(vrf
))
231 /* If the VRF is user configured, it'll stick around, just remove
232 * the ID mapping. Interfaces assigned to this VRF should've been
233 * removed already as part of the VRF going down.
235 if (vrf_is_user_cfged(vrf
)) {
236 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
237 /* Delete any VRF interfaces - should be only
238 * the VRF itself, other interfaces should've
239 * been moved out of the VRF.
242 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
243 vrf
->vrf_id
= VRF_UNKNOWN
;
248 if (vrf_master
.vrf_delete_hook
)
249 (*vrf_master
.vrf_delete_hook
)(vrf
);
254 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
255 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
256 if (vrf
->name
[0] != '\0')
257 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
259 XFREE(MTYPE_VRF
, vrf
);
262 /* Look up a VRF by identifier. */
263 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
267 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
271 * Enable a VRF - that is, let the VRF be ready to use.
272 * The VRF_ENABLE_HOOK callback will be called to inform
273 * that they can allocate resources in this VRF.
275 * RETURN: 1 - enabled successfully; otherwise, 0.
277 int vrf_enable(struct vrf
*vrf
)
279 if (vrf_is_enabled(vrf
))
283 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
285 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
287 if (vrf_master
.vrf_enable_hook
)
288 (*vrf_master
.vrf_enable_hook
)(vrf
);
291 * If we have any nexthop group entries that
292 * are awaiting vrf initialization then
293 * let's let people know about it
295 nexthop_group_enable_vrf(vrf
);
301 * Disable a VRF - that is, let the VRF be unusable.
302 * The VRF_DELETE_HOOK callback will be called to inform
303 * that they must release the resources in the VRF.
305 void vrf_disable(struct vrf
*vrf
)
307 if (!vrf_is_enabled(vrf
))
310 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
313 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
315 /* Till now, nothing to be done for the default VRF. */
316 // Pending: see why this statement.
318 if (vrf_master
.vrf_disable_hook
)
319 (*vrf_master
.vrf_disable_hook
)(vrf
);
322 const char *vrf_id_to_name(vrf_id_t vrf_id
)
326 vrf
= vrf_lookup_by_id(vrf_id
);
333 vrf_id_t
vrf_name_to_id(const char *name
)
336 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
337 // id/ routine not used.
341 vrf
= vrf_lookup_by_name(name
);
343 vrf_id
= vrf
->vrf_id
;
348 /* Get the data pointer of the specified VRF. If not found, create one. */
349 void *vrf_info_get(vrf_id_t vrf_id
)
351 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
355 /* Look up the data pointer of the specified VRF. */
356 void *vrf_info_lookup(vrf_id_t vrf_id
)
358 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
359 return vrf
? vrf
->info
: NULL
;
363 * VRF hash for storing set or not.
370 static unsigned int vrf_hash_bitmap_key(const void *data
)
372 const struct vrf_bit_set
*bit
= data
;
377 static bool vrf_hash_bitmap_cmp(const void *a
, const void *b
)
379 const struct vrf_bit_set
*bit1
= a
;
380 const struct vrf_bit_set
*bit2
= b
;
382 return bit1
->vrf_id
== bit2
->vrf_id
;
385 static void *vrf_hash_bitmap_alloc(void *data
)
387 struct vrf_bit_set
*copy
= data
;
388 struct vrf_bit_set
*bit
;
390 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
391 bit
->vrf_id
= copy
->vrf_id
;
396 static void vrf_hash_bitmap_free(void *data
)
398 struct vrf_bit_set
*bit
= data
;
400 XFREE(MTYPE_VRF_BITMAP
, bit
);
403 vrf_bitmap_t
vrf_bitmap_init(void)
405 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
409 void vrf_bitmap_free(vrf_bitmap_t bmap
)
411 struct hash
*vrf_hash
= bmap
;
413 if (vrf_hash
== NULL
)
416 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
420 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
422 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
423 struct hash
*vrf_hash
= bmap
;
424 struct vrf_bit_set
*bit
;
426 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
429 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
433 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
435 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
436 struct hash
*vrf_hash
= bmap
;
437 struct vrf_bit_set
*bit
;
439 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
442 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
446 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
448 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
449 struct hash
*vrf_hash
= bmap
;
450 struct vrf_bit_set
*bit
;
452 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
455 bit
= hash_lookup(vrf_hash
, &lookup
);
462 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
464 struct vrf
*vrf
= NULL
;
466 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
467 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
470 static const struct cmd_variable_handler vrf_var_handlers
[] = {
473 .completions
= vrf_autocomplete
,
475 {.completions
= NULL
},
478 /* Initialize VRF module. */
479 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
480 int (*disable
)(struct vrf
*), int (*destroy
)(struct vrf
*),
481 int ((*update
)(struct vrf
*)))
483 struct vrf
*default_vrf
;
485 /* initialise NS, in case VRF backend if NETNS */
488 zlog_debug("%s: Initializing VRF subsystem",
489 __PRETTY_FUNCTION__
);
491 vrf_master
.vrf_new_hook
= create
;
492 vrf_master
.vrf_enable_hook
= enable
;
493 vrf_master
.vrf_disable_hook
= disable
;
494 vrf_master
.vrf_delete_hook
= destroy
;
495 vrf_master
.vrf_update_name_hook
= update
;
497 /* The default VRF always exists. */
498 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
500 flog_err(EC_LIB_VRF_START
,
501 "vrf_init: failed to create the default VRF!");
504 if (vrf_is_backend_netns()) {
507 strlcpy(default_vrf
->data
.l
.netns_name
,
508 VRF_DEFAULT_NAME
, NS_NAMSIZ
);
509 ns
= ns_lookup(ns_get_default_id());
510 ns
->vrf_ctxt
= default_vrf
;
511 default_vrf
->ns_ctxt
= ns
;
514 /* Enable the default VRF. */
515 if (!vrf_enable(default_vrf
)) {
516 flog_err(EC_LIB_VRF_START
,
517 "vrf_init: failed to enable the default VRF!");
521 cmd_variable_handler_register(vrf_var_handlers
);
524 /* Terminate VRF module. */
525 void vrf_terminate(void)
530 zlog_debug("%s: Shutting down vrf subsystem",
531 __PRETTY_FUNCTION__
);
533 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
534 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
536 /* Clear configured flag and invoke delete. */
537 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
541 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
542 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
544 /* Clear configured flag and invoke delete. */
545 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
550 /* Create a socket for the VRF. */
551 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
552 const char *interfacename
)
554 int ret
, save_errno
, ret2
;
556 ret
= vrf_switch_to_netns(vrf_id
);
558 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
559 __func__
, vrf_id
, safe_strerror(errno
));
561 ret
= socket(domain
, type
, protocol
);
563 ret2
= vrf_switchback_to_initial();
565 flog_err_sys(EC_LIB_SOCKET
,
566 "%s: Can't switchback from VRF %u (%s)", __func__
,
567 vrf_id
, safe_strerror(errno
));
571 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
579 int vrf_is_backend_netns(void)
581 return (vrf_backend
== VRF_BACKEND_NETNS
);
584 int vrf_get_backend(void)
586 if (!vrf_backend_configured
)
587 return VRF_BACKEND_UNKNOWN
;
591 void vrf_configure_backend(int vrf_backend_netns
)
593 vrf_backend
= vrf_backend_netns
;
594 vrf_backend_configured
= 1;
597 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
602 if (strlen(vrfname
) > VRF_NAMSIZ
) {
605 "%% VRF name %s invalid: length exceeds %d bytes\n",
606 vrfname
, VRF_NAMSIZ
);
610 "%% VRF name %s invalid: length exceeds %d bytes\n",
611 vrfname
, VRF_NAMSIZ
);
612 return CMD_WARNING_CONFIG_FAILED
;
615 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
618 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
625 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
626 ns_id_t ns_id
, ns_id_t internal_ns_id
)
628 struct ns
*ns
= NULL
;
631 return CMD_WARNING_CONFIG_FAILED
;
632 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
635 "VRF %u is already configured with VRF %s\n",
636 vrf
->vrf_id
, vrf
->name
);
638 zlog_info("VRF %u is already configured with VRF %s",
639 vrf
->vrf_id
, vrf
->name
);
640 return CMD_WARNING_CONFIG_FAILED
;
642 if (vrf
->ns_ctxt
!= NULL
) {
643 ns
= (struct ns
*)vrf
->ns_ctxt
;
644 if (!strcmp(ns
->name
, pathname
)) {
647 "VRF %u already configured with NETNS %s\n",
648 vrf
->vrf_id
, ns
->name
);
651 "VRF %u already configured with NETNS %s",
652 vrf
->vrf_id
, ns
->name
);
653 return CMD_WARNING_CONFIG_FAILED
;
656 ns
= ns_lookup_name(pathname
);
657 if (ns
&& ns
->vrf_ctxt
) {
658 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
664 "NS %s is already configured"
665 " with VRF %u(%s)\n",
666 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
668 zlog_info("NS %s is already configured with VRF %u(%s)",
669 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
670 return CMD_WARNING_CONFIG_FAILED
;
672 ns
= ns_get_created(ns
, pathname
, ns_id
);
673 ns
->internal_ns_id
= internal_ns_id
;
674 ns
->vrf_ctxt
= (void *)vrf
;
675 vrf
->ns_ctxt
= (void *)ns
;
676 /* update VRF netns NAME */
677 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
679 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
681 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
682 ns
->ns_id
, ns
->name
);
684 zlog_info("Can not associate NS %u with NETNS %s",
685 ns
->ns_id
, ns
->name
);
686 return CMD_WARNING_CONFIG_FAILED
;
692 /* vrf CLI commands */
696 "Exit current mode and down to previous mode\n")
698 /* We have to set vrf context to default vrf */
699 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
700 vty
->node
= CONFIG_NODE
;
707 "Select a VRF to configure\n"
711 const char *vrfname
= argv
[idx_name
]->arg
;
713 return vrf_handler_create(vty
, vrfname
, NULL
);
720 "Delete a pseudo VRF's configuration\n"
723 const char *vrfname
= argv
[2]->arg
;
727 vrfp
= vrf_lookup_by_name(vrfname
);
730 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
731 return CMD_WARNING_CONFIG_FAILED
;
734 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
735 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
736 return CMD_WARNING_CONFIG_FAILED
;
739 /* Clear configured flag and invoke delete. */
740 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
747 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
749 DEFUN_NOSH (vrf_netns
,
752 "Attach VRF to a Namespace\n"
753 "The file name in " NS_RUN_DIR
", or a full pathname\n")
755 int idx_name
= 1, ret
;
756 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
758 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
761 return CMD_WARNING_CONFIG_FAILED
;
763 frr_with_privs(vrf_daemon_privs
) {
764 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
765 NS_UNKNOWN
, NS_UNKNOWN
);
770 DEFUN_NOSH (no_vrf_netns
,
774 "Detach VRF from a Namespace\n"
775 "The file name in " NS_RUN_DIR
", or a full pathname\n")
777 struct ns
*ns
= NULL
;
779 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
781 if (!vrf_is_backend_netns()) {
782 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
783 return CMD_WARNING_CONFIG_FAILED
;
786 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
787 vrf
->name
, vrf
->vrf_id
);
788 return CMD_WARNING_CONFIG_FAILED
;
791 ns
= (struct ns
*)vrf
->ns_ctxt
;
795 /* vrf ID from VRF is necessary for Zebra
796 * so that propagate to other clients is done
804 * Debug CLI for vrf's
829 static int vrf_write_host(struct vty
*vty
)
832 vty_out(vty
, "debug vrf\n");
837 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
839 void vrf_install_commands(void)
841 install_node(&vrf_debug_node
, vrf_write_host
);
843 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
844 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
845 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
846 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
849 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
850 struct zebra_privs_t
*daemon_privs
)
852 install_element(CONFIG_NODE
, &vrf_cmd
);
853 install_element(CONFIG_NODE
, &no_vrf_cmd
);
854 install_node(&vrf_node
, writefunc
);
855 install_default(VRF_NODE
);
856 install_element(VRF_NODE
, &vrf_exit_cmd
);
857 if (vrf_is_backend_netns() && ns_have_netns()) {
858 /* Install NS commands. */
859 vrf_daemon_privs
= daemon_privs
;
860 install_element(VRF_NODE
, &vrf_netns_cmd
);
861 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
865 void vrf_set_default_name(const char *default_name
, bool force
)
868 static bool def_vrf_forced
;
870 def_vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
871 assert(default_name
);
872 if (def_vrf
&& !force
&& def_vrf_forced
) {
873 zlog_debug("VRF: %s, avoid changing name to %s, previously forced (%u)",
874 def_vrf
->name
, default_name
,
878 if (strmatch(vrf_default_name
, default_name
))
880 snprintf(vrf_default_name
, VRF_NAMSIZ
, "%s", default_name
);
883 def_vrf_forced
= true;
884 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, def_vrf
);
885 strlcpy(def_vrf
->data
.l
.netns_name
,
886 vrf_default_name
, NS_NAMSIZ
);
887 strlcpy(def_vrf
->name
, vrf_default_name
, sizeof(def_vrf
->name
));
888 RB_INSERT(vrf_name_head
, &vrfs_by_name
, def_vrf
);
889 if (vrf_master
.vrf_update_name_hook
)
890 (*vrf_master
.vrf_update_name_hook
)(def_vrf
);
894 const char *vrf_get_default_name(void)
896 return vrf_default_name
;
899 vrf_id_t
vrf_get_default_id(void)
901 /* backend netns is only known by zebra
902 * for other daemons, we return VRF_DEFAULT_INTERNAL
904 if (vrf_is_backend_netns())
905 return ns_get_default_id();
907 return VRF_DEFAULT_INTERNAL
;
910 int vrf_bind(vrf_id_t vrf_id
, int fd
, const char *name
)
913 struct interface
*ifp
;
915 if (fd
< 0 || name
== NULL
)
917 /* the device should exist
918 * otherwise we should return
919 * case ifname = vrf in netns mode => return
921 ifp
= if_lookup_by_name(name
, vrf_id
);
924 #ifdef SO_BINDTODEVICE
925 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
927 zlog_debug("bind to interface %s failed, errno=%d", name
,
929 #endif /* SO_BINDTODEVICE */
932 int vrf_getaddrinfo(const char *node
, const char *service
,
933 const struct addrinfo
*hints
, struct addrinfo
**res
,
936 int ret
, ret2
, save_errno
;
938 ret
= vrf_switch_to_netns(vrf_id
);
940 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
941 __func__
, vrf_id
, safe_strerror(errno
));
942 ret
= getaddrinfo(node
, service
, hints
, res
);
944 ret2
= vrf_switchback_to_initial();
946 flog_err_sys(EC_LIB_SOCKET
,
947 "%s: Can't switchback from VRF %u (%s)", __func__
,
948 vrf_id
, safe_strerror(errno
));
953 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
955 int ret
, saved_errno
, rc
;
957 ret
= vrf_switch_to_netns(vrf_id
);
959 flog_err_sys(EC_LIB_SOCKET
, "%s: Can't switch to VRF %u (%s)",
960 __func__
, vrf_id
, safe_strerror(errno
));
963 rc
= ioctl(d
, request
, params
);
965 ret
= vrf_switchback_to_initial();
967 flog_err_sys(EC_LIB_SOCKET
,
968 "%s: Can't switchback from VRF %u (%s)", __func__
,
969 vrf_id
, safe_strerror(errno
));
974 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
975 const char *interfacename
)
977 int ret
, save_errno
, ret2
;
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
= sockunion_socket(su
);
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 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
1002 vrf_id_t
vrf_generate_id(void)
1004 static int vrf_id_local
;
1006 return ++vrf_id_local
;