]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rib.h
*: reindent
[mirror_frr.git] / zebra / rib.h
1 /*
2 * Routing Information Base header
3 * Copyright (C) 1997 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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #ifndef _ZEBRA_RIB_H
24 #define _ZEBRA_RIB_H
25
26 #include "zebra.h"
27 #include "hook.h"
28 #include "linklist.h"
29 #include "prefix.h"
30 #include "table.h"
31 #include "queue.h"
32 #include "nexthop.h"
33 #include "vrf.h"
34 #include "if.h"
35 #include "mpls.h"
36 #include "srcdest_table.h"
37
38 #define DISTANCE_INFINITY 255
39 #define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
40
41 struct rib {
42 /* Link list. */
43 struct rib *next;
44 struct rib *prev;
45
46 /* Nexthop structure */
47 struct nexthop *nexthop;
48
49 /* Refrence count. */
50 unsigned long refcnt;
51
52 /* Tag */
53 route_tag_t tag;
54
55 /* Uptime. */
56 time_t uptime;
57
58 /* Type fo this route. */
59 int type;
60
61 /* Source protocol instance */
62 u_short instance;
63
64 /* VRF identifier. */
65 vrf_id_t vrf_id;
66
67 /* Which routing table */
68 uint32_t table;
69
70 /* Metric */
71 u_int32_t metric;
72
73 /* MTU */
74 u_int32_t mtu;
75 u_int32_t nexthop_mtu;
76
77 /* Distance. */
78 u_char distance;
79
80 /* Flags of this route.
81 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
82 * to clients via Zserv
83 */
84 u_int32_t flags;
85
86 /* RIB internal status */
87 u_char status;
88 #define RIB_ENTRY_REMOVED 0x1
89 /* to simplify NHT logic when NHs change, instead of doing a NH by NH cmp */
90 #define RIB_ENTRY_NEXTHOPS_CHANGED 0x2
91 #define RIB_ENTRY_CHANGED 0x4
92 #define RIB_ENTRY_SELECTED_FIB 0x8
93 #define RIB_ENTRY_LABELS_CHANGED 0x10
94
95 /* Nexthop information. */
96 u_char nexthop_num;
97 u_char nexthop_active_num;
98 };
99
100 /* meta-queue structure:
101 * sub-queue 0: connected, kernel
102 * sub-queue 1: static
103 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
104 * sub-queue 3: iBGP, eBGP
105 * sub-queue 4: any other origin (if any)
106 */
107 #define MQ_SIZE 5
108 struct meta_queue {
109 struct list *subq[MQ_SIZE];
110 u_int32_t size; /* sum of lengths of all subqueues */
111 };
112
113 /*
114 * Structure that represents a single destination (prefix).
115 */
116 typedef struct rib_dest_t_ {
117
118 /*
119 * Back pointer to the route node for this destination. This helps
120 * us get to the prefix that this structure is for.
121 */
122 struct route_node *rnode;
123
124 /*
125 * Doubly-linked list of routes for this prefix.
126 */
127 struct rib *routes;
128
129 /*
130 * Flags, see below.
131 */
132 u_int32_t flags;
133
134 /*
135 * Linkage to put dest on the FPM processing queue.
136 */
137 TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
138
139 } rib_dest_t;
140
141 #define RIB_ROUTE_QUEUED(x) (1 << (x))
142
143 /*
144 * The maximum qindex that can be used.
145 */
146 #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
147
148 /*
149 * This flag indicates that a given prefix has been 'advertised' to
150 * the FPM to be installed in the forwarding plane.
151 */
152 #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
153
154 /*
155 * This flag is set when we need to send an update to the FPM about a
156 * dest.
157 */
158 #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
159
160 /*
161 * Macro to iterate over each route for a destination (prefix).
162 */
163 #define RIB_DEST_FOREACH_ROUTE(dest, rib) \
164 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
165
166 /*
167 * Same as above, but allows the current node to be unlinked.
168 */
169 #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
170 for ((rib) = (dest) ? (dest)->routes : NULL; \
171 (rib) && ((next) = (rib)->next, 1); (rib) = (next))
172
173 #define RNODE_FOREACH_RIB(rn, rib) \
174 RIB_DEST_FOREACH_ROUTE(rib_dest_from_rnode(rn), rib)
175
176 #define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
177 RIB_DEST_FOREACH_ROUTE_SAFE(rib_dest_from_rnode(rn), rib, next)
178
179 /* The following for loop allows to iterate over the nexthop
180 * structure of routes.
181 *
182 * We have to maintain quite a bit of state:
183 *
184 * nexthop: The pointer to the current nexthop, either in the
185 * top-level chain or in the resolved chain of ni.
186 * tnexthop: The pointer to the current nexthop in the top-level
187 * nexthop chain.
188 * recursing: Information if nh currently is in the top-level chain
189 * (0) or in a resolved chain (1).
190 *
191 * Initialization: Set `nexthop' and `tnexthop' to the head of the
192 * top-level chain. As nexthop is in the top level chain, set recursing
193 * to 0.
194 *
195 * Iteration check: Check that the `nexthop' pointer is not NULL.
196 *
197 * Iteration step: This is the tricky part. Check if `nexthop' has
198 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
199 * the top level chain and has at least one nexthop attached to
200 * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
201 * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
202 * `tnexthop' is left alone in that case so we can remember which nexthop
203 * in the top level chain we are currently handling.
204 *
205 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
206 * current chain. If we are recursing, `nexthop' will be set to
207 * `nexthop->next' and `tnexthop' will be left alone. If we are not
208 * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
209 * as we are progressing in the top level chain.
210 * If we encounter `nexthop->next == NULL', we will clear the `recursing'
211 * flag as we arived either at the end of the resolved chain or at the end
212 * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
213 * to `tnexthop->next', progressing to the next position in the top-level
214 * chain and possibly to its end marked by NULL.
215 */
216 #define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
217 (tnexthop) = (nexthop) = (head), (recursing) = 0; \
218 (nexthop); \
219 (nexthop) = \
220 CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
221 ? (((recursing) = 1), (nexthop)->resolved) \
222 : ((nexthop)->next \
223 ? ((recursing) ? (nexthop)->next \
224 : ((tnexthop) = \
225 (nexthop)->next)) \
226 : (((recursing) = 0), \
227 ((tnexthop) = (tnexthop)->next)))
228
229 #if defined(HAVE_RTADV)
230 /* Structure which hold status of router advertisement. */
231 struct rtadv {
232 int sock;
233
234 int adv_if_count;
235 int adv_msec_if_count;
236
237 struct thread *ra_read;
238 struct thread *ra_timer;
239 };
240 #endif /* HAVE_RTADV */
241
242 /*
243 * rib_table_info_t
244 *
245 * Structure that is hung off of a route_table that holds information about
246 * the table.
247 */
248 typedef struct rib_table_info_t_ {
249
250 /*
251 * Back pointer to zebra_vrf.
252 */
253 struct zebra_vrf *zvrf;
254 afi_t afi;
255 safi_t safi;
256
257 } rib_table_info_t;
258
259 typedef enum {
260 RIB_TABLES_ITER_S_INIT,
261 RIB_TABLES_ITER_S_ITERATING,
262 RIB_TABLES_ITER_S_DONE
263 } rib_tables_iter_state_t;
264
265 /*
266 * Structure that holds state for iterating over all tables in the
267 * Routing Information Base.
268 */
269 typedef struct rib_tables_iter_t_ {
270 vrf_id_t vrf_id;
271 int afi_safi_ix;
272
273 rib_tables_iter_state_t state;
274 } rib_tables_iter_t;
275
276 /* Events/reasons triggering a RIB update. */
277 typedef enum {
278 RIB_UPDATE_IF_CHANGE,
279 RIB_UPDATE_RMAP_CHANGE,
280 RIB_UPDATE_OTHER
281 } rib_update_event_t;
282
283 extern struct nexthop *rib_nexthop_ifindex_add(struct rib *, ifindex_t);
284 extern struct nexthop *rib_nexthop_blackhole_add(struct rib *);
285 extern struct nexthop *rib_nexthop_ipv4_add(struct rib *, struct in_addr *,
286 struct in_addr *);
287 extern struct nexthop *rib_nexthop_ipv4_ifindex_add(struct rib *,
288 struct in_addr *,
289 struct in_addr *,
290 ifindex_t);
291 extern void rib_nexthop_add(struct rib *rib, struct nexthop *nexthop);
292 extern void rib_copy_nexthops(struct rib *rib, struct nexthop *nh);
293
294 /* RPF lookup behaviour */
295 enum multicast_mode {
296 MCAST_NO_CONFIG = 0, /* MIX_MRIB_FIRST, but no show in config write */
297 MCAST_MRIB_ONLY, /* MRIB only */
298 MCAST_URIB_ONLY, /* URIB only */
299 MCAST_MIX_MRIB_FIRST, /* MRIB, if nothing at all then URIB */
300 MCAST_MIX_DISTANCE, /* MRIB & URIB, lower distance wins */
301 MCAST_MIX_PFXLEN, /* MRIB & URIB, longer prefix wins */
302 /* on equal value, MRIB wins for last 2 */
303 };
304
305 extern void multicast_mode_ipv4_set(enum multicast_mode mode);
306 extern enum multicast_mode multicast_mode_ipv4_get(void);
307
308 extern int nexthop_has_fib_child(struct nexthop *);
309 extern void rib_lookup_and_dump(struct prefix_ipv4 *, vrf_id_t);
310 extern void rib_lookup_and_pushup(struct prefix_ipv4 *, vrf_id_t);
311 #define rib_dump(prefix, src, rib) _rib_dump(__func__, prefix, src, rib)
312 extern void _rib_dump(const char *, union prefixconstptr, union prefixconstptr,
313 const struct rib *);
314 extern int rib_lookup_ipv4_route(struct prefix_ipv4 *, union sockunion *,
315 vrf_id_t);
316 #define ZEBRA_RIB_LOOKUP_ERROR -1
317 #define ZEBRA_RIB_FOUND_EXACT 0
318 #define ZEBRA_RIB_FOUND_NOGATE 1
319 #define ZEBRA_RIB_FOUND_CONNECTED 2
320 #define ZEBRA_RIB_NOTFOUND 3
321
322 extern void rib_nexthop_delete(struct rib *rib, struct nexthop *nexthop);
323 extern struct nexthop *rib_nexthop_ipv6_add(struct rib *, struct in6_addr *);
324 extern struct nexthop *rib_nexthop_ipv6_ifindex_add(struct rib *rib,
325 struct in6_addr *ipv6,
326 ifindex_t ifindex);
327
328 extern int is_zebra_valid_kernel_table(u_int32_t table_id);
329 extern int is_zebra_main_routing_table(u_int32_t table_id);
330 extern int zebra_check_addr(struct prefix *p);
331
332 extern void rib_addnode(struct route_node *rn, struct rib *rib, int process);
333 extern void rib_delnode(struct route_node *rn, struct rib *rib);
334 extern int rib_install_kernel(struct route_node *rn, struct rib *rib,
335 struct rib *old);
336 extern int rib_uninstall_kernel(struct route_node *rn, struct rib *rib);
337
338 /* NOTE:
339 * All rib_add function will not just add prefix into RIB, but
340 * also implicitly withdraw equal prefix of same type. */
341 extern int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
342 u_short instance, int flags, struct prefix *p,
343 struct prefix_ipv6 *src_p, union g_addr *gate,
344 union g_addr *src, ifindex_t ifindex, u_int32_t table_id,
345 u_int32_t, u_int32_t, u_char);
346
347 extern int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *,
348 struct prefix_ipv6 *src_p, struct rib *);
349
350 extern void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
351 u_short instance, int flags, struct prefix *p,
352 struct prefix_ipv6 *src_p, union g_addr *gate,
353 ifindex_t ifindex, u_int32_t table_id);
354
355 extern struct rib *rib_match(afi_t afi, safi_t safi, vrf_id_t, union g_addr *,
356 struct route_node **rn_out);
357 extern struct rib *rib_match_ipv4_multicast(vrf_id_t vrf_id,
358 struct in_addr addr,
359 struct route_node **rn_out);
360
361 extern struct rib *rib_lookup_ipv4(struct prefix_ipv4 *, vrf_id_t);
362
363 extern void rib_update(vrf_id_t, rib_update_event_t);
364 extern void rib_weed_tables(void);
365 extern void rib_sweep_route(void);
366 extern void rib_close_table(struct route_table *);
367 extern void rib_init(void);
368 extern unsigned long rib_score_proto(u_char proto, u_short instance);
369 extern void rib_queue_add(struct route_node *rn);
370 extern void meta_queue_free(struct meta_queue *mq);
371
372 extern struct route_table *rib_table_ipv6;
373
374 extern void rib_unlink(struct route_node *, struct rib *);
375 extern int rib_gc_dest(struct route_node *rn);
376 extern struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter);
377
378 extern u_char route_distance(int type);
379
380 /*
381 * Inline functions.
382 */
383
384 /*
385 * rib_table_info
386 */
387 static inline rib_table_info_t *rib_table_info(struct route_table *table)
388 {
389 return (rib_table_info_t *)table->info;
390 }
391
392 /*
393 * rib_dest_from_rnode
394 */
395 static inline rib_dest_t *rib_dest_from_rnode(struct route_node *rn)
396 {
397 return (rib_dest_t *)rn->info;
398 }
399
400 /*
401 * rnode_to_ribs
402 *
403 * Returns a pointer to the list of routes corresponding to the given
404 * route_node.
405 */
406 static inline struct rib *rnode_to_ribs(struct route_node *rn)
407 {
408 rib_dest_t *dest;
409
410 dest = rib_dest_from_rnode(rn);
411 if (!dest)
412 return NULL;
413
414 return dest->routes;
415 }
416
417 /*
418 * rib_dest_prefix
419 */
420 static inline struct prefix *rib_dest_prefix(rib_dest_t *dest)
421 {
422 return &dest->rnode->p;
423 }
424
425 /*
426 * rib_dest_af
427 *
428 * Returns the address family that the destination is for.
429 */
430 static inline u_char rib_dest_af(rib_dest_t *dest)
431 {
432 return dest->rnode->p.family;
433 }
434
435 /*
436 * rib_dest_table
437 */
438 static inline struct route_table *rib_dest_table(rib_dest_t *dest)
439 {
440 return srcdest_rnode_table(dest->rnode);
441 }
442
443 /*
444 * rib_dest_vrf
445 */
446 static inline struct zebra_vrf *rib_dest_vrf(rib_dest_t *dest)
447 {
448 return rib_table_info(rib_dest_table(dest))->zvrf;
449 }
450
451 /*
452 * rib_tables_iter_init
453 */
454 static inline void rib_tables_iter_init(rib_tables_iter_t *iter)
455
456 {
457 memset(iter, 0, sizeof(*iter));
458 iter->state = RIB_TABLES_ITER_S_INIT;
459 }
460
461 /*
462 * rib_tables_iter_started
463 *
464 * Returns TRUE if this iterator has started iterating over the set of
465 * tables.
466 */
467 static inline int rib_tables_iter_started(rib_tables_iter_t *iter)
468 {
469 return iter->state != RIB_TABLES_ITER_S_INIT;
470 }
471
472 /*
473 * rib_tables_iter_cleanup
474 */
475 static inline void rib_tables_iter_cleanup(rib_tables_iter_t *iter)
476 {
477 iter->state = RIB_TABLES_ITER_S_DONE;
478 }
479
480 DECLARE_HOOK(rib_update, (struct route_node * rn, const char *reason),
481 (rn, reason))
482
483 #endif /*_ZEBRA_RIB_H */