lpm.c revision 1.4 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.4 chs __KERNEL_RCSID(0, "$NetBSD: lpm.c,v 1.4 2017/06/01 02:45:14 chs 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 for (unsigned n = 0; n < hmap->hashsize; n++) {
163 1.1 christos lpm_ent_t *list = hmap->bucket[n];
164 1.1 christos
165 1.1 christos while (list) {
166 1.1 christos lpm_ent_t *entry = list;
167 1.1 christos uint32_t hash = fnv1a_hash(entry->key, entry->len);
168 1.1 christos const size_t i = hash & (hashsize - 1);
169 1.1 christos
170 1.1 christos list = entry->next;
171 1.1 christos entry->next = bucket[i];
172 1.1 christos bucket[i] = entry;
173 1.1 christos }
174 1.1 christos }
175 1.1 christos if (hmap->bucket)
176 1.2 rmind kmem_free(hmap->bucket, hmap->hashsize * sizeof(lpm_ent_t *));
177 1.1 christos hmap->bucket = bucket;
178 1.1 christos hmap->hashsize = hashsize;
179 1.1 christos return true;
180 1.1 christos }
181 1.1 christos
182 1.1 christos static lpm_ent_t *
183 1.1 christos hashmap_insert(lpm_hmap_t *hmap, const void *key, size_t len)
184 1.1 christos {
185 1.1 christos const uint32_t target = hmap->nitems + LPM_HASH_STEP;
186 1.1 christos const size_t entlen = offsetof(lpm_ent_t, key[len]);
187 1.1 christos uint32_t hash, i;
188 1.1 christos lpm_ent_t *entry;
189 1.1 christos
190 1.1 christos if (hmap->hashsize < target && !hashmap_rehash(hmap, target)) {
191 1.1 christos return NULL;
192 1.1 christos }
193 1.1 christos
194 1.1 christos hash = fnv1a_hash(key, len);
195 1.1 christos i = hash & (hmap->hashsize - 1);
196 1.1 christos entry = hmap->bucket[i];
197 1.1 christos while (entry) {
198 1.1 christos if (entry->len == len && memcmp(entry->key, key, len) == 0) {
199 1.1 christos return entry;
200 1.1 christos }
201 1.1 christos entry = entry->next;
202 1.1 christos }
203 1.1 christos
204 1.4 chs entry = kmem_alloc(entlen, KM_SLEEP);
205 1.1 christos memcpy(entry->key, key, len);
206 1.1 christos entry->next = hmap->bucket[i];
207 1.1 christos entry->len = len;
208 1.1 christos
209 1.1 christos hmap->bucket[i] = entry;
210 1.1 christos hmap->nitems++;
211 1.1 christos return entry;
212 1.1 christos }
213 1.1 christos
214 1.1 christos static lpm_ent_t *
215 1.1 christos hashmap_lookup(lpm_hmap_t *hmap, const void *key, size_t len)
216 1.1 christos {
217 1.1 christos const uint32_t hash = fnv1a_hash(key, len);
218 1.1 christos const uint32_t i = hash & (hmap->hashsize - 1);
219 1.1 christos lpm_ent_t *entry = hmap->bucket[i];
220 1.1 christos
221 1.1 christos while (entry) {
222 1.1 christos if (entry->len == len && memcmp(entry->key, key, len) == 0) {
223 1.1 christos return entry;
224 1.1 christos }
225 1.1 christos entry = entry->next;
226 1.1 christos }
227 1.1 christos return NULL;
228 1.1 christos }
229 1.1 christos
230 1.1 christos static int
231 1.1 christos hashmap_remove(lpm_hmap_t *hmap, const void *key, size_t len)
232 1.1 christos {
233 1.1 christos const uint32_t hash = fnv1a_hash(key, len);
234 1.1 christos const uint32_t i = hash & (hmap->hashsize - 1);
235 1.1 christos lpm_ent_t *prev = NULL, *entry = hmap->bucket[i];
236 1.1 christos
237 1.1 christos while (entry) {
238 1.1 christos if (entry->len == len && memcmp(entry->key, key, len) == 0) {
239 1.1 christos if (prev) {
240 1.1 christos prev->next = entry->next;
241 1.1 christos } else {
242 1.1 christos hmap->bucket[i] = entry->next;
243 1.1 christos }
244 1.3 rmind kmem_free(entry, offsetof(lpm_ent_t, key[len]));
245 1.1 christos return 0;
246 1.1 christos }
247 1.1 christos prev = entry;
248 1.1 christos entry = entry->next;
249 1.1 christos }
250 1.1 christos return -1;
251 1.1 christos }
252 1.1 christos
253 1.1 christos /*
254 1.1 christos * compute_prefix: given the address and prefix length, compute and
255 1.1 christos * return the address prefix.
256 1.1 christos */
257 1.1 christos static inline void
258 1.1 christos compute_prefix(const unsigned nwords, const uint32_t *addr,
259 1.1 christos unsigned preflen, uint32_t *prefix)
260 1.1 christos {
261 1.1 christos uint32_t addr2[4];
262 1.1 christos
263 1.1 christos if ((uintptr_t)addr & 3) {
264 1.1 christos /* Unaligned address: just copy for now. */
265 1.1 christos memcpy(addr2, addr, nwords * 4);
266 1.1 christos addr = addr2;
267 1.1 christos }
268 1.1 christos for (unsigned i = 0; i < nwords; i++) {
269 1.1 christos if (preflen == 0) {
270 1.1 christos prefix[i] = 0;
271 1.1 christos continue;
272 1.1 christos }
273 1.1 christos if (preflen < 32) {
274 1.1 christos uint32_t mask = htonl(0xffffffff << (32 - preflen));
275 1.1 christos prefix[i] = addr[i] & mask;
276 1.1 christos preflen = 0;
277 1.1 christos } else {
278 1.1 christos prefix[i] = addr[i];
279 1.1 christos preflen -= 32;
280 1.1 christos }
281 1.1 christos }
282 1.1 christos }
283 1.1 christos
284 1.1 christos /*
285 1.1 christos * lpm_insert: insert the CIDR into the LPM table.
286 1.1 christos *
287 1.1 christos * => Returns zero on success and -1 on failure.
288 1.1 christos */
289 1.1 christos int
290 1.1 christos lpm_insert(lpm_t *lpm, const void *addr,
291 1.1 christos size_t len, unsigned preflen, void *val)
292 1.1 christos {
293 1.1 christos const unsigned nwords = LPM_TO_WORDS(len);
294 1.1 christos uint32_t prefix[LPM_MAX_WORDS];
295 1.1 christos lpm_ent_t *entry;
296 1.1 christos
297 1.1 christos if (preflen == 0) {
298 1.1 christos /* Default is a special case. */
299 1.1 christos lpm->defval = val;
300 1.1 christos return 0;
301 1.1 christos }
302 1.1 christos compute_prefix(nwords, addr, preflen, prefix);
303 1.1 christos entry = hashmap_insert(&lpm->prefix[preflen], prefix, len);
304 1.1 christos if (entry) {
305 1.1 christos const unsigned n = --preflen >> 5;
306 1.1 christos lpm->bitmask[n] |= 0x80000000U >> (preflen & 31);
307 1.1 christos entry->val = val;
308 1.1 christos return 0;
309 1.1 christos }
310 1.1 christos return -1;
311 1.1 christos }
312 1.1 christos
313 1.1 christos /*
314 1.1 christos * lpm_remove: remove the specified prefix.
315 1.1 christos */
316 1.1 christos int
317 1.1 christos lpm_remove(lpm_t *lpm, const void *addr, size_t len, unsigned preflen)
318 1.1 christos {
319 1.1 christos const unsigned nwords = LPM_TO_WORDS(len);
320 1.1 christos uint32_t prefix[LPM_MAX_WORDS];
321 1.1 christos
322 1.1 christos if (preflen == 0) {
323 1.1 christos lpm->defval = NULL;
324 1.1 christos return 0;
325 1.1 christos }
326 1.1 christos compute_prefix(nwords, addr, preflen, prefix);
327 1.1 christos return hashmap_remove(&lpm->prefix[preflen], prefix, len);
328 1.1 christos }
329 1.1 christos
330 1.1 christos /*
331 1.1 christos * lpm_lookup: find the longest matching prefix given the IP address.
332 1.1 christos *
333 1.1 christos * => Returns the associated value on success or NULL on failure.
334 1.1 christos */
335 1.1 christos void *
336 1.1 christos lpm_lookup(lpm_t *lpm, const void *addr, size_t len)
337 1.1 christos {
338 1.1 christos const unsigned nwords = LPM_TO_WORDS(len);
339 1.1 christos unsigned i, n = nwords;
340 1.1 christos uint32_t prefix[LPM_MAX_WORDS];
341 1.1 christos
342 1.1 christos while (n--) {
343 1.1 christos uint32_t bitmask = lpm->bitmask[n];
344 1.1 christos
345 1.1 christos while ((i = ffs(bitmask)) != 0) {
346 1.1 christos const unsigned preflen = (32 * n) + (32 - --i);
347 1.1 christos lpm_hmap_t *hmap = &lpm->prefix[preflen];
348 1.1 christos lpm_ent_t *entry;
349 1.1 christos
350 1.1 christos compute_prefix(nwords, addr, preflen, prefix);
351 1.1 christos entry = hashmap_lookup(hmap, prefix, len);
352 1.1 christos if (entry) {
353 1.1 christos return entry->val;
354 1.1 christos }
355 1.1 christos bitmask &= ~(1U << i);
356 1.1 christos }
357 1.1 christos }
358 1.1 christos return lpm->defval;
359 1.1 christos }
360 1.1 christos
361 1.1 christos #if !defined(_KERNEL)
362 1.1 christos /*
363 1.1 christos * lpm_strtobin: convert CIDR string to the binary IP address and mask.
364 1.1 christos *
365 1.1 christos * => The address will be in the network byte order.
366 1.1 christos * => Returns 0 on success or -1 on failure.
367 1.1 christos */
368 1.1 christos int
369 1.1 christos lpm_strtobin(const char *cidr, void *addr, size_t *len, unsigned *preflen)
370 1.1 christos {
371 1.1 christos char *p, buf[INET6_ADDRSTRLEN];
372 1.1 christos
373 1.1 christos strncpy(buf, cidr, sizeof(buf));
374 1.1 christos buf[sizeof(buf) - 1] = '\0';
375 1.1 christos
376 1.1 christos if ((p = strchr(buf, '/')) != NULL) {
377 1.1 christos const ptrdiff_t off = p - buf;
378 1.1 christos *preflen = atoi(&buf[off + 1]);
379 1.1 christos buf[off] = '\0';
380 1.1 christos } else {
381 1.1 christos *preflen = LPM_MAX_PREFIX;
382 1.1 christos }
383 1.1 christos
384 1.1 christos if (inet_pton(AF_INET6, buf, addr) == 1) {
385 1.1 christos *len = 16;
386 1.1 christos return 0;
387 1.1 christos }
388 1.1 christos if (inet_pton(AF_INET, buf, addr) == 1) {
389 1.1 christos if (*preflen == LPM_MAX_PREFIX) {
390 1.1 christos *preflen = 32;
391 1.1 christos }
392 1.1 christos *len = 4;
393 1.1 christos return 0;
394 1.1 christos }
395 1.1 christos return -1;
396 1.1 christos }
397 1.1 christos #endif
398