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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "sockunion.h"
33 #include "workqueue.h"
40 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/interface.h"
50 #include "zebra/zebra_memory.h"
52 static void free_state(vrf_id_t vrf_id
, struct rib
*rib
, struct route_node
*rn
);
53 static void copy_state(struct rnh
*rnh
, struct rib
*rib
,
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 rib
*r1
, struct rib
*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
);
83 case RNH_NEXTHOP_TYPE
:
84 t
= zvrf
->rnh_table
[family2afi(family
)];
86 case RNH_IMPORT_CHECK_TYPE
:
87 t
= zvrf
->import_check_table
[family2afi(family
)];
94 char *rnh_str (struct rnh
*rnh
, char *buf
, int size
)
96 prefix2str(&(rnh
->node
->p
), buf
, size
);
101 zebra_add_rnh (struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
103 struct route_table
*table
;
104 struct route_node
*rn
;
105 struct rnh
*rnh
= NULL
;
106 char buf
[PREFIX2STR_BUFFER
];
108 if (IS_ZEBRA_DEBUG_NHT
)
110 prefix2str(p
, buf
, sizeof (buf
));
111 zlog_debug("%u: Add RNH %s type %d", vrfid
, buf
, type
);
113 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
116 prefix2str(p
, buf
, sizeof (buf
));
117 zlog_warn("%u: Add RNH %s type %d - table not found",
122 /* Make it sure prefixlen is applied to the prefix. */
125 /* Lookup (or add) route node.*/
126 rn
= route_node_get (table
, p
);
130 rnh
= XCALLOC(MTYPE_RNH
, sizeof(struct rnh
));
131 rnh
->client_list
= list_new();
133 rnh
->zebra_static_route_list
= list_new();
134 route_lock_node (rn
);
139 route_unlock_node (rn
);
144 zebra_lookup_rnh (struct prefix
*p
, vrf_id_t vrfid
, rnh_type_t type
)
146 struct route_table
*table
;
147 struct route_node
*rn
;
149 table
= get_rnh_table(vrfid
, PREFIX_FAMILY(p
), type
);
153 /* Make it sure prefixlen is applied to the prefix. */
156 /* Lookup route node.*/
157 rn
= route_node_lookup (table
, p
);
161 route_unlock_node (rn
);
166 zebra_free_rnh (struct rnh
*rnh
)
168 rnh
->flags
|= ZEBRA_NHT_DELETED
;
169 list_free (rnh
->client_list
);
170 list_free (rnh
->zebra_static_route_list
);
171 free_state (rnh
->vrf_id
, rnh
->state
, rnh
->node
);
172 XFREE (MTYPE_RNH
, rnh
);
176 zebra_delete_rnh (struct rnh
*rnh
, rnh_type_t type
)
178 struct route_node
*rn
;
180 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
) || !(rn
= rnh
->node
))
183 if (IS_ZEBRA_DEBUG_NHT
)
185 char buf
[PREFIX2STR_BUFFER
];
186 zlog_debug("%u: Del RNH %s type %d",
187 rnh
->vrf_id
, rnh_str(rnh
, buf
, sizeof (buf
)), type
);
190 zebra_free_rnh (rnh
);
192 route_unlock_node (rn
);
196 zebra_add_rnh_client (struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
,
199 if (IS_ZEBRA_DEBUG_NHT
)
201 char buf
[PREFIX2STR_BUFFER
];
202 zlog_debug("%u: Client %s registers for RNH %s type %d",
203 vrf_id
, zebra_route_string(client
->proto
),
204 rnh_str(rnh
, buf
, sizeof (buf
)), type
);
206 if (!listnode_lookup(rnh
->client_list
, client
))
208 listnode_add(rnh
->client_list
, client
);
209 send_client(rnh
, client
, type
, vrf_id
); // Pending: check if its needed
214 zebra_remove_rnh_client (struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
)
216 if (IS_ZEBRA_DEBUG_NHT
)
218 char buf
[PREFIX2STR_BUFFER
];
219 zlog_debug("Client %s unregisters for RNH %s type %d",
220 zebra_route_string(client
->proto
),
221 rnh_str(rnh
, buf
, sizeof (buf
)), type
);
223 listnode_delete(rnh
->client_list
, client
);
224 if (list_isempty(rnh
->client_list
) &&
225 list_isempty(rnh
->zebra_static_route_list
))
226 zebra_delete_rnh(rnh
, type
);
230 zebra_register_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
231 struct route_node
*static_rn
)
235 rnh
= zebra_add_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
236 if (rnh
&& !listnode_lookup(rnh
->zebra_static_route_list
, static_rn
))
238 listnode_add(rnh
->zebra_static_route_list
, static_rn
);
243 zebra_deregister_rnh_static_nh(vrf_id_t vrf_id
, struct prefix
*nh
,
244 struct route_node
*static_rn
)
248 rnh
= zebra_lookup_rnh(nh
, vrf_id
, RNH_NEXTHOP_TYPE
);
249 if (!rnh
|| (rnh
->flags
& ZEBRA_NHT_DELETED
))
252 listnode_delete(rnh
->zebra_static_route_list
, static_rn
);
254 if (list_isempty(rnh
->client_list
) &&
255 list_isempty(rnh
->zebra_static_route_list
))
256 zebra_delete_rnh(rnh
, RNH_NEXTHOP_TYPE
);
260 zebra_deregister_rnh_static_nexthops (vrf_id_t vrf_id
, struct nexthop
*nexthop
,
261 struct route_node
*rn
)
266 for (nh
= nexthop
; nh
; nh
= nh
->next
)
270 case NEXTHOP_TYPE_IPV4
:
271 case NEXTHOP_TYPE_IPV4_IFINDEX
:
272 nh_p
.family
= AF_INET
;
273 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
274 nh_p
.u
.prefix4
= nh
->gate
.ipv4
;
276 case NEXTHOP_TYPE_IPV6
:
277 case NEXTHOP_TYPE_IPV6_IFINDEX
:
278 nh_p
.family
= AF_INET6
;
279 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
280 nh_p
.u
.prefix6
= nh
->gate
.ipv6
;
283 * Not sure what really to do here, we are not
284 * supposed to have either of these for NHT
285 * and the code has no way to know what prefix
286 * to use. So I'm going to just continue
287 * for the moment, which is preferable to
288 * what is currently happening which is a
290 * Some simple testing shows that we
291 * are not leaving slag around for these
292 * skipped static routes. Since
293 * they don't appear to be installed
295 case NEXTHOP_TYPE_IFINDEX
:
296 case NEXTHOP_TYPE_BLACKHOLE
:
300 zebra_deregister_rnh_static_nh(vrf_id
, &nh_p
, rn
);
304 /* Apply the NHT route-map for a client to the route (and nexthops)
308 zebra_rnh_apply_nht_rmap(int family
, struct route_node
*prn
,
309 struct rib
*rib
, int proto
)
311 int at_least_one
= 0;
312 int rmap_family
; /* Route map has diff AF family enum */
313 struct nexthop
*nexthop
;
316 rmap_family
= (family
== AF_INET
) ? AFI_IP
: AFI_IP6
;
320 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
322 ret
= zebra_nht_route_map_check(rmap_family
, proto
, &prn
->p
, rib
,
324 if (ret
!= RMAP_DENYMATCH
)
326 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
327 at_least_one
++; /* at least one valid NH */
331 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
335 return (at_least_one
);
339 * Determine appropriate route (RIB entry) resolving a tracked entry
340 * (nexthop or BGP route for import).
343 zebra_rnh_resolve_entry (vrf_id_t vrfid
, int family
, rnh_type_t type
,
344 struct route_node
*nrn
, struct rnh
*rnh
,
345 struct route_node
**prn
)
347 struct route_table
*route_table
;
348 struct route_node
*rn
;
353 route_table
= zebra_vrf_table(family2afi(family
), SAFI_UNICAST
, vrfid
);
354 if (!route_table
) // unexpected
357 rn
= route_node_match(route_table
, &nrn
->p
);
361 /* When resolving nexthops, do not resolve via the default route unless
362 * 'ip nht resolve-via-default' is configured.
364 if ((type
== RNH_NEXTHOP_TYPE
) &&
365 (is_default_prefix (&rn
->p
) &&
366 !nh_resolve_via_default(rn
->p
.family
)))
368 else if ((type
== RNH_IMPORT_CHECK_TYPE
) &&
369 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
) &&
370 !prefix_same(&nrn
->p
, &rn
->p
))
374 /* Identify appropriate route entry. */
375 RNODE_FOREACH_RIB(rn
, rib
)
377 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
379 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
381 if (CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
383 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
386 else if ((type
== RNH_IMPORT_CHECK_TYPE
) &&
387 (rib
->type
== ZEBRA_ROUTE_BGP
))
395 /* Need to unlock route node */
396 route_unlock_node(rn
);
403 * See if a tracked route entry for import (by BGP) has undergone any
404 * change, and if so, notify the client.
407 zebra_rnh_eval_import_check_entry (vrf_id_t vrfid
, int family
, int force
,
408 struct route_node
*nrn
, struct rnh
*rnh
,
411 int state_changed
= 0;
412 struct zserv
*client
;
413 char bufn
[INET6_ADDRSTRLEN
];
414 struct listnode
*node
;
415 struct nexthop
*nexthop
, *tnexthop
;
418 if (rib
&& (rnh
->state
== NULL
))
420 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
421 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
427 else if (!rib
&& (rnh
->state
!= NULL
))
430 if (compare_state(rib
, rnh
->state
))
431 copy_state(rnh
, rib
, nrn
);
433 if (state_changed
|| force
)
435 if (IS_ZEBRA_DEBUG_NHT
)
437 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
438 zlog_debug("%u:%s: Route import check %s %s\n",
439 vrfid
, bufn
, rnh
->state
? "passed" : "failed",
440 state_changed
? "(state changed)" : "");
442 /* state changed, notify clients */
443 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
445 send_client(rnh
, client
, RNH_IMPORT_CHECK_TYPE
, vrfid
);
451 * Notify clients registered for this nexthop about a change.
454 zebra_rnh_notify_protocol_clients (vrf_id_t vrfid
, int family
,
455 struct route_node
*nrn
, struct rnh
*rnh
,
456 struct route_node
*prn
, struct rib
*rib
)
458 struct listnode
*node
;
459 struct zserv
*client
;
460 char bufn
[INET6_ADDRSTRLEN
];
461 char bufp
[INET6_ADDRSTRLEN
];
462 int num_resolving_nh
;
464 if (IS_ZEBRA_DEBUG_NHT
)
466 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
469 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
470 zlog_debug("%u:%s: NH resolved over route %s", vrfid
, bufn
, bufp
);
473 zlog_debug("%u:%s: NH has become unresolved", vrfid
, bufn
);
476 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
480 /* Apply route-map for this client to route resolving this
481 * nexthop to see if it is filtered or not.
483 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, rib
,
485 if (num_resolving_nh
)
486 rnh
->filtered
[client
->proto
] = 0;
488 rnh
->filtered
[client
->proto
] = 1;
490 if (IS_ZEBRA_DEBUG_NHT
)
491 zlog_debug("%u:%s: Notifying client %s about NH %s",
492 vrfid
, bufn
, zebra_route_string(client
->proto
),
493 num_resolving_nh
? "" : "(filtered by route-map)");
497 rnh
->filtered
[client
->proto
] = 0;
498 if (IS_ZEBRA_DEBUG_NHT
)
499 zlog_debug("%u:%s: Notifying client %s about NH (unreachable)",
500 vrfid
, bufn
, zebra_route_string(client
->proto
));
503 send_client(rnh
, client
, RNH_NEXTHOP_TYPE
, vrfid
);
508 zebra_rnh_process_static_routes (vrf_id_t vrfid
, int family
,
509 struct route_node
*nrn
, struct rnh
*rnh
,
510 struct route_node
*prn
, struct rib
*rib
)
512 struct listnode
*node
;
513 int num_resolving_nh
= 0;
514 struct route_node
*static_rn
;
516 struct nexthop
*nexthop
;
517 char bufn
[INET6_ADDRSTRLEN
];
518 char bufp
[INET6_ADDRSTRLEN
];
519 char bufs
[INET6_ADDRSTRLEN
];
521 if (IS_ZEBRA_DEBUG_NHT
)
523 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
525 prefix2str(&prn
->p
, bufp
, INET6_ADDRSTRLEN
);
530 /* Apply route-map for "static" to route resolving this
531 * nexthop to see if it is filtered or not.
533 num_resolving_nh
= zebra_rnh_apply_nht_rmap(family
, prn
, rib
,
535 if (num_resolving_nh
)
536 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
538 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 1;
541 rnh
->filtered
[ZEBRA_ROUTE_STATIC
] = 0;
543 /* Evaluate each static route associated with this nexthop. */
544 for (ALL_LIST_ELEMENTS_RO(rnh
->zebra_static_route_list
, node
,
547 RNODE_FOREACH_RIB(static_rn
, srib
)
549 if (srib
->type
== ZEBRA_ROUTE_STATIC
)
552 /* Set the filter flag for the correct nexthop - static route may
553 * be having multiple. We care here only about registered nexthops.
555 for (nexthop
= srib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
557 switch (nexthop
->type
)
559 case NEXTHOP_TYPE_IPV4
:
560 case NEXTHOP_TYPE_IPV4_IFINDEX
:
561 if (nexthop
->gate
.ipv4
.s_addr
== nrn
->p
.u
.prefix4
.s_addr
)
563 if (num_resolving_nh
)
564 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
566 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
569 case NEXTHOP_TYPE_IPV6
:
570 case NEXTHOP_TYPE_IPV6_IFINDEX
:
572 if (memcmp(&nexthop
->gate
.ipv6
,&nrn
->p
.u
.prefix6
, 16) == 0)
574 if (num_resolving_nh
)
575 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
577 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
);
585 if (IS_ZEBRA_DEBUG_NHT
)
587 prefix2str(&static_rn
->p
, bufs
, INET6_ADDRSTRLEN
);
589 zlog_debug("%u:%s: NH change %s, scheduling static route %s",
590 vrfid
, bufn
, num_resolving_nh
?
591 "" : "(filtered by route-map)", bufs
);
593 zlog_debug("%u:%s: NH unreachable, scheduling static route %s",
597 SET_FLAG(srib
->status
, RIB_ENTRY_CHANGED
);
598 SET_FLAG(srib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
601 rib_queue_add(static_rn
);
606 * See if a tracked nexthop entry has undergone any change, and if so,
607 * take appropriate action; this involves notifying any clients and/or
608 * scheduling dependent static routes for processing.
611 zebra_rnh_eval_nexthop_entry (vrf_id_t vrfid
, int family
, int force
,
612 struct route_node
*nrn
, struct rnh
*rnh
,
613 struct route_node
*prn
, struct rib
*rib
)
615 int state_changed
= 0;
617 /* If we're resolving over a different route, resolution has changed or
618 * the resolving route has some change (e.g., metric), there is a state
621 if (!prefix_same(&rnh
->resolved_route
, &prn
->p
))
624 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
627 prefix_copy(&rnh
->resolved_route
, &prn
->p
);
629 memset(&rnh
->resolved_route
, 0, sizeof(struct prefix
));
631 copy_state(rnh
, rib
, nrn
);
634 else if (compare_state(rib
, rnh
->state
))
637 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
639 copy_state(rnh
, rib
, nrn
);
643 if (state_changed
|| force
)
645 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
648 /* Notify registered protocol clients. */
649 zebra_rnh_notify_protocol_clients (vrfid
, family
, nrn
, rnh
,
652 /* Process static routes attached to this nexthop */
653 zebra_rnh_process_static_routes (vrfid
, family
, nrn
, rnh
,
658 /* Evaluate one tracked entry */
660 zebra_rnh_evaluate_entry (vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
661 struct route_node
*nrn
)
665 struct route_node
*prn
;
666 char bufn
[INET6_ADDRSTRLEN
];
668 if (IS_ZEBRA_DEBUG_NHT
)
670 prefix2str(&nrn
->p
, bufn
, INET6_ADDRSTRLEN
);
671 zlog_debug("%u:%s: Evaluate RNH, type %d %s",
672 vrfid
, bufn
, type
, force
? "(force)" : "");
677 /* Identify route entry (RIB) resolving this tracked entry. */
678 rib
= zebra_rnh_resolve_entry (vrfid
, family
, type
, nrn
, rnh
, &prn
);
680 /* If the entry cannot be resolved and that is also the existing state,
681 * there is nothing further to do.
683 if (!rib
&& rnh
->state
== NULL
&& !force
)
686 /* Process based on type of entry. */
687 if (type
== RNH_IMPORT_CHECK_TYPE
)
688 zebra_rnh_eval_import_check_entry (vrfid
, family
, force
,
691 zebra_rnh_eval_nexthop_entry (vrfid
, family
, force
,
696 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
697 * of a particular VRF and address-family or a specific prefix.
700 zebra_evaluate_rnh (vrf_id_t vrfid
, int family
, int force
, rnh_type_t type
,
703 struct route_table
*rnh_table
;
704 struct route_node
*nrn
;
706 rnh_table
= get_rnh_table(vrfid
, family
, type
);
707 if (!rnh_table
) // unexpected
712 /* Evaluating a specific entry, make sure it exists. */
713 nrn
= route_node_lookup (rnh_table
, p
);
714 if (nrn
&& nrn
->info
)
715 zebra_rnh_evaluate_entry (vrfid
, family
, force
, type
, nrn
);
717 route_unlock_node (nrn
);
721 /* Evaluate entire table. */
722 nrn
= route_top (rnh_table
);
726 zebra_rnh_evaluate_entry (vrfid
, family
, force
, type
, nrn
);
727 nrn
= route_next(nrn
); /* this will also unlock nrn */
733 zebra_print_rnh_table (vrf_id_t vrfid
, int af
, struct vty
*vty
, rnh_type_t type
)
735 struct route_table
*table
;
736 struct route_node
*rn
;
738 table
= get_rnh_table(vrfid
, af
, type
);
741 zlog_debug("print_rnhs: rnh table not found\n");
745 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
751 zebra_cleanup_rnh_client (vrf_id_t vrf_id
, int family
, struct zserv
*client
,
754 struct route_table
*ntable
;
755 struct route_node
*nrn
;
758 if (IS_ZEBRA_DEBUG_NHT
)
759 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
760 vrf_id
, zebra_route_string(client
->proto
), family
, type
);
762 ntable
= get_rnh_table(vrf_id
, family
, type
);
765 zlog_debug("cleanup_rnh_client: rnh table not found\n");
769 for (nrn
= route_top (ntable
); nrn
; nrn
= route_next (nrn
))
775 zebra_remove_rnh_client(rnh
, client
, type
);
781 * free_state - free up the rib structure associated with the rnh.
784 free_state (vrf_id_t vrf_id
, struct rib
*rib
, struct route_node
*rn
)
790 /* free RIB and nexthops */
791 zebra_deregister_rnh_static_nexthops (vrf_id
, rib
->nexthop
, rn
);
792 nexthops_free(rib
->nexthop
);
793 XFREE (MTYPE_RIB
, rib
);
797 copy_state (struct rnh
*rnh
, struct rib
*rib
, struct route_node
*rn
)
804 free_state(rnh
->vrf_id
, rnh
->state
, rn
);
811 state
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
812 state
->type
= rib
->type
;
813 state
->metric
= rib
->metric
;
815 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
816 rib_copy_nexthops(state
, nh
);
821 compare_state (struct rib
*r1
, struct rib
*r2
)
827 if ((!r1
&& r2
) || (r1
&& !r2
))
830 if (r1
->metric
!= r2
->metric
)
833 if (r1
->nexthop_num
!= r2
->nexthop_num
)
836 if (CHECK_FLAG(r1
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
))
843 send_client (struct rnh
*rnh
, struct zserv
*client
, rnh_type_t type
, vrf_id_t vrf_id
)
849 struct nexthop
*nexthop
;
850 struct route_node
*rn
;
851 int cmd
= (type
== RNH_IMPORT_CHECK_TYPE
)
852 ? ZEBRA_IMPORT_CHECK_UPDATE
: ZEBRA_NEXTHOP_UPDATE
;
857 /* Get output stream. */
861 zserv_create_header (s
, cmd
, vrf_id
);
863 stream_putw(s
, rn
->p
.family
);
864 switch (rn
->p
.family
)
867 stream_putc(s
, rn
->p
.prefixlen
);
868 stream_put_in_addr(s
, &rn
->p
.u
.prefix4
);
871 stream_putc(s
, rn
->p
.prefixlen
);
872 stream_put(s
, &rn
->p
.u
.prefix6
, IPV6_MAX_BYTELEN
);
875 zlog_err("%s: Unknown family (%d) notification attempted\n",
876 __FUNCTION__
, rn
->p
.family
);
881 stream_putl (s
, rib
->metric
);
883 nump
= stream_get_endp(s
);
885 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
886 if ((CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ||
887 CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)) &&
888 CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
890 stream_putc (s
, nexthop
->type
);
891 switch (nexthop
->type
)
893 case NEXTHOP_TYPE_IPV4
:
894 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
896 case NEXTHOP_TYPE_IFINDEX
:
897 stream_putl (s
, nexthop
->ifindex
);
899 case NEXTHOP_TYPE_IPV4_IFINDEX
:
900 stream_put_in_addr (s
, &nexthop
->gate
.ipv4
);
901 stream_putl (s
, nexthop
->ifindex
);
903 case NEXTHOP_TYPE_IPV6
:
904 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
906 case NEXTHOP_TYPE_IPV6_IFINDEX
:
907 stream_put (s
, &nexthop
->gate
.ipv6
, 16);
908 stream_putl (s
, nexthop
->ifindex
);
916 stream_putc_at (s
, nump
, num
);
923 stream_putw_at (s
, 0, stream_get_endp (s
));
925 client
->nh_last_upd_time
= quagga_monotime();
926 client
->last_write_cmd
= cmd
;
927 return zebra_server_send_message(client
);
931 print_nh (struct nexthop
*nexthop
, struct vty
*vty
)
934 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
936 switch (nexthop
->type
)
938 case NEXTHOP_TYPE_IPV4
:
939 case NEXTHOP_TYPE_IPV4_IFINDEX
:
940 vty_out (vty
, " via %s", inet_ntoa (nexthop
->gate
.ipv4
));
941 if (nexthop
->ifindex
)
942 vty_out (vty
, ", %s", ifindex2ifname_per_ns (zns
, nexthop
->ifindex
));
944 case NEXTHOP_TYPE_IPV6
:
945 case NEXTHOP_TYPE_IPV6_IFINDEX
:
947 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
948 if (nexthop
->ifindex
)
949 vty_out (vty
, ", via %s", ifindex2ifname_per_ns (zns
, nexthop
->ifindex
));
951 case NEXTHOP_TYPE_IFINDEX
:
952 vty_out (vty
, " is directly connected, %s",
953 ifindex2ifname_per_ns (zns
, nexthop
->ifindex
));
955 case NEXTHOP_TYPE_BLACKHOLE
:
956 vty_out (vty
, " is directly connected, Null0");
961 vty_out(vty
, "%s", VTY_NEWLINE
);
965 print_rnh (struct route_node
*rn
, struct vty
*vty
)
968 struct nexthop
*nexthop
;
969 struct listnode
*node
;
970 struct zserv
*client
;
974 vty_out(vty
, "%s%s%s", inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
975 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)" : "",
979 vty_out(vty
, " resolved via %s%s",
980 zebra_route_string(rnh
->state
->type
), VTY_NEWLINE
);
981 for (nexthop
= rnh
->state
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
982 print_nh(nexthop
, vty
);
985 vty_out(vty
, " unresolved%s%s",
986 CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
) ? "(Connected)" : "",
989 vty_out(vty
, " Client list:");
990 for (ALL_LIST_ELEMENTS_RO(rnh
->client_list
, node
, client
))
991 vty_out(vty
, " %s(fd %d)%s", zebra_route_string(client
->proto
),
992 client
->sock
, rnh
->filtered
[client
->proto
] ? "(filtered)" : "");
993 if (!list_isempty(rnh
->zebra_static_route_list
))
994 vty_out(vty
, " zebra%s", rnh
->filtered
[ZEBRA_ROUTE_STATIC
] ? "(filtered)" : "");
995 vty_out(vty
, "%s", VTY_NEWLINE
);