Skip to content

Commit

Permalink
Merge pull request moby#8319 from MalteJ/ipv6-ipallocator
Browse files Browse the repository at this point in the history
Implementing IPv6 functionality for ipallocator
  • Loading branch information
Jessie Frazelle committed Oct 29, 2014
2 parents 7d53854 + f5f4d2d commit fd19e47
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 83 deletions.
91 changes: 52 additions & 39 deletions daemon/networkdriver/ipallocator/allocator.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
package ipallocator

import (
"encoding/binary"
"errors"
"math/big"
"net"
"sync"

"github.com/docker/docker/daemon/networkdriver"
"github.com/docker/docker/pkg/log"
)

// allocatedMap is thread-unsafe set of allocated IP
type allocatedMap struct {
p map[uint32]struct{}
last uint32
begin uint32
end uint32
p map[string]struct{}
last *big.Int
begin *big.Int
end *big.Int
}

func newAllocatedMap(network *net.IPNet) *allocatedMap {
firstIP, lastIP := networkdriver.NetworkRange(network)
begin := ipToInt(firstIP) + 2
end := ipToInt(lastIP) - 1
begin := big.NewInt(0).Add(ipToBigInt(firstIP), big.NewInt(1))
end := big.NewInt(0).Sub(ipToBigInt(lastIP), big.NewInt(1))

// if IPv4 network, then allocation range starts at begin + 1 because begin is bridge IP
if len(firstIP) == 4 {
begin = begin.Add(begin, big.NewInt(1))
}

return &allocatedMap{
p: make(map[uint32]struct{}),
p: make(map[string]struct{}),
begin: begin,
end: end,
last: begin - 1, // so first allocated will be begin
last: big.NewInt(0).Sub(begin, big.NewInt(1)), // so first allocated will be begin
}
}

Expand Down Expand Up @@ -56,13 +63,16 @@ func RegisterSubnet(network *net.IPNet, subnet *net.IPNet) error {
}
n := newAllocatedMap(network)
beginIP, endIP := networkdriver.NetworkRange(subnet)
begin, end := ipToInt(beginIP)+1, ipToInt(endIP)-1
if !(begin >= n.begin && end <= n.end && begin < end) {
begin := big.NewInt(0).Add(ipToBigInt(beginIP), big.NewInt(1))
end := big.NewInt(0).Sub(ipToBigInt(endIP), big.NewInt(1))

// Check that subnet is within network
if !(begin.Cmp(n.begin) >= 0 && end.Cmp(n.end) <= 0 && begin.Cmp(end) == -1) {
return ErrBadSubnet
}
n.begin = begin
n.end = end
n.last = begin - 1
n.begin.Set(begin)
n.end.Set(end)
n.last.Sub(begin, big.NewInt(1))
allocatedIPs[key] = n
return nil
}
Expand Down Expand Up @@ -93,58 +103,61 @@ func ReleaseIP(network *net.IPNet, ip net.IP) error {
lock.Lock()
defer lock.Unlock()
if allocated, exists := allocatedIPs[network.String()]; exists {
pos := ipToInt(ip)
delete(allocated.p, pos)
delete(allocated.p, ip.String())
}
return nil
}

func (allocated *allocatedMap) checkIP(ip net.IP) (net.IP, error) {
pos := ipToInt(ip)

// Verify that the IP address has not been already allocated.
if _, ok := allocated.p[pos]; ok {
if _, ok := allocated.p[ip.String()]; ok {
return nil, ErrIPAlreadyAllocated
}

pos := ipToBigInt(ip)
// Verify that the IP address is within our network range.
if pos < allocated.begin || pos > allocated.end {
if pos.Cmp(allocated.begin) == -1 || pos.Cmp(allocated.end) == 1 {
return nil, ErrIPOutOfRange
}

// Register the IP.
allocated.p[pos] = struct{}{}
allocated.last = pos
allocated.p[ip.String()] = struct{}{}
allocated.last.Set(pos)

return ip, nil
}

// return an available ip if one is currently available. If not,
// return the next available ip for the nextwork
func (allocated *allocatedMap) getNextIP() (net.IP, error) {
for pos := allocated.last + 1; pos != allocated.last; pos++ {
if pos > allocated.end {
pos = allocated.begin
for pos := big.NewInt(0).Add(allocated.last, big.NewInt(1)); pos.Cmp(allocated.last) != 0; pos.Add(pos, big.NewInt(1)) {
if pos.Cmp(allocated.end) == 1 {
pos.Set(allocated.begin)
}
if _, ok := allocated.p[pos]; ok {
if _, ok := allocated.p[bigIntToIP(pos).String()]; ok {
continue
}
allocated.p[pos] = struct{}{}
allocated.last = pos
return intToIP(pos), nil
allocated.p[bigIntToIP(pos).String()] = struct{}{}
allocated.last.Set(pos)
return bigIntToIP(pos), nil
}
return nil, ErrNoAvailableIPs
}

// Converts a 4 bytes IP into a 32 bit integer
func ipToInt(ip net.IP) uint32 {
return binary.BigEndian.Uint32(ip.To4())
// Converts a 4 bytes IP into a 128 bit integer
func ipToBigInt(ip net.IP) *big.Int {
x := big.NewInt(0)
if ip4 := ip.To4(); ip4 != nil {
return x.SetBytes(ip4)
}
if ip6 := ip.To16(); ip6 != nil {
return x.SetBytes(ip6)
}

log.Errorf("ipToBigInt: Wrong IP length! %s", ip)
return nil
}

// Converts 32 bit integer into a 4 bytes IP address
func intToIP(n uint32) net.IP {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, n)
ip := net.IP(b)
return ip
// Converts 128 bit integer into a 4 bytes IP address
func bigIntToIP(v *big.Int) net.IP {
return net.IP(v.Bytes())
}
Loading

0 comments on commit fd19e47

Please sign in to comment.