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 /* check if nexthops are same, non-recursive */
40 int nexthop_same_no_recurse(const struct nexthop
*next1
,
41 const struct nexthop
*next2
)
43 if (next1
->type
!= next2
->type
)
46 switch (next1
->type
) {
47 case NEXTHOP_TYPE_IPV4
:
48 case NEXTHOP_TYPE_IPV4_IFINDEX
:
49 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
51 if (next1
->ifindex
&& (next1
->ifindex
!= next2
->ifindex
))
54 case NEXTHOP_TYPE_IFINDEX
:
55 if (next1
->ifindex
!= next2
->ifindex
)
58 case NEXTHOP_TYPE_IPV6
:
59 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
62 case NEXTHOP_TYPE_IPV6_IFINDEX
:
63 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
65 if (next1
->ifindex
!= next2
->ifindex
)
75 int nexthop_same_firsthop(struct nexthop
*next1
, struct nexthop
*next2
)
77 int type1
= NEXTHOP_FIRSTHOPTYPE(next1
->type
);
78 int type2
= NEXTHOP_FIRSTHOPTYPE(next2
->type
);
83 case NEXTHOP_TYPE_IPV4_IFINDEX
:
84 if (!IPV4_ADDR_SAME(&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
86 if (next1
->ifindex
!= next2
->ifindex
)
89 case NEXTHOP_TYPE_IFINDEX
:
90 if (next1
->ifindex
!= next2
->ifindex
)
93 case NEXTHOP_TYPE_IPV6_IFINDEX
:
94 if (!IPV6_ADDR_SAME(&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
96 if (next1
->ifindex
!= next2
->ifindex
)
107 * nexthop_type_to_str
109 const char *nexthop_type_to_str(enum nexthop_types_t nh_type
)
111 static const char *desc
[] = {
112 "none", "Directly connected",
113 "IPv4 nexthop", "IPv4 nexthop with ifindex",
114 "IPv6 nexthop", "IPv6 nexthop with ifindex",
118 return desc
[nh_type
];
122 * Check if the labels match for the 2 nexthops specified.
124 int nexthop_labels_match(struct nexthop
*nh1
, struct nexthop
*nh2
)
126 struct mpls_label_stack
*nhl1
, *nhl2
;
128 nhl1
= nh1
->nh_label
;
129 nhl2
= nh2
->nh_label
;
133 if (nhl1
->num_labels
!= nhl2
->num_labels
)
136 if (memcmp(nhl1
->label
, nhl2
->label
, nhl1
->num_labels
))
142 struct nexthop
*nexthop_new(void)
144 return XCALLOC(MTYPE_NEXTHOP
, sizeof(struct nexthop
));
148 void nexthop_free(struct nexthop
*nexthop
)
150 nexthop_del_labels(nexthop
);
151 if (nexthop
->resolved
)
152 nexthops_free(nexthop
->resolved
);
153 XFREE(MTYPE_NEXTHOP
, nexthop
);
156 /* Frees a list of nexthops */
157 void nexthops_free(struct nexthop
*nexthop
)
159 struct nexthop
*nh
, *next
;
161 for (nh
= nexthop
; nh
; nh
= next
) {
167 bool nexthop_same(const struct nexthop
*nh1
, const struct nexthop
*nh2
)
178 if (nh1
->vrf_id
!= nh2
->vrf_id
)
181 if (nh1
->type
!= nh2
->type
)
185 case NEXTHOP_TYPE_IFINDEX
:
186 if (nh1
->ifindex
!= nh2
->ifindex
)
189 case NEXTHOP_TYPE_IPV4
:
190 if (nh1
->gate
.ipv4
.s_addr
!= nh2
->gate
.ipv4
.s_addr
)
193 case NEXTHOP_TYPE_IPV4_IFINDEX
:
194 if (nh1
->gate
.ipv4
.s_addr
!= nh2
->gate
.ipv4
.s_addr
)
196 if (nh1
->ifindex
!= nh2
->ifindex
)
199 case NEXTHOP_TYPE_IPV6
:
200 if (memcmp(&nh1
->gate
.ipv6
, &nh2
->gate
.ipv6
, 16))
203 case NEXTHOP_TYPE_IPV6_IFINDEX
:
204 if (memcmp(&nh1
->gate
.ipv6
, &nh2
->gate
.ipv6
, 16))
206 if (nh1
->ifindex
!= nh2
->ifindex
)
209 case NEXTHOP_TYPE_BLACKHOLE
:
210 if (nh1
->bh_type
!= nh2
->bh_type
)
218 /* Update nexthop with label information. */
219 void nexthop_add_labels(struct nexthop
*nexthop
, enum lsp_types_t type
,
220 uint8_t num_labels
, mpls_label_t
*label
)
222 struct mpls_label_stack
*nh_label
;
225 nexthop
->nh_label_type
= type
;
226 nh_label
= XCALLOC(MTYPE_NH_LABEL
,
227 sizeof(struct mpls_label_stack
)
228 + num_labels
* sizeof(mpls_label_t
));
229 nh_label
->num_labels
= num_labels
;
230 for (i
= 0; i
< num_labels
; i
++)
231 nh_label
->label
[i
] = *(label
+ i
);
232 nexthop
->nh_label
= nh_label
;
235 /* Free label information of nexthop, if present. */
236 void nexthop_del_labels(struct nexthop
*nexthop
)
238 if (nexthop
->nh_label
) {
239 XFREE(MTYPE_NH_LABEL
, nexthop
->nh_label
);
240 nexthop
->nh_label_type
= ZEBRA_LSP_NONE
;
244 const char *nexthop2str(const struct nexthop
*nexthop
, char *str
, int size
)
246 switch (nexthop
->type
) {
247 case NEXTHOP_TYPE_IFINDEX
:
248 snprintf(str
, size
, "if %u", nexthop
->ifindex
);
250 case NEXTHOP_TYPE_IPV4
:
251 snprintf(str
, size
, "%s", inet_ntoa(nexthop
->gate
.ipv4
));
253 case NEXTHOP_TYPE_IPV4_IFINDEX
:
254 snprintf(str
, size
, "%s if %u", inet_ntoa(nexthop
->gate
.ipv4
),
257 case NEXTHOP_TYPE_IPV6
:
258 snprintf(str
, size
, "%s", inet6_ntoa(nexthop
->gate
.ipv6
));
260 case NEXTHOP_TYPE_IPV6_IFINDEX
:
261 snprintf(str
, size
, "%s if %u", inet6_ntoa(nexthop
->gate
.ipv6
),
264 case NEXTHOP_TYPE_BLACKHOLE
:
265 snprintf(str
, size
, "blackhole");
268 snprintf(str
, size
, "unknown");
276 * Iteration step for ALL_NEXTHOPS macro:
277 * This is the tricky part. Check if `nexthop' has
278 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has
279 * at least one nexthop attached to `nexthop->resolved', which will be
282 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
283 * current chain. In case its current chain end is reached, it will move
284 * upwards in the recursion levels and progress there. Whenever a step
285 * forward in a chain is done, recursion will be checked again.
286 * In a nustshell, it's equivalent to a pre-traversal order assuming that
287 * left branch is 'resolved' and right branch is 'next':
288 * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg
290 struct nexthop
*nexthop_next(struct nexthop
*nexthop
)
292 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
293 return nexthop
->resolved
;
296 return nexthop
->next
;
298 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
305 unsigned int nexthop_level(struct nexthop
*nexthop
)
309 for (struct nexthop
*par
= nexthop
->rparent
; par
; par
= par
->rparent
)
315 uint32_t nexthop_hash(struct nexthop
*nexthop
)
319 key
= jhash_1word(nexthop
->vrf_id
, 0x45afe398);
320 key
= jhash_1word(nexthop
->ifindex
, key
);
321 key
= jhash_1word(nexthop
->type
, key
);
322 key
= jhash(&nexthop
->gate
, sizeof(union g_addr
), key
);