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