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