2014-06-25 18:36:46 +08:00
|
|
|
# Copyright (C) 2006, 2013-2014 Red Hat, Inc.
|
2007-03-22 00:28:36 +08:00
|
|
|
# Copyright (C) 2006 Daniel P. Berrange <berrange@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-07-12 22:53:30 +08:00
|
|
|
|
2017-10-11 19:35:48 +08:00
|
|
|
import ipaddress
|
2018-09-05 07:02:40 +08:00
|
|
|
import logging
|
2013-07-12 22:53:30 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
from virtinst import Network
|
2013-07-12 22:53:30 +08:00
|
|
|
|
2014-09-13 04:10:45 +08:00
|
|
|
from .libvirtobject import vmmLibvirtObject
|
2007-03-22 00:28:36 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
def _make_addr_str(addrStr, prefix, netmaskStr):
|
|
|
|
if prefix:
|
2017-10-11 19:35:48 +08:00
|
|
|
return str(
|
|
|
|
ipaddress.ip_network(
|
2018-01-28 03:19:12 +08:00
|
|
|
str("{}/{}").format(addrStr, prefix), strict=False
|
2017-10-11 19:35:48 +08:00
|
|
|
)
|
|
|
|
)
|
2013-09-21 08:40:07 +08:00
|
|
|
elif netmaskStr:
|
2018-01-28 03:19:12 +08:00
|
|
|
netmask = ipaddress.ip_address(str((netmaskStr)))
|
|
|
|
network = ipaddress.ip_address(str((addrStr)))
|
2017-10-11 19:35:48 +08:00
|
|
|
return str(
|
|
|
|
ipaddress.ip_network(
|
2018-01-28 03:19:12 +08:00
|
|
|
str("{}/{}").format(network, netmask), strict=False
|
2017-10-11 19:35:48 +08:00
|
|
|
)
|
|
|
|
)
|
2013-09-21 08:40:07 +08:00
|
|
|
else:
|
2018-01-28 03:19:12 +08:00
|
|
|
return str(ipaddress.ip_network(str(addrStr), strict=False))
|
2013-09-21 08:40:07 +08:00
|
|
|
|
2009-12-02 00:49:08 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
class vmmNetwork(vmmLibvirtObject):
|
2013-07-07 20:42:57 +08:00
|
|
|
def __init__(self, conn, backend, key):
|
2013-09-23 20:34:50 +08:00
|
|
|
vmmLibvirtObject.__init__(self, conn, backend, key, Network)
|
2013-07-07 20:05:23 +08:00
|
|
|
|
2018-09-05 07:02:40 +08:00
|
|
|
self._leases = None
|
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
##########################
|
|
|
|
# Required class methods #
|
|
|
|
##########################
|
|
|
|
|
2015-04-10 21:15:44 +08:00
|
|
|
def _conn_tick_poll_param(self):
|
|
|
|
return "pollnet"
|
|
|
|
def class_name(self):
|
|
|
|
return "network"
|
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
def _XMLDesc(self, flags):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._backend.XMLDesc(flags)
|
2010-12-10 01:37:48 +08:00
|
|
|
def _define(self, xml):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self.conn.define_network(xml)
|
2014-02-12 02:19:15 +08:00
|
|
|
def _using_events(self):
|
|
|
|
return self.conn.using_network_events
|
2015-04-10 06:02:42 +08:00
|
|
|
def _check_supports_isactive(self):
|
2019-06-08 04:06:52 +08:00
|
|
|
return self.conn.support.net_isactive(self._backend)
|
2015-04-10 06:02:42 +08:00
|
|
|
def _get_backend_status(self):
|
|
|
|
return self._backend_get_active()
|
|
|
|
|
2015-04-10 21:15:44 +08:00
|
|
|
def tick(self, stats_update=True):
|
|
|
|
ignore = stats_update
|
2015-04-10 07:03:27 +08:00
|
|
|
self._refresh_status()
|
2010-12-10 01:37:48 +08:00
|
|
|
|
2015-04-11 02:08:25 +08:00
|
|
|
def _init_libvirt_state(self):
|
|
|
|
self.tick()
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
###########
|
|
|
|
# Actions #
|
|
|
|
###########
|
|
|
|
|
2015-04-10 06:27:45 +08:00
|
|
|
@vmmLibvirtObject.lifecycle_action
|
2007-03-30 09:23:17 +08:00
|
|
|
def start(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.create()
|
2007-03-30 09:23:17 +08:00
|
|
|
|
2015-04-10 06:27:45 +08:00
|
|
|
@vmmLibvirtObject.lifecycle_action
|
2007-04-04 02:01:45 +08:00
|
|
|
def stop(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.destroy()
|
2007-04-04 02:01:45 +08:00
|
|
|
|
2015-04-10 06:27:45 +08:00
|
|
|
@vmmLibvirtObject.lifecycle_action
|
2013-10-01 03:23:14 +08:00
|
|
|
def delete(self, force=True):
|
|
|
|
ignore = force
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.undefine()
|
|
|
|
self._backend = None
|
2007-04-04 02:01:45 +08:00
|
|
|
|
2015-04-10 06:02:42 +08:00
|
|
|
|
|
|
|
###############################
|
|
|
|
# XML/config handling parsing #
|
|
|
|
###############################
|
|
|
|
|
2007-03-31 03:02:09 +08:00
|
|
|
def get_autostart(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._backend.autostart()
|
2013-09-21 08:40:07 +08:00
|
|
|
def set_autostart(self, value):
|
|
|
|
self._backend.setAutostart(value)
|
2013-07-07 20:05:23 +08:00
|
|
|
|
2018-09-05 07:02:40 +08:00
|
|
|
def refresh_dhcp_leases(self):
|
|
|
|
try:
|
|
|
|
self._leases = self._backend.DHCPLeases()
|
|
|
|
except Exception as e:
|
|
|
|
logging.debug("Error getting %s DHCP leases: %s", self, str(e))
|
|
|
|
self._leases = []
|
|
|
|
|
|
|
|
def get_dhcp_leases(self):
|
|
|
|
if self._leases is None:
|
|
|
|
self.refresh_dhcp_leases()
|
|
|
|
return self._leases
|
|
|
|
|
2014-06-25 18:36:46 +08:00
|
|
|
def set_qos(self, **kwargs):
|
2015-04-11 05:23:11 +08:00
|
|
|
xmlobj = self._make_xmlobj_to_define()
|
2015-04-08 02:53:28 +08:00
|
|
|
q = xmlobj.bandwidth
|
2014-06-25 18:36:46 +08:00
|
|
|
for key, val in kwargs.items():
|
|
|
|
setattr(q, key, val)
|
|
|
|
|
2015-04-11 05:23:11 +08:00
|
|
|
self._redefine_xmlobj(xmlobj)
|
2014-06-25 18:36:46 +08:00
|
|
|
return self.is_active()
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
def get_uuid(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().uuid
|
2013-09-21 08:40:07 +08:00
|
|
|
def get_bridge_device(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().bridge
|
2013-09-21 08:40:07 +08:00
|
|
|
def get_name_domain(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().domain_name
|
2013-09-21 08:40:07 +08:00
|
|
|
def get_ipv6_enabled(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().ipv6
|
2013-09-21 08:40:07 +08:00
|
|
|
def get_ipv4_forward_mode(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().forward.mode
|
2014-06-25 18:36:46 +08:00
|
|
|
def get_qos(self):
|
|
|
|
return self.get_xmlobj().bandwidth
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
def can_pxe(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().can_pxe()
|
2013-09-21 08:40:07 +08:00
|
|
|
|
|
|
|
def _get_static_route(self, family):
|
2013-09-23 20:34:50 +08:00
|
|
|
xmlobj = self.get_xmlobj()
|
2013-09-21 08:40:07 +08:00
|
|
|
route = None
|
|
|
|
for r in xmlobj.routes:
|
|
|
|
if (r.family == family or (family == "ipv4" and not r.family)):
|
|
|
|
route = r
|
2013-05-15 00:15:02 +08:00
|
|
|
break
|
2013-09-21 08:40:07 +08:00
|
|
|
if not route:
|
|
|
|
return [None, None]
|
2013-05-15 00:15:02 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
routeAddr = _make_addr_str(route.address, route.prefix, route.netmask)
|
2018-01-28 03:19:12 +08:00
|
|
|
routeVia = str(ipaddress.ip_address(str(route.gateway)))
|
2013-05-15 00:15:02 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
if not routeAddr or not routeVia:
|
|
|
|
return [None, None]
|
|
|
|
return [routeAddr, routeVia]
|
|
|
|
|
|
|
|
def _get_network(self, family):
|
2013-03-30 02:38:28 +08:00
|
|
|
dhcpstart = None
|
|
|
|
dhcpend = None
|
|
|
|
|
2013-09-23 20:34:50 +08:00
|
|
|
xmlobj = self.get_xmlobj()
|
2013-09-21 08:40:07 +08:00
|
|
|
ip = None
|
|
|
|
for i in xmlobj.ips:
|
|
|
|
if (i.family == family or
|
|
|
|
(family == "ipv4" and not i.family)):
|
|
|
|
if i.ranges:
|
|
|
|
ip = i
|
|
|
|
dhcpstart = i.ranges[0].start
|
|
|
|
dhcpend = i.ranges[0].end
|
2013-03-30 02:38:28 +08:00
|
|
|
break
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
if not ip:
|
|
|
|
for i in xmlobj.ips:
|
|
|
|
if (i.family == family or
|
|
|
|
(family == "ipv4" and not i.family)):
|
|
|
|
ip = i
|
2013-03-30 02:38:28 +08:00
|
|
|
break
|
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
ret = None
|
|
|
|
if ip:
|
|
|
|
ret = _make_addr_str(ip.address, ip.prefix, ip.netmask)
|
2013-03-30 02:38:28 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
dhcp = [None, None]
|
2013-03-30 02:38:28 +08:00
|
|
|
if dhcpstart and dhcpend:
|
2018-01-28 03:19:12 +08:00
|
|
|
dhcp = [str(ipaddress.ip_address(str(dhcpstart))),
|
|
|
|
str(ipaddress.ip_address(str(dhcpend)))]
|
2013-09-21 08:40:07 +08:00
|
|
|
return [ret, dhcp]
|
2007-04-04 02:01:45 +08:00
|
|
|
|
2013-09-21 08:40:07 +08:00
|
|
|
def get_ipv4_network(self):
|
|
|
|
ret = self._get_network("ipv4")
|
|
|
|
return ret + [self._get_static_route("ipv4")]
|
|
|
|
def get_ipv6_network(self):
|
|
|
|
ret = self._get_network("ipv6")
|
|
|
|
return ret + [self._get_static_route("ipv6")]
|
2017-09-22 19:39:10 +08:00
|
|
|
|
|
|
|
def get_sriov_vf_networks(self):
|
|
|
|
xmlobj = self.get_xmlobj()
|
|
|
|
pf_name = None
|
|
|
|
vfs = None
|
|
|
|
ret = False
|
|
|
|
if xmlobj.forward.mode == "hostdev":
|
|
|
|
ret = True
|
|
|
|
if xmlobj.forward.pf:
|
|
|
|
pf_name = xmlobj.forward.pf[0].dev
|
|
|
|
vfs = xmlobj.forward.vfs
|
|
|
|
return (ret, pf_name, vfs)
|
2019-06-10 03:29:44 +08:00
|
|
|
|
|
|
|
def pretty_forward_mode(self):
|
|
|
|
mode = self.xmlobj.forward.mode
|
|
|
|
dev = self.xmlobj.forward.dev
|
|
|
|
|
|
|
|
if not mode:
|
|
|
|
return _("Isolated network")
|
|
|
|
|
|
|
|
if 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:
|
|
|
|
modestr = mode.capitalize()
|
|
|
|
if dev:
|
|
|
|
desc = (_("%(mode)s to %(device)s") %
|
|
|
|
{"mode": modestr, "device": dev})
|
|
|
|
else:
|
|
|
|
desc = _("%s network") % modestr
|
|
|
|
|
|
|
|
return desc
|