deviceinterface: Fix creating direct interfaces (bz 1006324)

And take the opportunity to standarize on setting net.source,
rather than have API users have to do net.bridge/network/source_dev
This commit is contained in:
Cole Robinson 2013-09-24 10:00:01 -04:00
parent 9ec845af03
commit a2e5206730
12 changed files with 93 additions and 95 deletions

View File

@ -291,5 +291,5 @@ def get_virtual_network():
dev = virtinst.VirtualNetworkInterface(_conn)
dev.macaddr = "22:22:33:44:55:66"
dev.type = virtinst.VirtualNetworkInterface.TYPE_VIRTUAL
dev.network = "default"
dev.source = "default"
return dev

View File

@ -600,14 +600,14 @@ class TestXMLConfig(unittest.TestCase):
net3.model = "e1000"
net4 = VirtualNetworkInterface(g.conn)
net4.bridge = "foobr0"
net4.source = "foobr0"
net4.macaddr = "22:22:22:22:22:22"
net4.target_dev = "foo1"
net5 = VirtualNetworkInterface(g.conn)
net5.type = "ethernet"
net5.macaddr = "00:11:00:22:00:33"
net5.source_dev = "testeth1"
net5.source = "testeth1"
g.add_device(net1)
g.add_device(net2)
@ -775,7 +775,7 @@ class TestXMLConfig(unittest.TestCase):
net3 = VirtualNetworkInterface(g.conn)
net3.type = virtinst.VirtualNetworkInterface.TYPE_VIRTUAL
net3.macaddr = "22:22:22:22:22:22"
net3.network = "default"
net3.source = "default"
net3.model = "spapr-vlan"
net3.address.set_addrstr("spapr-vio")
g.add_device(net1)

View File

@ -20,7 +20,6 @@
<emulator>/usr/lib/xen/bin/qemu-dm</emulator>
<interface type="user">
<mac address="AA:AA:AA:AA:AA:AA"/>
<source bridge="br0" network="route"/>
<model type="testmodel"/>
<filterref filter="foo"/>
</interface>
@ -30,7 +29,7 @@
<model type="virtio"/>
</interface>
<interface type="bridge">
<source bridge="newfoo0" network="default"/>
<source bridge="newfoo0"/>
<mac address="22:22:22:22:22:22"/>
<target dev="test1"/>
</interface>

View File

@ -412,37 +412,31 @@ class XMLParseTest(unittest.TestCase):
check = self._make_checker(dev1)
check("type", "user")
check("model", None, "testmodel")
check("bridge", None, "br0")
check("network", None, "route")
check("source", None, None,)
check("macaddr", "22:11:11:11:11:11", "AA:AA:AA:AA:AA:AA")
check("filterref", None, "foo")
self.assertEquals(dev1.get_source(), None)
check = self._make_checker(dev2)
self.assertEquals(dev2.get_source(), "default")
check("network", "default", None)
check("bridge", None, "newbr0")
check("source", "default", None)
check("type", "network", "bridge")
check("source", None, "newbr0")
check("model", "e1000", "virtio")
check = self._make_checker(dev3)
check("type", "bridge")
check("bridge", "foobr0", "newfoo0")
check("network", None, "default")
check("source", "foobr0", "newfoo0")
check("macaddr", "22:22:22:22:22:22")
check("target_dev", None, "test1")
self.assertEquals(dev3.get_source(), "newfoo0")
check = self._make_checker(dev4)
check("type", "ethernet")
check("source_dev", "eth0", "eth1")
check("source", "eth0", "eth1")
check("target_dev", "nic02", "nic03")
check("target_dev", "nic03", None)
self.assertEquals(dev4.get_source(), "eth1")
check = self._make_checker(dev5)
check("type", "direct")
check("source_dev", "eth0.1")
check("source", "eth0.1")
check("source_mode", "vepa", "bridge")
virtualport = dev5.virtualport
@ -782,7 +776,7 @@ class XMLParseTest(unittest.TestCase):
adddev = virtinst.VirtualNetworkInterface(conn=conn)
adddev.type = "network"
adddev.network = "default"
adddev.source = "default"
adddev.macaddr = "1A:2A:3A:4A:5A:6A"
guest.add_device(virtinst.VirtualWatchdog(conn))

View File

@ -81,7 +81,7 @@ def supports_pxe(guest):
return True
try:
netobj = nic.conn.networkLookupByName(nic.network)
netobj = nic.conn.networkLookupByName(nic.source)
xmlobj = virtinst.Network(nic.conn, parsexml=netobj.XMLDesc(0))
if xmlobj.can_pxe():
return True

View File

@ -571,6 +571,9 @@ class vmmAddHardware(vmmGObjectUI):
format_list.get_child().set_text("")
########################
# get_config_* methods #
########################
def build_combo_with_values(self, combo, values, default=None):
dev_model = Gtk.ListStore(str, str)
combo.set_model(dev_model)
@ -627,9 +630,6 @@ class vmmAddHardware(vmmGObjectUI):
self.build_combo_with_values(combo, types, default)
# get_config_* methods #
########################
def get_config_hardware_type(self):
row = self.get_hw_selection()
if not row:

View File

@ -288,7 +288,7 @@ class vmmCloneVM(vmmGObjectUI):
for net in self.orig_vm.get_network_devices():
mac = net.macaddr
net_dev = net.get_source()
net_dev = net.source
net_type = net.type
# Generate a new MAC

View File

@ -2900,7 +2900,7 @@ class vmmDetails(vmmGObjectUI):
return
nettype = net.type
source = net.get_source()
source = net.source
source_mode = net.source_mode
model = net.model

View File

@ -732,21 +732,9 @@ def validate_network(err, conn, nettype, devname, macaddr, model=None):
# Create network device
try:
bridge = None
netname = None
if nettype == virtinst.VirtualNetworkInterface.TYPE_VIRTUAL:
netname = devname
elif nettype == virtinst.VirtualNetworkInterface.TYPE_BRIDGE:
bridge = devname
elif nettype == virtinst.VirtualNetworkInterface.TYPE_DIRECT:
bridge = devname
elif nettype == virtinst.VirtualNetworkInterface.TYPE_USER:
pass
net = virtinst.VirtualNetworkInterface(conn.get_backend())
net.type = nettype
net.bridge = bridge
net.network = netname
net.source = devname
net.macaddr = macaddr
net.model = model
if net.model == "spapr-vlan":

View File

@ -1549,8 +1549,8 @@ def parse_network(guest, optstring, dev=None, mac=None):
set_param = _build_set_param(dev, opts)
set_param("type", "type", net_type)
set_param("network", "network")
set_param("bridge", "bridge")
set_param("source", "network")
set_param("source", "bridge")
set_param("model", "model")
set_param("macaddr", "mac")
set_param("filterref", "filterref")

View File

@ -137,6 +137,11 @@ class VirtualNetworkInterface(VirtualDevice):
self._random_mac = None
self._default_bridge = None
###############
# XML helpers #
###############
def _generate_default_bridge(self):
ret = self._default_bridge
if ret is None:
@ -148,34 +153,87 @@ class VirtualNetworkInterface(VirtualDevice):
self._default_bridge = ret
return ret or None
def get_source(self):
def _get_default_bridge(self):
if self.type == self.TYPE_BRIDGE:
return self._generate_default_bridge()
return None
def _default_source_mode(self):
if self.type == self.TYPE_DIRECT:
return "vepa"
return None
def _get_default_mac(self):
if not self._random_mac:
self._random_mac = self.generate_mac(self.conn)
return self._random_mac
def _validate_mac(self, val):
util.validate_macaddr(val)
return val
def _get_source(self):
"""
Convenince function, try to return the relevant <source> value
per the network type.
"""
if self.type == self.TYPE_VIRTUAL:
return self.network
return self._network
if self.type == self.TYPE_BRIDGE:
return self.bridge
return self._bridge
if self.type == self.TYPE_ETHERNET or self.type == self.TYPE_DIRECT:
return self.source_dev
return self._source_dev
if self.type == self.TYPE_USER:
return None
return self.network or self.bridge or self.source_dev
def set_source(self, newsource):
return self._network or self._bridge or self._source_dev
def _set_source(self, newsource):
"""
Conveninece function, try to set the relevant <source> value
per the network type
"""
self._bridge = None
self._network = None
self._source_dev = None
if self.type == self.TYPE_VIRTUAL:
self.network = newsource
self._network = newsource
elif self.type == self.TYPE_BRIDGE:
self.bridge = newsource
self._bridge = newsource
elif self.type == self.TYPE_ETHERNET or self.type == self.TYPE_DIRECT:
self.source_dev = newsource
return
source = property(get_source, set_source)
self._source_dev = newsource
source = property(_get_source, _set_source)
##################
# XML properties #
##################
_XML_PROP_ORDER = [
"_bridge", "_network", "_source_dev", "source_mode",
"macaddr", "target_dev", "model", "virtualport",
"filterref"]
_bridge = XMLProperty("./source/@bridge", default_cb=_get_default_bridge)
_network = XMLProperty("./source/@network")
_source_dev = XMLProperty("./source/@dev")
virtualport = XMLChildProperty(VirtualPort, is_single=True)
type = XMLProperty("./@type",
default_cb=lambda s: s.TYPE_BRIDGE)
macaddr = XMLProperty("./mac/@address",
set_converter=_validate_mac,
default_cb=_get_default_mac)
source_mode = XMLProperty("./source/@mode",
default_cb=_default_source_mode)
model = XMLProperty("./model/@type")
target_dev = XMLProperty("./target/@dev")
filterref = XMLProperty("./filterref/@filter")
#############
# Build API #
#############
def setup(self, meter=None):
ignore = meter
@ -191,46 +249,4 @@ class VirtualNetworkInterface(VirtualDevice):
raise RuntimeError(msg)
_XML_PROP_ORDER = [
"bridge", "network", "source_dev", "source_mode",
"macaddr", "target_dev", "model", "virtualport",
"filterref"]
virtualport = XMLChildProperty(VirtualPort, is_single=True)
type = XMLProperty("./@type",
default_cb=lambda s: s.TYPE_BRIDGE)
def _get_default_mac(self):
if not self._random_mac:
self._random_mac = self.generate_mac(self.conn)
return self._random_mac
def _validate_mac(self, val):
util.validate_macaddr(val)
return val
macaddr = XMLProperty("./mac/@address",
set_converter=_validate_mac,
default_cb=_get_default_mac)
def _get_default_bridge(self):
if self.type == self.TYPE_BRIDGE:
return self._generate_default_bridge()
return None
bridge = XMLProperty("./source/@bridge",
default_cb=_get_default_bridge)
network = XMLProperty("./source/@network")
source_dev = XMLProperty("./source/@dev")
def _default_source_mode(self):
if self.type == self.TYPE_DIRECT:
return "vepa"
return None
source_mode = XMLProperty("./source/@mode",
default_cb=_default_source_mode)
model = XMLProperty("./model/@type")
target_dev = XMLProperty("./target/@dev")
filterref = XMLProperty("./filterref/@filter")
VirtualNetworkInterface.register_type()

View File

@ -379,7 +379,8 @@ SUPPORT_CONN_HV_GRAPHICS_SPICE = _make(version=8006,
SUPPORT_CONN_HV_CHAR_SPICEVMC = _make(version=8008,
drv_version=[("qemu", 14000)])
SUPPORT_CONN_HV_DIRECT_INTERFACE = _make(version=8007,
drv_version=[("qemu", 0)])
drv_version=[("qemu", 0),
("test", 0)])
SUPPORT_CONN_HV_FILESYSTEM = _make(
drv_version=[("qemu", 13000), ("lxc", 0), ("openvz", 0), ("test", 0)],
drv_libvirt_version=[("qemu", 8005), ("lxc", 0),