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