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