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 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF")
38 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map")
42 static __inline
int vrf_id_compare(const struct vrf
*, const struct vrf
*);
43 static __inline
int vrf_name_compare(const struct vrf
*, const struct vrf
*);
45 RB_GENERATE(vrf_id_head
, vrf
, id_entry
, vrf_id_compare
);
46 RB_GENERATE(vrf_name_head
, vrf
, name_entry
, vrf_name_compare
);
48 struct vrf_id_head vrfs_by_id
= RB_INITIALIZER(&vrfs_by_id
);
49 struct vrf_name_head vrfs_by_name
= RB_INITIALIZER(&vrfs_by_name
);
51 static int vrf_backend
;
54 * Turn on/off debug code
59 /* Holding VRF hooks */
61 int (*vrf_new_hook
)(struct vrf
*);
62 int (*vrf_delete_hook
)(struct vrf
*);
63 int (*vrf_enable_hook
)(struct vrf
*);
64 int (*vrf_disable_hook
)(struct vrf
*);
69 static int vrf_is_enabled(struct vrf
*vrf
);
71 /* VRF list existance check by name. */
72 struct vrf
*vrf_lookup_by_name(const char *name
)
75 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
76 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
79 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
81 return (a
->vrf_id
- b
->vrf_id
);
84 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
86 return strcmp(a
->name
, b
->name
);
89 int vrf_switch_to_netns(vrf_id_t vrf_id
)
92 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
94 /* VRF has no NETNS backend. silently ignore */
95 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
97 /* VRF is default VRF. silently ignore */
98 if (vrf
->vrf_id
== VRF_DEFAULT
)
100 name
= ns_netns_pathname(NULL
, vrf
->data
.l
.netns_name
);
102 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
103 return ns_switch_to_netns(name
);
106 int vrf_switchback_to_initial(void)
108 int ret
= ns_switchback_to_initial();
110 if (ret
== 0 && debug_vrf
)
111 zlog_debug("VRF_SWITCHBACK");
115 /* return 1 if vrf can be enabled */
116 int vrf_update_vrf_id(vrf_id_t vrf_id
, struct vrf
*vrf
)
122 old_vrf_id
= vrf
->vrf_id
;
123 if (vrf_id
== vrf
->vrf_id
)
125 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
126 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
127 vrf
->vrf_id
= vrf_id
;
128 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
129 if (old_vrf_id
== VRF_UNKNOWN
)
134 /* Get a VRF. If not found, create one.
136 * name - The name of the vrf. May be NULL if unknown.
137 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
138 * Description: Please note that this routine can be called with just the name
141 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
143 struct vrf
*vrf
= NULL
;
147 zlog_debug("VRF_GET: %s(%u)",
148 name
== NULL
? "(NULL)" : name
, vrf_id
);
150 /* Nothing to see, move along here */
151 if (!name
&& vrf_id
== VRF_UNKNOWN
)
154 /* Try to find VRF both by ID and name */
155 if (vrf_id
!= VRF_UNKNOWN
)
156 vrf
= vrf_lookup_by_id(vrf_id
);
158 vrf
= vrf_lookup_by_name(name
);
161 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
162 vrf
->vrf_id
= VRF_UNKNOWN
;
167 zlog_debug("VRF(%u) %s is created.", vrf_id
,
168 (name
) ? name
: "(NULL)");
172 if (vrf_id
!= VRF_UNKNOWN
&& vrf
->vrf_id
== VRF_UNKNOWN
) {
173 vrf
->vrf_id
= vrf_id
;
174 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
178 if (name
&& vrf
->name
[0] != '\0' && strcmp(name
, vrf
->name
)) {
179 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
180 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
181 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
182 } else if (name
&& vrf
->name
[0] == '\0') {
183 strlcpy(vrf
->name
, name
, sizeof(vrf
->name
));
184 RB_INSERT(vrf_name_head
, &vrfs_by_name
, vrf
);
186 if (new &&vrf_master
.vrf_new_hook
)
187 (*vrf_master
.vrf_new_hook
)(vrf
);
192 /* Delete a VRF. This is called when the underlying VRF goes away, a
193 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
195 void vrf_delete(struct vrf
*vrf
)
198 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
200 if (vrf_is_enabled(vrf
))
203 /* If the VRF is user configured, it'll stick around, just remove
204 * the ID mapping. Interfaces assigned to this VRF should've been
205 * removed already as part of the VRF going down.
207 if (vrf_is_user_cfged(vrf
)) {
208 if (vrf
->vrf_id
!= VRF_UNKNOWN
) {
209 /* Delete any VRF interfaces - should be only
210 * the VRF itself, other interfaces should've
211 * been moved out of the VRF.
214 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
215 vrf
->vrf_id
= VRF_UNKNOWN
;
220 if (vrf_master
.vrf_delete_hook
)
221 (*vrf_master
.vrf_delete_hook
)(vrf
);
226 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
227 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
228 if (vrf
->name
[0] != '\0')
229 RB_REMOVE(vrf_name_head
, &vrfs_by_name
, vrf
);
231 XFREE(MTYPE_VRF
, vrf
);
234 /* Look up a VRF by identifier. */
235 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
239 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
243 * Enable a VRF - that is, let the VRF be ready to use.
244 * The VRF_ENABLE_HOOK callback will be called to inform
245 * that they can allocate resources in this VRF.
247 * RETURN: 1 - enabled successfully; otherwise, 0.
249 int vrf_enable(struct vrf
*vrf
)
251 if (vrf_is_enabled(vrf
))
255 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
257 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
259 if (vrf_master
.vrf_enable_hook
)
260 (*vrf_master
.vrf_enable_hook
)(vrf
);
266 * Disable a VRF - that is, let the VRF be unusable.
267 * The VRF_DELETE_HOOK callback will be called to inform
268 * that they must release the resources in the VRF.
270 void vrf_disable(struct vrf
*vrf
)
272 if (!vrf_is_enabled(vrf
))
275 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
278 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
280 /* Till now, nothing to be done for the default VRF. */
281 // Pending: see why this statement.
283 if (vrf_master
.vrf_disable_hook
)
284 (*vrf_master
.vrf_disable_hook
)(vrf
);
287 const char *vrf_id_to_name(vrf_id_t vrf_id
)
291 vrf
= vrf_lookup_by_id(vrf_id
);
298 vrf_id_t
vrf_name_to_id(const char *name
)
301 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
302 // id/ routine not used.
304 vrf
= vrf_lookup_by_name(name
);
306 vrf_id
= vrf
->vrf_id
;
311 /* Get the data pointer of the specified VRF. If not found, create one. */
312 void *vrf_info_get(vrf_id_t vrf_id
)
314 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
318 /* Look up the data pointer of the specified VRF. */
319 void *vrf_info_lookup(vrf_id_t vrf_id
)
321 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
322 return vrf
? vrf
->info
: NULL
;
329 #define VRF_BITMAP_NUM_OF_GROUPS 1024
330 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP (UINT32_MAX / VRF_BITMAP_NUM_OF_GROUPS)
331 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
332 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
334 #define VRF_BITMAP_GROUP(_id) ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
335 #define VRF_BITMAP_BIT_OFFSET(_id) ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
337 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) ((_bit_offset) / CHAR_BIT)
338 #define VRF_BITMAP_FLAG(_bit_offset) (((u_char)1) << ((_bit_offset) % CHAR_BIT))
341 u_char
*groups
[VRF_BITMAP_NUM_OF_GROUPS
];
344 vrf_bitmap_t
vrf_bitmap_init(void)
346 return (vrf_bitmap_t
)XCALLOC(MTYPE_VRF_BITMAP
,
347 sizeof(struct vrf_bitmap
));
350 void vrf_bitmap_free(vrf_bitmap_t bmap
)
352 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
355 if (bmap
== VRF_BITMAP_NULL
)
358 for (i
= 0; i
< VRF_BITMAP_NUM_OF_GROUPS
; i
++)
360 XFREE(MTYPE_VRF_BITMAP
, bm
->groups
[i
]);
362 XFREE(MTYPE_VRF_BITMAP
, bm
);
365 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
367 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
368 u_char group
= VRF_BITMAP_GROUP(vrf_id
);
369 u_char offset
= VRF_BITMAP_BIT_OFFSET(vrf_id
);
371 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
)
374 if (bm
->groups
[group
] == NULL
)
375 bm
->groups
[group
] = XCALLOC(MTYPE_VRF_BITMAP
,
376 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP
);
378 SET_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
379 VRF_BITMAP_FLAG(offset
));
382 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
384 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
385 u_char group
= VRF_BITMAP_GROUP(vrf_id
);
386 u_char offset
= VRF_BITMAP_BIT_OFFSET(vrf_id
);
388 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
389 || bm
->groups
[group
] == NULL
)
392 UNSET_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
393 VRF_BITMAP_FLAG(offset
));
396 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
398 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
399 u_char group
= VRF_BITMAP_GROUP(vrf_id
);
400 u_char offset
= VRF_BITMAP_BIT_OFFSET(vrf_id
);
402 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
403 || bm
->groups
[group
] == NULL
)
406 return CHECK_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
407 VRF_BITMAP_FLAG(offset
))
412 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
414 struct vrf
*vrf
= NULL
;
416 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
417 if (vrf
->vrf_id
!= VRF_DEFAULT
)
418 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
422 static const struct cmd_variable_handler vrf_var_handlers
[] = {
425 .completions
= vrf_autocomplete
,
427 {.completions
= NULL
},
430 /* Initialize VRF module. */
431 void vrf_init(int (*create
)(struct vrf
*), int (*enable
)(struct vrf
*),
432 int (*disable
)(struct vrf
*), int (*delete)(struct vrf
*))
434 struct vrf
*default_vrf
;
437 zlog_debug("%s: Initializing VRF subsystem",
438 __PRETTY_FUNCTION__
);
440 vrf_master
.vrf_new_hook
= create
;
441 vrf_master
.vrf_enable_hook
= enable
;
442 vrf_master
.vrf_disable_hook
= disable
;
443 vrf_master
.vrf_delete_hook
= delete;
445 /* The default VRF always exists. */
446 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
448 zlog_err("vrf_init: failed to create the default VRF!");
452 /* Enable the default VRF. */
453 if (!vrf_enable(default_vrf
)) {
454 zlog_err("vrf_init: failed to enable the default VRF!");
458 cmd_variable_handler_register(vrf_var_handlers
);
461 /* Terminate VRF module. */
462 void vrf_terminate(void)
467 zlog_debug("%s: Shutting down vrf subsystem",
468 __PRETTY_FUNCTION__
);
470 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
471 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
473 /* Clear configured flag and invoke delete. */
474 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
478 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
479 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
481 /* Clear configured flag and invoke delete. */
482 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
487 /* Create a socket for the VRF. */
488 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
)
492 ret
= socket(domain
, type
, protocol
);
497 int vrf_is_backend_netns(void)
499 return (vrf_backend
== VRF_BACKEND_NETNS
);
502 int vrf_get_backend(void)
507 void vrf_configure_backend(int vrf_backend_netns
)
509 vrf_backend
= vrf_backend_netns
;
512 int vrf_handler_create(struct vty
*vty
, const char *vrfname
, struct vrf
**vrf
)
516 if (strlen(vrfname
) > VRF_NAMSIZ
) {
519 "%% VRF name %s invalid: length exceeds %d bytes\n",
520 vrfname
, VRF_NAMSIZ
);
523 "%% VRF name %s invalid: length exceeds %d bytes\n",
524 vrfname
, VRF_NAMSIZ
);
525 return CMD_WARNING_CONFIG_FAILED
;
528 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
531 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
538 int vrf_is_mapped_on_netns(vrf_id_t vrf_id
)
540 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
542 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
544 if (vrf
->vrf_id
== VRF_DEFAULT
)
549 /* vrf CLI commands */
553 "Select a VRF to configure\n"
557 const char *vrfname
= argv
[idx_name
]->arg
;
559 return vrf_handler_create(vty
, vrfname
, NULL
);
566 "Delete a pseudo VRF's configuration\n"
569 const char *vrfname
= argv
[2]->arg
;
573 vrfp
= vrf_lookup_by_name(vrfname
);
576 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
577 return CMD_WARNING_CONFIG_FAILED
;
580 if (CHECK_FLAG(vrfp
->status
, VRF_ACTIVE
)) {
581 vty_out(vty
, "%% Only inactive VRFs can be deleted\n");
582 return CMD_WARNING_CONFIG_FAILED
;
585 /* Clear configured flag and invoke delete. */
586 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
593 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
596 * Debug CLI for vrf's
621 static int vrf_write_host(struct vty
*vty
)
624 vty_out(vty
, "debug vrf\n");
629 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
631 void vrf_install_commands(void)
633 install_node(&vrf_debug_node
, vrf_write_host
);
635 install_element(CONFIG_NODE
, &vrf_debug_cmd
);
636 install_element(ENABLE_NODE
, &vrf_debug_cmd
);
637 install_element(CONFIG_NODE
, &no_vrf_debug_cmd
);
638 install_element(ENABLE_NODE
, &no_vrf_debug_cmd
);
641 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
))
643 install_element(CONFIG_NODE
, &vrf_cmd
);
644 install_element(CONFIG_NODE
, &no_vrf_cmd
);
645 install_node(&vrf_node
, writefunc
);
646 install_default(VRF_NODE
);