]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
Merge pull request #12634 from anlancs/fix/lib-seq-adjust-return-value
[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 continue;
1310 } else
1311 SET_FLAG(re->status,
1312 ROUTE_ENTRY_REMOVED);
1313 }
1314
1315 info = srcdest_rnode_table_info(rn);
1316 srcdest_rnode_prefixes(rn, &p, NULL);
1317 zsend_route_notify_owner(
1318 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
1319 info->afi, info->safi);
1320 continue;
1321 }
1322 } else {
1323 /*
1324 * If the re has not changed and the nhg we have is
1325 * not usable, then we cannot use this route entry
1326 * for consideration, as that the route will just
1327 * not install if it is selected.
1328 */
1329 if (!nexthop_group_active_nexthop_num(&re->nhe->nhg))
1330 continue;
1331 }
1332
1333 /* Infinite distance. */
1334 if (re->distance == DISTANCE_INFINITY &&
1335 re->type != ZEBRA_ROUTE_KERNEL) {
1336 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1337 continue;
1338 }
1339
1340 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1341 best = rib_choose_best(new_fib, re);
1342 if (new_fib && best != new_fib)
1343 UNSET_FLAG(new_fib->status,
1344 ROUTE_ENTRY_CHANGED);
1345 new_fib = best;
1346 } else {
1347 best = rib_choose_best(new_selected, re);
1348 if (new_selected && best != new_selected)
1349 UNSET_FLAG(new_selected->status,
1350 ROUTE_ENTRY_CHANGED);
1351 new_selected = best;
1352 }
1353 if (best != re)
1354 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1355 } /* RNODE_FOREACH_RE */
1356
1357 /* If no FIB override route, use the selected route also for FIB */
1358 if (new_fib == NULL)
1359 new_fib = new_selected;
1360
1361 /* After the cycle is finished, the following pointers will be set:
1362 * old_selected --- RE entry currently having SELECTED
1363 * new_selected --- RE entry that is newly SELECTED
1364 * old_fib --- RE entry currently in kernel FIB
1365 * new_fib --- RE entry that is newly to be in kernel FIB
1366 *
1367 * new_selected will get SELECTED flag, and is going to be redistributed
1368 * the zclients. new_fib (which can be new_selected) will be installed
1369 * in kernel.
1370 */
1371
1372 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1373 struct route_entry *entry;
1374
1375 entry = old_selected
1376 ? old_selected
1377 : new_selected
1378 ? new_selected
1379 : old_fib ? old_fib
1380 : new_fib ? new_fib : NULL;
1381
1382 zlog_debug(
1383 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1384 VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn,
1385 (void *)old_selected, (void *)new_selected,
1386 (void *)old_fib, (void *)new_fib);
1387 }
1388
1389 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1390 * fib == selected */
1391 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1392 ROUTE_ENTRY_CHANGED);
1393
1394 /* Update SELECTED entry */
1395 if (old_selected != new_selected || selected_changed) {
1396
1397 if (new_selected && new_selected != new_fib)
1398 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
1399
1400 if (new_selected)
1401 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1402
1403 if (old_selected) {
1404 /*
1405 * If we're removing the old entry, we should tell
1406 * redist subscribers about that *if* they aren't
1407 * going to see a redist for the new entry.
1408 */
1409 if (!new_selected || CHECK_FLAG(old_selected->status,
1410 ROUTE_ENTRY_REMOVED))
1411 redistribute_delete(rn, old_selected,
1412 new_selected);
1413
1414 if (old_selected != new_selected)
1415 UNSET_FLAG(old_selected->flags,
1416 ZEBRA_FLAG_SELECTED);
1417 }
1418 }
1419
1420 /* Update fib according to selection results */
1421 if (new_fib && old_fib)
1422 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1423 else if (new_fib)
1424 rib_process_add_fib(zvrf, rn, new_fib);
1425 else if (old_fib)
1426 rib_process_del_fib(zvrf, rn, old_fib);
1427
1428 /* Remove all RE entries queued for removal */
1429 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1430 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1431 if (IS_ZEBRA_DEBUG_RIB) {
1432 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1433 (void *)rn, (void *)re);
1434 }
1435 rib_unlink(rn, re);
1436 }
1437 }
1438
1439 /*
1440 * Check if the dest can be deleted now.
1441 */
1442 rib_gc_dest(rn);
1443 }
1444
1445 static void zebra_rib_evaluate_mpls(struct route_node *rn)
1446 {
1447 rib_dest_t *dest = rib_dest_from_rnode(rn);
1448 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
1449
1450 if (!dest)
1451 return;
1452
1453 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1454 if (IS_ZEBRA_DEBUG_MPLS)
1455 zlog_debug(
1456 "%s(%u): Scheduling all LSPs upon RIB completion",
1457 zvrf_name(zvrf), zvrf_id(zvrf));
1458 zebra_mpls_lsp_schedule(zvrf);
1459 mpls_unmark_lsps_for_processing(rn);
1460 }
1461 }
1462
1463 /*
1464 * Utility to match route with dplane context data
1465 */
1466 static bool rib_route_match_ctx(const struct route_entry *re,
1467 const struct zebra_dplane_ctx *ctx,
1468 bool is_update)
1469 {
1470 bool result = false;
1471
1472 if (is_update) {
1473 /*
1474 * In 'update' case, we test info about the 'previous' or
1475 * 'old' route
1476 */
1477 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1478 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1479 result = true;
1480
1481 /* We use an extra test for statics, and another for
1482 * kernel routes.
1483 */
1484 if (re->type == ZEBRA_ROUTE_STATIC &&
1485 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1486 re->tag != dplane_ctx_get_old_tag(ctx))) {
1487 result = false;
1488 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1489 re->metric !=
1490 dplane_ctx_get_old_metric(ctx)) {
1491 result = false;
1492 }
1493 }
1494
1495 } else {
1496 /*
1497 * Ordinary, single-route case using primary context info
1498 */
1499 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1500 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1501 /* Skip route that's been deleted */
1502 goto done;
1503 }
1504
1505 if ((re->type == dplane_ctx_get_type(ctx)) &&
1506 (re->instance == dplane_ctx_get_instance(ctx))) {
1507 result = true;
1508
1509 /* We use an extra test for statics, and another for
1510 * kernel routes.
1511 */
1512 if (re->type == ZEBRA_ROUTE_STATIC &&
1513 (re->distance != dplane_ctx_get_distance(ctx) ||
1514 re->tag != dplane_ctx_get_tag(ctx))) {
1515 result = false;
1516 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1517 re->metric != dplane_ctx_get_metric(ctx)) {
1518 result = false;
1519 } else if (re->type == ZEBRA_ROUTE_CONNECT) {
1520 result = nexthop_group_equal_no_recurse(
1521 &re->nhe->nhg, dplane_ctx_get_ng(ctx));
1522 }
1523 }
1524 }
1525
1526 done:
1527 return result;
1528 }
1529
1530 static void zebra_rib_fixup_system(struct route_node *rn)
1531 {
1532 struct route_entry *re;
1533
1534 RNODE_FOREACH_RE(rn, re) {
1535 struct nexthop *nhop;
1536
1537 if (!RIB_SYSTEM_ROUTE(re))
1538 continue;
1539
1540 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1541 continue;
1542
1543 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1544 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1545 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
1546
1547 for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) {
1548 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1549 continue;
1550
1551 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1552 }
1553 }
1554 }
1555
1556 /* Route comparison logic, with various special cases. */
1557 static bool rib_compare_routes(const struct route_entry *re1,
1558 const struct route_entry *re2)
1559 {
1560 if (re1->type != re2->type)
1561 return false;
1562
1563 if (re1->instance != re2->instance)
1564 return false;
1565
1566 if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric)
1567 return false;
1568
1569 if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
1570 re1->distance != re2->distance)
1571 return false;
1572
1573 /* We support multiple connected routes: this supports multiple
1574 * v6 link-locals, and we also support multiple addresses in the same
1575 * subnet on a single interface.
1576 */
1577 if (re1->type != ZEBRA_ROUTE_CONNECT)
1578 return true;
1579
1580 return false;
1581 }
1582
1583 /*
1584 * Compare nexthop lists from a route and a dplane context; test whether
1585 * the list installed in the FIB matches the route's list.
1586 * Set 'changed_p' to 'true' if there were changes to the route's
1587 * installed nexthops.
1588 *
1589 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1590 * list.
1591 */
1592 static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg,
1593 const struct nexthop_group *ctx_nhg,
1594 bool *changed_p)
1595 {
1596 bool matched_p = true;
1597 struct nexthop *nexthop, *ctx_nexthop;
1598
1599 /* Get the first `installed` one to check against.
1600 * If the dataplane doesn't set these to be what was actually installed,
1601 * it will just be whatever was in re->nhe->nhg?
1602 */
1603 ctx_nexthop = ctx_nhg->nexthop;
1604
1605 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
1606 || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1607 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1608
1609 for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) {
1610
1611 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1612 continue;
1613
1614 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1615 continue;
1616
1617 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1618 if (!nexthop_same(ctx_nexthop, nexthop)) {
1619 /* If the FIB doesn't know about the nexthop,
1620 * it's not installed
1621 */
1622 if (IS_ZEBRA_DEBUG_RIB_DETAILED ||
1623 IS_ZEBRA_DEBUG_NHG_DETAIL) {
1624 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1625 __func__, nexthop,
1626 (CHECK_FLAG(nexthop->flags,
1627 NEXTHOP_FLAG_FIB) ?
1628 "(FIB)":""));
1629 }
1630 matched_p = false;
1631
1632 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1633 *changed_p = true;
1634
1635 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1636
1637 /* Keep checking nexthops */
1638 continue;
1639 }
1640
1641 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
1642 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1643 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1644 zlog_debug("%s: rib nh %pNHv -> installed",
1645 __func__, nexthop);
1646
1647 *changed_p = true;
1648 }
1649
1650 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1651 } else {
1652 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1653 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1654 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1655 __func__, nexthop);
1656
1657 *changed_p = true;
1658 }
1659
1660 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1661 }
1662
1663 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1664 }
1665
1666 return matched_p;
1667 }
1668
1669 /*
1670 * Update a route from a dplane context. This consolidates common code
1671 * that can be used in processing of results from FIB updates, and in
1672 * async notification processing.
1673 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1674 */
1675 static bool rib_update_re_from_ctx(struct route_entry *re,
1676 struct route_node *rn,
1677 struct zebra_dplane_ctx *ctx)
1678 {
1679 struct nexthop *nexthop;
1680 bool matched;
1681 const struct nexthop_group *ctxnhg;
1682 struct nexthop_group *re_nhg;
1683 bool is_selected = false; /* Is 're' currently the selected re? */
1684 bool changed_p = false; /* Change to nexthops? */
1685 rib_dest_t *dest;
1686 struct vrf *vrf;
1687
1688 vrf = vrf_lookup_by_id(re->vrf_id);
1689
1690 dest = rib_dest_from_rnode(rn);
1691 if (dest)
1692 is_selected = (re == dest->selected_fib);
1693
1694 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1695 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1696 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1697 (is_selected ? "" : "NOT "), re);
1698
1699 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1700 * If the installed set differs from the set requested by the rib/owner,
1701 * we use the fib-specific nexthop-group to record the actual FIB
1702 * status.
1703 */
1704 matched = false;
1705 ctxnhg = dplane_ctx_get_ng(ctx);
1706
1707 /* Check route's fib group and incoming notif group for equivalence.
1708 *
1709 * Let's assume the nexthops are ordered here to save time.
1710 */
1711 /* TODO -- this isn't testing or comparing the FIB flags; we should
1712 * do a more explicit loop, checking the incoming notification's flags.
1713 */
1714 if (re->fib_ng.nexthop && ctxnhg->nexthop &&
1715 nexthop_group_equal(&re->fib_ng, ctxnhg))
1716 matched = true;
1717
1718 /* If the new FIB set matches the existing FIB set, we're done. */
1719 if (matched) {
1720 if (IS_ZEBRA_DEBUG_RIB)
1721 zlog_debug(
1722 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1723 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1724 goto check_backups;
1725
1726 } else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) {
1727 /*
1728 * Free stale fib list and move on to check the rib nhg.
1729 */
1730 if (IS_ZEBRA_DEBUG_RIB)
1731 zlog_debug(
1732 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1733 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1734 nexthops_free(re->fib_ng.nexthop);
1735 re->fib_ng.nexthop = NULL;
1736
1737 UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1738
1739 /* Note that the installed nexthops have changed */
1740 changed_p = true;
1741 } else {
1742 if (IS_ZEBRA_DEBUG_RIB)
1743 zlog_debug(
1744 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1745 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1746 }
1747
1748 /*
1749 * Compare with the rib nexthop group. The comparison here is different:
1750 * the RIB group may be a superset of the list installed in the FIB. We
1751 * walk the RIB group, looking for the 'installable' candidate
1752 * nexthops, and then check those against the set
1753 * that is actually installed.
1754 *
1755 * Assume nexthops are ordered here as well.
1756 */
1757
1758 /* If nothing is installed, we can skip some of the checking/comparison
1759 * of nexthops.
1760 */
1761 if (ctxnhg->nexthop == NULL) {
1762 changed_p = true;
1763 goto no_nexthops;
1764 }
1765
1766 matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p);
1767
1768 /* If all nexthops were processed, we're done */
1769 if (matched) {
1770 if (IS_ZEBRA_DEBUG_RIB)
1771 zlog_debug(
1772 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1773 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1774 (changed_p ? "true" : "false"));
1775 goto check_backups;
1776 }
1777
1778 no_nexthops:
1779
1780 /* FIB nexthop set differs from the RIB set:
1781 * create a fib-specific nexthop-group
1782 */
1783 if (IS_ZEBRA_DEBUG_RIB)
1784 zlog_debug(
1785 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1786 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1787 (changed_p ? "true" : "false"),
1788 ctxnhg->nexthop != NULL ? "" : " (empty)");
1789
1790 /* Set the flag about the dedicated fib list */
1791 SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1792 if (ctxnhg->nexthop)
1793 copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, NULL);
1794
1795 check_backups:
1796
1797 /*
1798 * Check the status of the route's backup nexthops, if any.
1799 * The logic for backups is somewhat different: if any backup is
1800 * installed, a new fib nhg will be attached to the route.
1801 */
1802 re_nhg = zebra_nhg_get_backup_nhg(re->nhe);
1803 if (re_nhg == NULL)
1804 goto done; /* No backup nexthops */
1805
1806 /* First check the route's 'fib' list of backups, if it's present
1807 * from some previous event.
1808 */
1809 re_nhg = &re->fib_backup_ng;
1810 ctxnhg = dplane_ctx_get_backup_ng(ctx);
1811
1812 matched = false;
1813 if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg))
1814 matched = true;
1815
1816 /* If the new FIB set matches an existing FIB set, we're done. */
1817 if (matched) {
1818 if (IS_ZEBRA_DEBUG_RIB)
1819 zlog_debug(
1820 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1821 VRF_LOGNAME(vrf), re->vrf_id, rn);
1822 goto done;
1823
1824 } else if (re->fib_backup_ng.nexthop) {
1825 /*
1826 * Free stale fib backup list and move on to check
1827 * the route's backups.
1828 */
1829 if (IS_ZEBRA_DEBUG_RIB)
1830 zlog_debug(
1831 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1832 VRF_LOGNAME(vrf), re->vrf_id, rn);
1833 nexthops_free(re->fib_backup_ng.nexthop);
1834 re->fib_backup_ng.nexthop = NULL;
1835
1836 /* Note that the installed nexthops have changed */
1837 changed_p = true;
1838 } else {
1839 if (IS_ZEBRA_DEBUG_RIB)
1840 zlog_debug(
1841 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1842 VRF_LOGNAME(vrf), re->vrf_id, rn);
1843 }
1844
1845 /*
1846 * If a FIB backup nexthop set exists, attach a copy
1847 * to the route if any backup is installed
1848 */
1849 if (ctxnhg && ctxnhg->nexthop) {
1850
1851 for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) {
1852 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1853 break;
1854 }
1855
1856 /* If no installed backups, we're done */
1857 if (nexthop == NULL)
1858 goto done;
1859
1860 if (IS_ZEBRA_DEBUG_RIB)
1861 zlog_debug(
1862 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1863 VRF_LOGNAME(vrf), re->vrf_id, rn,
1864 (changed_p ? "true" : "false"));
1865
1866 copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop,
1867 NULL);
1868 }
1869
1870 done:
1871
1872 return changed_p;
1873 }
1874
1875 /*
1876 * Helper to locate a zebra route-node from a dplane context. This is used
1877 * when processing dplane results, e.g. Note well: the route-node is returned
1878 * with a ref held - route_unlock_node() must be called eventually.
1879 */
1880 struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
1881 {
1882 struct route_table *table = NULL;
1883 struct route_node *rn = NULL;
1884 const struct prefix *dest_pfx, *src_pfx;
1885
1886 /* Locate rn and re(s) from ctx */
1887
1888 table = zebra_vrf_lookup_table_with_table_id(
1889 dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx),
1890 dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx));
1891 if (table == NULL) {
1892 if (IS_ZEBRA_DEBUG_DPLANE) {
1893 zlog_debug(
1894 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1895 dplane_ctx_get_afi(ctx),
1896 dplane_ctx_get_safi(ctx),
1897 vrf_id_to_name(dplane_ctx_get_vrf(ctx)),
1898 dplane_ctx_get_vrf(ctx));
1899 }
1900 goto done;
1901 }
1902
1903 dest_pfx = dplane_ctx_get_dest(ctx);
1904 src_pfx = dplane_ctx_get_src(ctx);
1905
1906 rn = srcdest_rnode_get(table, dest_pfx,
1907 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1908
1909 done:
1910 return rn;
1911 }
1912
1913
1914
1915 /*
1916 * Route-update results processing after async dataplane update.
1917 */
1918 static void rib_process_result(struct zebra_dplane_ctx *ctx)
1919 {
1920 struct zebra_vrf *zvrf = NULL;
1921 struct vrf *vrf;
1922 struct route_node *rn = NULL;
1923 struct route_entry *re = NULL, *old_re = NULL, *rib;
1924 bool is_update = false;
1925 enum dplane_op_e op;
1926 enum zebra_dplane_result status;
1927 uint32_t seq;
1928 rib_dest_t *dest;
1929 bool fib_changed = false;
1930 struct rib_table_info *info;
1931 bool rt_delete = false;
1932
1933 zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
1934 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
1935
1936 /* Locate rn and re(s) from ctx */
1937 rn = rib_find_rn_from_ctx(ctx);
1938 if (rn == NULL) {
1939 if (IS_ZEBRA_DEBUG_DPLANE) {
1940 zlog_debug(
1941 "Failed to process dplane results: no route for %s(%u):%pRN",
1942 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn);
1943 }
1944 goto done;
1945 }
1946
1947 dest = rib_dest_from_rnode(rn);
1948 info = srcdest_rnode_table_info(rn);
1949
1950 op = dplane_ctx_get_op(ctx);
1951 status = dplane_ctx_get_status(ctx);
1952
1953 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1954 zlog_debug(
1955 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1956 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
1957 dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op),
1958 dplane_res2str(status));
1959
1960 /*
1961 * Update is a bit of a special case, where we may have both old and new
1962 * routes to post-process.
1963 */
1964 is_update = dplane_ctx_is_update(ctx);
1965
1966 /*
1967 * Take a pass through the routes, look for matches with the context
1968 * info.
1969 */
1970 RNODE_FOREACH_RE(rn, rib) {
1971
1972 if (re == NULL) {
1973 if (rib_route_match_ctx(rib, ctx, false))
1974 re = rib;
1975 }
1976
1977 /* Check for old route match */
1978 if (is_update && (old_re == NULL)) {
1979 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
1980 old_re = rib;
1981 }
1982
1983 /* Have we found the routes we need to work on? */
1984 if (re && ((!is_update || old_re)))
1985 break;
1986 }
1987
1988 seq = dplane_ctx_get_seq(ctx);
1989
1990 /*
1991 * Check sequence number(s) to detect stale results before continuing
1992 */
1993 if (re) {
1994 if (re->dplane_sequence != seq) {
1995 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1996 zlog_debug(
1997 "%s(%u):%pRN Stale dplane result for re %p",
1998 VRF_LOGNAME(vrf),
1999 dplane_ctx_get_vrf(ctx), rn, re);
2000 } else {
2001 if (!zrouter.asic_offloaded ||
2002 (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) ||
2003 CHECK_FLAG(re->flags,
2004 ZEBRA_FLAG_OFFLOAD_FAILED))) {
2005 UNSET_FLAG(re->status,
2006 ROUTE_ENTRY_ROUTE_REPLACING);
2007 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
2008 }
2009 }
2010 }
2011
2012 if (old_re) {
2013 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
2014 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2015 zlog_debug(
2016 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
2017 VRF_LOGNAME(vrf),
2018 dplane_ctx_get_vrf(ctx), old_re->table,
2019 rn, old_re);
2020 } else
2021 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
2022 }
2023
2024 switch (op) {
2025 case DPLANE_OP_ROUTE_INSTALL:
2026 case DPLANE_OP_ROUTE_UPDATE:
2027 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2028 if (re) {
2029 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2030 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2031 }
2032 /*
2033 * On an update operation from the same route type
2034 * context retrieval currently has no way to know
2035 * which was the old and which was the new.
2036 * So don't unset our flags that we just set.
2037 * We know redistribution is ok because the
2038 * old_re in this case is used for nothing
2039 * more than knowing whom to contact if necessary.
2040 */
2041 if (old_re && old_re != re) {
2042 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2043 UNSET_FLAG(old_re->status,
2044 ROUTE_ENTRY_INSTALLED);
2045 }
2046
2047 /* Update zebra route based on the results in
2048 * the context struct.
2049 */
2050 if (re) {
2051 fib_changed =
2052 rib_update_re_from_ctx(re, rn, ctx);
2053
2054 if (!fib_changed) {
2055 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2056 zlog_debug(
2057 "%s(%u:%u):%pRN no fib change for re",
2058 VRF_LOGNAME(vrf),
2059 dplane_ctx_get_vrf(ctx),
2060 dplane_ctx_get_table(
2061 ctx),
2062 rn);
2063 }
2064
2065 /* Redistribute if this is the selected re */
2066 if (dest && re == dest->selected_fib)
2067 redistribute_update(rn, re, old_re);
2068 }
2069
2070 /*
2071 * System routes are weird in that they
2072 * allow multiple to be installed that match
2073 * to the same prefix, so after we get the
2074 * result we need to clean them up so that
2075 * we can actually use them.
2076 */
2077 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2078 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2079 zebra_rib_fixup_system(rn);
2080
2081 if (zvrf)
2082 zvrf->installs++;
2083
2084 /* Notify route owner */
2085 if (zebra_router_notify_on_ack())
2086 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2087 else {
2088 if (re) {
2089 if (CHECK_FLAG(re->flags,
2090 ZEBRA_FLAG_OFFLOADED))
2091 zsend_route_notify_owner_ctx(
2092 ctx,
2093 ZAPI_ROUTE_INSTALLED);
2094 if (CHECK_FLAG(
2095 re->flags,
2096 ZEBRA_FLAG_OFFLOAD_FAILED))
2097 zsend_route_notify_owner_ctx(
2098 ctx,
2099 ZAPI_ROUTE_FAIL_INSTALL);
2100 }
2101 }
2102 } else {
2103 if (re) {
2104 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2105 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2106 } if (old_re)
2107 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2108 if (re)
2109 zsend_route_notify_owner(
2110 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
2111 info->afi, info->safi);
2112
2113 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2114 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2115 dplane_ctx_get_table(ctx), rn);
2116 }
2117 break;
2118 case DPLANE_OP_ROUTE_DELETE:
2119 rt_delete = true;
2120 if (re)
2121 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2122 /*
2123 * In the delete case, the zebra core datastructs were
2124 * updated (or removed) at the time the delete was issued,
2125 * so we're just notifying the route owner.
2126 */
2127 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2128 if (re) {
2129 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2130 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2131 }
2132 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
2133
2134 if (zvrf)
2135 zvrf->removals++;
2136 } else {
2137 if (re)
2138 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2139 zsend_route_notify_owner_ctx(ctx,
2140 ZAPI_ROUTE_REMOVE_FAIL);
2141
2142 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2143 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2144 dplane_ctx_get_table(ctx), rn);
2145 }
2146
2147 /*
2148 * System routes are weird in that they
2149 * allow multiple to be installed that match
2150 * to the same prefix, so after we get the
2151 * result we need to clean them up so that
2152 * we can actually use them.
2153 */
2154 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2155 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2156 zebra_rib_fixup_system(rn);
2157 break;
2158 default:
2159 break;
2160 }
2161
2162 zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete);
2163 zebra_rib_evaluate_mpls(rn);
2164 done:
2165
2166 if (rn)
2167 route_unlock_node(rn);
2168 }
2169
2170 /*
2171 * Count installed/FIB nexthops
2172 */
2173 static int rib_count_installed_nh(struct route_entry *re)
2174 {
2175 int count = 0;
2176 struct nexthop *nexthop;
2177 struct nexthop_group *nhg;
2178
2179 nhg = rib_get_fib_nhg(re);
2180
2181 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2182 /* The meaningful flag depends on where the installed
2183 * nexthops reside.
2184 */
2185 if (nhg == &(re->fib_ng)) {
2186 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2187 count++;
2188 } else {
2189 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2190 count++;
2191 }
2192 }
2193
2194 nhg = rib_get_fib_backup_nhg(re);
2195 if (nhg) {
2196 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2197 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2198 count++;
2199 }
2200 }
2201
2202 return count;
2203 }
2204
2205 /*
2206 * Handle notification from async dataplane: the dataplane has detected
2207 * some change to a route, and notifies zebra so that the control plane
2208 * can reflect that change.
2209 */
2210 static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2211 {
2212 struct route_node *rn = NULL;
2213 struct route_entry *re = NULL;
2214 struct vrf *vrf;
2215 struct nexthop *nexthop;
2216 rib_dest_t *dest;
2217 bool fib_changed = false;
2218 bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
2219 int start_count, end_count;
2220
2221 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
2222
2223 /* Locate rn and re(s) from ctx */
2224 rn = rib_find_rn_from_ctx(ctx);
2225 if (rn == NULL) {
2226 if (debug_p) {
2227 zlog_debug(
2228 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2229 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2230 dplane_ctx_get_table(ctx), rn);
2231 }
2232 goto done;
2233 }
2234
2235 dest = rib_dest_from_rnode(rn);
2236
2237 if (debug_p)
2238 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2239 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2240 dplane_ctx_get_table(ctx), rn, ctx);
2241
2242 /*
2243 * Take a pass through the routes, look for matches with the context
2244 * info.
2245 */
2246 RNODE_FOREACH_RE(rn, re) {
2247 if (rib_route_match_ctx(re, ctx, false /*!update*/))
2248 break;
2249 }
2250
2251 /* No match? Nothing we can do */
2252 if (re == NULL) {
2253 if (debug_p)
2254 zlog_debug(
2255 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2256 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2257 dplane_ctx_get_table(ctx), rn,
2258 zebra_route_string(dplane_ctx_get_type(ctx)));
2259
2260 goto done;
2261 }
2262
2263 /* Ensure we clear the QUEUED flag */
2264 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
2265 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
2266
2267 /* Is this a notification that ... matters? We mostly care about
2268 * the route that is currently selected for installation; we may also
2269 * get an un-install notification, and handle that too.
2270 */
2271 if (re != dest->selected_fib) {
2272 /*
2273 * If we need to, clean up after a delete that was part of
2274 * an update operation.
2275 */
2276 end_count = 0;
2277 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2278 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2279 end_count++;
2280 }
2281
2282 /* If no nexthops or none installed, ensure that this re
2283 * gets its 'installed' flag cleared.
2284 */
2285 if (end_count == 0) {
2286 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2287 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2288 if (debug_p)
2289 zlog_debug(
2290 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2291 VRF_LOGNAME(vrf),
2292 dplane_ctx_get_vrf(ctx),
2293 dplane_ctx_get_table(ctx), rn,
2294 zebra_route_string(
2295 dplane_ctx_get_type(ctx)));
2296 } else {
2297 /* At least report on the event. */
2298 if (debug_p)
2299 zlog_debug(
2300 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2301 VRF_LOGNAME(vrf),
2302 dplane_ctx_get_vrf(ctx),
2303 dplane_ctx_get_table(ctx), rn,
2304 zebra_route_string(
2305 dplane_ctx_get_type(ctx)));
2306 }
2307 goto done;
2308 } else {
2309 uint32_t flags = dplane_ctx_get_flags(ctx);
2310
2311 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOADED)) {
2312 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2313 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2314 }
2315 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOAD_FAILED)) {
2316 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2317 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2318 }
2319 if (CHECK_FLAG(flags, ZEBRA_FLAG_TRAPPED))
2320 SET_FLAG(re->flags, ZEBRA_FLAG_TRAPPED);
2321 }
2322
2323 /* We'll want to determine whether the installation status of the
2324 * route has changed: we'll check the status before processing,
2325 * and then again if there's been a change.
2326 */
2327 start_count = 0;
2328
2329 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2330 start_count = rib_count_installed_nh(re);
2331
2332 /* Update zebra's nexthop FIB flags based on the context struct's
2333 * nexthops.
2334 */
2335 fib_changed = rib_update_re_from_ctx(re, rn, ctx);
2336
2337 if (!fib_changed) {
2338 if (debug_p)
2339 zlog_debug(
2340 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2341 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2342 dplane_ctx_get_table(ctx), rn);
2343 }
2344
2345 /*
2346 * Perform follow-up work if the actual status of the prefix
2347 * changed.
2348 */
2349 end_count = rib_count_installed_nh(re);
2350
2351 /* Various fib transitions: changed nexthops; from installed to
2352 * not-installed; or not-installed to installed.
2353 */
2354 if (zrouter.asic_notification_nexthop_control) {
2355 if (start_count > 0 && end_count > 0) {
2356 if (debug_p)
2357 zlog_debug(
2358 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2359 VRF_LOGNAME(vrf),
2360 dplane_ctx_get_vrf(ctx),
2361 dplane_ctx_get_table(ctx), rn);
2362
2363 /* Changed nexthops - update kernel/others */
2364 dplane_route_notif_update(rn, re,
2365 DPLANE_OP_ROUTE_UPDATE, ctx);
2366
2367 } else if (start_count == 0 && end_count > 0) {
2368 if (debug_p)
2369 zlog_debug(
2370 "%s(%u:%u):%pRN installed transition from dplane notification",
2371 VRF_LOGNAME(vrf),
2372 dplane_ctx_get_vrf(ctx),
2373 dplane_ctx_get_table(ctx), rn);
2374
2375 /* We expect this to be the selected route, so we want
2376 * to tell others about this transition.
2377 */
2378 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2379
2380 /* Changed nexthops - update kernel/others */
2381 dplane_route_notif_update(rn, re,
2382 DPLANE_OP_ROUTE_UPDATE, ctx);
2383
2384 /* Redistribute, lsp, and nht update */
2385 redistribute_update(rn, re, NULL);
2386
2387 } else if (start_count > 0 && end_count == 0) {
2388 if (debug_p)
2389 zlog_debug(
2390 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2391 VRF_LOGNAME(vrf),
2392 dplane_ctx_get_vrf(ctx),
2393 dplane_ctx_get_table(ctx), rn);
2394
2395 /* Transition from _something_ installed to _nothing_
2396 * installed.
2397 */
2398 /* We expect this to be the selected route, so we want
2399 * to tell others about this transistion.
2400 */
2401 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2402
2403 /* Changed nexthops - update kernel/others */
2404 dplane_route_notif_update(rn, re,
2405 DPLANE_OP_ROUTE_DELETE, ctx);
2406
2407 /* Redistribute, lsp, and nht update */
2408 redistribute_delete(rn, re, NULL);
2409 }
2410 }
2411
2412 if (!zebra_router_notify_on_ack()) {
2413 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED))
2414 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2415 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED))
2416 zsend_route_notify_owner_ctx(ctx,
2417 ZAPI_ROUTE_FAIL_INSTALL);
2418 }
2419
2420 /* Make any changes visible for lsp and nexthop-tracking processing */
2421 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
2422 false);
2423
2424 zebra_rib_evaluate_mpls(rn);
2425
2426 done:
2427 if (rn)
2428 route_unlock_node(rn);
2429 }
2430
2431 /*
2432 * Process a node from the EVPN/VXLAN subqueue.
2433 */
2434 static void process_subq_evpn(struct listnode *lnode)
2435 {
2436 struct wq_evpn_wrapper *w;
2437
2438 /* In general, the list node points to a wrapper object
2439 * holding the info necessary to make some update.
2440 */
2441 w = listgetdata(lnode);
2442 if (!w)
2443 return;
2444
2445 if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) {
2446 if (w->add_p)
2447 zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr,
2448 &w->ip, &w->prefix);
2449 else
2450 zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip,
2451 &w->prefix);
2452 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) {
2453 if (w->add_p)
2454 zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4,
2455 w->esr_rxed, w->df_alg,
2456 w->df_pref);
2457 else
2458 zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4);
2459 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) {
2460 uint16_t ipa_len = 0;
2461
2462 if (w->ip.ipa_type == IPADDR_V4)
2463 ipa_len = IPV4_MAX_BYTELEN;
2464 else if (w->ip.ipa_type == IPADDR_V6)
2465 ipa_len = IPV6_MAX_BYTELEN;
2466
2467 if (w->add_p)
2468 zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len,
2469 &w->ip, w->flags, w->seq,
2470 w->vtep_ip, &w->esi);
2471 else
2472 zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len,
2473 &w->ip, w->vtep_ip);
2474 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) {
2475 if (w->add_p)
2476 zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni,
2477 w->vtep_ip, w->flags);
2478 else
2479 zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni,
2480 w->vtep_ip);
2481 }
2482
2483
2484 XFREE(MTYPE_WQ_WRAPPER, w);
2485 }
2486
2487 /*
2488 * Process the nexthop-group workqueue subqueue
2489 */
2490 static void process_subq_nhg(struct listnode *lnode)
2491 {
2492 struct nhg_ctx *ctx;
2493 struct nhg_hash_entry *nhe, *newnhe;
2494 struct wq_nhg_wrapper *w;
2495 uint8_t qindex = META_QUEUE_NHG;
2496
2497 w = listgetdata(lnode);
2498
2499 if (!w)
2500 return;
2501
2502 /* Two types of object - an update from the local kernel, or
2503 * an nhg update from a daemon.
2504 */
2505 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) {
2506 ctx = w->u.ctx;
2507
2508 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2509 zlog_debug(
2510 "NHG Context id=%u dequeued from sub-queue %s",
2511 ctx->id, subqueue2str(qindex));
2512
2513
2514 /* Process nexthop group updates coming 'up' from the OS */
2515 nhg_ctx_process(ctx);
2516
2517 } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) {
2518 nhe = w->u.nhe;
2519
2520 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2521 zlog_debug("NHG %u dequeued from sub-queue %s", nhe->id,
2522 subqueue2str(qindex));
2523
2524 /* Process incoming nhg update, probably from a proto daemon */
2525 newnhe = zebra_nhg_proto_add(nhe->id, nhe->type,
2526 nhe->zapi_instance,
2527 nhe->zapi_session, &nhe->nhg, 0);
2528
2529 /* Report error to daemon via ZAPI */
2530 if (newnhe == NULL)
2531 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2532 nhe->zapi_session, nhe->id,
2533 ZAPI_NHG_FAIL_INSTALL);
2534
2535 /* Free temp nhe - we own that memory. */
2536 zebra_nhg_free(nhe);
2537 }
2538
2539 XFREE(MTYPE_WQ_WRAPPER, w);
2540 }
2541
2542 static void process_subq_early_label(struct listnode *lnode)
2543 {
2544 struct wq_label_wrapper *w = listgetdata(lnode);
2545 struct zebra_vrf *zvrf;
2546
2547 if (!w)
2548 return;
2549
2550 zvrf = vrf_info_lookup(w->vrf_id);
2551 if (!zvrf) {
2552 XFREE(MTYPE_WQ_WRAPPER, w);
2553 return;
2554 }
2555
2556 switch (w->type) {
2557 case WQ_LABEL_FTN_UNINSTALL:
2558 zebra_mpls_ftn_uninstall(zvrf, w->ltype, &w->p, w->route_type,
2559 w->route_instance);
2560 break;
2561 case WQ_LABEL_LABELS_PROCESS:
2562 zebra_mpls_zapi_labels_process(w->add_p, zvrf, &w->zl);
2563 break;
2564 }
2565
2566 XFREE(MTYPE_WQ_WRAPPER, w);
2567 }
2568
2569 static void process_subq_route(struct listnode *lnode, uint8_t qindex)
2570 {
2571 struct route_node *rnode = NULL;
2572 rib_dest_t *dest = NULL;
2573 struct zebra_vrf *zvrf = NULL;
2574
2575 rnode = listgetdata(lnode);
2576 dest = rib_dest_from_rnode(rnode);
2577 assert(dest);
2578
2579 zvrf = rib_dest_vrf(dest);
2580
2581 rib_process(rnode);
2582
2583 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2584 struct route_entry *re = NULL;
2585
2586 /*
2587 * rib_process may have freed the dest
2588 * as part of the garbage collection. Let's
2589 * prevent stupidity from happening.
2590 */
2591 dest = rib_dest_from_rnode(rnode);
2592 if (dest)
2593 re = re_list_first(&dest->routes);
2594
2595 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2596 zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0,
2597 rnode, rnode, subqueue2str(qindex));
2598 }
2599
2600 if (rnode->info)
2601 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2602 RIB_ROUTE_QUEUED(qindex));
2603
2604 route_unlock_node(rnode);
2605 }
2606
2607 static void rib_re_nhg_free(struct route_entry *re)
2608 {
2609 if (re->nhe && re->nhe_id) {
2610 assert(re->nhe->id == re->nhe_id);
2611 route_entry_update_nhe(re, NULL);
2612 } else if (re->nhe && re->nhe->nhg.nexthop)
2613 nexthops_free(re->nhe->nhg.nexthop);
2614
2615 nexthops_free(re->fib_ng.nexthop);
2616 }
2617
2618 struct zebra_early_route {
2619 afi_t afi;
2620 safi_t safi;
2621 struct prefix p;
2622 struct prefix_ipv6 src_p;
2623 bool src_p_provided;
2624 struct route_entry *re;
2625 struct nhg_hash_entry *re_nhe;
2626 bool startup;
2627 bool deletion;
2628 bool fromkernel;
2629 };
2630
2631 static void early_route_memory_free(struct zebra_early_route *ere)
2632 {
2633 if (ere->re_nhe)
2634 zebra_nhg_free(ere->re_nhe);
2635
2636 XFREE(MTYPE_RE, ere->re);
2637 XFREE(MTYPE_WQ_WRAPPER, ere);
2638 }
2639
2640 static void process_subq_early_route_add(struct zebra_early_route *ere)
2641 {
2642 struct route_entry *re = ere->re;
2643 struct route_table *table;
2644 struct nhg_hash_entry *nhe = NULL;
2645 struct route_node *rn;
2646 struct route_entry *same = NULL, *first_same = NULL;
2647 int same_count = 0;
2648 rib_dest_t *dest;
2649
2650 /* Lookup table. */
2651 table = zebra_vrf_get_table_with_table_id(ere->afi, ere->safi,
2652 re->vrf_id, re->table);
2653 if (!table) {
2654 early_route_memory_free(ere);
2655 return;
2656 }
2657
2658 if (re->nhe_id > 0) {
2659 nhe = zebra_nhg_lookup_id(re->nhe_id);
2660
2661 if (!nhe) {
2662 /*
2663 * We've received from the kernel a nexthop id
2664 * that we don't have saved yet. More than likely
2665 * it has not been processed and is on the
2666 * queue to be processed. Let's stop what we
2667 * are doing and cause the meta q to be processed
2668 * storing this for later.
2669 *
2670 * This is being done this way because zebra
2671 * runs with the assumption t
2672 */
2673 flog_err(
2674 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2675 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2676 re->nhe_id, &ere->p);
2677
2678 early_route_memory_free(ere);
2679 return;
2680 }
2681 } else {
2682 /* Lookup nhe from route information */
2683 nhe = zebra_nhg_rib_find_nhe(ere->re_nhe, ere->afi);
2684 if (!nhe) {
2685 char buf2[PREFIX_STRLEN] = "";
2686
2687 flog_err(
2688 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2689 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2690 &ere->p, ere->src_p_provided ? " from " : "",
2691 ere->src_p_provided
2692 ? prefix2str(&ere->src_p, buf2,
2693 sizeof(buf2))
2694 : "");
2695
2696 early_route_memory_free(ere);
2697 return;
2698 }
2699 }
2700
2701 /*
2702 * Attach the re to the nhe's nexthop group.
2703 *
2704 * TODO: This will need to change when we start getting IDs from upper
2705 * level protocols, as the refcnt might be wrong, since it checks
2706 * if old_id != new_id.
2707 */
2708 route_entry_update_nhe(re, nhe);
2709
2710 /* Make it sure prefixlen is applied to the prefix. */
2711 apply_mask(&ere->p);
2712 if (ere->src_p_provided)
2713 apply_mask_ipv6(&ere->src_p);
2714
2715 /* Lookup route node.*/
2716 rn = srcdest_rnode_get(table, &ere->p,
2717 ere->src_p_provided ? &ere->src_p : NULL);
2718
2719 /*
2720 * If same type of route are installed, treat it as a implicit
2721 * withdraw. If the user has specified the No route replace semantics
2722 * for the install don't do a route replace.
2723 */
2724 RNODE_FOREACH_RE (rn, same) {
2725 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) {
2726 same_count++;
2727 continue;
2728 }
2729
2730 /* Compare various route_entry properties */
2731 if (rib_compare_routes(re, same)) {
2732 same_count++;
2733
2734 if (first_same == NULL)
2735 first_same = same;
2736 }
2737 }
2738
2739 same = first_same;
2740
2741 if (!ere->startup && (re->flags & ZEBRA_FLAG_SELFROUTE) &&
2742 zrouter.asic_offloaded) {
2743 if (!same) {
2744 if (IS_ZEBRA_DEBUG_RIB)
2745 zlog_debug(
2746 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2747 rn);
2748 /*
2749 * We are not on startup, this is a self route
2750 * and we have asic offload. Which means
2751 * we are getting a callback for a entry
2752 * that was already deleted to the kernel
2753 * but an earlier response was just handed
2754 * back. Drop it on the floor
2755 */
2756 early_route_memory_free(ere);
2757 return;
2758 }
2759 }
2760
2761 /* Set default distance by route type. */
2762 if (re->distance == 0) {
2763 if (same && !zebra_router_notify_on_ack())
2764 re->distance = same->distance;
2765 else
2766 re->distance = route_distance(re->type);
2767 }
2768
2769 if (re->metric == ROUTE_INSTALLATION_METRIC &&
2770 CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE)) {
2771 if (same && !zebra_router_notify_on_ack())
2772 re->metric = same->metric;
2773 else
2774 re->metric = 0;
2775 }
2776
2777 /* If this route is kernel/connected route, notify the dataplane. */
2778 if (RIB_SYSTEM_ROUTE(re)) {
2779 /* Notify dataplane */
2780 dplane_sys_route_add(rn, re);
2781 }
2782
2783 /* Link new re to node.*/
2784 if (IS_ZEBRA_DEBUG_RIB) {
2785 rnode_debug(
2786 rn, re->vrf_id,
2787 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2788 rn, re, zebra_route_string(re->type), same, same_count);
2789
2790 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2791 route_entry_dump(
2792 &ere->p,
2793 ere->src_p_provided ? &ere->src_p : NULL, re);
2794 }
2795
2796 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2797 rib_addnode(rn, re, 1);
2798
2799 /* Free implicit route.*/
2800 if (same)
2801 rib_delnode(rn, same);
2802
2803 /* See if we can remove some RE entries that are queued for
2804 * removal, but won't be considered in rib processing.
2805 */
2806 dest = rib_dest_from_rnode(rn);
2807 RNODE_FOREACH_RE_SAFE (rn, re, same) {
2808 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2809 /* If the route was used earlier, must retain it. */
2810 if (dest && re == dest->selected_fib)
2811 continue;
2812
2813 if (IS_ZEBRA_DEBUG_RIB)
2814 rnode_debug(rn, re->vrf_id,
2815 "rn %p, removing unneeded re %p",
2816 rn, re);
2817
2818 rib_unlink(rn, re);
2819 }
2820 }
2821
2822 route_unlock_node(rn);
2823 if (ere->re_nhe)
2824 zebra_nhg_free(ere->re_nhe);
2825 XFREE(MTYPE_WQ_WRAPPER, ere);
2826 }
2827
2828 static void process_subq_early_route_delete(struct zebra_early_route *ere)
2829 {
2830 struct route_table *table;
2831 struct route_node *rn;
2832 struct route_entry *re;
2833 struct route_entry *fib = NULL;
2834 struct route_entry *same = NULL;
2835 struct nexthop *rtnh;
2836 char buf2[INET6_ADDRSTRLEN];
2837 rib_dest_t *dest;
2838
2839 if (ere->src_p_provided)
2840 assert(!ere->src_p.prefixlen || ere->afi == AFI_IP6);
2841
2842 /* Lookup table. */
2843 table = zebra_vrf_lookup_table_with_table_id(
2844 ere->afi, ere->safi, ere->re->vrf_id, ere->re->table);
2845 if (!table) {
2846 early_route_memory_free(ere);
2847 return;
2848 }
2849
2850 /* Apply mask. */
2851 apply_mask(&ere->p);
2852 if (ere->src_p_provided)
2853 apply_mask_ipv6(&ere->src_p);
2854
2855 /* Lookup route node. */
2856 rn = srcdest_rnode_lookup(table, &ere->p,
2857 ere->src_p_provided ? &ere->src_p : NULL);
2858 if (!rn) {
2859 if (IS_ZEBRA_DEBUG_RIB) {
2860 char src_buf[PREFIX_STRLEN];
2861 struct vrf *vrf = vrf_lookup_by_id(ere->re->vrf_id);
2862
2863 if (ere->src_p_provided && ere->src_p.prefixlen)
2864 prefix2str(&ere->src_p, src_buf,
2865 sizeof(src_buf));
2866 else
2867 src_buf[0] = '\0';
2868
2869 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2870 vrf->name, ere->re->table, rn,
2871 (src_buf[0] != '\0') ? " from " : "",
2872 src_buf);
2873 }
2874 early_route_memory_free(ere);
2875 return;
2876 }
2877
2878 dest = rib_dest_from_rnode(rn);
2879 fib = dest->selected_fib;
2880
2881 struct nexthop *nh = NULL;
2882
2883 if (ere->re->nhe)
2884 nh = ere->re->nhe->nhg.nexthop;
2885
2886 /* Lookup same type route. */
2887 RNODE_FOREACH_RE (rn, re) {
2888 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2889 continue;
2890
2891 if (re->type != ere->re->type)
2892 continue;
2893 if (re->instance != ere->re->instance)
2894 continue;
2895 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2896 ere->re->distance != re->distance)
2897 continue;
2898
2899 if (re->type == ZEBRA_ROUTE_KERNEL &&
2900 re->metric != ere->re->metric)
2901 continue;
2902 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = nh) &&
2903 rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2904 if (rtnh->ifindex != nh->ifindex)
2905 continue;
2906 same = re;
2907 break;
2908 }
2909
2910 /* Make sure that the route found has the same gateway. */
2911 if (ere->re->nhe_id && re->nhe_id == ere->re->nhe_id) {
2912 same = re;
2913 break;
2914 }
2915
2916 if (nh == NULL) {
2917 same = re;
2918 break;
2919 }
2920 for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) {
2921 /*
2922 * No guarantee all kernel send nh with labels
2923 * on delete.
2924 */
2925 if (nexthop_same_no_labels(rtnh, nh)) {
2926 same = re;
2927 break;
2928 }
2929 }
2930
2931 if (same)
2932 break;
2933 }
2934 /*
2935 * If same type of route can't be found and this message is from
2936 * kernel.
2937 */
2938 if (!same) {
2939 /*
2940 * In the past(HA!) we could get here because
2941 * we were receiving a route delete from the
2942 * kernel and we're not marking the proto
2943 * as coming from it's appropriate originator.
2944 * Now that we are properly noticing the fact
2945 * that the kernel has deleted our route we
2946 * are not going to get called in this path
2947 * I am going to leave this here because
2948 * this might still work this way on non-linux
2949 * platforms as well as some weird state I have
2950 * not properly thought of yet.
2951 * If we can show that this code path is
2952 * dead then we can remove it.
2953 */
2954 if (fib && CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE)) {
2955 if (IS_ZEBRA_DEBUG_RIB) {
2956 rnode_debug(
2957 rn, ere->re->vrf_id,
2958 "rn %p, re %p (%s) was deleted from kernel, adding",
2959 rn, fib, zebra_route_string(fib->type));
2960 }
2961 if (zrouter.allow_delete ||
2962 CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) {
2963 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
2964 /* Unset flags. */
2965 for (rtnh = fib->nhe->nhg.nexthop; rtnh;
2966 rtnh = rtnh->next)
2967 UNSET_FLAG(rtnh->flags,
2968 NEXTHOP_FLAG_FIB);
2969
2970 /*
2971 * This is a non FRR route
2972 * as such we should mark
2973 * it as deleted
2974 */
2975 dest->selected_fib = NULL;
2976 } else {
2977 /*
2978 * This means someone else, other than Zebra,
2979 * has deleted a Zebra router from the kernel.
2980 * We will add it back
2981 */
2982 rib_install_kernel(rn, fib, NULL);
2983 }
2984 } else {
2985 if (IS_ZEBRA_DEBUG_RIB) {
2986 if (nh)
2987 rnode_debug(
2988 rn, ere->re->vrf_id,
2989 "via %s ifindex %d type %d doesn't exist in rib",
2990 inet_ntop(afi2family(ere->afi),
2991 &nh->gate, buf2,
2992 sizeof(buf2)),
2993 nh->ifindex, ere->re->type);
2994 else
2995 rnode_debug(
2996 rn, ere->re->vrf_id,
2997 "type %d doesn't exist in rib",
2998 ere->re->type);
2999 }
3000 route_unlock_node(rn);
3001 early_route_memory_free(ere);
3002 return;
3003 }
3004 }
3005
3006 if (same) {
3007 struct nexthop *tmp_nh;
3008
3009 if (ere->fromkernel &&
3010 CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE) &&
3011 !zrouter.allow_delete) {
3012 rib_install_kernel(rn, same, NULL);
3013 route_unlock_node(rn);
3014
3015 early_route_memory_free(ere);
3016 return;
3017 }
3018
3019 /* Special handling for IPv4 or IPv6 routes sourced from
3020 * EVPN - the nexthop (and associated MAC) need to be
3021 * uninstalled if no more refs.
3022 */
3023 for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) {
3024 struct ipaddr vtep_ip;
3025
3026 if (CHECK_FLAG(tmp_nh->flags, NEXTHOP_FLAG_EVPN)) {
3027 memset(&vtep_ip, 0, sizeof(struct ipaddr));
3028 if (ere->afi == AFI_IP) {
3029 vtep_ip.ipa_type = IPADDR_V4;
3030 memcpy(&(vtep_ip.ipaddr_v4),
3031 &(tmp_nh->gate.ipv4),
3032 sizeof(struct in_addr));
3033 } else {
3034 vtep_ip.ipa_type = IPADDR_V6;
3035 memcpy(&(vtep_ip.ipaddr_v6),
3036 &(tmp_nh->gate.ipv6),
3037 sizeof(struct in6_addr));
3038 }
3039 zebra_rib_queue_evpn_route_del(
3040 re->vrf_id, &vtep_ip, &ere->p);
3041 }
3042 }
3043
3044 /* Notify dplane if system route changes */
3045 if (RIB_SYSTEM_ROUTE(re))
3046 dplane_sys_route_del(rn, same);
3047
3048 rib_delnode(rn, same);
3049 }
3050
3051 route_unlock_node(rn);
3052
3053 early_route_memory_free(ere);
3054 }
3055
3056 /*
3057 * When FRR receives a route we need to match the route up to
3058 * nexthop groups. That we also may have just received
3059 * place the data on this queue so that this work of finding
3060 * the nexthop group entries for the route entry is always
3061 * done after the nexthop group has had a chance to be processed
3062 */
3063 static void process_subq_early_route(struct listnode *lnode)
3064 {
3065 struct zebra_early_route *ere = listgetdata(lnode);
3066
3067 if (ere->deletion)
3068 process_subq_early_route_delete(ere);
3069 else
3070 process_subq_early_route_add(ere);
3071 }
3072
3073 /*
3074 * Examine the specified subqueue; process one entry and return 1 if
3075 * there is a node, return 0 otherwise.
3076 */
3077 static unsigned int process_subq(struct list *subq,
3078 enum meta_queue_indexes qindex)
3079 {
3080 struct listnode *lnode = listhead(subq);
3081
3082 if (!lnode)
3083 return 0;
3084
3085 switch (qindex) {
3086 case META_QUEUE_EVPN:
3087 process_subq_evpn(lnode);
3088 break;
3089 case META_QUEUE_NHG:
3090 process_subq_nhg(lnode);
3091 break;
3092 case META_QUEUE_EARLY_ROUTE:
3093 process_subq_early_route(lnode);
3094 break;
3095 case META_QUEUE_EARLY_LABEL:
3096 process_subq_early_label(lnode);
3097 break;
3098 case META_QUEUE_CONNECTED:
3099 case META_QUEUE_KERNEL:
3100 case META_QUEUE_STATIC:
3101 case META_QUEUE_NOTBGP:
3102 case META_QUEUE_BGP:
3103 case META_QUEUE_OTHER:
3104 process_subq_route(lnode, qindex);
3105 break;
3106 }
3107
3108 list_delete_node(subq, lnode);
3109
3110 return 1;
3111 }
3112
3113 /* Dispatch the meta queue by picking and processing the next node from
3114 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3115 * data is pointed to the meta queue structure.
3116 */
3117 static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
3118 {
3119 struct meta_queue *mq = data;
3120 unsigned i;
3121 uint32_t queue_len, queue_limit;
3122
3123 /* Ensure there's room for more dataplane updates */
3124 queue_limit = dplane_get_in_queue_limit();
3125 queue_len = dplane_get_in_queue_len();
3126 if (queue_len > queue_limit) {
3127 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3128 zlog_debug(
3129 "rib queue: dplane queue len %u, limit %u, retrying",
3130 queue_len, queue_limit);
3131
3132 /* Ensure that the meta-queue is actually enqueued */
3133 if (work_queue_empty(zrouter.ribq))
3134 work_queue_add(zrouter.ribq, zrouter.mq);
3135
3136 return WQ_QUEUE_BLOCKED;
3137 }
3138
3139 for (i = 0; i < MQ_SIZE; i++)
3140 if (process_subq(mq->subq[i], i)) {
3141 mq->size--;
3142 break;
3143 }
3144 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
3145 }
3146
3147
3148 /*
3149 * Look into the RN and queue it into the highest priority queue
3150 * at this point in time for processing.
3151 *
3152 * We will enqueue a route node only once per invocation.
3153 *
3154 * There are two possibilities here that should be kept in mind.
3155 * If the original invocation has not been pulled off for processing
3156 * yet, A subsuquent invocation can have a route entry with a better
3157 * meta queue index value and we can have a situation where
3158 * we might have the same node enqueued 2 times. Not necessarily
3159 * an optimal situation but it should be ok.
3160 *
3161 * The other possibility is that the original invocation has not
3162 * been pulled off for processing yet, A subsusquent invocation
3163 * doesn't have a route_entry with a better meta-queue and the
3164 * original metaqueue index value will win and we'll end up with
3165 * the route node enqueued once.
3166 */
3167 static int rib_meta_queue_add(struct meta_queue *mq, void *data)
3168 {
3169 struct route_node *rn = NULL;
3170 struct route_entry *re = NULL, *curr_re = NULL;
3171 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
3172
3173 rn = (struct route_node *)data;
3174
3175 RNODE_FOREACH_RE (rn, curr_re) {
3176 curr_qindex = route_info[curr_re->type].meta_q_map;
3177
3178 if (curr_qindex <= qindex) {
3179 re = curr_re;
3180 qindex = curr_qindex;
3181 }
3182 }
3183
3184 if (!re)
3185 return -1;
3186
3187 /* Invariant: at this point we always have rn->info set. */
3188 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3189 RIB_ROUTE_QUEUED(qindex))) {
3190 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3191 rnode_debug(rn, re->vrf_id,
3192 "rn %p is already queued in sub-queue %s",
3193 (void *)rn, subqueue2str(qindex));
3194 return -1;
3195 }
3196
3197 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
3198 listnode_add(mq->subq[qindex], rn);
3199 route_lock_node(rn);
3200 mq->size++;
3201
3202 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3203 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %s",
3204 (void *)rn, subqueue2str(qindex));
3205
3206 return 0;
3207 }
3208
3209 static int early_label_meta_queue_add(struct meta_queue *mq, void *data)
3210 {
3211 listnode_add(mq->subq[META_QUEUE_EARLY_LABEL], data);
3212 mq->size++;
3213 return 0;
3214 }
3215
3216 static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data)
3217 {
3218 struct nhg_ctx *ctx = NULL;
3219 uint8_t qindex = META_QUEUE_NHG;
3220 struct wq_nhg_wrapper *w;
3221
3222 ctx = (struct nhg_ctx *)data;
3223
3224 if (!ctx)
3225 return -1;
3226
3227 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
3228
3229 w->type = WQ_NHG_WRAPPER_TYPE_CTX;
3230 w->u.ctx = ctx;
3231
3232 listnode_add(mq->subq[qindex], w);
3233 mq->size++;
3234
3235 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3236 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3237 ctx->id, subqueue2str(qindex));
3238
3239 return 0;
3240 }
3241
3242 static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data)
3243 {
3244 struct nhg_hash_entry *nhe = NULL;
3245 uint8_t qindex = META_QUEUE_NHG;
3246 struct wq_nhg_wrapper *w;
3247
3248 nhe = (struct nhg_hash_entry *)data;
3249
3250 if (!nhe)
3251 return -1;
3252
3253 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
3254
3255 w->type = WQ_NHG_WRAPPER_TYPE_NHG;
3256 w->u.nhe = nhe;
3257
3258 listnode_add(mq->subq[qindex], w);
3259 mq->size++;
3260
3261 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3262 zlog_debug("NHG id=%u queued into sub-queue %s", nhe->id,
3263 subqueue2str(qindex));
3264
3265 return 0;
3266 }
3267
3268 static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data)
3269 {
3270 listnode_add(mq->subq[META_QUEUE_EVPN], data);
3271 mq->size++;
3272
3273 return 0;
3274 }
3275
3276 static int mq_add_handler(void *data,
3277 int (*mq_add_func)(struct meta_queue *mq, void *data))
3278 {
3279 if (zrouter.ribq == NULL) {
3280 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3281 "%s: work_queue does not exist!", __func__);
3282 return -1;
3283 }
3284
3285 /*
3286 * The RIB queue should normally be either empty or holding the only
3287 * work_queue_item element. In the latter case this element would
3288 * hold a pointer to the meta queue structure, which must be used to
3289 * actually queue the route nodes to process. So create the MQ
3290 * holder, if necessary, then push the work into it in any case.
3291 * This semantics was introduced after 0.99.9 release.
3292 */
3293 if (work_queue_empty(zrouter.ribq))
3294 work_queue_add(zrouter.ribq, zrouter.mq);
3295
3296 return mq_add_func(zrouter.mq, data);
3297 }
3298
3299 void mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
3300 struct prefix *prefix, uint8_t route_type,
3301 uint8_t route_instance)
3302 {
3303 struct wq_label_wrapper *w;
3304
3305 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3306
3307 w->type = WQ_LABEL_FTN_UNINSTALL;
3308 w->vrf_id = zvrf->vrf->vrf_id;
3309 w->p = *prefix;
3310 w->ltype = type;
3311 w->route_type = route_type;
3312 w->route_instance = route_instance;
3313
3314 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3315 zlog_debug("Early Label Handling for %pFX", prefix);
3316
3317 mq_add_handler(w, early_label_meta_queue_add);
3318 }
3319
3320 void mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
3321 const struct zapi_labels *zl)
3322 {
3323 struct wq_label_wrapper *w;
3324
3325 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3326 w->type = WQ_LABEL_LABELS_PROCESS;
3327 w->vrf_id = zvrf->vrf->vrf_id;
3328 w->add_p = add_p;
3329 w->zl = *zl;
3330
3331 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3332 zlog_debug("Early Label Handling: Labels Process");
3333
3334 mq_add_handler(w, early_label_meta_queue_add);
3335 }
3336
3337 /* Add route_node to work queue and schedule processing */
3338 int rib_queue_add(struct route_node *rn)
3339 {
3340 assert(rn);
3341
3342 /* Pointless to queue a route_node with no RIB entries to add or remove
3343 */
3344 if (!rnode_to_ribs(rn)) {
3345 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3346 __func__, (void *)rn, route_node_get_lock_count(rn));
3347 zlog_backtrace(LOG_DEBUG);
3348 return -1;
3349 }
3350
3351 return mq_add_handler(rn, rib_meta_queue_add);
3352 }
3353
3354 /*
3355 * Enqueue incoming nhg info from OS for processing
3356 */
3357 int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx)
3358 {
3359 assert(ctx);
3360
3361 return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add);
3362 }
3363
3364 /*
3365 * Enqueue incoming nhg from proto daemon for processing
3366 */
3367 int rib_queue_nhe_add(struct nhg_hash_entry *nhe)
3368 {
3369 if (nhe == NULL)
3370 return -1;
3371
3372 return mq_add_handler(nhe, rib_meta_queue_nhg_add);
3373 }
3374
3375 /*
3376 * Enqueue evpn route for processing
3377 */
3378 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
3379 const struct ipaddr *vtep_ip,
3380 const struct prefix *host_prefix)
3381 {
3382 struct wq_evpn_wrapper *w;
3383
3384 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3385
3386 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3387 w->add_p = true;
3388 w->vrf_id = vrf_id;
3389 w->macaddr = *rmac;
3390 w->ip = *vtep_ip;
3391 w->prefix = *host_prefix;
3392
3393 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3394 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3395 vrf_id, vtep_ip, host_prefix);
3396
3397 return mq_add_handler(w, rib_meta_queue_evpn_add);
3398 }
3399
3400 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
3401 const struct ipaddr *vtep_ip,
3402 const struct prefix *host_prefix)
3403 {
3404 struct wq_evpn_wrapper *w;
3405
3406 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3407
3408 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3409 w->add_p = false;
3410 w->vrf_id = vrf_id;
3411 w->ip = *vtep_ip;
3412 w->prefix = *host_prefix;
3413
3414 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3415 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3416 vrf_id, vtep_ip, host_prefix);
3417
3418 return mq_add_handler(w, rib_meta_queue_evpn_add);
3419 }
3420
3421 /* Enqueue EVPN remote ES for processing */
3422 int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
3423 const struct in_addr *vtep_ip,
3424 bool esr_rxed, uint8_t df_alg,
3425 uint16_t df_pref)
3426 {
3427 struct wq_evpn_wrapper *w;
3428 char buf[ESI_STR_LEN];
3429
3430 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3431
3432 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3433 w->add_p = true;
3434 w->esi = *esi;
3435 w->ip.ipa_type = IPADDR_V4;
3436 w->ip.ipaddr_v4 = *vtep_ip;
3437 w->esr_rxed = esr_rxed;
3438 w->df_alg = df_alg;
3439 w->df_pref = df_pref;
3440
3441 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3442 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3443 esi_to_str(esi, buf, sizeof(buf)));
3444
3445 return mq_add_handler(w, rib_meta_queue_evpn_add);
3446 }
3447
3448 int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
3449 const struct in_addr *vtep_ip)
3450 {
3451 struct wq_evpn_wrapper *w;
3452 char buf[ESI_STR_LEN];
3453
3454 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3455
3456 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3457 w->add_p = false;
3458 w->esi = *esi;
3459 w->ip.ipa_type = IPADDR_V4;
3460 w->ip.ipaddr_v4 = *vtep_ip;
3461
3462 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3463 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3464 esi_to_str(esi, buf, sizeof(buf));
3465 else
3466 strlcpy(buf, "-", sizeof(buf));
3467
3468 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3469 buf);
3470 }
3471
3472 return mq_add_handler(w, rib_meta_queue_evpn_add);
3473 }
3474
3475 /*
3476 * Enqueue EVPN remote macip update for processing
3477 */
3478 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
3479 const struct ipaddr *ipaddr,
3480 uint8_t flags, uint32_t seq,
3481 struct in_addr vtep_ip, const esi_t *esi)
3482 {
3483 struct wq_evpn_wrapper *w;
3484 char buf[ESI_STR_LEN];
3485
3486 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3487
3488 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3489 w->add_p = true;
3490 w->vni = vni;
3491 w->macaddr = *macaddr;
3492 w->ip = *ipaddr;
3493 w->flags = flags;
3494 w->seq = seq;
3495 w->vtep_ip = vtep_ip;
3496 w->esi = *esi;
3497
3498 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3499 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3500 esi_to_str(esi, buf, sizeof(buf));
3501 else
3502 strlcpy(buf, "-", sizeof(buf));
3503
3504 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__,
3505 macaddr, &vtep_ip, buf);
3506 }
3507
3508 return mq_add_handler(w, rib_meta_queue_evpn_add);
3509 }
3510
3511 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
3512 const struct ipaddr *ip,
3513 struct in_addr vtep_ip)
3514 {
3515 struct wq_evpn_wrapper *w;
3516
3517 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3518
3519 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3520 w->add_p = false;
3521 w->vni = vni;
3522 w->macaddr = *macaddr;
3523 w->ip = *ip;
3524 w->vtep_ip = vtep_ip;
3525
3526 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3527 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__,
3528 macaddr, &vtep_ip);
3529
3530 return mq_add_handler(w, rib_meta_queue_evpn_add);
3531 }
3532
3533 /*
3534 * Enqueue remote VTEP address for processing
3535 */
3536 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
3537 struct in_addr vtep_ip, int flood_control)
3538 {
3539 struct wq_evpn_wrapper *w;
3540
3541 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3542
3543 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3544 w->add_p = true;
3545 w->vrf_id = vrf_id;
3546 w->vni = vni;
3547 w->vtep_ip = vtep_ip;
3548 w->flags = flood_control;
3549
3550 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3551 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3552 &vtep_ip);
3553
3554 return mq_add_handler(w, rib_meta_queue_evpn_add);
3555 }
3556
3557 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
3558 struct in_addr vtep_ip)
3559 {
3560 struct wq_evpn_wrapper *w;
3561
3562 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3563
3564 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3565 w->add_p = false;
3566 w->vrf_id = vrf_id;
3567 w->vni = vni;
3568 w->vtep_ip = vtep_ip;
3569
3570 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3571 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3572 &vtep_ip);
3573
3574 return mq_add_handler(w, rib_meta_queue_evpn_add);
3575 }
3576
3577 /* Create new meta queue.
3578 A destructor function doesn't seem to be necessary here.
3579 */
3580 static struct meta_queue *meta_queue_new(void)
3581 {
3582 struct meta_queue *new;
3583 unsigned i;
3584
3585 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
3586
3587 for (i = 0; i < MQ_SIZE; i++) {
3588 new->subq[i] = list_new();
3589 assert(new->subq[i]);
3590 }
3591
3592 return new;
3593 }
3594
3595 /* Clean up the EVPN meta-queue list */
3596 static void evpn_meta_queue_free(struct meta_queue *mq, struct list *l,
3597 struct zebra_vrf *zvrf)
3598 {
3599 struct listnode *node, *nnode;
3600 struct wq_evpn_wrapper *w;
3601
3602 /* Free the node wrapper object, and the struct it wraps */
3603 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3604 if (zvrf) {
3605 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3606
3607 if (w->vrf_id != vrf_id)
3608 continue;
3609 }
3610
3611 node->data = NULL;
3612
3613 XFREE(MTYPE_WQ_WRAPPER, w);
3614
3615 list_delete_node(l, node);
3616 mq->size--;
3617 }
3618 }
3619
3620 /* Clean up the nhg meta-queue list */
3621 static void nhg_meta_queue_free(struct meta_queue *mq, struct list *l,
3622 struct zebra_vrf *zvrf)
3623 {
3624 struct wq_nhg_wrapper *w;
3625 struct listnode *node, *nnode;
3626
3627 /* Free the node wrapper object, and the struct it wraps */
3628 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3629 if (zvrf) {
3630 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3631
3632 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX &&
3633 w->u.ctx->vrf_id != vrf_id)
3634 continue;
3635 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG &&
3636 w->u.nhe->vrf_id != vrf_id)
3637 continue;
3638 }
3639 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX)
3640 nhg_ctx_free(&w->u.ctx);
3641 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG)
3642 zebra_nhg_free(w->u.nhe);
3643
3644 node->data = NULL;
3645 XFREE(MTYPE_WQ_WRAPPER, w);
3646
3647 list_delete_node(l, node);
3648 mq->size--;
3649 }
3650 }
3651
3652 static void early_label_meta_queue_free(struct meta_queue *mq, struct list *l,
3653 struct zebra_vrf *zvrf)
3654 {
3655 struct wq_label_wrapper *w;
3656 struct listnode *node, *nnode;
3657
3658 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3659 if (zvrf && zvrf->vrf->vrf_id != w->vrf_id)
3660 continue;
3661
3662 switch (w->type) {
3663 case WQ_LABEL_FTN_UNINSTALL:
3664 case WQ_LABEL_LABELS_PROCESS:
3665 break;
3666 }
3667
3668 node->data = NULL;
3669 XFREE(MTYPE_WQ_WRAPPER, w);
3670 list_delete_node(l, node);
3671 mq->size--;
3672 }
3673 }
3674
3675 static void rib_meta_queue_free(struct meta_queue *mq, struct list *l,
3676 struct zebra_vrf *zvrf)
3677 {
3678 struct route_node *rnode;
3679 struct listnode *node, *nnode;
3680
3681 for (ALL_LIST_ELEMENTS(l, node, nnode, rnode)) {
3682 rib_dest_t *dest = rib_dest_from_rnode(rnode);
3683
3684 if (dest && rib_dest_vrf(dest) != zvrf)
3685 continue;
3686
3687 route_unlock_node(rnode);
3688 node->data = NULL;
3689 list_delete_node(l, node);
3690 mq->size--;
3691 }
3692 }
3693
3694 static void early_route_meta_queue_free(struct meta_queue *mq, struct list *l,
3695 struct zebra_vrf *zvrf)
3696 {
3697 struct zebra_early_route *ere;
3698 struct listnode *node, *nnode;
3699
3700 for (ALL_LIST_ELEMENTS(l, node, nnode, ere)) {
3701 if (zvrf && ere->re->vrf_id != zvrf->vrf->vrf_id)
3702 continue;
3703
3704 early_route_memory_free(ere);
3705 node->data = NULL;
3706 list_delete_node(l, node);
3707 mq->size--;
3708 }
3709 }
3710
3711 void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf)
3712 {
3713 enum meta_queue_indexes i;
3714
3715 for (i = 0; i < MQ_SIZE; i++) {
3716 /* Some subqueues may need cleanup - nhgs for example */
3717 switch (i) {
3718 case META_QUEUE_NHG:
3719 nhg_meta_queue_free(mq, mq->subq[i], zvrf);
3720 break;
3721 case META_QUEUE_EVPN:
3722 evpn_meta_queue_free(mq, mq->subq[i], zvrf);
3723 break;
3724 case META_QUEUE_EARLY_ROUTE:
3725 early_route_meta_queue_free(mq, mq->subq[i], zvrf);
3726 break;
3727 case META_QUEUE_EARLY_LABEL:
3728 early_label_meta_queue_free(mq, mq->subq[i], zvrf);
3729 break;
3730 case META_QUEUE_CONNECTED:
3731 case META_QUEUE_KERNEL:
3732 case META_QUEUE_STATIC:
3733 case META_QUEUE_NOTBGP:
3734 case META_QUEUE_BGP:
3735 case META_QUEUE_OTHER:
3736 rib_meta_queue_free(mq, mq->subq[i], zvrf);
3737 break;
3738 }
3739 if (!zvrf)
3740 list_delete(&mq->subq[i]);
3741 }
3742
3743 if (!zvrf)
3744 XFREE(MTYPE_WORK_QUEUE, mq);
3745 }
3746
3747 /* initialise zebra rib work queue */
3748 static void rib_queue_init(void)
3749 {
3750 if (!(zrouter.ribq = work_queue_new(zrouter.master,
3751 "route_node processing"))) {
3752 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3753 "%s: could not initialise work queue!", __func__);
3754 return;
3755 }
3756
3757 /* fill in the work queue spec */
3758 zrouter.ribq->spec.workfunc = &meta_queue_process;
3759 zrouter.ribq->spec.completion_func = NULL;
3760 /* XXX: TODO: These should be runtime configurable via vty */
3761 zrouter.ribq->spec.max_retries = 3;
3762 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
3763 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
3764
3765 if (!(zrouter.mq = meta_queue_new())) {
3766 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3767 "%s: could not initialise meta queue!", __func__);
3768 return;
3769 }
3770 return;
3771 }
3772
3773 rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
3774 {
3775 rib_dest_t *dest;
3776
3777 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
3778 rnh_list_init(&dest->nht);
3779 re_list_init(&dest->routes);
3780 route_lock_node(rn); /* rn route table reference */
3781 rn->info = dest;
3782 dest->rnode = rn;
3783
3784 return dest;
3785 }
3786
3787 /* RIB updates are processed via a queue of pointers to route_nodes.
3788 *
3789 * The queue length is bounded by the maximal size of the routing table,
3790 * as a route_node will not be requeued, if already queued.
3791 *
3792 * REs are submitted via rib_addnode or rib_delnode which set minimal
3793 * state, or static_install_route (when an existing RE is updated)
3794 * and then submit route_node to queue for best-path selection later.
3795 * Order of add/delete state changes are preserved for any given RE.
3796 *
3797 * Deleted REs are reaped during best-path selection.
3798 *
3799 * rib_addnode
3800 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3801 * |-------->| | best RE, if required
3802 * | |
3803 * static_install->|->rib_addqueue...... -> rib_process
3804 * | |
3805 * |-------->| |-> rib_unlink
3806 * |-> set ROUTE_ENTRY_REMOVE |
3807 * rib_delnode (RE freed)
3808 *
3809 * The 'info' pointer of a route_node points to a rib_dest_t
3810 * ('dest'). Queueing state for a route_node is kept on the dest. The
3811 * dest is created on-demand by rib_link() and is kept around at least
3812 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3813 *
3814 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3815 *
3816 * - route_nodes: refcounted by:
3817 * - dest attached to route_node:
3818 * - managed by: rib_link/rib_gc_dest
3819 * - route_node processing queue
3820 * - managed by: rib_addqueue, rib_process.
3821 *
3822 */
3823
3824 /* Add RE to head of the route node. */
3825 static void rib_link(struct route_node *rn, struct route_entry *re, int process)
3826 {
3827 rib_dest_t *dest;
3828 afi_t afi;
3829 const char *rmap_name;
3830
3831 assert(re && rn);
3832
3833 dest = rib_dest_from_rnode(rn);
3834 if (!dest) {
3835 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3836 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
3837
3838 dest = zebra_rib_create_dest(rn);
3839 }
3840
3841 re_list_add_head(&dest->routes, re);
3842
3843 afi = (rn->p.family == AF_INET)
3844 ? AFI_IP
3845 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
3846 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3847 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3848
3849 rmap_name = zebra_get_import_table_route_map(afi, re->table);
3850 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
3851 } else if (process)
3852 rib_queue_add(rn);
3853 }
3854
3855 static void rib_addnode(struct route_node *rn,
3856 struct route_entry *re, int process)
3857 {
3858 /* RE node has been un-removed before route-node is processed.
3859 * route_node must hence already be on the queue for processing..
3860 */
3861 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
3862 if (IS_ZEBRA_DEBUG_RIB)
3863 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
3864 (void *)rn, (void *)re);
3865
3866 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3867 return;
3868 }
3869 rib_link(rn, re, process);
3870 }
3871
3872 /*
3873 * rib_unlink
3874 *
3875 * Detach a rib structure from a route_node.
3876 *
3877 * Note that a call to rib_unlink() should be followed by a call to
3878 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3879 * longer required to be deleted.
3880 */
3881 void rib_unlink(struct route_node *rn, struct route_entry *re)
3882 {
3883 rib_dest_t *dest;
3884
3885 assert(rn && re);
3886
3887 if (IS_ZEBRA_DEBUG_RIB)
3888 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
3889 (void *)re);
3890
3891 dest = rib_dest_from_rnode(rn);
3892
3893 re_list_del(&dest->routes, re);
3894
3895 if (dest->selected_fib == re)
3896 dest->selected_fib = NULL;
3897
3898 rib_re_nhg_free(re);
3899
3900 zapi_re_opaque_free(re->opaque);
3901
3902 XFREE(MTYPE_RE, re);
3903 }
3904
3905 void rib_delnode(struct route_node *rn, struct route_entry *re)
3906 {
3907 afi_t afi;
3908
3909 if (IS_ZEBRA_DEBUG_RIB)
3910 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
3911 (void *)rn, (void *)re);
3912 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3913
3914 afi = (rn->p.family == AF_INET)
3915 ? AFI_IP
3916 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
3917 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3918 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3919
3920 zebra_del_import_table_entry(zvrf, rn, re);
3921 /* Just clean up if non main table */
3922 if (IS_ZEBRA_DEBUG_RIB)
3923 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3924 vrf_id_to_name(re->vrf_id), re->vrf_id, rn,
3925 rn, re, zebra_route_string(re->type));
3926
3927 rib_unlink(rn, re);
3928 } else {
3929 rib_queue_add(rn);
3930 }
3931 }
3932
3933 /*
3934 * Helper that debugs a single nexthop within a route-entry
3935 */
3936 static void _route_entry_dump_nh(const struct route_entry *re,
3937 const char *straddr,
3938 const struct nexthop *nexthop)
3939 {
3940 char nhname[PREFIX_STRLEN];
3941 char backup_str[50];
3942 char wgt_str[50];
3943 char temp_str[10];
3944 char label_str[MPLS_LABEL_STRLEN];
3945 int i;
3946 struct interface *ifp;
3947 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
3948
3949 switch (nexthop->type) {
3950 case NEXTHOP_TYPE_BLACKHOLE:
3951 snprintf(nhname, sizeof(nhname), "Blackhole");
3952 break;
3953 case NEXTHOP_TYPE_IFINDEX:
3954 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
3955 snprintf(nhname, sizeof(nhname), "%s",
3956 ifp ? ifp->name : "Unknown");
3957 break;
3958 case NEXTHOP_TYPE_IPV4:
3959 /* fallthrough */
3960 case NEXTHOP_TYPE_IPV4_IFINDEX:
3961 inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
3962 break;
3963 case NEXTHOP_TYPE_IPV6:
3964 case NEXTHOP_TYPE_IPV6_IFINDEX:
3965 inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
3966 break;
3967 }
3968
3969 /* Label stack */
3970 label_str[0] = '\0';
3971 if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) {
3972 mpls_label2str(nexthop->nh_label->num_labels,
3973 nexthop->nh_label->label, label_str,
3974 sizeof(label_str), 0 /*pretty*/);
3975 strlcat(label_str, ", ", sizeof(label_str));
3976 }
3977
3978 backup_str[0] = '\0';
3979 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
3980 snprintf(backup_str, sizeof(backup_str), "backup ");
3981 for (i = 0; i < nexthop->backup_num; i++) {
3982 snprintf(temp_str, sizeof(temp_str), "%d, ",
3983 nexthop->backup_idx[i]);
3984 strlcat(backup_str, temp_str, sizeof(backup_str));
3985 }
3986 }
3987
3988 wgt_str[0] = '\0';
3989 if (nexthop->weight)
3990 snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight);
3991
3992 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
3993 straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
3994 nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown",
3995 nexthop->vrf_id,
3996 wgt_str, backup_str,
3997 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
3998 ? "ACTIVE "
3999 : ""),
4000 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
4001 ? "FIB "
4002 : ""),
4003 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
4004 ? "RECURSIVE "
4005 : ""),
4006 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
4007 ? "ONLINK "
4008 : ""),
4009 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
4010 ? "DUPLICATE "
4011 : ""),
4012 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED)
4013 ? "FILTERED " : ""),
4014 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)
4015 ? "BACKUP " : ""),
4016 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE)
4017 ? "SRTE " : ""),
4018 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN)
4019 ? "EVPN " : ""));
4020
4021 }
4022
4023 /* This function dumps the contents of a given RE entry into
4024 * standard debug log. Calling function name and IP prefix in
4025 * question are passed as 1st and 2nd arguments.
4026 */
4027 void _route_entry_dump(const char *func, union prefixconstptr pp,
4028 union prefixconstptr src_pp,
4029 const struct route_entry *re)
4030 {
4031 const struct prefix *src_p = src_pp.p;
4032 bool is_srcdst = src_p && src_p->prefixlen;
4033 char straddr[PREFIX_STRLEN];
4034 char srcaddr[PREFIX_STRLEN];
4035 char flags_buf[128];
4036 char status_buf[128];
4037 struct nexthop *nexthop;
4038 struct vrf *vrf = vrf_lookup_by_id(re->vrf_id);
4039 struct nexthop_group *nhg;
4040
4041 prefix2str(pp, straddr, sizeof(straddr));
4042
4043 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func,
4044 (const void *)re, straddr,
4045 is_srcdst ? " from " : "",
4046 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
4047 : "",
4048 VRF_LOGNAME(vrf), re->vrf_id);
4049 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4050 straddr, (unsigned long)re->uptime, re->type, re->instance,
4051 re->table);
4052 zlog_debug(
4053 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4054 straddr, re->metric, re->mtu, re->distance,
4055 zclient_dump_route_flags(re->flags, flags_buf,
4056 sizeof(flags_buf)),
4057 _dump_re_status(re, status_buf, sizeof(status_buf)));
4058 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
4059 nexthop_group_nexthop_num(&(re->nhe->nhg)),
4060 nexthop_group_active_nexthop_num(&(re->nhe->nhg)));
4061
4062 /* Dump nexthops */
4063 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4064 _route_entry_dump_nh(re, straddr, nexthop);
4065
4066 if (zebra_nhg_get_backup_nhg(re->nhe)) {
4067 zlog_debug("%s: backup nexthops:", straddr);
4068
4069 nhg = zebra_nhg_get_backup_nhg(re->nhe);
4070 for (ALL_NEXTHOPS_PTR(nhg, nexthop))
4071 _route_entry_dump_nh(re, straddr, nexthop);
4072 }
4073
4074 zlog_debug("%s: dump complete", straddr);
4075 }
4076
4077 static int rib_meta_queue_early_route_add(struct meta_queue *mq, void *data)
4078 {
4079 struct zebra_early_route *ere = data;
4080
4081 listnode_add(mq->subq[META_QUEUE_EARLY_ROUTE], data);
4082 mq->size++;
4083
4084 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
4085 zlog_debug(
4086 "Route %pFX(%u) queued for processing into sub-queue %s",
4087 &ere->p, ere->re->vrf_id,
4088 subqueue2str(META_QUEUE_EARLY_ROUTE));
4089
4090 return 0;
4091 }
4092
4093 struct route_entry *zebra_rib_route_entry_new(vrf_id_t vrf_id, int type,
4094 uint8_t instance, uint32_t flags,
4095 uint32_t nhe_id,
4096 uint32_t table_id,
4097 uint32_t metric, uint32_t mtu,
4098 uint8_t distance, route_tag_t tag)
4099 {
4100 struct route_entry *re;
4101
4102 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
4103 re->type = type;
4104 re->instance = instance;
4105 re->distance = distance;
4106 re->flags = flags;
4107 re->metric = metric;
4108 re->mtu = mtu;
4109 re->table = table_id;
4110 re->vrf_id = vrf_id;
4111 re->uptime = monotime(NULL);
4112 re->tag = tag;
4113 re->nhe_id = nhe_id;
4114
4115 return re;
4116 }
4117 /*
4118 * Internal route-add implementation; there are a couple of different public
4119 * signatures. Callers in this path are responsible for the memory they
4120 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4121 * must free those objects. If this returns < 0, an error has occurred and the
4122 * route_entry 're' has not been captured; the caller should free that also.
4123 *
4124 * -1 -> error
4125 * 0 -> Add
4126 * 1 -> update
4127 */
4128 int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
4129 struct prefix_ipv6 *src_p, struct route_entry *re,
4130 struct nhg_hash_entry *re_nhe, bool startup)
4131 {
4132 struct zebra_early_route *ere;
4133
4134 if (!re)
4135 return -1;
4136
4137 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
4138
4139 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4140 ere->afi = afi;
4141 ere->safi = safi;
4142 ere->p = *p;
4143 if (src_p)
4144 ere->src_p = *src_p;
4145 ere->src_p_provided = !!src_p;
4146 ere->re = re;
4147 ere->re_nhe = re_nhe;
4148 ere->startup = startup;
4149
4150 return mq_add_handler(ere, rib_meta_queue_early_route_add);
4151 }
4152
4153 /*
4154 * Add a single route.
4155 */
4156 int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
4157 struct prefix_ipv6 *src_p, struct route_entry *re,
4158 struct nexthop_group *ng, bool startup)
4159 {
4160 int ret;
4161 struct nhg_hash_entry nhe, *n;
4162
4163 if (!re)
4164 return -1;
4165
4166 /* We either need nexthop(s) or an existing nexthop id */
4167 if (ng == NULL && re->nhe_id == 0)
4168 return -1;
4169
4170 /*
4171 * Use a temporary nhe to convey info to the common/main api.
4172 */
4173 zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL));
4174 if (ng)
4175 nhe.nhg.nexthop = ng->nexthop;
4176 else if (re->nhe_id > 0)
4177 nhe.id = re->nhe_id;
4178
4179 n = zebra_nhe_copy(&nhe, 0);
4180 ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, n, startup);
4181
4182 /* In error cases, free the route also */
4183 if (ret < 0)
4184 XFREE(MTYPE_RE, re);
4185
4186 return ret;
4187 }
4188
4189 void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
4190 unsigned short instance, uint32_t flags, struct prefix *p,
4191 struct prefix_ipv6 *src_p, const struct nexthop *nh,
4192 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
4193 uint8_t distance, bool fromkernel)
4194 {
4195 struct zebra_early_route *ere;
4196 struct route_entry *re = NULL;
4197 struct nhg_hash_entry *nhe = NULL;
4198
4199 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4200 table_id, metric, 0, distance, 0);
4201
4202 if (nh) {
4203 nhe = zebra_nhg_alloc();
4204 nhe->nhg.nexthop = nexthop_dup(nh, NULL);
4205 }
4206
4207 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4208 ere->afi = afi;
4209 ere->safi = safi;
4210 ere->p = *p;
4211 if (src_p)
4212 ere->src_p = *src_p;
4213 ere->src_p_provided = !!src_p;
4214 ere->re = re;
4215 ere->re_nhe = nhe;
4216 ere->startup = false;
4217 ere->deletion = true;
4218 ere->fromkernel = fromkernel;
4219
4220 mq_add_handler(ere, rib_meta_queue_early_route_add);
4221 }
4222
4223
4224 int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
4225 unsigned short instance, uint32_t flags, struct prefix *p,
4226 struct prefix_ipv6 *src_p, const struct nexthop *nh,
4227 uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu,
4228 uint8_t distance, route_tag_t tag, bool startup)
4229 {
4230 struct route_entry *re = NULL;
4231 struct nexthop nexthop = {};
4232 struct nexthop_group ng = {};
4233
4234 /* Allocate new route_entry structure. */
4235 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4236 table_id, metric, mtu, distance, tag);
4237
4238 /* If the owner of the route supplies a shared nexthop-group id,
4239 * we'll use that. Otherwise, pass the nexthop along directly.
4240 */
4241 if (!nhe_id) {
4242 /* Add nexthop. */
4243 nexthop = *nh;
4244 nexthop_group_add_sorted(&ng, &nexthop);
4245 }
4246
4247 return rib_add_multipath(afi, safi, p, src_p, re, &ng, startup);
4248 }
4249
4250 static const char *rib_update_event2str(enum rib_update_event event)
4251 {
4252 const char *ret = "UNKNOWN";
4253
4254 switch (event) {
4255 case RIB_UPDATE_KERNEL:
4256 ret = "RIB_UPDATE_KERNEL";
4257 break;
4258 case RIB_UPDATE_RMAP_CHANGE:
4259 ret = "RIB_UPDATE_RMAP_CHANGE";
4260 break;
4261 case RIB_UPDATE_OTHER:
4262 ret = "RIB_UPDATE_OTHER";
4263 break;
4264 case RIB_UPDATE_MAX:
4265 break;
4266 }
4267
4268 return ret;
4269 }
4270
4271
4272 /* Schedule route nodes to be processed if they match the type */
4273 static void rib_update_route_node(struct route_node *rn, int type)
4274 {
4275 struct route_entry *re, *next;
4276 bool re_changed = false;
4277
4278 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4279 if (type == ZEBRA_ROUTE_ALL || type == re->type) {
4280 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
4281 re_changed = true;
4282 }
4283 }
4284
4285 if (re_changed)
4286 rib_queue_add(rn);
4287 }
4288
4289 /* Schedule routes of a particular table (address-family) based on event. */
4290 void rib_update_table(struct route_table *table, enum rib_update_event event,
4291 int rtype)
4292 {
4293 struct route_node *rn;
4294
4295 if (IS_ZEBRA_DEBUG_EVENT) {
4296 struct zebra_vrf *zvrf;
4297 struct vrf *vrf;
4298
4299 zvrf = table->info
4300 ? ((struct rib_table_info *)table->info)->zvrf
4301 : NULL;
4302 vrf = zvrf ? zvrf->vrf : NULL;
4303
4304 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__,
4305 table->info ? afi2str(
4306 ((struct rib_table_info *)table->info)->afi)
4307 : "Unknown",
4308 VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0,
4309 rib_update_event2str(event), zebra_route_string(rtype));
4310 }
4311
4312 /* Walk all routes and queue for processing, if appropriate for
4313 * the trigger event.
4314 */
4315 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4316 /*
4317 * If we are looking at a route node and the node
4318 * has already been queued we don't
4319 * need to queue it up again
4320 */
4321 if (rn->info
4322 && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
4323 RIB_ROUTE_ANY_QUEUED))
4324 continue;
4325
4326 switch (event) {
4327 case RIB_UPDATE_KERNEL:
4328 rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL);
4329 break;
4330 case RIB_UPDATE_RMAP_CHANGE:
4331 case RIB_UPDATE_OTHER:
4332 rib_update_route_node(rn, rtype);
4333 break;
4334 default:
4335 break;
4336 }
4337 }
4338 }
4339
4340 static void rib_update_handle_vrf(vrf_id_t vrf_id, enum rib_update_event event,
4341 int rtype)
4342 {
4343 struct route_table *table;
4344
4345 if (IS_ZEBRA_DEBUG_EVENT)
4346 zlog_debug("%s: Handling VRF %s event %s", __func__,
4347 vrf_id_to_name(vrf_id), rib_update_event2str(event));
4348
4349 /* Process routes of interested address-families. */
4350 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
4351 if (table)
4352 rib_update_table(table, event, rtype);
4353
4354 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
4355 if (table)
4356 rib_update_table(table, event, rtype);
4357 }
4358
4359 static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype)
4360 {
4361 struct zebra_router_table *zrt;
4362
4363 if (IS_ZEBRA_DEBUG_EVENT)
4364 zlog_debug("%s: Handling VRF (ALL) event %s", __func__,
4365 rib_update_event2str(event));
4366
4367 /* Just iterate over all the route tables, rather than vrf lookups */
4368 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables)
4369 rib_update_table(zrt->table, event, rtype);
4370 }
4371
4372 struct rib_update_ctx {
4373 enum rib_update_event event;
4374 bool vrf_all;
4375 vrf_id_t vrf_id;
4376 };
4377
4378 static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id,
4379 enum rib_update_event event)
4380 {
4381 struct rib_update_ctx *ctx;
4382
4383 ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx));
4384
4385 ctx->event = event;
4386 ctx->vrf_id = vrf_id;
4387
4388 return ctx;
4389 }
4390
4391 static void rib_update_ctx_fini(struct rib_update_ctx **ctx)
4392 {
4393 XFREE(MTYPE_RIB_UPDATE_CTX, *ctx);
4394 }
4395
4396 static void rib_update_handler(struct thread *thread)
4397 {
4398 struct rib_update_ctx *ctx;
4399
4400 ctx = THREAD_ARG(thread);
4401
4402 if (ctx->vrf_all)
4403 rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL);
4404 else
4405 rib_update_handle_vrf(ctx->vrf_id, ctx->event, ZEBRA_ROUTE_ALL);
4406
4407 rib_update_ctx_fini(&ctx);
4408 }
4409
4410 /*
4411 * Thread list to ensure we don't schedule a ton of events
4412 * if interfaces are flapping for instance.
4413 */
4414 static struct thread *t_rib_update_threads[RIB_UPDATE_MAX];
4415
4416 /* Schedule a RIB update event for all vrfs */
4417 void rib_update(enum rib_update_event event)
4418 {
4419 struct rib_update_ctx *ctx;
4420
4421 if (thread_is_scheduled(t_rib_update_threads[event]))
4422 return;
4423
4424 ctx = rib_update_ctx_init(0, event);
4425 ctx->vrf_all = true;
4426
4427 thread_add_event(zrouter.master, rib_update_handler, ctx, 0,
4428 &t_rib_update_threads[event]);
4429
4430 if (IS_ZEBRA_DEBUG_EVENT)
4431 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__,
4432 rib_update_event2str(event));
4433 }
4434
4435 /* Delete self installed routes after zebra is relaunched. */
4436 void rib_sweep_table(struct route_table *table)
4437 {
4438 struct route_node *rn;
4439 struct route_entry *re;
4440 struct route_entry *next;
4441 struct nexthop *nexthop;
4442
4443 if (!table)
4444 return;
4445
4446 if (IS_ZEBRA_DEBUG_RIB)
4447 zlog_debug("%s: starting", __func__);
4448
4449 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4450 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4451
4452 if (IS_ZEBRA_DEBUG_RIB)
4453 route_entry_dump(&rn->p, NULL, re);
4454
4455 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4456 continue;
4457
4458 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
4459 continue;
4460
4461 /*
4462 * If routes are older than startup_time then
4463 * we know we read them in from the kernel.
4464 * As such we can safely remove them.
4465 */
4466 if (zrouter.startup_time < re->uptime)
4467 continue;
4468
4469 /*
4470 * So we are starting up and have received
4471 * routes from the kernel that we have installed
4472 * from a previous run of zebra but not cleaned
4473 * up ( say a kill -9 )
4474 * But since we haven't actually installed
4475 * them yet( we received them from the kernel )
4476 * we don't think they are active.
4477 * So let's pretend they are active to actually
4478 * remove them.
4479 * In all honesty I'm not sure if we should
4480 * mark them as active when we receive them
4481 * This is startup only so probably ok.
4482 *
4483 * If we ever decide to move rib_sweep_table
4484 * to a different spot (ie startup )
4485 * this decision needs to be revisited
4486 */
4487 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
4488 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4489 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
4490
4491 rib_uninstall_kernel(rn, re);
4492 rib_delnode(rn, re);
4493 }
4494 }
4495
4496 if (IS_ZEBRA_DEBUG_RIB)
4497 zlog_debug("%s: ends", __func__);
4498 }
4499
4500 /* Sweep all RIB tables. */
4501 void rib_sweep_route(struct thread *t)
4502 {
4503 struct vrf *vrf;
4504 struct zebra_vrf *zvrf;
4505
4506 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4507 if ((zvrf = vrf->info) == NULL)
4508 continue;
4509
4510 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
4511 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
4512 }
4513
4514 zebra_router_sweep_route();
4515 zebra_router_sweep_nhgs();
4516 }
4517
4518 /* Remove specific by protocol routes from 'table'. */
4519 unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
4520 struct route_table *table)
4521 {
4522 struct route_node *rn;
4523 struct route_entry *re;
4524 struct route_entry *next;
4525 unsigned long n = 0;
4526
4527 if (table)
4528 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
4529 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4530 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4531 continue;
4532 if (re->type == proto
4533 && re->instance == instance) {
4534 rib_delnode(rn, re);
4535 n++;
4536 }
4537 }
4538 return n;
4539 }
4540
4541 /* Remove specific by protocol routes. */
4542 unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
4543 {
4544 struct vrf *vrf;
4545 struct zebra_vrf *zvrf;
4546 struct other_route_table *ort;
4547 unsigned long cnt = 0;
4548
4549 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4550 zvrf = vrf->info;
4551 if (!zvrf)
4552 continue;
4553
4554 cnt += rib_score_proto_table(proto, instance,
4555 zvrf->table[AFI_IP][SAFI_UNICAST])
4556 + rib_score_proto_table(
4557 proto, instance,
4558 zvrf->table[AFI_IP6][SAFI_UNICAST]);
4559
4560 frr_each(otable, &zvrf->other_tables, ort) cnt +=
4561 rib_score_proto_table(proto, instance, ort->table);
4562 }
4563
4564 return cnt;
4565 }
4566
4567 /* Close RIB and clean up kernel routes. */
4568 void rib_close_table(struct route_table *table)
4569 {
4570 struct route_node *rn;
4571 rib_dest_t *dest;
4572
4573 if (!table)
4574 return;
4575
4576 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4577 dest = rib_dest_from_rnode(rn);
4578
4579 if (dest && dest->selected_fib) {
4580 rib_uninstall_kernel(rn, dest->selected_fib);
4581 dest->selected_fib = NULL;
4582 }
4583 }
4584 }
4585
4586 /*
4587 * Handler for async dataplane results after a pseudowire installation
4588 */
4589 static void handle_pw_result(struct zebra_dplane_ctx *ctx)
4590 {
4591 struct zebra_pw *pw;
4592 struct zebra_vrf *vrf;
4593
4594 /* The pseudowire code assumes success - we act on an error
4595 * result for installation attempts here.
4596 */
4597 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
4598 return;
4599
4600 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
4601 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
4602 pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx));
4603 if (pw)
4604 zebra_pw_install_failure(pw,
4605 dplane_ctx_get_pw_status(ctx));
4606 }
4607 }
4608
4609 /*
4610 * Handle results from the dataplane system. Dequeue update context
4611 * structs, dispatch to appropriate internal handlers.
4612 */
4613 static void rib_process_dplane_results(struct thread *thread)
4614 {
4615 struct zebra_dplane_ctx *ctx;
4616 struct dplane_ctx_q ctxlist;
4617 bool shut_p = false;
4618
4619 /* Dequeue a list of completed updates with one lock/unlock cycle */
4620
4621 do {
4622 TAILQ_INIT(&ctxlist);
4623
4624 /* Take lock controlling queue of results */
4625 frr_with_mutex (&dplane_mutex) {
4626 /* Dequeue list of context structs */
4627 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
4628 }
4629
4630 /* Dequeue context block */
4631 ctx = dplane_ctx_dequeue(&ctxlist);
4632
4633 /* If we've emptied the results queue, we're done */
4634 if (ctx == NULL)
4635 break;
4636
4637 /* If zebra is shutting down, avoid processing results,
4638 * just drain the results queue.
4639 */
4640 shut_p = atomic_load_explicit(&zrouter.in_shutdown,
4641 memory_order_relaxed);
4642 if (shut_p) {
4643 while (ctx) {
4644 dplane_ctx_fini(&ctx);
4645
4646 ctx = dplane_ctx_dequeue(&ctxlist);
4647 }
4648
4649 continue;
4650 }
4651
4652 #ifdef HAVE_SCRIPTING
4653 char *script_name = frrscript_names_get_script_name(
4654 ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
4655
4656 int ret = 1;
4657 struct frrscript *fs;
4658
4659 if (script_name) {
4660 fs = frrscript_new(script_name);
4661 if (fs)
4662 ret = frrscript_load(
4663 fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4664 NULL);
4665 }
4666 #endif /* HAVE_SCRIPTING */
4667
4668 while (ctx) {
4669
4670 #ifdef HAVE_SCRIPTING
4671 if (ret == 0)
4672 frrscript_call(fs,
4673 ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4674 ("ctx", ctx));
4675 #endif /* HAVE_SCRIPTING */
4676
4677 switch (dplane_ctx_get_op(ctx)) {
4678 case DPLANE_OP_ROUTE_INSTALL:
4679 case DPLANE_OP_ROUTE_UPDATE:
4680 case DPLANE_OP_ROUTE_DELETE:
4681 /* Bit of special case for route updates
4682 * that were generated by async notifications:
4683 * we don't want to continue processing these
4684 * in the rib.
4685 */
4686 if (dplane_ctx_get_notif_provider(ctx) == 0)
4687 rib_process_result(ctx);
4688 break;
4689
4690 case DPLANE_OP_ROUTE_NOTIFY:
4691 rib_process_dplane_notify(ctx);
4692 break;
4693
4694 case DPLANE_OP_NH_INSTALL:
4695 case DPLANE_OP_NH_UPDATE:
4696 case DPLANE_OP_NH_DELETE:
4697 zebra_nhg_dplane_result(ctx);
4698 break;
4699
4700 case DPLANE_OP_LSP_INSTALL:
4701 case DPLANE_OP_LSP_UPDATE:
4702 case DPLANE_OP_LSP_DELETE:
4703 /* Bit of special case for LSP updates
4704 * that were generated by async notifications:
4705 * we don't want to continue processing these.
4706 */
4707 if (dplane_ctx_get_notif_provider(ctx) == 0)
4708 zebra_mpls_lsp_dplane_result(ctx);
4709 break;
4710
4711 case DPLANE_OP_LSP_NOTIFY:
4712 zebra_mpls_process_dplane_notify(ctx);
4713 break;
4714
4715 case DPLANE_OP_PW_INSTALL:
4716 case DPLANE_OP_PW_UNINSTALL:
4717 handle_pw_result(ctx);
4718 break;
4719
4720 case DPLANE_OP_SYS_ROUTE_ADD:
4721 case DPLANE_OP_SYS_ROUTE_DELETE:
4722 break;
4723
4724 case DPLANE_OP_MAC_INSTALL:
4725 case DPLANE_OP_MAC_DELETE:
4726 zebra_vxlan_handle_result(ctx);
4727 break;
4728
4729 case DPLANE_OP_RULE_ADD:
4730 case DPLANE_OP_RULE_DELETE:
4731 case DPLANE_OP_RULE_UPDATE:
4732 case DPLANE_OP_IPTABLE_ADD:
4733 case DPLANE_OP_IPTABLE_DELETE:
4734 case DPLANE_OP_IPSET_ADD:
4735 case DPLANE_OP_IPSET_DELETE:
4736 case DPLANE_OP_IPSET_ENTRY_ADD:
4737 case DPLANE_OP_IPSET_ENTRY_DELETE:
4738 zebra_pbr_dplane_result(ctx);
4739 break;
4740
4741 case DPLANE_OP_INTF_ADDR_ADD:
4742 case DPLANE_OP_INTF_ADDR_DEL:
4743 case DPLANE_OP_INTF_INSTALL:
4744 case DPLANE_OP_INTF_UPDATE:
4745 case DPLANE_OP_INTF_DELETE:
4746 case DPLANE_OP_INTF_NETCONFIG:
4747 zebra_if_dplane_result(ctx);
4748 break;
4749
4750 case DPLANE_OP_TC_QDISC_INSTALL:
4751 case DPLANE_OP_TC_QDISC_UNINSTALL:
4752 case DPLANE_OP_TC_CLASS_ADD:
4753 case DPLANE_OP_TC_CLASS_DELETE:
4754 case DPLANE_OP_TC_CLASS_UPDATE:
4755 case DPLANE_OP_TC_FILTER_ADD:
4756 case DPLANE_OP_TC_FILTER_DELETE:
4757 case DPLANE_OP_TC_FILTER_UPDATE:
4758 break;
4759
4760 /* Some op codes not handled here */
4761 case DPLANE_OP_ADDR_INSTALL:
4762 case DPLANE_OP_ADDR_UNINSTALL:
4763 case DPLANE_OP_NEIGH_INSTALL:
4764 case DPLANE_OP_NEIGH_UPDATE:
4765 case DPLANE_OP_NEIGH_DELETE:
4766 case DPLANE_OP_NEIGH_IP_INSTALL:
4767 case DPLANE_OP_NEIGH_IP_DELETE:
4768 case DPLANE_OP_VTEP_ADD:
4769 case DPLANE_OP_VTEP_DELETE:
4770 case DPLANE_OP_NEIGH_DISCOVER:
4771 case DPLANE_OP_BR_PORT_UPDATE:
4772 case DPLANE_OP_NEIGH_TABLE_UPDATE:
4773 case DPLANE_OP_GRE_SET:
4774 case DPLANE_OP_NONE:
4775 break;
4776
4777 } /* Dispatch by op code */
4778
4779 dplane_ctx_fini(&ctx);
4780 ctx = dplane_ctx_dequeue(&ctxlist);
4781 }
4782
4783 } while (1);
4784 }
4785
4786 /*
4787 * Results are returned from the dataplane subsystem, in the context of
4788 * the dataplane pthread. We enqueue the results here for processing by
4789 * the main thread later.
4790 */
4791 static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
4792 {
4793 /* Take lock controlling queue of results */
4794 frr_with_mutex (&dplane_mutex) {
4795 /* Enqueue context blocks */
4796 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
4797 }
4798
4799 /* Ensure event is signalled to zebra main pthread */
4800 thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
4801 &t_dplane);
4802
4803 return 0;
4804 }
4805
4806 /*
4807 * Ensure there are no empty slots in the route_info array.
4808 * Every route type in zebra should be present there.
4809 */
4810 static void check_route_info(void)
4811 {
4812 int len = array_size(route_info);
4813
4814 /*
4815 * ZEBRA_ROUTE_SYSTEM is special cased since
4816 * its key is 0 anyway.
4817 *
4818 * ZEBRA_ROUTE_ALL is also ignored.
4819 */
4820 for (int i = 0; i < len; i++) {
4821 assert(route_info[i].key >= ZEBRA_ROUTE_SYSTEM &&
4822 route_info[i].key < ZEBRA_ROUTE_MAX);
4823 assert(route_info[i].meta_q_map < MQ_SIZE);
4824 }
4825 }
4826
4827 /* Routing information base initialize. */
4828 void rib_init(void)
4829 {
4830 check_route_info();
4831
4832 rib_queue_init();
4833
4834 /* Init dataplane, and register for results */
4835 pthread_mutex_init(&dplane_mutex, NULL);
4836 TAILQ_INIT(&rib_dplane_q);
4837 zebra_dplane_init(rib_dplane_results);
4838 }
4839
4840 /*
4841 * vrf_id_get_next
4842 *
4843 * Get the first vrf id that is greater than the given vrf id if any.
4844 *
4845 * Returns true if a vrf id was found, false otherwise.
4846 */
4847 static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
4848 {
4849 struct vrf *vrf;
4850
4851 vrf = vrf_lookup_by_id(vrf_id);
4852 if (vrf) {
4853 vrf = RB_NEXT(vrf_id_head, vrf);
4854 if (vrf) {
4855 *next_id_p = vrf->vrf_id;
4856 return 1;
4857 }
4858 }
4859
4860 return 0;
4861 }
4862
4863 /*
4864 * rib_tables_iter_next
4865 *
4866 * Returns the next table in the iteration.
4867 */
4868 struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
4869 {
4870 struct route_table *table;
4871
4872 /*
4873 * Array that helps us go over all AFI/SAFI combinations via one
4874 * index.
4875 */
4876 static const struct {
4877 afi_t afi;
4878 safi_t safi;
4879 } afi_safis[] = {
4880 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
4881 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
4882 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
4883 };
4884
4885 table = NULL;
4886
4887 switch (iter->state) {
4888
4889 case RIB_TABLES_ITER_S_INIT:
4890 iter->vrf_id = VRF_DEFAULT;
4891 iter->afi_safi_ix = -1;
4892
4893 /* Fall through */
4894
4895 case RIB_TABLES_ITER_S_ITERATING:
4896 iter->afi_safi_ix++;
4897 while (1) {
4898
4899 while (iter->afi_safi_ix
4900 < (int)array_size(afi_safis)) {
4901 table = zebra_vrf_table(
4902 afi_safis[iter->afi_safi_ix].afi,
4903 afi_safis[iter->afi_safi_ix].safi,
4904 iter->vrf_id);
4905 if (table)
4906 break;
4907
4908 iter->afi_safi_ix++;
4909 }
4910
4911 /*
4912 * Found another table in this vrf.
4913 */
4914 if (table)
4915 break;
4916
4917 /*
4918 * Done with all tables in the current vrf, go to the
4919 * next
4920 * one.
4921 */
4922 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
4923 break;
4924
4925 iter->afi_safi_ix = 0;
4926 }
4927
4928 break;
4929
4930 case RIB_TABLES_ITER_S_DONE:
4931 return NULL;
4932 }
4933
4934 if (table)
4935 iter->state = RIB_TABLES_ITER_S_ITERATING;
4936 else
4937 iter->state = RIB_TABLES_ITER_S_DONE;
4938
4939 return table;
4940 }