Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/macOS/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require github.com/Code-Hex/vz/v3 v3.0.0-00010101000000-000000000000
require (
github.com/Code-Hex/go-infinity-channel v1.0.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sys v0.36.0 // indirect
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ require (
golang.org/x/mod v0.22.0
)

require golang.org/x/sys v0.36.0 // indirect
require golang.org/x/sys v0.36.0
56 changes: 55 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package vz

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization -framework vmnet
# include "virtualization_11.h"
# include "virtualization_13.h"
# include "virtualization_26.h"
*/
import "C"
import (
Expand Down Expand Up @@ -260,6 +261,55 @@ func (f *FileHandleNetworkDeviceAttachment) MaximumTransmissionUnit() int {
return f.mtu
}

// VmnetNetworkDeviceAttachment represents a vmnet network device attachment.
//
// This attachment is used to connect a virtual machine to a vmnet network.
// The attachment is created with a VmnetNetwork and can be used with a VirtioNetworkDeviceConfiguration.
// see: https://developer.apple.com/documentation/virtualization/vzvmnetnetworkdeviceattachment?language=objc
//
// This is only supported on macOS 26 and newer, error will
// be returned on older versions.
type VmnetNetworkDeviceAttachment struct {
*pointer

*baseNetworkDeviceAttachment
}

func (*VmnetNetworkDeviceAttachment) String() string {
return "VmnetNetworkDeviceAttachment"
}

func (v *VmnetNetworkDeviceAttachment) Network() *VmnetNetwork {
network := C.VZVmnetNetworkDeviceAttachment_network(objc.Ptr(v))
return &VmnetNetwork{
pointer: objc.NewPointer(network),
}
}

var _ NetworkDeviceAttachment = (*VmnetNetworkDeviceAttachment)(nil)

// NewVmnetNetworkDeviceAttachment creates a new VmnetNetworkDeviceAttachment with network.
//
// This is only supported on macOS 26 and newer, error will
// be returned on older versions.
func NewVmnetNetworkDeviceAttachment(network *VmnetNetwork) (*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
}

// NetworkDeviceAttachment for a network device attachment.
// see: https://developer.apple.com/documentation/virtualization/vznetworkdeviceattachment?language=objc
type NetworkDeviceAttachment interface {
Expand Down Expand Up @@ -373,6 +423,10 @@ func (m *MACAddress) String() string {
return cstring.String()
}

func (m *MACAddress) ethernetAddress() C.ether_addr_t {
return C.getVZMACAddressEthernetAddress(objc.Ptr(m))
}

func (m *MACAddress) HardwareAddr() net.HardwareAddr {
hw, _ := net.ParseMAC(m.String())
return hw
Expand Down
19 changes: 0 additions & 19 deletions shared_directory_arm64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,6 @@ func rosettaConfiguration(t *testing.T, o vz.LinuxRosettaCachingOptions) func(*v
}
}

func (c *Container) exec(t *testing.T, cmds ...string) {
t.Helper()
for _, cmd := range cmds {
session := c.NewSession(t)
defer session.Close()
output, err := session.CombinedOutput(cmd)
if err != nil {
if len(output) > 0 {
t.Fatalf("failed to run command %q: %v, outputs:\n%s", cmd, err, string(output))
} else {
t.Fatalf("failed to run command %q: %v", cmd, err)
}
}
if len(output) > 0 {
t.Logf("command %q outputs:\n%s", cmd, string(output))
}
}
}

// rosettad's default unix socket
const rosettadDefaultUnixSocket = "~/.cache/rosettad/uds/rosetta.sock"

Expand Down
1 change: 1 addition & 0 deletions virtualization_11.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void *newVZVirtioSocketDeviceConfiguration();
void *newVZMACAddress(const char *macAddress);
void *newRandomLocallyAdministeredVZMACAddress();
const char *getVZMACAddressString(void *macAddress);
ether_addr_t getVZMACAddressEthernetAddress(void *macAddress);
void *newVZVirtioSocketListener(uintptr_t cgoHandle);
void *VZVirtualMachine_socketDevices(void *machine);
void VZVirtioSocketDevice_setSocketListenerForPort(void *socketDevice, void *vmQueue, void *listener, uint32_t port);
Expand Down
12 changes: 12 additions & 0 deletions virtualization_11.m
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,18 @@ VZVirtioSocketConnectionFlat convertVZVirtioSocketConnection2Flat(void *connecti
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract The address represented as an ether_addr_t.
*/
ether_addr_t getVZMACAddressEthernetAddress(void *macAddress)
{
if (@available(macOS 11, *)) {
return [(VZMACAddress *)macAddress ethernetAddress];
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract Create a valid, random, unicast, locally administered address.
@discussion The generated address is not guaranteed to be unique.
Expand Down
41 changes: 41 additions & 0 deletions virtualization_26.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// virtualization_26.h
//
// Created by codehex.
//

#pragma once

// FIXME(codehex): this is dirty hack to avoid clang-format error like below
// "Configuration file(s) do(es) not support C++: /github.com/Code-Hex/vz/.clang-format"
#define NSURLComponents NSURLComponents

#import "virtualization_helper.h"
#import <Virtualization/Virtualization.h>
#import <vmnet/vmnet.h>

/* exported from cgo */

/* macOS 26 API */
// VZVmnetNetworkConfiguration
void *VZVmnetNetworkConfigurationCreate(uint32_t mode, uint32_t *status);
uint32_t VZVmnetNetworkConfiguration_setExternalInterface(void *config, const char *ifname);
void VZVmnetNetworkConfiguration_disableNat44(void *config);
void VZVmnetNetworkConfiguration_disableNat66(void *config);
void VZVmnetNetworkConfiguration_disableDhcp(void *config);
void VZVmnetNetworkConfiguration_disableDnsProxy(void *config);
void VZVmnetNetworkConfiguration_disableRouterAdvertisement(void *config);
uint32_t VZVmnetNetworkConfiguration_setIPv4Subnet(void *config, struct in_addr const *subnet_addr, struct in_addr const *subnet_mask);
uint32_t VZVmnetNetworkConfiguration_setIPv6Prefix(void *config, struct in6_addr const *prefix, uint8_t prefix_len);
uint32_t VZVmnetNetworkConfiguration_addPortForwardingRule(void *config, uint8_t protocol, sa_family_t address_family, uint16_t internal_port, uint16_t external_port, void const *internal_address);
uint32_t VZVmnetNetworkConfiguration_addDhcpReservation(void *config, ether_addr_t const *client, struct in_addr const *reservation);
uint32_t VZVmnetNetworkConfiguration_setMtu(void *config, uint32_t mtu);
// vmnet_network
void *VZVmnetNetworkCreate(void *config, uint32_t *status);
void *VZVmnetNetworkCreateWithSerialization(CFTypeRef serialization, uint32_t *status);
CFTypeRef VZVmnetNetwork_copySerialization(void *network, uint32_t *status);
void VZVmnetNetwork_getIPv6Prefix(void *network, struct in6_addr *prefix, uint8_t *prefix_len);
void VZVmnetNetwork_getIPv4Subnet(void *network, struct in_addr *subnet, struct in_addr *mask);
// VZVmnetNetworkDeviceAttachment
void *newVZVmnetNetworkDeviceAttachment(void *network);
void *VZVmnetNetworkDeviceAttachment_network(void *attachment);
Loading
Loading