Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 1 | /*! \file osmo_io_uring.c |
| 2 | * io_uring backend for osmo_io. |
| 3 | * |
| 4 | * (C) 2022-2023 by sysmocom s.f.m.c. |
| 5 | * Author: Daniel Willmann <daniel@sysmocom.de> |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 6 | * (C) 2023-2024 by Harald Welte <laforge@osmocom.org> |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 7 | * |
| 8 | * All Rights Reserved. |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | */ |
| 22 | |
| 23 | /* TODO: |
| 24 | * Parameters: |
| 25 | * - number of simultaneous read/write in uring for given fd |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include "../config.h" |
| 30 | #if defined(__linux__) |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <talloc.h> |
| 34 | #include <unistd.h> |
| 35 | #include <string.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <errno.h> |
| 38 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 39 | #include <netinet/in.h> |
| 40 | #include <netinet/sctp.h> |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 41 | #include <sys/eventfd.h> |
| 42 | #include <liburing.h> |
| 43 | |
| 44 | #include <osmocom/core/osmo_io.h> |
| 45 | #include <osmocom/core/linuxlist.h> |
| 46 | #include <osmocom/core/logging.h> |
| 47 | #include <osmocom/core/msgb.h> |
| 48 | #include <osmocom/core/select.h> |
| 49 | #include <osmocom/core/talloc.h> |
| 50 | #include <osmocom/core/utils.h> |
| 51 | #include <osmocom/core/socket.h> |
| 52 | |
| 53 | #include "osmo_io_internal.h" |
| 54 | |
| 55 | #define IOFD_URING_ENTRIES 4096 |
| 56 | |
| 57 | struct osmo_io_uring { |
| 58 | struct osmo_fd event_ofd; |
| 59 | struct io_uring ring; |
| 60 | }; |
| 61 | |
| 62 | static __thread struct osmo_io_uring g_ring; |
| 63 | |
| 64 | static void iofd_uring_cqe(struct io_uring *ring); |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 65 | |
| 66 | /*! read call-back for eventfd notifying us if entries are in the completion queue */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 67 | static int iofd_uring_poll_cb(struct osmo_fd *ofd, unsigned int what) |
| 68 | { |
| 69 | struct io_uring *ring = ofd->data; |
| 70 | eventfd_t val; |
| 71 | int rc; |
| 72 | |
| 73 | if (what & OSMO_FD_READ) { |
| 74 | rc = eventfd_read(ofd->fd, &val); |
| 75 | if (rc < 0) { |
| 76 | LOGP(DLIO, LOGL_ERROR, "eventfd_read() returned error\n"); |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | iofd_uring_cqe(ring); |
| 81 | } |
| 82 | if (what & OSMO_FD_WRITE) |
| 83 | OSMO_ASSERT(0); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /*! initialize the uring and tie it into our event loop */ |
| 89 | void osmo_iofd_uring_init(void) |
| 90 | { |
| 91 | int rc; |
| 92 | rc = io_uring_queue_init(IOFD_URING_ENTRIES, &g_ring.ring, 0); |
| 93 | if (rc < 0) |
| 94 | OSMO_ASSERT(0); |
| 95 | |
| 96 | rc = eventfd(0, 0); |
| 97 | if (rc < 0) { |
| 98 | io_uring_queue_exit(&g_ring.ring); |
| 99 | OSMO_ASSERT(0); |
| 100 | } |
| 101 | |
| 102 | osmo_fd_setup(&g_ring.event_ofd, rc, OSMO_FD_READ, iofd_uring_poll_cb, &g_ring.ring, 0); |
| 103 | osmo_fd_register(&g_ring.event_ofd); |
| 104 | io_uring_register_eventfd(&g_ring.ring, rc); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void iofd_uring_submit_recv(struct osmo_io_fd *iofd, enum iofd_msg_action action) |
| 109 | { |
| 110 | struct msgb *msg; |
| 111 | struct iofd_msghdr *msghdr; |
| 112 | struct io_uring_sqe *sqe; |
| 113 | |
| 114 | msg = iofd_msgb_pending_or_alloc(iofd); |
| 115 | if (!msg) { |
| 116 | LOGPIO(iofd, LOGL_ERROR, "Could not allocate msgb for reading\n"); |
| 117 | OSMO_ASSERT(0); |
| 118 | } |
| 119 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 120 | msghdr = iofd_msghdr_alloc(iofd, action, msg, iofd->cmsg_size); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 121 | if (!msghdr) { |
| 122 | LOGPIO(iofd, LOGL_ERROR, "Could not allocate msghdr for reading\n"); |
| 123 | OSMO_ASSERT(0); |
| 124 | } |
| 125 | |
| 126 | msghdr->iov[0].iov_base = msg->tail; |
| 127 | msghdr->iov[0].iov_len = msgb_tailroom(msg); |
| 128 | |
| 129 | switch (action) { |
| 130 | case IOFD_ACT_READ: |
| 131 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 132 | case IOFD_ACT_RECVMSG: |
| 133 | msghdr->hdr.msg_control = msghdr->cmsg; |
| 134 | msghdr->hdr.msg_controllen = iofd->cmsg_size; |
| 135 | /* fall-through */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 136 | case IOFD_ACT_RECVFROM: |
| 137 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 138 | msghdr->hdr.msg_iovlen = 1; |
| 139 | msghdr->hdr.msg_name = &msghdr->osa.u.sa; |
| 140 | msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); |
| 141 | break; |
| 142 | default: |
| 143 | OSMO_ASSERT(0); |
| 144 | } |
| 145 | |
| 146 | sqe = io_uring_get_sqe(&g_ring.ring); |
| 147 | if (!sqe) { |
| 148 | LOGPIO(iofd, LOGL_ERROR, "Could not get io_uring_sqe\n"); |
| 149 | OSMO_ASSERT(0); |
| 150 | } |
| 151 | |
| 152 | switch (action) { |
| 153 | case IOFD_ACT_READ: |
| 154 | io_uring_prep_readv(sqe, iofd->fd, msghdr->iov, 1, 0); |
| 155 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 156 | case IOFD_ACT_RECVMSG: |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 157 | case IOFD_ACT_RECVFROM: |
| 158 | io_uring_prep_recvmsg(sqe, iofd->fd, &msghdr->hdr, msghdr->flags); |
| 159 | break; |
| 160 | default: |
| 161 | OSMO_ASSERT(0); |
| 162 | } |
| 163 | io_uring_sqe_set_data(sqe, msghdr); |
| 164 | |
| 165 | io_uring_submit(&g_ring.ring); |
| 166 | /* NOTE: This only works if we have one read per fd */ |
| 167 | iofd->u.uring.read_msghdr = msghdr; |
| 168 | } |
| 169 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 170 | /*! completion call-back for READ/RECVFROM */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 171 | static void iofd_uring_handle_recv(struct iofd_msghdr *msghdr, int rc) |
| 172 | { |
| 173 | struct osmo_io_fd *iofd = msghdr->iofd; |
| 174 | struct msgb *msg = msghdr->msg; |
| 175 | |
| 176 | if (rc > 0) |
| 177 | msgb_put(msg, rc); |
| 178 | |
| 179 | if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 180 | iofd_handle_recv(iofd, msg, rc, msghdr); |
| 181 | |
| 182 | if (iofd->u.uring.read_enabled && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 183 | iofd_uring_submit_recv(iofd, msghdr->action); |
| 184 | else |
| 185 | iofd->u.uring.read_msghdr = NULL; |
| 186 | |
| 187 | |
| 188 | iofd_msghdr_free(msghdr); |
| 189 | } |
| 190 | |
| 191 | static int iofd_uring_submit_tx(struct osmo_io_fd *iofd); |
| 192 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 193 | /*! completion call-back for WRITE/SENDTO */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 194 | static void iofd_uring_handle_tx(struct iofd_msghdr *msghdr, int rc) |
| 195 | { |
| 196 | struct osmo_io_fd *iofd = msghdr->iofd; |
| 197 | |
Andreas Eversberg | 0f123aa | 2024-02-15 12:14:48 +0100 | [diff] [blame] | 198 | /* Detach msghdr from iofd. It might get freed here or it is freed during iofd_handle_send_completion(). |
| 199 | * If there is pending data to send, iofd_uring_submit_tx() will attach it again. |
| 200 | * iofd_handle_send_completion() will invoke a callback function to signal the possibility of write/send. |
| 201 | * This callback function might close iofd, leading to the potential freeing of iofd->u.uring.write_msghdr if |
| 202 | * still attached. Since iofd_handle_send_completion() frees msghdr at the end of the function, detaching |
| 203 | * msghdr here prevents a double-free bug. */ |
| 204 | if (iofd->u.uring.write_msghdr == msghdr) |
| 205 | iofd->u.uring.write_msghdr = NULL; |
| 206 | |
Daniel Willmann | 8461188 | 2023-11-21 10:17:00 +0100 | [diff] [blame] | 207 | if (OSMO_UNLIKELY(IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))) { |
| 208 | msgb_free(msghdr->msg); |
| 209 | iofd_msghdr_free(msghdr); |
| 210 | } else { |
| 211 | iofd_handle_send_completion(iofd, rc, msghdr); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 214 | /* submit the next to-be-transmitted message for this file descriptor */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 215 | if (iofd->u.uring.write_enabled && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 216 | iofd_uring_submit_tx(iofd); |
| 217 | } |
| 218 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 219 | /*! handle completion of a single I/O message */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 220 | static void iofd_uring_handle_completion(struct iofd_msghdr *msghdr, int res) |
| 221 | { |
| 222 | struct osmo_io_fd *iofd = msghdr->iofd; |
| 223 | |
| 224 | IOFD_FLAG_SET(iofd, IOFD_FLAG_IN_CALLBACK); |
| 225 | |
| 226 | switch (msghdr->action) { |
| 227 | case IOFD_ACT_READ: |
| 228 | case IOFD_ACT_RECVFROM: |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 229 | case IOFD_ACT_RECVMSG: |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 230 | iofd_uring_handle_recv(msghdr, res); |
| 231 | break; |
| 232 | case IOFD_ACT_WRITE: |
| 233 | case IOFD_ACT_SENDTO: |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 234 | case IOFD_ACT_SENDMSG: |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 235 | iofd_uring_handle_tx(msghdr, res); |
| 236 | break; |
| 237 | default: |
| 238 | OSMO_ASSERT(0) |
| 239 | } |
| 240 | |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 241 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_IN_CALLBACK); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 242 | |
| 243 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_TO_FREE) && !iofd->u.uring.read_msghdr && !iofd->u.uring.write_msghdr) |
| 244 | talloc_free(iofd); |
| 245 | } |
| 246 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 247 | /*! process all pending completion queue entries in given io_uring */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 248 | static void iofd_uring_cqe(struct io_uring *ring) |
| 249 | { |
| 250 | int rc; |
| 251 | struct io_uring_cqe *cqe; |
| 252 | struct iofd_msghdr *msghdr; |
| 253 | |
| 254 | while (io_uring_peek_cqe(ring, &cqe) == 0) { |
| 255 | |
| 256 | msghdr = io_uring_cqe_get_data(cqe); |
| 257 | if (!msghdr) { |
| 258 | LOGP(DLIO, LOGL_DEBUG, "Cancellation returned\n"); |
| 259 | io_uring_cqe_seen(ring, cqe); |
| 260 | continue; |
| 261 | } |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 262 | if (!msghdr->iofd) { |
| 263 | io_uring_cqe_seen(ring, cqe); |
| 264 | iofd_msghdr_free(msghdr); |
| 265 | continue; |
| 266 | } |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 267 | |
| 268 | rc = cqe->res; |
| 269 | /* Hand the entry back to the kernel before */ |
| 270 | io_uring_cqe_seen(ring, cqe); |
| 271 | |
| 272 | iofd_uring_handle_completion(msghdr, rc); |
| 273 | |
| 274 | } |
| 275 | } |
| 276 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 277 | /*! will submit the next to-be-transmitted message for given iofd */ |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 278 | static int iofd_uring_submit_tx(struct osmo_io_fd *iofd) |
| 279 | { |
| 280 | struct io_uring_sqe *sqe; |
| 281 | struct iofd_msghdr *msghdr; |
| 282 | |
| 283 | msghdr = iofd_txqueue_dequeue(iofd); |
| 284 | if (!msghdr) |
| 285 | return -ENODATA; |
| 286 | |
| 287 | sqe = io_uring_get_sqe(&g_ring.ring); |
| 288 | if (!sqe) { |
| 289 | LOGPIO(iofd, LOGL_ERROR, "Could not get io_uring_sqe\n"); |
| 290 | OSMO_ASSERT(0); |
| 291 | } |
| 292 | |
| 293 | io_uring_sqe_set_data(sqe, msghdr); |
| 294 | |
| 295 | switch (msghdr->action) { |
| 296 | case IOFD_ACT_WRITE: |
| 297 | case IOFD_ACT_SENDTO: |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 298 | case IOFD_ACT_SENDMSG: |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 299 | io_uring_prep_sendmsg(sqe, msghdr->iofd->fd, &msghdr->hdr, msghdr->flags); |
| 300 | break; |
| 301 | default: |
| 302 | OSMO_ASSERT(0); |
| 303 | } |
| 304 | |
| 305 | io_uring_submit(&g_ring.ring); |
| 306 | iofd->u.uring.write_msghdr = msghdr; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static void iofd_uring_write_enable(struct osmo_io_fd *iofd); |
| 312 | static void iofd_uring_read_enable(struct osmo_io_fd *iofd); |
| 313 | |
| 314 | static int iofd_uring_register(struct osmo_io_fd *iofd) |
| 315 | { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int iofd_uring_unregister(struct osmo_io_fd *iofd) |
| 320 | { |
| 321 | struct io_uring_sqe *sqe; |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 322 | struct iofd_msghdr *msghdr; |
| 323 | |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 324 | if (iofd->u.uring.read_msghdr) { |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 325 | msghdr = iofd->u.uring.read_msghdr; |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 326 | sqe = io_uring_get_sqe(&g_ring.ring); |
| 327 | OSMO_ASSERT(sqe != NULL); |
| 328 | io_uring_sqe_set_data(sqe, NULL); |
| 329 | LOGPIO(iofd, LOGL_DEBUG, "Cancelling read\n"); |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 330 | iofd->u.uring.read_msghdr = NULL; |
| 331 | talloc_steal(OTC_GLOBAL, msghdr); |
| 332 | msghdr->iofd = NULL; |
| 333 | io_uring_prep_cancel(sqe, msghdr, 0); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (iofd->u.uring.write_msghdr) { |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 337 | msghdr = iofd->u.uring.write_msghdr; |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 338 | sqe = io_uring_get_sqe(&g_ring.ring); |
| 339 | OSMO_ASSERT(sqe != NULL); |
| 340 | io_uring_sqe_set_data(sqe, NULL); |
| 341 | LOGPIO(iofd, LOGL_DEBUG, "Cancelling write\n"); |
Andreas Eversberg | 8db6009 | 2024-02-15 10:16:33 +0100 | [diff] [blame] | 342 | iofd->u.uring.write_msghdr = NULL; |
| 343 | talloc_steal(OTC_GLOBAL, msghdr); |
| 344 | msgb_free(msghdr->msg); |
| 345 | msghdr->iofd = NULL; |
| 346 | io_uring_prep_cancel(sqe, msghdr, 0); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 347 | } |
| 348 | io_uring_submit(&g_ring.ring); |
| 349 | |
Andreas Eversberg | d7256c6 | 2024-02-09 13:01:15 +0100 | [diff] [blame] | 350 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_NOTIFY_CONNECTED)) { |
| 351 | osmo_fd_unregister(&iofd->u.uring.connect_ofd); |
| 352 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_NOTIFY_CONNECTED); |
| 353 | } |
| 354 | |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static void iofd_uring_write_enable(struct osmo_io_fd *iofd) |
| 359 | { |
| 360 | iofd->u.uring.write_enabled = true; |
| 361 | |
| 362 | if (iofd->u.uring.write_msghdr) |
| 363 | return; |
| 364 | |
Andreas Eversberg | d7256c6 | 2024-02-09 13:01:15 +0100 | [diff] [blame] | 365 | /* This function is called again, once the socket is connected. */ |
| 366 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_NOTIFY_CONNECTED)) |
| 367 | return; |
| 368 | |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 369 | if (osmo_iofd_txqueue_len(iofd) > 0) |
| 370 | iofd_uring_submit_tx(iofd); |
| 371 | else if (iofd->mode == OSMO_IO_FD_MODE_READ_WRITE) { |
| 372 | /* Empty write request to check when the socket is connected */ |
| 373 | struct iofd_msghdr *msghdr; |
| 374 | struct io_uring_sqe *sqe; |
| 375 | struct msgb *msg = msgb_alloc_headroom(0, 0, "io_uring write dummy"); |
| 376 | if (!msg) { |
| 377 | LOGPIO(iofd, LOGL_ERROR, "Could not allocate msgb for writing\n"); |
| 378 | OSMO_ASSERT(0); |
| 379 | } |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 380 | msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 381 | if (!msghdr) { |
| 382 | LOGPIO(iofd, LOGL_ERROR, "Could not allocate msghdr for writing\n"); |
| 383 | OSMO_ASSERT(0); |
| 384 | } |
| 385 | |
| 386 | msghdr->iov[0].iov_base = msgb_data(msg); |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 387 | msghdr->iov[0].iov_len = msgb_length(msg); |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 388 | |
| 389 | sqe = io_uring_get_sqe(&g_ring.ring); |
| 390 | if (!sqe) { |
| 391 | LOGPIO(iofd, LOGL_ERROR, "Could not get io_uring_sqe\n"); |
| 392 | OSMO_ASSERT(0); |
| 393 | } |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 394 | io_uring_prep_writev(sqe, iofd->fd, msghdr->iov, 1, 0); |
| 395 | io_uring_sqe_set_data(sqe, msghdr); |
| 396 | |
| 397 | io_uring_submit(&g_ring.ring); |
| 398 | iofd->u.uring.write_msghdr = msghdr; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | static void iofd_uring_write_disable(struct osmo_io_fd *iofd) |
| 403 | { |
| 404 | iofd->u.uring.write_enabled = false; |
| 405 | } |
| 406 | |
| 407 | static void iofd_uring_read_enable(struct osmo_io_fd *iofd) |
| 408 | { |
| 409 | iofd->u.uring.read_enabled = true; |
| 410 | |
| 411 | if (iofd->u.uring.read_msghdr) |
| 412 | return; |
| 413 | |
Andreas Eversberg | d7256c6 | 2024-02-09 13:01:15 +0100 | [diff] [blame] | 414 | /* This function is called again, once the socket is connected. */ |
| 415 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_NOTIFY_CONNECTED)) |
| 416 | return; |
| 417 | |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 418 | switch (iofd->mode) { |
| 419 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 420 | iofd_uring_submit_recv(iofd, IOFD_ACT_READ); |
| 421 | break; |
| 422 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 423 | iofd_uring_submit_recv(iofd, IOFD_ACT_RECVFROM); |
| 424 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 425 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 426 | iofd_uring_submit_recv(iofd, IOFD_ACT_RECVMSG); |
| 427 | break; |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 428 | default: |
| 429 | OSMO_ASSERT(0); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static void iofd_uring_read_disable(struct osmo_io_fd *iofd) |
| 434 | { |
| 435 | iofd->u.uring.read_enabled = false; |
| 436 | } |
| 437 | |
| 438 | static int iofd_uring_close(struct osmo_io_fd *iofd) |
| 439 | { |
| 440 | iofd_uring_read_disable(iofd); |
| 441 | iofd_uring_write_disable(iofd); |
| 442 | iofd_uring_unregister(iofd); |
| 443 | return close(iofd->fd); |
| 444 | } |
| 445 | |
Andreas Eversberg | d7256c6 | 2024-02-09 13:01:15 +0100 | [diff] [blame] | 446 | /* called via osmocom poll/select main handling once outbound non-blocking connect() completes */ |
| 447 | static int iofd_uring_connected_cb(struct osmo_fd *ofd, unsigned int what) |
| 448 | { |
| 449 | struct osmo_io_fd *iofd = ofd->data; |
| 450 | |
| 451 | LOGPIO(iofd, LOGL_DEBUG, "Socket connected or failed."); |
| 452 | |
| 453 | if (!(what & OSMO_FD_WRITE)) |
| 454 | return 0; |
| 455 | |
| 456 | /* Unregister from poll/select handling. */ |
| 457 | osmo_fd_unregister(ofd); |
| 458 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_NOTIFY_CONNECTED); |
| 459 | |
| 460 | /* Notify the application about this via a zero-length write completion call-back. */ |
| 461 | IOFD_FLAG_SET(iofd, IOFD_FLAG_IN_CALLBACK); |
| 462 | switch (iofd->mode) { |
| 463 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 464 | iofd->io_ops.write_cb(iofd, 0, NULL); |
| 465 | break; |
| 466 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 467 | iofd->io_ops.sendto_cb(iofd, 0, NULL, NULL); |
| 468 | break; |
| 469 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 470 | iofd->io_ops.sendmsg_cb(iofd, 0, NULL); |
| 471 | break; |
| 472 | } |
| 473 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_IN_CALLBACK); |
| 474 | |
| 475 | /* If write/read notifications are pending, enable it now. */ |
| 476 | if (iofd->u.uring.write_enabled && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 477 | iofd_uring_write_enable(iofd); |
| 478 | if (iofd->u.uring.read_enabled && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 479 | iofd_uring_read_enable(iofd); |
| 480 | |
| 481 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_TO_FREE) && !iofd->u.uring.read_msghdr && !iofd->u.uring.write_msghdr) |
| 482 | talloc_free(iofd); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static void iofd_uring_notify_connected(struct osmo_io_fd *iofd) |
| 487 | { |
| 488 | if (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG) { |
| 489 | /* Don't call this function after enabling read or write. */ |
| 490 | OSMO_ASSERT(!iofd->u.uring.write_enabled && !iofd->u.uring.read_enabled); |
| 491 | |
| 492 | /* Use a temporary osmo_fd which we can use to notify us once the connection is established |
| 493 | * or failed (indicated by FD becoming writable). |
| 494 | * This is needed as (at least for SCTP sockets) one cannot submit a zero-length writev/sendmsg |
| 495 | * in order to get notification when the socekt is writable.*/ |
| 496 | if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_NOTIFY_CONNECTED)) { |
| 497 | osmo_fd_setup(&iofd->u.uring.connect_ofd, iofd->fd, OSMO_FD_WRITE, |
| 498 | iofd_uring_connected_cb, iofd, 0); |
Andreas Eversberg | ada88ce | 2024-03-01 17:47:44 +0100 | [diff] [blame^] | 499 | if (osmo_fd_register(&iofd->u.uring.connect_ofd) < 0) |
| 500 | LOGPIO(iofd, LOGL_ERROR, "Failed to register FD for connect event.\n"); |
| 501 | else |
| 502 | IOFD_FLAG_SET(iofd, IOFD_FLAG_NOTIFY_CONNECTED); |
Andreas Eversberg | d7256c6 | 2024-02-09 13:01:15 +0100 | [diff] [blame] | 503 | } |
| 504 | } else |
| 505 | iofd_uring_write_enable(iofd); |
| 506 | } |
| 507 | |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 508 | const struct iofd_backend_ops iofd_uring_ops = { |
| 509 | .register_fd = iofd_uring_register, |
| 510 | .unregister_fd = iofd_uring_unregister, |
| 511 | .close = iofd_uring_close, |
| 512 | .write_enable = iofd_uring_write_enable, |
| 513 | .write_disable = iofd_uring_write_disable, |
| 514 | .read_enable = iofd_uring_read_enable, |
| 515 | .read_disable = iofd_uring_read_disable, |
Andreas Eversberg | d7256c6 | 2024-02-09 13:01:15 +0100 | [diff] [blame] | 516 | .notify_connected = iofd_uring_notify_connected, |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | #endif /* defined(__linux__) */ |