This commit is contained in:
Nate Brown 2025-04-15 21:00:50 -05:00
parent 2e2d82ca89
commit 4848cf051a
2 changed files with 72 additions and 68 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"io" "io"
"net/netip" "net/netip"
"runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -259,8 +258,6 @@ func (f *Interface) run() (func(), error) {
} }
func (f *Interface) listenOut(i int) { func (f *Interface) listenOut(i int) {
runtime.LockOSThread()
var li udp.Conn var li udp.Conn
if i > 0 { if i > 0 {
li = f.writers[i] li = f.writers[i]
@ -284,8 +281,6 @@ func (f *Interface) listenOut(i int) {
} }
func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) { func (f *Interface) listenIn(reader io.ReadWriteCloser, i int) {
runtime.LockOSThread()
packet := make([]byte, mtu) packet := make([]byte, mtu)
out := make([]byte, mtu) out := make([]byte, mtu)
fwPacket := &firewall.Packet{} fwPacket := &firewall.Packet{}

View File

@ -25,6 +25,13 @@ type StdConn struct {
isV4 bool isV4 bool
l *logrus.Logger l *logrus.Logger
batch int batch int
// cached fields for reading packets
msgs []rawMessage
buffers [][]byte
names [][]byte
n uintptr
err error
} }
func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) { func NewListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
@ -142,99 +149,101 @@ func (u *StdConn) LocalAddr() (netip.AddrPort, error) {
func (u *StdConn) ListenOut(r EncReader) { func (u *StdConn) ListenOut(r EncReader) {
var ip netip.Addr var ip netip.Addr
var n uintptr
var err error u.msgs, u.buffers, u.names = u.PrepareRawMessages(u.batch)
msgs, buffers, names := u.PrepareRawMessages(u.batch)
read := u.ReadMulti read := u.ReadMulti
if u.batch == 1 { if u.batch == 1 {
read = u.ReadSingle read = u.ReadSingle
} }
for { for {
read(msgs, &n, &err) read()
if err != nil { if u.err != nil {
u.l.WithError(err).Error("udp socket is closed, exiting read loop") //TODO: remove logging, return error
u.l.WithError(u.err).Error("udp socket is closed, exiting read loop")
return return
} }
for i := 0; i < int(n); i++ { for i := 0; i < int(u.n); i++ {
// Its ok to skip the ok check here, the slicing is the only error that can occur and it will panic // Its ok to skip the ok check here, the slicing is the only error that can occur and it will panic
if u.isV4 { if u.isV4 {
ip, _ = netip.AddrFromSlice(names[i][4:8]) ip, _ = netip.AddrFromSlice(u.names[i][4:8])
} else { } else {
ip, _ = netip.AddrFromSlice(names[i][8:24]) ip, _ = netip.AddrFromSlice(u.names[i][8:24])
} }
//u.l.Error("GOT A PACKET", msgs[i].Len) //u.l.Error("GOT A PACKET", msgs[i].Len)
r(netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(names[i][2:4])), buffers[i][:msgs[i].Len]) r(netip.AddrPortFrom(ip.Unmap(), binary.BigEndian.Uint16(u.names[i][2:4])), u.buffers[i][:u.msgs[i].Len])
} }
} }
} }
func (u *StdConn) ReadSingle(msgs []rawMessage, n *uintptr, err *error) { func (u *StdConn) ReadSingle() {
oErr := u.rc.Read(func(fd uintptr) bool { err := u.rc.Read(u.innerReadSingle)
in, _, nErr := unix.Syscall6( if u.err == nil && err != nil {
unix.SYS_RECVMSG, u.err = err
fd, u.n = 0
uintptr(unsafe.Pointer(&(msgs[0].Hdr))),
0, 0, 0, 0,
)
if nErr == syscall.EAGAIN || nErr == syscall.EINTR {
// Retry read
return false
} else if nErr != 0 {
u.l.Errorf("READING FROM UDP SINGLE had an errno %d", nErr)
*err = &net.OpError{Op: "recvmsg", Err: nErr}
*n = 0
return true
}
msgs[0].Len = uint32(in)
*n = 1
return true
})
if *err == nil && oErr != nil {
*err = oErr
*n = 0
return return
} }
} }
func (u *StdConn) ReadMulti(msgs []rawMessage, n *uintptr, err *error) { func (u *StdConn) innerReadSingle(fd uintptr) bool {
oErr := u.rc.Read(func(fd uintptr) bool { in, _, err := unix.Syscall6(
var nErr syscall.Errno unix.SYS_RECVMSG,
*n, _, nErr = unix.Syscall6( fd,
unix.SYS_RECVMMSG, uintptr(unsafe.Pointer(&(u.msgs[0].Hdr))),
fd, 0, 0, 0, 0,
uintptr(unsafe.Pointer(&(msgs[0].Hdr))), )
uintptr(len(msgs)),
unix.MSG_WAITFORONE,
0, 0,
)
if nErr == syscall.EAGAIN || nErr == syscall.EINTR { if err == syscall.EAGAIN || err == syscall.EINTR {
// Retry read // Retry read
return false return false
} else if nErr != 0 {
u.l.Errorf("READING FROM UDP MULTI had an errno %d", nErr)
*err = &net.OpError{Op: "recvmmsg", Err: nErr}
*n = 0
return true
}
} else if err != 0 {
u.l.Errorf("READING FROM UDP SINGLE had an errno %d", err)
u.err = &net.OpError{Op: "recvmsg", Err: err}
u.n = 0
return true return true
}) }
if *err == nil && oErr != nil { u.msgs[0].Len = uint32(in)
*err = oErr u.n = 1
*n = 0 return true
}
func (u *StdConn) ReadMulti() {
err := u.rc.Read(u.innerReadMulti)
if u.err == nil && err != nil {
u.err = err
u.n = 0
return return
} }
} }
func (u *StdConn) innerReadMulti(fd uintptr) bool {
var err syscall.Errno
u.n, _, err = unix.Syscall6(
unix.SYS_RECVMMSG,
fd,
uintptr(unsafe.Pointer(&u.msgs[0])),
uintptr(len(u.msgs)),
unix.MSG_WAITFORONE,
0, 0,
)
if err == syscall.EAGAIN || err == syscall.EINTR {
// Retry read
return false
} else if err != 0 {
u.l.Errorf("READING FROM UDP MULTI had an errno %d", err)
u.err = &net.OpError{Op: "recvmmsg", Err: err}
u.n = 0
return true
}
return true
}
func (u *StdConn) WriteTo(b []byte, ip netip.AddrPort) error { func (u *StdConn) WriteTo(b []byte, ip netip.AddrPort) error {
_, err := u.c.WriteToUDPAddrPort(b, ip) _, err := u.c.WriteToUDPAddrPort(b, ip)
return err return err