Loading...
Searching...
No Matches
osmo_io_internal.h
Go to the documentation of this file.
#pragma once
#include <unistd.h>
#include <stdbool.h>
#include <netinet/sctp.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>
#include "../config.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;
#endif
struct iofd_backend_ops {
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))
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;
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 {
bool read_enabled;
bool write_enabled;
void *read_msghdr;
void *write_msghdr;
/* 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,
};
struct iofd_msghdr {
struct llist_head list;
enum iofd_msg_action action;
struct msghdr hdr;
struct osmo_sockaddr osa;
struct iovec iov[1];
int flags;
struct msgb *msg;
struct osmo_io_fd *iofd;
char cmsg[0]; /* size is determined by iofd->cmsg_size on recvmsg, and by mcghdr->msg_controllen on sendmsg */
};
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_alloc(struct osmo_io_fd *iofd);
struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd);
struct msgb *iofd_msgb_pending_or_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);
void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
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:108
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:234
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:64
iofd_msghdr_free
void iofd_msghdr_free(struct iofd_msghdr *msghdr)
Free the msghdr.
Definition osmo_io.c:163
iofd_msgb_pending_or_alloc
struct msgb * iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
Return the pending msgb or allocate and return a new one.
Definition osmo_io.c:191
iofd_handle_segmented_read
void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
Restore message boundaries on read() and pass individual messages to the read callback.
Definition osmo_io.c:331
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:396
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:131
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:171
iofd_msgb_pending
struct msgb * iofd_msgb_pending(struct osmo_io_fd *iofd)
return the pending msgb in iofd or NULL if there is none
Definition osmo_io.c:180
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:210
iofd_txqueue_dequeue
struct iofd_msghdr * iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
Dequeue a message from the front.
Definition osmo_io.c:248
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:372
msg
uint8_t msg[0]
linuxlist.h
Simple doubly linked list implementation.
msgb.h
osmo_io.h
io(_uring) abstraction osmo fd compatibility
iofd_msg_action
iofd_msg_action
Definition osmo_io_internal.h:113
IOFD_ACT_RECVFROM
@ IOFD_ACT_RECVFROM
Definition osmo_io_internal.h:116
IOFD_ACT_RECVMSG
@ IOFD_ACT_RECVMSG
Definition osmo_io_internal.h:118
IOFD_ACT_SENDTO
@ IOFD_ACT_SENDTO
Definition osmo_io_internal.h:117
IOFD_ACT_READ
@ IOFD_ACT_READ
Definition osmo_io_internal.h:114
IOFD_ACT_WRITE
@ IOFD_ACT_WRITE
Definition osmo_io_internal.h:115
IOFD_ACT_SENDMSG
@ IOFD_ACT_SENDMSG
Definition osmo_io_internal.h:119
iofd_seg_act
iofd_seg_act
Definition osmo_io_internal.h:147
IOFD_SEG_ACT_DEFER
@ IOFD_SEG_ACT_DEFER
Definition osmo_io_internal.h:150
IOFD_SEG_ACT_HANDLE_MORE
@ IOFD_SEG_ACT_HANDLE_MORE
Definition osmo_io_internal.h:149
IOFD_SEG_ACT_HANDLE_ONE
@ IOFD_SEG_ACT_HANDLE_ONE
Definition osmo_io_internal.h:148
iofd_poll_ops
const struct iofd_backend_ops iofd_poll_ops
Definition osmo_io_poll.c:201
select.h
libmnl integration
socket.h
Osmocom socket convenience functions.
iofd_backend_ops
Definition osmo_io_internal.h:27
iofd_backend_ops::read_enable
void(* read_enable)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:33
iofd_backend_ops::write_enable
void(* write_enable)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:31
iofd_backend_ops::read_disable
void(* read_disable)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:34
iofd_backend_ops::write_disable
void(* write_disable)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:32
iofd_backend_ops::close
int(* close)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:30
iofd_backend_ops::register_fd
int(* register_fd)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:28
iofd_backend_ops::notify_connected
void(* notify_connected)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:35
iofd_backend_ops::unregister_fd
int(* unregister_fd)(struct osmo_io_fd *iofd)
Definition osmo_io_internal.h:29
iofd_msghdr
serialized version of 'struct msghdr' employed by sendmsg/recvmsg
Definition osmo_io_internal.h:124
iofd_msghdr::cmsg
char cmsg[0]
control message buffer for passing sctp_sndrcvinfo along
Definition osmo_io_internal.h:144
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:141
iofd_msghdr::list
struct llist_head list
entry into osmo_io_fd.tx_queue.msg_queue
Definition osmo_io_internal.h:126
iofd_msghdr::flags
int flags
flags we pass as argument to sendmsg / recvmsg
Definition osmo_io_internal.h:136
iofd_msghdr::action
enum iofd_msg_action action
Definition osmo_io_internal.h:127
iofd_msghdr::msg
struct msgb * msg
message-buffer containing data for this I/O operation
Definition osmo_io_internal.h:139
iofd_msghdr::iov
struct iovec iov[1]
io-vector we need to pass as argument to sendmsg/recvmsg; is set up to point into msg below
Definition osmo_io_internal.h:134
iofd_msghdr::hdr
struct msghdr hdr
the 'struct msghdr' we are wrapping/ecapsulating here
Definition osmo_io_internal.h:129
iofd_msghdr::osa
struct osmo_sockaddr osa
socket address of the remote peer
Definition osmo_io_internal.h:131
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:52
osmo_io_fd::flags
uint32_t flags
flags to guard closing/freeing of iofd
Definition osmo_io_internal.h:61
osmo_io_fd::current_length
unsigned int current_length
current length of write queue
Definition osmo_io_internal.h:92
osmo_io_fd::read_msghdr
void * read_msghdr
Definition osmo_io_internal.h:104
osmo_io_fd::pending
struct msgb * pending
Pending msgb to keep partial data during segmentation.
Definition osmo_io_internal.h:69
osmo_io_fd::read_enabled
bool read_enabled
Definition osmo_io_internal.h:102
osmo_io_fd::write_msghdr
void * write_msghdr
Definition osmo_io_internal.h:105
osmo_io_fd::msg_queue
struct llist_head msg_queue
actual linked list implementing the transmit queue
Definition osmo_io_internal.h:94
osmo_io_fd::write_enabled
bool write_enabled
Definition osmo_io_internal.h:103
osmo_io_fd::mode
enum osmo_io_fd_mode mode
type of read/write mode to use
Definition osmo_io_internal.h:58
osmo_io_fd::name
char * name
human-readable name to associte with fd
Definition osmo_io_internal.h:64
osmo_io_fd::list
struct llist_head list
linked list for internal management
Definition osmo_io_internal.h:54
osmo_io_fd::max_length
unsigned int max_length
maximum length of write queue
Definition osmo_io_internal.h:90
osmo_io_fd::cmsg_size
size_t cmsg_size
size of iofd_msghdr.cmsg[] when allocated in recvmsg path
Definition osmo_io_internal.h:77
osmo_io_fd::connect_ofd
struct osmo_fd connect_ofd
Definition osmo_io_internal.h:108
osmo_io_fd::headroom
unsigned int headroom
headroom to allocate when allocating msgb's
Definition osmo_io_internal.h:85
osmo_io_fd::ofd
struct osmo_fd ofd
Definition osmo_io_internal.h:99
osmo_io_fd::size
unsigned int size
size of msgb to allocate (excluding headroom)
Definition osmo_io_internal.h:83
osmo_io_fd::fd
int fd
actual operating-system level file decriptor
Definition osmo_io_internal.h:56
osmo_io_fd::ctx
const void * ctx
talloc context from which to allocate msgb when reading
Definition osmo_io_internal.h:81
osmo_io_fd::io_ops
struct osmo_io_ops io_ops
send/recv (msg) callback functions
Definition osmo_io_internal.h:67
osmo_io_fd::data
void * data
data pointer passed through to call-back function
Definition osmo_io_internal.h:72
osmo_io_fd::priv_nr
unsigned int priv_nr
private number, extending data
Definition osmo_io_internal.h:74
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:93
osmo_sockaddr
Definition socket.h:38