2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2017-05-25 17:07:35 +03:00
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems */
2008-06-17 20:27:32 +04:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef TRANSFER_MANAGER_H_
# define TRANSFER_MANAGER_H_
# include "MadManager.h"
# include "ActionManager.h"
# include "VirtualMachinePool.h"
# include "LifeCycleManager.h"
2008-11-13 19:21:17 +03:00
# include "TransferManagerDriver.h"
2008-06-17 20:27:32 +04:00
using namespace std ;
extern " C " void * tm_action_loop ( void * arg ) ;
2016-12-11 23:05:07 +03:00
class VirtualMachineDisk ;
2017-02-03 16:19:15 +03:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
class TMAction : public ActionRequest
2008-06-17 20:27:32 +04:00
{
public :
enum Actions
{
PROLOG ,
2008-11-13 19:21:17 +03:00
PROLOG_MIGR ,
PROLOG_RESUME ,
2015-03-18 14:51:01 +03:00
PROLOG_ATTACH ,
2008-06-17 20:27:32 +04:00
EPILOG ,
2016-05-02 19:34:42 +03:00
EPILOG_LOCAL ,
2008-11-13 19:21:17 +03:00
EPILOG_STOP ,
2009-07-09 18:34:34 +04:00
EPILOG_DELETE ,
EPILOG_DELETE_PREVIOUS ,
2012-06-27 20:50:19 +04:00
EPILOG_DELETE_STOP ,
2013-01-21 03:15:46 +04:00
EPILOG_DELETE_BOTH ,
2015-03-18 14:51:01 +03:00
EPILOG_DETACH ,
2008-06-17 20:27:32 +04:00
CHECKPOINT ,
2009-07-09 18:34:34 +04:00
DRIVER_CANCEL ,
2013-03-08 01:44:18 +04:00
SAVEAS_HOT ,
2015-05-20 18:48:27 +03:00
SNAPSHOT_CREATE ,
2015-05-26 12:24:34 +03:00
SNAPSHOT_REVERT ,
2015-05-20 18:48:27 +03:00
SNAPSHOT_DELETE ,
2017-02-03 16:19:15 +03:00
RESIZE
2008-06-17 20:27:32 +04:00
} ;
2017-02-03 16:19:15 +03:00
TMAction ( Actions a , int v ) : ActionRequest ( ActionRequest : : USER ) ,
_action ( a ) , _vm_id ( v ) { } ;
2017-02-08 14:24:42 +03:00
TMAction ( const TMAction & o ) : ActionRequest ( o . _type ) , _action ( o . _action ) ,
_vm_id ( o . _vm_id ) { } ;
2017-02-03 16:19:15 +03:00
Actions action ( ) const
{
return _action ;
}
int vm_id ( ) const
{
return _vm_id ;
}
2017-02-08 14:24:42 +03:00
ActionRequest * clone ( ) const
{
return new TMAction ( * this ) ;
}
2017-02-03 16:19:15 +03:00
private :
Actions _action ;
int _vm_id ;
} ;
class TransferManager : public MadManager , public ActionListener
{
public :
TransferManager (
VirtualMachinePool * _vmpool ,
HostPool * _hpool ,
vector < const VectorAttribute * > & _mads ) :
MadManager ( _mads ) ,
vmpool ( _vmpool ) ,
hpool ( _hpool )
{
am . addListener ( this ) ;
} ;
~ TransferManager ( ) { } ;
2008-06-17 20:27:32 +04:00
/**
* Triggers specific actions to the Information Manager . This function
* wraps the ActionManager trigger function .
* @ param action the IM action
2012-09-10 20:08:00 +04:00
* @ param vid VM unique id . This is the argument of the passed to the
2008-06-17 20:27:32 +04:00
* invoked action .
*/
2017-02-03 16:19:15 +03:00
void trigger ( TMAction : : Actions action , int vid )
{
TMAction tm_ar ( action , vid ) ;
am . trigger ( tm_ar ) ;
}
void finalize ( )
{
am . finalize ( ) ;
}
2012-09-10 20:08:00 +04:00
2008-06-17 20:27:32 +04:00
/**
2012-09-10 20:08:00 +04:00
* This functions starts the associated listener thread , and creates a
2008-06-17 20:27:32 +04:00
* new thread for the Information Manager . This thread will wait in
* an action loop till it receives ACTION_FINALIZE .
* @ return 0 on success .
*/
int start ( ) ;
/**
* Loads Virtual Machine Manager Mads defined in configuration file
2012-09-10 20:08:00 +04:00
* @ param uid of the user executing the driver . When uid is 0 the nebula
2008-06-17 20:27:32 +04:00
* identity will be used . Otherwise the Mad will be loaded through the
2012-09-10 20:08:00 +04:00
* sudo application .
2008-06-17 20:27:32 +04:00
*/
2013-10-25 17:16:44 +04:00
int load_mads ( int uid ) ;
2012-09-10 20:08:00 +04:00
2008-06-17 20:27:32 +04:00
/**
* Gets the thread identification .
* @ return pthread_t for the manager thread ( that in the action loop ) .
*/
pthread_t get_thread_id ( ) const
{
return tm_thread ;
} ;
2012-06-13 20:19:22 +04:00
/**
* Inserts a transfer command in the xfs stream
*
* @ param vm The VM
* @ param disk Disk to transfer
* @ param disk_index Disk index
* @ param system_tm_mad The Transfer Manager for the system datastore
* @ param opennebula_hostname The front - end hostname
* @ param xfr Stream where the transfer command will be written
2012-06-15 18:28:30 +04:00
* @ param error Error reason , if any
2012-06-13 20:19:22 +04:00
*
* @ return 0 on success
*/
int prolog_transfer_command (
VirtualMachine * vm ,
2016-12-11 23:05:07 +03:00
const VirtualMachineDisk * disk ,
2012-06-13 20:19:22 +04:00
string & system_tm_mad ,
string & opennebula_hostname ,
ostream & xfr ,
2012-06-15 18:28:30 +04:00
ostringstream & error ) ;
2012-06-13 20:19:22 +04:00
2016-01-25 18:20:12 +03:00
/**
* Inserts a context command in the xfs stream
*
* @ param vm The VM
* @ param token_password Owner user ' s token password
* @ param system_tm_mad The Transfer Manager for the system datastore
2016-03-16 21:13:40 +03:00
* @ param disk_id of the context disk
2016-01-25 18:20:12 +03:00
* @ param xfr Stream where the transfer command will be written
*
2016-03-16 21:13:40 +03:00
* @ return - 1 in case of error , 0 if the VM has no context , 1 on success
2016-01-25 18:20:12 +03:00
*/
int prolog_context_command (
VirtualMachine * vm ,
const string & token_password ,
string & system_tm_mad ,
2016-03-16 21:13:40 +03:00
int & disk_id ,
2016-01-26 15:24:42 +03:00
ostream & xfr ) ;
2016-01-25 18:20:12 +03:00
2012-06-14 19:45:41 +04:00
/**
* Inserts a transfer command in the xfs stream
*
* @ param vm The VM
2016-05-02 19:34:42 +03:00
* @ param host where the operation will be performed fe or host
2012-06-14 19:45:41 +04:00
* @ param disk Disk to transfer
* @ param disk_index Disk index
* @ param xfr Stream where the transfer command will be written
*/
2012-06-15 18:28:30 +04:00
void epilog_transfer_command (
2012-06-14 19:45:41 +04:00
VirtualMachine * vm ,
2016-05-02 19:34:42 +03:00
const string & host ,
2016-12-11 23:05:07 +03:00
const VirtualMachineDisk * disk ,
2012-06-15 18:28:30 +04:00
ostream & xfr ) ;
2012-09-08 01:58:45 +04:00
/**
* Inserts a transfer command in the xfs stream , for live migration
*
* @ param vm The VM
* @ param xfr Stream where the transfer command will be written
*/
void migrate_transfer_command (
VirtualMachine * vm ,
ostream & xfr ) ;
2012-06-14 19:45:41 +04:00
2013-01-21 03:15:46 +04:00
/**
2015-07-01 14:37:58 +03:00
* This function generates the epilog_delete sequence for current ,
2013-01-21 03:15:46 +04:00
* front - end and previous hosts .
* @ param vm pointer to VM , locked
* @ param xfr stream to write the commands
* @ param local true to delete the front - end
* @ param previous true to delete the previous host
*
* @ return 0 on success
*/
int epilog_delete_commands ( VirtualMachine * vm ,
ostream & xfr ,
bool local ,
bool previous ) ;
2015-07-01 14:37:58 +03:00
/**
* This function generates the TM command for the given snapshot action
* @ param vm pointer to VM , locked
* @ param snap_action : " SNAP_CREATE, SNAP_DELETE, SNAP_REVERT "
* @ param xfr stream to write the commands
*
* @ return 0 on success
*/
int snapshot_transfer_command ( VirtualMachine * vm ,
const char * snap_action ,
ostream & xfr ) ;
2016-12-11 23:05:07 +03:00
/**
* Inserts a resize command in the xfr stream
* @ param vm
* @ param disk to resize
* @ param xfr stream to include the command .
*/
void resize_command (
VirtualMachine * vm ,
const VirtualMachineDisk * disk ,
ostream & xfr ) ;
2012-06-15 18:28:30 +04:00
private :
2008-06-17 20:27:32 +04:00
/**
* Thread id for the Transfer Manager
*/
pthread_t tm_thread ;
/**
2008-11-13 19:21:17 +03:00
* Pointer to the Virtual Machine Pool , to access VMs
2008-06-17 20:27:32 +04:00
*/
2008-11-13 19:21:17 +03:00
VirtualMachinePool * vmpool ;
/**
* Pointer to the Host Pool , to access hosts
*/
HostPool * hpool ;
2012-09-10 20:08:00 +04:00
2008-06-17 20:27:32 +04:00
/**
* Action engine for the Manager
*/
2015-07-01 22:15:40 +03:00
ActionManager am ;
2008-11-13 19:21:17 +03:00
2012-02-25 04:28:28 +04:00
/**
* Generic name for the TransferManager driver
*/
static const char * transfer_driver_name ;
2008-11-13 19:21:17 +03:00
/**
* Returns a pointer to a Transfer Manager driver .
* @ param name of an attribute of the driver ( e . g . its type )
* @ param value of the attribute
* @ return the TM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const TransferManagerDriver * get (
const string & name ,
const string & value )
{
return static_cast < const TransferManagerDriver * >
2009-07-13 17:47:42 +04:00
( MadManager : : get ( 0 , name , value ) ) ;
2008-11-13 19:21:17 +03:00
} ;
2008-06-17 20:27:32 +04:00
2008-11-13 19:21:17 +03:00
/**
2012-09-10 20:08:00 +04:00
* Returns a pointer to a Transfer Manager driver . The driver is
2008-11-13 19:21:17 +03:00
* searched by its name .
* @ param name the name of the driver
* @ return the TM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const TransferManagerDriver * get (
const string & name )
{
string _name ( " NAME " ) ;
return static_cast < const TransferManagerDriver * >
2009-07-13 17:47:42 +04:00
( MadManager : : get ( 0 , _name , name ) ) ;
2008-11-13 19:21:17 +03:00
} ;
2012-09-10 20:08:00 +04:00
2012-02-25 04:28:28 +04:00
/**
2012-09-10 20:08:00 +04:00
* Returns a pointer to a Transfer Manager driver . The driver is
2012-02-25 04:28:28 +04:00
* searched by its name .
* @ return the TM driver for the Transfer Manager
*/
const TransferManagerDriver * get ( )
{
string _name ( " NAME " ) ;
return static_cast < const TransferManagerDriver * >
( MadManager : : get ( 0 , _name , transfer_driver_name ) ) ;
} ;
2008-06-17 20:27:32 +04:00
/**
2012-09-10 20:08:00 +04:00
* Function to execute the Manager action loop method within a new pthread
2008-06-17 20:27:32 +04:00
* ( requires C linkage )
*/
2012-09-10 20:08:00 +04:00
friend void * tm_action_loop ( void * arg ) ;
2017-02-03 16:19:15 +03:00
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
void finalize_action ( const ActionRequest & ar )
{
NebulaLog : : log ( " TM " , Log : : INFO , " Stopping Transfer Manager... " ) ;
MadManager : : stop ( ) ;
} ;
void user_action ( const ActionRequest & ar ) ;
2008-06-17 20:27:32 +04:00
/**
2012-09-10 20:08:00 +04:00
* This function starts the prolog sequence
2008-06-17 20:27:32 +04:00
*/
void prolog_action ( int vid ) ;
2008-11-13 19:21:17 +03:00
/**
2012-09-10 20:08:00 +04:00
* This function starts the prolog migration sequence
2008-11-13 19:21:17 +03:00
*/
void prolog_migr_action ( int vid ) ;
/**
2012-09-10 20:08:00 +04:00
* This function starts the prolog resume sequence
2008-11-13 19:21:17 +03:00
*/
void prolog_resume_action ( int vid ) ;
2012-09-10 20:08:00 +04:00
2015-03-18 14:51:01 +03:00
/**
* This function starts the prolog attach sequence
*/
void prolog_attach_action ( int vid ) ;
2008-06-17 20:27:32 +04:00
/**
* This function starts the epilog sequence
*/
2016-05-02 19:34:42 +03:00
void epilog_action ( bool local , int vid ) ;
2008-06-17 20:27:32 +04:00
2008-11-13 19:21:17 +03:00
/**
* This function starts the epilog_stop sequence
*/
void epilog_stop_action ( int vid ) ;
2012-09-10 20:08:00 +04:00
2009-07-09 18:34:34 +04:00
/**
2013-01-21 03:15:46 +04:00
* This function starts the epilog_delete sequence in the current host
* @ param vid the Virtual Machine ID
2009-07-09 18:34:34 +04:00
*/
2012-06-27 20:50:19 +04:00
void epilog_delete_action ( int vid )
{
epilog_delete_action ( false , vid ) ;
}
/**
* This function starts the epilog_delete_stop sequence on the local host
2013-01-21 03:15:46 +04:00
* i . e . the front - end ( the VM is not running )
* @ param vid the Virtual Machine ID
2012-06-27 20:50:19 +04:00
*/
void epilog_delete_stop_action ( int vid )
{
epilog_delete_action ( true , vid ) ;
}
/**
2013-01-21 03:15:46 +04:00
* This function starts the epilog_delete sequence on the previous host
* @ param vid the Virtual Machine ID
2012-06-27 20:50:19 +04:00
*/
2013-01-21 03:15:46 +04:00
void epilog_delete_previous_action ( int vid ) ;
2009-07-09 18:34:34 +04:00
2013-01-21 02:05:14 +04:00
/**
2013-01-21 03:15:46 +04:00
* This function starts the epilog_delete sequence on the current and
* previous hosts
* @ param vid the Virtual Machine ID
2013-01-21 02:05:14 +04:00
*/
2013-01-21 03:15:46 +04:00
void epilog_delete_both_action ( int vid ) ;
2009-07-09 18:34:34 +04:00
/**
2013-01-21 03:15:46 +04:00
* This function starts the epilog_delete sequence
2009-07-09 18:34:34 +04:00
*/
2013-01-21 03:15:46 +04:00
void epilog_delete_action ( bool local , int vid ) ;
2009-07-09 18:34:34 +04:00
2015-03-18 14:51:01 +03:00
/**
* This function starts the epilog detach sequence
*/
void epilog_detach_action ( int vid ) ;
2008-06-17 20:27:32 +04:00
/**
* This function starts the epilog sequence
*/
void checkpoint_action ( int vid ) ;
2009-07-09 18:34:34 +04:00
/**
* This function cancels the operation being performed by the driver
*/
void driver_cancel_action ( int vid ) ;
2013-03-08 01:44:18 +04:00
/**
* This function starts the saveas of the given disk
*/
void saveas_hot_action ( int vid ) ;
2015-05-20 18:48:27 +03:00
2015-05-26 12:24:34 +03:00
/**
* This function performs a generic snapshot action
*/
void do_snapshot_action ( int vid , const char * action ) ;
2015-05-20 18:48:27 +03:00
/**
* This function takes an snapshot of a disk
*/
void snapshot_create_action ( int vid ) ;
2015-05-26 12:24:34 +03:00
/**
* This function takes an snapshot of a disk
*/
void snapshot_revert_action ( int vid ) ;
2015-05-20 18:48:27 +03:00
/**
* This function deletes an snapshot of a disk
*/
void snapshot_delete_action ( int vid ) ;
2016-12-11 23:05:07 +03:00
/**
* This function resizes a VM disk
*/
void resize_action ( int vid ) ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*TRANSFER_MANAGER_H*/