[lxc-devel] [lxd/master] Bridged device MTU inheritance

tomponline on Github lxc-bot at linuxcontainers.org
Wed Aug 14 11:23:38 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 749 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190814/1ad1e5f3/attachment.bin>
-------------- next part --------------
From cac95d8390634ecdec6e16f2d0fdb70e3a979a1d Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 14 Aug 2019 12:19:50 +0100
Subject: [PATCH 1/2] device/device/utils/network: Add support for MTU
 inheriting from parent on bridged devices

If custom mtu setting is not supplied, and parent is supplied, then MTU is inherited from parent device.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/device/device_utils_network.go | 34 +++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/lxd/device/device_utils_network.go b/lxd/device/device_utils_network.go
index 0596916789..dbf4c9501d 100644
--- a/lxd/device/device_utils_network.go
+++ b/lxd/device/device_utils_network.go
@@ -268,20 +268,22 @@ func NetworkAttachInterface(netName string, devName string) error {
 	return nil
 }
 
-// networkCreateVethPair creates and configures a veth pair. It accepts the name of the host side
-// interface as a parameter and returns the peer interface name.
+// networkCreateVethPair creates and configures a veth pair. It will set the hwaddr and mtu settings
+// in the supplied config to the newly created peer interface. If mtu is not specified, but parent
+// is supplied in config, then the MTU of the new peer interface will inherit the parent MTU.
+// Accepts the name of the host side interface as a parameter and returns the peer interface name.
 func networkCreateVethPair(hostName string, m config.Device) (string, error) {
 	peerName := NetworkRandomDevName("veth")
 
 	_, err := shared.RunCommand("ip", "link", "add", "dev", hostName, "type", "veth", "peer", "name", peerName)
 	if err != nil {
-		return "", fmt.Errorf("Failed to create the veth interfaces %s and %s: %s", hostName, peerName, err)
+		return "", fmt.Errorf("Failed to create the veth interfaces %s and %s: %v", hostName, peerName, err)
 	}
 
 	_, err = shared.RunCommand("ip", "link", "set", "dev", hostName, "up")
 	if err != nil {
 		NetworkRemoveInterface(hostName)
-		return "", fmt.Errorf("Failed to bring up the veth interface %s: %s", hostName, err)
+		return "", fmt.Errorf("Failed to bring up the veth interface %s: %v", hostName, err)
 	}
 
 	// Set the MAC address on peer.
@@ -289,16 +291,32 @@ func networkCreateVethPair(hostName string, m config.Device) (string, error) {
 		_, err := shared.RunCommand("ip", "link", "set", "dev", peerName, "address", m["hwaddr"])
 		if err != nil {
 			NetworkRemoveInterface(peerName)
-			return "", fmt.Errorf("Failed to set the MAC address: %s", err)
+			return "", fmt.Errorf("Failed to set the MAC address: %v", err)
 		}
 	}
 
-	// Set the MTU on peer.
+	// Set the MTU on peer. If not specified and has parent, will inherit MTU from parent.
 	if m["mtu"] != "" {
-		_, err := shared.RunCommand("ip", "link", "set", "dev", peerName, "mtu", m["mtu"])
+		MTU, err := strconv.ParseUint(m["mtu"], 10, 32)
+		if err != nil {
+			return "", fmt.Errorf("Invalid MTU specified: %v", err)
+
+		}
+
+		err = NetworkSetDevMTU(peerName, MTU)
+		if err != nil {
+			NetworkRemoveInterface(peerName)
+			return "", fmt.Errorf("Failed to set the MTU: %v", err)
+		}
+	} else if m["parent"] != "" {
+		parentMTU, err := NetworkGetDevMTU(m["parent"])
+		if err != nil {
+			return "", fmt.Errorf("Failed to get the parent MTU: %v", err)
+		}
+		err = NetworkSetDevMTU(peerName, parentMTU)
 		if err != nil {
 			NetworkRemoveInterface(peerName)
-			return "", fmt.Errorf("Failed to set the MTU: %s", err)
+			return "", fmt.Errorf("Failed to set the MTU: %v", err)
 		}
 	}
 

From 550878119d5dd253f65af0f74538d3d2ad1559f8 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 14 Aug 2019 12:20:45 +0100
Subject: [PATCH 2/2] test: Adds test for bridged parent MTU inheritance

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 test/suites/container_devices_nic_bridged.sh | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/test/suites/container_devices_nic_bridged.sh b/test/suites/container_devices_nic_bridged.sh
index 7c1917f732..d531e7d9f0 100644
--- a/test/suites/container_devices_nic_bridged.sh
+++ b/test/suites/container_devices_nic_bridged.sh
@@ -257,6 +257,14 @@ test_container_devices_nic_bridged() {
     false
   fi
 
+  # Check that MTU is inherited from parent device when not specified on device.
+  lxc network set "${brName}" bridge.mtu "1405"
+  lxc config device unset "${ctName}" eth0 mtu
+  if ! lxc exec "${ctName}" -- grep "1405" /sys/class/net/eth0/mtu ; then
+    echo "mtu not inherited from parent"
+    false
+  fi
+  lxc network unset "${brName}" bridge.mtu
 
   # Add an external 3rd party route to the bridge interface and check that it and the container
   # routes remain when the network is reconfigured.


More information about the lxc-devel mailing list