]>
Commit | Line | Data |
---|---|---|
fb018d25 DS |
1 | /* A generic nexthop structure |
2 | * Copyright (C) 2013 Cumulus Networks, Inc. | |
3 | * | |
a399694f | 4 | * This file is part of Quagga. |
fb018d25 | 5 | * |
a399694f | 6 | * Quagga is free software; you can redistribute it and/or modify it |
fb018d25 DS |
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 | |
9 | * later version. | |
10 | * | |
a399694f | 11 | * Quagga is distributed in the hope that it will be useful, but |
fb018d25 DS |
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. | |
15 | * | |
896014f4 DL |
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 | |
fb018d25 DS |
19 | */ |
20 | #include <zebra.h> | |
21 | ||
22 | #include "prefix.h" | |
23 | #include "table.h" | |
24 | #include "memory.h" | |
fb018d25 DS |
25 | #include "command.h" |
26 | #include "if.h" | |
27 | #include "log.h" | |
28 | #include "sockunion.h" | |
29 | #include "linklist.h" | |
30 | #include "thread.h" | |
31 | #include "prefix.h" | |
32 | #include "nexthop.h" | |
40c7bdb0 | 33 | #include "mpls.h" |
d36d0d57 | 34 | #include "jhash.h" |
fb018d25 | 35 | |
d62a17ae | 36 | DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop") |
37 | DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label") | |
4a1ab8e4 | 38 | |
ebc403dd SW |
39 | static int nexthop_labels_cmp(const struct nexthop *nh1, |
40 | const struct nexthop *nh2) | |
41 | { | |
42 | const struct mpls_label_stack *nhl1 = NULL; | |
43 | const struct mpls_label_stack *nhl2 = NULL; | |
44 | ||
45 | nhl1 = nh1->nh_label; | |
46 | nhl2 = nh2->nh_label; | |
47 | ||
48 | /* No labels is a match */ | |
49 | if (!nhl1 && !nhl2) | |
50 | return 0; | |
51 | ||
52 | if (nhl1 && !nhl2) | |
53 | return 1; | |
54 | ||
55 | if (nhl2 && !nhl1) | |
56 | return -1; | |
57 | ||
58 | if (nhl1->num_labels > nhl2->num_labels) | |
59 | return 1; | |
60 | ||
61 | if (nhl1->num_labels < nhl2->num_labels) | |
62 | return -1; | |
63 | ||
64 | return memcmp(nhl1->label, nhl2->label, nhl1->num_labels); | |
65 | } | |
66 | ||
776c3e90 DS |
67 | int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2) |
68 | { | |
69 | int ret; | |
70 | uint32_t n1, n2; | |
71 | ||
72 | if (next1->vrf_id < next2->vrf_id) | |
73 | return -1; | |
74 | ||
75 | if (next1->vrf_id > next2->vrf_id) | |
76 | return 1; | |
77 | ||
78 | if (next1->type < next2->type) | |
79 | return -1; | |
80 | ||
81 | if (next1->type > next2->type) | |
82 | return 1; | |
83 | ||
84 | switch(next1->type) { | |
85 | case NEXTHOP_TYPE_IPV4: | |
86 | n1 = ntohl(next1->gate.ipv4.s_addr); | |
87 | n2 = ntohl(next2->gate.ipv4.s_addr); | |
88 | if (n1 < n2) | |
89 | return -1; | |
90 | if (n1 > n2) | |
91 | return 1; | |
92 | break; | |
93 | case NEXTHOP_TYPE_IPV6: | |
94 | ret = memcmp(&next1->gate, &next2->gate, sizeof(union g_addr)); | |
95 | if (!ret) | |
96 | return ret; | |
97 | break; | |
98 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
99 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
100 | ret = memcmp(&next1->gate, &next2->gate, sizeof(union g_addr)); | |
101 | if (!ret) | |
102 | return ret; | |
103 | /* Intentional Fall-Through */ | |
104 | case NEXTHOP_TYPE_IFINDEX: | |
105 | if (next1->ifindex < next2->ifindex) | |
106 | return -1; | |
107 | ||
108 | if (next1->ifindex > next2->ifindex) | |
109 | return 1; | |
110 | break; | |
111 | case NEXTHOP_TYPE_BLACKHOLE: | |
112 | if (next1->bh_type < next2->bh_type) | |
113 | return -1; | |
114 | ||
115 | if (next1->bh_type > next2->bh_type) | |
116 | return 1; | |
117 | break; | |
118 | } | |
119 | ||
120 | ret = memcmp(&next1->src, &next2->src, sizeof(union g_addr)); | |
121 | return ret; | |
122 | } | |
123 | ||
fb018d25 | 124 | /* check if nexthops are same, non-recursive */ |
fd36be7e DL |
125 | int nexthop_same_no_recurse(const struct nexthop *next1, |
126 | const struct nexthop *next2) | |
fb018d25 | 127 | { |
d62a17ae | 128 | if (next1->type != next2->type) |
129 | return 0; | |
130 | ||
131 | switch (next1->type) { | |
132 | case NEXTHOP_TYPE_IPV4: | |
133 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
134 | if (!IPV4_ADDR_SAME(&next1->gate.ipv4, &next2->gate.ipv4)) | |
135 | return 0; | |
136 | if (next1->ifindex && (next1->ifindex != next2->ifindex)) | |
137 | return 0; | |
138 | break; | |
139 | case NEXTHOP_TYPE_IFINDEX: | |
140 | if (next1->ifindex != next2->ifindex) | |
141 | return 0; | |
142 | break; | |
143 | case NEXTHOP_TYPE_IPV6: | |
144 | if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6)) | |
145 | return 0; | |
146 | break; | |
147 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
148 | if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6)) | |
149 | return 0; | |
150 | if (next1->ifindex != next2->ifindex) | |
151 | return 0; | |
25b9cb0c DL |
152 | break; |
153 | default: | |
154 | /* do nothing */ | |
155 | break; | |
156 | } | |
157 | return 1; | |
158 | } | |
159 | ||
996c9314 | 160 | int nexthop_same_firsthop(struct nexthop *next1, struct nexthop *next2) |
25b9cb0c DL |
161 | { |
162 | int type1 = NEXTHOP_FIRSTHOPTYPE(next1->type); | |
163 | int type2 = NEXTHOP_FIRSTHOPTYPE(next2->type); | |
164 | ||
165 | if (type1 != type2) | |
166 | return 0; | |
996c9314 | 167 | switch (type1) { |
25b9cb0c | 168 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
996c9314 | 169 | if (!IPV4_ADDR_SAME(&next1->gate.ipv4, &next2->gate.ipv4)) |
25b9cb0c DL |
170 | return 0; |
171 | if (next1->ifindex != next2->ifindex) | |
172 | return 0; | |
173 | break; | |
174 | case NEXTHOP_TYPE_IFINDEX: | |
175 | if (next1->ifindex != next2->ifindex) | |
176 | return 0; | |
177 | break; | |
178 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
996c9314 | 179 | if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6)) |
25b9cb0c DL |
180 | return 0; |
181 | if (next1->ifindex != next2->ifindex) | |
182 | return 0; | |
d62a17ae | 183 | break; |
184 | default: | |
185 | /* do nothing */ | |
186 | break; | |
187 | } | |
188 | return 1; | |
fb018d25 DS |
189 | } |
190 | ||
191 | /* | |
192 | * nexthop_type_to_str | |
193 | */ | |
d62a17ae | 194 | const char *nexthop_type_to_str(enum nexthop_types_t nh_type) |
fb018d25 | 195 | { |
d62a17ae | 196 | static const char *desc[] = { |
197 | "none", "Directly connected", | |
198 | "IPv4 nexthop", "IPv4 nexthop with ifindex", | |
199 | "IPv6 nexthop", "IPv6 nexthop with ifindex", | |
200 | "Null0 nexthop", | |
201 | }; | |
202 | ||
203 | return desc[nh_type]; | |
fb018d25 | 204 | } |
a399694f | 205 | |
a64448ba DS |
206 | /* |
207 | * Check if the labels match for the 2 nexthops specified. | |
208 | */ | |
89dc3160 | 209 | bool nexthop_labels_match(const struct nexthop *nh1, const struct nexthop *nh2) |
a64448ba | 210 | { |
89dc3160 SW |
211 | if (nexthop_labels_cmp(nh1, nh2) != 0) |
212 | return false; | |
a64448ba | 213 | |
89dc3160 | 214 | return true; |
a64448ba DS |
215 | } |
216 | ||
d62a17ae | 217 | struct nexthop *nexthop_new(void) |
a399694f | 218 | { |
d62a17ae | 219 | return XCALLOC(MTYPE_NEXTHOP, sizeof(struct nexthop)); |
a399694f DS |
220 | } |
221 | ||
a399694f | 222 | /* Free nexthop. */ |
d62a17ae | 223 | void nexthop_free(struct nexthop *nexthop) |
a399694f | 224 | { |
d62a17ae | 225 | nexthop_del_labels(nexthop); |
226 | if (nexthop->resolved) | |
227 | nexthops_free(nexthop->resolved); | |
228 | XFREE(MTYPE_NEXTHOP, nexthop); | |
a399694f DS |
229 | } |
230 | ||
231 | /* Frees a list of nexthops */ | |
d62a17ae | 232 | void nexthops_free(struct nexthop *nexthop) |
a399694f | 233 | { |
d62a17ae | 234 | struct nexthop *nh, *next; |
a399694f | 235 | |
d62a17ae | 236 | for (nh = nexthop; nh; nh = next) { |
237 | next = nh->next; | |
238 | nexthop_free(nh); | |
239 | } | |
a399694f | 240 | } |
80c2442a | 241 | |
31919191 DS |
242 | bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2) |
243 | { | |
244 | if (nh1 && !nh2) | |
245 | return false; | |
246 | ||
247 | if (!nh1 && nh2) | |
248 | return false; | |
249 | ||
250 | if (nh1 == nh2) | |
251 | return true; | |
252 | ||
253 | if (nh1->vrf_id != nh2->vrf_id) | |
254 | return false; | |
255 | ||
256 | if (nh1->type != nh2->type) | |
257 | return false; | |
258 | ||
259 | switch (nh1->type) { | |
260 | case NEXTHOP_TYPE_IFINDEX: | |
261 | if (nh1->ifindex != nh2->ifindex) | |
262 | return false; | |
263 | break; | |
264 | case NEXTHOP_TYPE_IPV4: | |
265 | if (nh1->gate.ipv4.s_addr != nh2->gate.ipv4.s_addr) | |
266 | return false; | |
267 | break; | |
268 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
269 | if (nh1->gate.ipv4.s_addr != nh2->gate.ipv4.s_addr) | |
270 | return false; | |
271 | if (nh1->ifindex != nh2->ifindex) | |
272 | return false; | |
273 | break; | |
274 | case NEXTHOP_TYPE_IPV6: | |
275 | if (memcmp(&nh1->gate.ipv6, &nh2->gate.ipv6, 16)) | |
276 | return false; | |
277 | break; | |
278 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
279 | if (memcmp(&nh1->gate.ipv6, &nh2->gate.ipv6, 16)) | |
280 | return false; | |
281 | if (nh1->ifindex != nh2->ifindex) | |
282 | return false; | |
283 | break; | |
284 | case NEXTHOP_TYPE_BLACKHOLE: | |
285 | if (nh1->bh_type != nh2->bh_type) | |
286 | return false; | |
287 | break; | |
288 | } | |
289 | ||
55f93d4b MS |
290 | /* Compare labels too (if present) */ |
291 | return (!!nexthop_labels_match(nh1, nh2)); | |
31919191 DS |
292 | } |
293 | ||
40c7bdb0 | 294 | /* Update nexthop with label information. */ |
d62a17ae | 295 | void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t type, |
d7c0a89a | 296 | uint8_t num_labels, mpls_label_t *label) |
40c7bdb0 | 297 | { |
8ecdb26e | 298 | struct mpls_label_stack *nh_label; |
d62a17ae | 299 | int i; |
300 | ||
301 | nexthop->nh_label_type = type; | |
302 | nh_label = XCALLOC(MTYPE_NH_LABEL, | |
8ecdb26e | 303 | sizeof(struct mpls_label_stack) |
d62a17ae | 304 | + num_labels * sizeof(mpls_label_t)); |
305 | nh_label->num_labels = num_labels; | |
306 | for (i = 0; i < num_labels; i++) | |
307 | nh_label->label[i] = *(label + i); | |
308 | nexthop->nh_label = nh_label; | |
40c7bdb0 | 309 | } |
310 | ||
311 | /* Free label information of nexthop, if present. */ | |
d62a17ae | 312 | void nexthop_del_labels(struct nexthop *nexthop) |
40c7bdb0 | 313 | { |
d62a17ae | 314 | if (nexthop->nh_label) { |
315 | XFREE(MTYPE_NH_LABEL, nexthop->nh_label); | |
316 | nexthop->nh_label_type = ZEBRA_LSP_NONE; | |
317 | } | |
40c7bdb0 | 318 | } |
319 | ||
d36d0d57 | 320 | const char *nexthop2str(const struct nexthop *nexthop, char *str, int size) |
80c2442a | 321 | { |
d62a17ae | 322 | switch (nexthop->type) { |
323 | case NEXTHOP_TYPE_IFINDEX: | |
324 | snprintf(str, size, "if %u", nexthop->ifindex); | |
325 | break; | |
326 | case NEXTHOP_TYPE_IPV4: | |
d62a17ae | 327 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
328 | snprintf(str, size, "%s if %u", inet_ntoa(nexthop->gate.ipv4), | |
329 | nexthop->ifindex); | |
330 | break; | |
331 | case NEXTHOP_TYPE_IPV6: | |
d62a17ae | 332 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
333 | snprintf(str, size, "%s if %u", inet6_ntoa(nexthop->gate.ipv6), | |
334 | nexthop->ifindex); | |
335 | break; | |
336 | case NEXTHOP_TYPE_BLACKHOLE: | |
337 | snprintf(str, size, "blackhole"); | |
338 | break; | |
339 | default: | |
340 | snprintf(str, size, "unknown"); | |
341 | break; | |
342 | } | |
343 | ||
344 | return str; | |
80c2442a | 345 | } |
9fb47c05 CF |
346 | |
347 | /* | |
348 | * Iteration step for ALL_NEXTHOPS macro: | |
349 | * This is the tricky part. Check if `nexthop' has | |
350 | * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has | |
351 | * at least one nexthop attached to `nexthop->resolved', which will be | |
352 | * the next one. | |
353 | * | |
354 | * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its | |
355 | * current chain. In case its current chain end is reached, it will move | |
356 | * upwards in the recursion levels and progress there. Whenever a step | |
357 | * forward in a chain is done, recursion will be checked again. | |
358 | * In a nustshell, it's equivalent to a pre-traversal order assuming that | |
359 | * left branch is 'resolved' and right branch is 'next': | |
360 | * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg | |
361 | */ | |
d62a17ae | 362 | struct nexthop *nexthop_next(struct nexthop *nexthop) |
9fb47c05 | 363 | { |
d62a17ae | 364 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
365 | return nexthop->resolved; | |
9fb47c05 | 366 | |
d62a17ae | 367 | if (nexthop->next) |
368 | return nexthop->next; | |
9fb47c05 | 369 | |
d62a17ae | 370 | for (struct nexthop *par = nexthop->rparent; par; par = par->rparent) |
371 | if (par->next) | |
372 | return par->next; | |
9fb47c05 | 373 | |
d62a17ae | 374 | return NULL; |
9fb47c05 | 375 | } |
e3054ee9 | 376 | |
d62a17ae | 377 | unsigned int nexthop_level(struct nexthop *nexthop) |
e3054ee9 | 378 | { |
d62a17ae | 379 | unsigned int rv = 0; |
e3054ee9 | 380 | |
d62a17ae | 381 | for (struct nexthop *par = nexthop->rparent; par; par = par->rparent) |
382 | rv++; | |
e3054ee9 | 383 | |
d62a17ae | 384 | return rv; |
e3054ee9 | 385 | } |
d36d0d57 | 386 | |
1b1fe1c4 | 387 | uint32_t nexthop_hash(const struct nexthop *nexthop) |
d36d0d57 | 388 | { |
1b1fe1c4 | 389 | uint32_t key = 0x45afe398; |
d36d0d57 | 390 | |
1b1fe1c4 SW |
391 | key = jhash_3words(nexthop->type, nexthop->vrf_id, |
392 | nexthop->nh_label_type, key); | |
393 | /* gate and blackhole are together in a union */ | |
394 | key = jhash(&nexthop->gate, sizeof(nexthop->gate), key); | |
395 | key = jhash(&nexthop->src, sizeof(nexthop->src), key); | |
396 | key = jhash(&nexthop->rmap_src, sizeof(nexthop->rmap_src), key); | |
d36d0d57 | 397 | |
1b1fe1c4 SW |
398 | if (nexthop->nh_label) { |
399 | int labels = nexthop->nh_label->num_labels; | |
400 | int i = 0; | |
401 | ||
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], | |
406 | key); | |
407 | labels -= 3; | |
408 | i += 3; | |
409 | } | |
410 | ||
411 | if (labels >= 2) { | |
412 | key = jhash_2words(nexthop->nh_label->label[i], | |
413 | nexthop->nh_label->label[i + 1], | |
414 | key); | |
415 | labels -= 2; | |
416 | i += 2; | |
417 | } | |
418 | ||
419 | if (labels >= 1) | |
420 | key = jhash_1word(nexthop->nh_label->label[i], key); | |
421 | } | |
422 | ||
423 | switch (nexthop->type) { | |
424 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
425 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
426 | case NEXTHOP_TYPE_IFINDEX: | |
427 | key = jhash_1word(nexthop->ifindex, key); | |
428 | break; | |
429 | case NEXTHOP_TYPE_BLACKHOLE: | |
430 | case NEXTHOP_TYPE_IPV4: | |
431 | case NEXTHOP_TYPE_IPV6: | |
432 | break; | |
433 | } | |
d36d0d57 QY |
434 | return key; |
435 | } |