Fixes #11624.
Signed-off-by: David Calavera <david.calavera@gmail.com>
| ... | ... |
@@ -80,6 +80,7 @@ var ( |
| 80 | 80 |
|
| 81 | 81 |
defaultBindingIP = net.ParseIP("0.0.0.0")
|
| 82 | 82 |
currentInterfaces = ifaces{c: make(map[string]*networkInterface)}
|
| 83 |
+ ipAllocator = ipallocator.New() |
|
| 83 | 84 |
) |
| 84 | 85 |
|
| 85 | 86 |
func InitDriver(job *engine.Job) engine.Status {
|
| ... | ... |
@@ -244,7 +245,7 @@ func InitDriver(job *engine.Job) engine.Status {
|
| 244 | 244 |
return job.Error(err) |
| 245 | 245 |
} |
| 246 | 246 |
log.Debugf("Subnet: %v", subnet)
|
| 247 |
- if err := ipallocator.RegisterSubnet(bridgeIPv4Network, subnet); err != nil {
|
|
| 247 |
+ if err := ipAllocator.RegisterSubnet(bridgeIPv4Network, subnet); err != nil {
|
|
| 248 | 248 |
return job.Error(err) |
| 249 | 249 |
} |
| 250 | 250 |
} |
| ... | ... |
@@ -255,14 +256,14 @@ func InitDriver(job *engine.Job) engine.Status {
|
| 255 | 255 |
return job.Error(err) |
| 256 | 256 |
} |
| 257 | 257 |
log.Debugf("Subnet: %v", subnet)
|
| 258 |
- if err := ipallocator.RegisterSubnet(subnet, subnet); err != nil {
|
|
| 258 |
+ if err := ipAllocator.RegisterSubnet(subnet, subnet); err != nil {
|
|
| 259 | 259 |
return job.Error(err) |
| 260 | 260 |
} |
| 261 | 261 |
globalIPv6Network = subnet |
| 262 | 262 |
} |
| 263 | 263 |
|
| 264 | 264 |
// Block BridgeIP in IP allocator |
| 265 |
- ipallocator.RequestIP(bridgeIPv4Network, bridgeIPv4Network.IP) |
|
| 265 |
+ ipAllocator.RequestIP(bridgeIPv4Network, bridgeIPv4Network.IP) |
|
| 266 | 266 |
|
| 267 | 267 |
// https://github.com/docker/docker/issues/2768 |
| 268 | 268 |
job.Eng.Hack_SetGlobalVar("httpapi.bridgeIP", bridgeIPv4Network.IP)
|
| ... | ... |
@@ -509,7 +510,7 @@ func Allocate(job *engine.Job) engine.Status {
|
| 509 | 509 |
globalIPv6 net.IP |
| 510 | 510 |
) |
| 511 | 511 |
|
| 512 |
- ip, err = ipallocator.RequestIP(bridgeIPv4Network, requestedIP) |
|
| 512 |
+ ip, err = ipAllocator.RequestIP(bridgeIPv4Network, requestedIP) |
|
| 513 | 513 |
if err != nil {
|
| 514 | 514 |
return job.Error(err) |
| 515 | 515 |
} |
| ... | ... |
@@ -530,7 +531,7 @@ func Allocate(job *engine.Job) engine.Status {
|
| 530 | 530 |
} |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 |
- globalIPv6, err = ipallocator.RequestIP(globalIPv6Network, requestedIPv6) |
|
| 533 |
+ globalIPv6, err = ipAllocator.RequestIP(globalIPv6Network, requestedIPv6) |
|
| 534 | 534 |
if err != nil {
|
| 535 | 535 |
log.Errorf("Allocator: RequestIP v6: %v", err)
|
| 536 | 536 |
return job.Error(err) |
| ... | ... |
@@ -591,11 +592,11 @@ func Release(job *engine.Job) engine.Status {
|
| 591 | 591 |
} |
| 592 | 592 |
} |
| 593 | 593 |
|
| 594 |
- if err := ipallocator.ReleaseIP(bridgeIPv4Network, containerInterface.IP); err != nil {
|
|
| 594 |
+ if err := ipAllocator.ReleaseIP(bridgeIPv4Network, containerInterface.IP); err != nil {
|
|
| 595 | 595 |
log.Infof("Unable to release IPv4 %s", err)
|
| 596 | 596 |
} |
| 597 | 597 |
if globalIPv6Network != nil {
|
| 598 |
- if err := ipallocator.ReleaseIP(globalIPv6Network, containerInterface.IPv6); err != nil {
|
|
| 598 |
+ if err := ipAllocator.ReleaseIP(globalIPv6Network, containerInterface.IPv6); err != nil {
|
|
| 599 | 599 |
log.Infof("Unable to release IPv6 %s", err)
|
| 600 | 600 |
} |
| 601 | 601 |
} |
| ... | ... |
@@ -41,19 +41,24 @@ var ( |
| 41 | 41 |
ErrBadSubnet = errors.New("network does not contain specified subnet")
|
| 42 | 42 |
) |
| 43 | 43 |
|
| 44 |
-var ( |
|
| 45 |
- lock = sync.Mutex{}
|
|
| 46 |
- allocatedIPs = networkSet{}
|
|
| 47 |
-) |
|
| 44 |
+type IPAllocator struct {
|
|
| 45 |
+ allocatedIPs networkSet |
|
| 46 |
+ mutex sync.Mutex |
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+func New() *IPAllocator {
|
|
| 50 |
+ return &IPAllocator{networkSet{}, sync.Mutex{}}
|
|
| 51 |
+} |
|
| 48 | 52 |
|
| 49 | 53 |
// RegisterSubnet registers network in global allocator with bounds |
| 50 | 54 |
// defined by subnet. If you want to use network range you must call |
| 51 | 55 |
// this method before first RequestIP, otherwise full network range will be used |
| 52 |
-func RegisterSubnet(network *net.IPNet, subnet *net.IPNet) error {
|
|
| 53 |
- lock.Lock() |
|
| 54 |
- defer lock.Unlock() |
|
| 56 |
+func (a *IPAllocator) RegisterSubnet(network *net.IPNet, subnet *net.IPNet) error {
|
|
| 57 |
+ a.mutex.Lock() |
|
| 58 |
+ defer a.mutex.Unlock() |
|
| 59 |
+ |
|
| 55 | 60 |
key := network.String() |
| 56 |
- if _, ok := allocatedIPs[key]; ok {
|
|
| 61 |
+ if _, ok := a.allocatedIPs[key]; ok {
|
|
| 57 | 62 |
return ErrNetworkAlreadyRegistered |
| 58 | 63 |
} |
| 59 | 64 |
n := newAllocatedMap(network) |
| ... | ... |
@@ -68,7 +73,7 @@ func RegisterSubnet(network *net.IPNet, subnet *net.IPNet) error {
|
| 68 | 68 |
n.begin.Set(begin) |
| 69 | 69 |
n.end.Set(end) |
| 70 | 70 |
n.last.Sub(begin, big.NewInt(1)) |
| 71 |
- allocatedIPs[key] = n |
|
| 71 |
+ a.allocatedIPs[key] = n |
|
| 72 | 72 |
return nil |
| 73 | 73 |
} |
| 74 | 74 |
|
| ... | ... |
@@ -76,14 +81,15 @@ func RegisterSubnet(network *net.IPNet, subnet *net.IPNet) error {
|
| 76 | 76 |
// will return the next available ip if the ip provided is nil. If the |
| 77 | 77 |
// ip provided is not nil it will validate that the provided ip is available |
| 78 | 78 |
// for use or return an error |
| 79 |
-func RequestIP(network *net.IPNet, ip net.IP) (net.IP, error) {
|
|
| 80 |
- lock.Lock() |
|
| 81 |
- defer lock.Unlock() |
|
| 79 |
+func (a *IPAllocator) RequestIP(network *net.IPNet, ip net.IP) (net.IP, error) {
|
|
| 80 |
+ a.mutex.Lock() |
|
| 81 |
+ defer a.mutex.Unlock() |
|
| 82 |
+ |
|
| 82 | 83 |
key := network.String() |
| 83 |
- allocated, ok := allocatedIPs[key] |
|
| 84 |
+ allocated, ok := a.allocatedIPs[key] |
|
| 84 | 85 |
if !ok {
|
| 85 | 86 |
allocated = newAllocatedMap(network) |
| 86 |
- allocatedIPs[key] = allocated |
|
| 87 |
+ a.allocatedIPs[key] = allocated |
|
| 87 | 88 |
} |
| 88 | 89 |
|
| 89 | 90 |
if ip == nil {
|
| ... | ... |
@@ -94,10 +100,11 @@ func RequestIP(network *net.IPNet, ip net.IP) (net.IP, error) {
|
| 94 | 94 |
|
| 95 | 95 |
// ReleaseIP adds the provided ip back into the pool of |
| 96 | 96 |
// available ips to be returned for use. |
| 97 |
-func ReleaseIP(network *net.IPNet, ip net.IP) error {
|
|
| 98 |
- lock.Lock() |
|
| 99 |
- defer lock.Unlock() |
|
| 100 |
- if allocated, exists := allocatedIPs[network.String()]; exists {
|
|
| 97 |
+func (a *IPAllocator) ReleaseIP(network *net.IPNet, ip net.IP) error {
|
|
| 98 |
+ a.mutex.Lock() |
|
| 99 |
+ defer a.mutex.Unlock() |
|
| 100 |
+ |
|
| 101 |
+ if allocated, exists := a.allocatedIPs[network.String()]; exists {
|
|
| 101 | 102 |
delete(allocated.p, ip.String()) |
| 102 | 103 |
} |
| 103 | 104 |
return nil |
| ... | ... |
@@ -7,10 +7,6 @@ import ( |
| 7 | 7 |
"testing" |
| 8 | 8 |
) |
| 9 | 9 |
|
| 10 |
-func reset() {
|
|
| 11 |
- allocatedIPs = networkSet{}
|
|
| 12 |
-} |
|
| 13 |
- |
|
| 14 | 10 |
func TestConversion(t *testing.T) {
|
| 15 | 11 |
ip := net.ParseIP("127.0.0.1")
|
| 16 | 12 |
i := ipToBigInt(ip) |
| ... | ... |
@@ -52,7 +48,8 @@ func TestConversionIPv6(t *testing.T) {
|
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
func TestRequestNewIps(t *testing.T) {
|
| 55 |
- defer reset() |
|
| 55 |
+ a := New() |
|
| 56 |
+ |
|
| 56 | 57 |
network := &net.IPNet{
|
| 57 | 58 |
IP: []byte{192, 168, 0, 1},
|
| 58 | 59 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -62,7 +59,7 @@ func TestRequestNewIps(t *testing.T) {
|
| 62 | 62 |
var err error |
| 63 | 63 |
|
| 64 | 64 |
for i := 1; i < 10; i++ {
|
| 65 |
- ip, err = RequestIP(network, nil) |
|
| 65 |
+ ip, err = a.RequestIP(network, nil) |
|
| 66 | 66 |
if err != nil {
|
| 67 | 67 |
t.Fatal(err) |
| 68 | 68 |
} |
| ... | ... |
@@ -72,10 +69,10 @@ func TestRequestNewIps(t *testing.T) {
|
| 72 | 72 |
} |
| 73 | 73 |
} |
| 74 | 74 |
value := bigIntToIP(big.NewInt(0).Add(ipToBigInt(ip), big.NewInt(1))).String() |
| 75 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 75 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 76 | 76 |
t.Fatal(err) |
| 77 | 77 |
} |
| 78 |
- ip, err = RequestIP(network, nil) |
|
| 78 |
+ ip, err = a.RequestIP(network, nil) |
|
| 79 | 79 |
if err != nil {
|
| 80 | 80 |
t.Fatal(err) |
| 81 | 81 |
} |
| ... | ... |
@@ -85,7 +82,8 @@ func TestRequestNewIps(t *testing.T) {
|
| 85 | 85 |
} |
| 86 | 86 |
|
| 87 | 87 |
func TestRequestNewIpV6(t *testing.T) {
|
| 88 |
- defer reset() |
|
| 88 |
+ a := New() |
|
| 89 |
+ |
|
| 89 | 90 |
network := &net.IPNet{
|
| 90 | 91 |
IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
| 91 | 92 |
Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
|
| ... | ... |
@@ -94,7 +92,7 @@ func TestRequestNewIpV6(t *testing.T) {
|
| 94 | 94 |
var ip net.IP |
| 95 | 95 |
var err error |
| 96 | 96 |
for i := 1; i < 10; i++ {
|
| 97 |
- ip, err = RequestIP(network, nil) |
|
| 97 |
+ ip, err = a.RequestIP(network, nil) |
|
| 98 | 98 |
if err != nil {
|
| 99 | 99 |
t.Fatal(err) |
| 100 | 100 |
} |
| ... | ... |
@@ -104,10 +102,10 @@ func TestRequestNewIpV6(t *testing.T) {
|
| 104 | 104 |
} |
| 105 | 105 |
} |
| 106 | 106 |
value := bigIntToIP(big.NewInt(0).Add(ipToBigInt(ip), big.NewInt(1))).String() |
| 107 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 107 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 108 | 108 |
t.Fatal(err) |
| 109 | 109 |
} |
| 110 |
- ip, err = RequestIP(network, nil) |
|
| 110 |
+ ip, err = a.RequestIP(network, nil) |
|
| 111 | 111 |
if err != nil {
|
| 112 | 112 |
t.Fatal(err) |
| 113 | 113 |
} |
| ... | ... |
@@ -117,68 +115,70 @@ func TestRequestNewIpV6(t *testing.T) {
|
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
func TestReleaseIp(t *testing.T) {
|
| 120 |
- defer reset() |
|
| 120 |
+ a := New() |
|
| 121 |
+ |
|
| 121 | 122 |
network := &net.IPNet{
|
| 122 | 123 |
IP: []byte{192, 168, 0, 1},
|
| 123 | 124 |
Mask: []byte{255, 255, 255, 0},
|
| 124 | 125 |
} |
| 125 | 126 |
|
| 126 |
- ip, err := RequestIP(network, nil) |
|
| 127 |
+ ip, err := a.RequestIP(network, nil) |
|
| 127 | 128 |
if err != nil {
|
| 128 | 129 |
t.Fatal(err) |
| 129 | 130 |
} |
| 130 | 131 |
|
| 131 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 132 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 132 | 133 |
t.Fatal(err) |
| 133 | 134 |
} |
| 134 | 135 |
} |
| 135 | 136 |
|
| 136 | 137 |
func TestReleaseIpV6(t *testing.T) {
|
| 137 |
- defer reset() |
|
| 138 |
+ a := New() |
|
| 139 |
+ |
|
| 138 | 140 |
network := &net.IPNet{
|
| 139 | 141 |
IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
| 140 | 142 |
Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
|
| 141 | 143 |
} |
| 142 | 144 |
|
| 143 |
- ip, err := RequestIP(network, nil) |
|
| 145 |
+ ip, err := a.RequestIP(network, nil) |
|
| 144 | 146 |
if err != nil {
|
| 145 | 147 |
t.Fatal(err) |
| 146 | 148 |
} |
| 147 | 149 |
|
| 148 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 150 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 149 | 151 |
t.Fatal(err) |
| 150 | 152 |
} |
| 151 | 153 |
} |
| 152 | 154 |
|
| 153 | 155 |
func TestGetReleasedIp(t *testing.T) {
|
| 154 |
- defer reset() |
|
| 156 |
+ a := New() |
|
| 155 | 157 |
network := &net.IPNet{
|
| 156 | 158 |
IP: []byte{192, 168, 0, 1},
|
| 157 | 159 |
Mask: []byte{255, 255, 255, 0},
|
| 158 | 160 |
} |
| 159 | 161 |
|
| 160 |
- ip, err := RequestIP(network, nil) |
|
| 162 |
+ ip, err := a.RequestIP(network, nil) |
|
| 161 | 163 |
if err != nil {
|
| 162 | 164 |
t.Fatal(err) |
| 163 | 165 |
} |
| 164 | 166 |
|
| 165 | 167 |
value := ip.String() |
| 166 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 168 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 167 | 169 |
t.Fatal(err) |
| 168 | 170 |
} |
| 169 | 171 |
|
| 170 | 172 |
for i := 0; i < 253; i++ {
|
| 171 |
- _, err = RequestIP(network, nil) |
|
| 173 |
+ _, err = a.RequestIP(network, nil) |
|
| 172 | 174 |
if err != nil {
|
| 173 | 175 |
t.Fatal(err) |
| 174 | 176 |
} |
| 175 |
- err = ReleaseIP(network, ip) |
|
| 177 |
+ err = a.ReleaseIP(network, ip) |
|
| 176 | 178 |
if err != nil {
|
| 177 | 179 |
t.Fatal(err) |
| 178 | 180 |
} |
| 179 | 181 |
} |
| 180 | 182 |
|
| 181 |
- ip, err = RequestIP(network, nil) |
|
| 183 |
+ ip, err = a.RequestIP(network, nil) |
|
| 182 | 184 |
if err != nil {
|
| 183 | 185 |
t.Fatal(err) |
| 184 | 186 |
} |
| ... | ... |
@@ -189,34 +189,35 @@ func TestGetReleasedIp(t *testing.T) {
|
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
func TestGetReleasedIpV6(t *testing.T) {
|
| 192 |
- defer reset() |
|
| 192 |
+ a := New() |
|
| 193 |
+ |
|
| 193 | 194 |
network := &net.IPNet{
|
| 194 | 195 |
IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
| 195 | 196 |
Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0},
|
| 196 | 197 |
} |
| 197 | 198 |
|
| 198 |
- ip, err := RequestIP(network, nil) |
|
| 199 |
+ ip, err := a.RequestIP(network, nil) |
|
| 199 | 200 |
if err != nil {
|
| 200 | 201 |
t.Fatal(err) |
| 201 | 202 |
} |
| 202 | 203 |
|
| 203 | 204 |
value := ip.String() |
| 204 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 205 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 205 | 206 |
t.Fatal(err) |
| 206 | 207 |
} |
| 207 | 208 |
|
| 208 | 209 |
for i := 0; i < 253; i++ {
|
| 209 |
- _, err = RequestIP(network, nil) |
|
| 210 |
+ _, err = a.RequestIP(network, nil) |
|
| 210 | 211 |
if err != nil {
|
| 211 | 212 |
t.Fatal(err) |
| 212 | 213 |
} |
| 213 |
- err = ReleaseIP(network, ip) |
|
| 214 |
+ err = a.ReleaseIP(network, ip) |
|
| 214 | 215 |
if err != nil {
|
| 215 | 216 |
t.Fatal(err) |
| 216 | 217 |
} |
| 217 | 218 |
} |
| 218 | 219 |
|
| 219 |
- ip, err = RequestIP(network, nil) |
|
| 220 |
+ ip, err = a.RequestIP(network, nil) |
|
| 220 | 221 |
if err != nil {
|
| 221 | 222 |
t.Fatal(err) |
| 222 | 223 |
} |
| ... | ... |
@@ -227,7 +228,8 @@ func TestGetReleasedIpV6(t *testing.T) {
|
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 | 229 |
func TestRequestSpecificIp(t *testing.T) {
|
| 230 |
- defer reset() |
|
| 230 |
+ a := New() |
|
| 231 |
+ |
|
| 231 | 232 |
network := &net.IPNet{
|
| 232 | 233 |
IP: []byte{192, 168, 0, 1},
|
| 233 | 234 |
Mask: []byte{255, 255, 255, 224},
|
| ... | ... |
@@ -236,23 +238,24 @@ func TestRequestSpecificIp(t *testing.T) {
|
| 236 | 236 |
ip := net.ParseIP("192.168.0.5")
|
| 237 | 237 |
|
| 238 | 238 |
// Request a "good" IP. |
| 239 |
- if _, err := RequestIP(network, ip); err != nil {
|
|
| 239 |
+ if _, err := a.RequestIP(network, ip); err != nil {
|
|
| 240 | 240 |
t.Fatal(err) |
| 241 | 241 |
} |
| 242 | 242 |
|
| 243 | 243 |
// Request the same IP again. |
| 244 |
- if _, err := RequestIP(network, ip); err != ErrIPAlreadyAllocated {
|
|
| 244 |
+ if _, err := a.RequestIP(network, ip); err != ErrIPAlreadyAllocated {
|
|
| 245 | 245 |
t.Fatalf("Got the same IP twice: %#v", err)
|
| 246 | 246 |
} |
| 247 | 247 |
|
| 248 | 248 |
// Request an out of range IP. |
| 249 |
- if _, err := RequestIP(network, net.ParseIP("192.168.0.42")); err != ErrIPOutOfRange {
|
|
| 249 |
+ if _, err := a.RequestIP(network, net.ParseIP("192.168.0.42")); err != ErrIPOutOfRange {
|
|
| 250 | 250 |
t.Fatalf("Got an out of range IP: %#v", err)
|
| 251 | 251 |
} |
| 252 | 252 |
} |
| 253 | 253 |
|
| 254 | 254 |
func TestRequestSpecificIpV6(t *testing.T) {
|
| 255 |
- defer reset() |
|
| 255 |
+ a := New() |
|
| 256 |
+ |
|
| 256 | 257 |
network := &net.IPNet{
|
| 257 | 258 |
IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
| 258 | 259 |
Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
|
| ... | ... |
@@ -261,22 +264,24 @@ func TestRequestSpecificIpV6(t *testing.T) {
|
| 261 | 261 |
ip := net.ParseIP("2a00:1450::5")
|
| 262 | 262 |
|
| 263 | 263 |
// Request a "good" IP. |
| 264 |
- if _, err := RequestIP(network, ip); err != nil {
|
|
| 264 |
+ if _, err := a.RequestIP(network, ip); err != nil {
|
|
| 265 | 265 |
t.Fatal(err) |
| 266 | 266 |
} |
| 267 | 267 |
|
| 268 | 268 |
// Request the same IP again. |
| 269 |
- if _, err := RequestIP(network, ip); err != ErrIPAlreadyAllocated {
|
|
| 269 |
+ if _, err := a.RequestIP(network, ip); err != ErrIPAlreadyAllocated {
|
|
| 270 | 270 |
t.Fatalf("Got the same IP twice: %#v", err)
|
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
// Request an out of range IP. |
| 274 |
- if _, err := RequestIP(network, net.ParseIP("2a00:1500::1")); err != ErrIPOutOfRange {
|
|
| 274 |
+ if _, err := a.RequestIP(network, net.ParseIP("2a00:1500::1")); err != ErrIPOutOfRange {
|
|
| 275 | 275 |
t.Fatalf("Got an out of range IP: %#v", err)
|
| 276 | 276 |
} |
| 277 | 277 |
} |
| 278 | 278 |
|
| 279 | 279 |
func TestIPAllocator(t *testing.T) {
|
| 280 |
+ a := New() |
|
| 281 |
+ |
|
| 280 | 282 |
expectedIPs := []net.IP{
|
| 281 | 283 |
0: net.IPv4(127, 0, 0, 1), |
| 282 | 284 |
1: net.IPv4(127, 0, 0, 2), |
| ... | ... |
@@ -296,7 +301,7 @@ func TestIPAllocator(t *testing.T) {
|
| 296 | 296 |
// Check that we get 6 IPs, from 127.0.0.1–127.0.0.6, in that |
| 297 | 297 |
// order. |
| 298 | 298 |
for i := 0; i < 6; i++ {
|
| 299 |
- ip, err := RequestIP(network, nil) |
|
| 299 |
+ ip, err := a.RequestIP(network, nil) |
|
| 300 | 300 |
if err != nil {
|
| 301 | 301 |
t.Fatal(err) |
| 302 | 302 |
} |
| ... | ... |
@@ -332,25 +337,25 @@ func TestIPAllocator(t *testing.T) {
|
| 332 | 332 |
// ↑ |
| 333 | 333 |
|
| 334 | 334 |
// Check that there are no more IPs |
| 335 |
- ip, err := RequestIP(network, nil) |
|
| 335 |
+ ip, err := a.RequestIP(network, nil) |
|
| 336 | 336 |
if err == nil {
|
| 337 | 337 |
t.Fatalf("There shouldn't be any IP addresses at this point, got %s\n", ip)
|
| 338 | 338 |
} |
| 339 | 339 |
|
| 340 | 340 |
// Release some IPs in non-sequential order |
| 341 |
- if err := ReleaseIP(network, expectedIPs[3]); err != nil {
|
|
| 341 |
+ if err := a.ReleaseIP(network, expectedIPs[3]); err != nil {
|
|
| 342 | 342 |
t.Fatal(err) |
| 343 | 343 |
} |
| 344 | 344 |
// 1(u) - 2(u) - 3(u) - 4(f) - 5(u) - 6(u) |
| 345 | 345 |
// ↑ |
| 346 | 346 |
|
| 347 |
- if err := ReleaseIP(network, expectedIPs[2]); err != nil {
|
|
| 347 |
+ if err := a.ReleaseIP(network, expectedIPs[2]); err != nil {
|
|
| 348 | 348 |
t.Fatal(err) |
| 349 | 349 |
} |
| 350 | 350 |
// 1(u) - 2(u) - 3(f) - 4(f) - 5(u) - 6(u) |
| 351 | 351 |
// ↑ |
| 352 | 352 |
|
| 353 |
- if err := ReleaseIP(network, expectedIPs[4]); err != nil {
|
|
| 353 |
+ if err := a.ReleaseIP(network, expectedIPs[4]); err != nil {
|
|
| 354 | 354 |
t.Fatal(err) |
| 355 | 355 |
} |
| 356 | 356 |
// 1(u) - 2(u) - 3(f) - 4(f) - 5(f) - 6(u) |
| ... | ... |
@@ -360,7 +365,7 @@ func TestIPAllocator(t *testing.T) {
|
| 360 | 360 |
// with the first released IP |
| 361 | 361 |
newIPs := make([]net.IP, 3) |
| 362 | 362 |
for i := 0; i < 3; i++ {
|
| 363 |
- ip, err := RequestIP(network, nil) |
|
| 363 |
+ ip, err := a.RequestIP(network, nil) |
|
| 364 | 364 |
if err != nil {
|
| 365 | 365 |
t.Fatal(err) |
| 366 | 366 |
} |
| ... | ... |
@@ -371,14 +376,15 @@ func TestIPAllocator(t *testing.T) {
|
| 371 | 371 |
assertIPEquals(t, expectedIPs[3], newIPs[1]) |
| 372 | 372 |
assertIPEquals(t, expectedIPs[4], newIPs[2]) |
| 373 | 373 |
|
| 374 |
- _, err = RequestIP(network, nil) |
|
| 374 |
+ _, err = a.RequestIP(network, nil) |
|
| 375 | 375 |
if err == nil {
|
| 376 | 376 |
t.Fatal("There shouldn't be any IP addresses at this point")
|
| 377 | 377 |
} |
| 378 | 378 |
} |
| 379 | 379 |
|
| 380 | 380 |
func TestAllocateFirstIP(t *testing.T) {
|
| 381 |
- defer reset() |
|
| 381 |
+ a := New() |
|
| 382 |
+ |
|
| 382 | 383 |
network := &net.IPNet{
|
| 383 | 384 |
IP: []byte{192, 168, 0, 0},
|
| 384 | 385 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -387,7 +393,7 @@ func TestAllocateFirstIP(t *testing.T) {
|
| 387 | 387 |
firstIP := network.IP.To4().Mask(network.Mask) |
| 388 | 388 |
first := big.NewInt(0).Add(ipToBigInt(firstIP), big.NewInt(1)) |
| 389 | 389 |
|
| 390 |
- ip, err := RequestIP(network, nil) |
|
| 390 |
+ ip, err := a.RequestIP(network, nil) |
|
| 391 | 391 |
if err != nil {
|
| 392 | 392 |
t.Fatal(err) |
| 393 | 393 |
} |
| ... | ... |
@@ -399,7 +405,8 @@ func TestAllocateFirstIP(t *testing.T) {
|
| 399 | 399 |
} |
| 400 | 400 |
|
| 401 | 401 |
func TestAllocateAllIps(t *testing.T) {
|
| 402 |
- defer reset() |
|
| 402 |
+ a := New() |
|
| 403 |
+ |
|
| 403 | 404 |
network := &net.IPNet{
|
| 404 | 405 |
IP: []byte{192, 168, 0, 1},
|
| 405 | 406 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -412,7 +419,7 @@ func TestAllocateAllIps(t *testing.T) {
|
| 412 | 412 |
) |
| 413 | 413 |
|
| 414 | 414 |
for err == nil {
|
| 415 |
- current, err = RequestIP(network, nil) |
|
| 415 |
+ current, err = a.RequestIP(network, nil) |
|
| 416 | 416 |
if isFirst {
|
| 417 | 417 |
first = current |
| 418 | 418 |
isFirst = false |
| ... | ... |
@@ -423,15 +430,15 @@ func TestAllocateAllIps(t *testing.T) {
|
| 423 | 423 |
t.Fatal(err) |
| 424 | 424 |
} |
| 425 | 425 |
|
| 426 |
- if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs {
|
|
| 426 |
+ if _, err := a.RequestIP(network, nil); err != ErrNoAvailableIPs {
|
|
| 427 | 427 |
t.Fatal(err) |
| 428 | 428 |
} |
| 429 | 429 |
|
| 430 |
- if err := ReleaseIP(network, first); err != nil {
|
|
| 430 |
+ if err := a.ReleaseIP(network, first); err != nil {
|
|
| 431 | 431 |
t.Fatal(err) |
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 |
- again, err := RequestIP(network, nil) |
|
| 434 |
+ again, err := a.RequestIP(network, nil) |
|
| 435 | 435 |
if err != nil {
|
| 436 | 436 |
t.Fatal(err) |
| 437 | 437 |
} |
| ... | ... |
@@ -439,17 +446,17 @@ func TestAllocateAllIps(t *testing.T) {
|
| 439 | 439 |
assertIPEquals(t, first, again) |
| 440 | 440 |
|
| 441 | 441 |
// ensure that alloc.last == alloc.begin won't result in dead loop |
| 442 |
- if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs {
|
|
| 442 |
+ if _, err := a.RequestIP(network, nil); err != ErrNoAvailableIPs {
|
|
| 443 | 443 |
t.Fatal(err) |
| 444 | 444 |
} |
| 445 | 445 |
|
| 446 | 446 |
// Test by making alloc.last the only free ip and ensure we get it back |
| 447 | 447 |
// #1. first of the range, (alloc.last == ipToInt(first) already) |
| 448 |
- if err := ReleaseIP(network, first); err != nil {
|
|
| 448 |
+ if err := a.ReleaseIP(network, first); err != nil {
|
|
| 449 | 449 |
t.Fatal(err) |
| 450 | 450 |
} |
| 451 | 451 |
|
| 452 |
- ret, err := RequestIP(network, nil) |
|
| 452 |
+ ret, err := a.RequestIP(network, nil) |
|
| 453 | 453 |
if err != nil {
|
| 454 | 454 |
t.Fatal(err) |
| 455 | 455 |
} |
| ... | ... |
@@ -458,9 +465,9 @@ func TestAllocateAllIps(t *testing.T) {
|
| 458 | 458 |
|
| 459 | 459 |
// #2. last of the range, note that current is the last one |
| 460 | 460 |
last := net.IPv4(192, 168, 0, 254) |
| 461 |
- setLastTo(t, network, last) |
|
| 461 |
+ setLastTo(t, a, network, last) |
|
| 462 | 462 |
|
| 463 |
- ret, err = RequestIP(network, nil) |
|
| 463 |
+ ret, err = a.RequestIP(network, nil) |
|
| 464 | 464 |
if err != nil {
|
| 465 | 465 |
t.Fatal(err) |
| 466 | 466 |
} |
| ... | ... |
@@ -469,9 +476,9 @@ func TestAllocateAllIps(t *testing.T) {
|
| 469 | 469 |
|
| 470 | 470 |
// #3. middle of the range |
| 471 | 471 |
mid := net.IPv4(192, 168, 0, 7) |
| 472 |
- setLastTo(t, network, mid) |
|
| 472 |
+ setLastTo(t, a, network, mid) |
|
| 473 | 473 |
|
| 474 |
- ret, err = RequestIP(network, nil) |
|
| 474 |
+ ret, err = a.RequestIP(network, nil) |
|
| 475 | 475 |
if err != nil {
|
| 476 | 476 |
t.Fatal(err) |
| 477 | 477 |
} |
| ... | ... |
@@ -481,25 +488,25 @@ func TestAllocateAllIps(t *testing.T) {
|
| 481 | 481 |
|
| 482 | 482 |
// make sure the pool is full when calling setLastTo. |
| 483 | 483 |
// we don't cheat here |
| 484 |
-func setLastTo(t *testing.T, network *net.IPNet, ip net.IP) {
|
|
| 485 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 484 |
+func setLastTo(t *testing.T, a *IPAllocator, network *net.IPNet, ip net.IP) {
|
|
| 485 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 486 | 486 |
t.Fatal(err) |
| 487 | 487 |
} |
| 488 | 488 |
|
| 489 |
- ret, err := RequestIP(network, nil) |
|
| 489 |
+ ret, err := a.RequestIP(network, nil) |
|
| 490 | 490 |
if err != nil {
|
| 491 | 491 |
t.Fatal(err) |
| 492 | 492 |
} |
| 493 | 493 |
|
| 494 | 494 |
assertIPEquals(t, ip, ret) |
| 495 | 495 |
|
| 496 |
- if err := ReleaseIP(network, ip); err != nil {
|
|
| 496 |
+ if err := a.ReleaseIP(network, ip); err != nil {
|
|
| 497 | 497 |
t.Fatal(err) |
| 498 | 498 |
} |
| 499 | 499 |
} |
| 500 | 500 |
|
| 501 | 501 |
func TestAllocateDifferentSubnets(t *testing.T) {
|
| 502 |
- defer reset() |
|
| 502 |
+ a := New() |
|
| 503 | 503 |
network1 := &net.IPNet{
|
| 504 | 504 |
IP: []byte{192, 168, 0, 1},
|
| 505 | 505 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -528,39 +535,39 @@ func TestAllocateDifferentSubnets(t *testing.T) {
|
| 528 | 528 |
8: net.ParseIP("2a00:1632::2"),
|
| 529 | 529 |
} |
| 530 | 530 |
|
| 531 |
- ip11, err := RequestIP(network1, nil) |
|
| 531 |
+ ip11, err := a.RequestIP(network1, nil) |
|
| 532 | 532 |
if err != nil {
|
| 533 | 533 |
t.Fatal(err) |
| 534 | 534 |
} |
| 535 |
- ip12, err := RequestIP(network1, nil) |
|
| 535 |
+ ip12, err := a.RequestIP(network1, nil) |
|
| 536 | 536 |
if err != nil {
|
| 537 | 537 |
t.Fatal(err) |
| 538 | 538 |
} |
| 539 |
- ip21, err := RequestIP(network2, nil) |
|
| 539 |
+ ip21, err := a.RequestIP(network2, nil) |
|
| 540 | 540 |
if err != nil {
|
| 541 | 541 |
t.Fatal(err) |
| 542 | 542 |
} |
| 543 |
- ip22, err := RequestIP(network2, nil) |
|
| 543 |
+ ip22, err := a.RequestIP(network2, nil) |
|
| 544 | 544 |
if err != nil {
|
| 545 | 545 |
t.Fatal(err) |
| 546 | 546 |
} |
| 547 |
- ip31, err := RequestIP(network3, nil) |
|
| 547 |
+ ip31, err := a.RequestIP(network3, nil) |
|
| 548 | 548 |
if err != nil {
|
| 549 | 549 |
t.Fatal(err) |
| 550 | 550 |
} |
| 551 |
- ip32, err := RequestIP(network3, nil) |
|
| 551 |
+ ip32, err := a.RequestIP(network3, nil) |
|
| 552 | 552 |
if err != nil {
|
| 553 | 553 |
t.Fatal(err) |
| 554 | 554 |
} |
| 555 |
- ip33, err := RequestIP(network3, nil) |
|
| 555 |
+ ip33, err := a.RequestIP(network3, nil) |
|
| 556 | 556 |
if err != nil {
|
| 557 | 557 |
t.Fatal(err) |
| 558 | 558 |
} |
| 559 |
- ip41, err := RequestIP(network4, nil) |
|
| 559 |
+ ip41, err := a.RequestIP(network4, nil) |
|
| 560 | 560 |
if err != nil {
|
| 561 | 561 |
t.Fatal(err) |
| 562 | 562 |
} |
| 563 |
- ip42, err := RequestIP(network4, nil) |
|
| 563 |
+ ip42, err := a.RequestIP(network4, nil) |
|
| 564 | 564 |
if err != nil {
|
| 565 | 565 |
t.Fatal(err) |
| 566 | 566 |
} |
| ... | ... |
@@ -576,7 +583,7 @@ func TestAllocateDifferentSubnets(t *testing.T) {
|
| 576 | 576 |
} |
| 577 | 577 |
|
| 578 | 578 |
func TestRegisterBadTwice(t *testing.T) {
|
| 579 |
- defer reset() |
|
| 579 |
+ a := New() |
|
| 580 | 580 |
network := &net.IPNet{
|
| 581 | 581 |
IP: []byte{192, 168, 1, 1},
|
| 582 | 582 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -586,20 +593,20 @@ func TestRegisterBadTwice(t *testing.T) {
|
| 586 | 586 |
Mask: []byte{255, 255, 255, 248},
|
| 587 | 587 |
} |
| 588 | 588 |
|
| 589 |
- if err := RegisterSubnet(network, subnet); err != nil {
|
|
| 589 |
+ if err := a.RegisterSubnet(network, subnet); err != nil {
|
|
| 590 | 590 |
t.Fatal(err) |
| 591 | 591 |
} |
| 592 | 592 |
subnet = &net.IPNet{
|
| 593 | 593 |
IP: []byte{192, 168, 1, 16},
|
| 594 | 594 |
Mask: []byte{255, 255, 255, 248},
|
| 595 | 595 |
} |
| 596 |
- if err := RegisterSubnet(network, subnet); err != ErrNetworkAlreadyRegistered {
|
|
| 596 |
+ if err := a.RegisterSubnet(network, subnet); err != ErrNetworkAlreadyRegistered {
|
|
| 597 | 597 |
t.Fatalf("Expecteded ErrNetworkAlreadyRegistered error, got %v", err)
|
| 598 | 598 |
} |
| 599 | 599 |
} |
| 600 | 600 |
|
| 601 | 601 |
func TestRegisterBadRange(t *testing.T) {
|
| 602 |
- defer reset() |
|
| 602 |
+ a := New() |
|
| 603 | 603 |
network := &net.IPNet{
|
| 604 | 604 |
IP: []byte{192, 168, 1, 1},
|
| 605 | 605 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -608,13 +615,13 @@ func TestRegisterBadRange(t *testing.T) {
|
| 608 | 608 |
IP: []byte{192, 168, 1, 1},
|
| 609 | 609 |
Mask: []byte{255, 255, 0, 0},
|
| 610 | 610 |
} |
| 611 |
- if err := RegisterSubnet(network, subnet); err != ErrBadSubnet {
|
|
| 611 |
+ if err := a.RegisterSubnet(network, subnet); err != ErrBadSubnet {
|
|
| 612 | 612 |
t.Fatalf("Expected ErrBadSubnet error, got %v", err)
|
| 613 | 613 |
} |
| 614 | 614 |
} |
| 615 | 615 |
|
| 616 | 616 |
func TestAllocateFromRange(t *testing.T) {
|
| 617 |
- defer reset() |
|
| 617 |
+ a := New() |
|
| 618 | 618 |
network := &net.IPNet{
|
| 619 | 619 |
IP: []byte{192, 168, 0, 1},
|
| 620 | 620 |
Mask: []byte{255, 255, 255, 0},
|
| ... | ... |
@@ -625,7 +632,7 @@ func TestAllocateFromRange(t *testing.T) {
|
| 625 | 625 |
Mask: []byte{255, 255, 255, 248},
|
| 626 | 626 |
} |
| 627 | 627 |
|
| 628 |
- if err := RegisterSubnet(network, subnet); err != nil {
|
|
| 628 |
+ if err := a.RegisterSubnet(network, subnet); err != nil {
|
|
| 629 | 629 |
t.Fatal(err) |
| 630 | 630 |
} |
| 631 | 631 |
expectedIPs := []net.IP{
|
| ... | ... |
@@ -637,19 +644,19 @@ func TestAllocateFromRange(t *testing.T) {
|
| 637 | 637 |
5: net.IPv4(192, 168, 0, 14), |
| 638 | 638 |
} |
| 639 | 639 |
for _, ip := range expectedIPs {
|
| 640 |
- rip, err := RequestIP(network, nil) |
|
| 640 |
+ rip, err := a.RequestIP(network, nil) |
|
| 641 | 641 |
if err != nil {
|
| 642 | 642 |
t.Fatal(err) |
| 643 | 643 |
} |
| 644 | 644 |
assertIPEquals(t, ip, rip) |
| 645 | 645 |
} |
| 646 | 646 |
|
| 647 |
- if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs {
|
|
| 647 |
+ if _, err := a.RequestIP(network, nil); err != ErrNoAvailableIPs {
|
|
| 648 | 648 |
t.Fatalf("Expected ErrNoAvailableIPs error, got %v", err)
|
| 649 | 649 |
} |
| 650 | 650 |
for _, ip := range expectedIPs {
|
| 651 |
- ReleaseIP(network, ip) |
|
| 652 |
- rip, err := RequestIP(network, nil) |
|
| 651 |
+ a.ReleaseIP(network, ip) |
|
| 652 |
+ rip, err := a.RequestIP(network, nil) |
|
| 653 | 653 |
if err != nil {
|
| 654 | 654 |
t.Fatal(err) |
| 655 | 655 |
} |
| ... | ... |
@@ -669,13 +676,15 @@ func BenchmarkRequestIP(b *testing.B) {
|
| 669 | 669 |
Mask: []byte{255, 255, 255, 0},
|
| 670 | 670 |
} |
| 671 | 671 |
b.ResetTimer() |
| 672 |
+ |
|
| 672 | 673 |
for i := 0; i < b.N; i++ {
|
| 674 |
+ a := New() |
|
| 675 |
+ |
|
| 673 | 676 |
for j := 0; j < 253; j++ {
|
| 674 |
- _, err := RequestIP(network, nil) |
|
| 677 |
+ _, err := a.RequestIP(network, nil) |
|
| 675 | 678 |
if err != nil {
|
| 676 | 679 |
b.Fatal(err) |
| 677 | 680 |
} |
| 678 | 681 |
} |
| 679 |
- reset() |
|
| 680 | 682 |
} |
| 681 | 683 |
} |