Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 1 | /*! \file osmo_io.c |
| 2 | * New osmocom async I/O API. |
| 3 | * |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 4 | * (C) 2022-2024 by Harald Welte <laforge@osmocom.org> |
| 5 | * (C) 2022-2024 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 6 | * Author: Daniel Willmann <dwillmann@sysmocom.de> |
| 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 | #include "../config.h" |
| 24 | #if defined(__linux__) |
| 25 | |
| 26 | #include <fcntl.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <talloc.h> |
| 30 | #include <unistd.h> |
| 31 | #include <string.h> |
| 32 | #include <stdbool.h> |
| 33 | #include <errno.h> |
| 34 | |
| 35 | #include <osmocom/core/osmo_io.h> |
| 36 | #include <osmocom/core/linuxlist.h> |
| 37 | #include <osmocom/core/logging.h> |
| 38 | #include <osmocom/core/msgb.h> |
| 39 | #include <osmocom/core/socket.h> |
| 40 | #include <osmocom/core/talloc.h> |
| 41 | #include <osmocom/core/utils.h> |
| 42 | |
| 43 | #include "osmo_io_internal.h" |
| 44 | |
| 45 | /*! This environment variable can be set to manually set the backend used in osmo_io */ |
| 46 | #define OSMO_IO_BACKEND_ENV "LIBOSMO_IO_BACKEND" |
| 47 | |
| 48 | const struct value_string osmo_io_backend_names[] = { |
| 49 | { OSMO_IO_BACKEND_POLL, "poll" }, |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 50 | { OSMO_IO_BACKEND_IO_URING, "io_uring" }, |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 51 | { 0, NULL } |
| 52 | }; |
| 53 | |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame^] | 54 | const struct value_string osmo_iofd_mode_names[] = { |
| 55 | { OSMO_IO_FD_MODE_READ_WRITE, "read/write" }, |
| 56 | { OSMO_IO_FD_MODE_RECVFROM_SENDTO, "recvfrom/sendto" }, |
| 57 | { OSMO_IO_FD_MODE_RECVMSG_SENDMSG, "recvmsg/sendmsg" }, |
| 58 | { 0, NULL } |
| 59 | }; |
| 60 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 61 | static enum osmo_io_backend g_io_backend; |
| 62 | |
| 63 | /* Used by some tests, can't be static */ |
| 64 | struct iofd_backend_ops osmo_iofd_ops; |
| 65 | |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 66 | #if defined(HAVE_URING) |
| 67 | void osmo_iofd_uring_init(void); |
| 68 | #endif |
| 69 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 70 | /*! initialize osmo_io for the current thread */ |
| 71 | void osmo_iofd_init(void) |
| 72 | { |
| 73 | switch (g_io_backend) { |
| 74 | case OSMO_IO_BACKEND_POLL: |
| 75 | break; |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 76 | #if defined(HAVE_URING) |
| 77 | case OSMO_IO_BACKEND_IO_URING: |
| 78 | osmo_iofd_uring_init(); |
| 79 | break; |
| 80 | #endif |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 81 | default: |
| 82 | OSMO_ASSERT(0); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* ensure main thread always has pre-initialized osmo_io |
| 88 | * priority 103: run after on_dso_load_select */ |
| 89 | static __attribute__((constructor(103))) void on_dso_load_osmo_io(void) |
| 90 | { |
| 91 | char *backend = getenv(OSMO_IO_BACKEND_ENV); |
| 92 | if (backend == NULL) |
| 93 | backend = OSMO_IO_BACKEND_DEFAULT; |
| 94 | |
| 95 | if (!strcmp("POLL", backend)) { |
| 96 | g_io_backend = OSMO_IO_BACKEND_POLL; |
| 97 | osmo_iofd_ops = iofd_poll_ops; |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 98 | #if defined(HAVE_URING) |
| 99 | } else if (!strcmp("IO_URING", backend)) { |
| 100 | g_io_backend = OSMO_IO_BACKEND_IO_URING; |
| 101 | osmo_iofd_ops = iofd_uring_ops; |
| 102 | #endif |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 103 | } else { |
| 104 | fprintf(stderr, "Invalid osmo_io backend requested: \"%s\"\nCheck the environment variable %s\n", backend, OSMO_IO_BACKEND_ENV); |
| 105 | exit(1); |
| 106 | } |
| 107 | |
| 108 | osmo_iofd_init(); |
| 109 | } |
| 110 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 111 | /*! Allocate the msghdr. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 112 | * \param[in] iofd the osmo_io file structure |
| 113 | * \param[in] action the action this msg(hdr) is for (read, write, ..) |
| 114 | * \param[in] msg the msg buffer to use. Will allocate a new one if NULL |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 115 | * \param[in] cmsg_size size (in bytes) of iofd_msghdr.cmsg buffer. Can be 0 if cmsg is not used. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 116 | * \returns the newly allocated msghdr or NULL in case of error */ |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 117 | struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, |
| 118 | size_t cmsg_size) |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 119 | { |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 120 | bool free_msg = false; |
| 121 | struct iofd_msghdr *hdr; |
| 122 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 123 | if (!msg) { |
| 124 | msg = iofd_msgb_alloc(iofd); |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 125 | if (!msg) |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 126 | return NULL; |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 127 | free_msg = true; |
Daniel Willmann | f083382 | 2023-07-27 18:00:32 +0200 | [diff] [blame] | 128 | } else { |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 129 | talloc_steal(iofd, msg); |
| 130 | } |
| 131 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 132 | hdr = talloc_zero_size(iofd, sizeof(struct iofd_msghdr) + cmsg_size); |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 133 | if (!hdr) { |
| 134 | if (free_msg) |
| 135 | talloc_free(msg); |
| 136 | return NULL; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | hdr->action = action; |
| 140 | hdr->iofd = iofd; |
| 141 | hdr->msg = msg; |
| 142 | |
| 143 | return hdr; |
| 144 | } |
| 145 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 146 | /*! Free the msghdr. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 147 | * \param[in] msghdr the msghdr to free |
| 148 | */ |
| 149 | void iofd_msghdr_free(struct iofd_msghdr *msghdr) |
| 150 | { |
| 151 | /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or |
| 152 | * or passed on to the read callback which takes ownership. */ |
| 153 | talloc_free(msghdr); |
| 154 | } |
| 155 | |
| 156 | /*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */ |
| 157 | struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd) |
| 158 | { |
| 159 | uint16_t headroom = iofd->msgb_alloc.headroom; |
| 160 | |
| 161 | OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom); |
Andreas Eversberg | a4ac5b8 | 2024-02-28 16:36:29 +0100 | [diff] [blame] | 162 | return msgb_alloc_headroom_c(iofd, iofd->msgb_alloc.size + headroom, headroom, "osmo_io_msgb"); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /*! return the pending msgb in iofd or NULL if there is none*/ |
| 166 | struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd) |
| 167 | { |
| 168 | struct msgb *msg = NULL; |
| 169 | |
| 170 | msg = iofd->pending; |
| 171 | iofd->pending = NULL; |
| 172 | |
| 173 | return msg; |
| 174 | } |
| 175 | |
| 176 | /*! Return the pending msgb or allocate and return a new one */ |
| 177 | struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd) |
| 178 | { |
| 179 | struct msgb *msg = NULL; |
| 180 | |
| 181 | msg = iofd_msgb_pending(iofd); |
| 182 | if (!msg) |
| 183 | msg = iofd_msgb_alloc(iofd); |
| 184 | |
| 185 | return msg; |
| 186 | } |
| 187 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 188 | /*! Enqueue a message to be sent. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 189 | * |
| 190 | * Enqueues the message at the back of the queue provided there is enough space. |
| 191 | * \param[in] iofd the file descriptor |
| 192 | * \param[in] msghdr the message to enqueue |
| 193 | * \returns 0 if the message was enqueued succcessfully, |
| 194 | * -ENOSPC if the queue already contains the maximum number of messages |
| 195 | */ |
| 196 | int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr) |
| 197 | { |
| 198 | if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length) |
| 199 | return -ENOSPC; |
| 200 | |
| 201 | llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue); |
| 202 | iofd->tx_queue.current_length++; |
| 203 | |
Daniel Willmann | e4ecd99 | 2023-06-30 10:52:11 +0200 | [diff] [blame] | 204 | if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 205 | osmo_iofd_ops.write_enable(iofd); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 210 | /*! Enqueue a message at the front. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 211 | * |
| 212 | * Used to enqueue a msgb from a partial send again. This function will always |
| 213 | * enqueue the message, even if the maximum number of messages is reached. |
| 214 | * \param[in] iofd the file descriptor |
| 215 | * \param[in] msghdr the message to enqueue |
| 216 | */ |
| 217 | void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr) |
| 218 | { |
| 219 | llist_add(&msghdr->list, &iofd->tx_queue.msg_queue); |
| 220 | iofd->tx_queue.current_length++; |
Daniel Willmann | e4ecd99 | 2023-06-30 10:52:11 +0200 | [diff] [blame] | 221 | |
| 222 | if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 223 | osmo_iofd_ops.write_enable(iofd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 226 | /*! Dequeue a message from the front. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 227 | * |
| 228 | * \param[in] iofd the file descriptor |
| 229 | * \returns the msghdr from the front of the queue or NULL if the queue is empty |
| 230 | */ |
| 231 | struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd) |
| 232 | { |
| 233 | struct llist_head *lh; |
| 234 | |
| 235 | if (iofd->tx_queue.current_length == 0) |
| 236 | return NULL; |
| 237 | |
| 238 | lh = iofd->tx_queue.msg_queue.next; |
| 239 | |
| 240 | OSMO_ASSERT(lh); |
| 241 | iofd->tx_queue.current_length--; |
| 242 | llist_del(lh); |
| 243 | |
| 244 | if (iofd->tx_queue.current_length == 0) |
| 245 | osmo_iofd_ops.write_disable(iofd); |
| 246 | |
| 247 | return llist_entry(lh, struct iofd_msghdr, list); |
| 248 | } |
| 249 | |
| 250 | /*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain |
| 251 | * one complete message. |
| 252 | * If there are bytes left over, *pending_out will point to a msgb with the remaining data. |
| 253 | */ |
| 254 | static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out) |
| 255 | { |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 256 | int extra_len, received_len; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 257 | struct msgb *msg_pending; |
| 258 | |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 259 | /* Save the start of message before segmentation_cb (which could change it) */ |
| 260 | uint8_t *data = msg->data; |
| 261 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 262 | received_len = msgb_length(msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 263 | |
| 264 | if (!iofd->io_ops.segmentation_cb) { |
| 265 | *pending_out = NULL; |
| 266 | return IOFD_SEG_ACT_HANDLE_ONE; |
| 267 | } |
| 268 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 269 | int expected_len = iofd->io_ops.segmentation_cb(msg); |
| 270 | if (expected_len == -EAGAIN) { |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 271 | goto defer; |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 272 | } else if (expected_len < 0) { |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 273 | /* Something is wrong, skip this msgb */ |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 274 | LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n", |
| 275 | expected_len, received_len); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 276 | *pending_out = NULL; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 277 | msgb_free(msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 278 | return IOFD_SEG_ACT_DEFER; |
| 279 | } |
| 280 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 281 | extra_len = received_len - expected_len; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 282 | /* No segmentation needed, return the whole msgb */ |
| 283 | if (extra_len == 0) { |
| 284 | *pending_out = NULL; |
| 285 | return IOFD_SEG_ACT_HANDLE_ONE; |
| 286 | /* segment is incomplete */ |
| 287 | } else if (extra_len < 0) { |
| 288 | goto defer; |
| 289 | } |
| 290 | |
| 291 | /* msgb contains more than one segment */ |
| 292 | /* Copy the trailing data over */ |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 293 | msg_pending = iofd_msgb_alloc(iofd); |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 294 | memcpy(msgb_data(msg_pending), data + expected_len, extra_len); |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 295 | msgb_put(msg_pending, extra_len); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 296 | *pending_out = msg_pending; |
| 297 | |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 298 | /* Trim the original msgb to size. Don't use msgb_trim because we need to reference |
| 299 | * msg->data from before it might have been modified by the segmentation_cb(). */ |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 300 | msg->tail = data + expected_len; |
Daniel Willmann | 97d2144 | 2023-07-18 09:46:27 +0200 | [diff] [blame] | 301 | msg->len = msg->tail - msg->data; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 302 | return IOFD_SEG_ACT_HANDLE_MORE; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 303 | |
| 304 | defer: |
| 305 | *pending_out = msg; |
| 306 | return IOFD_SEG_ACT_DEFER; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /*! Restore message boundaries on read() and pass individual messages to the read callback |
| 310 | */ |
| 311 | void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc) |
| 312 | { |
| 313 | int res; |
| 314 | struct msgb *pending = NULL; |
| 315 | |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 316 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE); |
| 317 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 318 | if (rc <= 0) { |
| 319 | iofd->io_ops.read_cb(iofd, rc, msg); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | do { |
| 324 | res = iofd_handle_segmentation(iofd, msg, &pending); |
| 325 | if (res != IOFD_SEG_ACT_DEFER || rc < 0) |
| 326 | iofd->io_ops.read_cb(iofd, rc, msg); |
| 327 | if (res == IOFD_SEG_ACT_HANDLE_MORE) |
| 328 | msg = pending; |
| 329 | } while (res == IOFD_SEG_ACT_HANDLE_MORE); |
| 330 | |
| 331 | OSMO_ASSERT(iofd->pending == NULL); |
| 332 | iofd->pending = pending; |
| 333 | } |
| 334 | |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 335 | /*! completion handler: Called by osmo_io backend after a given I/O operation has completed |
| 336 | * \param[in] iofd I/O file-descriptor on which I/O has completed |
| 337 | * \param[in] msg message buffer containing data related to completed I/O |
Andreas Eversberg | 76f7678 | 2024-02-14 14:33:10 +0100 | [diff] [blame] | 338 | * \param[in] rc result code with read size or error (-errno) |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 339 | * \param[in] hdr serialized msghdr containing state of completed I/O */ |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame] | 340 | void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr) |
| 341 | { |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 342 | talloc_steal(iofd->msgb_alloc.ctx, msg); |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame] | 343 | switch (iofd->mode) { |
| 344 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 345 | iofd_handle_segmented_read(iofd, msg, rc); |
| 346 | break; |
| 347 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 348 | iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa); |
| 349 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 350 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 351 | iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr); |
| 352 | break; |
| 353 | default: |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame] | 354 | OSMO_ASSERT(false); |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
Daniel Willmann | 8461188 | 2023-11-21 10:17:00 +0100 | [diff] [blame] | 359 | /*! completion handler: Calld by osmo_io backend after a given I/O operation has completed |
| 360 | * \param[in] iofd I/O file-descriptor on which I/O has completed |
| 361 | * \param[in] rc return value of the I/O operation |
| 362 | * \param[in] msghdr serialized msghdr containing state of completed I/O |
| 363 | */ |
| 364 | void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr) |
| 365 | { |
| 366 | struct msgb *msg = msghdr->msg; |
| 367 | |
| 368 | /* Incomplete write */ |
| 369 | if (rc > 0 && rc < msgb_length(msg)) { |
| 370 | /* Re-enqueue remaining data */ |
| 371 | msgb_pull(msg, rc); |
| 372 | msghdr->iov[0].iov_len = msgb_length(msg); |
| 373 | iofd_txqueue_enqueue_front(iofd, msghdr); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | /* Reenqueue the complete msgb */ |
| 378 | if (rc == -EAGAIN) { |
| 379 | iofd_txqueue_enqueue_front(iofd, msghdr); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | /* All other failure and success cases are handled here */ |
| 384 | switch (msghdr->action) { |
| 385 | case IOFD_ACT_WRITE: |
| 386 | iofd->io_ops.write_cb(iofd, rc, msg); |
| 387 | break; |
| 388 | case IOFD_ACT_SENDTO: |
| 389 | iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa); |
| 390 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 391 | case IOFD_ACT_SENDMSG: |
| 392 | iofd->io_ops.sendmsg_cb(iofd, rc, msg); |
| 393 | break; |
Daniel Willmann | 8461188 | 2023-11-21 10:17:00 +0100 | [diff] [blame] | 394 | default: |
| 395 | OSMO_ASSERT(0); |
| 396 | } |
| 397 | |
| 398 | msgb_free(msghdr->msg); |
| 399 | iofd_msghdr_free(msghdr); |
| 400 | } |
| 401 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 402 | /* Public functions */ |
| 403 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 404 | /*! Send a message through a connected socket. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 405 | * |
| 406 | * Appends the message to the internal transmit queue. |
| 407 | * If the function returns success (0) it will take ownership of the msgb and |
| 408 | * internally call msgb_free() after the write request completes. |
| 409 | * In case of an error the msgb needs to be freed by the caller. |
| 410 | * \param[in] iofd file descriptor to write to |
| 411 | * \param[in] msg message buffer to write |
| 412 | * \returns 0 in case of success; a negative value in case of error |
| 413 | */ |
| 414 | int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg) |
| 415 | { |
| 416 | int rc; |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 417 | |
Andreas Eversberg | 2ce17da | 2024-02-09 14:36:30 +0100 | [diff] [blame] | 418 | if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { |
| 419 | LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
Daniel Willmann | afdfc6a | 2023-11-21 10:10:37 +0100 | [diff] [blame] | 423 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE); |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 424 | if (OSMO_UNLIKELY(!iofd->io_ops.write_cb)) { |
| 425 | LOGPIO(iofd, LOGL_ERROR, "write_cb not set, Rejecting msgb\n"); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 429 | struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 430 | if (!msghdr) |
| 431 | return -ENOMEM; |
| 432 | |
Daniel Willmann | 92efac2 | 2023-08-01 09:55:13 +0200 | [diff] [blame] | 433 | msghdr->flags = MSG_NOSIGNAL; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 434 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 435 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 436 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 437 | msghdr->hdr.msg_iovlen = 1; |
| 438 | |
| 439 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 440 | if (rc < 0) { |
| 441 | iofd_msghdr_free(msghdr); |
| 442 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 443 | return rc; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 449 | /*! Send a message through an unconnected socket. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 450 | * |
| 451 | * Appends the message to the internal transmit queue. |
| 452 | * If the function returns success (0), it will take ownership of the msgb and |
| 453 | * internally call msgb_free() after the write request completes. |
| 454 | * In case of an error the msgb needs to be freed by the caller. |
| 455 | * \param[in] iofd file descriptor to write to |
| 456 | * \param[in] msg message buffer to send |
| 457 | * \param[in] sendto_flags Flags to pass to the send call |
| 458 | * \param[in] dest destination address to send the message to |
| 459 | * \returns 0 in case of success; a negative value in case of error |
| 460 | */ |
| 461 | int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest) |
| 462 | { |
| 463 | int rc; |
| 464 | |
Andreas Eversberg | 2ce17da | 2024-02-09 14:36:30 +0100 | [diff] [blame] | 465 | if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { |
| 466 | LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 470 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO); |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 471 | if (OSMO_UNLIKELY(!iofd->io_ops.sendto_cb)) { |
| 472 | LOGPIO(iofd, LOGL_ERROR, "sendto_cb not set, Rejecting msgb\n"); |
| 473 | return -EINVAL; |
| 474 | } |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 475 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 476 | struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 477 | if (!msghdr) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | if (dest) { |
| 481 | msghdr->osa = *dest; |
| 482 | msghdr->hdr.msg_name = &msghdr->osa.u.sa; |
| 483 | msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); |
| 484 | } |
| 485 | msghdr->flags = sendto_flags; |
| 486 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 487 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 488 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 489 | msghdr->hdr.msg_iovlen = 1; |
| 490 | |
| 491 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 492 | if (rc < 0) { |
| 493 | iofd_msghdr_free(msghdr); |
| 494 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 495 | return rc; |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 501 | /*! ismo_io equivalent of the sendmsg(2) socket API call |
| 502 | * |
| 503 | * Appends the message to the internal transmit queue. |
| 504 | * If the function returns success (0), it will take ownership of the msgb and |
| 505 | * internally call msgb_free() after the write request completes. |
| 506 | * In case of an error the msgb needs to be freed by the caller. |
| 507 | * \param[in] iofd file descriptor to write to |
| 508 | * \param[in] msg message buffer to send; is used to fill msgh->iov[] |
| 509 | * \param[in] sendmsg_flags Flags to pass to the send call |
| 510 | * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty! |
| 511 | * \returns 0 in case of success; a negative value in case of error |
| 512 | */ |
| 513 | int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh) |
| 514 | { |
| 515 | int rc; |
| 516 | struct iofd_msghdr *msghdr; |
| 517 | |
Andreas Eversberg | 2ce17da | 2024-02-09 14:36:30 +0100 | [diff] [blame] | 518 | if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { |
| 519 | LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 523 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG); |
| 524 | if (OSMO_UNLIKELY(!iofd->io_ops.sendmsg_cb)) { |
| 525 | LOGPIO(iofd, LOGL_ERROR, "sendmsg_cb not set, Rejecting msgb\n"); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) { |
| 530 | LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n", |
| 531 | msgh->msg_namelen, sizeof(msghdr->osa)); |
| 532 | return -EINVAL; |
| 533 | } |
| 534 | |
| 535 | if (OSMO_UNLIKELY(msgh->msg_iovlen)) { |
| 536 | LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n"); |
| 537 | return -EINVAL; |
| 538 | } |
| 539 | |
| 540 | msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen); |
| 541 | if (!msghdr) |
| 542 | return -ENOMEM; |
| 543 | |
| 544 | /* copy over optional address */ |
| 545 | if (msgh->msg_name) { |
| 546 | memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen); |
| 547 | msghdr->hdr.msg_name = &msghdr->osa.u.sa; |
| 548 | msghdr->hdr.msg_namelen = msgh->msg_namelen; |
| 549 | } |
| 550 | |
| 551 | /* build iov from msgb */ |
| 552 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 553 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 554 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 555 | msghdr->hdr.msg_iovlen = 1; |
| 556 | |
| 557 | /* copy over the cmsg from the msghdr */ |
| 558 | if (msgh->msg_control && msgh->msg_controllen) { |
| 559 | msghdr->hdr.msg_control = msghdr->cmsg; |
| 560 | msghdr->hdr.msg_controllen = msgh->msg_controllen; |
| 561 | memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen); |
| 562 | } |
| 563 | |
| 564 | /* copy over msg_flags */ |
| 565 | msghdr->hdr.msg_flags = sendmsg_flags; |
| 566 | |
| 567 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 568 | if (rc < 0) { |
| 569 | iofd_msghdr_free(msghdr); |
| 570 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 571 | return rc; |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 577 | static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops) |
| 578 | { |
| 579 | switch (mode) { |
| 580 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 581 | if (ops->recvfrom_cb || ops->sendto_cb) |
| 582 | return false; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 583 | if (ops->recvmsg_cb || ops->sendmsg_cb) |
| 584 | return false; |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 585 | break; |
| 586 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 587 | if (ops->read_cb || ops->write_cb) |
| 588 | return false; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 589 | if (ops->recvmsg_cb || ops->sendmsg_cb) |
| 590 | return false; |
| 591 | break; |
| 592 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 593 | if (ops->recvfrom_cb || ops->sendto_cb) |
| 594 | return false; |
| 595 | if (ops->read_cb || ops->write_cb) |
| 596 | return false; |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 597 | break; |
| 598 | default: |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 605 | /*! Allocate and setup a new iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 606 | * \param[in] ctx the parent context from which to allocate |
| 607 | * \param[in] fd the underlying system file descriptor |
| 608 | * \param[in] name the name of the iofd |
| 609 | * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom() |
| 610 | * \param[in] ioops structure with read/write/send/recv callbacks |
| 611 | * \param[in] data user data pointer accessible by the ioops callbacks |
| 612 | * \returns The newly allocated osmo_io_fd struct or NULL on failure |
| 613 | */ |
| 614 | struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode, |
| 615 | const struct osmo_io_ops *ioops, void *data) |
| 616 | { |
Harald Welte | c380f29 | 2023-11-18 19:54:46 +0100 | [diff] [blame] | 617 | struct osmo_io_fd *iofd; |
| 618 | |
| 619 | /* reject unsupported/unknown modes */ |
| 620 | switch (mode) { |
| 621 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 622 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 623 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
Harald Welte | c380f29 | 2023-11-18 19:54:46 +0100 | [diff] [blame] | 624 | break; |
| 625 | default: |
| 626 | return NULL; |
| 627 | } |
| 628 | |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame^] | 629 | if (ioops && !check_mode_callback_compat(mode, ioops)) { |
| 630 | LOGP(DLIO, LOGL_ERROR, "iofd(%s): rejecting call-backs incompatible with mode %s\n", |
| 631 | name ? name : "unknown", osmo_iofd_mode_name(mode)); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 632 | return NULL; |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame^] | 633 | } |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 634 | |
Harald Welte | c380f29 | 2023-11-18 19:54:46 +0100 | [diff] [blame] | 635 | iofd = talloc_zero(ctx, struct osmo_io_fd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 636 | if (!iofd) |
| 637 | return NULL; |
| 638 | |
| 639 | iofd->fd = fd; |
| 640 | iofd->mode = mode; |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 641 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 642 | |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 643 | if (name) |
| 644 | iofd->name = talloc_strdup(iofd, name); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 645 | |
| 646 | if (ioops) |
| 647 | iofd->io_ops = *ioops; |
| 648 | |
| 649 | iofd->pending = NULL; |
| 650 | |
| 651 | iofd->data = data; |
| 652 | |
| 653 | iofd->msgb_alloc.ctx = ctx; |
| 654 | iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE; |
| 655 | iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM; |
| 656 | |
| 657 | iofd->tx_queue.max_length = 32; |
| 658 | INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue); |
| 659 | |
| 660 | return iofd; |
| 661 | } |
| 662 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 663 | /*! Set the size of the control message buffer allocated when submitting recvmsg */ |
| 664 | int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size) |
| 665 | { |
| 666 | if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG) |
| 667 | return -EINVAL; |
| 668 | |
| 669 | iofd->cmsg_size = cmsg_size; |
| 670 | return 0; |
| 671 | } |
| 672 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 673 | /*! Register the fd with the underlying backend. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 674 | * |
| 675 | * \param[in] iofd the iofd file descriptor |
| 676 | * \param[in] fd the system fd number that will be registeres. If negative will use the one already set. |
| 677 | * \returns zero on success, a negative value on error |
| 678 | */ |
| 679 | int osmo_iofd_register(struct osmo_io_fd *iofd, int fd) |
| 680 | { |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 681 | int rc = 0; |
| 682 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 683 | if (fd >= 0) |
| 684 | iofd->fd = fd; |
Harald Welte | 04e6f6e | 2024-03-07 09:57:22 +0100 | [diff] [blame] | 685 | else if (iofd->fd < 0) { |
| 686 | /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */ |
| 687 | LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd); |
| 688 | return -EBADF; |
| 689 | } |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 690 | |
| 691 | if (osmo_iofd_ops.register_fd) |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 692 | rc = osmo_iofd_ops.register_fd(iofd); |
Daniel Willmann | acb9776 | 2023-06-07 17:02:31 +0200 | [diff] [blame] | 693 | if (rc) |
| 694 | return rc; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 695 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 696 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 697 | if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) || |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 698 | (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) || |
| 699 | (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) { |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 700 | osmo_iofd_ops.read_enable(iofd); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 701 | } |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 702 | |
| 703 | if (iofd->tx_queue.current_length > 0) |
| 704 | osmo_iofd_ops.write_enable(iofd); |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 705 | |
| 706 | return rc; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 709 | /*! Unregister the fd from the underlying backend. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 710 | * |
| 711 | * \param[in] iofd the file descriptor |
| 712 | * \returns zero on success, a negative value on error |
| 713 | */ |
| 714 | int osmo_iofd_unregister(struct osmo_io_fd *iofd) |
| 715 | { |
| 716 | if (osmo_iofd_ops.unregister_fd) |
| 717 | return osmo_iofd_ops.unregister_fd(iofd); |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 718 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 723 | /*! Get the number of messages in the tx queue. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 724 | * |
| 725 | * \param[in] iofd the file descriptor |
| 726 | */ |
| 727 | unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd) |
| 728 | { |
| 729 | return iofd->tx_queue.current_length; |
| 730 | } |
| 731 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 732 | /*! Clear the transmit queue of the the iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 733 | * |
| 734 | * This function frees all messages currently pending in the transmit queue |
| 735 | * \param[in] iofd the file descriptor |
| 736 | */ |
| 737 | void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd) |
| 738 | { |
| 739 | struct iofd_msghdr *hdr; |
| 740 | while ((hdr = iofd_txqueue_dequeue(iofd))) { |
| 741 | msgb_free(hdr->msg); |
| 742 | iofd_msghdr_free(hdr); |
| 743 | } |
| 744 | } |
| 745 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 746 | /*! Free the iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 747 | * |
| 748 | * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so. |
| 749 | * The iofd will be closed before. |
| 750 | * \param[in] iofd the file descriptor |
| 751 | */ |
| 752 | void osmo_iofd_free(struct osmo_io_fd *iofd) |
| 753 | { |
| 754 | if (!iofd) |
| 755 | return; |
| 756 | |
| 757 | osmo_iofd_close(iofd); |
| 758 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 759 | if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) { |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 760 | talloc_free(iofd); |
| 761 | } else { |
| 762 | /* Prevent our parent context from freeing us prematurely */ |
| 763 | talloc_steal(NULL, iofd); |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 764 | IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 768 | /*! Close the iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 769 | * |
| 770 | * This function closes the underlying fd and clears any messages in the tx queue |
| 771 | * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register() |
| 772 | * \param[in] iofd the file descriptor |
| 773 | * \ returns 0 on success, a negative value otherwise |
| 774 | */ |
| 775 | int osmo_iofd_close(struct osmo_io_fd *iofd) |
| 776 | { |
| 777 | int rc = 0; |
| 778 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 779 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 780 | return rc; |
| 781 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 782 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 783 | |
| 784 | /* Free pending msgs in tx queue */ |
| 785 | osmo_iofd_txqueue_clear(iofd); |
| 786 | msgb_free(iofd->pending); |
| 787 | |
| 788 | iofd->pending = NULL; |
| 789 | |
| 790 | if (osmo_iofd_ops.close) |
| 791 | rc = osmo_iofd_ops.close(iofd); |
| 792 | iofd->fd = -1; |
| 793 | return rc; |
| 794 | } |
| 795 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 796 | /*! Set the size and headroom of the msgb allocated when receiving messages. |
Daniel Willmann | 4731e71 | 2023-07-07 11:21:15 +0200 | [diff] [blame] | 797 | * \param[in] iofd the file descriptor |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 798 | * \param[in] size the size of the msgb when receiving data |
| 799 | * \param[in] headroom the headroom of the msgb when receiving data |
| 800 | */ |
| 801 | void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom) |
| 802 | { |
| 803 | iofd->msgb_alloc.headroom = headroom; |
| 804 | iofd->msgb_alloc.size = size; |
| 805 | } |
| 806 | |
Daniel Willmann | a9303f3 | 2023-07-07 11:20:48 +0200 | [diff] [blame] | 807 | /*! Set the maximum number of messages enqueued for sending. |
| 808 | * \param[in] iofd the file descriptor |
| 809 | * \param[in] size the maximum size of the transmit queue |
| 810 | */ |
| 811 | void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length) |
| 812 | { |
| 813 | iofd->tx_queue.max_length = max_length; |
| 814 | } |
| 815 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 816 | /*! Get the associated user-data from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 817 | * \param[in] iofd the file descriptor |
| 818 | * \returns the data that was previously set with \ref osmo_iofd_setup() |
| 819 | */ |
| 820 | void *osmo_iofd_get_data(const struct osmo_io_fd *iofd) |
| 821 | { |
| 822 | return iofd->data; |
| 823 | } |
| 824 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 825 | /*! Set the associated user-data from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 826 | * \param[in] iofd the file descriptor |
| 827 | * \param[in] data the data to set |
| 828 | */ |
| 829 | void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data) |
| 830 | { |
| 831 | iofd->data = data; |
| 832 | } |
| 833 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 834 | /*! Get the private number from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 835 | * \param[in] iofd the file descriptor |
| 836 | * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr() |
| 837 | */ |
| 838 | unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd) |
| 839 | { |
| 840 | return iofd->priv_nr; |
| 841 | } |
| 842 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 843 | /*! Set the private number from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 844 | * \param[in] iofd the file descriptor |
| 845 | * \param[in] priv_nr the private number to set |
| 846 | */ |
| 847 | void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr) |
| 848 | { |
| 849 | iofd->priv_nr = priv_nr; |
| 850 | } |
| 851 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 852 | /*! Get the underlying file descriptor from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 853 | * \param[in] iofd the file descriptor |
| 854 | * \returns the underlying file descriptor number */ |
| 855 | int osmo_iofd_get_fd(const struct osmo_io_fd *iofd) |
| 856 | { |
| 857 | return iofd->fd; |
| 858 | } |
| 859 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 860 | /*! Get the name of the file descriptor. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 861 | * \param[in] iofd the file descriptor |
| 862 | * \returns the name of the iofd as given in \ref osmo_iofd_setup() */ |
| 863 | const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd) |
| 864 | { |
| 865 | return iofd->name; |
| 866 | } |
| 867 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 868 | /*! Set the name of the file descriptor. |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 869 | * \param[in] iofd the file descriptor |
| 870 | * \param[in] name the name to set on the file descriptor */ |
| 871 | void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name) |
| 872 | { |
| 873 | osmo_talloc_replace_string(iofd, &iofd->name, name); |
| 874 | } |
| 875 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 876 | /*! Set the osmo_io_ops for an iofd. |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 877 | * \param[in] iofd Target iofd file descriptor |
| 878 | * \param[in] ioops osmo_io_ops structure to be set */ |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 879 | int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops) |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 880 | { |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame^] | 881 | if (!check_mode_callback_compat(iofd->mode, ioops)) { |
| 882 | LOGPIO(iofd, LOGL_ERROR, "rejecting call-backs incompatible with mode %s\n", |
| 883 | osmo_iofd_mode_name(iofd->mode)); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 884 | return -EINVAL; |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame^] | 885 | } |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 886 | |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 887 | iofd->io_ops = *ioops; |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 888 | |
| 889 | switch (iofd->mode) { |
| 890 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 891 | if (iofd->io_ops.read_cb) |
| 892 | osmo_iofd_ops.read_enable(iofd); |
| 893 | else |
| 894 | osmo_iofd_ops.read_disable(iofd); |
| 895 | break; |
| 896 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 897 | if (iofd->io_ops.recvfrom_cb) |
| 898 | osmo_iofd_ops.read_enable(iofd); |
| 899 | else |
| 900 | osmo_iofd_ops.read_disable(iofd); |
| 901 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 902 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 903 | if (iofd->io_ops.recvmsg_cb) |
| 904 | osmo_iofd_ops.read_enable(iofd); |
| 905 | else |
| 906 | osmo_iofd_ops.read_disable(iofd); |
| 907 | break; |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 908 | default: |
| 909 | OSMO_ASSERT(0); |
| 910 | } |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 911 | |
| 912 | return 0; |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Harald Welte | f574aea | 2024-02-23 12:07:03 +0100 | [diff] [blame] | 915 | /*! Get the osmo_io_ops for an iofd. |
| 916 | * \param[in] iofd Target iofd file descriptor |
| 917 | * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */ |
| 918 | void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops) |
| 919 | { |
| 920 | *ioops = iofd->io_ops; |
| 921 | } |
| 922 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 923 | /*! Notify the user if/when the socket is connected. |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 924 | * When the socket is connected the write_cb will be called. |
| 925 | * \param[in] iofd the file descriptor */ |
| 926 | void osmo_iofd_notify_connected(struct osmo_io_fd *iofd) |
| 927 | { |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 928 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE || |
| 929 | iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG); |
Andreas Eversberg | 848faf9 | 2024-02-09 12:38:17 +0100 | [diff] [blame] | 930 | OSMO_ASSERT(osmo_iofd_ops.notify_connected); |
| 931 | osmo_iofd_ops.notify_connected(iofd); |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 935 | #endif /* defined(__linux__) */ |