VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleImplTeleporter.cpp@ 71848

Last change on this file since 71848 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette