1 /* Zebra next hop tracking code
2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of GNU Zebra.
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
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.
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
29 #include "sockunion.h"
32 #include "workqueue.h"
39 #include "zebra/rib.h"
41 #include "zebra/zserv.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/zebra_rnh.h"
47 #include "zebra/zebra_routemap.h"
48 #include "zebra/interface.h"
49 #include "zebra/zebra_memory.h"
51 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
52 struct route_node
*rn
);
53 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
54 struct route_node
*rn
);
55 #define lookup_rnh_table(v, f) \
57 struct zebra_vrf *zvrf; \
58 struct route_table *t = NULL; \
59 zvrf = zebra_vrf_lookup_by_id(v); \
61 t = zvrf->rnh_table[family2afi(f)]; \
65 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
);
66 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
68 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
70 int zebra_rnh_ip_default_route
= 0;
71 int zebra_rnh_ipv6_default_route
= 0;
73 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, int family
,
76 struct zebra_vrf
*zvrf
;
77 struct route_table
*t
= NULL
;
79 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
82 case RNH_NEXTHOP_TYPE
:
83 t
= zvrf
->rnh_table
[family2afi(family
)];
85 case RNH_IMPORT_CHECK_TYPE
:
86 t
= zvrf
->import_check_table
[family2afi(family
)];
93 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
95 prefix2str(&(rnh
->node
->p
), buf
, size
);
99 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
101 struct route_table
*table
;
102 struct route_node
*rn
;
103 struct rnh
*rnh
= NULL
;
104 char buf
[PREFIX2STR_BUFFER
];
106 if (IS_ZEBRA_DEBUG_NHT
) {
107 prefix2str(p
, buf
, sizeof(buf
));
108 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
110 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
112 prefix2str(p
, buf
, sizeof(buf
));
113 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid
,
118 /* Make it sure prefixlen is applied to the prefix. */
121 /* Lookup (or add) route node.*/
122 rn
= route_node_get(table
, p
);
125 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
126 rnh
->client_list
= list_new();
128 rnh
->zebra_static_route_list
= list_new();
129 rnh
->zebra_pseudowire_list
= list_new();
135 route_unlock_node(rn
);
139 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
141 struct route_table
*table
;
142 struct route_node
*rn
;
144 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
148 /* Make it sure prefixlen is applied to the prefix. */
151 /* Lookup route node.*/
152 rn
= route_node_lookup(table
, p
);
156 route_unlock_node(rn
);
160 void zebra_free_rnh(struct rnh
*rnh
)
162 rnh
->flags
|= ZEBRA_NHT_DELETED
;
163 list_delete_and_null(&rnh
->client_list
);
164 list_delete_and_null(&rnh
->zebra_static_route_list
);
165 list_delete_and_null(&rnh
->zebra_pseudowire_list
);
166 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
167 XFREE(MTYPE_RNH
, rnh
);
170 void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
172 struct route_node
*rn
;
174 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
177 if (IS_ZEBRA_DEBUG_NHT
) {
178 char buf
[PREFIX2STR_BUFFER
];
179 zlog_debug("%u: Del RNH %s type %d", rnh
->vrf_id
,
180 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
185 route_unlock_node(rn
);
188 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
189 rnh_type_t type
, vrf_id_t vrf_id
)
191 if (IS_ZEBRA_DEBUG_NHT
) {
192 char buf
[PREFIX2STR_BUFFER
];
193 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
194 zebra_route_string(client
->proto
),
195 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
197 if (!listnode_lookup(rnh
->client_list
, client
)) {
198 listnode_add(rnh
->client_list
, client
);
199 send_client(rnh
, client
, type
,
200 vrf_id
); // Pending: check if its needed
204 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
207 if (IS_ZEBRA_DEBUG_NHT
) {
208 char buf
[PREFIX2STR_BUFFER
];
209 zlog_debug("Client %s unregisters for RNH %s type %d",
210 zebra_route_string(client
->proto
),
211 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
213 listnode_delete(rnh
->client_list
, client
);
214 if (list_isempty(rnh
->client_list
)
215 && list_isempty(rnh
->zebra_static_route_list
)
216 && list_isempty(rnh
->zebra_pseudowire_list
))
217 zebra_delete_rnh(rnh
, type
);
220 void zebra_register_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
221 struct route_node
*static_rn
)
225 rnh
= zebra_add_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
226 if (rnh
&& !listnode_lookup(rnh
->zebra_static_route_list
, static_rn
)) {
227 listnode_add(rnh
->zebra_static_route_list
, static_rn
);
231 void zebra_deregister_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
232 struct route_node
*static_rn
)
236 rnh
= zebra_lookup_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
237 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
))
240 listnode_delete(rnh
->zebra_static_route_list
, static_rn
);
242 if (list_isempty(rnh
->client_list
)
243 && list_isempty(rnh
->zebra_static_route_list
)
244 && list_isempty(rnh
->zebra_pseudowire_list
))
245 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
248 void zebra_deregister_rnh_static_nexthops(vrf_id_t vrf_id
,
249 struct nexthop
*nexthop
,
250 struct route_node
*rn
)
255 for (nh
= nexthop
; nh
; nh
= nh
->next
) {
257 case NEXTHOP_TYPE_IPV4
:
258 case NEXTHOP_TYPE_IPV4_IFINDEX
:
259 nh_p
.family
= AF_INET
;
260 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
261 nh_p
.u
.prefix4
= nh
->gate
.ipv4
;
263 case NEXTHOP_TYPE_IPV6
:
264 case NEXTHOP_TYPE_IPV6_IFINDEX
:
265 nh_p
.family
= AF_INET6
;
266 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
267 nh_p
.u
.prefix6
= nh
->gate
.ipv6
;
270 * Not sure what really to do here, we are not
271 * supposed to have either of these for NHT
272 * and the code has no way to know what prefix
273 * to use. So I'm going to just continue
274 * for the moment, which is preferable to
275 * what is currently happening which is a
277 * Some simple testing shows that we
278 * are not leaving slag around for these
279 * skipped static routes. Since
280 * they don't appear to be installed
282 case NEXTHOP_TYPE_IFINDEX
:
283 case NEXTHOP_TYPE_BLACKHOLE
:
287 zebra_deregister_rnh_static_nh(vrf_id
, &nh_p
, rn
);
291 /* XXX move this utility function elsewhere? */
292 static void addr2hostprefix(int af
, const union g_addr
*addr
,
293 struct prefix
*prefix
)
297 prefix
->family
= AF_INET
;
298 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
299 prefix
->u
.prefix4
= addr
->ipv4
;
302 prefix
->family
= AF_INET6
;
303 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
304 prefix
->u
.prefix6
= addr
->ipv6
;
307 memset(prefix
, 0, sizeof(*prefix
));
308 zlog_warn("%s: unknown address family %d", __func__
, af
);
313 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
318 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
319 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
320 if (rnh
&& !listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
321 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
323 zebra_evaluate_rnh(vrf_id
, pw
->af
, 1, RNH_NEXTHOP_TYPE
, &nh
);
327 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
335 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
338 if (list_isempty(rnh
->client_list
)
339 && list_isempty(rnh
->zebra_static_route_list
)
340 && list_isempty(rnh
->zebra_pseudowire_list
))
341 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
344 /* Apply the NHT route-map for a client to the route (and nexthops)
347 static int zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
348 struct route_entry
*re
, int proto
)
350 int at_least_one
= 0;
351 int rmap_family
; /* Route map has diff AF family enum */
352 struct nexthop
*nexthop
;
355 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
358 for (nexthop
= re
->ng
.nexthop
; nexthop
;
359 nexthop
= nexthop
->next
) {
360 ret
= zebra_nht_route_map_check(rmap_family
, proto
,
361 &prn
->p
, re
, nexthop
);
362 if (ret
!= RMAP_DENYMATCH
) {
363 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
364 at_least_one
++; /* at least one valid NH */
366 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
370 return (at_least_one
);
374 * Determine appropriate route (RE entry) resolving a tracked BGP route
375 * for BGP route for import.
377 static struct route_entry
*
378 zebra_rnh_resolve_import_entry(vrf_id_t vrfid
, int family
,
379 struct route_node
*nrn
, struct rnh
*rnh
,
380 struct route_node
**prn
)
382 struct route_table
*route_table
;
383 struct route_node
*rn
;
384 struct route_entry
*re
;
388 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
389 if (!route_table
) // unexpected
392 rn
= route_node_match(route_table
, &nrn
->p
);
396 /* Unlock route node - we don't need to lock when walking the tree. */
397 route_unlock_node(rn
);
399 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
400 && !prefix_same(&nrn
->p
, &rn
->p
))
403 /* Identify appropriate route entry. */
404 RNODE_FOREACH_RE (rn
, re
) {
405 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
406 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
407 && (re
->type
!= ZEBRA_ROUTE_BGP
))
417 * See if a tracked route entry for import (by BGP) has undergone any
418 * change, and if so, notify the client.
420 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid
, int family
,
421 int force
, struct route_node
*nrn
,
423 struct route_entry
*re
)
425 int state_changed
= 0;
426 struct zserv
*client
;
427 char bufn
[INET6_ADDRSTRLEN
];
428 struct listnode
*node
;
429 struct nexthop
*nexthop
;
431 if (re
&& (rnh
->state
== NULL
)) {
432 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
433 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
437 } else if (!re
&& (rnh
->state
!= NULL
))
440 if (compare_state(re
, rnh
->state
))
441 copy_state(rnh
, re
, nrn
);
443 if (state_changed
|| force
) {
444 if (IS_ZEBRA_DEBUG_NHT
) {
445 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
446 zlog_debug("%u:%s: Route import check %s %s\n", vrfid
,
447 bufn
, rnh
->state
? "passed" : "failed",
448 state_changed
? "(state changed)" : "");
450 /* state changed, notify clients */
451 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
452 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
458 * Notify clients registered for this nexthop about a change.
460 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid
, int family
,
461 struct route_node
*nrn
,
463 struct route_node
*prn
,
464 struct route_entry
*re
)
466 struct listnode
*node
;
467 struct zserv
*client
;
468 char bufn
[INET6_ADDRSTRLEN
];
469 char bufp
[INET6_ADDRSTRLEN
];
470 int num_resolving_nh
;
472 if (IS_ZEBRA_DEBUG_NHT
) {
473 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
475 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
476 zlog_debug("%u:%s: NH resolved over route %s", vrfid
,
479 zlog_debug("%u:%s: NH has become unresolved", vrfid
,
483 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
485 /* Apply route-map for this client to route resolving
487 * nexthop to see if it is filtered or not.
489 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
490 family
, prn
, re
, client
->proto
);
491 if (num_resolving_nh
)
492 rnh
->filtered
[client
->proto
] = 0;
494 rnh
->filtered
[client
->proto
] = 1;
496 if (IS_ZEBRA_DEBUG_NHT
)
498 "%u:%s: Notifying client %s about NH %s",
500 zebra_route_string(client
->proto
),
503 : "(filtered by route-map)");
505 rnh
->filtered
[client
->proto
] = 0;
506 if (IS_ZEBRA_DEBUG_NHT
)
508 "%u:%s: Notifying client %s about NH (unreachable)",
510 zebra_route_string(client
->proto
));
513 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
517 static void zebra_rnh_process_static_routes(vrf_id_t vrfid
, int family
,
518 struct route_node
*nrn
,
520 struct route_node
*prn
,
521 struct route_entry
*re
)
523 struct listnode
*node
;
524 int num_resolving_nh
= 0;
525 struct route_node
*static_rn
;
526 struct route_entry
*sre
;
527 struct nexthop
*nexthop
;
528 char bufn
[INET6_ADDRSTRLEN
];
529 char bufp
[INET6_ADDRSTRLEN
];
530 char bufs
[INET6_ADDRSTRLEN
];
532 if (IS_ZEBRA_DEBUG_NHT
) {
533 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
535 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
539 /* Apply route-map for "static" to route resolving this
540 * nexthop to see if it is filtered or not.
542 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, re
,
544 if (num_resolving_nh
)
545 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
547 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 1;
549 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
551 /* Evaluate each static route associated with this nexthop. */
552 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_static_route_list
, node
,
554 RNODE_FOREACH_RE (static_rn
, sre
) {
555 if (sre
->type
!= ZEBRA_ROUTE_STATIC
)
558 /* Set the filter flag for the correct nexthop - static
560 * be having multiple. We care here only about
561 * registered nexthops.
563 for (nexthop
= sre
->ng
.nexthop
; nexthop
;
564 nexthop
= nexthop
->next
) {
565 switch (nexthop
->type
) {
566 case NEXTHOP_TYPE_IPV4
:
567 case NEXTHOP_TYPE_IPV4_IFINDEX
:
568 if (nexthop
->gate
.ipv4
.s_addr
569 == nrn
->p
.u
.prefix4
.s_addr
) {
570 if (num_resolving_nh
)
573 NEXTHOP_FLAG_FILTERED
);
577 NEXTHOP_FLAG_FILTERED
);
580 case NEXTHOP_TYPE_IPV6
:
581 case NEXTHOP_TYPE_IPV6_IFINDEX
:
583 if (memcmp(&nexthop
->gate
.ipv6
,
584 &nrn
->p
.u
.prefix6
, 16)
586 if (num_resolving_nh
)
589 NEXTHOP_FLAG_FILTERED
);
593 NEXTHOP_FLAG_FILTERED
);
601 if (IS_ZEBRA_DEBUG_NHT
) {
602 prefix2str(&static_rn
->p
, bufs
,
606 "%u:%s: NH change %s, scheduling static route %s",
610 : "(filtered by route-map)",
614 "%u:%s: NH unreachable, scheduling static route %s",
618 SET_FLAG(sre
->status
, ROUTE_ENTRY_CHANGED
);
619 SET_FLAG(sre
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
622 rib_queue_add(static_rn
);
627 * Determine appropriate route (route entry) resolving a tracked
630 static struct route_entry
*
631 zebra_rnh_resolve_nexthop_entry(vrf_id_t vrfid
, int family
,
632 struct route_node
*nrn
, struct rnh
*rnh
,
633 struct route_node
**prn
)
635 struct route_table
*route_table
;
636 struct route_node
*rn
;
637 struct route_entry
*re
;
641 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
645 rn
= route_node_match(route_table
, &nrn
->p
);
649 /* Unlock route node - we don't need to lock when walking the tree. */
650 route_unlock_node(rn
);
652 /* While resolving nexthops, we may need to walk up the tree from the
653 * most-specific match. Do similar logic as in zebra_rib.c
656 /* Do not resolve over default route unless allowed &&
657 * match route to be exact if so specified
659 if (is_default_prefix(&rn
->p
)
660 && !rnh_resolve_via_default(rn
->p
.family
))
663 /* Identify appropriate route entry. */
664 RNODE_FOREACH_RE (rn
, re
) {
665 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
667 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
670 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
671 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
672 || (re
->type
== ZEBRA_ROUTE_STATIC
))
674 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
675 struct nexthop
*nexthop
;
677 for (nexthop
= re
->ng
.nexthop
; nexthop
;
678 nexthop
= nexthop
->next
)
680 == NEXTHOP_TYPE_IFINDEX
)
689 /* Route entry found, we're done; else, walk up the tree. */
695 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
704 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
707 struct listnode
*node
;
709 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
714 * See if a tracked nexthop entry has undergone any change, and if so,
715 * take appropriate action; this involves notifying any clients and/or
716 * scheduling dependent static routes for processing.
718 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid
, int family
, int force
,
719 struct route_node
*nrn
,
721 struct route_node
*prn
,
722 struct route_entry
*re
)
724 int state_changed
= 0;
726 /* If we're resolving over a different route, resolution has changed or
727 * the resolving route has some change (e.g., metric), there is a state
730 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
732 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
734 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
736 copy_state(rnh
, re
, nrn
);
738 } else if (compare_state(re
, rnh
->state
)) {
739 copy_state(rnh
, re
, nrn
);
743 if (state_changed
|| force
) {
744 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
747 /* Notify registered protocol clients. */
748 zebra_rnh_notify_protocol_clients(vrfid
, family
, nrn
, rnh
, prn
,
751 /* Process static routes attached to this nexthop */
752 zebra_rnh_process_static_routes(vrfid
, family
, nrn
, rnh
, prn
,
755 /* Process pseudowires attached to this nexthop */
756 zebra_rnh_process_pseudowires(vrfid
, rnh
);
760 /* Evaluate one tracked entry */
761 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid
, int family
, int force
,
762 rnh_type_t type
, struct route_node
*nrn
)
765 struct route_entry
*re
;
766 struct route_node
*prn
;
767 char bufn
[INET6_ADDRSTRLEN
];
769 if (IS_ZEBRA_DEBUG_NHT
) {
770 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
771 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid
, bufn
, type
,
772 force
? "(force)" : "");
777 /* Identify route entry (RE) resolving this tracked entry. */
778 if (type
== RNH_IMPORT_CHECK_TYPE
)
779 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
782 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
785 /* If the entry cannot be resolved and that is also the existing state,
786 * there is nothing further to do.
788 if (!re
&& rnh
->state
== NULL
&& !force
)
791 /* Process based on type of entry. */
792 if (type
== RNH_IMPORT_CHECK_TYPE
)
793 zebra_rnh_eval_import_check_entry(vrfid
, family
, force
, nrn
,
796 zebra_rnh_eval_nexthop_entry(vrfid
, family
, force
, nrn
, rnh
,
801 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
802 * from the re entries.
804 * Please note we are doing this *after* we have
805 * notified the world about each nexthop as that
806 * we can have a situation where one re entry
807 * covers multiple nexthops we are interested in.
809 static void zebra_rnh_clear_nhc_flag(vrf_id_t vrfid
, int family
,
810 rnh_type_t type
, struct route_node
*nrn
)
813 struct route_entry
*re
;
814 struct route_node
*prn
;
818 /* Identify route entry (RIB) resolving this tracked entry. */
819 if (type
== RNH_IMPORT_CHECK_TYPE
)
820 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
823 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
827 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
828 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
832 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
833 * of a particular VRF and address-family or a specific prefix.
835 void zebra_evaluate_rnh(vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
838 struct route_table
*rnh_table
;
839 struct route_node
*nrn
;
841 rnh_table
= get_rnh_table(vrfid
, family
, type
);
842 if (!rnh_table
) // unexpected
846 /* Evaluating a specific entry, make sure it exists. */
847 nrn
= route_node_lookup(rnh_table
, p
);
848 if (nrn
&& nrn
->info
)
849 zebra_rnh_evaluate_entry(vrfid
, family
, force
, type
,
853 route_unlock_node(nrn
);
855 /* Evaluate entire table. */
856 nrn
= route_top(rnh_table
);
859 zebra_rnh_evaluate_entry(vrfid
, family
, force
,
861 nrn
= route_next(nrn
); /* this will also unlock nrn */
863 nrn
= route_top(rnh_table
);
866 zebra_rnh_clear_nhc_flag(vrfid
, family
, type
,
868 nrn
= route_next(nrn
); /* this will also unlock nrn */
873 void zebra_print_rnh_table(vrf_id_t vrfid
, int af
, struct vty
*vty
,
876 struct route_table
*table
;
877 struct route_node
*rn
;
879 table
= get_rnh_table(vrfid
, af
, type
);
881 zlog_debug("print_rnhs: rnh table not found\n");
885 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
890 int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, int family
, struct zserv
*client
,
893 struct route_table
*ntable
;
894 struct route_node
*nrn
;
897 if (IS_ZEBRA_DEBUG_NHT
)
898 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
899 vrf_id
, zebra_route_string(client
->proto
), family
,
902 ntable
= get_rnh_table(vrf_id
, family
, type
);
904 zlog_debug("cleanup_rnh_client: rnh table not found\n");
908 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
913 zebra_remove_rnh_client(rnh
, client
, type
);
919 * free_state - free up the re structure associated with the rnh.
921 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
922 struct route_node
*rn
)
928 /* free RE and nexthops */
929 zebra_deregister_rnh_static_nexthops(vrf_id
, re
->ng
.nexthop
, rn
);
930 nexthops_free(re
->ng
.nexthop
);
934 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
935 struct route_node
*rn
)
937 struct route_entry
*state
;
940 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
947 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
948 state
->type
= re
->type
;
949 state
->distance
= re
->distance
;
950 state
->metric
= re
->metric
;
951 state
->vrf_id
= re
->vrf_id
;
953 route_entry_copy_nexthops(state
, re
->ng
.nexthop
);
957 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
963 if ((!r1
&& r2
) || (r1
&& !r2
))
966 if (r1
->distance
!= r2
->distance
)
969 if (r1
->metric
!= r2
->metric
)
972 if (r1
->nexthop_num
!= r2
->nexthop_num
)
975 if (CHECK_FLAG(r1
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
)
976 || CHECK_FLAG(r1
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
982 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
986 struct route_entry
*re
;
989 struct nexthop
*nexthop
;
990 struct route_node
*rn
;
991 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
992 : ZEBRA_NEXTHOP_UPDATE
;
997 /* Get output stream. */
998 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1000 zclient_create_header(s
, cmd
, vrf_id
);
1002 stream_putw(s
, rn
->p
.family
);
1003 switch (rn
->p
.family
) {
1005 stream_putc(s
, rn
->p
.prefixlen
);
1006 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
1009 stream_putc(s
, rn
->p
.prefixlen
);
1010 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
1013 zlog_err("%s: Unknown family (%d) notification attempted\n",
1014 __FUNCTION__
, rn
->p
.family
);
1018 stream_putc(s
, re
->type
);
1019 stream_putw(s
, re
->instance
);
1020 stream_putc(s
, re
->distance
);
1021 stream_putl(s
, re
->metric
);
1023 nump
= stream_get_endp(s
);
1025 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
1026 if ((CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
1027 || CHECK_FLAG(nexthop
->flags
,
1028 NEXTHOP_FLAG_RECURSIVE
))
1029 && CHECK_FLAG(nexthop
->flags
,
1030 NEXTHOP_FLAG_ACTIVE
)) {
1031 stream_putc(s
, nexthop
->type
);
1032 switch (nexthop
->type
) {
1033 case NEXTHOP_TYPE_IPV4
:
1034 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1035 stream_put_in_addr(s
,
1036 &nexthop
->gate
.ipv4
);
1037 stream_putl(s
, nexthop
->ifindex
);
1039 case NEXTHOP_TYPE_IFINDEX
:
1040 stream_putl(s
, nexthop
->ifindex
);
1042 case NEXTHOP_TYPE_IPV6
:
1043 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1044 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
1045 stream_putl(s
, nexthop
->ifindex
);
1053 stream_putc_at(s
, nump
, num
);
1055 stream_putc(s
, 0); // type
1056 stream_putw(s
, 0); // instance
1057 stream_putc(s
, 0); // distance
1058 stream_putl(s
, 0); // metric
1059 stream_putc(s
, 0); // nexthops
1061 stream_putw_at(s
, 0, stream_get_endp(s
));
1063 client
->nh_last_upd_time
= monotime(NULL
);
1064 client
->last_write_cmd
= cmd
;
1065 return zebra_server_send_message(client
, s
);
1068 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
1071 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1073 switch (nexthop
->type
) {
1074 case NEXTHOP_TYPE_IPV4
:
1075 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1076 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1077 if (nexthop
->ifindex
)
1078 vty_out(vty
, ", %s",
1079 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1081 case NEXTHOP_TYPE_IPV6
:
1082 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1084 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1085 if (nexthop
->ifindex
)
1086 vty_out(vty
, ", via %s",
1087 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1089 case NEXTHOP_TYPE_IFINDEX
:
1090 vty_out(vty
, " is directly connected, %s",
1091 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1093 case NEXTHOP_TYPE_BLACKHOLE
:
1094 vty_out(vty
, " is directly connected, Null0");
1102 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
1105 struct nexthop
*nexthop
;
1106 struct listnode
*node
;
1107 struct zserv
*client
;
1111 vty_out(vty
, "%s%s\n",
1112 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1113 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
1116 vty_out(vty
, " resolved via %s\n",
1117 zebra_route_string(rnh
->state
->type
));
1118 for (nexthop
= rnh
->state
->ng
.nexthop
; nexthop
;
1119 nexthop
= nexthop
->next
)
1120 print_nh(nexthop
, vty
);
1122 vty_out(vty
, " unresolved%s\n",
1123 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1127 vty_out(vty
, " Client list:");
1128 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1129 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1131 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1132 if (!list_isempty(rnh
->zebra_static_route_list
))
1133 vty_out(vty
, " zebra%s",
1134 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] ? "(filtered)" : "");
1135 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1136 vty_out(vty
, " zebra[pseudowires]");