blob: 57eba6cbe537e835ea34e1616de777c118ea0a70 [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \file osmo_io.c
2 * 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"
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
48const struct value_string osmo_io_backend_names[] = {
49 { OSMO_IO_BACKEND_POLL, "poll" },
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010050 { OSMO_IO_BACKEND_IO_URING, "io_uring" },
Harald Welte8857f3b2022-11-18 13:54:44 +010051 { 0, NULL }
52};
53
Harald Welte09ab0412024-03-07 10:11:52 +010054const struct value_string osmo_iofd_mode_names[] = {
55 { OSMO_IO_FD_MODE_READ_WRITE, "read/write" },
56 { OSMO_IO_FD_MODE_RECVFROM_SENDTO, "recvfrom/sendto" },
57 { OSMO_IO_FD_MODE_RECVMSG_SENDMSG, "recvmsg/sendmsg" },
58 { 0, NULL }
59};
60
Harald Welte8857f3b2022-11-18 13:54:44 +010061static enum osmo_io_backend g_io_backend;
62
63/* Used by some tests, can't be static */
64struct iofd_backend_ops osmo_iofd_ops;
65
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010066#if defined(HAVE_URING)
67void osmo_iofd_uring_init(void);
68#endif
69
Harald Welte8857f3b2022-11-18 13:54:44 +010070/*! initialize osmo_io for the current thread */
71void osmo_iofd_init(void)
72{
73 switch (g_io_backend) {
74 case OSMO_IO_BACKEND_POLL:
75 break;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010076#if defined(HAVE_URING)
77 case OSMO_IO_BACKEND_IO_URING:
78 osmo_iofd_uring_init();
79 break;
80#endif
Harald Welte8857f3b2022-11-18 13:54:44 +010081 default:
82 OSMO_ASSERT(0);
83 break;
84 }
85}
86
87/* ensure main thread always has pre-initialized osmo_io
88 * priority 103: run after on_dso_load_select */
89static __attribute__((constructor(103))) void on_dso_load_osmo_io(void)
90{
91 char *backend = getenv(OSMO_IO_BACKEND_ENV);
92 if (backend == NULL)
93 backend = OSMO_IO_BACKEND_DEFAULT;
94
95 if (!strcmp("POLL", backend)) {
96 g_io_backend = OSMO_IO_BACKEND_POLL;
97 osmo_iofd_ops = iofd_poll_ops;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010098#if defined(HAVE_URING)
99 } else if (!strcmp("IO_URING", backend)) {
100 g_io_backend = OSMO_IO_BACKEND_IO_URING;
101 osmo_iofd_ops = iofd_uring_ops;
102#endif
Harald Welte8857f3b2022-11-18 13:54:44 +0100103 } else {
104 fprintf(stderr, "Invalid osmo_io backend requested: \"%s\"\nCheck the environment variable %s\n", backend, OSMO_IO_BACKEND_ENV);
105 exit(1);
106 }
107
Harald Welte257e7892024-03-07 10:39:05 +0100108 OSMO_ASSERT(osmo_iofd_ops.close);
Harald Welte8b7af442024-03-07 10:41:57 +0100109 OSMO_ASSERT(osmo_iofd_ops.register_fd);
110 OSMO_ASSERT(osmo_iofd_ops.unregister_fd);
Harald Welte257e7892024-03-07 10:39:05 +0100111 OSMO_ASSERT(osmo_iofd_ops.write_enable);
112 OSMO_ASSERT(osmo_iofd_ops.write_disable);
113 OSMO_ASSERT(osmo_iofd_ops.read_enable);
114 OSMO_ASSERT(osmo_iofd_ops.read_disable);
115 OSMO_ASSERT(osmo_iofd_ops.notify_connected);
116
Harald Welte8857f3b2022-11-18 13:54:44 +0100117 osmo_iofd_init();
118}
119
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200120/*! Allocate the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100121 * \param[in] iofd the osmo_io file structure
122 * \param[in] action the action this msg(hdr) is for (read, write, ..)
123 * \param[in] msg the msg buffer to use. Will allocate a new one if NULL
Harald Welte1047ed72023-11-18 18:51:58 +0100124 * \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 +0100125 * \returns the newly allocated msghdr or NULL in case of error */
Harald Welte1047ed72023-11-18 18:51:58 +0100126struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg,
127 size_t cmsg_size)
Harald Welte8857f3b2022-11-18 13:54:44 +0100128{
Daniel Willmann012d9042023-08-10 10:47:25 +0200129 bool free_msg = false;
130 struct iofd_msghdr *hdr;
131
Harald Welte8857f3b2022-11-18 13:54:44 +0100132 if (!msg) {
133 msg = iofd_msgb_alloc(iofd);
Daniel Willmann012d9042023-08-10 10:47:25 +0200134 if (!msg)
Harald Welte8857f3b2022-11-18 13:54:44 +0100135 return NULL;
Daniel Willmann012d9042023-08-10 10:47:25 +0200136 free_msg = true;
Daniel Willmannf0833822023-07-27 18:00:32 +0200137 } else {
Daniel Willmann012d9042023-08-10 10:47:25 +0200138 talloc_steal(iofd, msg);
139 }
140
Harald Welte1047ed72023-11-18 18:51:58 +0100141 hdr = talloc_zero_size(iofd, sizeof(struct iofd_msghdr) + cmsg_size);
Daniel Willmann012d9042023-08-10 10:47:25 +0200142 if (!hdr) {
143 if (free_msg)
144 talloc_free(msg);
145 return NULL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100146 }
147
148 hdr->action = action;
149 hdr->iofd = iofd;
150 hdr->msg = msg;
151
152 return hdr;
153}
154
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200155/*! Free the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100156 * \param[in] msghdr the msghdr to free
157 */
158void iofd_msghdr_free(struct iofd_msghdr *msghdr)
159{
160 /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or
161 * or passed on to the read callback which takes ownership. */
162 talloc_free(msghdr);
163}
164
165/*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */
166struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd)
167{
168 uint16_t headroom = iofd->msgb_alloc.headroom;
169
170 OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom);
Andreas Eversberga4ac5b82024-02-28 16:36:29 +0100171 return msgb_alloc_headroom_c(iofd, iofd->msgb_alloc.size + headroom, headroom, "osmo_io_msgb");
Harald Welte8857f3b2022-11-18 13:54:44 +0100172}
173
174/*! return the pending msgb in iofd or NULL if there is none*/
175struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd)
176{
177 struct msgb *msg = NULL;
178
179 msg = iofd->pending;
180 iofd->pending = NULL;
181
182 return msg;
183}
184
185/*! Return the pending msgb or allocate and return a new one */
186struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
187{
188 struct msgb *msg = NULL;
189
190 msg = iofd_msgb_pending(iofd);
191 if (!msg)
192 msg = iofd_msgb_alloc(iofd);
193
194 return msg;
195}
196
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200197/*! Enqueue a message to be sent.
Harald Welte8857f3b2022-11-18 13:54:44 +0100198 *
199 * Enqueues the message at the back of the queue provided there is enough space.
200 * \param[in] iofd the file descriptor
201 * \param[in] msghdr the message to enqueue
202 * \returns 0 if the message was enqueued succcessfully,
203 * -ENOSPC if the queue already contains the maximum number of messages
204 */
205int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
206{
207 if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length)
208 return -ENOSPC;
209
210 llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue);
211 iofd->tx_queue.current_length++;
212
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200213 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100214 osmo_iofd_ops.write_enable(iofd);
215
216 return 0;
217}
218
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200219/*! Enqueue a message at the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100220 *
221 * Used to enqueue a msgb from a partial send again. This function will always
222 * enqueue the message, even if the maximum number of messages is reached.
223 * \param[in] iofd the file descriptor
224 * \param[in] msghdr the message to enqueue
225 */
226void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
227{
228 llist_add(&msghdr->list, &iofd->tx_queue.msg_queue);
229 iofd->tx_queue.current_length++;
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200230
231 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
232 osmo_iofd_ops.write_enable(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100233}
234
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200235/*! Dequeue a message from the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100236 *
237 * \param[in] iofd the file descriptor
238 * \returns the msghdr from the front of the queue or NULL if the queue is empty
239 */
240struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
241{
242 struct llist_head *lh;
243
244 if (iofd->tx_queue.current_length == 0)
245 return NULL;
246
247 lh = iofd->tx_queue.msg_queue.next;
248
249 OSMO_ASSERT(lh);
250 iofd->tx_queue.current_length--;
251 llist_del(lh);
252
253 if (iofd->tx_queue.current_length == 0)
254 osmo_iofd_ops.write_disable(iofd);
255
256 return llist_entry(lh, struct iofd_msghdr, list);
257}
258
259/*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain
260 * one complete message.
261 * If there are bytes left over, *pending_out will point to a msgb with the remaining data.
262*/
263static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out)
264{
arehbeinc0aa4bd2023-06-16 22:31:32 +0200265 int extra_len, received_len;
Harald Welte8857f3b2022-11-18 13:54:44 +0100266 struct msgb *msg_pending;
267
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200268 /* Save the start of message before segmentation_cb (which could change it) */
269 uint8_t *data = msg->data;
270
arehbeinc0aa4bd2023-06-16 22:31:32 +0200271 received_len = msgb_length(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100272
273 if (!iofd->io_ops.segmentation_cb) {
274 *pending_out = NULL;
275 return IOFD_SEG_ACT_HANDLE_ONE;
276 }
277
arehbeinc0aa4bd2023-06-16 22:31:32 +0200278 int expected_len = iofd->io_ops.segmentation_cb(msg);
279 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200280 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200281 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200282 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200283 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
284 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100285 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200286 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100287 return IOFD_SEG_ACT_DEFER;
288 }
289
arehbeinc0aa4bd2023-06-16 22:31:32 +0200290 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200291 /* No segmentation needed, return the whole msgb */
292 if (extra_len == 0) {
293 *pending_out = NULL;
294 return IOFD_SEG_ACT_HANDLE_ONE;
295 /* segment is incomplete */
296 } else if (extra_len < 0) {
297 goto defer;
298 }
299
300 /* msgb contains more than one segment */
301 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100302 msg_pending = iofd_msgb_alloc(iofd);
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200303 memcpy(msgb_data(msg_pending), data + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200304 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100305 *pending_out = msg_pending;
306
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200307 /* Trim the original msgb to size. Don't use msgb_trim because we need to reference
308 * msg->data from before it might have been modified by the segmentation_cb(). */
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200309 msg->tail = data + expected_len;
Daniel Willmann97d21442023-07-18 09:46:27 +0200310 msg->len = msg->tail - msg->data;
Harald Welte8857f3b2022-11-18 13:54:44 +0100311 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200312
313defer:
314 *pending_out = msg;
315 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100316}
317
318/*! Restore message boundaries on read() and pass individual messages to the read callback
319 */
320void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
321{
322 int res;
323 struct msgb *pending = NULL;
324
Harald Welteb365b1d2024-02-23 16:08:49 +0100325 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
326
Harald Welte8857f3b2022-11-18 13:54:44 +0100327 if (rc <= 0) {
328 iofd->io_ops.read_cb(iofd, rc, msg);
329 return;
330 }
331
332 do {
333 res = iofd_handle_segmentation(iofd, msg, &pending);
334 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
335 iofd->io_ops.read_cb(iofd, rc, msg);
336 if (res == IOFD_SEG_ACT_HANDLE_MORE)
337 msg = pending;
338 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
339
340 OSMO_ASSERT(iofd->pending == NULL);
341 iofd->pending = pending;
342}
343
Harald Welte987a86a2023-11-18 18:46:24 +0100344/*! completion handler: Called by osmo_io backend after a given I/O operation has completed
345 * \param[in] iofd I/O file-descriptor on which I/O has completed
346 * \param[in] msg message buffer containing data related to completed I/O
Andreas Eversberg76f76782024-02-14 14:33:10 +0100347 * \param[in] rc result code with read size or error (-errno)
Harald Welte987a86a2023-11-18 18:46:24 +0100348 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200349void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
350{
Daniel Willmann012d9042023-08-10 10:47:25 +0200351 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200352 switch (iofd->mode) {
353 case OSMO_IO_FD_MODE_READ_WRITE:
354 iofd_handle_segmented_read(iofd, msg, rc);
355 break;
356 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
357 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
358 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100359 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
360 iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr);
361 break;
362 default:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200363 OSMO_ASSERT(false);
364 break;
365 }
366}
367
Daniel Willmann84611882023-11-21 10:17:00 +0100368/*! completion handler: Calld by osmo_io backend after a given I/O operation has completed
369 * \param[in] iofd I/O file-descriptor on which I/O has completed
370 * \param[in] rc return value of the I/O operation
371 * \param[in] msghdr serialized msghdr containing state of completed I/O
372 */
373void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
374{
375 struct msgb *msg = msghdr->msg;
376
377 /* Incomplete write */
378 if (rc > 0 && rc < msgb_length(msg)) {
379 /* Re-enqueue remaining data */
380 msgb_pull(msg, rc);
381 msghdr->iov[0].iov_len = msgb_length(msg);
382 iofd_txqueue_enqueue_front(iofd, msghdr);
383 return;
384 }
385
386 /* Reenqueue the complete msgb */
387 if (rc == -EAGAIN) {
388 iofd_txqueue_enqueue_front(iofd, msghdr);
389 return;
390 }
391
392 /* All other failure and success cases are handled here */
393 switch (msghdr->action) {
394 case IOFD_ACT_WRITE:
395 iofd->io_ops.write_cb(iofd, rc, msg);
396 break;
397 case IOFD_ACT_SENDTO:
398 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
399 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100400 case IOFD_ACT_SENDMSG:
401 iofd->io_ops.sendmsg_cb(iofd, rc, msg);
402 break;
Daniel Willmann84611882023-11-21 10:17:00 +0100403 default:
404 OSMO_ASSERT(0);
405 }
406
407 msgb_free(msghdr->msg);
408 iofd_msghdr_free(msghdr);
409}
410
Harald Welte8857f3b2022-11-18 13:54:44 +0100411/* Public functions */
412
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200413/*! Send a message through a connected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100414 *
415 * Appends the message to the internal transmit queue.
416 * If the function returns success (0) it will take ownership of the msgb and
417 * internally call msgb_free() after the write request completes.
418 * In case of an error the msgb needs to be freed by the caller.
419 * \param[in] iofd file descriptor to write to
420 * \param[in] msg message buffer to write
421 * \returns 0 in case of success; a negative value in case of error
422 */
423int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
424{
425 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200426
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100427 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
428 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
429 return -EINVAL;
430 }
431
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100432 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200433 if (OSMO_UNLIKELY(!iofd->io_ops.write_cb)) {
434 LOGPIO(iofd, LOGL_ERROR, "write_cb not set, Rejecting msgb\n");
435 return -EINVAL;
436 }
437
Harald Welte1047ed72023-11-18 18:51:58 +0100438 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100439 if (!msghdr)
440 return -ENOMEM;
441
Daniel Willmann92efac22023-08-01 09:55:13 +0200442 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100443 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
444 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
445 msghdr->hdr.msg_iov = &msghdr->iov[0];
446 msghdr->hdr.msg_iovlen = 1;
447
448 rc = iofd_txqueue_enqueue(iofd, msghdr);
449 if (rc < 0) {
450 iofd_msghdr_free(msghdr);
451 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
452 return rc;
453 }
454
455 return 0;
456}
457
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200458/*! Send a message through an unconnected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100459 *
460 * Appends the message to the internal transmit queue.
461 * If the function returns success (0), it will take ownership of the msgb and
462 * internally call msgb_free() after the write request completes.
463 * In case of an error the msgb needs to be freed by the caller.
464 * \param[in] iofd file descriptor to write to
465 * \param[in] msg message buffer to send
466 * \param[in] sendto_flags Flags to pass to the send call
467 * \param[in] dest destination address to send the message to
468 * \returns 0 in case of success; a negative value in case of error
469 */
470int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
471{
472 int rc;
473
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100474 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
475 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
476 return -EINVAL;
477 }
478
Harald Welte8857f3b2022-11-18 13:54:44 +0100479 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200480 if (OSMO_UNLIKELY(!iofd->io_ops.sendto_cb)) {
481 LOGPIO(iofd, LOGL_ERROR, "sendto_cb not set, Rejecting msgb\n");
482 return -EINVAL;
483 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100484
Harald Welte1047ed72023-11-18 18:51:58 +0100485 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100486 if (!msghdr)
487 return -ENOMEM;
488
489 if (dest) {
490 msghdr->osa = *dest;
491 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
492 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
493 }
494 msghdr->flags = sendto_flags;
495 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
496 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
497 msghdr->hdr.msg_iov = &msghdr->iov[0];
498 msghdr->hdr.msg_iovlen = 1;
499
500 rc = iofd_txqueue_enqueue(iofd, msghdr);
501 if (rc < 0) {
502 iofd_msghdr_free(msghdr);
503 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
504 return rc;
505 }
506
507 return 0;
508}
509
Harald Welte1047ed72023-11-18 18:51:58 +0100510/*! ismo_io equivalent of the sendmsg(2) socket API call
511 *
512 * Appends the message to the internal transmit queue.
513 * If the function returns success (0), it will take ownership of the msgb and
514 * internally call msgb_free() after the write request completes.
515 * In case of an error the msgb needs to be freed by the caller.
516 * \param[in] iofd file descriptor to write to
517 * \param[in] msg message buffer to send; is used to fill msgh->iov[]
518 * \param[in] sendmsg_flags Flags to pass to the send call
519 * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty!
520 * \returns 0 in case of success; a negative value in case of error
521 */
522int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
523{
524 int rc;
525 struct iofd_msghdr *msghdr;
526
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100527 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
528 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
529 return -EINVAL;
530 }
531
Harald Welte1047ed72023-11-18 18:51:58 +0100532 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
533 if (OSMO_UNLIKELY(!iofd->io_ops.sendmsg_cb)) {
534 LOGPIO(iofd, LOGL_ERROR, "sendmsg_cb not set, Rejecting msgb\n");
535 return -EINVAL;
536 }
537
538 if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) {
539 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n",
540 msgh->msg_namelen, sizeof(msghdr->osa));
541 return -EINVAL;
542 }
543
544 if (OSMO_UNLIKELY(msgh->msg_iovlen)) {
545 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n");
546 return -EINVAL;
547 }
548
549 msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen);
550 if (!msghdr)
551 return -ENOMEM;
552
553 /* copy over optional address */
554 if (msgh->msg_name) {
555 memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen);
556 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
557 msghdr->hdr.msg_namelen = msgh->msg_namelen;
558 }
559
560 /* build iov from msgb */
561 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
562 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
563 msghdr->hdr.msg_iov = &msghdr->iov[0];
564 msghdr->hdr.msg_iovlen = 1;
565
566 /* copy over the cmsg from the msghdr */
567 if (msgh->msg_control && msgh->msg_controllen) {
568 msghdr->hdr.msg_control = msghdr->cmsg;
569 msghdr->hdr.msg_controllen = msgh->msg_controllen;
570 memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen);
571 }
572
573 /* copy over msg_flags */
574 msghdr->hdr.msg_flags = sendmsg_flags;
575
576 rc = iofd_txqueue_enqueue(iofd, msghdr);
577 if (rc < 0) {
578 iofd_msghdr_free(msghdr);
579 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
580 return rc;
581 }
582
583 return 0;
584}
585
Harald Welteb365b1d2024-02-23 16:08:49 +0100586static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops)
587{
588 switch (mode) {
589 case OSMO_IO_FD_MODE_READ_WRITE:
590 if (ops->recvfrom_cb || ops->sendto_cb)
591 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100592 if (ops->recvmsg_cb || ops->sendmsg_cb)
593 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100594 break;
595 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
596 if (ops->read_cb || ops->write_cb)
597 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100598 if (ops->recvmsg_cb || ops->sendmsg_cb)
599 return false;
600 break;
601 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
602 if (ops->recvfrom_cb || ops->sendto_cb)
603 return false;
604 if (ops->read_cb || ops->write_cb)
605 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100606 break;
607 default:
608 break;
609 }
610
611 return true;
612}
613
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200614/*! Allocate and setup a new iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100615 * \param[in] ctx the parent context from which to allocate
616 * \param[in] fd the underlying system file descriptor
617 * \param[in] name the name of the iofd
618 * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
619 * \param[in] ioops structure with read/write/send/recv callbacks
620 * \param[in] data user data pointer accessible by the ioops callbacks
621 * \returns The newly allocated osmo_io_fd struct or NULL on failure
622 */
623struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
624 const struct osmo_io_ops *ioops, void *data)
625{
Harald Weltec380f292023-11-18 19:54:46 +0100626 struct osmo_io_fd *iofd;
627
628 /* reject unsupported/unknown modes */
629 switch (mode) {
630 case OSMO_IO_FD_MODE_READ_WRITE:
631 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
Harald Welte1047ed72023-11-18 18:51:58 +0100632 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
Harald Weltec380f292023-11-18 19:54:46 +0100633 break;
634 default:
635 return NULL;
636 }
637
Harald Welte09ab0412024-03-07 10:11:52 +0100638 if (ioops && !check_mode_callback_compat(mode, ioops)) {
639 LOGP(DLIO, LOGL_ERROR, "iofd(%s): rejecting call-backs incompatible with mode %s\n",
640 name ? name : "unknown", osmo_iofd_mode_name(mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100641 return NULL;
Harald Welte09ab0412024-03-07 10:11:52 +0100642 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100643
Harald Weltec380f292023-11-18 19:54:46 +0100644 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100645 if (!iofd)
646 return NULL;
647
648 iofd->fd = fd;
649 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200650 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100651
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200652 if (name)
653 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100654
655 if (ioops)
656 iofd->io_ops = *ioops;
657
658 iofd->pending = NULL;
659
660 iofd->data = data;
661
662 iofd->msgb_alloc.ctx = ctx;
663 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
664 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
665
666 iofd->tx_queue.max_length = 32;
667 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
668
669 return iofd;
670}
671
Harald Welte1047ed72023-11-18 18:51:58 +0100672/*! Set the size of the control message buffer allocated when submitting recvmsg */
673int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
674{
675 if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG)
676 return -EINVAL;
677
678 iofd->cmsg_size = cmsg_size;
679 return 0;
680}
681
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200682/*! Register the fd with the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100683 *
684 * \param[in] iofd the iofd file descriptor
685 * \param[in] fd the system fd number that will be registeres. If negative will use the one already set.
686 * \returns zero on success, a negative value on error
687*/
688int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
689{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200690 int rc = 0;
691
Harald Welte8857f3b2022-11-18 13:54:44 +0100692 if (fd >= 0)
693 iofd->fd = fd;
Harald Welte04e6f6e2024-03-07 09:57:22 +0100694 else if (iofd->fd < 0) {
695 /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */
696 LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd);
697 return -EBADF;
698 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100699
Harald Welte8b7af442024-03-07 10:41:57 +0100700 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200701 if (rc)
702 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100703
Daniel Willmannf89162f2023-06-26 19:24:46 +0200704 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Harald Welteb365b1d2024-02-23 16:08:49 +0100705 if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) ||
Harald Welte1047ed72023-11-18 18:51:58 +0100706 (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) ||
707 (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) {
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200708 osmo_iofd_ops.read_enable(iofd);
Harald Welteb365b1d2024-02-23 16:08:49 +0100709 }
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200710
711 if (iofd->tx_queue.current_length > 0)
712 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200713
714 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100715}
716
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200717/*! Unregister the fd from the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100718 *
719 * \param[in] iofd the file descriptor
720 * \returns zero on success, a negative value on error
721 */
722int osmo_iofd_unregister(struct osmo_io_fd *iofd)
723{
Harald Welte8b7af442024-03-07 10:41:57 +0100724 return osmo_iofd_ops.unregister_fd(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100725}
726
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200727/*! Get the number of messages in the tx queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100728 *
729 * \param[in] iofd the file descriptor
730 */
731unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
732{
733 return iofd->tx_queue.current_length;
734}
735
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200736/*! Clear the transmit queue of the the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100737 *
738 * This function frees all messages currently pending in the transmit queue
739 * \param[in] iofd the file descriptor
740 */
741void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
742{
743 struct iofd_msghdr *hdr;
744 while ((hdr = iofd_txqueue_dequeue(iofd))) {
745 msgb_free(hdr->msg);
746 iofd_msghdr_free(hdr);
747 }
748}
749
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200750/*! Free the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100751 *
752 * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so.
753 * The iofd will be closed before.
754 * \param[in] iofd the file descriptor
755 */
756void osmo_iofd_free(struct osmo_io_fd *iofd)
757{
758 if (!iofd)
759 return;
760
761 osmo_iofd_close(iofd);
762
Daniel Willmannf89162f2023-06-26 19:24:46 +0200763 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100764 talloc_free(iofd);
765 } else {
766 /* Prevent our parent context from freeing us prematurely */
767 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200768 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100769 }
770}
771
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200772/*! Close the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100773 *
774 * This function closes the underlying fd and clears any messages in the tx queue
775 * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()
776 * \param[in] iofd the file descriptor
777 * \ returns 0 on success, a negative value otherwise
778 */
779int osmo_iofd_close(struct osmo_io_fd *iofd)
780{
781 int rc = 0;
782
Daniel Willmannf89162f2023-06-26 19:24:46 +0200783 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100784 return rc;
785
Daniel Willmannf89162f2023-06-26 19:24:46 +0200786 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100787
788 /* Free pending msgs in tx queue */
789 osmo_iofd_txqueue_clear(iofd);
790 msgb_free(iofd->pending);
791
792 iofd->pending = NULL;
793
Harald Weltee1d48582024-03-07 10:34:21 +0100794 rc = osmo_iofd_ops.close(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100795 iofd->fd = -1;
796 return rc;
797}
798
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200799/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200800 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100801 * \param[in] size the size of the msgb when receiving data
802 * \param[in] headroom the headroom of the msgb when receiving data
803 */
804void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
805{
806 iofd->msgb_alloc.headroom = headroom;
807 iofd->msgb_alloc.size = size;
808}
809
Daniel Willmanna9303f32023-07-07 11:20:48 +0200810/*! Set the maximum number of messages enqueued for sending.
811 * \param[in] iofd the file descriptor
812 * \param[in] size the maximum size of the transmit queue
813 */
814void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
815{
816 iofd->tx_queue.max_length = max_length;
817}
818
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200819/*! Get the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100820 * \param[in] iofd the file descriptor
821 * \returns the data that was previously set with \ref osmo_iofd_setup()
822 */
823void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
824{
825 return iofd->data;
826}
827
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200828/*! Set the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100829 * \param[in] iofd the file descriptor
830 * \param[in] data the data to set
831 */
832void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
833{
834 iofd->data = data;
835}
836
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200837/*! Get the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100838 * \param[in] iofd the file descriptor
839 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
840 */
841unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
842{
843 return iofd->priv_nr;
844}
845
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200846/*! Set the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100847 * \param[in] iofd the file descriptor
848 * \param[in] priv_nr the private number to set
849 */
850void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
851{
852 iofd->priv_nr = priv_nr;
853}
854
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200855/*! Get the underlying file descriptor from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100856 * \param[in] iofd the file descriptor
857 * \returns the underlying file descriptor number */
858int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
859{
860 return iofd->fd;
861}
862
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200863/*! Get the name of the file descriptor.
Harald Welte8857f3b2022-11-18 13:54:44 +0100864 * \param[in] iofd the file descriptor
865 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
866const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
867{
868 return iofd->name;
869}
870
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200871/*! Set the name of the file descriptor.
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200872 * \param[in] iofd the file descriptor
873 * \param[in] name the name to set on the file descriptor */
874void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
875{
876 osmo_talloc_replace_string(iofd, &iofd->name, name);
877}
878
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200879/*! Set the osmo_io_ops for an iofd.
arehbein0c374c62023-05-14 21:43:11 +0200880 * \param[in] iofd Target iofd file descriptor
881 * \param[in] ioops osmo_io_ops structure to be set */
Harald Welteb365b1d2024-02-23 16:08:49 +0100882int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
arehbein0c374c62023-05-14 21:43:11 +0200883{
Harald Welte09ab0412024-03-07 10:11:52 +0100884 if (!check_mode_callback_compat(iofd->mode, ioops)) {
885 LOGPIO(iofd, LOGL_ERROR, "rejecting call-backs incompatible with mode %s\n",
886 osmo_iofd_mode_name(iofd->mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100887 return -EINVAL;
Harald Welte09ab0412024-03-07 10:11:52 +0100888 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100889
arehbein0c374c62023-05-14 21:43:11 +0200890 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200891
892 switch (iofd->mode) {
893 case OSMO_IO_FD_MODE_READ_WRITE:
894 if (iofd->io_ops.read_cb)
895 osmo_iofd_ops.read_enable(iofd);
896 else
897 osmo_iofd_ops.read_disable(iofd);
898 break;
899 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
900 if (iofd->io_ops.recvfrom_cb)
901 osmo_iofd_ops.read_enable(iofd);
902 else
903 osmo_iofd_ops.read_disable(iofd);
904 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100905 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
906 if (iofd->io_ops.recvmsg_cb)
907 osmo_iofd_ops.read_enable(iofd);
908 else
909 osmo_iofd_ops.read_disable(iofd);
910 break;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200911 default:
912 OSMO_ASSERT(0);
913 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100914
915 return 0;
arehbein0c374c62023-05-14 21:43:11 +0200916}
917
Harald Weltef574aea2024-02-23 12:07:03 +0100918/*! Get the osmo_io_ops for an iofd.
919 * \param[in] iofd Target iofd file descriptor
920 * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */
921void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
922{
923 *ioops = iofd->io_ops;
924}
925
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200926/*! Notify the user if/when the socket is connected.
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200927 * When the socket is connected the write_cb will be called.
928 * \param[in] iofd the file descriptor */
929void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
930{
Harald Welte1047ed72023-11-18 18:51:58 +0100931 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE ||
932 iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Andreas Eversberg848faf92024-02-09 12:38:17 +0100933 osmo_iofd_ops.notify_connected(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200934}
935
936
Harald Welte8857f3b2022-11-18 13:54:44 +0100937#endif /* defined(__linux__) */