1 | /* $Id: ConsoleImplTeleporter.cpp 29250 2010-05-09 17:53:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Console COM Class implementation, The Teleporter Part.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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 | #include "ConsoleImpl.h"
|
---|
23 | #include "Global.h"
|
---|
24 | #include "ProgressImpl.h"
|
---|
25 |
|
---|
26 | #include "AutoCaller.h"
|
---|
27 | #include "Logging.h"
|
---|
28 |
|
---|
29 | #include <iprt/asm.h>
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/rand.h>
|
---|
32 | #include <iprt/socket.h>
|
---|
33 | #include <iprt/tcp.h>
|
---|
34 | #include <iprt/timer.h>
|
---|
35 |
|
---|
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 |
|
---|
43 | /*******************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *******************************************************************************/
|
---|
46 | /**
|
---|
47 | * Base class for the teleporter state.
|
---|
48 | *
|
---|
49 | * These classes are used as advanced structs, not as proper classes.
|
---|
50 | */
|
---|
51 | class TeleporterState
|
---|
52 | {
|
---|
53 | public:
|
---|
54 | ComPtr<Console> mptrConsole;
|
---|
55 | PVM mpVM;
|
---|
56 | ComObjPtr<Progress> mptrProgress;
|
---|
57 | Utf8Str mstrPassword;
|
---|
58 | bool const mfIsSource;
|
---|
59 |
|
---|
60 | /** @name stream stuff
|
---|
61 | * @{ */
|
---|
62 | RTSOCKET mhSocket;
|
---|
63 | uint64_t moffStream;
|
---|
64 | uint32_t mcbReadBlock;
|
---|
65 | bool volatile mfStopReading;
|
---|
66 | bool volatile mfEndOfStream;
|
---|
67 | bool volatile mfIOError;
|
---|
68 | /** @} */
|
---|
69 |
|
---|
70 | TeleporterState(Console *pConsole, PVM pVM, Progress *pProgress, bool fIsSource)
|
---|
71 | : mptrConsole(pConsole)
|
---|
72 | , mpVM(pVM)
|
---|
73 | , mptrProgress(pProgress)
|
---|
74 | , mfIsSource(fIsSource)
|
---|
75 | , mhSocket(NIL_RTSOCKET)
|
---|
76 | , moffStream(UINT64_MAX / 2)
|
---|
77 | , mcbReadBlock(0)
|
---|
78 | , mfStopReading(false)
|
---|
79 | , mfEndOfStream(false)
|
---|
80 | , mfIOError(false)
|
---|
81 | {
|
---|
82 | }
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Teleporter state used by the source side.
|
---|
88 | */
|
---|
89 | class TeleporterStateSrc : public TeleporterState
|
---|
90 | {
|
---|
91 | public:
|
---|
92 | Utf8Str mstrHostname;
|
---|
93 | uint32_t muPort;
|
---|
94 | uint32_t mcMsMaxDowntime;
|
---|
95 | MachineState_T menmOldMachineState;
|
---|
96 | bool mfSuspendedByUs;
|
---|
97 | bool mfUnlockedMedia;
|
---|
98 |
|
---|
99 | TeleporterStateSrc(Console *pConsole, PVM pVM, Progress *pProgress, MachineState_T enmOldMachineState)
|
---|
100 | : TeleporterState(pConsole, pVM, pProgress, true /*fIsSource*/)
|
---|
101 | , muPort(UINT32_MAX)
|
---|
102 | , mcMsMaxDowntime(250)
|
---|
103 | , menmOldMachineState(enmOldMachineState)
|
---|
104 | , mfSuspendedByUs(false)
|
---|
105 | , mfUnlockedMedia(false)
|
---|
106 | {
|
---|
107 | }
|
---|
108 | };
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Teleporter state used by the destiation side.
|
---|
113 | */
|
---|
114 | class TeleporterStateTrg : public TeleporterState
|
---|
115 | {
|
---|
116 | public:
|
---|
117 | IMachine *mpMachine;
|
---|
118 | IInternalMachineControl *mpControl;
|
---|
119 | PRTTCPSERVER mhServer;
|
---|
120 | PRTTIMERLR mphTimerLR;
|
---|
121 | bool mfLockedMedia;
|
---|
122 | int mRc;
|
---|
123 |
|
---|
124 | TeleporterStateTrg(Console *pConsole, PVM pVM, Progress *pProgress,
|
---|
125 | IMachine *pMachine, IInternalMachineControl *pControl,
|
---|
126 | PRTTIMERLR phTimerLR, bool fStartPaused)
|
---|
127 | : TeleporterState(pConsole, pVM, pProgress, false /*fIsSource*/)
|
---|
128 | , mpMachine(pMachine)
|
---|
129 | , mpControl(pControl)
|
---|
130 | , mhServer(NULL)
|
---|
131 | , mphTimerLR(phTimerLR)
|
---|
132 | , mfLockedMedia(false)
|
---|
133 | , mRc(VINF_SUCCESS)
|
---|
134 | {
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 |
|
---|
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 | */
|
---|
145 | typedef struct TELEPORTERTCPHDR
|
---|
146 | {
|
---|
147 | /** Magic value. */
|
---|
148 | uint32_t u32Magic;
|
---|
149 | /** The size of the data block following this header.
|
---|
150 | * 0 indicates the end of the stream, while UINT32_MAX indicates
|
---|
151 | * cancelation. */
|
---|
152 | uint32_t cb;
|
---|
153 | } TELEPORTERTCPHDR;
|
---|
154 | /** Magic value for TELEPORTERTCPHDR::u32Magic. (Egberto Gismonti Amin) */
|
---|
155 | #define TELEPORTERTCPHDR_MAGIC UINT32_C(0x19471205)
|
---|
156 | /** The max block size. */
|
---|
157 | #define TELEPORTERTCPHDR_MAX_SIZE UINT32_C(0x00fffff8)
|
---|
158 |
|
---|
159 |
|
---|
160 | /*******************************************************************************
|
---|
161 | * Global Variables *
|
---|
162 | *******************************************************************************/
|
---|
163 | static const char g_szWelcome[] = "VirtualBox-Teleporter-1.0\n";
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Reads a string from the socket.
|
---|
168 | *
|
---|
169 | * @returns VBox status code.
|
---|
170 | *
|
---|
171 | * @param pState The teleporter state structure.
|
---|
172 | * @param pszBuf The output buffer.
|
---|
173 | * @param cchBuf The size of the output buffer.
|
---|
174 | *
|
---|
175 | */
|
---|
176 | static int teleporterTcpReadLine(TeleporterState *pState, char *pszBuf, size_t cchBuf)
|
---|
177 | {
|
---|
178 | char *pszStart = pszBuf;
|
---|
179 | RTSOCKET Sock = pState->mhSocket;
|
---|
180 |
|
---|
181 | AssertReturn(cchBuf > 1, VERR_INTERNAL_ERROR);
|
---|
182 | *pszBuf = '\0';
|
---|
183 |
|
---|
184 | /* dead simple approach. */
|
---|
185 | for (;;)
|
---|
186 | {
|
---|
187 | char ch;
|
---|
188 | int rc = RTTcpRead(Sock, &ch, sizeof(ch), NULL);
|
---|
189 | if (RT_FAILURE(rc))
|
---|
190 | {
|
---|
191 | LogRel(("Teleporter: RTTcpRead -> %Rrc while reading string ('%s')\n", rc, pszStart));
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 | if ( ch == '\n'
|
---|
195 | || ch == '\0')
|
---|
196 | return VINF_SUCCESS;
|
---|
197 | if (cchBuf <= 1)
|
---|
198 | {
|
---|
199 | LogRel(("Teleporter: String buffer overflow: '%s'\n", pszStart));
|
---|
200 | return VERR_BUFFER_OVERFLOW;
|
---|
201 | }
|
---|
202 | *pszBuf++ = ch;
|
---|
203 | *pszBuf = '\0';
|
---|
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.
|
---|
213 | * @param pState The teleporter source state.
|
---|
214 | * @param pszWhich Which ACK is this this?
|
---|
215 | * @param pszNAckMsg Optional NACK message.
|
---|
216 | *
|
---|
217 | * @remarks the setError laziness forces this to be a Console member.
|
---|
218 | */
|
---|
219 | HRESULT
|
---|
220 | Console::teleporterSrcReadACK(TeleporterStateSrc *pState, const char *pszWhich,
|
---|
221 | const char *pszNAckMsg /*= NULL*/)
|
---|
222 | {
|
---|
223 | char szMsg[128];
|
---|
224 | int vrc = teleporterTcpReadLine(pState, szMsg, sizeof(szMsg));
|
---|
225 | if (RT_FAILURE(vrc))
|
---|
226 | return setError(E_FAIL, tr("Failed reading ACK(%s): %Rrc"), pszWhich, vrc);
|
---|
227 | if (strcmp(szMsg, "ACK"))
|
---|
228 | {
|
---|
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 | {
|
---|
237 | LogRel(("Teleporter: %s: NACK=%Rrc (%d)\n", pszWhich, vrc2, vrc2));
|
---|
238 | return setError(E_FAIL, pszNAckMsg);
|
---|
239 | }
|
---|
240 | return setError(E_FAIL, "NACK(%s) - %Rrc (%d)", pszWhich, vrc2, vrc2);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | return setError(E_FAIL, tr("%s: Expected ACK or NACK, got '%s'"), pszWhich, szMsg);
|
---|
244 | }
|
---|
245 | return S_OK;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
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 | *
|
---|
254 | * @param pState The teleporter source state.
|
---|
255 | * @param pszCommand The command.
|
---|
256 | * @param fWaitForAck Whether to wait for the ACK.
|
---|
257 | *
|
---|
258 | * @remarks the setError laziness forces this to be a Console member.
|
---|
259 | */
|
---|
260 | HRESULT
|
---|
261 | Console::teleporterSrcSubmitCommand(TeleporterStateSrc *pState, const char *pszCommand, bool fWaitForAck /*= true*/)
|
---|
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);
|
---|
267 | if (RT_SUCCESS(vrc))
|
---|
268 | vrc = RTTcpFlush(pState->mhSocket);
|
---|
269 | if (RT_FAILURE(vrc))
|
---|
270 | return setError(E_FAIL, tr("Failed writing command '%s': %Rrc"), pszCommand, vrc);
|
---|
271 | if (!fWaitForAck)
|
---|
272 | return S_OK;
|
---|
273 | return teleporterSrcReadACK(pState, pszCommand);
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * @copydoc SSMSTRMOPS::pfnWrite
|
---|
279 | */
|
---|
280 | static DECLCALLBACK(int) teleporterTcpOpWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
|
---|
281 | {
|
---|
282 | TeleporterState *pState = (TeleporterState *)pvUser;
|
---|
283 |
|
---|
284 | AssertReturn(cbToWrite > 0, VINF_SUCCESS);
|
---|
285 | AssertReturn(cbToWrite < UINT32_MAX, VERR_OUT_OF_RANGE);
|
---|
286 | AssertReturn(pState->mfIsSource, VERR_INVALID_HANDLE);
|
---|
287 |
|
---|
288 | for (;;)
|
---|
289 | {
|
---|
290 | /* Write block header. */
|
---|
291 | TELEPORTERTCPHDR Hdr;
|
---|
292 | Hdr.u32Magic = TELEPORTERTCPHDR_MAGIC;
|
---|
293 | Hdr.cb = RT_MIN((uint32_t)cbToWrite, TELEPORTERTCPHDR_MAX_SIZE);
|
---|
294 | int rc = RTTcpWrite(pState->mhSocket, &Hdr, sizeof(Hdr));
|
---|
295 | if (RT_FAILURE(rc))
|
---|
296 | {
|
---|
297 | LogRel(("Teleporter/TCP: Header write error: %Rrc\n", rc));
|
---|
298 | return rc;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /* Write the data. */
|
---|
302 | rc = RTTcpWrite(pState->mhSocket, pvBuf, Hdr.cb);
|
---|
303 | if (RT_FAILURE(rc))
|
---|
304 | {
|
---|
305 | LogRel(("Teleporter/TCP: Data write error: %Rrc (cb=%#x)\n", rc, Hdr.cb));
|
---|
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;
|
---|
315 | }
|
---|
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 | *
|
---|
328 | * @param pState The teleporter state data.
|
---|
329 | */
|
---|
330 | static int teleporterTcpReadSelect(TeleporterState *pState)
|
---|
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;
|
---|
339 | LogRel(("Teleporter/TCP: Header select error: %Rrc\n", rc));
|
---|
340 | break;
|
---|
341 | }
|
---|
342 | if (pState->mfStopReading)
|
---|
343 | {
|
---|
344 | rc = VERR_EOF;
|
---|
345 | break;
|
---|
346 | }
|
---|
347 | } while (rc == VERR_TIMEOUT);
|
---|
348 | return rc;
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * @copydoc SSMSTRMOPS::pfnRead
|
---|
354 | */
|
---|
355 | static DECLCALLBACK(int) teleporterTcpOpRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
356 | {
|
---|
357 | TeleporterState *pState = (TeleporterState *)pvUser;
|
---|
358 | AssertReturn(!pState->mfIsSource, VERR_INVALID_HANDLE);
|
---|
359 |
|
---|
360 | for (;;)
|
---|
361 | {
|
---|
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)
|
---|
379 | {
|
---|
380 | rc = teleporterTcpReadSelect(pState);
|
---|
381 | if (RT_FAILURE(rc))
|
---|
382 | return rc;
|
---|
383 | TELEPORTERTCPHDR Hdr;
|
---|
384 | rc = RTTcpRead(pState->mhSocket, &Hdr, sizeof(Hdr), NULL);
|
---|
385 | if (RT_FAILURE(rc))
|
---|
386 | {
|
---|
387 | pState->mfIOError = true;
|
---|
388 | LogRel(("Teleporter/TCP: Header read error: %Rrc\n", rc));
|
---|
389 | return rc;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (RT_UNLIKELY( Hdr.u32Magic != TELEPORTERTCPHDR_MAGIC
|
---|
393 | || Hdr.cb > TELEPORTERTCPHDR_MAX_SIZE
|
---|
394 | || Hdr.cb == 0))
|
---|
395 | {
|
---|
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 | }
|
---|
405 | pState->mfIOError = true;
|
---|
406 | LogRel(("Teleporter/TCP: Invalid block: u32Magic=%#x cb=%#x\n", Hdr.u32Magic, Hdr.cb));
|
---|
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 | */
|
---|
418 | rc = teleporterTcpReadSelect(pState);
|
---|
419 | if (RT_FAILURE(rc))
|
---|
420 | return rc;
|
---|
421 | uint32_t cb = (uint32_t)RT_MIN(pState->mcbReadBlock, cbToRead);
|
---|
422 | rc = RTTcpRead(pState->mhSocket, pvBuf, cb, pcbRead);
|
---|
423 | if (RT_FAILURE(rc))
|
---|
424 | {
|
---|
425 | pState->mfIOError = true;
|
---|
426 | LogRel(("Teleporter/TCP: Data read error: %Rrc (cb=%#x)\n", rc, cb));
|
---|
427 | return rc;
|
---|
428 | }
|
---|
429 | if (pcbRead)
|
---|
430 | {
|
---|
431 | cb = (uint32_t)*pcbRead;
|
---|
432 | pState->moffStream += cb;
|
---|
433 | pState->mcbReadBlock -= cb;
|
---|
434 | return VINF_SUCCESS;
|
---|
435 | }
|
---|
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;
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * @copydoc SSMSTRMOPS::pfnSeek
|
---|
450 | */
|
---|
451 | static DECLCALLBACK(int) teleporterTcpOpSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
|
---|
452 | {
|
---|
453 | return VERR_NOT_SUPPORTED;
|
---|
454 | }
|
---|
455 |
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * @copydoc SSMSTRMOPS::pfnTell
|
---|
459 | */
|
---|
460 | static DECLCALLBACK(uint64_t) teleporterTcpOpTell(void *pvUser)
|
---|
461 | {
|
---|
462 | TeleporterState *pState = (TeleporterState *)pvUser;
|
---|
463 | return pState->moffStream;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * @copydoc SSMSTRMOPS::pfnSize
|
---|
469 | */
|
---|
470 | static DECLCALLBACK(int) teleporterTcpOpSize(void *pvUser, uint64_t *pcb)
|
---|
471 | {
|
---|
472 | return VERR_NOT_SUPPORTED;
|
---|
473 | }
|
---|
474 |
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * @copydoc SSMSTRMOPS::pfnIsOk
|
---|
478 | */
|
---|
479 | static 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 | {
|
---|
491 | LogRel(("Teleporter/TCP: Incoming data detect by IsOk, assuming it is a cancellation NACK.\n"));
|
---|
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 | /**
|
---|
505 | * @copydoc SSMSTRMOPS::pfnClose
|
---|
506 | */
|
---|
507 | static DECLCALLBACK(int) teleporterTcpOpClose(void *pvUser, bool fCancelled)
|
---|
508 | {
|
---|
509 | TeleporterState *pState = (TeleporterState *)pvUser;
|
---|
510 |
|
---|
511 | if (pState->mfIsSource)
|
---|
512 | {
|
---|
513 | TELEPORTERTCPHDR EofHdr;
|
---|
514 | EofHdr.u32Magic = TELEPORTERTCPHDR_MAGIC;
|
---|
515 | EofHdr.cb = fCancelled ? UINT32_MAX : 0;
|
---|
516 | int rc = RTTcpWrite(pState->mhSocket, &EofHdr, sizeof(EofHdr));
|
---|
517 | if (RT_SUCCESS(rc))
|
---|
518 | rc = RTTcpFlush(pState->mhSocket);
|
---|
519 | if (RT_FAILURE(rc))
|
---|
520 | {
|
---|
521 | LogRel(("Teleporter/TCP: EOF Header write error: %Rrc\n", rc));
|
---|
522 | return rc;
|
---|
523 | }
|
---|
524 | }
|
---|
525 | else
|
---|
526 | {
|
---|
527 | ASMAtomicWriteBool(&pState->mfStopReading, true);
|
---|
528 | RTTcpFlush(pState->mhSocket);
|
---|
529 | }
|
---|
530 |
|
---|
531 | return VINF_SUCCESS;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Method table for a TCP based stream.
|
---|
537 | */
|
---|
538 | static SSMSTRMOPS const g_teleporterTcpOps =
|
---|
539 | {
|
---|
540 | SSMSTRMOPS_VERSION,
|
---|
541 | teleporterTcpOpWrite,
|
---|
542 | teleporterTcpOpRead,
|
---|
543 | teleporterTcpOpSeek,
|
---|
544 | teleporterTcpOpTell,
|
---|
545 | teleporterTcpOpSize,
|
---|
546 | teleporterTcpOpIsOk,
|
---|
547 | teleporterTcpOpClose,
|
---|
548 | SSMSTRMOPS_VERSION
|
---|
549 | };
|
---|
550 |
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * Progress cancelation callback.
|
---|
554 | */
|
---|
555 | static 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 | */
|
---|
569 | static 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. */
|
---|
578 | BOOL fCancelled;
|
---|
579 | hrc = pState->mptrProgress->COMGETTER(Canceled)(&fCancelled);
|
---|
580 | if (SUCCEEDED(hrc) && fCancelled)
|
---|
581 | {
|
---|
582 | SSMR3Cancel(pState->mpVM);
|
---|
583 | return VERR_SSM_CANCELLED;
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | return VINF_SUCCESS;
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * @copydoc FNRTTIMERLR
|
---|
594 | */
|
---|
595 | static DECLCALLBACK(void) teleporterDstTimeout(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick)
|
---|
596 | {
|
---|
597 | /* This is harmless for any open connections. */
|
---|
598 | RTTcpServerShutdown((PRTTCPSERVER)pvUser);
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Do the teleporter.
|
---|
604 | *
|
---|
605 | * @returns VBox status code.
|
---|
606 | * @param pState The teleporter state.
|
---|
607 | */
|
---|
608 | HRESULT
|
---|
609 | Console::teleporterSrc(TeleporterStateSrc *pState)
|
---|
610 | {
|
---|
611 | AutoCaller autoCaller(this);
|
---|
612 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
613 |
|
---|
614 | /*
|
---|
615 | * Wait for Console::Teleport to change the state.
|
---|
616 | */
|
---|
617 | { AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS); }
|
---|
618 |
|
---|
619 | BOOL fCancelled = TRUE;
|
---|
620 | HRESULT hrc = pState->mptrProgress->COMGETTER(Canceled)(&fCancelled);
|
---|
621 | if (FAILED(hrc))
|
---|
622 | return hrc;
|
---|
623 | if (fCancelled)
|
---|
624 | return setError(E_FAIL, tr("cancelled"));
|
---|
625 |
|
---|
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);
|
---|
631 | if (RT_FAILURE(vrc))
|
---|
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))];
|
---|
637 | RT_ZERO(szLine);
|
---|
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);
|
---|
641 | if (strcmp(szLine, g_szWelcome))
|
---|
642 | return setError(E_FAIL, tr("Unexpected welcome %.*Rhxs"), sizeof(g_szWelcome) - 1, szLine);
|
---|
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 */
|
---|
651 | hrc = teleporterSrcReadACK(pState, "password", tr("Invalid password"));
|
---|
652 | if (FAILED(hrc))
|
---|
653 | return hrc;
|
---|
654 |
|
---|
655 | /*
|
---|
656 | * Start loading the state.
|
---|
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.
|
---|
661 | */
|
---|
662 | hrc = teleporterSrcSubmitCommand(pState, "load");
|
---|
663 | if (FAILED(hrc))
|
---|
664 | return hrc;
|
---|
665 |
|
---|
666 | void *pvUser = static_cast<void *>(static_cast<TeleporterState *>(pState));
|
---|
667 | vrc = VMR3Teleport(pState->mpVM, pState->mcMsMaxDowntime,
|
---|
668 | &g_teleporterTcpOps, pvUser,
|
---|
669 | teleporterProgressCallback, pvUser,
|
---|
670 | &pState->mfSuspendedByUs);
|
---|
671 | if (RT_FAILURE(vrc))
|
---|
672 | return setError(E_FAIL, tr("VMR3Teleport -> %Rrc"), vrc);
|
---|
673 |
|
---|
674 | hrc = teleporterSrcReadACK(pState, "load-complete");
|
---|
675 | if (FAILED(hrc))
|
---|
676 | return hrc;
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * We're at the point of no return.
|
---|
680 | */
|
---|
681 | if (!pState->mptrProgress->notifyPointOfNoReturn())
|
---|
682 | {
|
---|
683 | teleporterSrcSubmitCommand(pState, "cancel", false /*fWaitForAck*/);
|
---|
684 | return E_FAIL;
|
---|
685 | }
|
---|
686 |
|
---|
687 | /*
|
---|
688 | * Hand over any media which we might be sharing.
|
---|
689 | *
|
---|
690 | * Note! This is only important on localhost teleportations.
|
---|
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 |
|
---|
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");
|
---|
710 | if (FAILED(hrc))
|
---|
711 | return hrc;
|
---|
712 |
|
---|
713 | /*
|
---|
714 | * teleporterSrcThreadWrapper will do the automatic power off because it
|
---|
715 | * has to release the AutoVMCaller.
|
---|
716 | */
|
---|
717 | return S_OK;
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * Static thread method wrapper.
|
---|
723 | *
|
---|
724 | * @returns VINF_SUCCESS (ignored).
|
---|
725 | * @param hThread The thread.
|
---|
726 | * @param pvUser Pointer to a TeleporterStateSrc instance.
|
---|
727 | */
|
---|
728 | /*static*/ DECLCALLBACK(int)
|
---|
729 | Console::teleporterSrcThreadWrapper(RTTHREAD hThread, void *pvUser)
|
---|
730 | {
|
---|
731 | TeleporterStateSrc *pState = (TeleporterStateSrc *)pvUser;
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * Console::teleporterSrc does the work, we just grab onto the VM handle
|
---|
735 | * and do the cleanups afterwards.
|
---|
736 | */
|
---|
737 | AutoVMCaller autoVMCaller(pState->mptrConsole);
|
---|
738 | HRESULT hrc = autoVMCaller.rc();
|
---|
739 |
|
---|
740 | if (SUCCEEDED(hrc))
|
---|
741 | hrc = pState->mptrConsole->teleporterSrc(pState);
|
---|
742 |
|
---|
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 |
|
---|
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 |
|
---|
755 | /* We can no longer be cancelled (success), or it doesn't matter any longer (failure). */
|
---|
756 | pState->mptrProgress->setCancelCallback(NULL, NULL);
|
---|
757 |
|
---|
758 | /*
|
---|
759 | * Write lock the console before resetting mptrCancelableProgress and
|
---|
760 | * fixing the state.
|
---|
761 | */
|
---|
762 | AutoWriteLock autoLock(pState->mptrConsole COMMA_LOCKVAL_SRC_POS);
|
---|
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))
|
---|
768 | {
|
---|
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();
|
---|
779 |
|
---|
780 | pState->mptrConsole->mVMIsAlreadyPoweringOff = true; /* (Make sure we stick in the TeleportingPausedVM state.) */
|
---|
781 | hrc = pState->mptrConsole->powerDown();
|
---|
782 | pState->mptrConsole->mVMIsAlreadyPoweringOff = false;
|
---|
783 |
|
---|
784 | pState->mptrProgress->notifyComplete(hrc);
|
---|
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 | )
|
---|
798 | {
|
---|
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 |
|
---|
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:
|
---|
829 | Assert(!pState->mfSuspendedByUs);
|
---|
830 | Assert(!pState->mfUnlockedMedia);
|
---|
831 | pState->mptrConsole->setMachineState(MachineState_Running);
|
---|
832 | break;
|
---|
833 |
|
---|
834 | case VMSTATE_GURU_MEDITATION:
|
---|
835 | case VMSTATE_GURU_MEDITATION_LS:
|
---|
836 | pState->mptrConsole->setMachineState(MachineState_Stuck);
|
---|
837 | break;
|
---|
838 |
|
---|
839 | case VMSTATE_FATAL_ERROR:
|
---|
840 | case VMSTATE_FATAL_ERROR_LS:
|
---|
841 | pState->mptrConsole->setMachineState(MachineState_Paused);
|
---|
842 | break;
|
---|
843 |
|
---|
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:
|
---|
851 | if (!pState->mfUnlockedMedia)
|
---|
852 | {
|
---|
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 | }
|
---|
861 | }
|
---|
862 | else
|
---|
863 | {
|
---|
864 | /* Faking a guru meditation is the best I can think of doing here... */
|
---|
865 | pState->mptrConsole->setMachineState(MachineState_Stuck);
|
---|
866 | }
|
---|
867 | break;
|
---|
868 | }
|
---|
869 | }
|
---|
870 | }
|
---|
871 | autoLock.leave();
|
---|
872 |
|
---|
873 | /*
|
---|
874 | * Cleanup.
|
---|
875 | */
|
---|
876 | Assert(pState->mhSocket == NIL_RTSOCKET);
|
---|
877 | delete pState;
|
---|
878 |
|
---|
879 | return VINF_SUCCESS; /* ignored */
|
---|
880 | }
|
---|
881 |
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Start teleporter to the specified target.
|
---|
885 | *
|
---|
886 | * @returns COM status code.
|
---|
887 | *
|
---|
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.
|
---|
893 | */
|
---|
894 | STDMETHODIMP
|
---|
895 | Console::Teleport(IN_BSTR aHostname, ULONG aPort, IN_BSTR aPassword, ULONG aMaxDowntime, IProgress **aProgress)
|
---|
896 | {
|
---|
897 | /*
|
---|
898 | * Validate parameters, check+hold object status, write lock the object
|
---|
899 | * and validate the state.
|
---|
900 | */
|
---|
901 | CheckComArgOutPointerValid(aProgress);
|
---|
902 | CheckComArgStrNotEmptyOrNull(aHostname);
|
---|
903 | CheckComArgStrNotEmptyOrNull(aHostname);
|
---|
904 | CheckComArgExprMsg(aPort, aPort > 0 && aPort <= 65535, ("is %u", aPort));
|
---|
905 | CheckComArgExprMsg(aMaxDowntime, aMaxDowntime > 0, ("is %u", aMaxDowntime));
|
---|
906 |
|
---|
907 | AutoCaller autoCaller(this);
|
---|
908 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
909 |
|
---|
910 | AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
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,
|
---|
921 | tr("Invalid machine state: %s (must be Running or Paused)"),
|
---|
922 | Global::stringifyMachineState(mMachineState));
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | /*
|
---|
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.
|
---|
929 | */
|
---|
930 | LogFlowThisFunc(("Initiating TELEPORT request...\n"));
|
---|
931 |
|
---|
932 | ComObjPtr<Progress> ptrProgress;
|
---|
933 | HRESULT hrc = ptrProgress.createObject();
|
---|
934 | if (FAILED(hrc)) return hrc;
|
---|
935 | hrc = ptrProgress->init(static_cast<IConsole *>(this), Bstr(tr("Teleporter")), TRUE /*aCancelable*/);
|
---|
936 | if (FAILED(hrc)) return hrc;
|
---|
937 |
|
---|
938 | TeleporterStateSrc *pState = new TeleporterStateSrc(this, mpVM, ptrProgress, mMachineState);
|
---|
939 | pState->mstrPassword = aPassword;
|
---|
940 | pState->mstrHostname = aHostname;
|
---|
941 | pState->muPort = aPort;
|
---|
942 | pState->mcMsMaxDowntime = aMaxDowntime;
|
---|
943 |
|
---|
944 | void *pvUser = static_cast<void *>(static_cast<TeleporterState *>(pState));
|
---|
945 | ptrProgress->setCancelCallback(teleporterProgressCancelCallback, pvUser);
|
---|
946 |
|
---|
947 | int vrc = RTThreadCreate(NULL, Console::teleporterSrcThreadWrapper, (void *)pState, 0 /*cbStack*/,
|
---|
948 | RTTHREADTYPE_EMULATION, 0 /*fFlags*/, "Teleport");
|
---|
949 | if (RT_SUCCESS(vrc))
|
---|
950 | {
|
---|
951 | if (mMachineState == MachineState_Running)
|
---|
952 | hrc = setMachineState(MachineState_Teleporting);
|
---|
953 | else
|
---|
954 | hrc = setMachineState(MachineState_TeleportingPausedVM);
|
---|
955 | if (SUCCEEDED(hrc))
|
---|
956 | {
|
---|
957 | ptrProgress.queryInterfaceTo(aProgress);
|
---|
958 | mptrCancelableProgress = ptrProgress;
|
---|
959 | }
|
---|
960 | else
|
---|
961 | ptrProgress->Cancel();
|
---|
962 | }
|
---|
963 | else
|
---|
964 | {
|
---|
965 | ptrProgress->setCancelCallback(NULL, NULL);
|
---|
966 | delete pState;
|
---|
967 | hrc = setError(E_FAIL, tr("RTThreadCreate -> %Rrc"), vrc);
|
---|
968 | }
|
---|
969 |
|
---|
970 | return hrc;
|
---|
971 | }
|
---|
972 |
|
---|
973 |
|
---|
974 | /**
|
---|
975 | * Creates a TCP server that listens for the source machine and passes control
|
---|
976 | * over to Console::teleporterTrgServeConnection().
|
---|
977 | *
|
---|
978 | * @returns VBox status code.
|
---|
979 | * @param pVM The VM handle
|
---|
980 | * @param pMachine The IMachine for the virtual machine.
|
---|
981 | * @param fStartPaused Whether to start it in the Paused (true) or
|
---|
982 | * Running (false) state,
|
---|
983 | * @param pProgress Pointer to the progress object.
|
---|
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...
|
---|
987 | */
|
---|
988 | int
|
---|
989 | Console::teleporterTrg(PVM pVM, IMachine *pMachine, bool fStartPaused, Progress *pProgress)
|
---|
990 | {
|
---|
991 | /*
|
---|
992 | * Get the config.
|
---|
993 | */
|
---|
994 | ULONG uPort;
|
---|
995 | HRESULT hrc = pMachine->COMGETTER(TeleporterPort)(&uPort);
|
---|
996 | if (FAILED(hrc))
|
---|
997 | return VERR_GENERAL_FAILURE;
|
---|
998 | ULONG const uPortOrg = uPort;
|
---|
999 |
|
---|
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 |
|
---|
1007 | Bstr bstrPassword;
|
---|
1008 | hrc = pMachine->COMGETTER(TeleporterPassword)(bstrPassword.asOutParam());
|
---|
1009 | if (FAILED(hrc))
|
---|
1010 | return VERR_GENERAL_FAILURE;
|
---|
1011 | Utf8Str strPassword(bstrPassword);
|
---|
1012 | strPassword.append('\n'); /* To simplify password checking. */
|
---|
1013 |
|
---|
1014 | /*
|
---|
1015 | * Create the TCP server.
|
---|
1016 | */
|
---|
1017 | int vrc;
|
---|
1018 | PRTTCPSERVER hServer;
|
---|
1019 | if (uPort)
|
---|
1020 | vrc = RTTcpServerCreateEx(pszAddress, uPort, &hServer);
|
---|
1021 | else
|
---|
1022 | {
|
---|
1023 | for (int cTries = 10240; cTries > 0; cTries--)
|
---|
1024 | {
|
---|
1025 | uPort = RTRandU32Ex(cTries >= 8192 ? 49152 : 1024, 65534);
|
---|
1026 | vrc = RTTcpServerCreateEx(pszAddress, uPort, &hServer);
|
---|
1027 | if (vrc != VERR_NET_ADDRESS_IN_USE)
|
---|
1028 | break;
|
---|
1029 | }
|
---|
1030 | if (RT_SUCCESS(vrc))
|
---|
1031 | {
|
---|
1032 | hrc = pMachine->COMSETTER(TeleporterPort)(uPort);
|
---|
1033 | if (FAILED(hrc))
|
---|
1034 | {
|
---|
1035 | RTTcpServerDestroy(hServer);
|
---|
1036 | return VERR_GENERAL_FAILURE;
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 | }
|
---|
1040 | if (RT_FAILURE(vrc))
|
---|
1041 | return vrc;
|
---|
1042 |
|
---|
1043 | /*
|
---|
1044 | * Create a one-shot timer for timing out after 5 mins.
|
---|
1045 | */
|
---|
1046 | RTTIMERLR hTimerLR;
|
---|
1047 | vrc = RTTimerLRCreateEx(&hTimerLR, 0 /*ns*/, RTTIMER_FLAGS_CPU_ANY, teleporterDstTimeout, hServer);
|
---|
1048 | if (RT_SUCCESS(vrc))
|
---|
1049 | {
|
---|
1050 | vrc = RTTimerLRStart(hTimerLR, 5*60*UINT64_C(1000000000) /*ns*/);
|
---|
1051 | if (RT_SUCCESS(vrc))
|
---|
1052 | {
|
---|
1053 | /*
|
---|
1054 | * Do the job, when it returns we're done.
|
---|
1055 | */
|
---|
1056 | TeleporterStateTrg theState(this, pVM, pProgress, pMachine, mControl, &hTimerLR, fStartPaused);
|
---|
1057 | theState.mstrPassword = strPassword;
|
---|
1058 | theState.mhServer = hServer;
|
---|
1059 |
|
---|
1060 | void *pvUser = static_cast<void *>(static_cast<TeleporterState *>(&theState));
|
---|
1061 | if (pProgress->setCancelCallback(teleporterProgressCancelCallback, pvUser))
|
---|
1062 | {
|
---|
1063 | LogRel(("Teleporter: Waiting for incoming VM...\n"));
|
---|
1064 | vrc = RTTcpServerListen(hServer, Console::teleporterTrgServeConnection, &theState);
|
---|
1065 | pProgress->setCancelCallback(NULL, NULL);
|
---|
1066 |
|
---|
1067 | bool fPowerOff = false;
|
---|
1068 | if (vrc == VERR_TCP_SERVER_STOP)
|
---|
1069 | {
|
---|
1070 | vrc = theState.mRc;
|
---|
1071 | /* Power off the VM on failure unless the state callback
|
---|
1072 | already did that. */
|
---|
1073 | if (RT_FAILURE(vrc))
|
---|
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 | }
|
---|
1098 | else
|
---|
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 | }
|
---|
1110 | }
|
---|
1111 | else
|
---|
1112 | vrc = VERR_SSM_CANCELLED;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | RTTimerLRDestroy(hTimerLR);
|
---|
1116 | }
|
---|
1117 | RTTcpServerDestroy(hServer);
|
---|
1118 |
|
---|
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 |
|
---|
1126 | return vrc;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 |
|
---|
1130 | /**
|
---|
1131 | * Unlock the media.
|
---|
1132 | *
|
---|
1133 | * This is used in error paths.
|
---|
1134 | *
|
---|
1135 | * @param pState The teleporter state.
|
---|
1136 | */
|
---|
1137 | static void teleporterTrgUnlockMedia(TeleporterStateTrg *pState)
|
---|
1138 | {
|
---|
1139 | if (pState->mfLockedMedia)
|
---|
1140 | {
|
---|
1141 | pState->mpControl->UnlockMedia();
|
---|
1142 | pState->mfLockedMedia = false;
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 |
|
---|
1147 | static int teleporterTcpWriteACK(TeleporterStateTrg *pState, bool fAutomaticUnlock = true)
|
---|
1148 | {
|
---|
1149 | int rc = RTTcpWrite(pState->mhSocket, "ACK\n", sizeof("ACK\n") - 1);
|
---|
1150 | if (RT_FAILURE(rc))
|
---|
1151 | {
|
---|
1152 | LogRel(("Teleporter: RTTcpWrite(,ACK,) -> %Rrc\n", rc));
|
---|
1153 | if (fAutomaticUnlock)
|
---|
1154 | teleporterTrgUnlockMedia(pState);
|
---|
1155 | }
|
---|
1156 | RTTcpFlush(pState->mhSocket);
|
---|
1157 | return rc;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 |
|
---|
1161 | static int teleporterTcpWriteNACK(TeleporterStateTrg *pState, int32_t rc2)
|
---|
1162 | {
|
---|
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 |
|
---|
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))
|
---|
1173 | LogRel(("Teleporter: RTTcpWrite(,%s,%zu) -> %Rrc\n", szMsg, cch, rc));
|
---|
1174 | RTTcpFlush(pState->mhSocket);
|
---|
1175 | return rc;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 |
|
---|
1179 | /**
|
---|
1180 | * @copydoc FNRTTCPSERVE
|
---|
1181 | *
|
---|
1182 | * @returns VINF_SUCCESS or VERR_TCP_SERVER_STOP.
|
---|
1183 | */
|
---|
1184 | /*static*/ DECLCALLBACK(int)
|
---|
1185 | Console::teleporterTrgServeConnection(RTSOCKET Sock, void *pvUser)
|
---|
1186 | {
|
---|
1187 | TeleporterStateTrg *pState = (TeleporterStateTrg *)pvUser;
|
---|
1188 | pState->mhSocket = Sock;
|
---|
1189 |
|
---|
1190 | /*
|
---|
1191 | * Say hello.
|
---|
1192 | */
|
---|
1193 | int vrc = RTTcpWrite(Sock, g_szWelcome, sizeof(g_szWelcome) - 1);
|
---|
1194 | if (RT_FAILURE(vrc))
|
---|
1195 | {
|
---|
1196 | LogRel(("Teleporter: Failed to write welcome message: %Rrc\n", vrc));
|
---|
1197 | return VINF_SUCCESS;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | /*
|
---|
1201 | * Password (includes '\n', see teleporterTrg).
|
---|
1202 | */
|
---|
1203 | const char *pszPassword = pState->mstrPassword.c_str();
|
---|
1204 | unsigned off = 0;
|
---|
1205 | while (pszPassword[off])
|
---|
1206 | {
|
---|
1207 | char ch;
|
---|
1208 | vrc = RTTcpRead(Sock, &ch, sizeof(ch), NULL);
|
---|
1209 | if ( RT_FAILURE(vrc)
|
---|
1210 | || pszPassword[off] != ch)
|
---|
1211 | {
|
---|
1212 | if (RT_FAILURE(vrc))
|
---|
1213 | LogRel(("Teleporter: Password read failure (off=%u): %Rrc\n", off, vrc));
|
---|
1214 | else
|
---|
1215 | LogRel(("Teleporter: Invalid password (off=%u)\n", off));
|
---|
1216 | teleporterTcpWriteNACK(pState, VERR_AUTHENTICATION_FAILURE);
|
---|
1217 | return VINF_SUCCESS;
|
---|
1218 | }
|
---|
1219 | off++;
|
---|
1220 | }
|
---|
1221 | vrc = teleporterTcpWriteACK(pState);
|
---|
1222 | if (RT_FAILURE(vrc))
|
---|
1223 | return VINF_SUCCESS;
|
---|
1224 | LogRel(("Teleporter: Incoming VM!\n"));
|
---|
1225 |
|
---|
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 | */
|
---|
1232 | RTTcpServerShutdown(pState->mhServer);
|
---|
1233 | RTTimerLRDestroy(*pState->mphTimerLR);
|
---|
1234 | *pState->mphTimerLR = NIL_RTTIMERLR;
|
---|
1235 |
|
---|
1236 | /*
|
---|
1237 | * Command processing loop.
|
---|
1238 | */
|
---|
1239 | bool fDone = false;
|
---|
1240 | for (;;)
|
---|
1241 | {
|
---|
1242 | char szCmd[128];
|
---|
1243 | vrc = teleporterTcpReadLine(pState, szCmd, sizeof(szCmd));
|
---|
1244 | if (RT_FAILURE(vrc))
|
---|
1245 | break;
|
---|
1246 |
|
---|
1247 | if (!strcmp(szCmd, "load"))
|
---|
1248 | {
|
---|
1249 | vrc = teleporterTcpWriteACK(pState);
|
---|
1250 | if (RT_FAILURE(vrc))
|
---|
1251 | break;
|
---|
1252 |
|
---|
1253 | RTSocketRetain(pState->mhSocket); /* For concurrent access by I/O thread and EMT. */
|
---|
1254 | pState->moffStream = 0;
|
---|
1255 | void *pvUser2 = static_cast<void *>(static_cast<TeleporterState *>(pState));
|
---|
1256 | vrc = VMR3LoadFromStream(pState->mpVM, &g_teleporterTcpOps, pvUser2,
|
---|
1257 | teleporterProgressCallback, pvUser2);
|
---|
1258 | RTSocketRelease(pState->mhSocket);
|
---|
1259 | if (RT_FAILURE(vrc))
|
---|
1260 | {
|
---|
1261 | LogRel(("Teleporter: VMR3LoadFromStream -> %Rrc\n", vrc));
|
---|
1262 | teleporterTcpWriteNACK(pState, vrc);
|
---|
1263 | break;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | /* The EOS might not have been read, make sure it is. */
|
---|
1267 | pState->mfStopReading = false;
|
---|
1268 | size_t cbRead;
|
---|
1269 | vrc = teleporterTcpOpRead(pvUser2, pState->moffStream, szCmd, 1, &cbRead);
|
---|
1270 | if (vrc != VERR_EOF)
|
---|
1271 | {
|
---|
1272 | LogRel(("Teleporter: Draining teleporterTcpOpRead -> %Rrc\n", vrc));
|
---|
1273 | teleporterTcpWriteNACK(pState, vrc);
|
---|
1274 | break;
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | vrc = teleporterTcpWriteACK(pState);
|
---|
1278 | }
|
---|
1279 | else if (!strcmp(szCmd, "cancel"))
|
---|
1280 | {
|
---|
1281 | /* Don't ACK this. */
|
---|
1282 | LogRel(("Teleporter: Received cancel command.\n"));
|
---|
1283 | vrc = VERR_SSM_CANCELLED;
|
---|
1284 | }
|
---|
1285 | else if (!strcmp(szCmd, "lock-media"))
|
---|
1286 | {
|
---|
1287 | HRESULT hrc = pState->mpControl->LockMedia();
|
---|
1288 | if (SUCCEEDED(hrc))
|
---|
1289 | {
|
---|
1290 | pState->mfLockedMedia = true;
|
---|
1291 | vrc = teleporterTcpWriteACK(pState);
|
---|
1292 | }
|
---|
1293 | else
|
---|
1294 | {
|
---|
1295 | vrc = VERR_FILE_LOCK_FAILED;
|
---|
1296 | teleporterTcpWriteNACK(pState, vrc);
|
---|
1297 | }
|
---|
1298 | }
|
---|
1299 | else if ( !strcmp(szCmd, "hand-over-resume")
|
---|
1300 | || !strcmp(szCmd, "hand-over-paused"))
|
---|
1301 | {
|
---|
1302 | /*
|
---|
1303 | * Point of no return.
|
---|
1304 | *
|
---|
1305 | * Note! Since we cannot tell whether a VMR3Resume failure is
|
---|
1306 | * destructive for the source or not, we have little choice
|
---|
1307 | * but to ACK it first and take any failures locally.
|
---|
1308 | *
|
---|
1309 | * Ideally, we should try resume it first and then ACK (or
|
---|
1310 | * NACK) the request since this would reduce latency and
|
---|
1311 | * make it possible to recover from some VMR3Resume failures.
|
---|
1312 | */
|
---|
1313 | if ( pState->mptrProgress->notifyPointOfNoReturn()
|
---|
1314 | && pState->mfLockedMedia)
|
---|
1315 | {
|
---|
1316 | vrc = teleporterTcpWriteACK(pState);
|
---|
1317 | if (RT_SUCCESS(vrc))
|
---|
1318 | {
|
---|
1319 | if (!strcmp(szCmd, "hand-over-resume"))
|
---|
1320 | vrc = VMR3Resume(pState->mpVM);
|
---|
1321 | else
|
---|
1322 | pState->mptrConsole->setMachineState(MachineState_Paused);
|
---|
1323 | fDone = true;
|
---|
1324 | break;
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | else
|
---|
1328 | {
|
---|
1329 | vrc = pState->mfLockedMedia ? VERR_WRONG_ORDER : VERR_SSM_CANCELLED;
|
---|
1330 | teleporterTcpWriteNACK(pState, vrc);
|
---|
1331 | }
|
---|
1332 | }
|
---|
1333 | else
|
---|
1334 | {
|
---|
1335 | LogRel(("Teleporter: Unknown command '%s' (%.*Rhxs)\n", szCmd, strlen(szCmd), szCmd));
|
---|
1336 | vrc = VERR_NOT_IMPLEMENTED;
|
---|
1337 | teleporterTcpWriteNACK(pState, vrc);
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | if (RT_FAILURE(vrc))
|
---|
1341 | break;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | if (RT_SUCCESS(vrc) && !fDone)
|
---|
1345 | vrc = VERR_WRONG_ORDER;
|
---|
1346 | if (RT_FAILURE(vrc))
|
---|
1347 | teleporterTrgUnlockMedia(pState);
|
---|
1348 |
|
---|
1349 | pState->mRc = vrc;
|
---|
1350 | pState->mhSocket = NIL_RTSOCKET;
|
---|
1351 | LogFlowFunc(("returns mRc=%Rrc\n", vrc));
|
---|
1352 | return VERR_TCP_SERVER_STOP;
|
---|
1353 | }
|
---|
1354 |
|
---|