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