mirror of https://github.com/apache/cloudstack.git
Added ways to acquired ip to network guru
This commit is contained in:
parent
50cf9cb8fb
commit
0353133369
|
|
@ -31,6 +31,7 @@ import com.cloud.network.NetworkProfile;
|
|||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.component.Adapter;
|
||||
import com.cloud.utils.net.Ip4Address;
|
||||
import com.cloud.vm.NicProfile;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
|
|
@ -42,65 +43,159 @@ import com.cloud.vm.VirtualMachineProfile;
|
|||
* - Designs a virtual network depending on the network offering.
|
||||
* - Implements the virtual network when a virtual machine requires the network to be started.
|
||||
*
|
||||
* NetworkManager is responsible for figuring which NetworkGuru to use when
|
||||
* networks are created and nics are designed.
|
||||
* There can be multiple NetworkGurus in a CloudStack system. Each NetworkGuru
|
||||
* is used by CloudStack to issue isolation and ip addresses and to reclaim
|
||||
* resources when VMs are gone.
|
||||
*
|
||||
* A Network goes through the following life cycles through the NetworkGuru.
|
||||
* - When a guest network is created, NetworkGuru is asked to "design" the network.
|
||||
* This means the NetworkGuru checks the parameters such as cidr, gateway,
|
||||
* vlan, etc and returns a network that can work with those paremeters.
|
||||
* Note that at this point the network is only a virtual network. It has
|
||||
* not been substantiated with resources, such as vlan, to make the network
|
||||
* functional in the physical environment. At this stage, the network is in
|
||||
* Allocated state.
|
||||
*
|
||||
* - When the first virtual machine is about to be started and requires network
|
||||
* services, the guest network needs to have resources to make it usable
|
||||
* within the physical environment. At this time, the NetworkGuru is
|
||||
* called with the implement() method to acquire those resources.
|
||||
*
|
||||
* - For every virtual machine starting in the network, the NetworkGuru is
|
||||
* asked via the reserve() method to make sure everything the virtual
|
||||
* machine needs to be functional in the network is reserved.
|
||||
*
|
||||
* - For every virtual machine being stopped in the network, the NetworkGuru
|
||||
* is informed via the release() method to make sure resources occupied
|
||||
* by the virtual machine is released.
|
||||
*
|
||||
* - If all virtual machines within the network have been stopped, the guest
|
||||
* network is garbage collected. When a guest network is garbage collected
|
||||
* the NetworkGuru is informed via the shutdown() method to release any
|
||||
* resources it allocated to that network.
|
||||
*
|
||||
* - When a guest network is being deleted, the NetworkGuru is informed via
|
||||
* the trash() method.
|
||||
*
|
||||
*/
|
||||
public interface NetworkGuru extends Adapter {
|
||||
/**
|
||||
* Design a network configuration given the information.
|
||||
* @param offering network offering that contains the information.
|
||||
* @param plan where is this network configuration will be deployed.
|
||||
* @param userSpecified user specified parameters for this network configuration.
|
||||
* @param owner owner of this network configuration.
|
||||
* @return NetworkConfiguration
|
||||
* Cloud stack requires the NetworkGuru to design a guest network given
|
||||
* the software packages Once a NetworkGuru returns the designed network,
|
||||
* that NetworkGuru is forever associated with the guest network. It is
|
||||
* very important for the NetworkGuru implementation to be very specific
|
||||
* about the network it is responsible for designing. Things that can
|
||||
* be used to make determination can be isolation methods, services
|
||||
* provided on the guest network and the service provider that's on the
|
||||
* guest network.
|
||||
*
|
||||
* If a network is already fully substantiated with the necessary resources
|
||||
* during this design phase, then the state should be set to Setup. If
|
||||
* the resources are not allocated at this point, the state should be set
|
||||
* to Allocated.
|
||||
*
|
||||
* @param offering network offering that contains the package of services
|
||||
* the end user intends to use on that network.
|
||||
* @param plan where is this network being deployed.
|
||||
* @param userSpecified user specified parameters for this network.
|
||||
* @param owner owner of this network.
|
||||
* @return Network
|
||||
*/
|
||||
Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner);
|
||||
|
||||
/**
|
||||
* allocate a nic in this network. This method implementation cannot take a long time as it is meant to allocate for
|
||||
* the DB.
|
||||
* For guest networks that are in Allocated state after the design stage,
|
||||
* resources are allocated when the guest network is actually being used
|
||||
* by a virtual machine. implement() is called to acquire those resources.
|
||||
*
|
||||
* @param network
|
||||
* configuration to allocate the nic in.
|
||||
* @param nic
|
||||
* user specified
|
||||
* @param vm
|
||||
* virtual machine the network configuration will be in.
|
||||
* @return NicProfile.
|
||||
* @throws InsufficientVirtualNetworkCapcityException
|
||||
* @throws InsufficientAddressCapacityException
|
||||
* @param network network to be implemented.
|
||||
* @param offering network offering that the network was created with.
|
||||
* @param destination where the network is being deployed in.
|
||||
* @return a fully implemented Network.
|
||||
* @throws InsufficientVirtualNetworkCapcityException if there's not
|
||||
* enough resources to make the guest network usable in the physical
|
||||
* environment. At this time, the admin generally must be involved to
|
||||
* allocate more resources before any more guest network can be implemented.
|
||||
*/
|
||||
Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException;
|
||||
|
||||
/**
|
||||
* Once a guest network has been designed, virtual machines can be
|
||||
* created. allocated() is called for the NetworkGuru to design a nic
|
||||
* that will make the virtual machine work within the guest network.
|
||||
*
|
||||
* @param network guest network that the virtual machine will be deployed in.
|
||||
* @param nic nic information that the end user wants to set. The
|
||||
* NetworkGuru should check this information with the guest
|
||||
* network settings to make sure everything will work.
|
||||
* @param vm virtual machine that is about to be deployed.
|
||||
* @return NicProfile nic with all of the information
|
||||
* @throws InsufficientVirtualNetworkCapcityException if there's
|
||||
* insufficient capacity within the guest network.
|
||||
* @throws InsufficientAddressCapacityException if there are not addresses
|
||||
* to be assigned.
|
||||
*/
|
||||
NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
/**
|
||||
* Fully implement the network configuration as specified.
|
||||
*
|
||||
* @param network
|
||||
* network configuration
|
||||
* @param offering
|
||||
* offering that the network configuration was based on.
|
||||
* @param destination
|
||||
* where were deploying to.
|
||||
* @return a fully implemented NetworkConfiguration.
|
||||
* @throws InsufficientVirtualNetworkCapcityException
|
||||
*/
|
||||
Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException;
|
||||
|
||||
/**
|
||||
* reserve a nic for this VM in this network.
|
||||
* @param nic
|
||||
* @param network
|
||||
* @param vm
|
||||
* @param dest
|
||||
* @return
|
||||
* @throws InsufficientVirtualNetworkCapcityException
|
||||
* @throws InsufficientAddressCapacityException
|
||||
* @throws ConcurrentOperationException
|
||||
* NetworkGuru is asked to acquire an IPv4 address.
|
||||
* @param network guest network that this IPv4 address is acquired in.
|
||||
* @param requestedIp ip address that is being requested.
|
||||
* @param reservationId id used to refer to this reservation.
|
||||
* @return Ip4Address ipv4 address
|
||||
*/
|
||||
Ip4Address acquireIp4Address(Network network, String requestedIp, String reservationId) throws InsufficientAddressCapacityException;
|
||||
|
||||
/**
|
||||
* Release the IPv4 address acquired.
|
||||
*
|
||||
* @param network guest network that it was acquired in.
|
||||
* @param reservationId reservation id that it was acquired in.
|
||||
* @return true if release was successful; false if not.
|
||||
*/
|
||||
boolean releaseIp4Address(Network network, String reservationId);
|
||||
|
||||
/**
|
||||
* Once a guest network is implemented, then the virtual machine must
|
||||
* be allocated its resources in order for it to participate within the
|
||||
* guest network. reserve() is called for the NetworkGuru to make sure
|
||||
* that works.
|
||||
*
|
||||
* @param nic nic that the vm is using to access the guest network.
|
||||
* @param network guest network the vm is in.
|
||||
* @param vm vm
|
||||
* @param dest destination the vm is deployed to
|
||||
* @param context Reservation context from which to get the owner, caller, and reservation id
|
||||
* @throws InsufficientVirtualNetworkCapcityException if there's not enough
|
||||
* resources.
|
||||
* @throws InsufficientAddressCapacityException if there's not enough ip
|
||||
* addresses.
|
||||
* @throws ConcurrentOperationException if there are multiple operations
|
||||
* happening on this guest network or vm.
|
||||
*/
|
||||
void reserve(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException;
|
||||
|
||||
/**
|
||||
* When a virtual machine is stopped, the NetworkGuru is informed via the
|
||||
* release() method to release any resources.
|
||||
*
|
||||
* @param nic nic that the vm is using to access the guest network.
|
||||
* @param vm virtual machine
|
||||
* @param reservationId reservation id passed to it in the ReservationContext
|
||||
* @return true if release is successful or false if unsuccessful.
|
||||
*/
|
||||
boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId);
|
||||
|
||||
/**
|
||||
* When a virtual machine is destroyed, the NetworkGuru is informed via
|
||||
* the deallocate() method to make sure any resources that are allocated
|
||||
* are released.
|
||||
*
|
||||
* @param network guest network that the vm was running in.
|
||||
* @param nic nic that the vm was using to access the guest network.
|
||||
* @param vm virtual machine being destroyed.
|
||||
*/
|
||||
void deallocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm);
|
||||
|
||||
/**
|
||||
|
|
@ -112,17 +207,26 @@ public interface NetworkGuru extends Adapter {
|
|||
@Deprecated
|
||||
void updateNicProfile(NicProfile profile, Network network);
|
||||
|
||||
/**
|
||||
* When no virtual machines are running in the network, the network is
|
||||
* shutdown and all physical resources are released. The NetworkGuru is
|
||||
* informed via the shutdown method().
|
||||
*
|
||||
* @param network guest network being shut down
|
||||
* @param offering network offering the guest network was created with.
|
||||
*/
|
||||
void shutdown(NetworkProfile network, NetworkOffering offering);
|
||||
|
||||
/**
|
||||
* Throw away the design.
|
||||
* @param network
|
||||
* @param offering
|
||||
* @param owner
|
||||
* @return
|
||||
* When a guest network is destroyed, the NetworkGuru is informed via the
|
||||
* trash() method to recover any resources.
|
||||
*
|
||||
* @param network guest network being destroyed.
|
||||
* @param offering network offering the guest network was created with.
|
||||
* @param owner owner of the network.
|
||||
* @return true if trash was successful; false if not.
|
||||
*/
|
||||
boolean trash(Network network, NetworkOffering offering, Account owner);
|
||||
|
||||
void updateNetworkProfile(NetworkProfile networkProfile);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ import com.cloud.exception.InsufficientAddressCapacityException;
|
|||
import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
|
||||
import com.cloud.hypervisor.Hypervisor.HypervisorType;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.NetworkManager;
|
||||
import com.cloud.network.NetworkProfile;
|
||||
import com.cloud.network.NetworkVO;
|
||||
import com.cloud.network.Networks.AddressFormat;
|
||||
|
|
@ -42,7 +43,6 @@ import com.cloud.network.Networks.BroadcastDomainType;
|
|||
import com.cloud.network.Networks.Mode;
|
||||
import com.cloud.network.Networks.TrafficType;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.offerings.dao.NetworkOfferingDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.component.Inject;
|
||||
|
|
@ -58,7 +58,7 @@ import com.cloud.vm.VirtualMachineProfile;
|
|||
public class ControlNetworkGuru extends PodBasedNetworkGuru implements NetworkGuru {
|
||||
private static final Logger s_logger = Logger.getLogger(ControlNetworkGuru.class);
|
||||
@Inject DataCenterDao _dcDao;
|
||||
@Inject NetworkOfferingDao _networkOfferingDao;
|
||||
@Inject NetworkManager _networkMgr;
|
||||
String _cidr;
|
||||
String _gateway;
|
||||
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ import com.cloud.utils.component.AdapterBase;
|
|||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.db.DB;
|
||||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.net.Ip4Address;
|
||||
import com.cloud.vm.Nic.ReservationStrategy;
|
||||
import com.cloud.vm.NicProfile;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
|
|
@ -232,4 +233,14 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||
networkProfile.setDns1(dc.getDns1());
|
||||
networkProfile.setDns2(dc.getDns2());
|
||||
}
|
||||
|
||||
@Override public Ip4Address acquireIp4Address(Network network, String requestedIp, String reservationId) throws InsufficientAddressCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public boolean releaseIp4Address(Network network, String reservationId) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,11 @@
|
|||
*/
|
||||
package com.cloud.network.guru;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import javax.ejb.Local;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
|
@ -49,6 +54,7 @@ import com.cloud.user.Account;
|
|||
import com.cloud.user.UserContext;
|
||||
import com.cloud.utils.component.AdapterBase;
|
||||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.net.Ip4Address;
|
||||
import com.cloud.utils.net.NetUtils;
|
||||
import com.cloud.vm.Nic.ReservationStrategy;
|
||||
import com.cloud.vm.NicProfile;
|
||||
|
|
@ -70,6 +76,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||
protected NicDao _nicDao;
|
||||
@Inject
|
||||
protected NetworkDao _networkDao;
|
||||
Random _rand = new Random(System.currentTimeMillis());
|
||||
|
||||
String _defaultGateway;
|
||||
String _defaultCidr;
|
||||
|
|
@ -257,4 +264,50 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||
networkProfile.setDns1(dc.getDns1());
|
||||
networkProfile.setDns2(dc.getDns2());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ip4Address acquireIp4Address(Network network, String requestedIp, String reservationId) throws InsufficientAddressCapacityException {
|
||||
assert requestedIp == null || requestedIp.equals(reservationId) : "The GuestNetworkGuru relies on the fact that the reservationId is always the same as the requestedIp because at this point, there's no way for the GuestNetworkGuru to record down the reservation id. It can be done but I'm just lazy so don't you be lazy when you hit this assert!";
|
||||
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
|
||||
String[] cidr = network.getCidr().split("/");
|
||||
Set<Long> usedIps = new TreeSet<Long>();
|
||||
|
||||
int size = Integer.parseInt(cidr[1]);
|
||||
|
||||
// Let's prepare the list of ips already taken.
|
||||
usedIps.add(NetUtils.ip2Long(network.getGateway()));
|
||||
for (String ip : ips) {
|
||||
usedIps.add(NetUtils.ip2Long(ip));
|
||||
}
|
||||
|
||||
if (requestedIp != null) { // Make sure requestedIp is not already taken.
|
||||
if (usedIps.contains(requestedIp)) {
|
||||
throw new InsufficientAddressCapacityException("The ip requested " + requestedIp + " is already assigned within " + network, DataCenter.class, network.getDataCenterId());
|
||||
}
|
||||
|
||||
if (!NetUtils.sameSubnetCIDR(requestedIp, cidr[0], size)) {
|
||||
throw new IllegalArgumentException("The ip requested " + requestedIp + " does not match the cidr, " + network.getCidr() + ", of " + network);
|
||||
}
|
||||
|
||||
return new Ip4Address(requestedIp);
|
||||
}
|
||||
|
||||
long base = NetUtils.ip2Long(cidr[0]);
|
||||
int diff = 1 << size - usedIps.size();
|
||||
for (diff = 1 << size - usedIps.size(); diff > 0; diff--) {
|
||||
long nextIp = _rand.nextInt(1 << size);
|
||||
nextIp += base;
|
||||
if (!usedIps.contains(nextIp)) {
|
||||
return new Ip4Address(nextIp);
|
||||
}
|
||||
}
|
||||
|
||||
throw new InsufficientAddressCapacityException("Unable to map more ip addresses into a cidr of " + network.getCidr(), DataCenter.class, network.getDataCenterId());
|
||||
}
|
||||
|
||||
@Override public boolean releaseIp4Address(Network network, String reservationId) {
|
||||
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@ import com.cloud.deploy.DeploymentPlan;
|
|||
import com.cloud.exception.InsufficientAddressCapacityException;
|
||||
import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.NetworkManager;
|
||||
import com.cloud.network.NetworkProfile;
|
||||
import com.cloud.network.NetworkVO;
|
||||
import com.cloud.network.Networks.AddressFormat;
|
||||
|
|
@ -47,6 +46,7 @@ import com.cloud.utils.Pair;
|
|||
import com.cloud.utils.component.AdapterBase;
|
||||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.utils.net.Ip4Address;
|
||||
import com.cloud.utils.net.NetUtils;
|
||||
import com.cloud.vm.Nic.ReservationStrategy;
|
||||
import com.cloud.vm.NicProfile;
|
||||
|
|
@ -58,7 +58,6 @@ import com.cloud.vm.VirtualMachineProfile;
|
|||
public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru {
|
||||
private static final Logger s_logger = Logger.getLogger(PodBasedNetworkGuru.class);
|
||||
@Inject DataCenterDao _dcDao;
|
||||
@Inject NetworkManager _networkMgr;
|
||||
Random _rand = new Random(System.currentTimeMillis());
|
||||
|
||||
@Override
|
||||
|
|
@ -153,4 +152,14 @@ public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||
public boolean trash(Network config, NetworkOffering offering, Account owner) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean releaseIp4Address(Network network, String reservationId) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override public Ip4Address acquireIp4Address(Network network, String requestedIp, String reservationId) throws InsufficientAddressCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,13 +48,13 @@ import com.cloud.network.Networks.TrafficType;
|
|||
import com.cloud.network.addr.PublicIp;
|
||||
import com.cloud.network.dao.IPAddressDao;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.offerings.dao.NetworkOfferingDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.component.AdapterBase;
|
||||
import com.cloud.utils.component.Inject;
|
||||
import com.cloud.utils.db.DB;
|
||||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.utils.net.Ip4Address;
|
||||
import com.cloud.vm.Nic.ReservationStrategy;
|
||||
import com.cloud.vm.NicProfile;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
|
|
@ -73,23 +73,14 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||
NetworkManager _networkMgr;
|
||||
@Inject
|
||||
IPAddressDao _ipAddressDao;
|
||||
@Inject
|
||||
NetworkOfferingDao _networkOfferingDao;
|
||||
|
||||
protected boolean canHandle(NetworkOffering offering, DataCenter dc) {
|
||||
if (offering.getTrafficType() == TrafficType.Public && offering.isSystemOnly()) {
|
||||
return true;
|
||||
} else {
|
||||
s_logger.trace("We take care only of System Public Virtual Network");
|
||||
return false;
|
||||
}
|
||||
protected boolean canHandle(NetworkOffering offering) {
|
||||
return offering.getTrafficType() == TrafficType.Public && offering.isSystemOnly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Network design(NetworkOffering offering, DeploymentPlan plan, Network network, Account owner) {
|
||||
DataCenter dc = _dcDao.findById(plan.getDataCenterId());
|
||||
|
||||
if (!canHandle(offering, dc)) {
|
||||
if (!canHandle(offering)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -213,4 +204,14 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru {
|
|||
networkProfile.setDns1(dc.getDns1());
|
||||
networkProfile.setDns2(dc.getDns2());
|
||||
}
|
||||
|
||||
@Override public Ip4Address acquireIp4Address(Network network, String requestedIp, String reservationId) throws InsufficientAddressCapacityException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public boolean releaseIp4Address(Network network, String reservationId) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue