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 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
*);
74 int (*vrf_update_name_hook
)(struct vrf
*vrf
);
79 static int vrf_is_enabled(struct vrf
*vrf
);
81 /* VRF list existance check by name. */
82 struct vrf
*vrf_lookup_by_name(const char *name
)
85 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
86 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
89 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
91 return (a
->vrf_id
- b
->vrf_id
);
94 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
96 return strcmp(a
->name
, b
->name
);
99 /* if ns_id is different and not VRF_UNKNOWN,
100 * then update vrf identifier, and enable VRF
102 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
104 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
106 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
110 old_vrf_id
= vrf
->vrf_id
;
111 if (vrf_id
== vrf
->vrf_id
)
113 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
114 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
115 vrf
->vrf_id
= vrf_id
;
116 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
117 if (old_vrf_id
== VRF_UNKNOWN
)
118 vrf_enable((struct vrf
*)vrf
);
121 int vrf_switch_to_netns(vrf_id_t vrf_id
)
124 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
126 /* VRF is default VRF. silently ignore */
127 if (!vrf
|| vrf
->vrf_id
== VRF_DEFAULT
)
128 return 1; /* 1 = default */
129 /* VRF has no NETNS backend. silently ignore */
130 if (vrf
->data
.l
.netns_name
[0] == '\0')
131 return 2; /* 2 = no netns */
132 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
134 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
135 return ns_switch_to_netns(name
);
138 int vrf_switchback_to_initial(void)
140 int ret
= ns_switchback_to_initial();
142 if (ret
== 0 && debug_vrf
)
143 zlog_debug("VRF_SWITCHBACK");
147 /* Get a VRF. If not found, create one.
149 * name - The name of the vrf. May be NULL if unknown.
150 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
151 * Description: Please note that this routine can be called with just the name
154 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
156 struct vrf
*vrf
= NULL
;
160 zlog_debug("VRF_GET: %s(%u)", name
== NULL
? "(NULL)" : name
,
163 /* Nothing to see, move along here */
164 if (!name
&& vrf_id
== VRF_UNKNOWN
)
167 /* attempt to find already available VRF
170 vrf
= vrf_lookup_by_name(name
);
171 if (vrf
&& vrf_id
!= VRF_UNKNOWN
172 && vrf
->vrf_id
!= VRF_UNKNOWN
173 && vrf
->vrf_id
!= vrf_id
) {
174 zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)",
175 name
, vrf_id
, vrf
->vrf_id
);
178 /* Try to find VRF both by ID and name */
179 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
180 vrf
= vrf_lookup_by_id(vrf_id
);
183 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
184 vrf
->vrf_id
= VRF_UNKNOWN
;
189 zlog_debug("VRF(%u) %s is created.", vrf_id
,
190 (name
) ? name
: "(NULL)");
194 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
195 vrf
->vrf_id
= vrf_id
;
196 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
200 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
201 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
202 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
203 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
204 } else if (name
&& vrf
->name
[0] == '\0') {
205 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
206 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
208 if (new &&vrf_master
.vrf_new_hook
)
209 (*vrf_master
.vrf_new_hook
)(vrf
);
214 /* Delete a VRF. This is called when the underlying VRF goes away, a
215 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
217 void vrf_delete(struct vrf
*vrf
)
220 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
222 if (vrf_is_enabled(vrf
))
225 /* If the VRF is user configured, it'll stick around, just remove
226 * the ID mapping. Interfaces assigned to this VRF should've been
227 * removed already as part of the VRF going down.
229 if (vrf_is_user_cfged(vrf
)) {
230 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
231 /* Delete any VRF interfaces - should be only
232 * the VRF itself, other interfaces should've
233 * been moved out of the VRF.
236 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
237 vrf
->vrf_id
= VRF_UNKNOWN
;
242 if (vrf_master
.vrf_delete_hook
)
243 (*vrf_master
.vrf_delete_hook
)(vrf
);
248 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
249 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
250 if (vrf
->name
[0] != '\0')
251 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
253 XFREE(MTYPE_VRF
, vrf
);
256 /* Look up a VRF by identifier. */
257 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
261 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
265 * Enable a VRF - that is, let the VRF be ready to use.
266 * The VRF_ENABLE_HOOK callback will be called to inform
267 * that they can allocate resources in this VRF.
269 * RETURN: 1 - enabled successfully; otherwise, 0.
271 int vrf_enable(struct vrf
*vrf
)
273 if (vrf_is_enabled(vrf
))
277 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
279 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
281 if (vrf_master
.vrf_enable_hook
)
282 (*vrf_master
.vrf_enable_hook
)(vrf
);
285 * If we have any nexthop group entries that
286 * are awaiting vrf initialization then
287 * let's let people know about it
289 nexthop_group_enable_vrf(vrf
);
295 * Disable a VRF - that is, let the VRF be unusable.
296 * The VRF_DELETE_HOOK callback will be called to inform
297 * that they must release the resources in the VRF.
299 void vrf_disable(struct vrf
*vrf
)
301 if (!vrf_is_enabled(vrf
))
304 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
307 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
309 /* Till now, nothing to be done for the default VRF. */
310 // Pending: see why this statement.
312 if (vrf_master
.vrf_disable_hook
)
313 (*vrf_master
.vrf_disable_hook
)(vrf
);
316 const char *vrf_id_to_name(vrf_id_t vrf_id
)
320 vrf
= vrf_lookup_by_id(vrf_id
);
327 vrf_id_t
vrf_name_to_id(const char *name
)
330 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
331 // id/ routine not used.
333 vrf
= vrf_lookup_by_name(name
);
335 vrf_id
= vrf
->vrf_id
;
340 /* Get the data pointer of the specified VRF. If not found, create one. */
341 void *vrf_info_get(vrf_id_t vrf_id
)
343 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
347 /* Look up the data pointer of the specified VRF. */
348 void *vrf_info_lookup(vrf_id_t vrf_id
)
350 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
351 return vrf
? vrf
->info
: NULL
;
355 * VRF hash for storing set or not.
362 static unsigned int vrf_hash_bitmap_key(void *data
)
364 struct vrf_bit_set
*bit
= data
;
369 static int vrf_hash_bitmap_cmp(const void *a
, const void *b
)
371 const struct vrf_bit_set
*bit1
= a
;
372 const struct vrf_bit_set
*bit2
= b
;
374 return bit1
->vrf_id
== bit2
->vrf_id
;
377 static void *vrf_hash_bitmap_alloc(void *data
)
379 struct vrf_bit_set
*copy
= data
;
380 struct vrf_bit_set
*bit
;
382 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
383 bit
->vrf_id
= copy
->vrf_id
;
388 static void vrf_hash_bitmap_free(void *data
)
390 struct vrf_bit_set
*bit
= data
;
392 XFREE(MTYPE_VRF_BITMAP
, bit
);
395 vrf_bitmap_t
vrf_bitmap_init(void)
397 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
401 void vrf_bitmap_free(vrf_bitmap_t bmap
)
403 struct hash
*vrf_hash
= bmap
;
405 if (vrf_hash
== NULL
)
408 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
412 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
414 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
415 struct hash
*vrf_hash
= bmap
;
416 struct vrf_bit_set
*bit
;
418 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
421 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
425 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
427 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
428 struct hash
*vrf_hash
= bmap
;
429 struct vrf_bit_set
*bit
;
431 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
434 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
438 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
440 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
441 struct hash
*vrf_hash
= bmap
;
442 struct vrf_bit_set
*bit
;
444 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
447 bit
= hash_lookup(vrf_hash
, &lookup
);
454 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
456 struct vrf
*vrf
= NULL
;
458 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
459 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
462 static const struct cmd_variable_handler vrf_var_handlers
[] = {
465 .completions
= vrf_autocomplete
,
467 {.completions
= NULL
},
470 /* Initialize VRF module. */
471 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
472 int (*disable
)(struct vrf
*), int (*delete)(struct vrf
*),
473 int ((*update
)(struct vrf
*)))
475 struct vrf
*default_vrf
;
477 /* initialise NS, in case VRF backend if NETNS */
480 zlog_debug("%s: Initializing VRF subsystem",
481 __PRETTY_FUNCTION__
);
483 vrf_master
.vrf_new_hook
= create
;
484 vrf_master
.vrf_enable_hook
= enable
;
485 vrf_master
.vrf_disable_hook
= disable
;
486 vrf_master
.vrf_delete_hook
= delete;
487 vrf_master
.vrf_update_name_hook
= update
;
489 /* The default VRF always exists. */
490 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
492 flog_err(LIB_ERR_VRF_START
,
493 "vrf_init: failed to create the default VRF!");
496 if (vrf_is_backend_netns())
497 strlcpy(default_vrf
->data
.l
.netns_name
,
498 VRF_DEFAULT_NAME
, NS_NAMSIZ
);
500 /* Enable the default VRF. */
501 if (!vrf_enable(default_vrf
)) {
502 flog_err(LIB_ERR_VRF_START
,
503 "vrf_init: failed to enable the default VRF!");
507 cmd_variable_handler_register(vrf_var_handlers
);
510 /* Terminate VRF module. */
511 void vrf_terminate(void)
516 zlog_debug("%s: Shutting down vrf subsystem",
517 __PRETTY_FUNCTION__
);
519 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
520 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
522 /* Clear configured flag and invoke delete. */
523 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
527 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
528 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
530 /* Clear configured flag and invoke delete. */
531 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
536 static int vrf_default_accepts_vrf(int type
)
538 const char *fname
= NULL
;
539 char buf
[32] = {0x0};
544 * TCP & UDP services running in the default VRF context (ie., not bound
545 * to any VRF device) can work across all VRF domains by enabling the
546 * tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
547 * sysctl -w net.ipv4.tcp_l3mdev_accept=1
548 * sysctl -w net.ipv4.udp_l3mdev_accept=1
550 if (type
== SOCK_STREAM
)
551 fname
= "/proc/sys/net/ipv4/tcp_l3mdev_accept";
552 else if (type
== SOCK_DGRAM
)
553 fname
= "/proc/sys/net/ipv4/udp_l3mdev_accept";
556 fd
= fopen(fname
, "r");
565 /* Create a socket for the VRF. */
566 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
569 int ret
, save_errno
, ret2
;
571 ret
= vrf_switch_to_netns(vrf_id
);
573 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
574 __func__
, vrf_id
, safe_strerror(errno
));
576 if (ret
> 0 && interfacename
&& vrf_default_accepts_vrf(type
)) {
577 zlog_err("VRF socket not used since net.ipv4.%s_l3mdev_accept != 0",
578 (type
== SOCK_STREAM
? "tcp" : "udp"));
579 errno
= EEXIST
; /* not sure if this is the best error... */
583 ret
= socket(domain
, type
, protocol
);
585 ret2
= vrf_switchback_to_initial();
587 flog_err_sys(LIB_ERR_SOCKET
,
588 "%s: Can't switchback from VRF %u (%s)", __func__
,
589 vrf_id
, safe_strerror(errno
));
593 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
601 int vrf_is_backend_netns(void)
603 return (vrf_backend
== VRF_BACKEND_NETNS
);
606 int vrf_get_backend(void)
611 void vrf_configure_backend(int vrf_backend_netns
)
613 vrf_backend
= vrf_backend_netns
;
616 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
621 if (strlen(vrfname
) > VRF_NAMSIZ
) {
624 "%% VRF name %s invalid: length exceeds %d bytes\n",
625 vrfname
, VRF_NAMSIZ
);
628 "%% VRF name %s invalid: length exceeds %d bytes\n",
629 vrfname
, VRF_NAMSIZ
);
630 return CMD_WARNING_CONFIG_FAILED
;
633 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
636 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
643 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
644 ns_id_t ns_id
, ns_id_t internal_ns_id
)
646 struct ns
*ns
= NULL
;
649 return CMD_WARNING_CONFIG_FAILED
;
650 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
653 "VRF %u is already configured with VRF %s\n",
654 vrf
->vrf_id
, vrf
->name
);
656 zlog_warn("VRF %u is already configured with VRF %s\n",
657 vrf
->vrf_id
, vrf
->name
);
658 return CMD_WARNING_CONFIG_FAILED
;
660 if (vrf
->ns_ctxt
!= NULL
) {
661 ns
= (struct ns
*)vrf
->ns_ctxt
;
662 if (!strcmp(ns
->name
, pathname
)) {
665 "VRF %u already configured with NETNS %s\n",
666 vrf
->vrf_id
, ns
->name
);
669 "VRF %u already configured with NETNS %s",
670 vrf
->vrf_id
, ns
->name
);
671 return CMD_WARNING_CONFIG_FAILED
;
674 ns
= ns_lookup_name(pathname
);
675 if (ns
&& ns
->vrf_ctxt
) {
676 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
682 "NS %s is already configured"
683 " with VRF %u(%s)\n",
684 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
686 zlog_warn("NS %s is already configured with VRF %u(%s)",
687 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
688 return CMD_WARNING_CONFIG_FAILED
;
690 ns
= ns_get_created(ns
, pathname
, ns_id
);
691 ns
->internal_ns_id
= internal_ns_id
;
692 ns
->vrf_ctxt
= (void *)vrf
;
693 vrf
->ns_ctxt
= (void *)ns
;
694 /* update VRF netns NAME */
695 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
697 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
699 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
700 ns
->ns_id
, ns
->name
);
702 zlog_warn("Can not associate NS %u with NETNS %s",
703 ns
->ns_id
, ns
->name
);
704 return CMD_WARNING_CONFIG_FAILED
;
710 int vrf_is_mapped_on_netns(struct vrf
*vrf
)
712 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
714 if (vrf
->vrf_id
== VRF_DEFAULT
)
719 /* vrf CLI commands */
723 "Exit current mode and down to previous mode\n")
725 /* We have to set vrf context to default vrf */
726 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
727 vty
->node
= CONFIG_NODE
;
734 "Select a VRF to configure\n"
738 const char *vrfname
= argv
[idx_name
]->arg
;
740 return vrf_handler_create(vty
, vrfname
, NULL
);
747 "Delete a pseudo VRF's configuration\n"
750 const char *vrfname
= argv
[2]->arg
;
754 vrfp
= vrf_lookup_by_name(vrfname
);
757 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
758 return CMD_WARNING_CONFIG_FAILED
;
761 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
762 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
763 return CMD_WARNING_CONFIG_FAILED
;
766 /* Clear configured flag and invoke delete. */
767 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
774 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
776 DEFUN_NOSH (vrf_netns
,
779 "Attach VRF to a Namespace\n"
780 "The file name in " NS_RUN_DIR
", or a full pathname\n")
782 int idx_name
= 1, ret
;
783 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
785 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
788 return CMD_WARNING_CONFIG_FAILED
;
790 frr_elevate_privs(vrf_daemon_privs
) {
791 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
792 NS_UNKNOWN
, NS_UNKNOWN
);
797 DEFUN_NOSH (no_vrf_netns
,
801 "Detach VRF from a Namespace\n"
802 "The file name in " NS_RUN_DIR
", or a full pathname\n")
804 struct ns
*ns
= NULL
;
806 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
808 if (!vrf_is_backend_netns()) {
809 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
810 return CMD_WARNING_CONFIG_FAILED
;
813 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
814 vrf
->name
, vrf
->vrf_id
);
815 return CMD_WARNING_CONFIG_FAILED
;
818 ns
= (struct ns
*)vrf
->ns_ctxt
;
822 /* vrf ID from VRF is necessary for Zebra
823 * so that propagate to other clients is done
831 * Debug CLI for vrf's
856 static int vrf_write_host(struct vty
*vty
)
859 vty_out(vty
, "debug vrf\n");
864 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
866 void vrf_install_commands(void)
868 install_node(&vrf_debug_node
, vrf_write_host
);
870 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
871 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
872 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
873 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
876 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
877 struct zebra_privs_t
*daemon_privs
)
879 install_element(CONFIG_NODE
, &vrf_cmd
);
880 install_element(CONFIG_NODE
, &no_vrf_cmd
);
881 install_node(&vrf_node
, writefunc
);
882 install_default(VRF_NODE
);
883 install_element(VRF_NODE
, &vrf_exit_cmd
);
884 if (vrf_is_backend_netns() && ns_have_netns()) {
885 /* Install NS commands. */
886 vrf_daemon_privs
= daemon_privs
;
887 install_element(VRF_NODE
, &vrf_netns_cmd
);
888 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
892 void vrf_set_default_name(const char *default_name
)
895 struct vrf
*vrf_with_default_name
= NULL
;
897 def_vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
898 assert(default_name
);
899 vrf_with_default_name
= vrf_lookup_by_name(default_name
);
900 if (vrf_with_default_name
&& vrf_with_default_name
!= def_vrf
) {
901 /* vrf name already used by an other VRF */
902 zlog_debug("VRF: %s, avoid changing name to %s, same name exists (%u)",
903 vrf_with_default_name
->name
, default_name
,
904 vrf_with_default_name
->vrf_id
);
907 snprintf(vrf_default_name
, VRF_NAMSIZ
, "%s", default_name
);
909 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, def_vrf
);
910 strlcpy(def_vrf
->data
.l
.netns_name
,
911 vrf_default_name
, NS_NAMSIZ
);
912 strlcpy(def_vrf
->name
, vrf_default_name
, sizeof(def_vrf
->name
));
913 RB_INSERT(vrf_name_head
, &vrfs_by_name
, def_vrf
);
914 if (vrf_master
.vrf_update_name_hook
)
915 (*vrf_master
.vrf_update_name_hook
)(def_vrf
);
919 const char *vrf_get_default_name(void)
921 return vrf_default_name
;
924 vrf_id_t
vrf_get_default_id(void)
926 /* backend netns is only known by zebra
927 * for other daemons, we return VRF_DEFAULT_INTERNAL
929 if (vrf_is_backend_netns())
930 return ns_get_default_id();
932 return VRF_DEFAULT_INTERNAL
;
935 int vrf_bind(vrf_id_t vrf_id
, int fd
, char *name
)
939 if (fd
< 0 || name
== NULL
)
941 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id
)))
943 #ifdef SO_BINDTODEVICE
944 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
946 zlog_debug("bind to interface %s failed, errno=%d", name
,
948 #endif /* SO_BINDTODEVICE */
951 int vrf_getaddrinfo(const char *node
, const char *service
,
952 const struct addrinfo
*hints
, struct addrinfo
**res
,
955 int ret
, ret2
, save_errno
;
957 ret
= vrf_switch_to_netns(vrf_id
);
959 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
960 __func__
, vrf_id
, safe_strerror(errno
));
961 ret
= getaddrinfo(node
, service
, hints
, res
);
963 ret2
= vrf_switchback_to_initial();
965 flog_err_sys(LIB_ERR_SOCKET
,
966 "%s: Can't switchback from VRF %u (%s)", __func__
,
967 vrf_id
, safe_strerror(errno
));
972 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
974 int ret
, saved_errno
, rc
;
976 ret
= vrf_switch_to_netns(vrf_id
);
978 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
979 __func__
, vrf_id
, safe_strerror(errno
));
982 rc
= ioctl(d
, request
, params
);
984 ret
= vrf_switchback_to_initial();
986 flog_err_sys(LIB_ERR_SOCKET
,
987 "%s: Can't switchback from VRF %u (%s)", __func__
,
988 vrf_id
, safe_strerror(errno
));
993 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
996 int ret
, save_errno
, ret2
;
998 ret
= vrf_switch_to_netns(vrf_id
);
1000 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
1001 __func__
, vrf_id
, safe_strerror(errno
));
1002 ret
= sockunion_socket(su
);
1004 ret2
= vrf_switchback_to_initial();
1006 flog_err_sys(LIB_ERR_SOCKET
,
1007 "%s: Can't switchback from VRF %u (%s)", __func__
,
1008 vrf_id
, safe_strerror(errno
));
1013 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);