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
28#include <sys/cdefs.h>
29#include "opt_inet.h"
30#include "opt_route.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/lock.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/kernel.h>
38
39#include <net/route/nhop_utils.h>
40
41#define	BLOCK_ITEMS	(8 * sizeof(u_long))	/* Number of items for ffsl() */
42
43#define	_BLOCKS_TO_SZ(_blocks)		((size_t)(_blocks) * sizeof(u_long))
44#define	_BLOCKS_TO_ITEMS(_blocks)	((uint32_t)(_blocks) * BLOCK_ITEMS)
45#define	_ITEMS_TO_BLOCKS(_items)	((_items) / BLOCK_ITEMS)
46
47static void _bitmask_init_idx(void *index, uint32_t items);
48
49void
50bitmask_init(struct bitmask_head *bh, void *idx, uint32_t num_items)
51{
52
53	if (idx != NULL)
54		_bitmask_init_idx(idx, num_items);
55
56	memset(bh, 0, sizeof(struct bitmask_head));
57	bh->blocks = _ITEMS_TO_BLOCKS(num_items);
58	bh->idx = (u_long *)idx;
59}
60
61uint32_t
62bitmask_get_resize_items(const struct bitmask_head *bh)
63{
64	if ((bh->items_count * 2 > _BLOCKS_TO_ITEMS(bh->blocks)) && bh->items_count < 65536)
65		return (_BLOCKS_TO_ITEMS(bh->blocks) * 2);
66
67	return (0);
68}
69
70int
71bitmask_should_resize(const struct bitmask_head *bh)
72{
73
74	return (bitmask_get_resize_items(bh) != 0);
75}
76
77#if 0
78uint32_t
79_bitmask_get_blocks(uint32_t items)
80{
81
82	return (items / BLOCK_ITEMS);
83}
84#endif
85
86size_t
87bitmask_get_size(uint32_t items)
88{
89#if _KERNEL
90	KASSERT((items % BLOCK_ITEMS) == 0,
91	   ("bitmask size needs to power of 2 and greater or equal to %zu",
92	    BLOCK_ITEMS));
93#else
94	assert((items % BLOCK_ITEMS) == 0);
95#endif
96
97	return (items / 8);
98}
99
100static void
101_bitmask_init_idx(void *_idx, uint32_t items)
102{
103	size_t size = bitmask_get_size(items);
104	u_long *idx = (u_long *)_idx;
105
106	/* Mark all as free */
107	memset(idx, 0xFF, size);
108	*idx &= ~(u_long)1; /* Always skip index 0 */
109}
110
111/*
112 * _try_merge api to allow shrinking?
113 */
114int
115bitmask_copy(const struct bitmask_head *bi, void *new_idx, uint32_t new_items)
116{
117	uint32_t new_blocks = _BLOCKS_TO_ITEMS(new_items);
118
119	_bitmask_init_idx(new_idx, new_items);
120
121	if (bi->blocks < new_blocks) {
122		/* extend current blocks */
123		if (bi->blocks > 0)
124			memcpy(new_idx, bi->idx, _BLOCKS_TO_SZ(bi->blocks));
125		return (0);
126	} else {
127		/* XXX: ensure all other blocks are non-zero */
128		for (int i = new_blocks; i < bi->blocks; i++) {
129		}
130
131		return (1);
132	}
133}
134
135void
136bitmask_swap(struct bitmask_head *bh, void *new_idx, uint32_t new_items, void **pidx)
137{
138	void *old_ptr;
139
140	old_ptr = bh->idx;
141
142	bh->idx = (u_long *)new_idx;
143	bh->blocks = _ITEMS_TO_BLOCKS(new_items);
144
145	if (pidx != NULL)
146		*pidx = old_ptr;
147}
148
149/*
150 * Allocate new index in given instance and stores in in @pidx.
151 * Returns 0 on success.
152 */
153int
154bitmask_alloc_idx(struct bitmask_head *bi, uint16_t *pidx)
155{
156	u_long *mask;
157	int i, off, v;
158
159	off = bi->free_off;
160	mask = &bi->idx[off];
161
162	for (i = off; i < bi->blocks; i++, mask++) {
163		if ((v = ffsl(*mask)) == 0)
164			continue;
165
166		/* Mark as busy */
167		*mask &= ~ ((u_long)1 << (v - 1));
168
169		bi->free_off = i;
170
171		v = BLOCK_ITEMS * i + v - 1;
172
173		*pidx = v;
174		bi->items_count++;
175		return (0);
176	}
177
178	return (1);
179}
180
181/*
182 * Removes index from given set.
183 * Returns 0 on success.
184 */
185int
186bitmask_free_idx(struct bitmask_head *bi, uint16_t idx)
187{
188	u_long *mask;
189	int i, v;
190
191	if (idx == 0)
192		return (1);
193
194	i = idx / BLOCK_ITEMS;
195	v = idx % BLOCK_ITEMS;
196
197	if (i >= bi->blocks)
198		return (1);
199
200	mask = &bi->idx[i];
201
202	if ((*mask & ((u_long)1 << v)) != 0)
203		return (1);
204
205	/* Mark as free */
206	*mask |= (u_long)1 << v;
207	bi->items_count--;
208
209	/* Update free offset */
210	if (bi->free_off > i)
211		bi->free_off = i;
212
213	return (0);
214}
215