VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/FTM.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.8 KB
RevLine 
[31754]1/* $Id: FTM.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
[31753]2/** @file
3 * FTM - Fault Tolerance Manager
4 */
5
6/*
[76553]7 * Copyright (C) 2010-2019 Oracle Corporation
[31753]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
[57358]19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
[31753]22#define LOG_GROUP LOG_GROUP_FTM
[44340]23#include <VBox/vmm/ftm.h>
24#include <VBox/vmm/em.h>
25#include <VBox/vmm/pdm.h>
26#include <VBox/vmm/pgm.h>
27#include <VBox/vmm/ssm.h>
28#include <VBox/vmm/vmm.h>
[31753]29#include "FTMInternal.h"
[35346]30#include <VBox/vmm/vm.h>
[44374]31#include <VBox/vmm/uvm.h>
[31753]32#include <VBox/err.h>
33#include <VBox/param.h>
34#include <VBox/log.h>
35
36#include <iprt/assert.h>
37#include <iprt/thread.h>
38#include <iprt/string.h>
39#include <iprt/mem.h>
40#include <iprt/tcp.h>
[31953]41#include <iprt/socket.h>
[31789]42#include <iprt/semaphore.h>
[31809]43#include <iprt/asm.h>
[31753]44
[32169]45
[31770]46/*******************************************************************************
[57372]47 * Structures and Typedefs *
[31809]48 *******************************************************************************/
49
50/**
51 * TCP stream header.
52 *
53 * This is an extra layer for fixing the problem with figuring out when the SSM
54 * stream ends.
55 */
56typedef struct FTMTCPHDR
57{
58 /** Magic value. */
59 uint32_t u32Magic;
60 /** The size of the data block following this header.
61 * 0 indicates the end of the stream, while UINT32_MAX indicates
62 * cancelation. */
63 uint32_t cb;
64} FTMTCPHDR;
[32001]65/** Magic value for FTMTCPHDR::u32Magic. (Egberto Gismonti Amin) */
[31809]66#define FTMTCPHDR_MAGIC UINT32_C(0x19471205)
67/** The max block size. */
68#define FTMTCPHDR_MAX_SIZE UINT32_C(0x00fffff8)
69
[32001]70/**
71 * TCP stream header.
72 *
73 * This is an extra layer for fixing the problem with figuring out when the SSM
74 * stream ends.
75 */
76typedef struct FTMTCPHDRMEM
77{
78 /** Magic value. */
79 uint32_t u32Magic;
80 /** Size (Uncompressed) of the pages following the header. */
81 uint32_t cbPageRange;
82 /** GC Physical address of the page(s) to sync. */
83 RTGCPHYS GCPhys;
84 /** The size of the data block following this header.
85 * 0 indicates the end of the stream, while UINT32_MAX indicates
86 * cancelation. */
87 uint32_t cb;
88} FTMTCPHDRMEM;
89
[57358]90
91/*********************************************************************************************************************************
92* Global Variables *
93*********************************************************************************************************************************/
[31770]94static const char g_szWelcome[] = "VirtualBox-Fault-Tolerance-Sync-1.0\n";
95
[32285]96static DECLCALLBACK(int) ftmR3PageTreeDestroyCallback(PAVLGCPHYSNODECORE pBaseNode, void *pvUser);
[32268]97
[31753]98/**
99 * Initializes the FTM.
100 *
101 * @returns VBox status code.
[58122]102 * @param pVM The cross context VM structure.
[31753]103 */
[44374]104VMMR3_INT_DECL(int) FTMR3Init(PVM pVM)
[31753]105{
[31782]106 /*
107 * Assert alignment and sizes.
108 */
109 AssertCompile(sizeof(pVM->ftm.s) <= sizeof(pVM->ftm.padding));
110 AssertCompileMemberAlignment(FTM, CritSect, sizeof(uintptr_t));
111
[31753]112 /** @todo saved state for master nodes! */
[31770]113 pVM->ftm.s.pszAddress = NULL;
114 pVM->ftm.s.pszPassword = NULL;
115 pVM->fFaultTolerantMaster = false;
116 pVM->ftm.s.fIsStandbyNode = false;
[31789]117 pVM->ftm.s.standby.hServer = NIL_RTTCPSERVER;
[32294]118 pVM->ftm.s.hShutdownEvent = NIL_RTSEMEVENT;
[31770]119 pVM->ftm.s.hSocket = NIL_RTSOCKET;
[31782]120
121 /*
122 * Initialize the PGM critical section.
123 */
124 int rc = PDMR3CritSectInit(pVM, &pVM->ftm.s.CritSect, RT_SRC_POS, "FTM");
125 AssertRCReturn(rc, rc);
126
[32102]127 /*
128 * Register statistics.
129 */
130 STAM_REL_REG(pVM, &pVM->ftm.s.StatReceivedMem, STAMTYPE_COUNTER, "/FT/Received/Mem", STAMUNIT_BYTES, "The amount of memory pages that was received.");
131 STAM_REL_REG(pVM, &pVM->ftm.s.StatReceivedState, STAMTYPE_COUNTER, "/FT/Received/State", STAMUNIT_BYTES, "The amount of state information that was received.");
132 STAM_REL_REG(pVM, &pVM->ftm.s.StatSentMem, STAMTYPE_COUNTER, "/FT/Sent/Mem", STAMUNIT_BYTES, "The amount of memory pages that was sent.");
133 STAM_REL_REG(pVM, &pVM->ftm.s.StatSentState, STAMTYPE_COUNTER, "/FT/Sent/State", STAMUNIT_BYTES, "The amount of state information that was sent.");
134 STAM_REL_REG(pVM, &pVM->ftm.s.StatDeltaVM, STAMTYPE_COUNTER, "/FT/Sync/DeltaVM", STAMUNIT_OCCURENCES, "Number of delta vm syncs.");
135 STAM_REL_REG(pVM, &pVM->ftm.s.StatFullSync, STAMTYPE_COUNTER, "/FT/Sync/Full", STAMUNIT_OCCURENCES, "Number of full vm syncs.");
136 STAM_REL_REG(pVM, &pVM->ftm.s.StatDeltaMem, STAMTYPE_COUNTER, "/FT/Sync/DeltaMem", STAMUNIT_OCCURENCES, "Number of delta mem syncs.");
[32144]137 STAM_REL_REG(pVM, &pVM->ftm.s.StatCheckpointStorage, STAMTYPE_COUNTER, "/FT/Checkpoint/Storage", STAMUNIT_OCCURENCES, "Number of storage checkpoints.");
[32431]138 STAM_REL_REG(pVM, &pVM->ftm.s.StatCheckpointNetwork, STAMTYPE_COUNTER, "/FT/Checkpoint/Network", STAMUNIT_OCCURENCES, "Number of network checkpoints.");
[32179]139#ifdef VBOX_WITH_STATISTICS
140 STAM_REG(pVM, &pVM->ftm.s.StatCheckpoint, STAMTYPE_PROFILE, "/FT/Checkpoint", STAMUNIT_TICKS_PER_CALL, "Profiling of FTMR3SetCheckpoint.");
[32308]141 STAM_REG(pVM, &pVM->ftm.s.StatCheckpointPause, STAMTYPE_PROFILE, "/FT/Checkpoint/Pause", STAMUNIT_TICKS_PER_CALL, "Profiling of FTMR3SetCheckpoint.");
142 STAM_REG(pVM, &pVM->ftm.s.StatCheckpointResume, STAMTYPE_PROFILE, "/FT/Checkpoint/Resume", STAMUNIT_TICKS_PER_CALL, "Profiling of FTMR3SetCheckpoint.");
[32302]143 STAM_REG(pVM, &pVM->ftm.s.StatSentMemRAM, STAMTYPE_COUNTER, "/FT/Sent/Mem/RAM", STAMUNIT_BYTES, "The amount of memory pages that was sent.");
144 STAM_REG(pVM, &pVM->ftm.s.StatSentMemMMIO2, STAMTYPE_COUNTER, "/FT/Sent/Mem/MMIO2", STAMUNIT_BYTES, "The amount of memory pages that was sent.");
145 STAM_REG(pVM, &pVM->ftm.s.StatSentMemShwROM, STAMTYPE_COUNTER, "/FT/Sent/Mem/ShwROM", STAMUNIT_BYTES, "The amount of memory pages that was sent.");
[32319]146 STAM_REG(pVM, &pVM->ftm.s.StatSentStateWrite, STAMTYPE_COUNTER, "/FT/Sent/State/Writes", STAMUNIT_BYTES, "The nr of write calls.");
[32179]147#endif
[31753]148 return VINF_SUCCESS;
149}
150
151/**
152 * Terminates the FTM.
153 *
154 * Termination means cleaning up and freeing all resources,
[31770]155 * the VM itself is at this point powered off or suspended.
[31753]156 *
157 * @returns VBox status code.
[58122]158 * @param pVM The cross context VM structure.
[31753]159 */
[44374]160VMMR3_INT_DECL(int) FTMR3Term(PVM pVM)
[31753]161{
[32294]162 if (pVM->ftm.s.hShutdownEvent != NIL_RTSEMEVENT)
[32090]163 {
[32294]164 RTSemEventDestroy(pVM->ftm.s.hShutdownEvent);
165 pVM->ftm.s.hShutdownEvent = NIL_RTSEMEVENT;
[32090]166 }
[32085]167 if (pVM->ftm.s.hSocket != NIL_RTSOCKET)
[32090]168 {
[32085]169 RTTcpClientClose(pVM->ftm.s.hSocket);
[32090]170 pVM->ftm.s.hSocket = NIL_RTSOCKET;
171 }
[32085]172 if (pVM->ftm.s.standby.hServer)
[32090]173 {
[32085]174 RTTcpServerDestroy(pVM->ftm.s.standby.hServer);
[32090]175 pVM->ftm.s.standby.hServer = NULL;
176 }
[31753]177 if (pVM->ftm.s.pszAddress)
178 RTMemFree(pVM->ftm.s.pszAddress);
179 if (pVM->ftm.s.pszPassword)
180 RTMemFree(pVM->ftm.s.pszPassword);
181
[32268]182 /* Remove all pending memory updates. */
[32287]183 if (pVM->ftm.s.standby.pPhysPageTree)
[32268]184 {
[32286]185 RTAvlGCPhysDestroy(&pVM->ftm.s.standby.pPhysPageTree, ftmR3PageTreeDestroyCallback, NULL);
186 pVM->ftm.s.standby.pPhysPageTree = NULL;
[32268]187 }
188
[32090]189 pVM->ftm.s.pszAddress = NULL;
190 pVM->ftm.s.pszPassword = NULL;
191
[31782]192 PDMR3CritSectDelete(&pVM->ftm.s.CritSect);
[31753]193 return VINF_SUCCESS;
194}
195
[31770]196
197static int ftmR3TcpWriteACK(PVM pVM)
198{
[46326]199 int rc = RTTcpWrite(pVM->ftm.s.hSocket, RT_STR_TUPLE("ACK\n"));
[31770]200 if (RT_FAILURE(rc))
201 {
202 LogRel(("FTSync: RTTcpWrite(,ACK,) -> %Rrc\n", rc));
203 }
204 return rc;
205}
206
207
208static int ftmR3TcpWriteNACK(PVM pVM, int32_t rc2, const char *pszMsgText = NULL)
209{
210 char szMsg[256];
211 size_t cch;
212 if (pszMsgText && *pszMsgText)
213 {
214 cch = RTStrPrintf(szMsg, sizeof(szMsg), "NACK=%d;%s\n", rc2, pszMsgText);
215 for (size_t off = 6; off + 1 < cch; off++)
216 if (szMsg[off] == '\n')
217 szMsg[off] = '\r';
218 }
219 else
220 cch = RTStrPrintf(szMsg, sizeof(szMsg), "NACK=%d\n", rc2);
221 int rc = RTTcpWrite(pVM->ftm.s.hSocket, szMsg, cch);
222 if (RT_FAILURE(rc))
223 LogRel(("FTSync: RTTcpWrite(,%s,%zu) -> %Rrc\n", szMsg, cch, rc));
224 return rc;
225}
226
[31753]227/**
[31770]228 * Reads a string from the socket.
229 *
230 * @returns VBox status code.
231 *
[58126]232 * @param pVM The cross context VM structure.
[31770]233 * @param pszBuf The output buffer.
234 * @param cchBuf The size of the output buffer.
235 *
236 */
237static int ftmR3TcpReadLine(PVM pVM, char *pszBuf, size_t cchBuf)
238{
239 char *pszStart = pszBuf;
240 RTSOCKET Sock = pVM->ftm.s.hSocket;
241
242 AssertReturn(cchBuf > 1, VERR_INTERNAL_ERROR);
243 *pszBuf = '\0';
244
245 /* dead simple approach. */
246 for (;;)
247 {
248 char ch;
249 int rc = RTTcpRead(Sock, &ch, sizeof(ch), NULL);
250 if (RT_FAILURE(rc))
251 {
252 LogRel(("FTSync: RTTcpRead -> %Rrc while reading string ('%s')\n", rc, pszStart));
253 return rc;
254 }
255 if ( ch == '\n'
256 || ch == '\0')
257 return VINF_SUCCESS;
258 if (cchBuf <= 1)
259 {
260 LogRel(("FTSync: String buffer overflow: '%s'\n", pszStart));
261 return VERR_BUFFER_OVERFLOW;
262 }
263 *pszBuf++ = ch;
264 *pszBuf = '\0';
265 cchBuf--;
266 }
267}
268
269/**
270 * Reads an ACK or NACK.
271 *
272 * @returns VBox status code.
[58122]273 * @param pVM The cross context VM structure.
[31770]274 * @param pszWhich Which ACK is this this?
275 * @param pszNAckMsg Optional NACK message.
276 */
[31953]277static int ftmR3TcpReadACK(PVM pVM, const char *pszWhich, const char *pszNAckMsg = NULL)
[31770]278{
279 char szMsg[256];
280 int rc = ftmR3TcpReadLine(pVM, szMsg, sizeof(szMsg));
281 if (RT_FAILURE(rc))
282 return rc;
283
284 if (!strcmp(szMsg, "ACK"))
285 return VINF_SUCCESS;
286
[46326]287 if (!strncmp(szMsg, RT_STR_TUPLE("NACK=")))
[31770]288 {
289 char *pszMsgText = strchr(szMsg, ';');
290 if (pszMsgText)
291 *pszMsgText++ = '\0';
292
293 int32_t vrc2;
294 rc = RTStrToInt32Full(&szMsg[sizeof("NACK=") - 1], 10, &vrc2);
295 if (rc == VINF_SUCCESS)
296 {
297 /*
298 * Well formed NACK, transform it into an error.
299 */
300 if (pszNAckMsg)
301 {
302 LogRel(("FTSync: %s: NACK=%Rrc (%d)\n", pszWhich, vrc2, vrc2));
303 return VERR_INTERNAL_ERROR;
304 }
305
306 if (pszMsgText)
307 {
308 pszMsgText = RTStrStrip(pszMsgText);
309 for (size_t off = 0; pszMsgText[off]; off++)
310 if (pszMsgText[off] == '\r')
311 pszMsgText[off] = '\n';
312
313 LogRel(("FTSync: %s: NACK=%Rrc (%d) - '%s'\n", pszWhich, vrc2, vrc2, pszMsgText));
314 }
315 return VERR_INTERNAL_ERROR_2;
316 }
317
318 if (pszMsgText)
319 pszMsgText[-1] = ';';
320 }
321 return VERR_INTERNAL_ERROR_3;
322}
323
324/**
[31953]325 * Submitts a command to the destination and waits for the ACK.
326 *
327 * @returns VBox status code.
328 *
[58122]329 * @param pVM The cross context VM structure.
[31953]330 * @param pszCommand The command.
331 * @param fWaitForAck Whether to wait for the ACK.
332 */
333static int ftmR3TcpSubmitCommand(PVM pVM, const char *pszCommand, bool fWaitForAck = true)
334{
[46326]335 int rc = RTTcpSgWriteL(pVM->ftm.s.hSocket, 2, pszCommand, strlen(pszCommand), RT_STR_TUPLE("\n"));
[31953]336 if (RT_FAILURE(rc))
337 return rc;
338 if (!fWaitForAck)
339 return VINF_SUCCESS;
340 return ftmR3TcpReadACK(pVM, pszCommand);
341}
342
343/**
[58126]344 * @interface_method_impl{SSMSTRMOPS,pfnWrite}
[31809]345 */
346static DECLCALLBACK(int) ftmR3TcpOpWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
347{
348 PVM pVM = (PVM)pvUser;
[39078]349 NOREF(offStream);
[31809]350
351 AssertReturn(cbToWrite > 0, VINF_SUCCESS);
352 AssertReturn(cbToWrite < UINT32_MAX, VERR_OUT_OF_RANGE);
353 AssertReturn(pVM->fFaultTolerantMaster, VERR_INVALID_HANDLE);
354
[32319]355 STAM_COUNTER_INC(&pVM->ftm.s.StatSentStateWrite);
[31809]356 for (;;)
357 {
358 FTMTCPHDR Hdr;
359 Hdr.u32Magic = FTMTCPHDR_MAGIC;
360 Hdr.cb = RT_MIN((uint32_t)cbToWrite, FTMTCPHDR_MAX_SIZE);
361 int rc = RTTcpSgWriteL(pVM->ftm.s.hSocket, 2, &Hdr, sizeof(Hdr), pvBuf, (size_t)Hdr.cb);
362 if (RT_FAILURE(rc))
363 {
364 LogRel(("FTSync/TCP: Write error: %Rrc (cb=%#x)\n", rc, Hdr.cb));
365 return rc;
366 }
[32096]367 pVM->ftm.s.StatSentState.c += Hdr.cb + sizeof(Hdr);
[31809]368 pVM->ftm.s.syncstate.uOffStream += Hdr.cb;
369 if (Hdr.cb == cbToWrite)
370 return VINF_SUCCESS;
371
372 /* advance */
373 cbToWrite -= Hdr.cb;
374 pvBuf = (uint8_t const *)pvBuf + Hdr.cb;
375 }
376}
377
378
379/**
380 * Selects and poll for close condition.
381 *
382 * We can use a relatively high poll timeout here since it's only used to get
383 * us out of error paths. In the normal cause of events, we'll get a
384 * end-of-stream header.
385 *
386 * @returns VBox status code.
387 *
[58126]388 * @param pVM The cross context VM structure.
[31809]389 */
390static int ftmR3TcpReadSelect(PVM pVM)
391{
392 int rc;
393 do
394 {
395 rc = RTTcpSelectOne(pVM->ftm.s.hSocket, 1000);
396 if (RT_FAILURE(rc) && rc != VERR_TIMEOUT)
397 {
398 pVM->ftm.s.syncstate.fIOError = true;
399 LogRel(("FTSync/TCP: Header select error: %Rrc\n", rc));
400 break;
401 }
402 if (pVM->ftm.s.syncstate.fStopReading)
403 {
404 rc = VERR_EOF;
405 break;
406 }
407 } while (rc == VERR_TIMEOUT);
408 return rc;
409}
410
411
412/**
[58126]413 * @interface_method_impl{SSMSTRMOPS,pfnRead}
[31809]414 */
415static DECLCALLBACK(int) ftmR3TcpOpRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
416{
417 PVM pVM = (PVM)pvUser;
418 AssertReturn(!pVM->fFaultTolerantMaster, VERR_INVALID_HANDLE);
[39078]419 NOREF(offStream);
[31809]420
421 for (;;)
422 {
423 int rc;
424
425 /*
426 * Check for various conditions and may have been signalled.
427 */
428 if (pVM->ftm.s.syncstate.fEndOfStream)
429 return VERR_EOF;
430 if (pVM->ftm.s.syncstate.fStopReading)
431 return VERR_EOF;
432 if (pVM->ftm.s.syncstate.fIOError)
433 return VERR_IO_GEN_FAILURE;
434
435 /*
436 * If there is no more data in the current block, read the next
437 * block header.
438 */
439 if (!pVM->ftm.s.syncstate.cbReadBlock)
440 {
441 rc = ftmR3TcpReadSelect(pVM);
442 if (RT_FAILURE(rc))
443 return rc;
444 FTMTCPHDR Hdr;
445 rc = RTTcpRead(pVM->ftm.s.hSocket, &Hdr, sizeof(Hdr), NULL);
446 if (RT_FAILURE(rc))
447 {
448 pVM->ftm.s.syncstate.fIOError = true;
449 LogRel(("FTSync/TCP: Header read error: %Rrc\n", rc));
450 return rc;
451 }
[32096]452 pVM->ftm.s.StatReceivedState.c += sizeof(Hdr);
[31809]453
454 if (RT_UNLIKELY( Hdr.u32Magic != FTMTCPHDR_MAGIC
455 || Hdr.cb > FTMTCPHDR_MAX_SIZE
456 || Hdr.cb == 0))
457 {
458 if ( Hdr.u32Magic == FTMTCPHDR_MAGIC
459 && ( Hdr.cb == 0
460 || Hdr.cb == UINT32_MAX)
461 )
462 {
463 pVM->ftm.s.syncstate.fEndOfStream = true;
464 pVM->ftm.s.syncstate.cbReadBlock = 0;
465 return Hdr.cb ? VERR_SSM_CANCELLED : VERR_EOF;
466 }
467 pVM->ftm.s.syncstate.fIOError = true;
468 LogRel(("FTSync/TCP: Invalid block: u32Magic=%#x cb=%#x\n", Hdr.u32Magic, Hdr.cb));
469 return VERR_IO_GEN_FAILURE;
470 }
471
472 pVM->ftm.s.syncstate.cbReadBlock = Hdr.cb;
473 if (pVM->ftm.s.syncstate.fStopReading)
474 return VERR_EOF;
475 }
476
477 /*
478 * Read more data.
479 */
480 rc = ftmR3TcpReadSelect(pVM);
481 if (RT_FAILURE(rc))
482 return rc;
[32096]483
[31809]484 uint32_t cb = (uint32_t)RT_MIN(pVM->ftm.s.syncstate.cbReadBlock, cbToRead);
485 rc = RTTcpRead(pVM->ftm.s.hSocket, pvBuf, cb, pcbRead);
486 if (RT_FAILURE(rc))
487 {
488 pVM->ftm.s.syncstate.fIOError = true;
489 LogRel(("FTSync/TCP: Data read error: %Rrc (cb=%#x)\n", rc, cb));
490 return rc;
491 }
492 if (pcbRead)
493 {
494 cb = (uint32_t)*pcbRead;
[32096]495 pVM->ftm.s.StatReceivedState.c += cb;
496 pVM->ftm.s.syncstate.uOffStream += cb;
[31809]497 pVM->ftm.s.syncstate.cbReadBlock -= cb;
498 return VINF_SUCCESS;
499 }
[32096]500 pVM->ftm.s.StatReceivedState.c += cb;
501 pVM->ftm.s.syncstate.uOffStream += cb;
[31809]502 pVM->ftm.s.syncstate.cbReadBlock -= cb;
503 if (cbToRead == cb)
504 return VINF_SUCCESS;
505
506 /* Advance to the next block. */
507 cbToRead -= cb;
508 pvBuf = (uint8_t *)pvBuf + cb;
509 }
510}
511
512
513/**
[58126]514 * @interface_method_impl{SSMSTRMOPS,pfnSeek}
[31809]515 */
516static DECLCALLBACK(int) ftmR3TcpOpSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
517{
[39078]518 NOREF(pvUser); NOREF(offSeek); NOREF(uMethod); NOREF(poffActual);
[31809]519 return VERR_NOT_SUPPORTED;
520}
521
522
523/**
[58126]524 * @interface_method_impl{SSMSTRMOPS,pfnTell}
[31809]525 */
526static DECLCALLBACK(uint64_t) ftmR3TcpOpTell(void *pvUser)
527{
528 PVM pVM = (PVM)pvUser;
529 return pVM->ftm.s.syncstate.uOffStream;
530}
531
532
533/**
[58126]534 * @interface_method_impl{SSMSTRMOPS,pfnSize}
[31809]535 */
536static DECLCALLBACK(int) ftmR3TcpOpSize(void *pvUser, uint64_t *pcb)
537{
[39078]538 NOREF(pvUser); NOREF(pcb);
[31809]539 return VERR_NOT_SUPPORTED;
540}
541
542
543/**
[58126]544 * @interface_method_impl{SSMSTRMOPS,pfnIsOk}
[31809]545 */
546static DECLCALLBACK(int) ftmR3TcpOpIsOk(void *pvUser)
547{
548 PVM pVM = (PVM)pvUser;
549
550 if (pVM->fFaultTolerantMaster)
551 {
552 /* Poll for incoming NACKs and errors from the other side */
553 int rc = RTTcpSelectOne(pVM->ftm.s.hSocket, 0);
554 if (rc != VERR_TIMEOUT)
555 {
556 if (RT_SUCCESS(rc))
557 {
558 LogRel(("FTSync/TCP: Incoming data detect by IsOk, assuming it is a cancellation NACK.\n"));
559 rc = VERR_SSM_CANCELLED;
560 }
561 else
562 LogRel(("FTSync/TCP: RTTcpSelectOne -> %Rrc (IsOk).\n", rc));
563 return rc;
564 }
565 }
566
567 return VINF_SUCCESS;
568}
569
570
571/**
[58126]572 * @interface_method_impl{SSMSTRMOPS,pfnClose}
[31809]573 */
[58126]574static DECLCALLBACK(int) ftmR3TcpOpClose(void *pvUser, bool fCancelled)
[31809]575{
576 PVM pVM = (PVM)pvUser;
577
578 if (pVM->fFaultTolerantMaster)
579 {
580 FTMTCPHDR EofHdr;
581 EofHdr.u32Magic = FTMTCPHDR_MAGIC;
[58126]582 EofHdr.cb = fCancelled ? UINT32_MAX : 0;
[31809]583 int rc = RTTcpWrite(pVM->ftm.s.hSocket, &EofHdr, sizeof(EofHdr));
584 if (RT_FAILURE(rc))
585 {
586 LogRel(("FTSync/TCP: EOF Header write error: %Rrc\n", rc));
587 return rc;
588 }
589 }
590 else
591 {
592 ASMAtomicWriteBool(&pVM->ftm.s.syncstate.fStopReading, true);
593 }
594
595 return VINF_SUCCESS;
596}
597
598
599/**
600 * Method table for a TCP based stream.
601 */
602static SSMSTRMOPS const g_ftmR3TcpOps =
603{
604 SSMSTRMOPS_VERSION,
605 ftmR3TcpOpWrite,
606 ftmR3TcpOpRead,
607 ftmR3TcpOpSeek,
608 ftmR3TcpOpTell,
609 ftmR3TcpOpSize,
610 ftmR3TcpOpIsOk,
611 ftmR3TcpOpClose,
612 SSMSTRMOPS_VERSION
613};
614
[32193]615
[31809]616/**
[32102]617 * VMR3ReqCallWait callback
618 *
[58122]619 * @param pVM The cross context VM structure.
[32102]620 *
621 */
622static DECLCALLBACK(void) ftmR3WriteProtectMemory(PVM pVM)
623{
624 int rc = PGMR3PhysWriteProtectRAM(pVM);
625 AssertRC(rc);
626}
627
[32193]628
[32102]629/**
[32193]630 * Sync the VM state
[31895]631 *
632 * @returns VBox status code.
[58122]633 * @param pVM The cross context VM structure.
[31895]634 */
[32193]635static int ftmR3PerformFullSync(PVM pVM)
[31895]636{
[32193]637 bool fSuspended = false;
[31895]638
[46788]639 int rc = VMR3Suspend(pVM->pUVM, VMSUSPENDREASON_FTM_SYNC);
[32193]640 AssertRCReturn(rc, rc);
[31895]641
[32193]642 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatFullSync);
[31953]643
[32193]644 RTSocketRetain(pVM->ftm.s.hSocket); /* For concurrent access by I/O thread and EMT. */
[32102]645
[32193]646 /* Reset the sync state. */
647 pVM->ftm.s.syncstate.uOffStream = 0;
648 pVM->ftm.s.syncstate.cbReadBlock = 0;
649 pVM->ftm.s.syncstate.fStopReading = false;
650 pVM->ftm.s.syncstate.fIOError = false;
651 pVM->ftm.s.syncstate.fEndOfStream = false;
[31953]652
[32193]653 rc = ftmR3TcpSubmitCommand(pVM, "full-sync");
654 AssertRC(rc);
[32155]655
[32193]656 pVM->ftm.s.fDeltaLoadSaveActive = false;
[44340]657 rc = VMR3SaveFT(pVM->pUVM, &g_ftmR3TcpOps, pVM, &fSuspended, false /* fSkipStateChanges */);
[32193]658 AssertRC(rc);
[32158]659
[32193]660 rc = ftmR3TcpReadACK(pVM, "full-sync-complete");
661 AssertRC(rc);
[31953]662
[32193]663 RTSocketRelease(pVM->ftm.s.hSocket);
[32158]664
[31895]665 /* Write protect all memory. */
[32102]666 rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)ftmR3WriteProtectMemory, 1, pVM);
667 AssertRCReturn(rc, rc);
[31895]668
[46788]669 rc = VMR3Resume(pVM->pUVM, VMRESUMEREASON_FTM_SYNC);
[32193]670 AssertRC(rc);
[32169]671
[32193]672 return rc;
[31895]673}
674
[32193]675
[31895]676/**
[31938]677 * PGMR3PhysEnumDirtyFTPages callback for syncing dirty physical pages
678 *
[58122]679 * @param pVM The cross context VM structure.
[31938]680 * @param GCPhys GC physical address
681 * @param pRange HC virtual address of the page(s)
682 * @param cbRange Size of the dirty range in bytes.
683 * @param pvUser User argument
684 */
685static DECLCALLBACK(int) ftmR3SyncDirtyPage(PVM pVM, RTGCPHYS GCPhys, uint8_t *pRange, unsigned cbRange, void *pvUser)
686{
[39078]687 NOREF(pvUser);
[32001]688 FTMTCPHDRMEM Hdr;
689 Hdr.u32Magic = FTMTCPHDR_MAGIC;
690 Hdr.GCPhys = GCPhys;
691 Hdr.cbPageRange = cbRange;
692 Hdr.cb = cbRange;
693 /** @todo compress page(s). */
694 int rc = RTTcpSgWriteL(pVM->ftm.s.hSocket, 2, &Hdr, sizeof(Hdr), pRange, (size_t)Hdr.cb);
695 if (RT_FAILURE(rc))
696 {
697 LogRel(("FTSync/TCP: Write error (ftmR3SyncDirtyPage): %Rrc (cb=%#x)\n", rc, Hdr.cb));
698 return rc;
699 }
[32057]700 pVM->ftm.s.StatSentMem.c += Hdr.cb + sizeof(Hdr);
[32302]701
702#ifdef VBOX_WITH_STATISTICS
703 switch (PGMPhysGetPageType(pVM, GCPhys))
704 {
705 case PGMPAGETYPE_RAM:
706 pVM->ftm.s.StatSentMemRAM.c += Hdr.cb + sizeof(Hdr);
707 break;
708
[32431]709 case PGMPAGETYPE_MMIO2:
[32302]710 pVM->ftm.s.StatSentMemMMIO2.c += Hdr.cb + sizeof(Hdr);
711 break;
712
713 case PGMPAGETYPE_ROM_SHADOW:
714 pVM->ftm.s.StatSentMemShwROM.c += Hdr.cb + sizeof(Hdr);
715 break;
716
717 case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
[47786]718 case PGMPAGETYPE_SPECIAL_ALIAS_MMIO:
[32302]719 AssertFailed();
720 break;
721
722 default:
723 AssertFailed();
724 break;
725 }
726#endif
727
[32199]728 return (pVM->ftm.s.fCheckpointingActive) ? VERR_INTERRUPTED : VINF_SUCCESS;
[31938]729}
730
731/**
[31753]732 * Thread function which starts syncing process for this master VM
733 *
[39078]734 * @param hThread The thread handle.
[41783]735 * @param pvUser Pointer to the VM.
[31753]736 * @return VINF_SUCCESS (ignored).
737 *
738 */
[39078]739static DECLCALLBACK(int) ftmR3MasterThread(RTTHREAD hThread, void *pvUser)
[31753]740{
[31770]741 int rc = VINF_SUCCESS;
742 PVM pVM = (PVM)pvUser;
[39078]743 NOREF(hThread);
[31770]744
745 for (;;)
746 {
747 /*
748 * Try connect to the standby machine.
749 */
[32085]750 Log(("ftmR3MasterThread: client connect to %s %d\n", pVM->ftm.s.pszAddress, pVM->ftm.s.uPort));
[31770]751 rc = RTTcpClientConnect(pVM->ftm.s.pszAddress, pVM->ftm.s.uPort, &pVM->ftm.s.hSocket);
752 if (RT_SUCCESS(rc))
753 {
[32085]754 Log(("ftmR3MasterThread: CONNECTED\n"));
755
[31770]756 /* Disable Nagle. */
757 rc = RTTcpSetSendCoalescing(pVM->ftm.s.hSocket, false /*fEnable*/);
758 AssertRC(rc);
759
760 /* Read and check the welcome message. */
761 char szLine[RT_MAX(128, sizeof(g_szWelcome))];
762 RT_ZERO(szLine);
763 rc = RTTcpRead(pVM->ftm.s.hSocket, szLine, sizeof(g_szWelcome) - 1, NULL);
764 if ( RT_SUCCESS(rc)
765 && !strcmp(szLine, g_szWelcome))
766 {
767 /* password */
[32092]768 if (pVM->ftm.s.pszPassword)
769 rc = RTTcpWrite(pVM->ftm.s.hSocket, pVM->ftm.s.pszPassword, strlen(pVM->ftm.s.pszPassword));
770
[31770]771 if (RT_SUCCESS(rc))
772 {
773 /* ACK */
774 rc = ftmR3TcpReadACK(pVM, "password", "Invalid password");
775 if (RT_SUCCESS(rc))
[31782]776 {
[63560]777 /** @todo verify VM config. */
[31770]778 break;
[31782]779 }
[31770]780 }
781 }
[32089]782 /* Failed, so don't bother anymore. */
783 return VINF_SUCCESS;
[31770]784 }
[32294]785 rc = RTSemEventWait(pVM->ftm.s.hShutdownEvent, 1000 /* 1 second */);
[31770]786 if (rc != VERR_TIMEOUT)
[32431]787 return VINF_SUCCESS; /* told to quit */
[31770]788 }
789
790 /* Successfully initialized the connection to the standby node.
791 * Start the sync process.
792 */
793
[32431]794 /* First sync all memory and write protect everything so
[31895]795 * we can send changed pages later on.
796 */
797
[32193]798 rc = ftmR3PerformFullSync(pVM);
[31895]799
[31782]800 for (;;)
801 {
[32294]802 rc = RTSemEventWait(pVM->ftm.s.hShutdownEvent, pVM->ftm.s.uInterval);
[31938]803 if (rc != VERR_TIMEOUT)
804 break; /* told to quit */
805
[31789]806 if (!pVM->ftm.s.fCheckpointingActive)
807 {
[31807]808 rc = PDMCritSectEnter(&pVM->ftm.s.CritSect, VERR_SEM_BUSY);
809 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
[31789]810
[32001]811 rc = ftmR3TcpSubmitCommand(pVM, "mem-sync");
812 AssertRC(rc);
813
[31789]814 /* sync the changed memory with the standby node. */
[32193]815 /* Write protect all memory. */
[32199]816 if (!pVM->ftm.s.fCheckpointingActive)
817 {
818 rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)ftmR3WriteProtectMemory, 1, pVM);
819 AssertRC(rc);
820 }
[31789]821
[32001]822 /* Enumerate all dirty pages and send them to the standby VM. */
[32199]823 if (!pVM->ftm.s.fCheckpointingActive)
824 {
825 rc = PGMR3PhysEnumDirtyFTPages(pVM, ftmR3SyncDirtyPage, NULL /* pvUser */);
826 Assert(rc == VINF_SUCCESS || rc == VERR_INTERRUPTED);
827 }
[31938]828
[32001]829 /* Send last memory header to signal the end. */
830 FTMTCPHDRMEM Hdr;
831 Hdr.u32Magic = FTMTCPHDR_MAGIC;
832 Hdr.GCPhys = 0;
833 Hdr.cbPageRange = 0;
834 Hdr.cb = 0;
[32002]835 rc = RTTcpSgWriteL(pVM->ftm.s.hSocket, 1, &Hdr, sizeof(Hdr));
[32001]836 if (RT_FAILURE(rc))
837 LogRel(("FTSync/TCP: Write error (ftmR3MasterThread): %Rrc (cb=%#x)\n", rc, Hdr.cb));
838
839 rc = ftmR3TcpReadACK(pVM, "mem-sync-complete");
840 AssertRC(rc);
841
[31789]842 PDMCritSectLeave(&pVM->ftm.s.CritSect);
843 }
[31782]844 }
[31770]845 return rc;
[31753]846}
847
848/**
[32268]849 * Syncs memory from the master VM
850 *
851 * @returns VBox status code.
[58122]852 * @param pVM The cross context VM structure.
[32268]853 */
854static int ftmR3SyncMem(PVM pVM)
855{
856 while (true)
857 {
858 FTMTCPHDRMEM Hdr;
859 RTGCPHYS GCPhys;
860
861 /* Read memory header. */
862 int rc = RTTcpRead(pVM->ftm.s.hSocket, &Hdr, sizeof(Hdr), NULL);
863 if (RT_FAILURE(rc))
864 {
865 Log(("RTTcpRead failed with %Rrc\n", rc));
866 break;
867 }
868 pVM->ftm.s.StatReceivedMem.c += sizeof(Hdr);
869
870 if (Hdr.cb == 0)
871 break; /* end of sync. */
872
873 Assert(Hdr.cb == Hdr.cbPageRange); /** @todo uncompress */
874 GCPhys = Hdr.GCPhys;
875
876 /* Must be a multiple of PAGE_SIZE. */
877 Assert((Hdr.cbPageRange & 0xfff) == 0);
878
879 while (Hdr.cbPageRange)
880 {
[32286]881 PFTMPHYSPAGETREENODE pNode = (PFTMPHYSPAGETREENODE)RTAvlGCPhysGet(&pVM->ftm.s.standby.pPhysPageTree, GCPhys);
[32268]882 if (!pNode)
883 {
884 /* Allocate memory for the node and page. */
885 pNode = (PFTMPHYSPAGETREENODE)RTMemAllocZ(sizeof(*pNode) + PAGE_SIZE);
886 AssertBreak(pNode);
887
888 /* Insert the node into the tree. */
889 pNode->Core.Key = GCPhys;
890 pNode->pPage = (void *)(pNode + 1);
[32286]891 bool fRet = RTAvlGCPhysInsert(&pVM->ftm.s.standby.pPhysPageTree, &pNode->Core);
[39034]892 Assert(fRet); NOREF(fRet);
[32268]893 }
894
895 /* Fetch the page. */
[32285]896 rc = RTTcpRead(pVM->ftm.s.hSocket, pNode->pPage, PAGE_SIZE, NULL);
[32268]897 if (RT_FAILURE(rc))
898 {
899 Log(("RTTcpRead page data (%d bytes) failed with %Rrc\n", Hdr.cb, rc));
900 break;
901 }
902 pVM->ftm.s.StatReceivedMem.c += PAGE_SIZE;
[32291]903 Hdr.cbPageRange -= PAGE_SIZE;
904 GCPhys += PAGE_SIZE;
[32268]905 }
906 }
907 return VINF_SUCCESS;
908}
909
910
911/**
[32285]912 * Callback handler for RTAvlGCPhysDestroy
[32268]913 *
914 * @returns 0 to continue, otherwise stop
915 * @param pBaseNode Node to destroy
[41783]916 * @param pvUser Pointer to the VM.
[32268]917 */
[32285]918static DECLCALLBACK(int) ftmR3PageTreeDestroyCallback(PAVLGCPHYSNODECORE pBaseNode, void *pvUser)
[32268]919{
920 PVM pVM = (PVM)pvUser;
921 PFTMPHYSPAGETREENODE pNode = (PFTMPHYSPAGETREENODE)pBaseNode;
922
923 if (pVM) /* NULL when the VM is destroyed. */
924 {
925 /* Update the guest memory of the standby VM. */
[55899]926 int rc = PGMR3PhysWriteExternal(pVM, pNode->Core.Key, pNode->pPage, PAGE_SIZE, PGMACCESSORIGIN_FTM);
[32268]927 AssertRC(rc);
928 }
929 RTMemFree(pNode);
930 return 0;
931}
932
[32294]933/**
934 * Thread function which monitors the health of the master VM
935 *
[39078]936 * @param hThread The thread handle.
[41783]937 * @param pvUser Pointer to the VM.
[32294]938 * @return VINF_SUCCESS (ignored).
939 *
940 */
[39078]941static DECLCALLBACK(int) ftmR3StandbyThread(RTTHREAD hThread, void *pvUser)
[32294]942{
943 PVM pVM = (PVM)pvUser;
[39078]944 NOREF(hThread);
[32268]945
[32294]946 for (;;)
947 {
948 uint64_t u64TimeNow;
949
950 int rc = RTSemEventWait(pVM->ftm.s.hShutdownEvent, pVM->ftm.s.uInterval);
951 if (rc != VERR_TIMEOUT)
952 break; /* told to quit */
953
954 if (pVM->ftm.s.standby.u64LastHeartbeat)
955 {
956 u64TimeNow = RTTimeMilliTS();
957
958 if (u64TimeNow > pVM->ftm.s.standby.u64LastHeartbeat + pVM->ftm.s.uInterval * 4)
959 {
960 /* Timeout; prepare to fallover. */
[32295]961 LogRel(("FTSync: TIMEOUT (%RX64 vs %RX64 ms): activate standby VM!\n", u64TimeNow, pVM->ftm.s.standby.u64LastHeartbeat + pVM->ftm.s.uInterval * 2));
[32294]962
963 pVM->ftm.s.fActivateStandby = true;
[63560]964 /** @todo prevent split-brain. */
[32294]965 break;
966 }
967 }
968 }
969
970 return VINF_SUCCESS;
971}
972
973
[32268]974/**
[31753]975 * Listen for incoming traffic destined for the standby VM.
976 *
977 * @copydoc FNRTTCPSERVE
978 *
979 * @returns VINF_SUCCESS or VERR_TCP_SERVER_STOP.
980 */
[58126]981static DECLCALLBACK(int) ftmR3StandbyServeConnection(RTSOCKET hSocket, void *pvUser)
[31753]982{
983 PVM pVM = (PVM)pvUser;
984
[58126]985 pVM->ftm.s.hSocket = hSocket;
[31770]986
[31753]987 /*
988 * Disable Nagle.
989 */
[58126]990 int rc = RTTcpSetSendCoalescing(hSocket, false /*fEnable*/);
[31753]991 AssertRC(rc);
992
[31770]993 /* Send the welcome message to the master node. */
[58126]994 rc = RTTcpWrite(hSocket, g_szWelcome, sizeof(g_szWelcome) - 1);
[31770]995 if (RT_FAILURE(rc))
996 {
997 LogRel(("Teleporter: Failed to write welcome message: %Rrc\n", rc));
998 return VINF_SUCCESS;
999 }
1000
1001 /*
1002 * Password.
1003 */
1004 const char *pszPassword = pVM->ftm.s.pszPassword;
[32092]1005 if (pszPassword)
[31770]1006 {
[32092]1007 unsigned off = 0;
1008 while (pszPassword[off])
[31770]1009 {
[32092]1010 char ch;
[58126]1011 rc = RTTcpRead(hSocket, &ch, sizeof(ch), NULL);
[32092]1012 if ( RT_FAILURE(rc)
1013 || pszPassword[off] != ch)
1014 {
1015 if (RT_FAILURE(rc))
1016 LogRel(("FTSync: Password read failure (off=%u): %Rrc\n", off, rc));
1017 else
1018 LogRel(("FTSync: Invalid password (off=%u)\n", off));
1019 ftmR3TcpWriteNACK(pVM, VERR_AUTHENTICATION_FAILURE);
1020 return VINF_SUCCESS;
1021 }
1022 off++;
[31770]1023 }
1024 }
1025 rc = ftmR3TcpWriteACK(pVM);
1026 if (RT_FAILURE(rc))
1027 return VINF_SUCCESS;
1028
[39034]1029 /** @todo verify VM config. */
[31782]1030
[31770]1031 /*
1032 * Stop the server.
1033 *
1034 * Note! After this point we must return VERR_TCP_SERVER_STOP, while prior
1035 * to it we must not return that value!
1036 */
1037 RTTcpServerShutdown(pVM->ftm.s.standby.hServer);
1038
1039 /*
1040 * Command processing loop.
1041 */
[39034]1042 //bool fDone = false;
[31770]1043 for (;;)
1044 {
[32051]1045 bool fFullSync = false;
[31770]1046 char szCmd[128];
[32051]1047
[31770]1048 rc = ftmR3TcpReadLine(pVM, szCmd, sizeof(szCmd));
1049 if (RT_FAILURE(rc))
1050 break;
1051
[32294]1052 pVM->ftm.s.standby.u64LastHeartbeat = RTTimeMilliTS();
[31770]1053 if (!strcmp(szCmd, "mem-sync"))
1054 {
[32001]1055 rc = ftmR3TcpWriteACK(pVM);
1056 AssertRC(rc);
1057 if (RT_FAILURE(rc))
1058 continue;
1059
[32268]1060 rc = ftmR3SyncMem(pVM);
1061 AssertRC(rc);
[32001]1062
1063 rc = ftmR3TcpWriteACK(pVM);
1064 AssertRC(rc);
[31770]1065 }
1066 else
[32051]1067 if ( !strcmp(szCmd, "checkpoint")
[32154]1068 || !strcmp(szCmd, "full-sync")
1069 || (fFullSync = true)) /* intended assignment */
[31770]1070 {
[31953]1071 rc = ftmR3TcpWriteACK(pVM);
1072 AssertRC(rc);
1073 if (RT_FAILURE(rc))
1074 continue;
1075
[32268]1076 /* Flush all pending memory updates. */
[32287]1077 if (pVM->ftm.s.standby.pPhysPageTree)
[32268]1078 {
[32286]1079 RTAvlGCPhysDestroy(&pVM->ftm.s.standby.pPhysPageTree, ftmR3PageTreeDestroyCallback, pVM);
1080 pVM->ftm.s.standby.pPhysPageTree = NULL;
[32268]1081 }
1082
[31953]1083 RTSocketRetain(pVM->ftm.s.hSocket); /* For concurrent access by I/O thread and EMT. */
1084
[32155]1085 /* Reset the sync state. */
1086 pVM->ftm.s.syncstate.uOffStream = 0;
1087 pVM->ftm.s.syncstate.cbReadBlock = 0;
1088 pVM->ftm.s.syncstate.fStopReading = false;
1089 pVM->ftm.s.syncstate.fIOError = false;
1090 pVM->ftm.s.syncstate.fEndOfStream = false;
1091
[32051]1092 pVM->ftm.s.fDeltaLoadSaveActive = (fFullSync == false);
[44340]1093 rc = VMR3LoadFromStreamFT(pVM->pUVM, &g_ftmR3TcpOps, pVM);
[32051]1094 pVM->ftm.s.fDeltaLoadSaveActive = false;
[31953]1095 RTSocketRelease(pVM->ftm.s.hSocket);
1096 AssertRC(rc);
1097 if (RT_FAILURE(rc))
1098 {
1099 LogRel(("FTSync: VMR3LoadFromStream -> %Rrc\n", rc));
1100 ftmR3TcpWriteNACK(pVM, rc);
1101 continue;
1102 }
1103
1104 /* The EOS might not have been read, make sure it is. */
1105 pVM->ftm.s.syncstate.fStopReading = false;
1106 size_t cbRead;
1107 rc = ftmR3TcpOpRead(pVM, pVM->ftm.s.syncstate.uOffStream, szCmd, 1, &cbRead);
1108 if (rc != VERR_EOF)
1109 {
1110 LogRel(("FTSync: Draining teleporterTcpOpRead -> %Rrc\n", rc));
1111 ftmR3TcpWriteNACK(pVM, rc);
1112 continue;
1113 }
1114
1115 rc = ftmR3TcpWriteACK(pVM);
1116 AssertRC(rc);
[32051]1117 }
[31770]1118 }
1119 LogFlowFunc(("returns mRc=%Rrc\n", rc));
1120 return VERR_TCP_SERVER_STOP;
[31753]1121}
1122
1123/**
1124 * Powers on the fault tolerant virtual machine.
1125 *
1126 * @returns VBox status code.
1127 *
[44374]1128 * @param pUVM The user mode VM handle.
[31753]1129 * @param fMaster FT master or standby
1130 * @param uInterval FT sync interval
1131 * @param pszAddress Standby VM address
1132 * @param uPort Standby VM port
1133 * @param pszPassword FT password (NULL for none)
1134 *
1135 * @thread Any thread.
1136 * @vmstate Created
1137 * @vmstateto PoweringOn+Running (master), PoweringOn+Running_FT (standby)
1138 */
[44374]1139VMMR3DECL(int) FTMR3PowerOn(PUVM pUVM, bool fMaster, unsigned uInterval,
1140 const char *pszAddress, unsigned uPort, const char *pszPassword)
[31753]1141{
[44374]1142 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1143 PVM pVM = pUVM->pVM;
1144 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[31753]1145
1146 VMSTATE enmVMState = VMR3GetState(pVM);
[32084]1147 AssertMsgReturn(enmVMState == VMSTATE_CREATED,
[31753]1148 ("%s\n", VMR3GetStateName(enmVMState)),
1149 VERR_INTERNAL_ERROR_4);
1150 AssertReturn(pszAddress, VERR_INVALID_PARAMETER);
1151
[31782]1152 if (pVM->ftm.s.uInterval)
1153 pVM->ftm.s.uInterval = uInterval;
1154 else
1155 pVM->ftm.s.uInterval = 50; /* standard sync interval of 50ms */
1156
1157 pVM->ftm.s.uPort = uPort;
1158 pVM->ftm.s.pszAddress = RTStrDup(pszAddress);
[31753]1159 if (pszPassword)
[31782]1160 pVM->ftm.s.pszPassword = RTStrDup(pszPassword);
[32294]1161
[44374]1162 int rc = RTSemEventCreate(&pVM->ftm.s.hShutdownEvent);
[32294]1163 if (RT_FAILURE(rc))
1164 return rc;
1165
[31753]1166 if (fMaster)
1167 {
[31770]1168 rc = RTThreadCreate(NULL, ftmR3MasterThread, pVM,
[32084]1169 0, RTTHREADTYPE_IO /* higher than normal priority */, 0, "ftmMaster");
[31753]1170 if (RT_FAILURE(rc))
1171 return rc;
1172
1173 pVM->fFaultTolerantMaster = true;
[31807]1174 if (PGMIsUsingLargePages(pVM))
1175 {
1176 /* Must disable large page usage as 2 MB pages are too big to write monitor. */
1177 LogRel(("FTSync: disabling large page usage.\n"));
1178 PGMSetLargePageUsage(pVM, false);
1179 }
1180 /** @todo might need to disable page fusion as well */
1181
[44340]1182 return VMR3PowerOn(pVM->pUVM);
[31753]1183 }
[32294]1184
[31753]1185
[44340]1186 /* standby */
1187 rc = RTThreadCreate(NULL, ftmR3StandbyThread, pVM,
1188 0, RTTHREADTYPE_DEFAULT, 0, "ftmStandby");
1189 if (RT_FAILURE(rc))
1190 return rc;
[31753]1191
[44340]1192 rc = RTTcpServerCreateEx(pszAddress, uPort, &pVM->ftm.s.standby.hServer);
1193 if (RT_FAILURE(rc))
1194 return rc;
1195 pVM->ftm.s.fIsStandbyNode = true;
1196
1197 rc = RTTcpServerListen(pVM->ftm.s.standby.hServer, ftmR3StandbyServeConnection, pVM);
1198 /** @todo deal with the exit code to check if we should activate this standby VM. */
1199 if (pVM->ftm.s.fActivateStandby)
1200 {
1201 /** @todo fallover. */
[31753]1202 }
[44340]1203
1204 if (pVM->ftm.s.standby.hServer)
1205 {
1206 RTTcpServerDestroy(pVM->ftm.s.standby.hServer);
1207 pVM->ftm.s.standby.hServer = NULL;
1208 }
1209 if (rc == VERR_TCP_SERVER_SHUTDOWN)
1210 rc = VINF_SUCCESS; /* ignore this error; the standby process was cancelled. */
[31753]1211 return rc;
1212}
1213
1214/**
1215 * Powers off the fault tolerant virtual machine (standby).
1216 *
1217 * @returns VBox status code.
1218 *
[44374]1219 * @param pUVM The user mode VM handle.
[31753]1220 */
[44374]1221VMMR3DECL(int) FTMR3CancelStandby(PUVM pUVM)
[31753]1222{
[44374]1223 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1224 PVM pVM = pUVM->pVM;
1225 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
[31753]1226 AssertReturn(!pVM->fFaultTolerantMaster, VERR_NOT_SUPPORTED);
1227 Assert(pVM->ftm.s.standby.hServer);
1228
1229 return RTTcpServerShutdown(pVM->ftm.s.standby.hServer);
1230}
[31782]1231
[32193]1232/**
1233 * Rendezvous callback used by FTMR3SetCheckpoint
1234 * Sync state + changed memory with the standby node.
1235 *
1236 * This is only called on one of the EMTs while the other ones are waiting for
1237 * it to complete this function.
1238 *
1239 * @returns VINF_SUCCESS (VBox strict status code).
[58122]1240 * @param pVM The cross context VM structure.
[58123]1241 * @param pVCpu The cross context virtual CPU structure of the calling EMT. Unused.
[39078]1242 * @param pvUser Not used.
[32193]1243 */
1244static DECLCALLBACK(VBOXSTRICTRC) ftmR3SetCheckpointRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
1245{
[39078]1246 int rc = VINF_SUCCESS;
1247 bool fSuspended = false;
1248 NOREF(pVCpu);
1249 NOREF(pvUser);
[31782]1250
[38614]1251 /* We don't call VMR3Suspend here to avoid the overhead of state changes and notifications. This
1252 * is only a short suspend.
[32193]1253 */
[32308]1254 STAM_PROFILE_START(&pVM->ftm.s.StatCheckpointPause, a);
[32193]1255 PDMR3Suspend(pVM);
1256
[38614]1257 /* Hack alert: as EM is responsible for dealing with the suspend state. We must do this here ourselves, but only for this EMT.*/
[32193]1258 EMR3NotifySuspend(pVM);
[32308]1259 STAM_PROFILE_STOP(&pVM->ftm.s.StatCheckpointPause, a);
[32193]1260
1261 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatDeltaVM);
1262
1263 RTSocketRetain(pVM->ftm.s.hSocket); /* For concurrent access by I/O thread and EMT. */
1264
1265 /* Reset the sync state. */
1266 pVM->ftm.s.syncstate.uOffStream = 0;
1267 pVM->ftm.s.syncstate.cbReadBlock = 0;
1268 pVM->ftm.s.syncstate.fStopReading = false;
1269 pVM->ftm.s.syncstate.fIOError = false;
1270 pVM->ftm.s.syncstate.fEndOfStream = false;
1271
1272 rc = ftmR3TcpSubmitCommand(pVM, "checkpoint");
1273 AssertRC(rc);
1274
1275 pVM->ftm.s.fDeltaLoadSaveActive = true;
[44340]1276 rc = VMR3SaveFT(pVM->pUVM, &g_ftmR3TcpOps, pVM, &fSuspended, true /* fSkipStateChanges */);
[32193]1277 pVM->ftm.s.fDeltaLoadSaveActive = false;
1278 AssertRC(rc);
1279
1280 rc = ftmR3TcpReadACK(pVM, "checkpoint-complete");
1281 AssertRC(rc);
1282
1283 RTSocketRelease(pVM->ftm.s.hSocket);
1284
1285 /* Write protect all memory. */
1286 rc = PGMR3PhysWriteProtectRAM(pVM);
1287 AssertRC(rc);
1288
[38614]1289 /* We don't call VMR3Resume here to avoid the overhead of state changes and notifications. This
1290 * is only a short suspend.
[32193]1291 */
[32308]1292 STAM_PROFILE_START(&pVM->ftm.s.StatCheckpointResume, b);
[32245]1293 PGMR3ResetNoMorePhysWritesFlag(pVM);
[32193]1294 PDMR3Resume(pVM);
1295
[38614]1296 /* Hack alert as EM is responsible for dealing with the suspend state. We must do this here ourselves, but only for this EMT.*/
[32193]1297 EMR3NotifyResume(pVM);
[32308]1298 STAM_PROFILE_STOP(&pVM->ftm.s.StatCheckpointResume, b);
1299
[32193]1300 return rc;
1301}
1302
[31782]1303/**
[31789]1304 * Performs a full sync to the standby node
[31782]1305 *
1306 * @returns VBox status code.
1307 *
[58122]1308 * @param pVM The cross context VM structure.
[32144]1309 * @param enmCheckpoint Checkpoint type
[31782]1310 */
[44374]1311VMMR3_INT_DECL(int) FTMR3SetCheckpoint(PVM pVM, FTMCHECKPOINTTYPE enmCheckpoint)
[31782]1312{
[32151]1313 int rc;
1314
[31782]1315 if (!pVM->fFaultTolerantMaster)
1316 return VINF_SUCCESS;
1317
[32144]1318 switch (enmCheckpoint)
1319 {
[44374]1320 case FTMCHECKPOINTTYPE_NETWORK:
1321 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatCheckpointNetwork);
1322 break;
[32144]1323
[44374]1324 case FTMCHECKPOINTTYPE_STORAGE:
1325 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatCheckpointStorage);
1326 break;
[32151]1327
[44374]1328 default:
1329 AssertMsgFailedReturn(("%d\n", enmCheckpoint), VERR_INVALID_PARAMETER);
[32144]1330 }
[44374]1331
[31789]1332 pVM->ftm.s.fCheckpointingActive = true;
[32151]1333 if (VM_IS_EMT(pVM))
1334 {
1335 PVMCPU pVCpu = VMMGetCpu(pVM);
1336
1337 /* We must take special care here as the memory sync is competing with us and requires a responsive EMT. */
1338 while ((rc = PDMCritSectTryEnter(&pVM->ftm.s.CritSect)) == VERR_SEM_BUSY)
1339 {
[74790]1340 if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
[32151]1341 {
1342 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
1343 AssertRC(rc);
1344 }
1345
[74790]1346 if (VM_FF_IS_SET(pVM, VM_FF_REQUEST))
[32151]1347 {
[38838]1348 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
[32151]1349 AssertRC(rc);
1350 }
1351 }
1352 }
1353 else
1354 rc = PDMCritSectEnter(&pVM->ftm.s.CritSect, VERR_SEM_BUSY);
1355
[31789]1356 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
1357
[32179]1358 STAM_PROFILE_START(&pVM->ftm.s.StatCheckpoint, a);
1359
[32193]1360 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, ftmR3SetCheckpointRendezvous, NULL);
[31809]1361
[32179]1362 STAM_PROFILE_STOP(&pVM->ftm.s.StatCheckpoint, a);
1363
[31789]1364 PDMCritSectLeave(&pVM->ftm.s.CritSect);
1365 pVM->ftm.s.fCheckpointingActive = false;
1366
[32141]1367 return rc;
[31785]1368}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use