瀏覽代碼

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

docker: detect defaultNetworkMtu from default route
Guillaume J. Charmes 11 年之前
父節點
當前提交
b56440fa24
共有 5 個文件被更改,包括 40 次插入12 次删除
  1. 12 3
      config.go
  2. 5 4
      docker/docker.go
  3. 1 0
      docs/sources/reference/commandline/cli.rst
  4. 5 4
      integration/utils_test.go
  5. 17 1
      networkdriver/utils.go

+ 12 - 3
config.go

@@ -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
+}

+ 5 - 4
docker/docker.go

@@ -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")

+ 1 - 0
docs/sources/reference/commandline/cli.rst

@@ -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.

+ 5 - 4
integration/utils_test.go

@@ -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)

+ 17 - 1
networkdriver/utils.go

@@ -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
+}