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