libosmocore  1.12.1
Osmocom core library
osmo_io_internal.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <unistd.h>
6 #include <stdbool.h>
7 #include <netinet/sctp.h>
8 
9 #include <osmocom/core/osmo_io.h>
10 #include <osmocom/core/linuxlist.h>
11 #include <osmocom/core/msgb.h>
12 #include <osmocom/core/select.h>
13 #include <osmocom/core/socket.h>
14 
15 #include "../config.h"
16 
17 #define OSMO_IO_DEFAULT_MSGB_SIZE 1024
18 #define OSMO_IO_DEFAULT_MSGB_HEADROOM 128
19 
20 extern const struct iofd_backend_ops iofd_poll_ops;
21 #define OSMO_IO_BACKEND_DEFAULT "POLL"
22 
23 #if defined(HAVE_URING)
24 extern const struct iofd_backend_ops iofd_uring_ops;
25 #endif
26 
28  int (*setup)(struct osmo_io_fd *iofd);
29  int (*register_fd)(struct osmo_io_fd *iofd);
30  int (*unregister_fd)(struct osmo_io_fd *iofd);
31  int (*close)(struct osmo_io_fd *iofd);
32  void (*write_enable)(struct osmo_io_fd *iofd);
33  void (*write_disable)(struct osmo_io_fd *iofd);
34  void (*read_enable)(struct osmo_io_fd *iofd);
35  void (*read_disable)(struct osmo_io_fd *iofd);
36  void (*notify_connected)(struct osmo_io_fd *iofd);
37 };
38 
39 #define IOFD_FLAG_CLOSED (1<<0)
40 #define IOFD_FLAG_IN_CALLBACK (1<<1)
41 #define IOFD_FLAG_TO_FREE (1<<2)
42 #define IOFD_FLAG_NOTIFY_CONNECTED (1<<3)
43 #define IOFD_FLAG_FD_REGISTERED (1<<4)
44 
45 #define IOFD_FLAG_SET(iofd, flag) \
46  (iofd)->flags |= (flag)
47 
48 #define IOFD_FLAG_UNSET(iofd, flag) \
49  (iofd)->flags &= ~(flag)
50 
51 #define IOFD_FLAG_ISSET(iofd, flag) ((iofd)->flags & (flag))
52 
53 #define IOFD_MSGHDR_MAX_READ_SQES 32
54 
55 struct osmo_io_fd {
57  struct llist_head list;
59  int fd;
61  enum osmo_io_fd_mode mode;
62 
64  uint32_t flags;
65 
67  char *name;
68 
70  struct osmo_io_ops io_ops;
72  struct msgb *pending;
73 
75  void *data;
77  unsigned int priv_nr;
78 
80  size_t cmsg_size;
81 
83  uint8_t io_read_buffers;
84 
87 
88  struct {
90  const void *ctx;
92  unsigned int size;
94  unsigned int headroom;
96 
97  struct {
99  unsigned int max_length;
101  unsigned int current_length;
103  struct llist_head msg_queue;
105 
106  union {
107  struct {
108  struct osmo_fd ofd;
109  } poll;
110  struct {
111  struct {
113  bool enabled;
115  uint8_t num_sqes;
119  struct io_uring *ring;
121  uint8_t sqes_submitted;
122  } read;
123  struct {
125  bool enabled;
127  void *msghdr;
129  struct io_uring *ring;
130  } write;
131  /* TODO: index into array of registered fd's? */
132  /* osmo_fd for non-blocking connect handling */
133  struct osmo_fd connect_ofd;
134  } uring;
135  } u;
136 };
137 
145 };
146 
147 #define IOFD_MSGHDR_IO_BUFFERS 8
148 
150 struct iofd_msghdr {
152  struct llist_head list;
153  enum iofd_msg_action action;
155  struct msghdr hdr;
157  struct osmo_sockaddr osa;
160  struct iovec iov[IOFD_MSGHDR_IO_BUFFERS];
162  int flags;
163 
165  uint8_t io_len;
169  struct osmo_io_fd *iofd;
170 
173 
175  char cmsg[0]; /* size is determined by iofd->cmsg_size on recvmsg, and by mcghdr->msg_controllen on sendmsg */
176 };
177 
182 };
183 
184 struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size);
185 void iofd_msghdr_free(struct iofd_msghdr *msghdr);
186 
187 struct msgb *iofd_msgb_alloc2(struct osmo_io_fd *iofd, size_t size);
188 struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
189 
190 void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr);
191 void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr);
192 void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
193 
194 int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
195 void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
Enqueue a message at the front.
Definition: osmo_io.c:234
osmo_io_fd_mode
The mode of an osmo_io_fd determines if read/write, recvfrom/sendmsg or recvmsg/sendmsg semantics are...
Definition: osmo_io.h:64
void iofd_msghdr_free(struct iofd_msghdr *msghdr)
Free the msghdr.
Definition: osmo_io.c:180
void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
Restore message boundaries on read() and pass individual messages to the read callback.
Definition: osmo_io.c:362
void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr)
completion handler: Internal function called by osmo_io_backend after a given I/O operation has compl...
Definition: osmo_io.c:449
struct msgb * iofd_msgb_alloc(struct osmo_io_fd *iofd)
convenience wrapper to call msgb_alloc with parameters from osmo_io_fd
Definition: osmo_io.c:197
struct iofd_msghdr * iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size)
Allocate the msghdr.
Definition: osmo_io.c:138
struct iofd_msghdr * iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
Dequeue a message from the front.
Definition: osmo_io.c:248
int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
Enqueue a message to be sent.
Definition: osmo_io.c:210
void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr)
completion handler: Internal function called by osmo_io_backend after a given I/O operation has compl...
Definition: osmo_io.c:424
struct msgb * iofd_msgb_alloc2(struct osmo_io_fd *iofd, size_t size)
convenience wrapper to call msgb_alloc with parameters from osmo_io_fd (of given size)
Definition: osmo_io.c:188
Simple doubly linked list implementation.
io(_uring) abstraction osmo fd compatibility
#define IOFD_MSGHDR_MAX_READ_SQES
Definition: osmo_io_internal.h:53
#define IOFD_MSGHDR_IO_BUFFERS
Definition: osmo_io_internal.h:147
iofd_msg_action
Definition: osmo_io_internal.h:138
@ IOFD_ACT_RECVFROM
Definition: osmo_io_internal.h:141
@ IOFD_ACT_RECVMSG
Definition: osmo_io_internal.h:143
@ IOFD_ACT_SENDTO
Definition: osmo_io_internal.h:142
@ IOFD_ACT_READ
Definition: osmo_io_internal.h:139
@ IOFD_ACT_WRITE
Definition: osmo_io_internal.h:140
@ IOFD_ACT_SENDMSG
Definition: osmo_io_internal.h:144
iofd_seg_act
Definition: osmo_io_internal.h:178
@ IOFD_SEG_ACT_DEFER
Definition: osmo_io_internal.h:181
@ IOFD_SEG_ACT_HANDLE_MORE
Definition: osmo_io_internal.h:180
@ IOFD_SEG_ACT_HANDLE_ONE
Definition: osmo_io_internal.h:179
const struct iofd_backend_ops iofd_poll_ops
Definition: osmo_io_poll.c:201
libmnl integration
Osmocom socket convenience functions.
Definition: osmo_io_internal.h:27
void(* read_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:34
void(* write_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:32
void(* read_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:35
void(* write_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:33
int(* close)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:31
int(* setup)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:28
int(* register_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:29
void(* notify_connected)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:36
int(* unregister_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:30
serialized version of 'struct msghdr' employed by sendmsg/recvmsg
Definition: osmo_io_internal.h:150
char cmsg[0]
control message buffer for passing sctp_sndrcvinfo along
Definition: osmo_io_internal.h:175
struct osmo_io_fd * iofd
I/O file descriptor on which we perform this I/O operation.
Definition: osmo_io_internal.h:169
struct llist_head list
entry into osmo_io_fd.tx_queue.msg_queue
Definition: osmo_io_internal.h:152
int flags
flags we pass as argument to sendmsg / recvmsg
Definition: osmo_io_internal.h:162
enum iofd_msg_action action
Definition: osmo_io_internal.h:153
uint8_t io_len
current number of message-buffers that are stored
Definition: osmo_io_internal.h:165
struct msghdr hdr
the 'struct msghdr' we are wrapping/ecapsulating here
Definition: osmo_io_internal.h:155
struct osmo_sockaddr osa
socket address of the remote peer
Definition: osmo_io_internal.h:157
struct msgb * msg[IOFD_MSGHDR_IO_BUFFERS]
message-buffer containing data for this I/O operation
Definition: osmo_io_internal.h:167
bool in_cancel_queue
msghdr is in the cancel_queue list
Definition: osmo_io_internal.h:172
struct iovec iov[IOFD_MSGHDR_IO_BUFFERS]
io-vector we need to pass as argument to sendmsg/recvmsg; is set up to point into msg below
Definition: osmo_io_internal.h:160
(double) linked list header structure
Definition: linuxlist.h:46
Osmocom message buffer.
Definition: msgb.h:31
Structure representing a file dsecriptor.
Definition: select.h:31
Definition: osmo_io_internal.h:55
uint32_t flags
flags to guard closing/freeing of iofd
Definition: osmo_io_internal.h:64
unsigned int current_length
current length of write queue
Definition: osmo_io_internal.h:101
struct msgb * pending
Pending msgb to keep partial data during segmentation.
Definition: osmo_io_internal.h:72
struct osmo_io_fd::@39 tx_queue
union osmo_io_fd::@40 u
uint8_t num_sqes
requested number of simultaniously submitted read SQEs
Definition: osmo_io_internal.h:115
bool enabled
read is enabled, due to registration of callback function
Definition: osmo_io_internal.h:113
struct llist_head msg_queue
actual linked list implementing the transmit queue
Definition: osmo_io_internal.h:103
struct osmo_io_fd::@40::@42::@43 read
struct osmo_io_fd::@40::@42::@44 write
struct io_uring * ring
ring the read SQEs have been submitted to
Definition: osmo_io_internal.h:119
enum osmo_io_fd_mode mode
type of read/write mode to use
Definition: osmo_io_internal.h:61
struct osmo_io_fd::@38 msgb_alloc
char * name
human-readable name to associte with fd
Definition: osmo_io_internal.h:67
struct llist_head list
linked list for internal management
Definition: osmo_io_internal.h:57
unsigned int max_length
maximum length of write queue
Definition: osmo_io_internal.h:99
size_t cmsg_size
size of iofd_msghdr.cmsg[] when allocated in recvmsg path
Definition: osmo_io_internal.h:80
uint8_t sqes_submitted
current number of simultaneously submitted read SQEs
Definition: osmo_io_internal.h:121
uint8_t io_write_buffers
maximum number of message-buffers per write operation
Definition: osmo_io_internal.h:86
struct osmo_fd connect_ofd
Definition: osmo_io_internal.h:133
struct osmo_io_fd::@40::@41 poll
struct osmo_io_fd::@40::@42 uring
unsigned int headroom
headroom to allocate when allocating msgb's
Definition: osmo_io_internal.h:94
struct osmo_fd ofd
Definition: osmo_io_internal.h:108
unsigned int size
size of msgb to allocate (excluding headroom)
Definition: osmo_io_internal.h:92
void * msghdr[IOFD_MSGHDR_MAX_READ_SQES]
array of simultaneously submitted read SQEs
Definition: osmo_io_internal.h:117
int fd
actual operating-system level file decriptor
Definition: osmo_io_internal.h:59
const void * ctx
talloc context from which to allocate msgb when reading
Definition: osmo_io_internal.h:90
uint8_t io_read_buffers
maximum number of message-buffers per read operation
Definition: osmo_io_internal.h:83
struct osmo_io_ops io_ops
send/recv (msg) callback functions
Definition: osmo_io_internal.h:70
void * data
data pointer passed through to call-back function
Definition: osmo_io_internal.h:75
unsigned int priv_nr
private number, extending data
Definition: osmo_io_internal.h:77
I/O operations (call-back functions) related to an osmo_io_fd.
Definition: osmo_io.h:103
Definition: socket.h:38