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
38 /* default VRF ID value used when VRF backend is not NETNS */
39 #define VRF_DEFAULT_INTERNAL 0
41 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF")
42 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map")
46 static __inline
int vrf_id_compare(const struct vrf
*, const struct vrf
*);
47 static __inline
int vrf_name_compare(const struct vrf
*, const struct vrf
*);
49 RB_GENERATE(vrf_id_head
, vrf
, id_entry
, vrf_id_compare
);
50 RB_GENERATE(vrf_name_head
, vrf
, name_entry
, vrf_name_compare
);
52 struct vrf_id_head vrfs_by_id
= RB_INITIALIZER(&vrfs_by_id
);
53 struct vrf_name_head vrfs_by_name
= RB_INITIALIZER(&vrfs_by_name
);
55 static int vrf_backend
;
56 static struct zebra_privs_t
*vrf_daemon_privs
;
59 * Turn on/off debug code
64 /* Holding VRF hooks */
66 int (*vrf_new_hook
)(struct vrf
*);
67 int (*vrf_delete_hook
)(struct vrf
*);
68 int (*vrf_enable_hook
)(struct vrf
*);
69 int (*vrf_disable_hook
)(struct vrf
*);
74 static int vrf_is_enabled(struct vrf
*vrf
);
76 /* VRF list existance check by name. */
77 struct vrf
*vrf_lookup_by_name(const char *name
)
80 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
81 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
84 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
86 return (a
->vrf_id
- b
->vrf_id
);
89 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
91 return strcmp(a
->name
, b
->name
);
94 /* if ns_id is different and not VRF_UNKNOWN,
95 * then update vrf identifier, and enable VRF
97 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
99 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
101 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
105 old_vrf_id
= vrf
->vrf_id
;
106 if (vrf_id
== vrf
->vrf_id
)
108 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
109 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
110 vrf
->vrf_id
= vrf_id
;
111 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
112 if (old_vrf_id
== VRF_UNKNOWN
)
113 vrf_enable((struct vrf
*)vrf
);
116 int vrf_switch_to_netns(vrf_id_t vrf_id
)
119 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
121 /* VRF is default VRF. silently ignore */
122 if (!vrf
|| vrf
->vrf_id
== VRF_DEFAULT
)
124 /* VRF has no NETNS backend. silently ignore */
125 if (vrf
->data
.l
.netns_name
[0] == '\0')
127 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
129 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
130 return ns_switch_to_netns(name
);
133 int vrf_switchback_to_initial(void)
135 int ret
= ns_switchback_to_initial();
137 if (ret
== 0 && debug_vrf
)
138 zlog_debug("VRF_SWITCHBACK");
142 /* Get a VRF. If not found, create one.
144 * name - The name of the vrf. May be NULL if unknown.
145 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
146 * Description: Please note that this routine can be called with just the name
149 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
151 struct vrf
*vrf
= NULL
;
155 zlog_debug("VRF_GET: %s(%u)", name
== NULL
? "(NULL)" : name
,
158 /* Nothing to see, move along here */
159 if (!name
&& vrf_id
== VRF_UNKNOWN
)
162 /* Try to find VRF both by ID and name */
163 if (vrf_id
!= VRF_UNKNOWN
)
164 vrf
= vrf_lookup_by_id(vrf_id
);
166 vrf
= vrf_lookup_by_name(name
);
169 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
170 vrf
->vrf_id
= VRF_UNKNOWN
;
175 zlog_debug("VRF(%u) %s is created.", vrf_id
,
176 (name
) ? name
: "(NULL)");
180 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
181 vrf
->vrf_id
= vrf_id
;
182 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
186 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
187 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
188 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
189 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
190 } else if (name
&& vrf
->name
[0] == '\0') {
191 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
192 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
194 if (new &&vrf_master
.vrf_new_hook
)
195 (*vrf_master
.vrf_new_hook
)(vrf
);
200 /* Delete a VRF. This is called when the underlying VRF goes away, a
201 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
203 void vrf_delete(struct vrf
*vrf
)
206 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
208 if (vrf_is_enabled(vrf
))
211 /* If the VRF is user configured, it'll stick around, just remove
212 * the ID mapping. Interfaces assigned to this VRF should've been
213 * removed already as part of the VRF going down.
215 if (vrf_is_user_cfged(vrf
)) {
216 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
217 /* Delete any VRF interfaces - should be only
218 * the VRF itself, other interfaces should've
219 * been moved out of the VRF.
222 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
223 vrf
->vrf_id
= VRF_UNKNOWN
;
228 if (vrf_master
.vrf_delete_hook
)
229 (*vrf_master
.vrf_delete_hook
)(vrf
);
234 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
235 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
236 if (vrf
->name
[0] != '\0')
237 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
239 XFREE(MTYPE_VRF
, vrf
);
242 /* Look up a VRF by identifier. */
243 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
247 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
251 * Enable a VRF - that is, let the VRF be ready to use.
252 * The VRF_ENABLE_HOOK callback will be called to inform
253 * that they can allocate resources in this VRF.
255 * RETURN: 1 - enabled successfully; otherwise, 0.
257 int vrf_enable(struct vrf
*vrf
)
259 if (vrf_is_enabled(vrf
))
263 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
265 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
267 if (vrf_master
.vrf_enable_hook
)
268 (*vrf_master
.vrf_enable_hook
)(vrf
);
274 * Disable a VRF - that is, let the VRF be unusable.
275 * The VRF_DELETE_HOOK callback will be called to inform
276 * that they must release the resources in the VRF.
278 void vrf_disable(struct vrf
*vrf
)
280 if (!vrf_is_enabled(vrf
))
283 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
286 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
288 /* Till now, nothing to be done for the default VRF. */
289 // Pending: see why this statement.
291 if (vrf_master
.vrf_disable_hook
)
292 (*vrf_master
.vrf_disable_hook
)(vrf
);
295 const char *vrf_id_to_name(vrf_id_t vrf_id
)
299 vrf
= vrf_lookup_by_id(vrf_id
);
306 vrf_id_t
vrf_name_to_id(const char *name
)
309 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
310 // id/ routine not used.
312 vrf
= vrf_lookup_by_name(name
);
314 vrf_id
= vrf
->vrf_id
;
319 /* Get the data pointer of the specified VRF. If not found, create one. */
320 void *vrf_info_get(vrf_id_t vrf_id
)
322 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
326 /* Look up the data pointer of the specified VRF. */
327 void *vrf_info_lookup(vrf_id_t vrf_id
)
329 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
330 return vrf
? vrf
->info
: NULL
;
337 #define VRF_BITMAP_NUM_OF_GROUPS 1024
338 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP (UINT32_MAX / VRF_BITMAP_NUM_OF_GROUPS)
339 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
340 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
342 #define VRF_BITMAP_GROUP(_id) ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
343 #define VRF_BITMAP_BIT_OFFSET(_id) ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
345 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) ((_bit_offset) / CHAR_BIT)
346 #define VRF_BITMAP_FLAG(_bit_offset) \
347 (((uint8_t)1) << ((_bit_offset) % CHAR_BIT))
350 uint8_t *groups
[VRF_BITMAP_NUM_OF_GROUPS
];
353 vrf_bitmap_t
vrf_bitmap_init(void)
355 return (vrf_bitmap_t
)XCALLOC(MTYPE_VRF_BITMAP
,
356 sizeof(struct vrf_bitmap
));
359 void vrf_bitmap_free(vrf_bitmap_t bmap
)
361 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
364 if (bmap
== VRF_BITMAP_NULL
)
367 for (i
= 0; i
< VRF_BITMAP_NUM_OF_GROUPS
; i
++)
369 XFREE(MTYPE_VRF_BITMAP
, bm
->groups
[i
]);
371 XFREE(MTYPE_VRF_BITMAP
, bm
);
374 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
376 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
377 uint8_t group
= VRF_BITMAP_GROUP(vrf_id
);
378 uint8_t offset
= VRF_BITMAP_BIT_OFFSET(vrf_id
);
380 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
)
383 if (bm
->groups
[group
] == NULL
)
384 bm
->groups
[group
] = XCALLOC(MTYPE_VRF_BITMAP
,
385 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP
);
387 SET_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
388 VRF_BITMAP_FLAG(offset
));
391 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
393 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
394 uint8_t group
= VRF_BITMAP_GROUP(vrf_id
);
395 uint8_t offset
= VRF_BITMAP_BIT_OFFSET(vrf_id
);
397 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
398 || bm
->groups
[group
] == NULL
)
401 UNSET_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
402 VRF_BITMAP_FLAG(offset
));
405 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
407 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
408 uint8_t group
= VRF_BITMAP_GROUP(vrf_id
);
409 uint8_t offset
= VRF_BITMAP_BIT_OFFSET(vrf_id
);
411 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
412 || bm
->groups
[group
] == NULL
)
415 return CHECK_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
416 VRF_BITMAP_FLAG(offset
))
421 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
423 struct vrf
*vrf
= NULL
;
425 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
426 if (vrf
->vrf_id
!= VRF_DEFAULT
)
427 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
431 static const struct cmd_variable_handler vrf_var_handlers
[] = {
434 .completions
= vrf_autocomplete
,
436 {.completions
= NULL
},
439 /* Initialize VRF module. */
440 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
441 int (*disable
)(struct vrf
*), int (*delete)(struct vrf
*))
443 struct vrf
*default_vrf
;
445 /* initialise NS, in case VRF backend if NETNS */
448 zlog_debug("%s: Initializing VRF subsystem",
449 __PRETTY_FUNCTION__
);
451 vrf_master
.vrf_new_hook
= create
;
452 vrf_master
.vrf_enable_hook
= enable
;
453 vrf_master
.vrf_disable_hook
= disable
;
454 vrf_master
.vrf_delete_hook
= delete;
456 /* The default VRF always exists. */
457 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
459 zlog_err("vrf_init: failed to create the default VRF!");
463 /* Enable the default VRF. */
464 if (!vrf_enable(default_vrf
)) {
465 zlog_err("vrf_init: failed to enable the default VRF!");
469 cmd_variable_handler_register(vrf_var_handlers
);
472 /* Terminate VRF module. */
473 void vrf_terminate(void)
478 zlog_debug("%s: Shutting down vrf subsystem",
479 __PRETTY_FUNCTION__
);
481 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
482 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
484 /* Clear configured flag and invoke delete. */
485 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
489 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
490 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
492 /* Clear configured flag and invoke delete. */
493 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
498 /* Create a socket for the VRF. */
499 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
502 int ret
, save_errno
, ret2
;
504 ret
= vrf_switch_to_netns(vrf_id
);
506 zlog_err("%s: Can't switch to VRF %u (%s)", __func__
, vrf_id
,
507 safe_strerror(errno
));
508 ret
= socket(domain
, type
, protocol
);
510 ret2
= vrf_switchback_to_initial();
512 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
513 vrf_id
, safe_strerror(errno
));
517 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
525 int vrf_is_backend_netns(void)
527 return (vrf_backend
== VRF_BACKEND_NETNS
);
530 int vrf_get_backend(void)
535 void vrf_configure_backend(int vrf_backend_netns
)
537 vrf_backend
= vrf_backend_netns
;
540 int vrf_handler_create(struct vty
*vty
, const char *vrfname
, struct vrf
**vrf
)
544 if (strlen(vrfname
) > VRF_NAMSIZ
) {
547 "%% VRF name %s invalid: length exceeds %d bytes\n",
548 vrfname
, VRF_NAMSIZ
);
551 "%% VRF name %s invalid: length exceeds %d bytes\n",
552 vrfname
, VRF_NAMSIZ
);
553 return CMD_WARNING_CONFIG_FAILED
;
556 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
559 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
566 int vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
, char *pathname
,
569 struct ns
*ns
= NULL
;
572 return CMD_WARNING_CONFIG_FAILED
;
573 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
576 "VRF %u is already configured with VRF %s\n",
577 vrf
->vrf_id
, vrf
->name
);
579 zlog_warn("VRF %u is already configured with VRF %s\n",
580 vrf
->vrf_id
, vrf
->name
);
581 return CMD_WARNING_CONFIG_FAILED
;
583 if (vrf
->ns_ctxt
!= NULL
) {
584 ns
= (struct ns
*)vrf
->ns_ctxt
;
585 if (ns
&& 0 != strcmp(ns
->name
, pathname
)) {
588 "VRF %u already configured with NETNS %s\n",
589 vrf
->vrf_id
, ns
->name
);
592 "VRF %u already configured with NETNS %s",
593 vrf
->vrf_id
, ns
->name
);
594 return CMD_WARNING_CONFIG_FAILED
;
597 ns
= ns_lookup_name(pathname
);
598 if (ns
&& ns
->vrf_ctxt
) {
599 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
605 "NS %s is already configured"
606 " with VRF %u(%s)\n",
607 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
609 zlog_warn("NS %s is already configured with VRF %u(%s)",
610 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
611 return CMD_WARNING_CONFIG_FAILED
;
613 ns
= ns_get_created(ns
, pathname
, ns_id
);
614 ns
->vrf_ctxt
= (void *)vrf
;
615 vrf
->ns_ctxt
= (void *)ns
;
616 /* update VRF netns NAME */
618 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
620 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
622 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
623 ns
->ns_id
, ns
->name
);
625 zlog_warn("Can not associate NS %u with NETNS %s",
626 ns
->ns_id
, ns
->name
);
627 return CMD_WARNING_CONFIG_FAILED
;
633 int vrf_is_mapped_on_netns(vrf_id_t vrf_id
)
635 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
637 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
639 if (vrf
->vrf_id
== VRF_DEFAULT
)
644 /* vrf CLI commands */
648 "Exit current mode and down to previous mode\n")
650 /* We have to set vrf context to default vrf */
651 VTY_PUSH_CONTEXT(VRF_NODE
, vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
));
652 vty
->node
= CONFIG_NODE
;
659 "Select a VRF to configure\n"
663 const char *vrfname
= argv
[idx_name
]->arg
;
665 return vrf_handler_create(vty
, vrfname
, NULL
);
672 "Delete a pseudo VRF's configuration\n"
675 const char *vrfname
= argv
[2]->arg
;
679 vrfp
= vrf_lookup_by_name(vrfname
);
682 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
683 return CMD_WARNING_CONFIG_FAILED
;
686 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
687 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
688 return CMD_WARNING_CONFIG_FAILED
;
691 /* Clear configured flag and invoke delete. */
692 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
699 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
701 DEFUN_NOSH (vrf_netns
,
704 "Attach VRF to a Namespace\n"
705 "The file name in " NS_RUN_DIR
", or a full pathname\n")
707 int idx_name
= 1, ret
;
708 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
710 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
713 return CMD_WARNING_CONFIG_FAILED
;
715 if (vrf_daemon_privs
&&
716 vrf_daemon_privs
->change(ZPRIVS_RAISE
))
717 zlog_err("%s: Can't raise privileges", __func__
);
719 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
, NS_UNKNOWN
);
721 if (vrf_daemon_privs
&&
722 vrf_daemon_privs
->change(ZPRIVS_LOWER
))
723 zlog_err("%s: Can't lower privileges", __func__
);
731 "Detach VRF from a Namespace\n"
732 "The file name in " NS_RUN_DIR
", or a full pathname\n")
734 struct ns
*ns
= NULL
;
736 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
738 if (!vrf_is_backend_netns()) {
739 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
740 return CMD_WARNING_CONFIG_FAILED
;
743 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
744 vrf
->name
, vrf
->vrf_id
);
745 return CMD_WARNING_CONFIG_FAILED
;
748 ns
= (struct ns
*)vrf
->ns_ctxt
;
752 /* vrf ID from VRF is necessary for Zebra
753 * so that propagate to other clients is done
761 * Debug CLI for vrf's
786 static int vrf_write_host(struct vty
*vty
)
789 vty_out(vty
, "debug vrf\n");
794 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
796 void vrf_install_commands(void)
798 install_node(&vrf_debug_node
, vrf_write_host
);
800 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
801 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
802 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
803 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
806 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
807 struct zebra_privs_t
*daemon_privs
)
809 install_element(CONFIG_NODE
, &vrf_cmd
);
810 install_element(CONFIG_NODE
, &no_vrf_cmd
);
811 install_node(&vrf_node
, writefunc
);
812 install_default(VRF_NODE
);
813 install_element(VRF_NODE
, &vrf_exit_cmd
);
814 if (vrf_is_backend_netns() && ns_have_netns()) {
815 /* Install NS commands. */
816 vrf_daemon_privs
= daemon_privs
;
817 install_element(VRF_NODE
, &vrf_netns_cmd
);
818 install_element(VRF_NODE
, &no_vrf_netns_cmd
);
822 vrf_id_t
vrf_get_default_id(void)
824 struct vrf
*vrf
= vrf_lookup_by_name(VRF_DEFAULT_NAME
);
828 if (vrf_is_backend_netns())
829 return ns_get_default_id();
831 return VRF_DEFAULT_INTERNAL
;
834 int vrf_bind(vrf_id_t vrf_id
, int fd
, char *name
)
838 if (fd
< 0 || name
== NULL
)
840 if (vrf_is_mapped_on_netns(vrf_id
))
842 #ifdef SO_BINDTODEVICE
843 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
));
845 zlog_debug("bind to interface %s failed, errno=%d", name
,
847 #endif /* SO_BINDTODEVICE */
850 int vrf_getaddrinfo(const char *node
, const char *service
,
851 const struct addrinfo
*hints
, struct addrinfo
**res
,
854 int ret
, ret2
, save_errno
;
856 ret
= vrf_switch_to_netns(vrf_id
);
858 zlog_err("%s: Can't switch to VRF %u (%s)", __func__
, vrf_id
,
859 safe_strerror(errno
));
860 ret
= getaddrinfo(node
, service
, hints
, res
);
862 ret2
= vrf_switchback_to_initial();
864 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
865 vrf_id
, safe_strerror(errno
));
870 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
872 int ret
, saved_errno
, rc
;
874 ret
= vrf_switch_to_netns(vrf_id
);
876 zlog_err("%s: Can't switch to VRF %u (%s)", __func__
, vrf_id
,
877 safe_strerror(errno
));
880 rc
= ioctl(d
, request
, params
);
882 ret
= vrf_switchback_to_initial();
884 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
885 vrf_id
, safe_strerror(errno
));
890 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
893 int ret
, save_errno
, ret2
;
895 ret
= vrf_switch_to_netns(vrf_id
);
897 zlog_err("%s: Can't switch to VRF %u (%s)", __func__
, vrf_id
,
898 safe_strerror(errno
));
899 ret
= sockunion_socket(su
);
901 ret2
= vrf_switchback_to_initial();
903 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
904 vrf_id
, safe_strerror(errno
));
909 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);