2 * Routing Information Base header
3 * Copyright (C) 1997 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
33 #include "nexthop_group.h"
37 #include "srcdest_table.h"
38 #include "zebra/zebra_nhg.h"
44 typedef enum { RNH_NEXTHOP_TYPE
, RNH_IMPORT_CHECK_TYPE
} rnh_type_t
;
46 PREDECL_LIST(rnh_list
)
48 /* Nexthop structure. */
52 #define ZEBRA_NHT_CONNECTED 0x1
53 #define ZEBRA_NHT_DELETED 0x2
54 #define ZEBRA_NHT_EXACT_MATCH 0x4
65 struct route_entry
*state
;
66 struct prefix resolved_route
;
67 struct list
*client_list
;
69 /* pseudowires dependent on this nh */
70 struct list
*zebra_pseudowire_list
;
72 struct route_node
*node
;
75 * if this has been filtered for the client
77 int filtered
[ZEBRA_ROUTE_MAX
];
79 struct rnh_list_item rnh_list_item
;
82 #define DISTANCE_INFINITY 255
83 #define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
89 struct re_list_item next
;
91 /* Nexthop group, shared/refcounted, based on the nexthop(s)
92 * provided by the owner of the route
94 struct nhg_hash_entry
*nhe
;
96 /* Nexthop group from FIB (optional), reflecting what is actually
97 * installed in the FIB if that differs.
99 struct nexthop_group fib_ng
;
101 /* Nexthop group hash entry ID */
110 /* Type fo this route. */
113 /* VRF identifier. */
116 /* Which routing table */
124 uint32_t nexthop_mtu
;
126 /* Flags of this route.
127 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
128 * to clients via Zserv
132 /* RIB internal status */
134 #define ROUTE_ENTRY_REMOVED 0x1
135 /* The Route Entry has changed */
136 #define ROUTE_ENTRY_CHANGED 0x2
137 /* The Label has changed on the Route entry */
138 #define ROUTE_ENTRY_LABELS_CHANGED 0x4
139 /* Route is queued for Installation into the Data Plane */
140 #define ROUTE_ENTRY_QUEUED 0x8
141 /* Route is installed into the Data Plane */
142 #define ROUTE_ENTRY_INSTALLED 0x10
143 /* Route has Failed installation into the Data Plane in some manner */
144 #define ROUTE_ENTRY_FAILED 0x20
146 /* Sequence value incremented for each dataplane operation */
147 uint32_t dplane_sequence
;
149 /* Source protocol instance */
156 #define RIB_SYSTEM_ROUTE(R) RSYSTEM_ROUTE((R)->type)
158 #define RIB_KERNEL_ROUTE(R) RKERNEL_ROUTE((R)->type)
160 /* meta-queue structure:
161 * sub-queue 0: nexthop group objects
162 * sub-queue 1: connected, kernel
163 * sub-queue 2: static
164 * sub-queue 3: RIP, RIPng, OSPF, OSPF6, IS-IS, EIGRP, NHRP
165 * sub-queue 4: iBGP, eBGP
166 * sub-queue 5: any other origin (if any)
170 struct list
*subq
[MQ_SIZE
];
171 uint32_t size
; /* sum of lengths of all subqueues */
175 * Structure that represents a single destination (prefix).
177 typedef struct rib_dest_t_
{
180 * Back pointer to the route node for this destination. This helps
181 * us get to the prefix that this structure is for.
183 struct route_node
*rnode
;
186 * Doubly-linked list of routes for this prefix.
188 struct re_list_head routes
;
190 struct route_entry
*selected_fib
;
198 * The list of nht prefixes that have ended up
199 * depending on this route node.
200 * After route processing is returned from
201 * the data plane we will run evaluate_rnh
204 struct rnh_list_head nht
;
207 * Linkage to put dest on the FPM processing queue.
209 TAILQ_ENTRY(rib_dest_t_
) fpm_q_entries
;
213 DECLARE_LIST(rnh_list
, struct rnh
, rnh_list_item
);
214 DECLARE_LIST(re_list
, struct route_entry
, next
);
216 #define RIB_ROUTE_QUEUED(x) (1 << (x))
217 // If MQ_SIZE is modified this value needs to be updated.
218 #define RIB_ROUTE_ANY_QUEUED 0x3F
221 * The maximum qindex that can be used.
223 #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
226 * This flag indicates that a given prefix has been 'advertised' to
227 * the FPM to be installed in the forwarding plane.
229 #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
232 * This flag is set when we need to send an update to the FPM about a
235 #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
237 #define RIB_DEST_UPDATE_LSPS (1 << (ZEBRA_MAX_QINDEX + 3))
240 * Macro to iterate over each route for a destination (prefix).
242 #define RE_DEST_FOREACH_ROUTE(dest, re) \
243 for ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL; (re); \
244 (re) = re_list_next(&((dest)->routes), (re)))
247 * Same as above, but allows the current node to be unlinked.
249 #define RE_DEST_FOREACH_ROUTE_SAFE(dest, re, next) \
250 for ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL; \
251 (re) && ((next) = re_list_next(&((dest)->routes), (re)), 1); \
254 #define RNODE_FOREACH_RE(rn, re) \
255 RE_DEST_FOREACH_ROUTE (rib_dest_from_rnode(rn), re)
257 #define RNODE_FOREACH_RE_SAFE(rn, re, next) \
258 RE_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode(rn), re, next)
260 #if defined(HAVE_RTADV)
261 /* Structure which hold status of router advertisement. */
266 int adv_msec_if_count
;
268 struct thread
*ra_read
;
269 struct thread
*ra_timer
;
271 #endif /* HAVE_RTADV */
276 * Structure that is hung off of a route_table that holds information about
279 typedef struct rib_table_info_t_
{
282 * Back pointer to zebra_vrf.
284 struct zebra_vrf
*zvrf
;
291 RIB_TABLES_ITER_S_INIT
,
292 RIB_TABLES_ITER_S_ITERATING
,
293 RIB_TABLES_ITER_S_DONE
294 } rib_tables_iter_state_t
;
297 * Structure that holds state for iterating over all tables in the
298 * Routing Information Base.
300 typedef struct rib_tables_iter_t_
{
304 rib_tables_iter_state_t state
;
307 /* Events/reasons triggering a RIB update. */
310 RIB_UPDATE_RMAP_CHANGE
,
313 } rib_update_event_t
;
315 extern void route_entry_copy_nexthops(struct route_entry
*re
,
317 int route_entry_update_nhe(struct route_entry
*re
, struct nhg_hash_entry
*new);
319 #define route_entry_dump(prefix, src, re) _route_entry_dump(__func__, prefix, src, re)
320 extern void _route_entry_dump(const char *func
, union prefixconstptr pp
,
321 union prefixconstptr src_pp
,
322 const struct route_entry
*re
);
324 extern void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
);
325 extern void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
);
327 #define ZEBRA_RIB_LOOKUP_ERROR -1
328 #define ZEBRA_RIB_FOUND_EXACT 0
329 #define ZEBRA_RIB_FOUND_NOGATE 1
330 #define ZEBRA_RIB_FOUND_CONNECTED 2
331 #define ZEBRA_RIB_NOTFOUND 3
333 extern int is_zebra_valid_kernel_table(uint32_t table_id
);
334 extern int is_zebra_main_routing_table(uint32_t table_id
);
335 extern int zebra_check_addr(const struct prefix
*p
);
337 extern void rib_delnode(struct route_node
*rn
, struct route_entry
*re
);
338 extern void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
339 struct route_entry
*old
);
340 extern void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
);
343 * All rib_add function will not just add prefix into RIB, but
344 * also implicitly withdraw equal prefix of same type. */
345 extern int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
346 unsigned short instance
, int flags
, struct prefix
*p
,
347 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
348 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
349 uint32_t mtu
, uint8_t distance
, route_tag_t tag
);
351 extern int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
352 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
353 struct nexthop_group
*ng
);
355 extern void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
356 unsigned short instance
, int flags
, struct prefix
*p
,
357 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
358 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
359 uint8_t distance
, bool fromkernel
);
361 extern struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
363 struct route_node
**rn_out
);
364 extern struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
366 struct route_node
**rn_out
);
368 extern struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
,
371 extern void rib_update(rib_update_event_t event
);
372 extern void rib_update_vrf(vrf_id_t vrf_id
, rib_update_event_t event
);
373 extern void rib_update_table(struct route_table
*table
,
374 rib_update_event_t event
);
375 extern int rib_sweep_route(struct thread
*t
);
376 extern void rib_sweep_table(struct route_table
*table
);
377 extern void rib_close_table(struct route_table
*table
);
378 extern void rib_init(void);
379 extern unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
);
380 extern unsigned long rib_score_proto_table(uint8_t proto
,
381 unsigned short instance
,
382 struct route_table
*table
);
384 extern int rib_queue_add(struct route_node
*rn
);
386 struct nhg_ctx
; /* Forward declaration */
388 extern int rib_queue_nhg_add(struct nhg_ctx
*ctx
);
390 extern void meta_queue_free(struct meta_queue
*mq
);
391 extern int zebra_rib_labeled_unicast(struct route_entry
*re
);
392 extern struct route_table
*rib_table_ipv6
;
394 extern void rib_unlink(struct route_node
*rn
, struct route_entry
*re
);
395 extern int rib_gc_dest(struct route_node
*rn
);
396 extern struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
);
398 extern uint8_t route_distance(int type
);
400 extern void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
);
409 static inline rib_table_info_t
*rib_table_info(struct route_table
*table
)
411 return (rib_table_info_t
*)route_table_get_info(table
);
415 * rib_dest_from_rnode
417 static inline rib_dest_t
*rib_dest_from_rnode(struct route_node
*rn
)
419 return (rib_dest_t
*)rn
->info
;
425 * Returns a pointer to the list of routes corresponding to the given
428 static inline struct route_entry
*rnode_to_ribs(struct route_node
*rn
)
432 dest
= rib_dest_from_rnode(rn
);
436 return re_list_first(&dest
->routes
);
442 static inline struct prefix
*rib_dest_prefix(rib_dest_t
*dest
)
444 return &dest
->rnode
->p
;
450 * Returns the address family that the destination is for.
452 static inline uint8_t rib_dest_af(rib_dest_t
*dest
)
454 return dest
->rnode
->p
.family
;
460 static inline struct route_table
*rib_dest_table(rib_dest_t
*dest
)
462 return srcdest_rnode_table(dest
->rnode
);
468 static inline struct zebra_vrf
*rib_dest_vrf(rib_dest_t
*dest
)
470 return rib_table_info(rib_dest_table(dest
))->zvrf
;
474 * Create the rib_dest_t and attach it to the specified node
476 extern rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
);
479 * rib_tables_iter_init
481 static inline void rib_tables_iter_init(rib_tables_iter_t
*iter
)
484 memset(iter
, 0, sizeof(*iter
));
485 iter
->state
= RIB_TABLES_ITER_S_INIT
;
489 * rib_tables_iter_started
491 * Returns true if this iterator has started iterating over the set of
494 static inline int rib_tables_iter_started(rib_tables_iter_t
*iter
)
496 return iter
->state
!= RIB_TABLES_ITER_S_INIT
;
500 * rib_tables_iter_cleanup
502 static inline void rib_tables_iter_cleanup(rib_tables_iter_t
*iter
)
504 iter
->state
= RIB_TABLES_ITER_S_DONE
;
507 DECLARE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
511 * Access active nexthop-group, either RIB or FIB version
513 static inline struct nexthop_group
*rib_active_nhg(struct route_entry
*re
)
515 if (re
->fib_ng
.nexthop
)
516 return &(re
->fib_ng
);
518 return &(re
->nhe
->nhg
);
521 extern void zebra_vty_init(void);
525 extern bool v6_rr_semantics
;
531 #endif /*_ZEBRA_RIB_H */