Skip to content

Commit

Permalink
Merge pull request #774 from rchirakk/subnet
Browse files Browse the repository at this point in the history
check the ip address returned from nextClear() is in range
  • Loading branch information
Joji Mekkatt authored Mar 8, 2017
2 parents e77be75 + 4f5a39a commit 5cc4797
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions netmaster/master/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func networkAllocAddress(nwCfg *mastercfg.CfgNetworkState, epgCfg *mastercfg.End
} else {
if epgCfg != nil && len(epgCfg.IPPool) > 0 { // allocate from epg network
log.Infof("allocating ip address from epg pool %s", epgCfg.IPPool)
ipAddrValue, found = epgCfg.EPGIPAllocMap.NextClear(0)
ipAddrValue, found = netutils.NextClear(epgCfg.EPGIPAllocMap, 0, nwCfg.SubnetLen)
if !found {
log.Errorf("auto allocation failed - address exhaustion in pool %s",
epgCfg.IPPool)
Expand All @@ -378,7 +378,7 @@ func networkAllocAddress(nwCfg *mastercfg.CfgNetworkState, epgCfg *mastercfg.End
}
epgCfg.EPGIPAllocMap.Set(ipAddrValue)
} else {
ipAddrValue, found = nwCfg.IPAllocMap.NextClear(0)
ipAddrValue, found = netutils.NextClear(nwCfg.IPAllocMap, 0, nwCfg.SubnetLen)
if !found {
log.Errorf("auto allocation failed - address exhaustion in subnet %s/%d",
nwCfg.SubnetIP, nwCfg.SubnetLen)
Expand Down
55 changes: 55 additions & 0 deletions test/integration/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,61 @@ func (its *integTestSuite) TestEndpointGroupCreateDelete(c *C) {
}

assertNoErr(its.client.NetworkDelete("default", "test"), c, "deleting network")

// test network/epg subnet
nwData := []struct {
subnet string
availableIP string
}{

{subnet: "10.100.100.194-10.100.100.222/27", availableIP: "10.100.100.194-10.100.100.222"},
{subnet: "10.1.1.5-10.1.1.15/27", availableIP: "10.1.1.5-10.1.1.15"},
{subnet: "10.1.1.0/27", availableIP: "10.1.1.1-10.1.1.30"},
{subnet: "10.1.1.0/28", availableIP: "10.1.1.1-10.1.1.14"},
{subnet: "10.1.1.0/29", availableIP: "10.1.1.1-10.1.1.6"},
{subnet: "10.1.1.0/30", availableIP: "10.1.1.1-10.1.1.2"},
{subnet: "10.1.1.0/31"},
}
for _, nwRange := range nwData {
err := its.client.NetworkPost(&client.Network{
TenantName: "default",
NetworkName: "subnet-test1",
Subnet: nwRange.subnet,
Encap: its.encap,
})
assertNoErr(err, c, "creating network")

// inspect
nInspect, err := its.client.NetworkInspect("default", "subnet-test1")
assertNoErr(err, c, fmt.Sprintf("inspect failed for %+v", nwRange.subnet))
assertOnTrue(c, nInspect.Oper.AllocatedIPAddresses != "",
fmt.Sprintf("invalid allocated address %+v", nInspect))
assertOnTrue(c, nInspect.Oper.AvailableIPAddresses != nwRange.availableIP,
fmt.Sprintf("invalid available address %+v", nInspect))


// check epg
err = its.client.EndpointGroupPost(&client.EndpointGroup{
TenantName: "default",
NetworkName: "subnet-test1",
GroupName: "epg1",
IpPool: nwRange.availableIP,
Policies: []string{},
ExtContractsGrps: []string{},
})
assertNoErr(err, c, fmt.Sprintf("create epg %+v", "epg1"))
// inspect
dInspect, e := its.client.EndpointGroupInspect("default", "epg1")
assertNoErr(e, c, fmt.Sprintf("inspect failed for %+v", nwRange))
assertOnTrue(c, dInspect.Oper.AllocatedIPAddresses != "",
fmt.Sprintf("invalid allocated address %+v", dInspect))
assertOnTrue(c, dInspect.Oper.AvailableIPAddresses != nwRange.availableIP,
fmt.Sprintf("invalid available address %+v", dInspect))
assertNoErr(its.client.EndpointGroupDelete("default", "epg1"), c, "delete epg")

assertNoErr(its.client.NetworkDelete("default", "subnet-test1"), c, "deleting network")
}

}

// TestEndpointGrouIPPoolCreateDelete tests EPG with IPAM create delete ops
Expand Down
13 changes: 12 additions & 1 deletion utils/netutils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,17 @@ func ListAllocatedIPs(allocMap bitset.BitSet, ipPool string, subnetIP string, su
return strings.Join(list, ", ")
}

// NextClear wrapper around Bitset to check max id
func NextClear(allocMap bitset.BitSet, idx uint, subnetLen uint) (uint, bool) {
maxHosts := uint(1 << (32 - subnetLen))

value, found := allocMap.NextClear(idx)
if found && value >= maxHosts {
return 0, false
}
return value, found
}

// ListAvailableIPs returns a string of available IPs in a network
func ListAvailableIPs(allocMap bitset.BitSet, subnetIP string, subnetLen uint) string {
idx := uint(0)
Expand All @@ -1002,7 +1013,7 @@ func ListAvailableIPs(allocMap bitset.BitSet, subnetIP string, subnetLen uint) s
inRange := false

for {
foundValue, found := allocMap.NextClear(idx)
foundValue, found := NextClear(allocMap, idx, subnetLen)
if !found {
break
}
Expand Down

0 comments on commit 5cc4797

Please sign in to comment.