Skip to content

Add vsock support for MacOS VM#60

Open
Fred78290 wants to merge 4 commits into
mdlayher:mainfrom
Fred78290:main
Open

Add vsock support for MacOS VM#60
Fred78290 wants to merge 4 commits into
mdlayher:mainfrom
Fred78290:main

Conversation

@Fred78290

Copy link
Copy Markdown

Since MacOS 13, it is able to virtualize MacOS with vsock support in the guest with Virtualization framework.

This PR propose to enable vsock support inside MacOS VM.

VSock on host is not yet available.

@SuperQ SuperQ left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This needs a rebase.

Copilot AI review requested due to automatic review settings May 26, 2026 07:16
@Fred78290

Copy link
Copy Markdown
Author

@SuperQ Done

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR adds macOS (darwin) support and introduces context-aware dialing while tightening build constraints so non-implemented platforms use stubs.

Changes:

  • Exclude darwin from the !linux “others” build-tagged files and add darwin-specific conn/listener implementations.
  • Add DialWithContext and thread context.Context through the internal dial implementations.
  • Add darwin-specific tests for opError behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
vsock_others_test.go Updates build tags to exclude darwin from the non-linux test stub.
vsock_others.go Updates build tags and removes local errUnimplemented definition.
vsock_darwin_test.go Adds darwin test coverage for opError normalization.
vsock.go Adds DialWithContext, threads context into dial, and relocates errUnimplemented.
listener_darwin.go Introduces a darwin net.Listener implementation using mdlayher/socket.
fd_darwin.go Adds darwin implementations of contextID and isErrno.
conn_linux.go Updates Linux dial to accept a context and use it for Connect.
conn_darwin.go Adds darwin dial implementation using VSOCK sockets.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread fd_darwin.go
Comment on lines +10 to +20
func contextID() (uint32, error) {
if fd, err := unix.Socket(unix.AF_VSOCK, unix.SOCK_STREAM, 0); err != nil {
return 2, nil
} else {
defer unix.Close(fd)

cid, err := unix.IoctlGetInt(fd, unix.IOCTL_VM_SOCKETS_GET_LOCAL_CID)

return uint32(cid), err
}
}
Comment thread vsock.go
Comment on lines 183 to +198
func Dial(contextID, port uint32, cfg *Config) (*Conn, error) {
c, err := dial(contextID, port, cfg)
return dial(context.Background(), contextID, port, cfg)
}

// DialWithContext connects to the address on the named network using
// the provided context.
//
// The provided Context must be non-nil. If the context expires before
// the connection is complete, an error is returned. Once successfully
// connected, any expiration of the context will not affect the
// connection.
//
// See func Dial for a description of the contextID and port
// parameters.
func DialWithContext(ctx context.Context, contextID, port uint32, cfg *Config) (*Conn, error) {
c, err := dial(ctx, contextID, port, cfg)
Comment thread listener_darwin.go
Comment on lines +54 to +55
// listen is the entry point for Listen on Linux.
func listen(cid, port uint32, _ *Config) (*Listener, error) {
Comment thread listener_darwin.go
Comment on lines +90 to +91
// fileListener is the entry point for FileListener on Linux.
func fileListener(f *os.File) (*Listener, error) {
Comment thread listener_darwin.go
Comment on lines +118 to +119
// All errors should wrapped with os.SyscallError.
return nil, os.NewSyscallError("listen", unix.EINVAL)
Comment thread conn_darwin.go
// methods.
type conn = socket.Conn

// dial is the entry point for Dial on Linux.
Comment thread fd_darwin.go
Comment on lines +22 to +33
// isErrno determines if an error a matches UNIX error number.
func isErrno(err error, errno int) bool {
switch errno {
case ebadf:
return err == unix.EBADF
case enotconn:
return err == unix.ENOTCONN
default:
panicf("vsock: isErrno called with unhandled error number parameter: %d", errno)
return false
}
}
Comment thread vsock.go
Comment on lines +190 to +193
// The provided Context must be non-nil. If the context expires before
// the connection is complete, an error is returned. Once successfully
// connected, any expiration of the context will not affect the
// connection.
Comment thread vsock.go
//
// See func Dial for a description of the contextID and port
// parameters.
func DialWithContext(ctx context.Context, contextID, port uint32, cfg *Config) (*Conn, error) {
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.

3 participants