blob: b589cb71c9ffd051b1a77394124a8d8c1cc3c7df [file] [log] [blame]
Harald Weltee988ec32024-03-14 21:29:28 +01001/*
Harald Welte8857f3b2022-11-18 13:54:44 +01002 * New osmocom async I/O API.
3 *
Harald Welte1047ed72023-11-18 18:51:58 +01004 * (C) 2022-2024 by Harald Welte <laforge@osmocom.org>
5 * (C) 2022-2024 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Harald Welte8857f3b2022-11-18 13:54:44 +01006 * 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"
Harald Weltefab993b2024-03-14 21:27:40 +010024#ifndef EMBEDDED
Harald Welte8857f3b2022-11-18 13:54:44 +010025
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
Harald Weltee988ec32024-03-14 21:29:28 +010045/*! \addtogroup osmo_io
46 * @{
47 *
48 * \file osmo_io.c */
49
Harald Welte8857f3b2022-11-18 13:54:44 +010050/*! This environment variable can be set to manually set the backend used in osmo_io */
51#define OSMO_IO_BACKEND_ENV "LIBOSMO_IO_BACKEND"
52
53const struct value_string osmo_io_backend_names[] = {
54 { OSMO_IO_BACKEND_POLL, "poll" },
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010055 { OSMO_IO_BACKEND_IO_URING, "io_uring" },
Harald Welte8857f3b2022-11-18 13:54:44 +010056 { 0, NULL }
57};
58
Harald Welte09ab0412024-03-07 10:11:52 +010059const struct value_string osmo_iofd_mode_names[] = {
60 { OSMO_IO_FD_MODE_READ_WRITE, "read/write" },
61 { OSMO_IO_FD_MODE_RECVFROM_SENDTO, "recvfrom/sendto" },
62 { OSMO_IO_FD_MODE_RECVMSG_SENDMSG, "recvmsg/sendmsg" },
63 { 0, NULL }
64};
65
Harald Welte8857f3b2022-11-18 13:54:44 +010066static enum osmo_io_backend g_io_backend;
67
68/* Used by some tests, can't be static */
69struct iofd_backend_ops osmo_iofd_ops;
70
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010071#if defined(HAVE_URING)
72void osmo_iofd_uring_init(void);
73#endif
74
Harald Welte8857f3b2022-11-18 13:54:44 +010075/*! initialize osmo_io for the current thread */
76void osmo_iofd_init(void)
77{
78 switch (g_io_backend) {
79 case OSMO_IO_BACKEND_POLL:
80 break;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010081#if defined(HAVE_URING)
82 case OSMO_IO_BACKEND_IO_URING:
83 osmo_iofd_uring_init();
84 break;
85#endif
Harald Welte8857f3b2022-11-18 13:54:44 +010086 default:
87 OSMO_ASSERT(0);
88 break;
89 }
90}
91
92/* ensure main thread always has pre-initialized osmo_io
93 * priority 103: run after on_dso_load_select */
94static __attribute__((constructor(103))) void on_dso_load_osmo_io(void)
95{
96 char *backend = getenv(OSMO_IO_BACKEND_ENV);
97 if (backend == NULL)
98 backend = OSMO_IO_BACKEND_DEFAULT;
99
100 if (!strcmp("POLL", backend)) {
101 g_io_backend = OSMO_IO_BACKEND_POLL;
102 osmo_iofd_ops = iofd_poll_ops;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +0100103#if defined(HAVE_URING)
104 } else if (!strcmp("IO_URING", backend)) {
105 g_io_backend = OSMO_IO_BACKEND_IO_URING;
106 osmo_iofd_ops = iofd_uring_ops;
107#endif
Harald Welte8857f3b2022-11-18 13:54:44 +0100108 } else {
109 fprintf(stderr, "Invalid osmo_io backend requested: \"%s\"\nCheck the environment variable %s\n", backend, OSMO_IO_BACKEND_ENV);
110 exit(1);
111 }
112
Harald Welte257e7892024-03-07 10:39:05 +0100113 OSMO_ASSERT(osmo_iofd_ops.close);
Harald Welte8b7af442024-03-07 10:41:57 +0100114 OSMO_ASSERT(osmo_iofd_ops.register_fd);
115 OSMO_ASSERT(osmo_iofd_ops.unregister_fd);
Harald Welte257e7892024-03-07 10:39:05 +0100116 OSMO_ASSERT(osmo_iofd_ops.write_enable);
117 OSMO_ASSERT(osmo_iofd_ops.write_disable);
118 OSMO_ASSERT(osmo_iofd_ops.read_enable);
119 OSMO_ASSERT(osmo_iofd_ops.read_disable);
120 OSMO_ASSERT(osmo_iofd_ops.notify_connected);
121
Harald Welte8857f3b2022-11-18 13:54:44 +0100122 osmo_iofd_init();
123}
124
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200125/*! Allocate the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100126 * \param[in] iofd the osmo_io file structure
127 * \param[in] action the action this msg(hdr) is for (read, write, ..)
128 * \param[in] msg the msg buffer to use. Will allocate a new one if NULL
Harald Welte1047ed72023-11-18 18:51:58 +0100129 * \param[in] cmsg_size size (in bytes) of iofd_msghdr.cmsg buffer. Can be 0 if cmsg is not used.
Harald Welte8857f3b2022-11-18 13:54:44 +0100130 * \returns the newly allocated msghdr or NULL in case of error */
Harald Welte1047ed72023-11-18 18:51:58 +0100131struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg,
132 size_t cmsg_size)
Harald Welte8857f3b2022-11-18 13:54:44 +0100133{
Daniel Willmann012d9042023-08-10 10:47:25 +0200134 bool free_msg = false;
135 struct iofd_msghdr *hdr;
136
Harald Welte8857f3b2022-11-18 13:54:44 +0100137 if (!msg) {
138 msg = iofd_msgb_alloc(iofd);
Daniel Willmann012d9042023-08-10 10:47:25 +0200139 if (!msg)
Harald Welte8857f3b2022-11-18 13:54:44 +0100140 return NULL;
Daniel Willmann012d9042023-08-10 10:47:25 +0200141 free_msg = true;
Daniel Willmannf0833822023-07-27 18:00:32 +0200142 } else {
Daniel Willmann012d9042023-08-10 10:47:25 +0200143 talloc_steal(iofd, msg);
144 }
145
Harald Welte1047ed72023-11-18 18:51:58 +0100146 hdr = talloc_zero_size(iofd, sizeof(struct iofd_msghdr) + cmsg_size);
Daniel Willmann012d9042023-08-10 10:47:25 +0200147 if (!hdr) {
148 if (free_msg)
149 talloc_free(msg);
150 return NULL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100151 }
152
153 hdr->action = action;
154 hdr->iofd = iofd;
155 hdr->msg = msg;
156
157 return hdr;
158}
159
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200160/*! Free the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100161 * \param[in] msghdr the msghdr to free
162 */
163void iofd_msghdr_free(struct iofd_msghdr *msghdr)
164{
165 /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or
166 * or passed on to the read callback which takes ownership. */
167 talloc_free(msghdr);
168}
169
170/*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */
171struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd)
172{
173 uint16_t headroom = iofd->msgb_alloc.headroom;
174
175 OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom);
Andreas Eversberga4ac5b82024-02-28 16:36:29 +0100176 return msgb_alloc_headroom_c(iofd, iofd->msgb_alloc.size + headroom, headroom, "osmo_io_msgb");
Harald Welte8857f3b2022-11-18 13:54:44 +0100177}
178
179/*! return the pending msgb in iofd or NULL if there is none*/
180struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd)
181{
182 struct msgb *msg = NULL;
183
184 msg = iofd->pending;
185 iofd->pending = NULL;
186
187 return msg;
188}
189
190/*! Return the pending msgb or allocate and return a new one */
191struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
192{
193 struct msgb *msg = NULL;
194
195 msg = iofd_msgb_pending(iofd);
196 if (!msg)
197 msg = iofd_msgb_alloc(iofd);
198
199 return msg;
200}
201
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200202/*! Enqueue a message to be sent.
Harald Welte8857f3b2022-11-18 13:54:44 +0100203 *
204 * Enqueues the message at the back of the queue provided there is enough space.
205 * \param[in] iofd the file descriptor
206 * \param[in] msghdr the message to enqueue
207 * \returns 0 if the message was enqueued succcessfully,
208 * -ENOSPC if the queue already contains the maximum number of messages
209 */
210int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
211{
212 if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length)
213 return -ENOSPC;
214
215 llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue);
216 iofd->tx_queue.current_length++;
217
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200218 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100219 osmo_iofd_ops.write_enable(iofd);
220
221 return 0;
222}
223
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200224/*! Enqueue a message at the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100225 *
226 * Used to enqueue a msgb from a partial send again. This function will always
227 * enqueue the message, even if the maximum number of messages is reached.
228 * \param[in] iofd the file descriptor
229 * \param[in] msghdr the message to enqueue
230 */
231void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
232{
233 llist_add(&msghdr->list, &iofd->tx_queue.msg_queue);
234 iofd->tx_queue.current_length++;
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200235
236 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
237 osmo_iofd_ops.write_enable(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100238}
239
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200240/*! Dequeue a message from the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100241 *
242 * \param[in] iofd the file descriptor
243 * \returns the msghdr from the front of the queue or NULL if the queue is empty
244 */
245struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
246{
247 struct llist_head *lh;
248
249 if (iofd->tx_queue.current_length == 0)
250 return NULL;
251
252 lh = iofd->tx_queue.msg_queue.next;
253
254 OSMO_ASSERT(lh);
255 iofd->tx_queue.current_length--;
256 llist_del(lh);
257
258 if (iofd->tx_queue.current_length == 0)
259 osmo_iofd_ops.write_disable(iofd);
260
261 return llist_entry(lh, struct iofd_msghdr, list);
262}
263
264/*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain
265 * one complete message.
266 * If there are bytes left over, *pending_out will point to a msgb with the remaining data.
267*/
268static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out)
269{
arehbeinc0aa4bd2023-06-16 22:31:32 +0200270 int extra_len, received_len;
Harald Welte8857f3b2022-11-18 13:54:44 +0100271 struct msgb *msg_pending;
272
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200273 /* Save the start of message before segmentation_cb (which could change it) */
274 uint8_t *data = msg->data;
275
arehbeinc0aa4bd2023-06-16 22:31:32 +0200276 received_len = msgb_length(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100277
278 if (!iofd->io_ops.segmentation_cb) {
279 *pending_out = NULL;
280 return IOFD_SEG_ACT_HANDLE_ONE;
281 }
282
arehbeinc0aa4bd2023-06-16 22:31:32 +0200283 int expected_len = iofd->io_ops.segmentation_cb(msg);
284 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200285 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200286 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200287 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200288 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
289 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100290 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200291 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100292 return IOFD_SEG_ACT_DEFER;
293 }
294
arehbeinc0aa4bd2023-06-16 22:31:32 +0200295 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200296 /* No segmentation needed, return the whole msgb */
297 if (extra_len == 0) {
298 *pending_out = NULL;
299 return IOFD_SEG_ACT_HANDLE_ONE;
300 /* segment is incomplete */
301 } else if (extra_len < 0) {
302 goto defer;
303 }
304
305 /* msgb contains more than one segment */
306 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100307 msg_pending = iofd_msgb_alloc(iofd);
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200308 memcpy(msgb_data(msg_pending), data + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200309 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100310 *pending_out = msg_pending;
311
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200312 /* Trim the original msgb to size. Don't use msgb_trim because we need to reference
313 * msg->data from before it might have been modified by the segmentation_cb(). */
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200314 msg->tail = data + expected_len;
Daniel Willmann97d21442023-07-18 09:46:27 +0200315 msg->len = msg->tail - msg->data;
Harald Welte8857f3b2022-11-18 13:54:44 +0100316 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200317
318defer:
319 *pending_out = msg;
320 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100321}
322
323/*! Restore message boundaries on read() and pass individual messages to the read callback
324 */
325void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
326{
327 int res;
328 struct msgb *pending = NULL;
329
Harald Welteb365b1d2024-02-23 16:08:49 +0100330 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
331
Harald Welte8857f3b2022-11-18 13:54:44 +0100332 if (rc <= 0) {
333 iofd->io_ops.read_cb(iofd, rc, msg);
334 return;
335 }
336
337 do {
338 res = iofd_handle_segmentation(iofd, msg, &pending);
339 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
340 iofd->io_ops.read_cb(iofd, rc, msg);
341 if (res == IOFD_SEG_ACT_HANDLE_MORE)
342 msg = pending;
343 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
344
345 OSMO_ASSERT(iofd->pending == NULL);
346 iofd->pending = pending;
347}
348
Harald Weltee988ec32024-03-14 21:29:28 +0100349/*! completion handler: Internal function called by osmo_io_backend after a given I/O operation has completed
Harald Welte987a86a2023-11-18 18:46:24 +0100350 * \param[in] iofd I/O file-descriptor on which I/O has completed
351 * \param[in] msg message buffer containing data related to completed I/O
Andreas Eversberg76f76782024-02-14 14:33:10 +0100352 * \param[in] rc result code with read size or error (-errno)
Harald Welte987a86a2023-11-18 18:46:24 +0100353 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200354void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
355{
Daniel Willmann012d9042023-08-10 10:47:25 +0200356 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200357 switch (iofd->mode) {
358 case OSMO_IO_FD_MODE_READ_WRITE:
359 iofd_handle_segmented_read(iofd, msg, rc);
360 break;
361 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
362 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
363 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100364 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
365 iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr);
366 break;
367 default:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200368 OSMO_ASSERT(false);
369 break;
370 }
371}
372
Harald Weltee988ec32024-03-14 21:29:28 +0100373/*! completion handler: Internal function called by osmo_io_backend after a given I/O operation has completed
Daniel Willmann84611882023-11-21 10:17:00 +0100374 * \param[in] iofd I/O file-descriptor on which I/O has completed
375 * \param[in] rc return value of the I/O operation
376 * \param[in] msghdr serialized msghdr containing state of completed I/O
377 */
378void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
379{
380 struct msgb *msg = msghdr->msg;
381
382 /* Incomplete write */
383 if (rc > 0 && rc < msgb_length(msg)) {
384 /* Re-enqueue remaining data */
385 msgb_pull(msg, rc);
386 msghdr->iov[0].iov_len = msgb_length(msg);
387 iofd_txqueue_enqueue_front(iofd, msghdr);
388 return;
389 }
390
391 /* Reenqueue the complete msgb */
392 if (rc == -EAGAIN) {
393 iofd_txqueue_enqueue_front(iofd, msghdr);
394 return;
395 }
396
397 /* All other failure and success cases are handled here */
398 switch (msghdr->action) {
399 case IOFD_ACT_WRITE:
Harald Welte4ec4a382024-03-16 19:04:15 +0100400 if (iofd->io_ops.write_cb)
401 iofd->io_ops.write_cb(iofd, rc, msg);
Daniel Willmann84611882023-11-21 10:17:00 +0100402 break;
403 case IOFD_ACT_SENDTO:
Harald Welte4ec4a382024-03-16 19:04:15 +0100404 if (iofd->io_ops.sendto_cb)
405 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
Daniel Willmann84611882023-11-21 10:17:00 +0100406 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100407 case IOFD_ACT_SENDMSG:
Harald Welte4ec4a382024-03-16 19:04:15 +0100408 if (iofd->io_ops.sendmsg_cb)
409 iofd->io_ops.sendmsg_cb(iofd, rc, msg);
Harald Welte1047ed72023-11-18 18:51:58 +0100410 break;
Daniel Willmann84611882023-11-21 10:17:00 +0100411 default:
412 OSMO_ASSERT(0);
413 }
414
415 msgb_free(msghdr->msg);
416 iofd_msghdr_free(msghdr);
417}
418
Harald Welte8857f3b2022-11-18 13:54:44 +0100419/* Public functions */
420
Harald Weltee988ec32024-03-14 21:29:28 +0100421/*! Write a message to a file descriptor / connected socket.
422 * The osmo_io_fd must be using OSMO_IO_FD_MODE_READ_WRITE.
Harald Welte8857f3b2022-11-18 13:54:44 +0100423 *
Harald Weltee988ec32024-03-14 21:29:28 +0100424 * Appends the message to the internal transmit queue for eventual non-blocking
425 * write to the underlying socket/file descriptor.
426 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100427 * If the function returns success (0) it will take ownership of the msgb and
428 * internally call msgb_free() after the write request completes.
Harald Weltee988ec32024-03-14 21:29:28 +0100429 * In case of an error, the msgb needs to be freed by the caller.
430 *
431 * \param[in] iofd osmo_io_fd file descriptor to write data to
432 * \param[in] msg message buffer containing the data to write
Harald Welte8857f3b2022-11-18 13:54:44 +0100433 * \returns 0 in case of success; a negative value in case of error
434 */
435int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
436{
437 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200438
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100439 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
440 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
441 return -EINVAL;
442 }
443
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100444 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200445
Harald Welte1047ed72023-11-18 18:51:58 +0100446 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100447 if (!msghdr)
448 return -ENOMEM;
449
Daniel Willmann92efac22023-08-01 09:55:13 +0200450 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100451 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
452 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
453 msghdr->hdr.msg_iov = &msghdr->iov[0];
454 msghdr->hdr.msg_iovlen = 1;
455
456 rc = iofd_txqueue_enqueue(iofd, msghdr);
457 if (rc < 0) {
458 iofd_msghdr_free(msghdr);
459 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
460 return rc;
461 }
462
463 return 0;
464}
465
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200466/*! Send a message through an unconnected socket.
Harald Weltee988ec32024-03-14 21:29:28 +0100467 * The osmo_io_fd must be using OSMO_IO_FD_MODE_RECVFROM_SENDTO.
Harald Welte8857f3b2022-11-18 13:54:44 +0100468 *
Harald Weltee988ec32024-03-14 21:29:28 +0100469 * Appends the message to the internal transmit queue for eventual non-blocking
470 * sendto on the underlying socket/file descriptor.
471 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100472 * If the function returns success (0), it will take ownership of the msgb and
Harald Weltee988ec32024-03-14 21:29:28 +0100473 * internally call msgb_free() after the sendto request completes.
Harald Welte8857f3b2022-11-18 13:54:44 +0100474 * In case of an error the msgb needs to be freed by the caller.
Harald Weltee988ec32024-03-14 21:29:28 +0100475 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100476 * \param[in] iofd file descriptor to write to
477 * \param[in] msg message buffer to send
478 * \param[in] sendto_flags Flags to pass to the send call
479 * \param[in] dest destination address to send the message to
480 * \returns 0 in case of success; a negative value in case of error
481 */
482int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
483{
484 int rc;
485
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100486 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
487 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
488 return -EINVAL;
489 }
490
Harald Welte8857f3b2022-11-18 13:54:44 +0100491 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
492
Harald Welte1047ed72023-11-18 18:51:58 +0100493 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100494 if (!msghdr)
495 return -ENOMEM;
496
497 if (dest) {
498 msghdr->osa = *dest;
499 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
500 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
501 }
502 msghdr->flags = sendto_flags;
503 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
504 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
505 msghdr->hdr.msg_iov = &msghdr->iov[0];
506 msghdr->hdr.msg_iovlen = 1;
507
508 rc = iofd_txqueue_enqueue(iofd, msghdr);
509 if (rc < 0) {
510 iofd_msghdr_free(msghdr);
511 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
512 return rc;
513 }
514
515 return 0;
516}
517
Harald Weltee988ec32024-03-14 21:29:28 +0100518/*! osmo_io equivalent of the sendmsg(2) socket API call.
519 * The osmo_io_fd must be using OSMO_IO_FD_MODE_RECVMSG_SENDMSG.
Harald Welte1047ed72023-11-18 18:51:58 +0100520 *
Harald Weltee988ec32024-03-14 21:29:28 +0100521 * Appends the message to the internal transmit queue for eventual non-blocking
522 * sendmsg on the underlying socket/file descriptor.
523 *
Harald Welte1047ed72023-11-18 18:51:58 +0100524 * If the function returns success (0), it will take ownership of the msgb and
Harald Weltee988ec32024-03-14 21:29:28 +0100525 * internally call msgb_free() after the sendmsg request completes.
Harald Welte1047ed72023-11-18 18:51:58 +0100526 * In case of an error the msgb needs to be freed by the caller.
Harald Weltee988ec32024-03-14 21:29:28 +0100527 *
Harald Welte1047ed72023-11-18 18:51:58 +0100528 * \param[in] iofd file descriptor to write to
529 * \param[in] msg message buffer to send; is used to fill msgh->iov[]
530 * \param[in] sendmsg_flags Flags to pass to the send call
531 * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty!
532 * \returns 0 in case of success; a negative value in case of error
533 */
534int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
535{
536 int rc;
537 struct iofd_msghdr *msghdr;
538
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100539 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
540 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
541 return -EINVAL;
542 }
543
Harald Welte1047ed72023-11-18 18:51:58 +0100544 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Harald Welte1047ed72023-11-18 18:51:58 +0100545
546 if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) {
547 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n",
548 msgh->msg_namelen, sizeof(msghdr->osa));
549 return -EINVAL;
550 }
551
552 if (OSMO_UNLIKELY(msgh->msg_iovlen)) {
553 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n");
554 return -EINVAL;
555 }
556
557 msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen);
558 if (!msghdr)
559 return -ENOMEM;
560
561 /* copy over optional address */
562 if (msgh->msg_name) {
563 memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen);
564 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
565 msghdr->hdr.msg_namelen = msgh->msg_namelen;
566 }
567
568 /* build iov from msgb */
569 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
570 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
571 msghdr->hdr.msg_iov = &msghdr->iov[0];
572 msghdr->hdr.msg_iovlen = 1;
573
574 /* copy over the cmsg from the msghdr */
575 if (msgh->msg_control && msgh->msg_controllen) {
576 msghdr->hdr.msg_control = msghdr->cmsg;
577 msghdr->hdr.msg_controllen = msgh->msg_controllen;
578 memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen);
579 }
580
581 /* copy over msg_flags */
582 msghdr->hdr.msg_flags = sendmsg_flags;
583
584 rc = iofd_txqueue_enqueue(iofd, msghdr);
585 if (rc < 0) {
586 iofd_msghdr_free(msghdr);
587 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
588 return rc;
589 }
590
591 return 0;
592}
593
Harald Welteb365b1d2024-02-23 16:08:49 +0100594static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops)
595{
596 switch (mode) {
597 case OSMO_IO_FD_MODE_READ_WRITE:
598 if (ops->recvfrom_cb || ops->sendto_cb)
599 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100600 if (ops->recvmsg_cb || ops->sendmsg_cb)
601 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100602 break;
603 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
604 if (ops->read_cb || ops->write_cb)
605 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100606 if (ops->recvmsg_cb || ops->sendmsg_cb)
607 return false;
608 break;
609 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
610 if (ops->recvfrom_cb || ops->sendto_cb)
611 return false;
612 if (ops->read_cb || ops->write_cb)
613 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100614 break;
615 default:
616 break;
617 }
618
619 return true;
620}
621
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200622/*! Allocate and setup a new iofd.
Harald Weltee988ec32024-03-14 21:29:28 +0100623 *
624 * Use this to create a new osmo_io_fd, specifying the osmo_io_fd_mode and osmo_io_ops, as well as optionally
625 * the file-descriptor number and a human-readable name. This is the first function you call for any
626 * osmo_io_fd.
627 *
628 * The created osmo_io_fd is not yet registered, and hence can not be used for any I/O until a subsequent
629 * call to osmo_iofd_register().
630 *
631 * The created osmo_io_fd is initialized with some default settings:
632 * * msgb allocations size: OSMO_IO_DEFAULT_MSGB_SIZE (1024)
633 * * msgb headroom: OSMO_IO_DEFAULT_MSGB_HEADROOM (128)
634 * * tx_queue depth: 32
635 *
636 * Those values may be adjusted from their defaults by using osmo_iofd_set_alloc_info() and
637 * osmo_iofd_set_txqueue_max_length() on the osmo_io_fd.
638 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100639 * \param[in] ctx the parent context from which to allocate
Harald Weltee988ec32024-03-14 21:29:28 +0100640 * \param[in] fd the underlying system file descriptor. May be -1 if not known yet; must then be specified
641 * at subsequent osmo_iofd_register() time.
642 * \param[in] name the optional human-readable name of the iofd; may be NULL
643 * \param[in] mode the osmo_io_fd_mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
644 * semantics.
645 * \param[in] ioops structure specifying the read/write/send/recv callbacks. Will be copied to the iofd, so
646 * the caller does not have to keep it around after issuing the osmo_iofd_setup call.
647 * \param[in] data opaque user data pointer accessible by the ioops callbacks
Harald Welte8857f3b2022-11-18 13:54:44 +0100648 * \returns The newly allocated osmo_io_fd struct or NULL on failure
649 */
650struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
651 const struct osmo_io_ops *ioops, void *data)
652{
Harald Weltec380f292023-11-18 19:54:46 +0100653 struct osmo_io_fd *iofd;
654
655 /* reject unsupported/unknown modes */
656 switch (mode) {
657 case OSMO_IO_FD_MODE_READ_WRITE:
658 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
Harald Welte1047ed72023-11-18 18:51:58 +0100659 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
Harald Weltec380f292023-11-18 19:54:46 +0100660 break;
661 default:
662 return NULL;
663 }
664
Harald Welte09ab0412024-03-07 10:11:52 +0100665 if (ioops && !check_mode_callback_compat(mode, ioops)) {
666 LOGP(DLIO, LOGL_ERROR, "iofd(%s): rejecting call-backs incompatible with mode %s\n",
667 name ? name : "unknown", osmo_iofd_mode_name(mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100668 return NULL;
Harald Welte09ab0412024-03-07 10:11:52 +0100669 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100670
Harald Weltec380f292023-11-18 19:54:46 +0100671 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100672 if (!iofd)
673 return NULL;
674
675 iofd->fd = fd;
676 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200677 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100678
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200679 if (name)
680 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100681
682 if (ioops)
683 iofd->io_ops = *ioops;
684
685 iofd->pending = NULL;
686
687 iofd->data = data;
688
689 iofd->msgb_alloc.ctx = ctx;
690 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
691 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
692
693 iofd->tx_queue.max_length = 32;
694 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
695
696 return iofd;
697}
698
Harald Weltee988ec32024-03-14 21:29:28 +0100699/*! Set the size of the control message buffer allocated when submitting recvmsg.
700 *
701 * If your osmo_io_fd is in OSMO_IO_FD_MODE_RECVMSG_SENDMSG mode, this API function can be used to tell the
702 * osmo_io code how much memory should be allocated for the cmsg (control message) buffer when performing
703 * recvmsg(). */
Harald Welte1047ed72023-11-18 18:51:58 +0100704int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
705{
706 if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG)
707 return -EINVAL;
708
709 iofd->cmsg_size = cmsg_size;
710 return 0;
711}
712
Harald Weltee988ec32024-03-14 21:29:28 +0100713/*! Register the osmo_io_fd for active I/O.
714 *
715 * Calling this function will register a previously initialized osmo_io_fd for performing I/O.
716 *
717 * If the osmo_iofd has a read_cb/recvfrom_cb_recvmsg_cb set in its osmo_io_ops, read/receive will be
718 * automatically enabled and the respective call-back is called at any time data becomes available.
719 *
720 * If there is to-be-transmitted data in the transmit queue, write will be automatically enabled, allowing
721 * the transmit queue to be drained as soon as the fd/socket becomes writable.
Harald Welte8857f3b2022-11-18 13:54:44 +0100722 *
723 * \param[in] iofd the iofd file descriptor
Harald Weltee988ec32024-03-14 21:29:28 +0100724 * \param[in] fd the system fd number that will be registered. If you did not yet specify the file descriptor
725 * number during osmo_fd_setup(), or if it has changed since then, you can state the [new] file descriptor
726 * number as argument. If you wish to proceed with the previously specified file descriptor number, pass -1.
Harald Welte8857f3b2022-11-18 13:54:44 +0100727 * \returns zero on success, a negative value on error
728*/
729int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
730{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200731 int rc = 0;
732
Harald Welte8857f3b2022-11-18 13:54:44 +0100733 if (fd >= 0)
734 iofd->fd = fd;
Harald Welte04e6f6e2024-03-07 09:57:22 +0100735 else if (iofd->fd < 0) {
736 /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */
737 LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd);
738 return -EBADF;
739 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100740
Harald Welte8b7af442024-03-07 10:41:57 +0100741 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200742 if (rc)
743 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100744
Daniel Willmannf89162f2023-06-26 19:24:46 +0200745 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Harald Welteb365b1d2024-02-23 16:08:49 +0100746 if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) ||
Harald Welte1047ed72023-11-18 18:51:58 +0100747 (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) ||
748 (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) {
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200749 osmo_iofd_ops.read_enable(iofd);
Harald Welteb365b1d2024-02-23 16:08:49 +0100750 }
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200751
752 if (iofd->tx_queue.current_length > 0)
753 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200754
755 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100756}
757
Harald Weltee988ec32024-03-14 21:29:28 +0100758/*! Unregister the given osmo_io_fd from osmo_io.
759 *
760 * After an osmo_io_fd has been successfully unregistered, it can no longer perform any I/O via osmo_io.
761 * However, it can be subsequently re-registered using osmo_iofd_register().
Harald Welte8857f3b2022-11-18 13:54:44 +0100762 *
763 * \param[in] iofd the file descriptor
764 * \returns zero on success, a negative value on error
765 */
766int osmo_iofd_unregister(struct osmo_io_fd *iofd)
767{
Harald Welte8b7af442024-03-07 10:41:57 +0100768 return osmo_iofd_ops.unregister_fd(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100769}
770
Harald Weltee988ec32024-03-14 21:29:28 +0100771/*! Retrieve the number of messages pending in the transmit queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100772 *
773 * \param[in] iofd the file descriptor
774 */
775unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
776{
777 return iofd->tx_queue.current_length;
778}
779
Harald Weltee988ec32024-03-14 21:29:28 +0100780/*! Clear the transmit queue of the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100781 *
782 * This function frees all messages currently pending in the transmit queue
783 * \param[in] iofd the file descriptor
784 */
785void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
786{
787 struct iofd_msghdr *hdr;
788 while ((hdr = iofd_txqueue_dequeue(iofd))) {
789 msgb_free(hdr->msg);
790 iofd_msghdr_free(hdr);
791 }
792}
793
Harald Weltee988ec32024-03-14 21:29:28 +0100794/*! Free the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100795 *
Harald Weltee988ec32024-03-14 21:29:28 +0100796 * The iofd will be automatically closed before via osmo_iofd_close() [which in turn will unregister
797 * it and clear any pending transmit queue items]. You must not reference the iofd
798 * after calling this function. However, it is safe to call this function from any of osmo_io
799 * call-backs; in this case, actual free will be internally delayed until that call-back completes.
800 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100801 * \param[in] iofd the file descriptor
802 */
803void osmo_iofd_free(struct osmo_io_fd *iofd)
804{
805 if (!iofd)
806 return;
807
808 osmo_iofd_close(iofd);
809
Daniel Willmannf89162f2023-06-26 19:24:46 +0200810 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100811 talloc_free(iofd);
812 } else {
813 /* Prevent our parent context from freeing us prematurely */
814 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200815 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100816 }
817}
818
Harald Weltee988ec32024-03-14 21:29:28 +0100819/*! Close the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100820 *
Harald Weltee988ec32024-03-14 21:29:28 +0100821 * This function closes the underlying fd, unregisters it from osmo_io and clears any messages in the tx
822 * queue. The iofd itself is not freed and can be assigned a new file descriptor with osmo_iofd_register()
Harald Welte8857f3b2022-11-18 13:54:44 +0100823 * \param[in] iofd the file descriptor
Harald Weltee988ec32024-03-14 21:29:28 +0100824 * \returns 0 on success, a negative value otherwise
Harald Welte8857f3b2022-11-18 13:54:44 +0100825 */
826int osmo_iofd_close(struct osmo_io_fd *iofd)
827{
828 int rc = 0;
829
Daniel Willmannf89162f2023-06-26 19:24:46 +0200830 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100831 return rc;
832
Daniel Willmannf89162f2023-06-26 19:24:46 +0200833 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100834
835 /* Free pending msgs in tx queue */
836 osmo_iofd_txqueue_clear(iofd);
837 msgb_free(iofd->pending);
838
839 iofd->pending = NULL;
840
Harald Weltee1d48582024-03-07 10:34:21 +0100841 rc = osmo_iofd_ops.close(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100842 iofd->fd = -1;
843 return rc;
844}
845
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200846/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200847 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100848 * \param[in] size the size of the msgb when receiving data
849 * \param[in] headroom the headroom of the msgb when receiving data
850 */
851void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
852{
853 iofd->msgb_alloc.headroom = headroom;
854 iofd->msgb_alloc.size = size;
855}
856
Daniel Willmanna9303f32023-07-07 11:20:48 +0200857/*! Set the maximum number of messages enqueued for sending.
858 * \param[in] iofd the file descriptor
859 * \param[in] size the maximum size of the transmit queue
860 */
861void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
862{
863 iofd->tx_queue.max_length = max_length;
864}
865
Harald Weltee988ec32024-03-14 21:29:28 +0100866/*! Retrieve the associated user-data from an osmo_io_fd.
867 *
868 * A call to this function will return the opaque user data pointer which was specified previously
869 * via osmo_iofd_setup() or via osmo_iofd_set_data().
870 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100871 * \param[in] iofd the file descriptor
872 * \returns the data that was previously set with \ref osmo_iofd_setup()
873 */
874void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
875{
876 return iofd->data;
877}
878
Harald Weltee988ec32024-03-14 21:29:28 +0100879/*! Set the associated user-data from an osmo_io_fd.
880 *
881 * Calling this function will set/overwrite the opaque user data pointer, which can later be retrieved using
882 * osmo_iofd_get_data().
883 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100884 * \param[in] iofd the file descriptor
885 * \param[in] data the data to set
886 */
887void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
888{
889 iofd->data = data;
890}
891
Harald Weltee988ec32024-03-14 21:29:28 +0100892/*! Retrieve the private number from an osmo_io_fd.
893 * Calling this function will retrieve the private user number previously set via osmo_iofd_set_priv_nr().
Harald Welte8857f3b2022-11-18 13:54:44 +0100894 * \param[in] iofd the file descriptor
895 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
896 */
897unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
898{
899 return iofd->priv_nr;
900}
901
Harald Weltee988ec32024-03-14 21:29:28 +0100902/*! Set the private number of an osmo_io_fd.
903 * The priv_nr passed in via this call can later be retrieved via osmo_iofd_get_priv_nr(). It provides
904 * a way how additional context can be stored in the osmo_io_fd beyond the opaque 'data' pointer.
Harald Welte8857f3b2022-11-18 13:54:44 +0100905 * \param[in] iofd the file descriptor
906 * \param[in] priv_nr the private number to set
907 */
908void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
909{
910 iofd->priv_nr = priv_nr;
911}
912
Harald Weltee988ec32024-03-14 21:29:28 +0100913/*! Retrieve the underlying file descriptor from an osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100914 * \param[in] iofd the file descriptor
915 * \returns the underlying file descriptor number */
916int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
917{
918 return iofd->fd;
919}
920
Harald Weltee988ec32024-03-14 21:29:28 +0100921/*! Retrieve the human-readable name of the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100922 * \param[in] iofd the file descriptor
923 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
924const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
925{
926 return iofd->name;
927}
928
Harald Weltee988ec32024-03-14 21:29:28 +0100929/*! Set the human-readable name of the file descriptor.
930 * The given name will be used as context by all related logging and future calls to osmo_iofd_get_name().
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200931 * \param[in] iofd the file descriptor
932 * \param[in] name the name to set on the file descriptor */
933void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
934{
935 osmo_talloc_replace_string(iofd, &iofd->name, name);
936}
937
Harald Weltee988ec32024-03-14 21:29:28 +0100938/*! Set the osmo_io_ops calbacks for an osmo_io_fd.
939 * This function can be used to update/overwrite the call-back functions for the given osmo_io_fd; it
940 * replaces the currently-set call-back function pointers from a previous call to osmo_iofd_set_ioops()
941 * or the original osmo_iofd_setup().
arehbein0c374c62023-05-14 21:43:11 +0200942 * \param[in] iofd Target iofd file descriptor
Harald Weltee988ec32024-03-14 21:29:28 +0100943 * \param[in] ioops osmo_io_ops structure to be copied to the osmo_io_fd.
944 * \returns 0 on success, negative on error */
Harald Welteb365b1d2024-02-23 16:08:49 +0100945int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
arehbein0c374c62023-05-14 21:43:11 +0200946{
Harald Welte09ab0412024-03-07 10:11:52 +0100947 if (!check_mode_callback_compat(iofd->mode, ioops)) {
948 LOGPIO(iofd, LOGL_ERROR, "rejecting call-backs incompatible with mode %s\n",
949 osmo_iofd_mode_name(iofd->mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100950 return -EINVAL;
Harald Welte09ab0412024-03-07 10:11:52 +0100951 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100952
arehbein0c374c62023-05-14 21:43:11 +0200953 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200954
955 switch (iofd->mode) {
956 case OSMO_IO_FD_MODE_READ_WRITE:
957 if (iofd->io_ops.read_cb)
958 osmo_iofd_ops.read_enable(iofd);
959 else
960 osmo_iofd_ops.read_disable(iofd);
961 break;
962 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
963 if (iofd->io_ops.recvfrom_cb)
964 osmo_iofd_ops.read_enable(iofd);
965 else
966 osmo_iofd_ops.read_disable(iofd);
967 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100968 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
969 if (iofd->io_ops.recvmsg_cb)
970 osmo_iofd_ops.read_enable(iofd);
971 else
972 osmo_iofd_ops.read_disable(iofd);
973 break;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200974 default:
975 OSMO_ASSERT(0);
976 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100977
978 return 0;
arehbein0c374c62023-05-14 21:43:11 +0200979}
980
Harald Weltee988ec32024-03-14 21:29:28 +0100981/*! Retrieve the osmo_io_ops for an iofd.
Harald Weltef574aea2024-02-23 12:07:03 +0100982 * \param[in] iofd Target iofd file descriptor
983 * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */
984void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
985{
986 *ioops = iofd->io_ops;
987}
988
Harald Weltee988ec32024-03-14 21:29:28 +0100989/*! Request notification of the user if/when a client socket is connected.
990 * Calling this function will request osmo_io to notify the user (via
991 * write call-back) once a non-blocking outbound connect() of the
992 * socket completes.
993 *
994 * This only works for connection oriented sockets in either
995 * OSMO_IO_FD_MODE_READ_WRITE or OSMO_IO_FD_MODE_RECVMSG_SENDMSG mode.
996 *
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200997 * \param[in] iofd the file descriptor */
998void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
999{
Harald Welte1047ed72023-11-18 18:51:58 +01001000 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE ||
1001 iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Andreas Eversberg848faf92024-02-09 12:38:17 +01001002 osmo_iofd_ops.notify_connected(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +02001003}
1004
Harald Weltee988ec32024-03-14 21:29:28 +01001005/*! @} */
Daniel Willmanne2a8dc42023-06-30 10:51:53 +02001006
Harald Weltefab993b2024-03-14 21:27:40 +01001007#endif /* ifndef(EMBEDDED) */