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