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