libosmocore 1.9.0.186-fab9.202403162026
Osmocom core library
osmo_io.h File Reference

io(_uring) abstraction osmo fd compatibility More...

#include <sys/socket.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/utils.h>

Go to the source code of this file.

Data Structures

struct  osmo_io_ops
 

Macros

#define LOGPIO(iofd, level, fmt, args...)    LOGP(DLIO, level, "iofd(%s)" fmt, iofd->name, ## args)
 

Enumerations

enum  osmo_io_fd_mode {
  OSMO_IO_FD_MODE_READ_WRITE ,
  OSMO_IO_FD_MODE_RECVFROM_SENDTO ,
  OSMO_IO_FD_MODE_RECVMSG_SENDMSG
}
 
enum  osmo_io_backend {
  OSMO_IO_BACKEND_POLL ,
  OSMO_IO_BACKEND_IO_URING
}
 

Functions

static const char * osmo_io_backend_name (enum osmo_io_backend val)
 
static const char * osmo_iofd_mode_name (enum osmo_io_fd_mode val)
 
void osmo_iofd_init (void)
 initialize osmo_io for the current thread More...
 
struct osmo_io_fdosmo_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. More...
 
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. More...
 
int osmo_iofd_register (struct osmo_io_fd *iofd, int fd)
 Register the fd with the underlying backend. More...
 
int osmo_iofd_unregister (struct osmo_io_fd *iofd)
 Unregister the fd from the underlying backend. More...
 
unsigned int osmo_iofd_txqueue_len (struct osmo_io_fd *iofd)
 Get the number of messages in the tx queue. More...
 
void osmo_iofd_txqueue_clear (struct osmo_io_fd *iofd)
 Clear the transmit queue of the the iofd. More...
 
int osmo_iofd_close (struct osmo_io_fd *iofd)
 Close the iofd. More...
 
void osmo_iofd_free (struct osmo_io_fd *iofd)
 Free the iofd. More...
 
void osmo_iofd_notify_connected (struct osmo_io_fd *iofd)
 Notify the user if/when the socket is connected. More...
 
int osmo_iofd_write_msgb (struct osmo_io_fd *iofd, struct msgb *msg)
 Send a message through a connected socket. More...
 
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. More...
 
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 More...
 
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. More...
 
void osmo_iofd_set_txqueue_max_length (struct osmo_io_fd *iofd, unsigned int size)
 Set the maximum number of messages enqueued for sending. More...
 
void * osmo_iofd_get_data (const struct osmo_io_fd *iofd)
 Get the associated user-data from an iofd. More...
 
void osmo_iofd_set_data (struct osmo_io_fd *iofd, void *data)
 Set the associated user-data from an iofd. More...
 
unsigned int osmo_iofd_get_priv_nr (const struct osmo_io_fd *iofd)
 Get the private number from an iofd. More...
 
void osmo_iofd_set_priv_nr (struct osmo_io_fd *iofd, unsigned int priv_nr)
 Set the private number from an iofd. More...
 
int osmo_iofd_get_fd (const struct osmo_io_fd *iofd)
 Get the underlying file descriptor from an iofd. More...
 
const char * osmo_iofd_get_name (const struct osmo_io_fd *iofd)
 Get the name of the file descriptor. More...
 
void osmo_iofd_set_name (struct osmo_io_fd *iofd, const char *name)
 Set the name of the file descriptor. More...
 
int osmo_iofd_set_ioops (struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
 Set the osmo_io_ops for an iofd. More...
 
void osmo_iofd_get_ioops (struct osmo_io_fd *iofd, struct osmo_io_ops *ioops)
 Get the osmo_io_ops for an iofd. More...
 

Variables

const struct value_string osmo_io_backend_names []
 
const struct value_string osmo_iofd_mode_names []
 

Detailed Description

io(_uring) abstraction osmo fd compatibility

Macro Definition Documentation

◆ LOGPIO

#define LOGPIO (   iofd,
  level,
  fmt,
  args... 
)     LOGP(DLIO, level, "iofd(%s)" fmt, iofd->name, ## args)

Enumeration Type Documentation

◆ osmo_io_backend

Enumerator
OSMO_IO_BACKEND_POLL 
OSMO_IO_BACKEND_IO_URING 

◆ osmo_io_fd_mode

Enumerator
OSMO_IO_FD_MODE_READ_WRITE 

use read() / write() calls

OSMO_IO_FD_MODE_RECVFROM_SENDTO 

use recvfrom() / sendto() calls

OSMO_IO_FD_MODE_RECVMSG_SENDMSG 

emulate recvmsg() / sendmsg()

Function Documentation

◆ osmo_io_backend_name()

static const char * osmo_io_backend_name ( enum osmo_io_backend  val)
inlinestatic

◆ osmo_iofd_close()

int osmo_iofd_close ( struct osmo_io_fd iofd)

Close the iofd.

This function closes the underlying fd and clears any messages in the tx queue The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()

Parameters
[in]iofdthe file descriptor \ returns 0 on success, a negative value otherwise

References iofd_backend_ops::close, osmo_io_fd::fd, iofd_msghdr::iofd, IOFD_FLAG_CLOSED, IOFD_FLAG_ISSET, IOFD_FLAG_SET, msgb_free(), osmo_iofd_ops, osmo_iofd_txqueue_clear(), and osmo_io_fd::pending.

Referenced by osmo_iofd_free().

◆ osmo_iofd_free()

void osmo_iofd_free ( struct osmo_io_fd iofd)

Free the iofd.

This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so. The iofd will be closed before.

Parameters
[in]iofdthe file descriptor

References iofd_msghdr::iofd, IOFD_FLAG_IN_CALLBACK, IOFD_FLAG_ISSET, IOFD_FLAG_SET, IOFD_FLAG_TO_FREE, and osmo_iofd_close().

◆ osmo_iofd_get_data()

void * osmo_iofd_get_data ( const struct osmo_io_fd iofd)

Get the associated user-data from an iofd.

Parameters
[in]iofdthe file descriptor
Returns
the data that was previously set with osmo_iofd_setup()

References osmo_io_fd::data, and iofd_msghdr::iofd.

◆ osmo_iofd_get_fd()

int osmo_iofd_get_fd ( const struct osmo_io_fd iofd)

Get the underlying file descriptor from an iofd.

Parameters
[in]iofdthe file descriptor
Returns
the underlying file descriptor number

References osmo_io_fd::fd, and iofd_msghdr::iofd.

◆ osmo_iofd_get_ioops()

void osmo_iofd_get_ioops ( struct osmo_io_fd iofd,
struct osmo_io_ops ioops 
)

Get the osmo_io_ops for an iofd.

Parameters
[in]iofdTarget iofd file descriptor
[in]ioopscaller-allocated osmo_io_ops structure to be filled

References osmo_io_fd::io_ops, and iofd_msghdr::iofd.

◆ osmo_iofd_get_name()

const char * osmo_iofd_get_name ( const struct osmo_io_fd iofd)

Get the name of the file descriptor.

Parameters
[in]iofdthe file descriptor
Returns
the name of the iofd as given in osmo_iofd_setup()

References iofd_msghdr::iofd, and osmo_io_fd::name.

◆ osmo_iofd_get_priv_nr()

unsigned int osmo_iofd_get_priv_nr ( const struct osmo_io_fd iofd)

Get the private number from an iofd.

Parameters
[in]iofdthe file descriptor
Returns
the private number that was previously set with osmo_iofd_set_priv_nr()

References iofd_msghdr::iofd, and osmo_io_fd::priv_nr.

◆ osmo_iofd_init()

void osmo_iofd_init ( void  )

initialize osmo_io for the current thread

References g_io_backend, OSMO_ASSERT, OSMO_IO_BACKEND_IO_URING, and OSMO_IO_BACKEND_POLL.

Referenced by __attribute__().

◆ osmo_iofd_mode_name()

static const char * osmo_iofd_mode_name ( enum osmo_io_fd_mode  val)
inlinestatic

◆ osmo_iofd_notify_connected()

void osmo_iofd_notify_connected ( struct osmo_io_fd iofd)

Notify the user if/when the socket is connected.

When the socket is connected the write_cb will be called.

Parameters
[in]iofdthe file descriptor

References iofd_msghdr::iofd, osmo_io_fd::mode, iofd_backend_ops::notify_connected, OSMO_ASSERT, OSMO_IO_FD_MODE_READ_WRITE, OSMO_IO_FD_MODE_RECVMSG_SENDMSG, and osmo_iofd_ops.

◆ osmo_iofd_register()

int osmo_iofd_register ( struct osmo_io_fd iofd,
int  fd 
)

Register the fd with the underlying backend.

Parameters
[in]iofdthe iofd file descriptor
[in]fdthe system fd number that will be registeres. If negative will use the one already set.
Returns
zero on success, a negative value on error

References osmo_io_fd::current_length, osmo_io_fd::fd, osmo_io_fd::io_ops, IOFD_FLAG_CLOSED, IOFD_FLAG_UNSET, LOGL_ERROR, LOGPIO, osmo_io_fd::mode, OSMO_IO_FD_MODE_READ_WRITE, OSMO_IO_FD_MODE_RECVFROM_SENDTO, OSMO_IO_FD_MODE_RECVMSG_SENDMSG, osmo_iofd_ops, osmo_io_ops::read_cb, iofd_backend_ops::read_enable, osmo_io_ops::recvfrom_cb, osmo_io_ops::recvmsg_cb, iofd_backend_ops::register_fd, osmo_io_fd::tx_queue, and iofd_backend_ops::write_enable.

◆ osmo_iofd_sendmsg_msgb()

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

Appends the message to the internal transmit queue. If the function returns success (0), it will take ownership of the msgb and internally call msgb_free() after the write request completes. In case of an error the msgb needs to be freed by the caller.

Parameters
[in]iofdfile descriptor to write to
[in]msgmessage buffer to send; is used to fill msgh->iov[]
[in]sendmsg_flagsFlags to pass to the send call
[in]msgh'struct msghdr' for name/control/flags. iov must be empty!
Returns
0 in case of success; a negative value in case of error

References iofd_msghdr::cmsg, iofd_msghdr::hdr, osmo_io_fd::io_ops, iofd_msghdr::iofd, IOFD_ACT_SENDMSG, iofd_msghdr_alloc(), iofd_msghdr_free(), iofd_txqueue_enqueue(), iofd_msghdr::iov, LOGL_ERROR, LOGPIO, osmo_io_fd::mode, iofd_msghdr::msg, msgb_data(), msgb_length(), iofd_msghdr::osa, OSMO_ASSERT, OSMO_IO_FD_MODE_RECVMSG_SENDMSG, OSMO_UNLIKELY, osmo_sockaddr::sa, osmo_io_ops::sendmsg_cb, and osmo_sockaddr::u.

◆ 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.

Appends the message to the internal transmit queue. If the function returns success (0), it will take ownership of the msgb and internally call msgb_free() after the write request completes. In case of an error the msgb needs to be freed by the caller.

Parameters
[in]iofdfile descriptor to write to
[in]msgmessage buffer to send
[in]sendto_flagsFlags to pass to the send call
[in]destdestination address to send the message to
Returns
0 in case of success; a negative value in case of error

References iofd_msghdr::flags, iofd_msghdr::hdr, osmo_io_fd::io_ops, iofd_msghdr::iofd, IOFD_ACT_SENDTO, iofd_msghdr_alloc(), iofd_msghdr_free(), iofd_txqueue_enqueue(), iofd_msghdr::iov, LOGL_ERROR, LOGPIO, osmo_io_fd::mode, iofd_msghdr::msg, msgb_data(), msgb_length(), iofd_msghdr::osa, OSMO_ASSERT, OSMO_IO_FD_MODE_RECVFROM_SENDTO, osmo_sockaddr_size(), OSMO_UNLIKELY, osmo_sockaddr::sa, osmo_io_ops::sendto_cb, and osmo_sockaddr::u.

◆ 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.

Parameters
[in]iofdthe file descriptor
[in]sizethe size of the msgb when receiving data
[in]headroomthe headroom of the msgb when receiving data

References osmo_io_fd::headroom, iofd_msghdr::iofd, osmo_io_fd::msgb_alloc, and osmo_io_fd::size.

◆ 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.

References osmo_io_fd::cmsg_size, osmo_io_fd::mode, and OSMO_IO_FD_MODE_RECVMSG_SENDMSG.

◆ osmo_iofd_set_data()

void osmo_iofd_set_data ( struct osmo_io_fd iofd,
void *  data 
)

Set the associated user-data from an iofd.

Parameters
[in]iofdthe file descriptor
[in]datathe data to set

References data, osmo_io_fd::data, and iofd_msghdr::iofd.

◆ osmo_iofd_set_ioops()

◆ osmo_iofd_set_name()

void osmo_iofd_set_name ( struct osmo_io_fd iofd,
const char *  name 
)

Set the name of the file descriptor.

Parameters
[in]iofdthe file descriptor
[in]namethe name to set on the file descriptor

References iofd_msghdr::iofd, name, osmo_io_fd::name, and osmo_talloc_replace_string().

◆ osmo_iofd_set_priv_nr()

void osmo_iofd_set_priv_nr ( struct osmo_io_fd iofd,
unsigned int  priv_nr 
)

Set the private number from an iofd.

Parameters
[in]iofdthe file descriptor
[in]priv_nrthe private number to set

References iofd_msghdr::iofd, and osmo_io_fd::priv_nr.

◆ osmo_iofd_set_txqueue_max_length()

void osmo_iofd_set_txqueue_max_length ( struct osmo_io_fd iofd,
unsigned int  max_length 
)

Set the maximum number of messages enqueued for sending.

Parameters
[in]iofdthe file descriptor
[in]sizethe maximum size of the transmit queue

References iofd_msghdr::iofd, osmo_io_fd::max_length, and osmo_io_fd::tx_queue.

◆ 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.

Parameters
[in]ctxthe parent context from which to allocate
[in]fdthe underlying system file descriptor
[in]namethe name of the iofd
[in]modethe mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
[in]ioopsstructure with read/write/send/recv callbacks
[in]datauser data pointer accessible by the ioops callbacks
Returns
The newly allocated osmo_io_fd struct or NULL on failure

References check_mode_callback_compat(), osmo_io_fd::ctx, data, osmo_io_fd::data, DLIO, osmo_io_fd::fd, osmo_io_fd::headroom, INIT_LLIST_HEAD, osmo_io_fd::io_ops, IOFD_FLAG_CLOSED, IOFD_FLAG_SET, LOGL_ERROR, LOGP, osmo_io_fd::max_length, osmo_io_fd::mode, osmo_io_fd::msg_queue, osmo_io_fd::msgb_alloc, name, osmo_io_fd::name, OSMO_IO_DEFAULT_MSGB_HEADROOM, OSMO_IO_DEFAULT_MSGB_SIZE, OSMO_IO_FD_MODE_READ_WRITE, OSMO_IO_FD_MODE_RECVFROM_SENDTO, OSMO_IO_FD_MODE_RECVMSG_SENDMSG, osmo_iofd_mode_name(), osmo_io_fd::pending, osmo_io_fd::size, and osmo_io_fd::tx_queue.

◆ osmo_iofd_txqueue_clear()

void osmo_iofd_txqueue_clear ( struct osmo_io_fd iofd)

Clear the transmit queue of the the iofd.

This function frees all messages currently pending in the transmit queue

Parameters
[in]iofdthe file descriptor

References iofd_msghdr::hdr, iofd_msghdr::iofd, iofd_msghdr_free(), iofd_txqueue_dequeue(), and msgb_free().

Referenced by osmo_iofd_close().

◆ osmo_iofd_txqueue_len()

unsigned int osmo_iofd_txqueue_len ( struct osmo_io_fd iofd)

Get the number of messages in the tx queue.

Parameters
[in]iofdthe file descriptor

References osmo_io_fd::current_length, and osmo_io_fd::tx_queue.

Referenced by iofd_poll_ofd_cb_recvmsg_sendmsg().

◆ osmo_iofd_unregister()

int osmo_iofd_unregister ( struct osmo_io_fd iofd)

Unregister the fd from the underlying backend.

Parameters
[in]iofdthe file descriptor
Returns
zero on success, a negative value on error

References osmo_iofd_ops, and iofd_backend_ops::unregister_fd.

◆ osmo_iofd_write_msgb()

int osmo_iofd_write_msgb ( struct osmo_io_fd iofd,
struct msgb msg 
)

Send a message through a connected socket.

Appends the message to the internal transmit queue. If the function returns success (0) it will take ownership of the msgb and internally call msgb_free() after the write request completes. In case of an error the msgb needs to be freed by the caller.

Parameters
[in]iofdfile descriptor to write to
[in]msgmessage buffer to write
Returns
0 in case of success; a negative value in case of error

References iofd_msghdr::flags, iofd_msghdr::hdr, osmo_io_fd::io_ops, iofd_msghdr::iofd, IOFD_ACT_WRITE, iofd_msghdr_alloc(), iofd_msghdr_free(), iofd_txqueue_enqueue(), iofd_msghdr::iov, LOGL_ERROR, LOGPIO, osmo_io_fd::mode, msg, iofd_msghdr::msg, msgb_data(), msgb_length(), OSMO_ASSERT, OSMO_IO_FD_MODE_READ_WRITE, OSMO_UNLIKELY, and osmo_io_ops::write_cb.

Variable Documentation

◆ osmo_io_backend_names

const struct value_string osmo_io_backend_names[]
extern

Referenced by osmo_io_backend_name().

◆ osmo_iofd_mode_names

const struct value_string osmo_iofd_mode_names[]
extern

Referenced by osmo_iofd_mode_name().