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/zebra_router.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"
51 #include "zebra/zebra_errors.h"
53 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
54 struct route_node
*rn
);
55 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
56 struct route_node
*rn
);
57 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
);
58 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
60 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
61 static int zebra_client_cleanup_rnh(struct zserv
*client
);
63 int zebra_rnh_ip_default_route
= 0;
64 int zebra_rnh_ipv6_default_route
= 0;
66 void zebra_rnh_init(void)
68 hook_register(zserv_client_close
, zebra_client_cleanup_rnh
);
71 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, afi_t afi
,
74 struct zebra_vrf
*zvrf
;
75 struct route_table
*t
= NULL
;
77 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
80 case RNH_NEXTHOP_TYPE
:
81 t
= zvrf
->rnh_table
[afi
];
83 case RNH_IMPORT_CHECK_TYPE
:
84 t
= zvrf
->import_check_table
[afi
];
91 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
93 prefix2str(&(rnh
->node
->p
), buf
, size
);
97 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
, family2afi(PREFIX_FAMILY(p
)), type
);
111 prefix2str(p
, buf
, sizeof(buf
));
112 flog_warn(EC_ZEBRA_RNH_NO_TABLE
,
113 "%u: Add RNH %s type %d - table not found", vrfid
,
119 /* Make it sure prefixlen is applied to the prefix. */
122 /* Lookup (or add) route node.*/
123 rn
= route_node_get(table
, p
);
126 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
127 rnh
->client_list
= list_new();
129 rnh
->zebra_pseudowire_list
= list_new();
137 route_unlock_node(rn
);
141 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
143 struct route_table
*table
;
144 struct route_node
*rn
;
146 table
= get_rnh_table(vrfid
, family2afi(PREFIX_FAMILY(p
)), type
);
150 /* Make it sure prefixlen is applied to the prefix. */
153 /* Lookup route node.*/
154 rn
= route_node_lookup(table
, p
);
158 route_unlock_node(rn
);
162 void zebra_free_rnh(struct rnh
*rnh
)
164 rnh
->flags
|= ZEBRA_NHT_DELETED
;
165 list_delete(&rnh
->client_list
);
166 list_delete(&rnh
->zebra_pseudowire_list
);
167 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
168 XFREE(MTYPE_RNH
, rnh
);
171 static void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
173 struct route_node
*rn
;
175 if (!list_isempty(rnh
->client_list
)
176 || !list_isempty(rnh
->zebra_pseudowire_list
))
179 if ((rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
182 if (IS_ZEBRA_DEBUG_NHT
) {
183 char buf
[PREFIX2STR_BUFFER
];
184 zlog_debug("%u: Del RNH %s type %d", rnh
->vrf_id
,
185 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
190 route_unlock_node(rn
);
194 * This code will send to the registering client
196 * For a rnh that was created, there is no data
197 * so it will send an empty nexthop group
198 * If rnh exists then we know it has been evaluated
199 * and as such it will have a resolved rnh.
201 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
202 rnh_type_t type
, vrf_id_t vrf_id
)
204 if (IS_ZEBRA_DEBUG_NHT
) {
205 char buf
[PREFIX2STR_BUFFER
];
206 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
207 zebra_route_string(client
->proto
),
208 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
210 if (!listnode_lookup(rnh
->client_list
, client
))
211 listnode_add(rnh
->client_list
, client
);
214 * We always need to respond with known information,
215 * currently multiple daemons expect this behavior
217 send_client(rnh
, client
, type
, vrf_id
);
220 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
223 if (IS_ZEBRA_DEBUG_NHT
) {
224 char buf
[PREFIX2STR_BUFFER
];
225 zlog_debug("Client %s unregisters for RNH %s type %d",
226 zebra_route_string(client
->proto
),
227 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
229 listnode_delete(rnh
->client_list
, client
);
230 zebra_delete_rnh(rnh
, type
);
233 /* XXX move this utility function elsewhere? */
234 static void addr2hostprefix(int af
, const union g_addr
*addr
,
235 struct prefix
*prefix
)
239 prefix
->family
= AF_INET
;
240 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
241 prefix
->u
.prefix4
= addr
->ipv4
;
244 prefix
->family
= AF_INET6
;
245 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
246 prefix
->u
.prefix6
= addr
->ipv6
;
249 memset(prefix
, 0, sizeof(*prefix
));
250 zlog_debug("%s: unknown address family %d", __func__
, af
);
255 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
260 struct zebra_vrf
*zvrf
;
262 zvrf
= vrf_info_lookup(vrf_id
);
266 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
267 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
, &exists
);
268 if (rnh
&& !listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
269 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
271 zebra_evaluate_rnh(zvrf
, family2afi(pw
->af
), 1,
272 RNH_NEXTHOP_TYPE
, &nh
);
276 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
284 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
287 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
290 /* Apply the NHT route-map for a client to the route (and nexthops)
293 static int zebra_rnh_apply_nht_rmap(afi_t afi
, struct zebra_vrf
*zvrf
,
294 struct route_node
*prn
,
295 struct route_entry
*re
, int proto
)
297 int at_least_one
= 0;
298 struct nexthop
*nexthop
;
302 for (nexthop
= re
->ng
.nexthop
; nexthop
;
303 nexthop
= nexthop
->next
) {
304 ret
= zebra_nht_route_map_check(
305 afi
, proto
, &prn
->p
, zvrf
, re
, nexthop
);
306 if (ret
!= RMAP_DENYMATCH
) {
307 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
308 at_least_one
++; /* at least one valid NH */
310 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
314 return (at_least_one
);
318 * Determine appropriate route (RE entry) resolving a tracked BGP route
319 * for BGP route for import.
321 static struct route_entry
*
322 zebra_rnh_resolve_import_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
323 struct route_node
*nrn
, struct rnh
*rnh
,
324 struct route_node
**prn
)
326 struct route_table
*route_table
;
327 struct route_node
*rn
;
328 struct route_entry
*re
;
332 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
333 if (!route_table
) // unexpected
336 rn
= route_node_match(route_table
, &nrn
->p
);
340 /* Unlock route node - we don't need to lock when walking the tree. */
341 route_unlock_node(rn
);
343 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
344 && !prefix_same(&nrn
->p
, &rn
->p
))
347 /* Identify appropriate route entry. */
348 RNODE_FOREACH_RE (rn
, re
) {
349 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
350 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
351 && (re
->type
!= ZEBRA_ROUTE_BGP
))
361 * See if a tracked route entry for import (by BGP) has undergone any
362 * change, and if so, notify the client.
364 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid
, afi_t afi
,
365 int force
, struct route_node
*nrn
,
367 struct route_entry
*re
)
369 int state_changed
= 0;
370 struct zserv
*client
;
371 char bufn
[INET6_ADDRSTRLEN
];
372 struct listnode
*node
;
374 if (re
&& (rnh
->state
== NULL
)) {
375 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
377 } else if (!re
&& (rnh
->state
!= NULL
))
380 if (compare_state(re
, rnh
->state
))
381 copy_state(rnh
, re
, nrn
);
383 if (state_changed
|| force
) {
384 if (IS_ZEBRA_DEBUG_NHT
) {
385 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
386 zlog_debug("%u:%s: Route import check %s %s", vrfid
,
387 bufn
, rnh
->state
? "passed" : "failed",
388 state_changed
? "(state changed)" : "");
390 /* state changed, notify clients */
391 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
392 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
398 * Notify clients registered for this nexthop about a change.
400 static void zebra_rnh_notify_protocol_clients(struct zebra_vrf
*zvrf
, afi_t afi
,
401 struct route_node
*nrn
,
403 struct route_node
*prn
,
404 struct route_entry
*re
)
406 struct listnode
*node
;
407 struct zserv
*client
;
408 char bufn
[INET6_ADDRSTRLEN
];
409 char bufp
[INET6_ADDRSTRLEN
];
410 int num_resolving_nh
;
412 if (IS_ZEBRA_DEBUG_NHT
) {
413 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
415 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
416 zlog_debug("%u:%s: NH resolved over route %s",
417 zvrf
->vrf
->vrf_id
, bufn
, bufp
);
419 zlog_debug("%u:%s: NH has become unresolved",
420 zvrf
->vrf
->vrf_id
, bufn
);
423 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
425 /* Apply route-map for this client to route resolving
427 * nexthop to see if it is filtered or not.
429 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
430 afi
, zvrf
, prn
, re
, client
->proto
);
431 if (num_resolving_nh
)
432 rnh
->filtered
[client
->proto
] = 0;
434 rnh
->filtered
[client
->proto
] = 1;
436 if (IS_ZEBRA_DEBUG_NHT
)
438 "%u:%s: Notifying client %s about NH %s",
439 zvrf
->vrf
->vrf_id
, bufn
,
440 zebra_route_string(client
->proto
),
443 : "(filtered by route-map)");
445 rnh
->filtered
[client
->proto
] = 0;
446 if (IS_ZEBRA_DEBUG_NHT
)
448 "%u:%s: Notifying client %s about NH (unreachable)",
449 zvrf
->vrf
->vrf_id
, bufn
,
450 zebra_route_string(client
->proto
));
453 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, zvrf
->vrf
->vrf_id
);
457 static void zebra_rnh_process_pbr_tables(afi_t afi
, struct route_node
*nrn
,
459 struct route_node
*prn
,
460 struct route_entry
*re
)
462 struct zebra_router_table
*zrt
;
463 struct route_entry
*o_re
;
464 struct route_node
*o_rn
;
465 struct listnode
*node
;
466 struct zserv
*client
;
469 * We are only concerned about nexthops that change for
472 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
473 if (client
->proto
== ZEBRA_ROUTE_PBR
)
480 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
484 for (o_rn
= route_top(zrt
->table
); o_rn
;
485 o_rn
= srcdest_route_next(o_rn
)) {
486 RNODE_FOREACH_RE (o_rn
, o_re
) {
487 if (o_re
->type
== ZEBRA_ROUTE_PBR
)
493 * If we have a PBR route and a nexthop changes
494 * just rethink it. Yes this is a hammer, but
498 SET_FLAG(o_re
->status
, ROUTE_ENTRY_CHANGED
);
506 * Utility to determine whether a candidate nexthop is useable. We make this
507 * check in a couple of places, so this is a single home for the logic we
510 static bool rnh_nexthop_valid(const struct route_entry
*re
,
511 const struct nexthop
*nh
)
513 return (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
514 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
));
518 * Determine appropriate route (route entry) resolving a tracked
521 static struct route_entry
*
522 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
523 struct route_node
*nrn
, struct rnh
*rnh
,
524 struct route_node
**prn
)
526 struct route_table
*route_table
;
527 struct route_node
*rn
;
528 struct route_entry
*re
;
529 struct nexthop
*nexthop
;
533 route_table
= zvrf
->table
[afi
][SAFI_UNICAST
];
537 rn
= route_node_match(route_table
, &nrn
->p
);
541 /* Unlock route node - we don't need to lock when walking the tree. */
542 route_unlock_node(rn
);
544 /* While resolving nexthops, we may need to walk up the tree from the
545 * most-specific match. Do similar logic as in zebra_rib.c
548 /* Do not resolve over default route unless allowed &&
549 * match route to be exact if so specified
551 if (is_default_prefix(&rn
->p
)
552 && !rnh_resolve_via_default(rn
->p
.family
))
555 /* Identify appropriate route entry. */
556 RNODE_FOREACH_RE (rn
, re
) {
557 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
559 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
562 /* Just being SELECTED isn't quite enough - must
563 * have an installed nexthop to be useful.
565 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
566 if (rnh_nexthop_valid(re
, nexthop
))
573 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
574 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
575 || (re
->type
== ZEBRA_ROUTE_STATIC
))
577 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
579 for (nexthop
= re
->ng
.nexthop
; nexthop
;
580 nexthop
= nexthop
->next
)
582 == NEXTHOP_TYPE_IFINDEX
)
591 /* Route entry found, we're done; else, walk up the tree. */
597 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
606 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
609 struct listnode
*node
;
611 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
616 * See if a tracked nexthop entry has undergone any change, and if so,
617 * take appropriate action; this involves notifying any clients and/or
618 * scheduling dependent static routes for processing.
620 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
621 int force
, struct route_node
*nrn
,
623 struct route_node
*prn
,
624 struct route_entry
*re
)
626 int state_changed
= 0;
628 /* If we're resolving over a different route, resolution has changed or
629 * the resolving route has some change (e.g., metric), there is a state
632 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
634 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
636 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
638 copy_state(rnh
, re
, nrn
);
640 } else if (compare_state(re
, rnh
->state
)) {
641 copy_state(rnh
, re
, nrn
);
645 if (state_changed
|| force
) {
646 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
649 /* Notify registered protocol clients. */
650 zebra_rnh_notify_protocol_clients(zvrf
, afi
, nrn
, rnh
, prn
,
653 zebra_rnh_process_pbr_tables(afi
, nrn
, rnh
, prn
, rnh
->state
);
655 /* Process pseudowires attached to this nexthop */
656 zebra_rnh_process_pseudowires(zvrf
->vrf
->vrf_id
, rnh
);
660 /* Evaluate one tracked entry */
661 static void zebra_rnh_evaluate_entry(struct zebra_vrf
*zvrf
, afi_t afi
,
662 int force
, rnh_type_t type
,
663 struct route_node
*nrn
)
666 struct route_entry
*re
;
667 struct route_node
*prn
;
668 char bufn
[INET6_ADDRSTRLEN
];
670 if (IS_ZEBRA_DEBUG_NHT
) {
671 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
672 zlog_debug("%u:%s: Evaluate RNH, type %d %s", zvrf
->vrf
->vrf_id
,
673 bufn
, type
, force
? "(force)" : "");
678 /* Identify route entry (RE) resolving this tracked entry. */
679 if (type
== RNH_IMPORT_CHECK_TYPE
)
680 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
682 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
, &prn
);
684 /* If the entry cannot be resolved and that is also the existing state,
685 * there is nothing further to do.
687 if (!re
&& rnh
->state
== NULL
&& !force
)
690 /* Process based on type of entry. */
691 if (type
== RNH_IMPORT_CHECK_TYPE
)
692 zebra_rnh_eval_import_check_entry(zvrf
->vrf
->vrf_id
, afi
, force
,
695 zebra_rnh_eval_nexthop_entry(zvrf
, afi
, force
, nrn
, rnh
, prn
,
700 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
701 * from the re entries.
703 * Please note we are doing this *after* we have
704 * notified the world about each nexthop as that
705 * we can have a situation where one re entry
706 * covers multiple nexthops we are interested in.
708 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf
*zvrf
, afi_t afi
,
709 rnh_type_t type
, struct route_node
*nrn
)
712 struct route_entry
*re
;
713 struct route_node
*prn
;
717 /* Identify route entry (RIB) resolving this tracked entry. */
718 if (type
== RNH_IMPORT_CHECK_TYPE
)
719 re
= zebra_rnh_resolve_import_entry(zvrf
, afi
, nrn
, rnh
,
722 re
= zebra_rnh_resolve_nexthop_entry(zvrf
, afi
, nrn
, rnh
,
726 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
727 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
731 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
732 * of a particular VRF and address-family or a specific prefix.
734 void zebra_evaluate_rnh(struct zebra_vrf
*zvrf
, afi_t afi
, int force
,
735 rnh_type_t type
, struct prefix
*p
)
737 struct route_table
*rnh_table
;
738 struct route_node
*nrn
;
740 rnh_table
= get_rnh_table(zvrf
->vrf
->vrf_id
, afi
, type
);
741 if (!rnh_table
) // unexpected
745 /* Evaluating a specific entry, make sure it exists. */
746 nrn
= route_node_lookup(rnh_table
, p
);
747 if (nrn
&& nrn
->info
)
748 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
, nrn
);
751 route_unlock_node(nrn
);
753 /* Evaluate entire table. */
754 nrn
= route_top(rnh_table
);
757 zebra_rnh_evaluate_entry(zvrf
, afi
, force
, type
,
759 nrn
= route_next(nrn
); /* this will also unlock nrn */
761 nrn
= route_top(rnh_table
);
764 zebra_rnh_clear_nhc_flag(zvrf
, afi
, type
, nrn
);
765 nrn
= route_next(nrn
); /* this will also unlock nrn */
770 void zebra_print_rnh_table(vrf_id_t vrfid
, afi_t afi
, struct vty
*vty
,
773 struct route_table
*table
;
774 struct route_node
*rn
;
776 table
= get_rnh_table(vrfid
, afi
, type
);
778 zlog_debug("print_rnhs: rnh table not found");
782 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
788 * free_state - free up the re structure associated with the rnh.
790 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
791 struct route_node
*rn
)
797 /* free RE and nexthops */
798 nexthops_free(re
->ng
.nexthop
);
802 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
803 struct route_node
*rn
)
805 struct route_entry
*state
;
808 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
815 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
816 state
->type
= re
->type
;
817 state
->distance
= re
->distance
;
818 state
->metric
= re
->metric
;
819 state
->vrf_id
= re
->vrf_id
;
820 state
->status
= re
->status
;
822 route_entry_copy_nexthops(state
, re
->ng
.nexthop
);
826 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
832 if ((!r1
&& r2
) || (r1
&& !r2
))
835 if (r1
->distance
!= r2
->distance
)
838 if (r1
->metric
!= r2
->metric
)
841 if (r1
->nexthop_num
!= r2
->nexthop_num
)
844 if (CHECK_FLAG(r1
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
)
845 || CHECK_FLAG(r1
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
851 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
855 struct route_entry
*re
;
859 struct route_node
*rn
;
860 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
861 : ZEBRA_NEXTHOP_UPDATE
;
866 /* Get output stream. */
867 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
869 zclient_create_header(s
, cmd
, vrf_id
);
871 stream_putw(s
, rn
->p
.family
);
872 switch (rn
->p
.family
) {
874 stream_putc(s
, rn
->p
.prefixlen
);
875 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
878 stream_putc(s
, rn
->p
.prefixlen
);
879 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
882 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY
,
883 "%s: Unknown family (%d) notification attempted\n",
884 __FUNCTION__
, rn
->p
.family
);
888 stream_putc(s
, re
->type
);
889 stream_putw(s
, re
->instance
);
890 stream_putc(s
, re
->distance
);
891 stream_putl(s
, re
->metric
);
893 nump
= stream_get_endp(s
);
895 for (ALL_NEXTHOPS(re
->ng
, nh
))
896 if (rnh_nexthop_valid(re
, nh
)) {
897 stream_putl(s
, nh
->vrf_id
);
898 stream_putc(s
, nh
->type
);
900 case NEXTHOP_TYPE_IPV4
:
901 case NEXTHOP_TYPE_IPV4_IFINDEX
:
902 stream_put_in_addr(s
, &nh
->gate
.ipv4
);
903 stream_putl(s
, nh
->ifindex
);
905 case NEXTHOP_TYPE_IFINDEX
:
906 stream_putl(s
, nh
->ifindex
);
908 case NEXTHOP_TYPE_IPV6
:
909 case NEXTHOP_TYPE_IPV6_IFINDEX
:
910 stream_put(s
, &nh
->gate
.ipv6
, 16);
911 stream_putl(s
, nh
->ifindex
);
919 nh
->nh_label
->num_labels
);
920 if (nh
->nh_label
->num_labels
)
923 &nh
->nh_label
->label
[0],
924 nh
->nh_label
->num_labels
925 * sizeof(mpls_label_t
));
930 stream_putc_at(s
, nump
, num
);
932 stream_putc(s
, 0); // type
933 stream_putw(s
, 0); // instance
934 stream_putc(s
, 0); // distance
935 stream_putl(s
, 0); // metric
936 stream_putc(s
, 0); // nexthops
938 stream_putw_at(s
, 0, stream_get_endp(s
));
940 client
->nh_last_upd_time
= monotime(NULL
);
941 client
->last_write_cmd
= cmd
;
942 return zserv_send_message(client
, s
);
945 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
948 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
950 switch (nexthop
->type
) {
951 case NEXTHOP_TYPE_IPV4
:
952 case NEXTHOP_TYPE_IPV4_IFINDEX
:
953 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
954 if (nexthop
->ifindex
)
956 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
958 case NEXTHOP_TYPE_IPV6
:
959 case NEXTHOP_TYPE_IPV6_IFINDEX
:
961 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
962 if (nexthop
->ifindex
)
963 vty_out(vty
, ", via %s",
964 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
966 case NEXTHOP_TYPE_IFINDEX
:
967 vty_out(vty
, " is directly connected, %s",
968 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
970 case NEXTHOP_TYPE_BLACKHOLE
:
971 vty_out(vty
, " is directly connected, Null0");
979 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
982 struct nexthop
*nexthop
;
983 struct listnode
*node
;
984 struct zserv
*client
;
988 vty_out(vty
, "%s%s\n",
989 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
990 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
993 vty_out(vty
, " resolved via %s\n",
994 zebra_route_string(rnh
->state
->type
));
995 for (nexthop
= rnh
->state
->ng
.nexthop
; nexthop
;
996 nexthop
= nexthop
->next
)
997 print_nh(nexthop
, vty
);
999 vty_out(vty
, " unresolved%s\n",
1000 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
1004 vty_out(vty
, " Client list:");
1005 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1006 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1008 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1009 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1010 vty_out(vty
, " zebra[pseudowires]");
1014 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, afi_t afi
,
1015 struct zserv
*client
, rnh_type_t type
)
1017 struct route_table
*ntable
;
1018 struct route_node
*nrn
;
1021 if (IS_ZEBRA_DEBUG_NHT
)
1022 zlog_debug("%u: Client %s RNH cleanup for family %s type %d",
1023 vrf_id
, zebra_route_string(client
->proto
),
1024 afi2str(afi
), type
);
1026 ntable
= get_rnh_table(vrf_id
, afi
, type
);
1028 zlog_debug("cleanup_rnh_client: rnh table not found");
1032 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1037 zebra_remove_rnh_client(rnh
, client
, type
);
1042 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1043 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1046 struct zebra_vrf
*zvrf
;
1048 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1051 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1053 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1055 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP
, client
,
1056 RNH_IMPORT_CHECK_TYPE
);
1057 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AFI_IP6
, client
,
1058 RNH_IMPORT_CHECK_TYPE
);
1059 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
1060 hash_iterate(zvrf
->lsp_table
,
1061 mpls_ldp_lsp_uninstall_all
,
1063 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
1064 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);