Loading...
Searching...
No Matches
osmo_io_internal.h
Go to the documentation of this file.
#pragma once
#include "../config.h"
#include <unistd.h>
#include <stdbool.h>
#include <osmocom/core/osmo_io.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/select.h>
#include <osmocom/core/socket.h>
#define OSMO_IO_DEFAULT_MSGB_SIZE 1024
#define OSMO_IO_DEFAULT_MSGB_HEADROOM 128
extern const struct iofd_backend_ops iofd_poll_ops;
#define OSMO_IO_BACKEND_DEFAULT "POLL"
#if defined(HAVE_URING)
extern const struct iofd_backend_ops iofd_uring_ops;
void osmo_iofd_uring_constructor(void);
void osmo_iofd_uring_init(void);
#endif
struct iofd_backend_ops {
int (*setup)(struct osmo_io_fd *iofd);
int (*register_fd)(struct osmo_io_fd *iofd);
int (*unregister_fd)(struct osmo_io_fd *iofd);
int (*close)(struct osmo_io_fd *iofd);
void (*write_enable)(struct osmo_io_fd *iofd);
void (*write_disable)(struct osmo_io_fd *iofd);
void (*read_enable)(struct osmo_io_fd *iofd);
void (*read_disable)(struct osmo_io_fd *iofd);
void (*notify_connected)(struct osmo_io_fd *iofd);
};
#define IOFD_FLAG_CLOSED (1<<0)
#define IOFD_FLAG_IN_CALLBACK (1<<1)
#define IOFD_FLAG_TO_FREE (1<<2)
#define IOFD_FLAG_NOTIFY_CONNECTED (1<<3)
#define IOFD_FLAG_FD_REGISTERED (1<<4)
#define IOFD_FLAG_SET(iofd, flag) \
(iofd)->flags |= (flag)
#define IOFD_FLAG_UNSET(iofd, flag) \
(iofd)->flags &= ~(flag)
#define IOFD_FLAG_ISSET(iofd, flag) ((iofd)->flags & (flag))
#define IOFD_MSGHDR_MAX_READ_SQES 32
struct osmo_io_fd {
struct llist_head list;
int fd;
enum osmo_io_fd_mode mode;
uint32_t flags;
char *name;
struct osmo_io_ops io_ops;
struct msgb *pending;
void *data;
unsigned int priv_nr;
size_t cmsg_size;
uint8_t io_read_buffers;
uint8_t io_write_buffers;
struct {
const void *ctx;
unsigned int size;
unsigned int headroom;
} msgb_alloc;
struct {
unsigned int max_length;
unsigned int current_length;
struct llist_head msg_queue;
} tx_queue;
union {
struct {
struct osmo_fd ofd;
} poll;
struct {
struct {
bool enabled;
uint8_t num_sqes;
void *msghdr[IOFD_MSGHDR_MAX_READ_SQES];
struct io_uring *ring;
uint8_t sqes_submitted;
} read;
struct {
bool enabled;
void *msghdr;
struct io_uring *ring;
} write;
/* TODO: index into array of registered fd's? */
/* osmo_fd for non-blocking connect handling */
struct osmo_fd connect_ofd;
} uring;
} u;
};
enum iofd_msg_action {
IOFD_ACT_READ,
IOFD_ACT_WRITE,
IOFD_ACT_RECVFROM,
IOFD_ACT_SENDTO,
IOFD_ACT_RECVMSG,
IOFD_ACT_SENDMSG,
};
#define IOFD_MSGHDR_IO_BUFFERS 8
struct iofd_msghdr {
struct llist_head list;
enum iofd_msg_action action;
struct msghdr hdr;
struct osmo_sockaddr osa;
struct iovec iov[IOFD_MSGHDR_IO_BUFFERS];
int flags;
uint8_t io_len;
struct msgb *msg[IOFD_MSGHDR_IO_BUFFERS];
struct osmo_io_fd *iofd;
bool in_cancel_queue;
char _Alignas(struct cmsghdr) cmsg[0];
};
enum iofd_seg_act {
IOFD_SEG_ACT_HANDLE_ONE,
IOFD_SEG_ACT_HANDLE_MORE,
IOFD_SEG_ACT_DEFER,
};
struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size);
void iofd_msghdr_free(struct iofd_msghdr *msghdr);
struct msgb *iofd_msgb_alloc2(struct osmo_io_fd *iofd, size_t size);
struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr);
void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr);
int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd);
msgb_alloc
struct msgb * msgb_alloc(uint16_t size, const char *name)
Allocate a new message buffer from tall_msgb_ctx.
Definition msgb.c:109
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:537
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
msg
uint8_t msg[0]
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::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::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::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::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_fd::tx_queue
struct osmo_io_fd::@213032166116346075373040302302243174211211232213 tx_queue
osmo_io_fd::u
union osmo_io_fd::@040257067356231006065175370111377221044300253311 u
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