2009-11-22 19:18:53 +03:00
#
2013-10-28 00:59:46 +04:00
# Copyright (C) 2009, 2013 Red Hat, Inc.
2009-11-22 19:18:53 +03: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 01:13:36 +03:00
from virtinst import Interface
2009-11-22 19:18:53 +03:00
2014-09-13 00:10:45 +04:00
from . libvirtobject import vmmLibvirtObject
2010-02-26 03:35:01 +03:00
2013-04-13 22:34:52 +04:00
2010-02-26 03:35:01 +03:00
class vmmInterface ( vmmLibvirtObject ) :
2013-07-07 16:42:57 +04:00
def __init__ ( self , conn , backend , key ) :
2013-09-23 16:34:50 +04:00
vmmLibvirtObject . __init__ ( self , conn , backend , key , Interface )
2010-02-26 03:35:01 +03:00
2015-04-10 16:15:44 +03:00
##########################
# Required class methods #
##########################
2010-02-26 03:35:01 +03:00
# Routines from vmmLibvirtObject
2015-04-10 16:15:44 +03:00
def _conn_tick_poll_param ( self ) :
return " polliface "
def class_name ( self ) :
return " interface "
2010-02-26 03:35:01 +03:00
def _XMLDesc ( self , flags ) :
2013-07-07 16:42:57 +04:00
return self . _backend . XMLDesc ( flags )
2010-02-26 03:35:01 +03:00
def _define ( self , xml ) :
2011-07-23 00:43:26 +04:00
return self . conn . define_interface ( xml )
2015-04-10 01:02:42 +03:00
def _check_supports_isactive ( self ) :
return self . conn . check_support (
self . conn . SUPPORT_INTERFACE_ISACTIVE , self . _backend )
def _get_backend_status ( self ) :
return self . _backend_get_active ( )
2010-02-09 01:13:36 +03:00
2015-04-10 16:15:44 +03:00
def tick ( self , stats_update = True ) :
ignore = stats_update
2015-04-10 02:03:27 +03:00
self . _refresh_status ( )
2013-07-07 16:05:23 +04:00
2015-04-10 21:08:25 +03:00
def _init_libvirt_state ( self ) :
( self . _inactive_xml_flags ,
self . _active_xml_flags ) = self . conn . get_interface_flags ( self . _backend )
self . tick ( )
2009-11-22 19:18:53 +03:00
2015-04-10 01:02:42 +03:00
#####################
# Object operations #
#####################
2013-07-07 19:06:15 +04:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2009-11-22 19:18:53 +03:00
def start ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . create ( 0 )
2009-11-22 19:18:53 +03:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2009-11-22 19:18:53 +03:00
def stop ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . destroy ( 0 )
2009-11-22 19:18:53 +03:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2013-09-30 23:23:14 +04:00
def delete ( self , force = True ) :
2013-07-07 16:42:57 +04:00
self . _backend . undefine ( )
2009-11-22 19:18:53 +03:00
2015-04-10 01:02:42 +03:00
################
# XML routines #
################
def get_mac ( self ) :
return self . get_xmlobj ( ) . macaddr
2009-11-26 01:07:12 +03:00
def is_bridge ( self ) :
typ = self . get_type ( )
return typ == " bridge "
2009-11-22 19:18:53 +03:00
def get_type ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . type
2009-11-22 19:18:53 +03:00
2010-02-09 01:13:36 +03:00
def get_pretty_type ( self ) :
itype = self . get_type ( )
2013-09-10 01:14:16 +04:00
if itype == Interface . INTERFACE_TYPE_VLAN :
2010-02-09 01:13:36 +03:00
return " VLAN "
elif itype :
2010-02-25 04:42:00 +03:00
return str ( itype ) . capitalize ( )
2010-02-09 01:13:36 +03:00
else :
2016-02-05 18:18:16 +03:00
return _ ( " Interface " )
2010-02-09 01:13:36 +03:00
def get_startmode ( self ) :
2015-05-14 17:27:55 +03:00
return self . get_xmlobj ( inactive = True ) . start_mode or " none "
2010-02-26 05:13:33 +03:00
2010-02-26 03:42:08 +03:00
def set_startmode ( self , newmode ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
xmlobj . start_mode = newmode
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2010-02-09 01:13:36 +03:00
def get_slaves ( self ) :
2016-02-05 18:18:16 +03:00
return [ [ obj . name , obj . type or _ ( " Unknown " ) ] for obj in
2013-09-23 16:34:50 +04:00
self . get_xmlobj ( ) . interfaces ]
2009-11-26 01:07:12 +03:00
2010-02-09 01:13:36 +03:00
def get_slave_names ( self ) :
# Returns a list of names of all enslaved interfaces
2013-04-12 00:32:00 +04:00
return [ x [ 0 ] for x in self . get_slaves ( ) ]
2010-02-09 01:13:36 +03:00
2013-09-10 01:14:16 +04:00
def _get_ip ( self , iptype ) :
2017-01-19 20:00:36 +03:00
# Get list of IP addresses from active XML and protocol configuration
# from inactive XML to figure out whether the IP address is static or
# from DHCP server.
activeObj = self . get_xmlobj ( )
inactiveObj = self . get_xmlobj ( inactive = True )
activeProto = None
inactiveProto = None
for protocol in activeObj . protocols :
2013-09-10 01:14:16 +04:00
if protocol . family == iptype :
2017-01-19 20:00:36 +03:00
activeProto = protocol
2013-09-10 01:14:16 +04:00
break
2017-01-19 20:00:36 +03:00
for protocol in inactiveObj . protocols :
if protocol . family == iptype :
inactiveProto = protocol
break
if not activeProto and not inactiveProto :
2013-09-10 01:14:16 +04:00
return None , [ ]
ret = [ ]
2017-05-10 11:00:39 +03:00
if activeProto :
for ip in activeProto . ips :
ipstr = ip . address
if not ipstr :
continue
if ip . prefix :
ipstr + = " / %s " % ip . prefix
ret . append ( ipstr )
2017-01-19 20:00:36 +03:00
return inactiveProto or activeProto , ret
2013-09-10 01:14:16 +04:00
2010-02-26 05:13:33 +03:00
def get_ipv4 ( self ) :
2013-09-10 01:14:16 +04:00
proto , ips = self . _get_ip ( " ipv4 " )
if proto is None :
2010-02-26 05:13:33 +03:00
return [ ]
2013-09-10 01:14:16 +04:00
ipstr = None
if ips :
ipstr = ips [ 0 ]
return [ proto . dhcp , ipstr ]
2010-02-26 05:13:33 +03:00
def get_ipv6 ( self ) :
2013-09-10 01:14:16 +04:00
proto , ips = self . _get_ip ( " ipv6 " )
if proto is None :
2010-02-26 05:13:33 +03:00
return [ ]
2013-09-10 01:14:16 +04:00
return [ proto . dhcp , proto . autoconf , ips ]
2010-02-09 01:13:36 +03:00
2014-09-21 23:02:27 +04:00
def get_protocol_xml ( self , inactive = False ) :
return self . get_xmlobj ( inactive = inactive ) . protocols [ : ]