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
43 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF")
44 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map")
48 static __inline
int vrf_id_compare(const struct vrf
*, const struct vrf
*);
49 static __inline
int vrf_name_compare(const struct vrf
*, const struct vrf
*);
51 RB_GENERATE(vrf_id_head
, vrf
, id_entry
, vrf_id_compare
);
52 RB_GENERATE(vrf_name_head
, vrf
, name_entry
, vrf_name_compare
);
54 struct vrf_id_head vrfs_by_id
= RB_INITIALIZER(&vrfs_by_id
);
55 struct vrf_name_head vrfs_by_name
= RB_INITIALIZER(&vrfs_by_name
);
57 static int vrf_backend
;
58 static struct zebra_privs_t
*vrf_daemon_privs
;
61 * Turn on/off debug code
66 /* Holding VRF hooks */
68 int (*vrf_new_hook
)(struct vrf
*);
69 int (*vrf_delete_hook
)(struct vrf
*);
70 int (*vrf_enable_hook
)(struct vrf
*);
71 int (*vrf_disable_hook
)(struct vrf
*);
76 static int vrf_is_enabled(struct vrf
*vrf
);
78 /* VRF list existance check by name. */
79 struct vrf
*vrf_lookup_by_name(const char *name
)
82 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
83 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
86 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
88 return (a
->vrf_id
- b
->vrf_id
);
91 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
93 return strcmp(a
->name
, b
->name
);
96 /* if ns_id is different and not VRF_UNKNOWN,
97 * then update vrf identifier, and enable VRF
99 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
101 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
103 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
107 old_vrf_id
= vrf
->vrf_id
;
108 if (vrf_id
== vrf
->vrf_id
)
110 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
111 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
112 vrf
->vrf_id
= vrf_id
;
113 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
114 if (old_vrf_id
== VRF_UNKNOWN
)
115 vrf_enable((struct vrf
*)vrf
);
118 int vrf_switch_to_netns(vrf_id_t vrf_id
)
121 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
123 /* VRF is default VRF. silently ignore */
124 if (!vrf
|| vrf
->vrf_id
== VRF_DEFAULT
)
125 return 1; /* 1 = default */
126 /* VRF has no NETNS backend. silently ignore */
127 if (vrf
->data
.l
.netns_name
[0] == '\0')
128 return 2; /* 2 = no netns */
129 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
131 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
132 return ns_switch_to_netns(name
);
135 int vrf_switchback_to_initial(void)
137 int ret
= ns_switchback_to_initial();
139 if (ret
== 0 && debug_vrf
)
140 zlog_debug("VRF_SWITCHBACK");
144 /* Get a VRF. If not found, create one.
146 * name - The name of the vrf. May be NULL if unknown.
147 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
148 * Description: Please note that this routine can be called with just the name
151 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
153 struct vrf
*vrf
= NULL
;
157 zlog_debug("VRF_GET: %s(%u)", name
== NULL
? "(NULL)" : name
,
160 /* Nothing to see, move along here */
161 if (!name
&& vrf_id
== VRF_UNKNOWN
)
164 /* attempt to find already available VRF
167 vrf
= vrf_lookup_by_name(name
);
168 /* Try to find VRF both by ID and name */
169 if (!vrf
&& vrf_id
!= VRF_UNKNOWN
)
170 vrf
= vrf_lookup_by_id(vrf_id
);
173 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
174 vrf
->vrf_id
= VRF_UNKNOWN
;
179 zlog_debug("VRF(%u) %s is created.", vrf_id
,
180 (name
) ? name
: "(NULL)");
184 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
185 vrf
->vrf_id
= vrf_id
;
186 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
190 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
191 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
192 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
193 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
194 } else if (name
&& vrf
->name
[0] == '\0') {
195 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
196 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
198 if (new &&vrf_master
.vrf_new_hook
)
199 (*vrf_master
.vrf_new_hook
)(vrf
);
204 /* Delete a VRF. This is called when the underlying VRF goes away, a
205 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
207 void vrf_delete(struct vrf
*vrf
)
210 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
212 if (vrf_is_enabled(vrf
))
215 /* If the VRF is user configured, it'll stick around, just remove
216 * the ID mapping. Interfaces assigned to this VRF should've been
217 * removed already as part of the VRF going down.
219 if (vrf_is_user_cfged(vrf
)) {
220 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
221 /* Delete any VRF interfaces - should be only
222 * the VRF itself, other interfaces should've
223 * been moved out of the VRF.
226 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
227 vrf
->vrf_id
= VRF_UNKNOWN
;
232 if (vrf_master
.vrf_delete_hook
)
233 (*vrf_master
.vrf_delete_hook
)(vrf
);
238 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
239 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
240 if (vrf
->name
[0] != '\0')
241 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
243 XFREE(MTYPE_VRF
, vrf
);
246 /* Look up a VRF by identifier. */
247 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
251 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
255 * Enable a VRF - that is, let the VRF be ready to use.
256 * The VRF_ENABLE_HOOK callback will be called to inform
257 * that they can allocate resources in this VRF.
259 * RETURN: 1 - enabled successfully; otherwise, 0.
261 int vrf_enable(struct vrf
*vrf
)
263 if (vrf_is_enabled(vrf
))
267 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
269 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
271 if (vrf_master
.vrf_enable_hook
)
272 (*vrf_master
.vrf_enable_hook
)(vrf
);
275 * If we have any nexthop group entries that
276 * are awaiting vrf initialization then
277 * let's let people know about it
279 nexthop_group_enable_vrf(vrf
);
285 * Disable a VRF - that is, let the VRF be unusable.
286 * The VRF_DELETE_HOOK callback will be called to inform
287 * that they must release the resources in the VRF.
289 void vrf_disable(struct vrf
*vrf
)
291 if (!vrf_is_enabled(vrf
))
294 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
297 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
299 /* Till now, nothing to be done for the default VRF. */
300 // Pending: see why this statement.
302 if (vrf_master
.vrf_disable_hook
)
303 (*vrf_master
.vrf_disable_hook
)(vrf
);
306 const char *vrf_id_to_name(vrf_id_t vrf_id
)
310 vrf
= vrf_lookup_by_id(vrf_id
);
317 vrf_id_t
vrf_name_to_id(const char *name
)
320 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
321 // id/ routine not used.
323 vrf
= vrf_lookup_by_name(name
);
325 vrf_id
= vrf
->vrf_id
;
330 /* Get the data pointer of the specified VRF. If not found, create one. */
331 void *vrf_info_get(vrf_id_t vrf_id
)
333 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
337 /* Look up the data pointer of the specified VRF. */
338 void *vrf_info_lookup(vrf_id_t vrf_id
)
340 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
341 return vrf
? vrf
->info
: NULL
;
345 * VRF hash for storing set or not.
352 static unsigned int vrf_hash_bitmap_key(void *data
)
354 struct vrf_bit_set
*bit
= data
;
359 static int vrf_hash_bitmap_cmp(const void *a
, const void *b
)
361 const struct vrf_bit_set
*bit1
= a
;
362 const struct vrf_bit_set
*bit2
= b
;
364 return bit1
->vrf_id
== bit2
->vrf_id
;
367 static void *vrf_hash_bitmap_alloc(void *data
)
369 struct vrf_bit_set
*copy
= data
;
370 struct vrf_bit_set
*bit
;
372 bit
= XMALLOC(MTYPE_VRF_BITMAP
, sizeof(*bit
));
373 bit
->vrf_id
= copy
->vrf_id
;
378 static void vrf_hash_bitmap_free(void *data
)
380 struct vrf_bit_set
*bit
= data
;
382 XFREE(MTYPE_VRF_BITMAP
, bit
);
385 vrf_bitmap_t
vrf_bitmap_init(void)
387 return hash_create_size(32, vrf_hash_bitmap_key
, vrf_hash_bitmap_cmp
,
391 void vrf_bitmap_free(vrf_bitmap_t bmap
)
393 struct hash
*vrf_hash
= bmap
;
395 if (vrf_hash
== NULL
)
398 hash_clean(vrf_hash
, vrf_hash_bitmap_free
);
402 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
404 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
405 struct hash
*vrf_hash
= bmap
;
406 struct vrf_bit_set
*bit
;
408 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
411 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
415 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
417 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
418 struct hash
*vrf_hash
= bmap
;
419 struct vrf_bit_set
*bit
;
421 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
424 bit
= hash_get(vrf_hash
, &lookup
, vrf_hash_bitmap_alloc
);
428 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
430 struct vrf_bit_set lookup
= { .vrf_id
= vrf_id
};
431 struct hash
*vrf_hash
= bmap
;
432 struct vrf_bit_set
*bit
;
434 if (vrf_hash
== NULL
|| vrf_id
== VRF_UNKNOWN
)
437 bit
= hash_lookup(vrf_hash
, &lookup
);
444 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
446 struct vrf
*vrf
= NULL
;
448 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
449 if (vrf
->vrf_id
!= VRF_DEFAULT
)
450 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
454 static const struct cmd_variable_handler vrf_var_handlers
[] = {
457 .completions
= vrf_autocomplete
,
459 {.completions
= NULL
},
462 /* Initialize VRF module. */
463 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
464 int (*disable
)(struct vrf
*), int (*delete)(struct vrf
*))
466 struct vrf
*default_vrf
;
468 /* initialise NS, in case VRF backend if NETNS */
471 zlog_debug("%s: Initializing VRF subsystem",
472 __PRETTY_FUNCTION__
);
474 vrf_master
.vrf_new_hook
= create
;
475 vrf_master
.vrf_enable_hook
= enable
;
476 vrf_master
.vrf_disable_hook
= disable
;
477 vrf_master
.vrf_delete_hook
= delete;
479 /* The default VRF always exists. */
480 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
482 flog_err(LIB_ERR_VRF_START
,
483 "vrf_init: failed to create the default VRF!");
487 /* Enable the default VRF. */
488 if (!vrf_enable(default_vrf
)) {
489 flog_err(LIB_ERR_VRF_START
,
490 "vrf_init: failed to enable the default VRF!");
494 cmd_variable_handler_register(vrf_var_handlers
);
497 /* Terminate VRF module. */
498 void vrf_terminate(void)
503 zlog_debug("%s: Shutting down vrf subsystem",
504 __PRETTY_FUNCTION__
);
506 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
507 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
509 /* Clear configured flag and invoke delete. */
510 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
514 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
515 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
517 /* Clear configured flag and invoke delete. */
518 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
523 static int vrf_default_accepts_vrf(int type
)
525 const char *fname
= NULL
;
526 char buf
[32] = {0x0};
531 * TCP & UDP services running in the default VRF context (ie., not bound
532 * to any VRF device) can work across all VRF domains by enabling the
533 * tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
534 * sysctl -w net.ipv4.tcp_l3mdev_accept=1
535 * sysctl -w net.ipv4.udp_l3mdev_accept=1
537 if (type
== SOCK_STREAM
)
538 fname
= "/proc/sys/net/ipv4/tcp_l3mdev_accept";
539 else if (type
== SOCK_DGRAM
)
540 fname
= "/proc/sys/net/ipv4/udp_l3mdev_accept";
543 fd
= fopen(fname
, "r");
552 /* Create a socket for the VRF. */
553 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
556 int ret
, save_errno
, ret2
;
558 ret
= vrf_switch_to_netns(vrf_id
);
560 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
561 __func__
, vrf_id
, safe_strerror(errno
));
563 if (ret
> 0 && interfacename
&& vrf_default_accepts_vrf(type
)) {
564 zlog_err("VRF socket not used since net.ipv4.%s_l3mdev_accept != 0",
565 (type
== SOCK_STREAM
? "tcp" : "udp"));
566 errno
= EEXIST
; /* not sure if this is the best error... */
570 ret
= socket(domain
, type
, protocol
);
572 ret2
= vrf_switchback_to_initial();
574 flog_err_sys(LIB_ERR_SOCKET
,
575 "%s: Can't switchback from VRF %u (%s)", __func__
,
576 vrf_id
, safe_strerror(errno
));
580 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
588 int vrf_is_backend_netns(void)
590 return (vrf_backend
== VRF_BACKEND_NETNS
);
593 int vrf_get_backend(void)
598 void vrf_configure_backend(int vrf_backend_netns
)
600 vrf_backend
= vrf_backend_netns
;
603 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
608 if (strlen(vrfname
) > VRF_NAMSIZ
) {
611 "%% VRF name %s invalid: length exceeds %d bytes\n",
612 vrfname
, VRF_NAMSIZ
);
615 "%% VRF name %s invalid: length exceeds %d bytes\n",
616 vrfname
, VRF_NAMSIZ
);
617 return CMD_WARNING_CONFIG_FAILED
;
620 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
623 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
630 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
631 ns_id_t ns_id
, ns_id_t internal_ns_id
)
633 struct ns
*ns
= NULL
;
636 return CMD_WARNING_CONFIG_FAILED
;
637 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
640 "VRF %u is already configured with VRF %s\n",
641 vrf
->vrf_id
, vrf
->name
);
643 zlog_warn("VRF %u is already configured with VRF %s\n",
644 vrf
->vrf_id
, vrf
->name
);
645 return CMD_WARNING_CONFIG_FAILED
;
647 if (vrf
->ns_ctxt
!= NULL
) {
648 ns
= (struct ns
*)vrf
->ns_ctxt
;
649 if (!strcmp(ns
->name
, pathname
)) {
652 "VRF %u already configured with NETNS %s\n",
653 vrf
->vrf_id
, ns
->name
);
656 "VRF %u already configured with NETNS %s",
657 vrf
->vrf_id
, ns
->name
);
658 return CMD_WARNING_CONFIG_FAILED
;
661 ns
= ns_lookup_name(pathname
);
662 if (ns
&& ns
->vrf_ctxt
) {
663 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
669 "NS %s is already configured"
670 " with VRF %u(%s)\n",
671 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
673 zlog_warn("NS %s is already configured with VRF %u(%s)",
674 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
675 return CMD_WARNING_CONFIG_FAILED
;
677 ns
= ns_get_created(ns
, pathname
, ns_id
);
678 ns
->internal_ns_id
= internal_ns_id
;
679 ns
->vrf_ctxt
= (void *)vrf
;
680 vrf
->ns_ctxt
= (void *)ns
;
681 /* update VRF netns NAME */
682 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
684 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
686 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
687 ns
->ns_id
, ns
->name
);
689 zlog_warn("Can not associate NS %u with NETNS %s",
690 ns
->ns_id
, ns
->name
);
691 return CMD_WARNING_CONFIG_FAILED
;
697 int vrf_is_mapped_on_netns(struct vrf
*vrf
)
699 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
701 if (vrf
->vrf_id
== VRF_DEFAULT
)
706 /* vrf CLI commands */
710 "Exit current mode and down to previous mode\n")
712 /* We have to set vrf context to default vrf */
713 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
714 vty
->node
= CONFIG_NODE
;
721 "Select a VRF to configure\n"
725 const char *vrfname
= argv
[idx_name
]->arg
;
727 return vrf_handler_create(vty
, vrfname
, NULL
);
734 "Delete a pseudo VRF's configuration\n"
737 const char *vrfname
= argv
[2]->arg
;
741 vrfp
= vrf_lookup_by_name(vrfname
);
744 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
745 return CMD_WARNING_CONFIG_FAILED
;
748 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
749 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
750 return CMD_WARNING_CONFIG_FAILED
;
753 /* Clear configured flag and invoke delete. */
754 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
761 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
763 DEFUN_NOSH (vrf_netns
,
766 "Attach VRF to a Namespace\n"
767 "The file name in " NS_RUN_DIR
", or a full pathname\n")
769 int idx_name
= 1, ret
;
770 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
772 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
775 return CMD_WARNING_CONFIG_FAILED
;
777 frr_elevate_privs(vrf_daemon_privs
) {
778 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
779 NS_UNKNOWN
, NS_UNKNOWN
);
784 DEFUN_NOSH (no_vrf_netns
,
788 "Detach VRF from a Namespace\n"
789 "The file name in " NS_RUN_DIR
", or a full pathname\n")
791 struct ns
*ns
= NULL
;
793 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
795 if (!vrf_is_backend_netns()) {
796 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
797 return CMD_WARNING_CONFIG_FAILED
;
800 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
801 vrf
->name
, vrf
->vrf_id
);
802 return CMD_WARNING_CONFIG_FAILED
;
805 ns
= (struct ns
*)vrf
->ns_ctxt
;
809 /* vrf ID from VRF is necessary for Zebra
810 * so that propagate to other clients is done
818 * Debug CLI for vrf's
843 static int vrf_write_host(struct vty
*vty
)
846 vty_out(vty
, "debug vrf\n");
851 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
853 void vrf_install_commands(void)
855 install_node(&vrf_debug_node
, vrf_write_host
);
857 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
858 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
859 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
860 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
863 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
864 struct zebra_privs_t
*daemon_privs
)
866 install_element(CONFIG_NODE
, &vrf_cmd
);
867 install_element(CONFIG_NODE
, &no_vrf_cmd
);
868 install_node(&vrf_node
, writefunc
);
869 install_default(VRF_NODE
);
870 install_element(VRF_NODE
, &vrf_exit_cmd
);
871 if (vrf_is_backend_netns() && ns_have_netns()) {
872 /* Install NS commands. */
873 vrf_daemon_privs
= daemon_privs
;
874 install_element(VRF_NODE
, &vrf_netns_cmd
);
875 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
879 vrf_id_t
vrf_get_default_id(void)
881 struct vrf
*vrf
= vrf_lookup_by_name(VRF_DEFAULT_NAME
);
885 /* backend netns is only known by zebra
886 * for other daemons, we return VRF_DEFAULT_INTERNAL
888 if (vrf_is_backend_netns())
889 return ns_get_default_id();
891 return VRF_DEFAULT_INTERNAL
;
894 int vrf_bind(vrf_id_t vrf_id
, int fd
, char *name
)
898 if (fd
< 0 || name
== NULL
)
900 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id
)))
902 #ifdef SO_BINDTODEVICE
903 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
905 zlog_debug("bind to interface %s failed, errno=%d", name
,
907 #endif /* SO_BINDTODEVICE */
910 int vrf_getaddrinfo(const char *node
, const char *service
,
911 const struct addrinfo
*hints
, struct addrinfo
**res
,
914 int ret
, ret2
, save_errno
;
916 ret
= vrf_switch_to_netns(vrf_id
);
918 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
919 __func__
, vrf_id
, safe_strerror(errno
));
920 ret
= getaddrinfo(node
, service
, hints
, res
);
922 ret2
= vrf_switchback_to_initial();
924 flog_err_sys(LIB_ERR_SOCKET
,
925 "%s: Can't switchback from VRF %u (%s)", __func__
,
926 vrf_id
, safe_strerror(errno
));
931 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
933 int ret
, saved_errno
, rc
;
935 ret
= vrf_switch_to_netns(vrf_id
);
937 flog_err_sys(LIB_ERR_SOCKET
, "%s: Can't switch to VRF %u (%s)",
938 __func__
, vrf_id
, safe_strerror(errno
));
941 rc
= ioctl(d
, request
, params
);
943 ret
= vrf_switchback_to_initial();
945 flog_err_sys(LIB_ERR_SOCKET
,
946 "%s: Can't switchback from VRF %u (%s)", __func__
,
947 vrf_id
, safe_strerror(errno
));
952 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
955 int ret
, save_errno
, ret2
;
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
= sockunion_socket(su
);
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 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);