Skip to content

Commit 32b76f6

Browse files
committed
Use ReadLink in NIC.py
1 parent ec3025e commit 32b76f6

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

lisa/nic.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from assertpy import assert_that
1414
from retry import retry
1515

16-
from lisa.tools import Cat, Ip, KernelConfig, Ls, Lspci, Modprobe, Tee
16+
from lisa.tools import Cat, Ip, KernelConfig, Ls, Lspci, Modprobe, Readlink, Tee
1717
from lisa.util import InitializableMixin, LisaException, constants, find_groups_in_lines
1818

1919
if TYPE_CHECKING:
@@ -121,11 +121,6 @@ class Nics(InitializableMixin):
121121
)
122122
)
123123

124-
# PCI slot pattern: XXXX:XX:XX.X (e.g., 34da:00:02.0)
125-
__pci_slot_pattern = re.compile(
126-
r"([a-zA-Z0-9]{4}:[a-zA-Z0-9]{2}:[a-zA-Z0-9]{2}\.[a-zA-Z0-9])"
127-
)
128-
129124
_file_not_exist = re.compile(r"No such file or directory", re.MULTILINE)
130125

131126
# ConnectX-3 uses mlx4_core
@@ -232,10 +227,12 @@ def get_nic_driver(self, nic_name: str) -> str:
232227
# get the current driver for the nic from the node
233228
# sysfs provides a link to the driver entry at device/driver
234229
nic = self.get_nic(nic_name)
235-
cmd = f"readlink -f /sys/class/net/{nic_name}/device/driver"
230+
readlink = self._node.tools[Readlink]
236231
# ex return value:
237232
# /sys/bus/vmbus/drivers/hv_netvsc
238-
found_link = self._node.execute(cmd, expected_exit_code=0).stdout
233+
found_link = readlink.get_canonical_path(
234+
f"/sys/class/net/{nic_name}/device/driver"
235+
)
239236
assert_that(found_link).described_as(
240237
f"sysfs check for NIC device {nic_name} driver returned no output"
241238
).is_not_equal_to("")
@@ -400,8 +397,9 @@ def _get_nic_names(self) -> List[str]:
400397
return non_virtual_nics
401398

402399
def _get_nic_uuid(self, nic_name: str) -> str:
403-
full_dev_path = self._node.execute(f"readlink /sys/class/net/{nic_name}/device")
404-
uuid = os.path.basename(full_dev_path.stdout.strip())
400+
readlink = self._node.tools[Readlink]
401+
full_dev_path = readlink.get_target(f"/sys/class/net/{nic_name}/device")
402+
uuid = os.path.basename(full_dev_path)
405403
self._node.log.debug(f"{nic_name} UUID:{uuid}")
406404
return uuid
407405

@@ -519,6 +517,7 @@ def _discover_standalone_pci_nics(self, lspci: Lspci) -> None:
519517
"""
520518
# Get unpaired NICs that might have PCI devices
521519
unpaired_nics = self.get_unpaired_devices()
520+
readlink = self._node.tools[Readlink]
522521

523522
for nic_name in unpaired_nics:
524523
nic = self.nics[nic_name]
@@ -527,16 +526,15 @@ def _discover_standalone_pci_nics(self, lspci: Lspci) -> None:
527526
continue
528527

529528
# Try to find the PCI slot for this NIC by checking its device path
530-
result = self._node.execute(
531-
f"readlink -f /sys/class/net/{nic_name}/device",
532-
shell=True,
529+
device_path = readlink.get_canonical_path(
530+
f"/sys/class/net/{nic_name}/device",
531+
no_error_log=True,
533532
)
534-
if result.exit_code == 0 and result.stdout.strip():
535-
# Extract PCI slot from device path
533+
if device_path:
534+
# Extract PCI slot from device path using lspci tool
536535
# Path format: /sys/devices/.../XXXX:XX:XX.X/net/nicname
537-
match = self.__pci_slot_pattern.search(result.stdout)
538-
if match:
539-
pci_slot = match.group(1)
536+
pci_slot = lspci.get_pci_slot_from_device_path(device_path)
537+
if pci_slot:
540538
# Get the module name for this PCI device
541539
try:
542540
module_name = lspci.get_used_module(pci_slot)
@@ -561,9 +559,10 @@ def _discover_standalone_pci_nics(self, lspci: Lspci) -> None:
561559
else:
562560
self._node.log.debug(
563561
f"Could not extract PCI slot from device path for {nic_name}: "
564-
f"{result.stdout.strip()}"
562+
f"{device_path}"
565563
)
566564

565+
567566
def _get_default_nic(self) -> None:
568567
self.default_nic: str = ""
569568
self.default_nic_route: str = ""

lisa/tools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
from .python import Pip, Python
105105
from .qemu import Qemu
106106
from .qemu_img import QemuImg
107+
from .readlink import Readlink
107108
from .reboot import Reboot
108109
from .remote_copy import RemoteCopy
109110
from .resize_partition import ResizePartition
@@ -242,6 +243,7 @@
242243
"Qemu",
243244
"QemuImg",
244245
"Reboot",
246+
"Readlink",
245247
"RemoteCopy",
246248
"ResizePartition",
247249
"Rpm",

lisa/tools/lspci.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@
138138
# Kernel driver in use: mlx5_core\r
139139
PATTERN_MODULE_IN_USE = re.compile(r"Kernel driver in use: ([A-Za-z0-9_-]*)", re.M)
140140

141+
# PCI slot pattern for extracting slot from device paths
142+
# Example: /sys/devices/pci0000:00/0000:00:02.0/net/eth0 -> 0000:00:02.0
143+
PATTERN_PCI_SLOT = re.compile(
144+
r"([a-fA-F0-9]{4}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}\.[a-fA-F0-9])"
145+
)
146+
141147

142148
class PciDevice:
143149
def __init__(self, pci_device_raw: str) -> None:
@@ -402,6 +408,13 @@ def get_devices_by_vendor_device_id(
402408
devices_list.append(device)
403409
return devices_list
404410

411+
def get_pci_slot_from_device_path(self, device_path: str) -> Optional[str]:
412+
"""
413+
Extract PCI slot information from a device path.
414+
"""
415+
pci_slot = get_matched_str(device_path, PATTERN_PCI_SLOT)
416+
return pci_slot if pci_slot else None
417+
405418

406419
class LspciBSD(Lspci):
407420
_DEVICE_DRIVER_MAPPING: Dict[str, Pattern[str]] = {

0 commit comments

Comments
 (0)