blob: 8b53aa6808afbf1132b1e0f3e285b03f22874eea [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
108 osmo_iofd_init();
109}
110
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200111/*! Allocate the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100112 * \param[in] iofd the osmo_io file structure
113 * \param[in] action the action this msg(hdr) is for (read, write, ..)
114 * \param[in] msg the msg buffer to use. Will allocate a new one if NULL
Harald Welte1047ed72023-11-18 18:51:58 +0100115 * \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 +0100116 * \returns the newly allocated msghdr or NULL in case of error */
Harald Welte1047ed72023-11-18 18:51:58 +0100117struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg,
118 size_t cmsg_size)
Harald Welte8857f3b2022-11-18 13:54:44 +0100119{
Daniel Willmann012d9042023-08-10 10:47:25 +0200120 bool free_msg = false;
121 struct iofd_msghdr *hdr;
122
Harald Welte8857f3b2022-11-18 13:54:44 +0100123 if (!msg) {
124 msg = iofd_msgb_alloc(iofd);
Daniel Willmann012d9042023-08-10 10:47:25 +0200125 if (!msg)
Harald Welte8857f3b2022-11-18 13:54:44 +0100126 return NULL;
Daniel Willmann012d9042023-08-10 10:47:25 +0200127 free_msg = true;
Daniel Willmannf0833822023-07-27 18:00:32 +0200128 } else {
Daniel Willmann012d9042023-08-10 10:47:25 +0200129 talloc_steal(iofd, msg);
130 }
131
Harald Welte1047ed72023-11-18 18:51:58 +0100132 hdr = talloc_zero_size(iofd, sizeof(struct iofd_msghdr) + cmsg_size);
Daniel Willmann012d9042023-08-10 10:47:25 +0200133 if (!hdr) {
134 if (free_msg)
135 talloc_free(msg);
136 return NULL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100137 }
138
139 hdr->action = action;
140 hdr->iofd = iofd;
141 hdr->msg = msg;
142
143 return hdr;
144}
145
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200146/*! Free the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100147 * \param[in] msghdr the msghdr to free
148 */
149void iofd_msghdr_free(struct iofd_msghdr *msghdr)
150{
151 /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or
152 * or passed on to the read callback which takes ownership. */
153 talloc_free(msghdr);
154}
155
156/*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */
157struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd)
158{
159 uint16_t headroom = iofd->msgb_alloc.headroom;
160
161 OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom);
Andreas Eversberga4ac5b82024-02-28 16:36:29 +0100162 return msgb_alloc_headroom_c(iofd, iofd->msgb_alloc.size + headroom, headroom, "osmo_io_msgb");
Harald Welte8857f3b2022-11-18 13:54:44 +0100163}
164
165/*! return the pending msgb in iofd or NULL if there is none*/
166struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd)
167{
168 struct msgb *msg = NULL;
169
170 msg = iofd->pending;
171 iofd->pending = NULL;
172
173 return msg;
174}
175
176/*! Return the pending msgb or allocate and return a new one */
177struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
178{
179 struct msgb *msg = NULL;
180
181 msg = iofd_msgb_pending(iofd);
182 if (!msg)
183 msg = iofd_msgb_alloc(iofd);
184
185 return msg;
186}
187
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200188/*! Enqueue a message to be sent.
Harald Welte8857f3b2022-11-18 13:54:44 +0100189 *
190 * Enqueues the message at the back of the queue provided there is enough space.
191 * \param[in] iofd the file descriptor
192 * \param[in] msghdr the message to enqueue
193 * \returns 0 if the message was enqueued succcessfully,
194 * -ENOSPC if the queue already contains the maximum number of messages
195 */
196int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
197{
198 if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length)
199 return -ENOSPC;
200
201 llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue);
202 iofd->tx_queue.current_length++;
203
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200204 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100205 osmo_iofd_ops.write_enable(iofd);
206
207 return 0;
208}
209
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200210/*! Enqueue a message at the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100211 *
212 * Used to enqueue a msgb from a partial send again. This function will always
213 * enqueue the message, even if the maximum number of messages is reached.
214 * \param[in] iofd the file descriptor
215 * \param[in] msghdr the message to enqueue
216 */
217void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
218{
219 llist_add(&msghdr->list, &iofd->tx_queue.msg_queue);
220 iofd->tx_queue.current_length++;
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200221
222 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
223 osmo_iofd_ops.write_enable(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100224}
225
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200226/*! Dequeue a message from the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100227 *
228 * \param[in] iofd the file descriptor
229 * \returns the msghdr from the front of the queue or NULL if the queue is empty
230 */
231struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
232{
233 struct llist_head *lh;
234
235 if (iofd->tx_queue.current_length == 0)
236 return NULL;
237
238 lh = iofd->tx_queue.msg_queue.next;
239
240 OSMO_ASSERT(lh);
241 iofd->tx_queue.current_length--;
242 llist_del(lh);
243
244 if (iofd->tx_queue.current_length == 0)
245 osmo_iofd_ops.write_disable(iofd);
246
247 return llist_entry(lh, struct iofd_msghdr, list);
248}
249
250/*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain
251 * one complete message.
252 * If there are bytes left over, *pending_out will point to a msgb with the remaining data.
253*/
254static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out)
255{
arehbeinc0aa4bd2023-06-16 22:31:32 +0200256 int extra_len, received_len;
Harald Welte8857f3b2022-11-18 13:54:44 +0100257 struct msgb *msg_pending;
258
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200259 /* Save the start of message before segmentation_cb (which could change it) */
260 uint8_t *data = msg->data;
261
arehbeinc0aa4bd2023-06-16 22:31:32 +0200262 received_len = msgb_length(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100263
264 if (!iofd->io_ops.segmentation_cb) {
265 *pending_out = NULL;
266 return IOFD_SEG_ACT_HANDLE_ONE;
267 }
268
arehbeinc0aa4bd2023-06-16 22:31:32 +0200269 int expected_len = iofd->io_ops.segmentation_cb(msg);
270 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200271 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200272 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200273 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200274 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
275 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100276 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200277 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100278 return IOFD_SEG_ACT_DEFER;
279 }
280
arehbeinc0aa4bd2023-06-16 22:31:32 +0200281 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200282 /* No segmentation needed, return the whole msgb */
283 if (extra_len == 0) {
284 *pending_out = NULL;
285 return IOFD_SEG_ACT_HANDLE_ONE;
286 /* segment is incomplete */
287 } else if (extra_len < 0) {
288 goto defer;
289 }
290
291 /* msgb contains more than one segment */
292 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100293 msg_pending = iofd_msgb_alloc(iofd);
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200294 memcpy(msgb_data(msg_pending), data + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200295 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100296 *pending_out = msg_pending;
297
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200298 /* Trim the original msgb to size. Don't use msgb_trim because we need to reference
299 * msg->data from before it might have been modified by the segmentation_cb(). */
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200300 msg->tail = data + expected_len;
Daniel Willmann97d21442023-07-18 09:46:27 +0200301 msg->len = msg->tail - msg->data;
Harald Welte8857f3b2022-11-18 13:54:44 +0100302 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200303
304defer:
305 *pending_out = msg;
306 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100307}
308
309/*! Restore message boundaries on read() and pass individual messages to the read callback
310 */
311void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
312{
313 int res;
314 struct msgb *pending = NULL;
315
Harald Welteb365b1d2024-02-23 16:08:49 +0100316 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
317
Harald Welte8857f3b2022-11-18 13:54:44 +0100318 if (rc <= 0) {
319 iofd->io_ops.read_cb(iofd, rc, msg);
320 return;
321 }
322
323 do {
324 res = iofd_handle_segmentation(iofd, msg, &pending);
325 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
326 iofd->io_ops.read_cb(iofd, rc, msg);
327 if (res == IOFD_SEG_ACT_HANDLE_MORE)
328 msg = pending;
329 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
330
331 OSMO_ASSERT(iofd->pending == NULL);
332 iofd->pending = pending;
333}
334
Harald Welte987a86a2023-11-18 18:46:24 +0100335/*! completion handler: Called by osmo_io backend after a given I/O operation has completed
336 * \param[in] iofd I/O file-descriptor on which I/O has completed
337 * \param[in] msg message buffer containing data related to completed I/O
Andreas Eversberg76f76782024-02-14 14:33:10 +0100338 * \param[in] rc result code with read size or error (-errno)
Harald Welte987a86a2023-11-18 18:46:24 +0100339 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200340void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
341{
Daniel Willmann012d9042023-08-10 10:47:25 +0200342 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200343 switch (iofd->mode) {
344 case OSMO_IO_FD_MODE_READ_WRITE:
345 iofd_handle_segmented_read(iofd, msg, rc);
346 break;
347 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
348 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
349 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100350 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
351 iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr);
352 break;
353 default:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200354 OSMO_ASSERT(false);
355 break;
356 }
357}
358
Daniel Willmann84611882023-11-21 10:17:00 +0100359/*! completion handler: Calld by osmo_io backend after a given I/O operation has completed
360 * \param[in] iofd I/O file-descriptor on which I/O has completed
361 * \param[in] rc return value of the I/O operation
362 * \param[in] msghdr serialized msghdr containing state of completed I/O
363 */
364void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
365{
366 struct msgb *msg = msghdr->msg;
367
368 /* Incomplete write */
369 if (rc > 0 && rc < msgb_length(msg)) {
370 /* Re-enqueue remaining data */
371 msgb_pull(msg, rc);
372 msghdr->iov[0].iov_len = msgb_length(msg);
373 iofd_txqueue_enqueue_front(iofd, msghdr);
374 return;
375 }
376
377 /* Reenqueue the complete msgb */
378 if (rc == -EAGAIN) {
379 iofd_txqueue_enqueue_front(iofd, msghdr);
380 return;
381 }
382
383 /* All other failure and success cases are handled here */
384 switch (msghdr->action) {
385 case IOFD_ACT_WRITE:
386 iofd->io_ops.write_cb(iofd, rc, msg);
387 break;
388 case IOFD_ACT_SENDTO:
389 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
390 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100391 case IOFD_ACT_SENDMSG:
392 iofd->io_ops.sendmsg_cb(iofd, rc, msg);
393 break;
Daniel Willmann84611882023-11-21 10:17:00 +0100394 default:
395 OSMO_ASSERT(0);
396 }
397
398 msgb_free(msghdr->msg);
399 iofd_msghdr_free(msghdr);
400}
401
Harald Welte8857f3b2022-11-18 13:54:44 +0100402/* Public functions */
403
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200404/*! Send a message through a connected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100405 *
406 * Appends the message to the internal transmit queue.
407 * If the function returns success (0) it will take ownership of the msgb and
408 * internally call msgb_free() after the write request completes.
409 * In case of an error the msgb needs to be freed by the caller.
410 * \param[in] iofd file descriptor to write to
411 * \param[in] msg message buffer to write
412 * \returns 0 in case of success; a negative value in case of error
413 */
414int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
415{
416 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200417
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100418 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
419 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
420 return -EINVAL;
421 }
422
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100423 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200424 if (OSMO_UNLIKELY(!iofd->io_ops.write_cb)) {
425 LOGPIO(iofd, LOGL_ERROR, "write_cb not set, Rejecting msgb\n");
426 return -EINVAL;
427 }
428
Harald Welte1047ed72023-11-18 18:51:58 +0100429 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100430 if (!msghdr)
431 return -ENOMEM;
432
Daniel Willmann92efac22023-08-01 09:55:13 +0200433 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100434 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
435 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
436 msghdr->hdr.msg_iov = &msghdr->iov[0];
437 msghdr->hdr.msg_iovlen = 1;
438
439 rc = iofd_txqueue_enqueue(iofd, msghdr);
440 if (rc < 0) {
441 iofd_msghdr_free(msghdr);
442 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
443 return rc;
444 }
445
446 return 0;
447}
448
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200449/*! Send a message through an unconnected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100450 *
451 * Appends the message to the internal transmit queue.
452 * If the function returns success (0), it will take ownership of the msgb and
453 * internally call msgb_free() after the write request completes.
454 * In case of an error the msgb needs to be freed by the caller.
455 * \param[in] iofd file descriptor to write to
456 * \param[in] msg message buffer to send
457 * \param[in] sendto_flags Flags to pass to the send call
458 * \param[in] dest destination address to send the message to
459 * \returns 0 in case of success; a negative value in case of error
460 */
461int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
462{
463 int rc;
464
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100465 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
466 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
467 return -EINVAL;
468 }
469
Harald Welte8857f3b2022-11-18 13:54:44 +0100470 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200471 if (OSMO_UNLIKELY(!iofd->io_ops.sendto_cb)) {
472 LOGPIO(iofd, LOGL_ERROR, "sendto_cb not set, Rejecting msgb\n");
473 return -EINVAL;
474 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100475
Harald Welte1047ed72023-11-18 18:51:58 +0100476 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100477 if (!msghdr)
478 return -ENOMEM;
479
480 if (dest) {
481 msghdr->osa = *dest;
482 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
483 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
484 }
485 msghdr->flags = sendto_flags;
486 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
487 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
488 msghdr->hdr.msg_iov = &msghdr->iov[0];
489 msghdr->hdr.msg_iovlen = 1;
490
491 rc = iofd_txqueue_enqueue(iofd, msghdr);
492 if (rc < 0) {
493 iofd_msghdr_free(msghdr);
494 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
495 return rc;
496 }
497
498 return 0;
499}
500
Harald Welte1047ed72023-11-18 18:51:58 +0100501/*! ismo_io equivalent of the sendmsg(2) socket API call
502 *
503 * Appends the message to the internal transmit queue.
504 * If the function returns success (0), it will take ownership of the msgb and
505 * internally call msgb_free() after the write request completes.
506 * In case of an error the msgb needs to be freed by the caller.
507 * \param[in] iofd file descriptor to write to
508 * \param[in] msg message buffer to send; is used to fill msgh->iov[]
509 * \param[in] sendmsg_flags Flags to pass to the send call
510 * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty!
511 * \returns 0 in case of success; a negative value in case of error
512 */
513int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
514{
515 int rc;
516 struct iofd_msghdr *msghdr;
517
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100518 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
519 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
520 return -EINVAL;
521 }
522
Harald Welte1047ed72023-11-18 18:51:58 +0100523 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
524 if (OSMO_UNLIKELY(!iofd->io_ops.sendmsg_cb)) {
525 LOGPIO(iofd, LOGL_ERROR, "sendmsg_cb not set, Rejecting msgb\n");
526 return -EINVAL;
527 }
528
529 if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) {
530 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n",
531 msgh->msg_namelen, sizeof(msghdr->osa));
532 return -EINVAL;
533 }
534
535 if (OSMO_UNLIKELY(msgh->msg_iovlen)) {
536 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n");
537 return -EINVAL;
538 }
539
540 msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen);
541 if (!msghdr)
542 return -ENOMEM;
543
544 /* copy over optional address */
545 if (msgh->msg_name) {
546 memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen);
547 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
548 msghdr->hdr.msg_namelen = msgh->msg_namelen;
549 }
550
551 /* build iov from msgb */
552 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
553 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
554 msghdr->hdr.msg_iov = &msghdr->iov[0];
555 msghdr->hdr.msg_iovlen = 1;
556
557 /* copy over the cmsg from the msghdr */
558 if (msgh->msg_control && msgh->msg_controllen) {
559 msghdr->hdr.msg_control = msghdr->cmsg;
560 msghdr->hdr.msg_controllen = msgh->msg_controllen;
561 memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen);
562 }
563
564 /* copy over msg_flags */
565 msghdr->hdr.msg_flags = sendmsg_flags;
566
567 rc = iofd_txqueue_enqueue(iofd, msghdr);
568 if (rc < 0) {
569 iofd_msghdr_free(msghdr);
570 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
571 return rc;
572 }
573
574 return 0;
575}
576
Harald Welteb365b1d2024-02-23 16:08:49 +0100577static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops)
578{
579 switch (mode) {
580 case OSMO_IO_FD_MODE_READ_WRITE:
581 if (ops->recvfrom_cb || ops->sendto_cb)
582 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100583 if (ops->recvmsg_cb || ops->sendmsg_cb)
584 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100585 break;
586 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
587 if (ops->read_cb || ops->write_cb)
588 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100589 if (ops->recvmsg_cb || ops->sendmsg_cb)
590 return false;
591 break;
592 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
593 if (ops->recvfrom_cb || ops->sendto_cb)
594 return false;
595 if (ops->read_cb || ops->write_cb)
596 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100597 break;
598 default:
599 break;
600 }
601
602 return true;
603}
604
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200605/*! Allocate and setup a new iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100606 * \param[in] ctx the parent context from which to allocate
607 * \param[in] fd the underlying system file descriptor
608 * \param[in] name the name of the iofd
609 * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
610 * \param[in] ioops structure with read/write/send/recv callbacks
611 * \param[in] data user data pointer accessible by the ioops callbacks
612 * \returns The newly allocated osmo_io_fd struct or NULL on failure
613 */
614struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
615 const struct osmo_io_ops *ioops, void *data)
616{
Harald Weltec380f292023-11-18 19:54:46 +0100617 struct osmo_io_fd *iofd;
618
619 /* reject unsupported/unknown modes */
620 switch (mode) {
621 case OSMO_IO_FD_MODE_READ_WRITE:
622 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
Harald Welte1047ed72023-11-18 18:51:58 +0100623 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
Harald Weltec380f292023-11-18 19:54:46 +0100624 break;
625 default:
626 return NULL;
627 }
628
Harald Welte09ab0412024-03-07 10:11:52 +0100629 if (ioops && !check_mode_callback_compat(mode, ioops)) {
630 LOGP(DLIO, LOGL_ERROR, "iofd(%s): rejecting call-backs incompatible with mode %s\n",
631 name ? name : "unknown", osmo_iofd_mode_name(mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100632 return NULL;
Harald Welte09ab0412024-03-07 10:11:52 +0100633 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100634
Harald Weltec380f292023-11-18 19:54:46 +0100635 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100636 if (!iofd)
637 return NULL;
638
639 iofd->fd = fd;
640 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200641 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100642
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200643 if (name)
644 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100645
646 if (ioops)
647 iofd->io_ops = *ioops;
648
649 iofd->pending = NULL;
650
651 iofd->data = data;
652
653 iofd->msgb_alloc.ctx = ctx;
654 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
655 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
656
657 iofd->tx_queue.max_length = 32;
658 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
659
660 return iofd;
661}
662
Harald Welte1047ed72023-11-18 18:51:58 +0100663/*! Set the size of the control message buffer allocated when submitting recvmsg */
664int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
665{
666 if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG)
667 return -EINVAL;
668
669 iofd->cmsg_size = cmsg_size;
670 return 0;
671}
672
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200673/*! Register the fd with the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100674 *
675 * \param[in] iofd the iofd file descriptor
676 * \param[in] fd the system fd number that will be registeres. If negative will use the one already set.
677 * \returns zero on success, a negative value on error
678*/
679int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
680{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200681 int rc = 0;
682
Harald Welte8857f3b2022-11-18 13:54:44 +0100683 if (fd >= 0)
684 iofd->fd = fd;
Harald Welte04e6f6e2024-03-07 09:57:22 +0100685 else if (iofd->fd < 0) {
686 /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */
687 LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd);
688 return -EBADF;
689 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100690
691 if (osmo_iofd_ops.register_fd)
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200692 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200693 if (rc)
694 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100695
Daniel Willmannf89162f2023-06-26 19:24:46 +0200696 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Harald Welteb365b1d2024-02-23 16:08:49 +0100697 if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) ||
Harald Welte1047ed72023-11-18 18:51:58 +0100698 (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) ||
699 (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) {
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200700 osmo_iofd_ops.read_enable(iofd);
Harald Welteb365b1d2024-02-23 16:08:49 +0100701 }
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200702
703 if (iofd->tx_queue.current_length > 0)
704 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200705
706 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100707}
708
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200709/*! Unregister the fd from the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100710 *
711 * \param[in] iofd the file descriptor
712 * \returns zero on success, a negative value on error
713 */
714int osmo_iofd_unregister(struct osmo_io_fd *iofd)
715{
716 if (osmo_iofd_ops.unregister_fd)
717 return osmo_iofd_ops.unregister_fd(iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200718 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100719
720 return 0;
721}
722
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200723/*! Get the number of messages in the tx queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100724 *
725 * \param[in] iofd the file descriptor
726 */
727unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
728{
729 return iofd->tx_queue.current_length;
730}
731
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200732/*! Clear the transmit queue of the the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100733 *
734 * This function frees all messages currently pending in the transmit queue
735 * \param[in] iofd the file descriptor
736 */
737void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
738{
739 struct iofd_msghdr *hdr;
740 while ((hdr = iofd_txqueue_dequeue(iofd))) {
741 msgb_free(hdr->msg);
742 iofd_msghdr_free(hdr);
743 }
744}
745
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200746/*! Free the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100747 *
748 * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so.
749 * The iofd will be closed before.
750 * \param[in] iofd the file descriptor
751 */
752void osmo_iofd_free(struct osmo_io_fd *iofd)
753{
754 if (!iofd)
755 return;
756
757 osmo_iofd_close(iofd);
758
Daniel Willmannf89162f2023-06-26 19:24:46 +0200759 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100760 talloc_free(iofd);
761 } else {
762 /* Prevent our parent context from freeing us prematurely */
763 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200764 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100765 }
766}
767
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200768/*! Close the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100769 *
770 * This function closes the underlying fd and clears any messages in the tx queue
771 * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()
772 * \param[in] iofd the file descriptor
773 * \ returns 0 on success, a negative value otherwise
774 */
775int osmo_iofd_close(struct osmo_io_fd *iofd)
776{
777 int rc = 0;
778
Daniel Willmannf89162f2023-06-26 19:24:46 +0200779 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100780 return rc;
781
Daniel Willmannf89162f2023-06-26 19:24:46 +0200782 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100783
784 /* Free pending msgs in tx queue */
785 osmo_iofd_txqueue_clear(iofd);
786 msgb_free(iofd->pending);
787
788 iofd->pending = NULL;
789
Harald Weltee1d48582024-03-07 10:34:21 +0100790 OSMO_ASSERT(osmo_iofd_ops.close);
791 rc = osmo_iofd_ops.close(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100792 iofd->fd = -1;
793 return rc;
794}
795
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200796/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200797 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100798 * \param[in] size the size of the msgb when receiving data
799 * \param[in] headroom the headroom of the msgb when receiving data
800 */
801void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
802{
803 iofd->msgb_alloc.headroom = headroom;
804 iofd->msgb_alloc.size = size;
805}
806
Daniel Willmanna9303f32023-07-07 11:20:48 +0200807/*! Set the maximum number of messages enqueued for sending.
808 * \param[in] iofd the file descriptor
809 * \param[in] size the maximum size of the transmit queue
810 */
811void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
812{
813 iofd->tx_queue.max_length = max_length;
814}
815
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200816/*! Get the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100817 * \param[in] iofd the file descriptor
818 * \returns the data that was previously set with \ref osmo_iofd_setup()
819 */
820void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
821{
822 return iofd->data;
823}
824
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200825/*! Set the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100826 * \param[in] iofd the file descriptor
827 * \param[in] data the data to set
828 */
829void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
830{
831 iofd->data = data;
832}
833
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200834/*! Get the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100835 * \param[in] iofd the file descriptor
836 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
837 */
838unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
839{
840 return iofd->priv_nr;
841}
842
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200843/*! Set the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100844 * \param[in] iofd the file descriptor
845 * \param[in] priv_nr the private number to set
846 */
847void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
848{
849 iofd->priv_nr = priv_nr;
850}
851
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200852/*! Get the underlying file descriptor from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100853 * \param[in] iofd the file descriptor
854 * \returns the underlying file descriptor number */
855int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
856{
857 return iofd->fd;
858}
859
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200860/*! Get the name of the file descriptor.
Harald Welte8857f3b2022-11-18 13:54:44 +0100861 * \param[in] iofd the file descriptor
862 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
863const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
864{
865 return iofd->name;
866}
867
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200868/*! Set the name of the file descriptor.
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200869 * \param[in] iofd the file descriptor
870 * \param[in] name the name to set on the file descriptor */
871void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
872{
873 osmo_talloc_replace_string(iofd, &iofd->name, name);
874}
875
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200876/*! Set the osmo_io_ops for an iofd.
arehbein0c374c62023-05-14 21:43:11 +0200877 * \param[in] iofd Target iofd file descriptor
878 * \param[in] ioops osmo_io_ops structure to be set */
Harald Welteb365b1d2024-02-23 16:08:49 +0100879int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
arehbein0c374c62023-05-14 21:43:11 +0200880{
Harald Welte09ab0412024-03-07 10:11:52 +0100881 if (!check_mode_callback_compat(iofd->mode, ioops)) {
882 LOGPIO(iofd, LOGL_ERROR, "rejecting call-backs incompatible with mode %s\n",
883 osmo_iofd_mode_name(iofd->mode));
Harald Welteb365b1d2024-02-23 16:08:49 +0100884 return -EINVAL;
Harald Welte09ab0412024-03-07 10:11:52 +0100885 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100886
arehbein0c374c62023-05-14 21:43:11 +0200887 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200888
889 switch (iofd->mode) {
890 case OSMO_IO_FD_MODE_READ_WRITE:
891 if (iofd->io_ops.read_cb)
892 osmo_iofd_ops.read_enable(iofd);
893 else
894 osmo_iofd_ops.read_disable(iofd);
895 break;
896 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
897 if (iofd->io_ops.recvfrom_cb)
898 osmo_iofd_ops.read_enable(iofd);
899 else
900 osmo_iofd_ops.read_disable(iofd);
901 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100902 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
903 if (iofd->io_ops.recvmsg_cb)
904 osmo_iofd_ops.read_enable(iofd);
905 else
906 osmo_iofd_ops.read_disable(iofd);
907 break;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200908 default:
909 OSMO_ASSERT(0);
910 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100911
912 return 0;
arehbein0c374c62023-05-14 21:43:11 +0200913}
914
Harald Weltef574aea2024-02-23 12:07:03 +0100915/*! Get the osmo_io_ops for an iofd.
916 * \param[in] iofd Target iofd file descriptor
917 * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */
918void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
919{
920 *ioops = iofd->io_ops;
921}
922
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200923/*! Notify the user if/when the socket is connected.
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200924 * When the socket is connected the write_cb will be called.
925 * \param[in] iofd the file descriptor */
926void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
927{
Harald Welte1047ed72023-11-18 18:51:58 +0100928 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE ||
929 iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Andreas Eversberg848faf92024-02-09 12:38:17 +0100930 OSMO_ASSERT(osmo_iofd_ops.notify_connected);
931 osmo_iofd_ops.notify_connected(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200932}
933
934
Harald Welte8857f3b2022-11-18 13:54:44 +0100935#endif /* defined(__linux__) */