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