]>
Commit | Line | Data |
---|---|---|
1 | /* A generic nexthop structure | |
2 | * Copyright (C) 2013 Cumulus Networks, Inc. | |
3 | * | |
4 | * This file is part of Quagga. | |
5 | * | |
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 | |
9 | * later version. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | #include <zebra.h> | |
21 | ||
22 | #include "prefix.h" | |
23 | #include "table.h" | |
24 | #include "memory.h" | |
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" | |
33 | #include "mpls.h" | |
34 | #include "jhash.h" | |
35 | ||
36 | DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop") | |
37 | DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label") | |
38 | ||
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 | ||
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 | ||
124 | /* check if nexthops are same, non-recursive */ | |
125 | int nexthop_same_no_recurse(const struct nexthop *next1, | |
126 | const struct nexthop *next2) | |
127 | { | |
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; | |
152 | break; | |
153 | default: | |
154 | /* do nothing */ | |
155 | break; | |
156 | } | |
157 | return 1; | |
158 | } | |
159 | ||
160 | int nexthop_same_firsthop(struct nexthop *next1, struct nexthop *next2) | |
161 | { | |
162 | int type1 = NEXTHOP_FIRSTHOPTYPE(next1->type); | |
163 | int type2 = NEXTHOP_FIRSTHOPTYPE(next2->type); | |
164 | ||
165 | if (type1 != type2) | |
166 | return 0; | |
167 | switch (type1) { | |
168 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
169 | if (!IPV4_ADDR_SAME(&next1->gate.ipv4, &next2->gate.ipv4)) | |
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: | |
179 | if (!IPV6_ADDR_SAME(&next1->gate.ipv6, &next2->gate.ipv6)) | |
180 | return 0; | |
181 | if (next1->ifindex != next2->ifindex) | |
182 | return 0; | |
183 | break; | |
184 | default: | |
185 | /* do nothing */ | |
186 | break; | |
187 | } | |
188 | return 1; | |
189 | } | |
190 | ||
191 | /* | |
192 | * nexthop_type_to_str | |
193 | */ | |
194 | const char *nexthop_type_to_str(enum nexthop_types_t nh_type) | |
195 | { | |
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]; | |
204 | } | |
205 | ||
206 | /* | |
207 | * Check if the labels match for the 2 nexthops specified. | |
208 | */ | |
209 | int nexthop_labels_match(const struct nexthop *nh1, const struct nexthop *nh2) | |
210 | { | |
211 | const struct mpls_label_stack *nhl1, *nhl2; | |
212 | ||
213 | nhl1 = nh1->nh_label; | |
214 | nhl2 = nh2->nh_label; | |
215 | ||
216 | /* No labels is a match */ | |
217 | if (!nhl1 && !nhl2) | |
218 | return 1; | |
219 | ||
220 | if (!nhl1 || !nhl2) | |
221 | return 0; | |
222 | ||
223 | if (nhl1->num_labels != nhl2->num_labels) | |
224 | return 0; | |
225 | ||
226 | if (memcmp(nhl1->label, nhl2->label, nhl1->num_labels)) | |
227 | return 0; | |
228 | ||
229 | return 1; | |
230 | } | |
231 | ||
232 | struct nexthop *nexthop_new(void) | |
233 | { | |
234 | return XCALLOC(MTYPE_NEXTHOP, sizeof(struct nexthop)); | |
235 | } | |
236 | ||
237 | /* Free nexthop. */ | |
238 | void nexthop_free(struct nexthop *nexthop) | |
239 | { | |
240 | nexthop_del_labels(nexthop); | |
241 | if (nexthop->resolved) | |
242 | nexthops_free(nexthop->resolved); | |
243 | XFREE(MTYPE_NEXTHOP, nexthop); | |
244 | } | |
245 | ||
246 | /* Frees a list of nexthops */ | |
247 | void nexthops_free(struct nexthop *nexthop) | |
248 | { | |
249 | struct nexthop *nh, *next; | |
250 | ||
251 | for (nh = nexthop; nh; nh = next) { | |
252 | next = nh->next; | |
253 | nexthop_free(nh); | |
254 | } | |
255 | } | |
256 | ||
257 | bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2) | |
258 | { | |
259 | if (nh1 && !nh2) | |
260 | return false; | |
261 | ||
262 | if (!nh1 && nh2) | |
263 | return false; | |
264 | ||
265 | if (nh1 == nh2) | |
266 | return true; | |
267 | ||
268 | if (nh1->vrf_id != nh2->vrf_id) | |
269 | return false; | |
270 | ||
271 | if (nh1->type != nh2->type) | |
272 | return false; | |
273 | ||
274 | switch (nh1->type) { | |
275 | case NEXTHOP_TYPE_IFINDEX: | |
276 | if (nh1->ifindex != nh2->ifindex) | |
277 | return false; | |
278 | break; | |
279 | case NEXTHOP_TYPE_IPV4: | |
280 | if (nh1->gate.ipv4.s_addr != nh2->gate.ipv4.s_addr) | |
281 | return false; | |
282 | break; | |
283 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
284 | if (nh1->gate.ipv4.s_addr != nh2->gate.ipv4.s_addr) | |
285 | return false; | |
286 | if (nh1->ifindex != nh2->ifindex) | |
287 | return false; | |
288 | break; | |
289 | case NEXTHOP_TYPE_IPV6: | |
290 | if (memcmp(&nh1->gate.ipv6, &nh2->gate.ipv6, 16)) | |
291 | return false; | |
292 | break; | |
293 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
294 | if (memcmp(&nh1->gate.ipv6, &nh2->gate.ipv6, 16)) | |
295 | return false; | |
296 | if (nh1->ifindex != nh2->ifindex) | |
297 | return false; | |
298 | break; | |
299 | case NEXTHOP_TYPE_BLACKHOLE: | |
300 | if (nh1->bh_type != nh2->bh_type) | |
301 | return false; | |
302 | break; | |
303 | } | |
304 | ||
305 | /* Compare labels too (if present) */ | |
306 | return (!!nexthop_labels_match(nh1, nh2)); | |
307 | } | |
308 | ||
309 | /* Update nexthop with label information. */ | |
310 | void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t type, | |
311 | uint8_t num_labels, mpls_label_t *label) | |
312 | { | |
313 | struct mpls_label_stack *nh_label; | |
314 | int i; | |
315 | ||
316 | nexthop->nh_label_type = type; | |
317 | nh_label = XCALLOC(MTYPE_NH_LABEL, | |
318 | sizeof(struct mpls_label_stack) | |
319 | + num_labels * sizeof(mpls_label_t)); | |
320 | nh_label->num_labels = num_labels; | |
321 | for (i = 0; i < num_labels; i++) | |
322 | nh_label->label[i] = *(label + i); | |
323 | nexthop->nh_label = nh_label; | |
324 | } | |
325 | ||
326 | /* Free label information of nexthop, if present. */ | |
327 | void nexthop_del_labels(struct nexthop *nexthop) | |
328 | { | |
329 | if (nexthop->nh_label) { | |
330 | XFREE(MTYPE_NH_LABEL, nexthop->nh_label); | |
331 | nexthop->nh_label_type = ZEBRA_LSP_NONE; | |
332 | } | |
333 | } | |
334 | ||
335 | const char *nexthop2str(const struct nexthop *nexthop, char *str, int size) | |
336 | { | |
337 | switch (nexthop->type) { | |
338 | case NEXTHOP_TYPE_IFINDEX: | |
339 | snprintf(str, size, "if %u", nexthop->ifindex); | |
340 | break; | |
341 | case NEXTHOP_TYPE_IPV4: | |
342 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
343 | snprintf(str, size, "%s if %u", inet_ntoa(nexthop->gate.ipv4), | |
344 | nexthop->ifindex); | |
345 | break; | |
346 | case NEXTHOP_TYPE_IPV6: | |
347 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
348 | snprintf(str, size, "%s if %u", inet6_ntoa(nexthop->gate.ipv6), | |
349 | nexthop->ifindex); | |
350 | break; | |
351 | case NEXTHOP_TYPE_BLACKHOLE: | |
352 | snprintf(str, size, "blackhole"); | |
353 | break; | |
354 | default: | |
355 | snprintf(str, size, "unknown"); | |
356 | break; | |
357 | } | |
358 | ||
359 | return str; | |
360 | } | |
361 | ||
362 | /* | |
363 | * Iteration step for ALL_NEXTHOPS macro: | |
364 | * This is the tricky part. Check if `nexthop' has | |
365 | * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' has | |
366 | * at least one nexthop attached to `nexthop->resolved', which will be | |
367 | * the next one. | |
368 | * | |
369 | * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its | |
370 | * current chain. In case its current chain end is reached, it will move | |
371 | * upwards in the recursion levels and progress there. Whenever a step | |
372 | * forward in a chain is done, recursion will be checked again. | |
373 | * In a nustshell, it's equivalent to a pre-traversal order assuming that | |
374 | * left branch is 'resolved' and right branch is 'next': | |
375 | * https://en.wikipedia.org/wiki/Tree_traversal#/media/File:Sorted_binary_tree_preorder.svg | |
376 | */ | |
377 | struct nexthop *nexthop_next(struct nexthop *nexthop) | |
378 | { | |
379 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
380 | return nexthop->resolved; | |
381 | ||
382 | if (nexthop->next) | |
383 | return nexthop->next; | |
384 | ||
385 | for (struct nexthop *par = nexthop->rparent; par; par = par->rparent) | |
386 | if (par->next) | |
387 | return par->next; | |
388 | ||
389 | return NULL; | |
390 | } | |
391 | ||
392 | unsigned int nexthop_level(struct nexthop *nexthop) | |
393 | { | |
394 | unsigned int rv = 0; | |
395 | ||
396 | for (struct nexthop *par = nexthop->rparent; par; par = par->rparent) | |
397 | rv++; | |
398 | ||
399 | return rv; | |
400 | } | |
401 | ||
402 | uint32_t nexthop_hash(const struct nexthop *nexthop) | |
403 | { | |
404 | uint32_t key = 0x45afe398; | |
405 | ||
406 | key = jhash_3words(nexthop->type, nexthop->vrf_id, | |
407 | nexthop->nh_label_type, key); | |
408 | /* gate and blackhole are together in a union */ | |
409 | key = jhash(&nexthop->gate, sizeof(nexthop->gate), key); | |
410 | key = jhash(&nexthop->src, sizeof(nexthop->src), key); | |
411 | key = jhash(&nexthop->rmap_src, sizeof(nexthop->rmap_src), key); | |
412 | ||
413 | if (nexthop->nh_label) { | |
414 | int labels = nexthop->nh_label->num_labels; | |
415 | int i = 0; | |
416 | ||
417 | while (labels >= 3) { | |
418 | key = jhash_3words(nexthop->nh_label->label[i], | |
419 | nexthop->nh_label->label[i + 1], | |
420 | nexthop->nh_label->label[i + 2], | |
421 | key); | |
422 | labels -= 3; | |
423 | i += 3; | |
424 | } | |
425 | ||
426 | if (labels >= 2) { | |
427 | key = jhash_2words(nexthop->nh_label->label[i], | |
428 | nexthop->nh_label->label[i + 1], | |
429 | key); | |
430 | labels -= 2; | |
431 | i += 2; | |
432 | } | |
433 | ||
434 | if (labels >= 1) | |
435 | key = jhash_1word(nexthop->nh_label->label[i], key); | |
436 | } | |
437 | ||
438 | switch (nexthop->type) { | |
439 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
440 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
441 | case NEXTHOP_TYPE_IFINDEX: | |
442 | key = jhash_1word(nexthop->ifindex, key); | |
443 | break; | |
444 | case NEXTHOP_TYPE_BLACKHOLE: | |
445 | case NEXTHOP_TYPE_IPV4: | |
446 | case NEXTHOP_TYPE_IPV6: | |
447 | break; | |
448 | } | |
449 | return key; | |
450 | } |