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