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