Skip to content

Commit d037d0c

Browse files
Allow CIDR update for the shared network when the network IPs are not in use (i.e. IPs not allocated to any instances)
1 parent f0838cd commit d037d0c

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

engine/schema/src/main/java/com/cloud/vm/dao/NicDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public interface NicDao extends GenericDao<NicVO, Long> {
3434

3535
List<NicVO> listByNetworkId(long networkId);
3636

37+
List<NicVO> listNonDeallocatedByNetworkId(long networkId);
38+
3739
NicVO findByNtwkIdAndInstanceId(long networkId, long instanceId);
3840

3941
NicVO findByInstanceIdAndNetworkIdIncludingRemoved(long networkId, long instanceId);

engine/schema/src/main/java/com/cloud/vm/dao/NicDaoImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ public List<NicVO> listByNetworkId(long networkId) {
151151
return listBy(sc);
152152
}
153153

154+
@Override
155+
public List<NicVO> listNonDeallocatedByNetworkId(long networkId) {
156+
SearchCriteria<NicVO> sc = NonReleasedSearch.create();
157+
sc.setParameters("network", networkId);
158+
sc.setParameters("state", Nic.State.Deallocating);
159+
return listBy(sc);
160+
}
161+
154162
@Override
155163
public NicVO findByNtwkIdAndInstanceId(long networkId, long instanceId) {
156164
SearchCriteria<NicVO> sc = AllFieldsSearch.create();

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,8 +3090,8 @@ public Network updateGuestNetwork(final UpdateNetworkCmd cmd) {
30903090
if (dc.getNetworkType() == NetworkType.Basic) {
30913091
throw new InvalidParameterValueException("Guest VM CIDR can't be specified for zone with " + NetworkType.Basic + " networking");
30923092
}
3093-
if (network.getGuestType() != GuestType.Isolated) {
3094-
throw new InvalidParameterValueException("Can only allow IP Reservation in networks with guest type " + GuestType.Isolated);
3093+
if (network.getGuestType() != GuestType.Isolated && network.getGuestType() != GuestType.Shared) {
3094+
throw new InvalidParameterValueException("Can only allow IP Reservation in networks with guest types: " + GuestType.Isolated + " or " + GuestType.Shared);
30953095
}
30963096
if (networkOfferingChanged) {
30973097
throw new InvalidParameterValueException("Cannot specify this network offering change and guestVmCidr at same time. Specify only one.");
@@ -3111,34 +3111,57 @@ public Network updateGuestNetwork(final UpdateNetworkCmd cmd) {
31113111
// But in case networkCidr is a non null value (IP reservation already exists), it implies network cidr is networkCidr
31123112
if (networkCidr != null) {
31133113
if (!NetUtils.isNetworkAWithinNetworkB(guestVmCidr, networkCidr)) {
3114-
throw new InvalidParameterValueException("Invalid value of Guest VM CIDR. For IP Reservation, Guest VM CIDR should be a subset of network CIDR : " + networkCidr);
3114+
throw new InvalidParameterValueException("Invalid value of Guest VM CIDR. For IP Reservation, Guest VM CIDR should be a subset of network CIDR: " + networkCidr);
31153115
}
31163116
} else {
31173117
if (!NetUtils.isNetworkAWithinNetworkB(guestVmCidr, network.getCidr())) {
3118-
throw new InvalidParameterValueException("Invalid value of Guest VM CIDR. For IP Reservation, Guest VM CIDR should be a subset of network CIDR : " + network.getCidr());
3118+
throw new InvalidParameterValueException("Invalid value of Guest VM CIDR. For IP Reservation, Guest VM CIDR should be a subset of network CIDR: " + network.getCidr());
31193119
}
31203120
}
31213121

31223122
// This check makes sure there are no active IPs existing outside the guestVmCidr in the network
31233123
String[] guestVmCidrPair = guestVmCidr.split("\\/");
31243124
Long size = Long.valueOf(guestVmCidrPair[1]);
3125-
List<NicVO> nicsPresent = _nicDao.listByNetworkId(networkId);
3126-
31273125
String cidrIpRange[] = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], size);
31283126
s_logger.info("The start IP of the specified guest vm cidr is: " + cidrIpRange[0] + " and end IP is: " + cidrIpRange[1]);
31293127
long startIp = NetUtils.ip2Long(cidrIpRange[0]);
31303128
long endIp = NetUtils.ip2Long(cidrIpRange[1]);
31313129
long range = endIp - startIp + 1;
31323130
s_logger.info("The specified guest vm cidr has " + range + " IPs");
31333131

3134-
for (NicVO nic : nicsPresent) {
3132+
List<NicVO> nonDellocatedNicsPresent = _nicDao.listNonDeallocatedByNetworkId(networkId);
3133+
if (network.getGuestType() == GuestType.Shared) {
3134+
if (CollectionUtils.isNotEmpty(nonDellocatedNicsPresent)) {
3135+
throw new InvalidParameterValueException("IPs are in use, cannot apply reservation");
3136+
}
3137+
List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
3138+
if (CollectionUtils.isNotEmpty(vlans)) {
3139+
for (VlanVO vlan : vlans) {
3140+
if (vlan == null) {
3141+
continue;
3142+
}
3143+
String vlanIpRange = vlan.getIpRange();
3144+
if (vlanIpRange == null) {
3145+
continue;
3146+
}
3147+
String[] vlanRange = vlanIpRange.split("-");
3148+
String vlanStartIP = vlanRange[0];
3149+
String vlanEndIP = vlanRange[1];
3150+
if (!NetUtils.isIpWithInCidrRange(vlanStartIP, guestVmCidr) || !NetUtils.isIpWithInCidrRange(vlanEndIP, guestVmCidr)) {
3151+
throw new InvalidParameterValueException(String.format("CIDR doesn't include the IP range %s, cannot apply reservation", vlanIpRange));
3152+
}
3153+
}
3154+
}
3155+
}
3156+
3157+
for (NicVO nic : nonDellocatedNicsPresent) {
31353158
if (nic.getIPv4Address() == null) {
31363159
continue;
31373160
}
31383161
long nicIp = NetUtils.ip2Long(nic.getIPv4Address());
31393162
//check if nic IP is outside the guest vm cidr
31403163
if ((nicIp < startIp || nicIp > endIp) && nic.getState() != Nic.State.Deallocating) {
3141-
throw new InvalidParameterValueException("Active IPs like " + nic.getIPv4Address() + " exist outside the Guest VM CIDR. Cannot apply reservation ");
3164+
throw new InvalidParameterValueException("Active IPs like " + nic.getIPv4Address() + " exist outside the Guest VM CIDR. Cannot apply reservation");
31423165
}
31433166
}
31443167

0 commit comments

Comments
 (0)