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