Queue Depth (QUEUE_DEPTH)
The specified QUEUE_DEPTH as 1 in the code indicates that only one submission queue entry (SQE) can be in the queue at once. This limits the concurrent I/O operations but keeps the example simple. In a real-world application, especially one doing many concurrent I/O operations, you might set a higher depth to allow multiple I/O operations to be in flight simultaneously.
System Calls Handling in io_uring
The io_uring_wait_cqe call will wait for at least one completion queue entry (CQE). If there are multiple CQEs available, it will allow you to iterate over them. However, with a queue depth of 1, you are essentially only handling a single operation at a time.
Explanation of struct io_uring_cqe *cqe
The struct io_uring_cqe *cqe is a pointer to the completion queue entry that you receive after the I/O operation completes. The completion queue entry contains the result of the I/O operation, including any error codes.
Associating User Data with CQE
You can associate custom user data with each SQE by using the user_data field in the io_uring_sqe structure. This user data is then accessible when the CQE is retrieved, making it easy to correlate which operation the CQE corresponds to.
Modified Example with User Data
Below, I'll modify the example to include user data and increase the QUEUE_DEPTH to 2 for illustration:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <liburing.h>
#define PORT 8080
#define QUEUE_DEPTH 2
#define BUFFER_SIZE 4096
int main() {
struct io_uring ring;
struct sockaddr_in server_addr;
int sockfd, ret;
int yes = 1, no = 0;
// Initialize io_uring instance with a queue depth
ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0); // io_uring setup
if (ret < 0) {
perror("io_uring_queue_init");
return -1;
}
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0); // Standard socket creation, not io_uring
if (sockfd < 0) {
perror("socket");
io_uring_queue_exit(&ring);
return -1;
}
// Set SO_CORK option
ret = setsockopt(sockfd, SOL_TCP, TCP_CORK, &yes, sizeof(yes)); // setsockopt is not io_uring
if (ret < 0) {
perror("setsockopt TCP_CORK");
close(sockfd);
io_uring_queue_exit(&ring);
return -1;
}
// Set SO_NODELAY option
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); // setsockopt is not io_uring
if (ret < 0) {
perror("setsockopt TCP_NODELAY");
close(sockfd);
io_uring_queue_exit(&ring);
return -1;
}
// Prepare the server address structure
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// Add connect operation to io_uring and associate user data
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); // Get a submission queue entry
if (!sqe) {
fprintf(stderr, "Failed to get SQE.\n");
close(sockfd);
io_uring_queue_exit(&ring);
return -1;
}
io_uring_prep_connect(sqe, sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)); // Prepare connect
sqe->user_data = 42; // Setting user-defined data, e.g., an identifier
struct io_uring_cqe *cqe;
ret = io_uring_submit_and_wait(&ring, 1); // Submits and waits for at least one completion
if (ret < 0) {
perror("io_uring_submit_and_wait");
close(sockfd);
io_uring_queue_exit(&ring);
return -1;
}
ret = io_uring_wait_cqe(&ring, &cqe); // Waits for completion event
if (ret < 0) {
perror("io_uring_wait_cqe");
close(sockfd);
io_uring_queue_exit(&ring);
return -1;
}
if (cqe->res < 0) {
fprintf(stderr, "Connection failed: %d\n", cqe->res);
close(sockfd);
io_uring_queue_exit(&ring);
return -1;
}
// Extract the user data from the CQE
printf("User data from CQE: %lu\n", cqe->user_data);
// Your code to handle successful connection would go here
io_uring_cqe_seen(&ring, cqe); // Mark this CQE as seen
// Clean up
close(sockfd);
io_uring_queue_exit(&ring);
return 0;
}
Summary of Changes and Points:
- QUEUE_DEPTH: Increased to 2 for demonstrating higher in-flight I/O capacity.
sqe->user_data: Set custom user data to correlate operations.
- Print User Data: Retrieve and print the user data from the CQE.
This setup allows you to manage multiple asynchronous I/O operations and associate each with user-defined data that helps in correlating the operations when processing the completion events.