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.
34 * Turn on/off debug code
39 /* Holding VRF hooks */
42 int (*vrf_new_hook
) (vrf_id_t
, const char *, void **);
43 int (*vrf_delete_hook
) (vrf_id_t
, const char *, void **);
44 int (*vrf_enable_hook
) (vrf_id_t
, const char *, void **);
45 int (*vrf_disable_hook
) (vrf_id_t
, const char *, void **);
49 struct route_table
*vrf_table
= NULL
;
51 /* VRF is part of a list too to store it before its actually active */
52 struct list
*vrf_list
;
54 static int vrf_is_enabled (struct vrf
*vrf
);
55 static void vrf_disable (struct vrf
*vrf
);
57 /* VRF list existance check by name. */
59 vrf_list_lookup_by_name (const char *name
)
61 struct listnode
*node
;
65 for (ALL_LIST_ELEMENTS_RO (vrf_list
, node
, vrfp
))
67 if (strcmp(name
, vrfp
->name
) == 0)
73 /* Build the table key */
75 vrf_build_key (vrf_id_t vrf_id
, struct prefix
*p
)
78 p
->prefixlen
= IPV4_MAX_BITLEN
;
79 p
->u
.prefix4
.s_addr
= vrf_id
;
82 /* Get a VRF. If not found, create one.
84 * name - The name of the vrf. May be NULL if unknown.
85 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
86 * Description: Please note that this routine can be called with just the name
90 vrf_get (vrf_id_t vrf_id
, const char *name
)
93 struct route_node
*rn
= NULL
;
94 struct vrf
*vrf
= NULL
;
97 zlog_debug ("VRF_GET: %s(%d)", name
, vrf_id
);
100 * Nothing to see, move along here
102 if (!name
&& vrf_id
== VRF_UNKNOWN
)
106 * Valid vrf name and unknown vrf_id case
108 * This is called when we are configured from
109 * the cli but we have no kernel information yet.
111 if (name
&& vrf_id
== VRF_UNKNOWN
)
113 vrf
= vrf_list_lookup_by_name (name
);
117 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
119 zlog_debug ("VRF(%u) %s is created.",
120 vrf_id
, (name
) ? name
: "(NULL)");
121 strcpy (vrf
->name
, name
);
122 listnode_add_sort (vrf_list
, vrf
);
123 if_init (&vrf
->iflist
);
124 if (vrf_master
.vrf_new_hook
)
126 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
128 if (debug_vrf
&& vrf
->info
)
129 zlog_info ("zvrf is created.");
132 zlog_debug("Vrf Created: %p", vrf
);
136 * Valid vrf name and valid vrf_id case
138 * This can be passed from the kernel
140 else if (name
&& vrf_id
!= VRF_UNKNOWN
)
142 vrf
= vrf_list_lookup_by_name (name
);
146 * If the passed in vrf_id and name match
147 * return, nothing to do here.
149 if (vrf
->vrf_id
== vrf_id
)
153 * Now we have a situation where we've had a
154 * vrf created, but not yet created the vrf_id route
155 * node, let's do so and match the code up.
157 vrf_build_key (vrf_id
, &p
);
158 rn
= route_node_get (vrf_table
, &p
);
162 vrf
->vrf_id
= vrf_id
;
163 if (vrf_master
.vrf_new_hook
)
164 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
167 zlog_debug("Vrf found matched stuff up: %p", vrf
);
174 * We can have 1 of two situations here
175 * We've already been told about the vrf_id
178 vrf_build_key (vrf_id
, &p
);
179 rn
= route_node_get (vrf_table
, &p
);
183 route_unlock_node (rn
);
185 * We know at this point that the vrf->name is not
186 * right because we would have caught it above.
189 strcpy (vrf
->name
, name
);
190 listnode_add_sort (vrf_list
, vrf
);
191 if (vrf_master
.vrf_new_hook
)
193 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
195 if (debug_vrf
&& vrf
->info
)
196 zlog_info ("zvrf is created.");
199 zlog_debug("Vrf Created: %p", vrf
);
204 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
208 vrf
->vrf_id
= vrf_id
;
209 strcpy (vrf
->name
, name
);
210 listnode_add_sort (vrf_list
, vrf
);
211 if_init (&vrf
->iflist
);
212 if (vrf_master
.vrf_new_hook
)
214 (*vrf_master
.vrf_new_hook
) (vrf_id
, name
, &vrf
->info
);
216 if (debug_vrf
&& vrf
->info
)
217 zlog_info ("zvrf is created.");
220 zlog_debug("Vrf Created: %p", vrf
);
226 * The final case, we've been passed a valid vrf_id
227 * but no name. So we create the route node
228 * if it hasn't already been created.
232 vrf_build_key (vrf_id
, &p
);
233 rn
= route_node_get (vrf_table
, &p
);
235 zlog_debug("Vrf found: %p", rn
->info
);
239 route_unlock_node (rn
);
244 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
247 vrf
->vrf_id
= vrf_id
;
248 if_init (&vrf
->iflist
);
250 zlog_debug("Vrf Created: %p", vrf
);
256 * We shouldn't get here and if we do
257 * something has gone wrong.
262 /* Delete a VRF. This is called in vrf_terminate(). */
264 vrf_delete (struct vrf
*vrf
)
267 zlog_debug ("VRF %u is to be deleted.", vrf
->vrf_id
);
269 if (vrf_is_enabled (vrf
))
272 if (vrf_master
.vrf_delete_hook
)
273 (*vrf_master
.vrf_delete_hook
) (vrf
->vrf_id
, vrf
->name
, &vrf
->info
);
275 if_terminate (&vrf
->iflist
);
279 vrf
->node
->info
= NULL
;
280 route_unlock_node(vrf
->node
);
283 listnode_delete (vrf_list
, vrf
);
285 XFREE (MTYPE_VRF
, vrf
);
288 /* Look up a VRF by identifier. */
290 vrf_lookup (vrf_id_t vrf_id
)
293 struct route_node
*rn
;
294 struct vrf
*vrf
= NULL
;
296 vrf_build_key (vrf_id
, &p
);
297 rn
= route_node_lookup (vrf_table
, &p
);
300 vrf
= (struct vrf
*)rn
->info
;
301 route_unlock_node (rn
); /* lookup */
307 * Check whether the VRF is enabled - that is, whether the VRF
308 * is ready to allocate resources. Currently there's only one
309 * type of resource: socket.
312 vrf_is_enabled (struct vrf
*vrf
)
314 return vrf
&& CHECK_FLAG (vrf
->status
, VRF_ACTIVE
);
316 /*Pending: figure out the real use of this routine.. it used to be..
317 return vrf && vrf->vrf_id == VRF_DEFAULT;
322 * Enable a VRF - that is, let the VRF be ready to use.
323 * The VRF_ENABLE_HOOK callback will be called to inform
324 * that they can allocate resources in this VRF.
326 * RETURN: 1 - enabled successfully; otherwise, 0.
329 vrf_enable (struct vrf
*vrf
)
332 zlog_debug ("VRF %u is enabled.", vrf
->vrf_id
);
334 if (!CHECK_FLAG (vrf
->status
, VRF_ACTIVE
))
335 SET_FLAG (vrf
->status
, VRF_ACTIVE
);
337 if (vrf_master
.vrf_enable_hook
)
338 (*vrf_master
.vrf_enable_hook
) (vrf
->vrf_id
, vrf
->name
, &vrf
->info
);
344 * Disable a VRF - that is, let the VRF be unusable.
345 * The VRF_DELETE_HOOK callback will be called to inform
346 * that they must release the resources in the VRF.
349 vrf_disable (struct vrf
*vrf
)
351 if (vrf_is_enabled (vrf
))
353 UNSET_FLAG (vrf
->status
, VRF_ACTIVE
);
356 zlog_debug ("VRF %u is to be disabled.", vrf
->vrf_id
);
358 /* Till now, nothing to be done for the default VRF. */
359 //Pending: see why this statement.
361 if (vrf_master
.vrf_disable_hook
)
362 (*vrf_master
.vrf_disable_hook
) (vrf
->vrf_id
, vrf
->name
, &vrf
->info
);
368 /* Add a VRF hook. Please add hooks before calling vrf_init(). */
370 vrf_add_hook (int type
, int (*func
)(vrf_id_t
, const char *, void **))
373 zlog_debug ("%s: Add Hook %d to function %p", __PRETTY_FUNCTION__
,
378 vrf_master
.vrf_new_hook
= func
;
380 case VRF_DELETE_HOOK
:
381 vrf_master
.vrf_delete_hook
= func
;
383 case VRF_ENABLE_HOOK
:
384 vrf_master
.vrf_enable_hook
= func
;
386 case VRF_DISABLE_HOOK
:
387 vrf_master
.vrf_disable_hook
= func
;
394 /* Return the iterator of the first VRF. */
398 struct route_node
*rn
;
400 for (rn
= route_top (vrf_table
); rn
; rn
= route_next (rn
))
403 route_unlock_node (rn
); /* top/next */
404 return (vrf_iter_t
)rn
;
406 return VRF_ITER_INVALID
;
409 /* Return the next VRF iterator to the given iterator. */
411 vrf_next (vrf_iter_t iter
)
413 struct route_node
*rn
= NULL
;
415 /* Lock it first because route_next() will unlock it. */
416 if (iter
!= VRF_ITER_INVALID
)
417 rn
= route_next (route_lock_node ((struct route_node
*)iter
));
419 for (; rn
; rn
= route_next (rn
))
422 route_unlock_node (rn
); /* next */
423 return (vrf_iter_t
)rn
;
425 return VRF_ITER_INVALID
;
428 /* Return the VRF iterator of the given VRF ID. If it does not exist,
429 * the iterator of the next existing VRF is returned. */
431 vrf_iterator (vrf_id_t vrf_id
)
434 struct route_node
*rn
;
436 vrf_build_key (vrf_id
, &p
);
437 rn
= route_node_get (vrf_table
, &p
);
440 /* OK, the VRF exists. */
441 route_unlock_node (rn
); /* get */
442 return (vrf_iter_t
)rn
;
445 /* Find the next VRF. */
446 for (rn
= route_next (rn
); rn
; rn
= route_next (rn
))
449 route_unlock_node (rn
); /* next */
450 return (vrf_iter_t
)rn
;
453 return VRF_ITER_INVALID
;
456 /* Obtain the VRF ID from the given VRF iterator. */
458 vrf_iter2id (vrf_iter_t iter
)
460 struct route_node
*rn
= (struct route_node
*) iter
;
461 return (rn
&& rn
->info
) ? ((struct vrf
*)rn
->info
)->vrf_id
: VRF_DEFAULT
;
465 vrf_iter2vrf (vrf_iter_t iter
)
467 struct route_node
*rn
= (struct route_node
*) iter
;
468 return (rn
&& rn
->info
) ? (struct vrf
*)rn
->info
: NULL
;
471 /* Obtain the data pointer from the given VRF iterator. */
473 vrf_iter2info (vrf_iter_t iter
)
475 struct route_node
*rn
= (struct route_node
*) iter
;
476 return (rn
&& rn
->info
) ? ((struct vrf
*)rn
->info
)->info
: NULL
;
479 /* Obtain the interface list from the given VRF iterator. */
481 vrf_iter2iflist (vrf_iter_t iter
)
483 struct route_node
*rn
= (struct route_node
*) iter
;
484 return (rn
&& rn
->info
) ? ((struct vrf
*)rn
->info
)->iflist
: NULL
;
487 /* Look up a VRF by name. */
489 vrf_lookup_by_name (const char *name
)
491 struct vrf
*vrf
= NULL
;
494 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
496 vrf
= vrf_iter2vrf (iter
);
497 if (vrf
&& !strcmp(vrf
->name
, name
))
505 vrf_name_to_id (const char *name
)
508 vrf_id_t vrf_id
= VRF_DEFAULT
; //Pending: need a way to return invalid id/ routine not used.
510 vrf
= vrf_lookup_by_name (name
);
512 vrf_id
= vrf
->vrf_id
;
517 /* Get the data pointer of the specified VRF. If not found, create one. */
519 vrf_info_get (vrf_id_t vrf_id
)
521 struct vrf
*vrf
= vrf_get (vrf_id
, NULL
);
525 /* Look up the data pointer of the specified VRF. */
527 vrf_info_lookup (vrf_id_t vrf_id
)
529 struct vrf
*vrf
= vrf_lookup (vrf_id
);
530 return vrf
? vrf
->info
: NULL
;
533 /* Look up the interface list in a VRF. */
535 vrf_iflist (vrf_id_t vrf_id
)
537 struct vrf
* vrf
= vrf_lookup (vrf_id
);
538 return vrf
? vrf
->iflist
: NULL
;
541 /* Get the interface list of the specified VRF. Create one if not find. */
543 vrf_iflist_get (vrf_id_t vrf_id
)
545 struct vrf
* vrf
= vrf_get (vrf_id
, NULL
);
549 /* Create the interface list for the specified VRF, if needed. */
551 vrf_iflist_create (vrf_id_t vrf_id
)
553 struct vrf
* vrf
= vrf_lookup (vrf_id
);
554 if (vrf
&& !vrf
->iflist
)
555 if_init (&vrf
->iflist
);
558 /* Free the interface list of the specified VRF. */
560 vrf_iflist_terminate (vrf_id_t vrf_id
)
562 struct vrf
* vrf
= vrf_lookup (vrf_id
);
563 if (vrf
&& vrf
->iflist
)
564 if_terminate (&vrf
->iflist
);
571 #define VRF_BITMAP_NUM_OF_GROUPS 8
572 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP \
573 (UINT16_MAX / VRF_BITMAP_NUM_OF_GROUPS)
574 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
575 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
577 #define VRF_BITMAP_GROUP(_id) \
578 ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
579 #define VRF_BITMAP_BIT_OFFSET(_id) \
580 ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
582 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) \
583 ((_bit_offset) / CHAR_BIT)
584 #define VRF_BITMAP_FLAG(_bit_offset) \
585 (((u_char)1) << ((_bit_offset) % CHAR_BIT))
589 u_char
*groups
[VRF_BITMAP_NUM_OF_GROUPS
];
593 vrf_bitmap_init (void)
595 return (vrf_bitmap_t
) XCALLOC (MTYPE_VRF_BITMAP
, sizeof (struct vrf_bitmap
));
599 vrf_bitmap_free (vrf_bitmap_t bmap
)
601 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
604 if (bmap
== VRF_BITMAP_NULL
)
607 for (i
= 0; i
< VRF_BITMAP_NUM_OF_GROUPS
; i
++)
609 XFREE (MTYPE_VRF_BITMAP
, bm
->groups
[i
]);
611 XFREE (MTYPE_VRF_BITMAP
, bm
);
615 vrf_bitmap_set (vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
617 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
618 u_char group
= VRF_BITMAP_GROUP (vrf_id
);
619 u_char offset
= VRF_BITMAP_BIT_OFFSET (vrf_id
);
621 if (bmap
== VRF_BITMAP_NULL
)
624 if (bm
->groups
[group
] == NULL
)
625 bm
->groups
[group
] = XCALLOC (MTYPE_VRF_BITMAP
,
626 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP
);
628 SET_FLAG (bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP (offset
)],
629 VRF_BITMAP_FLAG (offset
));
633 vrf_bitmap_unset (vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
635 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
636 u_char group
= VRF_BITMAP_GROUP (vrf_id
);
637 u_char offset
= VRF_BITMAP_BIT_OFFSET (vrf_id
);
639 if (bmap
== VRF_BITMAP_NULL
|| bm
->groups
[group
] == NULL
)
642 UNSET_FLAG (bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP (offset
)],
643 VRF_BITMAP_FLAG (offset
));
647 vrf_bitmap_check (vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
649 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*) bmap
;
650 u_char group
= VRF_BITMAP_GROUP (vrf_id
);
651 u_char offset
= VRF_BITMAP_BIT_OFFSET (vrf_id
);
653 if (bmap
== VRF_BITMAP_NULL
|| bm
->groups
[group
] == NULL
)
656 return CHECK_FLAG (bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP (offset
)],
657 VRF_BITMAP_FLAG (offset
)) ? 1 : 0;
660 /* Compare interface names, returning an integer greater than, equal to, or
661 * less than 0, (following the strcmp convention), according to the
662 * relationship between vrfp1 and vrfp2. Interface names consist of an
663 * alphabetic prefix and a numeric suffix. The primary sort key is
664 * lexicographic by name, and then numeric by number. No number sorts
665 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
666 * devpty0, de0 < del0
669 vrf_cmp_func (struct vrf
*vrfp1
, struct vrf
*vrfp2
)
671 return if_cmp_name_func (vrfp1
->name
, vrfp2
->name
);
674 /* Initialize VRF module. */
678 struct vrf
*default_vrf
;
681 zlog_debug ("%s: Initializing VRF subsystem", __PRETTY_FUNCTION__
);
683 vrf_list
= list_new ();
684 vrf_list
->cmp
= (int (*)(void *, void *))vrf_cmp_func
;
686 /* Allocate VRF table. */
687 vrf_table
= route_table_init ();
689 /* The default VRF always exists. */
690 default_vrf
= vrf_get (VRF_DEFAULT
, VRF_DEFAULT_NAME
);
693 zlog_err ("vrf_init: failed to create the default VRF!");
697 /* Enable the default VRF. */
698 if (!vrf_enable (default_vrf
))
700 zlog_err ("vrf_init: failed to enable the default VRF!");
705 /* Terminate VRF module. */
709 struct route_node
*rn
;
713 zlog_debug ("%s: Shutting down vrf subsystem", __PRETTY_FUNCTION__
);
715 for (rn
= route_top (vrf_table
); rn
; rn
= route_next (rn
))
716 if ((vrf
= rn
->info
) != NULL
)
719 route_table_finish (vrf_table
);
723 /* Create a socket for the VRF. */
725 vrf_socket (int domain
, int type
, int protocol
, vrf_id_t vrf_id
)
729 ret
= socket (domain
, type
, protocol
);
735 * Debug CLI for vrf's
761 vrf_write_host (struct vty
*vty
)
764 vty_out (vty
, "debug vrf%s", VTY_NEWLINE
);
769 static struct cmd_node vrf_debug_node
=
777 vrf_install_commands (void)
779 install_node (&vrf_debug_node
, vrf_write_host
);
781 install_element (CONFIG_NODE
, &vrf_debug_cmd
);
782 install_element (ENABLE_NODE
, &vrf_debug_cmd
);
783 install_element (CONFIG_NODE
, &no_vrf_debug_cmd
);
784 install_element (ENABLE_NODE
, &no_vrf_debug_cmd
);