-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
253 lines (225 loc) · 5.47 KB
/
node.go
File metadata and controls
253 lines (225 loc) · 5.47 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
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
package routing_table
// node is a single node in the binary trie. Each node has two possible children
// (bit 0 and bit 1). A non-nil route indicates a route terminates at this depth.
// The parent pointer enables upward pruning when routes are deleted.
type node struct {
children [2]*node
parent *node
// Inline storage for the first path (most common case).
// This avoids map/slice allocation for single-path prefixes.
pathID uint32
attrs *RouteAttributes
// Overflow for additional paths (Add-Path).
// We use a slice here instead of a map to reduce object count and overhead.
// BGP Add-Path typically has only 2-4 paths, so linear search is fine.
extra []pathEntry
// flags bitmask: bit 0 (hasPath), bit 1 (stale)
flags uint8
}
const (
flagHasPath uint8 = 1 << iota
flagStale
)
func (n *node) hasPath() bool {
return n.flags&flagHasPath != 0
}
func (n *node) isStale() bool {
return n.flags&flagStale != 0
}
func (n *node) setStale(stale bool) {
if stale {
n.flags |= flagStale
} else {
n.flags &= ^flagStale
}
}
func (n *node) isPathStale(pathID uint32) bool {
if !n.hasPath() {
return false
}
if n.pathID == pathID {
return n.isStale()
}
for _, entry := range n.extra {
if entry.pathID == pathID {
return entry.stale
}
}
return false
}
// bestPath returns the "best" path from the node's paths map using deterministic rules.
func (n *node) bestPath() *RouteAttributes {
attr, _ := n.bestPathWithID()
return attr
}
// pathsCount returns the total number of paths in the node.
func (n *node) pathsCount() int {
if !n.hasPath() {
return 0
}
return 1 + len(n.extra)
}
func (n *node) bestPathWithID() (*RouteAttributes, uint32) {
if !n.hasPath() {
return nil, 0
}
if len(n.extra) == 0 {
return n.attrs, n.pathID
}
bestAttr := n.attrs
bestPathID := n.pathID
for _, entry := range n.extra {
attr := entry.attrs
// 1. Higher LocalPref (0 = 100 default)
lp1 := attr.LocalPref
if lp1 == 0 {
lp1 = 100
}
lp2 := bestAttr.LocalPref
if lp2 == 0 {
lp2 = 100
}
if lp1 > lp2 {
bestAttr = attr
bestPathID = entry.pathID
continue
}
if lp1 < lp2 {
continue
}
// 2. Shorter AS Path
if len(attr.AsPath) < len(bestAttr.AsPath) {
bestAttr = attr
bestPathID = entry.pathID
continue
}
if len(attr.AsPath) > len(bestAttr.AsPath) {
continue
}
// 3. Lower PathID (tie-break)
if entry.pathID < bestPathID {
bestAttr = attr
bestPathID = entry.pathID
}
}
return bestAttr, bestPathID
}
func (n *node) allPaths() []pathEntry {
if !n.hasPath() {
return nil
}
res := make([]pathEntry, 1+len(n.extra))
res[0] = pathEntry{
attrs: n.attrs,
pathID: n.pathID,
stale: n.isStale(),
}
copy(res[1:], n.extra)
return res
}
func (n *node) deletePath(pathID uint32) (*RouteAttributes, bool) {
if !n.hasPath() {
return nil, false
}
if n.pathID == pathID {
oldAttrs := n.attrs
if len(n.extra) > 0 {
// Move first extra to inline
n.pathID = n.extra[0].pathID
n.attrs = n.extra[0].attrs
n.setStale(n.extra[0].stale)
n.extra = n.extra[1:]
if len(n.extra) == 0 {
n.extra = nil
}
} else {
n.attrs = nil
n.flags &= ^flagHasPath
n.setStale(false)
}
return oldAttrs, true
}
for i, entry := range n.extra {
if entry.pathID == pathID {
oldAttrs := entry.attrs
n.extra = append(n.extra[:i], n.extra[i+1:]...)
if len(n.extra) == 0 {
n.extra = nil
}
return oldAttrs, true
}
}
return nil, false
}
func (n *node) setPath(pathID uint32, attrs *RouteAttributes, stale bool) (*RouteAttributes, bool) {
// 1. Exact match by PathID (re-announcement or update of an existing path).
if n.hasPath() && n.pathID == pathID {
oldAttrs := n.attrs
n.attrs = attrs
n.setStale(stale)
return oldAttrs, true
}
for i := range n.extra {
if n.extra[i].pathID == pathID {
oldAttrs := n.extra[i].attrs
n.extra[i].attrs = attrs
n.extra[i].stale = stale
return oldAttrs, true
}
}
// 2. No exact match. Check if we can "replace" a stale path to save memory.
// This prevents path count doubling during Graceful Restart resync
// if the peer uses different PathIDs upon reconnect.
if n.hasPath() && n.isStale() {
oldAttrs := n.attrs
n.pathID = pathID
n.attrs = attrs
n.setStale(stale)
return oldAttrs, true
}
for i := range n.extra {
if n.extra[i].stale {
oldAttrs := n.extra[i].attrs
n.extra[i].pathID = pathID
n.extra[i].attrs = attrs
n.extra[i].stale = stale
return oldAttrs, true
}
}
// 3. No match and no stale path to replace. Add as a new path.
if !n.hasPath() {
n.pathID = pathID
n.attrs = attrs
n.setStale(stale)
n.flags |= flagHasPath
return nil, false
}
n.extra = append(n.extra, pathEntry{
attrs: attrs,
pathID: pathID,
stale: stale,
})
return nil, false
}
// deleteNode recursively prunes empty leaf nodes upward through the trie.
// A node is prunable only if it has no prefix and no children.
// Recursion stops at array entry nodes (parent == nil), which are cleaned
// up by the caller.
func deleteNode(node *node) uint64 {
// ensure we don't fall off the top of the tree.
if node.parent == nil {
return 0
}
// a node can only be deleted if it has no prefix and no children.
if node.children[0] == nil && node.children[1] == nil && !node.hasPath() {
// 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.
return 1 + deleteNode(node.parent)
}
}
}
return 0
}