bgp_table.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #ifndef _QUAGGA_BGP_TABLE_H
  17. #define _QUAGGA_BGP_TABLE_H
  18. #include "table.h"
  19. typedef enum
  20. {
  21. BGP_TABLE_MAIN,
  22. BGP_TABLE_RSCLIENT,
  23. } bgp_table_t;
  24. struct bgp_table
  25. {
  26. bgp_table_t type;
  27. /* afi/safi of this table */
  28. afi_t afi;
  29. safi_t safi;
  30. int lock;
  31. /* The owner of this 'bgp_table' structure. */
  32. struct peer *owner;
  33. struct route_table *route_table;
  34. };
  35. struct bgp_node
  36. {
  37. /*
  38. * CAUTION
  39. *
  40. * These fields must be the very first fields in this structure.
  41. *
  42. * @see bgp_node_to_rnode
  43. * @see bgp_node_from_rnode
  44. */
  45. ROUTE_NODE_FIELDS;
  46. struct bgp_adj_out *adj_out;
  47. struct bgp_adj_in *adj_in;
  48. struct bgp_node *prn;
  49. u_char flags;
  50. #define BGP_NODE_PROCESS_SCHEDULED (1 << 0)
  51. };
  52. /*
  53. * bgp_table_iter_t
  54. *
  55. * Structure that holds state for iterating over a bgp table.
  56. */
  57. typedef struct bgp_table_iter_t_
  58. {
  59. struct bgp_table *table;
  60. route_table_iter_t rt_iter;
  61. } bgp_table_iter_t;
  62. extern struct bgp_table *bgp_table_init (afi_t, safi_t);
  63. extern void bgp_table_lock (struct bgp_table *);
  64. extern void bgp_table_unlock (struct bgp_table *);
  65. extern void bgp_table_finish (struct bgp_table **);
  66. /*
  67. * bgp_node_from_rnode
  68. *
  69. * Returns the bgp_node structure corresponding to a route_node.
  70. */
  71. static inline struct bgp_node *
  72. bgp_node_from_rnode (struct route_node *rnode)
  73. {
  74. return (struct bgp_node *) rnode;
  75. }
  76. /*
  77. * bgp_node_to_rnode
  78. *
  79. * Returns the route_node structure corresponding to a bgp_node.
  80. */
  81. static inline struct route_node *
  82. bgp_node_to_rnode (struct bgp_node *node)
  83. {
  84. return (struct route_node *) node;
  85. }
  86. /*
  87. * bgp_node_table
  88. *
  89. * Returns the bgp_table that the given node is in.
  90. */
  91. static inline struct bgp_table *
  92. bgp_node_table (struct bgp_node *node)
  93. {
  94. return bgp_node_to_rnode (node)->table->info;
  95. }
  96. /*
  97. * bgp_node_info
  98. *
  99. * Returns the 'info' pointer corresponding to a bgp node.
  100. */
  101. static inline void *
  102. bgp_node_info (const struct bgp_node *node)
  103. {
  104. return node->info;
  105. }
  106. /*
  107. * bgp_node_set_info
  108. */
  109. static inline void
  110. bgp_node_set_info (struct bgp_node *node, void *info)
  111. {
  112. node->info = info;
  113. }
  114. /*
  115. * bgp_node_prefix
  116. */
  117. static inline struct prefix *
  118. bgp_node_prefix (struct bgp_node *node)
  119. {
  120. return &node->p;
  121. }
  122. /*
  123. * bgp_node_prefixlen
  124. */
  125. static inline u_char
  126. bgp_node_prefixlen (struct bgp_node *node)
  127. {
  128. return bgp_node_prefix (node)->prefixlen;
  129. }
  130. /*
  131. * bgp_node_parent_nolock
  132. *
  133. * Gets the parent node of the given node without locking it.
  134. */
  135. static inline struct bgp_node *
  136. bgp_node_parent_nolock (struct bgp_node *node)
  137. {
  138. return bgp_node_from_rnode (node->parent);
  139. }
  140. /*
  141. * bgp_unlock_node
  142. */
  143. static inline void
  144. bgp_unlock_node (struct bgp_node *node)
  145. {
  146. route_unlock_node (bgp_node_to_rnode (node));
  147. }
  148. /*
  149. * bgp_table_top_nolock
  150. *
  151. * Gets the top node in the table without locking it.
  152. *
  153. * @see bgp_table_top
  154. */
  155. static inline struct bgp_node *
  156. bgp_table_top_nolock (const struct bgp_table *const table)
  157. {
  158. return bgp_node_from_rnode (table->route_table->top);
  159. }
  160. /*
  161. * bgp_table_top
  162. */
  163. static inline struct bgp_node *
  164. bgp_table_top (const struct bgp_table *const table)
  165. {
  166. return bgp_node_from_rnode (route_top (table->route_table));
  167. }
  168. /*
  169. * bgp_route_next
  170. */
  171. static inline struct bgp_node *
  172. bgp_route_next (struct bgp_node *node)
  173. {
  174. return bgp_node_from_rnode (route_next (bgp_node_to_rnode (node)));
  175. }
  176. /*
  177. * bgp_route_next_until
  178. */
  179. static inline struct bgp_node *
  180. bgp_route_next_until (struct bgp_node *node, struct bgp_node *limit)
  181. {
  182. struct route_node *rnode;
  183. rnode = route_next_until (bgp_node_to_rnode (node),
  184. bgp_node_to_rnode (limit));
  185. return bgp_node_from_rnode (rnode);
  186. }
  187. /*
  188. * bgp_node_get
  189. */
  190. static inline struct bgp_node *
  191. bgp_node_get (struct bgp_table *const table, struct prefix *p)
  192. {
  193. return bgp_node_from_rnode (route_node_get (table->route_table, p));
  194. }
  195. /*
  196. * bgp_node_lookup
  197. */
  198. static inline struct bgp_node *
  199. bgp_node_lookup (const struct bgp_table *const table, struct prefix *p)
  200. {
  201. return bgp_node_from_rnode (route_node_lookup (table->route_table, p));
  202. }
  203. /*
  204. * bgp_lock_node
  205. */
  206. static inline struct bgp_node *
  207. bgp_lock_node (struct bgp_node *node)
  208. {
  209. return bgp_node_from_rnode (route_lock_node (bgp_node_to_rnode (node)));
  210. }
  211. /*
  212. * bgp_node_match
  213. */
  214. static inline struct bgp_node *
  215. bgp_node_match (const struct bgp_table *table, struct prefix *p)
  216. {
  217. return bgp_node_from_rnode (route_node_match (table->route_table, p));
  218. }
  219. /*
  220. * bgp_node_match_ipv4
  221. */
  222. static inline struct bgp_node *
  223. bgp_node_match_ipv4 (const struct bgp_table *table, struct in_addr *addr)
  224. {
  225. return bgp_node_from_rnode (route_node_match_ipv4 (table->route_table,
  226. addr));
  227. }
  228. #ifdef HAVE_IPV6
  229. /*
  230. * bgp_node_match_ipv6
  231. */
  232. static inline struct bgp_node *
  233. bgp_node_match_ipv6 (const struct bgp_table *table, struct in6_addr *addr)
  234. {
  235. return bgp_node_from_rnode (route_node_match_ipv6 (table->route_table,
  236. addr));
  237. }
  238. #endif /* HAVE_IPV6 */
  239. static inline unsigned long
  240. bgp_table_count (const struct bgp_table *const table)
  241. {
  242. return route_table_count (table->route_table);
  243. }
  244. /*
  245. * bgp_table_get_next
  246. */
  247. static inline struct bgp_node *
  248. bgp_table_get_next (const struct bgp_table *table, struct prefix *p)
  249. {
  250. return bgp_node_from_rnode (route_table_get_next (table->route_table, p));
  251. }
  252. /*
  253. * bgp_table_iter_init
  254. */
  255. static inline void
  256. bgp_table_iter_init (bgp_table_iter_t * iter, struct bgp_table *table)
  257. {
  258. bgp_table_lock (table);
  259. iter->table = table;
  260. route_table_iter_init (&iter->rt_iter, table->route_table);
  261. }
  262. /*
  263. * bgp_table_iter_next
  264. */
  265. static inline struct bgp_node *
  266. bgp_table_iter_next (bgp_table_iter_t * iter)
  267. {
  268. return bgp_node_from_rnode (route_table_iter_next (&iter->rt_iter));
  269. }
  270. /*
  271. * bgp_table_iter_cleanup
  272. */
  273. static inline void
  274. bgp_table_iter_cleanup (bgp_table_iter_t * iter)
  275. {
  276. route_table_iter_cleanup (&iter->rt_iter);
  277. bgp_table_unlock (iter->table);
  278. iter->table = NULL;
  279. }
  280. /*
  281. * bgp_table_iter_pause
  282. */
  283. static inline void
  284. bgp_table_iter_pause (bgp_table_iter_t * iter)
  285. {
  286. route_table_iter_pause (&iter->rt_iter);
  287. }
  288. /*
  289. * bgp_table_iter_is_done
  290. */
  291. static inline int
  292. bgp_table_iter_is_done (bgp_table_iter_t * iter)
  293. {
  294. return route_table_iter_is_done (&iter->rt_iter);
  295. }
  296. /*
  297. * bgp_table_iter_started
  298. */
  299. static inline int
  300. bgp_table_iter_started (bgp_table_iter_t * iter)
  301. {
  302. return route_table_iter_started (&iter->rt_iter);
  303. }
  304. #endif /* _QUAGGA_BGP_TABLE_H */