]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | ||
c6ffe645 | 26 | #include "linklist.h" |
ec1a4283 | 27 | #include "prefix.h" |
9fd92e3c | 28 | #include "table.h" |
5adc2528 | 29 | #include "queue.h" |
fb018d25 | 30 | #include "nexthop.h" |
244a59d2 | 31 | #include "vrf.h" |
fb148af4 | 32 | #include "if.h" |
40c7bdb0 | 33 | #include "mpls.h" |
ec1a4283 | 34 | |
718e3744 | 35 | #define DISTANCE_INFINITY 255 |
7a4bb9c5 | 36 | #define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */ |
718e3744 | 37 | |
718e3744 | 38 | struct rib |
39 | { | |
40 | /* Link list. */ | |
41 | struct rib *next; | |
42 | struct rib *prev; | |
e6d7d054 PJ |
43 | |
44 | /* Nexthop structure */ | |
45 | struct nexthop *nexthop; | |
46 | ||
47 | /* Refrence count. */ | |
48 | unsigned long refcnt; | |
49 | ||
50 | /* Uptime. */ | |
51 | time_t uptime; | |
718e3744 | 52 | |
53 | /* Type fo this route. */ | |
54 | int type; | |
55 | ||
7c8ff89e DS |
56 | /* Source protocol instance */ |
57 | u_short instance; | |
58 | ||
78104b9b FL |
59 | /* VRF identifier. */ |
60 | vrf_id_t vrf_id; | |
61 | ||
718e3744 | 62 | /* Which routing table */ |
4e3afb14 | 63 | uint32_t table; |
718e3744 | 64 | |
e6d7d054 PJ |
65 | /* Metric */ |
66 | u_int32_t metric; | |
67 | ||
c50ca33a TT |
68 | /* MTU */ |
69 | u_int32_t mtu; | |
70 | u_int32_t nexthop_mtu; | |
71 | ||
718e3744 | 72 | /* Distance. */ |
73 | u_char distance; | |
74 | ||
0d9551dc DS |
75 | /* Tag */ |
76 | u_short tag; | |
77 | ||
6d691129 PJ |
78 | /* Flags of this route. |
79 | * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed | |
80 | * to clients via Zserv | |
81 | */ | |
85eda2c9 | 82 | u_int32_t flags; |
718e3744 | 83 | |
6d691129 PJ |
84 | /* RIB internal status */ |
85 | u_char status; | |
6e26278c DS |
86 | #define RIB_ENTRY_REMOVED 0x1 |
87 | /* to simplify NHT logic when NHs change, instead of doing a NH by NH cmp */ | |
88 | #define RIB_ENTRY_NEXTHOPS_CHANGED 0x2 | |
2f97fa65 | 89 | #define RIB_ENTRY_CHANGED 0x4 |
6d691129 | 90 | |
718e3744 | 91 | /* Nexthop information. */ |
92 | u_char nexthop_num; | |
93 | u_char nexthop_active_num; | |
94 | u_char nexthop_fib_num; | |
718e3744 | 95 | }; |
96 | ||
e96f9203 DO |
97 | /* meta-queue structure: |
98 | * sub-queue 0: connected, kernel | |
99 | * sub-queue 1: static | |
100 | * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS | |
101 | * sub-queue 3: iBGP, eBGP | |
102 | * sub-queue 4: any other origin (if any) | |
103 | */ | |
104 | #define MQ_SIZE 5 | |
105 | struct meta_queue | |
106 | { | |
107 | struct list *subq[MQ_SIZE]; | |
108 | u_int32_t size; /* sum of lengths of all subqueues */ | |
109 | }; | |
110 | ||
9fd92e3c AS |
111 | /* |
112 | * Structure that represents a single destination (prefix). | |
113 | */ | |
114 | typedef struct rib_dest_t_ | |
115 | { | |
116 | ||
117 | /* | |
118 | * Back pointer to the route node for this destination. This helps | |
119 | * us get to the prefix that this structure is for. | |
120 | */ | |
121 | struct route_node *rnode; | |
122 | ||
123 | /* | |
124 | * Doubly-linked list of routes for this prefix. | |
125 | */ | |
126 | struct rib *routes; | |
127 | ||
128 | /* | |
129 | * Flags, see below. | |
130 | */ | |
131 | u_int32_t flags; | |
132 | ||
5adc2528 AS |
133 | /* |
134 | * Linkage to put dest on the FPM processing queue. | |
135 | */ | |
136 | TAILQ_ENTRY(rib_dest_t_) fpm_q_entries; | |
137 | ||
9fd92e3c AS |
138 | } rib_dest_t; |
139 | ||
140 | #define RIB_ROUTE_QUEUED(x) (1 << (x)) | |
141 | ||
142 | /* | |
143 | * The maximum qindex that can be used. | |
144 | */ | |
145 | #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1) | |
146 | ||
5adc2528 AS |
147 | /* |
148 | * This flag indicates that a given prefix has been 'advertised' to | |
149 | * the FPM to be installed in the forwarding plane. | |
150 | */ | |
151 | #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1)) | |
152 | ||
153 | /* | |
154 | * This flag is set when we need to send an update to the FPM about a | |
155 | * dest. | |
156 | */ | |
157 | #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2)) | |
158 | ||
9fd92e3c AS |
159 | /* |
160 | * Macro to iterate over each route for a destination (prefix). | |
161 | */ | |
162 | #define RIB_DEST_FOREACH_ROUTE(dest, rib) \ | |
163 | for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next) | |
164 | ||
165 | /* | |
166 | * Same as above, but allows the current node to be unlinked. | |
167 | */ | |
168 | #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \ | |
169 | for ((rib) = (dest) ? (dest)->routes : NULL; \ | |
170 | (rib) && ((next) = (rib)->next, 1); \ | |
171 | (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 | ||
fa713d9e CF |
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) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \ | |
220 | ? (((recursing) = 1), (nexthop)->resolved) \ | |
221 | : ((nexthop)->next ? ((recursing) ? (nexthop)->next \ | |
222 | : ((tnexthop) = (nexthop)->next)) \ | |
223 | : (((recursing) = 0),((tnexthop) = (tnexthop)->next))) | |
224 | ||
8da4e946 | 225 | #if defined (HAVE_RTADV) |
cd80d74f FL |
226 | /* Structure which hold status of router advertisement. */ |
227 | struct rtadv | |
228 | { | |
229 | int sock; | |
230 | ||
231 | int adv_if_count; | |
232 | int adv_msec_if_count; | |
233 | ||
234 | struct thread *ra_read; | |
235 | struct thread *ra_timer; | |
236 | }; | |
8da4e946 | 237 | #endif /* HAVE_RTADV */ |
cd80d74f | 238 | |
1b5ed1b0 AS |
239 | /* |
240 | * rib_table_info_t | |
241 | * | |
242 | * Structure that is hung off of a route_table that holds information about | |
243 | * the table. | |
244 | */ | |
245 | typedef struct rib_table_info_t_ | |
246 | { | |
247 | ||
248 | /* | |
b72ede27 | 249 | * Back pointer to zebra_vrf. |
1b5ed1b0 | 250 | */ |
b72ede27 | 251 | struct zebra_vrf *zvrf; |
1b5ed1b0 AS |
252 | afi_t afi; |
253 | safi_t safi; | |
254 | ||
255 | } rib_table_info_t; | |
256 | ||
0915bb0c AS |
257 | typedef enum |
258 | { | |
259 | RIB_TABLES_ITER_S_INIT, | |
260 | RIB_TABLES_ITER_S_ITERATING, | |
261 | RIB_TABLES_ITER_S_DONE | |
262 | } rib_tables_iter_state_t; | |
263 | ||
264 | /* | |
265 | * Structure that holds state for iterating over all tables in the | |
266 | * Routing Information Base. | |
267 | */ | |
268 | typedef struct rib_tables_iter_t_ | |
269 | { | |
b72ede27 | 270 | vrf_id_t vrf_id; |
0915bb0c AS |
271 | int afi_safi_ix; |
272 | ||
273 | rib_tables_iter_state_t state; | |
274 | } rib_tables_iter_t; | |
275 | ||
1c848137 | 276 | /* Events/reasons triggering a RIB update. */ |
277 | typedef enum | |
278 | { | |
279 | RIB_UPDATE_IF_CHANGE, | |
280 | RIB_UPDATE_RMAP_CHANGE, | |
281 | RIB_UPDATE_OTHER | |
282 | } rib_update_event_t; | |
283 | ||
b892f1dd | 284 | extern struct nexthop *rib_nexthop_ifindex_add (struct rib *, ifindex_t); |
a399694f DS |
285 | extern struct nexthop *rib_nexthop_blackhole_add (struct rib *); |
286 | extern struct nexthop *rib_nexthop_ipv4_add (struct rib *, struct in_addr *, | |
287 | struct in_addr *); | |
288 | extern struct nexthop *rib_nexthop_ipv4_ifindex_add (struct rib *, | |
289 | struct in_addr *, | |
290 | struct in_addr *, | |
b892f1dd | 291 | ifindex_t); |
a399694f DS |
292 | extern void rib_nexthop_add (struct rib *rib, struct nexthop *nexthop); |
293 | extern void rib_copy_nexthops (struct rib *rib, struct nexthop *nh); | |
fb018d25 | 294 | |
4623d897 DL |
295 | /* RPF lookup behaviour */ |
296 | enum multicast_mode | |
297 | { | |
298 | MCAST_NO_CONFIG = 0, /* MIX_MRIB_FIRST, but no show in config write */ | |
299 | MCAST_MRIB_ONLY, /* MRIB only */ | |
300 | MCAST_URIB_ONLY, /* URIB only */ | |
301 | MCAST_MIX_MRIB_FIRST, /* MRIB, if nothing at all then URIB */ | |
302 | MCAST_MIX_DISTANCE, /* MRIB & URIB, lower distance wins */ | |
303 | MCAST_MIX_PFXLEN, /* MRIB & URIB, longer prefix wins */ | |
304 | /* on equal value, MRIB wins for last 2 */ | |
305 | }; | |
306 | ||
307 | extern void multicast_mode_ipv4_set (enum multicast_mode mode); | |
308 | extern enum multicast_mode multicast_mode_ipv4_get (void); | |
309 | ||
fa713d9e | 310 | extern int nexthop_has_fib_child(struct nexthop *); |
12f6fb97 DS |
311 | extern void rib_lookup_and_dump (struct prefix_ipv4 *, vrf_id_t); |
312 | extern void rib_lookup_and_pushup (struct prefix_ipv4 *, vrf_id_t); | |
f7bf4153 DL |
313 | #define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib) |
314 | extern void _rib_dump (const char *, | |
315 | union prefix46constptr, const struct rib *); | |
78104b9b FL |
316 | extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *, |
317 | vrf_id_t); | |
dc95824a DO |
318 | #define ZEBRA_RIB_LOOKUP_ERROR -1 |
319 | #define ZEBRA_RIB_FOUND_EXACT 0 | |
320 | #define ZEBRA_RIB_FOUND_NOGATE 1 | |
321 | #define ZEBRA_RIB_FOUND_CONNECTED 2 | |
322 | #define ZEBRA_RIB_NOTFOUND 3 | |
323 | ||
28f6dde8 | 324 | extern void rib_nexthop_delete (struct rib *rib, struct nexthop *nexthop); |
a399694f DS |
325 | extern struct nexthop *rib_nexthop_ipv6_add (struct rib *, struct in6_addr *); |
326 | extern struct nexthop *rib_nexthop_ipv6_ifindex_add (struct rib *rib, | |
327 | struct in6_addr *ipv6, | |
b892f1dd | 328 | ifindex_t ifindex); |
718e3744 | 329 | |
7a4bb9c5 DS |
330 | extern int is_zebra_valid_kernel_table(u_int32_t table_id); |
331 | extern int is_zebra_main_routing_table(u_int32_t table_id); | |
0aabccc0 | 332 | extern int zebra_check_addr (struct prefix *p); |
718e3744 | 333 | |
28f6dde8 DS |
334 | extern void rib_addnode (struct route_node *rn, struct rib *rib, int process); |
335 | extern void rib_delnode (struct route_node *rn, struct rib *rib); | |
336 | extern int rib_install_kernel (struct route_node *rn, struct rib *rib, int update); | |
337 | extern int rib_uninstall_kernel (struct route_node *rn, struct rib *rib); | |
338 | ||
d24af186 | 339 | /* NOTE: |
b4c034b0 | 340 | * All rib_add function will not just add prefix into RIB, but |
d24af186 | 341 | * also implicitly withdraw equal prefix of same type. */ |
3b1098be DS |
342 | extern int rib_add (afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, |
343 | u_short instance, int flags, struct prefix *p, | |
344 | union g_addr *gate, union g_addr *src, | |
345 | ifindex_t ifindex, u_int32_t table_id, | |
346 | u_int32_t, u_int32_t, u_char); | |
718e3744 | 347 | |
b4c034b0 DS |
348 | extern int rib_add_multipath (afi_t afi, safi_t safi, struct prefix *, |
349 | struct rib *); | |
718e3744 | 350 | |
616368ed DS |
351 | extern int rib_delete (afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, |
352 | u_short instance, int flags, struct prefix *p, | |
353 | union g_addr *gate, ifindex_t ifindex, | |
354 | u_int32_t table_id); | |
718e3744 | 355 | |
14364a31 DS |
356 | extern struct rib *rib_match (afi_t afi, safi_t safi, vrf_id_t, union g_addr *, |
357 | struct route_node **rn_out); | |
f86a2b82 | 358 | extern struct rib *rib_match_ipv4_multicast (vrf_id_t vrf_id, struct in_addr addr, |
4623d897 | 359 | struct route_node **rn_out); |
718e3744 | 360 | |
78104b9b | 361 | extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *, vrf_id_t); |
718e3744 | 362 | |
1c848137 | 363 | extern void rib_update (vrf_id_t, rib_update_event_t); |
a1ac18c4 | 364 | extern void rib_weed_tables (void); |
365 | extern void rib_sweep_route (void); | |
a31c5886 | 366 | extern void rib_close_table (struct route_table *); |
a1ac18c4 | 367 | extern void rib_close (void); |
368 | extern void rib_init (void); | |
7c8ff89e | 369 | extern unsigned long rib_score_proto (u_char proto, u_short instance); |
44e9909d | 370 | extern void rib_queue_add (struct route_node *rn); |
6e26278c | 371 | |
718e3744 | 372 | extern struct route_table *rib_table_ipv6; |
373 | ||
9fd92e3c | 374 | extern int rib_gc_dest (struct route_node *rn); |
0915bb0c | 375 | extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter); |
9fd92e3c | 376 | |
40c7bdb0 | 377 | extern u_char route_distance(int type); |
378 | ||
9fd92e3c AS |
379 | /* |
380 | * Inline functions. | |
381 | */ | |
382 | ||
1b5ed1b0 AS |
383 | /* |
384 | * rib_table_info | |
385 | */ | |
386 | static inline rib_table_info_t * | |
387 | rib_table_info (struct route_table *table) | |
388 | { | |
389 | return (rib_table_info_t *) table->info; | |
390 | } | |
391 | ||
9fd92e3c AS |
392 | /* |
393 | * rib_dest_from_rnode | |
394 | */ | |
395 | static inline rib_dest_t * | |
396 | rib_dest_from_rnode (struct route_node *rn) | |
397 | { | |
398 | return (rib_dest_t *) rn->info; | |
399 | } | |
400 | ||
401 | /* | |
402 | * rnode_to_ribs | |
403 | * | |
404 | * Returns a pointer to the list of routes corresponding to the given | |
405 | * route_node. | |
406 | */ | |
407 | static inline struct rib * | |
408 | rnode_to_ribs (struct route_node *rn) | |
409 | { | |
410 | rib_dest_t *dest; | |
411 | ||
412 | dest = rib_dest_from_rnode (rn); | |
413 | if (!dest) | |
414 | return NULL; | |
415 | ||
416 | return dest->routes; | |
417 | } | |
418 | ||
419 | /* | |
420 | * rib_dest_prefix | |
421 | */ | |
422 | static inline struct prefix * | |
423 | rib_dest_prefix (rib_dest_t *dest) | |
424 | { | |
425 | return &dest->rnode->p; | |
426 | } | |
427 | ||
428 | /* | |
429 | * rib_dest_af | |
430 | * | |
431 | * Returns the address family that the destination is for. | |
432 | */ | |
433 | static inline u_char | |
434 | rib_dest_af (rib_dest_t *dest) | |
435 | { | |
436 | return dest->rnode->p.family; | |
437 | } | |
438 | ||
439 | /* | |
440 | * rib_dest_table | |
441 | */ | |
442 | static inline struct route_table * | |
443 | rib_dest_table (rib_dest_t *dest) | |
444 | { | |
445 | return dest->rnode->table; | |
446 | } | |
447 | ||
1b5ed1b0 AS |
448 | /* |
449 | * rib_dest_vrf | |
450 | */ | |
b72ede27 | 451 | static inline struct zebra_vrf * |
1b5ed1b0 AS |
452 | rib_dest_vrf (rib_dest_t *dest) |
453 | { | |
b72ede27 | 454 | return rib_table_info (rib_dest_table (dest))->zvrf; |
1b5ed1b0 AS |
455 | } |
456 | ||
0915bb0c AS |
457 | /* |
458 | * rib_tables_iter_init | |
459 | */ | |
460 | static inline void | |
461 | rib_tables_iter_init (rib_tables_iter_t *iter) | |
462 | ||
463 | { | |
464 | memset (iter, 0, sizeof (*iter)); | |
465 | iter->state = RIB_TABLES_ITER_S_INIT; | |
466 | } | |
467 | ||
468 | /* | |
469 | * rib_tables_iter_started | |
470 | * | |
471 | * Returns TRUE if this iterator has started iterating over the set of | |
472 | * tables. | |
473 | */ | |
474 | static inline int | |
475 | rib_tables_iter_started (rib_tables_iter_t *iter) | |
476 | { | |
477 | return iter->state != RIB_TABLES_ITER_S_INIT; | |
478 | } | |
479 | ||
480 | /* | |
481 | * rib_tables_iter_cleanup | |
482 | */ | |
483 | static inline void | |
484 | rib_tables_iter_cleanup (rib_tables_iter_t *iter) | |
485 | { | |
486 | iter->state = RIB_TABLES_ITER_S_DONE; | |
487 | } | |
488 | ||
718e3744 | 489 | #endif /*_ZEBRA_RIB_H */ |