blob: 5a5e05c76c3744593a0eb1132b4b41c4b72157e4 [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);
Andreas Eversberga4ac5b82024-02-28 16:36:29 +0100155 return msgb_alloc_headroom_c(iofd, iofd->msgb_alloc.size + headroom, headroom, "osmo_io_msgb");
Harald Welte8857f3b2022-11-18 13:54:44 +0100156}
157
158/*! return the pending msgb in iofd or NULL if there is none*/
159struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd)
160{
161 struct msgb *msg = NULL;
162
163 msg = iofd->pending;
164 iofd->pending = NULL;
165
166 return msg;
167}
168
169/*! Return the pending msgb or allocate and return a new one */
170struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
171{
172 struct msgb *msg = NULL;
173
174 msg = iofd_msgb_pending(iofd);
175 if (!msg)
176 msg = iofd_msgb_alloc(iofd);
177
178 return msg;
179}
180
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200181/*! Enqueue a message to be sent.
Harald Welte8857f3b2022-11-18 13:54:44 +0100182 *
183 * Enqueues the message at the back of the queue provided there is enough space.
184 * \param[in] iofd the file descriptor
185 * \param[in] msghdr the message to enqueue
186 * \returns 0 if the message was enqueued succcessfully,
187 * -ENOSPC if the queue already contains the maximum number of messages
188 */
189int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
190{
191 if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length)
192 return -ENOSPC;
193
194 llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue);
195 iofd->tx_queue.current_length++;
196
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200197 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100198 osmo_iofd_ops.write_enable(iofd);
199
200 return 0;
201}
202
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200203/*! Enqueue a message at the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100204 *
205 * Used to enqueue a msgb from a partial send again. This function will always
206 * enqueue the message, even if the maximum number of messages is reached.
207 * \param[in] iofd the file descriptor
208 * \param[in] msghdr the message to enqueue
209 */
210void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
211{
212 llist_add(&msghdr->list, &iofd->tx_queue.msg_queue);
213 iofd->tx_queue.current_length++;
Daniel Willmanne4ecd992023-06-30 10:52:11 +0200214
215 if (iofd->tx_queue.current_length == 1 && !IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
216 osmo_iofd_ops.write_enable(iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100217}
218
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200219/*! Dequeue a message from the front.
Harald Welte8857f3b2022-11-18 13:54:44 +0100220 *
221 * \param[in] iofd the file descriptor
222 * \returns the msghdr from the front of the queue or NULL if the queue is empty
223 */
224struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
225{
226 struct llist_head *lh;
227
228 if (iofd->tx_queue.current_length == 0)
229 return NULL;
230
231 lh = iofd->tx_queue.msg_queue.next;
232
233 OSMO_ASSERT(lh);
234 iofd->tx_queue.current_length--;
235 llist_del(lh);
236
237 if (iofd->tx_queue.current_length == 0)
238 osmo_iofd_ops.write_disable(iofd);
239
240 return llist_entry(lh, struct iofd_msghdr, list);
241}
242
243/*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain
244 * one complete message.
245 * If there are bytes left over, *pending_out will point to a msgb with the remaining data.
246*/
247static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out)
248{
arehbeinc0aa4bd2023-06-16 22:31:32 +0200249 int extra_len, received_len;
Harald Welte8857f3b2022-11-18 13:54:44 +0100250 struct msgb *msg_pending;
251
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200252 /* Save the start of message before segmentation_cb (which could change it) */
253 uint8_t *data = msg->data;
254
arehbeinc0aa4bd2023-06-16 22:31:32 +0200255 received_len = msgb_length(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100256
257 if (!iofd->io_ops.segmentation_cb) {
258 *pending_out = NULL;
259 return IOFD_SEG_ACT_HANDLE_ONE;
260 }
261
arehbeinc0aa4bd2023-06-16 22:31:32 +0200262 int expected_len = iofd->io_ops.segmentation_cb(msg);
263 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200264 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200265 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200266 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200267 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
268 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100269 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200270 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100271 return IOFD_SEG_ACT_DEFER;
272 }
273
arehbeinc0aa4bd2023-06-16 22:31:32 +0200274 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200275 /* No segmentation needed, return the whole msgb */
276 if (extra_len == 0) {
277 *pending_out = NULL;
278 return IOFD_SEG_ACT_HANDLE_ONE;
279 /* segment is incomplete */
280 } else if (extra_len < 0) {
281 goto defer;
282 }
283
284 /* msgb contains more than one segment */
285 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100286 msg_pending = iofd_msgb_alloc(iofd);
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200287 memcpy(msgb_data(msg_pending), data + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200288 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100289 *pending_out = msg_pending;
290
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200291 /* Trim the original msgb to size. Don't use msgb_trim because we need to reference
292 * msg->data from before it might have been modified by the segmentation_cb(). */
Daniel Willmann7b59bab2023-07-07 11:17:59 +0200293 msg->tail = data + expected_len;
Daniel Willmann97d21442023-07-18 09:46:27 +0200294 msg->len = msg->tail - msg->data;
Harald Welte8857f3b2022-11-18 13:54:44 +0100295 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200296
297defer:
298 *pending_out = msg;
299 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100300}
301
302/*! Restore message boundaries on read() and pass individual messages to the read callback
303 */
304void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
305{
306 int res;
307 struct msgb *pending = NULL;
308
Harald Welteb365b1d2024-02-23 16:08:49 +0100309 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
310
Harald Welte8857f3b2022-11-18 13:54:44 +0100311 if (rc <= 0) {
312 iofd->io_ops.read_cb(iofd, rc, msg);
313 return;
314 }
315
316 do {
317 res = iofd_handle_segmentation(iofd, msg, &pending);
318 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
319 iofd->io_ops.read_cb(iofd, rc, msg);
320 if (res == IOFD_SEG_ACT_HANDLE_MORE)
321 msg = pending;
322 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
323
324 OSMO_ASSERT(iofd->pending == NULL);
325 iofd->pending = pending;
326}
327
Harald Welte987a86a2023-11-18 18:46:24 +0100328/*! completion handler: Called by osmo_io backend after a given I/O operation has completed
329 * \param[in] iofd I/O file-descriptor on which I/O has completed
330 * \param[in] msg message buffer containing data related to completed I/O
Andreas Eversberg76f76782024-02-14 14:33:10 +0100331 * \param[in] rc result code with read size or error (-errno)
Harald Welte987a86a2023-11-18 18:46:24 +0100332 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200333void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
334{
Daniel Willmann012d9042023-08-10 10:47:25 +0200335 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200336 switch (iofd->mode) {
337 case OSMO_IO_FD_MODE_READ_WRITE:
338 iofd_handle_segmented_read(iofd, msg, rc);
339 break;
340 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
341 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
342 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100343 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
344 iofd->io_ops.recvmsg_cb(iofd, rc, msg, &hdr->hdr);
345 break;
346 default:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200347 OSMO_ASSERT(false);
348 break;
349 }
350}
351
Daniel Willmann84611882023-11-21 10:17:00 +0100352/*! completion handler: Calld by osmo_io backend after a given I/O operation has completed
353 * \param[in] iofd I/O file-descriptor on which I/O has completed
354 * \param[in] rc return value of the I/O operation
355 * \param[in] msghdr serialized msghdr containing state of completed I/O
356 */
357void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
358{
359 struct msgb *msg = msghdr->msg;
360
361 /* Incomplete write */
362 if (rc > 0 && rc < msgb_length(msg)) {
363 /* Re-enqueue remaining data */
364 msgb_pull(msg, rc);
365 msghdr->iov[0].iov_len = msgb_length(msg);
366 iofd_txqueue_enqueue_front(iofd, msghdr);
367 return;
368 }
369
370 /* Reenqueue the complete msgb */
371 if (rc == -EAGAIN) {
372 iofd_txqueue_enqueue_front(iofd, msghdr);
373 return;
374 }
375
376 /* All other failure and success cases are handled here */
377 switch (msghdr->action) {
378 case IOFD_ACT_WRITE:
379 iofd->io_ops.write_cb(iofd, rc, msg);
380 break;
381 case IOFD_ACT_SENDTO:
382 iofd->io_ops.sendto_cb(iofd, rc, msg, &msghdr->osa);
383 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100384 case IOFD_ACT_SENDMSG:
385 iofd->io_ops.sendmsg_cb(iofd, rc, msg);
386 break;
Daniel Willmann84611882023-11-21 10:17:00 +0100387 default:
388 OSMO_ASSERT(0);
389 }
390
391 msgb_free(msghdr->msg);
392 iofd_msghdr_free(msghdr);
393}
394
Harald Welte8857f3b2022-11-18 13:54:44 +0100395/* Public functions */
396
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200397/*! Send a message through a connected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100398 *
399 * Appends the message to the internal transmit queue.
400 * If the function returns success (0) it will take ownership of the msgb and
401 * internally call msgb_free() after the write request completes.
402 * In case of an error the msgb needs to be freed by the caller.
403 * \param[in] iofd file descriptor to write to
404 * \param[in] msg message buffer to write
405 * \returns 0 in case of success; a negative value in case of error
406 */
407int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
408{
409 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200410
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100411 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
412 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
413 return -EINVAL;
414 }
415
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100416 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200417 if (OSMO_UNLIKELY(!iofd->io_ops.write_cb)) {
418 LOGPIO(iofd, LOGL_ERROR, "write_cb not set, Rejecting msgb\n");
419 return -EINVAL;
420 }
421
Harald Welte1047ed72023-11-18 18:51:58 +0100422 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100423 if (!msghdr)
424 return -ENOMEM;
425
Daniel Willmann92efac22023-08-01 09:55:13 +0200426 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100427 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
428 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
429 msghdr->hdr.msg_iov = &msghdr->iov[0];
430 msghdr->hdr.msg_iovlen = 1;
431
432 rc = iofd_txqueue_enqueue(iofd, msghdr);
433 if (rc < 0) {
434 iofd_msghdr_free(msghdr);
435 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
436 return rc;
437 }
438
439 return 0;
440}
441
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200442/*! Send a message through an unconnected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100443 *
444 * Appends the message to the internal transmit queue.
445 * If the function returns success (0), it will take ownership of the msgb and
446 * internally call msgb_free() after the write request completes.
447 * In case of an error the msgb needs to be freed by the caller.
448 * \param[in] iofd file descriptor to write to
449 * \param[in] msg message buffer to send
450 * \param[in] sendto_flags Flags to pass to the send call
451 * \param[in] dest destination address to send the message to
452 * \returns 0 in case of success; a negative value in case of error
453 */
454int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
455{
456 int rc;
457
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100458 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
459 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
460 return -EINVAL;
461 }
462
Harald Welte8857f3b2022-11-18 13:54:44 +0100463 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200464 if (OSMO_UNLIKELY(!iofd->io_ops.sendto_cb)) {
465 LOGPIO(iofd, LOGL_ERROR, "sendto_cb not set, Rejecting msgb\n");
466 return -EINVAL;
467 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100468
Harald Welte1047ed72023-11-18 18:51:58 +0100469 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg, 0);
Harald Welte8857f3b2022-11-18 13:54:44 +0100470 if (!msghdr)
471 return -ENOMEM;
472
473 if (dest) {
474 msghdr->osa = *dest;
475 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
476 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
477 }
478 msghdr->flags = sendto_flags;
479 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
480 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
481 msghdr->hdr.msg_iov = &msghdr->iov[0];
482 msghdr->hdr.msg_iovlen = 1;
483
484 rc = iofd_txqueue_enqueue(iofd, msghdr);
485 if (rc < 0) {
486 iofd_msghdr_free(msghdr);
487 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
488 return rc;
489 }
490
491 return 0;
492}
493
Harald Welte1047ed72023-11-18 18:51:58 +0100494/*! ismo_io equivalent of the sendmsg(2) socket API call
495 *
496 * Appends the message to the internal transmit queue.
497 * If the function returns success (0), it will take ownership of the msgb and
498 * internally call msgb_free() after the write request completes.
499 * In case of an error the msgb needs to be freed by the caller.
500 * \param[in] iofd file descriptor to write to
501 * \param[in] msg message buffer to send; is used to fill msgh->iov[]
502 * \param[in] sendmsg_flags Flags to pass to the send call
503 * \param[in] msgh 'struct msghdr' for name/control/flags. iov must be empty!
504 * \returns 0 in case of success; a negative value in case of error
505 */
506int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
507{
508 int rc;
509 struct iofd_msghdr *msghdr;
510
Andreas Eversberg2ce17da2024-02-09 14:36:30 +0100511 if (OSMO_UNLIKELY(msgb_length(msg) == 0)) {
512 LOGPIO(iofd, LOGL_ERROR, "Length is 0, rejecting msgb.\n");
513 return -EINVAL;
514 }
515
Harald Welte1047ed72023-11-18 18:51:58 +0100516 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
517 if (OSMO_UNLIKELY(!iofd->io_ops.sendmsg_cb)) {
518 LOGPIO(iofd, LOGL_ERROR, "sendmsg_cb not set, Rejecting msgb\n");
519 return -EINVAL;
520 }
521
522 if (OSMO_UNLIKELY(msgh->msg_namelen > sizeof(msghdr->osa))) {
523 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg msg_namelen (%u) > supported %zu bytes\n",
524 msgh->msg_namelen, sizeof(msghdr->osa));
525 return -EINVAL;
526 }
527
528 if (OSMO_UNLIKELY(msgh->msg_iovlen)) {
529 LOGPIO(iofd, LOGL_ERROR, "osmo_iofd_sendmsg must have all in 'struct msgb', not in 'msg_iov'\n");
530 return -EINVAL;
531 }
532
533 msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDMSG, msg, msgh->msg_controllen);
534 if (!msghdr)
535 return -ENOMEM;
536
537 /* copy over optional address */
538 if (msgh->msg_name) {
539 memcpy(&msghdr->osa, msgh->msg_name, msgh->msg_namelen);
540 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
541 msghdr->hdr.msg_namelen = msgh->msg_namelen;
542 }
543
544 /* build iov from msgb */
545 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
546 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
547 msghdr->hdr.msg_iov = &msghdr->iov[0];
548 msghdr->hdr.msg_iovlen = 1;
549
550 /* copy over the cmsg from the msghdr */
551 if (msgh->msg_control && msgh->msg_controllen) {
552 msghdr->hdr.msg_control = msghdr->cmsg;
553 msghdr->hdr.msg_controllen = msgh->msg_controllen;
554 memcpy(msghdr->cmsg, msgh->msg_control, msgh->msg_controllen);
555 }
556
557 /* copy over msg_flags */
558 msghdr->hdr.msg_flags = sendmsg_flags;
559
560 rc = iofd_txqueue_enqueue(iofd, msghdr);
561 if (rc < 0) {
562 iofd_msghdr_free(msghdr);
563 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
564 return rc;
565 }
566
567 return 0;
568}
569
Harald Welteb365b1d2024-02-23 16:08:49 +0100570static int check_mode_callback_compat(enum osmo_io_fd_mode mode, const struct osmo_io_ops *ops)
571{
572 switch (mode) {
573 case OSMO_IO_FD_MODE_READ_WRITE:
574 if (ops->recvfrom_cb || ops->sendto_cb)
575 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100576 if (ops->recvmsg_cb || ops->sendmsg_cb)
577 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100578 break;
579 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
580 if (ops->read_cb || ops->write_cb)
581 return false;
Harald Welte1047ed72023-11-18 18:51:58 +0100582 if (ops->recvmsg_cb || ops->sendmsg_cb)
583 return false;
584 break;
585 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
586 if (ops->recvfrom_cb || ops->sendto_cb)
587 return false;
588 if (ops->read_cb || ops->write_cb)
589 return false;
Harald Welteb365b1d2024-02-23 16:08:49 +0100590 break;
591 default:
592 break;
593 }
594
595 return true;
596}
597
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200598/*! Allocate and setup a new iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100599 * \param[in] ctx the parent context from which to allocate
600 * \param[in] fd the underlying system file descriptor
601 * \param[in] name the name of the iofd
602 * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
603 * \param[in] ioops structure with read/write/send/recv callbacks
604 * \param[in] data user data pointer accessible by the ioops callbacks
605 * \returns The newly allocated osmo_io_fd struct or NULL on failure
606 */
607struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
608 const struct osmo_io_ops *ioops, void *data)
609{
Harald Weltec380f292023-11-18 19:54:46 +0100610 struct osmo_io_fd *iofd;
611
612 /* reject unsupported/unknown modes */
613 switch (mode) {
614 case OSMO_IO_FD_MODE_READ_WRITE:
615 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
Harald Welte1047ed72023-11-18 18:51:58 +0100616 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
Harald Weltec380f292023-11-18 19:54:46 +0100617 break;
618 default:
619 return NULL;
620 }
621
Andreas Eversberg25d5bf62024-03-01 17:31:27 +0100622 if (ioops && !check_mode_callback_compat(mode, ioops))
Harald Welteb365b1d2024-02-23 16:08:49 +0100623 return NULL;
624
Harald Weltec380f292023-11-18 19:54:46 +0100625 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100626 if (!iofd)
627 return NULL;
628
629 iofd->fd = fd;
630 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200631 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100632
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200633 if (name)
634 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100635
636 if (ioops)
637 iofd->io_ops = *ioops;
638
639 iofd->pending = NULL;
640
641 iofd->data = data;
642
643 iofd->msgb_alloc.ctx = ctx;
644 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
645 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
646
647 iofd->tx_queue.max_length = 32;
648 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
649
650 return iofd;
651}
652
Harald Welte1047ed72023-11-18 18:51:58 +0100653/*! Set the size of the control message buffer allocated when submitting recvmsg */
654int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
655{
656 if (iofd->mode != OSMO_IO_FD_MODE_RECVMSG_SENDMSG)
657 return -EINVAL;
658
659 iofd->cmsg_size = cmsg_size;
660 return 0;
661}
662
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200663/*! Register the fd with the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100664 *
665 * \param[in] iofd the iofd file descriptor
666 * \param[in] fd the system fd number that will be registeres. If negative will use the one already set.
667 * \returns zero on success, a negative value on error
668*/
669int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
670{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200671 int rc = 0;
672
Harald Welte8857f3b2022-11-18 13:54:44 +0100673 if (fd >= 0)
674 iofd->fd = fd;
Harald Welte8857f3b2022-11-18 13:54:44 +0100675
676 if (osmo_iofd_ops.register_fd)
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200677 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200678 if (rc)
679 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100680
Daniel Willmannf89162f2023-06-26 19:24:46 +0200681 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Harald Welteb365b1d2024-02-23 16:08:49 +0100682 if ((iofd->mode == OSMO_IO_FD_MODE_READ_WRITE && iofd->io_ops.read_cb) ||
Harald Welte1047ed72023-11-18 18:51:58 +0100683 (iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO && iofd->io_ops.recvfrom_cb) ||
684 (iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG && iofd->io_ops.recvmsg_cb)) {
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200685 osmo_iofd_ops.read_enable(iofd);
Harald Welteb365b1d2024-02-23 16:08:49 +0100686 }
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200687
688 if (iofd->tx_queue.current_length > 0)
689 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200690
691 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100692}
693
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200694/*! Unregister the fd from the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100695 *
696 * \param[in] iofd the file descriptor
697 * \returns zero on success, a negative value on error
698 */
699int osmo_iofd_unregister(struct osmo_io_fd *iofd)
700{
701 if (osmo_iofd_ops.unregister_fd)
702 return osmo_iofd_ops.unregister_fd(iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200703 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100704
705 return 0;
706}
707
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200708/*! Get the number of messages in the tx queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100709 *
710 * \param[in] iofd the file descriptor
711 */
712unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
713{
714 return iofd->tx_queue.current_length;
715}
716
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200717/*! Clear the transmit queue of the the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100718 *
719 * This function frees all messages currently pending in the transmit queue
720 * \param[in] iofd the file descriptor
721 */
722void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
723{
724 struct iofd_msghdr *hdr;
725 while ((hdr = iofd_txqueue_dequeue(iofd))) {
726 msgb_free(hdr->msg);
727 iofd_msghdr_free(hdr);
728 }
729}
730
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200731/*! Free the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100732 *
733 * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so.
734 * The iofd will be closed before.
735 * \param[in] iofd the file descriptor
736 */
737void osmo_iofd_free(struct osmo_io_fd *iofd)
738{
739 if (!iofd)
740 return;
741
742 osmo_iofd_close(iofd);
743
Daniel Willmannf89162f2023-06-26 19:24:46 +0200744 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100745 talloc_free(iofd);
746 } else {
747 /* Prevent our parent context from freeing us prematurely */
748 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200749 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100750 }
751}
752
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200753/*! Close the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100754 *
755 * This function closes the underlying fd and clears any messages in the tx queue
756 * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()
757 * \param[in] iofd the file descriptor
758 * \ returns 0 on success, a negative value otherwise
759 */
760int osmo_iofd_close(struct osmo_io_fd *iofd)
761{
762 int rc = 0;
763
Daniel Willmannf89162f2023-06-26 19:24:46 +0200764 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100765 return rc;
766
Daniel Willmannf89162f2023-06-26 19:24:46 +0200767 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100768
769 /* Free pending msgs in tx queue */
770 osmo_iofd_txqueue_clear(iofd);
771 msgb_free(iofd->pending);
772
773 iofd->pending = NULL;
774
775 if (osmo_iofd_ops.close)
776 rc = osmo_iofd_ops.close(iofd);
777 iofd->fd = -1;
778 return rc;
779}
780
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200781/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200782 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100783 * \param[in] size the size of the msgb when receiving data
784 * \param[in] headroom the headroom of the msgb when receiving data
785 */
786void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
787{
788 iofd->msgb_alloc.headroom = headroom;
789 iofd->msgb_alloc.size = size;
790}
791
Daniel Willmanna9303f32023-07-07 11:20:48 +0200792/*! Set the maximum number of messages enqueued for sending.
793 * \param[in] iofd the file descriptor
794 * \param[in] size the maximum size of the transmit queue
795 */
796void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
797{
798 iofd->tx_queue.max_length = max_length;
799}
800
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200801/*! Get the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100802 * \param[in] iofd the file descriptor
803 * \returns the data that was previously set with \ref osmo_iofd_setup()
804 */
805void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
806{
807 return iofd->data;
808}
809
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200810/*! Set the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100811 * \param[in] iofd the file descriptor
812 * \param[in] data the data to set
813 */
814void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
815{
816 iofd->data = data;
817}
818
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200819/*! Get the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100820 * \param[in] iofd the file descriptor
821 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
822 */
823unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
824{
825 return iofd->priv_nr;
826}
827
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200828/*! Set the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100829 * \param[in] iofd the file descriptor
830 * \param[in] priv_nr the private number to set
831 */
832void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
833{
834 iofd->priv_nr = priv_nr;
835}
836
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200837/*! Get the underlying file descriptor from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100838 * \param[in] iofd the file descriptor
839 * \returns the underlying file descriptor number */
840int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
841{
842 return iofd->fd;
843}
844
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200845/*! Get the name of the file descriptor.
Harald Welte8857f3b2022-11-18 13:54:44 +0100846 * \param[in] iofd the file descriptor
847 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
848const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
849{
850 return iofd->name;
851}
852
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200853/*! Set the name of the file descriptor.
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200854 * \param[in] iofd the file descriptor
855 * \param[in] name the name to set on the file descriptor */
856void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
857{
858 osmo_talloc_replace_string(iofd, &iofd->name, name);
859}
860
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200861/*! Set the osmo_io_ops for an iofd.
arehbein0c374c62023-05-14 21:43:11 +0200862 * \param[in] iofd Target iofd file descriptor
863 * \param[in] ioops osmo_io_ops structure to be set */
Harald Welteb365b1d2024-02-23 16:08:49 +0100864int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
arehbein0c374c62023-05-14 21:43:11 +0200865{
Harald Welteb365b1d2024-02-23 16:08:49 +0100866 if (!check_mode_callback_compat(iofd->mode, ioops))
867 return -EINVAL;
868
arehbein0c374c62023-05-14 21:43:11 +0200869 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200870
871 switch (iofd->mode) {
872 case OSMO_IO_FD_MODE_READ_WRITE:
873 if (iofd->io_ops.read_cb)
874 osmo_iofd_ops.read_enable(iofd);
875 else
876 osmo_iofd_ops.read_disable(iofd);
877 break;
878 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
879 if (iofd->io_ops.recvfrom_cb)
880 osmo_iofd_ops.read_enable(iofd);
881 else
882 osmo_iofd_ops.read_disable(iofd);
883 break;
Harald Welte1047ed72023-11-18 18:51:58 +0100884 case OSMO_IO_FD_MODE_RECVMSG_SENDMSG:
885 if (iofd->io_ops.recvmsg_cb)
886 osmo_iofd_ops.read_enable(iofd);
887 else
888 osmo_iofd_ops.read_disable(iofd);
889 break;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200890 default:
891 OSMO_ASSERT(0);
892 }
Harald Welteb365b1d2024-02-23 16:08:49 +0100893
894 return 0;
arehbein0c374c62023-05-14 21:43:11 +0200895}
896
Harald Weltef574aea2024-02-23 12:07:03 +0100897/*! Get the osmo_io_ops for an iofd.
898 * \param[in] iofd Target iofd file descriptor
899 * \param[in] ioops caller-allocated osmo_io_ops structure to be filled */
900void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
901{
902 *ioops = iofd->io_ops;
903}
904
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200905/*! Notify the user if/when the socket is connected.
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200906 * When the socket is connected the write_cb will be called.
907 * \param[in] iofd the file descriptor */
908void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
909{
Harald Welte1047ed72023-11-18 18:51:58 +0100910 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE ||
911 iofd->mode == OSMO_IO_FD_MODE_RECVMSG_SENDMSG);
Andreas Eversberg848faf92024-02-09 12:38:17 +0100912 OSMO_ASSERT(osmo_iofd_ops.notify_connected);
913 osmo_iofd_ops.notify_connected(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200914}
915
916
Harald Welte8857f3b2022-11-18 13:54:44 +0100917#endif /* defined(__linux__) */