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, |
| 30 | }; |
| 31 | |
| 32 | extern const struct value_string osmo_io_backend_names[]; |
| 33 | static inline const char *osmo_io_backend_name(enum osmo_io_backend val) |
| 34 | { return get_value_string(osmo_io_backend_names, val); } |
| 35 | |
| 36 | struct osmo_io_ops { |
| 37 | union { |
| 38 | /* mode OSMO_IO_FD_MODE_READ_WRITE: */ |
| 39 | struct { |
| 40 | /*! call-back function when something was read from fd */ |
| 41 | void (*read_cb)(struct osmo_io_fd *iofd, int res, struct msgb *msg); |
| 42 | /*! call-back function when write has completed on fd */ |
| 43 | void (*write_cb)(struct osmo_io_fd *iofd, int res, |
| 44 | const struct msgb *msg); |
| 45 | /*! call-back function to segment the data returned by read_cb */ |
| 46 | int (*segmentation_cb)(struct msgb *msg, int data_len); |
| 47 | }; |
| 48 | |
| 49 | /* mode OSMO_IO_FD_MODE_RECVFROM_SENDTO: */ |
| 50 | struct { |
| 51 | /*! call-back function emulating sendto */ |
| 52 | void (*sendto_cb)(struct osmo_io_fd *iofd, int res, |
| 53 | const struct msgb *msg, |
| 54 | const struct osmo_sockaddr *daddr); |
| 55 | /*! call-back function emulating recvfrom */ |
| 56 | void (*recvfrom_cb)(struct osmo_io_fd *iofd, int res, |
| 57 | struct msgb *msg, |
| 58 | const struct osmo_sockaddr *saddr); |
| 59 | }; |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | void osmo_io_init(void); |
| 64 | |
| 65 | struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, |
| 66 | enum osmo_io_fd_mode mode, const struct osmo_io_ops *ioops, void *data); |
| 67 | int osmo_iofd_register(struct osmo_io_fd *iofd, int fd); |
| 68 | int osmo_iofd_unregister(struct osmo_io_fd *iofd); |
| 69 | unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd); |
| 70 | void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd); |
| 71 | int osmo_iofd_close(struct osmo_io_fd *iofd); |
| 72 | void osmo_iofd_free(struct osmo_io_fd *iofd); |
| 73 | void osmo_iofd_read_enable(struct osmo_io_fd *iofd); |
| 74 | void osmo_iofd_read_disable(struct osmo_io_fd *iofd); |
| 75 | void osmo_iofd_write_enable(struct osmo_io_fd *iofd); |
| 76 | void osmo_iofd_write_disable(struct osmo_io_fd *iofd); |
| 77 | |
| 78 | int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg); |
| 79 | int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, |
| 80 | const struct osmo_sockaddr *dest); |
| 81 | |
| 82 | void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom); |
| 83 | void *osmo_iofd_get_data(const struct osmo_io_fd *iofd); |
| 84 | void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data); |
| 85 | |
| 86 | unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd); |
| 87 | void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr); |
| 88 | |
| 89 | int osmo_iofd_get_fd(const struct osmo_io_fd *iofd); |
| 90 | const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd); |
arehbein | 0c374c6 | 2023-05-14 21:43:11 +0200 | [diff] [blame^] | 91 | |
| 92 | void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops); |