blob: 77959e6d36684d0aca6bc5b452e5f4bc367e08ba [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 Barach371e4e12016-07-08 09:38:52 -040099void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
100 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Dave Barach371e4e12016-07-08 09:38:52 -0400102void
103vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104{
Dave Barach371e4e12016-07-08 09:38:52 -0400105 static int count;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Dave Barach371e4e12016-07-08 09:38:52 -0400107 if (count++ < 5)
108 clib_warning ("need to link against -lvlibsocket, msg not sent!");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109}
110
Dave Barach371e4e12016-07-08 09:38:52 -0400111void
112vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113{
Dave Barach371e4e12016-07-08 09:38:52 -0400114 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
115 {
116 vl_socket_api_send (rp, elem);
117 }
118 else
119 {
120 vl_msg_api_send_shmem (rp->vl_input_queue, elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 }
122}
123
Dave Barach557d1282016-11-10 14:22:49 -0500124u8 *
125vl_api_serialize_message_table (api_main_t * am, u8 * vector)
Dave Barach371e4e12016-07-08 09:38:52 -0400126{
Dave Barach557d1282016-11-10 14:22:49 -0500127 serialize_main_t _sm, *sm = &_sm;
128 hash_pair_t *hp;
129 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
130
131 serialize_open_vector (sm, vector);
132
133 /* serialize the count */
134 serialize_integer (sm, nmsg, sizeof (u32));
135
Dave Barach987e11d2017-02-27 13:10:27 -0500136 /* *INDENT-OFF* */
137 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
138 ({
139 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
140 serialize_cstring (sm, (char *) hp->key);
141 }));
142 /* *INDENT-ON* */
Dave Barach557d1282016-11-10 14:22:49 -0500143
144 return serialize_close_vector (sm);
Dave Barach371e4e12016-07-08 09:38:52 -0400145}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147/*
Dave Barach52851e62017-08-07 09:35:25 -0400148 * vl_api_memclnt_create_internal
149 */
150
151u32
152vl_api_memclnt_create_internal (char *name, unix_shared_memory_queue_t * q)
153{
154 vl_api_registration_t **regpp;
155 vl_api_registration_t *regp;
156 svm_region_t *svm;
157 void *oldheap;
158 api_main_t *am = &api_main;
159
160 ASSERT (vlib_get_thread_index () == 0);
161 pool_get (am->vl_clients, regpp);
162
163 svm = am->vlib_rp;
164
165 pthread_mutex_lock (&svm->mutex);
166 oldheap = svm_push_data_heap (svm);
167 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
168
169 regp = *regpp;
170 memset (regp, 0, sizeof (*regp));
171 regp->registration_type = REGISTRATION_TYPE_SHMEM;
172 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
173
174 regp->vl_input_queue = q;
175 regp->name = format (0, "%s%c", name, 0);
176
177 pthread_mutex_unlock (&svm->mutex);
178 svm_pop_heap (oldheap);
179 return vl_msg_api_handle_from_index_and_epoch
180 (regp->vl_api_registration_pool_index,
181 am->shmem_hdr->application_restarts);
182}
183
184
185/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 * vl_api_memclnt_create_t_handler
187 */
Dave Barach371e4e12016-07-08 09:38:52 -0400188void
189vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Dave Barach371e4e12016-07-08 09:38:52 -0400191 vl_api_registration_t **regpp;
192 vl_api_registration_t *regp;
193 vl_api_memclnt_create_reply_t *rp;
194 svm_region_t *svm;
195 unix_shared_memory_queue_t *q;
Dave Barach557d1282016-11-10 14:22:49 -0500196 int rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400197 void *oldheap;
198 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Dave Barach371e4e12016-07-08 09:38:52 -0400200 /*
201 * This is tortured. Maintain a vlib-address-space private
202 * pool of client registrations. We use the shared-memory virtual
203 * address of client structure as a handle, to allow direct
204 * manipulation of context quota vbls from the client library.
205 *
206 * This scheme causes trouble w/ API message trace replay, since
207 * some random VA from clib_mem_alloc() certainly won't
208 * occur in the Linux sim. The (very) few places
209 * that care need to use the pool index.
210 *
211 * Putting the registration object(s) into a pool in shared memory and
212 * using the pool index as a handle seems like a great idea.
213 * Unfortunately, each and every reference to that pool would need
214 * to be protected by a mutex:
215 *
216 * Client VLIB
217 * ------ ----
218 * convert pool index to
219 * pointer.
220 * <deschedule>
221 * expand pool
222 * <deschedule>
223 * kaboom!
224 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Dave Barach371e4e12016-07-08 09:38:52 -0400226 pool_get (am->vl_clients, regpp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Dave Barach371e4e12016-07-08 09:38:52 -0400228 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Dave Barach371e4e12016-07-08 09:38:52 -0400230 pthread_mutex_lock (&svm->mutex);
231 oldheap = svm_push_data_heap (svm);
232 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Dave Barach371e4e12016-07-08 09:38:52 -0400234 regp = *regpp;
235 memset (regp, 0, sizeof (*regp));
236 regp->registration_type = REGISTRATION_TYPE_SHMEM;
237 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barach371e4e12016-07-08 09:38:52 -0400239 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
240 mp->input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Dave Barach371e4e12016-07-08 09:38:52 -0400242 regp->name = format (0, "%s", mp->name);
243 vec_add1 (regp->name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
Dave Barach371e4e12016-07-08 09:38:52 -0400245 pthread_mutex_unlock (&svm->mutex);
246 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Dave Barach49fe0462017-09-12 17:06:56 -0400248 ASSERT (am->serialized_message_table_in_shmem);
Dave Barach557d1282016-11-10 14:22:49 -0500249
Dave Barach371e4e12016-07-08 09:38:52 -0400250 rp = vl_msg_api_alloc (sizeof (*rp));
251 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
252 rp->handle = (uword) regp;
253 rp->index = vl_msg_api_handle_from_index_and_epoch
254 (regp->vl_api_registration_pool_index,
255 am->shmem_hdr->application_restarts);
256 rp->context = mp->context;
257 rp->response = ntohl (rv);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200258 rp->message_table =
259 pointer_to_uword (am->serialized_message_table_in_shmem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
Dave Barach371e4e12016-07-08 09:38:52 -0400261 vl_msg_api_send_shmem (q, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262}
263
Dave Barachb64e4e22017-03-14 09:10:56 -0400264static int
265call_reaper_functions (u32 client_index)
Dave Barach371e4e12016-07-08 09:38:52 -0400266{
Dave Barachb64e4e22017-03-14 09:10:56 -0400267 clib_error_t *error = 0;
268 _vl_msg_api_function_list_elt_t *i;
269
270 i = api_main.reaper_function_registrations;
271 while (i)
272 {
273 error = i->f (client_index);
274 if (error)
275 clib_error_report (error);
276 i = i->next_init_function;
277 }
Dave Barach371e4e12016-07-08 09:38:52 -0400278 return 0;
279}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281/*
282 * vl_api_memclnt_delete_t_handler
283 */
Dave Barach371e4e12016-07-08 09:38:52 -0400284void
285vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286{
Dave Barach371e4e12016-07-08 09:38:52 -0400287 vl_api_registration_t **regpp;
288 vl_api_registration_t *regp;
289 vl_api_memclnt_delete_reply_t *rp;
290 svm_region_t *svm;
291 void *oldheap;
292 api_main_t *am = &api_main;
293 u32 handle, client_index, epoch;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Dave Barach371e4e12016-07-08 09:38:52 -0400295 handle = mp->index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Dave Barachb64e4e22017-03-14 09:10:56 -0400297 if (call_reaper_functions (handle))
Dave Barach371e4e12016-07-08 09:38:52 -0400298 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
Dave Barach371e4e12016-07-08 09:38:52 -0400300 epoch = vl_msg_api_handle_get_epoch (handle);
301 client_index = vl_msg_api_handle_get_index (handle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
Dave Barach371e4e12016-07-08 09:38:52 -0400303 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
304 {
305 clib_warning
306 ("Stale clnt delete index %d old epoch %d cur epoch %d",
307 client_index, epoch,
308 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
309 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 }
311
Dave Barach371e4e12016-07-08 09:38:52 -0400312 regpp = am->vl_clients + client_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Dave Barach371e4e12016-07-08 09:38:52 -0400314 if (!pool_is_free (am->vl_clients, regpp))
315 {
316 regp = *regpp;
317 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Dave Barach371e4e12016-07-08 09:38:52 -0400319 /* $$$ check the input queue for e.g. punted sf's */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Dave Barach371e4e12016-07-08 09:38:52 -0400321 rp = vl_msg_api_alloc (sizeof (*rp));
322 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
323 rp->handle = mp->handle;
324 rp->response = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
Dave Barach371e4e12016-07-08 09:38:52 -0400326 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Dave Barach371e4e12016-07-08 09:38:52 -0400328 if (client_index != regp->vl_api_registration_pool_index)
329 {
330 clib_warning ("mismatch client_index %d pool_index %d",
331 client_index, regp->vl_api_registration_pool_index);
332 vl_msg_api_free (rp);
333 return;
334 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Dave Barach371e4e12016-07-08 09:38:52 -0400336 /* No dangling references, please */
337 *regpp = 0;
338
339 pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
340
341 pthread_mutex_lock (&svm->mutex);
342 oldheap = svm_push_data_heap (svm);
343 /* Poison the old registration */
344 memset (regp, 0xF1, sizeof (*regp));
345 clib_mem_free (regp);
346 pthread_mutex_unlock (&svm->mutex);
347 svm_pop_heap (oldheap);
348 }
349 else
350 {
351 clib_warning ("unknown client ID %d", mp->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 }
353}
354
Dave Barach371e4e12016-07-08 09:38:52 -0400355void
356vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357{
Dave Barach371e4e12016-07-08 09:38:52 -0400358 vl_api_get_first_msg_id_reply_t *rmp;
359 unix_shared_memory_queue_t *q;
360 uword *p;
361 api_main_t *am = &api_main;
362 vl_api_msg_range_t *rp;
363 u8 name[64];
364 u16 first_msg_id = ~0;
365 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366
Dave Barach371e4e12016-07-08 09:38:52 -0400367 q = vl_api_client_index_to_input_queue (mp->client_index);
368 if (!q)
369 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach371e4e12016-07-08 09:38:52 -0400371 if (am->msg_range_by_name == 0)
372 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barach371e4e12016-07-08 09:38:52 -0400374 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Dave Barach371e4e12016-07-08 09:38:52 -0400376 p = hash_get_mem (am->msg_range_by_name, name);
377 if (p == 0)
378 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
Dave Barach371e4e12016-07-08 09:38:52 -0400380 rp = vec_elt_at_index (am->msg_ranges, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Dave Barach371e4e12016-07-08 09:38:52 -0400382 first_msg_id = rp->first_msg_id;
383 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384
385out:
386
Dave Barach371e4e12016-07-08 09:38:52 -0400387 rmp = vl_msg_api_alloc (sizeof (*rmp));
388 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
389 rmp->context = mp->context;
390 rmp->retval = ntohl (rv);
391 rmp->first_msg_id = ntohs (first_msg_id);
392 vl_msg_api_send_shmem (q, (u8 *) & rmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393}
394
395#define foreach_vlib_api_msg \
396_(MEMCLNT_CREATE, memclnt_create) \
397_(MEMCLNT_DELETE, memclnt_delete) \
398_(GET_FIRST_MSG_ID, get_first_msg_id)
399
400/*
401 * vl_api_init
402 */
Dave Barach371e4e12016-07-08 09:38:52 -0400403static int
Neale Rannse72be392017-04-26 13:59:20 -0700404memory_api_init (const char *region_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
Dave Barach371e4e12016-07-08 09:38:52 -0400406 int rv;
407 vl_msg_api_msg_config_t cfg;
408 vl_msg_api_msg_config_t *c = &cfg;
409
Dave Barach0691d6e2017-01-05 10:08:52 -0500410 memset (c, 0, sizeof (*c));
411
Dave Barach371e4e12016-07-08 09:38:52 -0400412 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
413 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
415#define _(N,n) do { \
416 c->id = VL_API_##N; \
417 c->name = #n; \
418 c->handler = vl_api_##n##_t_handler; \
419 c->cleanup = vl_noop_handler; \
420 c->endian = vl_api_##n##_t_endian; \
421 c->print = vl_api_##n##_t_print; \
422 c->size = sizeof(vl_api_##n##_t); \
423 c->traced = 1; /* trace, so these msgs print */ \
424 c->replay = 0; /* don't replay client create/delete msgs */ \
Dave Barach0691d6e2017-01-05 10:08:52 -0500425 c->message_bounce = 0; /* don't bounce this message */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 vl_msg_api_config(c);} while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400427
428 foreach_vlib_api_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429#undef _
430
Dave Barach371e4e12016-07-08 09:38:52 -0400431 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432}
433
434#define foreach_histogram_bucket \
435_(400) \
436_(200) \
437_(100) \
438_(10)
439
Dave Barach371e4e12016-07-08 09:38:52 -0400440typedef enum
441{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442#define _(n) SLEEP_##n##_US,
Dave Barach371e4e12016-07-08 09:38:52 -0400443 foreach_histogram_bucket
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444#undef _
445 SLEEP_N_BUCKETS,
446} histogram_index_t;
447
448static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
449
Dave Barach371e4e12016-07-08 09:38:52 -0400450static void memclnt_queue_callback (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
Dave Barach987e11d2017-02-27 13:10:27 -0500452/*
453 * Callback to send ourselves a plugin numbering-space trace msg
454 */
455static void
456send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
457{
458 vl_api_trace_plugin_msg_ids_t *mp;
459 api_main_t *am = &api_main;
460 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
461 unix_shared_memory_queue_t *q;
462
463 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
464 memset (mp, 0, sizeof (*mp));
465
466 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
467 strncpy ((char *) mp->plugin_name, (char *) name,
468 sizeof (mp->plugin_name) - 1);
469 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
470 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
471
472 q = shmem_hdr->vl_input_queue;
473
474 vl_msg_api_send_shmem (q, (u8 *) & mp);
475}
476
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477static uword
478memclnt_process (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400479 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480{
Dave Barach371e4e12016-07-08 09:38:52 -0400481 uword mp;
482 vl_shmem_hdr_t *shm;
483 unix_shared_memory_queue_t *q;
484 clib_error_t *e;
485 int rv;
486 api_main_t *am = &api_main;
487 f64 dead_client_scan_time;
488 f64 sleep_time, start_time;
489 f64 vector_rate;
Dave Barach987e11d2017-02-27 13:10:27 -0500490 int i;
Dave Barach49fe0462017-09-12 17:06:56 -0400491 u8 *serialized_message_table = 0;
492 svm_region_t *svm;
493 void *oldheap;
Dave Barach371e4e12016-07-08 09:38:52 -0400494
495 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
496
497 if ((rv = memory_api_init (am->region_name)) < 0)
498 {
499 clib_warning ("memory_api_init returned %d, wait for godot...", rv);
500 vlib_process_suspend (vm, 1e70);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 }
502
Dave Barach371e4e12016-07-08 09:38:52 -0400503 shm = am->shmem_hdr;
504 ASSERT (shm);
505 q = shm->vl_input_queue;
506 ASSERT (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
Dave Barach371e4e12016-07-08 09:38:52 -0400508 e = vlib_call_init_exit_functions
509 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
510 if (e)
511 clib_error_report (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Dave Barach371e4e12016-07-08 09:38:52 -0400513 sleep_time = 20.0;
514 dead_client_scan_time = vlib_time_now (vm) + 20.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
Dave Barach987e11d2017-02-27 13:10:27 -0500516 /*
517 * Send plugin message range messages for each plugin we loaded
518 */
519 for (i = 0; i < vec_len (am->msg_ranges); i++)
520 {
521 vl_api_msg_range_t *rp = am->msg_ranges + i;
522 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
523 rp->last_msg_id);
524 }
525
Dave Barach49fe0462017-09-12 17:06:56 -0400526 /*
527 * Snapshoot the api message table.
528 */
529 serialized_message_table = vl_api_serialize_message_table (am, 0);
530
531 svm = am->vlib_rp;
532 pthread_mutex_lock (&svm->mutex);
533 oldheap = svm_push_data_heap (svm);
534
535 am->serialized_message_table_in_shmem = vec_dup (serialized_message_table);
536
537 pthread_mutex_unlock (&svm->mutex);
538 svm_pop_heap (oldheap);
539
540 /*
541 * Save the api message table snapshot, if configured
542 */
543 if (am->save_msg_table_filename)
544 {
545 int fd, rv;
546 u8 *chroot_file;
547 if (strstr ((char *) am->save_msg_table_filename, "..")
548 || index ((char *) am->save_msg_table_filename, '/'))
549 {
550 clib_warning ("illegal save-message-table filename '%s'",
551 am->save_msg_table_filename);
552 goto skip_save;
553 }
554
555 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
556
557 fd = creat ((char *) chroot_file, 0644);
558
559 if (fd < 0)
560 {
561 clib_unix_warning ("creat");
562 goto skip_save;
563 }
564 rv = write (fd, serialized_message_table,
565 vec_len (serialized_message_table));
566
567 if (rv != vec_len (serialized_message_table))
568 clib_unix_warning ("write");
569
570 rv = close (fd);
571 if (rv < 0)
572 clib_unix_warning ("close");
573
574 vec_free (chroot_file);
575 }
576
577skip_save:
578 vec_free (serialized_message_table);
579
Dave Barach371e4e12016-07-08 09:38:52 -0400580 /* $$$ pay attention to frame size, control CPU usage */
581 while (1)
582 {
583 uword event_type __attribute__ ((unused));
584 i8 *headp;
585 int need_broadcast;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Dave Barach371e4e12016-07-08 09:38:52 -0400587 /*
588 * There's a reason for checking the queue before
589 * sleeping. If the vlib application crashes, it's entirely
590 * possible for a client to enqueue a connect request
591 * during the process restart interval.
592 *
593 * Unless some force of physics causes the new incarnation
594 * of the application to process the request, the client will
595 * sit and wait for Godot...
596 */
597 vector_rate = vlib_last_vector_length_per_node (vm);
598 start_time = vlib_time_now (vm);
599 while (1)
600 {
601 pthread_mutex_lock (&q->mutex);
602 if (q->cursize == 0)
603 {
604 vm->api_queue_nonempty = 0;
605 pthread_mutex_unlock (&q->mutex);
606
607 if (TRACE_VLIB_MEMORY_QUEUE)
608 {
609 /* *INDENT-OFF* */
610 ELOG_TYPE_DECLARE (e) =
611 {
612 .format = "q-underflow: len %d",
613 .format_args = "i4",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614 };
Dave Barach371e4e12016-07-08 09:38:52 -0400615 /* *INDENT-ON* */
616 struct
617 {
618 u32 len;
619 } *ed;
620 ed = ELOG_DATA (&vm->elog_main, e);
621 ed->len = 0;
622 }
623 sleep_time = 20.0;
624 break;
625 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Dave Barach371e4e12016-07-08 09:38:52 -0400627 headp = (i8 *) (q->data + sizeof (uword) * q->head);
628 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
Dave Barach371e4e12016-07-08 09:38:52 -0400630 q->head++;
631 need_broadcast = (q->cursize == q->maxsize / 2);
632 q->cursize--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633
Dave Barach371e4e12016-07-08 09:38:52 -0400634 if (PREDICT_FALSE (q->head == q->maxsize))
635 q->head = 0;
636 pthread_mutex_unlock (&q->mutex);
637 if (need_broadcast)
638 (void) pthread_cond_broadcast (&q->condvar);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
Dave Barach371e4e12016-07-08 09:38:52 -0400640 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641
Dave Barach371e4e12016-07-08 09:38:52 -0400642 /* Allow no more than 10us without a pause */
643 if (vlib_time_now (vm) > start_time + 10e-6)
644 {
645 int index = SLEEP_400_US;
646 if (vector_rate > 40.0)
647 sleep_time = 400e-6;
648 else if (vector_rate > 20.0)
649 {
650 index = SLEEP_200_US;
651 sleep_time = 200e-6;
652 }
653 else if (vector_rate >= 1.0)
654 {
655 index = SLEEP_100_US;
656 sleep_time = 100e-6;
657 }
658 else
659 {
660 index = SLEEP_10_US;
661 sleep_time = 10e-6;
662 }
663 vector_rate_histogram[index] += 1;
664 break;
665 }
666 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Dave Barach371e4e12016-07-08 09:38:52 -0400668 event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time);
669 vm->queue_signal_pending = 0;
670 vlib_process_get_events (vm, 0 /* event_data */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Dave Barach371e4e12016-07-08 09:38:52 -0400672 if (vlib_time_now (vm) > dead_client_scan_time)
673 {
674 vl_api_registration_t **regpp;
675 vl_api_registration_t *regp;
676 unix_shared_memory_queue_t *q;
677 static u32 *dead_indices;
678 static u32 *confused_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
Dave Barach371e4e12016-07-08 09:38:52 -0400680 vec_reset_length (dead_indices);
681 vec_reset_length (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Dave Barach371e4e12016-07-08 09:38:52 -0400683 /* *INDENT-OFF* */
684 pool_foreach (regpp, am->vl_clients,
685 ({
686 regp = *regpp;
687 if (regp)
688 {
689 q = regp->vl_input_queue;
690 if (kill (q->consumer_pid, 0) < 0)
691 {
692 vec_add1(dead_indices, regpp - am->vl_clients);
693 }
694 }
695 else
696 {
697 clib_warning ("NULL client registration index %d",
698 regpp - am->vl_clients);
699 vec_add1 (confused_indices, regpp - am->vl_clients);
700 }
701 }));
702 /* *INDENT-ON* */
703 /* This should "never happen," but if it does, fix it... */
704 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
705 {
706 int i;
707 for (i = 0; i < vec_len (confused_indices); i++)
708 {
709 pool_put_index (am->vl_clients, confused_indices[i]);
710 }
711 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712
Dave Barach371e4e12016-07-08 09:38:52 -0400713 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
714 {
715 int i;
716 svm_region_t *svm;
717 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
Dave Barach371e4e12016-07-08 09:38:52 -0400719 /* Allow the application to clean up its registrations */
720 for (i = 0; i < vec_len (dead_indices); i++)
721 {
722 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
723 if (regpp)
724 {
725 u32 handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Dave Barach371e4e12016-07-08 09:38:52 -0400727 handle = vl_msg_api_handle_from_index_and_epoch
728 (dead_indices[i], shm->application_restarts);
Dave Barachb64e4e22017-03-14 09:10:56 -0400729 (void) call_reaper_functions (handle);
Dave Barach371e4e12016-07-08 09:38:52 -0400730 }
731 }
732
733 svm = am->vlib_rp;
734 pthread_mutex_lock (&svm->mutex);
735 oldheap = svm_push_data_heap (svm);
736
737 for (i = 0; i < vec_len (dead_indices); i++)
738 {
739 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
740 if (regpp)
741 {
742 /* Poison the old registration */
743 memset (*regpp, 0xF3, sizeof (**regpp));
744 clib_mem_free (*regpp);
745 /* no dangling references, please */
746 *regpp = 0;
747 }
748 else
749 {
750 svm_pop_heap (oldheap);
751 clib_warning ("Duplicate free, client index %d",
752 regpp - am->vl_clients);
753 oldheap = svm_push_data_heap (svm);
754 }
755 }
756
757 svm_client_scan_this_region_nolock (am->vlib_rp);
758
759 pthread_mutex_unlock (&svm->mutex);
760 svm_pop_heap (oldheap);
761 for (i = 0; i < vec_len (dead_indices); i++)
762 pool_put_index (am->vl_clients, dead_indices[i]);
763 }
764
765 dead_client_scan_time = vlib_time_now (vm) + 20.0;
766 }
767
768 if (TRACE_VLIB_MEMORY_QUEUE)
769 {
770 /* *INDENT-OFF* */
771 ELOG_TYPE_DECLARE (e) = {
772 .format = "q-awake: len %d",
773 .format_args = "i4",
774 };
775 /* *INDENT-ON* */
776 struct
777 {
778 u32 len;
779 } *ed;
780 ed = ELOG_DATA (&vm->elog_main, e);
781 ed->len = q->cursize;
782 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 }
784
Dave Barach371e4e12016-07-08 09:38:52 -0400785 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786}
Dave Barach49fe0462017-09-12 17:06:56 -0400787/* *INDENT-OFF* */
788VLIB_REGISTER_NODE (memclnt_node,static) = {
789 .function = memclnt_process,
790 .type = VLIB_NODE_TYPE_PROCESS,
791 .name = "api-rx-from-ring",
792 .state = VLIB_NODE_STATE_DISABLED,
793};
794/* *INDENT-ON* */
795
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
797static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400798vl_api_show_histogram_command (vlib_main_t * vm,
799 unformat_input_t * input,
800 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801{
Dave Barach371e4e12016-07-08 09:38:52 -0400802 u64 total_counts = 0;
803 int i;
804
805 for (i = 0; i < SLEEP_N_BUCKETS; i++)
806 {
807 total_counts += vector_rate_histogram[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 }
809
Dave Barach371e4e12016-07-08 09:38:52 -0400810 if (total_counts == 0)
811 {
812 vlib_cli_output (vm, "No control-plane activity.");
813 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814 }
815
816#define _(n) \
817 do { \
818 f64 percent; \
819 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
820 / (f64) total_counts; \
821 percent *= 100.0; \
822 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
823 vector_rate_histogram[SLEEP_##n##_US], \
824 percent); \
825 } while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400826 foreach_histogram_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827#undef _
828
Dave Barach371e4e12016-07-08 09:38:52 -0400829 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830}
831
Dave Barach49fe0462017-09-12 17:06:56 -0400832/*?
833 * Display the binary api sleep-time histogram
834?*/
Dave Barach371e4e12016-07-08 09:38:52 -0400835/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -0400836VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) =
837{
838 .path = "show api histogram",
839 .short_help = "show api histogram",
840 .function = vl_api_show_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841};
Dave Barach371e4e12016-07-08 09:38:52 -0400842/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843
844static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400845vl_api_clear_histogram_command (vlib_main_t * vm,
846 unformat_input_t * input,
847 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848{
Dave Barach371e4e12016-07-08 09:38:52 -0400849 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850
Dave Barach371e4e12016-07-08 09:38:52 -0400851 for (i = 0; i < SLEEP_N_BUCKETS; i++)
852 vector_rate_histogram[i] = 0;
853 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854}
855
Dave Barach49fe0462017-09-12 17:06:56 -0400856/*?
857 * Clear the binary api sleep-time histogram
858?*/
Dave Barach371e4e12016-07-08 09:38:52 -0400859/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -0400860VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) =
861{
862 .path = "clear api histogram",
863 .short_help = "clear api histogram",
864 .function = vl_api_clear_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865};
Dave Barach371e4e12016-07-08 09:38:52 -0400866/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Dave Barach371e4e12016-07-08 09:38:52 -0400868static void
869memclnt_queue_callback (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870{
Dave Barach371e4e12016-07-08 09:38:52 -0400871 static volatile int *cursizep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872
Dave Barach16c75df2016-05-31 14:05:46 -0400873 if (PREDICT_FALSE (cursizep == 0))
874 {
875 api_main_t *am = &api_main;
876 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barach371e4e12016-07-08 09:38:52 -0400877 unix_shared_memory_queue_t *q;
878
Dave Barach16c75df2016-05-31 14:05:46 -0400879 if (shmem_hdr == 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400880 return;
881
Dave Barach16c75df2016-05-31 14:05:46 -0400882 q = shmem_hdr->vl_input_queue;
883 if (q == 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400884 return;
Dave Barach16c75df2016-05-31 14:05:46 -0400885 cursizep = &q->cursize;
886 }
Dave Barach371e4e12016-07-08 09:38:52 -0400887
Dave Barach16c75df2016-05-31 14:05:46 -0400888 if (*cursizep >= 1)
889 {
890 vm->queue_signal_pending = 1;
891 vm->api_queue_nonempty = 1;
Dave Barach371e4e12016-07-08 09:38:52 -0400892 vlib_process_signal_event (vm, memclnt_node.index,
893 /* event_type */ 0, /* event_data */ 0);
Dave Barach16c75df2016-05-31 14:05:46 -0400894 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895}
896
Dave Barach371e4e12016-07-08 09:38:52 -0400897void
898vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899{
Dave Barach371e4e12016-07-08 09:38:52 -0400900 vlib_node_set_state (vm, memclnt_node.index,
901 (enable
902 ? VLIB_NODE_STATE_POLLING
903 : VLIB_NODE_STATE_DISABLED));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904}
905
906static uword
907api_rx_from_node (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400908 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909{
Dave Barach371e4e12016-07-08 09:38:52 -0400910 uword n_packets = frame->n_vectors;
911 uword n_left_from;
912 u32 *from;
913 static u8 *long_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Dave Barach371e4e12016-07-08 09:38:52 -0400915 vec_validate (long_msg, 4095);
916 n_left_from = frame->n_vectors;
917 from = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918
Dave Barach371e4e12016-07-08 09:38:52 -0400919 while (n_left_from > 0)
920 {
921 u32 bi0;
922 vlib_buffer_t *b0;
923 void *msg;
924 uword msg_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925
Dave Barach371e4e12016-07-08 09:38:52 -0400926 bi0 = from[0];
927 b0 = vlib_get_buffer (vm, bi0);
928 from += 1;
929 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930
Dave Barach371e4e12016-07-08 09:38:52 -0400931 msg = b0->data + b0->current_data;
932 msg_len = b0->current_length;
933 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
934 {
935 ASSERT (long_msg != 0);
936 _vec_len (long_msg) = 0;
937 vec_add (long_msg, msg, msg_len);
938 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
939 {
940 b0 = vlib_get_buffer (vm, b0->next_buffer);
941 msg = b0->data + b0->current_data;
942 msg_len = b0->current_length;
943 vec_add (long_msg, msg, msg_len);
944 }
945 msg = long_msg;
946 }
947 vl_msg_api_handler_no_trace_no_free (msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 }
949
Dave Barach371e4e12016-07-08 09:38:52 -0400950 /* Free what we've been given. */
951 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952
Dave Barach371e4e12016-07-08 09:38:52 -0400953 return n_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954}
955
Dave Barach371e4e12016-07-08 09:38:52 -0400956/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
958 .function = api_rx_from_node,
959 .type = VLIB_NODE_TYPE_INTERNAL,
960 .vector_size = 4,
961 .name = "api-rx-from-node",
962};
Dave Barach371e4e12016-07-08 09:38:52 -0400963/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964
965static clib_error_t *
966setup_memclnt_exit (vlib_main_t * vm)
967{
Dave Barach371e4e12016-07-08 09:38:52 -0400968 atexit (vl_unmap_shmem);
969 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970}
971
972VLIB_INIT_FUNCTION (setup_memclnt_exit);
973
974
975static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400976vl_api_ring_command (vlib_main_t * vm,
977 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978{
Dave Barach371e4e12016-07-08 09:38:52 -0400979 int i;
980 ring_alloc_t *ap;
981 vl_shmem_hdr_t *shmem_hdr;
982 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
Dave Barach371e4e12016-07-08 09:38:52 -0400984 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Dave Barach371e4e12016-07-08 09:38:52 -0400986 if (shmem_hdr == 0)
987 {
988 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
989 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 }
991
Dave Barach371e4e12016-07-08 09:38:52 -0400992 vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
993 "Owner", "Size", "Nitems", "Hits", "Misses");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
Dave Barach371e4e12016-07-08 09:38:52 -0400995 ap = shmem_hdr->vl_rings;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996
Dave Barach371e4e12016-07-08 09:38:52 -0400997 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
998 {
999 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
1000 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
1001 ap++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 }
1003
Dave Barach371e4e12016-07-08 09:38:52 -04001004 ap = shmem_hdr->client_rings;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005
Dave Barach371e4e12016-07-08 09:38:52 -04001006 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
1007 {
1008 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
1009 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
1010 ap++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 }
1012
Dave Barach371e4e12016-07-08 09:38:52 -04001013 vlib_cli_output (vm, "%d ring miss fallback allocations\n",
1014 am->ring_misses);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
Dave Barach842b9c52017-01-09 15:54:00 -05001016 vlib_cli_output
1017 (vm, "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
1018 shmem_hdr->application_restarts,
1019 shmem_hdr->restart_reclaims, shmem_hdr->garbage_collects);
Dave Barach371e4e12016-07-08 09:38:52 -04001020 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021}
1022
Dave Barach371e4e12016-07-08 09:38:52 -04001023void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
1024 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025
Dave Barach371e4e12016-07-08 09:38:52 -04001026void
1027dump_socket_clients (vlib_main_t * vm, api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028{
1029}
1030
1031static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001032vl_api_client_command (vlib_main_t * vm,
1033 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034{
Dave Barach371e4e12016-07-08 09:38:52 -04001035 vl_api_registration_t **regpp, *regp;
1036 unix_shared_memory_queue_t *q;
1037 char *health;
1038 api_main_t *am = &api_main;
1039 u32 *confused_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
Dave Barach371e4e12016-07-08 09:38:52 -04001041 if (!pool_elts (am->vl_clients))
1042 goto socket_clients;
1043 vlib_cli_output (vm, "Shared memory clients");
1044 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
1045 "Name", "PID", "Queue Length", "Queue VA", "Health");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
Dave Barach371e4e12016-07-08 09:38:52 -04001047 /* *INDENT-OFF* */
1048 pool_foreach (regpp, am->vl_clients,
1049 ({
1050 regp = *regpp;
1051
1052 if (regp)
1053 {
1054 q = regp->vl_input_queue;
1055 if (kill (q->consumer_pid, 0) < 0)
1056 {
1057 health = "DEAD";
1058 }
1059 else
1060 {
1061 health = "alive";
1062 }
1063 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
1064 regp->name, q->consumer_pid, q->cursize,
1065 q, health);
1066 }
1067 else
1068 {
1069 clib_warning ("NULL client registration index %d",
1070 regpp - am->vl_clients);
1071 vec_add1 (confused_indices, regpp - am->vl_clients);
1072 }
1073 }));
1074 /* *INDENT-ON* */
1075
1076 /* This should "never happen," but if it does, fix it... */
1077 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
1078 {
1079 int i;
1080 for (i = 0; i < vec_len (confused_indices); i++)
1081 {
1082 pool_put_index (am->vl_clients, confused_indices[i]);
1083 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 }
Dave Barach371e4e12016-07-08 09:38:52 -04001085 vec_free (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086
Dave Barach371e4e12016-07-08 09:38:52 -04001087 if (am->missing_clients)
1088 vlib_cli_output (vm, "%u messages with missing clients",
1089 am->missing_clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090socket_clients:
Dave Barach371e4e12016-07-08 09:38:52 -04001091 dump_socket_clients (vm, am);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
Dave Barach371e4e12016-07-08 09:38:52 -04001093 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094}
1095
Calvin16649372016-07-28 13:52:05 -04001096static clib_error_t *
1097vl_api_status_command (vlib_main_t * vm,
1098 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1099{
1100 api_main_t *am = &api_main;
1101
1102 // check if rx_trace and tx_trace are not null pointers
1103
1104 if (am->rx_trace == 0)
1105 {
1106 vlib_cli_output (vm, "RX Trace disabled\n");
1107 }
1108 else
1109 {
1110 if (am->rx_trace->enabled == 0)
1111 vlib_cli_output (vm, "RX Trace disabled\n");
1112 else
1113 vlib_cli_output (vm, "RX Trace enabled\n");
1114 }
1115
1116 if (am->tx_trace == 0)
1117 {
1118 vlib_cli_output (vm, "TX Trace disabled\n");
1119 }
1120 else
1121 {
1122 if (am->tx_trace->enabled == 0)
1123 vlib_cli_output (vm, "TX Trace disabled\n");
1124 else
1125 vlib_cli_output (vm, "TX Trace enabled\n");
1126 }
1127
1128 return 0;
1129}
1130
Dave Barach371e4e12016-07-08 09:38:52 -04001131/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001132VLIB_CLI_COMMAND (cli_show_api_command, static) =
1133{
1134 .path = "show api",
1135 .short_help = "Show API information",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136};
Dave Barach371e4e12016-07-08 09:38:52 -04001137/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138
Dave Barach49fe0462017-09-12 17:06:56 -04001139/*?
1140 * Display binary api message allocation ring statistics
1141?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001142/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001143VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
1144{
1145 .path = "show api ring-stats",
1146 .short_help = "Message ring statistics",
1147 .function = vl_api_ring_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148};
Dave Barach371e4e12016-07-08 09:38:52 -04001149/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150
Dave Barach49fe0462017-09-12 17:06:56 -04001151/*?
1152 * Display current api client connections
1153?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001154/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001155VLIB_CLI_COMMAND (cli_show_api_clients_command, static) =
1156{
1157 .path = "show api clients",
1158 .short_help = "Client information",
1159 .function = vl_api_client_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160};
Dave Barach371e4e12016-07-08 09:38:52 -04001161/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162
Dave Barach49fe0462017-09-12 17:06:56 -04001163/*?
1164 * Display the current api message tracing status
1165?*/
Calvin16649372016-07-28 13:52:05 -04001166/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001167VLIB_CLI_COMMAND (cli_show_api_status_command, static) =
1168{
1169 .path = "show api trace-status",
1170 .short_help = "Display API trace status",
1171 .function = vl_api_status_command,
Calvin16649372016-07-28 13:52:05 -04001172};
1173/* *INDENT-ON* */
1174
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001176vl_api_message_table_command (vlib_main_t * vm,
1177 unformat_input_t * input,
1178 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179{
Dave Barach371e4e12016-07-08 09:38:52 -04001180 api_main_t *am = &api_main;
1181 int i;
1182 int verbose = 0;
1183
1184 if (unformat (input, "verbose"))
1185 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
1187
Dave Barach371e4e12016-07-08 09:38:52 -04001188 if (verbose == 0)
1189 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1190 else
1191 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1192 "MP-safe");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193
Dave Barach371e4e12016-07-08 09:38:52 -04001194 for (i = 1; i < vec_len (am->msg_names); i++)
1195 {
1196 if (verbose == 0)
1197 {
1198 vlib_cli_output (vm, "%-4d %s", i,
1199 am->msg_names[i] ? am->msg_names[i] :
1200 " [no handler]");
1201 }
1202 else
1203 {
1204 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1205 am->msg_names[i] ? am->msg_names[i] :
1206 " [no handler]", am->message_bounce[i],
1207 am->is_mp_safe[i]);
1208 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 }
1210
Dave Barach371e4e12016-07-08 09:38:52 -04001211 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212}
1213
Dave Barach49fe0462017-09-12 17:06:56 -04001214/*?
1215 * Display the current api message decode tables
1216?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001217/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001218VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) =
1219{
1220 .path = "show api message-table",
1221 .short_help = "Message Table",
1222 .function = vl_api_message_table_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223};
Dave Barach371e4e12016-07-08 09:38:52 -04001224/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001227vl_api_trace_command (vlib_main_t * vm,
1228 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229{
Dave Barach371e4e12016-07-08 09:38:52 -04001230 u32 nitems = 1024;
1231 vl_api_trace_which_t which = VL_API_TRACE_RX;
Dave Barach371e4e12016-07-08 09:38:52 -04001232 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233
Dave Barach371e4e12016-07-08 09:38:52 -04001234 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1235 {
1236 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1237 goto configure;
1238 else if (unformat (input, "tx nitems %u", &nitems)
1239 || unformat (input, "tx"))
1240 {
1241 which = VL_API_TRACE_RX;
1242 goto configure;
1243 }
1244 else if (unformat (input, "on rx"))
1245 {
1246 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1247 }
1248 else if (unformat (input, "on tx"))
1249 {
1250 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1251 }
1252 else if (unformat (input, "on"))
1253 {
1254 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1255 }
1256 else if (unformat (input, "off"))
1257 {
1258 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1259 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1260 }
1261 else if (unformat (input, "free"))
1262 {
1263 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1264 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1265 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1266 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1267 }
Dave Barach371e4e12016-07-08 09:38:52 -04001268 else if (unformat (input, "debug on"))
1269 {
1270 am->msg_print_flag = 1;
1271 }
1272 else if (unformat (input, "debug off"))
1273 {
1274 am->msg_print_flag = 0;
1275 }
1276 else
1277 return clib_error_return (0, "unknown input `%U'",
1278 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001279 }
Dave Barach371e4e12016-07-08 09:38:52 -04001280 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281
Dave Barach371e4e12016-07-08 09:38:52 -04001282configure:
1283 if (vl_msg_api_trace_configure (am, which, nitems))
1284 {
1285 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1286 which, nitems);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287 }
1288
Dave Barach371e4e12016-07-08 09:38:52 -04001289 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290}
1291
Dave Barach49fe0462017-09-12 17:06:56 -04001292/*?
1293 * Control the binary API trace mechanism
1294?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001295/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001296VLIB_CLI_COMMAND (trace, static) =
1297{
1298 .path = "set api-trace [on][on tx][on rx][off][free][debug on][debug off]",
1299 .short_help = "API trace",
1300 .function = vl_api_trace_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301};
Dave Barach371e4e12016-07-08 09:38:52 -04001302/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001303
1304clib_error_t *
1305vlibmemory_init (vlib_main_t * vm)
1306{
Dave Barach309bef22016-01-22 16:09:52 -05001307 api_main_t *am = &api_main;
Dave Barachb3d93da2016-08-03 14:34:38 -04001308 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -04001309
Dave Barachb3d93da2016-08-03 14:34:38 -04001310 memset (a, 0, sizeof (*a));
1311 a->root_path = am->root_path;
1312 a->name = SVM_GLOBAL_REGION_NAME;
Dave Barachc3799992016-08-15 11:12:27 -04001313 a->baseva = (am->global_baseva != 0) ?
Dave Barachb3d93da2016-08-03 14:34:38 -04001314 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1315 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1316 a->flags = SVM_FLAGS_NODATA;
1317 a->uid = am->api_uid;
1318 a->gid = am->api_gid;
Dave Barachc3799992016-08-15 11:12:27 -04001319 a->pvt_heap_size =
1320 (am->global_pvt_heap_size !=
1321 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
Dave Barachb3d93da2016-08-03 14:34:38 -04001322
1323 svm_region_init_args (a);
Dave Barach309bef22016-01-22 16:09:52 -05001324 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325}
1326
1327VLIB_INIT_FUNCTION (vlibmemory_init);
1328
Dave Barach371e4e12016-07-08 09:38:52 -04001329void
Neale Rannse72be392017-04-26 13:59:20 -07001330vl_set_memory_region_name (const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331{
Dave Barach371e4e12016-07-08 09:38:52 -04001332 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333
Dave Barach371e4e12016-07-08 09:38:52 -04001334 am->region_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335}
1336
Dave Barach371e4e12016-07-08 09:38:52 -04001337static int
1338range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001339{
Dave Barach371e4e12016-07-08 09:38:52 -04001340 int len0, len1, clen;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001341
Dave Barach371e4e12016-07-08 09:38:52 -04001342 len0 = vec_len (a0->name);
1343 len1 = vec_len (a1->name);
1344 clen = len0 < len1 ? len0 : len1;
1345 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346}
1347
Dave Barach371e4e12016-07-08 09:38:52 -04001348static u8 *
1349format_api_msg_range (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001350{
Dave Barach371e4e12016-07-08 09:38:52 -04001351 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001352
Dave Barach371e4e12016-07-08 09:38:52 -04001353 if (rp == 0)
Dave Barach49fe0462017-09-12 17:06:56 -04001354 s = format (s, "%-50s%9s%9s", "Name", "First-ID", "Last-ID");
Dave Barach371e4e12016-07-08 09:38:52 -04001355 else
Dave Barach49fe0462017-09-12 17:06:56 -04001356 s = format (s, "%-50s%9d%9d", rp->name, rp->first_msg_id,
Dave Barach371e4e12016-07-08 09:38:52 -04001357 rp->last_msg_id);
1358
1359 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001360}
1361
1362static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001363vl_api_show_plugin_command (vlib_main_t * vm,
1364 unformat_input_t * input,
1365 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366{
Dave Barach371e4e12016-07-08 09:38:52 -04001367 api_main_t *am = &api_main;
1368 vl_api_msg_range_t *rp = 0;
1369 int i;
1370
1371 if (vec_len (am->msg_ranges) == 0)
1372 {
1373 vlib_cli_output (vm, "No plugin API message ranges configured...");
1374 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376
Dave Barach371e4e12016-07-08 09:38:52 -04001377 rp = vec_dup (am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001378
Dave Barach371e4e12016-07-08 09:38:52 -04001379 vec_sort_with_function (rp, range_compare);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001380
Dave Barach371e4e12016-07-08 09:38:52 -04001381 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1382 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1383
1384 for (i = 0; i < vec_len (rp); i++)
1385 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1386
Dave Barachdfbee412017-03-02 18:24:10 -05001387 vec_free (rp);
1388
Dave Barach371e4e12016-07-08 09:38:52 -04001389 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390}
1391
Dave Barach49fe0462017-09-12 17:06:56 -04001392/*?
1393 * Display the plugin binary API message range table
1394?*/
Dave Barach371e4e12016-07-08 09:38:52 -04001395/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04001396VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) =
1397{
1398 .path = "show api plugin",
1399 .short_help = "show api plugin",
1400 .function = vl_api_show_plugin_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401};
Dave Barach371e4e12016-07-08 09:38:52 -04001402/* *INDENT-ON* */
Dave Barach4e281a42015-12-14 11:13:29 -05001403
Dave Barach371e4e12016-07-08 09:38:52 -04001404static void
1405vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001406{
Dave Barach11b8dbf2017-04-24 10:46:54 -04001407 vl_api_rpc_call_reply_t *rmp;
Dave Barach371e4e12016-07-08 09:38:52 -04001408 int (*fp) (void *);
Dave Barach4e281a42015-12-14 11:13:29 -05001409 i32 rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -04001410 vlib_main_t *vm = vlib_get_main ();
Dave Barach4e281a42015-12-14 11:13:29 -05001411
1412 if (mp->function == 0)
1413 {
1414 rv = -1;
1415 clib_warning ("rpc NULL function pointer");
1416 }
Dave Barach371e4e12016-07-08 09:38:52 -04001417
Dave Barach4e281a42015-12-14 11:13:29 -05001418 else
1419 {
1420 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001421 vlib_worker_thread_barrier_sync (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001422
Dave Barach371e4e12016-07-08 09:38:52 -04001423 fp = uword_to_pointer (mp->function, int (*)(void *));
1424 rv = fp (mp->data);
Dave Barach4e281a42015-12-14 11:13:29 -05001425
1426 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001427 vlib_worker_thread_barrier_release (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001428 }
1429
1430 if (mp->send_reply)
1431 {
Dave Barach371e4e12016-07-08 09:38:52 -04001432 unix_shared_memory_queue_t *q =
1433 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach4e281a42015-12-14 11:13:29 -05001434 if (q)
Dave Barach371e4e12016-07-08 09:38:52 -04001435 {
1436 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barach11b8dbf2017-04-24 10:46:54 -04001437 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
Dave Barach371e4e12016-07-08 09:38:52 -04001438 rmp->context = mp->context;
1439 rmp->retval = rv;
1440 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1441 }
Dave Barach4e281a42015-12-14 11:13:29 -05001442 }
1443 if (mp->multicast)
1444 {
1445 clib_warning ("multicast not yet implemented...");
1446 }
1447}
1448
Dave Barach371e4e12016-07-08 09:38:52 -04001449static void
Dave Barach11b8dbf2017-04-24 10:46:54 -04001450vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001451{
Dave Barach371e4e12016-07-08 09:38:52 -04001452 clib_warning ("unimplemented");
1453}
1454
John Lo7e9743a2017-09-23 08:59:58 -04001455always_inline void
1456vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
1457 u8 force_rpc)
Dave Barach371e4e12016-07-08 09:38:52 -04001458{
1459 vl_api_rpc_call_t *mp;
Dave Barach4e281a42015-12-14 11:13:29 -05001460 api_main_t *am = &api_main;
1461 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barache9183632016-10-04 16:53:56 -04001462 unix_shared_memory_queue_t *q;
Dave Barach4e281a42015-12-14 11:13:29 -05001463
Dave Barache9183632016-10-04 16:53:56 -04001464 /* Main thread: call the function directly */
John Lo7e9743a2017-09-23 08:59:58 -04001465 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
Dave Barache9183632016-10-04 16:53:56 -04001466 {
1467 vlib_main_t *vm = vlib_get_main ();
1468 void (*call_fp) (void *);
1469
1470 vlib_worker_thread_barrier_sync (vm);
1471
1472 call_fp = fp;
1473 call_fp (data);
1474
1475 vlib_worker_thread_barrier_release (vm);
1476 return;
1477 }
1478
1479 /* Any other thread, actually do an RPC call... */
Dave Barach4e281a42015-12-14 11:13:29 -05001480 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
Dave Barache9183632016-10-04 16:53:56 -04001481
Dave Barach4e281a42015-12-14 11:13:29 -05001482 memset (mp, 0, sizeof (*mp));
Damjan Marionf1213b82016-03-13 02:22:06 +01001483 clib_memcpy (mp->data, data, data_length);
Dave Barach4e281a42015-12-14 11:13:29 -05001484 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
Dave Barach371e4e12016-07-08 09:38:52 -04001485 mp->function = pointer_to_uword (fp);
Dave Barach4e281a42015-12-14 11:13:29 -05001486 mp->need_barrier_sync = 1;
Dave Barach371e4e12016-07-08 09:38:52 -04001487
Dave Barache9183632016-10-04 16:53:56 -04001488 /*
1489 * Use the "normal" control-plane mechanism for the main thread.
1490 * Well, almost. if the main input queue is full, we cannot
1491 * block. Otherwise, we can expect a barrier sync timeout.
1492 */
1493 q = shmem_hdr->vl_input_queue;
1494
1495 while (pthread_mutex_trylock (&q->mutex))
1496 vlib_worker_thread_barrier_check ();
1497
1498 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1499 {
1500 pthread_mutex_unlock (&q->mutex);
1501 vlib_worker_thread_barrier_check ();
1502 while (pthread_mutex_trylock (&q->mutex))
1503 vlib_worker_thread_barrier_check ();
1504 }
1505
1506 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1507
1508 pthread_mutex_unlock (&q->mutex);
Dave Barach4e281a42015-12-14 11:13:29 -05001509}
1510
John Lo7e9743a2017-09-23 08:59:58 -04001511/*
1512 * Check if called from worker threads.
1513 * If so, make rpc call of fp through shmem.
1514 * Otherwise, call fp directly
1515 */
1516void
1517vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1518{
1519 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1520 0);
1521}
1522
1523/*
1524 * Always make rpc call of fp through shmem, useful for calling from threads
1525 * not setup as worker threads, such as DPDK callback thread
1526 */
1527void
1528vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1529{
1530 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1531 1);
1532}
1533
Dave Barach987e11d2017-02-27 13:10:27 -05001534static void
1535vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1536{
Dave Barachdfbee412017-03-02 18:24:10 -05001537 api_main_t *am = &api_main;
1538 vl_api_msg_range_t *rp;
1539 uword *p;
Dave Barach987e11d2017-02-27 13:10:27 -05001540
Dave Barachdfbee412017-03-02 18:24:10 -05001541 /* Noop (except for tracing) during normal operation */
1542 if (am->replay_in_progress == 0)
1543 return;
1544
1545 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1546 if (p == 0)
1547 {
1548 clib_warning ("WARNING: traced plugin '%s' not in current image",
1549 mp->plugin_name);
1550 return;
1551 }
1552
1553 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1554 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1555 {
1556 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1557 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1558 rp->first_msg_id);
1559 }
1560
1561 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1562 {
1563 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1564 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1565 rp->last_msg_id);
1566 }
1567}
Dave Barach987e11d2017-02-27 13:10:27 -05001568
Dave Barach4e281a42015-12-14 11:13:29 -05001569#define foreach_rpc_api_msg \
1570_(RPC_CALL,rpc_call) \
Dave Barach11b8dbf2017-04-24 10:46:54 -04001571_(RPC_CALL_REPLY,rpc_call_reply)
Dave Barach4e281a42015-12-14 11:13:29 -05001572
Dave Barach987e11d2017-02-27 13:10:27 -05001573#define foreach_plugin_trace_msg \
1574_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1575
Dave Barach4e281a42015-12-14 11:13:29 -05001576static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001577rpc_api_hookup (vlib_main_t * vm)
Dave Barach4e281a42015-12-14 11:13:29 -05001578{
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001579 api_main_t *am = &api_main;
Dave Barach4e281a42015-12-14 11:13:29 -05001580#define _(N,n) \
1581 vl_msg_api_set_handlers(VL_API_##N, #n, \
1582 vl_api_##n##_t_handler, \
1583 vl_noop_handler, \
1584 vl_noop_handler, \
1585 vl_api_##n##_t_print, \
Dave Barach371e4e12016-07-08 09:38:52 -04001586 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1587 foreach_rpc_api_msg;
Dave Barach4e281a42015-12-14 11:13:29 -05001588#undef _
Dave Barach987e11d2017-02-27 13:10:27 -05001589
1590#define _(N,n) \
1591 vl_msg_api_set_handlers(VL_API_##N, #n, \
1592 vl_api_##n##_t_handler, \
1593 vl_noop_handler, \
1594 vl_noop_handler, \
1595 vl_api_##n##_t_print, \
1596 sizeof(vl_api_##n##_t), 1 /* do trace */);
1597 foreach_plugin_trace_msg;
1598#undef _
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001599
1600 /* No reason to halt the parade to create a trace record... */
1601 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
1602
Dave Barach371e4e12016-07-08 09:38:52 -04001603 return 0;
Dave Barach4e281a42015-12-14 11:13:29 -05001604}
1605
Dave Barach371e4e12016-07-08 09:38:52 -04001606VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1607
Dave Barach80f54e22017-03-08 19:08:56 -05001608typedef enum
1609{
1610 DUMP,
1611 CUSTOM_DUMP,
1612 REPLAY,
1613 INITIALIZERS,
1614} vl_api_replay_t;
1615
1616u8 *
1617format_vl_msg_api_trace_status (u8 * s, va_list * args)
1618{
1619 api_main_t *am = va_arg (*args, api_main_t *);
1620 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
1621 vl_api_trace_t *tp;
1622 char *trace_name;
1623
1624 switch (which)
1625 {
1626 case VL_API_TRACE_TX:
1627 tp = am->tx_trace;
1628 trace_name = "TX trace";
1629 break;
1630
1631 case VL_API_TRACE_RX:
1632 tp = am->rx_trace;
1633 trace_name = "RX trace";
1634 break;
1635
1636 default:
1637 abort ();
1638 }
1639
1640 if (tp == 0)
1641 {
1642 s = format (s, "%s: not yet configured.\n", trace_name);
1643 return s;
1644 }
1645
1646 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
1647 trace_name, vec_len (tp->traces), tp->nitems,
1648 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
1649 return s;
1650}
1651
1652void vl_msg_api_custom_dump_configure (api_main_t * am)
1653 __attribute__ ((weak));
1654void
1655vl_msg_api_custom_dump_configure (api_main_t * am)
1656{
1657}
1658
1659static void
1660vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
1661 u32 first_index, u32 last_index,
1662 vl_api_replay_t which)
1663{
1664 vl_api_trace_file_header_t *hp;
1665 int i, fd;
1666 struct stat statb;
1667 size_t file_size;
1668 u8 *msg;
1669 u8 endian_swap_needed = 0;
1670 api_main_t *am = &api_main;
1671 u8 *tmpbuf = 0;
1672 u32 nitems;
1673 void **saved_print_handlers = 0;
1674
1675 fd = open ((char *) filename, O_RDONLY);
1676
1677 if (fd < 0)
1678 {
1679 vlib_cli_output (vm, "Couldn't open %s\n", filename);
1680 return;
1681 }
1682
1683 if (fstat (fd, &statb) < 0)
1684 {
1685 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
1686 close (fd);
1687 return;
1688 }
1689
1690 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
1691 {
1692 vlib_cli_output (vm, "File not plausible: %s\n", filename);
1693 close (fd);
1694 return;
1695 }
1696
1697 file_size = statb.st_size;
1698 file_size = (file_size + 4095) & ~(4096);
1699
1700 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1701
1702 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
1703 {
1704 vlib_cli_output (vm, "mmap failed: %s\n", filename);
1705 close (fd);
1706 return;
1707 }
1708 close (fd);
1709
1710 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
1711 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
1712 endian_swap_needed = 1;
1713
1714 if (endian_swap_needed)
1715 nitems = ntohl (hp->nitems);
1716 else
1717 nitems = hp->nitems;
1718
1719 if (last_index == (u32) ~ 0)
1720 {
1721 last_index = nitems - 1;
1722 }
1723
1724 if (first_index >= nitems || last_index >= nitems)
1725 {
1726 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
1727 first_index, last_index, nitems - 1);
1728 munmap (hp, file_size);
1729 return;
1730 }
1731 if (hp->wrapped)
1732 vlib_cli_output (vm,
1733 "Note: wrapped/incomplete trace, results may vary\n");
1734
1735 if (which == CUSTOM_DUMP)
1736 {
1737 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
1738 vl_msg_api_custom_dump_configure (am);
1739 }
1740
1741
1742 msg = (u8 *) (hp + 1);
1743
1744 for (i = 0; i < first_index; i++)
1745 {
1746 trace_cfg_t *cfgp;
1747 int size;
1748 u16 msg_id;
1749
1750 size = clib_host_to_net_u32 (*(u32 *) msg);
1751 msg += sizeof (u32);
1752
1753 if (clib_arch_is_little_endian)
1754 msg_id = ntohs (*((u16 *) msg));
1755 else
1756 msg_id = *((u16 *) msg);
1757
1758 cfgp = am->api_trace_cfg + msg_id;
1759 if (!cfgp)
1760 {
1761 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1762 munmap (hp, file_size);
1763 return;
1764 }
1765 msg += size;
1766 }
1767
1768 if (which == REPLAY)
1769 am->replay_in_progress = 1;
1770
1771 for (; i <= last_index; i++)
1772 {
1773 trace_cfg_t *cfgp;
1774 u16 *msg_idp;
1775 u16 msg_id;
1776 int size;
1777
1778 if (which == DUMP)
1779 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
1780
1781 size = clib_host_to_net_u32 (*(u32 *) msg);
1782 msg += sizeof (u32);
1783
1784 if (clib_arch_is_little_endian)
1785 msg_id = ntohs (*((u16 *) msg));
1786 else
1787 msg_id = *((u16 *) msg);
1788
1789 cfgp = am->api_trace_cfg + msg_id;
1790 if (!cfgp)
1791 {
1792 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1793 munmap (hp, file_size);
1794 vec_free (tmpbuf);
1795 am->replay_in_progress = 0;
1796 return;
1797 }
1798
1799 /* Copy the buffer (from the read-only mmap'ed file) */
1800 vec_validate (tmpbuf, size - 1 + sizeof (uword));
1801 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
1802 memset (tmpbuf, 0xf, sizeof (uword));
1803
1804 /*
1805 * Endian swap if needed. All msg data is supposed to be
1806 * in network byte order. All msg handlers are supposed to
1807 * know that. The generic message dumpers don't know that.
1808 * One could fix apigen, I suppose.
1809 */
1810 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
1811 {
1812 void (*endian_fp) (void *);
1813 if (msg_id >= vec_len (am->msg_endian_handlers)
1814 || (am->msg_endian_handlers[msg_id] == 0))
1815 {
1816 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
1817 munmap (hp, file_size);
1818 vec_free (tmpbuf);
1819 am->replay_in_progress = 0;
1820 return;
1821 }
1822 endian_fp = am->msg_endian_handlers[msg_id];
1823 (*endian_fp) (tmpbuf + sizeof (uword));
1824 }
1825
1826 /* msg_id always in network byte order */
1827 if (clib_arch_is_little_endian)
1828 {
1829 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
1830 *msg_idp = msg_id;
1831 }
1832
1833 switch (which)
1834 {
1835 case CUSTOM_DUMP:
1836 case DUMP:
1837 if (msg_id < vec_len (am->msg_print_handlers) &&
1838 am->msg_print_handlers[msg_id])
1839 {
1840 u8 *(*print_fp) (void *, void *);
1841
1842 print_fp = (void *) am->msg_print_handlers[msg_id];
1843 (*print_fp) (tmpbuf + sizeof (uword), vm);
1844 }
1845 else
1846 {
1847 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
1848 msg_id);
1849 break;
1850 }
1851 break;
1852
1853 case INITIALIZERS:
1854 if (msg_id < vec_len (am->msg_print_handlers) &&
1855 am->msg_print_handlers[msg_id])
1856 {
1857 u8 *s;
1858 int j;
1859 u8 *(*print_fp) (void *, void *);
1860
1861 print_fp = (void *) am->msg_print_handlers[msg_id];
1862
1863 vlib_cli_output (vm, "/*");
1864
1865 (*print_fp) (tmpbuf + sizeof (uword), vm);
1866 vlib_cli_output (vm, "*/\n");
1867
1868 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
1869 am->msg_names[msg_id], i,
1870 am->api_trace_cfg[msg_id].size);
1871
1872 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
1873 {
1874 if ((j & 7) == 0)
1875 s = format (s, "\n ");
1876 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
1877 }
1878 s = format (s, "\n};\n%c", 0);
1879 vlib_cli_output (vm, (char *) s);
1880 vec_free (s);
1881 }
1882 break;
1883
1884 case REPLAY:
1885 if (msg_id < vec_len (am->msg_print_handlers) &&
1886 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
1887 {
1888 void (*handler) (void *);
1889
1890 handler = (void *) am->msg_handlers[msg_id];
1891
1892 if (!am->is_mp_safe[msg_id])
1893 vl_msg_api_barrier_sync ();
1894 (*handler) (tmpbuf + sizeof (uword));
1895 if (!am->is_mp_safe[msg_id])
1896 vl_msg_api_barrier_release ();
1897 }
1898 else
1899 {
1900 if (cfgp->replay_enable)
1901 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
1902 msg_id);
1903 break;
1904 }
1905 break;
1906 }
1907
1908 _vec_len (tmpbuf) = 0;
1909 msg += size;
1910 }
1911
1912 if (saved_print_handlers)
1913 {
1914 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
1915 vec_len (am->msg_print_handlers) * sizeof (void *));
1916 vec_free (saved_print_handlers);
1917 }
1918
1919 munmap (hp, file_size);
1920 vec_free (tmpbuf);
1921 am->replay_in_progress = 0;
1922}
1923
1924static clib_error_t *
1925api_trace_command_fn (vlib_main_t * vm,
1926 unformat_input_t * input, vlib_cli_command_t * cmd)
1927{
1928 u32 nitems = 256 << 10;
1929 api_main_t *am = &api_main;
1930 vl_api_trace_which_t which = VL_API_TRACE_RX;
1931 u8 *filename;
1932 u32 first = 0;
1933 u32 last = (u32) ~ 0;
1934 FILE *fp;
1935 int rv;
1936
1937 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1938 {
1939 if (unformat (input, "on") || unformat (input, "enable"))
1940 {
1941 if (unformat (input, "nitems %d", &nitems))
1942 ;
1943 vl_msg_api_trace_configure (am, which, nitems);
1944 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1945 }
1946 else if (unformat (input, "off"))
1947 {
1948 vl_msg_api_trace_onoff (am, which, 0);
1949 }
1950 else if (unformat (input, "save %s", &filename))
1951 {
1952 u8 *chroot_filename;
1953 if (strstr ((char *) filename, "..")
1954 || index ((char *) filename, '/'))
1955 {
1956 vlib_cli_output (vm, "illegal characters in filename '%s'",
1957 filename);
1958 return 0;
1959 }
1960
1961 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1962
1963 vec_free (filename);
1964
1965 fp = fopen ((char *) chroot_filename, "w");
1966 if (fp == NULL)
1967 {
1968 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
1969 return 0;
1970 }
1971 rv = vl_msg_api_trace_save (am, which, fp);
1972 fclose (fp);
1973 if (rv == -1)
1974 vlib_cli_output (vm, "API Trace data not present\n");
1975 else if (rv == -2)
1976 vlib_cli_output (vm, "File for writing is closed\n");
1977 else if (rv == -10)
1978 vlib_cli_output (vm, "Error while writing header to file\n");
1979 else if (rv == -11)
1980 vlib_cli_output (vm, "Error while writing trace to file\n");
1981 else if (rv == -12)
1982 vlib_cli_output (vm,
1983 "Error while writing end of buffer trace to file\n");
1984 else if (rv == -13)
1985 vlib_cli_output (vm,
1986 "Error while writing start of buffer trace to file\n");
1987 else if (rv < 0)
1988 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
1989 else
1990 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
1991 vec_free (chroot_filename);
1992 }
1993 else if (unformat (input, "dump %s", &filename))
1994 {
1995 vl_msg_api_process_file (vm, filename, first, last, DUMP);
1996 }
1997 else if (unformat (input, "custom-dump %s", &filename))
1998 {
1999 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
2000 }
2001 else if (unformat (input, "replay %s", &filename))
2002 {
2003 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
2004 }
2005 else if (unformat (input, "initializers %s", &filename))
2006 {
2007 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
2008 }
2009 else if (unformat (input, "tx"))
2010 {
2011 which = VL_API_TRACE_TX;
2012 }
2013 else if (unformat (input, "first %d", &first))
2014 {
2015 ;
2016 }
2017 else if (unformat (input, "last %d", &last))
2018 {
2019 ;
2020 }
2021 else if (unformat (input, "status"))
2022 {
2023 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
2024 am, which);
2025 }
2026 else if (unformat (input, "free"))
2027 {
2028 vl_msg_api_trace_onoff (am, which, 0);
2029 vl_msg_api_trace_free (am, which);
2030 }
2031 else if (unformat (input, "post-mortem-on"))
2032 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2033 else if (unformat (input, "post-mortem-off"))
2034 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
2035 else
2036 return clib_error_return (0, "unknown input `%U'",
2037 format_unformat_error, input);
2038 }
2039 return 0;
2040}
2041
Dave Barach49fe0462017-09-12 17:06:56 -04002042/*?
2043 * Display, replay, or save a binary API trace
2044?*/
2045
Dave Barach80f54e22017-03-08 19:08:56 -05002046/* *INDENT-OFF* */
Dave Barach49fe0462017-09-12 17:06:56 -04002047VLIB_CLI_COMMAND (api_trace_command, static) =
2048{
2049 .path = "api trace",
2050 .short_help =
2051 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
2052 .function = api_trace_command_fn,
Dave Barach80f54e22017-03-08 19:08:56 -05002053};
2054/* *INDENT-ON* */
2055
2056static clib_error_t *
2057api_config_fn (vlib_main_t * vm, unformat_input_t * input)
2058{
2059 u32 nitems = 256 << 10;
2060 vl_api_trace_which_t which = VL_API_TRACE_RX;
2061 api_main_t *am = &api_main;
2062
2063 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2064 {
2065 if (unformat (input, "on") || unformat (input, "enable"))
2066 {
2067 if (unformat (input, "nitems %d", &nitems))
2068 ;
2069 vl_msg_api_trace_configure (am, which, nitems);
2070 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
2071 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2072 }
Dave Barach49fe0462017-09-12 17:06:56 -04002073 else if (unformat (input, "save-api-table %s",
2074 &am->save_msg_table_filename))
2075 ;
Dave Barach80f54e22017-03-08 19:08:56 -05002076 else
2077 return clib_error_return (0, "unknown input `%U'",
2078 format_unformat_error, input);
2079 }
2080 return 0;
2081}
2082
Dave Barach49fe0462017-09-12 17:06:56 -04002083/*?
2084 * This module has three configuration parameters:
2085 * "on" or "enable" - enables binary api tracing
2086 * "nitems <nnn>" - sets the size of the circular buffer to <nnn>
2087 * "save-api-table <filename>" - dumps the API message table to /tmp/<filename>
2088?*/
Dave Barach80f54e22017-03-08 19:08:56 -05002089VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
2090
Dave Barach10d8cc62017-05-30 09:30:07 -04002091static clib_error_t *
2092api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
2093{
2094 api_main_t *am = &api_main;
2095 u32 nitems;
2096
2097 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2098 {
2099 if (unformat (input, "length %d", &nitems) ||
2100 (unformat (input, "len %d", &nitems)))
2101 {
2102 if (nitems >= 1024)
2103 am->vlib_input_queue_length = nitems;
2104 else
2105 clib_warning ("vlib input queue length %d too small, ignored",
2106 nitems);
2107 }
2108 else
2109 return clib_error_return (0, "unknown input `%U'",
2110 format_unformat_error, input);
2111 }
2112 return 0;
2113}
2114
2115VLIB_CONFIG_FUNCTION (api_queue_config_fn, "api-queue");
2116
Dave Barach49fe0462017-09-12 17:06:56 -04002117static u8 *
2118extract_name (u8 * s)
2119{
2120 u8 *rv;
2121
2122 rv = vec_dup (s);
2123
2124 while (vec_len (rv) && rv[vec_len (rv)] != '_')
2125 _vec_len (rv)--;
2126
2127 rv[vec_len (rv)] = 0;
2128
2129 return rv;
2130}
2131
2132static u8 *
2133extract_crc (u8 * s)
2134{
2135 int i;
2136 u8 *rv;
2137
2138 rv = vec_dup (s);
2139
2140 for (i = vec_len (rv) - 1; i >= 0; i--)
2141 {
2142 if (rv[i] == '_')
2143 {
2144 vec_delete (rv, i + 1, 0);
2145 break;
2146 }
2147 }
2148 return rv;
2149}
2150
2151typedef struct
2152{
2153 u8 *name_and_crc;
2154 u8 *name;
2155 u8 *crc;
2156 u32 msg_index;
2157 int which;
2158} msg_table_unserialize_t;
2159
2160static int
2161table_id_cmp (void *a1, void *a2)
2162{
2163 msg_table_unserialize_t *n1 = a1;
2164 msg_table_unserialize_t *n2 = a2;
2165
2166 return (n1->msg_index - n2->msg_index);
2167}
2168
2169static int
2170table_name_and_crc_cmp (void *a1, void *a2)
2171{
2172 msg_table_unserialize_t *n1 = a1;
2173 msg_table_unserialize_t *n2 = a2;
2174
2175 return strcmp ((char *) n1->name_and_crc, (char *) n2->name_and_crc);
2176}
2177
2178static clib_error_t *
2179dump_api_table_file_command_fn (vlib_main_t * vm,
2180 unformat_input_t * input,
2181 vlib_cli_command_t * cmd)
2182{
2183 u8 *filename = 0;
2184 api_main_t *am = &api_main;
2185 serialize_main_t _sm, *sm = &_sm;
2186 clib_error_t *error;
2187 u32 nmsgs;
2188 u32 msg_index;
2189 u8 *name_and_crc;
2190 int compare_current = 0;
2191 int numeric_sort = 0;
2192 msg_table_unserialize_t *table = 0, *item;
2193 u32 i;
2194 u32 ndifferences = 0;
2195
2196 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2197 {
2198 if (unformat (input, "file %s", &filename))
2199 ;
2200 else if (unformat (input, "compare-current")
2201 || unformat (input, "compare"))
2202 compare_current = 1;
2203 else if (unformat (input, "numeric"))
2204 numeric_sort = 1;
2205 else
2206 return clib_error_return (0, "unknown input `%U'",
2207 format_unformat_error, input);
2208 }
2209
2210 if (numeric_sort && compare_current)
2211 return clib_error_return
2212 (0, "Comparison and numeric sorting are incompatible");
2213
2214 if (filename == 0)
2215 return clib_error_return (0, "File not specified");
2216
2217 /* Load the serialized message table from the table dump */
2218
2219 error = unserialize_open_unix_file (sm, (char *) filename);
2220
2221 if (error)
2222 return error;
2223
2224 unserialize_integer (sm, &nmsgs, sizeof (u32));
2225
2226 for (i = 0; i < nmsgs; i++)
2227 {
2228 msg_index = unserialize_likely_small_unsigned_integer (sm);
2229 unserialize_cstring (sm, (char **) &name_and_crc);
2230 vec_add2 (table, item, 1);
2231 item->msg_index = msg_index;
2232 item->name_and_crc = name_and_crc;
2233 item->name = extract_name (name_and_crc);
2234 item->crc = extract_crc (name_and_crc);
2235 item->which = 0; /* file */
2236 }
2237 serialize_close (sm);
2238
2239 /* Compare with the current image? */
2240 if (compare_current)
2241 {
2242 /* Append the current message table */
2243 u8 *tblv = vec_dup (am->serialized_message_table_in_shmem);
2244
2245 serialize_open_vector (sm, tblv);
2246 unserialize_integer (sm, &nmsgs, sizeof (u32));
2247
2248 for (i = 0; i < nmsgs; i++)
2249 {
2250 msg_index = unserialize_likely_small_unsigned_integer (sm);
2251 unserialize_cstring (sm, (char **) &name_and_crc);
2252
2253 vec_add2 (table, item, 1);
2254 item->msg_index = msg_index;
2255 item->name_and_crc = name_and_crc;
2256 item->name = extract_name (name_and_crc);
2257 item->crc = extract_crc (name_and_crc);
2258 item->which = 1; /* current_image */
2259 }
2260 }
2261
2262 /* Sort the table. */
2263 if (numeric_sort)
2264 vec_sort_with_function (table, table_id_cmp);
2265 else
2266 vec_sort_with_function (table, table_name_and_crc_cmp);
2267
2268 if (compare_current)
2269 {
2270 ndifferences = 0;
2271
2272 /*
2273 * In this case, the recovered table will have two entries per
2274 * API message. So, if entries i and i+1 match, the message definitions
2275 * are identical. Otherwise, the crc is different, or a message is
2276 * present in only one of the tables.
2277 */
2278 vlib_cli_output (vm, "%=60s %s", "Message Name", "Result");
2279
2280 for (i = 0; i < vec_len (table);)
2281 {
2282 /* Last message lonely? */
2283 if (i == vec_len (table) - 1)
2284 {
2285 ndifferences++;
2286 goto last_unique;
2287 }
2288
2289 /* Identical pair? */
2290 if (!strncmp
2291 ((char *) table[i].name_and_crc,
2292 (char *) table[i + 1].name_and_crc,
2293 vec_len (table[i].name_and_crc)))
2294 {
2295 i += 2;
2296 continue;
2297 }
2298
2299 ndifferences++;
2300
2301 /* Only in one of two tables? */
2302 if (strncmp ((char *) table[i].name, (char *) table[i + 1].name,
2303 vec_len (table[i].name)))
2304 {
2305 last_unique:
2306 vlib_cli_output (vm, "%-60s only in %s",
2307 table[i].name, table[i].which ?
2308 "image" : "file");
2309 i++;
2310 continue;
2311 }
2312 /* In both tables, but with different signatures */
2313 vlib_cli_output (vm, "%-60s definition changed", table[i].name);
2314 i += 2;
2315 }
2316 if (ndifferences == 0)
2317 vlib_cli_output (vm, "No api message signature differences found.");
2318 else
2319 vlib_cli_output (vm, "Found %u api message signature differences",
2320 ndifferences);
2321 goto cleanup;
2322 }
2323
2324 /* Dump the table, sorted as shown above */
2325 vlib_cli_output (vm, "%=60s %=8s %=10s", "Message name", "MsgID", "CRC");
2326
2327 for (i = 0; i < vec_len (table); i++)
2328 {
2329 item = table + i;
2330 vlib_cli_output (vm, "%-60s %8u %10s", item->name,
2331 item->msg_index, item->crc);
2332 }
2333
2334cleanup:
2335 for (i = 0; i < vec_len (table); i++)
2336 {
2337 vec_free (table[i].name_and_crc);
2338 vec_free (table[i].name);
2339 vec_free (table[i].crc);
2340 }
2341
2342 vec_free (table);
2343
2344 return 0;
2345}
2346
2347/*?
2348 * Displays a serialized API message decode table, sorted by message name
2349 *
2350 * @cliexpar
2351 * @cliexstart{show api dump file <filename>}
2352 * Message name MsgID CRC
2353 * accept_session 407 8e2a127e
2354 * accept_session_reply 408 67d8c22a
2355 * add_node_next 549 e4202993
2356 * add_node_next_reply 550 e89d6eed
2357 * etc.
2358 * @cliexend
2359?*/
2360
2361/*?
2362 * Compares a serialized API message decode table with the current image
2363 *
2364 * @cliexpar
2365 * @cliexstart{show api dump file <filename> compare}
2366 * ip_add_del_route definition changed
2367 * ip_table_add_del definition changed
2368 * l2_macs_event only in image
2369 * vnet_ip4_fib_counters only in file
2370 * vnet_ip4_nbr_counters only in file
2371 * @cliexend
2372?*/
2373
2374/*?
Dave Barach905c14a2017-09-25 08:47:59 -04002375 * Display a serialized API message decode table, compare a saved
2376 * decode table with the current image, to establish API differences.
2377 *
Dave Barach49fe0462017-09-12 17:06:56 -04002378?*/
2379/* *INDENT-OFF* */
2380VLIB_CLI_COMMAND (dump_api_table_file, static) =
2381{
2382 .path = "show api dump",
2383 .short_help = "show api dump file <filename> [numeric | compare-current]",
2384 .function = dump_api_table_file_command_fn,
2385};
2386/* *INDENT-ON* */
2387
Dave Barach371e4e12016-07-08 09:38:52 -04002388/*
2389 * fd.io coding-style-patch-verification: ON
2390 *
2391 * Local Variables:
2392 * eval: (c-set-style "gnu")
2393 * End:
2394 */