1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A generic nexthop structure
3 * Copyright (C) 2013 Cumulus Networks, Inc.
12 #include "sockunion.h"
20 #include "nexthop_group.h"
22 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop");
23 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label");
24 DEFINE_MTYPE_STATIC(LIB
, NH_SRV6
, "Nexthop srv6");
26 static int _nexthop_labels_cmp(const struct nexthop
*nh1
,
27 const struct nexthop
*nh2
)
29 const struct mpls_label_stack
*nhl1
= NULL
;
30 const struct mpls_label_stack
*nhl2
= NULL
;
35 /* No labels is a match */
45 if (nhl1
->num_labels
> nhl2
->num_labels
)
48 if (nhl1
->num_labels
< nhl2
->num_labels
)
51 return memcmp(nhl1
->label
, nhl2
->label
,
52 (nhl1
->num_labels
* sizeof(mpls_label_t
)));
55 static int _nexthop_srv6_cmp(const struct nexthop
*nh1
,
56 const struct nexthop
*nh2
)
60 if (!nh1
->nh_srv6
&& !nh2
->nh_srv6
)
63 if (nh1
->nh_srv6
&& !nh2
->nh_srv6
)
66 if (!nh1
->nh_srv6
&& nh2
->nh_srv6
)
69 if (nh1
->nh_srv6
->seg6local_action
> nh2
->nh_srv6
->seg6local_action
)
72 if (nh2
->nh_srv6
->seg6local_action
< nh1
->nh_srv6
->seg6local_action
)
75 ret
= memcmp(&nh1
->nh_srv6
->seg6local_ctx
,
76 &nh2
->nh_srv6
->seg6local_ctx
,
77 sizeof(struct seg6local_context
));
81 ret
= memcmp(&nh1
->nh_srv6
->seg6_segs
,
82 &nh2
->nh_srv6
->seg6_segs
,
83 sizeof(struct in6_addr
));
88 int nexthop_g_addr_cmp(enum nexthop_types_t type
, const union g_addr
*addr1
,
89 const union g_addr
*addr2
)
94 case NEXTHOP_TYPE_IPV4
:
95 case NEXTHOP_TYPE_IPV4_IFINDEX
:
96 ret
= IPV4_ADDR_CMP(&addr1
->ipv4
, &addr2
->ipv4
);
98 case NEXTHOP_TYPE_IPV6
:
99 case NEXTHOP_TYPE_IPV6_IFINDEX
:
100 ret
= IPV6_ADDR_CMP(&addr1
->ipv6
, &addr2
->ipv6
);
102 case NEXTHOP_TYPE_IFINDEX
:
103 case NEXTHOP_TYPE_BLACKHOLE
:
111 static int _nexthop_gateway_cmp(const struct nexthop
*nh1
,
112 const struct nexthop
*nh2
)
114 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
117 static int _nexthop_source_cmp(const struct nexthop
*nh1
,
118 const struct nexthop
*nh2
)
120 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->src
, &nh2
->src
);
123 static int _nexthop_cmp_no_labels(const struct nexthop
*next1
,
124 const struct nexthop
*next2
)
128 if (next1
->vrf_id
< next2
->vrf_id
)
131 if (next1
->vrf_id
> next2
->vrf_id
)
134 if (next1
->type
< next2
->type
)
137 if (next1
->type
> next2
->type
)
140 if (next1
->weight
< next2
->weight
)
143 if (next1
->weight
> next2
->weight
)
146 switch (next1
->type
) {
147 case NEXTHOP_TYPE_IPV4
:
148 case NEXTHOP_TYPE_IPV6
:
149 ret
= _nexthop_gateway_cmp(next1
, next2
);
153 case NEXTHOP_TYPE_IPV4_IFINDEX
:
154 case NEXTHOP_TYPE_IPV6_IFINDEX
:
155 ret
= _nexthop_gateway_cmp(next1
, next2
);
158 /* Intentional Fall-Through */
159 case NEXTHOP_TYPE_IFINDEX
:
160 if (next1
->ifindex
< next2
->ifindex
)
163 if (next1
->ifindex
> next2
->ifindex
)
166 case NEXTHOP_TYPE_BLACKHOLE
:
167 if (next1
->bh_type
< next2
->bh_type
)
170 if (next1
->bh_type
> next2
->bh_type
)
175 if (next1
->srte_color
< next2
->srte_color
)
177 if (next1
->srte_color
> next2
->srte_color
)
180 ret
= _nexthop_source_cmp(next1
, next2
);
184 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
185 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
188 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
189 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
192 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
193 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
196 if (next1
->backup_num
== 0 && next2
->backup_num
== 0)
199 if (next1
->backup_num
< next2
->backup_num
)
202 if (next1
->backup_num
> next2
->backup_num
)
205 ret
= memcmp(next1
->backup_idx
,
206 next2
->backup_idx
, next1
->backup_num
);
212 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
216 ret
= _nexthop_cmp_no_labels(next1
, next2
);
220 ret
= _nexthop_labels_cmp(next1
, next2
);
224 ret
= _nexthop_srv6_cmp(next1
, next2
);
230 * More-limited comparison function used to detect duplicate
231 * nexthops. This is used in places where we don't need the full
232 * comparison of 'nexthop_cmp()'.
234 int nexthop_cmp_basic(const struct nexthop
*nh1
,
235 const struct nexthop
*nh2
)
238 const struct mpls_label_stack
*nhl1
= NULL
;
239 const struct mpls_label_stack
*nhl2
= NULL
;
241 if (nh1
== NULL
&& nh2
== NULL
)
250 if (nh1
->vrf_id
< nh2
->vrf_id
)
253 if (nh1
->vrf_id
> nh2
->vrf_id
)
256 if (nh1
->type
< nh2
->type
)
259 if (nh1
->type
> nh2
->type
)
262 if (nh1
->weight
< nh2
->weight
)
265 if (nh1
->weight
> nh2
->weight
)
269 case NEXTHOP_TYPE_IPV4
:
270 case NEXTHOP_TYPE_IPV6
:
271 ret
= nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
275 case NEXTHOP_TYPE_IPV4_IFINDEX
:
276 case NEXTHOP_TYPE_IPV6_IFINDEX
:
277 ret
= nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
280 /* Intentional Fall-Through */
281 case NEXTHOP_TYPE_IFINDEX
:
282 if (nh1
->ifindex
< nh2
->ifindex
)
285 if (nh1
->ifindex
> nh2
->ifindex
)
288 case NEXTHOP_TYPE_BLACKHOLE
:
289 if (nh1
->bh_type
< nh2
->bh_type
)
292 if (nh1
->bh_type
> nh2
->bh_type
)
297 /* Compare source addr */
298 ret
= nexthop_g_addr_cmp(nh1
->type
, &nh1
->src
, &nh2
->src
);
302 nhl1
= nh1
->nh_label
;
303 nhl2
= nh2
->nh_label
;
305 /* No labels is a match */
315 if (nhl1
->num_labels
> nhl2
->num_labels
)
318 if (nhl1
->num_labels
< nhl2
->num_labels
)
321 ret
= memcmp(nhl1
->label
, nhl2
->label
,
322 (nhl1
->num_labels
* sizeof(mpls_label_t
)));
329 * nexthop_type_to_str
331 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
333 static const char *const desc
[] = {
334 "none", "Directly connected",
335 "IPv4 nexthop", "IPv4 nexthop with ifindex",
336 "IPv6 nexthop", "IPv6 nexthop with ifindex",
340 return desc
[nh_type
];
344 * Check if the labels match for the 2 nexthops specified.
346 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
348 if (_nexthop_labels_cmp(nh1
, nh2
) != 0)
354 struct nexthop
*nexthop_new(void)
358 nh
= XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
361 * Default the weight to 1 here for all nexthops.
362 * The linux kernel does some weird stuff with adding +1 to
363 * all nexthop weights it gets over netlink.
364 * To handle this, just default everything to 1 right from
365 * from the beginning so we don't have to special case
366 * default weights in the linux netlink code.
368 * 1 should be a valid on all platforms anyway.
376 void nexthop_free(struct nexthop
*nexthop
)
378 nexthop_del_labels(nexthop
);
379 nexthop_del_srv6_seg6local(nexthop
);
380 nexthop_del_srv6_seg6(nexthop
);
381 if (nexthop
->resolved
)
382 nexthops_free(nexthop
->resolved
);
383 XFREE(MTYPE_NEXTHOP
, nexthop
);
386 /* Frees a list of nexthops */
387 void nexthops_free(struct nexthop
*nexthop
)
389 struct nexthop
*nh
, *next
;
391 for (nh
= nexthop
; nh
; nh
= next
) {
397 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
408 if (nexthop_cmp(nh1
, nh2
) != 0)
414 bool nexthop_same_no_labels(const struct nexthop
*nh1
,
415 const struct nexthop
*nh2
)
426 if (_nexthop_cmp_no_labels(nh1
, nh2
) != 0)
433 * Allocate a new nexthop object and initialize it from various args.
435 struct nexthop
*nexthop_from_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
)
437 struct nexthop
*nexthop
;
439 nexthop
= nexthop_new();
440 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
441 nexthop
->ifindex
= ifindex
;
442 nexthop
->vrf_id
= vrf_id
;
447 struct nexthop
*nexthop_from_ipv4(const struct in_addr
*ipv4
,
448 const struct in_addr
*src
,
451 struct nexthop
*nexthop
;
453 nexthop
= nexthop_new();
454 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
455 nexthop
->vrf_id
= vrf_id
;
456 nexthop
->gate
.ipv4
= *ipv4
;
458 nexthop
->src
.ipv4
= *src
;
463 struct nexthop
*nexthop_from_ipv4_ifindex(const struct in_addr
*ipv4
,
464 const struct in_addr
*src
,
465 ifindex_t ifindex
, vrf_id_t vrf_id
)
467 struct nexthop
*nexthop
;
469 nexthop
= nexthop_new();
470 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
471 nexthop
->vrf_id
= vrf_id
;
472 nexthop
->gate
.ipv4
= *ipv4
;
474 nexthop
->src
.ipv4
= *src
;
475 nexthop
->ifindex
= ifindex
;
480 struct nexthop
*nexthop_from_ipv6(const struct in6_addr
*ipv6
,
483 struct nexthop
*nexthop
;
485 nexthop
= nexthop_new();
486 nexthop
->vrf_id
= vrf_id
;
487 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
488 nexthop
->gate
.ipv6
= *ipv6
;
493 struct nexthop
*nexthop_from_ipv6_ifindex(const struct in6_addr
*ipv6
,
494 ifindex_t ifindex
, vrf_id_t vrf_id
)
496 struct nexthop
*nexthop
;
498 nexthop
= nexthop_new();
499 nexthop
->vrf_id
= vrf_id
;
500 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
501 nexthop
->gate
.ipv6
= *ipv6
;
502 nexthop
->ifindex
= ifindex
;
507 struct nexthop
*nexthop_from_blackhole(enum blackhole_type bh_type
,
510 struct nexthop
*nexthop
;
512 nexthop
= nexthop_new();
513 nexthop
->vrf_id
= nh_vrf_id
;
514 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
515 nexthop
->bh_type
= bh_type
;
520 /* Update nexthop with label information. */
521 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t ltype
,
522 uint8_t num_labels
, const mpls_label_t
*labels
)
524 struct mpls_label_stack
*nh_label
;
530 /* Enforce limit on label stack size */
531 if (num_labels
> MPLS_MAX_LABELS
)
532 num_labels
= MPLS_MAX_LABELS
;
534 nexthop
->nh_label_type
= ltype
;
536 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
537 sizeof(struct mpls_label_stack
)
538 + num_labels
* sizeof(mpls_label_t
));
539 nh_label
->num_labels
= num_labels
;
540 for (i
= 0; i
< num_labels
; i
++)
541 nh_label
->label
[i
] = *(labels
+ i
);
542 nexthop
->nh_label
= nh_label
;
545 /* Free label information of nexthop, if present. */
546 void nexthop_del_labels(struct nexthop
*nexthop
)
548 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
549 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
552 void nexthop_add_srv6_seg6local(struct nexthop
*nexthop
, uint32_t action
,
553 const struct seg6local_context
*ctx
)
555 if (action
== ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
558 if (!nexthop
->nh_srv6
)
559 nexthop
->nh_srv6
= XCALLOC(MTYPE_NH_SRV6
,
560 sizeof(struct nexthop_srv6
));
562 nexthop
->nh_srv6
->seg6local_action
= action
;
563 nexthop
->nh_srv6
->seg6local_ctx
= *ctx
;
566 void nexthop_del_srv6_seg6local(struct nexthop
*nexthop
)
568 if (!nexthop
->nh_srv6
)
571 nexthop
->nh_srv6
->seg6local_action
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
573 if (sid_zero(&nexthop
->nh_srv6
->seg6_segs
))
574 XFREE(MTYPE_NH_SRV6
, nexthop
->nh_srv6
);
577 void nexthop_add_srv6_seg6(struct nexthop
*nexthop
,
578 const struct in6_addr
*segs
)
583 if (!nexthop
->nh_srv6
)
584 nexthop
->nh_srv6
= XCALLOC(MTYPE_NH_SRV6
,
585 sizeof(struct nexthop_srv6
));
587 nexthop
->nh_srv6
->seg6_segs
= *segs
;
590 void nexthop_del_srv6_seg6(struct nexthop
*nexthop
)
592 if (!nexthop
->nh_srv6
)
595 memset(&nexthop
->nh_srv6
->seg6_segs
, 0,
596 sizeof(nexthop
->nh_srv6
->seg6_segs
));
598 if (nexthop
->nh_srv6
->seg6local_action
==
599 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
600 XFREE(MTYPE_NH_SRV6
, nexthop
->nh_srv6
);
603 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
605 switch (nexthop
->type
) {
606 case NEXTHOP_TYPE_IFINDEX
:
607 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
609 case NEXTHOP_TYPE_IPV4
:
610 case NEXTHOP_TYPE_IPV4_IFINDEX
:
611 snprintfrr(str
, size
, "%pI4 if %u", &nexthop
->gate
.ipv4
,
614 case NEXTHOP_TYPE_IPV6
:
615 case NEXTHOP_TYPE_IPV6_IFINDEX
:
616 snprintfrr(str
, size
, "%pI6 if %u", &nexthop
->gate
.ipv6
,
619 case NEXTHOP_TYPE_BLACKHOLE
:
620 snprintf(str
, size
, "blackhole");
628 * Iteration step for ALL_NEXTHOPS macro:
629 * This is the tricky part. Check if `nexthop' has
630 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
631 * at least one nexthop attached to `nexthop->resolved', which will be
634 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
635 * current chain. In case its current chain end is reached, it will move
636 * upwards in the recursion levels and progress there. Whenever a step
637 * forward in a chain is done, recursion will be checked again.
638 * In a nustshell, it's equivalent to a pre-traversal order assuming that
639 * left branch is 'resolved' and right branch is 'next':
640 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
642 struct nexthop
*nexthop_next(const struct nexthop
*nexthop
)
644 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
645 return nexthop
->resolved
;
648 return nexthop
->next
;
650 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
657 /* Return the next nexthop in the tree that is resolved and active */
658 struct nexthop
*nexthop_next_active_resolved(const struct nexthop
*nexthop
)
660 struct nexthop
*next
= nexthop_next(nexthop
);
663 && (CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_RECURSIVE
)
664 || !CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_ACTIVE
)))
665 next
= nexthop_next(next
);
670 unsigned int nexthop_level(const struct nexthop
*nexthop
)
674 for (const struct nexthop
*par
= nexthop
->rparent
;
675 par
; par
= par
->rparent
)
681 /* Only hash word-sized things, let cmp do the rest. */
682 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
)
684 uint32_t key
= 0x45afe398;
687 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
688 nexthop
->nh_label_type
, key
);
690 if (nexthop
->nh_label
) {
691 int labels
= nexthop
->nh_label
->num_labels
;
695 while (labels
>= 3) {
696 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
697 nexthop
->nh_label
->label
[i
+ 1],
698 nexthop
->nh_label
->label
[i
+ 2],
705 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
706 nexthop
->nh_label
->label
[i
+ 1],
713 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
716 key
= jhash_2words(nexthop
->ifindex
,
717 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
),
720 /* Include backup nexthops, if present */
721 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
722 int backups
= nexthop
->backup_num
;
726 while (backups
>= 3) {
727 key
= jhash_3words(nexthop
->backup_idx
[i
],
728 nexthop
->backup_idx
[i
+ 1],
729 nexthop
->backup_idx
[i
+ 2], key
);
734 while (backups
>= 2) {
735 key
= jhash_2words(nexthop
->backup_idx
[i
],
736 nexthop
->backup_idx
[i
+ 1], key
);
742 key
= jhash_1word(nexthop
->backup_idx
[i
], key
);
745 if (nexthop
->nh_srv6
) {
746 key
= jhash_1word(nexthop
->nh_srv6
->seg6local_action
, key
);
747 key
= jhash(&nexthop
->nh_srv6
->seg6local_ctx
,
748 sizeof(nexthop
->nh_srv6
->seg6local_ctx
), key
);
749 key
= jhash(&nexthop
->nh_srv6
->seg6_segs
,
750 sizeof(nexthop
->nh_srv6
->seg6_segs
), key
);
757 #define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
759 /* For a more granular hash */
760 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
762 uint32_t gate_src_rmap_raw
[GATE_SIZE
* 3] = {};
763 /* Get all the quick stuff */
764 uint32_t key
= nexthop_hash_quick(nexthop
);
766 assert(((sizeof(nexthop
->gate
) + sizeof(nexthop
->src
)
767 + sizeof(nexthop
->rmap_src
))
769 == (GATE_SIZE
* sizeof(uint32_t)));
771 memcpy(gate_src_rmap_raw
, &nexthop
->gate
, GATE_SIZE
);
772 memcpy(gate_src_rmap_raw
+ GATE_SIZE
, &nexthop
->src
, GATE_SIZE
);
773 memcpy(gate_src_rmap_raw
+ (2 * GATE_SIZE
), &nexthop
->rmap_src
,
776 key
= jhash2(gate_src_rmap_raw
, (GATE_SIZE
* 3), key
);
781 void nexthop_copy_no_recurse(struct nexthop
*copy
,
782 const struct nexthop
*nexthop
,
783 struct nexthop
*rparent
)
785 copy
->vrf_id
= nexthop
->vrf_id
;
786 copy
->ifindex
= nexthop
->ifindex
;
787 copy
->type
= nexthop
->type
;
788 copy
->flags
= nexthop
->flags
;
789 copy
->weight
= nexthop
->weight
;
791 assert(nexthop
->backup_num
< NEXTHOP_MAX_BACKUPS
);
792 copy
->backup_num
= nexthop
->backup_num
;
793 if (copy
->backup_num
> 0)
794 memcpy(copy
->backup_idx
, nexthop
->backup_idx
, copy
->backup_num
);
796 copy
->srte_color
= nexthop
->srte_color
;
797 memcpy(©
->gate
, &nexthop
->gate
, sizeof(nexthop
->gate
));
798 memcpy(©
->src
, &nexthop
->src
, sizeof(nexthop
->src
));
799 memcpy(©
->rmap_src
, &nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
));
800 copy
->rparent
= rparent
;
801 if (nexthop
->nh_label
)
802 nexthop_add_labels(copy
, nexthop
->nh_label_type
,
803 nexthop
->nh_label
->num_labels
,
804 &nexthop
->nh_label
->label
[0]);
806 if (nexthop
->nh_srv6
) {
807 if (nexthop
->nh_srv6
->seg6local_action
!=
808 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
809 nexthop_add_srv6_seg6local(copy
,
810 nexthop
->nh_srv6
->seg6local_action
,
811 &nexthop
->nh_srv6
->seg6local_ctx
);
812 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
))
813 nexthop_add_srv6_seg6(copy
,
814 &nexthop
->nh_srv6
->seg6_segs
);
818 void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
819 struct nexthop
*rparent
)
821 nexthop_copy_no_recurse(copy
, nexthop
, rparent
);
823 /* Bit of a special case here, we need to handle the case
824 * of a nexthop resolving to a group. Hence, we need to
825 * use a nexthop_group API.
827 if (CHECK_FLAG(copy
->flags
, NEXTHOP_FLAG_RECURSIVE
))
828 copy_nexthops(©
->resolved
, nexthop
->resolved
, copy
);
831 struct nexthop
*nexthop_dup_no_recurse(const struct nexthop
*nexthop
,
832 struct nexthop
*rparent
)
834 struct nexthop
*new = nexthop_new();
836 nexthop_copy_no_recurse(new, nexthop
, rparent
);
840 struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
841 struct nexthop
*rparent
)
843 struct nexthop
*new = nexthop_new();
845 nexthop_copy(new, nexthop
, rparent
);
850 * Parse one or more backup index values, as comma-separated numbers,
851 * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS
852 * in size. Mails back the number of values converted, and returns 0 on
853 * success, <0 if an error in parsing.
855 int nexthop_str2backups(const char *str
, int *num_backups
,
858 char *ostr
; /* copy of string (start) */
859 char *lstr
; /* working copy of string */
860 char *nump
; /* pointer to next segment */
861 char *endp
; /* end pointer */
863 uint8_t tmp
[NEXTHOP_MAX_BACKUPS
];
866 /* Copy incoming string; the parse is destructive */
867 lstr
= ostr
= XSTRDUP(MTYPE_TMP
, str
);
871 for (i
= 0; i
< NEXTHOP_MAX_BACKUPS
&& lstr
; i
++) {
872 nump
= strsep(&lstr
, ",");
873 lval
= strtoul(nump
, &endp
, 10);
887 /* Limit to one octet */
897 if (ret
== 0 && i
== NEXTHOP_MAX_BACKUPS
&& lstr
)
902 memcpy(backups
, tmp
, i
);
905 XFREE(MTYPE_TMP
, ostr
);
910 ssize_t
printfrr_nhs(struct fbuf
*buf
, const struct nexthop
*nexthop
)
915 return bputs(buf
, "(null)");
917 switch (nexthop
->type
) {
918 case NEXTHOP_TYPE_IFINDEX
:
919 ret
+= bprintfrr(buf
, "if %u", nexthop
->ifindex
);
921 case NEXTHOP_TYPE_IPV4
:
922 case NEXTHOP_TYPE_IPV4_IFINDEX
:
923 ret
+= bprintfrr(buf
, "%pI4 if %u", &nexthop
->gate
.ipv4
,
926 case NEXTHOP_TYPE_IPV6
:
927 case NEXTHOP_TYPE_IPV6_IFINDEX
:
928 ret
+= bprintfrr(buf
, "%pI6 if %u", &nexthop
->gate
.ipv6
,
931 case NEXTHOP_TYPE_BLACKHOLE
:
932 ret
+= bputs(buf
, "blackhole");
939 * nexthop printing variants:
943 * is directly connected, eth0
944 * unreachable (blackhole)
948 * directly connected, eth0
949 * unreachable (blackhole)
954 * (0-length if no IP address present)
957 * (0-length if no interface present)
959 printfrr_ext_autoreg_p("NH", printfrr_nh
);
960 static ssize_t
printfrr_nh(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
963 const struct nexthop
*nexthop
= ptr
;
965 const char *v_is
= "", *v_via
= "", *v_viaif
= "via ";
971 if (*ea
->fmt
== 'v') {
979 return bputs(buf
, "(null)");
981 switch (nexthop
->type
) {
982 case NEXTHOP_TYPE_IPV4
:
983 case NEXTHOP_TYPE_IPV4_IFINDEX
:
984 ret
+= bprintfrr(buf
, "%s%pI4", v_via
,
985 &nexthop
->gate
.ipv4
);
988 case NEXTHOP_TYPE_IPV6
:
989 case NEXTHOP_TYPE_IPV6_IFINDEX
:
990 ret
+= bprintfrr(buf
, "%s%pI6", v_via
,
991 &nexthop
->gate
.ipv6
);
994 case NEXTHOP_TYPE_IFINDEX
:
995 ret
+= bprintfrr(buf
, "%sdirectly connected, %s", v_is
,
996 ifindex2ifname(nexthop
->ifindex
,
999 case NEXTHOP_TYPE_BLACKHOLE
:
1000 ret
+= bputs(buf
, "unreachable");
1002 switch (nexthop
->bh_type
) {
1003 case BLACKHOLE_REJECT
:
1004 ret
+= bputs(buf
, " (ICMP unreachable)");
1006 case BLACKHOLE_ADMINPROHIB
:
1007 ret
+= bputs(buf
, " (ICMP admin-prohibited)");
1009 case BLACKHOLE_NULL
:
1010 ret
+= bputs(buf
, " (blackhole)");
1012 case BLACKHOLE_UNSPEC
:
1017 if (do_ifi
&& nexthop
->ifindex
)
1018 ret
+= bprintfrr(buf
, ", %s%s", v_viaif
,
1019 ifindex2ifname(nexthop
->ifindex
,
1026 ret
+= printfrr_nhs(buf
, nexthop
);
1030 if (*ea
->fmt
== 'g') {
1033 return bputs(buf
, "(null)");
1034 switch (nexthop
->type
) {
1035 case NEXTHOP_TYPE_IPV4
:
1036 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1037 ret
+= bprintfrr(buf
, "%pI4",
1038 &nexthop
->gate
.ipv4
);
1040 case NEXTHOP_TYPE_IPV6
:
1041 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1042 ret
+= bprintfrr(buf
, "%pI6",
1043 &nexthop
->gate
.ipv6
);
1045 case NEXTHOP_TYPE_IFINDEX
:
1046 case NEXTHOP_TYPE_BLACKHOLE
:
1049 } else if (*ea
->fmt
== 'i') {
1052 return bputs(buf
, "(null)");
1053 switch (nexthop
->type
) {
1054 case NEXTHOP_TYPE_IFINDEX
:
1057 ifindex2ifname(nexthop
->ifindex
,
1060 case NEXTHOP_TYPE_IPV4
:
1061 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1062 case NEXTHOP_TYPE_IPV6
:
1063 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1064 if (nexthop
->ifindex
)
1071 case NEXTHOP_TYPE_BLACKHOLE
: