libosmocore 1.9.0.186-fab9.202403182026
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
15
16#define LOGPIO(iofd, level, fmt, args...) \
17 LOGP(DLIO, level, "iofd(%s)" fmt, iofd->name, ## args)
18
19struct osmo_io_fd;
20
28};
29
33};
34
35extern const struct value_string osmo_io_backend_names[];
36static inline const char *osmo_io_backend_name(enum osmo_io_backend val)
38
39extern const struct value_string osmo_iofd_mode_names[];
40static inline const char *osmo_iofd_mode_name(enum osmo_io_fd_mode val)
42
44 /* mode OSMO_IO_FD_MODE_READ_WRITE: */
45 struct {
47 void (*read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
49 void (*write_cb)(struct osmo_io_fd *iofd, int res,
50 struct msgb *msg);
60 int (*segmentation_cb)(struct msgb *msg);
61 };
62
63 /* mode OSMO_IO_FD_MODE_RECVFROM_SENDTO: */
64 struct {
66 void (*recvfrom_cb)(struct osmo_io_fd *iofd, int res,
67 struct msgb *msg,
68 const struct osmo_sockaddr *saddr);
70 void (*sendto_cb)(struct osmo_io_fd *iofd, int res,
71 struct msgb *msg,
72 const struct osmo_sockaddr *daddr);
73 };
74
75 /* mode OSMO_IO_FD_MODE_RECVMSG_SENDMSG: */
76 struct {
77 void (*recvmsg_cb)(struct osmo_io_fd *iofd, int res,
78 struct msgb *msg, const struct msghdr *msgh);
79 void (*sendmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
80 };
81};
82
83void osmo_iofd_init(void);
84
85struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name,
86 enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data);
87int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size);
88int osmo_iofd_register(struct osmo_io_fd *iofd, int fd);
89int osmo_iofd_unregister(struct osmo_io_fd *iofd);
90unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd);
91void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd);
92int osmo_iofd_close(struct osmo_io_fd *iofd);
93void osmo_iofd_free(struct osmo_io_fd *iofd);
94
95void osmo_iofd_notify_connected(struct osmo_io_fd *iofd);
96
97int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg);
98int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags,
99 const struct osmo_sockaddr *dest);
100int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags,
101 const struct msghdr *msgh);
102
103void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom);
104void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int size);
105void *osmo_iofd_get_data(const struct osmo_io_fd *iofd);
106void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data);
107
108unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd);
109void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr);
110
111int osmo_iofd_get_fd(const struct osmo_io_fd *iofd);
112const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd);
113void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name);
114
115int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops);
116void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops);
const char * name
uint8_t res[16]
uint8_t data[0]
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.
void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
Set the associated user-data from an iofd.
Definition: osmo_io.c:832
int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
Send a message through a connected socket.
Definition: osmo_io.c:423
osmo_io_fd_mode
Definition: osmo_io.h:21
@ OSMO_IO_FD_MODE_RECVFROM_SENDTO
use recvfrom() / sendto() calls
Definition: osmo_io.h:25
@ OSMO_IO_FD_MODE_RECVMSG_SENDMSG
emulate recvmsg() / sendmsg()
Definition: osmo_io.h:27
@ OSMO_IO_FD_MODE_READ_WRITE
use read() / write() calls
Definition: osmo_io.h:23
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:814
void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
Get the osmo_io_ops for an iofd.
Definition: osmo_io.c:921
int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
Set the osmo_io_ops for an iofd.
Definition: osmo_io.c:882
static const char * osmo_iofd_mode_name(enum osmo_io_fd_mode val)
Definition: osmo_io.h:40
int osmo_iofd_unregister(struct osmo_io_fd *iofd)
Unregister the fd from the underlying backend.
Definition: osmo_io.c:722
static const char * osmo_io_backend_name(enum osmo_io_backend val)
Definition: osmo_io.h:36
void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
Notify the user if/when the socket is connected.
Definition: osmo_io.c:929
void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
Set the private number from an iofd.
Definition: osmo_io.c:850
osmo_io_backend
Definition: osmo_io.h:30
@ OSMO_IO_BACKEND_POLL
Definition: osmo_io.h:31
@ OSMO_IO_BACKEND_IO_URING
Definition: osmo_io.h:32
void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
Clear the transmit queue of the the iofd.
Definition: osmo_io.c:741
void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
Set the name of the file descriptor.
Definition: osmo_io.c:874
int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
Get the underlying file descriptor from an iofd.
Definition: osmo_io.c:858
void osmo_iofd_free(struct osmo_io_fd *iofd)
Free the iofd.
Definition: osmo_io.c:756
const struct value_string osmo_io_backend_names[]
Definition: osmo_io.c:48
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:623
int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
Register the fd with the underlying backend.
Definition: osmo_io.c:688
int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, const struct msghdr *msgh)
ismo_io equivalent of the sendmsg(2) socket API call
Definition: osmo_io.c:522
unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
Get the number of messages in the tx queue.
Definition: osmo_io.c:731
void osmo_iofd_init(void)
initialize osmo_io for the current thread
Definition: osmo_io.c:71
const struct value_string osmo_iofd_mode_names[]
Definition: osmo_io.c:54
const char * osmo_iofd_get_name(const struct osmo_io_fd *iofd)
Get the name of the file descriptor.
Definition: osmo_io.c:866
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:804
void * osmo_iofd_get_data(const struct osmo_io_fd *iofd)
Get the associated user-data from an iofd.
Definition: osmo_io.c:823
unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
Get the private number from an iofd.
Definition: osmo_io.c:841
int osmo_iofd_close(struct osmo_io_fd *iofd)
Close the iofd.
Definition: osmo_io.c:779
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:470
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:673
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
Definition: osmo_io.h:43
void(* read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg)
call-back function when something was read from fd
Definition: osmo_io.h:47
void(* recvfrom_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg, const struct osmo_sockaddr *saddr)
call-back function emulating recvfrom
Definition: osmo_io.h:66
void(* write_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg)
call-back function when write has completed on fd
Definition: osmo_io.h:49
void(* sendto_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg, const struct osmo_sockaddr *daddr)
call-back function emulating sendto
Definition: osmo_io.h:70
void(* sendmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg)
Definition: osmo_io.h:79
void(* recvmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg, const struct msghdr *msgh)
Definition: osmo_io.h:77
int(* segmentation_cb)(struct msgb *msg)
call-back function to segment the data at message boundaries.
Definition: osmo_io.h:60
Definition: socket.h:38
A mapping between human-readable string and numeric value.
Definition: utils.h:51