Repository navigation
Expand file tree
/
Copy pathart.h
More file actions
166 lines (140 loc) · 4.55 KB
/
Copy pathart.h
File metadata and controls
166 lines (140 loc) · 4.55 KB
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
/* $OpenBSD: art.h,v 1.29 2026/04/23 01:28:03 jsg Exp $ */
/*
* Copyright (c) 2015 Martin Pieuchot
*
* 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.
*/
#ifndef _NET_ART_H_
#define _NET_ART_H_
/*
* Allotment Routing Table (ART)
*
* Yoichi Hariguchi paper can be found at:
* http://www.hariguchi.org/art/art.pdf
*
* Locking:
*
* Modification (ie, art_insert or art_delete) and iteration
* (art_iter_next, etc) over the ART must be serialised by the caller.
* Lookups (ie, art_match and art_lookup) run within an SMR critical
* section.
*
* Iteration requires serialisation as it manipulates the reference
* counts on tables as it traverses the tree. The iterator maintains
* these references until it runs out of entries. This allows code
* iterating over the ART to release locks in between calls to
* art_iter_open and art_iter_next. The references may be dropped
* early with art_iter_close.
*
* Note, the iterator does not hold a reference to the art_node
* structure or the data hanging off the an_value pointer, they must
* be accounted for separately or their use must be serialised with
* art_delete.
*/
typedef uintptr_t art_heap_entry;
/*
* Root of the ART, equivalent to the radix head.
*/
struct art {
art_heap_entry *art_root;
const unsigned int *art_levels;
unsigned int art_nlevels;
unsigned int art_alen;
};
/*
* Allotment Table.
*/
struct art_table {
art_heap_entry *at_heap;
struct art_table *at_parent; /* Parent table */
unsigned int at_index; /* Index in the parent table */
unsigned int at_minfringe; /* Index that fringe begins */
unsigned int at_level; /* Level of the table */
unsigned int at_bits; /* Stride length of the table */
unsigned int at_offset; /* Sum of parents' stride len */
unsigned int at_refcnt;
};
#define ART_HEAP_IDX_TABLE 0
#define ART_HEAP_IDX_DEFAULT 1
#define AT_HEAPSIZE(bits) ((1 << ((bits) + 1)) * sizeof(art_heap_entry))
/*
* A node is the internal representation of a route entry.
*/
struct art_node {
void *an_value;
union {
struct art_node *an__gc;
uint8_t an__addr[16];
} an__u;
#define an_gc an__u.an__gc
#define an_addr an__u.an__addr
unsigned int an_plen;
};
static inline struct art_table *
art_heap_to_table(art_heap_entry *heap)
{
return ((struct art_table *)heap[ART_HEAP_IDX_TABLE]);
}
static inline int
art_heap_entry_is_node(art_heap_entry ahe)
{
return ((ahe & 1UL) == 0);
}
static inline struct art_node *
art_heap_entry_to_node(art_heap_entry ahe)
{
return ((struct art_node *)ahe);
}
static inline art_heap_entry *
art_heap_entry_to_heap(art_heap_entry ahe)
{
return ((art_heap_entry *)(ahe & ~1UL));
}
static inline art_heap_entry
art_node_to_heap_entry(struct art_node *an)
{
return ((art_heap_entry)an);
}
static inline art_heap_entry
art_heap_to_heap_entry(art_heap_entry *heap)
{
return ((art_heap_entry)heap | 1UL);
}
#ifdef _KERNEL
void art_boot(void);
struct art *art_alloc(unsigned int);
void art_init(struct art *, unsigned int);
struct art_node *art_insert(struct art *, struct art_node *);
struct art_node *art_delete(struct art *, const void *, unsigned int);
struct art_node *art_match(struct art *, const void *);
struct art_node *art_lookup(struct art *, const void *, unsigned int);
int art_is_empty(struct art *);
struct art_node *art_get(const uint8_t *, unsigned int);
void art_node_init(struct art_node *,
const uint8_t *, unsigned int);
void art_put(struct art_node *);
struct art_iter {
struct art *ai_art;
struct art_table *ai_table;
unsigned int ai_j;
unsigned int ai_i;
};
struct art_node *art_iter_open(struct art *, struct art_iter *);
struct art_node *art_iter_next(struct art_iter *);
void art_iter_close(struct art_iter *);
#define ART_FOREACH(_an, _art, _ai) \
for ((_an) = art_iter_open((_art), (_ai)); \
(_an) != NULL; \
(_an) = art_iter_next((_ai)))
#endif /* _KERNEL */
#endif /* _NET_ART_H_ */