blob: 184a0f8c0a9acb4dcb5bbbbe5b3ed405d102a2ea [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;
Dave Barach59b25652017-09-10 15:04:27 -0400191 u8 *serialized_message_table_in_shmem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barach371e4e12016-07-08 09:38:52 -0400193 /*
194 * This is tortured. Maintain a vlib-address-space private
195 * pool of client registrations. We use the shared-memory virtual
196 * address of client structure as a handle, to allow direct
197 * manipulation of context quota vbls from the client library.
198 *
199 * This scheme causes trouble w/ API message trace replay, since
200 * some random VA from clib_mem_alloc() certainly won't
201 * occur in the Linux sim. The (very) few places
202 * that care need to use the pool index.
203 *
204 * Putting the registration object(s) into a pool in shared memory and
205 * using the pool index as a handle seems like a great idea.
206 * Unfortunately, each and every reference to that pool would need
207 * to be protected by a mutex:
208 *
209 * Client VLIB
210 * ------ ----
211 * convert pool index to
212 * pointer.
213 * <deschedule>
214 * expand pool
215 * <deschedule>
216 * kaboom!
217 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Dave Barach371e4e12016-07-08 09:38:52 -0400219 pool_get (am->vl_clients, regpp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Dave Barach371e4e12016-07-08 09:38:52 -0400221 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
Dave Barach371e4e12016-07-08 09:38:52 -0400223 pthread_mutex_lock (&svm->mutex);
224 oldheap = svm_push_data_heap (svm);
225 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Dave Barach371e4e12016-07-08 09:38:52 -0400227 regp = *regpp;
228 memset (regp, 0, sizeof (*regp));
229 regp->registration_type = REGISTRATION_TYPE_SHMEM;
230 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Dave Barach59b25652017-09-10 15:04:27 -0400231 regp->vlib_rp = svm;
232 regp->shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Dave Barach371e4e12016-07-08 09:38:52 -0400234 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
235 mp->input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Dave Barach371e4e12016-07-08 09:38:52 -0400237 regp->name = format (0, "%s", mp->name);
238 vec_add1 (regp->name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Dave Barach59b25652017-09-10 15:04:27 -0400240 serialized_message_table_in_shmem = vl_api_serialize_message_table (am, 0);
241
Dave Barach371e4e12016-07-08 09:38:52 -0400242 pthread_mutex_unlock (&svm->mutex);
243 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
Dave Barach371e4e12016-07-08 09:38:52 -0400245 rp = vl_msg_api_alloc (sizeof (*rp));
246 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
247 rp->handle = (uword) regp;
248 rp->index = vl_msg_api_handle_from_index_and_epoch
249 (regp->vl_api_registration_pool_index,
250 am->shmem_hdr->application_restarts);
251 rp->context = mp->context;
252 rp->response = ntohl (rv);
Dave Barach59b25652017-09-10 15:04:27 -0400253 rp->message_table = pointer_to_uword (serialized_message_table_in_shmem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Dave Barach371e4e12016-07-08 09:38:52 -0400255 vl_msg_api_send_shmem (q, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256}
257
Dave Barachb64e4e22017-03-14 09:10:56 -0400258static int
259call_reaper_functions (u32 client_index)
Dave Barach371e4e12016-07-08 09:38:52 -0400260{
Dave Barachb64e4e22017-03-14 09:10:56 -0400261 clib_error_t *error = 0;
262 _vl_msg_api_function_list_elt_t *i;
263
264 i = api_main.reaper_function_registrations;
265 while (i)
266 {
267 error = i->f (client_index);
268 if (error)
269 clib_error_report (error);
270 i = i->next_init_function;
271 }
Dave Barach371e4e12016-07-08 09:38:52 -0400272 return 0;
273}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
275/*
276 * vl_api_memclnt_delete_t_handler
277 */
Dave Barach371e4e12016-07-08 09:38:52 -0400278void
279vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280{
Dave Barach371e4e12016-07-08 09:38:52 -0400281 vl_api_registration_t **regpp;
282 vl_api_registration_t *regp;
283 vl_api_memclnt_delete_reply_t *rp;
284 svm_region_t *svm;
285 void *oldheap;
286 api_main_t *am = &api_main;
287 u32 handle, client_index, epoch;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barach371e4e12016-07-08 09:38:52 -0400289 handle = mp->index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Dave Barachb64e4e22017-03-14 09:10:56 -0400291 if (call_reaper_functions (handle))
Dave Barach371e4e12016-07-08 09:38:52 -0400292 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Dave Barach371e4e12016-07-08 09:38:52 -0400294 epoch = vl_msg_api_handle_get_epoch (handle);
295 client_index = vl_msg_api_handle_get_index (handle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Dave Barach371e4e12016-07-08 09:38:52 -0400297 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
298 {
299 clib_warning
300 ("Stale clnt delete index %d old epoch %d cur epoch %d",
301 client_index, epoch,
302 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
303 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 }
305
Dave Barach371e4e12016-07-08 09:38:52 -0400306 regpp = am->vl_clients + client_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Dave Barach371e4e12016-07-08 09:38:52 -0400308 if (!pool_is_free (am->vl_clients, regpp))
309 {
Dave Barach59b25652017-09-10 15:04:27 -0400310 int i;
Dave Barach371e4e12016-07-08 09:38:52 -0400311 regp = *regpp;
312 svm = am->vlib_rp;
Dave Barach59b25652017-09-10 15:04:27 -0400313 int private_registration = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Dave Barach59b25652017-09-10 15:04:27 -0400315 /*
316 * Note: the API message handling path will set am->vlib_rp
317 * as appropriate for pairwise / private memory segments
318 */
Dave Barach371e4e12016-07-08 09:38:52 -0400319 rp = vl_msg_api_alloc (sizeof (*rp));
320 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
321 rp->handle = mp->handle;
322 rp->response = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Dave Barach371e4e12016-07-08 09:38:52 -0400324 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
Dave Barach371e4e12016-07-08 09:38:52 -0400326 if (client_index != regp->vl_api_registration_pool_index)
327 {
328 clib_warning ("mismatch client_index %d pool_index %d",
329 client_index, regp->vl_api_registration_pool_index);
330 vl_msg_api_free (rp);
331 return;
332 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
Dave Barach59b25652017-09-10 15:04:27 -0400334 /* For horizontal scaling, add a hash table... */
335 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
336 {
337 /* Is this a pairwise / private API segment? */
338 if (am->vlib_private_rps[i] == svm)
339 {
340 /* Note: account for the memfd header page */
341 u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
342 u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
343
344 /*
345 * Kill the registration pool element before we make
346 * the index vanish forever
347 */
348 pool_put_index (am->vl_clients,
349 regp->vl_api_registration_pool_index);
350
351 vec_delete (am->vlib_private_rps, 1, i);
352 /* Kill it, accounting for the memfd header page */
353 if (munmap ((void *) virtual_base, virtual_size) < 0)
354 clib_unix_warning ("munmap");
355 /* Reset the queue-length-address cache */
356 vec_reset_length (vl_api_queue_cursizes);
357 private_registration = 1;
358 break;
359 }
360 }
361
Dave Barach371e4e12016-07-08 09:38:52 -0400362 /* No dangling references, please */
363 *regpp = 0;
364
Dave Barach59b25652017-09-10 15:04:27 -0400365 if (private_registration == 0)
366 {
367 pool_put_index (am->vl_clients,
368 regp->vl_api_registration_pool_index);
369 pthread_mutex_lock (&svm->mutex);
370 oldheap = svm_push_data_heap (svm);
371 /* Poison the old registration */
372 memset (regp, 0xF1, sizeof (*regp));
373 clib_mem_free (regp);
374 pthread_mutex_unlock (&svm->mutex);
375 svm_pop_heap (oldheap);
376 /*
377 * These messages must be freed manually, since they're set up
378 * as "bounce" messages. In the private_registration == 1 case,
379 * we kill the shared-memory segment which contains the message
380 * with munmap.
381 */
382 vl_msg_api_free (mp);
383 }
Dave Barach371e4e12016-07-08 09:38:52 -0400384 }
385 else
386 {
387 clib_warning ("unknown client ID %d", mp->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 }
389}
390
Dave Barach371e4e12016-07-08 09:38:52 -0400391void
392vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393{
Dave Barach371e4e12016-07-08 09:38:52 -0400394 vl_api_get_first_msg_id_reply_t *rmp;
395 unix_shared_memory_queue_t *q;
396 uword *p;
397 api_main_t *am = &api_main;
398 vl_api_msg_range_t *rp;
399 u8 name[64];
400 u16 first_msg_id = ~0;
401 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402
Dave Barach371e4e12016-07-08 09:38:52 -0400403 q = vl_api_client_index_to_input_queue (mp->client_index);
404 if (!q)
405 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Dave Barach371e4e12016-07-08 09:38:52 -0400407 if (am->msg_range_by_name == 0)
408 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Dave Barach371e4e12016-07-08 09:38:52 -0400410 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Dave Barach371e4e12016-07-08 09:38:52 -0400412 p = hash_get_mem (am->msg_range_by_name, name);
413 if (p == 0)
414 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
Dave Barach371e4e12016-07-08 09:38:52 -0400416 rp = vec_elt_at_index (am->msg_ranges, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
Dave Barach371e4e12016-07-08 09:38:52 -0400418 first_msg_id = rp->first_msg_id;
419 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
421out:
422
Dave Barach371e4e12016-07-08 09:38:52 -0400423 rmp = vl_msg_api_alloc (sizeof (*rmp));
424 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
425 rmp->context = mp->context;
426 rmp->retval = ntohl (rv);
427 rmp->first_msg_id = ntohs (first_msg_id);
428 vl_msg_api_send_shmem (q, (u8 *) & rmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429}
430
Dave Barach59b25652017-09-10 15:04:27 -0400431/**
432 * client answered a ping, stave off the grim reaper...
433 */
434
435void
436 vl_api_memclnt_keepalive_reply_t_handler
437 (vl_api_memclnt_keepalive_reply_t * mp)
438{
439 vl_api_registration_t *regp;
440 vlib_main_t *vm = vlib_get_main ();
441
442 regp = vl_api_client_index_to_registration (mp->context);
443 if (regp)
444 {
445 regp->last_heard = vlib_time_now (vm);
446 regp->unanswered_pings = 0;
447 }
448 else
449 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
450}
451
452/**
453 * We can send ourselves these messages if someone uses the
454 * builtin binary api test tool...
455 */
456static void
457vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
458{
459 vl_api_memclnt_keepalive_reply_t *rmp;
460 api_main_t *am;
461 vl_shmem_hdr_t *shmem_hdr;
462
463 am = &api_main;
464 shmem_hdr = am->shmem_hdr;
465
466 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
467 memset (rmp, 0, sizeof (*rmp));
468 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
469 rmp->context = mp->context;
470 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
471}
472
473#define foreach_vlib_api_msg \
474_(MEMCLNT_CREATE, memclnt_create) \
475_(MEMCLNT_DELETE, memclnt_delete) \
476_(GET_FIRST_MSG_ID, get_first_msg_id) \
477_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
478_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
480/*
481 * vl_api_init
482 */
Dave Barach371e4e12016-07-08 09:38:52 -0400483static int
Neale Rannse72be392017-04-26 13:59:20 -0700484memory_api_init (const char *region_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485{
Dave Barach371e4e12016-07-08 09:38:52 -0400486 int rv;
Dave Barach59b25652017-09-10 15:04:27 -0400487 api_main_t *am = &api_main;
Dave Barach371e4e12016-07-08 09:38:52 -0400488 vl_msg_api_msg_config_t cfg;
489 vl_msg_api_msg_config_t *c = &cfg;
490
Dave Barach0691d6e2017-01-05 10:08:52 -0500491 memset (c, 0, sizeof (*c));
492
Dave Barach371e4e12016-07-08 09:38:52 -0400493 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
494 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495
496#define _(N,n) do { \
497 c->id = VL_API_##N; \
498 c->name = #n; \
499 c->handler = vl_api_##n##_t_handler; \
500 c->cleanup = vl_noop_handler; \
501 c->endian = vl_api_##n##_t_endian; \
502 c->print = vl_api_##n##_t_print; \
503 c->size = sizeof(vl_api_##n##_t); \
504 c->traced = 1; /* trace, so these msgs print */ \
505 c->replay = 0; /* don't replay client create/delete msgs */ \
Dave Barach0691d6e2017-01-05 10:08:52 -0500506 c->message_bounce = 0; /* don't bounce this message */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 vl_msg_api_config(c);} while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400508
509 foreach_vlib_api_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510#undef _
511
Dave Barach59b25652017-09-10 15:04:27 -0400512 /*
513 * special-case freeing of memclnt_delete messages, so we can
514 * simply munmap pairwise / private API segments...
515 */
516 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
517 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
518
Dave Barach371e4e12016-07-08 09:38:52 -0400519 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520}
521
522#define foreach_histogram_bucket \
523_(400) \
524_(200) \
525_(100) \
526_(10)
527
Dave Barach371e4e12016-07-08 09:38:52 -0400528typedef enum
529{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530#define _(n) SLEEP_##n##_US,
Dave Barach371e4e12016-07-08 09:38:52 -0400531 foreach_histogram_bucket
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532#undef _
533 SLEEP_N_BUCKETS,
534} histogram_index_t;
535
536static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
537
Dave Barach371e4e12016-07-08 09:38:52 -0400538static void memclnt_queue_callback (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
Dave Barach987e11d2017-02-27 13:10:27 -0500540/*
541 * Callback to send ourselves a plugin numbering-space trace msg
542 */
543static void
544send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
545{
546 vl_api_trace_plugin_msg_ids_t *mp;
547 api_main_t *am = &api_main;
548 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
549 unix_shared_memory_queue_t *q;
550
551 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
552 memset (mp, 0, sizeof (*mp));
553
554 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
555 strncpy ((char *) mp->plugin_name, (char *) name,
556 sizeof (mp->plugin_name) - 1);
557 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
558 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
559
560 q = shmem_hdr->vl_input_queue;
561
562 vl_msg_api_send_shmem (q, (u8 *) & mp);
563}
564
Dave Barach59b25652017-09-10 15:04:27 -0400565static void
566send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
567{
568 vl_api_memclnt_keepalive_t *mp;
569 unix_shared_memory_queue_t *q;
570 api_main_t *am = &api_main;
571 svm_region_t *save_vlib_rp = am->vlib_rp;
572 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
573
574 q = regp->vl_input_queue;
575
576 /*
577 * If the queue head is moving, assume that the client is processing
578 * messages and skip the ping. This heuristic may fail if the queue
579 * is in the same position as last time, net of wrapping; in which
580 * case, the client will receive a keepalive.
581 */
582 if (regp->last_queue_head != q->head)
583 {
584 regp->last_heard = now;
585 regp->unanswered_pings = 0;
586 regp->last_queue_head = q->head;
587 return;
588 }
589
590 /*
591 * push/pop shared memory segment, so this routine
592 * will work with "normal" as well as "private segment"
593 * memory clients..
594 */
595
596 am->vlib_rp = regp->vlib_rp;
597 am->shmem_hdr = regp->shmem_hdr;
598
599 mp = vl_msg_api_alloc (sizeof (*mp));
600 memset (mp, 0, sizeof (*mp));
601 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
602 mp->context = mp->client_index =
603 vl_msg_api_handle_from_index_and_epoch
604 (regp->vl_api_registration_pool_index,
605 am->shmem_hdr->application_restarts);
606
607 regp->unanswered_pings++;
608
609 /* Failure-to-send due to a stuffed queue is absolutely expected */
610 if (unix_shared_memory_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
611 vl_msg_api_free (mp);
612
613 am->vlib_rp = save_vlib_rp;
614 am->shmem_hdr = save_shmem_hdr;
615}
616
617static void
618dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
619{
620
621 vl_api_registration_t **regpp;
622 vl_api_registration_t *regp;
623 static u32 *dead_indices;
624 static u32 *confused_indices;
625
626 vec_reset_length (dead_indices);
627 vec_reset_length (confused_indices);
628
629 /* *INDENT-OFF* */
630 pool_foreach (regpp, am->vl_clients,
631 ({
632 regp = *regpp;
633 if (regp)
634 {
635 /* If we haven't heard from this client recently... */
636 if (regp->last_heard < (now - 10.0))
637 {
638 if (regp->unanswered_pings == 2)
639 {
640 unix_shared_memory_queue_t *q;
641 q = regp->vl_input_queue;
642 if (kill (q->consumer_pid, 0) >=0)
643 {
644 clib_warning ("REAPER: lazy binary API client '%s'",
645 regp->name);
646 regp->unanswered_pings = 0;
647 regp->last_heard = now;
648 }
649 else
650 {
651 clib_warning ("REAPER: binary API client '%s' died",
652 regp->name);
653 vec_add1(dead_indices, regpp - am->vl_clients);
654 }
655 }
656 else
657 send_memclnt_keepalive (regp, now);
658 }
659 else
660 regp->unanswered_pings = 0;
661 }
662 else
663 {
664 clib_warning ("NULL client registration index %d",
665 regpp - am->vl_clients);
666 vec_add1 (confused_indices, regpp - am->vl_clients);
667 }
668 }));
669 /* *INDENT-ON* */
670 /* This should "never happen," but if it does, fix it... */
671 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
672 {
673 int i;
674 for (i = 0; i < vec_len (confused_indices); i++)
675 {
676 pool_put_index (am->vl_clients, confused_indices[i]);
677 }
678 }
679
680 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
681 {
682 int i;
683 svm_region_t *svm;
684 void *oldheap;
685
686 /* Allow the application to clean up its registrations */
687 for (i = 0; i < vec_len (dead_indices); i++)
688 {
689 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
690 if (regpp)
691 {
692 u32 handle;
693
694 handle = vl_msg_api_handle_from_index_and_epoch
695 (dead_indices[i], shm->application_restarts);
696 (void) call_reaper_functions (handle);
697 }
698 }
699
700 svm = am->vlib_rp;
701 pthread_mutex_lock (&svm->mutex);
702 oldheap = svm_push_data_heap (svm);
703
704 for (i = 0; i < vec_len (dead_indices); i++)
705 {
706 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
707 if (regpp)
708 {
709 /* Is this a pairwise SVM segment? */
710 if ((*regpp)->vlib_rp != svm)
711 {
712 int i;
713 svm_region_t *dead_rp = (*regpp)->vlib_rp;
714 /* Note: account for the memfd header page */
715 u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
716 u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
717
718 /* For horizontal scaling, add a hash table... */
719 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
720 if (am->vlib_private_rps[i] == dead_rp)
721 {
722 vec_delete (am->vlib_private_rps, 1, i);
723 goto found;
724 }
725 clib_warning ("private rp %llx AWOL", dead_rp);
726
727 found:
728 /* Kill it, accounting for the memfd header page */
729 if (munmap ((void *) virtual_base, virtual_size) < 0)
730 clib_unix_warning ("munmap");
731 /* Reset the queue-length-address cache */
732 vec_reset_length (vl_api_queue_cursizes);
733 }
734 else
735 {
736 /* Poison the old registration */
737 memset (*regpp, 0xF3, sizeof (**regpp));
738 clib_mem_free (*regpp);
739 }
740 /* no dangling references, please */
741 *regpp = 0;
742 }
743 else
744 {
745 svm_pop_heap (oldheap);
746 clib_warning ("Duplicate free, client index %d",
747 regpp - am->vl_clients);
748 oldheap = svm_push_data_heap (svm);
749 }
750 }
751
752 svm_client_scan_this_region_nolock (am->vlib_rp);
753
754 pthread_mutex_unlock (&svm->mutex);
755 svm_pop_heap (oldheap);
756 for (i = 0; i < vec_len (dead_indices); i++)
757 pool_put_index (am->vl_clients, dead_indices[i]);
758 }
759}
760
761
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762static uword
763memclnt_process (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400764 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765{
Dave Barach371e4e12016-07-08 09:38:52 -0400766 uword mp;
767 vl_shmem_hdr_t *shm;
768 unix_shared_memory_queue_t *q;
769 clib_error_t *e;
770 int rv;
771 api_main_t *am = &api_main;
772 f64 dead_client_scan_time;
773 f64 sleep_time, start_time;
774 f64 vector_rate;
Dave Barach59b25652017-09-10 15:04:27 -0400775 clib_error_t *socksvr_api_init (vlib_main_t * vm);
776 clib_error_t *error;
Dave Barach987e11d2017-02-27 13:10:27 -0500777 int i;
Dave Barach59b25652017-09-10 15:04:27 -0400778 vl_socket_args_for_process_t *a;
779 uword event_type;
780 uword *event_data = 0;
781 int private_segment_rotor = 0;
782 svm_region_t *vlib_rp;
783 f64 now;
Dave Barach371e4e12016-07-08 09:38:52 -0400784
785 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
786
787 if ((rv = memory_api_init (am->region_name)) < 0)
788 {
Dave Barach59b25652017-09-10 15:04:27 -0400789 clib_warning ("memory_api_init returned %d, quitting...", rv);
790 return 0;
791 }
792
793 if ((error = socksvr_api_init (vm)))
794 {
795 clib_error_report (error);
796 clib_warning ("socksvr_api_init failed, quitting...");
797 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798 }
799
Dave Barach371e4e12016-07-08 09:38:52 -0400800 shm = am->shmem_hdr;
801 ASSERT (shm);
802 q = shm->vl_input_queue;
803 ASSERT (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804
Dave Barach371e4e12016-07-08 09:38:52 -0400805 e = vlib_call_init_exit_functions
806 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
807 if (e)
808 clib_error_report (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
Dave Barach59b25652017-09-10 15:04:27 -0400810 sleep_time = 10.0;
811 dead_client_scan_time = vlib_time_now (vm) + 10.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
Dave Barach987e11d2017-02-27 13:10:27 -0500813 /*
814 * Send plugin message range messages for each plugin we loaded
815 */
816 for (i = 0; i < vec_len (am->msg_ranges); i++)
817 {
818 vl_api_msg_range_t *rp = am->msg_ranges + i;
819 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
820 rp->last_msg_id);
821 }
822
Dave Barach49fe0462017-09-12 17:06:56 -0400823 /*
Dave Barach49fe0462017-09-12 17:06:56 -0400824 * Save the api message table snapshot, if configured
825 */
826 if (am->save_msg_table_filename)
827 {
828 int fd, rv;
829 u8 *chroot_file;
Dave Barach59b25652017-09-10 15:04:27 -0400830 u8 *serialized_message_table;
831
832 /*
833 * Snapshoot the api message table.
834 */
Dave Barach49fe0462017-09-12 17:06:56 -0400835 if (strstr ((char *) am->save_msg_table_filename, "..")
836 || index ((char *) am->save_msg_table_filename, '/'))
837 {
838 clib_warning ("illegal save-message-table filename '%s'",
839 am->save_msg_table_filename);
840 goto skip_save;
841 }
842
843 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
844
845 fd = creat ((char *) chroot_file, 0644);
846
847 if (fd < 0)
848 {
849 clib_unix_warning ("creat");
850 goto skip_save;
851 }
Dave Barach59b25652017-09-10 15:04:27 -0400852
853 serialized_message_table = vl_api_serialize_message_table (am, 0);
854
Dave Barach49fe0462017-09-12 17:06:56 -0400855 rv = write (fd, serialized_message_table,
856 vec_len (serialized_message_table));
857
858 if (rv != vec_len (serialized_message_table))
859 clib_unix_warning ("write");
860
861 rv = close (fd);
862 if (rv < 0)
863 clib_unix_warning ("close");
864
865 vec_free (chroot_file);
Dave Barach59b25652017-09-10 15:04:27 -0400866 vec_free (serialized_message_table);
Dave Barach49fe0462017-09-12 17:06:56 -0400867 }
868
869skip_save:
Dave Barach49fe0462017-09-12 17:06:56 -0400870
Dave Barach371e4e12016-07-08 09:38:52 -0400871 /* $$$ pay attention to frame size, control CPU usage */
872 while (1)
873 {
Dave Barach371e4e12016-07-08 09:38:52 -0400874 i8 *headp;
875 int need_broadcast;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876
Dave Barach371e4e12016-07-08 09:38:52 -0400877 /*
878 * There's a reason for checking the queue before
879 * sleeping. If the vlib application crashes, it's entirely
880 * possible for a client to enqueue a connect request
881 * during the process restart interval.
882 *
883 * Unless some force of physics causes the new incarnation
884 * of the application to process the request, the client will
885 * sit and wait for Godot...
886 */
887 vector_rate = vlib_last_vector_length_per_node (vm);
888 start_time = vlib_time_now (vm);
889 while (1)
890 {
891 pthread_mutex_lock (&q->mutex);
892 if (q->cursize == 0)
893 {
894 vm->api_queue_nonempty = 0;
895 pthread_mutex_unlock (&q->mutex);
896
897 if (TRACE_VLIB_MEMORY_QUEUE)
898 {
899 /* *INDENT-OFF* */
900 ELOG_TYPE_DECLARE (e) =
901 {
902 .format = "q-underflow: len %d",
903 .format_args = "i4",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904 };
Dave Barach371e4e12016-07-08 09:38:52 -0400905 /* *INDENT-ON* */
906 struct
907 {
908 u32 len;
909 } *ed;
910 ed = ELOG_DATA (&vm->elog_main, e);
911 ed->len = 0;
912 }
913 sleep_time = 20.0;
914 break;
915 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916
Dave Barach371e4e12016-07-08 09:38:52 -0400917 headp = (i8 *) (q->data + sizeof (uword) * q->head);
918 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919
Dave Barach371e4e12016-07-08 09:38:52 -0400920 q->head++;
921 need_broadcast = (q->cursize == q->maxsize / 2);
922 q->cursize--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923
Dave Barach371e4e12016-07-08 09:38:52 -0400924 if (PREDICT_FALSE (q->head == q->maxsize))
925 q->head = 0;
926 pthread_mutex_unlock (&q->mutex);
927 if (need_broadcast)
928 (void) pthread_cond_broadcast (&q->condvar);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929
Dave Barach371e4e12016-07-08 09:38:52 -0400930 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931
Dave Barach371e4e12016-07-08 09:38:52 -0400932 /* Allow no more than 10us without a pause */
933 if (vlib_time_now (vm) > start_time + 10e-6)
934 {
935 int index = SLEEP_400_US;
936 if (vector_rate > 40.0)
937 sleep_time = 400e-6;
938 else if (vector_rate > 20.0)
939 {
940 index = SLEEP_200_US;
941 sleep_time = 200e-6;
942 }
943 else if (vector_rate >= 1.0)
944 {
945 index = SLEEP_100_US;
946 sleep_time = 100e-6;
947 }
948 else
949 {
950 index = SLEEP_10_US;
951 sleep_time = 10e-6;
952 }
953 vector_rate_histogram[index] += 1;
954 break;
955 }
956 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
Dave Barach59b25652017-09-10 15:04:27 -0400958 /*
959 * see if we have any private api shared-memory segments
960 * If so, push required context variables, and process
961 * a message.
962 */
963 if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
Dave Barach371e4e12016-07-08 09:38:52 -0400964 {
Dave Barach59b25652017-09-10 15:04:27 -0400965 unix_shared_memory_queue_t *save_vlib_input_queue = q;
966 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
967 svm_region_t *save_vlib_rp = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968
Dave Barach59b25652017-09-10 15:04:27 -0400969 vlib_rp = am->vlib_rp = am->vlib_private_rps[private_segment_rotor];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970
Dave Barach59b25652017-09-10 15:04:27 -0400971 am->shmem_hdr = (void *) vlib_rp->user_ctx;
972 q = am->shmem_hdr->vl_input_queue;
973
974 pthread_mutex_lock (&q->mutex);
975 if (q->cursize > 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400976 {
Dave Barach59b25652017-09-10 15:04:27 -0400977 headp = (i8 *) (q->data + sizeof (uword) * q->head);
978 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
Dave Barach59b25652017-09-10 15:04:27 -0400980 q->head++;
981 need_broadcast = (q->cursize == q->maxsize / 2);
982 q->cursize--;
983
984 if (PREDICT_FALSE (q->head == q->maxsize))
985 q->head = 0;
986 pthread_mutex_unlock (&q->mutex);
987 if (need_broadcast)
988 (void) pthread_cond_broadcast (&q->condvar);
989
990 pthread_mutex_unlock (&q->mutex);
991
992 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
993 }
994 else
995 pthread_mutex_unlock (&q->mutex);
996
997 q = save_vlib_input_queue;
998 am->shmem_hdr = save_shmem_hdr;
999 am->vlib_rp = save_vlib_rp;
1000
1001 private_segment_rotor++;
1002 if (private_segment_rotor >= vec_len (am->vlib_private_rps))
1003 private_segment_rotor = 0;
1004 }
1005
1006 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1007 vec_reset_length (event_data);
1008 event_type = vlib_process_get_events (vm, &event_data);
1009 now = vlib_time_now (vm);
1010
1011 switch (event_type)
1012 {
1013 case QUEUE_SIGNAL_EVENT:
1014 vm->queue_signal_pending = 0;
1015 break;
1016
1017 case SOCKET_READ_EVENT:
1018 for (i = 0; i < vec_len (event_data); i++)
Dave Barach371e4e12016-07-08 09:38:52 -04001019 {
Dave Barach59b25652017-09-10 15:04:27 -04001020 a = pool_elt_at_index (socket_main.process_args, event_data[i]);
1021 vl_api_socket_process_msg (a->clib_file, a->regp,
1022 (i8 *) a->data);
1023 vec_free (a->data);
1024 pool_put (socket_main.process_args, a);
Dave Barach371e4e12016-07-08 09:38:52 -04001025 }
Dave Barach59b25652017-09-10 15:04:27 -04001026 break;
Dave Barach371e4e12016-07-08 09:38:52 -04001027
Dave Barach59b25652017-09-10 15:04:27 -04001028 /* Timeout... */
1029 case -1:
1030 break;
1031
1032 default:
1033 clib_warning ("unknown event type %d", event_type);
1034 break;
1035 }
1036
1037 if (now > dead_client_scan_time)
1038 {
1039 dead_client_scan (am, shm, now);
1040 dead_client_scan_time = vlib_time_now (vm) + 10.0;
Dave Barach371e4e12016-07-08 09:38:52 -04001041 }
1042
1043 if (TRACE_VLIB_MEMORY_QUEUE)
1044 {
1045 /* *INDENT-OFF* */
1046 ELOG_TYPE_DECLARE (e) = {
1047 .format = "q-awake: len %d",
1048 .format_args = "i4",
1049 };
1050 /* *INDENT-ON* */
1051 struct
1052 {
1053 u32 len;
1054 } *ed;
1055 ed = ELOG_DATA (&vm->elog_main, e);
1056 ed->len = q->cursize;
1057 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 }
1059
Dave Barach371e4e12016-07-08 09:38:52 -04001060 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061}
Dave Barach49fe0462017-09-12 17:06:56 -04001062/* *INDENT-OFF* */
Dave Barach59b25652017-09-10 15:04:27 -04001063VLIB_REGISTER_NODE (memclnt_node) =
1064{
1065 .function = memclnt_process,
1066 .type = VLIB_NODE_TYPE_PROCESS,
1067 .name = "api-rx-from-ring",
1068 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach49fe0462017-09-12 17:06:56 -04001069};
1070/* *INDENT-ON* */
1071
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
1073static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001074vl_api_show_histogram_command (vlib_main_t * vm,
1075 unformat_input_t * input,
1076 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077{
Dave Barach371e4e12016-07-08 09:38:52 -04001078 u64 total_counts = 0;
1079 int i;
1080
1081 for (i = 0; i < SLEEP_N_BUCKETS; i++)
1082 {
1083 total_counts += vector_rate_histogram[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 }
1085
Dave Barach371e4e12016-07-08 09:38:52 -04001086 if (total_counts == 0)
1087 {
1088 vlib_cli_output (vm, "No control-plane activity.");
1089 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090 }
1091
1092#define _(n) \
1093 do { \
1094 f64 percent; \
1095 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
1096 / (f64) total_counts; \
1097 percent *= 100.0; \
1098 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
1099 vector_rate_histogram[SLEEP_##n##_US], \
1100 percent); \
1101 } while (0);
Dave Barach371e4e12016-07-08 09:38:52 -04001102 foreach_histogram_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103#undef _
1104
Dave Barach371e4e12016-07-08 09:38:52 -04001105 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106}
1107
Dave Barach49fe0462017-09-12 17:06:56 -04001108/*?
1109 * Display the binary api sleep-time histogram
1110?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001111/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001112VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) =
1113{
1114 .path = "show api histogram",
1115 .short_help = "show api histogram",
1116 .function = vl_api_show_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117};
Dave Barach371e4e12016-07-08 09:38:52 -04001118/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119
1120static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001121vl_api_clear_histogram_command (vlib_main_t * vm,
1122 unformat_input_t * input,
1123 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124{
Dave Barach371e4e12016-07-08 09:38:52 -04001125 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126
Dave Barach371e4e12016-07-08 09:38:52 -04001127 for (i = 0; i < SLEEP_N_BUCKETS; i++)
1128 vector_rate_histogram[i] = 0;
1129 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130}
1131
Dave Barach49fe0462017-09-12 17:06:56 -04001132/*?
1133 * Clear the binary api sleep-time histogram
1134?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001135/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001136VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) =
1137{
1138 .path = "clear api histogram",
1139 .short_help = "clear api histogram",
1140 .function = vl_api_clear_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141};
Dave Barach371e4e12016-07-08 09:38:52 -04001142/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143
Dave Barach59b25652017-09-10 15:04:27 -04001144volatile int **vl_api_queue_cursizes;
1145
Dave Barach371e4e12016-07-08 09:38:52 -04001146static void
1147memclnt_queue_callback (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148{
Dave Barach59b25652017-09-10 15:04:27 -04001149 int i;
1150 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
Dave Barach59b25652017-09-10 15:04:27 -04001152 if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
1153 1 + vec_len (am->vlib_private_rps)))
Dave Barach16c75df2016-05-31 14:05:46 -04001154 {
Dave Barach16c75df2016-05-31 14:05:46 -04001155 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barach371e4e12016-07-08 09:38:52 -04001156 unix_shared_memory_queue_t *q;
1157
Dave Barach16c75df2016-05-31 14:05:46 -04001158 if (shmem_hdr == 0)
Dave Barach371e4e12016-07-08 09:38:52 -04001159 return;
1160
Dave Barach16c75df2016-05-31 14:05:46 -04001161 q = shmem_hdr->vl_input_queue;
1162 if (q == 0)
Dave Barach371e4e12016-07-08 09:38:52 -04001163 return;
Dave Barach59b25652017-09-10 15:04:27 -04001164
1165 vec_add1 (vl_api_queue_cursizes, &q->cursize);
1166
1167 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
1168 {
1169 svm_region_t *vlib_rp = am->vlib_private_rps[i];
1170
1171 shmem_hdr = (void *) vlib_rp->user_ctx;
1172 q = shmem_hdr->vl_input_queue;
1173 vec_add1 (vl_api_queue_cursizes, &q->cursize);
1174 }
Dave Barach16c75df2016-05-31 14:05:46 -04001175 }
Dave Barach371e4e12016-07-08 09:38:52 -04001176
Dave Barach59b25652017-09-10 15:04:27 -04001177 for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
Dave Barach16c75df2016-05-31 14:05:46 -04001178 {
Dave Barach59b25652017-09-10 15:04:27 -04001179 if (*vl_api_queue_cursizes[i])
1180 {
1181 vm->queue_signal_pending = 1;
1182 vm->api_queue_nonempty = 1;
1183 vlib_process_signal_event (vm, memclnt_node.index,
1184 /* event_type */ QUEUE_SIGNAL_EVENT,
1185 /* event_data */ 0);
1186 break;
1187 }
Dave Barach16c75df2016-05-31 14:05:46 -04001188 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189}
1190
Dave Barach371e4e12016-07-08 09:38:52 -04001191void
1192vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193{
Dave Barach371e4e12016-07-08 09:38:52 -04001194 vlib_node_set_state (vm, memclnt_node.index,
1195 (enable
1196 ? VLIB_NODE_STATE_POLLING
1197 : VLIB_NODE_STATE_DISABLED));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198}
1199
1200static uword
1201api_rx_from_node (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -04001202 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203{
Dave Barach371e4e12016-07-08 09:38:52 -04001204 uword n_packets = frame->n_vectors;
1205 uword n_left_from;
1206 u32 *from;
1207 static u8 *long_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
Dave Barach371e4e12016-07-08 09:38:52 -04001209 vec_validate (long_msg, 4095);
1210 n_left_from = frame->n_vectors;
1211 from = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212
Dave Barach371e4e12016-07-08 09:38:52 -04001213 while (n_left_from > 0)
1214 {
1215 u32 bi0;
1216 vlib_buffer_t *b0;
1217 void *msg;
1218 uword msg_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219
Dave Barach371e4e12016-07-08 09:38:52 -04001220 bi0 = from[0];
1221 b0 = vlib_get_buffer (vm, bi0);
1222 from += 1;
1223 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224
Dave Barach371e4e12016-07-08 09:38:52 -04001225 msg = b0->data + b0->current_data;
1226 msg_len = b0->current_length;
1227 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
1228 {
1229 ASSERT (long_msg != 0);
1230 _vec_len (long_msg) = 0;
1231 vec_add (long_msg, msg, msg_len);
1232 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
1233 {
1234 b0 = vlib_get_buffer (vm, b0->next_buffer);
1235 msg = b0->data + b0->current_data;
1236 msg_len = b0->current_length;
1237 vec_add (long_msg, msg, msg_len);
1238 }
1239 msg = long_msg;
1240 }
1241 vl_msg_api_handler_no_trace_no_free (msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 }
1243
Dave Barach371e4e12016-07-08 09:38:52 -04001244 /* Free what we've been given. */
1245 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246
Dave Barach371e4e12016-07-08 09:38:52 -04001247 return n_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248}
1249
Dave Barach371e4e12016-07-08 09:38:52 -04001250/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
1252 .function = api_rx_from_node,
1253 .type = VLIB_NODE_TYPE_INTERNAL,
1254 .vector_size = 4,
1255 .name = "api-rx-from-node",
1256};
Dave Barach371e4e12016-07-08 09:38:52 -04001257/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258
1259static clib_error_t *
1260setup_memclnt_exit (vlib_main_t * vm)
1261{
Dave Barach371e4e12016-07-08 09:38:52 -04001262 atexit (vl_unmap_shmem);
1263 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264}
1265
1266VLIB_INIT_FUNCTION (setup_memclnt_exit);
1267
Dave Barach59b25652017-09-10 15:04:27 -04001268u8 *
1269format_api_message_rings (u8 * s, va_list * args)
1270{
1271 api_main_t *am = va_arg (*args, api_main_t *);
1272 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
1273 int main_segment = va_arg (*args, int);
1274 ring_alloc_t *ap;
1275 int i;
1276
1277 if (shmem_hdr == 0)
1278 return format (s, "%8s %8s %8s %8s %8s\n",
1279 "Owner", "Size", "Nitems", "Hits", "Misses");
1280
1281 ap = shmem_hdr->vl_rings;
1282
1283 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
1284 {
1285 s = format (s, "%8s %8d %8d %8d %8d\n",
1286 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
1287 ap++;
1288 }
1289
1290 ap = shmem_hdr->client_rings;
1291
1292 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
1293 {
1294 s = format (s, "%8s %8d %8d %8d %8d\n",
1295 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
1296 ap++;
1297 }
1298
1299 if (main_segment)
1300 {
1301 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
1302 s = format
1303 (s,
1304 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
1305 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
1306 shmem_hdr->garbage_collects);
1307 }
1308 return s;
1309}
1310
Ed Warnickecb9cada2015-12-08 15:45:58 -07001311
1312static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001313vl_api_ring_command (vlib_main_t * vm,
1314 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315{
Dave Barach371e4e12016-07-08 09:38:52 -04001316 int i;
Dave Barach371e4e12016-07-08 09:38:52 -04001317 vl_shmem_hdr_t *shmem_hdr;
1318 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319
Dave Barach371e4e12016-07-08 09:38:52 -04001320 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001321
Dave Barach371e4e12016-07-08 09:38:52 -04001322 if (shmem_hdr == 0)
1323 {
1324 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
1325 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326 }
1327
Dave Barach59b25652017-09-10 15:04:27 -04001328 vlib_cli_output (vm, "Main API segment rings:");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329
Dave Barach59b25652017-09-10 15:04:27 -04001330 vlib_cli_output (vm, "%U", format_api_message_rings, am,
1331 0 /* print header */ , 0 /* notused */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332
Dave Barach59b25652017-09-10 15:04:27 -04001333 vlib_cli_output (vm, "%U", format_api_message_rings, am,
1334 shmem_hdr, 1 /* main segment */ );
1335
1336 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
Dave Barach371e4e12016-07-08 09:38:52 -04001337 {
Dave Barach59b25652017-09-10 15:04:27 -04001338 svm_region_t *vlib_rp = am->vlib_private_rps[i];
1339 shmem_hdr = (void *) vlib_rp->user_ctx;
1340 vl_api_registration_t **regpp;
1341 vl_api_registration_t *regp;
1342
1343 /* For horizontal scaling, add a hash table... */
1344 /* *INDENT-OFF* */
1345 pool_foreach (regpp, am->vl_clients,
1346 ({
1347 regp = *regpp;
1348 if (regp && regp->vlib_rp == vlib_rp)
1349 {
1350 vlib_cli_output (vm, "%s segment rings:", regp->name);
1351 goto found;
1352 }
1353 }));
1354 /* *INDENT-ON* */
1355 found:
1356 vlib_cli_output (vm, "%U", format_api_message_rings, am,
1357 shmem_hdr, 0 /* main segment */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358 }
1359
Dave Barach371e4e12016-07-08 09:38:52 -04001360 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361}
1362
Dave Barach371e4e12016-07-08 09:38:52 -04001363void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
1364 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365
Dave Barach371e4e12016-07-08 09:38:52 -04001366void
1367dump_socket_clients (vlib_main_t * vm, api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368{
1369}
1370
1371static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001372vl_api_client_command (vlib_main_t * vm,
1373 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374{
Dave Barach371e4e12016-07-08 09:38:52 -04001375 vl_api_registration_t **regpp, *regp;
1376 unix_shared_memory_queue_t *q;
1377 char *health;
1378 api_main_t *am = &api_main;
1379 u32 *confused_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001380
Dave Barach371e4e12016-07-08 09:38:52 -04001381 if (!pool_elts (am->vl_clients))
1382 goto socket_clients;
1383 vlib_cli_output (vm, "Shared memory clients");
1384 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
1385 "Name", "PID", "Queue Length", "Queue VA", "Health");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001386
Dave Barach371e4e12016-07-08 09:38:52 -04001387 /* *INDENT-OFF* */
1388 pool_foreach (regpp, am->vl_clients,
1389 ({
1390 regp = *regpp;
1391
1392 if (regp)
1393 {
Dave Barach59b25652017-09-10 15:04:27 -04001394 if (regp->unanswered_pings > 0)
1395 health = "questionable";
Dave Barach371e4e12016-07-08 09:38:52 -04001396 else
Dave Barach59b25652017-09-10 15:04:27 -04001397 health = "OK";
1398
1399 q = regp->vl_input_queue;
1400
Dave Barach371e4e12016-07-08 09:38:52 -04001401 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
1402 regp->name, q->consumer_pid, q->cursize,
1403 q, health);
1404 }
1405 else
1406 {
1407 clib_warning ("NULL client registration index %d",
1408 regpp - am->vl_clients);
1409 vec_add1 (confused_indices, regpp - am->vl_clients);
1410 }
1411 }));
1412 /* *INDENT-ON* */
1413
1414 /* This should "never happen," but if it does, fix it... */
1415 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
1416 {
1417 int i;
1418 for (i = 0; i < vec_len (confused_indices); i++)
1419 {
1420 pool_put_index (am->vl_clients, confused_indices[i]);
1421 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422 }
Dave Barach371e4e12016-07-08 09:38:52 -04001423 vec_free (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424
Dave Barach371e4e12016-07-08 09:38:52 -04001425 if (am->missing_clients)
1426 vlib_cli_output (vm, "%u messages with missing clients",
1427 am->missing_clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428socket_clients:
Dave Barach371e4e12016-07-08 09:38:52 -04001429 dump_socket_clients (vm, am);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430
Dave Barach371e4e12016-07-08 09:38:52 -04001431 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432}
1433
Calvin16649372016-07-28 13:52:05 -04001434static clib_error_t *
1435vl_api_status_command (vlib_main_t * vm,
1436 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1437{
1438 api_main_t *am = &api_main;
1439
1440 // check if rx_trace and tx_trace are not null pointers
1441
1442 if (am->rx_trace == 0)
1443 {
1444 vlib_cli_output (vm, "RX Trace disabled\n");
1445 }
1446 else
1447 {
1448 if (am->rx_trace->enabled == 0)
1449 vlib_cli_output (vm, "RX Trace disabled\n");
1450 else
1451 vlib_cli_output (vm, "RX Trace enabled\n");
1452 }
1453
1454 if (am->tx_trace == 0)
1455 {
1456 vlib_cli_output (vm, "TX Trace disabled\n");
1457 }
1458 else
1459 {
1460 if (am->tx_trace->enabled == 0)
1461 vlib_cli_output (vm, "TX Trace disabled\n");
1462 else
1463 vlib_cli_output (vm, "TX Trace enabled\n");
1464 }
1465
1466 return 0;
1467}
1468
Dave Barach371e4e12016-07-08 09:38:52 -04001469/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001470VLIB_CLI_COMMAND (cli_show_api_command, static) =
1471{
1472 .path = "show api",
1473 .short_help = "Show API information",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001474};
Dave Barach371e4e12016-07-08 09:38:52 -04001475/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476
Dave Barach49fe0462017-09-12 17:06:56 -04001477/*?
1478 * Display binary api message allocation ring statistics
1479?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001480/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001481VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
1482{
1483 .path = "show api ring-stats",
1484 .short_help = "Message ring statistics",
1485 .function = vl_api_ring_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486};
Dave Barach371e4e12016-07-08 09:38:52 -04001487/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488
Dave Barach49fe0462017-09-12 17:06:56 -04001489/*?
1490 * Display current api client connections
1491?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001492/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001493VLIB_CLI_COMMAND (cli_show_api_clients_command, static) =
1494{
1495 .path = "show api clients",
1496 .short_help = "Client information",
1497 .function = vl_api_client_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498};
Dave Barach371e4e12016-07-08 09:38:52 -04001499/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500
Dave Barach49fe0462017-09-12 17:06:56 -04001501/*?
1502 * Display the current api message tracing status
1503?*/
Calvin16649372016-07-28 13:52:05 -04001504/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001505VLIB_CLI_COMMAND (cli_show_api_status_command, static) =
1506{
1507 .path = "show api trace-status",
1508 .short_help = "Display API trace status",
1509 .function = vl_api_status_command,
Calvin16649372016-07-28 13:52:05 -04001510};
1511/* *INDENT-ON* */
1512
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001514vl_api_message_table_command (vlib_main_t * vm,
1515 unformat_input_t * input,
1516 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517{
Dave Barach371e4e12016-07-08 09:38:52 -04001518 api_main_t *am = &api_main;
1519 int i;
1520 int verbose = 0;
1521
1522 if (unformat (input, "verbose"))
1523 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524
1525
Dave Barach371e4e12016-07-08 09:38:52 -04001526 if (verbose == 0)
1527 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1528 else
1529 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1530 "MP-safe");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001531
Dave Barach371e4e12016-07-08 09:38:52 -04001532 for (i = 1; i < vec_len (am->msg_names); i++)
1533 {
1534 if (verbose == 0)
1535 {
1536 vlib_cli_output (vm, "%-4d %s", i,
1537 am->msg_names[i] ? am->msg_names[i] :
1538 " [no handler]");
1539 }
1540 else
1541 {
1542 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1543 am->msg_names[i] ? am->msg_names[i] :
1544 " [no handler]", am->message_bounce[i],
1545 am->is_mp_safe[i]);
1546 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547 }
1548
Dave Barach371e4e12016-07-08 09:38:52 -04001549 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001550}
1551
Dave Barach49fe0462017-09-12 17:06:56 -04001552/*?
1553 * Display the current api message decode tables
1554?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001555/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001556VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) =
1557{
1558 .path = "show api message-table",
1559 .short_help = "Message Table",
1560 .function = vl_api_message_table_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561};
Dave Barach371e4e12016-07-08 09:38:52 -04001562/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001565vl_api_trace_command (vlib_main_t * vm,
1566 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001567{
Dave Barach371e4e12016-07-08 09:38:52 -04001568 u32 nitems = 1024;
1569 vl_api_trace_which_t which = VL_API_TRACE_RX;
Dave Barach371e4e12016-07-08 09:38:52 -04001570 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001571
Dave Barach371e4e12016-07-08 09:38:52 -04001572 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1573 {
1574 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1575 goto configure;
1576 else if (unformat (input, "tx nitems %u", &nitems)
1577 || unformat (input, "tx"))
1578 {
1579 which = VL_API_TRACE_RX;
1580 goto configure;
1581 }
1582 else if (unformat (input, "on rx"))
1583 {
1584 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1585 }
1586 else if (unformat (input, "on tx"))
1587 {
1588 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1589 }
1590 else if (unformat (input, "on"))
1591 {
1592 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1593 }
1594 else if (unformat (input, "off"))
1595 {
1596 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1597 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1598 }
1599 else if (unformat (input, "free"))
1600 {
1601 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1602 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1603 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1604 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1605 }
Dave Barach371e4e12016-07-08 09:38:52 -04001606 else if (unformat (input, "debug on"))
1607 {
1608 am->msg_print_flag = 1;
1609 }
1610 else if (unformat (input, "debug off"))
1611 {
1612 am->msg_print_flag = 0;
1613 }
1614 else
1615 return clib_error_return (0, "unknown input `%U'",
1616 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617 }
Dave Barach371e4e12016-07-08 09:38:52 -04001618 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
Dave Barach371e4e12016-07-08 09:38:52 -04001620configure:
1621 if (vl_msg_api_trace_configure (am, which, nitems))
1622 {
1623 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1624 which, nitems);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001625 }
1626
Dave Barach371e4e12016-07-08 09:38:52 -04001627 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001628}
1629
Dave Barach49fe0462017-09-12 17:06:56 -04001630/*?
1631 * Control the binary API trace mechanism
1632?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001633/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001634VLIB_CLI_COMMAND (trace, static) =
1635{
1636 .path = "set api-trace [on][on tx][on rx][off][free][debug on][debug off]",
1637 .short_help = "API trace",
1638 .function = vl_api_trace_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001639};
Dave Barach371e4e12016-07-08 09:38:52 -04001640/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001641
1642clib_error_t *
1643vlibmemory_init (vlib_main_t * vm)
1644{
Dave Barach309bef22016-01-22 16:09:52 -05001645 api_main_t *am = &api_main;
Dave Barachb3d93da2016-08-03 14:34:38 -04001646 svm_map_region_args_t _a, *a = &_a;
Dave Barach59b25652017-09-10 15:04:27 -04001647 clib_error_t *error;
Dave Barachc3799992016-08-15 11:12:27 -04001648
Dave Barachb3d93da2016-08-03 14:34:38 -04001649 memset (a, 0, sizeof (*a));
1650 a->root_path = am->root_path;
1651 a->name = SVM_GLOBAL_REGION_NAME;
Dave Barachc3799992016-08-15 11:12:27 -04001652 a->baseva = (am->global_baseva != 0) ?
Dave Barachb3d93da2016-08-03 14:34:38 -04001653 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1654 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1655 a->flags = SVM_FLAGS_NODATA;
1656 a->uid = am->api_uid;
1657 a->gid = am->api_gid;
Dave Barachc3799992016-08-15 11:12:27 -04001658 a->pvt_heap_size =
1659 (am->global_pvt_heap_size !=
1660 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
Dave Barachb3d93da2016-08-03 14:34:38 -04001661
1662 svm_region_init_args (a);
Dave Barach59b25652017-09-10 15:04:27 -04001663
1664 error = vlib_call_init_function (vm, vlibsocket_init);
1665
1666 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001667}
1668
1669VLIB_INIT_FUNCTION (vlibmemory_init);
1670
Dave Barach371e4e12016-07-08 09:38:52 -04001671void
Neale Rannse72be392017-04-26 13:59:20 -07001672vl_set_memory_region_name (const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001673{
Dave Barach371e4e12016-07-08 09:38:52 -04001674 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001675
Dave Barach371e4e12016-07-08 09:38:52 -04001676 am->region_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001677}
1678
Dave Barach371e4e12016-07-08 09:38:52 -04001679static int
1680range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001681{
Dave Barach371e4e12016-07-08 09:38:52 -04001682 int len0, len1, clen;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683
Dave Barach371e4e12016-07-08 09:38:52 -04001684 len0 = vec_len (a0->name);
1685 len1 = vec_len (a1->name);
1686 clen = len0 < len1 ? len0 : len1;
1687 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001688}
1689
Dave Barach371e4e12016-07-08 09:38:52 -04001690static u8 *
1691format_api_msg_range (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001692{
Dave Barach371e4e12016-07-08 09:38:52 -04001693 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001694
Dave Barach371e4e12016-07-08 09:38:52 -04001695 if (rp == 0)
Dave Barach49fe0462017-09-12 17:06:56 -04001696 s = format (s, "%-50s%9s%9s", "Name", "First-ID", "Last-ID");
Dave Barach371e4e12016-07-08 09:38:52 -04001697 else
Dave Barach49fe0462017-09-12 17:06:56 -04001698 s = format (s, "%-50s%9d%9d", rp->name, rp->first_msg_id,
Dave Barach371e4e12016-07-08 09:38:52 -04001699 rp->last_msg_id);
1700
1701 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001702}
1703
1704static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001705vl_api_show_plugin_command (vlib_main_t * vm,
1706 unformat_input_t * input,
1707 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001708{
Dave Barach371e4e12016-07-08 09:38:52 -04001709 api_main_t *am = &api_main;
1710 vl_api_msg_range_t *rp = 0;
1711 int i;
1712
1713 if (vec_len (am->msg_ranges) == 0)
1714 {
1715 vlib_cli_output (vm, "No plugin API message ranges configured...");
1716 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001717 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001718
Dave Barach371e4e12016-07-08 09:38:52 -04001719 rp = vec_dup (am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001720
Dave Barach371e4e12016-07-08 09:38:52 -04001721 vec_sort_with_function (rp, range_compare);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001722
Dave Barach371e4e12016-07-08 09:38:52 -04001723 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1724 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1725
1726 for (i = 0; i < vec_len (rp); i++)
1727 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1728
Dave Barachdfbee412017-03-02 18:24:10 -05001729 vec_free (rp);
1730
Dave Barach371e4e12016-07-08 09:38:52 -04001731 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001732}
1733
Dave Barach49fe0462017-09-12 17:06:56 -04001734/*?
1735 * Display the plugin binary API message range table
1736?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001737/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001738VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) =
1739{
1740 .path = "show api plugin",
1741 .short_help = "show api plugin",
1742 .function = vl_api_show_plugin_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743};
Dave Barach371e4e12016-07-08 09:38:52 -04001744/* *INDENT-ON* */
Dave Barach4e281a42015-12-14 11:13:29 -05001745
Dave Barach371e4e12016-07-08 09:38:52 -04001746static void
1747vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001748{
Dave Barach11b8dbf2017-04-24 10:46:54 -04001749 vl_api_rpc_call_reply_t *rmp;
Dave Barach371e4e12016-07-08 09:38:52 -04001750 int (*fp) (void *);
Dave Barach4e281a42015-12-14 11:13:29 -05001751 i32 rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -04001752 vlib_main_t *vm = vlib_get_main ();
Dave Barach4e281a42015-12-14 11:13:29 -05001753
1754 if (mp->function == 0)
1755 {
1756 rv = -1;
1757 clib_warning ("rpc NULL function pointer");
1758 }
Dave Barach371e4e12016-07-08 09:38:52 -04001759
Dave Barach4e281a42015-12-14 11:13:29 -05001760 else
1761 {
1762 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001763 vlib_worker_thread_barrier_sync (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001764
Dave Barach371e4e12016-07-08 09:38:52 -04001765 fp = uword_to_pointer (mp->function, int (*)(void *));
1766 rv = fp (mp->data);
Dave Barach4e281a42015-12-14 11:13:29 -05001767
1768 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001769 vlib_worker_thread_barrier_release (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001770 }
1771
1772 if (mp->send_reply)
1773 {
Dave Barach371e4e12016-07-08 09:38:52 -04001774 unix_shared_memory_queue_t *q =
1775 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach4e281a42015-12-14 11:13:29 -05001776 if (q)
Dave Barach371e4e12016-07-08 09:38:52 -04001777 {
1778 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barach11b8dbf2017-04-24 10:46:54 -04001779 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
Dave Barach371e4e12016-07-08 09:38:52 -04001780 rmp->context = mp->context;
1781 rmp->retval = rv;
1782 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1783 }
Dave Barach4e281a42015-12-14 11:13:29 -05001784 }
1785 if (mp->multicast)
1786 {
1787 clib_warning ("multicast not yet implemented...");
1788 }
1789}
1790
Dave Barach371e4e12016-07-08 09:38:52 -04001791static void
Dave Barach11b8dbf2017-04-24 10:46:54 -04001792vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001793{
Dave Barach371e4e12016-07-08 09:38:52 -04001794 clib_warning ("unimplemented");
1795}
1796
John Lo7e9743a2017-09-23 08:59:58 -04001797always_inline void
1798vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
1799 u8 force_rpc)
Dave Barach371e4e12016-07-08 09:38:52 -04001800{
1801 vl_api_rpc_call_t *mp;
Dave Barach4e281a42015-12-14 11:13:29 -05001802 api_main_t *am = &api_main;
1803 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barache9183632016-10-04 16:53:56 -04001804 unix_shared_memory_queue_t *q;
Dave Barach4e281a42015-12-14 11:13:29 -05001805
Dave Barache9183632016-10-04 16:53:56 -04001806 /* Main thread: call the function directly */
John Lo7e9743a2017-09-23 08:59:58 -04001807 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
Dave Barache9183632016-10-04 16:53:56 -04001808 {
1809 vlib_main_t *vm = vlib_get_main ();
1810 void (*call_fp) (void *);
1811
1812 vlib_worker_thread_barrier_sync (vm);
1813
1814 call_fp = fp;
1815 call_fp (data);
1816
1817 vlib_worker_thread_barrier_release (vm);
1818 return;
1819 }
1820
1821 /* Any other thread, actually do an RPC call... */
Dave Barach4e281a42015-12-14 11:13:29 -05001822 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
Dave Barache9183632016-10-04 16:53:56 -04001823
Dave Barach4e281a42015-12-14 11:13:29 -05001824 memset (mp, 0, sizeof (*mp));
Damjan Marionf1213b82016-03-13 02:22:06 +01001825 clib_memcpy (mp->data, data, data_length);
Dave Barach4e281a42015-12-14 11:13:29 -05001826 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
Dave Barach371e4e12016-07-08 09:38:52 -04001827 mp->function = pointer_to_uword (fp);
Dave Barach4e281a42015-12-14 11:13:29 -05001828 mp->need_barrier_sync = 1;
Dave Barach371e4e12016-07-08 09:38:52 -04001829
Dave Barache9183632016-10-04 16:53:56 -04001830 /*
1831 * Use the "normal" control-plane mechanism for the main thread.
1832 * Well, almost. if the main input queue is full, we cannot
1833 * block. Otherwise, we can expect a barrier sync timeout.
1834 */
1835 q = shmem_hdr->vl_input_queue;
1836
1837 while (pthread_mutex_trylock (&q->mutex))
1838 vlib_worker_thread_barrier_check ();
1839
1840 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1841 {
1842 pthread_mutex_unlock (&q->mutex);
1843 vlib_worker_thread_barrier_check ();
1844 while (pthread_mutex_trylock (&q->mutex))
1845 vlib_worker_thread_barrier_check ();
1846 }
1847
1848 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1849
1850 pthread_mutex_unlock (&q->mutex);
Dave Barach4e281a42015-12-14 11:13:29 -05001851}
1852
John Lo7e9743a2017-09-23 08:59:58 -04001853/*
1854 * Check if called from worker threads.
1855 * If so, make rpc call of fp through shmem.
1856 * Otherwise, call fp directly
1857 */
1858void
1859vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1860{
1861 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1862 0);
1863}
1864
1865/*
1866 * Always make rpc call of fp through shmem, useful for calling from threads
1867 * not setup as worker threads, such as DPDK callback thread
1868 */
1869void
1870vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1871{
1872 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1873 1);
1874}
1875
Dave Barach987e11d2017-02-27 13:10:27 -05001876static void
1877vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1878{
Dave Barachdfbee412017-03-02 18:24:10 -05001879 api_main_t *am = &api_main;
1880 vl_api_msg_range_t *rp;
1881 uword *p;
Dave Barach987e11d2017-02-27 13:10:27 -05001882
Dave Barachdfbee412017-03-02 18:24:10 -05001883 /* Noop (except for tracing) during normal operation */
1884 if (am->replay_in_progress == 0)
1885 return;
1886
1887 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1888 if (p == 0)
1889 {
1890 clib_warning ("WARNING: traced plugin '%s' not in current image",
1891 mp->plugin_name);
1892 return;
1893 }
1894
1895 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1896 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1897 {
1898 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1899 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1900 rp->first_msg_id);
1901 }
1902
1903 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1904 {
1905 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1906 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1907 rp->last_msg_id);
1908 }
1909}
Dave Barach987e11d2017-02-27 13:10:27 -05001910
Dave Barach4e281a42015-12-14 11:13:29 -05001911#define foreach_rpc_api_msg \
1912_(RPC_CALL,rpc_call) \
Dave Barach11b8dbf2017-04-24 10:46:54 -04001913_(RPC_CALL_REPLY,rpc_call_reply)
Dave Barach4e281a42015-12-14 11:13:29 -05001914
Dave Barach987e11d2017-02-27 13:10:27 -05001915#define foreach_plugin_trace_msg \
1916_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1917
Dave Barach69128d02017-09-26 10:54:34 -04001918/*
1919 * Set the rpc callback at our earliest possible convenience.
1920 * This avoids ordering issues between thread_init() -> start_workers and
1921 * an init function which we could define here. If we ever intend to use
1922 * vlib all by itself, we can't create a link-time dependency on
1923 * an init function here and a typical "call foo_init first"
1924 * guitar lick.
1925 */
1926
1927extern void *rpc_call_main_thread_cb_fn;
1928
Dave Barach4e281a42015-12-14 11:13:29 -05001929static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001930rpc_api_hookup (vlib_main_t * vm)
Dave Barach4e281a42015-12-14 11:13:29 -05001931{
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001932 api_main_t *am = &api_main;
Dave Barach4e281a42015-12-14 11:13:29 -05001933#define _(N,n) \
1934 vl_msg_api_set_handlers(VL_API_##N, #n, \
1935 vl_api_##n##_t_handler, \
1936 vl_noop_handler, \
1937 vl_noop_handler, \
1938 vl_api_##n##_t_print, \
Dave Barach371e4e12016-07-08 09:38:52 -04001939 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1940 foreach_rpc_api_msg;
Dave Barach4e281a42015-12-14 11:13:29 -05001941#undef _
Dave Barach987e11d2017-02-27 13:10:27 -05001942
1943#define _(N,n) \
1944 vl_msg_api_set_handlers(VL_API_##N, #n, \
1945 vl_api_##n##_t_handler, \
1946 vl_noop_handler, \
1947 vl_noop_handler, \
1948 vl_api_##n##_t_print, \
1949 sizeof(vl_api_##n##_t), 1 /* do trace */);
1950 foreach_plugin_trace_msg;
1951#undef _
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001952
1953 /* No reason to halt the parade to create a trace record... */
1954 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
Dave Barach69128d02017-09-26 10:54:34 -04001955 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
Dave Barach371e4e12016-07-08 09:38:52 -04001956 return 0;
Dave Barach4e281a42015-12-14 11:13:29 -05001957}
1958
Dave Barach371e4e12016-07-08 09:38:52 -04001959VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1960
Dave Barach80f54e22017-03-08 19:08:56 -05001961typedef enum
1962{
1963 DUMP,
1964 CUSTOM_DUMP,
1965 REPLAY,
1966 INITIALIZERS,
1967} vl_api_replay_t;
1968
1969u8 *
1970format_vl_msg_api_trace_status (u8 * s, va_list * args)
1971{
1972 api_main_t *am = va_arg (*args, api_main_t *);
1973 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
1974 vl_api_trace_t *tp;
1975 char *trace_name;
1976
1977 switch (which)
1978 {
1979 case VL_API_TRACE_TX:
1980 tp = am->tx_trace;
1981 trace_name = "TX trace";
1982 break;
1983
1984 case VL_API_TRACE_RX:
1985 tp = am->rx_trace;
1986 trace_name = "RX trace";
1987 break;
1988
1989 default:
1990 abort ();
1991 }
1992
1993 if (tp == 0)
1994 {
1995 s = format (s, "%s: not yet configured.\n", trace_name);
1996 return s;
1997 }
1998
1999 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
2000 trace_name, vec_len (tp->traces), tp->nitems,
2001 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
2002 return s;
2003}
2004
2005void vl_msg_api_custom_dump_configure (api_main_t * am)
2006 __attribute__ ((weak));
2007void
2008vl_msg_api_custom_dump_configure (api_main_t * am)
2009{
2010}
2011
2012static void
2013vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
2014 u32 first_index, u32 last_index,
2015 vl_api_replay_t which)
2016{
2017 vl_api_trace_file_header_t *hp;
2018 int i, fd;
2019 struct stat statb;
2020 size_t file_size;
2021 u8 *msg;
2022 u8 endian_swap_needed = 0;
2023 api_main_t *am = &api_main;
2024 u8 *tmpbuf = 0;
2025 u32 nitems;
2026 void **saved_print_handlers = 0;
2027
2028 fd = open ((char *) filename, O_RDONLY);
2029
2030 if (fd < 0)
2031 {
2032 vlib_cli_output (vm, "Couldn't open %s\n", filename);
2033 return;
2034 }
2035
2036 if (fstat (fd, &statb) < 0)
2037 {
2038 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
2039 close (fd);
2040 return;
2041 }
2042
2043 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
2044 {
2045 vlib_cli_output (vm, "File not plausible: %s\n", filename);
2046 close (fd);
2047 return;
2048 }
2049
2050 file_size = statb.st_size;
2051 file_size = (file_size + 4095) & ~(4096);
2052
2053 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
2054
2055 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
2056 {
2057 vlib_cli_output (vm, "mmap failed: %s\n", filename);
2058 close (fd);
2059 return;
2060 }
2061 close (fd);
2062
2063 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
2064 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
2065 endian_swap_needed = 1;
2066
2067 if (endian_swap_needed)
2068 nitems = ntohl (hp->nitems);
2069 else
2070 nitems = hp->nitems;
2071
2072 if (last_index == (u32) ~ 0)
2073 {
2074 last_index = nitems - 1;
2075 }
2076
2077 if (first_index >= nitems || last_index >= nitems)
2078 {
2079 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
2080 first_index, last_index, nitems - 1);
2081 munmap (hp, file_size);
2082 return;
2083 }
2084 if (hp->wrapped)
2085 vlib_cli_output (vm,
2086 "Note: wrapped/incomplete trace, results may vary\n");
2087
2088 if (which == CUSTOM_DUMP)
2089 {
2090 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
2091 vl_msg_api_custom_dump_configure (am);
2092 }
2093
2094
2095 msg = (u8 *) (hp + 1);
2096
2097 for (i = 0; i < first_index; i++)
2098 {
2099 trace_cfg_t *cfgp;
2100 int size;
2101 u16 msg_id;
2102
2103 size = clib_host_to_net_u32 (*(u32 *) msg);
2104 msg += sizeof (u32);
2105
2106 if (clib_arch_is_little_endian)
2107 msg_id = ntohs (*((u16 *) msg));
2108 else
2109 msg_id = *((u16 *) msg);
2110
2111 cfgp = am->api_trace_cfg + msg_id;
2112 if (!cfgp)
2113 {
2114 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
2115 munmap (hp, file_size);
2116 return;
2117 }
2118 msg += size;
2119 }
2120
2121 if (which == REPLAY)
2122 am->replay_in_progress = 1;
2123
2124 for (; i <= last_index; i++)
2125 {
2126 trace_cfg_t *cfgp;
2127 u16 *msg_idp;
2128 u16 msg_id;
2129 int size;
2130
2131 if (which == DUMP)
2132 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
2133
2134 size = clib_host_to_net_u32 (*(u32 *) msg);
2135 msg += sizeof (u32);
2136
2137 if (clib_arch_is_little_endian)
2138 msg_id = ntohs (*((u16 *) msg));
2139 else
2140 msg_id = *((u16 *) msg);
2141
2142 cfgp = am->api_trace_cfg + msg_id;
2143 if (!cfgp)
2144 {
2145 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
2146 munmap (hp, file_size);
2147 vec_free (tmpbuf);
2148 am->replay_in_progress = 0;
2149 return;
2150 }
2151
2152 /* Copy the buffer (from the read-only mmap'ed file) */
2153 vec_validate (tmpbuf, size - 1 + sizeof (uword));
2154 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
2155 memset (tmpbuf, 0xf, sizeof (uword));
2156
2157 /*
2158 * Endian swap if needed. All msg data is supposed to be
2159 * in network byte order. All msg handlers are supposed to
2160 * know that. The generic message dumpers don't know that.
2161 * One could fix apigen, I suppose.
2162 */
2163 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
2164 {
2165 void (*endian_fp) (void *);
2166 if (msg_id >= vec_len (am->msg_endian_handlers)
2167 || (am->msg_endian_handlers[msg_id] == 0))
2168 {
2169 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
2170 munmap (hp, file_size);
2171 vec_free (tmpbuf);
2172 am->replay_in_progress = 0;
2173 return;
2174 }
2175 endian_fp = am->msg_endian_handlers[msg_id];
2176 (*endian_fp) (tmpbuf + sizeof (uword));
2177 }
2178
2179 /* msg_id always in network byte order */
2180 if (clib_arch_is_little_endian)
2181 {
2182 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
2183 *msg_idp = msg_id;
2184 }
2185
2186 switch (which)
2187 {
2188 case CUSTOM_DUMP:
2189 case DUMP:
2190 if (msg_id < vec_len (am->msg_print_handlers) &&
2191 am->msg_print_handlers[msg_id])
2192 {
2193 u8 *(*print_fp) (void *, void *);
2194
2195 print_fp = (void *) am->msg_print_handlers[msg_id];
2196 (*print_fp) (tmpbuf + sizeof (uword), vm);
2197 }
2198 else
2199 {
2200 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
2201 msg_id);
2202 break;
2203 }
2204 break;
2205
2206 case INITIALIZERS:
2207 if (msg_id < vec_len (am->msg_print_handlers) &&
2208 am->msg_print_handlers[msg_id])
2209 {
2210 u8 *s;
2211 int j;
2212 u8 *(*print_fp) (void *, void *);
2213
2214 print_fp = (void *) am->msg_print_handlers[msg_id];
2215
2216 vlib_cli_output (vm, "/*");
2217
2218 (*print_fp) (tmpbuf + sizeof (uword), vm);
2219 vlib_cli_output (vm, "*/\n");
2220
2221 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
2222 am->msg_names[msg_id], i,
2223 am->api_trace_cfg[msg_id].size);
2224
2225 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
2226 {
2227 if ((j & 7) == 0)
2228 s = format (s, "\n ");
2229 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
2230 }
2231 s = format (s, "\n};\n%c", 0);
2232 vlib_cli_output (vm, (char *) s);
2233 vec_free (s);
2234 }
2235 break;
2236
2237 case REPLAY:
2238 if (msg_id < vec_len (am->msg_print_handlers) &&
2239 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
2240 {
2241 void (*handler) (void *);
2242
2243 handler = (void *) am->msg_handlers[msg_id];
2244
2245 if (!am->is_mp_safe[msg_id])
2246 vl_msg_api_barrier_sync ();
2247 (*handler) (tmpbuf + sizeof (uword));
2248 if (!am->is_mp_safe[msg_id])
2249 vl_msg_api_barrier_release ();
2250 }
2251 else
2252 {
2253 if (cfgp->replay_enable)
2254 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
2255 msg_id);
2256 break;
2257 }
2258 break;
2259 }
2260
2261 _vec_len (tmpbuf) = 0;
2262 msg += size;
2263 }
2264
2265 if (saved_print_handlers)
2266 {
2267 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
2268 vec_len (am->msg_print_handlers) * sizeof (void *));
2269 vec_free (saved_print_handlers);
2270 }
2271
2272 munmap (hp, file_size);
2273 vec_free (tmpbuf);
2274 am->replay_in_progress = 0;
2275}
2276
2277static clib_error_t *
2278api_trace_command_fn (vlib_main_t * vm,
2279 unformat_input_t * input, vlib_cli_command_t * cmd)
2280{
2281 u32 nitems = 256 << 10;
2282 api_main_t *am = &api_main;
2283 vl_api_trace_which_t which = VL_API_TRACE_RX;
2284 u8 *filename;
2285 u32 first = 0;
2286 u32 last = (u32) ~ 0;
2287 FILE *fp;
2288 int rv;
2289
2290 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2291 {
2292 if (unformat (input, "on") || unformat (input, "enable"))
2293 {
2294 if (unformat (input, "nitems %d", &nitems))
2295 ;
2296 vl_msg_api_trace_configure (am, which, nitems);
2297 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
2298 }
2299 else if (unformat (input, "off"))
2300 {
2301 vl_msg_api_trace_onoff (am, which, 0);
2302 }
2303 else if (unformat (input, "save %s", &filename))
2304 {
2305 u8 *chroot_filename;
2306 if (strstr ((char *) filename, "..")
2307 || index ((char *) filename, '/'))
2308 {
2309 vlib_cli_output (vm, "illegal characters in filename '%s'",
2310 filename);
2311 return 0;
2312 }
2313
2314 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
2315
2316 vec_free (filename);
2317
2318 fp = fopen ((char *) chroot_filename, "w");
2319 if (fp == NULL)
2320 {
2321 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
2322 return 0;
2323 }
2324 rv = vl_msg_api_trace_save (am, which, fp);
2325 fclose (fp);
2326 if (rv == -1)
2327 vlib_cli_output (vm, "API Trace data not present\n");
2328 else if (rv == -2)
2329 vlib_cli_output (vm, "File for writing is closed\n");
2330 else if (rv == -10)
2331 vlib_cli_output (vm, "Error while writing header to file\n");
2332 else if (rv == -11)
2333 vlib_cli_output (vm, "Error while writing trace to file\n");
2334 else if (rv == -12)
2335 vlib_cli_output (vm,
2336 "Error while writing end of buffer trace to file\n");
2337 else if (rv == -13)
2338 vlib_cli_output (vm,
2339 "Error while writing start of buffer trace to file\n");
2340 else if (rv < 0)
2341 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
2342 else
2343 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
2344 vec_free (chroot_filename);
2345 }
2346 else if (unformat (input, "dump %s", &filename))
2347 {
2348 vl_msg_api_process_file (vm, filename, first, last, DUMP);
2349 }
2350 else if (unformat (input, "custom-dump %s", &filename))
2351 {
2352 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
2353 }
2354 else if (unformat (input, "replay %s", &filename))
2355 {
2356 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
2357 }
2358 else if (unformat (input, "initializers %s", &filename))
2359 {
2360 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
2361 }
2362 else if (unformat (input, "tx"))
2363 {
2364 which = VL_API_TRACE_TX;
2365 }
2366 else if (unformat (input, "first %d", &first))
2367 {
2368 ;
2369 }
2370 else if (unformat (input, "last %d", &last))
2371 {
2372 ;
2373 }
2374 else if (unformat (input, "status"))
2375 {
2376 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
2377 am, which);
2378 }
2379 else if (unformat (input, "free"))
2380 {
2381 vl_msg_api_trace_onoff (am, which, 0);
2382 vl_msg_api_trace_free (am, which);
2383 }
2384 else if (unformat (input, "post-mortem-on"))
2385 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2386 else if (unformat (input, "post-mortem-off"))
2387 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
2388 else
2389 return clib_error_return (0, "unknown input `%U'",
2390 format_unformat_error, input);
2391 }
2392 return 0;
2393}
2394
Dave Barach49fe0462017-09-12 17:06:56 -04002395/*?
2396 * Display, replay, or save a binary API trace
2397?*/
2398
Dave Barach80f54e22017-03-08 19:08:56 -05002399/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04002400VLIB_CLI_COMMAND (api_trace_command, static) =
2401{
2402 .path = "api trace",
2403 .short_help =
2404 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
2405 .function = api_trace_command_fn,
Dave Barach80f54e22017-03-08 19:08:56 -05002406};
2407/* *INDENT-ON* */
2408
2409static clib_error_t *
2410api_config_fn (vlib_main_t * vm, unformat_input_t * input)
2411{
2412 u32 nitems = 256 << 10;
2413 vl_api_trace_which_t which = VL_API_TRACE_RX;
2414 api_main_t *am = &api_main;
2415
2416 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2417 {
2418 if (unformat (input, "on") || unformat (input, "enable"))
2419 {
2420 if (unformat (input, "nitems %d", &nitems))
2421 ;
2422 vl_msg_api_trace_configure (am, which, nitems);
2423 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
2424 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2425 }
Dave Barach49fe0462017-09-12 17:06:56 -04002426 else if (unformat (input, "save-api-table %s",
2427 &am->save_msg_table_filename))
2428 ;
Dave Barach80f54e22017-03-08 19:08:56 -05002429 else
2430 return clib_error_return (0, "unknown input `%U'",
2431 format_unformat_error, input);
2432 }
2433 return 0;
2434}
2435
Dave Barach49fe0462017-09-12 17:06:56 -04002436/*?
2437 * This module has three configuration parameters:
2438 * "on" or "enable" - enables binary api tracing
2439 * "nitems <nnn>" - sets the size of the circular buffer to <nnn>
2440 * "save-api-table <filename>" - dumps the API message table to /tmp/<filename>
2441?*/
Dave Barach80f54e22017-03-08 19:08:56 -05002442VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
2443
Dave Barach10d8cc62017-05-30 09:30:07 -04002444static clib_error_t *
2445api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
2446{
2447 api_main_t *am = &api_main;
2448 u32 nitems;
2449
2450 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2451 {
2452 if (unformat (input, "length %d", &nitems) ||
2453 (unformat (input, "len %d", &nitems)))
2454 {
2455 if (nitems >= 1024)
2456 am->vlib_input_queue_length = nitems;
2457 else
2458 clib_warning ("vlib input queue length %d too small, ignored",
2459 nitems);
2460 }
2461 else
2462 return clib_error_return (0, "unknown input `%U'",
2463 format_unformat_error, input);
2464 }
2465 return 0;
2466}
2467
2468VLIB_CONFIG_FUNCTION (api_queue_config_fn, "api-queue");
2469
Dave Barach49fe0462017-09-12 17:06:56 -04002470static u8 *
2471extract_name (u8 * s)
2472{
2473 u8 *rv;
2474
2475 rv = vec_dup (s);
2476
2477 while (vec_len (rv) && rv[vec_len (rv)] != '_')
2478 _vec_len (rv)--;
2479
2480 rv[vec_len (rv)] = 0;
2481
2482 return rv;
2483}
2484
2485static u8 *
2486extract_crc (u8 * s)
2487{
2488 int i;
2489 u8 *rv;
2490
2491 rv = vec_dup (s);
2492
2493 for (i = vec_len (rv) - 1; i >= 0; i--)
2494 {
2495 if (rv[i] == '_')
2496 {
2497 vec_delete (rv, i + 1, 0);
2498 break;
2499 }
2500 }
2501 return rv;
2502}
2503
2504typedef struct
2505{
2506 u8 *name_and_crc;
2507 u8 *name;
2508 u8 *crc;
2509 u32 msg_index;
2510 int which;
2511} msg_table_unserialize_t;
2512
2513static int
2514table_id_cmp (void *a1, void *a2)
2515{
2516 msg_table_unserialize_t *n1 = a1;
2517 msg_table_unserialize_t *n2 = a2;
2518
2519 return (n1->msg_index - n2->msg_index);
2520}
2521
2522static int
2523table_name_and_crc_cmp (void *a1, void *a2)
2524{
2525 msg_table_unserialize_t *n1 = a1;
2526 msg_table_unserialize_t *n2 = a2;
2527
2528 return strcmp ((char *) n1->name_and_crc, (char *) n2->name_and_crc);
2529}
2530
2531static clib_error_t *
2532dump_api_table_file_command_fn (vlib_main_t * vm,
2533 unformat_input_t * input,
2534 vlib_cli_command_t * cmd)
2535{
2536 u8 *filename = 0;
2537 api_main_t *am = &api_main;
2538 serialize_main_t _sm, *sm = &_sm;
2539 clib_error_t *error;
2540 u32 nmsgs;
2541 u32 msg_index;
2542 u8 *name_and_crc;
2543 int compare_current = 0;
2544 int numeric_sort = 0;
2545 msg_table_unserialize_t *table = 0, *item;
2546 u32 i;
2547 u32 ndifferences = 0;
2548
2549 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2550 {
2551 if (unformat (input, "file %s", &filename))
2552 ;
2553 else if (unformat (input, "compare-current")
2554 || unformat (input, "compare"))
2555 compare_current = 1;
2556 else if (unformat (input, "numeric"))
2557 numeric_sort = 1;
2558 else
2559 return clib_error_return (0, "unknown input `%U'",
2560 format_unformat_error, input);
2561 }
2562
2563 if (numeric_sort && compare_current)
2564 return clib_error_return
2565 (0, "Comparison and numeric sorting are incompatible");
2566
2567 if (filename == 0)
2568 return clib_error_return (0, "File not specified");
2569
2570 /* Load the serialized message table from the table dump */
2571
Dave Barach59b25652017-09-10 15:04:27 -04002572 error = unserialize_open_clib_file (sm, (char *) filename);
Dave Barach49fe0462017-09-12 17:06:56 -04002573
2574 if (error)
2575 return error;
2576
2577 unserialize_integer (sm, &nmsgs, sizeof (u32));
2578
2579 for (i = 0; i < nmsgs; i++)
2580 {
2581 msg_index = unserialize_likely_small_unsigned_integer (sm);
2582 unserialize_cstring (sm, (char **) &name_and_crc);
2583 vec_add2 (table, item, 1);
2584 item->msg_index = msg_index;
2585 item->name_and_crc = name_and_crc;
2586 item->name = extract_name (name_and_crc);
2587 item->crc = extract_crc (name_and_crc);
2588 item->which = 0; /* file */
2589 }
2590 serialize_close (sm);
2591
2592 /* Compare with the current image? */
2593 if (compare_current)
2594 {
2595 /* Append the current message table */
Dave Barach59b25652017-09-10 15:04:27 -04002596 u8 *tblv = vl_api_serialize_message_table (am, 0);
Dave Barach49fe0462017-09-12 17:06:56 -04002597
2598 serialize_open_vector (sm, tblv);
2599 unserialize_integer (sm, &nmsgs, sizeof (u32));
2600
2601 for (i = 0; i < nmsgs; i++)
2602 {
2603 msg_index = unserialize_likely_small_unsigned_integer (sm);
2604 unserialize_cstring (sm, (char **) &name_and_crc);
2605
2606 vec_add2 (table, item, 1);
2607 item->msg_index = msg_index;
2608 item->name_and_crc = name_and_crc;
2609 item->name = extract_name (name_and_crc);
2610 item->crc = extract_crc (name_and_crc);
2611 item->which = 1; /* current_image */
2612 }
Dave Barach59b25652017-09-10 15:04:27 -04002613 vec_free (tblv);
Dave Barach49fe0462017-09-12 17:06:56 -04002614 }
2615
2616 /* Sort the table. */
2617 if (numeric_sort)
2618 vec_sort_with_function (table, table_id_cmp);
2619 else
2620 vec_sort_with_function (table, table_name_and_crc_cmp);
2621
2622 if (compare_current)
2623 {
2624 ndifferences = 0;
2625
2626 /*
2627 * In this case, the recovered table will have two entries per
2628 * API message. So, if entries i and i+1 match, the message definitions
2629 * are identical. Otherwise, the crc is different, or a message is
2630 * present in only one of the tables.
2631 */
2632 vlib_cli_output (vm, "%=60s %s", "Message Name", "Result");
2633
2634 for (i = 0; i < vec_len (table);)
2635 {
2636 /* Last message lonely? */
2637 if (i == vec_len (table) - 1)
2638 {
2639 ndifferences++;
2640 goto last_unique;
2641 }
2642
2643 /* Identical pair? */
2644 if (!strncmp
2645 ((char *) table[i].name_and_crc,
2646 (char *) table[i + 1].name_and_crc,
2647 vec_len (table[i].name_and_crc)))
2648 {
2649 i += 2;
2650 continue;
2651 }
2652
2653 ndifferences++;
2654
2655 /* Only in one of two tables? */
2656 if (strncmp ((char *) table[i].name, (char *) table[i + 1].name,
2657 vec_len (table[i].name)))
2658 {
2659 last_unique:
2660 vlib_cli_output (vm, "%-60s only in %s",
2661 table[i].name, table[i].which ?
2662 "image" : "file");
2663 i++;
2664 continue;
2665 }
2666 /* In both tables, but with different signatures */
2667 vlib_cli_output (vm, "%-60s definition changed", table[i].name);
2668 i += 2;
2669 }
2670 if (ndifferences == 0)
2671 vlib_cli_output (vm, "No api message signature differences found.");
2672 else
2673 vlib_cli_output (vm, "Found %u api message signature differences",
2674 ndifferences);
2675 goto cleanup;
2676 }
2677
2678 /* Dump the table, sorted as shown above */
2679 vlib_cli_output (vm, "%=60s %=8s %=10s", "Message name", "MsgID", "CRC");
2680
2681 for (i = 0; i < vec_len (table); i++)
2682 {
2683 item = table + i;
2684 vlib_cli_output (vm, "%-60s %8u %10s", item->name,
2685 item->msg_index, item->crc);
2686 }
2687
2688cleanup:
2689 for (i = 0; i < vec_len (table); i++)
2690 {
2691 vec_free (table[i].name_and_crc);
2692 vec_free (table[i].name);
2693 vec_free (table[i].crc);
2694 }
2695
2696 vec_free (table);
2697
2698 return 0;
2699}
2700
2701/*?
2702 * Displays a serialized API message decode table, sorted by message name
2703 *
2704 * @cliexpar
2705 * @cliexstart{show api dump file <filename>}
2706 * Message name MsgID CRC
2707 * accept_session 407 8e2a127e
2708 * accept_session_reply 408 67d8c22a
2709 * add_node_next 549 e4202993
2710 * add_node_next_reply 550 e89d6eed
2711 * etc.
2712 * @cliexend
2713?*/
2714
2715/*?
2716 * Compares a serialized API message decode table with the current image
2717 *
2718 * @cliexpar
2719 * @cliexstart{show api dump file <filename> compare}
2720 * ip_add_del_route definition changed
2721 * ip_table_add_del definition changed
2722 * l2_macs_event only in image
2723 * vnet_ip4_fib_counters only in file
2724 * vnet_ip4_nbr_counters only in file
2725 * @cliexend
2726?*/
2727
2728/*?
Dave Barach905c14a2017-09-25 08:47:59 -04002729 * Display a serialized API message decode table, compare a saved
2730 * decode table with the current image, to establish API differences.
2731 *
Dave Barach49fe0462017-09-12 17:06:56 -04002732?*/
2733/* *INDENT-OFF* */
2734VLIB_CLI_COMMAND (dump_api_table_file, static) =
2735{
2736 .path = "show api dump",
2737 .short_help = "show api dump file <filename> [numeric | compare-current]",
2738 .function = dump_api_table_file_command_fn,
2739};
2740/* *INDENT-ON* */
2741
Dave Barach371e4e12016-07-08 09:38:52 -04002742/*
2743 * fd.io coding-style-patch-verification: ON
2744 *
2745 * Local Variables:
2746 * eval: (c-set-style "gnu")
2747 * End:
2748 */