Skip to content

Commit

Permalink
Merge pull request #439 from shaleman/iprange
Browse files Browse the repository at this point in the history
enhance ip range config
  • Loading branch information
shaleman authored Jun 30, 2016
2 parents 76e212c + 32609c4 commit e1dbb39
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion netmaster/master/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func CreateNetwork(network intent.ConfigNetwork, stateDriver core.StateDriver, t

netutils.InitSubnetBitset(&nwCfg.IPAllocMap, nwCfg.SubnetLen)
subnetAddr := netutils.GetSubnetAddr(nwCfg.SubnetIP, nwCfg.SubnetLen)
nwCfg.SubnetIP = subnetAddr
nwCfg.IPAddrRange = netutils.GetIPAddrRange(subnetIP, subnetLen)

if network.Gateway != "" {
nwCfg.Gateway = network.Gateway
Expand All @@ -152,7 +154,6 @@ func CreateNetwork(network intent.ConfigNetwork, stateDriver core.StateDriver, t
if strings.Contains(subnetIP, "-") {
netutils.SetBitsOutsideRange(&nwCfg.IPAllocMap, subnetIP, subnetLen)
}
nwCfg.SubnetIP = subnetAddr

if network.IPv6Gateway != "" {
nwCfg.IPv6Gateway = network.IPv6Gateway
Expand Down Expand Up @@ -516,6 +517,7 @@ func ListAllocatedIPs(nwCfg *mastercfg.CfgNetworkState) string {
inRange := false

netutils.ClearReservedEntries(&nwCfg.IPAllocMap, nwCfg.SubnetLen)
netutils.ClearBitsOutsideRange(&nwCfg.IPAllocMap, nwCfg.IPAddrRange, nwCfg.SubnetLen)
for {
foundValue, found := nwCfg.IPAllocMap.NextSet(idx)
if !found {
Expand All @@ -542,6 +544,39 @@ func ListAllocatedIPs(nwCfg *mastercfg.CfgNetworkState) string {
return strings.Join(list, ", ")
}

// ListAvailableIPs returns a string of available IPs in a network
func ListAvailableIPs(nwCfg *mastercfg.CfgNetworkState) string {
idx := uint(0)
startIdx := idx
list := []string{}
inRange := false

for {
foundValue, found := nwCfg.IPAllocMap.NextClear(idx)
if !found {
break
}

if !inRange { // begin of range
startIdx = foundValue
inRange = true
} else if foundValue > idx { // end of range
thisRange := getIPRange(nwCfg, startIdx, idx-1)
list = append(list, thisRange)
startIdx = foundValue
}
idx = foundValue + 1
}

// list end with allocated value
if inRange {
thisRange := getIPRange(nwCfg, startIdx, idx-1)
list = append(list, thisRange)
}

return strings.Join(list, ", ")
}

// Allocate an address from the network
func networkAllocAddress(nwCfg *mastercfg.CfgNetworkState, reqAddr string, isIPv6 bool) (string, error) {
var ipAddress string
Expand Down
1 change: 1 addition & 0 deletions netmaster/mastercfg/networkstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type CfgNetworkState struct {
SubnetIP string `json:"subnetIP"`
SubnetLen uint `json:"subnetLen"`
Gateway string `json:"gateway"`
IPAddrRange string `json:"ipAddrRange"`
EpAddrCount int `json:"epAddrCount"`
EpCount int `json:"epCount"`
IPAllocMap bitset.BitSet `json:"ipAllocMap"`
Expand Down
8 changes: 4 additions & 4 deletions netmaster/objApi/apiController.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package objApi
import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/contiv/contivmodel"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/netmaster/gstate"
Expand All @@ -26,8 +29,6 @@ import (
"github.com/contiv/netplugin/netmaster/mastercfg"
"github.com/contiv/netplugin/utils"
"github.com/contiv/objdb/modeldb"
"strconv"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -727,6 +728,7 @@ func (ac *APIController) NetworkGetOper(network *contivModel.NetworkInspect) err
}

network.Oper.AllocatedAddressesCount = nwCfg.EpAddrCount
network.Oper.AvailableIPAddresses = master.ListAvailableIPs(nwCfg)
network.Oper.AllocatedIPAddresses = master.ListAllocatedIPs(nwCfg)
network.Oper.DnsServerIP = nwCfg.DNSServer
network.Oper.ExternalPktTag = nwCfg.ExtPktTag
Expand Down Expand Up @@ -755,8 +757,6 @@ func (ac *APIController) NetworkGetOper(network *contivModel.NetworkInspect) err
epOper.Labels = fmt.Sprintf("%s", ep.Labels)
epOper.ContainerID = ep.ContainerID
network.Oper.Endpoints = append(network.Oper.Endpoints, epOper)
} else {
log.Infof("Unmatch: ep's netId '%s' with '%s' ", ep.NetID, networkID)
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions netmaster/objApi/objapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package objApi

import (
"encoding/json"
"golang.org/x/net/context"
"log"
"net/http"
"os"
Expand All @@ -26,6 +25,8 @@ import (
"testing"
"time"

"golang.org/x/net/context"

"github.com/contiv/contivmodel"
"github.com/contiv/contivmodel/client"
"github.com/contiv/netplugin/core"
Expand Down Expand Up @@ -765,9 +766,22 @@ func TestNetworkAddDelete(t *testing.T) {
checkCreateNetwork(t, true, "default", "infraNw", "datatest", "vlan", "10.1.1.1/24", "10.1.1.254", 1, "", "")

// Basic IP range network checks
checkCreateNetwork(t, false, "default", "contiv", "data", "vxlan", "10.1.1.10-20/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, false, "default", "contiv", "data", "vxlan", "10.1.1.10-10.1.1.20/24", "10.1.1.254", 1, "", "")
verifyNetworkState(t, "default", "contiv", "data", "vxlan", "10.1.1.10", "10.1.1.254", 24, 1, 1, "", "", 0)
checkDeleteNetwork(t, false, "default", "contiv")
checkCreateNetwork(t, false, "default", "contiv", "data", "vxlan", "10.1.1.10-10.1.5.254/16", "10.1.254.254", 1, "", "")
verifyNetworkState(t, "default", "contiv", "data", "vxlan", "10.1.1.10", "10.1.254.254", 16, 1, 1, "", "", 0)
checkDeleteNetwork(t, false, "default", "contiv")

// Try invalid values for ip addr range
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.10-20/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.10-10.1.20/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.10-10.1.1.1/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.10-10.1.2.30/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.10-10.2.1.30/16", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.10-10.1.1.1.30/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1/24", "10.1.1.254", 1, "", "")
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.256/24", "10.1.1.254", 1, "", "")

// Try network create with invalid network range
checkCreateNetwork(t, true, "default", "contiv", "data", "vxlan", "10.1.1.1-70/26", "10.1.1.63", 1, "", "")
Expand Down
1 change: 1 addition & 0 deletions netplugin/netd.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ func serveRequests(netPlugin *plugin.NetPlugin) {
http.Error(w, "Error fetching stats from driver", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(stats)
})
s.HandleFunc("/inspect/driver", func(w http.ResponseWriter, r *http.Request) {
Expand Down
45 changes: 41 additions & 4 deletions utils/netutils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ValidateNetworkRangeParams(ipRange string, subnetLen uint) error {
firstAddr, _ := ipv4ToUint32(GetSubnetAddr(ipRange, subnetLen))
lastAddr, _ := ipv4ToUint32(getLastAddrInSubnet(ipRange, subnetLen))

if rangeMin < firstAddr || rangeMax > lastAddr {
if rangeMin < firstAddr || rangeMax > lastAddr || rangeMin > rangeMax {
return core.Errorf("Network subnet format not valid")
}

Expand Down Expand Up @@ -95,6 +95,45 @@ func SetBitsOutsideRange(ipAllocMap *bitset.BitSet, ipRange string, subnetLen ui
}
}

// GetIPAddrRange returns IP CIDR as a ip address range
func GetIPAddrRange(ipCIDR string, subnetLen uint) string {
rangeMin, _ := ipv4ToUint32(getFirstAddrInRange(ipCIDR))
rangeMax, _ := ipv4ToUint32(getLastAddrInRange(ipCIDR, subnetLen))
firstAddr, _ := ipv4ToUint32(GetSubnetAddr(ipCIDR, subnetLen))
lastAddr, _ := ipv4ToUint32(getLastAddrInSubnet(ipCIDR, subnetLen))

if rangeMin < firstAddr {
rangeMin = firstAddr
}
if rangeMax > lastAddr {
rangeMax = lastAddr
}

minAddr, _ := ipv4Uint32ToString(rangeMin)
maxAddr, _ := ipv4Uint32ToString(rangeMax)

return minAddr + "-" + maxAddr
}

// ClearBitsOutsideRange sets all IPs outside range as used
func ClearBitsOutsideRange(ipAllocMap *bitset.BitSet, ipRange string, subnetLen uint) {
var i uint32
rangeMin, _ := ipv4ToUint32(getFirstAddrInRange(ipRange))
rangeMax, _ := ipv4ToUint32(getLastAddrInRange(ipRange, subnetLen))
firstAddr, _ := ipv4ToUint32(GetSubnetAddr(ipRange, subnetLen))
lastAddr, _ := ipv4ToUint32(getLastAddrInSubnet(ipRange, subnetLen))

// Set bits lower than rangeMin as used
for i = 0; i < (rangeMin - firstAddr); i++ {
ipAllocMap.Clear(uint(i))
}

// Set bits greater than the rangeMax as used
for i = ((rangeMin - firstAddr) + ((rangeMax - rangeMin) + 1)); i < (lastAddr - firstAddr); i++ {
ipAllocMap.Clear(uint(i))
}
}

// CreateBitset initializes a bit set with 2^numBitsWide bits
func CreateBitset(numBitsWide uint) *bitset.BitSet {
maxSize := 1 << numBitsWide
Expand Down Expand Up @@ -601,9 +640,7 @@ func getLastAddrInRange(ipRange string, subnetLen uint) string {
var lastIP string

if isSubnetIPRange(ipRange) {
subnetRange := strings.Split(ipRange, "-")
commonSubnetPrefix := ipRange[0 : strings.LastIndex(subnetRange[0], ".")+1]
lastIP = commonSubnetPrefix + strings.Split(ipRange, "-")[1]
lastIP = strings.Split(ipRange, "-")[1]
} else {
lastIP = getLastAddrInSubnet(ipRange, subnetLen)
}
Expand Down
8 changes: 4 additions & 4 deletions utils/netutils/netutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func TestGetIPNumber(t *testing.T) {
}

var testValidNetParams = []testSubnetInfo{
{subnetIP: "11.2.1.10-100", subnetLen: 24},
{subnetIP: "11.2.1.10-11.2.1.100", subnetLen: 24},
{subnetIP: "10.123.16.0", subnetLen: 26},
{subnetIP: "10.123.16.66", subnetLen: 26},
{subnetIP: "10.123.16.10-100", subnetLen: 22},
{subnetIP: "10.123.16.10-10.123.16.100", subnetLen: 22},
}

func TestValidNetworkRangeParams(t *testing.T) {
Expand All @@ -99,8 +99,8 @@ func TestValidNetworkRangeParams(t *testing.T) {
}

var testInvalidNetParams = []testSubnetInfo{
{subnetIP: "10.123.16.10-100", subnetLen: 26},
{subnetIP: "10.123.16.60-100", subnetLen: 26},
{subnetIP: "10.123.16.10-10.123.16.100", subnetLen: 26},
{subnetIP: "10.123.16.60-10.123.16.100", subnetLen: 26},
}

func TestInvalidNetworkRangeParams(t *testing.T) {
Expand Down

0 comments on commit e1dbb39

Please sign in to comment.