Example #1
0
// networkReleaseAddress release the ip address
func networkReleaseAddress(nwCfg *mastercfg.CfgNetworkState, ipAddress string) error {
	ipAddrValue, err := netutils.GetIPNumber(nwCfg.SubnetIP, nwCfg.SubnetLen, 32, ipAddress)
	if err != nil {
		log.Errorf("error getting host id from hostIP %s Subnet %s/%d. Error: %s",
			ipAddress, nwCfg.SubnetIP, nwCfg.SubnetLen, err)
		return err
	}

	nwCfg.IPAllocMap.Clear(ipAddrValue)
	nwCfg.EpCount--

	return nil
}
Example #2
0
func freeEndpointResources(epCfg *drivers.OvsCfgEndpointState,
	nwCfg *drivers.OvsCfgNetworkState) error {
	if epCfg.IPAddress == "" {
		return nil
	}

	ipAddrValue, err := netutils.GetIPNumber(
		nwCfg.SubnetIP, nwCfg.SubnetLen, 32, epCfg.IPAddress)
	if err != nil {
		log.Errorf("error getting host id from hostIP %s Subnet %s/%d. Error: %s",
			epCfg.IPAddress, nwCfg.SubnetIP, nwCfg.SubnetLen, err)
		return err
	}
	nwCfg.IPAllocMap.Clear(ipAddrValue)
	nwCfg.EpCount--

	return nil
}
Example #3
0
// Allocate an address from the network
func networkAllocAddress(nwCfg *mastercfg.CfgNetworkState, reqAddr string) (string, error) {
	var ipAddress string
	var ipAddrValue uint
	var found bool
	var err error

	// alloc address
	if reqAddr == "" {
		ipAddrValue, found = nwCfg.IPAllocMap.NextClear(0)
		if !found {
			log.Errorf("auto allocation failed - address exhaustion in subnet %s/%d",
				nwCfg.SubnetIP, nwCfg.SubnetLen)
			err = core.Errorf("auto allocation failed - address exhaustion in subnet %s/%d",
				nwCfg.SubnetIP, nwCfg.SubnetLen)
			return "", err
		}

		ipAddress, err = netutils.GetSubnetIP(nwCfg.SubnetIP, nwCfg.SubnetLen, 32, ipAddrValue)
		if err != nil {
			log.Errorf("create eps: error acquiring subnet ip. Error: %s", err)
			return "", err
		}
	} else if reqAddr != "" && nwCfg.SubnetIP != "" {
		ipAddrValue, err = netutils.GetIPNumber(nwCfg.SubnetIP, nwCfg.SubnetLen, 32, reqAddr)
		if err != nil {
			log.Errorf("create eps: error getting host id from hostIP %s Subnet %s/%d. Error: %s",
				reqAddr, nwCfg.SubnetIP, nwCfg.SubnetLen, err)
			return "", err
		}

		ipAddress = reqAddr
	}

	// Set the bitmap
	nwCfg.IPAllocMap.Set(ipAddrValue)

	err = nwCfg.Write()
	if err != nil {
		log.Errorf("error writing nw config. Error: %s", err)
		return "", err
	}

	return ipAddress, nil
}
Example #4
0
func allocSetEpAddress(ep *intent.ConfigEP, epCfg *drivers.OvsCfgEndpointState,
	nwCfg *drivers.OvsCfgNetworkState) (err error) {

	var ipAddrValue uint
	var found bool

	ipAddress := ep.IPAddress
	if ipAddress == "" {
		if ipAddrValue, found = nwCfg.IPAllocMap.NextClear(0); !found {
			log.Errorf("auto allocation failed - address exhaustion in subnet %s/%d",
				nwCfg.SubnetIP, nwCfg.SubnetLen)
			err = core.Errorf("auto allocation failed - address exhaustion in subnet %s/%d",
				nwCfg.SubnetIP, nwCfg.SubnetLen)
			return
		}
		ipAddress, err = netutils.GetSubnetIP(
			nwCfg.SubnetIP, nwCfg.SubnetLen, 32, ipAddrValue)
		if err != nil {
			log.Errorf("create eps: error acquiring subnet ip. Error: %s", err)
			return
		}
	} else if ipAddress != "" && nwCfg.SubnetIP != "" {
		ipAddrValue, err = netutils.GetIPNumber(
			nwCfg.SubnetIP, nwCfg.SubnetLen, 32, ipAddress)
		if err != nil {
			log.Errorf("create eps: error getting host id from hostIP %s Subnet %s/%d. Error: %s",
				ipAddress, nwCfg.SubnetIP, nwCfg.SubnetLen, err)
			return
		}
	}
	epCfg.IPAddress = ipAddress
	nwCfg.IPAllocMap.Set(ipAddrValue)

	// Set mac address which is derived from IP address
	ipAddr := net.ParseIP(ipAddress)
	macAddr := fmt.Sprintf("02:02:%02x:%02x:%02x:%02x", ipAddr[12], ipAddr[13], ipAddr[14], ipAddr[15])
	epCfg.MacAddress = macAddr

	return
}
Example #5
0
// Deallocate the resource. Must be passed a SubnetIPLenPair.
func (r *AutoSubnetCfgResource) Deallocate(value interface{}) error {
	oper := &AutoSubnetOperResource{}
	oper.StateDriver = r.StateDriver
	err := oper.Read(r.ID)
	if err != nil {
		return err
	}

	pair, ok := value.(SubnetIPLenPair)
	if !ok {
		return core.Errorf("Invalid type for subnet value")
	}

	if pair.Len != r.AllocSubnetLen {
		return core.Errorf("Invalid subnet length. Exp: %d Rcvd: %d",
			r.AllocSubnetLen, pair.Len)
	}

	var subnet uint
	subnet, err = netutils.GetIPNumber(r.SubnetPool.String(), r.SubnetPoolLen,
		pair.Len, pair.IP.String())
	if err != nil {
		return err
	}

	if oper.FreeSubnets.Test(subnet) {
		return nil
	}
	oper.FreeSubnets.Set(subnet)

	err = oper.Write()
	if err != nil {
		return err
	}
	return nil
}