[lxc-devel] [lxd/master] Device Utils

tomponline on Github lxc-bot at linuxcontainers.org
Mon Jul 15 11:37:35 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 603 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190715/9d8d67b3/attachment.bin>
-------------- next part --------------
From 8e7a078554f38cad08420135cd9b612812bfe22a Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:15:32 +0100
Subject: [PATCH 1/7] device/utils: Moves networkSysctlGet to device_utils

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       | 11 ++++++-----
 lxd/device/device_utils.go | 17 +++++++++++++++++
 lxd/networks_utils.go      | 13 ++-----------
 3 files changed, 25 insertions(+), 16 deletions(-)
 create mode 100644 lxd/device/device_utils.go

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 5dbc44b9fd..44c5eda7ab 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -31,6 +31,7 @@ import (
 	"github.com/lxc/lxd/lxd/cluster"
 	"github.com/lxc/lxd/lxd/db"
 	"github.com/lxc/lxd/lxd/db/query"
+	"github.com/lxc/lxd/lxd/device"
 	"github.com/lxc/lxd/lxd/dnsmasq"
 	"github.com/lxc/lxd/lxd/iptables"
 	"github.com/lxc/lxd/lxd/maas"
@@ -1932,7 +1933,7 @@ func (c *containerLXC) initLXCIPVLAN(cc *lxc.Container, networkKeyPrefix string,
 	if m["ipv4.address"] != "" {
 		//Check necessary sysctls are configured for use with l2proxy parent in IPVLAN l3s mode.
 		ipv4FwdPath := fmt.Sprintf("ipv4/conf/%s/forwarding", m["parent"])
-		sysctlVal, err := networkSysctlGet(ipv4FwdPath)
+		sysctlVal, err := device.NetworkSysctlGet(ipv4FwdPath)
 		if err != nil {
 			return errors.Wrapf(err, "Error reading net sysctl %s", ipv4FwdPath)
 		}
@@ -1957,7 +1958,7 @@ func (c *containerLXC) initLXCIPVLAN(cc *lxc.Container, networkKeyPrefix string,
 	if m["ipv6.address"] != "" {
 		//Check necessary sysctls are configured for use with l2proxy parent in IPVLAN l3s mode.
 		ipv6FwdPath := fmt.Sprintf("ipv6/conf/%s/forwarding", m["parent"])
-		sysctlVal, err := networkSysctlGet(ipv6FwdPath)
+		sysctlVal, err := device.NetworkSysctlGet(ipv6FwdPath)
 		if err != nil {
 			return errors.Wrapf(err, "Error reading net sysctl %s", ipv6FwdPath)
 		}
@@ -1966,7 +1967,7 @@ func (c *containerLXC) initLXCIPVLAN(cc *lxc.Container, networkKeyPrefix string,
 		}
 
 		ipv6ProxyNdpPath := fmt.Sprintf("ipv6/conf/%s/proxy_ndp", m["parent"])
-		sysctlVal, err = networkSysctlGet(ipv6ProxyNdpPath)
+		sysctlVal, err = device.NetworkSysctlGet(ipv6ProxyNdpPath)
 		if err != nil {
 			return errors.Wrapf(err, "Error reading net sysctl %s", ipv6ProxyNdpPath)
 		}
@@ -2144,7 +2145,7 @@ func (c *containerLXC) startCommon() (string, error) {
 
 			if shared.IsTrue(m["security.ipv6_filtering"]) {
 				// Check br_netfilter is loaded and enabled for IPv6.
-				sysctlVal, err := networkSysctlGet("bridge/bridge-nf-call-ip6tables")
+				sysctlVal, err := device.NetworkSysctlGet("bridge/bridge-nf-call-ip6tables")
 				if err != nil || sysctlVal != "1\n" {
 					return "", errors.Wrapf(err, "security.ipv6_filtering requires br_netfilter and sysctl net.bridge.bridge-nf-call-ip6tables=1")
 				}
@@ -8788,7 +8789,7 @@ func (c *containerLXC) setNetworkFilters(deviceName string, m types.Device) (err
 
 	if shared.IsTrue(m["security.ipv6_filtering"]) {
 		// Check br_netfilter is loaded and enabled for IPv6.
-		sysctlVal, err := networkSysctlGet("bridge/bridge-nf-call-ip6tables")
+		sysctlVal, err := device.NetworkSysctlGet("bridge/bridge-nf-call-ip6tables")
 		if err != nil || sysctlVal != "1\n" {
 			return errors.Wrapf(err, "security.ipv6_filtering requires br_netfilter and sysctl net.bridge.bridge-nf-call-ip6tables=1")
 		}
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
new file mode 100644
index 0000000000..44c69b9717
--- /dev/null
+++ b/lxd/device/device_utils.go
@@ -0,0 +1,17 @@
+package device
+
+import (
+	"fmt"
+	"io/ioutil"
+)
+
+// NetworkSysctlGet retrieves the value of a sysctl file in /proc/sys/net.
+func NetworkSysctlGet(path string) (string, error) {
+	// Read the current content
+	content, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/net/%s", path))
+	if err != nil {
+		return "", err
+	}
+
+	return string(content), nil
+}
diff --git a/lxd/networks_utils.go b/lxd/networks_utils.go
index d15b8726e1..6f4a4ce43c 100644
--- a/lxd/networks_utils.go
+++ b/lxd/networks_utils.go
@@ -27,6 +27,7 @@ import (
 
 	"github.com/lxc/lxd/lxd/cluster"
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/device"
 	"github.com/lxc/lxd/lxd/dnsmasq"
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/state"
@@ -1273,19 +1274,9 @@ func networksGetForkdnsServersList(networkName string) ([]string, error) {
 	return servers, nil
 }
 
-func networkSysctlGet(path string) (string, error) {
-	// Read the current content
-	content, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/net/%s", path))
-	if err != nil {
-		return "", err
-	}
-
-	return string(content), nil
-}
-
 func networkSysctlSet(path string, value string) error {
 	// Get current value
-	current, err := networkSysctlGet(path)
+	current, err := device.NetworkSysctlGet(path)
 	if err == nil && current == value {
 		// Nothing to update
 		return nil

From da665b1f789d9f3b94c6aa6863f138d5fceec219 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:18:11 +0100
Subject: [PATCH 2/7] device/utils: Moves networkSysctlSet to device_utils

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       |  4 ++--
 lxd/device/device_utils.go | 12 ++++++++++++
 lxd/networks.go            | 13 +++++++------
 lxd/networks_utils.go      | 12 ------------
 4 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 44c5eda7ab..e89aaad197 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2679,7 +2679,7 @@ func (c *containerLXC) createVlanDeviceIfNeeded(m types.Device, hostName string)
 			}
 
 			// Attempt to disable IPv6 router advertisement acceptance
-			networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", hostName), "0")
+			device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", hostName), "0")
 
 			// We created a new vlan interface, return true
 			return true, nil
@@ -8318,7 +8318,7 @@ func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string,
 			}
 
 			// Attempt to disable router advertisement acceptance
-			networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", n1), "0")
+			device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", n1), "0")
 		}
 
 		// Record the new device's host name for use in setupHostVethDevice()
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
index 44c69b9717..94c618900e 100644
--- a/lxd/device/device_utils.go
+++ b/lxd/device/device_utils.go
@@ -15,3 +15,15 @@ func NetworkSysctlGet(path string) (string, error) {
 
 	return string(content), nil
 }
+
+// NetworkSysctlSet writes a value to a sysctl file in /proc/sys/net.
+func NetworkSysctlSet(path string, value string) error {
+	// Get current value
+	current, err := NetworkSysctlGet(path)
+	if err == nil && current == value {
+		// Nothing to update
+		return nil
+	}
+
+	return ioutil.WriteFile(fmt.Sprintf("/proc/sys/net/%s", path), []byte(value), 0)
+}
diff --git a/lxd/networks.go b/lxd/networks.go
index f80175215a..85760cd749 100644
--- a/lxd/networks.go
+++ b/lxd/networks.go
@@ -20,6 +20,7 @@ import (
 	lxd "github.com/lxc/lxd/client"
 	"github.com/lxc/lxd/lxd/cluster"
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/device"
 	"github.com/lxc/lxd/lxd/dnsmasq"
 	"github.com/lxc/lxd/lxd/iptables"
 	"github.com/lxc/lxd/lxd/node"
@@ -1083,12 +1084,12 @@ func (n *network) Start() error {
 
 	// IPv6 bridge configuration
 	if !shared.StringInSlice(n.config["ipv6.address"], []string{"", "none"}) {
-		err := networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/autoconf", n.name), "0")
+		err := device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/autoconf", n.name), "0")
 		if err != nil {
 			return err
 		}
 
-		err = networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_dad", n.name), "0")
+		err = device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_dad", n.name), "0")
 		if err != nil {
 			return err
 		}
@@ -1252,7 +1253,7 @@ func (n *network) Start() error {
 
 		// Allow forwarding
 		if n.config["bridge.mode"] == "fan" || n.config["ipv4.routing"] == "" || shared.IsTrue(n.config["ipv4.routing"]) {
-			err = networkSysctlSet("ipv4/ip_forward", "1")
+			err = device.NetworkSysctlSet("ipv4/ip_forward", "1")
 			if err != nil {
 				return err
 			}
@@ -1418,7 +1419,7 @@ func (n *network) Start() error {
 	// Configure IPv6
 	if !shared.StringInSlice(n.config["ipv6.address"], []string{"", "none"}) {
 		// Enable IPv6 for the subnet
-		err := networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/disable_ipv6", n.name), "0")
+		err := device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/disable_ipv6", n.name), "0")
 		if err != nil {
 			return err
 		}
@@ -1490,7 +1491,7 @@ func (n *network) Start() error {
 					continue
 				}
 
-				err = networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", entry.Name()), "2")
+				err = device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", entry.Name()), "2")
 				if err != nil && !os.IsNotExist(err) {
 					return err
 				}
@@ -1498,7 +1499,7 @@ func (n *network) Start() error {
 
 			// Then set forwarding for all of them
 			for _, entry := range entries {
-				err = networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/forwarding", entry.Name()), "1")
+				err = device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/forwarding", entry.Name()), "1")
 				if err != nil && !os.IsNotExist(err) {
 					return err
 				}
diff --git a/lxd/networks_utils.go b/lxd/networks_utils.go
index 6f4a4ce43c..a0cf562c9b 100644
--- a/lxd/networks_utils.go
+++ b/lxd/networks_utils.go
@@ -27,7 +27,6 @@ import (
 
 	"github.com/lxc/lxd/lxd/cluster"
 	"github.com/lxc/lxd/lxd/db"
-	"github.com/lxc/lxd/lxd/device"
 	"github.com/lxc/lxd/lxd/dnsmasq"
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/state"
@@ -1274,17 +1273,6 @@ func networksGetForkdnsServersList(networkName string) ([]string, error) {
 	return servers, nil
 }
 
-func networkSysctlSet(path string, value string) error {
-	// Get current value
-	current, err := device.NetworkSysctlGet(path)
-	if err == nil && current == value {
-		// Nothing to update
-		return nil
-	}
-
-	return ioutil.WriteFile(fmt.Sprintf("/proc/sys/net/%s", path), []byte(value), 0)
-}
-
 func networkGetMacSlice(hwaddr string) []string {
 	var buf []string
 

From e14a9ab407a7f71b26a84488fd1908c6f5fc5e68 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:21:17 +0100
Subject: [PATCH 3/7] device/utils: Moves networkGetDevMTU and networkSetDevMTU
 to device_utils

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       |  4 ++--
 lxd/device/device_utils.go | 38 ++++++++++++++++++++++++++++++++++++++
 lxd/networks.go            |  2 +-
 lxd/networks_utils.go      | 34 ----------------------------------
 4 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index e89aaad197..14d7a5dcf7 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2651,7 +2651,7 @@ func (c *containerLXC) snapshotPhysicalNic(deviceName string, hostName string, v
 	macKey := "volatile." + deviceName + ".last_state.hwaddr"
 
 	// Store current MTU for restoration on detach
-	mtu, err := networkGetDevMTU(hostName)
+	mtu, err := device.NetworkGetDevMTU(hostName)
 	if err != nil {
 		return err
 	}
@@ -2779,7 +2779,7 @@ func (c *containerLXC) restorePhysicalNic(deviceName string, hostName string) er
 			return fmt.Errorf("Failed to convert mtu for \"%s\" mtu \"%s\": %v", hostName, c.localConfig[mtuKey], err)
 		}
 
-		err = networkSetDevMTU(hostName, mtuInt)
+		err = device.NetworkSetDevMTU(hostName, mtuInt)
 		if err != nil {
 			return fmt.Errorf("Failed to restore physical dev \"%s\" mtu to \"%d\": %v", hostName, mtuInt, err)
 		}
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
index 94c618900e..091ef538d9 100644
--- a/lxd/device/device_utils.go
+++ b/lxd/device/device_utils.go
@@ -3,6 +3,10 @@ package device
 import (
 	"fmt"
 	"io/ioutil"
+	"strconv"
+	"strings"
+
+	"github.com/lxc/lxd/shared"
 )
 
 // NetworkSysctlGet retrieves the value of a sysctl file in /proc/sys/net.
@@ -27,3 +31,37 @@ func NetworkSysctlSet(path string, value string) error {
 
 	return ioutil.WriteFile(fmt.Sprintf("/proc/sys/net/%s", path), []byte(value), 0)
 }
+
+// NetworkGetDevMTU retrieves the current MTU setting for a named network device.
+func NetworkGetDevMTU(devName string) (uint64, error) {
+	content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/mtu", devName))
+	if err != nil {
+		return 0, err
+	}
+
+	// Parse value
+	mtu, err := strconv.ParseUint(strings.TrimSpace(string(content)), 10, 32)
+	if err != nil {
+		return 0, err
+	}
+
+	return mtu, nil
+}
+
+// NetworkSetDevMTU sets the MTU setting for a named network device if different from current.
+func NetworkSetDevMTU(devName string, mtu uint64) error {
+	curMTU, err := NetworkGetDevMTU(devName)
+	if err != nil {
+		return err
+	}
+
+	// Only try and change the MTU if the requested mac is different to current one.
+	if curMTU != mtu {
+		_, err := shared.RunCommand("ip", "link", "set", "dev", devName, "mtu", fmt.Sprintf("%d", mtu))
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
diff --git a/lxd/networks.go b/lxd/networks.go
index 85760cd749..5c99f77ee4 100644
--- a/lxd/networks.go
+++ b/lxd/networks.go
@@ -1611,7 +1611,7 @@ func (n *network) Start() error {
 		}
 
 		// Update the MTU based on overlay device (if available)
-		fanMtuInt, err := networkGetDevMTU(devName)
+		fanMtuInt, err := device.NetworkGetDevMTU(devName)
 		if err == nil {
 			// Apply overhead
 			if n.config["fan.type"] == "ipip" {
diff --git a/lxd/networks_utils.go b/lxd/networks_utils.go
index a0cf562c9b..a4bfb3058b 100644
--- a/lxd/networks_utils.go
+++ b/lxd/networks_utils.go
@@ -1482,40 +1482,6 @@ func networkGetState(netIf net.Interface) api.NetworkState {
 	return network
 }
 
-// networkGetDevMTU retrieves the current MTU setting for a named network device.
-func networkGetDevMTU(devName string) (uint64, error) {
-	content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/mtu", devName))
-	if err != nil {
-		return 0, err
-	}
-
-	// Parse value
-	mtu, err := strconv.ParseUint(strings.TrimSpace(string(content)), 10, 32)
-	if err != nil {
-		return 0, err
-	}
-
-	return mtu, nil
-}
-
-// networkSetDevMTU sets the MTU setting for a named network device if different from current.
-func networkSetDevMTU(devName string, mtu uint64) error {
-	curMTU, err := networkGetDevMTU(devName)
-	if err != nil {
-		return err
-	}
-
-	// Only try and change the MTU if the requested mac is different to current one.
-	if curMTU != mtu {
-		_, err := shared.RunCommand("ip", "link", "set", "dev", devName, "mtu", fmt.Sprintf("%d", mtu))
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
-}
-
 // networkGetDevMAC retrieves the current MAC setting for a named network device.
 func networkGetDevMAC(devName string) (string, error) {
 	content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/address", devName))

From b770c677d671872a4b267eabe77ccbdda965dc60 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:23:15 +0100
Subject: [PATCH 4/7] device/utils: Moves networkGetDevMAC and networkSetDevMAC
 to device_utils

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       |  4 ++--
 lxd/device/device_utils.go | 28 ++++++++++++++++++++++++++++
 lxd/networks_utils.go      | 28 ----------------------------
 3 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 14d7a5dcf7..13d88efe6c 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2658,7 +2658,7 @@ func (c *containerLXC) snapshotPhysicalNic(deviceName string, hostName string, v
 	volatile[mtuKey] = fmt.Sprintf("%d", mtu)
 
 	// Store current MAC for restoration on detach
-	mac, err := networkGetDevMAC(hostName)
+	mac, err := device.NetworkGetDevMAC(hostName)
 	if err != nil {
 		return err
 	}
@@ -2787,7 +2787,7 @@ func (c *containerLXC) restorePhysicalNic(deviceName string, hostName string) er
 
 	// If MAC value is specified then there is an original MAC that needs restoring.
 	if c.localConfig[macKey] != "" {
-		err := networkSetDevMAC(hostName, c.localConfig[macKey])
+		err := device.NetworkSetDevMAC(hostName, c.localConfig[macKey])
 		if err != nil {
 			return fmt.Errorf("Failed to restore physical dev \"%s\" mac to \"%s\": %v", hostName, c.localConfig[macKey], err)
 		}
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
index 091ef538d9..adcf801880 100644
--- a/lxd/device/device_utils.go
+++ b/lxd/device/device_utils.go
@@ -65,3 +65,31 @@ func NetworkSetDevMTU(devName string, mtu uint64) error {
 
 	return nil
 }
+
+// NetworkGetDevMAC retrieves the current MAC setting for a named network device.
+func NetworkGetDevMAC(devName string) (string, error) {
+	content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/address", devName))
+	if err != nil {
+		return "", err
+	}
+
+	return strings.TrimSpace(fmt.Sprintf("%s", content)), nil
+}
+
+// NetworkSetDevMAC sets the MAC setting for a named network device if different from current.
+func NetworkSetDevMAC(devName string, mac string) error {
+	curMac, err := NetworkGetDevMAC(devName)
+	if err != nil {
+		return err
+	}
+
+	// Only try and change the MAC if the requested mac is different to current one.
+	if curMac != mac {
+		_, err := shared.RunCommand("ip", "link", "set", "dev", devName, "address", mac)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
diff --git a/lxd/networks_utils.go b/lxd/networks_utils.go
index a4bfb3058b..63371b674e 100644
--- a/lxd/networks_utils.go
+++ b/lxd/networks_utils.go
@@ -1482,34 +1482,6 @@ func networkGetState(netIf net.Interface) api.NetworkState {
 	return network
 }
 
-// networkGetDevMAC retrieves the current MAC setting for a named network device.
-func networkGetDevMAC(devName string) (string, error) {
-	content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/address", devName))
-	if err != nil {
-		return "", err
-	}
-
-	return strings.TrimSpace(fmt.Sprintf("%s", content)), nil
-}
-
-// networkSetDevMAC sets the MAC setting for a named network device if different from current.
-func networkSetDevMAC(devName string, mac string) error {
-	curMac, err := networkGetDevMAC(devName)
-	if err != nil {
-		return err
-	}
-
-	// Only try and change the MAC if the requested mac is different to current one.
-	if curMac != mac {
-		_, err := shared.RunCommand("ip", "link", "set", "dev", devName, "address", mac)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
-}
-
 // networkListBootRoutesV4 returns a list of IPv4 boot routes on a named network device.
 func networkListBootRoutesV4(devName string) ([]string, error) {
 	routes := []string{}

From 1c2ac7f066a8d6c3d81e07bd9a8c35ffd6ed0c05 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:27:03 +0100
Subject: [PATCH 5/7] device/utis: Moves networkGetHostDevice to device_utils

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       |  8 +++----
 lxd/device/device_utils.go | 44 ++++++++++++++++++++++++++++++++++++++
 lxd/networks_utils.go      | 43 ++-----------------------------------
 3 files changed, 50 insertions(+), 45 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 13d88efe6c..43f42bbb2b 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1715,7 +1715,7 @@ func (c *containerLXC) initLXC(config bool) error {
 					return err
 				}
 			} else if shared.StringInSlice(m["nictype"], []string{"macvlan", "ipvlan", "physical"}) {
-				err = lxcSetConfigItem(cc, fmt.Sprintf("%s.%d.link", networkKeyPrefix, networkidx), networkGetHostDevice(m["parent"], m["vlan"]))
+				err = lxcSetConfigItem(cc, fmt.Sprintf("%s.%d.link", networkKeyPrefix, networkidx), device.NetworkGetHostDevice(m["parent"], m["vlan"]))
 				if err != nil {
 					return err
 				}
@@ -2697,7 +2697,7 @@ func (c *containerLXC) setupPhysicalParent(deviceName string, m types.Device) (s
 		return "", errors.New("No parent property on device")
 	}
 
-	hostName := networkGetHostDevice(m["parent"], m["vlan"])
+	hostName := device.NetworkGetHostDevice(m["parent"], m["vlan"])
 	createdDev, err := c.createVlanDeviceIfNeeded(m, hostName)
 	if err != nil {
 		return hostName, err
@@ -2813,7 +2813,7 @@ func (c *containerLXC) restorePhysicalParent(deviceName string, m types.Device)
 	}()
 
 	// Nothing to do if we don't know the original device name.
-	hostName := networkGetHostDevice(m["parent"], m["vlan"])
+	hostName := device.NetworkGetHostDevice(m["parent"], m["vlan"])
 	if hostName == "" {
 		return
 	}
@@ -9144,7 +9144,7 @@ func (c *containerLXC) removeNetworkDevice(name string, m types.Device) error {
 	// Get a temporary device name
 	var hostName string
 	if m["nictype"] == "physical" {
-		hostName = networkGetHostDevice(m["parent"], m["vlan"])
+		hostName = device.NetworkGetHostDevice(m["parent"], m["vlan"])
 	} else if m["nictype"] == "sriov" {
 		// hostName for sriov devices can change on each boot, so get out of volatile.
 		hostName = c.getVolatileHostName(name)
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
index adcf801880..f25f904c37 100644
--- a/lxd/device/device_utils.go
+++ b/lxd/device/device_utils.go
@@ -1,8 +1,10 @@
 package device
 
 import (
+	"bufio"
 	"fmt"
 	"io/ioutil"
+	"os"
 	"strconv"
 	"strings"
 
@@ -93,3 +95,45 @@ func NetworkSetDevMAC(devName string, mac string) error {
 
 	return nil
 }
+
+// NetworkGetHostDevice figures out whether there is an existing interface for the supplied
+// parent device and VLAN ID and returns it. Otherwise just returns the parent device name.
+func NetworkGetHostDevice(parent string, vlan string) string {
+	// If no VLAN, just use the raw device
+	if vlan == "" {
+		return parent
+	}
+
+	// If no VLANs are configured, use the default pattern
+	defaultVlan := fmt.Sprintf("%s.%s", parent, vlan)
+	if !shared.PathExists("/proc/net/vlan/config") {
+		return defaultVlan
+	}
+
+	// Look for an existing VLAN
+	f, err := os.Open("/proc/net/vlan/config")
+	if err != nil {
+		return defaultVlan
+	}
+	defer f.Close()
+
+	scanner := bufio.NewScanner(f)
+	for scanner.Scan() {
+		// Only grab the lines we're interested in
+		s := strings.Split(scanner.Text(), "|")
+		if len(s) != 3 {
+			continue
+		}
+
+		vlanIface := strings.TrimSpace(s[0])
+		vlanID := strings.TrimSpace(s[1])
+		vlanParent := strings.TrimSpace(s[2])
+
+		if vlanParent == parent && vlanID == vlan {
+			return vlanIface
+		}
+	}
+
+	// Return the default pattern
+	return defaultVlan
+}
diff --git a/lxd/networks_utils.go b/lxd/networks_utils.go
index 63371b674e..193b3ca375 100644
--- a/lxd/networks_utils.go
+++ b/lxd/networks_utils.go
@@ -27,6 +27,7 @@ import (
 
 	"github.com/lxc/lxd/lxd/cluster"
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/device"
 	"github.com/lxc/lxd/lxd/dnsmasq"
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/state"
@@ -130,7 +131,7 @@ func networkIsInUse(c container, name string) bool {
 			continue
 		}
 
-		if networkGetHostDevice(d["parent"], d["vlan"]) == name {
+		if device.NetworkGetHostDevice(d["parent"], d["vlan"]) == name {
 			return true
 		}
 	}
@@ -138,46 +139,6 @@ func networkIsInUse(c container, name string) bool {
 	return false
 }
 
-func networkGetHostDevice(parent string, vlan string) string {
-	// If no VLAN, just use the raw device
-	if vlan == "" {
-		return parent
-	}
-
-	// If no VLANs are configured, use the default pattern
-	defaultVlan := fmt.Sprintf("%s.%s", parent, vlan)
-	if !shared.PathExists("/proc/net/vlan/config") {
-		return defaultVlan
-	}
-
-	// Look for an existing VLAN
-	f, err := os.Open("/proc/net/vlan/config")
-	if err != nil {
-		return defaultVlan
-	}
-	defer f.Close()
-
-	scanner := bufio.NewScanner(f)
-	for scanner.Scan() {
-		// Only grab the lines we're interested in
-		s := strings.Split(scanner.Text(), "|")
-		if len(s) != 3 {
-			continue
-		}
-
-		vlanIface := strings.TrimSpace(s[0])
-		vlanId := strings.TrimSpace(s[1])
-		vlanParent := strings.TrimSpace(s[2])
-
-		if vlanParent == parent && vlanId == vlan {
-			return vlanIface
-		}
-	}
-
-	// Return the default pattern
-	return defaultVlan
-}
-
 func networkGetIP(subnet *net.IPNet, host int64) net.IP {
 	// Convert IP to a big int
 	bigIP := big.NewInt(0)

From 398f492b5f9648eeb5f0111b8f0bcb8bdf3b526c Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:30:20 +0100
Subject: [PATCH 6/7] device/utils: Renames deviceRemoveInterface to
 NetworkRemoveInterface

and moves to device_utils.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       | 12 ++++++------
 lxd/device/device_utils.go |  6 ++++++
 lxd/devices.go             |  5 -----
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 43f42bbb2b..b67aab5710 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2763,7 +2763,7 @@ func (c *containerLXC) restorePhysicalNic(deviceName string, hostName string) er
 
 	// If we created the "physical" device and then it should be removed.
 	if shared.IsTrue(c.localConfig[createdKey]) {
-		return deviceRemoveInterface(hostName)
+		return device.NetworkRemoveInterface(hostName)
 	}
 
 	// Bring the interface down, as this is sometimes needed to change settings on the nic.
@@ -8313,7 +8313,7 @@ func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string,
 		if m["nictype"] == "bridged" {
 			err = networkAttachInterface(m["parent"], n1)
 			if err != nil {
-				deviceRemoveInterface(n2)
+				device.NetworkRemoveInterface(n2)
 				return "", fmt.Errorf("Failed to add interface to bridge: %s", err)
 			}
 
@@ -8356,7 +8356,7 @@ func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string,
 	if m["hwaddr"] != "" {
 		_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "address", m["hwaddr"])
 		if err != nil {
-			deviceRemoveInterface(dev)
+			device.NetworkRemoveInterface(dev)
 			return "", fmt.Errorf("Failed to set the MAC address: %s", err)
 		}
 	}
@@ -8365,7 +8365,7 @@ func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string,
 	if m["mtu"] != "" {
 		_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "mtu", m["mtu"])
 		if err != nil {
-			deviceRemoveInterface(dev)
+			device.NetworkRemoveInterface(dev)
 			return "", fmt.Errorf("Failed to set the MTU: %s", err)
 		}
 	}
@@ -8373,7 +8373,7 @@ func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string,
 	// Bring the interface up
 	_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "up")
 	if err != nil {
-		deviceRemoveInterface(dev)
+		device.NetworkRemoveInterface(dev)
 		return "", fmt.Errorf("Failed to bring up the interface: %s", err)
 	}
 
@@ -9177,7 +9177,7 @@ func (c *containerLXC) removeNetworkDevice(name string, m types.Device) error {
 	// Remove host side veth settings and remove veth interface
 	if shared.StringInSlice(m["nictype"], []string{"bridged", "p2p"}) {
 		c.cleanupHostVethDevice(name, m)
-		deviceRemoveInterface(hostName)
+		device.NetworkRemoveInterface(hostName)
 	}
 
 	// If physical dev, restore MTU using value recorded on parent after removal from container.
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
index f25f904c37..3d9d6afe37 100644
--- a/lxd/device/device_utils.go
+++ b/lxd/device/device_utils.go
@@ -137,3 +137,9 @@ func NetworkGetHostDevice(parent string, vlan string) string {
 	// Return the default pattern
 	return defaultVlan
 }
+
+// NetworkRemoveInterface removes a network interface by name.
+func NetworkRemoveInterface(nic string) error {
+	_, err := shared.RunCommand("ip", "link", "del", "dev", nic)
+	return err
+}
diff --git a/lxd/devices.go b/lxd/devices.go
index 10d00f6e07..7535357640 100644
--- a/lxd/devices.go
+++ b/lxd/devices.go
@@ -1181,11 +1181,6 @@ func deviceNextVeth() string {
 	return "veth" + hex.EncodeToString(randBytes)
 }
 
-func deviceRemoveInterface(nic string) error {
-	_, err := shared.RunCommand("ip", "link", "del", "dev", nic)
-	return err
-}
-
 func deviceMountDisk(srcPath string, dstPath string, readonly bool, recursive bool, propagation string) error {
 	var err error
 

From 93dc5c28860cb3252e722af4da063056dbb64e9e Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 15 Jul 2019 12:35:15 +0100
Subject: [PATCH 7/7] device/utils: Renames createVlanDeviceIfNeeded to
 NetworkCreateVlanDeviceIfNeeded

and moves into device_utils.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container_lxc.go       | 24 +-----------------------
 lxd/device/device_utils.go | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index b67aab5710..11bfa50304 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2667,28 +2667,6 @@ func (c *containerLXC) snapshotPhysicalNic(deviceName string, hostName string, v
 	return nil
 }
 
-// createVlanDeviceIfNeeded detects the device type and whether it has a vlan parent configured.
-// It then attempts to detect if the parent vlan interface exists, and if not creates one.
-// Returns boolean as to whether we created it and any errors.
-func (c *containerLXC) createVlanDeviceIfNeeded(m types.Device, hostName string) (bool, error) {
-	if m["vlan"] != "" {
-		if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", hostName)) {
-			_, err := shared.RunCommand("ip", "link", "add", "link", m["parent"], "name", hostName, "up", "type", "vlan", "id", m["vlan"])
-			if err != nil {
-				return false, err
-			}
-
-			// Attempt to disable IPv6 router advertisement acceptance
-			device.NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", hostName), "0")
-
-			// We created a new vlan interface, return true
-			return true, nil
-		}
-	}
-
-	return false, nil
-}
-
 // setupPhysicalParent creates a VLAN device on parent if needed and tracks original properties of
 // the physical device if not just created so they can be restored when the device is detached.
 // Returns the parent device name detected.
@@ -2698,7 +2676,7 @@ func (c *containerLXC) setupPhysicalParent(deviceName string, m types.Device) (s
 	}
 
 	hostName := device.NetworkGetHostDevice(m["parent"], m["vlan"])
-	createdDev, err := c.createVlanDeviceIfNeeded(m, hostName)
+	createdDev, err := device.NetworkCreateVlanDeviceIfNeeded(m["parent"], hostName, m["vlan"])
 	if err != nil {
 		return hostName, err
 	}
diff --git a/lxd/device/device_utils.go b/lxd/device/device_utils.go
index 3d9d6afe37..89ce186fe4 100644
--- a/lxd/device/device_utils.go
+++ b/lxd/device/device_utils.go
@@ -143,3 +143,23 @@ func NetworkRemoveInterface(nic string) error {
 	_, err := shared.RunCommand("ip", "link", "del", "dev", nic)
 	return err
 }
+
+// NetworkCreateVlanDeviceIfNeeded creates a VLAN device if doesn't already exist.
+func NetworkCreateVlanDeviceIfNeeded(parent string, hostName string, vlan string) (bool, error) {
+	if vlan != "" {
+		if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", hostName)) {
+			_, err := shared.RunCommand("ip", "link", "add", "link", parent, "name", hostName, "up", "type", "vlan", "id", vlan)
+			if err != nil {
+				return false, err
+			}
+
+			// Attempt to disable IPv6 router advertisement acceptance
+			NetworkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", hostName), "0")
+
+			// We created a new vlan interface, return true
+			return true, nil
+		}
+	}
+
+	return false, nil
+}


More information about the lxc-devel mailing list