memif: complete refactor of socket handling code
[vpp.git] / src / plugins / memif / private.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vppinfra/lock.h>
19
20 #define MEMIF_DEFAULT_SOCKET_DIR "/run/vpp"
21 #define MEMIF_DEFAULT_SOCKET_FILENAME  "memif.sock"
22 #define MEMIF_DEFAULT_RING_SIZE 1024
23 #define MEMIF_DEFAULT_RX_QUEUES 1
24 #define MEMIF_DEFAULT_TX_QUEUES 1
25 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
26
27 #define MEMIF_VERSION_MAJOR             0
28 #define MEMIF_VERSION_MINOR             1
29 #define MEMIF_VERSION                   ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
30 #define MEMIF_COOKIE                    0xdeadbeef
31 #define MEMIF_MAX_M2S_RING              (vec_len (vlib_mains) - 1)
32 #define MEMIF_MAX_S2M_RING              (vec_len (vlib_mains) - 1)
33 #define MEMIF_MAX_REGION                255
34 #define MEMIF_MAX_LOG2_RING_SIZE        14
35
36 #define MEMIF_DEBUG 0
37
38 #if MEMIF_DEBUG == 1
39 #define DBG(...) clib_warning(__VA_ARGS__)
40 #define DBG_UNIX_LOG(...) clib_unix_warning(__VA_ARGS__)
41 #else
42 #define DBG(...)
43 #define DBG_UNIX_LOG(...)
44 #endif
45
46 #if MEMIF_DEBUG == 1
47 #define memif_file_add(a, b) do {                                       \
48   ASSERT (*a == ~0);                                                    \
49   *a = unix_file_add (&unix_main, b);                                   \
50   clib_warning ("unix_file_add fd %d private_data %u idx %u",           \
51                 (b)->file_descriptor, (b)->private_data, *a);           \
52 } while (0)
53
54 #define memif_file_del(a) do {                                          \
55   clib_warning ("unix_file_del idx %u",a - unix_main.file_pool);        \
56   unix_file_del (&unix_main, a);                                        \
57 } while (0)
58
59 #define memif_file_del_by_index(a) do {                                 \
60   clib_warning ("unix_file_del idx %u", a);                             \
61   unix_file_del_by_index (&unix_main, a);                               \
62 } while (0)
63 #else
64 #define memif_file_add(a, b) do {                                       \
65   ASSERT (*a == ~0);                                                    \
66   *a = unix_file_add (&unix_main, b);                                   \
67 } while (0)
68 #define memif_file_del(a) unix_file_del(&unix_main, a)
69 #define memif_file_del_by_index(a) unix_file_del_by_index(&unix_main, a)
70 #endif
71
72 typedef struct
73 {
74   u8 *filename;
75   int fd;
76   uword unix_file_index;
77   uword *pending_file_indices;
78   int ref_cnt;
79   int is_listener;
80
81   /* hash of all registered id */
82   mhash_t dev_instance_by_id;
83
84   /* hash of all registered fds */
85   uword *dev_instance_by_fd;
86 } memif_socket_file_t;
87
88 typedef struct
89 {
90   void *shm;
91   u32 region_size;
92   int fd;
93 } memif_region_t;
94
95 typedef struct
96 {
97   memif_msg_t msg;
98   int fd;
99 } memif_msg_fifo_elt_t;
100
101 typedef struct
102 {
103   /* ring data */
104   memif_ring_t *ring;
105   u8 log2_ring_size;
106   u8 region;
107   u32 offset;
108
109   u16 last_head;
110   u16 last_tail;
111
112   /* interrupts */
113   int int_fd;
114   uword int_unix_file_index;
115   u64 int_count;
116 } memif_queue_t;
117
118 #define foreach_memif_if_flag \
119   _(0, ADMIN_UP, "admin-up")            \
120   _(1, IS_SLAVE, "slave")               \
121   _(2, CONNECTING, "connecting")        \
122   _(3, CONNECTED, "connected")          \
123   _(4, DELETING, "deleting")
124
125 typedef enum
126 {
127 #define _(a, b, c) MEMIF_IF_FLAG_##b = (1 << a),
128   foreach_memif_if_flag
129 #undef _
130 } memif_if_flag_t;
131
132 typedef struct
133 {
134   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
135   clib_spinlock_t lockp;
136   u32 flags;
137   memif_interface_id_t id;
138   u32 hw_if_index;
139   u32 sw_if_index;
140   uword dev_instance;
141   memif_interface_mode_t mode:8;
142
143   u32 per_interface_next_index;
144
145   /* socket connection */
146   uword socket_file_index;
147   int conn_fd;
148   uword conn_unix_file_index;
149   memif_msg_fifo_elt_t *msg_queue;
150   u8 *secret;
151
152   memif_region_t *regions;
153
154   memif_queue_t *rx_queues;
155   memif_queue_t *tx_queues;
156
157   /* remote info */
158   pid_t remote_pid;
159   uid_t remote_uid;
160   gid_t remote_gid;
161   u8 *remote_name;
162   u8 *remote_if_name;
163
164   struct
165   {
166     u8 log2_ring_size;
167     u8 num_s2m_rings;
168     u8 num_m2s_rings;
169     u16 buffer_size;
170   } cfg;
171
172   struct
173   {
174     u8 log2_ring_size;
175     u8 num_s2m_rings;
176     u8 num_m2s_rings;
177     u16 buffer_size;
178   } run;
179
180   /* disconnect strings */
181   u8 *local_disc_string;
182   u8 *remote_disc_string;
183 } memif_if_t;
184
185 typedef struct
186 {
187   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
188
189   /** API message ID base */
190   u16 msg_id_base;
191
192   /* pool of all memory interfaces */
193   memif_if_t *interfaces;
194
195   /* pool of all unix socket files */
196   memif_socket_file_t *socket_files;
197   mhash_t socket_file_index_by_filename;
198
199   /* rx buffer cache */
200   u32 **rx_buffers;
201
202 } memif_main_t;
203
204 extern memif_main_t memif_main;
205 extern vnet_device_class_t memif_device_class;
206 extern vlib_node_registration_t memif_input_node;
207
208 enum
209 {
210   MEMIF_PROCESS_EVENT_START = 1,
211   MEMIF_PROCESS_EVENT_STOP = 2,
212 } memif_process_event_t;
213
214 typedef struct
215 {
216   memif_interface_id_t id;
217   u8 *socket_filename;
218   u8 *secret;
219   u8 is_master;
220   memif_interface_mode_t mode:8;
221   u8 log2_ring_size;
222   u16 buffer_size;
223   u8 hw_addr_set;
224   u8 hw_addr[6];
225   u8 rx_queues;
226   u8 tx_queues;
227
228   /* return */
229   u32 sw_if_index;
230 } memif_create_if_args_t;
231
232 int memif_create_if (vlib_main_t * vm, memif_create_if_args_t * args);
233 int memif_delete_if (vlib_main_t * vm, memif_if_t * mif);
234 clib_error_t *memif_plugin_api_hookup (vlib_main_t * vm);
235
236 #ifndef __NR_memfd_create
237 #if defined __x86_64__
238 #define __NR_memfd_create 319
239 #elif defined __arm__
240 #define __NR_memfd_create 385
241 #elif defined __aarch64__
242 #define __NR_memfd_create 279
243 #else
244 #error "__NR_memfd_create unknown for this architecture"
245 #endif
246 #endif
247
248 static inline int
249 memfd_create (const char *name, unsigned int flags)
250 {
251   return syscall (__NR_memfd_create, name, flags);
252 }
253
254 static_always_inline void *
255 memif_get_buffer (memif_if_t * mif, memif_ring_t * ring, u16 slot)
256 {
257   u16 region = ring->desc[slot].region;
258   return mif->regions[region].shm + ring->desc[slot].offset;
259 }
260
261 #ifndef F_LINUX_SPECIFIC_BASE
262 #define F_LINUX_SPECIFIC_BASE 1024
263 #endif
264 #define MFD_ALLOW_SEALING       0x0002U
265 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
266 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
267
268 #define F_SEAL_SEAL     0x0001  /* prevent further seals from being set */
269 #define F_SEAL_SHRINK   0x0002  /* prevent file from shrinking */
270 #define F_SEAL_GROW     0x0004  /* prevent file from growing */
271 #define F_SEAL_WRITE    0x0008  /* prevent writes */
272
273 /* memif.c */
274 clib_error_t *memif_init_regions_and_queues (memif_if_t * mif);
275 clib_error_t *memif_connect (memif_if_t * mif);
276 void memif_disconnect (memif_if_t * mif, clib_error_t * err);
277
278 /* socket.c */
279 clib_error_t *memif_conn_fd_accept_ready (unix_file_t * uf);
280 clib_error_t *memif_master_conn_fd_read_ready (unix_file_t * uf);
281 clib_error_t *memif_slave_conn_fd_read_ready (unix_file_t * uf);
282 clib_error_t *memif_master_conn_fd_write_ready (unix_file_t * uf);
283 clib_error_t *memif_slave_conn_fd_write_ready (unix_file_t * uf);
284 clib_error_t *memif_master_conn_fd_error (unix_file_t * uf);
285 clib_error_t *memif_slave_conn_fd_error (unix_file_t * uf);
286 clib_error_t *memif_msg_send_disconnect (memif_if_t * mif,
287                                          clib_error_t * err);
288 u8 *format_memif_device_name (u8 * s, va_list * args);
289
290 /*
291  * fd.io coding-style-patch-verification: ON
292  *
293  * Local Variables:
294  * eval: (c-set-style "gnu")
295  * End:
296  */