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
);
69 static int zebra_client_cleanup_rnh(struct zserv
*client
);
71 int zebra_rnh_ip_default_route
= 0;
72 int zebra_rnh_ipv6_default_route
= 0;
74 void zebra_rnh_init(void)
76 hook_register(zserv_client_close
, zebra_client_cleanup_rnh
);
79 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, int family
,
82 struct zebra_vrf
*zvrf
;
83 struct route_table
*t
= NULL
;
85 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
88 case RNH_NEXTHOP_TYPE
:
89 t
= zvrf
->rnh_table
[family2afi(family
)];
91 case RNH_IMPORT_CHECK_TYPE
:
92 t
= zvrf
->import_check_table
[family2afi(family
)];
99 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
101 prefix2str(&(rnh
->node
->p
), buf
, size
);
105 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
107 struct route_table
*table
;
108 struct route_node
*rn
;
109 struct rnh
*rnh
= NULL
;
110 char buf
[PREFIX2STR_BUFFER
];
112 if (IS_ZEBRA_DEBUG_NHT
) {
113 prefix2str(p
, buf
, sizeof(buf
));
114 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
116 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
118 prefix2str(p
, buf
, sizeof(buf
));
119 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid
,
124 /* Make it sure prefixlen is applied to the prefix. */
127 /* Lookup (or add) route node.*/
128 rn
= route_node_get(table
, p
);
131 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
132 rnh
->client_list
= list_new();
134 rnh
->zebra_pseudowire_list
= list_new();
140 route_unlock_node(rn
);
144 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
146 struct route_table
*table
;
147 struct route_node
*rn
;
149 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
153 /* Make it sure prefixlen is applied to the prefix. */
156 /* Lookup route node.*/
157 rn
= route_node_lookup(table
, p
);
161 route_unlock_node(rn
);
165 void zebra_free_rnh(struct rnh
*rnh
)
167 rnh
->flags
|= ZEBRA_NHT_DELETED
;
168 list_delete_and_null(&rnh
->client_list
);
169 list_delete_and_null(&rnh
->zebra_pseudowire_list
);
170 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
171 XFREE(MTYPE_RNH
, rnh
);
174 void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
176 struct route_node
*rn
;
178 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
181 if (IS_ZEBRA_DEBUG_NHT
) {
182 char buf
[PREFIX2STR_BUFFER
];
183 zlog_debug("%u: Del RNH %s type %d", rnh
->vrf_id
,
184 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
189 route_unlock_node(rn
);
192 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
193 rnh_type_t type
, vrf_id_t vrf_id
)
195 if (IS_ZEBRA_DEBUG_NHT
) {
196 char buf
[PREFIX2STR_BUFFER
];
197 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
198 zebra_route_string(client
->proto
),
199 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
201 if (!listnode_lookup(rnh
->client_list
, client
)) {
202 listnode_add(rnh
->client_list
, client
);
203 send_client(rnh
, client
, type
,
204 vrf_id
); // Pending: check if its needed
208 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
211 if (IS_ZEBRA_DEBUG_NHT
) {
212 char buf
[PREFIX2STR_BUFFER
];
213 zlog_debug("Client %s unregisters for RNH %s type %d",
214 zebra_route_string(client
->proto
),
215 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
217 listnode_delete(rnh
->client_list
, client
);
218 if (list_isempty(rnh
->client_list
)
219 && list_isempty(rnh
->zebra_pseudowire_list
))
220 zebra_delete_rnh(rnh
, type
);
223 /* XXX move this utility function elsewhere? */
224 static void addr2hostprefix(int af
, const union g_addr
*addr
,
225 struct prefix
*prefix
)
229 prefix
->family
= AF_INET
;
230 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
231 prefix
->u
.prefix4
= addr
->ipv4
;
234 prefix
->family
= AF_INET6
;
235 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
236 prefix
->u
.prefix6
= addr
->ipv6
;
239 memset(prefix
, 0, sizeof(*prefix
));
240 zlog_warn("%s: unknown address family %d", __func__
, af
);
245 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
250 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
251 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
252 if (rnh
&& !listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
253 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
255 zebra_evaluate_rnh(vrf_id
, pw
->af
, 1, RNH_NEXTHOP_TYPE
, &nh
);
259 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
267 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
270 if (list_isempty(rnh
->client_list
)
271 && list_isempty(rnh
->zebra_pseudowire_list
))
272 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
275 /* Apply the NHT route-map for a client to the route (and nexthops)
278 static int zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
279 struct route_entry
*re
, int proto
)
281 int at_least_one
= 0;
282 int rmap_family
; /* Route map has diff AF family enum */
283 struct nexthop
*nexthop
;
286 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
289 for (nexthop
= re
->ng
.nexthop
; nexthop
;
290 nexthop
= nexthop
->next
) {
291 ret
= zebra_nht_route_map_check(rmap_family
, proto
,
292 &prn
->p
, re
, nexthop
);
293 if (ret
!= RMAP_DENYMATCH
) {
294 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
295 at_least_one
++; /* at least one valid NH */
297 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
301 return (at_least_one
);
305 * Determine appropriate route (RE entry) resolving a tracked BGP route
306 * for BGP route for import.
308 static struct route_entry
*
309 zebra_rnh_resolve_import_entry(vrf_id_t vrfid
, int family
,
310 struct route_node
*nrn
, struct rnh
*rnh
,
311 struct route_node
**prn
)
313 struct route_table
*route_table
;
314 struct route_node
*rn
;
315 struct route_entry
*re
;
319 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
320 if (!route_table
) // unexpected
323 rn
= route_node_match(route_table
, &nrn
->p
);
327 /* Unlock route node - we don't need to lock when walking the tree. */
328 route_unlock_node(rn
);
330 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
331 && !prefix_same(&nrn
->p
, &rn
->p
))
334 /* Identify appropriate route entry. */
335 RNODE_FOREACH_RE (rn
, re
) {
336 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
337 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
338 && (re
->type
!= ZEBRA_ROUTE_BGP
))
348 * See if a tracked route entry for import (by BGP) has undergone any
349 * change, and if so, notify the client.
351 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid
, int family
,
352 int force
, struct route_node
*nrn
,
354 struct route_entry
*re
)
356 int state_changed
= 0;
357 struct zserv
*client
;
358 char bufn
[INET6_ADDRSTRLEN
];
359 struct listnode
*node
;
360 struct nexthop
*nexthop
;
362 if (re
&& (rnh
->state
== NULL
)) {
363 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
364 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
368 } else if (!re
&& (rnh
->state
!= NULL
))
371 if (compare_state(re
, rnh
->state
))
372 copy_state(rnh
, re
, nrn
);
374 if (state_changed
|| force
) {
375 if (IS_ZEBRA_DEBUG_NHT
) {
376 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
377 zlog_debug("%u:%s: Route import check %s %s\n", vrfid
,
378 bufn
, rnh
->state
? "passed" : "failed",
379 state_changed
? "(state changed)" : "");
381 /* state changed, notify clients */
382 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
383 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
389 * Notify clients registered for this nexthop about a change.
391 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid
, int family
,
392 struct route_node
*nrn
,
394 struct route_node
*prn
,
395 struct route_entry
*re
)
397 struct listnode
*node
;
398 struct zserv
*client
;
399 char bufn
[INET6_ADDRSTRLEN
];
400 char bufp
[INET6_ADDRSTRLEN
];
401 int num_resolving_nh
;
403 if (IS_ZEBRA_DEBUG_NHT
) {
404 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
406 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
407 zlog_debug("%u:%s: NH resolved over route %s", vrfid
,
410 zlog_debug("%u:%s: NH has become unresolved", vrfid
,
414 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
416 /* Apply route-map for this client to route resolving
418 * nexthop to see if it is filtered or not.
420 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
421 family
, prn
, re
, client
->proto
);
422 if (num_resolving_nh
)
423 rnh
->filtered
[client
->proto
] = 0;
425 rnh
->filtered
[client
->proto
] = 1;
427 if (IS_ZEBRA_DEBUG_NHT
)
429 "%u:%s: Notifying client %s about NH %s",
431 zebra_route_string(client
->proto
),
434 : "(filtered by route-map)");
436 rnh
->filtered
[client
->proto
] = 0;
437 if (IS_ZEBRA_DEBUG_NHT
)
439 "%u:%s: Notifying client %s about NH (unreachable)",
441 zebra_route_string(client
->proto
));
444 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
448 static void zebra_rnh_process_pbr_tables(int family
,
449 struct route_node
*nrn
,
451 struct route_node
*prn
,
452 struct route_entry
*re
)
454 struct zebra_ns_table
*znst
;
455 struct route_entry
*o_re
;
456 struct route_node
*o_rn
;
457 struct listnode
*node
;
458 struct zserv
*client
;
459 struct zebra_ns
*zns
;
462 if (family
== AF_INET6
)
466 * We are only concerned about nexthops that change for
469 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
470 if (client
->proto
== ZEBRA_ROUTE_PBR
)
477 zns
= zebra_ns_lookup(NS_DEFAULT
);
478 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
479 if (afi
!= znst
->afi
)
482 for (o_rn
= route_top(znst
->table
);
483 o_rn
; o_rn
= srcdest_route_next(o_rn
)) {
484 RNODE_FOREACH_RE (o_rn
, o_re
) {
485 if (o_re
->type
== ZEBRA_ROUTE_PBR
)
491 * If we have a PBR route and a nexthop changes
492 * just rethink it. Yes this is a hammer, but
496 SET_FLAG(o_re
->status
, ROUTE_ENTRY_CHANGED
);
504 * Determine appropriate route (route entry) resolving a tracked
507 static struct route_entry
*
508 zebra_rnh_resolve_nexthop_entry(vrf_id_t vrfid
, int family
,
509 struct route_node
*nrn
, struct rnh
*rnh
,
510 struct route_node
**prn
)
512 struct route_table
*route_table
;
513 struct route_node
*rn
;
514 struct route_entry
*re
;
518 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
522 rn
= route_node_match(route_table
, &nrn
->p
);
526 /* Unlock route node - we don't need to lock when walking the tree. */
527 route_unlock_node(rn
);
529 /* While resolving nexthops, we may need to walk up the tree from the
530 * most-specific match. Do similar logic as in zebra_rib.c
533 /* Do not resolve over default route unless allowed &&
534 * match route to be exact if so specified
536 if (is_default_prefix(&rn
->p
)
537 && !rnh_resolve_via_default(rn
->p
.family
))
540 /* Identify appropriate route entry. */
541 RNODE_FOREACH_RE (rn
, re
) {
542 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
544 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
547 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
548 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
549 || (re
->type
== ZEBRA_ROUTE_STATIC
))
551 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
552 struct nexthop
*nexthop
;
554 for (nexthop
= re
->ng
.nexthop
; nexthop
;
555 nexthop
= nexthop
->next
)
557 == NEXTHOP_TYPE_IFINDEX
)
566 /* Route entry found, we're done; else, walk up the tree. */
572 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
581 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
584 struct listnode
*node
;
586 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
591 * See if a tracked nexthop entry has undergone any change, and if so,
592 * take appropriate action; this involves notifying any clients and/or
593 * scheduling dependent static routes for processing.
595 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid
, int family
, int force
,
596 struct route_node
*nrn
,
598 struct route_node
*prn
,
599 struct route_entry
*re
)
601 int state_changed
= 0;
603 /* If we're resolving over a different route, resolution has changed or
604 * the resolving route has some change (e.g., metric), there is a state
607 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
609 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
611 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
613 copy_state(rnh
, re
, nrn
);
615 } else if (compare_state(re
, rnh
->state
)) {
616 copy_state(rnh
, re
, nrn
);
620 if (state_changed
|| force
) {
621 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
624 /* Notify registered protocol clients. */
625 zebra_rnh_notify_protocol_clients(vrfid
, family
, nrn
, rnh
, prn
,
628 zebra_rnh_process_pbr_tables(family
, nrn
, rnh
, prn
,
631 /* Process pseudowires attached to this nexthop */
632 zebra_rnh_process_pseudowires(vrfid
, rnh
);
636 /* Evaluate one tracked entry */
637 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid
, int family
, int force
,
638 rnh_type_t type
, struct route_node
*nrn
)
641 struct route_entry
*re
;
642 struct route_node
*prn
;
643 char bufn
[INET6_ADDRSTRLEN
];
645 if (IS_ZEBRA_DEBUG_NHT
) {
646 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
647 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid
, bufn
, type
,
648 force
? "(force)" : "");
653 /* Identify route entry (RE) resolving this tracked entry. */
654 if (type
== RNH_IMPORT_CHECK_TYPE
)
655 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
658 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
661 /* If the entry cannot be resolved and that is also the existing state,
662 * there is nothing further to do.
664 if (!re
&& rnh
->state
== NULL
&& !force
)
667 /* Process based on type of entry. */
668 if (type
== RNH_IMPORT_CHECK_TYPE
)
669 zebra_rnh_eval_import_check_entry(vrfid
, family
, force
, nrn
,
672 zebra_rnh_eval_nexthop_entry(vrfid
, family
, force
, nrn
, rnh
,
677 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
678 * from the re entries.
680 * Please note we are doing this *after* we have
681 * notified the world about each nexthop as that
682 * we can have a situation where one re entry
683 * covers multiple nexthops we are interested in.
685 static void zebra_rnh_clear_nhc_flag(vrf_id_t vrfid
, int family
,
686 rnh_type_t type
, struct route_node
*nrn
)
689 struct route_entry
*re
;
690 struct route_node
*prn
;
694 /* Identify route entry (RIB) resolving this tracked entry. */
695 if (type
== RNH_IMPORT_CHECK_TYPE
)
696 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
699 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
703 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
704 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
708 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
709 * of a particular VRF and address-family or a specific prefix.
711 void zebra_evaluate_rnh(vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
714 struct route_table
*rnh_table
;
715 struct route_node
*nrn
;
717 rnh_table
= get_rnh_table(vrfid
, family
, type
);
718 if (!rnh_table
) // unexpected
722 /* Evaluating a specific entry, make sure it exists. */
723 nrn
= route_node_lookup(rnh_table
, p
);
724 if (nrn
&& nrn
->info
)
725 zebra_rnh_evaluate_entry(vrfid
, family
, force
, type
,
729 route_unlock_node(nrn
);
731 /* Evaluate entire table. */
732 nrn
= route_top(rnh_table
);
735 zebra_rnh_evaluate_entry(vrfid
, family
, force
,
737 nrn
= route_next(nrn
); /* this will also unlock nrn */
739 nrn
= route_top(rnh_table
);
742 zebra_rnh_clear_nhc_flag(vrfid
, family
, type
,
744 nrn
= route_next(nrn
); /* this will also unlock nrn */
749 void zebra_print_rnh_table(vrf_id_t vrfid
, int af
, struct vty
*vty
,
752 struct route_table
*table
;
753 struct route_node
*rn
;
755 table
= get_rnh_table(vrfid
, af
, type
);
757 zlog_debug("print_rnhs: rnh table not found\n");
761 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
767 * free_state - free up the re structure associated with the rnh.
769 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
770 struct route_node
*rn
)
776 /* free RE and nexthops */
777 nexthops_free(re
->ng
.nexthop
);
781 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
782 struct route_node
*rn
)
784 struct route_entry
*state
;
787 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
794 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
795 state
->type
= re
->type
;
796 state
->distance
= re
->distance
;
797 state
->metric
= re
->metric
;
798 state
->vrf_id
= re
->vrf_id
;
800 route_entry_copy_nexthops(state
, re
->ng
.nexthop
);
804 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
810 if ((!r1
&& r2
) || (r1
&& !r2
))
813 if (r1
->distance
!= r2
->distance
)
816 if (r1
->metric
!= r2
->metric
)
819 if (r1
->nexthop_num
!= r2
->nexthop_num
)
822 if (CHECK_FLAG(r1
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
)
823 || CHECK_FLAG(r1
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
829 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
833 struct route_entry
*re
;
837 struct route_node
*rn
;
838 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
839 : ZEBRA_NEXTHOP_UPDATE
;
844 /* Get output stream. */
845 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
847 zclient_create_header(s
, cmd
, vrf_id
);
849 stream_putw(s
, rn
->p
.family
);
850 switch (rn
->p
.family
) {
852 stream_putc(s
, rn
->p
.prefixlen
);
853 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
856 stream_putc(s
, rn
->p
.prefixlen
);
857 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
860 zlog_err("%s: Unknown family (%d) notification attempted\n",
861 __FUNCTION__
, rn
->p
.family
);
865 stream_putc(s
, re
->type
);
866 stream_putw(s
, re
->instance
);
867 stream_putc(s
, re
->distance
);
868 stream_putl(s
, re
->metric
);
870 nump
= stream_get_endp(s
);
872 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
873 if ((CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_FIB
)
874 || CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
875 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
876 stream_putc(s
, nh
->type
);
878 case NEXTHOP_TYPE_IPV4
:
879 case NEXTHOP_TYPE_IPV4_IFINDEX
:
880 stream_put_in_addr(s
, &nh
->gate
.ipv4
);
881 stream_putl(s
, nh
->ifindex
);
883 case NEXTHOP_TYPE_IFINDEX
:
884 stream_putl(s
, nh
->ifindex
);
886 case NEXTHOP_TYPE_IPV6
:
887 case NEXTHOP_TYPE_IPV6_IFINDEX
:
888 stream_put(s
, &nh
->gate
.ipv6
, 16);
889 stream_putl(s
, nh
->ifindex
);
897 nh
->nh_label
->num_labels
);
898 if (nh
->nh_label
->num_labels
)
901 &nh
->nh_label
->label
[0],
902 nh
->nh_label
->num_labels
903 * sizeof(mpls_label_t
));
908 stream_putc_at(s
, nump
, num
);
910 stream_putc(s
, 0); // type
911 stream_putw(s
, 0); // instance
912 stream_putc(s
, 0); // distance
913 stream_putl(s
, 0); // metric
914 stream_putc(s
, 0); // nexthops
916 stream_putw_at(s
, 0, stream_get_endp(s
));
918 client
->nh_last_upd_time
= monotime(NULL
);
919 client
->last_write_cmd
= cmd
;
920 return zserv_send_message(client
, s
);
923 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
926 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
928 switch (nexthop
->type
) {
929 case NEXTHOP_TYPE_IPV4
:
930 case NEXTHOP_TYPE_IPV4_IFINDEX
:
931 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
932 if (nexthop
->ifindex
)
934 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
936 case NEXTHOP_TYPE_IPV6
:
937 case NEXTHOP_TYPE_IPV6_IFINDEX
:
939 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
940 if (nexthop
->ifindex
)
941 vty_out(vty
, ", via %s",
942 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
944 case NEXTHOP_TYPE_IFINDEX
:
945 vty_out(vty
, " is directly connected, %s",
946 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
948 case NEXTHOP_TYPE_BLACKHOLE
:
949 vty_out(vty
, " is directly connected, Null0");
957 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
960 struct nexthop
*nexthop
;
961 struct listnode
*node
;
962 struct zserv
*client
;
966 vty_out(vty
, "%s%s\n",
967 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
968 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
971 vty_out(vty
, " resolved via %s\n",
972 zebra_route_string(rnh
->state
->type
));
973 for (nexthop
= rnh
->state
->ng
.nexthop
; nexthop
;
974 nexthop
= nexthop
->next
)
975 print_nh(nexthop
, vty
);
977 vty_out(vty
, " unresolved%s\n",
978 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
982 vty_out(vty
, " Client list:");
983 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
984 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
986 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
987 if (!list_isempty(rnh
->zebra_pseudowire_list
))
988 vty_out(vty
, " zebra[pseudowires]");
992 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, int family
,
993 struct zserv
*client
, rnh_type_t type
)
995 struct route_table
*ntable
;
996 struct route_node
*nrn
;
999 if (IS_ZEBRA_DEBUG_NHT
)
1000 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
1001 vrf_id
, zebra_route_string(client
->proto
), family
,
1004 ntable
= get_rnh_table(vrf_id
, family
, type
);
1006 zlog_debug("cleanup_rnh_client: rnh table not found\n");
1010 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1015 zebra_remove_rnh_client(rnh
, client
, type
);
1020 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1021 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1024 struct zebra_vrf
*zvrf
;
1026 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1029 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1031 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
1032 client
, RNH_NEXTHOP_TYPE
);
1033 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1034 RNH_IMPORT_CHECK_TYPE
);
1035 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
1036 client
, RNH_IMPORT_CHECK_TYPE
);
1037 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
1038 hash_iterate(zvrf
->lsp_table
,
1039 mpls_ldp_lsp_uninstall_all
,
1041 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
1042 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);