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