Skip to content

Commit 964ab71

Browse files
committed
Explicitly set MTU on bridge devices.
This is purely cosmetic - if a non-default MTU is configured, the bridge will have the default MTU=1500 until a container's 'veth' is connected and an MTU is set on the veth. That's a disconcerting, it looks like the config has been ignored - so, set the bridge's MTU explicitly. Fixes moby#37937 Signed-off-by: Rob Murray <[email protected]>
1 parent ce1ee98 commit 964ab71

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

integration/network/network_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func TestDefaultNetworkOpts(t *testing.T) {
240240

241241
// Create a new network
242242
networkName := "testnet"
243-
network.CreateNoError(ctx, t, c, networkName, func(create *types.NetworkCreate) {
243+
networkId := network.CreateNoError(ctx, t, c, networkName, func(create *types.NetworkCreate) {
244244
if tc.configFrom {
245245
create.ConfigFrom = &ntypes.ConfigReference{
246246
Network: "from-net",
@@ -249,6 +249,15 @@ func TestDefaultNetworkOpts(t *testing.T) {
249249
})
250250
defer c.NetworkRemove(ctx, networkName)
251251

252+
// Check the MTU of the bridge itself, before any devices are connected. (The
253+
// bridge's MTU will be set to the minimum MTU of anything connected to it, but
254+
// it's set explicitly on the bridge anyway - so it doesn't look like the option
255+
// was ignored.)
256+
cmd := exec.Command("ip", "link", "show", "br-"+networkId[:12])
257+
output, err := cmd.CombinedOutput()
258+
assert.NilError(t, err)
259+
assert.Check(t, is.Contains(string(output), fmt.Sprintf(" mtu %d ", tc.mtu)), "Bridge MTU should have been set to %d", tc.mtu)
260+
252261
// Start a container to inspect the MTU of its network interface
253262
id1 := container.Run(ctx, t, c, container.WithNetworkMode(networkName))
254263
defer c.ContainerRemove(ctx, id1, containertypes.RemoveOptions{Force: true})

libnetwork/drivers/bridge/bridge_linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,14 @@ func (d *driver) createNetwork(config *networkConfiguration) (err error) {
735735
bridgeSetup.queueStep(setupDefaultSysctl)
736736
}
737737

738+
// Always set the bridge's MTU if specified. This is purely cosmetic; a bridge's
739+
// MTU is the min MTU of device connected to it, and MTU will be set on each
740+
// 'veth'. But, for a non-default MTU, the bridge's MTU will look wrong until a
741+
// container is attached.
742+
if config.Mtu > 0 {
743+
bridgeSetup.queueStep(setupMTU)
744+
}
745+
738746
// Even if a bridge exists try to setup IPv4.
739747
bridgeSetup.queueStep(setupBridgeIPv4)
740748

libnetwork/drivers/bridge/setup_device_linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ func setupDevice(config *networkConfiguration, i *bridgeInterface) error {
4545
return nil
4646
}
4747

48+
func setupMTU(config *networkConfiguration, i *bridgeInterface) error {
49+
if err := i.nlh.LinkSetMTU(i.Link, config.Mtu); err != nil {
50+
log.G(context.TODO()).WithError(err).Errorf("Failed to set bridge MTU %s via netlink", config.BridgeName)
51+
return err
52+
}
53+
return nil
54+
}
55+
4856
func setupDefaultSysctl(config *networkConfiguration, i *bridgeInterface) error {
4957
// Disable IPv6 router advertisements originating on the bridge
5058
sysPath := filepath.Join("/proc/sys/net/ipv6/conf/", config.BridgeName, "accept_ra")

0 commit comments

Comments
 (0)