Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | 6827bf8 | 2019-12-17 16:12:08 -0700 | [diff] [blame] | 2 | #include <liburing.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <syscall.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdio.h> |
| 10 | #include <errno.h> |
| 11 | #include "liburing.h" |
| 12 | #include "../src/syscall.h" |
| 13 | |
| 14 | struct io_uring io_uring; |
| 15 | |
| 16 | int sys_io_uring_enter(const int fd, |
| 17 | const unsigned to_submit, |
| 18 | const unsigned min_complete, |
| 19 | const unsigned flags, sigset_t * const sig) |
| 20 | { |
| 21 | return __sys_io_uring_enter(fd, to_submit, min_complete, flags, sig); |
| 22 | } |
| 23 | |
| 24 | int submit_sqe(void) |
| 25 | { |
| 26 | struct io_uring_sq *sq = &io_uring.sq; |
| 27 | const unsigned tail = *sq->ktail; |
| 28 | |
| 29 | sq->array[tail & *sq->kring_mask] = 0; |
| 30 | io_uring_smp_store_release(sq->ktail, tail + 1); |
| 31 | |
| 32 | return sys_io_uring_enter(io_uring.ring_fd, 1, 0, 0, NULL); |
| 33 | } |
| 34 | |
| 35 | int main(int argc, char **argv) |
| 36 | { |
| 37 | struct addrinfo *addr_info_list = NULL; |
| 38 | struct addrinfo *ai, *addr_info = NULL; |
| 39 | struct io_uring_params params; |
| 40 | struct io_uring_sqe *sqe; |
| 41 | struct addrinfo hints; |
| 42 | struct sockaddr sa; |
| 43 | socklen_t sa_size = sizeof(sa); |
Jens Axboe | c994d93 | 2019-12-23 21:02:57 -0700 | [diff] [blame] | 44 | int ret, listen_fd, connect_fd, val, i; |
Jens Axboe | 6827bf8 | 2019-12-17 16:12:08 -0700 | [diff] [blame] | 45 | |
Jens Axboe | a2141fc | 2020-05-19 17:36:19 -0600 | [diff] [blame] | 46 | if (argc > 1) |
| 47 | return 0; |
| 48 | |
Jens Axboe | 6827bf8 | 2019-12-17 16:12:08 -0700 | [diff] [blame] | 49 | memset(¶ms, 0, sizeof(params)); |
Jens Axboe | 60c05f5 | 2020-10-28 12:52:55 -0600 | [diff] [blame] | 50 | ret = io_uring_queue_init_params(4, &io_uring, ¶ms); |
Jens Axboe | 6827bf8 | 2019-12-17 16:12:08 -0700 | [diff] [blame] | 51 | if (ret) { |
| 52 | fprintf(stderr, "io_uring_init_failed: %d\n", ret); |
| 53 | return 1; |
| 54 | } |
| 55 | if (!(params.features & IORING_FEAT_SUBMIT_STABLE)) { |
| 56 | fprintf(stdout, "FEAT_SUBMIT_STABLE not there, skipping\n"); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | memset(&hints, 0, sizeof(hints)); |
| 61 | hints.ai_family = AF_UNSPEC; |
| 62 | hints.ai_socktype = SOCK_STREAM; |
| 63 | hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV; |
| 64 | |
| 65 | ret = getaddrinfo(NULL, "12345", &hints, &addr_info_list); |
| 66 | if (ret < 0) { |
| 67 | perror("getaddrinfo"); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | for (ai = addr_info_list; ai; ai = ai->ai_next) { |
| 72 | if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { |
| 73 | addr_info = ai; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | if (!addr_info) { |
| 78 | fprintf(stderr, "addrinfo not found\n"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | sqe = &io_uring.sq.sqes[0]; |
| 83 | listen_fd = -1; |
| 84 | |
| 85 | ret = socket(addr_info->ai_family, SOCK_STREAM, |
| 86 | addr_info->ai_protocol); |
| 87 | if (ret < 0) { |
| 88 | perror("socket"); |
| 89 | return 1; |
| 90 | } |
| 91 | listen_fd = ret; |
| 92 | |
| 93 | val = 1; |
| 94 | setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)); |
| 95 | setsockopt(listen_fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(int)); |
| 96 | |
| 97 | ret = bind(listen_fd, addr_info->ai_addr, addr_info->ai_addrlen); |
| 98 | if (ret < 0) { |
| 99 | perror("bind"); |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | ret = listen(listen_fd, SOMAXCONN); |
| 104 | if (ret < 0) { |
| 105 | perror("listen"); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | memset(&sa, 0, sizeof(sa)); |
| 110 | |
| 111 | io_uring_prep_accept(sqe, listen_fd, &sa, &sa_size, 0); |
| 112 | sqe->user_data = 1; |
| 113 | ret = submit_sqe(); |
| 114 | if (ret != 1) { |
| 115 | fprintf(stderr, "submit failed: %d\n", ret); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | connect_fd = -1; |
| 120 | ret = socket(addr_info->ai_family, SOCK_STREAM, addr_info->ai_protocol); |
| 121 | if (ret < 0) { |
| 122 | perror("socket"); |
| 123 | return 1; |
| 124 | } |
| 125 | connect_fd = ret; |
| 126 | |
| 127 | io_uring_prep_connect(sqe, connect_fd, addr_info->ai_addr, |
| 128 | addr_info->ai_addrlen); |
| 129 | sqe->user_data = 2; |
| 130 | ret = submit_sqe(); |
| 131 | if (ret != 1) { |
| 132 | fprintf(stderr, "submit failed: %d\n", ret); |
| 133 | return 1; |
| 134 | } |
| 135 | |
Jens Axboe | c994d93 | 2019-12-23 21:02:57 -0700 | [diff] [blame] | 136 | for (i = 0; i < 2; i++) { |
Jens Axboe | 6827bf8 | 2019-12-17 16:12:08 -0700 | [diff] [blame] | 137 | struct io_uring_cqe *cqe = NULL; |
| 138 | |
| 139 | ret = io_uring_wait_cqe(&io_uring, &cqe); |
| 140 | if (ret) { |
| 141 | fprintf(stderr, "io_uring_wait_cqe: %d\n", ret); |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | switch (cqe->user_data) { |
| 146 | case 1: |
| 147 | if (cqe->res < 0) { |
| 148 | fprintf(stderr, "accept failed: %d\n", cqe->res); |
| 149 | return 1; |
| 150 | } |
| 151 | break; |
| 152 | case 2: |
| 153 | if (cqe->res) { |
| 154 | fprintf(stderr, "connect failed: %d\n", cqe->res); |
| 155 | return 1; |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | io_uring_cq_advance(&io_uring, 1); |
| 160 | } |
| 161 | |
| 162 | freeaddrinfo(addr_info_list); |
| 163 | io_uring_queue_exit(&io_uring); |
| 164 | return 0; |
| 165 | } |