Skip to content

Commit 5b85edb

Browse files
committed
bug CS-16034 getRandomIp can return -1 unexpectedly
also fixes unit test failures
1 parent d626e29 commit 5b85edb

3 files changed

Lines changed: 28 additions & 15 deletions

File tree

‎server/src/com/cloud/network/guru/GuestNetworkGuru.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Random;
2222
import java.util.Set;
23+
import java.util.SortedSet;
2324
import java.util.TreeSet;
2425

2526
import javax.ejb.Local;
@@ -239,7 +240,7 @@ public void deallocate(Network network, NicProfile nic, VirtualMachineProfile<?
239240
public Ip4Address acquireIp4Address(Network network, Ip4Address requestedIp, String reservationId) {
240241
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
241242
String[] cidr = network.getCidr().split("/");
242-
Set<Long> usedIps = new TreeSet<Long>();
243+
SortedSet<Long> usedIps = new TreeSet<Long>();
243244

244245
if (requestedIp != null && requestedIp.equals(network.getGateway())) {
245246
s_logger.warn("Requested ip address " + requestedIp + " is used as a gateway address in network " + network);

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.List;
3333
import java.util.Random;
3434
import java.util.Set;
35+
import java.util.SortedSet;
3536
import java.util.StringTokenizer;
3637
import java.util.TreeSet;
3738
import java.util.regex.Matcher;
@@ -651,39 +652,48 @@ public static Set<Long> getAllIpsFromCidr(String cidr, long size) {
651652
* @param avoid set of ips to avoid
652653
* @return ip that is within the cidr range but not in the avoid set. -1 if unable to find one.
653654
*/
654-
public static long getRandomIpFromCidr(String startIp, int size, Set<Long> avoid) {
655+
public static long getRandomIpFromCidr(String startIp, int size, SortedSet<Long> avoid) {
655656
return getRandomIpFromCidr(ip2Long(startIp), size, avoid);
656657

657658
}
658659

659660
/**
660661
* Given a cidr, this method returns an ip address within the range but
661-
* is not in the avoid list.
662+
* is not in the avoid list.
663+
* Note: the gateway address has to be specified in the avoid list
662664
*
663-
* @param startIp ip that the cidr starts with
665+
* @param cidr ip that the cidr starts with
664666
* @param size size of the cidr
665667
* @param avoid set of ips to avoid
666668
* @return ip that is within the cidr range but not in the avoid set. -1 if unable to find one.
667669
*/
668-
public static long getRandomIpFromCidr(long cidr, int size, Set<Long> avoid) {
670+
public static long getRandomIpFromCidr(long cidr, int size, SortedSet<Long> avoid) {
669671
assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
670672

671673
long startNetMask = ip2Long(getCidrNetmask(size));
672-
long startIp = (cidr & startNetMask) + 2;
673-
int range = 1 << (32 - size);
674+
long startIp = (cidr & startNetMask) + 1; //exclude the first ip since it isnt valid, e.g., 192.168.10.0
675+
int range = 1 << (32 - size); //e.g., /24 = 2^8 = 256
676+
range = range -1; //exclude end of the range since that is the broadcast address, e.g., 192.168.10.255
674677

675-
if (avoid.size() > range) {
678+
if (avoid.size() >= range) {
676679
return -1;
677680
}
678-
679-
for (int i = 0; i < range; i++) {
680-
int next = _rand.nextInt(range);
681-
if (!avoid.contains(startIp + next)) {
682-
return startIp + next;
681+
682+
//Reduce the range by the size of the avoid set
683+
//e.g., cidr = 192.168.10.0, size = /24, avoid = 192.168.10.1, 192.168.10.20, 192.168.10.254
684+
// range = 2^8 - 1 - 3 = 252
685+
range = range - avoid.size();
686+
int next = _rand.nextInt(range); //note: nextInt excludes last value
687+
long ip = startIp + next;
688+
for (Long avoidable : avoid) {
689+
if (ip >= avoidable) {
690+
ip++;
691+
} else {
692+
break;
683693
}
684694
}
685695

686-
return -1;
696+
return ip;
687697
}
688698

689699
public static String getIpRangeStartIpFromCidr(String cidr, long size) {

‎utils/test/com/cloud/utils/net/NetUtilsTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.cloud.utils.net;
1818

1919
import java.util.Set;
20+
import java.util.SortedSet;
2021
import java.util.TreeSet;
2122

2223
import junit.framework.TestCase;
@@ -37,7 +38,7 @@ public void testGetRandomIpFromCidr() {
3738
ip = NetUtils.getRandomIpFromCidr(cidr, 8, new TreeSet<Long>());
3839
assertEquals("The ip " + NetUtils.long2Ip(ip) + " retrieved must be within the cidr " + cidr + "/8", cidr.substring(0, 4), NetUtils.long2Ip(ip).substring(0, 4));
3940

40-
Set<Long> avoid = new TreeSet<Long>();
41+
SortedSet<Long> avoid = new TreeSet<Long>();
4142
ip = NetUtils.getRandomIpFromCidr(cidr, 30, avoid);
4243
assertTrue("We should be able to retrieve an ip on the first call.", ip != -1);
4344
avoid.add(ip);
@@ -49,6 +50,7 @@ public void testGetRandomIpFromCidr() {
4950
assertTrue("We should be able to retrieve an ip on the third call.", ip != -1);
5051
assertTrue("ip returned is not in the avoid list", !avoid.contains(ip));
5152
avoid.add(ip);
53+
5254
ip = NetUtils.getRandomIpFromCidr(cidr, 30, avoid);
5355
assertEquals("This should be -1 because we ran out of ip addresses: " + ip, ip, -1);
5456
}

0 commit comments

Comments
 (0)