From c8291a94ddf695447f94728a2b819550138e36e8 Mon Sep 17 00:00:00 2001 From: Hrvoje Zeba Date: Wed, 27 Nov 2019 21:53:44 -0700 Subject: Add a simple connect test Tests for cases where we try to connect to a closed and opened port. Signed-off-by: Hrvoje Zeba Signed-off-by: Jens Axboe --- test/Makefile | 5 +- test/connect.c | 258 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 261 insertions(+), 2 deletions(-) create mode 100644 test/connect.c diff --git a/test/Makefile b/test/Makefile index cad90d6..40b7e76 100644 --- a/test/Makefile +++ b/test/Makefile @@ -11,7 +11,8 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register socket-rw accept timeout-overflow defer read-write io-cancel \ link-timeout cq-overflow link_drain fc2a85cb02ef-test \ poll-link accept-link fixed-link poll-cancel-ton teardowns \ - poll-many b5837bd5311d-test accept-test d77a67ed5f27-test + poll-many b5837bd5311d-test accept-test d77a67ed5f27-test \ + connect include ../Makefile.quiet @@ -30,7 +31,7 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \ timeout-overflow.c defer.c read-write.c io-cancel.c link-timeout.c \ cq-overflow.c link_drain.c fc2a85cb02ef-test.c poll-link.c \ accept-link.c fixed-link.c poll-cancel-ton.c teardowns.c poll-many.c \ - b5837bd5311d-test.c accept-test.c d77a67ed5f27-test.c + b5837bd5311d-test.c accept-test.c d77a67ed5f27-test.c connect.c test_objs := $(patsubst %.c,%.ol,$(test_srcs)) diff --git a/test/connect.c b/test/connect.c new file mode 100644 index 0000000..ea9334d --- /dev/null +++ b/test/connect.c @@ -0,0 +1,258 @@ +/* + * Check that IORING_OP_CONNECT works, with and without other side + * being open. + */ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "liburing.h" + +int create_socket() +{ + int fd; + + fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (fd == -1) { + perror("socket()"); + return -1; + } + + return fd; +} + +int submit_and_wait(struct io_uring* ring, int* res) +{ + struct io_uring_cqe* cqe; + int ret; + + ret = io_uring_submit_and_wait(ring, 1); + if (ret == -1) { + perror("io_uring_submit()"); + return -1; + } + + ret = io_uring_peek_cqe(ring, &cqe); + if (ret == -1) { + fprintf(stderr, "io_uring_peek_cqe(): no cqe returned"); + return -1; + } + + *res = cqe->res; + io_uring_cqe_seen(ring, cqe); + + return 0; +} + +int wait_for(struct io_uring* ring, int fd, int mask) +{ + struct io_uring_sqe* sqe; + int ret, res; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "unable to get sqe\n"); + return -1; + } + + io_uring_prep_poll_add(sqe, fd, mask); + sqe->user_data = 2; + + ret = submit_and_wait(ring, &res); + if (ret == -1) + return -1; + + if (res < 0) { + fprintf(stderr, "poll(): failed with %d\n", res); + return -1; + } + + return res; +} + +int listen_on_socket(int fd) +{ + struct sockaddr_in addr; + int ret; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = 0x1234; + addr.sin_addr.s_addr = 0x0100007fU; + + ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr)); + if (ret == -1) { + perror("bind()"); + return -1; + } + + ret = listen(fd, 128); + if (ret == -1) { + perror("listen()"); + return -1; + } + + return 0; +} + +int connect_socket(struct io_uring* ring, int fd, int* code) +{ + struct io_uring_sqe* sqe; + struct sockaddr_in addr; + int ret, res, val = 1; + socklen_t code_len = sizeof(*code); + + ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)); + if (ret == -1) { + perror("setsockopt()"); + return -1; + } + + ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + if (ret == -1) { + perror("setsockopt()"); + return -1; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = 0x1234; + addr.sin_addr.s_addr = 0x0100007fU; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "unable to get sqe\n"); + return -1; + } + + io_uring_prep_connect(sqe, fd, (struct sockaddr*)&addr, sizeof(addr)); + sqe->user_data = 1; + + ret = submit_and_wait(ring, &res); + if (ret == -1) + return -1; + + if (res != -EINPROGRESS) { + fprintf(stderr, "connect(): expected %d, got %d\n", EINPROGRESS, res); + return -1; + } + + ret = wait_for(ring, fd, POLLOUT | POLLHUP | POLLERR); + if (ret == -1) + return -1; + + int ev = (ret & POLLOUT) || (ret & POLLHUP) || (ret & POLLERR); + if (!ev) { + fprintf(stderr, "poll(): returned invalid value %#x\n", ret); + return -1; + } + + ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, code, &code_len); + if (ret == -1) { + perror("getsockopt()"); + return -1; + } + + return 0; +} + +int test_connect_with_no_peer(struct io_uring* ring) +{ + int connect_fd; + int ret, code; + + connect_fd = create_socket(); + if (connect_fd == -1) + return -1; + + ret = connect_socket(ring, connect_fd, &code); + if (ret == -1) + goto err; + + if (code != ECONNREFUSED) { + fprintf(stderr, "connect failed with %d\n", code); + goto err; + } + + close(connect_fd); + return 0; + +err: + close(connect_fd); + return -1; +} + +int test_connect(struct io_uring* ring) +{ + int accept_fd; + int connect_fd; + int ret, code; + + accept_fd = create_socket(); + if (accept_fd == -1) + return -1; + + ret = listen_on_socket(accept_fd); + if (ret == -1) + goto err1; + + connect_fd = create_socket(); + if (connect_fd == -1) + goto err1; + + ret = connect_socket(ring, connect_fd, &code); + if (ret == -1) + goto err2; + + if (code != 0) { + fprintf(stderr, "connect failed with %d\n", code); + goto err2; + } + + close(connect_fd); + close(accept_fd); + + return 0; + +err2: + close(connect_fd); + +err1: + close(accept_fd); + return -1; +} + +int main(int argc, char* argv[]) +{ + struct io_uring ring; + int ret; + + ret = io_uring_queue_init(8, &ring, 0); + if (ret == -1) { + perror("io_uring_queue_setup()"); + return 1; + } + + ret = test_connect_with_no_peer(&ring); + if (ret == -1) { + fprintf(stderr, "test_connect_with_no_peer(): failed\n"); + return 1; + } + + ret = test_connect(&ring); + if (ret == -1) { + fprintf(stderr, "test_connect(): failed\n"); + return 1; + } + + io_uring_queue_exit(&ring); + return 0; +} -- cgit v1.2.3