FD.io VPP  v21.10.1-2-g0a485f517
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 (vlib_get_n_threads ())
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) \
67  do \
68  { \
69  *a = clib_file_add (&file_main, b); \
70  memif_log_debug (0, "clib_file_add fd %d private_data %u idx %u", \
71  (b)->file_descriptor, (b)->private_data, *a); \
72  } \
73  while (0)
74 
75 #define memif_file_del(a) \
76  do \
77  { \
78  memif_log_debug (0, "clib_file_del idx %u", a - file_main.file_pool); \
79  clib_file_del (&file_main, a); \
80  } \
81  while (0)
82 
83 #define memif_file_del_by_index(a) \
84  do \
85  { \
86  memif_log_debug (0, "clib_file_del idx %u", a); \
87  clib_file_del_by_index (&file_main, a); \
88  } \
89  while (0)
90 
91 typedef struct
92 {
97  int ref_cnt;
99 
100  /* hash of all registered id */
102 
103  /* hash of all registered fds */
106 
107 typedef struct
108 {
109  /* Required for vec_validate_aligned */
110  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
111  void *shm;
113  int fd;
116 
117 typedef struct
118 {
120  int fd;
122 
123 typedef struct
124 {
125  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
126  /* ring data */
131 
136 
137  /* interrupts */
138  int int_fd;
141 
142  /* queue type */
145 } memif_queue_t;
146 
147 #define foreach_memif_if_flag \
148  _(0, ADMIN_UP, "admin-up") \
149  _(1, IS_SLAVE, "slave") \
150  _(2, CONNECTING, "connecting") \
151  _(3, CONNECTED, "connected") \
152  _(4, DELETING, "deleting") \
153  _(5, ZERO_COPY, "zero-copy") \
154  _(6, ERROR, "error")
155 
156 typedef enum
157 {
158 #define _(a, b, c) MEMIF_IF_FLAG_##b = (1 << a),
160 #undef _
162 
163 typedef struct
164 {
165  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
173 
175 
176  /* socket connection */
181 
183 
186 
187  /* remote info */
190 
191  struct
192  {
197  } cfg;
198 
199  struct
200  {
201  memif_log2_ring_size_t log2_ring_size;
202  u8 num_s2m_rings;
203  u8 num_m2s_rings;
204  u16 buffer_size;
205  } run;
206 
207  /* disconnect strings */
210 } memif_if_t;
211 
212 typedef struct
213 {
217 
218 typedef struct
219 {
220  CLIB_ALIGN_MARK (pad, 16); /* align up to 16 bytes for 32bit builds */
221  void *data;
226 
227 #define MEMIF_RX_VECTOR_SZ VLIB_FRAME_SIZE
228 
229 typedef struct
230 {
231  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
232 
233  /* copy vector */
237 
238  /* buffer template */
241 
242 typedef struct
243 {
244  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
245 
246  /** API message ID base */
248 
249  /* pool of all memory interfaces */
251 
252  /* pool of all unix socket files */
254  uword *socket_file_index_by_sock_id; /* map user socket id to pool idx */
255 
256  /* per thread data */
258 
260 
261 } memif_main_t;
262 
263 extern memif_main_t memif_main;
266 
267 typedef enum
268 {
273 
274 typedef struct
275 {
285  u8 hw_addr[6];
288 
289  /* return */
292 
293 int memif_socket_filename_add_del (u8 is_add, u32 sock_id,
294  u8 * sock_filename);
299  u32 flags);
300 
303 {
304  u16 region = ring->desc[slot].region;
305  return mif->regions[region].shm + ring->desc[slot].offset;
306 }
307 
308 /* memif.c */
311 void memif_disconnect (memif_if_t * mif, clib_error_t * err);
312 
313 /* socket.c */
314 void memif_socket_close (clib_socket_t ** sock);
323  clib_error_t * err);
324 u8 *format_memif_device_name (u8 * s, va_list * args);
325 
326 
327 /*
328  * fd.io coding-style-patch-verification: ON
329  *
330  * Local Variables:
331  * eval: (c-set-style "gnu")
332  * End:
333  */
memif_input_node
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:884
memif_init_regions_and_queues
clib_error_t * memif_init_regions_and_queues(memif_if_t *mif)
Definition: memif.c:356
memif_create_if_args_t::mode
memif_interface_mode_t mode
Definition: private.h:281
memif_create_if_args_t::sw_if_index
u32 sw_if_index
Definition: private.h:290
memif_main_t::log_class
vlib_log_class_t log_class
Definition: private.h:259
format_memif_device_name
u8 * format_memif_device_name(u8 *s, va_list *args)
Definition: device.c:51
memif_conn_fd_accept_ready
clib_error_t * memif_conn_fd_accept_ready(clib_file_t *uf)
Definition: socket.c:658
memif_socket_filename_add_del
int memif_socket_filename_add_del(u8 is_add, u32 sock_id, u8 *sock_filename)
Definition: memif.c:690
vnet_device_class_t
struct _vnet_device_class vnet_device_class_t
memif_if_t::socket_file_index
uword socket_file_index
Definition: private.h:178
memif_packet_op_t::packet_len
u32 packet_len
Definition: private.h:214
memif_socket_file_t::sock
clib_socket_t * sock
Definition: private.h:95
memif_ring_t::desc
memif_desc_t desc[]
Definition: memif.h:175
memif_if_t::per_interface_next_index
u32 per_interface_next_index
Definition: private.h:174
CLIB_CACHE_LINE_ALIGN_MARK
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
vlib_log_class_t
u32 vlib_log_class_t
Definition: vlib.h:52
memif_if_t::tx_queues
memif_queue_t * tx_queues
Definition: private.h:185
memif_copy_op_t::data_len
u32 data_len
Definition: private.h:222
memif_region_t::region_size
memif_region_size_t region_size
Definition: private.h:112
memif_msg_fifo_elt_t::fd
int fd
Definition: private.h:120
memif_if_t::regions
memif_region_t * regions
Definition: private.h:182
memif_if_t::sw_if_index
u32 sw_if_index
Definition: private.h:170
memif_create_if_args_t::buffer_size
u16 buffer_size
Definition: private.h:283
memif_queue_t::last_tail
u16 last_tail
Definition: private.h:133
memif_main_t::socket_files
memif_socket_file_t * socket_files
Definition: private.h:253
memif_if_flag_t
memif_if_flag_t
Definition: private.h:156
memif_create_if_args_t::log2_ring_size
memif_log2_ring_size_t log2_ring_size
Definition: private.h:282
memif_queue_t::queue_index
u32 queue_index
Definition: private.h:144
mhash_t
Definition: mhash.h:46
memif_disconnect
void memif_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: memif.c:87
memif_region_offset_t
uint32_t memif_region_offset_t
Definition: memif.h:63
memif_if_t::id
memif_interface_id_t id
Definition: private.h:168
memif_packet_op_t
Definition: private.h:212
memif_region_t::fd
int fd
Definition: private.h:113
memif_main_t::per_thread_data
memif_per_thread_data_t * per_thread_data
Definition: private.h:257
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
memif_queue_t::type
memif_ring_type_t type
Definition: private.h:143
memif_if_t::msg_queue
memif_msg_fifo_elt_t * msg_queue
Definition: private.h:179
memif_if_t::rx_queues
memif_queue_t * rx_queues
Definition: private.h:184
memif_master_conn_fd_read_ready
clib_error_t * memif_master_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:514
memif_packet_op_t::first_buffer_vec_index
u16 first_buffer_vec_index
Definition: private.h:215
memif_if_t::remote_name
u8 * remote_name
Definition: private.h:188
memif_main_t
Definition: private.h:242
memif_queue_t::ring
memif_ring_t * ring
Definition: private.h:127
memif_delete_if
int memif_delete_if(vlib_main_t *vm, memif_if_t *mif)
Definition: memif.c:769
MEMIF_PROCESS_EVENT_START
@ MEMIF_PROCESS_EVENT_START
Definition: private.h:269
memif_desc_t::region
memif_region_index_t region
Definition: memif.h:153
CLIB_ALIGN_MARK
#define CLIB_ALIGN_MARK(name, alignment)
Definition: clib.h:93
memif_queue_t::log2_ring_size
memif_log2_ring_size_t log2_ring_size
Definition: private.h:128
memif_msg_send_disconnect
clib_error_t * memif_msg_send_disconnect(memif_if_t *mif, clib_error_t *err)
Definition: socket.c:198
memif_queue_t::int_count
u64 int_count
Definition: private.h:140
memif_if_t::flags
u32 flags
Definition: private.h:167
memif_socket_file_t::pending_clients
clib_socket_t ** pending_clients
Definition: private.h:96
memif_master_conn_fd_error
clib_error_t * memif_master_conn_fd_error(clib_file_t *uf)
Definition: socket.c:618
i16
signed short i16
Definition: types.h:46
memif_create_if_args_t::tx_queues
u8 tx_queues
Definition: private.h:287
memif_main_t::interfaces
memif_if_t * interfaces
Definition: private.h:250
memif_socket_file_t::socket_id
u32 socket_id
Definition: private.h:94
slot
u8 slot
Definition: pci_types.api:22
memif_region_t
Definition: private.h:107
memif_if_t::lockp
clib_spinlock_t lockp
Definition: private.h:166
memif_create_if
int memif_create_if(vlib_main_t *vm, memif_create_if_args_t *args)
Definition: memif.c:849
memif_if_t::mode
memif_interface_mode_t mode
Definition: private.h:172
memif_per_thread_data_t
Definition: private.h:229
memif_ring_type_t
memif_ring_type_t
Definition: memif.h:49
clib_file
Definition: file.h:51
memif_device_class
vnet_device_class_t memif_device_class
memif_queue_t::region
memif_region_index_t region
Definition: private.h:129
lock.h
memif_socket_file_t::filename
u8 * filename
Definition: private.h:93
memif_create_if_args_t::is_zero_copy
u8 is_zero_copy
Definition: private.h:280
log.h
static_always_inline
#define static_always_inline
Definition: clib.h:112
clib_spinlock_s
Definition: lock.h:51
memif_interface_id_t
uint32_t memif_interface_id_t
Definition: memif.h:66
uword
u64 uword
Definition: types.h:112
memif_socket_file_t::ref_cnt
int ref_cnt
Definition: private.h:97
memif_main
memif_main_t memif_main
Definition: memif.c:43
memif_if_t::remote_if_name
u8 * remote_if_name
Definition: private.h:189
memif_region_t::shm
void * shm
Definition: private.h:111
memif_process_event_t
memif_process_event_t
Definition: private.h:267
memif_get_buffer
static_always_inline void * memif_get_buffer(memif_if_t *mif, memif_ring_t *ring, u16 slot)
Definition: private.h:302
memif_per_thread_data_t::buffer_template
vlib_buffer_t buffer_template
Definition: private.h:239
memif_if_t::dev_instance
uword dev_instance
Definition: private.h:171
foreach_memif_if_flag
#define foreach_memif_if_flag
Definition: private.h:147
memif_region_t::is_external
u8 is_external
Definition: private.h:114
memif_region_index_t
uint16_t memif_region_index_t
Definition: memif.h:62
memif_master_conn_fd_write_ready
clib_error_t * memif_master_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:579
memif_connect
clib_error_t * memif_connect(memif_if_t *mif)
Definition: memif.c:229
memif_per_thread_data_t::copy_ops
memif_copy_op_t * copy_ops
Definition: private.h:235
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
memif_main_t::socket_file_index_by_sock_id
uword * socket_file_index_by_sock_id
Definition: private.h:254
memif_if_t::sock
clib_socket_t * sock
Definition: private.h:177
memif_socket_file_t::is_listener
int is_listener
Definition: private.h:98
memif_slave_conn_fd_read_ready
clib_error_t * memif_slave_conn_fd_read_ready(clib_file_t *uf)
Definition: socket.c:553
memif_msg_fifo_elt_t::msg
memif_msg_t msg
Definition: private.h:119
memif_queue_t
Definition: private.h:123
memif_log2_ring_size_t
uint8_t memif_log2_ring_size_t
Definition: memif.h:68
vnet_main_t
Definition: vnet.h:76
memif_msg_t
Definition: memif.h:127
memif_socket_file_t::dev_instance_by_id
mhash_t dev_instance_by_id
Definition: private.h:101
memif_create_if_args_t::id
memif_interface_id_t id
Definition: private.h:276
memif_copy_op_t::buffer_vec_index
u16 buffer_vec_index
Definition: private.h:224
memif_if_t::buffer_size
u16 buffer_size
Definition: private.h:196
memif_create_if_args_t::rx_queues
u8 rx_queues
Definition: private.h:286
u64
unsigned long u64
Definition: types.h:89
memif_if_t::local_disc_string
u8 * local_disc_string
Definition: private.h:208
memif_queue_t::buffers
u32 * buffers
Definition: private.h:134
memif_region_size_t
uint64_t memif_region_size_t
Definition: memif.h:64
u32
unsigned int u32
Definition: types.h:88
memif_create_if_args_t::secret
u8 * secret
Definition: private.h:278
memif_interface_mode_t
memif_interface_mode_t
Definition: memif.h:55
memif_msg_fifo_elt_t
Definition: private.h:117
memif_copy_op_t
Definition: private.h:218
MEMIF_RX_VECTOR_SZ
#define MEMIF_RX_VECTOR_SZ
Definition: private.h:227
memif_interface_admin_up_down
clib_error_t * memif_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: memif.c:1076
memif_if_t::hw_if_index
u32 hw_if_index
Definition: private.h:169
memif_copy_op_t::data
void * data
Definition: private.h:221
memif_if_t::remote_disc_string
u8 * remote_disc_string
Definition: private.h:209
vlib_main_t
Definition: main.h:102
memif_queue_t::offset
memif_region_offset_t offset
Definition: private.h:130
memif_queue_t::buffer_pool_index
u8 buffer_pool_index
Definition: private.h:135
memif_slave_conn_fd_write_ready
clib_error_t * memif_slave_conn_fd_write_ready(clib_file_t *uf)
Definition: socket.c:596
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
memif_plugin_api_hookup
clib_error_t * memif_plugin_api_hookup(vlib_main_t *vm)
Definition: memif_api.c:354
memif_if_t
Definition: private.h:163
memif_if_t::num_m2s_rings
u8 num_m2s_rings
Definition: private.h:195
MEMIF_PROCESS_EVENT_STOP
@ MEMIF_PROCESS_EVENT_STOP
Definition: private.h:270
memif_per_thread_data_t::buffers
u32 * buffers
Definition: private.h:236
memif_create_if_args_t::hw_addr_set
u8 hw_addr_set
Definition: private.h:284
pad
u8 pad[3]
log2 (size of the packing page block)
Definition: bihash_doc.h:61
memif_if_t::secret
u8 * secret
Definition: private.h:180
memif_socket_close
void memif_socket_close(clib_socket_t **sock)
Definition: socket.c:43
clib_socket_t
struct _socket_t clib_socket_t
memif_slave_conn_fd_error
clib_error_t * memif_slave_conn_fd_error(clib_file_t *uf)
Definition: socket.c:604
memif_socket_file_t
Definition: private.h:91
memif_desc_t::offset
memif_region_offset_t offset
Definition: memif.h:155
memif_create_if_args_t
Definition: private.h:274
memif_socket_file_t::dev_instance_by_fd
uword * dev_instance_by_fd
Definition: private.h:104
memif_copy_op_t::buffer_offset
i16 buffer_offset
Definition: private.h:223
memif_ring_t
Definition: memif.h:165
memif_if_t::log2_ring_size
memif_log2_ring_size_t log2_ring_size
Definition: private.h:193
memif_queue_t::last_head
u16 last_head
Definition: private.h:132
memif_main_t::msg_id_base
u16 msg_id_base
API message ID base.
Definition: private.h:247
memif_queue_t::int_clib_file_index
uword int_clib_file_index
Definition: private.h:139
memif_queue_t::int_fd
int int_fd
Definition: private.h:138
memif_if_t::num_s2m_rings
u8 num_s2m_rings
Definition: private.h:194
MEMIF_PROCESS_EVENT_ADMIN_UP_DOWN
@ MEMIF_PROCESS_EVENT_ADMIN_UP_DOWN
Definition: private.h:271
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
memif_create_if_args_t::socket_id
u32 socket_id
Definition: private.h:277
memif_create_if_args_t::is_master
u8 is_master
Definition: private.h:279
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105