2007-03-22 00:28:36 +08:00
|
|
|
#
|
2013-05-22 20:59:27 +08:00
|
|
|
# Copyright (C) 2006, 2013 Red Hat, Inc.
|
2007-03-22 00:28:36 +08:00
|
|
|
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2007-11-21 00:12:20 +08:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
# MA 02110-1301 USA.
|
2007-03-22 00:28:36 +08:00
|
|
|
#
|
2013-07-12 22:53:30 +08:00
|
|
|
|
2013-03-30 02:38:28 +08:00
|
|
|
import ipaddr
|
|
|
|
import libxml2
|
2013-07-12 22:53:30 +08:00
|
|
|
|
|
|
|
from virtinst import util
|
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
from virtManager.libvirtobject import vmmLibvirtObject
|
2007-03-22 00:28:36 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
class vmmNetwork(vmmLibvirtObject):
|
2009-12-02 00:49:08 +08:00
|
|
|
@staticmethod
|
|
|
|
def pretty_desc(forward, forwardDev):
|
|
|
|
if forward or forwardDev:
|
|
|
|
if not forward or forward == "nat":
|
|
|
|
if forwardDev:
|
|
|
|
desc = _("NAT to %s") % forwardDev
|
|
|
|
else:
|
|
|
|
desc = _("NAT")
|
|
|
|
elif forward == "route":
|
|
|
|
if forwardDev:
|
|
|
|
desc = _("Route to %s") % forwardDev
|
|
|
|
else:
|
|
|
|
desc = _("Routed network")
|
2009-12-16 00:14:22 +08:00
|
|
|
else:
|
|
|
|
if forwardDev:
|
|
|
|
desc = "%s to %s" % (forward, forwardDev)
|
|
|
|
else:
|
|
|
|
desc = "%s network" % forward.capitalize()
|
2009-12-02 00:49:08 +08:00
|
|
|
else:
|
2013-03-30 02:38:28 +08:00
|
|
|
desc = _("Isolated network, internal and host routing only")
|
2009-12-02 00:49:08 +08:00
|
|
|
|
|
|
|
return desc
|
|
|
|
|
2013-07-07 20:42:57 +08:00
|
|
|
def __init__(self, conn, backend, key):
|
|
|
|
vmmLibvirtObject.__init__(self, conn, backend, key)
|
|
|
|
self._uuid = key
|
|
|
|
self._active = True
|
2013-07-07 20:05:23 +08:00
|
|
|
|
|
|
|
self._support_isactive = None
|
|
|
|
|
|
|
|
self.tick()
|
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
|
|
|
|
# Required class methods
|
|
|
|
def get_name(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._backend.name()
|
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)
|
2010-12-10 01:37:48 +08:00
|
|
|
|
2013-07-07 20:42:57 +08:00
|
|
|
def _set_active(self, state):
|
|
|
|
if state == self._active:
|
2013-07-07 20:05:23 +08:00
|
|
|
return
|
|
|
|
self.idle_emit(state and "started" or "stopped")
|
2013-07-07 20:42:57 +08:00
|
|
|
self._active = state
|
2007-04-04 02:01:45 +08:00
|
|
|
|
2007-03-22 00:28:36 +08:00
|
|
|
def is_active(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._active
|
2007-03-22 00:28:36 +08:00
|
|
|
|
|
|
|
def get_label(self):
|
|
|
|
return self.get_name()
|
|
|
|
|
|
|
|
def get_uuid(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._uuid
|
2007-03-22 00:28:36 +08:00
|
|
|
|
2007-03-28 07:52:00 +08:00
|
|
|
def get_bridge_device(self):
|
2009-11-16 20:14:49 +08:00
|
|
|
try:
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._backend.bridgeName()
|
2009-11-16 20:14:49 +08:00
|
|
|
except:
|
|
|
|
return ""
|
2007-03-28 07:52:00 +08:00
|
|
|
|
2013-07-07 23:06:15 +08:00
|
|
|
def _kick_conn(self):
|
|
|
|
self.conn.schedule_priority_tick(pollnet=True)
|
|
|
|
|
2007-03-30 09:23:17 +08:00
|
|
|
def start(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.create()
|
2013-07-07 23:06:15 +08:00
|
|
|
self._kick_conn()
|
2007-03-30 09:23:17 +08:00
|
|
|
|
2007-04-04 02:01:45 +08:00
|
|
|
def stop(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.destroy()
|
2013-07-07 23:06:15 +08:00
|
|
|
self._kick_conn()
|
2007-04-04 02:01:45 +08:00
|
|
|
|
|
|
|
def delete(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.undefine()
|
|
|
|
self._backend = None
|
2013-07-07 23:06:15 +08:00
|
|
|
self._kick_conn()
|
2007-04-04 02:01:45 +08:00
|
|
|
|
2007-03-30 09:23:17 +08:00
|
|
|
def set_autostart(self, value):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.setAutostart(value)
|
2007-03-30 09:23:17 +08:00
|
|
|
|
2007-03-31 03:02:09 +08:00
|
|
|
def get_autostart(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._backend.autostart()
|
2007-03-31 03:02:09 +08:00
|
|
|
|
2013-07-07 20:05:23 +08:00
|
|
|
def _backend_get_active(self):
|
|
|
|
if self._support_isactive is None:
|
|
|
|
self._support_isactive = self.conn.check_net_support(
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend,
|
2013-07-07 20:05:23 +08:00
|
|
|
self.conn.SUPPORT_NET_ISACTIVE)
|
|
|
|
|
|
|
|
if not self._support_isactive:
|
|
|
|
return True
|
2013-07-07 20:42:57 +08:00
|
|
|
return bool(self._backend.isActive())
|
2013-07-07 20:05:23 +08:00
|
|
|
|
|
|
|
def tick(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._set_active(self._backend_get_active())
|
2013-07-07 20:05:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
########################
|
|
|
|
# XML parsing routines #
|
|
|
|
########################
|
|
|
|
|
2013-05-15 00:15:02 +08:00
|
|
|
def get_ipv4_static_route(self):
|
|
|
|
doc = None
|
|
|
|
ret = None
|
|
|
|
routeAddr = None
|
|
|
|
routeVia = None
|
|
|
|
xml = self.get_xml()
|
|
|
|
doc = libxml2.parseDoc(xml)
|
|
|
|
nodes = doc.xpathEval('//route')
|
|
|
|
for node in nodes:
|
|
|
|
family = node.xpathEval('string(./@family)')
|
|
|
|
if not family or family == 'ipv4':
|
|
|
|
addrStr = node.xpathEval('string(./@address)')
|
|
|
|
netmaskStr = node.xpathEval('string(./@netmask)')
|
|
|
|
gatewayStr = node.xpathEval('string(./@gateway)')
|
|
|
|
prefix = node.xpathEval('string(./@prefix)')
|
|
|
|
if prefix:
|
|
|
|
prefix = int(prefix)
|
|
|
|
routeAddr = str(ipaddr.IPNetwork(str(addrStr) + "/" + str(prefix)).masked())
|
|
|
|
elif netmaskStr:
|
|
|
|
netmask = ipaddr.IPAddress(netmaskStr)
|
|
|
|
network = ipaddr.IPAddress(addrStr)
|
|
|
|
routeAddr = str(ipaddr.IPNetwork(str(network) + "/" + str(netmask)).masked())
|
|
|
|
else:
|
|
|
|
routeAddr = str(ipaddr.IPNetwork(str(addrStr)))
|
|
|
|
routeVia = str(ipaddr.IPAddress(str(gatewayStr)))
|
|
|
|
break
|
|
|
|
|
|
|
|
if doc:
|
|
|
|
doc.freeDoc()
|
|
|
|
if routeAddr and routeVia:
|
|
|
|
ret = [routeAddr, routeVia]
|
|
|
|
else:
|
|
|
|
ret = None
|
|
|
|
return ret
|
|
|
|
|
2007-04-04 02:01:45 +08:00
|
|
|
def get_ipv4_network(self):
|
2013-03-30 02:38:28 +08:00
|
|
|
doc = None
|
|
|
|
ret = None
|
|
|
|
goodNode = None
|
|
|
|
dhcpstart = None
|
|
|
|
dhcpend = None
|
2008-08-16 00:44:45 +08:00
|
|
|
xml = self.get_xml()
|
2013-03-30 02:38:28 +08:00
|
|
|
doc = libxml2.parseDoc(xml)
|
|
|
|
nodes = doc.xpathEval('//ip')
|
|
|
|
for node in nodes:
|
|
|
|
family = node.xpathEval('string(./@family)')
|
|
|
|
if not family or family == 'ipv4':
|
|
|
|
dhcp = node.xpathEval('string(./dhcp)')
|
|
|
|
if dhcp:
|
|
|
|
dhcpstart = node.xpathEval('string(./dhcp/range[1]/@start)')
|
|
|
|
dhcpend = node.xpathEval('string(./dhcp/range[1]/@end)')
|
|
|
|
goodNode = node
|
|
|
|
break
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
if goodNode is None:
|
2013-03-30 02:38:28 +08:00
|
|
|
for node in nodes:
|
|
|
|
family = node.xpathEval('string(./@family)')
|
|
|
|
if not family or family == 'ipv4':
|
2013-04-12 20:26:21 +08:00
|
|
|
goodNode = node
|
2013-03-30 02:38:28 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
if goodNode:
|
|
|
|
addrStr = goodNode.xpathEval('string(./@address)')
|
|
|
|
netmaskStr = goodNode.xpathEval('string(./@netmask)')
|
|
|
|
prefix = goodNode.xpathEval('string(./@prefix)')
|
|
|
|
if prefix:
|
|
|
|
prefix = int(prefix)
|
|
|
|
ret = str(ipaddr.IPNetwork(str(addrStr) + "/" + str(prefix)).masked())
|
|
|
|
elif netmaskStr:
|
|
|
|
netmask = ipaddr.IPAddress(netmaskStr)
|
|
|
|
network = ipaddr.IPAddress(addrStr)
|
|
|
|
ret = str(ipaddr.IPNetwork(str(network) + "/" + str(netmask)).masked())
|
|
|
|
else:
|
|
|
|
ret = str(ipaddr.IPNetwork(str(addrStr)))
|
|
|
|
if doc:
|
|
|
|
doc.freeDoc()
|
|
|
|
if dhcpstart and dhcpend:
|
|
|
|
dhcp = [str(ipaddr.IPAddress(dhcpstart)), str(ipaddr.IPAddress(dhcpend))]
|
|
|
|
else:
|
|
|
|
dhcp = None
|
2013-05-15 00:15:02 +08:00
|
|
|
route = self.get_ipv4_static_route()
|
|
|
|
return [ret, dhcp, route]
|
|
|
|
|
|
|
|
def get_ipv6_static_route(self):
|
|
|
|
doc = None
|
|
|
|
ret = None
|
|
|
|
routeAddr = None
|
|
|
|
routeVia = None
|
|
|
|
xml = self.get_xml()
|
|
|
|
doc = libxml2.parseDoc(xml)
|
|
|
|
nodes = doc.xpathEval('//route')
|
|
|
|
for node in nodes:
|
|
|
|
family = node.xpathEval('string(./@family)')
|
|
|
|
if family and family == 'ipv6':
|
|
|
|
addrStr = node.xpathEval('string(./@address)')
|
|
|
|
prefix = node.xpathEval('string(./@prefix)')
|
|
|
|
gatewayStr = node.xpathEval('string(./@gateway)')
|
|
|
|
if prefix:
|
|
|
|
prefix = int(prefix)
|
|
|
|
routeAddr = str(ipaddr.IPNetwork(str(addrStr) + "/" + str(prefix)).masked())
|
|
|
|
else:
|
|
|
|
routeAddr = str(ipaddr.IPNetwork(str(addrStr)))
|
|
|
|
routeVia = str(ipaddr.IPAddress(str(gatewayStr)))
|
|
|
|
break
|
|
|
|
|
|
|
|
if doc:
|
|
|
|
doc.freeDoc()
|
2013-03-30 02:38:28 +08:00
|
|
|
if routeAddr and routeVia:
|
2013-05-15 00:15:02 +08:00
|
|
|
ret = [routeAddr, routeVia]
|
2013-03-30 02:38:28 +08:00
|
|
|
else:
|
2013-05-15 00:15:02 +08:00
|
|
|
ret = None
|
|
|
|
return ret
|
2013-03-30 02:38:28 +08:00
|
|
|
|
|
|
|
def get_ipv6_network(self):
|
|
|
|
doc = None
|
|
|
|
ret = None
|
|
|
|
goodNode = None
|
|
|
|
dhcpstart = None
|
|
|
|
dhcpend = None
|
|
|
|
xml = self.get_xml()
|
|
|
|
doc = libxml2.parseDoc(xml)
|
|
|
|
nodes = doc.xpathEval('//ip')
|
|
|
|
for node in nodes:
|
|
|
|
family = node.xpathEval('string(./@family)')
|
|
|
|
if family and family == 'ipv6':
|
|
|
|
dhcp = node.xpathEval('string(./dhcp)')
|
|
|
|
if dhcp:
|
|
|
|
dhcpstart = node.xpathEval('string(./dhcp/range[1]/@start)')
|
|
|
|
dhcpend = node.xpathEval('string(./dhcp/range[1]/@end)')
|
|
|
|
goodNode = node
|
|
|
|
break
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
if goodNode is None:
|
2013-03-30 02:38:28 +08:00
|
|
|
for node in nodes:
|
|
|
|
family = node.xpathEval('string(./@family)')
|
|
|
|
if family and family == 'ipv6':
|
2013-04-12 20:26:21 +08:00
|
|
|
goodNode = node
|
2013-03-30 02:38:28 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
if goodNode:
|
|
|
|
addrStr = goodNode.xpathEval('string(./@address)')
|
|
|
|
prefix = goodNode.xpathEval('string(./@prefix)')
|
|
|
|
if prefix:
|
|
|
|
prefix = int(prefix)
|
|
|
|
ret = str(ipaddr.IPNetwork(str(addrStr) + "/" + str(prefix)).masked())
|
|
|
|
else:
|
|
|
|
ret = str(ipaddr.IPNetwork(str(addrStr)))
|
|
|
|
if doc:
|
|
|
|
doc.freeDoc()
|
|
|
|
if dhcpstart and dhcpend:
|
|
|
|
dhcp = [str(ipaddr.IPAddress(dhcpstart)), str(ipaddr.IPAddress(dhcpend))]
|
|
|
|
else:
|
|
|
|
dhcp = None
|
2013-05-15 00:15:02 +08:00
|
|
|
route = self.get_ipv6_static_route()
|
2013-03-30 02:38:28 +08:00
|
|
|
return [ret, dhcp, route]
|
2007-04-04 02:01:45 +08:00
|
|
|
|
2013-03-30 02:38:28 +08:00
|
|
|
def get_name_domain(self):
|
|
|
|
xml = self.get_xml()
|
|
|
|
name_domain = util.xpath(xml, "/network/domain/@name")
|
|
|
|
return name_domain
|
|
|
|
|
2013-05-15 00:15:02 +08:00
|
|
|
def get_ipv6_routing(self):
|
2013-03-30 02:38:28 +08:00
|
|
|
xml = self.get_xml()
|
2013-05-15 00:15:02 +08:00
|
|
|
ipv6_routing = util.xpath(xml, "/network/@ipv6")
|
|
|
|
return ipv6_routing
|
2007-04-04 02:01:45 +08:00
|
|
|
|
|
|
|
def get_ipv4_forward(self):
|
2008-08-16 00:44:45 +08:00
|
|
|
xml = self.get_xml()
|
2011-04-11 05:08:23 +08:00
|
|
|
fw = util.xpath(xml, "/network/forward/@mode")
|
|
|
|
forwardDev = util.xpath(xml, "/network/forward/@dev")
|
2009-11-16 20:14:49 +08:00
|
|
|
return [fw, forwardDev]
|
2007-03-28 07:52:00 +08:00
|
|
|
|
2009-11-16 20:14:49 +08:00
|
|
|
def pretty_forward_mode(self):
|
|
|
|
forward, forwardDev = self.get_ipv4_forward()
|
2009-12-02 00:49:08 +08:00
|
|
|
return vmmNetwork.pretty_desc(forward, forwardDev)
|
2009-11-16 20:14:49 +08:00
|
|
|
|
2010-12-05 01:28:44 +08:00
|
|
|
def can_pxe(self):
|
|
|
|
xml = self.get_xml()
|
2010-12-07 10:38:05 +08:00
|
|
|
forward = self.get_ipv4_forward()[0]
|
|
|
|
if forward and forward != "nat":
|
|
|
|
return True
|
2011-04-11 05:08:23 +08:00
|
|
|
return bool(util.xpath(xml, "/network/ip/dhcp/bootp/@file"))
|