aboutsummaryrefslogtreecommitdiff
path: root/sys/net/route/nhop_utils.c
blob: 56bca99c9ed87a076d59a04c72968ca0e42479f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2020 Alexander V. Chernikov
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_inet.h"
#include "opt_route.h"
#include "opt_mpath.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>

#include <net/route/nhop_utils.h>

#define	BLOCK_ITEMS	(8 * sizeof(u_long))	/* Number of items for ffsl() */

#define	_BLOCKS_TO_SZ(_blocks)		((size_t)(_blocks) * sizeof(u_long))
#define	_BLOCKS_TO_ITEMS(_blocks)	((uint32_t)(_blocks) * BLOCK_ITEMS)
#define	_ITEMS_TO_BLOCKS(_items)	((_items) / BLOCK_ITEMS)


static void _bitmask_init_idx(void *index, uint32_t items);

void
bitmask_init(struct bitmask_head *bh, void *idx, uint32_t num_items)
{

	if (idx != NULL)
		_bitmask_init_idx(idx, num_items);

	memset(bh, 0, sizeof(struct bitmask_head));
	bh->blocks = _ITEMS_TO_BLOCKS(num_items);
	bh->idx = (u_long *)idx;
}

uint32_t
bitmask_get_resize_items(const struct bitmask_head *bh)
{
	if ((bh->items_count * 2 > _BLOCKS_TO_ITEMS(bh->blocks)) && bh->items_count < 65536)
		return (_BLOCKS_TO_ITEMS(bh->blocks) * 2);

	return (0);
}

int
bitmask_should_resize(const struct bitmask_head *bh)
{

	return (bitmask_get_resize_items(bh) != 0);
}

#if 0
uint32_t
_bitmask_get_blocks(uint32_t items)
{

	return (items / BLOCK_ITEMS);
}
#endif

size_t
bitmask_get_size(uint32_t items)
{
#if _KERNEL
	KASSERT((items % BLOCK_ITEMS) == 0,
	   ("bitmask size needs to power of 2 and greater or equal to %zu",
	    BLOCK_ITEMS));
#else
	assert((items % BLOCK_ITEMS) == 0);
#endif

	return (items / 8);
}

static void
_bitmask_init_idx(void *_idx, uint32_t items)
{
	size_t size = bitmask_get_size(items);
	u_long *idx = (u_long *)_idx;

	/* Mark all as free */
	memset(idx, 0xFF, size);
	*idx &= ~(u_long)1; /* Always skip index 0 */
}


/*
 * _try_merge api to allow shrinking?
 */
int
bitmask_copy(const struct bitmask_head *bi, void *new_idx, uint32_t new_items)
{
	uint32_t new_blocks = _BLOCKS_TO_ITEMS(new_items);

	_bitmask_init_idx(new_idx, new_items);

	if (bi->blocks < new_blocks) {
		/* extend current blocks */
		if (bi->blocks > 0)
			memcpy(new_idx, bi->idx, _BLOCKS_TO_SZ(bi->blocks));
		return (0);
	} else {
		/* XXX: ensure all other blocks are non-zero */
		for (int i = new_blocks; i < bi->blocks; i++) {
		}

		return (1);
	}
}

void
bitmask_swap(struct bitmask_head *bh, void *new_idx, uint32_t new_items, void **pidx)
{
	void *old_ptr;

	old_ptr = bh->idx;

	bh->idx = (u_long *)new_idx;
	bh->blocks = _ITEMS_TO_BLOCKS(new_items);

	if (pidx != NULL)
		*pidx = old_ptr;
}

/*
 * Allocate new index in given instance and stores in in @pidx.
 * Returns 0 on success.
 */
int
bitmask_alloc_idx(struct bitmask_head *bi, uint16_t *pidx)
{
	u_long *mask;
	int i, off, v;

	off = bi->free_off;
	mask = &bi->idx[off];

	for (i = off; i < bi->blocks; i++, mask++) {
		if ((v = ffsl(*mask)) == 0)
			continue;

		/* Mark as busy */
		*mask &= ~ ((u_long)1 << (v - 1));

		bi->free_off = i;

		v = BLOCK_ITEMS * i + v - 1;

		*pidx = v;
		bi->items_count++;
		return (0);
	}

	return (1);
}

/*
 * Removes index from given set.
 * Returns 0 on success.
 */
int
bitmask_free_idx(struct bitmask_head *bi, uint16_t idx)
{
	u_long *mask;
	int i, v;

	if (idx == 0)
		return (1);

	i = idx / BLOCK_ITEMS;
	v = idx % BLOCK_ITEMS;

	if (i >= bi->blocks)
		return (1);

	mask = &bi->idx[i];

	if ((*mask & ((u_long)1 << v)) != 0)
		return (1);

	/* Mark as free */
	*mask |= (u_long)1 << v;
	bi->items_count--;

	/* Update free offset */
	if (bi->free_off > i)
		bi->free_off = i;

	return (0);
}