2013-09-21 08:40:07 +08:00
|
|
|
#
|
|
|
|
# Copyright 2013 Red Hat, Inc.
|
|
|
|
# Cole Robinson <crobinso@redhat.com>
|
|
|
|
#
|
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-09-21 08:40:07 +08:00
|
|
|
"""
|
|
|
|
Classes for building and installing libvirt <network> XML
|
|
|
|
"""
|
|
|
|
|
2013-10-01 03:06:52 +08:00
|
|
|
import logging
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
import libvirt
|
|
|
|
|
2014-09-13 03:59:22 +08:00
|
|
|
from . import util
|
|
|
|
from .xmlbuilder import XMLBuilder, XMLChildProperty, XMLProperty
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
class _NetworkDHCPRange(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "range"
|
2013-09-21 08:40:07 +08:00
|
|
|
start = XMLProperty("./@start")
|
|
|
|
end = XMLProperty("./@end")
|
|
|
|
|
|
|
|
|
|
|
|
class _NetworkDHCPHost(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "host"
|
2013-09-21 08:40:07 +08:00
|
|
|
macaddr = XMLProperty("./@mac")
|
|
|
|
name = XMLProperty("./@name")
|
|
|
|
ip = XMLProperty("./@ip")
|
|
|
|
|
|
|
|
|
|
|
|
class _NetworkIP(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "ip"
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
family = XMLProperty("./@family")
|
|
|
|
address = XMLProperty("./@address")
|
|
|
|
prefix = XMLProperty("./@prefix", is_int=True)
|
|
|
|
netmask = XMLProperty("./@netmask")
|
|
|
|
|
|
|
|
tftp = XMLProperty("./tftp/@root")
|
|
|
|
bootp_file = XMLProperty("./dhcp/bootp/@file")
|
|
|
|
bootp_server = XMLProperty("./dhcp/bootp/@server")
|
|
|
|
|
|
|
|
ranges = XMLChildProperty(_NetworkDHCPRange, relative_xpath="./dhcp")
|
|
|
|
hosts = XMLChildProperty(_NetworkDHCPHost, relative_xpath="./dhcp")
|
|
|
|
|
|
|
|
|
|
|
|
class _NetworkRoute(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "route"
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
family = XMLProperty("./@family")
|
|
|
|
address = XMLProperty("./@address")
|
|
|
|
prefix = XMLProperty("./@prefix", is_int=True)
|
|
|
|
gateway = XMLProperty("./@gateway")
|
2013-09-23 21:10:43 +08:00
|
|
|
netmask = XMLProperty("./@netmask")
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
|
2017-03-31 20:12:00 +08:00
|
|
|
class _NetworkForwardPf(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "pf"
|
2017-03-31 20:12:00 +08:00
|
|
|
dev = XMLProperty("./@dev")
|
|
|
|
|
|
|
|
|
2017-09-22 19:39:09 +08:00
|
|
|
class _NetworkForwardAddress(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "address"
|
2017-09-22 19:39:09 +08:00
|
|
|
type = XMLProperty("./@type")
|
|
|
|
domain = XMLProperty("./@domain", is_int=True)
|
|
|
|
bus = XMLProperty("./@bus", is_int=True)
|
|
|
|
slot = XMLProperty("./@slot", is_int=True)
|
|
|
|
function = XMLProperty("./@function", is_int=True)
|
|
|
|
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
class _NetworkForward(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "forward"
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
mode = XMLProperty("./@mode")
|
|
|
|
dev = XMLProperty("./@dev")
|
2017-03-31 20:12:00 +08:00
|
|
|
managed = XMLProperty("./@managed")
|
|
|
|
pf = XMLChildProperty(_NetworkForwardPf)
|
2017-09-22 19:39:09 +08:00
|
|
|
vfs = XMLChildProperty(_NetworkForwardAddress)
|
2017-03-31 20:12:00 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
def pretty_desc(self):
|
|
|
|
return Network.pretty_forward_desc(self.mode, self.dev)
|
|
|
|
|
|
|
|
|
2014-06-25 18:27:00 +08:00
|
|
|
class _NetworkBandwidth(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "bandwidth"
|
2014-06-25 18:27:00 +08:00
|
|
|
|
|
|
|
inbound_average = XMLProperty("./inbound/@average")
|
|
|
|
inbound_peak = XMLProperty("./inbound/@peak")
|
|
|
|
inbound_burst = XMLProperty("./inbound/@burst")
|
|
|
|
inbound_floor = XMLProperty("./inbound/@floor")
|
|
|
|
|
|
|
|
outbound_average = XMLProperty("./outbound/@average")
|
|
|
|
outbound_peak = XMLProperty("./outbound/@peak")
|
|
|
|
outbound_burst = XMLProperty("./outbound/@burst")
|
|
|
|
|
|
|
|
def is_inbound(self):
|
2015-07-10 20:07:02 +08:00
|
|
|
return bool(self.inbound_average or self.inbound_peak or
|
|
|
|
self.inbound_burst or self.inbound_floor)
|
2014-06-25 18:27:00 +08:00
|
|
|
|
|
|
|
def is_outbound(self):
|
2015-07-10 20:07:02 +08:00
|
|
|
return bool(self.outbound_average or self.outbound_peak or
|
|
|
|
self.outbound_burst)
|
2014-06-25 18:27:00 +08:00
|
|
|
|
|
|
|
def pretty_desc(self, inbound=True, outbound=True):
|
|
|
|
items_in = [(self.inbound_average, _("Average"), "KiB/s"),
|
|
|
|
(self.inbound_peak, _("Peak"), "KiB"),
|
|
|
|
(self.inbound_burst, _("Burst"), "KiB/s"),
|
|
|
|
(self.inbound_floor, _("Floor"), "KiB/s")]
|
|
|
|
|
|
|
|
items_out = [(self.outbound_average, _("Average"), "KiB/s"),
|
|
|
|
(self.outbound_peak, _("Peak"), "KiB"),
|
|
|
|
(self.outbound_burst, _("Burst"), "KiB/s")]
|
|
|
|
|
|
|
|
def stringify_items(items):
|
|
|
|
return ", ".join(["%s: %s %s" % (desc, val, unit)
|
|
|
|
for val, desc, unit in items if val])
|
|
|
|
|
|
|
|
ret = ""
|
|
|
|
show_name = inbound and outbound
|
|
|
|
|
|
|
|
if inbound:
|
|
|
|
if show_name:
|
|
|
|
ret += _("Inbound: ")
|
|
|
|
ret += stringify_items(items_in)
|
|
|
|
|
|
|
|
if outbound:
|
|
|
|
if ret:
|
|
|
|
ret += "\n"
|
|
|
|
if show_name:
|
|
|
|
ret += _("Outbound: ")
|
|
|
|
ret += stringify_items(items_out)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2014-06-01 02:20:56 +08:00
|
|
|
class _NetworkPortgroup(XMLBuilder):
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "portgroup"
|
2014-06-01 02:20:56 +08:00
|
|
|
|
|
|
|
name = XMLProperty("./@name")
|
|
|
|
default = XMLProperty("./@default", is_yesno=True)
|
|
|
|
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
class Network(XMLBuilder):
|
|
|
|
"""
|
|
|
|
Top level class for <network> object XML
|
|
|
|
"""
|
|
|
|
@staticmethod
|
|
|
|
def pretty_forward_desc(mode, dev):
|
|
|
|
if mode or dev:
|
|
|
|
if not mode or mode == "nat":
|
|
|
|
if dev:
|
|
|
|
desc = _("NAT to %s") % dev
|
|
|
|
else:
|
|
|
|
desc = _("NAT")
|
|
|
|
elif mode == "route":
|
|
|
|
if dev:
|
|
|
|
desc = _("Route to %s") % dev
|
|
|
|
else:
|
|
|
|
desc = _("Routed network")
|
|
|
|
else:
|
|
|
|
if dev:
|
2016-02-05 23:18:16 +08:00
|
|
|
desc = (_("%(mode)s to %(device)s") %
|
2017-08-05 14:39:32 +08:00
|
|
|
{"mode": mode, "device": dev})
|
2013-09-21 08:40:07 +08:00
|
|
|
else:
|
2016-02-05 23:18:16 +08:00
|
|
|
desc = _("%s network") % mode.capitalize()
|
2013-09-21 08:40:07 +08:00
|
|
|
else:
|
|
|
|
desc = _("Isolated network, internal and host routing only")
|
|
|
|
|
|
|
|
return desc
|
|
|
|
|
|
|
|
|
2013-09-23 05:04:22 +08:00
|
|
|
###################
|
|
|
|
# Helper routines #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def can_pxe(self):
|
|
|
|
forward = self.forward.mode
|
|
|
|
if forward and forward != "nat":
|
|
|
|
return True
|
|
|
|
for ip in self.ips:
|
|
|
|
if ip.bootp_file:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
######################
|
|
|
|
# Validation helpers #
|
|
|
|
######################
|
|
|
|
|
2014-01-18 07:40:30 +08:00
|
|
|
def _validate_name(self, name):
|
|
|
|
util.validate_name(_("Network"), name)
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
try:
|
|
|
|
self.conn.networkLookupByName(name)
|
|
|
|
except libvirt.libvirtError:
|
|
|
|
return
|
|
|
|
raise ValueError(_("Name '%s' already in use by another network." %
|
|
|
|
name))
|
|
|
|
|
|
|
|
|
|
|
|
##################
|
|
|
|
# XML properties #
|
|
|
|
##################
|
|
|
|
|
2018-03-21 22:53:34 +08:00
|
|
|
XML_NAME = "network"
|
2017-03-23 23:06:56 +08:00
|
|
|
_XML_PROP_ORDER = ["ipv6", "name", "uuid", "forward", "virtualport_type",
|
2013-09-21 08:40:07 +08:00
|
|
|
"bridge", "stp", "delay", "domain_name",
|
2014-06-25 18:27:00 +08:00
|
|
|
"macaddr", "ips", "routes", "bandwidth"]
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
ipv6 = XMLProperty("./@ipv6", is_yesno=True)
|
2014-01-18 07:40:30 +08:00
|
|
|
name = XMLProperty("./name", validate_cb=_validate_name)
|
2017-12-15 01:10:28 +08:00
|
|
|
uuid = XMLProperty("./uuid")
|
2013-09-21 08:40:07 +08:00
|
|
|
|
2017-03-23 23:06:56 +08:00
|
|
|
virtualport_type = XMLProperty("./virtualport/@type")
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
# Not entirely correct, there can be multiple routes
|
|
|
|
forward = XMLChildProperty(_NetworkForward, is_single=True)
|
|
|
|
|
|
|
|
domain_name = XMLProperty("./domain/@name")
|
|
|
|
|
|
|
|
bridge = XMLProperty("./bridge/@name")
|
|
|
|
stp = XMLProperty("./bridge/@stp", is_onoff=True)
|
|
|
|
delay = XMLProperty("./bridge/@delay", is_int=True)
|
|
|
|
macaddr = XMLProperty("./mac/@address")
|
|
|
|
|
2014-06-01 02:20:56 +08:00
|
|
|
portgroups = XMLChildProperty(_NetworkPortgroup)
|
2013-09-21 08:40:07 +08:00
|
|
|
ips = XMLChildProperty(_NetworkIP)
|
|
|
|
routes = XMLChildProperty(_NetworkRoute)
|
2014-06-25 18:27:00 +08:00
|
|
|
bandwidth = XMLChildProperty(_NetworkBandwidth, is_single=True)
|
2013-09-21 08:40:07 +08:00
|
|
|
|
2014-06-01 02:20:56 +08:00
|
|
|
|
2013-10-01 03:06:52 +08:00
|
|
|
##################
|
|
|
|
# build routines #
|
|
|
|
##################
|
|
|
|
|
|
|
|
def install(self, start=True, autostart=True):
|
2018-09-01 04:52:02 +08:00
|
|
|
xml = self.get_xml()
|
2013-10-01 03:06:52 +08:00
|
|
|
logging.debug("Creating virtual network '%s' with xml:\n%s",
|
|
|
|
self.name, xml)
|
|
|
|
|
|
|
|
net = self.conn.networkDefineXML(xml)
|
|
|
|
try:
|
|
|
|
if start:
|
|
|
|
net.create()
|
|
|
|
if autostart:
|
|
|
|
net.setAutostart(autostart)
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2013-10-01 03:06:52 +08:00
|
|
|
net.undefine()
|
|
|
|
raise
|
|
|
|
|
|
|
|
return net
|