1 /* A generic nexthop structure
2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of Quagga.
6 * Quagga 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 * Quagga 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
27 #include "sockunion.h"
35 #include "nexthop_group.h"
37 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop");
38 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label");
39 DEFINE_MTYPE_STATIC(LIB
, NH_SRV6
, "Nexthop srv6");
41 static int _nexthop_labels_cmp(const struct nexthop
*nh1
,
42 const struct nexthop
*nh2
)
44 const struct mpls_label_stack
*nhl1
= NULL
;
45 const struct mpls_label_stack
*nhl2
= NULL
;
50 /* No labels is a match */
60 if (nhl1
->num_labels
> nhl2
->num_labels
)
63 if (nhl1
->num_labels
< nhl2
->num_labels
)
66 return memcmp(nhl1
->label
, nhl2
->label
,
67 (nhl1
->num_labels
* sizeof(mpls_label_t
)));
70 static int _nexthop_srv6_cmp(const struct nexthop
*nh1
,
71 const struct nexthop
*nh2
)
75 if (!nh1
->nh_srv6
&& !nh2
->nh_srv6
)
78 if (nh1
->nh_srv6
&& !nh2
->nh_srv6
)
81 if (!nh1
->nh_srv6
&& nh2
->nh_srv6
)
84 if (nh1
->nh_srv6
->seg6local_action
> nh2
->nh_srv6
->seg6local_action
)
87 if (nh2
->nh_srv6
->seg6local_action
< nh1
->nh_srv6
->seg6local_action
)
90 ret
= memcmp(&nh1
->nh_srv6
->seg6local_ctx
,
91 &nh2
->nh_srv6
->seg6local_ctx
,
92 sizeof(struct seg6local_context
));
96 ret
= memcmp(&nh1
->nh_srv6
->seg6_segs
,
97 &nh2
->nh_srv6
->seg6_segs
,
98 sizeof(struct in6_addr
));
103 int nexthop_g_addr_cmp(enum nexthop_types_t type
, const union g_addr
*addr1
,
104 const union g_addr
*addr2
)
109 case NEXTHOP_TYPE_IPV4
:
110 case NEXTHOP_TYPE_IPV4_IFINDEX
:
111 ret
= IPV4_ADDR_CMP(&addr1
->ipv4
, &addr2
->ipv4
);
113 case NEXTHOP_TYPE_IPV6
:
114 case NEXTHOP_TYPE_IPV6_IFINDEX
:
115 ret
= IPV6_ADDR_CMP(&addr1
->ipv6
, &addr2
->ipv6
);
117 case NEXTHOP_TYPE_IFINDEX
:
118 case NEXTHOP_TYPE_BLACKHOLE
:
126 static int _nexthop_gateway_cmp(const struct nexthop
*nh1
,
127 const struct nexthop
*nh2
)
129 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
132 static int _nexthop_source_cmp(const struct nexthop
*nh1
,
133 const struct nexthop
*nh2
)
135 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->src
, &nh2
->src
);
138 static int _nexthop_cmp_no_labels(const struct nexthop
*next1
,
139 const struct nexthop
*next2
)
143 if (next1
->vrf_id
< next2
->vrf_id
)
146 if (next1
->vrf_id
> next2
->vrf_id
)
149 if (next1
->type
< next2
->type
)
152 if (next1
->type
> next2
->type
)
155 if (next1
->weight
< next2
->weight
)
158 if (next1
->weight
> next2
->weight
)
161 switch (next1
->type
) {
162 case NEXTHOP_TYPE_IPV4
:
163 case NEXTHOP_TYPE_IPV6
:
164 ret
= _nexthop_gateway_cmp(next1
, next2
);
168 case NEXTHOP_TYPE_IPV4_IFINDEX
:
169 case NEXTHOP_TYPE_IPV6_IFINDEX
:
170 ret
= _nexthop_gateway_cmp(next1
, next2
);
173 /* Intentional Fall-Through */
174 case NEXTHOP_TYPE_IFINDEX
:
175 if (next1
->ifindex
< next2
->ifindex
)
178 if (next1
->ifindex
> next2
->ifindex
)
181 case NEXTHOP_TYPE_BLACKHOLE
:
182 if (next1
->bh_type
< next2
->bh_type
)
185 if (next1
->bh_type
> next2
->bh_type
)
190 if (next1
->srte_color
< next2
->srte_color
)
192 if (next1
->srte_color
> next2
->srte_color
)
195 ret
= _nexthop_source_cmp(next1
, next2
);
199 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
200 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
203 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
204 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
207 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
208 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
211 if (next1
->backup_num
== 0 && next2
->backup_num
== 0)
214 if (next1
->backup_num
< next2
->backup_num
)
217 if (next1
->backup_num
> next2
->backup_num
)
220 ret
= memcmp(next1
->backup_idx
,
221 next2
->backup_idx
, next1
->backup_num
);
227 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
231 ret
= _nexthop_cmp_no_labels(next1
, next2
);
235 ret
= _nexthop_labels_cmp(next1
, next2
);
239 ret
= _nexthop_srv6_cmp(next1
, next2
);
245 * More-limited comparison function used to detect duplicate
246 * nexthops. This is used in places where we don't need the full
247 * comparison of 'nexthop_cmp()'.
249 int nexthop_cmp_basic(const struct nexthop
*nh1
,
250 const struct nexthop
*nh2
)
253 const struct mpls_label_stack
*nhl1
= NULL
;
254 const struct mpls_label_stack
*nhl2
= NULL
;
256 if (nh1
== NULL
&& nh2
== NULL
)
265 if (nh1
->vrf_id
< nh2
->vrf_id
)
268 if (nh1
->vrf_id
> nh2
->vrf_id
)
271 if (nh1
->type
< nh2
->type
)
274 if (nh1
->type
> nh2
->type
)
277 if (nh1
->weight
< nh2
->weight
)
280 if (nh1
->weight
> nh2
->weight
)
284 case NEXTHOP_TYPE_IPV4
:
285 case NEXTHOP_TYPE_IPV6
:
286 ret
= nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
290 case NEXTHOP_TYPE_IPV4_IFINDEX
:
291 case NEXTHOP_TYPE_IPV6_IFINDEX
:
292 ret
= nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
295 /* Intentional Fall-Through */
296 case NEXTHOP_TYPE_IFINDEX
:
297 if (nh1
->ifindex
< nh2
->ifindex
)
300 if (nh1
->ifindex
> nh2
->ifindex
)
303 case NEXTHOP_TYPE_BLACKHOLE
:
304 if (nh1
->bh_type
< nh2
->bh_type
)
307 if (nh1
->bh_type
> nh2
->bh_type
)
312 /* Compare source addr */
313 ret
= nexthop_g_addr_cmp(nh1
->type
, &nh1
->src
, &nh2
->src
);
317 nhl1
= nh1
->nh_label
;
318 nhl2
= nh2
->nh_label
;
320 /* No labels is a match */
330 if (nhl1
->num_labels
> nhl2
->num_labels
)
333 if (nhl1
->num_labels
< nhl2
->num_labels
)
336 ret
= memcmp(nhl1
->label
, nhl2
->label
,
337 (nhl1
->num_labels
* sizeof(mpls_label_t
)));
344 * nexthop_type_to_str
346 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
348 static const char *const desc
[] = {
349 "none", "Directly connected",
350 "IPv4 nexthop", "IPv4 nexthop with ifindex",
351 "IPv6 nexthop", "IPv6 nexthop with ifindex",
355 return desc
[nh_type
];
359 * Check if the labels match for the 2 nexthops specified.
361 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
363 if (_nexthop_labels_cmp(nh1
, nh2
) != 0)
369 struct nexthop
*nexthop_new(void)
373 nh
= XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
376 * Default the weight to 1 here for all nexthops.
377 * The linux kernel does some weird stuff with adding +1 to
378 * all nexthop weights it gets over netlink.
379 * To handle this, just default everything to 1 right from
380 * from the beginning so we don't have to special case
381 * default weights in the linux netlink code.
383 * 1 should be a valid on all platforms anyway.
391 void nexthop_free(struct nexthop
*nexthop
)
393 nexthop_del_labels(nexthop
);
394 nexthop_del_srv6_seg6local(nexthop
);
395 nexthop_del_srv6_seg6(nexthop
);
396 if (nexthop
->resolved
)
397 nexthops_free(nexthop
->resolved
);
398 XFREE(MTYPE_NEXTHOP
, nexthop
);
401 /* Frees a list of nexthops */
402 void nexthops_free(struct nexthop
*nexthop
)
404 struct nexthop
*nh
, *next
;
406 for (nh
= nexthop
; nh
; nh
= next
) {
412 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
423 if (nexthop_cmp(nh1
, nh2
) != 0)
429 bool nexthop_same_no_labels(const struct nexthop
*nh1
,
430 const struct nexthop
*nh2
)
441 if (_nexthop_cmp_no_labels(nh1
, nh2
) != 0)
448 * Allocate a new nexthop object and initialize it from various args.
450 struct nexthop
*nexthop_from_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
)
452 struct nexthop
*nexthop
;
454 nexthop
= nexthop_new();
455 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
456 nexthop
->ifindex
= ifindex
;
457 nexthop
->vrf_id
= vrf_id
;
462 struct nexthop
*nexthop_from_ipv4(const struct in_addr
*ipv4
,
463 const struct in_addr
*src
,
466 struct nexthop
*nexthop
;
468 nexthop
= nexthop_new();
469 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
470 nexthop
->vrf_id
= vrf_id
;
471 nexthop
->gate
.ipv4
= *ipv4
;
473 nexthop
->src
.ipv4
= *src
;
478 struct nexthop
*nexthop_from_ipv4_ifindex(const struct in_addr
*ipv4
,
479 const struct in_addr
*src
,
480 ifindex_t ifindex
, vrf_id_t vrf_id
)
482 struct nexthop
*nexthop
;
484 nexthop
= nexthop_new();
485 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
486 nexthop
->vrf_id
= vrf_id
;
487 nexthop
->gate
.ipv4
= *ipv4
;
489 nexthop
->src
.ipv4
= *src
;
490 nexthop
->ifindex
= ifindex
;
495 struct nexthop
*nexthop_from_ipv6(const struct in6_addr
*ipv6
,
498 struct nexthop
*nexthop
;
500 nexthop
= nexthop_new();
501 nexthop
->vrf_id
= vrf_id
;
502 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
503 nexthop
->gate
.ipv6
= *ipv6
;
508 struct nexthop
*nexthop_from_ipv6_ifindex(const struct in6_addr
*ipv6
,
509 ifindex_t ifindex
, vrf_id_t vrf_id
)
511 struct nexthop
*nexthop
;
513 nexthop
= nexthop_new();
514 nexthop
->vrf_id
= vrf_id
;
515 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
516 nexthop
->gate
.ipv6
= *ipv6
;
517 nexthop
->ifindex
= ifindex
;
522 struct nexthop
*nexthop_from_blackhole(enum blackhole_type bh_type
,
525 struct nexthop
*nexthop
;
527 nexthop
= nexthop_new();
528 nexthop
->vrf_id
= nh_vrf_id
;
529 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
530 nexthop
->bh_type
= bh_type
;
535 /* Update nexthop with label information. */
536 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t ltype
,
537 uint8_t num_labels
, const mpls_label_t
*labels
)
539 struct mpls_label_stack
*nh_label
;
545 /* Enforce limit on label stack size */
546 if (num_labels
> MPLS_MAX_LABELS
)
547 num_labels
= MPLS_MAX_LABELS
;
549 nexthop
->nh_label_type
= ltype
;
551 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
552 sizeof(struct mpls_label_stack
)
553 + num_labels
* sizeof(mpls_label_t
));
554 nh_label
->num_labels
= num_labels
;
555 for (i
= 0; i
< num_labels
; i
++)
556 nh_label
->label
[i
] = *(labels
+ i
);
557 nexthop
->nh_label
= nh_label
;
560 /* Free label information of nexthop, if present. */
561 void nexthop_del_labels(struct nexthop
*nexthop
)
563 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
564 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
567 void nexthop_add_srv6_seg6local(struct nexthop
*nexthop
, uint32_t action
,
568 const struct seg6local_context
*ctx
)
570 if (action
== ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
573 if (!nexthop
->nh_srv6
)
574 nexthop
->nh_srv6
= XCALLOC(MTYPE_NH_SRV6
,
575 sizeof(struct nexthop_srv6
));
577 nexthop
->nh_srv6
->seg6local_action
= action
;
578 nexthop
->nh_srv6
->seg6local_ctx
= *ctx
;
581 void nexthop_del_srv6_seg6local(struct nexthop
*nexthop
)
583 if (!nexthop
->nh_srv6
)
586 nexthop
->nh_srv6
->seg6local_action
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
588 if (sid_zero(&nexthop
->nh_srv6
->seg6_segs
))
589 XFREE(MTYPE_NH_SRV6
, nexthop
->nh_srv6
);
592 void nexthop_add_srv6_seg6(struct nexthop
*nexthop
,
593 const struct in6_addr
*segs
)
598 if (!nexthop
->nh_srv6
)
599 nexthop
->nh_srv6
= XCALLOC(MTYPE_NH_SRV6
,
600 sizeof(struct nexthop_srv6
));
602 nexthop
->nh_srv6
->seg6_segs
= *segs
;
605 void nexthop_del_srv6_seg6(struct nexthop
*nexthop
)
607 if (!nexthop
->nh_srv6
)
610 memset(&nexthop
->nh_srv6
->seg6_segs
, 0,
611 sizeof(nexthop
->nh_srv6
->seg6_segs
));
613 if (nexthop
->nh_srv6
->seg6local_action
==
614 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
615 XFREE(MTYPE_NH_SRV6
, nexthop
->nh_srv6
);
618 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
620 switch (nexthop
->type
) {
621 case NEXTHOP_TYPE_IFINDEX
:
622 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
624 case NEXTHOP_TYPE_IPV4
:
625 case NEXTHOP_TYPE_IPV4_IFINDEX
:
626 snprintfrr(str
, size
, "%pI4 if %u", &nexthop
->gate
.ipv4
,
629 case NEXTHOP_TYPE_IPV6
:
630 case NEXTHOP_TYPE_IPV6_IFINDEX
:
631 snprintfrr(str
, size
, "%pI6 if %u", &nexthop
->gate
.ipv6
,
634 case NEXTHOP_TYPE_BLACKHOLE
:
635 snprintf(str
, size
, "blackhole");
643 * Iteration step for ALL_NEXTHOPS macro:
644 * This is the tricky part. Check if `nexthop' has
645 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
646 * at least one nexthop attached to `nexthop->resolved', which will be
649 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
650 * current chain. In case its current chain end is reached, it will move
651 * upwards in the recursion levels and progress there. Whenever a step
652 * forward in a chain is done, recursion will be checked again.
653 * In a nustshell, it's equivalent to a pre-traversal order assuming that
654 * left branch is 'resolved' and right branch is 'next':
655 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
657 struct nexthop
*nexthop_next(const struct nexthop
*nexthop
)
659 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
660 return nexthop
->resolved
;
663 return nexthop
->next
;
665 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
672 /* Return the next nexthop in the tree that is resolved and active */
673 struct nexthop
*nexthop_next_active_resolved(const struct nexthop
*nexthop
)
675 struct nexthop
*next
= nexthop_next(nexthop
);
678 && (CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_RECURSIVE
)
679 || !CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_ACTIVE
)))
680 next
= nexthop_next(next
);
685 unsigned int nexthop_level(const struct nexthop
*nexthop
)
689 for (const struct nexthop
*par
= nexthop
->rparent
;
690 par
; par
= par
->rparent
)
696 /* Only hash word-sized things, let cmp do the rest. */
697 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
)
699 uint32_t key
= 0x45afe398;
702 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
703 nexthop
->nh_label_type
, key
);
705 if (nexthop
->nh_label
) {
706 int labels
= nexthop
->nh_label
->num_labels
;
710 while (labels
>= 3) {
711 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
712 nexthop
->nh_label
->label
[i
+ 1],
713 nexthop
->nh_label
->label
[i
+ 2],
720 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
721 nexthop
->nh_label
->label
[i
+ 1],
728 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
731 key
= jhash_2words(nexthop
->ifindex
,
732 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
),
735 /* Include backup nexthops, if present */
736 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
737 int backups
= nexthop
->backup_num
;
741 while (backups
>= 3) {
742 key
= jhash_3words(nexthop
->backup_idx
[i
],
743 nexthop
->backup_idx
[i
+ 1],
744 nexthop
->backup_idx
[i
+ 2], key
);
749 while (backups
>= 2) {
750 key
= jhash_2words(nexthop
->backup_idx
[i
],
751 nexthop
->backup_idx
[i
+ 1], key
);
757 key
= jhash_1word(nexthop
->backup_idx
[i
], key
);
760 if (nexthop
->nh_srv6
) {
761 key
= jhash_1word(nexthop
->nh_srv6
->seg6local_action
, key
);
762 key
= jhash(&nexthop
->nh_srv6
->seg6local_ctx
,
763 sizeof(nexthop
->nh_srv6
->seg6local_ctx
), key
);
764 key
= jhash(&nexthop
->nh_srv6
->seg6_segs
,
765 sizeof(nexthop
->nh_srv6
->seg6_segs
), key
);
772 #define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
774 /* For a more granular hash */
775 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
777 uint32_t gate_src_rmap_raw
[GATE_SIZE
* 3] = {};
778 /* Get all the quick stuff */
779 uint32_t key
= nexthop_hash_quick(nexthop
);
781 assert(((sizeof(nexthop
->gate
) + sizeof(nexthop
->src
)
782 + sizeof(nexthop
->rmap_src
))
784 == (GATE_SIZE
* sizeof(uint32_t)));
786 memcpy(gate_src_rmap_raw
, &nexthop
->gate
, GATE_SIZE
);
787 memcpy(gate_src_rmap_raw
+ GATE_SIZE
, &nexthop
->src
, GATE_SIZE
);
788 memcpy(gate_src_rmap_raw
+ (2 * GATE_SIZE
), &nexthop
->rmap_src
,
791 key
= jhash2(gate_src_rmap_raw
, (GATE_SIZE
* 3), key
);
796 void nexthop_copy_no_recurse(struct nexthop
*copy
,
797 const struct nexthop
*nexthop
,
798 struct nexthop
*rparent
)
800 copy
->vrf_id
= nexthop
->vrf_id
;
801 copy
->ifindex
= nexthop
->ifindex
;
802 copy
->type
= nexthop
->type
;
803 copy
->flags
= nexthop
->flags
;
804 copy
->weight
= nexthop
->weight
;
806 assert(nexthop
->backup_num
< NEXTHOP_MAX_BACKUPS
);
807 copy
->backup_num
= nexthop
->backup_num
;
808 if (copy
->backup_num
> 0)
809 memcpy(copy
->backup_idx
, nexthop
->backup_idx
, copy
->backup_num
);
811 copy
->srte_color
= nexthop
->srte_color
;
812 memcpy(©
->gate
, &nexthop
->gate
, sizeof(nexthop
->gate
));
813 memcpy(©
->src
, &nexthop
->src
, sizeof(nexthop
->src
));
814 memcpy(©
->rmap_src
, &nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
));
815 copy
->rparent
= rparent
;
816 if (nexthop
->nh_label
)
817 nexthop_add_labels(copy
, nexthop
->nh_label_type
,
818 nexthop
->nh_label
->num_labels
,
819 &nexthop
->nh_label
->label
[0]);
821 if (nexthop
->nh_srv6
) {
822 if (nexthop
->nh_srv6
->seg6local_action
!=
823 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
824 nexthop_add_srv6_seg6local(copy
,
825 nexthop
->nh_srv6
->seg6local_action
,
826 &nexthop
->nh_srv6
->seg6local_ctx
);
827 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
))
828 nexthop_add_srv6_seg6(copy
,
829 &nexthop
->nh_srv6
->seg6_segs
);
833 void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
834 struct nexthop
*rparent
)
836 nexthop_copy_no_recurse(copy
, nexthop
, rparent
);
838 /* Bit of a special case here, we need to handle the case
839 * of a nexthop resolving to a group. Hence, we need to
840 * use a nexthop_group API.
842 if (CHECK_FLAG(copy
->flags
, NEXTHOP_FLAG_RECURSIVE
))
843 copy_nexthops(©
->resolved
, nexthop
->resolved
, copy
);
846 struct nexthop
*nexthop_dup_no_recurse(const struct nexthop
*nexthop
,
847 struct nexthop
*rparent
)
849 struct nexthop
*new = nexthop_new();
851 nexthop_copy_no_recurse(new, nexthop
, rparent
);
855 struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
856 struct nexthop
*rparent
)
858 struct nexthop
*new = nexthop_new();
860 nexthop_copy(new, nexthop
, rparent
);
865 * Parse one or more backup index values, as comma-separated numbers,
866 * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS
867 * in size. Mails back the number of values converted, and returns 0 on
868 * success, <0 if an error in parsing.
870 int nexthop_str2backups(const char *str
, int *num_backups
,
873 char *ostr
; /* copy of string (start) */
874 char *lstr
; /* working copy of string */
875 char *nump
; /* pointer to next segment */
876 char *endp
; /* end pointer */
878 uint8_t tmp
[NEXTHOP_MAX_BACKUPS
];
881 /* Copy incoming string; the parse is destructive */
882 lstr
= ostr
= XSTRDUP(MTYPE_TMP
, str
);
886 for (i
= 0; i
< NEXTHOP_MAX_BACKUPS
&& lstr
; i
++) {
887 nump
= strsep(&lstr
, ",");
888 lval
= strtoul(nump
, &endp
, 10);
902 /* Limit to one octet */
912 if (ret
== 0 && i
== NEXTHOP_MAX_BACKUPS
&& lstr
)
917 memcpy(backups
, tmp
, i
);
920 XFREE(MTYPE_TMP
, ostr
);
926 * nexthop printing variants:
930 * is directly connected, eth0
931 * unreachable (blackhole)
935 * directly connected, eth0
936 * unreachable (blackhole)
941 * (0-length if no IP address present)
944 * (0-length if no interface present)
946 printfrr_ext_autoreg_p("NH", printfrr_nh
);
947 static ssize_t
printfrr_nh(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
950 const struct nexthop
*nexthop
= ptr
;
952 const char *v_is
= "", *v_via
= "", *v_viaif
= "via ";
958 if (*ea
->fmt
== 'v') {
966 return bputs(buf
, "(null)");
968 switch (nexthop
->type
) {
969 case NEXTHOP_TYPE_IPV4
:
970 case NEXTHOP_TYPE_IPV4_IFINDEX
:
971 ret
+= bprintfrr(buf
, "%s%pI4", v_via
,
972 &nexthop
->gate
.ipv4
);
975 case NEXTHOP_TYPE_IPV6
:
976 case NEXTHOP_TYPE_IPV6_IFINDEX
:
977 ret
+= bprintfrr(buf
, "%s%pI6", v_via
,
978 &nexthop
->gate
.ipv6
);
981 case NEXTHOP_TYPE_IFINDEX
:
982 ret
+= bprintfrr(buf
, "%sdirectly connected, %s", v_is
,
983 ifindex2ifname(nexthop
->ifindex
,
986 case NEXTHOP_TYPE_BLACKHOLE
:
987 ret
+= bputs(buf
, "unreachable");
989 switch (nexthop
->bh_type
) {
990 case BLACKHOLE_REJECT
:
991 ret
+= bputs(buf
, " (ICMP unreachable)");
993 case BLACKHOLE_ADMINPROHIB
:
994 ret
+= bputs(buf
, " (ICMP admin-prohibited)");
997 ret
+= bputs(buf
, " (blackhole)");
999 case BLACKHOLE_UNSPEC
:
1004 if (do_ifi
&& nexthop
->ifindex
)
1005 ret
+= bprintfrr(buf
, ", %s%s", v_viaif
,
1006 ifindex2ifname(nexthop
->ifindex
,
1014 return bputs(buf
, "(null)");
1016 switch (nexthop
->type
) {
1017 case NEXTHOP_TYPE_IFINDEX
:
1018 ret
+= bprintfrr(buf
, "if %u", nexthop
->ifindex
);
1020 case NEXTHOP_TYPE_IPV4
:
1021 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1022 ret
+= bprintfrr(buf
, "%pI4 if %u", &nexthop
->gate
.ipv4
,
1025 case NEXTHOP_TYPE_IPV6
:
1026 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1027 ret
+= bprintfrr(buf
, "%pI6 if %u", &nexthop
->gate
.ipv6
,
1030 case NEXTHOP_TYPE_BLACKHOLE
:
1031 ret
+= bputs(buf
, "blackhole");
1037 if (*ea
->fmt
== 'g') {
1040 return bputs(buf
, "(null)");
1041 switch (nexthop
->type
) {
1042 case NEXTHOP_TYPE_IPV4
:
1043 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1044 ret
+= bprintfrr(buf
, "%pI4",
1045 &nexthop
->gate
.ipv4
);
1047 case NEXTHOP_TYPE_IPV6
:
1048 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1049 ret
+= bprintfrr(buf
, "%pI6",
1050 &nexthop
->gate
.ipv6
);
1052 case NEXTHOP_TYPE_IFINDEX
:
1053 case NEXTHOP_TYPE_BLACKHOLE
:
1056 } else if (*ea
->fmt
== 'i') {
1059 return bputs(buf
, "(null)");
1060 switch (nexthop
->type
) {
1061 case NEXTHOP_TYPE_IFINDEX
:
1064 ifindex2ifname(nexthop
->ifindex
,
1067 case NEXTHOP_TYPE_IPV4
:
1068 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1069 case NEXTHOP_TYPE_IPV6
:
1070 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1071 if (nexthop
->ifindex
)
1078 case NEXTHOP_TYPE_BLACKHOLE
: