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
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
33 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF")
34 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map")
39 * Turn on/off debug code
44 /* Holding VRF hooks */
47 int (*vrf_new_hook
) (vrf_id_t
, const char *, void **);
48 int (*vrf_delete_hook
) (vrf_id_t
, const char *, void **);
49 int (*vrf_enable_hook
) (vrf_id_t
, const char *, void **);
50 int (*vrf_disable_hook
) (vrf_id_t
, const char *, void **);
54 struct route_table
*vrf_table
= NULL
;
56 /* VRF is part of a list too to store it before its actually active */
57 struct list
*vrf_list
;
59 static int vrf_is_enabled (struct vrf
*vrf
);
60 static void vrf_disable (struct vrf
*vrf
);
62 /* VRF list existance check by name. */
64 vrf_list_lookup_by_name (const char *name
)
66 struct listnode
*node
;
70 for (ALL_LIST_ELEMENTS_RO (vrf_list
, node
, vrfp
))
72 if (strcmp(name
, vrfp
->name
) == 0)
78 /* Build the table key */
80 vrf_build_key (vrf_id_t vrf_id
, struct prefix
*p
)
83 p
->prefixlen
= IPV4_MAX_BITLEN
;
84 p
->u
.prefix4
.s_addr
= vrf_id
;
87 /* Get a VRF. If not found, create one.
89 * name - The name of the vrf. May be NULL if unknown.
90 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
91 * Description: Please note that this routine can be called with just the name
95 vrf_get (vrf_id_t vrf_id
, const char *name
)
98 struct route_node
*rn
= NULL
;
99 struct vrf
*vrf
= NULL
;
102 zlog_debug ("VRF_GET: %s(%d)", name
, vrf_id
);
105 * Nothing to see, move along here
107 if (!name
&& vrf_id
== VRF_UNKNOWN
)
111 * Valid vrf name and unknown vrf_id case
113 * This is called when we are configured from
114 * the cli but we have no kernel information yet.
116 if (name
&& vrf_id
== VRF_UNKNOWN
)
118 vrf
= vrf_list_lookup_by_name (name
);
122 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
124 zlog_debug ("VRF(%u) %s is created.",
125 vrf_id
, (name
) ? name
: "(NULL)");
126 strcpy (vrf
->name
, name
);
127 listnode_add_sort (vrf_list
, vrf
);
128 if_init (&vrf
->iflist
);
130 if (vrf_master
.vrf_new_hook
)
132 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
134 if (debug_vrf
&& vrf
->info
)
135 zlog_info ("zvrf is created.");
138 zlog_debug("Vrf Created: %p", vrf
);
142 * Valid vrf name and valid vrf_id case
144 * This can be passed from the kernel
146 else if (name
&& vrf_id
!= VRF_UNKNOWN
)
148 vrf
= vrf_list_lookup_by_name (name
);
152 * If the passed in vrf_id and name match
153 * return, nothing to do here.
155 if (vrf
->vrf_id
== vrf_id
)
159 * Now we have a situation where we've had a
160 * vrf created, but not yet created the vrf_id route
161 * node, let's do so and match the code up.
163 vrf_build_key (vrf_id
, &p
);
164 rn
= route_node_get (vrf_table
, &p
);
168 vrf
->vrf_id
= vrf_id
;
169 if (vrf_master
.vrf_new_hook
)
170 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
173 zlog_debug("Vrf found matched stuff up: %p", vrf
);
180 * We can have 1 of two situations here
181 * We've already been told about the vrf_id
184 vrf_build_key (vrf_id
, &p
);
185 rn
= route_node_get (vrf_table
, &p
);
189 route_unlock_node (rn
);
191 * We know at this point that the vrf->name is not
192 * right because we would have caught it above.
195 strcpy (vrf
->name
, name
);
196 listnode_add_sort (vrf_list
, vrf
);
197 if (vrf_master
.vrf_new_hook
)
199 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
201 if (debug_vrf
&& vrf
->info
)
202 zlog_info ("zvrf is created.");
205 zlog_debug("Vrf Created: %p", vrf
);
210 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
214 vrf
->vrf_id
= vrf_id
;
215 strcpy (vrf
->name
, name
);
216 listnode_add_sort (vrf_list
, vrf
);
217 if_init (&vrf
->iflist
);
219 if (vrf_master
.vrf_new_hook
)
221 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
223 if (debug_vrf
&& vrf
->info
)
224 zlog_info ("zvrf is created.");
227 zlog_debug("Vrf Created: %p", vrf
);
233 * The final case, we've been passed a valid vrf_id
234 * but no name. So we create the route node
235 * if it hasn't already been created.
239 vrf_build_key (vrf_id
, &p
);
240 rn
= route_node_get (vrf_table
, &p
);
242 zlog_debug("Vrf found: %p", rn
->info
);
246 route_unlock_node (rn
);
251 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
254 vrf
->vrf_id
= vrf_id
;
255 if_init (&vrf
->iflist
);
258 zlog_debug("Vrf Created: %p", vrf
);
264 * We shouldn't get here and if we do
265 * something has gone wrong.
270 /* Delete a VRF. This is called in vrf_terminate(). */
272 vrf_delete (struct vrf
*vrf
)
275 zlog_debug ("VRF %u is to be deleted.", vrf
->vrf_id
);
277 if (vrf_is_enabled (vrf
))
280 if (vrf_master
.vrf_delete_hook
)
281 (*vrf_master
.vrf_delete_hook
) (vrf
->vrf_id
, vrf
->name
, &vrf
->info
);
284 if_terminate (&vrf
->iflist
);
288 vrf
->node
->info
= NULL
;
289 route_unlock_node(vrf
->node
);
292 listnode_delete (vrf_list
, vrf
);
294 XFREE (MTYPE_VRF
, vrf
);
297 /* Look up a VRF by identifier. */
299 vrf_lookup (vrf_id_t vrf_id
)
302 struct route_node
*rn
;
303 struct vrf
*vrf
= NULL
;
305 vrf_build_key (vrf_id
, &p
);
306 rn
= route_node_lookup (vrf_table
, &p
);
309 vrf
= (struct vrf
*)rn
->info
;
310 route_unlock_node (rn
); /* lookup */
316 * Check whether the VRF is enabled - that is, whether the VRF
317 * is ready to allocate resources. Currently there's only one
318 * type of resource: socket.
321 vrf_is_enabled (struct vrf
*vrf
)
323 return vrf
&& CHECK_FLAG (vrf
->status
, VRF_ACTIVE
);
325 /*Pending: figure out the real use of this routine.. it used to be..
326 return vrf && vrf->vrf_id == VRF_DEFAULT;
331 * Enable a VRF - that is, let the VRF be ready to use.
332 * The VRF_ENABLE_HOOK callback will be called to inform
333 * that they can allocate resources in this VRF.
335 * RETURN: 1 - enabled successfully; otherwise, 0.
338 vrf_enable (struct vrf
*vrf
)
341 zlog_debug ("VRF %u is enabled.", vrf
->vrf_id
);
343 if (!CHECK_FLAG (vrf
->status
, VRF_ACTIVE
))
344 SET_FLAG (vrf
->status
, VRF_ACTIVE
);
346 if (vrf_master
.vrf_enable_hook
)
347 (*vrf_master
.vrf_enable_hook
) (vrf
->vrf_id
, vrf
->name
, &vrf
->info
);
353 * Disable a VRF - that is, let the VRF be unusable.
354 * The VRF_DELETE_HOOK callback will be called to inform
355 * that they must release the resources in the VRF.
358 vrf_disable (struct vrf
*vrf
)
360 if (vrf_is_enabled (vrf
))
362 UNSET_FLAG (vrf
->status
, VRF_ACTIVE
);
365 zlog_debug ("VRF %u is to be disabled.", vrf
->vrf_id
);
367 /* Till now, nothing to be done for the default VRF. */
368 //Pending: see why this statement.
370 if (vrf_master
.vrf_disable_hook
)
371 (*vrf_master
.vrf_disable_hook
) (vrf
->vrf_id
, vrf
->name
, &vrf
->info
);
377 /* Add a VRF hook. Please add hooks before calling vrf_init(). */
379 vrf_add_hook (int type
, int (*func
)(vrf_id_t
, const char *, void **))
382 zlog_debug ("%s: Add Hook %d to function %p", __PRETTY_FUNCTION__
,
387 vrf_master
.vrf_new_hook
= func
;
389 case VRF_DELETE_HOOK
:
390 vrf_master
.vrf_delete_hook
= func
;
392 case VRF_ENABLE_HOOK
:
393 vrf_master
.vrf_enable_hook
= func
;
395 case VRF_DISABLE_HOOK
:
396 vrf_master
.vrf_disable_hook
= func
;
403 /* Return the iterator of the first VRF. */
407 struct route_node
*rn
;
409 for (rn
= route_top (vrf_table
); rn
; rn
= route_next (rn
))
412 route_unlock_node (rn
); /* top/next */
413 return (vrf_iter_t
)rn
;
415 return VRF_ITER_INVALID
;
418 /* Return the next VRF iterator to the given iterator. */
420 vrf_next (vrf_iter_t iter
)
422 struct route_node
*rn
= NULL
;
424 /* Lock it first because route_next() will unlock it. */
425 if (iter
!= VRF_ITER_INVALID
)
426 rn
= route_next (route_lock_node ((struct route_node
*)iter
));
428 for (; rn
; rn
= route_next (rn
))
431 route_unlock_node (rn
); /* next */
432 return (vrf_iter_t
)rn
;
434 return VRF_ITER_INVALID
;
437 /* Return the VRF iterator of the given VRF ID. If it does not exist,
438 * the iterator of the next existing VRF is returned. */
440 vrf_iterator (vrf_id_t vrf_id
)
443 struct route_node
*rn
;
445 vrf_build_key (vrf_id
, &p
);
446 rn
= route_node_get (vrf_table
, &p
);
449 /* OK, the VRF exists. */
450 route_unlock_node (rn
); /* get */
451 return (vrf_iter_t
)rn
;
454 /* Find the next VRF. */
455 for (rn
= route_next (rn
); rn
; rn
= route_next (rn
))
458 route_unlock_node (rn
); /* next */
459 return (vrf_iter_t
)rn
;
462 return VRF_ITER_INVALID
;
465 /* Obtain the VRF ID from the given VRF iterator. */
467 vrf_iter2id (vrf_iter_t iter
)
469 struct route_node
*rn
= (struct route_node
*) iter
;
470 return (rn
&& rn
->info
) ? ((struct vrf
*)rn
->info
)->vrf_id
: VRF_DEFAULT
;
474 vrf_iter2vrf (vrf_iter_t iter
)
476 struct route_node
*rn
= (struct route_node
*) iter
;
477 return (rn
&& rn
->info
) ? (struct vrf
*)rn
->info
: NULL
;
480 /* Obtain the data pointer from the given VRF iterator. */
482 vrf_iter2info (vrf_iter_t iter
)
484 struct route_node
*rn
= (struct route_node
*) iter
;
485 return (rn
&& rn
->info
) ? ((struct vrf
*)rn
->info
)->info
: NULL
;
488 /* Obtain the interface list from the given VRF iterator. */
490 vrf_iter2iflist (vrf_iter_t iter
)
492 struct route_node
*rn
= (struct route_node
*) iter
;
493 return (rn
&& rn
->info
) ? ((struct vrf
*)rn
->info
)->iflist
: NULL
;
496 /* Look up a VRF by name. */
498 vrf_lookup_by_name (const char *name
)
500 struct vrf
*vrf
= NULL
;
503 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
505 vrf
= vrf_iter2vrf (iter
);
506 if (vrf
&& !strcmp(vrf
->name
, name
))
514 vrf_name_to_id (const char *name
)
517 vrf_id_t vrf_id
= VRF_DEFAULT
; //Pending: need a way to return invalid id/ routine not used.
519 vrf
= vrf_lookup_by_name (name
);
521 vrf_id
= vrf
->vrf_id
;
526 /* Get the data pointer of the specified VRF. If not found, create one. */
528 vrf_info_get (vrf_id_t vrf_id
)
530 struct vrf
*vrf
= vrf_get (vrf_id
, NULL
);
534 /* Look up the data pointer of the specified VRF. */
536 vrf_info_lookup (vrf_id_t vrf_id
)
538 struct vrf
*vrf
= vrf_lookup (vrf_id
);
539 return vrf
? vrf
->info
: NULL
;
542 /* Look up the interface list in a VRF. */
544 vrf_iflist (vrf_id_t vrf_id
)
546 struct vrf
* vrf
= vrf_lookup (vrf_id
);
547 return vrf
? vrf
->iflist
: NULL
;
550 /* Get the interface list of the specified VRF. Create one if not find. */
552 vrf_iflist_get (vrf_id_t vrf_id
)
554 struct vrf
* vrf
= vrf_get (vrf_id
, NULL
);
558 /* Create the interface list for the specified VRF, if needed. */
560 vrf_iflist_create (vrf_id_t vrf_id
)
562 struct vrf
* vrf
= vrf_lookup (vrf_id
);
563 if (vrf
&& !vrf
->iflist
)
564 if_init (&vrf
->iflist
);
567 /* Free the interface list of the specified VRF. */
569 vrf_iflist_terminate (vrf_id_t vrf_id
)
571 struct vrf
* vrf
= vrf_lookup (vrf_id
);
572 if (vrf
&& vrf
->iflist
)
573 if_terminate (&vrf
->iflist
);
580 #define VRF_BITMAP_NUM_OF_GROUPS 8
581 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP \
582 (UINT16_MAX / VRF_BITMAP_NUM_OF_GROUPS)
583 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
584 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
586 #define VRF_BITMAP_GROUP(_id) \
587 ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
588 #define VRF_BITMAP_BIT_OFFSET(_id) \
589 ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
591 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) \
592 ((_bit_offset) / CHAR_BIT)
593 #define VRF_BITMAP_FLAG(_bit_offset) \
594 (((u_char)1) << ((_bit_offset) % CHAR_BIT))
598 u_char
*groups
[VRF_BITMAP_NUM_OF_GROUPS
];
602 vrf_bitmap_init (void)
604 return (vrf_bitmap_t
) XCALLOC (MTYPE_VRF_BITMAP
, sizeof (struct vrf_bitmap
));
608 vrf_bitmap_free (vrf_bitmap_t bmap
)
610 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
613 if (bmap
== VRF_BITMAP_NULL
)
616 for (i
= 0; i
< VRF_BITMAP_NUM_OF_GROUPS
; i
++)
618 XFREE (MTYPE_VRF_BITMAP
, bm
->groups
[i
]);
620 XFREE (MTYPE_VRF_BITMAP
, bm
);
624 vrf_bitmap_set (vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
626 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
627 u_char group
= VRF_BITMAP_GROUP (vrf_id
);
628 u_char offset
= VRF_BITMAP_BIT_OFFSET (vrf_id
);
630 if (bmap
== VRF_BITMAP_NULL
)
633 if (bm
->groups
[group
] == NULL
)
634 bm
->groups
[group
] = XCALLOC (MTYPE_VRF_BITMAP
,
635 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP
);
637 SET_FLAG (bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP (offset
)],
638 VRF_BITMAP_FLAG (offset
));
642 vrf_bitmap_unset (vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
644 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
645 u_char group
= VRF_BITMAP_GROUP (vrf_id
);
646 u_char offset
= VRF_BITMAP_BIT_OFFSET (vrf_id
);
648 if (bmap
== VRF_BITMAP_NULL
|| bm
->groups
[group
] == NULL
)
651 UNSET_FLAG (bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP (offset
)],
652 VRF_BITMAP_FLAG (offset
));
656 vrf_bitmap_check (vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
658 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
659 u_char group
= VRF_BITMAP_GROUP (vrf_id
);
660 u_char offset
= VRF_BITMAP_BIT_OFFSET (vrf_id
);
662 if (bmap
== VRF_BITMAP_NULL
|| bm
->groups
[group
] == NULL
)
665 return CHECK_FLAG (bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP (offset
)],
666 VRF_BITMAP_FLAG (offset
)) ? 1 : 0;
669 /* Compare interface names, returning an integer greater than, equal to, or
670 * less than 0, (following the strcmp convention), according to the
671 * relationship between vrfp1 and vrfp2. Interface names consist of an
672 * alphabetic prefix and a numeric suffix. The primary sort key is
673 * lexicographic by name, and then numeric by number. No number sorts
674 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
675 * devpty0, de0 < del0
678 vrf_cmp_func (struct vrf
*vrfp1
, struct vrf
*vrfp2
)
680 return if_cmp_name_func (vrfp1
->name
, vrfp2
->name
);
683 /* Initialize VRF module. */
687 struct vrf
*default_vrf
;
690 zlog_debug ("%s: Initializing VRF subsystem", __PRETTY_FUNCTION__
);
692 vrf_list
= list_new ();
693 vrf_list
->cmp
= (int (*)(void *, void *))vrf_cmp_func
;
695 /* Allocate VRF table. */
696 vrf_table
= route_table_init ();
698 /* The default VRF always exists. */
699 default_vrf
= vrf_get (VRF_DEFAULT
, VRF_DEFAULT_NAME
);
702 zlog_err ("vrf_init: failed to create the default VRF!");
706 /* Enable the default VRF. */
707 if (!vrf_enable (default_vrf
))
709 zlog_err ("vrf_init: failed to enable the default VRF!");
714 /* Terminate VRF module. */
718 struct route_node
*rn
;
722 zlog_debug ("%s: Shutting down vrf subsystem", __PRETTY_FUNCTION__
);
724 for (rn
= route_top (vrf_table
); rn
; rn
= route_next (rn
))
725 if ((vrf
= rn
->info
) != NULL
)
728 route_table_finish (vrf_table
);
732 /* Create a socket for the VRF. */
734 vrf_socket (int domain
, int type
, int protocol
, vrf_id_t vrf_id
)
738 ret
= socket (domain
, type
, protocol
);
743 /* vrf CLI commands */
747 "Select a VRF to configure\n"
753 if ((sl
= strlen(argv
[0])) > VRF_NAMSIZ
)
755 vty_out (vty
, "%% VRF name %s is invalid: length exceeds "
757 argv
[0], VRF_NAMSIZ
, VTY_NEWLINE
);
761 vrfp
= vrf_get (VRF_UNKNOWN
, argv
[0]);
763 VTY_PUSH_CONTEXT_COMPAT (VRF_NODE
, vrfp
);
772 "Delete a pseudo VRF's configuration\n"
777 vrfp
= vrf_list_lookup_by_name (argv
[0]);
781 vty_out (vty
, "%% VRF %s does not exist%s", argv
[0], VTY_NEWLINE
);
785 if (CHECK_FLAG (vrfp
->status
, VRF_ACTIVE
))
787 vty_out (vty
, "%% Only inactive VRFs can be deleted%s",
798 * Debug CLI for vrf's
824 vrf_write_host (struct vty
*vty
)
827 vty_out (vty
, "debug vrf%s", VTY_NEWLINE
);
832 static struct cmd_node vrf_debug_node
=
840 vrf_install_commands (void)
842 install_node (&vrf_debug_node
, vrf_write_host
);
844 install_element (CONFIG_NODE
, &vrf_debug_cmd
);
845 install_element (ENABLE_NODE
, &vrf_debug_cmd
);
846 install_element (CONFIG_NODE
, &no_vrf_debug_cmd
);
847 install_element (ENABLE_NODE
, &no_vrf_debug_cmd
);