FD.io VPP  v18.10-34-gcce845e
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 #include <vlib/log.h>
20 
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))
28 #define MEMIF_MAX_S2M_RING 256
29 #define MEMIF_MAX_REGION 256
30 #define MEMIF_MAX_LOG2_RING_SIZE 14
31 
32 
33 #define memif_log_debug(dev, f, ...) do { \
34  memif_if_t *_dev = (memif_if_t *) dev; \
35  if (_dev) \
36  vlib_log(VLIB_LOG_LEVEL_DEBUG, memif_main.log_class, "%U: " f, \
37  format_vnet_hw_if_index_name, vnet_get_main(), \
38  _dev->hw_if_index, ##__VA_ARGS__); \
39  else \
40  vlib_log(VLIB_LOG_LEVEL_DEBUG, memif_main.log_class, f, \
41  ##__VA_ARGS__); \
42 } while (0)
43 
44 #define memif_log_warn(dev, f, ...) do { \
45  memif_if_t *_dev = (memif_if_t *) dev; \
46  if (_dev) \
47  vlib_log(VLIB_LOG_LEVEL_WARNING, memif_main.log_class, "%U: " f, \
48  format_vnet_hw_if_index_name, vnet_get_main(), \
49  _dev->hw_if_index, ##__VA_ARGS__); \
50  else \
51  vlib_log(VLIB_LOG_LEVEL_WARNING, memif_main.log_class, f, \
52  ##__VA_ARGS__); \
53 } while (0)
54 
55 #define memif_log_err(dev, f, ...) do { \
56  memif_if_t *_dev = (memif_if_t *) dev; \
57  if (_dev) \
58  vlib_log(VLIB_LOG_LEVEL_ERR, memif_main.log_class, "%U: " f, \
59  format_vnet_hw_if_index_name, vnet_get_main(), \
60  _dev->hw_if_index, ##__VA_ARGS__); \
61  else \
62  vlib_log(VLIB_LOG_LEVEL_ERR, memif_main.log_class, f, \
63  ##__VA_ARGS__); \
64 } while (0)
65 
66 #define memif_file_add(a, b) do { \
67  *a = clib_file_add (&file_main, b); \
68  memif_log_warn (0, "clib_file_add fd %d private_data %u idx %u", \
69  (b)->file_descriptor, (b)->private_data, *a); \
70 } while (0)
71 
72 #define memif_file_del(a) do { \
73  memif_log_warn (0, "clib_file_del idx %u", a - file_main.file_pool); \
74  clib_file_del (&file_main, a); \
75 } while (0)
76 
77 #define memif_file_del_by_index(a) do { \
78  memif_log_warn (0, "clib_file_del idx %u", a); \
79  clib_file_del_by_index (&file_main, a); \
80 } while (0)
81 
82 typedef struct
83 {
88  int ref_cnt;
90 
91  /* hash of all registered id */
93 
94  /* hash of all registered fds */
97 
98 typedef struct
99 {
100  /* Required for vec_validate_aligned */
101  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
102  void *shm;
104  int fd;
107 
108 typedef struct
109 {
111  int fd;
113 
114 typedef struct
115 {
116  /* ring data */
121 
125 
126  /* interrupts */
127  int int_fd;
130 
131  /* queue type */
133 } memif_queue_t;
134 
135 #define foreach_memif_if_flag \
136  _(0, ADMIN_UP, "admin-up") \
137  _(1, IS_SLAVE, "slave") \
138  _(2, CONNECTING, "connecting") \
139  _(3, CONNECTED, "connected") \
140  _(4, DELETING, "deleting") \
141  _(5, ZERO_COPY, "zero-copy") \
142  _(6, ERROR, "error")
143 
144 typedef enum
145 {
146 #define _(a, b, c) MEMIF_IF_FLAG_##b = (1 << a),
148 #undef _
150 
151 typedef struct
152 {
153  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
161 
163 
164  /* socket connection */
169 
171 
174 
175  /* remote info */
178 
179  struct
180  {
185  } cfg;
186 
187  struct
188  {
189  memif_log2_ring_size_t log2_ring_size;
190  u8 num_s2m_rings;
191  u8 num_m2s_rings;
192  u16 buffer_size;
193  } run;
194 
195  /* disconnect strings */
198 } memif_if_t;
199 
200 typedef struct
201 {
205 
206 typedef struct
207 {
208  void *data;
213 
214 #define MEMIF_RX_VECTOR_SZ VLIB_FRAME_SIZE
215 
216 typedef struct
217 {
218  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
219 
220  /* copy vector */
224 
225  /* buffer template */
229 
230 typedef struct
231 {
232  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
233 
234  /** API message ID base */
236 
237  /* pool of all memory interfaces */
239 
240  /* pool of all unix socket files */
242  uword *socket_file_index_by_sock_id; /* map user socket id to pool idx */
243 
244  /* per thread data */
246 
248 
249 } memif_main_t;
250 
251 extern memif_main_t memif_main;
254 
255 enum
256 {
260 
261 typedef struct
262 {
272  u8 hw_addr[6];
275 
276  /* return */
279 
280 int memif_socket_filename_add_del (u8 is_add, u32 sock_id,
281  u8 * sock_filename);
285 
288 {
289  u16 region = ring->desc[slot].region;
290  return mif->regions[region].shm + ring->desc[slot].offset;
291 }
292 
293 /* memif.c */
296 void memif_disconnect (memif_if_t * mif, clib_error_t * err);
297 
298 /* socket.c */
299 void memif_socket_close (clib_socket_t ** sock);
308  clib_error_t * err);
309 u8 *format_memif_device_name (u8 * s, va_list * args);
310 
311 
312 /*
313  * fd.io coding-style-patch-verification: ON
314  *
315  * Local Variables:
316  * eval: (c-set-style "gnu")
317  * End:
318  */
memif_if_t * interfaces
Definition: private.h:238
vlib_log_class_t log_class
Definition: private.h:247
Definition: mhash.h:46
u32 vlib_log_class_t
Definition: log.h:21
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:889
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
u8 * secret
Definition: private.h:168
enum @485 memif_process_event_t
clib_socket_t ** pending_clients
Definition: private.h:87
u8 * format_memif_device_name(u8 *s, va_list *args)
Definition: device.c:51
memif_socket_file_t * socket_files
Definition: private.h:241
unsigned long u64
Definition: types.h:89
memif_log2_ring_size_t log2_ring_size
Definition: private.h:181
void memif_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: memif.c:69
memif_interface_mode_t
Definition: memif.h:53
uint16_t memif_region_index_t
Definition: memif.h:60
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:190
int memif_socket_filename_add_del(u8 is_add, u32 sock_id, u8 *sock_filename)
Definition: memif.c:619
u16 first_buffer_vec_index
Definition: private.h:203
u32 * buffers
Definition: private.h:124
struct _vnet_device_class vnet_device_class_t
u8 num_m2s_rings
Definition: private.h:183
unsigned char u8
Definition: types.h:56
#define static_always_inline
Definition: clib.h:95
u8 * remote_name
Definition: private.h:176
clib_error_t * memif_master_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:506
uword socket_file_index
Definition: private.h:166
u16 buffer_size
Definition: private.h:184
memif_log2_ring_size_t log2_ring_size
Definition: private.h:269
vnet_device_class_t memif_device_class
u32 per_interface_next_index
Definition: private.h:162
memif_region_offset_t offset
Definition: private.h:120
u16 msg_id_base
API message ID base.
Definition: private.h:235
unsigned int u32
Definition: types.h:88
void * shm
Definition: private.h:102
mhash_t dev_instance_by_id
Definition: private.h:92
clib_error_t * memif_master_conn_fd_error(clib_file_t *uf)
Definition: socket.c:610
memif_region_index_t region
Definition: memif.h:151
u16 last_head
Definition: private.h:122
memif_copy_op_t * copy_ops
Definition: private.h:222
clib_error_t * memif_master_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:571
memif_desc_t desc[0]
Definition: memif.h:173
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:698
uword dev_instance
Definition: private.h:159
uint32_t memif_region_offset_t
Definition: memif.h:61
uint8_t memif_log2_ring_size_t
Definition: memif.h:66
clib_spinlock_t lockp
Definition: private.h:154
unsigned short u16
Definition: types.h:57
void * data
Definition: private.h:208
#define foreach_memif_if_flag
Definition: private.h:135
memif_interface_id_t id
Definition: private.h:263
uword int_clib_file_index
Definition: private.h:128
memif_queue_t * tx_queues
Definition: private.h:173
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:778
vlib_main_t * vm
Definition: buffer.c:294
u8 * local_disc_string
Definition: private.h:196
u16 buffer_vec_index
Definition: private.h:211
clib_error_t * memif_connect(memif_if_t *mif)
Definition: memif.c:186
uint32_t memif_interface_id_t
Definition: memif.h:64
u16 last_tail
Definition: private.h:123
i16 buffer_offset
Definition: private.h:210
memif_ring_type_t type
Definition: private.h:132
memif_region_t * regions
Definition: private.h:170
memif_main_t memif_main
Definition: memif.c:43
memif_msg_t msg
Definition: private.h:110
static_always_inline void * memif_get_buffer(memif_if_t *mif, memif_ring_t *ring, u16 slot)
Definition: private.h:287
u32 flags
Definition: private.h:155
memif_ring_t * ring
Definition: private.h:117
vlib_buffer_t buffer_template
Definition: private.h:226
u32 hw_if_index
Definition: private.h:157
struct _socket_t clib_socket_t
clib_error_t * memif_slave_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:588
clib_error_t * memif_slave_conn_fd_error(clib_file_t *uf)
Definition: socket.c:596
clib_error_t * memif_slave_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:545
u64 int_count
Definition: private.h:129
memif_region_offset_t offset
Definition: memif.h:153
uword * dev_instance_by_fd
Definition: private.h:95
u8 num_s2m_rings
Definition: private.h:182
struct _vlib_node_registration vlib_node_registration_t
uint64_t memif_region_size_t
Definition: memif.h:62
#define MEMIF_RX_VECTOR_SZ
Definition: private.h:214
u64 uword
Definition: types.h:112
void memif_socket_close(clib_socket_t **sock)
Definition: socket.c:43
u8 * remote_if_name
Definition: private.h:177
memif_interface_id_t id
Definition: private.h:156
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:303
memif_log2_ring_size_t log2_ring_size
Definition: private.h:118
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:408
memif_per_thread_data_t * per_thread_data
Definition: private.h:245
u8 * remote_disc_string
Definition: private.h:197
memif_ring_type_t
Definition: memif.h:47
Definition: file.h:51
clib_socket_t * sock
Definition: private.h:165
memif_queue_t * rx_queues
Definition: private.h:172
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:649
memif_desc_t desc_template
Definition: private.h:227
uword * socket_file_index_by_sock_id
Definition: private.h:242
memif_msg_fifo_elt_t * msg_queue
Definition: private.h:167
memif_region_index_t region
Definition: private.h:119
clib_socket_t * sock
Definition: private.h:86
u32 sw_if_index
Definition: private.h:158
memif_region_size_t region_size
Definition: private.h:103
signed short i16
Definition: types.h:46
memif_if_flag_t
Definition: private.h:144