Skip to content

Commit 721cc2b

Browse files
committed
Merge user/damanmulye/hcs-fs-devices into user/damanmulye/hcs-integration
1 parent 3deec7c commit 721cc2b

12 files changed

Lines changed: 414 additions & 364 deletions

‎src/windows/common/HcsVirtualMachineBackend.cpp‎

Lines changed: 193 additions & 137 deletions
Large diffs are not rendered by default.

‎src/windows/common/HcsVirtualMachineBackend.h‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
4343
void Terminate() override;
4444

4545
VmGuestListener CreateGuestListener(GuestServicePort Port) override;
46-
wil::unique_socket AcceptGuestConnection(VmListenerId Listener) override;
4746
wil::unique_socket ConnectGuest(GuestServicePort Port) override;
4847
void CloseGuestListener(VmListenerId Listener) override;
4948

@@ -151,12 +150,20 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
151150
/// Returns the share of Device that already serves HostPath with MountOptions, if there is one.
152151
/// </summary>
153152
_Requires_lock_held_(m_lock)
154-
const FileSystemShare* FindFileSystemShareLocked(VmDeviceId Device, const std::wstring& HostPath, const std::wstring& MountOptions) const;
153+
const FileSystemShare* FindFileSystemShareLocked(
154+
VmDeviceId Device, const std::wstring& HostPath, const std::wstring& MountOptions, const std::wstring& Name) const;
155155

156156
/// <summary>
157-
/// Returns shared ownership of the token used to reach the host path, preferring the request's token.
157+
/// Returns the Plan 9 share of Device with Name, if there is one.
158158
/// </summary>
159-
wil::shared_handle ResolveShareUserToken(const FileSystemDevice& Device, const VmFileSystemShareRequest& Request) const;
159+
_Requires_lock_held_(m_lock)
160+
std::optional<VmFileSystemShare> FindPlan9ShareByNameLocked(VmDeviceId Device, const std::wstring& Name) const;
161+
162+
/// <summary>
163+
/// Resolves the token used to reach a host path, returning the first of Tokens that is set and
164+
/// falling back to the identity that created the VM. Callers list Tokens most specific first.
165+
/// </summary>
166+
wil::shared_handle ResolveUserToken(std::initializer_list<std::reference_wrapper<const wil::shared_handle>> Tokens) const;
160167

161168
/// <summary>
162169
/// Adds a share to a Plan 9 device and returns the name the guest uses to reach it.
@@ -181,13 +188,6 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
181188
_Requires_lock_held_(m_lock)
182189
wil::com_ptr<IWslVirtioNetDevice> GetUserModeNatDeviceLocked(VmDeviceId Device) const;
183190

184-
/// <summary>
185-
/// Adds or removes a host endpoint adapter at ResourcePath. HCS reports transient failures while
186-
/// the host network stack settles, so the modification is retried.
187-
/// </summary>
188-
_Requires_lock_held_(m_lock)
189-
void ModifyHostEndpointLocked(const VmHostEndpointNetwork& Configuration, const std::wstring& ResourcePath, wsl::windows::common::hcs::ModifyRequestType RequestType) const;
190-
191191
/// <summary>
192192
/// Removes the adapter's tracked state, tearing down the resource that serves it. Port bindings
193193
/// on the adapter are dropped because the device that tracked them is gone.
@@ -232,8 +232,6 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
232232
_Guarded_by_(m_lock) std::map<std::uint64_t, NetworkAdapter> m_networkAdapters;
233233
_Guarded_by_(m_lock) std::map<std::uint64_t, VmPortBinding> m_portBindings;
234234
_Guarded_by_(m_lock) std::uint64_t m_nextPortBindingId = 1;
235-
wil::unique_handle m_restrictedToken;
236-
237235
_Guarded_by_(m_lock) std::shared_ptr<GuestDeviceManager> m_guestDeviceManager;
238236
_Guarded_by_(m_lock) GUID m_runtimeId {};
239237
};

‎src/windows/common/IVirtualMachineBackend.h‎

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ struct VmInstanceId
5959
wil::shared_handle UserToken{};
6060
};
6161

62+
struct VmGuestListenerState;
63+
6264
template <typename Tag>
6365
struct VmResourceId
6466
{
@@ -107,83 +109,31 @@ struct VmRequestedValue
107109

108110
enum class VmFeature
109111
{
110-
LinuxDirectBoot,
111-
LinuxFirmwareBoot,
112112
NestedVirtualization,
113113
PerfmonPmu,
114114
PerfmonLbr,
115115
SmallPageMemory,
116-
MemoryOvercommit,
117116
DeferredMemoryCommit,
118117
ColdDiscard,
118+
PhysicalDisk,
119119
SerialConsole,
120120
VirtioConsole,
121-
Vhd,
122-
Vhdx,
123-
PhysicalDisk,
124-
PersistentMemory,
125-
Plan9Socket,
126-
Plan9Virtio,
127121
VirtioFsFileBacked,
128-
VirtioFsAggregate,
129-
SectionBackedSharedMemory,
130-
MirroredGpu,
131-
GpuVendorExtension,
132-
GpuDisableGdiAcceleration,
133-
GpuDisablePresentation,
134122
SavedStateOnCrash,
135-
GuestDmaWindow,
136-
HostEndpointNetwork,
137123
UserModeNatNetwork,
138124
TcpPortBinding,
139125
UdpPortBinding,
140126
Ipv6PortBinding,
141127
ScopedIpv6PortBinding,
142-
DynamicHostPort,
143-
VirtualHostAddress,
144-
StaticDnsARecord,
145128
Count
146129
};
147130

148-
static_assert(static_cast<size_t>(VmFeature::Count) == 35);
149-
150-
enum class VmOperation
151-
{
152-
Create,
153-
Start,
154-
Terminate,
155-
CreateGuestListener,
156-
AcceptGuestConnection,
157-
ConnectGuest,
158-
CloseGuestListener,
159-
AttachDisk,
160-
DetachDisk,
161-
AddPersistentMemory,
162-
CreateFileSystemDevice,
163-
AddFileSystemShare,
164-
RemoveFileSystemShare,
165-
GetFileSystemDeviceStatus,
166-
AddGpu,
167-
AddSharedMemory,
168-
ConfigureGuestDma,
169-
RemoveDevice,
170-
AddNetworkAdapter,
171-
UpdateNetworkAdapter,
172-
RemoveNetworkAdapter,
173-
BindPort,
174-
UnbindPort,
175-
CreateVirtualAddress,
176-
CreateDnsRecord,
177-
Count
178-
};
179-
180-
static_assert(static_cast<size_t>(VmOperation::Count) == 25);
131+
static_assert(static_cast<size_t>(VmFeature::Count) == 16);
181132

182133
struct VmPlatformCapabilities
183134
{
184135
BackendKind Backend;
185136
std::bitset<static_cast<size_t>(VmFeature::Count)> Features;
186-
std::bitset<static_cast<size_t>(VmOperation::Count)> Operations;
187137
};
188138

189139
enum class VmState
@@ -203,6 +153,9 @@ struct VmGuestListener
203153
{
204154
VmListenerId Id;
205155
GuestServicePort Port;
156+
std::shared_ptr<VmGuestListenerState> State;
157+
158+
wil::unique_socket Accept() const;
206159
};
207160

208161
struct VmGuestListenerState
@@ -235,6 +188,9 @@ struct VmMemoryRequest
235188
VmFeatureRequest SmallPageBacking = VmFeatureRequest::Disabled;
236189
std::optional<std::uint32_t> FaultClusterSizeShift;
237190
std::optional<std::uint32_t> DirectMapFaultClusterSizeShift;
191+
// Order of the page blocks the guest reports back to the host. Reporting blocks smaller than a
192+
// fault cluster hands back memory the next fault immediately reclaims, so this must be at least
193+
// as large as the fault cluster size shifts above.
238194
std::optional<std::uint32_t> PageReportingOrder;
239195
std::optional<std::wstring> HostingProcessNameSuffix;
240196
};
@@ -493,14 +449,20 @@ constexpr std::size_t c_maxVirtioFsTagLength = 36;
493449
struct VmVirtioFsShareOptions
494450
{
495451
std::map<std::wstring, std::wstring> MountOptions;
452+
// Identity the virtio-fs device reaches its host paths through. A virtio-fs device serves every
453+
// path it exposes through a single identity, so an aggregate device declares it here rather than
454+
// inheriting it from the first share; callers that need a second identity create a second
455+
// device. The VM identity token is used when this is unset.
456+
wil::shared_handle UserToken{};
496457
};
497458

498459
struct VmVirtioFsDevice
499460
{
500461
std::wstring Tag;
501462
VmVirtioFsLayout Layout = VmVirtioFsLayout::Aggregate;
502-
// Mount options applied to the device itself. Shares of an aggregate device carry their own
503-
// options; a single-share device inherits the options of the share that it serves.
463+
// Options applied to the device itself, including the identity it serves through. Shares of an
464+
// aggregate device carry their own mount options; a single-share device inherits the options of
465+
// the share that it serves, so it must leave these unset.
504466
VmVirtioFsShareOptions Options;
505467
};
506468

@@ -711,7 +673,6 @@ class IVirtualMachineBackend
711673
void RegisterTerminationCallback(TerminationCallback Callback);
712674

713675
virtual VmGuestListener CreateGuestListener(GuestServicePort Port) = 0;
714-
virtual wil::unique_socket AcceptGuestConnection(VmListenerId Listener) = 0;
715676
virtual wil::unique_socket ConnectGuest(GuestServicePort Port) = 0;
716677
virtual void CloseGuestListener(VmListenerId Listener) = 0;
717678

@@ -753,7 +714,6 @@ class IVirtualMachineBackend
753714
// These helpers require m_lock to be held exclusively. ConfigureGuestListener runs while
754715
// m_lock is held and must not re-enter another listener helper.
755716
VmGuestListener RegisterGuestListenerLocked(const VmInstanceId& Identity, GuestServicePort Port);
756-
wil::unique_socket AcceptGuestListenerConnection(VmListenerId Listener, const VmInstanceId& Identity) const;
757717
std::shared_ptr<VmGuestListenerState> RemoveGuestListenerLocked(VmListenerId Listener, const VmInstanceId& Identity);
758718
void CloseGuestListenersLocked(const VmInstanceId& Identity) noexcept;
759719
void NotifyTerminated(const VmInstanceId& Identity) noexcept;

‎src/windows/common/NatNetworking.cpp‎

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -365,42 +365,15 @@ void NatNetworking::AttachEndpoint(wsl::core::networking::EphemeralHcnEndpoint&&
365365
// for mirrored endpoints, we will set the InstanceId to the InterfaceGuid of the host interface we mirror - as we add &
366366
// remove them dynamically for NAT endpoints, we will just set the InstanceId to the EndpointId
367367

368-
ModifySettingRequest<NetworkAdapter> networkRequest{};
369-
networkRequest.ResourcePath = networking::c_networkAdapterPrefix + wsl::shared::string::GuidToString<wchar_t>(properties.ID);
370-
networkRequest.RequestType = ModifyRequestType::Add;
371-
networkRequest.Settings.EndpointId = properties.ID;
372-
networkRequest.Settings.InstanceId = properties.ID;
373-
374-
networkRequest.Settings.MacAddress = wsl::shared::string::ParseMacAddress(properties.MacAddress);
375-
auto retryCount = 0ul;
376-
const auto hr = wsl::shared::retry::RetryWithTimeout<HRESULT>(
377-
[&] {
378-
HRESULT exceptionHr = wil::ResultFromException(
379-
[&] { wsl::windows::common::hcs::ModifyComputeSystem(m_system, wsl::shared::ToJsonW(networkRequest).c_str()); });
380-
381-
WSL_LOG(
382-
"NatNetworking::AttachEndpoint [ModifyComputeSystem(ModifyRequestType::Add)]",
383-
TraceLoggingValue(properties.ID, "endpointId"),
384-
TraceLoggingValue(exceptionHr, "hr"),
385-
TraceLoggingValue(retryCount, "retryCount"));
386-
387-
++retryCount;
388-
return THROW_IF_FAILED(exceptionHr);
389-
},
390-
wsl::core::networking::AddEndpointRetryPeriod,
391-
wsl::core::networking::AddEndpointRetryTimeout,
392-
wsl::core::networking::AddEndpointRetryPredicate);
393-
394-
if (hr == HCN_E_ENDPOINT_ALREADY_ATTACHED)
395-
{
396-
WSL_LOG(
397-
"NatNetworking::AttachEndpoint [Adding the endpoint returned HCN_E_ENDPOINT_ALREADY_ATTACHED - continuing]",
398-
TraceLoggingValue(properties.ID, "endpointId"));
399-
}
400-
else if (FAILED(hr))
401-
{
402-
THROW_HR(hr);
403-
}
368+
const auto resourcePath = networking::c_networkAdapterPrefix + wsl::shared::string::GuidToString<wchar_t>(properties.ID);
369+
wsl::windows::common::hcs::ModifyNetworkAdapter(
370+
m_system,
371+
resourcePath.c_str(),
372+
ModifyRequestType::Add,
373+
properties.ID,
374+
properties.ID,
375+
wsl::shared::string::ParseMacAddress(properties.MacAddress),
376+
true);
404377

405378
m_endpoint = std::move(endpoint);
406379
m_networkSettings = GetEndpointSettings(properties);

‎src/windows/common/OpenVmmVirtualMachineBackend.cpp‎

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ void OpenVmmVirtualMachineBackend::Initialize(const VmCreateRequest& Request)
374374
THROW_LAST_ERROR_IF(result == WAIT_FAILED);
375375
THROW_HR_WITH_USER_ERROR_IF(WSL_E_VM_CRASHED, wsl::shared::Localization::MessageWSL2Crashed(), result == WAIT_OBJECT_0);
376376
THROW_IF_FAILED_MSG(createResult, "Failed to create OpenVMM VM");
377-
m_state = VmState::Created;
377+
{
378+
auto lock = m_lock.lock_exclusive();
379+
m_state = VmState::Created;
380+
}
378381
}
379382

380383
void OpenVmmVirtualMachineBackend::ReadProcessLog(wil::unique_hfile Pipe) noexcept
@@ -402,47 +405,23 @@ void OpenVmmVirtualMachineBackend::OnProcessExit(DWORD ExitCode) noexcept
402405
{
403406
WSL_LOG(
404407
"OpenVmmProcessExited", TraceLoggingValue(m_description.Identity.VmId, "vmId"), TraceLoggingValue(ExitCode, "exitCode"));
405-
LOG_IF_WIN32_BOOL_FALSE(SetEvent(m_exitEvent.get()));
406408
{
407409
auto lock = m_lock.lock_exclusive();
408410
m_state = VmState::Stopped;
409411
m_exitDetails = std::format(L"OpenVMM process exited with code {}", ExitCode);
410412
CloseGuestListenersLocked(m_description.Identity);
411413
}
414+
LOG_IF_WIN32_BOOL_FALSE(SetEvent(m_exitEvent.get()));
412415
NotifyTerminated(m_description.Identity);
413416
}
414417

415418
VmPlatformCapabilities OpenVmmVirtualMachineBackend::QueryCapabilities()
416419
{
417420
VmPlatformCapabilities capabilities;
418421
capabilities.Backend = BackendKind::OpenVmm;
419-
// Report known OpenVMM support independently of which backend methods are wired through the C ABI.
420-
for (const auto operation :
421-
{VmOperation::Create,
422-
VmOperation::Start,
423-
VmOperation::Terminate,
424-
VmOperation::CreateGuestListener,
425-
VmOperation::AcceptGuestConnection,
426-
VmOperation::ConnectGuest,
427-
VmOperation::CloseGuestListener,
428-
VmOperation::AttachDisk,
429-
VmOperation::DetachDisk,
430-
VmOperation::CreateFileSystemDevice,
431-
VmOperation::GetFileSystemDeviceStatus,
432-
VmOperation::AddFileSystemShare,
433-
VmOperation::RemoveFileSystemShare,
434-
VmOperation::RemoveDevice,
435-
VmOperation::BindPort,
436-
VmOperation::UnbindPort})
437-
{
438-
capabilities.Operations.set(static_cast<size_t>(operation));
439-
}
440422
for (const auto feature :
441-
{VmFeature::LinuxDirectBoot,
442-
VmFeature::SerialConsole,
423+
{VmFeature::SerialConsole,
443424
VmFeature::VirtioConsole,
444-
VmFeature::Vhd,
445-
VmFeature::Vhdx,
446425
VmFeature::VirtioFsFileBacked,
447426
VmFeature::UserModeNatNetwork,
448427
VmFeature::TcpPortBinding,
@@ -597,11 +576,6 @@ VmGuestListener OpenVmmVirtualMachineBackend::CreateGuestListener(GuestServicePo
597576
return RegisterGuestListenerLocked(m_description.Identity, Port);
598577
}
599578

600-
wil::unique_socket OpenVmmVirtualMachineBackend::AcceptGuestConnection(VmListenerId Listener)
601-
{
602-
return AcceptGuestListenerConnection(Listener, m_description.Identity);
603-
}
604-
605579
wil::unique_socket OpenVmmVirtualMachineBackend::ConnectGuest(GuestServicePort Port)
606580
{
607581
const auto startTimeMs = GetTickCount64();
@@ -758,6 +732,11 @@ VmFileSystemDevice OpenVmmVirtualMachineBackend::CreateFileSystemDevice(const Vm
758732
c_notSupported,
759733
transport->Layout != VmVirtioFsLayout::SingleShare,
760734
"OpenVMM currently supports only single-share virtio-fs devices");
735+
// WslOpenVmmVmAddShare carries neither mount options nor an identity, so device options are
736+
// rejected rather than silently dropped, just as share-level mount options are.
737+
THROW_HR_IF_MSG(c_notSupported, !transport->Options.MountOptions.empty(), "OpenVMM does not support virtio-fs mount options");
738+
THROW_HR_IF_MSG(
739+
c_notSupported, !!transport->Options.UserToken, "OpenVMM does not support serving a virtio-fs device under a user token");
761740

762741
auto lock = m_lock.lock_exclusive();
763742
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_vm);
@@ -804,6 +783,7 @@ VmFileSystemShare OpenVmmVirtualMachineBackend::AddFileSystemShare(VmDeviceId De
804783
const auto* options = std::get_if<VmVirtioFsShareOptions>(&Request.Options);
805784
THROW_HR_IF_MSG(c_notSupported, !options, "OpenVMM supports only virtio-fs share options");
806785
THROW_HR_IF_MSG(c_notSupported, !options->MountOptions.empty(), "OpenVMM does not support virtio-fs mount options");
786+
THROW_HR_IF_MSG(c_notSupported, !!options->UserToken, "OpenVMM does not support serving a virtio-fs share under a user token");
807787
const auto hostPath = wsl::windows::common::filesystem::GetCanonicalPath(Request.HostPath);
808788
const auto attributes = GetFileAttributesW(hostPath.c_str());
809789
THROW_LAST_ERROR_IF(attributes == INVALID_FILE_ATTRIBUTES);

‎src/windows/common/OpenVmmVirtualMachineBackend.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class OpenVmmVirtualMachineBackend : public IVirtualMachineBackend
4646
void Terminate() override;
4747

4848
VmGuestListener CreateGuestListener(GuestServicePort Port) override;
49-
wil::unique_socket AcceptGuestConnection(VmListenerId Listener) override;
5049
wil::unique_socket ConnectGuest(GuestServicePort Port) override;
5150
void CloseGuestListener(VmListenerId Listener) override;
5251

0 commit comments

Comments
 (0)