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 | |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 7 | #include <sys/socket.h> |
| 8 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 9 | #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 | |
| 19 | struct osmo_io_fd; |
| 20 | |
| 21 | enum 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 Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 26 | /*! emulate recvmsg() / sendmsg() */ |
| 27 | OSMO_IO_FD_MODE_RECVMSG_SENDMSG, |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | enum osmo_io_backend { |
| 31 | OSMO_IO_BACKEND_POLL, |
Daniel Willmann | f91d2aa | 2023-01-04 18:20:55 +0100 | [diff] [blame] | 32 | OSMO_IO_BACKEND_IO_URING, |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | extern const struct value_string osmo_io_backend_names[]; |
| 36 | static inline const char *osmo_io_backend_name(enum osmo_io_backend val) |
| 37 | { return get_value_string(osmo_io_backend_names, val); } |
| 38 | |
| 39 | struct osmo_io_ops { |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 40 | /* 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 Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 58 | |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 59 | /* 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 Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 69 | }; |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 70 | |
| 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 Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
arehbein | 2a405d4 | 2023-09-25 22:03:41 +0200 | [diff] [blame] | 79 | void osmo_iofd_init(void); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 80 | |
| 81 | struct 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 Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 83 | int osmo_iofd_set_cmsg_size(struct osmo_io_fd *iofd, size_t cmsg_size); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 84 | int osmo_iofd_register(struct osmo_io_fd *iofd, int fd); |
| 85 | int osmo_iofd_unregister(struct osmo_io_fd *iofd); |
| 86 | unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd); |
| 87 | void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd); |
| 88 | int osmo_iofd_close(struct osmo_io_fd *iofd); |
| 89 | void osmo_iofd_free(struct osmo_io_fd *iofd); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 90 | |
Daniel Willmann | e2a8dc4 | 2023-06-30 10:51:53 +0200 | [diff] [blame] | 91 | void osmo_iofd_notify_connected(struct osmo_io_fd *iofd); |
| 92 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 93 | int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg); |
| 94 | int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, |
| 95 | const struct osmo_sockaddr *dest); |
Harald Welte | 1047ed7 | 2023-11-18 18:51:58 +0100 | [diff] [blame] | 96 | int osmo_iofd_sendmsg_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendmsg_flags, |
| 97 | const struct msghdr *msgh); |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 98 | |
| 99 | 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] | 100 | 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] | 101 | void *osmo_iofd_get_data(const struct osmo_io_fd *iofd); |
| 102 | void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data); |
| 103 | |
| 104 | unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd); |
| 105 | void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr); |
| 106 | |
| 107 | int osmo_iofd_get_fd(const struct osmo_io_fd *iofd); |
| 108 | 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] | 109 | void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name); |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame] | 110 | |
Harald Welte | b365b1d | 2024-02-23 16:08:49 +0100 | [diff] [blame] | 111 | int osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops); |