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")
40 static int _nexthop_labels_cmp(const struct nexthop
*nh1
,
41 const struct nexthop
*nh2
)
43 const struct mpls_label_stack
*nhl1
= NULL
;
44 const struct mpls_label_stack
*nhl2
= NULL
;
49 /* No labels is a match */
59 if (nhl1
->num_labels
> nhl2
->num_labels
)
62 if (nhl1
->num_labels
< nhl2
->num_labels
)
65 return memcmp(nhl1
->label
, nhl2
->label
, nhl1
->num_labels
);
68 int nexthop_g_addr_cmp(enum nexthop_types_t type
, const union g_addr
*addr1
,
69 const union g_addr
*addr2
)
74 case NEXTHOP_TYPE_IPV4
:
75 case NEXTHOP_TYPE_IPV4_IFINDEX
:
76 ret
= IPV4_ADDR_CMP(&addr1
->ipv4
, &addr2
->ipv4
);
78 case NEXTHOP_TYPE_IPV6
:
79 case NEXTHOP_TYPE_IPV6_IFINDEX
:
80 ret
= IPV6_ADDR_CMP(&addr1
->ipv6
, &addr2
->ipv6
);
82 case NEXTHOP_TYPE_IFINDEX
:
83 case NEXTHOP_TYPE_BLACKHOLE
:
91 static int _nexthop_gateway_cmp(const struct nexthop
*nh1
,
92 const struct nexthop
*nh2
)
94 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
97 static int _nexthop_source_cmp(const struct nexthop
*nh1
,
98 const struct nexthop
*nh2
)
100 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->src
, &nh2
->src
);
103 static int _nexthop_cmp_no_labels(const struct nexthop
*next1
,
104 const struct nexthop
*next2
)
108 if (next1
->vrf_id
< next2
->vrf_id
)
111 if (next1
->vrf_id
> next2
->vrf_id
)
114 if (next1
->type
< next2
->type
)
117 if (next1
->type
> next2
->type
)
120 if (next1
->weight
< next2
->weight
)
123 if (next1
->weight
> next2
->weight
)
126 switch (next1
->type
) {
127 case NEXTHOP_TYPE_IPV4
:
128 case NEXTHOP_TYPE_IPV6
:
129 ret
= _nexthop_gateway_cmp(next1
, next2
);
133 case NEXTHOP_TYPE_IPV4_IFINDEX
:
134 case NEXTHOP_TYPE_IPV6_IFINDEX
:
135 ret
= _nexthop_gateway_cmp(next1
, next2
);
138 /* Intentional Fall-Through */
139 case NEXTHOP_TYPE_IFINDEX
:
140 if (next1
->ifindex
< next2
->ifindex
)
143 if (next1
->ifindex
> next2
->ifindex
)
146 case NEXTHOP_TYPE_BLACKHOLE
:
147 if (next1
->bh_type
< next2
->bh_type
)
150 if (next1
->bh_type
> next2
->bh_type
)
155 if (next1
->srte_color
< next2
->srte_color
)
157 if (next1
->srte_color
> next2
->srte_color
)
160 ret
= _nexthop_source_cmp(next1
, next2
);
164 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
165 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
168 if (!CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
169 CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
172 if (CHECK_FLAG(next1
->flags
, NEXTHOP_FLAG_HAS_BACKUP
) &&
173 !CHECK_FLAG(next2
->flags
, NEXTHOP_FLAG_HAS_BACKUP
))
176 if (next1
->backup_num
== 0 && next2
->backup_num
== 0)
179 if (next1
->backup_num
< next2
->backup_num
)
182 if (next1
->backup_num
> next2
->backup_num
)
185 ret
= memcmp(next1
->backup_idx
,
186 next2
->backup_idx
, next1
->backup_num
);
192 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
196 ret
= _nexthop_cmp_no_labels(next1
, next2
);
200 ret
= _nexthop_labels_cmp(next1
, next2
);
205 bool nexthop_same_firsthop(const struct nexthop
*next1
,
206 const struct nexthop
*next2
)
208 /* Map the TYPE_IPx types to TYPE_IPx_IFINDEX */
209 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
210 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
215 if (next1
->vrf_id
!= next2
->vrf_id
)
219 case NEXTHOP_TYPE_IPV4_IFINDEX
:
220 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
222 if (next1
->ifindex
!= next2
->ifindex
)
225 case NEXTHOP_TYPE_IFINDEX
:
226 if (next1
->ifindex
!= next2
->ifindex
)
229 case NEXTHOP_TYPE_IPV6_IFINDEX
:
230 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
232 if (next1
->ifindex
!= next2
->ifindex
)
243 * nexthop_type_to_str
245 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
247 static const char *const desc
[] = {
248 "none", "Directly connected",
249 "IPv4 nexthop", "IPv4 nexthop with ifindex",
250 "IPv6 nexthop", "IPv6 nexthop with ifindex",
254 return desc
[nh_type
];
258 * Check if the labels match for the 2 nexthops specified.
260 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
262 if (_nexthop_labels_cmp(nh1
, nh2
) != 0)
268 struct nexthop
*nexthop_new(void)
272 nh
= XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
275 * Default the weight to 1 here for all nexthops.
276 * The linux kernel does some weird stuff with adding +1 to
277 * all nexthop weights it gets over netlink.
278 * To handle this, just default everything to 1 right from
279 * from the beginning so we don't have to special case
280 * default weights in the linux netlink code.
282 * 1 should be a valid on all platforms anyway.
290 void nexthop_free(struct nexthop
*nexthop
)
292 nexthop_del_labels(nexthop
);
293 if (nexthop
->resolved
)
294 nexthops_free(nexthop
->resolved
);
295 XFREE(MTYPE_NEXTHOP
, nexthop
);
298 /* Frees a list of nexthops */
299 void nexthops_free(struct nexthop
*nexthop
)
301 struct nexthop
*nh
, *next
;
303 for (nh
= nexthop
; nh
; nh
= next
) {
309 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
320 if (nexthop_cmp(nh1
, nh2
) != 0)
326 bool nexthop_same_no_labels(const struct nexthop
*nh1
,
327 const struct nexthop
*nh2
)
338 if (_nexthop_cmp_no_labels(nh1
, nh2
) != 0)
345 * Allocate a new nexthop object and initialize it from various args.
347 struct nexthop
*nexthop_from_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
)
349 struct nexthop
*nexthop
;
351 nexthop
= nexthop_new();
352 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
353 nexthop
->ifindex
= ifindex
;
354 nexthop
->vrf_id
= vrf_id
;
359 struct nexthop
*nexthop_from_ipv4(const struct in_addr
*ipv4
,
360 const struct in_addr
*src
,
363 struct nexthop
*nexthop
;
365 nexthop
= nexthop_new();
366 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
367 nexthop
->vrf_id
= vrf_id
;
368 nexthop
->gate
.ipv4
= *ipv4
;
370 nexthop
->src
.ipv4
= *src
;
375 struct nexthop
*nexthop_from_ipv4_ifindex(const struct in_addr
*ipv4
,
376 const struct in_addr
*src
,
377 ifindex_t ifindex
, vrf_id_t vrf_id
)
379 struct nexthop
*nexthop
;
381 nexthop
= nexthop_new();
382 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
383 nexthop
->vrf_id
= vrf_id
;
384 nexthop
->gate
.ipv4
= *ipv4
;
386 nexthop
->src
.ipv4
= *src
;
387 nexthop
->ifindex
= ifindex
;
392 struct nexthop
*nexthop_from_ipv6(const struct in6_addr
*ipv6
,
395 struct nexthop
*nexthop
;
397 nexthop
= nexthop_new();
398 nexthop
->vrf_id
= vrf_id
;
399 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
400 nexthop
->gate
.ipv6
= *ipv6
;
405 struct nexthop
*nexthop_from_ipv6_ifindex(const struct in6_addr
*ipv6
,
406 ifindex_t ifindex
, vrf_id_t vrf_id
)
408 struct nexthop
*nexthop
;
410 nexthop
= nexthop_new();
411 nexthop
->vrf_id
= vrf_id
;
412 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
413 nexthop
->gate
.ipv6
= *ipv6
;
414 nexthop
->ifindex
= ifindex
;
419 struct nexthop
*nexthop_from_blackhole(enum blackhole_type bh_type
)
421 struct nexthop
*nexthop
;
423 nexthop
= nexthop_new();
424 nexthop
->vrf_id
= VRF_DEFAULT
;
425 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
426 nexthop
->bh_type
= bh_type
;
431 /* Update nexthop with label information. */
432 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t ltype
,
433 uint8_t num_labels
, const mpls_label_t
*labels
)
435 struct mpls_label_stack
*nh_label
;
441 /* Enforce limit on label stack size */
442 if (num_labels
> MPLS_MAX_LABELS
)
443 num_labels
= MPLS_MAX_LABELS
;
445 nexthop
->nh_label_type
= ltype
;
447 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
448 sizeof(struct mpls_label_stack
)
449 + num_labels
* sizeof(mpls_label_t
));
450 nh_label
->num_labels
= num_labels
;
451 for (i
= 0; i
< num_labels
; i
++)
452 nh_label
->label
[i
] = *(labels
+ i
);
453 nexthop
->nh_label
= nh_label
;
456 /* Free label information of nexthop, if present. */
457 void nexthop_del_labels(struct nexthop
*nexthop
)
459 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
460 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
463 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
465 switch (nexthop
->type
) {
466 case NEXTHOP_TYPE_IFINDEX
:
467 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
469 case NEXTHOP_TYPE_IPV4
:
470 case NEXTHOP_TYPE_IPV4_IFINDEX
:
471 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
474 case NEXTHOP_TYPE_IPV6
:
475 case NEXTHOP_TYPE_IPV6_IFINDEX
:
476 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
479 case NEXTHOP_TYPE_BLACKHOLE
:
480 snprintf(str
, size
, "blackhole");
483 snprintf(str
, size
, "unknown");
491 * Iteration step for ALL_NEXTHOPS macro:
492 * This is the tricky part. Check if `nexthop' has
493 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
494 * at least one nexthop attached to `nexthop->resolved', which will be
497 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
498 * current chain. In case its current chain end is reached, it will move
499 * upwards in the recursion levels and progress there. Whenever a step
500 * forward in a chain is done, recursion will be checked again.
501 * In a nustshell, it's equivalent to a pre-traversal order assuming that
502 * left branch is 'resolved' and right branch is 'next':
503 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
505 struct nexthop
*nexthop_next(const struct nexthop
*nexthop
)
507 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
508 return nexthop
->resolved
;
511 return nexthop
->next
;
513 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
520 /* Return the next nexthop in the tree that is resolved and active */
521 struct nexthop
*nexthop_next_active_resolved(const struct nexthop
*nexthop
)
523 struct nexthop
*next
= nexthop_next(nexthop
);
526 && (CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_RECURSIVE
)
527 || !CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_ACTIVE
)))
528 next
= nexthop_next(next
);
533 unsigned int nexthop_level(const struct nexthop
*nexthop
)
537 for (const struct nexthop
*par
= nexthop
->rparent
;
538 par
; par
= par
->rparent
)
544 /* Only hash word-sized things, let cmp do the rest. */
545 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
)
547 uint32_t key
= 0x45afe398;
550 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
551 nexthop
->nh_label_type
, key
);
553 if (nexthop
->nh_label
) {
554 int labels
= nexthop
->nh_label
->num_labels
;
558 while (labels
>= 3) {
559 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
560 nexthop
->nh_label
->label
[i
+ 1],
561 nexthop
->nh_label
->label
[i
+ 2],
568 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
569 nexthop
->nh_label
->label
[i
+ 1],
576 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
579 key
= jhash_2words(nexthop
->ifindex
,
580 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
),
583 /* Include backup nexthops, if present */
584 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
585 int backups
= nexthop
->backup_num
;
589 while (backups
>= 3) {
590 key
= jhash_3words(nexthop
->backup_idx
[i
],
591 nexthop
->backup_idx
[i
+ 1],
592 nexthop
->backup_idx
[i
+ 2], key
);
597 while (backups
>= 2) {
598 key
= jhash_2words(nexthop
->backup_idx
[i
],
599 nexthop
->backup_idx
[i
+ 1], key
);
605 key
= jhash_1word(nexthop
->backup_idx
[i
], key
);
612 #define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
614 /* For a more granular hash */
615 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
617 uint32_t gate_src_rmap_raw
[GATE_SIZE
* 3] = {};
618 /* Get all the quick stuff */
619 uint32_t key
= nexthop_hash_quick(nexthop
);
621 assert(((sizeof(nexthop
->gate
) + sizeof(nexthop
->src
)
622 + sizeof(nexthop
->rmap_src
))
624 == (GATE_SIZE
* sizeof(uint32_t)));
626 memcpy(gate_src_rmap_raw
, &nexthop
->gate
, GATE_SIZE
);
627 memcpy(gate_src_rmap_raw
+ GATE_SIZE
, &nexthop
->src
, GATE_SIZE
);
628 memcpy(gate_src_rmap_raw
+ (2 * GATE_SIZE
), &nexthop
->rmap_src
,
631 key
= jhash2(gate_src_rmap_raw
, (GATE_SIZE
* 3), key
);
636 void nexthop_copy_no_recurse(struct nexthop
*copy
,
637 const struct nexthop
*nexthop
,
638 struct nexthop
*rparent
)
640 copy
->vrf_id
= nexthop
->vrf_id
;
641 copy
->ifindex
= nexthop
->ifindex
;
642 copy
->type
= nexthop
->type
;
643 copy
->flags
= nexthop
->flags
;
644 copy
->weight
= nexthop
->weight
;
646 assert(nexthop
->backup_num
< NEXTHOP_MAX_BACKUPS
);
647 copy
->backup_num
= nexthop
->backup_num
;
648 if (copy
->backup_num
> 0)
649 memcpy(copy
->backup_idx
, nexthop
->backup_idx
, copy
->backup_num
);
651 copy
->srte_color
= nexthop
->srte_color
;
652 memcpy(©
->gate
, &nexthop
->gate
, sizeof(nexthop
->gate
));
653 memcpy(©
->src
, &nexthop
->src
, sizeof(nexthop
->src
));
654 memcpy(©
->rmap_src
, &nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
));
655 copy
->rparent
= rparent
;
656 if (nexthop
->nh_label
)
657 nexthop_add_labels(copy
, nexthop
->nh_label_type
,
658 nexthop
->nh_label
->num_labels
,
659 &nexthop
->nh_label
->label
[0]);
662 void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
663 struct nexthop
*rparent
)
665 nexthop_copy_no_recurse(copy
, nexthop
, rparent
);
667 /* Bit of a special case here, we need to handle the case
668 * of a nexthop resolving to a group. Hence, we need to
669 * use a nexthop_group API.
671 if (CHECK_FLAG(copy
->flags
, NEXTHOP_FLAG_RECURSIVE
))
672 copy_nexthops(©
->resolved
, nexthop
->resolved
, copy
);
675 struct nexthop
*nexthop_dup_no_recurse(const struct nexthop
*nexthop
,
676 struct nexthop
*rparent
)
678 struct nexthop
*new = nexthop_new();
680 nexthop_copy_no_recurse(new, nexthop
, rparent
);
684 struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
685 struct nexthop
*rparent
)
687 struct nexthop
*new = nexthop_new();
689 nexthop_copy(new, nexthop
, rparent
);
694 * Parse one or more backup index values, as comma-separated numbers,
695 * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS
696 * in size. Mails back the number of values converted, and returns 0 on
697 * success, <0 if an error in parsing.
699 int nexthop_str2backups(const char *str
, int *num_backups
,
702 char *ostr
; /* copy of string (start) */
703 char *lstr
; /* working copy of string */
704 char *nump
; /* pointer to next segment */
705 char *endp
; /* end pointer */
707 uint8_t tmp
[NEXTHOP_MAX_BACKUPS
];
710 /* Copy incoming string; the parse is destructive */
711 lstr
= ostr
= XSTRDUP(MTYPE_TMP
, str
);
715 for (i
= 0; i
< NEXTHOP_MAX_BACKUPS
&& lstr
; i
++) {
716 nump
= strsep(&lstr
, ",");
717 lval
= strtoul(nump
, &endp
, 10);
731 /* Limit to one octet */
741 if (ret
== 0 && i
== NEXTHOP_MAX_BACKUPS
&& lstr
)
746 memcpy(backups
, tmp
, i
);
749 XFREE(MTYPE_TMP
, ostr
);
755 * nexthop printing variants:
759 * is directly connected, eth0
760 * unreachable (blackhole)
764 * directly connected, eth0
765 * unreachable (blackhole)
769 printfrr_ext_autoreg_p("NH", printfrr_nh
)
770 static ssize_t
printfrr_nh(char *buf
, size_t bsz
, const char *fmt
,
771 int prec
, const void *ptr
)
773 const struct nexthop
*nexthop
= ptr
;
774 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
776 const char *s
, *v_is
= "", *v_via
= "", *v_viaif
= "via ";
788 switch (nexthop
->type
) {
789 case NEXTHOP_TYPE_IPV4
:
790 case NEXTHOP_TYPE_IPV4_IFINDEX
:
791 bprintfrr(&fb
, "%s%pI4", v_via
, &nexthop
->gate
.ipv4
);
794 case NEXTHOP_TYPE_IPV6
:
795 case NEXTHOP_TYPE_IPV6_IFINDEX
:
796 bprintfrr(&fb
, "%s%pI6", v_via
, &nexthop
->gate
.ipv6
);
799 case NEXTHOP_TYPE_IFINDEX
:
800 bprintfrr(&fb
, "%sdirectly connected, %s", v_is
,
801 ifindex2ifname(nexthop
->ifindex
,
804 case NEXTHOP_TYPE_BLACKHOLE
:
805 switch (nexthop
->bh_type
) {
806 case BLACKHOLE_REJECT
:
807 s
= " (ICMP unreachable)";
809 case BLACKHOLE_ADMINPROHIB
:
810 s
= " (ICMP admin-prohibited)";
819 bprintfrr(&fb
, "unreachable%s", s
);
824 if (do_ifi
&& nexthop
->ifindex
)
825 bprintfrr(&fb
, ", %s%s", v_viaif
, ifindex2ifname(
832 nexthop2str(nexthop
, buf
, bsz
);