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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "sockunion.h"
33 #include "workqueue.h"
40 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/interface.h"
50 #include "zebra/zebra_memory.h"
52 static void free_state(vrf_id_t vrf_id
, struct rib
*rib
, struct route_node
*rn
);
53 static void copy_state(struct rnh
*rnh
, struct rib
*rib
, struct route_node
*rn
);
54 #define lookup_rnh_table(v, f) \
56 struct zebra_vrf *zvrf; \
57 struct route_table *t = NULL; \
58 zvrf = zebra_vrf_lookup_by_id(v); \
60 t = zvrf->rnh_table[family2afi(f)]; \
64 static int compare_state(struct rib
*r1
, struct rib
*r2
);
65 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
67 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
69 int zebra_rnh_ip_default_route
= 0;
70 int zebra_rnh_ipv6_default_route
= 0;
72 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, int family
,
75 struct zebra_vrf
*zvrf
;
76 struct route_table
*t
= NULL
;
78 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
81 case RNH_NEXTHOP_TYPE
:
82 t
= zvrf
->rnh_table
[family2afi(family
)];
84 case RNH_IMPORT_CHECK_TYPE
:
85 t
= zvrf
->import_check_table
[family2afi(family
)];
92 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
94 prefix2str(&(rnh
->node
->p
), buf
, size
);
98 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
100 struct route_table
*table
;
101 struct route_node
*rn
;
102 struct rnh
*rnh
= NULL
;
103 char buf
[PREFIX2STR_BUFFER
];
105 if (IS_ZEBRA_DEBUG_NHT
) {
106 prefix2str(p
, buf
, sizeof(buf
));
107 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
109 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
111 prefix2str(p
, buf
, sizeof(buf
));
112 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid
,
117 /* Make it sure prefixlen is applied to the prefix. */
120 /* Lookup (or add) route node.*/
121 rn
= route_node_get(table
, p
);
124 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
125 rnh
->client_list
= list_new();
127 rnh
->zebra_static_route_list
= list_new();
128 rnh
->zebra_pseudowire_list
= list_new();
134 route_unlock_node(rn
);
138 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
140 struct route_table
*table
;
141 struct route_node
*rn
;
143 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
147 /* Make it sure prefixlen is applied to the prefix. */
150 /* Lookup route node.*/
151 rn
= route_node_lookup(table
, p
);
155 route_unlock_node(rn
);
159 void zebra_free_rnh(struct rnh
*rnh
)
161 rnh
->flags
|= ZEBRA_NHT_DELETED
;
162 list_free(rnh
->client_list
);
163 list_free(rnh
->zebra_static_route_list
);
164 list_free(rnh
->zebra_pseudowire_list
);
165 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
166 XFREE(MTYPE_RNH
, rnh
);
169 void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
171 struct route_node
*rn
;
173 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
176 if (IS_ZEBRA_DEBUG_NHT
) {
177 char buf
[PREFIX2STR_BUFFER
];
178 zlog_debug("%u: Del RNH %s type %d", rnh
->vrf_id
,
179 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
184 route_unlock_node(rn
);
187 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
188 rnh_type_t type
, vrf_id_t vrf_id
)
190 if (IS_ZEBRA_DEBUG_NHT
) {
191 char buf
[PREFIX2STR_BUFFER
];
192 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
193 zebra_route_string(client
->proto
),
194 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
196 if (!listnode_lookup(rnh
->client_list
, client
)) {
197 listnode_add(rnh
->client_list
, client
);
198 send_client(rnh
, client
, type
,
199 vrf_id
); // Pending: check if its needed
203 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
206 if (IS_ZEBRA_DEBUG_NHT
) {
207 char buf
[PREFIX2STR_BUFFER
];
208 zlog_debug("Client %s unregisters for RNH %s type %d",
209 zebra_route_string(client
->proto
),
210 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
212 listnode_delete(rnh
->client_list
, client
);
213 if (list_isempty(rnh
->client_list
)
214 && list_isempty(rnh
->zebra_static_route_list
)
215 && list_isempty(rnh
->zebra_pseudowire_list
))
216 zebra_delete_rnh(rnh
, type
);
219 void zebra_register_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
220 struct route_node
*static_rn
)
224 rnh
= zebra_add_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
225 if (rnh
&& !listnode_lookup(rnh
->zebra_static_route_list
, static_rn
)) {
226 listnode_add(rnh
->zebra_static_route_list
, static_rn
);
230 void zebra_deregister_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
231 struct route_node
*static_rn
)
235 rnh
= zebra_lookup_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
236 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
))
239 listnode_delete(rnh
->zebra_static_route_list
, static_rn
);
241 if (list_isempty(rnh
->client_list
)
242 && list_isempty(rnh
->zebra_static_route_list
)
243 && list_isempty(rnh
->zebra_pseudowire_list
))
244 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
247 void zebra_deregister_rnh_static_nexthops(vrf_id_t vrf_id
,
248 struct nexthop
*nexthop
,
249 struct route_node
*rn
)
254 for (nh
= nexthop
; nh
; nh
= nh
->next
) {
256 case NEXTHOP_TYPE_IPV4
:
257 case NEXTHOP_TYPE_IPV4_IFINDEX
:
258 nh_p
.family
= AF_INET
;
259 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
260 nh_p
.u
.prefix4
= nh
->gate
.ipv4
;
262 case NEXTHOP_TYPE_IPV6
:
263 case NEXTHOP_TYPE_IPV6_IFINDEX
:
264 nh_p
.family
= AF_INET6
;
265 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
266 nh_p
.u
.prefix6
= nh
->gate
.ipv6
;
269 * Not sure what really to do here, we are not
270 * supposed to have either of these for NHT
271 * and the code has no way to know what prefix
272 * to use. So I'm going to just continue
273 * for the moment, which is preferable to
274 * what is currently happening which is a
276 * Some simple testing shows that we
277 * are not leaving slag around for these
278 * skipped static routes. Since
279 * they don't appear to be installed
281 case NEXTHOP_TYPE_IFINDEX
:
282 case NEXTHOP_TYPE_BLACKHOLE
:
286 zebra_deregister_rnh_static_nh(vrf_id
, &nh_p
, rn
);
290 /* XXX move this utility function elsewhere? */
291 static void addr2hostprefix(int af
, const union g_addr
*addr
,
292 struct prefix
*prefix
)
296 prefix
->family
= AF_INET
;
297 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
298 prefix
->u
.prefix4
= addr
->ipv4
;
301 prefix
->family
= AF_INET6
;
302 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
303 prefix
->u
.prefix6
= addr
->ipv6
;
306 zlog_warn("%s: unknown address family %d", __func__
, af
);
311 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
316 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
317 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
318 if (rnh
&& !listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
319 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
321 zebra_evaluate_rnh(vrf_id
, pw
->af
, 1, RNH_NEXTHOP_TYPE
, &nh
);
325 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
333 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
336 if (list_isempty(rnh
->client_list
)
337 && list_isempty(rnh
->zebra_static_route_list
)
338 && list_isempty(rnh
->zebra_pseudowire_list
))
339 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
342 /* Apply the NHT route-map for a client to the route (and nexthops)
345 static int zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
346 struct rib
*rib
, int proto
)
348 int at_least_one
= 0;
349 int rmap_family
; /* Route map has diff AF family enum */
350 struct nexthop
*nexthop
;
353 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
356 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
357 ret
= zebra_nht_route_map_check(rmap_family
, proto
,
358 &prn
->p
, rib
, nexthop
);
359 if (ret
!= RMAP_DENYMATCH
) {
360 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
361 at_least_one
++; /* at least one valid NH */
363 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
367 return (at_least_one
);
371 * Determine appropriate route (RIB entry) resolving a tracked entry
372 * (nexthop or BGP route for import).
374 static struct rib
*zebra_rnh_resolve_entry(vrf_id_t vrfid
, int family
,
376 struct route_node
*nrn
,
378 struct route_node
**prn
)
380 struct route_table
*route_table
;
381 struct route_node
*rn
;
386 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
387 if (!route_table
) // unexpected
390 rn
= route_node_match(route_table
, &nrn
->p
);
394 /* When resolving nexthops, do not resolve via the default route unless
395 * 'ip nht resolve-via-default' is configured.
397 if ((type
== RNH_NEXTHOP_TYPE
)
398 && (is_default_prefix(&rn
->p
)
399 && !nh_resolve_via_default(rn
->p
.family
)))
401 else if ((type
== RNH_IMPORT_CHECK_TYPE
)
402 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
403 && !prefix_same(&nrn
->p
, &rn
->p
))
406 /* Identify appropriate route entry. */
407 RNODE_FOREACH_RIB(rn
, rib
)
409 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
411 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_SELECTED_FIB
))
414 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
415 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
417 if (rib
->type
== ZEBRA_ROUTE_NHRP
) {
418 struct nexthop
*nexthop
;
419 for (nexthop
= rib
->nexthop
; nexthop
;
420 nexthop
= nexthop
->next
)
422 == NEXTHOP_TYPE_IFINDEX
)
427 } else if ((type
== RNH_IMPORT_CHECK_TYPE
)
428 && (rib
->type
== ZEBRA_ROUTE_BGP
))
435 /* Need to unlock route node */
436 route_unlock_node(rn
);
443 * See if a tracked route entry for import (by BGP) has undergone any
444 * change, and if so, notify the client.
446 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid
, int family
,
447 int force
, struct route_node
*nrn
,
448 struct rnh
*rnh
, struct rib
*rib
)
450 int state_changed
= 0;
451 struct zserv
*client
;
452 char bufn
[INET6_ADDRSTRLEN
];
453 struct listnode
*node
;
454 struct nexthop
*nexthop
, *tnexthop
;
457 if (rib
&& (rnh
->state
== NULL
)) {
458 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
,
460 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
464 } else if (!rib
&& (rnh
->state
!= NULL
))
467 if (compare_state(rib
, rnh
->state
))
468 copy_state(rnh
, rib
, nrn
);
470 if (state_changed
|| force
) {
471 if (IS_ZEBRA_DEBUG_NHT
) {
472 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
473 zlog_debug("%u:%s: Route import check %s %s\n", vrfid
,
474 bufn
, rnh
->state
? "passed" : "failed",
475 state_changed
? "(state changed)" : "");
477 /* state changed, notify clients */
478 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
479 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
485 * Notify clients registered for this nexthop about a change.
487 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid
, int family
,
488 struct route_node
*nrn
,
490 struct route_node
*prn
,
493 struct listnode
*node
;
494 struct zserv
*client
;
495 char bufn
[INET6_ADDRSTRLEN
];
496 char bufp
[INET6_ADDRSTRLEN
];
497 int num_resolving_nh
;
499 if (IS_ZEBRA_DEBUG_NHT
) {
500 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
502 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
503 zlog_debug("%u:%s: NH resolved over route %s", vrfid
,
506 zlog_debug("%u:%s: NH has become unresolved", vrfid
,
510 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
512 /* Apply route-map for this client to route resolving
514 * nexthop to see if it is filtered or not.
516 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
517 family
, prn
, rib
, client
->proto
);
518 if (num_resolving_nh
)
519 rnh
->filtered
[client
->proto
] = 0;
521 rnh
->filtered
[client
->proto
] = 1;
523 if (IS_ZEBRA_DEBUG_NHT
)
525 "%u:%s: Notifying client %s about NH %s",
527 zebra_route_string(client
->proto
),
530 : "(filtered by route-map)");
532 rnh
->filtered
[client
->proto
] = 0;
533 if (IS_ZEBRA_DEBUG_NHT
)
535 "%u:%s: Notifying client %s about NH (unreachable)",
537 zebra_route_string(client
->proto
));
540 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
544 static void zebra_rnh_process_static_routes(vrf_id_t vrfid
, int family
,
545 struct route_node
*nrn
,
547 struct route_node
*prn
,
550 struct listnode
*node
;
551 int num_resolving_nh
= 0;
552 struct route_node
*static_rn
;
554 struct nexthop
*nexthop
;
555 char bufn
[INET6_ADDRSTRLEN
];
556 char bufp
[INET6_ADDRSTRLEN
];
557 char bufs
[INET6_ADDRSTRLEN
];
559 if (IS_ZEBRA_DEBUG_NHT
) {
560 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
562 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
566 /* Apply route-map for "static" to route resolving this
567 * nexthop to see if it is filtered or not.
569 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, rib
,
571 if (num_resolving_nh
)
572 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
574 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 1;
576 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
578 /* Evaluate each static route associated with this nexthop. */
579 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_static_route_list
, node
,
581 RNODE_FOREACH_RIB(static_rn
, srib
)
583 if (srib
->type
!= ZEBRA_ROUTE_STATIC
)
586 /* Set the filter flag for the correct nexthop - static
588 * be having multiple. We care here only about
589 * registered nexthops.
591 for (nexthop
= srib
->nexthop
; nexthop
;
592 nexthop
= nexthop
->next
) {
593 switch (nexthop
->type
) {
594 case NEXTHOP_TYPE_IPV4
:
595 case NEXTHOP_TYPE_IPV4_IFINDEX
:
596 if (nexthop
->gate
.ipv4
.s_addr
597 == nrn
->p
.u
.prefix4
.s_addr
) {
598 if (num_resolving_nh
)
601 NEXTHOP_FLAG_FILTERED
);
605 NEXTHOP_FLAG_FILTERED
);
608 case NEXTHOP_TYPE_IPV6
:
609 case NEXTHOP_TYPE_IPV6_IFINDEX
:
611 if (memcmp(&nexthop
->gate
.ipv6
,
612 &nrn
->p
.u
.prefix6
, 16)
614 if (num_resolving_nh
)
617 NEXTHOP_FLAG_FILTERED
);
621 NEXTHOP_FLAG_FILTERED
);
629 if (IS_ZEBRA_DEBUG_NHT
) {
630 prefix2str(&static_rn
->p
, bufs
,
634 "%u:%s: NH change %s, scheduling static route %s",
638 : "(filtered by route-map)",
642 "%u:%s: NH unreachable, scheduling static route %s",
646 SET_FLAG(srib
->status
, RIB_ENTRY_CHANGED
);
647 SET_FLAG(srib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
650 rib_queue_add(static_rn
);
654 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
657 struct listnode
*node
;
659 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
664 * See if a tracked nexthop entry has undergone any change, and if so,
665 * take appropriate action; this involves notifying any clients and/or
666 * scheduling dependent static routes for processing.
668 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid
, int family
, int force
,
669 struct route_node
*nrn
,
671 struct route_node
*prn
,
674 int state_changed
= 0;
676 /* If we're resolving over a different route, resolution has changed or
677 * the resolving route has some change (e.g., metric), there is a state
680 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
682 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
684 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
686 copy_state(rnh
, rib
, nrn
);
688 } else if (compare_state(rib
, rnh
->state
)) {
689 copy_state(rnh
, rib
, nrn
);
693 if (state_changed
|| force
) {
694 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
697 /* Notify registered protocol clients. */
698 zebra_rnh_notify_protocol_clients(vrfid
, family
, nrn
, rnh
, prn
,
701 /* Process static routes attached to this nexthop */
702 zebra_rnh_process_static_routes(vrfid
, family
, nrn
, rnh
, prn
,
705 /* Process pseudowires attached to this nexthop */
706 zebra_rnh_process_pseudowires(vrfid
, rnh
);
710 /* Evaluate one tracked entry */
711 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid
, int family
, int force
,
712 rnh_type_t type
, struct route_node
*nrn
)
716 struct route_node
*prn
;
717 char bufn
[INET6_ADDRSTRLEN
];
719 if (IS_ZEBRA_DEBUG_NHT
) {
720 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
721 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid
, bufn
, type
,
722 force
? "(force)" : "");
727 /* Identify route entry (RIB) resolving this tracked entry. */
728 rib
= zebra_rnh_resolve_entry(vrfid
, family
, type
, nrn
, rnh
, &prn
);
730 /* If the entry cannot be resolved and that is also the existing state,
731 * there is nothing further to do.
733 if (!rib
&& rnh
->state
== NULL
&& !force
)
736 /* Process based on type of entry. */
737 if (type
== RNH_IMPORT_CHECK_TYPE
)
738 zebra_rnh_eval_import_check_entry(vrfid
, family
, force
, nrn
,
741 zebra_rnh_eval_nexthop_entry(vrfid
, family
, force
, nrn
, rnh
,
746 * Clear the RIB_ENTRY_NEXTHOPS_CHANGED flag
747 * from the rib entries.
749 * Please note we are doing this *after* we have
750 * notified the world about each nexthop as that
751 * we can have a situation where one rib entry
752 * covers multiple nexthops we are interested in.
754 static void zebra_rnh_clear_nhc_flag(vrf_id_t vrfid
, int family
,
755 rnh_type_t type
, struct route_node
*nrn
)
759 struct route_node
*prn
;
763 rib
= zebra_rnh_resolve_entry(vrfid
, family
, type
, nrn
, rnh
, &prn
);
766 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
767 UNSET_FLAG(rib
->status
, RIB_ENTRY_LABELS_CHANGED
);
771 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
772 * of a particular VRF and address-family or a specific prefix.
774 void zebra_evaluate_rnh(vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
777 struct route_table
*rnh_table
;
778 struct route_node
*nrn
;
780 rnh_table
= get_rnh_table(vrfid
, family
, type
);
781 if (!rnh_table
) // unexpected
785 /* Evaluating a specific entry, make sure it exists. */
786 nrn
= route_node_lookup(rnh_table
, p
);
787 if (nrn
&& nrn
->info
)
788 zebra_rnh_evaluate_entry(vrfid
, family
, force
, type
,
792 route_unlock_node(nrn
);
794 /* Evaluate entire table. */
795 nrn
= route_top(rnh_table
);
798 zebra_rnh_evaluate_entry(vrfid
, family
, force
,
800 nrn
= route_next(nrn
); /* this will also unlock nrn */
802 nrn
= route_top(rnh_table
);
805 zebra_rnh_clear_nhc_flag(vrfid
, family
, type
,
807 nrn
= route_next(nrn
); /* this will also unlock nrn */
812 void zebra_print_rnh_table(vrf_id_t vrfid
, int af
, struct vty
*vty
,
815 struct route_table
*table
;
816 struct route_node
*rn
;
818 table
= get_rnh_table(vrfid
, af
, type
);
820 zlog_debug("print_rnhs: rnh table not found\n");
824 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
829 int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, int family
, struct zserv
*client
,
832 struct route_table
*ntable
;
833 struct route_node
*nrn
;
836 if (IS_ZEBRA_DEBUG_NHT
)
837 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
838 vrf_id
, zebra_route_string(client
->proto
), family
,
841 ntable
= get_rnh_table(vrf_id
, family
, type
);
843 zlog_debug("cleanup_rnh_client: rnh table not found\n");
847 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
852 zebra_remove_rnh_client(rnh
, client
, type
);
858 * free_state - free up the rib structure associated with the rnh.
860 static void free_state(vrf_id_t vrf_id
, struct rib
*rib
, struct route_node
*rn
)
866 /* free RIB and nexthops */
867 zebra_deregister_rnh_static_nexthops(vrf_id
, rib
->nexthop
, rn
);
868 nexthops_free(rib
->nexthop
);
869 XFREE(MTYPE_RIB
, rib
);
872 static void copy_state(struct rnh
*rnh
, struct rib
*rib
, struct route_node
*rn
)
878 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
885 state
= XCALLOC(MTYPE_RIB
, sizeof(struct rib
));
886 state
->type
= rib
->type
;
887 state
->metric
= rib
->metric
;
889 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
890 rib_copy_nexthops(state
, nh
);
894 static int compare_state(struct rib
*r1
, struct rib
*r2
)
900 if ((!r1
&& r2
) || (r1
&& !r2
))
903 if (r1
->metric
!= r2
->metric
)
906 if (r1
->nexthop_num
!= r2
->nexthop_num
)
909 if (CHECK_FLAG(r1
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
)
910 || CHECK_FLAG(r1
->status
, RIB_ENTRY_LABELS_CHANGED
))
916 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
923 struct nexthop
*nexthop
;
924 struct route_node
*rn
;
925 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
926 : ZEBRA_NEXTHOP_UPDATE
;
931 /* Get output stream. */
935 zserv_create_header(s
, cmd
, vrf_id
);
937 stream_putw(s
, rn
->p
.family
);
938 switch (rn
->p
.family
) {
940 stream_putc(s
, rn
->p
.prefixlen
);
941 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
944 stream_putc(s
, rn
->p
.prefixlen
);
945 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
948 zlog_err("%s: Unknown family (%d) notification attempted\n",
949 __FUNCTION__
, rn
->p
.family
);
953 stream_putc(s
, rib
->distance
);
954 stream_putl(s
, rib
->metric
);
956 nump
= stream_get_endp(s
);
958 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
959 if ((CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
960 || CHECK_FLAG(nexthop
->flags
,
961 NEXTHOP_FLAG_RECURSIVE
))
962 && CHECK_FLAG(nexthop
->flags
,
963 NEXTHOP_FLAG_ACTIVE
)) {
964 stream_putc(s
, nexthop
->type
);
965 switch (nexthop
->type
) {
966 case NEXTHOP_TYPE_IPV4
:
967 stream_put_in_addr(s
,
968 &nexthop
->gate
.ipv4
);
969 stream_putl(s
, nexthop
->ifindex
);
971 case NEXTHOP_TYPE_IFINDEX
:
972 stream_putl(s
, nexthop
->ifindex
);
974 case NEXTHOP_TYPE_IPV4_IFINDEX
:
975 stream_put_in_addr(s
,
976 &nexthop
->gate
.ipv4
);
977 stream_putl(s
, nexthop
->ifindex
);
979 case NEXTHOP_TYPE_IPV6
:
980 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
981 stream_putl(s
, nexthop
->ifindex
);
983 case NEXTHOP_TYPE_IPV6_IFINDEX
:
984 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
985 stream_putl(s
, nexthop
->ifindex
);
993 stream_putc_at(s
, nump
, num
);
995 stream_putc(s
, 0); // distance
996 stream_putl(s
, 0); // metric
997 stream_putc(s
, 0); // nexthops
999 stream_putw_at(s
, 0, stream_get_endp(s
));
1001 client
->nh_last_upd_time
= monotime(NULL
);
1002 client
->last_write_cmd
= cmd
;
1003 return zebra_server_send_message(client
);
1006 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
1009 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1011 switch (nexthop
->type
) {
1012 case NEXTHOP_TYPE_IPV4
:
1013 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1014 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1015 if (nexthop
->ifindex
)
1016 vty_out(vty
, ", %s",
1017 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1019 case NEXTHOP_TYPE_IPV6
:
1020 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1022 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1023 if (nexthop
->ifindex
)
1024 vty_out(vty
, ", via %s",
1025 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1027 case NEXTHOP_TYPE_IFINDEX
:
1028 vty_out(vty
, " is directly connected, %s",
1029 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
1031 case NEXTHOP_TYPE_BLACKHOLE
:
1032 vty_out(vty
, " is directly connected, Null0");
1037 vty_out(vty
, "%s", VTY_NEWLINE
);
1040 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
1043 struct nexthop
*nexthop
;
1044 struct listnode
*node
;
1045 struct zserv
*client
;
1049 vty_out(vty
, "%s%s%s",
1050 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1051 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
1055 vty_out(vty
, " resolved via %s%s",
1056 zebra_route_string(rnh
->state
->type
), VTY_NEWLINE
);
1057 for (nexthop
= rnh
->state
->nexthop
; nexthop
;
1058 nexthop
= nexthop
->next
)
1059 print_nh(nexthop
, vty
);
1061 vty_out(vty
, " unresolved%s%s",
1062 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1067 vty_out(vty
, " Client list:");
1068 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1069 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1071 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1072 if (!list_isempty(rnh
->zebra_static_route_list
))
1073 vty_out(vty
, " zebra[static routes]%s",
1074 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] ? "(filtered)" : "");
1075 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1076 vty_out(vty
, " zebra[pseudowires]");
1077 vty_out(vty
, "%s", VTY_NEWLINE
);