From 3dd205bafc30830e7b89b56e16d435b2583cb3d5 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 31 Oct 2017 23:21:05 +1100 Subject: [PATCH] [host] initial service framework implemented --- common/KVMGFXHeader.h | 3 + host/CaptureFactory.h | 15 +++ host/ICapture.h | 14 +++ host/Service.cpp | 165 ++++++++++++++++++++++++++ host/Service.h | 35 ++++++ host/ivshmem.cpp | 18 +++ host/ivshmem.h | 1 + host/kvm-ivshmem-host.vcxproj | 4 + host/kvm-ivshmem-host.vcxproj.filters | 12 ++ host/main.cpp | 15 ++- 10 files changed, 274 insertions(+), 8 deletions(-) create mode 100644 host/CaptureFactory.h create mode 100644 host/ICapture.h create mode 100644 host/Service.cpp create mode 100644 host/Service.h diff --git a/common/KVMGFXHeader.h b/common/KVMGFXHeader.h index ccc61b21..2da250c4 100644 --- a/common/KVMGFXHeader.h +++ b/common/KVMGFXHeader.h @@ -1,3 +1,4 @@ +#pragma once /* KVMGFX Client - A KVM Client for VGA Passthrough Copyright (C) 2017 Geoffrey McRae @@ -16,6 +17,8 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include + #define KVMGFX_HEADER_MAGIC "[[KVMGFXHeader]]" typedef enum FrameType diff --git a/host/CaptureFactory.h b/host/CaptureFactory.h new file mode 100644 index 00000000..89dff3b5 --- /dev/null +++ b/host/CaptureFactory.h @@ -0,0 +1,15 @@ +#pragma once + +#define W32_LEAN_AND_MEAN +#include + +#include "ICapture.h" + +static class CaptureFactory +{ +public: + static ICapture * GetCaptureDevice() + { + return NULL; + } +}; \ No newline at end of file diff --git a/host/ICapture.h b/host/ICapture.h new file mode 100644 index 00000000..54af9377 --- /dev/null +++ b/host/ICapture.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +__interface ICapture +{ +public: + bool Initialize(); + bool DeInitialize(); + enum FrameType GetFrameType(); + enum FrameComp GetFrameCompression(); + size_t GetMaxFrameSize(); + bool GrabFrame(void * buffer, size_t bufferSize, size_t * outLen); +}; \ No newline at end of file diff --git a/host/Service.cpp b/host/Service.cpp new file mode 100644 index 00000000..125bf65a --- /dev/null +++ b/host/Service.cpp @@ -0,0 +1,165 @@ +#include "service.h" +#include "ivshmem.h" + +#include +#include + +#include "CaptureFactory.h" + +Service * Service::m_instance = NULL; + +Service::Service() : + m_initialized(false), + m_readyEvent(INVALID_HANDLE_VALUE), + m_capture(NULL), + m_memory(NULL) +{ + m_ivshmem = IVSHMEM::Get(); +} + +Service::~Service() +{ +} + +bool Service::Initialize() +{ + if (m_initialized) + DeInitialize(); + + if (!m_ivshmem->Initialize()) + { + DEBUG_ERROR("IVSHMEM failed to initalize"); + DeInitialize(); + return false; + } + + if (m_ivshmem->GetSize() < sizeof(KVMGFXHeader)) + { + DEBUG_ERROR("Shared memory is not large enough for the KVMGFXHeader"); + DeInitialize(); + return false; + } + + m_memory = m_ivshmem->GetMemory(); + if (!m_memory) + { + DEBUG_ERROR("Failed to get IVSHMEM memory"); + DeInitialize(); + return false; + } + + m_readyEvent = m_ivshmem->CreateVectorEvent(0); + if (m_readyEvent == INVALID_HANDLE_VALUE) + { + DEBUG_ERROR("Failed to get event for vector 0"); + DeInitialize(); + return false; + } + + m_capture = CaptureFactory::GetCaptureDevice(); + if (!m_capture || !m_capture->Initialize()) + { + DEBUG_ERROR("Failed to initialize capture interface"); + DeInitialize(); + return false; + } + + KVMGFXHeader * header = static_cast(m_memory); + ZeroMemory(header, sizeof(KVMGFXHeader)); + memcpy(header->magic, KVMGFX_HEADER_MAGIC, sizeof(KVMGFX_HEADER_MAGIC)); + + header->version = 2; + header->guestID = m_ivshmem->GetPeerID(); + + m_initialized = true; + return true; +} + +void Service::DeInitialize() +{ + if (m_capture) + { + m_capture->DeInitialize(); + m_capture = NULL; + } + + if (m_readyEvent != INVALID_HANDLE_VALUE) + CloseHandle(m_readyEvent); + + m_memory = NULL; + m_ivshmem->DeInitialize(); + m_initialized = false; +} + +bool Service::Process(HANDLE stopEvent) +{ + if (!m_initialized) + return false; + + KVMGFXHeader * header = static_cast(m_memory ); + void * data = static_cast(header + 1); + const size_t available = m_ivshmem->GetSize() - sizeof(KVMGFXHeader); + if (m_capture->GetMaxFrameSize() > available) + { + DEBUG_ERROR("Frame could exceed buffer size!"); + return false; + } + + // setup the header + header->frameType = m_capture->GetFrameType(); + header->compType = m_capture->GetFrameCompression(); + + // capture a frame of data + if (!m_capture->GrabFrame( + data, + m_ivshmem->GetSize() - sizeof(KVMGFXHeader), + &header->dataLen)) + { + DEBUG_ERROR("Capture failed"); + return false; + } + + // tell the host where the cursor is + POINT cursorPos; + GetCursorPos(&cursorPos); + header->mouseX = cursorPos.x; + header->mouseY = cursorPos.y; + + // wait for the host to notify that is it is ready to proceed + ResetEvent(m_readyEvent); + while( + stopEvent == INVALID_HANDLE_VALUE || + (WaitForSingleObject(stopEvent, 0) == WAIT_OBJECT_0) + ) + { + if (!m_ivshmem->RingDoorbell(header->hostID, 0)) + { + DEBUG_ERROR("Failed to ring doorbell"); + return false; + } + + switch (WaitForSingleObject(m_readyEvent, 1000)) + { + case WAIT_ABANDONED: + DEBUG_ERROR("Wait abandoned"); + return false; + + case WAIT_OBJECT_0: + return true; + + // if we timed out we just continue to ring until we get an answer or we are stopped + case WAIT_TIMEOUT: + break; + + case WAIT_FAILED: + DEBUG_ERROR("Wait failed"); + return false; + + default: + DEBUG_ERROR("Unknown error"); + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/host/Service.h b/host/Service.h new file mode 100644 index 00000000..adbdeb24 --- /dev/null +++ b/host/Service.h @@ -0,0 +1,35 @@ +#pragma once + +#define W32_LEAN_AND_MEAN +#include +#include + +#include "ivshmem.h" +#include "ICapture.h" + +class Service +{ +public: + static Service * Get() + { + if (!m_instance) + m_instance = new Service(); + return m_instance; + } + + bool Initialize(); + void DeInitialize(); + bool Process(HANDLE stopEvent); + +private: + static Service * m_instance; + + Service(); + ~Service(); + + bool m_initialized; + IVSHMEM * m_ivshmem; + HANDLE m_readyEvent; + ICapture * m_capture; + void * m_memory; +}; \ No newline at end of file diff --git a/host/ivshmem.cpp b/host/ivshmem.cpp index b6d81724..acbc6e72 100644 --- a/host/ivshmem.cpp +++ b/host/ivshmem.cpp @@ -216,4 +216,22 @@ HANDLE IVSHMEM::CreateVectorEvent(UINT16 vector) } return event; +} + +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", GetLastError()); + return false; + } + + return true; } \ No newline at end of file diff --git a/host/ivshmem.h b/host/ivshmem.h index aa1e683a..aabe015d 100644 --- a/host/ivshmem.h +++ b/host/ivshmem.h @@ -23,6 +23,7 @@ public: UINT16 GetVectors(); void * GetMemory(); HANDLE CreateVectorEvent(UINT16 vector); + bool RingDoorbell(UINT16 peerID, UINT16 door); protected: diff --git a/host/kvm-ivshmem-host.vcxproj b/host/kvm-ivshmem-host.vcxproj index 7f09f611..53a210c5 100644 --- a/host/kvm-ivshmem-host.vcxproj +++ b/host/kvm-ivshmem-host.vcxproj @@ -154,9 +154,13 @@ + + + + diff --git a/host/kvm-ivshmem-host.vcxproj.filters b/host/kvm-ivshmem-host.vcxproj.filters index 77e02d12..9221acdf 100644 --- a/host/kvm-ivshmem-host.vcxproj.filters +++ b/host/kvm-ivshmem-host.vcxproj.filters @@ -21,10 +21,22 @@ Source Files + + Source Files + Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/host/main.cpp b/host/main.cpp index 00ce20c7..de6df86b 100644 --- a/host/main.cpp +++ b/host/main.cpp @@ -2,7 +2,7 @@ #include #include -#include "ivshmem.h" +#include "Service.h" #define SERVICE_NAME "kvm-ivshmem-host" @@ -146,19 +146,18 @@ VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode) DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) { - IVSHMEM * ivshmem = IVSHMEM::Get(); - if (!ivshmem->Initialize()) + Service *svc = svc->Get(); + if (!svc->Initialize()) { - DEBUG_ERROR("Failed to initialize IVSHMEM"); + DEBUG_ERROR("Failed to initialize service"); return ERROR; } while (WaitForSingleObject(app.serviceStopEvent, 0) != WAIT_OBJECT_0) - { - Sleep(1000); - } + if (!svc->Process(app.serviceStopEvent)) + break; - ivshmem->DeInitialize(); + svc->DeInitialize(); return ERROR_SUCCESS; }