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