Filter mediated devices for 3D controllers

Ensure that only mediated devices for 3D controller are returned
when listing the available mediated device types on a given
machine.

Change-Id: I22abfbbf09110ff333d6dbe0ba96982a8593b6ee
This commit is contained in:
James Page 2022-05-19 14:29:54 +01:00
parent 37e0fe5982
commit eda2fb6545
2 changed files with 14 additions and 14 deletions

View File

@ -60,6 +60,9 @@ def disable_nouveau_driver():
update_initramfs()
GPU_DEVICE_CLASS = '0x030200'
def list_vgpu_types():
"""Human-readable list of all vGPU types registered by the NVIDIA driver.
@ -72,6 +75,14 @@ def list_vgpu_types():
found_pci_addr_dirs = []
for root, dirs, files in os.walk('/sys/devices'):
if vgpu_types_dirname in dirs:
# Other types of device can present mediated devices
# so ensure that only 3D controller class devices
# are listed
device_class = (
Path(os.path.join(root, 'class')).read_text().rstrip()
)
if device_class != GPU_DEVICE_CLASS:
continue
# At this point root looks like
# /sys/devices/pci0000:40/0000:40:03.1/0000:41:00.0
found_pci_addr_dirs.append(root)
@ -118,7 +129,7 @@ def _has_nvidia_gpu_hardware_notcached():
device_subsystem_vendor = device.subsystem_vendor.name
except AttributeError:
device_subsystem_vendor = ''
logging.debug(device_class)
if '3D' in device_class and ('NVIDIA' in device_vendor or
'NVIDIA' in device_subsystem_vendor):
logging.debug('NVIDIA GPU found: {}'.format(device))

View File

@ -79,30 +79,19 @@ class TestNvidiaUtils(unittest.TestCase):
['mdev_supported_types'], []),
]
os_listdir_mock.side_effect = [
['nvidia-256', 'nvidia-257'],
['nvidia-301'],
]
path_mock_obj = MagicMock()
path_mock.return_value = path_mock_obj
path_mock_obj.read_text.side_effect = [
'GRID RTX6000-1Q',
('num_heads=4, frl_config=60, framebuffer=1024M, '
'max_resolution=5120x2880, max_instance=24'),
'GRID RTX6000-2Q',
('num_heads=4, frl_config=60, framebuffer=2048M, '
'max_resolution=7680x4320, max_instance=12'),
'0x030000', # causes device to not be included
'0x030200',
'GRID V100-16C',
('num_heads=1, frl_config=60, framebuffer=16384M, '
'max_resolution=4096x2160, max_instance=1'),
]
expected_output = '\n'.join([
('nvidia-256, 0000:41:00.0, GRID RTX6000-1Q, num_heads=4, '
'frl_config=60, framebuffer=1024M, max_resolution=5120x2880, '
'max_instance=24'),
('nvidia-257, 0000:41:00.0, GRID RTX6000-2Q, num_heads=4, '
'frl_config=60, framebuffer=2048M, max_resolution=7680x4320, '
'max_instance=12'),
('nvidia-301, 0000:c1:00.0, GRID V100-16C, num_heads=1, '
'frl_config=60, framebuffer=16384M, max_resolution=4096x2160, '
'max_instance=1'),