vrf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * VRF functions.
  3. * Copyright (C) 2014 6WIND S.A.
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  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.
  11. *
  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.
  16. *
  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.
  21. */
  22. #include <zebra.h>
  23. #include "vrf.h"
  24. #include "prefix.h"
  25. #include "table.h"
  26. #include "log.h"
  27. #include "memory.h"
  28. struct vrf
  29. {
  30. /* Identifier, same as the vector index */
  31. vrf_id_t vrf_id;
  32. /* Name */
  33. char *name;
  34. /* User data */
  35. void *info;
  36. };
  37. /* Holding VRF hooks */
  38. struct vrf_master
  39. {
  40. int (*vrf_new_hook) (vrf_id_t, void **);
  41. int (*vrf_delete_hook) (vrf_id_t, void **);
  42. } vrf_master = {0,};
  43. /* VRF table */
  44. struct route_table *vrf_table = NULL;
  45. /* Build the table key */
  46. static void
  47. vrf_build_key (vrf_id_t vrf_id, struct prefix *p)
  48. {
  49. p->family = AF_INET;
  50. p->prefixlen = IPV4_MAX_BITLEN;
  51. p->u.prefix4.s_addr = vrf_id;
  52. }
  53. /* Get a VRF. If not found, create one. */
  54. static struct vrf *
  55. vrf_get (vrf_id_t vrf_id)
  56. {
  57. struct prefix p;
  58. struct route_node *rn;
  59. struct vrf *vrf;
  60. vrf_build_key (vrf_id, &p);
  61. rn = route_node_get (vrf_table, &p);
  62. if (rn->info)
  63. {
  64. vrf = (struct vrf *)rn->info;
  65. route_unlock_node (rn); /* get */
  66. return vrf;
  67. }
  68. vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
  69. vrf->vrf_id = vrf_id;
  70. rn->info = vrf;
  71. zlog_info ("VRF %u is created.", vrf_id);
  72. if (vrf_master.vrf_new_hook)
  73. (*vrf_master.vrf_new_hook) (vrf_id, &vrf->info);
  74. return vrf;
  75. }
  76. /* Delete a VRF. This is called in vrf_terminate(). */
  77. static void
  78. vrf_delete (struct vrf *vrf)
  79. {
  80. zlog_info ("VRF %u is to be deleted.", vrf->vrf_id);
  81. if (vrf_master.vrf_delete_hook)
  82. (*vrf_master.vrf_delete_hook) (vrf->vrf_id, &vrf->info);
  83. if (vrf->name)
  84. XFREE (MTYPE_VRF_NAME, vrf->name);
  85. XFREE (MTYPE_VRF, vrf);
  86. }
  87. /* Look up a VRF by identifier. */
  88. static struct vrf *
  89. vrf_lookup (vrf_id_t vrf_id)
  90. {
  91. struct prefix p;
  92. struct route_node *rn;
  93. struct vrf *vrf = NULL;
  94. vrf_build_key (vrf_id, &p);
  95. rn = route_node_lookup (vrf_table, &p);
  96. if (rn)
  97. {
  98. vrf = (struct vrf *)rn->info;
  99. route_unlock_node (rn); /* lookup */
  100. }
  101. return vrf;
  102. }
  103. /* Add a VRF hook. Please add hooks before calling vrf_init(). */
  104. void
  105. vrf_add_hook (int type, int (*func)(vrf_id_t, void **))
  106. {
  107. switch (type) {
  108. case VRF_NEW_HOOK:
  109. vrf_master.vrf_new_hook = func;
  110. break;
  111. case VRF_DELETE_HOOK:
  112. vrf_master.vrf_delete_hook = func;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. /* Return the iterator of the first VRF. */
  119. vrf_iter_t
  120. vrf_first (void)
  121. {
  122. struct route_node *rn;
  123. for (rn = route_top (vrf_table); rn; rn = route_next (rn))
  124. if (rn->info)
  125. {
  126. route_unlock_node (rn); /* top/next */
  127. return (vrf_iter_t)rn;
  128. }
  129. return VRF_ITER_INVALID;
  130. }
  131. /* Return the next VRF iterator to the given iterator. */
  132. vrf_iter_t
  133. vrf_next (vrf_iter_t iter)
  134. {
  135. struct route_node *rn = NULL;
  136. /* Lock it first because route_next() will unlock it. */
  137. if (iter != VRF_ITER_INVALID)
  138. rn = route_next (route_lock_node ((struct route_node *)iter));
  139. for (; rn; rn = route_next (rn))
  140. if (rn->info)
  141. {
  142. route_unlock_node (rn); /* next */
  143. return (vrf_iter_t)rn;
  144. }
  145. return VRF_ITER_INVALID;
  146. }
  147. /* Return the VRF iterator of the given VRF ID. If it does not exist,
  148. * the iterator of the next existing VRF is returned. */
  149. vrf_iter_t
  150. vrf_iterator (vrf_id_t vrf_id)
  151. {
  152. struct prefix p;
  153. struct route_node *rn;
  154. vrf_build_key (vrf_id, &p);
  155. rn = route_node_get (vrf_table, &p);
  156. if (rn->info)
  157. {
  158. /* OK, the VRF exists. */
  159. route_unlock_node (rn); /* get */
  160. return (vrf_iter_t)rn;
  161. }
  162. /* Find the next VRF. */
  163. for (rn = route_next (rn); rn; rn = route_next (rn))
  164. if (rn->info)
  165. {
  166. route_unlock_node (rn); /* next */
  167. return (vrf_iter_t)rn;
  168. }
  169. return VRF_ITER_INVALID;
  170. }
  171. /* Obtain the VRF ID from the given VRF iterator. */
  172. vrf_id_t
  173. vrf_iter2id (vrf_iter_t iter)
  174. {
  175. struct route_node *rn = (struct route_node *) iter;
  176. return (rn && rn->info) ? ((struct vrf *)rn->info)->vrf_id : VRF_DEFAULT;
  177. }
  178. /* Obtain the data pointer from the given VRF iterator. */
  179. void *
  180. vrf_iter2info (vrf_iter_t iter)
  181. {
  182. struct route_node *rn = (struct route_node *) iter;
  183. return (rn && rn->info) ? ((struct vrf *)rn->info)->info : NULL;
  184. }
  185. /* Get the data pointer of the specified VRF. If not found, create one. */
  186. void *
  187. vrf_info_get (vrf_id_t vrf_id)
  188. {
  189. struct vrf *vrf = vrf_get (vrf_id);
  190. return vrf->info;
  191. }
  192. /* Look up the data pointer of the specified VRF. */
  193. void *
  194. vrf_info_lookup (vrf_id_t vrf_id)
  195. {
  196. struct vrf *vrf = vrf_lookup (vrf_id);
  197. return vrf ? vrf->info : NULL;
  198. }
  199. /* Initialize VRF module. */
  200. void
  201. vrf_init (void)
  202. {
  203. struct vrf *default_vrf;
  204. /* Allocate VRF table. */
  205. vrf_table = route_table_init ();
  206. /* The default VRF always exists. */
  207. default_vrf = vrf_get (VRF_DEFAULT);
  208. if (!default_vrf)
  209. {
  210. zlog_err ("vrf_init: failed to create the default VRF!");
  211. exit (1);
  212. }
  213. /* Set the default VRF name. */
  214. default_vrf->name = XSTRDUP (MTYPE_VRF_NAME, "Default-IP-Routing-Table");
  215. }
  216. /* Terminate VRF module. */
  217. void
  218. vrf_terminate (void)
  219. {
  220. struct route_node *rn;
  221. struct vrf *vrf;
  222. for (rn = route_top (vrf_table); rn; rn = route_next (rn))
  223. if ((vrf = rn->info) != NULL)
  224. vrf_delete (vrf);
  225. route_table_finish (vrf_table);
  226. vrf_table = NULL;
  227. }