2013-02-21 01:47:09 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-02-22 02:33:23 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2013-02-25 18:45:23 +00:00
|
|
|
"errors"
|
2013-02-21 02:20:18 +00:00
|
|
|
"fmt"
|
2013-02-28 19:50:02 +00:00
|
|
|
"log"
|
2013-02-21 01:47:09 +00:00
|
|
|
"net"
|
2013-02-28 19:50:02 +00:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-02-21 01:47:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-02-21 02:20:18 +00:00
|
|
|
networkBridgeIface = "lxcbr0"
|
2013-02-28 19:50:02 +00:00
|
|
|
portRangeStart = 49153
|
|
|
|
portRangeEnd = 65535
|
2013-02-21 01:47:09 +00:00
|
|
|
)
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Calculates the first and last IP addresses in an IPNet
|
2013-02-22 02:33:23 +00:00
|
|
|
func networkRange(network *net.IPNet) (net.IP, net.IP) {
|
|
|
|
netIP := network.IP.To4()
|
|
|
|
firstIP := netIP.Mask(network.Mask)
|
|
|
|
lastIP := net.IPv4(0, 0, 0, 0).To4()
|
|
|
|
for i := 0; i < len(lastIP); i++ {
|
|
|
|
lastIP[i] = netIP[i] | ^network.Mask[i]
|
|
|
|
}
|
|
|
|
return firstIP, lastIP
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Converts a 4 bytes IP into a 32 bit integer
|
2013-02-22 02:33:23 +00:00
|
|
|
func ipToInt(ip net.IP) (int32, error) {
|
|
|
|
buf := bytes.NewBuffer(ip.To4())
|
|
|
|
var n int32
|
|
|
|
if err := binary.Read(buf, binary.BigEndian, &n); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Converts 32 bit integer into a 4 bytes IP address
|
2013-02-22 02:33:23 +00:00
|
|
|
func intToIp(n int32) (net.IP, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := binary.Write(&buf, binary.BigEndian, &n); err != nil {
|
|
|
|
return net.IP{}, err
|
|
|
|
}
|
|
|
|
ip := net.IPv4(0, 0, 0, 0).To4()
|
|
|
|
for i := 0; i < net.IPv4len; i++ {
|
|
|
|
ip[i] = buf.Bytes()[i]
|
|
|
|
}
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Given a netmask, calculates the number of available hosts
|
2013-02-22 02:33:23 +00:00
|
|
|
func networkSize(mask net.IPMask) (int32, error) {
|
2013-02-25 22:06:22 +00:00
|
|
|
m := net.IPv4Mask(0, 0, 0, 0)
|
2013-02-22 02:33:23 +00:00
|
|
|
for i := 0; i < net.IPv4len; i++ {
|
2013-02-25 22:06:22 +00:00
|
|
|
m[i] = ^mask[i]
|
2013-02-22 02:33:23 +00:00
|
|
|
}
|
2013-02-25 22:06:22 +00:00
|
|
|
buf := bytes.NewBuffer(m)
|
2013-02-22 02:33:23 +00:00
|
|
|
var n int32
|
|
|
|
if err := binary.Read(buf, binary.BigEndian, &n); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n + 1, nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Wrapper around the iptables command
|
|
|
|
func iptables(args ...string) error {
|
|
|
|
if err := exec.Command("/sbin/iptables", args...).Run(); err != nil {
|
|
|
|
return fmt.Errorf("iptables failed: iptables %v", strings.Join(args, " "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the IPv4 address of a network interface
|
2013-02-25 22:06:22 +00:00
|
|
|
func getIfaceAddr(name string) (net.Addr, error) {
|
2013-02-21 02:20:18 +00:00
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var addrs4 []net.Addr
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip := (addr.(*net.IPNet)).IP
|
|
|
|
if ip4 := ip.To4(); len(ip4) == net.IPv4len {
|
|
|
|
addrs4 = append(addrs4, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case len(addrs4) == 0:
|
2013-02-28 19:50:02 +00:00
|
|
|
return nil, fmt.Errorf("Interface %v has no IP addresses", name)
|
2013-02-21 02:20:18 +00:00
|
|
|
case len(addrs4) > 1:
|
2013-03-21 16:19:22 +00:00
|
|
|
fmt.Printf("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n",
|
|
|
|
name, (addrs4[0].(*net.IPNet)).IP)
|
2013-02-21 02:20:18 +00:00
|
|
|
}
|
|
|
|
return addrs4[0], nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Port mapper takes care of mapping external ports to containers by setting
|
|
|
|
// up iptables rules.
|
|
|
|
// It keeps track of all mappings and is able to unmap at will
|
|
|
|
type PortMapper struct {
|
|
|
|
mapping map[int]net.TCPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *PortMapper) cleanup() error {
|
|
|
|
// Ignore errors - This could mean the chains were never set up
|
|
|
|
iptables("-t", "nat", "-D", "PREROUTING", "-j", "DOCKER")
|
|
|
|
iptables("-t", "nat", "-F", "DOCKER")
|
|
|
|
iptables("-t", "nat", "-X", "DOCKER")
|
|
|
|
mapper.mapping = make(map[int]net.TCPAddr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *PortMapper) setup() error {
|
|
|
|
if err := iptables("-t", "nat", "-N", "DOCKER"); err != nil {
|
|
|
|
return errors.New("Unable to setup port networking: Failed to create DOCKER chain")
|
2013-02-21 02:20:18 +00:00
|
|
|
}
|
2013-02-28 19:50:02 +00:00
|
|
|
if err := iptables("-t", "nat", "-A", "PREROUTING", "-j", "DOCKER"); err != nil {
|
|
|
|
return errors.New("Unable to setup port networking: Failed to inject docker in PREROUTING chain")
|
|
|
|
}
|
2013-03-22 14:06:14 +00:00
|
|
|
if err := iptables("-t", "nat", "-A", "OUTPUT", "-j", "DOCKER"); err != nil {
|
|
|
|
return errors.New("Unable to setup port networking: Failed to inject docker in OUTPUT chain")
|
|
|
|
}
|
2013-02-28 19:50:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *PortMapper) iptablesForward(rule string, port int, dest net.TCPAddr) error {
|
|
|
|
return iptables("-t", "nat", rule, "DOCKER", "-p", "tcp", "--dport", strconv.Itoa(port),
|
|
|
|
"-j", "DNAT", "--to-destination", net.JoinHostPort(dest.IP.String(), strconv.Itoa(dest.Port)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mapper *PortMapper) Map(port int, dest net.TCPAddr) error {
|
|
|
|
if err := mapper.iptablesForward("-A", port, dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mapper.mapping[port] = dest
|
|
|
|
return nil
|
|
|
|
}
|
2013-02-25 22:06:22 +00:00
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
func (mapper *PortMapper) Unmap(port int) error {
|
|
|
|
dest, ok := mapper.mapping[port]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Port is not mapped")
|
|
|
|
}
|
|
|
|
if err := mapper.iptablesForward("-D", port, dest); err != nil {
|
|
|
|
return err
|
2013-02-22 02:33:23 +00:00
|
|
|
}
|
2013-02-28 19:50:02 +00:00
|
|
|
delete(mapper.mapping, port)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPortMapper() (*PortMapper, error) {
|
|
|
|
mapper := &PortMapper{}
|
|
|
|
if err := mapper.cleanup(); err != nil {
|
2013-02-25 22:06:22 +00:00
|
|
|
return nil, err
|
2013-02-21 01:47:09 +00:00
|
|
|
}
|
2013-02-28 19:50:02 +00:00
|
|
|
if err := mapper.setup(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return mapper, nil
|
2013-02-21 01:47:09 +00:00
|
|
|
}
|
2013-02-25 18:45:23 +00:00
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
// Port allocator: Atomatically allocate and release networking ports
|
|
|
|
type PortAllocator struct {
|
|
|
|
ports chan (int)
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
func (alloc *PortAllocator) populate(start, end int) {
|
|
|
|
alloc.ports = make(chan int, end-start)
|
|
|
|
for port := start; port < end; port++ {
|
|
|
|
alloc.ports <- port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alloc *PortAllocator) Acquire() (int, error) {
|
2013-02-25 18:45:23 +00:00
|
|
|
select {
|
2013-02-28 19:50:02 +00:00
|
|
|
case port := <-alloc.ports:
|
|
|
|
return port, nil
|
2013-02-25 18:45:23 +00:00
|
|
|
default:
|
2013-02-28 19:50:02 +00:00
|
|
|
return -1, errors.New("No more ports available")
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|
2013-02-28 19:50:02 +00:00
|
|
|
return -1, nil
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
func (alloc *PortAllocator) Release(port int) error {
|
2013-02-25 18:45:23 +00:00
|
|
|
select {
|
2013-02-28 19:50:02 +00:00
|
|
|
case alloc.ports <- port:
|
2013-02-25 18:45:23 +00:00
|
|
|
return nil
|
|
|
|
default:
|
2013-02-28 19:50:02 +00:00
|
|
|
return errors.New("Too many ports have been released")
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
func newPortAllocator(start, end int) (*PortAllocator, error) {
|
|
|
|
allocator := &PortAllocator{}
|
|
|
|
allocator.populate(start, end)
|
|
|
|
return allocator, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IP allocator: Atomatically allocate and release networking ports
|
|
|
|
type IPAllocator struct {
|
|
|
|
network *net.IPNet
|
|
|
|
queue chan (net.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alloc *IPAllocator) populate() error {
|
|
|
|
firstIP, _ := networkRange(alloc.network)
|
|
|
|
size, err := networkSize(alloc.network.Mask)
|
2013-02-25 18:45:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// The queue size should be the network size - 3
|
|
|
|
// -1 for the network address, -1 for the broadcast address and
|
|
|
|
// -1 for the gateway address
|
|
|
|
alloc.queue = make(chan net.IP, size-3)
|
|
|
|
for i := int32(1); i < size-1; i++ {
|
|
|
|
ipNum, err := ipToInt(firstIP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ip, err := intToIp(ipNum + int32(i))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Discard the network IP (that's the host IP address)
|
2013-02-28 19:50:02 +00:00
|
|
|
if ip.Equal(alloc.network.IP) {
|
2013-02-25 18:45:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-02-28 19:50:02 +00:00
|
|
|
alloc.queue <- ip
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alloc *IPAllocator) Acquire() (net.IP, error) {
|
|
|
|
select {
|
|
|
|
case ip := <-alloc.queue:
|
|
|
|
return ip, nil
|
|
|
|
default:
|
|
|
|
return net.IP{}, errors.New("No more IP addresses available")
|
|
|
|
}
|
|
|
|
return net.IP{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alloc *IPAllocator) Release(ip net.IP) error {
|
|
|
|
select {
|
|
|
|
case alloc.queue <- ip:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("Too many IP addresses have been released")
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
func newIPAllocator(network *net.IPNet) (*IPAllocator, error) {
|
|
|
|
alloc := &IPAllocator{
|
|
|
|
network: network,
|
|
|
|
}
|
|
|
|
if err := alloc.populate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return alloc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network interface represents the networking stack of a container
|
|
|
|
type NetworkInterface struct {
|
|
|
|
IPNet net.IPNet
|
|
|
|
Gateway net.IP
|
|
|
|
|
|
|
|
manager *NetworkManager
|
|
|
|
extPorts []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate an external TCP port and map it to the interface
|
|
|
|
func (iface *NetworkInterface) AllocatePort(port int) (int, error) {
|
|
|
|
extPort, err := iface.manager.portAllocator.Acquire()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2013-03-21 03:11:16 +00:00
|
|
|
if err := iface.manager.portMapper.Map(extPort, net.TCPAddr{IP: iface.IPNet.IP, Port: port}); err != nil {
|
2013-02-28 19:50:02 +00:00
|
|
|
iface.manager.portAllocator.Release(extPort)
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
iface.extPorts = append(iface.extPorts, extPort)
|
|
|
|
return extPort, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release: Network cleanup - release all resources
|
|
|
|
func (iface *NetworkInterface) Release() error {
|
|
|
|
for _, port := range iface.extPorts {
|
|
|
|
if err := iface.manager.portMapper.Unmap(port); err != nil {
|
|
|
|
log.Printf("Unable to unmap port %v: %v", port, err)
|
|
|
|
}
|
|
|
|
if err := iface.manager.portAllocator.Release(port); err != nil {
|
|
|
|
log.Printf("Unable to release port %v: %v", port, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return iface.manager.ipAllocator.Release(iface.IPNet.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network Manager manages a set of network interfaces
|
|
|
|
// Only *one* manager per host machine should be used
|
|
|
|
type NetworkManager struct {
|
|
|
|
bridgeIface string
|
|
|
|
bridgeNetwork *net.IPNet
|
|
|
|
|
|
|
|
ipAllocator *IPAllocator
|
|
|
|
portAllocator *PortAllocator
|
|
|
|
portMapper *PortMapper
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a network interface
|
|
|
|
func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
|
|
|
|
ip, err := manager.ipAllocator.Acquire()
|
2013-02-25 18:45:23 +00:00
|
|
|
if err != nil {
|
2013-02-25 22:06:22 +00:00
|
|
|
return nil, err
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|
2013-02-25 22:06:22 +00:00
|
|
|
iface := &NetworkInterface{
|
2013-03-20 13:02:25 +00:00
|
|
|
IPNet: net.IPNet{IP: ip, Mask: manager.bridgeNetwork.Mask},
|
2013-02-28 19:50:02 +00:00
|
|
|
Gateway: manager.bridgeNetwork.IP,
|
|
|
|
manager: manager,
|
2013-02-25 22:06:22 +00:00
|
|
|
}
|
|
|
|
return iface, nil
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:50:02 +00:00
|
|
|
func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
|
|
|
|
addr, err := getIfaceAddr(bridgeIface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
network := addr.(*net.IPNet)
|
|
|
|
|
|
|
|
ipAllocator, err := newIPAllocator(network)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
portAllocator, err := newPortAllocator(portRangeStart, portRangeEnd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
portMapper, err := newPortMapper()
|
|
|
|
|
|
|
|
manager := &NetworkManager{
|
|
|
|
bridgeIface: bridgeIface,
|
|
|
|
bridgeNetwork: network,
|
|
|
|
ipAllocator: ipAllocator,
|
|
|
|
portAllocator: portAllocator,
|
|
|
|
portMapper: portMapper,
|
|
|
|
}
|
|
|
|
return manager, nil
|
2013-02-25 18:45:23 +00:00
|
|
|
}
|