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;
47 * Initializers for the (shared-memory) rings
48 * _(size, n). Note: each msg has space for a header.
50 #define foreach_vl_aring_size \
51 _(64+sizeof(ring_alloc_t), 1024) \
52 _(256+sizeof(ring_alloc_t), 128) \
53 _(1024+sizeof(ring_alloc_t), 64)
55 #define foreach_clnt_aring_size \
56 _(1024+sizeof(ring_alloc_t), 1024) \
57 _(2048+sizeof(ring_alloc_t), 128) \
58 _(4096+sizeof(ring_alloc_t), 8)
60 typedef struct vl_shmem_hdr_
64 /* getpid () for the VLIB client process */
67 /* Client sends VLIB msgs here. */
68 unix_shared_memory_queue_t *vl_input_queue;
70 /* Vector of rings; one for each size. */
72 /* VLIB allocates buffers to send msgs to clients here. */
73 ring_alloc_t *vl_rings;
75 /* Clients allocate buffer to send msgs to VLIB here. */
76 ring_alloc_t *client_rings;
78 /* Number of detected application restarts */
79 u32 application_restarts;
81 /* Number of messages reclaimed during application restart */
84 /* Number of garbage-collected messages */
88 #define VL_SHM_VERSION 2
90 #define VL_API_EPOCH_MASK 0xFF
91 #define VL_API_EPOCH_SHIFT 8
93 void *vl_msg_api_alloc (int nbytes);
94 void *vl_msg_api_alloc_or_null (int nbytes);
95 void *vl_msg_api_alloc_as_if_client (int nbytes);
96 void *vl_msg_api_alloc_as_if_client_or_null (int nbytes);
97 void vl_msg_api_free (void *a);
98 int vl_map_shmem (const char *region_name, int is_vlib);
99 void vl_register_mapped_shmem_region (svm_region_t * rp);
100 void vl_unmap_shmem (void);
101 void vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem);
102 void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem);
103 void vl_msg_api_send (vl_api_registration_t * rp, u8 * elem);
104 int vl_client_connect (const char *name, int ctx_quota, int input_queue_size);
105 void vl_client_disconnect (void);
106 unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index);
107 vl_api_registration_t *vl_api_client_index_to_registration (u32 index);
108 int vl_client_api_map (const char *region_name);
109 void vl_client_api_unmap (void);
110 void vl_set_memory_region_name (const char *name);
111 void vl_set_memory_root_path (const char *root_path);
112 void vl_set_memory_uid (int uid);
113 void vl_set_memory_gid (int gid);
114 void vl_set_global_memory_baseva (u64 baseva);
115 void vl_set_global_memory_size (u64 size);
116 void vl_set_api_memory_size (u64 size);
117 void vl_set_global_pvt_heap_size (u64 size);
118 void vl_set_api_pvt_heap_size (u64 size);
119 void vl_client_disconnect_from_vlib (void);
120 int vl_client_connect_to_vlib (const char *svm_name, const char *client_name,
122 int vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
123 const char *client_name,
125 int vl_client_connect_to_vlib_no_map (const char *svm_name,
126 const char *client_name,
128 u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
130 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
131 u32 vl_api_memclnt_create_internal (char *, unix_shared_memory_queue_t *);
132 void vl_init_shmem (svm_region_t * vlib_rp, int is_vlib,
133 int is_private_region);
134 void vl_client_install_client_message_handlers (void);
136 /* API messages over sockets */
138 extern vlib_node_registration_t memclnt_node;
139 extern volatile int **vl_api_queue_cursizes;
141 /* Events sent to the memclnt process */
142 #define QUEUE_SIGNAL_EVENT 1
143 #define SOCKET_READ_EVENT 2
145 #define API_SOCKET_FILE "/run/vpp-api.sock"
149 clib_file_t *clib_file;
150 vl_api_registration_t *regp;
152 } vl_socket_args_for_process_t;
156 /* Server port number */
159 /* By default, localhost... */
163 * (listen, server, client) registrations. Shared memory
164 * registrations are in shared memory
166 vl_api_registration_t *registration_pool;
168 * Chain-drag variables, so message API handlers
169 * (generally) don't know whether they're talking to a socket
170 * or to a shared-memory connection.
172 vl_api_registration_t *current_rp;
173 clib_file_t *current_uf;
174 /* One input buffer, shared across all sockets */
177 /* pool of process args for socket clients */
178 vl_socket_args_for_process_t *process_args;
180 /* Listen for API connections here */
181 clib_socket_t socksvr_listen_socket;
184 extern socket_main_t socket_main;
189 /* Temporarily disable the connection, so we can keep it around... */
192 clib_socket_t client_socket;
194 u32 socket_buffer_size;
195 u8 *socket_tx_buffer;
196 u8 *socket_rx_buffer;
197 u32 socket_tx_nbytes;
198 int control_pings_outstanding;
199 } socket_client_main_t;
201 extern socket_client_main_t socket_client_main;
203 #define SOCKET_CLIENT_DEFAULT_BUFFER_SIZE 4096
205 void socksvr_add_pending_output (struct clib_file *uf,
206 struct vl_api_registration_ *cf,
207 u8 * buffer, uword buffer_bytes);
209 void vl_free_socket_registration_index (u32 pool_index);
210 void vl_socket_process_msg (struct clib_file *uf,
211 struct vl_api_registration_ *rp, i8 * input_v);
212 clib_error_t *vl_socket_read_ready (struct clib_file *uf);
213 void vl_socket_add_pending_output (struct clib_file *uf,
214 struct vl_api_registration_ *rp,
215 u8 * buffer, uword buffer_bytes);
216 void vl_socket_add_pending_output_no_flush (struct clib_file *uf,
217 struct vl_api_registration_ *rp,
218 u8 * buffer, uword buffer_bytes);
219 clib_error_t *vl_socket_write_ready (struct clib_file *uf);
220 void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
221 u32 sockclnt_open_index (char *client_name, char *hostname, int port);
222 void sockclnt_close_index (u32 index);
223 void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
224 vl_api_registration_t *sockclnt_get_registration (u32 index);
225 void vl_api_socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
229 vl_socket_client_connect (socket_client_main_t * scm, char *socket_path,
230 char *client_name, u32 socket_buffer_size);
231 void vl_socket_client_read_reply (socket_client_main_t * scm);
232 void vl_socket_client_enable_disable (socket_client_main_t * scm, int enable);
234 #endif /* included_vlibmemory_api_common_h */
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "gnu")