[lxc-devel] [lxd/master] Add FirmwareVersion for network cards

stgraber on Github lxc-bot at linuxcontainers.org
Mon Oct 7 15:32:14 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20191007/8ab11b55/attachment.bin>
-------------- next part --------------
From 8cff6f4a05b44e0a73c49998b576f0bc96aeb837 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 7 Oct 2019 11:31:07 -0400
Subject: [PATCH 1/3] api: Add resources_network_firmware extension
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 doc/api-extensions.md | 3 +++
 shared/version/api.go | 1 +
 2 files changed, 4 insertions(+)

diff --git a/doc/api-extensions.md b/doc/api-extensions.md
index 6265130eb3..57b73118b7 100644
--- a/doc/api-extensions.md
+++ b/doc/api-extensions.md
@@ -845,3 +845,6 @@ roles that the member serves in the cluster.
 
 ## images\_expiry
 This allows for editing of the expiry date on images.
+
+## resources\_network\_firmware
+Adds a FirmwareVersion field to network card entries.
diff --git a/shared/version/api.go b/shared/version/api.go
index 90ec0a95f6..dd118706e4 100644
--- a/shared/version/api.go
+++ b/shared/version/api.go
@@ -169,6 +169,7 @@ var APIExtensions = []string{
 	"resources_disk_sata",
 	"clustering_roles",
 	"images_expiry",
+	"resources_network_firmware",
 }
 
 // APIExtensionsCount returns the number of available API extensions.

From f2bd6bd839f46ab17616f90004bffc5f8547c26d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 7 Oct 2019 11:31:26 -0400
Subject: [PATCH 2/3] shared/api: Add FirmwareVersion to ResourcesNetworkCard
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 shared/api/resource.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/shared/api/resource.go b/shared/api/resource.go
index 815e5a4b09..f2a9619d15 100644
--- a/shared/api/resource.go
+++ b/shared/api/resource.go
@@ -155,6 +155,9 @@ type ResourcesNetworkCard struct {
 	VendorID  string `json:"vendor_id,omitempty" yaml:"vendor_id,omitempty"`
 	Product   string `json:"product,omitempty" yaml:"product,omitempty"`
 	ProductID string `json:"product_id,omitempty" yaml:"product_id,omitempty"`
+
+	// API extension: resources_network_firmware
+	FirmwareVersion string `json:"firmware_version,omitempty" yaml:"firmware_version,omitempty"`
 }
 
 // ResourcesNetworkCardPort represents a network port on the system

From f4b364015c190698c75ac1c9a9f457057fb33db3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 7 Oct 2019 11:31:45 -0400
Subject: [PATCH 3/3] lxd/resources/network: Add FirmwareVersion retrieval
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/resources/network.go         |  6 ++++-
 lxd/resources/network_ethtool.go | 44 +++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/lxd/resources/network.go b/lxd/resources/network.go
index 09d40f7e5c..96330c823c 100644
--- a/lxd/resources/network.go
+++ b/lxd/resources/network.go
@@ -242,10 +242,14 @@ func networkAddDeviceInfo(devicePath string, pciDB *pcidb.PCIDB, uname unix.Utsn
 				continue
 			}
 
-			ethtoolAddInfo(info)
+			ethtoolAddPortInfo(info)
 
 			card.Ports = append(card.Ports, *info)
 		}
+
+		if len(card.Ports) > 0 {
+			ethtoolAddCardInfo(card.Ports[0].ID, card)
+		}
 	}
 
 	return nil
diff --git a/lxd/resources/network_ethtool.go b/lxd/resources/network_ethtool.go
index f0a733079e..1ff8ed5d44 100644
--- a/lxd/resources/network_ethtool.go
+++ b/lxd/resources/network_ethtool.go
@@ -1,6 +1,7 @@
 package resources
 
 import (
+	"bytes"
 	"unsafe"
 
 	"github.com/pkg/errors"
@@ -90,12 +91,53 @@ type ethtoolCmd struct {
 	reserved      [2]uint32
 }
 
+type ethtoolDrvInfo struct {
+	cmd         uint32
+	driver      [32]byte
+	version     [32]byte
+	fwVersion   [32]byte
+	busInfo     [32]byte
+	reserved1   [32]byte
+	reserved2   [16]byte
+	nStats      uint32
+	testinfoLen uint32
+	eedumpLen   uint32
+	regDumpLen  uint32
+}
+
 type ethtoolValue struct {
 	cmd  uint32
 	data uint32
 }
 
-func ethtoolAddInfo(info *api.ResourcesNetworkCardPort) error {
+func ethtoolAddCardInfo(name string, info *api.ResourcesNetworkCard) error {
+	// Open FD
+	ethtoolFd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
+	if err != nil {
+		return errors.Wrap(err, "Failed to open IPPROTO_IP socket")
+	}
+	defer unix.Close(ethtoolFd)
+
+	// Driver info
+	ethDrvInfo := ethtoolDrvInfo{
+		cmd: 0x00000003,
+	}
+	req := ethtoolReq{
+		data: uintptr(unsafe.Pointer(&ethDrvInfo)),
+	}
+	copy(req.name[:], []byte(name))
+
+	_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(ethtoolFd), unix.SIOCETHTOOL, uintptr(unsafe.Pointer(&req)))
+	if errno != 0 {
+		return errors.Wrap(unix.Errno(errno), "Failed to ETHTOOL_GDRVINFO")
+	}
+
+	info.FirmwareVersion = string(bytes.Trim(ethDrvInfo.fwVersion[:], "\x00"))
+
+	return nil
+}
+
+func ethtoolAddPortInfo(info *api.ResourcesNetworkCardPort) error {
 	// Open FD
 	ethtoolFd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
 	if err != nil {


More information about the lxc-devel mailing list