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