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