-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_table.go
341 lines (311 loc) · 7.61 KB
/
routing_table.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
package routing_table
import (
"fmt"
"net/netip"
"sync"
"time"
)
var (
v4nodes = 0
v4alreadycreated = 0
v6nodes = 0
v6alreadycreated = 0
nodes = 0
)
type router struct {
ribs []Rib
}
type Rib struct {
mu *sync.RWMutex
ipv4Root *node
ipv6Root *node
v4Count int
v6Count int
v4masks map[int]int
v6masks map[int]int
}
type node struct {
children [2]*node
prefix *netip.Prefix
parent *node
}
func GetNewRouter() router {
return router{}
}
func GetNewRib() Rib {
return Rib{
ipv4Root: &node{},
ipv6Root: &node{},
mu: &sync.RWMutex{},
v4masks: make(map[int]int),
v6masks: make(map[int]int),
}
}
func (r *router) Size() int {
return len(r.ribs)
}
func (r *router) AddRib(rib Rib) {
r.ribs = append(r.ribs, rib)
}
func (r *Rib) PrintRib() {
// TODO: figure out how to print the rib really
fmt.Printf("%d ipv4 prefixes\n", r.v4Count)
fmt.Printf("%d ipv4 nodes created\n", v4nodes)
fmt.Printf("%d ipv4 nodes already created\n", v4alreadycreated)
fmt.Printf("%d ipv6 prefixes\n", r.v6Count)
fmt.Printf("%d ipv6 nodes created\n", v6nodes)
fmt.Printf("%d ipv6 nodes already created\n", v6alreadycreated)
fmt.Printf("%d total ipv4 and ipv6 nodes created\n", v4nodes+v6nodes)
fmt.Printf("%d nodes deleted\n", nodes)
fmt.Printf("%v\n", r.v4masks)
fmt.Printf("%v\n", r.v6masks)
}
// Insert a prefix into the rib
func (r *Rib) InsertIPv4(prefix netip.Prefix) {
if prefix.Addr().Is6() {
return
}
r.mu.Lock()
defer r.mu.Unlock()
currentNode := r.ipv4Root
addr := prefix.Addr().As4()
mask := prefix.Bits()
bitCount := 1
// <3 because we really don't care about the last octet as we won't store anything > 24
for i := 0; i < 3; i++ {
// TODO: We never have < /8 either, so the first node should really be a decimal!
bits := intToBinBitwise(addr[i])
for _, bit := range bits {
if currentNode.children[bit] == nil {
v4nodes++
currentNode.children[bit] = &node{
parent: currentNode,
}
// fmt.Printf("memory size is %v bytes\n", unsafe.Sizeof(*currentNode.children[bit]))
} else {
v4alreadycreated++
}
currentNode = currentNode.children[bit]
if bitCount == mask {
currentNode.prefix = &prefix
r.v4Count++
r.v4masks[mask]++
return
}
bitCount++
}
}
}
func (r *Rib) InsertIPv6(prefix netip.Prefix) {
if prefix.Addr().Is4() {
return
}
r.mu.Lock()
defer r.mu.Unlock()
currentNode := r.ipv6Root
addr := prefix.Addr().As16()
mask := prefix.Bits()
bitCount := 1
// <6 because we really don't care about the last 10 octets as we won't store anything > 48
for i := 0; i < 6; i++ {
// TODO: only 2000::/3 is assigned. This means the first two bytes are ALWAYS 0 and the third byte is ALWAYS 1
// 2000 = 00100000
// 3fff = 00111111
// https://play.golang.org/p/MZ8-5obwF_B
bits := intToBinBitwise(addr[i])
for _, bit := range bits {
if currentNode.children[bit] == nil {
v6nodes++
currentNode.children[bit] = &node{
parent: currentNode,
}
} else {
v6alreadycreated++
}
currentNode = currentNode.children[bit]
if bitCount == mask {
currentNode.prefix = &prefix
r.v6Count++
r.v6masks[mask]++
return
}
bitCount++
}
}
}
func (r *Rib) DeleteIPv4(prefix netip.Prefix) {
if prefix.Addr().Is6() {
return
}
r.mu.Lock()
defer r.mu.Unlock()
// TODO: delete should be idempotent. Meaning search should be updated to search for prefix mask as well.
// If prefix and mask doesn't exist, do nothing.
currentNode := r.ipv4Root
addr := prefix.Addr().As4()
mask := prefix.Bits()
bitCount := 1
for i := 0; i < 3; i++ {
bits := intToBinBitwise(addr[i])
for _, bit := range bits {
// If prefix does not exist, we return early
if currentNode.children[bit] == nil {
return
}
// add node to list of potential deletes
// nodes = append(nodes, currentNode.children[bit])
currentNode = currentNode.children[bit]
if bitCount == mask {
r.v4Count--
currentNode.prefix = nil
r.v4masks[mask]--
deleteNode(currentNode)
return
}
bitCount++
}
}
}
func (r *Rib) DeleteIPv6(prefix netip.Prefix) {
if prefix.Addr().Is4() {
return
}
r.mu.Lock()
defer r.mu.Unlock()
// TODO: delete should be idempotent. Meaning search should be updated to search for prefix mask as well.
// If prefix and mask doesn't exist, do nothing.
currentNode := r.ipv6Root
addr := prefix.Addr().As16()
mask := prefix.Bits()
bitCount := 1
for i := 0; i < 6; i++ {
bits := intToBinBitwise(addr[i])
for _, bit := range bits {
// If prefix does not exist, we return early
if currentNode.children[bit] == nil {
return
}
// add node to list of potential deletes
// nodes = append(nodes, currentNode.children[bit])
currentNode = currentNode.children[bit]
if bitCount == mask {
r.v6Count--
currentNode.prefix = nil
r.v6masks[mask]--
deleteNode(currentNode)
return
}
bitCount++
}
}
}
// deleteNode will remove a node if it's empty, then will work upwards in the TRIE
// deleting all empty nodes it can.
func deleteNode(node *node) {
// ensure we don't fall off the top of the tree.
if node.parent == nil {
return
}
// a node can only be deleted if it has no prefix and no children.
if node.children[0] == nil && node.children[1] == nil && node.prefix == nil {
// each node can have two children, so need to check both.
for j := 0; j < 2; j++ {
if node.parent.children[j] == node {
node.parent.children[j] = nil
// keep deleting empty nodes.
deleteNode(node.parent)
nodes++
return
}
}
}
}
// Search the rib for a prefix
func (r *Rib) SearchIPv4(ip netip.Addr) *netip.Prefix {
if ip.Is6() {
return nil
}
start := time.Now()
r.mu.RLock()
defer r.mu.RUnlock()
defer fmt.Printf("IP lookup took %s\n", time.Since(start).String())
lpm := &netip.Prefix{}
currentNode := r.ipv4Root
addr := ip.As4()
bitCount := 1
// <3 because we really don't care about the last octet as we won't store anything > 24
for i := 0; i < 3; i++ {
bits := intToBinBitwise(addr[i])
for _, bit := range bits {
if currentNode.children[bit] != nil {
// save the current best path
currentNode = currentNode.children[bit]
if currentNode.prefix != nil {
lpm = currentNode.prefix
}
bitCount++
} else {
break
}
}
}
if lpm.Contains(ip) {
return lpm
}
return nil
}
// Search the rib for a prefix
func (r *Rib) SearchIPv6(ip netip.Addr) *netip.Prefix {
if ip.Is4() {
return nil
}
start := time.Now()
r.mu.RLock()
defer r.mu.RUnlock()
defer fmt.Printf("IP lookup took %s\n", time.Since(start).String())
lpm := &netip.Prefix{}
currentNode := r.ipv6Root
addr := ip.As16()
bitCount := 1
// <6 because we really don't care about the last 10 octets as we won't store anything > 48
for i := 0; i < 6; i++ {
bits := intToBinBitwise(addr[i])
for _, bit := range bits {
if currentNode.children[bit] != nil {
// save the current best path
currentNode = currentNode.children[bit]
if currentNode.prefix != nil {
lpm = currentNode.prefix
}
bitCount++
} else {
break
}
}
}
if lpm.Contains(ip) {
return lpm
}
return nil
}
// intToBinBitwise will take a uint8 and return a slice
// of 8 bits representing the binary version
// TODO: test if it's slower to pass in bit length to only get back
// the amount of bits we require
func intToBinBitwise(num uint8) []uint8 {
res := make([]uint8, 0, 8)
for i := 7; i >= 0; i-- {
k := num >> i
if (k & 1) > 0 {
res = append(res, 1)
} else {
res = append(res, 0)
}
}
return res
}
func (r *Rib) getSubnets() (map[int]int, map[int]int) {
r.mu.RLock()
defer r.mu.RUnlock()
return r.v4masks, r.v6masks
}