Пример #1
0
// AllocVXLAN allocates a new vxlan; ids for both the vxlan and vlan are returned.
func (gc *Cfg) AllocVXLAN(reqVxlan uint) (vxlan uint, localVLAN uint, err error) {

	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return 0, 0, err
	}
	ra := core.ResourceManager(tempRm)

	g := &Oper{}
	g.StateDriver = gc.StateDriver
	err = g.Read("")
	if err != nil {
		return 0, 0, err
	}

	if reqVxlan != 0 && reqVxlan <= g.FreeVXLANsStart {
		return 0, 0, errors.New("Requested vxlan is out of range")
	}

	if (reqVxlan != 0) && (reqVxlan >= g.FreeVXLANsStart) {
		reqVxlan = reqVxlan - g.FreeVXLANsStart
	}

	pair, err1 := ra.AllocateResourceVal("global", resources.AutoVXLANResource, reqVxlan)
	if err1 != nil {
		return 0, 0, err1
	}

	vxlan = pair.(resources.VXLANVLANPair).VXLAN + g.FreeVXLANsStart
	localVLAN = pair.(resources.VXLANVLANPair).VLAN

	return
}
Пример #2
0
// FreeVLAN releases a VLAN for a given ID.
func (gc *Cfg) FreeVLAN(vlan uint) error {
	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return err
	}
	ra := core.ResourceManager(tempRm)

	return ra.DeallocateResourceVal("global", resources.AutoVLANResource, vlan)
}
Пример #3
0
// Process validates, implements, and writes the state.
func (gc *Cfg) Process(res string) error {
	var err error

	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return err
	}

	ra := core.ResourceManager(tempRm)

	err = gc.checkErrors(res)
	if err != nil {
		return core.Errorf("process failed on error checks %s", err)
	}

	// Only define a vlan resource if a valid range was specified
	if res == "vlan" {
		if gc.Auto.VLANs != "" {
			var vlanRsrcCfg *bitset.BitSet
			vlanRsrcCfg, err = gc.initVLANBitset(gc.Auto.VLANs)
			if err != nil {
				return err
			}
			err = ra.DefineResource("global", resources.AutoVLANResource, vlanRsrcCfg)
			if err != nil {
				return err
			}
		}
	}
	// Only define a vxlan resource if a valid range was specified
	var freeVXLANsStart uint
	if res == "vxlan" {
		if gc.Auto.VXLANs != "" {
			var vxlanRsrcCfg *resources.AutoVXLANCfgResource
			vxlanRsrcCfg, freeVXLANsStart, err = gc.initVXLANBitset(gc.Auto.VXLANs)
			if err != nil {
				return err
			}
			err = ra.DefineResource("global", resources.AutoVXLANResource, vxlanRsrcCfg)
			if err != nil {
				return err
			}
		}

		g := &Oper{FreeVXLANsStart: freeVXLANsStart}

		g.StateDriver = gc.StateDriver
		err = g.Write()
		if err != nil {
			log.Errorf("error '%s' updating global oper state %v \n", err, g)
			return err
		}
	}

	log.Debugf("updating the global config to new state %v \n", gc)
	return nil
}
Пример #4
0
// GetVlansInUse gets the vlans that are currently in use
func (gc *Cfg) GetVlansInUse() (uint, string) {
	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		log.Errorf("error getting resource manager: %s", err)
		return 0, ""
	}
	ra := core.ResourceManager(tempRm)

	return ra.GetResourceList("global", resources.AutoVLANResource)
}
Пример #5
0
// AllocVLAN allocates a new VLAN resource. Returns an ID.
func (gc *Cfg) AllocVLAN(reqVlan uint) (uint, error) {
	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return 0, err
	}
	ra := core.ResourceManager(tempRm)

	vlan, err := ra.AllocateResourceVal("global", resources.AutoVLANResource, reqVlan)
	if err != nil {
		log.Errorf("alloc vlan failed: %q", err)
		return 0, err
	}

	return vlan.(uint), err
}
Пример #6
0
// UpdateResources deletes associated resources
func (gc *Cfg) UpdateResources(res string) error {
	log.Infof("Received update resource for res %s", res)
	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return err
	}

	ra := core.ResourceManager(tempRm)

	err = gc.checkErrors(res)
	if err != nil {
		return core.Errorf("process failed on error checks %s", err)
	}

	if res == "vlan" {
		var vlanRsrcCfg *bitset.BitSet
		vlanRsrcCfg, err = gc.initVLANBitset(gc.Auto.VLANs)
		if err != nil {
			return err
		}
		err = ra.RedefineResource("global", resources.AutoVLANResource, vlanRsrcCfg)
		if err != nil {
			log.Errorf("Error deleting vlan resource. Err: %v", err)
		}
	} else if res == "vxlan" {
		var vxlanRsrcCfg *resources.AutoVXLANCfgResource
		vxlanRsrcCfg, err = gc.initVXLANBitset(gc.Auto.VXLANs)
		if err != nil {
			return err
		}
		err = ra.RedefineResource("global", resources.AutoVXLANResource, vxlanRsrcCfg)
		if err != nil {
			log.Errorf("Error deleting vxlan resource. Err: %v", err)
		}
		g := &Oper{FreeVXLANsStart: vxlanRsrcCfg.FreeVXLANsStart}

		g.StateDriver = gc.StateDriver
		err = g.Write()
		if err != nil {
			log.Errorf("error '%s' updating global oper state %v \n", err, g)
			return err
		}
	}
	return err
}
Пример #7
0
// FreeVXLAN returns a VXLAN id to the pool.
func (gc *Cfg) FreeVXLAN(vxlan uint, localVLAN uint) error {
	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return err
	}
	ra := core.ResourceManager(tempRm)

	g := &Oper{}
	g.StateDriver = gc.StateDriver
	err = g.Read("")
	if err != nil {
		return nil
	}

	return ra.DeallocateResourceVal("global", resources.AutoVXLANResource,
		resources.VXLANVLANPair{
			VXLAN: vxlan - g.FreeVXLANsStart,
			VLAN:  localVLAN})
}
Пример #8
0
// DeleteResources deletes associated resources
func (gc *Cfg) DeleteResources() error {
	tempRm, err := resources.GetStateResourceManager()
	if err != nil {
		return err
	}

	ra := core.ResourceManager(tempRm)

	err = ra.UndefineResource("global", resources.AutoVLANResource)
	if err != nil {
		log.Errorf("Error deleting vlan resource. Err: %v", err)
	}

	err = ra.UndefineResource("global", resources.AutoVXLANResource)
	if err != nil {
		log.Errorf("Error deleting vxlan resource. Err: %v", err)
	}

	return err
}