]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rib.h
zebra: maintain the router-id per VRF
[mirror_frr.git] / zebra / rib.h
CommitLineData
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"
ec1a4283 31
718e3744 32#define DISTANCE_INFINITY 255
7a4bb9c5 33#define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
718e3744 34
718e3744 35struct rib
36{
37 /* Link list. */
38 struct rib *next;
39 struct rib *prev;
e6d7d054
PJ
40
41 /* Nexthop structure */
42 struct nexthop *nexthop;
43
44 /* Refrence count. */
45 unsigned long refcnt;
46
47 /* Uptime. */
48 time_t uptime;
718e3744 49
50 /* Type fo this route. */
51 int type;
52
7c8ff89e
DS
53 /* Source protocol instance */
54 u_short instance;
55
78104b9b
FL
56 /* VRF identifier. */
57 vrf_id_t vrf_id;
58
718e3744 59 /* Which routing table */
4e3afb14 60 uint32_t table;
718e3744 61
e6d7d054
PJ
62 /* Metric */
63 u_int32_t metric;
64
718e3744 65 /* Distance. */
66 u_char distance;
67
0d9551dc
DS
68 /* Tag */
69 u_short tag;
70
6d691129
PJ
71 /* Flags of this route.
72 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
73 * to clients via Zserv
74 */
718e3744 75 u_char flags;
76
6d691129
PJ
77 /* RIB internal status */
78 u_char status;
6e26278c
DS
79#define RIB_ENTRY_REMOVED 0x1
80 /* to simplify NHT logic when NHs change, instead of doing a NH by NH cmp */
81#define RIB_ENTRY_NEXTHOPS_CHANGED 0x2
6d691129 82
718e3744 83 /* Nexthop information. */
84 u_char nexthop_num;
85 u_char nexthop_active_num;
86 u_char nexthop_fib_num;
718e3744 87};
88
e96f9203
DO
89/* meta-queue structure:
90 * sub-queue 0: connected, kernel
91 * sub-queue 1: static
92 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
93 * sub-queue 3: iBGP, eBGP
94 * sub-queue 4: any other origin (if any)
95 */
96#define MQ_SIZE 5
97struct meta_queue
98{
99 struct list *subq[MQ_SIZE];
100 u_int32_t size; /* sum of lengths of all subqueues */
101};
102
9fd92e3c
AS
103/*
104 * Structure that represents a single destination (prefix).
105 */
106typedef struct rib_dest_t_
107{
108
109 /*
110 * Back pointer to the route node for this destination. This helps
111 * us get to the prefix that this structure is for.
112 */
113 struct route_node *rnode;
114
115 /*
116 * Doubly-linked list of routes for this prefix.
117 */
118 struct rib *routes;
119
120 /*
121 * Flags, see below.
122 */
123 u_int32_t flags;
124
5adc2528
AS
125 /*
126 * Linkage to put dest on the FPM processing queue.
127 */
128 TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
129
9fd92e3c
AS
130} rib_dest_t;
131
132#define RIB_ROUTE_QUEUED(x) (1 << (x))
133
134/*
135 * The maximum qindex that can be used.
136 */
137#define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
138
5adc2528
AS
139/*
140 * This flag indicates that a given prefix has been 'advertised' to
141 * the FPM to be installed in the forwarding plane.
142 */
143#define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
144
145/*
146 * This flag is set when we need to send an update to the FPM about a
147 * dest.
148 */
149#define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
150
9fd92e3c
AS
151/*
152 * Macro to iterate over each route for a destination (prefix).
153 */
154#define RIB_DEST_FOREACH_ROUTE(dest, rib) \
155 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
156
157/*
158 * Same as above, but allows the current node to be unlinked.
159 */
160#define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
161 for ((rib) = (dest) ? (dest)->routes : NULL; \
162 (rib) && ((next) = (rib)->next, 1); \
163 (rib) = (next))
164
165#define RNODE_FOREACH_RIB(rn, rib) \
166 RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
167
168#define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
169 RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
170
718e3744 171/* Static route information. */
172struct static_ipv4
173{
174 /* For linked list. */
175 struct static_ipv4 *prev;
176 struct static_ipv4 *next;
177
8f527c5e
FL
178 /* VRF identifier. */
179 vrf_id_t vrf_id;
180
718e3744 181 /* Administrative distance. */
182 u_char distance;
183
0d9551dc
DS
184 /* Tag */
185 u_short tag;
186
718e3744 187 /* Flag for this static route's type. */
188 u_char type;
189#define STATIC_IPV4_GATEWAY 1
190#define STATIC_IPV4_IFNAME 2
595db7f1 191#define STATIC_IPV4_BLACKHOLE 3
718e3744 192
193 /* Nexthop value. */
194 union
195 {
196 struct in_addr ipv4;
197 char *ifname;
198 } gate;
81dfcaa2 199
200 /* bit flags */
201 u_char flags;
202/*
203 see ZEBRA_FLAG_REJECT
204 ZEBRA_FLAG_BLACKHOLE
205 */
718e3744 206};
207
208#ifdef HAVE_IPV6
209/* Static route information. */
210struct static_ipv6
211{
212 /* For linked list. */
213 struct static_ipv6 *prev;
214 struct static_ipv6 *next;
215
8f527c5e
FL
216 /* VRF identifier. */
217 vrf_id_t vrf_id;
218
718e3744 219 /* Administrative distance. */
220 u_char distance;
221
0d9551dc
DS
222 /* Tag */
223 u_short tag;
224
718e3744 225 /* Flag for this static route's type. */
226 u_char type;
227#define STATIC_IPV6_GATEWAY 1
228#define STATIC_IPV6_GATEWAY_IFNAME 2
229#define STATIC_IPV6_IFNAME 3
230
231 /* Nexthop value. */
232 struct in6_addr ipv6;
233 char *ifname;
81dfcaa2 234
235 /* bit flags */
236 u_char flags;
237/*
238 see ZEBRA_FLAG_REJECT
239 ZEBRA_FLAG_BLACKHOLE
240 */
718e3744 241};
242#endif /* HAVE_IPV6 */
243
fa713d9e
CF
244/* The following for loop allows to iterate over the nexthop
245 * structure of routes.
246 *
247 * We have to maintain quite a bit of state:
248 *
249 * nexthop: The pointer to the current nexthop, either in the
250 * top-level chain or in the resolved chain of ni.
251 * tnexthop: The pointer to the current nexthop in the top-level
252 * nexthop chain.
253 * recursing: Information if nh currently is in the top-level chain
254 * (0) or in a resolved chain (1).
255 *
256 * Initialization: Set `nexthop' and `tnexthop' to the head of the
257 * top-level chain. As nexthop is in the top level chain, set recursing
258 * to 0.
259 *
260 * Iteration check: Check that the `nexthop' pointer is not NULL.
261 *
262 * Iteration step: This is the tricky part. Check if `nexthop' has
263 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
264 * the top level chain and has at least one nexthop attached to
265 * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
266 * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
267 * `tnexthop' is left alone in that case so we can remember which nexthop
268 * in the top level chain we are currently handling.
269 *
270 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
271 * current chain. If we are recursing, `nexthop' will be set to
272 * `nexthop->next' and `tnexthop' will be left alone. If we are not
273 * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
274 * as we are progressing in the top level chain.
275 * If we encounter `nexthop->next == NULL', we will clear the `recursing'
276 * flag as we arived either at the end of the resolved chain or at the end
277 * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
278 * to `tnexthop->next', progressing to the next position in the top-level
279 * chain and possibly to its end marked by NULL.
280 */
281#define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
282 (tnexthop) = (nexthop) = (head), (recursing) = 0; \
283 (nexthop); \
284 (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
285 ? (((recursing) = 1), (nexthop)->resolved) \
286 : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
287 : ((tnexthop) = (nexthop)->next)) \
288 : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
289
718e3744 290/* Routing table instance. */
b72ede27 291struct zebra_vrf
718e3744 292{
b72ede27
FL
293 /* Identifier. */
294 vrf_id_t vrf_id;
718e3744 295
296 /* Routing table name. */
297 char *name;
298
299 /* Description. */
300 char *desc;
301
302 /* FIB identifier. */
303 u_char fib_id;
304
305 /* Routing table. */
306 struct route_table *table[AFI_MAX][SAFI_MAX];
307
308 /* Static route configuration. */
309 struct route_table *stable[AFI_MAX][SAFI_MAX];
fb018d25
DS
310
311 /* Recursive Nexthop table */
312 struct route_table *rnh_table[AFI_MAX];
7a4bb9c5 313
078430f6
DS
314 /* Import check table (used mostly by BGP */
315 struct route_table *import_check_table[AFI_MAX];
316
7a4bb9c5
DS
317 /* Routing tables off of main table for redistribute table */
318 struct route_table *other_table[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX];
c6ffe645
FL
319
320 /* 2nd pointer type used primarily to quell a warning on
321 * ALL_LIST_ELEMENTS_RO
322 */
323 struct list _rid_all_sorted_list;
324 struct list _rid_lo_sorted_list;
325 struct list *rid_all_sorted_list;
326 struct list *rid_lo_sorted_list;
327 struct prefix rid_user_assigned;
718e3744 328};
329
1b5ed1b0
AS
330/*
331 * rib_table_info_t
332 *
333 * Structure that is hung off of a route_table that holds information about
334 * the table.
335 */
336typedef struct rib_table_info_t_
337{
338
339 /*
b72ede27 340 * Back pointer to zebra_vrf.
1b5ed1b0 341 */
b72ede27 342 struct zebra_vrf *zvrf;
1b5ed1b0
AS
343 afi_t afi;
344 safi_t safi;
345
346} rib_table_info_t;
347
0915bb0c
AS
348typedef enum
349{
350 RIB_TABLES_ITER_S_INIT,
351 RIB_TABLES_ITER_S_ITERATING,
352 RIB_TABLES_ITER_S_DONE
353} rib_tables_iter_state_t;
354
355/*
356 * Structure that holds state for iterating over all tables in the
357 * Routing Information Base.
358 */
359typedef struct rib_tables_iter_t_
360{
b72ede27 361 vrf_id_t vrf_id;
0915bb0c
AS
362 int afi_safi_ix;
363
364 rib_tables_iter_state_t state;
365} rib_tables_iter_t;
366
a1ac18c4 367extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
368extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
369extern struct nexthop *nexthop_blackhole_add (struct rib *);
7514fb77
PJ
370extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
371 struct in_addr *);
c8a1cb5c
DS
372extern struct nexthop * nexthop_ipv4_ifindex_ol_add (struct rib *, const struct in_addr *,
373 const struct in_addr *, const unsigned);
26e2ae36
JB
374extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
375 struct in_addr *,
376 struct in_addr *,
377 unsigned int);
6e26278c
DS
378extern void nexthop_free (struct nexthop *nexthop, struct route_node *);
379extern void nexthops_free (struct nexthop *nexthop, struct route_node *);
fb018d25 380extern void nexthop_add (struct rib *rib, struct nexthop *nexthop);
6e26278c 381extern void copy_nexthops (struct rib *rib, struct nexthop *nh);
fb018d25 382
fa713d9e 383extern int nexthop_has_fib_child(struct nexthop *);
dc95824a 384extern void rib_lookup_and_dump (struct prefix_ipv4 *);
20e5ff0a 385extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
f7bf4153
DL
386#define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
387extern void _rib_dump (const char *,
388 union prefix46constptr, const struct rib *);
78104b9b
FL
389extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *,
390 vrf_id_t);
dc95824a
DO
391#define ZEBRA_RIB_LOOKUP_ERROR -1
392#define ZEBRA_RIB_FOUND_EXACT 0
393#define ZEBRA_RIB_FOUND_NOGATE 1
394#define ZEBRA_RIB_FOUND_CONNECTED 2
395#define ZEBRA_RIB_NOTFOUND 3
396
718e3744 397#ifdef HAVE_IPV6
a1ac18c4 398extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
41fc2714
DS
399extern struct nexthop *nexthop_ipv6_ifindex_add (struct rib *rib,
400 struct in6_addr *ipv6, unsigned int ifindex);
401extern struct nexthop *nexthop_ipv6_ifname_add (struct rib *rib,
402 struct in6_addr *ipv6,
403 char *ifname);
404extern int
405rib_bogus_ipv6 (int type, struct prefix_ipv6 *p,
406 struct in6_addr *gate, unsigned int ifindex, int table);
718e3744 407#endif /* HAVE_IPV6 */
408
b72ede27
FL
409extern struct zebra_vrf *zebra_vrf_lookup (vrf_id_t vrf_id);
410extern struct zebra_vrf *zebra_vrf_alloc (vrf_id_t);
411extern struct route_table *zebra_vrf_table (afi_t, safi_t, vrf_id_t);
412extern struct route_table *zebra_vrf_static_table (afi_t, safi_t, vrf_id_t);
413extern struct route_table *zebra_vrf_other_route_table (afi_t afi, u_int32_t table_id,
414 vrf_id_t vrf_id);
7a4bb9c5
DS
415extern int is_zebra_valid_kernel_table(u_int32_t table_id);
416extern int is_zebra_main_routing_table(u_int32_t table_id);
0aabccc0 417extern int zebra_check_addr (struct prefix *p);
718e3744 418
d24af186 419/* NOTE:
420 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
421 * also implicitly withdraw equal prefix of same type. */
7c8ff89e 422extern int rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
7514fb77 423 struct in_addr *gate, struct in_addr *src,
78104b9b 424 unsigned int ifindex, vrf_id_t vrf_id, u_int32_t table_id,
cddf391b 425 u_int32_t, u_char, safi_t);
718e3744 426
cddf391b 427extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
718e3744 428
7c8ff89e 429extern int rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
a1ac18c4 430 struct in_addr *gate, unsigned int ifindex,
78104b9b 431 vrf_id_t, u_int32_t, safi_t safi);
718e3744 432
78104b9b 433extern struct rib *rib_match_ipv4 (struct in_addr, vrf_id_t);
718e3744 434
78104b9b 435extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *, vrf_id_t);
718e3744 436
78104b9b
FL
437extern void rib_update (vrf_id_t);
438extern void rib_update_static (vrf_id_t);
a1ac18c4 439extern void rib_weed_tables (void);
440extern void rib_sweep_route (void);
441extern void rib_close (void);
442extern void rib_init (void);
7c8ff89e 443extern unsigned long rib_score_proto (u_char proto, u_short instance);
6e26278c
DS
444struct zebra_t;
445extern void rib_queue_add (struct zebra_t *zebra, struct route_node *rn);
446
718e3744 447
a1ac18c4 448extern int
39db97e4 449static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
78104b9b 450 u_char flags, u_short tag, u_char distance, vrf_id_t vrf_id);
718e3744 451
a1ac18c4 452extern int
39db97e4 453static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
78104b9b 454 u_short tag, u_char distance, vrf_id_t vrf_id);
718e3744 455
456#ifdef HAVE_IPV6
a1ac18c4 457extern int
7c8ff89e 458rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
78104b9b
FL
459 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
460 u_int32_t table_id, u_int32_t metric, u_char distance, safi_t safi);
718e3744 461
a1ac18c4 462extern int
7c8ff89e 463rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
78104b9b
FL
464 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
465 u_int32_t table_id, safi_t safi);
718e3744 466
78104b9b 467extern struct rib *rib_lookup_ipv6 (struct in6_addr *, vrf_id_t);
718e3744 468
78104b9b 469extern struct rib *rib_match_ipv6 (struct in6_addr *, vrf_id_t);
718e3744 470
471extern struct route_table *rib_table_ipv6;
472
a1ac18c4 473extern int
718e3744 474static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
0d9551dc 475 const char *ifname, u_char flags, u_short tag,
78104b9b 476 u_char distance, vrf_id_t vrf_id);
718e3744 477
41fc2714 478extern int
8a92a8a0 479rib_add_ipv6_multipath (struct prefix *, struct rib *, safi_t,
41fc2714
DS
480 unsigned long);
481
a1ac18c4 482extern int
718e3744 483static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
0d9551dc 484 const char *ifname, u_short tag, u_char distance,
78104b9b 485 vrf_id_t vrf_id);
718e3744 486
487#endif /* HAVE_IPV6 */
488
9fd92e3c 489extern int rib_gc_dest (struct route_node *rn);
0915bb0c 490extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
9fd92e3c
AS
491
492/*
493 * Inline functions.
494 */
495
1b5ed1b0
AS
496/*
497 * rib_table_info
498 */
499static inline rib_table_info_t *
500rib_table_info (struct route_table *table)
501{
502 return (rib_table_info_t *) table->info;
503}
504
9fd92e3c
AS
505/*
506 * rib_dest_from_rnode
507 */
508static inline rib_dest_t *
509rib_dest_from_rnode (struct route_node *rn)
510{
511 return (rib_dest_t *) rn->info;
512}
513
514/*
515 * rnode_to_ribs
516 *
517 * Returns a pointer to the list of routes corresponding to the given
518 * route_node.
519 */
520static inline struct rib *
521rnode_to_ribs (struct route_node *rn)
522{
523 rib_dest_t *dest;
524
525 dest = rib_dest_from_rnode (rn);
526 if (!dest)
527 return NULL;
528
529 return dest->routes;
530}
531
532/*
533 * rib_dest_prefix
534 */
535static inline struct prefix *
536rib_dest_prefix (rib_dest_t *dest)
537{
538 return &dest->rnode->p;
539}
540
541/*
542 * rib_dest_af
543 *
544 * Returns the address family that the destination is for.
545 */
546static inline u_char
547rib_dest_af (rib_dest_t *dest)
548{
549 return dest->rnode->p.family;
550}
551
552/*
553 * rib_dest_table
554 */
555static inline struct route_table *
556rib_dest_table (rib_dest_t *dest)
557{
558 return dest->rnode->table;
559}
560
1b5ed1b0
AS
561/*
562 * rib_dest_vrf
563 */
b72ede27 564static inline struct zebra_vrf *
1b5ed1b0
AS
565rib_dest_vrf (rib_dest_t *dest)
566{
b72ede27 567 return rib_table_info (rib_dest_table (dest))->zvrf;
1b5ed1b0
AS
568}
569
0915bb0c
AS
570/*
571 * rib_tables_iter_init
572 */
573static inline void
574rib_tables_iter_init (rib_tables_iter_t *iter)
575
576{
577 memset (iter, 0, sizeof (*iter));
578 iter->state = RIB_TABLES_ITER_S_INIT;
579}
580
581/*
582 * rib_tables_iter_started
583 *
584 * Returns TRUE if this iterator has started iterating over the set of
585 * tables.
586 */
587static inline int
588rib_tables_iter_started (rib_tables_iter_t *iter)
589{
590 return iter->state != RIB_TABLES_ITER_S_INIT;
591}
592
593/*
594 * rib_tables_iter_cleanup
595 */
596static inline void
597rib_tables_iter_cleanup (rib_tables_iter_t *iter)
598{
599 iter->state = RIB_TABLES_ITER_S_DONE;
600}
601
718e3744 602#endif /*_ZEBRA_RIB_H */