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