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