xref: /freebsd/sys/net/route/nhgrp_ctl.c (revision be42ac4dceacedb0e4f676a0614e14d125fab36b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include "opt_inet.h"
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/lock.h>
32 #include <sys/rmlock.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 #include <sys/refcount.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/kernel.h>
39 #include <sys/epoch.h>
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_private.h>
44 #include <net/route.h>
45 #include <net/route/route_ctl.h>
46 #include <net/route/route_var.h>
47 #include <net/vnet.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_fib.h>
52 
53 #include <net/route/nhop_utils.h>
54 #include <net/route/nhop.h>
55 #include <net/route/nhop_var.h>
56 #include <net/route/nhgrp_var.h>
57 
58 #define	DEBUG_MOD_NAME	nhgrp_ctl
59 #define	DEBUG_MAX_LEVEL	LOG_DEBUG
60 #include <net/route/route_debug.h>
61 _DECLARE_DEBUG(LOG_INFO);
62 
63 /*
64  * This file contains the supporting functions for creating multipath groups
65  *  and compiling their dataplane parts.
66  */
67 
68 /* MPF_MULTIPATH must be the same as NHF_MULTIPATH for nhop selection to work */
69 _Static_assert(MPF_MULTIPATH == NHF_MULTIPATH,
70     "MPF_MULTIPATH must be the same as NHF_MULTIPATH");
71 /* Offset and size of flags field has to be the same for nhop/nhop groups */
72 CHK_STRUCT_FIELD_GENERIC(struct nhop_object, nh_flags, struct nhgrp_object, nhg_flags);
73 /* Cap multipath to 64, as the larger values would break rib_cmd_info bmasks */
74 CTASSERT(RIB_MAX_MPATH_WIDTH <= 64);
75 
76 static int wn_cmp_idx(const void *a, const void *b);
77 static void sort_weightened_nhops(struct weightened_nhop *wn, int num_nhops);
78 
79 static struct nhgrp_priv *get_nhgrp(struct nh_control *ctl,
80     struct weightened_nhop *wn, int num_nhops, uint32_t uidx, int *perror);
81 static void destroy_nhgrp(struct nhgrp_priv *nhg_priv);
82 static void destroy_nhgrp_epoch(epoch_context_t ctx);
83 static void free_nhgrp_nhops(struct nhgrp_priv *nhg_priv);
84 
85 static int
wn_cmp_idx(const void * a,const void * b)86 wn_cmp_idx(const void *a, const void *b)
87 {
88 	const struct weightened_nhop *w_a = a;
89 	const struct weightened_nhop *w_b = b;
90 	uint32_t a_idx = w_a->nh->nh_priv->nh_idx;
91 	uint32_t b_idx = w_b->nh->nh_priv->nh_idx;
92 
93 	if (a_idx < b_idx)
94 		return (-1);
95 	else if (a_idx > b_idx)
96 		return (1);
97 	else
98 		return (0);
99 }
100 
101 /*
102  * Perform in-place sorting for array of nexthops in @wn.
103  * Sort by nexthop index ascending.
104  */
105 static void
sort_weightened_nhops(struct weightened_nhop * wn,int num_nhops)106 sort_weightened_nhops(struct weightened_nhop *wn, int num_nhops)
107 {
108 
109 	qsort(wn, num_nhops, sizeof(struct weightened_nhop), wn_cmp_idx);
110 }
111 
112 /*
113  * In order to determine the minimum weight difference in the array
114  * of weights, create a sorted array of weights, using spare "storage"
115  * field in the `struct weightened_nhop`.
116  * Assume weights to be (mostly) the same and use insertion sort to
117  * make it sorted.
118  */
119 static void
sort_weightened_nhops_weights(struct weightened_nhop * wn,int num_items)120 sort_weightened_nhops_weights(struct weightened_nhop *wn, int num_items)
121 {
122 	wn[0].storage = wn[0].weight;
123 	for (int i = 1, j = 0; i < num_items; i++) {
124 		uint32_t weight = wn[i].weight; // read from 'weight' as it's not reordered
125 		/* Move all weights > weight 1 position right */
126 		for (j = i - 1; j >= 0 && wn[j].storage > weight; j--)
127 			wn[j + 1].storage = wn[j].storage;
128 		wn[j + 1].storage = weight;
129 	}
130 }
131 
132 /*
133  * Calculate minimum number of slots required to fit the existing
134  * set of weights in the common use case where weights are "easily"
135  * comparable.
136  * Assumes @wn is sorted by weight ascending and each weight is > 0.
137  * Returns number of slots or 0 if precise calculation failed.
138  * Only calculate for nexthops with specified metric and ignore the rest.
139  *
140  * Some examples:
141  * note: (i, X) pair means (nhop=i, weight=X):
142  * (1, 1) (2, 2) -> 3 slots [1, 2, 2]
143  * (1, 100), (2, 200) -> 3 slots [1, 2, 2]
144  * (1, 100), (2, 200), (3, 400) -> 7 slots [1, 2, 2, 3, 3, 3]
145  */
146 static uint32_t
calc_min_mpath_slots_fast(struct weightened_nhop * wn,size_t num_items,uint32_t metric,uint64_t * ptotal)147 calc_min_mpath_slots_fast(struct weightened_nhop *wn, size_t num_items,
148     uint32_t metric, uint64_t *ptotal)
149 {
150 	uint32_t i, x, last, xmin = 0;
151 	uint64_t total = 0;
152 
153 	// Get sorted array of weights in .storage field
154 	sort_weightened_nhops_weights(wn, num_items);
155 
156 	/* start with lowest metric */
157 	for (x = 0; x < num_items; x++) {
158 		if (nhop_get_metric(wn[x].nh) == metric) {
159 			xmin = wn[x].storage;
160 			break;
161 		}
162 	}
163 	last = 0;
164 	for (i = x; i < num_items; i++) {
165 		if (nhop_get_metric(wn[i].nh) != metric)
166 			continue;
167 
168 		total += wn[i].storage;
169 		if ((wn[i].storage != last) &&
170 		    ((wn[i].storage - last < xmin) || xmin == 0)) {
171 			xmin = wn[i].storage - last;
172 		}
173 		last = wn[i].storage;
174 	}
175 	*ptotal = total;
176 	/* xmin is the minimum unit of desired capacity */
177 	if ((total % xmin) != 0)
178 		return (0);
179 	for (i = 0; i < num_items; i++) {
180 		if ((wn[i].weight % xmin) != 0)
181 			return (0);
182 	}
183 
184 	return ((uint32_t)(total / xmin));
185 }
186 
187 /*
188  * Calculate minimum number of slots required to fit the existing
189  * set of weights while maintaining weight coefficients
190  * after filtering by metric.
191  *
192  * Assume @wn is sorted by weight ascending and each weight is > 0.
193  *
194  * Tries to find simple precise solution first and falls back to
195  *  RIB_MAX_MPATH_WIDTH in case of any failure.
196  */
197 static uint32_t
calc_min_mpath_slots(struct weightened_nhop * wn,size_t num_items,uint32_t metric)198 calc_min_mpath_slots(struct weightened_nhop *wn, size_t num_items,
199     uint32_t metric)
200 {
201 	uint32_t v;
202 	uint64_t total;
203 
204 	v = calc_min_mpath_slots_fast(wn, num_items, metric, &total);
205 	if (total == 0)
206 		return (0);
207 	if ((v == 0) || (v > RIB_MAX_MPATH_WIDTH))
208 		v = RIB_MAX_MPATH_WIDTH;
209 
210 	return (v);
211 }
212 
213 /*
214  * Nexthop group data consists of
215  * 1) dataplane part, with nhgrp_object as a header followed by an
216  *   arbitrary number of nexthop pointers.
217  * 2) control plane part, with nhgrp_priv as a header, followed by
218  *   an arbirtrary number of 'struct weightened_nhop' object.
219  *
220  * Given nexthop groups are (mostly) immutable, allocate all data
221  * in one go.
222  *
223  */
224 __noinline static size_t
get_nhgrp_alloc_size(uint32_t nhg_size,uint32_t num_nhops)225 get_nhgrp_alloc_size(uint32_t nhg_size, uint32_t num_nhops)
226 {
227 	size_t sz;
228 
229 	sz = sizeof(struct nhgrp_object);
230 	sz += nhg_size * sizeof(struct nhop_object *);
231 	sz += sizeof(struct nhgrp_priv);
232 	sz += num_nhops * sizeof(struct weightened_nhop);
233 	return (sz);
234 }
235 
236 /*
237  * Compile actual list of nexthops to be used by datapath from
238  *  the nexthop group @dst.
239  * Since we only need nexthops with lowest metric, only process
240  * nexthops with specified metric. The metric argument is taken
241  * from input and is expected to be the lowest metric in weightened_nhop.
242  *
243  * For example, compiling control plane list of 2 nexthops
244  *  [(200, A), (100, B)] would result in the datapath array
245  *  [A, A, B]
246  */
247 static void
compile_nhgrp(struct nhgrp_priv * dst_priv,const struct weightened_nhop * x,uint32_t num_slots,uint32_t metric)248 compile_nhgrp(struct nhgrp_priv *dst_priv, const struct weightened_nhop *x,
249     uint32_t num_slots, uint32_t metric)
250 {
251 	struct nhgrp_object *dst;
252 	int i, slot_idx, remaining_slots;
253 	uint64_t remaining_sum, nh_weight, nh_slots;
254 	bool one_reachable = true;
255 
256 	slot_idx  = 0;
257 	dst = dst_priv->nhg;
258 	/* Calculate sum of all weights with lowest metric */
259 	remaining_sum = nh_weight = 0;
260 	for (i = 0; i < dst_priv->nhg_nh_count; i++) {
261 		if (nhop_get_metric(x[i].nh) == metric) {
262 			/*
263 			 * Temporary store weight of unreachable nhops in nh_weight
264 			 * to ensure we have at least one reachable nexthop.
265 			 */
266 			if (NH_IS_VALID(x[i].nh))
267 				remaining_sum += x[i].weight;
268 			else
269 				nh_weight += x[i].weight;
270 		}
271 	}
272 
273 	/* If no reachable nhops exist, include all */
274 	if (remaining_sum == 0) {
275 		remaining_sum = nh_weight;
276 		one_reachable = false;
277 	}
278 
279 	remaining_slots = num_slots;
280 	FIB_NH_LOG(LOG_DEBUG3, x[0].nh, "sum: %lu, slots: %d, lowest_metric: %u",
281 	    remaining_sum, remaining_slots, metric);
282 	for (i = 0; i < dst_priv->nhg_nh_count; i++) {
283 		if (nhop_get_metric(x[i].nh) != metric)
284 			continue;
285 
286 		/*
287 		 * Calculate number of slots for the current nexthop.
288 		 * Exclude unreachable nexthops if there is one reachable.
289 		 */
290 		if (remaining_sum > 0 &&
291 		    (NH_IS_VALID(x[i].nh) || !one_reachable)) {
292 			nh_weight = (uint64_t)x[i].weight;
293 			nh_slots = (nh_weight * remaining_slots / remaining_sum);
294 			remaining_sum -= x[i].weight;
295 		} else
296 			nh_slots = 0;
297 
298 		remaining_slots -= nh_slots;
299 
300 		FIB_NH_LOG(LOG_DEBUG3, x[0].nh,
301 		    " rem_sum: %lu, rem_slots: %d nh_slots: %d, slot_idx: %d",
302 		    remaining_sum, remaining_slots, (int)nh_slots, slot_idx);
303 
304 		KASSERT((slot_idx + nh_slots <= num_slots),
305 		    ("index overflow during nhg compilation"));
306 		while (nh_slots-- > 0)
307 			dst->nhops[slot_idx++] = x[i].nh;
308 	}
309 }
310 
311 /*
312  * Allocates new nexthop group for the list of weightened nexthops.
313  * Assume sorted list.
314  * Does NOT reference any nexthops in the group.
315  * Returns group with refcount=1 or NULL.
316  */
317 static struct nhgrp_priv *
alloc_nhgrp(struct weightened_nhop * wn,int num_nhops,uint32_t min_metric)318 alloc_nhgrp(struct weightened_nhop *wn, int num_nhops, uint32_t min_metric)
319 {
320 	uint32_t nhgrp_size;
321 	struct nhgrp_object *nhg;
322 	struct nhgrp_priv *nhg_priv;
323 
324 	nhgrp_size = calc_min_mpath_slots(wn, num_nhops, min_metric);
325 	if (nhgrp_size == 0) {
326 		/* Zero weights, abort */
327 		return (NULL);
328 	}
329 
330 	size_t sz = get_nhgrp_alloc_size(nhgrp_size, num_nhops);
331 	nhg = malloc(sz, M_NHOP, M_NOWAIT | M_ZERO);
332 	if (nhg == NULL) {
333 		FIB_NH_LOG(LOG_INFO, wn[0].nh,
334 		    "unable to allocate group with num_nhops %d (compiled %u)",
335 		    num_nhops, nhgrp_size);
336 		return (NULL);
337 	}
338 
339 	/* Has to be the first to make NHGRP_PRIV() work */
340 	nhg->nhg_size = nhgrp_size;
341 	nhg->nhg_flags = MPF_MULTIPATH;
342 
343 	nhg_priv = NHGRP_PRIV(nhg);
344 	nhg_priv->nhg_nh_count = num_nhops;
345 	refcount_init(&nhg_priv->nhg_refcount, 1);
346 
347 	/* Please see nhgrp_free() comments on the initial value */
348 	refcount_init(&nhg_priv->nhg_linked, 2);
349 
350 	nhg_priv->nhg = nhg;
351 	memcpy(&nhg_priv->nhg_nh_weights[0], wn,
352 	  num_nhops * sizeof(struct weightened_nhop));
353 
354 	FIB_NH_LOG(LOG_DEBUG, wn[0].nh, "num_nhops: %d, compiled_nhop: %u",
355 	    num_nhops, nhgrp_size);
356 
357 	compile_nhgrp(nhg_priv, wn, nhg->nhg_size, min_metric);
358 
359 	return (nhg_priv);
360 }
361 
362 void
nhgrp_ref_object(struct nhgrp_object * nhg)363 nhgrp_ref_object(struct nhgrp_object *nhg)
364 {
365 	struct nhgrp_priv *nhg_priv;
366 	u_int old __diagused;
367 
368 	nhg_priv = NHGRP_PRIV(nhg);
369 	old = refcount_acquire(&nhg_priv->nhg_refcount);
370 	KASSERT(old > 0, ("%s: nhgrp object %p has 0 refs", __func__, nhg));
371 }
372 
373 void
nhgrp_free(struct nhgrp_object * nhg)374 nhgrp_free(struct nhgrp_object *nhg)
375 {
376 	struct nhgrp_priv *nhg_priv;
377 	struct nh_control *ctl;
378 	struct epoch_tracker et;
379 
380 	nhg_priv = NHGRP_PRIV(nhg);
381 
382 	if (!refcount_release(&nhg_priv->nhg_refcount))
383 		return;
384 
385 	/*
386 	 * group objects don't have an explicit lock attached to it.
387 	 * As groups are reclaimed based on reference count, it is possible
388 	 * that some groups will persist after vnet destruction callback
389 	 * called. Given that, handle scenario with nhgrp_free_group() being
390 	 * called either after or simultaneously with nhgrp_ctl_unlink_all()
391 	 * by using another reference counter: nhg_linked.
392 	 *
393 	 * There are only 2 places, where nhg_linked can be decreased:
394 	 *  rib destroy (nhgrp_ctl_unlink_all) and this function.
395 	 * nhg_link can never be increased.
396 	 *
397 	 * Hence, use initial value of 2 to make use of
398 	 *  refcount_release_if_not_last().
399 	 *
400 	 * There can be two scenarious when calling this function:
401 	 *
402 	 * 1) nhg_linked value is 2. This means that either
403 	 *  nhgrp_ctl_unlink_all() has not been called OR it is running,
404 	 *  but we are guaranteed that nh_control won't be freed in
405 	 *  this epoch. Hence, nexthop can be safely unlinked.
406 	 *
407 	 * 2) nh_linked value is 1. In that case, nhgrp_ctl_unlink_all()
408 	 *  has been called and nhgrp unlink can be skipped.
409 	 */
410 
411 	NET_EPOCH_ENTER(et);
412 	if (refcount_release_if_not_last(&nhg_priv->nhg_linked)) {
413 		ctl = nhg_priv->nh_control;
414 		if (unlink_nhgrp(ctl, nhg_priv) == NULL) {
415 			/* Do not try to reclaim */
416 			RT_LOG(LOG_INFO, "Failed to unlink nexhop group %p",
417 			    nhg_priv);
418 			NET_EPOCH_EXIT(et);
419 			return;
420 		}
421 		MPASS((nhg_priv->nhg_idx == 0));
422 		MPASS((nhg_priv->nhg_refcount == 0));
423 	}
424 	NET_EPOCH_EXIT(et);
425 
426 	NET_EPOCH_CALL(destroy_nhgrp_epoch, &nhg_priv->nhg_epoch_ctx);
427 }
428 
429 /*
430  * Destroys all local resources belonging to @nhg_priv.
431  */
432 __noinline static void
destroy_nhgrp_int(struct nhgrp_priv * nhg_priv)433 destroy_nhgrp_int(struct nhgrp_priv *nhg_priv)
434 {
435 
436 	free(nhg_priv->nhg, M_NHOP);
437 }
438 
439 __noinline static void
destroy_nhgrp(struct nhgrp_priv * nhg_priv)440 destroy_nhgrp(struct nhgrp_priv *nhg_priv)
441 {
442 	IF_DEBUG_LEVEL(LOG_DEBUG2) {
443 		char nhgbuf[NHOP_PRINT_BUFSIZE] __unused;
444 		FIB_NH_LOG(LOG_DEBUG2, nhg_priv->nhg_nh_weights[0].nh,
445 		    "destroying %s", nhgrp_print_buf(nhg_priv->nhg,
446 		    nhgbuf, sizeof(nhgbuf)));
447 	}
448 
449 	free_nhgrp_nhops(nhg_priv);
450 	destroy_nhgrp_int(nhg_priv);
451 }
452 
453 /*
454  * Epoch callback indicating group is safe to destroy
455  */
456 static void
destroy_nhgrp_epoch(epoch_context_t ctx)457 destroy_nhgrp_epoch(epoch_context_t ctx)
458 {
459 	struct nhgrp_priv *nhg_priv;
460 
461 	nhg_priv = __containerof(ctx, struct nhgrp_priv, nhg_epoch_ctx);
462 
463 	destroy_nhgrp(nhg_priv);
464 }
465 
466 static bool
ref_nhgrp_nhops(struct nhgrp_priv * nhg_priv)467 ref_nhgrp_nhops(struct nhgrp_priv *nhg_priv)
468 {
469 
470 	for (int i = 0; i < nhg_priv->nhg_nh_count; i++) {
471 		if (nhop_try_ref_object(nhg_priv->nhg_nh_weights[i].nh) != 0)
472 			continue;
473 
474 		/*
475 		 * Failed to ref the nexthop, b/c it's deleted.
476 		 * Need to rollback references back.
477 		 */
478 		for (int j = 0; j < i; j++)
479 			nhop_free(nhg_priv->nhg_nh_weights[j].nh);
480 		return (false);
481 	}
482 
483 	return (true);
484 }
485 
486 static void
free_nhgrp_nhops(struct nhgrp_priv * nhg_priv)487 free_nhgrp_nhops(struct nhgrp_priv *nhg_priv)
488 {
489 
490 	for (int i = 0; i < nhg_priv->nhg_nh_count; i++)
491 		nhop_free(nhg_priv->nhg_nh_weights[i].nh);
492 }
493 
494 /*
495  * Allocate nexthop group of size @num_nhops with nexthops specified by
496  * @wn. Nexthops have to be unique and match the fibnum/family of the group.
497  * Returns unlinked nhgrp object on success or NULL and non-zero perror.
498  */
499 struct nhgrp_object *
nhgrp_alloc(uint32_t fibnum,int family,struct weightened_nhop * wn,int num_nhops,int * perror)500 nhgrp_alloc(uint32_t fibnum, int family, struct weightened_nhop *wn, int num_nhops,
501     int *perror)
502 {
503 	struct rib_head *rh = rt_tables_get_rnh(fibnum, family);
504 	struct nhgrp_priv *nhg_priv;
505 	struct nh_control *ctl;
506 
507 	MPASS((num_nhops != 0));
508 
509 	if (rh == NULL) {
510 		*perror = E2BIG;
511 		return (NULL);
512 	}
513 
514 	ctl = rh->nh_control;
515 
516 	if (num_nhops > RIB_MAX_MPATH_WIDTH) {
517 		*perror = E2BIG;
518 		return (NULL);
519 	}
520 
521 	if (ctl->gr_head.hash_size == 0) {
522 		/* First multipath request. Bootstrap mpath datastructures. */
523 		if (nhgrp_ctl_alloc_default(ctl, M_NOWAIT) == 0) {
524 			*perror = ENOMEM;
525 			return (NULL);
526 		}
527 		if (V_fib_hash_outbound == 0) {
528 			/* Enable local outbound connections hashing. */
529 			if (bootverbose)
530 				printf("FIB: enabled flowid calculation for locally-originated packets\n");
531 			V_fib_hash_outbound = 1;
532 		}
533 	}
534 
535 	/* Sort nexthops & check there are no duplicates */
536 	sort_weightened_nhops(wn, num_nhops);
537 	uint32_t last_id = 0;
538 	uint32_t min_metric = nhop_get_metric(wn[0].nh);
539 	for (int i = 0; i < num_nhops; i++) {
540 		if (wn[i].nh->nh_priv->nh_control != ctl) {
541 			*perror = EINVAL;
542 			return (NULL);
543 		}
544 		if (wn[i].nh->nh_priv->nh_idx == last_id) {
545 			*perror = EEXIST;
546 			return (NULL);
547 		}
548 		last_id = wn[i].nh->nh_priv->nh_idx;
549 
550 		if (nhop_get_metric(wn[i].nh) < min_metric)
551 			min_metric = nhop_get_metric(wn[i].nh);
552 	}
553 
554 	if ((nhg_priv = alloc_nhgrp(wn, num_nhops, min_metric)) == NULL) {
555 		*perror = ENOMEM;
556 		return (NULL);
557 	}
558 	nhg_priv->nh_control = ctl;
559 
560 	*perror = 0;
561 	return (nhg_priv->nhg);
562 }
563 
564 /*
565  * Finds an existing group matching @nhg or links @nhg to the tree.
566  * Returns the referenced group or NULL and non-zero @perror.
567  */
568 struct nhgrp_object *
nhgrp_get_nhgrp(struct nhgrp_object * nhg,int * perror)569 nhgrp_get_nhgrp(struct nhgrp_object *nhg, int *perror)
570 {
571 	struct nhgrp_priv *nhg_priv, *key = NHGRP_PRIV(nhg);
572 	struct nh_control *ctl = key->nh_control;
573 
574 	nhg_priv = find_nhgrp(ctl, key);
575 	if (nhg_priv != NULL) {
576 		/*
577 		 * Free originally-created group. As it hasn't been linked
578 		 *  and the dependent nexhops haven't been referenced, just free
579 		 *  the group.
580 		 */
581 		destroy_nhgrp_int(key);
582 		*perror = 0;
583 		return (nhg_priv->nhg);
584 	} else {
585 		/* No existing group, try to link the new one */
586 		if (!ref_nhgrp_nhops(key)) {
587 			/*
588 			 * Some of the nexthops have been scheduled for deletion.
589 			 * As the group hasn't been linked / no nexhops have been
590 			 *  referenced, call the final destructor immediately.
591 			 */
592 			destroy_nhgrp_int(key);
593 			*perror = EAGAIN;
594 			return (NULL);
595 		}
596 		if (link_nhgrp(ctl, key) == 0) {
597 			/* Unable to allocate index? */
598 			*perror = EAGAIN;
599 			free_nhgrp_nhops(key);
600 			destroy_nhgrp_int(key);
601 			return (NULL);
602 		}
603 		*perror = 0;
604 		return (nhg);
605 	}
606 
607 	/* NOTREACHED */
608 }
609 
610 /*
611  * Creates or looks up an existing nexthop group based on @wn and @num_nhops.
612  *
613  * Returns referenced nhop group or NULL, passing error code in @perror.
614  */
615 struct nhgrp_priv *
get_nhgrp(struct nh_control * ctl,struct weightened_nhop * wn,int num_nhops,uint32_t uidx,int * perror)616 get_nhgrp(struct nh_control *ctl, struct weightened_nhop *wn, int num_nhops,
617     uint32_t uidx, int *perror)
618 {
619 	struct nhgrp_object *nhg;
620 
621 	nhg = nhgrp_alloc(ctl->ctl_rh->rib_fibnum, ctl->ctl_rh->rib_family,
622 	    wn, num_nhops, perror);
623 	if (nhg == NULL)
624 		return (NULL);
625 	nhgrp_set_uidx(nhg, uidx);
626 	nhg = nhgrp_get_nhgrp(nhg, perror);
627 	if (nhg != NULL)
628 		return (NHGRP_PRIV(nhg));
629 	return (NULL);
630 }
631 
632 
633 /*
634  * Appends one or more nexthops denoted by @wm to the nexthop group @gr_orig.
635  *
636  * Returns referenced nexthop group or NULL. In the latter case, @perror is
637  *  filled with an error code.
638  * Note that function does NOT care if the next nexthops already exists
639  * in the @gr_orig. As a result, they will be added, resulting in the
640  * same nexthop being present multiple times in the new group.
641  */
642 static struct nhgrp_priv *
append_nhops(struct nh_control * ctl,const struct nhgrp_object * gr_orig,struct weightened_nhop * wn,int num_nhops,int * perror)643 append_nhops(struct nh_control *ctl, const struct nhgrp_object *gr_orig,
644     struct weightened_nhop *wn, int num_nhops, int *perror)
645 {
646 	char storage[64];
647 	struct weightened_nhop *pnhops;
648 	struct nhgrp_priv *nhg_priv;
649 	const struct nhgrp_priv *src_priv;
650 	size_t sz;
651 	int curr_nhops;
652 
653 	src_priv = NHGRP_PRIV_CONST(gr_orig);
654 	curr_nhops = src_priv->nhg_nh_count;
655 
656 	*perror = 0;
657 
658 	sz = (src_priv->nhg_nh_count + num_nhops) * (sizeof(struct weightened_nhop));
659 	/* optimize for <= 4 paths, each path=16 bytes */
660 	if (sz <= sizeof(storage))
661 		pnhops = (struct weightened_nhop *)&storage[0];
662 	else {
663 		pnhops = malloc(sz, M_TEMP, M_NOWAIT);
664 		if (pnhops == NULL) {
665 			*perror = ENOMEM;
666 			return (NULL);
667 		}
668 	}
669 
670 	/* Copy nhops from original group first */
671 	memcpy(pnhops, src_priv->nhg_nh_weights,
672 	  curr_nhops * sizeof(struct weightened_nhop));
673 	memcpy(&pnhops[curr_nhops], wn, num_nhops * sizeof(struct weightened_nhop));
674 	curr_nhops += num_nhops;
675 
676 	nhg_priv = get_nhgrp(ctl, pnhops, curr_nhops, 0, perror);
677 
678 	if (pnhops != (struct weightened_nhop *)&storage[0])
679 		free(pnhops, M_TEMP);
680 
681 	if (nhg_priv == NULL)
682 		return (NULL);
683 
684 	return (nhg_priv);
685 }
686 
687 /*
688  * Merge nexthop group denoted by @gr_add with the nexthop group @gr_orig.
689  *
690  * Returns referenced nexthop group or NULL. In the latter case, @perror is
691  *  filled with an error code.
692  * Note that function does NOT care if the next nexthops already exists
693  * in the @gr_orig. As a result, they will be added, resulting in the
694  * same nexthop being present multiple times in the new group.
695  */
696 static struct nhgrp_priv *
merge_nhgrps(struct nh_control * ctl,const struct nhgrp_object * gr_orig,const struct nhgrp_object * gr_add,int * perror)697 merge_nhgrps(struct nh_control *ctl, const struct nhgrp_object *gr_orig,
698      const struct nhgrp_object *gr_add, int *perror)
699 {
700 	char storage[64];
701 	struct weightened_nhop *pnhops;
702 	struct nhgrp_priv *nhg_priv;
703 	const struct nhgrp_priv *orig_priv, *add_priv;
704 	size_t sz;
705 	int curr_nhops;
706 
707 	orig_priv = NHGRP_PRIV_CONST(gr_orig);
708 	add_priv = NHGRP_PRIV_CONST(gr_add);
709 	curr_nhops = orig_priv->nhg_nh_count;
710 
711 	*perror = 0;
712 
713 	sz = (orig_priv->nhg_nh_count + orig_priv->nhg_nh_count) *
714 		sizeof(struct weightened_nhop);
715 	/* optimize for <= 4 paths, each path=16 bytes */
716 	if (sz <= sizeof(storage))
717 		pnhops = (struct weightened_nhop *)&storage[0];
718 	else {
719 		pnhops = malloc(sz, M_TEMP, M_NOWAIT);
720 		if (pnhops == NULL) {
721 			*perror = ENOMEM;
722 			return (NULL);
723 		}
724 	}
725 
726 	/* First, copy nhops from first group */
727 	memcpy(pnhops, orig_priv->nhg_nh_weights,
728 	   orig_priv->nhg_nh_count * sizeof(struct weightened_nhop));
729 	memcpy(&pnhops[curr_nhops], add_priv->nhg_nh_weights,
730 	   add_priv->nhg_nh_count * sizeof(struct weightened_nhop));
731 	curr_nhops += add_priv->nhg_nh_count;
732 
733 	nhg_priv = get_nhgrp(ctl, pnhops, curr_nhops, 0, perror);
734 
735 	if (pnhops != (struct weightened_nhop *)&storage[0])
736 		free(pnhops, M_TEMP);
737 
738 	if (nhg_priv == NULL)
739 		return (NULL);
740 
741 	return (nhg_priv);
742 }
743 
744 
745 /*
746  * Creates/finds nexthop group based on @wn and @num_nhops.
747  * Returns 0 on success with referenced group in @rnd, or
748  * errno.
749  *
750  * If the error is EAGAIN, then the operation can be retried.
751  */
752 int
nhgrp_get_group(struct rib_head * rh,struct weightened_nhop * wn,int num_nhops,uint32_t uidx,struct nhgrp_object ** pnhg)753 nhgrp_get_group(struct rib_head *rh, struct weightened_nhop *wn, int num_nhops,
754     uint32_t uidx, struct nhgrp_object **pnhg)
755 {
756 	struct nh_control *ctl = rh->nh_control;
757 	struct nhgrp_priv *nhg_priv;
758 	int error;
759 
760 	nhg_priv = get_nhgrp(ctl, wn, num_nhops, uidx, &error);
761 	if (nhg_priv != NULL)
762 		*pnhg = nhg_priv->nhg;
763 
764 	return (error);
765 }
766 
767 /*
768  * Creates new nexthop group based on @src group without the nexthops
769  * chosen by @flt_func.
770  * Returns 0 on success, storring the reference nhop group/object in @rnd.
771  */
772 int
nhgrp_get_filtered_group(struct rib_head * rh,const struct rtentry * rt,const struct nhgrp_object * src,rib_filter_f_t flt_func,void * flt_data,struct route_nhop_data * rnd)773 nhgrp_get_filtered_group(struct rib_head *rh, const struct rtentry *rt,
774     const struct nhgrp_object *src, rib_filter_f_t flt_func, void *flt_data,
775     struct route_nhop_data *rnd)
776 {
777 	char storage[64];
778 	struct nh_control *ctl = rh->nh_control;
779 	struct weightened_nhop *pnhops;
780 	const struct nhgrp_priv *mp_priv, *src_priv;
781 	size_t sz;
782 	int error, i, num_nhops;
783 
784 	src_priv = NHGRP_PRIV_CONST(src);
785 
786 	sz = src_priv->nhg_nh_count * (sizeof(struct weightened_nhop));
787 	/* optimize for <= 4 paths, each path=16 bytes */
788 	if (sz <= sizeof(storage))
789 		pnhops = (struct weightened_nhop *)&storage[0];
790 	else {
791 		if ((pnhops = malloc(sz, M_TEMP, M_NOWAIT)) == NULL)
792 			return (ENOMEM);
793 	}
794 
795 	/* Filter nexthops */
796 	error = 0;
797 	num_nhops = 0;
798 	for (i = 0; i < src_priv->nhg_nh_count; i++) {
799 		if (flt_func(rt, src_priv->nhg_nh_weights[i].nh, flt_data))
800 			continue;
801 		memcpy(&pnhops[num_nhops++], &src_priv->nhg_nh_weights[i],
802 		  sizeof(struct weightened_nhop));
803 	}
804 
805 	if (num_nhops == 0) {
806 		rnd->rnd_nhgrp = NULL;
807 		rnd->rnd_weight = 0;
808 	} else if (num_nhops == 1) {
809 		rnd->rnd_nhop = pnhops[0].nh;
810 		rnd->rnd_weight = pnhops[0].weight;
811 		if (nhop_try_ref_object(rnd->rnd_nhop) == 0)
812 			error = EAGAIN;
813 	} else {
814 		mp_priv = get_nhgrp(ctl, pnhops, num_nhops, 0, &error);
815 		if (mp_priv != NULL)
816 			rnd->rnd_nhgrp = mp_priv->nhg;
817 		rnd->rnd_weight = 0;
818 	}
819 
820 	if (pnhops != (struct weightened_nhop *)&storage[0])
821 		free(pnhops, M_TEMP);
822 
823 	return (error);
824 }
825 
826 /*
827  * Creates new multipath group based on existing group/nhop in @rnd_orig and
828  *  to-be-added nhop @wn_add.
829  * Returns 0 on success and stores result in @rnd_new.
830  */
831 int
nhgrp_get_addition_group(struct rib_head * rh,struct route_nhop_data * rnd_orig,struct route_nhop_data * rnd_add,struct route_nhop_data * rnd_new)832 nhgrp_get_addition_group(struct rib_head *rh, struct route_nhop_data *rnd_orig,
833     struct route_nhop_data *rnd_add, struct route_nhop_data *rnd_new)
834 {
835 	struct nh_control *ctl = rh->nh_control;
836 	struct nhgrp_priv *nhg_priv;
837 	struct weightened_nhop wn[2] = {};
838 	int error;
839 
840 	MPASS((!NH_IS_NHGRP(rnd_add->rnd_nhop)));
841 
842 	if (rnd_orig->rnd_nhop == NULL) {
843 		/* No paths to add to, just reference current nhop */
844 		*rnd_new = *rnd_add;
845 		if (nhop_try_ref_object(rnd_new->rnd_nhop) == 0)
846 			return (EAGAIN);
847 		return (0);
848 	}
849 
850 	wn[0].nh = rnd_add->rnd_nhop;
851 	wn[0].weight = rnd_add->rnd_weight;
852 
853 	if (!NH_IS_NHGRP(rnd_orig->rnd_nhop)) {
854 		/* Simple merge of 2 non-multipath nexthops */
855 		wn[1].nh = rnd_orig->rnd_nhop;
856 		wn[1].weight = rnd_orig->rnd_weight;
857 		nhg_priv = get_nhgrp(ctl, wn, 2, 0, &error);
858 	} else {
859 		/* Get new nhop group with @rt->rt_nhop as an additional nhop */
860 		nhg_priv = append_nhops(ctl, rnd_orig->rnd_nhgrp, &wn[0], 1,
861 		    &error);
862 	}
863 
864 	if (nhg_priv == NULL)
865 		return (error);
866 	rnd_new->rnd_nhgrp = nhg_priv->nhg;
867 	rnd_new->rnd_weight = 0;
868 
869 	return (0);
870 }
871 
872 /*
873  * Creates new multipath group based on existing group/nhop in @rnd_orig and
874  *  to-be-merged nhgrp @wn_add.
875  * Returns 0 on success and stores result in @rnd_new.
876  */
877 int
nhgrp_get_merge_group(struct rib_head * rh,struct route_nhop_data * rnd_orig,struct route_nhop_data * rnd_add,struct route_nhop_data * rnd_new)878 nhgrp_get_merge_group(struct rib_head *rh, struct route_nhop_data *rnd_orig,
879     struct route_nhop_data *rnd_add, struct route_nhop_data *rnd_new)
880 {
881 	struct nh_control *ctl = rh->nh_control;
882 	struct nhgrp_priv *nhg_priv;
883 	struct weightened_nhop wn = {};
884 	int error;
885 
886 	MPASS((NH_IS_NHGRP(rnd_add->rnd_nhop)));
887 
888 	/* No paths to add to, Just give up */
889 	if (rnd_orig->rnd_nhop == NULL)
890 		return (EINVAL);
891 
892 	if (!NH_IS_NHGRP(rnd_orig->rnd_nhop)) {
893 		wn.nh = rnd_orig->rnd_nhop;
894 		wn.weight = rnd_orig->rnd_weight;
895 		/* Get new nhop group with addition of nhops in nhgrp */
896 		nhg_priv = append_nhops(ctl, rnd_add->rnd_nhgrp, &wn, 1,
897 		    &error);
898 	} else {
899 		/* Get new nhop group with addition of nhops in nhgrp */
900 		nhg_priv = merge_nhgrps(ctl, rnd_orig->rnd_nhgrp, rnd_add->rnd_nhgrp,
901 		    &error);
902 	}
903 
904 	if (nhg_priv == NULL)
905 		return (error);
906 	rnd_new->rnd_nhgrp = nhg_priv->nhg;
907 	rnd_new->rnd_weight = 0;
908 
909 	return (0);
910 }
911 
912 /*
913  * Returns pointer to array of nexthops with weights for
914  * given @nhg. Stores number of items in the array into @pnum_nhops.
915  */
916 const struct weightened_nhop *
nhgrp_get_nhops(const struct nhgrp_object * nhg,uint32_t * pnum_nhops)917 nhgrp_get_nhops(const struct nhgrp_object *nhg, uint32_t *pnum_nhops)
918 {
919 	const struct nhgrp_priv *nhg_priv;
920 
921 	KASSERT(((nhg->nhg_flags & MPF_MULTIPATH) != 0), ("nhop is not mpath"));
922 
923 	nhg_priv = NHGRP_PRIV_CONST(nhg);
924 	*pnum_nhops = nhg_priv->nhg_nh_count;
925 
926 	return (nhg_priv->nhg_nh_weights);
927 }
928 
929 void
nhgrp_set_uidx(struct nhgrp_object * nhg,uint32_t uidx)930 nhgrp_set_uidx(struct nhgrp_object *nhg, uint32_t uidx)
931 {
932 	struct nhgrp_priv *nhg_priv;
933 
934 	KASSERT(((nhg->nhg_flags & MPF_MULTIPATH) != 0), ("nhop is not mpath"));
935 
936 	nhg_priv = NHGRP_PRIV(nhg);
937 
938 	nhg_priv->nhg_uidx = uidx;
939 }
940 
941 uint32_t
nhgrp_get_uidx(const struct nhgrp_object * nhg)942 nhgrp_get_uidx(const struct nhgrp_object *nhg)
943 {
944 	const struct nhgrp_priv *nhg_priv;
945 
946 	KASSERT(((nhg->nhg_flags & MPF_MULTIPATH) != 0), ("nhop is not mpath"));
947 
948 	nhg_priv = NHGRP_PRIV_CONST(nhg);
949 	return (nhg_priv->nhg_uidx);
950 }
951 
952 /*
953  * Prints nexhop group @nhg data in the provided @buf.
954  * Example: nhg#33/sz=3:[#1:100,#2:100,#3:100]
955  * Example: nhg#33/sz=5:[#1:100,#2:100,..]
956  */
957 char *
nhgrp_print_buf(const struct nhgrp_object * nhg,char * buf,size_t bufsize)958 nhgrp_print_buf(const struct nhgrp_object *nhg, char *buf, size_t bufsize)
959 {
960 	const struct nhgrp_priv *nhg_priv = NHGRP_PRIV_CONST(nhg);
961 
962 	int off = snprintf(buf, bufsize, "nhg#%u/sz=%u:[", nhg_priv->nhg_idx,
963 	    nhg_priv->nhg_nh_count);
964 
965 	for (int i = 0; i < nhg_priv->nhg_nh_count; i++) {
966 		const struct weightened_nhop *wn = &nhg_priv->nhg_nh_weights[i];
967 		int len = snprintf(&buf[off], bufsize - off, "#%u:%u,",
968 		    wn->nh->nh_priv->nh_idx, wn->weight);
969 		if (len + off + 3 >= bufsize) {
970 			int len = snprintf(&buf[off], bufsize - off, "...");
971 			off += len;
972 			break;
973 		}
974 		off += len;
975 	}
976 	if (off > 0)
977 		off--; // remove last ","
978 	if (off + 1 < bufsize)
979 		snprintf(&buf[off], bufsize - off, "]");
980 	return buf;
981 }
982 
983 __noinline static int
dump_nhgrp_entry(struct rib_head * rh,const struct nhgrp_priv * nhg_priv,char * buffer,size_t buffer_size,struct sysctl_req * w)984 dump_nhgrp_entry(struct rib_head *rh, const struct nhgrp_priv *nhg_priv,
985     char *buffer, size_t buffer_size, struct sysctl_req *w)
986 {
987 	struct rt_msghdr *rtm;
988 	struct nhgrp_external *nhge;
989 	struct nhgrp_container *nhgc;
990 	const struct nhgrp_object *nhg;
991 	struct nhgrp_nhop_external *ext;
992 	int error;
993 	size_t sz;
994 
995 	nhg = nhg_priv->nhg;
996 
997 	sz = sizeof(struct rt_msghdr) + sizeof(struct nhgrp_external);
998 	/* controlplane nexthops */
999 	sz += sizeof(struct nhgrp_container);
1000 	sz += sizeof(struct nhgrp_nhop_external) * nhg_priv->nhg_nh_count;
1001 	/* dataplane nexthops */
1002 	sz += sizeof(struct nhgrp_container);
1003 	sz += sizeof(struct nhgrp_nhop_external) * nhg->nhg_size;
1004 
1005 	KASSERT(sz <= buffer_size, ("increase nhgrp buffer size"));
1006 
1007 	bzero(buffer, sz);
1008 
1009 	rtm = (struct rt_msghdr *)buffer;
1010 	rtm->rtm_msglen = sz;
1011 	rtm->rtm_version = RTM_VERSION;
1012 	rtm->rtm_type = RTM_GET;
1013 
1014 	nhge = (struct nhgrp_external *)(rtm + 1);
1015 
1016 	nhge->nhg_idx = nhg_priv->nhg_idx;
1017 	nhge->nhg_refcount = nhg_priv->nhg_refcount;
1018 
1019 	/* fill in control plane nexthops firs */
1020 	nhgc = (struct nhgrp_container *)(nhge + 1);
1021 	nhgc->nhgc_type = NHG_C_TYPE_CNHOPS;
1022 	nhgc->nhgc_subtype = 0;
1023 	nhgc->nhgc_len = sizeof(struct nhgrp_container);
1024 	nhgc->nhgc_len += sizeof(struct nhgrp_nhop_external) * nhg_priv->nhg_nh_count;
1025 	nhgc->nhgc_count = nhg_priv->nhg_nh_count;
1026 
1027 	ext = (struct nhgrp_nhop_external *)(nhgc + 1);
1028 	for (int i = 0; i < nhg_priv->nhg_nh_count; i++) {
1029 		ext[i].nh_idx = nhg_priv->nhg_nh_weights[i].nh->nh_priv->nh_idx;
1030 		ext[i].nh_weight = nhg_priv->nhg_nh_weights[i].weight;
1031 	}
1032 
1033 	/* fill in dataplane nexthops */
1034 	nhgc = (struct nhgrp_container *)(&ext[nhg_priv->nhg_nh_count]);
1035 	nhgc->nhgc_type = NHG_C_TYPE_DNHOPS;
1036 	nhgc->nhgc_subtype = 0;
1037 	nhgc->nhgc_len = sizeof(struct nhgrp_container);
1038 	nhgc->nhgc_len += sizeof(struct nhgrp_nhop_external) * nhg->nhg_size;
1039 	nhgc->nhgc_count = nhg->nhg_size;
1040 
1041 	ext = (struct nhgrp_nhop_external *)(nhgc + 1);
1042 	for (int i = 0; i < nhg->nhg_size; i++) {
1043 		ext[i].nh_idx = nhg->nhops[i]->nh_priv->nh_idx;
1044 		ext[i].nh_weight = 0;
1045 	}
1046 
1047 	error = SYSCTL_OUT(w, buffer, sz);
1048 
1049 	return (error);
1050 }
1051 
1052 uint32_t
nhgrp_get_idx(const struct nhgrp_object * nhg)1053 nhgrp_get_idx(const struct nhgrp_object *nhg)
1054 {
1055 	const struct nhgrp_priv *nhg_priv;
1056 
1057 	nhg_priv = NHGRP_PRIV_CONST(nhg);
1058 	return (nhg_priv->nhg_idx);
1059 }
1060 
1061 uint8_t
nhgrp_get_origin(const struct nhgrp_object * nhg)1062 nhgrp_get_origin(const struct nhgrp_object *nhg)
1063 {
1064 	return (NHGRP_PRIV_CONST(nhg)->nhg_origin);
1065 }
1066 
1067 void
nhgrp_set_origin(struct nhgrp_object * nhg,uint8_t origin)1068 nhgrp_set_origin(struct nhgrp_object *nhg, uint8_t origin)
1069 {
1070 	NHGRP_PRIV(nhg)->nhg_origin = origin;
1071 }
1072 
1073 uint32_t
nhgrp_get_count(struct rib_head * rh)1074 nhgrp_get_count(struct rib_head *rh)
1075 {
1076 	struct nh_control *ctl;
1077 	uint32_t count;
1078 
1079 	ctl = rh->nh_control;
1080 
1081 	NHOPS_RLOCK(ctl);
1082 	count = ctl->gr_head.items_count;
1083 	NHOPS_RUNLOCK(ctl);
1084 
1085 	return (count);
1086 }
1087 
1088 /*
1089  * Recompile nexthop group without changing the slots.
1090  * Since a nexthop might become reachable again soon,
1091  * this avoids unnecessary memory allocation.
1092  */
1093 static void
nhgrp_recompile_one(struct nhgrp_priv * nhg_priv)1094 nhgrp_recompile_one(struct nhgrp_priv *nhg_priv)
1095 {
1096 	struct nhgrp_object *nhg = nhg_priv->nhg;
1097 	const struct weightened_nhop *wn;
1098 	uint32_t num_nhops, min_metric;
1099 
1100 	wn = nhgrp_get_nhops(nhg, &num_nhops);
1101 
1102 	/* dataplane only has the lowest metric nhops, just pick one */
1103 	min_metric = nhop_get_metric(nhg->nhops[0]);
1104 	compile_nhgrp(nhg_priv, wn, nhg->nhg_size, min_metric);
1105 }
1106 
1107 void
nhgrp_recompile(struct rib_head * rh)1108 nhgrp_recompile(struct rib_head *rh)
1109 {
1110 	struct nh_control *ctl = rh->nh_control;
1111 	struct nhgrp_priv *nhg_priv;
1112 
1113 	NHOPS_WLOCK_ASSERT(ctl);
1114 
1115 	if (ctl->gr_head.items_count == 0)
1116 		return;
1117 
1118 	CHT_SLIST_FOREACH(&ctl->gr_head, mpath, nhg_priv) {
1119 		nhgrp_recompile_one(nhg_priv);
1120 	} CHT_SLIST_FOREACH_END;
1121 }
1122 
1123 int
nhgrp_dump_sysctl(struct rib_head * rh,struct sysctl_req * w)1124 nhgrp_dump_sysctl(struct rib_head *rh, struct sysctl_req *w)
1125 {
1126 	struct nh_control *ctl = rh->nh_control;
1127 	struct epoch_tracker et;
1128 	struct nhgrp_priv *nhg_priv;
1129 	char *buffer;
1130 	size_t sz;
1131 	int error = 0;
1132 
1133 	if (ctl->gr_head.items_count == 0)
1134 		return (0);
1135 
1136 	/* Calculate the maximum nhop group size in bytes */
1137 	sz = sizeof(struct rt_msghdr) + sizeof(struct nhgrp_external);
1138 	sz += 2 * sizeof(struct nhgrp_container);
1139 	sz += 2 * sizeof(struct nhgrp_nhop_external) * RIB_MAX_MPATH_WIDTH;
1140 	buffer = malloc(sz, M_TEMP, M_NOWAIT);
1141 	if (buffer == NULL)
1142 		return (ENOMEM);
1143 
1144 	NET_EPOCH_ENTER(et);
1145 	NHOPS_RLOCK(ctl);
1146 	CHT_SLIST_FOREACH(&ctl->gr_head, mpath, nhg_priv) {
1147 		error = dump_nhgrp_entry(rh, nhg_priv, buffer, sz, w);
1148 		if (error != 0)
1149 			break;
1150 	} CHT_SLIST_FOREACH_END;
1151 	NHOPS_RUNLOCK(ctl);
1152 	NET_EPOCH_EXIT(et);
1153 
1154 	free(buffer, M_TEMP);
1155 
1156 	return (error);
1157 }
1158