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