Home | History | Annotate | Line # | Download | only in npf
lpm.c revision 1.2
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2016 Mindaugas Rasiukevicius <rmind at noxt eu>
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  christos  *    documentation and/or other materials provided with the distribution.
     13  1.1  christos  *
     14  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  christos  * SUCH DAMAGE.
     25  1.1  christos  */
     26  1.1  christos 
     27  1.1  christos /*
     28  1.1  christos  * TODO: Simple linear scan for now (works just well with a few prefixes).
     29  1.1  christos  * TBD on a better algorithm.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #if defined(_KERNEL)
     33  1.1  christos #include <sys/cdefs.h>
     34  1.2     rmind __KERNEL_RCSID(0, "$NetBSD: lpm.c,v 1.2 2016/12/26 12:44:10 rmind Exp $");
     35  1.1  christos 
     36  1.1  christos #include <sys/param.h>
     37  1.1  christos #include <sys/types.h>
     38  1.1  christos #include <sys/malloc.h>
     39  1.1  christos #include <sys/kmem.h>
     40  1.1  christos #else
     41  1.1  christos #include <sys/socket.h>
     42  1.1  christos #include <arpa/inet.h>
     43  1.1  christos 
     44  1.1  christos #include <stdio.h>
     45  1.1  christos #include <stdlib.h>
     46  1.1  christos #include <stdbool.h>
     47  1.1  christos #include <stddef.h>
     48  1.1  christos #include <string.h>
     49  1.1  christos #include <strings.h>
     50  1.1  christos #include <errno.h>
     51  1.1  christos #include <assert.h>
     52  1.1  christos #define kmem_alloc(a, b) malloc(a)
     53  1.1  christos #define kmem_free(a, b) free(a)
     54  1.1  christos #define kmem_zalloc(a, b) calloc(a, 1)
     55  1.1  christos #endif
     56  1.1  christos 
     57  1.1  christos #include "lpm.h"
     58  1.1  christos 
     59  1.1  christos #define	LPM_MAX_PREFIX		(128)
     60  1.1  christos #define	LPM_MAX_WORDS		(LPM_MAX_PREFIX >> 5)
     61  1.1  christos #define	LPM_TO_WORDS(x)		((x) >> 2)
     62  1.1  christos #define	LPM_HASH_STEP		(8)
     63  1.1  christos 
     64  1.1  christos #ifdef DEBUG
     65  1.1  christos #define	ASSERT	assert
     66  1.1  christos #else
     67  1.1  christos #define	ASSERT
     68  1.1  christos #endif
     69  1.1  christos 
     70  1.1  christos typedef struct lpm_ent {
     71  1.1  christos 	struct lpm_ent *next;
     72  1.1  christos 	void *		val;
     73  1.1  christos 	unsigned	len;
     74  1.1  christos 	uint8_t		key[];
     75  1.1  christos } lpm_ent_t;
     76  1.1  christos 
     77  1.1  christos typedef struct {
     78  1.1  christos 	uint32_t	hashsize;
     79  1.1  christos 	uint32_t	nitems;
     80  1.1  christos 	lpm_ent_t **bucket;
     81  1.1  christos } lpm_hmap_t;
     82  1.1  christos 
     83  1.1  christos struct lpm {
     84  1.1  christos 	uint32_t	bitmask[LPM_MAX_WORDS];
     85  1.1  christos 	void *		defval;
     86  1.1  christos 	lpm_hmap_t	prefix[LPM_MAX_PREFIX + 1];
     87  1.1  christos };
     88  1.1  christos 
     89  1.1  christos lpm_t *
     90  1.1  christos lpm_create(void)
     91  1.1  christos {
     92  1.1  christos 	return kmem_zalloc(sizeof(lpm_t), KM_SLEEP);
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos void
     96  1.1  christos lpm_clear(lpm_t *lpm, lpm_dtor_t dtor, void *arg)
     97  1.1  christos {
     98  1.1  christos 	for (unsigned n = 0; n <= LPM_MAX_PREFIX; n++) {
     99  1.1  christos 		lpm_hmap_t *hmap = &lpm->prefix[n];
    100  1.1  christos 
    101  1.1  christos 		if (!hmap->hashsize) {
    102  1.1  christos 			KASSERT(!hmap->bucket);
    103  1.1  christos 			continue;
    104  1.1  christos 		}
    105  1.1  christos 		for (unsigned i = 0; i < hmap->hashsize; i++) {
    106  1.1  christos 			lpm_ent_t *entry = hmap->bucket[i];
    107  1.1  christos 
    108  1.1  christos 			while (entry) {
    109  1.1  christos 				lpm_ent_t *next = entry->next;
    110  1.1  christos 
    111  1.1  christos 				if (dtor) {
    112  1.1  christos 					dtor(arg, entry->key,
    113  1.1  christos 					    entry->len, entry->val);
    114  1.1  christos 				}
    115  1.1  christos 				kmem_free(entry,
    116  1.1  christos 				    offsetof(lpm_ent_t, key[entry->len]));
    117  1.1  christos 				entry = next;
    118  1.1  christos 			}
    119  1.1  christos 		}
    120  1.2     rmind 		kmem_free(hmap->bucket, hmap->hashsize * sizeof(lpm_ent_t *));
    121  1.1  christos 		hmap->bucket = NULL;
    122  1.1  christos 		hmap->hashsize = 0;
    123  1.1  christos 		hmap->nitems = 0;
    124  1.1  christos 	}
    125  1.1  christos 	memset(lpm->bitmask, 0, sizeof(lpm->bitmask));
    126  1.1  christos 	lpm->defval = NULL;
    127  1.1  christos }
    128  1.1  christos 
    129  1.1  christos void
    130  1.1  christos lpm_destroy(lpm_t *lpm)
    131  1.1  christos {
    132  1.1  christos 	lpm_clear(lpm, NULL, NULL);
    133  1.1  christos 	kmem_free(lpm, sizeof(*lpm));
    134  1.1  christos }
    135  1.1  christos 
    136  1.1  christos /*
    137  1.1  christos  * fnv1a_hash: Fowler-Noll-Vo hash function (FNV-1a variant).
    138  1.1  christos  */
    139  1.1  christos static uint32_t
    140  1.1  christos fnv1a_hash(const void *buf, size_t len)
    141  1.1  christos {
    142  1.1  christos 	uint32_t hash = 2166136261UL;
    143  1.1  christos 	const uint8_t *p = buf;
    144  1.1  christos 
    145  1.1  christos 	while (len--) {
    146  1.1  christos 		hash ^= *p++;
    147  1.1  christos 		hash *= 16777619U;
    148  1.1  christos 	}
    149  1.1  christos 	return hash;
    150  1.1  christos }
    151  1.1  christos 
    152  1.1  christos static bool
    153  1.1  christos hashmap_rehash(lpm_hmap_t *hmap, uint32_t size)
    154  1.1  christos {
    155  1.1  christos 	lpm_ent_t **bucket;
    156  1.1  christos 	uint32_t hashsize;
    157  1.1  christos 
    158  1.1  christos 	for (hashsize = 1; hashsize < size; hashsize <<= 1) {
    159  1.1  christos 		continue;
    160  1.1  christos 	}
    161  1.2     rmind 	bucket = kmem_zalloc(hashsize * sizeof(lpm_ent_t *), KM_SLEEP);
    162  1.1  christos 	if (bucket == NULL)
    163  1.1  christos 		return false;
    164  1.1  christos 	for (unsigned n = 0; n < hmap->hashsize; n++) {
    165  1.1  christos 		lpm_ent_t *list = hmap->bucket[n];
    166  1.1  christos 
    167  1.1  christos 		while (list) {
    168  1.1  christos 			lpm_ent_t *entry = list;
    169  1.1  christos 			uint32_t hash = fnv1a_hash(entry->key, entry->len);
    170  1.1  christos 			const size_t i = hash & (hashsize - 1);
    171  1.1  christos 
    172  1.1  christos 			list = entry->next;
    173  1.1  christos 			entry->next = bucket[i];
    174  1.1  christos 			bucket[i] = entry;
    175  1.1  christos 		}
    176  1.1  christos 	}
    177  1.1  christos 	if (hmap->bucket)
    178  1.2     rmind 		kmem_free(hmap->bucket, hmap->hashsize * sizeof(lpm_ent_t *));
    179  1.1  christos 	hmap->bucket = bucket;
    180  1.1  christos 	hmap->hashsize = hashsize;
    181  1.1  christos 	return true;
    182  1.1  christos }
    183  1.1  christos 
    184  1.1  christos static lpm_ent_t *
    185  1.1  christos hashmap_insert(lpm_hmap_t *hmap, const void *key, size_t len)
    186  1.1  christos {
    187  1.1  christos 	const uint32_t target = hmap->nitems + LPM_HASH_STEP;
    188  1.1  christos 	const size_t entlen = offsetof(lpm_ent_t, key[len]);
    189  1.1  christos 	uint32_t hash, i;
    190  1.1  christos 	lpm_ent_t *entry;
    191  1.1  christos 
    192  1.1  christos 	if (hmap->hashsize < target && !hashmap_rehash(hmap, target)) {
    193  1.1  christos 		return NULL;
    194  1.1  christos 	}
    195  1.1  christos 
    196  1.1  christos 	hash = fnv1a_hash(key, len);
    197  1.1  christos 	i = hash & (hmap->hashsize - 1);
    198  1.1  christos 	entry = hmap->bucket[i];
    199  1.1  christos 	while (entry) {
    200  1.1  christos 		if (entry->len == len && memcmp(entry->key, key, len) == 0) {
    201  1.1  christos 			return entry;
    202  1.1  christos 		}
    203  1.1  christos 		entry = entry->next;
    204  1.1  christos 	}
    205  1.1  christos 
    206  1.1  christos 	if ((entry = kmem_alloc(entlen, KM_SLEEP)) == NULL)
    207  1.1  christos 		return NULL;
    208  1.1  christos 
    209  1.1  christos 	memcpy(entry->key, key, len);
    210  1.1  christos 	entry->next = hmap->bucket[i];
    211  1.1  christos 	entry->len = len;
    212  1.1  christos 
    213  1.1  christos 	hmap->bucket[i] = entry;
    214  1.1  christos 	hmap->nitems++;
    215  1.1  christos 	return entry;
    216  1.1  christos }
    217  1.1  christos 
    218  1.1  christos static lpm_ent_t *
    219  1.1  christos hashmap_lookup(lpm_hmap_t *hmap, const void *key, size_t len)
    220  1.1  christos {
    221  1.1  christos 	const uint32_t hash = fnv1a_hash(key, len);
    222  1.1  christos 	const uint32_t i = hash & (hmap->hashsize - 1);
    223  1.1  christos 	lpm_ent_t *entry = hmap->bucket[i];
    224  1.1  christos 
    225  1.1  christos 	while (entry) {
    226  1.1  christos 		if (entry->len == len && memcmp(entry->key, key, len) == 0) {
    227  1.1  christos 			return entry;
    228  1.1  christos 		}
    229  1.1  christos 		entry = entry->next;
    230  1.1  christos 	}
    231  1.1  christos 	return NULL;
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos static int
    235  1.1  christos hashmap_remove(lpm_hmap_t *hmap, const void *key, size_t len)
    236  1.1  christos {
    237  1.1  christos 	const uint32_t hash = fnv1a_hash(key, len);
    238  1.1  christos 	const uint32_t i = hash & (hmap->hashsize - 1);
    239  1.1  christos 	lpm_ent_t *prev = NULL, *entry = hmap->bucket[i];
    240  1.1  christos 
    241  1.1  christos 	while (entry) {
    242  1.1  christos 		if (entry->len == len && memcmp(entry->key, key, len) == 0) {
    243  1.1  christos 			if (prev) {
    244  1.1  christos 				prev->next = entry->next;
    245  1.1  christos 			} else {
    246  1.1  christos 				hmap->bucket[i] = entry->next;
    247  1.1  christos 			}
    248  1.1  christos 			free(entry, M_TEMP);
    249  1.1  christos 			return 0;
    250  1.1  christos 		}
    251  1.1  christos 		prev = entry;
    252  1.1  christos 		entry = entry->next;
    253  1.1  christos 	}
    254  1.1  christos 	return -1;
    255  1.1  christos }
    256  1.1  christos 
    257  1.1  christos /*
    258  1.1  christos  * compute_prefix: given the address and prefix length, compute and
    259  1.1  christos  * return the address prefix.
    260  1.1  christos  */
    261  1.1  christos static inline void
    262  1.1  christos compute_prefix(const unsigned nwords, const uint32_t *addr,
    263  1.1  christos     unsigned preflen, uint32_t *prefix)
    264  1.1  christos {
    265  1.1  christos 	uint32_t addr2[4];
    266  1.1  christos 
    267  1.1  christos 	if ((uintptr_t)addr & 3) {
    268  1.1  christos 		/* Unaligned address: just copy for now. */
    269  1.1  christos 		memcpy(addr2, addr, nwords * 4);
    270  1.1  christos 		addr = addr2;
    271  1.1  christos 	}
    272  1.1  christos 	for (unsigned i = 0; i < nwords; i++) {
    273  1.1  christos 		if (preflen == 0) {
    274  1.1  christos 			prefix[i] = 0;
    275  1.1  christos 			continue;
    276  1.1  christos 		}
    277  1.1  christos 		if (preflen < 32) {
    278  1.1  christos 			uint32_t mask = htonl(0xffffffff << (32 - preflen));
    279  1.1  christos 			prefix[i] = addr[i] & mask;
    280  1.1  christos 			preflen = 0;
    281  1.1  christos 		} else {
    282  1.1  christos 			prefix[i] = addr[i];
    283  1.1  christos 			preflen -= 32;
    284  1.1  christos 		}
    285  1.1  christos 	}
    286  1.1  christos }
    287  1.1  christos 
    288  1.1  christos /*
    289  1.1  christos  * lpm_insert: insert the CIDR into the LPM table.
    290  1.1  christos  *
    291  1.1  christos  * => Returns zero on success and -1 on failure.
    292  1.1  christos  */
    293  1.1  christos int
    294  1.1  christos lpm_insert(lpm_t *lpm, const void *addr,
    295  1.1  christos     size_t len, unsigned preflen, void *val)
    296  1.1  christos {
    297  1.1  christos 	const unsigned nwords = LPM_TO_WORDS(len);
    298  1.1  christos 	uint32_t prefix[LPM_MAX_WORDS];
    299  1.1  christos 	lpm_ent_t *entry;
    300  1.1  christos 
    301  1.1  christos 	if (preflen == 0) {
    302  1.1  christos 		/* Default is a special case. */
    303  1.1  christos 		lpm->defval = val;
    304  1.1  christos 		return 0;
    305  1.1  christos 	}
    306  1.1  christos 	compute_prefix(nwords, addr, preflen, prefix);
    307  1.1  christos 	entry = hashmap_insert(&lpm->prefix[preflen], prefix, len);
    308  1.1  christos 	if (entry) {
    309  1.1  christos 		const unsigned n = --preflen >> 5;
    310  1.1  christos 		lpm->bitmask[n] |= 0x80000000U >> (preflen & 31);
    311  1.1  christos 		entry->val = val;
    312  1.1  christos 		return 0;
    313  1.1  christos 	}
    314  1.1  christos 	return -1;
    315  1.1  christos }
    316  1.1  christos 
    317  1.1  christos /*
    318  1.1  christos  * lpm_remove: remove the specified prefix.
    319  1.1  christos  */
    320  1.1  christos int
    321  1.1  christos lpm_remove(lpm_t *lpm, const void *addr, size_t len, unsigned preflen)
    322  1.1  christos {
    323  1.1  christos 	const unsigned nwords = LPM_TO_WORDS(len);
    324  1.1  christos 	uint32_t prefix[LPM_MAX_WORDS];
    325  1.1  christos 
    326  1.1  christos 	if (preflen == 0) {
    327  1.1  christos 		lpm->defval = NULL;
    328  1.1  christos 		return 0;
    329  1.1  christos 	}
    330  1.1  christos 	compute_prefix(nwords, addr, preflen, prefix);
    331  1.1  christos 	return hashmap_remove(&lpm->prefix[preflen], prefix, len);
    332  1.1  christos }
    333  1.1  christos 
    334  1.1  christos /*
    335  1.1  christos  * lpm_lookup: find the longest matching prefix given the IP address.
    336  1.1  christos  *
    337  1.1  christos  * => Returns the associated value on success or NULL on failure.
    338  1.1  christos  */
    339  1.1  christos void *
    340  1.1  christos lpm_lookup(lpm_t *lpm, const void *addr, size_t len)
    341  1.1  christos {
    342  1.1  christos 	const unsigned nwords = LPM_TO_WORDS(len);
    343  1.1  christos 	unsigned i, n = nwords;
    344  1.1  christos 	uint32_t prefix[LPM_MAX_WORDS];
    345  1.1  christos 
    346  1.1  christos 	while (n--) {
    347  1.1  christos 		uint32_t bitmask = lpm->bitmask[n];
    348  1.1  christos 
    349  1.1  christos 		while ((i = ffs(bitmask)) != 0) {
    350  1.1  christos 			const unsigned preflen = (32 * n) + (32 - --i);
    351  1.1  christos 			lpm_hmap_t *hmap = &lpm->prefix[preflen];
    352  1.1  christos 			lpm_ent_t *entry;
    353  1.1  christos 
    354  1.1  christos 			compute_prefix(nwords, addr, preflen, prefix);
    355  1.1  christos 			entry = hashmap_lookup(hmap, prefix, len);
    356  1.1  christos 			if (entry) {
    357  1.1  christos 				return entry->val;
    358  1.1  christos 			}
    359  1.1  christos 			bitmask &= ~(1U << i);
    360  1.1  christos 		}
    361  1.1  christos 	}
    362  1.1  christos 	return lpm->defval;
    363  1.1  christos }
    364  1.1  christos 
    365  1.1  christos #if !defined(_KERNEL)
    366  1.1  christos /*
    367  1.1  christos  * lpm_strtobin: convert CIDR string to the binary IP address and mask.
    368  1.1  christos  *
    369  1.1  christos  * => The address will be in the network byte order.
    370  1.1  christos  * => Returns 0 on success or -1 on failure.
    371  1.1  christos  */
    372  1.1  christos int
    373  1.1  christos lpm_strtobin(const char *cidr, void *addr, size_t *len, unsigned *preflen)
    374  1.1  christos {
    375  1.1  christos 	char *p, buf[INET6_ADDRSTRLEN];
    376  1.1  christos 
    377  1.1  christos 	strncpy(buf, cidr, sizeof(buf));
    378  1.1  christos 	buf[sizeof(buf) - 1] = '\0';
    379  1.1  christos 
    380  1.1  christos 	if ((p = strchr(buf, '/')) != NULL) {
    381  1.1  christos 		const ptrdiff_t off = p - buf;
    382  1.1  christos 		*preflen = atoi(&buf[off + 1]);
    383  1.1  christos 		buf[off] = '\0';
    384  1.1  christos 	} else {
    385  1.1  christos 		*preflen = LPM_MAX_PREFIX;
    386  1.1  christos 	}
    387  1.1  christos 
    388  1.1  christos 	if (inet_pton(AF_INET6, buf, addr) == 1) {
    389  1.1  christos 		*len = 16;
    390  1.1  christos 		return 0;
    391  1.1  christos 	}
    392  1.1  christos 	if (inet_pton(AF_INET, buf, addr) == 1) {
    393  1.1  christos 		if (*preflen == LPM_MAX_PREFIX) {
    394  1.1  christos 			*preflen = 32;
    395  1.1  christos 		}
    396  1.1  christos 		*len = 4;
    397  1.1  christos 		return 0;
    398  1.1  christos 	}
    399  1.1  christos 	return -1;
    400  1.1  christos }
    401  1.1  christos #endif
    402