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