/
vlan.go
426 lines (367 loc) · 9.97 KB
/
vlan.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright (c) 2022 NetLOX 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.
*/
package loxinet
import (
"errors"
"fmt"
"strings"
cmn "github.com/loxilb-io/loxilb/common"
tk "github.com/loxilb-io/loxilib"
)
// error codes for vlan mod api
const (
VlanBaseErr = iota - 2000
VlanExistsErr
VlaNotExistErr
VlanRangeErr
VlanAddBrpErr
VlanMpExistErr
VlanPortPhyErr
VlanPortExistErr
VlanPortTaggedErr
VlanNoPortErr
VlanPortCreateErr
VlanZoneErr
)
// constant to declare maximum number of vlans
const (
MaximumVlans = 4094
)
// vlanStat - statistics for vlan interface
type vlanStat struct {
inBytes uint64
inPackets uint64
outBytes uint64
outPackets uint64
}
// Vlan - vlan interface info
type Vlan struct {
VlanID int
Created bool
Name string
Zone string
NumTagPorts int
TaggedPorts [MaxInterfaces]*Port
NumUnTagPorts int
UnTaggedPorts [MaxInterfaces]*Port
Stat vlanStat
}
// VlansH - vlan context handler
type VlansH struct {
VlanMap [MaximumVlans]Vlan
Zone *Zone
}
// VlanInit - routine to initialize vlan context handler
func VlanInit(zone *Zone) *VlansH {
var nV = new(VlansH)
nV.Zone = zone
return nV
}
// VlanValid - routine to validate vlanId
func VlanValid(vlanID int) bool {
if vlanID > 0 && vlanID < MaximumVlans-1 {
return true
}
return false
}
// VlanGet - Routine to get vlan bridge details
func (V *VlansH) VlanGet() ([]cmn.VlanGet, error) {
ret := []cmn.VlanGet{}
for k, v := range V.VlanMap {
if v.Created {
tmpVlan := cmn.VlanGet{
Vid: k,
Dev: v.Name,
}
if v.NumTagPorts != 0 {
for _, port := range v.TaggedPorts {
if port != nil {
tmpSlave := cmn.VlanPortMod{
Vid: k,
Dev: port.Name,
Tagged: true,
}
tmpVlan.Member = append(tmpVlan.Member, tmpSlave)
}
}
}
if v.NumUnTagPorts != 0 {
for _, port := range v.UnTaggedPorts {
if port != nil {
tmpSlave := cmn.VlanPortMod{
Vid: k,
Dev: port.Name,
Tagged: false,
}
tmpVlan.Member = append(tmpVlan.Member, tmpSlave)
}
}
}
tmpVlan.Stat.InBytes = v.Stat.inBytes
tmpVlan.Stat.InPackets = v.Stat.inPackets
tmpVlan.Stat.OutBytes = v.Stat.outBytes
tmpVlan.Stat.OutPackets = v.Stat.outPackets
ret = append(ret, tmpVlan)
}
}
return ret, nil
}
// VlanAdd - routine to add vlan interface
func (V *VlansH) VlanAdd(vlanID int, name string, zone string, osid int, hwi PortHwInfo) (int, error) {
if VlanValid(vlanID) == false {
return VlanRangeErr, errors.New("Invalid VlanID")
}
if V.VlanMap[vlanID].Created == true {
return VlanExistsErr, errors.New("Vlan already created")
}
_, err := mh.zn.ZoneBrAdd(name, zone)
if err != nil {
return VlanExistsErr, errors.New("Vlan zone err")
}
ret, err := V.Zone.Ports.PortAdd(name, osid, cmn.PortVlanBr, zone, hwi, PortLayer2Info{false, vlanID})
if err != nil || ret != 0 {
tk.LogIt(tk.LogError, "Vlan bridge interface not created %d\n", ret)
mh.zn.ZoneBrDelete(name)
return VlanAddBrpErr, errors.New("Can't add vlan bridge")
}
v := &V.VlanMap[vlanID]
v.Name = name
v.VlanID = vlanID
v.Created = true
v.Zone = zone
tk.LogIt(tk.LogInfo, "vlan %d bd created\n", vlanID)
return 0, nil
}
// VlanDelete - routine to delete vlan interface
func (V *VlansH) VlanDelete(vlanID int) (int, error) {
if VlanValid(vlanID) == false {
return VlanRangeErr, errors.New("Invalid VlanID")
}
if V.VlanMap[vlanID].Created == false {
return VlaNotExistErr, errors.New("Vlan not yet created")
}
if V.VlanMap[vlanID].NumTagPorts != 0 ||
V.VlanMap[vlanID].NumUnTagPorts != 0 {
return VlanMpExistErr, errors.New("Vlan has ports configured")
}
v := &V.VlanMap[vlanID]
mh.zn.ZoneBrDelete(v.Name)
V.Zone.Ports.PortDel(v.Name, cmn.PortVlanBr)
v.DP(DpStatsClr)
v.Name = ""
v.VlanID = 0
v.Created = false
v.Zone = ""
tk.LogIt(tk.LogInfo, "vlan %d bd deleted\n", vlanID)
return 0, nil
}
// VlanPortAdd - routine to add a port membership to vlan
func (V *VlansH) VlanPortAdd(vlanID int, portName string, tagged bool) (int, error) {
if VlanValid(vlanID) == false {
return VlanRangeErr, errors.New("Invalid VlanID")
}
if V.VlanMap[vlanID].Created == false {
// FIXME : Do we create implicitly here
tk.LogIt(tk.LogError, "Vlan not created\n")
return VlaNotExistErr, errors.New("Vlan not created")
}
v := &V.VlanMap[vlanID]
p := V.Zone.Ports.PortFindByName(portName)
if p == nil {
tk.LogIt(tk.LogError, "Phy port not created %s\n", portName)
return VlanPortPhyErr, errors.New("Phy port not created")
}
if tagged {
var membPortName string
osID := 4000 + (vlanID * MaxRealInterfaces) + p.PortNo
membPortName = fmt.Sprintf("%s.%d", portName, vlanID)
if p.SInfo.PortType&cmn.PortVxlanBr == cmn.PortVxlanBr {
return VlanPortTaggedErr, errors.New("vxlan can not be tagged")
}
if v.TaggedPorts[p.PortNo] != nil {
return VlanPortExistErr, errors.New("vlan tag port exists")
}
hInfo := p.HInfo
hInfo.Real = p.Name
hInfo.Master = v.Name
if e, _ := V.Zone.Ports.PortAdd(membPortName, osID, cmn.PortVlanSif, v.Zone,
hInfo, PortLayer2Info{false, vlanID}); e == 0 {
tp := V.Zone.Ports.PortFindByName(membPortName)
if tp == nil {
return VlanPortCreateErr, errors.New("vlan tag port not created")
}
v.TaggedPorts[p.PortNo] = tp
v.NumTagPorts++
} else {
return VlanPortCreateErr, errors.New("vlan tag port create failed in DP")
}
} else {
if v.UnTaggedPorts[p.PortNo] != nil {
return VlanPortExistErr, errors.New("vlan untag port exists")
}
hInfo := p.HInfo
hInfo.Master = v.Name
if e, _ := V.Zone.Ports.PortAdd(portName, p.SInfo.OsID, cmn.PortVlanSif, v.Zone,
hInfo, PortLayer2Info{true, vlanID}); e == 0 {
v.UnTaggedPorts[p.PortNo] = p
v.NumUnTagPorts++
} else {
return VlanPortCreateErr, errors.New("vlan untag port create failed in DP")
}
}
return 0, nil
}
// VlanPortDelete - routine to delete a port membership from vlan
func (V *VlansH) VlanPortDelete(vlanID int, portName string, tagged bool) (int, error) {
if VlanValid(vlanID) == false {
return VlanRangeErr, errors.New("Invalid VlanID")
}
if V.VlanMap[vlanID].Created == false {
// FIXME : Do we create implicitly here ??
return VlaNotExistErr, errors.New("Vlan not created")
}
v := &V.VlanMap[vlanID]
p := V.Zone.Ports.PortFindByName(portName)
if p == nil {
return VlanPortPhyErr, errors.New("Phy port not created")
}
if tagged {
tp := v.TaggedPorts[p.PortNo]
if tp == nil {
return VlanNoPortErr, errors.New("No such tag port")
}
var membPortName string
membPortName = fmt.Sprintf("%s.%d", portName, vlanID)
V.Zone.Ports.PortDel(membPortName, cmn.PortVlanSif)
v.TaggedPorts[p.PortNo] = nil
v.NumTagPorts--
} else {
V.Zone.Ports.PortDel(portName, cmn.PortVlanSif)
v.UnTaggedPorts[p.PortNo] = nil
v.NumUnTagPorts--
}
return 0, nil
}
// VlanDestructAll - routine to delete all vlan interfaces
func (V *VlansH) VlanDestructAll() {
for i := 0; i < MaximumVlans; i++ {
v := V.VlanMap[i]
if v.Created == true {
vp := V.Zone.Ports.PortFindByName(v.Name)
if vp == nil {
continue
}
for p := 0; p < MaxInterfaces; p++ {
mp := v.TaggedPorts[p]
if mp != nil {
V.VlanPortDelete(i, mp.Name, true)
}
}
for p := 0; p < MaxInterfaces; p++ {
mp := v.UnTaggedPorts[p]
if mp != nil {
V.VlanPortDelete(i, mp.Name, false)
}
}
V.VlanDelete(i)
}
}
return
}
// Vlans2String - routine to convert vlan information to string
func (V *VlansH) Vlans2String(it IterIntf) error {
var s string
for i := 0; i < MaximumVlans; i++ {
s = ""
v := V.VlanMap[i]
if v.Created == true {
vp := V.Zone.Ports.PortFindByName(v.Name)
if vp != nil {
s += fmt.Sprintf("%-10s: ", vp.Name)
} else {
tk.LogIt(tk.LogError, "VLan %s not found\n", v.Name)
continue
}
s += fmt.Sprintf("Tagged- ")
for p := 0; p < MaxInterfaces; p++ {
mp := v.TaggedPorts[p]
if mp != nil {
s += fmt.Sprintf("%s,", mp.Name)
}
}
ts := strings.TrimSuffix(s, ",")
s = ts
s += fmt.Sprintf("\n%22s", "UnTagged- ")
for p := 0; p < MaxInterfaces; p++ {
mp := v.UnTaggedPorts[p]
if mp != nil {
s += fmt.Sprintf("%s,", mp.Name)
}
}
uts := strings.TrimSuffix(s, ",")
s = uts
s += fmt.Sprintf("\n")
it.NodeWalker(s)
}
}
return nil
}
// VlansSync - routine to sync vlan information with DP
func (V *VlansH) VlansSync() {
for i := 0; i < MaximumVlans; i++ {
v := &V.VlanMap[i]
if v.Created == true {
if v.Stat.inPackets != 0 || v.Stat.outPackets != 0 {
fmt.Printf("BD stats %d : in %v:%v out %v:%v\n",
i, v.Stat.inPackets, v.Stat.inBytes,
v.Stat.outPackets, v.Stat.outBytes)
}
v.DP(DpStatsGet)
}
}
}
// VlansTicker - ticker routine to sync all vlan information with datapath
func (V *VlansH) VlansTicker() {
V.VlansSync()
}
// DP - routine to sync vlan information with datapath
func (v *Vlan) DP(work DpWorkT) int {
if work == DpStatsGet {
iStat := new(StatDpWorkQ)
iStat.Work = work
iStat.Mark = uint32(v.VlanID)
iStat.Name = MapNameRxBD
iStat.Bytes = &v.Stat.inBytes
iStat.Packets = &v.Stat.inPackets
mh.dp.ToDpCh <- iStat
oStat := new(StatDpWorkQ)
oStat.Work = work
oStat.Mark = uint32(v.VlanID)
oStat.Name = MapNameTxBD
oStat.Bytes = &v.Stat.outBytes
oStat.Packets = &v.Stat.outPackets
mh.dp.ToDpCh <- oStat
return 0
} else if work == DpStatsClr {
cStat := new(StatDpWorkQ)
cStat.Work = work
cStat.Mark = uint32(v.VlanID)
cStat.Name = MapNameBD
mh.dp.ToDpCh <- cStat
return 0
}
return -1
}