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
)
108 struct route_table
*table
;
109 struct route_node
*rn
;
110 struct rnh
*rnh
= NULL
;
111 char buf
[PREFIX2STR_BUFFER
];
113 if (IS_ZEBRA_DEBUG_NHT
) {
114 prefix2str(p
, buf
, sizeof(buf
));
115 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
117 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
119 prefix2str(p
, buf
, sizeof(buf
));
120 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid
,
125 /* Make it sure prefixlen is applied to the prefix. */
128 /* Lookup (or add) route node.*/
129 rn
= route_node_get(table
, p
);
132 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
133 rnh
->client_list
= list_new();
135 rnh
->zebra_pseudowire_list
= list_new();
141 route_unlock_node(rn
);
145 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
147 struct route_table
*table
;
148 struct route_node
*rn
;
150 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
154 /* Make it sure prefixlen is applied to the prefix. */
157 /* Lookup route node.*/
158 rn
= route_node_lookup(table
, p
);
162 route_unlock_node(rn
);
166 void zebra_free_rnh(struct rnh
*rnh
)
168 rnh
->flags
|= ZEBRA_NHT_DELETED
;
169 list_delete_and_null(&rnh
->client_list
);
170 list_delete_and_null(&rnh
->zebra_pseudowire_list
);
171 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
172 XFREE(MTYPE_RNH
, rnh
);
175 void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
177 struct route_node
*rn
;
179 if (!rnh
|| (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
);
193 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
194 rnh_type_t type
, vrf_id_t vrf_id
)
196 if (IS_ZEBRA_DEBUG_NHT
) {
197 char buf
[PREFIX2STR_BUFFER
];
198 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
199 zebra_route_string(client
->proto
),
200 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
202 if (!listnode_lookup(rnh
->client_list
, client
)) {
203 listnode_add(rnh
->client_list
, client
);
204 send_client(rnh
, client
, type
,
205 vrf_id
); // Pending: check if its needed
209 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
212 if (IS_ZEBRA_DEBUG_NHT
) {
213 char buf
[PREFIX2STR_BUFFER
];
214 zlog_debug("Client %s unregisters for RNH %s type %d",
215 zebra_route_string(client
->proto
),
216 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
218 listnode_delete(rnh
->client_list
, client
);
219 if (list_isempty(rnh
->client_list
)
220 && list_isempty(rnh
->zebra_pseudowire_list
))
221 zebra_delete_rnh(rnh
, type
);
224 /* XXX move this utility function elsewhere? */
225 static void addr2hostprefix(int af
, const union g_addr
*addr
,
226 struct prefix
*prefix
)
230 prefix
->family
= AF_INET
;
231 prefix
->prefixlen
= IPV4_MAX_BITLEN
;
232 prefix
->u
.prefix4
= addr
->ipv4
;
235 prefix
->family
= AF_INET6
;
236 prefix
->prefixlen
= IPV6_MAX_BITLEN
;
237 prefix
->u
.prefix6
= addr
->ipv6
;
240 memset(prefix
, 0, sizeof(*prefix
));
241 zlog_warn("%s: unknown address family %d", __func__
, af
);
246 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
251 addr2hostprefix(pw
->af
, &pw
->nexthop
, &nh
);
252 rnh
= zebra_add_rnh(&nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
253 if (rnh
&& !listnode_lookup(rnh
->zebra_pseudowire_list
, pw
)) {
254 listnode_add(rnh
->zebra_pseudowire_list
, pw
);
256 zebra_evaluate_rnh(vrf_id
, pw
->af
, 1, RNH_NEXTHOP_TYPE
, &nh
);
260 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id
, struct zebra_pw
*pw
)
268 listnode_delete(rnh
->zebra_pseudowire_list
, pw
);
271 if (list_isempty(rnh
->client_list
)
272 && list_isempty(rnh
->zebra_pseudowire_list
))
273 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
276 /* Apply the NHT route-map for a client to the route (and nexthops)
279 static int zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
280 struct route_entry
*re
, int proto
)
282 int at_least_one
= 0;
283 int rmap_family
; /* Route map has diff AF family enum */
284 struct nexthop
*nexthop
;
287 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
290 for (nexthop
= re
->ng
.nexthop
; nexthop
;
291 nexthop
= nexthop
->next
) {
292 ret
= zebra_nht_route_map_check(rmap_family
, proto
,
293 &prn
->p
, re
, nexthop
);
294 if (ret
!= RMAP_DENYMATCH
) {
295 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
296 at_least_one
++; /* at least one valid NH */
298 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
302 return (at_least_one
);
306 * Determine appropriate route (RE entry) resolving a tracked BGP route
307 * for BGP route for import.
309 static struct route_entry
*
310 zebra_rnh_resolve_import_entry(vrf_id_t vrfid
, int family
,
311 struct route_node
*nrn
, struct rnh
*rnh
,
312 struct route_node
**prn
)
314 struct route_table
*route_table
;
315 struct route_node
*rn
;
316 struct route_entry
*re
;
320 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
321 if (!route_table
) // unexpected
324 rn
= route_node_match(route_table
, &nrn
->p
);
328 /* Unlock route node - we don't need to lock when walking the tree. */
329 route_unlock_node(rn
);
331 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
332 && !prefix_same(&nrn
->p
, &rn
->p
))
335 /* Identify appropriate route entry. */
336 RNODE_FOREACH_RE (rn
, re
) {
337 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
338 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
339 && (re
->type
!= ZEBRA_ROUTE_BGP
))
349 * See if a tracked route entry for import (by BGP) has undergone any
350 * change, and if so, notify the client.
352 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid
, int family
,
353 int force
, struct route_node
*nrn
,
355 struct route_entry
*re
)
357 int state_changed
= 0;
358 struct zserv
*client
;
359 char bufn
[INET6_ADDRSTRLEN
];
360 struct listnode
*node
;
361 struct nexthop
*nexthop
;
363 if (re
&& (rnh
->state
== NULL
)) {
364 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
365 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
369 } else if (!re
&& (rnh
->state
!= NULL
))
372 if (compare_state(re
, rnh
->state
))
373 copy_state(rnh
, re
, nrn
);
375 if (state_changed
|| force
) {
376 if (IS_ZEBRA_DEBUG_NHT
) {
377 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
378 zlog_debug("%u:%s: Route import check %s %s\n", vrfid
,
379 bufn
, rnh
->state
? "passed" : "failed",
380 state_changed
? "(state changed)" : "");
382 /* state changed, notify clients */
383 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
384 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
390 * Notify clients registered for this nexthop about a change.
392 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid
, int family
,
393 struct route_node
*nrn
,
395 struct route_node
*prn
,
396 struct route_entry
*re
)
398 struct listnode
*node
;
399 struct zserv
*client
;
400 char bufn
[INET6_ADDRSTRLEN
];
401 char bufp
[INET6_ADDRSTRLEN
];
402 int num_resolving_nh
;
404 if (IS_ZEBRA_DEBUG_NHT
) {
405 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
407 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
408 zlog_debug("%u:%s: NH resolved over route %s", vrfid
,
411 zlog_debug("%u:%s: NH has become unresolved", vrfid
,
415 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
417 /* Apply route-map for this client to route resolving
419 * nexthop to see if it is filtered or not.
421 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
422 family
, prn
, re
, client
->proto
);
423 if (num_resolving_nh
)
424 rnh
->filtered
[client
->proto
] = 0;
426 rnh
->filtered
[client
->proto
] = 1;
428 if (IS_ZEBRA_DEBUG_NHT
)
430 "%u:%s: Notifying client %s about NH %s",
432 zebra_route_string(client
->proto
),
435 : "(filtered by route-map)");
437 rnh
->filtered
[client
->proto
] = 0;
438 if (IS_ZEBRA_DEBUG_NHT
)
440 "%u:%s: Notifying client %s about NH (unreachable)",
442 zebra_route_string(client
->proto
));
445 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
449 static void zebra_rnh_process_pbr_tables(int family
,
450 struct route_node
*nrn
,
452 struct route_node
*prn
,
453 struct route_entry
*re
)
455 struct zebra_ns_table
*znst
;
456 struct route_entry
*o_re
;
457 struct route_node
*o_rn
;
458 struct listnode
*node
;
459 struct zserv
*client
;
460 struct zebra_ns
*zns
;
463 if (family
== AF_INET6
)
467 * We are only concerned about nexthops that change for
470 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
471 if (client
->proto
== ZEBRA_ROUTE_PBR
)
478 zns
= zebra_ns_lookup(NS_DEFAULT
);
479 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
480 if (afi
!= znst
->afi
)
483 for (o_rn
= route_top(znst
->table
);
484 o_rn
; o_rn
= srcdest_route_next(o_rn
)) {
485 RNODE_FOREACH_RE (o_rn
, o_re
) {
486 if (o_re
->type
== ZEBRA_ROUTE_PBR
)
492 * If we have a PBR route and a nexthop changes
493 * just rethink it. Yes this is a hammer, but
497 SET_FLAG(o_re
->status
, ROUTE_ENTRY_CHANGED
);
505 * Determine appropriate route (route entry) resolving a tracked
508 static struct route_entry
*
509 zebra_rnh_resolve_nexthop_entry(vrf_id_t vrfid
, int family
,
510 struct route_node
*nrn
, struct rnh
*rnh
,
511 struct route_node
**prn
)
513 struct route_table
*route_table
;
514 struct route_node
*rn
;
515 struct route_entry
*re
;
519 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
523 rn
= route_node_match(route_table
, &nrn
->p
);
527 /* Unlock route node - we don't need to lock when walking the tree. */
528 route_unlock_node(rn
);
530 /* While resolving nexthops, we may need to walk up the tree from the
531 * most-specific match. Do similar logic as in zebra_rib.c
534 /* Do not resolve over default route unless allowed &&
535 * match route to be exact if so specified
537 if (is_default_prefix(&rn
->p
)
538 && !rnh_resolve_via_default(rn
->p
.family
))
541 /* Identify appropriate route entry. */
542 RNODE_FOREACH_RE (rn
, re
) {
543 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
545 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
548 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
549 if ((re
->type
== ZEBRA_ROUTE_CONNECT
)
550 || (re
->type
== ZEBRA_ROUTE_STATIC
))
552 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
553 struct nexthop
*nexthop
;
555 for (nexthop
= re
->ng
.nexthop
; nexthop
;
556 nexthop
= nexthop
->next
)
558 == NEXTHOP_TYPE_IFINDEX
)
567 /* Route entry found, we're done; else, walk up the tree. */
573 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
582 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid
, struct rnh
*rnh
)
585 struct listnode
*node
;
587 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_pseudowire_list
, node
, pw
))
592 * See if a tracked nexthop entry has undergone any change, and if so,
593 * take appropriate action; this involves notifying any clients and/or
594 * scheduling dependent static routes for processing.
596 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid
, int family
, int force
,
597 struct route_node
*nrn
,
599 struct route_node
*prn
,
600 struct route_entry
*re
)
602 int state_changed
= 0;
604 /* If we're resolving over a different route, resolution has changed or
605 * the resolving route has some change (e.g., metric), there is a state
608 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
610 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
612 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
614 copy_state(rnh
, re
, nrn
);
616 } else if (compare_state(re
, rnh
->state
)) {
617 copy_state(rnh
, re
, nrn
);
621 if (state_changed
|| force
) {
622 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
625 /* Notify registered protocol clients. */
626 zebra_rnh_notify_protocol_clients(vrfid
, family
, nrn
, rnh
, prn
,
629 zebra_rnh_process_pbr_tables(family
, nrn
, rnh
, prn
,
632 /* Process pseudowires attached to this nexthop */
633 zebra_rnh_process_pseudowires(vrfid
, rnh
);
637 /* Evaluate one tracked entry */
638 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid
, int family
, int force
,
639 rnh_type_t type
, struct route_node
*nrn
)
642 struct route_entry
*re
;
643 struct route_node
*prn
;
644 char bufn
[INET6_ADDRSTRLEN
];
646 if (IS_ZEBRA_DEBUG_NHT
) {
647 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
648 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid
, bufn
, type
,
649 force
? "(force)" : "");
654 /* Identify route entry (RE) resolving this tracked entry. */
655 if (type
== RNH_IMPORT_CHECK_TYPE
)
656 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
659 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
662 /* If the entry cannot be resolved and that is also the existing state,
663 * there is nothing further to do.
665 if (!re
&& rnh
->state
== NULL
&& !force
)
668 /* Process based on type of entry. */
669 if (type
== RNH_IMPORT_CHECK_TYPE
)
670 zebra_rnh_eval_import_check_entry(vrfid
, family
, force
, nrn
,
673 zebra_rnh_eval_nexthop_entry(vrfid
, family
, force
, nrn
, rnh
,
678 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
679 * from the re entries.
681 * Please note we are doing this *after* we have
682 * notified the world about each nexthop as that
683 * we can have a situation where one re entry
684 * covers multiple nexthops we are interested in.
686 static void zebra_rnh_clear_nhc_flag(vrf_id_t vrfid
, int family
,
687 rnh_type_t type
, struct route_node
*nrn
)
690 struct route_entry
*re
;
691 struct route_node
*prn
;
695 /* Identify route entry (RIB) resolving this tracked entry. */
696 if (type
== RNH_IMPORT_CHECK_TYPE
)
697 re
= zebra_rnh_resolve_import_entry(vrfid
, family
, nrn
, rnh
,
700 re
= zebra_rnh_resolve_nexthop_entry(vrfid
, family
, nrn
, rnh
,
704 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
705 UNSET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
709 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
710 * of a particular VRF and address-family or a specific prefix.
712 void zebra_evaluate_rnh(vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
715 struct route_table
*rnh_table
;
716 struct route_node
*nrn
;
718 rnh_table
= get_rnh_table(vrfid
, family
, type
);
719 if (!rnh_table
) // unexpected
723 /* Evaluating a specific entry, make sure it exists. */
724 nrn
= route_node_lookup(rnh_table
, p
);
725 if (nrn
&& nrn
->info
)
726 zebra_rnh_evaluate_entry(vrfid
, family
, force
, type
,
730 route_unlock_node(nrn
);
732 /* Evaluate entire table. */
733 nrn
= route_top(rnh_table
);
736 zebra_rnh_evaluate_entry(vrfid
, family
, force
,
738 nrn
= route_next(nrn
); /* this will also unlock nrn */
740 nrn
= route_top(rnh_table
);
743 zebra_rnh_clear_nhc_flag(vrfid
, family
, type
,
745 nrn
= route_next(nrn
); /* this will also unlock nrn */
750 void zebra_print_rnh_table(vrf_id_t vrfid
, int af
, struct vty
*vty
,
753 struct route_table
*table
;
754 struct route_node
*rn
;
756 table
= get_rnh_table(vrfid
, af
, type
);
758 zlog_debug("print_rnhs: rnh table not found\n");
762 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
768 * free_state - free up the re structure associated with the rnh.
770 static void free_state(vrf_id_t vrf_id
, struct route_entry
*re
,
771 struct route_node
*rn
)
777 /* free RE and nexthops */
778 nexthops_free(re
->ng
.nexthop
);
782 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
783 struct route_node
*rn
)
785 struct route_entry
*state
;
788 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
795 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
796 state
->type
= re
->type
;
797 state
->distance
= re
->distance
;
798 state
->metric
= re
->metric
;
799 state
->vrf_id
= re
->vrf_id
;
801 route_entry_copy_nexthops(state
, re
->ng
.nexthop
);
805 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
811 if ((!r1
&& r2
) || (r1
&& !r2
))
814 if (r1
->distance
!= r2
->distance
)
817 if (r1
->metric
!= r2
->metric
)
820 if (r1
->nexthop_num
!= r2
->nexthop_num
)
823 if (CHECK_FLAG(r1
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
)
824 || CHECK_FLAG(r1
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
830 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
834 struct route_entry
*re
;
838 struct route_node
*rn
;
839 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
840 : ZEBRA_NEXTHOP_UPDATE
;
845 /* Get output stream. */
846 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
848 zclient_create_header(s
, cmd
, vrf_id
);
850 stream_putw(s
, rn
->p
.family
);
851 switch (rn
->p
.family
) {
853 stream_putc(s
, rn
->p
.prefixlen
);
854 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
857 stream_putc(s
, rn
->p
.prefixlen
);
858 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
861 flog_err(ZEBRA_ERR_RNH_UNKNOWN_FAMILY
,
862 "%s: Unknown family (%d) notification attempted\n",
863 __FUNCTION__
, rn
->p
.family
);
867 stream_putc(s
, re
->type
);
868 stream_putw(s
, re
->instance
);
869 stream_putc(s
, re
->distance
);
870 stream_putl(s
, re
->metric
);
872 nump
= stream_get_endp(s
);
874 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
875 if ((CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_FIB
)
876 || CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
877 && CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
878 stream_putc(s
, nh
->type
);
880 case NEXTHOP_TYPE_IPV4
:
881 case NEXTHOP_TYPE_IPV4_IFINDEX
:
882 stream_put_in_addr(s
, &nh
->gate
.ipv4
);
883 stream_putl(s
, nh
->ifindex
);
885 case NEXTHOP_TYPE_IFINDEX
:
886 stream_putl(s
, nh
->ifindex
);
888 case NEXTHOP_TYPE_IPV6
:
889 case NEXTHOP_TYPE_IPV6_IFINDEX
:
890 stream_put(s
, &nh
->gate
.ipv6
, 16);
891 stream_putl(s
, nh
->ifindex
);
899 nh
->nh_label
->num_labels
);
900 if (nh
->nh_label
->num_labels
)
903 &nh
->nh_label
->label
[0],
904 nh
->nh_label
->num_labels
905 * sizeof(mpls_label_t
));
910 stream_putc_at(s
, nump
, num
);
912 stream_putc(s
, 0); // type
913 stream_putw(s
, 0); // instance
914 stream_putc(s
, 0); // distance
915 stream_putl(s
, 0); // metric
916 stream_putc(s
, 0); // nexthops
918 stream_putw_at(s
, 0, stream_get_endp(s
));
920 client
->nh_last_upd_time
= monotime(NULL
);
921 client
->last_write_cmd
= cmd
;
922 return zserv_send_message(client
, s
);
925 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
928 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
930 switch (nexthop
->type
) {
931 case NEXTHOP_TYPE_IPV4
:
932 case NEXTHOP_TYPE_IPV4_IFINDEX
:
933 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
934 if (nexthop
->ifindex
)
936 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
938 case NEXTHOP_TYPE_IPV6
:
939 case NEXTHOP_TYPE_IPV6_IFINDEX
:
941 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
942 if (nexthop
->ifindex
)
943 vty_out(vty
, ", via %s",
944 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
946 case NEXTHOP_TYPE_IFINDEX
:
947 vty_out(vty
, " is directly connected, %s",
948 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
950 case NEXTHOP_TYPE_BLACKHOLE
:
951 vty_out(vty
, " is directly connected, Null0");
959 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
962 struct nexthop
*nexthop
;
963 struct listnode
*node
;
964 struct zserv
*client
;
968 vty_out(vty
, "%s%s\n",
969 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
970 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
973 vty_out(vty
, " resolved via %s\n",
974 zebra_route_string(rnh
->state
->type
));
975 for (nexthop
= rnh
->state
->ng
.nexthop
; nexthop
;
976 nexthop
= nexthop
->next
)
977 print_nh(nexthop
, vty
);
979 vty_out(vty
, " unresolved%s\n",
980 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
984 vty_out(vty
, " Client list:");
985 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
986 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
988 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
989 if (!list_isempty(rnh
->zebra_pseudowire_list
))
990 vty_out(vty
, " zebra[pseudowires]");
994 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, int family
,
995 struct zserv
*client
, rnh_type_t type
)
997 struct route_table
*ntable
;
998 struct route_node
*nrn
;
1001 if (IS_ZEBRA_DEBUG_NHT
)
1002 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
1003 vrf_id
, zebra_route_string(client
->proto
), family
,
1006 ntable
= get_rnh_table(vrf_id
, family
, type
);
1008 zlog_debug("cleanup_rnh_client: rnh table not found\n");
1012 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
1017 zebra_remove_rnh_client(rnh
, client
, type
);
1022 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1023 static int zebra_client_cleanup_rnh(struct zserv
*client
)
1026 struct zebra_vrf
*zvrf
;
1028 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1031 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1033 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
1034 client
, RNH_NEXTHOP_TYPE
);
1035 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET
, client
,
1036 RNH_IMPORT_CHECK_TYPE
);
1037 zebra_cleanup_rnh_client(zvrf_id(zvrf
), AF_INET6
,
1038 client
, RNH_IMPORT_CHECK_TYPE
);
1039 if (client
->proto
== ZEBRA_ROUTE_LDP
) {
1040 hash_iterate(zvrf
->lsp_table
,
1041 mpls_ldp_lsp_uninstall_all
,
1043 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP
);
1044 mpls_ldp_ftn_uninstall_all(zvrf
, AFI_IP6
);