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
*);
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 /* Try to find VRF both by ID and name */
172 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
173 vrf
= vrf_lookup_by_id(vrf_id
);
176 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
177 vrf
->vrf_id
= VRF_UNKNOWN
;
182 zlog_debug("VRF(%u) %s is created.", vrf_id
,
183 (name
) ? name
: "(NULL)");
187 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
188 vrf
->vrf_id
= vrf_id
;
189 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
193 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
194 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
195 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
196 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
197 } else if (name
&& vrf
->name
[0] == '\0') {
198 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
199 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
201 if (new &&vrf_master
.vrf_new_hook
)
202 (*vrf_master
.vrf_new_hook
)(vrf
);
207 /* Delete a VRF. This is called when the underlying VRF goes away, a
208 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
210 void vrf_delete(struct vrf
*vrf
)
213 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
215 if (vrf_is_enabled(vrf
))
218 /* If the VRF is user configured, it'll stick around, just remove
219 * the ID mapping. Interfaces assigned to this VRF should've been
220 * removed already as part of the VRF going down.
222 if (vrf_is_user_cfged(vrf
)) {
223 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
224 /* Delete any VRF interfaces - should be only
225 * the VRF itself, other interfaces should've
226 * been moved out of the VRF.
229 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
230 vrf
->vrf_id
= VRF_UNKNOWN
;
235 if (vrf_master
.vrf_delete_hook
)
236 (*vrf_master
.vrf_delete_hook
)(vrf
);
241 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
242 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
243 if (vrf
->name
[0] != '\0')
244 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
246 XFREE(MTYPE_VRF
, vrf
);
249 /* Look up a VRF by identifier. */
250 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
254 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
258 * Enable a VRF - that is, let the VRF be ready to use.
259 * The VRF_ENABLE_HOOK callback will be called to inform
260 * that they can allocate resources in this VRF.
262 * RETURN: 1 - enabled successfully; otherwise, 0.
264 int vrf_enable(struct vrf
*vrf
)
266 if (vrf_is_enabled(vrf
))
270 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
272 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
274 if (vrf_master
.vrf_enable_hook
)
275 (*vrf_master
.vrf_enable_hook
)(vrf
);
278 * If we have any nexthop group entries that
279 * are awaiting vrf initialization then
280 * let's let people know about it
282 nexthop_group_enable_vrf(vrf
);
288 * Disable a VRF - that is, let the VRF be unusable.
289 * The VRF_DELETE_HOOK callback will be called to inform
290 * that they must release the resources in the VRF.
292 void vrf_disable(struct vrf
*vrf
)
294 if (!vrf_is_enabled(vrf
))
297 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
300 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
302 /* Till now, nothing to be done for the default VRF. */
303 // Pending: see why this statement.
305 if (vrf_master
.vrf_disable_hook
)
306 (*vrf_master
.vrf_disable_hook
)(vrf
);
309 const char *vrf_id_to_name(vrf_id_t vrf_id
)
313 vrf
= vrf_lookup_by_id(vrf_id
);
320 vrf_id_t
vrf_name_to_id(const char *name
)
323 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
324 // id/ routine not used.
326 vrf
= vrf_lookup_by_name(name
);
328 vrf_id
= vrf
->vrf_id
;
333 /* Get the data pointer of the specified VRF. If not found, create one. */
334 void *vrf_info_get(vrf_id_t vrf_id
)
336 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
340 /* Look up the data pointer of the specified VRF. */
341 void *vrf_info_lookup(vrf_id_t vrf_id
)
343 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
344 return vrf
? vrf
->info
: NULL
;
348 * VRF hash for storing set or not.
355 static unsigned int vrf_hash_bitmap_key(void *data
)
357 struct vrf_bit_set
*bit
= data
;
362 static int vrf_hash_bitmap_cmp(const void *a
, const void *b
)
364 const struct vrf_bit_set
*bit1
= a
;
365 const struct vrf_bit_set
*bit2
= b
;
367 return bit1
->vrf_id
== bit2
->vrf_id
;
370 static void *vrf_hash_bitmap_alloc(void *data
)
372 struct vrf_bit_set
*copy
= data
;
373 struct vrf_bit_set
*bit
;
375 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
376 bit
->vrf_id
= copy
->vrf_id
;
381 static void vrf_hash_bitmap_free(void *data
)
383 struct vrf_bit_set
*bit
= data
;
385 XFREE(MTYPE_VRF_BITMAP
, bit
);
388 vrf_bitmap_t
vrf_bitmap_init(void)
390 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
394 void vrf_bitmap_free(vrf_bitmap_t bmap
)
396 struct hash
*vrf_hash
= bmap
;
398 if (vrf_hash
== NULL
)
401 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
405 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
407 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
408 struct hash
*vrf_hash
= bmap
;
409 struct vrf_bit_set
*bit
;
411 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
414 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
418 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
420 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
421 struct hash
*vrf_hash
= bmap
;
422 struct vrf_bit_set
*bit
;
424 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
427 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
431 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
433 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
434 struct hash
*vrf_hash
= bmap
;
435 struct vrf_bit_set
*bit
;
437 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
440 bit
= hash_lookup(vrf_hash
, &lookup
);
447 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
449 struct vrf
*vrf
= NULL
;
451 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
452 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
455 static const struct cmd_variable_handler vrf_var_handlers
[] = {
458 .completions
= vrf_autocomplete
,
460 {.completions
= NULL
},
463 /* Initialize VRF module. */
464 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
465 int (*disable
)(struct vrf
*), int (*delete)(struct vrf
*),
466 int ((*update
)(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;
480 vrf_master
.vrf_update_name_hook
= update
;
482 /* The default VRF always exists. */
483 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
485 flog_err(LIB_ERR_VRF_START
,
486 "vrf_init: failed to create the default VRF!");
490 /* Enable the default VRF. */
491 if (!vrf_enable(default_vrf
)) {
492 flog_err(LIB_ERR_VRF_START
,
493 "vrf_init: failed to enable the default VRF!");
497 cmd_variable_handler_register(vrf_var_handlers
);
500 /* Terminate VRF module. */
501 void vrf_terminate(void)
506 zlog_debug("%s: Shutting down vrf subsystem",
507 __PRETTY_FUNCTION__
);
509 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
510 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
512 /* Clear configured flag and invoke delete. */
513 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
517 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
518 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
520 /* Clear configured flag and invoke delete. */
521 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
526 static int vrf_default_accepts_vrf(int type
)
528 const char *fname
= NULL
;
529 char buf
[32] = {0x0};
534 * TCP & UDP services running in the default VRF context (ie., not bound
535 * to any VRF device) can work across all VRF domains by enabling the
536 * tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
537 * sysctl -w net.ipv4.tcp_l3mdev_accept=1
538 * sysctl -w net.ipv4.udp_l3mdev_accept=1
540 if (type
== SOCK_STREAM
)
541 fname
= "/proc/sys/net/ipv4/tcp_l3mdev_accept";
542 else if (type
== SOCK_DGRAM
)
543 fname
= "/proc/sys/net/ipv4/udp_l3mdev_accept";
546 fd
= fopen(fname
, "r");
555 /* Create a socket for the VRF. */
556 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
559 int ret
, save_errno
, ret2
;
561 ret
= vrf_switch_to_netns(vrf_id
);
563 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
564 __func__
, vrf_id
, safe_strerror(errno
));
566 if (ret
> 0 && interfacename
&& vrf_default_accepts_vrf(type
)) {
567 zlog_err("VRF socket not used since net.ipv4.%s_l3mdev_accept != 0",
568 (type
== SOCK_STREAM
? "tcp" : "udp"));
569 errno
= EEXIST
; /* not sure if this is the best error... */
573 ret
= socket(domain
, type
, protocol
);
575 ret2
= vrf_switchback_to_initial();
577 flog_err_sys(LIB_ERR_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)
601 void vrf_configure_backend(int vrf_backend_netns
)
603 vrf_backend
= vrf_backend_netns
;
606 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
611 if (strlen(vrfname
) > VRF_NAMSIZ
) {
614 "%% VRF name %s invalid: length exceeds %d bytes\n",
615 vrfname
, VRF_NAMSIZ
);
618 "%% VRF name %s invalid: length exceeds %d bytes\n",
619 vrfname
, VRF_NAMSIZ
);
620 return CMD_WARNING_CONFIG_FAILED
;
623 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
626 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
633 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
634 ns_id_t ns_id
, ns_id_t internal_ns_id
)
636 struct ns
*ns
= NULL
;
639 return CMD_WARNING_CONFIG_FAILED
;
640 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
643 "VRF %u is already configured with VRF %s\n",
644 vrf
->vrf_id
, vrf
->name
);
646 zlog_warn("VRF %u is already configured with VRF %s\n",
647 vrf
->vrf_id
, vrf
->name
);
648 return CMD_WARNING_CONFIG_FAILED
;
650 if (vrf
->ns_ctxt
!= NULL
) {
651 ns
= (struct ns
*)vrf
->ns_ctxt
;
652 if (!strcmp(ns
->name
, pathname
)) {
655 "VRF %u already configured with NETNS %s\n",
656 vrf
->vrf_id
, ns
->name
);
659 "VRF %u already configured with NETNS %s",
660 vrf
->vrf_id
, ns
->name
);
661 return CMD_WARNING_CONFIG_FAILED
;
664 ns
= ns_lookup_name(pathname
);
665 if (ns
&& ns
->vrf_ctxt
) {
666 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
672 "NS %s is already configured"
673 " with VRF %u(%s)\n",
674 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
676 zlog_warn("NS %s is already configured with VRF %u(%s)",
677 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
678 return CMD_WARNING_CONFIG_FAILED
;
680 ns
= ns_get_created(ns
, pathname
, ns_id
);
681 ns
->internal_ns_id
= internal_ns_id
;
682 ns
->vrf_ctxt
= (void *)vrf
;
683 vrf
->ns_ctxt
= (void *)ns
;
684 /* update VRF netns NAME */
685 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
687 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
689 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
690 ns
->ns_id
, ns
->name
);
692 zlog_warn("Can not associate NS %u with NETNS %s",
693 ns
->ns_id
, ns
->name
);
694 return CMD_WARNING_CONFIG_FAILED
;
700 int vrf_is_mapped_on_netns(struct vrf
*vrf
)
702 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
704 if (vrf
->vrf_id
== VRF_DEFAULT
)
709 /* vrf CLI commands */
713 "Exit current mode and down to previous mode\n")
715 /* We have to set vrf context to default vrf */
716 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
717 vty
->node
= CONFIG_NODE
;
724 "Select a VRF to configure\n"
728 const char *vrfname
= argv
[idx_name
]->arg
;
730 return vrf_handler_create(vty
, vrfname
, NULL
);
737 "Delete a pseudo VRF's configuration\n"
740 const char *vrfname
= argv
[2]->arg
;
744 vrfp
= vrf_lookup_by_name(vrfname
);
747 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
748 return CMD_WARNING_CONFIG_FAILED
;
751 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
752 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
753 return CMD_WARNING_CONFIG_FAILED
;
756 /* Clear configured flag and invoke delete. */
757 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
764 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
766 DEFUN_NOSH (vrf_netns
,
769 "Attach VRF to a Namespace\n"
770 "The file name in " NS_RUN_DIR
", or a full pathname\n")
772 int idx_name
= 1, ret
;
773 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
775 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
778 return CMD_WARNING_CONFIG_FAILED
;
780 frr_elevate_privs(vrf_daemon_privs
) {
781 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
782 NS_UNKNOWN
, NS_UNKNOWN
);
787 DEFUN_NOSH (no_vrf_netns
,
791 "Detach VRF from a Namespace\n"
792 "The file name in " NS_RUN_DIR
", or a full pathname\n")
794 struct ns
*ns
= NULL
;
796 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
798 if (!vrf_is_backend_netns()) {
799 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
800 return CMD_WARNING_CONFIG_FAILED
;
803 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
804 vrf
->name
, vrf
->vrf_id
);
805 return CMD_WARNING_CONFIG_FAILED
;
808 ns
= (struct ns
*)vrf
->ns_ctxt
;
812 /* vrf ID from VRF is necessary for Zebra
813 * so that propagate to other clients is done
821 * Debug CLI for vrf's
846 static int vrf_write_host(struct vty
*vty
)
849 vty_out(vty
, "debug vrf\n");
854 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
856 void vrf_install_commands(void)
858 install_node(&vrf_debug_node
, vrf_write_host
);
860 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
861 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
862 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
863 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
866 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
867 struct zebra_privs_t
*daemon_privs
)
869 install_element(CONFIG_NODE
, &vrf_cmd
);
870 install_element(CONFIG_NODE
, &no_vrf_cmd
);
871 install_node(&vrf_node
, writefunc
);
872 install_default(VRF_NODE
);
873 install_element(VRF_NODE
, &vrf_exit_cmd
);
874 if (vrf_is_backend_netns() && ns_have_netns()) {
875 /* Install NS commands. */
876 vrf_daemon_privs
= daemon_privs
;
877 install_element(VRF_NODE
, &vrf_netns_cmd
);
878 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
882 void vrf_set_default_name(const char *default_name
)
886 def_vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
887 assert(default_name
);
888 vrf_with_default_name
= vrf_lookup_by_name(default_name
);
889 if (vrf_with_default_name
&& vrf_with_default_name
!= def_vrf
) {
890 /* vrf name already used by an other VRF */
891 zlog_debug("VRF: %s, avoid changing name to %s, same name exists (%u)",
892 vrf_with_default_name
->name
, default_name
,
893 vrf_with_default_name
->vrf_id
);
896 snprintf(vrf_default_name
, VRF_NAMSIZ
, "%s", default_name
);
898 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, def_vrf
);
899 strlcpy(def_vrf
->data
.l
.netns_name
,
900 vrf_default_name
, NS_NAMSIZ
);
901 strlcpy(def_vrf
->name
, vrf_default_name
, sizeof(def_vrf
->name
));
902 RB_INSERT(vrf_name_head
, &vrfs_by_name
, def_vrf
);
903 if (vrf_master
.vrf_update_name_hook
)
904 (*vrf_master
.vrf_update_name_hook
)(def_vrf
);
908 const char *vrf_get_default_name(void)
910 return vrf_default_name
;
913 vrf_id_t
vrf_get_default_id(void)
915 /* backend netns is only known by zebra
916 * for other daemons, we return VRF_DEFAULT_INTERNAL
918 if (vrf_is_backend_netns())
919 return ns_get_default_id();
921 return VRF_DEFAULT_INTERNAL
;
924 int vrf_bind(vrf_id_t vrf_id
, int fd
, char *name
)
928 if (fd
< 0 || name
== NULL
)
930 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id
)))
932 #ifdef SO_BINDTODEVICE
933 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
935 zlog_debug("bind to interface %s failed, errno=%d", name
,
937 #endif /* SO_BINDTODEVICE */
940 int vrf_getaddrinfo(const char *node
, const char *service
,
941 const struct addrinfo
*hints
, struct addrinfo
**res
,
944 int ret
, ret2
, save_errno
;
946 ret
= vrf_switch_to_netns(vrf_id
);
948 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
949 __func__
, vrf_id
, safe_strerror(errno
));
950 ret
= getaddrinfo(node
, service
, hints
, res
);
952 ret2
= vrf_switchback_to_initial();
954 flog_err_sys(LIB_ERR_SOCKET
,
955 "%s: Can't switchback from VRF %u (%s)", __func__
,
956 vrf_id
, safe_strerror(errno
));
961 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
963 int ret
, saved_errno
, rc
;
965 ret
= vrf_switch_to_netns(vrf_id
);
967 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
968 __func__
, vrf_id
, safe_strerror(errno
));
971 rc
= ioctl(d
, request
, params
);
973 ret
= vrf_switchback_to_initial();
975 flog_err_sys(LIB_ERR_SOCKET
,
976 "%s: Can't switchback from VRF %u (%s)", __func__
,
977 vrf_id
, safe_strerror(errno
));
982 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
985 int ret
, save_errno
, ret2
;
987 ret
= vrf_switch_to_netns(vrf_id
);
989 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
990 __func__
, vrf_id
, safe_strerror(errno
));
991 ret
= sockunion_socket(su
);
993 ret2
= vrf_switchback_to_initial();
995 flog_err_sys(LIB_ERR_SOCKET
,
996 "%s: Can't switchback from VRF %u (%s)", __func__
,
997 vrf_id
, safe_strerror(errno
));
1002 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);