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