Class LibUring
This class provides direct access to Linux io_uring system calls via Java's Foreign Function & Memory API (JEP 454, finalized in Java 22). io_uring is a high-performance asynchronous I/O interface introduced in Linux kernel 5.1 that provides significant performance improvements over traditional epoll/select-based I/O mechanisms.
io_uring Architecture Overview
io_uring uses two ring buffers shared between user-space and kernel-space:
- Submission Queue (SQ): User-space writes I/O requests (SQEs - Submission Queue Entries) here. Each SQE describes an I/O operation (read, write, accept, connect, etc.)
- Completion Queue (CQ): Kernel writes completion results (CQEs - Completion Queue Entries) here. Each CQE contains the result of a completed operation.
This shared memory design enables:
- Zero-copy I/O: Data stays in place, no copying between kernel/user-space
- Batched submissions: Multiple operations submitted with a single syscall (100x syscall reduction)
- SQPOLL mode: Kernel thread polls SQ, eliminating submit syscalls entirely (further 2-5x improvement)
- Registered buffers: Pre-registered memory regions skip address validation (1.7x throughput improvement)
- Fixed files: Pre-registered file descriptors skip fd lookup overhead
Memory Layouts
This class defines memory layouts matching the Linux kernel ABI:
IO_URING_LAYOUT: Main io_uring structure containing SQ and CQIO_URING_SQE_LAYOUT: Submission Queue Entry (64 bytes)IO_URING_CQE_LAYOUT: Completion Queue Entry (16 bytes)IO_URING_PARAMS_LAYOUT: Initialization parameters for queue setupIOVEC_LAYOUT: I/O vector for buffer registrationKERNEL_TIMESPEC_LAYOUT: Kernel timespec for timeout operations
Key Operations
queueInit(int, MemorySegment, int)/queueInitParams(int, MemorySegment, MemorySegment)- Initialize submission/completion queuesregisterBuffers(MemorySegment, MemorySegment, int)- Pre-register buffers for 1.7x throughput improvementregisterFiles(MemorySegment, MemorySegment, int)- Pre-register file descriptors for faster fd lookupprepSend(MemorySegment, int, MemorySegment, long, int)/prepRecv(MemorySegment, int, MemorySegment, long, int)- Prepare network I/O operationsprepSendZc(MemorySegment, int, MemorySegment, long, int)- Prepare zero-copy send (Linux 6.0+)prepRecvMultishot(MemorySegment, int, MemorySegment, long, int)- Prepare persistent multi-shot receive (Linux 5.16+)submit(MemorySegment)- Batch submit all queued operations to kernelpeekCqe(MemorySegment, MemorySegment)/waitCqeTimeout(MemorySegment, MemorySegment, MemorySegment)- Retrieve completions
Advanced Features
- Buffer Rings (Linux 5.19+): Kernel-managed buffer selection for multishot recv
- Linked Operations: Chain SQEs for ordered execution (e.g., recv→send echo)
- CQE Skip on Success: Reduce CQE overhead for linked operations
Requirements
- Linux kernel 5.1+ (5.6+ recommended for full features, 6.0+ for zero-copy send)
- liburing shared library installed (liburing.so, typically from liburing-dev package)
- Java 21+ with FFM enabled (--enable-native-access=ALL-UNNAMED)
Performance Characteristics
- Latency: 2-5μs end-to-end vs 50-100μs for traditional NIO
- Throughput: 1.7x improvement with registered buffers
- Syscall reduction: 100x fewer syscalls with batch submission
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intAF_INET - IPv4 Internet protocols.static final intF_GETFL - Get file status flags.static final intF_SETFL - Set file status flags.static final StructLayoutMemory layout for io_uring_buf (single buffer in a ring).static final StructLayoutMemory layout for io_uring_buf_reg (buffer ring registration).static final StructLayoutMemory layout for io_uring_buf_ring (buffer ring header).static final StructLayoutMemory layout for io_uring_cq (Completion Queue).static final StructLayoutstruct io_uring_cqe- Completion Queue Entry (16 bytes).static final StructLayoutMemory layout for io_uring structure.static final StructLayoutMemory layout for io_uring_params.static final StructLayoutMemory layout for io_uring_sq (Submission Queue).static final StructLayoutstruct io_uring_sqe- Submission Queue Entry (64 bytes).static final intstatic final intstatic final intstatic final bytestatic final bytestatic final bytestatic final bytestatic final bytestatic final bytestatic final bytestatic final bytestatic final bytestatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final StructLayoutMemory layout for iovec structure (for buffer registration).static final intIPPROTO_TCP - TCP protocol.static final StructLayoutMemory layout for __kernel_timespec (for timeout operations).static final intO_NONBLOCK - Non-blocking I/O mode.static final intSO_REUSEADDR - Allow reuse of local addresses.static final intSO_REUSEPORT - Allow reuse of local port.static final intSOCK_NONBLOCK - Set O_NONBLOCK on the new socket.static final intSOCK_STREAM - Sequenced, reliable, connection-based byte streams.static final intSOL_SOCKET - Socket level for setsockopt. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbufferRingAdd(MemorySegment bufRing, long buf, int bufLen, short bid, int mask, int idx) Add a buffer to the buffer ring.static voidbufferRingAdvance(MemorySegment bufRing, int count) Advance the buffer ring tail to make buffers visible to the kernel.static voidbufferRingInit(MemorySegment bufRing) Initialize a buffer ring header.static intGet the buffer ID from a CQE with IORING_CQE_F_BUFFER flag set.static intcqeGetFlags(MemorySegment cqe) Get CQE flags field.static intcqeGetRes(MemorySegment cqe) Get CQE res field (result/bytes transferred).static longGet CQE user_data field.static booleanCheck if CQE has buffer ID set (IORING_CQE_F_BUFFER).static voidcqeSeen(MemorySegment ring, MemorySegment cqe) Mark a CQE as seen (consumed).static intHelper: Create and configure a non-blocking TCP socket.static MemorySegmentgetSqe(MemorySegment ring) static booleanCheck if a multishot receive is still active (more completions coming).static booleanCheck if io_uring is available on this system.static booleanCheck if buffer ring registration is supported.static booleanCheck if a CQE is a zero-copy notification (not actual completion).static intnativeAccept(int sockfd, MemorySegment addr, MemorySegment addrlen) Accept a connection on a socket.static intnativeBind(int sockfd, MemorySegment addr, int addrlen) Bind a socket to a local address.static intnativeClose(int fd) Close a file descriptor.static intnativeConnect(int sockfd, MemorySegment addr, int addrlen) Connect a socket to a remote address.static intnativeFcntl(int fd, int cmd, int arg) Manipulate file descriptor.static shortnativeHtons(short hostshort) Convert values between host and network byte order (16-bit).static intnativeInetPton(int af, MemorySegment src, MemorySegment dst) Convert IPv4/IPv6 address from text to binary form.static intnativeListen(int sockfd, int backlog) Listen for connections on a socket.static intnativeSetsockopt(int sockfd, int level, int optname, MemorySegment optval, int optlen) Set socket options.static intnativeSocket(int domain, int type, int protocol) Create a socket.static intpeekCqe(MemorySegment ring, MemorySegment cqePtr) Peek for a completion without blocking.static voidprepAccept(MemorySegment sqe, int fd, MemorySegment addr, MemorySegment addrlen, int flags) Prepare an accept operation.static voidprepConnect(MemorySegment sqe, int fd, MemorySegment addr, int addrlen) Prepare a connect operation.static voidprepRecv(MemorySegment sqe, int fd, MemorySegment buf, long len, int flags) Prepare a recv operation.static voidprepRecvFixed(MemorySegment sqe, int fd, short bufIndex, int len, int flags) Prepare a recv operation with fixed buffer.static voidprepRecvFixedFile(MemorySegment sqe, int fileIndex, MemorySegment buf, long len, int msgFlags) Prepare a recv operation using a registered file index.static voidprepRecvMultishot(MemorySegment sqe, int fd, MemorySegment buf, long len, int flags) Prepare a multi-shot receive operation.static voidprepRecvMultishotBufferSelect(MemorySegment sqe, int fd, short bgid, int flags) Prepare a multishot receive with buffer ring selection.static voidprepSend(MemorySegment sqe, int fd, MemorySegment buf, long len, int flags) Prepare a send operation.static voidprepSendFixed(MemorySegment sqe, int fd, short bufIndex, int len, int flags) Prepare a send operation with fixed buffer.static voidprepSendFixedFile(MemorySegment sqe, int fileIndex, MemorySegment buf, long len, int msgFlags) Prepare a send operation using a registered file index.static voidprepSendZc(MemorySegment sqe, int fd, MemorySegment buf, long len, int flags) Prepare a zero-copy send operation.static voidprepSendZcFixed(MemorySegment sqe, int fd, short bufIndex, int len, int flags) Prepare a zero-copy send operation with fixed buffer.static voidqueueExit(MemorySegment ring) Tear down an io_uring instance.static intqueueInit(int entries, MemorySegment ring, int flags) Initialize an io_uring instance.static intqueueInitParams(int entries, MemorySegment ring, MemorySegment params) Initialize io_uring with parameters.static intregisterBufferRing(MemorySegment ring, MemorySegment bufRing, int nentries, short bgid) Register a provided buffer ring with io_uring.static intregisterBuffers(MemorySegment ring, MemorySegment iovecs, int nrIovecs) Register buffers for zero-copy I/O.static intregisterFiles(MemorySegment ring, MemorySegment files, int nr_files) Get a submission queue entry.static intregisterFilesUpdate(MemorySegment ring, int off, MemorySegment files, int nr_files) static voidsetSqeFlags(MemorySegment sqe, int flags) Set flags for the SQE (e.g.static bytesqeGetFlags(MemorySegment sqe) Get SQE flags field.static voidsqeSetAddr(MemorySegment sqe, long addr) Set SQE addr field (buffer address).static voidsqeSetBufIndex(MemorySegment sqe, short bufIndex) Set SQE buf_index field (for fixed buffers).static voidSet CQE_SKIP_SUCCESS flag - don't generate CQE if operation succeeds.static voidsqeSetFd(MemorySegment sqe, int fd) Set SQE fd field.static voidsqeSetFlags(MemorySegment sqe, byte flags) Set SQE flags field.static voidSet the IO_HARDLINK flag on an SQE.static voidsqeSetLen(MemorySegment sqe, int len) Set SQE len field (buffer length).static voidsqeSetLink(MemorySegment sqe) Set the IO_LINK flag on an SQE to link it to the next SQE.static voidsqeSetOpcode(MemorySegment sqe, byte opcode) Set SQE opcode field.static voidsqeSetOpFlags(MemorySegment sqe, int opFlags) Set SQE op_flags field (operation-specific flags like msg_flags).static voidsqeSetUserData(MemorySegment sqe, long userData) Set SQE user_data field (for request tracking).static intsubmit(MemorySegment ring) Submit queued operations to the kernel.static intunregisterBufferRing(MemorySegment ring, int bgid) Unregister a buffer ring.static intUnregister previously registered buffers.static intunregisterFiles(MemorySegment ring) static intwaitCqe(MemorySegment ring, MemorySegment cqePtr) Wait for a completion queue entry.static intwaitCqeTimeout(MemorySegment ring, MemorySegment cqePtr, MemorySegment ts) Wait for a completion (with timeout).
-
Field Details
-
IO_URING_SQE_LAYOUT
struct io_uring_sqe- Submission Queue Entry (64 bytes).Layout with offsets:
0: opcode (1) 1: flags (1) 2: ioprio (2) 4: fd (4) 8: off (8) 16: addr (8) 24: len (4) 28: op_flags (4) 32: user_data (8) 40: buf_index (2) 42: buf_group (2) 44: personality (4) 48: splice_fd_in (4) 52: __pad (4) 56: addr3 (8)
-
IO_URING_CQE_LAYOUT
struct io_uring_cqe- Completion Queue Entry (16 bytes).struct io_uring_cqe { __u64 user_data; // sqe->user_data submission passed back __s32 res; // result code for this event __u32 flags; // IORING_CQE_F_* flags }; -
IORING_SETUP_SQPOLL
public static final int IORING_SETUP_SQPOLL- See Also:
-
IORING_SETUP_SQ_AFF
public static final int IORING_SETUP_SQ_AFF- See Also:
-
IORING_SETUP_CQSIZE
public static final int IORING_SETUP_CQSIZE- See Also:
-
IORING_SETUP_COOP_TASKRUN
public static final int IORING_SETUP_COOP_TASKRUN- See Also:
-
IORING_SETUP_SINGLE_ISSUER
public static final int IORING_SETUP_SINGLE_ISSUER- See Also:
-
IORING_OP_NOP
public static final byte IORING_OP_NOP- See Also:
-
IORING_OP_READV
public static final byte IORING_OP_READV- See Also:
-
IORING_OP_WRITEV
public static final byte IORING_OP_WRITEV- See Also:
-
IORING_OP_ACCEPT
public static final byte IORING_OP_ACCEPT- See Also:
-
IORING_OP_CONNECT
public static final byte IORING_OP_CONNECT- See Also:
-
IORING_OP_SEND
public static final byte IORING_OP_SEND- See Also:
-
IORING_OP_RECV
public static final byte IORING_OP_RECV- See Also:
-
IORING_OP_SEND_ZC
public static final byte IORING_OP_SEND_ZC- See Also:
-
IORING_OP_RECV_MULTISHOT
public static final byte IORING_OP_RECV_MULTISHOT- See Also:
-
IORING_RECV_MULTISHOT
public static final int IORING_RECV_MULTISHOT- See Also:
-
IOSQE_FIXED_FILE
public static final int IOSQE_FIXED_FILE- See Also:
-
IOSQE_IO_DRAIN
public static final int IOSQE_IO_DRAIN- See Also:
-
IOSQE_IO_LINK
public static final int IOSQE_IO_LINK- See Also:
-
IOSQE_IO_HARDLINK
public static final int IOSQE_IO_HARDLINK- See Also:
-
IOSQE_BUFFER_SELECT
public static final int IOSQE_BUFFER_SELECT- See Also:
-
IOSQE_CQE_SKIP_SUCCESS
public static final int IOSQE_CQE_SKIP_SUCCESS- See Also:
-
IORING_CQE_F_BUFFER
public static final int IORING_CQE_F_BUFFER- See Also:
-
IORING_CQE_F_MORE
public static final int IORING_CQE_F_MORE- See Also:
-
IORING_CQE_F_NOTIF
public static final int IORING_CQE_F_NOTIF- See Also:
-
IORING_REGISTER_PBUF_RING
public static final int IORING_REGISTER_PBUF_RING- See Also:
-
IORING_UNREGISTER_PBUF_RING
public static final int IORING_UNREGISTER_PBUF_RING- See Also:
-
IOU_PBUF_RING_MMAP
public static final int IOU_PBUF_RING_MMAP- See Also:
-
IO_URING_PARAMS_LAYOUT
Memory layout for io_uring_params. -
IO_URING_SQ_LAYOUT
Memory layout for io_uring_sq (Submission Queue). -
IO_URING_CQ_LAYOUT
Memory layout for io_uring_cq (Completion Queue). -
IO_URING_LAYOUT
Memory layout for io_uring structure. -
KERNEL_TIMESPEC_LAYOUT
Memory layout for __kernel_timespec (for timeout operations).- See Also:
-
IOVEC_LAYOUT
Memory layout for iovec structure (for buffer registration).- See Also:
-
IO_URING_BUF_LAYOUT
Memory layout for io_uring_buf (single buffer in a ring). Each entry represents one buffer that can be selected by the kernel. -
IO_URING_BUF_RING_LAYOUT
Memory layout for io_uring_buf_ring (buffer ring header). The ring uses a tail pointer to track which buffers are available. -
IO_URING_BUF_REG_LAYOUT
Memory layout for io_uring_buf_reg (buffer ring registration). Used with IORING_REGISTER_PBUF_RING. -
AF_INET
public static final int AF_INETAF_INET - IPv4 Internet protocols.- See Also:
-
SOCK_STREAM
public static final int SOCK_STREAMSOCK_STREAM - Sequenced, reliable, connection-based byte streams.- See Also:
-
SOCK_NONBLOCK
public static final int SOCK_NONBLOCKSOCK_NONBLOCK - Set O_NONBLOCK on the new socket.- See Also:
-
SOL_SOCKET
public static final int SOL_SOCKETSOL_SOCKET - Socket level for setsockopt.- See Also:
-
SO_REUSEADDR
public static final int SO_REUSEADDRSO_REUSEADDR - Allow reuse of local addresses.- See Also:
-
SO_REUSEPORT
public static final int SO_REUSEPORTSO_REUSEPORT - Allow reuse of local port.- See Also:
-
IPPROTO_TCP
public static final int IPPROTO_TCPIPPROTO_TCP - TCP protocol.- See Also:
-
F_GETFL
public static final int F_GETFLF_GETFL - Get file status flags.- See Also:
-
F_SETFL
public static final int F_SETFLF_SETFL - Set file status flags.- See Also:
-
O_NONBLOCK
public static final int O_NONBLOCKO_NONBLOCK - Non-blocking I/O mode.- See Also:
-
-
Method Details
-
queueInit
Initialize an io_uring instance.- Parameters:
entries- number of submission queue entries (must be power of 2)ring- pointer to io_uring structureflags- initialization flags- Returns:
- 0 on success, negative errno on failure
-
queueInitParams
Initialize io_uring with parameters.- Parameters:
entries- number of queue entriesring- pointer to io_uring structureparams- pointer to io_uring_params structure- Returns:
- 0 on success, negative errno on failure
-
queueExit
Tear down an io_uring instance. -
registerBuffers
Register buffers for zero-copy I/O.- Parameters:
ring- pointer to io_uring structureiovecs- array of iovec structuresnrIovecs- number of iovecs- Returns:
- 0 on success, negative errno on failure
-
unregisterBuffers
Unregister previously registered buffers. -
registerFiles
Get a submission queue entry.- Returns:
- pointer to SQE, or null if queue is full
-
unregisterFiles
-
registerFilesUpdate
public static int registerFilesUpdate(MemorySegment ring, int off, MemorySegment files, int nr_files) -
getSqe
-
submit
Submit queued operations to the kernel.- Returns:
- number of submitted operations, or negative errno
-
waitCqe
Wait for a completion queue entry.- Parameters:
ring- pointer to io_uring structurecqePtr- pointer to receive CQE pointer- Returns:
- 0 on success, negative errno on failure
-
waitCqeTimeout
Wait for a completion (with timeout).- Parameters:
ring- pointer to io_uring structurecqePtr- pointer to store CQE pointerts- pointer to __kernel_timespec structure (or null for no timeout)- Returns:
- 0 on success, negative errno on failure
-
peekCqe
Peek for a completion without blocking.- Returns:
- 0 if CQE available, -EAGAIN if not
-
cqeSeen
Mark a CQE as seen (consumed). -
prepConnect
Prepare a connect operation. -
prepAccept
public static void prepAccept(MemorySegment sqe, int fd, MemorySegment addr, MemorySegment addrlen, int flags) Prepare an accept operation. -
prepSend
Prepare a send operation. -
prepRecv
Prepare a recv operation. -
setSqeFlags
Set flags for the SQE (e.g. IOSQE_FIXED_FILE). -
prepSendFixedFile
public static void prepSendFixedFile(MemorySegment sqe, int fileIndex, MemorySegment buf, long len, int msgFlags) Prepare a send operation using a registered file index. -
prepRecvFixedFile
public static void prepRecvFixedFile(MemorySegment sqe, int fileIndex, MemorySegment buf, long len, int msgFlags) Prepare a recv operation using a registered file index. -
sqeSetOpcode
Set SQE opcode field. -
sqeSetFlags
Set SQE flags field. -
sqeSetFd
Set SQE fd field. -
sqeSetAddr
Set SQE addr field (buffer address). -
sqeSetLen
Set SQE len field (buffer length). -
sqeSetUserData
Set SQE user_data field (for request tracking). -
sqeSetBufIndex
Set SQE buf_index field (for fixed buffers). -
sqeSetOpFlags
Set SQE op_flags field (operation-specific flags like msg_flags). -
sqeGetFlags
Get SQE flags field. -
cqeGetUserData
Get CQE user_data field. -
cqeGetRes
Get CQE res field (result/bytes transferred). -
cqeGetFlags
Get CQE flags field. -
prepSendFixed
Prepare a send operation with fixed buffer.- Parameters:
sqe- submission queue entryfd- socket file descriptorbufIndex- index of registered bufferlen- number of bytes to sendflags- send flags
-
prepRecvFixed
Prepare a recv operation with fixed buffer.- Parameters:
sqe- submission queue entryfd- socket file descriptorbufIndex- index of registered bufferlen- maximum bytes to receiveflags- recv flags
-
prepSendZc
Prepare a zero-copy send operation.Zero-copy send avoids copying data from user-space to kernel-space, providing significant performance improvements for large buffers.
IMPORTANT: When using zero-copy send:
- You will receive TWO completions: one for send completion, one for notification (IORING_CQE_F_NOTIF)
- The buffer must NOT be modified until the NOTIF completion is received
- Check CQE flags for IORING_CQE_F_NOTIF to distinguish notification from send completion
- Parameters:
sqe- submission queue entryfd- socket file descriptorbuf- buffer to send (must remain valid until NOTIF completion)len- number of bytes to sendflags- send flags (MSG_* constants)
-
prepSendZcFixed
Prepare a zero-copy send operation with fixed buffer.- Parameters:
sqe- submission queue entryfd- socket file descriptorbufIndex- index of registered bufferlen- number of bytes to sendflags- send flags
-
isZeroCopyNotification
Check if a CQE is a zero-copy notification (not actual completion).- Parameters:
cqe- completion queue entry- Returns:
- true if this is a notification CQE
-
prepRecvMultishot
public static void prepRecvMultishot(MemorySegment sqe, int fd, MemorySegment buf, long len, int flags) Prepare a multi-shot receive operation.Multi-shot receive keeps the SQE active and generates multiple CQEs until the operation is cancelled or an error occurs. This is ideal for persistent receive loops as it eliminates the need to resubmit after each receive.
IMPORTANT:
- Check CQE flags for IORING_CQE_F_MORE - if set, more completions are coming
- If IORING_CQE_F_MORE is NOT set, the operation has terminated and must be resubmitted
- Works best with buffer rings (IOSQE_BUFFER_SELECT) for automatic buffer management
- Requires Linux 5.16+
- Parameters:
sqe- submission queue entryfd- socket file descriptorbuf- buffer for receiving datalen- maximum bytes to receiveflags- recv flags (MSG_* constants)
-
hasMoreCompletions
Check if a multishot receive is still active (more completions coming).- Parameters:
cqe- completion queue entry- Returns:
- true if more completions are expected
-
isBufferRingSupported
public static boolean isBufferRingSupported()Check if buffer ring registration is supported. Requires liburing with io_uring_register_buf_ring (Linux 5.19+).- Returns:
- true if buffer ring is available
-
registerBufferRing
public static int registerBufferRing(MemorySegment ring, MemorySegment bufRing, int nentries, short bgid) Register a provided buffer ring with io_uring.Buffer rings allow the kernel to automatically select buffers for receive operations, eliminating the need to specify buffers in each SQE. This is essential for efficient multishot receive.
Usage pattern:
- Allocate buffer ring memory (header + buffers)
- Initialize the ring with bufferRingInit()
- Add buffers with bufferRingAdd()
- Call bufferRingAdvance() to make buffers visible to kernel
- Register with registerBufferRing()
- Use IOSQE_BUFFER_SELECT flag in recv operations
- Parameters:
ring- io_uring instancebufRing- buffer ring memory (header at start)nentries- number of buffer entries (power of 2)bgid- buffer group ID (unique per ring)- Returns:
- 0 on success, negative errno on failure
-
unregisterBufferRing
Unregister a buffer ring.- Parameters:
ring- io_uring instancebgid- buffer group ID- Returns:
- 0 on success, negative errno on failure
-
bufferRingInit
Initialize a buffer ring header. Call this before adding buffers.- Parameters:
bufRing- pointer to buffer ring memory
-
bufferRingAdd
public static void bufferRingAdd(MemorySegment bufRing, long buf, int bufLen, short bid, int mask, int idx) Add a buffer to the buffer ring.- Parameters:
bufRing- pointer to buffer ringbuf- buffer addressbufLen- buffer lengthbid- buffer ID (used to identify buffer on completion)mask- ring mask (nentries - 1)idx- current index in ring
-
bufferRingAdvance
Advance the buffer ring tail to make buffers visible to the kernel. Must be called after adding buffers with bufferRingAdd().- Parameters:
bufRing- pointer to buffer ringcount- number of buffers added
-
cqeGetBufferId
Get the buffer ID from a CQE with IORING_CQE_F_BUFFER flag set.- Parameters:
cqe- completion queue entry- Returns:
- buffer ID (bid) or -1 if no buffer flag
-
cqeHasBuffer
Check if CQE has buffer ID set (IORING_CQE_F_BUFFER).- Parameters:
cqe- completion queue entry- Returns:
- true if buffer was selected from ring
-
prepRecvMultishotBufferSelect
Prepare a multishot receive with buffer ring selection.This is the most efficient receive pattern:
- Multishot: keeps receiving without resubmitting SQEs
- Buffer select: kernel picks buffers from the ring
- Parameters:
sqe- submission queue entryfd- socket file descriptorbgid- buffer group ID (matching registered buffer ring)flags- recv flags (MSG_* constants)
-
sqeSetLink
Set the IO_LINK flag on an SQE to link it to the next SQE.Linked operations are executed sequentially. If a linked operation fails, subsequent linked operations are cancelled (unless IOSQE_IO_HARDLINK is used).
Example: Echo pattern
sqe1 = getSqe() // recv prepRecv(sqe1, ...) sqeSetLink(sqe1) // link to next sqe2 = getSqe() // send (executes after recv completes) prepSend(sqe2, ...)
- Parameters:
sqe- submission queue entry to link
-
sqeSetHardLink
Set the IO_HARDLINK flag on an SQE. Unlike IOSQE_IO_LINK, the chain continues even if this operation fails.- Parameters:
sqe- submission queue entry to hard-link
-
sqeSetCqeSkipSuccess
Set CQE_SKIP_SUCCESS flag - don't generate CQE if operation succeeds. Useful for linked operations where only the final result matters. Requires Linux 5.17+.- Parameters:
sqe- submission queue entry
-
isAvailable
public static boolean isAvailable()Check if io_uring is available on this system. -
nativeSocket
public static int nativeSocket(int domain, int type, int protocol) Create a socket.- Parameters:
domain- AF_INET for IPv4type- SOCK_STREAM for TCPprotocol- 0 or IPPROTO_TCP- Returns:
- file descriptor on success, negative on failure
-
nativeConnect
Connect a socket to a remote address.- Parameters:
sockfd- socket file descriptoraddr- pointer to sockaddr structureaddrlen- size of sockaddr structure- Returns:
- 0 on success, -1 on failure (check errno)
-
nativeBind
Bind a socket to a local address. -
nativeListen
public static int nativeListen(int sockfd, int backlog) Listen for connections on a socket. -
nativeAccept
Accept a connection on a socket. -
nativeSetsockopt
public static int nativeSetsockopt(int sockfd, int level, int optname, MemorySegment optval, int optlen) Set socket options. -
nativeFcntl
public static int nativeFcntl(int fd, int cmd, int arg) Manipulate file descriptor. -
nativeClose
public static int nativeClose(int fd) Close a file descriptor. -
nativeInetPton
Convert IPv4/IPv6 address from text to binary form. -
nativeHtons
public static short nativeHtons(short hostshort) Convert values between host and network byte order (16-bit). -
createNonBlockingSocket
public static int createNonBlockingSocket()Helper: Create and configure a non-blocking TCP socket.
-