blob: 8507f464f5f724fdcc778c59f14bba026596fb8a [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \file osmo_io.c
2 * New osmocom async I/O API.
3 *
4 * (C) 2022 by Harald Welte <laforge@osmocom.org>
5 * (C) 2022-2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
6 * 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
108 * \returns the newly allocated msghdr or NULL in case of error */
109struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg)
110{
Daniel Willmann012d9042023-08-10 10:47:25 +0200111 bool free_msg = false;
112 struct iofd_msghdr *hdr;
113
Harald Welte8857f3b2022-11-18 13:54:44 +0100114 if (!msg) {
115 msg = iofd_msgb_alloc(iofd);
Daniel Willmann012d9042023-08-10 10:47:25 +0200116 if (!msg)
Harald Welte8857f3b2022-11-18 13:54:44 +0100117 return NULL;
Daniel Willmann012d9042023-08-10 10:47:25 +0200118 free_msg = true;
Daniel Willmannf0833822023-07-27 18:00:32 +0200119 } else {
Daniel Willmann012d9042023-08-10 10:47:25 +0200120 talloc_steal(iofd, msg);
121 }
122
Daniel Willmannf39c23f2023-08-30 17:07:59 +0200123 hdr = talloc_zero(iofd, struct iofd_msghdr);
Daniel Willmann012d9042023-08-10 10:47:25 +0200124 if (!hdr) {
125 if (free_msg)
126 talloc_free(msg);
127 return NULL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100128 }
129
130 hdr->action = action;
131 hdr->iofd = iofd;
132 hdr->msg = msg;
133
134 return hdr;
135}
136
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200137/*! Free the msghdr.
Harald Welte8857f3b2022-11-18 13:54:44 +0100138 * \param[in] msghdr the msghdr to free
139 */
140void iofd_msghdr_free(struct iofd_msghdr *msghdr)
141{
142 /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or
143 * or passed on to the read callback which takes ownership. */
144 talloc_free(msghdr);
145}
146
147/*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */
148struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd)
149{
150 uint16_t headroom = iofd->msgb_alloc.headroom;
151
152 OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom);
Daniel Willmann012d9042023-08-10 10:47:25 +0200153 return msgb_alloc_headroom_c(iofd,
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200154 iofd->msgb_alloc.size + headroom, headroom,
155 iofd->name ? : "iofd_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
309 if (rc <= 0) {
310 iofd->io_ops.read_cb(iofd, rc, msg);
311 return;
312 }
313
314 do {
315 res = iofd_handle_segmentation(iofd, msg, &pending);
316 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
317 iofd->io_ops.read_cb(iofd, rc, msg);
318 if (res == IOFD_SEG_ACT_HANDLE_MORE)
319 msg = pending;
320 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
321
322 OSMO_ASSERT(iofd->pending == NULL);
323 iofd->pending = pending;
324}
325
Harald Welte987a86a2023-11-18 18:46:24 +0100326/*! completion handler: Called by osmo_io backend after a given I/O operation has completed
327 * \param[in] iofd I/O file-descriptor on which I/O has completed
328 * \param[in] msg message buffer containing data related to completed I/O
329 * \param[in] hdr serialized msghdr containing state of completed I/O */
Daniel Willmann2b34e922023-08-23 18:02:13 +0200330void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *hdr)
331{
Daniel Willmann012d9042023-08-10 10:47:25 +0200332 talloc_steal(iofd->msgb_alloc.ctx, msg);
Daniel Willmann2b34e922023-08-23 18:02:13 +0200333 switch (iofd->mode) {
334 case OSMO_IO_FD_MODE_READ_WRITE:
335 iofd_handle_segmented_read(iofd, msg, rc);
336 break;
337 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
338 iofd->io_ops.recvfrom_cb(iofd, rc, msg, &hdr->osa);
339 break;
Harald Welte38d81702023-11-18 19:52:09 +0100340 case OSMO_IO_FD_MODE_SCTP_RECVMSG_SEND:
Daniel Willmann2b34e922023-08-23 18:02:13 +0200341 /* TODO Implement */
342 OSMO_ASSERT(false);
343 break;
344 }
345}
346
Harald Welte8857f3b2022-11-18 13:54:44 +0100347/* Public functions */
348
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200349/*! Send a message through a connected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100350 *
351 * Appends the message to the internal transmit queue.
352 * If the function returns success (0) it will take ownership of the msgb and
353 * internally call msgb_free() after the write request completes.
354 * In case of an error the msgb needs to be freed by the caller.
355 * \param[in] iofd file descriptor to write to
356 * \param[in] msg message buffer to write
357 * \returns 0 in case of success; a negative value in case of error
358 */
359int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
360{
361 int rc;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200362
Daniel Willmannafdfc6a2023-11-21 10:10:37 +0100363 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200364 if (OSMO_UNLIKELY(!iofd->io_ops.write_cb)) {
365 LOGPIO(iofd, LOGL_ERROR, "write_cb not set, Rejecting msgb\n");
366 return -EINVAL;
367 }
368
Harald Welte8857f3b2022-11-18 13:54:44 +0100369 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg);
370 if (!msghdr)
371 return -ENOMEM;
372
Daniel Willmann92efac22023-08-01 09:55:13 +0200373 msghdr->flags = MSG_NOSIGNAL;
Harald Welte8857f3b2022-11-18 13:54:44 +0100374 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
375 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
376 msghdr->hdr.msg_iov = &msghdr->iov[0];
377 msghdr->hdr.msg_iovlen = 1;
378
379 rc = iofd_txqueue_enqueue(iofd, msghdr);
380 if (rc < 0) {
381 iofd_msghdr_free(msghdr);
382 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
383 return rc;
384 }
385
386 return 0;
387}
388
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200389/*! Send a message through an unconnected socket.
Harald Welte8857f3b2022-11-18 13:54:44 +0100390 *
391 * Appends the message to the internal transmit queue.
392 * If the function returns success (0), it will take ownership of the msgb and
393 * internally call msgb_free() after the write request completes.
394 * In case of an error the msgb needs to be freed by the caller.
395 * \param[in] iofd file descriptor to write to
396 * \param[in] msg message buffer to send
397 * \param[in] sendto_flags Flags to pass to the send call
398 * \param[in] dest destination address to send the message to
399 * \returns 0 in case of success; a negative value in case of error
400 */
401int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
402{
403 int rc;
404
405 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200406 if (OSMO_UNLIKELY(!iofd->io_ops.sendto_cb)) {
407 LOGPIO(iofd, LOGL_ERROR, "sendto_cb not set, Rejecting msgb\n");
408 return -EINVAL;
409 }
Harald Welte8857f3b2022-11-18 13:54:44 +0100410
411 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg);
412 if (!msghdr)
413 return -ENOMEM;
414
415 if (dest) {
416 msghdr->osa = *dest;
417 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
418 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
419 }
420 msghdr->flags = sendto_flags;
421 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
422 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
423 msghdr->hdr.msg_iov = &msghdr->iov[0];
424 msghdr->hdr.msg_iovlen = 1;
425
426 rc = iofd_txqueue_enqueue(iofd, msghdr);
427 if (rc < 0) {
428 iofd_msghdr_free(msghdr);
429 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
430 return rc;
431 }
432
433 return 0;
434}
435
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200436/*! Allocate and setup a new iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100437 * \param[in] ctx the parent context from which to allocate
438 * \param[in] fd the underlying system file descriptor
439 * \param[in] name the name of the iofd
440 * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
441 * \param[in] ioops structure with read/write/send/recv callbacks
442 * \param[in] data user data pointer accessible by the ioops callbacks
443 * \returns The newly allocated osmo_io_fd struct or NULL on failure
444 */
445struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
446 const struct osmo_io_ops *ioops, void *data)
447{
Harald Weltec380f292023-11-18 19:54:46 +0100448 struct osmo_io_fd *iofd;
449
450 /* reject unsupported/unknown modes */
451 switch (mode) {
452 case OSMO_IO_FD_MODE_READ_WRITE:
453 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
454 break;
455 default:
456 return NULL;
457 }
458
459 iofd = talloc_zero(ctx, struct osmo_io_fd);
Harald Welte8857f3b2022-11-18 13:54:44 +0100460 if (!iofd)
461 return NULL;
462
463 iofd->fd = fd;
464 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200465 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100466
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200467 if (name)
468 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100469
470 if (ioops)
471 iofd->io_ops = *ioops;
472
473 iofd->pending = NULL;
474
475 iofd->data = data;
476
477 iofd->msgb_alloc.ctx = ctx;
478 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
479 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
480
481 iofd->tx_queue.max_length = 32;
482 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
483
484 return iofd;
485}
486
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200487/*! Register the fd with the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100488 *
489 * \param[in] iofd the iofd file descriptor
490 * \param[in] fd the system fd number that will be registeres. If negative will use the one already set.
491 * \returns zero on success, a negative value on error
492*/
493int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
494{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200495 int rc = 0;
496
Harald Welte8857f3b2022-11-18 13:54:44 +0100497 if (fd >= 0)
498 iofd->fd = fd;
Harald Welte8857f3b2022-11-18 13:54:44 +0100499
500 if (osmo_iofd_ops.register_fd)
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200501 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200502 if (rc)
503 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100504
Daniel Willmannf89162f2023-06-26 19:24:46 +0200505 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200506 if (iofd->io_ops.read_cb)
507 osmo_iofd_ops.read_enable(iofd);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200508
509 if (iofd->tx_queue.current_length > 0)
510 osmo_iofd_ops.write_enable(iofd);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200511
512 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100513}
514
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200515/*! Unregister the fd from the underlying backend.
Harald Welte8857f3b2022-11-18 13:54:44 +0100516 *
517 * \param[in] iofd the file descriptor
518 * \returns zero on success, a negative value on error
519 */
520int osmo_iofd_unregister(struct osmo_io_fd *iofd)
521{
522 if (osmo_iofd_ops.unregister_fd)
523 return osmo_iofd_ops.unregister_fd(iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200524 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100525
526 return 0;
527}
528
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200529/*! Get the number of messages in the tx queue.
Harald Welte8857f3b2022-11-18 13:54:44 +0100530 *
531 * \param[in] iofd the file descriptor
532 */
533unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
534{
535 return iofd->tx_queue.current_length;
536}
537
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200538/*! Clear the transmit queue of the the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100539 *
540 * This function frees all messages currently pending in the transmit queue
541 * \param[in] iofd the file descriptor
542 */
543void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
544{
545 struct iofd_msghdr *hdr;
546 while ((hdr = iofd_txqueue_dequeue(iofd))) {
547 msgb_free(hdr->msg);
548 iofd_msghdr_free(hdr);
549 }
550}
551
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200552/*! Free the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100553 *
554 * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so.
555 * The iofd will be closed before.
556 * \param[in] iofd the file descriptor
557 */
558void osmo_iofd_free(struct osmo_io_fd *iofd)
559{
560 if (!iofd)
561 return;
562
563 osmo_iofd_close(iofd);
564
Daniel Willmannf89162f2023-06-26 19:24:46 +0200565 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100566 talloc_free(iofd);
567 } else {
568 /* Prevent our parent context from freeing us prematurely */
569 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200570 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100571 }
572}
573
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200574/*! Close the iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100575 *
576 * This function closes the underlying fd and clears any messages in the tx queue
577 * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()
578 * \param[in] iofd the file descriptor
579 * \ returns 0 on success, a negative value otherwise
580 */
581int osmo_iofd_close(struct osmo_io_fd *iofd)
582{
583 int rc = 0;
584
Daniel Willmannf89162f2023-06-26 19:24:46 +0200585 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100586 return rc;
587
Daniel Willmannf89162f2023-06-26 19:24:46 +0200588 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100589
590 /* Free pending msgs in tx queue */
591 osmo_iofd_txqueue_clear(iofd);
592 msgb_free(iofd->pending);
593
594 iofd->pending = NULL;
595
596 if (osmo_iofd_ops.close)
597 rc = osmo_iofd_ops.close(iofd);
598 iofd->fd = -1;
599 return rc;
600}
601
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200602/*! Set the size and headroom of the msgb allocated when receiving messages.
Daniel Willmann4731e712023-07-07 11:21:15 +0200603 * \param[in] iofd the file descriptor
Harald Welte8857f3b2022-11-18 13:54:44 +0100604 * \param[in] size the size of the msgb when receiving data
605 * \param[in] headroom the headroom of the msgb when receiving data
606 */
607void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
608{
609 iofd->msgb_alloc.headroom = headroom;
610 iofd->msgb_alloc.size = size;
611}
612
Daniel Willmanna9303f32023-07-07 11:20:48 +0200613/*! Set the maximum number of messages enqueued for sending.
614 * \param[in] iofd the file descriptor
615 * \param[in] size the maximum size of the transmit queue
616 */
617void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int max_length)
618{
619 iofd->tx_queue.max_length = max_length;
620}
621
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200622/*! Get the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100623 * \param[in] iofd the file descriptor
624 * \returns the data that was previously set with \ref osmo_iofd_setup()
625 */
626void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
627{
628 return iofd->data;
629}
630
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200631/*! Set the associated user-data from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100632 * \param[in] iofd the file descriptor
633 * \param[in] data the data to set
634 */
635void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
636{
637 iofd->data = data;
638}
639
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200640/*! Get the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100641 * \param[in] iofd the file descriptor
642 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
643 */
644unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
645{
646 return iofd->priv_nr;
647}
648
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200649/*! Set the private number from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100650 * \param[in] iofd the file descriptor
651 * \param[in] priv_nr the private number to set
652 */
653void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
654{
655 iofd->priv_nr = priv_nr;
656}
657
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200658/*! Get the underlying file descriptor from an iofd.
Harald Welte8857f3b2022-11-18 13:54:44 +0100659 * \param[in] iofd the file descriptor
660 * \returns the underlying file descriptor number */
661int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
662{
663 return iofd->fd;
664}
665
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200666/*! Get the name of the file descriptor.
Harald Welte8857f3b2022-11-18 13:54:44 +0100667 * \param[in] iofd the file descriptor
668 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
669const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
670{
671 return iofd->name;
672}
673
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200674/*! Set the name of the file descriptor.
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200675 * \param[in] iofd the file descriptor
676 * \param[in] name the name to set on the file descriptor */
677void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
678{
679 osmo_talloc_replace_string(iofd, &iofd->name, name);
680}
681
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200682/*! Set the osmo_io_ops for an iofd.
arehbein0c374c62023-05-14 21:43:11 +0200683 * \param[in] iofd Target iofd file descriptor
684 * \param[in] ioops osmo_io_ops structure to be set */
685void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
686{
687 iofd->io_ops = *ioops;
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200688
689 switch (iofd->mode) {
690 case OSMO_IO_FD_MODE_READ_WRITE:
691 if (iofd->io_ops.read_cb)
692 osmo_iofd_ops.read_enable(iofd);
693 else
694 osmo_iofd_ops.read_disable(iofd);
695 break;
696 case OSMO_IO_FD_MODE_RECVFROM_SENDTO:
697 if (iofd->io_ops.recvfrom_cb)
698 osmo_iofd_ops.read_enable(iofd);
699 else
700 osmo_iofd_ops.read_disable(iofd);
701 break;
Harald Welte38d81702023-11-18 19:52:09 +0100702 case OSMO_IO_FD_MODE_SCTP_RECVMSG_SEND:
Daniel Willmann2386e9a2023-09-28 15:28:13 +0200703 default:
704 OSMO_ASSERT(0);
705 }
arehbein0c374c62023-05-14 21:43:11 +0200706}
707
Daniel Willmannd0d9ecb2023-07-12 11:55:52 +0200708/*! Notify the user if/when the socket is connected.
Daniel Willmanne2a8dc42023-06-30 10:51:53 +0200709 * When the socket is connected the write_cb will be called.
710 * \param[in] iofd the file descriptor */
711void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
712{
713 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
714 osmo_iofd_ops.write_enable(iofd);
715}
716
717
Harald Welte8857f3b2022-11-18 13:54:44 +0100718#endif /* defined(__linux__) */