diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java index c0e8d3e16a6..439879add9b 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java @@ -130,7 +130,7 @@ public class AddIpToVmNicCmd extends BaseAsyncCmd { try { result = _networkService.allocateSecondaryGuestIP(getNicId(), getIpaddress()); } catch (InsufficientAddressCapacityException e) { - throw new InvalidParameterValueException("Allocating guest ip for nic failed"); + throw new InvalidParameterValueException("Allocating guest ip for nic failed : " + e.getMessage()); } if (result != null) { diff --git a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java index 9fbfa279089..39b8470b8c2 100644 --- a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java +++ b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java @@ -51,4 +51,6 @@ public interface NicSecondaryIpDao extends GenericDao { NicSecondaryIpVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId, Long vmId, String vmIp); List getSecondaryIpAddressesForNic(long nicId); + + Long countByNicId(long nicId); } diff --git a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java index 2f3cc290b40..29af0198b47 100644 --- a/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java @@ -35,6 +35,7 @@ import com.cloud.utils.db.SearchCriteria.Op; public class NicSecondaryIpDaoImpl extends GenericDaoBase implements NicSecondaryIpDao { private final SearchBuilder AllFieldsSearch; private final GenericSearchBuilder IpSearch; + protected GenericSearchBuilder CountByNicId; protected NicSecondaryIpDaoImpl() { super(); @@ -50,6 +51,11 @@ public class NicSecondaryIpDaoImpl extends GenericDaoBase sc = CountByNicId.create(); + sc.setParameters("nic", nicId); + return customSearch(sc, null).get(0); + } } diff --git a/server/src/com/cloud/configuration/Config.java b/server/src/com/cloud/configuration/Config.java index 9117bc45e9d..6ba67749aef 100755 --- a/server/src/com/cloud/configuration/Config.java +++ b/server/src/com/cloud/configuration/Config.java @@ -402,6 +402,10 @@ public enum Config { "10", "The maximum number of subnets per customer gateway", null), + MaxNumberOfSecondaryIPsPerNIC( + "Network", ManagementServer.class, Integer.class, + "vm.network.nic.max.secondary.ipaddresses", "256", + "Specify the number of secondary ip addresses per nic per vm", null), // Console Proxy ConsoleProxyCapacityStandby( diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java b/server/src/com/cloud/network/NetworkServiceImpl.java index 056190f1c4a..399f08681d1 100755 --- a/server/src/com/cloud/network/NetworkServiceImpl.java +++ b/server/src/com/cloud/network/NetworkServiceImpl.java @@ -669,6 +669,14 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { 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; //Isolated network can exist in Basic zone only, so no need to verify the zone type diff --git a/setup/db/db/schema-430to440.sql b/setup/db/db/schema-430to440.sql index 1b6a9ab420f..0ce92023811 100644 --- a/setup/db/db/schema-430to440.sql +++ b/setup/db/db/schema-430to440.sql @@ -443,4 +443,4 @@ CREATE VIEW `cloud`.`user_vm_view` AS left join `cloud`.`user_vm_details` `custom_ram_size` ON (((`custom_ram_size`.`vm_id` = `cloud`.`vm_instance`.`id`) and (`custom_ram_size`.`name` = 'memory'))); - +INSERT INTO `cloud`.`configuration`(category, instance, component, name, value, description, default_value) VALUES ('NetworkManager', 'DEFAULT', 'management-server', 'vm.network.nic.max.secondary.ipaddresses', NULL, 'Specify the number of secondary ip addresses per nic per vm', '256') ON DUPLICATE KEY UPDATE category='NetworkManager'; \ No newline at end of file