FD.io VPP  v17.07.01-10-g3be13f0
Vector Packet Processing
private.h
Go to the documentation of this file.
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_MAX_M2S_RING (vec_len (vlib_mains) - 1)
28 #define MEMIF_MAX_S2M_RING (vec_len (vlib_mains) - 1)
29 #define MEMIF_MAX_REGION 255
30 #define MEMIF_MAX_LOG2_RING_SIZE 14
31 
32 #define MEMIF_DEBUG 0
33 
34 #if MEMIF_DEBUG == 1
35 #define DBG(...) clib_warning(__VA_ARGS__)
36 #define DBG_UNIX_LOG(...) clib_unix_warning(__VA_ARGS__)
37 #else
38 #define DBG(...)
39 #define DBG_UNIX_LOG(...)
40 #endif
41 
42 #if MEMIF_DEBUG == 1
43 #define memif_file_add(a, b) do { \
44  ASSERT (*a == ~0); \
45  *a = unix_file_add (&unix_main, b); \
46  clib_warning ("unix_file_add fd %d private_data %u idx %u", \
47  (b)->file_descriptor, (b)->private_data, *a); \
48 } while (0)
49 
50 #define memif_file_del(a) do { \
51  clib_warning ("unix_file_del idx %u",a - unix_main.file_pool); \
52  unix_file_del (&unix_main, a); \
53 } while (0)
54 
55 #define memif_file_del_by_index(a) do { \
56  clib_warning ("unix_file_del idx %u", a); \
57  unix_file_del_by_index (&unix_main, a); \
58 } while (0)
59 #else
60 #define memif_file_add(a, b) do { \
61  ASSERT (*a == ~0); \
62  *a = unix_file_add (&unix_main, b); \
63 } while (0)
64 #define memif_file_del(a) unix_file_del(&unix_main, a)
65 #define memif_file_del_by_index(a) unix_file_del_by_index(&unix_main, a)
66 #endif
67 
68 typedef struct
69 {
71  int fd;
74  int ref_cnt;
76 
77  /* hash of all registered id */
79 
80  /* hash of all registered fds */
83 
84 typedef struct
85 {
86  void *shm;
88  int fd;
90 
91 typedef struct
92 {
94  int fd;
96 
97 typedef struct
98 {
99  /* ring data */
104 
107 
108  /* interrupts */
109  int int_fd;
112 } memif_queue_t;
113 
114 #define foreach_memif_if_flag \
115  _(0, ADMIN_UP, "admin-up") \
116  _(1, IS_SLAVE, "slave") \
117  _(2, CONNECTING, "connecting") \
118  _(3, CONNECTED, "connected") \
119  _(4, DELETING, "deleting")
120 
121 typedef enum
122 {
123 #define _(a, b, c) MEMIF_IF_FLAG_##b = (1 << a),
125 #undef _
127 
128 typedef struct
129 {
130  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
138 
140 
141  /* socket connection */
143  int conn_fd;
147 
149 
152 
153  /* remote info */
154  pid_t remote_pid;
155  uid_t remote_uid;
156  gid_t remote_gid;
159 
160  struct
161  {
166  } cfg;
167 
168  struct
169  {
170  memif_log2_ring_size_t log2_ring_size;
171  u8 num_s2m_rings;
172  u8 num_m2s_rings;
173  u16 buffer_size;
174  } run;
175 
176  /* disconnect strings */
179 } memif_if_t;
180 
181 typedef struct
182 {
183  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
184 
185  /** API message ID base */
187 
188  /* pool of all memory interfaces */
190 
191  /* pool of all unix socket files */
194 
195  /* rx buffer cache */
197 
198 } memif_main_t;
199 
200 extern memif_main_t memif_main;
203 
204 enum
205 {
209 
210 typedef struct
211 {
220  u8 hw_addr[6];
223 
224  /* return */
227 
229 int memif_delete_if (vlib_main_t * vm, memif_if_t * mif);
231 
232 #ifndef __NR_memfd_create
233 #if defined __x86_64__
234 #define __NR_memfd_create 319
235 #elif defined __arm__
236 #define __NR_memfd_create 385
237 #elif defined __aarch64__
238 #define __NR_memfd_create 279
239 #else
240 #error "__NR_memfd_create unknown for this architecture"
241 #endif
242 #endif
243 
244 static inline int
245 memfd_create (const char *name, unsigned int flags)
246 {
247  return syscall (__NR_memfd_create, name, flags);
248 }
249 
252 {
253  u16 region = ring->desc[slot].region;
254  return mif->regions[region].shm + ring->desc[slot].offset;
255 }
256 
257 #ifndef F_LINUX_SPECIFIC_BASE
258 #define F_LINUX_SPECIFIC_BASE 1024
259 #endif
260 #define MFD_ALLOW_SEALING 0x0002U
261 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
262 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
263 
264 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
265 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
266 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
267 #define F_SEAL_WRITE 0x0008 /* prevent writes */
268 
269 /* memif.c */
272 void memif_disconnect (memif_if_t * mif, clib_error_t * err);
273 
274 /* socket.c */
283  clib_error_t * err);
284 u8 *format_memif_device_name (u8 * s, va_list * args);
285 
286 /*
287  * fd.io coding-style-patch-verification: ON
288  *
289  * Local Variables:
290  * eval: (c-set-style "gnu")
291  * End:
292  */
memif_if_t * interfaces
Definition: private.h:189
uint64_t memif_region_offset_t
Definition: memif.h:61
Definition: mhash.h:46
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:512
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:68
memif_ring_t * ring
Definition: private.h:100
memif_desc_t desc[0]
Definition: memif.h:174
u8 * secret
Definition: private.h:146
u8 * format_memif_device_name(u8 *s, va_list *args)
Definition: device.c:51
mhash_t socket_file_index_by_filename
Definition: private.h:193
memif_socket_file_t * socket_files
Definition: private.h:192
memif_log2_ring_size_t log2_ring_size
Definition: private.h:162
void memif_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: memif.c:68
struct _vlib_node_registration vlib_node_registration_t
clib_error_t * memif_slave_conn_fd_write_ready(unix_file_t *uf)
Definition: socket.c:632
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:212
uint16_t memif_region_index_t
Definition: memif.h:60
struct _vnet_device_class vnet_device_class_t
u8 num_m2s_rings
Definition: private.h:164
uid_t remote_uid
Definition: private.h:155
uword unix_file_index
Definition: private.h:72
#define static_always_inline
Definition: clib.h:85
u8 * remote_name
Definition: private.h:157
gid_t remote_gid
Definition: private.h:156
uword socket_file_index
Definition: private.h:142
u16 buffer_size
Definition: private.h:165
memif_log2_ring_size_t log2_ring_size
Definition: private.h:217
vnet_device_class_t memif_device_class
u32 per_interface_next_index
Definition: private.h:139
static int memfd_create(const char *name, unsigned int flags)
Definition: private.h:245
memif_region_offset_t offset
Definition: private.h:103
u16 msg_id_base
API message ID base.
Definition: private.h:186
unsigned long u64
Definition: types.h:89
void * shm
Definition: private.h:86
mhash_t dev_instance_by_id
Definition: private.h:78
clib_error_t * memif_master_conn_fd_error(unix_file_t *uf)
Definition: socket.c:654
memif_region_index_t region
Definition: memif.h:150
u16 last_head
Definition: private.h:105
clib_error_t * memif_slave_conn_fd_error(unix_file_t *uf)
Definition: socket.c:640
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:472
uword dev_instance
Definition: private.h:136
clib_spinlock_t lockp
Definition: private.h:131
pid_t remote_pid
Definition: private.h:154
#define foreach_memif_if_flag
Definition: private.h:114
memif_interface_id_t id
Definition: private.h:212
memif_queue_t * tx_queues
Definition: private.h:151
memif_interface_mode_t
Definition: memif.h:53
uint64_t memif_region_size_t
Definition: memif.h:62
uint32_t memif_interface_id_t
Definition: memif.h:64
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:542
u8 * local_disc_string
Definition: private.h:177
clib_error_t * memif_connect(memif_if_t *mif)
Definition: memif.c:172
u16 last_tail
Definition: private.h:106
memif_region_t * regions
Definition: private.h:148
u32 ** rx_buffers
Definition: private.h:196
memif_main_t memif_main
Definition: memif.c:42
unsigned int u32
Definition: types.h:88
memif_msg_t msg
Definition: private.h:93
enum @329 memif_process_event_t
clib_error_t * memif_conn_fd_accept_ready(unix_file_t *uf)
Definition: socket.c:691
static_always_inline void * memif_get_buffer(memif_if_t *mif, memif_ring_t *ring, u16 slot)
Definition: private.h:251
u32 flags
Definition: private.h:132
int conn_fd
Definition: private.h:143
uword conn_unix_file_index
Definition: private.h:144
u32 hw_if_index
Definition: private.h:134
u64 int_count
Definition: private.h:111
u64 uword
Definition: types.h:112
memif_region_offset_t offset
Definition: memif.h:154
clib_error_t * memif_master_conn_fd_write_ready(unix_file_t *uf)
Definition: socket.c:615
uword * dev_instance_by_fd
Definition: private.h:81
u8 num_s2m_rings
Definition: private.h:163
clib_error_t * memif_master_conn_fd_read_ready(unix_file_t *uf)
Definition: socket.c:552
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
uword int_unix_file_index
Definition: private.h:110
Definition: unix.h:49
u8 * remote_if_name
Definition: private.h:158
memif_interface_id_t id
Definition: private.h:133
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:263
memif_log2_ring_size_t log2_ring_size
Definition: private.h:101
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:306
u8 * remote_disc_string
Definition: private.h:178
memif_queue_t * rx_queues
Definition: private.h:150
clib_error_t * memif_slave_conn_fd_read_ready(unix_file_t *uf)
Definition: socket.c:590
uword * pending_file_indices
Definition: private.h:73
u32 flags
Definition: vhost-user.h:76
memif_msg_fifo_elt_t * msg_queue
Definition: private.h:145
uint8_t memif_log2_ring_size_t
Definition: memif.h:66
memif_region_index_t region
Definition: private.h:102
u32 sw_if_index
Definition: private.h:135
memif_region_size_t region_size
Definition: private.h:87
memif_if_flag_t
Definition: private.h:121