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