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