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