Merge pull request #3887 from proppy/detect-network-mtu

docker: detect defaultNetworkMtu from default route
This commit is contained in:
Guillaume J. Charmes 2014-02-03 16:35:15 -08:00
commit b56440fa24
5 changed files with 40 additions and 12 deletions

View file

@ -1,12 +1,14 @@
package docker
import (
"github.com/dotcloud/docker/engine"
"net"
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/networkdriver"
)
const (
DefaultNetworkMtu = 1500
defaultNetworkMtu = 1500
DisableNetworkBridge = "none"
)
@ -47,9 +49,16 @@ func DaemonConfigFromJob(job *engine.Job) *DaemonConfig {
if mtu := job.GetenvInt("Mtu"); mtu != 0 {
config.Mtu = mtu
} else {
config.Mtu = DefaultNetworkMtu
config.Mtu = GetDefaultNetworkMtu()
}
config.DisableNetwork = job.Getenv("BridgeIface") == DisableNetworkBridge
return config
}
func GetDefaultNetworkMtu() int {
if iface, err := networkdriver.GetDefaultRouteIface(); err == nil {
return iface.MTU
}
return defaultNetworkMtu
}

View file

@ -2,15 +2,16 @@ package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/api"
"github.com/dotcloud/docker/engine"
flag "github.com/dotcloud/docker/pkg/mflag"
"github.com/dotcloud/docker/sysinit"
"github.com/dotcloud/docker/utils"
"log"
"os"
"strings"
)
var (
@ -42,7 +43,7 @@ func main() {
flInterContainerComm = flag.Bool([]string{"#icc", "-icc"}, true, "Enable inter-container communication")
flGraphDriver = flag.String([]string{"s", "-storage-driver"}, "", "Force the docker runtime to use a specific storage driver")
flHosts = docker.NewListOpts(docker.ValidateHost)
flMtu = flag.Int([]string{"#mtu", "-mtu"}, docker.DefaultNetworkMtu, "Set the containers network mtu")
flMtu = flag.Int([]string{"#mtu", "-mtu"}, 0, "Set the containers network MTU; if no value is provided: default to the default route MTU or 1500 if not default route is available")
)
flag.Var(&flDns, []string{"#dns", "-dns"}, "Force docker to use specific DNS servers")
flag.Var(&flHosts, []string{"H", "-host"}, "tcp://host:port, unix://path/to/socket, fd://* or fd://socketfd to use in daemon mode. Multiple sockets can be specified")

View file

@ -80,6 +80,7 @@ Commands
-r, --restart=true: Restart previously running containers
-s, --storage-driver="": Force the docker runtime to use a specific storage driver
-v, --version=false: Print version information and quit
-mtu, --mtu=0: Set the containers network MTU; if no value is provided: default to the default route MTU or 1500 if not default route is available
The Docker daemon is the persistent process that manages containers. Docker uses the same binary for both the
daemon and client. To run the daemon you provide the ``-d`` flag.

View file

@ -4,9 +4,6 @@ import (
"archive/tar"
"bytes"
"fmt"
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/utils"
"io"
"io/ioutil"
"net/http"
@ -16,6 +13,10 @@ import (
"strings"
"testing"
"time"
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/utils"
)
// This file contains utility functions for docker's unit test suite.
@ -32,7 +33,7 @@ func mkRuntime(f utils.Fataler) *docker.Runtime {
config := &docker.DaemonConfig{
Root: root,
AutoRestart: false,
Mtu: docker.DefaultNetworkMtu,
Mtu: docker.GetDefaultNetworkMtu(),
}
eng, err := engine.New(root)

View file

@ -2,13 +2,16 @@ package networkdriver
import (
"encoding/binary"
"errors"
"fmt"
"github.com/dotcloud/docker/pkg/netlink"
"net"
"github.com/dotcloud/docker/pkg/netlink"
)
var (
networkGetRoutesFct = netlink.NetworkGetRoutes
ErrNoDefaultRoute = errors.New("no default route")
)
func CheckNameserverOverlaps(nameservers []string, toCheck *net.IPNet) error {
@ -100,3 +103,16 @@ func GetIfaceAddr(name string) (net.Addr, error) {
}
return addrs4[0], nil
}
func GetDefaultRouteIface() (*net.Interface, error) {
rs, err := networkGetRoutesFct()
if err != nil {
return nil, fmt.Errorf("unable to get routes: %v", err)
}
for _, r := range rs {
if r.Default {
return r.Iface, nil
}
}
return nil, ErrNoDefaultRoute
}