]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
zebra: Abstract nhg deletion to reduce code duplication
[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 #include "nexthop_group_private.h"
40 #include "frr_pthread.h"
41 #include "printfrr.h"
42 #include "frrscript.h"
43
44 #include "zebra/zebra_router.h"
45 #include "zebra/connected.h"
46 #include "zebra/debug.h"
47 #include "zebra/interface.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/rib.h"
50 #include "zebra/rt.h"
51 #include "zebra/zapi_msg.h"
52 #include "zebra/zebra_errors.h"
53 #include "zebra/zebra_ns.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/zebra_routemap.h"
56 #include "zebra/zebra_vrf.h"
57 #include "zebra/zebra_vxlan.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_dplane.h"
60 #include "zebra/zebra_evpn_mh.h"
61 #include "zebra/zebra_script.h"
62
63 DEFINE_MGROUP(ZEBRA, "zebra");
64
65 DEFINE_MTYPE(ZEBRA, RE, "Route Entry");
66 DEFINE_MTYPE_STATIC(ZEBRA, RIB_DEST, "RIB destination");
67 DEFINE_MTYPE_STATIC(ZEBRA, RIB_UPDATE_CTX, "Rib update context object");
68 DEFINE_MTYPE_STATIC(ZEBRA, WQ_WRAPPER, "WQ wrapper");
69
70 /*
71 * Event, list, and mutex for delivery of dataplane results
72 */
73 static pthread_mutex_t dplane_mutex;
74 static struct thread *t_dplane;
75 static struct dplane_ctx_q rib_dplane_q;
76
77 DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
78 (rn, reason));
79
80 /* Should we allow non FRR processes to delete our routes */
81 extern int allow_delete;
82
83 /* Each route type's string and default distance value. */
84 static const struct {
85 int key;
86 uint8_t distance;
87 uint8_t meta_q_map;
88 } route_info[ZEBRA_ROUTE_MAX] = {
89 [ZEBRA_ROUTE_NHG] = {ZEBRA_ROUTE_NHG, 255 /* Unneeded for nhg's */, 0},
90 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0, 7},
91 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0, 3},
92 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0, 2},
93 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1, 4},
94 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120, 5},
95 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120, 5},
96 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110, 5},
97 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110, 5},
98 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115, 5},
99 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */, 6},
100 [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255, 7},
101 [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90, 5},
102 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10, 5},
103 [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255, 7},
104 [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255, 7},
105 [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150, 4},
106 [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150, 7},
107 [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20, 6},
108 [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20, 6},
109 [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20, 6},
110 [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20, 6},
111 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20, 6},
112 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100, 5},
113 [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150, 7},
114 [ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, 200, 7},
115 [ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, 255, 7},
116 [ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC, 115, 5},
117 [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, 255, 7},
118 [ZEBRA_ROUTE_SRTE] = {ZEBRA_ROUTE_SRTE, 255, 7},
119 /* Any new route type added to zebra, should be mirrored here */
120
121 /* no entry/default: 150 */
122 };
123
124 /* EVPN/VXLAN subqueue is number 1 */
125 #define META_QUEUE_EVPN 1
126
127 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
128 * from the OS, and an 'nhe' is a nhe update.
129 */
130 struct wq_nhg_wrapper {
131 int type;
132 union {
133 struct nhg_ctx *ctx;
134 struct nhg_hash_entry *nhe;
135 } u;
136 };
137
138 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
139 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
140
141 /* Wrapper structs for evpn/vxlan workqueue items. */
142 struct wq_evpn_wrapper {
143 int type;
144 bool add_p;
145 vrf_id_t vrf_id;
146 bool esr_rxed;
147 uint8_t df_alg;
148 uint16_t df_pref;
149 uint32_t flags;
150 uint32_t seq;
151 esi_t esi;
152 vni_t vni;
153 struct ipaddr ip;
154 struct ethaddr macaddr;
155 struct prefix prefix;
156 struct in_addr vtep_ip;
157 };
158
159 #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
160 #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
161 #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
162 #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
163
164 /* %pRN is already a printer for route_nodes that just prints the prefix */
165 #ifdef _FRR_ATTRIBUTE_PRINTFRR
166 #pragma FRR printfrr_ext "%pZN" (struct route_node *)
167 #endif
168
169 printfrr_ext_autoreg_p("ZN", printfrr_zebra_node);
170 static ssize_t printfrr_zebra_node(struct fbuf *buf, struct printfrr_eargs *ea,
171 const void *ptr)
172 {
173 struct route_node *rn = (struct route_node *)ptr;
174 ssize_t rv = 0;
175
176 /* just the table number? */
177 if (ea->fmt[0] == 't') {
178 rib_dest_t *dest;
179 struct route_entry *re = NULL;
180
181 ea->fmt++;
182
183 if (!rn)
184 return bputch(buf, '!');
185
186 dest = rib_dest_from_rnode(rn);
187 if (dest)
188 re = re_list_first(&dest->routes);
189 if (re)
190 rv += bprintfrr(buf, "%u", re->table);
191 else
192 rv += bputch(buf, '?');
193
194 } else {
195 char cbuf[PREFIX_STRLEN * 2 + 6];
196 struct rib_table_info *info;
197
198 if (!rn)
199 return bputs(buf, "{(route_node *) NULL}");
200
201 srcdest_rnode2str(rn, cbuf, sizeof(cbuf));
202 rv += bputs(buf, cbuf);
203
204 info = srcdest_rnode_table_info(rn);
205 if (info->safi == SAFI_MULTICAST)
206 rv += bputs(buf, " (MRIB)");
207 }
208 return rv;
209 }
210
211 #define rnode_debug(node, vrf_id, msg, ...) \
212 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
213 ##__VA_ARGS__)
214
215 #define rnode_info(node, vrf_id, msg, ...) \
216 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
217 ##__VA_ARGS__)
218
219 static char *_dump_re_status(const struct route_entry *re, char *buf,
220 size_t len)
221 {
222 if (re->status == 0) {
223 snprintfrr(buf, len, "None ");
224 return buf;
225 }
226
227 snprintfrr(
228 buf, len, "%s%s%s%s%s%s%s",
229 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) ? "Removed " : "",
230 CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED) ? "Changed " : "",
231 CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)
232 ? "Label Changed "
233 : "",
234 CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED) ? "Queued " : "",
235 CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) ? "Installed "
236 : "",
237 CHECK_FLAG(re->status, ROUTE_ENTRY_FAILED) ? "Failed " : "",
238 CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG) ? "Fib NHG "
239 : "");
240 return buf;
241 }
242
243 uint8_t route_distance(int type)
244 {
245 uint8_t distance;
246
247 if ((unsigned)type >= array_size(route_info))
248 distance = 150;
249 else
250 distance = route_info[type].distance;
251
252 return distance;
253 }
254
255 int is_zebra_valid_kernel_table(uint32_t table_id)
256 {
257 #ifdef linux
258 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
259 || (table_id == RT_TABLE_COMPAT))
260 return 0;
261 #endif
262
263 return 1;
264 }
265
266 int is_zebra_main_routing_table(uint32_t table_id)
267 {
268 if (table_id == RT_TABLE_MAIN)
269 return 1;
270 return 0;
271 }
272
273 int zebra_check_addr(const struct prefix *p)
274 {
275 if (p->family == AF_INET) {
276 uint32_t addr;
277
278 addr = p->u.prefix4.s_addr;
279 addr = ntohl(addr);
280
281 if (IPV4_NET127(addr) || IN_CLASSD(addr)
282 || IPV4_LINKLOCAL(addr))
283 return 0;
284 }
285 if (p->family == AF_INET6) {
286 if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
287 return 0;
288 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
289 return 0;
290 }
291 return 1;
292 }
293
294 /**
295 * copy_nexthop - copy a nexthop to the rib structure.
296 */
297 void route_entry_copy_nexthops(struct route_entry *re, struct nexthop *nh)
298 {
299 assert(!re->nhe->nhg.nexthop);
300 copy_nexthops(&re->nhe->nhg.nexthop, nh, NULL);
301 }
302
303 static void route_entry_attach_ref(struct route_entry *re,
304 struct nhg_hash_entry *new)
305 {
306 re->nhe = new;
307 re->nhe_id = new->id;
308 re->nhe_installed_id = 0;
309
310 zebra_nhg_increment_ref(new);
311 }
312
313 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
314 int route_entry_update_nhe(struct route_entry *re,
315 struct nhg_hash_entry *new_nhghe)
316 {
317 int ret = 0;
318 struct nhg_hash_entry *old_nhg = NULL;
319
320 if (new_nhghe == NULL) {
321 old_nhg = re->nhe;
322
323 re->nhe_id = 0;
324 re->nhe_installed_id = 0;
325 re->nhe = NULL;
326 goto done;
327 }
328
329 if ((re->nhe_id != 0) && re->nhe && (re->nhe != new_nhghe)) {
330 /* Capture previous nhg, if any */
331 old_nhg = re->nhe;
332
333 route_entry_attach_ref(re, new_nhghe);
334 } else if (!re->nhe)
335 /* This is the first time it's being attached */
336 route_entry_attach_ref(re, new_nhghe);
337
338 done:
339 /* Detach / deref previous nhg */
340 if (old_nhg)
341 zebra_nhg_decrement_ref(old_nhg);
342
343 return ret;
344 }
345
346 void rib_handle_nhg_replace(struct nhg_hash_entry *old_entry,
347 struct nhg_hash_entry *new_entry)
348 {
349 struct zebra_router_table *zrt;
350 struct route_node *rn;
351 struct route_entry *re, *next;
352
353 if (IS_ZEBRA_DEBUG_RIB_DETAILED || IS_ZEBRA_DEBUG_NHG_DETAIL)
354 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
355 __func__, new_entry->id, new_entry, old_entry);
356
357 /* We have to do them ALL */
358 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
359 for (rn = route_top(zrt->table); rn;
360 rn = srcdest_route_next(rn)) {
361 RNODE_FOREACH_RE_SAFE (rn, re, next) {
362 if (re->nhe && re->nhe == old_entry)
363 route_entry_update_nhe(re, new_entry);
364 }
365 }
366 }
367 }
368
369 struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
370 const union g_addr *addr,
371 struct route_node **rn_out)
372 {
373 struct prefix p;
374 struct route_table *table;
375 struct route_node *rn;
376 struct route_entry *match = NULL;
377
378 /* Lookup table. */
379 table = zebra_vrf_table(afi, safi, vrf_id);
380 if (!table)
381 return 0;
382
383 memset(&p, 0, sizeof(struct prefix));
384 p.family = afi;
385 if (afi == AFI_IP) {
386 p.u.prefix4 = addr->ipv4;
387 p.prefixlen = IPV4_MAX_BITLEN;
388 } else {
389 p.u.prefix6 = addr->ipv6;
390 p.prefixlen = IPV6_MAX_BITLEN;
391 }
392
393 rn = route_node_match(table, &p);
394
395 while (rn) {
396 rib_dest_t *dest;
397
398 route_unlock_node(rn);
399
400 dest = rib_dest_from_rnode(rn);
401 if (dest && dest->selected_fib
402 && !CHECK_FLAG(dest->selected_fib->status,
403 ROUTE_ENTRY_REMOVED))
404 match = dest->selected_fib;
405
406 /* If there is no selected route or matched route is EGP, go up
407 tree. */
408 if (!match) {
409 do {
410 rn = rn->parent;
411 } while (rn && rn->info == NULL);
412 if (rn)
413 route_lock_node(rn);
414 } else {
415 if (match->type != ZEBRA_ROUTE_CONNECT) {
416 if (!CHECK_FLAG(match->status,
417 ROUTE_ENTRY_INSTALLED))
418 return NULL;
419 }
420
421 if (rn_out)
422 *rn_out = rn;
423 return match;
424 }
425 }
426 return NULL;
427 }
428
429 struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
430 struct in_addr addr,
431 struct route_node **rn_out)
432 {
433 struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
434 struct route_node *m_rn = NULL, *u_rn = NULL;
435 union g_addr gaddr = {.ipv4 = addr};
436
437 switch (zrouter.ipv4_multicast_mode) {
438 case MCAST_MRIB_ONLY:
439 return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
440 rn_out);
441 case MCAST_URIB_ONLY:
442 return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
443 case MCAST_NO_CONFIG:
444 case MCAST_MIX_MRIB_FIRST:
445 re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
446 &m_rn);
447 if (!mre)
448 re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id,
449 &gaddr, &u_rn);
450 break;
451 case MCAST_MIX_DISTANCE:
452 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
453 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
454 if (mre && ure)
455 re = ure->distance < mre->distance ? ure : mre;
456 else if (mre)
457 re = mre;
458 else if (ure)
459 re = ure;
460 break;
461 case MCAST_MIX_PFXLEN:
462 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
463 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
464 if (mre && ure)
465 re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
466 else if (mre)
467 re = mre;
468 else if (ure)
469 re = ure;
470 break;
471 }
472
473 if (rn_out)
474 *rn_out = (re == mre) ? m_rn : u_rn;
475
476 if (IS_ZEBRA_DEBUG_RIB) {
477 char buf[BUFSIZ];
478 inet_ntop(AF_INET, &addr, buf, BUFSIZ);
479
480 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__,
481 buf, vrf_id_to_name(vrf_id), vrf_id,
482 mre ? (ure ? "MRIB+URIB" : "MRIB")
483 : ure ? "URIB" : "nothing",
484 re == ure ? "URIB" : re == mre ? "MRIB" : "none");
485 }
486 return re;
487 }
488
489 struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
490 {
491 struct route_table *table;
492 struct route_node *rn;
493 struct route_entry *match = NULL;
494 rib_dest_t *dest;
495
496 /* Lookup table. */
497 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
498 if (!table)
499 return 0;
500
501 rn = route_node_lookup(table, (struct prefix *)p);
502
503 /* No route for this prefix. */
504 if (!rn)
505 return NULL;
506
507 /* Unlock node. */
508 route_unlock_node(rn);
509 dest = rib_dest_from_rnode(rn);
510
511 if (dest && dest->selected_fib
512 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
513 match = dest->selected_fib;
514
515 if (!match)
516 return NULL;
517
518 if (match->type == ZEBRA_ROUTE_CONNECT)
519 return match;
520
521 if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED))
522 return match;
523
524 return NULL;
525 }
526
527 /*
528 * Is this RIB labeled-unicast? It must be of type BGP and all paths
529 * (nexthops) must have a label.
530 */
531 int zebra_rib_labeled_unicast(struct route_entry *re)
532 {
533 struct nexthop *nexthop = NULL;
534
535 if (re->type != ZEBRA_ROUTE_BGP)
536 return 0;
537
538 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
539 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
540 return 0;
541
542 return 1;
543 }
544
545 /* Update flag indicates whether this is a "replace" or not. Currently, this
546 * is only used for IPv4.
547 */
548 void rib_install_kernel(struct route_node *rn, struct route_entry *re,
549 struct route_entry *old)
550 {
551 struct nexthop *nexthop;
552 struct rib_table_info *info = srcdest_rnode_table_info(rn);
553 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
554 const struct prefix *p, *src_p;
555 enum zebra_dplane_result ret;
556
557 rib_dest_t *dest = rib_dest_from_rnode(rn);
558
559 srcdest_rnode_prefixes(rn, &p, &src_p);
560
561 if (info->safi != SAFI_UNICAST) {
562 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
563 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
564 return;
565 }
566
567
568 /*
569 * Install the resolved nexthop object first.
570 */
571 zebra_nhg_install_kernel(re->nhe);
572
573 /*
574 * If this is a replace to a new RE let the originator of the RE
575 * know that they've lost
576 */
577 if (old && (old != re) && (old->type != re->type))
578 zsend_route_notify_owner(rn, old, ZAPI_ROUTE_BETTER_ADMIN_WON,
579 info->afi, info->safi);
580
581 /* Update fib selection */
582 dest->selected_fib = re;
583
584 /*
585 * Make sure we update the FPM any time we send new information to
586 * the kernel.
587 */
588 hook_call(rib_update, rn, "installing in kernel");
589
590 /* Send add or update */
591 if (old)
592 ret = dplane_route_update(rn, re, old);
593 else
594 ret = dplane_route_add(rn, re);
595
596 switch (ret) {
597 case ZEBRA_DPLANE_REQUEST_QUEUED:
598 SET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
599
600 if (old) {
601 SET_FLAG(old->status, ROUTE_ENTRY_QUEUED);
602
603 /* Free old FIB nexthop group */
604 UNSET_FLAG(old->status, ROUTE_ENTRY_USE_FIB_NHG);
605 if (old->fib_ng.nexthop) {
606 nexthops_free(old->fib_ng.nexthop);
607 old->fib_ng.nexthop = NULL;
608 }
609 }
610
611 if (zvrf)
612 zvrf->installs_queued++;
613 break;
614 case ZEBRA_DPLANE_REQUEST_FAILURE:
615 {
616 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
617 "%u:%u:%pRN: Failed to enqueue dataplane install",
618 re->vrf_id, re->table, rn);
619 break;
620 }
621 case ZEBRA_DPLANE_REQUEST_SUCCESS:
622 if (zvrf)
623 zvrf->installs++;
624 break;
625 }
626
627 return;
628 }
629
630 /* Uninstall the route from kernel. */
631 void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
632 {
633 struct nexthop *nexthop;
634 struct rib_table_info *info = srcdest_rnode_table_info(rn);
635 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
636
637 if (info->safi != SAFI_UNICAST) {
638 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
639 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
640 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
641 return;
642 }
643
644 /*
645 * Make sure we update the FPM any time we send new information to
646 * the dataplane.
647 */
648 hook_call(rib_update, rn, "uninstalling from kernel");
649
650 switch (dplane_route_delete(rn, re)) {
651 case ZEBRA_DPLANE_REQUEST_QUEUED:
652 if (zvrf)
653 zvrf->removals_queued++;
654 break;
655 case ZEBRA_DPLANE_REQUEST_FAILURE:
656 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
657 "%u:%pRN: Failed to enqueue dataplane uninstall",
658 re->vrf_id, rn);
659 break;
660 case ZEBRA_DPLANE_REQUEST_SUCCESS:
661 if (zvrf)
662 zvrf->removals++;
663 break;
664 }
665
666 return;
667 }
668
669 /*
670 * rib_can_delete_dest
671 *
672 * Returns true if the given dest can be deleted from the table.
673 */
674 static int rib_can_delete_dest(rib_dest_t *dest)
675 {
676 if (re_list_first(&dest->routes)) {
677 return 0;
678 }
679
680 /*
681 * Unresolved rnh's are stored on the default route's list
682 *
683 * dest->rnode can also be the source prefix node in an
684 * ipv6 sourcedest table. Fortunately the prefix of a
685 * source prefix node can never be the default prefix.
686 */
687 if (is_default_prefix(&dest->rnode->p))
688 return 0;
689
690 /*
691 * Don't delete the dest if we have to update the FPM about this
692 * prefix.
693 */
694 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
695 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
696 return 0;
697
698 return 1;
699 }
700
701 void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq,
702 bool rt_delete)
703 {
704 rib_dest_t *dest = rib_dest_from_rnode(rn);
705 struct rnh *rnh;
706
707 /*
708 * We are storing the rnh's associated withb
709 * the tracked nexthop as a list of the rn's.
710 * Unresolved rnh's are placed at the top
711 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
712 * As such for each rn we need to walk up the tree
713 * and see if any rnh's need to see if they
714 * would match a more specific route
715 */
716 while (rn) {
717 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
718 zlog_debug(
719 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
720 __func__, rn,
721 dest ? rnh_list_count(&dest->nht) : 0);
722
723 if (rt_delete && (!dest || !rnh_list_count(&dest->nht))) {
724 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
725 zlog_debug("%pRN has no tracking NHTs. Bailing",
726 rn);
727 break;
728 }
729 if (!dest) {
730 rn = rn->parent;
731 if (rn)
732 dest = rib_dest_from_rnode(rn);
733 continue;
734 }
735 /*
736 * If we have any rnh's stored in the nht list
737 * then we know that this route node was used for
738 * nht resolution and as such we need to call the
739 * nexthop tracking evaluation code
740 */
741 frr_each_safe(rnh_list, &dest->nht, rnh) {
742 struct zebra_vrf *zvrf =
743 zebra_vrf_lookup_by_id(rnh->vrf_id);
744 struct prefix *p = &rnh->node->p;
745
746 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
747 zlog_debug(
748 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
749 zvrf_name(zvrf), zvrf_id(zvrf), rn,
750 rnh->node, seq, rnh->seqno);
751
752 /*
753 * If we have evaluated this node on this pass
754 * already, due to following the tree up
755 * then we know that we can move onto the next
756 * rnh to process.
757 *
758 * Additionally we call zebra_evaluate_rnh
759 * when we gc the dest. In this case we know
760 * that there must be no other re's where
761 * we were originally as such we know that
762 * that sequence number is ok to respect.
763 */
764 if (rnh->seqno == seq) {
765 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
766 zlog_debug(
767 " Node processed and moved already");
768 continue;
769 }
770
771 rnh->seqno = seq;
772 zebra_evaluate_rnh(zvrf, family2afi(p->family), 0, p,
773 rnh->safi);
774 }
775
776 rn = rn->parent;
777 if (rn)
778 dest = rib_dest_from_rnode(rn);
779 }
780 }
781
782 /*
783 * rib_gc_dest
784 *
785 * Garbage collect the rib dest corresponding to the given route node
786 * if appropriate.
787 *
788 * Returns true if the dest was deleted, false otherwise.
789 */
790 int rib_gc_dest(struct route_node *rn)
791 {
792 rib_dest_t *dest;
793
794 dest = rib_dest_from_rnode(rn);
795 if (!dest)
796 return 0;
797
798 if (!rib_can_delete_dest(dest))
799 return 0;
800
801 if (IS_ZEBRA_DEBUG_RIB) {
802 struct zebra_vrf *zvrf;
803
804 zvrf = rib_dest_vrf(dest);
805 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
806 }
807
808 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
809 true);
810
811 dest->rnode = NULL;
812 rnh_list_fini(&dest->nht);
813 XFREE(MTYPE_RIB_DEST, dest);
814 rn->info = NULL;
815
816 /*
817 * Release the one reference that we keep on the route node.
818 */
819 route_unlock_node(rn);
820 return 1;
821 }
822
823 void zebra_rtable_node_cleanup(struct route_table *table,
824 struct route_node *node)
825 {
826 struct route_entry *re, *next;
827
828 RNODE_FOREACH_RE_SAFE (node, re, next) {
829 rib_unlink(node, re);
830 }
831
832 if (node->info) {
833 rib_dest_t *dest = node->info;
834
835 rnh_list_fini(&dest->nht);
836 XFREE(MTYPE_RIB_DEST, node->info);
837 }
838 }
839
840 static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
841 struct route_entry *new)
842 {
843 hook_call(rib_update, rn, "new route selected");
844
845 /* Update real nexthop. This may actually determine if nexthop is active
846 * or not. */
847 if (!nexthop_group_active_nexthop_num(&(new->nhe->nhg))) {
848 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
849 return;
850 }
851
852 if (IS_ZEBRA_DEBUG_RIB)
853 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
854 zvrf_name(zvrf), zvrf_id(zvrf), new->table, rn, rn,
855 new, zebra_route_string(new->type));
856
857 /* If labeled-unicast route, install transit LSP. */
858 if (zebra_rib_labeled_unicast(new))
859 zebra_mpls_lsp_install(zvrf, rn, new);
860
861 rib_install_kernel(rn, new, NULL);
862
863 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
864 }
865
866 static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
867 struct route_entry *old)
868 {
869 hook_call(rib_update, rn, "removing existing route");
870
871 /* Uninstall from kernel. */
872 if (IS_ZEBRA_DEBUG_RIB)
873 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
874 zvrf_name(zvrf), zvrf_id(zvrf), old->table, rn, rn,
875 old, zebra_route_string(old->type));
876
877 /* If labeled-unicast route, uninstall transit LSP. */
878 if (zebra_rib_labeled_unicast(old))
879 zebra_mpls_lsp_uninstall(zvrf, rn, old);
880
881 rib_uninstall_kernel(rn, old);
882
883 /* Update nexthop for route, reset changed flag. */
884 /* Note: this code also handles the Linux case when an interface goes
885 * down, causing the kernel to delete routes without sending DELROUTE
886 * notifications
887 */
888 if (RIB_KERNEL_ROUTE(old))
889 SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
890 else
891 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
892 }
893
894 static void rib_process_update_fib(struct zebra_vrf *zvrf,
895 struct route_node *rn,
896 struct route_entry *old,
897 struct route_entry *new)
898 {
899 int nh_active = 0;
900
901 /*
902 * We have to install or update if a new route has been selected or
903 * something has changed.
904 */
905 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
906 hook_call(rib_update, rn, "updating existing route");
907
908 /* Update the nexthop; we could determine here that nexthop is
909 * inactive. */
910 if (nexthop_group_active_nexthop_num(&(new->nhe->nhg)))
911 nh_active = 1;
912
913 /* If nexthop is active, install the selected route, if
914 * appropriate. If
915 * the install succeeds, cleanup flags for prior route, if
916 * different from
917 * newly selected.
918 */
919 if (nh_active) {
920 if (IS_ZEBRA_DEBUG_RIB) {
921 if (new != old)
922 zlog_debug(
923 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
924 zvrf_name(zvrf), zvrf_id(zvrf),
925 new->table, rn, rn, new,
926 zebra_route_string(new->type),
927 old,
928 zebra_route_string(old->type));
929 else
930 zlog_debug(
931 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
932 zvrf_name(zvrf), zvrf_id(zvrf),
933 new->table, rn, rn, new,
934 zebra_route_string(new->type));
935 }
936
937 /* If labeled-unicast route, uninstall transit LSP. */
938 if (zebra_rib_labeled_unicast(old))
939 zebra_mpls_lsp_uninstall(zvrf, rn, old);
940
941 /*
942 * Non-system route should be installed.
943 * If labeled-unicast route, install transit
944 * LSP.
945 */
946 if (zebra_rib_labeled_unicast(new))
947 zebra_mpls_lsp_install(zvrf, rn, new);
948
949 rib_install_kernel(rn, new, old);
950 }
951
952 /*
953 * If nexthop for selected route is not active or install
954 * failed, we
955 * may need to uninstall and delete for redistribution.
956 */
957 if (!nh_active) {
958 if (IS_ZEBRA_DEBUG_RIB) {
959 if (new != old)
960 zlog_debug(
961 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
962 zvrf_name(zvrf), zvrf_id(zvrf),
963 new->table, rn, rn, new,
964 zebra_route_string(new->type),
965 old,
966 zebra_route_string(old->type));
967 else
968 zlog_debug(
969 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
970 zvrf_name(zvrf), zvrf_id(zvrf),
971 new->table, rn, rn, new,
972 zebra_route_string(new->type));
973 }
974
975 /*
976 * When we have gotten to this point
977 * the new route entry has no nexthops
978 * that are usable and as such we need
979 * to remove the old route, but only
980 * if we were the one who installed
981 * the old route
982 */
983 if (!RIB_SYSTEM_ROUTE(old)) {
984 /* If labeled-unicast route, uninstall transit
985 * LSP. */
986 if (zebra_rib_labeled_unicast(old))
987 zebra_mpls_lsp_uninstall(zvrf, rn, old);
988
989 rib_uninstall_kernel(rn, old);
990 }
991 }
992 } else {
993 /*
994 * Same route selected; check if in the FIB and if not,
995 * re-install. This is housekeeping code to deal with
996 * race conditions in kernel with linux netlink reporting
997 * interface up before IPv4 or IPv6 protocol is ready
998 * to add routes.
999 */
1000 if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) ||
1001 RIB_SYSTEM_ROUTE(new))
1002 rib_install_kernel(rn, new, NULL);
1003 }
1004
1005 /* Update prior route. */
1006 if (new != old)
1007 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1008
1009 /* Clear changed flag. */
1010 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1011 }
1012
1013 /* Check if 'alternate' RIB entry is better than 'current'. */
1014 static struct route_entry *rib_choose_best(struct route_entry *current,
1015 struct route_entry *alternate)
1016 {
1017 if (current == NULL)
1018 return alternate;
1019
1020 /* filter route selection in following order:
1021 * - connected beats other types
1022 * - if both connected, loopback or vrf wins
1023 * - lower distance beats higher
1024 * - lower metric beats higher for equal distance
1025 * - last, hence oldest, route wins tie break.
1026 */
1027
1028 /* Connected routes. Check to see if either are a vrf
1029 * or loopback interface. If not, pick the last connected
1030 * route of the set of lowest metric connected routes.
1031 */
1032 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
1033 if (current->type != ZEBRA_ROUTE_CONNECT)
1034 return alternate;
1035
1036 /* both are connected. are either loop or vrf? */
1037 struct nexthop *nexthop = NULL;
1038
1039 for (ALL_NEXTHOPS(alternate->nhe->nhg, nexthop)) {
1040 struct interface *ifp = if_lookup_by_index(
1041 nexthop->ifindex, alternate->vrf_id);
1042
1043 if (ifp && if_is_loopback(ifp))
1044 return alternate;
1045 }
1046
1047 for (ALL_NEXTHOPS(current->nhe->nhg, nexthop)) {
1048 struct interface *ifp = if_lookup_by_index(
1049 nexthop->ifindex, current->vrf_id);
1050
1051 if (ifp && if_is_loopback(ifp))
1052 return current;
1053 }
1054
1055 /* Neither are loop or vrf so pick best metric */
1056 if (alternate->metric <= current->metric)
1057 return alternate;
1058
1059 return current;
1060 }
1061
1062 if (current->type == ZEBRA_ROUTE_CONNECT)
1063 return current;
1064
1065 /* higher distance loses */
1066 if (alternate->distance < current->distance)
1067 return alternate;
1068 if (current->distance < alternate->distance)
1069 return current;
1070
1071 /* metric tie-breaks equal distance */
1072 if (alternate->metric <= current->metric)
1073 return alternate;
1074
1075 return current;
1076 }
1077
1078 /* Core function for processing routing information base. */
1079 static void rib_process(struct route_node *rn)
1080 {
1081 struct route_entry *re;
1082 struct route_entry *next;
1083 struct route_entry *old_selected = NULL;
1084 struct route_entry *new_selected = NULL;
1085 struct route_entry *old_fib = NULL;
1086 struct route_entry *new_fib = NULL;
1087 struct route_entry *best = NULL;
1088 rib_dest_t *dest;
1089 struct zebra_vrf *zvrf = NULL;
1090 struct vrf *vrf;
1091
1092 vrf_id_t vrf_id = VRF_UNKNOWN;
1093
1094 assert(rn);
1095
1096 dest = rib_dest_from_rnode(rn);
1097 /*
1098 * We have an enqueued node with nothing to process here
1099 * let's just finish up and return;
1100 */
1101 if (!dest)
1102 return;
1103
1104 zvrf = rib_dest_vrf(dest);
1105 vrf_id = zvrf_id(zvrf);
1106
1107 vrf = vrf_lookup_by_id(vrf_id);
1108
1109 /*
1110 * we can have rn's that have a NULL info pointer
1111 * (dest). As such let's not let the deref happen
1112 * additionally we know RNODE_FOREACH_RE_SAFE
1113 * will not iterate so we are ok.
1114 */
1115 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1116 struct route_entry *re = re_list_first(&dest->routes);
1117
1118 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1119 VRF_LOGNAME(vrf), vrf_id, re->table, rn,
1120 rn);
1121 }
1122
1123 old_fib = dest->selected_fib;
1124
1125 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1126 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1127 char flags_buf[128];
1128 char status_buf[128];
1129
1130 zlog_debug(
1131 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1132 VRF_LOGNAME(vrf), vrf_id, re->table, rn, re,
1133 zebra_route_string(re->type),
1134 _dump_re_status(re, status_buf,
1135 sizeof(status_buf)),
1136 zclient_dump_route_flags(re->flags, flags_buf,
1137 sizeof(flags_buf)),
1138 re->distance, re->metric);
1139 }
1140
1141 /* Currently selected re. */
1142 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1143 assert(old_selected == NULL);
1144 old_selected = re;
1145 }
1146
1147 /* Skip deleted entries from selection */
1148 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1149 continue;
1150
1151 /*
1152 * If the route entry has changed, verify/resolve
1153 * the nexthops associated with the entry.
1154 *
1155 * In any event if we have nexthops that are not active
1156 * then we cannot use this particular route entry so
1157 * skip it.
1158 */
1159 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1160 if (!nexthop_active_update(rn, re)) {
1161 const struct prefix *p;
1162 struct rib_table_info *info;
1163
1164 if (re->type == ZEBRA_ROUTE_TABLE) {
1165 /* XXX: HERE BE DRAGONS!!!!!
1166 * In all honesty, I have not yet
1167 * figured out what this part does or
1168 * why the ROUTE_ENTRY_CHANGED test
1169 * above is correct or why we need to
1170 * delete a route here, and also not
1171 * whether this concerns both selected
1172 * and fib route, or only selected
1173 * or only fib
1174 *
1175 * This entry was denied by the 'ip
1176 * protocol
1177 * table' route-map, we need to delete
1178 * it */
1179 if (re != old_selected) {
1180 if (IS_ZEBRA_DEBUG_RIB)
1181 zlog_debug(
1182 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1183 __func__,
1184 VRF_LOGNAME(
1185 vrf),
1186 vrf_id, rn);
1187 rib_unlink(rn, re);
1188 } else
1189 SET_FLAG(re->status,
1190 ROUTE_ENTRY_REMOVED);
1191 }
1192
1193 info = srcdest_rnode_table_info(rn);
1194 srcdest_rnode_prefixes(rn, &p, NULL);
1195 zsend_route_notify_owner(
1196 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
1197 info->afi, info->safi);
1198 continue;
1199 }
1200 } else {
1201 /*
1202 * If the re has not changed and the nhg we have is
1203 * not usable, then we cannot use this route entry
1204 * for consideration, as that the route will just
1205 * not install if it is selected.
1206 */
1207 if (!nexthop_group_active_nexthop_num(&re->nhe->nhg))
1208 continue;
1209 }
1210
1211 /* Infinite distance. */
1212 if (re->distance == DISTANCE_INFINITY &&
1213 re->type != ZEBRA_ROUTE_KERNEL) {
1214 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1215 continue;
1216 }
1217
1218 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1219 best = rib_choose_best(new_fib, re);
1220 if (new_fib && best != new_fib)
1221 UNSET_FLAG(new_fib->status,
1222 ROUTE_ENTRY_CHANGED);
1223 new_fib = best;
1224 } else {
1225 best = rib_choose_best(new_selected, re);
1226 if (new_selected && best != new_selected)
1227 UNSET_FLAG(new_selected->status,
1228 ROUTE_ENTRY_CHANGED);
1229 new_selected = best;
1230 }
1231 if (best != re)
1232 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1233 } /* RNODE_FOREACH_RE */
1234
1235 /* If no FIB override route, use the selected route also for FIB */
1236 if (new_fib == NULL)
1237 new_fib = new_selected;
1238
1239 /* After the cycle is finished, the following pointers will be set:
1240 * old_selected --- RE entry currently having SELECTED
1241 * new_selected --- RE entry that is newly SELECTED
1242 * old_fib --- RE entry currently in kernel FIB
1243 * new_fib --- RE entry that is newly to be in kernel FIB
1244 *
1245 * new_selected will get SELECTED flag, and is going to be redistributed
1246 * the zclients. new_fib (which can be new_selected) will be installed
1247 * in kernel.
1248 */
1249
1250 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1251 struct route_entry *entry;
1252
1253 entry = old_selected
1254 ? old_selected
1255 : new_selected
1256 ? new_selected
1257 : old_fib ? old_fib
1258 : new_fib ? new_fib : NULL;
1259
1260 zlog_debug(
1261 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1262 VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn,
1263 (void *)old_selected, (void *)new_selected,
1264 (void *)old_fib, (void *)new_fib);
1265 }
1266
1267 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1268 * fib == selected */
1269 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1270 ROUTE_ENTRY_CHANGED);
1271
1272 /* Update SELECTED entry */
1273 if (old_selected != new_selected || selected_changed) {
1274
1275 if (new_selected && new_selected != new_fib)
1276 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
1277
1278 if (new_selected)
1279 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1280
1281 if (old_selected) {
1282 /*
1283 * If we're removing the old entry, we should tell
1284 * redist subscribers about that *if* they aren't
1285 * going to see a redist for the new entry.
1286 */
1287 if (!new_selected || CHECK_FLAG(old_selected->status,
1288 ROUTE_ENTRY_REMOVED))
1289 redistribute_delete(rn, old_selected,
1290 new_selected);
1291
1292 if (old_selected != new_selected)
1293 UNSET_FLAG(old_selected->flags,
1294 ZEBRA_FLAG_SELECTED);
1295 }
1296 }
1297
1298 /* Update fib according to selection results */
1299 if (new_fib && old_fib)
1300 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1301 else if (new_fib)
1302 rib_process_add_fib(zvrf, rn, new_fib);
1303 else if (old_fib)
1304 rib_process_del_fib(zvrf, rn, old_fib);
1305
1306 /* Remove all RE entries queued for removal */
1307 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1308 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1309 if (IS_ZEBRA_DEBUG_RIB) {
1310 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1311 (void *)rn, (void *)re);
1312 }
1313 rib_unlink(rn, re);
1314 }
1315 }
1316
1317 /*
1318 * Check if the dest can be deleted now.
1319 */
1320 rib_gc_dest(rn);
1321 }
1322
1323 static void zebra_rib_evaluate_mpls(struct route_node *rn)
1324 {
1325 rib_dest_t *dest = rib_dest_from_rnode(rn);
1326 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
1327
1328 if (!dest)
1329 return;
1330
1331 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1332 if (IS_ZEBRA_DEBUG_MPLS)
1333 zlog_debug(
1334 "%s(%u): Scheduling all LSPs upon RIB completion",
1335 zvrf_name(zvrf), zvrf_id(zvrf));
1336 zebra_mpls_lsp_schedule(zvrf);
1337 mpls_unmark_lsps_for_processing(rn);
1338 }
1339 }
1340
1341 /*
1342 * Utility to match route with dplane context data
1343 */
1344 static bool rib_route_match_ctx(const struct route_entry *re,
1345 const struct zebra_dplane_ctx *ctx,
1346 bool is_update)
1347 {
1348 bool result = false;
1349
1350 if (is_update) {
1351 /*
1352 * In 'update' case, we test info about the 'previous' or
1353 * 'old' route
1354 */
1355 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1356 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1357 result = true;
1358
1359 /* We use an extra test for statics, and another for
1360 * kernel routes.
1361 */
1362 if (re->type == ZEBRA_ROUTE_STATIC &&
1363 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1364 re->tag != dplane_ctx_get_old_tag(ctx))) {
1365 result = false;
1366 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1367 re->metric !=
1368 dplane_ctx_get_old_metric(ctx)) {
1369 result = false;
1370 }
1371 }
1372
1373 } else {
1374 /*
1375 * Ordinary, single-route case using primary context info
1376 */
1377 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1378 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1379 /* Skip route that's been deleted */
1380 goto done;
1381 }
1382
1383 if ((re->type == dplane_ctx_get_type(ctx)) &&
1384 (re->instance == dplane_ctx_get_instance(ctx))) {
1385 result = true;
1386
1387 /* We use an extra test for statics, and another for
1388 * kernel routes.
1389 */
1390 if (re->type == ZEBRA_ROUTE_STATIC &&
1391 (re->distance != dplane_ctx_get_distance(ctx) ||
1392 re->tag != dplane_ctx_get_tag(ctx))) {
1393 result = false;
1394 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1395 re->metric != dplane_ctx_get_metric(ctx)) {
1396 result = false;
1397 } else if (re->type == ZEBRA_ROUTE_CONNECT) {
1398 result = nexthop_group_equal_no_recurse(
1399 &re->nhe->nhg, dplane_ctx_get_ng(ctx));
1400 }
1401 }
1402 }
1403
1404 done:
1405
1406 return (result);
1407 }
1408
1409 static void zebra_rib_fixup_system(struct route_node *rn)
1410 {
1411 struct route_entry *re;
1412
1413 RNODE_FOREACH_RE(rn, re) {
1414 struct nexthop *nhop;
1415
1416 if (!RIB_SYSTEM_ROUTE(re))
1417 continue;
1418
1419 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1420 continue;
1421
1422 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1423 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1424
1425 for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) {
1426 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1427 continue;
1428
1429 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1430 }
1431 }
1432 }
1433
1434 /* Route comparison logic, with various special cases. */
1435 static bool rib_compare_routes(const struct route_entry *re1,
1436 const struct route_entry *re2)
1437 {
1438 if (re1->type != re2->type)
1439 return false;
1440
1441 if (re1->instance != re2->instance)
1442 return false;
1443
1444 if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric)
1445 return false;
1446
1447 if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
1448 re1->distance != re2->distance)
1449 return false;
1450
1451 /* We support multiple connected routes: this supports multiple
1452 * v6 link-locals, and we also support multiple addresses in the same
1453 * subnet on a single interface.
1454 */
1455 if (re1->type != ZEBRA_ROUTE_CONNECT)
1456 return true;
1457
1458 return false;
1459 }
1460
1461 /*
1462 * Compare nexthop lists from a route and a dplane context; test whether
1463 * the list installed in the FIB matches the route's list.
1464 * Set 'changed_p' to 'true' if there were changes to the route's
1465 * installed nexthops.
1466 *
1467 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1468 * list.
1469 */
1470 static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg,
1471 const struct nexthop_group *ctx_nhg,
1472 bool *changed_p)
1473 {
1474 bool matched_p = true;
1475 struct nexthop *nexthop, *ctx_nexthop;
1476
1477 /* Get the first `installed` one to check against.
1478 * If the dataplane doesn't set these to be what was actually installed,
1479 * it will just be whatever was in re->nhe->nhg?
1480 */
1481 ctx_nexthop = ctx_nhg->nexthop;
1482
1483 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
1484 || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1485 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1486
1487 for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) {
1488
1489 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1490 continue;
1491
1492 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1493 continue;
1494
1495 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1496 if (!nexthop_same(ctx_nexthop, nexthop)) {
1497 /* If the FIB doesn't know about the nexthop,
1498 * it's not installed
1499 */
1500 if (IS_ZEBRA_DEBUG_RIB_DETAILED ||
1501 IS_ZEBRA_DEBUG_NHG_DETAIL) {
1502 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1503 __func__, nexthop,
1504 (CHECK_FLAG(nexthop->flags,
1505 NEXTHOP_FLAG_FIB) ?
1506 "(FIB)":""));
1507 }
1508 matched_p = false;
1509
1510 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1511 *changed_p = true;
1512
1513 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1514
1515 /* Keep checking nexthops */
1516 continue;
1517 }
1518
1519 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
1520 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1521 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1522 zlog_debug("%s: rib nh %pNHv -> installed",
1523 __func__, nexthop);
1524
1525 *changed_p = true;
1526 }
1527
1528 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1529 } else {
1530 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1531 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1532 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1533 __func__, nexthop);
1534
1535 *changed_p = true;
1536 }
1537
1538 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1539 }
1540
1541 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1542 }
1543
1544 return matched_p;
1545 }
1546
1547 /*
1548 * Update a route from a dplane context. This consolidates common code
1549 * that can be used in processing of results from FIB updates, and in
1550 * async notification processing.
1551 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1552 */
1553 static bool rib_update_re_from_ctx(struct route_entry *re,
1554 struct route_node *rn,
1555 struct zebra_dplane_ctx *ctx)
1556 {
1557 struct nexthop *nexthop;
1558 bool matched;
1559 const struct nexthop_group *ctxnhg;
1560 struct nexthop_group *re_nhg;
1561 bool is_selected = false; /* Is 're' currently the selected re? */
1562 bool changed_p = false; /* Change to nexthops? */
1563 rib_dest_t *dest;
1564 struct vrf *vrf;
1565
1566 vrf = vrf_lookup_by_id(re->vrf_id);
1567
1568 dest = rib_dest_from_rnode(rn);
1569 if (dest)
1570 is_selected = (re == dest->selected_fib);
1571
1572 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1573 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1574 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1575 (is_selected ? "" : "NOT "), re);
1576
1577 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1578 * If the installed set differs from the set requested by the rib/owner,
1579 * we use the fib-specific nexthop-group to record the actual FIB
1580 * status.
1581 */
1582 matched = false;
1583 ctxnhg = dplane_ctx_get_ng(ctx);
1584
1585 /* Check route's fib group and incoming notif group for equivalence.
1586 *
1587 * Let's assume the nexthops are ordered here to save time.
1588 */
1589 /* TODO -- this isn't testing or comparing the FIB flags; we should
1590 * do a more explicit loop, checking the incoming notification's flags.
1591 */
1592 if (re->fib_ng.nexthop && ctxnhg->nexthop &&
1593 nexthop_group_equal(&re->fib_ng, ctxnhg))
1594 matched = true;
1595
1596 /* If the new FIB set matches the existing FIB set, we're done. */
1597 if (matched) {
1598 if (IS_ZEBRA_DEBUG_RIB)
1599 zlog_debug(
1600 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1601 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1602 goto check_backups;
1603
1604 } else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) {
1605 /*
1606 * Free stale fib list and move on to check the rib nhg.
1607 */
1608 if (IS_ZEBRA_DEBUG_RIB)
1609 zlog_debug(
1610 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1611 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1612 nexthops_free(re->fib_ng.nexthop);
1613 re->fib_ng.nexthop = NULL;
1614
1615 UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1616
1617 /* Note that the installed nexthops have changed */
1618 changed_p = true;
1619 } else {
1620 if (IS_ZEBRA_DEBUG_RIB)
1621 zlog_debug(
1622 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1623 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
1624 }
1625
1626 /*
1627 * Compare with the rib nexthop group. The comparison here is different:
1628 * the RIB group may be a superset of the list installed in the FIB. We
1629 * walk the RIB group, looking for the 'installable' candidate
1630 * nexthops, and then check those against the set
1631 * that is actually installed.
1632 *
1633 * Assume nexthops are ordered here as well.
1634 */
1635
1636 /* If nothing is installed, we can skip some of the checking/comparison
1637 * of nexthops.
1638 */
1639 if (ctxnhg->nexthop == NULL) {
1640 changed_p = true;
1641 goto no_nexthops;
1642 }
1643
1644 matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p);
1645
1646 /* If all nexthops were processed, we're done */
1647 if (matched) {
1648 if (IS_ZEBRA_DEBUG_RIB)
1649 zlog_debug(
1650 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1651 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1652 (changed_p ? "true" : "false"));
1653 goto check_backups;
1654 }
1655
1656 no_nexthops:
1657
1658 /* FIB nexthop set differs from the RIB set:
1659 * create a fib-specific nexthop-group
1660 */
1661 if (IS_ZEBRA_DEBUG_RIB)
1662 zlog_debug(
1663 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1664 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1665 (changed_p ? "true" : "false"),
1666 ctxnhg->nexthop != NULL ? "" : " (empty)");
1667
1668 /* Set the flag about the dedicated fib list */
1669 SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1670 if (ctxnhg->nexthop)
1671 copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, NULL);
1672
1673 check_backups:
1674
1675 /*
1676 * Check the status of the route's backup nexthops, if any.
1677 * The logic for backups is somewhat different: if any backup is
1678 * installed, a new fib nhg will be attached to the route.
1679 */
1680 re_nhg = zebra_nhg_get_backup_nhg(re->nhe);
1681 if (re_nhg == NULL)
1682 goto done; /* No backup nexthops */
1683
1684 /* First check the route's 'fib' list of backups, if it's present
1685 * from some previous event.
1686 */
1687 re_nhg = &re->fib_backup_ng;
1688 ctxnhg = dplane_ctx_get_backup_ng(ctx);
1689
1690 matched = false;
1691 if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg))
1692 matched = true;
1693
1694 /* If the new FIB set matches an existing FIB set, we're done. */
1695 if (matched) {
1696 if (IS_ZEBRA_DEBUG_RIB)
1697 zlog_debug(
1698 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1699 VRF_LOGNAME(vrf), re->vrf_id, rn);
1700 goto done;
1701
1702 } else if (re->fib_backup_ng.nexthop) {
1703 /*
1704 * Free stale fib backup list and move on to check
1705 * the route's backups.
1706 */
1707 if (IS_ZEBRA_DEBUG_RIB)
1708 zlog_debug(
1709 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1710 VRF_LOGNAME(vrf), re->vrf_id, rn);
1711 nexthops_free(re->fib_backup_ng.nexthop);
1712 re->fib_backup_ng.nexthop = NULL;
1713
1714 /* Note that the installed nexthops have changed */
1715 changed_p = true;
1716 } else {
1717 if (IS_ZEBRA_DEBUG_RIB)
1718 zlog_debug(
1719 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1720 VRF_LOGNAME(vrf), re->vrf_id, rn);
1721 }
1722
1723 /*
1724 * If a FIB backup nexthop set exists, attach a copy
1725 * to the route if any backup is installed
1726 */
1727 if (ctxnhg && ctxnhg->nexthop) {
1728
1729 for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) {
1730 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1731 break;
1732 }
1733
1734 /* If no installed backups, we're done */
1735 if (nexthop == NULL)
1736 goto done;
1737
1738 if (IS_ZEBRA_DEBUG_RIB)
1739 zlog_debug(
1740 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1741 VRF_LOGNAME(vrf), re->vrf_id, rn,
1742 (changed_p ? "true" : "false"));
1743
1744 copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop,
1745 NULL);
1746 }
1747
1748 done:
1749
1750 return changed_p;
1751 }
1752
1753 /*
1754 * Helper to locate a zebra route-node from a dplane context. This is used
1755 * when processing dplane results, e.g. Note well: the route-node is returned
1756 * with a ref held - route_unlock_node() must be called eventually.
1757 */
1758 struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
1759 {
1760 struct route_table *table = NULL;
1761 struct route_node *rn = NULL;
1762 const struct prefix *dest_pfx, *src_pfx;
1763
1764 /* Locate rn and re(s) from ctx */
1765
1766 table = zebra_vrf_lookup_table_with_table_id(
1767 dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx),
1768 dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx));
1769 if (table == NULL) {
1770 if (IS_ZEBRA_DEBUG_DPLANE) {
1771 zlog_debug(
1772 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1773 dplane_ctx_get_afi(ctx),
1774 dplane_ctx_get_safi(ctx),
1775 vrf_id_to_name(dplane_ctx_get_vrf(ctx)),
1776 dplane_ctx_get_vrf(ctx));
1777 }
1778 goto done;
1779 }
1780
1781 dest_pfx = dplane_ctx_get_dest(ctx);
1782 src_pfx = dplane_ctx_get_src(ctx);
1783
1784 rn = srcdest_rnode_get(table, dest_pfx,
1785 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1786
1787 done:
1788 return rn;
1789 }
1790
1791
1792
1793 /*
1794 * Route-update results processing after async dataplane update.
1795 */
1796 static void rib_process_result(struct zebra_dplane_ctx *ctx)
1797 {
1798 struct zebra_vrf *zvrf = NULL;
1799 struct vrf *vrf;
1800 struct route_node *rn = NULL;
1801 struct route_entry *re = NULL, *old_re = NULL, *rib;
1802 bool is_update = false;
1803 enum dplane_op_e op;
1804 enum zebra_dplane_result status;
1805 uint32_t seq;
1806 rib_dest_t *dest;
1807 bool fib_changed = false;
1808 struct rib_table_info *info;
1809 bool rt_delete = false;
1810
1811 zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
1812 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
1813
1814 /* Locate rn and re(s) from ctx */
1815 rn = rib_find_rn_from_ctx(ctx);
1816 if (rn == NULL) {
1817 if (IS_ZEBRA_DEBUG_DPLANE) {
1818 zlog_debug(
1819 "Failed to process dplane results: no route for %s(%u):%pRN",
1820 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn);
1821 }
1822 goto done;
1823 }
1824
1825 dest = rib_dest_from_rnode(rn);
1826 info = srcdest_rnode_table_info(rn);
1827
1828 op = dplane_ctx_get_op(ctx);
1829 status = dplane_ctx_get_status(ctx);
1830
1831 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1832 zlog_debug(
1833 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1834 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
1835 dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op),
1836 dplane_res2str(status));
1837
1838 /*
1839 * Update is a bit of a special case, where we may have both old and new
1840 * routes to post-process.
1841 */
1842 is_update = dplane_ctx_is_update(ctx);
1843
1844 /*
1845 * Take a pass through the routes, look for matches with the context
1846 * info.
1847 */
1848 RNODE_FOREACH_RE(rn, rib) {
1849
1850 if (re == NULL) {
1851 if (rib_route_match_ctx(rib, ctx, false))
1852 re = rib;
1853 }
1854
1855 /* Check for old route match */
1856 if (is_update && (old_re == NULL)) {
1857 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
1858 old_re = rib;
1859 }
1860
1861 /* Have we found the routes we need to work on? */
1862 if (re && ((!is_update || old_re)))
1863 break;
1864 }
1865
1866 seq = dplane_ctx_get_seq(ctx);
1867
1868 /*
1869 * Check sequence number(s) to detect stale results before continuing
1870 */
1871 if (re) {
1872 if (re->dplane_sequence != seq) {
1873 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1874 zlog_debug(
1875 "%s(%u):%pRN Stale dplane result for re %p",
1876 VRF_LOGNAME(vrf),
1877 dplane_ctx_get_vrf(ctx), rn, re);
1878 } else {
1879 if (!zrouter.asic_offloaded ||
1880 (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) ||
1881 CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED)))
1882 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1883 }
1884 }
1885
1886 if (old_re) {
1887 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
1888 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1889 zlog_debug(
1890 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
1891 VRF_LOGNAME(vrf),
1892 dplane_ctx_get_vrf(ctx), old_re->table,
1893 rn, old_re);
1894 } else
1895 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
1896 }
1897
1898 switch (op) {
1899 case DPLANE_OP_ROUTE_INSTALL:
1900 case DPLANE_OP_ROUTE_UPDATE:
1901 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
1902 if (re) {
1903 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
1904 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1905 }
1906 /*
1907 * On an update operation from the same route type
1908 * context retrieval currently has no way to know
1909 * which was the old and which was the new.
1910 * So don't unset our flags that we just set.
1911 * We know redistribution is ok because the
1912 * old_re in this case is used for nothing
1913 * more than knowing whom to contact if necessary.
1914 */
1915 if (old_re && old_re != re) {
1916 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
1917 UNSET_FLAG(old_re->status,
1918 ROUTE_ENTRY_INSTALLED);
1919 }
1920
1921 /* Update zebra route based on the results in
1922 * the context struct.
1923 */
1924 if (re) {
1925 fib_changed =
1926 rib_update_re_from_ctx(re, rn, ctx);
1927
1928 if (!fib_changed) {
1929 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1930 zlog_debug(
1931 "%s(%u:%u):%pRN no fib change for re",
1932 VRF_LOGNAME(vrf),
1933 dplane_ctx_get_vrf(ctx),
1934 dplane_ctx_get_table(
1935 ctx),
1936 rn);
1937 }
1938
1939 /* Redistribute if this is the selected re */
1940 if (dest && re == dest->selected_fib)
1941 redistribute_update(rn, re, old_re);
1942 }
1943
1944 /*
1945 * System routes are weird in that they
1946 * allow multiple to be installed that match
1947 * to the same prefix, so after we get the
1948 * result we need to clean them up so that
1949 * we can actually use them.
1950 */
1951 if ((re && RIB_SYSTEM_ROUTE(re)) ||
1952 (old_re && RIB_SYSTEM_ROUTE(old_re)))
1953 zebra_rib_fixup_system(rn);
1954
1955 if (zvrf)
1956 zvrf->installs++;
1957
1958 /* Notify route owner */
1959 if (zebra_router_notify_on_ack())
1960 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
1961 else {
1962 if (re) {
1963 if (CHECK_FLAG(re->flags,
1964 ZEBRA_FLAG_OFFLOADED))
1965 zsend_route_notify_owner_ctx(
1966 ctx,
1967 ZAPI_ROUTE_INSTALLED);
1968 if (CHECK_FLAG(
1969 re->flags,
1970 ZEBRA_FLAG_OFFLOAD_FAILED))
1971 zsend_route_notify_owner_ctx(
1972 ctx,
1973 ZAPI_ROUTE_FAIL_INSTALL);
1974 }
1975 }
1976 } else {
1977 if (re) {
1978 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
1979 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1980 } if (old_re)
1981 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
1982 if (re)
1983 zsend_route_notify_owner(
1984 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
1985 info->afi, info->safi);
1986
1987 zlog_warn("%s(%u:%u):%pRN: Route install failed",
1988 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
1989 dplane_ctx_get_table(ctx), rn);
1990 }
1991 break;
1992 case DPLANE_OP_ROUTE_DELETE:
1993 rt_delete = true;
1994 if (re)
1995 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
1996 /*
1997 * In the delete case, the zebra core datastructs were
1998 * updated (or removed) at the time the delete was issued,
1999 * so we're just notifying the route owner.
2000 */
2001 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
2002 if (re) {
2003 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2004 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2005 }
2006 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
2007
2008 if (zvrf)
2009 zvrf->removals++;
2010 } else {
2011 if (re)
2012 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2013 zsend_route_notify_owner_ctx(ctx,
2014 ZAPI_ROUTE_REMOVE_FAIL);
2015
2016 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2017 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2018 dplane_ctx_get_table(ctx), rn);
2019 }
2020
2021 /*
2022 * System routes are weird in that they
2023 * allow multiple to be installed that match
2024 * to the same prefix, so after we get the
2025 * result we need to clean them up so that
2026 * we can actually use them.
2027 */
2028 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2029 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2030 zebra_rib_fixup_system(rn);
2031 break;
2032 default:
2033 break;
2034 }
2035
2036 zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete);
2037 zebra_rib_evaluate_mpls(rn);
2038 done:
2039
2040 if (rn)
2041 route_unlock_node(rn);
2042
2043 /* Return context to dataplane module */
2044 dplane_ctx_fini(&ctx);
2045 }
2046
2047 /*
2048 * Count installed/FIB nexthops
2049 */
2050 static int rib_count_installed_nh(struct route_entry *re)
2051 {
2052 int count = 0;
2053 struct nexthop *nexthop;
2054 struct nexthop_group *nhg;
2055
2056 nhg = rib_get_fib_nhg(re);
2057
2058 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2059 /* The meaningful flag depends on where the installed
2060 * nexthops reside.
2061 */
2062 if (nhg == &(re->fib_ng)) {
2063 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2064 count++;
2065 } else {
2066 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2067 count++;
2068 }
2069 }
2070
2071 nhg = rib_get_fib_backup_nhg(re);
2072 if (nhg) {
2073 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2074 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2075 count++;
2076 }
2077 }
2078
2079 return count;
2080 }
2081
2082 /*
2083 * Handle notification from async dataplane: the dataplane has detected
2084 * some change to a route, and notifies zebra so that the control plane
2085 * can reflect that change.
2086 */
2087 static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2088 {
2089 struct route_node *rn = NULL;
2090 struct route_entry *re = NULL;
2091 struct vrf *vrf;
2092 struct nexthop *nexthop;
2093 rib_dest_t *dest;
2094 bool fib_changed = false;
2095 bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
2096 int start_count, end_count;
2097
2098 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
2099
2100 /* Locate rn and re(s) from ctx */
2101 rn = rib_find_rn_from_ctx(ctx);
2102 if (rn == NULL) {
2103 if (debug_p) {
2104 zlog_debug(
2105 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2106 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2107 dplane_ctx_get_table(ctx), rn);
2108 }
2109 goto done;
2110 }
2111
2112 dest = rib_dest_from_rnode(rn);
2113
2114 if (debug_p)
2115 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2116 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2117 dplane_ctx_get_table(ctx), rn, ctx);
2118
2119 /*
2120 * Take a pass through the routes, look for matches with the context
2121 * info.
2122 */
2123 RNODE_FOREACH_RE(rn, re) {
2124 if (rib_route_match_ctx(re, ctx, false /*!update*/))
2125 break;
2126 }
2127
2128 /* No match? Nothing we can do */
2129 if (re == NULL) {
2130 if (debug_p)
2131 zlog_debug(
2132 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2133 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2134 dplane_ctx_get_table(ctx), rn,
2135 zebra_route_string(dplane_ctx_get_type(ctx)));
2136
2137 goto done;
2138 }
2139
2140 /* Ensure we clear the QUEUED flag */
2141 if (!zrouter.asic_offloaded)
2142 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
2143
2144 /* Is this a notification that ... matters? We mostly care about
2145 * the route that is currently selected for installation; we may also
2146 * get an un-install notification, and handle that too.
2147 */
2148 if (re != dest->selected_fib) {
2149 /*
2150 * If we need to, clean up after a delete that was part of
2151 * an update operation.
2152 */
2153 end_count = 0;
2154 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2155 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2156 end_count++;
2157 }
2158
2159 /* If no nexthops or none installed, ensure that this re
2160 * gets its 'installed' flag cleared.
2161 */
2162 if (end_count == 0) {
2163 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2164 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2165 if (debug_p)
2166 zlog_debug(
2167 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2168 VRF_LOGNAME(vrf),
2169 dplane_ctx_get_vrf(ctx),
2170 dplane_ctx_get_table(ctx), rn,
2171 zebra_route_string(
2172 dplane_ctx_get_type(ctx)));
2173 } else {
2174 /* At least report on the event. */
2175 if (debug_p)
2176 zlog_debug(
2177 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2178 VRF_LOGNAME(vrf),
2179 dplane_ctx_get_vrf(ctx),
2180 dplane_ctx_get_table(ctx), rn,
2181 zebra_route_string(
2182 dplane_ctx_get_type(ctx)));
2183 }
2184 goto done;
2185 }
2186
2187 /* We'll want to determine whether the installation status of the
2188 * route has changed: we'll check the status before processing,
2189 * and then again if there's been a change.
2190 */
2191 start_count = 0;
2192
2193 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2194 start_count = rib_count_installed_nh(re);
2195
2196 /* Update zebra's nexthop FIB flags based on the context struct's
2197 * nexthops.
2198 */
2199 fib_changed = rib_update_re_from_ctx(re, rn, ctx);
2200
2201 if (!fib_changed) {
2202 if (debug_p)
2203 zlog_debug(
2204 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2205 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2206 dplane_ctx_get_table(ctx), rn);
2207 }
2208
2209 /*
2210 * Perform follow-up work if the actual status of the prefix
2211 * changed.
2212 */
2213 end_count = rib_count_installed_nh(re);
2214
2215 /* Various fib transitions: changed nexthops; from installed to
2216 * not-installed; or not-installed to installed.
2217 */
2218 if (start_count > 0 && end_count > 0) {
2219 if (debug_p)
2220 zlog_debug(
2221 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2222 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2223 dplane_ctx_get_table(ctx), rn);
2224
2225 /* Changed nexthops - update kernel/others */
2226 dplane_route_notif_update(rn, re,
2227 DPLANE_OP_ROUTE_UPDATE, ctx);
2228
2229 } else if (start_count == 0 && end_count > 0) {
2230 if (debug_p)
2231 zlog_debug(
2232 "%s(%u:%u):%pRN installed transition from dplane notification",
2233 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2234 dplane_ctx_get_table(ctx), rn);
2235
2236 /* We expect this to be the selected route, so we want
2237 * to tell others about this transition.
2238 */
2239 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2240
2241 /* Changed nexthops - update kernel/others */
2242 dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_UPDATE, ctx);
2243
2244 /* Redistribute, lsp, and nht update */
2245 redistribute_update(rn, re, NULL);
2246
2247 } else if (start_count > 0 && end_count == 0) {
2248 if (debug_p)
2249 zlog_debug(
2250 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2251 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
2252 dplane_ctx_get_table(ctx), rn);
2253
2254 /* Transition from _something_ installed to _nothing_
2255 * installed.
2256 */
2257 /* We expect this to be the selected route, so we want
2258 * to tell others about this transistion.
2259 */
2260 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2261
2262 /* Changed nexthops - update kernel/others */
2263 dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_DELETE, ctx);
2264
2265 /* Redistribute, lsp, and nht update */
2266 redistribute_delete(rn, re, NULL);
2267 }
2268
2269 /* Make any changes visible for lsp and nexthop-tracking processing */
2270 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
2271 false);
2272
2273 zebra_rib_evaluate_mpls(rn);
2274
2275 done:
2276 if (rn)
2277 route_unlock_node(rn);
2278
2279 /* Return context to dataplane module */
2280 dplane_ctx_fini(&ctx);
2281 }
2282
2283 /*
2284 * Process a node from the EVPN/VXLAN subqueue.
2285 */
2286 static void process_subq_evpn(struct listnode *lnode)
2287 {
2288 struct wq_evpn_wrapper *w;
2289
2290 /* In general, the list node points to a wrapper object
2291 * holding the info necessary to make some update.
2292 */
2293 w = listgetdata(lnode);
2294 if (!w)
2295 return;
2296
2297 if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) {
2298 if (w->add_p)
2299 zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr,
2300 &w->ip, &w->prefix);
2301 else
2302 zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip,
2303 &w->prefix);
2304 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) {
2305 if (w->add_p)
2306 zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4,
2307 w->esr_rxed, w->df_alg,
2308 w->df_pref);
2309 else
2310 zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4);
2311 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) {
2312 uint16_t ipa_len = 0;
2313
2314 if (w->ip.ipa_type == IPADDR_V4)
2315 ipa_len = IPV4_MAX_BYTELEN;
2316 else if (w->ip.ipa_type == IPADDR_V6)
2317 ipa_len = IPV6_MAX_BYTELEN;
2318
2319 if (w->add_p)
2320 zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len,
2321 &w->ip, w->flags, w->seq,
2322 w->vtep_ip, &w->esi);
2323 else
2324 zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len,
2325 &w->ip, w->vtep_ip);
2326 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) {
2327 if (w->add_p)
2328 zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni,
2329 w->vtep_ip, w->flags);
2330 else
2331 zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni,
2332 w->vtep_ip);
2333 }
2334
2335
2336 XFREE(MTYPE_WQ_WRAPPER, w);
2337 }
2338
2339 /*
2340 * Process the nexthop-group workqueue subqueue
2341 */
2342 static void process_subq_nhg(struct listnode *lnode)
2343 {
2344 struct nhg_ctx *ctx;
2345 struct nhg_hash_entry *nhe, *newnhe;
2346 struct wq_nhg_wrapper *w;
2347 uint8_t qindex = route_info[ZEBRA_ROUTE_NHG].meta_q_map;
2348
2349 w = listgetdata(lnode);
2350
2351 if (!w)
2352 return;
2353
2354 /* Two types of object - an update from the local kernel, or
2355 * an nhg update from a daemon.
2356 */
2357 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) {
2358 ctx = w->u.ctx;
2359
2360 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2361 zlog_debug(
2362 "NHG Context id=%u dequeued from sub-queue %u",
2363 ctx->id, qindex);
2364
2365
2366 /* Process nexthop group updates coming 'up' from the OS */
2367 nhg_ctx_process(ctx);
2368
2369 } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) {
2370 nhe = w->u.nhe;
2371
2372 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2373 zlog_debug("NHG %u dequeued from sub-queue %u",
2374 nhe->id, qindex);
2375
2376 /* Process incoming nhg update, probably from a proto daemon */
2377 newnhe = zebra_nhg_proto_add(nhe->id, nhe->type,
2378 nhe->zapi_instance,
2379 nhe->zapi_session, &nhe->nhg, 0);
2380
2381 /* Report error to daemon via ZAPI */
2382 if (newnhe == NULL)
2383 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2384 nhe->zapi_session, nhe->id,
2385 ZAPI_NHG_FAIL_INSTALL);
2386
2387 /* Free temp nhe - we own that memory. */
2388 zebra_nhg_free(nhe);
2389 }
2390
2391 XFREE(MTYPE_WQ_WRAPPER, w);
2392 }
2393
2394 static void process_subq_route(struct listnode *lnode, uint8_t qindex)
2395 {
2396 struct route_node *rnode = NULL;
2397 rib_dest_t *dest = NULL;
2398 struct zebra_vrf *zvrf = NULL;
2399
2400 rnode = listgetdata(lnode);
2401 dest = rib_dest_from_rnode(rnode);
2402 assert(dest);
2403
2404 zvrf = rib_dest_vrf(dest);
2405
2406 rib_process(rnode);
2407
2408 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2409 struct route_entry *re = NULL;
2410
2411 /*
2412 * rib_process may have freed the dest
2413 * as part of the garbage collection. Let's
2414 * prevent stupidity from happening.
2415 */
2416 dest = rib_dest_from_rnode(rnode);
2417 if (dest)
2418 re = re_list_first(&dest->routes);
2419
2420 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %u",
2421 zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0,
2422 rnode, rnode, qindex);
2423 }
2424
2425 if (rnode->info)
2426 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2427 RIB_ROUTE_QUEUED(qindex));
2428
2429 route_unlock_node(rnode);
2430 }
2431
2432 /*
2433 * Examine the specified subqueue; process one entry and return 1 if
2434 * there is a node, return 0 otherwise.
2435 */
2436 static unsigned int process_subq(struct list *subq, uint8_t qindex)
2437 {
2438 struct listnode *lnode = listhead(subq);
2439
2440 if (!lnode)
2441 return 0;
2442
2443 if (qindex == META_QUEUE_EVPN)
2444 process_subq_evpn(lnode);
2445 else if (qindex == route_info[ZEBRA_ROUTE_NHG].meta_q_map)
2446 process_subq_nhg(lnode);
2447 else
2448 process_subq_route(lnode, qindex);
2449
2450 list_delete_node(subq, lnode);
2451
2452 return 1;
2453 }
2454
2455 /* Dispatch the meta queue by picking and processing the next node from
2456 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2457 * data is pointed to the meta queue structure.
2458 */
2459 static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
2460 {
2461 struct meta_queue *mq = data;
2462 unsigned i;
2463 uint32_t queue_len, queue_limit;
2464
2465 /* Ensure there's room for more dataplane updates */
2466 queue_limit = dplane_get_in_queue_limit();
2467 queue_len = dplane_get_in_queue_len();
2468 if (queue_len > queue_limit) {
2469 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2470 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2471 queue_len, queue_limit);
2472
2473 /* Ensure that the meta-queue is actually enqueued */
2474 if (work_queue_empty(zrouter.ribq))
2475 work_queue_add(zrouter.ribq, zrouter.mq);
2476
2477 return WQ_QUEUE_BLOCKED;
2478 }
2479
2480 for (i = 0; i < MQ_SIZE; i++)
2481 if (process_subq(mq->subq[i], i)) {
2482 mq->size--;
2483 break;
2484 }
2485 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
2486 }
2487
2488
2489 /*
2490 * Look into the RN and queue it into the highest priority queue
2491 * at this point in time for processing.
2492 *
2493 * We will enqueue a route node only once per invocation.
2494 *
2495 * There are two possibilities here that should be kept in mind.
2496 * If the original invocation has not been pulled off for processing
2497 * yet, A subsuquent invocation can have a route entry with a better
2498 * meta queue index value and we can have a situation where
2499 * we might have the same node enqueued 2 times. Not necessarily
2500 * an optimal situation but it should be ok.
2501 *
2502 * The other possibility is that the original invocation has not
2503 * been pulled off for processing yet, A subsusquent invocation
2504 * doesn't have a route_entry with a better meta-queue and the
2505 * original metaqueue index value will win and we'll end up with
2506 * the route node enqueued once.
2507 */
2508 static int rib_meta_queue_add(struct meta_queue *mq, void *data)
2509 {
2510 struct route_node *rn = NULL;
2511 struct route_entry *re = NULL, *curr_re = NULL;
2512 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
2513
2514 rn = (struct route_node *)data;
2515
2516 RNODE_FOREACH_RE (rn, curr_re) {
2517 curr_qindex = route_info[curr_re->type].meta_q_map;
2518
2519 if (curr_qindex <= qindex) {
2520 re = curr_re;
2521 qindex = curr_qindex;
2522 }
2523 }
2524
2525 if (!re)
2526 return -1;
2527
2528 /* Invariant: at this point we always have rn->info set. */
2529 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2530 RIB_ROUTE_QUEUED(qindex))) {
2531 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2532 rnode_debug(rn, re->vrf_id,
2533 "rn %p is already queued in sub-queue %u",
2534 (void *)rn, qindex);
2535 return -1;
2536 }
2537
2538 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
2539 listnode_add(mq->subq[qindex], rn);
2540 route_lock_node(rn);
2541 mq->size++;
2542
2543 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2544 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %u",
2545 (void *)rn, qindex);
2546
2547 return 0;
2548 }
2549
2550 static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data)
2551 {
2552 struct nhg_ctx *ctx = NULL;
2553 uint8_t qindex = route_info[ZEBRA_ROUTE_NHG].meta_q_map;
2554 struct wq_nhg_wrapper *w;
2555
2556 ctx = (struct nhg_ctx *)data;
2557
2558 if (!ctx)
2559 return -1;
2560
2561 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
2562
2563 w->type = WQ_NHG_WRAPPER_TYPE_CTX;
2564 w->u.ctx = ctx;
2565
2566 listnode_add(mq->subq[qindex], w);
2567 mq->size++;
2568
2569 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2570 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2571 ctx->id, qindex);
2572
2573 return 0;
2574 }
2575
2576 static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data)
2577 {
2578 struct nhg_hash_entry *nhe = NULL;
2579 uint8_t qindex = route_info[ZEBRA_ROUTE_NHG].meta_q_map;
2580 struct wq_nhg_wrapper *w;
2581
2582 nhe = (struct nhg_hash_entry *)data;
2583
2584 if (!nhe)
2585 return -1;
2586
2587 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
2588
2589 w->type = WQ_NHG_WRAPPER_TYPE_NHG;
2590 w->u.nhe = nhe;
2591
2592 listnode_add(mq->subq[qindex], w);
2593 mq->size++;
2594
2595 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2596 zlog_debug("NHG id=%u queued into sub-queue %u",
2597 nhe->id, qindex);
2598
2599 return 0;
2600 }
2601
2602 static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data)
2603 {
2604 listnode_add(mq->subq[META_QUEUE_EVPN], data);
2605 mq->size++;
2606
2607 return 0;
2608 }
2609
2610 static int mq_add_handler(void *data,
2611 int (*mq_add_func)(struct meta_queue *mq, void *data))
2612 {
2613 if (zrouter.ribq == NULL) {
2614 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
2615 "%s: work_queue does not exist!", __func__);
2616 return -1;
2617 }
2618
2619 /*
2620 * The RIB queue should normally be either empty or holding the only
2621 * work_queue_item element. In the latter case this element would
2622 * hold a pointer to the meta queue structure, which must be used to
2623 * actually queue the route nodes to process. So create the MQ
2624 * holder, if necessary, then push the work into it in any case.
2625 * This semantics was introduced after 0.99.9 release.
2626 */
2627 if (work_queue_empty(zrouter.ribq))
2628 work_queue_add(zrouter.ribq, zrouter.mq);
2629
2630 return mq_add_func(zrouter.mq, data);
2631 }
2632
2633 /* Add route_node to work queue and schedule processing */
2634 int rib_queue_add(struct route_node *rn)
2635 {
2636 assert(rn);
2637
2638 /* Pointless to queue a route_node with no RIB entries to add or remove
2639 */
2640 if (!rnode_to_ribs(rn)) {
2641 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
2642 __func__, (void *)rn, route_node_get_lock_count(rn));
2643 zlog_backtrace(LOG_DEBUG);
2644 return -1;
2645 }
2646
2647 return mq_add_handler(rn, rib_meta_queue_add);
2648 }
2649
2650 /*
2651 * Enqueue incoming nhg info from OS for processing
2652 */
2653 int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx)
2654 {
2655 assert(ctx);
2656
2657 return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add);
2658 }
2659
2660 /*
2661 * Enqueue incoming nhg from proto daemon for processing
2662 */
2663 int rib_queue_nhe_add(struct nhg_hash_entry *nhe)
2664 {
2665 if (nhe == NULL)
2666 return -1;
2667
2668 return mq_add_handler(nhe, rib_meta_queue_nhg_add);
2669 }
2670
2671 /*
2672 * Enqueue evpn route for processing
2673 */
2674 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
2675 const struct ipaddr *vtep_ip,
2676 const struct prefix *host_prefix)
2677 {
2678 struct wq_evpn_wrapper *w;
2679
2680 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2681
2682 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
2683 w->add_p = true;
2684 w->vrf_id = vrf_id;
2685 w->macaddr = *rmac;
2686 w->ip = *vtep_ip;
2687 w->prefix = *host_prefix;
2688
2689 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2690 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
2691 vrf_id, vtep_ip, host_prefix);
2692
2693 return mq_add_handler(w, rib_meta_queue_evpn_add);
2694 }
2695
2696 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
2697 const struct ipaddr *vtep_ip,
2698 const struct prefix *host_prefix)
2699 {
2700 struct wq_evpn_wrapper *w;
2701
2702 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2703
2704 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
2705 w->add_p = false;
2706 w->vrf_id = vrf_id;
2707 w->ip = *vtep_ip;
2708 w->prefix = *host_prefix;
2709
2710 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2711 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
2712 vrf_id, vtep_ip, host_prefix);
2713
2714 return mq_add_handler(w, rib_meta_queue_evpn_add);
2715 }
2716
2717 /* Enqueue EVPN remote ES for processing */
2718 int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
2719 const struct in_addr *vtep_ip,
2720 bool esr_rxed, uint8_t df_alg,
2721 uint16_t df_pref)
2722 {
2723 struct wq_evpn_wrapper *w;
2724 char buf[ESI_STR_LEN];
2725
2726 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2727
2728 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
2729 w->add_p = true;
2730 w->esi = *esi;
2731 w->ip.ipa_type = IPADDR_V4;
2732 w->ip.ipaddr_v4 = *vtep_ip;
2733 w->esr_rxed = esr_rxed;
2734 w->df_alg = df_alg;
2735 w->df_pref = df_pref;
2736
2737 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2738 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
2739 esi_to_str(esi, buf, sizeof(buf)));
2740
2741 return mq_add_handler(w, rib_meta_queue_evpn_add);
2742 }
2743
2744 int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
2745 const struct in_addr *vtep_ip)
2746 {
2747 struct wq_evpn_wrapper *w;
2748 char buf[ESI_STR_LEN];
2749
2750 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2751
2752 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
2753 w->add_p = false;
2754 w->esi = *esi;
2755 w->ip.ipa_type = IPADDR_V4;
2756 w->ip.ipaddr_v4 = *vtep_ip;
2757
2758 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2759 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
2760 esi_to_str(esi, buf, sizeof(buf));
2761 else
2762 strlcpy(buf, "-", sizeof(buf));
2763
2764 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
2765 buf);
2766 }
2767
2768 return mq_add_handler(w, rib_meta_queue_evpn_add);
2769 }
2770
2771 /*
2772 * Enqueue EVPN remote macip update for processing
2773 */
2774 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
2775 const struct ipaddr *ipaddr,
2776 uint8_t flags, uint32_t seq,
2777 struct in_addr vtep_ip, const esi_t *esi)
2778 {
2779 struct wq_evpn_wrapper *w;
2780 char buf[ESI_STR_LEN];
2781
2782 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2783
2784 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
2785 w->add_p = true;
2786 w->vni = vni;
2787 w->macaddr = *macaddr;
2788 w->ip = *ipaddr;
2789 w->flags = flags;
2790 w->seq = seq;
2791 w->vtep_ip = vtep_ip;
2792 w->esi = *esi;
2793
2794 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2795 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
2796 esi_to_str(esi, buf, sizeof(buf));
2797 else
2798 strlcpy(buf, "-", sizeof(buf));
2799
2800 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__,
2801 macaddr, &vtep_ip, buf);
2802 }
2803
2804 return mq_add_handler(w, rib_meta_queue_evpn_add);
2805 }
2806
2807 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
2808 const struct ipaddr *ip,
2809 struct in_addr vtep_ip)
2810 {
2811 struct wq_evpn_wrapper *w;
2812
2813 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2814
2815 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
2816 w->add_p = false;
2817 w->vni = vni;
2818 w->macaddr = *macaddr;
2819 w->ip = *ip;
2820 w->vtep_ip = vtep_ip;
2821
2822 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2823 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__,
2824 macaddr, &vtep_ip);
2825
2826 return mq_add_handler(w, rib_meta_queue_evpn_add);
2827 }
2828
2829 /*
2830 * Enqueue remote VTEP address for processing
2831 */
2832 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
2833 struct in_addr vtep_ip, int flood_control)
2834 {
2835 struct wq_evpn_wrapper *w;
2836
2837 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2838
2839 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
2840 w->add_p = true;
2841 w->vrf_id = vrf_id;
2842 w->vni = vni;
2843 w->vtep_ip = vtep_ip;
2844 w->flags = flood_control;
2845
2846 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2847 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
2848 &vtep_ip);
2849
2850 return mq_add_handler(w, rib_meta_queue_evpn_add);
2851 }
2852
2853 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
2854 struct in_addr vtep_ip)
2855 {
2856 struct wq_evpn_wrapper *w;
2857
2858 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
2859
2860 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
2861 w->add_p = false;
2862 w->vrf_id = vrf_id;
2863 w->vni = vni;
2864 w->vtep_ip = vtep_ip;
2865
2866 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2867 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
2868 &vtep_ip);
2869
2870 return mq_add_handler(w, rib_meta_queue_evpn_add);
2871 }
2872
2873 /* Clean up the EVPN meta-queue list */
2874 static void evpn_meta_queue_free(struct list *l)
2875 {
2876 struct listnode *node;
2877 struct wq_evpn_wrapper *w;
2878
2879 /* Free the node wrapper object, and the struct it wraps */
2880 while ((node = listhead(l)) != NULL) {
2881 w = node->data;
2882 node->data = NULL;
2883
2884 XFREE(MTYPE_WQ_WRAPPER, w);
2885
2886 list_delete_node(l, node);
2887 }
2888 }
2889
2890 /* Clean up the nhg meta-queue list */
2891 static void nhg_meta_queue_free(struct list *l)
2892 {
2893 struct wq_nhg_wrapper *w;
2894 struct listnode *node;
2895
2896 /* Free the node wrapper object, and the struct it wraps */
2897 while ((node = listhead(l)) != NULL) {
2898 w = node->data;
2899 node->data = NULL;
2900
2901 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX)
2902 nhg_ctx_free(&w->u.ctx);
2903 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG)
2904 zebra_nhg_free(w->u.nhe);
2905
2906 XFREE(MTYPE_WQ_WRAPPER, w);
2907
2908 list_delete_node(l, node);
2909 }
2910 }
2911
2912 /* Create new meta queue.
2913 A destructor function doesn't seem to be necessary here.
2914 */
2915 static struct meta_queue *meta_queue_new(void)
2916 {
2917 struct meta_queue *new;
2918 unsigned i;
2919
2920 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
2921
2922 for (i = 0; i < MQ_SIZE; i++) {
2923 new->subq[i] = list_new();
2924 assert(new->subq[i]);
2925 }
2926
2927 return new;
2928 }
2929
2930 void meta_queue_free(struct meta_queue *mq)
2931 {
2932 unsigned i;
2933
2934 for (i = 0; i < MQ_SIZE; i++) {
2935 /* Some subqueues may need cleanup - nhgs for example */
2936 if (i == route_info[ZEBRA_ROUTE_NHG].meta_q_map)
2937 nhg_meta_queue_free(mq->subq[i]);
2938 else if (i == META_QUEUE_EVPN)
2939 evpn_meta_queue_free(mq->subq[i]);
2940
2941 list_delete(&mq->subq[i]);
2942 }
2943
2944 XFREE(MTYPE_WORK_QUEUE, mq);
2945 }
2946
2947 void rib_meta_queue_free_vrf(struct meta_queue *mq, struct zebra_vrf *zvrf)
2948 {
2949 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
2950 unsigned int i;
2951
2952 for (i = 0; i < MQ_SIZE; i++) {
2953 struct listnode *lnode, *nnode;
2954 void *data;
2955 bool del;
2956
2957 for (ALL_LIST_ELEMENTS(mq->subq[i], lnode, nnode, data)) {
2958 del = false;
2959
2960 if (i == META_QUEUE_EVPN) {
2961 struct wq_evpn_wrapper *w = data;
2962
2963 if (w->vrf_id == vrf_id) {
2964 XFREE(MTYPE_WQ_WRAPPER, w);
2965 del = true;
2966 }
2967 } else if (i ==
2968 route_info[ZEBRA_ROUTE_NHG].meta_q_map) {
2969 struct wq_nhg_wrapper *w = data;
2970
2971 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX &&
2972 w->u.ctx->vrf_id == vrf_id) {
2973 nhg_ctx_free(&w->u.ctx);
2974 XFREE(MTYPE_WQ_WRAPPER, w);
2975 del = true;
2976 } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG &&
2977 w->u.nhe->vrf_id == vrf_id) {
2978 zebra_nhg_free(w->u.nhe);
2979 XFREE(MTYPE_WQ_WRAPPER, w);
2980 del = true;
2981 }
2982 } else {
2983 struct route_node *rnode = data;
2984 rib_dest_t *dest = rib_dest_from_rnode(rnode);
2985
2986 if (dest && rib_dest_vrf(dest) == zvrf) {
2987 route_unlock_node(rnode);
2988 del = true;
2989 }
2990 }
2991
2992 if (del) {
2993 list_delete_node(mq->subq[i], lnode);
2994 mq->size--;
2995 }
2996 }
2997 }
2998 }
2999
3000 /* initialise zebra rib work queue */
3001 static void rib_queue_init(void)
3002 {
3003 if (!(zrouter.ribq = work_queue_new(zrouter.master,
3004 "route_node processing"))) {
3005 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3006 "%s: could not initialise work queue!", __func__);
3007 return;
3008 }
3009
3010 /* fill in the work queue spec */
3011 zrouter.ribq->spec.workfunc = &meta_queue_process;
3012 zrouter.ribq->spec.errorfunc = NULL;
3013 zrouter.ribq->spec.completion_func = NULL;
3014 /* XXX: TODO: These should be runtime configurable via vty */
3015 zrouter.ribq->spec.max_retries = 3;
3016 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
3017 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
3018
3019 if (!(zrouter.mq = meta_queue_new())) {
3020 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
3021 "%s: could not initialise meta queue!", __func__);
3022 return;
3023 }
3024 return;
3025 }
3026
3027 rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
3028 {
3029 rib_dest_t *dest;
3030
3031 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
3032 rnh_list_init(&dest->nht);
3033 re_list_init(&dest->routes);
3034 route_lock_node(rn); /* rn route table reference */
3035 rn->info = dest;
3036 dest->rnode = rn;
3037
3038 return dest;
3039 }
3040
3041 /* RIB updates are processed via a queue of pointers to route_nodes.
3042 *
3043 * The queue length is bounded by the maximal size of the routing table,
3044 * as a route_node will not be requeued, if already queued.
3045 *
3046 * REs are submitted via rib_addnode or rib_delnode which set minimal
3047 * state, or static_install_route (when an existing RE is updated)
3048 * and then submit route_node to queue for best-path selection later.
3049 * Order of add/delete state changes are preserved for any given RE.
3050 *
3051 * Deleted REs are reaped during best-path selection.
3052 *
3053 * rib_addnode
3054 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3055 * |-------->| | best RE, if required
3056 * | |
3057 * static_install->|->rib_addqueue...... -> rib_process
3058 * | |
3059 * |-------->| |-> rib_unlink
3060 * |-> set ROUTE_ENTRY_REMOVE |
3061 * rib_delnode (RE freed)
3062 *
3063 * The 'info' pointer of a route_node points to a rib_dest_t
3064 * ('dest'). Queueing state for a route_node is kept on the dest. The
3065 * dest is created on-demand by rib_link() and is kept around at least
3066 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3067 *
3068 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3069 *
3070 * - route_nodes: refcounted by:
3071 * - dest attached to route_node:
3072 * - managed by: rib_link/rib_gc_dest
3073 * - route_node processing queue
3074 * - managed by: rib_addqueue, rib_process.
3075 *
3076 */
3077
3078 /* Add RE to head of the route node. */
3079 static void rib_link(struct route_node *rn, struct route_entry *re, int process)
3080 {
3081 rib_dest_t *dest;
3082 afi_t afi;
3083 const char *rmap_name;
3084
3085 assert(re && rn);
3086
3087 dest = rib_dest_from_rnode(rn);
3088 if (!dest) {
3089 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3090 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
3091
3092 dest = zebra_rib_create_dest(rn);
3093 }
3094
3095 re_list_add_head(&dest->routes, re);
3096
3097 afi = (rn->p.family == AF_INET)
3098 ? AFI_IP
3099 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
3100 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3101 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3102
3103 rmap_name = zebra_get_import_table_route_map(afi, re->table);
3104 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
3105 } else if (process)
3106 rib_queue_add(rn);
3107 }
3108
3109 static void rib_addnode(struct route_node *rn,
3110 struct route_entry *re, int process)
3111 {
3112 /* RE node has been un-removed before route-node is processed.
3113 * route_node must hence already be on the queue for processing..
3114 */
3115 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
3116 if (IS_ZEBRA_DEBUG_RIB)
3117 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
3118 (void *)rn, (void *)re);
3119
3120 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3121 return;
3122 }
3123 rib_link(rn, re, process);
3124 }
3125
3126 static void rib_re_nhg_free(struct route_entry *re)
3127 {
3128 if (re->nhe && re->nhe_id) {
3129 assert(re->nhe->id == re->nhe_id);
3130 route_entry_update_nhe(re, NULL);
3131 } else if (re->nhe && re->nhe->nhg.nexthop)
3132 nexthops_free(re->nhe->nhg.nexthop);
3133
3134 nexthops_free(re->fib_ng.nexthop);
3135 }
3136
3137 /*
3138 * rib_unlink
3139 *
3140 * Detach a rib structure from a route_node.
3141 *
3142 * Note that a call to rib_unlink() should be followed by a call to
3143 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3144 * longer required to be deleted.
3145 */
3146 void rib_unlink(struct route_node *rn, struct route_entry *re)
3147 {
3148 rib_dest_t *dest;
3149
3150 assert(rn && re);
3151
3152 if (IS_ZEBRA_DEBUG_RIB)
3153 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
3154 (void *)re);
3155
3156 dest = rib_dest_from_rnode(rn);
3157
3158 re_list_del(&dest->routes, re);
3159
3160 if (dest->selected_fib == re)
3161 dest->selected_fib = NULL;
3162
3163 rib_re_nhg_free(re);
3164
3165 zapi_re_opaque_free(re->opaque);
3166
3167 XFREE(MTYPE_RE, re);
3168 }
3169
3170 void rib_delnode(struct route_node *rn, struct route_entry *re)
3171 {
3172 afi_t afi;
3173
3174 if (IS_ZEBRA_DEBUG_RIB)
3175 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
3176 (void *)rn, (void *)re);
3177 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3178
3179 afi = (rn->p.family == AF_INET)
3180 ? AFI_IP
3181 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
3182 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3183 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3184
3185 zebra_del_import_table_entry(zvrf, rn, re);
3186 /* Just clean up if non main table */
3187 if (IS_ZEBRA_DEBUG_RIB)
3188 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3189 vrf_id_to_name(re->vrf_id), re->vrf_id, rn,
3190 rn, re, zebra_route_string(re->type));
3191
3192 rib_unlink(rn, re);
3193 } else {
3194 rib_queue_add(rn);
3195 }
3196 }
3197
3198 /*
3199 * Helper that debugs a single nexthop within a route-entry
3200 */
3201 static void _route_entry_dump_nh(const struct route_entry *re,
3202 const char *straddr,
3203 const struct nexthop *nexthop)
3204 {
3205 char nhname[PREFIX_STRLEN];
3206 char backup_str[50];
3207 char wgt_str[50];
3208 char temp_str[10];
3209 char label_str[MPLS_LABEL_STRLEN];
3210 int i;
3211 struct interface *ifp;
3212 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
3213
3214 switch (nexthop->type) {
3215 case NEXTHOP_TYPE_BLACKHOLE:
3216 snprintf(nhname, sizeof(nhname), "Blackhole");
3217 break;
3218 case NEXTHOP_TYPE_IFINDEX:
3219 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
3220 snprintf(nhname, sizeof(nhname), "%s",
3221 ifp ? ifp->name : "Unknown");
3222 break;
3223 case NEXTHOP_TYPE_IPV4:
3224 /* fallthrough */
3225 case NEXTHOP_TYPE_IPV4_IFINDEX:
3226 inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
3227 break;
3228 case NEXTHOP_TYPE_IPV6:
3229 case NEXTHOP_TYPE_IPV6_IFINDEX:
3230 inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
3231 break;
3232 }
3233
3234 /* Label stack */
3235 label_str[0] = '\0';
3236 if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) {
3237 mpls_label2str(nexthop->nh_label->num_labels,
3238 nexthop->nh_label->label, label_str,
3239 sizeof(label_str), 0 /*pretty*/);
3240 strlcat(label_str, ", ", sizeof(label_str));
3241 }
3242
3243 backup_str[0] = '\0';
3244 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
3245 snprintf(backup_str, sizeof(backup_str), "backup ");
3246 for (i = 0; i < nexthop->backup_num; i++) {
3247 snprintf(temp_str, sizeof(temp_str), "%d, ",
3248 nexthop->backup_idx[i]);
3249 strlcat(backup_str, temp_str, sizeof(backup_str));
3250 }
3251 }
3252
3253 wgt_str[0] = '\0';
3254 if (nexthop->weight)
3255 snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight);
3256
3257 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s",
3258 straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
3259 nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown",
3260 nexthop->vrf_id,
3261 wgt_str, backup_str,
3262 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
3263 ? "ACTIVE "
3264 : ""),
3265 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
3266 ? "FIB "
3267 : ""),
3268 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
3269 ? "RECURSIVE "
3270 : ""),
3271 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
3272 ? "ONLINK "
3273 : ""),
3274 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
3275 ? "DUPLICATE "
3276 : ""),
3277 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED)
3278 ? "FILTERED " : ""),
3279 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)
3280 ? "BACKUP " : ""),
3281 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE)
3282 ? "SRTE " : ""));
3283
3284 }
3285
3286 /* This function dumps the contents of a given RE entry into
3287 * standard debug log. Calling function name and IP prefix in
3288 * question are passed as 1st and 2nd arguments.
3289 */
3290 void _route_entry_dump(const char *func, union prefixconstptr pp,
3291 union prefixconstptr src_pp,
3292 const struct route_entry *re)
3293 {
3294 const struct prefix *src_p = src_pp.p;
3295 bool is_srcdst = src_p && src_p->prefixlen;
3296 char straddr[PREFIX_STRLEN];
3297 char srcaddr[PREFIX_STRLEN];
3298 char flags_buf[128];
3299 char status_buf[128];
3300 struct nexthop *nexthop;
3301 struct vrf *vrf = vrf_lookup_by_id(re->vrf_id);
3302 struct nexthop_group *nhg;
3303
3304 prefix2str(pp, straddr, sizeof(straddr));
3305
3306 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func,
3307 (const void *)re, straddr,
3308 is_srcdst ? " from " : "",
3309 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
3310 : "",
3311 VRF_LOGNAME(vrf), re->vrf_id);
3312 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
3313 straddr, (unsigned long)re->uptime, re->type, re->instance,
3314 re->table);
3315 zlog_debug(
3316 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
3317 straddr, re->metric, re->mtu, re->distance,
3318 zclient_dump_route_flags(re->flags, flags_buf,
3319 sizeof(flags_buf)),
3320 _dump_re_status(re, status_buf, sizeof(status_buf)));
3321 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
3322 nexthop_group_nexthop_num(&(re->nhe->nhg)),
3323 nexthop_group_active_nexthop_num(&(re->nhe->nhg)));
3324
3325 /* Dump nexthops */
3326 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
3327 _route_entry_dump_nh(re, straddr, nexthop);
3328
3329 if (zebra_nhg_get_backup_nhg(re->nhe)) {
3330 zlog_debug("%s: backup nexthops:", straddr);
3331
3332 nhg = zebra_nhg_get_backup_nhg(re->nhe);
3333 for (ALL_NEXTHOPS_PTR(nhg, nexthop))
3334 _route_entry_dump_nh(re, straddr, nexthop);
3335 }
3336
3337 zlog_debug("%s: dump complete", straddr);
3338 }
3339
3340 /*
3341 * Internal route-add implementation; there are a couple of different public
3342 * signatures. Callers in this path are responsible for the memory they
3343 * allocate: if they allocate a nexthop_group or backup nexthop info, they
3344 * must free those objects. If this returns < 0, an error has occurred and the
3345 * route_entry 're' has not been captured; the caller should free that also.
3346 *
3347 * -1 -> error
3348 * 0 -> Add
3349 * 1 -> update
3350 */
3351 int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
3352 struct prefix_ipv6 *src_p, struct route_entry *re,
3353 struct nhg_hash_entry *re_nhe, bool startup)
3354 {
3355 struct nhg_hash_entry *nhe = NULL;
3356 struct route_table *table;
3357 struct route_node *rn;
3358 struct route_entry *same = NULL, *first_same = NULL;
3359 int ret = 0;
3360 int same_count = 0;
3361 rib_dest_t *dest;
3362
3363 if (!re || !re_nhe)
3364 return -1;
3365
3366 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
3367
3368 /* Lookup table. */
3369 table = zebra_vrf_get_table_with_table_id(afi, safi, re->vrf_id,
3370 re->table);
3371 if (!table)
3372 return -1;
3373
3374 if (re->nhe_id > 0) {
3375 nhe = zebra_nhg_lookup_id(re->nhe_id);
3376
3377 if (!nhe) {
3378 flog_err(
3379 EC_ZEBRA_TABLE_LOOKUP_FAILED,
3380 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
3381 re->nhe_id);
3382
3383 return -1;
3384 }
3385 } else {
3386 /* Lookup nhe from route information */
3387 nhe = zebra_nhg_rib_find_nhe(re_nhe, afi);
3388 if (!nhe) {
3389 char buf2[PREFIX_STRLEN] = "";
3390
3391 flog_err(
3392 EC_ZEBRA_TABLE_LOOKUP_FAILED,
3393 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
3394 p, src_p ? " from " : "",
3395 src_p ? prefix2str(src_p, buf2, sizeof(buf2))
3396 : "");
3397
3398 return -1;
3399 }
3400 }
3401
3402 /*
3403 * Attach the re to the nhe's nexthop group.
3404 *
3405 * TODO: This will need to change when we start getting IDs from upper
3406 * level protocols, as the refcnt might be wrong, since it checks
3407 * if old_id != new_id.
3408 */
3409 route_entry_update_nhe(re, nhe);
3410
3411 /* Make it sure prefixlen is applied to the prefix. */
3412 apply_mask(p);
3413 if (src_p)
3414 apply_mask_ipv6(src_p);
3415
3416 /* Set default distance by route type. */
3417 if (re->distance == 0)
3418 re->distance = route_distance(re->type);
3419
3420 /* Lookup route node.*/
3421 rn = srcdest_rnode_get(table, p, src_p);
3422
3423 /*
3424 * If same type of route are installed, treat it as a implicit
3425 * withdraw. If the user has specified the No route replace semantics
3426 * for the install don't do a route replace.
3427 */
3428 RNODE_FOREACH_RE (rn, same) {
3429 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) {
3430 same_count++;
3431 continue;
3432 }
3433
3434 /* Compare various route_entry properties */
3435 if (rib_compare_routes(re, same)) {
3436 same_count++;
3437
3438 if (first_same == NULL)
3439 first_same = same;
3440 }
3441 }
3442
3443 same = first_same;
3444
3445 if (!startup &&
3446 (re->flags & ZEBRA_FLAG_SELFROUTE) && zrouter.asic_offloaded) {
3447 if (!same) {
3448 if (IS_ZEBRA_DEBUG_RIB)
3449 zlog_debug("prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless", rn);
3450 /*
3451 * We are not on startup, this is a self route
3452 * and we have asic offload. Which means
3453 * we are getting a callback for a entry
3454 * that was already deleted to the kernel
3455 * but an earlier response was just handed
3456 * back. Drop it on the floor
3457 */
3458 rib_re_nhg_free(re);
3459
3460 XFREE(MTYPE_RE, re);
3461 return ret;
3462 }
3463 }
3464
3465 /* If this route is kernel/connected route, notify the dataplane. */
3466 if (RIB_SYSTEM_ROUTE(re)) {
3467 /* Notify dataplane */
3468 dplane_sys_route_add(rn, re);
3469 }
3470
3471 /* Link new re to node.*/
3472 if (IS_ZEBRA_DEBUG_RIB) {
3473 rnode_debug(rn, re->vrf_id,
3474 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
3475 rn, re, zebra_route_string(re->type), same,
3476 same_count);
3477
3478 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3479 route_entry_dump(p, src_p, re);
3480 }
3481
3482 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
3483 rib_addnode(rn, re, 1);
3484
3485 /* Free implicit route.*/
3486 if (same) {
3487 ret = 1;
3488 rib_delnode(rn, same);
3489 }
3490
3491 /* See if we can remove some RE entries that are queued for
3492 * removal, but won't be considered in rib processing.
3493 */
3494 dest = rib_dest_from_rnode(rn);
3495 RNODE_FOREACH_RE_SAFE (rn, re, same) {
3496 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
3497 /* If the route was used earlier, must retain it. */
3498 if (dest && re == dest->selected_fib)
3499 continue;
3500
3501 if (IS_ZEBRA_DEBUG_RIB)
3502 rnode_debug(rn, re->vrf_id, "rn %p, removing unneeded re %p",
3503 rn, re);
3504
3505 rib_unlink(rn, re);
3506 }
3507 }
3508
3509 route_unlock_node(rn);
3510 return ret;
3511 }
3512
3513 /*
3514 * Add a single route.
3515 */
3516 int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
3517 struct prefix_ipv6 *src_p, struct route_entry *re,
3518 struct nexthop_group *ng, bool startup)
3519 {
3520 int ret;
3521 struct nhg_hash_entry nhe;
3522
3523 if (!re)
3524 return -1;
3525
3526 /* We either need nexthop(s) or an existing nexthop id */
3527 if (ng == NULL && re->nhe_id == 0)
3528 return -1;
3529
3530 /*
3531 * Use a temporary nhe to convey info to the common/main api.
3532 */
3533 zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL));
3534 if (ng)
3535 nhe.nhg.nexthop = ng->nexthop;
3536 else if (re->nhe_id > 0)
3537 nhe.id = re->nhe_id;
3538
3539 ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, &nhe, startup);
3540
3541 /* In this path, the callers expect memory to be freed. */
3542 nexthop_group_delete(&ng);
3543
3544 /* In error cases, free the route also */
3545 if (ret < 0)
3546 XFREE(MTYPE_RE, re);
3547
3548 return ret;
3549 }
3550
3551 void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
3552 unsigned short instance, uint32_t flags, struct prefix *p,
3553 struct prefix_ipv6 *src_p, const struct nexthop *nh,
3554 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
3555 uint8_t distance, bool fromkernel)
3556 {
3557 struct route_table *table;
3558 struct route_node *rn;
3559 struct route_entry *re;
3560 struct route_entry *fib = NULL;
3561 struct route_entry *same = NULL;
3562 struct nexthop *rtnh;
3563 char buf2[INET6_ADDRSTRLEN];
3564 rib_dest_t *dest;
3565
3566 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
3567
3568 /* Lookup table. */
3569 table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id,
3570 table_id);
3571 if (!table)
3572 return;
3573
3574 /* Apply mask. */
3575 apply_mask(p);
3576 if (src_p)
3577 apply_mask_ipv6(src_p);
3578
3579 /* Lookup route node. */
3580 rn = srcdest_rnode_lookup(table, p, src_p);
3581 if (!rn) {
3582 if (IS_ZEBRA_DEBUG_RIB) {
3583 char src_buf[PREFIX_STRLEN];
3584 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
3585
3586 if (src_p && src_p->prefixlen)
3587 prefix2str(src_p, src_buf, sizeof(src_buf));
3588 else
3589 src_buf[0] = '\0';
3590
3591 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
3592 vrf->name, table_id, rn,
3593 (src_buf[0] != '\0') ? " from " : "",
3594 src_buf);
3595 }
3596 return;
3597 }
3598
3599 dest = rib_dest_from_rnode(rn);
3600 fib = dest->selected_fib;
3601
3602 /* Lookup same type route. */
3603 RNODE_FOREACH_RE (rn, re) {
3604 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3605 continue;
3606
3607 if (re->type != type)
3608 continue;
3609 if (re->instance != instance)
3610 continue;
3611 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
3612 distance != re->distance)
3613 continue;
3614
3615 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
3616 continue;
3617 if (re->type == ZEBRA_ROUTE_CONNECT &&
3618 (rtnh = re->nhe->nhg.nexthop)
3619 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
3620 if (rtnh->ifindex != nh->ifindex)
3621 continue;
3622 same = re;
3623 break;
3624 }
3625
3626 /* Make sure that the route found has the same gateway. */
3627 if (nhe_id && re->nhe_id == nhe_id) {
3628 same = re;
3629 break;
3630 }
3631
3632 if (nh == NULL) {
3633 same = re;
3634 break;
3635 }
3636 for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) {
3637 /*
3638 * No guarantee all kernel send nh with labels
3639 * on delete.
3640 */
3641 if (nexthop_same_no_labels(rtnh, nh)) {
3642 same = re;
3643 break;
3644 }
3645 }
3646
3647 if (same)
3648 break;
3649 }
3650 /* If same type of route can't be found and this message is from
3651 kernel. */
3652 if (!same) {
3653 /*
3654 * In the past(HA!) we could get here because
3655 * we were receiving a route delete from the
3656 * kernel and we're not marking the proto
3657 * as coming from it's appropriate originator.
3658 * Now that we are properly noticing the fact
3659 * that the kernel has deleted our route we
3660 * are not going to get called in this path
3661 * I am going to leave this here because
3662 * this might still work this way on non-linux
3663 * platforms as well as some weird state I have
3664 * not properly thought of yet.
3665 * If we can show that this code path is
3666 * dead then we can remove it.
3667 */
3668 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
3669 if (IS_ZEBRA_DEBUG_RIB) {
3670 rnode_debug(rn, vrf_id,
3671 "rn %p, re %p (%s) was deleted from kernel, adding",
3672 rn, fib,
3673 zebra_route_string(fib->type));
3674 }
3675 if (allow_delete
3676 || CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) {
3677 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
3678 /* Unset flags. */
3679 for (rtnh = fib->nhe->nhg.nexthop; rtnh;
3680 rtnh = rtnh->next)
3681 UNSET_FLAG(rtnh->flags,
3682 NEXTHOP_FLAG_FIB);
3683
3684 /*
3685 * This is a non FRR route
3686 * as such we should mark
3687 * it as deleted
3688 */
3689 dest->selected_fib = NULL;
3690 } else {
3691 /* This means someone else, other than Zebra,
3692 * has deleted
3693 * a Zebra router from the kernel. We will add
3694 * it back */
3695 rib_install_kernel(rn, fib, NULL);
3696 }
3697 } else {
3698 if (IS_ZEBRA_DEBUG_RIB) {
3699 if (nh)
3700 rnode_debug(
3701 rn, vrf_id,
3702 "via %s ifindex %d type %d doesn't exist in rib",
3703 inet_ntop(afi2family(afi),
3704 &nh->gate, buf2,
3705 sizeof(buf2)),
3706 nh->ifindex, type);
3707 else
3708 rnode_debug(
3709 rn, vrf_id,
3710 "type %d doesn't exist in rib",
3711 type);
3712 }
3713 route_unlock_node(rn);
3714 return;
3715 }
3716 }
3717
3718 if (same) {
3719 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
3720 && !allow_delete) {
3721 rib_install_kernel(rn, same, NULL);
3722 route_unlock_node(rn);
3723
3724 return;
3725 }
3726
3727 /* Special handling for IPv4 or IPv6 routes sourced from
3728 * EVPN - the nexthop (and associated MAC) need to be
3729 * uninstalled if no more refs.
3730 */
3731 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
3732 struct nexthop *tmp_nh;
3733
3734 for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) {
3735 struct ipaddr vtep_ip;
3736
3737 memset(&vtep_ip, 0, sizeof(struct ipaddr));
3738 if (afi == AFI_IP) {
3739 vtep_ip.ipa_type = IPADDR_V4;
3740 memcpy(&(vtep_ip.ipaddr_v4),
3741 &(tmp_nh->gate.ipv4),
3742 sizeof(struct in_addr));
3743 } else {
3744 vtep_ip.ipa_type = IPADDR_V6;
3745 memcpy(&(vtep_ip.ipaddr_v6),
3746 &(tmp_nh->gate.ipv6),
3747 sizeof(struct in6_addr));
3748 }
3749 zebra_rib_queue_evpn_route_del(re->vrf_id,
3750 &vtep_ip, p);
3751 }
3752 }
3753
3754 /* Notify dplane if system route changes */
3755 if (RIB_SYSTEM_ROUTE(re))
3756 dplane_sys_route_del(rn, same);
3757
3758 rib_delnode(rn, same);
3759 }
3760
3761 route_unlock_node(rn);
3762 return;
3763 }
3764
3765
3766 int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
3767 unsigned short instance, uint32_t flags, struct prefix *p,
3768 struct prefix_ipv6 *src_p, const struct nexthop *nh,
3769 uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu,
3770 uint8_t distance, route_tag_t tag, bool startup)
3771 {
3772 struct route_entry *re = NULL;
3773 struct nexthop *nexthop = NULL;
3774 struct nexthop_group *ng = NULL;
3775
3776 /* Allocate new route_entry structure. */
3777 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
3778 re->type = type;
3779 re->instance = instance;
3780 re->distance = distance;
3781 re->flags = flags;
3782 re->metric = metric;
3783 re->mtu = mtu;
3784 re->table = table_id;
3785 re->vrf_id = vrf_id;
3786 re->uptime = monotime(NULL);
3787 re->tag = tag;
3788 re->nhe_id = nhe_id;
3789
3790 /* If the owner of the route supplies a shared nexthop-group id,
3791 * we'll use that. Otherwise, pass the nexthop along directly.
3792 */
3793 if (!nhe_id) {
3794 ng = nexthop_group_new();
3795
3796 /* Add nexthop. */
3797 nexthop = nexthop_new();
3798 *nexthop = *nh;
3799 nexthop_group_add_sorted(ng, nexthop);
3800 }
3801
3802 return rib_add_multipath(afi, safi, p, src_p, re, ng, startup);
3803 }
3804
3805 static const char *rib_update_event2str(enum rib_update_event event)
3806 {
3807 const char *ret = "UNKNOWN";
3808
3809 switch (event) {
3810 case RIB_UPDATE_KERNEL:
3811 ret = "RIB_UPDATE_KERNEL";
3812 break;
3813 case RIB_UPDATE_RMAP_CHANGE:
3814 ret = "RIB_UPDATE_RMAP_CHANGE";
3815 break;
3816 case RIB_UPDATE_OTHER:
3817 ret = "RIB_UPDATE_OTHER";
3818 break;
3819 case RIB_UPDATE_MAX:
3820 break;
3821 }
3822
3823 return ret;
3824 }
3825
3826
3827 /* Schedule route nodes to be processed if they match the type */
3828 static void rib_update_route_node(struct route_node *rn, int type)
3829 {
3830 struct route_entry *re, *next;
3831 bool re_changed = false;
3832
3833 RNODE_FOREACH_RE_SAFE (rn, re, next) {
3834 if (type == ZEBRA_ROUTE_ALL || type == re->type) {
3835 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
3836 re_changed = true;
3837 }
3838 }
3839
3840 if (re_changed)
3841 rib_queue_add(rn);
3842 }
3843
3844 /* Schedule routes of a particular table (address-family) based on event. */
3845 void rib_update_table(struct route_table *table, enum rib_update_event event,
3846 int rtype)
3847 {
3848 struct route_node *rn;
3849
3850 if (IS_ZEBRA_DEBUG_EVENT) {
3851 struct zebra_vrf *zvrf;
3852 struct vrf *vrf;
3853
3854 zvrf = table->info
3855 ? ((struct rib_table_info *)table->info)->zvrf
3856 : NULL;
3857 vrf = zvrf ? zvrf->vrf : NULL;
3858
3859 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__,
3860 table->info ? afi2str(
3861 ((struct rib_table_info *)table->info)->afi)
3862 : "Unknown",
3863 VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0,
3864 rib_update_event2str(event), zebra_route_string(rtype));
3865 }
3866
3867 /* Walk all routes and queue for processing, if appropriate for
3868 * the trigger event.
3869 */
3870 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
3871 /*
3872 * If we are looking at a route node and the node
3873 * has already been queued we don't
3874 * need to queue it up again
3875 */
3876 if (rn->info
3877 && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3878 RIB_ROUTE_ANY_QUEUED))
3879 continue;
3880
3881 switch (event) {
3882 case RIB_UPDATE_KERNEL:
3883 rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL);
3884 break;
3885 case RIB_UPDATE_RMAP_CHANGE:
3886 case RIB_UPDATE_OTHER:
3887 rib_update_route_node(rn, rtype);
3888 break;
3889 default:
3890 break;
3891 }
3892 }
3893 }
3894
3895 static void rib_update_handle_vrf(vrf_id_t vrf_id, enum rib_update_event event,
3896 int rtype)
3897 {
3898 struct route_table *table;
3899
3900 if (IS_ZEBRA_DEBUG_EVENT)
3901 zlog_debug("%s: Handling VRF %s event %s", __func__,
3902 vrf_id_to_name(vrf_id), rib_update_event2str(event));
3903
3904 /* Process routes of interested address-families. */
3905 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
3906 if (table)
3907 rib_update_table(table, event, rtype);
3908
3909 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
3910 if (table)
3911 rib_update_table(table, event, rtype);
3912 }
3913
3914 static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype)
3915 {
3916 struct zebra_router_table *zrt;
3917
3918 if (IS_ZEBRA_DEBUG_EVENT)
3919 zlog_debug("%s: Handling VRF (ALL) event %s", __func__,
3920 rib_update_event2str(event));
3921
3922 /* Just iterate over all the route tables, rather than vrf lookups */
3923 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables)
3924 rib_update_table(zrt->table, event, rtype);
3925 }
3926
3927 struct rib_update_ctx {
3928 enum rib_update_event event;
3929 bool vrf_all;
3930 vrf_id_t vrf_id;
3931 };
3932
3933 static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id,
3934 enum rib_update_event event)
3935 {
3936 struct rib_update_ctx *ctx;
3937
3938 ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx));
3939
3940 ctx->event = event;
3941 ctx->vrf_id = vrf_id;
3942
3943 return ctx;
3944 }
3945
3946 static void rib_update_ctx_fini(struct rib_update_ctx **ctx)
3947 {
3948 XFREE(MTYPE_RIB_UPDATE_CTX, *ctx);
3949 }
3950
3951 static int rib_update_handler(struct thread *thread)
3952 {
3953 struct rib_update_ctx *ctx;
3954
3955 ctx = THREAD_ARG(thread);
3956
3957 if (ctx->vrf_all)
3958 rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL);
3959 else
3960 rib_update_handle_vrf(ctx->vrf_id, ctx->event, ZEBRA_ROUTE_ALL);
3961
3962 rib_update_ctx_fini(&ctx);
3963
3964 return 0;
3965 }
3966
3967 /*
3968 * Thread list to ensure we don't schedule a ton of events
3969 * if interfaces are flapping for instance.
3970 */
3971 static struct thread *t_rib_update_threads[RIB_UPDATE_MAX];
3972
3973 /* Schedule a RIB update event for all vrfs */
3974 void rib_update(enum rib_update_event event)
3975 {
3976 struct rib_update_ctx *ctx;
3977
3978 if (thread_is_scheduled(t_rib_update_threads[event]))
3979 return;
3980
3981 ctx = rib_update_ctx_init(0, event);
3982 ctx->vrf_all = true;
3983
3984 thread_add_event(zrouter.master, rib_update_handler, ctx, 0,
3985 &t_rib_update_threads[event]);
3986
3987 if (IS_ZEBRA_DEBUG_EVENT)
3988 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__,
3989 rib_update_event2str(event));
3990 }
3991
3992 /* Delete self installed routes after zebra is relaunched. */
3993 void rib_sweep_table(struct route_table *table)
3994 {
3995 struct route_node *rn;
3996 struct route_entry *re;
3997 struct route_entry *next;
3998 struct nexthop *nexthop;
3999
4000 if (!table)
4001 return;
4002
4003 if (IS_ZEBRA_DEBUG_RIB)
4004 zlog_debug("%s: starting", __func__);
4005
4006 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4007 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4008
4009 if (IS_ZEBRA_DEBUG_RIB)
4010 route_entry_dump(&rn->p, NULL, re);
4011
4012 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4013 continue;
4014
4015 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
4016 continue;
4017
4018 /*
4019 * If routes are older than startup_time then
4020 * we know we read them in from the kernel.
4021 * As such we can safely remove them.
4022 */
4023 if (zrouter.startup_time < re->uptime)
4024 continue;
4025
4026 /*
4027 * So we are starting up and have received
4028 * routes from the kernel that we have installed
4029 * from a previous run of zebra but not cleaned
4030 * up ( say a kill -9 )
4031 * But since we haven't actually installed
4032 * them yet( we received them from the kernel )
4033 * we don't think they are active.
4034 * So let's pretend they are active to actually
4035 * remove them.
4036 * In all honesty I'm not sure if we should
4037 * mark them as active when we receive them
4038 * This is startup only so probably ok.
4039 *
4040 * If we ever decide to move rib_sweep_table
4041 * to a different spot (ie startup )
4042 * this decision needs to be revisited
4043 */
4044 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
4045 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4046 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
4047
4048 rib_uninstall_kernel(rn, re);
4049 rib_delnode(rn, re);
4050 }
4051 }
4052
4053 if (IS_ZEBRA_DEBUG_RIB)
4054 zlog_debug("%s: ends", __func__);
4055 }
4056
4057 /* Sweep all RIB tables. */
4058 int rib_sweep_route(struct thread *t)
4059 {
4060 struct vrf *vrf;
4061 struct zebra_vrf *zvrf;
4062
4063 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4064 if ((zvrf = vrf->info) == NULL)
4065 continue;
4066
4067 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
4068 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
4069 }
4070
4071 zebra_router_sweep_route();
4072 zebra_router_sweep_nhgs();
4073
4074 return 0;
4075 }
4076
4077 /* Remove specific by protocol routes from 'table'. */
4078 unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
4079 struct route_table *table)
4080 {
4081 struct route_node *rn;
4082 struct route_entry *re;
4083 struct route_entry *next;
4084 unsigned long n = 0;
4085
4086 if (table)
4087 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
4088 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4089 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4090 continue;
4091 if (re->type == proto
4092 && re->instance == instance) {
4093 rib_delnode(rn, re);
4094 n++;
4095 }
4096 }
4097 return n;
4098 }
4099
4100 /* Remove specific by protocol routes. */
4101 unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
4102 {
4103 struct vrf *vrf;
4104 struct zebra_vrf *zvrf;
4105 struct other_route_table *ort;
4106 unsigned long cnt = 0;
4107
4108 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4109 zvrf = vrf->info;
4110 if (!zvrf)
4111 continue;
4112
4113 cnt += rib_score_proto_table(proto, instance,
4114 zvrf->table[AFI_IP][SAFI_UNICAST])
4115 + rib_score_proto_table(
4116 proto, instance,
4117 zvrf->table[AFI_IP6][SAFI_UNICAST]);
4118
4119 frr_each(otable, &zvrf->other_tables, ort) cnt +=
4120 rib_score_proto_table(proto, instance, ort->table);
4121 }
4122
4123 return cnt;
4124 }
4125
4126 /* Close RIB and clean up kernel routes. */
4127 void rib_close_table(struct route_table *table)
4128 {
4129 struct route_node *rn;
4130 struct rib_table_info *info;
4131 rib_dest_t *dest;
4132
4133 if (!table)
4134 return;
4135
4136 info = route_table_get_info(table);
4137
4138 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4139 dest = rib_dest_from_rnode(rn);
4140
4141 if (dest && dest->selected_fib) {
4142 if (info->safi == SAFI_UNICAST)
4143 hook_call(rib_update, rn, NULL);
4144
4145 rib_uninstall_kernel(rn, dest->selected_fib);
4146 dest->selected_fib = NULL;
4147 }
4148 }
4149 }
4150
4151 /*
4152 * Handler for async dataplane results after a pseudowire installation
4153 */
4154 static int handle_pw_result(struct zebra_dplane_ctx *ctx)
4155 {
4156 struct zebra_pw *pw;
4157 struct zebra_vrf *vrf;
4158
4159 /* The pseudowire code assumes success - we act on an error
4160 * result for installation attempts here.
4161 */
4162 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
4163 goto done;
4164
4165 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
4166 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
4167 pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx));
4168 if (pw)
4169 zebra_pw_install_failure(pw,
4170 dplane_ctx_get_pw_status(ctx));
4171 }
4172
4173 done:
4174 dplane_ctx_fini(&ctx);
4175
4176 return 0;
4177 }
4178
4179
4180 /*
4181 * Handle results from the dataplane system. Dequeue update context
4182 * structs, dispatch to appropriate internal handlers.
4183 */
4184 static int rib_process_dplane_results(struct thread *thread)
4185 {
4186 struct zebra_dplane_ctx *ctx;
4187 struct dplane_ctx_q ctxlist;
4188 bool shut_p = false;
4189
4190 /* Dequeue a list of completed updates with one lock/unlock cycle */
4191
4192 do {
4193 TAILQ_INIT(&ctxlist);
4194
4195 /* Take lock controlling queue of results */
4196 frr_with_mutex(&dplane_mutex) {
4197 /* Dequeue list of context structs */
4198 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
4199 }
4200
4201 /* Dequeue context block */
4202 ctx = dplane_ctx_dequeue(&ctxlist);
4203
4204 /* If we've emptied the results queue, we're done */
4205 if (ctx == NULL)
4206 break;
4207
4208 /* If zebra is shutting down, avoid processing results,
4209 * just drain the results queue.
4210 */
4211 shut_p = atomic_load_explicit(&zrouter.in_shutdown,
4212 memory_order_relaxed);
4213 if (shut_p) {
4214 while (ctx) {
4215 dplane_ctx_fini(&ctx);
4216
4217 ctx = dplane_ctx_dequeue(&ctxlist);
4218 }
4219
4220 continue;
4221 }
4222
4223 #ifdef HAVE_SCRIPTING
4224 char *script_name = frrscript_names_get_script_name(
4225 ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
4226
4227 int ret = 1;
4228 struct frrscript *fs;
4229
4230 if (script_name) {
4231 fs = frrscript_new(script_name);
4232 if (fs)
4233 ret = frrscript_load(
4234 fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4235 NULL);
4236 }
4237 #endif /* HAVE_SCRIPTING */
4238
4239 while (ctx) {
4240
4241 #ifdef HAVE_SCRIPTING
4242 if (ret == 0)
4243 frrscript_call(fs,
4244 ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4245 ("ctx", ctx));
4246 #endif /* HAVE_SCRIPTING */
4247
4248 switch (dplane_ctx_get_op(ctx)) {
4249 case DPLANE_OP_ROUTE_INSTALL:
4250 case DPLANE_OP_ROUTE_UPDATE:
4251 case DPLANE_OP_ROUTE_DELETE:
4252 {
4253 /* Bit of special case for route updates
4254 * that were generated by async notifications:
4255 * we don't want to continue processing these
4256 * in the rib.
4257 */
4258 if (dplane_ctx_get_notif_provider(ctx) == 0)
4259 rib_process_result(ctx);
4260 else
4261 dplane_ctx_fini(&ctx);
4262 }
4263 break;
4264
4265 case DPLANE_OP_ROUTE_NOTIFY:
4266 rib_process_dplane_notify(ctx);
4267 break;
4268
4269 case DPLANE_OP_NH_INSTALL:
4270 case DPLANE_OP_NH_UPDATE:
4271 case DPLANE_OP_NH_DELETE:
4272 zebra_nhg_dplane_result(ctx);
4273 break;
4274
4275 case DPLANE_OP_LSP_INSTALL:
4276 case DPLANE_OP_LSP_UPDATE:
4277 case DPLANE_OP_LSP_DELETE:
4278 {
4279 /* Bit of special case for LSP updates
4280 * that were generated by async notifications:
4281 * we don't want to continue processing these.
4282 */
4283 if (dplane_ctx_get_notif_provider(ctx) == 0)
4284 zebra_mpls_lsp_dplane_result(ctx);
4285 else
4286 dplane_ctx_fini(&ctx);
4287 }
4288 break;
4289
4290 case DPLANE_OP_LSP_NOTIFY:
4291 zebra_mpls_process_dplane_notify(ctx);
4292 break;
4293
4294 case DPLANE_OP_PW_INSTALL:
4295 case DPLANE_OP_PW_UNINSTALL:
4296 handle_pw_result(ctx);
4297 break;
4298
4299 case DPLANE_OP_SYS_ROUTE_ADD:
4300 case DPLANE_OP_SYS_ROUTE_DELETE:
4301 /* No further processing in zebra for these. */
4302 dplane_ctx_fini(&ctx);
4303 break;
4304
4305 case DPLANE_OP_MAC_INSTALL:
4306 case DPLANE_OP_MAC_DELETE:
4307 zebra_vxlan_handle_result(ctx);
4308 break;
4309
4310 case DPLANE_OP_RULE_ADD:
4311 case DPLANE_OP_RULE_DELETE:
4312 case DPLANE_OP_RULE_UPDATE:
4313 case DPLANE_OP_IPTABLE_ADD:
4314 case DPLANE_OP_IPTABLE_DELETE:
4315 case DPLANE_OP_IPSET_ADD:
4316 case DPLANE_OP_IPSET_DELETE:
4317 case DPLANE_OP_IPSET_ENTRY_ADD:
4318 case DPLANE_OP_IPSET_ENTRY_DELETE:
4319 zebra_pbr_dplane_result(ctx);
4320 break;
4321
4322 case DPLANE_OP_INTF_ADDR_ADD:
4323 case DPLANE_OP_INTF_ADDR_DEL:
4324 zebra_if_addr_update_ctx(ctx);
4325 break;
4326
4327 /* Some op codes not handled here */
4328 case DPLANE_OP_ADDR_INSTALL:
4329 case DPLANE_OP_ADDR_UNINSTALL:
4330 case DPLANE_OP_NEIGH_INSTALL:
4331 case DPLANE_OP_NEIGH_UPDATE:
4332 case DPLANE_OP_NEIGH_DELETE:
4333 case DPLANE_OP_NEIGH_IP_INSTALL:
4334 case DPLANE_OP_NEIGH_IP_DELETE:
4335 case DPLANE_OP_VTEP_ADD:
4336 case DPLANE_OP_VTEP_DELETE:
4337 case DPLANE_OP_NEIGH_DISCOVER:
4338 case DPLANE_OP_BR_PORT_UPDATE:
4339 case DPLANE_OP_NEIGH_TABLE_UPDATE:
4340 case DPLANE_OP_GRE_SET:
4341 case DPLANE_OP_NONE:
4342 /* Don't expect this: just return the struct? */
4343 dplane_ctx_fini(&ctx);
4344 break;
4345
4346 } /* Dispatch by op code */
4347
4348 ctx = dplane_ctx_dequeue(&ctxlist);
4349 }
4350
4351 } while (1);
4352
4353 return 0;
4354 }
4355
4356 /*
4357 * Results are returned from the dataplane subsystem, in the context of
4358 * the dataplane pthread. We enqueue the results here for processing by
4359 * the main thread later.
4360 */
4361 static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
4362 {
4363 /* Take lock controlling queue of results */
4364 frr_with_mutex(&dplane_mutex) {
4365 /* Enqueue context blocks */
4366 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
4367 }
4368
4369 /* Ensure event is signalled to zebra main pthread */
4370 thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
4371 &t_dplane);
4372
4373 return 0;
4374 }
4375
4376 /*
4377 * Ensure there are no empty slots in the route_info array.
4378 * Every route type in zebra should be present there.
4379 */
4380 static void check_route_info(void)
4381 {
4382 int len = array_size(route_info);
4383
4384 /*
4385 * ZEBRA_ROUTE_SYSTEM is special cased since
4386 * its key is 0 anyway.
4387 *
4388 * ZEBRA_ROUTE_ALL is also ignored.
4389 */
4390 for (int i = 0; i < len; i++) {
4391 if (i == ZEBRA_ROUTE_SYSTEM || i == ZEBRA_ROUTE_ALL)
4392 continue;
4393 assert(route_info[i].key);
4394 assert(route_info[i].meta_q_map < MQ_SIZE);
4395 }
4396 }
4397
4398 /* Routing information base initialize. */
4399 void rib_init(void)
4400 {
4401 check_route_info();
4402
4403 rib_queue_init();
4404
4405 /* Init dataplane, and register for results */
4406 pthread_mutex_init(&dplane_mutex, NULL);
4407 TAILQ_INIT(&rib_dplane_q);
4408 zebra_dplane_init(rib_dplane_results);
4409 }
4410
4411 /*
4412 * vrf_id_get_next
4413 *
4414 * Get the first vrf id that is greater than the given vrf id if any.
4415 *
4416 * Returns true if a vrf id was found, false otherwise.
4417 */
4418 static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
4419 {
4420 struct vrf *vrf;
4421
4422 vrf = vrf_lookup_by_id(vrf_id);
4423 if (vrf) {
4424 vrf = RB_NEXT(vrf_id_head, vrf);
4425 if (vrf) {
4426 *next_id_p = vrf->vrf_id;
4427 return 1;
4428 }
4429 }
4430
4431 return 0;
4432 }
4433
4434 /*
4435 * rib_tables_iter_next
4436 *
4437 * Returns the next table in the iteration.
4438 */
4439 struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
4440 {
4441 struct route_table *table;
4442
4443 /*
4444 * Array that helps us go over all AFI/SAFI combinations via one
4445 * index.
4446 */
4447 static const struct {
4448 afi_t afi;
4449 safi_t safi;
4450 } afi_safis[] = {
4451 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
4452 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
4453 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
4454 };
4455
4456 table = NULL;
4457
4458 switch (iter->state) {
4459
4460 case RIB_TABLES_ITER_S_INIT:
4461 iter->vrf_id = VRF_DEFAULT;
4462 iter->afi_safi_ix = -1;
4463
4464 /* Fall through */
4465
4466 case RIB_TABLES_ITER_S_ITERATING:
4467 iter->afi_safi_ix++;
4468 while (1) {
4469
4470 while (iter->afi_safi_ix
4471 < (int)array_size(afi_safis)) {
4472 table = zebra_vrf_table(
4473 afi_safis[iter->afi_safi_ix].afi,
4474 afi_safis[iter->afi_safi_ix].safi,
4475 iter->vrf_id);
4476 if (table)
4477 break;
4478
4479 iter->afi_safi_ix++;
4480 }
4481
4482 /*
4483 * Found another table in this vrf.
4484 */
4485 if (table)
4486 break;
4487
4488 /*
4489 * Done with all tables in the current vrf, go to the
4490 * next
4491 * one.
4492 */
4493 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
4494 break;
4495
4496 iter->afi_safi_ix = 0;
4497 }
4498
4499 break;
4500
4501 case RIB_TABLES_ITER_S_DONE:
4502 return NULL;
4503 }
4504
4505 if (table)
4506 iter->state = RIB_TABLES_ITER_S_ITERATING;
4507 else
4508 iter->state = RIB_TABLES_ITER_S_DONE;
4509
4510 return table;
4511 }