forked from cagnulein/ivshmem-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IVSHMEM.cpp
290 lines (238 loc) · 6.86 KB
/
IVSHMEM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2019 Geoffrey McRae <[email protected]>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <windows.h>
#include <SetupAPI.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "IVSHMEM.h"
#include "debug.h"
IVSHMEM * IVSHMEM::m_instance = NULL;
IVSHMEM::IVSHMEM() :
m_initialized(false),
m_handle(INVALID_HANDLE_VALUE),
m_gotSize(false),
m_gotPeerID(false),
m_gotMemory(false)
{
}
IVSHMEM::~IVSHMEM()
{
DeInitialize();
}
bool IVSHMEM::Initialize()
{
if (m_initialized)
DeInitialize();
HDEVINFO deviceInfoSet;
PSP_DEVICE_INTERFACE_DETAIL_DATA infData = NULL;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
deviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE);
ZeroMemory(&deviceInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
while (true)
{
if (SetupDiEnumDeviceInterfaces(deviceInfoSet, NULL, &GUID_DEVINTERFACE_IVSHMEM, 0, &deviceInterfaceData) == FALSE)
{
DWORD error = GetLastError();
if (error == ERROR_NO_MORE_ITEMS)
{
DEBUG_ERROR("Unable to enumerate the device, is it attached?");
break;
}
DEBUG_ERROR("SetupDiEnumDeviceInterfaces failed");
break;
}
DWORD reqSize = 0;
SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &reqSize, NULL);
if (!reqSize)
{
DEBUG_ERROR("SetupDiGetDeviceInterfaceDetail");
break;
}
infData = static_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(malloc(reqSize));
ZeroMemory(infData, reqSize);
infData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, infData, reqSize, NULL, NULL))
{
DEBUG_ERROR("SetupDiGetDeviceInterfaceDetail");
break;
}
m_handle = CreateFile(infData->DevicePath, 0, 0, NULL, OPEN_EXISTING, 0, 0);
if (m_handle == INVALID_HANDLE_VALUE)
{
DEBUG_ERROR("CreateFile returned INVALID_HANDLE_VALUE");
break;
}
m_initialized = true;
break;
}
if (infData)
free(infData);
SetupDiDestroyDeviceInfoList(deviceInfoSet);
return m_initialized;
}
void IVSHMEM::DeInitialize()
{
if (!m_initialized)
return;
if (m_gotMemory)
{
if (!DeviceIoControl(m_handle, IOCTL_IVSHMEM_RELEASE_MMAP, NULL, 0, NULL, 0, NULL, NULL))
DEBUG_ERROR("DeviceIoControl failed: %d", (int)GetLastError());
m_memory = NULL;
}
if (m_handle != INVALID_HANDLE_VALUE)
CloseHandle(m_handle);
m_initialized = false;
m_handle = INVALID_HANDLE_VALUE;
m_gotSize = false;
m_gotPeerID = false;
m_gotVectors = false;
m_gotMemory = false;
}
bool IVSHMEM::IsInitialized()
{
return m_initialized;
}
UINT64 IVSHMEM::GetSize()
{
if (!m_initialized)
return 0;
if (m_gotSize)
return m_size;
IVSHMEM_SIZE size;
if (!DeviceIoControl(m_handle, IOCTL_IVSHMEM_REQUEST_SIZE, NULL, 0, &size, sizeof(IVSHMEM_SIZE), NULL, NULL))
{
DEBUG_ERROR("DeviceIoControl Failed: %d", (int)GetLastError());
return 0;
}
m_gotSize = true;
m_size = static_cast<UINT64>(size);
return m_size;
}
UINT16 IVSHMEM::GetPeerID()
{
if (!m_initialized)
return 0;
if (m_gotPeerID)
return m_peerID;
IVSHMEM_PEERID peerID;
if (!DeviceIoControl(m_handle, IOCTL_IVSHMEM_REQUEST_SIZE, NULL, 0, &peerID, sizeof(IVSHMEM_PEERID), NULL, NULL))
{
DEBUG_ERROR("DeviceIoControl Failed: %d", (int)GetLastError());
return 0;
}
m_gotPeerID = true;
m_peerID = static_cast<UINT16>(peerID);
return m_peerID;
}
UINT16 IVSHMEM::GetVectors()
{
if (!m_initialized)
return 0;
if (!m_gotVectors)
return 0;
return m_vectors;
}
void * IVSHMEM::GetMemory()
{
if (!m_initialized)
return NULL;
if (m_gotMemory)
return m_memory;
// this if define can be removed later once everyone is un the latest version
// old versions of the IVSHMEM driver ignore the input argument, as such this
// is completely backwards compatible
#if defined(IVSHMEM_CACHE_WRITECOMBINED)
IVSHMEM_MMAP_CONFIG config;
config.cacheMode = IVSHMEM_CACHE_WRITECOMBINED;
#endif
IVSHMEM_MMAP map;
ZeroMemory(&map, sizeof(IVSHMEM_MMAP));
if (!DeviceIoControl(
m_handle,
IOCTL_IVSHMEM_REQUEST_MMAP,
#if defined(IVSHMEM_CACHE_WRITECOMBINED)
&config, sizeof(IVSHMEM_MMAP_CONFIG),
#else
NULL , 0,
#endif
&map , sizeof(IVSHMEM_MMAP ),
NULL, NULL))
{
DEBUG_ERROR("DeviceIoControl Failed: %d", (int)GetLastError());
return NULL;
}
m_gotSize = true;
m_gotPeerID = true;
m_gotMemory = true;
m_gotVectors = true;
m_size = static_cast<UINT64>(map.size );
m_peerID = static_cast<UINT16>(map.peerID );
m_vectors = static_cast<UINT16>(map.vectors);
m_memory = map.ptr;
return m_memory;
}
waitEvent* IVSHMEM::CreateVectorEvent(UINT16 vector)
{
if (!m_initialized)
return nullptr;
HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (event == INVALID_HANDLE_VALUE)
{
DEBUG_ERROR("CreateEvent Failed: %d", (int)GetLastError());
return nullptr;
}
IVSHMEM_EVENT msg;
msg.event = event;
msg.singleShot = false;
msg.vector = vector;
if (!DeviceIoControl(m_handle, IOCTL_IVSHMEM_REGISTER_EVENT, &msg, sizeof(IVSHMEM_EVENT), NULL, 0, NULL, NULL))
{
DEBUG_ERROR("DeviceIoControl Failed: %d", (int)GetLastError());
CloseHandle(event);
return nullptr;
}
if(waitVector.size() <= vector || waitVector.at(vector) == nullptr)
{
waitVector.insert(vector, new waitEvent(vector, event));
}
if(!waitVector.at(vector)->isRunning())
{
waitVector.at(vector)->event = event;
waitVector.at(vector)->start();
}
return waitVector.at(vector);
}
bool IVSHMEM::RingDoorbell(UINT16 peerID, UINT16 door)
{
if (!m_initialized)
return false;
IVSHMEM_RING msg;
msg.peerID = peerID;
msg.vector = door;
if (!DeviceIoControl(m_handle, IOCTL_IVSHMEM_RING_DOORBELL, &msg, sizeof(IVSHMEM_RING), NULL, 0, NULL, NULL))
{
DEBUG_ERROR("DeviceIoControl Failed: %d", (int)GetLastError());
return false;
}
return true;
}
HANDLE IVSHMEM::getHandle()
{
return m_handle;
}