2013-03-18 05:06:52 +08:00
|
|
|
#
|
2013-10-28 04:59:46 +08:00
|
|
|
# Copyright 2006-2009, 2013 Red Hat, Inc.
|
2013-03-18 05:06:52 +08:00
|
|
|
#
|
2018-04-04 21:35:41 +08:00
|
|
|
# This work is licensed under the GNU GPLv2 or later.
|
2018-03-21 03:00:02 +08:00
|
|
|
# See the COPYING file in the top-level directory.
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2016-08-25 04:14:33 +08:00
|
|
|
import os
|
2013-07-12 21:54:01 +08:00
|
|
|
import random
|
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
from .device import Device
|
2019-06-17 09:12:39 +08:00
|
|
|
from ..logger import log
|
2018-03-21 00:27:37 +08:00
|
|
|
from ..xmlbuilder import XMLBuilder, XMLChildProperty, XMLProperty
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-11 23:11:21 +08:00
|
|
|
|
2013-07-12 21:54:01 +08:00
|
|
|
def _random_mac(conn):
|
|
|
|
"""Generate a random MAC address.
|
|
|
|
|
|
|
|
00-16-3E allocated to xensource
|
|
|
|
52-54-00 used by qemu/kvm
|
|
|
|
|
2018-04-30 20:56:53 +08:00
|
|
|
The OUI list is available at https://standards.ieee.org/regauth/oui/oui.txt.
|
2013-07-12 21:54:01 +08:00
|
|
|
|
|
|
|
The remaining 3 fields are random, with the first bit of the first
|
|
|
|
random field set 0.
|
2013-04-11 23:11:21 +08:00
|
|
|
|
2013-07-12 21:54:01 +08:00
|
|
|
@return: MAC address string
|
|
|
|
"""
|
2013-04-11 23:11:21 +08:00
|
|
|
|
2014-09-12 21:36:35 +08:00
|
|
|
if conn.is_qemu():
|
|
|
|
oui = [0x52, 0x54, 0x00]
|
|
|
|
else:
|
|
|
|
# Xen
|
|
|
|
oui = [0x00, 0x16, 0x3E]
|
2013-07-12 21:54:01 +08:00
|
|
|
|
|
|
|
mac = oui + [
|
|
|
|
random.randint(0x00, 0xff),
|
|
|
|
random.randint(0x00, 0xff),
|
|
|
|
random.randint(0x00, 0xff)]
|
|
|
|
return ':'.join(["%02x" % x for x in mac])
|
2013-04-11 23:11:21 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2016-08-25 04:14:33 +08:00
|
|
|
def _default_route():
|
|
|
|
route_file = "/proc/net/route"
|
2019-06-11 02:15:50 +08:00
|
|
|
if not os.path.exists(route_file): # pragma: no cover
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("route_file=%s does not exist", route_file)
|
2016-08-25 04:14:33 +08:00
|
|
|
return None
|
|
|
|
|
2017-05-06 02:19:54 +08:00
|
|
|
for line in open(route_file):
|
2016-08-25 04:14:33 +08:00
|
|
|
info = line.split()
|
2019-06-11 02:15:50 +08:00
|
|
|
if len(info) != 11: # pragma: no cover
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("Unexpected field count=%s when parsing %s",
|
2016-08-25 04:14:33 +08:00
|
|
|
len(info), route_file)
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
route = int(info[1], 16)
|
|
|
|
if route == 0:
|
|
|
|
return info[0]
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
|
2019-06-11 02:15:50 +08:00
|
|
|
return None # pragma: no cover
|
2016-08-25 04:14:33 +08:00
|
|
|
|
|
|
|
|
2020-01-26 08:03:28 +08:00
|
|
|
def _host_default_bridge():
|
2016-08-25 04:14:33 +08:00
|
|
|
dev = _default_route()
|
|
|
|
if not dev:
|
2019-06-11 02:15:50 +08:00
|
|
|
return None # pragma: no cover
|
2016-08-25 04:14:33 +08:00
|
|
|
|
|
|
|
# New style peth0 == phys dev, eth0 == bridge, eth0 == default route
|
|
|
|
if os.path.exists("/sys/class/net/%s/bridge" % dev):
|
2019-06-11 02:15:50 +08:00
|
|
|
return dev # pragma: no cover
|
2016-08-25 04:14:33 +08:00
|
|
|
|
|
|
|
# Old style, peth0 == phys dev, eth0 == netloop, xenbr0 == bridge,
|
|
|
|
# vif0.0 == netloop enslaved, eth0 == default route
|
|
|
|
try:
|
|
|
|
defn = int(dev[-1])
|
2019-06-11 02:15:50 +08:00
|
|
|
except Exception: # pragma: no cover
|
2016-08-25 04:14:33 +08:00
|
|
|
defn = -1
|
|
|
|
|
|
|
|
if (defn >= 0 and
|
|
|
|
os.path.exists("/sys/class/net/peth%d/brport" % defn) and
|
|
|
|
os.path.exists("/sys/class/net/xenbr%d/bridge" % defn)):
|
2019-06-11 02:15:50 +08:00
|
|
|
return "xenbr%d" # pragma: no cover
|
2016-08-25 04:14:33 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-01-26 08:17:23 +08:00
|
|
|
# Cache the host default bridge lookup. It can change over the lifetime
|
|
|
|
# of a virt-manager run, but that should be rare, and this saves us
|
|
|
|
# possibly spamming logs if host lookup goes wrong
|
|
|
|
_HOST_DEFAULT_BRIDGE = -1
|
|
|
|
|
|
|
|
|
2020-01-26 08:03:28 +08:00
|
|
|
def _default_bridge(conn):
|
|
|
|
if conn.is_remote():
|
|
|
|
return None
|
|
|
|
|
2020-01-26 08:17:23 +08:00
|
|
|
global _HOST_DEFAULT_BRIDGE
|
|
|
|
if _HOST_DEFAULT_BRIDGE == -1:
|
|
|
|
try:
|
|
|
|
ret = _host_default_bridge()
|
2020-01-27 19:59:17 +08:00
|
|
|
except Exception: # pragma: no cover
|
2020-01-26 08:17:23 +08:00
|
|
|
log.debug("Error getting host default bridge", exc_info=True)
|
|
|
|
ret = None
|
|
|
|
_HOST_DEFAULT_BRIDGE = ret
|
|
|
|
|
2020-01-27 19:59:17 +08:00
|
|
|
ret = _HOST_DEFAULT_BRIDGE
|
2020-01-26 08:03:28 +08:00
|
|
|
if conn.in_testsuite():
|
2020-01-27 19:59:17 +08:00
|
|
|
ret = "testsuitebr0"
|
|
|
|
return ret
|
2016-08-25 04:14:33 +08:00
|
|
|
|
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
class _VirtualPort(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "virtualport"
|
2013-07-25 00:02:37 +08:00
|
|
|
|
2013-09-11 23:47:09 +08:00
|
|
|
type = XMLProperty("./@type")
|
|
|
|
managerid = XMLProperty("./parameters/@managerid", is_int=True)
|
|
|
|
typeid = XMLProperty("./parameters/@typeid", is_int=True)
|
|
|
|
typeidversion = XMLProperty("./parameters/@typeidversion", is_int=True)
|
|
|
|
instanceid = XMLProperty("./parameters/@instanceid")
|
2015-08-28 19:12:49 +08:00
|
|
|
profileid = XMLProperty("./parameters/@profileid")
|
|
|
|
interfaceid = XMLProperty("./parameters/@interfaceid")
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
class DeviceInterface(Device):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "interface"
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
TYPE_BRIDGE = "bridge"
|
|
|
|
TYPE_VIRTUAL = "network"
|
|
|
|
TYPE_USER = "user"
|
2016-08-27 13:41:38 +08:00
|
|
|
TYPE_VHOSTUSER = "vhostuser"
|
2013-03-18 05:06:52 +08:00
|
|
|
TYPE_ETHERNET = "ethernet"
|
|
|
|
TYPE_DIRECT = "direct"
|
|
|
|
|
2013-07-12 21:54:01 +08:00
|
|
|
@staticmethod
|
|
|
|
def generate_mac(conn):
|
|
|
|
"""
|
|
|
|
Generate a random MAC that doesn't conflict with any VMs on
|
|
|
|
the connection.
|
|
|
|
"""
|
2015-09-06 22:36:17 +08:00
|
|
|
if conn.fake_conn_predictable():
|
2013-07-12 21:54:01 +08:00
|
|
|
# Testing hack
|
|
|
|
return "00:11:22:33:44:55"
|
|
|
|
|
|
|
|
for ignore in range(256):
|
|
|
|
mac = _random_mac(conn)
|
2018-09-04 04:54:23 +08:00
|
|
|
try:
|
2020-07-06 06:56:36 +08:00
|
|
|
DeviceInterface.check_mac_in_use(conn, mac)
|
2013-07-12 21:54:01 +08:00
|
|
|
return mac
|
2019-06-11 02:15:50 +08:00
|
|
|
except RuntimeError: # pragma: no cover
|
2018-09-04 04:54:23 +08:00
|
|
|
continue
|
2013-07-12 21:54:01 +08:00
|
|
|
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug( # pragma: no cover
|
2019-06-11 02:15:50 +08:00
|
|
|
"Failed to generate non-conflicting MAC")
|
|
|
|
return None # pragma: no cover
|
2013-07-12 21:54:01 +08:00
|
|
|
|
|
|
|
@staticmethod
|
2020-07-06 06:56:36 +08:00
|
|
|
def check_mac_in_use(conn, searchmac):
|
2013-07-12 21:54:01 +08:00
|
|
|
"""
|
2018-09-04 04:54:23 +08:00
|
|
|
Raise RuntimeError if the passed mac conflicts with a defined VM
|
2013-07-12 21:54:01 +08:00
|
|
|
"""
|
2020-07-06 06:39:15 +08:00
|
|
|
if not searchmac:
|
|
|
|
return
|
|
|
|
|
2018-09-01 03:20:50 +08:00
|
|
|
vms = conn.fetch_all_domains()
|
2013-07-12 21:54:01 +08:00
|
|
|
for vm in vms:
|
2018-03-21 05:23:34 +08:00
|
|
|
for nic in vm.devices.interface:
|
2013-07-12 21:54:01 +08:00
|
|
|
nicmac = nic.macaddr or ""
|
|
|
|
if nicmac.lower() == searchmac.lower():
|
2018-09-04 04:54:23 +08:00
|
|
|
raise RuntimeError(
|
|
|
|
_("The MAC address '%s' is in use "
|
|
|
|
"by another virtual machine.") % searchmac)
|
2013-07-12 21:54:01 +08:00
|
|
|
|
2020-01-26 08:39:26 +08:00
|
|
|
@staticmethod
|
|
|
|
def default_bridge(conn):
|
|
|
|
"""
|
|
|
|
Return the bridge virt-install would use as a default value,
|
|
|
|
if one is setup on the host
|
|
|
|
"""
|
|
|
|
return _default_bridge(conn)
|
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
###############
|
|
|
|
# XML helpers #
|
|
|
|
###############
|
|
|
|
|
|
|
|
def _get_source(self):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2015-06-02 20:41:21 +08:00
|
|
|
Convenience function, try to return the relevant <source> value
|
2013-03-18 05:06:52 +08:00
|
|
|
per the network type.
|
|
|
|
"""
|
|
|
|
if self.type == self.TYPE_VIRTUAL:
|
2019-02-05 06:35:47 +08:00
|
|
|
return self.network
|
2013-03-18 05:06:52 +08:00
|
|
|
if self.type == self.TYPE_BRIDGE:
|
2019-02-05 06:35:47 +08:00
|
|
|
return self.bridge
|
2016-07-08 23:58:28 +08:00
|
|
|
if self.type == self.TYPE_DIRECT:
|
2019-02-05 06:35:47 +08:00
|
|
|
return self.source_dev
|
2019-06-11 04:08:45 +08:00
|
|
|
return None
|
2013-09-24 22:00:01 +08:00
|
|
|
def _set_source(self, newsource):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2015-06-02 20:41:21 +08:00
|
|
|
Convenience function, try to set the relevant <source> value
|
2013-03-18 05:06:52 +08:00
|
|
|
per the network type
|
|
|
|
"""
|
2019-02-05 06:35:47 +08:00
|
|
|
self.bridge = None
|
|
|
|
self.network = None
|
|
|
|
self.source_dev = None
|
2013-09-24 22:00:01 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
if self.type == self.TYPE_VIRTUAL:
|
2019-02-05 06:35:47 +08:00
|
|
|
self.network = newsource
|
2013-03-18 05:06:52 +08:00
|
|
|
elif self.type == self.TYPE_BRIDGE:
|
2019-02-05 06:35:47 +08:00
|
|
|
self.bridge = newsource
|
2016-07-08 23:58:28 +08:00
|
|
|
elif self.type == self.TYPE_DIRECT:
|
2019-02-05 06:35:47 +08:00
|
|
|
self.source_dev = newsource
|
2013-09-24 22:00:01 +08:00
|
|
|
source = property(_get_source, _set_source)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-07-16 01:56:49 +08:00
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
##################
|
|
|
|
# XML properties #
|
|
|
|
##################
|
2013-07-16 01:56:49 +08:00
|
|
|
|
2013-07-17 00:48:52 +08:00
|
|
|
_XML_PROP_ORDER = [
|
2019-02-05 06:35:47 +08:00
|
|
|
"bridge", "network", "source_dev", "source_type", "source_path",
|
2016-08-27 13:41:38 +08:00
|
|
|
"source_mode", "portgroup", "macaddr", "target_dev", "model",
|
2018-06-06 22:07:46 +08:00
|
|
|
"virtualport", "filterref", "rom_bar", "rom_file", "mtu_size"]
|
2013-07-16 01:56:49 +08:00
|
|
|
|
2019-02-05 06:35:47 +08:00
|
|
|
bridge = XMLProperty("./source/@bridge")
|
|
|
|
network = XMLProperty("./source/@network")
|
|
|
|
source_dev = XMLProperty("./source/@dev")
|
2013-09-24 22:00:01 +08:00
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
virtualport = XMLChildProperty(_VirtualPort, is_single=True)
|
2018-09-02 08:22:47 +08:00
|
|
|
type = XMLProperty("./@type")
|
2017-03-06 03:43:31 +08:00
|
|
|
trustGuestRxFilters = XMLProperty("./@trustGuestRxFilters", is_yesno=True)
|
2013-07-16 01:56:49 +08:00
|
|
|
|
2018-09-04 04:44:18 +08:00
|
|
|
macaddr = XMLProperty("./mac/@address")
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2016-08-27 13:41:38 +08:00
|
|
|
source_type = XMLProperty("./source/@type")
|
|
|
|
source_path = XMLProperty("./source/@path")
|
2018-09-02 08:22:47 +08:00
|
|
|
source_mode = XMLProperty("./source/@mode")
|
2014-06-01 02:30:07 +08:00
|
|
|
portgroup = XMLProperty("./source/@portgroup")
|
2013-09-20 01:27:30 +08:00
|
|
|
model = XMLProperty("./model/@type")
|
|
|
|
target_dev = XMLProperty("./target/@dev")
|
|
|
|
filterref = XMLProperty("./filterref/@filter")
|
2015-11-19 02:59:15 +08:00
|
|
|
link_state = XMLProperty("./link/@state")
|
2013-07-24 20:46:55 +08:00
|
|
|
|
2014-02-01 20:48:04 +08:00
|
|
|
driver_name = XMLProperty("./driver/@name")
|
|
|
|
driver_queues = XMLProperty("./driver/@queues", is_int=True)
|
2013-07-24 20:46:55 +08:00
|
|
|
|
2016-01-20 23:53:23 +08:00
|
|
|
rom_bar = XMLProperty("./rom/@bar", is_onoff=True)
|
|
|
|
rom_file = XMLProperty("./rom/@file")
|
|
|
|
|
2018-06-06 22:07:46 +08:00
|
|
|
mtu_size = XMLProperty("./mtu/@size", is_int=True)
|
|
|
|
|
2016-01-20 23:53:23 +08:00
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
#############
|
|
|
|
# Build API #
|
|
|
|
#############
|
|
|
|
|
2014-02-06 06:45:46 +08:00
|
|
|
def set_default_source(self):
|
2020-02-03 08:43:06 +08:00
|
|
|
if self.conn.is_qemu_unprivileged() or self.conn.is_test():
|
2014-02-06 06:45:46 +08:00
|
|
|
self.type = self.TYPE_USER
|
2020-01-26 08:03:28 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
nettype = DeviceInterface.TYPE_BRIDGE
|
2020-01-27 19:59:17 +08:00
|
|
|
source = DeviceInterface.default_bridge(self.conn)
|
2020-01-26 08:03:28 +08:00
|
|
|
if not source:
|
|
|
|
nettype = DeviceInterface.TYPE_VIRTUAL
|
|
|
|
source = "default"
|
|
|
|
|
|
|
|
self.type = nettype
|
|
|
|
self.source = source
|
2018-09-02 08:22:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
##################
|
|
|
|
# Default config #
|
|
|
|
##################
|
|
|
|
|
|
|
|
@staticmethod
|
2018-09-02 08:36:53 +08:00
|
|
|
def default_model(guest):
|
2018-09-02 08:22:47 +08:00
|
|
|
if not guest.os.is_hvm():
|
|
|
|
return None
|
|
|
|
if guest.supports_virtionet():
|
|
|
|
return "virtio"
|
|
|
|
if guest.os.is_q35():
|
|
|
|
return "e1000e"
|
2018-09-30 02:31:55 +08:00
|
|
|
if not guest.os.is_x86():
|
|
|
|
return None
|
2018-09-02 08:22:47 +08:00
|
|
|
|
|
|
|
prefs = ["e1000", "rtl8139", "ne2k_pci", "pcnet"]
|
|
|
|
supported_models = guest.osinfo.supported_netmodels()
|
|
|
|
for pref in prefs:
|
|
|
|
if pref in supported_models:
|
|
|
|
return pref
|
2018-09-30 02:31:55 +08:00
|
|
|
return "e1000"
|
2018-09-02 08:22:47 +08:00
|
|
|
|
|
|
|
def set_defaults(self, guest):
|
|
|
|
if not self.type:
|
|
|
|
self.type = self.TYPE_BRIDGE
|
|
|
|
if not self.macaddr:
|
|
|
|
self.macaddr = self.generate_mac(self.conn)
|
2019-02-05 06:35:47 +08:00
|
|
|
if self.type == self.TYPE_BRIDGE and not self.bridge:
|
2020-01-26 08:03:28 +08:00
|
|
|
self.bridge = _default_bridge(self.conn)
|
2018-09-02 08:22:47 +08:00
|
|
|
if not self.model:
|
2018-09-02 08:36:53 +08:00
|
|
|
self.model = self.default_model(guest)
|