blob: 9c86e0511c0e3b9273ec3a31f6d06d3563409848 [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \file osmo_io_internal.h */
2
3#pragma once
4
5#include <unistd.h>
6#include <stdbool.h>
7
8#include <osmocom/core/osmo_io.h>
9#include <osmocom/core/linuxlist.h>
10#include <osmocom/core/msgb.h>
11#include <osmocom/core/select.h>
12#include <osmocom/core/socket.h>
13
14#include "../config.h"
15
16#define OSMO_IO_DEFAULT_MSGB_SIZE 1024
17#define OSMO_IO_DEFAULT_MSGB_HEADROOM 128
18
19extern const struct iofd_backend_ops iofd_poll_ops;
20#define OSMO_IO_BACKEND_DEFAULT "POLL"
21
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010022#if defined(HAVE_URING)
23extern const struct iofd_backend_ops iofd_uring_ops;
24#endif
25
Harald Welte8857f3b2022-11-18 13:54:44 +010026struct iofd_backend_ops {
27 int (*register_fd)(struct osmo_io_fd *iofd);
28 int (*unregister_fd)(struct osmo_io_fd *iofd);
29 int (*close)(struct osmo_io_fd *iofd);
30 void (*write_enable)(struct osmo_io_fd *iofd);
31 void (*write_disable)(struct osmo_io_fd *iofd);
32 void (*read_enable)(struct osmo_io_fd *iofd);
33 void (*read_disable)(struct osmo_io_fd *iofd);
Andreas Eversberg848faf92024-02-09 12:38:17 +010034 void (*notify_connected)(struct osmo_io_fd *iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +010035};
36
Daniel Willmannf89162f2023-06-26 19:24:46 +020037#define IOFD_FLAG_CLOSED (1<<0)
38#define IOFD_FLAG_IN_CALLBACK (1<<1)
39#define IOFD_FLAG_TO_FREE (1<<2)
40#define IOFD_FLAG_NOTIFY_CONNECTED (1<<3)
Andreas Eversberg848faf92024-02-09 12:38:17 +010041#define IOFD_FLAG_FD_REGISTERED (1<<4)
Daniel Willmannf89162f2023-06-26 19:24:46 +020042
43#define IOFD_FLAG_SET(iofd, flag) \
44 (iofd)->flags |= (flag)
45
46#define IOFD_FLAG_UNSET(iofd, flag) \
47 (iofd)->flags &= ~(flag)
48
49#define IOFD_FLAG_ISSET(iofd, flag) ((iofd)->flags & (flag))
50
Harald Welte8857f3b2022-11-18 13:54:44 +010051struct osmo_io_fd {
52 /*! linked list for internal management */
53 struct llist_head list;
54 /*! actual operating-system level file decriptor */
55 int fd;
56 /*! type of read/write mode to use */
57 enum osmo_io_fd_mode mode;
58
59 /*! flags to guard closing/freeing of iofd */
Daniel Willmannf89162f2023-06-26 19:24:46 +020060 uint32_t flags;
Harald Welte8857f3b2022-11-18 13:54:44 +010061
Harald Welte8857f3b2022-11-18 13:54:44 +010062 /*! human-readable name to associte with fd */
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +020063 char *name;
Harald Welte8857f3b2022-11-18 13:54:44 +010064
65 /*! send/recv (msg) callback functions */
66 struct osmo_io_ops io_ops;
67 /*! Pending msgb to keep partial data during segmentation */
68 struct msgb *pending;
69
70 /*! data pointer passed through to call-back function */
71 void *data;
72 /*! private number, extending \a data */
73 unsigned int priv_nr;
74
75 struct {
76 /*! talloc context from which to allocate msgb when reading */
77 const void *ctx;
78 /*! size of msgb to allocate (excluding headroom) */
79 unsigned int size;
80 /*! headroom to allocate when allocating msgb's */
81 unsigned int headroom;
82 } msgb_alloc;
83
84 struct {
85 /*! maximum length of write queue */
86 unsigned int max_length;
87 /*! current length of write queue */
88 unsigned int current_length;
89 /*! actual linked list implementing the transmit queue */
90 struct llist_head msg_queue;
91 } tx_queue;
92
93 union {
94 struct {
95 struct osmo_fd ofd;
96 } poll;
97 struct {
98 bool read_enabled;
Harald Welte8857f3b2022-11-18 13:54:44 +010099 bool write_enabled;
Daniel Willmannf91d2aa2023-01-04 18:20:55 +0100100 void *read_msghdr;
101 void *write_msghdr;
Harald Welte8857f3b2022-11-18 13:54:44 +0100102 /* TODO: index into array of registered fd's? */
103 } uring;
104 } u;
105};
106
107enum iofd_msg_action {
108 IOFD_ACT_READ,
109 IOFD_ACT_WRITE,
110 IOFD_ACT_RECVFROM,
111 IOFD_ACT_SENDTO,
112 // TODO: SCTP_*
113};
114
115
Harald Welte987a86a2023-11-18 18:46:24 +0100116/*! serialized version of 'struct msghdr' employed by sendmsg/recvmsg */
Harald Welte8857f3b2022-11-18 13:54:44 +0100117struct iofd_msghdr {
Harald Welte987a86a2023-11-18 18:46:24 +0100118 /*! entry into osmo_io_fd.tx_queue.msg_queue */
Harald Welte8857f3b2022-11-18 13:54:44 +0100119 struct llist_head list;
120 enum iofd_msg_action action;
Harald Welte987a86a2023-11-18 18:46:24 +0100121 /*! the 'struct msghdr' we are wrapping/ecapsulating here */
Harald Welte8857f3b2022-11-18 13:54:44 +0100122 struct msghdr hdr;
Harald Welte987a86a2023-11-18 18:46:24 +0100123 /*! socket address of the remote peer */
Harald Welte8857f3b2022-11-18 13:54:44 +0100124 struct osmo_sockaddr osa;
Harald Welte987a86a2023-11-18 18:46:24 +0100125 /*! io-vector we need to pass as argument to sendmsg/recvmsg; is set up
126 * to point into msg below */
Harald Welte8857f3b2022-11-18 13:54:44 +0100127 struct iovec iov[1];
Harald Welte987a86a2023-11-18 18:46:24 +0100128 /*! flags we pass as argument to sendmsg / recvmsg */
Harald Welte8857f3b2022-11-18 13:54:44 +0100129 int flags;
130
Harald Welte987a86a2023-11-18 18:46:24 +0100131 /*! message-buffer containing data for this I/O operation */
Harald Welte8857f3b2022-11-18 13:54:44 +0100132 struct msgb *msg;
Harald Welte987a86a2023-11-18 18:46:24 +0100133 /*! I/O file descriptor on which we perform this I/O operation */
Harald Welte8857f3b2022-11-18 13:54:44 +0100134 struct osmo_io_fd *iofd;
135};
136
137enum iofd_seg_act {
138 IOFD_SEG_ACT_HANDLE_ONE,
139 IOFD_SEG_ACT_HANDLE_MORE,
140 IOFD_SEG_ACT_DEFER,
141};
142
143struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg);
144void iofd_msghdr_free(struct iofd_msghdr *msghdr);
145
146struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
147struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd);
148struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd);
149
Daniel Willmann2b34e922023-08-23 18:02:13 +0200150void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr);
Daniel Willmann84611882023-11-21 10:17:00 +0100151void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr);
Harald Welte8857f3b2022-11-18 13:54:44 +0100152void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
153
154int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
155void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
156struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd);