Skip to content

Commit

Permalink
Define constants for the VIF model types
Browse files Browse the repository at this point in the history
Define constants for the common VIF model types. This will
ensure that all of the drivers will make use of the same values.

Change-Id: I2099bbe92906ac0b1b54f1819113b33565e78980
  • Loading branch information
gkotton authored and openstack-gerrit committed Apr 13, 2014
1 parent a03a977 commit 0f263d7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
9 changes: 9 additions & 0 deletions nova/network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def ensure_string_keys(d):
VIF_DETAIL_PORT_FILTER = 'port_filter'
VIF_DETAIL_OVS_HYBRID_PLUG = 'ovs_hybrid_plug'

# Constants for the 'vif_model' values
VIF_MODEL_VIRTIO = 'virtio'
VIF_MODEL_NE2K_PCI = 'ne2k_pci'
VIF_MODEL_PCNET = 'pcnet'
VIF_MODEL_RTL8139 = 'rtl8139'
VIF_MODEL_E1000 = 'e1000'
VIF_MODEL_E1000E = 'e1000e'
VIF_MODEL_NETFRONT = 'netfront'

# Constant for max length of network interface names
# eg 'bridge' in the Network class or 'devname' in
# the VIF class
Expand Down
11 changes: 6 additions & 5 deletions nova/tests/virt/libvirt/test_libvirt_vif.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,19 @@ def test_model_kvm(self):
d = vif.LibvirtGenericVIFDriver(self._get_conn())
xml = self._get_instance_xml(d, self.vif_bridge)

self._assertModel(xml, "virtio")
self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)

def test_model_kvm_custom(self):
self.flags(use_virtio_for_bridges=True,
virt_type='kvm',
group='libvirt')

d = vif.LibvirtGenericVIFDriver(self._get_conn())
image_meta = {'properties': {'hw_vif_model': 'e1000'}}
image_meta = {'properties': {'hw_vif_model':
network_model.VIF_MODEL_E1000}}
xml = self._get_instance_xml(d, self.vif_bridge,
image_meta)
self._assertModel(xml, "e1000")
self._assertModel(xml, network_model.VIF_MODEL_E1000)

def test_model_kvm_bogus(self):
self.flags(use_virtio_for_bridges=True,
Expand Down Expand Up @@ -416,7 +417,7 @@ def _test_model_qemu(self, *vif_objs, **kw):
self.assertEqual(outbound.get("burst"),
self.bandwidth['quota:vif_outbound_burst'])

self._assertModel(xml, "virtio", "qemu")
self._assertModel(xml, network_model.VIF_MODEL_VIRTIO, "qemu")

def test_model_qemu_no_firewall(self):
self.flags(firewall_driver="nova.virt.firewall.NoopFirewallDriver")
Expand Down Expand Up @@ -760,7 +761,7 @@ def test_mlnx_direct_vif_driver(self):
self._assertTypeEquals(node, "direct", "source",
"mode", "passthrough")
self._assertMacEquals(node, self.vif_mlnx)
self._assertModel(xml, "virtio")
self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)

def test_midonet_ethernet_vif_driver(self):
d = vif.LibvirtGenericVIFDriver(self._get_conn())
Expand Down
17 changes: 17 additions & 0 deletions nova/tests/virt/vmwareapi/test_vm_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import mock

from nova import exception
from nova.network import model as network_model
from nova.openstack.common.gettextutils import _
from nova.openstack.common import units
from nova.openstack.common import uuidutils
Expand Down Expand Up @@ -630,3 +631,19 @@ def test_get_vm_create_spec(self):
expected = re.sub(r'\s+', '', expected)
result = re.sub(r'\s+', '', repr(result))
self.assertEqual(expected, result)

def test_convert_vif_model(self):
expected = "VirtualE1000"
result = vm_util._convert_vif_model(network_model.VIF_MODEL_E1000)
self.assertEqual(expected, result)
expected = "VirtualE1000e"
result = vm_util._convert_vif_model(network_model.VIF_MODEL_E1000E)
self.assertEqual(expected, result)
types = ["VirtualE1000", "VirtualE1000e", "VirtualPCNet32",
"VirtualVmxnet"]
for type in types:
self.assertEqual(type,
vm_util._convert_vif_model(type))
self.assertRaises(exception.Invalid,
vm_util._convert_vif_model,
"InvalidVifModel")
23 changes: 18 additions & 5 deletions nova/virt/libvirt/vif.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@

def is_vif_model_valid_for_virt(virt_type, vif_model):
valid_models = {
'qemu': ['virtio', 'ne2k_pci', 'pcnet', 'rtl8139', 'e1000'],
'kvm': ['virtio', 'ne2k_pci', 'pcnet', 'rtl8139', 'e1000'],
'xen': ['netfront', 'ne2k_pci', 'pcnet', 'rtl8139', 'e1000'],
'qemu': [network_model.VIF_MODEL_VIRTIO,
network_model.VIF_MODEL_NE2K_PCI,
network_model.VIF_MODEL_PCNET,
network_model.VIF_MODEL_RTL8139,
network_model.VIF_MODEL_E1000],
'kvm': [network_model.VIF_MODEL_VIRTIO,
network_model.VIF_MODEL_NE2K_PCI,
network_model.VIF_MODEL_PCNET,
network_model.VIF_MODEL_RTL8139,
network_model.VIF_MODEL_E1000],
'xen': [network_model.VIF_MODEL_NETFRONT,
network_model.VIF_MODEL_NE2K_PCI,
network_model.VIF_MODEL_PCNET,
network_model.VIF_MODEL_RTL8139,
network_model.VIF_MODEL_E1000],
'lxc': [],
'uml': [],
}
Expand Down Expand Up @@ -113,11 +125,12 @@ def get_config(self, instance, vif, image_meta, inst_type):
if (model is None and
CONF.libvirt.virt_type in ('kvm', 'qemu') and
CONF.libvirt.use_virtio_for_bridges):
model = "virtio"
model = network_model.VIF_MODEL_VIRTIO

# Workaround libvirt bug, where it mistakenly
# enables vhost mode, even for non-KVM guests
if model == "virtio" and CONF.libvirt.virt_type == "qemu":
if (model == network_model.VIF_MODEL_VIRTIO and
CONF.libvirt.virt_type == "qemu"):
driver = "qemu"

if not is_vif_model_valid_for_virt(CONF.libvirt.virt_type,
Expand Down
19 changes: 17 additions & 2 deletions nova/virt/vmwareapi/vm_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from oslo.config import cfg

from nova import exception
from nova.network import model as network_model
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova.openstack.common import units
Expand All @@ -35,6 +36,9 @@
CONF = cfg.CONF
LOG = logging.getLogger(__name__)

ALL_SUPPORTED_NETWORK_DEVICES = ['VirtualE1000', 'VirtualE1000e',
'VirtualPCNet32', 'VirtualSriovEthernetCard',
'VirtualVmxnet']
DSRecord = collections.namedtuple(
'DSRecord', ['datastore', 'name', 'capacity', 'freespace'])

Expand Down Expand Up @@ -183,6 +187,18 @@ def create_controller_spec(client_factory, key, adapter_type="lsiLogic"):
return virtual_device_config


def _convert_vif_model(name):
"""Converts standard VIF_MODEL types to the internal VMware ones."""
if name == network_model.VIF_MODEL_E1000:
return 'VirtualE1000'
if name == network_model.VIF_MODEL_E1000E:
return 'VirtualE1000e'
if name not in ALL_SUPPORTED_NETWORK_DEVICES:
msg = _('%s is not supported.') % name
raise exception.Invalid(msg)
return name


def create_network_spec(client_factory, vif_info):
"""Builds a config spec for the addition of a new network
adapter to the VM.
Expand All @@ -191,8 +207,7 @@ def create_network_spec(client_factory, vif_info):
network_spec.operation = "add"

# Keep compatible with other Hyper vif model parameter.
if vif_info['vif_model'] == "e1000":
vif_info['vif_model'] = "VirtualE1000"
vif_info['vif_model'] = _convert_vif_model(vif_info['vif_model'])

vif = 'ns0:' + vif_info['vif_model']
net_device = client_factory.create(vif)
Expand Down
4 changes: 3 additions & 1 deletion nova/virt/vmwareapi/vmops.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from nova.compute import task_states
from nova import context as nova_context
from nova import exception
from nova.network import model as network_model
from nova.openstack.common import excutils
from nova.openstack.common.gettextutils import _
from nova.openstack.common import lockutils
Expand Down Expand Up @@ -235,7 +236,8 @@ def _get_image_properties(root_size):
disk_type = image_properties.get("vmware_disktype",
"preallocated")
# Get the network card type from the image properties.
vif_model = image_properties.get("hw_vif_model", "VirtualE1000")
vif_model = image_properties.get("hw_vif_model",
network_model.VIF_MODEL_E1000)

# Fetch the image_linked_clone data here. It is retrieved
# with the above network based API call. To retrieve it
Expand Down

0 comments on commit 0f263d7

Please sign in to comment.