Repair vlib API socket server
[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 /*
47  * Initializers for the (shared-memory) rings
48  * _(size, n). Note: each msg has space for a header.
49  */
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)
54
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)
59
60 typedef struct vl_shmem_hdr_
61 {
62   int version;
63
64   /* getpid () for the VLIB client process */
65   volatile int vl_pid;
66
67   /* Client sends VLIB msgs here. */
68   unix_shared_memory_queue_t *vl_input_queue;
69
70   /* Vector of rings; one for each size. */
71
72   /* VLIB allocates buffers to send msgs to clients here. */
73   ring_alloc_t *vl_rings;
74
75   /* Clients allocate buffer to send msgs to VLIB here. */
76   ring_alloc_t *client_rings;
77
78   /* Number of detected application restarts */
79   u32 application_restarts;
80
81   /* Number of messages reclaimed during application restart */
82   u32 restart_reclaims;
83
84   /* Number of garbage-collected messages */
85   u32 garbage_collects;
86 } vl_shmem_hdr_t;
87
88 #define VL_SHM_VERSION 2
89
90 #define VL_API_EPOCH_MASK 0xFF
91 #define VL_API_EPOCH_SHIFT 8
92
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,
121                                int rx_queue_size);
122 int vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
123                                              const char *client_name,
124                                              int rx_queue_size);
125 int vl_client_connect_to_vlib_no_map (const char *svm_name,
126                                       const char *client_name,
127                                       int rx_queue_size);
128 u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
129
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);
135
136 /* API messages over sockets */
137
138 extern vlib_node_registration_t memclnt_node;
139 extern volatile int **vl_api_queue_cursizes;
140
141 /* Events sent to the memclnt process */
142 #define QUEUE_SIGNAL_EVENT 1
143 #define SOCKET_READ_EVENT 2
144
145 #define API_SOCKET_FILE "/run/vpp-api.sock"
146
147 typedef struct
148 {
149   clib_file_t *clib_file;
150   vl_api_registration_t *regp;
151   u8 *data;
152 } vl_socket_args_for_process_t;
153
154 typedef struct
155 {
156   /* Server port number */
157   u8 *socket_name;
158
159   /* By default, localhost... */
160   u32 bind_address;
161
162   /*
163    * (listen, server, client) registrations. Shared memory
164    * registrations are in shared memory
165    */
166   vl_api_registration_t *registration_pool;
167   /*
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.
171    */
172   vl_api_registration_t *current_rp;
173   clib_file_t *current_uf;
174   /* One input buffer, shared across all sockets */
175   i8 *input_buffer;
176
177   /* pool of process args for socket clients */
178   vl_socket_args_for_process_t *process_args;
179
180   /* Listen for API connections here */
181   clib_socket_t socksvr_listen_socket;
182 } socket_main_t;
183
184 extern socket_main_t socket_main;
185
186 typedef struct
187 {
188   int socket_fd;
189   /* Temporarily disable the connection, so we can keep it around... */
190   int socket_enable;
191
192   clib_socket_t client_socket;
193
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;
200
201 extern socket_client_main_t socket_client_main;
202
203 #define SOCKET_CLIENT_DEFAULT_BUFFER_SIZE 4096
204
205 void socksvr_add_pending_output (struct clib_file *uf,
206                                  struct vl_api_registration_ *cf,
207                                  u8 * buffer, uword buffer_bytes);
208
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,
226                                 i8 * input_v);
227
228 int
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);
233
234 #endif /* included_vlibmemory_api_common_h */
235
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "gnu")
241  * End:
242  */