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 | { |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 270 | int extra_len, received_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 | |
| 278 | if (!iofd->io_ops.segmentation_cb) { |
| 279 | *pending_out = NULL; |
| 280 | return IOFD_SEG_ACT_HANDLE_ONE; |
| 281 | } |
| 282 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 283 | int expected_len = iofd->io_ops.segmentation_cb(msg); |
| 284 | if (expected_len == -EAGAIN) { |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 285 | goto defer; |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 286 | } else if (expected_len < 0) { |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 287 | /* Something is wrong, skip this msgb */ |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 288 | LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n", |
| 289 | expected_len, received_len); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 290 | *pending_out = NULL; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 291 | msgb_free(msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 292 | return IOFD_SEG_ACT_DEFER; |
| 293 | } |
| 294 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 295 | extra_len = received_len - expected_len; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 296 | /* No segmentation needed, return the whole msgb */ |
| 297 | if (extra_len == 0) { |
| 298 | *pending_out = NULL; |
| 299 | return IOFD_SEG_ACT_HANDLE_ONE; |
| 300 | /* segment is incomplete */ |
| 301 | } else if (extra_len < 0) { |
| 302 | goto defer; |
| 303 | } |
| 304 | |
| 305 | /* msgb contains more than one segment */ |
| 306 | /* Copy the trailing data over */ |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 307 | msg_pending = iofd_msgb_alloc(iofd); |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 308 | memcpy(msgb_data(msg_pending), data + expected_len, extra_len); |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 309 | msgb_put(msg_pending, extra_len); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 310 | *pending_out = msg_pending; |
| 311 | |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 312 | /* Trim the original msgb to size. Don't use msgb_trim because we need to reference |
| 313 | * 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] | 314 | msg->tail = data + expected_len; |
Daniel Willmann | 97d2144 | 2023-07-18 09:46:27 +0200 | [diff] [blame] | 315 | msg->len = msg->tail - msg->data; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 316 | return IOFD_SEG_ACT_HANDLE_MORE; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 317 | |
| 318 | defer: |
| 319 | *pending_out = msg; |
| 320 | return IOFD_SEG_ACT_DEFER; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /*! Restore message boundaries on read() and pass individual messages to the read callback |
| 324 | */ |
| 325 | void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc) |
| 326 | { |
| 327 | int res; |
| 328 | struct msgb *pending = NULL; |
| 329 | |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 330 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE); |
| 331 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 332 | if (rc <= 0) { |
| 333 | iofd->io_ops.read_cb(iofd, rc, msg); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | do { |
| 338 | res = iofd_handle_segmentation(iofd, msg, &pending); |
| 339 | if (res != IOFD_SEG_ACT_DEFER || rc < 0) |
| 340 | iofd->io_ops.read_cb(iofd, rc, msg); |
| 341 | if (res == IOFD_SEG_ACT_HANDLE_MORE) |
| 342 | msg = pending; |
| 343 | } while (res == IOFD_SEG_ACT_HANDLE_MORE); |
| 344 | |
| 345 | OSMO_ASSERT(iofd->pending == NULL); |
| 346 | iofd->pending = pending; |
| 347 | } |
| 348 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 349 | /*! 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] | 350 | * \param[in] iofd I/O file-descriptor on which I/O has completed |
| 351 | * \param[in] msg message buffer containing data related to completed I/O |
Andreas Eversberg | 76f7678 | 2024-02-14 14:33:10 +0100 | [diff] [blame] | 352 | * \param[in] rc result code with read size or error (-errno) |
Harald Welte | 987a86a | 2023-11-18 18:46:24 +0100 | [diff] [blame] | 353 | * \param[in] hdr serialized msghdr containing state of completed I/O */ |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame] | 354 | void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr) |
| 355 | { |
Daniel Willmann | 012d904 | 2023-08-10 10:47:25 +0200 | [diff] [blame] | 356 | talloc_steal(iofd->msgb_alloc.ctx, msg); |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame] | 357 | switch (iofd->mode) { |
| 358 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 359 | iofd_handle_segmented_read(iofd, msg, rc); |
| 360 | break; |
| 361 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 362 | iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa); |
| 363 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 364 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 365 | iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr); |
| 366 | break; |
| 367 | default: |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame] | 368 | OSMO_ASSERT(false); |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 373 | /*! 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] | 374 | * \param[in] iofd I/O file-descriptor on which I/O has completed |
| 375 | * \param[in] rc return value of the I/O operation |
| 376 | * \param[in] msghdr serialized msghdr containing state of completed I/O |
| 377 | */ |
| 378 | void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr) |
| 379 | { |
| 380 | struct msgb *msg = msghdr->msg; |
| 381 | |
| 382 | /* Incomplete write */ |
| 383 | if (rc > 0 && rc < msgb_length(msg)) { |
| 384 | /* Re-enqueue remaining data */ |
| 385 | msgb_pull(msg, rc); |
| 386 | msghdr->iov[0].iov_len = msgb_length(msg); |
| 387 | iofd_txqueue_enqueue_front(iofd, msghdr); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* Reenqueue the complete msgb */ |
| 392 | if (rc == -EAGAIN) { |
| 393 | iofd_txqueue_enqueue_front(iofd, msghdr); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | /* All other failure and success cases are handled here */ |
| 398 | switch (msghdr->action) { |
| 399 | case IOFD_ACT_WRITE: |
Harald Welte | 4ec4a38 | 2024-03-16 19:04:15 +0100 | [diff] [blame^] | 400 | if (iofd->io_ops.write_cb) |
| 401 | iofd->io_ops.write_cb(iofd, rc, msg); |
Daniel Willmann | 8461188 | 2023-11-21 10:17:00 +0100 | [diff] [blame] | 402 | break; |
| 403 | case IOFD_ACT_SENDTO: |
Harald Welte | 4ec4a38 | 2024-03-16 19:04:15 +0100 | [diff] [blame^] | 404 | if (iofd->io_ops.sendto_cb) |
| 405 | iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa); |
Daniel Willmann | 8461188 | 2023-11-21 10:17:00 +0100 | [diff] [blame] | 406 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 407 | case IOFD_ACT_SENDMSG: |
Harald Welte | 4ec4a38 | 2024-03-16 19:04:15 +0100 | [diff] [blame^] | 408 | if (iofd->io_ops.sendmsg_cb) |
| 409 | iofd->io_ops.sendmsg_cb(iofd, rc, msg); |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 410 | break; |
Daniel Willmann | 8461188 | 2023-11-21 10:17:00 +0100 | [diff] [blame] | 411 | default: |
| 412 | OSMO_ASSERT(0); |
| 413 | } |
| 414 | |
| 415 | msgb_free(msghdr->msg); |
| 416 | iofd_msghdr_free(msghdr); |
| 417 | } |
| 418 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 419 | /* Public functions */ |
| 420 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 421 | /*! Write a message to a file descriptor / connected socket. |
| 422 | * 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] | 423 | * |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 424 | * Appends the message to the internal transmit queue for eventual non-blocking |
| 425 | * write to the underlying socket/file descriptor. |
| 426 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 427 | * If the function returns success (0) it will take ownership of the msgb and |
| 428 | * internally call msgb_free() after the write request completes. |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 429 | * In case of an error, the msgb needs to be freed by the caller. |
| 430 | * |
| 431 | * \param[in] iofd osmo_io_fd file descriptor to write data to |
| 432 | * \param[in] msg message buffer containing the data to write |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 433 | * \returns 0 in case of success; a negative value in case of error |
| 434 | */ |
| 435 | int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg) |
| 436 | { |
| 437 | int rc; |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 438 | |
Andreas Eversberg | 2ce17da | 2024-02-09 14:36:30 +0100 | [diff] [blame] | 439 | if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { |
| 440 | LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | |
Daniel Willmann | afdfc6a | 2023-11-21 10:10:37 +0100 | [diff] [blame] | 444 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE); |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 445 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 446 | 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] | 447 | if (!msghdr) |
| 448 | return -ENOMEM; |
| 449 | |
Daniel Willmann | 92efac2 | 2023-08-01 09:55:13 +0200 | [diff] [blame] | 450 | msghdr->flags = MSG_NOSIGNAL; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 451 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 452 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 453 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 454 | msghdr->hdr.msg_iovlen = 1; |
| 455 | |
| 456 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 457 | if (rc < 0) { |
| 458 | iofd_msghdr_free(msghdr); |
| 459 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 460 | return rc; |
| 461 | } |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 466 | /*! Send a message through an unconnected socket. |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 467 | * 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] | 468 | * |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 469 | * Appends the message to the internal transmit queue for eventual non-blocking |
| 470 | * sendto on the underlying socket/file descriptor. |
| 471 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 472 | * 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] | 473 | * internally call msgb_free() after the sendto request completes. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 474 | * 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] | 475 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 476 | * \param[in] iofd file descriptor to write to |
| 477 | * \param[in] msg message buffer to send |
| 478 | * \param[in] sendto_flags Flags to pass to the send call |
| 479 | * \param[in] dest destination address to send the message to |
| 480 | * \returns 0 in case of success; a negative value in case of error |
| 481 | */ |
| 482 | int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest) |
| 483 | { |
| 484 | int rc; |
| 485 | |
Andreas Eversberg | 2ce17da | 2024-02-09 14:36:30 +0100 | [diff] [blame] | 486 | if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { |
| 487 | LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 491 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO); |
| 492 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 493 | 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] | 494 | if (!msghdr) |
| 495 | return -ENOMEM; |
| 496 | |
| 497 | if (dest) { |
| 498 | msghdr->osa = *dest; |
| 499 | msghdr->hdr.msg_name = &msghdr->osa.u.sa; |
| 500 | msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); |
| 501 | } |
| 502 | msghdr->flags = sendto_flags; |
| 503 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 504 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 505 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 506 | msghdr->hdr.msg_iovlen = 1; |
| 507 | |
| 508 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 509 | if (rc < 0) { |
| 510 | iofd_msghdr_free(msghdr); |
| 511 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 512 | return rc; |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 518 | /*! osmo_io equivalent of the sendmsg(2) socket API call. |
| 519 | * 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] | 520 | * |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 521 | * Appends the message to the internal transmit queue for eventual non-blocking |
| 522 | * sendmsg on the underlying socket/file descriptor. |
| 523 | * |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 524 | * 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] | 525 | * internally call msgb_free() after the sendmsg request completes. |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 526 | * 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] | 527 | * |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 528 | * \param[in] iofd file descriptor to write to |
| 529 | * \param[in] msg message buffer to send; is used to fill msgh->iov[] |
| 530 | * \param[in] sendmsg_flags Flags to pass to the send call |
| 531 | * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty! |
| 532 | * \returns 0 in case of success; a negative value in case of error |
| 533 | */ |
| 534 | int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh) |
| 535 | { |
| 536 | int rc; |
| 537 | struct iofd_msghdr *msghdr; |
| 538 | |
Andreas Eversberg | 2ce17da | 2024-02-09 14:36:30 +0100 | [diff] [blame] | 539 | if (OSMO_UNLIKELY(msgb_length(msg) == 0)) { |
| 540 | LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n"); |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 544 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG); |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 545 | |
| 546 | if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) { |
| 547 | LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n", |
| 548 | msgh->msg_namelen, sizeof(msghdr->osa)); |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | |
| 552 | if (OSMO_UNLIKELY(msgh->msg_iovlen)) { |
| 553 | LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n"); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen); |
| 558 | if (!msghdr) |
| 559 | return -ENOMEM; |
| 560 | |
| 561 | /* copy over optional address */ |
| 562 | if (msgh->msg_name) { |
| 563 | memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen); |
| 564 | msghdr->hdr.msg_name = &msghdr->osa.u.sa; |
| 565 | msghdr->hdr.msg_namelen = msgh->msg_namelen; |
| 566 | } |
| 567 | |
| 568 | /* build iov from msgb */ |
| 569 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 570 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 571 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 572 | msghdr->hdr.msg_iovlen = 1; |
| 573 | |
| 574 | /* copy over the cmsg from the msghdr */ |
| 575 | if (msgh->msg_control && msgh->msg_controllen) { |
| 576 | msghdr->hdr.msg_control = msghdr->cmsg; |
| 577 | msghdr->hdr.msg_controllen = msgh->msg_controllen; |
| 578 | memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen); |
| 579 | } |
| 580 | |
| 581 | /* copy over msg_flags */ |
| 582 | msghdr->hdr.msg_flags = sendmsg_flags; |
| 583 | |
| 584 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 585 | if (rc < 0) { |
| 586 | iofd_msghdr_free(msghdr); |
| 587 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 588 | return rc; |
| 589 | } |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 594 | static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops) |
| 595 | { |
| 596 | switch (mode) { |
| 597 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 598 | if (ops->recvfrom_cb || ops->sendto_cb) |
| 599 | return false; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 600 | if (ops->recvmsg_cb || ops->sendmsg_cb) |
| 601 | return false; |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 602 | break; |
| 603 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 604 | if (ops->read_cb || ops->write_cb) |
| 605 | return false; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 606 | if (ops->recvmsg_cb || ops->sendmsg_cb) |
| 607 | return false; |
| 608 | break; |
| 609 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 610 | if (ops->recvfrom_cb || ops->sendto_cb) |
| 611 | return false; |
| 612 | if (ops->read_cb || ops->write_cb) |
| 613 | return false; |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 614 | break; |
| 615 | default: |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | return true; |
| 620 | } |
| 621 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 622 | /*! Allocate and setup a new iofd. |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 623 | * |
| 624 | * Use this to create a new osmo_io_fd, specifying the osmo_io_fd_mode and osmo_io_ops, as well as optionally |
| 625 | * the file-descriptor number and a human-readable name. This is the first function you call for any |
| 626 | * osmo_io_fd. |
| 627 | * |
| 628 | * The created osmo_io_fd is not yet registered, and hence can not be used for any I/O until a subsequent |
| 629 | * call to osmo_iofd_register(). |
| 630 | * |
| 631 | * The created osmo_io_fd is initialized with some default settings: |
| 632 | * * msgb allocations size: OSMO_IO_DEFAULT_MSGB_SIZE (1024) |
| 633 | * * msgb headroom: OSMO_IO_DEFAULT_MSGB_HEADROOM (128) |
| 634 | * * tx_queue depth: 32 |
| 635 | * |
| 636 | * Those values may be adjusted from their defaults by using osmo_iofd_set_alloc_info() and |
| 637 | * osmo_iofd_set_txqueue_max_length() on the osmo_io_fd. |
| 638 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 639 | * \param[in] ctx the parent context from which to allocate |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 640 | * \param[in] fd the underlying system file descriptor. May be -1 if not known yet; must then be specified |
| 641 | * at subsequent osmo_iofd_register() time. |
| 642 | * \param[in] name the optional human-readable name of the iofd; may be NULL |
| 643 | * \param[in] mode the osmo_io_fd_mode of the iofd, whether it should use read()/write(), sendto()/recvfrom() |
| 644 | * semantics. |
| 645 | * \param[in] ioops structure specifying the read/write/send/recv callbacks. Will be copied to the iofd, so |
| 646 | * the caller does not have to keep it around after issuing the osmo_iofd_setup call. |
| 647 | * \param[in] data opaque user data pointer accessible by the ioops callbacks |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 648 | * \returns The newly allocated osmo_io_fd struct or NULL on failure |
| 649 | */ |
| 650 | struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode, |
| 651 | const struct osmo_io_ops *ioops, void *data) |
| 652 | { |
Harald Welte | c380f29 | 2023-11-18 19:54:46 +0100 | [diff] [blame] | 653 | struct osmo_io_fd *iofd; |
| 654 | |
| 655 | /* reject unsupported/unknown modes */ |
| 656 | switch (mode) { |
| 657 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 658 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 659 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
Harald Welte | c380f29 | 2023-11-18 19:54:46 +0100 | [diff] [blame] | 660 | break; |
| 661 | default: |
| 662 | return NULL; |
| 663 | } |
| 664 | |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame] | 665 | if (ioops && !check_mode_callback_compat(mode, ioops)) { |
| 666 | LOGP(DLIO, LOGL_ERROR, "iofd(%s): rejecting call-backs incompatible with mode %s\n", |
| 667 | name ? name : "unknown", osmo_iofd_mode_name(mode)); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 668 | return NULL; |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame] | 669 | } |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 670 | |
Harald Welte | c380f29 | 2023-11-18 19:54:46 +0100 | [diff] [blame] | 671 | iofd = talloc_zero(ctx, struct osmo_io_fd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 672 | if (!iofd) |
| 673 | return NULL; |
| 674 | |
| 675 | iofd->fd = fd; |
| 676 | iofd->mode = mode; |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 677 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 678 | |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 679 | if (name) |
| 680 | iofd->name = talloc_strdup(iofd, name); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 681 | |
| 682 | if (ioops) |
| 683 | iofd->io_ops = *ioops; |
| 684 | |
| 685 | iofd->pending = NULL; |
| 686 | |
| 687 | iofd->data = data; |
| 688 | |
| 689 | iofd->msgb_alloc.ctx = ctx; |
| 690 | iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE; |
| 691 | iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM; |
| 692 | |
| 693 | iofd->tx_queue.max_length = 32; |
| 694 | INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue); |
| 695 | |
| 696 | return iofd; |
| 697 | } |
| 698 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 699 | /*! Set the size of the control message buffer allocated when submitting recvmsg. |
| 700 | * |
| 701 | * If your osmo_io_fd is in OSMO_IO_FD_MODE_RECVMSG_SENDMSG mode, this API function can be used to tell the |
| 702 | * osmo_io code how much memory should be allocated for the cmsg (control message) buffer when performing |
| 703 | * recvmsg(). */ |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 704 | int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size) |
| 705 | { |
| 706 | if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | iofd->cmsg_size = cmsg_size; |
| 710 | return 0; |
| 711 | } |
| 712 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 713 | /*! Register the osmo_io_fd for active I/O. |
| 714 | * |
| 715 | * Calling this function will register a previously initialized osmo_io_fd for performing I/O. |
| 716 | * |
| 717 | * If the osmo_iofd has a read_cb/recvfrom_cb_recvmsg_cb set in its osmo_io_ops, read/receive will be |
| 718 | * automatically enabled and the respective call-back is called at any time data becomes available. |
| 719 | * |
| 720 | * If there is to-be-transmitted data in the transmit queue, write will be automatically enabled, allowing |
| 721 | * 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] | 722 | * |
| 723 | * \param[in] iofd the iofd file descriptor |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 724 | * \param[in] fd the system fd number that will be registered. If you did not yet specify the file descriptor |
| 725 | * number during osmo_fd_setup(), or if it has changed since then, you can state the [new] file descriptor |
| 726 | * 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] | 727 | * \returns zero on success, a negative value on error |
| 728 | */ |
| 729 | int osmo_iofd_register(struct osmo_io_fd *iofd, int fd) |
| 730 | { |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 731 | int rc = 0; |
| 732 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 733 | if (fd >= 0) |
| 734 | iofd->fd = fd; |
Harald Welte | 04e6f6e | 2024-03-07 09:57:22 +0100 | [diff] [blame] | 735 | else if (iofd->fd < 0) { |
| 736 | /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */ |
| 737 | LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd); |
| 738 | return -EBADF; |
| 739 | } |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 740 | |
Harald Welte | 8b7af44 | 2024-03-07 10:41:57 +0100 | [diff] [blame] | 741 | rc = osmo_iofd_ops.register_fd(iofd); |
Daniel Willmann | acb9776 | 2023-06-07 17:02:31 +0200 | [diff] [blame] | 742 | if (rc) |
| 743 | return rc; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 744 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 745 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 746 | 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] | 747 | (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) || |
| 748 | (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] | 749 | osmo_iofd_ops.read_enable(iofd); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 750 | } |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 751 | |
| 752 | if (iofd->tx_queue.current_length > 0) |
| 753 | osmo_iofd_ops.write_enable(iofd); |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 754 | |
| 755 | return rc; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 758 | /*! Unregister the given osmo_io_fd from osmo_io. |
| 759 | * |
| 760 | * After an osmo_io_fd has been successfully unregistered, it can no longer perform any I/O via osmo_io. |
| 761 | * However, it can be subsequently re-registered using osmo_iofd_register(). |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 762 | * |
| 763 | * \param[in] iofd the file descriptor |
| 764 | * \returns zero on success, a negative value on error |
| 765 | */ |
| 766 | int osmo_iofd_unregister(struct osmo_io_fd *iofd) |
| 767 | { |
Harald Welte | 8b7af44 | 2024-03-07 10:41:57 +0100 | [diff] [blame] | 768 | return osmo_iofd_ops.unregister_fd(iofd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 771 | /*! Retrieve the number of messages pending in the transmit queue. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 772 | * |
| 773 | * \param[in] iofd the file descriptor |
| 774 | */ |
| 775 | unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd) |
| 776 | { |
| 777 | return iofd->tx_queue.current_length; |
| 778 | } |
| 779 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 780 | /*! Clear the transmit queue of the given osmo_io_fd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 781 | * |
| 782 | * This function frees all messages currently pending in the transmit queue |
| 783 | * \param[in] iofd the file descriptor |
| 784 | */ |
| 785 | void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd) |
| 786 | { |
| 787 | struct iofd_msghdr *hdr; |
| 788 | while ((hdr = iofd_txqueue_dequeue(iofd))) { |
| 789 | msgb_free(hdr->msg); |
| 790 | iofd_msghdr_free(hdr); |
| 791 | } |
| 792 | } |
| 793 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 794 | /*! Free the given osmo_io_fd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 795 | * |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 796 | * The iofd will be automatically closed before via osmo_iofd_close() [which in turn will unregister |
| 797 | * it and clear any pending transmit queue items]. You must not reference the iofd |
| 798 | * after calling this function. However, it is safe to call this function from any of osmo_io |
| 799 | * call-backs; in this case, actual free will be internally delayed until that call-back completes. |
| 800 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 801 | * \param[in] iofd the file descriptor |
| 802 | */ |
| 803 | void osmo_iofd_free(struct osmo_io_fd *iofd) |
| 804 | { |
| 805 | if (!iofd) |
| 806 | return; |
| 807 | |
| 808 | osmo_iofd_close(iofd); |
| 809 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 810 | if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) { |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 811 | talloc_free(iofd); |
| 812 | } else { |
| 813 | /* Prevent our parent context from freeing us prematurely */ |
| 814 | talloc_steal(NULL, iofd); |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 815 | IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 819 | /*! Close the given osmo_io_fd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 820 | * |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 821 | * This function closes the underlying fd, unregisters it from osmo_io and clears any messages in the tx |
| 822 | * 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] | 823 | * \param[in] iofd the file descriptor |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 824 | * \returns 0 on success, a negative value otherwise |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 825 | */ |
| 826 | int osmo_iofd_close(struct osmo_io_fd *iofd) |
| 827 | { |
| 828 | int rc = 0; |
| 829 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 830 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 831 | return rc; |
| 832 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 833 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 834 | |
| 835 | /* Free pending msgs in tx queue */ |
| 836 | osmo_iofd_txqueue_clear(iofd); |
| 837 | msgb_free(iofd->pending); |
| 838 | |
| 839 | iofd->pending = NULL; |
| 840 | |
Harald Welte | e1d4858 | 2024-03-07 10:34:21 +0100 | [diff] [blame] | 841 | rc = osmo_iofd_ops.close(iofd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 842 | iofd->fd = -1; |
| 843 | return rc; |
| 844 | } |
| 845 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 846 | /*! Set the size and headroom of the msgb allocated when receiving messages. |
Daniel Willmann | 4731e71 | 2023-07-07 11:21:15 +0200 | [diff] [blame] | 847 | * \param[in] iofd the file descriptor |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 848 | * \param[in] size the size of the msgb when receiving data |
| 849 | * \param[in] headroom the headroom of the msgb when receiving data |
| 850 | */ |
| 851 | void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom) |
| 852 | { |
| 853 | iofd->msgb_alloc.headroom = headroom; |
| 854 | iofd->msgb_alloc.size = size; |
| 855 | } |
| 856 | |
Daniel Willmann | a9303f3 | 2023-07-07 11:20:48 +0200 | [diff] [blame] | 857 | /*! Set the maximum number of messages enqueued for sending. |
| 858 | * \param[in] iofd the file descriptor |
| 859 | * \param[in] size the maximum size of the transmit queue |
| 860 | */ |
| 861 | void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length) |
| 862 | { |
| 863 | iofd->tx_queue.max_length = max_length; |
| 864 | } |
| 865 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 866 | /*! Retrieve the associated user-data from an osmo_io_fd. |
| 867 | * |
| 868 | * A call to this function will return the opaque user data pointer which was specified previously |
| 869 | * via osmo_iofd_setup() or via osmo_iofd_set_data(). |
| 870 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 871 | * \param[in] iofd the file descriptor |
| 872 | * \returns the data that was previously set with \ref osmo_iofd_setup() |
| 873 | */ |
| 874 | void *osmo_iofd_get_data(const struct osmo_io_fd *iofd) |
| 875 | { |
| 876 | return iofd->data; |
| 877 | } |
| 878 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 879 | /*! Set the associated user-data from an osmo_io_fd. |
| 880 | * |
| 881 | * Calling this function will set/overwrite the opaque user data pointer, which can later be retrieved using |
| 882 | * osmo_iofd_get_data(). |
| 883 | * |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 884 | * \param[in] iofd the file descriptor |
| 885 | * \param[in] data the data to set |
| 886 | */ |
| 887 | void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data) |
| 888 | { |
| 889 | iofd->data = data; |
| 890 | } |
| 891 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 892 | /*! Retrieve the private number from an osmo_io_fd. |
| 893 | * 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] | 894 | * \param[in] iofd the file descriptor |
| 895 | * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr() |
| 896 | */ |
| 897 | unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd) |
| 898 | { |
| 899 | return iofd->priv_nr; |
| 900 | } |
| 901 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 902 | /*! Set the private number of an osmo_io_fd. |
| 903 | * The priv_nr passed in via this call can later be retrieved via osmo_iofd_get_priv_nr(). It provides |
| 904 | * 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] | 905 | * \param[in] iofd the file descriptor |
| 906 | * \param[in] priv_nr the private number to set |
| 907 | */ |
| 908 | void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr) |
| 909 | { |
| 910 | iofd->priv_nr = priv_nr; |
| 911 | } |
| 912 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 913 | /*! Retrieve the underlying file descriptor from an osmo_io_fd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 914 | * \param[in] iofd the file descriptor |
| 915 | * \returns the underlying file descriptor number */ |
| 916 | int osmo_iofd_get_fd(const struct osmo_io_fd *iofd) |
| 917 | { |
| 918 | return iofd->fd; |
| 919 | } |
| 920 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 921 | /*! Retrieve the human-readable name of the given osmo_io_fd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 922 | * \param[in] iofd the file descriptor |
| 923 | * \returns the name of the iofd as given in \ref osmo_iofd_setup() */ |
| 924 | const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd) |
| 925 | { |
| 926 | return iofd->name; |
| 927 | } |
| 928 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 929 | /*! Set the human-readable name of the file descriptor. |
| 930 | * 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] | 931 | * \param[in] iofd the file descriptor |
| 932 | * \param[in] name the name to set on the file descriptor */ |
| 933 | void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name) |
| 934 | { |
| 935 | osmo_talloc_replace_string(iofd, &iofd->name, name); |
| 936 | } |
| 937 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 938 | /*! Set the osmo_io_ops calbacks for an osmo_io_fd. |
| 939 | * This function can be used to update/overwrite the call-back functions for the given osmo_io_fd; it |
| 940 | * replaces the currently-set call-back function pointers from a previous call to osmo_iofd_set_ioops() |
| 941 | * or the original osmo_iofd_setup(). |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 942 | * \param[in] iofd Target iofd file descriptor |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 943 | * \param[in] ioops osmo_io_ops structure to be copied to the osmo_io_fd. |
| 944 | * \returns 0 on success, negative on error */ |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 945 | 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] | 946 | { |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame] | 947 | if (!check_mode_callback_compat(iofd->mode, ioops)) { |
| 948 | LOGPIO(iofd, LOGL_ERROR, "rejecting call-backs incompatible with mode %s\n", |
| 949 | osmo_iofd_mode_name(iofd->mode)); |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 950 | return -EINVAL; |
Harald Welte | 09ab041 | 2024-03-07 10:11:52 +0100 | [diff] [blame] | 951 | } |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 952 | |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 953 | iofd->io_ops = *ioops; |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 954 | |
| 955 | switch (iofd->mode) { |
| 956 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 957 | if (iofd->io_ops.read_cb) |
| 958 | osmo_iofd_ops.read_enable(iofd); |
| 959 | else |
| 960 | osmo_iofd_ops.read_disable(iofd); |
| 961 | break; |
| 962 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 963 | if (iofd->io_ops.recvfrom_cb) |
| 964 | osmo_iofd_ops.read_enable(iofd); |
| 965 | else |
| 966 | osmo_iofd_ops.read_disable(iofd); |
| 967 | break; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 968 | case OSMO_IO_FD_MODE_RECVMSG_SENDMSG: |
| 969 | if (iofd->io_ops.recvmsg_cb) |
| 970 | osmo_iofd_ops.read_enable(iofd); |
| 971 | else |
| 972 | osmo_iofd_ops.read_disable(iofd); |
| 973 | break; |
Daniel Willmann | 2386e9a | 2023-09-28 15:28:13 +0200 | [diff] [blame] | 974 | default: |
| 975 | OSMO_ASSERT(0); |
| 976 | } |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 977 | |
| 978 | return 0; |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 979 | } |
| 980 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 981 | /*! Retrieve the osmo_io_ops for an iofd. |
Harald Welte | f574aea | 2024-02-23 12:07:03 +0100 | [diff] [blame] | 982 | * \param[in] iofd Target iofd file descriptor |
| 983 | * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */ |
| 984 | void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops) |
| 985 | { |
| 986 | *ioops = iofd->io_ops; |
| 987 | } |
| 988 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 989 | /*! Request notification of the user if/when a client socket is connected. |
| 990 | * Calling this function will request osmo_io to notify the user (via |
| 991 | * write call-back) once a non-blocking outbound connect() of the |
| 992 | * socket completes. |
| 993 | * |
| 994 | * This only works for connection oriented sockets in either |
| 995 | * OSMO_IO_FD_MODE_READ_WRITE or OSMO_IO_FD_MODE_RECVMSG_SENDMSG mode. |
| 996 | * |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 997 | * \param[in] iofd the file descriptor */ |
| 998 | void osmo_iofd_notify_connected(struct osmo_io_fd *iofd) |
| 999 | { |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 1000 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE || |
| 1001 | iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG); |
Andreas Eversberg | 848faf9 | 2024-02-09 12:38:17 +0100 | [diff] [blame] | 1002 | osmo_iofd_ops.notify_connected(iofd); |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Harald Welte | e988ec3 | 2024-03-14 21:29:28 +0100 | [diff] [blame] | 1005 | /*! @} */ |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 1006 | |
Harald Welte | fab993b | 2024-03-14 21:27:40 +0100 | [diff] [blame] | 1007 | #endif /* ifndef(EMBEDDED) */ |