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
|
|
|
# Jeremy Katz <katzj@redhat.com>
|
|
|
|
#
|
2018-03-21 03:00:02 +08:00
|
|
|
# This work is licensed under the GNU GPLv2.
|
|
|
|
# See the COPYING file in the top-level directory.
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
import logging
|
2016-08-25 04:14:33 +08:00
|
|
|
import os
|
2013-07-12 21:54:01 +08:00
|
|
|
import random
|
|
|
|
|
2018-03-21 00:27:37 +08:00
|
|
|
from .. import util
|
2018-03-21 00:18:35 +08:00
|
|
|
from .device import Device
|
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
|
|
|
|
|
|
|
|
The OUI list is available at http://standards.ieee.org/regauth/oui/oui.txt.
|
|
|
|
|
|
|
|
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"
|
|
|
|
if not os.path.exists(route_file):
|
|
|
|
logging.debug("route_file=%s does not exist", route_file)
|
|
|
|
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()
|
|
|
|
if len(info) != 11:
|
|
|
|
logging.debug("Unexpected field count=%s when parsing %s",
|
|
|
|
len(info), route_file)
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
route = int(info[1], 16)
|
|
|
|
if route == 0:
|
|
|
|
return info[0]
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _default_bridge(conn):
|
|
|
|
if "VIRTINST_TEST_SUITE" in os.environ:
|
|
|
|
return "eth0"
|
|
|
|
|
|
|
|
if conn.is_remote():
|
|
|
|
return None
|
|
|
|
|
|
|
|
dev = _default_route()
|
|
|
|
if not dev:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# New style peth0 == phys dev, eth0 == bridge, eth0 == default route
|
|
|
|
if os.path.exists("/sys/class/net/%s/bridge" % dev):
|
|
|
|
return dev
|
|
|
|
|
|
|
|
# Old style, peth0 == phys dev, eth0 == netloop, xenbr0 == bridge,
|
|
|
|
# vif0.0 == netloop enslaved, eth0 == default route
|
|
|
|
try:
|
|
|
|
defn = int(dev[-1])
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
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)):
|
|
|
|
return "xenbr%d"
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _default_network(conn):
|
|
|
|
ret = _default_bridge(conn)
|
|
|
|
if ret:
|
|
|
|
return ["bridge", ret]
|
|
|
|
return ["network", "default"]
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
network_types = [TYPE_BRIDGE, TYPE_VIRTUAL, TYPE_USER, TYPE_ETHERNET,
|
|
|
|
TYPE_DIRECT]
|
|
|
|
|
2013-07-12 21:54:01 +08:00
|
|
|
@staticmethod
|
2013-03-18 05:06:52 +08:00
|
|
|
def get_network_type_desc(net_type):
|
|
|
|
"""
|
|
|
|
Return human readable description for passed network type
|
|
|
|
"""
|
|
|
|
desc = net_type.capitalize()
|
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
if net_type == DeviceInterface.TYPE_BRIDGE:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Shared physical device")
|
2018-03-21 00:18:35 +08:00
|
|
|
elif net_type == DeviceInterface.TYPE_VIRTUAL:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Virtual networking")
|
2018-03-21 00:18:35 +08:00
|
|
|
elif net_type == DeviceInterface.TYPE_USER:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Usermode networking")
|
|
|
|
|
|
|
|
return desc
|
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-03-21 00:18:35 +08:00
|
|
|
ret = DeviceInterface.is_conflict_net(conn, mac)
|
2013-07-12 21:54:01 +08:00
|
|
|
if ret[1] is None:
|
|
|
|
return mac
|
|
|
|
|
|
|
|
logging.debug("Failed to generate non-conflicting MAC")
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_conflict_net(conn, searchmac):
|
|
|
|
"""
|
2018-02-14 20:17:31 +08:00
|
|
|
:returns: a two element tuple:
|
2013-07-12 21:54:01 +08:00
|
|
|
first element is True if fatal collision occured
|
|
|
|
second element is a string description of the collision.
|
|
|
|
|
|
|
|
Non fatal collisions (mac addr collides with inactive guest) will
|
|
|
|
return (False, "description of collision")
|
|
|
|
"""
|
|
|
|
if searchmac is None:
|
|
|
|
return (False, None)
|
|
|
|
|
|
|
|
vms = conn.fetch_all_guests()
|
|
|
|
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():
|
|
|
|
return (True, _("The MAC address '%s' is in use "
|
|
|
|
"by another virtual machine.") % searchmac)
|
|
|
|
return (False, None)
|
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-09-11 23:47:09 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-03-21 00:18:35 +08:00
|
|
|
Device.__init__(self, *args, **kwargs)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
self._random_mac = None
|
|
|
|
self._default_bridge = None
|
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
|
|
|
|
###############
|
|
|
|
# XML helpers #
|
|
|
|
###############
|
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def _generate_default_bridge(self):
|
|
|
|
ret = self._default_bridge
|
|
|
|
if ret is None:
|
|
|
|
ret = False
|
2016-08-25 04:14:33 +08:00
|
|
|
default = _default_bridge(self.conn)
|
2013-03-18 05:06:52 +08:00
|
|
|
if default:
|
2014-10-28 07:26:31 +08:00
|
|
|
ret = default
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
self._default_bridge = ret
|
|
|
|
return ret or None
|
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
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):
|
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:
|
2013-09-24 22:00:01 +08:00
|
|
|
return self._network
|
2013-03-18 05:06:52 +08:00
|
|
|
if self.type == self.TYPE_BRIDGE:
|
2013-09-24 22:00:01 +08:00
|
|
|
return self._bridge
|
2016-07-08 23:58:28 +08:00
|
|
|
if self.type == self.TYPE_DIRECT:
|
2013-09-24 22:00:01 +08:00
|
|
|
return self._source_dev
|
2016-07-08 23:58:28 +08:00
|
|
|
if self.type == self.TYPE_USER or self.type == self.TYPE_ETHERNET:
|
2013-03-18 05:06:52 +08:00
|
|
|
return None
|
2013-09-24 22:00:01 +08:00
|
|
|
return self._network or self._bridge or self._source_dev
|
|
|
|
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
|
|
|
|
"""
|
2013-09-24 22:00:01 +08:00
|
|
|
self._bridge = None
|
|
|
|
self._network = None
|
|
|
|
self._source_dev = None
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
if self.type == self.TYPE_VIRTUAL:
|
2013-09-24 22:00:01 +08:00
|
|
|
self._network = newsource
|
2013-03-18 05:06:52 +08:00
|
|
|
elif self.type == self.TYPE_BRIDGE:
|
2013-09-24 22:00:01 +08:00
|
|
|
self._bridge = newsource
|
2016-07-08 23:58:28 +08:00
|
|
|
elif self.type == self.TYPE_DIRECT:
|
2013-09-24 22:00:01 +08:00
|
|
|
self._source_dev = newsource
|
|
|
|
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 = [
|
2016-08-27 13:41:38 +08:00
|
|
|
"_bridge", "_network", "_source_dev", "source_type", "source_path",
|
|
|
|
"source_mode", "portgroup", "macaddr", "target_dev", "model",
|
|
|
|
"virtualport", "filterref", "rom_bar", "rom_file"]
|
2013-07-16 01:56:49 +08:00
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
_bridge = XMLProperty("./source/@bridge", default_cb=_get_default_bridge)
|
|
|
|
_network = XMLProperty("./source/@network")
|
|
|
|
_source_dev = XMLProperty("./source/@dev")
|
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
virtualport = XMLChildProperty(_VirtualPort, is_single=True)
|
2013-09-20 01:27:30 +08:00
|
|
|
type = XMLProperty("./@type",
|
2013-07-16 02:14:05 +08:00
|
|
|
default_cb=lambda s: s.TYPE_BRIDGE)
|
2017-03-06 03:43:31 +08:00
|
|
|
trustGuestRxFilters = XMLProperty("./@trustGuestRxFilters", is_yesno=True)
|
2013-07-16 01:56:49 +08:00
|
|
|
|
2013-09-20 01:27:30 +08:00
|
|
|
macaddr = XMLProperty("./mac/@address",
|
2013-07-16 01:56:49 +08:00
|
|
|
set_converter=_validate_mac,
|
|
|
|
default_cb=_get_default_mac)
|
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")
|
2013-09-20 01:27:30 +08:00
|
|
|
source_mode = XMLProperty("./source/@mode",
|
2013-07-16 01:56:49 +08:00
|
|
|
default_cb=_default_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")
|
|
|
|
|
|
|
|
|
2013-09-24 22:00:01 +08:00
|
|
|
#############
|
|
|
|
# Build API #
|
|
|
|
#############
|
|
|
|
|
|
|
|
def setup(self, meter=None):
|
|
|
|
ignore = meter
|
|
|
|
if not self.macaddr:
|
|
|
|
return
|
|
|
|
|
|
|
|
ret, msg = self.is_conflict_net(self.conn, self.macaddr)
|
|
|
|
if msg is None:
|
|
|
|
return
|
|
|
|
if ret is False:
|
|
|
|
logging.warning(msg)
|
|
|
|
else:
|
|
|
|
raise RuntimeError(msg)
|
|
|
|
|
2014-02-06 06:45:46 +08:00
|
|
|
def set_default_source(self):
|
|
|
|
if (self.conn.is_qemu_session() or self.conn.is_test()):
|
|
|
|
self.type = self.TYPE_USER
|
|
|
|
else:
|
2016-08-25 04:14:33 +08:00
|
|
|
self.type, self.source = _default_network(self.conn)
|