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