libosmocore 1.13.1.1-afb4 Osmocom core library

osmo_io_internal.h
Go to the documentation of this file.
1
3#pragma once
4
5#include "../config.h"
6
7#include <unistd.h>
8#include <stdbool.h>
9
10#include <osmocom/core/osmo_io.h>
11#include <osmocom/core/linuxlist.h>
12#include <osmocom/core/msgb.h>
13#include <osmocom/core/select.h>
14#include <osmocom/core/socket.h>
15
16#define OSMO_IO_DEFAULT_MSGB_SIZE 1024
17#define OSMO_IO_DEFAULT_MSGB_HEADROOM 128
18
19 extern const struct iofd_backend_ops iofd_poll_ops;
20#define OSMO_IO_BACKEND_DEFAULT "POLL"
21
22#if defined(HAVE_URING)
23 extern const struct iofd_backend_ops iofd_uring_ops;
24 void osmo_iofd_uring_constructor(void);
25 void osmo_iofd_uring_init(void);
26#endif
27
28 struct iofd_backend_ops {
29 int (*setup)(struct osmo_io_fd *iofd);
30 int (*register_fd)(struct osmo_io_fd *iofd);
31 int (*unregister_fd)(struct osmo_io_fd *iofd);
32 int (*close)(struct osmo_io_fd *iofd);
33 void (*write_enable)(struct osmo_io_fd *iofd);
34 void (*write_disable)(struct osmo_io_fd *iofd);
35 void (*read_enable)(struct osmo_io_fd *iofd);
36 void (*read_disable)(struct osmo_io_fd *iofd);
37 void (*notify_connected)(struct osmo_io_fd *iofd);
38};
39
40#define IOFD_FLAG_CLOSED (1<<0)
41#define IOFD_FLAG_IN_CALLBACK (1<<1)
42#define IOFD_FLAG_TO_FREE (1<<2)
43#define IOFD_FLAG_NOTIFY_CONNECTED (1<<3)
44#define IOFD_FLAG_FD_REGISTERED (1<<4)
45
46#define IOFD_FLAG_SET(iofd, flag) \
47 (iofd)->flags |= (flag)
48
49#define IOFD_FLAG_UNSET(iofd, flag) \
50 (iofd)->flags &= ~(flag)
51
52#define IOFD_FLAG_ISSET(iofd, flag) ((iofd)->flags & (flag))
53
54#define IOFD_MSGHDR_MAX_READ_SQES 32
55
56 struct osmo_io_fd {
58 struct llist_head list;
60 int fd;
62 enum osmo_io_fd_mode mode;
63
65 uint32_t flags;
66
68 char *name;
69
71 struct osmo_io_ops io_ops;
73 struct msgb *pending;
74
76 void *data;
78 unsigned int priv_nr;
79
81 size_t cmsg_size;
82
84 uint8_t io_read_buffers;
85
87 uint8_t io_write_buffers;
88
89 struct {
91 const void *ctx;
93 unsigned int size;
95 unsigned int headroom;
96 } msgb_alloc;
97
98 struct {
100 unsigned int max_length;
102 unsigned int current_length;
104 struct llist_head msg_queue;
105 } tx_queue;
106
107 union {
108 struct {
109 struct osmo_fd ofd;
110 } poll;
111 struct {
112 struct {
114 bool enabled;
116 uint8_t num_sqes;
118 void *msghdr[IOFD_MSGHDR_MAX_READ_SQES];
120 struct io_uring *ring;
122 uint8_t sqes_submitted;
123 } read;
124 struct {
126 bool enabled;
128 void *msghdr;
130 struct io_uring *ring;
131 } write;
132 /* TODO: index into array of registered fd's? */
133 /* osmo_fd for non-blocking connect handling */
134 struct osmo_fd connect_ofd;
135 } uring;
136 } u;
137};
138
139 enum iofd_msg_action {
140 IOFD_ACT_READ,
141 IOFD_ACT_WRITE,
142 IOFD_ACT_RECVFROM,
143 IOFD_ACT_SENDTO,
144 IOFD_ACT_RECVMSG,
145 IOFD_ACT_SENDMSG,
146};
147
148#define IOFD_MSGHDR_IO_BUFFERS 8
149
151 struct iofd_msghdr {
153 struct llist_head list;
154 enum iofd_msg_action action;
156 struct msghdr hdr;
158 struct osmo_sockaddr osa;
161 struct iovec iov[IOFD_MSGHDR_IO_BUFFERS];
163 int flags;
164
166 uint8_t io_len;
168 struct msgb *msg[IOFD_MSGHDR_IO_BUFFERS];
170 struct osmo_io_fd *iofd;
171
173 bool in_cancel_queue;
174
179 char _Alignas(struct cmsghdr) cmsg[0];
180};
181
182 enum iofd_seg_act {
183 IOFD_SEG_ACT_HANDLE_ONE,
184 IOFD_SEG_ACT_HANDLE_MORE,
185 IOFD_SEG_ACT_DEFER,
186};
187
188 struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size);
189 void iofd_msghdr_free(struct iofd_msghdr *msghdr);
190
191 struct msgb *iofd_msgb_alloc2(struct osmo_io_fd *iofd, size_t size);
192 struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
193
194 void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr);
195 void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr);
196
197 int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
198 void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
199 struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd);
iofd_txqueue_enqueue_front
void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
Enqueue a message at the front.
Definition: osmo_io.c:238
osmo_io_fd_mode
osmo_io_fd_mode
The mode of an osmo_io_fd determines if read/write, recvfrom/sendmsg or recvmsg/sendmsg semantics are...
Definition: osmo_io.h:66
iofd_msghdr_free
void iofd_msghdr_free(struct iofd_msghdr *msghdr)
Free the msghdr.
Definition: osmo_io.c:184
iofd_handle_send_completion
void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
completion handler: Internal function called by osmo_io_backend after a given I/O operation has compl...
Definition: osmo_io.c:517
iofd_msghdr_alloc
struct iofd_msghdr * iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size)
Allocate the msghdr.
Definition: osmo_io.c:142
iofd_msgb_alloc2
struct msgb * iofd_msgb_alloc2(struct osmo_io_fd *iofd, size_t size)
convenience wrapper to call msgb_alloc with parameters from osmo_io_fd (of given size)
Definition: osmo_io.c:192
iofd_msgb_alloc
struct msgb * iofd_msgb_alloc(struct osmo_io_fd *iofd)
convenience wrapper to call msgb_alloc with parameters from osmo_io_fd
Definition: osmo_io.c:201
iofd_txqueue_enqueue
int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
Enqueue a message to be sent.
Definition: osmo_io.c:214
iofd_txqueue_dequeue
struct iofd_msghdr * iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
Dequeue a message from the front.
Definition: osmo_io.c:252
iofd_handle_recv
void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr)
completion handler: Internal function called by osmo_io_backend after a given I/O operation has compl...
Definition: osmo_io.c:492
linuxlist.h
Simple doubly linked list implementation.
msgb.h
osmo_io.h
io(_uring) abstraction osmo fd compatibility
IOFD_MSGHDR_MAX_READ_SQES
#define IOFD_MSGHDR_MAX_READ_SQES
Definition: osmo_io_internal.h:54
IOFD_MSGHDR_IO_BUFFERS
#define IOFD_MSGHDR_IO_BUFFERS
Definition: osmo_io_internal.h:148
iofd_msg_action
iofd_msg_action
Definition: osmo_io_internal.h:139
IOFD_ACT_RECVFROM
@ IOFD_ACT_RECVFROM
Definition: osmo_io_internal.h:142
IOFD_ACT_RECVMSG
@ IOFD_ACT_RECVMSG
Definition: osmo_io_internal.h:144
IOFD_ACT_SENDTO
@ IOFD_ACT_SENDTO
Definition: osmo_io_internal.h:143
IOFD_ACT_READ
@ IOFD_ACT_READ
Definition: osmo_io_internal.h:140
IOFD_ACT_WRITE
@ IOFD_ACT_WRITE
Definition: osmo_io_internal.h:141
IOFD_ACT_SENDMSG
@ IOFD_ACT_SENDMSG
Definition: osmo_io_internal.h:145
iofd_seg_act
iofd_seg_act
Definition: osmo_io_internal.h:182
IOFD_SEG_ACT_DEFER
@ IOFD_SEG_ACT_DEFER
Definition: osmo_io_internal.h:185
IOFD_SEG_ACT_HANDLE_MORE
@ IOFD_SEG_ACT_HANDLE_MORE
Definition: osmo_io_internal.h:184
IOFD_SEG_ACT_HANDLE_ONE
@ IOFD_SEG_ACT_HANDLE_ONE
Definition: osmo_io_internal.h:183
iofd_poll_ops
const struct iofd_backend_ops iofd_poll_ops
Definition: osmo_io_poll.c:261
select.h
libmnl integration
socket.h
Osmocom socket convenience functions.
iofd_backend_ops
Definition: osmo_io_internal.h:28
iofd_backend_ops::read_enable
void(* read_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:35
iofd_backend_ops::write_enable
void(* write_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:33
iofd_backend_ops::read_disable
void(* read_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:36
iofd_backend_ops::write_disable
void(* write_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:34
iofd_backend_ops::close
int(* close)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:32
iofd_backend_ops::setup
int(* setup)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:29
iofd_backend_ops::register_fd
int(* register_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:30
iofd_backend_ops::notify_connected
void(* notify_connected)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:37
iofd_backend_ops::unregister_fd
int(* unregister_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:31
iofd_msghdr
serialized version of 'struct msghdr' employed by sendmsg/recvmsg
Definition: osmo_io_internal.h:151
iofd_msghdr::iofd
struct osmo_io_fd * iofd
I/O file descriptor on which we perform this I/O operation.
Definition: osmo_io_internal.h:170
iofd_msghdr::list
struct llist_head list
entry into osmo_io_fd.tx_queue.msg_queue
Definition: osmo_io_internal.h:153
iofd_msghdr::flags
int flags
flags we pass as argument to sendmsg / recvmsg
Definition: osmo_io_internal.h:163
iofd_msghdr::_Alignas
char _Alignas(struct cmsghdr) cmsg[0]
control message buffer for passing sctp_sndrcvinfo along.
iofd_msghdr::action
enum iofd_msg_action action
Definition: osmo_io_internal.h:154
iofd_msghdr::io_len
uint8_t io_len
current number of message-buffers that are stored
Definition: osmo_io_internal.h:166
iofd_msghdr::hdr
struct msghdr hdr
the 'struct msghdr' we are wrapping/ecapsulating here
Definition: osmo_io_internal.h:156
iofd_msghdr::osa
struct osmo_sockaddr osa
socket address of the remote peer
Definition: osmo_io_internal.h:158
iofd_msghdr::msg
struct msgb * msg[IOFD_MSGHDR_IO_BUFFERS]
message-buffer containing data for this I/O operation
Definition: osmo_io_internal.h:168
iofd_msghdr::in_cancel_queue
bool in_cancel_queue
msghdr is in the cancel_queue list
Definition: osmo_io_internal.h:173
iofd_msghdr::iov
struct iovec iov[IOFD_MSGHDR_IO_BUFFERS]
io-vector we need to pass as argument to sendmsg/recvmsg; is set up to point into msg below
Definition: osmo_io_internal.h:161
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
msgb
Osmocom message buffer.
Definition: msgb.h:31
osmo_fd
Structure representing a file dsecriptor.
Definition: select.h:31
osmo_io_fd
Definition: osmo_io_internal.h:56
osmo_io_fd::flags
uint32_t flags
flags to guard closing/freeing of iofd
Definition: osmo_io_internal.h:65
osmo_io_fd::current_length
unsigned int current_length
current length of write queue
Definition: osmo_io_internal.h:102
osmo_io_fd::pending
struct msgb * pending
Pending msgb to keep partial data during segmentation.
Definition: osmo_io_internal.h:73
osmo_io_fd::tx_queue
struct osmo_io_fd::@39 tx_queue
osmo_io_fd::u
union osmo_io_fd::@40 u
osmo_io_fd::num_sqes
uint8_t num_sqes
requested number of simultaniously submitted read SQEs
Definition: osmo_io_internal.h:116
osmo_io_fd::enabled
bool enabled
read is enabled, due to registration of callback function
Definition: osmo_io_internal.h:114
osmo_io_fd::msg_queue
struct llist_head msg_queue
actual linked list implementing the transmit queue
Definition: osmo_io_internal.h:104
osmo_io_fd::read
struct osmo_io_fd::@40::@42::@43 read
osmo_io_fd::write
struct osmo_io_fd::@40::@42::@44 write
osmo_io_fd::ring
struct io_uring * ring
ring the read SQEs have been submitted to
Definition: osmo_io_internal.h:120
osmo_io_fd::mode
enum osmo_io_fd_mode mode
type of read/write mode to use
Definition: osmo_io_internal.h:62
osmo_io_fd::msgb_alloc
struct osmo_io_fd::@38 msgb_alloc
osmo_io_fd::name
char * name
human-readable name to associte with fd
Definition: osmo_io_internal.h:68
osmo_io_fd::list
struct llist_head list
linked list for internal management
Definition: osmo_io_internal.h:58
osmo_io_fd::max_length
unsigned int max_length
maximum length of write queue
Definition: osmo_io_internal.h:100
osmo_io_fd::cmsg_size
size_t cmsg_size
size of iofd_msghdr.cmsg[] when allocated in recvmsg path
Definition: osmo_io_internal.h:81
osmo_io_fd::sqes_submitted
uint8_t sqes_submitted
current number of simultaneously submitted read SQEs
Definition: osmo_io_internal.h:122
osmo_io_fd::io_write_buffers
uint8_t io_write_buffers
maximum number of message-buffers per write operation
Definition: osmo_io_internal.h:87
osmo_io_fd::connect_ofd
struct osmo_fd connect_ofd
Definition: osmo_io_internal.h:134
osmo_io_fd::poll
struct osmo_io_fd::@40::@41 poll
osmo_io_fd::uring
struct osmo_io_fd::@40::@42 uring
osmo_io_fd::headroom
unsigned int headroom
headroom to allocate when allocating msgb's
Definition: osmo_io_internal.h:95
osmo_io_fd::ofd
struct osmo_fd ofd
Definition: osmo_io_internal.h:109
osmo_io_fd::size
unsigned int size
size of msgb to allocate (excluding headroom)
Definition: osmo_io_internal.h:93
osmo_io_fd::msghdr
void * msghdr[IOFD_MSGHDR_MAX_READ_SQES]
array of simultaneously submitted read SQEs
Definition: osmo_io_internal.h:118
osmo_io_fd::fd
int fd
actual operating-system level file decriptor
Definition: osmo_io_internal.h:60
osmo_io_fd::ctx
const void * ctx
talloc context from which to allocate msgb when reading
Definition: osmo_io_internal.h:91
osmo_io_fd::io_read_buffers
uint8_t io_read_buffers
maximum number of message-buffers per read operation
Definition: osmo_io_internal.h:84
osmo_io_fd::io_ops
struct osmo_io_ops io_ops
send/recv (msg) callback functions
Definition: osmo_io_internal.h:71
osmo_io_fd::data
void * data
data pointer passed through to call-back function
Definition: osmo_io_internal.h:76
osmo_io_fd::priv_nr
unsigned int priv_nr
private number, extending data
Definition: osmo_io_internal.h:78
osmo_io_ops
I/O operations (call-back functions) related to an osmo_io_fd.
Definition: osmo_io.h:105
osmo_sockaddr
Definition: socket.h:33