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-IP-Routing-Table"
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 struct zebra_privs_t
*vrf_daemon_privs
;
60 static char vrf_default_name
[VRF_NAMSIZ
] = VRF_DEFAULT_NAME_INTERNAL
;
63 * Turn on/off debug code
68 /* Holding VRF hooks */
70 int (*vrf_new_hook
)(struct vrf
*);
71 int (*vrf_delete_hook
)(struct vrf
*);
72 int (*vrf_enable_hook
)(struct vrf
*);
73 int (*vrf_disable_hook
)(struct vrf
*);
78 static int vrf_is_enabled(struct vrf
*vrf
);
80 /* VRF list existance check by name. */
81 struct vrf
*vrf_lookup_by_name(const char *name
)
84 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
85 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
88 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
90 return (a
->vrf_id
- b
->vrf_id
);
93 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
95 return strcmp(a
->name
, b
->name
);
98 /* if ns_id is different and not VRF_UNKNOWN,
99 * then update vrf identifier, and enable VRF
101 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
103 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
105 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
109 old_vrf_id
= vrf
->vrf_id
;
110 if (vrf_id
== vrf
->vrf_id
)
112 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
113 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
114 vrf
->vrf_id
= vrf_id
;
115 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
116 if (old_vrf_id
== VRF_UNKNOWN
)
117 vrf_enable((struct vrf
*)vrf
);
120 int vrf_switch_to_netns(vrf_id_t vrf_id
)
123 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
125 /* VRF is default VRF. silently ignore */
126 if (!vrf
|| vrf
->vrf_id
== VRF_DEFAULT
)
127 return 1; /* 1 = default */
128 /* VRF has no NETNS backend. silently ignore */
129 if (vrf
->data
.l
.netns_name
[0] == '\0')
130 return 2; /* 2 = no netns */
131 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
133 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
134 return ns_switch_to_netns(name
);
137 int vrf_switchback_to_initial(void)
139 int ret
= ns_switchback_to_initial();
141 if (ret
== 0 && debug_vrf
)
142 zlog_debug("VRF_SWITCHBACK");
146 /* Get a VRF. If not found, create one.
148 * name - The name of the vrf. May be NULL if unknown.
149 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
150 * Description: Please note that this routine can be called with just the name
153 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
155 struct vrf
*vrf
= NULL
;
159 zlog_debug("VRF_GET: %s(%u)", name
== NULL
? "(NULL)" : name
,
162 /* Nothing to see, move along here */
163 if (!name
&& vrf_id
== VRF_UNKNOWN
)
166 /* attempt to find already available VRF
169 vrf
= vrf_lookup_by_name(name
);
170 /* Try to find VRF both by ID and name */
171 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
172 vrf
= vrf_lookup_by_id(vrf_id
);
175 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
176 vrf
->vrf_id
= VRF_UNKNOWN
;
181 zlog_debug("VRF(%u) %s is created.", vrf_id
,
182 (name
) ? name
: "(NULL)");
186 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
187 vrf
->vrf_id
= vrf_id
;
188 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
192 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
193 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
194 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
195 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
196 } else if (name
&& vrf
->name
[0] == '\0') {
197 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
198 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
200 if (new &&vrf_master
.vrf_new_hook
)
201 (*vrf_master
.vrf_new_hook
)(vrf
);
206 /* Delete a VRF. This is called when the underlying VRF goes away, a
207 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
209 void vrf_delete(struct vrf
*vrf
)
212 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
214 if (vrf_is_enabled(vrf
))
217 /* If the VRF is user configured, it'll stick around, just remove
218 * the ID mapping. Interfaces assigned to this VRF should've been
219 * removed already as part of the VRF going down.
221 if (vrf_is_user_cfged(vrf
)) {
222 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
223 /* Delete any VRF interfaces - should be only
224 * the VRF itself, other interfaces should've
225 * been moved out of the VRF.
228 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
229 vrf
->vrf_id
= VRF_UNKNOWN
;
234 if (vrf_master
.vrf_delete_hook
)
235 (*vrf_master
.vrf_delete_hook
)(vrf
);
240 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
241 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
242 if (vrf
->name
[0] != '\0')
243 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
245 XFREE(MTYPE_VRF
, vrf
);
248 /* Look up a VRF by identifier. */
249 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
253 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
257 * Enable a VRF - that is, let the VRF be ready to use.
258 * The VRF_ENABLE_HOOK callback will be called to inform
259 * that they can allocate resources in this VRF.
261 * RETURN: 1 - enabled successfully; otherwise, 0.
263 int vrf_enable(struct vrf
*vrf
)
265 if (vrf_is_enabled(vrf
))
269 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
271 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
273 if (vrf_master
.vrf_enable_hook
)
274 (*vrf_master
.vrf_enable_hook
)(vrf
);
277 * If we have any nexthop group entries that
278 * are awaiting vrf initialization then
279 * let's let people know about it
281 nexthop_group_enable_vrf(vrf
);
287 * Disable a VRF - that is, let the VRF be unusable.
288 * The VRF_DELETE_HOOK callback will be called to inform
289 * that they must release the resources in the VRF.
291 void vrf_disable(struct vrf
*vrf
)
293 if (!vrf_is_enabled(vrf
))
296 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
299 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
301 /* Till now, nothing to be done for the default VRF. */
302 // Pending: see why this statement.
304 if (vrf_master
.vrf_disable_hook
)
305 (*vrf_master
.vrf_disable_hook
)(vrf
);
308 const char *vrf_id_to_name(vrf_id_t vrf_id
)
312 vrf
= vrf_lookup_by_id(vrf_id
);
319 vrf_id_t
vrf_name_to_id(const char *name
)
322 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
323 // id/ routine not used.
325 vrf
= vrf_lookup_by_name(name
);
327 vrf_id
= vrf
->vrf_id
;
332 /* Get the data pointer of the specified VRF. If not found, create one. */
333 void *vrf_info_get(vrf_id_t vrf_id
)
335 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
339 /* Look up the data pointer of the specified VRF. */
340 void *vrf_info_lookup(vrf_id_t vrf_id
)
342 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
343 return vrf
? vrf
->info
: NULL
;
347 * VRF hash for storing set or not.
354 static unsigned int vrf_hash_bitmap_key(void *data
)
356 struct vrf_bit_set
*bit
= data
;
361 static int vrf_hash_bitmap_cmp(const void *a
, const void *b
)
363 const struct vrf_bit_set
*bit1
= a
;
364 const struct vrf_bit_set
*bit2
= b
;
366 return bit1
->vrf_id
== bit2
->vrf_id
;
369 static void *vrf_hash_bitmap_alloc(void *data
)
371 struct vrf_bit_set
*copy
= data
;
372 struct vrf_bit_set
*bit
;
374 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
375 bit
->vrf_id
= copy
->vrf_id
;
380 static void vrf_hash_bitmap_free(void *data
)
382 struct vrf_bit_set
*bit
= data
;
384 XFREE(MTYPE_VRF_BITMAP
, bit
);
387 vrf_bitmap_t
vrf_bitmap_init(void)
389 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
393 void vrf_bitmap_free(vrf_bitmap_t bmap
)
395 struct hash
*vrf_hash
= bmap
;
397 if (vrf_hash
== NULL
)
400 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
404 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
406 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
407 struct hash
*vrf_hash
= bmap
;
408 struct vrf_bit_set
*bit
;
410 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
413 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
417 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
419 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
420 struct hash
*vrf_hash
= bmap
;
421 struct vrf_bit_set
*bit
;
423 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
426 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
430 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
432 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
433 struct hash
*vrf_hash
= bmap
;
434 struct vrf_bit_set
*bit
;
436 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
439 bit
= hash_lookup(vrf_hash
, &lookup
);
446 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
448 struct vrf
*vrf
= NULL
;
450 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
451 if (vrf
->vrf_id
!= VRF_DEFAULT
)
452 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
456 static const struct cmd_variable_handler vrf_var_handlers
[] = {
459 .completions
= vrf_autocomplete
,
461 {.completions
= NULL
},
464 /* Initialize VRF module. */
465 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
466 int (*disable
)(struct vrf
*), int (*delete)(struct vrf
*))
468 struct vrf
*default_vrf
;
470 /* initialise NS, in case VRF backend if NETNS */
473 zlog_debug("%s: Initializing VRF subsystem",
474 __PRETTY_FUNCTION__
);
476 vrf_master
.vrf_new_hook
= create
;
477 vrf_master
.vrf_enable_hook
= enable
;
478 vrf_master
.vrf_disable_hook
= disable
;
479 vrf_master
.vrf_delete_hook
= delete;
481 /* The default VRF always exists. */
482 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
484 flog_err(LIB_ERR_VRF_START
,
485 "vrf_init: failed to create the default VRF!");
489 /* Enable the default VRF. */
490 if (!vrf_enable(default_vrf
)) {
491 flog_err(LIB_ERR_VRF_START
,
492 "vrf_init: failed to enable the default VRF!");
496 cmd_variable_handler_register(vrf_var_handlers
);
499 /* Terminate VRF module. */
500 void vrf_terminate(void)
505 zlog_debug("%s: Shutting down vrf subsystem",
506 __PRETTY_FUNCTION__
);
508 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
509 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
511 /* Clear configured flag and invoke delete. */
512 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
516 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
517 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
519 /* Clear configured flag and invoke delete. */
520 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
525 static int vrf_default_accepts_vrf(int type
)
527 const char *fname
= NULL
;
528 char buf
[32] = {0x0};
533 * TCP & UDP services running in the default VRF context (ie., not bound
534 * to any VRF device) can work across all VRF domains by enabling the
535 * tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
536 * sysctl -w net.ipv4.tcp_l3mdev_accept=1
537 * sysctl -w net.ipv4.udp_l3mdev_accept=1
539 if (type
== SOCK_STREAM
)
540 fname
= "/proc/sys/net/ipv4/tcp_l3mdev_accept";
541 else if (type
== SOCK_DGRAM
)
542 fname
= "/proc/sys/net/ipv4/udp_l3mdev_accept";
545 fd
= fopen(fname
, "r");
554 /* Create a socket for the VRF. */
555 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
558 int ret
, save_errno
, ret2
;
560 ret
= vrf_switch_to_netns(vrf_id
);
562 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
563 __func__
, vrf_id
, safe_strerror(errno
));
565 if (ret
> 0 && interfacename
&& vrf_default_accepts_vrf(type
)) {
566 zlog_err("VRF socket not used since net.ipv4.%s_l3mdev_accept != 0",
567 (type
== SOCK_STREAM
? "tcp" : "udp"));
568 errno
= EEXIST
; /* not sure if this is the best error... */
572 ret
= socket(domain
, type
, protocol
);
574 ret2
= vrf_switchback_to_initial();
576 flog_err_sys(LIB_ERR_SOCKET
,
577 "%s: Can't switchback from VRF %u (%s)", __func__
,
578 vrf_id
, safe_strerror(errno
));
582 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
590 int vrf_is_backend_netns(void)
592 return (vrf_backend
== VRF_BACKEND_NETNS
);
595 int vrf_get_backend(void)
600 void vrf_configure_backend(int vrf_backend_netns
)
602 vrf_backend
= vrf_backend_netns
;
605 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
610 if (strlen(vrfname
) > VRF_NAMSIZ
) {
613 "%% VRF name %s invalid: length exceeds %d bytes\n",
614 vrfname
, VRF_NAMSIZ
);
617 "%% VRF name %s invalid: length exceeds %d bytes\n",
618 vrfname
, VRF_NAMSIZ
);
619 return CMD_WARNING_CONFIG_FAILED
;
622 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
625 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
632 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
633 ns_id_t ns_id
, ns_id_t internal_ns_id
)
635 struct ns
*ns
= NULL
;
638 return CMD_WARNING_CONFIG_FAILED
;
639 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
642 "VRF %u is already configured with VRF %s\n",
643 vrf
->vrf_id
, vrf
->name
);
645 zlog_warn("VRF %u is already configured with VRF %s\n",
646 vrf
->vrf_id
, vrf
->name
);
647 return CMD_WARNING_CONFIG_FAILED
;
649 if (vrf
->ns_ctxt
!= NULL
) {
650 ns
= (struct ns
*)vrf
->ns_ctxt
;
651 if (!strcmp(ns
->name
, pathname
)) {
654 "VRF %u already configured with NETNS %s\n",
655 vrf
->vrf_id
, ns
->name
);
658 "VRF %u already configured with NETNS %s",
659 vrf
->vrf_id
, ns
->name
);
660 return CMD_WARNING_CONFIG_FAILED
;
663 ns
= ns_lookup_name(pathname
);
664 if (ns
&& ns
->vrf_ctxt
) {
665 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
671 "NS %s is already configured"
672 " with VRF %u(%s)\n",
673 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
675 zlog_warn("NS %s is already configured with VRF %u(%s)",
676 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
677 return CMD_WARNING_CONFIG_FAILED
;
679 ns
= ns_get_created(ns
, pathname
, ns_id
);
680 ns
->internal_ns_id
= internal_ns_id
;
681 ns
->vrf_ctxt
= (void *)vrf
;
682 vrf
->ns_ctxt
= (void *)ns
;
683 /* update VRF netns NAME */
684 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
686 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
688 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
689 ns
->ns_id
, ns
->name
);
691 zlog_warn("Can not associate NS %u with NETNS %s",
692 ns
->ns_id
, ns
->name
);
693 return CMD_WARNING_CONFIG_FAILED
;
699 int vrf_is_mapped_on_netns(struct vrf
*vrf
)
701 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
703 if (vrf
->vrf_id
== VRF_DEFAULT
)
708 /* vrf CLI commands */
712 "Exit current mode and down to previous mode\n")
714 /* We have to set vrf context to default vrf */
715 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
716 vty
->node
= CONFIG_NODE
;
723 "Select a VRF to configure\n"
727 const char *vrfname
= argv
[idx_name
]->arg
;
729 return vrf_handler_create(vty
, vrfname
, NULL
);
736 "Delete a pseudo VRF's configuration\n"
739 const char *vrfname
= argv
[2]->arg
;
743 vrfp
= vrf_lookup_by_name(vrfname
);
746 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
747 return CMD_WARNING_CONFIG_FAILED
;
750 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
751 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
752 return CMD_WARNING_CONFIG_FAILED
;
755 /* Clear configured flag and invoke delete. */
756 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
763 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
765 DEFUN_NOSH (vrf_netns
,
768 "Attach VRF to a Namespace\n"
769 "The file name in " NS_RUN_DIR
", or a full pathname\n")
771 int idx_name
= 1, ret
;
772 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
774 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
777 return CMD_WARNING_CONFIG_FAILED
;
779 frr_elevate_privs(vrf_daemon_privs
) {
780 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
781 NS_UNKNOWN
, NS_UNKNOWN
);
786 DEFUN_NOSH (no_vrf_netns
,
790 "Detach VRF from a Namespace\n"
791 "The file name in " NS_RUN_DIR
", or a full pathname\n")
793 struct ns
*ns
= NULL
;
795 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
797 if (!vrf_is_backend_netns()) {
798 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
799 return CMD_WARNING_CONFIG_FAILED
;
802 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
803 vrf
->name
, vrf
->vrf_id
);
804 return CMD_WARNING_CONFIG_FAILED
;
807 ns
= (struct ns
*)vrf
->ns_ctxt
;
811 /* vrf ID from VRF is necessary for Zebra
812 * so that propagate to other clients is done
820 * Debug CLI for vrf's
845 static int vrf_write_host(struct vty
*vty
)
848 vty_out(vty
, "debug vrf\n");
853 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
855 void vrf_install_commands(void)
857 install_node(&vrf_debug_node
, vrf_write_host
);
859 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
860 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
861 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
862 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
865 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
866 struct zebra_privs_t
*daemon_privs
)
868 install_element(CONFIG_NODE
, &vrf_cmd
);
869 install_element(CONFIG_NODE
, &no_vrf_cmd
);
870 install_node(&vrf_node
, writefunc
);
871 install_default(VRF_NODE
);
872 install_element(VRF_NODE
, &vrf_exit_cmd
);
873 if (vrf_is_backend_netns() && ns_have_netns()) {
874 /* Install NS commands. */
875 vrf_daemon_privs
= daemon_privs
;
876 install_element(VRF_NODE
, &vrf_netns_cmd
);
877 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
881 void vrf_set_default_name(const char *default_name
)
885 def_vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
886 assert(default_name
);
887 vrf_with_default_name
= vrf_lookup_by_name(default_name
);
888 if (vrf_with_default_name
&& vrf_with_default_name
!= def_vrf
) {
889 /* vrf name already used by an other VRF */
890 zlog_debug("VRF: %s, avoid changing name to %s, same name exists (%u)",
891 vrf_with_default_name
->name
, default_name
,
892 vrf_with_default_name
->vrf_id
);
895 snprintf(vrf_default_name
, VRF_NAMSIZ
, "%s", default_name
);
897 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, def_vrf
);
898 strlcpy(def_vrf
->data
.l
.netns_name
,
899 vrf_default_name
, NS_NAMSIZ
);
900 strlcpy(def_vrf
->name
, vrf_default_name
, sizeof(def_vrf
->name
));
901 RB_INSERT(vrf_name_head
, &vrfs_by_name
, def_vrf
);
905 const char *vrf_get_default_name(void)
907 return vrf_default_name
;
910 vrf_id_t
vrf_get_default_id(void)
912 /* backend netns is only known by zebra
913 * for other daemons, we return VRF_DEFAULT_INTERNAL
915 if (vrf_is_backend_netns())
916 return ns_get_default_id();
918 return VRF_DEFAULT_INTERNAL
;
921 int vrf_bind(vrf_id_t vrf_id
, int fd
, char *name
)
925 if (fd
< 0 || name
== NULL
)
927 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id
)))
929 #ifdef SO_BINDTODEVICE
930 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
932 zlog_debug("bind to interface %s failed, errno=%d", name
,
934 #endif /* SO_BINDTODEVICE */
937 int vrf_getaddrinfo(const char *node
, const char *service
,
938 const struct addrinfo
*hints
, struct addrinfo
**res
,
941 int ret
, ret2
, save_errno
;
943 ret
= vrf_switch_to_netns(vrf_id
);
945 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
946 __func__
, vrf_id
, safe_strerror(errno
));
947 ret
= getaddrinfo(node
, service
, hints
, res
);
949 ret2
= vrf_switchback_to_initial();
951 flog_err_sys(LIB_ERR_SOCKET
,
952 "%s: Can't switchback from VRF %u (%s)", __func__
,
953 vrf_id
, safe_strerror(errno
));
958 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
960 int ret
, saved_errno
, rc
;
962 ret
= vrf_switch_to_netns(vrf_id
);
964 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
965 __func__
, vrf_id
, safe_strerror(errno
));
968 rc
= ioctl(d
, request
, params
);
970 ret
= vrf_switchback_to_initial();
972 flog_err_sys(LIB_ERR_SOCKET
,
973 "%s: Can't switchback from VRF %u (%s)", __func__
,
974 vrf_id
, safe_strerror(errno
));
979 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
982 int ret
, save_errno
, ret2
;
984 ret
= vrf_switch_to_netns(vrf_id
);
986 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
987 __func__
, vrf_id
, safe_strerror(errno
));
988 ret
= sockunion_socket(su
);
990 ret2
= vrf_switchback_to_initial();
992 flog_err_sys(LIB_ERR_SOCKET
,
993 "%s: Can't switchback from VRF %u (%s)", __func__
,
994 vrf_id
, safe_strerror(errno
));
999 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);