mirror of https://github.com/apache/cloudstack.git
CLOUDSTACK-1647: IP Reservation should not happen if the guest-vm cidr and network cidr is not same but their start ip and end ip are same.
Signed-off-by: Sateesh Chodapuneedi <sateesh@apache.org>
This commit is contained in:
parent
5233e3216b
commit
5dc7387d3b
|
|
@ -2116,6 +2116,21 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
|
|||
}
|
||||
}
|
||||
|
||||
// In some scenarios even though guesVmCidr and network CIDR do not appear similar but
|
||||
// the IP ranges exactly matches, in these special cases make sure no Reservation gets applied
|
||||
if (network.getNetworkCidr() == null) {
|
||||
if (NetUtils.isSameIpRange(guestVmCidr, network.getCidr()) && !guestVmCidr.equals(network.getCidr())) {
|
||||
throw new InvalidParameterValueException("The Start IP and End IP of guestvmcidr: "+ guestVmCidr + " and CIDR: " + network.getCidr() + " are same, " +
|
||||
"even though both the cidrs appear to be different. As a precaution no IP Reservation will be applied.");
|
||||
}
|
||||
} else {
|
||||
if(NetUtils.isSameIpRange(guestVmCidr, network.getNetworkCidr()) && !guestVmCidr.equals(network.getNetworkCidr())) {
|
||||
throw new InvalidParameterValueException("The Start IP and End IP of guestvmcidr: "+ guestVmCidr + " and Network CIDR: " + network.getNetworkCidr() + " are same, " +
|
||||
"even though both the cidrs appear to be different. As a precaution IP Reservation will not be affected. If you want to reset IP Reservation, " +
|
||||
"specify guestVmCidr to be: " + network.getNetworkCidr());
|
||||
}
|
||||
}
|
||||
|
||||
// When reservation is applied for the first time, network_cidr will be null
|
||||
// Populate it with the actual network cidr
|
||||
if (network.getNetworkCidr() == null) {
|
||||
|
|
|
|||
|
|
@ -1023,6 +1023,34 @@ public class NetUtils {
|
|||
return NetUtils.getIpRangeStartIpFromCidr(splitResult[0], size);
|
||||
}
|
||||
|
||||
// Check if 2 CIDRs have exactly same IP Range
|
||||
public static boolean isSameIpRange (String cidrA, String cidrB) {
|
||||
|
||||
if(!NetUtils.isValidCIDR(cidrA)) {
|
||||
s_logger.info("Invalid value of cidr " + cidrA);
|
||||
return false;
|
||||
}
|
||||
if (!NetUtils.isValidCIDR(cidrB)) {
|
||||
s_logger.info("Invalid value of cidr " + cidrB);
|
||||
return false;
|
||||
}
|
||||
String[] cidrPairFirst = cidrA.split("\\/");
|
||||
String[] cidrPairSecond = cidrB.split("\\/");
|
||||
|
||||
Long networkSizeFirst = Long.valueOf(cidrPairFirst[1]);
|
||||
Long networkSizeSecond = Long.valueOf(cidrPairSecond[1]);
|
||||
String ipRangeFirst [] = NetUtils.getIpRangeFromCidr(cidrPairFirst[0], networkSizeFirst);
|
||||
String ipRangeSecond [] = NetUtils.getIpRangeFromCidr(cidrPairFirst[0], networkSizeSecond);
|
||||
|
||||
long startIpFirst = NetUtils.ip2Long(ipRangeFirst[0]);
|
||||
long endIpFirst = NetUtils.ip2Long(ipRangeFirst[1]);
|
||||
long startIpSecond = NetUtils.ip2Long(ipRangeSecond[0]);
|
||||
long endIpSecond = NetUtils.ip2Long(ipRangeSecond[1]);
|
||||
if(startIpFirst == startIpSecond && endIpFirst == endIpSecond) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static boolean validateGuestCidr(String cidr) {
|
||||
// RFC 1918 - The Internet Assigned Numbers Authority (IANA) has reserved the
|
||||
// following three blocks of the IP address space for private internets:
|
||||
|
|
|
|||
|
|
@ -136,4 +136,23 @@ public class NetUtilsTest extends TestCase {
|
|||
assertTrue(NetUtils.getPrimaryPvlanFromUri(uri).equals("123"));
|
||||
assertTrue(NetUtils.getIsolatedPvlanFromUri(uri).equals("456"));
|
||||
}
|
||||
|
||||
public void testIsSameIpRange() {
|
||||
//Test to check IP Range of 2 CIDRs
|
||||
String cidrFirst = "10.0.144.0/20";
|
||||
String cidrSecond = "10.0.151.0/20";
|
||||
String cidrThird = "10.0.144.0/21";
|
||||
assertTrue(NetUtils.isValidCIDR(cidrFirst));
|
||||
assertTrue(NetUtils.isValidCIDR(cidrSecond));
|
||||
assertTrue(NetUtils.isValidCIDR(cidrThird));
|
||||
|
||||
//Check for exactly same CIDRs
|
||||
assertTrue(NetUtils.isSameIpRange(cidrFirst, cidrFirst));
|
||||
//Check for 2 different CIDRs, but same IP Range
|
||||
assertTrue(NetUtils.isSameIpRange(cidrFirst, cidrSecond));
|
||||
//Check for 2 different CIDRs and different IP Range
|
||||
assertFalse(NetUtils.isSameIpRange(cidrFirst, cidrThird));
|
||||
//Check for Incorrect format of CIDR
|
||||
assertFalse(NetUtils.isSameIpRange(cidrFirst, "10.3.6.5/50"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue