Skip to content

Commit b5bdd5a

Browse files
committed
Fix handling larger root complex with PCI path
Larger systems have multiple root buses, adjust handling appropriately.
1 parent 5644fc0 commit b5bdd5a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

qubes/tests/devices_pci.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import qubes.tests
2525
import qubes.ext.pci
2626
from qubes.device_protocol import DeviceInterface
27-
from qubes.utils import sbdf_to_path, path_to_sbdf
27+
from qubes.utils import sbdf_to_path, path_to_sbdf, is_pci_path
2828

2929
orig_open = open
3030

@@ -157,7 +157,17 @@ def test_011_path_to_sbdf2(self):
157157
path = path_to_sbdf("0000_00_18.4")
158158
self.assertEqual(path, "0000:00:18.4")
159159

160+
def test_020_is_pci_path(self):
161+
self.assertTrue(is_pci_path("0000_00_18.4"))
160162

163+
def test_021_is_pci_path_false(self):
164+
self.assertFalse(is_pci_path("0000_c6_00.0"))
165+
166+
def test_022_is_pci_path_non_00_bus(self):
167+
self.assertTrue(is_pci_path("0000_c0_00.0"))
168+
169+
170+
@mock.patch("qubes.utils.SYSFS_BASE", tests_sysfs_path)
161171
class TC_10_PCI(qubes.tests.QubesTestCase):
162172
def setUp(self):
163173
super().setUp()

qubes/utils.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,18 @@ def sbdf_to_path(device_id: str):
373373
)
374374
sysfs_pci_devs_base = f"{SYSFS_BASE}/bus/pci/devices"
375375

376+
root_buses = [dev[3:]
377+
for dev in os.listdir(f"{SYSFS_BASE}/devices")
378+
if dev.startswith("pci")]
379+
376380
dev_match = regex.match(device_id)
377381
if not dev_match:
378382
raise ValueError("Invalid device identifier: {!r}".format(device_id))
379383
if dev_match["segment"] is not None:
380384
segment = dev_match["segment"]
381385
else:
382386
segment = "0000"
383-
if dev_match["bus"] == "00":
387+
if f"{segment}:{dev_match['bus']}" in root_buses:
384388
return (f"{segment}_" if segment != "0000" else "") + (
385389
f"{dev_match['bus']}_"
386390
f"{dev_match['device']}.{dev_match['function']}"
@@ -488,8 +492,15 @@ def is_pci_path(device_id: str):
488492
:param device_id: device id to check
489493
:return:
490494
"""
495+
496+
root_buses = [dev[3:].replace(":", "_")
497+
for dev in os.listdir(f"{SYSFS_BASE}/devices")
498+
if dev.startswith("pci")]
499+
# add segment prefix for easier matching
500+
if len(device_id) > 2 and device_id[2] == "_":
501+
device_id = "0000_" + device_id
491502
path_re = re.compile(
492-
r"\A([0-9a-f]{4}_)?00_[0-9a-f]{2}\.[0-9a-f]"
503+
r"\A(" + "|".join(root_buses) + r")_[0-9a-f]{2}\.[0-9a-f]"
493504
r"(-[0-9a-f]{2}_[0-9a-f]{2}\.[0-9a-f])*\Z"
494505
)
495506
return bool(path_re.match(device_id))

0 commit comments

Comments
 (0)