memif: multi-queues support
[vpp.git] / src / plugins / memif / memif.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 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 typedef struct
21 {
22   u16 version;
23 #define MEMIF_VERSION_MAJOR 0
24 #define MEMIF_VERSION_MINOR 1
25 #define MEMIF_VERSION ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
26   u8 type;
27 #define MEMIF_MSG_TYPE_CONNECT_REQ  0
28 #define MEMIF_MSG_TYPE_CONNECT_RESP 1
29 #define MEMIF_MSG_TYPE_DISCONNECT   2
30
31   /* Connection-request parameters: */
32   u64 key;
33   u8 log2_ring_size;
34 #define MEMIF_DEFAULT_RING_SIZE 1024
35   u16 num_s2m_rings;
36 #define MEMIF_DEFAULT_RX_QUEUES 1
37   u16 num_m2s_rings;
38 #define MEMIF_DEFAULT_TX_QUEUES 1
39   u16 buffer_size;
40 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
41   u32 shared_mem_size;
42
43   /* Connection-response parameters: */
44   u8 retval;
45 } memif_msg_t;
46
47 typedef struct __attribute__ ((packed))
48 {
49   u16 flags;
50 #define MEMIF_DESC_FLAG_NEXT (1 << 0)
51   u16 region;
52   u32 buffer_length;
53   u32 length;;
54   u8 reserved[4];
55   u64 offset;
56   u64 metadata;
57 } memif_desc_t;
58
59 STATIC_ASSERT_SIZEOF (memif_desc_t, 32);
60
61 typedef struct
62 {
63   u16 head __attribute__ ((aligned (128)));
64   u16 tail __attribute__ ((aligned (128)));
65   memif_desc_t desc[0] __attribute__ ((aligned (128)));
66 } memif_ring_t;
67
68 typedef struct
69 {
70   u32 cookie __attribute__ ((aligned (128)));
71 } memif_shm_t;
72
73
74 typedef struct
75 {
76   u16 last_head;
77   u16 last_tail;
78 } memif_ring_data_t;
79
80 typedef struct
81 {
82   int fd;
83   u32 index;
84 } memif_file_t;
85
86 typedef struct
87 {
88   uword index;
89   dev_t sock_dev;
90   ino_t sock_ino;
91   memif_file_t socket;
92   u16 usage_counter;
93 } memif_listener_t;
94
95 typedef struct
96 {
97   uword index;
98   memif_file_t connection;
99   uword listener_index;
100 } memif_pending_conn_t;
101
102 typedef struct
103 {
104   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
105   clib_spinlock_t lockp;
106   u32 flags;
107 #define MEMIF_IF_FLAG_ADMIN_UP   (1 << 0)
108 #define MEMIF_IF_FLAG_IS_SLAVE   (1 << 1)
109 #define MEMIF_IF_FLAG_CONNECTING (1 << 2)
110 #define MEMIF_IF_FLAG_CONNECTED  (1 << 3)
111 #define MEMIF_IF_FLAG_DELETING   (1 << 4)
112
113   u64 key;
114   uword if_index;
115   u32 hw_if_index;
116   u32 sw_if_index;
117
118   u32 per_interface_next_index;
119
120   uword listener_index;
121   memif_file_t connection;
122   memif_file_t interrupt_line;
123   u8 *socket_filename;
124
125   void **regions;
126
127   u8 log2_ring_size;
128   u8 num_s2m_rings;
129   u8 num_m2s_rings;
130   u16 buffer_size;
131   u32 shared_mem_size;
132
133   memif_ring_data_t *ring_data;
134
135   /* remote info */
136   pid_t remote_pid;
137   uid_t remote_uid;
138 } memif_if_t;
139
140 typedef struct
141 {
142   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
143
144   /** API message ID base */
145   u16 msg_id_base;
146
147   /* pool of all memory interfaces */
148   memif_if_t *interfaces;
149
150   /* pool of all listeners */
151   memif_listener_t *listeners;
152
153   /* pool of pending connections */
154   memif_pending_conn_t *pending_conns;
155
156   /* bitmap of pending rx interfaces */
157   uword *pending_input_bitmap;
158
159   /* rx buffer cache */
160   u32 **rx_buffers;
161
162   /* hash of all registered keys */
163   mhash_t if_index_by_key;
164
165   /* first cpu index */
166   u32 input_cpu_first_index;
167
168   /* total cpu count */
169   u32 input_cpu_count;
170
171   /* configuration */
172   u8 *default_socket_filename;
173 #define MEMIF_DEFAULT_SOCKET_FILENAME  "/var/vpp/memif.sock"
174 } memif_main_t;
175
176 extern memif_main_t memif_main;
177 extern vnet_device_class_t memif_device_class;
178 extern vlib_node_registration_t memif_input_node;
179
180 enum
181 {
182   MEMIF_PROCESS_EVENT_START = 1,
183   MEMIF_PROCESS_EVENT_STOP = 2,
184 } memif_process_event_t;
185
186 typedef struct
187 {
188   u64 key;
189   u8 *socket_filename;
190   u8 is_master;
191   u8 log2_ring_size;
192   u16 buffer_size;
193   u8 hw_addr_set;
194   u8 hw_addr[6];
195   u8 rx_queues;
196   u8 tx_queues;
197
198   /* return */
199   u32 sw_if_index;
200 } memif_create_if_args_t;
201
202 int memif_create_if (vlib_main_t * vm, memif_create_if_args_t * args);
203 int memif_delete_if (vlib_main_t * vm, u64 key);
204 void memif_disconnect (vlib_main_t * vm, memif_if_t * mif);
205 clib_error_t *memif_plugin_api_hookup (vlib_main_t * vm);
206
207 #ifndef __NR_memfd_create
208 #if defined __x86_64__
209 #define __NR_memfd_create 319
210 #elif defined __arm__
211 #define __NR_memfd_create 385
212 #elif defined __aarch64__
213 #define __NR_memfd_create 279
214 #else
215 #error "__NR_memfd_create unknown for this architecture"
216 #endif
217 #endif
218
219 static_always_inline u8
220 memif_get_rx_queues (memif_if_t * mif)
221 {
222   u8 rx_queues;
223
224   if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
225     rx_queues = mif->num_m2s_rings;
226   else
227     rx_queues = mif->num_s2m_rings;
228
229   return (rx_queues);
230 }
231
232 static_always_inline u8
233 memif_get_tx_queues (memif_if_t * mif)
234 {
235   u8 tx_queues;
236
237   if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
238     tx_queues = mif->num_s2m_rings;
239   else
240     tx_queues = mif->num_m2s_rings;
241
242   return (tx_queues);
243 }
244
245 static inline int
246 memfd_create (const char *name, unsigned int flags)
247 {
248   return syscall (__NR_memfd_create, name, flags);
249 }
250
251 typedef enum
252 {
253   MEMIF_RING_S2M = 0,
254   MEMIF_RING_M2S = 1
255 } memif_ring_type_t;
256
257 static_always_inline memif_ring_t *
258 memif_get_ring (memif_if_t * mif, memif_ring_type_t type, u16 ring_num)
259 {
260   if (vec_len (mif->regions) == 0)
261     return NULL;
262   void *p = mif->regions[0];
263   int ring_size =
264     sizeof (memif_ring_t) +
265     sizeof (memif_desc_t) * (1 << mif->log2_ring_size);
266   p += sizeof (memif_shm_t);
267   p += (ring_num + type * mif->num_s2m_rings) * ring_size;
268
269   return (memif_ring_t *) p;
270 }
271
272 static_always_inline void *
273 memif_get_buffer (memif_if_t * mif, memif_ring_t * ring, u16 slot)
274 {
275   u16 region = ring->desc[slot].region;
276   return mif->regions[region] + ring->desc[slot].offset;
277 }
278
279 #ifndef F_LINUX_SPECIFIC_BASE
280 #define F_LINUX_SPECIFIC_BASE 1024
281 #endif
282 #define MFD_ALLOW_SEALING       0x0002U
283 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
284 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
285
286 #define F_SEAL_SEAL     0x0001  /* prevent further seals from being set */
287 #define F_SEAL_SHRINK   0x0002  /* prevent file from shrinking */
288 #define F_SEAL_GROW     0x0004  /* prevent file from growing */
289 #define F_SEAL_WRITE    0x0008  /* prevent writes */
290
291 /*
292  * fd.io coding-style-patch-verification: ON
293  *
294  * Local Variables:
295  * eval: (c-set-style "gnu")
296  * End:
297  */