1a6663252SAlexander V. Chernikov /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3a6663252SAlexander V. Chernikov *
4a6663252SAlexander V. Chernikov * Copyright (c) 2020 Alexander V. Chernikov
5a6663252SAlexander V. Chernikov *
6a6663252SAlexander V. Chernikov * Redistribution and use in source and binary forms, with or without
7a6663252SAlexander V. Chernikov * modification, are permitted provided that the following conditions
8a6663252SAlexander V. Chernikov * are met:
9a6663252SAlexander V. Chernikov * 1. Redistributions of source code must retain the above copyright
10a6663252SAlexander V. Chernikov * notice, this list of conditions and the following disclaimer.
11a6663252SAlexander V. Chernikov * 2. Redistributions in binary form must reproduce the above copyright
12a6663252SAlexander V. Chernikov * notice, this list of conditions and the following disclaimer in the
13a6663252SAlexander V. Chernikov * documentation and/or other materials provided with the distribution.
14a6663252SAlexander V. Chernikov *
15a6663252SAlexander V. Chernikov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16a6663252SAlexander V. Chernikov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a6663252SAlexander V. Chernikov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a6663252SAlexander V. Chernikov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19a6663252SAlexander V. Chernikov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a6663252SAlexander V. Chernikov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21a6663252SAlexander V. Chernikov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a6663252SAlexander V. Chernikov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23a6663252SAlexander V. Chernikov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24a6663252SAlexander V. Chernikov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25a6663252SAlexander V. Chernikov * SUCH DAMAGE.
26a6663252SAlexander V. Chernikov */
27a6663252SAlexander V. Chernikov
28a6663252SAlexander V. Chernikov #include <sys/cdefs.h>
29a6663252SAlexander V. Chernikov #include "opt_inet.h"
30a6663252SAlexander V. Chernikov #include "opt_route.h"
31a6663252SAlexander V. Chernikov
32a6663252SAlexander V. Chernikov #include <sys/param.h>
33a6663252SAlexander V. Chernikov #include <sys/systm.h>
34a6663252SAlexander V. Chernikov #include <sys/lock.h>
35a6663252SAlexander V. Chernikov #include <sys/rwlock.h>
36a6663252SAlexander V. Chernikov #include <sys/malloc.h>
37a6663252SAlexander V. Chernikov #include <sys/mbuf.h>
38a6663252SAlexander V. Chernikov #include <sys/socket.h>
39a6663252SAlexander V. Chernikov #include <sys/kernel.h>
40a6663252SAlexander V. Chernikov
41a6663252SAlexander V. Chernikov #include <net/if.h>
42a6663252SAlexander V. Chernikov #include <net/if_var.h>
43a6663252SAlexander V. Chernikov #include <net/route.h>
44e7d8af4fSAlexander V. Chernikov #include <net/route/route_var.h>
45a6663252SAlexander V. Chernikov #include <net/route/nhop_utils.h>
46a6663252SAlexander V. Chernikov #include <net/route/nhop.h>
47a6663252SAlexander V. Chernikov #include <net/route/nhop_var.h>
48a6663252SAlexander V. Chernikov #include <net/vnet.h>
49a6663252SAlexander V. Chernikov
505c23343bSAlexander V. Chernikov #define DEBUG_MOD_NAME nhop
515c23343bSAlexander V. Chernikov #define DEBUG_MAX_LEVEL LOG_DEBUG
525c23343bSAlexander V. Chernikov #include <net/route/route_debug.h>
535c23343bSAlexander V. Chernikov _DECLARE_DEBUG(LOG_INFO);
545c23343bSAlexander V. Chernikov
55a6663252SAlexander V. Chernikov /*
56a6663252SAlexander V. Chernikov * This file contains data structures management logic for the nexthop ("nhop")
57a6663252SAlexander V. Chernikov * route subsystem.
58a6663252SAlexander V. Chernikov *
59a6663252SAlexander V. Chernikov * Nexthops in the original sense are the objects containing all the necessary
60a6663252SAlexander V. Chernikov * information to forward the packet to the selected destination.
61a6663252SAlexander V. Chernikov * In particular, nexthop is defined by a combination of
62a6663252SAlexander V. Chernikov * ifp, ifa, aifp, mtu, gw addr(if set), nh_type, nh_family, mask of rt_flags and
63a6663252SAlexander V. Chernikov * NHF_DEFAULT
64a6663252SAlexander V. Chernikov *
65a6663252SAlexander V. Chernikov * All nexthops are stored in the resizable hash table.
66a6663252SAlexander V. Chernikov * Additionally, each nexthop gets assigned its unique index (nexthop index)
67a6663252SAlexander V. Chernikov * so userland programs can interact with the nexthops easier. Index allocation
68a6663252SAlexander V. Chernikov * is backed by the bitmask array.
69a6663252SAlexander V. Chernikov */
70a6663252SAlexander V. Chernikov
71fedeb08bSAlexander V. Chernikov MALLOC_DEFINE(M_NHOP, "nhops", "nexthops data");
72a6663252SAlexander V. Chernikov
73a6663252SAlexander V. Chernikov /* Hash management functions */
74a6663252SAlexander V. Chernikov
75a6663252SAlexander V. Chernikov int
nhops_init_rib(struct rib_head * rh)76a6663252SAlexander V. Chernikov nhops_init_rib(struct rib_head *rh)
77a6663252SAlexander V. Chernikov {
78a6663252SAlexander V. Chernikov struct nh_control *ctl;
79a6663252SAlexander V. Chernikov size_t alloc_size;
80a6663252SAlexander V. Chernikov uint32_t num_buckets, num_items;
81a6663252SAlexander V. Chernikov void *ptr;
82a6663252SAlexander V. Chernikov
83a6663252SAlexander V. Chernikov ctl = malloc(sizeof(struct nh_control), M_NHOP, M_WAITOK | M_ZERO);
84a6663252SAlexander V. Chernikov
85a6663252SAlexander V. Chernikov /*
86a6663252SAlexander V. Chernikov * Allocate nexthop hash. Start with 16 items by default (128 bytes).
87a6663252SAlexander V. Chernikov * This will be enough for most of the cases.
88a6663252SAlexander V. Chernikov */
89a6663252SAlexander V. Chernikov num_buckets = 16;
90a6663252SAlexander V. Chernikov alloc_size = CHT_SLIST_GET_RESIZE_SIZE(num_buckets);
91a6663252SAlexander V. Chernikov ptr = malloc(alloc_size, M_NHOP, M_WAITOK | M_ZERO);
92a6663252SAlexander V. Chernikov CHT_SLIST_INIT(&ctl->nh_head, ptr, num_buckets);
93a6663252SAlexander V. Chernikov
94a6663252SAlexander V. Chernikov /*
95a6663252SAlexander V. Chernikov * Allocate nexthop index bitmask.
96a6663252SAlexander V. Chernikov */
97a6663252SAlexander V. Chernikov num_items = 128 * 8; /* 128 bytes */
98a6663252SAlexander V. Chernikov ptr = malloc(bitmask_get_size(num_items), M_NHOP, M_WAITOK | M_ZERO);
99a6663252SAlexander V. Chernikov bitmask_init(&ctl->nh_idx_head, ptr, num_items);
100a6663252SAlexander V. Chernikov
101a6663252SAlexander V. Chernikov NHOPS_LOCK_INIT(ctl);
102a6663252SAlexander V. Chernikov
103a6663252SAlexander V. Chernikov rh->nh_control = ctl;
104a6663252SAlexander V. Chernikov ctl->ctl_rh = rh;
105a6663252SAlexander V. Chernikov
1065c23343bSAlexander V. Chernikov FIB_CTL_LOG(LOG_DEBUG2, ctl, "nhops init: ctl %p rh %p", ctl, rh);
107a6663252SAlexander V. Chernikov
108a6663252SAlexander V. Chernikov return (0);
109a6663252SAlexander V. Chernikov }
110a6663252SAlexander V. Chernikov
111a6663252SAlexander V. Chernikov static void
destroy_ctl(struct nh_control * ctl)112a6663252SAlexander V. Chernikov destroy_ctl(struct nh_control *ctl)
113a6663252SAlexander V. Chernikov {
114a6663252SAlexander V. Chernikov
115a6663252SAlexander V. Chernikov NHOPS_LOCK_DESTROY(ctl);
116a6663252SAlexander V. Chernikov free(ctl->nh_head.ptr, M_NHOP);
117a6663252SAlexander V. Chernikov free(ctl->nh_idx_head.idx, M_NHOP);
118fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
119fedeb08bSAlexander V. Chernikov nhgrp_ctl_free(ctl);
120fedeb08bSAlexander V. Chernikov #endif
121a6663252SAlexander V. Chernikov free(ctl, M_NHOP);
122a6663252SAlexander V. Chernikov }
123a6663252SAlexander V. Chernikov
124a6663252SAlexander V. Chernikov /*
125a6663252SAlexander V. Chernikov * Epoch callback indicating ctl is safe to destroy
126a6663252SAlexander V. Chernikov */
127a6663252SAlexander V. Chernikov static void
destroy_ctl_epoch(epoch_context_t ctx)128a6663252SAlexander V. Chernikov destroy_ctl_epoch(epoch_context_t ctx)
129a6663252SAlexander V. Chernikov {
130a6663252SAlexander V. Chernikov struct nh_control *ctl;
131a6663252SAlexander V. Chernikov
132a6663252SAlexander V. Chernikov ctl = __containerof(ctx, struct nh_control, ctl_epoch_ctx);
133a6663252SAlexander V. Chernikov
134a6663252SAlexander V. Chernikov destroy_ctl(ctl);
135a6663252SAlexander V. Chernikov }
136a6663252SAlexander V. Chernikov
137a6663252SAlexander V. Chernikov void
nhops_destroy_rib(struct rib_head * rh)138a6663252SAlexander V. Chernikov nhops_destroy_rib(struct rib_head *rh)
139a6663252SAlexander V. Chernikov {
140a6663252SAlexander V. Chernikov struct nh_control *ctl;
141a6663252SAlexander V. Chernikov struct nhop_priv *nh_priv;
142a6663252SAlexander V. Chernikov
143a6663252SAlexander V. Chernikov ctl = rh->nh_control;
144a6663252SAlexander V. Chernikov
145a6663252SAlexander V. Chernikov /*
146a6663252SAlexander V. Chernikov * All routes should have been deleted in rt_table_destroy().
147a6663252SAlexander V. Chernikov * However, TCP stack or other consumers may store referenced
148a6663252SAlexander V. Chernikov * nexthop pointers. When these references go to zero,
149a6663252SAlexander V. Chernikov * nhop_free() will try to unlink these records from the
150a6663252SAlexander V. Chernikov * datastructures, most likely leading to panic.
151a6663252SAlexander V. Chernikov *
152a6663252SAlexander V. Chernikov * Avoid that by explicitly marking all of the remaining
153a6663252SAlexander V. Chernikov * nexthops as unlinked by removing a reference from a special
154a6663252SAlexander V. Chernikov * counter. Please see nhop_free() comments for more
155a6663252SAlexander V. Chernikov * details.
156a6663252SAlexander V. Chernikov */
157a6663252SAlexander V. Chernikov
158a6663252SAlexander V. Chernikov NHOPS_WLOCK(ctl);
159a6663252SAlexander V. Chernikov CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
1605c23343bSAlexander V. Chernikov FIB_RH_LOG(LOG_DEBUG3, rh, "marking nhop %u unlinked", nh_priv->nh_idx);
161a6663252SAlexander V. Chernikov refcount_release(&nh_priv->nh_linked);
162a6663252SAlexander V. Chernikov } CHT_SLIST_FOREACH_END;
163fedeb08bSAlexander V. Chernikov #ifdef ROUTE_MPATH
164fedeb08bSAlexander V. Chernikov nhgrp_ctl_unlink_all(ctl);
165fedeb08bSAlexander V. Chernikov #endif
166a6663252SAlexander V. Chernikov NHOPS_WUNLOCK(ctl);
167a6663252SAlexander V. Chernikov
168a6663252SAlexander V. Chernikov /*
169a6663252SAlexander V. Chernikov * Postpone destruction till the end of current epoch
170a6663252SAlexander V. Chernikov * so nhop_free() can safely use nh_control pointer.
171a6663252SAlexander V. Chernikov */
17273336a6fSZhenlei Huang NET_EPOCH_CALL(destroy_ctl_epoch, &ctl->ctl_epoch_ctx);
173a6663252SAlexander V. Chernikov }
174a6663252SAlexander V. Chernikov
175a6663252SAlexander V. Chernikov /*
176a6663252SAlexander V. Chernikov * Nexhop hash calculation:
177a6663252SAlexander V. Chernikov *
178a6663252SAlexander V. Chernikov * Nexthops distribution:
179a6663252SAlexander V. Chernikov * 2 "mandatory" nexthops per interface ("interface route", "loopback").
180a6663252SAlexander V. Chernikov * For direct peering: 1 nexthop for the peering router per ifp/af.
181a6663252SAlexander V. Chernikov * For Ix-like peering: tens to hundreds nexthops of neghbors per ifp/af.
182a6663252SAlexander V. Chernikov * IGP control plane & broadcast segment: tens of nexthops per ifp/af.
183a6663252SAlexander V. Chernikov *
184a6663252SAlexander V. Chernikov * Each fib/af combination has its own hash table.
185a6663252SAlexander V. Chernikov * With that in mind, hash nexthops by the combination of the interface
186a6663252SAlexander V. Chernikov * and GW IP address.
187a6663252SAlexander V. Chernikov *
188ad2a0aecSGleb Smirnoff * To optimize hash calculation, ignore lower bits of ifnet pointer,
189ad2a0aecSGleb Smirnoff * as they give very little entropy.
190a6663252SAlexander V. Chernikov * Similarly, use lower 4 bytes of IPv6 address to distinguish between the
191a6663252SAlexander V. Chernikov * neighbors.
192a6663252SAlexander V. Chernikov */
193a6663252SAlexander V. Chernikov struct _hash_data {
194ad2a0aecSGleb Smirnoff uint16_t ifentropy;
195a6663252SAlexander V. Chernikov uint8_t family;
196a6663252SAlexander V. Chernikov uint8_t nh_type;
197a6663252SAlexander V. Chernikov uint32_t gw_addr;
198a6663252SAlexander V. Chernikov };
199a6663252SAlexander V. Chernikov
200a6663252SAlexander V. Chernikov static unsigned
djb_hash(const unsigned char * h,const int len)201a6663252SAlexander V. Chernikov djb_hash(const unsigned char *h, const int len)
202a6663252SAlexander V. Chernikov {
203a6663252SAlexander V. Chernikov unsigned int result = 0;
204a6663252SAlexander V. Chernikov int i;
205a6663252SAlexander V. Chernikov
206a6663252SAlexander V. Chernikov for (i = 0; i < len; i++)
207a6663252SAlexander V. Chernikov result = 33 * result ^ h[i];
208a6663252SAlexander V. Chernikov
209a6663252SAlexander V. Chernikov return (result);
210a6663252SAlexander V. Chernikov }
211a6663252SAlexander V. Chernikov
212a6663252SAlexander V. Chernikov static uint32_t
hash_priv(const struct nhop_priv * priv)213a6663252SAlexander V. Chernikov hash_priv(const struct nhop_priv *priv)
214a6663252SAlexander V. Chernikov {
215ad2a0aecSGleb Smirnoff struct nhop_object *nh = priv->nh;
216ad2a0aecSGleb Smirnoff struct _hash_data key = {
217ad2a0aecSGleb Smirnoff .ifentropy = (uint16_t)((((uintptr_t)nh->nh_ifp) >> 6) & 0xFFFF),
218ad2a0aecSGleb Smirnoff .family = nh->gw_sa.sa_family,
219ad2a0aecSGleb Smirnoff .nh_type = priv->nh_type & 0xFF,
220ad2a0aecSGleb Smirnoff .gw_addr = (nh->gw_sa.sa_family == AF_INET6) ?
221ad2a0aecSGleb Smirnoff nh->gw6_sa.sin6_addr.s6_addr32[3] :
222ad2a0aecSGleb Smirnoff nh->gw4_sa.sin_addr.s_addr
223ad2a0aecSGleb Smirnoff };
224a6663252SAlexander V. Chernikov
225a6663252SAlexander V. Chernikov return (uint32_t)(djb_hash((const unsigned char *)&key, sizeof(key)));
226a6663252SAlexander V. Chernikov }
227a6663252SAlexander V. Chernikov
228a6663252SAlexander V. Chernikov /*
229a6663252SAlexander V. Chernikov * Checks if hash needs resizing and performs this resize if necessary
230a6663252SAlexander V. Chernikov *
231a6663252SAlexander V. Chernikov */
232a6663252SAlexander V. Chernikov static void
consider_resize(struct nh_control * ctl,uint32_t new_nh_buckets,uint32_t new_idx_items)233a6663252SAlexander V. Chernikov consider_resize(struct nh_control *ctl, uint32_t new_nh_buckets, uint32_t new_idx_items)
234a6663252SAlexander V. Chernikov {
235a6663252SAlexander V. Chernikov void *nh_ptr, *nh_idx_ptr;
236a6663252SAlexander V. Chernikov void *old_idx_ptr;
237a6663252SAlexander V. Chernikov size_t alloc_size;
238a6663252SAlexander V. Chernikov
239a6663252SAlexander V. Chernikov nh_ptr = NULL;
240a6663252SAlexander V. Chernikov if (new_nh_buckets != 0) {
241a6663252SAlexander V. Chernikov alloc_size = CHT_SLIST_GET_RESIZE_SIZE(new_nh_buckets);
242a6663252SAlexander V. Chernikov nh_ptr = malloc(alloc_size, M_NHOP, M_NOWAIT | M_ZERO);
243a6663252SAlexander V. Chernikov }
244a6663252SAlexander V. Chernikov
245a6663252SAlexander V. Chernikov nh_idx_ptr = NULL;
246a6663252SAlexander V. Chernikov if (new_idx_items != 0) {
247a6663252SAlexander V. Chernikov alloc_size = bitmask_get_size(new_idx_items);
248a6663252SAlexander V. Chernikov nh_idx_ptr = malloc(alloc_size, M_NHOP, M_NOWAIT | M_ZERO);
249a6663252SAlexander V. Chernikov }
250a6663252SAlexander V. Chernikov
251a6663252SAlexander V. Chernikov if (nh_ptr == NULL && nh_idx_ptr == NULL) {
252a6663252SAlexander V. Chernikov /* Either resize is not required or allocations have failed. */
253a6663252SAlexander V. Chernikov return;
254a6663252SAlexander V. Chernikov }
255a6663252SAlexander V. Chernikov
2565c23343bSAlexander V. Chernikov FIB_CTL_LOG(LOG_DEBUG, ctl,
2575c23343bSAlexander V. Chernikov "going to resize: nh:[ptr:%p sz:%u] idx:[ptr:%p sz:%u]",
2585c23343bSAlexander V. Chernikov nh_ptr, new_nh_buckets, nh_idx_ptr, new_idx_items);
259a6663252SAlexander V. Chernikov
260a6663252SAlexander V. Chernikov old_idx_ptr = NULL;
261a6663252SAlexander V. Chernikov
262a6663252SAlexander V. Chernikov NHOPS_WLOCK(ctl);
263a6663252SAlexander V. Chernikov if (nh_ptr != NULL) {
264a6663252SAlexander V. Chernikov CHT_SLIST_RESIZE(&ctl->nh_head, nhops, nh_ptr, new_nh_buckets);
265a6663252SAlexander V. Chernikov }
266a6663252SAlexander V. Chernikov if (nh_idx_ptr != NULL) {
267a6663252SAlexander V. Chernikov if (bitmask_copy(&ctl->nh_idx_head, nh_idx_ptr, new_idx_items) == 0)
268a6663252SAlexander V. Chernikov bitmask_swap(&ctl->nh_idx_head, nh_idx_ptr, new_idx_items, &old_idx_ptr);
269a6663252SAlexander V. Chernikov }
270a6663252SAlexander V. Chernikov NHOPS_WUNLOCK(ctl);
271a6663252SAlexander V. Chernikov
272a6663252SAlexander V. Chernikov if (nh_ptr != NULL)
273a6663252SAlexander V. Chernikov free(nh_ptr, M_NHOP);
274a6663252SAlexander V. Chernikov if (old_idx_ptr != NULL)
275a6663252SAlexander V. Chernikov free(old_idx_ptr, M_NHOP);
276a6663252SAlexander V. Chernikov }
277a6663252SAlexander V. Chernikov
278a6663252SAlexander V. Chernikov /*
279a6663252SAlexander V. Chernikov * Links nextop @nh_priv to the nexhop hash table and allocates
280a6663252SAlexander V. Chernikov * nexhop index.
281a6663252SAlexander V. Chernikov * Returns allocated index or 0 on failure.
282a6663252SAlexander V. Chernikov */
283a6663252SAlexander V. Chernikov int
link_nhop(struct nh_control * ctl,struct nhop_priv * nh_priv)284a6663252SAlexander V. Chernikov link_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv)
285a6663252SAlexander V. Chernikov {
286a6663252SAlexander V. Chernikov uint16_t idx;
287a6663252SAlexander V. Chernikov uint32_t num_buckets_new, num_items_new;
288a6663252SAlexander V. Chernikov
289a6663252SAlexander V. Chernikov KASSERT((nh_priv->nh_idx == 0), ("nhop index is already allocated"));
290a6663252SAlexander V. Chernikov NHOPS_WLOCK(ctl);
291a6663252SAlexander V. Chernikov
292a6663252SAlexander V. Chernikov /*
293a6663252SAlexander V. Chernikov * Check if we need to resize hash and index.
294a6663252SAlexander V. Chernikov * The following 2 functions returns either new size or 0
295a6663252SAlexander V. Chernikov * if resize is not required.
296a6663252SAlexander V. Chernikov */
297a6663252SAlexander V. Chernikov num_buckets_new = CHT_SLIST_GET_RESIZE_BUCKETS(&ctl->nh_head);
298a6663252SAlexander V. Chernikov num_items_new = bitmask_get_resize_items(&ctl->nh_idx_head);
299a6663252SAlexander V. Chernikov
300a6663252SAlexander V. Chernikov if (bitmask_alloc_idx(&ctl->nh_idx_head, &idx) != 0) {
301a6663252SAlexander V. Chernikov NHOPS_WUNLOCK(ctl);
3025c23343bSAlexander V. Chernikov FIB_CTL_LOG(LOG_INFO, ctl, "Unable to allocate nhop index");
303a6663252SAlexander V. Chernikov RTSTAT_INC(rts_nh_idx_alloc_failure);
304a6663252SAlexander V. Chernikov consider_resize(ctl, num_buckets_new, num_items_new);
305a6663252SAlexander V. Chernikov return (0);
306a6663252SAlexander V. Chernikov }
307a6663252SAlexander V. Chernikov
308a6663252SAlexander V. Chernikov nh_priv->nh_idx = idx;
309a6663252SAlexander V. Chernikov nh_priv->nh_control = ctl;
310800c6846SAlexander V. Chernikov nh_priv->nh_finalized = 1;
311a6663252SAlexander V. Chernikov
312a6663252SAlexander V. Chernikov CHT_SLIST_INSERT_HEAD(&ctl->nh_head, nhops, nh_priv);
313a6663252SAlexander V. Chernikov
314a6663252SAlexander V. Chernikov NHOPS_WUNLOCK(ctl);
315a6663252SAlexander V. Chernikov
3165c23343bSAlexander V. Chernikov FIB_RH_LOG(LOG_DEBUG2, ctl->ctl_rh,
3175c23343bSAlexander V. Chernikov "Linked nhop priv %p to %d, hash %u, ctl %p",
3185c23343bSAlexander V. Chernikov nh_priv, idx, hash_priv(nh_priv), ctl);
319a6663252SAlexander V. Chernikov consider_resize(ctl, num_buckets_new, num_items_new);
320a6663252SAlexander V. Chernikov
321a6663252SAlexander V. Chernikov return (idx);
322a6663252SAlexander V. Chernikov }
323a6663252SAlexander V. Chernikov
324a6663252SAlexander V. Chernikov /*
325a6663252SAlexander V. Chernikov * Unlinks nexthop specified by @nh_priv data from the hash.
326a6663252SAlexander V. Chernikov *
327a6663252SAlexander V. Chernikov * Returns found nexthop or NULL.
328a6663252SAlexander V. Chernikov */
329a6663252SAlexander V. Chernikov struct nhop_priv *
unlink_nhop(struct nh_control * ctl,struct nhop_priv * nh_priv_del)330a6663252SAlexander V. Chernikov unlink_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv_del)
331a6663252SAlexander V. Chernikov {
332a6663252SAlexander V. Chernikov struct nhop_priv *priv_ret;
333a6663252SAlexander V. Chernikov int idx;
334a6663252SAlexander V. Chernikov uint32_t num_buckets_new, num_items_new;
335a6663252SAlexander V. Chernikov
336a6663252SAlexander V. Chernikov idx = 0;
337a6663252SAlexander V. Chernikov
338a6663252SAlexander V. Chernikov NHOPS_WLOCK(ctl);
339054948bdSAlexander V. Chernikov CHT_SLIST_REMOVE(&ctl->nh_head, nhops, nh_priv_del, priv_ret);
340a6663252SAlexander V. Chernikov
341a6663252SAlexander V. Chernikov if (priv_ret != NULL) {
342a6663252SAlexander V. Chernikov idx = priv_ret->nh_idx;
343a6663252SAlexander V. Chernikov priv_ret->nh_idx = 0;
344a6663252SAlexander V. Chernikov
345a6663252SAlexander V. Chernikov KASSERT((idx != 0), ("bogus nhop index 0"));
346a6663252SAlexander V. Chernikov if ((bitmask_free_idx(&ctl->nh_idx_head, idx)) != 0) {
3475c23343bSAlexander V. Chernikov FIB_CTL_LOG(LOG_DEBUG, ctl,
3485c23343bSAlexander V. Chernikov "Unable to remove index %d from fib %u af %d",
3495c23343bSAlexander V. Chernikov idx, ctl->ctl_rh->rib_fibnum, ctl->ctl_rh->rib_family);
350a6663252SAlexander V. Chernikov }
351a6663252SAlexander V. Chernikov }
352a6663252SAlexander V. Chernikov
353a6663252SAlexander V. Chernikov /* Check if hash or index needs to be resized */
354a6663252SAlexander V. Chernikov num_buckets_new = CHT_SLIST_GET_RESIZE_BUCKETS(&ctl->nh_head);
355a6663252SAlexander V. Chernikov num_items_new = bitmask_get_resize_items(&ctl->nh_idx_head);
356a6663252SAlexander V. Chernikov
357a6663252SAlexander V. Chernikov NHOPS_WUNLOCK(ctl);
3585c23343bSAlexander V. Chernikov
3595c23343bSAlexander V. Chernikov if (priv_ret == NULL) {
3605c23343bSAlexander V. Chernikov FIB_CTL_LOG(LOG_INFO, ctl,
3615c23343bSAlexander V. Chernikov "Unable to unlink nhop priv %p from hash, hash %u ctl %p",
3625c23343bSAlexander V. Chernikov nh_priv_del, hash_priv(nh_priv_del), ctl);
3635c23343bSAlexander V. Chernikov } else {
3645c23343bSAlexander V. Chernikov FIB_CTL_LOG(LOG_DEBUG2, ctl, "Unlinked nhop %p priv idx %d",
3655c23343bSAlexander V. Chernikov priv_ret, idx);
3665c23343bSAlexander V. Chernikov }
367a6663252SAlexander V. Chernikov
368a6663252SAlexander V. Chernikov consider_resize(ctl, num_buckets_new, num_items_new);
369a6663252SAlexander V. Chernikov
370a6663252SAlexander V. Chernikov return (priv_ret);
371a6663252SAlexander V. Chernikov }
372a6663252SAlexander V. Chernikov
373a6663252SAlexander V. Chernikov /*
374a6663252SAlexander V. Chernikov * Searches for the nexthop by data specifcied in @nh_priv.
375a6663252SAlexander V. Chernikov * Returns referenced nexthop or NULL.
376a6663252SAlexander V. Chernikov */
377a6663252SAlexander V. Chernikov struct nhop_priv *
find_nhop(struct nh_control * ctl,const struct nhop_priv * nh_priv)378a6663252SAlexander V. Chernikov find_nhop(struct nh_control *ctl, const struct nhop_priv *nh_priv)
379a6663252SAlexander V. Chernikov {
380a6663252SAlexander V. Chernikov struct nhop_priv *nh_priv_ret;
381a6663252SAlexander V. Chernikov
382a6663252SAlexander V. Chernikov NHOPS_RLOCK(ctl);
383a6663252SAlexander V. Chernikov CHT_SLIST_FIND_BYOBJ(&ctl->nh_head, nhops, nh_priv, nh_priv_ret);
384a6663252SAlexander V. Chernikov if (nh_priv_ret != NULL) {
385a6663252SAlexander V. Chernikov if (refcount_acquire_if_not_zero(&nh_priv_ret->nh_refcnt) == 0){
386a6663252SAlexander V. Chernikov /* refcount was 0 -> nhop is being deleted */
387a6663252SAlexander V. Chernikov nh_priv_ret = NULL;
388a6663252SAlexander V. Chernikov }
389a6663252SAlexander V. Chernikov }
390a6663252SAlexander V. Chernikov NHOPS_RUNLOCK(ctl);
391a6663252SAlexander V. Chernikov
392a6663252SAlexander V. Chernikov return (nh_priv_ret);
393a6663252SAlexander V. Chernikov }
394