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"
36 DEFINE_MTYPE_STATIC(LIB
, NEXTHOP
, "Nexthop")
37 DEFINE_MTYPE_STATIC(LIB
, NH_LABEL
, "Nexthop label")
39 static int nexthop_labels_cmp(const struct nexthop
*nh1
,
40 const struct nexthop
*nh2
)
42 const struct mpls_label_stack
*nhl1
= NULL
;
43 const struct mpls_label_stack
*nhl2
= NULL
;
48 /* No labels is a match */
58 if (nhl1
->num_labels
> nhl2
->num_labels
)
61 if (nhl1
->num_labels
< nhl2
->num_labels
)
64 return memcmp(nhl1
->label
, nhl2
->label
, nhl1
->num_labels
);
67 int nexthop_cmp(const struct nexthop
*next1
, const struct nexthop
*next2
)
73 if (next1
->vrf_id
< next2
->vrf_id
)
76 if (next1
->vrf_id
> next2
->vrf_id
)
79 if (next1
->type
< next2
->type
)
82 if (next1
->type
> next2
->type
)
85 switch (next1
->type
) {
86 case NEXTHOP_TYPE_IPV4
:
87 n1
= ntohl(next1
->gate
.ipv4
.s_addr
);
88 n2
= ntohl(next2
->gate
.ipv4
.s_addr
);
94 case NEXTHOP_TYPE_IPV6
:
95 ret
= memcmp(&next1
->gate
, &next2
->gate
, sizeof(union g_addr
));
99 case NEXTHOP_TYPE_IPV4_IFINDEX
:
100 case NEXTHOP_TYPE_IPV6_IFINDEX
:
101 ret
= memcmp(&next1
->gate
, &next2
->gate
, sizeof(union g_addr
));
104 /* Intentional Fall-Through */
105 case NEXTHOP_TYPE_IFINDEX
:
106 if (next1
->ifindex
< next2
->ifindex
)
109 if (next1
->ifindex
> next2
->ifindex
)
112 case NEXTHOP_TYPE_BLACKHOLE
:
113 if (next1
->bh_type
< next2
->bh_type
)
116 if (next1
->bh_type
> next2
->bh_type
)
121 ret
= memcmp(&next1
->src
, &next2
->src
, sizeof(union g_addr
));
125 ret
= nexthop_labels_cmp(next1
, next2
);
129 /* check if nexthops are same, non-recursive */
130 int nexthop_same_no_recurse(const struct nexthop
*next1
,
131 const struct nexthop
*next2
)
133 if (next1
->type
!= next2
->type
)
136 switch (next1
->type
) {
137 case NEXTHOP_TYPE_IPV4
:
138 case NEXTHOP_TYPE_IPV4_IFINDEX
:
139 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
141 if (next1
->ifindex
&& (next1
->ifindex
!= next2
->ifindex
))
144 case NEXTHOP_TYPE_IFINDEX
:
145 if (next1
->ifindex
!= next2
->ifindex
)
148 case NEXTHOP_TYPE_IPV6
:
149 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
152 case NEXTHOP_TYPE_IPV6_IFINDEX
:
153 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
155 if (next1
->ifindex
!= next2
->ifindex
)
165 int nexthop_same_firsthop(struct nexthop
*next1
, struct nexthop
*next2
)
167 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
168 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
173 case NEXTHOP_TYPE_IPV4_IFINDEX
:
174 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
176 if (next1
->ifindex
!= next2
->ifindex
)
179 case NEXTHOP_TYPE_IFINDEX
:
180 if (next1
->ifindex
!= next2
->ifindex
)
183 case NEXTHOP_TYPE_IPV6_IFINDEX
:
184 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
186 if (next1
->ifindex
!= next2
->ifindex
)
197 * nexthop_type_to_str
199 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
201 static const char *desc
[] = {
202 "none", "Directly connected",
203 "IPv4 nexthop", "IPv4 nexthop with ifindex",
204 "IPv6 nexthop", "IPv6 nexthop with ifindex",
208 return desc
[nh_type
];
212 * Check if the labels match for the 2 nexthops specified.
214 bool nexthop_labels_match(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
216 if (nexthop_labels_cmp(nh1
, nh2
) != 0)
222 struct nexthop
*nexthop_new(void)
224 return XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
228 void nexthop_free(struct nexthop
*nexthop
)
230 nexthop_del_labels(nexthop
);
231 if (nexthop
->resolved
)
232 nexthops_free(nexthop
->resolved
);
233 XFREE(MTYPE_NEXTHOP
, nexthop
);
236 /* Frees a list of nexthops */
237 void nexthops_free(struct nexthop
*nexthop
)
239 struct nexthop
*nh
, *next
;
241 for (nh
= nexthop
; nh
; nh
= next
) {
247 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
258 if (nh1
->vrf_id
!= nh2
->vrf_id
)
261 if (nh1
->type
!= nh2
->type
)
265 case NEXTHOP_TYPE_IFINDEX
:
266 if (nh1
->ifindex
!= nh2
->ifindex
)
269 case NEXTHOP_TYPE_IPV4
:
270 if (nh1
->gate
.ipv4
.s_addr
!= nh2
->gate
.ipv4
.s_addr
)
273 case NEXTHOP_TYPE_IPV4_IFINDEX
:
274 if (nh1
->gate
.ipv4
.s_addr
!= nh2
->gate
.ipv4
.s_addr
)
276 if (nh1
->ifindex
!= nh2
->ifindex
)
279 case NEXTHOP_TYPE_IPV6
:
280 if (memcmp(&nh1
->gate
.ipv6
, &nh2
->gate
.ipv6
, 16))
283 case NEXTHOP_TYPE_IPV6_IFINDEX
:
284 if (memcmp(&nh1
->gate
.ipv6
, &nh2
->gate
.ipv6
, 16))
286 if (nh1
->ifindex
!= nh2
->ifindex
)
289 case NEXTHOP_TYPE_BLACKHOLE
:
290 if (nh1
->bh_type
!= nh2
->bh_type
)
295 /* Compare labels too (if present) */
296 return (!!nexthop_labels_match(nh1
, nh2
));
299 /* Update nexthop with label information. */
300 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
301 uint8_t num_labels
, mpls_label_t
*label
)
303 struct mpls_label_stack
*nh_label
;
306 nexthop
->nh_label_type
= type
;
307 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
308 sizeof(struct mpls_label_stack
)
309 + num_labels
* sizeof(mpls_label_t
));
310 nh_label
->num_labels
= num_labels
;
311 for (i
= 0; i
< num_labels
; i
++)
312 nh_label
->label
[i
] = *(label
+ i
);
313 nexthop
->nh_label
= nh_label
;
316 /* Free label information of nexthop, if present. */
317 void nexthop_del_labels(struct nexthop
*nexthop
)
319 if (nexthop
->nh_label
) {
320 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
321 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
325 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
327 switch (nexthop
->type
) {
328 case NEXTHOP_TYPE_IFINDEX
:
329 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
331 case NEXTHOP_TYPE_IPV4
:
332 case NEXTHOP_TYPE_IPV4_IFINDEX
:
333 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
336 case NEXTHOP_TYPE_IPV6
:
337 case NEXTHOP_TYPE_IPV6_IFINDEX
:
338 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
341 case NEXTHOP_TYPE_BLACKHOLE
:
342 snprintf(str
, size
, "blackhole");
345 snprintf(str
, size
, "unknown");
353 * Iteration step for ALL_NEXTHOPS macro:
354 * This is the tricky part. Check if `nexthop' has
355 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
356 * at least one nexthop attached to `nexthop->resolved', which will be
359 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
360 * current chain. In case its current chain end is reached, it will move
361 * upwards in the recursion levels and progress there. Whenever a step
362 * forward in a chain is done, recursion will be checked again.
363 * In a nustshell, it's equivalent to a pre-traversal order assuming that
364 * left branch is 'resolved' and right branch is 'next':
365 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
367 struct nexthop
*nexthop_next(struct nexthop
*nexthop
)
369 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
370 return nexthop
->resolved
;
373 return nexthop
->next
;
375 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
382 unsigned int nexthop_level(struct nexthop
*nexthop
)
386 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
392 uint32_t nexthop_hash(const struct nexthop
*nexthop
)
394 uint32_t key
= 0x45afe398;
396 key
= jhash_3words(nexthop
->type
, nexthop
->vrf_id
,
397 nexthop
->nh_label_type
, key
);
398 /* gate and blackhole are together in a union */
399 key
= jhash(&nexthop
->gate
, sizeof(nexthop
->gate
), key
);
400 key
= jhash(&nexthop
->src
, sizeof(nexthop
->src
), key
);
401 key
= jhash(&nexthop
->rmap_src
, sizeof(nexthop
->rmap_src
), key
);
403 if (nexthop
->nh_label
) {
404 int labels
= nexthop
->nh_label
->num_labels
;
407 while (labels
>= 3) {
408 key
= jhash_3words(nexthop
->nh_label
->label
[i
],
409 nexthop
->nh_label
->label
[i
+ 1],
410 nexthop
->nh_label
->label
[i
+ 2],
417 key
= jhash_2words(nexthop
->nh_label
->label
[i
],
418 nexthop
->nh_label
->label
[i
+ 1],
425 key
= jhash_1word(nexthop
->nh_label
->label
[i
], key
);
428 switch (nexthop
->type
) {
429 case NEXTHOP_TYPE_IPV4_IFINDEX
:
430 case NEXTHOP_TYPE_IPV6_IFINDEX
:
431 case NEXTHOP_TYPE_IFINDEX
:
432 key
= jhash_1word(nexthop
->ifindex
, key
);
434 case NEXTHOP_TYPE_BLACKHOLE
:
435 case NEXTHOP_TYPE_IPV4
:
436 case NEXTHOP_TYPE_IPV6
: