libosmocore  1.9.0.20-4ca0f.202310312026
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 
8 #include <osmocom/core/osmo_io.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 #if defined(HAVE_URING)
23 extern const struct iofd_backend_ops iofd_uring_ops;
24 #endif
25 
27  int (*register_fd)(struct osmo_io_fd *iofd);
28  int (*unregister_fd)(struct osmo_io_fd *iofd);
29  int (*close)(struct osmo_io_fd *iofd);
30  void (*write_enable)(struct osmo_io_fd *iofd);
31  void (*write_disable)(struct osmo_io_fd *iofd);
32  void (*read_enable)(struct osmo_io_fd *iofd);
33  void (*read_disable)(struct osmo_io_fd *iofd);
34 };
35 
36 #define IOFD_FLAG_CLOSED (1<<0)
37 #define IOFD_FLAG_IN_CALLBACK (1<<1)
38 #define IOFD_FLAG_TO_FREE (1<<2)
39 #define IOFD_FLAG_NOTIFY_CONNECTED (1<<3)
40 
41 #define IOFD_FLAG_SET(iofd, flag) \
42  (iofd)->flags |= (flag)
43 
44 #define IOFD_FLAG_UNSET(iofd, flag) \
45  (iofd)->flags &= ~(flag)
46 
47 #define IOFD_FLAG_ISSET(iofd, flag) ((iofd)->flags & (flag))
48 
49 struct osmo_io_fd {
51  struct llist_head list;
53  int fd;
55  enum osmo_io_fd_mode mode;
56 
58  uint32_t flags;
59 
61  char *name;
62 
64  struct osmo_io_ops io_ops;
66  struct msgb *pending;
67 
69  void *data;
71  unsigned int priv_nr;
72 
73  struct {
75  const void *ctx;
77  unsigned int size;
79  unsigned int headroom;
81 
82  struct {
84  unsigned int max_length;
86  unsigned int current_length;
88  struct llist_head msg_queue;
90 
91  union {
92  struct {
93  struct osmo_fd ofd;
94  } poll;
95  struct {
98  void *read_msghdr;
99  void *write_msghdr;
100  /* TODO: index into array of registered fd's? */
101  } uring;
102  } u;
103 };
104 
110  // TODO: SCTP_*
111 };
112 
113 
114 /* serialized version of 'struct msghdr' employed by sendmsg/recvmsg */
115 struct iofd_msghdr {
116  struct llist_head list;
117  enum iofd_msg_action action;
118  struct msghdr hdr;
119  struct osmo_sockaddr osa;
120  struct iovec iov[1];
121  int flags;
122 
123  struct msgb *msg;
124  struct osmo_io_fd *iofd;
125 };
126 
131 };
132 
134 void iofd_msghdr_free(struct iofd_msghdr *msghdr);
135 
136 struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd);
137 struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd);
139 
140 void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr);
141 void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc);
142 
143 int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
144 void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr);
Simple doubly linked list implementation.
io(_uring) abstraction osmo fd compatibility
osmo_io_fd_mode
Definition: osmo_io.h:19
void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
void iofd_msghdr_free(struct iofd_msghdr *msghdr)
struct iofd_msghdr * iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg)
iofd_msg_action
Definition: osmo_io_internal.h:105
@ IOFD_ACT_RECVFROM
Definition: osmo_io_internal.h:108
@ IOFD_ACT_SENDTO
Definition: osmo_io_internal.h:109
@ IOFD_ACT_READ
Definition: osmo_io_internal.h:106
@ IOFD_ACT_WRITE
Definition: osmo_io_internal.h:107
void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
struct msgb * iofd_msgb_pending(struct osmo_io_fd *iofd)
struct msgb * iofd_msgb_alloc(struct osmo_io_fd *iofd)
iofd_seg_act
Definition: osmo_io_internal.h:127
@ IOFD_SEG_ACT_DEFER
Definition: osmo_io_internal.h:130
@ IOFD_SEG_ACT_HANDLE_MORE
Definition: osmo_io_internal.h:129
@ IOFD_SEG_ACT_HANDLE_ONE
Definition: osmo_io_internal.h:128
struct iofd_msghdr * iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
struct msgb * iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
const struct iofd_backend_ops iofd_poll_ops
int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
void iofd_handle_recv(struct osmo_io_fd *iofd, struct msgb *msg, int rc, struct iofd_msghdr *msghdr)
libmnl integration
Osmocom socket convenience functions.
Definition: osmo_io_internal.h:26
void(* read_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:32
void(* write_enable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:30
void(* read_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:33
void(* write_disable)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:31
int(* close)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:29
int(* register_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:27
int(* unregister_fd)(struct osmo_io_fd *iofd)
Definition: osmo_io_internal.h:28
Definition: osmo_io_internal.h:115
struct osmo_io_fd * iofd
Definition: osmo_io_internal.h:124
struct llist_head list
Definition: osmo_io_internal.h:116
int flags
Definition: osmo_io_internal.h:121
enum iofd_msg_action action
Definition: osmo_io_internal.h:117
struct msgb * msg
Definition: osmo_io_internal.h:123
struct iovec iov[1]
Definition: osmo_io_internal.h:120
struct msghdr hdr
Definition: osmo_io_internal.h:118
struct osmo_sockaddr osa
Definition: osmo_io_internal.h:119
(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:49
uint32_t flags
flags to guard closing/freeing of iofd
Definition: osmo_io_internal.h:58
unsigned int current_length
current length of write queue
Definition: osmo_io_internal.h:86
void * read_msghdr
Definition: osmo_io_internal.h:98
struct msgb * pending
Pending msgb to keep partial data during segmentation.
Definition: osmo_io_internal.h:66
bool read_enabled
Definition: osmo_io_internal.h:96
struct osmo_io_fd::@39 tx_queue
union osmo_io_fd::@40 u
void * write_msghdr
Definition: osmo_io_internal.h:99
struct llist_head msg_queue
actual linked list implementing the transmit queue
Definition: osmo_io_internal.h:88
bool write_enabled
Definition: osmo_io_internal.h:97
enum osmo_io_fd_mode mode
type of read/write mode to use
Definition: osmo_io_internal.h:55
struct osmo_io_fd::@38 msgb_alloc
char * name
human-readable name to associte with fd
Definition: osmo_io_internal.h:61
struct llist_head list
linked list for internal management
Definition: osmo_io_internal.h:51
unsigned int max_length
maximum length of write queue
Definition: osmo_io_internal.h:84
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:79
struct osmo_fd ofd
Definition: osmo_io_internal.h:93
unsigned int size
size of msgb to allocate (excluding headroom)
Definition: osmo_io_internal.h:77
int fd
actual operating-system level file decriptor
Definition: osmo_io_internal.h:53
const void * ctx
talloc context from which to allocate msgb when reading
Definition: osmo_io_internal.h:75
struct osmo_io_ops io_ops
send/recv (msg) callback functions
Definition: osmo_io_internal.h:64
void * data
data pointer passed through to call-back function
Definition: osmo_io_internal.h:69
unsigned int priv_nr
private number, extending data
Definition: osmo_io_internal.h:71
Definition: osmo_io.h:37
Definition: socket.h:26