VirtualBox

source: vbox/trunk/src/VBox/Main/ConsoleImplTeleporter.cpp@ 25414

Last change on this file since 25414 was 25310, checked in by vboxsync, 14 years ago

Main: lock validator, first batch: implement per-thread stack to trace locking (disabled by default, use VBOX_WITH_LOCK_VALIDATOR, but that WILL FAIL presently)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.9 KB
RevLine 
[23599]1/* $Id: ConsoleImplTeleporter.cpp 25310 2009-12-10 17:06:44Z vboxsync $ */
2/** @file
[23801]3 * VBox Console COM Class implementation, The Teleporter Part.
[23599]4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
[23686]22
[23599]23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include "ConsoleImpl.h"
[23663]27#include "Global.h"
[23599]28#include "Logging.h"
[23663]29#include "ProgressImpl.h"
[23599]30
[23626]31#include <iprt/err.h>
32#include <iprt/rand.h>
33#include <iprt/tcp.h>
34#include <iprt/timer.h>
35
[23599]36#include <VBox/vmapi.h>
37#include <VBox/ssm.h>
38#include <VBox/err.h>
39#include <VBox/version.h>
40#include <VBox/com/string.h>
41
42
[23626]43/*******************************************************************************
44* Structures and Typedefs *
45*******************************************************************************/
46/**
[23801]47 * Base class for the teleporter state.
[23663]48 *
49 * These classes are used as advanced structs, not as proper classes.
[23626]50 */
[23801]51class TeleporterState
[23626]52{
[23663]53public:
54 ComPtr<Console> mptrConsole;
55 PVM mpVM;
[23810]56 ComObjPtr<Progress> mptrProgress;
[23663]57 Utf8Str mstrPassword;
[23686]58 bool const mfIsSource;
[23626]59
[23663]60 /** @name stream stuff
61 * @{ */
62 RTSOCKET mhSocket;
63 uint64_t moffStream;
[23686]64 uint32_t mcbReadBlock;
[23669]65 bool volatile mfStopReading;
[23686]66 bool volatile mfEndOfStream;
67 bool volatile mfIOError;
[23663]68 /** @} */
[23626]69
[23810]70 TeleporterState(Console *pConsole, PVM pVM, Progress *pProgress, bool fIsSource)
[23663]71 : mptrConsole(pConsole)
72 , mpVM(pVM)
[23810]73 , mptrProgress(pProgress)
[23686]74 , mfIsSource(fIsSource)
[23663]75 , mhSocket(NIL_RTSOCKET)
76 , moffStream(UINT64_MAX / 2)
[23686]77 , mcbReadBlock(0)
[23669]78 , mfStopReading(false)
[23686]79 , mfEndOfStream(false)
80 , mfIOError(false)
[23663]81 {
82 }
83};
84
85
86/**
[23801]87 * Teleporter state used by the source side.
[23663]88 */
[23801]89class TeleporterStateSrc : public TeleporterState
[23663]90{
91public:
92 Utf8Str mstrHostname;
93 uint32_t muPort;
[24874]94 uint32_t mcMsMaxDowntime;
[23904]95 MachineState_T menmOldMachineState;
[24353]96 bool mfSuspendedByUs;
[24558]97 bool mfUnlockedMedia;
[23663]98
[23904]99 TeleporterStateSrc(Console *pConsole, PVM pVM, Progress *pProgress, MachineState_T enmOldMachineState)
[23810]100 : TeleporterState(pConsole, pVM, pProgress, true /*fIsSource*/)
[23663]101 , muPort(UINT32_MAX)
[24874]102 , mcMsMaxDowntime(250)
[23904]103 , menmOldMachineState(enmOldMachineState)
[24353]104 , mfSuspendedByUs(false)
[24558]105 , mfUnlockedMedia(false)
[23663]106 {
107 }
108};
109
110
111/**
[23801]112 * Teleporter state used by the destiation side.
[23663]113 */
[23801]114class TeleporterStateTrg : public TeleporterState
[23663]115{
116public:
[24558]117 IMachine *mpMachine;
118 IInternalMachineControl *mpControl;
119 PRTTCPSERVER mhServer;
120 PRTTIMERLR mphTimerLR;
121 bool mfLockedMedia;
122 int mRc;
[23663]123
[23810]124 TeleporterStateTrg(Console *pConsole, PVM pVM, Progress *pProgress,
[24558]125 IMachine *pMachine, IInternalMachineControl *pControl,
126 PRTTIMERLR phTimerLR, bool fStartPaused)
[23810]127 : TeleporterState(pConsole, pVM, pProgress, false /*fIsSource*/)
[23663]128 , mpMachine(pMachine)
[24558]129 , mpControl(pControl)
[23669]130 , mhServer(NULL)
[23692]131 , mphTimerLR(phTimerLR)
[24558]132 , mfLockedMedia(false)
[23663]133 , mRc(VINF_SUCCESS)
134 {
135 }
136};
137
138
[23686]139/**
140 * TCP stream header.
141 *
142 * This is an extra layer for fixing the problem with figuring out when the SSM
143 * stream ends.
144 */
[23801]145typedef struct TELEPORTERTCPHDR
[23686]146{
147 /** Magic value. */
148 uint32_t u32Magic;
149 /** The size of the data block following this header.
[24917]150 * 0 indicates the end of the stream, while UINT32_MAX indicates
151 * cancelation. */
[23686]152 uint32_t cb;
[23801]153} TELEPORTERTCPHDR;
154/** Magic value for TELEPORTERTCPHDR::u32Magic. (Egberto Gismonti Amin) */
155#define TELEPORTERTCPHDR_MAGIC UINT32_C(0x19471205)
[23686]156/** The max block size. */
[23801]157#define TELEPORTERTCPHDR_MAX_SIZE UINT32_C(0x00fffff8)
[23663]158
[23686]159
[23626]160/*******************************************************************************
161* Global Variables *
162*******************************************************************************/
[23801]163static const char g_szWelcome[] = "VirtualBox-Teleporter-1.0\n";
[23626]164
165
[23633]166/**
[23663]167 * Reads a string from the socket.
168 *
169 * @returns VBox status code.
170 *
[23801]171 * @param pState The teleporter state structure.
[23663]172 * @param pszBuf The output buffer.
173 * @param cchBuf The size of the output buffer.
174 *
175 */
[23801]176static int teleporterTcpReadLine(TeleporterState *pState, char *pszBuf, size_t cchBuf)
[23663]177{
178 char *pszStart = pszBuf;
179 RTSOCKET Sock = pState->mhSocket;
180
181 AssertReturn(cchBuf > 1, VERR_INTERNAL_ERROR);
[23721]182 *pszBuf = '\0';
[23663]183
[23721]184 /* dead simple approach. */
[23663]185 for (;;)
186 {
187 char ch;
188 int rc = RTTcpRead(Sock, &ch, sizeof(ch), NULL);
189 if (RT_FAILURE(rc))
190 {
[23801]191 LogRel(("Teleporter: RTTcpRead -> %Rrc while reading string ('%s')\n", rc, pszStart));
[23663]192 return rc;
193 }
194 if ( ch == '\n'
195 || ch == '\0')
196 return VINF_SUCCESS;
197 if (cchBuf <= 1)
198 {
[23801]199 LogRel(("Teleporter: String buffer overflow: '%s'\n", pszStart));
[23663]200 return VERR_BUFFER_OVERFLOW;
201 }
202 *pszBuf++ = ch;
[23721]203 *pszBuf = '\0';
[23663]204 cchBuf--;
205 }
206}
207
208
209/**
210 * Reads an ACK or NACK.
211 *
212 * @returns S_OK on ACK, E_FAIL+setError() on failure or NACK.
[23801]213 * @param pState The teleporter source state.
[23703]214 * @param pszWhich Which ACK is this this?
[23669]215 * @param pszNAckMsg Optional NACK message.
[23663]216 *
217 * @remarks the setError laziness forces this to be a Console member.
218 */
219HRESULT
[23801]220Console::teleporterSrcReadACK(TeleporterStateSrc *pState, const char *pszWhich,
[23703]221 const char *pszNAckMsg /*= NULL*/)
[23663]222{
223 char szMsg[128];
[23801]224 int vrc = teleporterTcpReadLine(pState, szMsg, sizeof(szMsg));
[23663]225 if (RT_FAILURE(vrc))
[23703]226 return setError(E_FAIL, tr("Failed reading ACK(%s): %Rrc"), pszWhich, vrc);
[23669]227 if (strcmp(szMsg, "ACK"))
[23663]228 {
[23692]229 if (!strncmp(szMsg, "NACK=", sizeof("NACK=") - 1))
230 {
231 int32_t vrc2;
232 vrc = RTStrToInt32Full(&szMsg[sizeof("NACK=") - 1], 10, &vrc2);
233 if (vrc == VINF_SUCCESS)
234 {
235 if (pszNAckMsg)
236 {
[23801]237 LogRel(("Teleporter: %s: NACK=%Rrc (%d)\n", pszWhich, vrc2, vrc2));
[23692]238 return setError(E_FAIL, pszNAckMsg);
239 }
[23703]240 return setError(E_FAIL, "NACK(%s) - %Rrc (%d)", pszWhich, vrc2, vrc2);
[23692]241 }
242 }
[23703]243 return setError(E_FAIL, tr("%s: Expected ACK or NACK, got '%s'"), pszWhich, szMsg);
[23663]244 }
245 return S_OK;
246}
247
[23686]248
[23663]249/**
250 * Submitts a command to the destination and waits for the ACK.
251 *
252 * @returns S_OK on ACKed command, E_FAIL+setError() on failure.
253 *
[23801]254 * @param pState The teleporter source state.
[23663]255 * @param pszCommand The command.
[23827]256 * @param fWaitForAck Whether to wait for the ACK.
[23663]257 *
258 * @remarks the setError laziness forces this to be a Console member.
259 */
260HRESULT
[23827]261Console::teleporterSrcSubmitCommand(TeleporterStateSrc *pState, const char *pszCommand, bool fWaitForAck /*= true*/)
[23663]262{
263 size_t cchCommand = strlen(pszCommand);
264 int vrc = RTTcpWrite(pState->mhSocket, pszCommand, cchCommand);
265 if (RT_SUCCESS(vrc))
266 vrc = RTTcpWrite(pState->mhSocket, "\n", sizeof("\n") - 1);
[23703]267 if (RT_SUCCESS(vrc))
268 vrc = RTTcpFlush(pState->mhSocket);
[23663]269 if (RT_FAILURE(vrc))
270 return setError(E_FAIL, tr("Failed writing command '%s': %Rrc"), pszCommand, vrc);
[23827]271 if (!fWaitForAck)
272 return S_OK;
[23801]273 return teleporterSrcReadACK(pState, pszCommand);
[23663]274}
275
276
277/**
[23633]278 * @copydoc SSMSTRMOPS::pfnWrite
279 */
[23801]280static DECLCALLBACK(int) teleporterTcpOpWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
[23633]281{
[23801]282 TeleporterState *pState = (TeleporterState *)pvUser;
[23686]283
284 AssertReturn(cbToWrite > 0, VINF_SUCCESS);
[24365]285 AssertReturn(cbToWrite < UINT32_MAX, VERR_OUT_OF_RANGE);
[23686]286 AssertReturn(pState->mfIsSource, VERR_INVALID_HANDLE);
287
288 for (;;)
[23633]289 {
[23686]290 /* Write block header. */
[23801]291 TELEPORTERTCPHDR Hdr;
292 Hdr.u32Magic = TELEPORTERTCPHDR_MAGIC;
[24365]293 Hdr.cb = RT_MIN((uint32_t)cbToWrite, TELEPORTERTCPHDR_MAX_SIZE);
[24895]294 int rc = RTTcpWrite(pState->mhSocket, &Hdr, sizeof(Hdr));
[23686]295 if (RT_FAILURE(rc))
296 {
[23801]297 LogRel(("Teleporter/TCP: Header write error: %Rrc\n", rc));
[23686]298 return rc;
299 }
300
301 /* Write the data. */
302 rc = RTTcpWrite(pState->mhSocket, pvBuf, Hdr.cb);
303 if (RT_FAILURE(rc))
304 {
[23801]305 LogRel(("Teleporter/TCP: Data write error: %Rrc (cb=%#x)\n", rc, Hdr.cb));
[23686]306 return rc;
307 }
308 pState->moffStream += Hdr.cb;
309 if (Hdr.cb == cbToWrite)
310 return VINF_SUCCESS;
311
312 /* advance */
313 cbToWrite -= Hdr.cb;
314 pvBuf = (uint8_t const *)pvBuf + Hdr.cb;
[23633]315 }
[23686]316}
317
318
319/**
320 * Selects and poll for close condition.
321 *
322 * We can use a relatively high poll timeout here since it's only used to get
323 * us out of error paths. In the normal cause of events, we'll get a
324 * end-of-stream header.
325 *
326 * @returns VBox status code.
327 *
[23801]328 * @param pState The teleporter state data.
[23686]329 */
[23801]330static int teleporterTcpReadSelect(TeleporterState *pState)
[23686]331{
332 int rc;
333 do
334 {
335 rc = RTTcpSelectOne(pState->mhSocket, 1000);
336 if (RT_FAILURE(rc) && rc != VERR_TIMEOUT)
337 {
338 pState->mfIOError = true;
[23801]339 LogRel(("Teleporter/TCP: Header select error: %Rrc\n", rc));
[23686]340 break;
341 }
342 if (pState->mfStopReading)
343 {
344 rc = VERR_EOF;
345 break;
346 }
347 } while (rc == VERR_TIMEOUT);
[23633]348 return rc;
349}
[23626]350
[23633]351
[23626]352/**
[23633]353 * @copydoc SSMSTRMOPS::pfnRead
354 */
[23801]355static DECLCALLBACK(int) teleporterTcpOpRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
[23633]356{
[23801]357 TeleporterState *pState = (TeleporterState *)pvUser;
[23686]358 AssertReturn(!pState->mfIsSource, VERR_INVALID_HANDLE);
[23669]359
360 for (;;)
[23633]361 {
[23686]362 int rc;
363
364 /*
365 * Check for various conditions and may have been signalled.
366 */
367 if (pState->mfEndOfStream)
368 return VERR_EOF;
369 if (pState->mfStopReading)
370 return VERR_EOF;
371 if (pState->mfIOError)
372 return VERR_IO_GEN_FAILURE;
373
374 /*
375 * If there is no more data in the current block, read the next
376 * block header.
377 */
378 if (!pState->mcbReadBlock)
[23669]379 {
[23801]380 rc = teleporterTcpReadSelect(pState);
[23669]381 if (RT_FAILURE(rc))
382 return rc;
[23801]383 TELEPORTERTCPHDR Hdr;
[23686]384 rc = RTTcpRead(pState->mhSocket, &Hdr, sizeof(Hdr), NULL);
385 if (RT_FAILURE(rc))
386 {
387 pState->mfIOError = true;
[23801]388 LogRel(("Teleporter/TCP: Header read error: %Rrc\n", rc));
[23686]389 return rc;
390 }
[24917]391
392 if (RT_UNLIKELY( Hdr.u32Magic != TELEPORTERTCPHDR_MAGIC
393 || Hdr.cb > TELEPORTERTCPHDR_MAX_SIZE
394 || Hdr.cb == 0))
[23686]395 {
[24917]396 if ( Hdr.u32Magic == TELEPORTERTCPHDR_MAGIC
397 && ( Hdr.cb == 0
398 || Hdr.cb == UINT32_MAX)
399 )
400 {
401 pState->mfEndOfStream = true;
402 pState->mcbReadBlock = 0;
403 return Hdr.cb ? VERR_SSM_CANCELLED : VERR_EOF;
404 }
[23686]405 pState->mfIOError = true;
[23801]406 LogRel(("Teleporter/TCP: Invalid block: u32Magic=%#x cb=%#x\n", Hdr.u32Magic, Hdr.cb));
[23686]407 return VERR_IO_GEN_FAILURE;
408 }
409
410 pState->mcbReadBlock = Hdr.cb;
411 if (pState->mfStopReading)
412 return VERR_EOF;
413 }
414
415 /*
416 * Read more data.
417 */
[23801]418 rc = teleporterTcpReadSelect(pState);
[23686]419 if (RT_FAILURE(rc))
420 return rc;
[24365]421 uint32_t cb = (uint32_t)RT_MIN(pState->mcbReadBlock, cbToRead);
[23686]422 rc = RTTcpRead(pState->mhSocket, pvBuf, cb, pcbRead);
423 if (RT_FAILURE(rc))
424 {
425 pState->mfIOError = true;
[23801]426 LogRel(("Teleporter/TCP: Data read error: %Rrc (cb=%#x)\n", rc, cb));
[23686]427 return rc;
428 }
429 if (pcbRead)
430 {
[24365]431 cb = (uint32_t)*pcbRead;
432 pState->moffStream += cb;
433 pState->mcbReadBlock -= cb;
[23669]434 return VINF_SUCCESS;
435 }
[23686]436 pState->moffStream += cb;
437 pState->mcbReadBlock -= cb;
438 if (cbToRead == cb)
439 return VINF_SUCCESS;
440
441 /* Advance to the next block. */
442 cbToRead -= cb;
443 pvBuf = (uint8_t *)pvBuf + cb;
[23633]444 }
445}
446
447
448/**
449 * @copydoc SSMSTRMOPS::pfnSeek
450 */
[23801]451static DECLCALLBACK(int) teleporterTcpOpSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
[23633]452{
453 return VERR_NOT_SUPPORTED;
454}
455
456
457/**
458 * @copydoc SSMSTRMOPS::pfnTell
459 */
[23801]460static DECLCALLBACK(uint64_t) teleporterTcpOpTell(void *pvUser)
[23633]461{
[23801]462 TeleporterState *pState = (TeleporterState *)pvUser;
[23663]463 return pState->moffStream;
[23633]464}
465
466
467/**
468 * @copydoc SSMSTRMOPS::pfnSize
469 */
[23801]470static DECLCALLBACK(int) teleporterTcpOpSize(void *pvUser, uint64_t *pcb)
[23633]471{
472 return VERR_NOT_SUPPORTED;
473}
474
475
476/**
[24895]477 * @copydoc SSMSTRMOPS::pfnIsOk
478 */
479static DECLCALLBACK(int) teleporterTcpOpIsOk(void *pvUser)
480{
481 TeleporterState *pState = (TeleporterState *)pvUser;
482
483 if (pState->mfIsSource)
484 {
485 /* Poll for incoming NACKs and errors from the other side */
486 int rc = RTTcpSelectOne(pState->mhSocket, 0);
487 if (rc != VERR_TIMEOUT)
488 {
489 if (RT_SUCCESS(rc))
490 {
[24896]491 LogRel(("Teleporter/TCP: Incoming data detect by IsOk, assuming it is a cancellation NACK.\n"));
[24895]492 rc = VERR_SSM_CANCELLED;
493 }
494 else
495 LogRel(("Teleporter/TCP: RTTcpSelectOne -> %Rrc (IsOk).\n", rc));
496 return rc;
497 }
498 }
499
500 return VINF_SUCCESS;
501}
502
503
504/**
[23633]505 * @copydoc SSMSTRMOPS::pfnClose
506 */
[24917]507static DECLCALLBACK(int) teleporterTcpOpClose(void *pvUser, bool fCancelled)
[23633]508{
[23801]509 TeleporterState *pState = (TeleporterState *)pvUser;
[23686]510
511 if (pState->mfIsSource)
512 {
[24917]513 TELEPORTERTCPHDR EofHdr;
514 EofHdr.u32Magic = TELEPORTERTCPHDR_MAGIC;
515 EofHdr.cb = fCancelled ? UINT32_MAX : 0;
[23686]516 int rc = RTTcpWrite(pState->mhSocket, &EofHdr, sizeof(EofHdr));
[23703]517 if (RT_SUCCESS(rc))
518 rc = RTTcpFlush(pState->mhSocket);
[23686]519 if (RT_FAILURE(rc))
520 {
[23801]521 LogRel(("Teleporter/TCP: EOF Header write error: %Rrc\n", rc));
[23686]522 return rc;
523 }
524 }
525 else
526 {
527 ASMAtomicWriteBool(&pState->mfStopReading, true);
528 RTTcpFlush(pState->mhSocket);
529 }
530
[23633]531 return VINF_SUCCESS;
532}
533
534
535/**
536 * Method table for a TCP based stream.
537 */
[23801]538static SSMSTRMOPS const g_teleporterTcpOps =
[23633]539{
540 SSMSTRMOPS_VERSION,
[23801]541 teleporterTcpOpWrite,
542 teleporterTcpOpRead,
543 teleporterTcpOpSeek,
544 teleporterTcpOpTell,
545 teleporterTcpOpSize,
[24895]546 teleporterTcpOpIsOk,
[23801]547 teleporterTcpOpClose,
[23633]548 SSMSTRMOPS_VERSION
549};
550
551
552/**
[23810]553 * Progress cancelation callback.
554 */
555static void teleporterProgressCancelCallback(void *pvUser)
556{
557 TeleporterState *pState = (TeleporterState *)pvUser;
558 SSMR3Cancel(pState->mpVM);
559 if (!pState->mfIsSource)
560 {
561 TeleporterStateTrg *pStateTrg = (TeleporterStateTrg *)pState;
562 RTTcpServerShutdown(pStateTrg->mhServer);
563 }
564}
565
566/**
567 * @copydoc PFNVMPROGRESS
568 */
569static DECLCALLBACK(int) teleporterProgressCallback(PVM pVM, unsigned uPercent, void *pvUser)
570{
571 TeleporterState *pState = (TeleporterState *)pvUser;
572 if (pState->mptrProgress)
573 {
574 HRESULT hrc = pState->mptrProgress->SetCurrentOperationProgress(uPercent);
575 if (FAILED(hrc))
576 {
577 /* check if the failure was caused by cancellation. */
[23827]578 BOOL fCancelled;
579 hrc = pState->mptrProgress->COMGETTER(Canceled)(&fCancelled);
580 if (SUCCEEDED(hrc) && fCancelled)
[23810]581 {
582 SSMR3Cancel(pState->mpVM);
583 return VERR_SSM_CANCELLED;
584 }
585 }
586 }
587
588 return VINF_SUCCESS;
589}
590
591
592/**
[23626]593 * @copydoc FNRTTIMERLR
594 */
[23810]595static DECLCALLBACK(void) teleporterDstTimeout(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick)
[23626]596{
597 /* This is harmless for any open connections. */
598 RTTcpServerShutdown((PRTTCPSERVER)pvUser);
599}
600
601
[23633]602/**
[23801]603 * Do the teleporter.
[23663]604 *
605 * @returns VBox status code.
[23801]606 * @param pState The teleporter state.
[23663]607 */
608HRESULT
[23801]609Console::teleporterSrc(TeleporterStateSrc *pState)
[23663]610{
611 AutoCaller autoCaller(this);
[25149]612 if (FAILED(autoCaller.rc())) return autoCaller.rc();
[23663]613
614 /*
[23801]615 * Wait for Console::Teleport to change the state.
[23663]616 */
[25310]617 { AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS); }
[23663]618
[23827]619 BOOL fCancelled = TRUE;
620 HRESULT hrc = pState->mptrProgress->COMGETTER(Canceled)(&fCancelled);
[23669]621 if (FAILED(hrc))
622 return hrc;
[23827]623 if (fCancelled)
624 return setError(E_FAIL, tr("cancelled"));
[23669]625
[23663]626 /*
627 * Try connect to the destination machine.
628 * (Note. The caller cleans up mhSocket, so we can return without worries.)
629 */
630 int vrc = RTTcpClientConnect(pState->mstrHostname.c_str(), pState->muPort, &pState->mhSocket);
[23669]631 if (RT_FAILURE(vrc))
[23663]632 return setError(E_FAIL, tr("Failed to connect to port %u on '%s': %Rrc"),
633 pState->muPort, pState->mstrHostname.c_str(), vrc);
634
635 /* Read and check the welcome message. */
636 char szLine[RT_MAX(128, sizeof(g_szWelcome))];
[23707]637 RT_ZERO(szLine);
[23663]638 vrc = RTTcpRead(pState->mhSocket, szLine, sizeof(g_szWelcome) - 1, NULL);
639 if (RT_FAILURE(vrc))
640 return setError(E_FAIL, tr("Failed to read welcome message: %Rrc"), vrc);
[23671]641 if (strcmp(szLine, g_szWelcome))
[23707]642 return setError(E_FAIL, tr("Unexpected welcome %.*Rhxs"), sizeof(g_szWelcome) - 1, szLine);
[23663]643
644 /* password */
645 pState->mstrPassword.append('\n');
646 vrc = RTTcpWrite(pState->mhSocket, pState->mstrPassword.c_str(), pState->mstrPassword.length());
647 if (RT_FAILURE(vrc))
648 return setError(E_FAIL, tr("Failed to send password: %Rrc"), vrc);
649
650 /* ACK */
[23801]651 hrc = teleporterSrcReadACK(pState, "password", tr("Invalid password"));
[23663]652 if (FAILED(hrc))
653 return hrc;
654
655 /*
656 * Start loading the state.
[24353]657 *
658 * Note! The saved state includes vital configuration data which will be
659 * verified against the VM config on the other end. This is all done
660 * in the first pass, so we should fail pretty promptly on misconfig.
[23663]661 */
[23801]662 hrc = teleporterSrcSubmitCommand(pState, "load");
[23663]663 if (FAILED(hrc))
664 return hrc;
665
[23801]666 void *pvUser = static_cast<void *>(static_cast<TeleporterState *>(pState));
[24874]667 vrc = VMR3Teleport(pState->mpVM, pState->mcMsMaxDowntime,
668 &g_teleporterTcpOps, pvUser,
669 teleporterProgressCallback, pvUser,
670 &pState->mfSuspendedByUs);
[23904]671 if (RT_FAILURE(vrc))
[23801]672 return setError(E_FAIL, tr("VMR3Teleport -> %Rrc"), vrc);
[23663]673
[23801]674 hrc = teleporterSrcReadACK(pState, "load-complete");
[23663]675 if (FAILED(hrc))
676 return hrc;
677
678 /*
[24353]679 * We're at the point of no return.
[23663]680 */
[23827]681 if (!pState->mptrProgress->notifyPointOfNoReturn())
682 {
683 teleporterSrcSubmitCommand(pState, "cancel", false /*fWaitForAck*/);
684 return E_FAIL;
685 }
[24558]686
687 /*
[24614]688 * Hand over any media which we might be sharing.
689 *
690 * Note! This is only important on localhost teleportations.
[24558]691 */
692 /** @todo Maybe we should only do this if it's a local teleportation... */
693 hrc = mControl->UnlockMedia();
694 if (FAILED(hrc))
695 return hrc;
696 pState->mfUnlockedMedia = true;
697
698 hrc = teleporterSrcSubmitCommand(pState, "lock-media");
699 if (FAILED(hrc))
700 return hrc;
701
[24614]702 /*
703 * The FINAL step is giving the target instructions how to proceed with the VM.
704 */
705 if ( vrc == VINF_SSM_LIVE_SUSPENDED
706 || pState->menmOldMachineState == MachineState_Paused)
707 hrc = teleporterSrcSubmitCommand(pState, "hand-over-paused");
708 else
709 hrc = teleporterSrcSubmitCommand(pState, "hand-over-resume");
[23703]710 if (FAILED(hrc))
711 return hrc;
[23663]712
[24353]713 /*
714 * teleporterSrcThreadWrapper will do the automatic power off because it
715 * has to release the AutoVMCaller.
716 */
[23663]717 return S_OK;
718}
719
720
721/**
722 * Static thread method wrapper.
723 *
724 * @returns VINF_SUCCESS (ignored).
725 * @param hThread The thread.
[23801]726 * @param pvUser Pointer to a TeleporterStateSrc instance.
[23663]727 */
728/*static*/ DECLCALLBACK(int)
[23801]729Console::teleporterSrcThreadWrapper(RTTHREAD hThread, void *pvUser)
[23663]730{
[23801]731 TeleporterStateSrc *pState = (TeleporterStateSrc *)pvUser;
[23663]732
[24353]733 /*
734 * Console::teleporterSrc does the work, we just grab onto the VM handle
735 * and do the cleanups afterwards.
736 */
[23703]737 AutoVMCaller autoVMCaller(pState->mptrConsole);
738 HRESULT hrc = autoVMCaller.rc();
739
740 if (SUCCEEDED(hrc))
[23801]741 hrc = pState->mptrConsole->teleporterSrc(pState);
[23703]742
[24614]743 /* Close the connection ASAP on so that the other side can complete. */
744 if (pState->mhSocket != NIL_RTSOCKET)
745 {
746 RTTcpClientClose(pState->mhSocket);
747 pState->mhSocket = NIL_RTSOCKET;
748 }
749
[24365]750 /* Aaarg! setMachineState trashes error info on Windows, so we have to
751 complete things here on failure instead of right before cleanup. */
752 if (FAILED(hrc))
753 pState->mptrProgress->notifyComplete(hrc);
754
[24353]755 /* We can no longer be cancelled (success), or it doesn't matter any longer (failure). */
[23827]756 pState->mptrProgress->setCancelCallback(NULL, NULL);
[23663]757
[24614]758 /*
759 * Write lock the console before resetting mptrCancelableProgress and
760 * fixing the state.
761 */
[25310]762 AutoWriteLock autoLock(pState->mptrConsole COMMA_LOCKVAL_SRC_POS);
[24353]763 pState->mptrConsole->mptrCancelableProgress.setNull();
764
765 VMSTATE const enmVMState = VMR3GetState(pState->mpVM);
766 MachineState_T const enmMachineState = pState->mptrConsole->mMachineState;
767 if (SUCCEEDED(hrc))
[23669]768 {
[24353]769 /*
770 * Automatically shut down the VM on success.
771 *
772 * Note! We have to release the VM caller object or we'll deadlock in
773 * powerDown.
774 */
775 AssertLogRelMsg(enmVMState == VMSTATE_SUSPENDED, ("%s\n", VMR3GetStateName(enmVMState)));
776 AssertLogRelMsg(enmMachineState == MachineState_TeleportingPausedVM, ("%s\n", Global::stringifyMachineState(enmMachineState)));
777
778 autoVMCaller.release();
[24899]779
780 pState->mptrConsole->mVMIsAlreadyPoweringOff = true; /* (Make sure we stick in the TeleportingPausedVM state.) */
[24353]781 hrc = pState->mptrConsole->powerDown();
[24899]782 pState->mptrConsole->mVMIsAlreadyPoweringOff = false;
783
[24365]784 pState->mptrProgress->notifyComplete(hrc);
[24353]785 }
786 else
787 {
788 /*
789 * Work the state machinery on failure.
790 *
791 * If the state is no longer 'Teleporting*', some other operation has
792 * canceled us and there is nothing we need to do here. In all other
793 * cases, we've failed one way or another.
794 */
795 if ( enmMachineState == MachineState_Teleporting
796 || enmMachineState == MachineState_TeleportingPausedVM
797 )
[23669]798 {
[24558]799 if (pState->mfUnlockedMedia)
800 {
801 ErrorInfoKeeper Oak;
802 HRESULT hrc2 = pState->mptrConsole->mControl->LockMedia();
803 if (FAILED(hrc2))
804 {
805 uint64_t StartMS = RTTimeMilliTS();
806 do
807 {
808 RTThreadSleep(2);
809 hrc2 = pState->mptrConsole->mControl->LockMedia();
810 } while ( FAILED(hrc2)
811 && RTTimeMilliTS() - StartMS < 2000);
812 }
813 if (SUCCEEDED(hrc2))
814 pState->mfUnlockedMedia = true;
815 else
816 LogRel(("FATAL ERROR: Failed to re-take the media locks. hrc2=%Rhrc\n", hrc2));
817 }
818
[23671]819 switch (enmVMState)
820 {
821 case VMSTATE_RUNNING:
822 case VMSTATE_RUNNING_LS:
823 case VMSTATE_DEBUGGING:
824 case VMSTATE_DEBUGGING_LS:
825 case VMSTATE_POWERING_OFF:
826 case VMSTATE_POWERING_OFF_LS:
827 case VMSTATE_RESETTING:
828 case VMSTATE_RESETTING_LS:
[24353]829 Assert(!pState->mfSuspendedByUs);
[24558]830 Assert(!pState->mfUnlockedMedia);
[24353]831 pState->mptrConsole->setMachineState(MachineState_Running);
[23671]832 break;
[24353]833
[23671]834 case VMSTATE_GURU_MEDITATION:
835 case VMSTATE_GURU_MEDITATION_LS:
836 pState->mptrConsole->setMachineState(MachineState_Stuck);
837 break;
[24353]838
839 case VMSTATE_FATAL_ERROR:
840 case VMSTATE_FATAL_ERROR_LS:
841 pState->mptrConsole->setMachineState(MachineState_Paused);
842 break;
843
[23671]844 default:
845 AssertMsgFailed(("%s\n", VMR3GetStateName(enmVMState)));
846 case VMSTATE_SUSPENDED:
847 case VMSTATE_SUSPENDED_LS:
848 case VMSTATE_SUSPENDING:
849 case VMSTATE_SUSPENDING_LS:
850 case VMSTATE_SUSPENDING_EXT_LS:
[24558]851 if (!pState->mfUnlockedMedia)
[24353]852 {
[24558]853 pState->mptrConsole->setMachineState(MachineState_Paused);
854 if (pState->mfSuspendedByUs)
855 {
856 autoLock.leave();
857 int rc = VMR3Resume(pState->mpVM);
858 AssertLogRelMsgRC(rc, ("VMR3Resume -> %Rrc\n", rc));
859 autoLock.enter();
860 }
[24353]861 }
[24558]862 else
863 {
864 /* Faking a guru meditation is the best I can think of doing here... */
865 pState->mptrConsole->setMachineState(MachineState_Stuck);
866 }
[23671]867 break;
868 }
869 }
[23669]870 }
[24353]871 autoLock.leave();
[23669]872
[23827]873 /*
874 * Cleanup.
875 */
[24614]876 Assert(pState->mhSocket == NIL_RTSOCKET);
[23663]877 delete pState;
878
[23827]879 return VINF_SUCCESS; /* ignored */
[23663]880}
881
882
883/**
[23801]884 * Start teleporter to the specified target.
[23633]885 *
886 * @returns COM status code.
887 *
[24874]888 * @param aHostname The name of the target host.
889 * @param aPort The TCP port number.
890 * @param aPassword The password.
891 * @param aMaxDowntime Max allowed "downtime" in milliseconds.
892 * @param aProgress Where to return the progress object.
[23633]893 */
[23599]894STDMETHODIMP
[24874]895Console::Teleport(IN_BSTR aHostname, ULONG aPort, IN_BSTR aPassword, ULONG aMaxDowntime, IProgress **aProgress)
[23599]896{
[23633]897 /*
[23663]898 * Validate parameters, check+hold object status, write lock the object
899 * and validate the state.
[23633]900 */
[23663]901 CheckComArgOutPointerValid(aProgress);
902 CheckComArgStrNotEmptyOrNull(aHostname);
903 CheckComArgNotNull(aHostname);
904 CheckComArgExprMsg(aPort, aPort > 0 && aPort <= 65535, ("is %u", aPort));
[24874]905 CheckComArgExprMsg(aMaxDowntime, aMaxDowntime > 0, ("is %u", aMaxDowntime));
[23633]906
[23663]907 AutoCaller autoCaller(this);
[25149]908 if (FAILED(autoCaller.rc())) return autoCaller.rc();
[23663]909
[25310]910 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
[23663]911 LogFlowThisFunc(("mMachineState=%d\n", mMachineState));
912
913 switch (mMachineState)
914 {
915 case MachineState_Running:
916 case MachineState_Paused:
917 break;
918
919 default:
920 return setError(VBOX_E_INVALID_VM_STATE,
[24301]921 tr("Invalid machine state: %s (must be Running or Paused)"),
[23663]922 Global::stringifyMachineState(mMachineState));
923 }
924
925
[23633]926 /*
[23663]927 * Create a progress object, spawn a worker thread and change the state.
928 * Note! The thread won't start working until we release the lock.
[23633]929 */
[24353]930 LogFlowThisFunc(("Initiating TELEPORT request...\n"));
[23633]931
[23810]932 ComObjPtr<Progress> ptrProgress;
933 HRESULT hrc = ptrProgress.createObject();
[25149]934 if (FAILED(hrc)) return hrc;
[24353]935 hrc = ptrProgress->init(static_cast<IConsole *>(this), Bstr(tr("Teleporter")), TRUE /*aCancelable*/);
[25149]936 if (FAILED(hrc)) return hrc;
[23663]937
[23904]938 TeleporterStateSrc *pState = new TeleporterStateSrc(this, mpVM, ptrProgress, mMachineState);
[24874]939 pState->mstrPassword = aPassword;
940 pState->mstrHostname = aHostname;
941 pState->muPort = aPort;
942 pState->mcMsMaxDowntime = aMaxDowntime;
[23663]943
[23810]944 void *pvUser = static_cast<void *>(static_cast<TeleporterState *>(pState));
945 ptrProgress->setCancelCallback(teleporterProgressCancelCallback, pvUser);
946
[23801]947 int vrc = RTThreadCreate(NULL, Console::teleporterSrcThreadWrapper, (void *)pState, 0 /*cbStack*/,
948 RTTHREADTYPE_EMULATION, 0 /*fFlags*/, "Teleport");
[23663]949 if (RT_SUCCESS(vrc))
[23669]950 {
[24301]951 if (mMachineState == MachineState_Running)
952 hrc = setMachineState(MachineState_Teleporting);
953 else
954 hrc = setMachineState(MachineState_TeleportingPausedVM);
[23669]955 if (SUCCEEDED(hrc))
[24353]956 {
[23810]957 ptrProgress.queryInterfaceTo(aProgress);
[24353]958 mptrCancelableProgress = ptrProgress;
959 }
[23669]960 else
[23810]961 ptrProgress->Cancel();
[23669]962 }
963 else
964 {
[23810]965 ptrProgress->setCancelCallback(NULL, NULL);
[23663]966 delete pState;
[23669]967 hrc = setError(E_FAIL, tr("RTThreadCreate -> %Rrc"), vrc);
968 }
[23663]969
[23669]970 return hrc;
[23599]971}
972
[23626]973
974/**
975 * Creates a TCP server that listens for the source machine and passes control
[23801]976 * over to Console::teleporterTrgServeConnection().
[23626]977 *
978 * @returns VBox status code.
979 * @param pVM The VM handle
980 * @param pMachine The IMachine for the virtual machine.
[23669]981 * @param fStartPaused Whether to start it in the Paused (true) or
982 * Running (false) state,
[23810]983 * @param pProgress Pointer to the progress object.
[23841]984 *
985 * @remarks The caller expects error information to be set on failure.
986 * @todo Check that all the possible failure paths sets error info...
[23626]987 */
[23606]988int
[23810]989Console::teleporterTrg(PVM pVM, IMachine *pMachine, bool fStartPaused, Progress *pProgress)
[23606]990{
991 /*
992 * Get the config.
993 */
994 ULONG uPort;
[23801]995 HRESULT hrc = pMachine->COMGETTER(TeleporterPort)(&uPort);
[23606]996 if (FAILED(hrc))
997 return VERR_GENERAL_FAILURE;
[23804]998 ULONG const uPortOrg = uPort;
[23599]999
[23801]1000 Bstr bstrAddress;
1001 hrc = pMachine->COMGETTER(TeleporterAddress)(bstrAddress.asOutParam());
1002 if (FAILED(hrc))
1003 return VERR_GENERAL_FAILURE;
1004 Utf8Str strAddress(bstrAddress);
1005 const char *pszAddress = strAddress.isEmpty() ? NULL : strAddress.c_str();
1006
[23606]1007 Bstr bstrPassword;
[23801]1008 hrc = pMachine->COMGETTER(TeleporterPassword)(bstrPassword.asOutParam());
[23606]1009 if (FAILED(hrc))
1010 return VERR_GENERAL_FAILURE;
1011 Utf8Str strPassword(bstrPassword);
[23669]1012 strPassword.append('\n'); /* To simplify password checking. */
[23606]1013
1014 /*
1015 * Create the TCP server.
1016 */
[23663]1017 int vrc;
[23626]1018 PRTTCPSERVER hServer;
1019 if (uPort)
[23801]1020 vrc = RTTcpServerCreateEx(pszAddress, uPort, &hServer);
[23626]1021 else
1022 {
1023 for (int cTries = 10240; cTries > 0; cTries--)
1024 {
1025 uPort = RTRandU32Ex(cTries >= 8192 ? 49152 : 1024, 65534);
[23801]1026 vrc = RTTcpServerCreateEx(pszAddress, uPort, &hServer);
[23663]1027 if (vrc != VERR_NET_ADDRESS_IN_USE)
[23626]1028 break;
1029 }
[23663]1030 if (RT_SUCCESS(vrc))
[23626]1031 {
[23827]1032 hrc = pMachine->COMSETTER(TeleporterPort)(uPort);
[23626]1033 if (FAILED(hrc))
1034 {
1035 RTTcpServerDestroy(hServer);
1036 return VERR_GENERAL_FAILURE;
1037 }
1038 }
1039 }
[23663]1040 if (RT_FAILURE(vrc))
1041 return vrc;
[23606]1042
[23626]1043 /*
[23633]1044 * Create a one-shot timer for timing out after 5 mins.
[23626]1045 */
[23633]1046 RTTIMERLR hTimerLR;
[23810]1047 vrc = RTTimerLRCreateEx(&hTimerLR, 0 /*ns*/, RTTIMER_FLAGS_CPU_ANY, teleporterDstTimeout, hServer);
[23663]1048 if (RT_SUCCESS(vrc))
[23626]1049 {
[23663]1050 vrc = RTTimerLRStart(hTimerLR, 5*60*UINT64_C(1000000000) /*ns*/);
1051 if (RT_SUCCESS(vrc))
[23626]1052 {
1053 /*
1054 * Do the job, when it returns we're done.
1055 */
[24989]1056 TeleporterStateTrg theState(this, pVM, pProgress, pMachine, mControl, &hTimerLR, fStartPaused);
1057 theState.mstrPassword = strPassword;
1058 theState.mhServer = hServer;
[23633]1059
[24989]1060 void *pvUser = static_cast<void *>(static_cast<TeleporterState *>(&theState));
[23827]1061 if (pProgress->setCancelCallback(teleporterProgressCancelCallback, pvUser))
1062 {
[24878]1063 LogRel(("Teleporter: Waiting for incoming VM...\n"));
[24989]1064 vrc = RTTcpServerListen(hServer, Console::teleporterTrgServeConnection, &theState);
[23827]1065 pProgress->setCancelCallback(NULL, NULL);
[23810]1066
[23827]1067 bool fPowerOff = false;
1068 if (vrc == VERR_TCP_SERVER_STOP)
1069 {
[24989]1070 vrc = theState.mRc;
[23827]1071 /* Power off the VM on failure unless the state callback
1072 already did that. */
[24614]1073 if (RT_FAILURE(vrc))
[23827]1074 {
1075 VMSTATE enmVMState = VMR3GetState(pVM);
1076 if ( enmVMState != VMSTATE_OFF
1077 && enmVMState != VMSTATE_POWERING_OFF)
1078 fPowerOff = true;
1079 }
1080 }
1081 else if (vrc == VERR_TCP_SERVER_SHUTDOWN)
1082 {
1083 BOOL fCancelled = TRUE;
1084 hrc = pProgress->COMGETTER(Canceled)(&fCancelled);
1085 if (FAILED(hrc) || fCancelled)
1086 {
1087 setError(E_FAIL, tr("Teleporting canceled"));
1088 vrc = VERR_SSM_CANCELLED;
1089 }
1090 else
1091 {
1092 setError(E_FAIL, tr("Teleporter timed out waiting for incoming connection"));
1093 vrc = VERR_TIMEOUT;
1094 }
1095 LogRel(("Teleporter: RTTcpServerListen aborted - %Rrc\n", vrc));
1096 fPowerOff = true;
1097 }
[23669]1098 else
[23827]1099 {
1100 LogRel(("Teleporter: Unexpected RTTcpServerListen rc: %Rrc\n", vrc));
1101 vrc = VERR_IPE_UNEXPECTED_STATUS;
1102 fPowerOff = true;
1103 }
1104
1105 if (fPowerOff)
1106 {
1107 int vrc2 = VMR3PowerOff(pVM);
1108 AssertRC(vrc2);
1109 }
[23669]1110 }
1111 else
[23827]1112 vrc = VERR_SSM_CANCELLED;
[23626]1113 }
[23606]1114
[23633]1115 RTTimerLRDestroy(hTimerLR);
[23626]1116 }
1117 RTTcpServerDestroy(hServer);
1118
[23804]1119 /*
1120 * If we change TeleporterPort above, set it back to it's original
1121 * value before returning.
1122 */
1123 if (uPortOrg != uPort)
1124 pMachine->COMSETTER(TeleporterPort)(uPortOrg);
1125
[23663]1126 return vrc;
[23606]1127}
1128
[23633]1129
[24558]1130/**
1131 * Unlock the media.
1132 *
1133 * This is used in error paths.
1134 *
1135 * @param pState The teleporter state.
1136 */
1137static void teleporterTrgUnlockMedia(TeleporterStateTrg *pState)
1138{
1139 if (pState->mfLockedMedia)
1140 {
1141 pState->mpControl->UnlockMedia();
1142 pState->mfLockedMedia = false;
1143 }
1144}
1145
1146
[24614]1147static int teleporterTcpWriteACK(TeleporterStateTrg *pState, bool fAutomaticUnlock = true)
[23692]1148{
1149 int rc = RTTcpWrite(pState->mhSocket, "ACK\n", sizeof("ACK\n") - 1);
1150 if (RT_FAILURE(rc))
[24558]1151 {
[23801]1152 LogRel(("Teleporter: RTTcpWrite(,ACK,) -> %Rrc\n", rc));
[24614]1153 if (fAutomaticUnlock)
1154 teleporterTrgUnlockMedia(pState);
[24558]1155 }
[23692]1156 RTTcpFlush(pState->mhSocket);
1157 return rc;
1158}
1159
1160
[23801]1161static int teleporterTcpWriteNACK(TeleporterStateTrg *pState, int32_t rc2)
[23692]1162{
[24558]1163 /*
1164 * Unlock media sending the NACK. That way the other doesn't have to spin
1165 * waiting to regain the locks.
1166 */
1167 teleporterTrgUnlockMedia(pState);
1168
[23692]1169 char szMsg[64];
1170 size_t cch = RTStrPrintf(szMsg, sizeof(szMsg), "NACK=%d\n", rc2);
1171 int rc = RTTcpWrite(pState->mhSocket, szMsg, cch);
1172 if (RT_FAILURE(rc))
[23801]1173 LogRel(("Teleporter: RTTcpWrite(,%s,%zu) -> %Rrc\n", szMsg, cch, rc));
[23692]1174 RTTcpFlush(pState->mhSocket);
1175 return rc;
1176}
1177
1178
[23626]1179/**
1180 * @copydoc FNRTTCPSERVE
[23827]1181 *
1182 * @returns VINF_SUCCESS or VERR_TCP_SERVER_STOP.
[23626]1183 */
1184/*static*/ DECLCALLBACK(int)
[23801]1185Console::teleporterTrgServeConnection(RTSOCKET Sock, void *pvUser)
[23626]1186{
[23801]1187 TeleporterStateTrg *pState = (TeleporterStateTrg *)pvUser;
[23669]1188 pState->mhSocket = Sock;
[23626]1189
1190 /*
1191 * Say hello.
1192 */
[23663]1193 int vrc = RTTcpWrite(Sock, g_szWelcome, sizeof(g_szWelcome) - 1);
1194 if (RT_FAILURE(vrc))
[23626]1195 {
[23801]1196 LogRel(("Teleporter: Failed to write welcome message: %Rrc\n", vrc));
[23626]1197 return VINF_SUCCESS;
1198 }
1199
1200 /*
[23827]1201 * Password (includes '\n', see teleporterTrg).
[23626]1202 */
[23663]1203 const char *pszPassword = pState->mstrPassword.c_str();
[23633]1204 unsigned off = 0;
[23626]1205 while (pszPassword[off])
1206 {
1207 char ch;
[23663]1208 vrc = RTTcpRead(Sock, &ch, sizeof(ch), NULL);
1209 if ( RT_FAILURE(vrc)
1210 || pszPassword[off] != ch)
[23626]1211 {
[23663]1212 if (RT_FAILURE(vrc))
[23801]1213 LogRel(("Teleporter: Password read failure (off=%u): %Rrc\n", off, vrc));
[23663]1214 else
[23801]1215 LogRel(("Teleporter: Invalid password (off=%u)\n", off));
1216 teleporterTcpWriteNACK(pState, VERR_AUTHENTICATION_FAILURE);
[23626]1217 return VINF_SUCCESS;
1218 }
1219 off++;
1220 }
[23801]1221 vrc = teleporterTcpWriteACK(pState);
[23663]1222 if (RT_FAILURE(vrc))
[23827]1223 return VINF_SUCCESS;
[24878]1224 LogRel(("Teleporter: Incoming VM!\n"));
[23692]1225
[23827]1226 /*
1227 * Stop the server and cancel the timeout timer.
1228 *
1229 * Note! After this point we must return VERR_TCP_SERVER_STOP, while prior
1230 * to it we must not return that value!
1231 */
[23669]1232 RTTcpServerShutdown(pState->mhServer);
[23692]1233 RTTimerLRDestroy(*pState->mphTimerLR);
1234 *pState->mphTimerLR = NIL_RTTIMERLR;
[23626]1235
1236 /*
1237 * Command processing loop.
1238 */
[24558]1239 bool fDone = false;
[23626]1240 for (;;)
1241 {
1242 char szCmd[128];
[23801]1243 vrc = teleporterTcpReadLine(pState, szCmd, sizeof(szCmd));
[23663]1244 if (RT_FAILURE(vrc))
[23626]1245 break;
1246
[23633]1247 if (!strcmp(szCmd, "load"))
[23626]1248 {
[23801]1249 vrc = teleporterTcpWriteACK(pState);
[23663]1250 if (RT_FAILURE(vrc))
1251 break;
1252
1253 pState->moffStream = 0;
[24989]1254 void *pvUser2 = static_cast<void *>(static_cast<TeleporterState *>(pState));
1255 vrc = VMR3LoadFromStream(pState->mpVM, &g_teleporterTcpOps, pvUser2,
1256 teleporterProgressCallback, pvUser2);
[23663]1257 if (RT_FAILURE(vrc))
[23633]1258 {
[23801]1259 LogRel(("Teleporter: VMR3LoadFromStream -> %Rrc\n", vrc));
1260 teleporterTcpWriteNACK(pState, vrc);
[23633]1261 break;
1262 }
[23663]1263
[23721]1264 /* The EOS might not have been read, make sure it is. */
1265 pState->mfStopReading = false;
1266 size_t cbRead;
[24989]1267 vrc = teleporterTcpOpRead(pvUser2, pState->moffStream, szCmd, 1, &cbRead);
[23721]1268 if (vrc != VERR_EOF)
1269 {
[23801]1270 LogRel(("Teleporter: Draining teleporterTcpOpRead -> %Rrc\n", vrc));
1271 teleporterTcpWriteNACK(pState, vrc);
[23721]1272 break;
1273 }
1274
[23801]1275 vrc = teleporterTcpWriteACK(pState);
[23626]1276 }
[23827]1277 else if (!strcmp(szCmd, "cancel"))
1278 {
1279 /* Don't ACK this. */
1280 LogRel(("Teleporter: Received cancel command.\n"));
1281 vrc = VERR_SSM_CANCELLED;
1282 }
[24558]1283 else if (!strcmp(szCmd, "lock-media"))
1284 {
1285 HRESULT hrc = pState->mpControl->LockMedia();
1286 if (SUCCEEDED(hrc))
1287 {
1288 pState->mfLockedMedia = true;
1289 vrc = teleporterTcpWriteACK(pState);
1290 }
1291 else
1292 {
1293 vrc = VERR_FILE_LOCK_FAILED;
1294 teleporterTcpWriteNACK(pState, vrc);
1295 }
1296 }
[24614]1297 else if ( !strcmp(szCmd, "hand-over-resume")
1298 || !strcmp(szCmd, "hand-over-paused"))
[23663]1299 {
[23827]1300 /*
[24614]1301 * Point of no return.
1302 *
1303 * Note! Since we cannot tell whether a VMR3Resume failure is
1304 * destructive for the source or not, we have little choice
1305 * but to ACK it first and take any failures locally.
1306 *
1307 * Ideally, we should try resume it first and then ACK (or
1308 * NACK) the request since this would reduce latency and
1309 * make it possible to recover from some VMR3Resume failures.
[23827]1310 */
[24558]1311 if ( pState->mptrProgress->notifyPointOfNoReturn()
1312 && pState->mfLockedMedia)
[24614]1313 {
[23827]1314 vrc = teleporterTcpWriteACK(pState);
[24614]1315 if (RT_SUCCESS(vrc))
1316 {
1317 if (!strcmp(szCmd, "hand-over-resume"))
1318 vrc = VMR3Resume(pState->mpVM);
1319 else
1320 pState->mptrConsole->setMachineState(MachineState_Paused);
1321 fDone = true;
1322 break;
1323 }
1324 }
[23827]1325 else
1326 {
[24558]1327 vrc = pState->mfLockedMedia ? VERR_WRONG_ORDER : VERR_SSM_CANCELLED;
[23827]1328 teleporterTcpWriteNACK(pState, vrc);
1329 }
[23663]1330 }
[23626]1331 else
1332 {
[23801]1333 LogRel(("Teleporter: Unknown command '%s' (%.*Rhxs)\n", szCmd, strlen(szCmd), szCmd));
[23703]1334 vrc = VERR_NOT_IMPLEMENTED;
[23801]1335 teleporterTcpWriteNACK(pState, vrc);
[23626]1336 }
[23904]1337
1338 if (RT_FAILURE(vrc))
1339 break;
[23626]1340 }
[23669]1341
[24558]1342 if (RT_SUCCESS(vrc) && !fDone)
1343 vrc = VERR_WRONG_ORDER;
1344 if (RT_FAILURE(vrc))
1345 teleporterTrgUnlockMedia(pState);
1346
[23692]1347 pState->mRc = vrc;
[23663]1348 pState->mhSocket = NIL_RTSOCKET;
[23692]1349 LogFlowFunc(("returns mRc=%Rrc\n", vrc));
[23626]1350 return VERR_TCP_SERVER_STOP;
1351}
1352
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use