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