2 *------------------------------------------------------------------
3 * Copyright (c) 2009 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 #ifndef included_vlibmemory_api_common_h
19 #define included_vlibmemory_api_common_h
21 #include <svm/svm_common.h>
22 #include <vppinfra/file.h>
23 #include <vlibapi/api_common.h>
24 #include <vlibmemory/unix_shared_memory_queue.h>
26 /* Allocated in shared memory */
29 * Ring-allocation scheme for client API messages
31 * Only one proc/thread has control of a given message buffer.
32 * To free a buffer allocated from one of these rings, we clear
33 * a field in the buffer (header), and leave.
35 * No locks, no hits, no errors...
37 typedef struct ring_alloc_
39 unix_shared_memory_queue_t *rp;
51 } vl_api_shm_config_type_t;
53 typedef struct vl_api_ring_config_
59 } vl_api_shm_elem_config_t;
61 STATIC_ASSERT (sizeof (vl_api_shm_elem_config_t) == 8,
62 "Size must be exactly 8 bytes");
65 * Initializers for the (shared-memory) rings
66 * _(size, n). Note: each msg has space for a header.
68 #define foreach_vl_aring_size \
69 _(64+sizeof(ring_alloc_t), 1024) \
70 _(256+sizeof(ring_alloc_t), 128) \
71 _(1024+sizeof(ring_alloc_t), 64)
73 #define foreach_clnt_aring_size \
74 _(1024+sizeof(ring_alloc_t), 1024) \
75 _(2048+sizeof(ring_alloc_t), 128) \
76 _(4096+sizeof(ring_alloc_t), 8)
78 typedef struct vl_shmem_hdr_
82 /* getpid () for the VLIB client process */
85 /* Client sends VLIB msgs here. */
86 unix_shared_memory_queue_t *vl_input_queue;
88 /* Vector of rings; one for each size. */
90 /* VLIB allocates buffers to send msgs to clients here. */
91 ring_alloc_t *vl_rings;
93 /* Clients allocate buffer to send msgs to VLIB here. */
94 ring_alloc_t *client_rings;
96 /* Number of detected application restarts */
97 u32 application_restarts;
99 /* Number of messages reclaimed during application restart */
100 u32 restart_reclaims;
102 /* Number of garbage-collected messages */
103 u32 garbage_collects;
106 #define VL_SHM_VERSION 2
108 #define VL_API_EPOCH_MASK 0xFF
109 #define VL_API_EPOCH_SHIFT 8
111 void *vl_msg_api_alloc (int nbytes);
112 void *vl_msg_api_alloc_or_null (int nbytes);
113 void *vl_msg_api_alloc_as_if_client (int nbytes);
114 void *vl_msg_api_alloc_as_if_client_or_null (int nbytes);
115 void vl_msg_api_free (void *a);
116 int vl_map_shmem (const char *region_name, int is_vlib);
117 void vl_register_mapped_shmem_region (svm_region_t * rp);
118 void vl_unmap_shmem (void);
119 void vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem);
120 void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem);
121 void vl_msg_api_send (vl_api_registration_t * rp, u8 * elem);
122 int vl_client_connect (const char *name, int ctx_quota, int input_queue_size);
123 void vl_client_disconnect (void);
124 unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index);
125 vl_api_registration_t *vl_api_client_index_to_registration (u32 index);
126 int vl_client_api_map (const char *region_name);
127 void vl_client_api_unmap (void);
128 void vl_set_memory_region_name (const char *name);
129 void vl_set_memory_root_path (const char *root_path);
130 void vl_set_memory_uid (int uid);
131 void vl_set_memory_gid (int gid);
132 void vl_set_global_memory_baseva (u64 baseva);
133 void vl_set_global_memory_size (u64 size);
134 void vl_set_api_memory_size (u64 size);
135 void vl_set_global_pvt_heap_size (u64 size);
136 void vl_set_api_pvt_heap_size (u64 size);
137 void vl_client_disconnect_from_vlib (void);
138 int vl_client_connect_to_vlib (const char *svm_name, const char *client_name,
140 int vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
141 const char *client_name,
143 int vl_client_connect_to_vlib_no_map (const char *svm_name,
144 const char *client_name,
146 u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
148 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
149 u32 vl_api_memclnt_create_internal (char *, unix_shared_memory_queue_t *);
150 void vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
151 int is_vlib, int is_private_region);
152 void vl_client_install_client_message_handlers (void);
153 void vl_api_send_pending_rpc_requests (vlib_main_t * vm);
155 /* API messages over sockets */
157 extern vlib_node_registration_t memclnt_node;
158 extern volatile int **vl_api_queue_cursizes;
160 /* Events sent to the memclnt process */
161 #define QUEUE_SIGNAL_EVENT 1
162 #define SOCKET_READ_EVENT 2
164 #define API_SOCKET_FILE "/run/vpp-api.sock"
168 clib_file_t *clib_file;
169 vl_api_registration_t *regp;
171 } vl_socket_args_for_process_t;
175 /* Server port number */
178 /* By default, localhost... */
182 * (listen, server, client) registrations. Shared memory
183 * registrations are in shared memory
185 vl_api_registration_t *registration_pool;
187 * Chain-drag variables, so message API handlers
188 * (generally) don't know whether they're talking to a socket
189 * or to a shared-memory connection.
191 vl_api_registration_t *current_rp;
192 clib_file_t *current_uf;
193 /* One input buffer, shared across all sockets */
196 /* pool of process args for socket clients */
197 vl_socket_args_for_process_t *process_args;
199 /* Listen for API connections here */
200 clib_socket_t socksvr_listen_socket;
203 extern socket_main_t socket_main;
208 /* Temporarily disable the connection, so we can keep it around... */
211 clib_socket_t client_socket;
213 u32 socket_buffer_size;
214 u8 *socket_tx_buffer;
215 u8 *socket_rx_buffer;
216 u32 socket_tx_nbytes;
217 int control_pings_outstanding;
220 clib_time_t clib_time;
221 } socket_client_main_t;
223 extern socket_client_main_t socket_client_main;
225 #define SOCKET_CLIENT_DEFAULT_BUFFER_SIZE 4096
227 void socksvr_add_pending_output (struct clib_file *uf,
228 struct vl_api_registration_ *cf,
229 u8 * buffer, uword buffer_bytes);
231 void vl_free_socket_registration_index (u32 pool_index);
232 void vl_socket_process_msg (struct clib_file *uf,
233 struct vl_api_registration_ *rp, i8 * input_v);
234 clib_error_t *vl_socket_read_ready (struct clib_file *uf);
235 void vl_socket_add_pending_output (struct clib_file *uf,
236 struct vl_api_registration_ *rp,
237 u8 * buffer, uword buffer_bytes);
238 void vl_socket_add_pending_output_no_flush (struct clib_file *uf,
239 struct vl_api_registration_ *rp,
240 u8 * buffer, uword buffer_bytes);
241 clib_error_t *vl_socket_write_ready (struct clib_file *uf);
242 void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
243 u32 sockclnt_open_index (char *client_name, char *hostname, int port);
244 void sockclnt_close_index (u32 index);
245 void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
246 vl_api_registration_t *sockclnt_get_registration (u32 index);
247 void vl_api_socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
250 int vl_socket_client_connect (char *socket_path, char *client_name,
251 u32 socket_buffer_size);
252 int vl_socket_client_init_shm (vl_api_shm_elem_config_t * config);
253 void vl_socket_client_disconnect (void);
254 int vl_socket_client_read (int wait);
255 int vl_socket_client_write (void);
256 void vl_socket_client_enable_disable (int enable);
257 void *vl_socket_client_msg_alloc (int nbytes);
259 #endif /* included_vlibmemory_api_common_h */
262 * fd.io coding-style-patch-verification: ON
265 * eval: (c-set-style "gnu")