Skip to content

Commit 3544e5a

Browse files
committed
Fix usages of readmsg_x
1 parent c6a5f77 commit 3544e5a

5 files changed

Lines changed: 15 additions & 131 deletions

File tree

internal/fdbased_darwin/endpoint.go

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,12 @@ func New(opts *Options) (stack.LinkEndpoint, error) {
285285
return nil, fmt.Errorf("unix.SetNonblock(%v) failed: %v", fd, err)
286286
}
287287

288-
isSocket, err := isSocketFD(fd)
289-
if err != nil {
290-
return nil, err
291-
}
292-
e.fds = append(e.fds, fdInfo{fd: fd, isSocket: isSocket})
288+
e.fds = append(e.fds, fdInfo{fd: fd, isSocket: true})
293289
if opts.ProcessorsPerChannel == 0 {
294290
opts.ProcessorsPerChannel = common.Max(1, runtime.GOMAXPROCS(0)/len(opts.FDs))
295291
}
296292

297-
inboundDispatcher, err := createInboundDispatcher(e, fd, isSocket, opts)
293+
inboundDispatcher, err := newRecvMMsgDispatcher(fd, e, opts)
298294
if err != nil {
299295
return nil, fmt.Errorf("createInboundDispatcher(...) = %v", err)
300296
}
@@ -304,40 +300,6 @@ func New(opts *Options) (stack.LinkEndpoint, error) {
304300
return e, nil
305301
}
306302

307-
func createInboundDispatcher(e *endpoint, fd int, isSocket bool, opts *Options) (linkDispatcher, error) {
308-
// By default use the readv() dispatcher as it works with all kinds of
309-
// FDs (tap/tun/unix domain sockets and af_packet).
310-
inboundDispatcher, err := newReadVDispatcher(fd, e, opts)
311-
if err != nil {
312-
return nil, fmt.Errorf("newReadVDispatcher(%d, %+v) = %v", fd, e, err)
313-
}
314-
315-
if isSocket {
316-
switch e.packetDispatchMode {
317-
case RecvMMsg:
318-
// If the provided FD is a socket then we optimize
319-
// packet reads by using recvmmsg() instead of read() to
320-
// read packets in a batch.
321-
inboundDispatcher, err = newRecvMMsgDispatcher(fd, e, opts)
322-
if err != nil {
323-
return nil, fmt.Errorf("newRecvMMsgDispatcher(%d, %+v) = %v", fd, e, err)
324-
}
325-
case Readv:
326-
default:
327-
return nil, fmt.Errorf("unknown dispatch mode %d", e.packetDispatchMode)
328-
}
329-
}
330-
return inboundDispatcher, nil
331-
}
332-
333-
func isSocketFD(fd int) (bool, error) {
334-
var stat unix.Stat_t
335-
if err := unix.Fstat(fd, &stat); err != nil {
336-
return false, fmt.Errorf("unix.Fstat(%v,...) failed: %v", fd, err)
337-
}
338-
return (stat.Mode & unix.S_IFSOCK) == unix.S_IFSOCK, nil
339-
}
340-
341303
// Attach launches the goroutine that reads packets from the file descriptor and
342304
// dispatches them via the provided dispatcher. If one is already attached,
343305
// then nothing happens.

internal/fdbased_darwin/packet_dispatchers.go

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -79,70 +79,6 @@ func (b *iovecBuffer) release() {
7979
}
8080
}
8181

82-
// readVDispatcher uses readv() system call to read inbound packets and
83-
// dispatches them.
84-
//
85-
// +stateify savable
86-
type readVDispatcher struct {
87-
stopfd.StopFD
88-
// fd is the file descriptor used to send and receive packets.
89-
fd int
90-
91-
// e is the endpoint this dispatcher is attached to.
92-
e *endpoint
93-
94-
// buf is the iovec buffer that contains the packet contents.
95-
buf *iovecBuffer
96-
97-
// mgr is the processor goroutine manager.
98-
mgr *processorManager
99-
}
100-
101-
func newReadVDispatcher(fd int, e *endpoint, opts *Options) (linkDispatcher, error) {
102-
stopFD, err := stopfd.New()
103-
if err != nil {
104-
return nil, err
105-
}
106-
d := &readVDispatcher{
107-
StopFD: stopFD,
108-
fd: fd,
109-
e: e,
110-
}
111-
d.buf = newIovecBuffer(opts.MTU)
112-
d.mgr = newProcessorManager(opts, e)
113-
d.mgr.start()
114-
return d, nil
115-
}
116-
117-
func (d *readVDispatcher) release() {
118-
d.buf.release()
119-
d.mgr.close()
120-
}
121-
122-
// dispatch reads one packet from the file descriptor and dispatches it.
123-
func (d *readVDispatcher) dispatch() (bool, tcpip.Error) {
124-
n, errno := rawfile.BlockingReadvUntilStopped(d.ReadFD, d.fd, d.buf.nextIovecs())
125-
if n <= 0 || errno != 0 {
126-
return false, TranslateErrno(errno)
127-
}
128-
129-
payload := d.buf.pullBuffer(n)
130-
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
131-
Payload: payload,
132-
})
133-
defer pkt.DecRef()
134-
135-
d.e.mu.RLock()
136-
addr := d.e.addr
137-
d.e.mu.RUnlock()
138-
if !d.e.parseInboundHeader(pkt, addr) {
139-
return false, nil
140-
}
141-
d.mgr.queuePacket(pkt, d.e.hdrSize > 0)
142-
d.mgr.wakeReady()
143-
return true, nil
144-
}
145-
14682
// recvMMsgDispatcher uses the recvmmsg system call to read inbound packets and
14783
// dispatches them.
14884
//
@@ -216,7 +152,7 @@ func (d *recvMMsgDispatcher) dispatch() (bool, tcpip.Error) {
216152
for k := range d.msgHdrs {
217153
iovecs := d.bufs[k].nextIovecs()
218154
iovLen := len(iovecs)
219-
d.msgHdrs[k].DataLen = 0
155+
d.msgHdrs[k] = rawfile.MsgHdrX{}
220156
d.msgHdrs[k].Msg.Iov = &iovecs[0]
221157
d.msgHdrs[k].Msg.SetIovlen(iovLen)
222158
}

stack_mixed.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
package tun
44

55
import (
6-
"errors"
7-
"os"
8-
96
"github.com/sagernet/gvisor/pkg/buffer"
107
"github.com/sagernet/gvisor/pkg/tcpip"
118
gHdr "github.com/sagernet/gvisor/pkg/tcpip/header"
@@ -81,9 +78,8 @@ func (m *Mixed) tunLoop() {
8178
}
8279
}
8380
if darwinTUN, isDarwinTUN := m.tun.(DarwinTUN); isDarwinTUN {
84-
if m.batchLoopDarwin(darwinTUN) {
85-
return
86-
}
81+
m.batchLoopDarwin(darwinTUN)
82+
return
8783
}
8884
packetBuffer := make([]byte, m.mtu+PacketOffset)
8985
for {
@@ -167,15 +163,13 @@ func (m *Mixed) batchLoopLinux(linuxTUN LinuxTUN, batchSize int) {
167163
}
168164
}
169165

170-
func (m *Mixed) batchLoopDarwin(darwinTUN DarwinTUN) bool {
166+
func (m *Mixed) batchLoopDarwin(darwinTUN DarwinTUN) {
171167
var writeBuffers []*buf.Buffer
172168
for {
173169
buffers, err := darwinTUN.BatchRead()
174170
if err != nil {
175-
if errors.Is(err, os.ErrInvalid) {
176-
return false
177-
} else if E.IsClosed(err) {
178-
return true
171+
if E.IsClosed(err) {
172+
return
179173
}
180174
m.logger.Error(E.Cause(err, "batch read packet"))
181175
}

stack_system.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"net"
77
"net/netip"
8-
"os"
98
"syscall"
109
"time"
1110

@@ -176,9 +175,8 @@ func (s *System) tunLoop() {
176175
}
177176
}
178177
if darwinTUN, isDarwinTUN := s.tun.(DarwinTUN); isDarwinTUN {
179-
if s.batchLoopDarwin(darwinTUN) {
180-
return
181-
}
178+
s.batchLoopDarwin(darwinTUN)
179+
return
182180
}
183181
packetBuffer := make([]byte, s.mtu+PacketOffset)
184182
for {
@@ -262,15 +260,13 @@ func (s *System) batchLoopLinux(linuxTUN LinuxTUN, batchSize int) {
262260
}
263261
}
264262

265-
func (s *System) batchLoopDarwin(darwinTUN DarwinTUN) bool {
263+
func (s *System) batchLoopDarwin(darwinTUN DarwinTUN) {
266264
var writeBuffers []*buf.Buffer
267265
for {
268266
buffers, err := darwinTUN.BatchRead()
269267
if err != nil {
270-
if errors.Is(err, os.ErrInvalid) {
271-
return false
272-
} else if E.IsClosed(err) {
273-
return true
268+
if E.IsClosed(err) {
269+
return
274270
}
275271
s.logger.Error(E.Cause(err, "batch read packet"))
276272
}

tun_darwin.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (t *NativeTun) BatchSize() int {
354354
func (t *NativeTun) BatchRead() ([]*buf.Buffer, error) {
355355
for i := 0; i < t.batchSize; i++ {
356356
iovecs := t.iovecs[i].nextIovecs()
357-
t.msgHdrs[i].DataLen = 0
357+
t.msgHdrs[i] = rawfile.MsgHdrX{}
358358
t.msgHdrs[i].Msg.Iov = &iovecs[0]
359359
t.msgHdrs[i].Msg.Iovlen = 2
360360
}
@@ -365,11 +365,7 @@ func (t *NativeTun) BatchRead() ([]*buf.Buffer, error) {
365365
t.iovecs[k].buffer = nil
366366
}
367367
t.buffers = t.buffers[:0]
368-
if errors.Is(errno, unix.EINVAL) {
369-
return nil, os.ErrInvalid
370-
} else {
371-
return nil, errno
372-
}
368+
return nil, errno
373369
}
374370
if n < 1 {
375371
return nil, nil

0 commit comments

Comments
 (0)