libosmocore 1.9.0.196-9975
Osmocom core library
osmo_io.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <sys/socket.h>
8
11#include <osmocom/core/msgb.h>
12#include <osmocom/core/socket.h>
13#include <osmocom/core/utils.h>
14
57#define LOGPIO(iofd, level, fmt, args...) \
58 LOGP(DLIO, level, "iofd(%s)" fmt, iofd->name, ## args)
59
60struct osmo_io_fd;
61
71};
72
80};
81
82extern const struct value_string osmo_io_backend_names[];
84static inline const char *osmo_io_backend_name(enum osmo_io_backend val)
86
87extern const struct value_string osmo_iofd_mode_names[];
89static inline const char *osmo_iofd_mode_name(enum osmo_io_fd_mode val)
91
94 /* mode OSMO_IO_FD_MODE_READ_WRITE: */
95 struct {
102 void (*read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
103
110 void (*write_cb)(struct osmo_io_fd *iofd, int res,
111 struct msgb *msg);
112
132 int (*segmentation_cb)(struct msgb *msg);
133
147 int (*segmentation_cb2)(struct osmo_io_fd *iofd, struct msgb *msg);
148 };
149
150 /* mode OSMO_IO_FD_MODE_RECVFROM_SENDTO: */
151 struct {
159 void (*recvfrom_cb)(struct osmo_io_fd *iofd, int res,
160 struct msgb *msg,
161 const struct osmo_sockaddr *saddr);
169 void (*sendto_cb)(struct osmo_io_fd *iofd, int res,
170 struct msgb *msg,
171 const struct osmo_sockaddr *daddr);
172 };
173
174 /* mode OSMO_IO_FD_MODE_RECVMSG_SENDMSG: */
175 struct {
184 void (*recvmsg_cb)(struct osmo_io_fd *iofd, int res,
185 struct msgb *msg, const struct msghdr *msgh);
192 void (*sendmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
193 };
194};
195
196void osmo_iofd_init(void);
197
198struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name,
199 enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data);
200int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size);
201int osmo_iofd_register(struct osmo_io_fd *iofd, int fd);
202int osmo_iofd_unregister(struct osmo_io_fd *iofd);
203unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd);
204void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd);
205int osmo_iofd_close(struct osmo_io_fd *iofd);
206void osmo_iofd_free(struct osmo_io_fd *iofd);
207
208void osmo_iofd_notify_connected(struct osmo_io_fd *iofd);
209
210int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg);
211int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags,
212 const struct osmo_sockaddr *dest);
213int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags,
214 const struct msghdr *msgh);
215
216void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom);
217void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int size);
218void *osmo_iofd_get_data(const struct osmo_io_fd *iofd);
219void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data);
220
221unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd);
222void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr);
223
224int osmo_iofd_get_fd(const struct osmo_io_fd *iofd);
225const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd);
226void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name);
227
228int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops);
229void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops);
230
const char * name
uint8_t res[16]
uint8_t data[0]
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:893
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:438
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:64
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:990
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:951
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:89
int osmo_iofd_unregister(struct osmo_io_fd *iofd)
Unregister the given osmo_io_fd from osmo_io.
Definition: osmo_io.c:772
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:84
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:1004
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:914
osmo_io_backend
The back-end used by osmo_io.
Definition: osmo_io.h:75
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:867
void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
Clear the transmit queue of the given osmo_io_fd.
Definition: osmo_io.c:791
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:939
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:922
void osmo_iofd_free(struct osmo_io_fd *iofd)
Free the given osmo_io_fd.
Definition: osmo_io.c:809
const struct value_string osmo_io_backend_names[]
Definition: osmo_io.c:53
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:656
int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
Register the osmo_io_fd for active I/O.
Definition: osmo_io.c:735
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:537
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:781
void osmo_iofd_init(void)
initialize osmo_io for the current thread
Definition: osmo_io.c:76
const struct value_string osmo_iofd_mode_names[]
Definition: osmo_io.c:59
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:930
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:857
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:880
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:903
int osmo_iofd_close(struct osmo_io_fd *iofd)
Close the given osmo_io_fd.
Definition: osmo_io.c:832
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:485
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:710
@ OSMO_IO_FD_MODE_RECVFROM_SENDTO
use recvfrom() / sendto() semantics with recvfrom_cb/sendto_cb in osmo_io_ops
Definition: osmo_io.h:68
@ OSMO_IO_FD_MODE_RECVMSG_SENDMSG
emulate recvmsg() / sendmsg() semantics with recvmsg_cb/sendto_cb in osmo_io_ops
Definition: osmo_io.h:70
@ OSMO_IO_FD_MODE_READ_WRITE
use read() / write() semantics with read_cb/write_cb in osmo_io_ops
Definition: osmo_io.h:66
@ OSMO_IO_BACKEND_POLL
classic back-end using poll(2) and direct read/write/recvfrom/sendto/recvmsg/sendmsg syscalls
Definition: osmo_io.h:77
@ OSMO_IO_BACKEND_IO_URING
back-end using io_uring to perform efficient I/O and reduce syscall overhead
Definition: osmo_io.h:79
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:54
uint8_t msg[0]
Simple doubly linked list implementation.
Osmocom socket convenience functions.
Osmocom message buffer.
Definition: msgb.h:31
Definition: osmo_io_internal.h:52
enum osmo_io_fd_mode mode
type of read/write mode to use
Definition: osmo_io_internal.h:58
size_t cmsg_size
size of iofd_msghdr.cmsg[] when allocated in recvmsg path
Definition: osmo_io_internal.h:77
unsigned int headroom
headroom to allocate when allocating msgb's
Definition: osmo_io_internal.h:85
unsigned int size
size of msgb to allocate (excluding headroom)
Definition: osmo_io_internal.h:83
int fd
actual operating-system level file decriptor
Definition: osmo_io_internal.h:56
const void * ctx
talloc context from which to allocate msgb when reading
Definition: osmo_io_internal.h:81
unsigned int priv_nr
private number, extending data
Definition: osmo_io_internal.h:74
I/O operations (call-back functions) related to an osmo_io_fd.
Definition: osmo_io.h:93
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:102
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:159
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:147
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:110
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:169
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:192
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:184
int(* segmentation_cb)(struct msgb *msg)
optional call-back function to segment the data at message boundaries.
Definition: osmo_io.h:132
Definition: socket.h:38
A mapping between human-readable string and numeric value.
Definition: utils.h:51