libosmocore 1.13.1.1-afb4 Osmocom core library

osmo_io.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <osmocom/core/linuxlist.h>
8#include <osmocom/core/logging.h>
9#include <osmocom/core/msgb.h>
10#include <osmocom/core/socket.h>
11#include <osmocom/core/utils.h>
12
13/* struct msghdr is defined in sys/socket.h but that header may not be available.
14 * We only really need a forward declaration here: */
15 struct msghdr;
16
59#define LOGPIO(iofd, level, fmt, args...) \
60 LOGP(DLIO, level, "iofd(%s) " fmt, iofd->name, ## args)
61
62 struct osmo_io_fd;
63
66 enum osmo_io_fd_mode {
68 OSMO_IO_FD_MODE_READ_WRITE,
70 OSMO_IO_FD_MODE_RECVFROM_SENDTO,
72 OSMO_IO_FD_MODE_RECVMSG_SENDMSG,
73};
74
76 enum osmo_io_op {
78 OSMO_IO_OP_READ,
80 OSMO_IO_OP_WRITE,
81};
82
85 enum osmo_io_backend {
87 OSMO_IO_BACKEND_POLL,
89 OSMO_IO_BACKEND_IO_URING,
90};
91
92 enum osmo_io_backend osmo_io_get_backend(void);
93
94 extern const struct value_string osmo_io_backend_names[];
96 static inline const char *osmo_io_backend_name(enum osmo_io_backend val)
97{ return get_value_string(osmo_io_backend_names, val); }
98
99 extern const struct value_string osmo_iofd_mode_names[];
101 static inline const char *osmo_iofd_mode_name(enum osmo_io_fd_mode val)
102{ return get_value_string(osmo_iofd_mode_names, val); }
103
105 struct osmo_io_ops {
106 /* mode OSMO_IO_FD_MODE_READ_WRITE: */
107 struct {
118 void (*read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
119
126 void (*write_cb)(struct osmo_io_fd *iofd, int res,
127 struct msgb *msg);
128
148 int (*segmentation_cb)(struct msgb *msg);
149
163 int (*segmentation_cb2)(struct osmo_io_fd *iofd, struct msgb *msg);
164 };
165
166 /* mode OSMO_IO_FD_MODE_RECVFROM_SENDTO: */
167 struct {
175 void (*recvfrom_cb)(struct osmo_io_fd *iofd, int res,
176 struct msgb *msg,
177 const struct osmo_sockaddr *saddr);
185 void (*sendto_cb)(struct osmo_io_fd *iofd, int res,
186 struct msgb *msg,
187 const struct osmo_sockaddr *daddr);
188 };
189
190 /* mode OSMO_IO_FD_MODE_RECVMSG_SENDMSG: */
191 struct {
200 void (*recvmsg_cb)(struct osmo_io_fd *iofd, int res,
201 struct msgb *msg, const struct msghdr *msgh);
208 void (*sendmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
209 };
210};
211
212 void osmo_iofd_init(void);
213
214 struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name,
215 enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data);
216 int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size);
217 int osmo_iofd_set_io_buffers(struct osmo_io_fd *iofd, enum osmo_io_op op, uint8_t buffers);
218 int osmo_iofd_set_sqes(struct osmo_io_fd *iofd, enum osmo_io_op op, uint8_t sqes);
219 int osmo_iofd_register(struct osmo_io_fd *iofd, int fd);
220 int osmo_iofd_unregister(struct osmo_io_fd *iofd);
221 unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd);
222 void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd);
223 int osmo_iofd_close(struct osmo_io_fd *iofd);
224 void osmo_iofd_free(struct osmo_io_fd *iofd);
225
226 void osmo_iofd_notify_connected(struct osmo_io_fd *iofd);
227
228 int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg);
229 int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags,
230 const struct osmo_sockaddr *dest);
231 int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags,
232 const struct msghdr *msgh);
233
234 void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom);
235 void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int size);
236 unsigned int osmo_iofd_get_txqueue_max_length(const struct osmo_io_fd *iofd);
237 void *osmo_iofd_get_data(const struct osmo_io_fd *iofd);
238 void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data);
239
240 unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd);
241 void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr);
242
243 int osmo_iofd_get_fd(const struct osmo_io_fd *iofd);
244 const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd);
245 void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name);
246 void osmo_iofd_set_name_f(struct osmo_io_fd *iofd, const char *fmt, ...);
247
248 int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops);
249 void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops);
250
name
const char * name
res
uint8_t res[16]
data
uint8_t data[0]
osmo_iofd_set_data
void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
Set the associated user-data from an osmo_io_fd.
Definition: osmo_io.c:1192
osmo_iofd_write_msgb
int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
Write a message to a file descriptor / connected socket.
Definition: osmo_io.c:606
osmo_iofd_set_sqes
int osmo_iofd_set_sqes(struct osmo_io_fd *iofd, enum osmo_io_op op, uint8_t sqes)
Set the number of SQEs that are submitted to an io_unring before completion is received.
Definition: osmo_io.c:963
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
osmo_iofd_set_io_buffers
int osmo_iofd_set_io_buffers(struct osmo_io_fd *iofd, enum osmo_io_op op, uint8_t buffers)
Set the number of buffers that can be used in a single read or write operation.
Definition: osmo_io.c:928
osmo_iofd_get_ioops
void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
Retrieve the osmo_io_ops for an iofd.
Definition: osmo_io.c:1307
osmo_iofd_set_ioops
int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
Set the osmo_io_ops calbacks for an osmo_io_fd.
Definition: osmo_io.c:1268
osmo_iofd_mode_name
static const char * osmo_iofd_mode_name(enum osmo_io_fd_mode val)
return the string name of an osmo_io_mode
Definition: osmo_io.h:101
osmo_iofd_unregister
int osmo_iofd_unregister(struct osmo_io_fd *iofd)
Unregister the given osmo_io_fd from osmo_io.
Definition: osmo_io.c:1046
osmo_io_backend_name
static const char * osmo_io_backend_name(enum osmo_io_backend val)
return the string name of an osmo_io_backend
Definition: osmo_io.h:96
osmo_iofd_notify_connected
void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
Request notification of the user if/when a client socket is connected.
Definition: osmo_io.c:1331
osmo_iofd_set_priv_nr
void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
Set the private number of an osmo_io_fd.
Definition: osmo_io.c:1213
osmo_io_backend
osmo_io_backend
The back-end used by osmo_io.
Definition: osmo_io.h:85
osmo_iofd_set_txqueue_max_length
void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int size)
Set the maximum number of messages enqueued for sending.
Definition: osmo_io.c:1156
osmo_iofd_txqueue_clear
void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
Clear the transmit queue of the given osmo_io_fd.
Definition: osmo_io.c:1072
osmo_iofd_set_name
void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
Set the human-readable name of the file descriptor.
Definition: osmo_io.c:1238
osmo_iofd_get_fd
int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
Retrieve the underlying file descriptor from an osmo_io_fd.
Definition: osmo_io.c:1221
osmo_iofd_free
void osmo_iofd_free(struct osmo_io_fd *iofd)
Free the given osmo_io_fd.
Definition: osmo_io.c:1093
osmo_io_backend_names
const struct value_string osmo_io_backend_names[]
Definition: osmo_io.c:54
osmo_iofd_get_txqueue_max_length
unsigned int osmo_iofd_get_txqueue_max_length(const struct osmo_io_fd *iofd)
Get the maximum number of messages enqueued for sending.
Definition: osmo_io.c:1166
osmo_io_get_backend
enum osmo_io_backend osmo_io_get_backend(void)
Obtain the osmo_io_backend in use by the process.
Definition: osmo_io.c:131
osmo_iofd_setup
struct osmo_io_fd * osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data)
Allocate and setup a new iofd.
Definition: osmo_io.c:838
osmo_iofd_register
int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
Register the osmo_io_fd for active I/O.
Definition: osmo_io.c:997
osmo_iofd_sendmsg_msgb
int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
osmo_io equivalent of the sendmsg(2) socket API call.
Definition: osmo_io.c:720
osmo_iofd_txqueue_len
unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
Retrieve the number of messages pending in the transmit queue.
Definition: osmo_io.c:1062
osmo_iofd_init
void osmo_iofd_init(void)
initialize osmo_io for the current thread
Definition: osmo_io.c:75
osmo_iofd_mode_names
const struct value_string osmo_iofd_mode_names[]
Definition: osmo_io.c:60
osmo_iofd_get_name
const char * osmo_iofd_get_name(const struct osmo_io_fd *iofd)
Retrieve the human-readable name of the given osmo_io_fd.
Definition: osmo_io.c:1229
osmo_iofd_set_alloc_info
void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
Set the size and headroom of the msgb allocated when receiving messages.
Definition: osmo_io.c:1145
osmo_iofd_set_name_f
void osmo_iofd_set_name_f(struct osmo_io_fd *iofd, const char *fmt,...)
Set the human-readable name of the file descriptor using arguments like printf()
Definition: osmo_io.c:1246
osmo_iofd_get_data
void * osmo_iofd_get_data(const struct osmo_io_fd *iofd)
Retrieve the associated user-data from an osmo_io_fd.
Definition: osmo_io.c:1179
osmo_iofd_get_priv_nr
unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
Retrieve the private number from an osmo_io_fd.
Definition: osmo_io.c:1202
osmo_iofd_close
int osmo_iofd_close(struct osmo_io_fd *iofd)
Close the given osmo_io_fd.
Definition: osmo_io.c:1116
osmo_iofd_sendto_msgb
int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
Send a message through an unconnected socket.
Definition: osmo_io.c:669
osmo_io_op
osmo_io_op
The operation of an osmo_io_fd determines if read or write operation are to be configured used.
Definition: osmo_io.h:76
osmo_iofd_set_cmsg_size
int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size)
Set the size of the control message buffer allocated when submitting recvmsg.
Definition: osmo_io.c:904
OSMO_IO_FD_MODE_RECVFROM_SENDTO
@ OSMO_IO_FD_MODE_RECVFROM_SENDTO
use recvfrom() / sendto() semantics with recvfrom_cb/sendto_cb in osmo_io_ops
Definition: osmo_io.h:70
OSMO_IO_FD_MODE_RECVMSG_SENDMSG
@ OSMO_IO_FD_MODE_RECVMSG_SENDMSG
emulate recvmsg() / sendmsg() semantics with recvmsg_cb/sendto_cb in osmo_io_ops
Definition: osmo_io.h:72
OSMO_IO_FD_MODE_READ_WRITE
@ OSMO_IO_FD_MODE_READ_WRITE
use read() / write() semantics with read_cb/write_cb in osmo_io_ops
Definition: osmo_io.h:68
OSMO_IO_BACKEND_POLL
@ OSMO_IO_BACKEND_POLL
classic back-end using poll(2) and direct read/write/recvfrom/sendto/recvmsg/sendmsg syscalls
Definition: osmo_io.h:87
OSMO_IO_BACKEND_IO_URING
@ OSMO_IO_BACKEND_IO_URING
back-end using io_uring to perform efficient I/O and reduce syscall overhead
Definition: osmo_io.h:89
OSMO_IO_OP_READ
@ OSMO_IO_OP_READ
change parameters for read()
Definition: osmo_io.h:78
OSMO_IO_OP_WRITE
@ OSMO_IO_OP_WRITE
change parameters for write()
Definition: osmo_io.h:80
get_value_string
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:63
msg
uint8_t msg[0]
linuxlist.h
Simple doubly linked list implementation.
logging.h
msgb.h
socket.h
Osmocom socket convenience functions.
msgb
Osmocom message buffer.
Definition: msgb.h:31
osmo_io_fd
Definition: osmo_io_internal.h:56
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::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::headroom
unsigned int headroom
headroom to allocate when allocating msgb's
Definition: osmo_io_internal.h:95
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::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_io_ops::read_cb
void(* read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg)
completion call-back function when something was read from fd.
Definition: osmo_io.h:118
osmo_io_ops::recvfrom_cb
void(* recvfrom_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg, const struct osmo_sockaddr *saddr)
completion call-back function when recvfrom(2) has completed.
Definition: osmo_io.h:175
osmo_io_ops::segmentation_cb2
int(* segmentation_cb2)(struct osmo_io_fd *iofd, struct msgb *msg)
optional call-back function to segment the data at message boundaries.
Definition: osmo_io.h:163
osmo_io_ops::write_cb
void(* write_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg)
completion call-back function when write issued via osmo_iofd_write_msgb() has completed on fd.
Definition: osmo_io.h:126
osmo_io_ops::sendto_cb
void(* sendto_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg, const struct osmo_sockaddr *daddr)
completion call-back function when sendto() issued via osmo_iofd_sendto_msgb() has completed on fd.
Definition: osmo_io.h:185
osmo_io_ops::sendmsg_cb
void(* sendmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg)
completion call-back function when sendmsg() issued via osmo_iofd_sendmsg_msgb() has completed on fd.
Definition: osmo_io.h:208
osmo_io_ops::recvmsg_cb
void(* recvmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg, const struct msghdr *msgh)
completion call-back function when recvmsg(2) has completed.
Definition: osmo_io.h:200
osmo_io_ops::segmentation_cb
int(* segmentation_cb)(struct msgb *msg)
optional call-back function to segment the data at message boundaries.
Definition: osmo_io.h:148
osmo_sockaddr
Definition: socket.h:33
value_string
A mapping between human-readable string and numeric value.
Definition: utils.h:51
utils.h