3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _ZEBRA_TABLE_H
23 #define _ZEBRA_TABLE_H
27 DECLARE_MTYPE(ROUTE_TABLE
)
28 DECLARE_MTYPE(ROUTE_NODE
)
31 * Forward declarations.
37 * route_table_delegate_t
39 * Function vector that can be used by a client to customize the
40 * behavior of one or more route tables.
42 typedef struct route_table_delegate_t_ route_table_delegate_t
;
44 typedef struct route_node
*(*route_table_create_node_func_t
)(
45 route_table_delegate_t
*, struct route_table
*);
47 typedef void (*route_table_destroy_node_func_t
)(route_table_delegate_t
*,
51 struct route_table_delegate_t_
{
52 route_table_create_node_func_t create_node
;
53 route_table_destroy_node_func_t destroy_node
;
56 /* Routing table top structure. */
58 struct route_node
*top
;
62 * Delegate that performs certain functions for this table.
64 route_table_delegate_t
*delegate
;
65 void (*cleanup
)(struct route_table
*, struct route_node
*);
76 * node->link is really internal to the table code and should not be
77 * accessed by outside code. We don't have any writers (yay), though some
78 * readers are left to be fixed.
80 * rationale: we need to add a hash table in parallel, to speed up
81 * exact-match lookups.
83 * same really applies for node->parent, though that's less of an issue.
84 * table->link should be - and is - NEVER written by outside code
86 #ifdef FRR_COMPILING_TABLE_C
87 #define table_rdonly(x) x
88 #define table_internal(x) x
90 #define table_rdonly(x) const x
91 #define table_internal(x) \
92 const x __attribute__( \
93 (deprecated("this should only be accessed by lib/table.c")))
94 /* table_internal is for node->link and node->lock, once we have done
95 * something about remaining accesses */
98 /* so... the problem with this is that "const" doesn't mean "readonly".
99 * It in fact may allow the compiler to optimize based on the assumption
100 * that the value doesn't change. Hence, since the only purpose of this
101 * is to aid in development, don't put the "const" in release builds.
103 * (I haven't seen this actually break, but GCC and LLVM are getting ever
104 * more aggressive in optimizing...)
108 #define table_rdonly(x) x
112 * Macro that defines all fields in a route node.
114 #define ROUTE_NODE_FIELDS \
115 /* Actual prefix of this radix. */ \
119 struct route_table *table_rdonly(table); \
120 struct route_node *table_rdonly(parent); \
121 struct route_node *table_rdonly(link[2]); \
123 /* Lock of this radix */ \
124 unsigned int table_rdonly(lock); \
126 /* Each node of route. */ \
133 /* Each routing entry. */
137 #define l_left link[0]
138 #define l_right link[1]
141 typedef struct route_table_iter_t_ route_table_iter_t
;
145 RT_ITER_STATE_ITERATING
,
146 RT_ITER_STATE_PAUSED
,
148 } route_table_iter_state_t
;
153 * Structure that holds state for iterating over a route table.
155 struct route_table_iter_t_
{
157 route_table_iter_state_t state
;
160 * Routing table that we are iterating over. The caller must ensure
161 * that that table outlives the iterator.
163 struct route_table
*table
;
166 * The node that the iterator is currently on.
168 struct route_node
*current
;
171 * The last prefix that the iterator processed before it was paused.
173 struct prefix pause_prefix
;
177 extern struct route_table
*route_table_init(void);
179 extern struct route_table
*
180 route_table_init_with_delegate(route_table_delegate_t
*);
182 extern route_table_delegate_t
*route_table_get_default_delegate(void);
184 extern void route_table_finish(struct route_table
*);
185 extern struct route_node
*route_top(struct route_table
*);
186 extern struct route_node
*route_next(struct route_node
*);
187 extern struct route_node
*route_next_until(struct route_node
*,
188 const struct route_node
*);
189 extern struct route_node
*route_node_get(struct route_table
*const,
190 union prefixconstptr
);
191 extern struct route_node
*route_node_lookup(const struct route_table
*,
192 union prefixconstptr
);
193 extern struct route_node
*route_node_lookup_maynull(const struct route_table
*,
194 union prefixconstptr
);
195 extern struct route_node
*route_node_match(const struct route_table
*,
196 union prefixconstptr
);
197 extern struct route_node
*route_node_match_ipv4(const struct route_table
*,
198 const struct in_addr
*);
199 extern struct route_node
*route_node_match_ipv6(const struct route_table
*,
200 const struct in6_addr
*);
202 extern unsigned long route_table_count(const struct route_table
*);
204 extern struct route_node
*route_node_create(route_table_delegate_t
*,
205 struct route_table
*);
206 extern void route_node_delete(struct route_node
*);
207 extern void route_node_destroy(route_table_delegate_t
*, struct route_table
*,
208 struct route_node
*);
210 extern struct route_node
*route_table_get_next(const struct route_table
*table
,
211 union prefixconstptr pu
);
212 extern int route_table_prefix_iter_cmp(const struct prefix
*p1
,
213 const struct prefix
*p2
);
216 * Iterator functions.
218 extern void route_table_iter_init(route_table_iter_t
*iter
,
219 struct route_table
*table
);
220 extern void route_table_iter_pause(route_table_iter_t
*iter
);
221 extern void route_table_iter_cleanup(route_table_iter_t
*iter
);
228 static inline struct route_node
*route_lock_node(struct route_node
*node
)
230 (*(unsigned *)&node
->lock
)++;
235 static inline void route_unlock_node(struct route_node
*node
)
237 assert(node
->lock
> 0);
238 (*(unsigned *)&node
->lock
)--;
241 route_node_delete(node
);
245 * route_table_iter_next
247 * Get the next node in the tree.
249 static inline struct route_node
*route_table_iter_next(route_table_iter_t
*iter
)
251 struct route_node
*node
;
253 switch (iter
->state
) {
255 case RT_ITER_STATE_INIT
:
258 * We're just starting the iteration.
260 node
= route_top(iter
->table
);
263 case RT_ITER_STATE_ITERATING
:
264 node
= route_next(iter
->current
);
267 case RT_ITER_STATE_PAUSED
:
270 * Start with the node following pause_prefix.
272 node
= route_table_get_next(iter
->table
, &iter
->pause_prefix
);
275 case RT_ITER_STATE_DONE
:
282 iter
->current
= node
;
284 iter
->state
= RT_ITER_STATE_ITERATING
;
286 iter
->state
= RT_ITER_STATE_DONE
;
292 * route_table_iter_is_done
294 * Returns TRUE if the iteration is complete.
296 static inline int route_table_iter_is_done(route_table_iter_t
*iter
)
298 return iter
->state
== RT_ITER_STATE_DONE
;
302 * route_table_iter_started
304 * Returns TRUE if this iterator has started iterating over the tree.
306 static inline int route_table_iter_started(route_table_iter_t
*iter
)
308 return iter
->state
!= RT_ITER_STATE_INIT
;
311 #endif /* _ZEBRA_TABLE_H */