Fix darwin writev

This commit is contained in:
世界
2025-07-20 16:15:25 +08:00
parent 3af7305b85
commit 0310956cc0
3 changed files with 15 additions and 13 deletions

2
go.mod
View File

@@ -9,7 +9,7 @@ require (
github.com/sagernet/gvisor v0.0.0-20241123041152-536d05261cff
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a
github.com/sagernet/nftables v0.3.0-beta.4
github.com/sagernet/sing v0.6.0-beta.2
github.com/sagernet/sing v0.7.0-beta.1
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.26.0

4
go.sum
View File

@@ -22,8 +22,8 @@ github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZN
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/nftables v0.3.0-beta.4 h1:kbULlAwAC3jvdGAC1P5Fa3GSxVwQJibNenDW2zaXr8I=
github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8=
github.com/sagernet/sing v0.6.0-beta.2 h1:Dcutp3kxrsZes9q3oTiHQhYYjQvDn5rwp1OI9fDLYwQ=
github.com/sagernet/sing v0.6.0-beta.2/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing v0.7.0-beta.1 h1:2D44KzgeDZwD/R4Ts8jwSUHTRR238a1FpXDrl7l4tVw=
github.com/sagernet/sing v0.7.0-beta.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=

View File

@@ -40,7 +40,7 @@ type NativeTun struct {
inet4Address [4]byte
inet6Address [16]byte
routeSet bool
writeMsgX bool
sendMsgX bool
}
type iovecBuffer struct {
@@ -64,8 +64,7 @@ func (b *iovecBuffer) nextIovecs() []unix.Iovec {
}
if b.buffer == nil {
b.buffer = buf.NewSize(b.mtu)
b.iovecs[1].Base = &b.buffer.FreeBytes()[0]
b.iovecs[1].SetLen(b.mtu)
b.iovecs[1] = b.buffer.Iovec(b.buffer.Cap())
}
return b.iovecs
}
@@ -77,8 +76,7 @@ func (b *iovecBuffer) nextIovecsOutput(buffer *buf.Buffer) []unix.Iovec {
case header.IPv6Version:
b.iovecs[0] = packetHeaderVec6
}
b.iovecs[1].Base = &buffer.Bytes()[0]
b.iovecs[1].SetLen(buffer.Len())
b.iovecs[1] = buffer.Iovec(buffer.Len())
return b.iovecs
}
@@ -132,7 +130,7 @@ func New(options Options) (Tun, error) {
msgHdrs: make([]rawfile.MsgHdrX, batchSize),
msgHdrsOutput: make([]rawfile.MsgHdrX, batchSize),
stopFd: common.Must1(stopfd.New()),
writeMsgX: options.EXP_SendMsgX,
sendMsgX: options.EXP_SendMsgX,
}
for i := 0; i < batchSize; i++ {
nativeTun.iovecs[i] = newIovecBuffer(int(options.MTU))
@@ -364,7 +362,7 @@ func (t *NativeTun) BatchRead() ([]*buf.Buffer, error) {
}
func (t *NativeTun) BatchWrite(buffers []*buf.Buffer) error {
if !t.writeMsgX {
if !t.sendMsgX {
for i, buffer := range buffers {
t.iovecsOutput[i].nextIovecsOutput(buffer)
}
@@ -381,9 +379,13 @@ func (t *NativeTun) BatchWrite(buffers []*buf.Buffer) error {
t.msgHdrsOutput[i].Msg.Iov = &iovecs[0]
t.msgHdrsOutput[i].Msg.Iovlen = 2
}
_, errno := rawfile.NonBlockingSendMMsg(t.tunFd, t.msgHdrsOutput[:len(buffers)])
if errno != 0 {
return errno
var n int
for n != len(buffers) {
sent, errno := rawfile.NonBlockingSendMMsg(t.tunFd, t.msgHdrsOutput[n:len(buffers)])
if errno != 0 {
return errno
}
n += sent
}
}
return nil