use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class NetworkServiceImpl method allocateSecondaryGuestIP.
/**
* It allocates a secondary IP alias on the NIC. It can be either an Ipv4 or an Ipv6 or even both, according to the the given IpAddresses object.
*/
@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_SECONDARY_IP_ASSIGN, eventDescription = "assigning secondary ip to nic", create = true)
public NicSecondaryIp allocateSecondaryGuestIP(final long nicId, IpAddresses requestedIpPair) throws InsufficientAddressCapacityException {
Account caller = CallContext.current().getCallingAccount();
String ipv4Address = requestedIpPair.getIp4Address();
String ipv6Address = requestedIpPair.getIp6Address();
// check whether the nic belongs to user vm.
NicVO nicVO = _nicDao.findById(nicId);
if (nicVO == null) {
throw new InvalidParameterValueException("There is no NIC with the ID: " + nicId);
}
if (nicVO.getVmType() != VirtualMachine.Type.User) {
throw new InvalidParameterValueException(String.format("The NIC [%s] does not belong to a user VM", nicVO.getUuid()));
}
VirtualMachine vm = _userVmDao.findById(nicVO.getInstanceId());
if (vm == null) {
throw new InvalidParameterValueException(String.format("There is no VM with the NIC [%s]", nicVO.getUuid()));
}
final long networkId = nicVO.getNetworkId();
final Account ipOwner = _accountMgr.getAccount(vm.getAccountId());
// verify permissions
_accountMgr.checkAccess(caller, null, true, vm);
Network network = _networksDao.findById(networkId);
if (network == null) {
throw new InvalidParameterValueException("Invalid network id is given");
}
int maxAllowedIpsPerNic = NumbersUtil.parseInt(_configDao.getValue(Config.MaxNumberOfSecondaryIPsPerNIC.key()), 10);
Long nicWiseIpCount = _nicSecondaryIpDao.countByNicId(nicId);
if (nicWiseIpCount.intValue() >= maxAllowedIpsPerNic) {
s_logger.error("Maximum Number of Ips \"vm.network.nic.max.secondary.ipaddresses = \"" + maxAllowedIpsPerNic + " per Nic has been crossed for the nic " + nicId + ".");
throw new InsufficientAddressCapacityException("Maximum Number of Ips per Nic has been crossed.", Nic.class, nicId);
}
s_logger.debug("Calling the ip allocation ...");
String ipaddr = null;
String ip6addr = null;
// Isolated network can exist in Basic zone only, so no need to verify the zone type
if (network.getGuestType() == Network.GuestType.Isolated) {
if ((ipv4Address != null || NetUtils.isIpv4(network.getGateway()) && StringUtils.isBlank(ipv6Address))) {
ipaddr = _ipAddrMgr.allocateGuestIP(network, ipv4Address);
}
if (StringUtils.isNotBlank(ipv6Address)) {
ip6addr = ipv6AddrMgr.allocateGuestIpv6(network, ipv6Address);
}
} else if (network.getGuestType() == Network.GuestType.Shared) {
// for basic zone, need to provide the podId to ensure proper ip alloation
Long podId = null;
DataCenter dc = _dcDao.findById(network.getDataCenterId());
if (dc.getNetworkType() == NetworkType.Basic) {
VMInstanceVO vmi = (VMInstanceVO) vm;
podId = vmi.getPodIdToDeployIn();
if (podId == null) {
throw new InvalidParameterValueException("vm pod id is null in Basic zone; can't decide the range for ip allocation");
}
}
try {
if (ipv6Address != null) {
ip6addr = ipv6AddrMgr.allocatePublicIp6ForGuestNic(network, podId, ipOwner, ipv6Address);
} else {
ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, ipv4Address);
}
if (ipaddr == null && ipv6Address == null) {
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicId + " failed");
}
} catch (InsufficientAddressCapacityException e) {
s_logger.error("Allocating ip to guest nic " + nicId + " failed");
return null;
}
} else {
s_logger.error("AddIpToVMNic is not supported in this network...");
return null;
}
if (!StringUtils.isAllBlank(ipaddr, ip6addr)) {
// we got the ip addr so up the nics table and secodary ip
final String ip4AddrFinal = ipaddr;
final String ip6AddrFinal = ip6addr;
long id = Transaction.execute(new TransactionCallback<Long>() {
@Override
public Long doInTransaction(TransactionStatus status) {
boolean nicSecondaryIpSet = nicVO.getSecondaryIp();
if (!nicSecondaryIpSet) {
nicVO.setSecondaryIp(true);
// commit when previously set ??
s_logger.debug("Setting nics table ...");
_nicDao.update(nicId, nicVO);
}
s_logger.debug("Setting nic_secondary_ip table ...");
Long vmId = nicVO.getInstanceId();
NicSecondaryIpVO secondaryIpVO = new NicSecondaryIpVO(nicId, ip4AddrFinal, ip6AddrFinal, vmId, ipOwner.getId(), ipOwner.getDomainId(), networkId);
_nicSecondaryIpDao.persist(secondaryIpVO);
return secondaryIpVO.getId();
}
});
return getNicSecondaryIp(id);
} else {
return null;
}
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class NetworkModelImpl method getNextAvailableMacAddressInNetwork.
@Override
public String getNextAvailableMacAddressInNetwork(long networkId) throws InsufficientAddressCapacityException {
NetworkVO network = _networksDao.findById(networkId);
String mac = _networksDao.getNextAvailableMacAddress(networkId, MACIdentifier.value());
if (mac == null) {
throw new InsufficientAddressCapacityException("Unable to create another mac address", Network.class, networkId);
}
return mac;
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class PodBasedNetworkGuru method reserve.
@Override
public void reserve(NicProfile nic, Network config, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
Pod pod = dest.getPod();
boolean forSystemVms = vm.getType().equals(VirtualMachine.Type.ConsoleProxy) || vm.getType().equals(VirtualMachine.Type.SecondaryStorageVm);
PrivateAllocationData result = _dcDao.allocatePrivateIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId(), context.getReservationId(), forSystemVms);
if (result == null) {
throw new InsufficientAddressCapacityException("Unable to get a management ip address", Pod.class, pod.getId());
}
Integer vlan = result.getVlan();
nic.setIPv4Address(result.getIpAddress());
nic.setMacAddress(NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(result.getMacAddress(), NetworkModel.MACIdentifier.value())));
nic.setIPv4Gateway(pod.getGateway());
nic.setFormat(AddressFormat.Ip4);
String netmask = NetUtils.getCidrNetmask(pod.getCidrSize());
nic.setIPv4Netmask(netmask);
nic.setBroadcastType(BroadcastDomainType.Native);
if (vlan != null) {
nic.setBroadcastUri(BroadcastDomainType.Native.toUri(vlan));
} else {
nic.setBroadcastUri(null);
}
nic.setIsolationUri(null);
s_logger.debug("Allocated a nic " + nic + " for " + vm);
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class DirectPodBasedNetworkGuru method getIp.
@DB
protected void getIp(final NicProfile nic, final Pod pod, final VirtualMachineProfile vm, final Network network) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
final DataCenter dc = _dcDao.findById(pod.getDataCenterId());
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientAddressCapacityException>() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientAddressCapacityException {
PublicIp ip = null;
List<PodVlanMapVO> podRefs = _podVlanDao.listPodVlanMapsByPod(pod.getId());
VlanVO vlan = _vlanDao.findById(podRefs.get(0).getVlanDbId());
if (nic.getIPv4Address() == null) {
String podRangeGateway = null;
if (!podRefs.isEmpty()) {
podRangeGateway = vlan.getVlanGateway();
}
// Get ip address from the placeholder and don't allocate a new one
if (vm.getType() == VirtualMachine.Type.DomainRouter) {
Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId());
if (placeholderNic != null) {
IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), placeholderNic.getIPv4Address());
ip = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
s_logger.debug("Nic got an ip address " + placeholderNic.getIPv4Address() + " stored in placeholder nic for the network " + network + " and gateway " + podRangeGateway);
}
}
if (ip == null) {
ip = _ipAddrMgr.assignPublicIpAddress(dc.getId(), pod.getId(), vm.getOwner(), VlanType.DirectAttached, network.getId(), null, false, false);
}
nic.setIPv4Address(ip.getAddress().toString());
nic.setFormat(AddressFormat.Ip4);
nic.setIPv4Gateway(ip.getGateway());
nic.setIPv4Netmask(ip.getNetmask());
if (ip.getVlanTag() != null && ip.getVlanTag().equalsIgnoreCase(Vlan.UNTAGGED)) {
nic.setIsolationUri(IsolationType.Ec2.toUri(Vlan.UNTAGGED));
nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED));
nic.setBroadcastType(BroadcastDomainType.Native);
}
nic.setReservationId(String.valueOf(ip.getVlanTag()));
nic.setMacAddress(ip.getMacAddress());
// save the placeholder nic if the vm is the Virtual router
if (vm.getType() == VirtualMachine.Type.DomainRouter) {
Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId());
if (placeholderNic == null) {
s_logger.debug("Saving placeholder nic with ip4 address " + nic.getIPv4Address() + " for the network " + network);
_networkMgr.savePlaceholderNic(network, nic.getIPv4Address(), null, VirtualMachine.Type.DomainRouter);
}
}
}
/**
* Calculate the IPv6 Address the Instance will obtain using SLAAC and IPv6 EUI-64
*
* Linux, FreeBSD and Windows all calculate the same IPv6 address when configured properly.
*
* Using Router Advertisements the routers in the network should announce the IPv6 CIDR which is configured
* in in the vlan table in the database.
*
* This way the NIC will be populated with a IPv6 address on which the Instance is reachable.
*/
if (vlan.getIp6Cidr() != null) {
if (nic.getIPv6Address() == null) {
s_logger.debug("Found IPv6 CIDR " + vlan.getIp6Cidr() + " for VLAN " + vlan.getId());
nic.setIPv6Cidr(vlan.getIp6Cidr());
nic.setIPv6Gateway(vlan.getIp6Gateway());
IPv6Address ipv6addr = NetUtils.EUI64Address(vlan.getIp6Cidr(), nic.getMacAddress());
s_logger.info("Calculated IPv6 address " + ipv6addr + " using EUI-64 for NIC " + nic.getUuid());
nic.setIPv6Address(ipv6addr.toString());
}
} else {
s_logger.debug("No IPv6 CIDR configured for VLAN " + vlan.getId());
}
}
});
nic.setIPv4Dns1(dc.getDns1());
nic.setIPv4Dns2(dc.getDns2());
}
use of com.cloud.exception.InsufficientAddressCapacityException in project cloudstack by apache.
the class GuestNetworkGuru method allocate.
@Override
public NicProfile allocate(final Network network, NicProfile nic, final VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
assert network.getTrafficType() == TrafficType.Guest : "Look at my name! Why are you calling" + " me when the traffic type is : " + network.getTrafficType();
if (nic == null) {
nic = new NicProfile(ReservationStrategy.Start, null, null, null, null);
}
final DataCenter dc = _dcDao.findById(network.getDataCenterId());
if (nic.getIPv4Address() == null) {
nic.setBroadcastUri(network.getBroadcastUri());
nic.setIsolationUri(network.getBroadcastUri());
nic.setIPv4Gateway(network.getGateway());
String guestIp = null;
if (network.getSpecifyIpRanges()) {
_ipAddrMgr.allocateDirectIp(nic, dc, vm, network, nic.getRequestedIPv4(), null);
} else {
// if Vm is router vm and source nat is enabled in the network, set ip4 to the network gateway
boolean isGateway = false;
if (vm.getVirtualMachine().getType() == VirtualMachine.Type.DomainRouter) {
if (network.getVpcId() != null) {
final Vpc vpc = _vpcDao.findById(network.getVpcId());
// Redundant Networks need a guest IP that is not the same as the gateway IP.
if (_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.SourceNat, Provider.VPCVirtualRouter) && !vpc.isRedundant()) {
isGateway = true;
}
} else {
if (_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.SourceNat, Provider.VirtualRouter)) {
isGateway = true;
}
}
}
if (isGateway) {
guestIp = network.getGateway();
} else {
if (network.getGuestType() != GuestType.L2 && vm.getType() == VirtualMachine.Type.DomainRouter) {
Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null);
if (placeholderNic != null) {
s_logger.debug("Nic got an ip address " + placeholderNic.getIPv4Address() + " stored in placeholder nic for the network " + network);
guestIp = placeholderNic.getIPv4Address();
}
}
if (guestIp == null) {
if (vm.getVirtualMachine().getType() == VirtualMachine.Type.DomainRouter) {
guestIp = _ipAddrMgr.acquireGuestIpAddressByPlacement(network, nic.getRequestedIPv4());
} else {
guestIp = _ipAddrMgr.acquireGuestIpAddress(network, nic.getRequestedIPv4());
}
}
if (guestIp == null && network.getGuestType() != GuestType.L2 && !_networkModel.listNetworkOfferingServices(network.getNetworkOfferingId()).isEmpty()) {
throw new InsufficientVirtualNetworkCapacityException("Unable to acquire Guest IP" + " address for network " + network, DataCenter.class, dc.getId());
}
}
nic.setIPv4Address(guestIp);
if (network.getCidr() != null) {
nic.setIPv4Netmask(NetUtils.cidr2Netmask(_networkModel.getValidNetworkCidr(network)));
}
nic.setIPv4Dns1(dc.getDns1());
nic.setIPv4Dns2(dc.getDns2());
nic.setFormat(AddressFormat.Ip4);
}
}
nic.setReservationStrategy(ReservationStrategy.Start);
if (nic.getMacAddress() == null) {
nic.setMacAddress(_networkModel.getNextAvailableMacAddressInNetwork(network.getId()));
if (nic.getMacAddress() == null) {
throw new InsufficientAddressCapacityException("Unable to allocate more mac addresses", Network.class, network.getId());
}
}
return nic;
}
Aggregations