Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HvSocket support for containers #2353

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anmaxvl
Copy link
Contributor

@anmaxvl anmaxvl commented Jan 13, 2025

Applications connecting from the host into the container should use
container-specific VMID. This ID will need to be the same as the
container's VMID inside the guest, which is calculated by HCS/GCS
like it's done in this PR by HCSIDToGUID.

To allow the container ID to work with HvSocket on the host, we
need to set up an AddressInfo mapping to tell HvSocket to redirect
the call into the UVM, which is done in this PR by default for
all WCOW containers.

@anmaxvl anmaxvl requested a review from a team as a code owner January 13, 2025 18:56
Applications connecting from the host into the container should use
container-specific VMID. This ID will need to be the same as the
container's VMID inside the guest, which is calculated by HCS/GCS
like it's done in this PR by `HCSIDToGUID`.

To allow the container ID to work with HvSocket on the host, we
need to set up an AddressInfo mapping to tell HvSocket to redirect
the call into the UVM, which is done in this PR by default for
all WCOW containers.

Signed-off-by: Maksim An <[email protected]>
@anmaxvl anmaxvl force-pushed the hvsocket-for-containers branch from 4d40dbd to caf030c Compare January 13, 2025 19:46
Copy link
Contributor

@helsaawy helsaawy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the address info ioctl have to go through before the container is created?
if not, then we could probably remove the HCSIDToGUID code and instead add the call to CreateAddressInfo in hcsoci.Create after the system is created and we have its GUID from HCS (so we don't rely on their internal logic for generating the GUID):

func CreateContainer(/* ... */) (_ cow.Container, _ *resources.Resources, err error) {
	// ...
	if gcsDocument != nil {
		c, err := coi.HostingSystem.CreateContainer(ctx, coi.actualID, gcsDocument)
		if err != nil {
			return nil, r, err
		}

		if p, err := c.PropertiesV2(ctx); err != nil {
			return nil, r, fmt.Errorf("retrieve created container compute system properties: %w", err)
		}
		hvsockAI, err := hvsocket.CreateAddressInfo(p.RuntimeId, coi.HostingSystem.RuntimeID(), true)
		if err != nil {
			return nil, r, fmt.Errorf("exposing container Hyper-V socket: %w", err)
		}
		r.Add(hvsockAI)

		return c, r, nil
	}
}

Comment on lines +31 to +41
const (
cAddressFlagPassthru = 0x00000001
cIOCtlHVSocketUpdateAddressInfo = 0x21c004
)

type tAddressInfo struct {
SystemID guid.GUID
VirtualMachineID guid.GUID
SiloID guid.GUID
Flags uint32
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you've been looking at C++ code for too long 😅

Suggested change
const (
cAddressFlagPassthru = 0x00000001
cIOCtlHVSocketUpdateAddressInfo = 0x21c004
)
type tAddressInfo struct {
SystemID guid.GUID
VirtualMachineID guid.GUID
SiloID guid.GUID
Flags uint32
}
const (
addressFlagPassthru = 0x00000001
ioCtlHVSocketUpdateAddressInfo = 0x21c004
)
type addressInfo struct {
systemID guid.GUID
virtualMachineID guid.GUID
siloID guid.GUID
flags uint32
}

Comment on lines +43 to +49
type Handle windows.Handle

func (h Handle) Release(ctx context.Context) error {
return windows.CloseHandle(windows.Handle(h))
}

func CreateAddressInfo(cid, vmid guid.GUID, passthru bool) (Handle, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be better to keep the type private and also make it a proper struct to prevent casting from handles not created below:

Suggested change
type Handle windows.Handle
func (h Handle) Release(ctx context.Context) error {
return windows.CloseHandle(windows.Handle(h))
}
func CreateAddressInfo(cid, vmid guid.GUID, passthru bool) (Handle, error) {
type addressInfo {
h windows.Handle
}
var _ resources.ResourceCloser = addressInfo{}
func (ai addressInfo) Release(ctx context.Context) error {
return windows.CloseHandle(windows.Handle(ai.h))
}
func CreateAddressInfo(cid, vmid guid.GUID, passthru bool) (resources.ResourceCloser , error) {

@helsaawy
Copy link
Contributor

on a side note, how are users expected to get the runtime ID of the container for creating hv sockets?
are they expected to query HCS? or replicate the logic in HCSIDToGUID?

@apurv15
Copy link
Contributor

apurv15 commented Jan 18, 2025

What is the use case here? Is user expected to connect to inside the UVM using the containerId?
Can this be not satisfied through shimdiag exec into the UVM using the PodId?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants