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