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