blob: cd620e79b53a5549563139091c1d287de2b13041 [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 */
Daniel Willmanneb9edba2023-06-06 16:53:38 +020041 /* TODO: Move to bitfield */
Harald Welte8857f3b2022-11-18 13:54:44 +010042 bool closed;
43 bool in_callback;
44 bool to_free;
45
Harald Welte8857f3b2022-11-18 13:54:44 +010046 /*! human-readable name to associte with fd */
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +020047 char *name;
Harald Welte8857f3b2022-11-18 13:54:44 +010048
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
91enum 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 */
101struct 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
113enum iofd_seg_act {
114 IOFD_SEG_ACT_HANDLE_ONE,
115 IOFD_SEG_ACT_HANDLE_MORE,
116 IOFD_SEG_ACT_DEFER,
117};
118
119struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg);
120void iofd_msghdr_free(struct iofd_msghdr *msghdr);
121
122struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
123struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd);
124struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd);
125
126void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
127
128int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
129void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
130struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd);