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