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