-
-
Save 1Jo1/accb91b737abb55d07487799739ad70a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <fcntl.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <strings.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
#include <poll.h> | |
#include <assert.h> | |
#include "liburing.h" | |
#include <assert.h> | |
#define BACKLOG 512 | |
#define PORT 9700 | |
#define SQ_POLL 1 | |
struct io_uring ring; | |
int setup_io_uring() { | |
struct io_uring_params p; | |
int ret; | |
memset(&p, 0, sizeof(p)); | |
if (SQ_POLL) { | |
p.flags = IORING_SETUP_SQPOLL; | |
} | |
ret = io_uring_queue_init_params(16, &ring, &p); | |
if (ret) { | |
fprintf(stderr, "Unable to setup io_uring: %s\n", strerror(-ret)); | |
return 1; | |
} | |
return 0; | |
} | |
void add_poll(int fd, unsigned int poll_mask) { | |
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); | |
io_uring_prep_poll_add(sqe, fd, poll_mask); | |
if (SQ_POLL) { | |
sqe->flags = IOSQE_FIXED_FILE; | |
} | |
sqe->user_data = 11; | |
} | |
void add_accept(int fd) { | |
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); | |
io_uring_prep_accept(sqe, fd, 0, 0, SOCK_NONBLOCK | SOCK_CLOEXEC); | |
if (SQ_POLL) { | |
sqe->flags = IOSQE_FIXED_FILE; | |
} | |
sqe->user_data = 22; | |
} | |
int create_server_socket(struct sockaddr_in serv_addr) { | |
int sock_listen_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); | |
const int val = 1; | |
setsockopt(sock_listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); | |
if (bind(sock_listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { | |
perror("Error binding socket\n"); | |
exit(1); | |
} | |
if (listen(sock_listen_fd, BACKLOG) < 0) { | |
perror("Error listening on socket\n"); | |
exit(1); | |
} | |
return sock_listen_fd; | |
} | |
int main(int argc, char *argv[]) { | |
setup_io_uring(); | |
int sock; | |
struct sockaddr_in serv_addr; | |
memset(&serv_addr, 0, sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(PORT); | |
serv_addr.sin_addr.s_addr = INADDR_ANY; | |
int sock_listen_fd = create_server_socket(serv_addr); | |
int *fds = malloc(10 * sizeof(int)); | |
fds[0] = sock_listen_fd; | |
if(SQ_POLL) { | |
printf("register files: %d \n", io_uring_register_files(&ring, fds, 10)); | |
} | |
if(SQ_POLL) { | |
sock = 0; //register file index | |
} else { | |
sock = sock_listen_fd; | |
} | |
add_poll(sock, POLLIN); | |
io_uring_submit(&ring); | |
struct io_uring_cqe *cqe; | |
io_uring_wait_cqe(&ring, &cqe); | |
assert(cqe->user_data == 11); | |
assert(cqe->res > 0); | |
io_uring_cqe_seen(&ring, cqe); | |
add_accept(sock); | |
io_uring_submit(&ring); | |
io_uring_wait_cqe(&ring, &cqe); | |
assert(cqe->user_data == 22); | |
//it seems to fail with -EINVAL | |
printf("cqe->res: %d \n", cqe->res); | |
assert(cqe->res > 0); | |
sleep(8); | |
io_uring_queue_exit(&ring); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment