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