Skip to content

Commit

Permalink
minor refactor of CreateAddressInfo
Browse files Browse the repository at this point in the history
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 <maksiman@microsoft.com>
  • Loading branch information
anmaxvl committed Feb 13, 2025
1 parent 517e1a1 commit 6b567c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/hcsoci/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 22 additions & 4 deletions internal/hvsocket/hvsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 6b567c4

Please sign in to comment.