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