| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include "DeviceHostProxy.h" |
| 6 | |
| 7 | inline const std::wstring c_defaultDeviceTag = L"default"; |
| 8 | |
| 9 | struct VirtioFsShareOptions |
| 10 | { |
| 11 | VirtiofsShareKind Kind = VirtiofsShareKind_FileBacked; |
| 12 | UINT32 SharedMemorySizeMb = 0; |
| 13 | }; |
| 14 | |
| 15 | // |
| 16 | // Provides synchronized access to guest device operations. |
| 17 | // |
| 18 | class GuestDeviceManager |
| 19 | { |
| 20 | public: |
| 21 | GuestDeviceManager(_In_ const std::wstring& machineId, _In_ const GUID& runtimeId, bool EnableTelemetry = true); |
| 22 | ~GuestDeviceManager(); |
| 23 | |
| 24 | _Requires_lock_not_held_(m_lock) |
| 25 | GUID AddVirtiofsDevice(_In_ PCWSTR Label, _In_opt_ PCWSTR MountOptions, _In_ PCWSTR RootPath, _In_ HANDLE UserToken, VirtioFsShareOptions Options = {}); |
| 26 | |
| 27 | _Requires_lock_not_held_(m_lock) |
| 28 | void AddVirtiofsChild(_In_ const GUID& InstanceId, _In_ PCWSTR Name, _In_opt_ PCWSTR MountOptions, _In_ PCWSTR RootPath); |
| 29 | |
| 30 | _Requires_lock_not_held_(m_lock) |
| 31 | void RemoveVirtiofsChild(_In_ const GUID& InstanceId, _In_ PCWSTR Name); |
| 32 | |
| 33 | _Requires_lock_not_held_(m_lock) |
| 34 | GUID AddVirtioPmemDevice(_In_ PCWSTR Path, bool ReadOnly, _In_ HANDLE UserToken); |
| 35 | |
| 36 | _Requires_lock_not_held_(m_lock) |
| 37 | GUID AddNewDevice(_In_ const GUID& deviceId, _In_ const wil::com_ptr<IPlan9FileSystem>& server, _In_ PCWSTR tag); |
| 38 | |
| 39 | _Requires_lock_not_held_(m_lock) |
| 40 | GUID AddVirtioNetDevice(_In_ PCWSTR Tag, const WslVirtioNetConfig& Config, const std::vector<IpAddress>& Nameservers, _In_ HANDLE UserToken); |
| 41 | |
| 42 | wil::com_ptr<IWslVirtioNetDevice> GetVirtioNetDevice(_In_ PCWSTR Tag); |
| 43 | |
| 44 | void AddRemoteFileSystem(_In_ REFCLSID clsid, _In_ PCWSTR tag, _In_ const wil::com_ptr<IPlan9FileSystem>& server); |
| 45 | |
| 46 | void AddSharedMemoryDevice(_In_ PCWSTR Tag, _In_ PCWSTR Path, _In_ UINT32 SizeMb, _In_ HANDLE UserToken); |
| 47 | |
| 48 | wil::com_ptr<IPlan9FileSystem> GetRemoteFileSystem(_In_ REFCLSID clsid, _In_ std::wstring_view tag); |
| 49 | |
| 50 | void SetSwiotlb(UINT64 GpaBase, UINT64 SizeBytes); |
| 51 | |
| 52 | _Requires_lock_not_held_(m_lock) |
| 53 | void RemoveGuestDevice(_In_ const GUID& InstanceId); |
| 54 | |
| 55 | private: |
| 56 | struct DirectoryObjectLifetime |
| 57 | { |
| 58 | std::wstring Path; |
| 59 | // Directory objects are temporary, even if they have children, so need to keep |
| 60 | // any created handles open in order for the directory to remain accessible. |
| 61 | std::vector<wil::unique_handle> HierarchyLifetimes; |
| 62 | }; |
| 63 | |
| 64 | DirectoryObjectLifetime CreateSectionObjectRoot(_In_ std::wstring_view RelativeRootPath, _In_ HANDLE UserToken) const; |
| 65 | |
| 66 | wil::srwlock m_lock; |
| 67 | std::wstring m_machineId; |
| 68 | wil::com_ptr<DeviceHostProxy> m_deviceHostSupport; |
| 69 | _Guarded_by_(m_lock) std::vector<DirectoryObjectLifetime> m_objectDirectories; |
| 70 | _Guarded_by_(m_lock) std::map<std::wstring, GUID> m_virtioNetDevices; |
| 71 | }; |