forked from FlexSNR/l3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ribdRouteServer.go
364 lines (352 loc) · 10.7 KB
/
ribdRouteServer.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
//
//Copyright [2016] [SnapRoute Inc]
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// _______ __ __________ ___ _______.____ __ ____ __ .___________. ______ __ __
// | ____|| | | ____\ \ / / / |\ \ / \ / / | | | | / || | | |
// | |__ | | | |__ \ V / | (----` \ \/ \/ / | | `---| |----`| ,----'| |__| |
// | __| | | | __| > < \ \ \ / | | | | | | | __ |
// | | | `----.| |____ / . \ .----) | \ /\ / | | | | | `----.| | | |
// |__| |_______||_______/__/ \__\ |_______/ \__/ \__/ |__| |__| \______||__| |__|
//
// ribdRouteServer.go
package server
import (
"l3/rib/ribdCommonDefs"
"ribd"
"strconv"
)
type RouteConfigInfo struct {
OrigRoute *ribd.IPv4Route
NewRoute *ribd.IPv4Route
Attrset []bool
Op string //"add"/"del"/"update"
}
type TrackReachabilityInfo struct {
IpAddr string
Protocol string
Op string
}
type NextHopInfoKey struct {
nextHopIp string
}
type NextHopInfo struct {
refCount int //number of routes using this as a next hop
}
type RouteCountInfo struct {
totalcount int
ecmpcount int
}
type PerProtocolRouteInfo struct {
v4routeMap map[string]RouteCountInfo
v6routeMap map[string]RouteCountInfo
totalcount RouteCountInfo
}
var ProtocolRouteMap map[string]PerProtocolRouteInfo //map[string]int
var InterfaceRouteMap map[string]PerProtocolRouteInfo //map[string]int
func UpdateV4ProtocolRouteMap(protocol string, op string, value string, ecmp bool) {
var info PerProtocolRouteInfo
if ProtocolRouteMap == nil {
if op == "del" {
return
}
ProtocolRouteMap = make(map[string]PerProtocolRouteInfo) //map[string]int)
}
info, ok := ProtocolRouteMap[protocol]
if !ok || info.v4routeMap == nil {
if op == "del" {
return
}
if info.v4routeMap == nil {
info.v4routeMap = make(map[string]RouteCountInfo)
}
}
protocolroutemap := info.v4routeMap
if protocolroutemap == nil {
return
}
totalcount := info.totalcount
count, ok := protocolroutemap[value]
if !ok {
if op == "del" {
return
}
}
if op == "add" {
count.totalcount++
totalcount.totalcount++
if ecmp {
if count.ecmpcount == 0 {
//first time ecmp route is added, add an additional count
count.ecmpcount++
totalcount.ecmpcount++
}
count.ecmpcount++
totalcount.ecmpcount++
}
} else if op == "del" {
count.totalcount--
totalcount.totalcount--
if ecmp {
count.ecmpcount--
totalcount.ecmpcount--
if count.totalcount <= 1 {
count.ecmpcount--
totalcount.ecmpcount--
}
}
}
protocolroutemap[value] = count
info.v4routeMap = protocolroutemap
info.totalcount = totalcount
ProtocolRouteMap[protocol] = info
}
func UpdateV6ProtocolRouteMap(protocol string, op string, value string, ecmp bool) {
var info PerProtocolRouteInfo
if ProtocolRouteMap == nil {
if op == "del" {
return
}
ProtocolRouteMap = make(map[string]PerProtocolRouteInfo) //map[string]int)
}
info, ok := ProtocolRouteMap[protocol]
if !ok || info.v6routeMap == nil {
if op == "del" {
return
}
if info.v6routeMap == nil {
info.v6routeMap = make(map[string]RouteCountInfo)
}
}
protocolroutemap := info.v6routeMap
if protocolroutemap == nil {
return
}
totalcount := info.totalcount
count, ok := protocolroutemap[value]
if !ok {
if op == "del" {
return
}
}
if op == "add" {
count.totalcount++
totalcount.totalcount++
if ecmp {
if count.ecmpcount == 0 {
//first time ecmp route is added, add an additional count
count.ecmpcount++
totalcount.ecmpcount++
}
count.ecmpcount++
totalcount.ecmpcount++
}
} else if op == "del" {
count.totalcount--
totalcount.totalcount--
if ecmp {
count.ecmpcount--
totalcount.ecmpcount--
if count.totalcount <= 1 {
count.ecmpcount--
totalcount.ecmpcount--
}
}
}
protocolroutemap[value] = count
info.v6routeMap = protocolroutemap
info.totalcount = totalcount
ProtocolRouteMap[protocol] = info
}
func UpdateProtocolRouteMap(protocol string, op string, ipType ribdCommonDefs.IPType, value string, ecmp bool) {
//logger.Debug("UpdateProtocolRouteMap,protocol:", protocol, " iptype:", ipType)
if ipType == ribdCommonDefs.IPv4 {
UpdateV4ProtocolRouteMap(protocol, op, value, ecmp)
} else {
UpdateV6ProtocolRouteMap(protocol, op, value, ecmp)
}
}
func UpdateV4InterfaceRouteMap(intfref string, op string, value string, ecmp bool) {
var info PerProtocolRouteInfo
if InterfaceRouteMap == nil {
if op == "del" {
return
}
InterfaceRouteMap = make(map[string]PerProtocolRouteInfo) //map[string]int)
}
info, ok := InterfaceRouteMap[intfref]
if !ok || info.v4routeMap == nil {
if op == "del" {
return
}
if info.v4routeMap == nil {
info.v4routeMap = make(map[string]RouteCountInfo)
}
}
interfaceroutemap := info.v4routeMap
if interfaceroutemap == nil {
return
}
totalcount := info.totalcount
count, ok := interfaceroutemap[value]
if !ok {
if op == "del" {
return
}
}
if op == "add" {
count.totalcount++
totalcount.totalcount++
if ecmp {
if count.ecmpcount == 0 {
//first time ecmp route is added, add an additional count
count.ecmpcount++
totalcount.ecmpcount++
}
count.ecmpcount++
totalcount.ecmpcount++
}
} else if op == "del" {
count.totalcount--
totalcount.totalcount--
if ecmp {
count.ecmpcount--
totalcount.ecmpcount--
if count.totalcount <= 1 {
count.ecmpcount--
totalcount.ecmpcount--
}
}
}
interfaceroutemap[value] = count
info.v4routeMap = interfaceroutemap
info.totalcount = totalcount
InterfaceRouteMap[intfref] = info
}
func UpdateV6InterfaceRouteMap(intfref string, op string, value string, ecmp bool) {
var info PerProtocolRouteInfo
if InterfaceRouteMap == nil {
if op == "del" {
return
}
InterfaceRouteMap = make(map[string]PerProtocolRouteInfo) //map[string]int)
}
info, ok := InterfaceRouteMap[intfref]
if !ok || info.v6routeMap == nil {
if op == "del" {
return
}
if info.v6routeMap == nil {
info.v6routeMap = make(map[string]RouteCountInfo)
}
}
interfaceroutemap := info.v6routeMap
if interfaceroutemap == nil {
return
}
totalcount := info.totalcount
count, ok := interfaceroutemap[value]
if !ok {
if op == "del" {
return
}
}
if op == "add" {
count.totalcount++
totalcount.totalcount++
if ecmp {
if count.ecmpcount == 0 {
//first time ecmp route is added, add an additional count
count.ecmpcount++
totalcount.ecmpcount++
}
count.ecmpcount++
totalcount.ecmpcount++
}
} else if op == "del" {
count.totalcount--
totalcount.totalcount--
if ecmp {
count.ecmpcount--
totalcount.ecmpcount--
if count.totalcount <= 1 {
count.ecmpcount--
totalcount.ecmpcount--
}
}
}
interfaceroutemap[value] = count
info.v6routeMap = interfaceroutemap
info.totalcount = totalcount
InterfaceRouteMap[intfref] = info
}
func UpdateInterfaceRouteMap(intf int, op string, ipType ribdCommonDefs.IPType, value string, ecmp bool) {
intfref := strconv.Itoa(int(intf))
intfEntry, ok := IntfIdNameMap[int32(intf)]
if ok {
//logger.Debug("Map foud for ifndex : ", routeInfoList[sel].nextHopIfIndex, "Name = ", intfEntry.name)
intfref = intfEntry.name
}
if ipType == ribdCommonDefs.IPv4 {
UpdateV4InterfaceRouteMap(intfref, op, value, ecmp)
} else {
UpdateV6InterfaceRouteMap(intfref, op, value, ecmp)
}
}
func (ribdServiceHandler *RIBDServer) StartRouteProcessServer() {
logger.Info("Starting the routeserver loop")
ProtocolRouteMap = make(map[string]PerProtocolRouteInfo) //map[string]int)
for {
select {
case routeConf := <-ribdServiceHandler.RouteConfCh:
//logger.Debug(fmt.Sprintln("received message on RouteConfCh channel, op: ", routeConf.Op)
if routeConf.Op == "add" {
ribdServiceHandler.ProcessV4RouteCreateConfig(routeConf.OrigConfigObject.(*ribd.IPv4Route), FIBAndRIB, ribd.Int(len(destNetSlice)))
} else if routeConf.Op == "addFIBOnly" {
ribdServiceHandler.ProcessV4RouteCreateConfig(routeConf.OrigConfigObject.(*ribd.IPv4Route), FIBOnly, routeConf.AdditionalParams.(ribd.Int))
} else if routeConf.Op == "addBulk" {
ribdServiceHandler.ProcessBulkRouteCreateConfig(routeConf.OrigBulkRouteConfigObject) //.([]*ribd.IPv4Route))
} else if routeConf.Op == "del" {
ribdServiceHandler.ProcessV4RouteDeleteConfig(routeConf.OrigConfigObject.(*ribd.IPv4Route), FIBAndRIB)
} else if routeConf.Op == "delFIBOnly" {
ribdServiceHandler.ProcessV4RouteDeleteConfig(routeConf.OrigConfigObject.(*ribd.IPv4Route), FIBOnly)
} else if routeConf.Op == "update" {
if routeConf.PatchOp == nil || len(routeConf.PatchOp) == 0 {
ribdServiceHandler.Processv4RouteUpdateConfig(routeConf.OrigConfigObject.(*ribd.IPv4Route), routeConf.NewConfigObject.(*ribd.IPv4Route), routeConf.AttrSet)
} else {
ribdServiceHandler.Processv4RoutePatchUpdateConfig(routeConf.OrigConfigObject.(*ribd.IPv4Route), routeConf.NewConfigObject.(*ribd.IPv4Route), routeConf.PatchOp)
}
} else if routeConf.Op == "addv6" {
//create ipv6 route
ribdServiceHandler.ProcessV6RouteCreateConfig(routeConf.OrigConfigObject.(*ribd.IPv6Route), FIBAndRIB, ribd.Int(len(destNetSlice)))
} else if routeConf.Op == "addv6FIBOnly" {
//create ipv6 route
ribdServiceHandler.ProcessV6RouteCreateConfig(routeConf.OrigConfigObject.(*ribd.IPv6Route), FIBOnly, routeConf.AdditionalParams.(ribd.Int))
} else if routeConf.Op == "delv6" {
//delete ipv6 route
ribdServiceHandler.ProcessV6RouteDeleteConfig(routeConf.OrigConfigObject.(*ribd.IPv6Route), FIBAndRIB)
} else if routeConf.Op == "delv6FIBOnly" {
//delete ipv6 route
ribdServiceHandler.ProcessV6RouteDeleteConfig(routeConf.OrigConfigObject.(*ribd.IPv6Route), FIBOnly)
} else if routeConf.Op == "updatev6" {
//update ipv6 route
if routeConf.PatchOp == nil || len(routeConf.PatchOp) == 0 {
ribdServiceHandler.Processv6RouteUpdateConfig(routeConf.OrigConfigObject.(*ribd.IPv6Route), routeConf.NewConfigObject.(*ribd.IPv6Route), routeConf.AttrSet)
} else {
ribdServiceHandler.Processv6RoutePatchUpdateConfig(routeConf.OrigConfigObject.(*ribd.IPv6Route), routeConf.NewConfigObject.(*ribd.IPv6Route), routeConf.PatchOp)
}
}
}
}
}