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