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