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