blob: af096e6ea01fb27be52fd6f3916a024bc3593b53 [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{
Pau Espin Pedrol99750d52024-04-17 14:11:43 +0200270 int extra_len, received_len, expected_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
Pau Espin Pedrol99750d52024-04-17 14:11:43 +0200278 if (iofd->io_ops.segmentation_cb2) {
279 expected_len = iofd->io_ops.segmentation_cb2(iofd, msg);
280 } else if (iofd->io_ops.segmentation_cb) {
281 expected_len = iofd->io_ops.segmentation_cb(msg);
282 } else {
Harald Welte8857f3b2022-11-18 13:54:44 +0100283 *pending_out = NULL;
284 return IOFD_SEG_ACT_HANDLE_ONE;
285 }
286
arehbeinc0aa4bd2023-06-16 22:31:32 +0200287 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200288 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200289 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200290 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200291 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
292 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100293 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200294 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100295 return IOFD_SEG_ACT_DEFER;
296 }
297
arehbeinc0aa4bd2023-06-16 22:31:32 +0200298 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200299 /* No segmentation needed, return the whole msgb */
300 if (extra_len == 0) {
301 *pending_out = NULL;
302 return IOFD_SEG_ACT_HANDLE_ONE;
303 /* segment is incomplete */
304 } else if (extra_len < 0) {
305 goto defer;
306 }
307
308 /* msgb contains more than one segment */
309 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100310 msg_pending = iofd_msgb_alloc(iofd);
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200311 memcpy(msgb_data(msg_pending), data + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200312 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100313 *pending_out = msg_pending;
314
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200315 /* Trim the original msgb to size. Don't use msgb_trim because we need to reference
316 * msg->data from before it might have been modified by the segmentation_cb(). */
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200317 msg->tail = data + expected_len;
Daniel Willmann97d21442023-07-18 09:46:27 +0200318 msg->len = msg->tail - msg->data;
Harald Welte8857f3b2022-11-18 13:54:44 +0100319 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200320
321defer:
322 *pending_out = msg;
323 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100324}
325
326/*! Restore message boundaries on read() and pass individual messages to the read callback
327 */
328void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
329{
330 int res;
331 struct msgb *pending = NULL;
332
Harald Welteb365b1d2024-02-23 16:08:49 +0100333 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
334
Harald Welte8857f3b2022-11-18 13:54:44 +0100335 if (rc <= 0) {
336 iofd->io_ops.read_cb(iofd, rc, msg);
337 return;
338 }
339
340 do {
341 res = iofd_handle_segmentation(iofd, msg, &pending);
342 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
343 iofd->io_ops.read_cb(iofd, rc, msg);
344 if (res == IOFD_SEG_ACT_HANDLE_MORE)
345 msg = pending;
346 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
347
348 OSMO_ASSERT(iofd->pending == NULL);
349 iofd->pending = pending;
350}
351
Harald Weltee988ec32024-03-14 21:29:28 +0100352/*! completion handler: Internal function called by osmo_io_backend after a given I/O operation has completed
Harald Welte987a86a2023-11-18 18:46:24 +0100353 * \param[in] iofd I/O file-descriptor on which I/O has completed
354 * \param[in] msg message buffer containing data related to completed I/O
Andreas Eversberg76f76782024-02-14 14:33:10 +0100355 * \param[in] rc result code with read size or error (-errno)
Harald Welte987a86a2023-11-18 18:46:24 +0100356 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200357void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
358{
Daniel Willmann012d9042023-08-10 10:47:25 +0200359 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200360 switch (iofd->mode) {
361 case OSMO_IO_FD_MODE_READ_WRITE:
362 iofd_handle_segmented_read(iofd, msg, rc);
363 break;
364 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
365 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
366 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100367 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
368 iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr);
369 break;
370 default:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200371 OSMO_ASSERT(false);
372 break;
373 }
374}
375
Harald Weltee988ec32024-03-14 21:29:28 +0100376/*! completion handler: Internal function called by osmo_io_backend after a given I/O operation has completed
Daniel Willmann84611882023-11-21 10:17:00 +0100377 * \param[in] iofd I/O file-descriptor on which I/O has completed
378 * \param[in] rc return value of the I/O operation
379 * \param[in] msghdr serialized msghdr containing state of completed I/O
380 */
381void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
382{
383 struct msgb *msg = msghdr->msg;
384
385 /* Incomplete write */
386 if (rc > 0 && rc < msgb_length(msg)) {
387 /* Re-enqueue remaining data */
388 msgb_pull(msg, rc);
389 msghdr->iov[0].iov_len = msgb_length(msg);
390 iofd_txqueue_enqueue_front(iofd, msghdr);
391 return;
392 }
393
394 /* Reenqueue the complete msgb */
395 if (rc == -EAGAIN) {
396 iofd_txqueue_enqueue_front(iofd, msghdr);
397 return;
398 }
399
400 /* All other failure and success cases are handled here */
401 switch (msghdr->action) {
402 case IOFD_ACT_WRITE:
Harald Welte4ec4a382024-03-16 19:04:15 +0100403 if (iofd->io_ops.write_cb)
404 iofd->io_ops.write_cb(iofd, rc, msg);
Daniel Willmann84611882023-11-21 10:17:00 +0100405 break;
406 case IOFD_ACT_SENDTO:
Harald Welte4ec4a382024-03-16 19:04:15 +0100407 if (iofd->io_ops.sendto_cb)
408 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
Daniel Willmann84611882023-11-21 10:17:00 +0100409 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100410 case IOFD_ACT_SENDMSG:
Harald Welte4ec4a382024-03-16 19:04:15 +0100411 if (iofd->io_ops.sendmsg_cb)
412 iofd->io_ops.sendmsg_cb(iofd, rc, msg);
Harald Welte1047ed72023-11-18 18:51:58 +0100413 break;
Daniel Willmann84611882023-11-21 10:17:00 +0100414 default:
415 OSMO_ASSERT(0);
416 }
417
418 msgb_free(msghdr->msg);
419 iofd_msghdr_free(msghdr);
420}
421
Harald Welte8857f3b2022-11-18 13:54:44 +0100422/* Public functions */
423
Harald Weltee988ec32024-03-14 21:29:28 +0100424/*! Write a message to a file descriptor / connected socket.
425 * The osmo_io_fd must be using OSMO_IO_FD_MODE_READ_WRITE.
Harald Welte8857f3b2022-11-18 13:54:44 +0100426 *
Harald Weltee988ec32024-03-14 21:29:28 +0100427 * Appends the message to the internal transmit queue for eventual non-blocking
428 * write to the underlying socket/file descriptor.
429 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100430 * If the function returns success (0) it will take ownership of the msgb and
431 * internally call msgb_free() after the write request completes.
Harald Weltee988ec32024-03-14 21:29:28 +0100432 * In case of an error, the msgb needs to be freed by the caller.
433 *
434 * \param[in] iofd osmo_io_fd file descriptor to write data to
435 * \param[in] msg message buffer containing the data to write
Harald Welte8857f3b2022-11-18 13:54:44 +0100436 * \returns 0 in case of success; a negative value in case of error
437 */
438int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
439{
440 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200441
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100442 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
443 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
444 return -EINVAL;
445 }
446
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100447 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200448
Harald Welte1047ed72023-11-18 18:51:58 +0100449 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100450 if (!msghdr)
451 return -ENOMEM;
452
Daniel Willmann92efac22023-08-01 09:55:13 +0200453 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100454 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
455 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
456 msghdr->hdr.msg_iov = &msghdr->iov[0];
457 msghdr->hdr.msg_iovlen = 1;
458
459 rc = iofd_txqueue_enqueue(iofd, msghdr);
460 if (rc < 0) {
461 iofd_msghdr_free(msghdr);
462 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
463 return rc;
464 }
465
466 return 0;
467}
468
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200469/*! Send a message through an unconnected socket.
Harald Weltee988ec32024-03-14 21:29:28 +0100470 * The osmo_io_fd must be using OSMO_IO_FD_MODE_RECVFROM_SENDTO.
Harald Welte8857f3b2022-11-18 13:54:44 +0100471 *
Harald Weltee988ec32024-03-14 21:29:28 +0100472 * Appends the message to the internal transmit queue for eventual non-blocking
473 * sendto on the underlying socket/file descriptor.
474 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100475 * If the function returns success (0), it will take ownership of the msgb and
Harald Weltee988ec32024-03-14 21:29:28 +0100476 * internally call msgb_free() after the sendto request completes.
Harald Welte8857f3b2022-11-18 13:54:44 +0100477 * In case of an error the msgb needs to be freed by the caller.
Harald Weltee988ec32024-03-14 21:29:28 +0100478 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100479 * \param[in] iofd file descriptor to write to
480 * \param[in] msg message buffer to send
481 * \param[in] sendto_flags Flags to pass to the send call
482 * \param[in] dest destination address to send the message to
483 * \returns 0 in case of success; a negative value in case of error
484 */
485int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
486{
487 int rc;
488
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100489 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
490 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
491 return -EINVAL;
492 }
493
Harald Welte8857f3b2022-11-18 13:54:44 +0100494 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
495
Harald Welte1047ed72023-11-18 18:51:58 +0100496 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100497 if (!msghdr)
498 return -ENOMEM;
499
500 if (dest) {
501 msghdr->osa = *dest;
502 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
503 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
504 }
505 msghdr->flags = sendto_flags;
506 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
507 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
508 msghdr->hdr.msg_iov = &msghdr->iov[0];
509 msghdr->hdr.msg_iovlen = 1;
510
511 rc = iofd_txqueue_enqueue(iofd, msghdr);
512 if (rc < 0) {
513 iofd_msghdr_free(msghdr);
514 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
515 return rc;
516 }
517
518 return 0;
519}
520
Harald Weltee988ec32024-03-14 21:29:28 +0100521/*! osmo_io equivalent of the sendmsg(2) socket API call.
522 * The osmo_io_fd must be using OSMO_IO_FD_MODE_RECVMSG_SENDMSG.
Harald Welte1047ed72023-11-18 18:51:58 +0100523 *
Harald Weltee988ec32024-03-14 21:29:28 +0100524 * Appends the message to the internal transmit queue for eventual non-blocking
525 * sendmsg on the underlying socket/file descriptor.
526 *
Harald Welte1047ed72023-11-18 18:51:58 +0100527 * If the function returns success (0), it will take ownership of the msgb and
Harald Weltee988ec32024-03-14 21:29:28 +0100528 * internally call msgb_free() after the sendmsg request completes.
Harald Welte1047ed72023-11-18 18:51:58 +0100529 * In case of an error the msgb needs to be freed by the caller.
Harald Weltee988ec32024-03-14 21:29:28 +0100530 *
Harald Welte1047ed72023-11-18 18:51:58 +0100531 * \param[in] iofd file descriptor to write to
532 * \param[in] msg message buffer to send; is used to fill msgh->iov[]
533 * \param[in] sendmsg_flags Flags to pass to the send call
534 * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty!
535 * \returns 0 in case of success; a negative value in case of error
536 */
537int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
538{
539 int rc;
540 struct iofd_msghdr *msghdr;
541
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100542 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
543 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
544 return -EINVAL;
545 }
546
Harald Welte1047ed72023-11-18 18:51:58 +0100547 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Harald Welte1047ed72023-11-18 18:51:58 +0100548
549 if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) {
550 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n",
551 msgh->msg_namelen, sizeof(msghdr->osa));
552 return -EINVAL;
553 }
554
555 if (OSMO_UNLIKELY(msgh->msg_iovlen)) {
556 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n");
557 return -EINVAL;
558 }
559
560 msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen);
561 if (!msghdr)
562 return -ENOMEM;
563
564 /* copy over optional address */
565 if (msgh->msg_name) {
566 memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen);
567 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
568 msghdr->hdr.msg_namelen = msgh->msg_namelen;
569 }
570
571 /* build iov from msgb */
572 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
573 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
574 msghdr->hdr.msg_iov = &msghdr->iov[0];
575 msghdr->hdr.msg_iovlen = 1;
576
577 /* copy over the cmsg from the msghdr */
578 if (msgh->msg_control && msgh->msg_controllen) {
579 msghdr->hdr.msg_control = msghdr->cmsg;
580 msghdr->hdr.msg_controllen = msgh->msg_controllen;
581 memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen);
582 }
583
584 /* copy over msg_flags */
585 msghdr->hdr.msg_flags = sendmsg_flags;
586
587 rc = iofd_txqueue_enqueue(iofd, msghdr);
588 if (rc < 0) {
589 iofd_msghdr_free(msghdr);
590 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
591 return rc;
592 }
593
594 return 0;
595}
596
Harald Welteb365b1d2024-02-23 16:08:49 +0100597static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops)
598{
599 switch (mode) {
600 case OSMO_IO_FD_MODE_READ_WRITE:
601 if (ops->recvfrom_cb || ops->sendto_cb)
602 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100603 if (ops->recvmsg_cb || ops->sendmsg_cb)
604 return false;
Pau Espin Pedrol99750d52024-04-17 14:11:43 +0200605 /* Forbid both segementation_cb set, something is wrong: */
606 if (ops->segmentation_cb && ops->segmentation_cb2)
607 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100608 break;
609 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
610 if (ops->read_cb || ops->write_cb)
611 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100612 if (ops->recvmsg_cb || ops->sendmsg_cb)
613 return false;
614 break;
615 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
616 if (ops->recvfrom_cb || ops->sendto_cb)
617 return false;
618 if (ops->read_cb || ops->write_cb)
619 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100620 break;
621 default:
622 break;
623 }
624
625 return true;
626}
627
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200628/*! Allocate and setup a new iofd.
Harald Weltee988ec32024-03-14 21:29:28 +0100629 *
630 * Use this to create a new osmo_io_fd, specifying the osmo_io_fd_mode and osmo_io_ops, as well as optionally
631 * the file-descriptor number and a human-readable name. This is the first function you call for any
632 * osmo_io_fd.
633 *
634 * The created osmo_io_fd is not yet registered, and hence can not be used for any I/O until a subsequent
635 * call to osmo_iofd_register().
636 *
637 * The created osmo_io_fd is initialized with some default settings:
638 * * msgb allocations size: OSMO_IO_DEFAULT_MSGB_SIZE (1024)
639 * * msgb headroom: OSMO_IO_DEFAULT_MSGB_HEADROOM (128)
640 * * tx_queue depth: 32
641 *
642 * Those values may be adjusted from their defaults by using osmo_iofd_set_alloc_info() and
643 * osmo_iofd_set_txqueue_max_length() on the osmo_io_fd.
644 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100645 * \param[in] ctx the parent context from which to allocate
Harald Weltee988ec32024-03-14 21:29:28 +0100646 * \param[in] fd the underlying system file descriptor. May be -1 if not known yet; must then be specified
647 * at subsequent osmo_iofd_register() time.
648 * \param[in] name the optional human-readable name of the iofd; may be NULL
649 * \param[in] mode the osmo_io_fd_mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
650 * semantics.
651 * \param[in] ioops structure specifying the read/write/send/recv callbacks. Will be copied to the iofd, so
652 * the caller does not have to keep it around after issuing the osmo_iofd_setup call.
653 * \param[in] data opaque user data pointer accessible by the ioops callbacks
Harald Welte8857f3b2022-11-18 13:54:44 +0100654 * \returns The newly allocated osmo_io_fd struct or NULL on failure
655 */
656struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
657 const struct osmo_io_ops *ioops, void *data)
658{
Harald Weltec380f292023-11-18 19:54:46 +0100659 struct osmo_io_fd *iofd;
660
661 /* reject unsupported/unknown modes */
662 switch (mode) {
663 case OSMO_IO_FD_MODE_READ_WRITE:
664 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
Harald Welte1047ed72023-11-18 18:51:58 +0100665 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
Harald Weltec380f292023-11-18 19:54:46 +0100666 break;
667 default:
668 return NULL;
669 }
670
Harald Welte09ab0412024-03-07 10:11:52 +0100671 if (ioops && !check_mode_callback_compat(mode, ioops)) {
672 LOGP(DLIO, LOGL_ERROR, "iofd(%s): rejecting call-backs incompatible with mode %s\n",
673 name ? name : "unknown", osmo_iofd_mode_name(mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100674 return NULL;
Harald Welte09ab0412024-03-07 10:11:52 +0100675 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100676
Harald Weltec380f292023-11-18 19:54:46 +0100677 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100678 if (!iofd)
679 return NULL;
680
681 iofd->fd = fd;
682 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200683 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100684
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200685 if (name)
686 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100687
688 if (ioops)
689 iofd->io_ops = *ioops;
690
691 iofd->pending = NULL;
692
693 iofd->data = data;
694
695 iofd->msgb_alloc.ctx = ctx;
696 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
697 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
698
699 iofd->tx_queue.max_length = 32;
700 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
701
702 return iofd;
703}
704
Harald Weltee988ec32024-03-14 21:29:28 +0100705/*! Set the size of the control message buffer allocated when submitting recvmsg.
706 *
707 * If your osmo_io_fd is in OSMO_IO_FD_MODE_RECVMSG_SENDMSG mode, this API function can be used to tell the
708 * osmo_io code how much memory should be allocated for the cmsg (control message) buffer when performing
709 * recvmsg(). */
Harald Welte1047ed72023-11-18 18:51:58 +0100710int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
711{
712 if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG)
713 return -EINVAL;
714
715 iofd->cmsg_size = cmsg_size;
716 return 0;
717}
718
Harald Weltee988ec32024-03-14 21:29:28 +0100719/*! Register the osmo_io_fd for active I/O.
720 *
721 * Calling this function will register a previously initialized osmo_io_fd for performing I/O.
722 *
723 * If the osmo_iofd has a read_cb/recvfrom_cb_recvmsg_cb set in its osmo_io_ops, read/receive will be
724 * automatically enabled and the respective call-back is called at any time data becomes available.
725 *
726 * If there is to-be-transmitted data in the transmit queue, write will be automatically enabled, allowing
727 * the transmit queue to be drained as soon as the fd/socket becomes writable.
Harald Welte8857f3b2022-11-18 13:54:44 +0100728 *
729 * \param[in] iofd the iofd file descriptor
Harald Weltee988ec32024-03-14 21:29:28 +0100730 * \param[in] fd the system fd number that will be registered. If you did not yet specify the file descriptor
731 * number during osmo_fd_setup(), or if it has changed since then, you can state the [new] file descriptor
732 * number as argument. If you wish to proceed with the previously specified file descriptor number, pass -1.
Harald Welte8857f3b2022-11-18 13:54:44 +0100733 * \returns zero on success, a negative value on error
734*/
735int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
736{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200737 int rc = 0;
738
Harald Welte8857f3b2022-11-18 13:54:44 +0100739 if (fd >= 0)
740 iofd->fd = fd;
Harald Welte04e6f6e2024-03-07 09:57:22 +0100741 else if (iofd->fd < 0) {
742 /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */
743 LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd);
744 return -EBADF;
745 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100746
Harald Welte8b7af442024-03-07 10:41:57 +0100747 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200748 if (rc)
749 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100750
Daniel Willmannf89162f2023-06-26 19:24:46 +0200751 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Harald Welteb365b1d2024-02-23 16:08:49 +0100752 if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) ||
Harald Welte1047ed72023-11-18 18:51:58 +0100753 (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) ||
754 (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) {
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200755 osmo_iofd_ops.read_enable(iofd);
Harald Welteb365b1d2024-02-23 16:08:49 +0100756 }
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200757
758 if (iofd->tx_queue.current_length > 0)
759 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200760
761 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100762}
763
Harald Weltee988ec32024-03-14 21:29:28 +0100764/*! Unregister the given osmo_io_fd from osmo_io.
765 *
766 * After an osmo_io_fd has been successfully unregistered, it can no longer perform any I/O via osmo_io.
767 * However, it can be subsequently re-registered using osmo_iofd_register().
Harald Welte8857f3b2022-11-18 13:54:44 +0100768 *
769 * \param[in] iofd the file descriptor
770 * \returns zero on success, a negative value on error
771 */
772int osmo_iofd_unregister(struct osmo_io_fd *iofd)
773{
Harald Welte8b7af442024-03-07 10:41:57 +0100774 return osmo_iofd_ops.unregister_fd(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100775}
776
Harald Weltee988ec32024-03-14 21:29:28 +0100777/*! Retrieve the number of messages pending in the transmit queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100778 *
779 * \param[in] iofd the file descriptor
780 */
781unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
782{
783 return iofd->tx_queue.current_length;
784}
785
Harald Weltee988ec32024-03-14 21:29:28 +0100786/*! Clear the transmit queue of the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100787 *
788 * This function frees all messages currently pending in the transmit queue
789 * \param[in] iofd the file descriptor
790 */
791void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
792{
793 struct iofd_msghdr *hdr;
794 while ((hdr = iofd_txqueue_dequeue(iofd))) {
795 msgb_free(hdr->msg);
796 iofd_msghdr_free(hdr);
797 }
798}
799
Harald Weltee988ec32024-03-14 21:29:28 +0100800/*! Free the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100801 *
Harald Weltee988ec32024-03-14 21:29:28 +0100802 * The iofd will be automatically closed before via osmo_iofd_close() [which in turn will unregister
803 * it and clear any pending transmit queue items]. You must not reference the iofd
804 * after calling this function. However, it is safe to call this function from any of osmo_io
805 * call-backs; in this case, actual free will be internally delayed until that call-back completes.
806 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100807 * \param[in] iofd the file descriptor
808 */
809void osmo_iofd_free(struct osmo_io_fd *iofd)
810{
811 if (!iofd)
812 return;
813
814 osmo_iofd_close(iofd);
815
Daniel Willmannf89162f2023-06-26 19:24:46 +0200816 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100817 talloc_free(iofd);
818 } else {
819 /* Prevent our parent context from freeing us prematurely */
820 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200821 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100822 }
823}
824
Harald Weltee988ec32024-03-14 21:29:28 +0100825/*! Close the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100826 *
Harald Weltee988ec32024-03-14 21:29:28 +0100827 * This function closes the underlying fd, unregisters it from osmo_io and clears any messages in the tx
828 * 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 +0100829 * \param[in] iofd the file descriptor
Harald Weltee988ec32024-03-14 21:29:28 +0100830 * \returns 0 on success, a negative value otherwise
Harald Welte8857f3b2022-11-18 13:54:44 +0100831 */
832int osmo_iofd_close(struct osmo_io_fd *iofd)
833{
834 int rc = 0;
835
Daniel Willmannf89162f2023-06-26 19:24:46 +0200836 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100837 return rc;
838
Daniel Willmannf89162f2023-06-26 19:24:46 +0200839 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100840
841 /* Free pending msgs in tx queue */
842 osmo_iofd_txqueue_clear(iofd);
843 msgb_free(iofd->pending);
844
845 iofd->pending = NULL;
846
Harald Weltee1d48582024-03-07 10:34:21 +0100847 rc = osmo_iofd_ops.close(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100848 iofd->fd = -1;
849 return rc;
850}
851
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200852/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200853 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100854 * \param[in] size the size of the msgb when receiving data
855 * \param[in] headroom the headroom of the msgb when receiving data
856 */
857void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
858{
859 iofd->msgb_alloc.headroom = headroom;
860 iofd->msgb_alloc.size = size;
861}
862
Daniel Willmanna9303f32023-07-07 11:20:48 +0200863/*! Set the maximum number of messages enqueued for sending.
864 * \param[in] iofd the file descriptor
865 * \param[in] size the maximum size of the transmit queue
866 */
867void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
868{
869 iofd->tx_queue.max_length = max_length;
870}
871
Harald Weltee988ec32024-03-14 21:29:28 +0100872/*! Retrieve the associated user-data from an osmo_io_fd.
873 *
874 * A call to this function will return the opaque user data pointer which was specified previously
875 * via osmo_iofd_setup() or via osmo_iofd_set_data().
876 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100877 * \param[in] iofd the file descriptor
878 * \returns the data that was previously set with \ref osmo_iofd_setup()
879 */
880void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
881{
882 return iofd->data;
883}
884
Harald Weltee988ec32024-03-14 21:29:28 +0100885/*! Set the associated user-data from an osmo_io_fd.
886 *
887 * Calling this function will set/overwrite the opaque user data pointer, which can later be retrieved using
888 * osmo_iofd_get_data().
889 *
Harald Welte8857f3b2022-11-18 13:54:44 +0100890 * \param[in] iofd the file descriptor
891 * \param[in] data the data to set
892 */
893void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
894{
895 iofd->data = data;
896}
897
Harald Weltee988ec32024-03-14 21:29:28 +0100898/*! Retrieve the private number from an osmo_io_fd.
899 * Calling this function will retrieve the private user number previously set via osmo_iofd_set_priv_nr().
Harald Welte8857f3b2022-11-18 13:54:44 +0100900 * \param[in] iofd the file descriptor
901 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
902 */
903unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
904{
905 return iofd->priv_nr;
906}
907
Harald Weltee988ec32024-03-14 21:29:28 +0100908/*! Set the private number of an osmo_io_fd.
909 * The priv_nr passed in via this call can later be retrieved via osmo_iofd_get_priv_nr(). It provides
910 * 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 +0100911 * \param[in] iofd the file descriptor
912 * \param[in] priv_nr the private number to set
913 */
914void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
915{
916 iofd->priv_nr = priv_nr;
917}
918
Harald Weltee988ec32024-03-14 21:29:28 +0100919/*! Retrieve the underlying file descriptor from an osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100920 * \param[in] iofd the file descriptor
921 * \returns the underlying file descriptor number */
922int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
923{
924 return iofd->fd;
925}
926
Harald Weltee988ec32024-03-14 21:29:28 +0100927/*! Retrieve the human-readable name of the given osmo_io_fd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100928 * \param[in] iofd the file descriptor
929 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
930const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
931{
932 return iofd->name;
933}
934
Harald Weltee988ec32024-03-14 21:29:28 +0100935/*! Set the human-readable name of the file descriptor.
936 * 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 +0200937 * \param[in] iofd the file descriptor
938 * \param[in] name the name to set on the file descriptor */
939void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
940{
941 osmo_talloc_replace_string(iofd, &iofd->name, name);
942}
943
Harald Weltee988ec32024-03-14 21:29:28 +0100944/*! Set the osmo_io_ops calbacks for an osmo_io_fd.
945 * This function can be used to update/overwrite the call-back functions for the given osmo_io_fd; it
946 * replaces the currently-set call-back function pointers from a previous call to osmo_iofd_set_ioops()
947 * or the original osmo_iofd_setup().
arehbein0c374c62023-05-14 21:43:11 +0200948 * \param[in] iofd Target iofd file descriptor
Harald Weltee988ec32024-03-14 21:29:28 +0100949 * \param[in] ioops osmo_io_ops structure to be copied to the osmo_io_fd.
950 * \returns 0 on success, negative on error */
Harald Welteb365b1d2024-02-23 16:08:49 +0100951int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
arehbein0c374c62023-05-14 21:43:11 +0200952{
Harald Welte09ab0412024-03-07 10:11:52 +0100953 if (!check_mode_callback_compat(iofd->mode, ioops)) {
954 LOGPIO(iofd, LOGL_ERROR, "rejecting call-backs incompatible with mode %s\n",
955 osmo_iofd_mode_name(iofd->mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100956 return -EINVAL;
Harald Welte09ab0412024-03-07 10:11:52 +0100957 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100958
arehbein0c374c62023-05-14 21:43:11 +0200959 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200960
961 switch (iofd->mode) {
962 case OSMO_IO_FD_MODE_READ_WRITE:
963 if (iofd->io_ops.read_cb)
964 osmo_iofd_ops.read_enable(iofd);
965 else
966 osmo_iofd_ops.read_disable(iofd);
967 break;
968 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
969 if (iofd->io_ops.recvfrom_cb)
970 osmo_iofd_ops.read_enable(iofd);
971 else
972 osmo_iofd_ops.read_disable(iofd);
973 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100974 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
975 if (iofd->io_ops.recvmsg_cb)
976 osmo_iofd_ops.read_enable(iofd);
977 else
978 osmo_iofd_ops.read_disable(iofd);
979 break;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200980 default:
981 OSMO_ASSERT(0);
982 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100983
984 return 0;
arehbein0c374c62023-05-14 21:43:11 +0200985}
986
Harald Weltee988ec32024-03-14 21:29:28 +0100987/*! Retrieve the osmo_io_ops for an iofd.
Harald Weltef574aea2024-02-23 12:07:03 +0100988 * \param[in] iofd Target iofd file descriptor
989 * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */
990void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
991{
992 *ioops = iofd->io_ops;
993}
994
Harald Weltee988ec32024-03-14 21:29:28 +0100995/*! Request notification of the user if/when a client socket is connected.
996 * Calling this function will request osmo_io to notify the user (via
997 * write call-back) once a non-blocking outbound connect() of the
998 * socket completes.
999 *
1000 * This only works for connection oriented sockets in either
1001 * OSMO_IO_FD_MODE_READ_WRITE or OSMO_IO_FD_MODE_RECVMSG_SENDMSG mode.
1002 *
Daniel Willmanne2a8dc42023-06-30 10:51:53 +02001003 * \param[in] iofd the file descriptor */
1004void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
1005{
Harald Welte1047ed72023-11-18 18:51:58 +01001006 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE ||
1007 iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Andreas Eversberg848faf92024-02-09 12:38:17 +01001008 osmo_iofd_ops.notify_connected(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +02001009}
1010
Harald Weltee988ec32024-03-14 21:29:28 +01001011/*! @} */
Daniel Willmanne2a8dc42023-06-30 10:51:53 +02001012
Harald Weltefab993b2024-03-14 21:27:40 +01001013#endif /* ifndef(EMBEDDED) */