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 if (next1
->weight
< next2
->weight
)
124 if (next1
->weight
> next2
->weight
)
127 switch (next1
->type
) {
128 case NEXTHOP_TYPE_IPV4
:
129 case NEXTHOP_TYPE_IPV6
:
130 ret
= _nexthop_gateway_cmp(next1
, next2
);
134 case NEXTHOP_TYPE_IPV4_IFINDEX
:
135 case NEXTHOP_TYPE_IPV6_IFINDEX
:
136 ret
= _nexthop_gateway_cmp(next1
, next2
);
139 /* Intentional Fall-Through */
140 case NEXTHOP_TYPE_IFINDEX
:
141 if (next1
->ifindex
< next2
->ifindex
)
144 if (next1
->ifindex
> next2
->ifindex
)
147 case NEXTHOP_TYPE_BLACKHOLE
:
148 if (next1
->bh_type
< next2
->bh_type
)
151 if (next1
->bh_type
> next2
->bh_type
)
156 ret
= _nexthop_source_cmp(next1
, next2
);
161 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
165 ret
= _nexthop_cmp_no_labels(next1
, next2
);
169 ret
= _nexthop_labels_cmp(next1
, next2
);
174 int nexthop_same_firsthop(struct nexthop
*next1
, struct nexthop
*next2
)
176 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
177 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
182 case NEXTHOP_TYPE_IPV4_IFINDEX
:
183 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
185 if (next1
->ifindex
!= next2
->ifindex
)
188 case NEXTHOP_TYPE_IFINDEX
:
189 if (next1
->ifindex
!= next2
->ifindex
)
192 case NEXTHOP_TYPE_IPV6_IFINDEX
:
193 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
195 if (next1
->ifindex
!= next2
->ifindex
)
206 * nexthop_type_to_str
208 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
210 static const char *const desc
[] = {
211 "none", "Directly connected",
212 "IPv4 nexthop", "IPv4 nexthop with ifindex",
213 "IPv6 nexthop", "IPv6 nexthop with ifindex",
217 return desc
[nh_type
];
221 * Check if the labels match for the 2 nexthops specified.
223 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
225 if (_nexthop_labels_cmp(nh1
, nh2
) != 0)
231 struct nexthop
*nexthop_new(void)
235 nh
= XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
238 * Default the weight to 1 here for all nexthops.
239 * The linux kernel does some weird stuff with adding +1 to
240 * all nexthop weights it gets over netlink.
241 * To handle this, just default everything to 1 right from
242 * from the beggining so we don't have to special case
243 * default weights in the linux netlink code.
245 * 1 should be a valid on all platforms anyway.
253 void nexthop_free(struct nexthop
*nexthop
)
255 nexthop_del_labels(nexthop
);
256 if (nexthop
->resolved
)
257 nexthops_free(nexthop
->resolved
);
258 XFREE(MTYPE_NEXTHOP
, nexthop
);
261 /* Frees a list of nexthops */
262 void nexthops_free(struct nexthop
*nexthop
)
264 struct nexthop
*nh
, *next
;
266 for (nh
= nexthop
; nh
; nh
= next
) {
272 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
283 if (nexthop_cmp(nh1
, nh2
) != 0)
289 bool nexthop_same_no_labels(const struct nexthop
*nh1
,
290 const struct nexthop
*nh2
)
301 if (_nexthop_cmp_no_labels(nh1
, nh2
) != 0)
308 * Allocate a new nexthop object and initialize it from various args.
310 struct nexthop
*nexthop_from_ifindex(ifindex_t ifindex
, vrf_id_t vrf_id
)
312 struct nexthop
*nexthop
;
314 nexthop
= nexthop_new();
315 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
316 nexthop
->ifindex
= ifindex
;
317 nexthop
->vrf_id
= vrf_id
;
322 struct nexthop
*nexthop_from_ipv4(const struct in_addr
*ipv4
,
323 const struct in_addr
*src
,
326 struct nexthop
*nexthop
;
328 nexthop
= nexthop_new();
329 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
330 nexthop
->vrf_id
= vrf_id
;
331 nexthop
->gate
.ipv4
= *ipv4
;
333 nexthop
->src
.ipv4
= *src
;
338 struct nexthop
*nexthop_from_ipv4_ifindex(const struct in_addr
*ipv4
,
339 const struct in_addr
*src
,
340 ifindex_t ifindex
, vrf_id_t vrf_id
)
342 struct nexthop
*nexthop
;
344 nexthop
= nexthop_new();
345 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
346 nexthop
->vrf_id
= vrf_id
;
347 nexthop
->gate
.ipv4
= *ipv4
;
349 nexthop
->src
.ipv4
= *src
;
350 nexthop
->ifindex
= ifindex
;
355 struct nexthop
*nexthop_from_ipv6(const struct in6_addr
*ipv6
,
358 struct nexthop
*nexthop
;
360 nexthop
= nexthop_new();
361 nexthop
->vrf_id
= vrf_id
;
362 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
363 nexthop
->gate
.ipv6
= *ipv6
;
368 struct nexthop
*nexthop_from_ipv6_ifindex(const struct in6_addr
*ipv6
,
369 ifindex_t ifindex
, vrf_id_t vrf_id
)
371 struct nexthop
*nexthop
;
373 nexthop
= nexthop_new();
374 nexthop
->vrf_id
= vrf_id
;
375 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
376 nexthop
->gate
.ipv6
= *ipv6
;
377 nexthop
->ifindex
= ifindex
;
382 struct nexthop
*nexthop_from_blackhole(enum blackhole_type bh_type
)
384 struct nexthop
*nexthop
;
386 nexthop
= nexthop_new();
387 nexthop
->vrf_id
= VRF_DEFAULT
;
388 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
389 nexthop
->bh_type
= bh_type
;
394 /* Update nexthop with label information. */
395 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
396 uint8_t num_labels
, mpls_label_t
*label
)
398 struct mpls_label_stack
*nh_label
;
404 nexthop
->nh_label_type
= type
;
405 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
406 sizeof(struct mpls_label_stack
)
407 + num_labels
* sizeof(mpls_label_t
));
408 nh_label
->num_labels
= num_labels
;
409 for (i
= 0; i
< num_labels
; i
++)
410 nh_label
->label
[i
] = *(label
+ i
);
411 nexthop
->nh_label
= nh_label
;
414 /* Free label information of nexthop, if present. */
415 void nexthop_del_labels(struct nexthop
*nexthop
)
417 if (nexthop
->nh_label
) {
418 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
419 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
423 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
425 switch (nexthop
->type
) {
426 case NEXTHOP_TYPE_IFINDEX
:
427 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
429 case NEXTHOP_TYPE_IPV4
:
430 case NEXTHOP_TYPE_IPV4_IFINDEX
:
431 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
434 case NEXTHOP_TYPE_IPV6
:
435 case NEXTHOP_TYPE_IPV6_IFINDEX
:
436 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
439 case NEXTHOP_TYPE_BLACKHOLE
:
440 snprintf(str
, size
, "blackhole");
443 snprintf(str
, size
, "unknown");
451 * Iteration step for ALL_NEXTHOPS macro:
452 * This is the tricky part. Check if `nexthop' has
453 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
454 * at least one nexthop attached to `nexthop->resolved', which will be
457 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
458 * current chain. In case its current chain end is reached, it will move
459 * upwards in the recursion levels and progress there. Whenever a step
460 * forward in a chain is done, recursion will be checked again.
461 * In a nustshell, it's equivalent to a pre-traversal order assuming that
462 * left branch is 'resolved' and right branch is 'next':
463 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
465 struct nexthop
*nexthop_next(const struct nexthop
*nexthop
)
467 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
468 return nexthop
->resolved
;
471 return nexthop
->next
;
473 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
480 /* Return the next nexthop in the tree that is resolved and active */
481 struct nexthop
*nexthop_next_active_resolved(const struct nexthop
*nexthop
)
483 struct nexthop
*next
= nexthop_next(nexthop
);
486 && (CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_RECURSIVE
)
487 || !CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_ACTIVE
)))
488 next
= nexthop_next(next
);
493 unsigned int nexthop_level(struct nexthop
*nexthop
)
497 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
503 /* Only hash word-sized things, let cmp do the rest. */
504 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
)
506 uint32_t key
= 0x45afe398;
508 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
509 nexthop
->nh_label_type
, key
);
511 if (nexthop
->nh_label
) {
512 int labels
= nexthop
->nh_label
->num_labels
;
515 while (labels
>= 3) {
516 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
517 nexthop
->nh_label
->label
[i
+ 1],
518 nexthop
->nh_label
->label
[i
+ 2],
525 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
526 nexthop
->nh_label
->label
[i
+ 1],
533 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
536 key
= jhash_2words(nexthop
->ifindex
,
537 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
),
544 #define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
546 /* For a more granular hash */
547 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
549 uint32_t gate_src_rmap_raw
[GATE_SIZE
* 3] = {};
550 /* Get all the quick stuff */
551 uint32_t key
= nexthop_hash_quick(nexthop
);
553 assert(((sizeof(nexthop
->gate
) + sizeof(nexthop
->src
)
554 + sizeof(nexthop
->rmap_src
))
556 == (GATE_SIZE
* sizeof(uint32_t)));
558 memcpy(gate_src_rmap_raw
, &nexthop
->gate
, GATE_SIZE
);
559 memcpy(gate_src_rmap_raw
+ GATE_SIZE
, &nexthop
->src
, GATE_SIZE
);
560 memcpy(gate_src_rmap_raw
+ (2 * GATE_SIZE
), &nexthop
->rmap_src
,
563 key
= jhash2(gate_src_rmap_raw
, (GATE_SIZE
* 3), key
);
568 void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
569 struct nexthop
*rparent
)
571 copy
->vrf_id
= nexthop
->vrf_id
;
572 copy
->ifindex
= nexthop
->ifindex
;
573 copy
->type
= nexthop
->type
;
574 copy
->flags
= nexthop
->flags
;
575 copy
->weight
= nexthop
->weight
;
576 memcpy(©
->gate
, &nexthop
->gate
, sizeof(nexthop
->gate
));
577 memcpy(©
->src
, &nexthop
->src
, sizeof(nexthop
->src
));
578 memcpy(©
->rmap_src
, &nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
));
579 copy
->rparent
= rparent
;
580 if (nexthop
->nh_label
)
581 nexthop_add_labels(copy
, nexthop
->nh_label_type
,
582 nexthop
->nh_label
->num_labels
,
583 &nexthop
->nh_label
->label
[0]);
586 struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
587 struct nexthop
*rparent
)
589 struct nexthop
*new = nexthop_new();
591 nexthop_copy(new, nexthop
, rparent
);
596 * nexthop printing variants:
600 * is directly connected, eth0
601 * unreachable (blackhole)
605 * directly connected, eth0
606 * unreachable (blackhole)
610 printfrr_ext_autoreg_p("NH", printfrr_nh
)
611 static ssize_t
printfrr_nh(char *buf
, size_t bsz
, const char *fmt
,
612 int prec
, const void *ptr
)
614 const struct nexthop
*nexthop
= ptr
;
615 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
617 const char *s
, *v_is
= "", *v_via
= "", *v_viaif
= "via ";
629 switch (nexthop
->type
) {
630 case NEXTHOP_TYPE_IPV4
:
631 case NEXTHOP_TYPE_IPV4_IFINDEX
:
632 bprintfrr(&fb
, "%s%pI4", v_via
, &nexthop
->gate
.ipv4
);
635 case NEXTHOP_TYPE_IPV6
:
636 case NEXTHOP_TYPE_IPV6_IFINDEX
:
637 bprintfrr(&fb
, "%s%pI6", v_via
, &nexthop
->gate
.ipv6
);
640 case NEXTHOP_TYPE_IFINDEX
:
641 bprintfrr(&fb
, "%sdirectly connected, %s", v_is
,
642 ifindex2ifname(nexthop
->ifindex
,
645 case NEXTHOP_TYPE_BLACKHOLE
:
646 switch (nexthop
->bh_type
) {
647 case BLACKHOLE_REJECT
:
648 s
= " (ICMP unreachable)";
650 case BLACKHOLE_ADMINPROHIB
:
651 s
= " (ICMP admin-prohibited)";
660 bprintfrr(&fb
, "unreachable%s", s
);
665 if (do_ifi
&& nexthop
->ifindex
)
666 bprintfrr(&fb
, ", %s%s", v_viaif
, ifindex2ifname(
673 nexthop2str(nexthop
, buf
, bsz
);