FD.io VPP  v20.01-48-g3e0dafb74
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  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
117  /* ring data */
122 
127 
128  /* interrupts */
129  int int_fd;
132 
133  /* queue type */
135 } memif_queue_t;
136 
137 #define foreach_memif_if_flag \
138  _(0, ADMIN_UP, "admin-up") \
139  _(1, IS_SLAVE, "slave") \
140  _(2, CONNECTING, "connecting") \
141  _(3, CONNECTED, "connected") \
142  _(4, DELETING, "deleting") \
143  _(5, ZERO_COPY, "zero-copy") \
144  _(6, ERROR, "error")
145 
146 typedef enum
147 {
148 #define _(a, b, c) MEMIF_IF_FLAG_##b = (1 << a),
150 #undef _
152 
153 typedef struct
154 {
155  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
163 
165 
166  /* socket connection */
171 
173 
176 
177  /* remote info */
180 
181  struct
182  {
187  } cfg;
188 
189  struct
190  {
191  memif_log2_ring_size_t log2_ring_size;
192  u8 num_s2m_rings;
193  u8 num_m2s_rings;
194  u16 buffer_size;
195  } run;
196 
197  /* disconnect strings */
200 } memif_if_t;
201 
202 typedef struct
203 {
207 
208 typedef struct
209 {
210  CLIB_ALIGN_MARK (pad, 16); /* align up to 16 bytes for 32bit builds */
211  void *data;
216 
217 #define MEMIF_RX_VECTOR_SZ VLIB_FRAME_SIZE
218 
219 typedef struct
220 {
221  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
222 
223  /* copy vector */
227 
228  /* buffer template */
232 
233 typedef struct
234 {
235  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
236 
237  /** API message ID base */
239 
240  /* pool of all memory interfaces */
242 
243  /* pool of all unix socket files */
245  uword *socket_file_index_by_sock_id; /* map user socket id to pool idx */
246 
247  /* per thread data */
249 
251 
252 } memif_main_t;
253 
254 extern memif_main_t memif_main;
257 
258 typedef enum
259 {
263 
264 typedef struct
265 {
275  u8 hw_addr[6];
278 
279  /* return */
282 
283 int memif_socket_filename_add_del (u8 is_add, u32 sock_id,
284  u8 * sock_filename);
288 
291 {
292  u16 region = ring->desc[slot].region;
293  return mif->regions[region].shm + ring->desc[slot].offset;
294 }
295 
296 /* memif.c */
299 void memif_disconnect (memif_if_t * mif, clib_error_t * err);
300 
301 /* socket.c */
302 void memif_socket_close (clib_socket_t ** sock);
311  clib_error_t * err);
312 u8 *format_memif_device_name (u8 * s, va_list * args);
313 
314 
315 /*
316  * fd.io coding-style-patch-verification: ON
317  *
318  * Local Variables:
319  * eval: (c-set-style "gnu")
320  * End:
321  */
memif_if_t * interfaces
Definition: private.h:241
vlib_log_class_t log_class
Definition: private.h:250
Definition: mhash.h:46
u8 pad[3]
log2 (size of the packing page block)
Definition: bihash_doc.h:61
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:926
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
u8 * secret
Definition: private.h:170
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:244
unsigned long u64
Definition: types.h:89
memif_log2_ring_size_t log2_ring_size
Definition: private.h:183
void memif_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: memif.c:87
memif_interface_mode_t
Definition: memif.h:55
uint16_t memif_region_index_t
Definition: memif.h:62
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:198
memif_interface_mode_t mode
Definition: private.h:271
int memif_socket_filename_add_del(u8 is_add, u32 sock_id, u8 *sock_filename)
Definition: memif.c:660
u16 first_buffer_vec_index
Definition: private.h:205
u32 * buffers
Definition: private.h:125
struct _vnet_device_class vnet_device_class_t
u8 num_m2s_rings
Definition: private.h:185
unsigned char u8
Definition: types.h:56
u32 vlib_log_class_t
Definition: vlib.h:51
#define static_always_inline
Definition: clib.h:99
u8 * remote_name
Definition: private.h:178
clib_error_t * memif_master_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:514
uword socket_file_index
Definition: private.h:168
u8 buffer_pool_index
Definition: private.h:126
u16 buffer_size
Definition: private.h:186
memif_log2_ring_size_t log2_ring_size
Definition: private.h:272
vnet_device_class_t memif_device_class
u32 per_interface_next_index
Definition: private.h:164
memif_region_offset_t offset
Definition: private.h:121
u16 msg_id_base
API message ID base.
Definition: private.h:238
unsigned int u32
Definition: types.h:88
void * shm
Definition: private.h:102
mhash_t dev_instance_by_id
Definition: private.h:92
memif_process_event_t
Definition: private.h:258
clib_error_t * memif_master_conn_fd_error(clib_file_t *uf)
Definition: socket.c:618
memif_region_index_t region
Definition: memif.h:153
u16 last_head
Definition: private.h:123
memif_copy_op_t * copy_ops
Definition: private.h:225
clib_error_t * memif_master_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:579
memif_desc_t desc[0]
Definition: memif.h:175
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:739
uword dev_instance
Definition: private.h:161
uint32_t memif_region_offset_t
Definition: memif.h:63
uint8_t memif_log2_ring_size_t
Definition: memif.h:68
clib_spinlock_t lockp
Definition: private.h:156
unsigned short u16
Definition: types.h:57
void * data
Definition: private.h:211
#define foreach_memif_if_flag
Definition: private.h:137
memif_interface_id_t id
Definition: private.h:266
uword int_clib_file_index
Definition: private.h:130
vlib_main_t * vm
Definition: in2out_ed.c:1810
memif_queue_t * tx_queues
Definition: private.h:175
u8 slot
Definition: pci_types.api:22
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:819
u8 * local_disc_string
Definition: private.h:198
u16 buffer_vec_index
Definition: private.h:214
clib_error_t * memif_connect(memif_if_t *mif)
Definition: memif.c:222
uint32_t memif_interface_id_t
Definition: memif.h:66
u16 last_tail
Definition: private.h:124
i16 buffer_offset
Definition: private.h:213
memif_ring_type_t type
Definition: private.h:134
memif_region_t * regions
Definition: private.h:172
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:290
u32 flags
Definition: private.h:157
memif_ring_t * ring
Definition: private.h:118
vlib_buffer_t buffer_template
Definition: private.h:229
u32 hw_if_index
Definition: private.h:159
struct _socket_t clib_socket_t
clib_error_t * memif_slave_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:596
clib_error_t * memif_slave_conn_fd_error(clib_file_t *uf)
Definition: socket.c:604
clib_error_t * memif_slave_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:553
u64 int_count
Definition: private.h:131
memif_region_offset_t offset
Definition: memif.h:155
uword * dev_instance_by_fd
Definition: private.h:95
u8 num_s2m_rings
Definition: private.h:184
struct _vlib_node_registration vlib_node_registration_t
#define CLIB_ALIGN_MARK(name, alignment)
Definition: clib.h:85
uint64_t memif_region_size_t
Definition: memif.h:64
#define MEMIF_RX_VECTOR_SZ
Definition: private.h:217
VLIB buffer representation.
Definition: buffer.h:102
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:179
memif_interface_id_t id
Definition: private.h:158
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:344
memif_log2_ring_size_t log2_ring_size
Definition: private.h:119
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:354
memif_per_thread_data_t * per_thread_data
Definition: private.h:248
u8 * remote_disc_string
Definition: private.h:199
memif_ring_type_t
Definition: memif.h:49
Definition: file.h:51
clib_socket_t * sock
Definition: private.h:167
memif_queue_t * rx_queues
Definition: private.h:174
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:657
memif_desc_t desc_template
Definition: private.h:230
uword * socket_file_index_by_sock_id
Definition: private.h:245
memif_msg_fifo_elt_t * msg_queue
Definition: private.h:169
memif_region_index_t region
Definition: private.h:120
clib_socket_t * sock
Definition: private.h:86
u32 sw_if_index
Definition: private.h:160
memif_interface_mode_t mode
Definition: private.h:162
memif_region_size_t region_size
Definition: private.h:103
signed short i16
Definition: types.h:46
memif_if_flag_t
Definition: private.h:146