Skip to content

Commit 4925b9f

Browse files
Damodar ReddyJayapal
authored andcommitted
CLOUDSTACK-2031:support for number of ips per nic limit needs to be added for the multiple ip address per nic
1 parent 4b3784a commit 4925b9f

File tree

6 files changed

+29
-2
lines changed

6 files changed

+29
-2
lines changed

api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void execute() throws ResourceUnavailableException, ResourceAllocationExc
130130
try {
131131
result = _networkService.allocateSecondaryGuestIP(getNicId(), getIpaddress());
132132
} catch (InsufficientAddressCapacityException e) {
133-
throw new InvalidParameterValueException("Allocating guest ip for nic failed");
133+
throw new InvalidParameterValueException("Allocating guest ip for nic failed : " + e.getMessage());
134134
}
135135

136136
if (result != null) {

engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ public interface NicSecondaryIpDao extends GenericDao<NicSecondaryIpVO, Long> {
5151
NicSecondaryIpVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId, Long vmId, String vmIp);
5252

5353
List<String> getSecondaryIpAddressesForNic(long nicId);
54+
55+
Long countByNicId(long nicId);
5456
}

engine/schema/src/com/cloud/vm/dao/NicSecondaryIpDaoImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public class NicSecondaryIpDaoImpl extends GenericDaoBase<NicSecondaryIpVO, Long> implements NicSecondaryIpDao {
3636
private final SearchBuilder<NicSecondaryIpVO> AllFieldsSearch;
3737
private final GenericSearchBuilder<NicSecondaryIpVO, String> IpSearch;
38+
protected GenericSearchBuilder<NicSecondaryIpVO, Long> CountByNicId;
3839

3940
protected NicSecondaryIpDaoImpl() {
4041
super();
@@ -50,6 +51,11 @@ protected NicSecondaryIpDaoImpl() {
5051
IpSearch.and("network", IpSearch.entity().getNetworkId(), Op.EQ);
5152
IpSearch.and("address", IpSearch.entity().getIp4Address(), Op.NNULL);
5253
IpSearch.done();
54+
55+
CountByNicId = createSearchBuilder(Long.class);
56+
CountByNicId.select(null, Func.COUNT, null);
57+
CountByNicId.and("nic", CountByNicId.entity().getNicId(), SearchCriteria.Op.EQ);
58+
CountByNicId.done();
5359
}
5460

5561
@Override
@@ -135,4 +141,11 @@ public NicSecondaryIpVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId
135141
sc.setParameters("address", vmIp);
136142
return findOneBy(sc);
137143
}
144+
145+
@Override
146+
public Long countByNicId(long nicId) {
147+
SearchCriteria<Long> sc = CountByNicId.create();
148+
sc.setParameters("nic", nicId);
149+
return customSearch(sc, null).get(0);
150+
}
138151
}

server/src/com/cloud/configuration/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ public enum Config {
402402
"10",
403403
"The maximum number of subnets per customer gateway",
404404
null),
405+
MaxNumberOfSecondaryIPsPerNIC(
406+
"Network", ManagementServer.class, Integer.class,
407+
"vm.network.nic.max.secondary.ipaddresses", "256",
408+
"Specify the number of secondary ip addresses per nic per vm", null),
405409

406410
// Console Proxy
407411
ConsoleProxyCapacityStandby(

server/src/com/cloud/network/NetworkServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,14 @@ public NicSecondaryIp allocateSecondaryGuestIP(final long nicId, String requeste
669669
throw new InvalidParameterValueException("Invalid network id is given");
670670
}
671671

672+
int maxAllowedIpsPerNic = NumbersUtil.parseInt(_configDao.getValue(Config.MaxNumberOfSecondaryIPsPerNIC.key()), 10);
673+
Long nicWiseIpCount = _nicSecondaryIpDao.countByNicId(nicId);
674+
if(nicWiseIpCount.intValue() >= maxAllowedIpsPerNic) {
675+
s_logger.error("Maximum Number of Ips \"vm.network.nic.max.secondary.ipaddresses = \"" + maxAllowedIpsPerNic + " per Nic has been crossed for the nic " + nicId + ".");
676+
throw new InsufficientAddressCapacityException("Maximum Number of Ips per Nic has been crossed.", Nic.class, nicId);
677+
}
678+
679+
672680
s_logger.debug("Calling the ip allocation ...");
673681
String ipaddr = null;
674682
//Isolated network can exist in Basic zone only, so no need to verify the zone type

setup/db/db/schema-430to440.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,4 @@ CREATE VIEW `cloud`.`user_vm_view` AS
443443
left join
444444
`cloud`.`user_vm_details` `custom_ram_size` ON (((`custom_ram_size`.`vm_id` = `cloud`.`vm_instance`.`id`) and (`custom_ram_size`.`name` = 'memory')));
445445

446-
446+
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';

0 commit comments

Comments
 (0)