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
28 #include "sockunion.h"
38 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop")
39 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label")
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
, nhl1
->num_labels
);
69 int nexthop_g_addr_cmp(enum nexthop_types_t type
, const union g_addr
*addr1
,
70 const union g_addr
*addr2
)
75 case NEXTHOP_TYPE_IPV4
:
76 case NEXTHOP_TYPE_IPV4_IFINDEX
:
77 ret
= IPV4_ADDR_CMP(&addr1
->ipv4
, &addr2
->ipv4
);
79 case NEXTHOP_TYPE_IPV6
:
80 case NEXTHOP_TYPE_IPV6_IFINDEX
:
81 ret
= IPV6_ADDR_CMP(&addr1
->ipv6
, &addr2
->ipv6
);
83 case NEXTHOP_TYPE_IFINDEX
:
84 case NEXTHOP_TYPE_BLACKHOLE
:
92 static int _nexthop_gateway_cmp(const struct nexthop
*nh1
,
93 const struct nexthop
*nh2
)
95 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->gate
, &nh2
->gate
);
98 static int _nexthop_source_cmp(const struct nexthop
*nh1
,
99 const struct nexthop
*nh2
)
101 return nexthop_g_addr_cmp(nh1
->type
, &nh1
->src
, &nh2
->src
);
104 static int _nexthop_cmp_no_labels(const struct nexthop
*next1
,
105 const struct nexthop
*next2
)
109 if (next1
->vrf_id
< next2
->vrf_id
)
112 if (next1
->vrf_id
> next2
->vrf_id
)
115 if (next1
->type
< next2
->type
)
118 if (next1
->type
> next2
->type
)
121 switch (next1
->type
) {
122 case NEXTHOP_TYPE_IPV4
:
123 case NEXTHOP_TYPE_IPV6
:
124 ret
= _nexthop_gateway_cmp(next1
, next2
);
128 case NEXTHOP_TYPE_IPV4_IFINDEX
:
129 case NEXTHOP_TYPE_IPV6_IFINDEX
:
130 ret
= _nexthop_gateway_cmp(next1
, next2
);
133 /* Intentional Fall-Through */
134 case NEXTHOP_TYPE_IFINDEX
:
135 if (next1
->ifindex
< next2
->ifindex
)
138 if (next1
->ifindex
> next2
->ifindex
)
141 case NEXTHOP_TYPE_BLACKHOLE
:
142 if (next1
->bh_type
< next2
->bh_type
)
145 if (next1
->bh_type
> next2
->bh_type
)
150 ret
= _nexthop_source_cmp(next1
, next2
);
155 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
159 ret
= _nexthop_cmp_no_labels(next1
, next2
);
163 ret
= _nexthop_labels_cmp(next1
, next2
);
168 int nexthop_same_firsthop(struct nexthop
*next1
, struct nexthop
*next2
)
170 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
171 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
176 case NEXTHOP_TYPE_IPV4_IFINDEX
:
177 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
179 if (next1
->ifindex
!= next2
->ifindex
)
182 case NEXTHOP_TYPE_IFINDEX
:
183 if (next1
->ifindex
!= next2
->ifindex
)
186 case NEXTHOP_TYPE_IPV6_IFINDEX
:
187 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
189 if (next1
->ifindex
!= next2
->ifindex
)
200 * nexthop_type_to_str
202 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
204 static const char *const desc
[] = {
205 "none", "Directly connected",
206 "IPv4 nexthop", "IPv4 nexthop with ifindex",
207 "IPv6 nexthop", "IPv6 nexthop with ifindex",
211 return desc
[nh_type
];
215 * Check if the labels match for the 2 nexthops specified.
217 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
219 if (_nexthop_labels_cmp(nh1
, nh2
) != 0)
225 struct nexthop
*nexthop_new(void)
227 return XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
231 void nexthop_free(struct nexthop
*nexthop
)
233 nexthop_del_labels(nexthop
);
234 if (nexthop
->resolved
)
235 nexthops_free(nexthop
->resolved
);
236 XFREE(MTYPE_NEXTHOP
, nexthop
);
239 /* Frees a list of nexthops */
240 void nexthops_free(struct nexthop
*nexthop
)
242 struct nexthop
*nh
, *next
;
244 for (nh
= nexthop
; nh
; nh
= next
) {
250 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
261 if (nexthop_cmp(nh1
, nh2
) != 0)
267 bool nexthop_same_no_labels(const struct nexthop
*nh1
,
268 const struct nexthop
*nh2
)
279 if (_nexthop_cmp_no_labels(nh1
, nh2
) != 0)
286 * Allocate a new nexthop object and initialize it from various args.
288 struct nexthop
*nexthop_from_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
)
290 struct nexthop
*nexthop
;
292 nexthop
= nexthop_new();
293 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
294 nexthop
->ifindex
= ifindex
;
295 nexthop
->vrf_id
= vrf_id
;
300 struct nexthop
*nexthop_from_ipv4(const struct in_addr
*ipv4
,
301 const struct in_addr
*src
,
304 struct nexthop
*nexthop
;
306 nexthop
= nexthop_new();
307 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
308 nexthop
->vrf_id
= vrf_id
;
309 nexthop
->gate
.ipv4
= *ipv4
;
311 nexthop
->src
.ipv4
= *src
;
316 struct nexthop
*nexthop_from_ipv4_ifindex(const struct in_addr
*ipv4
,
317 const struct in_addr
*src
,
318 ifindex_t ifindex
, vrf_id_t vrf_id
)
320 struct nexthop
*nexthop
;
322 nexthop
= nexthop_new();
323 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
324 nexthop
->vrf_id
= vrf_id
;
325 nexthop
->gate
.ipv4
= *ipv4
;
327 nexthop
->src
.ipv4
= *src
;
328 nexthop
->ifindex
= ifindex
;
333 struct nexthop
*nexthop_from_ipv6(const struct in6_addr
*ipv6
,
336 struct nexthop
*nexthop
;
338 nexthop
= nexthop_new();
339 nexthop
->vrf_id
= vrf_id
;
340 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
341 nexthop
->gate
.ipv6
= *ipv6
;
346 struct nexthop
*nexthop_from_ipv6_ifindex(const struct in6_addr
*ipv6
,
347 ifindex_t ifindex
, vrf_id_t vrf_id
)
349 struct nexthop
*nexthop
;
351 nexthop
= nexthop_new();
352 nexthop
->vrf_id
= vrf_id
;
353 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
354 nexthop
->gate
.ipv6
= *ipv6
;
355 nexthop
->ifindex
= ifindex
;
360 struct nexthop
*nexthop_from_blackhole(enum blackhole_type bh_type
)
362 struct nexthop
*nexthop
;
364 nexthop
= nexthop_new();
365 nexthop
->vrf_id
= VRF_DEFAULT
;
366 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
367 nexthop
->bh_type
= bh_type
;
372 /* Update nexthop with label information. */
373 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
374 uint8_t num_labels
, mpls_label_t
*label
)
376 struct mpls_label_stack
*nh_label
;
382 nexthop
->nh_label_type
= type
;
383 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
384 sizeof(struct mpls_label_stack
)
385 + num_labels
* sizeof(mpls_label_t
));
386 nh_label
->num_labels
= num_labels
;
387 for (i
= 0; i
< num_labels
; i
++)
388 nh_label
->label
[i
] = *(label
+ i
);
389 nexthop
->nh_label
= nh_label
;
392 /* Free label information of nexthop, if present. */
393 void nexthop_del_labels(struct nexthop
*nexthop
)
395 if (nexthop
->nh_label
) {
396 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
397 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
401 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
403 switch (nexthop
->type
) {
404 case NEXTHOP_TYPE_IFINDEX
:
405 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
407 case NEXTHOP_TYPE_IPV4
:
408 case NEXTHOP_TYPE_IPV4_IFINDEX
:
409 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
412 case NEXTHOP_TYPE_IPV6
:
413 case NEXTHOP_TYPE_IPV6_IFINDEX
:
414 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
417 case NEXTHOP_TYPE_BLACKHOLE
:
418 snprintf(str
, size
, "blackhole");
421 snprintf(str
, size
, "unknown");
429 * Iteration step for ALL_NEXTHOPS macro:
430 * This is the tricky part. Check if `nexthop' has
431 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
432 * at least one nexthop attached to `nexthop->resolved', which will be
435 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
436 * current chain. In case its current chain end is reached, it will move
437 * upwards in the recursion levels and progress there. Whenever a step
438 * forward in a chain is done, recursion will be checked again.
439 * In a nustshell, it's equivalent to a pre-traversal order assuming that
440 * left branch is 'resolved' and right branch is 'next':
441 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
443 struct nexthop
*nexthop_next(const struct nexthop
*nexthop
)
445 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
446 return nexthop
->resolved
;
449 return nexthop
->next
;
451 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
458 /* Return the next nexthop in the tree that is resolved and active */
459 struct nexthop
*nexthop_next_active_resolved(const struct nexthop
*nexthop
)
461 struct nexthop
*next
= nexthop_next(nexthop
);
464 && (CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_RECURSIVE
)
465 || !CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_ACTIVE
)))
466 next
= nexthop_next(next
);
471 unsigned int nexthop_level(struct nexthop
*nexthop
)
475 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
481 /* Only hash word-sized things, let cmp do the rest. */
482 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
)
484 uint32_t key
= 0x45afe398;
486 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
487 nexthop
->nh_label_type
, key
);
489 if (nexthop
->nh_label
) {
490 int labels
= nexthop
->nh_label
->num_labels
;
493 while (labels
>= 3) {
494 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
495 nexthop
->nh_label
->label
[i
+ 1],
496 nexthop
->nh_label
->label
[i
+ 2],
503 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
504 nexthop
->nh_label
->label
[i
+ 1],
511 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
514 key
= jhash_2words(nexthop
->ifindex
,
515 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
),
522 #define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
524 /* For a more granular hash */
525 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
527 uint32_t gate_src_rmap_raw
[GATE_SIZE
* 3] = {};
528 /* Get all the quick stuff */
529 uint32_t key
= nexthop_hash_quick(nexthop
);
531 assert(((sizeof(nexthop
->gate
) + sizeof(nexthop
->src
)
532 + sizeof(nexthop
->rmap_src
))
534 == (GATE_SIZE
* sizeof(uint32_t)));
536 memcpy(gate_src_rmap_raw
, &nexthop
->gate
, GATE_SIZE
);
537 memcpy(gate_src_rmap_raw
+ GATE_SIZE
, &nexthop
->src
, GATE_SIZE
);
538 memcpy(gate_src_rmap_raw
+ (2 * GATE_SIZE
), &nexthop
->rmap_src
,
541 key
= jhash2(gate_src_rmap_raw
, (GATE_SIZE
* 3), key
);
546 void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
547 struct nexthop
*rparent
)
549 copy
->vrf_id
= nexthop
->vrf_id
;
550 copy
->ifindex
= nexthop
->ifindex
;
551 copy
->type
= nexthop
->type
;
552 copy
->flags
= nexthop
->flags
;
553 copy
->weight
= nexthop
->weight
;
554 memcpy(©
->gate
, &nexthop
->gate
, sizeof(nexthop
->gate
));
555 memcpy(©
->src
, &nexthop
->src
, sizeof(nexthop
->src
));
556 memcpy(©
->rmap_src
, &nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
));
557 copy
->rparent
= rparent
;
558 if (nexthop
->nh_label
)
559 nexthop_add_labels(copy
, nexthop
->nh_label_type
,
560 nexthop
->nh_label
->num_labels
,
561 &nexthop
->nh_label
->label
[0]);
564 struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
565 struct nexthop
*rparent
)
567 struct nexthop
*new = nexthop_new();
569 nexthop_copy(new, nexthop
, rparent
);
574 * nexthop printing variants:
578 * is directly connected, eth0
579 * unreachable (blackhole)
583 * directly connected, eth0
584 * unreachable (blackhole)
588 printfrr_ext_autoreg_p("NH", printfrr_nh
)
589 static ssize_t
printfrr_nh(char *buf
, size_t bsz
, const char *fmt
,
590 int prec
, const void *ptr
)
592 const struct nexthop
*nexthop
= ptr
;
593 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
595 const char *s
, *v_is
= "", *v_via
= "", *v_viaif
= "via ";
607 switch (nexthop
->type
) {
608 case NEXTHOP_TYPE_IPV4
:
609 case NEXTHOP_TYPE_IPV4_IFINDEX
:
610 bprintfrr(&fb
, "%s%pI4", v_via
, &nexthop
->gate
.ipv4
);
613 case NEXTHOP_TYPE_IPV6
:
614 case NEXTHOP_TYPE_IPV6_IFINDEX
:
615 bprintfrr(&fb
, "%s%pI6", v_via
, &nexthop
->gate
.ipv6
);
618 case NEXTHOP_TYPE_IFINDEX
:
619 bprintfrr(&fb
, "%sdirectly connected, %s", v_is
,
620 ifindex2ifname(nexthop
->ifindex
,
623 case NEXTHOP_TYPE_BLACKHOLE
:
624 switch (nexthop
->bh_type
) {
625 case BLACKHOLE_REJECT
:
626 s
= " (ICMP unreachable)";
628 case BLACKHOLE_ADMINPROHIB
:
629 s
= " (ICMP admin-prohibited)";
638 bprintfrr(&fb
, "unreachable%s", s
);
643 if (do_ifi
&& nexthop
->ifindex
)
644 bprintfrr(&fb
, ", %s%s", v_viaif
, ifindex2ifname(
651 nexthop2str(nexthop
, buf
, bsz
);