1 | /* $Id: FTM.cpp 31754 2010-08-18 11:31:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * FTM - Fault Tolerance Manager
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 "FTMInternal.h"
|
---|
24 | #include <VBox/vm.h>
|
---|
25 | #include <VBox/vmm.h>
|
---|
26 | #include <VBox/err.h>
|
---|
27 | #include <VBox/param.h>
|
---|
28 | #include <VBox/ssm.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/thread.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/tcp.h>
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Initializes the FTM.
|
---|
39 | *
|
---|
40 | * @returns VBox status code.
|
---|
41 | * @param pVM The VM to operate on.
|
---|
42 | */
|
---|
43 | VMMR3DECL(int) FTMR3Init(PVM pVM)
|
---|
44 | {
|
---|
45 | /** @todo saved state for master nodes! */
|
---|
46 | pVM->ftm.s.pszAddress = NULL;
|
---|
47 | pVM->ftm.s.pszPassword = NULL;
|
---|
48 | pVM->fFaultTolerantMaster = false;
|
---|
49 | pVM->ftm.s.fIsStandbyNode = false;
|
---|
50 | pVM->ftm.s.standby.hServer = NULL;
|
---|
51 | return VINF_SUCCESS;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Terminates the FTM.
|
---|
56 | *
|
---|
57 | * Termination means cleaning up and freeing all resources,
|
---|
58 | * the VM it self is at this point powered off or suspended.
|
---|
59 | *
|
---|
60 | * @returns VBox status code.
|
---|
61 | * @param pVM The VM to operate on.
|
---|
62 | */
|
---|
63 | VMMR3DECL(int) FTMR3Term(PVM pVM)
|
---|
64 | {
|
---|
65 | if (pVM->ftm.s.pszAddress)
|
---|
66 | RTMemFree(pVM->ftm.s.pszAddress);
|
---|
67 | if (pVM->ftm.s.pszPassword)
|
---|
68 | RTMemFree(pVM->ftm.s.pszPassword);
|
---|
69 | if (pVM->ftm.s.standby.hServer)
|
---|
70 | RTTcpServerDestroy(pVM->ftm.s.standby.hServer);
|
---|
71 |
|
---|
72 | return VINF_SUCCESS;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Thread function which starts syncing process for this master VM
|
---|
77 | *
|
---|
78 | * @param Thread The thread id.
|
---|
79 | * @param pvUser Not used
|
---|
80 | * @return VINF_SUCCESS (ignored).
|
---|
81 | *
|
---|
82 | * @note Locks the Console object for writing.
|
---|
83 | */
|
---|
84 | static DECLCALLBACK(int) ftmR3MasterThread(RTTHREAD Thread, void *pvUser)
|
---|
85 | {
|
---|
86 | return VINF_SUCCESS;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Listen for incoming traffic destined for the standby VM.
|
---|
91 | *
|
---|
92 | * @copydoc FNRTTCPSERVE
|
---|
93 | *
|
---|
94 | * @returns VINF_SUCCESS or VERR_TCP_SERVER_STOP.
|
---|
95 | */
|
---|
96 | static DECLCALLBACK(int) ftmR3StandbyServeConnection(RTSOCKET Sock, void *pvUser)
|
---|
97 | {
|
---|
98 | PVM pVM = (PVM)pvUser;
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Disable Nagle.
|
---|
102 | */
|
---|
103 | int rc = RTTcpSetSendCoalescing(Sock, false /*fEnable*/);
|
---|
104 | AssertRC(rc);
|
---|
105 |
|
---|
106 | return VINF_SUCCESS;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Powers on the fault tolerant virtual machine.
|
---|
111 | *
|
---|
112 | * @returns VBox status code.
|
---|
113 | *
|
---|
114 | * @param pVM The VM to power on.
|
---|
115 | * @param fMaster FT master or standby
|
---|
116 | * @param uInterval FT sync interval
|
---|
117 | * @param pszAddress Standby VM address
|
---|
118 | * @param uPort Standby VM port
|
---|
119 | * @param pszPassword FT password (NULL for none)
|
---|
120 | *
|
---|
121 | * @thread Any thread.
|
---|
122 | * @vmstate Created
|
---|
123 | * @vmstateto PoweringOn+Running (master), PoweringOn+Running_FT (standby)
|
---|
124 | */
|
---|
125 | VMMR3DECL(int) FTMR3PowerOn(PVM pVM, bool fMaster, unsigned uInterval, const char *pszAddress, unsigned uPort, const char *pszPassword)
|
---|
126 | {
|
---|
127 | int rc = VINF_SUCCESS;
|
---|
128 |
|
---|
129 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
130 | AssertMsgReturn(enmVMState == VMSTATE_POWERING_ON,
|
---|
131 | ("%s\n", VMR3GetStateName(enmVMState)),
|
---|
132 | VERR_INTERNAL_ERROR_4);
|
---|
133 | AssertReturn(pszAddress, VERR_INVALID_PARAMETER);
|
---|
134 |
|
---|
135 | pVM->ftm.s.uInterval = uInterval;
|
---|
136 | pVM->ftm.s.uPort = uPort;
|
---|
137 | pVM->ftm.s.pszAddress = RTStrDup(pszAddress);
|
---|
138 | if (pszPassword)
|
---|
139 | pVM->ftm.s.pszPassword = RTStrDup(pszPassword);
|
---|
140 | if (fMaster)
|
---|
141 | {
|
---|
142 | rc = RTThreadCreate(NULL, ftmR3MasterThread, NULL,
|
---|
143 | 0, RTTHREADTYPE_IO /* higher than normal priority */, 0, "ftmR3MasterThread");
|
---|
144 | if (RT_FAILURE(rc))
|
---|
145 | return rc;
|
---|
146 |
|
---|
147 | pVM->fFaultTolerantMaster = true;
|
---|
148 | return VMR3PowerOn(pVM);
|
---|
149 | }
|
---|
150 | else
|
---|
151 | {
|
---|
152 | /* standby */
|
---|
153 | rc = RTTcpServerCreateEx(pszAddress, uPort, &pVM->ftm.s.standby.hServer);
|
---|
154 | if (RT_FAILURE(rc))
|
---|
155 | return rc;
|
---|
156 | pVM->ftm.s.fIsStandbyNode = true;
|
---|
157 |
|
---|
158 | rc = RTTcpServerListen(pVM->ftm.s.standby.hServer, ftmR3StandbyServeConnection, pVM);
|
---|
159 | /** @todo deal with the exit code to check if we should activate this standby VM. */
|
---|
160 |
|
---|
161 | RTTcpServerDestroy(pVM->ftm.s.standby.hServer);
|
---|
162 | pVM->ftm.s.standby.hServer = NULL;
|
---|
163 | }
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Powers off the fault tolerant virtual machine (standby).
|
---|
169 | *
|
---|
170 | * @returns VBox status code.
|
---|
171 | *
|
---|
172 | * @param pVM The VM to power on.
|
---|
173 | */
|
---|
174 | VMMR3DECL(int) FTMR3StandbyCancel(PVM pVM)
|
---|
175 | {
|
---|
176 | AssertReturn(!pVM->fFaultTolerantMaster, VERR_NOT_SUPPORTED);
|
---|
177 | Assert(pVM->ftm.s.standby.hServer);
|
---|
178 |
|
---|
179 | return RTTcpServerShutdown(pVM->ftm.s.standby.hServer);
|
---|
180 | }
|
---|