-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathgstate.go
413 lines (342 loc) · 10.8 KB
/
gstate.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
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
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 gstate
import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"github.com/jainvipin/bitset"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/netutils"
"github.com/contiv/netplugin/resources"
)
const (
BASE_GLOBAL = "/contiv/"
CFG_GLOBAL_PREFIX = BASE_GLOBAL + "config/global/"
CFG_GLOBAL_PATH = CFG_GLOBAL_PREFIX + "%s"
OPER_GLOBAL_PREFIX = BASE_GLOBAL + "oper/global/"
OPER_GLOBAL_PATH = OPER_GLOBAL_PREFIX + "%s"
)
const (
VersionBeta1 = "0.01"
)
// specifies various parameters to choose the auto allocation values to pick from
// this allows mostly hands-free allocation of networks, endpoints, attach/detach
// operations without having to specify these each time an entity gets created
type AutoParams struct {
SubnetPool string `json:"subnetPool"`
SubnetLen uint `json:"subnetLen"`
AllocSubnetLen uint `json:"AllocSubnetLen"`
Vlans string `json:"Vlans"`
Vxlans string `json:"Vxlans"`
}
// specifies parameters that decides the deployment choices
type DeployParams struct {
DefaultNetType string `json:"defaultNetType"`
}
// global state of the network plugin
type Cfg struct {
core.CommonState
Version string `json:"version"`
Tenant string `json:"tenant"`
Auto AutoParams `json:auto"`
Deploy DeployParams `json:"deploy"`
}
type Oper struct {
core.CommonState
Tenant string `json:"tenant"`
FreeVxlansStart uint `json:"freeVxlansStart"`
}
func (gc *Cfg) Dump() error {
log.Printf("Global State %v \n", gc)
return nil
}
func (gc *Cfg) checkErrors() error {
var err error
if net.ParseIP(gc.Auto.SubnetPool) == nil {
return errors.New(fmt.Sprintf("invalid ip address pool %s",
gc.Auto.SubnetPool))
}
_, err = netutils.ParseTagRanges(gc.Auto.Vlans, "vlan")
if err != nil {
return err
}
_, err = netutils.ParseTagRanges(gc.Auto.Vxlans, "vxlan")
if err != nil {
return err
}
if gc.Deploy.DefaultNetType != "vlan" &&
gc.Deploy.DefaultNetType != "vxlan" {
return errors.New(fmt.Sprintf("unsupported net type %s",
gc.Deploy.DefaultNetType))
}
if gc.Auto.SubnetLen > gc.Auto.AllocSubnetLen {
return errors.New(fmt.Sprintf(
"subnet size %d is smaller than subnets to be allocated from it",
gc.Auto.SubnetLen, gc.Auto.AllocSubnetLen))
}
return err
}
func Parse(configBytes []byte) (*Cfg, error) {
var gc Cfg
err := json.Unmarshal(configBytes, &gc)
if err != nil {
return nil, err
}
err = gc.checkErrors()
if err != nil {
return nil, err
}
return &gc, err
}
func (gc *Cfg) Write() error {
key := fmt.Sprintf(CFG_GLOBAL_PATH, gc.Tenant)
return gc.StateDriver.WriteState(key, gc, json.Marshal)
}
func (gc *Cfg) Read(tenant string) error {
key := fmt.Sprintf(CFG_GLOBAL_PATH, tenant)
return gc.StateDriver.ReadState(key, gc, json.Unmarshal)
}
func (gc *Cfg) ReadAll() ([]core.State, error) {
return gc.StateDriver.ReadAllState(CFG_GLOBAL_PREFIX, gc, json.Unmarshal)
}
func (gc *Cfg) Clear() error {
key := fmt.Sprintf(CFG_GLOBAL_PATH, gc.Tenant)
return gc.StateDriver.ClearState(key)
}
func (g *Oper) Write() error {
key := fmt.Sprintf(OPER_GLOBAL_PATH, g.Tenant)
return g.StateDriver.WriteState(key, g, json.Marshal)
}
func (g *Oper) Read(tenant string) error {
key := fmt.Sprintf(OPER_GLOBAL_PATH, tenant)
return g.StateDriver.ReadState(key, g, json.Unmarshal)
}
func (g *Oper) ReadAll() ([]core.State, error) {
return g.StateDriver.ReadAllState(OPER_GLOBAL_PREFIX, g, json.Unmarshal)
}
func (g *Oper) Clear() error {
key := fmt.Sprintf(OPER_GLOBAL_PATH, g.Tenant)
return g.StateDriver.ClearState(key)
}
// The following function derives the number of available vlan tags.
// XXX: Since it is run at netmaster, it is guaranteed to have a consistent view of
// resource usage. Revisit if this assumption changes, as then it might need to
// be moved to resource-manager
func deriveAvailableVlans(stateDriver core.StateDriver) (*bitset.BitSet, error) {
// available vlans = vlan-space - For each tenant (vlans + local-vxlan-vlans)
availableVlans := netutils.CreateBitset(12)
// get all vlans
readVlanRsrc := &resources.AutoVlanCfgResource{}
readVlanRsrc.StateDriver = stateDriver
vlanRsrcs, err := readVlanRsrc.ReadAll()
if core.ErrIfKeyExists(err) != nil {
return nil, err
} else if err != nil {
vlanRsrcs = []core.State{}
}
for _, rsrc := range vlanRsrcs {
cfg := rsrc.(*resources.AutoVlanCfgResource)
availableVlans = availableVlans.Union(cfg.Vlans)
}
//get all vxlan-vlans
readVxlanRsrc := &resources.AutoVxlanCfgResource{}
readVxlanRsrc.StateDriver = stateDriver
vxlanRsrcs, err := readVxlanRsrc.ReadAll()
if core.ErrIfKeyExists(err) != nil {
return nil, err
} else if err != nil {
vxlanRsrcs = []core.State{}
}
for _, rsrc := range vxlanRsrcs {
cfg := rsrc.(*resources.AutoVxlanCfgResource)
availableVlans = availableVlans.Union(cfg.LocalVlans)
}
// subtract to get availableVlans
availableVlans = availableVlans.Complement()
clearReservedVlans(availableVlans)
return availableVlans, nil
}
func (gc *Cfg) initVxlanBitset(vxlans string) (*resources.AutoVxlanCfgResource,
uint, error) {
vxlanRsrcCfg := &resources.AutoVxlanCfgResource{}
vxlanRsrcCfg.Vxlans = netutils.CreateBitset(14)
vxlanRange := netutils.TagRange{}
vxlanRanges, err := netutils.ParseTagRanges(vxlans, "vxlan")
if err != nil {
return nil, 0, err
}
// XXX: REVISIT, we seem to accept one contiguous vxlan range
vxlanRange = vxlanRanges[0]
freeVxlansStart := uint(vxlanRange.Min)
for vxlan := vxlanRange.Min; vxlan <= vxlanRange.Max; vxlan++ {
vxlanRsrcCfg.Vxlans.Set(uint(vxlan - vxlanRange.Min))
}
availableVlans, err := deriveAvailableVlans(gc.StateDriver)
if err != nil {
return nil, 0, err
}
localVlansReqd := vxlanRange.Max - vxlanRange.Min + 1
if count := availableVlans.Count(); int(count) < localVlansReqd {
return nil, 0, &core.Error{Desc: fmt.Sprintf("Available free local vlans (%d) is less than possible vxlans (%d)",
count, vxlanRange.Max-vxlanRange.Min)}
} else if int(count) > localVlansReqd {
//only reserve the #vxlan amount of bits
var clearBitMarker uint = 0
for i := 0; i < localVlansReqd; i++ {
clearBitMarker, _ = availableVlans.NextSet(clearBitMarker)
clearBitMarker += 1
}
clearBitMarker += 1
for {
if bit, ok := availableVlans.NextSet(clearBitMarker); ok {
availableVlans.Clear(bit)
} else {
break
}
}
}
vxlanRsrcCfg.LocalVlans = availableVlans
return vxlanRsrcCfg, freeVxlansStart, nil
}
func (gc *Cfg) AllocVxlan(ra core.ResourceManager) (vxlan uint,
localVlan uint, err error) {
pair, err1 := ra.AllocateResourceVal(gc.Tenant, resources.AUTO_VXLAN_RSRC)
if err1 != nil {
return 0, 0, err1
}
g := &Oper{}
g.StateDriver = gc.StateDriver
err = g.Read(gc.Tenant)
if err != nil {
return 0, 0, err
}
vxlan = pair.(resources.VxlanVlanPair).Vxlan + g.FreeVxlansStart
localVlan = pair.(resources.VxlanVlanPair).Vlan
return
}
func (gc *Cfg) FreeVxlan(ra core.ResourceManager, vxlan uint, localVlan uint) error {
g := &Oper{}
g.StateDriver = gc.StateDriver
err := g.Read(gc.Tenant)
if err != nil {
return nil
}
return ra.DeallocateResourceVal(gc.Tenant, resources.AUTO_VXLAN_RSRC,
resources.VxlanVlanPair{
Vxlan: vxlan - g.FreeVxlansStart,
Vlan: localVlan})
}
func clearReservedVlans(vlanBitset *bitset.BitSet) {
vlanBitset.Clear(0)
vlanBitset.Clear(4095)
}
func (gc *Cfg) initVlanBitset(vlans string) (*bitset.BitSet, error) {
vlanBitset := netutils.CreateBitset(12)
vlanRanges, err := netutils.ParseTagRanges(vlans, "vlan")
if err != nil {
return nil, err
}
for _, vlanRange := range vlanRanges {
for vlan := vlanRange.Min; vlan <= vlanRange.Max; vlan++ {
vlanBitset.Set(uint(vlan))
}
}
clearReservedVlans(vlanBitset)
return vlanBitset, nil
}
func (gc *Cfg) AllocVlan(ra core.ResourceManager) (uint, error) {
vlan, err := ra.AllocateResourceVal(gc.Tenant, resources.AUTO_VLAN_RSRC)
if err != nil {
log.Printf("alloc vlan failed: %q", err)
return 0, err
}
return vlan.(uint), err
}
func (gc *Cfg) FreeVlan(ra core.ResourceManager, vlan uint) error {
return ra.DeallocateResourceVal(gc.Tenant, resources.AUTO_VLAN_RSRC, vlan)
}
func (gc *Cfg) AllocSubnet(ra core.ResourceManager) (string, error) {
pair, err := ra.AllocateResourceVal(gc.Tenant, resources.AUTO_SUBNET_RSRC)
if err != nil {
return "", err
}
return pair.(resources.SubnetIpLenPair).Ip.String(), err
}
func (gc *Cfg) FreeSubnet(ra core.ResourceManager, subnetIp string) error {
return ra.DeallocateResourceVal(gc.Tenant, resources.AUTO_SUBNET_RSRC,
resources.SubnetIpLenPair{
Ip: net.ParseIP(subnetIp),
Len: gc.Auto.AllocSubnetLen})
}
func (gc *Cfg) Process(ra core.ResourceManager) error {
var err error
if gc.Version != VersionBeta1 {
return &core.Error{Desc: fmt.Sprintf("unsupported verison %s",
gc.Version)}
}
err = gc.checkErrors()
if err != nil {
return &core.Error{Desc: fmt.Sprintf(
"process failed on error checks %s \n", err)}
}
tenant := gc.Tenant
if tenant == "" {
return &core.Error{Desc: "null tenant"}
}
subnetRsrcCfg := &resources.AutoSubnetCfgResource{
SubnetPool: net.ParseIP(gc.Auto.SubnetPool),
SubnetPoolLen: gc.Auto.SubnetLen,
AllocSubnetLen: gc.Auto.AllocSubnetLen}
err = ra.DefineResource(tenant, resources.AUTO_SUBNET_RSRC, subnetRsrcCfg)
if err != nil {
return err
}
// Only define a vlan resource if a valid range was specified
if gc.Auto.Vlans != "" {
var vlanRsrcCfg *bitset.BitSet
vlanRsrcCfg, err = gc.initVlanBitset(gc.Auto.Vlans)
if err != nil {
return err
}
err = ra.DefineResource(tenant, resources.AUTO_VLAN_RSRC, vlanRsrcCfg)
if err != nil {
return err
}
}
// Only define a vxlan resource if a valid range was specified
var freeVxlansStart uint = 0
if gc.Auto.Vxlans != "" {
var vxlanRsrcCfg *resources.AutoVxlanCfgResource
vxlanRsrcCfg, freeVxlansStart, err = gc.initVxlanBitset(gc.Auto.Vxlans)
if err != nil {
return err
}
err = ra.DefineResource(tenant, resources.AUTO_VXLAN_RSRC, vxlanRsrcCfg)
if err != nil {
return err
}
}
g := &Oper{Tenant: gc.Tenant,
FreeVxlansStart: freeVxlansStart}
g.StateDriver = gc.StateDriver
err = g.Write()
if err != nil {
log.Printf("error '%s' updating goper state %v \n", err, g)
return err
}
log.Printf("updating the global config to new state %v \n", gc)
return nil
}