xref: /freebsd/sys/net/route/nhop_utils.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 #ifndef	_NET_ROUTE_NHOP_UTILS_H_
29 #define	_NET_ROUTE_NHOP_UTILS_H_
30 
31 /* Chained hash table */
32 struct _cht_head {
33 	uint32_t	hash_size;
34 	uint32_t	items_count;
35 	void		**ptr;
36 };
37 
38 static inline uint32_t
39 _cht_get_resize_size(const struct _cht_head *head)
40 {
41 	uint32_t new_size = 0;
42 
43 	if ((head->items_count * 2 > head->hash_size) && (head->hash_size < 65536))
44 		new_size = head->hash_size * 2;
45 	else if ((head->items_count * 4 < head->hash_size) && head->hash_size > 16)
46 		new_size = head->hash_size / 2;
47 
48 	return (new_size);
49 }
50 
51 static inline int
52 _cht_need_resize(const struct _cht_head *head)
53 {
54 
55 	return (_cht_get_resize_size(head) > 0);
56 }
57 
58 #ifndef	typeof
59 #define	typeof	__typeof
60 #endif
61 
62 #define	CHT_SLIST_NEED_RESIZE(_head)		\
63 	_cht_need_resize((const struct _cht_head *)(_head))
64 #define	CHT_SLIST_GET_RESIZE_BUCKETS(_head)	\
65 	_cht_get_resize_size((const struct _cht_head *)(_head))
66 #define	CHT_SLIST_GET_RESIZE_SIZE(_buckets)	((_buckets) * sizeof(void *))
67 
68 #define	CHT_SLIST_DEFINE(_HNAME, _ITEM_TYPE)	\
69 struct _HNAME##_head {				\
70 	uint32_t	hash_size;		\
71 	uint32_t	items_count;		\
72 	_ITEM_TYPE	**ptr;			\
73 }
74 
75 #define	CHT_SLIST_INIT(_head, _ptr, _num_buckets)	\
76 	(_head)->hash_size = _num_buckets;		\
77 	(_head)->items_count = 0;			\
78 	(_head)->ptr = _ptr;
79 
80 /* Default hash method for constant-size keys */
81 
82 #define	CHT_GET_BUCK(_head, _PX, _key)	_PX##_hash_key(_key) & ((_head)->hash_size - 1)
83 #define	CHT_GET_BUCK_OBJ(_head, _PX, _obj)	_PX##_hash_obj(_obj) & ((_head)->hash_size - 1)
84 
85 #define	CHT_FIRST(_head, idx)	_CHT_FIRST((_head)->ptr, idx)
86 #define	_CHT_FIRST(_ptr, idx)	(_ptr)[idx]
87 
88 #define	CHT_SLIST_FIND(_head, _PX, _key, _ret) do {			\
89 	uint32_t _buck = CHT_GET_BUCK(_head, _PX, _key);		\
90 	_ret = CHT_FIRST(_head, _buck);					\
91 	for ( ; _ret != NULL; _ret = _PX##_next(_ret)) {		\
92 		if (_PX##_cmp(_key, (_ret)))				\
93 			break;						\
94 	}								\
95 } while(0)
96 
97 /*
98  * hash_obj, nhop_cmp
99  */
100 #define	CHT_SLIST_FIND_BYOBJ(_head, _PX, _obj, _ret) do {		\
101 	uint32_t _buck = CHT_GET_BUCK_OBJ(_head, _PX, _obj);		\
102 	_ret = CHT_FIRST(_head, _buck);					\
103 	for ( ; _ret != NULL; _ret = _PX##_next(_ret)) {		\
104 		if (_PX##_cmp(_obj, _ret))				\
105 			break;						\
106 	}								\
107 } while(0)
108 
109 #define	CHT_SLIST_INSERT_HEAD(_head, _PX, _obj) do {			\
110 	uint32_t _buck = CHT_GET_BUCK_OBJ(_head, _PX, _obj);		\
111 	_PX##_next(_obj) = CHT_FIRST(_head, _buck);			\
112 	CHT_FIRST(_head, _buck) = _obj;					\
113 	(_head)->items_count++;						\
114 } while(0)
115 
116 #define	CHT_SLIST_REMOVE(_head, _PX, _obj, _ret) do {			\
117 	typeof(*(_head)->ptr) _tmp;					\
118 	uint32_t _buck = CHT_GET_BUCK_OBJ(_head, _PX, _obj);		\
119 	_ret = CHT_FIRST(_head, _buck);					\
120 	_tmp = NULL;							\
121 	for ( ; _ret != NULL; _tmp = _ret, _ret = _PX##_next(_ret)) {	\
122 		if (_obj == _ret)					\
123 			break;						\
124 	}								\
125 	if (_ret != NULL) {						\
126 		if (_tmp == NULL)					\
127 			CHT_FIRST(_head, _buck) = _PX##_next(_ret);	\
128 		else							\
129 			_PX##_next(_tmp) = _PX##_next(_ret);		\
130 		(_head)->items_count--;					\
131 	}								\
132 } while(0)
133 #define	CHT_SLIST_REMOVE_BYOBJ	CHT_SLIST_REMOVE
134 
135 #define	CHT_SLIST_FOREACH(_head, _PX, _x)				\
136 	for (uint32_t _i = 0; _i < (_head)->hash_size; _i++) {		\
137 		for (_x = CHT_FIRST(_head, _i); _x; _x = _PX##_next(_x))
138 #define	CHT_SLIST_FOREACH_END	}
139 
140 #define	CHT_SLIST_FOREACH_SAFE(_head, _PX, _x, _tmp)			\
141 	for (uint32_t _i = 0; _i < (_head)->hash_size; _i++) {		\
142 		for (_x = CHT_FIRST(_head, _i); (_tmp = _PX##_next(_x), _x); _x = _tmp)
143 #define	CHT_SLIST_FOREACH_SAFE_END	}
144 
145 #define	CHT_SLIST_RESIZE(_head, _PX, _new_void_ptr, _new_hsize)		\
146 	uint32_t _new_idx;						\
147 	typeof((_head)->ptr) _new_ptr = (void *)_new_void_ptr;		\
148 	typeof(*(_head)->ptr) _x, _y;					\
149 	for (uint32_t _old_idx = 0; _old_idx < (_head)->hash_size; _old_idx++) {\
150 		_x = CHT_FIRST(_head, _old_idx);			\
151 		_y = _x;						\
152 		while (_y != NULL) {					\
153 			_y = _PX##_next(_x);				\
154 			_new_idx = _PX##_hash_obj(_x) & (_new_hsize - 1);\
155 			_PX##_next(_x) = _CHT_FIRST(_new_ptr, _new_idx);\
156 			_CHT_FIRST(_new_ptr, _new_idx) = _x;		\
157 			_x = _y;					\
158 		}							\
159 	}								\
160 	(_head)->hash_size = _new_hsize;				\
161 	_new_void_ptr = (void *)(_head)->ptr;				\
162 	(_head)->ptr = _new_ptr;
163 
164 /* bitmasks */
165 
166 struct bitmask_head {
167 	uint16_t	free_off; /* index of the first potentially free block */
168 	uint16_t	blocks; /* number of 4/8-byte blocks in the index */
169 	uint32_t	items_count; /* total number of items */
170 	u_long		*idx;
171 };
172 
173 size_t bitmask_get_size(uint32_t items);
174 uint32_t bitmask_get_resize_items(const struct bitmask_head *nh);
175 int bitmask_should_resize(const struct bitmask_head *bh);
176 void bitmask_swap(struct bitmask_head *bh, void *new_idx, uint32_t new_items, void **pidx);
177 void bitmask_init(struct bitmask_head *bh, void *idx, uint32_t num_items);
178 int bitmask_copy(const struct bitmask_head *bi, void *new_idx, uint32_t new_items);
179 int bitmask_alloc_idx(struct bitmask_head *bi, uint16_t *pidx);
180 int bitmask_free_idx(struct bitmask_head *bi, uint16_t idx);
181 
182 #endif
183