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" }, |
| 50 | { 0, NULL } |
| 51 | }; |
| 52 | |
| 53 | static enum osmo_io_backend g_io_backend; |
| 54 | |
| 55 | /* Used by some tests, can't be static */ |
| 56 | struct iofd_backend_ops osmo_iofd_ops; |
| 57 | |
| 58 | /*! initialize osmo_io for the current thread */ |
| 59 | void osmo_iofd_init(void) |
| 60 | { |
| 61 | switch (g_io_backend) { |
| 62 | case OSMO_IO_BACKEND_POLL: |
| 63 | break; |
| 64 | default: |
| 65 | OSMO_ASSERT(0); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* ensure main thread always has pre-initialized osmo_io |
| 71 | * priority 103: run after on_dso_load_select */ |
| 72 | static __attribute__((constructor(103))) void on_dso_load_osmo_io(void) |
| 73 | { |
| 74 | char *backend = getenv(OSMO_IO_BACKEND_ENV); |
| 75 | if (backend == NULL) |
| 76 | backend = OSMO_IO_BACKEND_DEFAULT; |
| 77 | |
| 78 | if (!strcmp("POLL", backend)) { |
| 79 | g_io_backend = OSMO_IO_BACKEND_POLL; |
| 80 | osmo_iofd_ops = iofd_poll_ops; |
| 81 | } else { |
| 82 | fprintf(stderr, "Invalid osmo_io backend requested: \"%s\"\nCheck the environment variable %s\n", backend, OSMO_IO_BACKEND_ENV); |
| 83 | exit(1); |
| 84 | } |
| 85 | |
| 86 | osmo_iofd_init(); |
| 87 | } |
| 88 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 89 | /*! Allocate the msghdr. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 90 | * \param[in] iofd the osmo_io file structure |
| 91 | * \param[in] action the action this msg(hdr) is for (read, write, ..) |
| 92 | * \param[in] msg the msg buffer to use. Will allocate a new one if NULL |
| 93 | * \returns the newly allocated msghdr or NULL in case of error */ |
| 94 | struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg) |
| 95 | { |
| 96 | struct iofd_msghdr *hdr = talloc_zero(iofd, struct iofd_msghdr); |
| 97 | if (!hdr) |
| 98 | return NULL; |
| 99 | if (!msg) { |
| 100 | msg = iofd_msgb_alloc(iofd); |
| 101 | if (!msg) { |
| 102 | talloc_free(hdr); |
| 103 | return NULL; |
| 104 | } |
Daniel Willmann | f083382 | 2023-07-27 18:00:32 +0200 | [diff] [blame] | 105 | } else { |
| 106 | talloc_steal(iofd->msgb_alloc.ctx, msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | hdr->action = action; |
| 110 | hdr->iofd = iofd; |
| 111 | hdr->msg = msg; |
| 112 | |
| 113 | return hdr; |
| 114 | } |
| 115 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 116 | /*! Free the msghdr. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 117 | * \param[in] msghdr the msghdr to free |
| 118 | */ |
| 119 | void iofd_msghdr_free(struct iofd_msghdr *msghdr) |
| 120 | { |
| 121 | /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or |
| 122 | * or passed on to the read callback which takes ownership. */ |
| 123 | talloc_free(msghdr); |
| 124 | } |
| 125 | |
| 126 | /*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */ |
| 127 | struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd) |
| 128 | { |
| 129 | uint16_t headroom = iofd->msgb_alloc.headroom; |
| 130 | |
| 131 | OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom); |
| 132 | return msgb_alloc_headroom_c(iofd->msgb_alloc.ctx, |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 133 | iofd->msgb_alloc.size + headroom, headroom, |
| 134 | iofd->name ? : "iofd_msgb"); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /*! return the pending msgb in iofd or NULL if there is none*/ |
| 138 | struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd) |
| 139 | { |
| 140 | struct msgb *msg = NULL; |
| 141 | |
| 142 | msg = iofd->pending; |
| 143 | iofd->pending = NULL; |
| 144 | |
| 145 | return msg; |
| 146 | } |
| 147 | |
| 148 | /*! Return the pending msgb or allocate and return a new one */ |
| 149 | struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd) |
| 150 | { |
| 151 | struct msgb *msg = NULL; |
| 152 | |
| 153 | msg = iofd_msgb_pending(iofd); |
| 154 | if (!msg) |
| 155 | msg = iofd_msgb_alloc(iofd); |
| 156 | |
| 157 | return msg; |
| 158 | } |
| 159 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 160 | /*! Enqueue a message to be sent. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 161 | * |
| 162 | * Enqueues the message at the back of the queue provided there is enough space. |
| 163 | * \param[in] iofd the file descriptor |
| 164 | * \param[in] msghdr the message to enqueue |
| 165 | * \returns 0 if the message was enqueued succcessfully, |
| 166 | * -ENOSPC if the queue already contains the maximum number of messages |
| 167 | */ |
| 168 | int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr) |
| 169 | { |
| 170 | if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length) |
| 171 | return -ENOSPC; |
| 172 | |
| 173 | llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue); |
| 174 | iofd->tx_queue.current_length++; |
| 175 | |
Daniel Willmann | e4ecd99 | 2023-06-30 10:52:11 +0200 | [diff] [blame] | 176 | 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] | 177 | osmo_iofd_ops.write_enable(iofd); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 182 | /*! Enqueue a message at the front. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 183 | * |
| 184 | * Used to enqueue a msgb from a partial send again. This function will always |
| 185 | * enqueue the message, even if the maximum number of messages is reached. |
| 186 | * \param[in] iofd the file descriptor |
| 187 | * \param[in] msghdr the message to enqueue |
| 188 | */ |
| 189 | void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr) |
| 190 | { |
| 191 | llist_add(&msghdr->list, &iofd->tx_queue.msg_queue); |
| 192 | iofd->tx_queue.current_length++; |
Daniel Willmann | e4ecd99 | 2023-06-30 10:52:11 +0200 | [diff] [blame] | 193 | |
| 194 | if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
| 195 | osmo_iofd_ops.write_enable(iofd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 198 | /*! Dequeue a message from the front. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 199 | * |
| 200 | * \param[in] iofd the file descriptor |
| 201 | * \returns the msghdr from the front of the queue or NULL if the queue is empty |
| 202 | */ |
| 203 | struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd) |
| 204 | { |
| 205 | struct llist_head *lh; |
| 206 | |
| 207 | if (iofd->tx_queue.current_length == 0) |
| 208 | return NULL; |
| 209 | |
| 210 | lh = iofd->tx_queue.msg_queue.next; |
| 211 | |
| 212 | OSMO_ASSERT(lh); |
| 213 | iofd->tx_queue.current_length--; |
| 214 | llist_del(lh); |
| 215 | |
| 216 | if (iofd->tx_queue.current_length == 0) |
| 217 | osmo_iofd_ops.write_disable(iofd); |
| 218 | |
| 219 | return llist_entry(lh, struct iofd_msghdr, list); |
| 220 | } |
| 221 | |
| 222 | /*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain |
| 223 | * one complete message. |
| 224 | * If there are bytes left over, *pending_out will point to a msgb with the remaining data. |
| 225 | */ |
| 226 | static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out) |
| 227 | { |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 228 | int extra_len, received_len; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 229 | struct msgb *msg_pending; |
| 230 | |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 231 | /* Save the start of message before segmentation_cb (which could change it) */ |
| 232 | uint8_t *data = msg->data; |
| 233 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 234 | received_len = msgb_length(msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 235 | |
| 236 | if (!iofd->io_ops.segmentation_cb) { |
| 237 | *pending_out = NULL; |
| 238 | return IOFD_SEG_ACT_HANDLE_ONE; |
| 239 | } |
| 240 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 241 | int expected_len = iofd->io_ops.segmentation_cb(msg); |
| 242 | if (expected_len == -EAGAIN) { |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 243 | goto defer; |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 244 | } else if (expected_len < 0) { |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 245 | /* Something is wrong, skip this msgb */ |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 246 | LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n", |
| 247 | expected_len, received_len); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 248 | *pending_out = NULL; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 249 | msgb_free(msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 250 | return IOFD_SEG_ACT_DEFER; |
| 251 | } |
| 252 | |
arehbein | c0aa4bd | 2023-06-16 22:31:32 +0200 | [diff] [blame] | 253 | extra_len = received_len - expected_len; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 254 | /* No segmentation needed, return the whole msgb */ |
| 255 | if (extra_len == 0) { |
| 256 | *pending_out = NULL; |
| 257 | return IOFD_SEG_ACT_HANDLE_ONE; |
| 258 | /* segment is incomplete */ |
| 259 | } else if (extra_len < 0) { |
| 260 | goto defer; |
| 261 | } |
| 262 | |
| 263 | /* msgb contains more than one segment */ |
| 264 | /* Copy the trailing data over */ |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 265 | msg_pending = iofd_msgb_alloc(iofd); |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 266 | memcpy(msgb_data(msg_pending), data + expected_len, extra_len); |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 267 | msgb_put(msg_pending, extra_len); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 268 | *pending_out = msg_pending; |
| 269 | |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 270 | /* Trim the original msgb to size. Don't use msgb_trim because we need to reference |
| 271 | * 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] | 272 | msg->tail = data + expected_len; |
Daniel Willmann | 97d2144 | 2023-07-18 09:46:27 +0200 | [diff] [blame] | 273 | msg->len = msg->tail - msg->data; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 274 | return IOFD_SEG_ACT_HANDLE_MORE; |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 275 | |
| 276 | defer: |
| 277 | *pending_out = msg; |
| 278 | return IOFD_SEG_ACT_DEFER; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /*! Restore message boundaries on read() and pass individual messages to the read callback |
| 282 | */ |
| 283 | void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc) |
| 284 | { |
| 285 | int res; |
| 286 | struct msgb *pending = NULL; |
| 287 | |
| 288 | if (rc <= 0) { |
| 289 | iofd->io_ops.read_cb(iofd, rc, msg); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | do { |
| 294 | res = iofd_handle_segmentation(iofd, msg, &pending); |
| 295 | if (res != IOFD_SEG_ACT_DEFER || rc < 0) |
| 296 | iofd->io_ops.read_cb(iofd, rc, msg); |
| 297 | if (res == IOFD_SEG_ACT_HANDLE_MORE) |
| 298 | msg = pending; |
| 299 | } while (res == IOFD_SEG_ACT_HANDLE_MORE); |
| 300 | |
| 301 | OSMO_ASSERT(iofd->pending == NULL); |
| 302 | iofd->pending = pending; |
| 303 | } |
| 304 | |
Daniel Willmann | 2b34e92 | 2023-08-23 18:02:13 +0200 | [diff] [blame^] | 305 | void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr) |
| 306 | { |
| 307 | switch (iofd->mode) { |
| 308 | case OSMO_IO_FD_MODE_READ_WRITE: |
| 309 | iofd_handle_segmented_read(iofd, msg, rc); |
| 310 | break; |
| 311 | case OSMO_IO_FD_MODE_RECVFROM_SENDTO: |
| 312 | iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa); |
| 313 | break; |
| 314 | case OSMO_IO_FD_MODE_SCTP_RECVMSG_SENDMSG: |
| 315 | /* TODO Implement */ |
| 316 | OSMO_ASSERT(false); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 321 | /* Public functions */ |
| 322 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 323 | /*! Send a message through a connected socket. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 324 | * |
| 325 | * Appends the message to the internal transmit queue. |
| 326 | * If the function returns success (0) it will take ownership of the msgb and |
| 327 | * internally call msgb_free() after the write request completes. |
| 328 | * In case of an error the msgb needs to be freed by the caller. |
| 329 | * \param[in] iofd file descriptor to write to |
| 330 | * \param[in] msg message buffer to write |
| 331 | * \returns 0 in case of success; a negative value in case of error |
| 332 | */ |
| 333 | int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg) |
| 334 | { |
| 335 | int rc; |
| 336 | struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg); |
| 337 | if (!msghdr) |
| 338 | return -ENOMEM; |
| 339 | |
Daniel Willmann | 92efac2 | 2023-08-01 09:55:13 +0200 | [diff] [blame] | 340 | msghdr->flags = MSG_NOSIGNAL; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 341 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 342 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 343 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 344 | msghdr->hdr.msg_iovlen = 1; |
| 345 | |
| 346 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 347 | if (rc < 0) { |
| 348 | iofd_msghdr_free(msghdr); |
| 349 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 350 | return rc; |
| 351 | } |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 356 | /*! Send a message through an unconnected socket. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 357 | * |
| 358 | * Appends the message to the internal transmit queue. |
| 359 | * If the function returns success (0), it will take ownership of the msgb and |
| 360 | * internally call msgb_free() after the write request completes. |
| 361 | * In case of an error the msgb needs to be freed by the caller. |
| 362 | * \param[in] iofd file descriptor to write to |
| 363 | * \param[in] msg message buffer to send |
| 364 | * \param[in] sendto_flags Flags to pass to the send call |
| 365 | * \param[in] dest destination address to send the message to |
| 366 | * \returns 0 in case of success; a negative value in case of error |
| 367 | */ |
| 368 | int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest) |
| 369 | { |
| 370 | int rc; |
| 371 | |
| 372 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO); |
| 373 | |
| 374 | struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg); |
| 375 | if (!msghdr) |
| 376 | return -ENOMEM; |
| 377 | |
| 378 | if (dest) { |
| 379 | msghdr->osa = *dest; |
| 380 | msghdr->hdr.msg_name = &msghdr->osa.u.sa; |
| 381 | msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa); |
| 382 | } |
| 383 | msghdr->flags = sendto_flags; |
| 384 | msghdr->iov[0].iov_base = msgb_data(msghdr->msg); |
| 385 | msghdr->iov[0].iov_len = msgb_length(msghdr->msg); |
| 386 | msghdr->hdr.msg_iov = &msghdr->iov[0]; |
| 387 | msghdr->hdr.msg_iovlen = 1; |
| 388 | |
| 389 | rc = iofd_txqueue_enqueue(iofd, msghdr); |
| 390 | if (rc < 0) { |
| 391 | iofd_msghdr_free(msghdr); |
| 392 | LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc); |
| 393 | return rc; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 399 | /*! Allocate and setup a new iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 400 | * \param[in] ctx the parent context from which to allocate |
| 401 | * \param[in] fd the underlying system file descriptor |
| 402 | * \param[in] name the name of the iofd |
| 403 | * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom() |
| 404 | * \param[in] ioops structure with read/write/send/recv callbacks |
| 405 | * \param[in] data user data pointer accessible by the ioops callbacks |
| 406 | * \returns The newly allocated osmo_io_fd struct or NULL on failure |
| 407 | */ |
| 408 | struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode, |
| 409 | const struct osmo_io_ops *ioops, void *data) |
| 410 | { |
| 411 | struct osmo_io_fd *iofd = talloc_zero(ctx, struct osmo_io_fd); |
| 412 | if (!iofd) |
| 413 | return NULL; |
| 414 | |
| 415 | iofd->fd = fd; |
| 416 | iofd->mode = mode; |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 417 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 418 | |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 419 | if (name) |
| 420 | iofd->name = talloc_strdup(iofd, name); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 421 | |
| 422 | if (ioops) |
| 423 | iofd->io_ops = *ioops; |
| 424 | |
| 425 | iofd->pending = NULL; |
| 426 | |
| 427 | iofd->data = data; |
| 428 | |
| 429 | iofd->msgb_alloc.ctx = ctx; |
| 430 | iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE; |
| 431 | iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM; |
| 432 | |
| 433 | iofd->tx_queue.max_length = 32; |
| 434 | INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue); |
| 435 | |
| 436 | return iofd; |
| 437 | } |
| 438 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 439 | /*! Register the fd with the underlying backend. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 440 | * |
| 441 | * \param[in] iofd the iofd file descriptor |
| 442 | * \param[in] fd the system fd number that will be registeres. If negative will use the one already set. |
| 443 | * \returns zero on success, a negative value on error |
| 444 | */ |
| 445 | int osmo_iofd_register(struct osmo_io_fd *iofd, int fd) |
| 446 | { |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 447 | int rc = 0; |
| 448 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 449 | if (fd >= 0) |
| 450 | iofd->fd = fd; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 451 | |
| 452 | if (osmo_iofd_ops.register_fd) |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 453 | rc = osmo_iofd_ops.register_fd(iofd); |
Daniel Willmann | acb9776 | 2023-06-07 17:02:31 +0200 | [diff] [blame] | 454 | if (rc) |
| 455 | return rc; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 456 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 457 | IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED); |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 458 | osmo_iofd_ops.read_enable(iofd); |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 459 | |
| 460 | if (iofd->tx_queue.current_length > 0) |
| 461 | osmo_iofd_ops.write_enable(iofd); |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 462 | |
| 463 | return rc; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 466 | /*! Unregister the fd from the underlying backend. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 467 | * |
| 468 | * \param[in] iofd the file descriptor |
| 469 | * \returns zero on success, a negative value on error |
| 470 | */ |
| 471 | int osmo_iofd_unregister(struct osmo_io_fd *iofd) |
| 472 | { |
| 473 | if (osmo_iofd_ops.unregister_fd) |
| 474 | return osmo_iofd_ops.unregister_fd(iofd); |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 475 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 480 | /*! Get the number of messages in the tx queue. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 481 | * |
| 482 | * \param[in] iofd the file descriptor |
| 483 | */ |
| 484 | unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd) |
| 485 | { |
| 486 | return iofd->tx_queue.current_length; |
| 487 | } |
| 488 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 489 | /*! Clear the transmit queue of the the iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 490 | * |
| 491 | * This function frees all messages currently pending in the transmit queue |
| 492 | * \param[in] iofd the file descriptor |
| 493 | */ |
| 494 | void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd) |
| 495 | { |
| 496 | struct iofd_msghdr *hdr; |
| 497 | while ((hdr = iofd_txqueue_dequeue(iofd))) { |
| 498 | msgb_free(hdr->msg); |
| 499 | iofd_msghdr_free(hdr); |
| 500 | } |
| 501 | } |
| 502 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 503 | /*! Free the iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 504 | * |
| 505 | * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so. |
| 506 | * The iofd will be closed before. |
| 507 | * \param[in] iofd the file descriptor |
| 508 | */ |
| 509 | void osmo_iofd_free(struct osmo_io_fd *iofd) |
| 510 | { |
| 511 | if (!iofd) |
| 512 | return; |
| 513 | |
| 514 | osmo_iofd_close(iofd); |
| 515 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 516 | if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) { |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 517 | talloc_free(iofd); |
| 518 | } else { |
| 519 | /* Prevent our parent context from freeing us prematurely */ |
| 520 | talloc_steal(NULL, iofd); |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 521 | IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 525 | /*! Close the iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 526 | * |
| 527 | * This function closes the underlying fd and clears any messages in the tx queue |
| 528 | * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register() |
| 529 | * \param[in] iofd the file descriptor |
| 530 | * \ returns 0 on success, a negative value otherwise |
| 531 | */ |
| 532 | int osmo_iofd_close(struct osmo_io_fd *iofd) |
| 533 | { |
| 534 | int rc = 0; |
| 535 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 536 | if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED)) |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 537 | return rc; |
| 538 | |
Daniel Willmann | f89162f | 2023-06-26 19:24:46 +0200 | [diff] [blame] | 539 | IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 540 | |
| 541 | /* Free pending msgs in tx queue */ |
| 542 | osmo_iofd_txqueue_clear(iofd); |
| 543 | msgb_free(iofd->pending); |
| 544 | |
| 545 | iofd->pending = NULL; |
| 546 | |
| 547 | if (osmo_iofd_ops.close) |
| 548 | rc = osmo_iofd_ops.close(iofd); |
| 549 | iofd->fd = -1; |
| 550 | return rc; |
| 551 | } |
| 552 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 553 | /*! Set the size and headroom of the msgb allocated when receiving messages. |
Daniel Willmann | 4731e71 | 2023-07-07 11:21:15 +0200 | [diff] [blame] | 554 | * \param[in] iofd the file descriptor |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 555 | * \param[in] size the size of the msgb when receiving data |
| 556 | * \param[in] headroom the headroom of the msgb when receiving data |
| 557 | */ |
| 558 | void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom) |
| 559 | { |
| 560 | iofd->msgb_alloc.headroom = headroom; |
| 561 | iofd->msgb_alloc.size = size; |
| 562 | } |
| 563 | |
Daniel Willmann | a9303f3 | 2023-07-07 11:20:48 +0200 | [diff] [blame] | 564 | /*! Set the maximum number of messages enqueued for sending. |
| 565 | * \param[in] iofd the file descriptor |
| 566 | * \param[in] size the maximum size of the transmit queue |
| 567 | */ |
| 568 | void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length) |
| 569 | { |
| 570 | iofd->tx_queue.max_length = max_length; |
| 571 | } |
| 572 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 573 | /*! Get the associated user-data from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 574 | * \param[in] iofd the file descriptor |
| 575 | * \returns the data that was previously set with \ref osmo_iofd_setup() |
| 576 | */ |
| 577 | void *osmo_iofd_get_data(const struct osmo_io_fd *iofd) |
| 578 | { |
| 579 | return iofd->data; |
| 580 | } |
| 581 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 582 | /*! Set the associated user-data from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 583 | * \param[in] iofd the file descriptor |
| 584 | * \param[in] data the data to set |
| 585 | */ |
| 586 | void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data) |
| 587 | { |
| 588 | iofd->data = data; |
| 589 | } |
| 590 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 591 | /*! Get the private number from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 592 | * \param[in] iofd the file descriptor |
| 593 | * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr() |
| 594 | */ |
| 595 | unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd) |
| 596 | { |
| 597 | return iofd->priv_nr; |
| 598 | } |
| 599 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 600 | /*! Set the private number from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 601 | * \param[in] iofd the file descriptor |
| 602 | * \param[in] priv_nr the private number to set |
| 603 | */ |
| 604 | void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr) |
| 605 | { |
| 606 | iofd->priv_nr = priv_nr; |
| 607 | } |
| 608 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 609 | /*! Get the underlying file descriptor from an iofd. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 610 | * \param[in] iofd the file descriptor |
| 611 | * \returns the underlying file descriptor number */ |
| 612 | int osmo_iofd_get_fd(const struct osmo_io_fd *iofd) |
| 613 | { |
| 614 | return iofd->fd; |
| 615 | } |
| 616 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 617 | /*! Get the name of the file descriptor. |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 618 | * \param[in] iofd the file descriptor |
| 619 | * \returns the name of the iofd as given in \ref osmo_iofd_setup() */ |
| 620 | const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd) |
| 621 | { |
| 622 | return iofd->name; |
| 623 | } |
| 624 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 625 | /*! Set the name of the file descriptor. |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 626 | * \param[in] iofd the file descriptor |
| 627 | * \param[in] name the name to set on the file descriptor */ |
| 628 | void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name) |
| 629 | { |
| 630 | osmo_talloc_replace_string(iofd, &iofd->name, name); |
| 631 | } |
| 632 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 633 | /*! Set the osmo_io_ops for an iofd. |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 634 | * \param[in] iofd Target iofd file descriptor |
| 635 | * \param[in] ioops osmo_io_ops structure to be set */ |
| 636 | void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops) |
| 637 | { |
| 638 | iofd->io_ops = *ioops; |
| 639 | } |
| 640 | |
Daniel Willmann | d0d9ecb | 2023-07-12 11:55:52 +0200 | [diff] [blame] | 641 | /*! Notify the user if/when the socket is connected. |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 642 | * When the socket is connected the write_cb will be called. |
| 643 | * \param[in] iofd the file descriptor */ |
| 644 | void osmo_iofd_notify_connected(struct osmo_io_fd *iofd) |
| 645 | { |
| 646 | OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE); |
| 647 | osmo_iofd_ops.write_enable(iofd); |
| 648 | } |
| 649 | |
| 650 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 651 | #endif /* defined(__linux__) */ |