2007-03-21 19:28:36 +03:00
#
2014-06-25 14:36:46 +04:00
# Copyright (C) 2006, 2013-2014 Red Hat, Inc.
2007-03-21 19:28:36 +03: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-20 19:12:20 +03:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
2007-03-21 19:28:36 +03:00
#
2013-07-12 18:53:30 +04:00
2013-03-29 22:38:28 +04:00
import ipaddr
2013-07-12 18:53:30 +04:00
2013-09-21 04:40:07 +04:00
from virtinst import Network
2013-07-12 18:53:30 +04:00
2014-09-13 00:10:45 +04:00
from . libvirtobject import vmmLibvirtObject
2007-03-21 19:28:36 +03:00
2013-04-13 22:34:52 +04:00
2013-09-21 04:40:07 +04:00
def _make_addr_str ( addrStr , prefix , netmaskStr ) :
if prefix :
return str ( ipaddr . IPNetwork ( str ( addrStr ) + " / " +
str ( prefix ) ) . masked ( ) )
elif netmaskStr :
netmask = ipaddr . IPAddress ( netmaskStr )
network = ipaddr . IPAddress ( addrStr )
return str ( ipaddr . IPNetwork ( str ( network ) + " / " +
str ( netmask ) ) . masked ( ) )
else :
return str ( ipaddr . IPNetwork ( str ( addrStr ) ) )
2009-12-01 19:49:08 +03:00
2013-09-21 04:40:07 +04:00
class vmmNetwork ( 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 , Network )
2013-07-07 16:05:23 +04:00
2010-12-09 20:37:48 +03:00
2013-09-21 04:40:07 +04:00
##########################
# Required class methods #
##########################
2015-04-10 16:15:44 +03:00
def _conn_tick_poll_param ( self ) :
return " pollnet "
def class_name ( self ) :
return " network "
2010-12-09 20:37:48 +03:00
def _XMLDesc ( self , flags ) :
2013-07-07 16:42:57 +04:00
return self . _backend . XMLDesc ( flags )
2010-12-09 20:37:48 +03:00
def _define ( self , xml ) :
2013-07-07 16:42:57 +04:00
return self . conn . define_network ( xml )
2014-02-11 22:19:15 +04:00
def _using_events ( self ) :
return self . conn . using_network_events
2015-04-10 01:02:42 +03:00
def _check_supports_isactive ( self ) :
return self . conn . check_support (
self . conn . SUPPORT_NET_ISACTIVE , self . _backend )
def _get_backend_status ( self ) :
return self . _backend_get_active ( )
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 ( )
2010-12-09 20:37:48 +03:00
2015-04-10 21:08:25 +03:00
def _init_libvirt_state ( self ) :
self . tick ( )
2013-09-21 04:40:07 +04:00
###########
# Actions #
###########
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2007-03-30 05:23:17 +04:00
def start ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . create ( )
2007-03-30 05:23:17 +04:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2007-04-03 22:01:45 +04:00
def stop ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . destroy ( )
2007-04-03 22:01:45 +04: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 ) :
ignore = force
2013-07-07 16:42:57 +04:00
self . _backend . undefine ( )
self . _backend = None
2007-04-03 22:01:45 +04:00
2015-04-10 01:02:42 +03:00
###############################
# XML/config handling parsing #
###############################
2007-03-30 23:02:09 +04:00
def get_autostart ( self ) :
2013-07-07 16:42:57 +04:00
return self . _backend . autostart ( )
2013-09-21 04:40:07 +04:00
def set_autostart ( self , value ) :
self . _backend . setAutostart ( value )
2013-07-07 16:05:23 +04:00
2014-06-25 14:36:46 +04:00
def set_qos ( self , * * kwargs ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
q = xmlobj . bandwidth
2014-06-25 14:36:46 +04:00
for key , val in kwargs . items ( ) :
setattr ( q , key , val )
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2014-06-25 14:36:46 +04:00
return self . is_active ( )
2013-09-21 04:40:07 +04:00
def get_uuid ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . uuid
2013-09-21 04:40:07 +04:00
def get_bridge_device ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . bridge
2013-09-21 04:40:07 +04:00
def get_name_domain ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . domain_name
2013-09-21 04:40:07 +04:00
def get_ipv6_enabled ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . ipv6
2013-09-21 04:40:07 +04:00
def get_ipv4_forward_mode ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . forward . mode
2013-09-21 04:40:07 +04:00
def pretty_forward_mode ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . forward . pretty_desc ( )
2014-06-25 14:36:46 +04:00
def get_qos ( self ) :
return self . get_xmlobj ( ) . bandwidth
2013-09-21 04:40:07 +04:00
def can_pxe ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . can_pxe ( )
2013-09-21 04:40:07 +04:00
def _get_static_route ( self , family ) :
2013-09-23 16:34:50 +04:00
xmlobj = self . get_xmlobj ( )
2013-09-21 04:40:07 +04:00
route = None
for r in xmlobj . routes :
if ( r . family == family or ( family == " ipv4 " and not r . family ) ) :
route = r
2013-05-14 20:15:02 +04:00
break
2013-09-21 04:40:07 +04:00
if not route :
return [ None , None ]
2013-05-14 20:15:02 +04:00
2013-09-21 04:40:07 +04:00
routeAddr = _make_addr_str ( route . address , route . prefix , route . netmask )
routeVia = str ( ipaddr . IPAddress ( str ( route . gateway ) ) )
2013-05-14 20:15:02 +04:00
2013-09-21 04:40:07 +04:00
if not routeAddr or not routeVia :
return [ None , None ]
return [ routeAddr , routeVia ]
def _get_network ( self , family ) :
2013-03-29 22:38:28 +04:00
dhcpstart = None
dhcpend = None
2013-09-23 16:34:50 +04:00
xmlobj = self . get_xmlobj ( )
2013-09-21 04:40:07 +04: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-29 22:38:28 +04:00
break
2013-09-21 04:40:07 +04:00
if not ip :
for i in xmlobj . ips :
if ( i . family == family or
( family == " ipv4 " and not i . family ) ) :
ip = i
2013-03-29 22:38:28 +04:00
break
2013-09-21 04:40:07 +04:00
ret = None
if ip :
ret = _make_addr_str ( ip . address , ip . prefix , ip . netmask )
2013-03-29 22:38:28 +04:00
2013-09-21 04:40:07 +04:00
dhcp = [ None , None ]
2013-03-29 22:38:28 +04:00
if dhcpstart and dhcpend :
2013-09-21 04:40:07 +04:00
dhcp = [ str ( ipaddr . IPAddress ( dhcpstart ) ) ,
str ( ipaddr . IPAddress ( dhcpend ) ) ]
return [ ret , dhcp ]
2007-04-03 22:01:45 +04:00
2013-09-21 04:40:07 +04: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 " ) ]