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