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