This commit is contained in:
JackDoan
2026-04-17 12:27:50 -05:00
parent bd0a63a545
commit 4a2134775d
3 changed files with 49 additions and 28 deletions

View File

@@ -355,6 +355,12 @@ func (f *Interface) listenIn(reader overlay.Queue, i int) {
} }
func (f *Interface) flushBatch(batch *sendBatch, q int) { func (f *Interface) flushBatch(batch *sendBatch, q int) {
if len(batch.bufs) == 1 {
if err := f.writers[q].WriteTo(batch.bufs[0], batch.dsts[0]); err != nil {
f.l.WithError(err).WithField("writer", q).Error("Failed to write outgoing single-batch")
}
return
}
if err := f.writers[q].WriteBatch(batch.bufs, batch.dsts); err != nil { if err := f.writers[q].WriteBatch(batch.bufs, batch.dsts); err != nil {
f.l.WithError(err).WithField("writer", q).Error("Failed to write outgoing batch") f.l.WithError(err).WithField("writer", q).Error("Failed to write outgoing batch")
} }

View File

@@ -411,7 +411,7 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, multiqueu
return nil, err return nil, err
} }
vnetHdr := true vnetHdr := true
name, err := tunSetIff(fd, nameStr, baseFlags|unix.IFF_VNET_HDR) name, err := tunSetIff(fd, nameStr, baseFlags|unix.IFF_VNET_HDR|unix.IFF_NAPI)
if err != nil { if err != nil {
_ = unix.Close(fd) _ = unix.Close(fd)
vnetHdr = false vnetHdr = false
@@ -566,7 +566,7 @@ func (t *tun) NewMultiQueueReader() (Queue, error) {
flags := uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE) flags := uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
if t.vnetHdr { if t.vnetHdr {
flags |= unix.IFF_VNET_HDR flags |= unix.IFF_VNET_HDR | unix.IFF_NAPI
} }
if _, err = tunSetIff(fd, t.Device, flags); err != nil { if _, err = tunSetIff(fd, t.Device, flags); err != nil {
_ = unix.Close(fd) _ = unix.Close(fd)

View File

@@ -31,6 +31,13 @@ type StdConn struct {
writeMsgs []rawMessage writeMsgs []rawMessage
writeIovs []iovec writeIovs []iovec
writeNames [][]byte writeNames [][]byte
// Preallocated closure + in/out slots for sendmmsg, so the hot path
// does not heap-allocate a fresh closure per call.
writeChunk int
writeSent int
writeErrno syscall.Errno
writeFunc func(fd uintptr) bool
} }
func setReusePort(network, address string, c syscall.RawConn) error { func setReusePort(network, address string, c syscall.RawConn) error {
@@ -78,6 +85,7 @@ func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch in
out.isV4 = af == unix.AF_INET out.isV4 = af == unix.AF_INET
out.prepareWriteMessages(MaxWriteBatch) out.prepareWriteMessages(MaxWriteBatch)
out.writeFunc = out.sendmmsgRawWrite
return out, nil return out, nil
} }
@@ -252,7 +260,7 @@ func (u *StdConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
if len(bufs) != len(addrs) { if len(bufs) != len(addrs) {
return fmt.Errorf("WriteBatch: len(bufs)=%d != len(addrs)=%d", len(bufs), len(addrs)) return fmt.Errorf("WriteBatch: len(bufs)=%d != len(addrs)=%d", len(bufs), len(addrs))
} }
//u.l.WithField("bufs", len(bufs)).Info("WriteBatch")
i := 0 i := 0
for i < len(bufs) { for i < len(bufs) {
chunk := len(bufs) - i chunk := len(bufs) - i
@@ -301,32 +309,39 @@ func (u *StdConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
return nil return nil
} }
func (u *StdConn) sendmmsg(n int) (int, error) { // sendmmsgRawWrite is the preallocated callback passed to rawConn.Write. It
var sent int // reads its input (u.writeChunk) and writes its outputs (u.writeSent,
var sysErr error // u.writeErrno) through StdConn fields so the closure itself does not
err := u.rawConn.Write(func(fd uintptr) (done bool) { // capture per-call locals and therefore does not heap-allocate.
r1, _, errno := unix.Syscall6( func (u *StdConn) sendmmsgRawWrite(fd uintptr) bool {
unix.SYS_SENDMMSG, r1, _, errno := unix.Syscall6(
fd, unix.SYS_SENDMMSG,
uintptr(unsafe.Pointer(&u.writeMsgs[0])), fd,
uintptr(n), uintptr(unsafe.Pointer(&u.writeMsgs[0])),
0, uintptr(u.writeChunk),
0, 0,
0, 0,
) 0,
if errno == syscall.EAGAIN || errno == syscall.EWOULDBLOCK { )
return false if errno == syscall.EAGAIN || errno == syscall.EWOULDBLOCK {
} return false
sent = int(r1)
if errno != 0 {
sysErr = &net.OpError{Op: "sendmmsg", Err: errno}
}
return true
})
if err != nil {
return sent, err
} }
return sent, sysErr u.writeSent = int(r1)
u.writeErrno = errno
return true
}
func (u *StdConn) sendmmsg(n int) (int, error) {
u.writeChunk = n
u.writeSent = 0
u.writeErrno = 0
if err := u.rawConn.Write(u.writeFunc); err != nil {
return u.writeSent, err
}
if u.writeErrno != 0 {
return u.writeSent, &net.OpError{Op: "sendmmsg", Err: u.writeErrno}
}
return u.writeSent, nil
} }
// writeSockaddr encodes addr into buf (which must be at least // writeSockaddr encodes addr into buf (which must be at least