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