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