bgp_table.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* BGP routing table
  2. Copyright (C) 1998, 2001 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "prefix.h"
  18. #include "memory.h"
  19. #include "sockunion.h"
  20. #include "vty.h"
  21. #include "bgpd/bgpd.h"
  22. #include "bgpd/bgp_table.h"
  23. void
  24. bgp_table_lock (struct bgp_table *rt)
  25. {
  26. rt->lock++;
  27. }
  28. void
  29. bgp_table_unlock (struct bgp_table *rt)
  30. {
  31. assert (rt->lock > 0);
  32. rt->lock--;
  33. if (rt->lock != 0)
  34. {
  35. return;
  36. }
  37. route_table_finish (rt->route_table);
  38. rt->route_table = NULL;
  39. if (rt->owner)
  40. {
  41. peer_unlock (rt->owner);
  42. rt->owner = NULL;
  43. }
  44. XFREE (MTYPE_BGP_TABLE, rt);
  45. }
  46. void
  47. bgp_table_finish (struct bgp_table **rt)
  48. {
  49. if (*rt != NULL)
  50. {
  51. bgp_table_unlock(*rt);
  52. *rt = NULL;
  53. }
  54. }
  55. /*
  56. * bgp_node_create
  57. */
  58. static struct route_node *
  59. bgp_node_create (route_table_delegate_t *delegate, struct route_table *table)
  60. {
  61. struct bgp_node *node;
  62. node = XCALLOC (MTYPE_BGP_NODE, sizeof (struct bgp_node));
  63. return bgp_node_to_rnode (node);
  64. }
  65. /*
  66. * bgp_node_destroy
  67. */
  68. static void
  69. bgp_node_destroy (route_table_delegate_t *delegate,
  70. struct route_table *table, struct route_node *node)
  71. {
  72. struct bgp_node *bgp_node;
  73. bgp_node = bgp_node_from_rnode (node);
  74. XFREE (MTYPE_BGP_NODE, bgp_node);
  75. }
  76. /*
  77. * Function vector to customize the behavior of the route table
  78. * library for BGP route tables.
  79. */
  80. route_table_delegate_t bgp_table_delegate = {
  81. .create_node = bgp_node_create,
  82. .destroy_node = bgp_node_destroy
  83. };
  84. /*
  85. * bgp_table_init
  86. */
  87. struct bgp_table *
  88. bgp_table_init (afi_t afi, safi_t safi)
  89. {
  90. struct bgp_table *rt;
  91. rt = XCALLOC (MTYPE_BGP_TABLE, sizeof (struct bgp_table));
  92. rt->route_table = route_table_init_with_delegate (&bgp_table_delegate);
  93. /*
  94. * Set up back pointer to bgp_table.
  95. */
  96. rt->route_table->info = rt;
  97. bgp_table_lock (rt);
  98. rt->type = BGP_TABLE_MAIN;
  99. rt->afi = afi;
  100. rt->safi = safi;
  101. return rt;
  102. }