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