]>
Commit | Line | Data |
---|---|---|
ad28e79a SW |
1 | /* Zebra Nexthop Group Code. |
2 | * Copyright (C) 2019 Cumulus Networks, Inc. | |
3 | * Donald Sharp | |
4 | * Stephen Worley | |
5 | * | |
6 | * This file is part of FRR. | |
7 | * | |
8 | * FRR is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * FRR is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with FRR; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
21 | * 02111-1307, USA. | |
22 | */ | |
23 | #include <zebra.h> | |
24 | ||
25 | #include "lib/nexthop.h" | |
50d89650 | 26 | #include "lib/nexthop_group_private.h" |
ad28e79a | 27 | #include "lib/routemap.h" |
b43434ad | 28 | #include "lib/mpls.h" |
69171da2 | 29 | #include "lib/jhash.h" |
51d80884 | 30 | #include "lib/debug.h" |
ad28e79a SW |
31 | |
32 | #include "zebra/connected.h" | |
33 | #include "zebra/debug.h" | |
34 | #include "zebra/zebra_router.h" | |
5948f013 | 35 | #include "zebra/zebra_nhg_private.h" |
ad28e79a SW |
36 | #include "zebra/zebra_rnh.h" |
37 | #include "zebra/zebra_routemap.h" | |
51d80884 SW |
38 | #include "zebra/zebra_memory.h" |
39 | #include "zebra/zserv.h" | |
ad28e79a | 40 | #include "zebra/rt.h" |
d9f5b2f5 | 41 | #include "zebra_errors.h" |
0c8215cb | 42 | #include "zebra_dplane.h" |
fe593b78 | 43 | #include "zebra/interface.h" |
d9f5b2f5 | 44 | |
51d80884 | 45 | DEFINE_MTYPE_STATIC(ZEBRA, NHG, "Nexthop Group Entry"); |
a15d4c00 | 46 | DEFINE_MTYPE_STATIC(ZEBRA, NHG_CONNECTED, "Nexthop Group Connected"); |
e22e8001 | 47 | DEFINE_MTYPE_STATIC(ZEBRA, NHG_CTX, "Nexthop Group Context"); |
0c8215cb | 48 | |
38e40db1 SW |
49 | /* id counter to keep in sync with kernel */ |
50 | uint32_t id_counter; | |
51 | ||
7c99d51b MS |
52 | /* */ |
53 | static bool g_nexthops_enabled = true; | |
54 | ||
cb86eba3 MS |
55 | static struct nhg_hash_entry *depends_find(const struct nexthop *nh, |
56 | afi_t afi); | |
37c6708b | 57 | static void depends_add(struct nhg_connected_tree_head *head, |
5657e7e9 | 58 | struct nhg_hash_entry *depend); |
38e40db1 SW |
59 | static struct nhg_hash_entry * |
60 | depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh, | |
61 | afi_t afi); | |
62 | static struct nhg_hash_entry * | |
63 | depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id); | |
37c6708b | 64 | static void depends_decrement_free(struct nhg_connected_tree_head *head); |
0c8215cb | 65 | |
1d48702e MS |
66 | static struct nhg_backup_info * |
67 | nhg_backup_copy(const struct nhg_backup_info *orig); | |
68 | ||
e22e8001 | 69 | |
5948f013 | 70 | static void nhg_connected_free(struct nhg_connected *dep) |
0c8215cb | 71 | { |
a15d4c00 | 72 | XFREE(MTYPE_NHG_CONNECTED, dep); |
0c8215cb SW |
73 | } |
74 | ||
5948f013 | 75 | static struct nhg_connected *nhg_connected_new(struct nhg_hash_entry *nhe) |
0c8215cb | 76 | { |
a15d4c00 | 77 | struct nhg_connected *new = NULL; |
0c8215cb | 78 | |
a15d4c00 | 79 | new = XCALLOC(MTYPE_NHG_CONNECTED, sizeof(struct nhg_connected)); |
0c8215cb SW |
80 | new->nhe = nhe; |
81 | ||
82 | return new; | |
83 | } | |
84 | ||
37c6708b | 85 | void nhg_connected_tree_free(struct nhg_connected_tree_head *head) |
0c8215cb | 86 | { |
a15d4c00 | 87 | struct nhg_connected *rb_node_dep = NULL; |
0c8215cb | 88 | |
37c6708b | 89 | if (!nhg_connected_tree_is_empty(head)) { |
fec211ad | 90 | frr_each_safe(nhg_connected_tree, head, rb_node_dep) { |
37c6708b | 91 | nhg_connected_tree_del(head, rb_node_dep); |
fe593b78 SW |
92 | nhg_connected_free(rb_node_dep); |
93 | } | |
0c8215cb | 94 | } |
0c8215cb SW |
95 | } |
96 | ||
37c6708b | 97 | bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head *head) |
0c8215cb | 98 | { |
fec211ad | 99 | return nhg_connected_tree_count(head) ? false : true; |
0c8215cb SW |
100 | } |
101 | ||
98cda54a | 102 | struct nhg_connected * |
37c6708b | 103 | nhg_connected_tree_root(struct nhg_connected_tree_head *head) |
98cda54a | 104 | { |
37c6708b | 105 | return nhg_connected_tree_first(head); |
98cda54a SW |
106 | } |
107 | ||
5bf15faa SW |
108 | struct nhg_hash_entry * |
109 | nhg_connected_tree_del_nhe(struct nhg_connected_tree_head *head, | |
110 | struct nhg_hash_entry *depend) | |
0c8215cb | 111 | { |
a15d4c00 | 112 | struct nhg_connected lookup = {}; |
085304dc | 113 | struct nhg_connected *remove = NULL; |
5bf15faa | 114 | struct nhg_hash_entry *removed_nhe; |
0c8215cb SW |
115 | |
116 | lookup.nhe = depend; | |
3119f6a1 | 117 | |
085304dc | 118 | /* Lookup to find the element, then remove it */ |
37c6708b | 119 | remove = nhg_connected_tree_find(head, &lookup); |
085304dc | 120 | if (remove) |
5bf15faa SW |
121 | /* Re-returning here just in case this API changes.. |
122 | * the _del list api's are a bit undefined at the moment. | |
123 | * | |
124 | * So hopefully returning here will make it fail if the api | |
125 | * changes to something different than currently expected. | |
126 | */ | |
127 | remove = nhg_connected_tree_del(head, remove); | |
128 | ||
129 | /* If the entry was sucessfully removed, free the 'connected` struct */ | |
130 | if (remove) { | |
131 | removed_nhe = remove->nhe; | |
085304dc | 132 | nhg_connected_free(remove); |
5bf15faa SW |
133 | return removed_nhe; |
134 | } | |
135 | ||
136 | return NULL; | |
3119f6a1 SW |
137 | } |
138 | ||
5bf15faa SW |
139 | /* Assuming UNIQUE RB tree. If this changes, assumptions here about |
140 | * insertion need to change. | |
141 | */ | |
142 | struct nhg_hash_entry * | |
143 | nhg_connected_tree_add_nhe(struct nhg_connected_tree_head *head, | |
144 | struct nhg_hash_entry *depend) | |
3119f6a1 | 145 | { |
a15d4c00 | 146 | struct nhg_connected *new = NULL; |
0c8215cb | 147 | |
a15d4c00 | 148 | new = nhg_connected_new(depend); |
0c8215cb | 149 | |
5bf15faa SW |
150 | /* On success, NULL will be returned from the |
151 | * RB code. | |
152 | */ | |
153 | if (new && (nhg_connected_tree_add(head, new) == NULL)) | |
154 | return NULL; | |
155 | ||
156 | /* If it wasn't successful, it must be a duplicate. We enforce the | |
157 | * unique property for the `nhg_connected` tree. | |
158 | */ | |
159 | nhg_connected_free(new); | |
160 | ||
161 | return depend; | |
3119f6a1 SW |
162 | } |
163 | ||
37c6708b SW |
164 | static void |
165 | nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head *head) | |
32e29e79 SW |
166 | { |
167 | struct nhg_connected *rb_node_dep = NULL; | |
32e29e79 | 168 | |
fec211ad | 169 | frr_each_safe(nhg_connected_tree, head, rb_node_dep) { |
32e29e79 SW |
170 | zebra_nhg_decrement_ref(rb_node_dep->nhe); |
171 | } | |
172 | } | |
173 | ||
37c6708b SW |
174 | static void |
175 | nhg_connected_tree_increment_ref(struct nhg_connected_tree_head *head) | |
32e29e79 SW |
176 | { |
177 | struct nhg_connected *rb_node_dep = NULL; | |
178 | ||
fec211ad | 179 | frr_each(nhg_connected_tree, head, rb_node_dep) { |
32e29e79 SW |
180 | zebra_nhg_increment_ref(rb_node_dep->nhe); |
181 | } | |
182 | } | |
183 | ||
98cda54a SW |
184 | struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe) |
185 | { | |
186 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE) | |
187 | && !zebra_nhg_depends_is_empty(nhe)) { | |
37c6708b | 188 | nhe = nhg_connected_tree_root(&nhe->nhg_depends)->nhe; |
98cda54a SW |
189 | return zebra_nhg_resolve(nhe); |
190 | } | |
191 | ||
192 | return nhe; | |
193 | } | |
194 | ||
fe593b78 | 195 | unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe) |
a15d4c00 | 196 | { |
37c6708b | 197 | return nhg_connected_tree_count(&nhe->nhg_depends); |
a15d4c00 SW |
198 | } |
199 | ||
fe593b78 | 200 | bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe) |
a15d4c00 | 201 | { |
37c6708b | 202 | return nhg_connected_tree_is_empty(&nhe->nhg_depends); |
a15d4c00 SW |
203 | } |
204 | ||
5948f013 SW |
205 | static void zebra_nhg_depends_del(struct nhg_hash_entry *from, |
206 | struct nhg_hash_entry *depend) | |
3119f6a1 | 207 | { |
37c6708b | 208 | nhg_connected_tree_del_nhe(&from->nhg_depends, depend); |
3119f6a1 SW |
209 | } |
210 | ||
5948f013 | 211 | static void zebra_nhg_depends_init(struct nhg_hash_entry *nhe) |
148a0103 | 212 | { |
37c6708b | 213 | nhg_connected_tree_init(&nhe->nhg_depends); |
148a0103 SW |
214 | } |
215 | ||
fe593b78 SW |
216 | unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe) |
217 | { | |
37c6708b | 218 | return nhg_connected_tree_count(&nhe->nhg_dependents); |
fe593b78 SW |
219 | } |
220 | ||
5948f013 | 221 | |
fe593b78 SW |
222 | bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe) |
223 | { | |
37c6708b | 224 | return nhg_connected_tree_is_empty(&nhe->nhg_dependents); |
fe593b78 SW |
225 | } |
226 | ||
5948f013 SW |
227 | static void zebra_nhg_dependents_del(struct nhg_hash_entry *from, |
228 | struct nhg_hash_entry *dependent) | |
fe593b78 | 229 | { |
37c6708b | 230 | nhg_connected_tree_del_nhe(&from->nhg_dependents, dependent); |
fe593b78 SW |
231 | } |
232 | ||
5948f013 SW |
233 | static void zebra_nhg_dependents_add(struct nhg_hash_entry *to, |
234 | struct nhg_hash_entry *dependent) | |
fe593b78 | 235 | { |
37c6708b | 236 | nhg_connected_tree_add_nhe(&to->nhg_dependents, dependent); |
fe593b78 SW |
237 | } |
238 | ||
5948f013 | 239 | static void zebra_nhg_dependents_init(struct nhg_hash_entry *nhe) |
fe593b78 | 240 | { |
37c6708b | 241 | nhg_connected_tree_init(&nhe->nhg_dependents); |
fe593b78 SW |
242 | } |
243 | ||
21615102 SW |
244 | /* Release this nhe from anything depending on it */ |
245 | static void zebra_nhg_dependents_release(struct nhg_hash_entry *nhe) | |
246 | { | |
80286aa5 | 247 | struct nhg_connected *rb_node_dep = NULL; |
21615102 | 248 | |
80286aa5 SW |
249 | frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) { |
250 | zebra_nhg_depends_del(rb_node_dep->nhe, nhe); | |
251 | /* recheck validity of the dependent */ | |
252 | zebra_nhg_check_valid(rb_node_dep->nhe); | |
21615102 SW |
253 | } |
254 | } | |
255 | ||
5948f013 SW |
256 | /* Release this nhe from anything that it depends on */ |
257 | static void zebra_nhg_depends_release(struct nhg_hash_entry *nhe) | |
258 | { | |
259 | if (!zebra_nhg_depends_is_empty(nhe)) { | |
260 | struct nhg_connected *rb_node_dep = NULL; | |
261 | ||
262 | frr_each_safe(nhg_connected_tree, &nhe->nhg_depends, | |
263 | rb_node_dep) { | |
264 | zebra_nhg_dependents_del(rb_node_dep->nhe, nhe); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | ||
d9f5b2f5 SW |
270 | struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id) |
271 | { | |
0c8215cb | 272 | struct nhg_hash_entry lookup = {}; |
d9f5b2f5 SW |
273 | |
274 | lookup.id = id; | |
275 | return hash_lookup(zrouter.nhgs_id, &lookup); | |
276 | } | |
277 | ||
5948f013 | 278 | static int zebra_nhg_insert_id(struct nhg_hash_entry *nhe) |
d9f5b2f5 SW |
279 | { |
280 | if (hash_lookup(zrouter.nhgs_id, nhe)) { | |
281 | flog_err( | |
282 | EC_ZEBRA_NHG_TABLE_INSERT_FAILED, | |
283 | "Failed inserting NHG id=%u into the ID hash table, entry already exists", | |
284 | nhe->id); | |
285 | return -1; | |
286 | } | |
287 | ||
288 | hash_get(zrouter.nhgs_id, nhe, hash_alloc_intern); | |
289 | ||
290 | return 0; | |
291 | } | |
ad28e79a | 292 | |
5948f013 SW |
293 | static void zebra_nhg_set_if(struct nhg_hash_entry *nhe, struct interface *ifp) |
294 | { | |
295 | nhe->ifp = ifp; | |
296 | if_nhg_dependents_add(ifp, nhe); | |
297 | } | |
298 | ||
38e40db1 SW |
299 | static void |
300 | zebra_nhg_connect_depends(struct nhg_hash_entry *nhe, | |
377e29f7 | 301 | struct nhg_connected_tree_head *nhg_depends) |
4e49c8b8 | 302 | { |
a15d4c00 SW |
303 | struct nhg_connected *rb_node_dep = NULL; |
304 | ||
38e40db1 SW |
305 | /* This has been allocated higher above in the stack. Could probably |
306 | * re-allocate and free the old stuff but just using the same memory | |
307 | * for now. Otherwise, their might be a time trade-off for repeated | |
308 | * alloc/frees as startup. | |
309 | */ | |
377e29f7 | 310 | nhe->nhg_depends = *nhg_depends; |
4e49c8b8 | 311 | |
a15d4c00 | 312 | /* Attach backpointer to anything that it depends on */ |
fe593b78 | 313 | zebra_nhg_dependents_init(nhe); |
a15d4c00 | 314 | if (!zebra_nhg_depends_is_empty(nhe)) { |
fec211ad | 315 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
377e29f7 MS |
316 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
317 | zlog_debug("%s: nhe %p (%u), dep %p (%u)", | |
318 | __func__, nhe, nhe->id, | |
319 | rb_node_dep->nhe, | |
320 | rb_node_dep->nhe->id); | |
321 | ||
a15d4c00 SW |
322 | zebra_nhg_dependents_add(rb_node_dep->nhe, nhe); |
323 | } | |
324 | } | |
377e29f7 | 325 | } |
4e49c8b8 | 326 | |
377e29f7 MS |
327 | /* Init an nhe, for use in a hash lookup for example */ |
328 | void zebra_nhe_init(struct nhg_hash_entry *nhe, afi_t afi, | |
329 | const struct nexthop *nh) | |
330 | { | |
331 | memset(nhe, 0, sizeof(struct nhg_hash_entry)); | |
332 | nhe->vrf_id = VRF_DEFAULT; | |
333 | nhe->type = ZEBRA_ROUTE_NHG; | |
334 | nhe->afi = AFI_UNSPEC; | |
7b683a96 | 335 | |
377e29f7 MS |
336 | /* There are some special rules that apply to groups representing |
337 | * a single nexthop. | |
338 | */ | |
339 | if (nh && (nh->next == NULL)) { | |
340 | switch (nh->type) { | |
341 | case (NEXTHOP_TYPE_IFINDEX): | |
342 | case (NEXTHOP_TYPE_BLACKHOLE): | |
343 | /* | |
344 | * This switch case handles setting the afi different | |
345 | * for ipv4/v6 routes. Ifindex/blackhole nexthop | |
346 | * objects cannot be ambiguous, they must be Address | |
347 | * Family specific. If we get here, we will either use | |
348 | * the AF of the route, or the one we got passed from | |
349 | * here from the kernel. | |
350 | */ | |
351 | nhe->afi = afi; | |
352 | break; | |
353 | case (NEXTHOP_TYPE_IPV4_IFINDEX): | |
354 | case (NEXTHOP_TYPE_IPV4): | |
355 | nhe->afi = AFI_IP; | |
356 | break; | |
357 | case (NEXTHOP_TYPE_IPV6_IFINDEX): | |
358 | case (NEXTHOP_TYPE_IPV6): | |
359 | nhe->afi = AFI_IP6; | |
360 | break; | |
361 | } | |
7b683a96 | 362 | } |
38e40db1 SW |
363 | } |
364 | ||
0eb97b86 | 365 | struct nhg_hash_entry *zebra_nhg_alloc(void) |
38e40db1 SW |
366 | { |
367 | struct nhg_hash_entry *nhe; | |
368 | ||
369 | nhe = XCALLOC(MTYPE_NHG, sizeof(struct nhg_hash_entry)); | |
370 | ||
0eb97b86 MS |
371 | return nhe; |
372 | } | |
373 | ||
f727646a MS |
374 | /* |
375 | * Allocate new nhe and make shallow copy of 'orig'; no | |
376 | * recursive info is copied. | |
377 | */ | |
378 | struct nhg_hash_entry *zebra_nhe_copy(const struct nhg_hash_entry *orig, | |
379 | uint32_t id) | |
0eb97b86 MS |
380 | { |
381 | struct nhg_hash_entry *nhe; | |
382 | ||
383 | nhe = zebra_nhg_alloc(); | |
384 | ||
38e40db1 SW |
385 | nhe->id = id; |
386 | ||
1d48702e | 387 | nexthop_group_copy(&(nhe->nhg), &(orig->nhg)); |
38e40db1 | 388 | |
1d48702e MS |
389 | nhe->vrf_id = orig->vrf_id; |
390 | nhe->afi = orig->afi; | |
391 | nhe->type = orig->type ? orig->type : ZEBRA_ROUTE_NHG; | |
38e40db1 SW |
392 | nhe->refcnt = 0; |
393 | nhe->dplane_ref = zebra_router_get_next_sequence(); | |
394 | ||
1d48702e MS |
395 | /* Copy backup info also, if present */ |
396 | if (orig->backup_info) | |
397 | nhe->backup_info = nhg_backup_copy(orig->backup_info); | |
398 | ||
38e40db1 SW |
399 | return nhe; |
400 | } | |
401 | ||
402 | /* Allocation via hash handler */ | |
403 | static void *zebra_nhg_hash_alloc(void *arg) | |
404 | { | |
405 | struct nhg_hash_entry *nhe = NULL; | |
406 | struct nhg_hash_entry *copy = arg; | |
7b683a96 | 407 | |
f727646a | 408 | nhe = zebra_nhe_copy(copy, copy->id); |
9ef49038 SW |
409 | |
410 | /* Mark duplicate nexthops in a group at creation time. */ | |
c415d895 | 411 | nexthop_group_mark_duplicates(&(nhe->nhg)); |
9ef49038 | 412 | |
377e29f7 MS |
413 | zebra_nhg_connect_depends(nhe, &(copy->nhg_depends)); |
414 | ||
415 | /* Add the ifp now if it's not a group or recursive and has ifindex */ | |
416 | if (zebra_nhg_depends_is_empty(nhe) && nhe->nhg.nexthop | |
417 | && nhe->nhg.nexthop->ifindex) { | |
418 | struct interface *ifp = NULL; | |
419 | ||
420 | ifp = if_lookup_by_index(nhe->nhg.nexthop->ifindex, | |
421 | nhe->nhg.nexthop->vrf_id); | |
422 | if (ifp) | |
423 | zebra_nhg_set_if(nhe, ifp); | |
424 | else | |
425 | flog_err( | |
426 | EC_ZEBRA_IF_LOOKUP_FAILED, | |
427 | "Zebra failed to lookup an interface with ifindex=%d in vrf=%u for NHE id=%u", | |
428 | nhe->nhg.nexthop->ifindex, | |
429 | nhe->nhg.nexthop->vrf_id, nhe->id); | |
430 | } | |
431 | ||
d9f5b2f5 SW |
432 | zebra_nhg_insert_id(nhe); |
433 | ||
4e49c8b8 DS |
434 | return nhe; |
435 | } | |
436 | ||
4e49c8b8 DS |
437 | uint32_t zebra_nhg_hash_key(const void *arg) |
438 | { | |
439 | const struct nhg_hash_entry *nhe = arg; | |
1d48702e MS |
440 | uint32_t val, key = 0x5a351234; |
441 | ||
442 | val = nexthop_group_hash(&(nhe->nhg)); | |
443 | if (nhe->backup_info) { | |
444 | val = jhash_2words(val, | |
445 | nexthop_group_hash( | |
446 | &(nhe->backup_info->nhe->nhg)), | |
447 | key); | |
448 | } | |
d9f5b2f5 | 449 | |
1d48702e | 450 | key = jhash_3words(nhe->vrf_id, nhe->afi, val, key); |
d9f5b2f5 | 451 | |
d9f5b2f5 | 452 | return key; |
4e49c8b8 DS |
453 | } |
454 | ||
a95b8020 SW |
455 | uint32_t zebra_nhg_id_key(const void *arg) |
456 | { | |
457 | const struct nhg_hash_entry *nhe = arg; | |
458 | ||
459 | return nhe->id; | |
460 | } | |
461 | ||
1d48702e MS |
462 | /* Helper with common nhg/nhe nexthop comparison logic */ |
463 | static bool nhg_compare_nexthops(const struct nexthop *nh1, | |
464 | const struct nexthop *nh2) | |
465 | { | |
f924db49 | 466 | assert(nh1 != NULL && nh2 != NULL); |
1d48702e MS |
467 | |
468 | /* | |
469 | * We have to check the active flag of each individual one, | |
470 | * not just the overall active_num. This solves the special case | |
471 | * issue of a route with a nexthop group with one nexthop | |
472 | * resolving to itself and thus marking it inactive. If we | |
473 | * have two different routes each wanting to mark a different | |
474 | * nexthop inactive, they need to hash to two different groups. | |
475 | * | |
476 | * If we just hashed on num_active, they would hash the same | |
477 | * which is incorrect. | |
478 | * | |
479 | * ex) | |
480 | * 1.1.1.0/24 | |
481 | * -> 1.1.1.1 dummy1 (inactive) | |
482 | * -> 1.1.2.1 dummy2 | |
483 | * | |
484 | * 1.1.2.0/24 | |
485 | * -> 1.1.1.1 dummy1 | |
486 | * -> 1.1.2.1 dummy2 (inactive) | |
487 | * | |
488 | * Without checking each individual one, they would hash to | |
489 | * the same group and both have 1.1.1.1 dummy1 marked inactive. | |
490 | * | |
491 | */ | |
492 | if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_ACTIVE) | |
493 | != CHECK_FLAG(nh2->flags, NEXTHOP_FLAG_ACTIVE)) | |
494 | return false; | |
495 | ||
496 | if (!nexthop_same(nh1, nh2)) | |
497 | return false; | |
498 | ||
499 | return true; | |
500 | } | |
501 | ||
4e49c8b8 DS |
502 | bool zebra_nhg_hash_equal(const void *arg1, const void *arg2) |
503 | { | |
504 | const struct nhg_hash_entry *nhe1 = arg1; | |
505 | const struct nhg_hash_entry *nhe2 = arg2; | |
148813c2 SW |
506 | struct nexthop *nexthop1; |
507 | struct nexthop *nexthop2; | |
4e49c8b8 | 508 | |
98cda54a SW |
509 | /* No matter what if they equal IDs, assume equal */ |
510 | if (nhe1->id && nhe2->id && (nhe1->id == nhe2->id)) | |
511 | return true; | |
512 | ||
4e49c8b8 DS |
513 | if (nhe1->vrf_id != nhe2->vrf_id) |
514 | return false; | |
515 | ||
77b76fc9 SW |
516 | if (nhe1->afi != nhe2->afi) |
517 | return false; | |
518 | ||
1d48702e | 519 | /* Nexthops should be in-order, so we simply compare them in-place */ |
c415d895 | 520 | for (nexthop1 = nhe1->nhg.nexthop, nexthop2 = nhe2->nhg.nexthop; |
f924db49 | 521 | nexthop1 && nexthop2; |
148813c2 | 522 | nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) { |
148813c2 | 523 | |
1d48702e | 524 | if (!nhg_compare_nexthops(nexthop1, nexthop2)) |
148813c2 | 525 | return false; |
1d48702e | 526 | } |
148813c2 | 527 | |
f924db49 MS |
528 | /* Check for unequal list lengths */ |
529 | if (nexthop1 || nexthop2) | |
530 | return false; | |
531 | ||
1d48702e MS |
532 | /* If there's no backup info, comparison is done. */ |
533 | if ((nhe1->backup_info == NULL) && (nhe2->backup_info == NULL)) | |
534 | return true; | |
535 | ||
536 | /* Compare backup info also - test the easy things first */ | |
537 | if (nhe1->backup_info && (nhe2->backup_info == NULL)) | |
538 | return false; | |
539 | if (nhe2->backup_info && (nhe1->backup_info == NULL)) | |
540 | return false; | |
541 | ||
542 | /* Compare number of backups before actually comparing any */ | |
543 | for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop, | |
544 | nexthop2 = nhe2->backup_info->nhe->nhg.nexthop; | |
545 | nexthop1 && nexthop2; | |
546 | nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) { | |
547 | ; | |
548 | } | |
549 | ||
550 | /* Did we find the end of one list before the other? */ | |
551 | if (nexthop1 || nexthop2) | |
552 | return false; | |
553 | ||
554 | /* Have to compare the backup nexthops */ | |
555 | for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop, | |
556 | nexthop2 = nhe2->backup_info->nhe->nhg.nexthop; | |
f924db49 | 557 | nexthop1 && nexthop2; |
1d48702e | 558 | nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) { |
148813c2 | 559 | |
1d48702e | 560 | if (!nhg_compare_nexthops(nexthop1, nexthop2)) |
148813c2 SW |
561 | return false; |
562 | } | |
e4ac313b | 563 | |
4e49c8b8 DS |
564 | return true; |
565 | } | |
566 | ||
d9f5b2f5 | 567 | bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2) |
4e49c8b8 | 568 | { |
d9f5b2f5 SW |
569 | const struct nhg_hash_entry *nhe1 = arg1; |
570 | const struct nhg_hash_entry *nhe2 = arg2; | |
4e49c8b8 | 571 | |
d9f5b2f5 SW |
572 | return nhe1->id == nhe2->id; |
573 | } | |
4e49c8b8 | 574 | |
1b366e63 SW |
575 | static int zebra_nhg_process_grp(struct nexthop_group *nhg, |
576 | struct nhg_connected_tree_head *depends, | |
577 | struct nh_grp *grp, uint8_t count) | |
e22e8001 | 578 | { |
37c6708b | 579 | nhg_connected_tree_init(depends); |
e22e8001 SW |
580 | |
581 | for (int i = 0; i < count; i++) { | |
582 | struct nhg_hash_entry *depend = NULL; | |
583 | /* We do not care about nexthop_grp.weight at | |
584 | * this time. But we should figure out | |
585 | * how to adapt this to our code in | |
586 | * the future. | |
587 | */ | |
38e40db1 | 588 | depend = depends_find_id_add(depends, grp[i].id); |
e22e8001 | 589 | |
38e40db1 | 590 | if (!depend) { |
e22e8001 SW |
591 | flog_err( |
592 | EC_ZEBRA_NHG_SYNC, | |
593 | "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table", | |
594 | grp[i].id); | |
1b366e63 | 595 | return -1; |
e22e8001 | 596 | } |
38e40db1 SW |
597 | |
598 | /* | |
599 | * If this is a nexthop with its own group | |
600 | * dependencies, add them as well. Not sure its | |
601 | * even possible to have a group within a group | |
602 | * in the kernel. | |
603 | */ | |
604 | ||
c415d895 | 605 | copy_nexthops(&nhg->nexthop, depend->nhg.nexthop, NULL); |
e22e8001 | 606 | } |
1b366e63 SW |
607 | |
608 | return 0; | |
e22e8001 SW |
609 | } |
610 | ||
6384cbcb SW |
611 | static void handle_recursive_depend(struct nhg_connected_tree_head *nhg_depends, |
612 | struct nexthop *nh, afi_t afi) | |
613 | { | |
614 | struct nhg_hash_entry *depend = NULL; | |
615 | struct nexthop_group resolved_ng = {}; | |
616 | ||
1d049aba | 617 | resolved_ng.nexthop = nh; |
6384cbcb | 618 | |
377e29f7 MS |
619 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
620 | zlog_debug("%s: head %p, nh %pNHv", | |
621 | __func__, nhg_depends, nh); | |
622 | ||
6384cbcb | 623 | depend = zebra_nhg_rib_find(0, &resolved_ng, afi); |
a7e1b02d | 624 | |
377e29f7 MS |
625 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
626 | zlog_debug("%s: nh %pNHv => %p (%u)", | |
627 | __func__, nh, depend, | |
628 | depend ? depend->id : 0); | |
629 | ||
a7e1b02d SW |
630 | if (depend) |
631 | depends_add(nhg_depends, depend); | |
6384cbcb | 632 | } |
e22e8001 | 633 | |
377e29f7 MS |
634 | /* |
635 | * Lookup an nhe in the global hash, using data from another nhe. If 'lookup' | |
636 | * has an id value, that's used. Create a new global/shared nhe if not found. | |
637 | */ | |
638 | static bool zebra_nhe_find(struct nhg_hash_entry **nhe, /* return value */ | |
639 | struct nhg_hash_entry *lookup, | |
640 | struct nhg_connected_tree_head *nhg_depends, | |
641 | afi_t afi) | |
642 | { | |
643 | bool created = false; | |
644 | bool recursive = false; | |
0328a5bd | 645 | struct nhg_hash_entry *newnhe, *backup_nhe; |
377e29f7 MS |
646 | struct nexthop *nh = NULL; |
647 | ||
648 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
649 | zlog_debug("%s: id %u, lookup %p, vrf %d, type %d, depends %p", | |
650 | __func__, lookup->id, lookup, | |
651 | lookup->vrf_id, lookup->type, | |
652 | nhg_depends); | |
653 | ||
654 | if (lookup->id) | |
655 | (*nhe) = zebra_nhg_lookup_id(lookup->id); | |
656 | else | |
657 | (*nhe) = hash_lookup(zrouter.nhgs, lookup); | |
658 | ||
659 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
660 | zlog_debug("%s: lookup => %p (%u)", | |
661 | __func__, (*nhe), | |
662 | (*nhe) ? (*nhe)->id : 0); | |
663 | ||
664 | /* If we found an existing object, we're done */ | |
665 | if (*nhe) | |
666 | goto done; | |
667 | ||
668 | /* We're going to create/insert a new nhe: | |
669 | * assign the next global id value if necessary. | |
670 | */ | |
671 | if (lookup->id == 0) | |
672 | lookup->id = ++id_counter; | |
673 | newnhe = hash_get(zrouter.nhgs, lookup, zebra_nhg_hash_alloc); | |
674 | created = true; | |
675 | ||
676 | /* Mail back the new object */ | |
677 | *nhe = newnhe; | |
678 | ||
679 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
680 | zlog_debug("%s: => created %p (%u)", __func__, newnhe, | |
681 | newnhe->id); | |
682 | ||
683 | /* Only hash/lookup the depends if the first lookup | |
684 | * fails to find something. This should hopefully save a | |
685 | * lot of cycles for larger ecmp sizes. | |
686 | */ | |
687 | if (nhg_depends) { | |
688 | /* If you don't want to hash on each nexthop in the | |
689 | * nexthop group struct you can pass the depends | |
690 | * directly. Kernel-side we do this since it just looks | |
691 | * them up via IDs. | |
692 | */ | |
693 | zebra_nhg_connect_depends(newnhe, nhg_depends); | |
694 | goto done; | |
695 | } | |
696 | ||
697 | /* Prepare dependency relationships if this is not a | |
698 | * singleton nexthop. There are two cases: a single | |
699 | * recursive nexthop, where we need a relationship to the | |
700 | * resolving nexthop; or a group of nexthops, where we need | |
701 | * relationships with the corresponding singletons. | |
702 | */ | |
703 | zebra_nhg_depends_init(lookup); | |
704 | ||
705 | nh = newnhe->nhg.nexthop; | |
706 | ||
707 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE)) | |
708 | SET_FLAG(newnhe->flags, NEXTHOP_GROUP_VALID); | |
709 | ||
710 | if (nh->next == NULL) { | |
711 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) { | |
712 | /* Single recursive nexthop */ | |
713 | handle_recursive_depend(&newnhe->nhg_depends, | |
714 | nh->resolved, afi); | |
715 | recursive = true; | |
716 | } | |
717 | } else { | |
718 | /* List of nexthops */ | |
719 | for (nh = newnhe->nhg.nexthop; nh; nh = nh->next) { | |
720 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
721 | zlog_debug("%s: depends NH %pNHv %s", | |
722 | __func__, nh, | |
723 | CHECK_FLAG(nh->flags, | |
724 | NEXTHOP_FLAG_RECURSIVE) ? | |
725 | "(R)" : ""); | |
726 | ||
727 | depends_find_add(&newnhe->nhg_depends, nh, afi); | |
728 | } | |
729 | } | |
730 | ||
0328a5bd MS |
731 | if (recursive) |
732 | SET_FLAG((*nhe)->flags, NEXTHOP_GROUP_RECURSIVE); | |
733 | ||
377e29f7 MS |
734 | if (zebra_nhg_get_backup_nhg(newnhe) == NULL || |
735 | zebra_nhg_get_backup_nhg(newnhe)->nexthop == NULL) | |
0328a5bd MS |
736 | goto done; |
737 | ||
738 | /* If there are backup nexthops, add them to the backup | |
739 | * depends tree. The rules here are a little different. | |
740 | */ | |
741 | recursive = false; | |
742 | backup_nhe = newnhe->backup_info->nhe; | |
377e29f7 | 743 | |
0328a5bd | 744 | nh = backup_nhe->nhg.nexthop; |
377e29f7 MS |
745 | |
746 | /* Singleton recursive NH */ | |
747 | if (nh->next == NULL && | |
748 | CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) { | |
749 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
750 | zlog_debug("%s: backup depend NH %pNHv (R)", | |
751 | __func__, nh); | |
752 | ||
753 | /* Single recursive nexthop */ | |
0328a5bd | 754 | handle_recursive_depend(&backup_nhe->nhg_depends, |
377e29f7 MS |
755 | nh->resolved, afi); |
756 | recursive = true; | |
757 | } else { | |
758 | /* One or more backup NHs */ | |
759 | for (; nh; nh = nh->next) { | |
760 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
761 | zlog_debug("%s: backup depend NH %pNHv %s", | |
762 | __func__, nh, | |
763 | CHECK_FLAG(nh->flags, | |
764 | NEXTHOP_FLAG_RECURSIVE) ? | |
765 | "(R)" : ""); | |
766 | ||
0328a5bd | 767 | depends_find_add(&backup_nhe->nhg_depends, |
377e29f7 MS |
768 | nh, afi); |
769 | } | |
770 | } | |
771 | ||
377e29f7 | 772 | if (recursive) |
0328a5bd | 773 | SET_FLAG(backup_nhe->flags, NEXTHOP_GROUP_RECURSIVE); |
377e29f7 MS |
774 | |
775 | done: | |
776 | ||
777 | return created; | |
778 | } | |
779 | ||
780 | /* | |
781 | * Lookup or create an nhe, based on an nhg or an nhe id. | |
782 | */ | |
4505578b SW |
783 | static bool zebra_nhg_find(struct nhg_hash_entry **nhe, uint32_t id, |
784 | struct nexthop_group *nhg, | |
37c6708b | 785 | struct nhg_connected_tree_head *nhg_depends, |
38e40db1 | 786 | vrf_id_t vrf_id, afi_t afi, int type) |
a95b8020 | 787 | { |
0c8215cb | 788 | struct nhg_hash_entry lookup = {}; |
4505578b SW |
789 | bool created = false; |
790 | ||
1d48702e MS |
791 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
792 | zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p", | |
793 | __func__, id, nhg, vrf_id, type, | |
794 | nhg_depends); | |
795 | ||
377e29f7 MS |
796 | /* Use a temporary nhe and call into the superset/common code */ |
797 | lookup.id = id; | |
9a1588c4 | 798 | lookup.type = type ? type : ZEBRA_ROUTE_NHG; |
c415d895 | 799 | lookup.nhg = *nhg; |
e22e8001 | 800 | |
88cafda7 | 801 | lookup.vrf_id = vrf_id; |
c415d895 | 802 | if (lookup.nhg.nexthop->next) { |
6384cbcb SW |
803 | /* Groups can have all vrfs and AF's in them */ |
804 | lookup.afi = AFI_UNSPEC; | |
6384cbcb | 805 | } else { |
c415d895 | 806 | switch (lookup.nhg.nexthop->type) { |
4d21c7c0 SW |
807 | case (NEXTHOP_TYPE_IFINDEX): |
808 | case (NEXTHOP_TYPE_BLACKHOLE): | |
809 | /* | |
810 | * This switch case handles setting the afi different | |
811 | * for ipv4/v6 routes. Ifindex/blackhole nexthop | |
812 | * objects cannot be ambiguous, they must be Address | |
813 | * Family specific. If we get here, we will either use | |
814 | * the AF of the route, or the one we got passed from | |
815 | * here from the kernel. | |
816 | */ | |
817 | lookup.afi = afi; | |
818 | break; | |
819 | case (NEXTHOP_TYPE_IPV4_IFINDEX): | |
820 | case (NEXTHOP_TYPE_IPV4): | |
821 | lookup.afi = AFI_IP; | |
822 | break; | |
823 | case (NEXTHOP_TYPE_IPV6_IFINDEX): | |
824 | case (NEXTHOP_TYPE_IPV6): | |
825 | lookup.afi = AFI_IP6; | |
826 | break; | |
827 | } | |
6384cbcb | 828 | } |
a95b8020 | 829 | |
377e29f7 | 830 | created = zebra_nhe_find(nhe, &lookup, nhg_depends, afi); |
d9f5b2f5 | 831 | |
4505578b | 832 | return created; |
a95b8020 SW |
833 | } |
834 | ||
e22e8001 | 835 | /* Find/create a single nexthop */ |
6384cbcb SW |
836 | static struct nhg_hash_entry * |
837 | zebra_nhg_find_nexthop(uint32_t id, struct nexthop *nh, afi_t afi, int type) | |
3057df51 | 838 | { |
6384cbcb | 839 | struct nhg_hash_entry *nhe = NULL; |
e22e8001 | 840 | struct nexthop_group nhg = {}; |
88cafda7 | 841 | vrf_id_t vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nh->vrf_id; |
e22e8001 | 842 | |
0eb97b86 | 843 | nexthop_group_add_sorted(&nhg, nh); |
e22e8001 | 844 | |
88cafda7 | 845 | zebra_nhg_find(&nhe, id, &nhg, NULL, vrf_id, afi, type); |
8a507796 | 846 | |
377e29f7 MS |
847 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
848 | zlog_debug("%s: nh %pNHv => %p (%u)", | |
849 | __func__, nh, nhe, nhe ? nhe->id : 0); | |
850 | ||
6384cbcb | 851 | return nhe; |
e22e8001 SW |
852 | } |
853 | ||
10200d40 SW |
854 | static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx) |
855 | { | |
856 | return ctx->id; | |
857 | } | |
858 | ||
1b366e63 | 859 | static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status) |
e22e8001 SW |
860 | { |
861 | ctx->status = status; | |
862 | } | |
863 | ||
1b366e63 | 864 | static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx) |
e22e8001 SW |
865 | { |
866 | return ctx->status; | |
867 | } | |
868 | ||
869 | static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op) | |
870 | { | |
871 | ctx->op = op; | |
872 | } | |
873 | ||
874 | static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx) | |
875 | { | |
876 | return ctx->op; | |
877 | } | |
878 | ||
10200d40 SW |
879 | static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx) |
880 | { | |
881 | return ctx->vrf_id; | |
882 | } | |
883 | ||
884 | static int nhg_ctx_get_type(const struct nhg_ctx *ctx) | |
885 | { | |
886 | return ctx->type; | |
887 | } | |
888 | ||
889 | static int nhg_ctx_get_afi(const struct nhg_ctx *ctx) | |
890 | { | |
891 | return ctx->afi; | |
892 | } | |
893 | ||
894 | static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx) | |
895 | { | |
896 | return &ctx->u.nh; | |
897 | } | |
898 | ||
899 | static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx) | |
900 | { | |
901 | return ctx->count; | |
902 | } | |
903 | ||
904 | static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx) | |
905 | { | |
906 | return ctx->u.grp; | |
907 | } | |
908 | ||
99e7ab12 | 909 | static struct nhg_ctx *nhg_ctx_new(void) |
7c6d5f25 | 910 | { |
99e7ab12 | 911 | struct nhg_ctx *new; |
7c6d5f25 SW |
912 | |
913 | new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx)); | |
914 | ||
915 | return new; | |
916 | } | |
917 | ||
918 | static void nhg_ctx_free(struct nhg_ctx **ctx) | |
919 | { | |
920 | struct nexthop *nh; | |
921 | ||
922 | if (ctx == NULL) | |
923 | return; | |
924 | ||
925 | assert((*ctx) != NULL); | |
926 | ||
927 | if (nhg_ctx_get_count(*ctx)) | |
928 | goto done; | |
929 | ||
930 | nh = nhg_ctx_get_nh(*ctx); | |
931 | ||
932 | nexthop_del_labels(nh); | |
933 | ||
934 | done: | |
935 | XFREE(MTYPE_NHG_CTX, *ctx); | |
7c6d5f25 SW |
936 | } |
937 | ||
81505946 SW |
938 | static struct nhg_ctx *nhg_ctx_init(uint32_t id, struct nexthop *nh, |
939 | struct nh_grp *grp, vrf_id_t vrf_id, | |
940 | afi_t afi, int type, uint8_t count) | |
941 | { | |
942 | struct nhg_ctx *ctx = NULL; | |
943 | ||
944 | ctx = nhg_ctx_new(); | |
945 | ||
946 | ctx->id = id; | |
947 | ctx->vrf_id = vrf_id; | |
948 | ctx->afi = afi; | |
949 | ctx->type = type; | |
950 | ctx->count = count; | |
951 | ||
952 | if (count) | |
953 | /* Copy over the array */ | |
954 | memcpy(&ctx->u.grp, grp, count * sizeof(struct nh_grp)); | |
955 | else if (nh) | |
956 | ctx->u.nh = *nh; | |
957 | ||
958 | return ctx; | |
959 | } | |
960 | ||
c1da832a | 961 | static bool zebra_nhg_contains_unhashable(struct nhg_hash_entry *nhe) |
38e40db1 SW |
962 | { |
963 | struct nhg_connected *rb_node_dep = NULL; | |
964 | ||
fec211ad | 965 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
38e40db1 | 966 | if (CHECK_FLAG(rb_node_dep->nhe->flags, |
c1da832a | 967 | NEXTHOP_GROUP_UNHASHABLE)) |
38e40db1 SW |
968 | return true; |
969 | } | |
970 | ||
971 | return false; | |
972 | } | |
973 | ||
c1da832a | 974 | static void zebra_nhg_set_unhashable(struct nhg_hash_entry *nhe) |
38e40db1 | 975 | { |
c1da832a | 976 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE); |
38e40db1 SW |
977 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); |
978 | ||
cb3e512d MS |
979 | flog(LOG_INFO, |
980 | EC_ZEBRA_DUPLICATE_NHG_MESSAGE, | |
981 | "Nexthop Group with ID (%d) is a duplicate, therefore unhashable, ignoring", | |
982 | nhe->id); | |
38e40db1 SW |
983 | } |
984 | ||
80286aa5 SW |
985 | static void zebra_nhg_set_valid(struct nhg_hash_entry *nhe) |
986 | { | |
987 | struct nhg_connected *rb_node_dep; | |
988 | ||
989 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
990 | ||
991 | frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) | |
992 | zebra_nhg_set_valid(rb_node_dep->nhe); | |
993 | } | |
994 | ||
995 | static void zebra_nhg_set_invalid(struct nhg_hash_entry *nhe) | |
996 | { | |
997 | struct nhg_connected *rb_node_dep; | |
998 | ||
999 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
1000 | ||
1001 | /* Update validity of nexthops depending on it */ | |
1002 | frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) | |
1003 | zebra_nhg_check_valid(rb_node_dep->nhe); | |
1004 | } | |
1005 | ||
1006 | void zebra_nhg_check_valid(struct nhg_hash_entry *nhe) | |
1007 | { | |
1008 | struct nhg_connected *rb_node_dep = NULL; | |
1009 | bool valid = false; | |
1010 | ||
1011 | /* If anthing else in the group is valid, the group is valid */ | |
1012 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { | |
1013 | if (CHECK_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID)) { | |
1014 | valid = true; | |
1015 | goto done; | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | done: | |
1020 | if (valid) | |
1021 | zebra_nhg_set_valid(nhe); | |
1022 | else | |
1023 | zebra_nhg_set_invalid(nhe); | |
1024 | } | |
1025 | ||
1026 | ||
177e711d | 1027 | static void zebra_nhg_release(struct nhg_hash_entry *nhe) |
9a1588c4 | 1028 | { |
377e29f7 MS |
1029 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1030 | zlog_debug("%s: nhe %p (%u)", __func__, nhe, nhe->id); | |
1031 | ||
9a1588c4 SW |
1032 | /* Remove it from any lists it may be on */ |
1033 | zebra_nhg_depends_release(nhe); | |
1034 | zebra_nhg_dependents_release(nhe); | |
1035 | if (nhe->ifp) | |
1036 | if_nhg_dependents_del(nhe->ifp, nhe); | |
1037 | ||
1038 | /* | |
c1da832a | 1039 | * If its unhashable, we didn't store it here and have to be |
9a1588c4 SW |
1040 | * sure we don't clear one thats actually being used. |
1041 | */ | |
c1da832a | 1042 | if (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE)) |
9a1588c4 | 1043 | hash_release(zrouter.nhgs, nhe); |
9a1588c4 | 1044 | |
9a1588c4 SW |
1045 | hash_release(zrouter.nhgs_id, nhe); |
1046 | } | |
1047 | ||
9a1588c4 SW |
1048 | static void zebra_nhg_handle_uninstall(struct nhg_hash_entry *nhe) |
1049 | { | |
177e711d | 1050 | zebra_nhg_release(nhe); |
9a1588c4 SW |
1051 | zebra_nhg_free(nhe); |
1052 | } | |
1053 | ||
80286aa5 SW |
1054 | static void zebra_nhg_handle_install(struct nhg_hash_entry *nhe) |
1055 | { | |
1056 | /* Update validity of groups depending on it */ | |
1057 | struct nhg_connected *rb_node_dep; | |
1058 | ||
1059 | frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) | |
1060 | zebra_nhg_set_valid(rb_node_dep->nhe); | |
1061 | } | |
1062 | ||
9a1588c4 SW |
1063 | /* |
1064 | * The kernel/other program has changed the state of a nexthop object we are | |
1065 | * using. | |
1066 | */ | |
1067 | static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry *nhe, | |
1068 | bool is_delete) | |
1069 | { | |
1070 | if (nhe->refcnt) { | |
1071 | flog_err( | |
1072 | EC_ZEBRA_NHG_SYNC, | |
1073 | "Kernel %s a nexthop group with ID (%u) that we are still using for a route, sending it back down", | |
1074 | (is_delete ? "deleted" : "updated"), nhe->id); | |
1075 | ||
1076 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
1077 | zebra_nhg_install_kernel(nhe); | |
177e711d | 1078 | } else |
9a1588c4 | 1079 | zebra_nhg_handle_uninstall(nhe); |
9a1588c4 SW |
1080 | } |
1081 | ||
e22e8001 SW |
1082 | static int nhg_ctx_process_new(struct nhg_ctx *ctx) |
1083 | { | |
1084 | struct nexthop_group *nhg = NULL; | |
37c6708b | 1085 | struct nhg_connected_tree_head nhg_depends = {}; |
9a1588c4 | 1086 | struct nhg_hash_entry *lookup = NULL; |
3057df51 SW |
1087 | struct nhg_hash_entry *nhe = NULL; |
1088 | ||
10200d40 SW |
1089 | uint32_t id = nhg_ctx_get_id(ctx); |
1090 | uint8_t count = nhg_ctx_get_count(ctx); | |
1091 | vrf_id_t vrf_id = nhg_ctx_get_vrf_id(ctx); | |
1092 | int type = nhg_ctx_get_type(ctx); | |
1093 | afi_t afi = nhg_ctx_get_afi(ctx); | |
1094 | ||
1095 | lookup = zebra_nhg_lookup_id(id); | |
9a1588c4 | 1096 | |
377e29f7 MS |
1097 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1098 | zlog_debug("%s: id %u, count %d, lookup => %p", | |
1099 | __func__, id, count, lookup); | |
1100 | ||
9a1588c4 SW |
1101 | if (lookup) { |
1102 | /* This is already present in our table, hence an update | |
1103 | * that we did not initate. | |
1104 | */ | |
1105 | zebra_nhg_handle_kernel_state_change(lookup, false); | |
1106 | return 0; | |
1107 | } | |
1108 | ||
10200d40 | 1109 | if (nhg_ctx_get_count(ctx)) { |
e22e8001 | 1110 | nhg = nexthop_group_new(); |
1b366e63 SW |
1111 | if (zebra_nhg_process_grp(nhg, &nhg_depends, |
1112 | nhg_ctx_get_grp(ctx), count)) { | |
1113 | depends_decrement_free(&nhg_depends); | |
d3a35138 | 1114 | nexthop_group_delete(&nhg); |
fec211ad | 1115 | return -ENOENT; |
1b366e63 SW |
1116 | } |
1117 | ||
10200d40 SW |
1118 | if (!zebra_nhg_find(&nhe, id, nhg, &nhg_depends, vrf_id, type, |
1119 | afi)) | |
38e40db1 | 1120 | depends_decrement_free(&nhg_depends); |
4505578b | 1121 | |
e22e8001 | 1122 | /* These got copied over in zebra_nhg_alloc() */ |
d3a35138 | 1123 | nexthop_group_delete(&nhg); |
38e40db1 | 1124 | } else |
10200d40 SW |
1125 | nhe = zebra_nhg_find_nexthop(id, nhg_ctx_get_nh(ctx), afi, |
1126 | type); | |
e22e8001 SW |
1127 | |
1128 | if (nhe) { | |
10200d40 | 1129 | if (id != nhe->id) { |
38e40db1 SW |
1130 | struct nhg_hash_entry *kernel_nhe = NULL; |
1131 | ||
e22e8001 | 1132 | /* Duplicate but with different ID from |
2d3c57e6 SW |
1133 | * the kernel |
1134 | */ | |
e22e8001 SW |
1135 | |
1136 | /* The kernel allows duplicate nexthops | |
1137 | * as long as they have different IDs. | |
1138 | * We are ignoring those to prevent | |
1139 | * syncing problems with the kernel | |
1140 | * changes. | |
38e40db1 SW |
1141 | * |
1142 | * We maintain them *ONLY* in the ID hash table to | |
c1da832a SW |
1143 | * track them and set the flag to indicated |
1144 | * their attributes are unhashable. | |
38e40db1 SW |
1145 | */ |
1146 | ||
f727646a | 1147 | kernel_nhe = zebra_nhe_copy(nhe, id); |
377e29f7 MS |
1148 | |
1149 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1150 | zlog_debug("%s: copying kernel nhe (%u), dup of %u", | |
1151 | __func__, id, nhe->id); | |
1152 | ||
38e40db1 | 1153 | zebra_nhg_insert_id(kernel_nhe); |
c1da832a SW |
1154 | zebra_nhg_set_unhashable(kernel_nhe); |
1155 | } else if (zebra_nhg_contains_unhashable(nhe)) { | |
1156 | /* The group we got contains an unhashable/duplicated | |
1157 | * depend, so lets mark this group as unhashable as well | |
1158 | * and release it from the non-ID hash. | |
e22e8001 | 1159 | */ |
377e29f7 MS |
1160 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1161 | zlog_debug("%s: nhe %p (%u) unhashable", | |
1162 | __func__, nhe, nhe->id); | |
1163 | ||
38e40db1 | 1164 | hash_release(zrouter.nhgs, nhe); |
c1da832a | 1165 | zebra_nhg_set_unhashable(nhe); |
38e40db1 | 1166 | } else { |
e22e8001 | 1167 | /* It actually created a new nhe */ |
377e29f7 MS |
1168 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1169 | zlog_debug("%s: nhe %p (%u) is new", | |
1170 | __func__, nhe, nhe->id); | |
1171 | ||
38e40db1 SW |
1172 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); |
1173 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
e22e8001 SW |
1174 | } |
1175 | } else { | |
1176 | flog_err( | |
1177 | EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
1178 | "Zebra failed to find or create a nexthop hash entry for ID (%u)", | |
10200d40 | 1179 | id); |
e22e8001 SW |
1180 | return -1; |
1181 | } | |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
9a1588c4 SW |
1186 | static int nhg_ctx_process_del(struct nhg_ctx *ctx) |
1187 | { | |
1188 | struct nhg_hash_entry *nhe = NULL; | |
10200d40 | 1189 | uint32_t id = nhg_ctx_get_id(ctx); |
9a1588c4 | 1190 | |
10200d40 | 1191 | nhe = zebra_nhg_lookup_id(id); |
9a1588c4 SW |
1192 | |
1193 | if (!nhe) { | |
1194 | flog_warn( | |
1195 | EC_ZEBRA_BAD_NHG_MESSAGE, | |
1196 | "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table", | |
10200d40 | 1197 | id); |
81505946 | 1198 | return -1; |
9a1588c4 SW |
1199 | } |
1200 | ||
1201 | zebra_nhg_handle_kernel_state_change(nhe, true); | |
1202 | ||
1203 | return 0; | |
1204 | } | |
1205 | ||
7c6d5f25 | 1206 | static void nhg_ctx_fini(struct nhg_ctx **ctx) |
e22e8001 SW |
1207 | { |
1208 | /* | |
1209 | * Just freeing for now, maybe do something more in the future | |
1210 | * based on flag. | |
1211 | */ | |
1212 | ||
7134ba70 | 1213 | nhg_ctx_free(ctx); |
e22e8001 SW |
1214 | } |
1215 | ||
1b366e63 SW |
1216 | static int queue_add(struct nhg_ctx *ctx) |
1217 | { | |
1218 | /* If its queued or already processed do nothing */ | |
1219 | if (nhg_ctx_get_status(ctx) == NHG_CTX_QUEUED) | |
1220 | return 0; | |
1221 | ||
1222 | if (rib_queue_nhg_add(ctx)) { | |
1223 | nhg_ctx_set_status(ctx, NHG_CTX_FAILURE); | |
1224 | return -1; | |
1225 | } | |
1226 | ||
1227 | nhg_ctx_set_status(ctx, NHG_CTX_QUEUED); | |
1228 | ||
1229 | return 0; | |
1230 | } | |
1231 | ||
e22e8001 SW |
1232 | int nhg_ctx_process(struct nhg_ctx *ctx) |
1233 | { | |
1234 | int ret = 0; | |
1235 | ||
1236 | switch (nhg_ctx_get_op(ctx)) { | |
1237 | case NHG_CTX_OP_NEW: | |
1238 | ret = nhg_ctx_process_new(ctx); | |
fec211ad | 1239 | if (nhg_ctx_get_count(ctx) && ret == -ENOENT |
1b366e63 | 1240 | && nhg_ctx_get_status(ctx) != NHG_CTX_REQUEUED) { |
e1292378 SW |
1241 | /** |
1242 | * We have entered a situation where we are | |
1243 | * processing a group from the kernel | |
1244 | * that has a contained nexthop which | |
1245 | * we have not yet processed. | |
1b366e63 | 1246 | * |
e1292378 SW |
1247 | * Re-enqueue this ctx to be handled exactly one |
1248 | * more time (indicated by the flag). | |
1249 | * | |
1250 | * By the time we get back to it, we | |
1251 | * should have processed its depends. | |
1b366e63 SW |
1252 | */ |
1253 | nhg_ctx_set_status(ctx, NHG_CTX_NONE); | |
1254 | if (queue_add(ctx) == 0) { | |
1255 | nhg_ctx_set_status(ctx, NHG_CTX_REQUEUED); | |
1256 | return 0; | |
1257 | } | |
1258 | } | |
e22e8001 SW |
1259 | break; |
1260 | case NHG_CTX_OP_DEL: | |
9a1588c4 | 1261 | ret = nhg_ctx_process_del(ctx); |
e22e8001 SW |
1262 | case NHG_CTX_OP_NONE: |
1263 | break; | |
1264 | } | |
1265 | ||
1266 | nhg_ctx_set_status(ctx, (ret ? NHG_CTX_FAILURE : NHG_CTX_SUCCESS)); | |
1267 | ||
7c6d5f25 | 1268 | nhg_ctx_fini(&ctx); |
e22e8001 SW |
1269 | |
1270 | return ret; | |
1271 | } | |
3057df51 | 1272 | |
e22e8001 SW |
1273 | /* Kernel-side, you either get a single new nexthop or a array of ID's */ |
1274 | int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, struct nh_grp *grp, | |
38e40db1 SW |
1275 | uint8_t count, vrf_id_t vrf_id, afi_t afi, int type, |
1276 | int startup) | |
e22e8001 | 1277 | { |
e22e8001 SW |
1278 | struct nhg_ctx *ctx = NULL; |
1279 | ||
377e29f7 MS |
1280 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1281 | zlog_debug("%s: nh %pNHv, id %u, count %d", | |
1282 | __func__, nh, id, (int)count); | |
1283 | ||
38e40db1 SW |
1284 | if (id > id_counter) |
1285 | /* Increase our counter so we don't try to create | |
1286 | * an ID that already exists | |
1287 | */ | |
1288 | id_counter = id; | |
1289 | ||
81505946 | 1290 | ctx = nhg_ctx_init(id, nh, grp, vrf_id, afi, type, count); |
e22e8001 SW |
1291 | nhg_ctx_set_op(ctx, NHG_CTX_OP_NEW); |
1292 | ||
38e40db1 SW |
1293 | /* Under statup conditions, we need to handle them immediately |
1294 | * like we do for routes. Otherwise, we are going to get a route | |
1295 | * with a nhe_id that we have not handled. | |
1296 | */ | |
1297 | if (startup) | |
1298 | return nhg_ctx_process(ctx); | |
1299 | ||
e22e8001 | 1300 | if (queue_add(ctx)) { |
7c6d5f25 | 1301 | nhg_ctx_fini(&ctx); |
e22e8001 SW |
1302 | return -1; |
1303 | } | |
1304 | ||
1305 | return 0; | |
1306 | } | |
1307 | ||
9a1588c4 | 1308 | /* Kernel-side, received delete message */ |
88cafda7 | 1309 | int zebra_nhg_kernel_del(uint32_t id, vrf_id_t vrf_id) |
9a1588c4 SW |
1310 | { |
1311 | struct nhg_ctx *ctx = NULL; | |
1312 | ||
88cafda7 | 1313 | ctx = nhg_ctx_init(id, NULL, NULL, vrf_id, 0, 0, 0); |
9a1588c4 SW |
1314 | |
1315 | nhg_ctx_set_op(ctx, NHG_CTX_OP_DEL); | |
1316 | ||
1317 | if (queue_add(ctx)) { | |
7c6d5f25 | 1318 | nhg_ctx_fini(&ctx); |
9a1588c4 SW |
1319 | return -1; |
1320 | } | |
1321 | ||
1322 | return 0; | |
1323 | } | |
1324 | ||
5657e7e9 | 1325 | /* Some dependency helper functions */ |
0fff714e SW |
1326 | static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh, |
1327 | afi_t afi) | |
98cda54a | 1328 | { |
0fff714e SW |
1329 | struct nhg_hash_entry *nhe; |
1330 | struct nexthop *lookup = NULL; | |
98cda54a | 1331 | |
77bf9504 | 1332 | lookup = nexthop_dup(nh, NULL); |
0fff714e SW |
1333 | |
1334 | nhe = zebra_nhg_find_nexthop(0, lookup, afi, 0); | |
1335 | ||
1336 | nexthops_free(lookup); | |
1337 | ||
1338 | return nhe; | |
1339 | } | |
1340 | ||
1341 | static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh, | |
1342 | afi_t afi) | |
1343 | { | |
1344 | struct nhg_hash_entry *nhe; | |
1345 | struct nexthop lookup = {}; | |
606fa9e5 | 1346 | |
cb86eba3 MS |
1347 | /* Capture a snapshot of this single nh; it might be part of a list, |
1348 | * so we need to make a standalone copy. | |
1349 | */ | |
77bf9504 | 1350 | nexthop_copy_no_recurse(&lookup, nh, NULL); |
8a507796 | 1351 | |
cb86eba3 | 1352 | nhe = zebra_nhg_find_nexthop(0, &lookup, afi, 0); |
8a507796 | 1353 | |
cb86eba3 MS |
1354 | /* The copy may have allocated labels; free them if necessary. */ |
1355 | nexthop_del_labels(&lookup); | |
4505578b | 1356 | |
377e29f7 MS |
1357 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1358 | zlog_debug("%s: nh %pNHv => %p (%u)", | |
1359 | __func__, nh, nhe, nhe ? nhe->id : 0); | |
1360 | ||
0fff714e SW |
1361 | return nhe; |
1362 | } | |
1363 | ||
1364 | static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi) | |
1365 | { | |
1366 | struct nhg_hash_entry *nhe = NULL; | |
377e29f7 | 1367 | char rbuf[10]; |
0fff714e SW |
1368 | |
1369 | if (!nh) | |
1370 | goto done; | |
1371 | ||
1372 | /* We are separating these functions out to increase handling speed | |
1373 | * in the non-recursive case (by not alloc/freeing) | |
1374 | */ | |
377e29f7 | 1375 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) { |
0fff714e | 1376 | nhe = depends_find_recursive(nh, afi); |
377e29f7 MS |
1377 | strlcpy(rbuf, "(R)", sizeof(rbuf)); |
1378 | } else { | |
0fff714e | 1379 | nhe = depends_find_singleton(nh, afi); |
377e29f7 MS |
1380 | rbuf[0] = '\0'; |
1381 | } | |
1382 | ||
1383 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1384 | zlog_debug("%s: nh %pNHv %s => %p (%u)", | |
1385 | __func__, nh, rbuf, | |
1386 | nhe, nhe ? nhe->id : 0); | |
0fff714e | 1387 | |
606fa9e5 | 1388 | done: |
4505578b | 1389 | return nhe; |
98cda54a SW |
1390 | } |
1391 | ||
37c6708b | 1392 | static void depends_add(struct nhg_connected_tree_head *head, |
5657e7e9 SW |
1393 | struct nhg_hash_entry *depend) |
1394 | { | |
377e29f7 MS |
1395 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1396 | zlog_debug("%s: head %p nh %pNHv", | |
1397 | __func__, head, depend->nhg.nexthop); | |
1398 | ||
5bf15faa SW |
1399 | /* If NULL is returned, it was successfully added and |
1400 | * needs to have its refcnt incremented. | |
1401 | * | |
1402 | * Else the NHE is already present in the tree and doesn't | |
1403 | * need to increment the refcnt. | |
1404 | */ | |
1405 | if (nhg_connected_tree_add_nhe(head, depend) == NULL) | |
1406 | zebra_nhg_increment_ref(depend); | |
5657e7e9 SW |
1407 | } |
1408 | ||
38e40db1 SW |
1409 | static struct nhg_hash_entry * |
1410 | depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh, | |
1411 | afi_t afi) | |
5657e7e9 SW |
1412 | { |
1413 | struct nhg_hash_entry *depend = NULL; | |
1414 | ||
1415 | depend = depends_find(nh, afi); | |
1b366e63 | 1416 | |
1d48702e MS |
1417 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1418 | zlog_debug("%s: nh %pNHv => %p", | |
1419 | __func__, nh, depend); | |
1420 | ||
1b366e63 SW |
1421 | if (depend) |
1422 | depends_add(head, depend); | |
38e40db1 SW |
1423 | |
1424 | return depend; | |
1425 | } | |
1426 | ||
1427 | static struct nhg_hash_entry * | |
1428 | depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id) | |
1429 | { | |
1430 | struct nhg_hash_entry *depend = NULL; | |
1431 | ||
1432 | depend = zebra_nhg_lookup_id(id); | |
1b366e63 SW |
1433 | |
1434 | if (depend) | |
1435 | depends_add(head, depend); | |
38e40db1 SW |
1436 | |
1437 | return depend; | |
5657e7e9 SW |
1438 | } |
1439 | ||
37c6708b | 1440 | static void depends_decrement_free(struct nhg_connected_tree_head *head) |
5657e7e9 | 1441 | { |
37c6708b SW |
1442 | nhg_connected_tree_decrement_ref(head); |
1443 | nhg_connected_tree_free(head); | |
5657e7e9 SW |
1444 | } |
1445 | ||
377e29f7 | 1446 | /* Find an nhe based on a list of nexthops */ |
7f997721 SW |
1447 | struct nhg_hash_entry * |
1448 | zebra_nhg_rib_find(uint32_t id, struct nexthop_group *nhg, afi_t rt_afi) | |
e22e8001 SW |
1449 | { |
1450 | struct nhg_hash_entry *nhe = NULL; | |
88cafda7 DS |
1451 | vrf_id_t vrf_id; |
1452 | ||
1453 | /* | |
1454 | * CLANG SA is complaining that nexthop may be NULL | |
1455 | * Make it happy but this is ridonc | |
1456 | */ | |
1457 | assert(nhg->nexthop); | |
1458 | vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nhg->nexthop->vrf_id; | |
98cda54a | 1459 | |
88cafda7 | 1460 | zebra_nhg_find(&nhe, id, nhg, NULL, vrf_id, rt_afi, 0); |
4505578b | 1461 | |
377e29f7 MS |
1462 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1463 | zlog_debug("%s: => nhe %p (%u)", | |
1464 | __func__, nhe, nhe ? nhe->id : 0); | |
1465 | ||
1466 | return nhe; | |
1467 | } | |
1468 | ||
1469 | /* Find an nhe based on a route's nhe */ | |
1470 | struct nhg_hash_entry * | |
1471 | zebra_nhg_rib_find_nhe(struct nhg_hash_entry *rt_nhe, afi_t rt_afi) | |
1472 | { | |
1473 | struct nhg_hash_entry *nhe = NULL; | |
1474 | ||
1475 | if (!(rt_nhe && rt_nhe->nhg.nexthop)) { | |
1476 | flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
1477 | "No nexthop passed to %s", __func__); | |
1478 | return NULL; | |
1479 | } | |
1480 | ||
1481 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
f924db49 | 1482 | zlog_debug("%s: rt_nhe %p (%u)", __func__, rt_nhe, rt_nhe->id); |
377e29f7 MS |
1483 | |
1484 | zebra_nhe_find(&nhe, rt_nhe, NULL, rt_afi); | |
1485 | ||
1486 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1487 | zlog_debug("%s: => nhe %p (%u)", | |
1488 | __func__, nhe, nhe ? nhe->id : 0); | |
1489 | ||
3057df51 SW |
1490 | return nhe; |
1491 | } | |
1492 | ||
1d48702e MS |
1493 | /* |
1494 | * Allocate backup nexthop info object. Typically these are embedded in | |
1495 | * nhg_hash_entry objects. | |
1496 | */ | |
1497 | struct nhg_backup_info *zebra_nhg_backup_alloc(void) | |
1498 | { | |
1499 | struct nhg_backup_info *p; | |
1500 | ||
1501 | p = XCALLOC(MTYPE_NHG, sizeof(struct nhg_backup_info)); | |
1502 | ||
1503 | p->nhe = zebra_nhg_alloc(); | |
1504 | ||
1505 | /* Identify the embedded group used to hold the list of backups */ | |
1506 | SET_FLAG(p->nhe->flags, NEXTHOP_GROUP_BACKUP); | |
1507 | ||
1508 | return p; | |
1509 | } | |
1510 | ||
1511 | /* | |
1512 | * Free backup nexthop info object, deal with any embedded allocations | |
1513 | */ | |
1514 | void zebra_nhg_backup_free(struct nhg_backup_info **p) | |
1515 | { | |
1516 | if (p && *p) { | |
1517 | if ((*p)->nhe) | |
1518 | zebra_nhg_free((*p)->nhe); | |
1519 | ||
1520 | XFREE(MTYPE_NHG, (*p)); | |
1521 | } | |
1522 | } | |
1523 | ||
1d48702e MS |
1524 | /* Accessor for backup nexthop group */ |
1525 | struct nexthop_group *zebra_nhg_get_backup_nhg(struct nhg_hash_entry *nhe) | |
1526 | { | |
1527 | struct nexthop_group *p = NULL; | |
1528 | ||
1529 | if (nhe) { | |
1530 | if (nhe->backup_info && nhe->backup_info->nhe) | |
1531 | p = &(nhe->backup_info->nhe->nhg); | |
1532 | } | |
1533 | ||
1534 | return p; | |
1535 | } | |
1536 | ||
1537 | /* | |
1538 | * Helper to return a copy of a backup_info - note that this is a shallow | |
1539 | * copy, meant to be used when creating a new nhe from info passed in with | |
1540 | * a route e.g. | |
1541 | */ | |
1542 | static struct nhg_backup_info * | |
1543 | nhg_backup_copy(const struct nhg_backup_info *orig) | |
1544 | { | |
1545 | struct nhg_backup_info *b; | |
1546 | ||
1547 | b = zebra_nhg_backup_alloc(); | |
1548 | ||
1549 | /* Copy list of nexthops */ | |
1550 | nexthop_group_copy(&(b->nhe->nhg), &(orig->nhe->nhg)); | |
1551 | ||
1552 | return b; | |
1553 | } | |
1554 | ||
5948f013 | 1555 | static void zebra_nhg_free_members(struct nhg_hash_entry *nhe) |
b599cd2a | 1556 | { |
c415d895 MS |
1557 | nexthops_free(nhe->nhg.nexthop); |
1558 | ||
1d48702e MS |
1559 | zebra_nhg_backup_free(&nhe->backup_info); |
1560 | ||
58396544 | 1561 | /* Decrement to remove connection ref */ |
37c6708b SW |
1562 | nhg_connected_tree_decrement_ref(&nhe->nhg_depends); |
1563 | nhg_connected_tree_free(&nhe->nhg_depends); | |
1564 | nhg_connected_tree_free(&nhe->nhg_dependents); | |
b599cd2a SW |
1565 | } |
1566 | ||
0eb97b86 | 1567 | void zebra_nhg_free(struct nhg_hash_entry *nhe) |
a95b8020 | 1568 | { |
377e29f7 MS |
1569 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) { |
1570 | /* Group or singleton? */ | |
1571 | if (nhe->nhg.nexthop && nhe->nhg.nexthop->next) | |
1572 | zlog_debug("%s: nhe %p (%u), refcnt %d", | |
f924db49 | 1573 | __func__, nhe, nhe->id, nhe->refcnt); |
377e29f7 MS |
1574 | else |
1575 | zlog_debug("%s: nhe %p (%u), refcnt %d, NH %pNHv", | |
f924db49 | 1576 | __func__, nhe, nhe->id, nhe->refcnt, |
377e29f7 MS |
1577 | nhe->nhg.nexthop); |
1578 | } | |
1579 | ||
38e40db1 SW |
1580 | if (nhe->refcnt) |
1581 | zlog_debug("nhe_id=%u hash refcnt=%d", nhe->id, nhe->refcnt); | |
1582 | ||
8e401b25 | 1583 | zebra_nhg_free_members(nhe); |
51d80884 SW |
1584 | |
1585 | XFREE(MTYPE_NHG, nhe); | |
a95b8020 SW |
1586 | } |
1587 | ||
0eb97b86 MS |
1588 | void zebra_nhg_hash_free(void *p) |
1589 | { | |
1590 | zebra_nhg_free((struct nhg_hash_entry *)p); | |
1591 | } | |
1592 | ||
d9f5b2f5 SW |
1593 | void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe) |
1594 | { | |
377e29f7 MS |
1595 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1596 | zlog_debug("%s: nhe %p (%u) %d => %d", | |
1597 | __func__, nhe, nhe->id, nhe->refcnt, | |
1598 | nhe->refcnt - 1); | |
1599 | ||
e22e8001 SW |
1600 | nhe->refcnt--; |
1601 | ||
32e29e79 | 1602 | if (!zebra_nhg_depends_is_empty(nhe)) |
37c6708b | 1603 | nhg_connected_tree_decrement_ref(&nhe->nhg_depends); |
f54ef6a5 | 1604 | |
38e40db1 | 1605 | if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0) |
cb50cbc9 | 1606 | zebra_nhg_uninstall_kernel(nhe); |
7fd392cc SW |
1607 | } |
1608 | ||
7fd392cc SW |
1609 | void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe) |
1610 | { | |
377e29f7 MS |
1611 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1612 | zlog_debug("%s: nhe %p (%u) %d => %d", | |
1613 | __func__, nhe, nhe->id, nhe->refcnt, | |
1614 | nhe->refcnt + 1); | |
1615 | ||
e22e8001 SW |
1616 | nhe->refcnt++; |
1617 | ||
32e29e79 | 1618 | if (!zebra_nhg_depends_is_empty(nhe)) |
37c6708b | 1619 | nhg_connected_tree_increment_ref(&nhe->nhg_depends); |
e22e8001 | 1620 | } |
d9f5b2f5 | 1621 | |
ad28e79a SW |
1622 | static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop, |
1623 | struct nexthop *nexthop) | |
1624 | { | |
1625 | struct nexthop *resolved_hop; | |
b43434ad SW |
1626 | uint8_t num_labels = 0; |
1627 | mpls_label_t labels[MPLS_MAX_LABELS]; | |
1628 | enum lsp_types_t label_type = ZEBRA_LSP_NONE; | |
1629 | int i = 0; | |
ad28e79a SW |
1630 | |
1631 | resolved_hop = nexthop_new(); | |
1632 | SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); | |
1633 | ||
1634 | resolved_hop->vrf_id = nexthop->vrf_id; | |
1635 | switch (newhop->type) { | |
1636 | case NEXTHOP_TYPE_IPV4: | |
1637 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1638 | /* If the resolving route specifies a gateway, use it */ | |
1639 | resolved_hop->type = newhop->type; | |
1640 | resolved_hop->gate.ipv4 = newhop->gate.ipv4; | |
1641 | ||
1642 | if (newhop->ifindex) { | |
1643 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
1644 | resolved_hop->ifindex = newhop->ifindex; | |
1645 | } | |
1646 | break; | |
1647 | case NEXTHOP_TYPE_IPV6: | |
1648 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1649 | resolved_hop->type = newhop->type; | |
1650 | resolved_hop->gate.ipv6 = newhop->gate.ipv6; | |
1651 | ||
1652 | if (newhop->ifindex) { | |
1653 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1654 | resolved_hop->ifindex = newhop->ifindex; | |
1655 | } | |
1656 | break; | |
1657 | case NEXTHOP_TYPE_IFINDEX: | |
1658 | /* If the resolving route is an interface route, | |
1659 | * it means the gateway we are looking up is connected | |
1660 | * to that interface. (The actual network is _not_ onlink). | |
1661 | * Therefore, the resolved route should have the original | |
1662 | * gateway as nexthop as it is directly connected. | |
1663 | * | |
1664 | * On Linux, we have to set the onlink netlink flag because | |
1665 | * otherwise, the kernel won't accept the route. | |
1666 | */ | |
1667 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
1668 | if (afi == AFI_IP) { | |
1669 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
1670 | resolved_hop->gate.ipv4 = nexthop->gate.ipv4; | |
1671 | } else if (afi == AFI_IP6) { | |
1672 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1673 | resolved_hop->gate.ipv6 = nexthop->gate.ipv6; | |
1674 | } | |
1675 | resolved_hop->ifindex = newhop->ifindex; | |
1676 | break; | |
1677 | case NEXTHOP_TYPE_BLACKHOLE: | |
1678 | resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE; | |
2dc359a6 | 1679 | resolved_hop->bh_type = newhop->bh_type; |
ad28e79a SW |
1680 | break; |
1681 | } | |
1682 | ||
1683 | if (newhop->flags & NEXTHOP_FLAG_ONLINK) | |
1684 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
1685 | ||
b43434ad SW |
1686 | /* Copy labels of the resolved route and the parent resolving to it */ |
1687 | if (newhop->nh_label) { | |
6bc5d977 MS |
1688 | for (i = 0; i < newhop->nh_label->num_labels; i++) { |
1689 | /* Be a bit picky about overrunning the local array */ | |
1690 | if (num_labels >= MPLS_MAX_LABELS) { | |
1691 | if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB) | |
1692 | zlog_debug("%s: too many labels in newhop %pNHv", | |
1693 | __func__, newhop); | |
1694 | break; | |
1695 | } | |
b43434ad | 1696 | labels[num_labels++] = newhop->nh_label->label[i]; |
6bc5d977 MS |
1697 | } |
1698 | /* Use the "outer" type */ | |
b43434ad SW |
1699 | label_type = newhop->nh_label_type; |
1700 | } | |
1701 | ||
1702 | if (nexthop->nh_label) { | |
6bc5d977 MS |
1703 | for (i = 0; i < nexthop->nh_label->num_labels; i++) { |
1704 | /* Be a bit picky about overrunning the local array */ | |
1705 | if (num_labels >= MPLS_MAX_LABELS) { | |
1706 | if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB) | |
1707 | zlog_debug("%s: too many labels in nexthop %pNHv", | |
1708 | __func__, nexthop); | |
1709 | break; | |
1710 | } | |
b43434ad | 1711 | labels[num_labels++] = nexthop->nh_label->label[i]; |
6bc5d977 | 1712 | } |
b43434ad | 1713 | |
6bc5d977 MS |
1714 | /* If the parent has labels, use its type if |
1715 | * we don't already have one. | |
1716 | */ | |
1717 | if (label_type == ZEBRA_LSP_NONE) | |
1718 | label_type = nexthop->nh_label_type; | |
b43434ad SW |
1719 | } |
1720 | ||
1721 | if (num_labels) | |
1722 | nexthop_add_labels(resolved_hop, label_type, num_labels, | |
1723 | labels); | |
ad28e79a SW |
1724 | |
1725 | resolved_hop->rparent = nexthop; | |
50d89650 | 1726 | _nexthop_add(&nexthop->resolved, resolved_hop); |
ad28e79a SW |
1727 | } |
1728 | ||
6913cb1b SW |
1729 | /* Checks if nexthop we are trying to resolve to is valid */ |
1730 | static bool nexthop_valid_resolve(const struct nexthop *nexthop, | |
1731 | const struct nexthop *resolved) | |
1732 | { | |
1733 | /* Can't resolve to a recursive nexthop */ | |
1734 | if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_RECURSIVE)) | |
1735 | return false; | |
1736 | ||
1737 | switch (nexthop->type) { | |
1738 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1739 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1740 | /* If the nexthop we are resolving to does not match the | |
1741 | * ifindex for the nexthop the route wanted, its not valid. | |
1742 | */ | |
1743 | if (nexthop->ifindex != resolved->ifindex) | |
1744 | return false; | |
1745 | break; | |
1746 | case NEXTHOP_TYPE_IPV4: | |
1747 | case NEXTHOP_TYPE_IPV6: | |
1748 | case NEXTHOP_TYPE_IFINDEX: | |
1749 | case NEXTHOP_TYPE_BLACKHOLE: | |
1750 | break; | |
1751 | } | |
1752 | ||
1753 | return true; | |
1754 | } | |
1755 | ||
ad28e79a SW |
1756 | /* |
1757 | * Given a nexthop we need to properly recursively resolve | |
1758 | * the route. As such, do a table lookup to find and match | |
98cda54a SW |
1759 | * if at all possible. Set the nexthop->ifindex and resolved_id |
1760 | * as appropriate | |
ad28e79a SW |
1761 | */ |
1762 | static int nexthop_active(afi_t afi, struct route_entry *re, | |
8a507796 | 1763 | struct nexthop *nexthop, struct route_node *top) |
ad28e79a SW |
1764 | { |
1765 | struct prefix p; | |
1766 | struct route_table *table; | |
1767 | struct route_node *rn; | |
1768 | struct route_entry *match = NULL; | |
1769 | int resolved; | |
1770 | struct nexthop *newhop; | |
1771 | struct interface *ifp; | |
1772 | rib_dest_t *dest; | |
5a0bdc78 | 1773 | struct zebra_vrf *zvrf; |
ad28e79a SW |
1774 | |
1775 | if ((nexthop->type == NEXTHOP_TYPE_IPV4) | |
1776 | || nexthop->type == NEXTHOP_TYPE_IPV6) | |
1777 | nexthop->ifindex = 0; | |
1778 | ||
8a507796 | 1779 | |
ad28e79a SW |
1780 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
1781 | nexthops_free(nexthop->resolved); | |
1782 | nexthop->resolved = NULL; | |
1783 | re->nexthop_mtu = 0; | |
1784 | ||
377e29f7 MS |
1785 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1786 | zlog_debug("%s: re %p, nexthop %pNHv", | |
1787 | __func__, re, nexthop); | |
1788 | ||
ad28e79a | 1789 | /* |
a8c427ee | 1790 | * If the kernel has sent us a NEW route, then |
ad28e79a | 1791 | * by golly gee whiz it's a good route. |
a8c427ee SW |
1792 | * |
1793 | * If its an already INSTALLED route we have already handled, then the | |
1794 | * kernel route's nexthop might have became unreachable | |
1795 | * and we have to handle that. | |
ad28e79a | 1796 | */ |
a8c427ee SW |
1797 | if (!CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) |
1798 | && (re->type == ZEBRA_ROUTE_KERNEL | |
1799 | || re->type == ZEBRA_ROUTE_SYSTEM)) | |
ad28e79a SW |
1800 | return 1; |
1801 | ||
1802 | /* | |
12b4d77b | 1803 | * If the nexthop has been marked as 'onlink' we just need to make |
1804 | * sure the nexthop's interface is known and is operational. | |
ad28e79a SW |
1805 | */ |
1806 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) { | |
1807 | ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id); | |
1808 | if (!ifp) { | |
12b4d77b | 1809 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1810 | zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist", | |
1811 | nexthop, nexthop->ifindex); | |
ad28e79a SW |
1812 | return 0; |
1813 | } | |
12b4d77b | 1814 | if (!if_is_operative(ifp)) { |
1815 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1816 | zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational", | |
1817 | nexthop, ifp->name); | |
2d3c57e6 | 1818 | return 0; |
ad28e79a | 1819 | } |
12b4d77b | 1820 | return 1; |
ad28e79a SW |
1821 | } |
1822 | ||
4dcc2276 DS |
1823 | if ((top->p.family == AF_INET && top->p.prefixlen == 32 |
1824 | && nexthop->gate.ipv4.s_addr == top->p.u.prefix4.s_addr) | |
1825 | || (top->p.family == AF_INET6 && top->p.prefixlen == 128 | |
1826 | && memcmp(&nexthop->gate.ipv6, &top->p.u.prefix6, 16) == 0)) { | |
1827 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
1828 | zlog_debug( | |
d6951e5e | 1829 | " :%s: Attempting to install a max prefixlength route through itself", |
5e81f5dd | 1830 | __func__); |
4dcc2276 DS |
1831 | return 0; |
1832 | } | |
1833 | ||
ad28e79a SW |
1834 | /* Make lookup prefix. */ |
1835 | memset(&p, 0, sizeof(struct prefix)); | |
1836 | switch (afi) { | |
1837 | case AFI_IP: | |
1838 | p.family = AF_INET; | |
1839 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
1840 | p.u.prefix4 = nexthop->gate.ipv4; | |
1841 | break; | |
1842 | case AFI_IP6: | |
1843 | p.family = AF_INET6; | |
1844 | p.prefixlen = IPV6_MAX_PREFIXLEN; | |
1845 | p.u.prefix6 = nexthop->gate.ipv6; | |
1846 | break; | |
1847 | default: | |
1848 | assert(afi != AFI_IP && afi != AFI_IP6); | |
1849 | break; | |
1850 | } | |
1851 | /* Lookup table. */ | |
1852 | table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id); | |
5a0bdc78 PG |
1853 | /* get zvrf */ |
1854 | zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id); | |
1855 | if (!table || !zvrf) { | |
ad28e79a | 1856 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
d6951e5e | 1857 | zlog_debug(" %s: Table not found", __func__); |
ad28e79a SW |
1858 | return 0; |
1859 | } | |
1860 | ||
1861 | rn = route_node_match(table, (struct prefix *)&p); | |
1862 | while (rn) { | |
1863 | route_unlock_node(rn); | |
1864 | ||
1865 | /* Lookup should halt if we've matched against ourselves ('top', | |
1866 | * if specified) - i.e., we cannot have a nexthop NH1 is | |
1867 | * resolved by a route NH1. The exception is if the route is a | |
1868 | * host route. | |
1869 | */ | |
92756825 | 1870 | if (rn == top) |
ad28e79a SW |
1871 | if (((afi == AFI_IP) && (rn->p.prefixlen != 32)) |
1872 | || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) { | |
1873 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
1874 | zlog_debug( | |
d6951e5e | 1875 | " %s: Matched against ourself and prefix length is not max bit length", |
5e81f5dd | 1876 | __func__); |
ad28e79a SW |
1877 | return 0; |
1878 | } | |
1879 | ||
1880 | /* Pick up selected route. */ | |
1881 | /* However, do not resolve over default route unless explicitly | |
2d3c57e6 SW |
1882 | * allowed. |
1883 | */ | |
ad28e79a | 1884 | if (is_default_prefix(&rn->p) |
5a0bdc78 | 1885 | && !rnh_resolve_via_default(zvrf, p.family)) { |
ad28e79a SW |
1886 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1887 | zlog_debug( | |
d6951e5e | 1888 | " :%s: Resolved against default route", |
5e81f5dd | 1889 | __func__); |
ad28e79a SW |
1890 | return 0; |
1891 | } | |
1892 | ||
1893 | dest = rib_dest_from_rnode(rn); | |
1894 | if (dest && dest->selected_fib | |
1895 | && !CHECK_FLAG(dest->selected_fib->status, | |
1896 | ROUTE_ENTRY_REMOVED) | |
1897 | && dest->selected_fib->type != ZEBRA_ROUTE_TABLE) | |
1898 | match = dest->selected_fib; | |
1899 | ||
1900 | /* If there is no selected route or matched route is EGP, go up | |
2d3c57e6 SW |
1901 | * tree. |
1902 | */ | |
ad28e79a SW |
1903 | if (!match) { |
1904 | do { | |
1905 | rn = rn->parent; | |
1906 | } while (rn && rn->info == NULL); | |
1907 | if (rn) | |
1908 | route_lock_node(rn); | |
1909 | ||
1910 | continue; | |
1911 | } | |
1912 | ||
1913 | if (match->type == ZEBRA_ROUTE_CONNECT) { | |
1914 | /* Directly point connected route. */ | |
c415d895 | 1915 | newhop = match->nhe->nhg.nexthop; |
ad28e79a SW |
1916 | if (newhop) { |
1917 | if (nexthop->type == NEXTHOP_TYPE_IPV4 | |
1918 | || nexthop->type == NEXTHOP_TYPE_IPV6) | |
1919 | nexthop->ifindex = newhop->ifindex; | |
1920 | } | |
377e29f7 MS |
1921 | |
1922 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1923 | zlog_debug("%s: CONNECT match %p (%u), newhop %pNHv", | |
1924 | __func__, match, | |
1925 | match->nhe->id, newhop); | |
1926 | ||
ad28e79a SW |
1927 | return 1; |
1928 | } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) { | |
1929 | resolved = 0; | |
c415d895 | 1930 | for (ALL_NEXTHOPS(match->nhe->nhg, newhop)) { |
ad28e79a SW |
1931 | if (!CHECK_FLAG(match->status, |
1932 | ROUTE_ENTRY_INSTALLED)) | |
1933 | continue; | |
6913cb1b | 1934 | if (!nexthop_valid_resolve(nexthop, newhop)) |
ad28e79a SW |
1935 | continue; |
1936 | ||
377e29f7 MS |
1937 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1938 | zlog_debug("%s: RECURSIVE match %p (%u), newhop %pNHv", | |
1939 | __func__, match, | |
1940 | match->nhe->id, newhop); | |
1941 | ||
ad28e79a SW |
1942 | SET_FLAG(nexthop->flags, |
1943 | NEXTHOP_FLAG_RECURSIVE); | |
ad28e79a SW |
1944 | nexthop_set_resolved(afi, newhop, nexthop); |
1945 | resolved = 1; | |
1946 | } | |
f924db49 | 1947 | |
8a507796 | 1948 | if (resolved) |
ad28e79a | 1949 | re->nexthop_mtu = match->mtu; |
f924db49 | 1950 | else if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
d6951e5e DL |
1951 | zlog_debug( |
1952 | " %s: Recursion failed to find", | |
1953 | __func__); | |
f924db49 | 1954 | |
ad28e79a | 1955 | return resolved; |
ad28e79a SW |
1956 | } else { |
1957 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
1958 | zlog_debug( | |
d6951e5e | 1959 | " %s: Route Type %s has not turned on recursion", |
5e81f5dd | 1960 | __func__, zebra_route_string(re->type)); |
ad28e79a SW |
1961 | if (re->type == ZEBRA_ROUTE_BGP |
1962 | && !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP)) | |
1963 | zlog_debug( | |
d6951e5e | 1964 | " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\""); |
ad28e79a SW |
1965 | } |
1966 | return 0; | |
1967 | } | |
1968 | } | |
1969 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
d6951e5e DL |
1970 | zlog_debug(" %s: Nexthop did not lookup in table", |
1971 | __func__); | |
ad28e79a SW |
1972 | return 0; |
1973 | } | |
1974 | ||
1975 | /* This function verifies reachability of one given nexthop, which can be | |
1976 | * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored | |
1977 | * in nexthop->flags field. The nexthop->ifindex will be updated | |
1978 | * appropriately as well. An existing route map can turn | |
1979 | * (otherwise active) nexthop into inactive, but not vice versa. | |
1980 | * | |
98cda54a SW |
1981 | * If it finds a nexthop recursivedly, set the resolved_id |
1982 | * to match that nexthop's nhg_hash_entry ID; | |
1983 | * | |
ad28e79a SW |
1984 | * The return value is the final value of 'ACTIVE' flag. |
1985 | */ | |
1986 | static unsigned nexthop_active_check(struct route_node *rn, | |
1987 | struct route_entry *re, | |
8a507796 | 1988 | struct nexthop *nexthop) |
ad28e79a SW |
1989 | { |
1990 | struct interface *ifp; | |
b68885f9 | 1991 | route_map_result_t ret = RMAP_PERMITMATCH; |
ad28e79a SW |
1992 | int family; |
1993 | char buf[SRCDEST2STR_BUFFER]; | |
1994 | const struct prefix *p, *src_p; | |
1995 | struct zebra_vrf *zvrf; | |
1996 | ||
1997 | srcdest_rnode_prefixes(rn, &p, &src_p); | |
1998 | ||
1999 | if (rn->p.family == AF_INET) | |
2000 | family = AFI_IP; | |
2001 | else if (rn->p.family == AF_INET6) | |
2002 | family = AFI_IP6; | |
2003 | else | |
2004 | family = 0; | |
2005 | switch (nexthop->type) { | |
2006 | case NEXTHOP_TYPE_IFINDEX: | |
2007 | ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id); | |
fc8a02c4 SW |
2008 | /* |
2009 | * If the interface exists and its operative or its a kernel | |
2010 | * route and interface is up, its active. We trust kernel routes | |
2011 | * to be good. | |
2012 | */ | |
2013 | if (ifp | |
2014 | && (if_is_operative(ifp) | |
2015 | || (if_is_up(ifp) | |
2016 | && (re->type == ZEBRA_ROUTE_KERNEL | |
2017 | || re->type == ZEBRA_ROUTE_SYSTEM)))) | |
ad28e79a SW |
2018 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2019 | else | |
2020 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2021 | break; | |
2022 | case NEXTHOP_TYPE_IPV4: | |
2023 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
2024 | family = AFI_IP; | |
8a507796 | 2025 | if (nexthop_active(AFI_IP, re, nexthop, rn)) |
ad28e79a SW |
2026 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2027 | else | |
2028 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2029 | break; | |
2030 | case NEXTHOP_TYPE_IPV6: | |
2031 | family = AFI_IP6; | |
8a507796 | 2032 | if (nexthop_active(AFI_IP6, re, nexthop, rn)) |
ad28e79a SW |
2033 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2034 | else | |
2035 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2036 | break; | |
2037 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
2038 | /* RFC 5549, v4 prefix with v6 NH */ | |
2039 | if (rn->p.family != AF_INET) | |
2040 | family = AFI_IP6; | |
2041 | if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) { | |
2042 | ifp = if_lookup_by_index(nexthop->ifindex, | |
2043 | nexthop->vrf_id); | |
2044 | if (ifp && if_is_operative(ifp)) | |
2045 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2046 | else | |
2047 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2048 | } else { | |
8a507796 | 2049 | if (nexthop_active(AFI_IP6, re, nexthop, rn)) |
ad28e79a SW |
2050 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2051 | else | |
2052 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2053 | } | |
2054 | break; | |
2055 | case NEXTHOP_TYPE_BLACKHOLE: | |
2056 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2057 | break; | |
2058 | default: | |
2059 | break; | |
2060 | } | |
377e29f7 | 2061 | |
ad28e79a SW |
2062 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) { |
2063 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
377e29f7 MS |
2064 | zlog_debug(" %s: Unable to find active nexthop", |
2065 | __func__); | |
ad28e79a SW |
2066 | return 0; |
2067 | } | |
2068 | ||
2069 | /* XXX: What exactly do those checks do? Do we support | |
2070 | * e.g. IPv4 routes with IPv6 nexthops or vice versa? | |
2071 | */ | |
2072 | if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET) | |
2073 | || (family == AFI_IP6 && p->family != AF_INET6)) | |
2074 | return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2075 | ||
2076 | /* The original code didn't determine the family correctly | |
2077 | * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi | |
2078 | * from the rib_table_info in those cases. | |
2079 | * Possibly it may be better to use only the rib_table_info | |
2080 | * in every case. | |
2081 | */ | |
2082 | if (!family) { | |
630d5962 | 2083 | struct rib_table_info *info; |
ad28e79a SW |
2084 | |
2085 | info = srcdest_rnode_table_info(rn); | |
2086 | family = info->afi; | |
2087 | } | |
2088 | ||
2089 | memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr)); | |
2090 | ||
2091 | zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id); | |
2092 | if (!zvrf) { | |
2093 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
d6951e5e | 2094 | zlog_debug(" %s: zvrf is NULL", __func__); |
ad28e79a SW |
2095 | return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2096 | } | |
2097 | ||
2098 | /* It'll get set if required inside */ | |
2099 | ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop, | |
2100 | zvrf, re->tag); | |
2101 | if (ret == RMAP_DENYMATCH) { | |
2102 | if (IS_ZEBRA_DEBUG_RIB) { | |
2103 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
2104 | zlog_debug( | |
2105 | "%u:%s: Filtering out with NH out %s due to route map", | |
2106 | re->vrf_id, buf, | |
2107 | ifindex2ifname(nexthop->ifindex, | |
2108 | nexthop->vrf_id)); | |
2109 | } | |
2110 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2111 | } | |
2112 | return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2113 | } | |
2114 | ||
086e4e02 SW |
2115 | /* Helper function called after resolution to walk nhg rb trees |
2116 | * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop | |
2117 | * is active on singleton NHEs. | |
2118 | */ | |
2119 | static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry *nhe) | |
2120 | { | |
2121 | struct nhg_connected *rb_node_dep = NULL; | |
2122 | bool valid = false; | |
2123 | ||
2124 | if (!zebra_nhg_depends_is_empty(nhe)) { | |
2125 | /* Is at least one depend valid? */ | |
2126 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { | |
2127 | if (zebra_nhg_set_valid_if_active(rb_node_dep->nhe)) | |
2128 | valid = true; | |
2129 | } | |
2130 | ||
2131 | goto done; | |
2132 | } | |
2133 | ||
2134 | /* should be fully resolved singleton at this point */ | |
2135 | if (CHECK_FLAG(nhe->nhg.nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
2136 | valid = true; | |
2137 | ||
2138 | done: | |
2139 | if (valid) | |
2140 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
2141 | ||
2142 | return valid; | |
2143 | } | |
2144 | ||
ad28e79a | 2145 | /* |
377e29f7 MS |
2146 | * Process a list of nexthops, given the head of the list, determining |
2147 | * whether each one is ACTIVE/installable at this time. | |
ad28e79a | 2148 | */ |
377e29f7 MS |
2149 | static uint32_t nexthop_list_active_update(struct route_node *rn, |
2150 | struct route_entry *re, | |
2151 | struct nexthop *nexthop) | |
ad28e79a | 2152 | { |
ad28e79a SW |
2153 | union g_addr prev_src; |
2154 | unsigned int prev_active, new_active; | |
2155 | ifindex_t prev_index; | |
377e29f7 | 2156 | uint32_t counter = 0; |
e22e8001 | 2157 | |
377e29f7 MS |
2158 | /* Process nexthops one-by-one */ |
2159 | for ( ; nexthop; nexthop = nexthop->next) { | |
ad28e79a | 2160 | |
ad28e79a | 2161 | /* No protocol daemon provides src and so we're skipping |
377e29f7 MS |
2162 | * tracking it |
2163 | */ | |
ad28e79a SW |
2164 | prev_src = nexthop->rmap_src; |
2165 | prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2166 | prev_index = nexthop->ifindex; | |
2167 | /* | |
2168 | * We need to respect the multipath_num here | |
2169 | * as that what we should be able to install from | |
377e29f7 | 2170 | * a multipath perspective should not be a data plane |
ad28e79a SW |
2171 | * decision point. |
2172 | */ | |
98cda54a | 2173 | new_active = |
8a507796 | 2174 | nexthop_active_check(rn, re, nexthop); |
98cda54a | 2175 | |
377e29f7 | 2176 | if (new_active && counter >= zrouter.multipath_num) { |
4c55b5ff SW |
2177 | struct nexthop *nh; |
2178 | ||
2179 | /* Set it and its resolved nexthop as inactive. */ | |
2180 | for (nh = nexthop; nh; nh = nh->resolved) | |
2181 | UNSET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE); | |
2182 | ||
ad28e79a SW |
2183 | new_active = 0; |
2184 | } | |
9a0d4dd3 | 2185 | |
df31a989 | 2186 | if (new_active) |
377e29f7 | 2187 | counter++; |
9a0d4dd3 | 2188 | |
ad28e79a SW |
2189 | /* Don't allow src setting on IPv6 addr for now */ |
2190 | if (prev_active != new_active || prev_index != nexthop->ifindex | |
2191 | || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX | |
2192 | && nexthop->type < NEXTHOP_TYPE_IPV6) | |
2193 | && prev_src.ipv4.s_addr | |
2194 | != nexthop->rmap_src.ipv4.s_addr) | |
2195 | || ((nexthop->type >= NEXTHOP_TYPE_IPV6 | |
2196 | && nexthop->type < NEXTHOP_TYPE_BLACKHOLE) | |
2197 | && !(IPV6_ADDR_SAME(&prev_src.ipv6, | |
2198 | &nexthop->rmap_src.ipv6))) | |
8a507796 | 2199 | || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)) |
ad28e79a | 2200 | SET_FLAG(re->status, ROUTE_ENTRY_CHANGED); |
ad28e79a SW |
2201 | } |
2202 | ||
377e29f7 MS |
2203 | return counter; |
2204 | } | |
2205 | ||
2206 | /* | |
2207 | * Iterate over all nexthops of the given RIB entry and refresh their | |
2208 | * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag, | |
2209 | * the whole re structure is flagged with ROUTE_ENTRY_CHANGED. | |
2210 | * | |
2211 | * Return value is the new number of active nexthops. | |
2212 | */ | |
2213 | int nexthop_active_update(struct route_node *rn, struct route_entry *re) | |
2214 | { | |
2215 | struct nhg_hash_entry *curr_nhe; | |
2216 | uint32_t curr_active = 0, backup_active = 0; | |
2217 | ||
2218 | afi_t rt_afi = family2afi(rn->p.family); | |
2219 | ||
2220 | UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
2221 | ||
2222 | /* Make a local copy of the existing nhe, so we don't work on/modify | |
2223 | * the shared nhe. | |
2224 | */ | |
f727646a | 2225 | curr_nhe = zebra_nhe_copy(re->nhe, re->nhe->id); |
377e29f7 MS |
2226 | |
2227 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2228 | zlog_debug("%s: re %p nhe %p (%u), curr_nhe %p", | |
2229 | __func__, re, re->nhe, re->nhe->id, | |
2230 | curr_nhe); | |
2231 | ||
2232 | /* Clear the existing id, if any: this will avoid any confusion | |
2233 | * if the id exists, and will also force the creation | |
2234 | * of a new nhe reflecting the changes we may make in this local copy. | |
2235 | */ | |
2236 | curr_nhe->id = 0; | |
2237 | ||
2238 | /* Process nexthops */ | |
2239 | curr_active = nexthop_list_active_update(rn, re, curr_nhe->nhg.nexthop); | |
2240 | ||
2241 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2242 | zlog_debug("%s: re %p curr_active %u", __func__, re, | |
2243 | curr_active); | |
2244 | ||
2245 | /* If there are no backup nexthops, we are done */ | |
2246 | if (zebra_nhg_get_backup_nhg(curr_nhe) == NULL) | |
2247 | goto backups_done; | |
2248 | ||
2249 | backup_active = nexthop_list_active_update( | |
2250 | rn, re, zebra_nhg_get_backup_nhg(curr_nhe)->nexthop); | |
2251 | ||
2252 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2253 | zlog_debug("%s: re %p backup_active %u", __func__, re, | |
2254 | backup_active); | |
2255 | ||
2256 | backups_done: | |
2257 | ||
2258 | /* | |
2259 | * Ref or create an nhe that matches the current state of the | |
2260 | * nexthop(s). | |
2261 | */ | |
8a507796 | 2262 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) { |
98cda54a | 2263 | struct nhg_hash_entry *new_nhe = NULL; |
98cda54a | 2264 | |
377e29f7 MS |
2265 | new_nhe = zebra_nhg_rib_find_nhe(curr_nhe, rt_afi); |
2266 | ||
2267 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2268 | zlog_debug("%s: re %p CHANGED: nhe %p (%u) => new_nhe %p (%u)", | |
2269 | __func__, re, re->nhe, | |
2270 | re->nhe->id, new_nhe, new_nhe->id); | |
98cda54a | 2271 | |
5463ce26 | 2272 | route_entry_update_nhe(re, new_nhe); |
e22e8001 SW |
2273 | } |
2274 | ||
377e29f7 | 2275 | |
086e4e02 SW |
2276 | /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID |
2277 | * flag where appropriate. | |
2278 | */ | |
715e5c70 | 2279 | if (curr_active) |
086e4e02 | 2280 | zebra_nhg_set_valid_if_active(re->nhe); |
98cda54a SW |
2281 | |
2282 | /* | |
377e29f7 MS |
2283 | * Do not need the old / copied nhe anymore since it |
2284 | * was either copied over into a new nhe or not | |
98cda54a SW |
2285 | * used at all. |
2286 | */ | |
377e29f7 | 2287 | zebra_nhg_free(curr_nhe); |
9a0d4dd3 | 2288 | return curr_active; |
ad28e79a | 2289 | } |
5be96a2d | 2290 | |
497ff579 SW |
2291 | /* Recursively construct a grp array of fully resolved IDs. |
2292 | * | |
2293 | * This function allows us to account for groups within groups, | |
2294 | * by converting them into a flat array of IDs. | |
2295 | * | |
2296 | * nh_grp is modified at every level of recursion to append | |
2297 | * to it the next unique, fully resolved ID from the entire tree. | |
2298 | * | |
2299 | * | |
2300 | * Note: | |
2301 | * I'm pretty sure we only allow ONE level of group within group currently. | |
2302 | * But making this recursive just in case that ever changes. | |
2303 | */ | |
2304 | static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp *grp, | |
2305 | uint8_t curr_index, | |
2306 | struct nhg_hash_entry *nhe, | |
2307 | int max_num) | |
98cda54a SW |
2308 | { |
2309 | struct nhg_connected *rb_node_dep = NULL; | |
2310 | struct nhg_hash_entry *depend = NULL; | |
497ff579 | 2311 | uint8_t i = curr_index; |
98cda54a | 2312 | |
fec211ad | 2313 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
8dbc800f SW |
2314 | bool duplicate = false; |
2315 | ||
497ff579 SW |
2316 | if (i >= max_num) |
2317 | goto done; | |
2318 | ||
98cda54a SW |
2319 | depend = rb_node_dep->nhe; |
2320 | ||
2321 | /* | |
2322 | * If its recursive, use its resolved nhe in the group | |
2323 | */ | |
2324 | if (CHECK_FLAG(depend->flags, NEXTHOP_GROUP_RECURSIVE)) { | |
2325 | depend = zebra_nhg_resolve(depend); | |
2326 | if (!depend) { | |
2327 | flog_err( | |
2328 | EC_ZEBRA_NHG_FIB_UPDATE, | |
df31a989 SW |
2329 | "Failed to recursively resolve Nexthop Hash Entry in the group id=%u", |
2330 | nhe->id); | |
98cda54a SW |
2331 | continue; |
2332 | } | |
2333 | } | |
2334 | ||
497ff579 SW |
2335 | if (!zebra_nhg_depends_is_empty(depend)) { |
2336 | /* This is a group within a group */ | |
2337 | i = zebra_nhg_nhe2grp_internal(grp, i, depend, max_num); | |
2338 | } else { | |
086e4e02 SW |
2339 | if (!CHECK_FLAG(depend->flags, NEXTHOP_GROUP_VALID)) { |
2340 | if (IS_ZEBRA_DEBUG_RIB_DETAILED | |
2341 | || IS_ZEBRA_DEBUG_NHG) | |
2342 | zlog_debug( | |
2343 | "%s: Nexthop ID (%u) not valid, not appending to dataplane install group", | |
2344 | __func__, depend->id); | |
2345 | continue; | |
2346 | } | |
2347 | ||
1866b3af SW |
2348 | /* If the nexthop not installed/queued for install don't |
2349 | * put in the ID array. | |
2350 | */ | |
2351 | if (!(CHECK_FLAG(depend->flags, NEXTHOP_GROUP_INSTALLED) | |
2352 | || CHECK_FLAG(depend->flags, | |
2353 | NEXTHOP_GROUP_QUEUED))) { | |
2354 | if (IS_ZEBRA_DEBUG_RIB_DETAILED | |
2355 | || IS_ZEBRA_DEBUG_NHG) | |
2356 | zlog_debug( | |
2357 | "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group", | |
2358 | __func__, depend->id); | |
2359 | continue; | |
2360 | } | |
2361 | ||
b1c3f7ef | 2362 | /* Check for duplicate IDs, ignore if found. */ |
497ff579 | 2363 | for (int j = 0; j < i; j++) { |
d43122b5 | 2364 | if (depend->id == grp[j].id) { |
497ff579 | 2365 | duplicate = true; |
d43122b5 SW |
2366 | break; |
2367 | } | |
497ff579 | 2368 | } |
8dbc800f | 2369 | |
b1c3f7ef SW |
2370 | if (duplicate) { |
2371 | if (IS_ZEBRA_DEBUG_RIB_DETAILED | |
2372 | || IS_ZEBRA_DEBUG_NHG) | |
2373 | zlog_debug( | |
2374 | "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group", | |
2375 | __func__, depend->id); | |
2376 | continue; | |
497ff579 | 2377 | } |
b1c3f7ef SW |
2378 | |
2379 | grp[i].id = depend->id; | |
2380 | grp[i].weight = depend->nhg.nexthop->weight; | |
2381 | i++; | |
8dbc800f | 2382 | } |
98cda54a | 2383 | } |
8dbc800f | 2384 | |
0328a5bd MS |
2385 | if (nhe->backup_info == NULL || nhe->backup_info->nhe == NULL) |
2386 | goto done; | |
2387 | ||
2388 | /* TODO -- For now, we are not trying to use or install any | |
2389 | * backup info in this nexthop-id path: we aren't prepared | |
2390 | * to use the backups here yet. We're just debugging what we find. | |
2391 | */ | |
2392 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2393 | zlog_debug("%s: skipping backup nhe", __func__); | |
2394 | ||
8dbc800f | 2395 | done: |
98cda54a SW |
2396 | return i; |
2397 | } | |
2398 | ||
497ff579 SW |
2399 | /* Convert a nhe into a group array */ |
2400 | uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe, | |
2401 | int max_num) | |
2402 | { | |
2403 | /* Call into the recursive function */ | |
2404 | return zebra_nhg_nhe2grp_internal(grp, 0, nhe, max_num); | |
2405 | } | |
2406 | ||
5be96a2d SW |
2407 | void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe) |
2408 | { | |
f429bd1b SW |
2409 | struct nhg_connected *rb_node_dep = NULL; |
2410 | ||
2411 | /* Resolve it first */ | |
2412 | nhe = zebra_nhg_resolve(nhe); | |
2413 | ||
2414 | /* Make sure all depends are installed/queued */ | |
fec211ad | 2415 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
f429bd1b SW |
2416 | zebra_nhg_install_kernel(rb_node_dep->nhe); |
2417 | } | |
2418 | ||
086e4e02 SW |
2419 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_VALID) |
2420 | && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED) | |
e22e8001 | 2421 | && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) { |
724583ed SW |
2422 | /* Change its type to us since we are installing it */ |
2423 | nhe->type = ZEBRA_ROUTE_NHG; | |
2424 | ||
147bad16 | 2425 | int ret = dplane_nexthop_add(nhe); |
2d3c57e6 | 2426 | |
147bad16 SW |
2427 | switch (ret) { |
2428 | case ZEBRA_DPLANE_REQUEST_QUEUED: | |
2429 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED); | |
2430 | break; | |
2431 | case ZEBRA_DPLANE_REQUEST_FAILURE: | |
2432 | flog_err( | |
2433 | EC_ZEBRA_DP_INSTALL_FAIL, | |
2434 | "Failed to install Nexthop ID (%u) into the kernel", | |
2435 | nhe->id); | |
2436 | break; | |
2437 | case ZEBRA_DPLANE_REQUEST_SUCCESS: | |
2438 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
80286aa5 | 2439 | zebra_nhg_handle_install(nhe); |
147bad16 SW |
2440 | break; |
2441 | } | |
2442 | } | |
2443 | } | |
2444 | ||
147bad16 SW |
2445 | void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe) |
2446 | { | |
2447 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)) { | |
2448 | int ret = dplane_nexthop_delete(nhe); | |
2d3c57e6 | 2449 | |
147bad16 SW |
2450 | switch (ret) { |
2451 | case ZEBRA_DPLANE_REQUEST_QUEUED: | |
2452 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED); | |
2453 | break; | |
2454 | case ZEBRA_DPLANE_REQUEST_FAILURE: | |
2455 | flog_err( | |
2456 | EC_ZEBRA_DP_DELETE_FAIL, | |
2457 | "Failed to uninstall Nexthop ID (%u) from the kernel", | |
2458 | nhe->id); | |
2459 | break; | |
2460 | case ZEBRA_DPLANE_REQUEST_SUCCESS: | |
2461 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
2462 | break; | |
2463 | } | |
177e711d SW |
2464 | } |
2465 | ||
2466 | zebra_nhg_handle_uninstall(nhe); | |
147bad16 SW |
2467 | } |
2468 | ||
5f3c9e52 SW |
2469 | void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx) |
2470 | { | |
2471 | enum dplane_op_e op; | |
2472 | enum zebra_dplane_result status; | |
2473 | uint32_t id = 0; | |
2474 | struct nhg_hash_entry *nhe = NULL; | |
2475 | ||
2476 | op = dplane_ctx_get_op(ctx); | |
2477 | status = dplane_ctx_get_status(ctx); | |
2478 | ||
0c8215cb | 2479 | id = dplane_ctx_get_nhe_id(ctx); |
e22e8001 | 2480 | |
377e29f7 | 2481 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL || IS_ZEBRA_DEBUG_NHG_DETAIL) |
177e711d SW |
2482 | zlog_debug( |
2483 | "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s", | |
2484 | ctx, dplane_op2str(op), id, dplane_res2str(status)); | |
5f3c9e52 | 2485 | |
177e711d SW |
2486 | switch (op) { |
2487 | case DPLANE_OP_NH_DELETE: | |
2488 | if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) | |
2489 | flog_err( | |
2490 | EC_ZEBRA_DP_DELETE_FAIL, | |
2491 | "Failed to uninstall Nexthop ID (%u) from the kernel", | |
2492 | id); | |
2493 | /* We already free'd the data, nothing to do */ | |
2494 | break; | |
2495 | case DPLANE_OP_NH_INSTALL: | |
2496 | case DPLANE_OP_NH_UPDATE: | |
2497 | nhe = zebra_nhg_lookup_id(id); | |
2498 | ||
2499 | if (!nhe) { | |
2500 | flog_err( | |
2501 | EC_ZEBRA_NHG_SYNC, | |
2502 | "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table", | |
2503 | dplane_op2str(op), id); | |
5f3c9e52 SW |
2504 | break; |
2505 | } | |
177e711d SW |
2506 | |
2507 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED); | |
2508 | if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) { | |
2509 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
2510 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
80286aa5 | 2511 | zebra_nhg_handle_install(nhe); |
177e711d SW |
2512 | } else |
2513 | flog_err( | |
2514 | EC_ZEBRA_DP_INSTALL_FAIL, | |
2515 | "Failed to install Nexthop ID (%u) into the kernel", | |
2516 | nhe->id); | |
2517 | break; | |
2518 | case DPLANE_OP_ROUTE_INSTALL: | |
2519 | case DPLANE_OP_ROUTE_UPDATE: | |
2520 | case DPLANE_OP_ROUTE_DELETE: | |
2521 | case DPLANE_OP_ROUTE_NOTIFY: | |
2522 | case DPLANE_OP_LSP_INSTALL: | |
2523 | case DPLANE_OP_LSP_UPDATE: | |
2524 | case DPLANE_OP_LSP_DELETE: | |
2525 | case DPLANE_OP_LSP_NOTIFY: | |
2526 | case DPLANE_OP_PW_INSTALL: | |
2527 | case DPLANE_OP_PW_UNINSTALL: | |
2528 | case DPLANE_OP_SYS_ROUTE_ADD: | |
2529 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
2530 | case DPLANE_OP_ADDR_INSTALL: | |
2531 | case DPLANE_OP_ADDR_UNINSTALL: | |
2532 | case DPLANE_OP_MAC_INSTALL: | |
2533 | case DPLANE_OP_MAC_DELETE: | |
2534 | case DPLANE_OP_NEIGH_INSTALL: | |
2535 | case DPLANE_OP_NEIGH_UPDATE: | |
2536 | case DPLANE_OP_NEIGH_DELETE: | |
2537 | case DPLANE_OP_VTEP_ADD: | |
2538 | case DPLANE_OP_VTEP_DELETE: | |
60d8d43b JU |
2539 | case DPLANE_OP_RULE_ADD: |
2540 | case DPLANE_OP_RULE_DELETE: | |
2541 | case DPLANE_OP_RULE_UPDATE: | |
177e711d SW |
2542 | case DPLANE_OP_NONE: |
2543 | break; | |
2544 | } | |
71593b3f SW |
2545 | |
2546 | dplane_ctx_fini(&ctx); | |
5be96a2d SW |
2547 | } |
2548 | ||
38e40db1 SW |
2549 | static void zebra_nhg_sweep_entry(struct hash_bucket *bucket, void *arg) |
2550 | { | |
2551 | struct nhg_hash_entry *nhe = NULL; | |
2552 | ||
2553 | nhe = (struct nhg_hash_entry *)bucket->data; | |
2554 | ||
2555 | /* If its being ref'd, just let it be uninstalled via a route removal */ | |
2556 | if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0) | |
2557 | zebra_nhg_uninstall_kernel(nhe); | |
2558 | } | |
2559 | ||
2560 | void zebra_nhg_sweep_table(struct hash *hash) | |
2561 | { | |
2562 | hash_iterate(hash, zebra_nhg_sweep_entry, NULL); | |
2563 | } | |
7c99d51b MS |
2564 | |
2565 | /* Global control to disable use of kernel nexthops, if available. We can't | |
2566 | * force the kernel to support nexthop ids, of course, but we can disable | |
2567 | * zebra's use of them, for testing e.g. By default, if the kernel supports | |
2568 | * nexthop ids, zebra uses them. | |
2569 | */ | |
2570 | void zebra_nhg_enable_kernel_nexthops(bool set) | |
2571 | { | |
2572 | g_nexthops_enabled = set; | |
2573 | } | |
2574 | ||
2575 | bool zebra_nhg_kernel_nexthops_enabled(void) | |
2576 | { | |
2577 | return g_nexthops_enabled; | |
2578 | } |