Coverage for nova/conf/devices.py: 94%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 15:08 +0000

1# Licensed under the Apache License, Version 2.0 (the "License"); you may 

2# not use this file except in compliance with the License. You may obtain 

3# a copy of the License at 

4# 

5# http://www.apache.org/licenses/LICENSE-2.0 

6# 

7# Unless required by applicable law or agreed to in writing, software 

8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

10# License for the specific language governing permissions and limitations 

11# under the License. 

12 

13from oslo_config import cfg 

14 

15devices_group = cfg.OptGroup( 

16 name='devices', 

17 title='physical or virtual device options') 

18 

19mdev_opts = [ 

20 cfg.ListOpt('enabled_mdev_types', 

21 default=[], 

22 deprecated_name='enabled_vgpu_types', 

23 help=""" 

24The mdev types enabled in the compute node. 

25 

26Some hardware (e.g. NVIDIA GRID K1) support different mdev types. User can use 

27this option to specify a list of enabled mdev types that may be assigned to a 

28guest instance. 

29 

30If more than one single mdev type is provided, then for each *mdev type* an 

31additional section, ``[mdev_$(MDEV_TYPE)]``, must be added to the configuration 

32file. Each section then can be configured with a single configuration option, 

33``device_addresses``, which should be a list of PCI addresses corresponding to 

34the physical GPU(s) or mdev-capable hardware to assign to this type. If 

35`device_addresses` is not provided, then the related GPU type will be the 

36default for all the found GPUs that aren't used by other types. 

37 

38 

39If one or more sections are missing (meaning that a specific type is not wanted 

40to use for at least one physical device), then Nova will only use the first 

41type that was provided by ``[devices]/enabled_mdev_types``. 

42 

43If two or more sections are not set with ``device_addresses`` values, then only 

44the first one will be used for defaulting all the non-defined GPUs to use this 

45type. 

46 

47If the same PCI address is provided for two different types, nova-compute will 

48return an InvalidLibvirtMdevConfig exception at restart. 

49 

50As an interim period, old configuration groups named ``[vgpu_$(MDEV_TYPE)]`` 

51will be accepted. A valid configuration could then be:: 

52 

53 [devices] 

54 enabled_mdev_types = nvidia-35, nvidia-36 

55 

56 [mdev_nvidia-35] 

57 device_addresses = 0000:84:00.0,0000:85:00.0 

58 

59 [vgpu_nvidia-36] 

60 device_addresses = 0000:86:00.0 

61 

62Another valid configuration could be:: 

63 

64 [devices] 

65 enabled_mdev_types = nvidia-35, nvidia-36 

66 

67 [mdev_nvidia-35] 

68 

69 [mdev_nvidia-36] 

70 device_addresses = 0000:86:00.0 

71 

72 

73""") 

74] 

75 

76 

77def register_opts(conf): 

78 conf.register_group(devices_group) 

79 conf.register_opts(mdev_opts, group=devices_group) 

80 

81 

82def register_dynamic_opts(conf): 

83 """Register dynamically-generated options and groups. 

84 

85 This must be called by the service that wishes to use the options **after** 

86 the initial configuration has been loaded. 

87 """ 

88 

89 for mdev_type in conf.devices.enabled_mdev_types: 

90 # Register the '[mdev_$(MDEV_TYPE)]/device_addresses' opts, implicitly 

91 # registering the '[mdev_$(MDEV_TYPE)]' groups in the process 

92 opt = cfg.ListOpt('device_addresses', default=[], 

93 item_type=cfg.types.String(), 

94 deprecated_group='vgpu_%s' % mdev_type) 

95 conf.register_opt(opt, group='mdev_%s' % mdev_type) 

96 

97 # Register the '[mdev_$(MDEV_TYPE)]/mdev_class' opts 

98 class_opt = cfg.StrOpt( 

99 'mdev_class', 

100 default='VGPU', 

101 regex=r'^(VGPU|CUSTOM_[A-Z0-9_]+)$', 

102 max_length=255, 

103 help='Class of mediated device to manage used to differentiate ' 

104 'between device types. The name has to be prefixed by ' 

105 'CUSTOM_ if it is not VGPU.') 

106 conf.register_opt(class_opt, group='mdev_%s' % mdev_type) 

107 

108 # Register the '[mdev_$(MDEV_TYPE)]/max_instances' opts 

109 max_inst_opt = cfg.IntOpt( 

110 'max_instances', 

111 default=None, min=1, 

112 help='Number of mediated devices that type can create. ' 

113 'If not set, it implies that we use the maximum allowed by ' 

114 'the type.') 

115 conf.register_opt(max_inst_opt, group='mdev_%s' % mdev_type) 

116 

117 

118def list_opts(): 

119 return {devices_group: mdev_opts}