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