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