Skip to content

Add VZVmnetNetworkDeviceAttachment (macOS 26+)#218

Open
lixin9311 wants to merge 1 commit into
Code-Hex:mainfrom
lixin9311:feat/vmnet-network-device-attachment
Open

Add VZVmnetNetworkDeviceAttachment (macOS 26+)#218
lixin9311 wants to merge 1 commit into
Code-Hex:mainfrom
lixin9311:feat/vmnet-network-device-attachment

Conversation

@lixin9311

@lixin9311 lixin9311 commented May 26, 2026

Copy link
Copy Markdown

Description

Wraps the new VZVmnetNetworkDeviceAttachment class added to Virtualization.framework in macOS 26.

This is a simplified version of #205. This PR does not try to provide a full vmnet package ambitiously.

The goal is simple, we want to solve the weirdness of Apple's DHCP server and pin my VM with a stable static IP.

More features of vmnet should be added progressively.

@lixin9311 lixin9311 marked this pull request as draft May 26, 2026 10:01
@lixin9311 lixin9311 force-pushed the feat/vmnet-network-device-attachment branch from c943cab to afba220 Compare May 26, 2026 10:25
Wraps the new VZVmnetNetworkDeviceAttachment class added in macOS 26
and exposes the underlying vmnet_network configuration surface as a
new `vmnet` subpackage so callers can drive vmnet_network_create
with mode, IPv4 subnet, and DHCP reservations.

  package vmnet (new):

      type Mode int
      const (
          HostMode    Mode = 1000
          SharedMode  Mode = 1001
          BridgedMode Mode = 1002
      )

      type NetworkConfiguration struct { *objc.Pointer }
      func NewNetworkConfiguration(mode Mode) (*NetworkConfiguration, error)
      func (*NetworkConfiguration) SetIPv4Subnet(netip.Prefix) error
      func (*NetworkConfiguration) AddDhcpReservation(net.HardwareAddr, netip.Addr) error

      type Network struct { *objc.Pointer }
      func NewNetwork(config *NetworkConfiguration) (*Network, error)
      func NewNetworkFromPointer(*objc.Pointer) *Network

  package vz (additions in network.go):

      type VmnetNetworkDeviceAttachment struct {
          *pointer
          *baseNetworkDeviceAttachment
      }
      func (*VmnetNetworkDeviceAttachment) String() string
      func (*VmnetNetworkDeviceAttachment) Network() *vmnet.Network
      func NewVmnetNetworkDeviceAttachment(*vmnet.Network) (*VmnetNetworkDeviceAttachment, error)

NetworkConfiguration.SetIPv4Subnet normalizes its netip.Prefix
argument to the gateway IP (first host of the network) before
calling vmnet_network_configuration_set_ipv4_subnet. Apple's docs
call that parameter "subnet_addr" but the actual semantic —
confirmed via vmnet_network_get_ipv4_subnet round-trip — is the
gateway IP, matching the older `vmnet_start_address_key` XPC key.

VZVmnetNetworkDeviceAttachment.network is a C-typed
`@property (readonly) vmnet_network_ref`. ObjC properties for
non-NSObject C types default to `assign` semantics, so the
attachment does not retain. Lifecycle:

  - vmnet_network_create returns +1 retained — owned by the Go
    *vmnet.Network wrapper, which CFReleases on GC finalize.
  - The VZ attachment stores the same pointer without retaining;
    callers must keep the *vmnet.Network alive while the attachment
    is in use (it's stored on the VirtualMachineConfiguration, so
    the typical orchestration code keeps it reachable).
  - Network() reads VZ's stored pointer, CFRetain's it, and returns
    a fresh *vmnet.Network wrapper that owns its own refcount.

SDK compile-time guard via the existing INCLUDE_TARGET_OSX_26 macro
in virtualization_helper.h; runtime guard via @available(macOS 26,
*) in the Obj-C wrappers; macOS-version + build-target guards on
the Go side via the existing osversion helpers.

Header dependency note: VZVmnetNetworkDeviceAttachment.h references
`vmnet_network_ref` via a @Property, so every compilation unit that
ends up pulling Virtualization.h needs <vmnet/vmnet.h> in scope
first. Added the include to virtualization_helper.h (imported by
every per-version header).
@lixin9311 lixin9311 force-pushed the feat/vmnet-network-device-attachment branch from afba220 to 20274db Compare May 26, 2026 10:29
@lixin9311 lixin9311 marked this pull request as ready for review May 26, 2026 11:08
@AkihiroSuda AkihiroSuda requested a review from Copilot June 25, 2026 03:57
@AkihiroSuda

Copy link
Copy Markdown
Collaborator

cc @norio-nomura (author of #205)

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

This PR adds support for macOS 26’s new VZVmnetNetworkDeviceAttachment by introducing a small vmnet subpackage that can create/configure a vmnet_network_ref (IPv4 subnet + DHCP reservations) and then wrapping that network into a Virtualization.framework attachment.

Changes:

  • Added a new vmnet package with CGO wrappers for vmnet_network_configuration_* and vmnet_network_* APIs (macOS 26+).
  • Added vz.VmnetNetworkDeviceAttachment plus a getter for the underlying vmnet.Network.
  • Updated build-target gating and headers to account for macOS 26 SDK/runtime availability.

Reviewed changes

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

Show a summary per file
File Description
vmnet/vmnet.go Go-facing vmnet configuration/network API and lifetime management via finalizers.
vmnet/cgo.m Objective-C/C wrappers around the new vmnet_network APIs with runtime availability checks.
vmnet/cgo.h C header for vmnet wrapper functions plus SDK gating macro.
virtualization_helper.h Ensures <vmnet/vmnet.h> is visible before including Virtualization headers; adds macOS 26 SDK gate.
virtualization_26.m Adds wrappers for creating VZVmnetNetworkDeviceAttachment and reading back its network.
virtualization_26.h Declares macOS 26 wrapper functions for VZVmnetNetworkDeviceAttachment.
osversion.go Adds __MAC_26_0 build-target mapping for macOSBuildTargetAvailable.
network.go Links vmnet framework and adds VmnetNetworkDeviceAttachment constructor + network accessor.

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

Comment thread vmnet/vmnet.go
Comment on lines +80 to +88
if !subnet.Addr().Is4() {
return fmt.Errorf("vmnet: SetIPv4Subnet requires an IPv4 prefix, got %s", subnet)
}
if !subnet.IsValid() {
return errors.New("vmnet: SetIPv4Subnet got an invalid Prefix")
}
// Normalize: gateway is the first host of the network.
gateway := subnet.Masked().Addr().Next()
mask := net.IP(net.CIDRMask(subnet.Bits(), 32)).String()
Comment thread vmnet/vmnet.go
Comment on lines +108 to +110
if !reservation.Is4() {
return fmt.Errorf("vmnet: AddDhcpReservation requires an IPv4 reservation, got %s", reservation)
}
Comment thread vmnet/vmnet.go
Comment on lines +133 to +135
func NewNetwork(config *NetworkConfiguration) (*Network, error) {
var status C.int
ptr := C.newNetwork(objc.Ptr(config), &status)
Comment thread network.go
Comment on lines +280 to +284
type VmnetNetworkDeviceAttachment struct {
*pointer

*baseNetworkDeviceAttachment
}
Comment thread network.go
Comment on lines +304 to +318
func NewVmnetNetworkDeviceAttachment(network *vmnet.Network) (*VmnetNetworkDeviceAttachment, error) {
if err := macOSAvailable(26); err != nil {
return nil, err
}

attachment := &VmnetNetworkDeviceAttachment{
pointer: objc.NewPointer(
C.newVZVmnetNetworkDeviceAttachment(objc.Ptr(network)),
),
}
objc.SetFinalizer(attachment, func(self *VmnetNetworkDeviceAttachment) {
objc.Release(self)
})
return attachment, nil
}
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