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