]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
bgpd: Always send down correct admin distance
[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"
718e3744 39
89272910 40#include "zebra/zebra_router.h"
43e52561
QY
41#include "zebra/connected.h"
42#include "zebra/debug.h"
43#include "zebra/interface.h"
44#include "zebra/redistribute.h"
718e3744 45#include "zebra/rib.h"
46#include "zebra/rt.h"
43e52561
QY
47#include "zebra/zapi_msg.h"
48#include "zebra/zebra_errors.h"
49#include "zebra/zebra_memory.h"
7c551956 50#include "zebra/zebra_ns.h"
fb018d25 51#include "zebra/zebra_rnh.h"
43e52561
QY
52#include "zebra/zebra_routemap.h"
53#include "zebra/zebra_vrf.h"
6134fd82 54#include "zebra/zebra_vxlan.h"
7cdb1a84
MS
55#include "zebra/zapi_msg.h"
56#include "zebra/zebra_dplane.h"
57
58/*
59 * Event, list, and mutex for delivery of dataplane results
60 */
61static pthread_mutex_t dplane_mutex;
62static struct thread *t_dplane;
25779064 63static struct dplane_ctx_q rib_dplane_q;
718e3744 64
d62a17ae 65DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
66 (rn, reason))
4f8ea50c 67
6baf7bb8
DS
68/* Should we allow non Quagga processes to delete our routes */
69extern int allow_delete;
70
718e3744 71/* Each route type's string and default distance value. */
d62a17ae 72static const struct {
73 int key;
8dc7a759 74 uint8_t distance;
73547a75 75 uint8_t meta_q_map;
d62a17ae 76} route_info[ZEBRA_ROUTE_MAX] = {
73547a75
DS
77 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0, 4},
78 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0, 0},
79 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0, 0},
80 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1, 1},
81 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120, 2},
82 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120, 2},
83 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110, 2},
84 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110, 2},
85 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115, 2},
86 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */, 3},
87 [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255, 4},
88 [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90, 2},
89 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10, 2},
90 [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255, 4},
91 [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255, 4},
92 [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150, 1},
93 [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150, 4},
94 [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20, 3},
95 [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20, 3},
97 [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20, 3},
99 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100, 2},
100 [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150, 4},
98156652 101 [ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, 200, 4},
42d96b73 102 [ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, 255, 4},
eab7b6e3 103 [ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC, 115, 2},
303b93cd 104 [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, 255, 4}
d6abd8b0 105 /* Any new route type added to zebra, should be mirrored here */
c710b277 106
d62a17ae 107 /* no entry/default: 150 */
718e3744 108};
6b0655a2 109
4623d897
DL
110/* RPF lookup behaviour */
111static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
112
6c4f4e6e 113
d62a17ae 114static void __attribute__((format(printf, 5, 6)))
115_rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn,
116 int priority, const char *msgfmt, ...)
2263a412 117{
d62a17ae 118 char buf[SRCDEST2STR_BUFFER + sizeof(" (MRIB)")];
119 char msgbuf[512];
120 va_list ap;
2263a412 121
d62a17ae 122 va_start(ap, msgfmt);
123 vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
124 va_end(ap);
2263a412 125
d62a17ae 126 if (rn) {
127 rib_table_info_t *info = srcdest_rnode_table_info(rn);
128 srcdest_rnode2str(rn, buf, sizeof(buf));
cb653491 129
d62a17ae 130 if (info->safi == SAFI_MULTICAST)
131 strcat(buf, " (MRIB)");
132 } else {
133 snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
134 }
2263a412 135
d62a17ae 136 zlog(priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
2263a412
DL
137}
138
d62a17ae 139#define rnode_debug(node, vrf_id, ...) \
2263a412 140 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
d62a17ae 141#define rnode_info(node, ...) \
2263a412
DL
142 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
143
fd289fc8 144uint8_t route_distance(int type)
40c7bdb0 145{
fd289fc8 146 uint8_t distance;
40c7bdb0 147
d62a17ae 148 if ((unsigned)type >= array_size(route_info))
149 distance = 150;
150 else
151 distance = route_info[type].distance;
40c7bdb0 152
d62a17ae 153 return distance;
40c7bdb0 154}
155
d7c0a89a 156int is_zebra_valid_kernel_table(uint32_t table_id)
7a4bb9c5 157{
8f500a1c 158#ifdef linux
d62a17ae 159 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
160 || (table_id == RT_TABLE_COMPAT))
161 return 0;
8f500a1c
RW
162#endif
163
d62a17ae 164 return 1;
7a4bb9c5
DS
165}
166
d7c0a89a 167int is_zebra_main_routing_table(uint32_t table_id)
7a4bb9c5 168{
c447ad08 169 if (table_id == RT_TABLE_MAIN)
d62a17ae 170 return 1;
171 return 0;
7a4bb9c5
DS
172}
173
86391e56 174int zebra_check_addr(const struct prefix *p)
0aabccc0 175{
d62a17ae 176 if (p->family == AF_INET) {
d7c0a89a 177 uint32_t addr;
0aabccc0 178
d62a17ae 179 addr = p->u.prefix4.s_addr;
180 addr = ntohl(addr);
0aabccc0 181
d62a17ae 182 if (IPV4_NET127(addr) || IN_CLASSD(addr)
183 || IPV4_LINKLOCAL(addr))
184 return 0;
185 }
186 if (p->family == AF_INET6) {
187 if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
188 return 0;
189 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
190 return 0;
191 }
192 return 1;
0aabccc0
DD
193}
194
fa713d9e 195/* Add nexthop to the end of a rib node's nexthop list */
d62a17ae 196void route_entry_nexthop_add(struct route_entry *re, struct nexthop *nexthop)
fa713d9e 197{
7ee30f28 198 nexthop_add(&re->ng.nexthop, nexthop);
d62a17ae 199 re->nexthop_num++;
718e3744 200}
201
6e26278c 202
6e26278c
DS
203/**
204 * copy_nexthop - copy a nexthop to the rib structure.
205 */
d62a17ae 206void route_entry_copy_nexthops(struct route_entry *re, struct nexthop *nh)
6e26278c 207{
7ee30f28
DS
208 assert(!re->ng.nexthop);
209 copy_nexthops(&re->ng.nexthop, nh, NULL);
d62a17ae 210 for (struct nexthop *nexthop = nh; nexthop; nexthop = nexthop->next)
211 re->nexthop_num++;
6e26278c
DS
212}
213
718e3744 214/* Delete specified nexthop from the list. */
d62a17ae 215void route_entry_nexthop_delete(struct route_entry *re, struct nexthop *nexthop)
718e3744 216{
d62a17ae 217 if (nexthop->next)
218 nexthop->next->prev = nexthop->prev;
219 if (nexthop->prev)
220 nexthop->prev->next = nexthop->next;
221 else
7ee30f28 222 re->ng.nexthop = nexthop->next;
d62a17ae 223 re->nexthop_num--;
718e3744 224}
225
fa713d9e 226
d62a17ae 227struct nexthop *route_entry_nexthop_ifindex_add(struct route_entry *re,
4a7371e9
DS
228 ifindex_t ifindex,
229 vrf_id_t nh_vrf_id)
718e3744 230{
d62a17ae 231 struct nexthop *nexthop;
718e3744 232
d62a17ae 233 nexthop = nexthop_new();
234 nexthop->type = NEXTHOP_TYPE_IFINDEX;
235 nexthop->ifindex = ifindex;
4a7371e9 236 nexthop->vrf_id = nh_vrf_id;
718e3744 237
d62a17ae 238 route_entry_nexthop_add(re, nexthop);
718e3744 239
d62a17ae 240 return nexthop;
718e3744 241}
242
d62a17ae 243struct nexthop *route_entry_nexthop_ipv4_add(struct route_entry *re,
244 struct in_addr *ipv4,
4a7371e9
DS
245 struct in_addr *src,
246 vrf_id_t nh_vrf_id)
718e3744 247{
d62a17ae 248 struct nexthop *nexthop;
718e3744 249
d62a17ae 250 nexthop = nexthop_new();
251 nexthop->type = NEXTHOP_TYPE_IPV4;
4a7371e9 252 nexthop->vrf_id = nh_vrf_id;
d62a17ae 253 nexthop->gate.ipv4 = *ipv4;
254 if (src)
255 nexthop->src.ipv4 = *src;
718e3744 256
d62a17ae 257 route_entry_nexthop_add(re, nexthop);
718e3744 258
d62a17ae 259 return nexthop;
718e3744 260}
261
d62a17ae 262struct nexthop *route_entry_nexthop_ipv4_ifindex_add(struct route_entry *re,
263 struct in_addr *ipv4,
264 struct in_addr *src,
4a7371e9
DS
265 ifindex_t ifindex,
266 vrf_id_t nh_vrf_id)
718e3744 267{
d62a17ae 268 struct nexthop *nexthop;
269 struct interface *ifp;
718e3744 270
d62a17ae 271 nexthop = nexthop_new();
4a7371e9 272 nexthop->vrf_id = nh_vrf_id;
d62a17ae 273 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
274 nexthop->gate.ipv4 = *ipv4;
275 if (src)
276 nexthop->src.ipv4 = *src;
277 nexthop->ifindex = ifindex;
4a7371e9 278 ifp = if_lookup_by_index(nexthop->ifindex, nh_vrf_id);
d62a17ae 279 /*Pending: need to think if null ifp here is ok during bootup?
280 There was a crash because ifp here was coming to be NULL */
281 if (ifp)
2b83602b 282 if (connected_is_unnumbered(ifp))
d62a17ae 283 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
718e3744 284
d62a17ae 285 route_entry_nexthop_add(re, nexthop);
718e3744 286
d62a17ae 287 return nexthop;
718e3744 288}
289
d62a17ae 290struct nexthop *route_entry_nexthop_ipv6_add(struct route_entry *re,
4a7371e9
DS
291 struct in6_addr *ipv6,
292 vrf_id_t nh_vrf_id)
718e3744 293{
d62a17ae 294 struct nexthop *nexthop;
718e3744 295
d62a17ae 296 nexthop = nexthop_new();
4a7371e9 297 nexthop->vrf_id = nh_vrf_id;
d62a17ae 298 nexthop->type = NEXTHOP_TYPE_IPV6;
299 nexthop->gate.ipv6 = *ipv6;
718e3744 300
d62a17ae 301 route_entry_nexthop_add(re, nexthop);
718e3744 302
d62a17ae 303 return nexthop;
718e3744 304}
305
d62a17ae 306struct nexthop *route_entry_nexthop_ipv6_ifindex_add(struct route_entry *re,
307 struct in6_addr *ipv6,
4a7371e9
DS
308 ifindex_t ifindex,
309 vrf_id_t nh_vrf_id)
718e3744 310{
d62a17ae 311 struct nexthop *nexthop;
718e3744 312
d62a17ae 313 nexthop = nexthop_new();
4a7371e9 314 nexthop->vrf_id = nh_vrf_id;
d62a17ae 315 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
316 nexthop->gate.ipv6 = *ipv6;
317 nexthop->ifindex = ifindex;
718e3744 318
d62a17ae 319 route_entry_nexthop_add(re, nexthop);
718e3744 320
d62a17ae 321 return nexthop;
718e3744 322}
718e3744 323
a8309422 324struct nexthop *route_entry_nexthop_blackhole_add(struct route_entry *re,
60466a63 325 enum blackhole_type bh_type)
595db7f1 326{
d62a17ae 327 struct nexthop *nexthop;
595db7f1 328
d62a17ae 329 nexthop = nexthop_new();
4a7371e9 330 nexthop->vrf_id = VRF_DEFAULT;
d62a17ae 331 nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
a8309422 332 nexthop->bh_type = bh_type;
595db7f1 333
d62a17ae 334 route_entry_nexthop_add(re, nexthop);
595db7f1 335
d62a17ae 336 return nexthop;
595db7f1 337}
338
86391e56 339static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
d62a17ae 340 struct nexthop *nexthop)
4491a88f 341{
d62a17ae 342 struct nexthop *resolved_hop;
4491a88f 343
d62a17ae 344 resolved_hop = nexthop_new();
345 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
d855d11f 346
4a7371e9 347 resolved_hop->vrf_id = nexthop->vrf_id;
d855d11f
RW
348 switch (newhop->type) {
349 case NEXTHOP_TYPE_IPV4:
350 case NEXTHOP_TYPE_IPV4_IFINDEX:
351 /* If the resolving route specifies a gateway, use it */
d62a17ae 352 resolved_hop->type = newhop->type;
353 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
4491a88f 354
d62a17ae 355 if (newhop->ifindex) {
356 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
357 resolved_hop->ifindex = newhop->ifindex;
d62a17ae 358 }
d855d11f
RW
359 break;
360 case NEXTHOP_TYPE_IPV6:
361 case NEXTHOP_TYPE_IPV6_IFINDEX:
d62a17ae 362 resolved_hop->type = newhop->type;
363 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
364
365 if (newhop->ifindex) {
366 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
367 resolved_hop->ifindex = newhop->ifindex;
368 }
d855d11f
RW
369 break;
370 case NEXTHOP_TYPE_IFINDEX:
371 /* If the resolving route is an interface route,
372 * it means the gateway we are looking up is connected
373 * to that interface. (The actual network is _not_ onlink).
374 * Therefore, the resolved route should have the original
375 * gateway as nexthop as it is directly connected.
376 *
377 * On Linux, we have to set the onlink netlink flag because
378 * otherwise, the kernel won't accept the route.
379 */
d62a17ae 380 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
381 if (afi == AFI_IP) {
382 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
383 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
384 } else if (afi == AFI_IP6) {
385 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
386 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
387 }
388 resolved_hop->ifindex = newhop->ifindex;
d855d11f
RW
389 break;
390 case NEXTHOP_TYPE_BLACKHOLE:
59693377
DS
391 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
392 resolved_hop->bh_type = nexthop->bh_type;
d855d11f 393 break;
59693377 394 }
d855d11f 395
effcfaeb
DS
396 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
397 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
398
f674dfe2
RW
399 /* Copy labels of the resolved route */
400 if (newhop->nh_label)
401 nexthop_add_labels(resolved_hop, newhop->nh_label_type,
402 newhop->nh_label->num_labels,
403 &newhop->nh_label->label[0]);
404
d62a17ae 405 resolved_hop->rparent = nexthop;
406 nexthop_add(&nexthop->resolved, resolved_hop);
4491a88f
DS
407}
408
99eabcec
DS
409/*
410 * Given a nexthop we need to properly recursively resolve
411 * the route. As such, do a table lookup to find and match
412 * if at all possible. Set the nexthop->ifindex as appropriate
413 */
d62a17ae 414static int nexthop_active(afi_t afi, struct route_entry *re,
99eabcec 415 struct nexthop *nexthop,
d62a17ae 416 struct route_node *top)
417{
418 struct prefix p;
419 struct route_table *table;
420 struct route_node *rn;
5f7a4718 421 struct route_entry *match = NULL;
d62a17ae 422 int resolved;
423 struct nexthop *newhop;
424 struct interface *ifp;
5f7a4718 425 rib_dest_t *dest;
d62a17ae 426
427 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
428 || nexthop->type == NEXTHOP_TYPE_IPV6)
429 nexthop->ifindex = 0;
430
99eabcec
DS
431 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
432 nexthops_free(nexthop->resolved);
433 nexthop->resolved = NULL;
434 re->nexthop_mtu = 0;
18ff3edd 435
058c16b7
DS
436 /*
437 * If the kernel has sent us a route, then
438 * by golly gee whiz it's a good route.
439 */
440 if (re->type == ZEBRA_ROUTE_KERNEL ||
441 re->type == ZEBRA_ROUTE_SYSTEM)
442 return 1;
443
d62a17ae 444 /*
445 * Check to see if we should trust the passed in information
446 * for UNNUMBERED interfaces as that we won't find the GW
447 * address in the routing table.
2b83602b 448 * This check should suffice to handle IPv4 or IPv6 routes
449 * sourced from EVPN routes which are installed with the
450 * next hop as the remote VTEP IP.
d62a17ae 451 */
452 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
4a7371e9 453 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
fe85601c
DS
454 if (!ifp) {
455 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
456 zlog_debug(
457 "\t%s: Onlink and interface: %u[%u] does not exist",
458 __PRETTY_FUNCTION__, nexthop->ifindex,
459 nexthop->vrf_id);
460 return 0;
461 }
462 if (connected_is_unnumbered(ifp)) {
d62a17ae 463 if (if_is_operative(ifp))
464 return 1;
34815ea3
DS
465 else {
466 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
467 zlog_debug(
468 "\t%s: Onlink and interface %s is not operative",
469 __PRETTY_FUNCTION__, ifp->name);
d62a17ae 470 return 0;
34815ea3 471 }
fe85601c
DS
472 }
473 if (!if_is_operative(ifp)) {
34815ea3
DS
474 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
475 zlog_debug(
476 "\t%s: Interface %s is not unnumbered",
4bb55bbe 477 __PRETTY_FUNCTION__, ifp->name);
d62a17ae 478 return 0;
34815ea3 479 }
16814f96 480 }
718e3744 481
d62a17ae 482 /* Make lookup prefix. */
483 memset(&p, 0, sizeof(struct prefix));
484 switch (afi) {
485 case AFI_IP:
486 p.family = AF_INET;
487 p.prefixlen = IPV4_MAX_PREFIXLEN;
488 p.u.prefix4 = nexthop->gate.ipv4;
489 break;
490 case AFI_IP6:
491 p.family = AF_INET6;
492 p.prefixlen = IPV6_MAX_PREFIXLEN;
493 p.u.prefix6 = nexthop->gate.ipv6;
494 break;
495 default:
496 assert(afi != AFI_IP && afi != AFI_IP6);
497 break;
718e3744 498 }
d62a17ae 499 /* Lookup table. */
4a7371e9 500 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
34815ea3
DS
501 if (!table) {
502 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
503 zlog_debug("\t%s: Table not found",
504 __PRETTY_FUNCTION__);
d62a17ae 505 return 0;
34815ea3 506 }
d62a17ae 507
508 rn = route_node_match(table, (struct prefix *)&p);
509 while (rn) {
510 route_unlock_node(rn);
511
fd7fd9e5
DS
512 /* Lookup should halt if we've matched against ourselves ('top',
513 * if specified) - i.e., we cannot have a nexthop NH1 is
514 * resolved by a route NH1. The exception is if the route is a
515 * host route.
516 */
517 if (top && rn == top)
996c9314 518 if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
34815ea3
DS
519 || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
520 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
521 zlog_debug(
522 "\t%s: Matched against ourself and prefix length is not max bit length",
523 __PRETTY_FUNCTION__);
fd7fd9e5 524 return 0;
34815ea3 525 }
d62a17ae 526
527 /* Pick up selected route. */
528 /* However, do not resolve over default route unless explicitly
529 * allowed. */
530 if (is_default_prefix(&rn->p)
34815ea3
DS
531 && !rnh_resolve_via_default(p.family)) {
532 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
533 zlog_debug(
534 "\t:%s: Resolved against default route",
535 __PRETTY_FUNCTION__);
d62a17ae 536 return 0;
34815ea3 537 }
d62a17ae 538
5f7a4718 539 dest = rib_dest_from_rnode(rn);
996c9314
LB
540 if (dest && dest->selected_fib
541 && !CHECK_FLAG(dest->selected_fib->status,
542 ROUTE_ENTRY_REMOVED)
543 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
5f7a4718 544 match = dest->selected_fib;
48a53dc7 545
d62a17ae 546 /* If there is no selected route or matched route is EGP, go up
547 tree. */
548 if (!match) {
549 do {
550 rn = rn->parent;
551 } while (rn && rn->info == NULL);
552 if (rn)
553 route_lock_node(rn);
c87bdd2b 554
d62a17ae 555 continue;
556 }
557
d62a17ae 558 if (match->type == ZEBRA_ROUTE_CONNECT) {
559 /* Directly point connected route. */
7ee30f28 560 newhop = match->ng.nexthop;
d62a17ae 561 if (newhop) {
562 if (nexthop->type == NEXTHOP_TYPE_IPV4
563 || nexthop->type == NEXTHOP_TYPE_IPV6)
564 nexthop->ifindex = newhop->ifindex;
565 }
566 return 1;
4e8b02f4 567 } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
d62a17ae 568 resolved = 0;
7ee30f28 569 for (ALL_NEXTHOPS(match->ng, newhop)) {
677c1dd5
DS
570 if (!CHECK_FLAG(match->status,
571 ROUTE_ENTRY_INSTALLED))
a8309422
DL
572 continue;
573 if (CHECK_FLAG(newhop->flags,
574 NEXTHOP_FLAG_RECURSIVE))
575 continue;
576
99eabcec
DS
577 SET_FLAG(nexthop->flags,
578 NEXTHOP_FLAG_RECURSIVE);
579 SET_FLAG(re->status,
580 ROUTE_ENTRY_NEXTHOPS_CHANGED);
581 nexthop_set_resolved(afi, newhop, nexthop);
a8309422
DL
582 resolved = 1;
583 }
99eabcec 584 if (resolved)
43e31305 585 re->nexthop_mtu = match->mtu;
34815ea3
DS
586 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
587 zlog_debug("\t%s: Recursion failed to find",
588 __PRETTY_FUNCTION__);
d62a17ae 589 return resolved;
590 } else if (re->type == ZEBRA_ROUTE_STATIC) {
591 resolved = 0;
7ee30f28 592 for (ALL_NEXTHOPS(match->ng, newhop)) {
677c1dd5
DS
593 if (!CHECK_FLAG(match->status,
594 ROUTE_ENTRY_INSTALLED))
a8309422 595 continue;
6c47d399
DS
596 if (CHECK_FLAG(newhop->flags,
597 NEXTHOP_FLAG_RECURSIVE))
598 continue;
a8309422 599
99eabcec
DS
600 SET_FLAG(nexthop->flags,
601 NEXTHOP_FLAG_RECURSIVE);
602 nexthop_set_resolved(afi, newhop, nexthop);
a8309422
DL
603 resolved = 1;
604 }
99eabcec 605 if (resolved)
d62a17ae 606 re->nexthop_mtu = match->mtu;
34815ea3
DS
607
608 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
609 zlog_debug(
610 "\t%s: Static route unable to resolve",
611 __PRETTY_FUNCTION__);
d62a17ae 612 return resolved;
613 } else {
7939ff76
DS
614 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
615 zlog_debug("\t%s: Route Type %s has not turned on recursion",
616 __PRETTY_FUNCTION__,
617 zebra_route_string(re->type));
618 if (re->type == ZEBRA_ROUTE_BGP &&
619 !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
620 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
621 }
d62a17ae 622 return 0;
623 }
718e3744 624 }
7939ff76
DS
625 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
626 zlog_debug("\t%s: Nexthop did not lookup in table",
627 __PRETTY_FUNCTION__);
d62a17ae 628 return 0;
718e3744 629}
718e3744 630
d62a17ae 631struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
632 union g_addr *addr, struct route_node **rn_out)
633{
634 struct prefix p;
635 struct route_table *table;
636 struct route_node *rn;
5f7a4718 637 struct route_entry *match = NULL;
d62a17ae 638
639 /* Lookup table. */
640 table = zebra_vrf_table(afi, safi, vrf_id);
641 if (!table)
642 return 0;
643
644 memset(&p, 0, sizeof(struct prefix));
645 p.family = afi;
646 if (afi == AFI_IP) {
647 p.u.prefix4 = addr->ipv4;
648 p.prefixlen = IPV4_MAX_PREFIXLEN;
649 } else {
650 p.u.prefix6 = addr->ipv6;
651 p.prefixlen = IPV6_MAX_PREFIXLEN;
652 }
718e3744 653
d62a17ae 654 rn = route_node_match(table, (struct prefix *)&p);
718e3744 655
d62a17ae 656 while (rn) {
5f7a4718
DS
657 rib_dest_t *dest;
658
d62a17ae 659 route_unlock_node(rn);
718e3744 660
5f7a4718 661 dest = rib_dest_from_rnode(rn);
996c9314
LB
662 if (dest && dest->selected_fib
663 && !CHECK_FLAG(dest->selected_fib->status,
664 ROUTE_ENTRY_REMOVED))
5f7a4718 665 match = dest->selected_fib;
718e3744 666
d62a17ae 667 /* If there is no selected route or matched route is EGP, go up
668 tree. */
669 if (!match) {
670 do {
671 rn = rn->parent;
672 } while (rn && rn->info == NULL);
673 if (rn)
674 route_lock_node(rn);
675 } else {
676 if (match->type != ZEBRA_ROUTE_CONNECT) {
677c1dd5
DS
677 if (!CHECK_FLAG(match->status,
678 ROUTE_ENTRY_INSTALLED))
d62a17ae 679 return NULL;
680 }
681
682 if (rn_out)
683 *rn_out = rn;
684 return match;
685 }
16814f96 686 }
d62a17ae 687 return NULL;
688}
689
690struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
691 struct in_addr addr,
692 struct route_node **rn_out)
693{
694 struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
695 struct route_node *m_rn = NULL, *u_rn = NULL;
696 union g_addr gaddr = {.ipv4 = addr};
697
698 switch (ipv4_multicast_mode) {
699 case MCAST_MRIB_ONLY:
700 return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
701 rn_out);
702 case MCAST_URIB_ONLY:
703 return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
704 case MCAST_NO_CONFIG:
705 case MCAST_MIX_MRIB_FIRST:
706 re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
707 &m_rn);
708 if (!mre)
709 re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id,
710 &gaddr, &u_rn);
711 break;
712 case MCAST_MIX_DISTANCE:
713 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
714 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
715 if (mre && ure)
716 re = ure->distance < mre->distance ? ure : mre;
717 else if (mre)
718 re = mre;
719 else if (ure)
720 re = ure;
721 break;
722 case MCAST_MIX_PFXLEN:
723 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
724 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
725 if (mre && ure)
726 re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
727 else if (mre)
728 re = mre;
729 else if (ure)
730 re = ure;
731 break;
718e3744 732 }
33550aa8 733
d62a17ae 734 if (rn_out)
735 *rn_out = (re == mre) ? m_rn : u_rn;
718e3744 736
d62a17ae 737 if (IS_ZEBRA_DEBUG_RIB) {
738 char buf[BUFSIZ];
739 inet_ntop(AF_INET, &addr, buf, BUFSIZ);
4623d897 740
32391aff
DS
741 zlog_debug("%s: %s: vrf: %u found %s, using %s",
742 __func__, buf, vrf_id,
d62a17ae 743 mre ? (ure ? "MRIB+URIB" : "MRIB")
744 : ure ? "URIB" : "nothing",
745 re == ure ? "URIB" : re == mre ? "MRIB" : "none");
746 }
747 return re;
4623d897
DL
748}
749
d62a17ae 750void multicast_mode_ipv4_set(enum multicast_mode mode)
4623d897 751{
d62a17ae 752 if (IS_ZEBRA_DEBUG_RIB)
753 zlog_debug("%s: multicast lookup mode set (%d)", __func__,
754 mode);
755 ipv4_multicast_mode = mode;
4623d897
DL
756}
757
d62a17ae 758enum multicast_mode multicast_mode_ipv4_get(void)
4623d897 759{
d62a17ae 760 return ipv4_multicast_mode;
4623d897
DL
761}
762
d62a17ae 763struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
718e3744 764{
d62a17ae 765 struct route_table *table;
766 struct route_node *rn;
5f7a4718 767 struct route_entry *match = NULL;
5f7a4718 768 rib_dest_t *dest;
718e3744 769
d62a17ae 770 /* Lookup table. */
771 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
772 if (!table)
773 return 0;
718e3744 774
d62a17ae 775 rn = route_node_lookup(table, (struct prefix *)p);
718e3744 776
d62a17ae 777 /* No route for this prefix. */
778 if (!rn)
779 return NULL;
718e3744 780
d62a17ae 781 /* Unlock node. */
782 route_unlock_node(rn);
5f7a4718 783 dest = rib_dest_from_rnode(rn);
718e3744 784
996c9314
LB
785 if (dest && dest->selected_fib
786 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
5f7a4718 787 match = dest->selected_fib;
718e3744 788
d62a17ae 789 if (!match)
790 return NULL;
718e3744 791
d62a17ae 792 if (match->type == ZEBRA_ROUTE_CONNECT)
793 return match;
f9e1b38e 794
677c1dd5
DS
795 if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED))
796 return match;
718e3744 797
d62a17ae 798 return NULL;
718e3744 799}
800
dc95824a
DO
801/* This function verifies reachability of one given nexthop, which can be
802 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
99eabcec
DS
803 * in nexthop->flags field. The nexthop->ifindex will be updated
804 * appropriately as well. An existing route map can turn
805 * (otherwise active) nexthop into inactive, but not vice versa.
dc95824a
DO
806 *
807 * The return value is the final value of 'ACTIVE' flag.
808 */
d62a17ae 809static unsigned nexthop_active_check(struct route_node *rn,
810 struct route_entry *re,
99eabcec 811 struct nexthop *nexthop)
d62a17ae 812{
813 struct interface *ifp;
814 route_map_result_t ret = RMAP_MATCH;
815 int family;
816 char buf[SRCDEST2STR_BUFFER];
86391e56 817 const struct prefix *p, *src_p;
ac6eebce 818 struct zebra_vrf *zvrf;
819
d62a17ae 820 srcdest_rnode_prefixes(rn, &p, &src_p);
821
822 if (rn->p.family == AF_INET)
823 family = AFI_IP;
824 else if (rn->p.family == AF_INET6)
825 family = AFI_IP6;
826 else
827 family = 0;
828 switch (nexthop->type) {
829 case NEXTHOP_TYPE_IFINDEX:
4a7371e9 830 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
d62a17ae 831 if (ifp && if_is_operative(ifp))
832 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
833 else
834 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
835 break;
836 case NEXTHOP_TYPE_IPV4:
837 case NEXTHOP_TYPE_IPV4_IFINDEX:
838 family = AFI_IP;
99eabcec 839 if (nexthop_active(AFI_IP, re, nexthop, rn))
d62a17ae 840 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
841 else
842 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
843 break;
844 case NEXTHOP_TYPE_IPV6:
845 family = AFI_IP6;
99eabcec 846 if (nexthop_active(AFI_IP6, re, nexthop, rn))
d62a17ae 847 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
848 else
849 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
850 break;
851 case NEXTHOP_TYPE_IPV6_IFINDEX:
852 /* RFC 5549, v4 prefix with v6 NH */
853 if (rn->p.family != AF_INET)
854 family = AFI_IP6;
855 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
007dbee6 856 ifp = if_lookup_by_index(nexthop->ifindex,
4a7371e9 857 nexthop->vrf_id);
d62a17ae 858 if (ifp && if_is_operative(ifp))
859 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
860 else
861 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
862 } else {
99eabcec 863 if (nexthop_active(AFI_IP6, re, nexthop, rn))
d62a17ae 864 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
865 else
866 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
867 }
868 break;
869 case NEXTHOP_TYPE_BLACKHOLE:
870 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
871 break;
872 default:
873 break;
874 }
34815ea3
DS
875 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
876 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
877 zlog_debug("\t%s: Unable to find a active nexthop",
878 __PRETTY_FUNCTION__);
d62a17ae 879 return 0;
34815ea3 880 }
d62a17ae 881
882 /* XXX: What exactly do those checks do? Do we support
ac6eebce 883 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
884 */
d62a17ae 885 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
886 || (family == AFI_IP6 && p->family != AF_INET6))
887 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
888
889 /* The original code didn't determine the family correctly
890 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
891 * from the rib_table_info in those cases.
892 * Possibly it may be better to use only the rib_table_info
893 * in every case.
894 */
895 if (!family) {
896 rib_table_info_t *info;
897
898 info = srcdest_rnode_table_info(rn);
899 family = info->afi;
718e3744 900 }
c52ef59f 901
d62a17ae 902 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
903
ac6eebce 904 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
905 if (!zvrf) {
906 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
907 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__);
908 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
909 }
910
d62a17ae 911 /* It'll get set if required inside */
ac6eebce 912 ret = zebra_route_map_check(family, re->type, re->instance, p,
913 nexthop, zvrf, re->tag);
d62a17ae 914 if (ret == RMAP_DENYMATCH) {
915 if (IS_ZEBRA_DEBUG_RIB) {
916 srcdest_rnode2str(rn, buf, sizeof(buf));
917 zlog_debug(
918 "%u:%s: Filtering out with NH out %s due to route map",
919 re->vrf_id, buf,
99b9d960 920 ifindex2ifname(nexthop->ifindex,
4a7371e9 921 nexthop->vrf_id));
d62a17ae 922 }
923 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
518f0eb1 924 }
d62a17ae 925 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
718e3744 926}
927
99eabcec
DS
928/*
929 * Iterate over all nexthops of the given RIB entry and refresh their
f0f77c9a
DS
930 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
931 * nexthop is found to toggle the ACTIVE flag, the whole re structure
99eabcec 932 * is flagged with ROUTE_ENTRY_CHANGED.
03e232a4
DO
933 *
934 * Return value is the new number of active nexthops.
935 */
99eabcec 936static int nexthop_active_update(struct route_node *rn, struct route_entry *re)
d62a17ae 937{
938 struct nexthop *nexthop;
939 union g_addr prev_src;
80ad0418 940 unsigned int prev_active, new_active;
d62a17ae 941 ifindex_t prev_index;
ac6eebce 942
d62a17ae 943 re->nexthop_active_num = 0;
944 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
945
7ee30f28 946 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
d62a17ae 947 /* No protocol daemon provides src and so we're skipping
948 * tracking it */
949 prev_src = nexthop->rmap_src;
950 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
951 prev_index = nexthop->ifindex;
220f0f42
DS
952 /*
953 * We need to respect the multipath_num here
954 * as that what we should be able to install from
955 * a multipath perpsective should not be a data plane
956 * decision point.
957 */
99eabcec 958 new_active = nexthop_active_check(rn, re, nexthop);
b3f2b590
DS
959 if (new_active
960 && re->nexthop_active_num >= zrouter.multipath_num) {
220f0f42
DS
961 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
962 new_active = 0;
963 }
964 if (new_active)
d62a17ae 965 re->nexthop_active_num++;
966 /* Don't allow src setting on IPv6 addr for now */
967 if (prev_active != new_active || prev_index != nexthop->ifindex
968 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
969 && nexthop->type < NEXTHOP_TYPE_IPV6)
970 && prev_src.ipv4.s_addr
971 != nexthop->rmap_src.ipv4.s_addr)
972 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
973 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
974 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
6883bf8d
DS
975 &nexthop->rmap_src.ipv6)))
976 || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)) {
d62a17ae 977 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
978 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
979 }
980 }
981
d62a17ae 982 return re->nexthop_active_num;
718e3744 983}
6baeb988 984
a64448ba
DS
985/*
986 * Is this RIB labeled-unicast? It must be of type BGP and all paths
987 * (nexthops) must have a label.
988 */
d62a17ae 989int zebra_rib_labeled_unicast(struct route_entry *re)
a64448ba 990{
d62a17ae 991 struct nexthop *nexthop = NULL;
a64448ba 992
d62a17ae 993 if (re->type != ZEBRA_ROUTE_BGP)
994 return 0;
a64448ba 995
7ee30f28 996 for (ALL_NEXTHOPS(re->ng, nexthop))
d62a17ae 997 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
998 return 0;
6b0655a2 999
d62a17ae 1000 return 1;
a64448ba 1001}
718e3744 1002
6ae24471
DS
1003/* Update flag indicates whether this is a "replace" or not. Currently, this
1004 * is only used for IPv4.
1005 */
0c555cc6
DS
1006void rib_install_kernel(struct route_node *rn, struct route_entry *re,
1007 struct route_entry *old)
718e3744 1008{
d62a17ae 1009 struct nexthop *nexthop;
1010 rib_table_info_t *info = srcdest_rnode_table_info(rn);
d62a17ae 1011 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
97f5b441
MS
1012 const struct prefix *p, *src_p;
1013 enum zebra_dplane_result ret;
1014
1015 rib_dest_t *dest = rib_dest_from_rnode(rn);
718e3744 1016
d62a17ae 1017 srcdest_rnode_prefixes(rn, &p, &src_p);
416ec78d 1018
d62a17ae 1019 if (info->safi != SAFI_UNICAST) {
7ee30f28 1020 for (ALL_NEXTHOPS(re->ng, nexthop))
d62a17ae 1021 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 1022 return;
25b9cb0c
DL
1023 } else {
1024 struct nexthop *prev;
1025
7ee30f28 1026 for (ALL_NEXTHOPS(re->ng, nexthop)) {
25b9cb0c 1027 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_DUPLICATE);
7ee30f28 1028 for (ALL_NEXTHOPS(re->ng, prev)) {
25b9cb0c
DL
1029 if (prev == nexthop)
1030 break;
996c9314
LB
1031 if (nexthop_same_firsthop(nexthop, prev)) {
1032 SET_FLAG(nexthop->flags,
1033 NEXTHOP_FLAG_DUPLICATE);
25b9cb0c
DL
1034 break;
1035 }
1036 }
1037 }
d62a17ae 1038 }
718e3744 1039
2063a814
DS
1040 /*
1041 * If this is a replace to a new RE let the originator of the RE
1042 * know that they've lost
1043 */
9a9f8926 1044 if (old && (old != re) && (old->type != re->type))
28610f7e 1045 zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON);
25b9cb0c 1046
97f5b441
MS
1047 /* Update fib selection */
1048 dest->selected_fib = re;
1049
d62a17ae 1050 /*
1051 * Make sure we update the FPM any time we send new information to
1052 * the kernel.
1053 */
1054 hook_call(rib_update, rn, "installing in kernel");
97f5b441
MS
1055
1056 /* Send add or update */
8263d1d0 1057 if (old)
97f5b441 1058 ret = dplane_route_update(rn, re, old);
5709131c 1059 else
97f5b441 1060 ret = dplane_route_add(rn, re);
97f5b441
MS
1061
1062 switch (ret) {
ea1c14f6 1063 case ZEBRA_DPLANE_REQUEST_QUEUED:
60f98b23
DS
1064 SET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1065 if (old)
1066 SET_FLAG(old->status, ROUTE_ENTRY_QUEUED);
97f5b441
MS
1067 if (zvrf)
1068 zvrf->installs_queued++;
1e885672 1069 break;
ea1c14f6 1070 case ZEBRA_DPLANE_REQUEST_FAILURE:
97f5b441
MS
1071 {
1072 char str[SRCDEST2STR_BUFFER];
1073
1074 srcdest_rnode2str(rn, str, sizeof(str));
1075 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
1076 "%u:%s: Failed to enqueue dataplane install",
1077 re->vrf_id, str);
1e885672 1078 break;
97f5b441 1079 }
ea1c14f6 1080 case ZEBRA_DPLANE_REQUEST_SUCCESS:
97f5b441
MS
1081 if (zvrf)
1082 zvrf->installs++;
1e885672
DS
1083 break;
1084 }
d62a17ae 1085
0c555cc6 1086 return;
718e3744 1087}
1088
1089/* Uninstall the route from kernel. */
0c555cc6 1090void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
718e3744 1091{
d62a17ae 1092 struct nexthop *nexthop;
1093 rib_table_info_t *info = srcdest_rnode_table_info(rn);
d62a17ae 1094 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
05737783 1095
d62a17ae 1096 if (info->safi != SAFI_UNICAST) {
677c1dd5 1097 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
7ee30f28 1098 for (ALL_NEXTHOPS(re->ng, nexthop))
d6792f9d 1099 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 1100 return;
d62a17ae 1101 }
416ec78d 1102
d62a17ae 1103 /*
1104 * Make sure we update the FPM any time we send new information to
97f5b441 1105 * the dataplane.
d62a17ae 1106 */
1107 hook_call(rib_update, rn, "uninstalling from kernel");
97f5b441
MS
1108
1109 switch (dplane_route_delete(rn, re)) {
ea1c14f6 1110 case ZEBRA_DPLANE_REQUEST_QUEUED:
97f5b441
MS
1111 if (zvrf)
1112 zvrf->removals_queued++;
1e885672 1113 break;
ea1c14f6 1114 case ZEBRA_DPLANE_REQUEST_FAILURE:
97f5b441
MS
1115 {
1116 char str[SRCDEST2STR_BUFFER];
1117
1118 srcdest_rnode2str(rn, str, sizeof(str));
1119 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
1120 "%u:%s: Failed to enqueue dataplane uninstall",
1121 re->vrf_id, str);
1e885672 1122 break;
97f5b441 1123 }
ea1c14f6 1124 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1e885672
DS
1125 if (zvrf)
1126 zvrf->removals++;
1127 break;
1128 }
718e3744 1129
0c555cc6 1130 return;
718e3744 1131}
1132
1133/* Uninstall the route from kernel. */
d62a17ae 1134static void rib_uninstall(struct route_node *rn, struct route_entry *re)
718e3744 1135{
d62a17ae 1136 rib_table_info_t *info = srcdest_rnode_table_info(rn);
5f7a4718 1137 rib_dest_t *dest = rib_dest_from_rnode(rn);
97f5b441 1138 struct nexthop *nexthop;
416ec78d 1139
5f7a4718 1140 if (dest && dest->selected_fib == re) {
d62a17ae 1141 if (info->safi == SAFI_UNICAST)
1142 hook_call(rib_update, rn, "rib_uninstall");
5adc2528 1143
d62a17ae 1144 /* If labeled-unicast route, uninstall transit LSP. */
1145 if (zebra_rib_labeled_unicast(re))
1146 zebra_mpls_lsp_uninstall(info->zvrf, rn, re);
7cdb1a84 1147
3cdba47a 1148 rib_uninstall_kernel(rn, re);
97f5b441
MS
1149
1150 dest->selected_fib = NULL;
1151
1152 for (ALL_NEXTHOPS(re->ng, nexthop))
1153 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
d62a17ae 1154 }
446bb95e 1155
d62a17ae 1156 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
86391e56
MS
1157 const struct prefix *p, *src_p;
1158
d62a17ae 1159 srcdest_rnode_prefixes(rn, &p, &src_p);
05737783 1160
d62a17ae 1161 redistribute_delete(p, src_p, re);
1162 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
1163 }
718e3744 1164}
1165
9fd92e3c
AS
1166/*
1167 * rib_can_delete_dest
1168 *
1169 * Returns TRUE if the given dest can be deleted from the table.
1170 */
d62a17ae 1171static int rib_can_delete_dest(rib_dest_t *dest)
9fd92e3c 1172{
045207e2 1173 if (re_list_first(&dest->routes)) {
d62a17ae 1174 return 0;
1175 }
9fd92e3c 1176
c86ba6c2
DS
1177 /*
1178 * Unresolved rnh's are stored on the default route's list
1179 *
1180 * dest->rnode can also be the source prefix node in an
1181 * ipv6 sourcedest table. Fortunately the prefix of a
1182 * source prefix node can never be the default prefix.
1183 */
1184 if (is_default_prefix(&dest->rnode->p))
1185 return 0;
1186
d62a17ae 1187 /*
1188 * Don't delete the dest if we have to update the FPM about this
1189 * prefix.
1190 */
1191 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
1192 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
1193 return 0;
5adc2528 1194
d62a17ae 1195 return 1;
9fd92e3c
AS
1196}
1197
699dae23
DS
1198void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq)
1199{
1200 rib_dest_t *dest = rib_dest_from_rnode(rn);
699dae23
DS
1201 struct rnh *rnh;
1202
1203 /*
50872b08 1204 * We are storing the rnh's associated withb
699dae23
DS
1205 * the tracked nexthop as a list of the rn's.
1206 * Unresolved rnh's are placed at the top
1207 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
1208 * As such for each rn we need to walk up the tree
1209 * and see if any rnh's need to see if they
1210 * would match a more specific route
1211 */
1212 while (rn) {
50872b08
DS
1213 if (IS_ZEBRA_DEBUG_NHT_DETAILED) {
1214 char buf[PREFIX_STRLEN];
1215
1216 zlog_debug("%s: %s Being examined for Nexthop Tracking",
1217 __PRETTY_FUNCTION__,
1218 srcdest_rnode2str(rn, buf, sizeof(buf)));
1219 }
699dae23
DS
1220 if (!dest) {
1221 rn = rn->parent;
1222 if (rn)
1223 dest = rib_dest_from_rnode(rn);
1224 continue;
1225 }
1226 /*
1227 * If we have any rnh's stored in the nht list
1228 * then we know that this route node was used for
1229 * nht resolution and as such we need to call the
1230 * nexthop tracking evaluation code
1231 */
81fddbe7 1232 frr_each (rnh_list, &dest->nht, rnh) {
699dae23
DS
1233 struct zebra_vrf *zvrf =
1234 zebra_vrf_lookup_by_id(rnh->vrf_id);
1235 struct prefix *p = &rnh->node->p;
1236
50872b08 1237 if (IS_ZEBRA_DEBUG_NHT_DETAILED) {
699dae23
DS
1238 char buf1[PREFIX_STRLEN];
1239 char buf2[PREFIX_STRLEN];
1240
1241 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
1242 zvrf->vrf->vrf_id,
50872b08 1243 srcdest_rnode2str(rn, buf1,
699dae23
DS
1244 sizeof(buf1)),
1245 prefix2str(p, buf2, sizeof(buf2)),
1246 seq, rnh->seqno);
1247 }
1248
1249 /*
1250 * If we have evaluated this node on this pass
1251 * already, due to following the tree up
1252 * then we know that we can move onto the next
1253 * rnh to process.
1254 *
1255 * Additionally we call zebra_evaluate_rnh
1256 * when we gc the dest. In this case we know
1257 * that there must be no other re's where
1258 * we were originally as such we know that
1259 * that sequence number is ok to respect.
1260 */
50872b08
DS
1261 if (rnh->seqno == seq) {
1262 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
1263 zlog_debug(
1264 "\tNode processed and moved already");
699dae23 1265 continue;
50872b08 1266 }
699dae23
DS
1267
1268 rnh->seqno = seq;
1269 zebra_evaluate_rnh(zvrf, family2afi(p->family), 0,
1270 rnh->type, p);
1271 }
1272
1273 rn = rn->parent;
1274 if (rn)
1275 dest = rib_dest_from_rnode(rn);
1276 }
1277}
1278
9fd92e3c
AS
1279/*
1280 * rib_gc_dest
1281 *
1282 * Garbage collect the rib dest corresponding to the given route node
1283 * if appropriate.
1284 *
1285 * Returns TRUE if the dest was deleted, FALSE otherwise.
1286 */
d62a17ae 1287int rib_gc_dest(struct route_node *rn)
9fd92e3c 1288{
d62a17ae 1289 rib_dest_t *dest;
9fd92e3c 1290
d62a17ae 1291 dest = rib_dest_from_rnode(rn);
1292 if (!dest)
1293 return 0;
9fd92e3c 1294
d62a17ae 1295 if (!rib_can_delete_dest(dest))
1296 return 0;
9fd92e3c 1297
c9abf558
DS
1298 if (IS_ZEBRA_DEBUG_RIB) {
1299 struct zebra_vrf *zvrf;
1300
1301 zvrf = rib_dest_vrf(dest);
d62a17ae 1302 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
c9abf558 1303 }
9fd92e3c 1304
699dae23
DS
1305 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence());
1306
d62a17ae 1307 dest->rnode = NULL;
aa57abfb 1308 rnh_list_fini(&dest->nht);
d62a17ae 1309 XFREE(MTYPE_RIB_DEST, dest);
1310 rn->info = NULL;
9fd92e3c 1311
d62a17ae 1312 /*
1313 * Release the one reference that we keep on the route node.
1314 */
1315 route_unlock_node(rn);
1316 return 1;
9fd92e3c
AS
1317}
1318
d62a17ae 1319static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1320 struct route_entry *new)
3e5c6e00 1321{
d62a17ae 1322 hook_call(rib_update, rn, "new route selected");
3e5c6e00 1323
d62a17ae 1324 /* Update real nexthop. This may actually determine if nexthop is active
1325 * or not. */
6883bf8d 1326 if (!nexthop_group_active_nexthop_num(&new->ng)) {
d62a17ae 1327 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1328 return;
1329 }
3e5c6e00 1330
d62a17ae 1331 if (IS_ZEBRA_DEBUG_RIB) {
1332 char buf[SRCDEST2STR_BUFFER];
1333 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
1334 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1335 zvrf_id(zvrf), buf, rn, new,
1336 zebra_route_string(new->type));
d62a17ae 1337 }
3e5c6e00 1338
d62a17ae 1339 /* If labeled-unicast route, install transit LSP. */
1340 if (zebra_rib_labeled_unicast(new))
1341 zebra_mpls_lsp_install(zvrf, rn, new);
a64448ba 1342
3cdba47a 1343 rib_install_kernel(rn, new, NULL);
3e5c6e00 1344
d62a17ae 1345 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1346}
1347
d62a17ae 1348static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1349 struct route_entry *old)
3e5c6e00 1350{
d62a17ae 1351 hook_call(rib_update, rn, "removing existing route");
3e5c6e00 1352
d62a17ae 1353 /* Uninstall from kernel. */
1354 if (IS_ZEBRA_DEBUG_RIB) {
1355 char buf[SRCDEST2STR_BUFFER];
1356 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
1357 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1358 zvrf_id(zvrf), buf, rn, old,
1359 zebra_route_string(old->type));
d62a17ae 1360 }
3e5c6e00 1361
d62a17ae 1362 /* If labeled-unicast route, uninstall transit LSP. */
1363 if (zebra_rib_labeled_unicast(old))
1364 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1365
3cdba47a 1366 rib_uninstall_kernel(rn, old);
d62a17ae 1367
1368 /* Update nexthop for route, reset changed flag. */
ef57f35f
DL
1369 /* Note: this code also handles the Linux case when an interface goes
1370 * down, causing the kernel to delete routes without sending DELROUTE
1371 * notifications
1372 */
6883bf8d 1373 if (RIB_KERNEL_ROUTE(old))
212df1de
PG
1374 SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
1375 else
1376 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
d62a17ae 1377}
1378
1379static void rib_process_update_fib(struct zebra_vrf *zvrf,
1380 struct route_node *rn,
1381 struct route_entry *old,
1382 struct route_entry *new)
1383{
d62a17ae 1384 int nh_active = 0;
d62a17ae 1385
1386 /*
1387 * We have to install or update if a new route has been selected or
1388 * something has changed.
1389 */
1390 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1391 hook_call(rib_update, rn, "updating existing route");
1392
1393 /* Update the nexthop; we could determine here that nexthop is
1394 * inactive. */
6883bf8d 1395 if (nexthop_group_active_nexthop_num(&new->ng))
d62a17ae 1396 nh_active = 1;
1397
1398 /* If nexthop is active, install the selected route, if
1399 * appropriate. If
1400 * the install succeeds, cleanup flags for prior route, if
1401 * different from
1402 * newly selected.
1403 */
1404 if (nh_active) {
1405 if (IS_ZEBRA_DEBUG_RIB) {
1406 char buf[SRCDEST2STR_BUFFER];
1407 srcdest_rnode2str(rn, buf, sizeof(buf));
1408 if (new != old)
1409 zlog_debug(
2da33d6b 1410 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
d62a17ae 1411 zvrf_id(zvrf), buf, rn, new,
2da33d6b
DS
1412 zebra_route_string(new->type),
1413 old,
1414 zebra_route_string(old->type));
d62a17ae 1415 else
1416 zlog_debug(
2da33d6b 1417 "%u:%s: Updating route rn %p, re %p (%s)",
d62a17ae 1418 zvrf_id(zvrf), buf, rn, new,
2da33d6b 1419 zebra_route_string(new->type));
d62a17ae 1420 }
1421
1422 /* If labeled-unicast route, uninstall transit LSP. */
1423 if (zebra_rib_labeled_unicast(old))
1424 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1425
3cdba47a
DS
1426 /*
1427 * Non-system route should be installed.
1428 * If labeled-unicast route, install transit
1429 * LSP.
1430 */
1431 if (zebra_rib_labeled_unicast(new))
1432 zebra_mpls_lsp_install(zvrf, rn, new);
d62a17ae 1433
3cdba47a 1434 rib_install_kernel(rn, new, old);
d62a17ae 1435 }
a64448ba 1436
d62a17ae 1437 /*
1438 * If nexthop for selected route is not active or install
1439 * failed, we
1440 * may need to uninstall and delete for redistribution.
1441 */
ed216282 1442 if (!nh_active) {
d62a17ae 1443 if (IS_ZEBRA_DEBUG_RIB) {
1444 char buf[SRCDEST2STR_BUFFER];
1445 srcdest_rnode2str(rn, buf, sizeof(buf));
1446 if (new != old)
1447 zlog_debug(
2da33d6b 1448 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
d62a17ae 1449 zvrf_id(zvrf), buf, rn, new,
2da33d6b
DS
1450 zebra_route_string(new->type),
1451 old,
1452 zebra_route_string(old->type));
d62a17ae 1453 else
1454 zlog_debug(
2da33d6b 1455 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
d62a17ae 1456 zvrf_id(zvrf), buf, rn, new,
2da33d6b 1457 zebra_route_string(new->type));
d62a17ae 1458 }
1459
1460 /* If labeled-unicast route, uninstall transit LSP. */
1461 if (zebra_rib_labeled_unicast(old))
1462 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1463
3cdba47a 1464 rib_uninstall_kernel(rn, old);
d62a17ae 1465 }
1466 } else {
1467 /*
1468 * Same route selected; check if in the FIB and if not,
3cdba47a
DS
1469 * re-install. This is housekeeping code to deal with
1470 * race conditions in kernel with linux netlink reporting
1471 * interface up before IPv4 or IPv6 protocol is ready
d62a17ae 1472 * to add routes.
1473 */
3cdba47a
DS
1474 if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) ||
1475 RIB_SYSTEM_ROUTE(new))
677c1dd5 1476 rib_install_kernel(rn, new, NULL);
d62a17ae 1477 }
1478
1479 /* Update prior route. */
6883bf8d 1480 if (new != old)
d62a17ae 1481 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1482
d62a17ae 1483 /* Clear changed flag. */
1484 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1485}
1486
d62a17ae 1487/* Check if 'alternate' RIB entry is better than 'current'. */
1488static struct route_entry *rib_choose_best(struct route_entry *current,
1489 struct route_entry *alternate)
1490{
1491 if (current == NULL)
1492 return alternate;
1493
1494 /* filter route selection in following order:
1495 * - connected beats other types
fec4ca19 1496 * - if both connected, loopback or vrf wins
d62a17ae 1497 * - lower distance beats higher
1498 * - lower metric beats higher for equal distance
1499 * - last, hence oldest, route wins tie break.
1500 */
1501
fec4ca19
DS
1502 /* Connected routes. Check to see if either are a vrf
1503 * or loopback interface. If not, pick the last connected
d62a17ae 1504 * route of the set of lowest metric connected routes.
1505 */
1506 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
fec4ca19
DS
1507 if (current->type != ZEBRA_ROUTE_CONNECT)
1508 return alternate;
1509
1510 /* both are connected. are either loop or vrf? */
1511 struct nexthop *nexthop = NULL;
1512
1513 for (ALL_NEXTHOPS(alternate->ng, nexthop)) {
1514 if (if_is_loopback_or_vrf(if_lookup_by_index(
1515 nexthop->ifindex, alternate->vrf_id)))
1516 return alternate;
1517 }
1518
1519 for (ALL_NEXTHOPS(current->ng, nexthop)) {
1520 if (if_is_loopback_or_vrf(if_lookup_by_index(
1521 nexthop->ifindex, current->vrf_id)))
1522 return current;
1523 }
1524
1525 /* Neither are loop or vrf so pick best metric */
1526 if (alternate->metric <= current->metric)
d62a17ae 1527 return alternate;
1528
1529 return current;
1530 }
3e5c6e00 1531
d62a17ae 1532 if (current->type == ZEBRA_ROUTE_CONNECT)
1533 return current;
3e5c6e00 1534
d62a17ae 1535 /* higher distance loses */
1536 if (alternate->distance < current->distance)
1537 return alternate;
1538 if (current->distance < alternate->distance)
1539 return current;
3e5c6e00 1540
d62a17ae 1541 /* metric tie-breaks equal distance */
1542 if (alternate->metric <= current->metric)
1543 return alternate;
3e5c6e00 1544
d62a17ae 1545 return current;
3e5c6e00 1546}
1547
d62a17ae 1548/* Core function for processing routing information base. */
1549static void rib_process(struct route_node *rn)
1550{
1551 struct route_entry *re;
1552 struct route_entry *next;
1553 struct route_entry *old_selected = NULL;
1554 struct route_entry *new_selected = NULL;
1555 struct route_entry *old_fib = NULL;
1556 struct route_entry *new_fib = NULL;
1557 struct route_entry *best = NULL;
1558 char buf[SRCDEST2STR_BUFFER];
1559 rib_dest_t *dest;
1560 struct zebra_vrf *zvrf = NULL;
86391e56
MS
1561 const struct prefix *p, *src_p;
1562
d62a17ae 1563 srcdest_rnode_prefixes(rn, &p, &src_p);
1564 vrf_id_t vrf_id = VRF_UNKNOWN;
1565
1566 assert(rn);
1567
1568 dest = rib_dest_from_rnode(rn);
1569 if (dest) {
1570 zvrf = rib_dest_vrf(dest);
1571 vrf_id = zvrf_id(zvrf);
1572 }
bab85d4f 1573
d62a17ae 1574 if (IS_ZEBRA_DEBUG_RIB)
1575 srcdest_rnode2str(rn, buf, sizeof(buf));
bab85d4f 1576
d62a17ae 1577 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1578 zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn);
bab85d4f 1579
607425e5
DS
1580 /*
1581 * we can have rn's that have a NULL info pointer
1582 * (dest). As such let's not let the deref happen
1583 * additionally we know RNODE_FOREACH_RE_SAFE
1584 * will not iterate so we are ok.
1585 */
1586 if (dest)
1587 old_fib = dest->selected_fib;
5f7a4718 1588
a2addae8 1589 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1590 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1591 zlog_debug(
2da33d6b
DS
1592 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1593 vrf_id, buf, re, zebra_route_string(re->type),
1594 re->status, re->flags, re->distance,
1595 re->metric);
d62a17ae 1596
1597 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1598
1599 /* Currently selected re. */
1600 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1601 assert(old_selected == NULL);
1602 old_selected = re;
1603 }
bab85d4f 1604
d62a17ae 1605 /* Skip deleted entries from selection */
1606 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1607 continue;
1608
1609 /* Skip unreachable nexthop. */
1610 /* This first call to nexthop_active_update is merely to
6883bf8d
DS
1611 * determine if there's any change to nexthops associated
1612 * with this RIB entry. Now, rib_process() can be invoked due
1613 * to an external event such as link down or due to
1614 * next-hop-tracking evaluation. In the latter case,
d62a17ae 1615 * a decision has already been made that the NHs have changed.
6883bf8d
DS
1616 * So, no need to invoke a potentially expensive call again.
1617 * Further, since the change might be in a recursive NH which
1618 * is not caught in the nexthop_active_update() code. Thus, we
1619 * might miss changes to recursive NHs.
d62a17ae 1620 */
6883bf8d 1621 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
99eabcec 1622 && !nexthop_active_update(rn, re)) {
d62a17ae 1623 if (re->type == ZEBRA_ROUTE_TABLE) {
1624 /* XXX: HERE BE DRAGONS!!!!!
1625 * In all honesty, I have not yet figured out
6883bf8d
DS
1626 * what this part does or why the
1627 * ROUTE_ENTRY_CHANGED test above is correct
d62a17ae 1628 * or why we need to delete a route here, and
6883bf8d
DS
1629 * also not whether this concerns both selected
1630 * and fib route, or only selected
1631 * or only fib
1632 *
1633 * This entry was denied by the 'ip protocol
1634 * table' route-map, we need to delete it */
d62a17ae 1635 if (re != old_selected) {
1636 if (IS_ZEBRA_DEBUG_RIB)
1637 zlog_debug(
32391aff 1638 "%s: %u:%s: imported via import-table but denied "
d62a17ae 1639 "by the ip protocol table route-map",
32391aff 1640 __func__, vrf_id, buf);
d62a17ae 1641 rib_unlink(rn, re);
1642 } else
1643 SET_FLAG(re->status,
1644 ROUTE_ENTRY_REMOVED);
1645 }
1646
1647 continue;
1648 }
bab85d4f 1649
d62a17ae 1650 /* Infinite distance. */
1651 if (re->distance == DISTANCE_INFINITY) {
1652 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1653 continue;
1654 }
bab85d4f 1655
d62a17ae 1656 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1657 best = rib_choose_best(new_fib, re);
1658 if (new_fib && best != new_fib)
1659 UNSET_FLAG(new_fib->status,
1660 ROUTE_ENTRY_CHANGED);
1661 new_fib = best;
1662 } else {
1663 best = rib_choose_best(new_selected, re);
1664 if (new_selected && best != new_selected)
1665 UNSET_FLAG(new_selected->status,
1666 ROUTE_ENTRY_CHANGED);
1667 new_selected = best;
1668 }
1669 if (best != re)
1670 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1671 } /* RNODE_FOREACH_RE */
1672
1673 /* If no FIB override route, use the selected route also for FIB */
1674 if (new_fib == NULL)
1675 new_fib = new_selected;
1676
1677 /* After the cycle is finished, the following pointers will be set:
1678 * old_selected --- RE entry currently having SELECTED
1679 * new_selected --- RE entry that is newly SELECTED
1680 * old_fib --- RE entry currently in kernel FIB
1681 * new_fib --- RE entry that is newly to be in kernel FIB
1682 *
1683 * new_selected will get SELECTED flag, and is going to be redistributed
1684 * the zclients. new_fib (which can be new_selected) will be installed
1685 * in kernel.
1686 */
1687
1688 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1689 zlog_debug(
1690 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1691 vrf_id, buf, (void *)old_selected, (void *)new_selected,
1692 (void *)old_fib, (void *)new_fib);
1693 }
446bb95e 1694
d62a17ae 1695 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1696 * fib == selected */
9d303b37
DL
1697 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1698 ROUTE_ENTRY_CHANGED);
d62a17ae 1699
1700 /* Update fib according to selection results */
1701 if (new_fib && old_fib)
1702 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1703 else if (new_fib)
1704 rib_process_add_fib(zvrf, rn, new_fib);
1705 else if (old_fib)
1706 rib_process_del_fib(zvrf, rn, old_fib);
1707
8cb41cd6 1708 /* Update SELECTED entry */
d62a17ae 1709 if (old_selected != new_selected || selected_changed) {
93bdadae 1710
6883bf8d 1711 if (new_selected && new_selected != new_fib)
d62a17ae 1712 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
41ec9222 1713
3cdba47a 1714 if (new_selected)
5af4b346
MS
1715 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1716
d62a17ae 1717 if (old_selected) {
1718 if (!new_selected)
1719 redistribute_delete(p, src_p, old_selected);
1720 if (old_selected != new_selected)
1721 UNSET_FLAG(old_selected->flags,
1722 ZEBRA_FLAG_SELECTED);
f857321e 1723 }
d62a17ae 1724 }
3e5c6e00 1725
d62a17ae 1726 /* Remove all RE entries queued for removal */
a2addae8 1727 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1728 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1729 if (IS_ZEBRA_DEBUG_RIB) {
1730 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1731 (void *)rn, (void *)re);
1732 }
1733 rib_unlink(rn, re);
1734 }
1735 }
4d38fdb4 1736
d62a17ae 1737 /*
1738 * Check if the dest can be deleted now.
1739 */
1740 rib_gc_dest(rn);
e96f9203
DO
1741}
1742
a1494c25
DS
1743static void zebra_rib_evaluate_mpls(struct route_node *rn)
1744{
1745 rib_dest_t *dest = rib_dest_from_rnode(rn);
1746 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
1747
1748 if (!dest)
1749 return;
1750
1751 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1752 if (IS_ZEBRA_DEBUG_MPLS)
1753 zlog_debug(
1754 "%u: Scheduling all LSPs upon RIB completion",
1755 zvrf_id(zvrf));
1756 zebra_mpls_lsp_schedule(zvrf);
1757 mpls_unmark_lsps_for_processing(rn);
1758 }
1759}
1760
e5ac2adf
MS
1761/*
1762 * Utility to match route with dplane context data
1763 */
1764static bool rib_route_match_ctx(const struct route_entry *re,
25779064
MS
1765 const struct zebra_dplane_ctx *ctx,
1766 bool is_update)
e5ac2adf
MS
1767{
1768 bool result = false;
1769
1770 if (is_update) {
1771 /*
1772 * In 'update' case, we test info about the 'previous' or
1773 * 'old' route
1774 */
1775 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1776 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1777 result = true;
1778
1779 /* TODO -- we're using this extra test, but it's not
1780 * exactly clear why.
1781 */
1782 if (re->type == ZEBRA_ROUTE_STATIC &&
1783 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1784 re->tag != dplane_ctx_get_old_tag(ctx))) {
1785 result = false;
1786 }
1787 }
1788
1789 } else {
1790 /*
1791 * Ordinary, single-route case using primary context info
1792 */
1793 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1794 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1795 /* Skip route that's been deleted */
1796 goto done;
1797 }
1798
1799 if ((re->type == dplane_ctx_get_type(ctx)) &&
1800 (re->instance == dplane_ctx_get_instance(ctx))) {
1801 result = true;
1802
1803 /* TODO -- we're using this extra test, but it's not
1804 * exactly clear why.
1805 */
1806 if (re->type == ZEBRA_ROUTE_STATIC &&
1807 (re->distance != dplane_ctx_get_distance(ctx) ||
1808 re->tag != dplane_ctx_get_tag(ctx))) {
1809 result = false;
1810 }
1811 }
1812 }
1813
1814done:
1815
1816 return (result);
1817}
1818
3cdba47a
DS
1819static void zebra_rib_fixup_system(struct route_node *rn)
1820{
1821 struct route_entry *re;
1822
1823 RNODE_FOREACH_RE(rn, re) {
1824 struct nexthop *nhop;
1825
1826 if (!RIB_SYSTEM_ROUTE(re))
1827 continue;
1828
1829 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1830 continue;
1831
1832 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1833
1834 for (ALL_NEXTHOPS(re->ng, nhop)) {
1835 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1836 continue;
1837
1838 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1839 }
1840 }
1841}
1842
e5ac2adf 1843/*
91f16812 1844 * Route-update results processing after async dataplane update.
e5ac2adf 1845 */
d37f4d6c 1846static void rib_process_result(struct zebra_dplane_ctx *ctx)
e5ac2adf
MS
1847{
1848 struct route_table *table = NULL;
97f5b441 1849 struct zebra_vrf *zvrf = NULL;
e5ac2adf
MS
1850 struct route_node *rn = NULL;
1851 struct route_entry *re = NULL, *old_re = NULL, *rib;
1852 bool is_update = false;
f183e380 1853 struct nexthop *nexthop, *ctx_nexthop;
97f5b441 1854 char dest_str[PREFIX_STRLEN] = "";
5709131c 1855 enum dplane_op_e op;
e5ac2adf
MS
1856 enum zebra_dplane_result status;
1857 const struct prefix *dest_pfx, *src_pfx;
699dae23 1858 uint32_t seq;
e5ac2adf
MS
1859
1860 /* Locate rn and re(s) from ctx */
1861
1862 table = zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx),
1863 dplane_ctx_get_safi(ctx),
1864 dplane_ctx_get_vrf(ctx),
1865 dplane_ctx_get_table(ctx));
1866 if (table == NULL) {
1867 if (IS_ZEBRA_DEBUG_DPLANE) {
fe2c53d4 1868 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
e5ac2adf
MS
1869 dplane_ctx_get_afi(ctx),
1870 dplane_ctx_get_safi(ctx),
1871 dplane_ctx_get_vrf(ctx));
1872 }
1873 goto done;
1874 }
1875
97f5b441
MS
1876 zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
1877
e5ac2adf
MS
1878 dest_pfx = dplane_ctx_get_dest(ctx);
1879
1880 /* Note well: only capturing the prefix string if debug is enabled here;
1881 * unconditional log messages will have to generate the string.
1882 */
5709131c 1883 if (IS_ZEBRA_DEBUG_DPLANE)
e5ac2adf 1884 prefix2str(dest_pfx, dest_str, sizeof(dest_str));
e5ac2adf
MS
1885
1886 src_pfx = dplane_ctx_get_src(ctx);
1887 rn = srcdest_rnode_get(table, dplane_ctx_get_dest(ctx),
5709131c 1888 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
e5ac2adf
MS
1889 if (rn == NULL) {
1890 if (IS_ZEBRA_DEBUG_DPLANE) {
fe2c53d4 1891 zlog_debug("Failed to process dplane results: no route for %u:%s",
e5ac2adf
MS
1892 dplane_ctx_get_vrf(ctx), dest_str);
1893 }
1894 goto done;
1895 }
1896
1897 srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx);
1898
1899 op = dplane_ctx_get_op(ctx);
1900 status = dplane_ctx_get_status(ctx);
1901
c831033f 1902 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
f183e380 1903 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
e5ac2adf 1904 dplane_ctx_get_vrf(ctx), dest_str, ctx,
f183e380 1905 dplane_op2str(op), dplane_res2str(status));
e5ac2adf 1906
e5ac2adf
MS
1907 /*
1908 * Update is a bit of a special case, where we may have both old and new
1909 * routes to post-process.
1910 */
1911 is_update = dplane_ctx_is_update(ctx);
1912
1913 /*
1914 * Take a pass through the routes, look for matches with the context
1915 * info.
1916 */
1917 RNODE_FOREACH_RE(rn, rib) {
1918
1919 if (re == NULL) {
5709131c 1920 if (rib_route_match_ctx(rib, ctx, false))
e5ac2adf 1921 re = rib;
e5ac2adf
MS
1922 }
1923
1924 /* Check for old route match */
1925 if (is_update && (old_re == NULL)) {
5709131c 1926 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
e5ac2adf 1927 old_re = rib;
e5ac2adf
MS
1928 }
1929
1930 /* Have we found the routes we need to work on? */
5709131c 1931 if (re && ((!is_update || old_re)))
e5ac2adf 1932 break;
e5ac2adf
MS
1933 }
1934
699dae23
DS
1935 seq = dplane_ctx_get_seq(ctx);
1936
e5ac2adf
MS
1937 /*
1938 * Check sequence number(s) to detect stale results before continuing
1939 */
60f98b23 1940 if (re) {
699dae23 1941 if (re->dplane_sequence != seq) {
60f98b23
DS
1942 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1943 zlog_debug("%u:%s Stale dplane result for re %p",
1944 dplane_ctx_get_vrf(ctx),
1945 dest_str, re);
1946 } else
1947 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
1948 }
1949
60f98b23
DS
1950 if (old_re) {
1951 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
1952 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1953 zlog_debug("%u:%s Stale dplane result for old_re %p",
1954 dplane_ctx_get_vrf(ctx),
1955 dest_str, old_re);
1956 } else
9ef0c6ba 1957 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
1958 }
1959
12e7fe3a 1960 switch (op) {
12e7fe3a
DS
1961 case DPLANE_OP_ROUTE_INSTALL:
1962 case DPLANE_OP_ROUTE_UPDATE:
1963 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
1964 if (re) {
1965 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
1966 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1967 }
b9f0e5ee
DS
1968 /*
1969 * On an update operation from the same route type
1970 * context retrieval currently has no way to know
1971 * which was the old and which was the new.
1972 * So don't unset our flags that we just set.
1973 * We know redistribution is ok because the
1974 * old_re in this case is used for nothing
1975 * more than knowing whom to contact if necessary.
1976 */
1977 if (old_re && old_re != re) {
677c1dd5
DS
1978 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
1979 UNSET_FLAG(old_re->status,
1980 ROUTE_ENTRY_INSTALLED);
1981 }
12e7fe3a
DS
1982 /* Update zebra nexthop FIB flag for each
1983 * nexthop that was installed.
1984 */
1985 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
1986 ctx_nexthop)) {
f183e380 1987
677c1dd5
DS
1988 if (!re)
1989 continue;
1990
12e7fe3a
DS
1991 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1992 if (nexthop_same(ctx_nexthop, nexthop))
1993 break;
1994 }
1995
1996 if (nexthop == NULL)
1997 continue;
1998
1999 if (CHECK_FLAG(nexthop->flags,
2000 NEXTHOP_FLAG_RECURSIVE))
2001 continue;
2002
2003 if (CHECK_FLAG(ctx_nexthop->flags,
2004 NEXTHOP_FLAG_FIB))
2005 SET_FLAG(nexthop->flags,
2006 NEXTHOP_FLAG_FIB);
2007 else
2008 UNSET_FLAG(nexthop->flags,
2009 NEXTHOP_FLAG_FIB);
f183e380
MS
2010 }
2011
3cdba47a
DS
2012 /*
2013 * System routes are weird in that they
2014 * allow multiple to be installed that match
2015 * to the same prefix, so after we get the
2016 * result we need to clean them up so that
2017 * we can actually use them.
2018 */
2019 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2020 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2021 zebra_rib_fixup_system(rn);
2022
f4c6e2a8 2023 if (zvrf)
12e7fe3a 2024 zvrf->installs++;
f183e380 2025
12e7fe3a
DS
2026 /* Redistribute */
2027 /*
2028 * TODO -- still calling the redist api using the
2029 * route_entries, and there's a corner-case here:
2030 * if there's no client for the 'new' route, a redist
2031 * deleting the 'old' route will be sent. But if the
2032 * 'old' context info was stale, 'old_re' will be
2033 * NULL here and that delete will not be sent.
2034 */
677c1dd5
DS
2035 if (re)
2036 redistribute_update(dest_pfx, src_pfx,
2037 re, old_re);
e5ac2adf 2038
12e7fe3a 2039 /* Notify route owner */
677c1dd5 2040 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
e5ac2adf 2041
12e7fe3a 2042 } else {
7a230a9d 2043 if (re) {
677c1dd5 2044 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
7a230a9d
DS
2045 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2046 } if (old_re)
677c1dd5
DS
2047 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2048 if (re)
2049 zsend_route_notify_owner(re, dest_pfx,
2050 ZAPI_ROUTE_FAIL_INSTALL);
97f5b441 2051
12e7fe3a
DS
2052 zlog_warn("%u:%s: Route install failed",
2053 dplane_ctx_get_vrf(ctx),
2054 prefix2str(dest_pfx,
2055 dest_str, sizeof(dest_str)));
2056 }
2057 break;
2058 case DPLANE_OP_ROUTE_DELETE:
677c1dd5
DS
2059 if (re)
2060 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2061 /*
2062 * In the delete case, the zebra core datastructs were
2063 * updated (or removed) at the time the delete was issued,
2064 * so we're just notifying the route owner.
e5ac2adf 2065 */
12e7fe3a 2066 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2067 if (re) {
2068 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2069 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2070 }
12e7fe3a 2071 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
e5ac2adf 2072
12e7fe3a
DS
2073 if (zvrf)
2074 zvrf->removals++;
2075 } else {
677c1dd5
DS
2076 if (re)
2077 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2078 zsend_route_notify_owner_ctx(ctx,
2079 ZAPI_ROUTE_REMOVE_FAIL);
e5ac2adf 2080
12e7fe3a
DS
2081 zlog_warn("%u:%s: Route Deletion failure",
2082 dplane_ctx_get_vrf(ctx),
2083 prefix2str(dest_pfx,
2084 dest_str, sizeof(dest_str)));
2085 }
3cdba47a
DS
2086
2087 /*
2088 * System routes are weird in that they
2089 * allow multiple to be installed that match
2090 * to the same prefix, so after we get the
2091 * result we need to clean them up so that
2092 * we can actually use them.
2093 */
2094 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2095 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2096 zebra_rib_fixup_system(rn);
12e7fe3a 2097 break;
d37f4d6c
MS
2098 default:
2099 break;
e5ac2adf 2100 }
699dae23
DS
2101
2102 zebra_rib_evaluate_rn_nexthops(rn, seq);
a1494c25 2103 zebra_rib_evaluate_mpls(rn);
e5ac2adf
MS
2104done:
2105
5f27bcba
DS
2106 if (rn)
2107 route_unlock_node(rn);
2108
e5ac2adf
MS
2109 /* Return context to dataplane module */
2110 dplane_ctx_fini(&ctx);
2111}
2112
5110a0c6 2113/* Take a list of route_node structs and return 1, if there was a record
d62a17ae 2114 * picked from it and processed by rib_process(). Don't process more,
5110a0c6 2115 * than one RN record; operate only in the specified sub-queue.
e96f9203 2116 */
d7c0a89a 2117static unsigned int process_subq(struct list *subq, uint8_t qindex)
e96f9203 2118{
d62a17ae 2119 struct listnode *lnode = listhead(subq);
2120 struct route_node *rnode;
2121 rib_dest_t *dest;
2122 struct zebra_vrf *zvrf = NULL;
5110a0c6 2123
d62a17ae 2124 if (!lnode)
2125 return 0;
5110a0c6 2126
d62a17ae 2127 rnode = listgetdata(lnode);
2128 dest = rib_dest_from_rnode(rnode);
2129 if (dest)
2130 zvrf = rib_dest_vrf(dest);
41ec9222 2131
d62a17ae 2132 rib_process(rnode);
5110a0c6 2133
d62a17ae 2134 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2135 char buf[SRCDEST2STR_BUFFER];
2136 srcdest_rnode2str(rnode, buf, sizeof(buf));
2137 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2138 zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
2139 }
41ec9222 2140
d62a17ae 2141 if (rnode->info)
2142 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2143 RIB_ROUTE_QUEUED(qindex));
9fd92e3c 2144
67b9467f 2145#if 0
5110a0c6
SH
2146 else
2147 {
2148 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2149 __func__, rnode, rnode->lock);
2150 zlog_backtrace(LOG_DEBUG);
2151 }
67b9467f 2152#endif
d62a17ae 2153 route_unlock_node(rnode);
2154 list_delete_node(subq, lnode);
2155 return 1;
e96f9203
DO
2156}
2157
a1494c25 2158
fb018d25 2159/*
f183e380 2160 * Perform next-hop tracking processing after RIB updates.
fb018d25 2161 */
f183e380 2162static void do_nht_processing(void)
fb018d25 2163{
fb018d25
DS
2164}
2165
e96f9203 2166/* Dispatch the meta queue by picking, processing and unlocking the next RN from
d62a17ae 2167 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2168 * data
e96f9203
DO
2169 * is pointed to the meta queue structure.
2170 */
d62a17ae 2171static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
e96f9203 2172{
d62a17ae 2173 struct meta_queue *mq = data;
2174 unsigned i;
91f16812
MS
2175 uint32_t queue_len, queue_limit;
2176
2177 /* Ensure there's room for more dataplane updates */
2178 queue_limit = dplane_get_in_queue_limit();
2179 queue_len = dplane_get_in_queue_len();
2180 if (queue_len > queue_limit) {
2181 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
8b962e77
MS
2182 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2183 queue_len, queue_limit);
91f16812
MS
2184
2185 /* Ensure that the meta-queue is actually enqueued */
489a9614 2186 if (work_queue_empty(zrouter.ribq))
ea45a4e7 2187 work_queue_add(zrouter.ribq, zrouter.mq);
91f16812
MS
2188
2189 return WQ_QUEUE_BLOCKED;
2190 }
5110a0c6 2191
d62a17ae 2192 for (i = 0; i < MQ_SIZE; i++)
2193 if (process_subq(mq->subq[i], i)) {
2194 mq->size--;
2195 break;
2196 }
2197 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
e96f9203
DO
2198}
2199
f52ed677
DS
2200
2201/*
2202 * Look into the RN and queue it into the highest priority queue
2203 * at this point in time for processing.
2204 *
2205 * We will enqueue a route node only once per invocation.
2206 *
2207 * There are two possibilities here that should be kept in mind.
2208 * If the original invocation has not been pulled off for processing
2209 * yet, A subsuquent invocation can have a route entry with a better
2210 * meta queue index value and we can have a situation where
2211 * we might have the same node enqueued 2 times. Not necessarily
2212 * an optimal situation but it should be ok.
2213 *
2214 * The other possibility is that the original invocation has not
2215 * been pulled off for processing yet, A subsusquent invocation
2216 * doesn't have a route_entry with a better meta-queue and the
2217 * original metaqueue index value will win and we'll end up with
2218 * the route node enqueued once.
e96f9203 2219 */
d62a17ae 2220static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
e96f9203 2221{
f52ed677
DS
2222 struct route_entry *re = NULL, *curr_re = NULL;
2223 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
5110a0c6 2224
f52ed677
DS
2225 RNODE_FOREACH_RE (rn, curr_re) {
2226 curr_qindex = route_info[curr_re->type].meta_q_map;
d62a17ae 2227
f52ed677
DS
2228 if (curr_qindex <= qindex) {
2229 re = curr_re;
2230 qindex = curr_qindex;
d62a17ae 2231 }
f52ed677 2232 }
5110a0c6 2233
f52ed677
DS
2234 if (!re)
2235 return;
5110a0c6 2236
f52ed677
DS
2237 /* Invariant: at this point we always have rn->info set. */
2238 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2239 RIB_ROUTE_QUEUED(qindex))) {
d62a17ae 2240 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2241 rnode_debug(rn, re->vrf_id,
f52ed677 2242 "rn %p is already queued in sub-queue %u",
d62a17ae 2243 (void *)rn, qindex);
f52ed677 2244 return;
d62a17ae 2245 }
f52ed677
DS
2246
2247 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
2248 listnode_add(mq->subq[qindex], rn);
2249 route_lock_node(rn);
2250 mq->size++;
2251
2252 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2253 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %u",
2254 (void *)rn, qindex);
4d38fdb4 2255}
2256
6d691129 2257/* Add route_node to work queue and schedule processing */
d62a17ae 2258void rib_queue_add(struct route_node *rn)
4d38fdb4 2259{
d62a17ae 2260 assert(rn);
fc328ac9 2261
d62a17ae 2262 /* Pointless to queue a route_node with no RIB entries to add or remove
2263 */
2264 if (!rnode_to_ribs(rn)) {
2265 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2266 __func__, (void *)rn, rn->lock);
2267 zlog_backtrace(LOG_DEBUG);
2268 return;
2269 }
4d38fdb4 2270
489a9614 2271 if (zrouter.ribq == NULL) {
e914ccbe 2272 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2273 "%s: work_queue does not exist!", __func__);
d62a17ae 2274 return;
2275 }
2276
2277 /*
2278 * The RIB queue should normally be either empty or holding the only
2279 * work_queue_item element. In the latter case this element would
2280 * hold a pointer to the meta queue structure, which must be used to
2281 * actually queue the route nodes to process. So create the MQ
2282 * holder, if necessary, then push the work into it in any case.
2283 * This semantics was introduced after 0.99.9 release.
2284 */
489a9614 2285 if (work_queue_empty(zrouter.ribq))
ea45a4e7 2286 work_queue_add(zrouter.ribq, zrouter.mq);
e96f9203 2287
ea45a4e7 2288 rib_meta_queue_add(zrouter.mq, rn);
fc328ac9 2289
d62a17ae 2290 return;
4d38fdb4 2291}
2292
5110a0c6
SH
2293/* Create new meta queue.
2294 A destructor function doesn't seem to be necessary here.
2295 */
d62a17ae 2296static struct meta_queue *meta_queue_new(void)
e96f9203 2297{
d62a17ae 2298 struct meta_queue *new;
2299 unsigned i;
5110a0c6 2300
d62a17ae 2301 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
e96f9203 2302
d62a17ae 2303 for (i = 0; i < MQ_SIZE; i++) {
2304 new->subq[i] = list_new();
2305 assert(new->subq[i]);
2306 }
5110a0c6 2307
d62a17ae 2308 return new;
e96f9203
DO
2309}
2310
d62a17ae 2311void meta_queue_free(struct meta_queue *mq)
5a8dfcd8 2312{
d62a17ae 2313 unsigned i;
5a8dfcd8 2314
d62a17ae 2315 for (i = 0; i < MQ_SIZE; i++)
6a154c88 2316 list_delete(&mq->subq[i]);
5a8dfcd8 2317
d62a17ae 2318 XFREE(MTYPE_WORK_QUEUE, mq);
5a8dfcd8
RW
2319}
2320
4d38fdb4 2321/* initialise zebra rib work queue */
2561d12e 2322static void rib_queue_init(void)
4d38fdb4 2323{
489a9614
DS
2324 if (!(zrouter.ribq = work_queue_new(zrouter.master,
2325 "route_node processing"))) {
e914ccbe 2326 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2327 "%s: could not initialise work queue!", __func__);
d62a17ae 2328 return;
2329 }
4d38fdb4 2330
d62a17ae 2331 /* fill in the work queue spec */
489a9614
DS
2332 zrouter.ribq->spec.workfunc = &meta_queue_process;
2333 zrouter.ribq->spec.errorfunc = NULL;
46a4e345 2334 zrouter.ribq->spec.completion_func = NULL;
d62a17ae 2335 /* XXX: TODO: These should be runtime configurable via vty */
489a9614
DS
2336 zrouter.ribq->spec.max_retries = 3;
2337 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
2338 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
d62a17ae 2339
ea45a4e7 2340 if (!(zrouter.mq = meta_queue_new())) {
e914ccbe 2341 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2342 "%s: could not initialise meta queue!", __func__);
d62a17ae 2343 return;
2344 }
2345 return;
718e3744 2346}
2347
434434f7
DS
2348rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
2349{
2350 rib_dest_t *dest;
2351
2352 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
aa57abfb 2353 rnh_list_init(&dest->nht);
434434f7
DS
2354 route_lock_node(rn); /* rn route table reference */
2355 rn->info = dest;
2356 dest->rnode = rn;
2357
2358 return dest;
2359}
2360
6d691129
PJ
2361/* RIB updates are processed via a queue of pointers to route_nodes.
2362 *
2363 * The queue length is bounded by the maximal size of the routing table,
2364 * as a route_node will not be requeued, if already queued.
2365 *
f0f77c9a
DS
2366 * REs are submitted via rib_addnode or rib_delnode which set minimal
2367 * state, or static_install_route (when an existing RE is updated)
3c0755dc 2368 * and then submit route_node to queue for best-path selection later.
f0f77c9a 2369 * Order of add/delete state changes are preserved for any given RE.
6d691129 2370 *
f0f77c9a 2371 * Deleted REs are reaped during best-path selection.
6d691129
PJ
2372 *
2373 * rib_addnode
f0f77c9a
DS
2374 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2375 * |-------->| | best RE, if required
3c0755dc
PJ
2376 * | |
2377 * static_install->|->rib_addqueue...... -> rib_process
2378 * | |
2379 * |-------->| |-> rib_unlink
f0f77c9a
DS
2380 * |-> set ROUTE_ENTRY_REMOVE |
2381 * rib_delnode (RE freed)
6d691129 2382 *
9fd92e3c
AS
2383 * The 'info' pointer of a route_node points to a rib_dest_t
2384 * ('dest'). Queueing state for a route_node is kept on the dest. The
2385 * dest is created on-demand by rib_link() and is kept around at least
2386 * as long as there are ribs hanging off it (@see rib_gc_dest()).
d62a17ae 2387 *
6d691129
PJ
2388 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2389 *
2390 * - route_nodes: refcounted by:
9fd92e3c
AS
2391 * - dest attached to route_node:
2392 * - managed by: rib_link/rib_gc_dest
6d691129
PJ
2393 * - route_node processing queue
2394 * - managed by: rib_addqueue, rib_process.
2395 *
2396 */
d62a17ae 2397
f0f77c9a 2398/* Add RE to head of the route node. */
d62a17ae 2399static void rib_link(struct route_node *rn, struct route_entry *re, int process)
2400{
d62a17ae 2401 rib_dest_t *dest;
2402 afi_t afi;
2403 const char *rmap_name;
9fd92e3c 2404
d62a17ae 2405 assert(re && rn);
9fd92e3c 2406
d62a17ae 2407 dest = rib_dest_from_rnode(rn);
2408 if (!dest) {
2409 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2410 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
7a4bb9c5 2411
434434f7 2412 dest = zebra_rib_create_dest(rn);
d62a17ae 2413 }
2263a412 2414
045207e2 2415 re_list_add_head(&dest->routes, re);
d62a17ae 2416
2417 afi = (rn->p.family == AF_INET)
2418 ? AFI_IP
2419 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2420 if (is_zebra_import_table_enabled(afi, re->table)) {
2421 rmap_name = zebra_get_import_table_route_map(afi, re->table);
2422 zebra_add_import_table_entry(rn, re, rmap_name);
2423 } else if (process)
2424 rib_queue_add(rn);
2425}
2426
7e24fdf3
DS
2427static void rib_addnode(struct route_node *rn,
2428 struct route_entry *re, int process)
d62a17ae 2429{
2430 /* RE node has been un-removed before route-node is processed.
2431 * route_node must hence already be on the queue for processing..
2432 */
2433 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2434 if (IS_ZEBRA_DEBUG_RIB)
2435 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
2436 (void *)rn, (void *)re);
2437
2438 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2439 return;
2440 }
2441 rib_link(rn, re, process);
6d691129
PJ
2442}
2443
9fd92e3c
AS
2444/*
2445 * rib_unlink
2446 *
2447 * Detach a rib structure from a route_node.
2448 *
2449 * Note that a call to rib_unlink() should be followed by a call to
2450 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2451 * longer required to be deleted.
2452 */
d62a17ae 2453void rib_unlink(struct route_node *rn, struct route_entry *re)
6d691129 2454{
d62a17ae 2455 rib_dest_t *dest;
9fd92e3c 2456
d62a17ae 2457 assert(rn && re);
6d691129 2458
d62a17ae 2459 if (IS_ZEBRA_DEBUG_RIB)
2460 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
2461 (void *)re);
6d691129 2462
d62a17ae 2463 dest = rib_dest_from_rnode(rn);
6d691129 2464
045207e2 2465 re_list_del(&dest->routes, re);
7a4bb9c5 2466
2eb07de3
DS
2467 if (dest->selected_fib == re)
2468 dest->selected_fib = NULL;
2469
7ee30f28 2470 nexthops_free(re->ng.nexthop);
d62a17ae 2471 XFREE(MTYPE_RE, re);
2472}
2473
2474void rib_delnode(struct route_node *rn, struct route_entry *re)
2475{
2476 afi_t afi;
2477
2478 if (IS_ZEBRA_DEBUG_RIB)
2479 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
2480 (void *)rn, (void *)re);
2481 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2482
2483 afi = (rn->p.family == AF_INET)
2484 ? AFI_IP
2485 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2486 if (is_zebra_import_table_enabled(afi, re->table)) {
2487 zebra_del_import_table_entry(rn, re);
2488 /* Just clean up if non main table */
2489 if (IS_ZEBRA_DEBUG_RIB) {
2490 char buf[SRCDEST2STR_BUFFER];
2491 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
2492 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2493 re->vrf_id, buf, rn, re,
2494 zebra_route_string(re->type));
d62a17ae 2495 }
7a4bb9c5 2496
d62a17ae 2497 rib_unlink(rn, re);
2498 } else {
2499 rib_queue_add(rn);
2500 }
718e3744 2501}
2502
f0f77c9a 2503/* This function dumps the contents of a given RE entry into
dc95824a
DO
2504 * standard debug log. Calling function name and IP prefix in
2505 * question are passed as 1st and 2nd arguments.
2506 */
2507
d62a17ae 2508void _route_entry_dump(const char *func, union prefixconstptr pp,
2509 union prefixconstptr src_pp,
2510 const struct route_entry *re)
2511{
d62a17ae 2512 const struct prefix *src_p = src_pp.p;
2513 bool is_srcdst = src_p && src_p->prefixlen;
2514 char straddr[PREFIX_STRLEN];
2515 char srcaddr[PREFIX_STRLEN];
2516 struct nexthop *nexthop;
2517
2518 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
2519 (const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
2520 is_srcdst ? " from " : "",
2521 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
2522 : "",
2523 re->vrf_id);
cc54cfee
RW
2524 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2525 func, (unsigned long)re->uptime, re->type, re->instance,
2526 re->table);
d62a17ae 2527 zlog_debug(
2528 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2529 func, re->metric, re->mtu, re->distance, re->flags, re->status);
2530 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func,
2531 re->nexthop_num, re->nexthop_active_num);
2532
7ee30f28 2533 for (ALL_NEXTHOPS(re->ng, nexthop)) {
2d68a0f2
DS
2534 struct interface *ifp;
2535 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
2536
2537 switch (nexthop->type) {
2538 case NEXTHOP_TYPE_BLACKHOLE:
2539 sprintf(straddr, "Blackhole");
2540 break;
2541 case NEXTHOP_TYPE_IFINDEX:
2542 ifp = if_lookup_by_index(nexthop->ifindex,
2543 nexthop->vrf_id);
2544 sprintf(straddr, "%s", ifp ? ifp->name : "Unknown");
2545 break;
2546 case NEXTHOP_TYPE_IPV4:
2547 /* fallthrough */
2548 case NEXTHOP_TYPE_IPV4_IFINDEX:
2549 inet_ntop(AF_INET, &nexthop->gate, straddr,
2550 INET6_ADDRSTRLEN);
2551 break;
2552 case NEXTHOP_TYPE_IPV6:
2553 case NEXTHOP_TYPE_IPV6_IFINDEX:
2554 inet_ntop(AF_INET6, &nexthop->gate, straddr,
2555 INET6_ADDRSTRLEN);
2556 break;
2557 }
df38b099
DS
2558 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2559 func, (nexthop->rparent ? " NH" : "NH"), straddr,
2d68a0f2
DS
2560 nexthop->ifindex, vrf ? vrf->name : "Unknown",
2561 nexthop->vrf_id,
d62a17ae 2562 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
2563 ? "ACTIVE "
2564 : ""),
677c1dd5 2565 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
d62a17ae 2566 ? "FIB "
2567 : ""),
2568 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
df38b099
DS
2569 ? "RECURSIVE "
2570 : ""),
2571 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
2572 ? "ONLINK "
2573 : ""),
2574 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_MATCHED)
2575 ? "MATCHED "
2576 : ""),
2577 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
2578 ? "DUPLICATE "
d62a17ae 2579 : ""));
2580 }
2581 zlog_debug("%s: dump complete", func);
dc95824a
DO
2582}
2583
2584/* This is an exported helper to rtm_read() to dump the strange
f0f77c9a 2585 * RE entry found by rib_lookup_ipv4_route()
dc95824a
DO
2586 */
2587
d62a17ae 2588void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2589{
2590 struct route_table *table;
2591 struct route_node *rn;
2592 struct route_entry *re;
2593 char prefix_buf[INET_ADDRSTRLEN];
2594
2595 /* Lookup table. */
2596 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2597 if (!table) {
e914ccbe 2598 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2599 "%s:%u zebra_vrf_table() returned NULL", __func__,
2600 vrf_id);
d62a17ae 2601 return;
2602 }
2603
2604 /* Scan the RIB table for exactly matching RE entry. */
2605 rn = route_node_lookup(table, (struct prefix *)p);
2606
2607 /* No route for this prefix. */
2608 if (!rn) {
32391aff 2609 zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
d62a17ae 2610 prefix2str((struct prefix *)p, prefix_buf,
2611 sizeof(prefix_buf)));
2612 return;
2613 }
2614
2615 /* Unlock node. */
2616 route_unlock_node(rn);
2617
2618 /* let's go */
a2addae8 2619 RNODE_FOREACH_RE (rn, re) {
32391aff
DS
2620 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2621 __func__, vrf_id,
2622 (void *)rn, (void *)re,
d62a17ae 2623 (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
2624 ? "removed"
2625 : "NOT removed"),
2626 (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
2627 ? "selected"
2628 : "NOT selected"));
2629 route_entry_dump(p, NULL, re);
2630 }
dc95824a
DO
2631}
2632
20e5ff0a
DO
2633/* Check if requested address assignment will fail due to another
2634 * route being installed by zebra in FIB already. Take necessary
2635 * actions, if needed: remove such a route from FIB and deSELECT
f0f77c9a 2636 * corresponding RE entry. Then put affected RN into RIBQ head.
20e5ff0a 2637 */
d62a17ae 2638void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2639{
2640 struct route_table *table;
2641 struct route_node *rn;
5f7a4718 2642 rib_dest_t *dest;
d62a17ae 2643
2644 if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
e914ccbe 2645 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2646 "%s:%u zebra_vrf_table() returned NULL", __func__,
2647 vrf_id);
d62a17ae 2648 return;
2649 }
2650
2651 /* No matches would be the simplest case. */
2652 if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
2653 return;
2654
2655 /* Unlock node. */
2656 route_unlock_node(rn);
2657
5f7a4718 2658 dest = rib_dest_from_rnode(rn);
d62a17ae 2659 /* Check all RE entries. In case any changes have to be done, requeue
2660 * the RN into RIBQ head. If the routing message about the new connected
2661 * route (generated by the IP address we are going to assign very soon)
2662 * comes before the RIBQ is processed, the new RE entry will join
2663 * RIBQ record already on head. This is necessary for proper
2664 * revalidation
2665 * of the rest of the RE.
2666 */
3cdba47a 2667 if (dest->selected_fib) {
5f7a4718
DS
2668 if (IS_ZEBRA_DEBUG_RIB) {
2669 char buf[PREFIX_STRLEN];
2670
2671 zlog_debug("%u:%s: freeing way for connected prefix",
2672 dest->selected_fib->vrf_id,
2673 prefix2str(&rn->p, buf, sizeof(buf)));
2674 route_entry_dump(&rn->p, NULL, dest->selected_fib);
d62a17ae 2675 }
5f7a4718 2676 rib_uninstall(rn, dest->selected_fib);
d62a17ae 2677 rib_queue_add(rn);
ade4a886 2678 }
20e5ff0a
DO
2679}
2680
d62a17ae 2681int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
2682 struct prefix_ipv6 *src_p, struct route_entry *re)
718e3744 2683{
d62a17ae 2684 struct route_table *table;
2685 struct route_node *rn;
40ecd8e4 2686 struct route_entry *same = NULL;
d62a17ae 2687 int ret = 0;
b4c034b0 2688
d62a17ae 2689 if (!re)
2690 return 0;
b4c034b0 2691
1f610a1f 2692 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
05737783 2693
d62a17ae 2694 /* Lookup table. */
7865c65d
RW
2695 table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
2696 if (!table) {
2697 XFREE(MTYPE_RE, re);
d62a17ae 2698 return 0;
7865c65d 2699 }
cddf391b 2700
d62a17ae 2701 /* Make it sure prefixlen is applied to the prefix. */
2702 apply_mask(p);
2703 if (src_p)
2704 apply_mask_ipv6(src_p);
718e3744 2705
d62a17ae 2706 /* Set default distance by route type. */
2707 if (re->distance == 0) {
0492eea0 2708 re->distance = route_distance(re->type);
718e3744 2709
d62a17ae 2710 /* iBGP distance is 200. */
2711 if (re->type == ZEBRA_ROUTE_BGP
2712 && CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
2713 re->distance = 200;
2714 }
718e3744 2715
d62a17ae 2716 /* Lookup route node.*/
2717 rn = srcdest_rnode_get(table, p, src_p);
718e3744 2718
40ecd8e4
DS
2719 /*
2720 * If same type of route are installed, treat it as a implicit
2721 * withdraw.
2722 * If the user has specified the No route replace semantics
2723 * for the install don't do a route replace.
2724 */
a2addae8 2725 RNODE_FOREACH_RE (rn, same) {
d62a17ae 2726 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
2727 continue;
41ec9222 2728
eb327fa5
RW
2729 if (same->type != re->type)
2730 continue;
2731 if (same->instance != re->instance)
2732 continue;
996c9314
LB
2733 if (same->type == ZEBRA_ROUTE_KERNEL
2734 && same->metric != re->metric)
eb327fa5 2735 continue;
40ecd8e4
DS
2736
2737 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2738 same->distance != re->distance)
2739 continue;
2740
844b3a87 2741 /*
40ecd8e4
DS
2742 * We should allow duplicate connected routes
2743 * because of IPv6 link-local routes and unnumbered
2744 * interfaces on Linux.
844b3a87
RW
2745 */
2746 if (same->type != ZEBRA_ROUTE_CONNECT)
d62a17ae 2747 break;
2748 }
718e3744 2749
cf363e1b
MS
2750 /* If this route is kernel/connected route, notify the dataplane. */
2751 if (RIB_SYSTEM_ROUTE(re)) {
2752 /* Notify dataplane */
2753 dplane_sys_route_add(rn, re);
2754 }
2755
d62a17ae 2756 /* Link new re to node.*/
2757 if (IS_ZEBRA_DEBUG_RIB) {
2da33d6b
DS
2758 rnode_debug(rn, re->vrf_id,
2759 "Inserting route rn %p, re %p (%s) existing %p",
2760 rn, re, zebra_route_string(re->type), same);
718e3744 2761
d62a17ae 2762 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2763 route_entry_dump(p, src_p, re);
718e3744 2764 }
6883bf8d
DS
2765
2766 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
d62a17ae 2767 rib_addnode(rn, re, 1);
2768 ret = 1;
6b0655a2 2769
d62a17ae 2770 /* Free implicit route.*/
2771 if (same) {
2772 rib_delnode(rn, same);
2773 ret = -1;
2774 }
718e3744 2775
d62a17ae 2776 route_unlock_node(rn);
2777 return ret;
2778}
2779
2780void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
d7c0a89a 2781 unsigned short instance, int flags, struct prefix *p,
fd36be7e 2782 struct prefix_ipv6 *src_p, const struct nexthop *nh,
40ecd8e4
DS
2783 uint32_t table_id, uint32_t metric, uint8_t distance,
2784 bool fromkernel)
d62a17ae 2785{
2786 struct route_table *table;
2787 struct route_node *rn;
2788 struct route_entry *re;
2789 struct route_entry *fib = NULL;
2790 struct route_entry *same = NULL;
fd36be7e 2791 struct nexthop *rtnh;
d62a17ae 2792 char buf2[INET6_ADDRSTRLEN];
5f7a4718 2793 rib_dest_t *dest;
d62a17ae 2794
1f610a1f 2795 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
d62a17ae 2796
2797 /* Lookup table. */
2798 table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
2799 if (!table)
2800 return;
2801
2802 /* Apply mask. */
2803 apply_mask(p);
2804 if (src_p)
2805 apply_mask_ipv6(src_p);
2806
2807 /* Lookup route node. */
2808 rn = srcdest_rnode_lookup(table, p, src_p);
2809 if (!rn) {
2810 char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
2811
2812 prefix2str(p, dst_buf, sizeof(dst_buf));
2813 if (src_p && src_p->prefixlen)
2814 prefix2str(src_p, src_buf, sizeof(src_buf));
2815 else
2816 src_buf[0] = '\0';
2817
2818 if (IS_ZEBRA_DEBUG_RIB)
2819 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id,
2820 dst_buf,
2821 (src_buf[0] != '\0') ? " from " : "",
2822 src_buf);
2823 return;
2824 }
718e3744 2825
5f7a4718
DS
2826 dest = rib_dest_from_rnode(rn);
2827 fib = dest->selected_fib;
2828
d62a17ae 2829 /* Lookup same type route. */
a2addae8 2830 RNODE_FOREACH_RE (rn, re) {
d62a17ae 2831 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2832 continue;
2833
d62a17ae 2834 if (re->type != type)
2835 continue;
2836 if (re->instance != instance)
2837 continue;
40ecd8e4
DS
2838 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2839 distance != re->distance)
2840 continue;
2841
996c9314 2842 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
f19435a8 2843 continue;
7ee30f28 2844 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
fd36be7e
DL
2845 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2846 if (rtnh->ifindex != nh->ifindex)
d62a17ae 2847 continue;
d62a17ae 2848 same = re;
2849 break;
2850 }
2851 /* Make sure that the route found has the same gateway. */
2852 else {
fd36be7e 2853 if (nh == NULL) {
d62a17ae 2854 same = re;
2855 break;
2856 }
7ee30f28 2857 for (ALL_NEXTHOPS(re->ng, rtnh))
a5a2d802
SW
2858 /*
2859 * No guarantee all kernel send nh with labels
2860 * on delete.
2861 */
2862 if (nexthop_same_no_labels(rtnh, nh)) {
d62a17ae 2863 same = re;
2864 break;
2865 }
2866 if (same)
2867 break;
2868 }
2869 }
2870 /* If same type of route can't be found and this message is from
2871 kernel. */
2872 if (!same) {
5dfeba19
DS
2873 /*
2874 * In the past(HA!) we could get here because
2875 * we were receiving a route delete from the
2876 * kernel and we're not marking the proto
2877 * as coming from it's appropriate originator.
2878 * Now that we are properly noticing the fact
2879 * that the kernel has deleted our route we
2880 * are not going to get called in this path
2881 * I am going to leave this here because
2882 * this might still work this way on non-linux
2883 * platforms as well as some weird state I have
2884 * not properly thought of yet.
2885 * If we can show that this code path is
2886 * dead then we can remove it.
2887 */
b8faa875 2888 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
d62a17ae 2889 if (IS_ZEBRA_DEBUG_RIB) {
2da33d6b
DS
2890 rnode_debug(rn, vrf_id,
2891 "rn %p, re %p (%s) was deleted from kernel, adding",
2892 rn, fib,
2893 zebra_route_string(fib->type));
d62a17ae 2894 }
2895 if (allow_delete) {
677c1dd5 2896 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
d62a17ae 2897 /* Unset flags. */
7ee30f28 2898 for (rtnh = fib->ng.nexthop; rtnh;
fd36be7e
DL
2899 rtnh = rtnh->next)
2900 UNSET_FLAG(rtnh->flags,
d62a17ae 2901 NEXTHOP_FLAG_FIB);
2902
ed216282
DS
2903 /*
2904 * This is a non FRR route
2905 * as such we should mark
2906 * it as deleted
2907 */
5f7a4718 2908 dest->selected_fib = NULL;
d62a17ae 2909 } else {
2910 /* This means someone else, other than Zebra,
2911 * has deleted
2912 * a Zebra router from the kernel. We will add
2913 * it back */
2914 rib_install_kernel(rn, fib, NULL);
2915 }
2916 } else {
2917 if (IS_ZEBRA_DEBUG_RIB) {
fd36be7e 2918 if (nh)
d62a17ae 2919 rnode_debug(
2920 rn, vrf_id,
2921 "via %s ifindex %d type %d "
2922 "doesn't exist in rib",
36228974 2923 inet_ntop(afi2family(afi),
2924 &nh->gate, buf2,
2925 sizeof(buf2)),
2926 nh->ifindex, type);
d62a17ae 2927 else
2928 rnode_debug(
2929 rn, vrf_id,
fd36be7e
DL
2930 "type %d doesn't exist in rib",
2931 type);
d62a17ae 2932 }
2933 route_unlock_node(rn);
2934 return;
2935 }
2936 }
718e3744 2937
5dfeba19 2938 if (same) {
996c9314
LB
2939 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
2940 && !allow_delete) {
5dfeba19
DS
2941 rib_install_kernel(rn, same, NULL);
2942 route_unlock_node(rn);
2943
2944 return;
2945 }
6134fd82 2946
2b83602b 2947 /* Special handling for IPv4 or IPv6 routes sourced from
2948 * EVPN - the nexthop (and associated MAC) need to be
2949 * uninstalled if no more refs.
2950 */
90264d64 2951 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
6134fd82 2952 struct nexthop *tmp_nh;
2953
7ee30f28 2954 for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
6134fd82 2955 struct ipaddr vtep_ip;
2956
2957 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1ec31309 2958 if (afi == AFI_IP) {
2959 vtep_ip.ipa_type = IPADDR_V4;
2960 memcpy(&(vtep_ip.ipaddr_v4),
2961 &(tmp_nh->gate.ipv4),
2962 sizeof(struct in_addr));
2963 } else {
2964 vtep_ip.ipa_type = IPADDR_V6;
2965 memcpy(&(vtep_ip.ipaddr_v6),
2966 &(tmp_nh->gate.ipv6),
2967 sizeof(struct in6_addr));
2968 }
a317a9b9 2969 zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
6134fd82 2970 &vtep_ip, p);
2971 }
2972 }
cf363e1b
MS
2973
2974 /* Notify dplane if system route changes */
2975 if (RIB_SYSTEM_ROUTE(re))
2976 dplane_sys_route_del(rn, same);
2977
d62a17ae 2978 rib_delnode(rn, same);
5dfeba19 2979 }
05737783 2980
d62a17ae 2981 route_unlock_node(rn);
2982 return;
2983}
718e3744 2984
718e3744 2985
d7c0a89a
QY
2986int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2987 unsigned short instance, int flags, struct prefix *p,
2988 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2989 uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
2990 route_tag_t tag)
d62a17ae 2991{
2992 struct route_entry *re;
66af6845 2993 struct nexthop *nexthop;
718e3744 2994
66af6845 2995 /* Allocate new route_entry structure. */
d62a17ae 2996 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
d62a17ae 2997 re->type = type;
2998 re->instance = instance;
2999 re->distance = distance;
3000 re->flags = flags;
3001 re->metric = metric;
3002 re->mtu = mtu;
3003 re->table = table_id;
3004 re->vrf_id = vrf_id;
3005 re->nexthop_num = 0;
98572489 3006 re->uptime = monotime(NULL);
4e40b6d6 3007 re->tag = tag;
d62a17ae 3008
66af6845
RW
3009 /* Add nexthop. */
3010 nexthop = nexthop_new();
3011 *nexthop = *nh;
3012 route_entry_nexthop_add(re, nexthop);
718e3744 3013
66af6845 3014 return rib_add_multipath(afi, safi, p, src_p, re);
718e3744 3015}
3016
1c848137 3017/* Schedule routes of a particular table (address-family) based on event. */
d5b8c216 3018void rib_update_table(struct route_table *table, rib_update_event_t event)
d62a17ae 3019{
3020 struct route_node *rn;
3021 struct route_entry *re, *next;
3022
3023 /* Walk all routes and queue for processing, if appropriate for
3024 * the trigger event.
3025 */
3026 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
1ca60f2c
DS
3027 /*
3028 * If we are looking at a route node and the node
3029 * has already been queued we don't
3030 * need to queue it up again
3031 */
996c9314
LB
3032 if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3033 RIB_ROUTE_ANY_QUEUED))
1ca60f2c 3034 continue;
d62a17ae 3035 switch (event) {
3036 case RIB_UPDATE_IF_CHANGE:
3037 /* Examine all routes that won't get processed by the
3038 * protocol or
3039 * triggered by nexthop evaluation (NHT). This would be
3040 * system,
3041 * kernel and certain static routes. Note that NHT will
3042 * get
3043 * triggered upon an interface event as connected routes
3044 * always
3045 * get queued for processing.
3046 */
a2addae8 3047 RNODE_FOREACH_RE_SAFE (rn, re, next) {
0a16efff
DS
3048 struct nexthop *nh;
3049
996c9314
LB
3050 if (re->type != ZEBRA_ROUTE_SYSTEM
3051 && re->type != ZEBRA_ROUTE_KERNEL
3052 && re->type != ZEBRA_ROUTE_CONNECT
3053 && re->type != ZEBRA_ROUTE_STATIC)
0a16efff
DS
3054 continue;
3055
3056 if (re->type != ZEBRA_ROUTE_STATIC) {
ade4a886
DS
3057 SET_FLAG(re->status,
3058 ROUTE_ENTRY_CHANGED);
0a16efff
DS
3059 rib_queue_add(rn);
3060 continue;
3061 }
3062
7ee30f28 3063 for (nh = re->ng.nexthop; nh; nh = nh->next)
0a16efff
DS
3064 if (!(nh->type == NEXTHOP_TYPE_IPV4
3065 || nh->type == NEXTHOP_TYPE_IPV6))
3066 break;
3067
3068 /* If we only have nexthops to a
3069 * gateway, NHT will
3070 * take care.
3071 */
ade4a886
DS
3072 if (nh) {
3073 SET_FLAG(re->status,
3074 ROUTE_ENTRY_CHANGED);
d62a17ae 3075 rib_queue_add(rn);
ade4a886 3076 }
d62a17ae 3077 }
3078 break;
3079
3080 case RIB_UPDATE_RMAP_CHANGE:
3081 case RIB_UPDATE_OTHER:
3082 /* Right now, examine all routes. Can restrict to a
3083 * protocol in
3084 * some cases (TODO).
3085 */
ade4a886
DS
3086 if (rnode_to_ribs(rn)) {
3087 RNODE_FOREACH_RE_SAFE (rn, re, next)
3088 SET_FLAG(re->status,
3089 ROUTE_ENTRY_CHANGED);
d62a17ae 3090 rib_queue_add(rn);
ade4a886 3091 }
d62a17ae 3092 break;
3093
3094 default:
3095 break;
3096 }
3097 }
b84c7253 3098}
3099
718e3744 3100/* RIB update function. */
d62a17ae 3101void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
718e3744 3102{
d62a17ae 3103 struct route_table *table;
1c848137 3104
d62a17ae 3105 /* Process routes of interested address-families. */
3106 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
d5b8c216 3107 if (table) {
3108 if (IS_ZEBRA_DEBUG_EVENT)
3109 zlog_debug("%s : AFI_IP event %d", __func__, event);
d62a17ae 3110 rib_update_table(table, event);
d5b8c216 3111 }
718e3744 3112
d62a17ae 3113 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
d5b8c216 3114 if (table) {
3115 if (IS_ZEBRA_DEBUG_EVENT)
3116 zlog_debug("%s : AFI_IP6 event %d", __func__, event);
d62a17ae 3117 rib_update_table(table, event);
d5b8c216 3118 }
718e3744 3119}
3120
718e3744 3121/* Delete self installed routes after zebra is relaunched. */
95a29032 3122void rib_sweep_table(struct route_table *table)
d62a17ae 3123{
3124 struct route_node *rn;
3125 struct route_entry *re;
3126 struct route_entry *next;
915902cb 3127 struct nexthop *nexthop;
d62a17ae 3128
915902cb
DS
3129 if (!table)
3130 return;
d62a17ae 3131
915902cb 3132 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
a2addae8 3133 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d4644d41 3134
915902cb
DS
3135 if (IS_ZEBRA_DEBUG_RIB)
3136 route_entry_dump(&rn->p, NULL, re);
3137
3138 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3139 continue;
3140
3141 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
3142 continue;
3143
d4644d41
DS
3144 /*
3145 * If routes are older than startup_time then
3146 * we know we read them in from the kernel.
3147 * As such we can safely remove them.
3148 */
3149 if (zrouter.startup_time < re->uptime)
3150 continue;
3151
915902cb
DS
3152 /*
3153 * So we are starting up and have received
3154 * routes from the kernel that we have installed
3155 * from a previous run of zebra but not cleaned
3156 * up ( say a kill -9 )
3157 * But since we haven't actually installed
3158 * them yet( we received them from the kernel )
3159 * we don't think they are active.
3160 * So let's pretend they are active to actually
3161 * remove them.
3162 * In all honesty I'm not sure if we should
3163 * mark them as active when we receive them
3164 * This is startup only so probably ok.
3165 *
3166 * If we ever decide to move rib_sweep_table
3167 * to a different spot (ie startup )
3168 * this decision needs to be revisited
3169 */
677c1dd5 3170 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
7ee30f28 3171 for (ALL_NEXTHOPS(re->ng, nexthop))
915902cb
DS
3172 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
3173
0c555cc6
DS
3174 rib_uninstall_kernel(rn, re);
3175 rib_delnode(rn, re);
915902cb
DS
3176 }
3177 }
718e3744 3178}
3179
3180/* Sweep all RIB tables. */
d4644d41 3181int rib_sweep_route(struct thread *t)
718e3744 3182{
d62a17ae 3183 struct vrf *vrf;
3184 struct zebra_vrf *zvrf;
78104b9b 3185
a2addae8 3186 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
915902cb
DS
3187 if ((zvrf = vrf->info) == NULL)
3188 continue;
3189
d62a17ae 3190 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
3191 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
3192 }
95a29032 3193
89272910 3194 zebra_router_sweep_route();
d4644d41
DS
3195
3196 return 0;
718e3744 3197}
2ea1ab1c
VT
3198
3199/* Remove specific by protocol routes from 'table'. */
d7c0a89a 3200unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
47a08aa9 3201 struct route_table *table)
d62a17ae 3202{
3203 struct route_node *rn;
3204 struct route_entry *re;
3205 struct route_entry *next;
3206 unsigned long n = 0;
3207
3208 if (table)
3209 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 3210 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 3211 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3212 continue;
3213 if (re->type == proto
3214 && re->instance == instance) {
3215 rib_delnode(rn, re);
3216 n++;
3217 }
3218 }
3219 return n;
2ea1ab1c
VT
3220}
3221
3222/* Remove specific by protocol routes. */
d7c0a89a 3223unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2ea1ab1c 3224{
d62a17ae 3225 struct vrf *vrf;
3226 struct zebra_vrf *zvrf;
d8612e65 3227 struct other_route_table *ort;
d62a17ae 3228 unsigned long cnt = 0;
78104b9b 3229
d8612e65
DS
3230 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
3231 zvrf = vrf->info;
3232 if (!zvrf)
3233 continue;
78104b9b 3234
d8612e65
DS
3235 cnt += rib_score_proto_table(proto, instance,
3236 zvrf->table[AFI_IP][SAFI_UNICAST])
3237 + rib_score_proto_table(
3238 proto, instance,
3239 zvrf->table[AFI_IP6][SAFI_UNICAST]);
3240
81fddbe7 3241 frr_each(otable, &zvrf->other_tables, ort) cnt +=
d8612e65
DS
3242 rib_score_proto_table(proto, instance, ort->table);
3243 }
47a08aa9 3244
d62a17ae 3245 return cnt;
2ea1ab1c
VT
3246}
3247
718e3744 3248/* Close RIB and clean up kernel routes. */
d62a17ae 3249void rib_close_table(struct route_table *table)
718e3744 3250{
d62a17ae 3251 struct route_node *rn;
1e9f448f 3252 rib_table_info_t *info;
5f7a4718 3253 rib_dest_t *dest;
718e3744 3254
1e9f448f
DS
3255 if (!table)
3256 return;
9fd92e3c 3257
6ca30e9e 3258 info = route_table_get_info(table);
5adc2528 3259
5f7a4718
DS
3260 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
3261 dest = rib_dest_from_rnode(rn);
1e9f448f 3262
5f7a4718 3263 if (dest && dest->selected_fib) {
1e9f448f
DS
3264 if (info->safi == SAFI_UNICAST)
3265 hook_call(rib_update, rn, NULL);
3266
3cdba47a
DS
3267 rib_uninstall_kernel(rn, dest->selected_fib);
3268 dest->selected_fib = NULL;
1e9f448f 3269 }
5f7a4718 3270 }
718e3744 3271}
3272
9bd9717b
MS
3273/*
3274 * Handler for async dataplane results after a pseudowire installation
3275 */
3276static int handle_pw_result(struct zebra_dplane_ctx *ctx)
3277{
9bd9717b
MS
3278 struct zebra_pw *pw;
3279 struct zebra_vrf *vrf;
3280
3281 /* The pseudowire code assumes success - we act on an error
3282 * result for installation attempts here.
3283 */
3284 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
3285 goto done;
3286
3287 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
3288 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
3289 pw = zebra_pw_find(vrf, dplane_ctx_get_pw_ifname(ctx));
3290 if (pw)
3291 zebra_pw_install_failure(pw);
3292 }
3293
3294done:
3295
9f2d0354 3296 return 0;
9bd9717b
MS
3297}
3298
3299
7cdb1a84 3300/*
d37f4d6c
MS
3301 * Handle results from the dataplane system. Dequeue update context
3302 * structs, dispatch to appropriate internal handlers.
7cdb1a84
MS
3303 */
3304static int rib_process_dplane_results(struct thread *thread)
3305{
25779064 3306 struct zebra_dplane_ctx *ctx;
4c206c8f
MS
3307 struct dplane_ctx_q ctxlist;
3308
3309 /* Dequeue a list of completed updates with one lock/unlock cycle */
7cdb1a84
MS
3310
3311 do {
4c206c8f
MS
3312 TAILQ_INIT(&ctxlist);
3313
7cdb1a84
MS
3314 /* Take lock controlling queue of results */
3315 pthread_mutex_lock(&dplane_mutex);
3316 {
d37f4d6c 3317 /* Dequeue list of context structs */
4c206c8f 3318 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
7cdb1a84
MS
3319 }
3320 pthread_mutex_unlock(&dplane_mutex);
3321
4c206c8f
MS
3322 /* Dequeue context block */
3323 ctx = dplane_ctx_dequeue(&ctxlist);
3324
3325 /* If we've emptied the results queue, we're done */
3326 if (ctx == NULL)
7cdb1a84 3327 break;
7cdb1a84 3328
4c206c8f 3329 while (ctx) {
d37f4d6c
MS
3330 switch (dplane_ctx_get_op(ctx)) {
3331 case DPLANE_OP_ROUTE_INSTALL:
3332 case DPLANE_OP_ROUTE_UPDATE:
3333 case DPLANE_OP_ROUTE_DELETE:
3334 rib_process_result(ctx);
3335 break;
3336
3337 case DPLANE_OP_LSP_INSTALL:
3338 case DPLANE_OP_LSP_UPDATE:
3339 case DPLANE_OP_LSP_DELETE:
3340 zebra_mpls_lsp_dplane_result(ctx);
3341 break;
3342
9bd9717b
MS
3343 case DPLANE_OP_PW_INSTALL:
3344 case DPLANE_OP_PW_UNINSTALL:
3345 handle_pw_result(ctx);
3346 break;
3347
cf363e1b
MS
3348 case DPLANE_OP_SYS_ROUTE_ADD:
3349 case DPLANE_OP_SYS_ROUTE_DELETE:
3350 /* No further processing in zebra for these. */
3351 dplane_ctx_fini(&ctx);
3352 break;
3353
d37f4d6c
MS
3354 default:
3355 /* Don't expect this: just return the struct? */
3356 dplane_ctx_fini(&ctx);
3357 break;
3358 } /* Dispatch by op code */
4c206c8f
MS
3359
3360 ctx = dplane_ctx_dequeue(&ctxlist);
3361 }
3362
5709131c 3363 } while (1);
7cdb1a84 3364
f183e380
MS
3365 /* Check for nexthop tracking processing after finishing with results */
3366 do_nht_processing();
3367
5709131c 3368 return 0;
7cdb1a84
MS
3369}
3370
3371/*
3372 * Results are returned from the dataplane subsystem, in the context of
1bcea841 3373 * the dataplane pthread. We enqueue the results here for processing by
7cdb1a84
MS
3374 * the main thread later.
3375 */
4c206c8f 3376static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
7cdb1a84
MS
3377{
3378 /* Take lock controlling queue of results */
3379 pthread_mutex_lock(&dplane_mutex);
3380 {
4c206c8f
MS
3381 /* Enqueue context blocks */
3382 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
7cdb1a84
MS
3383 }
3384 pthread_mutex_unlock(&dplane_mutex);
3385
4c206c8f 3386 /* Ensure event is signalled to zebra main pthread */
3801e764 3387 thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
7cdb1a84
MS
3388 &t_dplane);
3389
5709131c 3390 return 0;
7cdb1a84
MS
3391}
3392
eaa2716d
SW
3393/*
3394 * Ensure there are no empty slots in the route_info array.
3395 * Every route type in zebra should be present there.
3396 */
3397static void check_route_info(void)
3398{
3399 int len = array_size(route_info);
3400
3401 /*
3402 * ZEBRA_ROUTE_SYSTEM is special cased since
3403 * its key is 0 anyway.
3404 *
3405 * ZEBRA_ROUTE_ALL is also ignored.
3406 */
3407 for (int i = 0; i < len; i++) {
3408 if (i == ZEBRA_ROUTE_SYSTEM || i == ZEBRA_ROUTE_ALL)
3409 continue;
3410 assert(route_info[i].key);
8dc7a759 3411 assert(route_info[i].meta_q_map < MQ_SIZE);
eaa2716d
SW
3412 }
3413}
3414
718e3744 3415/* Routing information base initialize. */
d62a17ae 3416void rib_init(void)
718e3744 3417{
eaa2716d
SW
3418 check_route_info();
3419
2561d12e 3420 rib_queue_init();
7cdb1a84
MS
3421
3422 /* Init dataplane, and register for results */
3423 pthread_mutex_init(&dplane_mutex, NULL);
3424 TAILQ_INIT(&rib_dplane_q);
4c206c8f 3425 zebra_dplane_init(rib_dplane_results);
718e3744 3426}
0915bb0c
AS
3427
3428/*
3429 * vrf_id_get_next
3430 *
3431 * Get the first vrf id that is greater than the given vrf id if any.
3432 *
3433 * Returns TRUE if a vrf id was found, FALSE otherwise.
3434 */
d62a17ae 3435static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 3436{
d62a17ae 3437 struct vrf *vrf;
b72ede27 3438
d62a17ae 3439 vrf = vrf_lookup_by_id(vrf_id);
3440 if (vrf) {
3441 vrf = RB_NEXT(vrf_id_head, vrf);
3442 if (vrf) {
3443 *next_id_p = vrf->vrf_id;
3444 return 1;
3445 }
3446 }
0915bb0c 3447
d62a17ae 3448 return 0;
0915bb0c
AS
3449}
3450
3451/*
3452 * rib_tables_iter_next
3453 *
3454 * Returns the next table in the iteration.
3455 */
d62a17ae 3456struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
3457{
3458 struct route_table *table;
3459
3460 /*
3461 * Array that helps us go over all AFI/SAFI combinations via one
3462 * index.
3463 */
3464 static struct {
3465 afi_t afi;
3466 safi_t safi;
3467 } afi_safis[] = {
3468 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
3469 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
3470 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
3471 };
3472
3473 table = NULL;
3474
3475 switch (iter->state) {
3476
3477 case RIB_TABLES_ITER_S_INIT:
3478 iter->vrf_id = VRF_DEFAULT;
3479 iter->afi_safi_ix = -1;
3480
3481 /* Fall through */
3482
3483 case RIB_TABLES_ITER_S_ITERATING:
3484 iter->afi_safi_ix++;
3485 while (1) {
3486
3487 while (iter->afi_safi_ix
7e3a1ec7 3488 < (int)array_size(afi_safis)) {
d62a17ae 3489 table = zebra_vrf_table(
3490 afi_safis[iter->afi_safi_ix].afi,
3491 afi_safis[iter->afi_safi_ix].safi,
3492 iter->vrf_id);
3493 if (table)
3494 break;
3495
3496 iter->afi_safi_ix++;
3497 }
3498
3499 /*
3500 * Found another table in this vrf.
3501 */
3502 if (table)
3503 break;
3504
3505 /*
3506 * Done with all tables in the current vrf, go to the
3507 * next
3508 * one.
3509 */
3510 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
3511 break;
3512
3513 iter->afi_safi_ix = 0;
3514 }
0915bb0c 3515
0915bb0c
AS
3516 break;
3517
d62a17ae 3518 case RIB_TABLES_ITER_S_DONE:
3519 return NULL;
0915bb0c
AS
3520 }
3521
d62a17ae 3522 if (table)
3523 iter->state = RIB_TABLES_ITER_S_ITERATING;
3524 else
3525 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c 3526
d62a17ae 3527 return table;
0915bb0c 3528}