Skip to content

Commit

Permalink
weird state shenanigans
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza El-Saawy <[email protected]>
  • Loading branch information
helsaawy committed Apr 23, 2024
1 parent 470efa7 commit 19311c0
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 109 deletions.
4 changes: 2 additions & 2 deletions internal/computecore/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (
// _In_ HCS_OPERATION operation,
// _In_opt_ void* context
// );
HCSOperationCompletion func(op HCSOperation, hcsCtx HCSContext)
HCSOperationCompletion func(op hcsOperation, hcsCtx HCSContext)

hcsOperationCompletionUintptr uintptr
)
Expand All @@ -29,7 +29,7 @@ func (f HCSOperationCompletion) asCallback() hcsOperationCompletionUintptr {
return hcsOperationCompletionUintptr(0)
}
return hcsOperationCompletionUintptr(windows.NewCallback(
func(op HCSOperation, hcsCtx HCSContext) uintptr {
func(op hcsOperation, hcsCtx HCSContext) uintptr {
f(op, hcsCtx)
return 0
},
Expand Down
29 changes: 22 additions & 7 deletions internal/computecore/computecore.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type HCSContext uintptr
//
//sys hcsEnumerateComputeSystems(query string, operation HCSOperation) (hr error) = computecore.HcsEnumerateComputeSystems?

func EnumerateComputeSystems(ctx context.Context, op HCSOperation, query *hcsschema.SystemQuery) (properties []hcsschema.Properties, err error) {
func EnumerateComputeSystems(ctx context.Context, op hcsOperation, query *hcsschema.SystemQuery) (properties []hcsschema.Properties, err error) {
ctx, cancel := context.WithTimeout(ctx, timeout.SyscallWatcher)
defer cancel()

ctx, span := oc.StartSpan(ctx, "computecore::HcsEnumerateComputeSystems", oc.WithClientSpanKind)
ctx, span := oc.StartSpan(ctx, computecoreSpanName("HcsEnumerateComputeSystems"), oc.WithClientSpanKind)
defer func() {
if len(properties) != 0 {
span.AddAttributes(trace.StringAttribute("properties", log.Format(ctx, properties)))
Expand All @@ -70,7 +70,7 @@ func EnumerateComputeSystems(ctx context.Context, op HCSOperation, query *hcssch
return runOperation[[]hcsschema.Properties](
ctx,
op,
func(_ context.Context, op HCSOperation) (err error) {
func(_ context.Context, op hcsOperation) (err error) {
return hcsEnumerateComputeSystems(q, op)
},
)
Expand All @@ -88,7 +88,7 @@ func EnumerateComputeSystems(ctx context.Context, op HCSOperation, query *hcssch

// SetCallback assigns a callback to handle events for the compute system.
func (s HCSSystem) SetCallback(ctx context.Context, options HCSEventOptions, hcsCtx HCSContext, callback HCSEventCallback) (err error) {
_, span := oc.StartSpan(ctx, "computecore::HcsSetComputeSystemCallback", oc.WithClientSpanKind)
_, span := oc.StartSpan(ctx, computecoreSpanName("HcsSetComputeSystemCallback"), oc.WithClientSpanKind)
defer func() {
oc.SetSpanStatus(span, err)
span.End()
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s HCSSystem) SetCallback(ctx context.Context, options HCSEventOptions, hcs

// SetCallback assigns a callback to handle events for the compute system.
func (p HCSProcess) SetCallback(ctx context.Context, options HCSEventOptions, hcsCtx HCSContext, callback HCSEventCallback) (err error) {
_, span := oc.StartSpan(ctx, "computecore::HcsSetProcessCallback", oc.WithClientSpanKind)
_, span := oc.StartSpan(ctx, computecoreSpanName("HcsSetProcessCallback"), oc.WithClientSpanKind)
defer func() {
oc.SetSpanStatus(span, err)
span.End()
Expand All @@ -134,8 +134,8 @@ func (p HCSProcess) SetCallback(ctx context.Context, options HCSEventOptions, hc

func runOperation[T any](
ctx context.Context,
op HCSOperation,
f func(context.Context, HCSOperation) error,
op hcsOperation,
f func(context.Context, hcsOperation) error,
) (v T, err error) {
if err := f(ctx, op); err != nil {
return v, err
Expand Down Expand Up @@ -173,3 +173,18 @@ func encode(v any) (string, error) {
// encoder.Encode appends a newline to the end
return strings.TrimSpace(buf.String()), nil
}

func validHandle(h windows.Handle) bool {
return h != 0 && h != windows.InvalidHandle
}

func computecoreSpanName(names ...string) string {
s := make([]string, 0, len(names))
for _, n := range names {
n = strings.TrimSpace(n)
if n != "" {
s = append(s, n)
}
}
return strings.Join(append([]string{"computecore"}, s...), "::")
}
115 changes: 115 additions & 0 deletions internal/computecore/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//go:build windows

package computecore

import "golang.org/x/sys/windows"

const hcsHResultPrefix = 0x80370000

// HCS specific error codes.
//
// See [documentation] for more info.
//
// [documentation]: https://learn.microsoft.com/en-us/virtualization/api/hcs/reference/hcshresult
//
//nolint:stylecheck // ST1003: ALL_CAPS
const (
// The virtual machine or container exited unexpectedly while starting.
HCS_E_TERMINATED_DURING_START = windows.Errno(hcsHResultPrefix + 0x0100)

// The container operating system does not match the host operating system.
HCS_E_IMAGE_MISMATCH = windows.Errno(hcsHResultPrefix + 0x0101)

// The virtual machine could not be started because a required feature is not installed.
HCS_E_HYPERV_NOT_INSTALLED = windows.Errno(hcsHResultPrefix + 0x0102)

// The requested virtual machine or container operation is not valid in the current state.
HCS_E_INVALID_STATE = windows.Errno(hcsHResultPrefix + 0x0105)

// The virtual machine or container exited unexpectedly.
HCS_E_UNEXPECTED_EXIT = windows.Errno(hcsHResultPrefix + 0x0106)

// The virtual machine or container was forcefully exited.
HCS_E_TERMINATED = windows.Errno(hcsHResultPrefix + 0x0107)

// A connection could not be established with the container or virtual machine.
HCS_E_CONNECT_FAILED = windows.Errno(hcsHResultPrefix + 0x0108)

// The operation timed out because a response was not received from the virtual machine or container.
HCS_E_CONNECTION_TIMEOUT = windows.Errno(hcsHResultPrefix + 0x0109)

// The connection with the virtual machine or container was closed.
HCS_E_CONNECTION_CLOSED = windows.Errno(hcsHResultPrefix + 0x010A)

// An unknown internal message was received by the virtual machine or container.
HCS_E_UNKNOWN_MESSAGE = windows.Errno(hcsHResultPrefix + 0x010B)

// The virtual machine or container does not support an available version of the communication protocol with the host.
HCS_E_UNSUPPORTED_PROTOCOL_VERSION = windows.Errno(hcsHResultPrefix + 0x010C)

// The virtual machine or container JSON document is invalid.
HCS_E_INVALID_JSON = windows.Errno(hcsHResultPrefix + 0x010D)

// A virtual machine or container with the specified identifier does not exist.
HCS_E_SYSTEM_NOT_FOUND = windows.Errno(hcsHResultPrefix + 0x010E)

// A virtual machine or container with the specified identifier already exists.
HCS_E_SYSTEM_ALREADY_EXISTS = windows.Errno(hcsHResultPrefix + 0x010F)

// The virtual machine or container with the specified identifier is not running.
HCS_E_SYSTEM_ALREADY_STOPPED = windows.Errno(hcsHResultPrefix + 0x0110)

// A communication protocol error has occurred between the virtual machine or container and the host.
HCS_E_PROTOCOL_ERROR = windows.Errno(hcsHResultPrefix + 0x0111)

// The container image contains a layer with an unrecognized format.
HCS_E_INVALID_LAYER = windows.Errno(hcsHResultPrefix + 0x0112)

// To use this container image, you must join the Windows Insider Program.
// Please see https://go.microsoft.com/fwlink/?linkid=850659 for more information.
HCS_E_WINDOWS_INSIDER_REQUIRED = windows.Errno(hcsHResultPrefix + 0x0113)

// The operation could not be started because a required feature is not installed.
HCS_E_SERVICE_NOT_AVAILABLE = windows.Errno(hcsHResultPrefix + 0x0114)

// The operation has not started.
HCS_E_OPERATION_NOT_STARTED = windows.Errno(hcsHResultPrefix + 0x0115)

// The operation is already running.
HCS_E_OPERATION_ALREADY_STARTED = windows.Errno(hcsHResultPrefix + 0x0116)

// The operation is still running.
HCS_E_OPERATION_PENDING = windows.Errno(hcsHResultPrefix + 0x0117)

// The operation did not complete in time.
HCS_E_OPERATION_TIMEOUT = windows.Errno(hcsHResultPrefix + 0x0118)

// An event callback has already been registered on this handle.
HCS_E_OPERATION_SYSTEM_CALLBACK_ALREADY_SET = windows.Errno(hcsHResultPrefix + 0x0119)

// Not enough memory available to return the result of the operation.
HCS_E_OPERATION_RESULT_ALLOCATION_FAILED = windows.Errno(hcsHResultPrefix + 0x011A)

// Insufficient privileges.
// Only administrators or users that are members of the Hyper-V Administrators user group are permitted to access virtual machines or containers.
// To add yourself to the Hyper-V Administrators user group, please see https://aka.ms/hcsadmin for more information.
HCS_E_ACCESS_DENIED = windows.Errno(hcsHResultPrefix + 0x011B)

// The virtual machine or container reported a critical error and was stopped or restarted.
HCS_E_GUEST_CRITICAL_ERROR = windows.Errno(hcsHResultPrefix + 0x011C)

// The process information is not available.
HCS_E_PROCESS_INFO_NOT_AVAILABLE = windows.Errno(hcsHResultPrefix + 0x011D)

// The host compute system service has disconnected unexpectedly.
HCS_E_SERVICE_DISCONNECT = windows.Errno(hcsHResultPrefix + 0x011E)

// The process has already exited.
HCS_E_PROCESS_ALREADY_STOPPED = windows.Errno(hcsHResultPrefix + 0x011F)

// The virtual machine or container is not configured to perform the operation.
HCS_E_SYSTEM_NOT_CONFIGURED_FOR_OPERATION = windows.Errno(hcsHResultPrefix + 0x0120)

// The operation has already been cancelled.
HCS_E_OPERATION_ALREADY_CANCELLED = windows.Errno(hcsHResultPrefix + 0x0121)
)
2 changes: 1 addition & 1 deletion internal/computecore/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type HCSEvent struct {
EventData *uint16
// Handle to a completed operation, if Type is eventOperationCallback.
// This is only possible when HcsSetComputeSystemCallback has specified event option HcsEventOptionEnableOperationCallbacks.
Operation HCSOperation
Operation hcsOperation
}

//go:generate go run golang.org/x/tools/cmd/stringer -type=HCSEventType -trimprefix=Event event.go
Expand Down
Loading

0 comments on commit 19311c0

Please sign in to comment.