-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgold_table_test.go
279 lines (240 loc) · 5.6 KB
/
gold_table_test.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
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT
package bart
import (
"cmp"
"net/netip"
"slices"
)
// goldTable is a simple and slow route table, implemented as a slice of prefixes
// and values as a golden reference for bart.Table.
type goldTable[V any] []goldTableItem[V]
type goldTableItem[V any] struct {
pfx netip.Prefix
val V
}
//nolint:unused
func (t *goldTable[V]) insert(pfx netip.Prefix, val V) {
pfx = pfx.Masked()
for i, ent := range *t {
if ent.pfx == pfx {
(*t)[i].val = val
return
}
}
*t = append(*t, goldTableItem[V]{pfx, val})
}
func (t *goldTable[V]) insertMany(pfxs []goldTableItem[V]) *goldTable[V] {
cast := goldTable[V](pfxs)
t = &cast
return t
}
func (t *goldTable[V]) get(pfx netip.Prefix) (val V, ok bool) {
pfx = pfx.Masked()
for _, ent := range *t {
if ent.pfx == pfx {
return ent.val, true
}
}
return val, false
}
func (t *goldTable[V]) update(pfx netip.Prefix, cb func(V, bool) V) (val V) {
pfx = pfx.Masked()
for i, ent := range *t {
if ent.pfx == pfx {
// update val
(*t)[i].val = cb(ent.val, true)
return
}
}
// new val
val = cb(val, false)
*t = append(*t, goldTableItem[V]{pfx, val})
return val
}
func (ta *goldTable[V]) union(tb *goldTable[V]) {
for _, bItem := range *tb {
var match bool
for i, aItem := range *ta {
if aItem.pfx == bItem.pfx {
(*ta)[i] = bItem
match = true
break
}
}
if !match {
*ta = append(*ta, bItem)
}
}
}
func (t *goldTable[V]) lookup(addr netip.Addr) (val V, ok bool) {
bestLen := -1
for _, item := range *t {
if item.pfx.Contains(addr) && item.pfx.Bits() > bestLen {
val = item.val
ok = true
bestLen = item.pfx.Bits()
}
}
return
}
func (t *goldTable[V]) lookupPfx(pfx netip.Prefix) (val V, ok bool) {
bestLen := -1
for _, item := range *t {
if item.pfx.Overlaps(pfx) && item.pfx.Bits() <= pfx.Bits() && item.pfx.Bits() > bestLen {
val = item.val
ok = true
bestLen = item.pfx.Bits()
}
}
return
}
func (t *goldTable[V]) lookupPfxLPM(pfx netip.Prefix) (lpm netip.Prefix, val V, ok bool) {
bestLen := -1
for _, item := range *t {
if item.pfx.Overlaps(pfx) && item.pfx.Bits() <= pfx.Bits() && item.pfx.Bits() > bestLen {
val = item.val
lpm = item.pfx
ok = true
bestLen = item.pfx.Bits()
}
}
return
}
func (t *goldTable[V]) subnets(pfx netip.Prefix) []netip.Prefix {
var result []netip.Prefix
for _, item := range *t {
if pfx.Overlaps(item.pfx) && pfx.Bits() <= item.pfx.Bits() {
result = append(result, item.pfx)
}
}
slices.SortFunc(result, cmpPrefix)
return result
}
func (t *goldTable[V]) supernets(pfx netip.Prefix) []netip.Prefix {
var result []netip.Prefix
for _, item := range *t {
if item.pfx.Overlaps(pfx) && item.pfx.Bits() <= pfx.Bits() {
result = append(result, item.pfx)
}
}
slices.SortFunc(result, cmpPrefix)
slices.Reverse(result)
return result
}
func (t *goldTable[V]) overlapsPrefix(pfx netip.Prefix) bool {
for _, p := range *t {
if p.pfx.Overlaps(pfx) {
return true
}
}
return false
}
func (ta *goldTable[V]) overlaps(tb *goldTable[V]) bool {
for _, aItem := range *ta {
for _, bItem := range *tb {
if aItem.pfx.Overlaps(bItem.pfx) {
return true
}
}
}
return false
}
// sort, inplace by netip.Prefix, all prefixes are in normalized form
func (t *goldTable[V]) sort() {
slices.SortFunc(*t, func(a, b goldTableItem[V]) int {
if cmp := a.pfx.Addr().Compare(b.pfx.Addr()); cmp != 0 {
return cmp
}
return cmp.Compare(a.pfx.Bits(), b.pfx.Bits())
})
}
// randomPrefixes returns n randomly generated prefixes and associated values,
// distributed equally between IPv4 and IPv6.
func randomPrefixes(n int) []goldTableItem[int] {
pfxs := randomPrefixes4(n / 2)
pfxs = append(pfxs, randomPrefixes6(n-len(pfxs))...)
return pfxs
}
// randomPrefixes4 returns n randomly generated IPv4 prefixes and associated values.
// skip default route
func randomPrefixes4(n int) []goldTableItem[int] {
pfxs := map[netip.Prefix]bool{}
for len(pfxs) < n {
bits := prng.IntN(32)
bits++
pfx, err := randomIP4().Prefix(bits)
if err != nil {
panic(err)
}
pfxs[pfx] = true
}
ret := make([]goldTableItem[int], 0, len(pfxs))
for pfx := range pfxs {
ret = append(ret, goldTableItem[int]{pfx, prng.Int()})
}
return ret
}
// randomPrefixes6 returns n randomly generated IPv4 prefixes and associated values.
// skip default route
func randomPrefixes6(n int) []goldTableItem[int] {
pfxs := map[netip.Prefix]bool{}
for len(pfxs) < n {
bits := prng.IntN(128)
bits++
pfx, err := randomIP6().Prefix(bits)
if err != nil {
panic(err)
}
pfxs[pfx] = true
}
ret := make([]goldTableItem[int], 0, len(pfxs))
for pfx := range pfxs {
ret = append(ret, goldTableItem[int]{pfx, prng.Int()})
}
return ret
}
// #####################################################################
// randomPrefix returns a randomly generated prefix
func randomPrefix() netip.Prefix {
if prng.IntN(2) == 1 {
return randomPrefix4()
}
return randomPrefix6()
}
func randomPrefix4() netip.Prefix {
bits := prng.IntN(33)
pfx, err := randomIP4().Prefix(bits)
if err != nil {
panic(err)
}
return pfx
}
func randomPrefix6() netip.Prefix {
bits := prng.IntN(129)
pfx, err := randomIP6().Prefix(bits)
if err != nil {
panic(err)
}
return pfx
}
func randomIP4() netip.Addr {
var b [4]byte
for i := range b {
b[i] = byte(prng.Uint32() & 0xff)
}
return netip.AddrFrom4(b)
}
func randomIP6() netip.Addr {
var b [16]byte
for i := range b {
b[i] = byte(prng.Uint32() & 0xff)
}
return netip.AddrFrom16(b)
}
func randomAddr() netip.Addr {
if prng.IntN(2) == 1 {
return randomIP4()
}
return randomIP6()
}