mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Refactor to support multiple ip addresses
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
da30eb7c20
commit
ac2a4e6410
2 changed files with 73 additions and 21 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/networkdriver"
|
"github.com/dotcloud/docker/networkdriver"
|
||||||
"github.com/dotcloud/docker/networkdriver/ipallocator"
|
"github.com/dotcloud/docker/networkdriver/ipallocator"
|
||||||
|
"github.com/dotcloud/docker/networkdriver/portallocator"
|
||||||
"github.com/dotcloud/docker/pkg/iptables"
|
"github.com/dotcloud/docker/pkg/iptables"
|
||||||
"github.com/dotcloud/docker/pkg/netlink"
|
"github.com/dotcloud/docker/pkg/netlink"
|
||||||
"github.com/dotcloud/docker/proxy"
|
"github.com/dotcloud/docker/proxy"
|
||||||
|
@ -20,8 +21,6 @@ const (
|
||||||
DefaultNetworkBridge = "docker0"
|
DefaultNetworkBridge = "docker0"
|
||||||
DisableNetworkBridge = "none"
|
DisableNetworkBridge = "none"
|
||||||
DefaultNetworkMtu = 1500
|
DefaultNetworkMtu = 1500
|
||||||
portRangeStart = 49153
|
|
||||||
portRangeEnd = 65535
|
|
||||||
siocBRADDBR = 0x89a0
|
siocBRADDBR = 0x89a0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,19 @@ package portallocator
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/dotcloud/docker/pkg/collections"
|
"github.com/dotcloud/docker/pkg/collections"
|
||||||
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type portMappings map[string]*collections.OrderedIntSet
|
type portMappings map[string]*collections.OrderedIntSet
|
||||||
|
|
||||||
|
type ipData struct {
|
||||||
|
allocatedPorts portMappings
|
||||||
|
availablePorts portMappings
|
||||||
|
}
|
||||||
|
|
||||||
|
type ipMapping map[net.IP]*ipData
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BeginPortRange = 49153
|
BeginPortRange = 49153
|
||||||
EndPortRange = 65535
|
EndPortRange = 65535
|
||||||
|
@ -20,22 +28,62 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
defaultIPData *ipData
|
||||||
|
|
||||||
lock = sync.Mutex{}
|
lock = sync.Mutex{}
|
||||||
allocatedPorts = portMappings{}
|
ips = ipMapping{}
|
||||||
availablePorts = portMappings{}
|
defaultIP = net.ParseIP("0.0.0.0")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
allocatedPorts["udp"] = collections.NewOrderedIntSet()
|
defaultIPData = newIpData()
|
||||||
availablePorts["udp"] = collections.NewOrderedIntSet()
|
ips[defaultIP] = defaultIP
|
||||||
allocatedPorts["tcp"] = collections.NewOrderedIntSet()
|
}
|
||||||
availablePorts["tcp"] = collections.NewOrderedIntSet()
|
|
||||||
|
func newIpData() {
|
||||||
|
data := &ipData{
|
||||||
|
allocatedPorts: portMappings{},
|
||||||
|
availablePorts: portMappings{},
|
||||||
|
}
|
||||||
|
|
||||||
|
data.allocatedPorts["udp"] = collections.NewOrderedIntSet()
|
||||||
|
data.availablePorts["udp"] = collections.NewOrderedIntSet()
|
||||||
|
data.allocatedPorts["tcp"] = collections.NewOrderedIntSet()
|
||||||
|
data.availablePorts["tcp"] = collections.NewOrderedIntSet()
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func getData(ip net.IP) *ipData {
|
||||||
|
data, exists := ips[ip]
|
||||||
|
if !exists {
|
||||||
|
data = newIpData()
|
||||||
|
ips[ip] = data
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateMapping(data *ipData, proto string, port int) error {
|
||||||
|
allocated := data.allocatedPorts[proto]
|
||||||
|
if allocated.Exists(proto) {
|
||||||
|
return ErrPortAlreadyAllocated
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func usePort(data *ipData, proto string, port int) {
|
||||||
|
allocated, available := data.allocatedPorts[proto], data.availablePorts[proto]
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
allocated.Push(port)
|
||||||
|
available.Remove(port)
|
||||||
|
allocated, available = defaultIPData.allocatedPorts[proto], defaultIPData.availablePorts[proto]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestPort returns an available port if the port is 0
|
// RequestPort returns an available port if the port is 0
|
||||||
// If the provided port is not 0 then it will be checked if
|
// If the provided port is not 0 then it will be checked if
|
||||||
// it is available for allocation
|
// it is available for allocation
|
||||||
func RequestPort(proto string, port int) (int, error) {
|
func RequestPort(ip net.IP, proto string, port int) (int, error) {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
@ -43,20 +91,28 @@ func RequestPort(proto string, port int) (int, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
data := getData(ip)
|
||||||
allocated = allocatedPorts[proto]
|
allocated, available := data.allocatedPorts[proto], data.availablePorts[proto]
|
||||||
available = availablePorts[proto]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
// If the user requested a specific port to be allocated
|
||||||
if port != 0 {
|
if port != 0 {
|
||||||
if allocated.Exists(port) {
|
if err := validateMapping(defaultIP, proto, port); err != nil {
|
||||||
return 0, ErrPortAlreadyAllocated
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !defaultIP.Equal(ip) {
|
||||||
|
if err := validateMapping(data, proto, port); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
available.Remove(port)
|
available.Remove(port)
|
||||||
allocated.Push(port)
|
allocated.Push(port)
|
||||||
|
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dynamic allocation
|
||||||
next := available.Pop()
|
next := available.Pop()
|
||||||
if next == 0 {
|
if next == 0 {
|
||||||
next = allocated.PullBack()
|
next = allocated.PullBack()
|
||||||
|
@ -76,7 +132,7 @@ func RequestPort(proto string, port int) (int, error) {
|
||||||
|
|
||||||
// ReleasePort will return the provided port back into the
|
// ReleasePort will return the provided port back into the
|
||||||
// pool for reuse
|
// pool for reuse
|
||||||
func ReleasePort(proto string, port int) error {
|
func ReleasePort(ip net.IP, proto string, port int) error {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
@ -84,10 +140,7 @@ func ReleasePort(proto string, port int) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
allocated, available := getCollection(ip, proto)
|
||||||
allocated = allocatedPorts[proto]
|
|
||||||
available = availablePorts[proto]
|
|
||||||
)
|
|
||||||
|
|
||||||
allocated.Remove(port)
|
allocated.Remove(port)
|
||||||
available.Push(port)
|
available.Push(port)
|
||||||
|
|
Loading…
Reference in a new issue