2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_route.h"
32 #include "opt_mpath.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/kernel.h>
41 #include <net/route/nhop_utils.h>
43 #define BLOCK_ITEMS (8 * sizeof(u_long)) /* Number of items for ffsl() */
45 #define _BLOCKS_TO_SZ(_blocks) ((size_t)(_blocks) * sizeof(u_long))
46 #define _BLOCKS_TO_ITEMS(_blocks) ((uint32_t)(_blocks) * BLOCK_ITEMS)
47 #define _ITEMS_TO_BLOCKS(_items) ((_items) / BLOCK_ITEMS)
50 static void _bitmask_init_idx(void *index, uint32_t items);
53 bitmask_init(struct bitmask_head *bh, void *idx, uint32_t num_items)
57 _bitmask_init_idx(idx, num_items);
59 memset(bh, 0, sizeof(struct bitmask_head));
60 bh->blocks = _ITEMS_TO_BLOCKS(num_items);
61 bh->idx = (u_long *)idx;
65 bitmask_get_resize_items(const struct bitmask_head *bh)
67 if ((bh->items_count * 2 > _BLOCKS_TO_ITEMS(bh->blocks)) && bh->items_count < 65536)
68 return (_BLOCKS_TO_ITEMS(bh->blocks) * 2);
74 bitmask_should_resize(const struct bitmask_head *bh)
77 return (bitmask_get_resize_items(bh) != 0);
82 _bitmask_get_blocks(uint32_t items)
85 return (items / BLOCK_ITEMS);
90 bitmask_get_size(uint32_t items)
93 KASSERT((items % BLOCK_ITEMS) == 0,
94 ("bitmask size needs to power of 2 and greater or equal to %zu",
97 assert((items % BLOCK_ITEMS) == 0);
104 _bitmask_init_idx(void *_idx, uint32_t items)
106 size_t size = bitmask_get_size(items);
107 u_long *idx = (u_long *)_idx;
109 /* Mark all as free */
110 memset(idx, 0xFF, size);
111 *idx &= ~(u_long)1; /* Always skip index 0 */
116 * _try_merge api to allow shrinking?
119 bitmask_copy(const struct bitmask_head *bi, void *new_idx, uint32_t new_items)
121 uint32_t new_blocks = _BLOCKS_TO_ITEMS(new_items);
123 _bitmask_init_idx(new_idx, new_items);
125 if (bi->blocks < new_blocks) {
126 /* extend current blocks */
128 memcpy(new_idx, bi->idx, _BLOCKS_TO_SZ(bi->blocks));
131 /* XXX: ensure all other blocks are non-zero */
132 for (int i = new_blocks; i < bi->blocks; i++) {
140 bitmask_swap(struct bitmask_head *bh, void *new_idx, uint32_t new_items, void **pidx)
146 bh->idx = (u_long *)new_idx;
147 bh->blocks = _ITEMS_TO_BLOCKS(new_items);
154 * Allocate new index in given instance and stores in in @pidx.
155 * Returns 0 on success.
158 bitmask_alloc_idx(struct bitmask_head *bi, uint16_t *pidx)
164 mask = &bi->idx[off];
166 for (i = off; i < bi->blocks; i++, mask++) {
167 if ((v = ffsl(*mask)) == 0)
171 *mask &= ~ ((u_long)1 << (v - 1));
175 v = BLOCK_ITEMS * i + v - 1;
186 * Removes index from given set.
187 * Returns 0 on success.
190 bitmask_free_idx(struct bitmask_head *bi, uint16_t idx)
198 i = idx / BLOCK_ITEMS;
199 v = idx % BLOCK_ITEMS;
206 if ((*mask & ((u_long)1 << v)) != 0)
210 *mask |= (u_long)1 << v;
213 /* Update free offset */
214 if (bi->free_off > i)