Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 1 | /*! \file osmo_io.h |
| 2 | * io(_uring) abstraction osmo fd compatibility |
| 3 | */ |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <osmocom/core/linuxlist.h> |
| 8 | #include <osmocom/core/logging.h> |
| 9 | #include <osmocom/core/msgb.h> |
| 10 | #include <osmocom/core/socket.h> |
| 11 | #include <osmocom/core/utils.h> |
| 12 | |
| 13 | |
| 14 | #define LOGPIO(iofd, level, fmt, args...) \ |
| 15 | LOGP(DLIO, level, "iofd(%s)" fmt, iofd->name, ## args) |
| 16 | |
| 17 | struct osmo_io_fd; |
| 18 | |
| 19 | enum osmo_io_fd_mode { |
| 20 | /*! use read() / write() calls */ |
| 21 | OSMO_IO_FD_MODE_READ_WRITE, |
| 22 | /*! use recvfrom() / sendto() calls */ |
| 23 | OSMO_IO_FD_MODE_RECVFROM_SENDTO, |
| 24 | /*! emulate sctp_recvmsg() and sctp_sendmsg() */ |
| 25 | OSMO_IO_FD_MODE_SCTP_RECVMSG_SENDMSG, |
| 26 | }; |
| 27 | |
| 28 | enum osmo_io_backend { |
| 29 | OSMO_IO_BACKEND_POLL, |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 30 | OSMO_IO_BACKEND_IO_URING, |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | extern const struct value_string osmo_io_backend_names[]; |
| 34 | static inline const char *osmo_io_backend_name(enum osmo_io_backend val) |
| 35 | { return get_value_string(osmo_io_backend_names, val); } |
| 36 | |
| 37 | struct osmo_io_ops { |
| 38 | union { |
| 39 | /* mode OSMO_IO_FD_MODE_READ_WRITE: */ |
| 40 | struct { |
| 41 | /*! call-back function when something was read from fd */ |
| 42 | void (*read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg); |
| 43 | /*! call-back function when write has completed on fd */ |
| 44 | void (*write_cb)(struct osmo_io_fd *iofd, int res, |
Daniel Willmann | 3ed8772 | 2023-06-06 11:34:14 +0200 | [diff] [blame] | 45 | struct msgb *msg); |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 46 | /*! call-back function to segment the data at message boundaries. |
| 47 | * Needs to return the size of the next message. If it returns |
| 48 | * -EAGAIN or a value larger than msgb_length() (message is incomplete) |
| 49 | * osmo_io will wait for more data to be read. Other negative values |
Daniel Willmann | 7b59bab | 2023-07-07 11:17:59 +0200 | [diff] [blame] | 50 | * cause the msg to be discarded. |
| 51 | * If a full message was received (segmentation_cb() returns a value <= msgb_length()) |
| 52 | * the msgb will be trimmed to size by osmo_io and forwarded to the read call-back. Any |
| 53 | * parsing done to the msgb by segmentation_cb() will be preserved for the read_cb() |
| 54 | * (e.g. setting lxh or msgb->cb). */ |
Daniel Willmann | d4d0370 | 2023-05-17 12:38:14 +0200 | [diff] [blame] | 55 | int (*segmentation_cb)(struct msgb *msg); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /* mode OSMO_IO_FD_MODE_RECVFROM_SENDTO: */ |
| 59 | struct { |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 60 | /*! call-back function emulating recvfrom */ |
| 61 | void (*recvfrom_cb)(struct osmo_io_fd *iofd, int res, |
| 62 | struct msgb *msg, |
| 63 | const struct osmo_sockaddr *saddr); |
Daniel Willmann | 2dd8da6 | 2023-06-06 11:34:51 +0200 | [diff] [blame] | 64 | /*! call-back function emulating sendto */ |
| 65 | void (*sendto_cb)(struct osmo_io_fd *iofd, int res, |
Daniel Willmann | 3ed8772 | 2023-06-06 11:34:14 +0200 | [diff] [blame] | 66 | struct msgb *msg, |
Daniel Willmann | 2dd8da6 | 2023-06-06 11:34:51 +0200 | [diff] [blame] | 67 | const struct osmo_sockaddr *daddr); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 68 | }; |
| 69 | }; |
| 70 | }; |
| 71 | |
| 72 | void osmo_io_init(void); |
| 73 | |
| 74 | struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, |
| 75 | enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data); |
| 76 | int osmo_iofd_register(struct osmo_io_fd *iofd, int fd); |
| 77 | int osmo_iofd_unregister(struct osmo_io_fd *iofd); |
| 78 | unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd); |
| 79 | void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd); |
| 80 | int osmo_iofd_close(struct osmo_io_fd *iofd); |
| 81 | void osmo_iofd_free(struct osmo_io_fd *iofd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 82 | |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 83 | void osmo_iofd_notify_connected(struct osmo_io_fd *iofd); |
| 84 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 85 | int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg); |
| 86 | int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, |
| 87 | const struct osmo_sockaddr *dest); |
| 88 | |
| 89 | void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom); |
Daniel Willmann | a9303f3 | 2023-07-07 11:20:48 +0200 | [diff] [blame] | 90 | void osmo_iofd_set_txqueue_max_length(struct osmo_io_fd *iofd, unsigned int size); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 91 | void *osmo_iofd_get_data(const struct osmo_io_fd *iofd); |
| 92 | void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data); |
| 93 | |
| 94 | unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd); |
| 95 | void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr); |
| 96 | |
| 97 | int osmo_iofd_get_fd(const struct osmo_io_fd *iofd); |
| 98 | const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd); |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame] | 99 | void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name); |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 100 | |
| 101 | void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops); |