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