-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.go
565 lines (452 loc) · 14.9 KB
/
node.go
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
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT
package bart
import (
"net/netip"
"slices"
"github.com/bits-and-blooms/bitset"
)
const (
strideLen = 8 // octet
maxTreeDepth = 128 / strideLen // 16
maxNodeChildren = 1 << strideLen // 256
maxNodePrefixes = 1 << (strideLen + 1) // 512
)
// zero value, used manifold
var zeroPath [16]byte
// node is a level node in the multibit-trie.
// A node has prefixes and children.
//
// The prefixes form a complete binary tree, see the artlookup.pdf
// paper in the doc folder to understand the data structure.
//
// In contrast to the ART algorithm, popcount-compressed slices are used
// instead of fixed-size arrays.
//
// The array slots are also not pre-allocated as in the ART algorithm,
// but backtracking is used for the longest-prefix-match.
//
// The lookup is then slower by a factor of about 2, but this is
// the intended trade-off to prevent memory consumption from exploding.
type node[V any] struct {
prefixesBitset *bitset.BitSet
childrenBitset *bitset.BitSet
// popcount compressed slices
prefixes []V
children []*node[V]
}
// newNode, BitSets have to be initialized.
func newNode[V any]() *node[V] {
return &node[V]{
prefixesBitset: bitset.New(0), // init BitSet
childrenBitset: bitset.New(0), // init BitSet
}
}
// isEmpty returns true if node has neither prefixes nor children.
func (n *node[V]) isEmpty() bool {
return len(n.prefixes) == 0 && len(n.children) == 0
}
// ################## prefixes ################################
// prefixRank, Rank() is the key of the popcount compression algorithm,
// mapping between bitset index and slice index.
func (n *node[V]) prefixRank(idx uint) int {
// adjust offset by one to slice index
return int(n.prefixesBitset.Rank(idx)) - 1
}
// insertPrefix adds the route for baseIdx, with value val.
// If the value already exists, overwrite it with val and return false.
func (n *node[V]) insertPrefix(idx uint, val V) (ok bool) {
// prefix exists, overwrite val
if n.prefixesBitset.Test(idx) {
n.prefixes[n.prefixRank(idx)] = val
return false
}
// new, insert into bitset and slice
n.prefixesBitset.Set(idx)
n.prefixes = slices.Insert(n.prefixes, n.prefixRank(idx), val)
return true
}
// deletePrefix removes the route octet/prefixLen.
// Returns false if there was no prefix to delete.
func (n *node[V]) deletePrefix(octet byte, prefixLen int) (ok bool) {
idx := pfxToIdx(octet, prefixLen)
// no route entry
if !n.prefixesBitset.Test(idx) {
return false
}
rnk := n.prefixRank(idx)
// delete from slice
n.prefixes = slices.Delete(n.prefixes, rnk, rnk+1)
// delete from bitset, followed by Compact to reduce memory consumption
n.prefixesBitset.Clear(idx)
n.prefixesBitset.Compact()
return true
}
// updatePrefix, update or set the value at prefix via callback. The new value returned
// and a bool wether the prefix was already present in the node.
func (n *node[V]) updatePrefix(octet byte, prefixLen int, cb func(V, bool) V) (newVal V, wasPresent bool) {
// calculate idx once
idx := pfxToIdx(octet, prefixLen)
var rnk int
// if prefix is set, get current value
var oldVal V
if wasPresent = n.prefixesBitset.Test(idx); wasPresent {
rnk = n.prefixRank(idx)
oldVal = n.prefixes[rnk]
}
// callback function to get updated or new value
newVal = cb(oldVal, wasPresent)
// prefix is already set, update and return value
if wasPresent {
n.prefixes[rnk] = newVal
return
}
// new prefix, insert into bitset ...
n.prefixesBitset.Set(idx)
// bitset has changed, recalc rank
rnk = n.prefixRank(idx)
// ... and insert value into slice
n.prefixes = slices.Insert(n.prefixes, rnk, newVal)
return
}
// lpm does a route lookup for idx in the 8-bit (stride) routing table
// at this depth and returns (baseIdx, value, true) if a matching
// longest prefix exists, or ok=false otherwise.
//
// backtracking is fast, it's just a bitset test and, if found, one popcount.
// max steps in backtracking is the stride length.
func (n *node[V]) lpm(idx uint) (baseIdx uint, val V, ok bool) {
// backtracking the CBT, make it as fast as possible
for baseIdx = idx; baseIdx > 0; baseIdx >>= 1 {
// practically it's getValueOK, but getValueOK is not inlined
if n.prefixesBitset.Test(baseIdx) {
return baseIdx, n.prefixes[n.prefixRank(baseIdx)], true
}
}
// not found (on this level)
return 0, val, false
}
// lpmTest for faster lpm tests without value returns
func (n *node[V]) lpmTest(idx uint) bool {
// backtracking the CBT
for idx := idx; idx > 0; idx >>= 1 {
if n.prefixesBitset.Test(idx) {
return true
}
}
return false
}
// getValueOK for idx..
func (n *node[V]) getValueOK(idx uint) (val V, ok bool) {
if n.prefixesBitset.Test(idx) {
return n.prefixes[n.prefixRank(idx)], true
}
return
}
// mustGetValue for idx, use it only after a successful bitset test.
// n.prefixesBitset.Test(idx) must be true
func (n *node[V]) mustGetValue(idx uint) V {
return n.prefixes[n.prefixRank(idx)]
}
// allStrideIndexes returns all baseIndexes set in this stride node in ascending order.
func (n *node[V]) allStrideIndexes(buffer []uint) []uint {
_, buffer = n.prefixesBitset.NextSetMany(0, buffer)
return buffer
}
// ################## children ################################
// childRank, Rank() is the key of the popcount compression algorithm,
// mapping between bitset index and slice index.
func (n *node[V]) childRank(octet byte) int {
// adjust offset by one to slice index
return int(n.childrenBitset.Rank(uint(octet))) - 1
}
// insertChild, insert the child
func (n *node[V]) insertChild(octet byte, child *node[V]) {
// child exists, overwrite it
if n.childrenBitset.Test(uint(octet)) {
n.children[n.childRank(octet)] = child
return
}
// new insert into bitset and slice
n.childrenBitset.Set(uint(octet))
n.children = slices.Insert(n.children, n.childRank(octet), child)
}
// deleteChild, delete the child at octet. It is valid to delete a non-existent child.
func (n *node[V]) deleteChild(octet byte) {
if !n.childrenBitset.Test(uint(octet)) {
return
}
rnk := n.childRank(octet)
// delete from slice
n.children = slices.Delete(n.children, rnk, rnk+1)
// delete from bitset, followed by Compact to reduce memory consumption
n.childrenBitset.Clear(uint(octet))
n.childrenBitset.Compact()
}
// getChild returns the child pointer for octet, or nil if none.
func (n *node[V]) getChild(octet byte) *node[V] {
if !n.childrenBitset.Test(uint(octet)) {
return nil
}
return n.children[n.childRank(octet)]
}
// allChildAddrs fills the buffer with the octets of all child nodes in ascending order,
// panics if the buffer isn't big enough.
func (n *node[V]) allChildAddrs(buffer []uint) []uint {
_, buffer = n.childrenBitset.NextSetMany(0, buffer)
return buffer
}
// #################### nodes #############################################
// eachLookupPrefix does an all prefix match in the 8-bit (stride) routing table
// at this depth and calls yield() for any matching CIDR.
func (n *node[V]) eachLookupPrefix(path [16]byte, depth int, is4 bool, octet byte, bits int, yield func(netip.Prefix, V) bool) bool {
// backtracking the CBT
for idx := pfxToIdx(octet, bits); idx > 0; idx >>= 1 {
if val, ok := n.getValueOK(idx); ok {
cidr, _ := cidrFromPath(path, depth, is4, idx)
if !yield(cidr, val) {
// early exit
return false
}
}
}
return true
}
// eachSubnet calls yield() for any covered CIDR by parent prefix in natural CIDR sort order.
func (n *node[V]) eachSubnet(path [16]byte, depth int, is4 bool, octet byte, pfxLen int, yield func(netip.Prefix, V) bool) bool {
// ###############################################################
// 1. collect all indices in n covered by prefix
// ###############################################################
pfxFirstAddr := uint(octet)
pfxLastAddr := uint(octet | ^netMask[pfxLen])
idxBackingArray := [maxNodePrefixes]uint{}
allCoveredIndices := idxBackingArray[:0]
var idx uint
var ok bool
for {
if idx, ok = n.prefixesBitset.NextSet(idx); !ok {
break
}
// idx is covered by prefix
thisOctet, thisPfxLen := idxToPfx(idx)
thisFirstAddr := uint(thisOctet)
thisLastAddr := uint(thisOctet | ^netMask[thisPfxLen])
if thisFirstAddr >= pfxFirstAddr && thisLastAddr <= pfxLastAddr {
allCoveredIndices = append(allCoveredIndices, idx)
}
idx++
}
// sort indices in CIDR sort order
slices.SortFunc(allCoveredIndices, cmpIndexRank)
// ###############################################################
// 2. collect all children in n covered by prefix
// ###############################################################
addrBackingArray := [maxNodeChildren]uint{}
allCoveredAddrs := addrBackingArray[:0]
var addr uint
for {
if addr, ok = n.childrenBitset.NextSet(addr); !ok {
break
}
// host addrs are sorted in indexRank order
if addr > pfxLastAddr {
break
}
if addr >= pfxFirstAddr {
allCoveredAddrs = append(allCoveredAddrs, addr)
}
addr++
}
cursor := 0
// #####################################################
// 3. yield indices and childs in CIDR sort order
// #####################################################
for _, idx := range allCoveredIndices {
thisOctet, _ := idxToPfx(idx)
// yield all childs before idx
for j := cursor; j < len(allCoveredAddrs); j++ {
addr = allCoveredAddrs[j]
// yield prefix
if addr >= uint(thisOctet) {
break
}
// yield child
octet = byte(addr)
c := n.getChild(octet)
// add (set) this octet to path
path[depth] = octet
// all cidrs under this child are covered by pfx
if !c.allRecSorted(path, depth+1, is4, yield) {
// early exit
return false
}
cursor++
}
// yield the prefix for this idx
cidr, _ := cidrFromPath(path, depth, is4, idx)
if !yield(cidr, n.mustGetValue(idx)) {
// early exit
return false
}
}
// ###############################################
// 4. yield the rest of childs, if any
// ###############################################
for j := cursor; j < len(allCoveredAddrs); j++ {
addr = allCoveredAddrs[j]
octet = byte(addr)
c := n.getChild(octet)
// add (set) this octet to path
path[depth] = octet
// all cidrs under this child are covered by pfx
if !c.allRecSorted(path, depth+1, is4, yield) {
// early exit
return false
}
}
return true
}
// unionRec combines two nodes, changing the receiver node.
// If there are duplicate entries, the value is taken from the other node.
// Count duplicate entries to adjust the t.size struct members.
func (n *node[V]) unionRec(o *node[V]) (duplicates int) {
// make backing array, no heap allocs
idxBacking := make([]uint, maxNodePrefixes)
// for all prefixes in other node do ...
for i, oIdx := range o.allStrideIndexes(idxBacking) {
// insert/overwrite prefix/value from oNode to nNode
ok := n.insertPrefix(oIdx, o.prefixes[i])
// this prefix is duplicate in n and o
if !ok {
duplicates++
}
}
// make backing array, no heap allocs
addrBacking := make([]uint, maxNodeChildren)
// for all children in other node do ...
for i, oOctet := range o.allChildAddrs(addrBacking) {
octet := byte(oOctet)
// we know the slice index, faster as o.getChild(octet)
oc := o.children[i]
// get n child with same octet,
// we don't know the slice index in n.children
nc := n.getChild(octet)
if nc == nil {
// insert cloned child from oNode into nNode
n.insertChild(octet, oc.cloneRec())
} else {
// both nodes have child with octet, call union rec-descent
duplicates += nc.unionRec(oc)
}
}
return duplicates
}
// cloneRec, clones the node recursive.
func (n *node[V]) cloneRec() *node[V] {
c := newNode[V]()
if n.isEmpty() {
return c
}
c.prefixesBitset = n.prefixesBitset.Clone() // deep
c.prefixes = slices.Clone(n.prefixes) // values, shallow copy
// deep copy if V implements Cloner[V]
for i, v := range c.prefixes {
if v, ok := any(v).(Cloner[V]); ok {
c.prefixes[i] = v.Clone()
} else {
break
}
}
c.childrenBitset = n.childrenBitset.Clone() // deep
c.children = slices.Clone(n.children) // children, shallow copy
// deep copy of children
for i, child := range c.children {
c.children[i] = child.cloneRec()
}
return c
}
// allRec runs recursive the trie, starting at this node and
// the yield function is called for each route entry with prefix and value.
// If the yield function returns false the recursion ends prematurely and the
// false value is propagated.
//
// The iteration order is not defined, just the simplest and fastest recursive implementation.
func (n *node[V]) allRec(path [16]byte, depth int, is4 bool, yield func(netip.Prefix, V) bool) bool {
idxBacking := make([]uint, maxNodePrefixes)
// for all prefixes in this node do ...
for _, idx := range n.allStrideIndexes(idxBacking) {
cidr, _ := cidrFromPath(path, depth, is4, idx)
// make the callback for this prefix
if !yield(cidr, n.mustGetValue(idx)) {
// early exit
return false
}
}
addrBacking := make([]uint, maxNodeChildren)
// for all children in this node do ...
for i, addr := range n.allChildAddrs(addrBacking) {
child := n.children[i]
path[depth] = byte(addr)
if !child.allRec(path, depth+1, is4, yield) {
// early exit
return false
}
}
return true
}
// allRecSorted runs recursive the trie, starting at node and
// the yield function is called for each route entry with prefix and value.
// The iteration is in prefix sort order.
//
// If the yield function returns false the recursion ends prematurely and the
// false value is propagated.
func (n *node[V]) allRecSorted(path [16]byte, depth int, is4 bool, yield func(netip.Prefix, V) bool) bool {
// make backing arrays, no heap allocs
addrBacking := make([]uint, maxNodeChildren)
idxBacking := make([]uint, maxNodePrefixes)
// get slice of all child octets, sorted by addr
childAddrs := n.allChildAddrs(addrBacking)
// get slice of all indexes, sorted by idx
allIndices := n.allStrideIndexes(idxBacking)
// sort indices in CIDR sort order
slices.SortFunc(allIndices, cmpIndexRank)
childCursor := 0
// yield indices and childs in CIDR sort order
for _, idx := range allIndices {
octet, _ := idxToPfx(idx)
// yield all childs before idx
for j := childCursor; j < len(childAddrs); j++ {
addr := childAddrs[j]
if addr >= uint(octet) {
break
}
// yield the child for this addr
c := n.children[j]
// add (set) this octet to path
path[depth] = byte(addr)
// all cidrs under this child are covered by pfx
if !c.allRecSorted(path, depth+1, is4, yield) {
// early exit
return false
}
childCursor++
}
// yield the prefix for this idx
cidr, _ := cidrFromPath(path, depth, is4, idx)
if !yield(cidr, n.mustGetValue(idx)) {
// early exit
return false
}
}
// yield the rest of childs, if any
for j := childCursor; j < len(childAddrs); j++ {
addr := childAddrs[j]
c := n.children[j]
path[depth] = byte(addr)
if !c.allRecSorted(path, depth+1, is4, yield) {
// early exit
return false
}
}
return true
}