The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/7958
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === - Adds `device.NICState` interface for detecting and retrieving NIC state from devices that support it. - Updates `qemu.RenderState()` to use the `NICState` interface. - Implements the `NICState` interface on `bridged` NIC type.
From 69f8c14dcfe6fa92fa72aa306a3f6f36f5e2d1b1 Mon Sep 17 00:00:00 2001 From: Thomas Parrott <thomas.parr...@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.parr...@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.parr...@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.parr...@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.parr...@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.parr...@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 } }
_______________________________________________ lxc-devel mailing list lxc-devel@lists.linuxcontainers.org http://lists.linuxcontainers.org/listinfo/lxc-devel