ConsoleImpl-LiveMigration.cpp revision 1218a65d2078888b30c4646d1352a11b3fe2b467
/* $Id$ */
/** @file
* VBox Console COM Class implementation, The Live Migration Part.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "ConsoleImpl.h"
#include "Global.h"
#include "Logging.h"
#include "ProgressImpl.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Base class for the migration state.
*
* These classes are used as advanced structs, not as proper classes.
*/
class MigrationState
{
public:
/** @name stream stuff
* @{ */
bool volatile mfStopReading;
/** @} */
, mfStopReading(false)
{
}
};
/**
* Migration state used by the source side.
*/
class MigrationStateSrc : public MigrationState
{
public:
, muPort(UINT32_MAX)
{
}
};
/**
* Migration state used by the destiation side.
*/
class MigrationStateDst : public MigrationState
{
public:
void *mpvVMCallbackTask;
int mRc;
, mRc(VINF_SUCCESS)
{
}
};
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static const char g_szWelcome[] = "VirtualBox-LiveMigration-1.0\n";
/**
* Reads a string from the socket.
*
* @returns VBox status code.
*
* @param pState The live migration state structure.
* @param pszBuf The output buffer.
* @param cchBuf The size of the output buffer.
*
*/
{
/* dead simple (stupid) approach. */
for (;;)
{
char ch;
if (RT_FAILURE(rc))
{
return rc;
}
if ( ch == '\n'
|| ch == '\0')
return VINF_SUCCESS;
if (cchBuf <= 1)
{
return VERR_BUFFER_OVERFLOW;
}
*pszBuf = '\0';
cchBuf--;
}
}
{
if (RT_FAILURE(rc))
return rc;
}
{
if (RT_FAILURE(rc))
return rc;
}
/**
* Reads an ACK or NACK.
*
* @returns S_OK on ACK, E_FAIL+setError() on failure or NACK.
* @param pState The live migration source state.
* @param pszNAckMsg Optional NACK message.
*
* @remarks the setError laziness forces this to be a Console member.
*/
{
char szMsg[128];
if (RT_FAILURE(vrc))
{
}
return S_OK;
}
/**
* Submitts a command to the destination and waits for the ACK.
*
* @returns S_OK on ACKed command, E_FAIL+setError() on failure.
*
* @param pState The live migration source state.
* @param pszCommand The command.
*
* @remarks the setError laziness forces this to be a Console member.
*/
{
if (RT_SUCCESS(vrc))
if (RT_FAILURE(vrc))
return migrationSrcReadACK(pState);
}
/**
* @copydoc SSMSTRMOPS::pfnWrite
*/
static DECLCALLBACK(int) migrationTcpOpWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* @copydoc SSMSTRMOPS::pfnRead
*/
static DECLCALLBACK(int) migrationTcpOpRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
for (;;)
{
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
if (rc != VERR_TIMEOUT)
return rc;
if (pState->mfStopReading)
return VERR_EOF;
}
}
/**
* @copydoc SSMSTRMOPS::pfnSeek
*/
static DECLCALLBACK(int) migrationTcpOpSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
{
return VERR_NOT_SUPPORTED;
}
/**
* @copydoc SSMSTRMOPS::pfnTell
*/
{
return pState->moffStream;
}
/**
* @copydoc SSMSTRMOPS::pfnSize
*/
{
return VERR_NOT_SUPPORTED;
}
/**
* @copydoc SSMSTRMOPS::pfnClose
*/
{
return VINF_SUCCESS;
}
/**
* Method table for a TCP based stream.
*/
static SSMSTRMOPS const g_migrationTcpOps =
{
};
/**
* @copydoc FNRTTIMERLR
*/
{
/* This is harmless for any open connections. */
}
/**
* Do the live migration.
*
* @returns VBox status code.
* @param pState The migration state.
*/
{
AutoCaller autoCaller(this);
/*
* Wait for Console::Migrate to change the state.
*/
{ AutoWriteLock autoLock(); }
return hrc;
if (fCanceled)
/*
* Try connect to the destination machine.
* (Note. The caller cleans up mhSocket, so we can return without worries.)
*/
if (RT_FAILURE(vrc))
/* Read and check the welcome message. */
if (RT_FAILURE(vrc))
/* password */
if (RT_FAILURE(vrc))
/* ACK */
return hrc;
/*
* Do compatability checks of the VM config and the host hardware.
*/
/** @todo later */
/*
* Start loading the state.
*/
return hrc;
if (vrc)
return hrc;
/*
* State fun? Automatic power off?
*/
return S_OK;
}
/**
* Static thread method wrapper.
*
* @returns VINF_SUCCESS (ignored).
* @param hThread The thread.
* @param pvUser Pointer to a MigrationStateSrc instance.
*/
/*static*/ DECLCALLBACK(int)
{
{
switch (enmVMState)
{
case VMSTATE_RUNNING:
case VMSTATE_RUNNING_LS:
case VMSTATE_DEBUGGING:
case VMSTATE_DEBUGGING_LS:
case VMSTATE_POWERING_OFF:
case VMSTATE_POWERING_OFF_LS:
case VMSTATE_RESETTING:
case VMSTATE_RESETTING_LS:
break;
case VMSTATE_GURU_MEDITATION:
break;
default:
case VMSTATE_SUSPENDED:
case VMSTATE_SUSPENDED_LS:
case VMSTATE_SUSPENDING:
case VMSTATE_SUSPENDING_LS:
break;
}
}
{
}
delete pState;
return VINF_SUCCESS;
}
/**
* Start live migration to the specified target.
*
* @returns COM status code.
*
* @param aHostname The name of the target host.
* @param aPort The TCP port number.
* @param aPassword The password.
* @param aProgress Where to return the progress object.
*/
{
/*
* Validate parameters, check+hold object status, write lock the object
* and validate the state.
*/
AutoCaller autoCaller(this);
AutoWriteLock autoLock(this);
switch (mMachineState)
{
case MachineState_Running:
case MachineState_Paused:
break;
default:
return setError(VBOX_E_INVALID_VM_STATE,
tr("Invalid machine state: %s (must be Running, Paused or Stuck)"),
}
/*
* Create a progress object, spawn a worker thread and change the state.
* Note! The thread won't start working until we release the lock.
*/
LogFlowThisFunc(("Initiating LIVE MIGRATION request...\n"));
TRUE /*aCancelable*/);
if (RT_SUCCESS(vrc))
{
else
}
else
{
delete pState;
}
return hrc;
}
/**
* Creates a TCP server that listens for the source machine and passes control
* over to Console::migrationDstServeConnection().
*
* @returns VBox status code.
* @param pVM The VM handle
* @param pMachine The IMachine for the virtual machine.
* @param fStartPaused Whether to start it in the Paused (true) or
* Running (false) state,
* @param pvVMCallbackTask The callback task pointer for
* stateProgressCallback().
*/
int
{
/*
* Get the config.
*/
return VERR_GENERAL_FAILURE;
return VERR_GENERAL_FAILURE;
/** @todo Add a bind address property. */
/*
* Create the TCP server.
*/
int vrc;
if (uPort)
else
{
{
if (vrc != VERR_NET_ADDRESS_IN_USE)
break;
}
if (RT_SUCCESS(vrc))
{
{
return VERR_GENERAL_FAILURE;
}
}
}
if (RT_FAILURE(vrc))
return vrc;
/*
* Create a one-shot timer for timing out after 5 mins.
*/
if (RT_SUCCESS(vrc))
{
if (RT_SUCCESS(vrc))
{
/*
* Do the job, when it returns we're done.
*/
if (vrc == VERR_TCP_SERVER_STOP)
if (RT_SUCCESS(vrc))
{
if (fStartPaused)
else
}
else
{
}
}
}
return vrc;
}
/**
* @copydoc FNRTTCPSERVE
*/
/*static*/ DECLCALLBACK(int)
{
/*
* Say hello.
*/
if (RT_FAILURE(vrc))
{
return VINF_SUCCESS;
}
/*
* Password (includes '\n', see migrationDst). If it's right, tell
* the TCP server to stop listening (frees up host resources and makes sure
* this is the last connection attempt).
*/
unsigned off = 0;
while (pszPassword[off])
{
char ch;
if ( RT_FAILURE(vrc)
{
if (RT_FAILURE(vrc))
else
return VINF_SUCCESS;
}
off++;
}
if (RT_FAILURE(vrc))
return vrc;
/*
* Command processing loop.
*/
for (;;)
{
char szCmd[128];
if (RT_FAILURE(vrc))
break;
{
if (RT_FAILURE(vrc))
break;
pState->moffStream = 0;
if (RT_FAILURE(vrc))
{
break;
}
if (RT_FAILURE(vrc))
break;
}
/** @todo implement config verification and hardware compatability checks. Or
* maybe leave part of these to the saved state machinery? */
{
break;
}
else
{
break;
}
}
return VERR_TCP_SERVER_STOP;
}