xref: /freebsd/sys/netpfil/ipfw/dn_heap.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
5  * All rights reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Binary heap and hash tables, used in dummynet
31  */
32 
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #ifdef _KERNEL
36 __FBSDID("$FreeBSD$");
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <netpfil/ipfw/dn_heap.h>
41 #ifndef log
42 #define log(x, arg...)
43 #endif
44 
45 #else /* !_KERNEL */
46 
47 #include <stdio.h>
48 #include <dn_test.h>
49 #include <strings.h>
50 #include <stdlib.h>
51 
52 #include  "dn_heap.h"
53 #define log(x, arg...)	fprintf(stderr, ## arg)
54 #define panic(x...)	fprintf(stderr, ## x), exit(1)
55 #define MALLOC_DEFINE(a, b, c)	volatile int __dummy__ ## a __attribute__((__unused__))
56 static void *my_malloc(int s) {	return malloc(s); }
57 static void my_free(void *p) {	free(p); }
58 #define malloc(s, t, w)	my_malloc(s)
59 #define free(p, t)	my_free(p)
60 #endif /* !_KERNEL */
61 
62 static MALLOC_DEFINE(M_DN_HEAP, "dummynet", "dummynet heap");
63 
64 /*
65  * Heap management functions.
66  *
67  * In the heap, first node is element 0. Children of i are 2i+1 and 2i+2.
68  * Some macros help finding parent/children so we can optimize them.
69  *
70  * heap_init() is called to expand the heap when needed.
71  * Increment size in blocks of 16 entries.
72  * Returns 1 on error, 0 on success
73  */
74 #define HEAP_FATHER(x) ( ( (x) - 1 ) / 2 )
75 #define HEAP_LEFT(x) ( (x)+(x) + 1 )
76 #define	HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; }
77 #define HEAP_INCREMENT	15
78 
79 static int
80 heap_resize(struct dn_heap *h, unsigned int new_size)
81 {
82 	struct dn_heap_entry *p;
83 
84 	if ((unsigned int)h->size >= new_size )	/* have enough room */
85 		return 0;
86 #if 1  /* round to the next power of 2 */
87 	new_size |= new_size >> 1;
88 	new_size |= new_size >> 2;
89 	new_size |= new_size >> 4;
90 	new_size |= new_size >> 8;
91 	new_size |= new_size >> 16;
92 #else
93 	new_size = (new_size + HEAP_INCREMENT ) & ~HEAP_INCREMENT;
94 #endif
95 	p = mallocarray(new_size, sizeof(*p), M_DN_HEAP, M_NOWAIT);
96 	if (p == NULL) {
97 		printf("--- %s, resize %d failed\n", __func__, new_size );
98 		return 1; /* error */
99 	}
100 	if (h->size > 0) {
101 		bcopy(h->p, p, h->size * sizeof(*p) );
102 		free(h->p, M_DN_HEAP);
103 	}
104 	h->p = p;
105 	h->size = new_size;
106 	return 0;
107 }
108 
109 int
110 heap_init(struct dn_heap *h, int size, int ofs)
111 {
112 	if (heap_resize(h, size))
113 		return 1;
114 	h->elements = 0;
115 	h->ofs = ofs;
116 	return 0;
117 }
118 
119 /*
120  * Insert element in heap. Normally, p != NULL, we insert p in
121  * a new position and bubble up. If p == NULL, then the element is
122  * already in place, and key is the position where to start the
123  * bubble-up.
124  * Returns 1 on failure (cannot allocate new heap entry)
125  *
126  * If ofs > 0 the position (index, int) of the element in the heap is
127  * also stored in the element itself at the given offset in bytes.
128  */
129 #define SET_OFFSET(h, i) do {					\
130 	if (h->ofs > 0)						\
131 	    *((int32_t *)((char *)(h->p[i].object) + h->ofs)) = i;	\
132 	} while (0)
133 /*
134  * RESET_OFFSET is used for sanity checks. It sets ofs
135  * to an invalid value.
136  */
137 #define RESET_OFFSET(h, i) do {					\
138 	if (h->ofs > 0)						\
139 	    *((int32_t *)((char *)(h->p[i].object) + h->ofs)) = -16;	\
140 	} while (0)
141 
142 int
143 heap_insert(struct dn_heap *h, uint64_t key1, void *p)
144 {
145 	int son = h->elements;
146 
147 	//log("%s key %llu p %p\n", __FUNCTION__, key1, p);
148 	if (p == NULL) { /* data already there, set starting point */
149 		son = key1;
150 	} else { /* insert new element at the end, possibly resize */
151 		son = h->elements;
152 		if (son == h->size) /* need resize... */
153 			// XXX expand by 16 or so
154 			if (heap_resize(h, h->elements+16) )
155 				return 1; /* failure... */
156 		h->p[son].object = p;
157 		h->p[son].key = key1;
158 		h->elements++;
159 	}
160 	/* make sure that son >= father along the path */
161 	while (son > 0) {
162 		int father = HEAP_FATHER(son);
163 		struct dn_heap_entry tmp;
164 
165 		if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
166 			break; /* found right position */
167 		/* son smaller than father, swap and repeat */
168 		HEAP_SWAP(h->p[son], h->p[father], tmp);
169 		SET_OFFSET(h, son);
170 		son = father;
171 	}
172 	SET_OFFSET(h, son);
173 	return 0;
174 }
175 
176 /*
177  * remove top element from heap, or obj if obj != NULL
178  */
179 bool
180 heap_extract(struct dn_heap *h, void *obj)
181 {
182 	int child, father, max = h->elements - 1;
183 
184 	if (max < 0) {
185 		return false;
186 	}
187 	if (obj == NULL)
188 		father = 0; /* default: move up smallest child */
189 	else { /* extract specific element, index is at offset */
190 		if (h->ofs <= 0)
191 			panic("%s: extract from middle not set on %p\n",
192 				__FUNCTION__, h);
193 		father = *((int *)((char *)obj + h->ofs));
194 		if (father < 0 || father >= h->elements)
195 			return false;
196 	}
197 	/* We should make sure that the object we're trying to remove is
198 	 * actually in this heap. */
199 	if (obj != NULL && h->p[father].object != obj)
200 		return false;
201 
202 	/*
203 	 * below, father is the index of the empty element, which
204 	 * we replace at each step with the smallest child until we
205 	 * reach the bottom level.
206 	 */
207 	// XXX why removing RESET_OFFSET increases runtime by 10% ?
208 	RESET_OFFSET(h, father);
209 	while ( (child = HEAP_LEFT(father)) <= max ) {
210 		if (child != max &&
211 		    DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
212 			child++; /* take right child, otherwise left */
213 		h->p[father] = h->p[child];
214 		SET_OFFSET(h, father);
215 		father = child;
216 	}
217 	h->elements--;
218 	if (father != max) {
219 		/*
220 		 * Fill hole with last entry and bubble up,
221 		 * reusing the insert code
222 		 */
223 		h->p[father] = h->p[max];
224 		heap_insert(h, father, NULL);
225 	}
226 
227 	return true;
228 }
229 
230 #if 0
231 /*
232  * change object position and update references
233  * XXX this one is never used!
234  */
235 static void
236 heap_move(struct dn_heap *h, uint64_t new_key, void *object)
237 {
238 	int temp, i, max = h->elements-1;
239 	struct dn_heap_entry *p, buf;
240 
241 	if (h->ofs <= 0)
242 		panic("cannot move items on this heap");
243 	p = h->p;	/* shortcut */
244 
245 	i = *((int *)((char *)object + h->ofs));
246 	if (DN_KEY_LT(new_key, p[i].key) ) { /* must move up */
247 		p[i].key = new_key;
248 		for (; i>0 &&
249 		    DN_KEY_LT(new_key, p[(temp = HEAP_FATHER(i))].key);
250 		    i = temp ) { /* bubble up */
251 			HEAP_SWAP(p[i], p[temp], buf);
252 			SET_OFFSET(h, i);
253 		}
254 	} else {		/* must move down */
255 		p[i].key = new_key;
256 		while ( (temp = HEAP_LEFT(i)) <= max ) {
257 			/* found left child */
258 			if (temp != max &&
259 			    DN_KEY_LT(p[temp+1].key, p[temp].key))
260 				temp++; /* select child with min key */
261 			if (DN_KEY_LT(>p[temp].key, new_key)) {
262 				/* go down */
263 				HEAP_SWAP(p[i], p[temp], buf);
264 				SET_OFFSET(h, i);
265 			} else
266 				break;
267 			i = temp;
268 		}
269 	}
270 	SET_OFFSET(h, i);
271 }
272 #endif /* heap_move, unused */
273 
274 /*
275  * heapify() will reorganize data inside an array to maintain the
276  * heap property. It is needed when we delete a bunch of entries.
277  */
278 static void
279 heapify(struct dn_heap *h)
280 {
281 	int i;
282 
283 	for (i = 0; i < h->elements; i++ )
284 		heap_insert(h, i , NULL);
285 }
286 
287 int
288 heap_scan(struct dn_heap *h, int (*fn)(void *, uintptr_t),
289 	uintptr_t arg)
290 {
291 	int i, ret, found;
292 
293 	for (i = found = 0 ; i < h->elements ;) {
294 		ret = fn(h->p[i].object, arg);
295 		if (ret & HEAP_SCAN_DEL) {
296 			h->elements-- ;
297 			h->p[i] = h->p[h->elements] ;
298 			found++ ;
299 		} else
300 			i++ ;
301 		if (ret & HEAP_SCAN_END)
302 			break;
303 	}
304 	if (found)
305 		heapify(h);
306 	return found;
307 }
308 
309 /*
310  * cleanup the heap and free data structure
311  */
312 void
313 heap_free(struct dn_heap *h)
314 {
315 	if (h->size >0 )
316 		free(h->p, M_DN_HEAP);
317 	bzero(h, sizeof(*h) );
318 }
319 
320 /*
321  * hash table support.
322  */
323 
324 struct dn_ht {
325         int buckets;            /* how many buckets, really buckets - 1*/
326         int entries;            /* how many entries */
327         int ofs;	        /* offset of link field */
328         uint32_t (*hash)(uintptr_t, int, void *arg);
329         int (*match)(void *_el, uintptr_t key, int, void *);
330         void *(*newh)(uintptr_t, int, void *);
331         void **ht;              /* bucket heads */
332 };
333 /*
334  * Initialize, allocating bucket pointers inline.
335  * Recycle previous record if possible.
336  * If the 'newh' function is not supplied, we assume that the
337  * key passed to ht_find is the same object to be stored in.
338  */
339 struct dn_ht *
340 dn_ht_init(struct dn_ht *ht, int buckets, int ofs,
341         uint32_t (*h)(uintptr_t, int, void *),
342         int (*match)(void *, uintptr_t, int, void *),
343 	void *(*newh)(uintptr_t, int, void *))
344 {
345 	int l;
346 
347 	/*
348 	 * Notes about rounding bucket size to a power of two.
349 	 * Given the original bucket size, we compute the nearest lower and
350 	 * higher power of two, minus 1  (respectively b_min and b_max) because
351 	 * this value will be used to do an AND with the index returned
352 	 * by hash function.
353 	 * To choice between these two values, the original bucket size is
354 	 * compared with b_min. If the original size is greater than 4/3 b_min,
355 	 * we round the bucket size to b_max, else to b_min.
356 	 * This ratio try to round to the nearest power of two, advantaging
357 	 * the greater size if the different between two power is relatively
358 	 * big.
359 	 * Rounding the bucket size to a power of two avoid the use of
360 	 * module when calculating the correct bucket.
361 	 * The ht->buckets variable store the bucket size - 1 to simply
362 	 * do an AND between the index returned by hash function and ht->bucket
363 	 * instead of a module.
364 	 */
365 	int b_min; /* min buckets */
366 	int b_max; /* max buckets */
367 	int b_ori; /* original buckets */
368 
369 	if (h == NULL || match == NULL) {
370 		printf("--- missing hash or match function");
371 		return NULL;
372 	}
373 	if (buckets < 1 || buckets > 65536)
374 		return NULL;
375 
376 	b_ori = buckets;
377 	/* calculate next power of 2, - 1*/
378 	buckets |= buckets >> 1;
379 	buckets |= buckets >> 2;
380 	buckets |= buckets >> 4;
381 	buckets |= buckets >> 8;
382 	buckets |= buckets >> 16;
383 
384 	b_max = buckets; /* Next power */
385 	b_min = buckets >> 1; /* Previous power */
386 
387 	/* Calculate the 'nearest' bucket size */
388 	if (b_min * 4000 / 3000 < b_ori)
389 		buckets = b_max;
390 	else
391 		buckets = b_min;
392 
393 	if (ht) {	/* see if we can reuse */
394 		if (buckets <= ht->buckets) {
395 			ht->buckets = buckets;
396 		} else {
397 			/* free pointers if not allocated inline */
398 			if (ht->ht != (void *)(ht + 1))
399 				free(ht->ht, M_DN_HEAP);
400 			free(ht, M_DN_HEAP);
401 			ht = NULL;
402 		}
403 	}
404 	if (ht == NULL) {
405 		/* Allocate buckets + 1 entries because buckets is use to
406 		 * do the AND with the index returned by hash function
407 		 */
408 		l = sizeof(*ht) + (buckets + 1) * sizeof(void **);
409 		ht = malloc(l, M_DN_HEAP, M_NOWAIT | M_ZERO);
410 	}
411 	if (ht) {
412 		ht->ht = (void **)(ht + 1);
413 		ht->buckets = buckets;
414 		ht->ofs = ofs;
415 		ht->hash = h;
416 		ht->match = match;
417 		ht->newh = newh;
418 	}
419 	return ht;
420 }
421 
422 /* dummy callback for dn_ht_free to unlink all */
423 static int
424 do_del(void *obj, void *arg)
425 {
426 	(void)obj;
427 	(void)arg;
428 	return DNHT_SCAN_DEL;
429 }
430 
431 void
432 dn_ht_free(struct dn_ht *ht, int flags)
433 {
434 	if (ht == NULL)
435 		return;
436 	if (flags & DNHT_REMOVE) {
437 		(void)dn_ht_scan(ht, do_del, NULL);
438 	} else {
439 		if (ht->ht && ht->ht != (void *)(ht + 1))
440 			free(ht->ht, M_DN_HEAP);
441 		free(ht, M_DN_HEAP);
442 	}
443 }
444 
445 int
446 dn_ht_entries(struct dn_ht *ht)
447 {
448 	return ht ? ht->entries : 0;
449 }
450 
451 /* lookup and optionally create or delete element */
452 void *
453 dn_ht_find(struct dn_ht *ht, uintptr_t key, int flags, void *arg)
454 {
455 	int i;
456 	void **pp, *p;
457 
458 	if (ht == NULL)	/* easy on an empty hash */
459 		return NULL;
460 	i = (ht->buckets == 1) ? 0 :
461 		(ht->hash(key, flags, arg) & ht->buckets);
462 
463 	for (pp = &ht->ht[i]; (p = *pp); pp = (void **)((char *)p + ht->ofs)) {
464 		if (flags & DNHT_MATCH_PTR) {
465 			if (key == (uintptr_t)p)
466 				break;
467 		} else if (ht->match(p, key, flags, arg)) /* found match */
468 			break;
469 	}
470 	if (p) {
471 		if (flags & DNHT_REMOVE) {
472 			/* link in the next element */
473 			*pp = *(void **)((char *)p + ht->ofs);
474 			*(void **)((char *)p + ht->ofs) = NULL;
475 			ht->entries--;
476 		}
477 	} else if (flags & DNHT_INSERT) {
478 		// printf("%s before calling new, bucket %d ofs %d\n",
479 		//	__FUNCTION__, i, ht->ofs);
480 		p = ht->newh ? ht->newh(key, flags, arg) : (void *)key;
481 		// printf("%s newh returns %p\n", __FUNCTION__, p);
482 		if (p) {
483 			ht->entries++;
484 			*(void **)((char *)p + ht->ofs) = ht->ht[i];
485 			ht->ht[i] = p;
486 		}
487 	}
488 	return p;
489 }
490 
491 /*
492  * do a scan with the option to delete the object. Extract next before
493  * running the callback because the element may be destroyed there.
494  */
495 int
496 dn_ht_scan(struct dn_ht *ht, int (*fn)(void *, void *), void *arg)
497 {
498 	int i, ret, found = 0;
499 	void **curp, *cur, *next;
500 
501 	if (ht == NULL || fn == NULL)
502 		return 0;
503 	for (i = 0; i <= ht->buckets; i++) {
504 		curp = &ht->ht[i];
505 		while ( (cur = *curp) != NULL) {
506 			next = *(void **)((char *)cur + ht->ofs);
507 			ret = fn(cur, arg);
508 			if (ret & DNHT_SCAN_DEL) {
509 				found++;
510 				ht->entries--;
511 				*curp = next;
512 			} else {
513 				curp = (void **)((char *)cur + ht->ofs);
514 			}
515 			if (ret & DNHT_SCAN_END)
516 				return found;
517 		}
518 	}
519 	return found;
520 }
521 
522 /*
523  * Similar to dn_ht_scan(), except that the scan is performed only
524  * in the bucket 'bucket'. The function returns a correct bucket number if
525  * the original is invalid.
526  * If the callback returns DNHT_SCAN_END, the function move the ht->ht[i]
527  * pointer to the last entry processed. Moreover, the bucket number passed
528  * by caller is decremented, because usually the caller increment it.
529  */
530 int
531 dn_ht_scan_bucket(struct dn_ht *ht, int *bucket, int (*fn)(void *, void *),
532 		 void *arg)
533 {
534 	int i, ret, found = 0;
535 	void **curp, *cur, *next;
536 
537 	if (ht == NULL || fn == NULL)
538 		return 0;
539 	if (*bucket > ht->buckets)
540 		*bucket = 0;
541 	i = *bucket;
542 
543 	curp = &ht->ht[i];
544 	while ( (cur = *curp) != NULL) {
545 		next = *(void **)((char *)cur + ht->ofs);
546 		ret = fn(cur, arg);
547 		if (ret & DNHT_SCAN_DEL) {
548 			found++;
549 			ht->entries--;
550 			*curp = next;
551 		} else {
552 			curp = (void **)((char *)cur + ht->ofs);
553 		}
554 		if (ret & DNHT_SCAN_END)
555 			return found;
556 	}
557 	return found;
558 }
559