/
loc_rib.go
353 lines (287 loc) · 8.47 KB
/
loc_rib.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
package locRIB
import (
"fmt"
"sync"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
"github.com/bio-routing/bio-rd/util/math"
log "github.com/sirupsen/logrus"
)
// LocRIB represents a routing information base
type LocRIB struct {
name string
clientManager *routingtable.ClientManager
rt *routingtable.RoutingTable
mu sync.RWMutex
contributingASNs *routingtable.ContributingASNs
countTarget *countTarget
}
type countTarget struct {
target uint64
ch chan struct{}
}
// New creates a new routing information base
func New(name string) *LocRIB {
a := &LocRIB{
name: name,
rt: routingtable.NewRoutingTable(),
contributingASNs: routingtable.NewContributingASNs(),
}
a.clientManager = routingtable.NewClientManager(a)
return a
}
// Name gets the name of the LocRIB
func (a *LocRIB) Name() string {
return a.name
}
// ClientCount gets the number of registered clients
func (a *LocRIB) ClientCount() uint64 {
return a.clientManager.ClientCount()
}
// GetContributingASNs returns a pointer to the list of contributing ASNs
func (a *LocRIB) GetContributingASNs() *routingtable.ContributingASNs {
return a.contributingASNs
}
// Count routes from the LocRIB
func (a *LocRIB) Count() uint64 {
return uint64(a.rt.GetRouteCount())
}
// LPM performs a longest prefix match on the routing table
func (a *LocRIB) LPM(pfx *net.Prefix) (res []*route.Route) {
return a.rt.LPM(pfx)
}
// Get gets a route
func (a *LocRIB) Get(pfx *net.Prefix) *route.Route {
return a.rt.Get(pfx)
}
// GetLonger gets all more specifics
func (a *LocRIB) GetLonger(pfx *net.Prefix) (res []*route.Route) {
return a.rt.GetLonger(pfx)
}
// Dump dumps the RIB
func (a *LocRIB) Dump() []*route.Route {
a.mu.RLock()
defer a.mu.RUnlock()
return a.rt.Dump()
}
// SetCountTarget sets a target and a channel to send a message to when a certain route count is reached
func (a *LocRIB) SetCountTarget(count uint64, ch chan struct{}) {
a.countTarget = &countTarget{
target: count,
ch: ch,
}
}
// UpdateNewClient sends current state to a new client
func (a *LocRIB) UpdateNewClient(client routingtable.RouteTableClient) error {
a.mu.RLock()
defer a.mu.RUnlock()
opts := a.clientManager.GetOptions(client)
routes := a.rt.Dump()
for _, r := range routes {
n := uint(0)
if opts.BestOnly {
n = 1
} else if opts.EcmpOnly {
n = r.ECMPPathCount()
} else {
n = opts.MaxPaths
n = uint(math.Min(int(n), len(r.Paths())))
}
for _, p := range r.Paths()[:n] {
client.AddPathInitialDump(r.Prefix(), p)
}
}
return nil
}
// RefreshClient re-sends all propagated paths to a certain client
func (a *LocRIB) RefreshClient(client routingtable.RouteTableClient) {
a.mu.RLock()
defer a.mu.RUnlock()
opts := a.clientManager.GetOptions(client)
routes := a.rt.Dump()
for _, r := range routes {
n := uint(0)
if opts.BestOnly {
n = 1
} else if opts.EcmpOnly {
n = r.ECMPPathCount()
} else {
n = opts.MaxPaths
n = uint(math.Min(int(n), len(r.Paths())))
}
client.RefreshRoute(r.Prefix(), r.Paths()[:n])
}
}
// RouteCount returns the number of stored routes
func (a *LocRIB) RouteCount() int64 {
return a.rt.GetRouteCount()
}
func (a *LocRIB) AddPathInitialDump(pfx *net.Prefix, p *route.Path) error {
return a.AddPath(pfx, p)
}
// AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added.
func (a *LocRIB) AddPath(pfx *net.Prefix, p *route.Path) error {
a.mu.Lock()
defer a.mu.Unlock()
log.WithFields(map[string]interface{}{
"Prefix": pfx,
"Route": p,
}).Debug("AddPath to locRIB")
routeExisted := false
oldRoute := &route.Route{}
r := a.rt.Get(pfx)
if r != nil {
oldRoute = r.Copy()
routeExisted = true
}
// FIXME: in AddPath() we assume that the same reference of route (r) is modified (not responsibility of locRIB). If this implementation changes in the future this code will break.
a.rt.AddPath(pfx, p)
if !routeExisted {
r = a.rt.Get(pfx)
}
r.PathSelection()
newRoute := r.Copy()
a.propagateChanges(oldRoute, newRoute)
if a.countTarget != nil {
if a.RouteCount() == int64(a.countTarget.target) {
a.countTarget.ch <- struct{}{}
}
}
return nil
}
// RemovePath removes the path for prefix `pfx`
func (a *LocRIB) RemovePath(pfx *net.Prefix, p *route.Path) bool {
a.mu.Lock()
defer a.mu.Unlock()
log.WithFields(map[string]interface{}{
"Prefix": pfx,
"Route": p,
}).Debug("Remove from locRIB")
var oldRoute *route.Route
r := a.rt.Get(pfx)
if r != nil {
oldRoute = r.Copy()
} else {
return true
}
a.rt.RemovePath(pfx, p)
r.PathSelection()
r = a.rt.Get(pfx)
newRoute := r.Copy()
a.propagateChanges(oldRoute, newRoute)
return true
}
func (a *LocRIB) ReplacePath(pfx *net.Prefix, oldPath *route.Path, newPath *route.Path) {
a.mu.Lock()
defer a.mu.Unlock()
r := a.rt.Get(pfx)
if r == nil {
log.Errorf("Unable to replace path of prefix %s: prefix not found", pfx.String())
return
}
oldRoute := r.Copy()
err := r.ReplacePath(oldPath, newPath)
if err != nil {
log.Errorf("Unable to replace path: %v", err)
return
}
r.PathSelection()
a.propagateChanges(oldRoute, r)
}
func (a *LocRIB) propagateChanges(oldRoute *route.Route, newRoute *route.Route) {
a.removePathsFromClients(oldRoute, newRoute)
a.addPathsToClients(oldRoute, newRoute)
}
func (a *LocRIB) addPathsToClients(oldRoute *route.Route, newRoute *route.Route) {
for _, client := range a.clientManager.Clients() {
opts := a.clientManager.GetOptions(client)
oldMaxPaths := opts.GetMaxPaths(oldRoute.ECMPPathCount())
newMaxPaths := opts.GetMaxPaths(newRoute.ECMPPathCount())
oldPathsLimit := int(math.Min(int(oldMaxPaths), len(oldRoute.Paths())))
newPathsLimit := int(math.Min(int(newMaxPaths), len(newRoute.Paths())))
advertise := route.PathsDiff(newRoute.Paths()[0:newPathsLimit], oldRoute.Paths()[0:oldPathsLimit])
for _, p := range advertise {
client.AddPath(newRoute.Prefix(), p)
}
}
}
func (a *LocRIB) removePathsFromClients(oldRoute *route.Route, newRoute *route.Route) {
for _, client := range a.clientManager.Clients() {
opts := a.clientManager.GetOptions(client)
oldMaxPaths := opts.GetMaxPaths(oldRoute.ECMPPathCount())
newMaxPaths := opts.GetMaxPaths(newRoute.ECMPPathCount())
oldPathsLimit := int(math.Min(int(oldMaxPaths), len(oldRoute.Paths())))
newPathsLimit := int(math.Min(int(newMaxPaths), len(newRoute.Paths())))
withdraw := route.PathsDiff(oldRoute.Paths()[0:oldPathsLimit], newRoute.Paths()[0:newPathsLimit])
for _, p := range withdraw {
client.RemovePath(oldRoute.Prefix(), p)
}
}
}
// ContainsPfxPath returns true if this prefix and path combination is
// present in this LocRIB.
func (a *LocRIB) ContainsPfxPath(pfx *net.Prefix, p *route.Path) bool {
a.mu.RLock()
defer a.mu.RUnlock()
r := a.rt.Get(pfx)
if r == nil {
return false
}
for _, path := range r.Paths() {
if path.Equal(p) {
return true
}
}
return false
}
func (a *LocRIB) String() string {
a.mu.RLock()
defer a.mu.RUnlock()
ret := ""
routes := a.rt.Dump()
for idx, r := range routes {
if idx < len(routes)-1 {
ret += fmt.Sprintf("%s, ", r.Prefix().String())
} else {
ret += fmt.Sprintf("%s", r.Prefix().String())
}
}
return ret
}
func (a *LocRIB) Print() string {
a.mu.RLock()
defer a.mu.RUnlock()
ret := "Loc-RIB DUMP:\n"
routes := a.rt.Dump()
for _, r := range routes {
ret += fmt.Sprintf("%s\n", r.Prefix().String())
}
return ret
}
// Register registers a client for updates
func (a *LocRIB) Register(client routingtable.RouteTableClient) {
a.clientManager.RegisterWithOptions(client, routingtable.ClientOptions{BestOnly: true})
}
// RegisterWithOptions registers a client with options for updates
func (a *LocRIB) RegisterWithOptions(client routingtable.RouteTableClient, opt routingtable.ClientOptions) {
a.clientManager.RegisterWithOptions(client, opt)
}
// Unregister unregisters a client
func (a *LocRIB) Unregister(client routingtable.RouteTableClient) {
a.clientManager.Unregister(client)
}
// ReplaceFilterChain is here to fulfill an interface
func (a *LocRIB) ReplaceFilterChain(filter.Chain) {
return
}
// RefreshRoute is here to fulfill an interface
func (a *LocRIB) RefreshRoute(*net.Prefix, []*route.Path) {
}
// Dispose tells all clients that this LocRIB is not to be used anymore (this can happen when RIS loses a BMP connection)
func (a *LocRIB) Dispose() {
for _, c := range a.clientManager.Clients() {
c.Dispose()
}
}