libosmocore  1.10.0.13-ddc5.202410052026
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 (*register_fd)(struct osmo_io_fd *iofd);
29  int (*unregister_fd)(struct osmo_io_fd *iofd);
30  int (*close)(struct osmo_io_fd *iofd);
31  void (*write_enable)(struct osmo_io_fd *iofd);
32  void (*write_disable)(struct osmo_io_fd *iofd);
33  void (*read_enable)(struct osmo_io_fd *iofd);
34  void (*read_disable)(struct osmo_io_fd *iofd);
35  void (*notify_connected)(struct osmo_io_fd *iofd);
36 };
37 
38 #define IOFD_FLAG_CLOSED (1<<0)
39 #define IOFD_FLAG_IN_CALLBACK (1<<1)
40 #define IOFD_FLAG_TO_FREE (1<<2)
41 #define IOFD_FLAG_NOTIFY_CONNECTED (1<<3)
42 #define IOFD_FLAG_FD_REGISTERED (1<<4)
43 
44 #define IOFD_FLAG_SET(iofd, flag) \
45  (iofd)->flags |= (flag)
46 
47 #define IOFD_FLAG_UNSET(iofd, flag) \
48  (iofd)->flags &= ~(flag)
49 
50 #define IOFD_FLAG_ISSET(iofd, flag) ((iofd)->flags & (flag))
51 
52 struct osmo_io_fd {
54  struct llist_head list;
56  int fd;
58  enum osmo_io_fd_mode mode;
59 
61  uint32_t flags;
62 
64  char *name;
65 
67  struct osmo_io_ops io_ops;
69  struct msgb *pending;
70 
72  void *data;
74  unsigned int priv_nr;
75 
77  size_t cmsg_size;
78 
79  struct {
81  const void *ctx;
83  unsigned int size;
85  unsigned int headroom;
87 
88  struct {
90  unsigned int max_length;
92  unsigned int current_length;
94  struct llist_head msg_queue;
96 
97  union {
98  struct {
99  struct osmo_fd ofd;
100  } poll;
101  struct {
104  void *read_msghdr;
106  /* TODO: index into array of registered fd's? */
107  /* osmo_fd for non-blocking connect handling */
108  struct osmo_fd connect_ofd;
109  } uring;
110  } u;
111 };
112 
120 };
121 
122 
124 struct iofd_msghdr {
126  struct llist_head list;
127  enum iofd_msg_action action;
129  struct msghdr hdr;
131  struct osmo_sockaddr osa;
134  struct iovec iov[1];
136  int flags;
137 
139  struct msgb *msg;
141  struct osmo_io_fd *iofd;
142 
144  char cmsg[0]; /* size is determined by iofd->cmsg_size on recvmsg, and by mcghdr->msg_controllen on sendmsg */
145 };
146 
151 };
152 
153 struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg, size_t cmsg_size);
154 void iofd_msghdr_free(struct iofd_msghdr *msghdr);
155 
156 struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
157 struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd);
158 struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd);
159 
160 void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr);
161 void iofd_handle_send_completion(struct osmo_io_fd *iofd, int rc, struct iofd_msghdr *msghdr);
162 void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
163 
164 int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
165 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:231
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:163
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:328
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:381
struct msgb * iofd_msgb_pending(struct osmo_io_fd *iofd)
return the pending msgb in iofd or NULL if there is none
Definition: osmo_io.c:180
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:171
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:131
struct iofd_msghdr * iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
Dequeue a message from the front.
Definition: osmo_io.c:245
struct msgb * iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
Return the pending msgb or allocate and return a new one.
Definition: osmo_io.c:191
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:357
Simple doubly linked list implementation.
io(_uring) abstraction osmo fd compatibility
iofd_msg_action
Definition: osmo_io_internal.h:113
@ IOFD_ACT_RECVFROM
Definition: osmo_io_internal.h:116
@ IOFD_ACT_RECVMSG
Definition: osmo_io_internal.h:118
@ IOFD_ACT_SENDTO
Definition: osmo_io_internal.h:117
@ IOFD_ACT_READ
Definition: osmo_io_internal.h:114
@ IOFD_ACT_WRITE
Definition: osmo_io_internal.h:115
@ IOFD_ACT_SENDMSG
Definition: osmo_io_internal.h:119
iofd_seg_act
Definition: osmo_io_internal.h:147
@ IOFD_SEG_ACT_DEFER
Definition: osmo_io_internal.h:150
@ IOFD_SEG_ACT_HANDLE_MORE
Definition: osmo_io_internal.h:149
@ IOFD_SEG_ACT_HANDLE_ONE
Definition: osmo_io_internal.h:148
const struct iofd_backend_ops iofd_poll_ops
Definition: osmo_io_poll.c:190
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:33
void(* write_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:31
void(* read_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:34
void(* write_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:32
int(* close)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:30
int(* register_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:28
void(* notify_connected)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:35
int(* unregister_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:29
serialized version of 'struct msghdr' employed by sendmsg/recvmsg
Definition: osmo_io_internal.h:124
char cmsg[0]
control message buffer for passing sctp_sndrcvinfo along
Definition: osmo_io_internal.h:144
struct osmo_io_fd * iofd
I/O file descriptor on which we perform this I/O operation.
Definition: osmo_io_internal.h:141
struct llist_head list
entry into osmo_io_fd.tx_queue.msg_queue
Definition: osmo_io_internal.h:126
int flags
flags we pass as argument to sendmsg / recvmsg
Definition: osmo_io_internal.h:136
enum iofd_msg_action action
Definition: osmo_io_internal.h:127
struct msgb * msg
message-buffer containing data for this I/O operation
Definition: osmo_io_internal.h:139
struct iovec iov[1]
io-vector we need to pass as argument to sendmsg/recvmsg; is set up to point into msg below
Definition: osmo_io_internal.h:134
struct msghdr hdr
the 'struct msghdr' we are wrapping/ecapsulating here
Definition: osmo_io_internal.h:129
struct osmo_sockaddr osa
socket address of the remote peer
Definition: osmo_io_internal.h:131
(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:52
uint32_t flags
flags to guard closing/freeing of iofd
Definition: osmo_io_internal.h:61
unsigned int current_length
current length of write queue
Definition: osmo_io_internal.h:92
void * read_msghdr
Definition: osmo_io_internal.h:104
struct msgb * pending
Pending msgb to keep partial data during segmentation.
Definition: osmo_io_internal.h:69
bool read_enabled
Definition: osmo_io_internal.h:102
struct osmo_io_fd::@39 tx_queue
union osmo_io_fd::@40 u
void * write_msghdr
Definition: osmo_io_internal.h:105
struct llist_head msg_queue
actual linked list implementing the transmit queue
Definition: osmo_io_internal.h:94
bool write_enabled
Definition: osmo_io_internal.h:103
enum osmo_io_fd_mode mode
type of read/write mode to use
Definition: osmo_io_internal.h:58
struct osmo_io_fd::@38 msgb_alloc
char * name
human-readable name to associte with fd
Definition: osmo_io_internal.h:64
struct llist_head list
linked list for internal management
Definition: osmo_io_internal.h:54
unsigned int max_length
maximum length of write queue
Definition: osmo_io_internal.h:90
size_t cmsg_size
size of iofd_msghdr.cmsg[] when allocated in recvmsg path
Definition: osmo_io_internal.h:77
struct osmo_fd connect_ofd
Definition: osmo_io_internal.h:108
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:85
struct osmo_fd ofd
Definition: osmo_io_internal.h:99
unsigned int size
size of msgb to allocate (excluding headroom)
Definition: osmo_io_internal.h:83
int fd
actual operating-system level file decriptor
Definition: osmo_io_internal.h:56
const void * ctx
talloc context from which to allocate msgb when reading
Definition: osmo_io_internal.h:81
struct osmo_io_ops io_ops
send/recv (msg) callback functions
Definition: osmo_io_internal.h:67
void * data
data pointer passed through to call-back function
Definition: osmo_io_internal.h:72
unsigned int priv_nr
private number, extending data
Definition: osmo_io_internal.h:74
I/O operations (call-back functions) related to an osmo_io_fd.
Definition: osmo_io.h:93
Definition: socket.h:38