Skip to content
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0fabed9
HCS/HCN API wrapper implementation [wip]
xmkg Jan 30, 2025
7232028
[hyperv-hcs-hcn] Remove the WIL dependency
xmkg Feb 3, 2025
fd2800d
[hyperv-hcs]: Add unit tests for the HCSWrapper
xmkg Feb 11, 2025
51eeeb8
[hyperv-hcs]: add logging assertions to the unit test cases
xmkg Feb 12, 2025
c02a402
[hyperv-hcn-hcs] overall grooming & polishing
xmkg Feb 12, 2025
4e31987
[hyperv-hcs] add update_cpu_count to API
xmkg Feb 14, 2025
feb1b26
[hyperv-virtdisk] virtdisk api wrapper implementation
xmkg Feb 14, 2025
f27fb00
[hyperv] fix EOF newline
xmkg Feb 14, 2025
1d88683
[hyperv-hcn] remove the redundant "PortName" from endpoint JSON
xmkg Feb 17, 2025
83c7209
[hyperv-virtdisk] added unit tests
xmkg Feb 17, 2025
4258184
[hyperv-hcn] removed firewall vm-creator-id parameter
xmkg Feb 17, 2025
a064155
[hyperv-virtdisk] verify logs in unit tests
xmkg Feb 17, 2025
de8aaac
[hyperv-virtdisk] fix header guard name for virtdisk_api_wrapper.h
xmkg Feb 17, 2025
04e1da1
[hyperv-api-tests] add initial component integration tests
xmkg Feb 17, 2025
00d345c
[hyperv-api-table] do not rely on rtti for printing api function bind…
xmkg Feb 18, 2025
1deb854
[hyperv-api] more grooming
xmkg Feb 18, 2025
abb1b62
[hyperv-api] switch to the format overload of mpl::log
xmkg Mar 4, 2025
aed777f
[hyperv-api] even more grooming
xmkg Mar 4, 2025
69c9700
[hyperv-api] set all trace log levels to debug
xmkg Mar 7, 2025
525d6ba
[hyperv-api] switch to log-level functions
xmkg Mar 11, 2025
3497b6c
[hyperv-api] link required libraries via CMake
xmkg Mar 18, 2025
2597a3f
[hyperv-api] review changes
xmkg Mar 18, 2025
6ecb885
[hyperv-virtdisk] code review changes for the unit tests
xmkg Mar 18, 2025
b705973
[hyperv-api-common] guid_from_wstring: switch to CLSIDFromString
xmkg Mar 18, 2025
f473102
[hyperv-cmake] update fmt reference with fmt::fmt-header-only
xmkg May 8, 2025
c02b482
[hyperv-hcs] `constexpr` -> `constexpr static` to make msvc 19 happy
xmkg Jul 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ src/client/gui/flutter*.log
packaging/windows/wix/obj/*
packaging/windows/custom-actions/packages/*
packaging/windows/custom-actions/x64/*

# clangd cache path
.cache/
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ if(MSVC)
add_definitions(-DLIBSSH_STATIC) # otherwise adds declspec specifiers to libssh apis
add_definitions(-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS)
add_definitions(-DWIN32_LEAN_AND_MEAN)
set(MULTIPASS_BACKENDS hyperv virtualbox)
set(MULTIPASS_BACKENDS hyperv hyperv_api virtualbox)
set(MULTIPASS_PLATFORM windows)
else()
add_compile_options(-Werror -Wall -pedantic -fPIC -Wno-error=deprecated-declarations)
Expand Down
57 changes: 57 additions & 0 deletions src/platform/backends/hyperv_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (C) Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# 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, see <http://www.gnu.org/licenses/>.
#

if(WIN32)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary if we are already selecting linked libraries by platform in src/platform/CMakeLists.txt?


include(CheckCXXSourceRuns)

macro(check_pragma_lib LIB_NAME HEADER_NAME OUT_VAR)
check_cxx_source_runs("
#pragma comment(lib, \"${LIB_NAME}\")
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <${HEADER_NAME}>
int main(void){ return 0; }
" ${OUT_VAR})
endmacro()

check_pragma_lib("computecore.lib" "computecore.h" HAS_COMPUTECORE)
check_pragma_lib("computenetwork.lib" "computenetwork.h" HAS_COMPUTENETWORK)
check_pragma_lib("virtdisk.lib" "virtdisk.h" HAS_VIRTDISK)

if(NOT (HAS_COMPUTECORE AND HAS_COMPUTENETWORK AND HAS_VIRTDISK))
message(FATAL_ERROR
"[hyperv_api] One or more required libraries are missing:\n"
" HAS_COMPUTECORE_LIB=${HAS_COMPUTECORE_LIB}\n"
" HAS_COMPUTENETWORK_LIB=${HAS_COMPUTENETWORK_LIB}\n"
" HAS_VIRTDISK_LIB=${HAS_VIRTDISK_LIB}\n"
)
endif()

add_library(hyperv_api_backend STATIC
hyperv_api_common.cpp
hcn/hyperv_hcn_api_wrapper.cpp
hcs/hyperv_hcs_api_wrapper.cpp
virtdisk/virtdisk_api_wrapper.cpp
)

target_link_libraries(hyperv_api_backend PRIVATE
fmt::fmt-header-only
utils
computecore.lib
computenetwork.lib
virtdisk.lib
)
endif()
88 changes: 88 additions & 0 deletions src/platform/backends/hyperv_api/hcn/hyperv_hcn_api_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) Canonical, Ltd.
*
* 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; version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef MULTIPASS_HYPERV_API_HCN_API_TABLE
#define MULTIPASS_HYPERV_API_HCN_API_TABLE

// clang-format off
// (xmkg): clang-format is messing with the include order.
#include <windows.h>
#include <computenetwork.h>
#include <objbase.h> // for CoTaskMemFree
// clang-format on

#include <fmt/format.h>
#include <functional>

namespace multipass::hyperv::hcn
{

/**
* API function table for the Host Compute Network API
*/
struct HCNAPITable
{
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcncreatenetwork
std::function<decltype(HcnCreateNetwork)> CreateNetwork = &HcnCreateNetwork;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcnopennetwork
std::function<decltype(HcnOpenNetwork)> OpenNetwork = &HcnOpenNetwork;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcndeletenetwork
std::function<decltype(HcnDeleteNetwork)> DeleteNetwork = &HcnDeleteNetwork;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcnclosenetwork
std::function<decltype(HcnCloseNetwork)> CloseNetwork = &HcnCloseNetwork;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcncreateendpoint
std::function<decltype(HcnCreateEndpoint)> CreateEndpoint = &HcnCreateEndpoint;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcnopenendpoint
std::function<decltype(HcnOpenEndpoint)> OpenEndpoint = &HcnOpenEndpoint;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcndeleteendpoint
std::function<decltype(HcnDeleteEndpoint)> DeleteEndpoint = &HcnDeleteEndpoint;
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcndeleteendpoint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcndeleteendpoint
// @ref https://learn.microsoft.com/en-us/virtualization/api/hcn/reference/hcncloseendpoint

std::function<decltype(HcnCloseEndpoint)> CloseEndpoint = &HcnCloseEndpoint;
// @ref https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemfree
std::function<decltype(::CoTaskMemFree)> CoTaskMemFree = &::CoTaskMemFree;
};

} // namespace multipass::hyperv::hcn

/**
* Formatter type specialization for HCNAPITable
*/
template <typename Char>
struct fmt::formatter<multipass::hyperv::hcn::HCNAPITable, Char>
{
constexpr auto parse(basic_format_parse_context<Char>& ctx)
{
return ctx.begin();
}

template <typename FormatContext>
auto format(const multipass::hyperv::hcn::HCNAPITable& api, FormatContext& ctx) const
{
return format_to(ctx.out(),
"CreateNetwork: ({}) | OpenNetwork: ({}) | DeleteNetwork: ({}) | CreateEndpoint: ({}) | "
"OpenEndpoint: ({}) | DeleteEndpoint: ({}) | CoTaskMemFree: ({})",
static_cast<bool>(api.CreateNetwork),
static_cast<bool>(api.OpenNetwork),
static_cast<bool>(api.DeleteNetwork),
static_cast<bool>(api.CreateEndpoint),
static_cast<bool>(api.OpenEndpoint),
static_cast<bool>(api.DeleteEndpoint),
static_cast<bool>(api.CoTaskMemFree));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing api.CloseEndpoint and api.CloseNetwork, is this intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sharp eyes :) I'll add them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid rebasing, I'll make the changes in the succeeding PR.

}
};

#endif // MULTIPASS_HYPERV_API_HCN_API_TABLE
Loading
Loading