]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
Merge pull request #13145 from donaldsharp/do_delete
[mirror_frr.git] / zebra / zebra_rib.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Routing Information Base.
3 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include "command.h"
9 #include "if.h"
10 #include "linklist.h"
11 #include "log.h"
12 #include "memory.h"
13 #include "mpls.h"
14 #include "nexthop.h"
15 #include "prefix.h"
16 #include "prefix.h"
17 #include "routemap.h"
18 #include "sockunion.h"
19 #include "srcdest_table.h"
20 #include "table.h"
21 #include "frrevent.h"
22 #include "vrf.h"
23 #include "workqueue.h"
24 #include "nexthop_group_private.h"
25 #include "frr_pthread.h"
26 #include "printfrr.h"
27 #include "frrscript.h"
28
29 #include "zebra/zebra_router.h"
30 #include "zebra/connected.h"
31 #include "zebra/debug.h"
32 #include "zebra/interface.h"
33 #include "zebra/redistribute.h"
34 #include "zebra/rib.h"
35 #include "zebra/rt.h"
36 #include "zebra/zapi_msg.h"
37 #include "zebra/zebra_errors.h"
38 #include "zebra/zebra_ns.h"
39 #include "zebra/zebra_rnh.h"
40 #include "zebra/zebra_routemap.h"
41 #include "zebra/zebra_vrf.h"
42 #include "zebra/zebra_vxlan.h"
43 #include "zebra/zapi_msg.h"
44 #include "zebra/zebra_dplane.h"
45 #include "zebra/zebra_evpn_mh.h"
46 #include "zebra/zebra_script.h"
47
48 DEFINE_MGROUP(ZEBRA, "zebra");
49
50 DEFINE_MTYPE(ZEBRA, RE, "Route Entry");
51 DEFINE_MTYPE_STATIC(ZEBRA, RIB_DEST, "RIB destination");
52 DEFINE_MTYPE_STATIC(ZEBRA, RIB_UPDATE_CTX, "Rib update context object");
53 DEFINE_MTYPE_STATIC(ZEBRA, WQ_WRAPPER, "WQ wrapper");
54
55 /*
56 * Event, list, and mutex for delivery of dataplane results
57 */
58 static pthread_mutex_t dplane_mutex;
59 static struct event *t_dplane;
60 static struct dplane_ctx_list_head rib_dplane_q;
61
62 DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
63 (rn, reason));
64 DEFINE_HOOK(rib_shutdown, (struct route_node * rn), (rn));
65
66
67 /*
68 * Meta Q's specific names
69 *
70 * If you add something here ensure that you
71 * change MQ_SIZE as well over in rib.h
72 */
73 enum meta_queue_indexes {
74 META_QUEUE_NHG,
75 META_QUEUE_EVPN,
76 META_QUEUE_EARLY_ROUTE,
77 META_QUEUE_EARLY_LABEL,
78 META_QUEUE_CONNECTED,
79 META_QUEUE_KERNEL,
80 META_QUEUE_STATIC,
81 META_QUEUE_NOTBGP,
82 META_QUEUE_BGP,
83 META_QUEUE_OTHER,
84 META_QUEUE_GR_RUN,
85 };
86
87 /* Each route type's string and default distance value. */
88 static const struct {
89 int key;
90 uint8_t distance;
91 enum meta_queue_indexes meta_q_map;
92 } route_info[ZEBRA_ROUTE_MAX] = {
93 [ZEBRA_ROUTE_NHG] =
94 {ZEBRA_ROUTE_NHG,
95 ZEBRA_MAX_DISTANCE_DEFAULT /* Unneeded for nhg's */,
96 META_QUEUE_NHG},
97 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM,
98 ZEBRA_KERNEL_DISTANCE_DEFAULT,
99 META_QUEUE_KERNEL},
100 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL,
101 ZEBRA_KERNEL_DISTANCE_DEFAULT,
102 META_QUEUE_KERNEL},
103 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT,
104 ZEBRA_CONNECT_DISTANCE_DEFAULT,
105 META_QUEUE_CONNECTED},
106 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC,
107 ZEBRA_STATIC_DISTANCE_DEFAULT,
108 META_QUEUE_STATIC},
109 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, ZEBRA_RIP_DISTANCE_DEFAULT,
110 META_QUEUE_NOTBGP},
111 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, ZEBRA_RIP_DISTANCE_DEFAULT,
112 META_QUEUE_NOTBGP},
113 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, ZEBRA_OSPF_DISTANCE_DEFAULT,
114 META_QUEUE_NOTBGP},
115 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, ZEBRA_OSPF6_DISTANCE_DEFAULT,
116 META_QUEUE_NOTBGP},
117 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, ZEBRA_ISIS_DISTANCE_DEFAULT,
118 META_QUEUE_NOTBGP},
119 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP,
120 ZEBRA_EBGP_DISTANCE_DEFAULT /* IBGP is 200. */,
121 META_QUEUE_BGP},
122 [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, ZEBRA_MAX_DISTANCE_DEFAULT,
123 META_QUEUE_OTHER},
124 [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, ZEBRA_EIGRP_DISTANCE_DEFAULT,
125 META_QUEUE_NOTBGP},
126 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, ZEBRA_NHRP_DISTANCE_DEFAULT,
127 META_QUEUE_NOTBGP},
128 [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, ZEBRA_MAX_DISTANCE_DEFAULT,
129 META_QUEUE_OTHER},
130 [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, ZEBRA_MAX_DISTANCE_DEFAULT,
131 META_QUEUE_OTHER},
132 [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, ZEBRA_TABLE_DISTANCE_DEFAULT, META_QUEUE_STATIC},
133 [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, ZEBRA_LDP_DISTANCE_DEFAULT,
134 META_QUEUE_OTHER},
135 [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, ZEBRA_EBGP_DISTANCE_DEFAULT,
136 META_QUEUE_BGP},
137 [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT,
138 ZEBRA_EBGP_DISTANCE_DEFAULT,
139 META_QUEUE_BGP},
140 [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH,
141 ZEBRA_EBGP_DISTANCE_DEFAULT,
142 META_QUEUE_BGP},
143 [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT,
144 ZEBRA_EBGP_DISTANCE_DEFAULT,
145 META_QUEUE_BGP},
146 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT,
147 ZEBRA_EBGP_DISTANCE_DEFAULT,
148 META_QUEUE_BGP},
149 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, ZEBRA_BABEL_DISTANCE_DEFAULT,
150 META_QUEUE_NOTBGP},
151 [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, ZEBRA_SHARP_DISTANCE_DEFAULT,
152 META_QUEUE_OTHER},
153 [ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, ZEBRA_PBR_DISTANCE_DEFAULT,
154 META_QUEUE_OTHER},
155 [ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, ZEBRA_MAX_DISTANCE_DEFAULT,
156 META_QUEUE_OTHER},
157 [ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC,
158 ZEBRA_OPENFABRIC_DISTANCE_DEFAULT,
159 META_QUEUE_NOTBGP},
160 [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, ZEBRA_MAX_DISTANCE_DEFAULT,
161 META_QUEUE_OTHER},
162 [ZEBRA_ROUTE_SRTE] = {ZEBRA_ROUTE_SRTE, ZEBRA_MAX_DISTANCE_DEFAULT,
163 META_QUEUE_OTHER},
164 [ZEBRA_ROUTE_ALL] = {ZEBRA_ROUTE_ALL, ZEBRA_MAX_DISTANCE_DEFAULT,
165 META_QUEUE_OTHER},
166 /* Any new route type added to zebra, should be mirrored here */
167
168 /* no entry/default: 150 */
169 };
170
171 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
172 * from the OS, and an 'nhe' is a nhe update.
173 */
174 struct wq_nhg_wrapper {
175 int type;
176 union {
177 struct nhg_ctx *ctx;
178 struct nhg_hash_entry *nhe;
179 } u;
180 };
181
182 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
183 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
184
185 /* Wrapper structs for evpn/vxlan workqueue items. */
186 struct wq_evpn_wrapper {
187 int type;
188 bool add_p;
189 vrf_id_t vrf_id;
190 bool esr_rxed;
191 uint8_t df_alg;
192 uint16_t df_pref;
193 uint32_t flags;
194 uint32_t seq;
195 esi_t esi;
196 vni_t vni;
197 struct ipaddr ip;
198 struct ethaddr macaddr;
199 struct prefix prefix;
200 struct in_addr vtep_ip;
201 };
202
203 #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
204 #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
205 #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
206 #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
207
208 enum wq_label_types {
209 WQ_LABEL_FTN_UNINSTALL,
210 WQ_LABEL_LABELS_PROCESS,
211 };
212
213 struct wq_label_wrapper {
214 enum wq_label_types type;
215 vrf_id_t vrf_id;
216
217 struct prefix p;
218 enum lsp_types_t ltype;
219 uint8_t route_type;
220 uint8_t route_instance;
221
222 bool add_p;
223 struct zapi_labels zl;
224
225 int afi;
226 };
227
228 static void rib_addnode(struct route_node *rn, struct route_entry *re,
229 int process);
230
231 /* %pRN is already a printer for route_nodes that just prints the prefix */
232 #ifdef _FRR_ATTRIBUTE_PRINTFRR
233 #pragma FRR printfrr_ext "%pZN" (struct route_node *)
234 #endif
235
236 static const char *subqueue2str(enum meta_queue_indexes index)
237 {
238 switch (index) {
239 case META_QUEUE_NHG:
240 return "NHG Objects";
241 case META_QUEUE_EVPN:
242 return "EVPN/VxLan Objects";
243 case META_QUEUE_EARLY_ROUTE:
244 return "Early Route Processing";
245 case META_QUEUE_EARLY_LABEL:
246 return "Early Label Handling";
247 case META_QUEUE_CONNECTED:
248 return "Connected Routes";
249 case META_QUEUE_KERNEL:
250 return "Kernel Routes";
251 case META_QUEUE_STATIC:
252 return "Static Routes";
253 case META_QUEUE_NOTBGP:
254 return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
255 case META_QUEUE_BGP:
256 return "BGP Routes";
257 case META_QUEUE_OTHER:
258 return "Other Routes";
259 case META_QUEUE_GR_RUN:
260 return "Graceful Restart";
261 }
262
263 return "Unknown";
264 }
265
266 printfrr_ext_autoreg_p("ZN", printfrr_zebra_node);
267 static ssize_t printfrr_zebra_node(struct fbuf *buf, struct printfrr_eargs *ea,
268 const void *ptr)
269 {
270 struct route_node *rn = (struct route_node *)ptr;
271 ssize_t rv = 0;
272
273 /* just the table number? */
274 if (ea->fmt[0] == 't') {
275 rib_dest_t *dest;
276 struct route_entry *re = NULL;
277
278 ea->fmt++;
279
280 if (!rn)
281 return bputch(buf, '!');
282
283 dest = rib_dest_from_rnode(rn);
284 if (dest)
285 re = re_list_first(&dest->routes);
286 if (re)
287 rv += bprintfrr(buf, "%u", re->table);
288 else
289 rv += bputch(buf, '?');
290
291 } else {
292 char cbuf[PREFIX_STRLEN * 2 + 6];
293 struct rib_table_info *info;
294
295 if (!rn)
296 return bputs(buf, "{(route_node *) NULL}");
297
298 srcdest_rnode2str(rn, cbuf, sizeof(cbuf));
299 rv += bputs(buf, cbuf);
300
301 info = srcdest_rnode_table_info(rn);
302 if (info->safi == SAFI_MULTICAST)
303 rv += bputs(buf, " (MRIB)");
304 }
305 return rv;
306 }
307
308 #define rnode_debug(node, vrf_id, msg, ...) \
309 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
310 ##__VA_ARGS__)
311
312 #define rnode_info(node, vrf_id, msg, ...) \
313 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
314 ##__VA_ARGS__)
315
316 static char *_dump_re_status(const struct route_entry *re, char *buf,
317 size_t len)
318 {
319 if (re->status == 0) {
320 snprintfrr(buf, len, "None ");
321 return buf;
322 }
323
324 snprintfrr(
325 buf, len, "%s%s%s%s%s%s%s%s",
326 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) ? "Removed " : "",
327 CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED) ? "Changed " : "",
328 CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)
329 ? "Label Changed "
330 : "",
331 CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED) ? "Queued " : "",
332 CHECK_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING)
333 ? "Replacing"
334 : "",
335 CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) ? "Installed "
336 : "",
337 CHECK_FLAG(re->status, ROUTE_ENTRY_FAILED) ? "Failed " : "",
338 CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG) ? "Fib NHG "
339 : "");
340 return buf;
341 }
342
343 uint8_t route_distance(int type)
344 {
345 uint8_t distance;
346
347 if ((unsigned)type >= array_size(route_info))
348 distance = 150;
349 else
350 distance = route_info[type].distance;
351
352 return distance;
353 }
354
355 int is_zebra_valid_kernel_table(uint32_t table_id)
356 {
357 #ifdef linux
358 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
359 || (table_id == RT_TABLE_COMPAT))
360 return 0;
361 #endif
362
363 return 1;
364 }
365
366 int is_zebra_main_routing_table(uint32_t table_id)
367 {
368 if (table_id == RT_TABLE_MAIN)
369 return 1;
370 return 0;
371 }
372
373 int zebra_check_addr(const struct prefix *p)
374 {
375 if (p->family == AF_INET) {
376 uint32_t addr;
377
378 addr = p->u.prefix4.s_addr;
379 addr = ntohl(addr);
380
381 if (IPV4_NET127(addr) || IN_CLASSD(addr)
382 || IPV4_LINKLOCAL(addr))
383 return 0;
384 }
385 if (p->family == AF_INET6) {
386 if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
387 return 0;
388 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
389 return 0;
390 }
391 return 1;
392 }
393
394 static void route_entry_attach_ref(struct route_entry *re,
395 struct nhg_hash_entry *new)
396 {
397 re->nhe = new;
398 re->nhe_id = new->id;
399 re->nhe_installed_id = 0;
400
401 zebra_nhg_increment_ref(new);
402 }
403
404 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
405 int route_entry_update_nhe(struct route_entry *re,
406 struct nhg_hash_entry *new_nhghe)
407 {
408 int ret = 0;
409 struct nhg_hash_entry *old_nhg = NULL;
410
411 if (new_nhghe == NULL) {
412 old_nhg = re->nhe;
413
414 re->nhe_id = 0;
415 re->nhe_installed_id = 0;
416 re->nhe = NULL;
417 goto done;
418 }
419
420 if ((re->nhe_id != 0) && re->nhe && (re->nhe != new_nhghe)) {
421 /* Capture previous nhg, if any */
422 old_nhg = re->nhe;
423
424 route_entry_attach_ref(re, new_nhghe);
425 } else if (!re->nhe)
426 /* This is the first time it's being attached */
427 route_entry_attach_ref(re, new_nhghe);
428
429 done:
430 /* Detach / deref previous nhg */
431 if (old_nhg)
432 zebra_nhg_decrement_ref(old_nhg);
433
434 return ret;
435 }
436
437 void rib_handle_nhg_replace(struct nhg_hash_entry *old_entry,
438 struct nhg_hash_entry *new_entry)
439 {
440 struct zebra_router_table *zrt;
441 struct route_node *rn;
442 struct route_entry *re, *next;
443
444 if (IS_ZEBRA_DEBUG_RIB_DETAILED || IS_ZEBRA_DEBUG_NHG_DETAIL)
445 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
446 __func__, new_entry->id, new_entry, old_entry);
447
448 /* We have to do them ALL */
449 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
450 for (rn = route_top(zrt->table); rn;
451 rn = srcdest_route_next(rn)) {
452 RNODE_FOREACH_RE_SAFE (rn, re, next) {
453 if (re->nhe && re->nhe == old_entry)
454 route_entry_update_nhe(re, new_entry);
455 }
456 }
457 }
458 }
459
460 struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
461 const union g_addr *addr,
462 struct route_node **rn_out)
463 {
464 struct prefix p;
465 struct route_table *table;
466 struct route_node *rn;
467 struct route_entry *match = NULL;
468
469 /* Lookup table. */
470 table = zebra_vrf_table(afi, safi, vrf_id);
471 if (!table)
472 return 0;
473
474 memset(&p, 0, sizeof(p));
475 p.family = afi;
476 if (afi == AFI_IP) {
477 p.u.prefix4 = addr->ipv4;
478 p.prefixlen = IPV4_MAX_BITLEN;
479 } else {
480 p.u.prefix6 = addr->ipv6;
481 p.prefixlen = IPV6_MAX_BITLEN;
482 }
483
484 rn = route_node_match(table, &p);
485
486 while (rn) {
487 rib_dest_t *dest;
488
489 route_unlock_node(rn);
490
491 dest = rib_dest_from_rnode(rn);
492 if (dest && dest->selected_fib
493 && !CHECK_FLAG(dest->selected_fib->status,
494 ROUTE_ENTRY_REMOVED))
495 match = dest->selected_fib;
496
497 /* If there is no selected route or matched route is EGP, go up
498 tree. */
499 if (!match) {
500 do {
501 rn = rn->parent;
502 } while (rn && rn->info == NULL);
503 if (rn)
504 route_lock_node(rn);
505 } else {
506 if (match->type != ZEBRA_ROUTE_CONNECT) {
507 if (!CHECK_FLAG(match->status,
508 ROUTE_ENTRY_INSTALLED))
509 return NULL;
510 }
511
512 if (rn_out)
513 *rn_out = rn;
514 return match;
515 }
516 }
517 return NULL;
518 }
519
520 struct route_entry *rib_match_multicast(afi_t afi, vrf_id_t vrf_id,
521 union g_addr *gaddr,
522 struct route_node **rn_out)
523 {
524 struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
525 struct route_node *m_rn = NULL, *u_rn = NULL;
526
527 switch (zrouter.ipv4_multicast_mode) {
528 case MCAST_MRIB_ONLY:
529 return rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, rn_out);
530 case MCAST_URIB_ONLY:
531 return rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, rn_out);
532 case MCAST_NO_CONFIG:
533 case MCAST_MIX_MRIB_FIRST:
534 re = mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn);
535 if (!mre)
536 re = ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr,
537 &u_rn);
538 break;
539 case MCAST_MIX_DISTANCE:
540 mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn);
541 ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, &u_rn);
542 if (mre && ure)
543 re = ure->distance < mre->distance ? ure : mre;
544 else if (mre)
545 re = mre;
546 else if (ure)
547 re = ure;
548 break;
549 case MCAST_MIX_PFXLEN:
550 mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn);
551 ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, &u_rn);
552 if (mre && ure)
553 re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
554 else if (mre)
555 re = mre;
556 else if (ure)
557 re = ure;
558 break;
559 }
560
561 if (rn_out)
562 *rn_out = (re == mre) ? m_rn : u_rn;
563
564 if (IS_ZEBRA_DEBUG_RIB) {
565 char buf[BUFSIZ];
566 inet_ntop(afi == AFI_IP ? AF_INET : AF_INET6, gaddr, buf,
567 BUFSIZ);
568
569 zlog_debug("%s: %s: %pRN vrf: %s(%u) found %s, using %s",
570 __func__, buf, (re == mre) ? m_rn : u_rn,
571 vrf_id_to_name(vrf_id), vrf_id,
572 mre ? (ure ? "MRIB+URIB" : "MRIB")
573 : ure ? "URIB" : "nothing",
574 re == ure ? "URIB" : re == mre ? "MRIB" : "none");
575 }
576 return re;
577 }
578
579 struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
580 {
581 struct route_table *table;
582 struct route_node *rn;
583 struct route_entry *match = NULL;
584 rib_dest_t *dest;
585
586 /* Lookup table. */
587 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
588 if (!table)
589 return 0;
590
591 rn = route_node_lookup(table, (struct prefix *)p);
592
593 /* No route for this prefix. */
594 if (!rn)
595 return NULL;
596
597 /* Unlock node. */
598 route_unlock_node(rn);
599 dest = rib_dest_from_rnode(rn);
600
601 if (dest && dest->selected_fib
602 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
603 match = dest->selected_fib;
604
605 if (!match)
606 return NULL;
607
608 if (match->type == ZEBRA_ROUTE_CONNECT)
609 return match;
610
611 if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED))
612 return match;
613
614 return NULL;
615 }
616
617 /*
618 * Is this RIB labeled-unicast? It must be of type BGP and all paths
619 * (nexthops) must have a label.
620 */
621 int zebra_rib_labeled_unicast(struct route_entry *re)
622 {
623 struct nexthop *nexthop = NULL;
624
625 if (re->type != ZEBRA_ROUTE_BGP)
626 return 0;
627
628 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
629 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
630 return 0;
631
632 return 1;
633 }
634
635 /* Update flag indicates whether this is a "replace" or not. Currently, this
636 * is only used for IPv4.
637 */
638 void rib_install_kernel(struct route_node *rn, struct route_entry *re,
639 struct route_entry *old)
640 {
641 struct nexthop *nexthop;
642 struct rib_table_info *info = srcdest_rnode_table_info(rn);
643 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
644 const struct prefix *p, *src_p;
645 enum zebra_dplane_result ret;
646
647 rib_dest_t *dest = rib_dest_from_rnode(rn);
648
649 srcdest_rnode_prefixes(rn, &p, &src_p);
650
651 if (info->safi != SAFI_UNICAST) {
652 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
653 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
654 return;
655 }
656
657
658 /*
659 * Install the resolved nexthop object first.
660 */
661 zebra_nhg_install_kernel(re->nhe);
662
663 /*
664 * If this is a replace to a new RE let the originator of the RE
665 * know that they've lost
666 */
667 if (old && (old != re) && (old->type != re->type))
668 zsend_route_notify_owner(rn, old, ZAPI_ROUTE_BETTER_ADMIN_WON,
669 info->afi, info->safi);
670
671 /* Update fib selection */
672 dest->selected_fib = re;
673
674 /*
675 * Make sure we update the FPM any time we send new information to
676 * the kernel.
677 */
678 hook_call(rib_update, rn, "installing in kernel");
679
680 /* Send add or update */
681 if (old)
682 ret = dplane_route_update(rn, re, old);
683 else
684 ret = dplane_route_add(rn, re);
685
686 switch (ret) {
687 case ZEBRA_DPLANE_REQUEST_QUEUED:
688 SET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
689
690 if (old) {
691 SET_FLAG(old->status, ROUTE_ENTRY_QUEUED);
692 SET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
693
694 /* Free old FIB nexthop group */
695 UNSET_FLAG(old->status, ROUTE_ENTRY_USE_FIB_NHG);
696 if (old->fib_ng.nexthop) {
697 nexthops_free(old->fib_ng.nexthop);
698 old->fib_ng.nexthop = NULL;
699 }
700 }
701
702 if (zvrf)
703 zvrf->installs_queued++;
704 break;
705 case ZEBRA_DPLANE_REQUEST_FAILURE:
706 {
707 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
708 "%u:%u:%pRN: Failed to enqueue dataplane install",
709 re->vrf_id, re->table, rn);
710 break;
711 }
712 case ZEBRA_DPLANE_REQUEST_SUCCESS:
713 if (zvrf)
714 zvrf->installs++;
715 break;
716 }
717
718 return;
719 }
720
721 /* Uninstall the route from kernel. */
722 void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
723 {
724 struct nexthop *nexthop;
725 struct rib_table_info *info = srcdest_rnode_table_info(rn);
726 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
727
728 if (info->safi != SAFI_UNICAST) {
729 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
730 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
731 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
732 return;
733 }
734
735 /*
736 * Make sure we update the FPM any time we send new information to
737 * the dataplane.
738 */
739 hook_call(rib_update, rn, "uninstalling from kernel");
740
741 switch (dplane_route_delete(rn, re)) {
742 case ZEBRA_DPLANE_REQUEST_QUEUED:
743 if (zvrf)
744 zvrf->removals_queued++;
745 break;
746 case ZEBRA_DPLANE_REQUEST_FAILURE:
747 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
748 "%u:%pRN: Failed to enqueue dataplane uninstall",
749 re->vrf_id, rn);
750 break;
751 case ZEBRA_DPLANE_REQUEST_SUCCESS:
752 if (zvrf)
753 zvrf->removals++;
754 break;
755 }
756
757 return;
758 }
759
760 /*
761 * rib_can_delete_dest
762 *
763 * Returns true if the given dest can be deleted from the table.
764 */
765 static int rib_can_delete_dest(rib_dest_t *dest)
766 {
767 if (re_list_first(&dest->routes)) {
768 return 0;
769 }
770
771 /*
772 * Unresolved rnh's are stored on the default route's list
773 *
774 * dest->rnode can also be the source prefix node in an
775 * ipv6 sourcedest table. Fortunately the prefix of a
776 * source prefix node can never be the default prefix.
777 */
778 if (is_default_prefix(&dest->rnode->p))
779 return 0;
780
781 /*
782 * Don't delete the dest if we have to update the FPM about this
783 * prefix.
784 */
785 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
786 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
787 return 0;
788
789 return 1;
790 }
791
792 void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq,
793 bool rt_delete)
794 {
795 rib_dest_t *dest = rib_dest_from_rnode(rn);
796 struct rnh *rnh;
797
798 /*
799 * We are storing the rnh's associated withb
800 * the tracked nexthop as a list of the rn's.
801 * Unresolved rnh's are placed at the top
802 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
803 * As such for each rn we need to walk up the tree
804 * and see if any rnh's need to see if they
805 * would match a more specific route
806 */
807 while (rn) {
808 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
809 zlog_debug(
810 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
811 __func__, rn,
812 dest ? rnh_list_count(&dest->nht) : 0);
813
814 if (rt_delete && (!dest || !rnh_list_count(&dest->nht))) {
815 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
816 zlog_debug("%pRN has no tracking NHTs. Bailing",
817 rn);
818 break;
819 }
820 if (!dest) {
821 rn = rn->parent;
822 if (rn)
823 dest = rib_dest_from_rnode(rn);
824 continue;
825 }
826 /*
827 * If we have any rnh's stored in the nht list
828 * then we know that this route node was used for
829 * nht resolution and as such we need to call the
830 * nexthop tracking evaluation code
831 */
832 frr_each_safe(rnh_list, &dest->nht, rnh) {
833 struct zebra_vrf *zvrf =
834 zebra_vrf_lookup_by_id(rnh->vrf_id);
835 struct prefix *p = &rnh->node->p;
836
837 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
838 zlog_debug(
839 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
840 zvrf_name(zvrf), zvrf_id(zvrf), rn,
841 rnh->node, seq, rnh->seqno);
842
843 /*
844 * If we have evaluated this node on this pass
845 * already, due to following the tree up
846 * then we know that we can move onto the next
847 * rnh to process.
848 *
849 * Additionally we call zebra_evaluate_rnh
850 * when we gc the dest. In this case we know
851 * that there must be no other re's where
852 * we were originally as such we know that
853 * that sequence number is ok to respect.
854 */
855 if (rnh->seqno == seq) {
856 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
857 zlog_debug(
858 " Node processed and moved already");
859 continue;
860 }
861
862 rnh->seqno = seq;
863 zebra_evaluate_rnh(zvrf, family2afi(p->family), 0, p,
864 rnh->safi);
865 }
866
867 rn = rn->parent;
868 if (rn)
869 dest = rib_dest_from_rnode(rn);
870 }
871 }
872
873 /*
874 * rib_gc_dest
875 *
876 * Garbage collect the rib dest corresponding to the given route node
877 * if appropriate.
878 *
879 * Returns true if the dest was deleted, false otherwise.
880 */
881 int rib_gc_dest(struct route_node *rn)
882 {
883 rib_dest_t *dest;
884
885 dest = rib_dest_from_rnode(rn);
886 if (!dest)
887 return 0;
888
889 if (!rib_can_delete_dest(dest))
890 return 0;
891
892 if (IS_ZEBRA_DEBUG_RIB) {
893 struct zebra_vrf *zvrf;
894
895 zvrf = rib_dest_vrf(dest);
896 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
897 }
898
899 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
900 true);
901
902 dest->rnode = NULL;
903 rnh_list_fini(&dest->nht);
904 XFREE(MTYPE_RIB_DEST, dest);
905 rn->info = NULL;
906
907 /*
908 * Release the one reference that we keep on the route node.
909 */
910 route_unlock_node(rn);
911 return 1;
912 }
913
914 void zebra_rtable_node_cleanup(struct route_table *table,
915 struct route_node *node)
916 {
917 struct route_entry *re, *next;
918
919 RNODE_FOREACH_RE_SAFE (node, re, next) {
920 rib_unlink(node, re);
921 }
922
923 if (node->info) {
924 rib_dest_t *dest = node->info;
925
926 /* Remove from update queue of FPM module */
927 hook_call(rib_shutdown, node);
928
929 rnh_list_fini(&dest->nht);
930 XFREE(MTYPE_RIB_DEST, node->info);
931 }
932 }
933
934 static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
935 struct route_entry *new)
936 {
937 hook_call(rib_update, rn, "new route selected");
938
939 /* Update real nexthop. This may actually determine if nexthop is active
940 * or not. */
941 if (!nexthop_group_active_nexthop_num(&(new->nhe->nhg))) {
942 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
943 return;
944 }
945
946 if (IS_ZEBRA_DEBUG_RIB)
947 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
948 zvrf_name(zvrf), zvrf_id(zvrf), new->table, rn, rn,
949 new, zebra_route_string(new->type));
950
951 /* If labeled-unicast route, install transit LSP. */
952 if (zebra_rib_labeled_unicast(new))
953 zebra_mpls_lsp_install(zvrf, rn, new);
954
955 rib_install_kernel(rn, new, NULL);
956
957 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
958 }
959
960 static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
961 struct route_entry *old)
962 {
963 hook_call(rib_update, rn, "removing existing route");
964
965 /* Uninstall from kernel. */
966 if (IS_ZEBRA_DEBUG_RIB)
967 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
968 zvrf_name(zvrf), zvrf_id(zvrf), old->table, rn, rn,
969 old, zebra_route_string(old->type));
970
971 /* If labeled-unicast route, uninstall transit LSP. */
972 if (zebra_rib_labeled_unicast(old))
973 zebra_mpls_lsp_uninstall(zvrf, rn, old);
974
975 rib_uninstall_kernel(rn, old);
976
977 /* Update nexthop for route, reset changed flag. */
978 /* Note: this code also handles the Linux case when an interface goes
979 * down, causing the kernel to delete routes without sending DELROUTE
980 * notifications
981 */
982 if (RIB_KERNEL_ROUTE(old))
983 SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
984 else
985 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
986 }
987
988 static void rib_process_update_fib(struct zebra_vrf *zvrf,
989 struct route_node *rn,
990 struct route_entry *old,
991 struct route_entry *new)
992 {
993 int nh_active = 0;
994
995 /*
996 * We have to install or update if a new route has been selected or
997 * something has changed.
998 */
999 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1000 hook_call(rib_update, rn, "updating existing route");
1001
1002 /* Update the nexthop; we could determine here that nexthop is
1003 * inactive. */
1004 if (nexthop_group_active_nexthop_num(&(new->nhe->nhg)))
1005 nh_active = 1;
1006
1007 /* If nexthop is active, install the selected route, if
1008 * appropriate. If
1009 * the install succeeds, cleanup flags for prior route, if
1010 * different from
1011 * newly selected.
1012 */
1013 if (nh_active) {
1014 if (IS_ZEBRA_DEBUG_RIB) {
1015 if (new != old)
1016 zlog_debug(
1017 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
1018 zvrf_name(zvrf), zvrf_id(zvrf),
1019 new->table, rn, rn, new,
1020 zebra_route_string(new->type),
1021 old,
1022 zebra_route_string(old->type));
1023 else
1024 zlog_debug(
1025 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
1026 zvrf_name(zvrf), zvrf_id(zvrf),
1027 new->table, rn, rn, new,
1028 zebra_route_string(new->type));
1029 }
1030
1031 /* If labeled-unicast route, uninstall transit LSP. */
1032 if (zebra_rib_labeled_unicast(old))
1033 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1034
1035 /*
1036 * Non-system route should be installed.
1037 * If labeled-unicast route, install transit
1038 * LSP.
1039 */
1040 if (zebra_rib_labeled_unicast(new))
1041 zebra_mpls_lsp_install(zvrf, rn, new);
1042
1043 rib_install_kernel(rn, new, old);
1044 }
1045
1046 /*
1047 * If nexthop for selected route is not active or install
1048 * failed, we
1049 * may need to uninstall and delete for redistribution.
1050 */
1051 if (!nh_active) {
1052 if (IS_ZEBRA_DEBUG_RIB) {
1053 if (new != old)
1054 zlog_debug(
1055 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1056 zvrf_name(zvrf), zvrf_id(zvrf),
1057 new->table, rn, rn, new,
1058 zebra_route_string(new->type),
1059 old,
1060 zebra_route_string(old->type));
1061 else
1062 zlog_debug(
1063 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
1064 zvrf_name(zvrf), zvrf_id(zvrf),
1065 new->table, rn, rn, new,
1066 zebra_route_string(new->type));
1067 }
1068
1069 /*
1070 * When we have gotten to this point
1071 * the new route entry has no nexthops
1072 * that are usable and as such we need
1073 * to remove the old route, but only
1074 * if we were the one who installed
1075 * the old route
1076 */
1077 if (!RIB_SYSTEM_ROUTE(old)) {
1078 /* If labeled-unicast route, uninstall transit
1079 * LSP. */
1080 if (zebra_rib_labeled_unicast(old))
1081 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1082
1083 rib_uninstall_kernel(rn, old);
1084 }
1085 }
1086 } else {
1087 /*
1088 * Same route selected; check if in the FIB and if not,
1089 * re-install. This is housekeeping code to deal with
1090 * race conditions in kernel with linux netlink reporting
1091 * interface up before IPv4 or IPv6 protocol is ready
1092 * to add routes.
1093 */
1094 if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) ||
1095 RIB_SYSTEM_ROUTE(new))
1096 rib_install_kernel(rn, new, NULL);
1097 }
1098
1099 /* Update prior route. */
1100 if (new != old)
1101 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1102
1103 /* Clear changed flag. */
1104 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1105 }
1106
1107 /* Check if 'alternate' RIB entry is better than 'current'. */
1108 static struct route_entry *rib_choose_best(struct route_entry *current,
1109 struct route_entry *alternate)
1110 {
1111 if (current == NULL)
1112 return alternate;
1113
1114 /* filter route selection in following order:
1115 * - connected beats other types
1116 * - if both connected, loopback or vrf wins
1117 * - lower distance beats higher
1118 * - lower metric beats higher for equal distance
1119 * - last, hence oldest, route wins tie break.
1120 */
1121
1122 /* Connected routes. Check to see if either are a vrf
1123 * or loopback interface. If not, pick the last connected
1124 * route of the set of lowest metric connected routes.
1125 */
1126 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
1127 if (current->type != ZEBRA_ROUTE_CONNECT)
1128 return alternate;
1129
1130 /* both are connected. are either loop or vrf? */
1131 struct nexthop *nexthop = NULL;
1132
1133 for (ALL_NEXTHOPS(alternate->nhe->nhg, nexthop)) {
1134 struct interface *ifp = if_lookup_by_index(
1135 nexthop->ifindex, alternate->vrf_id);
1136
1137 if (ifp && if_is_loopback(ifp))
1138 return alternate;
1139 }
1140
1141 for (ALL_NEXTHOPS(current->nhe->nhg, nexthop)) {
1142 struct interface *ifp = if_lookup_by_index(
1143 nexthop->ifindex, current->vrf_id);
1144
1145 if (ifp && if_is_loopback(ifp))
1146 return current;
1147 }
1148
1149 /* Neither are loop or vrf so pick best metric */
1150 if (alternate->metric <= current->metric)
1151 return alternate;
1152
1153 return current;
1154 }
1155
1156 if (current->type == ZEBRA_ROUTE_CONNECT)
1157 return current;
1158
1159 /* higher distance loses */
1160 if (alternate->distance < current->distance)
1161 return alternate;
1162 if (current->distance < alternate->distance)
1163 return current;
1164
1165 /* metric tie-breaks equal distance */
1166 if (alternate->metric <= current->metric)
1167 return alternate;
1168
1169 return current;
1170 }
1171
1172 /* Core function for processing routing information base. */
1173 static void rib_process(struct route_node *rn)
1174 {
1175 struct route_entry *re;
1176 struct route_entry *next;
1177 struct route_entry *old_selected = NULL;
1178 struct route_entry *new_selected = NULL;
1179 struct route_entry *old_fib = NULL;
1180 struct route_entry *new_fib = NULL;
1181 struct route_entry *best = NULL;
1182 rib_dest_t *dest;
1183 struct zebra_vrf *zvrf = NULL;
1184 struct vrf *vrf;
1185
1186 vrf_id_t vrf_id = VRF_UNKNOWN;
1187
1188 assert(rn);
1189
1190 dest = rib_dest_from_rnode(rn);
1191 /*
1192 * We have an enqueued node with nothing to process here
1193 * let's just finish up and return;
1194 */
1195 if (!dest)
1196 return;
1197
1198 zvrf = rib_dest_vrf(dest);
1199 vrf_id = zvrf_id(zvrf);
1200
1201 vrf = vrf_lookup_by_id(vrf_id);
1202
1203 /*
1204 * we can have rn's that have a NULL info pointer
1205 * (dest). As such let's not let the deref happen
1206 * additionally we know RNODE_FOREACH_RE_SAFE
1207 * will not iterate so we are ok.
1208 */
1209 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1210 struct route_entry *re = re_list_first(&dest->routes);
1211
1212 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1213 VRF_LOGNAME(vrf), vrf_id, re->table, rn,
1214 rn);
1215 }
1216
1217 old_fib = dest->selected_fib;
1218
1219 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1220 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1221 char flags_buf[128];
1222 char status_buf[128];
1223
1224 zlog_debug(
1225 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1226 VRF_LOGNAME(vrf), vrf_id, re->table, rn, re,
1227 zebra_route_string(re->type),
1228 _dump_re_status(re, status_buf,
1229 sizeof(status_buf)),
1230 zclient_dump_route_flags(re->flags, flags_buf,
1231 sizeof(flags_buf)),
1232 re->distance, re->metric);
1233 }
1234
1235 /* Currently selected re. */
1236 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1237 assert(old_selected == NULL);
1238 old_selected = re;
1239 }
1240
1241 /* Skip deleted entries from selection */
1242 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1243 continue;
1244
1245 /*
1246 * If the route entry has changed, verify/resolve
1247 * the nexthops associated with the entry.
1248 *
1249 * In any event if we have nexthops that are not active
1250 * then we cannot use this particular route entry so
1251 * skip it.
1252 */
1253 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1254 if (!nexthop_active_update(rn, re)) {
1255 const struct prefix *p;
1256 struct rib_table_info *info;
1257
1258 if (re->type == ZEBRA_ROUTE_TABLE) {
1259 /* XXX: HERE BE DRAGONS!!!!!
1260 * In all honesty, I have not yet
1261 * figured out what this part does or
1262 * why the ROUTE_ENTRY_CHANGED test
1263 * above is correct or why we need to
1264 * delete a route here, and also not
1265 * whether this concerns both selected
1266 * and fib route, or only selected
1267 * or only fib
1268 *
1269 * This entry was denied by the 'ip
1270 * protocol
1271 * table' route-map, we need to delete
1272 * it */
1273 if (re != old_selected) {
1274 if (IS_ZEBRA_DEBUG_RIB)
1275 zlog_debug(
1276 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1277 __func__,
1278 VRF_LOGNAME(
1279 vrf),
1280 vrf_id, rn);
1281 rib_unlink(rn, re);
1282 continue;
1283 } else
1284 SET_FLAG(re->status,
1285 ROUTE_ENTRY_REMOVED);
1286 }
1287
1288 info = srcdest_rnode_table_info(rn);
1289 srcdest_rnode_prefixes(rn, &p, NULL);
1290 zsend_route_notify_owner(
1291 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
1292 info->afi, info->safi);
1293 continue;
1294 }
1295 } else {
1296 /*
1297 * If the re has not changed and the nhg we have is
1298 * not usable, then we cannot use this route entry
1299 * for consideration, as that the route will just
1300 * not install if it is selected.
1301 */
1302 if (!nexthop_group_active_nexthop_num(&re->nhe->nhg))
1303 continue;
1304 }
1305
1306 /* Infinite distance. */
1307 if (re->distance == DISTANCE_INFINITY &&
1308 re->type != ZEBRA_ROUTE_KERNEL) {
1309 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1310 continue;
1311 }
1312
1313 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1314 best = rib_choose_best(new_fib, re);
1315 if (new_fib && best != new_fib)
1316 UNSET_FLAG(new_fib->status,
1317 ROUTE_ENTRY_CHANGED);
1318 new_fib = best;
1319 } else {
1320 best = rib_choose_best(new_selected, re);
1321 if (new_selected && best != new_selected)
1322 UNSET_FLAG(new_selected->status,
1323 ROUTE_ENTRY_CHANGED);
1324 new_selected = best;
1325 }
1326 if (best != re)
1327 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1328 } /* RNODE_FOREACH_RE */
1329
1330 /* If no FIB override route, use the selected route also for FIB */
1331 if (new_fib == NULL)
1332 new_fib = new_selected;
1333
1334 /* After the cycle is finished, the following pointers will be set:
1335 * old_selected --- RE entry currently having SELECTED
1336 * new_selected --- RE entry that is newly SELECTED
1337 * old_fib --- RE entry currently in kernel FIB
1338 * new_fib --- RE entry that is newly to be in kernel FIB
1339 *
1340 * new_selected will get SELECTED flag, and is going to be redistributed
1341 * the zclients. new_fib (which can be new_selected) will be installed
1342 * in kernel.
1343 */
1344
1345 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1346 struct route_entry *entry;
1347
1348 entry = old_selected
1349 ? old_selected
1350 : new_selected
1351 ? new_selected
1352 : old_fib ? old_fib
1353 : new_fib ? new_fib : NULL;
1354
1355 zlog_debug(
1356 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1357 VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn,
1358 (void *)old_selected, (void *)new_selected,
1359 (void *)old_fib, (void *)new_fib);
1360 }
1361
1362 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1363 * fib == selected */
1364 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1365 ROUTE_ENTRY_CHANGED);
1366
1367 /* Update SELECTED entry */
1368 if (old_selected != new_selected || selected_changed) {
1369
1370 if (new_selected && new_selected != new_fib)
1371 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
1372
1373 if (new_selected)
1374 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1375
1376 if (old_selected) {
1377 /*
1378 * If we're removing the old entry, we should tell
1379 * redist subscribers about that *if* they aren't
1380 * going to see a redist for the new entry.
1381 */
1382 if (!new_selected || CHECK_FLAG(old_selected->status,
1383 ROUTE_ENTRY_REMOVED))
1384 redistribute_delete(rn, old_selected,
1385 new_selected);
1386
1387 if (old_selected != new_selected)
1388 UNSET_FLAG(old_selected->flags,
1389 ZEBRA_FLAG_SELECTED);
1390 }
1391 }
1392
1393 /* Update fib according to selection results */
1394 if (new_fib && old_fib)
1395 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1396 else if (new_fib)
1397 rib_process_add_fib(zvrf, rn, new_fib);
1398 else if (old_fib)
1399 rib_process_del_fib(zvrf, rn, old_fib);
1400
1401 /* Remove all RE entries queued for removal */
1402 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1403 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1404 if (IS_ZEBRA_DEBUG_RIB) {
1405 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1406 (void *)rn, (void *)re);
1407 }
1408 rib_unlink(rn, re);
1409 }
1410 }
1411
1412 /*
1413 * Check if the dest can be deleted now.
1414 */
1415 rib_gc_dest(rn);
1416 }
1417
1418 static void zebra_rib_evaluate_mpls(struct route_node *rn)
1419 {
1420 rib_dest_t *dest = rib_dest_from_rnode(rn);
1421 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
1422
1423 if (!dest)
1424 return;
1425
1426 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1427 if (IS_ZEBRA_DEBUG_MPLS)
1428 zlog_debug(
1429 "%s(%u): Scheduling all LSPs upon RIB completion",
1430 zvrf_name(zvrf), zvrf_id(zvrf));
1431 zebra_mpls_lsp_schedule(zvrf);
1432 mpls_unmark_lsps_for_processing(rn);
1433 }
1434 }
1435
1436 /*
1437 * Utility to match route with dplane context data
1438 */
1439 static bool rib_route_match_ctx(const struct route_entry *re,
1440 const struct zebra_dplane_ctx *ctx,
1441 bool is_update)
1442 {
1443 bool result = false;
1444
1445 if (is_update) {
1446 /*
1447 * In 'update' case, we test info about the 'previous' or
1448 * 'old' route
1449 */
1450 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1451 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1452 result = true;
1453
1454 /* We use an extra test for statics, and another for
1455 * kernel routes.
1456 */
1457 if (re->type == ZEBRA_ROUTE_STATIC &&
1458 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1459 re->tag != dplane_ctx_get_old_tag(ctx))) {
1460 result = false;
1461 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1462 re->metric !=
1463 dplane_ctx_get_old_metric(ctx)) {
1464 result = false;
1465 }
1466 }
1467
1468 } else {
1469 /*
1470 * Ordinary, single-route case using primary context info
1471 */
1472 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1473 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1474 /* Skip route that's been deleted */
1475 goto done;
1476 }
1477
1478 if ((re->type == dplane_ctx_get_type(ctx)) &&
1479 (re->instance == dplane_ctx_get_instance(ctx))) {
1480 result = true;
1481
1482 /* We use an extra test for statics, and another for
1483 * kernel routes.
1484 */
1485 if (re->type == ZEBRA_ROUTE_STATIC &&
1486 (re->distance != dplane_ctx_get_distance(ctx) ||
1487 re->tag != dplane_ctx_get_tag(ctx))) {
1488 result = false;
1489 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1490 re->metric != dplane_ctx_get_metric(ctx)) {
1491 result = false;
1492 } else if (re->type == ZEBRA_ROUTE_CONNECT) {
1493 result = nexthop_group_equal_no_recurse(
1494 &re->nhe->nhg, dplane_ctx_get_ng(ctx));
1495 }
1496 }
1497 }
1498
1499 done:
1500 return result;
1501 }
1502
1503 static void zebra_rib_fixup_system(struct route_node *rn)
1504 {
1505 struct route_entry *re;
1506
1507 RNODE_FOREACH_RE(rn, re) {
1508 struct nexthop *nhop;
1509
1510 if (!RIB_SYSTEM_ROUTE(re))
1511 continue;
1512
1513 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1514 continue;
1515
1516 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1517 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1518 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
1519
1520 for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) {
1521 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1522 continue;
1523
1524 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1525 }
1526 }
1527 }
1528
1529 /* Route comparison logic, with various special cases. */
1530 static bool rib_compare_routes(const struct route_entry *re1,
1531 const struct route_entry *re2)
1532 {
1533 if (re1->type != re2->type)
1534 return false;
1535
1536 if (re1->instance != re2->instance)
1537 return false;
1538
1539 if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric)
1540 return false;
1541
1542 if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
1543 re1->distance != re2->distance)
1544 return false;
1545
1546 /* We support multiple connected routes: this supports multiple
1547 * v6 link-locals, and we also support multiple addresses in the same
1548 * subnet on a single interface.
1549 */
1550 if (re1->type != ZEBRA_ROUTE_CONNECT)
1551 return true;
1552
1553 return false;
1554 }
1555
1556 /*
1557 * Compare nexthop lists from a route and a dplane context; test whether
1558 * the list installed in the FIB matches the route's list.
1559 * Set 'changed_p' to 'true' if there were changes to the route's
1560 * installed nexthops.
1561 *
1562 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1563 * list.
1564 */
1565 static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg,
1566 const struct nexthop_group *ctx_nhg,
1567 bool *changed_p)
1568 {
1569 bool matched_p = true;
1570 struct nexthop *nexthop, *ctx_nexthop;
1571
1572 /* Get the first `installed` one to check against.
1573 * If the dataplane doesn't set these to be what was actually installed,
1574 * it will just be whatever was in re->nhe->nhg?
1575 */
1576 ctx_nexthop = ctx_nhg->nexthop;
1577
1578 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
1579 || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1580 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1581
1582 for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) {
1583
1584 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1585 continue;
1586
1587 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1588 continue;
1589
1590 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1591 if (!nexthop_same(ctx_nexthop, nexthop)) {
1592 /* If the FIB doesn't know about the nexthop,
1593 * it's not installed
1594 */
1595 if (IS_ZEBRA_DEBUG_RIB_DETAILED ||
1596 IS_ZEBRA_DEBUG_NHG_DETAIL) {
1597 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1598 __func__, nexthop,
1599 (CHECK_FLAG(nexthop->flags,
1600 NEXTHOP_FLAG_FIB) ?
1601 "(FIB)":""));
1602 }
1603 matched_p = false;
1604
1605 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1606 *changed_p = true;
1607
1608 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1609
1610 /* Keep checking nexthops */
1611 continue;
1612 }
1613
1614 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
1615 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1616 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1617 zlog_debug("%s: rib nh %pNHv -> installed",
1618 __func__, nexthop);
1619
1620 *changed_p = true;
1621 }
1622
1623 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1624 } else {
1625 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1626 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1627 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1628 __func__, nexthop);
1629
1630 *changed_p = true;
1631 }
1632
1633 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1634 }
1635
1636 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1637 }
1638
1639 return matched_p;
1640 }
1641
1642 /*
1643 * Update a route from a dplane context. This consolidates common code
1644 * that can be used in processing of results from FIB updates, and in
1645 * async notification processing.
1646 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1647 */
1648 static bool rib_update_re_from_ctx(struct route_entry *re,
1649 struct route_node *rn,
1650 struct zebra_dplane_ctx *ctx)
1651 {
1652 struct nexthop *nexthop;
1653 bool matched;
1654 const struct nexthop_group *ctxnhg;
1655 struct nexthop_group *re_nhg;
1656 bool is_selected = false; /* Is 're' currently the selected re? */
1657 bool changed_p = false; /* Change to nexthops? */
1658 rib_dest_t *dest;
1659 struct vrf *vrf;
1660
1661 vrf = vrf_lookup_by_id(re->vrf_id);
1662
1663 dest = rib_dest_from_rnode(rn);
1664 if (dest)
1665 is_selected = (re == dest->selected_fib);
1666
1667 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1668 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1669 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1670 (is_selected ? "" : "NOT "), re);
1671
1672 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1673 * If the installed set differs from the set requested by the rib/owner,
1674 * we use the fib-specific nexthop-group to record the actual FIB
1675 * status.
1676 */
1677 matched = false;
1678 ctxnhg = dplane_ctx_get_ng(ctx);
1679
1680 /* Check route's fib group and incoming notif group for equivalence.
1681 *
1682 * Let's assume the nexthops are ordered here to save time.
1683 */
1684 /* TODO -- this isn't testing or comparing the FIB flags; we should
1685 * do a more explicit loop, checking the incoming notification's flags.
1686 */
1687 if (re->fib_ng.nexthop && ctxnhg->nexthop &&
1688 nexthop_group_equal(&re->fib_ng, ctxnhg))
1689 matched = true;
1690
1691 /* If the new FIB set matches the existing FIB set, we're done. */
1692 if (matched) {
1693 if (IS_ZEBRA_DEBUG_RIB)
1694 zlog_debug(
1695 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1696 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1697 goto check_backups;
1698
1699 } else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) {
1700 /*
1701 * Free stale fib list and move on to check the rib nhg.
1702 */
1703 if (IS_ZEBRA_DEBUG_RIB)
1704 zlog_debug(
1705 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1706 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1707 nexthops_free(re->fib_ng.nexthop);
1708 re->fib_ng.nexthop = NULL;
1709
1710 UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1711
1712 /* Note that the installed nexthops have changed */
1713 changed_p = true;
1714 } else {
1715 if (IS_ZEBRA_DEBUG_RIB)
1716 zlog_debug(
1717 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1718 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1719 }
1720
1721 /*
1722 * Compare with the rib nexthop group. The comparison here is different:
1723 * the RIB group may be a superset of the list installed in the FIB. We
1724 * walk the RIB group, looking for the 'installable' candidate
1725 * nexthops, and then check those against the set
1726 * that is actually installed.
1727 *
1728 * Assume nexthops are ordered here as well.
1729 */
1730
1731 /* If nothing is installed, we can skip some of the checking/comparison
1732 * of nexthops.
1733 */
1734 if (ctxnhg->nexthop == NULL) {
1735 changed_p = true;
1736 goto no_nexthops;
1737 }
1738
1739 matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p);
1740
1741 /* If all nexthops were processed, we're done */
1742 if (matched) {
1743 if (IS_ZEBRA_DEBUG_RIB)
1744 zlog_debug(
1745 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1746 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1747 (changed_p ? "true" : "false"));
1748 goto check_backups;
1749 }
1750
1751 no_nexthops:
1752
1753 /* FIB nexthop set differs from the RIB set:
1754 * create a fib-specific nexthop-group
1755 */
1756 if (IS_ZEBRA_DEBUG_RIB)
1757 zlog_debug(
1758 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1759 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1760 (changed_p ? "true" : "false"),
1761 ctxnhg->nexthop != NULL ? "" : " (empty)");
1762
1763 /* Set the flag about the dedicated fib list */
1764 if (zrouter.asic_notification_nexthop_control) {
1765 SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1766 if (ctxnhg->nexthop)
1767 copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop,
1768 NULL);
1769 }
1770
1771 check_backups:
1772
1773 /*
1774 * Check the status of the route's backup nexthops, if any.
1775 * The logic for backups is somewhat different: if any backup is
1776 * installed, a new fib nhg will be attached to the route.
1777 */
1778 re_nhg = zebra_nhg_get_backup_nhg(re->nhe);
1779 if (re_nhg == NULL)
1780 goto done; /* No backup nexthops */
1781
1782 /* First check the route's 'fib' list of backups, if it's present
1783 * from some previous event.
1784 */
1785 re_nhg = &re->fib_backup_ng;
1786 ctxnhg = dplane_ctx_get_backup_ng(ctx);
1787
1788 matched = false;
1789 if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg))
1790 matched = true;
1791
1792 /* If the new FIB set matches an existing FIB set, we're done. */
1793 if (matched) {
1794 if (IS_ZEBRA_DEBUG_RIB)
1795 zlog_debug(
1796 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1797 VRF_LOGNAME(vrf), re->vrf_id, rn);
1798 goto done;
1799
1800 } else if (re->fib_backup_ng.nexthop) {
1801 /*
1802 * Free stale fib backup list and move on to check
1803 * the route's backups.
1804 */
1805 if (IS_ZEBRA_DEBUG_RIB)
1806 zlog_debug(
1807 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1808 VRF_LOGNAME(vrf), re->vrf_id, rn);
1809 nexthops_free(re->fib_backup_ng.nexthop);
1810 re->fib_backup_ng.nexthop = NULL;
1811
1812 /* Note that the installed nexthops have changed */
1813 changed_p = true;
1814 } else {
1815 if (IS_ZEBRA_DEBUG_RIB)
1816 zlog_debug(
1817 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1818 VRF_LOGNAME(vrf), re->vrf_id, rn);
1819 }
1820
1821 /*
1822 * If a FIB backup nexthop set exists, attach a copy
1823 * to the route if any backup is installed
1824 */
1825 if (ctxnhg && ctxnhg->nexthop) {
1826
1827 for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) {
1828 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1829 break;
1830 }
1831
1832 /* If no installed backups, we're done */
1833 if (nexthop == NULL)
1834 goto done;
1835
1836 if (IS_ZEBRA_DEBUG_RIB)
1837 zlog_debug(
1838 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1839 VRF_LOGNAME(vrf), re->vrf_id, rn,
1840 (changed_p ? "true" : "false"));
1841
1842 copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop,
1843 NULL);
1844 }
1845
1846 done:
1847
1848 return changed_p;
1849 }
1850
1851 /*
1852 * Helper to locate a zebra route-node from a dplane context. This is used
1853 * when processing dplane results, e.g. Note well: the route-node is returned
1854 * with a ref held - route_unlock_node() must be called eventually.
1855 */
1856 struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
1857 {
1858 struct route_table *table = NULL;
1859 struct route_node *rn = NULL;
1860 const struct prefix *dest_pfx, *src_pfx;
1861
1862 /* Locate rn and re(s) from ctx */
1863
1864 table = zebra_vrf_lookup_table_with_table_id(
1865 dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx),
1866 dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx));
1867 if (table == NULL) {
1868 if (IS_ZEBRA_DEBUG_DPLANE) {
1869 zlog_debug(
1870 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1871 dplane_ctx_get_afi(ctx),
1872 dplane_ctx_get_safi(ctx),
1873 vrf_id_to_name(dplane_ctx_get_vrf(ctx)),
1874 dplane_ctx_get_vrf(ctx));
1875 }
1876 goto done;
1877 }
1878
1879 dest_pfx = dplane_ctx_get_dest(ctx);
1880 src_pfx = dplane_ctx_get_src(ctx);
1881
1882 rn = srcdest_rnode_get(table, dest_pfx,
1883 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1884
1885 done:
1886 return rn;
1887 }
1888
1889
1890
1891 /*
1892 * Route-update results processing after async dataplane update.
1893 */
1894 static void rib_process_result(struct zebra_dplane_ctx *ctx)
1895 {
1896 struct zebra_vrf *zvrf = NULL;
1897 struct vrf *vrf;
1898 struct route_node *rn = NULL;
1899 struct route_entry *re = NULL, *old_re = NULL, *rib;
1900 bool is_update = false;
1901 enum dplane_op_e op;
1902 enum zebra_dplane_result status;
1903 uint32_t seq;
1904 rib_dest_t *dest;
1905 bool fib_changed = false;
1906 struct rib_table_info *info;
1907 bool rt_delete = false;
1908
1909 zvrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
1910 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
1911
1912 /* Locate rn and re(s) from ctx */
1913 rn = rib_find_rn_from_ctx(ctx);
1914 if (rn == NULL) {
1915 if (IS_ZEBRA_DEBUG_DPLANE) {
1916 zlog_debug(
1917 "Failed to process dplane results: no route for %s(%u):%pRN",
1918 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn);
1919 }
1920 goto done;
1921 }
1922
1923 dest = rib_dest_from_rnode(rn);
1924 info = srcdest_rnode_table_info(rn);
1925
1926 op = dplane_ctx_get_op(ctx);
1927 status = dplane_ctx_get_status(ctx);
1928
1929 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1930 zlog_debug(
1931 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1932 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
1933 dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op),
1934 dplane_res2str(status));
1935
1936 /*
1937 * Update is a bit of a special case, where we may have both old and new
1938 * routes to post-process.
1939 */
1940 is_update = dplane_ctx_is_update(ctx);
1941
1942 /*
1943 * Take a pass through the routes, look for matches with the context
1944 * info.
1945 */
1946 RNODE_FOREACH_RE(rn, rib) {
1947
1948 if (re == NULL) {
1949 if (rib_route_match_ctx(rib, ctx, false))
1950 re = rib;
1951 }
1952
1953 /* Check for old route match */
1954 if (is_update && (old_re == NULL)) {
1955 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
1956 old_re = rib;
1957 }
1958
1959 /* Have we found the routes we need to work on? */
1960 if (re && ((!is_update || old_re)))
1961 break;
1962 }
1963
1964 seq = dplane_ctx_get_seq(ctx);
1965
1966 /*
1967 * Check sequence number(s) to detect stale results before continuing
1968 */
1969 if (re) {
1970 if (re->dplane_sequence != seq) {
1971 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1972 zlog_debug(
1973 "%s(%u):%pRN Stale dplane result for re %p",
1974 VRF_LOGNAME(vrf),
1975 dplane_ctx_get_vrf(ctx), rn, re);
1976 } else {
1977 if (!zrouter.asic_offloaded ||
1978 (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) ||
1979 CHECK_FLAG(re->flags,
1980 ZEBRA_FLAG_OFFLOAD_FAILED))) {
1981 UNSET_FLAG(re->status,
1982 ROUTE_ENTRY_ROUTE_REPLACING);
1983 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1984 }
1985 }
1986 }
1987
1988 if (old_re) {
1989 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
1990 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1991 zlog_debug(
1992 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
1993 VRF_LOGNAME(vrf),
1994 dplane_ctx_get_vrf(ctx), old_re->table,
1995 rn, old_re);
1996 } else
1997 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
1998 }
1999
2000 switch (op) {
2001 case DPLANE_OP_ROUTE_INSTALL:
2002 case DPLANE_OP_ROUTE_UPDATE:
2003 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2004 if (re) {
2005 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2006 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2007 }
2008 /*
2009 * On an update operation from the same route type
2010 * context retrieval currently has no way to know
2011 * which was the old and which was the new.
2012 * So don't unset our flags that we just set.
2013 * We know redistribution is ok because the
2014 * old_re in this case is used for nothing
2015 * more than knowing whom to contact if necessary.
2016 */
2017 if (old_re && old_re != re) {
2018 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2019 UNSET_FLAG(old_re->status,
2020 ROUTE_ENTRY_INSTALLED);
2021 }
2022
2023 /* Update zebra route based on the results in
2024 * the context struct.
2025 */
2026 if (re) {
2027 fib_changed =
2028 rib_update_re_from_ctx(re, rn, ctx);
2029
2030 if (!fib_changed) {
2031 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2032 zlog_debug(
2033 "%s(%u:%u):%pRN no fib change for re",
2034 VRF_LOGNAME(vrf),
2035 dplane_ctx_get_vrf(ctx),
2036 dplane_ctx_get_table(
2037 ctx),
2038 rn);
2039 }
2040
2041 /* Redistribute if this is the selected re */
2042 if (dest && re == dest->selected_fib)
2043 redistribute_update(rn, re, old_re);
2044 }
2045
2046 /*
2047 * System routes are weird in that they
2048 * allow multiple to be installed that match
2049 * to the same prefix, so after we get the
2050 * result we need to clean them up so that
2051 * we can actually use them.
2052 */
2053 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2054 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2055 zebra_rib_fixup_system(rn);
2056
2057 if (zvrf)
2058 zvrf->installs++;
2059
2060 /* Notify route owner */
2061 if (zebra_router_notify_on_ack())
2062 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2063 else {
2064 if (re) {
2065 if (CHECK_FLAG(re->flags,
2066 ZEBRA_FLAG_OFFLOADED))
2067 zsend_route_notify_owner_ctx(
2068 ctx,
2069 ZAPI_ROUTE_INSTALLED);
2070 if (CHECK_FLAG(
2071 re->flags,
2072 ZEBRA_FLAG_OFFLOAD_FAILED))
2073 zsend_route_notify_owner_ctx(
2074 ctx,
2075 ZAPI_ROUTE_FAIL_INSTALL);
2076 }
2077 }
2078 } else {
2079 if (re) {
2080 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2081 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2082 } if (old_re)
2083 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2084 if (re)
2085 zsend_route_notify_owner(
2086 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
2087 info->afi, info->safi);
2088
2089 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2090 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2091 dplane_ctx_get_table(ctx), rn);
2092 }
2093 break;
2094 case DPLANE_OP_ROUTE_DELETE:
2095 rt_delete = true;
2096 if (re)
2097 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2098 /*
2099 * In the delete case, the zebra core datastructs were
2100 * updated (or removed) at the time the delete was issued,
2101 * so we're just notifying the route owner.
2102 */
2103 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2104 if (re) {
2105 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2106 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2107 }
2108 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
2109
2110 if (zvrf)
2111 zvrf->removals++;
2112 } else {
2113 if (re)
2114 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2115 zsend_route_notify_owner_ctx(ctx,
2116 ZAPI_ROUTE_REMOVE_FAIL);
2117
2118 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2119 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2120 dplane_ctx_get_table(ctx), rn);
2121 }
2122
2123 /*
2124 * System routes are weird in that they
2125 * allow multiple to be installed that match
2126 * to the same prefix, so after we get the
2127 * result we need to clean them up so that
2128 * we can actually use them.
2129 */
2130 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2131 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2132 zebra_rib_fixup_system(rn);
2133 break;
2134
2135 case DPLANE_OP_NONE:
2136 case DPLANE_OP_ROUTE_NOTIFY:
2137 case DPLANE_OP_NH_INSTALL:
2138 case DPLANE_OP_NH_UPDATE:
2139 case DPLANE_OP_NH_DELETE:
2140 case DPLANE_OP_LSP_INSTALL:
2141 case DPLANE_OP_LSP_UPDATE:
2142 case DPLANE_OP_LSP_DELETE:
2143 case DPLANE_OP_LSP_NOTIFY:
2144 case DPLANE_OP_PW_INSTALL:
2145 case DPLANE_OP_PW_UNINSTALL:
2146 case DPLANE_OP_SYS_ROUTE_ADD:
2147 case DPLANE_OP_SYS_ROUTE_DELETE:
2148 case DPLANE_OP_ADDR_INSTALL:
2149 case DPLANE_OP_ADDR_UNINSTALL:
2150 case DPLANE_OP_MAC_INSTALL:
2151 case DPLANE_OP_MAC_DELETE:
2152 case DPLANE_OP_NEIGH_INSTALL:
2153 case DPLANE_OP_NEIGH_UPDATE:
2154 case DPLANE_OP_NEIGH_DELETE:
2155 case DPLANE_OP_VTEP_ADD:
2156 case DPLANE_OP_VTEP_DELETE:
2157 case DPLANE_OP_RULE_ADD:
2158 case DPLANE_OP_RULE_DELETE:
2159 case DPLANE_OP_RULE_UPDATE:
2160 case DPLANE_OP_NEIGH_DISCOVER:
2161 case DPLANE_OP_BR_PORT_UPDATE:
2162 case DPLANE_OP_IPTABLE_ADD:
2163 case DPLANE_OP_IPTABLE_DELETE:
2164 case DPLANE_OP_IPSET_ADD:
2165 case DPLANE_OP_IPSET_DELETE:
2166 case DPLANE_OP_IPSET_ENTRY_ADD:
2167 case DPLANE_OP_IPSET_ENTRY_DELETE:
2168 case DPLANE_OP_NEIGH_IP_INSTALL:
2169 case DPLANE_OP_NEIGH_IP_DELETE:
2170 case DPLANE_OP_NEIGH_TABLE_UPDATE:
2171 case DPLANE_OP_GRE_SET:
2172 case DPLANE_OP_INTF_ADDR_ADD:
2173 case DPLANE_OP_INTF_ADDR_DEL:
2174 case DPLANE_OP_INTF_NETCONFIG:
2175 case DPLANE_OP_INTF_INSTALL:
2176 case DPLANE_OP_INTF_UPDATE:
2177 case DPLANE_OP_INTF_DELETE:
2178 case DPLANE_OP_TC_QDISC_INSTALL:
2179 case DPLANE_OP_TC_QDISC_UNINSTALL:
2180 case DPLANE_OP_TC_CLASS_ADD:
2181 case DPLANE_OP_TC_CLASS_DELETE:
2182 case DPLANE_OP_TC_CLASS_UPDATE:
2183 case DPLANE_OP_TC_FILTER_ADD:
2184 case DPLANE_OP_TC_FILTER_DELETE:
2185 case DPLANE_OP_TC_FILTER_UPDATE:
2186 break;
2187 }
2188
2189 zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete);
2190 zebra_rib_evaluate_mpls(rn);
2191 done:
2192
2193 if (rn)
2194 route_unlock_node(rn);
2195 }
2196
2197 /*
2198 * Count installed/FIB nexthops
2199 */
2200 static int rib_count_installed_nh(struct route_entry *re)
2201 {
2202 int count = 0;
2203 struct nexthop *nexthop;
2204 struct nexthop_group *nhg;
2205
2206 nhg = rib_get_fib_nhg(re);
2207
2208 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2209 /* The meaningful flag depends on where the installed
2210 * nexthops reside.
2211 */
2212 if (nhg == &(re->fib_ng)) {
2213 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2214 count++;
2215 } else {
2216 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2217 count++;
2218 }
2219 }
2220
2221 nhg = rib_get_fib_backup_nhg(re);
2222 if (nhg) {
2223 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2224 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2225 count++;
2226 }
2227 }
2228
2229 return count;
2230 }
2231
2232 /*
2233 * Handle notification from async dataplane: the dataplane has detected
2234 * some change to a route, and notifies zebra so that the control plane
2235 * can reflect that change.
2236 */
2237 static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2238 {
2239 struct route_node *rn = NULL;
2240 struct route_entry *re = NULL;
2241 struct vrf *vrf;
2242 struct nexthop *nexthop;
2243 rib_dest_t *dest;
2244 bool fib_changed = false;
2245 bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
2246 int start_count, end_count;
2247
2248 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
2249
2250 /* Locate rn and re(s) from ctx */
2251 rn = rib_find_rn_from_ctx(ctx);
2252 if (rn == NULL) {
2253 if (debug_p) {
2254 zlog_debug(
2255 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2256 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2257 dplane_ctx_get_table(ctx), rn);
2258 }
2259 goto done;
2260 }
2261
2262 dest = rib_dest_from_rnode(rn);
2263
2264 if (debug_p)
2265 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2266 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2267 dplane_ctx_get_table(ctx), rn, ctx);
2268
2269 /*
2270 * Take a pass through the routes, look for matches with the context
2271 * info.
2272 */
2273 RNODE_FOREACH_RE(rn, re) {
2274 if (rib_route_match_ctx(re, ctx, false /*!update*/))
2275 break;
2276 }
2277
2278 /* No match? Nothing we can do */
2279 if (re == NULL) {
2280 if (debug_p)
2281 zlog_debug(
2282 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2283 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2284 dplane_ctx_get_table(ctx), rn,
2285 zebra_route_string(dplane_ctx_get_type(ctx)));
2286
2287 goto done;
2288 }
2289
2290 /* Ensure we clear the QUEUED flag */
2291 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
2292 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
2293
2294 /* Is this a notification that ... matters? We mostly care about
2295 * the route that is currently selected for installation; we may also
2296 * get an un-install notification, and handle that too.
2297 */
2298 if (re != dest->selected_fib) {
2299 /*
2300 * If we need to, clean up after a delete that was part of
2301 * an update operation.
2302 */
2303 end_count = 0;
2304 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2305 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2306 end_count++;
2307 }
2308
2309 /* If no nexthops or none installed, ensure that this re
2310 * gets its 'installed' flag cleared.
2311 */
2312 if (end_count == 0) {
2313 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2314 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2315 if (debug_p)
2316 zlog_debug(
2317 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2318 VRF_LOGNAME(vrf),
2319 dplane_ctx_get_vrf(ctx),
2320 dplane_ctx_get_table(ctx), rn,
2321 zebra_route_string(
2322 dplane_ctx_get_type(ctx)));
2323 } else {
2324 /* At least report on the event. */
2325 if (debug_p)
2326 zlog_debug(
2327 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2328 VRF_LOGNAME(vrf),
2329 dplane_ctx_get_vrf(ctx),
2330 dplane_ctx_get_table(ctx), rn,
2331 zebra_route_string(
2332 dplane_ctx_get_type(ctx)));
2333 }
2334 goto done;
2335 } else {
2336 uint32_t flags = dplane_ctx_get_flags(ctx);
2337
2338 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOADED)) {
2339 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2340 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2341 }
2342 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOAD_FAILED)) {
2343 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2344 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2345 }
2346 if (CHECK_FLAG(flags, ZEBRA_FLAG_TRAPPED))
2347 SET_FLAG(re->flags, ZEBRA_FLAG_TRAPPED);
2348 }
2349
2350 /* We'll want to determine whether the installation status of the
2351 * route has changed: we'll check the status before processing,
2352 * and then again if there's been a change.
2353 */
2354 start_count = 0;
2355
2356 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2357 start_count = rib_count_installed_nh(re);
2358
2359 /* Update zebra's nexthop FIB flags based on the context struct's
2360 * nexthops.
2361 */
2362 fib_changed = rib_update_re_from_ctx(re, rn, ctx);
2363
2364 if (!fib_changed) {
2365 if (debug_p)
2366 zlog_debug(
2367 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2368 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2369 dplane_ctx_get_table(ctx), rn);
2370 }
2371
2372 /*
2373 * Perform follow-up work if the actual status of the prefix
2374 * changed.
2375 */
2376 end_count = rib_count_installed_nh(re);
2377
2378 /* Various fib transitions: changed nexthops; from installed to
2379 * not-installed; or not-installed to installed.
2380 */
2381 if (zrouter.asic_notification_nexthop_control) {
2382 if (start_count > 0 && end_count > 0) {
2383 if (debug_p)
2384 zlog_debug(
2385 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2386 VRF_LOGNAME(vrf),
2387 dplane_ctx_get_vrf(ctx),
2388 dplane_ctx_get_table(ctx), rn);
2389
2390 /* Changed nexthops - update kernel/others */
2391 dplane_route_notif_update(rn, re,
2392 DPLANE_OP_ROUTE_UPDATE, ctx);
2393
2394 } else if (start_count == 0 && end_count > 0) {
2395 if (debug_p)
2396 zlog_debug(
2397 "%s(%u:%u):%pRN installed transition from dplane notification",
2398 VRF_LOGNAME(vrf),
2399 dplane_ctx_get_vrf(ctx),
2400 dplane_ctx_get_table(ctx), rn);
2401
2402 /* We expect this to be the selected route, so we want
2403 * to tell others about this transition.
2404 */
2405 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2406
2407 /* Changed nexthops - update kernel/others */
2408 dplane_route_notif_update(rn, re,
2409 DPLANE_OP_ROUTE_UPDATE, ctx);
2410
2411 /* Redistribute, lsp, and nht update */
2412 redistribute_update(rn, re, NULL);
2413
2414 } else if (start_count > 0 && end_count == 0) {
2415 if (debug_p)
2416 zlog_debug(
2417 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2418 VRF_LOGNAME(vrf),
2419 dplane_ctx_get_vrf(ctx),
2420 dplane_ctx_get_table(ctx), rn);
2421
2422 /* Transition from _something_ installed to _nothing_
2423 * installed.
2424 */
2425 /* We expect this to be the selected route, so we want
2426 * to tell others about this transistion.
2427 */
2428 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2429
2430 /* Changed nexthops - update kernel/others */
2431 dplane_route_notif_update(rn, re,
2432 DPLANE_OP_ROUTE_DELETE, ctx);
2433
2434 /* Redistribute, lsp, and nht update */
2435 redistribute_delete(rn, re, NULL);
2436 }
2437 }
2438
2439 if (!zebra_router_notify_on_ack()) {
2440 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED))
2441 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2442 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED))
2443 zsend_route_notify_owner_ctx(ctx,
2444 ZAPI_ROUTE_FAIL_INSTALL);
2445 }
2446
2447 /* Make any changes visible for lsp and nexthop-tracking processing */
2448 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
2449 false);
2450
2451 zebra_rib_evaluate_mpls(rn);
2452
2453 done:
2454 if (rn)
2455 route_unlock_node(rn);
2456 }
2457
2458 /*
2459 * Process a node from the EVPN/VXLAN subqueue.
2460 */
2461 static void process_subq_evpn(struct listnode *lnode)
2462 {
2463 struct wq_evpn_wrapper *w;
2464
2465 /* In general, the list node points to a wrapper object
2466 * holding the info necessary to make some update.
2467 */
2468 w = listgetdata(lnode);
2469 if (!w)
2470 return;
2471
2472 if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) {
2473 if (w->add_p)
2474 zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr,
2475 &w->ip, &w->prefix);
2476 else
2477 zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip,
2478 &w->prefix);
2479 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) {
2480 if (w->add_p)
2481 zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4,
2482 w->esr_rxed, w->df_alg,
2483 w->df_pref);
2484 else
2485 zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4);
2486 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) {
2487 uint16_t ipa_len = 0;
2488
2489 if (w->ip.ipa_type == IPADDR_V4)
2490 ipa_len = IPV4_MAX_BYTELEN;
2491 else if (w->ip.ipa_type == IPADDR_V6)
2492 ipa_len = IPV6_MAX_BYTELEN;
2493
2494 if (w->add_p)
2495 zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len,
2496 &w->ip, w->flags, w->seq,
2497 w->vtep_ip, &w->esi);
2498 else
2499 zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len,
2500 &w->ip, w->vtep_ip);
2501 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) {
2502 if (w->add_p)
2503 zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni,
2504 w->vtep_ip, w->flags);
2505 else
2506 zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni,
2507 w->vtep_ip);
2508 }
2509
2510
2511 XFREE(MTYPE_WQ_WRAPPER, w);
2512 }
2513
2514 /*
2515 * Process the nexthop-group workqueue subqueue
2516 */
2517 static void process_subq_nhg(struct listnode *lnode)
2518 {
2519 struct nhg_ctx *ctx;
2520 struct nhg_hash_entry *nhe, *newnhe;
2521 struct wq_nhg_wrapper *w;
2522 uint8_t qindex = META_QUEUE_NHG;
2523
2524 w = listgetdata(lnode);
2525
2526 if (!w)
2527 return;
2528
2529 /* Two types of object - an update from the local kernel, or
2530 * an nhg update from a daemon.
2531 */
2532 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) {
2533 ctx = w->u.ctx;
2534
2535 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2536 zlog_debug(
2537 "NHG Context id=%u dequeued from sub-queue %s",
2538 ctx->id, subqueue2str(qindex));
2539
2540
2541 /* Process nexthop group updates coming 'up' from the OS */
2542 nhg_ctx_process(ctx);
2543
2544 } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) {
2545 nhe = w->u.nhe;
2546
2547 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2548 zlog_debug("NHG %u dequeued from sub-queue %s", nhe->id,
2549 subqueue2str(qindex));
2550
2551 /* Process incoming nhg update, probably from a proto daemon */
2552 newnhe = zebra_nhg_proto_add(nhe->id, nhe->type,
2553 nhe->zapi_instance,
2554 nhe->zapi_session, &nhe->nhg, 0);
2555
2556 /* Report error to daemon via ZAPI */
2557 if (newnhe == NULL)
2558 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2559 nhe->zapi_session, nhe->id,
2560 ZAPI_NHG_FAIL_INSTALL);
2561
2562 /* Free temp nhe - we own that memory. */
2563 zebra_nhg_free(nhe);
2564 }
2565
2566 XFREE(MTYPE_WQ_WRAPPER, w);
2567 }
2568
2569 static void process_subq_early_label(struct listnode *lnode)
2570 {
2571 struct wq_label_wrapper *w = listgetdata(lnode);
2572 struct zebra_vrf *zvrf;
2573
2574 if (!w)
2575 return;
2576
2577 zvrf = zebra_vrf_lookup_by_id(w->vrf_id);
2578 if (!zvrf) {
2579 XFREE(MTYPE_WQ_WRAPPER, w);
2580 return;
2581 }
2582
2583 switch (w->type) {
2584 case WQ_LABEL_FTN_UNINSTALL:
2585 zebra_mpls_ftn_uninstall(zvrf, w->ltype, &w->p, w->route_type,
2586 w->route_instance);
2587 break;
2588 case WQ_LABEL_LABELS_PROCESS:
2589 zebra_mpls_zapi_labels_process(w->add_p, zvrf, &w->zl);
2590 break;
2591 }
2592
2593 XFREE(MTYPE_WQ_WRAPPER, w);
2594 }
2595
2596 static void process_subq_route(struct listnode *lnode, uint8_t qindex)
2597 {
2598 struct route_node *rnode = NULL;
2599 rib_dest_t *dest = NULL;
2600 struct zebra_vrf *zvrf = NULL;
2601
2602 rnode = listgetdata(lnode);
2603 dest = rib_dest_from_rnode(rnode);
2604 assert(dest);
2605
2606 zvrf = rib_dest_vrf(dest);
2607
2608 rib_process(rnode);
2609
2610 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2611 struct route_entry *re = NULL;
2612
2613 /*
2614 * rib_process may have freed the dest
2615 * as part of the garbage collection. Let's
2616 * prevent stupidity from happening.
2617 */
2618 dest = rib_dest_from_rnode(rnode);
2619 if (dest)
2620 re = re_list_first(&dest->routes);
2621
2622 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2623 zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0,
2624 rnode, rnode, subqueue2str(qindex));
2625 }
2626
2627 if (rnode->info)
2628 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2629 RIB_ROUTE_QUEUED(qindex));
2630
2631 route_unlock_node(rnode);
2632 }
2633
2634 static void rib_re_nhg_free(struct route_entry *re)
2635 {
2636 if (re->nhe && re->nhe_id) {
2637 assert(re->nhe->id == re->nhe_id);
2638 route_entry_update_nhe(re, NULL);
2639 } else if (re->nhe && re->nhe->nhg.nexthop)
2640 nexthops_free(re->nhe->nhg.nexthop);
2641
2642 nexthops_free(re->fib_ng.nexthop);
2643 }
2644
2645 struct zebra_early_route {
2646 afi_t afi;
2647 safi_t safi;
2648 struct prefix p;
2649 struct prefix_ipv6 src_p;
2650 bool src_p_provided;
2651 struct route_entry *re;
2652 struct nhg_hash_entry *re_nhe;
2653 bool startup;
2654 bool deletion;
2655 bool fromkernel;
2656 };
2657
2658 static void early_route_memory_free(struct zebra_early_route *ere)
2659 {
2660 if (ere->re_nhe)
2661 zebra_nhg_free(ere->re_nhe);
2662
2663 XFREE(MTYPE_RE, ere->re);
2664 XFREE(MTYPE_WQ_WRAPPER, ere);
2665 }
2666
2667 static void process_subq_early_route_add(struct zebra_early_route *ere)
2668 {
2669 struct route_entry *re = ere->re;
2670 struct route_table *table;
2671 struct nhg_hash_entry *nhe = NULL;
2672 struct route_node *rn;
2673 struct route_entry *same = NULL, *first_same = NULL;
2674 int same_count = 0;
2675 rib_dest_t *dest;
2676
2677 /* Lookup table. */
2678 table = zebra_vrf_get_table_with_table_id(ere->afi, ere->safi,
2679 re->vrf_id, re->table);
2680 if (!table) {
2681 early_route_memory_free(ere);
2682 return;
2683 }
2684
2685 if (re->nhe_id > 0) {
2686 nhe = zebra_nhg_lookup_id(re->nhe_id);
2687
2688 if (!nhe) {
2689 /*
2690 * We've received from the kernel a nexthop id
2691 * that we don't have saved yet. More than likely
2692 * it has not been processed and is on the
2693 * queue to be processed. Let's stop what we
2694 * are doing and cause the meta q to be processed
2695 * storing this for later.
2696 *
2697 * This is being done this way because zebra
2698 * runs with the assumption t
2699 */
2700 flog_err(
2701 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2702 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2703 re->nhe_id, &ere->p);
2704
2705 early_route_memory_free(ere);
2706 return;
2707 }
2708 } else {
2709 /* Lookup nhe from route information */
2710 nhe = zebra_nhg_rib_find_nhe(ere->re_nhe, ere->afi);
2711 if (!nhe) {
2712 char buf2[PREFIX_STRLEN] = "";
2713
2714 flog_err(
2715 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2716 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2717 &ere->p, ere->src_p_provided ? " from " : "",
2718 ere->src_p_provided
2719 ? prefix2str(&ere->src_p, buf2,
2720 sizeof(buf2))
2721 : "");
2722
2723 early_route_memory_free(ere);
2724 return;
2725 }
2726 }
2727
2728 /*
2729 * Attach the re to the nhe's nexthop group.
2730 *
2731 * TODO: This will need to change when we start getting IDs from upper
2732 * level protocols, as the refcnt might be wrong, since it checks
2733 * if old_id != new_id.
2734 */
2735 route_entry_update_nhe(re, nhe);
2736
2737 /* Make it sure prefixlen is applied to the prefix. */
2738 apply_mask(&ere->p);
2739 if (ere->src_p_provided)
2740 apply_mask_ipv6(&ere->src_p);
2741
2742 /* Lookup route node.*/
2743 rn = srcdest_rnode_get(table, &ere->p,
2744 ere->src_p_provided ? &ere->src_p : NULL);
2745
2746 /*
2747 * If same type of route are installed, treat it as a implicit
2748 * withdraw. If the user has specified the No route replace semantics
2749 * for the install don't do a route replace.
2750 */
2751 RNODE_FOREACH_RE (rn, same) {
2752 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) {
2753 same_count++;
2754 continue;
2755 }
2756
2757 /* Compare various route_entry properties */
2758 if (rib_compare_routes(re, same)) {
2759 same_count++;
2760
2761 if (first_same == NULL)
2762 first_same = same;
2763 }
2764 }
2765
2766 same = first_same;
2767
2768 if (!ere->startup && (re->flags & ZEBRA_FLAG_SELFROUTE) &&
2769 zrouter.asic_offloaded) {
2770 if (!same) {
2771 if (IS_ZEBRA_DEBUG_RIB)
2772 zlog_debug(
2773 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2774 rn);
2775 /*
2776 * We are not on startup, this is a self route
2777 * and we have asic offload. Which means
2778 * we are getting a callback for a entry
2779 * that was already deleted to the kernel
2780 * but an earlier response was just handed
2781 * back. Drop it on the floor
2782 */
2783 early_route_memory_free(ere);
2784 return;
2785 }
2786 }
2787
2788 /* Set default distance by route type. */
2789 if (re->distance == 0) {
2790 if (same && !zebra_router_notify_on_ack())
2791 re->distance = same->distance;
2792 else
2793 re->distance = route_distance(re->type);
2794 }
2795
2796 if (re->metric == ROUTE_INSTALLATION_METRIC &&
2797 CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE)) {
2798 if (same && !zebra_router_notify_on_ack())
2799 re->metric = same->metric;
2800 else
2801 re->metric = 0;
2802 }
2803
2804 /* If this route is kernel/connected route, notify the dataplane. */
2805 if (RIB_SYSTEM_ROUTE(re)) {
2806 /* Notify dataplane */
2807 dplane_sys_route_add(rn, re);
2808 }
2809
2810 /* Link new re to node.*/
2811 if (IS_ZEBRA_DEBUG_RIB) {
2812 rnode_debug(
2813 rn, re->vrf_id,
2814 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2815 rn, re, zebra_route_string(re->type), same, same_count);
2816
2817 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2818 route_entry_dump(
2819 &ere->p,
2820 ere->src_p_provided ? &ere->src_p : NULL, re);
2821 }
2822
2823 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2824 rib_addnode(rn, re, 1);
2825
2826 /* Free implicit route.*/
2827 if (same)
2828 rib_delnode(rn, same);
2829
2830 /* See if we can remove some RE entries that are queued for
2831 * removal, but won't be considered in rib processing.
2832 */
2833 dest = rib_dest_from_rnode(rn);
2834 RNODE_FOREACH_RE_SAFE (rn, re, same) {
2835 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2836 /* If the route was used earlier, must retain it. */
2837 if (dest && re == dest->selected_fib)
2838 continue;
2839
2840 if (IS_ZEBRA_DEBUG_RIB)
2841 rnode_debug(rn, re->vrf_id,
2842 "rn %p, removing unneeded re %p",
2843 rn, re);
2844
2845 rib_unlink(rn, re);
2846 }
2847 }
2848
2849 route_unlock_node(rn);
2850 if (ere->re_nhe)
2851 zebra_nhg_free(ere->re_nhe);
2852 XFREE(MTYPE_WQ_WRAPPER, ere);
2853 }
2854
2855 static void process_subq_early_route_delete(struct zebra_early_route *ere)
2856 {
2857 struct route_table *table;
2858 struct route_node *rn;
2859 struct route_entry *re;
2860 struct route_entry *fib = NULL;
2861 struct route_entry *same = NULL;
2862 struct nexthop *rtnh;
2863 char buf2[INET6_ADDRSTRLEN];
2864 rib_dest_t *dest;
2865
2866 if (ere->src_p_provided)
2867 assert(!ere->src_p.prefixlen || ere->afi == AFI_IP6);
2868
2869 /* Lookup table. */
2870 table = zebra_vrf_lookup_table_with_table_id(
2871 ere->afi, ere->safi, ere->re->vrf_id, ere->re->table);
2872 if (!table) {
2873 early_route_memory_free(ere);
2874 return;
2875 }
2876
2877 /* Apply mask. */
2878 apply_mask(&ere->p);
2879 if (ere->src_p_provided)
2880 apply_mask_ipv6(&ere->src_p);
2881
2882 /* Lookup route node. */
2883 rn = srcdest_rnode_lookup(table, &ere->p,
2884 ere->src_p_provided ? &ere->src_p : NULL);
2885 if (!rn) {
2886 if (IS_ZEBRA_DEBUG_RIB) {
2887 char src_buf[PREFIX_STRLEN];
2888 struct vrf *vrf = vrf_lookup_by_id(ere->re->vrf_id);
2889
2890 if (ere->src_p_provided && ere->src_p.prefixlen)
2891 prefix2str(&ere->src_p, src_buf,
2892 sizeof(src_buf));
2893 else
2894 src_buf[0] = '\0';
2895
2896 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2897 vrf->name, ere->re->table, rn,
2898 (src_buf[0] != '\0') ? " from " : "",
2899 src_buf);
2900 }
2901 early_route_memory_free(ere);
2902 return;
2903 }
2904
2905 dest = rib_dest_from_rnode(rn);
2906 fib = dest->selected_fib;
2907
2908 struct nexthop *nh = NULL;
2909
2910 if (ere->re->nhe)
2911 nh = ere->re->nhe->nhg.nexthop;
2912
2913 /* Lookup same type route. */
2914 RNODE_FOREACH_RE (rn, re) {
2915 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2916 continue;
2917
2918 if (re->type != ere->re->type)
2919 continue;
2920 if (re->instance != ere->re->instance)
2921 continue;
2922 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2923 ere->re->distance != re->distance)
2924 continue;
2925
2926 if (re->type == ZEBRA_ROUTE_KERNEL &&
2927 re->metric != ere->re->metric)
2928 continue;
2929 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = nh) &&
2930 rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2931 if (rtnh->ifindex != nh->ifindex)
2932 continue;
2933 same = re;
2934 break;
2935 }
2936
2937 /* Make sure that the route found has the same gateway. */
2938 if (ere->re->nhe_id && re->nhe_id == ere->re->nhe_id) {
2939 same = re;
2940 break;
2941 }
2942
2943 if (nh == NULL) {
2944 same = re;
2945 break;
2946 }
2947 for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) {
2948 /*
2949 * No guarantee all kernel send nh with labels
2950 * on delete.
2951 */
2952 if (nexthop_same_no_labels(rtnh, nh)) {
2953 same = re;
2954 break;
2955 }
2956 }
2957
2958 if (same)
2959 break;
2960 }
2961 /*
2962 * If same type of route can't be found and this message is from
2963 * kernel.
2964 */
2965 if (!same) {
2966 /*
2967 * In the past(HA!) we could get here because
2968 * we were receiving a route delete from the
2969 * kernel and we're not marking the proto
2970 * as coming from it's appropriate originator.
2971 * Now that we are properly noticing the fact
2972 * that the kernel has deleted our route we
2973 * are not going to get called in this path
2974 * I am going to leave this here because
2975 * this might still work this way on non-linux
2976 * platforms as well as some weird state I have
2977 * not properly thought of yet.
2978 * If we can show that this code path is
2979 * dead then we can remove it.
2980 */
2981 if (fib && CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE)) {
2982 if (IS_ZEBRA_DEBUG_RIB) {
2983 rnode_debug(
2984 rn, ere->re->vrf_id,
2985 "rn %p, re %p (%s) was deleted from kernel, adding",
2986 rn, fib, zebra_route_string(fib->type));
2987 }
2988 if (zrouter.allow_delete ||
2989 CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) {
2990 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
2991 /* Unset flags. */
2992 for (rtnh = fib->nhe->nhg.nexthop; rtnh;
2993 rtnh = rtnh->next)
2994 UNSET_FLAG(rtnh->flags,
2995 NEXTHOP_FLAG_FIB);
2996
2997 /*
2998 * This is a non FRR route
2999 * as such we should mark
3000 * it as deleted
3001 */
3002 dest->selected_fib = NULL;
3003 } else {
3004 /*
3005 * This means someone else, other than Zebra,
3006 * has deleted a Zebra router from the kernel.
3007 * We will add it back
3008 */
3009 rib_install_kernel(rn, fib, NULL);
3010 }
3011 } else {
3012 if (IS_ZEBRA_DEBUG_RIB) {
3013 if (nh)
3014 rnode_debug(
3015 rn, ere->re->vrf_id,
3016 "via %s ifindex %d type %d doesn't exist in rib",
3017 inet_ntop(afi2family(ere->afi),
3018 &nh->gate, buf2,
3019 sizeof(buf2)),
3020 nh->ifindex, ere->re->type);
3021 else
3022 rnode_debug(
3023 rn, ere->re->vrf_id,
3024 "type %d doesn't exist in rib",
3025 ere->re->type);
3026 }
3027 route_unlock_node(rn);
3028 early_route_memory_free(ere);
3029 return;
3030 }
3031 }
3032
3033 if (same) {
3034 struct nexthop *tmp_nh;
3035
3036 if (ere->fromkernel &&
3037 CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE) &&
3038 !zrouter.allow_delete) {
3039 rib_install_kernel(rn, same, NULL);
3040 route_unlock_node(rn);
3041
3042 early_route_memory_free(ere);
3043 return;
3044 }
3045
3046 /* Special handling for IPv4 or IPv6 routes sourced from
3047 * EVPN - the nexthop (and associated MAC) need to be
3048 * uninstalled if no more refs.
3049 */
3050 for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) {
3051 struct ipaddr vtep_ip;
3052
3053 if (CHECK_FLAG(tmp_nh->flags, NEXTHOP_FLAG_EVPN)) {
3054 memset(&vtep_ip, 0, sizeof(struct ipaddr));
3055 if (ere->afi == AFI_IP) {
3056 vtep_ip.ipa_type = IPADDR_V4;
3057 memcpy(&(vtep_ip.ipaddr_v4),
3058 &(tmp_nh->gate.ipv4),
3059 sizeof(struct in_addr));
3060 } else {
3061 vtep_ip.ipa_type = IPADDR_V6;
3062 memcpy(&(vtep_ip.ipaddr_v6),
3063 &(tmp_nh->gate.ipv6),
3064 sizeof(struct in6_addr));
3065 }
3066 zebra_rib_queue_evpn_route_del(
3067 re->vrf_id, &vtep_ip, &ere->p);
3068 }
3069 }
3070
3071 /* Notify dplane if system route changes */
3072 if (RIB_SYSTEM_ROUTE(re))
3073 dplane_sys_route_del(rn, same);
3074
3075 rib_delnode(rn, same);
3076 }
3077
3078 route_unlock_node(rn);
3079
3080 early_route_memory_free(ere);
3081 }
3082
3083 /*
3084 * When FRR receives a route we need to match the route up to
3085 * nexthop groups. That we also may have just received
3086 * place the data on this queue so that this work of finding
3087 * the nexthop group entries for the route entry is always
3088 * done after the nexthop group has had a chance to be processed
3089 */
3090 static void process_subq_early_route(struct listnode *lnode)
3091 {
3092 struct zebra_early_route *ere = listgetdata(lnode);
3093
3094 if (ere->deletion)
3095 process_subq_early_route_delete(ere);
3096 else
3097 process_subq_early_route_add(ere);
3098 }
3099
3100 struct meta_q_gr_run {
3101 afi_t afi;
3102 vrf_id_t vrf_id;
3103 uint8_t proto;
3104 uint8_t instance;
3105 };
3106
3107 static void process_subq_gr_run(struct listnode *lnode)
3108 {
3109 struct meta_q_gr_run *gr_run = listgetdata(lnode);
3110
3111 zebra_gr_process_client(gr_run->afi, gr_run->vrf_id, gr_run->proto,
3112 gr_run->instance);
3113
3114 XFREE(MTYPE_WQ_WRAPPER, gr_run);
3115 }
3116
3117 /*
3118 * Examine the specified subqueue; process one entry and return 1 if
3119 * there is a node, return 0 otherwise.
3120 */
3121 static unsigned int process_subq(struct list *subq,
3122 enum meta_queue_indexes qindex)
3123 {
3124 struct listnode *lnode = listhead(subq);
3125
3126 if (!lnode)
3127 return 0;
3128
3129 switch (qindex) {
3130 case META_QUEUE_EVPN:
3131 process_subq_evpn(lnode);
3132 break;
3133 case META_QUEUE_NHG:
3134 process_subq_nhg(lnode);
3135 break;
3136 case META_QUEUE_EARLY_ROUTE:
3137 process_subq_early_route(lnode);
3138 break;
3139 case META_QUEUE_EARLY_LABEL:
3140 process_subq_early_label(lnode);
3141 break;
3142 case META_QUEUE_CONNECTED:
3143 case META_QUEUE_KERNEL:
3144 case META_QUEUE_STATIC:
3145 case META_QUEUE_NOTBGP:
3146 case META_QUEUE_BGP:
3147 case META_QUEUE_OTHER:
3148 process_subq_route(lnode, qindex);
3149 break;
3150 case META_QUEUE_GR_RUN:
3151 process_subq_gr_run(lnode);
3152 break;
3153 }
3154
3155 list_delete_node(subq, lnode);
3156
3157 return 1;
3158 }
3159
3160 /* Dispatch the meta queue by picking and processing the next node from
3161 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3162 * data is pointed to the meta queue structure.
3163 */
3164 static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
3165 {
3166 struct meta_queue *mq = data;
3167 unsigned i;
3168 uint32_t queue_len, queue_limit;
3169
3170 /* Ensure there's room for more dataplane updates */
3171 queue_limit = dplane_get_in_queue_limit();
3172 queue_len = dplane_get_in_queue_len();
3173 if (queue_len > queue_limit) {
3174 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3175 zlog_debug(
3176 "rib queue: dplane queue len %u, limit %u, retrying",
3177 queue_len, queue_limit);
3178
3179 /* Ensure that the meta-queue is actually enqueued */
3180 if (work_queue_empty(zrouter.ribq))
3181 work_queue_add(zrouter.ribq, zrouter.mq);
3182
3183 return WQ_QUEUE_BLOCKED;
3184 }
3185
3186 for (i = 0; i < MQ_SIZE; i++)
3187 if (process_subq(mq->subq[i], i)) {
3188 mq->size--;
3189 break;
3190 }
3191 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
3192 }
3193
3194
3195 /*
3196 * Look into the RN and queue it into the highest priority queue
3197 * at this point in time for processing.
3198 *
3199 * We will enqueue a route node only once per invocation.
3200 *
3201 * There are two possibilities here that should be kept in mind.
3202 * If the original invocation has not been pulled off for processing
3203 * yet, A subsuquent invocation can have a route entry with a better
3204 * meta queue index value and we can have a situation where
3205 * we might have the same node enqueued 2 times. Not necessarily
3206 * an optimal situation but it should be ok.
3207 *
3208 * The other possibility is that the original invocation has not
3209 * been pulled off for processing yet, A subsusquent invocation
3210 * doesn't have a route_entry with a better meta-queue and the
3211 * original metaqueue index value will win and we'll end up with
3212 * the route node enqueued once.
3213 */
3214 static int rib_meta_queue_add(struct meta_queue *mq, void *data)
3215 {
3216 struct route_node *rn = NULL;
3217 struct route_entry *re = NULL, *curr_re = NULL;
3218 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
3219
3220 rn = (struct route_node *)data;
3221
3222 RNODE_FOREACH_RE (rn, curr_re) {
3223 curr_qindex = route_info[curr_re->type].meta_q_map;
3224
3225 if (curr_qindex <= qindex) {
3226 re = curr_re;
3227 qindex = curr_qindex;
3228 }
3229 }
3230
3231 if (!re)
3232 return -1;
3233
3234 /* Invariant: at this point we always have rn->info set. */
3235 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3236 RIB_ROUTE_QUEUED(qindex))) {
3237 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3238 rnode_debug(rn, re->vrf_id,
3239 "rn %p is already queued in sub-queue %s",
3240 (void *)rn, subqueue2str(qindex));
3241 return -1;
3242 }
3243
3244 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
3245 listnode_add(mq->subq[qindex], rn);
3246 route_lock_node(rn);
3247 mq->size++;
3248
3249 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3250 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %s",
3251 (void *)rn, subqueue2str(qindex));
3252
3253 return 0;
3254 }
3255
3256 static int early_label_meta_queue_add(struct meta_queue *mq, void *data)
3257 {
3258 listnode_add(mq->subq[META_QUEUE_EARLY_LABEL], data);
3259 mq->size++;
3260 return 0;
3261 }
3262
3263 static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data)
3264 {
3265 struct nhg_ctx *ctx = NULL;
3266 uint8_t qindex = META_QUEUE_NHG;
3267 struct wq_nhg_wrapper *w;
3268
3269 ctx = (struct nhg_ctx *)data;
3270
3271 if (!ctx)
3272 return -1;
3273
3274 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
3275
3276 w->type = WQ_NHG_WRAPPER_TYPE_CTX;
3277 w->u.ctx = ctx;
3278
3279 listnode_add(mq->subq[qindex], w);
3280 mq->size++;
3281
3282 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3283 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3284 ctx->id, subqueue2str(qindex));
3285
3286 return 0;
3287 }
3288
3289 static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data)
3290 {
3291 struct nhg_hash_entry *nhe = NULL;
3292 uint8_t qindex = META_QUEUE_NHG;
3293 struct wq_nhg_wrapper *w;
3294
3295 nhe = (struct nhg_hash_entry *)data;
3296
3297 if (!nhe)
3298 return -1;
3299
3300 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
3301
3302 w->type = WQ_NHG_WRAPPER_TYPE_NHG;
3303 w->u.nhe = nhe;
3304
3305 listnode_add(mq->subq[qindex], w);
3306 mq->size++;
3307
3308 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3309 zlog_debug("NHG id=%u queued into sub-queue %s", nhe->id,
3310 subqueue2str(qindex));
3311
3312 return 0;
3313 }
3314
3315 static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data)
3316 {
3317 listnode_add(mq->subq[META_QUEUE_EVPN], data);
3318 mq->size++;
3319
3320 return 0;
3321 }
3322
3323 static int mq_add_handler(void *data,
3324 int (*mq_add_func)(struct meta_queue *mq, void *data))
3325 {
3326 if (zrouter.ribq == NULL) {
3327 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3328 "%s: work_queue does not exist!", __func__);
3329 return -1;
3330 }
3331
3332 /*
3333 * The RIB queue should normally be either empty or holding the only
3334 * work_queue_item element. In the latter case this element would
3335 * hold a pointer to the meta queue structure, which must be used to
3336 * actually queue the route nodes to process. So create the MQ
3337 * holder, if necessary, then push the work into it in any case.
3338 * This semantics was introduced after 0.99.9 release.
3339 */
3340 if (work_queue_empty(zrouter.ribq))
3341 work_queue_add(zrouter.ribq, zrouter.mq);
3342
3343 return mq_add_func(zrouter.mq, data);
3344 }
3345
3346 void mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
3347 struct prefix *prefix, uint8_t route_type,
3348 uint8_t route_instance)
3349 {
3350 struct wq_label_wrapper *w;
3351
3352 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3353
3354 w->type = WQ_LABEL_FTN_UNINSTALL;
3355 w->vrf_id = zvrf->vrf->vrf_id;
3356 w->p = *prefix;
3357 w->ltype = type;
3358 w->route_type = route_type;
3359 w->route_instance = route_instance;
3360
3361 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3362 zlog_debug("Early Label Handling for %pFX", prefix);
3363
3364 mq_add_handler(w, early_label_meta_queue_add);
3365 }
3366
3367 void mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
3368 const struct zapi_labels *zl)
3369 {
3370 struct wq_label_wrapper *w;
3371
3372 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3373 w->type = WQ_LABEL_LABELS_PROCESS;
3374 w->vrf_id = zvrf->vrf->vrf_id;
3375 w->add_p = add_p;
3376 w->zl = *zl;
3377
3378 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3379 zlog_debug("Early Label Handling: Labels Process");
3380
3381 mq_add_handler(w, early_label_meta_queue_add);
3382 }
3383
3384 /* Add route_node to work queue and schedule processing */
3385 int rib_queue_add(struct route_node *rn)
3386 {
3387 assert(rn);
3388
3389 /* Pointless to queue a route_node with no RIB entries to add or remove
3390 */
3391 if (!rnode_to_ribs(rn)) {
3392 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3393 __func__, (void *)rn, route_node_get_lock_count(rn));
3394 zlog_backtrace(LOG_DEBUG);
3395 return -1;
3396 }
3397
3398 return mq_add_handler(rn, rib_meta_queue_add);
3399 }
3400
3401 /*
3402 * Enqueue incoming nhg info from OS for processing
3403 */
3404 int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx)
3405 {
3406 assert(ctx);
3407
3408 return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add);
3409 }
3410
3411 /*
3412 * Enqueue incoming nhg from proto daemon for processing
3413 */
3414 int rib_queue_nhe_add(struct nhg_hash_entry *nhe)
3415 {
3416 if (nhe == NULL)
3417 return -1;
3418
3419 return mq_add_handler(nhe, rib_meta_queue_nhg_add);
3420 }
3421
3422 /*
3423 * Enqueue evpn route for processing
3424 */
3425 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
3426 const struct ipaddr *vtep_ip,
3427 const struct prefix *host_prefix)
3428 {
3429 struct wq_evpn_wrapper *w;
3430
3431 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3432
3433 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3434 w->add_p = true;
3435 w->vrf_id = vrf_id;
3436 w->macaddr = *rmac;
3437 w->ip = *vtep_ip;
3438 w->prefix = *host_prefix;
3439
3440 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3441 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3442 vrf_id, vtep_ip, host_prefix);
3443
3444 return mq_add_handler(w, rib_meta_queue_evpn_add);
3445 }
3446
3447 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
3448 const struct ipaddr *vtep_ip,
3449 const struct prefix *host_prefix)
3450 {
3451 struct wq_evpn_wrapper *w;
3452
3453 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3454
3455 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3456 w->add_p = false;
3457 w->vrf_id = vrf_id;
3458 w->ip = *vtep_ip;
3459 w->prefix = *host_prefix;
3460
3461 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3462 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3463 vrf_id, vtep_ip, host_prefix);
3464
3465 return mq_add_handler(w, rib_meta_queue_evpn_add);
3466 }
3467
3468 /* Enqueue EVPN remote ES for processing */
3469 int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
3470 const struct in_addr *vtep_ip,
3471 bool esr_rxed, uint8_t df_alg,
3472 uint16_t df_pref)
3473 {
3474 struct wq_evpn_wrapper *w;
3475 char buf[ESI_STR_LEN];
3476
3477 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3478
3479 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3480 w->add_p = true;
3481 w->esi = *esi;
3482 w->ip.ipa_type = IPADDR_V4;
3483 w->ip.ipaddr_v4 = *vtep_ip;
3484 w->esr_rxed = esr_rxed;
3485 w->df_alg = df_alg;
3486 w->df_pref = df_pref;
3487
3488 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3489 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3490 esi_to_str(esi, buf, sizeof(buf)));
3491
3492 return mq_add_handler(w, rib_meta_queue_evpn_add);
3493 }
3494
3495 int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
3496 const struct in_addr *vtep_ip)
3497 {
3498 struct wq_evpn_wrapper *w;
3499 char buf[ESI_STR_LEN];
3500
3501 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3502
3503 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3504 w->add_p = false;
3505 w->esi = *esi;
3506 w->ip.ipa_type = IPADDR_V4;
3507 w->ip.ipaddr_v4 = *vtep_ip;
3508
3509 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3510 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3511 esi_to_str(esi, buf, sizeof(buf));
3512 else
3513 strlcpy(buf, "-", sizeof(buf));
3514
3515 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3516 buf);
3517 }
3518
3519 return mq_add_handler(w, rib_meta_queue_evpn_add);
3520 }
3521
3522 /*
3523 * Enqueue EVPN remote macip update for processing
3524 */
3525 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
3526 const struct ipaddr *ipaddr,
3527 uint8_t flags, uint32_t seq,
3528 struct in_addr vtep_ip, const esi_t *esi)
3529 {
3530 struct wq_evpn_wrapper *w;
3531 char buf[ESI_STR_LEN];
3532
3533 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3534
3535 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3536 w->add_p = true;
3537 w->vni = vni;
3538 w->macaddr = *macaddr;
3539 w->ip = *ipaddr;
3540 w->flags = flags;
3541 w->seq = seq;
3542 w->vtep_ip = vtep_ip;
3543 w->esi = *esi;
3544
3545 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3546 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3547 esi_to_str(esi, buf, sizeof(buf));
3548 else
3549 strlcpy(buf, "-", sizeof(buf));
3550
3551 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__,
3552 macaddr, &vtep_ip, buf);
3553 }
3554
3555 return mq_add_handler(w, rib_meta_queue_evpn_add);
3556 }
3557
3558 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
3559 const struct ipaddr *ip,
3560 struct in_addr vtep_ip)
3561 {
3562 struct wq_evpn_wrapper *w;
3563
3564 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3565
3566 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3567 w->add_p = false;
3568 w->vni = vni;
3569 w->macaddr = *macaddr;
3570 w->ip = *ip;
3571 w->vtep_ip = vtep_ip;
3572
3573 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3574 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__,
3575 macaddr, &vtep_ip);
3576
3577 return mq_add_handler(w, rib_meta_queue_evpn_add);
3578 }
3579
3580 /*
3581 * Enqueue remote VTEP address for processing
3582 */
3583 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
3584 struct in_addr vtep_ip, int flood_control)
3585 {
3586 struct wq_evpn_wrapper *w;
3587
3588 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3589
3590 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3591 w->add_p = true;
3592 w->vrf_id = vrf_id;
3593 w->vni = vni;
3594 w->vtep_ip = vtep_ip;
3595 w->flags = flood_control;
3596
3597 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3598 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3599 &vtep_ip);
3600
3601 return mq_add_handler(w, rib_meta_queue_evpn_add);
3602 }
3603
3604 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
3605 struct in_addr vtep_ip)
3606 {
3607 struct wq_evpn_wrapper *w;
3608
3609 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3610
3611 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3612 w->add_p = false;
3613 w->vrf_id = vrf_id;
3614 w->vni = vni;
3615 w->vtep_ip = vtep_ip;
3616
3617 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3618 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3619 &vtep_ip);
3620
3621 return mq_add_handler(w, rib_meta_queue_evpn_add);
3622 }
3623
3624 /* Create new meta queue.
3625 A destructor function doesn't seem to be necessary here.
3626 */
3627 static struct meta_queue *meta_queue_new(void)
3628 {
3629 struct meta_queue *new;
3630 unsigned i;
3631
3632 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
3633
3634 for (i = 0; i < MQ_SIZE; i++) {
3635 new->subq[i] = list_new();
3636 assert(new->subq[i]);
3637 }
3638
3639 return new;
3640 }
3641
3642 /* Clean up the EVPN meta-queue list */
3643 static void evpn_meta_queue_free(struct meta_queue *mq, struct list *l,
3644 struct zebra_vrf *zvrf)
3645 {
3646 struct listnode *node, *nnode;
3647 struct wq_evpn_wrapper *w;
3648
3649 /* Free the node wrapper object, and the struct it wraps */
3650 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3651 if (zvrf) {
3652 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3653
3654 if (w->vrf_id != vrf_id)
3655 continue;
3656 }
3657
3658 node->data = NULL;
3659
3660 XFREE(MTYPE_WQ_WRAPPER, w);
3661
3662 list_delete_node(l, node);
3663 mq->size--;
3664 }
3665 }
3666
3667 /* Clean up the nhg meta-queue list */
3668 static void nhg_meta_queue_free(struct meta_queue *mq, struct list *l,
3669 struct zebra_vrf *zvrf)
3670 {
3671 struct wq_nhg_wrapper *w;
3672 struct listnode *node, *nnode;
3673
3674 /* Free the node wrapper object, and the struct it wraps */
3675 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3676 if (zvrf) {
3677 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3678
3679 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX &&
3680 w->u.ctx->vrf_id != vrf_id)
3681 continue;
3682 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG &&
3683 w->u.nhe->vrf_id != vrf_id)
3684 continue;
3685 }
3686 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX)
3687 nhg_ctx_free(&w->u.ctx);
3688 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG)
3689 zebra_nhg_free(w->u.nhe);
3690
3691 node->data = NULL;
3692 XFREE(MTYPE_WQ_WRAPPER, w);
3693
3694 list_delete_node(l, node);
3695 mq->size--;
3696 }
3697 }
3698
3699 static void early_label_meta_queue_free(struct meta_queue *mq, struct list *l,
3700 struct zebra_vrf *zvrf)
3701 {
3702 struct wq_label_wrapper *w;
3703 struct listnode *node, *nnode;
3704
3705 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3706 if (zvrf && zvrf->vrf->vrf_id != w->vrf_id)
3707 continue;
3708
3709 switch (w->type) {
3710 case WQ_LABEL_FTN_UNINSTALL:
3711 case WQ_LABEL_LABELS_PROCESS:
3712 break;
3713 }
3714
3715 node->data = NULL;
3716 XFREE(MTYPE_WQ_WRAPPER, w);
3717 list_delete_node(l, node);
3718 mq->size--;
3719 }
3720 }
3721
3722 static void rib_meta_queue_free(struct meta_queue *mq, struct list *l,
3723 struct zebra_vrf *zvrf)
3724 {
3725 struct route_node *rnode;
3726 struct listnode *node, *nnode;
3727
3728 for (ALL_LIST_ELEMENTS(l, node, nnode, rnode)) {
3729 rib_dest_t *dest = rib_dest_from_rnode(rnode);
3730
3731 if (dest && rib_dest_vrf(dest) != zvrf)
3732 continue;
3733
3734 route_unlock_node(rnode);
3735 node->data = NULL;
3736 list_delete_node(l, node);
3737 mq->size--;
3738 }
3739 }
3740
3741 static void early_route_meta_queue_free(struct meta_queue *mq, struct list *l,
3742 struct zebra_vrf *zvrf)
3743 {
3744 struct zebra_early_route *ere;
3745 struct listnode *node, *nnode;
3746
3747 for (ALL_LIST_ELEMENTS(l, node, nnode, ere)) {
3748 if (zvrf && ere->re->vrf_id != zvrf->vrf->vrf_id)
3749 continue;
3750
3751 early_route_memory_free(ere);
3752 node->data = NULL;
3753 list_delete_node(l, node);
3754 mq->size--;
3755 }
3756 }
3757
3758 static void rib_meta_queue_gr_run_free(struct meta_queue *mq, struct list *l,
3759 struct zebra_vrf *zvrf)
3760 {
3761 struct meta_q_gr_run *gr_run;
3762 struct listnode *node, *nnode;
3763
3764 for (ALL_LIST_ELEMENTS(l, node, nnode, gr_run)) {
3765 if (zvrf && zvrf->vrf->vrf_id != gr_run->vrf_id)
3766 continue;
3767
3768 XFREE(MTYPE_WQ_WRAPPER, gr_run);
3769 }
3770 }
3771
3772 void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf)
3773 {
3774 enum meta_queue_indexes i;
3775
3776 for (i = 0; i < MQ_SIZE; i++) {
3777 /* Some subqueues may need cleanup - nhgs for example */
3778 switch (i) {
3779 case META_QUEUE_NHG:
3780 nhg_meta_queue_free(mq, mq->subq[i], zvrf);
3781 break;
3782 case META_QUEUE_EVPN:
3783 evpn_meta_queue_free(mq, mq->subq[i], zvrf);
3784 break;
3785 case META_QUEUE_EARLY_ROUTE:
3786 early_route_meta_queue_free(mq, mq->subq[i], zvrf);
3787 break;
3788 case META_QUEUE_EARLY_LABEL:
3789 early_label_meta_queue_free(mq, mq->subq[i], zvrf);
3790 break;
3791 case META_QUEUE_CONNECTED:
3792 case META_QUEUE_KERNEL:
3793 case META_QUEUE_STATIC:
3794 case META_QUEUE_NOTBGP:
3795 case META_QUEUE_BGP:
3796 case META_QUEUE_OTHER:
3797 rib_meta_queue_free(mq, mq->subq[i], zvrf);
3798 break;
3799 case META_QUEUE_GR_RUN:
3800 rib_meta_queue_gr_run_free(mq, mq->subq[i], zvrf);
3801 break;
3802 }
3803 if (!zvrf)
3804 list_delete(&mq->subq[i]);
3805 }
3806
3807 if (!zvrf)
3808 XFREE(MTYPE_WORK_QUEUE, mq);
3809 }
3810
3811 /* initialise zebra rib work queue */
3812 static void rib_queue_init(void)
3813 {
3814 if (!(zrouter.ribq = work_queue_new(zrouter.master,
3815 "route_node processing"))) {
3816 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3817 "%s: could not initialise work queue!", __func__);
3818 return;
3819 }
3820
3821 /* fill in the work queue spec */
3822 zrouter.ribq->spec.workfunc = &meta_queue_process;
3823 zrouter.ribq->spec.completion_func = NULL;
3824 /* XXX: TODO: These should be runtime configurable via vty */
3825 zrouter.ribq->spec.max_retries = 3;
3826 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
3827 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
3828
3829 if (!(zrouter.mq = meta_queue_new())) {
3830 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3831 "%s: could not initialise meta queue!", __func__);
3832 return;
3833 }
3834 return;
3835 }
3836
3837 rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
3838 {
3839 rib_dest_t *dest;
3840
3841 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
3842 rnh_list_init(&dest->nht);
3843 re_list_init(&dest->routes);
3844 route_lock_node(rn); /* rn route table reference */
3845 rn->info = dest;
3846 dest->rnode = rn;
3847
3848 return dest;
3849 }
3850
3851 /* RIB updates are processed via a queue of pointers to route_nodes.
3852 *
3853 * The queue length is bounded by the maximal size of the routing table,
3854 * as a route_node will not be requeued, if already queued.
3855 *
3856 * REs are submitted via rib_addnode or rib_delnode which set minimal
3857 * state, or static_install_route (when an existing RE is updated)
3858 * and then submit route_node to queue for best-path selection later.
3859 * Order of add/delete state changes are preserved for any given RE.
3860 *
3861 * Deleted REs are reaped during best-path selection.
3862 *
3863 * rib_addnode
3864 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3865 * |-------->| | best RE, if required
3866 * | |
3867 * static_install->|->rib_addqueue...... -> rib_process
3868 * | |
3869 * |-------->| |-> rib_unlink
3870 * |-> set ROUTE_ENTRY_REMOVE |
3871 * rib_delnode (RE freed)
3872 *
3873 * The 'info' pointer of a route_node points to a rib_dest_t
3874 * ('dest'). Queueing state for a route_node is kept on the dest. The
3875 * dest is created on-demand by rib_link() and is kept around at least
3876 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3877 *
3878 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3879 *
3880 * - route_nodes: refcounted by:
3881 * - dest attached to route_node:
3882 * - managed by: rib_link/rib_gc_dest
3883 * - route_node processing queue
3884 * - managed by: rib_addqueue, rib_process.
3885 *
3886 */
3887
3888 /* Add RE to head of the route node. */
3889 static void rib_link(struct route_node *rn, struct route_entry *re, int process)
3890 {
3891 rib_dest_t *dest;
3892 afi_t afi;
3893 const char *rmap_name;
3894
3895 assert(re && rn);
3896
3897 dest = rib_dest_from_rnode(rn);
3898 if (!dest) {
3899 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3900 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
3901
3902 dest = zebra_rib_create_dest(rn);
3903 }
3904
3905 re_list_add_head(&dest->routes, re);
3906
3907 afi = (rn->p.family == AF_INET)
3908 ? AFI_IP
3909 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
3910 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3911 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3912
3913 rmap_name = zebra_get_import_table_route_map(afi, re->table);
3914 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
3915 }
3916
3917 if (process)
3918 rib_queue_add(rn);
3919 }
3920
3921 static void rib_addnode(struct route_node *rn,
3922 struct route_entry *re, int process)
3923 {
3924 /* RE node has been un-removed before route-node is processed.
3925 * route_node must hence already be on the queue for processing..
3926 */
3927 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
3928 if (IS_ZEBRA_DEBUG_RIB)
3929 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
3930 (void *)rn, (void *)re);
3931
3932 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3933 return;
3934 }
3935 rib_link(rn, re, process);
3936 }
3937
3938 /*
3939 * rib_unlink
3940 *
3941 * Detach a rib structure from a route_node.
3942 *
3943 * Note that a call to rib_unlink() should be followed by a call to
3944 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3945 * longer required to be deleted.
3946 */
3947 void rib_unlink(struct route_node *rn, struct route_entry *re)
3948 {
3949 rib_dest_t *dest;
3950
3951 assert(rn && re);
3952
3953 if (IS_ZEBRA_DEBUG_RIB)
3954 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
3955 (void *)re);
3956
3957 dest = rib_dest_from_rnode(rn);
3958
3959 re_list_del(&dest->routes, re);
3960
3961 if (dest->selected_fib == re)
3962 dest->selected_fib = NULL;
3963
3964 rib_re_nhg_free(re);
3965
3966 zapi_re_opaque_free(re->opaque);
3967
3968 XFREE(MTYPE_RE, re);
3969 }
3970
3971 void rib_delnode(struct route_node *rn, struct route_entry *re)
3972 {
3973 afi_t afi;
3974
3975 if (IS_ZEBRA_DEBUG_RIB)
3976 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
3977 (void *)rn, (void *)re);
3978 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3979
3980 afi = (rn->p.family == AF_INET)
3981 ? AFI_IP
3982 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
3983 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3984 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3985
3986 zebra_del_import_table_entry(zvrf, rn, re);
3987 /* Just clean up if non main table */
3988 if (IS_ZEBRA_DEBUG_RIB)
3989 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3990 vrf_id_to_name(re->vrf_id), re->vrf_id, rn,
3991 rn, re, zebra_route_string(re->type));
3992 }
3993
3994 rib_queue_add(rn);
3995 }
3996
3997 /*
3998 * Helper that debugs a single nexthop within a route-entry
3999 */
4000 static void _route_entry_dump_nh(const struct route_entry *re,
4001 const char *straddr,
4002 const struct nexthop *nexthop)
4003 {
4004 char nhname[PREFIX_STRLEN];
4005 char backup_str[50];
4006 char wgt_str[50];
4007 char temp_str[10];
4008 char label_str[MPLS_LABEL_STRLEN];
4009 int i;
4010 struct interface *ifp;
4011 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
4012
4013 switch (nexthop->type) {
4014 case NEXTHOP_TYPE_BLACKHOLE:
4015 snprintf(nhname, sizeof(nhname), "Blackhole");
4016 break;
4017 case NEXTHOP_TYPE_IFINDEX:
4018 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
4019 snprintf(nhname, sizeof(nhname), "%s",
4020 ifp ? ifp->name : "Unknown");
4021 break;
4022 case NEXTHOP_TYPE_IPV4:
4023 /* fallthrough */
4024 case NEXTHOP_TYPE_IPV4_IFINDEX:
4025 inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
4026 break;
4027 case NEXTHOP_TYPE_IPV6:
4028 case NEXTHOP_TYPE_IPV6_IFINDEX:
4029 inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
4030 break;
4031 }
4032
4033 /* Label stack */
4034 label_str[0] = '\0';
4035 if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) {
4036 mpls_label2str(nexthop->nh_label->num_labels,
4037 nexthop->nh_label->label, label_str,
4038 sizeof(label_str), nexthop->nh_label_type,
4039 0 /*pretty*/);
4040 strlcat(label_str, ", ", sizeof(label_str));
4041 }
4042
4043 backup_str[0] = '\0';
4044 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
4045 snprintf(backup_str, sizeof(backup_str), "backup ");
4046 for (i = 0; i < nexthop->backup_num; i++) {
4047 snprintf(temp_str, sizeof(temp_str), "%d, ",
4048 nexthop->backup_idx[i]);
4049 strlcat(backup_str, temp_str, sizeof(backup_str));
4050 }
4051 }
4052
4053 wgt_str[0] = '\0';
4054 if (nexthop->weight)
4055 snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight);
4056
4057 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
4058 straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
4059 nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown",
4060 nexthop->vrf_id,
4061 wgt_str, backup_str,
4062 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
4063 ? "ACTIVE "
4064 : ""),
4065 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
4066 ? "FIB "
4067 : ""),
4068 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
4069 ? "RECURSIVE "
4070 : ""),
4071 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
4072 ? "ONLINK "
4073 : ""),
4074 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
4075 ? "DUPLICATE "
4076 : ""),
4077 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED)
4078 ? "FILTERED " : ""),
4079 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)
4080 ? "BACKUP " : ""),
4081 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE)
4082 ? "SRTE " : ""),
4083 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN)
4084 ? "EVPN " : ""));
4085
4086 }
4087
4088 /* This function dumps the contents of a given RE entry into
4089 * standard debug log. Calling function name and IP prefix in
4090 * question are passed as 1st and 2nd arguments.
4091 */
4092 void _route_entry_dump(const char *func, union prefixconstptr pp,
4093 union prefixconstptr src_pp,
4094 const struct route_entry *re)
4095 {
4096 const struct prefix *src_p = src_pp.p;
4097 bool is_srcdst = src_p && src_p->prefixlen;
4098 char straddr[PREFIX_STRLEN];
4099 char srcaddr[PREFIX_STRLEN];
4100 char flags_buf[128];
4101 char status_buf[128];
4102 struct nexthop *nexthop;
4103 struct vrf *vrf = vrf_lookup_by_id(re->vrf_id);
4104 struct nexthop_group *nhg;
4105
4106 prefix2str(pp, straddr, sizeof(straddr));
4107
4108 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func,
4109 (const void *)re, straddr,
4110 is_srcdst ? " from " : "",
4111 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
4112 : "",
4113 VRF_LOGNAME(vrf), re->vrf_id);
4114 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4115 straddr, (unsigned long)re->uptime, re->type, re->instance,
4116 re->table);
4117 zlog_debug(
4118 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4119 straddr, re->metric, re->mtu, re->distance,
4120 zclient_dump_route_flags(re->flags, flags_buf,
4121 sizeof(flags_buf)),
4122 _dump_re_status(re, status_buf, sizeof(status_buf)));
4123 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
4124 nexthop_group_nexthop_num(&(re->nhe->nhg)),
4125 nexthop_group_active_nexthop_num(&(re->nhe->nhg)));
4126
4127 /* Dump nexthops */
4128 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4129 _route_entry_dump_nh(re, straddr, nexthop);
4130
4131 if (zebra_nhg_get_backup_nhg(re->nhe)) {
4132 zlog_debug("%s: backup nexthops:", straddr);
4133
4134 nhg = zebra_nhg_get_backup_nhg(re->nhe);
4135 for (ALL_NEXTHOPS_PTR(nhg, nexthop))
4136 _route_entry_dump_nh(re, straddr, nexthop);
4137 }
4138
4139 zlog_debug("%s: dump complete", straddr);
4140 }
4141
4142 static int rib_meta_queue_gr_run_add(struct meta_queue *mq, void *data)
4143 {
4144 listnode_add(mq->subq[META_QUEUE_GR_RUN], data);
4145 mq->size++;
4146
4147 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
4148 zlog_debug("Graceful Run adding");
4149
4150 return 0;
4151 }
4152
4153 static int rib_meta_queue_early_route_add(struct meta_queue *mq, void *data)
4154 {
4155 struct zebra_early_route *ere = data;
4156
4157 listnode_add(mq->subq[META_QUEUE_EARLY_ROUTE], data);
4158 mq->size++;
4159
4160 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
4161 zlog_debug(
4162 "Route %pFX(%u) queued for processing into sub-queue %s",
4163 &ere->p, ere->re->vrf_id,
4164 subqueue2str(META_QUEUE_EARLY_ROUTE));
4165
4166 return 0;
4167 }
4168
4169 int rib_add_gr_run(afi_t afi, vrf_id_t vrf_id, uint8_t proto, uint8_t instance)
4170 {
4171 struct meta_q_gr_run *gr_run;
4172
4173 gr_run = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*gr_run));
4174
4175 gr_run->afi = afi;
4176 gr_run->proto = proto;
4177 gr_run->vrf_id = vrf_id;
4178 gr_run->instance = instance;
4179
4180 return mq_add_handler(gr_run, rib_meta_queue_gr_run_add);
4181 }
4182
4183 struct route_entry *zebra_rib_route_entry_new(vrf_id_t vrf_id, int type,
4184 uint8_t instance, uint32_t flags,
4185 uint32_t nhe_id,
4186 uint32_t table_id,
4187 uint32_t metric, uint32_t mtu,
4188 uint8_t distance, route_tag_t tag)
4189 {
4190 struct route_entry *re;
4191
4192 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
4193 re->type = type;
4194 re->instance = instance;
4195 re->distance = distance;
4196 re->flags = flags;
4197 re->metric = metric;
4198 re->mtu = mtu;
4199 re->table = table_id;
4200 re->vrf_id = vrf_id;
4201 re->uptime = monotime(NULL);
4202 re->tag = tag;
4203 re->nhe_id = nhe_id;
4204
4205 return re;
4206 }
4207 /*
4208 * Internal route-add implementation; there are a couple of different public
4209 * signatures. Callers in this path are responsible for the memory they
4210 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4211 * must free those objects. If this returns < 0, an error has occurred and the
4212 * route_entry 're' has not been captured; the caller should free that also.
4213 *
4214 * -1 -> error
4215 * 0 -> Add
4216 * 1 -> update
4217 */
4218 int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
4219 struct prefix_ipv6 *src_p, struct route_entry *re,
4220 struct nhg_hash_entry *re_nhe, bool startup)
4221 {
4222 struct zebra_early_route *ere;
4223
4224 if (!re)
4225 return -1;
4226
4227 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
4228
4229 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4230 ere->afi = afi;
4231 ere->safi = safi;
4232 ere->p = *p;
4233 if (src_p)
4234 ere->src_p = *src_p;
4235 ere->src_p_provided = !!src_p;
4236 ere->re = re;
4237 ere->re_nhe = re_nhe;
4238 ere->startup = startup;
4239
4240 return mq_add_handler(ere, rib_meta_queue_early_route_add);
4241 }
4242
4243 /*
4244 * Add a single route.
4245 */
4246 int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
4247 struct prefix_ipv6 *src_p, struct route_entry *re,
4248 struct nexthop_group *ng, bool startup)
4249 {
4250 int ret;
4251 struct nhg_hash_entry nhe, *n;
4252
4253 if (!re)
4254 return -1;
4255
4256 /* We either need nexthop(s) or an existing nexthop id */
4257 if (ng == NULL && re->nhe_id == 0)
4258 return -1;
4259
4260 /*
4261 * Use a temporary nhe to convey info to the common/main api.
4262 */
4263 zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL));
4264 if (ng)
4265 nhe.nhg.nexthop = ng->nexthop;
4266 else if (re->nhe_id > 0)
4267 nhe.id = re->nhe_id;
4268
4269 n = zebra_nhe_copy(&nhe, 0);
4270 ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, n, startup);
4271
4272 /* In error cases, free the route also */
4273 if (ret < 0)
4274 XFREE(MTYPE_RE, re);
4275
4276 return ret;
4277 }
4278
4279 void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
4280 unsigned short instance, uint32_t flags, struct prefix *p,
4281 struct prefix_ipv6 *src_p, const struct nexthop *nh,
4282 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
4283 uint8_t distance, bool fromkernel)
4284 {
4285 struct zebra_early_route *ere;
4286 struct route_entry *re = NULL;
4287 struct nhg_hash_entry *nhe = NULL;
4288
4289 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4290 table_id, metric, 0, distance, 0);
4291
4292 if (nh) {
4293 nhe = zebra_nhg_alloc();
4294 nhe->nhg.nexthop = nexthop_dup(nh, NULL);
4295 }
4296
4297 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4298 ere->afi = afi;
4299 ere->safi = safi;
4300 ere->p = *p;
4301 if (src_p)
4302 ere->src_p = *src_p;
4303 ere->src_p_provided = !!src_p;
4304 ere->re = re;
4305 ere->re_nhe = nhe;
4306 ere->startup = false;
4307 ere->deletion = true;
4308 ere->fromkernel = fromkernel;
4309
4310 mq_add_handler(ere, rib_meta_queue_early_route_add);
4311 }
4312
4313
4314 int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
4315 unsigned short instance, uint32_t flags, struct prefix *p,
4316 struct prefix_ipv6 *src_p, const struct nexthop *nh,
4317 uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu,
4318 uint8_t distance, route_tag_t tag, bool startup)
4319 {
4320 struct route_entry *re = NULL;
4321 struct nexthop nexthop = {};
4322 struct nexthop_group ng = {};
4323
4324 /* Allocate new route_entry structure. */
4325 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4326 table_id, metric, mtu, distance, tag);
4327
4328 /* If the owner of the route supplies a shared nexthop-group id,
4329 * we'll use that. Otherwise, pass the nexthop along directly.
4330 */
4331 if (!nhe_id) {
4332 /* Add nexthop. */
4333 nexthop = *nh;
4334 nexthop_group_add_sorted(&ng, &nexthop);
4335 }
4336
4337 return rib_add_multipath(afi, safi, p, src_p, re, &ng, startup);
4338 }
4339
4340 static const char *rib_update_event2str(enum rib_update_event event)
4341 {
4342 const char *ret = "UNKNOWN";
4343
4344 switch (event) {
4345 case RIB_UPDATE_KERNEL:
4346 ret = "RIB_UPDATE_KERNEL";
4347 break;
4348 case RIB_UPDATE_RMAP_CHANGE:
4349 ret = "RIB_UPDATE_RMAP_CHANGE";
4350 break;
4351 case RIB_UPDATE_OTHER:
4352 ret = "RIB_UPDATE_OTHER";
4353 break;
4354 case RIB_UPDATE_MAX:
4355 break;
4356 }
4357
4358 return ret;
4359 }
4360
4361
4362 /* Schedule route nodes to be processed if they match the type */
4363 static void rib_update_route_node(struct route_node *rn, int type)
4364 {
4365 struct route_entry *re, *next;
4366 bool re_changed = false;
4367
4368 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4369 if (type == ZEBRA_ROUTE_ALL || type == re->type) {
4370 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
4371 re_changed = true;
4372 }
4373 }
4374
4375 if (re_changed)
4376 rib_queue_add(rn);
4377 }
4378
4379 /* Schedule routes of a particular table (address-family) based on event. */
4380 void rib_update_table(struct route_table *table, enum rib_update_event event,
4381 int rtype)
4382 {
4383 struct route_node *rn;
4384
4385 if (IS_ZEBRA_DEBUG_EVENT) {
4386 struct zebra_vrf *zvrf;
4387 struct vrf *vrf;
4388
4389 zvrf = table->info
4390 ? ((struct rib_table_info *)table->info)->zvrf
4391 : NULL;
4392 vrf = zvrf ? zvrf->vrf : NULL;
4393
4394 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__,
4395 table->info ? afi2str(
4396 ((struct rib_table_info *)table->info)->afi)
4397 : "Unknown",
4398 VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0,
4399 rib_update_event2str(event), zebra_route_string(rtype));
4400 }
4401
4402 /* Walk all routes and queue for processing, if appropriate for
4403 * the trigger event.
4404 */
4405 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4406 /*
4407 * If we are looking at a route node and the node
4408 * has already been queued we don't
4409 * need to queue it up again
4410 */
4411 if (rn->info
4412 && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
4413 RIB_ROUTE_ANY_QUEUED))
4414 continue;
4415
4416 switch (event) {
4417 case RIB_UPDATE_KERNEL:
4418 rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL);
4419 break;
4420 case RIB_UPDATE_RMAP_CHANGE:
4421 case RIB_UPDATE_OTHER:
4422 rib_update_route_node(rn, rtype);
4423 break;
4424 case RIB_UPDATE_MAX:
4425 break;
4426 }
4427 }
4428 }
4429
4430 static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype)
4431 {
4432 struct zebra_router_table *zrt;
4433
4434 if (IS_ZEBRA_DEBUG_EVENT)
4435 zlog_debug("%s: Handling VRF (ALL) event %s", __func__,
4436 rib_update_event2str(event));
4437
4438 /* Just iterate over all the route tables, rather than vrf lookups */
4439 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables)
4440 rib_update_table(zrt->table, event, rtype);
4441 }
4442
4443 struct rib_update_ctx {
4444 enum rib_update_event event;
4445 vrf_id_t vrf_id;
4446 };
4447
4448 static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id,
4449 enum rib_update_event event)
4450 {
4451 struct rib_update_ctx *ctx;
4452
4453 ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx));
4454
4455 ctx->event = event;
4456 ctx->vrf_id = vrf_id;
4457
4458 return ctx;
4459 }
4460
4461 static void rib_update_ctx_fini(struct rib_update_ctx **ctx)
4462 {
4463 XFREE(MTYPE_RIB_UPDATE_CTX, *ctx);
4464 }
4465
4466 static void rib_update_handler(struct event *thread)
4467 {
4468 struct rib_update_ctx *ctx;
4469
4470 ctx = EVENT_ARG(thread);
4471
4472 rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL);
4473
4474 rib_update_ctx_fini(&ctx);
4475 }
4476
4477 /*
4478 * Thread list to ensure we don't schedule a ton of events
4479 * if interfaces are flapping for instance.
4480 */
4481 static struct event *t_rib_update_threads[RIB_UPDATE_MAX];
4482
4483 void rib_update_finish(void)
4484 {
4485 int i;
4486
4487 for (i = RIB_UPDATE_KERNEL; i < RIB_UPDATE_MAX; i++) {
4488 if (event_is_scheduled(t_rib_update_threads[i])) {
4489 struct rib_update_ctx *ctx;
4490
4491 ctx = EVENT_ARG(t_rib_update_threads[i]);
4492
4493 rib_update_ctx_fini(&ctx);
4494 EVENT_OFF(t_rib_update_threads[i]);
4495 }
4496 }
4497 }
4498
4499 /* Schedule a RIB update event for all vrfs */
4500 void rib_update(enum rib_update_event event)
4501 {
4502 struct rib_update_ctx *ctx;
4503
4504 if (event_is_scheduled(t_rib_update_threads[event]))
4505 return;
4506
4507 if (zebra_router_in_shutdown())
4508 return;
4509
4510 ctx = rib_update_ctx_init(0, event);
4511
4512 event_add_event(zrouter.master, rib_update_handler, ctx, 0,
4513 &t_rib_update_threads[event]);
4514
4515 if (IS_ZEBRA_DEBUG_EVENT)
4516 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__,
4517 rib_update_event2str(event));
4518 }
4519
4520 /* Delete self installed routes after zebra is relaunched. */
4521 void rib_sweep_table(struct route_table *table)
4522 {
4523 struct route_node *rn;
4524 struct route_entry *re;
4525 struct route_entry *next;
4526 struct nexthop *nexthop;
4527
4528 if (!table)
4529 return;
4530
4531 if (IS_ZEBRA_DEBUG_RIB)
4532 zlog_debug("%s: starting", __func__);
4533
4534 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4535 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4536
4537 if (IS_ZEBRA_DEBUG_RIB)
4538 route_entry_dump(&rn->p, NULL, re);
4539
4540 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4541 continue;
4542
4543 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
4544 continue;
4545
4546 /*
4547 * If routes are older than startup_time then
4548 * we know we read them in from the kernel.
4549 * As such we can safely remove them.
4550 */
4551 if (zrouter.startup_time < re->uptime)
4552 continue;
4553
4554 /*
4555 * So we are starting up and have received
4556 * routes from the kernel that we have installed
4557 * from a previous run of zebra but not cleaned
4558 * up ( say a kill -9 )
4559 * But since we haven't actually installed
4560 * them yet( we received them from the kernel )
4561 * we don't think they are active.
4562 * So let's pretend they are active to actually
4563 * remove them.
4564 * In all honesty I'm not sure if we should
4565 * mark them as active when we receive them
4566 * This is startup only so probably ok.
4567 *
4568 * If we ever decide to move rib_sweep_table
4569 * to a different spot (ie startup )
4570 * this decision needs to be revisited
4571 */
4572 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
4573 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4574 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
4575
4576 rib_uninstall_kernel(rn, re);
4577 rib_delnode(rn, re);
4578 }
4579 }
4580
4581 if (IS_ZEBRA_DEBUG_RIB)
4582 zlog_debug("%s: ends", __func__);
4583 }
4584
4585 /* Sweep all RIB tables. */
4586 void rib_sweep_route(struct event *t)
4587 {
4588 struct vrf *vrf;
4589 struct zebra_vrf *zvrf;
4590
4591 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4592 if ((zvrf = vrf->info) == NULL)
4593 continue;
4594
4595 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
4596 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
4597 }
4598
4599 zebra_router_sweep_route();
4600 zebra_router_sweep_nhgs();
4601 }
4602
4603 /* Remove specific by protocol routes from 'table'. */
4604 unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
4605 struct route_table *table)
4606 {
4607 struct route_node *rn;
4608 struct route_entry *re;
4609 struct route_entry *next;
4610 unsigned long n = 0;
4611
4612 if (table)
4613 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
4614 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4615 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4616 continue;
4617 if (re->type == proto
4618 && re->instance == instance) {
4619 rib_delnode(rn, re);
4620 n++;
4621 }
4622 }
4623 return n;
4624 }
4625
4626 /* Remove specific by protocol routes. */
4627 unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
4628 {
4629 struct vrf *vrf;
4630 struct zebra_vrf *zvrf;
4631 struct other_route_table *ort;
4632 unsigned long cnt = 0;
4633
4634 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4635 zvrf = vrf->info;
4636 if (!zvrf)
4637 continue;
4638
4639 cnt += rib_score_proto_table(proto, instance,
4640 zvrf->table[AFI_IP][SAFI_UNICAST])
4641 + rib_score_proto_table(
4642 proto, instance,
4643 zvrf->table[AFI_IP6][SAFI_UNICAST]);
4644
4645 frr_each(otable, &zvrf->other_tables, ort) cnt +=
4646 rib_score_proto_table(proto, instance, ort->table);
4647 }
4648
4649 return cnt;
4650 }
4651
4652 /* Close RIB and clean up kernel routes. */
4653 void rib_close_table(struct route_table *table)
4654 {
4655 struct route_node *rn;
4656 rib_dest_t *dest;
4657
4658 if (!table)
4659 return;
4660
4661 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4662 dest = rib_dest_from_rnode(rn);
4663
4664 if (dest && dest->selected_fib) {
4665 rib_uninstall_kernel(rn, dest->selected_fib);
4666 dest->selected_fib = NULL;
4667 }
4668 }
4669 }
4670
4671 /*
4672 * Handler for async dataplane results after a pseudowire installation
4673 */
4674 static void handle_pw_result(struct zebra_dplane_ctx *ctx)
4675 {
4676 struct zebra_pw *pw;
4677 struct zebra_vrf *vrf;
4678
4679 /* The pseudowire code assumes success - we act on an error
4680 * result for installation attempts here.
4681 */
4682 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
4683 return;
4684
4685 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
4686 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
4687 pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx));
4688 if (pw)
4689 zebra_pw_install_failure(pw,
4690 dplane_ctx_get_pw_status(ctx));
4691 }
4692 }
4693
4694 /*
4695 * Handle results from the dataplane system. Dequeue update context
4696 * structs, dispatch to appropriate internal handlers.
4697 */
4698 static void rib_process_dplane_results(struct event *thread)
4699 {
4700 struct zebra_dplane_ctx *ctx;
4701 struct dplane_ctx_list_head ctxlist;
4702 bool shut_p = false;
4703
4704 /* Dequeue a list of completed updates with one lock/unlock cycle */
4705
4706 do {
4707 dplane_ctx_q_init(&ctxlist);
4708
4709 /* Take lock controlling queue of results */
4710 frr_with_mutex (&dplane_mutex) {
4711 /* Dequeue list of context structs */
4712 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
4713 }
4714
4715 /* Dequeue context block */
4716 ctx = dplane_ctx_dequeue(&ctxlist);
4717
4718 /* If we've emptied the results queue, we're done */
4719 if (ctx == NULL)
4720 break;
4721
4722 /* If zebra is shutting down, avoid processing results,
4723 * just drain the results queue.
4724 */
4725 shut_p = atomic_load_explicit(&zrouter.in_shutdown,
4726 memory_order_relaxed);
4727 if (shut_p) {
4728 while (ctx) {
4729 dplane_ctx_fini(&ctx);
4730
4731 ctx = dplane_ctx_dequeue(&ctxlist);
4732 }
4733
4734 continue;
4735 }
4736
4737 #ifdef HAVE_SCRIPTING
4738 char *script_name = frrscript_names_get_script_name(
4739 ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
4740
4741 int ret = 1;
4742 struct frrscript *fs;
4743
4744 if (script_name) {
4745 fs = frrscript_new(script_name);
4746 if (fs)
4747 ret = frrscript_load(
4748 fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4749 NULL);
4750 }
4751 #endif /* HAVE_SCRIPTING */
4752
4753 while (ctx) {
4754
4755 #ifdef HAVE_SCRIPTING
4756 if (ret == 0)
4757 frrscript_call(fs,
4758 ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4759 ("ctx", ctx));
4760 #endif /* HAVE_SCRIPTING */
4761
4762 switch (dplane_ctx_get_op(ctx)) {
4763 case DPLANE_OP_ROUTE_INSTALL:
4764 case DPLANE_OP_ROUTE_UPDATE:
4765 case DPLANE_OP_ROUTE_DELETE:
4766 /* Bit of special case for route updates
4767 * that were generated by async notifications:
4768 * we don't want to continue processing these
4769 * in the rib.
4770 */
4771 if (dplane_ctx_get_notif_provider(ctx) == 0)
4772 rib_process_result(ctx);
4773 break;
4774
4775 case DPLANE_OP_ROUTE_NOTIFY:
4776 rib_process_dplane_notify(ctx);
4777 break;
4778
4779 case DPLANE_OP_NH_INSTALL:
4780 case DPLANE_OP_NH_UPDATE:
4781 case DPLANE_OP_NH_DELETE:
4782 zebra_nhg_dplane_result(ctx);
4783 break;
4784
4785 case DPLANE_OP_LSP_INSTALL:
4786 case DPLANE_OP_LSP_UPDATE:
4787 case DPLANE_OP_LSP_DELETE:
4788 /* Bit of special case for LSP updates
4789 * that were generated by async notifications:
4790 * we don't want to continue processing these.
4791 */
4792 if (dplane_ctx_get_notif_provider(ctx) == 0)
4793 zebra_mpls_lsp_dplane_result(ctx);
4794 break;
4795
4796 case DPLANE_OP_LSP_NOTIFY:
4797 zebra_mpls_process_dplane_notify(ctx);
4798 break;
4799
4800 case DPLANE_OP_PW_INSTALL:
4801 case DPLANE_OP_PW_UNINSTALL:
4802 handle_pw_result(ctx);
4803 break;
4804
4805 case DPLANE_OP_SYS_ROUTE_ADD:
4806 case DPLANE_OP_SYS_ROUTE_DELETE:
4807 break;
4808
4809 case DPLANE_OP_MAC_INSTALL:
4810 case DPLANE_OP_MAC_DELETE:
4811 zebra_vxlan_handle_result(ctx);
4812 break;
4813
4814 case DPLANE_OP_RULE_ADD:
4815 case DPLANE_OP_RULE_DELETE:
4816 case DPLANE_OP_RULE_UPDATE:
4817 case DPLANE_OP_IPTABLE_ADD:
4818 case DPLANE_OP_IPTABLE_DELETE:
4819 case DPLANE_OP_IPSET_ADD:
4820 case DPLANE_OP_IPSET_DELETE:
4821 case DPLANE_OP_IPSET_ENTRY_ADD:
4822 case DPLANE_OP_IPSET_ENTRY_DELETE:
4823 zebra_pbr_dplane_result(ctx);
4824 break;
4825
4826 case DPLANE_OP_INTF_ADDR_ADD:
4827 case DPLANE_OP_INTF_ADDR_DEL:
4828 case DPLANE_OP_INTF_INSTALL:
4829 case DPLANE_OP_INTF_UPDATE:
4830 case DPLANE_OP_INTF_DELETE:
4831 case DPLANE_OP_INTF_NETCONFIG:
4832 zebra_if_dplane_result(ctx);
4833 break;
4834
4835 case DPLANE_OP_TC_QDISC_INSTALL:
4836 case DPLANE_OP_TC_QDISC_UNINSTALL:
4837 case DPLANE_OP_TC_CLASS_ADD:
4838 case DPLANE_OP_TC_CLASS_DELETE:
4839 case DPLANE_OP_TC_CLASS_UPDATE:
4840 case DPLANE_OP_TC_FILTER_ADD:
4841 case DPLANE_OP_TC_FILTER_DELETE:
4842 case DPLANE_OP_TC_FILTER_UPDATE:
4843 break;
4844
4845 /* Some op codes not handled here */
4846 case DPLANE_OP_ADDR_INSTALL:
4847 case DPLANE_OP_ADDR_UNINSTALL:
4848 case DPLANE_OP_NEIGH_INSTALL:
4849 case DPLANE_OP_NEIGH_UPDATE:
4850 case DPLANE_OP_NEIGH_DELETE:
4851 case DPLANE_OP_NEIGH_IP_INSTALL:
4852 case DPLANE_OP_NEIGH_IP_DELETE:
4853 case DPLANE_OP_VTEP_ADD:
4854 case DPLANE_OP_VTEP_DELETE:
4855 case DPLANE_OP_NEIGH_DISCOVER:
4856 case DPLANE_OP_BR_PORT_UPDATE:
4857 case DPLANE_OP_NEIGH_TABLE_UPDATE:
4858 case DPLANE_OP_GRE_SET:
4859 case DPLANE_OP_NONE:
4860 break;
4861
4862 } /* Dispatch by op code */
4863
4864 dplane_ctx_fini(&ctx);
4865 ctx = dplane_ctx_dequeue(&ctxlist);
4866 }
4867
4868 } while (1);
4869 }
4870
4871 /*
4872 * Results are returned from the dataplane subsystem, in the context of
4873 * the dataplane pthread. We enqueue the results here for processing by
4874 * the main thread later.
4875 */
4876 static int rib_dplane_results(struct dplane_ctx_list_head *ctxlist)
4877 {
4878 /* Take lock controlling queue of results */
4879 frr_with_mutex (&dplane_mutex) {
4880 /* Enqueue context blocks */
4881 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
4882 }
4883
4884 /* Ensure event is signalled to zebra main pthread */
4885 event_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
4886 &t_dplane);
4887
4888 return 0;
4889 }
4890
4891 /*
4892 * Ensure there are no empty slots in the route_info array.
4893 * Every route type in zebra should be present there.
4894 */
4895 static void check_route_info(void)
4896 {
4897 int len = array_size(route_info);
4898
4899 /*
4900 * ZEBRA_ROUTE_SYSTEM is special cased since
4901 * its key is 0 anyway.
4902 *
4903 * ZEBRA_ROUTE_ALL is also ignored.
4904 */
4905 for (int i = 0; i < len; i++) {
4906 assert(route_info[i].key >= ZEBRA_ROUTE_SYSTEM &&
4907 route_info[i].key < ZEBRA_ROUTE_MAX);
4908 assert(route_info[i].meta_q_map < MQ_SIZE);
4909 }
4910 }
4911
4912 /* Routing information base initialize. */
4913 void rib_init(void)
4914 {
4915 check_route_info();
4916
4917 rib_queue_init();
4918
4919 /* Init dataplane, and register for results */
4920 pthread_mutex_init(&dplane_mutex, NULL);
4921 dplane_ctx_q_init(&rib_dplane_q);
4922 zebra_dplane_init(rib_dplane_results);
4923 }
4924
4925 /*
4926 * vrf_id_get_next
4927 *
4928 * Get the first vrf id that is greater than the given vrf id if any.
4929 *
4930 * Returns true if a vrf id was found, false otherwise.
4931 */
4932 static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
4933 {
4934 struct vrf *vrf;
4935
4936 vrf = vrf_lookup_by_id(vrf_id);
4937 if (vrf) {
4938 vrf = RB_NEXT(vrf_id_head, vrf);
4939 if (vrf) {
4940 *next_id_p = vrf->vrf_id;
4941 return 1;
4942 }
4943 }
4944
4945 return 0;
4946 }
4947
4948 /*
4949 * rib_tables_iter_next
4950 *
4951 * Returns the next table in the iteration.
4952 */
4953 struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
4954 {
4955 struct route_table *table;
4956
4957 /*
4958 * Array that helps us go over all AFI/SAFI combinations via one
4959 * index.
4960 */
4961 static const struct {
4962 afi_t afi;
4963 safi_t safi;
4964 } afi_safis[] = {
4965 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
4966 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
4967 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
4968 };
4969
4970 table = NULL;
4971
4972 switch (iter->state) {
4973
4974 case RIB_TABLES_ITER_S_INIT:
4975 iter->vrf_id = VRF_DEFAULT;
4976 iter->afi_safi_ix = -1;
4977
4978 /* Fall through */
4979
4980 case RIB_TABLES_ITER_S_ITERATING:
4981 iter->afi_safi_ix++;
4982 while (1) {
4983
4984 while (iter->afi_safi_ix
4985 < (int)array_size(afi_safis)) {
4986 table = zebra_vrf_table(
4987 afi_safis[iter->afi_safi_ix].afi,
4988 afi_safis[iter->afi_safi_ix].safi,
4989 iter->vrf_id);
4990 if (table)
4991 break;
4992
4993 iter->afi_safi_ix++;
4994 }
4995
4996 /*
4997 * Found another table in this vrf.
4998 */
4999 if (table)
5000 break;
5001
5002 /*
5003 * Done with all tables in the current vrf, go to the
5004 * next
5005 * one.
5006 */
5007 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
5008 break;
5009
5010 iter->afi_safi_ix = 0;
5011 }
5012
5013 break;
5014
5015 case RIB_TABLES_ITER_S_DONE:
5016 return NULL;
5017 }
5018
5019 if (table)
5020 iter->state = RIB_TABLES_ITER_S_ITERATING;
5021 else
5022 iter->state = RIB_TABLES_ITER_S_DONE;
5023
5024 return table;
5025 }