[lxc-devel] [lxd/master] Network: OVN fallback state

tomponline on Github lxc-bot at linuxcontainers.org
Wed Sep 30 15:48:28 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 531 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200930/ae00ad6f/attachment.bin>
-------------- next part --------------
From 69f8c14dcfe6fa92fa72aa306a3f6f36f5e2d1b1 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 30 Sep 2020 16:37:50 +0100
Subject: [PATCH 1/3] lxd/device/device/interface: Adds NICState interface for
 getting NIC state

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

diff --git a/lxd/device/device_interface.go b/lxd/device/device_interface.go
index 52fc2edf89..736da6fdc7 100644
--- a/lxd/device/device_interface.go
+++ b/lxd/device/device_interface.go
@@ -4,6 +4,7 @@ import (
 	deviceConfig "github.com/lxc/lxd/lxd/device/config"
 	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/state"
+	"github.com/lxc/lxd/shared/api"
 )
 
 // VolatileSetter is a function that accepts one or more key/value strings to save into the LXD
@@ -64,3 +65,8 @@ type device interface {
 	// validateConfig checks Config stored by init() is valid for the instance type.
 	validateConfig(instance.ConfigReader) error
 }
+
+// NICState provides the ability to access NIC state.
+type NICState interface {
+	State() (*api.InstanceStateNetwork, error)
+}

From 083134c9581e5c6c2663cca7c27b22bee8b5d156 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 30 Sep 2020 16:39:10 +0100
Subject: [PATCH 2/3] lxd/device/nic/bridged: Implements NICState interface by
 adding State function

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

diff --git a/lxd/device/nic_bridged.go b/lxd/device/nic_bridged.go
index 86f0447838..f8a255e4d8 100644
--- a/lxd/device/nic_bridged.go
+++ b/lxd/device/nic_bridged.go
@@ -1083,3 +1083,51 @@ func (d *nicBridged) setupOVSBridgePortVLANs(hostName string) error {
 
 	return nil
 }
+
+// State gets the state of a bridged NIC by parsing the local DHCP server leases file.
+func (d *nicBridged) State() (*api.InstanceStateNetwork, error) {
+	v := d.volatileGet()
+
+	// Populate device config with volatile fields if needed.
+	networkVethFillFromVolatile(d.config, v)
+
+	if d.config["hwaddr"] == "" {
+		return nil, nil
+	}
+
+	// Parse the leases file.
+	addresses, err := network.GetLeaseAddresses(d.state, d.config["parent"], d.config["hwaddr"])
+	if err != nil {
+		return nil, err
+	}
+
+	if len(addresses) == 0 {
+		return nil, nil
+	}
+
+	// Get MTU.
+	iface, err := net.InterfaceByName(d.config["parent"])
+	if err != nil {
+		return nil, err
+	}
+
+	// Retrieve the host counters, as we report the values from the instance's point of view,
+	// those counters need to be reversed below.
+	hostCounters := shared.NetworkGetCounters(d.config["host_name"])
+	network := api.InstanceStateNetwork{
+		Addresses: addresses,
+		Counters: api.InstanceStateNetworkCounters{
+			BytesReceived:   hostCounters.BytesSent,
+			BytesSent:       hostCounters.BytesReceived,
+			PacketsReceived: hostCounters.PacketsSent,
+			PacketsSent:     hostCounters.PacketsReceived,
+		},
+		Hwaddr:   d.config["hwaddr"],
+		HostName: d.config["host_name"],
+		Mtu:      iface.MTU,
+		State:    "up",
+		Type:     "broadcast",
+	}
+
+	return &network, nil
+}

From 460939570b7e6ead88546ccc4163d769871f9d54 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 30 Sep 2020 16:01:56 +0100
Subject: [PATCH 3/3] lxd/instance/drivers/driver/qemu: Refactors RenderState
 to support multiple NIC types in the future

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 54 ++++++-----------------------
 1 file changed, 11 insertions(+), 43 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index 3fed78efcb..b7c0360223 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -4089,61 +4089,29 @@ func (vm *qemu) RenderState() (*api.InstanceState, error) {
 			status.Processes = -1
 			networks := map[string]api.InstanceStateNetwork{}
 			for k, m := range vm.ExpandedDevices() {
-				nicType, err := nictype.NICType(vm.state, vm.Project(), m)
-				if err != nil {
-					return nil, err
-				}
-
-				// We only care about bridged nics as these can use a local DHCP server that allows
-				// us to parse the leases file below.
-				if m["type"] != "nic" || nicType != "bridged" {
+				if m["type"] != "nic" {
 					continue
 				}
 
-				// Fill the MAC address.
-				m, err := vm.FillNetworkDevice(k, m)
+				d, _, err := vm.deviceLoad(k, m)
 				if err != nil {
-					return nil, err
-				}
-
-				// Temporarily populate parent from network setting if used.
-				if m["network"] != "" {
-					m["parent"] = m["network"]
-				}
-
-				// Parse the lease file.
-				addresses, err := network.GetLeaseAddresses(vm.state, m["parent"], m["hwaddr"])
-				if err != nil {
-					return nil, err
+					logger.Warn("Could not load device", log.Ctx{"project": vm.Project(), "instance": vm.Name(), "device": k, "err": err})
+					continue
 				}
 
-				if len(addresses) == 0 {
+				// Only some NIC types support fallback state mechanisms when there is no agent.
+				nic, ok := d.(device.NICState)
+				if !ok {
 					continue
 				}
 
-				// Get MTU.
-				iface, err := net.InterfaceByName(m["parent"])
+				network, err := nic.State()
 				if err != nil {
-					return nil, err
+					return nil, errors.Wrapf(err, "Failed getting NIC state for %q", k)
 				}
 
-				// Retrieve the host counters, as we report the values
-				// from the instance's point of view, those counters need to be reversed below.
-				hostCounters := shared.NetworkGetCounters(m["host_name"])
-
-				networks[k] = api.InstanceStateNetwork{
-					Addresses: addresses,
-					Counters: api.InstanceStateNetworkCounters{
-						BytesReceived:   hostCounters.BytesSent,
-						BytesSent:       hostCounters.BytesReceived,
-						PacketsReceived: hostCounters.PacketsSent,
-						PacketsSent:     hostCounters.PacketsReceived,
-					},
-					Hwaddr:   m["hwaddr"],
-					HostName: m["host_name"],
-					Mtu:      iface.MTU,
-					State:    "up",
-					Type:     "broadcast",
+				if network != nil {
+					networks[k] = *network
 				}
 			}
 


More information about the lxc-devel mailing list