VirtualBox

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

Last change on this file since 78425 was 76553, checked in by vboxsync, 6 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
Line 
1/* $Id: FTM.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * FTM - Fault Tolerance Manager
4 */
5
6/*
7 * Copyright (C) 2010-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_FTM
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>
29#include "FTMInternal.h"
30#include <VBox/vmm/vm.h>
31#include <VBox/vmm/uvm.h>
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>
41#include <iprt/socket.h>
42#include <iprt/semaphore.h>
43#include <iprt/asm.h>
44
45
46/*******************************************************************************
47 * Structures and Typedefs *
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;
65/** Magic value for FTMTCPHDR::u32Magic. (Egberto Gismonti Amin) */
66#define FTMTCPHDR_MAGIC UINT32_C(0x19471205)
67/** The max block size. */
68#define FTMTCPHDR_MAX_SIZE UINT32_C(0x00fffff8)
69
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
90
91/*********************************************************************************************************************************
92* Global Variables *
93*********************************************************************************************************************************/
94static const char g_szWelcome[] = "VirtualBox-Fault-Tolerance-Sync-1.0\n";
95
96static DECLCALLBACK(int) ftmR3PageTreeDestroyCallback(PAVLGCPHYSNODECORE pBaseNode, void *pvUser);
97
98/**
99 * Initializes the FTM.
100 *
101 * @returns VBox status code.
102 * @param pVM The cross context VM structure.
103 */
104VMMR3_INT_DECL(int) FTMR3Init(PVM pVM)
105{
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
112 /** @todo saved state for master nodes! */
113 pVM->ftm.s.pszAddress = NULL;
114 pVM->ftm.s.pszPassword = NULL;
115 pVM->fFaultTolerantMaster = false;
116 pVM->ftm.s.fIsStandbyNode = false;
117 pVM->ftm.s.standby.hServer = NIL_RTTCPSERVER;
118 pVM->ftm.s.hShutdownEvent = NIL_RTSEMEVENT;
119 pVM->ftm.s.hSocket = NIL_RTSOCKET;
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
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.");
137 STAM_REL_REG(pVM, &pVM->ftm.s.StatCheckpointStorage, STAMTYPE_COUNTER, "/FT/Checkpoint/Storage", STAMUNIT_OCCURENCES, "Number of storage checkpoints.");
138 STAM_REL_REG(pVM, &pVM->ftm.s.StatCheckpointNetwork, STAMTYPE_COUNTER, "/FT/Checkpoint/Network", STAMUNIT_OCCURENCES, "Number of network checkpoints.");
139#ifdef VBOX_WITH_STATISTICS
140 STAM_REG(pVM, &pVM->ftm.s.StatCheckpoint, STAMTYPE_PROFILE, "/FT/Checkpoint", STAMUNIT_TICKS_PER_CALL, "Profiling of FTMR3SetCheckpoint.");
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.");
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.");
146 STAM_REG(pVM, &pVM->ftm.s.StatSentStateWrite, STAMTYPE_COUNTER, "/FT/Sent/State/Writes", STAMUNIT_BYTES, "The nr of write calls.");
147#endif
148 return VINF_SUCCESS;
149}
150
151/**
152 * Terminates the FTM.
153 *
154 * Termination means cleaning up and freeing all resources,
155 * the VM itself is at this point powered off or suspended.
156 *
157 * @returns VBox status code.
158 * @param pVM The cross context VM structure.
159 */
160VMMR3_INT_DECL(int) FTMR3Term(PVM pVM)
161{
162 if (pVM->ftm.s.hShutdownEvent != NIL_RTSEMEVENT)
163 {
164 RTSemEventDestroy(pVM->ftm.s.hShutdownEvent);
165 pVM->ftm.s.hShutdownEvent = NIL_RTSEMEVENT;
166 }
167 if (pVM->ftm.s.hSocket != NIL_RTSOCKET)
168 {
169 RTTcpClientClose(pVM->ftm.s.hSocket);
170 pVM->ftm.s.hSocket = NIL_RTSOCKET;
171 }
172 if (pVM->ftm.s.standby.hServer)
173 {
174 RTTcpServerDestroy(pVM->ftm.s.standby.hServer);
175 pVM->ftm.s.standby.hServer = NULL;
176 }
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
182 /* Remove all pending memory updates. */
183 if (pVM->ftm.s.standby.pPhysPageTree)
184 {
185 RTAvlGCPhysDestroy(&pVM->ftm.s.standby.pPhysPageTree, ftmR3PageTreeDestroyCallback, NULL);
186 pVM->ftm.s.standby.pPhysPageTree = NULL;
187 }
188
189 pVM->ftm.s.pszAddress = NULL;
190 pVM->ftm.s.pszPassword = NULL;
191
192 PDMR3CritSectDelete(&pVM->ftm.s.CritSect);
193 return VINF_SUCCESS;
194}
195
196
197static int ftmR3TcpWriteACK(PVM pVM)
198{
199 int rc = RTTcpWrite(pVM->ftm.s.hSocket, RT_STR_TUPLE("ACK\n"));
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
227/**
228 * Reads a string from the socket.
229 *
230 * @returns VBox status code.
231 *
232 * @param pVM The cross context VM structure.
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.
273 * @param pVM The cross context VM structure.
274 * @param pszWhich Which ACK is this this?
275 * @param pszNAckMsg Optional NACK message.
276 */
277static int ftmR3TcpReadACK(PVM pVM, const char *pszWhich, const char *pszNAckMsg = NULL)
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
287 if (!strncmp(szMsg, RT_STR_TUPLE("NACK=")))
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/**
325 * Submitts a command to the destination and waits for the ACK.
326 *
327 * @returns VBox status code.
328 *
329 * @param pVM The cross context VM structure.
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{
335 int rc = RTTcpSgWriteL(pVM->ftm.s.hSocket, 2, pszCommand, strlen(pszCommand), RT_STR_TUPLE("\n"));
336 if (RT_FAILURE(rc))
337 return rc;
338 if (!fWaitForAck)
339 return VINF_SUCCESS;
340 return ftmR3TcpReadACK(pVM, pszCommand);
341}
342
343/**
344 * @interface_method_impl{SSMSTRMOPS,pfnWrite}
345 */
346static DECLCALLBACK(int) ftmR3TcpOpWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
347{
348 PVM pVM = (PVM)pvUser;
349 NOREF(offStream);
350
351 AssertReturn(cbToWrite > 0, VINF_SUCCESS);
352 AssertReturn(cbToWrite < UINT32_MAX, VERR_OUT_OF_RANGE);
353 AssertReturn(pVM->fFaultTolerantMaster, VERR_INVALID_HANDLE);
354
355 STAM_COUNTER_INC(&pVM->ftm.s.StatSentStateWrite);
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 }
367 pVM->ftm.s.StatSentState.c += Hdr.cb + sizeof(Hdr);
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 *
388 * @param pVM The cross context VM structure.
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/**
413 * @interface_method_impl{SSMSTRMOPS,pfnRead}
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);
419 NOREF(offStream);
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 }
452 pVM->ftm.s.StatReceivedState.c += sizeof(Hdr);
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;
483
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;
495 pVM->ftm.s.StatReceivedState.c += cb;
496 pVM->ftm.s.syncstate.uOffStream += cb;
497 pVM->ftm.s.syncstate.cbReadBlock -= cb;
498 return VINF_SUCCESS;
499 }
500 pVM->ftm.s.StatReceivedState.c += cb;
501 pVM->ftm.s.syncstate.uOffStream += cb;
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/**
514 * @interface_method_impl{SSMSTRMOPS,pfnSeek}
515 */
516static DECLCALLBACK(int) ftmR3TcpOpSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
517{
518 NOREF(pvUser); NOREF(offSeek); NOREF(uMethod); NOREF(poffActual);
519 return VERR_NOT_SUPPORTED;
520}
521
522
523/**
524 * @interface_method_impl{SSMSTRMOPS,pfnTell}
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/**
534 * @interface_method_impl{SSMSTRMOPS,pfnSize}
535 */
536static DECLCALLBACK(int) ftmR3TcpOpSize(void *pvUser, uint64_t *pcb)
537{
538 NOREF(pvUser); NOREF(pcb);
539 return VERR_NOT_SUPPORTED;
540}
541
542
543/**
544 * @interface_method_impl{SSMSTRMOPS,pfnIsOk}
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/**
572 * @interface_method_impl{SSMSTRMOPS,pfnClose}
573 */
574static DECLCALLBACK(int) ftmR3TcpOpClose(void *pvUser, bool fCancelled)
575{
576 PVM pVM = (PVM)pvUser;
577
578 if (pVM->fFaultTolerantMaster)
579 {
580 FTMTCPHDR EofHdr;
581 EofHdr.u32Magic = FTMTCPHDR_MAGIC;
582 EofHdr.cb = fCancelled ? UINT32_MAX : 0;
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
615
616/**
617 * VMR3ReqCallWait callback
618 *
619 * @param pVM The cross context VM structure.
620 *
621 */
622static DECLCALLBACK(void) ftmR3WriteProtectMemory(PVM pVM)
623{
624 int rc = PGMR3PhysWriteProtectRAM(pVM);
625 AssertRC(rc);
626}
627
628
629/**
630 * Sync the VM state
631 *
632 * @returns VBox status code.
633 * @param pVM The cross context VM structure.
634 */
635static int ftmR3PerformFullSync(PVM pVM)
636{
637 bool fSuspended = false;
638
639 int rc = VMR3Suspend(pVM->pUVM, VMSUSPENDREASON_FTM_SYNC);
640 AssertRCReturn(rc, rc);
641
642 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatFullSync);
643
644 RTSocketRetain(pVM->ftm.s.hSocket); /* For concurrent access by I/O thread and EMT. */
645
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;
652
653 rc = ftmR3TcpSubmitCommand(pVM, "full-sync");
654 AssertRC(rc);
655
656 pVM->ftm.s.fDeltaLoadSaveActive = false;
657 rc = VMR3SaveFT(pVM->pUVM, &g_ftmR3TcpOps, pVM, &fSuspended, false /* fSkipStateChanges */);
658 AssertRC(rc);
659
660 rc = ftmR3TcpReadACK(pVM, "full-sync-complete");
661 AssertRC(rc);
662
663 RTSocketRelease(pVM->ftm.s.hSocket);
664
665 /* Write protect all memory. */
666 rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)ftmR3WriteProtectMemory, 1, pVM);
667 AssertRCReturn(rc, rc);
668
669 rc = VMR3Resume(pVM->pUVM, VMRESUMEREASON_FTM_SYNC);
670 AssertRC(rc);
671
672 return rc;
673}
674
675
676/**
677 * PGMR3PhysEnumDirtyFTPages callback for syncing dirty physical pages
678 *
679 * @param pVM The cross context VM structure.
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{
687 NOREF(pvUser);
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 }
700 pVM->ftm.s.StatSentMem.c += Hdr.cb + sizeof(Hdr);
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
709 case PGMPAGETYPE_MMIO2:
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:
718 case PGMPAGETYPE_SPECIAL_ALIAS_MMIO:
719 AssertFailed();
720 break;
721
722 default:
723 AssertFailed();
724 break;
725 }
726#endif
727
728 return (pVM->ftm.s.fCheckpointingActive) ? VERR_INTERRUPTED : VINF_SUCCESS;
729}
730
731/**
732 * Thread function which starts syncing process for this master VM
733 *
734 * @param hThread The thread handle.
735 * @param pvUser Pointer to the VM.
736 * @return VINF_SUCCESS (ignored).
737 *
738 */
739static DECLCALLBACK(int) ftmR3MasterThread(RTTHREAD hThread, void *pvUser)
740{
741 int rc = VINF_SUCCESS;
742 PVM pVM = (PVM)pvUser;
743 NOREF(hThread);
744
745 for (;;)
746 {
747 /*
748 * Try connect to the standby machine.
749 */
750 Log(("ftmR3MasterThread: client connect to %s %d\n", pVM->ftm.s.pszAddress, pVM->ftm.s.uPort));
751 rc = RTTcpClientConnect(pVM->ftm.s.pszAddress, pVM->ftm.s.uPort, &pVM->ftm.s.hSocket);
752 if (RT_SUCCESS(rc))
753 {
754 Log(("ftmR3MasterThread: CONNECTED\n"));
755
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 */
768 if (pVM->ftm.s.pszPassword)
769 rc = RTTcpWrite(pVM->ftm.s.hSocket, pVM->ftm.s.pszPassword, strlen(pVM->ftm.s.pszPassword));
770
771 if (RT_SUCCESS(rc))
772 {
773 /* ACK */
774 rc = ftmR3TcpReadACK(pVM, "password", "Invalid password");
775 if (RT_SUCCESS(rc))
776 {
777 /** @todo verify VM config. */
778 break;
779 }
780 }
781 }
782 /* Failed, so don't bother anymore. */
783 return VINF_SUCCESS;
784 }
785 rc = RTSemEventWait(pVM->ftm.s.hShutdownEvent, 1000 /* 1 second */);
786 if (rc != VERR_TIMEOUT)
787 return VINF_SUCCESS; /* told to quit */
788 }
789
790 /* Successfully initialized the connection to the standby node.
791 * Start the sync process.
792 */
793
794 /* First sync all memory and write protect everything so
795 * we can send changed pages later on.
796 */
797
798 rc = ftmR3PerformFullSync(pVM);
799
800 for (;;)
801 {
802 rc = RTSemEventWait(pVM->ftm.s.hShutdownEvent, pVM->ftm.s.uInterval);
803 if (rc != VERR_TIMEOUT)
804 break; /* told to quit */
805
806 if (!pVM->ftm.s.fCheckpointingActive)
807 {
808 rc = PDMCritSectEnter(&pVM->ftm.s.CritSect, VERR_SEM_BUSY);
809 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
810
811 rc = ftmR3TcpSubmitCommand(pVM, "mem-sync");
812 AssertRC(rc);
813
814 /* sync the changed memory with the standby node. */
815 /* Write protect all memory. */
816 if (!pVM->ftm.s.fCheckpointingActive)
817 {
818 rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)ftmR3WriteProtectMemory, 1, pVM);
819 AssertRC(rc);
820 }
821
822 /* Enumerate all dirty pages and send them to the standby VM. */
823 if (!pVM->ftm.s.fCheckpointingActive)
824 {
825 rc = PGMR3PhysEnumDirtyFTPages(pVM, ftmR3SyncDirtyPage, NULL /* pvUser */);
826 Assert(rc == VINF_SUCCESS || rc == VERR_INTERRUPTED);
827 }
828
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;
835 rc = RTTcpSgWriteL(pVM->ftm.s.hSocket, 1, &Hdr, sizeof(Hdr));
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
842 PDMCritSectLeave(&pVM->ftm.s.CritSect);
843 }
844 }
845 return rc;
846}
847
848/**
849 * Syncs memory from the master VM
850 *
851 * @returns VBox status code.
852 * @param pVM The cross context VM structure.
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 {
881 PFTMPHYSPAGETREENODE pNode = (PFTMPHYSPAGETREENODE)RTAvlGCPhysGet(&pVM->ftm.s.standby.pPhysPageTree, GCPhys);
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);
891 bool fRet = RTAvlGCPhysInsert(&pVM->ftm.s.standby.pPhysPageTree, &pNode->Core);
892 Assert(fRet); NOREF(fRet);
893 }
894
895 /* Fetch the page. */
896 rc = RTTcpRead(pVM->ftm.s.hSocket, pNode->pPage, PAGE_SIZE, NULL);
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;
903 Hdr.cbPageRange -= PAGE_SIZE;
904 GCPhys += PAGE_SIZE;
905 }
906 }
907 return VINF_SUCCESS;
908}
909
910
911/**
912 * Callback handler for RTAvlGCPhysDestroy
913 *
914 * @returns 0 to continue, otherwise stop
915 * @param pBaseNode Node to destroy
916 * @param pvUser Pointer to the VM.
917 */
918static DECLCALLBACK(int) ftmR3PageTreeDestroyCallback(PAVLGCPHYSNODECORE pBaseNode, void *pvUser)
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. */
926 int rc = PGMR3PhysWriteExternal(pVM, pNode->Core.Key, pNode->pPage, PAGE_SIZE, PGMACCESSORIGIN_FTM);
927 AssertRC(rc);
928 }
929 RTMemFree(pNode);
930 return 0;
931}
932
933/**
934 * Thread function which monitors the health of the master VM
935 *
936 * @param hThread The thread handle.
937 * @param pvUser Pointer to the VM.
938 * @return VINF_SUCCESS (ignored).
939 *
940 */
941static DECLCALLBACK(int) ftmR3StandbyThread(RTTHREAD hThread, void *pvUser)
942{
943 PVM pVM = (PVM)pvUser;
944 NOREF(hThread);
945
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. */
961 LogRel(("FTSync: TIMEOUT (%RX64 vs %RX64 ms): activate standby VM!\n", u64TimeNow, pVM->ftm.s.standby.u64LastHeartbeat + pVM->ftm.s.uInterval * 2));
962
963 pVM->ftm.s.fActivateStandby = true;
964 /** @todo prevent split-brain. */
965 break;
966 }
967 }
968 }
969
970 return VINF_SUCCESS;
971}
972
973
974/**
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 */
981static DECLCALLBACK(int) ftmR3StandbyServeConnection(RTSOCKET hSocket, void *pvUser)
982{
983 PVM pVM = (PVM)pvUser;
984
985 pVM->ftm.s.hSocket = hSocket;
986
987 /*
988 * Disable Nagle.
989 */
990 int rc = RTTcpSetSendCoalescing(hSocket, false /*fEnable*/);
991 AssertRC(rc);
992
993 /* Send the welcome message to the master node. */
994 rc = RTTcpWrite(hSocket, g_szWelcome, sizeof(g_szWelcome) - 1);
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;
1005 if (pszPassword)
1006 {
1007 unsigned off = 0;
1008 while (pszPassword[off])
1009 {
1010 char ch;
1011 rc = RTTcpRead(hSocket, &ch, sizeof(ch), NULL);
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++;
1023 }
1024 }
1025 rc = ftmR3TcpWriteACK(pVM);
1026 if (RT_FAILURE(rc))
1027 return VINF_SUCCESS;
1028
1029 /** @todo verify VM config. */
1030
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 */
1042 //bool fDone = false;
1043 for (;;)
1044 {
1045 bool fFullSync = false;
1046 char szCmd[128];
1047
1048 rc = ftmR3TcpReadLine(pVM, szCmd, sizeof(szCmd));
1049 if (RT_FAILURE(rc))
1050 break;
1051
1052 pVM->ftm.s.standby.u64LastHeartbeat = RTTimeMilliTS();
1053 if (!strcmp(szCmd, "mem-sync"))
1054 {
1055 rc = ftmR3TcpWriteACK(pVM);
1056 AssertRC(rc);
1057 if (RT_FAILURE(rc))
1058 continue;
1059
1060 rc = ftmR3SyncMem(pVM);
1061 AssertRC(rc);
1062
1063 rc = ftmR3TcpWriteACK(pVM);
1064 AssertRC(rc);
1065 }
1066 else
1067 if ( !strcmp(szCmd, "checkpoint")
1068 || !strcmp(szCmd, "full-sync")
1069 || (fFullSync = true)) /* intended assignment */
1070 {
1071 rc = ftmR3TcpWriteACK(pVM);
1072 AssertRC(rc);
1073 if (RT_FAILURE(rc))
1074 continue;
1075
1076 /* Flush all pending memory updates. */
1077 if (pVM->ftm.s.standby.pPhysPageTree)
1078 {
1079 RTAvlGCPhysDestroy(&pVM->ftm.s.standby.pPhysPageTree, ftmR3PageTreeDestroyCallback, pVM);
1080 pVM->ftm.s.standby.pPhysPageTree = NULL;
1081 }
1082
1083 RTSocketRetain(pVM->ftm.s.hSocket); /* For concurrent access by I/O thread and EMT. */
1084
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
1092 pVM->ftm.s.fDeltaLoadSaveActive = (fFullSync == false);
1093 rc = VMR3LoadFromStreamFT(pVM->pUVM, &g_ftmR3TcpOps, pVM);
1094 pVM->ftm.s.fDeltaLoadSaveActive = false;
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);
1117 }
1118 }
1119 LogFlowFunc(("returns mRc=%Rrc\n", rc));
1120 return VERR_TCP_SERVER_STOP;
1121}
1122
1123/**
1124 * Powers on the fault tolerant virtual machine.
1125 *
1126 * @returns VBox status code.
1127 *
1128 * @param pUVM The user mode VM handle.
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 */
1139VMMR3DECL(int) FTMR3PowerOn(PUVM pUVM, bool fMaster, unsigned uInterval,
1140 const char *pszAddress, unsigned uPort, const char *pszPassword)
1141{
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);
1145
1146 VMSTATE enmVMState = VMR3GetState(pVM);
1147 AssertMsgReturn(enmVMState == VMSTATE_CREATED,
1148 ("%s\n", VMR3GetStateName(enmVMState)),
1149 VERR_INTERNAL_ERROR_4);
1150 AssertReturn(pszAddress, VERR_INVALID_PARAMETER);
1151
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);
1159 if (pszPassword)
1160 pVM->ftm.s.pszPassword = RTStrDup(pszPassword);
1161
1162 int rc = RTSemEventCreate(&pVM->ftm.s.hShutdownEvent);
1163 if (RT_FAILURE(rc))
1164 return rc;
1165
1166 if (fMaster)
1167 {
1168 rc = RTThreadCreate(NULL, ftmR3MasterThread, pVM,
1169 0, RTTHREADTYPE_IO /* higher than normal priority */, 0, "ftmMaster");
1170 if (RT_FAILURE(rc))
1171 return rc;
1172
1173 pVM->fFaultTolerantMaster = true;
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
1182 return VMR3PowerOn(pVM->pUVM);
1183 }
1184
1185
1186 /* standby */
1187 rc = RTThreadCreate(NULL, ftmR3StandbyThread, pVM,
1188 0, RTTHREADTYPE_DEFAULT, 0, "ftmStandby");
1189 if (RT_FAILURE(rc))
1190 return rc;
1191
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. */
1202 }
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. */
1211 return rc;
1212}
1213
1214/**
1215 * Powers off the fault tolerant virtual machine (standby).
1216 *
1217 * @returns VBox status code.
1218 *
1219 * @param pUVM The user mode VM handle.
1220 */
1221VMMR3DECL(int) FTMR3CancelStandby(PUVM pUVM)
1222{
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);
1226 AssertReturn(!pVM->fFaultTolerantMaster, VERR_NOT_SUPPORTED);
1227 Assert(pVM->ftm.s.standby.hServer);
1228
1229 return RTTcpServerShutdown(pVM->ftm.s.standby.hServer);
1230}
1231
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).
1240 * @param pVM The cross context VM structure.
1241 * @param pVCpu The cross context virtual CPU structure of the calling EMT. Unused.
1242 * @param pvUser Not used.
1243 */
1244static DECLCALLBACK(VBOXSTRICTRC) ftmR3SetCheckpointRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
1245{
1246 int rc = VINF_SUCCESS;
1247 bool fSuspended = false;
1248 NOREF(pVCpu);
1249 NOREF(pvUser);
1250
1251 /* We don't call VMR3Suspend here to avoid the overhead of state changes and notifications. This
1252 * is only a short suspend.
1253 */
1254 STAM_PROFILE_START(&pVM->ftm.s.StatCheckpointPause, a);
1255 PDMR3Suspend(pVM);
1256
1257 /* Hack alert: as EM is responsible for dealing with the suspend state. We must do this here ourselves, but only for this EMT.*/
1258 EMR3NotifySuspend(pVM);
1259 STAM_PROFILE_STOP(&pVM->ftm.s.StatCheckpointPause, a);
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;
1276 rc = VMR3SaveFT(pVM->pUVM, &g_ftmR3TcpOps, pVM, &fSuspended, true /* fSkipStateChanges */);
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
1289 /* We don't call VMR3Resume here to avoid the overhead of state changes and notifications. This
1290 * is only a short suspend.
1291 */
1292 STAM_PROFILE_START(&pVM->ftm.s.StatCheckpointResume, b);
1293 PGMR3ResetNoMorePhysWritesFlag(pVM);
1294 PDMR3Resume(pVM);
1295
1296 /* Hack alert as EM is responsible for dealing with the suspend state. We must do this here ourselves, but only for this EMT.*/
1297 EMR3NotifyResume(pVM);
1298 STAM_PROFILE_STOP(&pVM->ftm.s.StatCheckpointResume, b);
1299
1300 return rc;
1301}
1302
1303/**
1304 * Performs a full sync to the standby node
1305 *
1306 * @returns VBox status code.
1307 *
1308 * @param pVM The cross context VM structure.
1309 * @param enmCheckpoint Checkpoint type
1310 */
1311VMMR3_INT_DECL(int) FTMR3SetCheckpoint(PVM pVM, FTMCHECKPOINTTYPE enmCheckpoint)
1312{
1313 int rc;
1314
1315 if (!pVM->fFaultTolerantMaster)
1316 return VINF_SUCCESS;
1317
1318 switch (enmCheckpoint)
1319 {
1320 case FTMCHECKPOINTTYPE_NETWORK:
1321 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatCheckpointNetwork);
1322 break;
1323
1324 case FTMCHECKPOINTTYPE_STORAGE:
1325 STAM_REL_COUNTER_INC(&pVM->ftm.s.StatCheckpointStorage);
1326 break;
1327
1328 default:
1329 AssertMsgFailedReturn(("%d\n", enmCheckpoint), VERR_INVALID_PARAMETER);
1330 }
1331
1332 pVM->ftm.s.fCheckpointingActive = true;
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 {
1340 if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
1341 {
1342 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
1343 AssertRC(rc);
1344 }
1345
1346 if (VM_FF_IS_SET(pVM, VM_FF_REQUEST))
1347 {
1348 rc = VMR3ReqProcessU(pVM->pUVM, VMCPUID_ANY, true /*fPriorityOnly*/);
1349 AssertRC(rc);
1350 }
1351 }
1352 }
1353 else
1354 rc = PDMCritSectEnter(&pVM->ftm.s.CritSect, VERR_SEM_BUSY);
1355
1356 AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
1357
1358 STAM_PROFILE_START(&pVM->ftm.s.StatCheckpoint, a);
1359
1360 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, ftmR3SetCheckpointRendezvous, NULL);
1361
1362 STAM_PROFILE_STOP(&pVM->ftm.s.StatCheckpoint, a);
1363
1364 PDMCritSectLeave(&pVM->ftm.s.CritSect);
1365 pVM->ftm.s.fCheckpointingActive = false;
1366
1367 return rc;
1368}
Note: See TracBrowser for help on using the repository browser.

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