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