blob: 8a7e53abfe45c4eb811ee29eb1c39081d5473189 [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \file osmo_io.h
2 * io(_uring) abstraction osmo fd compatibility
3 */
4
5#pragma once
6
Harald Welte1047ed72023-11-18 18:51:58 +01007#include <sys/socket.h>
8
Harald Welte8857f3b2022-11-18 13:54:44 +01009#include <osmocom/core/linuxlist.h>
10#include <osmocom/core/logging.h>
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
21enum osmo_io_fd_mode {
22 /*! use read() / write() calls */
23 OSMO_IO_FD_MODE_READ_WRITE,
24 /*! use recvfrom() / sendto() calls */
25 OSMO_IO_FD_MODE_RECVFROM_SENDTO,
Harald Welte1047ed72023-11-18 18:51:58 +010026 /*! emulate recvmsg() / sendmsg() */
27 OSMO_IO_FD_MODE_RECVMSG_SENDMSG,
Harald Welte8857f3b2022-11-18 13:54:44 +010028};
29
30enum osmo_io_backend {
31 OSMO_IO_BACKEND_POLL,
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010032 OSMO_IO_BACKEND_IO_URING,
Harald Welte8857f3b2022-11-18 13:54:44 +010033};
34
35extern const struct value_string osmo_io_backend_names[];
36static inline const char *osmo_io_backend_name(enum osmo_io_backend val)
37{ return get_value_string(osmo_io_backend_names, val); }
38
39struct osmo_io_ops {
Harald Welteb365b1d2024-02-23 16:08:49 +010040 /* mode OSMO_IO_FD_MODE_READ_WRITE: */
41 struct {
42 /*! call-back function when something was read from fd */
43 void (*read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
44 /*! call-back function when write has completed on fd */
45 void (*write_cb)(struct osmo_io_fd *iofd, int res,
46 struct msgb *msg);
47 /*! call-back function to segment the data at message boundaries.
48 * Needs to return the size of the next message. If it returns
49 * -EAGAIN or a value larger than msgb_length() (message is incomplete)
50 * osmo_io will wait for more data to be read. Other negative values
51 * cause the msg to be discarded.
52 * If a full message was received (segmentation_cb() returns a value <= msgb_length())
53 * the msgb will be trimmed to size by osmo_io and forwarded to the read call-back. Any
54 * parsing done to the msgb by segmentation_cb() will be preserved for the read_cb()
55 * (e.g. setting lxh or msgb->cb). */
56 int (*segmentation_cb)(struct msgb *msg);
57 };
Harald Welte8857f3b2022-11-18 13:54:44 +010058
Harald Welteb365b1d2024-02-23 16:08:49 +010059 /* mode OSMO_IO_FD_MODE_RECVFROM_SENDTO: */
60 struct {
61 /*! call-back function emulating recvfrom */
62 void (*recvfrom_cb)(struct osmo_io_fd *iofd, int res,
63 struct msgb *msg,
64 const struct osmo_sockaddr *saddr);
65 /*! call-back function emulating sendto */
66 void (*sendto_cb)(struct osmo_io_fd *iofd, int res,
67 struct msgb *msg,
68 const struct osmo_sockaddr *daddr);
Harald Welte8857f3b2022-11-18 13:54:44 +010069 };
Harald Welte1047ed72023-11-18 18:51:58 +010070
71 /* mode OSMO_IO_FD_MODE_RECVMSG_SENDMSG: */
72 struct {
73 void (*recvmsg_cb)(struct osmo_io_fd *iofd, int res,
74 struct msgb *msg, const struct msghdr *msgh);
75 void (*sendmsg_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg);
76 };
Harald Welte8857f3b2022-11-18 13:54:44 +010077};
78
arehbein2a405d42023-09-25 22:03:41 +020079void osmo_iofd_init(void);
Harald Welte8857f3b2022-11-18 13:54:44 +010080
81struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name,
82 enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data);
Harald Welte1047ed72023-11-18 18:51:58 +010083int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size);
Harald Welte8857f3b2022-11-18 13:54:44 +010084int osmo_iofd_register(struct osmo_io_fd *iofd, int fd);
85int osmo_iofd_unregister(struct osmo_io_fd *iofd);
86unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd);
87void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd);
88int osmo_iofd_close(struct osmo_io_fd *iofd);
89void osmo_iofd_free(struct osmo_io_fd *iofd);
Harald Welte8857f3b2022-11-18 13:54:44 +010090
Daniel Willmanne2a8dc42023-06-30 10:51:53 +020091void osmo_iofd_notify_connected(struct osmo_io_fd *iofd);
92
Harald Welte8857f3b2022-11-18 13:54:44 +010093int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg);
94int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags,
95 const struct osmo_sockaddr *dest);
Harald Welte1047ed72023-11-18 18:51:58 +010096int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags,
97 const struct msghdr *msgh);
Harald Welte8857f3b2022-11-18 13:54:44 +010098
99void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom);
Daniel Willmanna9303f32023-07-07 11:20:48 +0200100void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int size);
Harald Welte8857f3b2022-11-18 13:54:44 +0100101void *osmo_iofd_get_data(const struct osmo_io_fd *iofd);
102void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data);
103
104unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd);
105void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr);
106
107int osmo_iofd_get_fd(const struct osmo_io_fd *iofd);
108const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd);
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200109void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name);
arehbein0c374c62023-05-14 21:43:11 +0200110
Harald Welteb365b1d2024-02-23 16:08:49 +0100111int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops);
Harald Weltef574aea2024-02-23 12:07:03 +0100112void osmo_iofd_get_ioops(struct osmo_io_fd *iofd, struct osmo_io_ops *ioops);