2015-05-01 20:01:21 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-20 16:28:46 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-01 20:01:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultBindingIP = net.IPv4(0, 0, 0, 0)
|
|
|
|
)
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
func (n *bridgeNetwork) allocatePorts(epConfig *endpointConfiguration, ep *bridgeEndpoint, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
2015-05-01 20:01:21 -04:00
|
|
|
if epConfig == nil || epConfig.PortBindings == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defHostIP := defaultBindingIP
|
|
|
|
if reqDefBindIP != nil {
|
|
|
|
defHostIP = reqDefBindIP
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:21:23 -04:00
|
|
|
return n.allocatePortsInternal(epConfig.PortBindings, ep.addr.IP, defHostIP, ulPxyEnabled)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) allocatePortsInternal(bindings []types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
2015-05-20 16:28:46 -04:00
|
|
|
bs := make([]types.PortBinding, 0, len(bindings))
|
2015-05-01 20:01:21 -04:00
|
|
|
for _, c := range bindings {
|
|
|
|
b := c.GetCopy()
|
2015-06-01 16:14:29 -04:00
|
|
|
if err := n.allocatePort(&b, containerIP, defHostIP, ulPxyEnabled); err != nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
// On allocation failure, release previously allocated ports. On cleanup error, just log a warning message
|
2015-06-01 16:14:29 -04:00
|
|
|
if cuErr := n.releasePortsInternal(bs); cuErr != nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
logrus.Warnf("Upon allocation failure for %v, failed to clear previously allocated port bindings: %v", b, cuErr)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bs = append(bs, b)
|
|
|
|
}
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) allocatePort(bnd *types.PortBinding, containerIP, defHostIP net.IP, ulPxyEnabled bool) error {
|
2015-05-01 20:01:21 -04:00
|
|
|
var (
|
|
|
|
host net.Addr
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Store the container interface address in the operational binding
|
|
|
|
bnd.IP = containerIP
|
|
|
|
|
|
|
|
// Adjust the host address in the operational binding
|
|
|
|
if len(bnd.HostIP) == 0 {
|
|
|
|
bnd.HostIP = defHostIP
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:19:42 -04:00
|
|
|
// Adjust HostPortEnd if this is not a range.
|
|
|
|
if bnd.HostPortEnd == 0 {
|
|
|
|
bnd.HostPortEnd = bnd.HostPort
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
// Construct the container side transport address
|
|
|
|
container, err := bnd.ContainerAddr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try up to maxAllocatePortAttempts times to get a port that's not already allocated.
|
|
|
|
for i := 0; i < maxAllocatePortAttempts; i++ {
|
2015-06-11 21:19:42 -04:00
|
|
|
if host, err = n.portMapper.MapRange(container, bnd.HostIP, int(bnd.HostPort), int(bnd.HostPortEnd), ulPxyEnabled); err == nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// There is no point in immediately retrying to map an explicitly chosen port.
|
|
|
|
if bnd.HostPort != 0 {
|
2015-06-11 21:19:42 -04:00
|
|
|
logrus.Warnf("Failed to allocate and map port %d-%d: %s", bnd.HostPort, bnd.HostPortEnd, err)
|
2015-05-01 20:01:21 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
logrus.Warnf("Failed to allocate and map port: %s, retry: %d", err, i+1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the host port (regardless it was or not specified in the binding)
|
|
|
|
switch netAddr := host.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
bnd.HostPort = uint16(host.(*net.TCPAddr).Port)
|
|
|
|
return nil
|
|
|
|
case *net.UDPAddr:
|
|
|
|
bnd.HostPort = uint16(host.(*net.UDPAddr).Port)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
// For completeness
|
|
|
|
return ErrUnsupportedAddressType(fmt.Sprintf("%T", netAddr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) releasePorts(ep *bridgeEndpoint) error {
|
|
|
|
return n.releasePortsInternal(ep.portMapping)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) releasePortsInternal(bindings []types.PortBinding) error {
|
2015-05-01 20:01:21 -04:00
|
|
|
var errorBuf bytes.Buffer
|
|
|
|
|
|
|
|
// Attempt to release all port bindings, do not stop on failure
|
|
|
|
for _, m := range bindings {
|
2015-06-01 16:14:29 -04:00
|
|
|
if err := n.releasePort(m); err != nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
errorBuf.WriteString(fmt.Sprintf("\ncould not release %v because of %v", m, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorBuf.Len() != 0 {
|
|
|
|
return errors.New(errorBuf.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
func (n *bridgeNetwork) releasePort(bnd types.PortBinding) error {
|
2015-05-01 20:01:21 -04:00
|
|
|
// Construct the host side transport address
|
|
|
|
host, err := bnd.HostAddr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-01 16:14:29 -04:00
|
|
|
return n.portMapper.Unmap(host)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|