vrf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 "if.h"
  24. #include "vrf.h"
  25. #include "prefix.h"
  26. #include "table.h"
  27. #include "log.h"
  28. #include "memory.h"
  29. #define VRF_DEFAULT_NAME "Default-IP-Routing-Table"
  30. struct vrf
  31. {
  32. /* Identifier, same as the vector index */
  33. vrf_id_t vrf_id;
  34. /* Name */
  35. char *name;
  36. /* Master list of interfaces belonging to this VRF */
  37. struct list *iflist;
  38. /* User data */
  39. void *info;
  40. };
  41. /* Holding VRF hooks */
  42. struct vrf_master
  43. {
  44. int (*vrf_new_hook) (vrf_id_t, void **);
  45. int (*vrf_delete_hook) (vrf_id_t, void **);
  46. int (*vrf_enable_hook) (vrf_id_t, void **);
  47. int (*vrf_disable_hook) (vrf_id_t, void **);
  48. } vrf_master = {0,};
  49. /* VRF table */
  50. struct route_table *vrf_table = NULL;
  51. static int vrf_is_enabled (struct vrf *vrf);
  52. static int vrf_enable (struct vrf *vrf);
  53. static void vrf_disable (struct vrf *vrf);
  54. /* Build the table key */
  55. static void
  56. vrf_build_key (vrf_id_t vrf_id, struct prefix *p)
  57. {
  58. p->family = AF_INET;
  59. p->prefixlen = IPV4_MAX_BITLEN;
  60. p->u.prefix4.s_addr = vrf_id;
  61. }
  62. /* Get a VRF. If not found, create one. */
  63. static struct vrf *
  64. vrf_get (vrf_id_t vrf_id)
  65. {
  66. struct prefix p;
  67. struct route_node *rn;
  68. struct vrf *vrf;
  69. vrf_build_key (vrf_id, &p);
  70. rn = route_node_get (vrf_table, &p);
  71. if (rn->info)
  72. {
  73. vrf = (struct vrf *)rn->info;
  74. route_unlock_node (rn); /* get */
  75. return vrf;
  76. }
  77. vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
  78. vrf->vrf_id = vrf_id;
  79. rn->info = vrf;
  80. /* Initialize interfaces. */
  81. if_init (vrf_id, &vrf->iflist);
  82. zlog_info ("VRF %u is created.", vrf_id);
  83. if (vrf_master.vrf_new_hook)
  84. (*vrf_master.vrf_new_hook) (vrf_id, &vrf->info);
  85. return vrf;
  86. }
  87. /* Delete a VRF. This is called in vrf_terminate(). */
  88. static void
  89. vrf_delete (struct vrf *vrf)
  90. {
  91. zlog_info ("VRF %u is to be deleted.", vrf->vrf_id);
  92. if (vrf_is_enabled (vrf))
  93. vrf_disable (vrf);
  94. if (vrf_master.vrf_delete_hook)
  95. (*vrf_master.vrf_delete_hook) (vrf->vrf_id, &vrf->info);
  96. if_terminate (vrf->vrf_id, &vrf->iflist);
  97. if (vrf->name)
  98. XFREE (MTYPE_VRF_NAME, vrf->name);
  99. XFREE (MTYPE_VRF, vrf);
  100. }
  101. /* Look up a VRF by identifier. */
  102. static struct vrf *
  103. vrf_lookup (vrf_id_t vrf_id)
  104. {
  105. struct prefix p;
  106. struct route_node *rn;
  107. struct vrf *vrf = NULL;
  108. vrf_build_key (vrf_id, &p);
  109. rn = route_node_lookup (vrf_table, &p);
  110. if (rn)
  111. {
  112. vrf = (struct vrf *)rn->info;
  113. route_unlock_node (rn); /* lookup */
  114. }
  115. return vrf;
  116. }
  117. /*
  118. * Check whether the VRF is enabled - that is, whether the VRF
  119. * is ready to allocate resources. Currently there's only one
  120. * type of resource: socket.
  121. */
  122. static int
  123. vrf_is_enabled (struct vrf *vrf)
  124. {
  125. return vrf && vrf->vrf_id == VRF_DEFAULT;
  126. }
  127. /*
  128. * Enable a VRF - that is, let the VRF be ready to use.
  129. * The VRF_ENABLE_HOOK callback will be called to inform
  130. * that they can allocate resources in this VRF.
  131. *
  132. * RETURN: 1 - enabled successfully; otherwise, 0.
  133. */
  134. static int
  135. vrf_enable (struct vrf *vrf)
  136. {
  137. /* Till now, only the default VRF can be enabled. */
  138. if (vrf->vrf_id == VRF_DEFAULT)
  139. {
  140. zlog_info ("VRF %u is enabled.", vrf->vrf_id);
  141. if (vrf_master.vrf_enable_hook)
  142. (*vrf_master.vrf_enable_hook) (vrf->vrf_id, &vrf->info);
  143. return 1;
  144. }
  145. return 0;
  146. }
  147. /*
  148. * Disable a VRF - that is, let the VRF be unusable.
  149. * The VRF_DELETE_HOOK callback will be called to inform
  150. * that they must release the resources in the VRF.
  151. */
  152. static void
  153. vrf_disable (struct vrf *vrf)
  154. {
  155. if (vrf_is_enabled (vrf))
  156. {
  157. zlog_info ("VRF %u is to be disabled.", vrf->vrf_id);
  158. /* Till now, nothing to be done for the default VRF. */
  159. if (vrf_master.vrf_disable_hook)
  160. (*vrf_master.vrf_disable_hook) (vrf->vrf_id, &vrf->info);
  161. }
  162. }
  163. /* Add a VRF hook. Please add hooks before calling vrf_init(). */
  164. void
  165. vrf_add_hook (int type, int (*func)(vrf_id_t, void **))
  166. {
  167. switch (type) {
  168. case VRF_NEW_HOOK:
  169. vrf_master.vrf_new_hook = func;
  170. break;
  171. case VRF_DELETE_HOOK:
  172. vrf_master.vrf_delete_hook = func;
  173. break;
  174. case VRF_ENABLE_HOOK:
  175. vrf_master.vrf_enable_hook = func;
  176. break;
  177. case VRF_DISABLE_HOOK:
  178. vrf_master.vrf_disable_hook = func;
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. /* Return the iterator of the first VRF. */
  185. vrf_iter_t
  186. vrf_first (void)
  187. {
  188. struct route_node *rn;
  189. for (rn = route_top (vrf_table); rn; rn = route_next (rn))
  190. if (rn->info)
  191. {
  192. route_unlock_node (rn); /* top/next */
  193. return (vrf_iter_t)rn;
  194. }
  195. return VRF_ITER_INVALID;
  196. }
  197. /* Return the next VRF iterator to the given iterator. */
  198. vrf_iter_t
  199. vrf_next (vrf_iter_t iter)
  200. {
  201. struct route_node *rn = NULL;
  202. /* Lock it first because route_next() will unlock it. */
  203. if (iter != VRF_ITER_INVALID)
  204. rn = route_next (route_lock_node ((struct route_node *)iter));
  205. for (; rn; rn = route_next (rn))
  206. if (rn->info)
  207. {
  208. route_unlock_node (rn); /* next */
  209. return (vrf_iter_t)rn;
  210. }
  211. return VRF_ITER_INVALID;
  212. }
  213. /* Return the VRF iterator of the given VRF ID. If it does not exist,
  214. * the iterator of the next existing VRF is returned. */
  215. vrf_iter_t
  216. vrf_iterator (vrf_id_t vrf_id)
  217. {
  218. struct prefix p;
  219. struct route_node *rn;
  220. vrf_build_key (vrf_id, &p);
  221. rn = route_node_get (vrf_table, &p);
  222. if (rn->info)
  223. {
  224. /* OK, the VRF exists. */
  225. route_unlock_node (rn); /* get */
  226. return (vrf_iter_t)rn;
  227. }
  228. /* Find the next VRF. */
  229. for (rn = route_next (rn); rn; rn = route_next (rn))
  230. if (rn->info)
  231. {
  232. route_unlock_node (rn); /* next */
  233. return (vrf_iter_t)rn;
  234. }
  235. return VRF_ITER_INVALID;
  236. }
  237. /* Obtain the VRF ID from the given VRF iterator. */
  238. vrf_id_t
  239. vrf_iter2id (vrf_iter_t iter)
  240. {
  241. struct route_node *rn = (struct route_node *) iter;
  242. return (rn && rn->info) ? ((struct vrf *)rn->info)->vrf_id : VRF_DEFAULT;
  243. }
  244. /* Obtain the data pointer from the given VRF iterator. */
  245. void *
  246. vrf_iter2info (vrf_iter_t iter)
  247. {
  248. struct route_node *rn = (struct route_node *) iter;
  249. return (rn && rn->info) ? ((struct vrf *)rn->info)->info : NULL;
  250. }
  251. /* Obtain the interface list from the given VRF iterator. */
  252. struct list *
  253. vrf_iter2iflist (vrf_iter_t iter)
  254. {
  255. struct route_node *rn = (struct route_node *) iter;
  256. return (rn && rn->info) ? ((struct vrf *)rn->info)->iflist : NULL;
  257. }
  258. /* Get the data pointer of the specified VRF. If not found, create one. */
  259. void *
  260. vrf_info_get (vrf_id_t vrf_id)
  261. {
  262. struct vrf *vrf = vrf_get (vrf_id);
  263. return vrf->info;
  264. }
  265. /* Look up the data pointer of the specified VRF. */
  266. void *
  267. vrf_info_lookup (vrf_id_t vrf_id)
  268. {
  269. struct vrf *vrf = vrf_lookup (vrf_id);
  270. return vrf ? vrf->info : NULL;
  271. }
  272. /* Look up the interface list in a VRF. */
  273. struct list *
  274. vrf_iflist (vrf_id_t vrf_id)
  275. {
  276. struct vrf * vrf = vrf_lookup (vrf_id);
  277. return vrf ? vrf->iflist : NULL;
  278. }
  279. /* Get the interface list of the specified VRF. Create one if not find. */
  280. struct list *
  281. vrf_iflist_get (vrf_id_t vrf_id)
  282. {
  283. struct vrf * vrf = vrf_get (vrf_id);
  284. return vrf->iflist;
  285. }
  286. /*
  287. * VRF bit-map
  288. */
  289. #define VRF_BITMAP_NUM_OF_GROUPS 8
  290. #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP \
  291. (UINT16_MAX / VRF_BITMAP_NUM_OF_GROUPS)
  292. #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
  293. (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
  294. #define VRF_BITMAP_GROUP(_id) \
  295. ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
  296. #define VRF_BITMAP_BIT_OFFSET(_id) \
  297. ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
  298. #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) \
  299. ((_bit_offset) / CHAR_BIT)
  300. #define VRF_BITMAP_FLAG(_bit_offset) \
  301. (((u_char)1) << ((_bit_offset) % CHAR_BIT))
  302. struct vrf_bitmap
  303. {
  304. u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
  305. };
  306. vrf_bitmap_t
  307. vrf_bitmap_init (void)
  308. {
  309. return (vrf_bitmap_t) XCALLOC (MTYPE_VRF_BITMAP, sizeof (struct vrf_bitmap));
  310. }
  311. void
  312. vrf_bitmap_free (vrf_bitmap_t bmap)
  313. {
  314. struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
  315. int i;
  316. if (bmap == VRF_BITMAP_NULL)
  317. return;
  318. for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
  319. if (bm->groups[i])
  320. XFREE (MTYPE_VRF_BITMAP, bm->groups[i]);
  321. XFREE (MTYPE_VRF_BITMAP, bm);
  322. }
  323. void
  324. vrf_bitmap_set (vrf_bitmap_t bmap, vrf_id_t vrf_id)
  325. {
  326. struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
  327. u_char group = VRF_BITMAP_GROUP (vrf_id);
  328. u_char offset = VRF_BITMAP_BIT_OFFSET (vrf_id);
  329. if (bmap == VRF_BITMAP_NULL)
  330. return;
  331. if (bm->groups[group] == NULL)
  332. bm->groups[group] = XCALLOC (MTYPE_VRF_BITMAP,
  333. VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
  334. SET_FLAG (bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP (offset)],
  335. VRF_BITMAP_FLAG (offset));
  336. }
  337. void
  338. vrf_bitmap_unset (vrf_bitmap_t bmap, vrf_id_t vrf_id)
  339. {
  340. struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
  341. u_char group = VRF_BITMAP_GROUP (vrf_id);
  342. u_char offset = VRF_BITMAP_BIT_OFFSET (vrf_id);
  343. if (bmap == VRF_BITMAP_NULL || bm->groups[group] == NULL)
  344. return;
  345. UNSET_FLAG (bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP (offset)],
  346. VRF_BITMAP_FLAG (offset));
  347. }
  348. int
  349. vrf_bitmap_check (vrf_bitmap_t bmap, vrf_id_t vrf_id)
  350. {
  351. struct vrf_bitmap *bm = (struct vrf_bitmap *) bmap;
  352. u_char group = VRF_BITMAP_GROUP (vrf_id);
  353. u_char offset = VRF_BITMAP_BIT_OFFSET (vrf_id);
  354. if (bmap == VRF_BITMAP_NULL || bm->groups[group] == NULL)
  355. return 0;
  356. return CHECK_FLAG (bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP (offset)],
  357. VRF_BITMAP_FLAG (offset)) ? 1 : 0;
  358. }
  359. /* Initialize VRF module. */
  360. void
  361. vrf_init (void)
  362. {
  363. struct vrf *default_vrf;
  364. /* Allocate VRF table. */
  365. vrf_table = route_table_init ();
  366. /* The default VRF always exists. */
  367. default_vrf = vrf_get (VRF_DEFAULT);
  368. if (!default_vrf)
  369. {
  370. zlog_err ("vrf_init: failed to create the default VRF!");
  371. exit (1);
  372. }
  373. /* Set the default VRF name. */
  374. default_vrf->name = XSTRDUP (MTYPE_VRF_NAME, VRF_DEFAULT_NAME);
  375. /* Enable the default VRF. */
  376. if (!vrf_enable (default_vrf))
  377. {
  378. zlog_err ("vrf_init: failed to enable the default VRF!");
  379. exit (1);
  380. }
  381. }
  382. /* Terminate VRF module. */
  383. void
  384. vrf_terminate (void)
  385. {
  386. struct route_node *rn;
  387. struct vrf *vrf;
  388. for (rn = route_top (vrf_table); rn; rn = route_next (rn))
  389. if ((vrf = rn->info) != NULL)
  390. vrf_delete (vrf);
  391. route_table_finish (vrf_table);
  392. vrf_table = NULL;
  393. }
  394. /* Create a socket for the VRF. */
  395. int
  396. vrf_socket (int domain, int type, int protocol, vrf_id_t vrf_id)
  397. {
  398. int ret = -1;
  399. if (!vrf_is_enabled (vrf_lookup (vrf_id)))
  400. {
  401. errno = ENOSYS;
  402. return -1;
  403. }
  404. if (vrf_id == VRF_DEFAULT)
  405. ret = socket (domain, type, protocol);
  406. else
  407. errno = ENOSYS;
  408. return ret;
  409. }