blob: 7fc4b6b891d1670e3a235bffd6598fdd13c87700 [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \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
19extern const struct iofd_backend_ops iofd_poll_ops;
20#define OSMO_IO_BACKEND_DEFAULT "POLL"
21
22struct 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
32struct 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 */
41 bool closed;
42 bool in_callback;
43 bool to_free;
44
45 bool write_enabled;
46 bool read_enabled;
47
48 /*! human-readable name to associte with fd */
49 const char *name;
50
51 /*! send/recv (msg) callback functions */
52 struct osmo_io_ops io_ops;
53 /*! Pending msgb to keep partial data during segmentation */
54 struct msgb *pending;
55
56 /*! data pointer passed through to call-back function */
57 void *data;
58 /*! private number, extending \a data */
59 unsigned int priv_nr;
60
61 struct {
62 /*! talloc context from which to allocate msgb when reading */
63 const void *ctx;
64 /*! size of msgb to allocate (excluding headroom) */
65 unsigned int size;
66 /*! headroom to allocate when allocating msgb's */
67 unsigned int headroom;
68 } msgb_alloc;
69
70 struct {
71 /*! maximum length of write queue */
72 unsigned int max_length;
73 /*! current length of write queue */
74 unsigned int current_length;
75 /*! actual linked list implementing the transmit queue */
76 struct llist_head msg_queue;
77 } tx_queue;
78
79 union {
80 struct {
81 struct osmo_fd ofd;
82 } poll;
83 struct {
84 bool read_enabled;
85 bool read_pending;
86 bool write_pending;
87 bool write_enabled;
88 /* TODO: index into array of registered fd's? */
89 } uring;
90 } u;
91};
92
93enum iofd_msg_action {
94 IOFD_ACT_READ,
95 IOFD_ACT_WRITE,
96 IOFD_ACT_RECVFROM,
97 IOFD_ACT_SENDTO,
98 // TODO: SCTP_*
99};
100
101
102/* serialized version of 'struct msghdr' employed by sendmsg/recvmsg */
103struct iofd_msghdr {
104 struct llist_head list;
105 enum iofd_msg_action action;
106 struct msghdr hdr;
107 struct osmo_sockaddr osa;
108 struct iovec iov[1];
109 int flags;
110
111 struct msgb *msg;
112 struct osmo_io_fd *iofd;
113};
114
115enum iofd_seg_act {
116 IOFD_SEG_ACT_HANDLE_ONE,
117 IOFD_SEG_ACT_HANDLE_MORE,
118 IOFD_SEG_ACT_DEFER,
119};
120
121struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg);
122void iofd_msghdr_free(struct iofd_msghdr *msghdr);
123
124struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
125struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd);
126struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd);
127
128void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
129
130int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
131void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
132struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd);