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