Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly derive vlans required for vxlan networks #40

Merged
merged 1 commit into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/two_hosts_multiple_tenants_mix_vlan_vxlan.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"DefaultNetType" : "vxlan",
"SubnetPool" : "11.1.0.0/16",
"AllocSubnetLen" : 24,
"Vxlans" : "10001-14000",
"Vxlans" : "10001-12000",
"Networks" : [{
"Name" : "purple",
"Endpoints" : [{
Expand Down
78 changes: 62 additions & 16 deletions gstate/gstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,48 @@ func (g *Oper) Clear() error {
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) {

Expand All @@ -182,26 +224,30 @@ func (gc *Cfg) initVxlanBitset(vxlans string) (*resources.AutoVxlanCfgResource,
vxlanRsrcCfg.Vxlans.Set(uint(vxlan - vxlanRange.Min))
}

// XXX: we should derive free-vlans by looking at all tenants,
// instead of just one.
vlanRsrcCfg := &resources.AutoVlanCfgResource{}
vlanRsrcCfg.StateDriver = gc.StateDriver
err = vlanRsrcCfg.Read(gc.Tenant)
if core.ErrIfKeyExists(err) != nil {
availableVlans, err := deriveAvailableVlans(gc.StateDriver)
if err != nil {
return nil, 0, err
} else if err != nil {
// a vlan resource has not been defined, assume entire space available
// for vxlan.
vlanRsrcCfg.Vlans = netutils.CreateBitset(12)
vlanRsrcCfg.Vlans.ClearAll()
}
// available vlans are the ones that are configured to be not consumed by
// vlan networks
availableVlans := vlanRsrcCfg.Vlans.Complement()
clearReservedVlans(availableVlans)
if count := availableVlans.Count(); int(count) < vxlanRange.Max-vxlanRange.Min {

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
Expand Down
67 changes: 53 additions & 14 deletions netdcli/netdcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,32 @@ import (
"github.com/contiv/netplugin/drivers"
"github.com/contiv/netplugin/gstate"
"github.com/contiv/netplugin/netutils"
"github.com/contiv/netplugin/resources"
)

const (
CLI_CONSTRUCT_GLOBAL = "global"
CLI_CONSTRUCT_NW = "network"
CLI_CONSTRUCT_EP = "endpoint"
CLI_OPER_GET = "get"
CLI_OPER_CREATE = "create"
CLI_OPER_DELETE = "delete"
CLI_OPER_ATTACH = "attach"
CLI_OPER_DETACH = "detach"
CLI_CONSTRUCT_GLOBAL = "global"
CLI_CONSTRUCT_NW = "network"
CLI_CONSTRUCT_EP = "endpoint"
CLI_CONSTRUCT_VLAN_RSRC = "vlan-rsrc"
CLI_CONSTRUCT_VXLAN_RSRC = "vxlan-rsrc"
CLI_CONSTRUCT_SUBNET_RSRC = "subnet-rsrc"
CLI_OPER_GET = "get"
CLI_OPER_CREATE = "create"
CLI_OPER_DELETE = "delete"
CLI_OPER_ATTACH = "attach"
CLI_OPER_DETACH = "detach"
)

var constructs = []string{
CLI_CONSTRUCT_GLOBAL,
CLI_CONSTRUCT_NW,
CLI_CONSTRUCT_EP,
CLI_CONSTRUCT_VLAN_RSRC,
CLI_CONSTRUCT_VXLAN_RSRC,
CLI_CONSTRUCT_SUBNET_RSRC,
}

var validOperList = []string{CLI_OPER_GET, CLI_OPER_CREATE, CLI_OPER_DELETE, CLI_OPER_ATTACH, CLI_OPER_DETACH}

type CliError struct {
Expand Down Expand Up @@ -87,16 +100,18 @@ type Construct struct {
}

func (c *Construct) String() string {
return fmt.Sprintf("%s or %s", CLI_CONSTRUCT_NW, CLI_CONSTRUCT_EP)
return fmt.Sprintf("%s", constructs)
}

func (c *Construct) Set(val string) error {
if val != CLI_CONSTRUCT_NW && val != CLI_CONSTRUCT_EP && val != CLI_CONSTRUCT_GLOBAL {
return &CliError{Desc: fmt.Sprintf("invalid value for construct (%s). Allowed values: %s",
val, c.String())}
for _, str := range constructs {
if str == val {
c.val = val
return nil
}
}
c.val = val
return nil
return &CliError{Desc: fmt.Sprintf("invalid value for construct (%s). Allowed values: %s",
val, c.String())}
}

func (c *Construct) Get() interface{} {
Expand Down Expand Up @@ -444,6 +459,30 @@ func executeOpts(opts *cliOpts) error {
log.Fatalf("error '%s' \n", err)
}
return err
case CLI_CONSTRUCT_VLAN_RSRC:
fallthrough
case CLI_CONSTRUCT_VXLAN_RSRC:
fallthrough
case CLI_CONSTRUCT_SUBNET_RSRC:
if opts.oper.Get() == CLI_OPER_GET {
if CLI_CONSTRUCT_VLAN_RSRC == opts.construct.Get() {
rsrc := &resources.AutoVlanCfgResource{}
rsrc.StateDriver = etcdDriver
state = rsrc
}
if CLI_CONSTRUCT_VXLAN_RSRC == opts.construct.Get() {
rsrc := &resources.AutoVxlanCfgResource{}
rsrc.StateDriver = etcdDriver
state = rsrc
}
if CLI_CONSTRUCT_SUBNET_RSRC == opts.construct.Get() {
rsrc := &resources.AutoSubnetCfgResource{}
rsrc.StateDriver = etcdDriver
state = rsrc
}
} else {
return fmt.Errorf("Only get operation is supported for resources")
}
}

switch opts.oper.Get() {
Expand Down