Improve darwin tun performance
This commit is contained in:
parent
7812930a48
commit
aa1fd4d994
16 changed files with 192 additions and 115 deletions
|
|
@ -6,16 +6,43 @@ import (
|
|||
"github.com/sagernet/gvisor/pkg/tcpip/link/qdisc/fifo"
|
||||
"github.com/sagernet/gvisor/pkg/tcpip/stack"
|
||||
"github.com/sagernet/sing-tun/internal/fdbased_darwin"
|
||||
"github.com/sagernet/sing-tun/internal/rawfile_darwin"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ GVisorTun = (*NativeTun)(nil)
|
||||
|
||||
func (t *NativeTun) WritePacket(pkt *stack.PacketBuffer) (int, error) {
|
||||
iovecs := t.iovecsOutputDefault
|
||||
var dataLen int
|
||||
for _, packetSlice := range pkt.AsSlices() {
|
||||
dataLen += len(packetSlice)
|
||||
iovec := unix.Iovec{
|
||||
Base: &packetSlice[0],
|
||||
}
|
||||
iovec.SetLen(len(packetSlice))
|
||||
iovecs = append(iovecs, iovec)
|
||||
}
|
||||
if cap(iovecs) > cap(t.iovecsOutputDefault) {
|
||||
t.iovecsOutputDefault = iovecs[:0]
|
||||
}
|
||||
errno := rawfile.NonBlockingWriteIovec(t.tunFd, iovecs)
|
||||
if errno == 0 {
|
||||
return dataLen, nil
|
||||
} else {
|
||||
return 0, errno
|
||||
}
|
||||
}
|
||||
|
||||
func (t *NativeTun) NewEndpoint() (stack.LinkEndpoint, stack.NICOptions, error) {
|
||||
ep, err := fdbased.New(&fdbased.Options{
|
||||
FDs: []int{t.tunFd},
|
||||
MTU: t.options.MTU,
|
||||
RXChecksumOffload: true,
|
||||
PacketDispatchMode: fdbased.RecvMMsg,
|
||||
FDs: []int{t.tunFd},
|
||||
MTU: t.options.MTU,
|
||||
RXChecksumOffload: true,
|
||||
PacketDispatchMode: fdbased.RecvMMsg,
|
||||
MultiPendingPackets: t.options.EXP_MultiPendingPackets,
|
||||
SendMsgX: t.options.EXP_SendMsgX,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, stack.NICOptions{}, err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue