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