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