CLOUDSTACK-2160: fix bug add a huge size guest network will cause out of memory

Signed-off-by: Mice Xia <mice_xia@tcloudcomputing.com>
This commit is contained in:
Hongtu Zang 2013-04-24 11:13:29 +08:00 committed by Mice Xia
parent f101241050
commit f1c794de1b
3 changed files with 12 additions and 14 deletions

View File

@ -1666,20 +1666,17 @@ public class NetworkModelImpl extends ManagerBase implements NetworkModel {
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
List<String> secondaryIps = _nicSecondaryIpDao.listSecondaryIpAddressInNetwork(network.getId());
ips.addAll(secondaryIps);
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
Set<Long> usedIps = new TreeSet<Long>();
for (String ip : ips) {
if (requestedIp != null && requestedIp.equals(ip)) {
s_logger.warn("Requested ip address " + requestedIp + " is already in use in network" + network);
return null;
}
usedIps.add(NetUtils.ip2Long(ip));
}
if (usedIps.size() != 0) {
allPossibleIps.removeAll(usedIps);
}
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]), usedIps);
String gateway = network.getGateway();
if ((gateway != null) && (allPossibleIps.contains(NetUtils.ip2Long(gateway))))

View File

@ -2066,9 +2066,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
protected Set<Long> getAvailableIps(Network network, String requestedIp) {
String[] cidr = network.getCidr().split("/");
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
Set<Long> usedIps = new TreeSet<Long>();
for (String ip : ips) {
if (requestedIp != null && requestedIp.equals(ip)) {
s_logger.warn("Requested ip address " + requestedIp + " is already in use in network" + network);
@ -2077,9 +2076,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
usedIps.add(NetUtils.ip2Long(ip));
}
if (usedIps.size() != 0) {
allPossibleIps.removeAll(usedIps);
}
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]), usedIps);
String gateway = network.getGateway();
if ((gateway != null) && (allPossibleIps.contains(NetUtils.ip2Long(gateway))))

View File

@ -627,7 +627,7 @@ public class NetUtils {
return result;
}
public static Set<Long> getAllIpsFromCidr(String cidr, long size) {
public static Set<Long> getAllIpsFromCidr(String cidr, long size, Set<Long> usedIps) {
assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
Set<Long> result = new TreeSet<Long>();
long ip = ip2Long(cidr);
@ -639,8 +639,12 @@ public class NetUtils {
end++;
end = (end << (32 - size)) - 2;
while (start <= end) {
result.add(start);
int maxIps = 255; // get 255 ips as maximum
while (start <= end && maxIps > 0) {
if (!usedIps.contains(start)){
result.add(start);
maxIps--;
}
start++;
}