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
);
70 int zebra_rnh_ip_default_route
= 0;
71 int zebra_rnh_ipv6_default_route
= 0;
73 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, int family
,
76 struct zebra_vrf
*zvrf
;
77 struct route_table
*t
= NULL
;
79 zvrf
= zebra_vrf_lookup_by_id(vrfid
);
82 case RNH_NEXTHOP_TYPE
:
83 t
= zvrf
->rnh_table
[family2afi(family
)];
85 case RNH_IMPORT_CHECK_TYPE
:
86 t
= zvrf
->import_check_table
[family2afi(family
)];
93 char *rnh_str(struct rnh
*rnh
, char *buf
, int size
)
95 prefix2str(&(rnh
->node
->p
), buf
, size
);
99 struct rnh
*zebra_add_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
101 struct route_table
*table
;
102 struct route_node
*rn
;
103 struct rnh
*rnh
= NULL
;
104 char buf
[PREFIX2STR_BUFFER
];
106 if (IS_ZEBRA_DEBUG_NHT
) {
107 prefix2str(p
, buf
, sizeof(buf
));
108 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
110 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
112 prefix2str(p
, buf
, sizeof(buf
));
113 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid
,
118 /* Make it sure prefixlen is applied to the prefix. */
121 /* Lookup (or add) route node.*/
122 rn
= route_node_get(table
, p
);
125 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
126 rnh
->client_list
= list_new();
128 rnh
->zebra_static_route_list
= list_new();
134 route_unlock_node(rn
);
138 struct rnh
*zebra_lookup_rnh(struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
140 struct route_table
*table
;
141 struct route_node
*rn
;
143 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
147 /* Make it sure prefixlen is applied to the prefix. */
150 /* Lookup route node.*/
151 rn
= route_node_lookup(table
, p
);
155 route_unlock_node(rn
);
159 void zebra_free_rnh(struct rnh
*rnh
)
161 rnh
->flags
|= ZEBRA_NHT_DELETED
;
162 list_free(rnh
->client_list
);
163 list_free(rnh
->zebra_static_route_list
);
164 free_state(rnh
->vrf_id
, rnh
->state
, rnh
->node
);
165 XFREE(MTYPE_RNH
, rnh
);
168 void zebra_delete_rnh(struct rnh
*rnh
, rnh_type_t type
)
170 struct route_node
*rn
;
172 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
175 if (IS_ZEBRA_DEBUG_NHT
) {
176 char buf
[PREFIX2STR_BUFFER
];
177 zlog_debug("%u: Del RNH %s type %d", rnh
->vrf_id
,
178 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
183 route_unlock_node(rn
);
186 void zebra_add_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
187 rnh_type_t type
, vrf_id_t vrf_id
)
189 if (IS_ZEBRA_DEBUG_NHT
) {
190 char buf
[PREFIX2STR_BUFFER
];
191 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id
,
192 zebra_route_string(client
->proto
),
193 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
195 if (!listnode_lookup(rnh
->client_list
, client
)) {
196 listnode_add(rnh
->client_list
, client
);
197 send_client(rnh
, client
, type
,
198 vrf_id
); // Pending: check if its needed
202 void zebra_remove_rnh_client(struct rnh
*rnh
, struct zserv
*client
,
205 if (IS_ZEBRA_DEBUG_NHT
) {
206 char buf
[PREFIX2STR_BUFFER
];
207 zlog_debug("Client %s unregisters for RNH %s type %d",
208 zebra_route_string(client
->proto
),
209 rnh_str(rnh
, buf
, sizeof(buf
)), type
);
211 listnode_delete(rnh
->client_list
, client
);
212 if (list_isempty(rnh
->client_list
)
213 && list_isempty(rnh
->zebra_static_route_list
))
214 zebra_delete_rnh(rnh
, type
);
217 void zebra_register_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
218 struct route_node
*static_rn
)
222 rnh
= zebra_add_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
223 if (rnh
&& !listnode_lookup(rnh
->zebra_static_route_list
, static_rn
)) {
224 listnode_add(rnh
->zebra_static_route_list
, static_rn
);
228 void zebra_deregister_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
229 struct route_node
*static_rn
)
233 rnh
= zebra_lookup_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
234 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
))
237 listnode_delete(rnh
->zebra_static_route_list
, static_rn
);
239 if (list_isempty(rnh
->client_list
)
240 && list_isempty(rnh
->zebra_static_route_list
))
241 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
244 void zebra_deregister_rnh_static_nexthops(vrf_id_t vrf_id
,
245 struct nexthop
*nexthop
,
246 struct route_node
*rn
)
251 for (nh
= nexthop
; nh
; nh
= nh
->next
) {
253 case NEXTHOP_TYPE_IPV4
:
254 case NEXTHOP_TYPE_IPV4_IFINDEX
:
255 nh_p
.family
= AF_INET
;
256 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
257 nh_p
.u
.prefix4
= nh
->gate
.ipv4
;
259 case NEXTHOP_TYPE_IPV6
:
260 case NEXTHOP_TYPE_IPV6_IFINDEX
:
261 nh_p
.family
= AF_INET6
;
262 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
263 nh_p
.u
.prefix6
= nh
->gate
.ipv6
;
266 * Not sure what really to do here, we are not
267 * supposed to have either of these for NHT
268 * and the code has no way to know what prefix
269 * to use. So I'm going to just continue
270 * for the moment, which is preferable to
271 * what is currently happening which is a
273 * Some simple testing shows that we
274 * are not leaving slag around for these
275 * skipped static routes. Since
276 * they don't appear to be installed
278 case NEXTHOP_TYPE_IFINDEX
:
279 case NEXTHOP_TYPE_BLACKHOLE
:
283 zebra_deregister_rnh_static_nh(vrf_id
, &nh_p
, rn
);
287 /* Apply the NHT route-map for a client to the route (and nexthops)
290 static int zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
291 struct route_entry
*re
, int proto
)
293 int at_least_one
= 0;
294 int rmap_family
; /* Route map has diff AF family enum */
295 struct nexthop
*nexthop
;
298 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
301 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
302 ret
= zebra_nht_route_map_check(rmap_family
, proto
,
303 &prn
->p
, re
, nexthop
);
304 if (ret
!= RMAP_DENYMATCH
) {
305 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
306 at_least_one
++; /* at least one valid NH */
308 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
312 return (at_least_one
);
316 * Determine appropriate route (RE entry) resolving a tracked entry
317 * (nexthop or BGP route for import).
319 static struct route_entry
*zebra_rnh_resolve_entry(vrf_id_t vrfid
, int family
,
321 struct route_node
*nrn
,
323 struct route_node
**prn
)
325 struct route_table
*route_table
;
326 struct route_node
*rn
;
327 struct route_entry
*re
;
331 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
332 if (!route_table
) // unexpected
335 rn
= route_node_match(route_table
, &nrn
->p
);
339 /* When resolving nexthops, do not resolve via the default route unless
340 * 'ip nht resolve-via-default' is configured.
342 if ((type
== RNH_NEXTHOP_TYPE
)
343 && (is_default_prefix(&rn
->p
)
344 && !nh_resolve_via_default(rn
->p
.family
)))
346 else if ((type
== RNH_IMPORT_CHECK_TYPE
)
347 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
)
348 && !prefix_same(&nrn
->p
, &rn
->p
))
351 /* Identify appropriate route entry. */
352 RNODE_FOREACH_RE(rn
, re
)
354 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
356 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
359 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)) {
360 if (re
->type
== ZEBRA_ROUTE_CONNECT
)
362 if (re
->type
== ZEBRA_ROUTE_NHRP
) {
363 struct nexthop
*nexthop
;
364 for (nexthop
= re
->nexthop
; nexthop
;
365 nexthop
= nexthop
->next
)
367 == NEXTHOP_TYPE_IFINDEX
)
372 } else if ((type
== RNH_IMPORT_CHECK_TYPE
)
373 && (re
->type
== ZEBRA_ROUTE_BGP
))
380 /* Need to unlock route node */
381 route_unlock_node(rn
);
388 * See if a tracked route entry for import (by BGP) has undergone any
389 * change, and if so, notify the client.
391 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid
, int family
,
392 int force
, struct route_node
*nrn
,
394 struct route_entry
*re
)
396 int state_changed
= 0;
397 struct zserv
*client
;
398 char bufn
[INET6_ADDRSTRLEN
];
399 struct listnode
*node
;
400 struct nexthop
*nexthop
;
402 if (re
&& (rnh
->state
== NULL
)) {
403 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
404 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
408 } else if (!re
&& (rnh
->state
!= NULL
))
411 if (compare_state(re
, rnh
->state
))
412 copy_state(rnh
, re
, nrn
);
414 if (state_changed
|| force
) {
415 if (IS_ZEBRA_DEBUG_NHT
) {
416 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
417 zlog_debug("%u:%s: Route import check %s %s\n", vrfid
,
418 bufn
, rnh
->state
? "passed" : "failed",
419 state_changed
? "(state changed)" : "");
421 /* state changed, notify clients */
422 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
423 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
429 * Notify clients registered for this nexthop about a change.
431 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid
, int family
,
432 struct route_node
*nrn
,
434 struct route_node
*prn
,
435 struct route_entry
*re
)
437 struct listnode
*node
;
438 struct zserv
*client
;
439 char bufn
[INET6_ADDRSTRLEN
];
440 char bufp
[INET6_ADDRSTRLEN
];
441 int num_resolving_nh
;
443 if (IS_ZEBRA_DEBUG_NHT
) {
444 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
446 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
447 zlog_debug("%u:%s: NH resolved over route %s", vrfid
,
450 zlog_debug("%u:%s: NH has become unresolved", vrfid
,
454 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
)) {
456 /* Apply route-map for this client to route resolving
458 * nexthop to see if it is filtered or not.
460 num_resolving_nh
= zebra_rnh_apply_nht_rmap(
461 family
, prn
, re
, client
->proto
);
462 if (num_resolving_nh
)
463 rnh
->filtered
[client
->proto
] = 0;
465 rnh
->filtered
[client
->proto
] = 1;
467 if (IS_ZEBRA_DEBUG_NHT
)
469 "%u:%s: Notifying client %s about NH %s",
471 zebra_route_string(client
->proto
),
474 : "(filtered by route-map)");
476 rnh
->filtered
[client
->proto
] = 0;
477 if (IS_ZEBRA_DEBUG_NHT
)
479 "%u:%s: Notifying client %s about NH (unreachable)",
481 zebra_route_string(client
->proto
));
484 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
488 static void zebra_rnh_process_static_routes(vrf_id_t vrfid
, int family
,
489 struct route_node
*nrn
,
491 struct route_node
*prn
,
492 struct route_entry
*re
)
494 struct listnode
*node
;
495 int num_resolving_nh
= 0;
496 struct route_node
*static_rn
;
497 struct route_entry
*sre
;
498 struct nexthop
*nexthop
;
499 char bufn
[INET6_ADDRSTRLEN
];
500 char bufp
[INET6_ADDRSTRLEN
];
501 char bufs
[INET6_ADDRSTRLEN
];
503 if (IS_ZEBRA_DEBUG_NHT
) {
504 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
506 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
510 /* Apply route-map for "static" to route resolving this
511 * nexthop to see if it is filtered or not.
513 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, re
,
515 if (num_resolving_nh
)
516 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
518 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 1;
520 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
522 /* Evaluate each static route associated with this nexthop. */
523 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_static_route_list
, node
,
525 RNODE_FOREACH_RE(static_rn
, sre
)
527 if (sre
->type
!= ZEBRA_ROUTE_STATIC
)
530 /* Set the filter flag for the correct nexthop - static
532 * be having multiple. We care here only about
533 * registered nexthops.
535 for (nexthop
= sre
->nexthop
; nexthop
;
536 nexthop
= nexthop
->next
) {
537 switch (nexthop
->type
) {
538 case NEXTHOP_TYPE_IPV4
:
539 case NEXTHOP_TYPE_IPV4_IFINDEX
:
540 if (nexthop
->gate
.ipv4
.s_addr
541 == nrn
->p
.u
.prefix4
.s_addr
) {
542 if (num_resolving_nh
)
545 NEXTHOP_FLAG_FILTERED
);
549 NEXTHOP_FLAG_FILTERED
);
552 case NEXTHOP_TYPE_IPV6
:
553 case NEXTHOP_TYPE_IPV6_IFINDEX
:
555 if (memcmp(&nexthop
->gate
.ipv6
,
556 &nrn
->p
.u
.prefix6
, 16)
558 if (num_resolving_nh
)
561 NEXTHOP_FLAG_FILTERED
);
565 NEXTHOP_FLAG_FILTERED
);
573 if (IS_ZEBRA_DEBUG_NHT
) {
574 prefix2str(&static_rn
->p
, bufs
,
578 "%u:%s: NH change %s, scheduling static route %s",
582 : "(filtered by route-map)",
586 "%u:%s: NH unreachable, scheduling static route %s",
590 SET_FLAG(sre
->status
, ROUTE_ENTRY_CHANGED
);
591 SET_FLAG(sre
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
594 rib_queue_add(static_rn
);
599 * See if a tracked nexthop entry has undergone any change, and if so,
600 * take appropriate action; this involves notifying any clients and/or
601 * scheduling dependent static routes for processing.
603 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid
, int family
, int force
,
604 struct route_node
*nrn
,
606 struct route_node
*prn
,
607 struct route_entry
*re
)
609 int state_changed
= 0;
611 /* If we're resolving over a different route, resolution has changed or
612 * the resolving route has some change (e.g., metric), there is a state
615 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
)) {
617 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
619 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
621 copy_state(rnh
, re
, nrn
);
623 } else if (compare_state(re
, rnh
->state
)) {
624 copy_state(rnh
, re
, nrn
);
628 if (state_changed
|| force
) {
629 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
632 /* Notify registered protocol clients. */
633 zebra_rnh_notify_protocol_clients(vrfid
, family
, nrn
, rnh
, prn
,
636 /* Process static routes attached to this nexthop */
637 zebra_rnh_process_static_routes(vrfid
, family
, nrn
, rnh
, prn
,
642 /* Evaluate one tracked entry */
643 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid
, int family
, int force
,
644 rnh_type_t type
, struct route_node
*nrn
)
647 struct route_entry
*re
;
648 struct route_node
*prn
;
649 char bufn
[INET6_ADDRSTRLEN
];
651 if (IS_ZEBRA_DEBUG_NHT
) {
652 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
653 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid
, bufn
, type
,
654 force
? "(force)" : "");
659 /* Identify route entry (RE) resolving this tracked entry. */
660 re
= zebra_rnh_resolve_entry(vrfid
, family
, type
, nrn
, rnh
, &prn
);
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 re
= zebra_rnh_resolve_entry(vrfid
, family
, type
, nrn
, rnh
, &prn
);
698 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
701 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
702 * of a particular VRF and address-family or a specific prefix.
704 void zebra_evaluate_rnh(vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
707 struct route_table
*rnh_table
;
708 struct route_node
*nrn
;
710 rnh_table
= get_rnh_table(vrfid
, family
, type
);
711 if (!rnh_table
) // unexpected
715 /* Evaluating a specific entry, make sure it exists. */
716 nrn
= route_node_lookup(rnh_table
, p
);
717 if (nrn
&& nrn
->info
)
718 zebra_rnh_evaluate_entry(vrfid
, family
, force
, type
,
722 route_unlock_node(nrn
);
724 /* Evaluate entire table. */
725 nrn
= route_top(rnh_table
);
728 zebra_rnh_evaluate_entry(vrfid
, family
, force
,
730 nrn
= route_next(nrn
); /* this will also unlock nrn */
732 nrn
= route_top(rnh_table
);
735 zebra_rnh_clear_nhc_flag(vrfid
, family
, type
,
737 nrn
= route_next(nrn
); /* this will also unlock nrn */
742 void zebra_print_rnh_table(vrf_id_t vrfid
, int af
, struct vty
*vty
,
745 struct route_table
*table
;
746 struct route_node
*rn
;
748 table
= get_rnh_table(vrfid
, af
, type
);
750 zlog_debug("print_rnhs: rnh table not found\n");
754 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
759 int zebra_cleanup_rnh_client(vrf_id_t vrf_id
, int family
, struct zserv
*client
,
762 struct route_table
*ntable
;
763 struct route_node
*nrn
;
766 if (IS_ZEBRA_DEBUG_NHT
)
767 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
768 vrf_id
, zebra_route_string(client
->proto
), family
,
771 ntable
= get_rnh_table(vrf_id
, family
, type
);
773 zlog_debug("cleanup_rnh_client: rnh table not found\n");
777 for (nrn
= route_top(ntable
); nrn
; nrn
= route_next(nrn
)) {
782 zebra_remove_rnh_client(rnh
, client
, type
);
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 zebra_deregister_rnh_static_nexthops(vrf_id
, re
->nexthop
, rn
);
799 nexthops_free(re
->nexthop
);
803 static void copy_state(struct rnh
*rnh
, struct route_entry
*re
,
804 struct route_node
*rn
)
806 struct route_entry
*state
;
809 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
816 state
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
817 state
->type
= re
->type
;
818 state
->distance
= re
->distance
;
819 state
->metric
= re
->metric
;
821 route_entry_copy_nexthops(state
, re
->nexthop
);
825 static int compare_state(struct route_entry
*r1
, struct route_entry
*r2
)
831 if ((!r1
&& r2
) || (r1
&& !r2
))
834 if (r1
->distance
!= r2
->distance
)
837 if (r1
->metric
!= r2
->metric
)
840 if (r1
->nexthop_num
!= r2
->nexthop_num
)
843 if (CHECK_FLAG(r1
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
))
849 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
853 struct route_entry
*re
;
856 struct nexthop
*nexthop
;
857 struct route_node
*rn
;
858 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
) ? ZEBRA_IMPORT_CHECK_UPDATE
859 : ZEBRA_NEXTHOP_UPDATE
;
864 /* Get output stream. */
868 zserv_create_header(s
, cmd
, vrf_id
);
870 stream_putw(s
, rn
->p
.family
);
871 switch (rn
->p
.family
) {
873 stream_putc(s
, rn
->p
.prefixlen
);
874 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
877 stream_putc(s
, rn
->p
.prefixlen
);
878 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
881 zlog_err("%s: Unknown family (%d) notification attempted\n",
882 __FUNCTION__
, rn
->p
.family
);
886 stream_putc(s
, re
->distance
);
887 stream_putl(s
, re
->metric
);
889 nump
= stream_get_endp(s
);
891 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
892 if ((CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
893 || CHECK_FLAG(nexthop
->flags
,
894 NEXTHOP_FLAG_RECURSIVE
))
895 && CHECK_FLAG(nexthop
->flags
,
896 NEXTHOP_FLAG_ACTIVE
)) {
897 stream_putc(s
, nexthop
->type
);
898 switch (nexthop
->type
) {
899 case NEXTHOP_TYPE_IPV4
:
900 stream_put_in_addr(s
,
901 &nexthop
->gate
.ipv4
);
902 stream_putl(s
, nexthop
->ifindex
);
904 case NEXTHOP_TYPE_IFINDEX
:
905 stream_putl(s
, nexthop
->ifindex
);
907 case NEXTHOP_TYPE_IPV4_IFINDEX
:
908 stream_put_in_addr(s
,
909 &nexthop
->gate
.ipv4
);
910 stream_putl(s
, nexthop
->ifindex
);
912 case NEXTHOP_TYPE_IPV6
:
913 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
914 stream_putl(s
, nexthop
->ifindex
);
916 case NEXTHOP_TYPE_IPV6_IFINDEX
:
917 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
918 stream_putl(s
, nexthop
->ifindex
);
926 stream_putc_at(s
, nump
, num
);
928 stream_putc(s
, 0); // distance
929 stream_putl(s
, 0); // metric
930 stream_putc(s
, 0); // nexthops
932 stream_putw_at(s
, 0, stream_get_endp(s
));
934 client
->nh_last_upd_time
= monotime(NULL
);
935 client
->last_write_cmd
= cmd
;
936 return zebra_server_send_message(client
);
939 static void print_nh(struct nexthop
*nexthop
, struct vty
*vty
)
942 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
944 switch (nexthop
->type
) {
945 case NEXTHOP_TYPE_IPV4
:
946 case NEXTHOP_TYPE_IPV4_IFINDEX
:
947 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
948 if (nexthop
->ifindex
)
950 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
952 case NEXTHOP_TYPE_IPV6
:
953 case NEXTHOP_TYPE_IPV6_IFINDEX
:
955 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
956 if (nexthop
->ifindex
)
957 vty_out(vty
, ", via %s",
958 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
960 case NEXTHOP_TYPE_IFINDEX
:
961 vty_out(vty
, " is directly connected, %s",
962 ifindex2ifname_per_ns(zns
, nexthop
->ifindex
));
964 case NEXTHOP_TYPE_BLACKHOLE
:
965 vty_out(vty
, " is directly connected, Null0");
973 static void print_rnh(struct route_node
*rn
, struct vty
*vty
)
976 struct nexthop
*nexthop
;
977 struct listnode
*node
;
978 struct zserv
*client
;
982 vty_out(vty
, "%s%s\n",
983 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
984 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)"
987 vty_out(vty
, " resolved via %s\n",
988 zebra_route_string(rnh
->state
->type
));
989 for (nexthop
= rnh
->state
->nexthop
; nexthop
;
990 nexthop
= nexthop
->next
)
991 print_nh(nexthop
, vty
);
993 vty_out(vty
, " unresolved%s\n",
994 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
)
998 vty_out(vty
, " Client list:");
999 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1000 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1002 rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1003 if (!list_isempty(rnh
->zebra_static_route_list
))
1004 vty_out(vty
, " zebra%s",
1005 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] ? "(filtered)" : "");