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