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