Skip to content
This repository was archived by the owner on Jan 15, 2020. It is now read-only.

Commit f1c794d

Browse files
Hongtu ZangMice Xia
authored andcommitted
CLOUDSTACK-2160: fix bug add a huge size guest network will cause out of memory
Signed-off-by: Mice Xia <mice_xia@tcloudcomputing.com>
1 parent f101241 commit f1c794d

3 files changed

Lines changed: 12 additions & 14 deletions

File tree

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,20 +1666,17 @@ public Set<Long> getAvailableIps(Network network, String requestedIp) {
16661666
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
16671667
List<String> secondaryIps = _nicSecondaryIpDao.listSecondaryIpAddressInNetwork(network.getId());
16681668
ips.addAll(secondaryIps);
1669-
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
16701669
Set<Long> usedIps = new TreeSet<Long>();
1671-
1670+
16721671
for (String ip : ips) {
16731672
if (requestedIp != null && requestedIp.equals(ip)) {
16741673
s_logger.warn("Requested ip address " + requestedIp + " is already in use in network" + network);
16751674
return null;
16761675
}
1677-
1676+
16781677
usedIps.add(NetUtils.ip2Long(ip));
16791678
}
1680-
if (usedIps.size() != 0) {
1681-
allPossibleIps.removeAll(usedIps);
1682-
}
1679+
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]), usedIps);
16831680

16841681
String gateway = network.getGateway();
16851682
if ((gateway != null) && (allPossibleIps.contains(NetUtils.ip2Long(gateway))))

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,9 +2066,8 @@ public Network updateGuestNetwork(long networkId, String name, String displayTex
20662066
protected Set<Long> getAvailableIps(Network network, String requestedIp) {
20672067
String[] cidr = network.getCidr().split("/");
20682068
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
2069-
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
20702069
Set<Long> usedIps = new TreeSet<Long>();
2071-
2070+
20722071
for (String ip : ips) {
20732072
if (requestedIp != null && requestedIp.equals(ip)) {
20742073
s_logger.warn("Requested ip address " + requestedIp + " is already in use in network" + network);
@@ -2077,9 +2076,7 @@ protected Set<Long> getAvailableIps(Network network, String requestedIp) {
20772076

20782077
usedIps.add(NetUtils.ip2Long(ip));
20792078
}
2080-
if (usedIps.size() != 0) {
2081-
allPossibleIps.removeAll(usedIps);
2082-
}
2079+
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]), usedIps);
20832080

20842081
String gateway = network.getGateway();
20852082
if ((gateway != null) && (allPossibleIps.contains(NetUtils.ip2Long(gateway))))

utils/src/com/cloud/utils/net/NetUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public static String[] getIpRangeFromCidr(String cidr, long size) {
627627
return result;
628628
}
629629

630-
public static Set<Long> getAllIpsFromCidr(String cidr, long size) {
630+
public static Set<Long> getAllIpsFromCidr(String cidr, long size, Set<Long> usedIps) {
631631
assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
632632
Set<Long> result = new TreeSet<Long>();
633633
long ip = ip2Long(cidr);
@@ -639,8 +639,12 @@ public static Set<Long> getAllIpsFromCidr(String cidr, long size) {
639639

640640
end++;
641641
end = (end << (32 - size)) - 2;
642-
while (start <= end) {
643-
result.add(start);
642+
int maxIps = 255; // get 255 ips as maximum
643+
while (start <= end && maxIps > 0) {
644+
if (!usedIps.contains(start)){
645+
result.add(start);
646+
maxIps--;
647+
}
644648
start++;
645649
}
646650

0 commit comments

Comments
 (0)