[lxc-devel] [lxd/master] lxd/resources: Use permanent MAC when available

stgraber on Github lxc-bot at linuxcontainers.org
Sat May 2 03:05:52 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200501/0f0e533f/attachment.bin>
-------------- next part --------------
From 6df3188088ce92474f6f2c370013ab7955501785 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 1 May 2020 23:05:35 -0400
Subject: [PATCH] lxd/resources: Use permanent MAC when available
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         |  2 +-
 lxd/resources/network_ethtool.go | 32 ++++++++++++++++++++++++++------
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/lxd/resources/network.go b/lxd/resources/network.go
index 96330c823c..fb3d6a1a3c 100644
--- a/lxd/resources/network.go
+++ b/lxd/resources/network.go
@@ -144,7 +144,7 @@ func networkAddDeviceInfo(devicePath string, pciDB *pcidb.PCIDB, uname unix.Utsn
 			}
 
 			// Add MAC address
-			if sysfsExists(filepath.Join(interfacePath, "address")) {
+			if info.Address == "" && sysfsExists(filepath.Join(interfacePath, "address")) {
 				address, err := ioutil.ReadFile(filepath.Join(interfacePath, "address"))
 				if err != nil {
 					return errors.Wrapf(err, "Failed to read \"%s\"", filepath.Join(interfacePath, "address"))
diff --git a/lxd/resources/network_ethtool.go b/lxd/resources/network_ethtool.go
index 1ff8ed5d44..ce3d00fb52 100644
--- a/lxd/resources/network_ethtool.go
+++ b/lxd/resources/network_ethtool.go
@@ -2,6 +2,7 @@ package resources
 
 import (
 	"bytes"
+	"net"
 	"unsafe"
 
 	"github.com/pkg/errors"
@@ -105,6 +106,12 @@ type ethtoolDrvInfo struct {
 	regDumpLen  uint32
 }
 
+type ethtoolPermAddr struct {
+	cmd  uint32
+	size uint32
+	data [32]byte
+}
+
 type ethtoolValue struct {
 	cmd  uint32
 	data uint32
@@ -145,17 +152,30 @@ func ethtoolAddPortInfo(info *api.ResourcesNetworkCardPort) error {
 	}
 	defer unix.Close(ethtoolFd)
 
+	// Prepare the request struct
+	req := ethtoolReq{}
+	copy(req.name[:], []byte(info.ID))
+
+	// Try to get MAC address
+	ethPermaddr := ethtoolPermAddr{
+		cmd:  0x00000020,
+		size: 32,
+	}
+	req.data = uintptr(unsafe.Pointer(&ethPermaddr))
+
+	_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(ethtoolFd), unix.SIOCETHTOOL, uintptr(unsafe.Pointer(&req)))
+	if errno == 0 {
+		hwaddr := net.HardwareAddr(ethPermaddr.data[0:ethPermaddr.size])
+		info.Address = hwaddr.String()
+	}
+
 	// Link state
 	ethGlink := ethtoolValue{
 		cmd: 0x0000000a,
 	}
+	req.data = uintptr(unsafe.Pointer(&ethGlink))
 
-	req := ethtoolReq{
-		data: uintptr(unsafe.Pointer(&ethGlink)),
-	}
-	copy(req.name[:], []byte(info.ID))
-
-	_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(ethtoolFd), unix.SIOCETHTOOL, uintptr(unsafe.Pointer(&req)))
+	_, _, 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_GLINK")
 	}


More information about the lxc-devel mailing list