blob: 63a7e5e4188fcf5d1b96ea81fe94d8edc9f96cac [file] [log] [blame]
Klement Sekera58eb8662017-06-09 06:06:49 +02001/*
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>
Dave Barach59b25652017-09-10 15:04:27 -040022#include <vppinfra/file.h>
Klement Sekera58eb8662017-06-09 06:06:49 +020023#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 */
37typedef 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
Dave Barach59b25652017-09-10 15:04:27 -040048 * _(size, n). Note: each msg has space for a header.
Klement Sekera58eb8662017-06-09 06:06:49 +020049 */
50#define foreach_vl_aring_size \
Dave Barach59b25652017-09-10 15:04:27 -040051_(64+sizeof(ring_alloc_t), 1024) \
52_(256+sizeof(ring_alloc_t), 128) \
53_(1024+sizeof(ring_alloc_t), 64)
Klement Sekera58eb8662017-06-09 06:06:49 +020054
55#define foreach_clnt_aring_size \
Dave Barach59b25652017-09-10 15:04:27 -040056 _(1024+sizeof(ring_alloc_t), 1024) \
57 _(2048+sizeof(ring_alloc_t), 128) \
58 _(4096+sizeof(ring_alloc_t), 8)
Klement Sekera58eb8662017-06-09 06:06:49 +020059
60typedef 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;
Klement Sekera58eb8662017-06-09 06:06:49 +020086} 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
93void *vl_msg_api_alloc (int nbytes);
94void *vl_msg_api_alloc_or_null (int nbytes);
95void *vl_msg_api_alloc_as_if_client (int nbytes);
96void *vl_msg_api_alloc_as_if_client_or_null (int nbytes);
97void vl_msg_api_free (void *a);
98int vl_map_shmem (const char *region_name, int is_vlib);
99void vl_register_mapped_shmem_region (svm_region_t * rp);
100void vl_unmap_shmem (void);
101void vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem);
102void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem);
103void vl_msg_api_send (vl_api_registration_t * rp, u8 * elem);
104int vl_client_connect (const char *name, int ctx_quota, int input_queue_size);
105void vl_client_disconnect (void);
106unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index);
107vl_api_registration_t *vl_api_client_index_to_registration (u32 index);
108int vl_client_api_map (const char *region_name);
109void vl_client_api_unmap (void);
110void vl_set_memory_region_name (const char *name);
111void vl_set_memory_root_path (const char *root_path);
112void vl_set_memory_uid (int uid);
113void vl_set_memory_gid (int gid);
114void vl_set_global_memory_baseva (u64 baseva);
115void vl_set_global_memory_size (u64 size);
116void vl_set_api_memory_size (u64 size);
117void vl_set_global_pvt_heap_size (u64 size);
118void vl_set_api_pvt_heap_size (u64 size);
119void vl_client_disconnect_from_vlib (void);
120int vl_client_connect_to_vlib (const char *svm_name, const char *client_name,
121 int rx_queue_size);
122int vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
123 const char *client_name,
124 int rx_queue_size);
Dave Barach59b25652017-09-10 15:04:27 -0400125int vl_client_connect_to_vlib_no_map (const char *svm_name,
126 const char *client_name,
127 int rx_queue_size);
Klement Sekera58eb8662017-06-09 06:06:49 +0200128u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
129
130void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barach52851e62017-08-07 09:35:25 -0400131u32 vl_api_memclnt_create_internal (char *, unix_shared_memory_queue_t *);
Dave Barach59b25652017-09-10 15:04:27 -0400132void vl_init_shmem (svm_region_t * vlib_rp, int is_vlib,
133 int is_private_region);
134void vl_client_install_client_message_handlers (void);
135
136/* API messages over sockets */
137
138extern vlib_node_registration_t memclnt_node;
139extern 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
147typedef 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
154typedef 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
184extern socket_main_t socket_main;
185
186typedef 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
201extern socket_client_main_t socket_client_main;
202
203#define SOCKET_CLIENT_DEFAULT_BUFFER_SIZE 4096
204
205void socksvr_add_pending_output (struct clib_file *uf,
206 struct vl_api_registration_ *cf,
207 u8 * buffer, uword buffer_bytes);
208
209void vl_free_socket_registration_index (u32 pool_index);
210void vl_socket_process_msg (struct clib_file *uf,
211 struct vl_api_registration_ *rp, i8 * input_v);
212clib_error_t *vl_socket_read_ready (struct clib_file *uf);
213void vl_socket_add_pending_output (struct clib_file *uf,
214 struct vl_api_registration_ *rp,
215 u8 * buffer, uword buffer_bytes);
216void vl_socket_add_pending_output_no_flush (struct clib_file *uf,
217 struct vl_api_registration_ *rp,
218 u8 * buffer, uword buffer_bytes);
219clib_error_t *vl_socket_write_ready (struct clib_file *uf);
220void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
221u32 sockclnt_open_index (char *client_name, char *hostname, int port);
222void sockclnt_close_index (u32 index);
223void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
224vl_api_registration_t *sockclnt_get_registration (u32 index);
225void vl_api_socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
226 i8 * input_v);
227
228int
229vl_socket_client_connect (socket_client_main_t * scm, char *socket_path,
230 char *client_name, u32 socket_buffer_size);
231void vl_socket_client_read_reply (socket_client_main_t * scm);
232void vl_socket_client_enable_disable (socket_client_main_t * scm, int enable);
Klement Sekera58eb8662017-06-09 06:06:49 +0200233
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 */