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