]> git.proxmox.com Git - mirror_frr.git/blame - lib/table.h
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[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"
4a1ab8e4 28DECLARE_MTYPE(ROUTE_TABLE)
0964ad9c 29DECLARE_MTYPE(ROUTE_NODE)
4a1ab8e4 30
f9c1b7bb
AS
31/*
32 * Forward declarations.
33 */
34struct route_node;
35struct 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 */
43typedef struct route_table_delegate_t_ route_table_delegate_t;
44
d62a17ae 45typedef struct route_node *(*route_table_create_node_func_t)(
46 route_table_delegate_t *, struct route_table *);
f9c1b7bb 47
d62a17ae 48typedef void (*route_table_destroy_node_func_t)(route_table_delegate_t *,
49 struct route_table *,
50 struct route_node *);
f9c1b7bb 51
d62a17ae 52struct route_table_delegate_t_ {
53 route_table_create_node_func_t create_node;
54 route_table_destroy_node_func_t destroy_node;
f9c1b7bb
AS
55};
56
718e3744 57/* Routing table top structure. */
d62a17ae 58struct 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;
718e3744 74};
75
4cb260c3
DL
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
d62a17ae 92#define table_internal(x) \
93 const x __attribute__( \
94 (deprecated("this should only be accessed by lib/table.c")))
4cb260c3
DL
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
f9c1b7bb
AS
112/*
113 * Macro that defines all fields in a route node.
114 */
d62a17ae 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; \
f9c1b7bb
AS
129
130
718e3744 131/* Each routing entry. */
d62a17ae 132struct route_node {
133 ROUTE_NODE_FIELDS
718e3744 134
718e3744 135#define l_left link[0]
136#define l_right link[1]
718e3744 137};
138
28971c8c
AS
139typedef struct route_table_iter_t_ route_table_iter_t;
140
d62a17ae 141typedef enum {
142 RT_ITER_STATE_INIT,
143 RT_ITER_STATE_ITERATING,
144 RT_ITER_STATE_PAUSED,
145 RT_ITER_STATE_DONE
28971c8c
AS
146} route_table_iter_state_t;
147
148/*
149 * route_table_iter_t
d62a17ae 150 *
28971c8c
AS
151 * Structure that holds state for iterating over a route table.
152 */
d62a17ae 153struct route_table_iter_t_ {
28971c8c 154
d62a17ae 155 route_table_iter_state_t state;
28971c8c 156
d62a17ae 157 /*
158 * Routing table that we are iterating over. The caller must ensure
159 * that that table outlives the iterator.
160 */
161 struct route_table *table;
28971c8c 162
d62a17ae 163 /*
164 * The node that the iterator is currently on.
165 */
166 struct route_node *current;
28971c8c 167
d62a17ae 168 /*
169 * The last prefix that the iterator processed before it was paused.
170 */
171 struct prefix pause_prefix;
28971c8c
AS
172};
173
718e3744 174/* Prototypes. */
d62a17ae 175extern struct route_table *route_table_init(void);
f9c1b7bb
AS
176
177extern struct route_table *
e0700290 178route_table_init_with_delegate(route_table_delegate_t *delegate);
d62a17ae 179
180extern route_table_delegate_t *route_table_get_default_delegate(void);
181
6ca30e9e
DS
182static inline void *route_table_get_info(struct route_table *table)
183{
184 return table->info;
185}
186
187static inline void route_table_set_info(struct route_table *table, void *d)
188{
189 table->info = d;
190}
191
e0700290
DS
192extern void route_table_finish(struct route_table *table);
193extern struct route_node *route_top(struct route_table *table);
194extern struct route_node *route_next(struct route_node *node);
195extern struct route_node *route_next_until(struct route_node *node,
196 const struct route_node *limit);
197extern struct route_node *route_node_get(struct route_table *const table,
198 union prefixconstptr pu);
199extern struct route_node *route_node_lookup(const struct route_table *table,
200 union prefixconstptr pu);
201extern struct route_node *
202route_node_lookup_maynull(const struct route_table *table,
203 union prefixconstptr pu);
204extern struct route_node *route_node_match(const struct route_table *table,
205 union prefixconstptr pu);
206extern struct route_node *route_node_match_ipv4(const struct route_table *table,
207 const struct in_addr *addr);
208extern struct route_node *route_node_match_ipv6(const struct route_table *table,
209 const struct in6_addr *addr);
210
211extern unsigned long route_table_count(const struct route_table *table);
212
213extern struct route_node *route_node_create(route_table_delegate_t *delegate,
214 struct route_table *table);
215extern void route_node_delete(struct route_node *node);
216extern void route_node_destroy(route_table_delegate_t *delegate,
217 struct route_table *table,
218 struct route_node *node);
d62a17ae 219
220extern struct route_node *route_table_get_next(const struct route_table *table,
221 union prefixconstptr pu);
222extern int route_table_prefix_iter_cmp(const struct prefix *p1,
223 const struct prefix *p2);
28971c8c
AS
224
225/*
226 * Iterator functions.
227 */
d62a17ae 228extern void route_table_iter_init(route_table_iter_t *iter,
229 struct route_table *table);
230extern void route_table_iter_pause(route_table_iter_t *iter);
231extern void route_table_iter_cleanup(route_table_iter_t *iter);
28971c8c
AS
232
233/*
234 * Inline functions.
235 */
236
01dccc0b
JB
237/* Lock node. */
238static inline struct route_node *route_lock_node(struct route_node *node)
239{
acf3a851 240 (*(unsigned *)&node->lock)++;
01dccc0b
JB
241 return node;
242}
243
244/* Unlock node. */
0e70e6c8 245static inline void route_unlock_node(struct route_node *node)
01dccc0b
JB
246{
247 assert(node->lock > 0);
acf3a851 248 (*(unsigned *)&node->lock)--;
01dccc0b 249
0e70e6c8 250 if (node->lock == 0)
01dccc0b
JB
251 route_node_delete(node);
252}
253
28971c8c
AS
254/*
255 * route_table_iter_next
256 *
257 * Get the next node in the tree.
258 */
d62a17ae 259static inline struct route_node *route_table_iter_next(route_table_iter_t *iter)
28971c8c 260{
d62a17ae 261 struct route_node *node;
28971c8c 262
d62a17ae 263 switch (iter->state) {
28971c8c 264
d62a17ae 265 case RT_ITER_STATE_INIT:
28971c8c 266
d62a17ae 267 /*
268 * We're just starting the iteration.
269 */
270 node = route_top(iter->table);
271 break;
28971c8c 272
d62a17ae 273 case RT_ITER_STATE_ITERATING:
274 node = route_next(iter->current);
275 break;
28971c8c 276
d62a17ae 277 case RT_ITER_STATE_PAUSED:
28971c8c 278
d62a17ae 279 /*
280 * Start with the node following pause_prefix.
281 */
282 node = route_table_get_next(iter->table, &iter->pause_prefix);
283 break;
28971c8c 284
d62a17ae 285 case RT_ITER_STATE_DONE:
286 return NULL;
28971c8c 287
d62a17ae 288 default:
289 assert(0);
290 }
28971c8c 291
d62a17ae 292 iter->current = node;
293 if (node)
294 iter->state = RT_ITER_STATE_ITERATING;
295 else
296 iter->state = RT_ITER_STATE_DONE;
28971c8c 297
d62a17ae 298 return node;
28971c8c
AS
299}
300
301/*
302 * route_table_iter_is_done
303 *
304 * Returns TRUE if the iteration is complete.
305 */
d62a17ae 306static inline int route_table_iter_is_done(route_table_iter_t *iter)
28971c8c 307{
d62a17ae 308 return iter->state == RT_ITER_STATE_DONE;
28971c8c
AS
309}
310
311/*
312 * route_table_iter_started
313 *
314 * Returns TRUE if this iterator has started iterating over the tree.
315 */
d62a17ae 316static inline int route_table_iter_started(route_table_iter_t *iter)
28971c8c 317{
d62a17ae 318 return iter->state != RT_ITER_STATE_INIT;
28971c8c
AS
319}
320
718e3744 321#endif /* _ZEBRA_TABLE_H */