blob: 338fda45241db038d826b592d4341c3c06278603 [file] [log] [blame]
Dave Barach371e4e12016-07-08 09:38:52 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
Dave Barach371e4e12016-07-08 09:38:52 -04003 * memory_vlib.c
Ed Warnickecb9cada2015-12-08 15:45:58 -07004 *
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24#include <sys/types.h>
Dave Barach80f54e22017-03-08 19:08:56 -050025#include <sys/stat.h>
26#include <fcntl.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027#include <signal.h>
28#include <pthread.h>
29#include <vppinfra/vec.h>
30#include <vppinfra/hash.h>
31#include <vppinfra/pool.h>
32#include <vppinfra/format.h>
33#include <vppinfra/byte_order.h>
34#include <vppinfra/elog.h>
35#include <stdarg.h>
36#include <vlib/vlib.h>
37#include <vlib/unix/unix.h>
38#include <vlibapi/api.h>
39#include <vlibmemory/api.h>
40
Dave Barach49fe0462017-09-12 17:06:56 -040041/**
42 * @file
43 * @brief Binary API messaging via shared memory
44 * Low-level, primary provisioning interface
45 */
46/*? %%clicmd:group_label Binary API CLI %% ?*/
47/*? %%syscfg:group_label Binary API configuration %% ?*/
48
Ed Warnickecb9cada2015-12-08 15:45:58 -070049#define TRACE_VLIB_MEMORY_QUEUE 0
50
Dave Barach371e4e12016-07-08 09:38:52 -040051#include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Dave Barach371e4e12016-07-08 09:38:52 -040053#define vl_typedefs /* define message structures */
54#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070055#undef vl_typedefs
56
57/* instantiate all the print functions we know about */
58#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
59#define vl_printfun
Dave Barach371e4e12016-07-08 09:38:52 -040060#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070061#undef vl_printfun
62
63static inline void *
Dave Barach371e4e12016-07-08 09:38:52 -040064vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Dave Barach371e4e12016-07-08 09:38:52 -040066 vl_print (handle, "vl_api_memclnt_create_t:\n");
67 vl_print (handle, "name: %s\n", a->name);
68 vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
69 vl_print (handle, "context: %u\n", (unsigned) a->context);
70 vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
71 return handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072}
73
74static inline void *
Dave Barach371e4e12016-07-08 09:38:52 -040075vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
Dave Barach371e4e12016-07-08 09:38:52 -040077 vl_print (handle, "vl_api_memclnt_delete_t:\n");
78 vl_print (handle, "index: %u\n", (unsigned) a->index);
79 vl_print (handle, "handle: 0x%wx\n", a->handle);
80 return handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081}
82
Dave Barach987e11d2017-02-27 13:10:27 -050083static inline void *
84vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
85 void *handle)
86{
87 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
88 a->plugin_name,
89 clib_host_to_net_u16 (a->first_msg_id),
90 clib_host_to_net_u16 (a->last_msg_id));
91 return handle;
92}
93
Ed Warnickecb9cada2015-12-08 15:45:58 -070094/* instantiate all the endian swap functions we know about */
95#define vl_endianfun
Dave Barach371e4e12016-07-08 09:38:52 -040096#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070097#undef vl_endianfun
98
Dave Barach59b25652017-09-10 15:04:27 -040099extern void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
Dave Barach371e4e12016-07-08 09:38:52 -0400101void
102vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103{
Dave Barach371e4e12016-07-08 09:38:52 -0400104 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
105 {
106 vl_socket_api_send (rp, elem);
107 }
108 else
109 {
Dave Barach59b25652017-09-10 15:04:27 -0400110 vl_msg_api_send_shmem (rp->vl_input_queue, (u8 *) & elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 }
112}
113
Dave Barach557d1282016-11-10 14:22:49 -0500114u8 *
115vl_api_serialize_message_table (api_main_t * am, u8 * vector)
Dave Barach371e4e12016-07-08 09:38:52 -0400116{
Dave Barach557d1282016-11-10 14:22:49 -0500117 serialize_main_t _sm, *sm = &_sm;
118 hash_pair_t *hp;
119 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
120
121 serialize_open_vector (sm, vector);
122
123 /* serialize the count */
124 serialize_integer (sm, nmsg, sizeof (u32));
125
Dave Barach987e11d2017-02-27 13:10:27 -0500126 /* *INDENT-OFF* */
127 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
128 ({
129 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
130 serialize_cstring (sm, (char *) hp->key);
131 }));
132 /* *INDENT-ON* */
Dave Barach557d1282016-11-10 14:22:49 -0500133
134 return serialize_close_vector (sm);
Dave Barach371e4e12016-07-08 09:38:52 -0400135}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136
137/*
Dave Barach52851e62017-08-07 09:35:25 -0400138 * vl_api_memclnt_create_internal
139 */
140
141u32
142vl_api_memclnt_create_internal (char *name, unix_shared_memory_queue_t * q)
143{
144 vl_api_registration_t **regpp;
145 vl_api_registration_t *regp;
146 svm_region_t *svm;
147 void *oldheap;
148 api_main_t *am = &api_main;
149
150 ASSERT (vlib_get_thread_index () == 0);
151 pool_get (am->vl_clients, regpp);
152
153 svm = am->vlib_rp;
154
155 pthread_mutex_lock (&svm->mutex);
156 oldheap = svm_push_data_heap (svm);
157 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
158
159 regp = *regpp;
160 memset (regp, 0, sizeof (*regp));
161 regp->registration_type = REGISTRATION_TYPE_SHMEM;
162 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Florin Coras780fc392017-10-03 14:26:10 -0400163 regp->vlib_rp = svm;
164 regp->shmem_hdr = am->shmem_hdr;
Dave Barach52851e62017-08-07 09:35:25 -0400165
166 regp->vl_input_queue = q;
167 regp->name = format (0, "%s%c", name, 0);
168
169 pthread_mutex_unlock (&svm->mutex);
170 svm_pop_heap (oldheap);
171 return vl_msg_api_handle_from_index_and_epoch
172 (regp->vl_api_registration_pool_index,
173 am->shmem_hdr->application_restarts);
174}
175
176
177/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 * vl_api_memclnt_create_t_handler
179 */
Dave Barach371e4e12016-07-08 09:38:52 -0400180void
181vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182{
Dave Barach371e4e12016-07-08 09:38:52 -0400183 vl_api_registration_t **regpp;
184 vl_api_registration_t *regp;
185 vl_api_memclnt_create_reply_t *rp;
186 svm_region_t *svm;
187 unix_shared_memory_queue_t *q;
Dave Barach557d1282016-11-10 14:22:49 -0500188 int rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400189 void *oldheap;
190 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barach371e4e12016-07-08 09:38:52 -0400192 /*
193 * This is tortured. Maintain a vlib-address-space private
194 * pool of client registrations. We use the shared-memory virtual
195 * address of client structure as a handle, to allow direct
196 * manipulation of context quota vbls from the client library.
197 *
198 * This scheme causes trouble w/ API message trace replay, since
199 * some random VA from clib_mem_alloc() certainly won't
200 * occur in the Linux sim. The (very) few places
201 * that care need to use the pool index.
202 *
203 * Putting the registration object(s) into a pool in shared memory and
204 * using the pool index as a handle seems like a great idea.
205 * Unfortunately, each and every reference to that pool would need
206 * to be protected by a mutex:
207 *
208 * Client VLIB
209 * ------ ----
210 * convert pool index to
211 * pointer.
212 * <deschedule>
213 * expand pool
214 * <deschedule>
215 * kaboom!
216 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
Dave Barach371e4e12016-07-08 09:38:52 -0400218 pool_get (am->vl_clients, regpp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Dave Barach371e4e12016-07-08 09:38:52 -0400220 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Dave Barach371e4e12016-07-08 09:38:52 -0400222 pthread_mutex_lock (&svm->mutex);
223 oldheap = svm_push_data_heap (svm);
224 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Dave Barach371e4e12016-07-08 09:38:52 -0400226 regp = *regpp;
227 memset (regp, 0, sizeof (*regp));
228 regp->registration_type = REGISTRATION_TYPE_SHMEM;
229 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Dave Barach59b25652017-09-10 15:04:27 -0400230 regp->vlib_rp = svm;
231 regp->shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
Dave Barach371e4e12016-07-08 09:38:52 -0400233 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
234 mp->input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Dave Barach371e4e12016-07-08 09:38:52 -0400236 regp->name = format (0, "%s", mp->name);
237 vec_add1 (regp->name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barachcf5e8482017-10-17 11:48:29 -0400239 if (am->serialized_message_table_in_shmem == 0)
240 am->serialized_message_table_in_shmem =
241 vl_api_serialize_message_table (am, 0);
Dave Barach59b25652017-09-10 15:04:27 -0400242
Dave Barach371e4e12016-07-08 09:38:52 -0400243 pthread_mutex_unlock (&svm->mutex);
244 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Dave Barach371e4e12016-07-08 09:38:52 -0400246 rp = vl_msg_api_alloc (sizeof (*rp));
247 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
248 rp->handle = (uword) regp;
249 rp->index = vl_msg_api_handle_from_index_and_epoch
250 (regp->vl_api_registration_pool_index,
251 am->shmem_hdr->application_restarts);
252 rp->context = mp->context;
253 rp->response = ntohl (rv);
Dave Barachcf5e8482017-10-17 11:48:29 -0400254 rp->message_table =
255 pointer_to_uword (am->serialized_message_table_in_shmem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barach371e4e12016-07-08 09:38:52 -0400257 vl_msg_api_send_shmem (q, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258}
259
Dave Barachb64e4e22017-03-14 09:10:56 -0400260static int
261call_reaper_functions (u32 client_index)
Dave Barach371e4e12016-07-08 09:38:52 -0400262{
Dave Barachb64e4e22017-03-14 09:10:56 -0400263 clib_error_t *error = 0;
264 _vl_msg_api_function_list_elt_t *i;
265
266 i = api_main.reaper_function_registrations;
267 while (i)
268 {
269 error = i->f (client_index);
270 if (error)
271 clib_error_report (error);
272 i = i->next_init_function;
273 }
Dave Barach371e4e12016-07-08 09:38:52 -0400274 return 0;
275}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
277/*
278 * vl_api_memclnt_delete_t_handler
279 */
Dave Barach371e4e12016-07-08 09:38:52 -0400280void
281vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282{
Dave Barach371e4e12016-07-08 09:38:52 -0400283 vl_api_registration_t **regpp;
284 vl_api_registration_t *regp;
285 vl_api_memclnt_delete_reply_t *rp;
286 svm_region_t *svm;
287 void *oldheap;
288 api_main_t *am = &api_main;
289 u32 handle, client_index, epoch;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Dave Barach371e4e12016-07-08 09:38:52 -0400291 handle = mp->index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Dave Barachb64e4e22017-03-14 09:10:56 -0400293 if (call_reaper_functions (handle))
Dave Barach371e4e12016-07-08 09:38:52 -0400294 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Dave Barach371e4e12016-07-08 09:38:52 -0400296 epoch = vl_msg_api_handle_get_epoch (handle);
297 client_index = vl_msg_api_handle_get_index (handle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Dave Barach371e4e12016-07-08 09:38:52 -0400299 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
300 {
301 clib_warning
302 ("Stale clnt delete index %d old epoch %d cur epoch %d",
303 client_index, epoch,
304 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
305 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 }
307
Dave Barach371e4e12016-07-08 09:38:52 -0400308 regpp = am->vl_clients + client_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
Dave Barach371e4e12016-07-08 09:38:52 -0400310 if (!pool_is_free (am->vl_clients, regpp))
311 {
Dave Barach59b25652017-09-10 15:04:27 -0400312 int i;
Dave Barach371e4e12016-07-08 09:38:52 -0400313 regp = *regpp;
314 svm = am->vlib_rp;
Dave Barach59b25652017-09-10 15:04:27 -0400315 int private_registration = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316
Dave Barach59b25652017-09-10 15:04:27 -0400317 /*
318 * Note: the API message handling path will set am->vlib_rp
319 * as appropriate for pairwise / private memory segments
320 */
Dave Barach371e4e12016-07-08 09:38:52 -0400321 rp = vl_msg_api_alloc (sizeof (*rp));
322 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
323 rp->handle = mp->handle;
324 rp->response = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
Dave Barach371e4e12016-07-08 09:38:52 -0400326 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Dave Barach371e4e12016-07-08 09:38:52 -0400328 if (client_index != regp->vl_api_registration_pool_index)
329 {
330 clib_warning ("mismatch client_index %d pool_index %d",
331 client_index, regp->vl_api_registration_pool_index);
332 vl_msg_api_free (rp);
333 return;
334 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Dave Barach59b25652017-09-10 15:04:27 -0400336 /* For horizontal scaling, add a hash table... */
337 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
338 {
339 /* Is this a pairwise / private API segment? */
340 if (am->vlib_private_rps[i] == svm)
341 {
342 /* Note: account for the memfd header page */
343 u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
344 u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
345
346 /*
347 * Kill the registration pool element before we make
348 * the index vanish forever
349 */
350 pool_put_index (am->vl_clients,
351 regp->vl_api_registration_pool_index);
352
353 vec_delete (am->vlib_private_rps, 1, i);
354 /* Kill it, accounting for the memfd header page */
355 if (munmap ((void *) virtual_base, virtual_size) < 0)
356 clib_unix_warning ("munmap");
357 /* Reset the queue-length-address cache */
358 vec_reset_length (vl_api_queue_cursizes);
359 private_registration = 1;
360 break;
361 }
362 }
363
Dave Barach371e4e12016-07-08 09:38:52 -0400364 /* No dangling references, please */
365 *regpp = 0;
366
Dave Barach59b25652017-09-10 15:04:27 -0400367 if (private_registration == 0)
368 {
369 pool_put_index (am->vl_clients,
370 regp->vl_api_registration_pool_index);
371 pthread_mutex_lock (&svm->mutex);
372 oldheap = svm_push_data_heap (svm);
373 /* Poison the old registration */
374 memset (regp, 0xF1, sizeof (*regp));
375 clib_mem_free (regp);
376 pthread_mutex_unlock (&svm->mutex);
377 svm_pop_heap (oldheap);
378 /*
379 * These messages must be freed manually, since they're set up
380 * as "bounce" messages. In the private_registration == 1 case,
381 * we kill the shared-memory segment which contains the message
382 * with munmap.
383 */
384 vl_msg_api_free (mp);
385 }
Dave Barach371e4e12016-07-08 09:38:52 -0400386 }
387 else
388 {
389 clib_warning ("unknown client ID %d", mp->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 }
391}
392
Dave Barach371e4e12016-07-08 09:38:52 -0400393void
394vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395{
Dave Barach371e4e12016-07-08 09:38:52 -0400396 vl_api_get_first_msg_id_reply_t *rmp;
397 unix_shared_memory_queue_t *q;
398 uword *p;
399 api_main_t *am = &api_main;
400 vl_api_msg_range_t *rp;
401 u8 name[64];
402 u16 first_msg_id = ~0;
403 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
Dave Barach371e4e12016-07-08 09:38:52 -0400405 q = vl_api_client_index_to_input_queue (mp->client_index);
406 if (!q)
407 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Dave Barach371e4e12016-07-08 09:38:52 -0400409 if (am->msg_range_by_name == 0)
410 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Dave Barach371e4e12016-07-08 09:38:52 -0400412 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413
Dave Barach371e4e12016-07-08 09:38:52 -0400414 p = hash_get_mem (am->msg_range_by_name, name);
415 if (p == 0)
416 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
Dave Barach371e4e12016-07-08 09:38:52 -0400418 rp = vec_elt_at_index (am->msg_ranges, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Dave Barach371e4e12016-07-08 09:38:52 -0400420 first_msg_id = rp->first_msg_id;
421 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
423out:
424
Dave Barach371e4e12016-07-08 09:38:52 -0400425 rmp = vl_msg_api_alloc (sizeof (*rmp));
426 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
427 rmp->context = mp->context;
428 rmp->retval = ntohl (rv);
429 rmp->first_msg_id = ntohs (first_msg_id);
430 vl_msg_api_send_shmem (q, (u8 *) & rmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431}
432
Dave Barach59b25652017-09-10 15:04:27 -0400433/**
434 * client answered a ping, stave off the grim reaper...
435 */
436
437void
438 vl_api_memclnt_keepalive_reply_t_handler
439 (vl_api_memclnt_keepalive_reply_t * mp)
440{
441 vl_api_registration_t *regp;
442 vlib_main_t *vm = vlib_get_main ();
443
444 regp = vl_api_client_index_to_registration (mp->context);
445 if (regp)
446 {
447 regp->last_heard = vlib_time_now (vm);
448 regp->unanswered_pings = 0;
449 }
450 else
451 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
452}
453
454/**
455 * We can send ourselves these messages if someone uses the
456 * builtin binary api test tool...
457 */
458static void
459vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
460{
461 vl_api_memclnt_keepalive_reply_t *rmp;
462 api_main_t *am;
463 vl_shmem_hdr_t *shmem_hdr;
464
465 am = &api_main;
466 shmem_hdr = am->shmem_hdr;
467
468 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
469 memset (rmp, 0, sizeof (*rmp));
470 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
471 rmp->context = mp->context;
472 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
473}
474
Dave Barach0d056e52017-09-28 15:11:16 -0400475void
476vl_api_api_versions_t_handler (vl_api_api_versions_t * mp)
477{
478 api_main_t *am = &api_main;
479 vl_api_api_versions_reply_t *rmp;
480 unix_shared_memory_queue_t *q;
481 u32 nmsg = vec_len (am->api_version_list);
482 int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
483 int i;
484
485 q = vl_api_client_index_to_input_queue (mp->client_index);
486 if (q == 0)
487 return;
488
489 rmp = vl_msg_api_alloc (msg_size);
490 memset (rmp, 0, msg_size);
491 rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);
492
493 /* fill in the message */
494 rmp->context = mp->context;
495 rmp->count = htonl (nmsg);
496
497 for (i = 0; i < nmsg; ++i)
498 {
499 api_version_t *vl = &am->api_version_list[i];
500 rmp->api_versions[i].major = htonl (vl->major);
501 rmp->api_versions[i].minor = htonl (vl->minor);
502 rmp->api_versions[i].patch = htonl (vl->patch);
Ole Troan7504e992017-10-10 08:43:35 +0200503 strncpy ((char *) rmp->api_versions[i].name, vl->name, 64 - 1);
Dave Barach0d056e52017-09-28 15:11:16 -0400504 }
505
506 vl_msg_api_send_shmem (q, (u8 *) & rmp);
507
508}
509
Dave Barach59b25652017-09-10 15:04:27 -0400510#define foreach_vlib_api_msg \
511_(MEMCLNT_CREATE, memclnt_create) \
512_(MEMCLNT_DELETE, memclnt_delete) \
513_(GET_FIRST_MSG_ID, get_first_msg_id) \
514_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
Dave Barach0d056e52017-09-28 15:11:16 -0400515_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply) \
516_(API_VERSIONS, api_versions)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
518/*
519 * vl_api_init
520 */
Dave Barach371e4e12016-07-08 09:38:52 -0400521static int
Neale Rannse72be392017-04-26 13:59:20 -0700522memory_api_init (const char *region_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523{
Dave Barach371e4e12016-07-08 09:38:52 -0400524 int rv;
Dave Barach59b25652017-09-10 15:04:27 -0400525 api_main_t *am = &api_main;
Dave Barach371e4e12016-07-08 09:38:52 -0400526 vl_msg_api_msg_config_t cfg;
527 vl_msg_api_msg_config_t *c = &cfg;
528
Dave Barach0691d6e2017-01-05 10:08:52 -0500529 memset (c, 0, sizeof (*c));
530
Dave Barach371e4e12016-07-08 09:38:52 -0400531 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
532 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
534#define _(N,n) do { \
535 c->id = VL_API_##N; \
536 c->name = #n; \
537 c->handler = vl_api_##n##_t_handler; \
538 c->cleanup = vl_noop_handler; \
539 c->endian = vl_api_##n##_t_endian; \
540 c->print = vl_api_##n##_t_print; \
541 c->size = sizeof(vl_api_##n##_t); \
542 c->traced = 1; /* trace, so these msgs print */ \
543 c->replay = 0; /* don't replay client create/delete msgs */ \
Dave Barach0691d6e2017-01-05 10:08:52 -0500544 c->message_bounce = 0; /* don't bounce this message */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 vl_msg_api_config(c);} while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400546
547 foreach_vlib_api_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548#undef _
549
Dave Barach59b25652017-09-10 15:04:27 -0400550 /*
551 * special-case freeing of memclnt_delete messages, so we can
552 * simply munmap pairwise / private API segments...
553 */
554 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
555 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
556
Dave Barach371e4e12016-07-08 09:38:52 -0400557 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558}
559
560#define foreach_histogram_bucket \
561_(400) \
562_(200) \
563_(100) \
564_(10)
565
Dave Barach371e4e12016-07-08 09:38:52 -0400566typedef enum
567{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568#define _(n) SLEEP_##n##_US,
Dave Barach371e4e12016-07-08 09:38:52 -0400569 foreach_histogram_bucket
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570#undef _
571 SLEEP_N_BUCKETS,
572} histogram_index_t;
573
574static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
575
Dave Barach371e4e12016-07-08 09:38:52 -0400576static void memclnt_queue_callback (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
Dave Barach987e11d2017-02-27 13:10:27 -0500578/*
579 * Callback to send ourselves a plugin numbering-space trace msg
580 */
581static void
582send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
583{
584 vl_api_trace_plugin_msg_ids_t *mp;
585 api_main_t *am = &api_main;
586 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
587 unix_shared_memory_queue_t *q;
588
589 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
590 memset (mp, 0, sizeof (*mp));
591
592 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
593 strncpy ((char *) mp->plugin_name, (char *) name,
594 sizeof (mp->plugin_name) - 1);
595 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
596 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
597
598 q = shmem_hdr->vl_input_queue;
599
600 vl_msg_api_send_shmem (q, (u8 *) & mp);
601}
602
Dave Barach59b25652017-09-10 15:04:27 -0400603static void
604send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
605{
606 vl_api_memclnt_keepalive_t *mp;
607 unix_shared_memory_queue_t *q;
608 api_main_t *am = &api_main;
609 svm_region_t *save_vlib_rp = am->vlib_rp;
610 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
611
612 q = regp->vl_input_queue;
613
614 /*
615 * If the queue head is moving, assume that the client is processing
616 * messages and skip the ping. This heuristic may fail if the queue
617 * is in the same position as last time, net of wrapping; in which
618 * case, the client will receive a keepalive.
619 */
620 if (regp->last_queue_head != q->head)
621 {
622 regp->last_heard = now;
623 regp->unanswered_pings = 0;
624 regp->last_queue_head = q->head;
625 return;
626 }
627
628 /*
629 * push/pop shared memory segment, so this routine
630 * will work with "normal" as well as "private segment"
631 * memory clients..
632 */
633
634 am->vlib_rp = regp->vlib_rp;
635 am->shmem_hdr = regp->shmem_hdr;
636
637 mp = vl_msg_api_alloc (sizeof (*mp));
638 memset (mp, 0, sizeof (*mp));
639 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
640 mp->context = mp->client_index =
641 vl_msg_api_handle_from_index_and_epoch
642 (regp->vl_api_registration_pool_index,
643 am->shmem_hdr->application_restarts);
644
645 regp->unanswered_pings++;
646
647 /* Failure-to-send due to a stuffed queue is absolutely expected */
648 if (unix_shared_memory_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
649 vl_msg_api_free (mp);
650
651 am->vlib_rp = save_vlib_rp;
652 am->shmem_hdr = save_shmem_hdr;
653}
654
655static void
656dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
657{
658
659 vl_api_registration_t **regpp;
660 vl_api_registration_t *regp;
661 static u32 *dead_indices;
662 static u32 *confused_indices;
663
664 vec_reset_length (dead_indices);
665 vec_reset_length (confused_indices);
666
667 /* *INDENT-OFF* */
668 pool_foreach (regpp, am->vl_clients,
669 ({
670 regp = *regpp;
671 if (regp)
672 {
673 /* If we haven't heard from this client recently... */
674 if (regp->last_heard < (now - 10.0))
675 {
676 if (regp->unanswered_pings == 2)
677 {
678 unix_shared_memory_queue_t *q;
679 q = regp->vl_input_queue;
680 if (kill (q->consumer_pid, 0) >=0)
681 {
682 clib_warning ("REAPER: lazy binary API client '%s'",
683 regp->name);
684 regp->unanswered_pings = 0;
685 regp->last_heard = now;
686 }
687 else
688 {
689 clib_warning ("REAPER: binary API client '%s' died",
690 regp->name);
691 vec_add1(dead_indices, regpp - am->vl_clients);
692 }
693 }
694 else
695 send_memclnt_keepalive (regp, now);
696 }
697 else
698 regp->unanswered_pings = 0;
699 }
700 else
701 {
702 clib_warning ("NULL client registration index %d",
703 regpp - am->vl_clients);
704 vec_add1 (confused_indices, regpp - am->vl_clients);
705 }
706 }));
707 /* *INDENT-ON* */
708 /* This should "never happen," but if it does, fix it... */
709 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
710 {
711 int i;
712 for (i = 0; i < vec_len (confused_indices); i++)
713 {
714 pool_put_index (am->vl_clients, confused_indices[i]);
715 }
716 }
717
718 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
719 {
720 int i;
721 svm_region_t *svm;
722 void *oldheap;
723
724 /* Allow the application to clean up its registrations */
725 for (i = 0; i < vec_len (dead_indices); i++)
726 {
727 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
728 if (regpp)
729 {
730 u32 handle;
731
732 handle = vl_msg_api_handle_from_index_and_epoch
733 (dead_indices[i], shm->application_restarts);
734 (void) call_reaper_functions (handle);
735 }
736 }
737
738 svm = am->vlib_rp;
739 pthread_mutex_lock (&svm->mutex);
740 oldheap = svm_push_data_heap (svm);
741
742 for (i = 0; i < vec_len (dead_indices); i++)
743 {
744 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
745 if (regpp)
746 {
747 /* Is this a pairwise SVM segment? */
748 if ((*regpp)->vlib_rp != svm)
749 {
750 int i;
751 svm_region_t *dead_rp = (*regpp)->vlib_rp;
752 /* Note: account for the memfd header page */
753 u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
754 u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
755
756 /* For horizontal scaling, add a hash table... */
757 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
758 if (am->vlib_private_rps[i] == dead_rp)
759 {
760 vec_delete (am->vlib_private_rps, 1, i);
761 goto found;
762 }
763 clib_warning ("private rp %llx AWOL", dead_rp);
764
765 found:
766 /* Kill it, accounting for the memfd header page */
767 if (munmap ((void *) virtual_base, virtual_size) < 0)
768 clib_unix_warning ("munmap");
769 /* Reset the queue-length-address cache */
770 vec_reset_length (vl_api_queue_cursizes);
771 }
772 else
773 {
774 /* Poison the old registration */
775 memset (*regpp, 0xF3, sizeof (**regpp));
776 clib_mem_free (*regpp);
777 }
778 /* no dangling references, please */
779 *regpp = 0;
780 }
781 else
782 {
783 svm_pop_heap (oldheap);
784 clib_warning ("Duplicate free, client index %d",
785 regpp - am->vl_clients);
786 oldheap = svm_push_data_heap (svm);
787 }
788 }
789
790 svm_client_scan_this_region_nolock (am->vlib_rp);
791
792 pthread_mutex_unlock (&svm->mutex);
793 svm_pop_heap (oldheap);
794 for (i = 0; i < vec_len (dead_indices); i++)
795 pool_put_index (am->vl_clients, dead_indices[i]);
796 }
797}
798
799
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800static uword
801memclnt_process (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400802 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803{
Dave Barach371e4e12016-07-08 09:38:52 -0400804 uword mp;
805 vl_shmem_hdr_t *shm;
806 unix_shared_memory_queue_t *q;
807 clib_error_t *e;
808 int rv;
809 api_main_t *am = &api_main;
810 f64 dead_client_scan_time;
811 f64 sleep_time, start_time;
812 f64 vector_rate;
Dave Barach59b25652017-09-10 15:04:27 -0400813 clib_error_t *socksvr_api_init (vlib_main_t * vm);
814 clib_error_t *error;
Dave Barach987e11d2017-02-27 13:10:27 -0500815 int i;
Dave Barach59b25652017-09-10 15:04:27 -0400816 vl_socket_args_for_process_t *a;
817 uword event_type;
818 uword *event_data = 0;
819 int private_segment_rotor = 0;
820 svm_region_t *vlib_rp;
821 f64 now;
Dave Barach371e4e12016-07-08 09:38:52 -0400822
823 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
824
825 if ((rv = memory_api_init (am->region_name)) < 0)
826 {
Dave Barach59b25652017-09-10 15:04:27 -0400827 clib_warning ("memory_api_init returned %d, quitting...", rv);
828 return 0;
829 }
830
831 if ((error = socksvr_api_init (vm)))
832 {
833 clib_error_report (error);
834 clib_warning ("socksvr_api_init failed, quitting...");
835 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836 }
837
Dave Barach371e4e12016-07-08 09:38:52 -0400838 shm = am->shmem_hdr;
839 ASSERT (shm);
840 q = shm->vl_input_queue;
841 ASSERT (q);
Dave Barach7939f902017-10-04 10:03:52 -0400842 /* Make a note so we can always find the primary region easily */
843 am->vlib_primary_rp = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844
Dave Barach371e4e12016-07-08 09:38:52 -0400845 e = vlib_call_init_exit_functions
846 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
847 if (e)
848 clib_error_report (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Dave Barach59b25652017-09-10 15:04:27 -0400850 sleep_time = 10.0;
851 dead_client_scan_time = vlib_time_now (vm) + 10.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Dave Barach987e11d2017-02-27 13:10:27 -0500853 /*
854 * Send plugin message range messages for each plugin we loaded
855 */
856 for (i = 0; i < vec_len (am->msg_ranges); i++)
857 {
858 vl_api_msg_range_t *rp = am->msg_ranges + i;
859 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
860 rp->last_msg_id);
861 }
862
Dave Barach49fe0462017-09-12 17:06:56 -0400863 /*
Dave Barach49fe0462017-09-12 17:06:56 -0400864 * Save the api message table snapshot, if configured
865 */
866 if (am->save_msg_table_filename)
867 {
868 int fd, rv;
869 u8 *chroot_file;
Dave Barach59b25652017-09-10 15:04:27 -0400870 u8 *serialized_message_table;
871
872 /*
873 * Snapshoot the api message table.
874 */
Dave Barach49fe0462017-09-12 17:06:56 -0400875 if (strstr ((char *) am->save_msg_table_filename, "..")
876 || index ((char *) am->save_msg_table_filename, '/'))
877 {
878 clib_warning ("illegal save-message-table filename '%s'",
879 am->save_msg_table_filename);
880 goto skip_save;
881 }
882
883 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
884
885 fd = creat ((char *) chroot_file, 0644);
886
887 if (fd < 0)
888 {
889 clib_unix_warning ("creat");
890 goto skip_save;
891 }
Dave Barach59b25652017-09-10 15:04:27 -0400892
893 serialized_message_table = vl_api_serialize_message_table (am, 0);
894
Dave Barach49fe0462017-09-12 17:06:56 -0400895 rv = write (fd, serialized_message_table,
896 vec_len (serialized_message_table));
897
898 if (rv != vec_len (serialized_message_table))
899 clib_unix_warning ("write");
900
901 rv = close (fd);
902 if (rv < 0)
903 clib_unix_warning ("close");
904
905 vec_free (chroot_file);
Dave Barach59b25652017-09-10 15:04:27 -0400906 vec_free (serialized_message_table);
Dave Barach49fe0462017-09-12 17:06:56 -0400907 }
908
909skip_save:
Dave Barach49fe0462017-09-12 17:06:56 -0400910
Dave Barach371e4e12016-07-08 09:38:52 -0400911 /* $$$ pay attention to frame size, control CPU usage */
912 while (1)
913 {
Dave Barach371e4e12016-07-08 09:38:52 -0400914 i8 *headp;
915 int need_broadcast;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916
Dave Barach371e4e12016-07-08 09:38:52 -0400917 /*
918 * There's a reason for checking the queue before
919 * sleeping. If the vlib application crashes, it's entirely
920 * possible for a client to enqueue a connect request
921 * during the process restart interval.
922 *
923 * Unless some force of physics causes the new incarnation
924 * of the application to process the request, the client will
925 * sit and wait for Godot...
926 */
927 vector_rate = vlib_last_vector_length_per_node (vm);
928 start_time = vlib_time_now (vm);
929 while (1)
930 {
931 pthread_mutex_lock (&q->mutex);
932 if (q->cursize == 0)
933 {
934 vm->api_queue_nonempty = 0;
935 pthread_mutex_unlock (&q->mutex);
936
937 if (TRACE_VLIB_MEMORY_QUEUE)
938 {
939 /* *INDENT-OFF* */
940 ELOG_TYPE_DECLARE (e) =
941 {
942 .format = "q-underflow: len %d",
943 .format_args = "i4",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944 };
Dave Barach371e4e12016-07-08 09:38:52 -0400945 /* *INDENT-ON* */
946 struct
947 {
948 u32 len;
949 } *ed;
950 ed = ELOG_DATA (&vm->elog_main, e);
951 ed->len = 0;
952 }
953 sleep_time = 20.0;
954 break;
955 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956
Dave Barach371e4e12016-07-08 09:38:52 -0400957 headp = (i8 *) (q->data + sizeof (uword) * q->head);
958 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959
Dave Barach371e4e12016-07-08 09:38:52 -0400960 q->head++;
961 need_broadcast = (q->cursize == q->maxsize / 2);
962 q->cursize--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963
Dave Barach371e4e12016-07-08 09:38:52 -0400964 if (PREDICT_FALSE (q->head == q->maxsize))
965 q->head = 0;
966 pthread_mutex_unlock (&q->mutex);
967 if (need_broadcast)
968 (void) pthread_cond_broadcast (&q->condvar);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969
Dave Barach371e4e12016-07-08 09:38:52 -0400970 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Dave Barach371e4e12016-07-08 09:38:52 -0400972 /* Allow no more than 10us without a pause */
973 if (vlib_time_now (vm) > start_time + 10e-6)
974 {
975 int index = SLEEP_400_US;
976 if (vector_rate > 40.0)
977 sleep_time = 400e-6;
978 else if (vector_rate > 20.0)
979 {
980 index = SLEEP_200_US;
981 sleep_time = 200e-6;
982 }
983 else if (vector_rate >= 1.0)
984 {
985 index = SLEEP_100_US;
986 sleep_time = 100e-6;
987 }
988 else
989 {
990 index = SLEEP_10_US;
991 sleep_time = 10e-6;
992 }
993 vector_rate_histogram[index] += 1;
994 break;
995 }
996 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997
Dave Barach59b25652017-09-10 15:04:27 -0400998 /*
999 * see if we have any private api shared-memory segments
1000 * If so, push required context variables, and process
1001 * a message.
1002 */
1003 if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
Dave Barach371e4e12016-07-08 09:38:52 -04001004 {
Dave Barach59b25652017-09-10 15:04:27 -04001005 unix_shared_memory_queue_t *save_vlib_input_queue = q;
1006 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
1007 svm_region_t *save_vlib_rp = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008
Dave Barach59b25652017-09-10 15:04:27 -04001009 vlib_rp = am->vlib_rp = am->vlib_private_rps[private_segment_rotor];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010
Dave Barach59b25652017-09-10 15:04:27 -04001011 am->shmem_hdr = (void *) vlib_rp->user_ctx;
1012 q = am->shmem_hdr->vl_input_queue;
1013
1014 pthread_mutex_lock (&q->mutex);
1015 if (q->cursize > 0)
Dave Barach371e4e12016-07-08 09:38:52 -04001016 {
Dave Barach59b25652017-09-10 15:04:27 -04001017 headp = (i8 *) (q->data + sizeof (uword) * q->head);
1018 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019
Dave Barach59b25652017-09-10 15:04:27 -04001020 q->head++;
1021 need_broadcast = (q->cursize == q->maxsize / 2);
1022 q->cursize--;
1023
1024 if (PREDICT_FALSE (q->head == q->maxsize))
1025 q->head = 0;
1026 pthread_mutex_unlock (&q->mutex);
1027 if (need_broadcast)
1028 (void) pthread_cond_broadcast (&q->condvar);
1029
1030 pthread_mutex_unlock (&q->mutex);
1031
1032 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
1033 }
1034 else
1035 pthread_mutex_unlock (&q->mutex);
1036
1037 q = save_vlib_input_queue;
1038 am->shmem_hdr = save_shmem_hdr;
1039 am->vlib_rp = save_vlib_rp;
1040
1041 private_segment_rotor++;
1042 if (private_segment_rotor >= vec_len (am->vlib_private_rps))
1043 private_segment_rotor = 0;
1044 }
1045
1046 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1047 vec_reset_length (event_data);
1048 event_type = vlib_process_get_events (vm, &event_data);
1049 now = vlib_time_now (vm);
1050
1051 switch (event_type)
1052 {
1053 case QUEUE_SIGNAL_EVENT:
1054 vm->queue_signal_pending = 0;
1055 break;
1056
1057 case SOCKET_READ_EVENT:
1058 for (i = 0; i < vec_len (event_data); i++)
Dave Barach371e4e12016-07-08 09:38:52 -04001059 {
Dave Barach59b25652017-09-10 15:04:27 -04001060 a = pool_elt_at_index (socket_main.process_args, event_data[i]);
1061 vl_api_socket_process_msg (a->clib_file, a->regp,
1062 (i8 *) a->data);
1063 vec_free (a->data);
1064 pool_put (socket_main.process_args, a);
Dave Barach371e4e12016-07-08 09:38:52 -04001065 }
Dave Barach59b25652017-09-10 15:04:27 -04001066 break;
Dave Barach371e4e12016-07-08 09:38:52 -04001067
Dave Barach59b25652017-09-10 15:04:27 -04001068 /* Timeout... */
1069 case -1:
1070 break;
1071
1072 default:
1073 clib_warning ("unknown event type %d", event_type);
1074 break;
1075 }
1076
1077 if (now > dead_client_scan_time)
1078 {
1079 dead_client_scan (am, shm, now);
1080 dead_client_scan_time = vlib_time_now (vm) + 10.0;
Dave Barach371e4e12016-07-08 09:38:52 -04001081 }
1082
1083 if (TRACE_VLIB_MEMORY_QUEUE)
1084 {
1085 /* *INDENT-OFF* */
1086 ELOG_TYPE_DECLARE (e) = {
1087 .format = "q-awake: len %d",
1088 .format_args = "i4",
1089 };
1090 /* *INDENT-ON* */
1091 struct
1092 {
1093 u32 len;
1094 } *ed;
1095 ed = ELOG_DATA (&vm->elog_main, e);
1096 ed->len = q->cursize;
1097 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 }
1099
Dave Barach371e4e12016-07-08 09:38:52 -04001100 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101}
Dave Barach49fe0462017-09-12 17:06:56 -04001102/* *INDENT-OFF* */
Dave Barach59b25652017-09-10 15:04:27 -04001103VLIB_REGISTER_NODE (memclnt_node) =
1104{
1105 .function = memclnt_process,
1106 .type = VLIB_NODE_TYPE_PROCESS,
1107 .name = "api-rx-from-ring",
1108 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach49fe0462017-09-12 17:06:56 -04001109};
1110/* *INDENT-ON* */
1111
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112
1113static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001114vl_api_show_histogram_command (vlib_main_t * vm,
1115 unformat_input_t * input,
1116 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117{
Dave Barach371e4e12016-07-08 09:38:52 -04001118 u64 total_counts = 0;
1119 int i;
1120
1121 for (i = 0; i < SLEEP_N_BUCKETS; i++)
1122 {
1123 total_counts += vector_rate_histogram[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124 }
1125
Dave Barach371e4e12016-07-08 09:38:52 -04001126 if (total_counts == 0)
1127 {
1128 vlib_cli_output (vm, "No control-plane activity.");
1129 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130 }
1131
1132#define _(n) \
1133 do { \
1134 f64 percent; \
1135 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
1136 / (f64) total_counts; \
1137 percent *= 100.0; \
1138 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
1139 vector_rate_histogram[SLEEP_##n##_US], \
1140 percent); \
1141 } while (0);
Dave Barach371e4e12016-07-08 09:38:52 -04001142 foreach_histogram_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143#undef _
1144
Dave Barach371e4e12016-07-08 09:38:52 -04001145 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146}
1147
Dave Barach49fe0462017-09-12 17:06:56 -04001148/*?
1149 * Display the binary api sleep-time histogram
1150?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001151/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001152VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) =
1153{
1154 .path = "show api histogram",
1155 .short_help = "show api histogram",
1156 .function = vl_api_show_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157};
Dave Barach371e4e12016-07-08 09:38:52 -04001158/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159
1160static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001161vl_api_clear_histogram_command (vlib_main_t * vm,
1162 unformat_input_t * input,
1163 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001164{
Dave Barach371e4e12016-07-08 09:38:52 -04001165 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166
Dave Barach371e4e12016-07-08 09:38:52 -04001167 for (i = 0; i < SLEEP_N_BUCKETS; i++)
1168 vector_rate_histogram[i] = 0;
1169 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170}
1171
Dave Barach49fe0462017-09-12 17:06:56 -04001172/*?
1173 * Clear the binary api sleep-time histogram
1174?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001175/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001176VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) =
1177{
1178 .path = "clear api histogram",
1179 .short_help = "clear api histogram",
1180 .function = vl_api_clear_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181};
Dave Barach371e4e12016-07-08 09:38:52 -04001182/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183
Dave Barach59b25652017-09-10 15:04:27 -04001184volatile int **vl_api_queue_cursizes;
1185
Dave Barach371e4e12016-07-08 09:38:52 -04001186static void
1187memclnt_queue_callback (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188{
Dave Barach59b25652017-09-10 15:04:27 -04001189 int i;
1190 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
Dave Barach59b25652017-09-10 15:04:27 -04001192 if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
1193 1 + vec_len (am->vlib_private_rps)))
Dave Barach16c75df2016-05-31 14:05:46 -04001194 {
Dave Barach16c75df2016-05-31 14:05:46 -04001195 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barach371e4e12016-07-08 09:38:52 -04001196 unix_shared_memory_queue_t *q;
1197
Dave Barach16c75df2016-05-31 14:05:46 -04001198 if (shmem_hdr == 0)
Dave Barach371e4e12016-07-08 09:38:52 -04001199 return;
1200
Dave Barach16c75df2016-05-31 14:05:46 -04001201 q = shmem_hdr->vl_input_queue;
1202 if (q == 0)
Dave Barach371e4e12016-07-08 09:38:52 -04001203 return;
Dave Barach59b25652017-09-10 15:04:27 -04001204
1205 vec_add1 (vl_api_queue_cursizes, &q->cursize);
1206
1207 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
1208 {
1209 svm_region_t *vlib_rp = am->vlib_private_rps[i];
1210
1211 shmem_hdr = (void *) vlib_rp->user_ctx;
1212 q = shmem_hdr->vl_input_queue;
1213 vec_add1 (vl_api_queue_cursizes, &q->cursize);
1214 }
Dave Barach16c75df2016-05-31 14:05:46 -04001215 }
Dave Barach371e4e12016-07-08 09:38:52 -04001216
Dave Barach59b25652017-09-10 15:04:27 -04001217 for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
Dave Barach16c75df2016-05-31 14:05:46 -04001218 {
Dave Barach59b25652017-09-10 15:04:27 -04001219 if (*vl_api_queue_cursizes[i])
1220 {
1221 vm->queue_signal_pending = 1;
1222 vm->api_queue_nonempty = 1;
1223 vlib_process_signal_event (vm, memclnt_node.index,
1224 /* event_type */ QUEUE_SIGNAL_EVENT,
1225 /* event_data */ 0);
1226 break;
1227 }
Dave Barach16c75df2016-05-31 14:05:46 -04001228 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229}
1230
Dave Barach371e4e12016-07-08 09:38:52 -04001231void
1232vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233{
Dave Barach371e4e12016-07-08 09:38:52 -04001234 vlib_node_set_state (vm, memclnt_node.index,
1235 (enable
1236 ? VLIB_NODE_STATE_POLLING
1237 : VLIB_NODE_STATE_DISABLED));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238}
1239
1240static uword
1241api_rx_from_node (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -04001242 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243{
Dave Barach371e4e12016-07-08 09:38:52 -04001244 uword n_packets = frame->n_vectors;
1245 uword n_left_from;
1246 u32 *from;
1247 static u8 *long_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
Dave Barach371e4e12016-07-08 09:38:52 -04001249 vec_validate (long_msg, 4095);
1250 n_left_from = frame->n_vectors;
1251 from = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
Dave Barach371e4e12016-07-08 09:38:52 -04001253 while (n_left_from > 0)
1254 {
1255 u32 bi0;
1256 vlib_buffer_t *b0;
1257 void *msg;
1258 uword msg_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259
Dave Barach371e4e12016-07-08 09:38:52 -04001260 bi0 = from[0];
1261 b0 = vlib_get_buffer (vm, bi0);
1262 from += 1;
1263 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264
Dave Barach371e4e12016-07-08 09:38:52 -04001265 msg = b0->data + b0->current_data;
1266 msg_len = b0->current_length;
1267 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
1268 {
1269 ASSERT (long_msg != 0);
1270 _vec_len (long_msg) = 0;
1271 vec_add (long_msg, msg, msg_len);
1272 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
1273 {
1274 b0 = vlib_get_buffer (vm, b0->next_buffer);
1275 msg = b0->data + b0->current_data;
1276 msg_len = b0->current_length;
1277 vec_add (long_msg, msg, msg_len);
1278 }
1279 msg = long_msg;
1280 }
1281 vl_msg_api_handler_no_trace_no_free (msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282 }
1283
Dave Barach371e4e12016-07-08 09:38:52 -04001284 /* Free what we've been given. */
1285 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286
Dave Barach371e4e12016-07-08 09:38:52 -04001287 return n_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001288}
1289
Dave Barach371e4e12016-07-08 09:38:52 -04001290/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001291VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
1292 .function = api_rx_from_node,
1293 .type = VLIB_NODE_TYPE_INTERNAL,
1294 .vector_size = 4,
1295 .name = "api-rx-from-node",
1296};
Dave Barach371e4e12016-07-08 09:38:52 -04001297/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298
1299static clib_error_t *
1300setup_memclnt_exit (vlib_main_t * vm)
1301{
Dave Barach371e4e12016-07-08 09:38:52 -04001302 atexit (vl_unmap_shmem);
1303 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304}
1305
1306VLIB_INIT_FUNCTION (setup_memclnt_exit);
1307
Dave Barach59b25652017-09-10 15:04:27 -04001308u8 *
1309format_api_message_rings (u8 * s, va_list * args)
1310{
1311 api_main_t *am = va_arg (*args, api_main_t *);
1312 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
1313 int main_segment = va_arg (*args, int);
1314 ring_alloc_t *ap;
1315 int i;
1316
1317 if (shmem_hdr == 0)
1318 return format (s, "%8s %8s %8s %8s %8s\n",
1319 "Owner", "Size", "Nitems", "Hits", "Misses");
1320
1321 ap = shmem_hdr->vl_rings;
1322
1323 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
1324 {
1325 s = format (s, "%8s %8d %8d %8d %8d\n",
1326 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
1327 ap++;
1328 }
1329
1330 ap = shmem_hdr->client_rings;
1331
1332 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
1333 {
1334 s = format (s, "%8s %8d %8d %8d %8d\n",
1335 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
1336 ap++;
1337 }
1338
1339 if (main_segment)
1340 {
1341 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
1342 s = format
1343 (s,
1344 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
1345 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
1346 shmem_hdr->garbage_collects);
1347 }
1348 return s;
1349}
1350
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351
1352static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001353vl_api_ring_command (vlib_main_t * vm,
1354 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355{
Dave Barach371e4e12016-07-08 09:38:52 -04001356 int i;
Dave Barach371e4e12016-07-08 09:38:52 -04001357 vl_shmem_hdr_t *shmem_hdr;
1358 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359
Dave Barach7939f902017-10-04 10:03:52 -04001360 /* First, dump the primary region rings.. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361
Dave Barach7939f902017-10-04 10:03:52 -04001362 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
Dave Barach371e4e12016-07-08 09:38:52 -04001363 {
1364 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
1365 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366 }
1367
Dave Barach7939f902017-10-04 10:03:52 -04001368 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
1369
Dave Barach59b25652017-09-10 15:04:27 -04001370 vlib_cli_output (vm, "Main API segment rings:");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371
Dave Barach59b25652017-09-10 15:04:27 -04001372 vlib_cli_output (vm, "%U", format_api_message_rings, am,
1373 0 /* print header */ , 0 /* notused */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374
Dave Barach59b25652017-09-10 15:04:27 -04001375 vlib_cli_output (vm, "%U", format_api_message_rings, am,
1376 shmem_hdr, 1 /* main segment */ );
1377
1378 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
Dave Barach371e4e12016-07-08 09:38:52 -04001379 {
Dave Barach59b25652017-09-10 15:04:27 -04001380 svm_region_t *vlib_rp = am->vlib_private_rps[i];
1381 shmem_hdr = (void *) vlib_rp->user_ctx;
1382 vl_api_registration_t **regpp;
Dave Barach7939f902017-10-04 10:03:52 -04001383 vl_api_registration_t *regp = 0;
Dave Barach59b25652017-09-10 15:04:27 -04001384
1385 /* For horizontal scaling, add a hash table... */
1386 /* *INDENT-OFF* */
1387 pool_foreach (regpp, am->vl_clients,
1388 ({
1389 regp = *regpp;
1390 if (regp && regp->vlib_rp == vlib_rp)
1391 {
1392 vlib_cli_output (vm, "%s segment rings:", regp->name);
1393 goto found;
1394 }
1395 }));
Dave Barach7939f902017-10-04 10:03:52 -04001396 vlib_cli_output (vm, "regp %llx not found?", regp);
1397 continue;
Dave Barach59b25652017-09-10 15:04:27 -04001398 /* *INDENT-ON* */
1399 found:
1400 vlib_cli_output (vm, "%U", format_api_message_rings, am,
Dave Barach7939f902017-10-04 10:03:52 -04001401 0 /* print header */ , 0 /* notused */ );
1402 vlib_cli_output (vm, "%U", format_api_message_rings, am,
Dave Barach59b25652017-09-10 15:04:27 -04001403 shmem_hdr, 0 /* main segment */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001404 }
1405
Dave Barach371e4e12016-07-08 09:38:52 -04001406 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001407}
1408
Dave Barach371e4e12016-07-08 09:38:52 -04001409void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
1410 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001411
Dave Barach371e4e12016-07-08 09:38:52 -04001412void
1413dump_socket_clients (vlib_main_t * vm, api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001414{
1415}
1416
1417static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001418vl_api_client_command (vlib_main_t * vm,
1419 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001420{
Dave Barach371e4e12016-07-08 09:38:52 -04001421 vl_api_registration_t **regpp, *regp;
1422 unix_shared_memory_queue_t *q;
1423 char *health;
1424 api_main_t *am = &api_main;
1425 u32 *confused_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001426
Dave Barach371e4e12016-07-08 09:38:52 -04001427 if (!pool_elts (am->vl_clients))
1428 goto socket_clients;
1429 vlib_cli_output (vm, "Shared memory clients");
1430 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
1431 "Name", "PID", "Queue Length", "Queue VA", "Health");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
Dave Barach371e4e12016-07-08 09:38:52 -04001433 /* *INDENT-OFF* */
1434 pool_foreach (regpp, am->vl_clients,
1435 ({
1436 regp = *regpp;
1437
1438 if (regp)
1439 {
Dave Barach59b25652017-09-10 15:04:27 -04001440 if (regp->unanswered_pings > 0)
1441 health = "questionable";
Dave Barach371e4e12016-07-08 09:38:52 -04001442 else
Dave Barach59b25652017-09-10 15:04:27 -04001443 health = "OK";
1444
1445 q = regp->vl_input_queue;
1446
Dave Barach371e4e12016-07-08 09:38:52 -04001447 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
1448 regp->name, q->consumer_pid, q->cursize,
1449 q, health);
1450 }
1451 else
1452 {
1453 clib_warning ("NULL client registration index %d",
1454 regpp - am->vl_clients);
1455 vec_add1 (confused_indices, regpp - am->vl_clients);
1456 }
1457 }));
1458 /* *INDENT-ON* */
1459
1460 /* This should "never happen," but if it does, fix it... */
1461 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
1462 {
1463 int i;
1464 for (i = 0; i < vec_len (confused_indices); i++)
1465 {
1466 pool_put_index (am->vl_clients, confused_indices[i]);
1467 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468 }
Dave Barach371e4e12016-07-08 09:38:52 -04001469 vec_free (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001470
Dave Barach371e4e12016-07-08 09:38:52 -04001471 if (am->missing_clients)
1472 vlib_cli_output (vm, "%u messages with missing clients",
1473 am->missing_clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001474socket_clients:
Dave Barach371e4e12016-07-08 09:38:52 -04001475 dump_socket_clients (vm, am);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476
Dave Barach371e4e12016-07-08 09:38:52 -04001477 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001478}
1479
Calvin16649372016-07-28 13:52:05 -04001480static clib_error_t *
1481vl_api_status_command (vlib_main_t * vm,
1482 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1483{
1484 api_main_t *am = &api_main;
1485
1486 // check if rx_trace and tx_trace are not null pointers
1487
1488 if (am->rx_trace == 0)
1489 {
1490 vlib_cli_output (vm, "RX Trace disabled\n");
1491 }
1492 else
1493 {
1494 if (am->rx_trace->enabled == 0)
1495 vlib_cli_output (vm, "RX Trace disabled\n");
1496 else
1497 vlib_cli_output (vm, "RX Trace enabled\n");
1498 }
1499
1500 if (am->tx_trace == 0)
1501 {
1502 vlib_cli_output (vm, "TX Trace disabled\n");
1503 }
1504 else
1505 {
1506 if (am->tx_trace->enabled == 0)
1507 vlib_cli_output (vm, "TX Trace disabled\n");
1508 else
1509 vlib_cli_output (vm, "TX Trace enabled\n");
1510 }
1511
1512 return 0;
1513}
1514
Dave Barach371e4e12016-07-08 09:38:52 -04001515/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001516VLIB_CLI_COMMAND (cli_show_api_command, static) =
1517{
1518 .path = "show api",
1519 .short_help = "Show API information",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520};
Dave Barach371e4e12016-07-08 09:38:52 -04001521/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522
Dave Barach49fe0462017-09-12 17:06:56 -04001523/*?
1524 * Display binary api message allocation ring statistics
1525?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001526/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001527VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
1528{
1529 .path = "show api ring-stats",
1530 .short_help = "Message ring statistics",
1531 .function = vl_api_ring_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532};
Dave Barach371e4e12016-07-08 09:38:52 -04001533/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001534
Dave Barach49fe0462017-09-12 17:06:56 -04001535/*?
1536 * Display current api client connections
1537?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001538/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001539VLIB_CLI_COMMAND (cli_show_api_clients_command, static) =
1540{
1541 .path = "show api clients",
1542 .short_help = "Client information",
1543 .function = vl_api_client_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544};
Dave Barach371e4e12016-07-08 09:38:52 -04001545/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546
Dave Barach49fe0462017-09-12 17:06:56 -04001547/*?
1548 * Display the current api message tracing status
1549?*/
Calvin16649372016-07-28 13:52:05 -04001550/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001551VLIB_CLI_COMMAND (cli_show_api_status_command, static) =
1552{
1553 .path = "show api trace-status",
1554 .short_help = "Display API trace status",
1555 .function = vl_api_status_command,
Calvin16649372016-07-28 13:52:05 -04001556};
1557/* *INDENT-ON* */
1558
Ed Warnickecb9cada2015-12-08 15:45:58 -07001559static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001560vl_api_message_table_command (vlib_main_t * vm,
1561 unformat_input_t * input,
1562 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563{
Dave Barach371e4e12016-07-08 09:38:52 -04001564 api_main_t *am = &api_main;
1565 int i;
1566 int verbose = 0;
1567
1568 if (unformat (input, "verbose"))
1569 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001570
1571
Dave Barach371e4e12016-07-08 09:38:52 -04001572 if (verbose == 0)
1573 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1574 else
1575 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1576 "MP-safe");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577
Dave Barach371e4e12016-07-08 09:38:52 -04001578 for (i = 1; i < vec_len (am->msg_names); i++)
1579 {
1580 if (verbose == 0)
1581 {
1582 vlib_cli_output (vm, "%-4d %s", i,
1583 am->msg_names[i] ? am->msg_names[i] :
1584 " [no handler]");
1585 }
1586 else
1587 {
1588 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1589 am->msg_names[i] ? am->msg_names[i] :
1590 " [no handler]", am->message_bounce[i],
1591 am->is_mp_safe[i]);
1592 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001593 }
1594
Dave Barach371e4e12016-07-08 09:38:52 -04001595 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596}
1597
Dave Barach49fe0462017-09-12 17:06:56 -04001598/*?
1599 * Display the current api message decode tables
1600?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001601/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001602VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) =
1603{
1604 .path = "show api message-table",
1605 .short_help = "Message Table",
1606 .function = vl_api_message_table_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001607};
Dave Barach371e4e12016-07-08 09:38:52 -04001608/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001609
Ed Warnickecb9cada2015-12-08 15:45:58 -07001610static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001611vl_api_trace_command (vlib_main_t * vm,
1612 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613{
Dave Barach371e4e12016-07-08 09:38:52 -04001614 u32 nitems = 1024;
1615 vl_api_trace_which_t which = VL_API_TRACE_RX;
Dave Barach371e4e12016-07-08 09:38:52 -04001616 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617
Dave Barach371e4e12016-07-08 09:38:52 -04001618 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1619 {
1620 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1621 goto configure;
1622 else if (unformat (input, "tx nitems %u", &nitems)
1623 || unformat (input, "tx"))
1624 {
1625 which = VL_API_TRACE_RX;
1626 goto configure;
1627 }
1628 else if (unformat (input, "on rx"))
1629 {
1630 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1631 }
1632 else if (unformat (input, "on tx"))
1633 {
1634 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1635 }
1636 else if (unformat (input, "on"))
1637 {
1638 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1639 }
1640 else if (unformat (input, "off"))
1641 {
1642 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1643 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1644 }
1645 else if (unformat (input, "free"))
1646 {
1647 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1648 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1649 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1650 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1651 }
Dave Barach371e4e12016-07-08 09:38:52 -04001652 else if (unformat (input, "debug on"))
1653 {
1654 am->msg_print_flag = 1;
1655 }
1656 else if (unformat (input, "debug off"))
1657 {
1658 am->msg_print_flag = 0;
1659 }
1660 else
1661 return clib_error_return (0, "unknown input `%U'",
1662 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001663 }
Dave Barach371e4e12016-07-08 09:38:52 -04001664 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001665
Dave Barach371e4e12016-07-08 09:38:52 -04001666configure:
1667 if (vl_msg_api_trace_configure (am, which, nitems))
1668 {
1669 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1670 which, nitems);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001671 }
1672
Dave Barach371e4e12016-07-08 09:38:52 -04001673 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001674}
1675
Dave Barach49fe0462017-09-12 17:06:56 -04001676/*?
1677 * Control the binary API trace mechanism
1678?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001679/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001680VLIB_CLI_COMMAND (trace, static) =
1681{
1682 .path = "set api-trace [on][on tx][on rx][off][free][debug on][debug off]",
1683 .short_help = "API trace",
1684 .function = vl_api_trace_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001685};
Dave Barach371e4e12016-07-08 09:38:52 -04001686/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687
1688clib_error_t *
1689vlibmemory_init (vlib_main_t * vm)
1690{
Dave Barach309bef22016-01-22 16:09:52 -05001691 api_main_t *am = &api_main;
Dave Barachb3d93da2016-08-03 14:34:38 -04001692 svm_map_region_args_t _a, *a = &_a;
Dave Barach59b25652017-09-10 15:04:27 -04001693 clib_error_t *error;
Dave Barachc3799992016-08-15 11:12:27 -04001694
Dave Barachb3d93da2016-08-03 14:34:38 -04001695 memset (a, 0, sizeof (*a));
1696 a->root_path = am->root_path;
1697 a->name = SVM_GLOBAL_REGION_NAME;
Dave Barachc3799992016-08-15 11:12:27 -04001698 a->baseva = (am->global_baseva != 0) ?
Dave Barachb3d93da2016-08-03 14:34:38 -04001699 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1700 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1701 a->flags = SVM_FLAGS_NODATA;
1702 a->uid = am->api_uid;
1703 a->gid = am->api_gid;
Dave Barachc3799992016-08-15 11:12:27 -04001704 a->pvt_heap_size =
1705 (am->global_pvt_heap_size !=
1706 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
Dave Barachb3d93da2016-08-03 14:34:38 -04001707
1708 svm_region_init_args (a);
Dave Barach59b25652017-09-10 15:04:27 -04001709
1710 error = vlib_call_init_function (vm, vlibsocket_init);
1711
1712 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001713}
1714
1715VLIB_INIT_FUNCTION (vlibmemory_init);
1716
Dave Barach371e4e12016-07-08 09:38:52 -04001717void
Neale Rannse72be392017-04-26 13:59:20 -07001718vl_set_memory_region_name (const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001719{
Dave Barach371e4e12016-07-08 09:38:52 -04001720 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721
Dave Barach371e4e12016-07-08 09:38:52 -04001722 am->region_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723}
1724
Dave Barach371e4e12016-07-08 09:38:52 -04001725static int
1726range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001727{
Dave Barach371e4e12016-07-08 09:38:52 -04001728 int len0, len1, clen;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001729
Dave Barach371e4e12016-07-08 09:38:52 -04001730 len0 = vec_len (a0->name);
1731 len1 = vec_len (a1->name);
1732 clen = len0 < len1 ? len0 : len1;
1733 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001734}
1735
Dave Barach371e4e12016-07-08 09:38:52 -04001736static u8 *
1737format_api_msg_range (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001738{
Dave Barach371e4e12016-07-08 09:38:52 -04001739 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740
Dave Barach371e4e12016-07-08 09:38:52 -04001741 if (rp == 0)
Dave Barach49fe0462017-09-12 17:06:56 -04001742 s = format (s, "%-50s%9s%9s", "Name", "First-ID", "Last-ID");
Dave Barach371e4e12016-07-08 09:38:52 -04001743 else
Dave Barach49fe0462017-09-12 17:06:56 -04001744 s = format (s, "%-50s%9d%9d", rp->name, rp->first_msg_id,
Dave Barach371e4e12016-07-08 09:38:52 -04001745 rp->last_msg_id);
1746
1747 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001748}
1749
1750static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001751vl_api_show_plugin_command (vlib_main_t * vm,
1752 unformat_input_t * input,
1753 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001754{
Dave Barach371e4e12016-07-08 09:38:52 -04001755 api_main_t *am = &api_main;
1756 vl_api_msg_range_t *rp = 0;
1757 int i;
1758
1759 if (vec_len (am->msg_ranges) == 0)
1760 {
1761 vlib_cli_output (vm, "No plugin API message ranges configured...");
1762 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001764
Dave Barach371e4e12016-07-08 09:38:52 -04001765 rp = vec_dup (am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766
Dave Barach371e4e12016-07-08 09:38:52 -04001767 vec_sort_with_function (rp, range_compare);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768
Dave Barach371e4e12016-07-08 09:38:52 -04001769 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1770 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1771
1772 for (i = 0; i < vec_len (rp); i++)
1773 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1774
Dave Barachdfbee412017-03-02 18:24:10 -05001775 vec_free (rp);
1776
Dave Barach371e4e12016-07-08 09:38:52 -04001777 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001778}
1779
Dave Barach49fe0462017-09-12 17:06:56 -04001780/*?
1781 * Display the plugin binary API message range table
1782?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001783/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001784VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) =
1785{
1786 .path = "show api plugin",
1787 .short_help = "show api plugin",
1788 .function = vl_api_show_plugin_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001789};
Dave Barach371e4e12016-07-08 09:38:52 -04001790/* *INDENT-ON* */
Dave Barach4e281a42015-12-14 11:13:29 -05001791
Dave Barach371e4e12016-07-08 09:38:52 -04001792static void
1793vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001794{
Dave Barach11b8dbf2017-04-24 10:46:54 -04001795 vl_api_rpc_call_reply_t *rmp;
Dave Barach371e4e12016-07-08 09:38:52 -04001796 int (*fp) (void *);
Dave Barach4e281a42015-12-14 11:13:29 -05001797 i32 rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -04001798 vlib_main_t *vm = vlib_get_main ();
Dave Barach4e281a42015-12-14 11:13:29 -05001799
1800 if (mp->function == 0)
1801 {
1802 rv = -1;
1803 clib_warning ("rpc NULL function pointer");
1804 }
Dave Barach371e4e12016-07-08 09:38:52 -04001805
Dave Barach4e281a42015-12-14 11:13:29 -05001806 else
1807 {
1808 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001809 vlib_worker_thread_barrier_sync (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001810
Dave Barach371e4e12016-07-08 09:38:52 -04001811 fp = uword_to_pointer (mp->function, int (*)(void *));
1812 rv = fp (mp->data);
Dave Barach4e281a42015-12-14 11:13:29 -05001813
1814 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001815 vlib_worker_thread_barrier_release (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001816 }
1817
1818 if (mp->send_reply)
1819 {
Dave Barach371e4e12016-07-08 09:38:52 -04001820 unix_shared_memory_queue_t *q =
1821 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach4e281a42015-12-14 11:13:29 -05001822 if (q)
Dave Barach371e4e12016-07-08 09:38:52 -04001823 {
1824 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barach11b8dbf2017-04-24 10:46:54 -04001825 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
Dave Barach371e4e12016-07-08 09:38:52 -04001826 rmp->context = mp->context;
1827 rmp->retval = rv;
1828 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1829 }
Dave Barach4e281a42015-12-14 11:13:29 -05001830 }
1831 if (mp->multicast)
1832 {
1833 clib_warning ("multicast not yet implemented...");
1834 }
1835}
1836
Dave Barach371e4e12016-07-08 09:38:52 -04001837static void
Dave Barach11b8dbf2017-04-24 10:46:54 -04001838vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001839{
Dave Barach371e4e12016-07-08 09:38:52 -04001840 clib_warning ("unimplemented");
1841}
1842
John Lo7e9743a2017-09-23 08:59:58 -04001843always_inline void
1844vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
1845 u8 force_rpc)
Dave Barach371e4e12016-07-08 09:38:52 -04001846{
1847 vl_api_rpc_call_t *mp;
Dave Barach4e281a42015-12-14 11:13:29 -05001848 api_main_t *am = &api_main;
1849 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barache9183632016-10-04 16:53:56 -04001850 unix_shared_memory_queue_t *q;
Dave Barach4e281a42015-12-14 11:13:29 -05001851
Dave Barache9183632016-10-04 16:53:56 -04001852 /* Main thread: call the function directly */
John Lo7e9743a2017-09-23 08:59:58 -04001853 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
Dave Barache9183632016-10-04 16:53:56 -04001854 {
1855 vlib_main_t *vm = vlib_get_main ();
1856 void (*call_fp) (void *);
1857
1858 vlib_worker_thread_barrier_sync (vm);
1859
1860 call_fp = fp;
1861 call_fp (data);
1862
1863 vlib_worker_thread_barrier_release (vm);
1864 return;
1865 }
1866
1867 /* Any other thread, actually do an RPC call... */
Dave Barach4e281a42015-12-14 11:13:29 -05001868 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
Dave Barache9183632016-10-04 16:53:56 -04001869
Dave Barach4e281a42015-12-14 11:13:29 -05001870 memset (mp, 0, sizeof (*mp));
Damjan Marionf1213b82016-03-13 02:22:06 +01001871 clib_memcpy (mp->data, data, data_length);
Dave Barach4e281a42015-12-14 11:13:29 -05001872 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
Dave Barach371e4e12016-07-08 09:38:52 -04001873 mp->function = pointer_to_uword (fp);
Dave Barach4e281a42015-12-14 11:13:29 -05001874 mp->need_barrier_sync = 1;
Dave Barach371e4e12016-07-08 09:38:52 -04001875
Dave Barache9183632016-10-04 16:53:56 -04001876 /*
1877 * Use the "normal" control-plane mechanism for the main thread.
1878 * Well, almost. if the main input queue is full, we cannot
1879 * block. Otherwise, we can expect a barrier sync timeout.
1880 */
1881 q = shmem_hdr->vl_input_queue;
1882
1883 while (pthread_mutex_trylock (&q->mutex))
1884 vlib_worker_thread_barrier_check ();
1885
1886 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1887 {
1888 pthread_mutex_unlock (&q->mutex);
1889 vlib_worker_thread_barrier_check ();
1890 while (pthread_mutex_trylock (&q->mutex))
1891 vlib_worker_thread_barrier_check ();
1892 }
1893
1894 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1895
1896 pthread_mutex_unlock (&q->mutex);
Dave Barach4e281a42015-12-14 11:13:29 -05001897}
1898
John Lo7e9743a2017-09-23 08:59:58 -04001899/*
1900 * Check if called from worker threads.
1901 * If so, make rpc call of fp through shmem.
1902 * Otherwise, call fp directly
1903 */
1904void
1905vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1906{
1907 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1908 0);
1909}
1910
1911/*
1912 * Always make rpc call of fp through shmem, useful for calling from threads
1913 * not setup as worker threads, such as DPDK callback thread
1914 */
1915void
1916vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1917{
1918 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1919 1);
1920}
1921
Dave Barach987e11d2017-02-27 13:10:27 -05001922static void
1923vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1924{
Dave Barachdfbee412017-03-02 18:24:10 -05001925 api_main_t *am = &api_main;
1926 vl_api_msg_range_t *rp;
1927 uword *p;
Dave Barach987e11d2017-02-27 13:10:27 -05001928
Dave Barachdfbee412017-03-02 18:24:10 -05001929 /* Noop (except for tracing) during normal operation */
1930 if (am->replay_in_progress == 0)
1931 return;
1932
1933 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1934 if (p == 0)
1935 {
1936 clib_warning ("WARNING: traced plugin '%s' not in current image",
1937 mp->plugin_name);
1938 return;
1939 }
1940
1941 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1942 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1943 {
1944 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1945 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1946 rp->first_msg_id);
1947 }
1948
1949 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1950 {
1951 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1952 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1953 rp->last_msg_id);
1954 }
1955}
Dave Barach987e11d2017-02-27 13:10:27 -05001956
Dave Barach4e281a42015-12-14 11:13:29 -05001957#define foreach_rpc_api_msg \
1958_(RPC_CALL,rpc_call) \
Dave Barach11b8dbf2017-04-24 10:46:54 -04001959_(RPC_CALL_REPLY,rpc_call_reply)
Dave Barach4e281a42015-12-14 11:13:29 -05001960
Dave Barach987e11d2017-02-27 13:10:27 -05001961#define foreach_plugin_trace_msg \
1962_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1963
Dave Barach69128d02017-09-26 10:54:34 -04001964/*
1965 * Set the rpc callback at our earliest possible convenience.
1966 * This avoids ordering issues between thread_init() -> start_workers and
1967 * an init function which we could define here. If we ever intend to use
1968 * vlib all by itself, we can't create a link-time dependency on
1969 * an init function here and a typical "call foo_init first"
1970 * guitar lick.
1971 */
1972
1973extern void *rpc_call_main_thread_cb_fn;
1974
Dave Barach4e281a42015-12-14 11:13:29 -05001975static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001976rpc_api_hookup (vlib_main_t * vm)
Dave Barach4e281a42015-12-14 11:13:29 -05001977{
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001978 api_main_t *am = &api_main;
Dave Barach4e281a42015-12-14 11:13:29 -05001979#define _(N,n) \
1980 vl_msg_api_set_handlers(VL_API_##N, #n, \
1981 vl_api_##n##_t_handler, \
1982 vl_noop_handler, \
1983 vl_noop_handler, \
1984 vl_api_##n##_t_print, \
Dave Barach371e4e12016-07-08 09:38:52 -04001985 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1986 foreach_rpc_api_msg;
Dave Barach4e281a42015-12-14 11:13:29 -05001987#undef _
Dave Barach987e11d2017-02-27 13:10:27 -05001988
1989#define _(N,n) \
1990 vl_msg_api_set_handlers(VL_API_##N, #n, \
1991 vl_api_##n##_t_handler, \
1992 vl_noop_handler, \
1993 vl_noop_handler, \
1994 vl_api_##n##_t_print, \
1995 sizeof(vl_api_##n##_t), 1 /* do trace */);
1996 foreach_plugin_trace_msg;
1997#undef _
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001998
1999 /* No reason to halt the parade to create a trace record... */
2000 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
Dave Barach69128d02017-09-26 10:54:34 -04002001 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
Dave Barach371e4e12016-07-08 09:38:52 -04002002 return 0;
Dave Barach4e281a42015-12-14 11:13:29 -05002003}
2004
Dave Barach371e4e12016-07-08 09:38:52 -04002005VLIB_API_INIT_FUNCTION (rpc_api_hookup);
2006
Dave Barach80f54e22017-03-08 19:08:56 -05002007typedef enum
2008{
2009 DUMP,
2010 CUSTOM_DUMP,
2011 REPLAY,
2012 INITIALIZERS,
2013} vl_api_replay_t;
2014
2015u8 *
2016format_vl_msg_api_trace_status (u8 * s, va_list * args)
2017{
2018 api_main_t *am = va_arg (*args, api_main_t *);
2019 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
2020 vl_api_trace_t *tp;
2021 char *trace_name;
2022
2023 switch (which)
2024 {
2025 case VL_API_TRACE_TX:
2026 tp = am->tx_trace;
2027 trace_name = "TX trace";
2028 break;
2029
2030 case VL_API_TRACE_RX:
2031 tp = am->rx_trace;
2032 trace_name = "RX trace";
2033 break;
2034
2035 default:
2036 abort ();
2037 }
2038
2039 if (tp == 0)
2040 {
2041 s = format (s, "%s: not yet configured.\n", trace_name);
2042 return s;
2043 }
2044
2045 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
2046 trace_name, vec_len (tp->traces), tp->nitems,
2047 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
2048 return s;
2049}
2050
2051void vl_msg_api_custom_dump_configure (api_main_t * am)
2052 __attribute__ ((weak));
2053void
2054vl_msg_api_custom_dump_configure (api_main_t * am)
2055{
2056}
2057
2058static void
2059vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
2060 u32 first_index, u32 last_index,
2061 vl_api_replay_t which)
2062{
2063 vl_api_trace_file_header_t *hp;
2064 int i, fd;
2065 struct stat statb;
2066 size_t file_size;
2067 u8 *msg;
2068 u8 endian_swap_needed = 0;
2069 api_main_t *am = &api_main;
2070 u8 *tmpbuf = 0;
2071 u32 nitems;
2072 void **saved_print_handlers = 0;
2073
2074 fd = open ((char *) filename, O_RDONLY);
2075
2076 if (fd < 0)
2077 {
2078 vlib_cli_output (vm, "Couldn't open %s\n", filename);
2079 return;
2080 }
2081
2082 if (fstat (fd, &statb) < 0)
2083 {
2084 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
2085 close (fd);
2086 return;
2087 }
2088
2089 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
2090 {
2091 vlib_cli_output (vm, "File not plausible: %s\n", filename);
2092 close (fd);
2093 return;
2094 }
2095
2096 file_size = statb.st_size;
2097 file_size = (file_size + 4095) & ~(4096);
2098
2099 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
2100
2101 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
2102 {
2103 vlib_cli_output (vm, "mmap failed: %s\n", filename);
2104 close (fd);
2105 return;
2106 }
2107 close (fd);
2108
2109 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
2110 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
2111 endian_swap_needed = 1;
2112
2113 if (endian_swap_needed)
2114 nitems = ntohl (hp->nitems);
2115 else
2116 nitems = hp->nitems;
2117
2118 if (last_index == (u32) ~ 0)
2119 {
2120 last_index = nitems - 1;
2121 }
2122
2123 if (first_index >= nitems || last_index >= nitems)
2124 {
2125 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
2126 first_index, last_index, nitems - 1);
2127 munmap (hp, file_size);
2128 return;
2129 }
2130 if (hp->wrapped)
2131 vlib_cli_output (vm,
2132 "Note: wrapped/incomplete trace, results may vary\n");
2133
2134 if (which == CUSTOM_DUMP)
2135 {
2136 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
2137 vl_msg_api_custom_dump_configure (am);
2138 }
2139
2140
2141 msg = (u8 *) (hp + 1);
2142
2143 for (i = 0; i < first_index; i++)
2144 {
2145 trace_cfg_t *cfgp;
2146 int size;
2147 u16 msg_id;
2148
2149 size = clib_host_to_net_u32 (*(u32 *) msg);
2150 msg += sizeof (u32);
2151
2152 if (clib_arch_is_little_endian)
2153 msg_id = ntohs (*((u16 *) msg));
2154 else
2155 msg_id = *((u16 *) msg);
2156
2157 cfgp = am->api_trace_cfg + msg_id;
2158 if (!cfgp)
2159 {
2160 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
2161 munmap (hp, file_size);
2162 return;
2163 }
2164 msg += size;
2165 }
2166
2167 if (which == REPLAY)
2168 am->replay_in_progress = 1;
2169
2170 for (; i <= last_index; i++)
2171 {
2172 trace_cfg_t *cfgp;
2173 u16 *msg_idp;
2174 u16 msg_id;
2175 int size;
2176
2177 if (which == DUMP)
2178 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
2179
2180 size = clib_host_to_net_u32 (*(u32 *) msg);
2181 msg += sizeof (u32);
2182
2183 if (clib_arch_is_little_endian)
2184 msg_id = ntohs (*((u16 *) msg));
2185 else
2186 msg_id = *((u16 *) msg);
2187
2188 cfgp = am->api_trace_cfg + msg_id;
2189 if (!cfgp)
2190 {
2191 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
2192 munmap (hp, file_size);
2193 vec_free (tmpbuf);
2194 am->replay_in_progress = 0;
2195 return;
2196 }
2197
2198 /* Copy the buffer (from the read-only mmap'ed file) */
2199 vec_validate (tmpbuf, size - 1 + sizeof (uword));
2200 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
2201 memset (tmpbuf, 0xf, sizeof (uword));
2202
2203 /*
2204 * Endian swap if needed. All msg data is supposed to be
2205 * in network byte order. All msg handlers are supposed to
2206 * know that. The generic message dumpers don't know that.
2207 * One could fix apigen, I suppose.
2208 */
2209 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
2210 {
2211 void (*endian_fp) (void *);
2212 if (msg_id >= vec_len (am->msg_endian_handlers)
2213 || (am->msg_endian_handlers[msg_id] == 0))
2214 {
2215 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
2216 munmap (hp, file_size);
2217 vec_free (tmpbuf);
2218 am->replay_in_progress = 0;
2219 return;
2220 }
2221 endian_fp = am->msg_endian_handlers[msg_id];
2222 (*endian_fp) (tmpbuf + sizeof (uword));
2223 }
2224
2225 /* msg_id always in network byte order */
2226 if (clib_arch_is_little_endian)
2227 {
2228 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
2229 *msg_idp = msg_id;
2230 }
2231
2232 switch (which)
2233 {
2234 case CUSTOM_DUMP:
2235 case DUMP:
2236 if (msg_id < vec_len (am->msg_print_handlers) &&
2237 am->msg_print_handlers[msg_id])
2238 {
2239 u8 *(*print_fp) (void *, void *);
2240
2241 print_fp = (void *) am->msg_print_handlers[msg_id];
2242 (*print_fp) (tmpbuf + sizeof (uword), vm);
2243 }
2244 else
2245 {
2246 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
2247 msg_id);
2248 break;
2249 }
2250 break;
2251
2252 case INITIALIZERS:
2253 if (msg_id < vec_len (am->msg_print_handlers) &&
2254 am->msg_print_handlers[msg_id])
2255 {
2256 u8 *s;
2257 int j;
2258 u8 *(*print_fp) (void *, void *);
2259
2260 print_fp = (void *) am->msg_print_handlers[msg_id];
2261
2262 vlib_cli_output (vm, "/*");
2263
2264 (*print_fp) (tmpbuf + sizeof (uword), vm);
2265 vlib_cli_output (vm, "*/\n");
2266
2267 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
2268 am->msg_names[msg_id], i,
2269 am->api_trace_cfg[msg_id].size);
2270
2271 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
2272 {
2273 if ((j & 7) == 0)
2274 s = format (s, "\n ");
2275 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
2276 }
2277 s = format (s, "\n};\n%c", 0);
2278 vlib_cli_output (vm, (char *) s);
2279 vec_free (s);
2280 }
2281 break;
2282
2283 case REPLAY:
2284 if (msg_id < vec_len (am->msg_print_handlers) &&
2285 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
2286 {
2287 void (*handler) (void *);
2288
2289 handler = (void *) am->msg_handlers[msg_id];
2290
2291 if (!am->is_mp_safe[msg_id])
2292 vl_msg_api_barrier_sync ();
2293 (*handler) (tmpbuf + sizeof (uword));
2294 if (!am->is_mp_safe[msg_id])
2295 vl_msg_api_barrier_release ();
2296 }
2297 else
2298 {
2299 if (cfgp->replay_enable)
2300 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
2301 msg_id);
2302 break;
2303 }
2304 break;
2305 }
2306
2307 _vec_len (tmpbuf) = 0;
2308 msg += size;
2309 }
2310
2311 if (saved_print_handlers)
2312 {
2313 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
2314 vec_len (am->msg_print_handlers) * sizeof (void *));
2315 vec_free (saved_print_handlers);
2316 }
2317
2318 munmap (hp, file_size);
2319 vec_free (tmpbuf);
2320 am->replay_in_progress = 0;
2321}
2322
2323static clib_error_t *
2324api_trace_command_fn (vlib_main_t * vm,
2325 unformat_input_t * input, vlib_cli_command_t * cmd)
2326{
2327 u32 nitems = 256 << 10;
2328 api_main_t *am = &api_main;
2329 vl_api_trace_which_t which = VL_API_TRACE_RX;
2330 u8 *filename;
2331 u32 first = 0;
2332 u32 last = (u32) ~ 0;
2333 FILE *fp;
2334 int rv;
2335
2336 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2337 {
2338 if (unformat (input, "on") || unformat (input, "enable"))
2339 {
2340 if (unformat (input, "nitems %d", &nitems))
2341 ;
2342 vl_msg_api_trace_configure (am, which, nitems);
2343 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
2344 }
2345 else if (unformat (input, "off"))
2346 {
2347 vl_msg_api_trace_onoff (am, which, 0);
2348 }
2349 else if (unformat (input, "save %s", &filename))
2350 {
2351 u8 *chroot_filename;
2352 if (strstr ((char *) filename, "..")
2353 || index ((char *) filename, '/'))
2354 {
2355 vlib_cli_output (vm, "illegal characters in filename '%s'",
2356 filename);
2357 return 0;
2358 }
2359
2360 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
2361
2362 vec_free (filename);
2363
2364 fp = fopen ((char *) chroot_filename, "w");
2365 if (fp == NULL)
2366 {
2367 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
2368 return 0;
2369 }
2370 rv = vl_msg_api_trace_save (am, which, fp);
2371 fclose (fp);
2372 if (rv == -1)
2373 vlib_cli_output (vm, "API Trace data not present\n");
2374 else if (rv == -2)
2375 vlib_cli_output (vm, "File for writing is closed\n");
2376 else if (rv == -10)
2377 vlib_cli_output (vm, "Error while writing header to file\n");
2378 else if (rv == -11)
2379 vlib_cli_output (vm, "Error while writing trace to file\n");
2380 else if (rv == -12)
2381 vlib_cli_output (vm,
2382 "Error while writing end of buffer trace to file\n");
2383 else if (rv == -13)
2384 vlib_cli_output (vm,
2385 "Error while writing start of buffer trace to file\n");
2386 else if (rv < 0)
2387 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
2388 else
2389 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
2390 vec_free (chroot_filename);
2391 }
2392 else if (unformat (input, "dump %s", &filename))
2393 {
2394 vl_msg_api_process_file (vm, filename, first, last, DUMP);
2395 }
2396 else if (unformat (input, "custom-dump %s", &filename))
2397 {
2398 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
2399 }
2400 else if (unformat (input, "replay %s", &filename))
2401 {
2402 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
2403 }
2404 else if (unformat (input, "initializers %s", &filename))
2405 {
2406 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
2407 }
2408 else if (unformat (input, "tx"))
2409 {
2410 which = VL_API_TRACE_TX;
2411 }
2412 else if (unformat (input, "first %d", &first))
2413 {
2414 ;
2415 }
2416 else if (unformat (input, "last %d", &last))
2417 {
2418 ;
2419 }
2420 else if (unformat (input, "status"))
2421 {
2422 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
2423 am, which);
2424 }
2425 else if (unformat (input, "free"))
2426 {
2427 vl_msg_api_trace_onoff (am, which, 0);
2428 vl_msg_api_trace_free (am, which);
2429 }
2430 else if (unformat (input, "post-mortem-on"))
2431 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2432 else if (unformat (input, "post-mortem-off"))
2433 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
2434 else
2435 return clib_error_return (0, "unknown input `%U'",
2436 format_unformat_error, input);
2437 }
2438 return 0;
2439}
2440
Dave Barach49fe0462017-09-12 17:06:56 -04002441/*?
2442 * Display, replay, or save a binary API trace
2443?*/
2444
Dave Barach80f54e22017-03-08 19:08:56 -05002445/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04002446VLIB_CLI_COMMAND (api_trace_command, static) =
2447{
2448 .path = "api trace",
2449 .short_help =
2450 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
2451 .function = api_trace_command_fn,
Dave Barach80f54e22017-03-08 19:08:56 -05002452};
2453/* *INDENT-ON* */
2454
2455static clib_error_t *
2456api_config_fn (vlib_main_t * vm, unformat_input_t * input)
2457{
2458 u32 nitems = 256 << 10;
2459 vl_api_trace_which_t which = VL_API_TRACE_RX;
2460 api_main_t *am = &api_main;
2461
2462 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2463 {
2464 if (unformat (input, "on") || unformat (input, "enable"))
2465 {
2466 if (unformat (input, "nitems %d", &nitems))
2467 ;
2468 vl_msg_api_trace_configure (am, which, nitems);
2469 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
2470 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2471 }
Dave Barach49fe0462017-09-12 17:06:56 -04002472 else if (unformat (input, "save-api-table %s",
2473 &am->save_msg_table_filename))
2474 ;
Dave Barach80f54e22017-03-08 19:08:56 -05002475 else
2476 return clib_error_return (0, "unknown input `%U'",
2477 format_unformat_error, input);
2478 }
2479 return 0;
2480}
2481
Dave Barach49fe0462017-09-12 17:06:56 -04002482/*?
2483 * This module has three configuration parameters:
2484 * "on" or "enable" - enables binary api tracing
2485 * "nitems <nnn>" - sets the size of the circular buffer to <nnn>
2486 * "save-api-table <filename>" - dumps the API message table to /tmp/<filename>
2487?*/
Dave Barach80f54e22017-03-08 19:08:56 -05002488VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
2489
Dave Barach10d8cc62017-05-30 09:30:07 -04002490static clib_error_t *
2491api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
2492{
2493 api_main_t *am = &api_main;
2494 u32 nitems;
2495
2496 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2497 {
2498 if (unformat (input, "length %d", &nitems) ||
2499 (unformat (input, "len %d", &nitems)))
2500 {
2501 if (nitems >= 1024)
2502 am->vlib_input_queue_length = nitems;
2503 else
2504 clib_warning ("vlib input queue length %d too small, ignored",
2505 nitems);
2506 }
2507 else
2508 return clib_error_return (0, "unknown input `%U'",
2509 format_unformat_error, input);
2510 }
2511 return 0;
2512}
2513
2514VLIB_CONFIG_FUNCTION (api_queue_config_fn, "api-queue");
2515
Dave Barach49fe0462017-09-12 17:06:56 -04002516static u8 *
2517extract_name (u8 * s)
2518{
2519 u8 *rv;
2520
2521 rv = vec_dup (s);
2522
2523 while (vec_len (rv) && rv[vec_len (rv)] != '_')
2524 _vec_len (rv)--;
2525
2526 rv[vec_len (rv)] = 0;
2527
2528 return rv;
2529}
2530
2531static u8 *
2532extract_crc (u8 * s)
2533{
2534 int i;
2535 u8 *rv;
2536
2537 rv = vec_dup (s);
2538
2539 for (i = vec_len (rv) - 1; i >= 0; i--)
2540 {
2541 if (rv[i] == '_')
2542 {
2543 vec_delete (rv, i + 1, 0);
2544 break;
2545 }
2546 }
2547 return rv;
2548}
2549
2550typedef struct
2551{
2552 u8 *name_and_crc;
2553 u8 *name;
2554 u8 *crc;
2555 u32 msg_index;
2556 int which;
2557} msg_table_unserialize_t;
2558
2559static int
2560table_id_cmp (void *a1, void *a2)
2561{
2562 msg_table_unserialize_t *n1 = a1;
2563 msg_table_unserialize_t *n2 = a2;
2564
2565 return (n1->msg_index - n2->msg_index);
2566}
2567
2568static int
2569table_name_and_crc_cmp (void *a1, void *a2)
2570{
2571 msg_table_unserialize_t *n1 = a1;
2572 msg_table_unserialize_t *n2 = a2;
2573
2574 return strcmp ((char *) n1->name_and_crc, (char *) n2->name_and_crc);
2575}
2576
2577static clib_error_t *
2578dump_api_table_file_command_fn (vlib_main_t * vm,
2579 unformat_input_t * input,
2580 vlib_cli_command_t * cmd)
2581{
2582 u8 *filename = 0;
2583 api_main_t *am = &api_main;
2584 serialize_main_t _sm, *sm = &_sm;
2585 clib_error_t *error;
2586 u32 nmsgs;
2587 u32 msg_index;
2588 u8 *name_and_crc;
2589 int compare_current = 0;
2590 int numeric_sort = 0;
2591 msg_table_unserialize_t *table = 0, *item;
2592 u32 i;
2593 u32 ndifferences = 0;
2594
2595 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2596 {
2597 if (unformat (input, "file %s", &filename))
2598 ;
2599 else if (unformat (input, "compare-current")
2600 || unformat (input, "compare"))
2601 compare_current = 1;
2602 else if (unformat (input, "numeric"))
2603 numeric_sort = 1;
2604 else
2605 return clib_error_return (0, "unknown input `%U'",
2606 format_unformat_error, input);
2607 }
2608
2609 if (numeric_sort && compare_current)
2610 return clib_error_return
2611 (0, "Comparison and numeric sorting are incompatible");
2612
2613 if (filename == 0)
2614 return clib_error_return (0, "File not specified");
2615
2616 /* Load the serialized message table from the table dump */
2617
Dave Barach59b25652017-09-10 15:04:27 -04002618 error = unserialize_open_clib_file (sm, (char *) filename);
Dave Barach49fe0462017-09-12 17:06:56 -04002619
2620 if (error)
2621 return error;
2622
2623 unserialize_integer (sm, &nmsgs, sizeof (u32));
2624
2625 for (i = 0; i < nmsgs; i++)
2626 {
2627 msg_index = unserialize_likely_small_unsigned_integer (sm);
2628 unserialize_cstring (sm, (char **) &name_and_crc);
2629 vec_add2 (table, item, 1);
2630 item->msg_index = msg_index;
2631 item->name_and_crc = name_and_crc;
2632 item->name = extract_name (name_and_crc);
2633 item->crc = extract_crc (name_and_crc);
2634 item->which = 0; /* file */
2635 }
2636 serialize_close (sm);
2637
2638 /* Compare with the current image? */
2639 if (compare_current)
2640 {
2641 /* Append the current message table */
Dave Barach59b25652017-09-10 15:04:27 -04002642 u8 *tblv = vl_api_serialize_message_table (am, 0);
Dave Barach49fe0462017-09-12 17:06:56 -04002643
2644 serialize_open_vector (sm, tblv);
2645 unserialize_integer (sm, &nmsgs, sizeof (u32));
2646
2647 for (i = 0; i < nmsgs; i++)
2648 {
2649 msg_index = unserialize_likely_small_unsigned_integer (sm);
2650 unserialize_cstring (sm, (char **) &name_and_crc);
2651
2652 vec_add2 (table, item, 1);
2653 item->msg_index = msg_index;
2654 item->name_and_crc = name_and_crc;
2655 item->name = extract_name (name_and_crc);
2656 item->crc = extract_crc (name_and_crc);
2657 item->which = 1; /* current_image */
2658 }
Dave Barach59b25652017-09-10 15:04:27 -04002659 vec_free (tblv);
Dave Barach49fe0462017-09-12 17:06:56 -04002660 }
2661
2662 /* Sort the table. */
2663 if (numeric_sort)
2664 vec_sort_with_function (table, table_id_cmp);
2665 else
2666 vec_sort_with_function (table, table_name_and_crc_cmp);
2667
2668 if (compare_current)
2669 {
2670 ndifferences = 0;
2671
2672 /*
2673 * In this case, the recovered table will have two entries per
2674 * API message. So, if entries i and i+1 match, the message definitions
2675 * are identical. Otherwise, the crc is different, or a message is
2676 * present in only one of the tables.
2677 */
2678 vlib_cli_output (vm, "%=60s %s", "Message Name", "Result");
2679
2680 for (i = 0; i < vec_len (table);)
2681 {
2682 /* Last message lonely? */
2683 if (i == vec_len (table) - 1)
2684 {
2685 ndifferences++;
2686 goto last_unique;
2687 }
2688
2689 /* Identical pair? */
2690 if (!strncmp
2691 ((char *) table[i].name_and_crc,
2692 (char *) table[i + 1].name_and_crc,
2693 vec_len (table[i].name_and_crc)))
2694 {
2695 i += 2;
2696 continue;
2697 }
2698
2699 ndifferences++;
2700
2701 /* Only in one of two tables? */
2702 if (strncmp ((char *) table[i].name, (char *) table[i + 1].name,
2703 vec_len (table[i].name)))
2704 {
2705 last_unique:
2706 vlib_cli_output (vm, "%-60s only in %s",
2707 table[i].name, table[i].which ?
2708 "image" : "file");
2709 i++;
2710 continue;
2711 }
2712 /* In both tables, but with different signatures */
2713 vlib_cli_output (vm, "%-60s definition changed", table[i].name);
2714 i += 2;
2715 }
2716 if (ndifferences == 0)
2717 vlib_cli_output (vm, "No api message signature differences found.");
2718 else
2719 vlib_cli_output (vm, "Found %u api message signature differences",
2720 ndifferences);
2721 goto cleanup;
2722 }
2723
2724 /* Dump the table, sorted as shown above */
2725 vlib_cli_output (vm, "%=60s %=8s %=10s", "Message name", "MsgID", "CRC");
2726
2727 for (i = 0; i < vec_len (table); i++)
2728 {
2729 item = table + i;
2730 vlib_cli_output (vm, "%-60s %8u %10s", item->name,
2731 item->msg_index, item->crc);
2732 }
2733
2734cleanup:
2735 for (i = 0; i < vec_len (table); i++)
2736 {
2737 vec_free (table[i].name_and_crc);
2738 vec_free (table[i].name);
2739 vec_free (table[i].crc);
2740 }
2741
2742 vec_free (table);
2743
2744 return 0;
2745}
2746
2747/*?
2748 * Displays a serialized API message decode table, sorted by message name
2749 *
2750 * @cliexpar
2751 * @cliexstart{show api dump file <filename>}
2752 * Message name MsgID CRC
2753 * accept_session 407 8e2a127e
2754 * accept_session_reply 408 67d8c22a
2755 * add_node_next 549 e4202993
2756 * add_node_next_reply 550 e89d6eed
2757 * etc.
2758 * @cliexend
2759?*/
2760
2761/*?
2762 * Compares a serialized API message decode table with the current image
2763 *
2764 * @cliexpar
2765 * @cliexstart{show api dump file <filename> compare}
2766 * ip_add_del_route definition changed
2767 * ip_table_add_del definition changed
2768 * l2_macs_event only in image
2769 * vnet_ip4_fib_counters only in file
2770 * vnet_ip4_nbr_counters only in file
2771 * @cliexend
2772?*/
2773
2774/*?
Dave Barach905c14a2017-09-25 08:47:59 -04002775 * Display a serialized API message decode table, compare a saved
2776 * decode table with the current image, to establish API differences.
2777 *
Dave Barach49fe0462017-09-12 17:06:56 -04002778?*/
2779/* *INDENT-OFF* */
2780VLIB_CLI_COMMAND (dump_api_table_file, static) =
2781{
2782 .path = "show api dump",
2783 .short_help = "show api dump file <filename> [numeric | compare-current]",
2784 .function = dump_api_table_file_command_fn,
2785};
2786/* *INDENT-ON* */
2787
Dave Barach371e4e12016-07-08 09:38:52 -04002788/*
2789 * fd.io coding-style-patch-verification: ON
2790 *
2791 * Local Variables:
2792 * eval: (c-set-style "gnu")
2793 * End:
2794 */