]>
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" |
31f937fb | 31 | #include "lib/lib_errors.h" |
ad28e79a SW |
32 | |
33 | #include "zebra/connected.h" | |
34 | #include "zebra/debug.h" | |
35 | #include "zebra/zebra_router.h" | |
5948f013 | 36 | #include "zebra/zebra_nhg_private.h" |
ad28e79a SW |
37 | #include "zebra/zebra_rnh.h" |
38 | #include "zebra/zebra_routemap.h" | |
31f937fb | 39 | #include "zebra/zebra_srte.h" |
51d80884 | 40 | #include "zebra/zserv.h" |
ad28e79a | 41 | #include "zebra/rt.h" |
d9f5b2f5 | 42 | #include "zebra_errors.h" |
0c8215cb | 43 | #include "zebra_dplane.h" |
fe593b78 | 44 | #include "zebra/interface.h" |
ee94437e | 45 | #include "zebra/zapi_msg.h" |
48dc8610 | 46 | #include "zebra/rib.h" |
d9f5b2f5 | 47 | |
51d80884 | 48 | DEFINE_MTYPE_STATIC(ZEBRA, NHG, "Nexthop Group Entry"); |
a15d4c00 | 49 | DEFINE_MTYPE_STATIC(ZEBRA, NHG_CONNECTED, "Nexthop Group Connected"); |
e22e8001 | 50 | DEFINE_MTYPE_STATIC(ZEBRA, NHG_CTX, "Nexthop Group Context"); |
0c8215cb | 51 | |
5530d55d MS |
52 | /* Map backup nexthop indices between two nhes */ |
53 | struct backup_nh_map_s { | |
54 | int map_count; | |
55 | ||
56 | struct { | |
57 | uint8_t orig_idx; | |
58 | uint8_t new_idx; | |
59 | } map[MULTIPATH_NUM]; | |
60 | }; | |
61 | ||
38e40db1 SW |
62 | /* id counter to keep in sync with kernel */ |
63 | uint32_t id_counter; | |
64 | ||
aa458838 | 65 | /* Controlled through ui */ |
7c99d51b | 66 | static bool g_nexthops_enabled = true; |
73937edb | 67 | static bool proto_nexthops_only; |
aa458838 | 68 | static bool use_recursive_backups = true; |
7c99d51b | 69 | |
0885b1e3 | 70 | static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi, |
5588801e | 71 | int type, bool from_dplane); |
37c6708b | 72 | static void depends_add(struct nhg_connected_tree_head *head, |
5657e7e9 | 73 | struct nhg_hash_entry *depend); |
38e40db1 SW |
74 | static struct nhg_hash_entry * |
75 | depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh, | |
5588801e | 76 | afi_t afi, int type, bool from_dplane); |
38e40db1 SW |
77 | static struct nhg_hash_entry * |
78 | depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id); | |
37c6708b | 79 | static void depends_decrement_free(struct nhg_connected_tree_head *head); |
0c8215cb | 80 | |
1d48702e MS |
81 | static struct nhg_backup_info * |
82 | nhg_backup_copy(const struct nhg_backup_info *orig); | |
83 | ||
ac5d1091 | 84 | /* Helper function for getting the next allocatable ID */ |
73937edb | 85 | static uint32_t nhg_get_next_id(void) |
ac5d1091 SW |
86 | { |
87 | while (1) { | |
88 | id_counter++; | |
89 | ||
90 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
91 | zlog_debug("%s: ID %u checking", __func__, id_counter); | |
92 | ||
93 | if (id_counter == ZEBRA_NHG_PROTO_LOWER) { | |
94 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
95 | zlog_debug("%s: ID counter wrapped", __func__); | |
96 | ||
97 | id_counter = 0; | |
98 | continue; | |
99 | } | |
100 | ||
101 | if (zebra_nhg_lookup_id(id_counter)) { | |
102 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
103 | zlog_debug("%s: ID already exists", __func__); | |
104 | ||
105 | continue; | |
106 | } | |
107 | ||
108 | break; | |
109 | } | |
110 | ||
111 | return id_counter; | |
112 | } | |
e22e8001 | 113 | |
5948f013 | 114 | static void nhg_connected_free(struct nhg_connected *dep) |
0c8215cb | 115 | { |
a15d4c00 | 116 | XFREE(MTYPE_NHG_CONNECTED, dep); |
0c8215cb SW |
117 | } |
118 | ||
5948f013 | 119 | static struct nhg_connected *nhg_connected_new(struct nhg_hash_entry *nhe) |
0c8215cb | 120 | { |
a15d4c00 | 121 | struct nhg_connected *new = NULL; |
0c8215cb | 122 | |
a15d4c00 | 123 | new = XCALLOC(MTYPE_NHG_CONNECTED, sizeof(struct nhg_connected)); |
0c8215cb SW |
124 | new->nhe = nhe; |
125 | ||
126 | return new; | |
127 | } | |
128 | ||
37c6708b | 129 | void nhg_connected_tree_free(struct nhg_connected_tree_head *head) |
0c8215cb | 130 | { |
a15d4c00 | 131 | struct nhg_connected *rb_node_dep = NULL; |
0c8215cb | 132 | |
37c6708b | 133 | if (!nhg_connected_tree_is_empty(head)) { |
fec211ad | 134 | frr_each_safe(nhg_connected_tree, head, rb_node_dep) { |
37c6708b | 135 | nhg_connected_tree_del(head, rb_node_dep); |
fe593b78 SW |
136 | nhg_connected_free(rb_node_dep); |
137 | } | |
0c8215cb | 138 | } |
0c8215cb SW |
139 | } |
140 | ||
37c6708b | 141 | bool nhg_connected_tree_is_empty(const struct nhg_connected_tree_head *head) |
0c8215cb | 142 | { |
fec211ad | 143 | return nhg_connected_tree_count(head) ? false : true; |
0c8215cb SW |
144 | } |
145 | ||
98cda54a | 146 | struct nhg_connected * |
37c6708b | 147 | nhg_connected_tree_root(struct nhg_connected_tree_head *head) |
98cda54a | 148 | { |
37c6708b | 149 | return nhg_connected_tree_first(head); |
98cda54a SW |
150 | } |
151 | ||
5bf15faa SW |
152 | struct nhg_hash_entry * |
153 | nhg_connected_tree_del_nhe(struct nhg_connected_tree_head *head, | |
154 | struct nhg_hash_entry *depend) | |
0c8215cb | 155 | { |
a15d4c00 | 156 | struct nhg_connected lookup = {}; |
085304dc | 157 | struct nhg_connected *remove = NULL; |
5bf15faa | 158 | struct nhg_hash_entry *removed_nhe; |
0c8215cb SW |
159 | |
160 | lookup.nhe = depend; | |
3119f6a1 | 161 | |
085304dc | 162 | /* Lookup to find the element, then remove it */ |
37c6708b | 163 | remove = nhg_connected_tree_find(head, &lookup); |
085304dc | 164 | if (remove) |
5bf15faa SW |
165 | /* Re-returning here just in case this API changes.. |
166 | * the _del list api's are a bit undefined at the moment. | |
167 | * | |
168 | * So hopefully returning here will make it fail if the api | |
169 | * changes to something different than currently expected. | |
170 | */ | |
171 | remove = nhg_connected_tree_del(head, remove); | |
172 | ||
173 | /* If the entry was sucessfully removed, free the 'connected` struct */ | |
174 | if (remove) { | |
175 | removed_nhe = remove->nhe; | |
085304dc | 176 | nhg_connected_free(remove); |
5bf15faa SW |
177 | return removed_nhe; |
178 | } | |
179 | ||
180 | return NULL; | |
3119f6a1 SW |
181 | } |
182 | ||
5bf15faa SW |
183 | /* Assuming UNIQUE RB tree. If this changes, assumptions here about |
184 | * insertion need to change. | |
185 | */ | |
186 | struct nhg_hash_entry * | |
187 | nhg_connected_tree_add_nhe(struct nhg_connected_tree_head *head, | |
188 | struct nhg_hash_entry *depend) | |
3119f6a1 | 189 | { |
a15d4c00 | 190 | struct nhg_connected *new = NULL; |
0c8215cb | 191 | |
a15d4c00 | 192 | new = nhg_connected_new(depend); |
0c8215cb | 193 | |
5bf15faa SW |
194 | /* On success, NULL will be returned from the |
195 | * RB code. | |
196 | */ | |
197 | if (new && (nhg_connected_tree_add(head, new) == NULL)) | |
198 | return NULL; | |
199 | ||
200 | /* If it wasn't successful, it must be a duplicate. We enforce the | |
201 | * unique property for the `nhg_connected` tree. | |
202 | */ | |
203 | nhg_connected_free(new); | |
204 | ||
205 | return depend; | |
3119f6a1 SW |
206 | } |
207 | ||
37c6708b SW |
208 | static void |
209 | nhg_connected_tree_decrement_ref(struct nhg_connected_tree_head *head) | |
32e29e79 SW |
210 | { |
211 | struct nhg_connected *rb_node_dep = NULL; | |
32e29e79 | 212 | |
fec211ad | 213 | frr_each_safe(nhg_connected_tree, head, rb_node_dep) { |
32e29e79 SW |
214 | zebra_nhg_decrement_ref(rb_node_dep->nhe); |
215 | } | |
216 | } | |
217 | ||
37c6708b SW |
218 | static void |
219 | nhg_connected_tree_increment_ref(struct nhg_connected_tree_head *head) | |
32e29e79 SW |
220 | { |
221 | struct nhg_connected *rb_node_dep = NULL; | |
222 | ||
fec211ad | 223 | frr_each(nhg_connected_tree, head, rb_node_dep) { |
32e29e79 SW |
224 | zebra_nhg_increment_ref(rb_node_dep->nhe); |
225 | } | |
226 | } | |
227 | ||
98cda54a SW |
228 | struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe) |
229 | { | |
230 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE) | |
231 | && !zebra_nhg_depends_is_empty(nhe)) { | |
37c6708b | 232 | nhe = nhg_connected_tree_root(&nhe->nhg_depends)->nhe; |
98cda54a SW |
233 | return zebra_nhg_resolve(nhe); |
234 | } | |
235 | ||
236 | return nhe; | |
237 | } | |
238 | ||
fe593b78 | 239 | unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe) |
a15d4c00 | 240 | { |
37c6708b | 241 | return nhg_connected_tree_count(&nhe->nhg_depends); |
a15d4c00 SW |
242 | } |
243 | ||
fe593b78 | 244 | bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe) |
a15d4c00 | 245 | { |
37c6708b | 246 | return nhg_connected_tree_is_empty(&nhe->nhg_depends); |
a15d4c00 SW |
247 | } |
248 | ||
5948f013 SW |
249 | static void zebra_nhg_depends_del(struct nhg_hash_entry *from, |
250 | struct nhg_hash_entry *depend) | |
3119f6a1 | 251 | { |
37c6708b | 252 | nhg_connected_tree_del_nhe(&from->nhg_depends, depend); |
3119f6a1 SW |
253 | } |
254 | ||
5948f013 | 255 | static void zebra_nhg_depends_init(struct nhg_hash_entry *nhe) |
148a0103 | 256 | { |
37c6708b | 257 | nhg_connected_tree_init(&nhe->nhg_depends); |
148a0103 SW |
258 | } |
259 | ||
fe593b78 SW |
260 | unsigned int zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe) |
261 | { | |
37c6708b | 262 | return nhg_connected_tree_count(&nhe->nhg_dependents); |
fe593b78 SW |
263 | } |
264 | ||
5948f013 | 265 | |
fe593b78 SW |
266 | bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe) |
267 | { | |
37c6708b | 268 | return nhg_connected_tree_is_empty(&nhe->nhg_dependents); |
fe593b78 SW |
269 | } |
270 | ||
5948f013 SW |
271 | static void zebra_nhg_dependents_del(struct nhg_hash_entry *from, |
272 | struct nhg_hash_entry *dependent) | |
fe593b78 | 273 | { |
37c6708b | 274 | nhg_connected_tree_del_nhe(&from->nhg_dependents, dependent); |
fe593b78 SW |
275 | } |
276 | ||
5948f013 SW |
277 | static void zebra_nhg_dependents_add(struct nhg_hash_entry *to, |
278 | struct nhg_hash_entry *dependent) | |
fe593b78 | 279 | { |
37c6708b | 280 | nhg_connected_tree_add_nhe(&to->nhg_dependents, dependent); |
fe593b78 SW |
281 | } |
282 | ||
5948f013 | 283 | static void zebra_nhg_dependents_init(struct nhg_hash_entry *nhe) |
fe593b78 | 284 | { |
37c6708b | 285 | nhg_connected_tree_init(&nhe->nhg_dependents); |
fe593b78 SW |
286 | } |
287 | ||
21615102 SW |
288 | /* Release this nhe from anything depending on it */ |
289 | static void zebra_nhg_dependents_release(struct nhg_hash_entry *nhe) | |
290 | { | |
80286aa5 | 291 | struct nhg_connected *rb_node_dep = NULL; |
21615102 | 292 | |
80286aa5 SW |
293 | frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) { |
294 | zebra_nhg_depends_del(rb_node_dep->nhe, nhe); | |
295 | /* recheck validity of the dependent */ | |
296 | zebra_nhg_check_valid(rb_node_dep->nhe); | |
21615102 SW |
297 | } |
298 | } | |
299 | ||
5948f013 SW |
300 | /* Release this nhe from anything that it depends on */ |
301 | static void zebra_nhg_depends_release(struct nhg_hash_entry *nhe) | |
302 | { | |
303 | if (!zebra_nhg_depends_is_empty(nhe)) { | |
304 | struct nhg_connected *rb_node_dep = NULL; | |
305 | ||
306 | frr_each_safe(nhg_connected_tree, &nhe->nhg_depends, | |
307 | rb_node_dep) { | |
308 | zebra_nhg_dependents_del(rb_node_dep->nhe, nhe); | |
309 | } | |
310 | } | |
311 | } | |
312 | ||
313 | ||
d9f5b2f5 SW |
314 | struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id) |
315 | { | |
0c8215cb | 316 | struct nhg_hash_entry lookup = {}; |
d9f5b2f5 SW |
317 | |
318 | lookup.id = id; | |
319 | return hash_lookup(zrouter.nhgs_id, &lookup); | |
320 | } | |
321 | ||
5948f013 | 322 | static int zebra_nhg_insert_id(struct nhg_hash_entry *nhe) |
d9f5b2f5 SW |
323 | { |
324 | if (hash_lookup(zrouter.nhgs_id, nhe)) { | |
325 | flog_err( | |
326 | EC_ZEBRA_NHG_TABLE_INSERT_FAILED, | |
68d188be DS |
327 | "Failed inserting NHG %pNG into the ID hash table, entry already exists", |
328 | nhe); | |
d9f5b2f5 SW |
329 | return -1; |
330 | } | |
331 | ||
8e3aae66 | 332 | (void)hash_get(zrouter.nhgs_id, nhe, hash_alloc_intern); |
d9f5b2f5 SW |
333 | |
334 | return 0; | |
335 | } | |
ad28e79a | 336 | |
5948f013 SW |
337 | static void zebra_nhg_set_if(struct nhg_hash_entry *nhe, struct interface *ifp) |
338 | { | |
339 | nhe->ifp = ifp; | |
340 | if_nhg_dependents_add(ifp, nhe); | |
341 | } | |
342 | ||
38e40db1 SW |
343 | static void |
344 | zebra_nhg_connect_depends(struct nhg_hash_entry *nhe, | |
377e29f7 | 345 | struct nhg_connected_tree_head *nhg_depends) |
4e49c8b8 | 346 | { |
a15d4c00 SW |
347 | struct nhg_connected *rb_node_dep = NULL; |
348 | ||
38e40db1 SW |
349 | /* This has been allocated higher above in the stack. Could probably |
350 | * re-allocate and free the old stuff but just using the same memory | |
351 | * for now. Otherwise, their might be a time trade-off for repeated | |
352 | * alloc/frees as startup. | |
353 | */ | |
377e29f7 | 354 | nhe->nhg_depends = *nhg_depends; |
4e49c8b8 | 355 | |
a15d4c00 | 356 | /* Attach backpointer to anything that it depends on */ |
fe593b78 | 357 | zebra_nhg_dependents_init(nhe); |
a15d4c00 | 358 | if (!zebra_nhg_depends_is_empty(nhe)) { |
fec211ad | 359 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
377e29f7 | 360 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be DS |
361 | zlog_debug("%s: nhe %p (%pNG), dep %p (%pNG)", |
362 | __func__, nhe, nhe, rb_node_dep->nhe, | |
363 | rb_node_dep->nhe); | |
377e29f7 | 364 | |
a15d4c00 SW |
365 | zebra_nhg_dependents_add(rb_node_dep->nhe, nhe); |
366 | } | |
367 | } | |
377e29f7 | 368 | } |
4e49c8b8 | 369 | |
377e29f7 MS |
370 | /* Init an nhe, for use in a hash lookup for example */ |
371 | void zebra_nhe_init(struct nhg_hash_entry *nhe, afi_t afi, | |
372 | const struct nexthop *nh) | |
373 | { | |
374 | memset(nhe, 0, sizeof(struct nhg_hash_entry)); | |
375 | nhe->vrf_id = VRF_DEFAULT; | |
376 | nhe->type = ZEBRA_ROUTE_NHG; | |
377 | nhe->afi = AFI_UNSPEC; | |
7b683a96 | 378 | |
377e29f7 MS |
379 | /* There are some special rules that apply to groups representing |
380 | * a single nexthop. | |
381 | */ | |
382 | if (nh && (nh->next == NULL)) { | |
383 | switch (nh->type) { | |
9b4ab909 MS |
384 | case NEXTHOP_TYPE_IFINDEX: |
385 | case NEXTHOP_TYPE_BLACKHOLE: | |
377e29f7 MS |
386 | /* |
387 | * This switch case handles setting the afi different | |
388 | * for ipv4/v6 routes. Ifindex/blackhole nexthop | |
389 | * objects cannot be ambiguous, they must be Address | |
390 | * Family specific. If we get here, we will either use | |
391 | * the AF of the route, or the one we got passed from | |
392 | * here from the kernel. | |
393 | */ | |
394 | nhe->afi = afi; | |
395 | break; | |
9b4ab909 MS |
396 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
397 | case NEXTHOP_TYPE_IPV4: | |
377e29f7 MS |
398 | nhe->afi = AFI_IP; |
399 | break; | |
9b4ab909 MS |
400 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
401 | case NEXTHOP_TYPE_IPV6: | |
377e29f7 MS |
402 | nhe->afi = AFI_IP6; |
403 | break; | |
404 | } | |
7b683a96 | 405 | } |
38e40db1 SW |
406 | } |
407 | ||
0eb97b86 | 408 | struct nhg_hash_entry *zebra_nhg_alloc(void) |
38e40db1 SW |
409 | { |
410 | struct nhg_hash_entry *nhe; | |
411 | ||
412 | nhe = XCALLOC(MTYPE_NHG, sizeof(struct nhg_hash_entry)); | |
413 | ||
0eb97b86 MS |
414 | return nhe; |
415 | } | |
416 | ||
f727646a MS |
417 | /* |
418 | * Allocate new nhe and make shallow copy of 'orig'; no | |
419 | * recursive info is copied. | |
420 | */ | |
421 | struct nhg_hash_entry *zebra_nhe_copy(const struct nhg_hash_entry *orig, | |
422 | uint32_t id) | |
0eb97b86 MS |
423 | { |
424 | struct nhg_hash_entry *nhe; | |
425 | ||
426 | nhe = zebra_nhg_alloc(); | |
427 | ||
38e40db1 SW |
428 | nhe->id = id; |
429 | ||
1d48702e | 430 | nexthop_group_copy(&(nhe->nhg), &(orig->nhg)); |
38e40db1 | 431 | |
1d48702e MS |
432 | nhe->vrf_id = orig->vrf_id; |
433 | nhe->afi = orig->afi; | |
434 | nhe->type = orig->type ? orig->type : ZEBRA_ROUTE_NHG; | |
38e40db1 SW |
435 | nhe->refcnt = 0; |
436 | nhe->dplane_ref = zebra_router_get_next_sequence(); | |
437 | ||
1d48702e MS |
438 | /* Copy backup info also, if present */ |
439 | if (orig->backup_info) | |
440 | nhe->backup_info = nhg_backup_copy(orig->backup_info); | |
441 | ||
38e40db1 SW |
442 | return nhe; |
443 | } | |
444 | ||
445 | /* Allocation via hash handler */ | |
446 | static void *zebra_nhg_hash_alloc(void *arg) | |
447 | { | |
448 | struct nhg_hash_entry *nhe = NULL; | |
449 | struct nhg_hash_entry *copy = arg; | |
7b683a96 | 450 | |
f727646a | 451 | nhe = zebra_nhe_copy(copy, copy->id); |
9ef49038 SW |
452 | |
453 | /* Mark duplicate nexthops in a group at creation time. */ | |
c415d895 | 454 | nexthop_group_mark_duplicates(&(nhe->nhg)); |
9ef49038 | 455 | |
bf157b92 SW |
456 | /* |
457 | * Add the ifp now if it's not a group or recursive and has ifindex. | |
458 | * | |
459 | * A proto-owned ID is always a group. | |
460 | */ | |
461 | if (!PROTO_OWNED(nhe) && nhe->nhg.nexthop && !nhe->nhg.nexthop->next | |
462 | && !nhe->nhg.nexthop->resolved && nhe->nhg.nexthop->ifindex) { | |
377e29f7 MS |
463 | struct interface *ifp = NULL; |
464 | ||
465 | ifp = if_lookup_by_index(nhe->nhg.nexthop->ifindex, | |
466 | nhe->nhg.nexthop->vrf_id); | |
467 | if (ifp) | |
468 | zebra_nhg_set_if(nhe, ifp); | |
30672034 SW |
469 | else { |
470 | if (IS_ZEBRA_DEBUG_NHG) | |
471 | zlog_debug( | |
68d188be | 472 | "Failed to lookup an interface with ifindex=%d in vrf=%u for NHE %pNG", |
30672034 | 473 | nhe->nhg.nexthop->ifindex, |
68d188be | 474 | nhe->nhg.nexthop->vrf_id, nhe); |
30672034 | 475 | } |
377e29f7 MS |
476 | } |
477 | ||
4e49c8b8 DS |
478 | return nhe; |
479 | } | |
480 | ||
4e49c8b8 DS |
481 | uint32_t zebra_nhg_hash_key(const void *arg) |
482 | { | |
483 | const struct nhg_hash_entry *nhe = arg; | |
0885b1e3 SW |
484 | uint32_t key = 0x5a351234; |
485 | uint32_t primary = 0; | |
486 | uint32_t backup = 0; | |
487 | ||
488 | primary = nexthop_group_hash(&(nhe->nhg)); | |
489 | if (nhe->backup_info) | |
490 | backup = nexthop_group_hash(&(nhe->backup_info->nhe->nhg)); | |
d9f5b2f5 | 491 | |
0885b1e3 SW |
492 | key = jhash_3words(primary, backup, nhe->type, key); |
493 | ||
494 | key = jhash_2words(nhe->vrf_id, nhe->afi, key); | |
d9f5b2f5 | 495 | |
d9f5b2f5 | 496 | return key; |
4e49c8b8 DS |
497 | } |
498 | ||
a95b8020 SW |
499 | uint32_t zebra_nhg_id_key(const void *arg) |
500 | { | |
501 | const struct nhg_hash_entry *nhe = arg; | |
502 | ||
503 | return nhe->id; | |
504 | } | |
505 | ||
1d48702e MS |
506 | /* Helper with common nhg/nhe nexthop comparison logic */ |
507 | static bool nhg_compare_nexthops(const struct nexthop *nh1, | |
508 | const struct nexthop *nh2) | |
509 | { | |
f924db49 | 510 | assert(nh1 != NULL && nh2 != NULL); |
1d48702e MS |
511 | |
512 | /* | |
513 | * We have to check the active flag of each individual one, | |
514 | * not just the overall active_num. This solves the special case | |
515 | * issue of a route with a nexthop group with one nexthop | |
516 | * resolving to itself and thus marking it inactive. If we | |
517 | * have two different routes each wanting to mark a different | |
518 | * nexthop inactive, they need to hash to two different groups. | |
519 | * | |
520 | * If we just hashed on num_active, they would hash the same | |
521 | * which is incorrect. | |
522 | * | |
523 | * ex) | |
524 | * 1.1.1.0/24 | |
525 | * -> 1.1.1.1 dummy1 (inactive) | |
526 | * -> 1.1.2.1 dummy2 | |
527 | * | |
528 | * 1.1.2.0/24 | |
529 | * -> 1.1.1.1 dummy1 | |
530 | * -> 1.1.2.1 dummy2 (inactive) | |
531 | * | |
532 | * Without checking each individual one, they would hash to | |
533 | * the same group and both have 1.1.1.1 dummy1 marked inactive. | |
534 | * | |
535 | */ | |
536 | if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_ACTIVE) | |
537 | != CHECK_FLAG(nh2->flags, NEXTHOP_FLAG_ACTIVE)) | |
538 | return false; | |
539 | ||
540 | if (!nexthop_same(nh1, nh2)) | |
541 | return false; | |
542 | ||
543 | return true; | |
544 | } | |
545 | ||
4e49c8b8 DS |
546 | bool zebra_nhg_hash_equal(const void *arg1, const void *arg2) |
547 | { | |
548 | const struct nhg_hash_entry *nhe1 = arg1; | |
549 | const struct nhg_hash_entry *nhe2 = arg2; | |
148813c2 SW |
550 | struct nexthop *nexthop1; |
551 | struct nexthop *nexthop2; | |
4e49c8b8 | 552 | |
98cda54a SW |
553 | /* No matter what if they equal IDs, assume equal */ |
554 | if (nhe1->id && nhe2->id && (nhe1->id == nhe2->id)) | |
555 | return true; | |
556 | ||
0885b1e3 SW |
557 | if (nhe1->type != nhe2->type) |
558 | return false; | |
559 | ||
4e49c8b8 DS |
560 | if (nhe1->vrf_id != nhe2->vrf_id) |
561 | return false; | |
562 | ||
77b76fc9 SW |
563 | if (nhe1->afi != nhe2->afi) |
564 | return false; | |
565 | ||
1d48702e | 566 | /* Nexthops should be in-order, so we simply compare them in-place */ |
c415d895 | 567 | for (nexthop1 = nhe1->nhg.nexthop, nexthop2 = nhe2->nhg.nexthop; |
f924db49 | 568 | nexthop1 && nexthop2; |
148813c2 | 569 | nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) { |
148813c2 | 570 | |
1d48702e | 571 | if (!nhg_compare_nexthops(nexthop1, nexthop2)) |
148813c2 | 572 | return false; |
1d48702e | 573 | } |
148813c2 | 574 | |
f924db49 MS |
575 | /* Check for unequal list lengths */ |
576 | if (nexthop1 || nexthop2) | |
577 | return false; | |
578 | ||
1d48702e MS |
579 | /* If there's no backup info, comparison is done. */ |
580 | if ((nhe1->backup_info == NULL) && (nhe2->backup_info == NULL)) | |
581 | return true; | |
582 | ||
583 | /* Compare backup info also - test the easy things first */ | |
584 | if (nhe1->backup_info && (nhe2->backup_info == NULL)) | |
585 | return false; | |
586 | if (nhe2->backup_info && (nhe1->backup_info == NULL)) | |
587 | return false; | |
588 | ||
589 | /* Compare number of backups before actually comparing any */ | |
590 | for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop, | |
591 | nexthop2 = nhe2->backup_info->nhe->nhg.nexthop; | |
592 | nexthop1 && nexthop2; | |
593 | nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) { | |
594 | ; | |
595 | } | |
596 | ||
597 | /* Did we find the end of one list before the other? */ | |
598 | if (nexthop1 || nexthop2) | |
599 | return false; | |
600 | ||
601 | /* Have to compare the backup nexthops */ | |
602 | for (nexthop1 = nhe1->backup_info->nhe->nhg.nexthop, | |
603 | nexthop2 = nhe2->backup_info->nhe->nhg.nexthop; | |
f924db49 | 604 | nexthop1 && nexthop2; |
1d48702e | 605 | nexthop1 = nexthop1->next, nexthop2 = nexthop2->next) { |
148813c2 | 606 | |
1d48702e | 607 | if (!nhg_compare_nexthops(nexthop1, nexthop2)) |
148813c2 SW |
608 | return false; |
609 | } | |
e4ac313b | 610 | |
4e49c8b8 DS |
611 | return true; |
612 | } | |
613 | ||
d9f5b2f5 | 614 | bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2) |
4e49c8b8 | 615 | { |
d9f5b2f5 SW |
616 | const struct nhg_hash_entry *nhe1 = arg1; |
617 | const struct nhg_hash_entry *nhe2 = arg2; | |
4e49c8b8 | 618 | |
d9f5b2f5 SW |
619 | return nhe1->id == nhe2->id; |
620 | } | |
4e49c8b8 | 621 | |
1b366e63 SW |
622 | static int zebra_nhg_process_grp(struct nexthop_group *nhg, |
623 | struct nhg_connected_tree_head *depends, | |
624 | struct nh_grp *grp, uint8_t count) | |
e22e8001 | 625 | { |
37c6708b | 626 | nhg_connected_tree_init(depends); |
e22e8001 SW |
627 | |
628 | for (int i = 0; i < count; i++) { | |
629 | struct nhg_hash_entry *depend = NULL; | |
630 | /* We do not care about nexthop_grp.weight at | |
631 | * this time. But we should figure out | |
632 | * how to adapt this to our code in | |
633 | * the future. | |
634 | */ | |
38e40db1 | 635 | depend = depends_find_id_add(depends, grp[i].id); |
e22e8001 | 636 | |
38e40db1 | 637 | if (!depend) { |
e22e8001 SW |
638 | flog_err( |
639 | EC_ZEBRA_NHG_SYNC, | |
640 | "Received Nexthop Group from the kernel with a dependent Nexthop ID (%u) which we do not have in our table", | |
641 | grp[i].id); | |
1b366e63 | 642 | return -1; |
e22e8001 | 643 | } |
38e40db1 SW |
644 | |
645 | /* | |
646 | * If this is a nexthop with its own group | |
647 | * dependencies, add them as well. Not sure its | |
648 | * even possible to have a group within a group | |
649 | * in the kernel. | |
650 | */ | |
651 | ||
c415d895 | 652 | copy_nexthops(&nhg->nexthop, depend->nhg.nexthop, NULL); |
e22e8001 | 653 | } |
1b366e63 SW |
654 | |
655 | return 0; | |
e22e8001 SW |
656 | } |
657 | ||
6384cbcb | 658 | static void handle_recursive_depend(struct nhg_connected_tree_head *nhg_depends, |
0885b1e3 | 659 | struct nexthop *nh, afi_t afi, int type) |
6384cbcb SW |
660 | { |
661 | struct nhg_hash_entry *depend = NULL; | |
662 | struct nexthop_group resolved_ng = {}; | |
663 | ||
1d049aba | 664 | resolved_ng.nexthop = nh; |
6384cbcb | 665 | |
377e29f7 MS |
666 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
667 | zlog_debug("%s: head %p, nh %pNHv", | |
668 | __func__, nhg_depends, nh); | |
669 | ||
0885b1e3 | 670 | depend = zebra_nhg_rib_find(0, &resolved_ng, afi, type); |
a7e1b02d | 671 | |
377e29f7 MS |
672 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
673 | zlog_debug("%s: nh %pNHv => %p (%u)", | |
674 | __func__, nh, depend, | |
675 | depend ? depend->id : 0); | |
676 | ||
a7e1b02d SW |
677 | if (depend) |
678 | depends_add(nhg_depends, depend); | |
6384cbcb | 679 | } |
e22e8001 | 680 | |
377e29f7 MS |
681 | /* |
682 | * Lookup an nhe in the global hash, using data from another nhe. If 'lookup' | |
683 | * has an id value, that's used. Create a new global/shared nhe if not found. | |
684 | */ | |
685 | static bool zebra_nhe_find(struct nhg_hash_entry **nhe, /* return value */ | |
686 | struct nhg_hash_entry *lookup, | |
687 | struct nhg_connected_tree_head *nhg_depends, | |
5588801e | 688 | afi_t afi, bool from_dplane) |
377e29f7 MS |
689 | { |
690 | bool created = false; | |
691 | bool recursive = false; | |
0328a5bd | 692 | struct nhg_hash_entry *newnhe, *backup_nhe; |
377e29f7 MS |
693 | struct nexthop *nh = NULL; |
694 | ||
695 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
5588801e SW |
696 | zlog_debug( |
697 | "%s: id %u, lookup %p, vrf %d, type %d, depends %p%s", | |
698 | __func__, lookup->id, lookup, lookup->vrf_id, | |
699 | lookup->type, nhg_depends, | |
700 | (from_dplane ? " (from dplane)" : "")); | |
377e29f7 MS |
701 | |
702 | if (lookup->id) | |
703 | (*nhe) = zebra_nhg_lookup_id(lookup->id); | |
704 | else | |
705 | (*nhe) = hash_lookup(zrouter.nhgs, lookup); | |
706 | ||
707 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be | 708 | zlog_debug("%s: lookup => %p (%pNG)", __func__, *nhe, *nhe); |
377e29f7 MS |
709 | |
710 | /* If we found an existing object, we're done */ | |
711 | if (*nhe) | |
712 | goto done; | |
713 | ||
714 | /* We're going to create/insert a new nhe: | |
715 | * assign the next global id value if necessary. | |
716 | */ | |
717 | if (lookup->id == 0) | |
ac5d1091 | 718 | lookup->id = nhg_get_next_id(); |
0885b1e3 | 719 | |
475852b2 | 720 | if (!from_dplane && lookup->id < ZEBRA_NHG_PROTO_LOWER) { |
0885b1e3 SW |
721 | /* |
722 | * This is a zebra hashed/owned NHG. | |
723 | * | |
724 | * It goes in HASH and ID table. | |
725 | */ | |
726 | newnhe = hash_get(zrouter.nhgs, lookup, zebra_nhg_hash_alloc); | |
727 | zebra_nhg_insert_id(newnhe); | |
728 | } else { | |
729 | /* | |
475852b2 SW |
730 | * This is upperproto owned NHG or one we read in from dataplane |
731 | * and should not be hashed to. | |
0885b1e3 SW |
732 | * |
733 | * It goes in ID table. | |
734 | */ | |
735 | newnhe = | |
736 | hash_get(zrouter.nhgs_id, lookup, zebra_nhg_hash_alloc); | |
737 | } | |
738 | ||
377e29f7 MS |
739 | created = true; |
740 | ||
741 | /* Mail back the new object */ | |
742 | *nhe = newnhe; | |
743 | ||
744 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be DS |
745 | zlog_debug("%s: => created %p (%pNG)", __func__, newnhe, |
746 | newnhe); | |
377e29f7 MS |
747 | |
748 | /* Only hash/lookup the depends if the first lookup | |
749 | * fails to find something. This should hopefully save a | |
750 | * lot of cycles for larger ecmp sizes. | |
751 | */ | |
752 | if (nhg_depends) { | |
753 | /* If you don't want to hash on each nexthop in the | |
754 | * nexthop group struct you can pass the depends | |
755 | * directly. Kernel-side we do this since it just looks | |
756 | * them up via IDs. | |
757 | */ | |
758 | zebra_nhg_connect_depends(newnhe, nhg_depends); | |
759 | goto done; | |
760 | } | |
761 | ||
762 | /* Prepare dependency relationships if this is not a | |
763 | * singleton nexthop. There are two cases: a single | |
764 | * recursive nexthop, where we need a relationship to the | |
765 | * resolving nexthop; or a group of nexthops, where we need | |
766 | * relationships with the corresponding singletons. | |
767 | */ | |
84591282 | 768 | zebra_nhg_depends_init(newnhe); |
377e29f7 MS |
769 | |
770 | nh = newnhe->nhg.nexthop; | |
771 | ||
772 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE)) | |
773 | SET_FLAG(newnhe->flags, NEXTHOP_GROUP_VALID); | |
774 | ||
54c89c93 | 775 | if (nh->next == NULL && newnhe->id < ZEBRA_NHG_PROTO_LOWER) { |
377e29f7 MS |
776 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) { |
777 | /* Single recursive nexthop */ | |
778 | handle_recursive_depend(&newnhe->nhg_depends, | |
0885b1e3 SW |
779 | nh->resolved, afi, |
780 | newnhe->type); | |
377e29f7 MS |
781 | recursive = true; |
782 | } | |
783 | } else { | |
dd1e105f | 784 | /* Proto-owned are groups by default */ |
377e29f7 MS |
785 | /* List of nexthops */ |
786 | for (nh = newnhe->nhg.nexthop; nh; nh = nh->next) { | |
787 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
788 | zlog_debug("%s: depends NH %pNHv %s", | |
789 | __func__, nh, | |
790 | CHECK_FLAG(nh->flags, | |
791 | NEXTHOP_FLAG_RECURSIVE) ? | |
792 | "(R)" : ""); | |
793 | ||
0885b1e3 | 794 | depends_find_add(&newnhe->nhg_depends, nh, afi, |
5588801e | 795 | newnhe->type, from_dplane); |
377e29f7 MS |
796 | } |
797 | } | |
798 | ||
0328a5bd | 799 | if (recursive) |
84591282 SW |
800 | SET_FLAG(newnhe->flags, NEXTHOP_GROUP_RECURSIVE); |
801 | ||
802 | /* Attach dependent backpointers to singletons */ | |
803 | zebra_nhg_connect_depends(newnhe, &newnhe->nhg_depends); | |
804 | ||
805 | /** | |
806 | * Backup Nexthops | |
807 | */ | |
0328a5bd | 808 | |
377e29f7 MS |
809 | if (zebra_nhg_get_backup_nhg(newnhe) == NULL || |
810 | zebra_nhg_get_backup_nhg(newnhe)->nexthop == NULL) | |
0328a5bd MS |
811 | goto done; |
812 | ||
813 | /* If there are backup nexthops, add them to the backup | |
814 | * depends tree. The rules here are a little different. | |
815 | */ | |
816 | recursive = false; | |
817 | backup_nhe = newnhe->backup_info->nhe; | |
377e29f7 | 818 | |
0328a5bd | 819 | nh = backup_nhe->nhg.nexthop; |
377e29f7 MS |
820 | |
821 | /* Singleton recursive NH */ | |
822 | if (nh->next == NULL && | |
823 | CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) { | |
824 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
825 | zlog_debug("%s: backup depend NH %pNHv (R)", | |
826 | __func__, nh); | |
827 | ||
828 | /* Single recursive nexthop */ | |
0885b1e3 SW |
829 | handle_recursive_depend(&backup_nhe->nhg_depends, nh->resolved, |
830 | afi, backup_nhe->type); | |
377e29f7 MS |
831 | recursive = true; |
832 | } else { | |
833 | /* One or more backup NHs */ | |
834 | for (; nh; nh = nh->next) { | |
835 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
836 | zlog_debug("%s: backup depend NH %pNHv %s", | |
837 | __func__, nh, | |
838 | CHECK_FLAG(nh->flags, | |
839 | NEXTHOP_FLAG_RECURSIVE) ? | |
840 | "(R)" : ""); | |
841 | ||
0885b1e3 | 842 | depends_find_add(&backup_nhe->nhg_depends, nh, afi, |
5588801e | 843 | backup_nhe->type, from_dplane); |
377e29f7 MS |
844 | } |
845 | } | |
846 | ||
377e29f7 | 847 | if (recursive) |
0328a5bd | 848 | SET_FLAG(backup_nhe->flags, NEXTHOP_GROUP_RECURSIVE); |
377e29f7 MS |
849 | |
850 | done: | |
45691de9 SW |
851 | /* Reset time since last update */ |
852 | (*nhe)->uptime = monotime(NULL); | |
377e29f7 MS |
853 | |
854 | return created; | |
855 | } | |
856 | ||
857 | /* | |
858 | * Lookup or create an nhe, based on an nhg or an nhe id. | |
859 | */ | |
4505578b SW |
860 | static bool zebra_nhg_find(struct nhg_hash_entry **nhe, uint32_t id, |
861 | struct nexthop_group *nhg, | |
37c6708b | 862 | struct nhg_connected_tree_head *nhg_depends, |
5588801e SW |
863 | vrf_id_t vrf_id, afi_t afi, int type, |
864 | bool from_dplane) | |
a95b8020 | 865 | { |
0c8215cb | 866 | struct nhg_hash_entry lookup = {}; |
4505578b SW |
867 | bool created = false; |
868 | ||
1d48702e MS |
869 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
870 | zlog_debug("%s: id %u, nhg %p, vrf %d, type %d, depends %p", | |
871 | __func__, id, nhg, vrf_id, type, | |
872 | nhg_depends); | |
873 | ||
377e29f7 MS |
874 | /* Use a temporary nhe and call into the superset/common code */ |
875 | lookup.id = id; | |
9a1588c4 | 876 | lookup.type = type ? type : ZEBRA_ROUTE_NHG; |
c415d895 | 877 | lookup.nhg = *nhg; |
e22e8001 | 878 | |
88cafda7 | 879 | lookup.vrf_id = vrf_id; |
475852b2 | 880 | if (nhg_depends || lookup.nhg.nexthop->next) { |
6384cbcb SW |
881 | /* Groups can have all vrfs and AF's in them */ |
882 | lookup.afi = AFI_UNSPEC; | |
6384cbcb | 883 | } else { |
c415d895 | 884 | switch (lookup.nhg.nexthop->type) { |
4d21c7c0 SW |
885 | case (NEXTHOP_TYPE_IFINDEX): |
886 | case (NEXTHOP_TYPE_BLACKHOLE): | |
887 | /* | |
888 | * This switch case handles setting the afi different | |
889 | * for ipv4/v6 routes. Ifindex/blackhole nexthop | |
890 | * objects cannot be ambiguous, they must be Address | |
891 | * Family specific. If we get here, we will either use | |
892 | * the AF of the route, or the one we got passed from | |
893 | * here from the kernel. | |
894 | */ | |
895 | lookup.afi = afi; | |
896 | break; | |
897 | case (NEXTHOP_TYPE_IPV4_IFINDEX): | |
898 | case (NEXTHOP_TYPE_IPV4): | |
899 | lookup.afi = AFI_IP; | |
900 | break; | |
901 | case (NEXTHOP_TYPE_IPV6_IFINDEX): | |
902 | case (NEXTHOP_TYPE_IPV6): | |
903 | lookup.afi = AFI_IP6; | |
904 | break; | |
905 | } | |
6384cbcb | 906 | } |
a95b8020 | 907 | |
5588801e | 908 | created = zebra_nhe_find(nhe, &lookup, nhg_depends, afi, from_dplane); |
d9f5b2f5 | 909 | |
4505578b | 910 | return created; |
a95b8020 SW |
911 | } |
912 | ||
e22e8001 | 913 | /* Find/create a single nexthop */ |
5588801e SW |
914 | static struct nhg_hash_entry *zebra_nhg_find_nexthop(uint32_t id, |
915 | struct nexthop *nh, | |
916 | afi_t afi, int type, | |
917 | bool from_dplane) | |
3057df51 | 918 | { |
6384cbcb | 919 | struct nhg_hash_entry *nhe = NULL; |
e22e8001 | 920 | struct nexthop_group nhg = {}; |
88cafda7 | 921 | vrf_id_t vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nh->vrf_id; |
e22e8001 | 922 | |
0eb97b86 | 923 | nexthop_group_add_sorted(&nhg, nh); |
e22e8001 | 924 | |
5588801e | 925 | zebra_nhg_find(&nhe, id, &nhg, NULL, vrf_id, afi, type, from_dplane); |
8a507796 | 926 | |
377e29f7 | 927 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be | 928 | zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__, nh, nhe, nhe); |
377e29f7 | 929 | |
6384cbcb | 930 | return nhe; |
e22e8001 SW |
931 | } |
932 | ||
10200d40 SW |
933 | static uint32_t nhg_ctx_get_id(const struct nhg_ctx *ctx) |
934 | { | |
935 | return ctx->id; | |
936 | } | |
937 | ||
1b366e63 | 938 | static void nhg_ctx_set_status(struct nhg_ctx *ctx, enum nhg_ctx_status status) |
e22e8001 SW |
939 | { |
940 | ctx->status = status; | |
941 | } | |
942 | ||
1b366e63 | 943 | static enum nhg_ctx_status nhg_ctx_get_status(const struct nhg_ctx *ctx) |
e22e8001 SW |
944 | { |
945 | return ctx->status; | |
946 | } | |
947 | ||
948 | static void nhg_ctx_set_op(struct nhg_ctx *ctx, enum nhg_ctx_op_e op) | |
949 | { | |
950 | ctx->op = op; | |
951 | } | |
952 | ||
953 | static enum nhg_ctx_op_e nhg_ctx_get_op(const struct nhg_ctx *ctx) | |
954 | { | |
955 | return ctx->op; | |
956 | } | |
957 | ||
10200d40 SW |
958 | static vrf_id_t nhg_ctx_get_vrf_id(const struct nhg_ctx *ctx) |
959 | { | |
960 | return ctx->vrf_id; | |
961 | } | |
962 | ||
963 | static int nhg_ctx_get_type(const struct nhg_ctx *ctx) | |
964 | { | |
965 | return ctx->type; | |
966 | } | |
967 | ||
968 | static int nhg_ctx_get_afi(const struct nhg_ctx *ctx) | |
969 | { | |
970 | return ctx->afi; | |
971 | } | |
972 | ||
973 | static struct nexthop *nhg_ctx_get_nh(struct nhg_ctx *ctx) | |
974 | { | |
975 | return &ctx->u.nh; | |
976 | } | |
977 | ||
978 | static uint8_t nhg_ctx_get_count(const struct nhg_ctx *ctx) | |
979 | { | |
980 | return ctx->count; | |
981 | } | |
982 | ||
983 | static struct nh_grp *nhg_ctx_get_grp(struct nhg_ctx *ctx) | |
984 | { | |
985 | return ctx->u.grp; | |
986 | } | |
987 | ||
99e7ab12 | 988 | static struct nhg_ctx *nhg_ctx_new(void) |
7c6d5f25 | 989 | { |
99e7ab12 | 990 | struct nhg_ctx *new; |
7c6d5f25 SW |
991 | |
992 | new = XCALLOC(MTYPE_NHG_CTX, sizeof(struct nhg_ctx)); | |
993 | ||
994 | return new; | |
995 | } | |
996 | ||
04bec7b2 | 997 | void nhg_ctx_free(struct nhg_ctx **ctx) |
7c6d5f25 SW |
998 | { |
999 | struct nexthop *nh; | |
1000 | ||
1001 | if (ctx == NULL) | |
1002 | return; | |
1003 | ||
1004 | assert((*ctx) != NULL); | |
1005 | ||
1006 | if (nhg_ctx_get_count(*ctx)) | |
1007 | goto done; | |
1008 | ||
1009 | nh = nhg_ctx_get_nh(*ctx); | |
1010 | ||
1011 | nexthop_del_labels(nh); | |
eab0f8f0 HS |
1012 | nexthop_del_srv6_seg6local(nh); |
1013 | nexthop_del_srv6_seg6(nh); | |
7c6d5f25 SW |
1014 | |
1015 | done: | |
1016 | XFREE(MTYPE_NHG_CTX, *ctx); | |
7c6d5f25 SW |
1017 | } |
1018 | ||
81505946 SW |
1019 | static struct nhg_ctx *nhg_ctx_init(uint32_t id, struct nexthop *nh, |
1020 | struct nh_grp *grp, vrf_id_t vrf_id, | |
1021 | afi_t afi, int type, uint8_t count) | |
1022 | { | |
1023 | struct nhg_ctx *ctx = NULL; | |
1024 | ||
1025 | ctx = nhg_ctx_new(); | |
1026 | ||
1027 | ctx->id = id; | |
1028 | ctx->vrf_id = vrf_id; | |
1029 | ctx->afi = afi; | |
1030 | ctx->type = type; | |
1031 | ctx->count = count; | |
1032 | ||
1033 | if (count) | |
1034 | /* Copy over the array */ | |
1035 | memcpy(&ctx->u.grp, grp, count * sizeof(struct nh_grp)); | |
1036 | else if (nh) | |
1037 | ctx->u.nh = *nh; | |
1038 | ||
1039 | return ctx; | |
1040 | } | |
1041 | ||
80286aa5 SW |
1042 | static void zebra_nhg_set_valid(struct nhg_hash_entry *nhe) |
1043 | { | |
1044 | struct nhg_connected *rb_node_dep; | |
1045 | ||
1046 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
1047 | ||
1048 | frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) | |
1049 | zebra_nhg_set_valid(rb_node_dep->nhe); | |
1050 | } | |
1051 | ||
1052 | static void zebra_nhg_set_invalid(struct nhg_hash_entry *nhe) | |
1053 | { | |
1054 | struct nhg_connected *rb_node_dep; | |
1055 | ||
1056 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
1057 | ||
382858d0 DS |
1058 | /* If we're in shutdown, this interface event needs to clean |
1059 | * up installed NHGs, so don't clear that flag directly. | |
1060 | */ | |
1061 | if (!zrouter.in_shutdown) | |
1062 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
1063 | ||
80286aa5 SW |
1064 | /* Update validity of nexthops depending on it */ |
1065 | frr_each(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) | |
1066 | zebra_nhg_check_valid(rb_node_dep->nhe); | |
1067 | } | |
1068 | ||
1069 | void zebra_nhg_check_valid(struct nhg_hash_entry *nhe) | |
1070 | { | |
1071 | struct nhg_connected *rb_node_dep = NULL; | |
1072 | bool valid = false; | |
1073 | ||
1074 | /* If anthing else in the group is valid, the group is valid */ | |
1075 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { | |
1076 | if (CHECK_FLAG(rb_node_dep->nhe->flags, NEXTHOP_GROUP_VALID)) { | |
1077 | valid = true; | |
1078 | goto done; | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | done: | |
1083 | if (valid) | |
1084 | zebra_nhg_set_valid(nhe); | |
1085 | else | |
1086 | zebra_nhg_set_invalid(nhe); | |
1087 | } | |
1088 | ||
dd1e105f | 1089 | static void zebra_nhg_release_all_deps(struct nhg_hash_entry *nhe) |
9a1588c4 SW |
1090 | { |
1091 | /* Remove it from any lists it may be on */ | |
1092 | zebra_nhg_depends_release(nhe); | |
1093 | zebra_nhg_dependents_release(nhe); | |
1094 | if (nhe->ifp) | |
1095 | if_nhg_dependents_del(nhe->ifp, nhe); | |
dd1e105f SW |
1096 | } |
1097 | ||
1098 | static void zebra_nhg_release(struct nhg_hash_entry *nhe) | |
1099 | { | |
1100 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be | 1101 | zlog_debug("%s: nhe %p (%pNG)", __func__, nhe, nhe); |
dd1e105f SW |
1102 | |
1103 | zebra_nhg_release_all_deps(nhe); | |
9a1588c4 SW |
1104 | |
1105 | /* | |
0885b1e3 | 1106 | * If its not zebra owned, we didn't store it here and have to be |
9a1588c4 SW |
1107 | * sure we don't clear one thats actually being used. |
1108 | */ | |
3bccc0f5 | 1109 | if (nhe->id < ZEBRA_NHG_PROTO_LOWER) |
9a1588c4 | 1110 | hash_release(zrouter.nhgs, nhe); |
9a1588c4 | 1111 | |
9a1588c4 SW |
1112 | hash_release(zrouter.nhgs_id, nhe); |
1113 | } | |
1114 | ||
9a1588c4 SW |
1115 | static void zebra_nhg_handle_uninstall(struct nhg_hash_entry *nhe) |
1116 | { | |
177e711d | 1117 | zebra_nhg_release(nhe); |
9a1588c4 SW |
1118 | zebra_nhg_free(nhe); |
1119 | } | |
1120 | ||
80286aa5 SW |
1121 | static void zebra_nhg_handle_install(struct nhg_hash_entry *nhe) |
1122 | { | |
1123 | /* Update validity of groups depending on it */ | |
1124 | struct nhg_connected *rb_node_dep; | |
1125 | ||
1126 | frr_each_safe(nhg_connected_tree, &nhe->nhg_dependents, rb_node_dep) | |
1127 | zebra_nhg_set_valid(rb_node_dep->nhe); | |
1128 | } | |
1129 | ||
9a1588c4 SW |
1130 | /* |
1131 | * The kernel/other program has changed the state of a nexthop object we are | |
1132 | * using. | |
1133 | */ | |
1134 | static void zebra_nhg_handle_kernel_state_change(struct nhg_hash_entry *nhe, | |
1135 | bool is_delete) | |
1136 | { | |
1137 | if (nhe->refcnt) { | |
1138 | flog_err( | |
1139 | EC_ZEBRA_NHG_SYNC, | |
68d188be DS |
1140 | "Kernel %s a nexthop group with ID (%pNG) that we are still using for a route, sending it back down", |
1141 | (is_delete ? "deleted" : "updated"), nhe); | |
9a1588c4 SW |
1142 | |
1143 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
1144 | zebra_nhg_install_kernel(nhe); | |
177e711d | 1145 | } else |
9a1588c4 | 1146 | zebra_nhg_handle_uninstall(nhe); |
9a1588c4 SW |
1147 | } |
1148 | ||
e22e8001 SW |
1149 | static int nhg_ctx_process_new(struct nhg_ctx *ctx) |
1150 | { | |
1151 | struct nexthop_group *nhg = NULL; | |
37c6708b | 1152 | struct nhg_connected_tree_head nhg_depends = {}; |
9a1588c4 | 1153 | struct nhg_hash_entry *lookup = NULL; |
3057df51 SW |
1154 | struct nhg_hash_entry *nhe = NULL; |
1155 | ||
10200d40 SW |
1156 | uint32_t id = nhg_ctx_get_id(ctx); |
1157 | uint8_t count = nhg_ctx_get_count(ctx); | |
1158 | vrf_id_t vrf_id = nhg_ctx_get_vrf_id(ctx); | |
1159 | int type = nhg_ctx_get_type(ctx); | |
1160 | afi_t afi = nhg_ctx_get_afi(ctx); | |
1161 | ||
1162 | lookup = zebra_nhg_lookup_id(id); | |
9a1588c4 | 1163 | |
377e29f7 MS |
1164 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1165 | zlog_debug("%s: id %u, count %d, lookup => %p", | |
1166 | __func__, id, count, lookup); | |
1167 | ||
9a1588c4 SW |
1168 | if (lookup) { |
1169 | /* This is already present in our table, hence an update | |
1170 | * that we did not initate. | |
1171 | */ | |
1172 | zebra_nhg_handle_kernel_state_change(lookup, false); | |
1173 | return 0; | |
1174 | } | |
1175 | ||
10200d40 | 1176 | if (nhg_ctx_get_count(ctx)) { |
e22e8001 | 1177 | nhg = nexthop_group_new(); |
1b366e63 SW |
1178 | if (zebra_nhg_process_grp(nhg, &nhg_depends, |
1179 | nhg_ctx_get_grp(ctx), count)) { | |
1180 | depends_decrement_free(&nhg_depends); | |
d3a35138 | 1181 | nexthop_group_delete(&nhg); |
fec211ad | 1182 | return -ENOENT; |
1b366e63 SW |
1183 | } |
1184 | ||
0885b1e3 | 1185 | if (!zebra_nhg_find(&nhe, id, nhg, &nhg_depends, vrf_id, afi, |
5588801e | 1186 | type, true)) |
38e40db1 | 1187 | depends_decrement_free(&nhg_depends); |
4505578b | 1188 | |
e22e8001 | 1189 | /* These got copied over in zebra_nhg_alloc() */ |
d3a35138 | 1190 | nexthop_group_delete(&nhg); |
38e40db1 | 1191 | } else |
5588801e SW |
1192 | nhe = zebra_nhg_find_nexthop(id, nhg_ctx_get_nh(ctx), afi, type, |
1193 | true); | |
e22e8001 | 1194 | |
5b27c09d | 1195 | if (!nhe) { |
e22e8001 SW |
1196 | flog_err( |
1197 | EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
1198 | "Zebra failed to find or create a nexthop hash entry for ID (%u)", | |
10200d40 | 1199 | id); |
e22e8001 SW |
1200 | return -1; |
1201 | } | |
1202 | ||
5b27c09d | 1203 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be | 1204 | zlog_debug("%s: nhe %p (%pNG) is new", __func__, nhe, nhe); |
5b27c09d | 1205 | |
dc65cd99 SW |
1206 | /* |
1207 | * If daemon nhg from the kernel, add a refcnt here to indicate the | |
1208 | * daemon owns it. | |
1209 | */ | |
1210 | if (PROTO_OWNED(nhe)) | |
1211 | zebra_nhg_increment_ref(nhe); | |
1212 | ||
5b27c09d SW |
1213 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); |
1214 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
1215 | ||
e22e8001 SW |
1216 | return 0; |
1217 | } | |
1218 | ||
9a1588c4 SW |
1219 | static int nhg_ctx_process_del(struct nhg_ctx *ctx) |
1220 | { | |
1221 | struct nhg_hash_entry *nhe = NULL; | |
10200d40 | 1222 | uint32_t id = nhg_ctx_get_id(ctx); |
9a1588c4 | 1223 | |
10200d40 | 1224 | nhe = zebra_nhg_lookup_id(id); |
9a1588c4 SW |
1225 | |
1226 | if (!nhe) { | |
1227 | flog_warn( | |
1228 | EC_ZEBRA_BAD_NHG_MESSAGE, | |
1229 | "Kernel delete message received for nexthop group ID (%u) that we do not have in our ID table", | |
10200d40 | 1230 | id); |
81505946 | 1231 | return -1; |
9a1588c4 SW |
1232 | } |
1233 | ||
1234 | zebra_nhg_handle_kernel_state_change(nhe, true); | |
1235 | ||
1236 | return 0; | |
1237 | } | |
1238 | ||
7c6d5f25 | 1239 | static void nhg_ctx_fini(struct nhg_ctx **ctx) |
e22e8001 SW |
1240 | { |
1241 | /* | |
1242 | * Just freeing for now, maybe do something more in the future | |
1243 | * based on flag. | |
1244 | */ | |
1245 | ||
7134ba70 | 1246 | nhg_ctx_free(ctx); |
e22e8001 SW |
1247 | } |
1248 | ||
1b366e63 SW |
1249 | static int queue_add(struct nhg_ctx *ctx) |
1250 | { | |
1251 | /* If its queued or already processed do nothing */ | |
1252 | if (nhg_ctx_get_status(ctx) == NHG_CTX_QUEUED) | |
1253 | return 0; | |
1254 | ||
04bec7b2 | 1255 | if (rib_queue_nhg_ctx_add(ctx)) { |
1b366e63 SW |
1256 | nhg_ctx_set_status(ctx, NHG_CTX_FAILURE); |
1257 | return -1; | |
1258 | } | |
1259 | ||
1260 | nhg_ctx_set_status(ctx, NHG_CTX_QUEUED); | |
1261 | ||
1262 | return 0; | |
1263 | } | |
1264 | ||
e22e8001 SW |
1265 | int nhg_ctx_process(struct nhg_ctx *ctx) |
1266 | { | |
1267 | int ret = 0; | |
1268 | ||
1269 | switch (nhg_ctx_get_op(ctx)) { | |
1270 | case NHG_CTX_OP_NEW: | |
1271 | ret = nhg_ctx_process_new(ctx); | |
fec211ad | 1272 | if (nhg_ctx_get_count(ctx) && ret == -ENOENT |
1b366e63 | 1273 | && nhg_ctx_get_status(ctx) != NHG_CTX_REQUEUED) { |
e1292378 SW |
1274 | /** |
1275 | * We have entered a situation where we are | |
1276 | * processing a group from the kernel | |
1277 | * that has a contained nexthop which | |
1278 | * we have not yet processed. | |
1b366e63 | 1279 | * |
e1292378 SW |
1280 | * Re-enqueue this ctx to be handled exactly one |
1281 | * more time (indicated by the flag). | |
1282 | * | |
1283 | * By the time we get back to it, we | |
1284 | * should have processed its depends. | |
1b366e63 SW |
1285 | */ |
1286 | nhg_ctx_set_status(ctx, NHG_CTX_NONE); | |
1287 | if (queue_add(ctx) == 0) { | |
1288 | nhg_ctx_set_status(ctx, NHG_CTX_REQUEUED); | |
1289 | return 0; | |
1290 | } | |
1291 | } | |
e22e8001 SW |
1292 | break; |
1293 | case NHG_CTX_OP_DEL: | |
9a1588c4 | 1294 | ret = nhg_ctx_process_del(ctx); |
e22e8001 SW |
1295 | case NHG_CTX_OP_NONE: |
1296 | break; | |
1297 | } | |
1298 | ||
1299 | nhg_ctx_set_status(ctx, (ret ? NHG_CTX_FAILURE : NHG_CTX_SUCCESS)); | |
1300 | ||
7c6d5f25 | 1301 | nhg_ctx_fini(&ctx); |
e22e8001 SW |
1302 | |
1303 | return ret; | |
1304 | } | |
3057df51 | 1305 | |
e22e8001 SW |
1306 | /* Kernel-side, you either get a single new nexthop or a array of ID's */ |
1307 | int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, struct nh_grp *grp, | |
38e40db1 SW |
1308 | uint8_t count, vrf_id_t vrf_id, afi_t afi, int type, |
1309 | int startup) | |
e22e8001 | 1310 | { |
e22e8001 SW |
1311 | struct nhg_ctx *ctx = NULL; |
1312 | ||
377e29f7 MS |
1313 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1314 | zlog_debug("%s: nh %pNHv, id %u, count %d", | |
1315 | __func__, nh, id, (int)count); | |
1316 | ||
54c89c93 | 1317 | if (id > id_counter && id < ZEBRA_NHG_PROTO_LOWER) |
38e40db1 SW |
1318 | /* Increase our counter so we don't try to create |
1319 | * an ID that already exists | |
1320 | */ | |
1321 | id_counter = id; | |
1322 | ||
81505946 | 1323 | ctx = nhg_ctx_init(id, nh, grp, vrf_id, afi, type, count); |
e22e8001 SW |
1324 | nhg_ctx_set_op(ctx, NHG_CTX_OP_NEW); |
1325 | ||
38e40db1 SW |
1326 | /* Under statup conditions, we need to handle them immediately |
1327 | * like we do for routes. Otherwise, we are going to get a route | |
1328 | * with a nhe_id that we have not handled. | |
1329 | */ | |
1330 | if (startup) | |
1331 | return nhg_ctx_process(ctx); | |
1332 | ||
e22e8001 | 1333 | if (queue_add(ctx)) { |
7c6d5f25 | 1334 | nhg_ctx_fini(&ctx); |
e22e8001 SW |
1335 | return -1; |
1336 | } | |
1337 | ||
1338 | return 0; | |
1339 | } | |
1340 | ||
9a1588c4 | 1341 | /* Kernel-side, received delete message */ |
88cafda7 | 1342 | int zebra_nhg_kernel_del(uint32_t id, vrf_id_t vrf_id) |
9a1588c4 SW |
1343 | { |
1344 | struct nhg_ctx *ctx = NULL; | |
1345 | ||
88cafda7 | 1346 | ctx = nhg_ctx_init(id, NULL, NULL, vrf_id, 0, 0, 0); |
9a1588c4 SW |
1347 | |
1348 | nhg_ctx_set_op(ctx, NHG_CTX_OP_DEL); | |
1349 | ||
1350 | if (queue_add(ctx)) { | |
7c6d5f25 | 1351 | nhg_ctx_fini(&ctx); |
9a1588c4 SW |
1352 | return -1; |
1353 | } | |
1354 | ||
1355 | return 0; | |
1356 | } | |
1357 | ||
5657e7e9 | 1358 | /* Some dependency helper functions */ |
0fff714e | 1359 | static struct nhg_hash_entry *depends_find_recursive(const struct nexthop *nh, |
0885b1e3 | 1360 | afi_t afi, int type) |
98cda54a | 1361 | { |
0fff714e SW |
1362 | struct nhg_hash_entry *nhe; |
1363 | struct nexthop *lookup = NULL; | |
98cda54a | 1364 | |
77bf9504 | 1365 | lookup = nexthop_dup(nh, NULL); |
0fff714e | 1366 | |
5588801e | 1367 | nhe = zebra_nhg_find_nexthop(0, lookup, afi, type, false); |
0fff714e SW |
1368 | |
1369 | nexthops_free(lookup); | |
1370 | ||
1371 | return nhe; | |
1372 | } | |
1373 | ||
1374 | static struct nhg_hash_entry *depends_find_singleton(const struct nexthop *nh, | |
5588801e SW |
1375 | afi_t afi, int type, |
1376 | bool from_dplane) | |
0fff714e SW |
1377 | { |
1378 | struct nhg_hash_entry *nhe; | |
1379 | struct nexthop lookup = {}; | |
606fa9e5 | 1380 | |
cb86eba3 MS |
1381 | /* Capture a snapshot of this single nh; it might be part of a list, |
1382 | * so we need to make a standalone copy. | |
1383 | */ | |
77bf9504 | 1384 | nexthop_copy_no_recurse(&lookup, nh, NULL); |
8a507796 | 1385 | |
5588801e | 1386 | nhe = zebra_nhg_find_nexthop(0, &lookup, afi, type, from_dplane); |
8a507796 | 1387 | |
cb86eba3 MS |
1388 | /* The copy may have allocated labels; free them if necessary. */ |
1389 | nexthop_del_labels(&lookup); | |
eab0f8f0 HS |
1390 | nexthop_del_srv6_seg6local(&lookup); |
1391 | nexthop_del_srv6_seg6(&lookup); | |
4505578b | 1392 | |
377e29f7 | 1393 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be | 1394 | zlog_debug("%s: nh %pNHv => %p (%pNG)", __func__, nh, nhe, nhe); |
377e29f7 | 1395 | |
0fff714e SW |
1396 | return nhe; |
1397 | } | |
1398 | ||
0885b1e3 | 1399 | static struct nhg_hash_entry *depends_find(const struct nexthop *nh, afi_t afi, |
5588801e | 1400 | int type, bool from_dplane) |
0fff714e SW |
1401 | { |
1402 | struct nhg_hash_entry *nhe = NULL; | |
1403 | ||
1404 | if (!nh) | |
1405 | goto done; | |
1406 | ||
1407 | /* We are separating these functions out to increase handling speed | |
1408 | * in the non-recursive case (by not alloc/freeing) | |
1409 | */ | |
bed74d17 | 1410 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) |
0885b1e3 | 1411 | nhe = depends_find_recursive(nh, afi, type); |
bed74d17 | 1412 | else |
5588801e | 1413 | nhe = depends_find_singleton(nh, afi, type, from_dplane); |
377e29f7 | 1414 | |
bed74d17 DS |
1415 | |
1416 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) { | |
68d188be | 1417 | zlog_debug("%s: nh %pNHv %s => %p (%pNG)", __func__, nh, |
bed74d17 DS |
1418 | CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE) ? "(R)" |
1419 | : "", | |
68d188be | 1420 | nhe, nhe); |
bed74d17 | 1421 | } |
0fff714e | 1422 | |
606fa9e5 | 1423 | done: |
4505578b | 1424 | return nhe; |
98cda54a SW |
1425 | } |
1426 | ||
37c6708b | 1427 | static void depends_add(struct nhg_connected_tree_head *head, |
5657e7e9 SW |
1428 | struct nhg_hash_entry *depend) |
1429 | { | |
377e29f7 MS |
1430 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1431 | zlog_debug("%s: head %p nh %pNHv", | |
1432 | __func__, head, depend->nhg.nexthop); | |
1433 | ||
5bf15faa SW |
1434 | /* If NULL is returned, it was successfully added and |
1435 | * needs to have its refcnt incremented. | |
1436 | * | |
1437 | * Else the NHE is already present in the tree and doesn't | |
1438 | * need to increment the refcnt. | |
1439 | */ | |
1440 | if (nhg_connected_tree_add_nhe(head, depend) == NULL) | |
1441 | zebra_nhg_increment_ref(depend); | |
5657e7e9 SW |
1442 | } |
1443 | ||
38e40db1 SW |
1444 | static struct nhg_hash_entry * |
1445 | depends_find_add(struct nhg_connected_tree_head *head, struct nexthop *nh, | |
5588801e | 1446 | afi_t afi, int type, bool from_dplane) |
5657e7e9 SW |
1447 | { |
1448 | struct nhg_hash_entry *depend = NULL; | |
1449 | ||
5588801e | 1450 | depend = depends_find(nh, afi, type, from_dplane); |
1b366e63 | 1451 | |
1d48702e MS |
1452 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
1453 | zlog_debug("%s: nh %pNHv => %p", | |
1454 | __func__, nh, depend); | |
1455 | ||
1b366e63 SW |
1456 | if (depend) |
1457 | depends_add(head, depend); | |
38e40db1 SW |
1458 | |
1459 | return depend; | |
1460 | } | |
1461 | ||
1462 | static struct nhg_hash_entry * | |
1463 | depends_find_id_add(struct nhg_connected_tree_head *head, uint32_t id) | |
1464 | { | |
1465 | struct nhg_hash_entry *depend = NULL; | |
1466 | ||
1467 | depend = zebra_nhg_lookup_id(id); | |
1b366e63 SW |
1468 | |
1469 | if (depend) | |
1470 | depends_add(head, depend); | |
38e40db1 SW |
1471 | |
1472 | return depend; | |
5657e7e9 SW |
1473 | } |
1474 | ||
37c6708b | 1475 | static void depends_decrement_free(struct nhg_connected_tree_head *head) |
5657e7e9 | 1476 | { |
37c6708b SW |
1477 | nhg_connected_tree_decrement_ref(head); |
1478 | nhg_connected_tree_free(head); | |
5657e7e9 SW |
1479 | } |
1480 | ||
377e29f7 | 1481 | /* Find an nhe based on a list of nexthops */ |
0885b1e3 SW |
1482 | struct nhg_hash_entry *zebra_nhg_rib_find(uint32_t id, |
1483 | struct nexthop_group *nhg, | |
1484 | afi_t rt_afi, int type) | |
e22e8001 SW |
1485 | { |
1486 | struct nhg_hash_entry *nhe = NULL; | |
88cafda7 DS |
1487 | vrf_id_t vrf_id; |
1488 | ||
1489 | /* | |
1490 | * CLANG SA is complaining that nexthop may be NULL | |
1491 | * Make it happy but this is ridonc | |
1492 | */ | |
1493 | assert(nhg->nexthop); | |
1494 | vrf_id = !vrf_is_backend_netns() ? VRF_DEFAULT : nhg->nexthop->vrf_id; | |
98cda54a | 1495 | |
5588801e | 1496 | zebra_nhg_find(&nhe, id, nhg, NULL, vrf_id, rt_afi, type, false); |
4505578b | 1497 | |
377e29f7 | 1498 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be | 1499 | zlog_debug("%s: => nhe %p (%pNG)", __func__, nhe, nhe); |
377e29f7 MS |
1500 | |
1501 | return nhe; | |
1502 | } | |
1503 | ||
1504 | /* Find an nhe based on a route's nhe */ | |
1505 | struct nhg_hash_entry * | |
1506 | zebra_nhg_rib_find_nhe(struct nhg_hash_entry *rt_nhe, afi_t rt_afi) | |
1507 | { | |
1508 | struct nhg_hash_entry *nhe = NULL; | |
1509 | ||
1510 | if (!(rt_nhe && rt_nhe->nhg.nexthop)) { | |
1511 | flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
1512 | "No nexthop passed to %s", __func__); | |
1513 | return NULL; | |
1514 | } | |
1515 | ||
1516 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be | 1517 | zlog_debug("%s: rt_nhe %p (%pNG)", __func__, rt_nhe, rt_nhe); |
377e29f7 | 1518 | |
5588801e | 1519 | zebra_nhe_find(&nhe, rt_nhe, NULL, rt_afi, false); |
377e29f7 MS |
1520 | |
1521 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be | 1522 | zlog_debug("%s: => nhe %p (%pNG)", __func__, nhe, nhe); |
377e29f7 | 1523 | |
3057df51 SW |
1524 | return nhe; |
1525 | } | |
1526 | ||
1d48702e MS |
1527 | /* |
1528 | * Allocate backup nexthop info object. Typically these are embedded in | |
1529 | * nhg_hash_entry objects. | |
1530 | */ | |
1531 | struct nhg_backup_info *zebra_nhg_backup_alloc(void) | |
1532 | { | |
1533 | struct nhg_backup_info *p; | |
1534 | ||
1535 | p = XCALLOC(MTYPE_NHG, sizeof(struct nhg_backup_info)); | |
1536 | ||
1537 | p->nhe = zebra_nhg_alloc(); | |
1538 | ||
1539 | /* Identify the embedded group used to hold the list of backups */ | |
1540 | SET_FLAG(p->nhe->flags, NEXTHOP_GROUP_BACKUP); | |
1541 | ||
1542 | return p; | |
1543 | } | |
1544 | ||
1545 | /* | |
1546 | * Free backup nexthop info object, deal with any embedded allocations | |
1547 | */ | |
1548 | void zebra_nhg_backup_free(struct nhg_backup_info **p) | |
1549 | { | |
1550 | if (p && *p) { | |
1551 | if ((*p)->nhe) | |
1552 | zebra_nhg_free((*p)->nhe); | |
1553 | ||
1554 | XFREE(MTYPE_NHG, (*p)); | |
1555 | } | |
1556 | } | |
1557 | ||
1d48702e MS |
1558 | /* Accessor for backup nexthop group */ |
1559 | struct nexthop_group *zebra_nhg_get_backup_nhg(struct nhg_hash_entry *nhe) | |
1560 | { | |
1561 | struct nexthop_group *p = NULL; | |
1562 | ||
1563 | if (nhe) { | |
1564 | if (nhe->backup_info && nhe->backup_info->nhe) | |
1565 | p = &(nhe->backup_info->nhe->nhg); | |
1566 | } | |
1567 | ||
1568 | return p; | |
1569 | } | |
1570 | ||
1571 | /* | |
1572 | * Helper to return a copy of a backup_info - note that this is a shallow | |
1573 | * copy, meant to be used when creating a new nhe from info passed in with | |
1574 | * a route e.g. | |
1575 | */ | |
1576 | static struct nhg_backup_info * | |
1577 | nhg_backup_copy(const struct nhg_backup_info *orig) | |
1578 | { | |
1579 | struct nhg_backup_info *b; | |
1580 | ||
1581 | b = zebra_nhg_backup_alloc(); | |
1582 | ||
1583 | /* Copy list of nexthops */ | |
1584 | nexthop_group_copy(&(b->nhe->nhg), &(orig->nhe->nhg)); | |
1585 | ||
1586 | return b; | |
1587 | } | |
1588 | ||
5948f013 | 1589 | static void zebra_nhg_free_members(struct nhg_hash_entry *nhe) |
b599cd2a | 1590 | { |
c415d895 MS |
1591 | nexthops_free(nhe->nhg.nexthop); |
1592 | ||
1d48702e MS |
1593 | zebra_nhg_backup_free(&nhe->backup_info); |
1594 | ||
58396544 | 1595 | /* Decrement to remove connection ref */ |
37c6708b SW |
1596 | nhg_connected_tree_decrement_ref(&nhe->nhg_depends); |
1597 | nhg_connected_tree_free(&nhe->nhg_depends); | |
1598 | nhg_connected_tree_free(&nhe->nhg_dependents); | |
b599cd2a SW |
1599 | } |
1600 | ||
0eb97b86 | 1601 | void zebra_nhg_free(struct nhg_hash_entry *nhe) |
a95b8020 | 1602 | { |
377e29f7 MS |
1603 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) { |
1604 | /* Group or singleton? */ | |
1605 | if (nhe->nhg.nexthop && nhe->nhg.nexthop->next) | |
68d188be DS |
1606 | zlog_debug("%s: nhe %p (%pNG), refcnt %d", __func__, |
1607 | nhe, nhe, nhe->refcnt); | |
377e29f7 | 1608 | else |
68d188be DS |
1609 | zlog_debug("%s: nhe %p (%pNG), refcnt %d, NH %pNHv", |
1610 | __func__, nhe, nhe, nhe->refcnt, | |
377e29f7 MS |
1611 | nhe->nhg.nexthop); |
1612 | } | |
1613 | ||
9d1fec4c DS |
1614 | THREAD_OFF(nhe->timer); |
1615 | ||
38e40db1 | 1616 | if (nhe->refcnt) |
68d188be | 1617 | zlog_debug("nhe_id=%pNG hash refcnt=%d", nhe, nhe->refcnt); |
38e40db1 | 1618 | |
8e401b25 | 1619 | zebra_nhg_free_members(nhe); |
51d80884 SW |
1620 | |
1621 | XFREE(MTYPE_NHG, nhe); | |
a95b8020 SW |
1622 | } |
1623 | ||
0eb97b86 MS |
1624 | void zebra_nhg_hash_free(void *p) |
1625 | { | |
84591282 | 1626 | zebra_nhg_release_all_deps((struct nhg_hash_entry *)p); |
0eb97b86 MS |
1627 | zebra_nhg_free((struct nhg_hash_entry *)p); |
1628 | } | |
1629 | ||
35729f38 DS |
1630 | static void zebra_nhg_timer(struct thread *thread) |
1631 | { | |
1632 | struct nhg_hash_entry *nhe = THREAD_ARG(thread); | |
1633 | ||
1634 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1635 | zlog_debug("Nexthop Timer for nhe: %pNG", nhe); | |
1636 | ||
1637 | if (nhe->refcnt == 1) | |
1638 | zebra_nhg_decrement_ref(nhe); | |
1639 | } | |
1640 | ||
d9f5b2f5 SW |
1641 | void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe) |
1642 | { | |
377e29f7 | 1643 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be DS |
1644 | zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__, nhe, nhe, |
1645 | nhe->refcnt, nhe->refcnt - 1); | |
377e29f7 | 1646 | |
e22e8001 SW |
1647 | nhe->refcnt--; |
1648 | ||
35729f38 DS |
1649 | if (!zrouter.in_shutdown && nhe->refcnt <= 0 && |
1650 | CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED) && | |
1651 | !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_KEEP_AROUND)) { | |
1652 | nhe->refcnt = 1; | |
1653 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_KEEP_AROUND); | |
c9af62e3 DS |
1654 | thread_add_timer(zrouter.master, zebra_nhg_timer, nhe, |
1655 | zrouter.nhg_keep, &nhe->timer); | |
34a67a7d | 1656 | return; |
35729f38 DS |
1657 | } |
1658 | ||
32e29e79 | 1659 | if (!zebra_nhg_depends_is_empty(nhe)) |
37c6708b | 1660 | nhg_connected_tree_decrement_ref(&nhe->nhg_depends); |
f54ef6a5 | 1661 | |
38e40db1 | 1662 | if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0) |
cb50cbc9 | 1663 | zebra_nhg_uninstall_kernel(nhe); |
7fd392cc SW |
1664 | } |
1665 | ||
7fd392cc SW |
1666 | void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe) |
1667 | { | |
377e29f7 | 1668 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be DS |
1669 | zlog_debug("%s: nhe %p (%pNG) %d => %d", __func__, nhe, nhe, |
1670 | nhe->refcnt, nhe->refcnt + 1); | |
377e29f7 | 1671 | |
e22e8001 SW |
1672 | nhe->refcnt++; |
1673 | ||
35729f38 DS |
1674 | if (thread_is_scheduled(nhe->timer)) { |
1675 | THREAD_OFF(nhe->timer); | |
1676 | nhe->refcnt--; | |
1677 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_KEEP_AROUND); | |
1678 | } | |
1679 | ||
32e29e79 | 1680 | if (!zebra_nhg_depends_is_empty(nhe)) |
37c6708b | 1681 | nhg_connected_tree_increment_ref(&nhe->nhg_depends); |
e22e8001 | 1682 | } |
d9f5b2f5 | 1683 | |
5530d55d MS |
1684 | static struct nexthop *nexthop_set_resolved(afi_t afi, |
1685 | const struct nexthop *newhop, | |
1686 | struct nexthop *nexthop, | |
1687 | struct zebra_sr_policy *policy) | |
ad28e79a SW |
1688 | { |
1689 | struct nexthop *resolved_hop; | |
b43434ad SW |
1690 | uint8_t num_labels = 0; |
1691 | mpls_label_t labels[MPLS_MAX_LABELS]; | |
1692 | enum lsp_types_t label_type = ZEBRA_LSP_NONE; | |
1693 | int i = 0; | |
ad28e79a SW |
1694 | |
1695 | resolved_hop = nexthop_new(); | |
1696 | SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); | |
1697 | ||
1698 | resolved_hop->vrf_id = nexthop->vrf_id; | |
1699 | switch (newhop->type) { | |
1700 | case NEXTHOP_TYPE_IPV4: | |
1701 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1702 | /* If the resolving route specifies a gateway, use it */ | |
1703 | resolved_hop->type = newhop->type; | |
1704 | resolved_hop->gate.ipv4 = newhop->gate.ipv4; | |
1705 | ||
1706 | if (newhop->ifindex) { | |
1707 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
1708 | resolved_hop->ifindex = newhop->ifindex; | |
1709 | } | |
1710 | break; | |
1711 | case NEXTHOP_TYPE_IPV6: | |
1712 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1713 | resolved_hop->type = newhop->type; | |
1714 | resolved_hop->gate.ipv6 = newhop->gate.ipv6; | |
1715 | ||
1716 | if (newhop->ifindex) { | |
1717 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1718 | resolved_hop->ifindex = newhop->ifindex; | |
1719 | } | |
1720 | break; | |
1721 | case NEXTHOP_TYPE_IFINDEX: | |
1722 | /* If the resolving route is an interface route, | |
1723 | * it means the gateway we are looking up is connected | |
1724 | * to that interface. (The actual network is _not_ onlink). | |
1725 | * Therefore, the resolved route should have the original | |
1726 | * gateway as nexthop as it is directly connected. | |
1727 | * | |
1728 | * On Linux, we have to set the onlink netlink flag because | |
1729 | * otherwise, the kernel won't accept the route. | |
1730 | */ | |
1731 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
1732 | if (afi == AFI_IP) { | |
1733 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
1734 | resolved_hop->gate.ipv4 = nexthop->gate.ipv4; | |
1735 | } else if (afi == AFI_IP6) { | |
1736 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1737 | resolved_hop->gate.ipv6 = nexthop->gate.ipv6; | |
1738 | } | |
1739 | resolved_hop->ifindex = newhop->ifindex; | |
1740 | break; | |
1741 | case NEXTHOP_TYPE_BLACKHOLE: | |
1742 | resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE; | |
2dc359a6 | 1743 | resolved_hop->bh_type = newhop->bh_type; |
ad28e79a SW |
1744 | break; |
1745 | } | |
1746 | ||
1747 | if (newhop->flags & NEXTHOP_FLAG_ONLINK) | |
1748 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
1749 | ||
b43434ad | 1750 | /* Copy labels of the resolved route and the parent resolving to it */ |
31f937fb SM |
1751 | if (policy) { |
1752 | int i = 0; | |
1753 | ||
1754 | /* | |
1755 | * Don't push the first SID if the corresponding action in the | |
1756 | * LFIB is POP. | |
1757 | */ | |
1758 | if (!newhop->nh_label || !newhop->nh_label->num_labels | |
1759 | || newhop->nh_label->label[0] == MPLS_LABEL_IMPLICIT_NULL) | |
1760 | i = 1; | |
1761 | ||
1762 | for (; i < policy->segment_list.label_num; i++) | |
1763 | labels[num_labels++] = policy->segment_list.labels[i]; | |
1764 | label_type = policy->segment_list.type; | |
1765 | } else if (newhop->nh_label) { | |
6bc5d977 MS |
1766 | for (i = 0; i < newhop->nh_label->num_labels; i++) { |
1767 | /* Be a bit picky about overrunning the local array */ | |
1768 | if (num_labels >= MPLS_MAX_LABELS) { | |
1769 | if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB) | |
1770 | zlog_debug("%s: too many labels in newhop %pNHv", | |
1771 | __func__, newhop); | |
1772 | break; | |
1773 | } | |
b43434ad | 1774 | labels[num_labels++] = newhop->nh_label->label[i]; |
6bc5d977 MS |
1775 | } |
1776 | /* Use the "outer" type */ | |
b43434ad SW |
1777 | label_type = newhop->nh_label_type; |
1778 | } | |
1779 | ||
1780 | if (nexthop->nh_label) { | |
6bc5d977 MS |
1781 | for (i = 0; i < nexthop->nh_label->num_labels; i++) { |
1782 | /* Be a bit picky about overrunning the local array */ | |
1783 | if (num_labels >= MPLS_MAX_LABELS) { | |
1784 | if (IS_ZEBRA_DEBUG_NHG || IS_ZEBRA_DEBUG_RIB) | |
1785 | zlog_debug("%s: too many labels in nexthop %pNHv", | |
1786 | __func__, nexthop); | |
1787 | break; | |
1788 | } | |
b43434ad | 1789 | labels[num_labels++] = nexthop->nh_label->label[i]; |
6bc5d977 | 1790 | } |
b43434ad | 1791 | |
6bc5d977 MS |
1792 | /* If the parent has labels, use its type if |
1793 | * we don't already have one. | |
1794 | */ | |
1795 | if (label_type == ZEBRA_LSP_NONE) | |
1796 | label_type = nexthop->nh_label_type; | |
b43434ad SW |
1797 | } |
1798 | ||
1799 | if (num_labels) | |
1800 | nexthop_add_labels(resolved_hop, label_type, num_labels, | |
1801 | labels); | |
ad28e79a | 1802 | |
24b3c59c RS |
1803 | if (nexthop->nh_srv6) { |
1804 | nexthop_add_srv6_seg6local(resolved_hop, | |
1805 | nexthop->nh_srv6->seg6local_action, | |
1806 | &nexthop->nh_srv6->seg6local_ctx); | |
1807 | nexthop_add_srv6_seg6(resolved_hop, | |
1808 | &nexthop->nh_srv6->seg6_segs); | |
1809 | } | |
1810 | ||
ad28e79a | 1811 | resolved_hop->rparent = nexthop; |
50d89650 | 1812 | _nexthop_add(&nexthop->resolved, resolved_hop); |
5530d55d MS |
1813 | |
1814 | return resolved_hop; | |
ad28e79a SW |
1815 | } |
1816 | ||
6913cb1b SW |
1817 | /* Checks if nexthop we are trying to resolve to is valid */ |
1818 | static bool nexthop_valid_resolve(const struct nexthop *nexthop, | |
1819 | const struct nexthop *resolved) | |
1820 | { | |
1821 | /* Can't resolve to a recursive nexthop */ | |
1822 | if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_RECURSIVE)) | |
1823 | return false; | |
1824 | ||
9d43854d MS |
1825 | /* Must be ACTIVE */ |
1826 | if (!CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_ACTIVE)) | |
1827 | return false; | |
1828 | ||
3d30f6de SW |
1829 | /* Must not be duplicate */ |
1830 | if (CHECK_FLAG(resolved->flags, NEXTHOP_FLAG_DUPLICATE)) | |
1831 | return false; | |
1832 | ||
6913cb1b SW |
1833 | switch (nexthop->type) { |
1834 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1835 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1836 | /* If the nexthop we are resolving to does not match the | |
1837 | * ifindex for the nexthop the route wanted, its not valid. | |
1838 | */ | |
1839 | if (nexthop->ifindex != resolved->ifindex) | |
1840 | return false; | |
1841 | break; | |
1842 | case NEXTHOP_TYPE_IPV4: | |
1843 | case NEXTHOP_TYPE_IPV6: | |
1844 | case NEXTHOP_TYPE_IFINDEX: | |
1845 | case NEXTHOP_TYPE_BLACKHOLE: | |
1846 | break; | |
1847 | } | |
1848 | ||
1849 | return true; | |
1850 | } | |
1851 | ||
ad28e79a | 1852 | /* |
5530d55d MS |
1853 | * When resolving a recursive nexthop, capture backup nexthop(s) also |
1854 | * so they can be conveyed through the dataplane to the FIB. We'll look | |
1855 | * at the backups in the resolving nh 'nexthop' and its nhe, and copy them | |
1856 | * into the route's resolved nh 'resolved' and its nhe 'nhe'. | |
1857 | */ | |
1858 | static int resolve_backup_nexthops(const struct nexthop *nexthop, | |
1859 | const struct nhg_hash_entry *nhe, | |
1860 | struct nexthop *resolved, | |
1861 | struct nhg_hash_entry *resolve_nhe, | |
1862 | struct backup_nh_map_s *map) | |
1863 | { | |
1864 | int i, j, idx; | |
1865 | const struct nexthop *bnh; | |
1866 | struct nexthop *nh, *newnh; | |
a082cd9a MS |
1867 | mpls_label_t labels[MPLS_MAX_LABELS]; |
1868 | uint8_t num_labels; | |
5530d55d MS |
1869 | |
1870 | assert(nexthop->backup_num <= NEXTHOP_MAX_BACKUPS); | |
1871 | ||
5530d55d MS |
1872 | /* Locate backups from the original nexthop's backup index and nhe */ |
1873 | for (i = 0; i < nexthop->backup_num; i++) { | |
1874 | idx = nexthop->backup_idx[i]; | |
1875 | ||
1876 | /* Do we already know about this particular backup? */ | |
1877 | for (j = 0; j < map->map_count; j++) { | |
1878 | if (map->map[j].orig_idx == idx) | |
1879 | break; | |
1880 | } | |
1881 | ||
1882 | if (j < map->map_count) { | |
1883 | resolved->backup_idx[resolved->backup_num] = | |
1884 | map->map[j].new_idx; | |
1885 | resolved->backup_num++; | |
1886 | ||
c56c16eb MS |
1887 | SET_FLAG(resolved->flags, NEXTHOP_FLAG_HAS_BACKUP); |
1888 | ||
5530d55d MS |
1889 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1890 | zlog_debug("%s: found map idx orig %d, new %d", | |
1891 | __func__, map->map[j].orig_idx, | |
1892 | map->map[j].new_idx); | |
1893 | ||
1894 | continue; | |
1895 | } | |
1896 | ||
1897 | /* We can't handle any new map entries at this point. */ | |
1898 | if (map->map_count == MULTIPATH_NUM) | |
1899 | break; | |
1900 | ||
1901 | /* Need to create/copy a new backup */ | |
1902 | bnh = nhe->backup_info->nhe->nhg.nexthop; | |
1903 | for (j = 0; j < idx; j++) { | |
1904 | if (bnh == NULL) | |
1905 | break; | |
1906 | bnh = bnh->next; | |
1907 | } | |
1908 | ||
1909 | /* Whoops - bad index in the nexthop? */ | |
1910 | if (bnh == NULL) | |
1911 | continue; | |
1912 | ||
c56c16eb MS |
1913 | if (resolve_nhe->backup_info == NULL) |
1914 | resolve_nhe->backup_info = zebra_nhg_backup_alloc(); | |
1915 | ||
5530d55d MS |
1916 | /* Update backup info in the resolving nexthop and its nhe */ |
1917 | newnh = nexthop_dup_no_recurse(bnh, NULL); | |
1918 | ||
a082cd9a MS |
1919 | /* We may need some special handling for mpls labels: the new |
1920 | * backup needs to carry the recursive nexthop's labels, | |
1921 | * if any: they may be vrf labels e.g. | |
1922 | * The original/inner labels are in the stack of 'resolve_nhe', | |
1923 | * if that is longer than the stack in 'nexthop'. | |
1924 | */ | |
1925 | if (newnh->nh_label && resolved->nh_label && | |
1926 | nexthop->nh_label) { | |
1927 | if (resolved->nh_label->num_labels > | |
1928 | nexthop->nh_label->num_labels) { | |
1929 | /* Prepare new label stack */ | |
1930 | num_labels = 0; | |
1931 | for (j = 0; j < newnh->nh_label->num_labels; | |
1932 | j++) { | |
1933 | labels[j] = newnh->nh_label->label[j]; | |
1934 | num_labels++; | |
1935 | } | |
1936 | ||
1937 | /* Include inner labels */ | |
1938 | for (j = nexthop->nh_label->num_labels; | |
1939 | j < resolved->nh_label->num_labels; | |
1940 | j++) { | |
1941 | labels[num_labels] = | |
1942 | resolved->nh_label->label[j]; | |
1943 | num_labels++; | |
1944 | } | |
1945 | ||
1946 | /* Replace existing label stack in the backup */ | |
1947 | nexthop_del_labels(newnh); | |
1948 | nexthop_add_labels(newnh, bnh->nh_label_type, | |
1949 | num_labels, labels); | |
1950 | } | |
1951 | } | |
1952 | ||
5530d55d MS |
1953 | /* Need to compute the new backup index in the new |
1954 | * backup list, and add to map struct. | |
1955 | */ | |
1956 | j = 0; | |
1957 | nh = resolve_nhe->backup_info->nhe->nhg.nexthop; | |
1958 | if (nh) { | |
1959 | while (nh->next) { | |
1960 | nh = nh->next; | |
1961 | j++; | |
1962 | } | |
1963 | ||
1964 | nh->next = newnh; | |
c56c16eb | 1965 | j++; |
5530d55d MS |
1966 | |
1967 | } else /* First one */ | |
1968 | resolve_nhe->backup_info->nhe->nhg.nexthop = newnh; | |
1969 | ||
1970 | /* Capture index */ | |
1971 | resolved->backup_idx[resolved->backup_num] = j; | |
1972 | resolved->backup_num++; | |
1973 | ||
c56c16eb MS |
1974 | SET_FLAG(resolved->flags, NEXTHOP_FLAG_HAS_BACKUP); |
1975 | ||
5530d55d MS |
1976 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1977 | zlog_debug("%s: added idx orig %d, new %d", | |
1978 | __func__, idx, j); | |
1979 | ||
1980 | /* Update map/cache */ | |
1981 | map->map[map->map_count].orig_idx = idx; | |
1982 | map->map[map->map_count].new_idx = j; | |
1983 | map->map_count++; | |
1984 | } | |
1985 | ||
1986 | return 0; | |
1987 | } | |
1988 | ||
48dc8610 DS |
1989 | /* |
1990 | * So this nexthop resolution has decided that a connected route | |
1991 | * is the correct choice. At this point in time if FRR has multiple | |
1992 | * connected routes that all point to the same prefix one will be | |
1993 | * selected, *but* the particular interface may not be the one | |
1994 | * that the nexthop points at. Let's look at all the available | |
1995 | * connected routes on this node and if any of them auto match | |
1996 | * the routes nexthops ifindex that is good enough for a match | |
1997 | * | |
1998 | * This code is depending on the fact that a nexthop->ifindex is 0 | |
1999 | * if it is not known, if this assumption changes, yummy! | |
2000 | * Additionally a ifindx of 0 means figure it out for us. | |
2001 | */ | |
2002 | static struct route_entry * | |
2003 | zebra_nhg_connected_ifindex(struct route_node *rn, struct route_entry *match, | |
2004 | int32_t curr_ifindex) | |
2005 | { | |
2006 | struct nexthop *newhop = match->nhe->nhg.nexthop; | |
2007 | struct route_entry *re; | |
2008 | ||
2009 | assert(newhop); /* What a kick in the patooey */ | |
2010 | ||
2011 | if (curr_ifindex == 0) | |
2012 | return match; | |
2013 | ||
2014 | if (curr_ifindex == newhop->ifindex) | |
2015 | return match; | |
2016 | ||
2017 | /* | |
2018 | * At this point we know that this route is matching a connected | |
2019 | * but there are possibly a bunch of connected routes that are | |
2020 | * alive that should be considered as well. So let's iterate over | |
2021 | * all the re's and see if they are connected as well and maybe one | |
2022 | * of those ifindexes match as well. | |
2023 | */ | |
2024 | RNODE_FOREACH_RE (rn, re) { | |
2025 | if (re->type != ZEBRA_ROUTE_CONNECT) | |
2026 | continue; | |
2027 | ||
2028 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
2029 | continue; | |
2030 | ||
2031 | /* | |
2032 | * zebra has a connected route that is not removed | |
2033 | * let's test if it is good | |
2034 | */ | |
2035 | newhop = re->nhe->nhg.nexthop; | |
2036 | assert(newhop); | |
2037 | if (curr_ifindex == newhop->ifindex) | |
2038 | return re; | |
2039 | } | |
2040 | ||
2041 | return match; | |
2042 | } | |
2043 | ||
5530d55d MS |
2044 | /* |
2045 | * Given a nexthop we need to properly recursively resolve, | |
2046 | * do a table lookup to find and match if at all possible. | |
2047 | * Set the nexthop->ifindex and resolution info as appropriate. | |
ad28e79a | 2048 | */ |
5530d55d | 2049 | static int nexthop_active(struct nexthop *nexthop, struct nhg_hash_entry *nhe, |
9b4ab909 | 2050 | const struct prefix *top, int type, uint32_t flags, |
3f04f9cf | 2051 | uint32_t *pmtu, vrf_id_t vrf_id) |
ad28e79a SW |
2052 | { |
2053 | struct prefix p; | |
2054 | struct route_table *table; | |
2055 | struct route_node *rn; | |
2056 | struct route_entry *match = NULL; | |
2057 | int resolved; | |
f2595bd5 | 2058 | struct zebra_nhlfe *nhlfe; |
ad28e79a SW |
2059 | struct nexthop *newhop; |
2060 | struct interface *ifp; | |
2061 | rib_dest_t *dest; | |
5a0bdc78 | 2062 | struct zebra_vrf *zvrf; |
31f937fb SM |
2063 | struct in_addr local_ipv4; |
2064 | struct in_addr *ipv4; | |
5530d55d | 2065 | afi_t afi = AFI_IP; |
ad28e79a | 2066 | |
9b4ab909 | 2067 | /* Reset some nexthop attributes that we'll recompute if necessary */ |
ad28e79a | 2068 | if ((nexthop->type == NEXTHOP_TYPE_IPV4) |
9b4ab909 | 2069 | || (nexthop->type == NEXTHOP_TYPE_IPV6)) |
ad28e79a SW |
2070 | nexthop->ifindex = 0; |
2071 | ||
2072 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
2073 | nexthops_free(nexthop->resolved); | |
2074 | nexthop->resolved = NULL; | |
377e29f7 | 2075 | |
ad28e79a | 2076 | /* |
5530d55d | 2077 | * Set afi based on nexthop type. |
9b4ab909 MS |
2078 | * Some nexthop types get special handling, possibly skipping |
2079 | * the normal processing. | |
ad28e79a | 2080 | */ |
9b4ab909 MS |
2081 | switch (nexthop->type) { |
2082 | case NEXTHOP_TYPE_IFINDEX: | |
5530d55d | 2083 | |
9b4ab909 MS |
2084 | ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id); |
2085 | /* | |
2086 | * If the interface exists and its operative or its a kernel | |
2087 | * route and interface is up, its active. We trust kernel routes | |
2088 | * to be good. | |
2089 | */ | |
fc3de981 | 2090 | if (ifp && (if_is_operative(ifp))) |
9b4ab909 MS |
2091 | return 1; |
2092 | else | |
2093 | return 0; | |
2094 | break; | |
2095 | ||
2096 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
5530d55d MS |
2097 | afi = AFI_IP6; |
2098 | ||
9b4ab909 MS |
2099 | if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) { |
2100 | ifp = if_lookup_by_index(nexthop->ifindex, | |
2101 | nexthop->vrf_id); | |
2102 | if (ifp && if_is_operative(ifp)) | |
2103 | return 1; | |
2104 | else | |
2105 | return 0; | |
2106 | } | |
2107 | break; | |
2108 | ||
9b4ab909 MS |
2109 | case NEXTHOP_TYPE_IPV4: |
2110 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
5530d55d MS |
2111 | afi = AFI_IP; |
2112 | break; | |
9b4ab909 | 2113 | case NEXTHOP_TYPE_IPV6: |
5530d55d | 2114 | afi = AFI_IP6; |
9b4ab909 | 2115 | break; |
5530d55d MS |
2116 | |
2117 | case NEXTHOP_TYPE_BLACKHOLE: | |
2118 | return 1; | |
9b4ab909 MS |
2119 | } |
2120 | ||
ad28e79a | 2121 | /* |
12b4d77b | 2122 | * If the nexthop has been marked as 'onlink' we just need to make |
2123 | * sure the nexthop's interface is known and is operational. | |
ad28e79a SW |
2124 | */ |
2125 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) { | |
2126 | ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id); | |
2127 | if (!ifp) { | |
a24d04f4 | 2128 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
12b4d77b | 2129 | zlog_debug("nexthop %pNHv marked onlink but nhif %u doesn't exist", |
2130 | nexthop, nexthop->ifindex); | |
ad28e79a SW |
2131 | return 0; |
2132 | } | |
12b4d77b | 2133 | if (!if_is_operative(ifp)) { |
a24d04f4 | 2134 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
12b4d77b | 2135 | zlog_debug("nexthop %pNHv marked onlink but nhif %s is not operational", |
2136 | nexthop, ifp->name); | |
2d3c57e6 | 2137 | return 0; |
ad28e79a | 2138 | } |
12b4d77b | 2139 | return 1; |
ad28e79a SW |
2140 | } |
2141 | ||
3f04f9cf | 2142 | if (top && |
2143 | ((top->family == AF_INET && top->prefixlen == IPV4_MAX_BITLEN && | |
2144 | nexthop->gate.ipv4.s_addr == top->u.prefix4.s_addr) || | |
2145 | (top->family == AF_INET6 && top->prefixlen == IPV6_MAX_BITLEN && | |
2146 | memcmp(&nexthop->gate.ipv6, &top->u.prefix6, IPV6_MAX_BYTELEN) == | |
2147 | 0)) && | |
2148 | nexthop->vrf_id == vrf_id) { | |
4dcc2276 DS |
2149 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
2150 | zlog_debug( | |
d6951e5e | 2151 | " :%s: Attempting to install a max prefixlength route through itself", |
5e81f5dd | 2152 | __func__); |
4dcc2276 DS |
2153 | return 0; |
2154 | } | |
2155 | ||
92d6f769 K |
2156 | /* Validation for ipv4 mapped ipv6 nexthop. */ |
2157 | if (IS_MAPPED_IPV6(&nexthop->gate.ipv6)) { | |
2158 | afi = AFI_IP; | |
31f937fb SM |
2159 | ipv4 = &local_ipv4; |
2160 | ipv4_mapped_ipv6_to_ipv4(&nexthop->gate.ipv6, ipv4); | |
2161 | } else { | |
2162 | ipv4 = &nexthop->gate.ipv4; | |
2163 | } | |
2164 | ||
9b4ab909 MS |
2165 | /* Processing for nexthops with SR 'color' attribute, using |
2166 | * the corresponding SR policy object. | |
2167 | */ | |
31f937fb SM |
2168 | if (nexthop->srte_color) { |
2169 | struct ipaddr endpoint = {0}; | |
2170 | struct zebra_sr_policy *policy; | |
2171 | ||
2172 | switch (afi) { | |
2173 | case AFI_IP: | |
2174 | endpoint.ipa_type = IPADDR_V4; | |
2175 | endpoint.ipaddr_v4 = *ipv4; | |
2176 | break; | |
2177 | case AFI_IP6: | |
2178 | endpoint.ipa_type = IPADDR_V6; | |
2179 | endpoint.ipaddr_v6 = nexthop->gate.ipv6; | |
2180 | break; | |
2181 | default: | |
2182 | flog_err(EC_LIB_DEVELOPMENT, | |
2183 | "%s: unknown address-family: %u", __func__, | |
2184 | afi); | |
2185 | exit(1); | |
2186 | } | |
2187 | ||
2188 | policy = zebra_sr_policy_find(nexthop->srte_color, &endpoint); | |
2189 | if (policy && policy->status == ZEBRA_SR_POLICY_UP) { | |
2190 | resolved = 0; | |
2191 | frr_each_safe (nhlfe_list, &policy->lsp->nhlfe_list, | |
2192 | nhlfe) { | |
2193 | if (!CHECK_FLAG(nhlfe->flags, | |
2194 | NHLFE_FLAG_SELECTED) | |
2195 | || CHECK_FLAG(nhlfe->flags, | |
2196 | NHLFE_FLAG_DELETED)) | |
2197 | continue; | |
2198 | SET_FLAG(nexthop->flags, | |
2199 | NEXTHOP_FLAG_RECURSIVE); | |
2200 | nexthop_set_resolved(afi, nhlfe->nexthop, | |
2201 | nexthop, policy); | |
2202 | resolved = 1; | |
2203 | } | |
2204 | if (resolved) | |
2205 | return 1; | |
2206 | } | |
92d6f769 K |
2207 | } |
2208 | ||
ad28e79a SW |
2209 | /* Make lookup prefix. */ |
2210 | memset(&p, 0, sizeof(struct prefix)); | |
2211 | switch (afi) { | |
2212 | case AFI_IP: | |
2213 | p.family = AF_INET; | |
936fbaef | 2214 | p.prefixlen = IPV4_MAX_BITLEN; |
31f937fb | 2215 | p.u.prefix4 = *ipv4; |
ad28e79a SW |
2216 | break; |
2217 | case AFI_IP6: | |
2218 | p.family = AF_INET6; | |
f4d81e55 | 2219 | p.prefixlen = IPV6_MAX_BITLEN; |
ad28e79a SW |
2220 | p.u.prefix6 = nexthop->gate.ipv6; |
2221 | break; | |
2222 | default: | |
2223 | assert(afi != AFI_IP && afi != AFI_IP6); | |
2224 | break; | |
2225 | } | |
2226 | /* Lookup table. */ | |
2227 | table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id); | |
5a0bdc78 PG |
2228 | /* get zvrf */ |
2229 | zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id); | |
2230 | if (!table || !zvrf) { | |
ad28e79a | 2231 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
d6951e5e | 2232 | zlog_debug(" %s: Table not found", __func__); |
ad28e79a SW |
2233 | return 0; |
2234 | } | |
2235 | ||
2236 | rn = route_node_match(table, (struct prefix *)&p); | |
2237 | while (rn) { | |
2238 | route_unlock_node(rn); | |
2239 | ||
2240 | /* Lookup should halt if we've matched against ourselves ('top', | |
2241 | * if specified) - i.e., we cannot have a nexthop NH1 is | |
2242 | * resolved by a route NH1. The exception is if the route is a | |
2243 | * host route. | |
2244 | */ | |
9b4ab909 | 2245 | if (prefix_same(&rn->p, top)) |
12256b84 DA |
2246 | if (((afi == AFI_IP) |
2247 | && (rn->p.prefixlen != IPV4_MAX_BITLEN)) | |
13ccce6e DA |
2248 | || ((afi == AFI_IP6) |
2249 | && (rn->p.prefixlen != IPV6_MAX_BITLEN))) { | |
ad28e79a SW |
2250 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
2251 | zlog_debug( | |
d6951e5e | 2252 | " %s: Matched against ourself and prefix length is not max bit length", |
5e81f5dd | 2253 | __func__); |
ad28e79a SW |
2254 | return 0; |
2255 | } | |
2256 | ||
2257 | /* Pick up selected route. */ | |
2258 | /* However, do not resolve over default route unless explicitly | |
2d3c57e6 SW |
2259 | * allowed. |
2260 | */ | |
ad28e79a | 2261 | if (is_default_prefix(&rn->p) |
5a0bdc78 | 2262 | && !rnh_resolve_via_default(zvrf, p.family)) { |
ad28e79a SW |
2263 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
2264 | zlog_debug( | |
d6951e5e | 2265 | " :%s: Resolved against default route", |
5e81f5dd | 2266 | __func__); |
ad28e79a SW |
2267 | return 0; |
2268 | } | |
2269 | ||
2270 | dest = rib_dest_from_rnode(rn); | |
2271 | if (dest && dest->selected_fib | |
2272 | && !CHECK_FLAG(dest->selected_fib->status, | |
2273 | ROUTE_ENTRY_REMOVED) | |
2274 | && dest->selected_fib->type != ZEBRA_ROUTE_TABLE) | |
2275 | match = dest->selected_fib; | |
2276 | ||
2277 | /* If there is no selected route or matched route is EGP, go up | |
2d3c57e6 SW |
2278 | * tree. |
2279 | */ | |
ad28e79a SW |
2280 | if (!match) { |
2281 | do { | |
2282 | rn = rn->parent; | |
2283 | } while (rn && rn->info == NULL); | |
2284 | if (rn) | |
2285 | route_lock_node(rn); | |
2286 | ||
2287 | continue; | |
2288 | } | |
2289 | ||
c9e4abf8 DS |
2290 | if ((match->type == ZEBRA_ROUTE_CONNECT) || |
2291 | (RIB_SYSTEM_ROUTE(match) && RSYSTEM_ROUTE(type))) { | |
48dc8610 DS |
2292 | match = zebra_nhg_connected_ifindex(rn, match, |
2293 | nexthop->ifindex); | |
2294 | ||
c415d895 | 2295 | newhop = match->nhe->nhg.nexthop; |
48dc8610 DS |
2296 | if (nexthop->type == NEXTHOP_TYPE_IPV4 || |
2297 | nexthop->type == NEXTHOP_TYPE_IPV6) | |
2298 | nexthop->ifindex = newhop->ifindex; | |
2299 | else if (nexthop->ifindex != newhop->ifindex) { | |
2300 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2301 | zlog_debug( | |
2302 | "%s: %pNHv given ifindex does not match nexthops ifindex found: %pNHv", | |
2303 | __func__, nexthop, newhop); | |
2304 | /* | |
2305 | * NEXTHOP_TYPE_*_IFINDEX but ifindex | |
2306 | * doesn't match what we found. | |
2307 | */ | |
2308 | return 0; | |
ad28e79a | 2309 | } |
377e29f7 MS |
2310 | |
2311 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be DS |
2312 | zlog_debug( |
2313 | "%s: CONNECT match %p (%pNG), newhop %pNHv", | |
2314 | __func__, match, match->nhe, newhop); | |
377e29f7 | 2315 | |
ad28e79a | 2316 | return 1; |
9b4ab909 | 2317 | } else if (CHECK_FLAG(flags, ZEBRA_FLAG_ALLOW_RECURSION)) { |
f2646720 | 2318 | struct nexthop_group *nhg; |
5530d55d MS |
2319 | struct nexthop *resolver; |
2320 | struct backup_nh_map_s map = {}; | |
f2646720 | 2321 | |
ad28e79a | 2322 | resolved = 0; |
f2646720 | 2323 | |
92ad0c55 MS |
2324 | /* Only useful if installed */ |
2325 | if (!CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED)) { | |
a24d04f4 | 2326 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
68d188be DS |
2327 | zlog_debug( |
2328 | "%s: match %p (%pNG) not installed", | |
2329 | __func__, match, match->nhe); | |
92ad0c55 MS |
2330 | |
2331 | goto done_with_match; | |
2332 | } | |
2333 | ||
3c0e1622 MS |
2334 | /* Examine installed nexthops; note that there |
2335 | * may not be any installed primary nexthops if | |
2336 | * only backups are installed. | |
2337 | */ | |
2338 | nhg = rib_get_fib_nhg(match); | |
f2646720 | 2339 | for (ALL_NEXTHOPS_PTR(nhg, newhop)) { |
6913cb1b | 2340 | if (!nexthop_valid_resolve(nexthop, newhop)) |
ad28e79a SW |
2341 | continue; |
2342 | ||
377e29f7 | 2343 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
68d188be DS |
2344 | zlog_debug( |
2345 | "%s: RECURSIVE match %p (%pNG), newhop %pNHv", | |
2346 | __func__, match, match->nhe, | |
2347 | newhop); | |
377e29f7 | 2348 | |
ad28e79a SW |
2349 | SET_FLAG(nexthop->flags, |
2350 | NEXTHOP_FLAG_RECURSIVE); | |
5530d55d MS |
2351 | resolver = nexthop_set_resolved(afi, newhop, |
2352 | nexthop, NULL); | |
ad28e79a | 2353 | resolved = 1; |
5530d55d MS |
2354 | |
2355 | /* If there are backup nexthops, capture | |
2356 | * that info with the resolving nexthop. | |
2357 | */ | |
2358 | if (resolver && newhop->backup_num > 0) { | |
2359 | resolve_backup_nexthops(newhop, | |
2360 | match->nhe, | |
2361 | resolver, nhe, | |
2362 | &map); | |
2363 | } | |
ad28e79a | 2364 | } |
f924db49 | 2365 | |
9959f1da MS |
2366 | /* Examine installed backup nexthops, if any. There |
2367 | * are only installed backups *if* there is a | |
aa458838 MS |
2368 | * dedicated fib list. The UI can also control use |
2369 | * of backups for resolution. | |
9959f1da MS |
2370 | */ |
2371 | nhg = rib_get_fib_backup_nhg(match); | |
aa458838 MS |
2372 | if (!use_recursive_backups || |
2373 | nhg == NULL || nhg->nexthop == NULL) | |
f2646720 MS |
2374 | goto done_with_match; |
2375 | ||
2376 | for (ALL_NEXTHOPS_PTR(nhg, newhop)) { | |
f2646720 MS |
2377 | if (!nexthop_valid_resolve(nexthop, newhop)) |
2378 | continue; | |
2379 | ||
2380 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be DS |
2381 | zlog_debug( |
2382 | "%s: RECURSIVE match backup %p (%pNG), newhop %pNHv", | |
2383 | __func__, match, match->nhe, | |
2384 | newhop); | |
f2646720 MS |
2385 | |
2386 | SET_FLAG(nexthop->flags, | |
2387 | NEXTHOP_FLAG_RECURSIVE); | |
31f937fb SM |
2388 | nexthop_set_resolved(afi, newhop, nexthop, |
2389 | NULL); | |
f2646720 MS |
2390 | resolved = 1; |
2391 | } | |
9b4ab909 | 2392 | |
f2646720 | 2393 | done_with_match: |
9b4ab909 MS |
2394 | /* Capture resolving mtu */ |
2395 | if (resolved) { | |
2396 | if (pmtu) | |
2397 | *pmtu = match->mtu; | |
2398 | ||
2399 | } else if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
d6951e5e DL |
2400 | zlog_debug( |
2401 | " %s: Recursion failed to find", | |
2402 | __func__); | |
f924db49 | 2403 | |
ad28e79a | 2404 | return resolved; |
ad28e79a SW |
2405 | } else { |
2406 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
2407 | zlog_debug( | |
d6951e5e | 2408 | " %s: Route Type %s has not turned on recursion", |
9b4ab909 MS |
2409 | __func__, zebra_route_string(type)); |
2410 | if (type == ZEBRA_ROUTE_BGP | |
2411 | && !CHECK_FLAG(flags, ZEBRA_FLAG_IBGP)) | |
ad28e79a | 2412 | zlog_debug( |
d6951e5e | 2413 | " EBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\""); |
ad28e79a SW |
2414 | } |
2415 | return 0; | |
2416 | } | |
2417 | } | |
2418 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
d6951e5e DL |
2419 | zlog_debug(" %s: Nexthop did not lookup in table", |
2420 | __func__); | |
ad28e79a SW |
2421 | return 0; |
2422 | } | |
2423 | ||
2424 | /* This function verifies reachability of one given nexthop, which can be | |
2425 | * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored | |
2426 | * in nexthop->flags field. The nexthop->ifindex will be updated | |
5530d55d MS |
2427 | * appropriately as well. |
2428 | * | |
2429 | * An existing route map can turn an otherwise active nexthop into inactive, | |
2430 | * but not vice versa. | |
ad28e79a SW |
2431 | * |
2432 | * The return value is the final value of 'ACTIVE' flag. | |
2433 | */ | |
2434 | static unsigned nexthop_active_check(struct route_node *rn, | |
2435 | struct route_entry *re, | |
5530d55d MS |
2436 | struct nexthop *nexthop, |
2437 | struct nhg_hash_entry *nhe) | |
ad28e79a | 2438 | { |
b68885f9 | 2439 | route_map_result_t ret = RMAP_PERMITMATCH; |
f5b7e50f | 2440 | afi_t family; |
ad28e79a SW |
2441 | const struct prefix *p, *src_p; |
2442 | struct zebra_vrf *zvrf; | |
9b4ab909 | 2443 | uint32_t mtu = 0; |
3f04f9cf | 2444 | vrf_id_t vrf_id; |
ad28e79a SW |
2445 | |
2446 | srcdest_rnode_prefixes(rn, &p, &src_p); | |
2447 | ||
2448 | if (rn->p.family == AF_INET) | |
2449 | family = AFI_IP; | |
2450 | else if (rn->p.family == AF_INET6) | |
2451 | family = AFI_IP6; | |
2452 | else | |
81ef8a69 | 2453 | family = AF_UNSPEC; |
ee94437e | 2454 | |
9b4ab909 MS |
2455 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
2456 | zlog_debug("%s: re %p, nexthop %pNHv", __func__, re, nexthop); | |
2457 | ||
2458 | /* | |
fc3de981 | 2459 | * If this is a kernel route, then if the interface is *up* then |
9b4ab909 | 2460 | * by golly gee whiz it's a good route. |
9b4ab909 | 2461 | */ |
fc3de981 DS |
2462 | if (re->type == ZEBRA_ROUTE_KERNEL || re->type == ZEBRA_ROUTE_SYSTEM) { |
2463 | struct interface *ifp; | |
2464 | ||
2465 | ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id); | |
9b4ab909 | 2466 | |
fc3de981 DS |
2467 | if (ifp && (if_is_operative(ifp) || if_is_up(ifp))) { |
2468 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2469 | goto skip_check; | |
2470 | } | |
2471 | } | |
3f04f9cf | 2472 | |
2473 | vrf_id = zvrf_id(rib_dest_vrf(rib_dest_from_rnode(rn))); | |
ad28e79a SW |
2474 | switch (nexthop->type) { |
2475 | case NEXTHOP_TYPE_IFINDEX: | |
3f04f9cf | 2476 | if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags, |
2477 | &mtu, vrf_id)) | |
ad28e79a SW |
2478 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2479 | else | |
2480 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2481 | break; | |
2482 | case NEXTHOP_TYPE_IPV4: | |
2483 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
2484 | family = AFI_IP; | |
3f04f9cf | 2485 | if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags, |
2486 | &mtu, vrf_id)) | |
ad28e79a SW |
2487 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2488 | else | |
2489 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2490 | break; | |
2491 | case NEXTHOP_TYPE_IPV6: | |
2492 | family = AFI_IP6; | |
3f04f9cf | 2493 | if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags, |
2494 | &mtu, vrf_id)) | |
ad28e79a SW |
2495 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2496 | else | |
2497 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2498 | break; | |
2499 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
2500 | /* RFC 5549, v4 prefix with v6 NH */ | |
2501 | if (rn->p.family != AF_INET) | |
2502 | family = AFI_IP6; | |
9b4ab909 | 2503 | |
3f04f9cf | 2504 | if (nexthop_active(nexthop, nhe, &rn->p, re->type, re->flags, |
2505 | &mtu, vrf_id)) | |
9b4ab909 MS |
2506 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2507 | else | |
2508 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
ad28e79a SW |
2509 | break; |
2510 | case NEXTHOP_TYPE_BLACKHOLE: | |
2511 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2512 | break; | |
2513 | default: | |
2514 | break; | |
2515 | } | |
377e29f7 | 2516 | |
9b4ab909 MS |
2517 | skip_check: |
2518 | ||
ad28e79a SW |
2519 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) { |
2520 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
377e29f7 MS |
2521 | zlog_debug(" %s: Unable to find active nexthop", |
2522 | __func__); | |
ad28e79a SW |
2523 | return 0; |
2524 | } | |
2525 | ||
9b4ab909 MS |
2526 | /* Capture recursive nexthop mtu. |
2527 | * TODO -- the code used to just reset the re's value to zero | |
2528 | * for each nexthop, and then jam any resolving route's mtu value in, | |
2529 | * whether or not that was zero, or lt/gt any existing value? The | |
2530 | * way this is used appears to be as a floor value, so let's try | |
2531 | * using it that way here. | |
2532 | */ | |
2533 | if (mtu > 0) { | |
2534 | if (re->nexthop_mtu == 0 || re->nexthop_mtu > mtu) | |
2535 | re->nexthop_mtu = mtu; | |
2536 | } | |
2537 | ||
ad28e79a SW |
2538 | /* XXX: What exactly do those checks do? Do we support |
2539 | * e.g. IPv4 routes with IPv6 nexthops or vice versa? | |
2540 | */ | |
2541 | if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET) | |
2542 | || (family == AFI_IP6 && p->family != AF_INET6)) | |
2543 | return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2544 | ||
2545 | /* The original code didn't determine the family correctly | |
2546 | * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi | |
2547 | * from the rib_table_info in those cases. | |
2548 | * Possibly it may be better to use only the rib_table_info | |
2549 | * in every case. | |
2550 | */ | |
9b4ab909 | 2551 | if (family == 0) { |
630d5962 | 2552 | struct rib_table_info *info; |
ad28e79a SW |
2553 | |
2554 | info = srcdest_rnode_table_info(rn); | |
2555 | family = info->afi; | |
2556 | } | |
2557 | ||
2558 | memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr)); | |
2559 | ||
45dafca8 | 2560 | zvrf = zebra_vrf_lookup_by_id(re->vrf_id); |
ad28e79a SW |
2561 | if (!zvrf) { |
2562 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
d6951e5e | 2563 | zlog_debug(" %s: zvrf is NULL", __func__); |
ad28e79a SW |
2564 | return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
2565 | } | |
2566 | ||
2567 | /* It'll get set if required inside */ | |
2568 | ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop, | |
2569 | zvrf, re->tag); | |
2570 | if (ret == RMAP_DENYMATCH) { | |
2571 | if (IS_ZEBRA_DEBUG_RIB) { | |
ad28e79a | 2572 | zlog_debug( |
7e010c4b DS |
2573 | "%u:%pRN: Filtering out with NH out %s due to route map", |
2574 | re->vrf_id, rn, | |
ad28e79a SW |
2575 | ifindex2ifname(nexthop->ifindex, |
2576 | nexthop->vrf_id)); | |
2577 | } | |
2578 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2579 | } | |
2580 | return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2581 | } | |
2582 | ||
086e4e02 SW |
2583 | /* Helper function called after resolution to walk nhg rb trees |
2584 | * and toggle the NEXTHOP_GROUP_VALID flag if the nexthop | |
2585 | * is active on singleton NHEs. | |
2586 | */ | |
2587 | static bool zebra_nhg_set_valid_if_active(struct nhg_hash_entry *nhe) | |
2588 | { | |
2589 | struct nhg_connected *rb_node_dep = NULL; | |
2590 | bool valid = false; | |
2591 | ||
2592 | if (!zebra_nhg_depends_is_empty(nhe)) { | |
2593 | /* Is at least one depend valid? */ | |
2594 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { | |
2595 | if (zebra_nhg_set_valid_if_active(rb_node_dep->nhe)) | |
2596 | valid = true; | |
2597 | } | |
2598 | ||
2599 | goto done; | |
2600 | } | |
2601 | ||
2602 | /* should be fully resolved singleton at this point */ | |
2603 | if (CHECK_FLAG(nhe->nhg.nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
2604 | valid = true; | |
2605 | ||
2606 | done: | |
2607 | if (valid) | |
2608 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
2609 | ||
2610 | return valid; | |
2611 | } | |
2612 | ||
ad28e79a | 2613 | /* |
5530d55d | 2614 | * Process a list of nexthops, given an nhe, determining |
377e29f7 | 2615 | * whether each one is ACTIVE/installable at this time. |
ad28e79a | 2616 | */ |
377e29f7 MS |
2617 | static uint32_t nexthop_list_active_update(struct route_node *rn, |
2618 | struct route_entry *re, | |
5530d55d MS |
2619 | struct nhg_hash_entry *nhe, |
2620 | bool is_backup) | |
ad28e79a | 2621 | { |
ad28e79a SW |
2622 | union g_addr prev_src; |
2623 | unsigned int prev_active, new_active; | |
2624 | ifindex_t prev_index; | |
377e29f7 | 2625 | uint32_t counter = 0; |
f2646720 | 2626 | struct nexthop *nexthop; |
5530d55d | 2627 | struct nexthop_group *nhg = &nhe->nhg; |
f2646720 MS |
2628 | |
2629 | nexthop = nhg->nexthop; | |
e22e8001 | 2630 | |
9b4ab909 MS |
2631 | /* Init recursive nh mtu */ |
2632 | re->nexthop_mtu = 0; | |
2633 | ||
377e29f7 MS |
2634 | /* Process nexthops one-by-one */ |
2635 | for ( ; nexthop; nexthop = nexthop->next) { | |
ad28e79a | 2636 | |
ad28e79a | 2637 | /* No protocol daemon provides src and so we're skipping |
377e29f7 MS |
2638 | * tracking it |
2639 | */ | |
ad28e79a SW |
2640 | prev_src = nexthop->rmap_src; |
2641 | prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
2642 | prev_index = nexthop->ifindex; | |
5530d55d MS |
2643 | |
2644 | /* Include the containing nhe for primary nexthops: if there's | |
2645 | * recursive resolution, we capture the backup info also. | |
2646 | */ | |
2647 | new_active = | |
2648 | nexthop_active_check(rn, re, nexthop, | |
2649 | (is_backup ? NULL : nhe)); | |
2650 | ||
ad28e79a SW |
2651 | /* |
2652 | * We need to respect the multipath_num here | |
2653 | * as that what we should be able to install from | |
377e29f7 | 2654 | * a multipath perspective should not be a data plane |
ad28e79a SW |
2655 | * decision point. |
2656 | */ | |
377e29f7 | 2657 | if (new_active && counter >= zrouter.multipath_num) { |
4c55b5ff SW |
2658 | struct nexthop *nh; |
2659 | ||
2660 | /* Set it and its resolved nexthop as inactive. */ | |
2661 | for (nh = nexthop; nh; nh = nh->resolved) | |
2662 | UNSET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE); | |
2663 | ||
ad28e79a SW |
2664 | new_active = 0; |
2665 | } | |
9a0d4dd3 | 2666 | |
df31a989 | 2667 | if (new_active) |
377e29f7 | 2668 | counter++; |
9a0d4dd3 | 2669 | |
5530d55d | 2670 | /* Check for changes to the nexthop - set ROUTE_ENTRY_CHANGED */ |
ad28e79a SW |
2671 | if (prev_active != new_active || prev_index != nexthop->ifindex |
2672 | || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX | |
2673 | && nexthop->type < NEXTHOP_TYPE_IPV6) | |
2674 | && prev_src.ipv4.s_addr | |
2675 | != nexthop->rmap_src.ipv4.s_addr) | |
2676 | || ((nexthop->type >= NEXTHOP_TYPE_IPV6 | |
2677 | && nexthop->type < NEXTHOP_TYPE_BLACKHOLE) | |
2678 | && !(IPV6_ADDR_SAME(&prev_src.ipv6, | |
2679 | &nexthop->rmap_src.ipv6))) | |
8a507796 | 2680 | || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)) |
ad28e79a | 2681 | SET_FLAG(re->status, ROUTE_ENTRY_CHANGED); |
ad28e79a SW |
2682 | } |
2683 | ||
377e29f7 MS |
2684 | return counter; |
2685 | } | |
2686 | ||
2c41ef8c SW |
2687 | |
2688 | static uint32_t proto_nhg_nexthop_active_update(struct nexthop_group *nhg) | |
2689 | { | |
2690 | struct nexthop *nh; | |
2691 | uint32_t curr_active = 0; | |
2692 | ||
2693 | /* Assume all active for now */ | |
2694 | ||
2695 | for (nh = nhg->nexthop; nh; nh = nh->next) { | |
2696 | SET_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE); | |
2697 | curr_active++; | |
2698 | } | |
2699 | ||
2700 | return curr_active; | |
2701 | } | |
2702 | ||
377e29f7 MS |
2703 | /* |
2704 | * Iterate over all nexthops of the given RIB entry and refresh their | |
2705 | * ACTIVE flag. If any nexthop is found to toggle the ACTIVE flag, | |
2706 | * the whole re structure is flagged with ROUTE_ENTRY_CHANGED. | |
2707 | * | |
2708 | * Return value is the new number of active nexthops. | |
2709 | */ | |
2710 | int nexthop_active_update(struct route_node *rn, struct route_entry *re) | |
2711 | { | |
2712 | struct nhg_hash_entry *curr_nhe; | |
2713 | uint32_t curr_active = 0, backup_active = 0; | |
2714 | ||
65f137fe | 2715 | if (PROTO_OWNED(re->nhe)) |
2c41ef8c SW |
2716 | return proto_nhg_nexthop_active_update(&re->nhe->nhg); |
2717 | ||
377e29f7 MS |
2718 | afi_t rt_afi = family2afi(rn->p.family); |
2719 | ||
2720 | UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
2721 | ||
2722 | /* Make a local copy of the existing nhe, so we don't work on/modify | |
2723 | * the shared nhe. | |
2724 | */ | |
f727646a | 2725 | curr_nhe = zebra_nhe_copy(re->nhe, re->nhe->id); |
377e29f7 MS |
2726 | |
2727 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be DS |
2728 | zlog_debug("%s: re %p nhe %p (%pNG), curr_nhe %p", __func__, re, |
2729 | re->nhe, re->nhe, curr_nhe); | |
377e29f7 MS |
2730 | |
2731 | /* Clear the existing id, if any: this will avoid any confusion | |
2732 | * if the id exists, and will also force the creation | |
2733 | * of a new nhe reflecting the changes we may make in this local copy. | |
2734 | */ | |
2735 | curr_nhe->id = 0; | |
2736 | ||
2737 | /* Process nexthops */ | |
5530d55d | 2738 | curr_active = nexthop_list_active_update(rn, re, curr_nhe, false); |
377e29f7 MS |
2739 | |
2740 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2741 | zlog_debug("%s: re %p curr_active %u", __func__, re, | |
2742 | curr_active); | |
2743 | ||
2744 | /* If there are no backup nexthops, we are done */ | |
2745 | if (zebra_nhg_get_backup_nhg(curr_nhe) == NULL) | |
2746 | goto backups_done; | |
2747 | ||
2748 | backup_active = nexthop_list_active_update( | |
5530d55d | 2749 | rn, re, curr_nhe->backup_info->nhe, true /*is_backup*/); |
377e29f7 MS |
2750 | |
2751 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2752 | zlog_debug("%s: re %p backup_active %u", __func__, re, | |
2753 | backup_active); | |
2754 | ||
2755 | backups_done: | |
2756 | ||
2757 | /* | |
2758 | * Ref or create an nhe that matches the current state of the | |
2759 | * nexthop(s). | |
2760 | */ | |
8a507796 | 2761 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) { |
98cda54a | 2762 | struct nhg_hash_entry *new_nhe = NULL; |
98cda54a | 2763 | |
377e29f7 MS |
2764 | new_nhe = zebra_nhg_rib_find_nhe(curr_nhe, rt_afi); |
2765 | ||
2766 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be DS |
2767 | zlog_debug( |
2768 | "%s: re %p CHANGED: nhe %p (%pNG) => new_nhe %p (%pNG)", | |
2769 | __func__, re, re->nhe, re->nhe, new_nhe, | |
2770 | new_nhe); | |
98cda54a | 2771 | |
5463ce26 | 2772 | route_entry_update_nhe(re, new_nhe); |
e22e8001 SW |
2773 | } |
2774 | ||
377e29f7 | 2775 | |
086e4e02 SW |
2776 | /* Walk the NHE depends tree and toggle NEXTHOP_GROUP_VALID |
2777 | * flag where appropriate. | |
2778 | */ | |
715e5c70 | 2779 | if (curr_active) |
086e4e02 | 2780 | zebra_nhg_set_valid_if_active(re->nhe); |
98cda54a SW |
2781 | |
2782 | /* | |
377e29f7 MS |
2783 | * Do not need the old / copied nhe anymore since it |
2784 | * was either copied over into a new nhe or not | |
98cda54a SW |
2785 | * used at all. |
2786 | */ | |
377e29f7 | 2787 | zebra_nhg_free(curr_nhe); |
9a0d4dd3 | 2788 | return curr_active; |
ad28e79a | 2789 | } |
5be96a2d | 2790 | |
497ff579 SW |
2791 | /* Recursively construct a grp array of fully resolved IDs. |
2792 | * | |
2793 | * This function allows us to account for groups within groups, | |
2794 | * by converting them into a flat array of IDs. | |
2795 | * | |
2796 | * nh_grp is modified at every level of recursion to append | |
2797 | * to it the next unique, fully resolved ID from the entire tree. | |
2798 | * | |
2799 | * | |
2800 | * Note: | |
2801 | * I'm pretty sure we only allow ONE level of group within group currently. | |
2802 | * But making this recursive just in case that ever changes. | |
2803 | */ | |
2804 | static uint8_t zebra_nhg_nhe2grp_internal(struct nh_grp *grp, | |
2805 | uint8_t curr_index, | |
2806 | struct nhg_hash_entry *nhe, | |
2807 | int max_num) | |
98cda54a SW |
2808 | { |
2809 | struct nhg_connected *rb_node_dep = NULL; | |
2810 | struct nhg_hash_entry *depend = NULL; | |
497ff579 | 2811 | uint8_t i = curr_index; |
98cda54a | 2812 | |
fec211ad | 2813 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
8dbc800f SW |
2814 | bool duplicate = false; |
2815 | ||
497ff579 SW |
2816 | if (i >= max_num) |
2817 | goto done; | |
2818 | ||
98cda54a SW |
2819 | depend = rb_node_dep->nhe; |
2820 | ||
2821 | /* | |
2822 | * If its recursive, use its resolved nhe in the group | |
2823 | */ | |
2824 | if (CHECK_FLAG(depend->flags, NEXTHOP_GROUP_RECURSIVE)) { | |
2825 | depend = zebra_nhg_resolve(depend); | |
2826 | if (!depend) { | |
2827 | flog_err( | |
2828 | EC_ZEBRA_NHG_FIB_UPDATE, | |
68d188be DS |
2829 | "Failed to recursively resolve Nexthop Hash Entry in the group id=%pNG", |
2830 | nhe); | |
98cda54a SW |
2831 | continue; |
2832 | } | |
2833 | } | |
2834 | ||
497ff579 SW |
2835 | if (!zebra_nhg_depends_is_empty(depend)) { |
2836 | /* This is a group within a group */ | |
2837 | i = zebra_nhg_nhe2grp_internal(grp, i, depend, max_num); | |
2838 | } else { | |
086e4e02 SW |
2839 | if (!CHECK_FLAG(depend->flags, NEXTHOP_GROUP_VALID)) { |
2840 | if (IS_ZEBRA_DEBUG_RIB_DETAILED | |
2841 | || IS_ZEBRA_DEBUG_NHG) | |
2842 | zlog_debug( | |
2843 | "%s: Nexthop ID (%u) not valid, not appending to dataplane install group", | |
2844 | __func__, depend->id); | |
2845 | continue; | |
2846 | } | |
2847 | ||
1866b3af SW |
2848 | /* If the nexthop not installed/queued for install don't |
2849 | * put in the ID array. | |
2850 | */ | |
2851 | if (!(CHECK_FLAG(depend->flags, NEXTHOP_GROUP_INSTALLED) | |
2852 | || CHECK_FLAG(depend->flags, | |
2853 | NEXTHOP_GROUP_QUEUED))) { | |
2854 | if (IS_ZEBRA_DEBUG_RIB_DETAILED | |
2855 | || IS_ZEBRA_DEBUG_NHG) | |
2856 | zlog_debug( | |
2857 | "%s: Nexthop ID (%u) not installed or queued for install, not appending to dataplane install group", | |
2858 | __func__, depend->id); | |
2859 | continue; | |
2860 | } | |
2861 | ||
b1c3f7ef | 2862 | /* Check for duplicate IDs, ignore if found. */ |
497ff579 | 2863 | for (int j = 0; j < i; j++) { |
d43122b5 | 2864 | if (depend->id == grp[j].id) { |
497ff579 | 2865 | duplicate = true; |
d43122b5 SW |
2866 | break; |
2867 | } | |
497ff579 | 2868 | } |
8dbc800f | 2869 | |
b1c3f7ef SW |
2870 | if (duplicate) { |
2871 | if (IS_ZEBRA_DEBUG_RIB_DETAILED | |
2872 | || IS_ZEBRA_DEBUG_NHG) | |
2873 | zlog_debug( | |
2874 | "%s: Nexthop ID (%u) is duplicate, not appending to dataplane install group", | |
2875 | __func__, depend->id); | |
2876 | continue; | |
497ff579 | 2877 | } |
b1c3f7ef SW |
2878 | |
2879 | grp[i].id = depend->id; | |
2880 | grp[i].weight = depend->nhg.nexthop->weight; | |
2881 | i++; | |
8dbc800f | 2882 | } |
98cda54a | 2883 | } |
8dbc800f | 2884 | |
0328a5bd MS |
2885 | if (nhe->backup_info == NULL || nhe->backup_info->nhe == NULL) |
2886 | goto done; | |
2887 | ||
2888 | /* TODO -- For now, we are not trying to use or install any | |
2889 | * backup info in this nexthop-id path: we aren't prepared | |
2890 | * to use the backups here yet. We're just debugging what we find. | |
2891 | */ | |
2892 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
2893 | zlog_debug("%s: skipping backup nhe", __func__); | |
2894 | ||
8dbc800f | 2895 | done: |
98cda54a SW |
2896 | return i; |
2897 | } | |
2898 | ||
497ff579 SW |
2899 | /* Convert a nhe into a group array */ |
2900 | uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe, | |
2901 | int max_num) | |
2902 | { | |
2903 | /* Call into the recursive function */ | |
2904 | return zebra_nhg_nhe2grp_internal(grp, 0, nhe, max_num); | |
2905 | } | |
2906 | ||
5be96a2d SW |
2907 | void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe) |
2908 | { | |
f429bd1b SW |
2909 | struct nhg_connected *rb_node_dep = NULL; |
2910 | ||
2911 | /* Resolve it first */ | |
2912 | nhe = zebra_nhg_resolve(nhe); | |
2913 | ||
2914 | /* Make sure all depends are installed/queued */ | |
fec211ad | 2915 | frr_each(nhg_connected_tree, &nhe->nhg_depends, rb_node_dep) { |
f429bd1b SW |
2916 | zebra_nhg_install_kernel(rb_node_dep->nhe); |
2917 | } | |
2918 | ||
086e4e02 | 2919 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_VALID) |
e3b9c0f2 | 2920 | && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED) |
e22e8001 | 2921 | && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) { |
724583ed | 2922 | /* Change its type to us since we are installing it */ |
0885b1e3 SW |
2923 | if (!ZEBRA_NHG_CREATED(nhe)) |
2924 | nhe->type = ZEBRA_ROUTE_NHG; | |
724583ed | 2925 | |
147bad16 | 2926 | int ret = dplane_nexthop_add(nhe); |
2d3c57e6 | 2927 | |
147bad16 SW |
2928 | switch (ret) { |
2929 | case ZEBRA_DPLANE_REQUEST_QUEUED: | |
2930 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED); | |
2931 | break; | |
2932 | case ZEBRA_DPLANE_REQUEST_FAILURE: | |
2933 | flog_err( | |
2934 | EC_ZEBRA_DP_INSTALL_FAIL, | |
68d188be DS |
2935 | "Failed to install Nexthop ID (%pNG) into the kernel", |
2936 | nhe); | |
147bad16 SW |
2937 | break; |
2938 | case ZEBRA_DPLANE_REQUEST_SUCCESS: | |
2939 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
80286aa5 | 2940 | zebra_nhg_handle_install(nhe); |
147bad16 SW |
2941 | break; |
2942 | } | |
2943 | } | |
2944 | } | |
2945 | ||
147bad16 SW |
2946 | void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe) |
2947 | { | |
2948 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)) { | |
2949 | int ret = dplane_nexthop_delete(nhe); | |
2d3c57e6 | 2950 | |
147bad16 SW |
2951 | switch (ret) { |
2952 | case ZEBRA_DPLANE_REQUEST_QUEUED: | |
2953 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED); | |
2954 | break; | |
2955 | case ZEBRA_DPLANE_REQUEST_FAILURE: | |
2956 | flog_err( | |
2957 | EC_ZEBRA_DP_DELETE_FAIL, | |
68d188be DS |
2958 | "Failed to uninstall Nexthop ID (%pNG) from the kernel", |
2959 | nhe); | |
147bad16 SW |
2960 | break; |
2961 | case ZEBRA_DPLANE_REQUEST_SUCCESS: | |
2962 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
2963 | break; | |
2964 | } | |
177e711d SW |
2965 | } |
2966 | ||
2967 | zebra_nhg_handle_uninstall(nhe); | |
147bad16 SW |
2968 | } |
2969 | ||
5f3c9e52 SW |
2970 | void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx) |
2971 | { | |
2972 | enum dplane_op_e op; | |
2973 | enum zebra_dplane_result status; | |
2974 | uint32_t id = 0; | |
2975 | struct nhg_hash_entry *nhe = NULL; | |
2976 | ||
2977 | op = dplane_ctx_get_op(ctx); | |
2978 | status = dplane_ctx_get_status(ctx); | |
2979 | ||
0c8215cb | 2980 | id = dplane_ctx_get_nhe_id(ctx); |
e22e8001 | 2981 | |
377e29f7 | 2982 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL || IS_ZEBRA_DEBUG_NHG_DETAIL) |
177e711d SW |
2983 | zlog_debug( |
2984 | "Nexthop dplane ctx %p, op %s, nexthop ID (%u), result %s", | |
2985 | ctx, dplane_op2str(op), id, dplane_res2str(status)); | |
5f3c9e52 | 2986 | |
177e711d SW |
2987 | switch (op) { |
2988 | case DPLANE_OP_NH_DELETE: | |
2989 | if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) | |
2990 | flog_err( | |
2991 | EC_ZEBRA_DP_DELETE_FAIL, | |
2992 | "Failed to uninstall Nexthop ID (%u) from the kernel", | |
2993 | id); | |
ee94437e | 2994 | |
177e711d SW |
2995 | /* We already free'd the data, nothing to do */ |
2996 | break; | |
2997 | case DPLANE_OP_NH_INSTALL: | |
2998 | case DPLANE_OP_NH_UPDATE: | |
2999 | nhe = zebra_nhg_lookup_id(id); | |
3000 | ||
3001 | if (!nhe) { | |
30672034 SW |
3002 | if (IS_ZEBRA_DEBUG_NHG) |
3003 | zlog_debug( | |
3004 | "%s operation preformed on Nexthop ID (%u) in the kernel, that we no longer have in our table", | |
3005 | dplane_op2str(op), id); | |
3006 | ||
5f3c9e52 SW |
3007 | break; |
3008 | } | |
177e711d SW |
3009 | |
3010 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED); | |
3011 | if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) { | |
3012 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_VALID); | |
3013 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
80286aa5 | 3014 | zebra_nhg_handle_install(nhe); |
ee94437e MS |
3015 | |
3016 | /* If daemon nhg, send it an update */ | |
65f137fe | 3017 | if (PROTO_OWNED(nhe)) |
ee94437e MS |
3018 | zsend_nhg_notify(nhe->type, nhe->zapi_instance, |
3019 | nhe->zapi_session, nhe->id, | |
3020 | ZAPI_NHG_INSTALLED); | |
3021 | } else { | |
3022 | /* If daemon nhg, send it an update */ | |
65f137fe | 3023 | if (PROTO_OWNED(nhe)) |
ee94437e MS |
3024 | zsend_nhg_notify(nhe->type, nhe->zapi_instance, |
3025 | nhe->zapi_session, nhe->id, | |
3026 | ZAPI_NHG_FAIL_INSTALL); | |
3027 | ||
1cadfaf2 DS |
3028 | if (!(zebra_nhg_proto_nexthops_only() && |
3029 | !PROTO_OWNED(nhe))) | |
3030 | flog_err( | |
3031 | EC_ZEBRA_DP_INSTALL_FAIL, | |
68d188be DS |
3032 | "Failed to install Nexthop (%pNG) into the kernel", |
3033 | nhe); | |
ee94437e | 3034 | } |
177e711d | 3035 | break; |
ee94437e | 3036 | |
177e711d SW |
3037 | case DPLANE_OP_ROUTE_INSTALL: |
3038 | case DPLANE_OP_ROUTE_UPDATE: | |
3039 | case DPLANE_OP_ROUTE_DELETE: | |
3040 | case DPLANE_OP_ROUTE_NOTIFY: | |
3041 | case DPLANE_OP_LSP_INSTALL: | |
3042 | case DPLANE_OP_LSP_UPDATE: | |
3043 | case DPLANE_OP_LSP_DELETE: | |
3044 | case DPLANE_OP_LSP_NOTIFY: | |
3045 | case DPLANE_OP_PW_INSTALL: | |
3046 | case DPLANE_OP_PW_UNINSTALL: | |
3047 | case DPLANE_OP_SYS_ROUTE_ADD: | |
3048 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
3049 | case DPLANE_OP_ADDR_INSTALL: | |
3050 | case DPLANE_OP_ADDR_UNINSTALL: | |
3051 | case DPLANE_OP_MAC_INSTALL: | |
3052 | case DPLANE_OP_MAC_DELETE: | |
3053 | case DPLANE_OP_NEIGH_INSTALL: | |
3054 | case DPLANE_OP_NEIGH_UPDATE: | |
3055 | case DPLANE_OP_NEIGH_DELETE: | |
0a27a2fe PG |
3056 | case DPLANE_OP_NEIGH_IP_INSTALL: |
3057 | case DPLANE_OP_NEIGH_IP_DELETE: | |
177e711d SW |
3058 | case DPLANE_OP_VTEP_ADD: |
3059 | case DPLANE_OP_VTEP_DELETE: | |
60d8d43b JU |
3060 | case DPLANE_OP_RULE_ADD: |
3061 | case DPLANE_OP_RULE_DELETE: | |
3062 | case DPLANE_OP_RULE_UPDATE: | |
d68e74b4 | 3063 | case DPLANE_OP_NEIGH_DISCOVER: |
c60522f7 | 3064 | case DPLANE_OP_BR_PORT_UPDATE: |
177e711d | 3065 | case DPLANE_OP_NONE: |
5162e000 PG |
3066 | case DPLANE_OP_IPTABLE_ADD: |
3067 | case DPLANE_OP_IPTABLE_DELETE: | |
ef524230 PG |
3068 | case DPLANE_OP_IPSET_ADD: |
3069 | case DPLANE_OP_IPSET_DELETE: | |
3070 | case DPLANE_OP_IPSET_ENTRY_ADD: | |
3071 | case DPLANE_OP_IPSET_ENTRY_DELETE: | |
e18747a9 | 3072 | case DPLANE_OP_NEIGH_TABLE_UPDATE: |
62b4b7e4 | 3073 | case DPLANE_OP_GRE_SET: |
9d59df63 MS |
3074 | case DPLANE_OP_INTF_ADDR_ADD: |
3075 | case DPLANE_OP_INTF_ADDR_DEL: | |
728f2017 | 3076 | case DPLANE_OP_INTF_NETCONFIG: |
5d414138 SW |
3077 | case DPLANE_OP_INTF_INSTALL: |
3078 | case DPLANE_OP_INTF_UPDATE: | |
3079 | case DPLANE_OP_INTF_DELETE: | |
177e711d SW |
3080 | break; |
3081 | } | |
5be96a2d SW |
3082 | } |
3083 | ||
07b9ebca | 3084 | static int zebra_nhg_sweep_entry(struct hash_bucket *bucket, void *arg) |
38e40db1 SW |
3085 | { |
3086 | struct nhg_hash_entry *nhe = NULL; | |
3087 | ||
3088 | nhe = (struct nhg_hash_entry *)bucket->data; | |
3089 | ||
dc65cd99 SW |
3090 | /* |
3091 | * same logic as with routes. | |
3092 | * | |
3093 | * If older than startup time, we know we read them in from the | |
3094 | * kernel and have not gotten and update for them since startup | |
3095 | * from an upper level proto. | |
3096 | */ | |
3097 | if (zrouter.startup_time < nhe->uptime) | |
07b9ebca | 3098 | return HASHWALK_CONTINUE; |
dc65cd99 SW |
3099 | |
3100 | /* | |
3101 | * If it's proto-owned and not being used by a route, remove it since | |
3102 | * we haven't gotten an update about it from the proto since startup. | |
3103 | * This means that either the config for it was removed or the daemon | |
3104 | * didn't get started. This handles graceful restart & retain scenario. | |
3105 | */ | |
3106 | if (PROTO_OWNED(nhe) && nhe->refcnt == 1) { | |
3107 | zebra_nhg_decrement_ref(nhe); | |
07b9ebca | 3108 | return HASHWALK_ABORT; |
dc65cd99 SW |
3109 | } |
3110 | ||
3111 | /* | |
3112 | * If its being ref'd by routes, just let it be uninstalled via a route | |
3113 | * removal. | |
3114 | */ | |
07b9ebca | 3115 | if (ZEBRA_NHG_CREATED(nhe) && nhe->refcnt <= 0) { |
38e40db1 | 3116 | zebra_nhg_uninstall_kernel(nhe); |
07b9ebca DS |
3117 | return HASHWALK_ABORT; |
3118 | } | |
3119 | ||
3120 | return HASHWALK_CONTINUE; | |
38e40db1 SW |
3121 | } |
3122 | ||
3123 | void zebra_nhg_sweep_table(struct hash *hash) | |
3124 | { | |
07b9ebca DS |
3125 | uint32_t count; |
3126 | ||
3127 | /* | |
3128 | * Yes this is extremely odd. Effectively nhg's have | |
3129 | * other nexthop groups that depend on them and when you | |
3130 | * remove them, you can have other entries blown up. | |
3131 | * our hash code does not work with deleting multiple | |
3132 | * entries at a time and will possibly cause crashes | |
3133 | * So what to do? Whenever zebra_nhg_sweep_entry | |
3134 | * deletes an entry it will return HASHWALK_ABORT, | |
3135 | * cause that deletion might have triggered more. | |
3136 | * then we can just keep sweeping this table | |
3137 | * until nothing more is found to do. | |
3138 | */ | |
3139 | do { | |
3140 | count = hashcount(hash); | |
3141 | hash_walk(hash, zebra_nhg_sweep_entry, NULL); | |
3142 | } while (count != hashcount(hash)); | |
38e40db1 | 3143 | } |
7c99d51b | 3144 | |
b1b07ef5 DS |
3145 | static void zebra_nhg_mark_keep_entry(struct hash_bucket *bucket, void *arg) |
3146 | { | |
3147 | struct nhg_hash_entry *nhe = bucket->data; | |
3148 | ||
3149 | UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED); | |
3150 | } | |
3151 | ||
3152 | /* | |
3153 | * When we are shutting down and we have retain mode enabled | |
3154 | * in zebra the process is to mark each vrf that it's | |
3155 | * routes should not be deleted. The problem with that | |
3156 | * is that shutdown actually free's up memory which | |
3157 | * causes the nexthop group's ref counts to go to zero | |
3158 | * we need a way to subtly tell the system to not remove | |
3159 | * the nexthop groups from the kernel at the same time. | |
3160 | * The easiest just looks like that we should not mark | |
3161 | * the nhg's as installed any more and when the ref count | |
3162 | * goes to zero we'll attempt to delete and do nothing | |
3163 | */ | |
3164 | void zebra_nhg_mark_keep(void) | |
3165 | { | |
3166 | hash_iterate(zrouter.nhgs_id, zebra_nhg_mark_keep_entry, NULL); | |
3167 | } | |
3168 | ||
7c99d51b MS |
3169 | /* Global control to disable use of kernel nexthops, if available. We can't |
3170 | * force the kernel to support nexthop ids, of course, but we can disable | |
3171 | * zebra's use of them, for testing e.g. By default, if the kernel supports | |
3172 | * nexthop ids, zebra uses them. | |
3173 | */ | |
3174 | void zebra_nhg_enable_kernel_nexthops(bool set) | |
3175 | { | |
3176 | g_nexthops_enabled = set; | |
3177 | } | |
3178 | ||
3179 | bool zebra_nhg_kernel_nexthops_enabled(void) | |
3180 | { | |
3181 | return g_nexthops_enabled; | |
3182 | } | |
0885b1e3 | 3183 | |
aa458838 MS |
3184 | /* Global control for use of activated backups for recursive resolution. */ |
3185 | void zebra_nhg_set_recursive_use_backups(bool set) | |
3186 | { | |
3187 | use_recursive_backups = set; | |
3188 | } | |
3189 | ||
3190 | bool zebra_nhg_recursive_use_backups(void) | |
3191 | { | |
3192 | return use_recursive_backups; | |
3193 | } | |
3194 | ||
6c67f41f SW |
3195 | /* |
3196 | * Global control to only use kernel nexthops for protocol created NHGs. | |
3197 | * There are some use cases where you may not want zebra to implicitly | |
3198 | * create kernel nexthops for all routes and only create them for NHGs | |
3199 | * passed down by upper level protos. | |
3200 | * | |
3201 | * Default is off. | |
3202 | */ | |
3203 | void zebra_nhg_set_proto_nexthops_only(bool set) | |
3204 | { | |
3205 | proto_nexthops_only = set; | |
3206 | } | |
3207 | ||
3208 | bool zebra_nhg_proto_nexthops_only(void) | |
3209 | { | |
3210 | return proto_nexthops_only; | |
3211 | } | |
3212 | ||
0885b1e3 SW |
3213 | /* Add NHE from upper level proto */ |
3214 | struct nhg_hash_entry *zebra_nhg_proto_add(uint32_t id, int type, | |
ee94437e | 3215 | uint16_t instance, uint32_t session, |
0885b1e3 SW |
3216 | struct nexthop_group *nhg, afi_t afi) |
3217 | { | |
3218 | struct nhg_hash_entry lookup; | |
dd1e105f | 3219 | struct nhg_hash_entry *new, *old; |
0885b1e3 | 3220 | struct nhg_connected *rb_node_dep = NULL; |
1f655680 SW |
3221 | struct nexthop *newhop; |
3222 | bool replace = false; | |
3223 | ||
3224 | if (!nhg->nexthop) { | |
3225 | if (IS_ZEBRA_DEBUG_NHG) | |
3226 | zlog_debug("%s: id %u, no nexthops passed to add", | |
3227 | __func__, id); | |
3228 | return NULL; | |
3229 | } | |
3230 | ||
3231 | ||
3232 | /* Set nexthop list as active, since they wont go through rib | |
3233 | * processing. | |
3234 | * | |
3235 | * Assuming valid/onlink for now. | |
3236 | * | |
3237 | * Once resolution is figured out, we won't need this! | |
3238 | */ | |
70347b7a | 3239 | for (ALL_NEXTHOPS_PTR(nhg, newhop)) { |
21735352 SW |
3240 | if (CHECK_FLAG(newhop->flags, NEXTHOP_FLAG_HAS_BACKUP)) { |
3241 | if (IS_ZEBRA_DEBUG_NHG) | |
3242 | zlog_debug( | |
3243 | "%s: id %u, backup nexthops not supported", | |
3244 | __func__, id); | |
3245 | return NULL; | |
3246 | } | |
3247 | ||
70f3cda6 SW |
3248 | if (newhop->type == NEXTHOP_TYPE_BLACKHOLE) { |
3249 | if (IS_ZEBRA_DEBUG_NHG) | |
3250 | zlog_debug( | |
3251 | "%s: id %u, blackhole nexthop not supported", | |
3252 | __func__, id); | |
3253 | return NULL; | |
3254 | } | |
3255 | ||
3256 | if (newhop->type == NEXTHOP_TYPE_IFINDEX) { | |
3257 | if (IS_ZEBRA_DEBUG_NHG) | |
3258 | zlog_debug( | |
3259 | "%s: id %u, nexthop without gateway not supported", | |
3260 | __func__, id); | |
3261 | return NULL; | |
3262 | } | |
3263 | ||
70347b7a SW |
3264 | if (!newhop->ifindex) { |
3265 | if (IS_ZEBRA_DEBUG_NHG) | |
3266 | zlog_debug( | |
70f3cda6 | 3267 | "%s: id %u, nexthop without ifindex is not supported", |
70347b7a SW |
3268 | __func__, id); |
3269 | return NULL; | |
3270 | } | |
1f655680 | 3271 | SET_FLAG(newhop->flags, NEXTHOP_FLAG_ACTIVE); |
70347b7a | 3272 | } |
0885b1e3 SW |
3273 | |
3274 | zebra_nhe_init(&lookup, afi, nhg->nexthop); | |
3275 | lookup.nhg.nexthop = nhg->nexthop; | |
3276 | lookup.id = id; | |
3277 | lookup.type = type; | |
3278 | ||
dd1e105f SW |
3279 | old = zebra_nhg_lookup_id(id); |
3280 | ||
3281 | if (old) { | |
3282 | /* | |
3283 | * This is a replace, just release NHE from ID for now, The | |
84591282 SW |
3284 | * depends/dependents may still be used in the replacement so |
3285 | * we don't touch them other than to remove their refs to their | |
3286 | * old parent. | |
dd1e105f | 3287 | */ |
1f655680 | 3288 | replace = true; |
dd1e105f | 3289 | hash_release(zrouter.nhgs_id, old); |
84591282 SW |
3290 | |
3291 | /* Free all the things */ | |
3292 | zebra_nhg_release_all_deps(old); | |
dd1e105f SW |
3293 | } |
3294 | ||
0885b1e3 SW |
3295 | new = zebra_nhg_rib_find_nhe(&lookup, afi); |
3296 | ||
1f655680 SW |
3297 | zebra_nhg_increment_ref(new); |
3298 | ||
ee94437e MS |
3299 | /* Capture zapi client info */ |
3300 | new->zapi_instance = instance; | |
3301 | new->zapi_session = session; | |
3302 | ||
1f655680 SW |
3303 | zebra_nhg_set_valid_if_active(new); |
3304 | ||
3305 | zebra_nhg_install_kernel(new); | |
3306 | ||
dd1e105f | 3307 | if (old) { |
8155e8c5 SW |
3308 | /* |
3309 | * Check to handle recving DEL while routes still in use then | |
3310 | * a replace. | |
3311 | * | |
3312 | * In this case we would have decremented the refcnt already | |
3313 | * but set the FLAG here. Go ahead and increment once to fix | |
3314 | * the misordering we have been sent. | |
3315 | */ | |
3316 | if (CHECK_FLAG(old->flags, NEXTHOP_GROUP_PROTO_RELEASED)) | |
3317 | zebra_nhg_increment_ref(old); | |
3318 | ||
1f655680 | 3319 | rib_handle_nhg_replace(old, new); |
dd1e105f | 3320 | |
1f655680 SW |
3321 | /* We have to decrement its singletons |
3322 | * because some might not exist in NEW. | |
3323 | */ | |
3324 | if (!zebra_nhg_depends_is_empty(old)) { | |
3325 | frr_each (nhg_connected_tree, &old->nhg_depends, | |
3326 | rb_node_dep) | |
3327 | zebra_nhg_decrement_ref(rb_node_dep->nhe); | |
3328 | } | |
3329 | ||
1f655680 SW |
3330 | /* Dont call the dec API, we dont want to uninstall the ID */ |
3331 | old->refcnt = 0; | |
35729f38 | 3332 | THREAD_OFF(old->timer); |
1f655680 SW |
3333 | zebra_nhg_free(old); |
3334 | old = NULL; | |
0885b1e3 SW |
3335 | } |
3336 | ||
3337 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
dd1e105f | 3338 | zlog_debug("%s: %s nhe %p (%u), vrf %d, type %s", __func__, |
1f655680 | 3339 | (replace ? "replaced" : "added"), new, new->id, |
dd1e105f | 3340 | new->vrf_id, zebra_route_string(new->type)); |
0885b1e3 SW |
3341 | |
3342 | return new; | |
3343 | } | |
3344 | ||
1f655680 | 3345 | /* Delete NHE from upper level proto, caller must decrement ref */ |
aaa42e05 | 3346 | struct nhg_hash_entry *zebra_nhg_proto_del(uint32_t id, int type) |
0885b1e3 SW |
3347 | { |
3348 | struct nhg_hash_entry *nhe; | |
3349 | ||
3350 | nhe = zebra_nhg_lookup_id(id); | |
3351 | ||
3352 | if (!nhe) { | |
8155e8c5 | 3353 | if (IS_ZEBRA_DEBUG_NHG) |
0885b1e3 SW |
3354 | zlog_debug("%s: id %u, lookup failed", __func__, id); |
3355 | ||
3356 | return NULL; | |
3357 | } | |
3358 | ||
aaa42e05 SW |
3359 | if (type != nhe->type) { |
3360 | if (IS_ZEBRA_DEBUG_NHG) | |
3361 | zlog_debug( | |
3362 | "%s: id %u, type %s mismatch, sent by %s, ignoring", | |
3363 | __func__, id, zebra_route_string(nhe->type), | |
3364 | zebra_route_string(type)); | |
3365 | return NULL; | |
3366 | } | |
3367 | ||
8155e8c5 SW |
3368 | if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_PROTO_RELEASED)) { |
3369 | if (IS_ZEBRA_DEBUG_NHG) | |
3370 | zlog_debug("%s: id %u, already released", __func__, id); | |
3371 | ||
3372 | return NULL; | |
3373 | } | |
3374 | ||
3375 | SET_FLAG(nhe->flags, NEXTHOP_GROUP_PROTO_RELEASED); | |
3376 | ||
1f655680 | 3377 | if (nhe->refcnt > 1) { |
0885b1e3 | 3378 | if (IS_ZEBRA_DEBUG_NHG) |
1f655680 | 3379 | zlog_debug( |
68d188be DS |
3380 | "%s: %pNG, still being used by routes refcnt %u", |
3381 | __func__, nhe, nhe->refcnt); | |
9c6c48bc | 3382 | return nhe; |
0885b1e3 SW |
3383 | } |
3384 | ||
3385 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
68d188be DS |
3386 | zlog_debug("%s: deleted nhe %p (%pNG), vrf %d, type %s", |
3387 | __func__, nhe, nhe, nhe->vrf_id, | |
0885b1e3 SW |
3388 | zebra_route_string(nhe->type)); |
3389 | ||
3390 | return nhe; | |
3391 | } | |
3392 | ||
24db1a7b SW |
3393 | struct nhg_score_proto_iter { |
3394 | int type; | |
8f830b8c | 3395 | struct list *found; |
24db1a7b SW |
3396 | }; |
3397 | ||
3398 | static void zebra_nhg_score_proto_entry(struct hash_bucket *bucket, void *arg) | |
3399 | { | |
3400 | struct nhg_hash_entry *nhe; | |
3401 | struct nhg_score_proto_iter *iter; | |
3402 | ||
3403 | nhe = (struct nhg_hash_entry *)bucket->data; | |
3404 | iter = arg; | |
3405 | ||
3406 | /* Needs to match type and outside zebra ID space */ | |
65f137fe | 3407 | if (nhe->type == iter->type && PROTO_OWNED(nhe)) { |
24db1a7b SW |
3408 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) |
3409 | zlog_debug( | |
68d188be DS |
3410 | "%s: found nhe %p (%pNG), vrf %d, type %s after client disconnect", |
3411 | __func__, nhe, nhe, nhe->vrf_id, | |
24db1a7b SW |
3412 | zebra_route_string(nhe->type)); |
3413 | ||
8f830b8c SW |
3414 | /* Add to removal list */ |
3415 | listnode_add(iter->found, nhe); | |
24db1a7b SW |
3416 | } |
3417 | } | |
3418 | ||
3419 | /* Remove specific by proto NHGs */ | |
3420 | unsigned long zebra_nhg_score_proto(int type) | |
3421 | { | |
8f830b8c | 3422 | struct nhg_hash_entry *nhe; |
24db1a7b | 3423 | struct nhg_score_proto_iter iter = {}; |
8f830b8c SW |
3424 | struct listnode *ln; |
3425 | unsigned long count; | |
24db1a7b SW |
3426 | |
3427 | iter.type = type; | |
8f830b8c | 3428 | iter.found = list_new(); |
24db1a7b | 3429 | |
8f830b8c | 3430 | /* Find matching entries to remove */ |
24db1a7b SW |
3431 | hash_iterate(zrouter.nhgs_id, zebra_nhg_score_proto_entry, &iter); |
3432 | ||
8f830b8c SW |
3433 | /* Now remove them */ |
3434 | for (ALL_LIST_ELEMENTS_RO(iter.found, ln, nhe)) { | |
3435 | /* | |
3436 | * This should be the last ref if we remove client routes too, | |
3437 | * and thus should remove and free them. | |
3438 | */ | |
3439 | zebra_nhg_decrement_ref(nhe); | |
3440 | } | |
3441 | ||
3442 | count = iter.found->count; | |
3443 | list_delete(&iter.found); | |
3444 | ||
3445 | return count; | |
24db1a7b | 3446 | } |
cc75cbea DS |
3447 | |
3448 | printfrr_ext_autoreg_p("NG", printfrr_nhghe); | |
3449 | static ssize_t printfrr_nhghe(struct fbuf *buf, struct printfrr_eargs *ea, | |
3450 | const void *ptr) | |
3451 | { | |
3452 | const struct nhg_hash_entry *nhe = ptr; | |
3453 | const struct nhg_connected *dep; | |
3454 | ssize_t ret = 0; | |
3455 | ||
3456 | if (!nhe) | |
3457 | return bputs(buf, "[NULL]"); | |
3458 | ||
3459 | ret += bprintfrr(buf, "%u[", nhe->id); | |
3460 | if (nhe->ifp) | |
3461 | ret += printfrr_nhs(buf, nhe->nhg.nexthop); | |
3462 | else { | |
3463 | int count = zebra_nhg_depends_count(nhe); | |
3464 | ||
3465 | frr_each (nhg_connected_tree_const, &nhe->nhg_depends, dep) { | |
3466 | ret += bprintfrr(buf, "%u", dep->nhe->id); | |
3467 | if (count > 1) | |
3468 | ret += bputs(buf, "/"); | |
3469 | count--; | |
3470 | } | |
3471 | } | |
3472 | ||
3473 | ret += bputs(buf, "]"); | |
3474 | return ret; | |
3475 | } |