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