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