Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 1 | /*! \file osmo_io_internal.h */ |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <unistd.h> |
| 6 | #include <stdbool.h> |
| 7 | |
| 8 | #include <osmocom/core/osmo_io.h> |
| 9 | #include <osmocom/core/linuxlist.h> |
| 10 | #include <osmocom/core/msgb.h> |
| 11 | #include <osmocom/core/select.h> |
| 12 | #include <osmocom/core/socket.h> |
| 13 | |
| 14 | #include "../config.h" |
| 15 | |
| 16 | #define OSMO_IO_DEFAULT_MSGB_SIZE 1024 |
| 17 | #define OSMO_IO_DEFAULT_MSGB_HEADROOM 128 |
| 18 | |
| 19 | extern const struct iofd_backend_ops iofd_poll_ops; |
| 20 | #define OSMO_IO_BACKEND_DEFAULT "POLL" |
| 21 | |
| 22 | struct iofd_backend_ops { |
| 23 | int (*register_fd)(struct osmo_io_fd *iofd); |
| 24 | int (*unregister_fd)(struct osmo_io_fd *iofd); |
| 25 | int (*close)(struct osmo_io_fd *iofd); |
| 26 | void (*write_enable)(struct osmo_io_fd *iofd); |
| 27 | void (*write_disable)(struct osmo_io_fd *iofd); |
| 28 | void (*read_enable)(struct osmo_io_fd *iofd); |
| 29 | void (*read_disable)(struct osmo_io_fd *iofd); |
| 30 | }; |
| 31 | |
| 32 | struct osmo_io_fd { |
| 33 | /*! linked list for internal management */ |
| 34 | struct llist_head list; |
| 35 | /*! actual operating-system level file decriptor */ |
| 36 | int fd; |
| 37 | /*! type of read/write mode to use */ |
| 38 | enum osmo_io_fd_mode mode; |
| 39 | |
| 40 | /*! flags to guard closing/freeing of iofd */ |
Daniel Willmann | eb9edba | 2023-06-06 16:53:38 +0200 | [diff] [blame] | 41 | /* TODO: Move to bitfield */ |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 42 | bool closed; |
| 43 | bool in_callback; |
| 44 | bool to_free; |
| 45 | |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 46 | /*! human-readable name to associte with fd */ |
Pau Espin Pedrol | 63e45e6 | 2023-06-16 16:19:45 +0200 | [diff] [blame^] | 47 | char *name; |
Harald Welte | 8857f3b | 2022-11-18 13:54:44 +0100 | [diff] [blame] | 48 | |
| 49 | /*! send/recv (msg) callback functions */ |
| 50 | struct osmo_io_ops io_ops; |
| 51 | /*! Pending msgb to keep partial data during segmentation */ |
| 52 | struct msgb *pending; |
| 53 | |
| 54 | /*! data pointer passed through to call-back function */ |
| 55 | void *data; |
| 56 | /*! private number, extending \a data */ |
| 57 | unsigned int priv_nr; |
| 58 | |
| 59 | struct { |
| 60 | /*! talloc context from which to allocate msgb when reading */ |
| 61 | const void *ctx; |
| 62 | /*! size of msgb to allocate (excluding headroom) */ |
| 63 | unsigned int size; |
| 64 | /*! headroom to allocate when allocating msgb's */ |
| 65 | unsigned int headroom; |
| 66 | } msgb_alloc; |
| 67 | |
| 68 | struct { |
| 69 | /*! maximum length of write queue */ |
| 70 | unsigned int max_length; |
| 71 | /*! current length of write queue */ |
| 72 | unsigned int current_length; |
| 73 | /*! actual linked list implementing the transmit queue */ |
| 74 | struct llist_head msg_queue; |
| 75 | } tx_queue; |
| 76 | |
| 77 | union { |
| 78 | struct { |
| 79 | struct osmo_fd ofd; |
| 80 | } poll; |
| 81 | struct { |
| 82 | bool read_enabled; |
| 83 | bool read_pending; |
| 84 | bool write_pending; |
| 85 | bool write_enabled; |
| 86 | /* TODO: index into array of registered fd's? */ |
| 87 | } uring; |
| 88 | } u; |
| 89 | }; |
| 90 | |
| 91 | enum iofd_msg_action { |
| 92 | IOFD_ACT_READ, |
| 93 | IOFD_ACT_WRITE, |
| 94 | IOFD_ACT_RECVFROM, |
| 95 | IOFD_ACT_SENDTO, |
| 96 | // TODO: SCTP_* |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | /* serialized version of 'struct msghdr' employed by sendmsg/recvmsg */ |
| 101 | struct iofd_msghdr { |
| 102 | struct llist_head list; |
| 103 | enum iofd_msg_action action; |
| 104 | struct msghdr hdr; |
| 105 | struct osmo_sockaddr osa; |
| 106 | struct iovec iov[1]; |
| 107 | int flags; |
| 108 | |
| 109 | struct msgb *msg; |
| 110 | struct osmo_io_fd *iofd; |
| 111 | }; |
| 112 | |
| 113 | enum iofd_seg_act { |
| 114 | IOFD_SEG_ACT_HANDLE_ONE, |
| 115 | IOFD_SEG_ACT_HANDLE_MORE, |
| 116 | IOFD_SEG_ACT_DEFER, |
| 117 | }; |
| 118 | |
| 119 | struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg); |
| 120 | void iofd_msghdr_free(struct iofd_msghdr *msghdr); |
| 121 | |
| 122 | struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd); |
| 123 | struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd); |
| 124 | struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd); |
| 125 | |
| 126 | void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc); |
| 127 | |
| 128 | int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr); |
| 129 | void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr); |
| 130 | struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd); |