blob: 472edf9f5ce0ec16db25305767482fc6e7e5d3a3 [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
54static enum osmo_io_backend g_io_backend;
55
56/* Used by some tests, can't be static */
57struct iofd_backend_ops osmo_iofd_ops;
58
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010059#if defined(HAVE_URING)
60void osmo_iofd_uring_init(void);
61#endif
62
Harald Welte8857f3b2022-11-18 13:54:44 +010063/*! initialize osmo_io for the current thread */
64void osmo_iofd_init(void)
65{
66 switch (g_io_backend) {
67 case OSMO_IO_BACKEND_POLL:
68 break;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010069#if defined(HAVE_URING)
70 case OSMO_IO_BACKEND_IO_URING:
71 osmo_iofd_uring_init();
72 break;
73#endif
Harald Welte8857f3b2022-11-18 13:54:44 +010074 default:
75 OSMO_ASSERT(0);
76 break;
77 }
78}
79
80/* ensure main thread always has pre-initialized osmo_io
81 * priority 103: run after on_dso_load_select */
82static __attribute__((constructor(103))) void on_dso_load_osmo_io(void)
83{
84 char *backend = getenv(OSMO_IO_BACKEND_ENV);
85 if (backend == NULL)
86 backend = OSMO_IO_BACKEND_DEFAULT;
87
88 if (!strcmp("POLL", backend)) {
89 g_io_backend = OSMO_IO_BACKEND_POLL;
90 osmo_iofd_ops = iofd_poll_ops;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010091#if defined(HAVE_URING)
92 } else if (!strcmp("IO_URING", backend)) {
93 g_io_backend = OSMO_IO_BACKEND_IO_URING;
94 osmo_iofd_ops = iofd_uring_ops;
95#endif
Harald Welte8857f3b2022-11-18 13:54:44 +010096 } else {
97 fprintf(stderr, "Invalid osmo_io backend requested: \"%s\"\nCheck the environment variable %s\n", backend, OSMO_IO_BACKEND_ENV);
98 exit(1);
99 }
100
101 osmo_iofd_init();
102}
103
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200104/*! Allocate the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100105 * \param[in] iofd the osmo_io file structure
106 * \param[in] action the action this msg(hdr) is for (read, write, ..)
107 * \param[in] msg the msg buffer to use. Will allocate a new one if NULL
Harald Welte1047ed72023-11-18 18:51:58 +0100108 * \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 +0100109 * \returns the newly allocated msghdr or NULL in case of error */
Harald Welte1047ed72023-11-18 18:51:58 +0100110struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg,
111 size_t cmsg_size)
Harald Welte8857f3b2022-11-18 13:54:44 +0100112{
Daniel Willmann012d9042023-08-10 10:47:25 +0200113 bool free_msg = false;
114 struct iofd_msghdr *hdr;
115
Harald Welte8857f3b2022-11-18 13:54:44 +0100116 if (!msg) {
117 msg = iofd_msgb_alloc(iofd);
Daniel Willmann012d9042023-08-10 10:47:25 +0200118 if (!msg)
Harald Welte8857f3b2022-11-18 13:54:44 +0100119 return NULL;
Daniel Willmann012d9042023-08-10 10:47:25 +0200120 free_msg = true;
Daniel Willmannf0833822023-07-27 18:00:32 +0200121 } else {
Daniel Willmann012d9042023-08-10 10:47:25 +0200122 talloc_steal(iofd, msg);
123 }
124
Harald Welte1047ed72023-11-18 18:51:58 +0100125 hdr = talloc_zero_size(iofd, sizeof(struct iofd_msghdr) + cmsg_size);
Daniel Willmann012d9042023-08-10 10:47:25 +0200126 if (!hdr) {
127 if (free_msg)
128 talloc_free(msg);
129 return NULL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100130 }
131
132 hdr->action = action;
133 hdr->iofd = iofd;
134 hdr->msg = msg;
135
136 return hdr;
137}
138
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200139/*! Free the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100140 * \param[in] msghdr the msghdr to free
141 */
142void iofd_msghdr_free(struct iofd_msghdr *msghdr)
143{
144 /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or
145 * or passed on to the read callback which takes ownership. */
146 talloc_free(msghdr);
147}
148
149/*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */
150struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd)
151{
152 uint16_t headroom = iofd->msgb_alloc.headroom;
153
154 OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom);
Daniel Willmann012d9042023-08-10 10:47:25 +0200155 return msgb_alloc_headroom_c(iofd,
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200156 iofd->msgb_alloc.size + headroom, headroom,
157 iofd->name ? : "iofd_msgb");
Harald Welte8857f3b2022-11-18 13:54:44 +0100158}
159
160/*! return the pending msgb in iofd or NULL if there is none*/
161struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd)
162{
163 struct msgb *msg = NULL;
164
165 msg = iofd->pending;
166 iofd->pending = NULL;
167
168 return msg;
169}
170
171/*! Return the pending msgb or allocate and return a new one */
172struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
173{
174 struct msgb *msg = NULL;
175
176 msg = iofd_msgb_pending(iofd);
177 if (!msg)
178 msg = iofd_msgb_alloc(iofd);
179
180 return msg;
181}
182
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200183/*! Enqueue a message to be sent.
Harald Welte8857f3b2022-11-18 13:54:44 +0100184 *
185 * Enqueues the message at the back of the queue provided there is enough space.
186 * \param[in] iofd the file descriptor
187 * \param[in] msghdr the message to enqueue
188 * \returns 0 if the message was enqueued succcessfully,
189 * -ENOSPC if the queue already contains the maximum number of messages
190 */
191int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
192{
193 if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length)
194 return -ENOSPC;
195
196 llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue);
197 iofd->tx_queue.current_length++;
198
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200199 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100200 osmo_iofd_ops.write_enable(iofd);
201
202 return 0;
203}
204
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200205/*! Enqueue a message at the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100206 *
207 * Used to enqueue a msgb from a partial send again. This function will always
208 * enqueue the message, even if the maximum number of messages is reached.
209 * \param[in] iofd the file descriptor
210 * \param[in] msghdr the message to enqueue
211 */
212void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
213{
214 llist_add(&msghdr->list, &iofd->tx_queue.msg_queue);
215 iofd->tx_queue.current_length++;
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200216
217 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
218 osmo_iofd_ops.write_enable(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100219}
220
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200221/*! Dequeue a message from the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100222 *
223 * \param[in] iofd the file descriptor
224 * \returns the msghdr from the front of the queue or NULL if the queue is empty
225 */
226struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
227{
228 struct llist_head *lh;
229
230 if (iofd->tx_queue.current_length == 0)
231 return NULL;
232
233 lh = iofd->tx_queue.msg_queue.next;
234
235 OSMO_ASSERT(lh);
236 iofd->tx_queue.current_length--;
237 llist_del(lh);
238
239 if (iofd->tx_queue.current_length == 0)
240 osmo_iofd_ops.write_disable(iofd);
241
242 return llist_entry(lh, struct iofd_msghdr, list);
243}
244
245/*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain
246 * one complete message.
247 * If there are bytes left over, *pending_out will point to a msgb with the remaining data.
248*/
249static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out)
250{
arehbeinc0aa4bd2023-06-16 22:31:32 +0200251 int extra_len, received_len;
Harald Welte8857f3b2022-11-18 13:54:44 +0100252 struct msgb *msg_pending;
253
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200254 /* Save the start of message before segmentation_cb (which could change it) */
255 uint8_t *data = msg->data;
256
arehbeinc0aa4bd2023-06-16 22:31:32 +0200257 received_len = msgb_length(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100258
259 if (!iofd->io_ops.segmentation_cb) {
260 *pending_out = NULL;
261 return IOFD_SEG_ACT_HANDLE_ONE;
262 }
263
arehbeinc0aa4bd2023-06-16 22:31:32 +0200264 int expected_len = iofd->io_ops.segmentation_cb(msg);
265 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200266 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200267 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200268 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200269 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
270 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100271 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200272 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100273 return IOFD_SEG_ACT_DEFER;
274 }
275
arehbeinc0aa4bd2023-06-16 22:31:32 +0200276 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200277 /* No segmentation needed, return the whole msgb */
278 if (extra_len == 0) {
279 *pending_out = NULL;
280 return IOFD_SEG_ACT_HANDLE_ONE;
281 /* segment is incomplete */
282 } else if (extra_len < 0) {
283 goto defer;
284 }
285
286 /* msgb contains more than one segment */
287 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100288 msg_pending = iofd_msgb_alloc(iofd);
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200289 memcpy(msgb_data(msg_pending), data + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200290 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100291 *pending_out = msg_pending;
292
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200293 /* Trim the original msgb to size. Don't use msgb_trim because we need to reference
294 * msg->data from before it might have been modified by the segmentation_cb(). */
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200295 msg->tail = data + expected_len;
Daniel Willmann97d21442023-07-18 09:46:27 +0200296 msg->len = msg->tail - msg->data;
Harald Welte8857f3b2022-11-18 13:54:44 +0100297 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200298
299defer:
300 *pending_out = msg;
301 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100302}
303
304/*! Restore message boundaries on read() and pass individual messages to the read callback
305 */
306void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
307{
308 int res;
309 struct msgb *pending = NULL;
310
Harald Welteb365b1d2024-02-23 16:08:49 +0100311 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
312
Harald Welte8857f3b2022-11-18 13:54:44 +0100313 if (rc <= 0) {
314 iofd->io_ops.read_cb(iofd, rc, msg);
315 return;
316 }
317
318 do {
319 res = iofd_handle_segmentation(iofd, msg, &pending);
320 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
321 iofd->io_ops.read_cb(iofd, rc, msg);
322 if (res == IOFD_SEG_ACT_HANDLE_MORE)
323 msg = pending;
324 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
325
326 OSMO_ASSERT(iofd->pending == NULL);
327 iofd->pending = pending;
328}
329
Harald Welte987a86a2023-11-18 18:46:24 +0100330/*! completion handler: Called by osmo_io backend after a given I/O operation has completed
331 * \param[in] iofd I/O file-descriptor on which I/O has completed
332 * \param[in] msg message buffer containing data related to completed I/O
333 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200334void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
335{
Daniel Willmann012d9042023-08-10 10:47:25 +0200336 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200337 switch (iofd->mode) {
338 case OSMO_IO_FD_MODE_READ_WRITE:
339 iofd_handle_segmented_read(iofd, msg, rc);
340 break;
341 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
342 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
343 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100344 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
345 iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr);
346 break;
347 default:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200348 OSMO_ASSERT(false);
349 break;
350 }
351}
352
Daniel Willmann84611882023-11-21 10:17:00 +0100353/*! completion handler: Calld by osmo_io backend after a given I/O operation has completed
354 * \param[in] iofd I/O file-descriptor on which I/O has completed
355 * \param[in] rc return value of the I/O operation
356 * \param[in] msghdr serialized msghdr containing state of completed I/O
357 */
358void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
359{
360 struct msgb *msg = msghdr->msg;
361
362 /* Incomplete write */
363 if (rc > 0 && rc < msgb_length(msg)) {
364 /* Re-enqueue remaining data */
365 msgb_pull(msg, rc);
366 msghdr->iov[0].iov_len = msgb_length(msg);
367 iofd_txqueue_enqueue_front(iofd, msghdr);
368 return;
369 }
370
371 /* Reenqueue the complete msgb */
372 if (rc == -EAGAIN) {
373 iofd_txqueue_enqueue_front(iofd, msghdr);
374 return;
375 }
376
377 /* All other failure and success cases are handled here */
378 switch (msghdr->action) {
379 case IOFD_ACT_WRITE:
380 iofd->io_ops.write_cb(iofd, rc, msg);
381 break;
382 case IOFD_ACT_SENDTO:
383 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
384 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100385 case IOFD_ACT_SENDMSG:
386 iofd->io_ops.sendmsg_cb(iofd, rc, msg);
387 break;
Daniel Willmann84611882023-11-21 10:17:00 +0100388 default:
389 OSMO_ASSERT(0);
390 }
391
392 msgb_free(msghdr->msg);
393 iofd_msghdr_free(msghdr);
394}
395
Harald Welte8857f3b2022-11-18 13:54:44 +0100396/* Public functions */
397
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200398/*! Send a message through a connected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100399 *
400 * Appends the message to the internal transmit queue.
401 * If the function returns success (0) it will take ownership of the msgb and
402 * internally call msgb_free() after the write request completes.
403 * In case of an error the msgb needs to be freed by the caller.
404 * \param[in] iofd file descriptor to write to
405 * \param[in] msg message buffer to write
406 * \returns 0 in case of success; a negative value in case of error
407 */
408int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
409{
410 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200411
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100412 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
413 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
414 return -EINVAL;
415 }
416
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100417 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200418 if (OSMO_UNLIKELY(!iofd->io_ops.write_cb)) {
419 LOGPIO(iofd, LOGL_ERROR, "write_cb not set, Rejecting msgb\n");
420 return -EINVAL;
421 }
422
Harald Welte1047ed72023-11-18 18:51:58 +0100423 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100424 if (!msghdr)
425 return -ENOMEM;
426
Daniel Willmann92efac22023-08-01 09:55:13 +0200427 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100428 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
429 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
430 msghdr->hdr.msg_iov = &msghdr->iov[0];
431 msghdr->hdr.msg_iovlen = 1;
432
433 rc = iofd_txqueue_enqueue(iofd, msghdr);
434 if (rc < 0) {
435 iofd_msghdr_free(msghdr);
436 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
437 return rc;
438 }
439
440 return 0;
441}
442
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200443/*! Send a message through an unconnected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100444 *
445 * Appends the message to the internal transmit queue.
446 * If the function returns success (0), it will take ownership of the msgb and
447 * internally call msgb_free() after the write request completes.
448 * In case of an error the msgb needs to be freed by the caller.
449 * \param[in] iofd file descriptor to write to
450 * \param[in] msg message buffer to send
451 * \param[in] sendto_flags Flags to pass to the send call
452 * \param[in] dest destination address to send the message to
453 * \returns 0 in case of success; a negative value in case of error
454 */
455int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
456{
457 int rc;
458
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100459 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
460 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
461 return -EINVAL;
462 }
463
Harald Welte8857f3b2022-11-18 13:54:44 +0100464 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200465 if (OSMO_UNLIKELY(!iofd->io_ops.sendto_cb)) {
466 LOGPIO(iofd, LOGL_ERROR, "sendto_cb not set, Rejecting msgb\n");
467 return -EINVAL;
468 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100469
Harald Welte1047ed72023-11-18 18:51:58 +0100470 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100471 if (!msghdr)
472 return -ENOMEM;
473
474 if (dest) {
475 msghdr->osa = *dest;
476 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
477 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
478 }
479 msghdr->flags = sendto_flags;
480 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
481 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
482 msghdr->hdr.msg_iov = &msghdr->iov[0];
483 msghdr->hdr.msg_iovlen = 1;
484
485 rc = iofd_txqueue_enqueue(iofd, msghdr);
486 if (rc < 0) {
487 iofd_msghdr_free(msghdr);
488 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
489 return rc;
490 }
491
492 return 0;
493}
494
Harald Welte1047ed72023-11-18 18:51:58 +0100495/*! ismo_io equivalent of the sendmsg(2) socket API call
496 *
497 * Appends the message to the internal transmit queue.
498 * If the function returns success (0), it will take ownership of the msgb and
499 * internally call msgb_free() after the write request completes.
500 * In case of an error the msgb needs to be freed by the caller.
501 * \param[in] iofd file descriptor to write to
502 * \param[in] msg message buffer to send; is used to fill msgh->iov[]
503 * \param[in] sendmsg_flags Flags to pass to the send call
504 * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty!
505 * \returns 0 in case of success; a negative value in case of error
506 */
507int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
508{
509 int rc;
510 struct iofd_msghdr *msghdr;
511
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100512 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
513 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
514 return -EINVAL;
515 }
516
Harald Welte1047ed72023-11-18 18:51:58 +0100517 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
518 if (OSMO_UNLIKELY(!iofd->io_ops.sendmsg_cb)) {
519 LOGPIO(iofd, LOGL_ERROR, "sendmsg_cb not set, Rejecting msgb\n");
520 return -EINVAL;
521 }
522
523 if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) {
524 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n",
525 msgh->msg_namelen, sizeof(msghdr->osa));
526 return -EINVAL;
527 }
528
529 if (OSMO_UNLIKELY(msgh->msg_iovlen)) {
530 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n");
531 return -EINVAL;
532 }
533
534 msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen);
535 if (!msghdr)
536 return -ENOMEM;
537
538 /* copy over optional address */
539 if (msgh->msg_name) {
540 memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen);
541 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
542 msghdr->hdr.msg_namelen = msgh->msg_namelen;
543 }
544
545 /* build iov from msgb */
546 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
547 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
548 msghdr->hdr.msg_iov = &msghdr->iov[0];
549 msghdr->hdr.msg_iovlen = 1;
550
551 /* copy over the cmsg from the msghdr */
552 if (msgh->msg_control && msgh->msg_controllen) {
553 msghdr->hdr.msg_control = msghdr->cmsg;
554 msghdr->hdr.msg_controllen = msgh->msg_controllen;
555 memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen);
556 }
557
558 /* copy over msg_flags */
559 msghdr->hdr.msg_flags = sendmsg_flags;
560
561 rc = iofd_txqueue_enqueue(iofd, msghdr);
562 if (rc < 0) {
563 iofd_msghdr_free(msghdr);
564 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
565 return rc;
566 }
567
568 return 0;
569}
570
Harald Welteb365b1d2024-02-23 16:08:49 +0100571static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops)
572{
573 switch (mode) {
574 case OSMO_IO_FD_MODE_READ_WRITE:
575 if (ops->recvfrom_cb || ops->sendto_cb)
576 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100577 if (ops->recvmsg_cb || ops->sendmsg_cb)
578 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100579 break;
580 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
581 if (ops->read_cb || ops->write_cb)
582 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100583 if (ops->recvmsg_cb || ops->sendmsg_cb)
584 return false;
585 break;
586 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
587 if (ops->recvfrom_cb || ops->sendto_cb)
588 return false;
589 if (ops->read_cb || ops->write_cb)
590 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100591 break;
592 default:
593 break;
594 }
595
596 return true;
597}
598
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200599/*! Allocate and setup a new iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100600 * \param[in] ctx the parent context from which to allocate
601 * \param[in] fd the underlying system file descriptor
602 * \param[in] name the name of the iofd
603 * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
604 * \param[in] ioops structure with read/write/send/recv callbacks
605 * \param[in] data user data pointer accessible by the ioops callbacks
606 * \returns The newly allocated osmo_io_fd struct or NULL on failure
607 */
608struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
609 const struct osmo_io_ops *ioops, void *data)
610{
Harald Weltec380f292023-11-18 19:54:46 +0100611 struct osmo_io_fd *iofd;
612
613 /* reject unsupported/unknown modes */
614 switch (mode) {
615 case OSMO_IO_FD_MODE_READ_WRITE:
616 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
Harald Welte1047ed72023-11-18 18:51:58 +0100617 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
Harald Weltec380f292023-11-18 19:54:46 +0100618 break;
619 default:
620 return NULL;
621 }
622
Harald Welteb365b1d2024-02-23 16:08:49 +0100623 if (!check_mode_callback_compat(mode, ioops))
624 return NULL;
625
Harald Weltec380f292023-11-18 19:54:46 +0100626 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100627 if (!iofd)
628 return NULL;
629
630 iofd->fd = fd;
631 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200632 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100633
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200634 if (name)
635 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100636
637 if (ioops)
638 iofd->io_ops = *ioops;
639
640 iofd->pending = NULL;
641
642 iofd->data = data;
643
644 iofd->msgb_alloc.ctx = ctx;
645 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
646 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
647
648 iofd->tx_queue.max_length = 32;
649 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
650
651 return iofd;
652}
653
Harald Welte1047ed72023-11-18 18:51:58 +0100654/*! Set the size of the control message buffer allocated when submitting recvmsg */
655int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
656{
657 if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG)
658 return -EINVAL;
659
660 iofd->cmsg_size = cmsg_size;
661 return 0;
662}
663
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200664/*! Register the fd with the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100665 *
666 * \param[in] iofd the iofd file descriptor
667 * \param[in] fd the system fd number that will be registeres. If negative will use the one already set.
668 * \returns zero on success, a negative value on error
669*/
670int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
671{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200672 int rc = 0;
673
Harald Welte8857f3b2022-11-18 13:54:44 +0100674 if (fd >= 0)
675 iofd->fd = fd;
Harald Welte8857f3b2022-11-18 13:54:44 +0100676
677 if (osmo_iofd_ops.register_fd)
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200678 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200679 if (rc)
680 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100681
Daniel Willmannf89162f2023-06-26 19:24:46 +0200682 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Harald Welteb365b1d2024-02-23 16:08:49 +0100683 if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) ||
Harald Welte1047ed72023-11-18 18:51:58 +0100684 (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) ||
685 (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) {
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200686 osmo_iofd_ops.read_enable(iofd);
Harald Welteb365b1d2024-02-23 16:08:49 +0100687 }
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200688
689 if (iofd->tx_queue.current_length > 0)
690 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200691
692 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100693}
694
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200695/*! Unregister the fd from the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100696 *
697 * \param[in] iofd the file descriptor
698 * \returns zero on success, a negative value on error
699 */
700int osmo_iofd_unregister(struct osmo_io_fd *iofd)
701{
702 if (osmo_iofd_ops.unregister_fd)
703 return osmo_iofd_ops.unregister_fd(iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200704 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100705
706 return 0;
707}
708
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200709/*! Get the number of messages in the tx queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100710 *
711 * \param[in] iofd the file descriptor
712 */
713unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
714{
715 return iofd->tx_queue.current_length;
716}
717
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200718/*! Clear the transmit queue of the the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100719 *
720 * This function frees all messages currently pending in the transmit queue
721 * \param[in] iofd the file descriptor
722 */
723void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
724{
725 struct iofd_msghdr *hdr;
726 while ((hdr = iofd_txqueue_dequeue(iofd))) {
727 msgb_free(hdr->msg);
728 iofd_msghdr_free(hdr);
729 }
730}
731
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200732/*! Free the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100733 *
734 * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so.
735 * The iofd will be closed before.
736 * \param[in] iofd the file descriptor
737 */
738void osmo_iofd_free(struct osmo_io_fd *iofd)
739{
740 if (!iofd)
741 return;
742
743 osmo_iofd_close(iofd);
744
Daniel Willmannf89162f2023-06-26 19:24:46 +0200745 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100746 talloc_free(iofd);
747 } else {
748 /* Prevent our parent context from freeing us prematurely */
749 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200750 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100751 }
752}
753
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200754/*! Close the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100755 *
756 * This function closes the underlying fd and clears any messages in the tx queue
757 * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()
758 * \param[in] iofd the file descriptor
759 * \ returns 0 on success, a negative value otherwise
760 */
761int osmo_iofd_close(struct osmo_io_fd *iofd)
762{
763 int rc = 0;
764
Daniel Willmannf89162f2023-06-26 19:24:46 +0200765 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100766 return rc;
767
Daniel Willmannf89162f2023-06-26 19:24:46 +0200768 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100769
770 /* Free pending msgs in tx queue */
771 osmo_iofd_txqueue_clear(iofd);
772 msgb_free(iofd->pending);
773
774 iofd->pending = NULL;
775
776 if (osmo_iofd_ops.close)
777 rc = osmo_iofd_ops.close(iofd);
778 iofd->fd = -1;
779 return rc;
780}
781
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200782/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200783 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100784 * \param[in] size the size of the msgb when receiving data
785 * \param[in] headroom the headroom of the msgb when receiving data
786 */
787void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
788{
789 iofd->msgb_alloc.headroom = headroom;
790 iofd->msgb_alloc.size = size;
791}
792
Daniel Willmanna9303f32023-07-07 11:20:48 +0200793/*! Set the maximum number of messages enqueued for sending.
794 * \param[in] iofd the file descriptor
795 * \param[in] size the maximum size of the transmit queue
796 */
797void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
798{
799 iofd->tx_queue.max_length = max_length;
800}
801
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200802/*! Get the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100803 * \param[in] iofd the file descriptor
804 * \returns the data that was previously set with \ref osmo_iofd_setup()
805 */
806void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
807{
808 return iofd->data;
809}
810
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200811/*! Set the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100812 * \param[in] iofd the file descriptor
813 * \param[in] data the data to set
814 */
815void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
816{
817 iofd->data = data;
818}
819
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200820/*! Get the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100821 * \param[in] iofd the file descriptor
822 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
823 */
824unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
825{
826 return iofd->priv_nr;
827}
828
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200829/*! Set the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100830 * \param[in] iofd the file descriptor
831 * \param[in] priv_nr the private number to set
832 */
833void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
834{
835 iofd->priv_nr = priv_nr;
836}
837
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200838/*! Get the underlying file descriptor from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100839 * \param[in] iofd the file descriptor
840 * \returns the underlying file descriptor number */
841int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
842{
843 return iofd->fd;
844}
845
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200846/*! Get the name of the file descriptor.
Harald Welte8857f3b2022-11-18 13:54:44 +0100847 * \param[in] iofd the file descriptor
848 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
849const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
850{
851 return iofd->name;
852}
853
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200854/*! Set the name of the file descriptor.
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200855 * \param[in] iofd the file descriptor
856 * \param[in] name the name to set on the file descriptor */
857void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
858{
859 osmo_talloc_replace_string(iofd, &iofd->name, name);
860}
861
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200862/*! Set the osmo_io_ops for an iofd.
arehbein0c374c62023-05-14 21:43:11 +0200863 * \param[in] iofd Target iofd file descriptor
864 * \param[in] ioops osmo_io_ops structure to be set */
Harald Welteb365b1d2024-02-23 16:08:49 +0100865int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
arehbein0c374c62023-05-14 21:43:11 +0200866{
Harald Welteb365b1d2024-02-23 16:08:49 +0100867 if (!check_mode_callback_compat(iofd->mode, ioops))
868 return -EINVAL;
869
arehbein0c374c62023-05-14 21:43:11 +0200870 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200871
872 switch (iofd->mode) {
873 case OSMO_IO_FD_MODE_READ_WRITE:
874 if (iofd->io_ops.read_cb)
875 osmo_iofd_ops.read_enable(iofd);
876 else
877 osmo_iofd_ops.read_disable(iofd);
878 break;
879 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
880 if (iofd->io_ops.recvfrom_cb)
881 osmo_iofd_ops.read_enable(iofd);
882 else
883 osmo_iofd_ops.read_disable(iofd);
884 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100885 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
886 if (iofd->io_ops.recvmsg_cb)
887 osmo_iofd_ops.read_enable(iofd);
888 else
889 osmo_iofd_ops.read_disable(iofd);
890 break;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200891 default:
892 OSMO_ASSERT(0);
893 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100894
895 return 0;
arehbein0c374c62023-05-14 21:43:11 +0200896}
897
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200898/*! Notify the user if/when the socket is connected.
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200899 * When the socket is connected the write_cb will be called.
900 * \param[in] iofd the file descriptor */
901void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
902{
Harald Welte1047ed72023-11-18 18:51:58 +0100903 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE ||
904 iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Andreas Eversberg848faf92024-02-09 12:38:17 +0100905 OSMO_ASSERT(osmo_iofd_ops.notify_connected);
906 osmo_iofd_ops.notify_connected(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200907}
908
909
Harald Welte8857f3b2022-11-18 13:54:44 +0100910#endif /* defined(__linux__) */