From 9b4ef14d6d69ac246e91ffaf34e237244fa84aa7 Mon Sep 17 00:00:00 2001 From: Maksim An Date: Wed, 12 Feb 2025 11:29:05 -0800 Subject: [PATCH] minor refactor of `CreateAddressInfo` add a generic function for creating HvSocket address info mapping. export a function that creates a mapping for containers only. Signed-off-by: Maksim An --- internal/hcsoci/create.go | 2 +- internal/hvsocket/hvsocket.go | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/hcsoci/create.go b/internal/hcsoci/create.go index 3c2072d1b3..c287cf7f8b 100644 --- a/internal/hcsoci/create.go +++ b/internal/hcsoci/create.go @@ -291,7 +291,7 @@ func CreateContainer(ctx context.Context, createOptions *CreateOptions) (_ cow.C if err != nil { return nil, r, fmt.Errorf("convert to system GUID failed: %w", err) } - addressInfoCloser, err := hvsocket.CreateAddressInfo(containerSystemGUID, coi.HostingSystem.RuntimeID(), true) + addressInfoCloser, err := hvsocket.CreateContainerAddressInfo(containerSystemGUID, coi.HostingSystem.RuntimeID()) if err != nil { return nil, r, fmt.Errorf("redirect container HvSocket failed: %w", err) } diff --git a/internal/hvsocket/hvsocket.go b/internal/hvsocket/hvsocket.go index 8817f06e26..cf3ecb10f9 100644 --- a/internal/hvsocket/hvsocket.go +++ b/internal/hvsocket/hvsocket.go @@ -36,8 +36,24 @@ func (aic addressInfoCloser) Release(_ context.Context) error { return windows.CloseHandle(aic.handle) } -func CreateAddressInfo(cid, vmid guid.GUID, passthru bool) (resources.ResourceCloser, error) { - path := fmt.Sprintf(`\\.\HvSocketSystem\AddressInfo\{%s}`, cid) +// CreateContainerAddressInfo creates an address info entry in HvSocket to redirect +// the calls to the container silo inside UVM. +func CreateContainerAddressInfo(containerID, uvmID guid.GUID) (resources.ResourceCloser, error) { + return CreateAddressInfo(containerID, uvmID, guid.GUID{}, true) +} + +// CreateAddressInfo creates an address info entry in the HvSocket provider to map a +// compute system GUID to a virtual machine ID or compartment ID. +// +// `systemID` is the compute system GUID to map. +// `vmID` is the virtual machine ID to which the system GUID maps to. Must be guid.GUID{} to specify +// that the system GUID maps to a network compartment ID on the hosting system. +// `siloID` is the silo object ID to which the system GUID maps to. +// `passthru` when vmID is not guid.GUID{}, specifies whether the systemID maps to the primary +// compartment of the virtual machine (set to `false`) or to another compartment within the +// virtual machine (set to `true`) +func CreateAddressInfo(systemID, vmID, siloID guid.GUID, passthru bool) (resources.ResourceCloser, error) { + path := fmt.Sprintf(`\\.\HvSocketSystem\AddressInfo\{%s}`, systemID) u16, err := windows.UTF16PtrFromString(path) if err != nil { return nil, err @@ -56,9 +72,11 @@ func CreateAddressInfo(cid, vmid guid.GUID, passthru bool) (resources.ResourceCl } addrInfo := addressInfo{ - systemID: cid, - virtualMachineID: vmid, + systemID: systemID, + virtualMachineID: vmID, + siloID: siloID, } + if passthru { addrInfo.flags |= addressFlagPassthru }