]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rib.h
lib: msg: refactor common connection code from mgmtd
[mirror_frr.git] / zebra / rib.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Routing Information Base header
4 * Copyright (C) 1997 Kunihiro Ishiguro
5 */
6
7 #ifndef _ZEBRA_RIB_H
8 #define _ZEBRA_RIB_H
9
10 #include "zebra.h"
11 #include "memory.h"
12 #include "hook.h"
13 #include "typesafe.h"
14 #include "linklist.h"
15 #include "prefix.h"
16 #include "table.h"
17 #include "queue.h"
18 #include "nexthop.h"
19 #include "nexthop_group.h"
20 #include "vrf.h"
21 #include "if.h"
22 #include "mpls.h"
23 #include "srcdest_table.h"
24 #include "zebra/zebra_nhg.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 DECLARE_MGROUP(ZEBRA);
31
32 DECLARE_MTYPE(RE);
33
34 PREDECL_LIST(rnh_list);
35
36 /* Nexthop structure. */
37 struct rnh {
38 uint8_t flags;
39
40 #define ZEBRA_NHT_CONNECTED 0x1
41 #define ZEBRA_NHT_DELETED 0x2
42 #define ZEBRA_NHT_RESOLVE_VIA_DEFAULT 0x4
43
44 /* VRF identifier. */
45 vrf_id_t vrf_id;
46
47 afi_t afi;
48 safi_t safi;
49
50 uint32_t seqno;
51
52 struct route_entry *state;
53 struct prefix resolved_route;
54 struct list *client_list;
55
56 /* pseudowires dependent on this nh */
57 struct list *zebra_pseudowire_list;
58
59 struct route_node *node;
60
61 /*
62 * if this has been filtered for the client
63 */
64 int filtered[ZEBRA_ROUTE_MAX];
65
66 struct rnh_list_item rnh_list_item;
67 };
68
69 #define DISTANCE_INFINITY 255
70 #define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
71
72 PREDECL_LIST(re_list);
73
74 struct re_opaque {
75 uint16_t length;
76 uint8_t data[];
77 };
78
79 struct route_entry {
80 /* Link list. */
81 struct re_list_item next;
82
83 /* Nexthop group, shared/refcounted, based on the nexthop(s)
84 * provided by the owner of the route
85 */
86 struct nhg_hash_entry *nhe;
87
88 /* Nexthop group from FIB (optional), reflecting what is actually
89 * installed in the FIB if that differs. The 'backup' group is used
90 * when backup nexthops are present in the route's nhg.
91 */
92 struct nexthop_group fib_ng;
93 struct nexthop_group fib_backup_ng;
94
95 /* Nexthop group hash entry IDs. The "installed" id is the id
96 * used in linux/netlink, if available.
97 */
98 uint32_t nhe_id;
99 uint32_t nhe_installed_id;
100
101 /* Tag */
102 route_tag_t tag;
103
104 /* Uptime. */
105 time_t uptime;
106
107 /* Type of this route. */
108 int type;
109
110 /* VRF identifier. */
111 vrf_id_t vrf_id;
112
113 /* Which routing table */
114 uint32_t table;
115
116 /* Metric */
117 uint32_t metric;
118
119 /* MTU */
120 uint32_t mtu;
121 uint32_t nexthop_mtu;
122
123 /* Flags of this route.
124 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
125 * to clients via Zserv
126 */
127 uint32_t flags;
128
129 /* RIB internal status */
130 uint32_t status;
131 #define ROUTE_ENTRY_REMOVED 0x1
132 /* The Route Entry has changed */
133 #define ROUTE_ENTRY_CHANGED 0x2
134 /* The Label has changed on the Route entry */
135 #define ROUTE_ENTRY_LABELS_CHANGED 0x4
136 /* Route is queued for Installation into the Data Plane */
137 #define ROUTE_ENTRY_QUEUED 0x8
138 /* Route is installed into the Data Plane */
139 #define ROUTE_ENTRY_INSTALLED 0x10
140 /* Route has Failed installation into the Data Plane in some manner */
141 #define ROUTE_ENTRY_FAILED 0x20
142 /* Route has a 'fib' set of nexthops, probably because the installed set
143 * differs from the rib/normal set of nexthops.
144 */
145 #define ROUTE_ENTRY_USE_FIB_NHG 0x40
146 /*
147 * Route entries that are going to the dplane for a Route Replace
148 * let's note the fact that this is happening. This will
149 * be useful when zebra is determing if a route can be
150 * used for nexthops
151 */
152 #define ROUTE_ENTRY_ROUTE_REPLACING 0x80
153
154 /* Sequence value incremented for each dataplane operation */
155 uint32_t dplane_sequence;
156
157 /* Source protocol instance */
158 uint16_t instance;
159
160 /* Distance. */
161 uint8_t distance;
162
163 struct re_opaque *opaque;
164 };
165
166 #define RIB_SYSTEM_ROUTE(R) RSYSTEM_ROUTE((R)->type)
167
168 #define RIB_KERNEL_ROUTE(R) RKERNEL_ROUTE((R)->type)
169
170 /* meta-queue structure:
171 * sub-queue 0: nexthop group objects
172 * sub-queue 1: EVPN/VxLAN objects
173 * sub-queue 2: Early Route Processing
174 * sub-queue 3: Early Label Processing
175 * sub-queue 4: connected
176 * sub-queue 5: kernel
177 * sub-queue 6: static
178 * sub-queue 7: RIP, RIPng, OSPF, OSPF6, IS-IS, EIGRP, NHRP
179 * sub-queue 8: iBGP, eBGP
180 * sub-queue 9: any other origin (if any) typically those that
181 * don't generate routes
182 */
183 #define MQ_SIZE 11
184 struct meta_queue {
185 struct list *subq[MQ_SIZE];
186 uint32_t size; /* sum of lengths of all subqueues */
187 };
188
189 /*
190 * Structure that represents a single destination (prefix).
191 */
192 typedef struct rib_dest_t_ {
193
194 /*
195 * Back pointer to the route node for this destination. This helps
196 * us get to the prefix that this structure is for.
197 */
198 struct route_node *rnode;
199
200 /*
201 * Doubly-linked list of routes for this prefix.
202 */
203 struct re_list_head routes;
204
205 struct route_entry *selected_fib;
206
207 /*
208 * Flags, see below.
209 */
210 uint32_t flags;
211
212 /*
213 * The list of nht prefixes that have ended up
214 * depending on this route node.
215 * After route processing is returned from
216 * the data plane we will run evaluate_rnh
217 * on these prefixes.
218 */
219 struct rnh_list_head nht;
220
221 /*
222 * Linkage to put dest on the FPM processing queue.
223 */
224 TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
225
226 } rib_dest_t;
227
228 DECLARE_LIST(rnh_list, struct rnh, rnh_list_item);
229 DECLARE_LIST(re_list, struct route_entry, next);
230
231 #define RIB_ROUTE_QUEUED(x) (1 << (x))
232 // If MQ_SIZE is modified this value needs to be updated.
233 #define RIB_ROUTE_ANY_QUEUED 0x3F
234
235 /*
236 * The maximum qindex that can be used.
237 */
238 #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
239
240 /*
241 * This flag indicates that a given prefix has been 'advertised' to
242 * the FPM to be installed in the forwarding plane.
243 */
244 #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
245
246 /*
247 * This flag is set when we need to send an update to the FPM about a
248 * dest.
249 */
250 #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
251
252 #define RIB_DEST_UPDATE_LSPS (1 << (ZEBRA_MAX_QINDEX + 3))
253
254 /*
255 * Macro to iterate over each route for a destination (prefix).
256 */
257 #define RE_DEST_FOREACH_ROUTE(dest, re) \
258 for ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL; (re); \
259 (re) = re_list_next(&((dest)->routes), (re)))
260
261 /*
262 * Same as above, but allows the current node to be unlinked.
263 */
264 #define RE_DEST_FOREACH_ROUTE_SAFE(dest, re, next) \
265 for ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL; \
266 (re) && ((next) = re_list_next(&((dest)->routes), (re)), 1); \
267 (re) = (next))
268
269 #define RE_DEST_FIRST_ROUTE(dest, re) \
270 ((re) = (dest) ? re_list_first(&((dest)->routes)) : NULL)
271
272 #define RE_DEST_NEXT_ROUTE(dest, re) \
273 ((re) = (dest) ? re_list_next(&((dest)->routes), (re)) : NULL)
274
275 #define RNODE_FOREACH_RE(rn, re) \
276 RE_DEST_FOREACH_ROUTE (rib_dest_from_rnode(rn), re)
277
278 #define RNODE_FOREACH_RE_SAFE(rn, re, next) \
279 RE_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode(rn), re, next)
280
281 #define RNODE_FIRST_RE(rn, re) RE_DEST_FIRST_ROUTE(rib_dest_from_rnode(rn), re)
282
283 #define RNODE_NEXT_RE(rn, re) RE_DEST_NEXT_ROUTE(rib_dest_from_rnode(rn), re)
284
285 /*
286 * rib_table_info_t
287 *
288 * Structure that is hung off of a route_table that holds information about
289 * the table.
290 */
291 struct rib_table_info {
292
293 /*
294 * Back pointer to zebra_vrf.
295 */
296 struct zebra_vrf *zvrf;
297 afi_t afi;
298 safi_t safi;
299 uint32_t table_id;
300 };
301
302 enum rib_tables_iter_state {
303 RIB_TABLES_ITER_S_INIT,
304 RIB_TABLES_ITER_S_ITERATING,
305 RIB_TABLES_ITER_S_DONE
306 };
307
308 /*
309 * Structure that holds state for iterating over all tables in the
310 * Routing Information Base.
311 */
312 typedef struct rib_tables_iter_t_ {
313 vrf_id_t vrf_id;
314 int afi_safi_ix;
315
316 enum rib_tables_iter_state state;
317 } rib_tables_iter_t;
318
319 /* Events/reasons triggering a RIB update. */
320 enum rib_update_event {
321 RIB_UPDATE_KERNEL,
322 RIB_UPDATE_RMAP_CHANGE,
323 RIB_UPDATE_OTHER,
324 RIB_UPDATE_MAX
325 };
326 void rib_update_finish(void);
327
328 int route_entry_update_nhe(struct route_entry *re,
329 struct nhg_hash_entry *new_nhghe);
330
331 /* NHG replace has happend, we have to update route_entry pointers to new one */
332 void rib_handle_nhg_replace(struct nhg_hash_entry *old_entry,
333 struct nhg_hash_entry *new_entry);
334
335 #define route_entry_dump(prefix, src, re) _route_entry_dump(__func__, prefix, src, re)
336 extern void _route_entry_dump(const char *func, union prefixconstptr pp,
337 union prefixconstptr src_pp,
338 const struct route_entry *re);
339
340 struct route_entry *
341 zebra_rib_route_entry_new(vrf_id_t vrf_id, int type, uint8_t instance,
342 uint32_t flags, uint32_t nhe_id, uint32_t table_id,
343 uint32_t metric, uint32_t mtu, uint8_t distance,
344 route_tag_t tag);
345
346 #define ZEBRA_RIB_LOOKUP_ERROR -1
347 #define ZEBRA_RIB_FOUND_EXACT 0
348 #define ZEBRA_RIB_FOUND_NOGATE 1
349 #define ZEBRA_RIB_FOUND_CONNECTED 2
350 #define ZEBRA_RIB_NOTFOUND 3
351
352 extern int is_zebra_valid_kernel_table(uint32_t table_id);
353 extern int is_zebra_main_routing_table(uint32_t table_id);
354 extern int zebra_check_addr(const struct prefix *p);
355
356 extern void rib_delnode(struct route_node *rn, struct route_entry *re);
357 extern void rib_install_kernel(struct route_node *rn, struct route_entry *re,
358 struct route_entry *old);
359 extern void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re);
360
361 /* NOTE:
362 * All rib_add function will not just add prefix into RIB, but
363 * also implicitly withdraw equal prefix of same type. */
364 extern int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
365 unsigned short instance, uint32_t flags, struct prefix *p,
366 struct prefix_ipv6 *src_p, const struct nexthop *nh,
367 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
368 uint32_t mtu, uint8_t distance, route_tag_t tag,
369 bool startup);
370 /*
371 * Multipath route apis.
372 */
373 extern int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
374 struct prefix_ipv6 *src_p, struct route_entry *re,
375 struct nexthop_group *ng, bool startup);
376 /*
377 * -1 -> some sort of error
378 * 0 -> an add
379 * 1 -> an update
380 */
381 extern int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
382 struct prefix_ipv6 *src_p,
383 struct route_entry *re,
384 struct nhg_hash_entry *nhe, bool startup);
385
386 extern void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
387 unsigned short instance, uint32_t flags,
388 struct prefix *p, struct prefix_ipv6 *src_p,
389 const struct nexthop *nh, uint32_t nhe_id,
390 uint32_t table_id, uint32_t metric, uint8_t distance,
391 bool fromkernel);
392
393 extern struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
394 const union g_addr *addr,
395 struct route_node **rn_out);
396 extern struct route_entry *rib_match_multicast(afi_t afi, vrf_id_t vrf_id,
397 union g_addr *gaddr,
398 struct route_node **rn_out);
399
400 extern struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p,
401 vrf_id_t vrf_id);
402
403 extern void rib_update(enum rib_update_event event);
404 extern void rib_update_table(struct route_table *table,
405 enum rib_update_event event, int rtype);
406 extern void rib_sweep_route(struct event *t);
407 extern void rib_sweep_table(struct route_table *table);
408 extern void rib_close_table(struct route_table *table);
409 extern void rib_init(void);
410 extern unsigned long rib_score_proto(uint8_t proto, unsigned short instance);
411 extern unsigned long rib_score_proto_table(uint8_t proto,
412 unsigned short instance,
413 struct route_table *table);
414
415 extern int rib_queue_add(struct route_node *rn);
416
417 struct nhg_ctx; /* Forward declaration */
418
419 /* Enqueue incoming nhg from OS for processing */
420 extern int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx);
421
422 /* Enqueue incoming nhg from proto daemon for processing */
423 extern int rib_queue_nhe_add(struct nhg_hash_entry *nhe);
424
425 /* Enqueue evpn route for processing */
426 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
427 const struct ipaddr *vtep_ip,
428 const struct prefix *host_prefix);
429 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
430 const struct ipaddr *vtep_ip,
431 const struct prefix *host_prefix);
432 /* Enqueue EVPN remote ES for processing */
433 int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
434 const struct in_addr *vtep_ip,
435 bool esr_rxed, uint8_t df_alg,
436 uint16_t df_pref);
437 int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
438 const struct in_addr *vtep_ip);
439 /* Enqueue EVPN remote macip update for processing */
440 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
441 const struct ipaddr *ip,
442 struct in_addr vtep_ip);
443 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
444 const struct ipaddr *ipaddr,
445 uint8_t flags, uint32_t seq,
446 struct in_addr vtep_ip,
447 const esi_t *esi);
448 /* Enqueue VXLAN remote vtep update for processing */
449 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
450 struct in_addr vtep_ip,
451 int flood_control);
452 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
453 struct in_addr vtep_ip);
454
455 extern void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf);
456 extern int zebra_rib_labeled_unicast(struct route_entry *re);
457 extern struct route_table *rib_table_ipv6;
458
459 extern void rib_unlink(struct route_node *rn, struct route_entry *re);
460 extern int rib_gc_dest(struct route_node *rn);
461 extern struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter);
462
463 extern uint8_t route_distance(int type);
464
465 extern void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq,
466 bool rt_delete);
467
468 extern struct route_node *
469 rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx);
470
471 /*
472 * Inline functions.
473 */
474
475 /*
476 * rib_table_info
477 */
478 static inline struct rib_table_info *rib_table_info(struct route_table *table)
479 {
480 return (struct rib_table_info *)route_table_get_info(table);
481 }
482
483 /*
484 * rib_dest_from_rnode
485 */
486 static inline rib_dest_t *rib_dest_from_rnode(struct route_node *rn)
487 {
488 return (rib_dest_t *)rn->info;
489 }
490
491 /*
492 * rnode_to_ribs
493 *
494 * Returns a pointer to the list of routes corresponding to the given
495 * route_node.
496 */
497 static inline struct route_entry *rnode_to_ribs(struct route_node *rn)
498 {
499 rib_dest_t *dest;
500
501 dest = rib_dest_from_rnode(rn);
502 if (!dest)
503 return NULL;
504
505 return re_list_first(&dest->routes);
506 }
507
508 /*
509 * rib_dest_prefix
510 */
511 static inline struct prefix *rib_dest_prefix(rib_dest_t *dest)
512 {
513 return &dest->rnode->p;
514 }
515
516 /*
517 * rib_dest_af
518 *
519 * Returns the address family that the destination is for.
520 */
521 static inline uint8_t rib_dest_af(rib_dest_t *dest)
522 {
523 return dest->rnode->p.family;
524 }
525
526 /*
527 * rib_dest_table
528 */
529 static inline struct route_table *rib_dest_table(rib_dest_t *dest)
530 {
531 return srcdest_rnode_table(dest->rnode);
532 }
533
534 /*
535 * rib_dest_vrf
536 */
537 static inline struct zebra_vrf *rib_dest_vrf(rib_dest_t *dest)
538 {
539 return rib_table_info(rib_dest_table(dest))->zvrf;
540 }
541
542 /*
543 * Create the rib_dest_t and attach it to the specified node
544 */
545 extern rib_dest_t *zebra_rib_create_dest(struct route_node *rn);
546
547 /*
548 * rib_tables_iter_init
549 */
550 static inline void rib_tables_iter_init(rib_tables_iter_t *iter)
551
552 {
553 memset(iter, 0, sizeof(*iter));
554 iter->state = RIB_TABLES_ITER_S_INIT;
555 }
556
557 /*
558 * rib_tables_iter_started
559 *
560 * Returns true if this iterator has started iterating over the set of
561 * tables.
562 */
563 static inline int rib_tables_iter_started(rib_tables_iter_t *iter)
564 {
565 return iter->state != RIB_TABLES_ITER_S_INIT;
566 }
567
568 /*
569 * rib_tables_iter_cleanup
570 */
571 static inline void rib_tables_iter_cleanup(rib_tables_iter_t *iter)
572 {
573 iter->state = RIB_TABLES_ITER_S_DONE;
574 }
575
576 DECLARE_HOOK(rib_update, (struct route_node * rn, const char *reason),
577 (rn, reason));
578 DECLARE_HOOK(rib_shutdown, (struct route_node * rn), (rn));
579
580 /*
581 * Access installed/fib nexthops, which may be a subset of the
582 * rib nexthops.
583 */
584 static inline struct nexthop_group *rib_get_fib_nhg(struct route_entry *re)
585 {
586 /* If the fib set is a subset of the active rib set,
587 * use the dedicated fib list.
588 */
589 if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG))
590 return &(re->fib_ng);
591 else
592 return &(re->nhe->nhg);
593 }
594
595 /*
596 * Access backup nexthop-group that represents the installed backup nexthops;
597 * any installed backup will be on the fib list.
598 */
599 static inline struct nexthop_group *rib_get_fib_backup_nhg(
600 struct route_entry *re)
601 {
602 return &(re->fib_backup_ng);
603 }
604
605 extern void zebra_gr_process_client(afi_t afi, vrf_id_t vrf_id, uint8_t proto,
606 uint8_t instance);
607
608 extern int rib_add_gr_run(afi_t afi, vrf_id_t vrf_id, uint8_t proto,
609 uint8_t instance);
610
611 extern void zebra_vty_init(void);
612
613 extern pid_t pid;
614
615 extern bool v6_rr_semantics;
616
617 /* Name of hook calls */
618 #define ZEBRA_ON_RIB_PROCESS_HOOK_CALL "on_rib_process_dplane_results"
619
620 #ifdef __cplusplus
621 }
622 #endif
623
624 #endif /*_ZEBRA_RIB_H */