Coverage for nova/virt/libvirt/designer.py: 99%

99 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright (C) 2013 Red Hat, Inc. 

2# 

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

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

5# a copy of the License at 

6# 

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

8# 

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

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

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

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15""" 

16Policy based configuration of libvirt objects 

17 

18This module provides helper APIs for populating the config.py 

19classes based on common operational needs / policies 

20""" 

21 

22 

23from nova.pci import utils as pci_utils 

24 

25 

26def set_vif_guest_frontend_config(conf, mac, model, driver, queues, 

27 rx_queue_size, packed): 

28 """Populate a LibvirtConfigGuestInterface instance 

29 with guest frontend details. 

30 

31 NOTE: @model, @driver, @queues and @rx_queue_size can be None. 

32 """ 

33 conf.mac_addr = mac 

34 if model is not None: 

35 conf.model = model 

36 if driver is not None: 

37 conf.driver_name = driver 

38 if queues is not None: 

39 conf.vhost_queues = queues 

40 if rx_queue_size: 

41 conf.vhost_rx_queue_size = rx_queue_size 

42 if packed is not None: 

43 conf.driver_packed = packed 

44 

45 

46def set_vif_host_backend_ethernet_config(conf, tapname): 

47 """Populate a LibvirtConfigGuestInterface instance 

48 with host backend details for an externally configured 

49 host device. 

50 

51 NB use of this configuration is discouraged by 

52 libvirt project and will mark domains as 'tainted'. 

53 """ 

54 

55 conf.net_type = "ethernet" 

56 conf.target_dev = tapname 

57 conf.script = None 

58 

59 

60def set_vif_host_backend_802qbg_config(conf, devname, managerid, 

61 typeid, typeidversion, 

62 instanceid, tapname=None): 

63 """Populate a LibvirtConfigGuestInterface instance 

64 with host backend details for an 802.1qbg device. 

65 """ 

66 

67 conf.net_type = "direct" 

68 conf.source_dev = devname 

69 conf.source_mode = "vepa" 

70 conf.vporttype = "802.1Qbg" 

71 conf.add_vport_param("managerid", managerid) 

72 conf.add_vport_param("typeid", typeid) 

73 conf.add_vport_param("typeidversion", typeidversion) 

74 conf.add_vport_param("instanceid", instanceid) 

75 if tapname: 

76 conf.target_dev = tapname 

77 

78 

79def set_vif_host_backend_802qbh_config(conf, net_type, devname, profileid, 

80 tapname=None): 

81 """Populate a LibvirtConfigGuestInterface instance 

82 with host backend details for an 802.1qbh device. 

83 """ 

84 

85 conf.net_type = net_type 

86 if net_type == 'direct': 

87 conf.source_mode = 'passthrough' 

88 conf.source_dev = pci_utils.get_ifname_by_pci_address(devname) 

89 conf.driver_name = 'vhost' 

90 else: 

91 conf.source_dev = devname 

92 conf.model = None 

93 conf.vporttype = "802.1Qbh" 

94 conf.add_vport_param("profileid", profileid) 

95 if tapname: 

96 conf.target_dev = tapname 

97 

98 

99def set_vif_host_backend_hw_veb(conf, net_type, devname, vlan, 

100 tapname=None): 

101 """Populate a LibvirtConfigGuestInterface instance 

102 with host backend details for an device that supports hardware 

103 virtual ethernet bridge. 

104 """ 

105 

106 conf.net_type = net_type 

107 conf.vlan = vlan 

108 if net_type == 'direct': 

109 conf.source_mode = 'passthrough' 

110 conf.source_dev = pci_utils.get_ifname_by_pci_address(devname) 

111 conf.driver_name = 'vhost' 

112 else: # net_type == network_model.VNIC_TYPE_DIRECT 

113 conf.source_dev = devname 

114 conf.model = None 

115 if tapname: 

116 conf.target_dev = tapname 

117 

118 

119def set_vif_host_backend_hostdev_pci_config(conf, pci_slot): 

120 """Populate a LibvirtConfigGuestHostdev instance with pci address data.""" 

121 

122 conf.domain, conf.bus, conf.slot, conf.function = ( 

123 pci_utils.get_pci_address_fields(pci_slot)) 

124 

125 

126def set_vif_host_backend_direct_config(conf, devname, mode="passthrough"): 

127 """Populate a LibvirtConfigGuestInterface instance 

128 with direct Interface. 

129 """ 

130 

131 conf.net_type = "direct" 

132 conf.source_mode = mode 

133 conf.source_dev = devname 

134 conf.model = "virtio" 

135 

136 

137def set_vif_host_backend_vhostuser_config(conf, mode, path, rx_queue_size, 

138 tx_queue_size, tapname=None): 

139 """Populate a LibvirtConfigGuestInterface instance 

140 with host backend details for vhostuser socket. 

141 

142 NOTE: @rx_queue_size and @tx_queue_size can be None 

143 """ 

144 conf.net_type = "vhostuser" 

145 conf.vhostuser_type = "unix" 

146 conf.vhostuser_mode = mode 

147 conf.vhostuser_path = path 

148 if rx_queue_size: 

149 conf.vhost_rx_queue_size = rx_queue_size 

150 if tx_queue_size: 

151 conf.vhost_tx_queue_size = tx_queue_size 

152 if tapname: 

153 conf.target_dev = tapname 

154 

155 

156def set_vif_host_backend_vdpa_config( 

157 conf, dev_path, rx_queue_size=None, tx_queue_size=None, 

158): 

159 """Populate a LibvirtConfigGuestInterface instance 

160 with host backend details for a vdpa device. 

161 

162 NOTE: @rx_queue_size and @tx_queue_size can be None 

163 """ 

164 conf.net_type = "vdpa" 

165 conf.source_dev = dev_path 

166 if rx_queue_size: 

167 conf.vhost_rx_queue_size = rx_queue_size 

168 if tx_queue_size: 

169 conf.vhost_tx_queue_size = tx_queue_size 

170 

171 

172def set_vif_mtu_config(conf, mtu): 

173 """Populate a LibvirtConfigGuestInterface instance 

174 with network mtu. 

175 """ 

176 conf.mtu = mtu 

177 

178 

179def set_vif_bandwidth_config(conf, flavor): 

180 """Config vif inbound/outbound bandwidth limit. parameters are 

181 set in instance_type_extra_specs table, key is in the format 

182 quota:vif_inbound_average. 

183 """ 

184 

185 bandwidth_items = ['vif_inbound_average', 'vif_inbound_peak', 

186 'vif_inbound_burst', 'vif_outbound_average', 'vif_outbound_peak', 

187 'vif_outbound_burst'] 

188 for key, value in flavor.get('extra_specs', {}).items(): 

189 scope = key.split(':') 

190 if len(scope) > 1 and scope[0] == 'quota': 

191 if scope[1] in bandwidth_items: 191 ↛ 188line 191 didn't jump to line 188 because the condition on line 191 was always true

192 setattr(conf, scope[1], value) 

193 

194 

195def set_numa_memnode(conf, guest_node_id, host_cell_id): 

196 """Prepares numa memory node config for the guest. 

197 """ 

198 conf.cellid = guest_node_id 

199 conf.nodeset = [host_cell_id] 

200 conf.mode = "strict" 

201 

202 

203def set_vcpu_realtime_scheduler(conf, vcpus_rt, priority): 

204 """Prepares realtime config for the guest.""" 

205 conf.vcpus = vcpus_rt 

206 conf.scheduler = "fifo" 

207 conf.priority = priority 

208 

209 

210def set_driver_iommu_for_device(dev): 

211 if dev.uses_virtio: 

212 dev.driver_iommu = True 

213 

214 

215def set_driver_iommu_for_all_devices(conf): 

216 for dev in conf.devices: 

217 set_driver_iommu_for_device(dev)