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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <vppinfra/lock.h>
23 #define MEMIF_VERSION_MAJOR 0
24 #define MEMIF_VERSION_MINOR 1
25 #define MEMIF_VERSION ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
27 #define MEMIF_MSG_TYPE_CONNECT_REQ 0
28 #define MEMIF_MSG_TYPE_CONNECT_RESP 1
29 #define MEMIF_MSG_TYPE_DISCONNECT 2
31 /* Connection-request parameters: */
34 #define MEMIF_DEFAULT_RING_SIZE 1024
38 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
41 /* Connection-response parameters: */
45 typedef struct __attribute__ ((packed))
48 #define MEMIF_DESC_FLAG_NEXT (1 << 0)
57 STATIC_ASSERT_SIZEOF (memif_desc_t, 32);
61 u16 head __attribute__ ((aligned (128)));
62 u16 tail __attribute__ ((aligned (128)));
63 memif_desc_t desc[0] __attribute__ ((aligned (128)));
68 u32 cookie __attribute__ ((aligned (128)));
96 memif_file_t connection;
98 } memif_pending_conn_t;
102 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
103 clib_spinlock_t lockp;
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)
116 u32 per_interface_next_index;
118 uword listener_index;
119 memif_file_t connection;
120 memif_file_t interrupt_line;
130 memif_ring_data_t *ring_data;
139 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
141 /** API message ID base */
144 /* pool of all memory interfaces */
145 memif_if_t *interfaces;
147 /* pool of all listeners */
148 memif_listener_t *listeners;
150 /* pool of pending connections */
151 memif_pending_conn_t *pending_conns;
153 /* bitmap of pending rx interfaces */
154 uword *pending_input_bitmap;
156 /* rx buffer cache */
159 /* hash of all registered keys */
160 mhash_t if_index_by_key;
162 /* first cpu index */
163 u32 input_cpu_first_index;
165 /* total cpu count */
169 u8 *default_socket_filename;
170 #define MEMIF_DEFAULT_SOCKET_FILENAME "/var/vpp/memif.sock"
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;
179 MEMIF_PROCESS_EVENT_START = 1,
180 MEMIF_PROCESS_EVENT_STOP = 2,
181 } memif_process_event_t;
195 } memif_create_if_args_t;
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);
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
210 #error "__NR_memfd_create unknown for this architecture"
215 memfd_create (const char *name, unsigned int flags)
217 return syscall (__NR_memfd_create, name, flags);
226 static_always_inline memif_ring_t *
227 memif_get_ring (memif_if_t * mif, memif_ring_type_t type, u16 ring_num)
229 if (vec_len (mif->regions) == 0)
231 void *p = mif->regions[0];
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;
238 return (memif_ring_t *) p;
241 static_always_inline void *
242 memif_get_buffer (memif_if_t * mif, memif_ring_t * ring, u16 slot)
244 u16 region = ring->desc[slot].region;
245 return mif->regions[region] + ring->desc[slot].offset;
248 #ifndef F_LINUX_SPECIFIC_BASE
249 #define F_LINUX_SPECIFIC_BASE 1024
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)
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 */
261 * fd.io coding-style-patch-verification: ON
264 * eval: (c-set-style "gnu")