// Init the state from configuration. func (r *AutoSubnetCfgResource) Init(rsrcCfg interface{}) error { cfg, ok := rsrcCfg.(*AutoSubnetCfgResource) if !ok { return core.Errorf("Invalid type for subnet resource config") } r.SubnetPool = cfg.SubnetPool r.SubnetPoolLen = cfg.SubnetPoolLen r.AllocSubnetLen = cfg.AllocSubnetLen if cfg.AllocSubnetLen < cfg.SubnetPoolLen { return core.Errorf("AllocSubnetLen should be greater than or equal to SubnetPoolLen") } err := r.Write() if err != nil { return err } defer func() { if err != nil { r.Clear() } }() allocSubnetSize := r.AllocSubnetLen - r.SubnetPoolLen oper := &AutoSubnetOperResource{FreeSubnets: netutils.CreateBitset(allocSubnetSize).Complement()} oper.StateDriver = r.StateDriver oper.ID = r.ID err = oper.Write() if err != nil { return err } return 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.Errorf("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 for i := 0; i < localVLANsReqd; i++ { clearBitMarker, _ = availableVLANs.NextSet(clearBitMarker) clearBitMarker++ } clearBitMarker++ for { if bit, ok := availableVLANs.NextSet(clearBitMarker); ok { availableVLANs.Clear(bit) } else { break } } } vxlanRsrcCfg.LocalVLANs = availableVLANs return vxlanRsrcCfg, freeVXLANsStart, nil }
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 }
// 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 }