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