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
34 DECLARE_MTYPE(ROUTE_NODE
)
37 * Forward declarations.
43 * route_table_delegate_t
45 * Function vector that can be used by a client to customize the
46 * behavior of one or more route tables.
48 typedef const struct route_table_delegate_t_ route_table_delegate_t
;
50 typedef struct route_node
*(*route_table_create_node_func_t
)(
51 route_table_delegate_t
*, struct route_table
*);
53 typedef void (*route_table_destroy_node_func_t
)(route_table_delegate_t
*,
57 struct route_table_delegate_t_
{
58 route_table_create_node_func_t create_node
;
59 route_table_destroy_node_func_t destroy_node
;
62 PREDECL_HASH(rn_hash_node
)
64 /* Routing table top structure. */
66 struct route_node
*top
;
67 struct rn_hash_node_head hash
;
70 * Delegate that performs certain functions for this table.
72 route_table_delegate_t
*delegate
;
73 void (*cleanup
)(struct route_table
*, struct route_node
*);
84 * node->link is really internal to the table code and should not be
85 * accessed by outside code. We don't have any writers (yay), though some
86 * readers are left to be fixed.
88 * rationale: we need to add a hash table in parallel, to speed up
89 * exact-match lookups.
91 * same really applies for node->parent, though that's less of an issue.
92 * table->link should be - and is - NEVER written by outside code
94 #ifdef FRR_COMPILING_TABLE_C
95 #define table_rdonly(x) x
96 #define table_internal(x) x
98 #define table_rdonly(x) const x
99 #define table_internal(x) \
100 const x __attribute__( \
101 (deprecated("this should only be accessed by lib/table.c")))
102 /* table_internal is for node->link and node->lock, once we have done
103 * something about remaining accesses */
106 /* so... the problem with this is that "const" doesn't mean "readonly".
107 * It in fact may allow the compiler to optimize based on the assumption
108 * that the value doesn't change. Hence, since the only purpose of this
109 * is to aid in development, don't put the "const" in release builds.
111 * (I haven't seen this actually break, but GCC and LLVM are getting ever
112 * more aggressive in optimizing...)
116 #define table_rdonly(x) x
120 * Macro that defines all fields in a route node.
122 #define ROUTE_NODE_FIELDS \
123 /* Actual prefix of this radix. */ \
127 struct route_table *table_rdonly(table); \
128 struct route_node *table_rdonly(parent); \
129 struct route_node *table_rdonly(link[2]); \
131 /* Lock of this radix */ \
132 unsigned int table_rdonly(lock); \
134 struct rn_hash_node_item nodehash; \
135 /* Each node of route. */ \
139 /* Each routing entry. */
143 #define l_left link[0]
144 #define l_right link[1]
147 typedef struct route_table_iter_t_ route_table_iter_t
;
151 RT_ITER_STATE_ITERATING
,
152 RT_ITER_STATE_PAUSED
,
154 } route_table_iter_state_t
;
159 * Structure that holds state for iterating over a route table.
161 struct route_table_iter_t_
{
163 route_table_iter_state_t state
;
166 * Routing table that we are iterating over. The caller must ensure
167 * that that table outlives the iterator.
169 struct route_table
*table
;
172 * The node that the iterator is currently on.
174 struct route_node
*current
;
177 * The last prefix that the iterator processed before it was paused.
179 struct prefix pause_prefix
;
183 extern struct route_table
*route_table_init(void);
185 extern struct route_table
*
186 route_table_init_with_delegate(route_table_delegate_t
*delegate
);
188 extern route_table_delegate_t
*route_table_get_default_delegate(void);
190 static inline void *route_table_get_info(struct route_table
*table
)
195 static inline void route_table_set_info(struct route_table
*table
, void *d
)
200 /* ext_pure => extern __attribute__((pure))
201 * does not modify memory (but depends on mem), allows compiler to optimize
204 extern void route_table_finish(struct route_table
*table
);
205 ext_pure
struct route_node
*route_top(struct route_table
*table
);
206 ext_pure
struct route_node
*route_next(struct route_node
*node
);
207 ext_pure
struct route_node
*route_next_until(struct route_node
*node
,
208 const struct route_node
*limit
);
209 extern struct route_node
*route_node_get(struct route_table
*table
,
210 union prefixconstptr pu
);
211 ext_pure
struct route_node
*route_node_lookup(struct route_table
*table
,
212 union prefixconstptr pu
);
213 ext_pure
struct route_node
*route_node_lookup_maynull(struct route_table
*table
,
214 union prefixconstptr pu
);
215 ext_pure
struct route_node
*route_node_match(struct route_table
*table
,
216 union prefixconstptr pu
);
217 ext_pure
struct route_node
*route_node_match_ipv4(struct route_table
*table
,
218 const struct in_addr
*addr
);
219 ext_pure
struct route_node
*route_node_match_ipv6(struct route_table
*table
,
220 const struct in6_addr
*addr
);
222 ext_pure
unsigned long route_table_count(struct route_table
*table
);
224 extern struct route_node
*route_node_create(route_table_delegate_t
*delegate
,
225 struct route_table
*table
);
226 extern void route_node_delete(struct route_node
*node
);
227 extern void route_node_destroy(route_table_delegate_t
*delegate
,
228 struct route_table
*table
,
229 struct route_node
*node
);
231 ext_pure
struct route_node
*route_table_get_next(struct route_table
*table
,
232 union prefixconstptr pu
);
233 ext_pure
int route_table_prefix_iter_cmp(const struct prefix
*p1
,
234 const struct prefix
*p2
);
237 * Iterator functions.
239 extern void route_table_iter_init(route_table_iter_t
*iter
,
240 struct route_table
*table
);
241 extern void route_table_iter_pause(route_table_iter_t
*iter
);
242 extern void route_table_iter_cleanup(route_table_iter_t
*iter
);
249 static inline struct route_node
*route_lock_node(struct route_node
*node
)
251 (*(unsigned *)&node
->lock
)++;
256 static inline void route_unlock_node(struct route_node
*node
)
258 assert(node
->lock
> 0);
259 (*(unsigned *)&node
->lock
)--;
262 route_node_delete(node
);
266 * route_table_iter_next
268 * Get the next node in the tree.
270 static inline struct route_node
*route_table_iter_next(route_table_iter_t
*iter
)
272 struct route_node
*node
;
274 switch (iter
->state
) {
276 case RT_ITER_STATE_INIT
:
279 * We're just starting the iteration.
281 node
= route_top(iter
->table
);
284 case RT_ITER_STATE_ITERATING
:
285 node
= route_next(iter
->current
);
288 case RT_ITER_STATE_PAUSED
:
291 * Start with the node following pause_prefix.
293 node
= route_table_get_next(iter
->table
, &iter
->pause_prefix
);
296 case RT_ITER_STATE_DONE
:
300 /* Suppress uninitialized variable warning */
305 iter
->current
= node
;
307 iter
->state
= RT_ITER_STATE_ITERATING
;
309 iter
->state
= RT_ITER_STATE_DONE
;
315 * route_table_iter_is_done
317 * Returns true if the iteration is complete.
319 static inline int route_table_iter_is_done(route_table_iter_t
*iter
)
321 return iter
->state
== RT_ITER_STATE_DONE
;
325 * route_table_iter_started
327 * Returns true if this iterator has started iterating over the tree.
329 static inline int route_table_iter_started(route_table_iter_t
*iter
)
331 return iter
->state
!= RT_ITER_STATE_INIT
;
338 #endif /* _ZEBRA_TABLE_H */