Browse Source

Get Mtu from default route

If no Mtu value is provided to the docker daemon, get the mtu from the
default route's interface.  If there is no default route, default to a
mtu of 1500.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
Michael Crosby 10 years ago
parent
commit
ff4e58ff56
1 changed files with 30 additions and 0 deletions
  1. 30 0
      daemon/daemon.go

+ 30 - 0
daemon/daemon.go

@@ -36,6 +36,7 @@ import (
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/runconfig"
 	"github.com/docker/docker/runconfig"
 	"github.com/docker/docker/trust"
 	"github.com/docker/docker/trust"
+	"github.com/docker/libcontainer/netlink"
 	"github.com/docker/libnetwork"
 	"github.com/docker/libnetwork"
 )
 )
 
 
@@ -589,6 +590,8 @@ func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.
 }
 }
 
 
 func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemon, err error) {
 func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemon, err error) {
+	setDefaultMtu(config)
+
 	// Ensure we have compatible configuration options
 	// Ensure we have compatible configuration options
 	if err := checkConfigOptions(config); err != nil {
 	if err := checkConfigOptions(config); err != nil {
 		return nil, err
 		return nil, err
@@ -981,3 +984,30 @@ func (daemon *Daemon) newBaseContainer(id string) CommonContainer {
 		root:         daemon.containerRoot(id),
 		root:         daemon.containerRoot(id),
 	}
 	}
 }
 }
+
+func setDefaultMtu(config *Config) {
+	// do nothing if the config does not have the default 0 value.
+	if config.Mtu != 0 {
+		return
+	}
+	config.Mtu = defaultNetworkMtu
+	if routeMtu, err := getDefaultRouteMtu(); err == nil {
+		config.Mtu = routeMtu
+	}
+}
+
+var errNoDefaultRoute = errors.New("no default route was found")
+
+// getDefaultRouteMtu returns the MTU for the default route's interface.
+func getDefaultRouteMtu() (int, error) {
+	routes, err := netlink.NetworkGetRoutes()
+	if err != nil {
+		return 0, err
+	}
+	for _, r := range routes {
+		if r.Default {
+			return r.Iface.MTU, nil
+		}
+	}
+	return 0, errNoDefaultRoute
+}