1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
|
/* $OpenBSD: art.c,v 1.8 2015/11/12 14:29:04 mpi Exp $ */
/*
* Copyright (c) 2015 Martin Pieuchot
* Copyright (c) 2001 Yoichi Hariguchi
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Allotment Routing Table (ART).
*
* Yoichi Hariguchi paper can be found at:
* http://www.hariguchi.org/art/art.pdf
*/
#ifndef _KERNEL
#include "kern_compat.h"
#else
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/pool.h>
#include <sys/socket.h>
#endif
#include <net/art.h>
#define ISLEAF(e) (((unsigned long)(e).node & 1) == 0)
#define SUBTABLE(e) (((struct art_table *)((unsigned long)(e).child & ~1)))
#define ASNODE(t) ((struct art_node *)((unsigned long)(t) | 1))
/*
* Allotment Table.
*/
struct art_table {
struct art_table *at_parent; /* Parent table */
uint32_t at_index; /* Index in the parent table */
uint32_t at_minfringe; /* Index that fringe begins */
uint32_t at_level; /* Level of the table */
uint8_t at_bits; /* Stride length of the table */
uint8_t at_offset; /* Sum of parents' stride len */
/*
* Items stored in the heap are pointers to nodes, in the leaf
* case, or tables otherwise. One exception is index 0 which
* is a route counter.
*/
union {
struct art_node *node;
struct art_table *child;
unsigned long count;
} *at_heap; /* Array of 2^(slen+1) items */
};
#define at_refcnt at_heap[0].count/* Refcounter (1 per different route) */
#define at_default at_heap[1].node /* Default route (was in parent heap) */
/* Heap size for an ART table of stride length ``slen''. */
#define AT_HEAPSIZE(slen) ((1 << ((slen) + 1)) * sizeof(void *))
int art_bindex(struct art_table *, uint8_t *, int);
void art_allot(struct art_table *at, int, struct art_node *,
struct art_node *);
struct art_table *art_table_get(struct art_root *, struct art_table *,
int);
struct art_table *art_table_put(struct art_root *, struct art_table *);
struct art_node *art_table_insert(struct art_root *, struct art_table *,
int, struct art_node *);
struct art_node *art_table_delete(struct art_root *, struct art_table *,
int, struct art_node *);
void art_table_ref(struct art_root *, struct art_table *);
int art_table_free(struct art_root *, struct art_table *);
int art_table_walk(struct art_root *, struct art_table *,
int (*f)(struct art_node *, void *), void *);
int at_init = 0;
struct pool at_pool;
struct pool at_heap_8_pool;
struct pool at_heap_16_pool;
/*
* Per routing table initialization API function.
*/
struct art_root *
art_alloc(unsigned int rtableid, int off)
{
struct art_root *ar;
int i;
if (!at_init) {
at_init = 1;
pool_init(&at_pool, sizeof(struct art_table), 0, 0, 0,
"art_table", NULL);
pool_init(&at_heap_8_pool, AT_HEAPSIZE(8), 0, 0, 0,
"art_heap8", NULL);
pool_init(&at_heap_16_pool, AT_HEAPSIZE(16), 0, 0, 0,
"art_heap16", NULL);
}
ar = malloc(sizeof(*ar), M_RTABLE, M_NOWAIT|M_ZERO);
if (ar == NULL)
return (NULL);
/* XXX using the offset is a hack. */
switch (off) {
case 4: /* AF_INET && AF_MPLS */
ar->ar_alen = 32;
ar->ar_nlvl = 3;
ar->ar_bits[0] = 16;
ar->ar_bits[1] = 8;
ar->ar_bits[2] = 8;
break;
#ifdef INET6
case 8: /* AF_INET6 */
ar->ar_alen = 128;
ar->ar_nlvl = 16;
for (i = 0; i < ar->ar_nlvl; i++)
ar->ar_bits[i] = 8;
break;
#endif /* INET6 */
default:
printf("%s: unknown offset %d\n", __func__, off);
free(ar, M_RTABLE, sizeof(*ar));
return (NULL);
}
ar->ar_off = off;
ar->ar_rtableid = rtableid;
return (ar);
}
/*
* Return 1 if ``old'' and ``new`` are identical, 0 otherwise.
*/
static inline int
art_check_duplicate(struct art_root *ar, struct art_node *old,
struct art_node *new)
{
if (old == NULL)
return (0);
if (old->an_plen == new->an_plen)
return (1);
return (0);
}
/*
* Return the base index of the part of ``addr'' and ``plen''
* corresponding to the range covered by the table ``at''.
*
* In other words, this function take the multi-level (complete)
* address ``addr'' and prefix length ``plen'' and return the
* single level base index for the table ``at''.
*
* For example with an address size of 32bit divided into four
* 8bit-long tables, there's a maximum of 4 base indexes if the
* prefix length is > 24.
*/
int
art_bindex(struct art_table *at, uint8_t *addr, int plen)
{
uint8_t boff, bend;
uint32_t k;
if (plen < at->at_offset || plen > (at->at_offset + at->at_bits))
return (-1);
/*
* We are only interested in the part of the prefix length
* corresponding to the range of this table.
*/
plen -= at->at_offset;
/*
* Jump to the first byte of the address containing bits
* covered by this table.
*/
addr += (at->at_offset / 8);
/* ``at'' covers the bit range between ``boff'' & ``bend''. */
boff = (at->at_offset % 8);
bend = (at->at_bits + boff);
KASSERT(bend <= 32);
if (bend > 24) {
k = (addr[0] & ((1 << (8 - boff)) - 1)) << (bend - 8);
k |= addr[1] << (bend - 16);
k |= addr[2] << (bend - 24);
k |= addr[3] >> (32 - bend);
} else if (bend > 16) {
k = (addr[0] & ((1 << (8 - boff)) - 1)) << (bend - 8);
k |= addr[1] << (bend - 16);
k |= addr[2] >> (24 - bend);
} else if (bend > 8) {
k = (addr[0] & ((1 << (8 - boff)) - 1)) << (bend - 8);
k |= addr[1] >> (16 - bend);
} else {
k = (addr[0] >> (8 - bend)) & ((1 << at->at_bits) - 1);
}
/*
* Single level base index formula:
*/
return ((k >> (at->at_bits - plen)) + (1 << plen));
}
/*
* Single level lookup function.
*
* Return the fringe index of the part of ``addr''
* corresponding to the range covered by the table ``at''.
*/
static inline int
art_findex(struct art_table *at, uint8_t *addr)
{
return art_bindex(at, addr, (at->at_offset + at->at_bits));
}
/*
* (Non-perfect) lookup API function.
*
* Return the best existing match for a destination.
*/
struct art_node *
art_match(struct art_root *ar, uint8_t *addr)
{
struct art_table *at;
struct art_node *dflt = NULL;
int j;
at = ar->ar_root;
if (at == NULL)
return (NULL);
/*
* Iterate until we find a leaf.
*/
while (1) {
/*
* Rember the default route of this table in case
* we do not find a better matching route.
*/
if (at->at_default != NULL)
dflt = at->at_default;
/* Do a single level route lookup. */
j = art_findex(at, addr);
/* If this is a leaf we're done. */
if (ISLEAF(at->at_heap[j]))
break;
at = SUBTABLE(at->at_heap[j]);
}
if (at->at_heap[j].node != NULL)
return (at->at_heap[j].node);
return (dflt);
}
/*
* Perfect lookup API function.
*
* Return a perfect match for a destination/prefix-length pair or NULL if
* it does not exist.
*/
struct art_node *
art_lookup(struct art_root *ar, uint8_t *addr, int plen)
{
struct art_table *at;
struct art_node *an;
int i, j;
KASSERT(plen >= 0 && plen <= ar->ar_alen);
at = ar->ar_root;
if (at == NULL)
return (NULL);
/* Default route */
if (plen == 0)
return (at->at_default);
/*
* If the prefix length is smaller than the sum of
* the stride length at this level the entry must
* be in the current table.
*/
while (plen > (at->at_offset + at->at_bits)) {
/* Do a single level route lookup. */
j = art_findex(at, addr);
/* A leaf is a match, but not a perfect one. */
if (ISLEAF(at->at_heap[j]))
return (NULL);
at = SUBTABLE(at->at_heap[j]);
}
i = art_bindex(at, addr, plen);
if (i == -1)
return (NULL);
if (!ISLEAF(at->at_heap[i]))
an = SUBTABLE(at->at_heap[i])->at_default;
else
an = at->at_heap[i].node;
return (an);
}
/*
* Insertion API function.
*
* Insert the given node or return an existing one if a node with the
* same destination/mask pair is already present.
*/
struct art_node *
art_insert(struct art_root *ar, struct art_node *an, uint8_t *addr, int plen)
{
struct art_table *at;
int i, j;
KASSERT(plen >= 0 && plen <= ar->ar_alen);
at = ar->ar_root;
if (at == NULL) {
at = art_table_get(ar, NULL, -1);
if (at == NULL)
return (NULL);
ar->ar_root = at;
}
/* Default route */
if (plen == 0) {
art_table_ref(ar, at);
at->at_default = an;
return (an);
}
/*
* If the prefix length is smaller than the sum of
* the stride length at this level the entry must
* be in the current table.
*/
while (plen > (at->at_offset + at->at_bits)) {
/* Do a single level route lookup. */
j = art_findex(at, addr);
/*
* If the node corresponding to the fringe index is
* a leaf we need to allocate a subtable. The route
* entry of this node will then become the default
* route of the subtable.
*/
if (ISLEAF(at->at_heap[j])) {
struct art_table *child;
child = art_table_get(ar, at, j);
if (child == NULL)
return (NULL);
art_table_ref(ar, at);
at->at_heap[j].node = ASNODE(child);
}
at = SUBTABLE(at->at_heap[j]);
}
i = art_bindex(at, addr, plen);
if (i == -1)
return (NULL);
return (art_table_insert(ar, at, i, an));
}
/*
* Single level insertion.
*/
struct art_node *
art_table_insert(struct art_root *ar, struct art_table *at, int i,
struct art_node *an)
{
struct art_node *prev;
if (!ISLEAF(at->at_heap[i]))
prev = SUBTABLE(at->at_heap[i])->at_default;
else
prev = at->at_heap[i].node;
if (art_check_duplicate(ar, prev, an))
return (prev);
art_table_ref(ar, at);
/*
* If the index `i' of the route that we are inserting is not
* a fringe index, we need to allot this new route pointer to
* all the corresponding fringe indices.
*/
if (i < at->at_minfringe)
art_allot(at, i, prev, an);
else if (!ISLEAF(at->at_heap[i]))
SUBTABLE(at->at_heap[i])->at_default = an;
else
at->at_heap[i].node = an;
return (an);
}
/*
* Deletion API function.
*/
struct art_node *
art_delete(struct art_root *ar, struct art_node *an, uint8_t *addr, int plen)
{
struct art_table *at;
struct art_node *dflt;
int i, j;
KASSERT(plen >= 0 && plen <= ar->ar_alen);
at = ar->ar_root;
if (at == NULL)
return (NULL);
/* Default route */
if (plen == 0) {
dflt = at->at_default;
at->at_default = NULL;
art_table_free(ar, at);
return (dflt);
}
/*
* If the prefix length is smaller than the sum of
* the stride length at this level the entry must
* be in the current table.
*/
while (plen > (at->at_offset + at->at_bits)) {
/* Do a single level route lookup. */
j = art_findex(at, addr);
/* If this is a leaf, there is no route to delete. */
if (ISLEAF(at->at_heap[j]))
return (NULL);
at = SUBTABLE(at->at_heap[j]);
}
i = art_bindex(at, addr, plen);
if (i == -1)
return (NULL);
return (art_table_delete(ar, at, i, an));
}
/*
* Single level deletion.
*/
struct art_node *
art_table_delete(struct art_root *ar, struct art_table *at, int i,
struct art_node *node)
{
struct art_node *next;
#ifdef DIAGNOSTIC
struct art_node *prev;
if (!ISLEAF(at->at_heap[i]))
prev = SUBTABLE(at->at_heap[i])->at_default;
else
prev = at->at_heap[i].node;
KASSERT(prev == node);
#endif
/* We are removing an entry from this table. */
if (art_table_free(ar, at))
return (node);
/* Get the next most specific route for the index `i'. */
if ((i >> 1) > 1)
next = at->at_heap[i >> 1].node;
else
next = NULL;
/*
* If the index `i' of the route that we are removing is not
* a fringe index, we need to allot the next most specific
* route pointer to all the corresponding fringe indices.
*/
if (i < at->at_minfringe)
art_allot(at, i, node, next);
else if (!ISLEAF(at->at_heap[i]))
SUBTABLE(at->at_heap[i])->at_default = next;
else
at->at_heap[i].node = next;
return (node);
}
void
art_table_ref(struct art_root *ar, struct art_table *at)
{
at->at_refcnt++;
}
int
art_table_free(struct art_root *ar, struct art_table *at)
{
if (--at->at_refcnt == 0) {
/*
* Garbage collect this table and all its parents
* that are empty.
*/
do {
at = art_table_put(ar, at);
} while (at != NULL && --at->at_refcnt == 0);
return (1);
}
return (0);
}
/*
* Iteration API function.
*/
int
art_walk(struct art_root *ar, int (*f)(struct art_node *, void *), void *arg)
{
struct art_table *at;
int error;
at = ar->ar_root;
if (at == NULL)
return (0);
/*
* The default route should be processed here because the root
* table does not have a parent.
*/
if (at->at_default != NULL) {
error = (*f)(at->at_default, arg);
if (error)
return (error);
}
return (art_table_walk(ar, at, f, arg));
}
int
art_table_walk(struct art_root *ar, struct art_table *at,
int (*f)(struct art_node *, void *), void *arg)
{
struct art_node *next, *an = NULL;
int i, j, error = 0;
uint32_t maxfringe = (at->at_minfringe << 1);
/* Prevent this table to be freed while we're manipulating it. */
art_table_ref(ar, at);
/*
* Iterate non-fringe nodes in ``natural'' order.
*/
for (j = 1; j < at->at_minfringe; j += 2) {
/*
* The default route (index 1) is processed by the
* parent table (where it belongs) otherwise it could
* be processed more than once.
*/
for (i = max(j, 2); i < at->at_minfringe; i <<= 1) {
next = at->at_heap[i >> 1].node;
an = at->at_heap[i].node;
if ((an != NULL) && (an != next)) {
error = (*f)(an, arg);
if (error)
goto out;
}
}
}
/*
* Iterate fringe nodes.
*/
for (i = at->at_minfringe; i < maxfringe; i++) {
next = at->at_heap[i >> 1].node;
if (!ISLEAF(at->at_heap[i]))
an = SUBTABLE(at->at_heap[i])->at_default;
else
an = at->at_heap[i].node;
if ((an != NULL) && (an != next)) {
error = (*f)(an, arg);
if (error)
goto out;
}
if (ISLEAF(at->at_heap[i]))
continue;
error = art_table_walk(ar, SUBTABLE(at->at_heap[i]), f, arg);
if (error)
break;
}
out:
art_table_free(ar, at);
return (error);
}
/*
* Create a table and use the given index to set its default route.
*
* Note: This function does not modify the root or the parent.
*/
struct art_table *
art_table_get(struct art_root *ar, struct art_table *parent, int j)
{
struct art_table *at;
void *at_heap;
uint32_t lvl;
KASSERT(j != 0 && j != 1);
KASSERT(parent != NULL || j == -1);
if (parent != NULL)
lvl = parent->at_level + 1;
else
lvl = 0;
KASSERT(lvl < ar->ar_nlvl);
at = pool_get(&at_pool, PR_NOWAIT|PR_ZERO);
if (at == NULL)
return (NULL);
switch (AT_HEAPSIZE(ar->ar_bits[lvl])) {
case AT_HEAPSIZE(8):
at_heap = pool_get(&at_heap_8_pool, PR_NOWAIT|PR_ZERO);
break;
case AT_HEAPSIZE(16):
at_heap = pool_get(&at_heap_16_pool, PR_NOWAIT|PR_ZERO);
break;
default:
panic("incorrect stride length %u", ar->ar_bits[lvl]);
}
if (at_heap == NULL) {
pool_put(&at_pool, at);
return (NULL);
}
at->at_parent = parent;
at->at_index = j;
at->at_minfringe = (1 << ar->ar_bits[lvl]);
at->at_level = lvl;
at->at_bits = ar->ar_bits[lvl];
at->at_heap = at_heap;
at->at_refcnt = 0;
if (parent != NULL) {
at->at_default = parent->at_heap[j].node;
at->at_offset = (parent->at_offset + parent->at_bits);
}
return (at);
}
/*
* Delete a table and use its index to restore its parent's default route.
*
* Note: Modify its parent to unlink the table from it.
*/
struct art_table *
art_table_put(struct art_root *ar, struct art_table *at)
{
struct art_table *parent = at->at_parent;
uint32_t lvl = at->at_level;
uint32_t j = at->at_index;
KASSERT(j != 0 && j != 1);
KASSERT(parent != NULL || j == -1);
if (parent != NULL) {
KASSERT(lvl == parent->at_level + 1);
KASSERT(parent->at_refcnt >= 1);
/* Give the route back to its parent. */
parent->at_heap[j].node = at->at_default;
} else {
ar->ar_root = NULL;
}
switch (AT_HEAPSIZE(ar->ar_bits[lvl])) {
case AT_HEAPSIZE(8):
pool_put(&at_heap_8_pool, at->at_heap);
break;
case AT_HEAPSIZE(16):
pool_put(&at_heap_16_pool, at->at_heap);
break;
default:
panic("incorrect stride length %u", ar->ar_bits[lvl]);
}
pool_put(&at_pool, at);
return (parent);
}
/*
* Substitute a node by another in the subtree whose root index is given.
*
* This function iterates on the table ``at'' at index ``i'' until no
* more ``old'' node can be replaced by ``new''.
*
* This function was originally written by Don Knuth in CWEB. The
* complicated ``goto''s are the result of expansion of the two
* following recursions:
*
* art_allot(at, i, old, new)
* {
* int k = i;
* if (at->at_heap[k] == old)
* at->at_heap[k] = new;
* if (k >= at->at_minfringe)
* return;
* k <<= 1;
* art_allot(at, k, old, new);
* k++;
* art_allot(at, k, old, new);
* }
*/
void
art_allot(struct art_table *at, int i, struct art_node *old,
struct art_node *new)
{
int k = i;
KASSERT(i < at->at_minfringe);
again:
k <<= 1;
if (k < at->at_minfringe)
goto nonfringe;
/* Change fringe nodes. */
while (1) {
if (!ISLEAF(at->at_heap[k])) {
if (SUBTABLE(at->at_heap[k])->at_default == old) {
SUBTABLE(at->at_heap[k])->at_default = new;
}
} else if (at->at_heap[k].node == old) {
at->at_heap[k].node = new;
}
if (k % 2)
goto moveup;
k++;
}
nonfringe:
if (at->at_heap[k].node == old)
goto again;
moveon:
if (k % 2)
goto moveup;
k++;
goto nonfringe;
moveup:
k >>= 1;
at->at_heap[k].node = new;
/* Change non-fringe node. */
if (k != i)
goto moveon;
}
|