sock api: add infra for bootstrapping shm clients
[vpp.git] / src / vlibmemory / api_common.h
1 /*
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:
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 #ifndef included_vlibmemory_api_common_h
19 #define included_vlibmemory_api_common_h
20
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>
25
26 /* Allocated in shared memory */
27
28 /*
29  * Ring-allocation scheme for client API messages
30  *
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.
34  *
35  * No locks, no hits, no errors...
36  */
37 typedef struct ring_alloc_
38 {
39   unix_shared_memory_queue_t *rp;
40   u16 size;
41   u16 nitems;
42   u32 hits;
43   u32 misses;
44 } ring_alloc_t;
45
46 typedef enum
47 {
48   VL_API_VLIB_RING,
49   VL_API_CLIENT_RING,
50   VL_API_QUEUE
51 } vl_api_shm_config_type_t;
52
53 typedef struct vl_api_ring_config_
54 {
55   u8 type;
56   u8 _pad;
57   u16 count;
58   u32 size;
59 } vl_api_shm_elem_config_t;
60
61 STATIC_ASSERT (sizeof (vl_api_shm_elem_config_t) == 8,
62                "Size must be exactly 8 bytes");
63
64 /*
65  * Initializers for the (shared-memory) rings
66  * _(size, n). Note: each msg has space for a header.
67  */
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)
72
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)
77
78 typedef struct vl_shmem_hdr_
79 {
80   int version;
81
82   /* getpid () for the VLIB client process */
83   volatile int vl_pid;
84
85   /* Client sends VLIB msgs here. */
86   unix_shared_memory_queue_t *vl_input_queue;
87
88   /* Vector of rings; one for each size. */
89
90   /* VLIB allocates buffers to send msgs to clients here. */
91   ring_alloc_t *vl_rings;
92
93   /* Clients allocate buffer to send msgs to VLIB here. */
94   ring_alloc_t *client_rings;
95
96   /* Number of detected application restarts */
97   u32 application_restarts;
98
99   /* Number of messages reclaimed during application restart */
100   u32 restart_reclaims;
101
102   /* Number of garbage-collected messages */
103   u32 garbage_collects;
104 } vl_shmem_hdr_t;
105
106 #define VL_SHM_VERSION 2
107
108 #define VL_API_EPOCH_MASK 0xFF
109 #define VL_API_EPOCH_SHIFT 8
110
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,
139                                int rx_queue_size);
140 int vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
141                                              const char *client_name,
142                                              int rx_queue_size);
143 int vl_client_connect_to_vlib_no_map (const char *svm_name,
144                                       const char *client_name,
145                                       int rx_queue_size);
146 u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
147
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);
154
155 /* API messages over sockets */
156
157 extern vlib_node_registration_t memclnt_node;
158 extern volatile int **vl_api_queue_cursizes;
159
160 /* Events sent to the memclnt process */
161 #define QUEUE_SIGNAL_EVENT 1
162 #define SOCKET_READ_EVENT 2
163
164 #define API_SOCKET_FILE "/run/vpp-api.sock"
165
166 typedef struct
167 {
168   clib_file_t *clib_file;
169   vl_api_registration_t *regp;
170   u8 *data;
171 } vl_socket_args_for_process_t;
172
173 typedef struct
174 {
175   /* Server port number */
176   u8 *socket_name;
177
178   /* By default, localhost... */
179   u32 bind_address;
180
181   /*
182    * (listen, server, client) registrations. Shared memory
183    * registrations are in shared memory
184    */
185   vl_api_registration_t *registration_pool;
186   /*
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.
190    */
191   vl_api_registration_t *current_rp;
192   clib_file_t *current_uf;
193   /* One input buffer, shared across all sockets */
194   i8 *input_buffer;
195
196   /* pool of process args for socket clients */
197   vl_socket_args_for_process_t *process_args;
198
199   /* Listen for API connections here */
200   clib_socket_t socksvr_listen_socket;
201 } socket_main_t;
202
203 extern socket_main_t socket_main;
204
205 typedef struct
206 {
207   int socket_fd;
208   /* Temporarily disable the connection, so we can keep it around... */
209   int socket_enable;
210
211   clib_socket_t client_socket;
212
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;
218
219   u8 *name;
220   clib_time_t clib_time;
221 } socket_client_main_t;
222
223 extern socket_client_main_t socket_client_main;
224
225 #define SOCKET_CLIENT_DEFAULT_BUFFER_SIZE 4096
226
227 void socksvr_add_pending_output (struct clib_file *uf,
228                                  struct vl_api_registration_ *cf,
229                                  u8 * buffer, uword buffer_bytes);
230
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,
248                                 i8 * input_v);
249
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);
258
259 #endif /* included_vlibmemory_api_common_h */
260
261 /*
262  * fd.io coding-style-patch-verification: ON
263  *
264  * Local Variables:
265  * eval: (c-set-style "gnu")
266  * End:
267  */