Skip to content

Commit 21bab9c

Browse files
committed
updates
1 parent 5207c4c commit 21bab9c

8 files changed

Lines changed: 294 additions & 179 deletions

‎src/windows/common/HcsVirtualMachineBackend.cpp‎

Lines changed: 103 additions & 97 deletions
Large diffs are not rendered by default.

‎src/windows/common/HcsVirtualMachineBackend.h‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
7070
void CreateDnsRecord(VmDeviceId Device, const VmDnsRecord& Record) override;
7171

7272
private:
73-
struct AttachedDisk
73+
struct HcsDisk
7474
{
75-
VmDiskAttachment Attachment;
7675
// Set for pass-through disks, cleared for virtual disks.
7776
bool PassThrough = false;
7877
std::wstring Path;
@@ -82,6 +81,8 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
8281
wil::unique_hfile BackingFile;
8382
};
8483

84+
using AttachedDisk = VmResource<VmDiskAttachment, HcsDisk>;
85+
8586
struct VmConfiguration
8687
{
8788
VmDescription Description;
@@ -92,42 +93,38 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
9293
std::uint64_t NextDiskId = 1;
9394
};
9495

95-
struct FileSystemDevice
96+
struct HcsFileSystemDevice
9697
{
97-
VmFileSystemDevice Device;
98-
VmFileSystemDeviceTransport Transport;
98+
// Required effective identity, resolved from the request or VM identity at creation.
99+
wil::shared_handle UserToken;
99100
// Mount options applied when the virtio-fs device is created.
100101
std::wstring MountOptions;
101102
wil::com_ptr<IPlan9FileSystem> Plan9Server;
102103
};
103104

105+
using FileSystemDevice = VmResource<VmFileSystemDevice, HcsFileSystemDevice>;
104106
using FileSystemDeviceMap = std::map<std::uint64_t, FileSystemDevice>;
105107

106-
struct FileSystemShare
108+
struct HcsFileSystemShare
107109
{
108-
VmFileSystemShare Share;
109110
// Options the share was created with. Together with the host path these identify a virtio-fs
110111
// share, so a repeated request reuses the existing share instead of creating a second one.
111112
std::wstring MountOptions;
112-
// Token the share was created with, if any. A Plan 9 share is removed under the same identity
113-
// that added it. Unset shares fall back to the VM identity token.
113+
// Required effective identity. Plan 9 removal uses the same token as creation,
114+
// including when it inherited the device identity.
114115
wil::shared_handle UserToken;
115116
};
116117

117-
struct NetworkAdapter
118+
using FileSystemShare = VmResource<VmFileSystemShare, HcsFileSystemShare>;
119+
120+
struct HcsNetworkAdapter
118121
{
119-
VmNetworkAttachment Attachment;
120122
// Resource path the adapter was added at. Set only for host endpoint networks, which are
121123
// removed from the compute system at the same path that added them.
122124
std::wstring ResourcePath;
123125
};
124126

125-
struct PortBinding
126-
{
127-
VmPortBinding Binding;
128-
// Tag of the user-mode NAT device that owns the binding.
129-
std::wstring Tag;
130-
};
127+
using NetworkAdapter = VmResource<VmNetworkAttachment, HcsNetworkAdapter>;
131128

132129
HcsVirtualMachineBackend();
133130
void Initialize(const VmCreateRequest& Request);
@@ -157,9 +154,9 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
157154
const FileSystemShare* FindFileSystemShareLocked(VmDeviceId Device, const std::wstring& HostPath, const std::wstring& MountOptions) const;
158155

159156
/// <summary>
160-
/// Resolves the token used to reach the host path of a share, preferring the one on the request.
157+
/// Returns shared ownership of the token used to reach the host path, preferring the request's token.
161158
/// </summary>
162-
HANDLE ResolveShareUserToken(const VmFileSystemShareRequest& Request) const;
159+
wil::shared_handle ResolveShareUserToken(const FileSystemDevice& Device, const VmFileSystemShareRequest& Request) const;
163160

164161
/// <summary>
165162
/// Adds a share to a Plan 9 device and returns the name the guest uses to reach it.
@@ -234,7 +231,7 @@ class HcsVirtualMachineBackend : public IVirtualMachineBackend
234231
_Guarded_by_(m_lock) std::uint64_t m_nextShareId = 1;
235232
_Guarded_by_(m_lock) std::vector<VmNetworkAdapterRequest> m_pendingNetworkAdapters;
236233
_Guarded_by_(m_lock) std::map<std::uint64_t, NetworkAdapter> m_networkAdapters;
237-
_Guarded_by_(m_lock) std::map<std::uint64_t, PortBinding> m_portBindings;
234+
_Guarded_by_(m_lock) std::map<std::uint64_t, VmPortBinding> m_portBindings;
238235
_Guarded_by_(m_lock) std::uint64_t m_nextPortBindingId = 1;
239236
wil::unique_handle m_restrictedToken;
240237

‎src/windows/common/IVirtualMachineBackend.h‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ enum class BackendKind
4444
struct VmInstanceId
4545
{
4646
GUID VmId{};
47+
// Required VM identity; unlike request overrides, this token cannot be omitted.
4748
wil::shared_handle UserToken{};
4849
};
4950

@@ -66,6 +67,13 @@ using VmShareId = VmResourceId<VmShareTag>;
6667
using VmListenerId = VmResourceId<VmListenerTag>;
6768
using VmPortBindingId = VmResourceId<VmPortBindingTag>;
6869

70+
// Backends own the extended resource; API methods return only its description by value.
71+
template <typename Description, typename BackendState>
72+
struct VmResource : Description
73+
{
74+
BackendState Backend;
75+
};
76+
6977
enum class VmFeatureRequest
7078
{
7179
Disabled,
@@ -300,9 +308,9 @@ struct VmDiskRequest
300308
std::variant<VmVirtualDiskSource, VmPhysicalDiskSource> Source;
301309
bool ReadOnly = true;
302310
std::optional<VmScsiPlacement> Placement;
303-
// Token whose identity is used to grant access to the disk. The VM identity token is used when
304-
// this is unset.
305-
wil::shared_handle UserToken{};
311+
// VHD access uses this token, falling back to the VM identity when unset. Physical disk
312+
// requests require an elevated token; an unset token is reserved for service-authorized restore.
313+
std::optional<wil::shared_handle> UserToken;
306314
// Set for disks that the user explicitly attached (for instance via 'wsl --mount'), as opposed
307315
// to disks that WSL attaches on the user's behalf.
308316
bool UserDisk = false;
@@ -516,6 +524,9 @@ using VmFileSystemDeviceTransport =
516524
struct VmFileSystemDeviceRequest
517525
{
518526
VmFileSystemDeviceTransport Transport;
527+
// Host identity for the device, defaulting to the VM identity. Aggregate virtio-fs children
528+
// inherit this identity because the device host does not accept a token when adding a child.
529+
std::optional<wil::shared_handle> UserToken;
519530
};
520531

521532
enum class VmFileSystemDeviceState
@@ -532,6 +543,7 @@ struct VmFileSystemDevice
532543
// Set once the device exists in the VM. Backends that create the device when its first share is
533544
// added report a prepared device without a guest instance id.
534545
std::optional<GUID> GuestInstanceId;
546+
VmFileSystemDeviceTransport Transport;
535547
};
536548

537549
struct VmPlan9ShareOptions
@@ -552,10 +564,9 @@ struct VmFileSystemShareRequest
552564
std::wstring Name;
553565
bool ReadOnly = true;
554566
VmFileSystemShareOptions Options;
555-
// Token whose identity is used to reach the host path. Elevated and unelevated callers share the
556-
// same VM, so a share carries its own token instead of reusing the one that created the VM. The
557-
// VM identity token is used when this is unset.
558-
wil::shared_handle UserToken{};
567+
// Token whose identity is used to reach the host path, defaulting to the device identity.
568+
// Aggregate virtio-fs shares always use the device identity; set its token at device creation.
569+
std::optional<wil::shared_handle> UserToken;
559570
};
560571

561572
struct VmVirtioFsShareAddress
@@ -600,7 +611,7 @@ struct VmPersistentMemoryRequest
600611
bool ReadOnly = true;
601612
// Token whose identity is used to reach the backing file. The VM identity token is used when
602613
// this is unset.
603-
wil::shared_handle UserToken{};
614+
std::optional<wil::shared_handle> UserToken;
604615
VmPersistentMemoryReadyCallback WaitForGuestDevice;
605616
};
606617

@@ -645,7 +656,8 @@ struct VmSharedMemoryRequest
645656
std::wstring Tag;
646657
std::wstring Path;
647658
std::uint64_t SizeBytes = 0;
648-
wil::shared_handle UserToken{};
659+
// Host identity for the section, defaulting to the VM identity when omitted.
660+
std::optional<wil::shared_handle> UserToken;
649661
};
650662

651663
struct VmSharedMemoryDevice
@@ -777,4 +789,4 @@ const VmVirtualDiskSource& ValidateDiskRequest(const VmDiskRequest& Request);
777789

778790
const std::wstring& ValidateDiskSource(const VmDiskRequest& Request);
779791

780-
} // namespace wsl::windows::common::vm::validation
792+
} // namespace wsl::windows::common::vm::validation

‎src/windows/common/OpenVmmVirtualMachineBackend.cpp‎

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void OpenVmmVirtualMachineBackend::Initialize(const VmCreateRequest& Request)
324324
const auto& configuration = std::get<VmUserModeNatNetwork>(attachment.EffectiveConfiguration);
325325
const auto macAddress = wsl::shared::string::FormatMacAddress(configuration.ClientMacAddress(), L'-');
326326
THROW_IF_FAILED(WslOpenVmmConfigSetConsommeNic(config.get(), nicId.c_str(), macAddress.c_str()));
327-
m_networkAdapters.emplace(attachment.Id.Value, NetworkAdapter{attachment, nicId});
327+
m_networkAdapters.emplace(attachment.Id.Value, NetworkAdapter{attachment, {nicId}});
328328
m_nextDeviceId = attachment.Id.Value + 1;
329329
}
330330
for (const auto& disk : Request.BootDisks)
@@ -757,6 +757,7 @@ VmGpuAttachment OpenVmmVirtualMachineBackend::AddGpu(const VmGpuRequest&)
757757

758758
VmFileSystemDevice OpenVmmVirtualMachineBackend::CreateFileSystemDevice(const VmFileSystemDeviceRequest& Request)
759759
{
760+
THROW_HR_IF_MSG(c_notSupported, Request.UserToken.has_value(), "OpenVMM does not support a per-device user token");
760761
const auto* transport = std::get_if<VmVirtioFsDevice>(&Request.Transport);
761762
WSL_LOG(
762763
"OpenVmmCreateFileSystemDeviceBegin",
@@ -773,11 +774,13 @@ VmFileSystemDevice OpenVmmVirtualMachineBackend::CreateFileSystemDevice(const Vm
773774
THROW_HR_IF(E_BOUNDS, m_nextDeviceId == UINT64_MAX);
774775
for (const auto& entry : m_fileSystemDevices)
775776
{
776-
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), wsl::shared::string::IsEqual(entry.second.Transport.Tag, transport->Tag, false));
777+
THROW_HR_IF(
778+
HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS),
779+
wsl::shared::string::IsEqual(std::get<VmVirtioFsDevice>(entry.second.Transport).Tag, transport->Tag, false));
777780
}
778781

779-
VmFileSystemDevice device{{m_description.Identity, m_nextDeviceId}, VmFileSystemDeviceState::Prepared};
780-
const auto inserted = m_fileSystemDevices.emplace(device.Id.Value, FileSystemDevice{device, *transport, {}}).second;
782+
VmFileSystemDevice device{{m_description.Identity, m_nextDeviceId}, VmFileSystemDeviceState::Prepared, {}, Request.Transport};
783+
const auto inserted = m_fileSystemDevices.emplace(device.Id.Value, FileSystemDevice{device, {}}).second;
781784
WI_ASSERT(inserted);
782785
++m_nextDeviceId;
783786
WSL_LOG(
@@ -795,7 +798,7 @@ VmFileSystemDevice OpenVmmVirtualMachineBackend::GetFileSystemDeviceStatus(VmDev
795798
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_vm);
796799
const auto device = m_fileSystemDevices.find(Device.Value);
797800
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), device == m_fileSystemDevices.end());
798-
return device->second.Device;
801+
return device->second;
799802
}
800803

801804
VmFileSystemShare OpenVmmVirtualMachineBackend::AddFileSystemShare(VmDeviceId Device, const VmFileSystemShareRequest& Request)
@@ -821,23 +824,23 @@ VmFileSystemShare OpenVmmVirtualMachineBackend::AddFileSystemShare(VmDeviceId De
821824
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_vm);
822825
const auto device = m_fileSystemDevices.find(Device.Value);
823826
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), device == m_fileSystemDevices.end());
824-
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), device->second.Share.has_value());
827+
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), device->second.Backend.Share.has_value());
825828
THROW_HR_IF(E_BOUNDS, m_nextShareId == UINT64_MAX);
826829

827830
VmFileSystemShare share{
828831
{m_description.Identity, m_nextShareId},
829832
Device,
830-
VmVirtioFsShareAddress{device->second.Transport.Tag, {}},
833+
VmVirtioFsShareAddress{std::get<VmVirtioFsDevice>(device->second.Transport).Tag, {}},
831834
hostPath,
832835
Request.ReadOnly};
833836
const auto& guestAddress = std::get<VmVirtioFsShareAddress>(share.GuestAddress);
834-
const auto [entry, inserted] = m_fileSystemShares.emplace(share.Id.Value, FileSystemShare{share});
837+
const auto [entry, inserted] = m_fileSystemShares.emplace(share.Id.Value, share);
835838
WI_ASSERT(inserted);
836-
device->second.Share = share.Id.Value;
837-
device->second.Device.State = VmFileSystemDeviceState::Serving;
839+
device->second.Backend.Share = share.Id.Value;
840+
device->second.State = VmFileSystemDeviceState::Serving;
838841
auto rollback = wil::scope_exit([this, &device, &entry] {
839-
device->second.Share.reset();
840-
device->second.Device.State = VmFileSystemDeviceState::Prepared;
842+
device->second.Backend.Share.reset();
843+
device->second.State = VmFileSystemDeviceState::Prepared;
841844
m_fileSystemShares.erase(entry);
842845
});
843846
const auto result = WslOpenVmmVmAddShare(m_vm.get(), guestAddress.Tag.c_str(), hostPath.c_str(), Request.ReadOnly);
@@ -865,21 +868,21 @@ void OpenVmmVirtualMachineBackend::RemoveFileSystemShare(VmShareId Share)
865868
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_vm);
866869
const auto share = m_fileSystemShares.find(Share.Value);
867870
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), share == m_fileSystemShares.end());
868-
const auto device = m_fileSystemDevices.find(share->second.Share.Device.Value);
869-
THROW_HR_IF(E_UNEXPECTED, device == m_fileSystemDevices.end() || device->second.Share != Share.Value);
871+
const auto device = m_fileSystemDevices.find(share->second.Device.Value);
872+
THROW_HR_IF(E_UNEXPECTED, device == m_fileSystemDevices.end() || device->second.Backend.Share != Share.Value);
870873

871-
const auto& guestAddress = std::get<VmVirtioFsShareAddress>(share->second.Share.GuestAddress);
874+
const auto& guestAddress = std::get<VmVirtioFsShareAddress>(share->second.GuestAddress);
872875
const auto result = WslOpenVmmVmRemoveShare(m_vm.get(), guestAddress.Tag.c_str());
873876
WSL_LOG(
874877
"OpenVmmRemoveFileSystemShareEnd",
875878
TraceLoggingValue(m_description.Identity.VmId, "vmId"),
876879
TraceLoggingValue(Share.Value, "shareId"),
877-
TraceLoggingValue(device->second.Device.Id.Value, "deviceId"),
880+
TraceLoggingValue(device->second.Id.Value, "deviceId"),
878881
TraceLoggingValue(guestAddress.Tag.c_str(), "tag"),
879882
TraceLoggingHResult(result, "result"));
880883
THROW_IF_FAILED(result);
881-
device->second.Share.reset();
882-
device->second.Device.State = VmFileSystemDeviceState::Prepared;
884+
device->second.Backend.Share.reset();
885+
device->second.State = VmFileSystemDeviceState::Prepared;
883886
m_fileSystemShares.erase(share);
884887
}
885888

@@ -900,7 +903,7 @@ void OpenVmmVirtualMachineBackend::RemoveDevice(VmDeviceId Device)
900903
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_vm);
901904
const auto device = m_fileSystemDevices.find(Device.Value);
902905
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), device == m_fileSystemDevices.end());
903-
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_BUSY), device->second.Share.has_value());
906+
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_BUSY), device->second.Backend.Share.has_value());
904907
m_fileSystemDevices.erase(device);
905908
}
906909

@@ -967,11 +970,12 @@ VmPortBinding OpenVmmVirtualMachineBackend::BindPort(VmDeviceId Device, const Vm
967970
Request.ListenScopeId,
968971
Request.HostPort,
969972
Request.GuestPort};
970-
const auto [entry, inserted] = m_portBindings.emplace(binding.Id.Value, PortBinding{binding, adapter->second.NicId, hostAddress});
973+
const auto [entry, inserted] =
974+
m_portBindings.emplace(binding.Id.Value, PortBinding{binding, {adapter->second.Backend.NicId, hostAddress}});
971975
WI_ASSERT(inserted);
972976
auto rollback = wil::scope_exit([this, &entry] { m_portBindings.erase(entry); });
973-
const auto result =
974-
WslOpenVmmVmBindPort(m_vm.get(), adapter->second.NicId.c_str(), Request.HostPort, Request.GuestPort, tcp, hostAddress.c_str());
977+
const auto result = WslOpenVmmVmBindPort(
978+
m_vm.get(), adapter->second.Backend.NicId.c_str(), Request.HostPort, Request.GuestPort, tcp, hostAddress.c_str());
975979
WSL_LOG(
976980
"OpenVmmBindPortEnd",
977981
TraceLoggingValue(m_description.Identity.VmId, "vmId"),
@@ -999,16 +1003,16 @@ void OpenVmmVirtualMachineBackend::UnbindPort(VmPortBindingId Binding)
9991003

10001004
const auto result = WslOpenVmmVmUnbindPort(
10011005
m_vm.get(),
1002-
binding->second.NicId.c_str(),
1003-
binding->second.Binding.EffectiveHostPort,
1004-
binding->second.Binding.GuestPort,
1005-
binding->second.Binding.Protocol == TransportProtocol_Tcp,
1006-
binding->second.HostAddress.c_str());
1006+
binding->second.Backend.NicId.c_str(),
1007+
binding->second.EffectiveHostPort,
1008+
binding->second.GuestPort,
1009+
binding->second.Protocol == TransportProtocol_Tcp,
1010+
binding->second.Backend.HostAddress.c_str());
10071011
WSL_LOG(
10081012
"OpenVmmUnbindPortEnd",
10091013
TraceLoggingValue(m_description.Identity.VmId, "vmId"),
10101014
TraceLoggingValue(Binding.Value, "bindingId"),
1011-
TraceLoggingValue(binding->second.Binding.Device.Value, "deviceId"),
1015+
TraceLoggingValue(binding->second.Device.Value, "deviceId"),
10121016
TraceLoggingHResult(result, "result"));
10131017
THROW_IF_FAILED(result);
10141018
m_portBindings.erase(binding);
@@ -1026,4 +1030,4 @@ void OpenVmmVirtualMachineBackend::CreateDnsRecord(VmDeviceId Device, const VmDn
10261030
ExecutionContext context(Context::ConfigureNetworking);
10271031
validation::ValidateResourceId(Device, m_description.Identity);
10281032
THROW_HR_MSG(c_notSupported, "OpenVMM does not support static DNS records");
1029-
}
1033+
}

‎src/windows/common/OpenVmmVirtualMachineBackend.h‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,28 @@ class OpenVmmVirtualMachineBackend : public IVirtualMachineBackend
9494
wil::unique_hfile SocketFile;
9595
};
9696

97-
struct FileSystemDevice
97+
struct OpenVmmFileSystemDevice
9898
{
99-
VmFileSystemDevice Device;
100-
VmVirtioFsDevice Transport;
10199
std::optional<std::uint64_t> Share;
102100
};
103101

104-
struct FileSystemShare
105-
{
106-
VmFileSystemShare Share;
107-
};
102+
using FileSystemDevice = VmResource<VmFileSystemDevice, OpenVmmFileSystemDevice>;
108103

109-
struct NetworkAdapter
104+
struct OpenVmmNetworkAdapter
110105
{
111-
VmNetworkAttachment Attachment;
112106
std::wstring NicId;
113107
};
114108

115-
struct PortBinding
109+
using NetworkAdapter = VmResource<VmNetworkAttachment, OpenVmmNetworkAdapter>;
110+
111+
struct OpenVmmPortBinding
116112
{
117-
VmPortBinding Binding;
118113
std::wstring NicId;
119114
std::wstring HostAddress;
120115
};
121116

117+
using PortBinding = VmResource<VmPortBinding, OpenVmmPortBinding>;
118+
122119
struct SessionFileSystemResources
123120
{
124121
std::filesystem::path SocketDirectory;
@@ -131,7 +128,7 @@ class OpenVmmVirtualMachineBackend : public IVirtualMachineBackend
131128
_Guarded_by_(m_lock) std::map<std::uint64_t, VmDiskAttachment> m_attachedDisks;
132129
_Guarded_by_(m_lock) std::uint64_t m_nextDiskId = 1;
133130
_Guarded_by_(m_lock) std::map<std::uint64_t, FileSystemDevice> m_fileSystemDevices;
134-
_Guarded_by_(m_lock) std::map<std::uint64_t, FileSystemShare> m_fileSystemShares;
131+
_Guarded_by_(m_lock) std::map<std::uint64_t, VmFileSystemShare> m_fileSystemShares;
135132
_Guarded_by_(m_lock) std::map<std::uint64_t, NetworkAdapter> m_networkAdapters;
136133
_Guarded_by_(m_lock) std::map<std::uint64_t, PortBinding> m_portBindings;
137134
_Guarded_by_(m_lock) std::uint64_t m_nextDeviceId = 1;

0 commit comments

Comments
 (0)