Skip to content

Commit 7c9c0db

Browse files
committed
Refactor + fix locking story
1 parent eeeb90e commit 7c9c0db

4 files changed

Lines changed: 260 additions & 168 deletions

File tree

‎src/windows/service/exe/IVirtualMachineBackend.h‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ struct VmGuestListener
180180
GuestServicePort Port;
181181
};
182182

183+
struct VmGuestListenerState
184+
{
185+
VmGuestListener Listener;
186+
wil::unique_socket Socket;
187+
wil::unique_event CancellationEvent{wil::EventOptions::ManualReset};
188+
};
189+
183190
struct VmProcessorRequest
184191
{
185192
std::uint32_t Count = 0;
@@ -518,8 +525,45 @@ class IVirtualMachineBackend
518525
virtual VmNetworkAttachment AddNetworkAdapter(const VmNetworkAdapterRequest& Request) = 0;
519526
virtual VmPortBinding BindPort(VmDeviceId Device, const VmPortBindingRequest& Request) = 0;
520527
virtual void UnbindPort(VmPortBindingId Binding) = 0;
528+
529+
530+
protected:
531+
// Protects all mutable backend state, including the base listener registry. Callers must
532+
// release it before performing blocking I/O or waiting for a callback.
533+
mutable wil::srwlock m_lock;
534+
535+
// These helpers require m_lock to be held exclusively. ConfigureGuestListener runs while
536+
// m_lock is held and must not re-enter another listener helper.
537+
VmGuestListener RegisterGuestListenerLocked(const VmInstanceId& Identity, GuestServicePort Port);
538+
wil::unique_socket AcceptGuestListenerConnection(VmListenerId Listener, const VmInstanceId& Identity) const;
539+
std::shared_ptr<VmGuestListenerState> RemoveGuestListenerLocked(VmListenerId Listener, const VmInstanceId& Identity);
540+
void CloseGuestListenersLocked(const VmInstanceId& Identity) noexcept;
541+
542+
private:
543+
virtual std::shared_ptr<VmGuestListenerState> ConfigureGuestListener(const VmGuestListener& Listener);
544+
545+
_Guarded_by_(m_lock) std::map<std::uint64_t, std::shared_ptr<VmGuestListenerState>> m_guestListeners;
546+
_Guarded_by_(m_lock) std::uint64_t m_nextListenerId = 1;
521547
};
522548

523549
VmPlatformCapabilities QueryVirtualMachineBackendCapabilities(BackendKind Kind);
524550

525-
std::unique_ptr<IVirtualMachineBackend> CreateVirtualMachineBackend(BackendKind Kind, const VmCreateRequest& Request);
551+
std::unique_ptr<IVirtualMachineBackend> CreateVirtualMachineBackend(BackendKind Kind, const VmCreateRequest& Request);
552+
553+
namespace wsl::windows::common::vm::validation {
554+
555+
void ValidateFeature(VmFeatureRequest Request, PCWSTR Setting);
556+
void ValidateUnsupportedSelection(VmSelectionPolicy Policy);
557+
void ValidatePath(const std::filesystem::path& Path, PCWSTR Backend);
558+
const VmVirtualDiskSource& ValidateDiskRequest(const VmDiskRequest& Request, UINT32 MaximumDisks);
559+
void ValidateConsolePath(const std::filesystem::path& Path, PCWSTR Backend, HRESULT Error, bool RequireName);
560+
void ValidateName(std::wstring_view Name, PCWSTR Description);
561+
void ValidateResourceId(UINT64 Value, const GUID& VmId, const VmInstanceId& Owner);
562+
563+
template <typename Tag>
564+
void ValidateResourceId(const VmResourceId<Tag>& Id, const VmInstanceId& Owner)
565+
{
566+
ValidateResourceId(Id.Value, Id.Owner.VmId, Owner);
567+
}
568+
569+
}

0 commit comments

Comments
 (0)