forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnet_allocator.go
79 lines (67 loc) · 2 KB
/
subnet_allocator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package netutils
import (
"fmt"
"net"
)
type SubnetAllocator struct {
network *net.IPNet
capacity uint
allocMap map[string]bool
}
func NewSubnetAllocator(network string, capacity uint, inUse []string) (*SubnetAllocator, error) {
_, netIP, err := net.ParseCIDR(network)
if err != nil {
return nil, fmt.Errorf("Failed to parse network address: %q", network)
}
netMaskSize, _ := netIP.Mask.Size()
if capacity > (32 - uint(netMaskSize)) {
return nil, fmt.Errorf("Subnet capacity cannot be larger than number of networks available.")
}
amap := make(map[string]bool)
for _, netStr := range inUse {
_, nIp, err := net.ParseCIDR(netStr)
if err != nil {
fmt.Println("Failed to parse network address: ", netStr)
continue
}
if !netIP.Contains(nIp.IP) {
fmt.Println("Provided subnet doesn't belong to network: ", nIp)
continue
}
amap[nIp.String()] = true
}
return &SubnetAllocator{network: netIP, capacity: capacity, allocMap: amap}, nil
}
func (sna *SubnetAllocator) GetNetwork() (*net.IPNet, error) {
var (
numSubnets uint32
numSubnetBits uint
)
baseipu := IPToUint32(sna.network.IP)
netMaskSize, _ := sna.network.Mask.Size()
numSubnetBits = 32 - uint(netMaskSize) - sna.capacity
numSubnets = 1 << numSubnetBits
var i uint32
for i = 0; i < numSubnets; i++ {
shifted := i << sna.capacity
ipu := baseipu | shifted
genIp := Uint32ToIP(ipu)
genSubnet := &net.IPNet{IP: genIp, Mask: net.CIDRMask(int(numSubnetBits)+netMaskSize, 32)}
if !sna.allocMap[genSubnet.String()] {
sna.allocMap[genSubnet.String()] = true
return genSubnet, nil
}
}
return nil, fmt.Errorf("No subnets available.")
}
func (sna *SubnetAllocator) ReleaseNetwork(ipnet *net.IPNet) error {
if !sna.network.Contains(ipnet.IP) {
return fmt.Errorf("Provided subnet %v doesn't belong to the network %v.", ipnet, sna.network)
}
ipnetStr := ipnet.String()
if !sna.allocMap[ipnetStr] {
return fmt.Errorf("Provided subnet %v is already available.", ipnet)
}
sna.allocMap[ipnetStr] = false
return nil
}