2015-05-01 20:01:21 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-01 20:01:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultBindingIP = net.IPv4(0, 0, 0, 0)
|
|
|
|
)
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
func (n *bridgeNetwork) allocatePorts(ep *bridgeEndpoint, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
|
|
|
|
if ep.extConnConfig == nil || ep.extConnConfig.PortBindings == nil {
|
2015-05-01 20:01:21 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defHostIP := defaultBindingIP
|
|
|
|
if reqDefBindIP != nil {
|
|
|
|
defHostIP = reqDefBindIP
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
return n.allocatePortsInternal(ep.extConnConfig.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
|
|
|
}
|