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