Skip to content

Commit

Permalink
Merge pull request #95 from Code-Hex/add/mtu
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Oct 31, 2022
2 parents 6a6b4ab + 167ee90 commit e8e1c28
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
34 changes: 34 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package vz
#cgo darwin CFLAGS: -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization.h"
# include "virtualization_13.h"
*/
import "C"
import (
Expand Down Expand Up @@ -117,6 +118,8 @@ type FileHandleNetworkDeviceAttachment struct {
*pointer

*baseNetworkDeviceAttachment

mtu int
}

var _ NetworkDeviceAttachment = (*FileHandleNetworkDeviceAttachment)(nil)
Expand All @@ -138,13 +141,44 @@ func NewFileHandleNetworkDeviceAttachment(file *os.File) (*FileHandleNetworkDevi
C.int(file.Fd()),
),
),
mtu: 1500, // The default MTU is 1500.
}
runtime.SetFinalizer(attachment, func(self *FileHandleNetworkDeviceAttachment) {
objc.Release(self)
})
return attachment, nil
}

// SetMaximumTransmissionUnit sets the maximum transmission unit (MTU) associated with this attachment.
//
// The maximum MTU allowed is 65535, and the minimum MTU allowed is 1500. An invalid MTU value will result in an invalid
// virtual machine configuration.
//
// The client side of the associated datagram socket must be properly configured with the appropriate values
// for SO_SNDBUF, and SO_RCVBUF. Set these using the setsockopt(_:_:_:_:_:) system call. The system expects
// the value of SO_RCVBUF to be at least double the value of SO_SNDBUF, and for optimal performance, the
// recommended value of SO_RCVBUF is four times the value of SO_SNDBUF.
//
// This is only supported on macOS 13 and newer, ErrUnsupportedOSVersion will
// be returned on older versions.
func (f *FileHandleNetworkDeviceAttachment) SetMaximumTransmissionUnit(mtu int) error {
if macosMajorVersionLessThan(13) {
return ErrUnsupportedOSVersion
}
C.setMaximumTransmissionUnitVZFileHandleNetworkDeviceAttachment(
objc.Ptr(f),
C.NSInteger(mtu),
)
f.mtu = mtu
return nil
}

// MaximumTransmissionUnit returns the maximum transmission unit (MTU) associated with this attachment.
// The default MTU is 1500.
func (f *FileHandleNetworkDeviceAttachment) MaximumTransmissionUnit() int {
return f.mtu
}

// NetworkDeviceAttachment for a network device attachment.
// see: https://developer.apple.com/documentation/virtualization/vznetworkdeviceattachment?language=objc
type NetworkDeviceAttachment interface {
Expand Down
47 changes: 47 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package vz_test

import (
"net"
"testing"

"github.com/Code-Hex/vz/v2"
)

func TestFileHandleNetworkDeviceAttachmentMTU(t *testing.T) {
if vz.MacosMajorVersionLessThan(13) {
t.Skip("FileHandleNetworkDeviceAttachment.SetMaximumTransmissionUnit is supported from macOS 13")
}

ln, err := net.ListenUDP("udp", &net.UDPAddr{
Port: 0,
IP: net.ParseIP("127.0.0.1"),
})
if err != nil {
t.Fatal(err)
}
defer ln.Close()

f, err := ln.File()
if err != nil {
t.Fatal(err)
}

attachment, err := vz.NewFileHandleNetworkDeviceAttachment(f)
if err != nil {
t.Fatal(err)
}
got := attachment.MaximumTransmissionUnit()
if got != 1500 {
t.Fatalf("want default mtu 1500 but got %d", got)
}

want := 2000
if err := attachment.SetMaximumTransmissionUnit(want); err != nil {
t.Fatal(err)
}

got2 := attachment.MaximumTransmissionUnit()
if got2 != want {
t.Fatalf("want mtu %d but got %d", want, got)
}
}
4 changes: 3 additions & 1 deletion virtualization_13.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ const char *getSpiceAgentPortName();

void startWithOptionsCompletionHandler(void *machine, void *queue, void *options, void *completionHandler);

const char *getMacOSGuestAutomountTag();
const char *getMacOSGuestAutomountTag();

void setMaximumTransmissionUnitVZFileHandleNetworkDeviceAttachment(void *attachment, NSInteger mtu);
23 changes: 23 additions & 0 deletions virtualization_13.m
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,27 @@ void startWithOptionsCompletionHandler(void *machine, void *queue, void *options
}
#endif
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract The maximum transmission unit (MTU) associated with this attachment.
@discussion
The client side of the associated datagram socket must be properly configured with the appropriate values for
`SO_SNDBUF`, and `SO_RCVBUF`, which can be set using the `setsockopt` system call. The value of `SO_RCVBUF` is
expected to be at least double the value of `SO_SNDBUF`, and for optimal performance, the value of `SO_RCVBUF`
is recommended to be four times the value of `SO_SNDBUF`.
The default MTU is 1500.
The maximum MTU allowed is 65535, and the minimum MTU allowed is 1500. An invalid MTU value will result in an invalid
virtual machine configuration.
*/
void setMaximumTransmissionUnitVZFileHandleNetworkDeviceAttachment(void *attachment, NSInteger mtu)
{
#ifdef INCLUDE_TARGET_OSX_13
if (@available(macOS 13, *)) {
[(VZFileHandleNetworkDeviceAttachment *)attachment setMaximumTransmissionUnit:mtu];
return;
}
#endif
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

0 comments on commit e8e1c28

Please sign in to comment.