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"
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 switch (next1
->type
) {
121 case NEXTHOP_TYPE_IPV4
:
122 case NEXTHOP_TYPE_IPV6
:
123 ret
= _nexthop_gateway_cmp(next1
, next2
);
127 case NEXTHOP_TYPE_IPV4_IFINDEX
:
128 case NEXTHOP_TYPE_IPV6_IFINDEX
:
129 ret
= _nexthop_gateway_cmp(next1
, next2
);
132 /* Intentional Fall-Through */
133 case NEXTHOP_TYPE_IFINDEX
:
134 if (next1
->ifindex
< next2
->ifindex
)
137 if (next1
->ifindex
> next2
->ifindex
)
140 case NEXTHOP_TYPE_BLACKHOLE
:
141 if (next1
->bh_type
< next2
->bh_type
)
144 if (next1
->bh_type
> next2
->bh_type
)
149 ret
= _nexthop_source_cmp(next1
, next2
);
154 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
158 ret
= _nexthop_cmp_no_labels(next1
, next2
);
162 ret
= _nexthop_labels_cmp(next1
, next2
);
167 int nexthop_same_firsthop(struct nexthop
*next1
, struct nexthop
*next2
)
169 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
170 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
175 case NEXTHOP_TYPE_IPV4_IFINDEX
:
176 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
178 if (next1
->ifindex
!= next2
->ifindex
)
181 case NEXTHOP_TYPE_IFINDEX
:
182 if (next1
->ifindex
!= next2
->ifindex
)
185 case NEXTHOP_TYPE_IPV6_IFINDEX
:
186 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
188 if (next1
->ifindex
!= next2
->ifindex
)
199 * nexthop_type_to_str
201 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
203 static const char *desc
[] = {
204 "none", "Directly connected",
205 "IPv4 nexthop", "IPv4 nexthop with ifindex",
206 "IPv6 nexthop", "IPv6 nexthop with ifindex",
210 return desc
[nh_type
];
214 * Check if the labels match for the 2 nexthops specified.
216 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
218 if (_nexthop_labels_cmp(nh1
, nh2
) != 0)
224 struct nexthop
*nexthop_new(void)
226 return XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
230 void nexthop_free(struct nexthop
*nexthop
)
232 nexthop_del_labels(nexthop
);
233 if (nexthop
->resolved
)
234 nexthops_free(nexthop
->resolved
);
235 XFREE(MTYPE_NEXTHOP
, nexthop
);
238 /* Frees a list of nexthops */
239 void nexthops_free(struct nexthop
*nexthop
)
241 struct nexthop
*nh
, *next
;
243 for (nh
= nexthop
; nh
; nh
= next
) {
249 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
260 if (nexthop_cmp(nh1
, nh2
) != 0)
266 bool nexthop_same_no_labels(const struct nexthop
*nh1
,
267 const struct nexthop
*nh2
)
278 if (_nexthop_cmp_no_labels(nh1
, nh2
) != 0)
284 /* Update nexthop with label information. */
285 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
286 uint8_t num_labels
, mpls_label_t
*label
)
288 struct mpls_label_stack
*nh_label
;
291 nexthop
->nh_label_type
= type
;
292 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
293 sizeof(struct mpls_label_stack
)
294 + num_labels
* sizeof(mpls_label_t
));
295 nh_label
->num_labels
= num_labels
;
296 for (i
= 0; i
< num_labels
; i
++)
297 nh_label
->label
[i
] = *(label
+ i
);
298 nexthop
->nh_label
= nh_label
;
301 /* Free label information of nexthop, if present. */
302 void nexthop_del_labels(struct nexthop
*nexthop
)
304 if (nexthop
->nh_label
) {
305 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
306 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
310 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
312 switch (nexthop
->type
) {
313 case NEXTHOP_TYPE_IFINDEX
:
314 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
316 case NEXTHOP_TYPE_IPV4
:
317 case NEXTHOP_TYPE_IPV4_IFINDEX
:
318 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
321 case NEXTHOP_TYPE_IPV6
:
322 case NEXTHOP_TYPE_IPV6_IFINDEX
:
323 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
326 case NEXTHOP_TYPE_BLACKHOLE
:
327 snprintf(str
, size
, "blackhole");
330 snprintf(str
, size
, "unknown");
338 * Iteration step for ALL_NEXTHOPS macro:
339 * This is the tricky part. Check if `nexthop' has
340 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
341 * at least one nexthop attached to `nexthop->resolved', which will be
344 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
345 * current chain. In case its current chain end is reached, it will move
346 * upwards in the recursion levels and progress there. Whenever a step
347 * forward in a chain is done, recursion will be checked again.
348 * In a nustshell, it's equivalent to a pre-traversal order assuming that
349 * left branch is 'resolved' and right branch is 'next':
350 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
352 struct nexthop
*nexthop_next(const struct nexthop
*nexthop
)
354 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
355 return nexthop
->resolved
;
358 return nexthop
->next
;
360 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
367 /* Return the next nexthop in the tree that is resolved and active */
368 struct nexthop
*nexthop_next_active_resolved(const struct nexthop
*nexthop
)
370 struct nexthop
*next
= nexthop_next(nexthop
);
373 && (CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_RECURSIVE
)
374 || !CHECK_FLAG(next
->flags
, NEXTHOP_FLAG_ACTIVE
)))
375 next
= nexthop_next(next
);
380 unsigned int nexthop_level(struct nexthop
*nexthop
)
384 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
390 /* Only hash word-sized things, let cmp do the rest. */
391 uint32_t nexthop_hash_quick(const struct nexthop
*nexthop
)
393 uint32_t key
= 0x45afe398;
395 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
396 nexthop
->nh_label_type
, key
);
398 if (nexthop
->nh_label
) {
399 int labels
= nexthop
->nh_label
->num_labels
;
402 while (labels
>= 3) {
403 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
404 nexthop
->nh_label
->label
[i
+ 1],
405 nexthop
->nh_label
->label
[i
+ 2],
412 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
413 nexthop
->nh_label
->label
[i
+ 1],
420 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
423 key
= jhash_2words(nexthop
->ifindex
,
424 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
),
431 #define GATE_SIZE 4 /* Number of uint32_t words in struct g_addr */
433 /* For a more granular hash */
434 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
436 uint32_t gate_src_rmap_raw
[GATE_SIZE
* 3] = {};
437 /* Get all the quick stuff */
438 uint32_t key
= nexthop_hash_quick(nexthop
);
440 assert(((sizeof(nexthop
->gate
) + sizeof(nexthop
->src
)
441 + sizeof(nexthop
->rmap_src
))
443 == (GATE_SIZE
* sizeof(uint32_t)));
445 memcpy(gate_src_rmap_raw
, &nexthop
->gate
, GATE_SIZE
);
446 memcpy(gate_src_rmap_raw
+ GATE_SIZE
, &nexthop
->src
, GATE_SIZE
);
447 memcpy(gate_src_rmap_raw
+ (2 * GATE_SIZE
), &nexthop
->rmap_src
,
450 key
= jhash2(gate_src_rmap_raw
, (GATE_SIZE
* 3), key
);
455 void nexthop_copy(struct nexthop
*copy
, const struct nexthop
*nexthop
,
456 struct nexthop
*rparent
)
458 copy
->vrf_id
= nexthop
->vrf_id
;
459 copy
->ifindex
= nexthop
->ifindex
;
460 copy
->type
= nexthop
->type
;
461 copy
->flags
= nexthop
->flags
;
462 memcpy(©
->gate
, &nexthop
->gate
, sizeof(nexthop
->gate
));
463 memcpy(©
->src
, &nexthop
->src
, sizeof(nexthop
->src
));
464 memcpy(©
->rmap_src
, &nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
));
465 copy
->rparent
= rparent
;
466 if (nexthop
->nh_label
)
467 nexthop_add_labels(copy
, nexthop
->nh_label_type
,
468 nexthop
->nh_label
->num_labels
,
469 &nexthop
->nh_label
->label
[0]);
472 struct nexthop
*nexthop_dup(const struct nexthop
*nexthop
,
473 struct nexthop
*rparent
)
475 struct nexthop
*new = nexthop_new();
477 nexthop_copy(new, nexthop
, rparent
);
482 * nexthop printing variants:
486 * is directly connected, eth0
487 * unreachable (blackhole)
491 * directly connected, eth0
492 * unreachable (blackhole)
496 printfrr_ext_autoreg_p("NH", printfrr_nh
)
497 static ssize_t
printfrr_nh(char *buf
, size_t bsz
, const char *fmt
,
498 int prec
, const void *ptr
)
500 const struct nexthop
*nexthop
= ptr
;
501 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= bsz
- 1 };
503 const char *s
, *v_is
= "", *v_via
= "", *v_viaif
= "via ";
515 switch (nexthop
->type
) {
516 case NEXTHOP_TYPE_IPV4
:
517 case NEXTHOP_TYPE_IPV4_IFINDEX
:
518 bprintfrr(&fb
, "%s%pI4", v_via
, &nexthop
->gate
.ipv4
);
521 case NEXTHOP_TYPE_IPV6
:
522 case NEXTHOP_TYPE_IPV6_IFINDEX
:
523 bprintfrr(&fb
, "%s%pI6", v_via
, &nexthop
->gate
.ipv6
);
526 case NEXTHOP_TYPE_IFINDEX
:
527 bprintfrr(&fb
, "%sdirectly connected, %s", v_is
,
528 ifindex2ifname(nexthop
->ifindex
,
531 case NEXTHOP_TYPE_BLACKHOLE
:
532 switch (nexthop
->bh_type
) {
533 case BLACKHOLE_REJECT
:
534 s
= " (ICMP unreachable)";
536 case BLACKHOLE_ADMINPROHIB
:
537 s
= " (ICMP admin-prohibited)";
546 bprintfrr(&fb
, "unreachable%s", s
);
551 if (do_ifi
&& nexthop
->ifindex
)
552 bprintfrr(&fb
, ", %s%s", v_viaif
, ifindex2ifname(
559 nexthop2str(nexthop
, buf
, bsz
);