Skip to content

Commit 666a1d8

Browse files
committed
vmnet: Add doc comments to *FileAdaptorForInterfaces
Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent 7420c05 commit 666a1d8

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

vmnet/datagram_darwin.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ import (
2020
// - Invoke the returned function in a separate goroutine to start packet forwarding between the vmnet interface and the file.
2121
// - The context can be used to stop the goroutines and the interface.
2222
// - The returned error channel can be used to receive errors from the goroutines.
23+
// - The connection closure is reported as [io.EOF] error or [syscall.ECONNRESET] error in the error channel.
24+
//
25+
// The returned file can be used as a datagram file descriptor for QEMU, krunkit, or VZ.
2326
//
24-
// The returned file can be used as a file descriptor for QEMU's netdev datagram backend or VZ's [NewFileHandleNetworkDeviceAttachment]
2527
// QEMU:
2628
//
2729
// -netdev datagram,id=net0,addr.type=fd,addr.str=<file descriptor>
30+
// -netdev tap,id=net1,fd=<file descriptor>
31+
//
32+
// krunkit:
33+
//
34+
// --device virtio-net,type=unixgram,fd=<file descriptor>,offloading=on // offloading=on is recommended. See krunkit driver in LIMA.
2835
//
2936
// VZ:
3037
//

vmnet/fileadapter_darwin.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ type PacketForwarder[T io.Closer] interface {
7272
}
7373

7474
// FileAdaptorForInterface is a generic function that returns a file for the given [Network].
75-
// The returned file is used as a file descriptor for network devices in QEMU or Virtualization frameworks.
75+
// The returned file is used as a file descriptor for network devices in QEMU, krunkit, or Virtualization frameworks.
7676
// - Invoke the returned function in a separate goroutine to start packet forwarding between the vmnet interface and the file.
7777
// - The context can be used to stop the goroutines and the interface.
7878
// - The returned error channel can be used to receive errors from the goroutines.
79+
// - The connection closure is reported as [io.EOF] error or [syscall.ECONNRESET] error in the error channel.
7980
func FileAdaptorForInterface[T PacketForwarder[U], U io.Closer](ctx context.Context, iface *Interface, opts ...Sockopt) (file *os.File, start func(), errCh <-chan error, err error) {
8081
var factory T
8182
forwarder := factory.New()
@@ -106,6 +107,10 @@ func FileAdaptorForInterface[T PacketForwarder[U], U io.Closer](ctx context.Cont
106107
errChRW := make(chan error, 10)
107108
reportError := func(err error, message string) {
108109
if err != nil {
110+
if strings.Contains(err.Error(), "use of closed network connection") {
111+
// Silently ignore the error caused by connection closure
112+
return
113+
}
109114
errChRW <- fmt.Errorf("%s: %w", message, err)
110115
}
111116
}
@@ -144,12 +149,10 @@ func FileAdaptorForInterface[T PacketForwarder[U], U io.Closer](ctx context.Cont
144149
// Read all available packets in a loop.
145150
for {
146151
// Read packets from the connection to writeDescs
152+
// It won't return until at least one packet is read or connection is closed.
153+
// Remote closure may be detected as io.EOF on stream connection.
147154
packetCount, err := forwarder.ReadPacketsFromConn(conn)
148155
if err != nil {
149-
if strings.Contains(err.Error(), "use of closed network connection") {
150-
// Normal closure
151-
break
152-
}
153156
reportError(err, "forwarder.ReadPacketsFromConn failed")
154157
break
155158
}

vmnet/msg_x_darwin.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ import (
2424
// - Invoke the returned function in a separate goroutine to start packet forwarding between the vmnet interface and the file.
2525
// - The context can be used to stop the goroutines and the interface.
2626
// - The returned error channel can be used to receive errors from the goroutines.
27+
// - The connection closure is reported as [io.EOF] error or [syscall.ECONNRESET] error in the error channel.
28+
//
29+
// The returned file can be used as a datagram file descriptor for QEMU, krunkit, or VZ.
2730
//
28-
// The returned file can be used as a file descriptor for QEMU's netdev datagram backend or VZ's [NewFileHandleNetworkDeviceAttachment]
2931
// QEMU:
3032
//
3133
// -netdev datagram,id=net0,addr.type=fd,addr.str=<file descriptor>
34+
// -netdev tap,id=net1,fd=<file descriptor>
35+
//
36+
// krunkit:
37+
//
38+
// --device virtio-net,type=unixgram,fd=<file descriptor>,offloading=on // offloading=on is recommended. See krunkit driver in LIMA.
3239
//
3340
// VZ:
3441
//

vmnet/stream_darwin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
// - Invoke the returned function in a separate goroutine to start packet forwarding between the vmnet interface and the file.
2424
// - The context can be used to stop the goroutines and the interface.
2525
// - The returned error channel can be used to receive errors from the goroutines.
26+
// - The connection closure is reported as [io.EOF] error or [syscall.ECONNRESET] error in the error channel.
2627
//
2728
// The returned file can be used as a file descriptor for QEMU's netdev stream or socket backend.
2829
//

0 commit comments

Comments
 (0)