2009-11-23 00:18:53 +08:00
|
|
|
#
|
2013-10-28 04:59:46 +08:00
|
|
|
# Copyright (C) 2009, 2013 Red Hat, Inc.
|
2009-11-23 00:18:53 +08:00
|
|
|
# Copyright (C) 2009 Cole Robinson <crobinso@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
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
# MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
|
2010-02-09 06:13:36 +08:00
|
|
|
from virtinst import Interface
|
2009-11-23 00:18:53 +08:00
|
|
|
|
2014-09-13 04:10:45 +08:00
|
|
|
from .libvirtobject import vmmLibvirtObject
|
2010-02-26 08:35:01 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2010-02-26 08:35:01 +08:00
|
|
|
class vmmInterface(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, Interface)
|
2010-02-26 08:35:01 +08:00
|
|
|
|
2013-07-07 20:42:57 +08:00
|
|
|
self._active = True
|
2009-11-23 00:18:53 +08:00
|
|
|
|
2010-02-26 08:35:01 +08:00
|
|
|
(self._inactive_xml_flags,
|
2013-09-10 05:14:16 +08:00
|
|
|
self._active_xml_flags) = self.conn.get_interface_flags(self._backend)
|
2010-02-26 08:35:01 +08:00
|
|
|
|
2013-07-07 20:05:23 +08:00
|
|
|
self._support_isactive = None
|
|
|
|
|
|
|
|
self.tick()
|
2010-02-09 06:13:36 +08:00
|
|
|
|
2010-02-26 08:35:01 +08:00
|
|
|
# Routines from vmmLibvirtObject
|
|
|
|
def _XMLDesc(self, flags):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._backend.XMLDesc(flags)
|
2010-02-26 08:35:01 +08:00
|
|
|
def _define(self, xml):
|
2011-07-23 04:43:26 +08:00
|
|
|
return self.conn.define_interface(xml)
|
2010-02-09 06:13:36 +08:00
|
|
|
|
2009-11-23 00:18:53 +08:00
|
|
|
def set_active(self, state):
|
2013-07-07 20:42:57 +08:00
|
|
|
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
|
2010-02-26 08:35:01 +08:00
|
|
|
self.refresh_xml()
|
2009-11-23 00:18:53 +08:00
|
|
|
|
2013-07-07 20:05:23 +08:00
|
|
|
def _backend_get_active(self):
|
|
|
|
ret = True
|
|
|
|
if self._support_isactive is None:
|
2013-10-06 22:08:04 +08:00
|
|
|
self._support_isactive = self.conn.check_support(
|
|
|
|
self.conn.SUPPORT_INTERFACE_ISACTIVE, self._backend)
|
2013-07-07 20:05:23 +08:00
|
|
|
|
|
|
|
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):
|
|
|
|
self.set_active(self._backend_get_active())
|
|
|
|
|
2009-11-23 00:18:53 +08:00
|
|
|
def is_active(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
return self._active
|
2009-11-23 00:18:53 +08:00
|
|
|
|
|
|
|
def get_mac(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().macaddr
|
2009-11-23 00:18:53 +08:00
|
|
|
|
2013-07-07 23:06:15 +08:00
|
|
|
def _kick_conn(self):
|
|
|
|
self.conn.schedule_priority_tick(polliface=True)
|
|
|
|
|
2009-11-23 00:18:53 +08:00
|
|
|
def start(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.create(0)
|
2012-02-11 03:07:51 +08:00
|
|
|
self.idle_add(self.refresh_xml)
|
2013-07-07 23:06:15 +08:00
|
|
|
self._kick_conn()
|
2009-11-23 00:18:53 +08:00
|
|
|
|
|
|
|
def stop(self):
|
2013-07-07 20:42:57 +08:00
|
|
|
self._backend.destroy(0)
|
2012-02-11 03:07:51 +08:00
|
|
|
self.idle_add(self.refresh_xml)
|
2013-07-07 23:06:15 +08:00
|
|
|
self._kick_conn()
|
2009-11-23 00:18:53 +08:00
|
|
|
|
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()
|
2013-07-07 23:06:15 +08:00
|
|
|
self._kick_conn()
|
2009-11-23 00:18:53 +08:00
|
|
|
|
2009-11-26 06:07:12 +08:00
|
|
|
def is_bridge(self):
|
|
|
|
typ = self.get_type()
|
|
|
|
return typ == "bridge"
|
2009-11-23 00:18:53 +08:00
|
|
|
|
|
|
|
def get_type(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().type
|
2009-11-23 00:18:53 +08:00
|
|
|
|
2010-02-09 06:13:36 +08:00
|
|
|
def get_pretty_type(self):
|
|
|
|
itype = self.get_type()
|
|
|
|
|
2013-09-10 05:14:16 +08:00
|
|
|
if itype == Interface.INTERFACE_TYPE_VLAN:
|
2010-02-09 06:13:36 +08:00
|
|
|
return "VLAN"
|
|
|
|
elif itype:
|
2010-02-25 09:42:00 +08:00
|
|
|
return str(itype).capitalize()
|
2010-02-09 06:13:36 +08:00
|
|
|
else:
|
|
|
|
return "Interface"
|
|
|
|
|
|
|
|
def get_startmode(self):
|
2013-09-23 20:34:50 +08:00
|
|
|
return self.get_xmlobj().start_mode or "none"
|
2010-02-26 10:13:33 +08:00
|
|
|
|
2010-02-26 08:42:08 +08:00
|
|
|
def set_startmode(self, newmode):
|
2015-04-08 02:53:28 +08:00
|
|
|
xmlobj = self._get_xmlobj_to_define()
|
|
|
|
xmlobj.start_mode = newmode
|
2013-09-10 05:14:16 +08:00
|
|
|
self.redefine_cached()
|
2010-02-09 06:13:36 +08:00
|
|
|
|
|
|
|
def get_slaves(self):
|
2013-09-10 05:14:16 +08:00
|
|
|
return [[obj.name, obj.type or "Unknown"] for obj in
|
2013-09-23 20:34:50 +08:00
|
|
|
self.get_xmlobj().interfaces]
|
2009-11-26 06:07:12 +08:00
|
|
|
|
2010-02-09 06:13:36 +08:00
|
|
|
def get_slave_names(self):
|
|
|
|
# Returns a list of names of all enslaved interfaces
|
2013-04-12 04:32:00 +08:00
|
|
|
return [x[0] for x in self.get_slaves()]
|
2010-02-09 06:13:36 +08:00
|
|
|
|
2013-09-10 05:14:16 +08:00
|
|
|
def _get_ip(self, iptype):
|
2013-09-23 20:34:50 +08:00
|
|
|
obj = self.get_xmlobj()
|
2013-09-10 05:14:16 +08:00
|
|
|
found = None
|
|
|
|
for protocol in obj.protocols:
|
|
|
|
if protocol.family == iptype:
|
|
|
|
found = protocol
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
return None, []
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for ip in found.ips:
|
|
|
|
ipstr = ip.address
|
|
|
|
if not ipstr:
|
|
|
|
continue
|
|
|
|
if ip.prefix:
|
|
|
|
ipstr += "/%s" % ip.prefix
|
|
|
|
ret.append(ipstr)
|
|
|
|
return found, ret
|
|
|
|
|
2010-02-26 10:13:33 +08:00
|
|
|
def get_ipv4(self):
|
2013-09-10 05:14:16 +08:00
|
|
|
proto, ips = self._get_ip("ipv4")
|
|
|
|
if proto is None:
|
2010-02-26 10:13:33 +08:00
|
|
|
return []
|
|
|
|
|
2013-09-10 05:14:16 +08:00
|
|
|
ipstr = None
|
|
|
|
if ips:
|
|
|
|
ipstr = ips[0]
|
|
|
|
return [proto.dhcp, ipstr]
|
2010-02-26 10:13:33 +08:00
|
|
|
|
|
|
|
def get_ipv6(self):
|
2013-09-10 05:14:16 +08:00
|
|
|
proto, ips = self._get_ip("ipv6")
|
|
|
|
if proto is None:
|
2010-02-26 10:13:33 +08:00
|
|
|
return []
|
2013-09-10 05:14:16 +08:00
|
|
|
return [proto.dhcp, proto.autoconf, ips]
|
2010-02-09 06:13:36 +08:00
|
|
|
|
2014-09-22 03:02:27 +08:00
|
|
|
def get_protocol_xml(self, inactive=False):
|
|
|
|
return self.get_xmlobj(inactive=inactive).protocols[:]
|