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