fix compile for 386

This commit is contained in:
Ryan Huber
2025-11-03 10:12:02 +00:00
parent b394112ad9
commit 3dea761530

View File

@@ -18,18 +18,18 @@ import (
) )
const ( const (
ioringOpSendmsg = 9 ioringOpSendmsg = 9
ioringOpRecvmsg = 10 ioringOpRecvmsg = 10
ioringEnterGetevents = 1 << 0 ioringEnterGetevents = 1 << 0
ioringSetupClamp = 1 << 4 ioringSetupClamp = 1 << 4
ioringSetupCoopTaskrun = 1 << 8 // Kernel 5.19+: reduce thread creation ioringSetupCoopTaskrun = 1 << 8 // Kernel 5.19+: reduce thread creation
ioringSetupSingleIssuer = 1 << 12 // Kernel 6.0+: single submitter optimization ioringSetupSingleIssuer = 1 << 12 // Kernel 6.0+: single submitter optimization
ioringRegisterIowqMaxWorkers = 19 // Register opcode to limit workers ioringRegisterIowqMaxWorkers = 19 // Register opcode to limit workers
ioringOffSqRing = 0 ioringOffSqRing = 0
ioringOffCqRing = 0x8000000 ioringOffCqRing = 0x8000000
ioringOffSqes = 0x10000000 ioringOffSqes = 0x10000000
defaultIoUringEntries = 256 defaultIoUringEntries = 256
ioUringSqeSize = 64 // struct io_uring_sqe size defined by kernel ABI ioUringSqeSize = 64 // struct io_uring_sqe size defined by kernel ABI
) )
type ioSqringOffsets struct { type ioSqringOffsets struct {
@@ -170,13 +170,13 @@ type ioUringState struct {
// recvBuffer represents a single receive operation with its associated buffers // recvBuffer represents a single receive operation with its associated buffers
type recvBuffer struct { type recvBuffer struct {
payloadBuf []byte // Buffer for packet data payloadBuf []byte // Buffer for packet data
nameBuf []byte // Buffer for source address nameBuf []byte // Buffer for source address
controlBuf []byte // Buffer for control messages controlBuf []byte // Buffer for control messages
msghdr *unix.Msghdr // Message header for recvmsg msghdr *unix.Msghdr // Message header for recvmsg
iovec *unix.Iovec // IO vector pointing to payloadBuf iovec *unix.Iovec // IO vector pointing to payloadBuf
userData uint64 // User data for tracking this operation userData uint64 // User data for tracking this operation
inFlight atomic.Bool // Whether this buffer has a pending io_uring operation inFlight atomic.Bool // Whether this buffer has a pending io_uring operation
} }
// ioUringRecvState manages a dedicated io_uring for receiving packets // ioUringRecvState manages a dedicated io_uring for receiving packets
@@ -200,16 +200,16 @@ type ioUringRecvState struct {
cqRingMask *uint32 cqRingMask *uint32
cqRingEntries *uint32 cqRingEntries *uint32
mu sync.Mutex mu sync.Mutex
userData uint64 userData uint64
bufferPool []*recvBuffer // Pool of all receive buffers bufferPool []*recvBuffer // Pool of all receive buffers
bufferMap map[uint64]*recvBuffer // Map userData -> buffer bufferMap map[uint64]*recvBuffer // Map userData -> buffer
sqEntryCount uint32 sqEntryCount uint32
cqEntryCount uint32 cqEntryCount uint32
sockFd int // Socket file descriptor to receive from sockFd int // Socket file descriptor to receive from
closed atomic.Bool closed atomic.Bool
} }
func alignUint32(v, alignment uint32) uint32 { func alignUint32(v, alignment uint32) uint32 {
@@ -240,7 +240,7 @@ func newIoUringState(entries uint32) (*ioUringState, error) {
// Note: SINGLE_ISSUER causes EEXIST errors, so it's excluded // Note: SINGLE_ISSUER causes EEXIST errors, so it's excluded
flagSets := []uint32{ flagSets := []uint32{
ioringSetupClamp | ioringSetupCoopTaskrun, // Kernel 5.19+: reduce thread creation ioringSetupClamp | ioringSetupCoopTaskrun, // Kernel 5.19+: reduce thread creation
ioringSetupClamp, // All kernels ioringSetupClamp, // All kernels
} }
flagSetIdx := 0 flagSetIdx := 0
@@ -1049,7 +1049,7 @@ func newIoUringRecvState(sockFd int, entries uint32, poolSize int, bufferSize in
// Note: SINGLE_ISSUER causes EEXIST errors, so it's excluded // Note: SINGLE_ISSUER causes EEXIST errors, so it's excluded
flagSets := []uint32{ flagSets := []uint32{
ioringSetupClamp | ioringSetupCoopTaskrun, // Kernel 5.19+: reduce thread creation ioringSetupClamp | ioringSetupCoopTaskrun, // Kernel 5.19+: reduce thread creation
ioringSetupClamp, // All kernels ioringSetupClamp, // All kernels
} }
flagSetIdx := 0 flagSetIdx := 0
@@ -1116,33 +1116,33 @@ func newIoUringRecvState(sockFd int, entries uint32, poolSize int, bufferSize in
buf.msghdr.Iov = buf.iovec buf.msghdr.Iov = buf.iovec
buf.msghdr.Iovlen = 1 buf.msghdr.Iovlen = 1
buf.msghdr.Control = &buf.controlBuf[0] buf.msghdr.Control = &buf.controlBuf[0]
buf.msghdr.Controllen = uint64(len(buf.controlBuf)) buf.msghdr.Controllen = controllen(len(buf.controlBuf))
ring.bufferPool[i] = buf ring.bufferPool[i] = buf
ring.bufferMap[buf.userData] = buf ring.bufferMap[buf.userData] = buf
}
logrus.WithFields(logrus.Fields{
"poolSize": poolSize,
"entries": ring.sqEntryCount,
"bufferSize": bufferSize,
}).Info("io_uring receive ring created")
// Limit kernel worker threads to prevent thousands being spawned
// [0] = bounded workers, [1] = unbounded workers
maxWorkers := [2]uint32{4, 4} // Limit to 4 workers of each type
_, _, errno = unix.Syscall6(
unix.SYS_IO_URING_REGISTER,
uintptr(fd),
uintptr(ioringRegisterIowqMaxWorkers),
uintptr(unsafe.Pointer(&maxWorkers[0])),
2, // array length
0, 0,
)
// Ignore errors - older kernels don't support this
return ring, nil
} }
logrus.WithFields(logrus.Fields{
"poolSize": poolSize,
"entries": ring.sqEntryCount,
"bufferSize": bufferSize,
}).Info("io_uring receive ring created")
// Limit kernel worker threads to prevent thousands being spawned
// [0] = bounded workers, [1] = unbounded workers
maxWorkers := [2]uint32{4, 4} // Limit to 4 workers of each type
_, _, errno = unix.Syscall6(
unix.SYS_IO_URING_REGISTER,
uintptr(fd),
uintptr(ioringRegisterIowqMaxWorkers),
uintptr(unsafe.Pointer(&maxWorkers[0])),
2, // array length
0, 0,
)
// Ignore errors - older kernels don't support this
return ring, nil
}
} }
func (r *ioUringRecvState) mapRings(params *ioUringParams) error { func (r *ioUringRecvState) mapRings(params *ioUringParams) error {
@@ -1215,7 +1215,7 @@ func (r *ioUringRecvState) submitRecvLocked(buf *recvBuffer) error {
// Reset buffer state for reuse // Reset buffer state for reuse
buf.msghdr.Namelen = uint32(len(buf.nameBuf)) buf.msghdr.Namelen = uint32(len(buf.nameBuf))
buf.msghdr.Controllen = uint64(len(buf.controlBuf)) buf.msghdr.Controllen = controllen(len(buf.controlBuf))
buf.msghdr.Flags = 0 buf.msghdr.Flags = 0
buf.iovec.SetLen(len(buf.payloadBuf)) buf.iovec.SetLen(len(buf.payloadBuf))
@@ -1414,12 +1414,12 @@ func (r *ioUringRecvState) receivePackets(wait bool) ([]RecvPacket, error) {
} }
packets = append(packets, RecvPacket{ packets = append(packets, RecvPacket{
Data: dataBuf, Data: dataBuf,
N: n, N: n,
From: &from, From: &from,
Flags: flags, Flags: flags,
Control: controlBuf, Control: controlBuf,
Controllen: controllen, Controllen: controllen,
RecycleFunc: func() { RecycleFunc: func() {
// Return buffers to pool // Return buffers to pool
recvPacketDataPool.Put(dataBufPtr) recvPacketDataPool.Put(dataBufPtr)