From 4d0a026c98721da874a03f5b6993045bae95842a Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Sat, 1 Feb 2014 03:38:39 -0800 Subject: [PATCH] docker: detect defaultNetworkMtu from default route Docker-DCO-1.1-Signed-off-by: Johan Euphrosine (github: google) --- config.go | 13 +++++++++++-- docker/docker.go | 9 +++++---- networkdriver/utils.go | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 40d885810b..e7f87ace77 100644 --- a/config.go +++ b/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" ) @@ -53,3 +55,10 @@ func DaemonConfigFromJob(job *engine.Job) *DaemonConfig { return config } + +func GetDefaultNetworkMtu() int { + if iface, err := networkdriver.GetDefaultRouteIface(); err == nil { + return iface.MTU + } + return defaultNetworkMtu +} diff --git a/docker/docker.go b/docker/docker.go index aaeced95f6..8d4ae7fce7 100644 --- a/docker/docker.go +++ b/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"}, docker.GetDefaultNetworkMtu(), "Set the containers network mtu") ) 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") diff --git a/networkdriver/utils.go b/networkdriver/utils.go index 6f6dbe4f18..611ca75867 100644 --- a/networkdriver/utils.go +++ b/networkdriver/utils.go @@ -3,8 +3,9 @@ package networkdriver import ( "encoding/binary" "fmt" - "github.com/dotcloud/docker/pkg/netlink" "net" + + "github.com/dotcloud/docker/pkg/netlink" ) var ( @@ -100,3 +101,16 @@ func GetIfaceAddr(name string) (net.Addr, error) { } return addrs4[0], nil } + +func GetDefaultRouteIface() (*net.Interface, error) { + rs, err := netlink.NetworkGetRoutes() + 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, fmt.Errorf("no default route") +}