]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rib.h
lib: Add encode/decode of opaque data
[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 *
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_RIB_H
23#define _ZEBRA_RIB_H
24
dc9ffce8 25#include "zebra.h"
4f8ea50c 26#include "hook.h"
aa57abfb 27#include "typesafe.h"
c6ffe645 28#include "linklist.h"
ec1a4283 29#include "prefix.h"
9fd92e3c 30#include "table.h"
5adc2528 31#include "queue.h"
fb018d25 32#include "nexthop.h"
7ee30f28 33#include "nexthop_group.h"
244a59d2 34#include "vrf.h"
fb148af4 35#include "if.h"
40c7bdb0 36#include "mpls.h"
05737783 37#include "srcdest_table.h"
5463ce26 38#include "zebra/zebra_nhg.h"
ec1a4283 39
51e94aa7
EDP
40#ifdef __cplusplus
41extern "C" {
42#endif
43
f9215571 44enum rnh_type { RNH_NEXTHOP_TYPE, RNH_IMPORT_CHECK_TYPE };
aa57abfb
DS
45
46PREDECL_LIST(rnh_list)
47
48/* Nexthop structure. */
49struct rnh {
50 uint8_t flags;
51
52#define ZEBRA_NHT_CONNECTED 0x1
53#define ZEBRA_NHT_DELETED 0x2
54#define ZEBRA_NHT_EXACT_MATCH 0x4
55
56 /* VRF identifier. */
57 vrf_id_t vrf_id;
58
59 afi_t afi;
60
f9215571 61 enum rnh_type type;
aa57abfb
DS
62
63 uint32_t seqno;
64
65 struct route_entry *state;
66 struct prefix resolved_route;
67 struct list *client_list;
68
69 /* pseudowires dependent on this nh */
70 struct list *zebra_pseudowire_list;
71
72 struct route_node *node;
73
74 /*
75 * if this has been filtered for the client
76 */
77 int filtered[ZEBRA_ROUTE_MAX];
78
79 struct rnh_list_item rnh_list_item;
80};
81
718e3744 82#define DISTANCE_INFINITY 255
7a4bb9c5 83#define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
718e3744 84
045207e2
DS
85PREDECL_LIST(re_list)
86
d62a17ae 87struct route_entry {
88 /* Link list. */
045207e2 89 struct re_list_item next;
d62a17ae 90
0eb97b86
MS
91 /* Nexthop group, shared/refcounted, based on the nexthop(s)
92 * provided by the owner of the route
93 */
94 struct nhg_hash_entry *nhe;
d62a17ae 95
0eb97b86 96 /* Nexthop group from FIB (optional), reflecting what is actually
4db01e79
MS
97 * installed in the FIB if that differs. The 'backup' group is used
98 * when backup nexthops are present in the route's nhg.
0eb97b86 99 */
ee5e8a48 100 struct nexthop_group fib_ng;
4db01e79 101 struct nexthop_group fib_backup_ng;
ee5e8a48 102
8e766379
SW
103 /* Nexthop group hash entry ID */
104 uint32_t nhe_id;
f0cb8e16 105
d62a17ae 106 /* Tag */
107 route_tag_t tag;
108
109 /* Uptime. */
110 time_t uptime;
111
1d48702e 112 /* Type of this route. */
d62a17ae 113 int type;
114
d62a17ae 115 /* VRF identifier. */
116 vrf_id_t vrf_id;
117
118 /* Which routing table */
119 uint32_t table;
120
121 /* Metric */
d7c0a89a 122 uint32_t metric;
d62a17ae 123
124 /* MTU */
d7c0a89a
QY
125 uint32_t mtu;
126 uint32_t nexthop_mtu;
d62a17ae 127
d62a17ae 128 /* Flags of this route.
129 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
130 * to clients via Zserv
131 */
d7c0a89a 132 uint32_t flags;
d62a17ae 133
134 /* RIB internal status */
88f7ea31 135 uint32_t status;
f0f77c9a 136#define ROUTE_ENTRY_REMOVED 0x1
88f7ea31 137/* The Route Entry has changed */
42fc558e 138#define ROUTE_ENTRY_CHANGED 0x2
88f7ea31 139/* The Label has changed on the Route entry */
42fc558e 140#define ROUTE_ENTRY_LABELS_CHANGED 0x4
88f7ea31 141/* Route is queued for Installation into the Data Plane */
42fc558e 142#define ROUTE_ENTRY_QUEUED 0x8
88f7ea31 143/* Route is installed into the Data Plane */
42fc558e 144#define ROUTE_ENTRY_INSTALLED 0x10
88f7ea31 145/* Route has Failed installation into the Data Plane in some manner */
42fc558e 146#define ROUTE_ENTRY_FAILED 0x20
7483dcbe
MS
147/* Route has a 'fib' set of nexthops, probably because the installed set
148 * differs from the rib/normal set of nexthops.
149 */
150#define ROUTE_ENTRY_USE_FIB_NHG 0x40
6d691129 151
7cdb1a84
MS
152 /* Sequence value incremented for each dataplane operation */
153 uint32_t dplane_sequence;
7a741326
DS
154
155 /* Source protocol instance */
156 uint16_t instance;
157
158 /* Distance. */
159 uint8_t distance;
718e3744 160};
161
66cb1845 162#define RIB_SYSTEM_ROUTE(R) RSYSTEM_ROUTE((R)->type)
84340a15 163
66cb1845 164#define RIB_KERNEL_ROUTE(R) RKERNEL_ROUTE((R)->type)
84340a15 165
e96f9203 166/* meta-queue structure:
e22e8001 167 * sub-queue 0: nexthop group objects
c17b2d5b
DS
168 * sub-queue 1: connected
169 * sub-queue 2: kernel
170 * sub-queue 3: static
171 * sub-queue 4: RIP, RIPng, OSPF, OSPF6, IS-IS, EIGRP, NHRP
172 * sub-queue 5: iBGP, eBGP
173 * sub-queue 6: any other origin (if any) typically those that
174 * don't generate routes
e96f9203 175 */
c17b2d5b 176#define MQ_SIZE 7
d62a17ae 177struct meta_queue {
178 struct list *subq[MQ_SIZE];
d7c0a89a 179 uint32_t size; /* sum of lengths of all subqueues */
e96f9203
DO
180};
181
9fd92e3c
AS
182/*
183 * Structure that represents a single destination (prefix).
184 */
d62a17ae 185typedef struct rib_dest_t_ {
9fd92e3c 186
d62a17ae 187 /*
188 * Back pointer to the route node for this destination. This helps
189 * us get to the prefix that this structure is for.
190 */
191 struct route_node *rnode;
9fd92e3c 192
d62a17ae 193 /*
194 * Doubly-linked list of routes for this prefix.
195 */
045207e2 196 struct re_list_head routes;
9fd92e3c 197
5f7a4718
DS
198 struct route_entry *selected_fib;
199
d62a17ae 200 /*
201 * Flags, see below.
202 */
d7c0a89a 203 uint32_t flags;
9fd92e3c 204
699dae23
DS
205 /*
206 * The list of nht prefixes that have ended up
207 * depending on this route node.
208 * After route processing is returned from
209 * the data plane we will run evaluate_rnh
210 * on these prefixes.
211 */
aa57abfb 212 struct rnh_list_head nht;
699dae23 213
d62a17ae 214 /*
215 * Linkage to put dest on the FPM processing queue.
216 */
217 TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
5adc2528 218
9fd92e3c
AS
219} rib_dest_t;
220
aa57abfb 221DECLARE_LIST(rnh_list, struct rnh, rnh_list_item);
045207e2 222DECLARE_LIST(re_list, struct route_entry, next);
aa57abfb 223
9fd92e3c 224#define RIB_ROUTE_QUEUED(x) (1 << (x))
1ca60f2c 225// If MQ_SIZE is modified this value needs to be updated.
e22e8001 226#define RIB_ROUTE_ANY_QUEUED 0x3F
9fd92e3c
AS
227
228/*
229 * The maximum qindex that can be used.
230 */
231#define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
232
5adc2528
AS
233/*
234 * This flag indicates that a given prefix has been 'advertised' to
235 * the FPM to be installed in the forwarding plane.
236 */
237#define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
238
239/*
240 * This flag is set when we need to send an update to the FPM about a
241 * dest.
242 */
243#define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
244
a1494c25
DS
245#define RIB_DEST_UPDATE_LSPS (1 << (ZEBRA_MAX_QINDEX + 3))
246
9fd92e3c
AS
247/*
248 * Macro to iterate over each route for a destination (prefix).
249 */
d62a17ae 250#define RE_DEST_FOREACH_ROUTE(dest, re) \
045207e2
DS
251 for ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL; (re); \
252 (re) = re_list_next(&((dest)->routes), (re)))
9fd92e3c
AS
253
254/*
255 * Same as above, but allows the current node to be unlinked.
256 */
d62a17ae 257#define RE_DEST_FOREACH_ROUTE_SAFE(dest, re, next) \
045207e2
DS
258 for ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL; \
259 (re) && ((next) = re_list_next(&((dest)->routes), (re)), 1); \
260 (re) = (next))
9fd92e3c 261
db856683
CS
262#define RE_DEST_FIRST_ROUTE(dest, re) \
263 ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL)
264
265#define RE_DEST_NEXT_ROUTE(dest, re) \
266 ((re) = (dest) ? re_list_next(&((dest)->routes), (re)) : NULL)
267
d62a17ae 268#define RNODE_FOREACH_RE(rn, re) \
996c9314 269 RE_DEST_FOREACH_ROUTE (rib_dest_from_rnode(rn), re)
9fd92e3c 270
d62a17ae 271#define RNODE_FOREACH_RE_SAFE(rn, re, next) \
996c9314 272 RE_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode(rn), re, next)
9fd92e3c 273
db856683
CS
274#define RNODE_FIRST_RE(rn, re) RE_DEST_FIRST_ROUTE(rib_dest_from_rnode(rn), re)
275
276#define RNODE_NEXT_RE(rn, re) RE_DEST_NEXT_ROUTE(rib_dest_from_rnode(rn), re)
277
d62a17ae 278#if defined(HAVE_RTADV)
cd80d74f 279/* Structure which hold status of router advertisement. */
d62a17ae 280struct rtadv {
281 int sock;
cd80d74f 282
d62a17ae 283 int adv_if_count;
284 int adv_msec_if_count;
cd80d74f 285
d62a17ae 286 struct thread *ra_read;
287 struct thread *ra_timer;
cd80d74f 288};
8da4e946 289#endif /* HAVE_RTADV */
cd80d74f 290
1b5ed1b0
AS
291/*
292 * rib_table_info_t
293 *
294 * Structure that is hung off of a route_table that holds information about
295 * the table.
296 */
630d5962 297struct rib_table_info {
1b5ed1b0 298
d62a17ae 299 /*
300 * Back pointer to zebra_vrf.
301 */
302 struct zebra_vrf *zvrf;
303 afi_t afi;
304 safi_t safi;
b62983cf 305 uint32_t table_id;
630d5962 306};
1b5ed1b0 307
4d5647d0 308enum rib_tables_iter_state {
d62a17ae 309 RIB_TABLES_ITER_S_INIT,
310 RIB_TABLES_ITER_S_ITERATING,
311 RIB_TABLES_ITER_S_DONE
4d5647d0 312};
0915bb0c
AS
313
314/*
315 * Structure that holds state for iterating over all tables in the
316 * Routing Information Base.
317 */
d62a17ae 318typedef struct rib_tables_iter_t_ {
319 vrf_id_t vrf_id;
320 int afi_safi_ix;
0915bb0c 321
4d5647d0 322 enum rib_tables_iter_state state;
0915bb0c
AS
323} rib_tables_iter_t;
324
1c848137 325/* Events/reasons triggering a RIB update. */
91e6f25b 326enum rib_update_event {
2a99ab95 327 RIB_UPDATE_KERNEL,
d62a17ae 328 RIB_UPDATE_RMAP_CHANGE,
2a99ab95
SW
329 RIB_UPDATE_OTHER,
330 RIB_UPDATE_MAX
91e6f25b 331};
1c848137 332
d62a17ae 333extern void route_entry_copy_nexthops(struct route_entry *re,
334 struct nexthop *nh);
cd7108ba
EDP
335int route_entry_update_nhe(struct route_entry *re,
336 struct nhg_hash_entry *new_nhghe);
f0f77c9a 337
1f655680
SW
338/* NHG replace has happend, we have to update route_entry pointers to new one */
339void rib_handle_nhg_replace(struct nhg_hash_entry *old,
340 struct nhg_hash_entry *new);
341
f0f77c9a 342#define route_entry_dump(prefix, src, re) _route_entry_dump(__func__, prefix, src, re)
072834ca
DS
343extern void _route_entry_dump(const char *func, union prefixconstptr pp,
344 union prefixconstptr src_pp,
345 const struct route_entry *re);
4623d897 346
072834ca
DS
347extern void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id);
348extern void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id);
f0f77c9a 349
dc95824a
DO
350#define ZEBRA_RIB_LOOKUP_ERROR -1
351#define ZEBRA_RIB_FOUND_EXACT 0
352#define ZEBRA_RIB_FOUND_NOGATE 1
353#define ZEBRA_RIB_FOUND_CONNECTED 2
354#define ZEBRA_RIB_NOTFOUND 3
355
d7c0a89a
QY
356extern int is_zebra_valid_kernel_table(uint32_t table_id);
357extern int is_zebra_main_routing_table(uint32_t table_id);
86391e56 358extern int zebra_check_addr(const struct prefix *p);
718e3744 359
d62a17ae 360extern void rib_delnode(struct route_node *rn, struct route_entry *re);
0c555cc6
DS
361extern void rib_install_kernel(struct route_node *rn, struct route_entry *re,
362 struct route_entry *old);
363extern void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re);
28f6dde8 364
d24af186 365/* NOTE:
b4c034b0 366 * All rib_add function will not just add prefix into RIB, but
d24af186 367 * also implicitly withdraw equal prefix of same type. */
4a7371e9 368extern int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
acde7f6b 369 unsigned short instance, uint32_t flags, struct prefix *p,
fd36be7e 370 struct prefix_ipv6 *src_p, const struct nexthop *nh,
8032b717
SW
371 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
372 uint32_t mtu, uint8_t distance, route_tag_t tag);
1d48702e
MS
373/*
374 * Multipath route apis.
375 */
072834ca 376extern int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
0eb97b86
MS
377 struct prefix_ipv6 *src_p, struct route_entry *re,
378 struct nexthop_group *ng);
1d48702e
MS
379extern int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
380 struct prefix_ipv6 *src_p,
381 struct route_entry *re,
382 struct nhg_hash_entry *nhe);
d62a17ae 383
384extern void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
acde7f6b
DS
385 unsigned short instance, uint32_t flags,
386 struct prefix *p, struct prefix_ipv6 *src_p,
387 const struct nexthop *nh, uint32_t nhe_id,
388 uint32_t table_id, uint32_t metric, uint8_t distance,
389 bool fromkernel, bool connected_down);
d62a17ae 390
072834ca
DS
391extern struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
392 union g_addr *addr,
d62a17ae 393 struct route_node **rn_out);
394extern struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
395 struct in_addr addr,
396 struct route_node **rn_out);
397
072834ca
DS
398extern struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p,
399 vrf_id_t vrf_id);
d62a17ae 400
91e6f25b
DS
401extern void rib_update(enum rib_update_event event);
402extern void rib_update_vrf(vrf_id_t vrf_id, enum rib_update_event event);
d5b8c216 403extern void rib_update_table(struct route_table *table,
91e6f25b 404 enum rib_update_event event);
d4644d41 405extern int rib_sweep_route(struct thread *t);
072834ca
DS
406extern void rib_sweep_table(struct route_table *table);
407extern void rib_close_table(struct route_table *table);
d62a17ae 408extern void rib_init(void);
d7c0a89a
QY
409extern unsigned long rib_score_proto(uint8_t proto, unsigned short instance);
410extern unsigned long rib_score_proto_table(uint8_t proto,
411 unsigned short instance,
47a08aa9 412 struct route_table *table);
e22e8001
SW
413
414extern int rib_queue_add(struct route_node *rn);
415
416struct nhg_ctx; /* Forward declaration */
417
418extern int rib_queue_nhg_add(struct nhg_ctx *ctx);
419
d62a17ae 420extern void meta_queue_free(struct meta_queue *mq);
421extern int zebra_rib_labeled_unicast(struct route_entry *re);
718e3744 422extern struct route_table *rib_table_ipv6;
423
072834ca 424extern void rib_unlink(struct route_node *rn, struct route_entry *re);
d62a17ae 425extern int rib_gc_dest(struct route_node *rn);
426extern struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter);
9fd92e3c 427
fd289fc8 428extern uint8_t route_distance(int type);
40c7bdb0 429
699dae23
DS
430extern void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq);
431
9fd92e3c
AS
432/*
433 * Inline functions.
434 */
435
1b5ed1b0
AS
436/*
437 * rib_table_info
438 */
630d5962 439static inline struct rib_table_info *rib_table_info(struct route_table *table)
1b5ed1b0 440{
630d5962 441 return (struct rib_table_info *)route_table_get_info(table);
1b5ed1b0
AS
442}
443
9fd92e3c
AS
444/*
445 * rib_dest_from_rnode
446 */
d62a17ae 447static inline rib_dest_t *rib_dest_from_rnode(struct route_node *rn)
9fd92e3c 448{
d62a17ae 449 return (rib_dest_t *)rn->info;
9fd92e3c
AS
450}
451
452/*
453 * rnode_to_ribs
454 *
455 * Returns a pointer to the list of routes corresponding to the given
456 * route_node.
457 */
d62a17ae 458static inline struct route_entry *rnode_to_ribs(struct route_node *rn)
9fd92e3c 459{
d62a17ae 460 rib_dest_t *dest;
9fd92e3c 461
d62a17ae 462 dest = rib_dest_from_rnode(rn);
463 if (!dest)
464 return NULL;
9fd92e3c 465
045207e2 466 return re_list_first(&dest->routes);
9fd92e3c
AS
467}
468
469/*
470 * rib_dest_prefix
471 */
d62a17ae 472static inline struct prefix *rib_dest_prefix(rib_dest_t *dest)
9fd92e3c 473{
d62a17ae 474 return &dest->rnode->p;
9fd92e3c
AS
475}
476
477/*
478 * rib_dest_af
479 *
480 * Returns the address family that the destination is for.
481 */
d7c0a89a 482static inline uint8_t rib_dest_af(rib_dest_t *dest)
9fd92e3c 483{
d62a17ae 484 return dest->rnode->p.family;
9fd92e3c
AS
485}
486
487/*
488 * rib_dest_table
489 */
d62a17ae 490static inline struct route_table *rib_dest_table(rib_dest_t *dest)
9fd92e3c 491{
d62a17ae 492 return srcdest_rnode_table(dest->rnode);
9fd92e3c
AS
493}
494
1b5ed1b0
AS
495/*
496 * rib_dest_vrf
497 */
d62a17ae 498static inline struct zebra_vrf *rib_dest_vrf(rib_dest_t *dest)
1b5ed1b0 499{
d62a17ae 500 return rib_table_info(rib_dest_table(dest))->zvrf;
1b5ed1b0
AS
501}
502
434434f7
DS
503/*
504 * Create the rib_dest_t and attach it to the specified node
505 */
506extern rib_dest_t *zebra_rib_create_dest(struct route_node *rn);
507
0915bb0c
AS
508/*
509 * rib_tables_iter_init
510 */
d62a17ae 511static inline void rib_tables_iter_init(rib_tables_iter_t *iter)
0915bb0c
AS
512
513{
d62a17ae 514 memset(iter, 0, sizeof(*iter));
515 iter->state = RIB_TABLES_ITER_S_INIT;
0915bb0c
AS
516}
517
518/*
519 * rib_tables_iter_started
520 *
2951a7a4 521 * Returns true if this iterator has started iterating over the set of
0915bb0c
AS
522 * tables.
523 */
d62a17ae 524static inline int rib_tables_iter_started(rib_tables_iter_t *iter)
0915bb0c 525{
d62a17ae 526 return iter->state != RIB_TABLES_ITER_S_INIT;
0915bb0c
AS
527}
528
529/*
530 * rib_tables_iter_cleanup
531 */
d62a17ae 532static inline void rib_tables_iter_cleanup(rib_tables_iter_t *iter)
0915bb0c 533{
d62a17ae 534 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c
AS
535}
536
d62a17ae 537DECLARE_HOOK(rib_update, (struct route_node * rn, const char *reason),
538 (rn, reason))
4f8ea50c 539
ee5e8a48 540/*
7483dcbe
MS
541 * Access installed/fib nexthops, which may be a subset of the
542 * rib nexthops.
ee5e8a48 543 */
4db01e79 544static inline struct nexthop_group *rib_get_fib_nhg(struct route_entry *re)
ee5e8a48 545{
7483dcbe
MS
546 /* If the fib set is a subset of the active rib set,
547 * use the dedicated fib list.
548 */
549 if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG))
ee5e8a48
MS
550 return &(re->fib_ng);
551 else
c415d895 552 return &(re->nhe->nhg);
ee5e8a48 553}
05f7f5db 554
4db01e79 555/*
7483dcbe
MS
556 * Access backup nexthop-group that represents the installed backup nexthops;
557 * any installed backup will be on the fib list.
4db01e79
MS
558 */
559static inline struct nexthop_group *rib_get_fib_backup_nhg(
560 struct route_entry *re)
561{
7483dcbe 562 return &(re->fib_backup_ng);
4db01e79
MS
563}
564
05f7f5db 565extern void zebra_vty_init(void);
4060008b 566
05f7f5db
DS
567extern pid_t pid;
568
6b093863 569extern bool v6_rr_semantics;
51e94aa7
EDP
570
571#ifdef __cplusplus
572}
573#endif
574
718e3744 575#endif /*_ZEBRA_RIB_H */