]> git.proxmox.com Git - mirror_frr.git/blob - lib/table.h
Merge pull request #2884 from opensourcerouting/assorted-20180821
[mirror_frr.git] / lib / table.h
1 /*
2 * Routing Table
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * 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 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
20 */
21
22 #ifndef _ZEBRA_TABLE_H
23 #define _ZEBRA_TABLE_H
24
25 #include "memory.h"
26 #include "hash.h"
27 #include "prefix.h"
28 DECLARE_MTYPE(ROUTE_TABLE)
29 DECLARE_MTYPE(ROUTE_NODE)
30
31 /*
32 * Forward declarations.
33 */
34 struct route_node;
35 struct route_table;
36
37 /*
38 * route_table_delegate_t
39 *
40 * Function vector that can be used by a client to customize the
41 * behavior of one or more route tables.
42 */
43 typedef struct route_table_delegate_t_ route_table_delegate_t;
44
45 typedef struct route_node *(*route_table_create_node_func_t)(
46 route_table_delegate_t *, struct route_table *);
47
48 typedef void (*route_table_destroy_node_func_t)(route_table_delegate_t *,
49 struct route_table *,
50 struct route_node *);
51
52 struct route_table_delegate_t_ {
53 route_table_create_node_func_t create_node;
54 route_table_destroy_node_func_t destroy_node;
55 };
56
57 /* Routing table top structure. */
58 struct route_table {
59 struct route_node *top;
60 struct hash *hash;
61
62 /*
63 * Delegate that performs certain functions for this table.
64 */
65 route_table_delegate_t *delegate;
66 void (*cleanup)(struct route_table *, struct route_node *);
67
68 unsigned long count;
69
70 /*
71 * User data.
72 */
73 void *info;
74 };
75
76 /*
77 * node->link is really internal to the table code and should not be
78 * accessed by outside code. We don't have any writers (yay), though some
79 * readers are left to be fixed.
80 *
81 * rationale: we need to add a hash table in parallel, to speed up
82 * exact-match lookups.
83 *
84 * same really applies for node->parent, though that's less of an issue.
85 * table->link should be - and is - NEVER written by outside code
86 */
87 #ifdef FRR_COMPILING_TABLE_C
88 #define table_rdonly(x) x
89 #define table_internal(x) x
90 #else
91 #define table_rdonly(x) const x
92 #define table_internal(x) \
93 const x __attribute__( \
94 (deprecated("this should only be accessed by lib/table.c")))
95 /* table_internal is for node->link and node->lock, once we have done
96 * something about remaining accesses */
97 #endif
98
99 /* so... the problem with this is that "const" doesn't mean "readonly".
100 * It in fact may allow the compiler to optimize based on the assumption
101 * that the value doesn't change. Hence, since the only purpose of this
102 * is to aid in development, don't put the "const" in release builds.
103 *
104 * (I haven't seen this actually break, but GCC and LLVM are getting ever
105 * more aggressive in optimizing...)
106 */
107 #ifndef DEV_BUILD
108 #undef table_rdonly
109 #define table_rdonly(x) x
110 #endif
111
112 /*
113 * Macro that defines all fields in a route node.
114 */
115 #define ROUTE_NODE_FIELDS \
116 /* Actual prefix of this radix. */ \
117 struct prefix p; \
118 \
119 /* Tree link. */ \
120 struct route_table *table_rdonly(table); \
121 struct route_node *table_rdonly(parent); \
122 struct route_node *table_rdonly(link[2]); \
123 \
124 /* Lock of this radix */ \
125 unsigned int table_rdonly(lock); \
126 \
127 /* Each node of route. */ \
128 void *info; \
129 \
130 /* Aggregation. */ \
131 void *aggregate;
132
133
134 /* Each routing entry. */
135 struct route_node {
136 ROUTE_NODE_FIELDS
137
138 #define l_left link[0]
139 #define l_right link[1]
140 };
141
142 typedef struct route_table_iter_t_ route_table_iter_t;
143
144 typedef enum {
145 RT_ITER_STATE_INIT,
146 RT_ITER_STATE_ITERATING,
147 RT_ITER_STATE_PAUSED,
148 RT_ITER_STATE_DONE
149 } route_table_iter_state_t;
150
151 /*
152 * route_table_iter_t
153 *
154 * Structure that holds state for iterating over a route table.
155 */
156 struct route_table_iter_t_ {
157
158 route_table_iter_state_t state;
159
160 /*
161 * Routing table that we are iterating over. The caller must ensure
162 * that that table outlives the iterator.
163 */
164 struct route_table *table;
165
166 /*
167 * The node that the iterator is currently on.
168 */
169 struct route_node *current;
170
171 /*
172 * The last prefix that the iterator processed before it was paused.
173 */
174 struct prefix pause_prefix;
175 };
176
177 /* Prototypes. */
178 extern struct route_table *route_table_init(void);
179
180 extern struct route_table *
181 route_table_init_with_delegate(route_table_delegate_t *delegate);
182
183 extern route_table_delegate_t *route_table_get_default_delegate(void);
184
185 extern void route_table_finish(struct route_table *table);
186 extern struct route_node *route_top(struct route_table *table);
187 extern struct route_node *route_next(struct route_node *node);
188 extern struct route_node *route_next_until(struct route_node *node,
189 const struct route_node *limit);
190 extern struct route_node *route_node_get(struct route_table *const table,
191 union prefixconstptr pu);
192 extern struct route_node *route_node_lookup(const struct route_table *table,
193 union prefixconstptr pu);
194 extern struct route_node *
195 route_node_lookup_maynull(const struct route_table *table,
196 union prefixconstptr pu);
197 extern struct route_node *route_node_match(const struct route_table *table,
198 union prefixconstptr pu);
199 extern struct route_node *route_node_match_ipv4(const struct route_table *table,
200 const struct in_addr *addr);
201 extern struct route_node *route_node_match_ipv6(const struct route_table *table,
202 const struct in6_addr *addr);
203
204 extern unsigned long route_table_count(const struct route_table *table);
205
206 extern struct route_node *route_node_create(route_table_delegate_t *delegate,
207 struct route_table *table);
208 extern void route_node_delete(struct route_node *node);
209 extern void route_node_destroy(route_table_delegate_t *delegate,
210 struct route_table *table,
211 struct route_node *node);
212
213 extern struct route_node *route_table_get_next(const struct route_table *table,
214 union prefixconstptr pu);
215 extern int route_table_prefix_iter_cmp(const struct prefix *p1,
216 const struct prefix *p2);
217
218 /*
219 * Iterator functions.
220 */
221 extern void route_table_iter_init(route_table_iter_t *iter,
222 struct route_table *table);
223 extern void route_table_iter_pause(route_table_iter_t *iter);
224 extern void route_table_iter_cleanup(route_table_iter_t *iter);
225
226 /*
227 * Inline functions.
228 */
229
230 /* Lock node. */
231 static inline struct route_node *route_lock_node(struct route_node *node)
232 {
233 (*(unsigned *)&node->lock)++;
234 return node;
235 }
236
237 /* Unlock node. */
238 static inline void route_unlock_node(struct route_node *node)
239 {
240 assert(node->lock > 0);
241 (*(unsigned *)&node->lock)--;
242
243 if (node->lock == 0)
244 route_node_delete(node);
245 }
246
247 /*
248 * route_table_iter_next
249 *
250 * Get the next node in the tree.
251 */
252 static inline struct route_node *route_table_iter_next(route_table_iter_t *iter)
253 {
254 struct route_node *node;
255
256 switch (iter->state) {
257
258 case RT_ITER_STATE_INIT:
259
260 /*
261 * We're just starting the iteration.
262 */
263 node = route_top(iter->table);
264 break;
265
266 case RT_ITER_STATE_ITERATING:
267 node = route_next(iter->current);
268 break;
269
270 case RT_ITER_STATE_PAUSED:
271
272 /*
273 * Start with the node following pause_prefix.
274 */
275 node = route_table_get_next(iter->table, &iter->pause_prefix);
276 break;
277
278 case RT_ITER_STATE_DONE:
279 return NULL;
280
281 default:
282 assert(0);
283 }
284
285 iter->current = node;
286 if (node)
287 iter->state = RT_ITER_STATE_ITERATING;
288 else
289 iter->state = RT_ITER_STATE_DONE;
290
291 return node;
292 }
293
294 /*
295 * route_table_iter_is_done
296 *
297 * Returns TRUE if the iteration is complete.
298 */
299 static inline int route_table_iter_is_done(route_table_iter_t *iter)
300 {
301 return iter->state == RT_ITER_STATE_DONE;
302 }
303
304 /*
305 * route_table_iter_started
306 *
307 * Returns TRUE if this iterator has started iterating over the tree.
308 */
309 static inline int route_table_iter_started(route_table_iter_t *iter)
310 {
311 return iter->state != RT_ITER_STATE_INIT;
312 }
313
314 #endif /* _ZEBRA_TABLE_H */