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