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 rib
*rib
, struct route_node
*rn
);
52 static void copy_state(struct rnh
*rnh
, struct rib
*rib
,
53 struct route_node
*rn
);
54 #define lookup_rnh_table(v, f) \
56 struct zebra_vrf *zvrf; \
57 struct route_table *t = NULL; \
58 zvrf = zebra_vrf_lookup_by_id(v); \
60 t = zvrf->rnh_table[family2afi(f)]; \
64 static int compare_state(struct rib
*r1
, struct rib
*r2
);
65 static int send_client(struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
67 static void print_rnh(struct route_node
*rn
, struct vty
*vty
);
69 int zebra_rnh_ip_default_route
= 0;
70 int zebra_rnh_ipv6_default_route
= 0;
72 static inline struct route_table
*get_rnh_table(vrf_id_t vrfid
, int family
,
75 struct zebra_vrf
*zvrf
;
76 struct route_table
*t
= NULL
;
78 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
);
100 zebra_add_rnh (struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
102 struct route_table
*table
;
103 struct route_node
*rn
;
104 struct rnh
*rnh
= NULL
;
105 char buf
[PREFIX2STR_BUFFER
];
107 if (IS_ZEBRA_DEBUG_NHT
)
109 prefix2str(p
, buf
, sizeof (buf
));
110 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
112 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
115 prefix2str(p
, buf
, sizeof (buf
));
116 zlog_warn("%u: Add RNH %s type %d - table not found",
121 /* Make it sure prefixlen is applied to the prefix. */
124 /* Lookup (or add) route node.*/
125 rn
= route_node_get (table
, p
);
129 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
130 rnh
->client_list
= list_new();
132 rnh
->zebra_static_route_list
= list_new();
133 route_lock_node (rn
);
138 route_unlock_node (rn
);
143 zebra_lookup_rnh (struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
145 struct route_table
*table
;
146 struct route_node
*rn
;
148 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
152 /* Make it sure prefixlen is applied to the prefix. */
155 /* Lookup route node.*/
156 rn
= route_node_lookup (table
, p
);
160 route_unlock_node (rn
);
165 zebra_free_rnh (struct rnh
*rnh
)
167 rnh
->flags
|= ZEBRA_NHT_DELETED
;
168 list_free (rnh
->client_list
);
169 list_free (rnh
->zebra_static_route_list
);
170 free_state (rnh
->vrf_id
, rnh
->state
, rnh
->node
);
171 XFREE (MTYPE_RNH
, rnh
);
175 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
)
184 char buf
[PREFIX2STR_BUFFER
];
185 zlog_debug("%u: Del RNH %s type %d",
186 rnh
->vrf_id
, rnh_str(rnh
, buf
, sizeof (buf
)), type
);
189 zebra_free_rnh (rnh
);
191 route_unlock_node (rn
);
195 zebra_add_rnh_client (struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
198 if (IS_ZEBRA_DEBUG_NHT
)
200 char buf
[PREFIX2STR_BUFFER
];
201 zlog_debug("%u: Client %s registers for RNH %s type %d",
202 vrf_id
, zebra_route_string(client
->proto
),
203 rnh_str(rnh
, buf
, sizeof (buf
)), type
);
205 if (!listnode_lookup(rnh
->client_list
, client
))
207 listnode_add(rnh
->client_list
, client
);
208 send_client(rnh
, client
, type
, vrf_id
); // Pending: check if its needed
213 zebra_remove_rnh_client (struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
)
215 if (IS_ZEBRA_DEBUG_NHT
)
217 char buf
[PREFIX2STR_BUFFER
];
218 zlog_debug("Client %s unregisters for RNH %s type %d",
219 zebra_route_string(client
->proto
),
220 rnh_str(rnh
, buf
, sizeof (buf
)), type
);
222 listnode_delete(rnh
->client_list
, client
);
223 if (list_isempty(rnh
->client_list
) &&
224 list_isempty(rnh
->zebra_static_route_list
))
225 zebra_delete_rnh(rnh
, type
);
229 zebra_register_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
230 struct route_node
*static_rn
)
234 rnh
= zebra_add_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
235 if (rnh
&& !listnode_lookup(rnh
->zebra_static_route_list
, static_rn
))
237 listnode_add(rnh
->zebra_static_route_list
, static_rn
);
242 zebra_deregister_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
243 struct route_node
*static_rn
)
247 rnh
= zebra_lookup_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
248 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
))
251 listnode_delete(rnh
->zebra_static_route_list
, static_rn
);
253 if (list_isempty(rnh
->client_list
) &&
254 list_isempty(rnh
->zebra_static_route_list
))
255 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
259 zebra_deregister_rnh_static_nexthops (vrf_id_t vrf_id
, struct nexthop
*nexthop
,
260 struct route_node
*rn
)
265 for (nh
= nexthop
; nh
; nh
= nh
->next
)
269 case NEXTHOP_TYPE_IPV4
:
270 case NEXTHOP_TYPE_IPV4_IFINDEX
:
271 nh_p
.family
= AF_INET
;
272 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
273 nh_p
.u
.prefix4
= nh
->gate
.ipv4
;
275 case NEXTHOP_TYPE_IPV6
:
276 case NEXTHOP_TYPE_IPV6_IFINDEX
:
277 nh_p
.family
= AF_INET6
;
278 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
279 nh_p
.u
.prefix6
= nh
->gate
.ipv6
;
282 * Not sure what really to do here, we are not
283 * supposed to have either of these for NHT
284 * and the code has no way to know what prefix
285 * to use. So I'm going to just continue
286 * for the moment, which is preferable to
287 * what is currently happening which is a
289 * Some simple testing shows that we
290 * are not leaving slag around for these
291 * skipped static routes. Since
292 * they don't appear to be installed
294 case NEXTHOP_TYPE_IFINDEX
:
295 case NEXTHOP_TYPE_BLACKHOLE
:
299 zebra_deregister_rnh_static_nh(vrf_id
, &nh_p
, rn
);
303 /* Apply the NHT route-map for a client to the route (and nexthops)
307 zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
308 struct rib
*rib
, int proto
)
310 int at_least_one
= 0;
311 int rmap_family
; /* Route map has diff AF family enum */
312 struct nexthop
*nexthop
;
315 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
319 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
321 ret
= zebra_nht_route_map_check(rmap_family
, proto
, &prn
->p
, rib
,
323 if (ret
!= RMAP_DENYMATCH
)
325 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
326 at_least_one
++; /* at least one valid NH */
330 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
334 return (at_least_one
);
338 * Determine appropriate route (RIB entry) resolving a tracked entry
339 * (nexthop or BGP route for import).
342 zebra_rnh_resolve_entry (vrf_id_t vrfid
, int family
, rnh_type_t type
,
343 struct route_node
*nrn
, struct rnh
*rnh
,
344 struct route_node
**prn
)
346 struct route_table
*route_table
;
347 struct route_node
*rn
;
352 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
353 if (!route_table
) // unexpected
356 rn
= route_node_match(route_table
, &nrn
->p
);
360 /* When resolving nexthops, do not resolve via the default route unless
361 * 'ip nht resolve-via-default' is configured.
363 if ((type
== RNH_NEXTHOP_TYPE
) &&
364 (is_default_prefix (&rn
->p
) &&
365 !nh_resolve_via_default(rn
->p
.family
)))
367 else if ((type
== RNH_IMPORT_CHECK_TYPE
) &&
368 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
) &&
369 !prefix_same(&nrn
->p
, &rn
->p
))
373 /* Identify appropriate route entry. */
374 RNODE_FOREACH_RIB(rn
, rib
)
376 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
378 if (! CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
381 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
383 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
385 if (rib
->type
== ZEBRA_ROUTE_NHRP
)
387 struct nexthop
*nexthop
;
388 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
389 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
395 else if ((type
== RNH_IMPORT_CHECK_TYPE
) &&
396 (rib
->type
== ZEBRA_ROUTE_BGP
))
403 /* Need to unlock route node */
404 route_unlock_node(rn
);
411 * See if a tracked route entry for import (by BGP) has undergone any
412 * change, and if so, notify the client.
415 zebra_rnh_eval_import_check_entry (vrf_id_t vrfid
, int family
, int force
,
416 struct route_node
*nrn
, struct rnh
*rnh
,
419 int state_changed
= 0;
420 struct zserv
*client
;
421 char bufn
[INET6_ADDRSTRLEN
];
422 struct listnode
*node
;
423 struct nexthop
*nexthop
, *tnexthop
;
426 if (rib
&& (rnh
->state
== NULL
))
428 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
429 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
435 else if (!rib
&& (rnh
->state
!= NULL
))
438 if (compare_state(rib
, rnh
->state
))
439 copy_state(rnh
, rib
, nrn
);
441 if (state_changed
|| force
)
443 if (IS_ZEBRA_DEBUG_NHT
)
445 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
446 zlog_debug("%u:%s: Route import check %s %s\n",
447 vrfid
, bufn
, rnh
->state
? "passed" : "failed",
448 state_changed
? "(state changed)" : "");
450 /* state changed, notify clients */
451 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
453 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
459 * Notify clients registered for this nexthop about a change.
462 zebra_rnh_notify_protocol_clients (vrf_id_t vrfid
, int family
,
463 struct route_node
*nrn
, struct rnh
*rnh
,
464 struct route_node
*prn
, struct rib
*rib
)
466 struct listnode
*node
;
467 struct zserv
*client
;
468 char bufn
[INET6_ADDRSTRLEN
];
469 char bufp
[INET6_ADDRSTRLEN
];
470 int num_resolving_nh
;
472 if (IS_ZEBRA_DEBUG_NHT
)
474 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
477 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
478 zlog_debug("%u:%s: NH resolved over route %s", vrfid
, bufn
, bufp
);
481 zlog_debug("%u:%s: NH has become unresolved", vrfid
, bufn
);
484 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
488 /* Apply route-map for this client to route resolving this
489 * nexthop to see if it is filtered or not.
491 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, rib
,
493 if (num_resolving_nh
)
494 rnh
->filtered
[client
->proto
] = 0;
496 rnh
->filtered
[client
->proto
] = 1;
498 if (IS_ZEBRA_DEBUG_NHT
)
499 zlog_debug("%u:%s: Notifying client %s about NH %s",
500 vrfid
, bufn
, zebra_route_string(client
->proto
),
501 num_resolving_nh
? "" : "(filtered by route-map)");
505 rnh
->filtered
[client
->proto
] = 0;
506 if (IS_ZEBRA_DEBUG_NHT
)
507 zlog_debug("%u:%s: Notifying client %s about NH (unreachable)",
508 vrfid
, bufn
, zebra_route_string(client
->proto
));
511 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
516 zebra_rnh_process_static_routes (vrf_id_t vrfid
, int family
,
517 struct route_node
*nrn
, struct rnh
*rnh
,
518 struct route_node
*prn
, struct rib
*rib
)
520 struct listnode
*node
;
521 int num_resolving_nh
= 0;
522 struct route_node
*static_rn
;
524 struct nexthop
*nexthop
;
525 char bufn
[INET6_ADDRSTRLEN
];
526 char bufp
[INET6_ADDRSTRLEN
];
527 char bufs
[INET6_ADDRSTRLEN
];
529 if (IS_ZEBRA_DEBUG_NHT
)
531 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
533 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
538 /* Apply route-map for "static" to route resolving this
539 * nexthop to see if it is filtered or not.
541 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, rib
,
543 if (num_resolving_nh
)
544 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
546 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 1;
549 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
551 /* Evaluate each static route associated with this nexthop. */
552 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_static_route_list
, node
,
555 RNODE_FOREACH_RIB(static_rn
, srib
)
557 if (srib
->type
== ZEBRA_ROUTE_STATIC
)
560 /* Set the filter flag for the correct nexthop - static route may
561 * be having multiple. We care here only about registered nexthops.
563 for (nexthop
= srib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
565 switch (nexthop
->type
)
567 case NEXTHOP_TYPE_IPV4
:
568 case NEXTHOP_TYPE_IPV4_IFINDEX
:
569 if (nexthop
->gate
.ipv4
.s_addr
== nrn
->p
.u
.prefix4
.s_addr
)
571 if (num_resolving_nh
)
572 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
574 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
577 case NEXTHOP_TYPE_IPV6
:
578 case NEXTHOP_TYPE_IPV6_IFINDEX
:
580 if (memcmp(&nexthop
->gate
.ipv6
,&nrn
->p
.u
.prefix6
, 16) == 0)
582 if (num_resolving_nh
)
583 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
585 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
593 if (IS_ZEBRA_DEBUG_NHT
)
595 prefix2str(&static_rn
->p
, bufs
, INET6_ADDRSTRLEN
);
597 zlog_debug("%u:%s: NH change %s, scheduling static route %s",
598 vrfid
, bufn
, num_resolving_nh
?
599 "" : "(filtered by route-map)", bufs
);
601 zlog_debug("%u:%s: NH unreachable, scheduling static route %s",
605 SET_FLAG(srib
->status
, RIB_ENTRY_CHANGED
);
606 SET_FLAG(srib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
609 rib_queue_add(static_rn
);
614 * See if a tracked nexthop entry has undergone any change, and if so,
615 * take appropriate action; this involves notifying any clients and/or
616 * scheduling dependent static routes for processing.
619 zebra_rnh_eval_nexthop_entry (vrf_id_t vrfid
, int family
, int force
,
620 struct route_node
*nrn
, struct rnh
*rnh
,
621 struct route_node
*prn
, struct rib
*rib
)
623 int state_changed
= 0;
625 /* If we're resolving over a different route, resolution has changed or
626 * the resolving route has some change (e.g., metric), there is a state
629 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
))
632 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
634 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
636 copy_state(rnh
, rib
, nrn
);
639 else if (compare_state(rib
, rnh
->state
))
641 copy_state(rnh
, rib
, nrn
);
645 if (state_changed
|| force
)
647 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
650 /* Notify registered protocol clients. */
651 zebra_rnh_notify_protocol_clients (vrfid
, family
, nrn
, rnh
,
654 /* Process static routes attached to this nexthop */
655 zebra_rnh_process_static_routes (vrfid
, family
, nrn
, rnh
,
660 /* Evaluate one tracked entry */
662 zebra_rnh_evaluate_entry (vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
663 struct route_node
*nrn
)
667 struct route_node
*prn
;
668 char bufn
[INET6_ADDRSTRLEN
];
670 if (IS_ZEBRA_DEBUG_NHT
)
672 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
673 zlog_debug("%u:%s: Evaluate RNH, type %d %s",
674 vrfid
, bufn
, type
, force
? "(force)" : "");
679 /* Identify route entry (RIB) resolving this tracked entry. */
680 rib
= zebra_rnh_resolve_entry (vrfid
, family
, type
, nrn
, rnh
, &prn
);
682 /* If the entry cannot be resolved and that is also the existing state,
683 * there is nothing further to do.
685 if (!rib
&& rnh
->state
== NULL
&& !force
)
688 /* Process based on type of entry. */
689 if (type
== RNH_IMPORT_CHECK_TYPE
)
690 zebra_rnh_eval_import_check_entry (vrfid
, family
, force
,
693 zebra_rnh_eval_nexthop_entry (vrfid
, family
, force
,
698 * Clear the RIB_ENTRY_NEXTHOPS_CHANGED flag
699 * from the rib entries.
701 * Please note we are doing this *after* we have
702 * notified the world about each nexthop as that
703 * we can have a situation where one rib entry
704 * covers multiple nexthops we are interested in.
707 zebra_rnh_clear_nhc_flag (vrf_id_t vrfid
, int family
, rnh_type_t type
,
708 struct route_node
*nrn
)
712 struct route_node
*prn
;
716 rib
= zebra_rnh_resolve_entry (vrfid
, family
, type
, nrn
, rnh
, &prn
);
719 UNSET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
722 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
723 * of a particular VRF and address-family or a specific prefix.
726 zebra_evaluate_rnh (vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
729 struct route_table
*rnh_table
;
730 struct route_node
*nrn
;
732 rnh_table
= get_rnh_table(vrfid
, family
, type
);
733 if (!rnh_table
) // unexpected
738 /* Evaluating a specific entry, make sure it exists. */
739 nrn
= route_node_lookup (rnh_table
, p
);
740 if (nrn
&& nrn
->info
)
741 zebra_rnh_evaluate_entry (vrfid
, family
, force
, type
, nrn
);
744 route_unlock_node (nrn
);
748 /* Evaluate entire table. */
749 nrn
= route_top (rnh_table
);
753 zebra_rnh_evaluate_entry (vrfid
, family
, force
, type
, nrn
);
754 nrn
= route_next(nrn
); /* this will also unlock nrn */
756 nrn
= route_top (rnh_table
);
760 zebra_rnh_clear_nhc_flag (vrfid
, family
, type
, nrn
);
761 nrn
= route_next(nrn
); /* this will also unlock nrn */
767 zebra_print_rnh_table (vrf_id_t vrfid
, int af
, struct vty
*vty
, rnh_type_t type
)
769 struct route_table
*table
;
770 struct route_node
*rn
;
772 table
= get_rnh_table(vrfid
, af
, type
);
775 zlog_debug("print_rnhs: rnh table not found\n");
779 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
785 zebra_cleanup_rnh_client (vrf_id_t vrf_id
, int family
, struct zserv
*client
,
788 struct route_table
*ntable
;
789 struct route_node
*nrn
;
792 if (IS_ZEBRA_DEBUG_NHT
)
793 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
794 vrf_id
, zebra_route_string(client
->proto
), family
, type
);
796 ntable
= get_rnh_table(vrf_id
, family
, type
);
799 zlog_debug("cleanup_rnh_client: rnh table not found\n");
803 for (nrn
= route_top (ntable
); nrn
; nrn
= route_next (nrn
))
809 zebra_remove_rnh_client(rnh
, client
, type
);
815 * free_state - free up the rib structure associated with the rnh.
818 free_state (vrf_id_t vrf_id
, struct rib
*rib
, struct route_node
*rn
)
824 /* free RIB and nexthops */
825 zebra_deregister_rnh_static_nexthops (vrf_id
, rib
->nexthop
, rn
);
826 nexthops_free(rib
->nexthop
);
827 XFREE (MTYPE_RIB
, rib
);
831 copy_state (struct rnh
*rnh
, struct rib
*rib
, struct route_node
*rn
)
838 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
845 state
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
846 state
->type
= rib
->type
;
847 state
->metric
= rib
->metric
;
849 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
850 rib_copy_nexthops(state
, nh
);
855 compare_state (struct rib
*r1
, struct rib
*r2
)
861 if ((!r1
&& r2
) || (r1
&& !r2
))
864 if (r1
->metric
!= r2
->metric
)
867 if (r1
->nexthop_num
!= r2
->nexthop_num
)
870 if (CHECK_FLAG(r1
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
))
877 send_client (struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
, vrf_id_t vrf_id
)
883 struct nexthop
*nexthop
;
884 struct route_node
*rn
;
885 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
)
886 ? ZEBRA_IMPORT_CHECK_UPDATE
: ZEBRA_NEXTHOP_UPDATE
;
891 /* Get output stream. */
895 zserv_create_header (s
, cmd
, vrf_id
);
897 stream_putw(s
, rn
->p
.family
);
898 switch (rn
->p
.family
)
901 stream_putc(s
, rn
->p
.prefixlen
);
902 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
905 stream_putc(s
, rn
->p
.prefixlen
);
906 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
909 zlog_err("%s: Unknown family (%d) notification attempted\n",
910 __FUNCTION__
, rn
->p
.family
);
915 stream_putc (s
, rib
->distance
);
916 stream_putl (s
, rib
->metric
);
918 nump
= stream_get_endp(s
);
920 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
921 if ((CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ||
922 CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)) &&
923 CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
925 stream_putc (s
, nexthop
->type
);
926 switch (nexthop
->type
)
928 case NEXTHOP_TYPE_IPV4
:
929 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
930 stream_putl (s
, nexthop
->ifindex
);
932 case NEXTHOP_TYPE_IFINDEX
:
933 stream_putl (s
, nexthop
->ifindex
);
935 case NEXTHOP_TYPE_IPV4_IFINDEX
:
936 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
937 stream_putl (s
, nexthop
->ifindex
);
939 case NEXTHOP_TYPE_IPV6
:
940 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
941 stream_putl (s
, nexthop
->ifindex
);
943 case NEXTHOP_TYPE_IPV6_IFINDEX
:
944 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
945 stream_putl (s
, nexthop
->ifindex
);
953 stream_putc_at (s
, nump
, num
);
957 stream_putc (s
, 0); // distance
958 stream_putl (s
, 0); // metric
959 stream_putc (s
, 0); // nexthops
961 stream_putw_at (s
, 0, stream_get_endp (s
));
963 client
->nh_last_upd_time
= monotime(NULL
);
964 client
->last_write_cmd
= cmd
;
965 return zebra_server_send_message(client
);
969 print_nh (struct nexthop
*nexthop
, struct vty
*vty
)
972 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
974 switch (nexthop
->type
)
976 case NEXTHOP_TYPE_IPV4
:
977 case NEXTHOP_TYPE_IPV4_IFINDEX
:
978 vty_out (vty
, " via %s", inet_ntoa (nexthop
->gate
.ipv4
));
979 if (nexthop
->ifindex
)
980 vty_out (vty
, ", %s", ifindex2ifname_per_ns (zns
, nexthop
->ifindex
));
982 case NEXTHOP_TYPE_IPV6
:
983 case NEXTHOP_TYPE_IPV6_IFINDEX
:
985 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
986 if (nexthop
->ifindex
)
987 vty_out (vty
, ", via %s", ifindex2ifname_per_ns (zns
, nexthop
->ifindex
));
989 case NEXTHOP_TYPE_IFINDEX
:
990 vty_out (vty
, " is directly connected, %s",
991 ifindex2ifname_per_ns (zns
, nexthop
->ifindex
));
993 case NEXTHOP_TYPE_BLACKHOLE
:
994 vty_out (vty
, " is directly connected, Null0");
999 vty_out(vty
, "%s", VTY_NEWLINE
);
1003 print_rnh (struct route_node
*rn
, struct vty
*vty
)
1006 struct nexthop
*nexthop
;
1007 struct listnode
*node
;
1008 struct zserv
*client
;
1012 vty_out(vty
, "%s%s%s", inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
1013 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)" : "",
1017 vty_out(vty
, " resolved via %s%s",
1018 zebra_route_string(rnh
->state
->type
), VTY_NEWLINE
);
1019 for (nexthop
= rnh
->state
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1020 print_nh(nexthop
, vty
);
1023 vty_out(vty
, " unresolved%s%s",
1024 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)" : "",
1027 vty_out(vty
, " Client list:");
1028 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
1029 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
1030 client
->sock
, rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
1031 if (!list_isempty(rnh
->zebra_static_route_list
))
1032 vty_out(vty
, " zebra%s", rnh
->filtered
[ZEBRA_ROUTE_STATIC
] ? "(filtered)" : "");
1033 vty_out(vty
, "%s", VTY_NEWLINE
);