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"
50 #include "zebra/zebra_errors.h"
52 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
53 struct route_node
*rn
);
54 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
55 struct route_node
*rn
);
56 #define lookup_rnh_table(v, f) \
58 struct zebra_vrf *zvrf; \
59 struct route_table *t = NULL; \
60 zvrf = zebra_vrf_lookup_by_id(v); \
62 t = zvrf->rnh_table[family2afi(f)]; \
66 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
);
67 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
69 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
70 static int zebra_client_cleanup_rnh(struct zserv
*client
);
72 int zebra_rnh_ip_default_route
= 0;
73 int zebra_rnh_ipv6_default_route
= 0;
75 void zebra_rnh_init(void)
77 hook_register(zserv_client_close
, zebra_client_cleanup_rnh
);
80 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, int family
,
83 struct zebra_vrf
*zvrf
;
84 struct route_table
*t
= NULL
;
86 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
89 case RNH_NEXTHOP_TYPE
:
90 t
= zvrf
->rnh_table
[family2afi(family
)];
92 case RNH_IMPORT_CHECK_TYPE
:
93 t
= zvrf
->import_check_table
[family2afi(family
)];
100 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
102 prefix2str(&(rnh
->node
->p
), buf
, size
);
106 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
,
109 struct route_table
*table
;
110 struct route_node
*rn
;
111 struct rnh
*rnh
= NULL
;
112 char buf
[PREFIX2STR_BUFFER
];
114 if (IS_ZEBRA_DEBUG_NHT
) {
115 prefix2str(p
, buf
, sizeof(buf
));
116 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
118 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
120 prefix2str(p
, buf
, sizeof(buf
));
121 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid
,
127 /* Make it sure prefixlen is applied to the prefix. */
130 /* Lookup (or add) route node.*/
131 rn
= route_node_get(table
, p
);
134 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
135 rnh
->client_list
= list_new();
137 rnh
->zebra_pseudowire_list
= list_new();
145 route_unlock_node(rn
);
149 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
151 struct route_table
*table
;
152 struct route_node
*rn
;
154 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
158 /* Make it sure prefixlen is applied to the prefix. */
161 /* Lookup route node.*/
162 rn
= route_node_lookup(table
, p
);
166 route_unlock_node(rn
);
170 void zebra_free_rnh(struct rnh
*rnh
)
172 rnh
->flags
|= ZEBRA_NHT_DELETED
;
173 list_delete_and_null(&rnh
->client_list
);
174 list_delete_and_null(&rnh
->zebra_pseudowire_list
);
175 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
176 XFREE(MTYPE_RNH
, rnh
);
179 void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
181 struct route_node
*rn
;
183 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
186 if (IS_ZEBRA_DEBUG_NHT
) {
187 char buf
[PREFIX2STR_BUFFER
];
188 zlog_debug("%u: Del RNH %s type %d", rnh
->vrf_id
,
189 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
194 route_unlock_node(rn
);
198 * This code will send to the registering client
200 * For a rnh that was created, there is no data
201 * so it will send an empty nexthop group
202 * If rnh exists then we know it has been evaluated
203 * and as such it will have a resolved rnh.
205 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
206 rnh_type_t type
, vrf_id_t vrf_id
)
208 if (IS_ZEBRA_DEBUG_NHT
) {
209 char buf
[PREFIX2STR_BUFFER
];
210 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
211 zebra_route_string(client
->proto
),
212 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
214 if (!listnode_lookup(rnh
->client_list
, client
)) {
215 listnode_add(rnh
->client_list
, client
);
216 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 if (list_isempty(rnh
->client_list
)
231 && list_isempty(rnh
->zebra_pseudowire_list
))
232 zebra_delete_rnh(rnh
, type
);
235 /* XXX move this utility function elsewhere? */
236 static void addr2hostprefix(int af
, const union g_addr
*addr
,
237 struct prefix
*prefix
)
241 prefix
->family
= AF_INET
;
242 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
243 prefix
->u
.prefix4
= addr
->ipv4
;
246 prefix
->family
= AF_INET6
;
247 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
248 prefix
->u
.prefix6
= addr
->ipv6
;
251 memset(prefix
, 0, sizeof(*prefix
));
252 zlog_warn("%s: unknown address family %d", __func__
, af
);
257 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
263 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
264 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
, &exists
);
265 if (rnh
&& !listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
266 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
268 zebra_evaluate_rnh(vrf_id
, pw
->af
, 1, RNH_NEXTHOP_TYPE
, &nh
);
272 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
280 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
283 if (list_isempty(rnh
->client_list
)
284 && list_isempty(rnh
->zebra_pseudowire_list
))
285 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
288 /* Apply the NHT route-map for a client to the route (and nexthops)
291 static int zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
292 struct route_entry
*re
, int proto
)
294 int at_least_one
= 0;
295 int rmap_family
; /* Route map has diff AF family enum */
296 struct nexthop
*nexthop
;
299 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
302 for (nexthop
= re
->ng
.nexthop
; nexthop
;
303 nexthop
= nexthop
->next
) {
304 ret
= zebra_nht_route_map_check(rmap_family
, proto
,
305 &prn
->p
, 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(vrf_id_t vrfid
, int family
,
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
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
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
, int family
,
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
;
373 struct nexthop
*nexthop
;
375 if (re
&& (rnh
->state
== NULL
)) {
376 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
377 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
381 } else if (!re
&& (rnh
->state
!= NULL
))
384 if (compare_state(re
, rnh
->state
))
385 copy_state(rnh
, re
, nrn
);
387 if (state_changed
|| force
) {
388 if (IS_ZEBRA_DEBUG_NHT
) {
389 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
390 zlog_debug("%u:%s: Route import check %s %s\n", vrfid
,
391 bufn
, rnh
->state
? "passed" : "failed",
392 state_changed
? "(state changed)" : "");
394 /* state changed, notify clients */
395 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
396 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
402 * Notify clients registered for this nexthop about a change.
404 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid
, int family
,
405 struct route_node
*nrn
,
407 struct route_node
*prn
,
408 struct route_entry
*re
)
410 struct listnode
*node
;
411 struct zserv
*client
;
412 char bufn
[INET6_ADDRSTRLEN
];
413 char bufp
[INET6_ADDRSTRLEN
];
414 int num_resolving_nh
;
416 if (IS_ZEBRA_DEBUG_NHT
) {
417 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
419 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
420 zlog_debug("%u:%s: NH resolved over route %s", vrfid
,
423 zlog_debug("%u:%s: NH has become unresolved", vrfid
,
427 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
429 /* Apply route-map for this client to route resolving
431 * nexthop to see if it is filtered or not.
433 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
434 family
, prn
, re
, client
->proto
);
435 if (num_resolving_nh
)
436 rnh
->filtered
[client
->proto
] = 0;
438 rnh
->filtered
[client
->proto
] = 1;
440 if (IS_ZEBRA_DEBUG_NHT
)
442 "%u:%s: Notifying client %s about NH %s",
444 zebra_route_string(client
->proto
),
447 : "(filtered by route-map)");
449 rnh
->filtered
[client
->proto
] = 0;
450 if (IS_ZEBRA_DEBUG_NHT
)
452 "%u:%s: Notifying client %s about NH (unreachable)",
454 zebra_route_string(client
->proto
));
457 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
461 static void zebra_rnh_process_pbr_tables(int family
,
462 struct route_node
*nrn
,
464 struct route_node
*prn
,
465 struct route_entry
*re
)
467 struct zebra_ns_table
*znst
;
468 struct route_entry
*o_re
;
469 struct route_node
*o_rn
;
470 struct listnode
*node
;
471 struct zserv
*client
;
472 struct zebra_ns
*zns
;
475 if (family
== AF_INET6
)
479 * We are only concerned about nexthops that change for
482 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
483 if (client
->proto
== ZEBRA_ROUTE_PBR
)
490 zns
= zebra_ns_lookup(NS_DEFAULT
);
491 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
492 if (afi
!= znst
->afi
)
495 for (o_rn
= route_top(znst
->table
);
496 o_rn
; o_rn
= srcdest_route_next(o_rn
)) {
497 RNODE_FOREACH_RE (o_rn
, o_re
) {
498 if (o_re
->type
== ZEBRA_ROUTE_PBR
)
504 * If we have a PBR route and a nexthop changes
505 * just rethink it. Yes this is a hammer, but
509 SET_FLAG(o_re
->status
, ROUTE_ENTRY_CHANGED
);
517 * Determine appropriate route (route entry) resolving a tracked
520 static struct route_entry
*
521 zebra_rnh_resolve_nexthop_entry(vrf_id_t vrfid
, int family
,
522 struct route_node
*nrn
, struct rnh
*rnh
,
523 struct route_node
**prn
)
525 struct route_table
*route_table
;
526 struct route_node
*rn
;
527 struct route_entry
*re
;
531 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
535 rn
= route_node_match(route_table
, &nrn
->p
);
539 /* Unlock route node - we don't need to lock when walking the tree. */
540 route_unlock_node(rn
);
542 /* While resolving nexthops, we may need to walk up the tree from the
543 * most-specific match. Do similar logic as in zebra_rib.c
546 /* Do not resolve over default route unless allowed &&
547 * match route to be exact if so specified
549 if (is_default_prefix(&rn
->p
)
550 && !rnh_resolve_via_default(rn
->p
.family
))
553 /* Identify appropriate route entry. */
554 RNODE_FOREACH_RE (rn
, re
) {
555 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
557 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
560 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
561 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
562 || (re
->type
== ZEBRA_ROUTE_STATIC
))
564 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
565 struct nexthop
*nexthop
;
567 for (nexthop
= re
->ng
.nexthop
; nexthop
;
568 nexthop
= nexthop
->next
)
570 == NEXTHOP_TYPE_IFINDEX
)
579 /* Route entry found, we're done; else, walk up the tree. */
585 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
594 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
597 struct listnode
*node
;
599 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
604 * See if a tracked nexthop entry has undergone any change, and if so,
605 * take appropriate action; this involves notifying any clients and/or
606 * scheduling dependent static routes for processing.
608 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid
, int family
, int force
,
609 struct route_node
*nrn
,
611 struct route_node
*prn
,
612 struct route_entry
*re
)
614 int state_changed
= 0;
616 /* If we're resolving over a different route, resolution has changed or
617 * the resolving route has some change (e.g., metric), there is a state
620 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
622 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
624 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
626 copy_state(rnh
, re
, nrn
);
628 } else if (compare_state(re
, rnh
->state
)) {
629 copy_state(rnh
, re
, nrn
);
633 if (state_changed
|| force
) {
634 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
637 /* Notify registered protocol clients. */
638 zebra_rnh_notify_protocol_clients(vrfid
, family
, nrn
, rnh
, prn
,
641 zebra_rnh_process_pbr_tables(family
, nrn
, rnh
, prn
,
644 /* Process pseudowires attached to this nexthop */
645 zebra_rnh_process_pseudowires(vrfid
, rnh
);
649 /* Evaluate one tracked entry */
650 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid
, int family
, int force
,
651 rnh_type_t type
, struct route_node
*nrn
)
654 struct route_entry
*re
;
655 struct route_node
*prn
;
656 char bufn
[INET6_ADDRSTRLEN
];
658 if (IS_ZEBRA_DEBUG_NHT
) {
659 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
660 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid
, bufn
, type
,
661 force
? "(force)" : "");
666 /* Identify route entry (RE) resolving this tracked entry. */
667 if (type
== RNH_IMPORT_CHECK_TYPE
)
668 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
671 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
674 /* If the entry cannot be resolved and that is also the existing state,
675 * there is nothing further to do.
677 if (!re
&& rnh
->state
== NULL
&& !force
)
680 /* Process based on type of entry. */
681 if (type
== RNH_IMPORT_CHECK_TYPE
)
682 zebra_rnh_eval_import_check_entry(vrfid
, family
, force
, nrn
,
685 zebra_rnh_eval_nexthop_entry(vrfid
, family
, force
, nrn
, rnh
,
690 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
691 * from the re entries.
693 * Please note we are doing this *after* we have
694 * notified the world about each nexthop as that
695 * we can have a situation where one re entry
696 * covers multiple nexthops we are interested in.
698 static void zebra_rnh_clear_nhc_flag(vrf_id_t vrfid
, int family
,
699 rnh_type_t type
, struct route_node
*nrn
)
702 struct route_entry
*re
;
703 struct route_node
*prn
;
707 /* Identify route entry (RIB) resolving this tracked entry. */
708 if (type
== RNH_IMPORT_CHECK_TYPE
)
709 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
712 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
716 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
717 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
721 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
722 * of a particular VRF and address-family or a specific prefix.
724 void zebra_evaluate_rnh(vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
727 struct route_table
*rnh_table
;
728 struct route_node
*nrn
;
730 rnh_table
= get_rnh_table(vrfid
, family
, type
);
731 if (!rnh_table
) // unexpected
735 /* Evaluating a specific entry, make sure it exists. */
736 nrn
= route_node_lookup(rnh_table
, p
);
737 if (nrn
&& nrn
->info
)
738 zebra_rnh_evaluate_entry(vrfid
, family
, force
, type
,
742 route_unlock_node(nrn
);
744 /* Evaluate entire table. */
745 nrn
= route_top(rnh_table
);
748 zebra_rnh_evaluate_entry(vrfid
, family
, force
,
750 nrn
= route_next(nrn
); /* this will also unlock nrn */
752 nrn
= route_top(rnh_table
);
755 zebra_rnh_clear_nhc_flag(vrfid
, family
, type
,
757 nrn
= route_next(nrn
); /* this will also unlock nrn */
762 void zebra_print_rnh_table(vrf_id_t vrfid
, int af
, struct vty
*vty
,
765 struct route_table
*table
;
766 struct route_node
*rn
;
768 table
= get_rnh_table(vrfid
, af
, type
);
770 zlog_debug("print_rnhs: rnh table not found\n");
774 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
780 * free_state - free up the re structure associated with the rnh.
782 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
783 struct route_node
*rn
)
789 /* free RE and nexthops */
790 nexthops_free(re
->ng
.nexthop
);
794 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
795 struct route_node
*rn
)
797 struct route_entry
*state
;
800 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
807 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
808 state
->type
= re
->type
;
809 state
->distance
= re
->distance
;
810 state
->metric
= re
->metric
;
811 state
->vrf_id
= re
->vrf_id
;
813 route_entry_copy_nexthops(state
, re
->ng
.nexthop
);
817 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
823 if ((!r1
&& r2
) || (r1
&& !r2
))
826 if (r1
->distance
!= r2
->distance
)
829 if (r1
->metric
!= r2
->metric
)
832 if (r1
->nexthop_num
!= r2
->nexthop_num
)
835 if (CHECK_FLAG(r1
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
)
836 || CHECK_FLAG(r1
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
842 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
846 struct route_entry
*re
;
850 struct route_node
*rn
;
851 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
852 : ZEBRA_NEXTHOP_UPDATE
;
857 /* Get output stream. */
858 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
860 zclient_create_header(s
, cmd
, vrf_id
);
862 stream_putw(s
, rn
->p
.family
);
863 switch (rn
->p
.family
) {
865 stream_putc(s
, rn
->p
.prefixlen
);
866 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
869 stream_putc(s
, rn
->p
.prefixlen
);
870 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
873 flog_err(ZEBRA_ERR_RNH_UNKNOWN_FAMILY
,
874 "%s: Unknown family (%d) notification attempted\n",
875 __FUNCTION__
, rn
->p
.family
);
879 stream_putc(s
, re
->type
);
880 stream_putw(s
, re
->instance
);
881 stream_putc(s
, re
->distance
);
882 stream_putl(s
, re
->metric
);
884 nump
= stream_get_endp(s
);
886 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
887 if ((CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_FIB
)
888 || CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
889 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
890 stream_putc(s
, nh
->type
);
892 case NEXTHOP_TYPE_IPV4
:
893 case NEXTHOP_TYPE_IPV4_IFINDEX
:
894 stream_put_in_addr(s
, &nh
->gate
.ipv4
);
895 stream_putl(s
, nh
->ifindex
);
897 case NEXTHOP_TYPE_IFINDEX
:
898 stream_putl(s
, nh
->ifindex
);
900 case NEXTHOP_TYPE_IPV6
:
901 case NEXTHOP_TYPE_IPV6_IFINDEX
:
902 stream_put(s
, &nh
->gate
.ipv6
, 16);
903 stream_putl(s
, nh
->ifindex
);
911 nh
->nh_label
->num_labels
);
912 if (nh
->nh_label
->num_labels
)
915 &nh
->nh_label
->label
[0],
916 nh
->nh_label
->num_labels
917 * sizeof(mpls_label_t
));
922 stream_putc_at(s
, nump
, num
);
924 stream_putc(s
, 0); // type
925 stream_putw(s
, 0); // instance
926 stream_putc(s
, 0); // distance
927 stream_putl(s
, 0); // metric
928 stream_putc(s
, 0); // nexthops
930 stream_putw_at(s
, 0, stream_get_endp(s
));
932 client
->nh_last_upd_time
= monotime(NULL
);
933 client
->last_write_cmd
= cmd
;
934 return zserv_send_message(client
, s
);
937 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
940 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
942 switch (nexthop
->type
) {
943 case NEXTHOP_TYPE_IPV4
:
944 case NEXTHOP_TYPE_IPV4_IFINDEX
:
945 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
946 if (nexthop
->ifindex
)
948 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
950 case NEXTHOP_TYPE_IPV6
:
951 case NEXTHOP_TYPE_IPV6_IFINDEX
:
953 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
954 if (nexthop
->ifindex
)
955 vty_out(vty
, ", via %s",
956 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
958 case NEXTHOP_TYPE_IFINDEX
:
959 vty_out(vty
, " is directly connected, %s",
960 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
962 case NEXTHOP_TYPE_BLACKHOLE
:
963 vty_out(vty
, " is directly connected, Null0");
971 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
974 struct nexthop
*nexthop
;
975 struct listnode
*node
;
976 struct zserv
*client
;
980 vty_out(vty
, "%s%s\n",
981 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
982 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
985 vty_out(vty
, " resolved via %s\n",
986 zebra_route_string(rnh
->state
->type
));
987 for (nexthop
= rnh
->state
->ng
.nexthop
; nexthop
;
988 nexthop
= nexthop
->next
)
989 print_nh(nexthop
, vty
);
991 vty_out(vty
, " unresolved%s\n",
992 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
996 vty_out(vty
, " Client list:");
997 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
998 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1000 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1001 if (!list_isempty(rnh
->zebra_pseudowire_list
))
1002 vty_out(vty
, " zebra[pseudowires]");
1006 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, int family
,
1007 struct zserv
*client
, rnh_type_t type
)
1009 struct route_table
*ntable
;
1010 struct route_node
*nrn
;
1013 if (IS_ZEBRA_DEBUG_NHT
)
1014 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
1015 vrf_id
, zebra_route_string(client
->proto
), family
,
1018 ntable
= get_rnh_table(vrf_id
, family
, type
);
1020 zlog_debug("cleanup_rnh_client: rnh table not found\n");
1024 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1029 zebra_remove_rnh_client(rnh
, client
, type
);
1034 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1035 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1038 struct zebra_vrf
*zvrf
;
1040 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1043 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1045 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
1046 client
, RNH_NEXTHOP_TYPE
);
1047 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1048 RNH_IMPORT_CHECK_TYPE
);
1049 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
1050 client
, RNH_IMPORT_CHECK_TYPE
);
1051 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
1052 hash_iterate(zvrf
->lsp_table
,
1053 mpls_ldp_lsp_uninstall_all
,
1055 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
1056 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);