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
Copy link
Copy Markdown

@lixin9311 lixin9311 commented May 26, 2026

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
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.

1 participant