blob: e5d88732c2e94d0e8af6ca7c892c05193603e06e [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
41#define TRACE_VLIB_MEMORY_QUEUE 0
42
Dave Barach371e4e12016-07-08 09:38:52 -040043#include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
Dave Barach371e4e12016-07-08 09:38:52 -040045#define vl_typedefs /* define message structures */
46#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#undef vl_typedefs
48
49/* instantiate all the print functions we know about */
50#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
51#define vl_printfun
Dave Barach371e4e12016-07-08 09:38:52 -040052#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070053#undef vl_printfun
54
55static inline void *
Dave Barach371e4e12016-07-08 09:38:52 -040056vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barach371e4e12016-07-08 09:38:52 -040058 vl_print (handle, "vl_api_memclnt_create_t:\n");
59 vl_print (handle, "name: %s\n", a->name);
60 vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
61 vl_print (handle, "context: %u\n", (unsigned) a->context);
62 vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
63 return handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064}
65
66static inline void *
Dave Barach371e4e12016-07-08 09:38:52 -040067vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barach371e4e12016-07-08 09:38:52 -040069 vl_print (handle, "vl_api_memclnt_delete_t:\n");
70 vl_print (handle, "index: %u\n", (unsigned) a->index);
71 vl_print (handle, "handle: 0x%wx\n", a->handle);
72 return handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -070073}
74
Dave Barach987e11d2017-02-27 13:10:27 -050075static inline void *
76vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
77 void *handle)
78{
79 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
80 a->plugin_name,
81 clib_host_to_net_u16 (a->first_msg_id),
82 clib_host_to_net_u16 (a->last_msg_id));
83 return handle;
84}
85
Ed Warnickecb9cada2015-12-08 15:45:58 -070086/* instantiate all the endian swap functions we know about */
87#define vl_endianfun
Dave Barach371e4e12016-07-08 09:38:52 -040088#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070089#undef vl_endianfun
90
Dave Barach371e4e12016-07-08 09:38:52 -040091void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
92 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barach371e4e12016-07-08 09:38:52 -040094void
95vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Dave Barach371e4e12016-07-08 09:38:52 -040097 static int count;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Dave Barach371e4e12016-07-08 09:38:52 -040099 if (count++ < 5)
100 clib_warning ("need to link against -lvlibsocket, msg not sent!");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101}
102
Dave Barach371e4e12016-07-08 09:38:52 -0400103void
104vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105{
Dave Barach371e4e12016-07-08 09:38:52 -0400106 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
107 {
108 vl_socket_api_send (rp, elem);
109 }
110 else
111 {
112 vl_msg_api_send_shmem (rp->vl_input_queue, elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 }
114}
115
Dave Barach557d1282016-11-10 14:22:49 -0500116u8 *
117vl_api_serialize_message_table (api_main_t * am, u8 * vector)
Dave Barach371e4e12016-07-08 09:38:52 -0400118{
Dave Barach557d1282016-11-10 14:22:49 -0500119 serialize_main_t _sm, *sm = &_sm;
120 hash_pair_t *hp;
121 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
122
123 serialize_open_vector (sm, vector);
124
125 /* serialize the count */
126 serialize_integer (sm, nmsg, sizeof (u32));
127
Dave Barach987e11d2017-02-27 13:10:27 -0500128 /* *INDENT-OFF* */
129 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
130 ({
131 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
132 serialize_cstring (sm, (char *) hp->key);
133 }));
134 /* *INDENT-ON* */
Dave Barach557d1282016-11-10 14:22:49 -0500135
136 return serialize_close_vector (sm);
Dave Barach371e4e12016-07-08 09:38:52 -0400137}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139/*
140 * vl_api_memclnt_create_t_handler
141 */
Dave Barach371e4e12016-07-08 09:38:52 -0400142void
143vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144{
Dave Barach371e4e12016-07-08 09:38:52 -0400145 vl_api_registration_t **regpp;
146 vl_api_registration_t *regp;
147 vl_api_memclnt_create_reply_t *rp;
148 svm_region_t *svm;
149 unix_shared_memory_queue_t *q;
Dave Barach557d1282016-11-10 14:22:49 -0500150 int rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400151 void *oldheap;
152 api_main_t *am = &api_main;
Dave Barach557d1282016-11-10 14:22:49 -0500153 u8 *serialized_message_table = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Dave Barach371e4e12016-07-08 09:38:52 -0400155 /*
156 * This is tortured. Maintain a vlib-address-space private
157 * pool of client registrations. We use the shared-memory virtual
158 * address of client structure as a handle, to allow direct
159 * manipulation of context quota vbls from the client library.
160 *
161 * This scheme causes trouble w/ API message trace replay, since
162 * some random VA from clib_mem_alloc() certainly won't
163 * occur in the Linux sim. The (very) few places
164 * that care need to use the pool index.
165 *
166 * Putting the registration object(s) into a pool in shared memory and
167 * using the pool index as a handle seems like a great idea.
168 * Unfortunately, each and every reference to that pool would need
169 * to be protected by a mutex:
170 *
171 * Client VLIB
172 * ------ ----
173 * convert pool index to
174 * pointer.
175 * <deschedule>
176 * expand pool
177 * <deschedule>
178 * kaboom!
179 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Dave Barach371e4e12016-07-08 09:38:52 -0400181 pool_get (am->vl_clients, regpp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach371e4e12016-07-08 09:38:52 -0400183 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Dave Barach557d1282016-11-10 14:22:49 -0500185 if (am->serialized_message_table_in_shmem == 0)
186 serialized_message_table = vl_api_serialize_message_table (am, 0);
187
Dave Barach371e4e12016-07-08 09:38:52 -0400188 pthread_mutex_lock (&svm->mutex);
189 oldheap = svm_push_data_heap (svm);
190 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barach371e4e12016-07-08 09:38:52 -0400192 regp = *regpp;
193 memset (regp, 0, sizeof (*regp));
194 regp->registration_type = REGISTRATION_TYPE_SHMEM;
195 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barach371e4e12016-07-08 09:38:52 -0400197 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
198 mp->input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Dave Barach371e4e12016-07-08 09:38:52 -0400200 regp->name = format (0, "%s", mp->name);
201 vec_add1 (regp->name, 0);
Dave Barach557d1282016-11-10 14:22:49 -0500202 if (serialized_message_table)
203 am->serialized_message_table_in_shmem =
204 vec_dup (serialized_message_table);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barach371e4e12016-07-08 09:38:52 -0400206 pthread_mutex_unlock (&svm->mutex);
207 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Dave Barach557d1282016-11-10 14:22:49 -0500209 vec_free (serialized_message_table);
210
Dave Barach371e4e12016-07-08 09:38:52 -0400211 rp = vl_msg_api_alloc (sizeof (*rp));
212 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
213 rp->handle = (uword) regp;
214 rp->index = vl_msg_api_handle_from_index_and_epoch
215 (regp->vl_api_registration_pool_index,
216 am->shmem_hdr->application_restarts);
217 rp->context = mp->context;
218 rp->response = ntohl (rv);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200219 rp->message_table =
220 pointer_to_uword (am->serialized_message_table_in_shmem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Dave Barach371e4e12016-07-08 09:38:52 -0400222 vl_msg_api_send_shmem (q, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223}
224
Dave Barachb64e4e22017-03-14 09:10:56 -0400225static int
226call_reaper_functions (u32 client_index)
Dave Barach371e4e12016-07-08 09:38:52 -0400227{
Dave Barachb64e4e22017-03-14 09:10:56 -0400228 clib_error_t *error = 0;
229 _vl_msg_api_function_list_elt_t *i;
230
231 i = api_main.reaper_function_registrations;
232 while (i)
233 {
234 error = i->f (client_index);
235 if (error)
236 clib_error_report (error);
237 i = i->next_init_function;
238 }
Dave Barach371e4e12016-07-08 09:38:52 -0400239 return 0;
240}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
242/*
243 * vl_api_memclnt_delete_t_handler
244 */
Dave Barach371e4e12016-07-08 09:38:52 -0400245void
246vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247{
Dave Barach371e4e12016-07-08 09:38:52 -0400248 vl_api_registration_t **regpp;
249 vl_api_registration_t *regp;
250 vl_api_memclnt_delete_reply_t *rp;
251 svm_region_t *svm;
252 void *oldheap;
253 api_main_t *am = &api_main;
254 u32 handle, client_index, epoch;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Dave Barach371e4e12016-07-08 09:38:52 -0400256 handle = mp->index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Dave Barachb64e4e22017-03-14 09:10:56 -0400258 if (call_reaper_functions (handle))
Dave Barach371e4e12016-07-08 09:38:52 -0400259 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
Dave Barach371e4e12016-07-08 09:38:52 -0400261 epoch = vl_msg_api_handle_get_epoch (handle);
262 client_index = vl_msg_api_handle_get_index (handle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
Dave Barach371e4e12016-07-08 09:38:52 -0400264 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
265 {
266 clib_warning
267 ("Stale clnt delete index %d old epoch %d cur epoch %d",
268 client_index, epoch,
269 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
270 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 }
272
Dave Barach371e4e12016-07-08 09:38:52 -0400273 regpp = am->vl_clients + client_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Dave Barach371e4e12016-07-08 09:38:52 -0400275 if (!pool_is_free (am->vl_clients, regpp))
276 {
277 regp = *regpp;
278 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barach371e4e12016-07-08 09:38:52 -0400280 /* $$$ check the input queue for e.g. punted sf's */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Dave Barach371e4e12016-07-08 09:38:52 -0400282 rp = vl_msg_api_alloc (sizeof (*rp));
283 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
284 rp->handle = mp->handle;
285 rp->response = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barach371e4e12016-07-08 09:38:52 -0400287 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barach371e4e12016-07-08 09:38:52 -0400289 if (client_index != regp->vl_api_registration_pool_index)
290 {
291 clib_warning ("mismatch client_index %d pool_index %d",
292 client_index, regp->vl_api_registration_pool_index);
293 vl_msg_api_free (rp);
294 return;
295 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Dave Barach371e4e12016-07-08 09:38:52 -0400297 /* No dangling references, please */
298 *regpp = 0;
299
300 pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
301
302 pthread_mutex_lock (&svm->mutex);
303 oldheap = svm_push_data_heap (svm);
304 /* Poison the old registration */
305 memset (regp, 0xF1, sizeof (*regp));
306 clib_mem_free (regp);
307 pthread_mutex_unlock (&svm->mutex);
308 svm_pop_heap (oldheap);
309 }
310 else
311 {
312 clib_warning ("unknown client ID %d", mp->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 }
314}
315
Dave Barach371e4e12016-07-08 09:38:52 -0400316void
317vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318{
Dave Barach371e4e12016-07-08 09:38:52 -0400319 vl_api_get_first_msg_id_reply_t *rmp;
320 unix_shared_memory_queue_t *q;
321 uword *p;
322 api_main_t *am = &api_main;
323 vl_api_msg_range_t *rp;
324 u8 name[64];
325 u16 first_msg_id = ~0;
326 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Dave Barach371e4e12016-07-08 09:38:52 -0400328 q = vl_api_client_index_to_input_queue (mp->client_index);
329 if (!q)
330 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
Dave Barach371e4e12016-07-08 09:38:52 -0400332 if (am->msg_range_by_name == 0)
333 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Dave Barach371e4e12016-07-08 09:38:52 -0400335 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Dave Barach371e4e12016-07-08 09:38:52 -0400337 p = hash_get_mem (am->msg_range_by_name, name);
338 if (p == 0)
339 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Dave Barach371e4e12016-07-08 09:38:52 -0400341 rp = vec_elt_at_index (am->msg_ranges, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Dave Barach371e4e12016-07-08 09:38:52 -0400343 first_msg_id = rp->first_msg_id;
344 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
346out:
347
Dave Barach371e4e12016-07-08 09:38:52 -0400348 rmp = vl_msg_api_alloc (sizeof (*rmp));
349 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
350 rmp->context = mp->context;
351 rmp->retval = ntohl (rv);
352 rmp->first_msg_id = ntohs (first_msg_id);
353 vl_msg_api_send_shmem (q, (u8 *) & rmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354}
355
356#define foreach_vlib_api_msg \
357_(MEMCLNT_CREATE, memclnt_create) \
358_(MEMCLNT_DELETE, memclnt_delete) \
359_(GET_FIRST_MSG_ID, get_first_msg_id)
360
361/*
362 * vl_api_init
363 */
Dave Barach371e4e12016-07-08 09:38:52 -0400364static int
Neale Rannse72be392017-04-26 13:59:20 -0700365memory_api_init (const char *region_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366{
Dave Barach371e4e12016-07-08 09:38:52 -0400367 int rv;
368 vl_msg_api_msg_config_t cfg;
369 vl_msg_api_msg_config_t *c = &cfg;
370
Dave Barach0691d6e2017-01-05 10:08:52 -0500371 memset (c, 0, sizeof (*c));
372
Dave Barach371e4e12016-07-08 09:38:52 -0400373 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
374 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
376#define _(N,n) do { \
377 c->id = VL_API_##N; \
378 c->name = #n; \
379 c->handler = vl_api_##n##_t_handler; \
380 c->cleanup = vl_noop_handler; \
381 c->endian = vl_api_##n##_t_endian; \
382 c->print = vl_api_##n##_t_print; \
383 c->size = sizeof(vl_api_##n##_t); \
384 c->traced = 1; /* trace, so these msgs print */ \
385 c->replay = 0; /* don't replay client create/delete msgs */ \
Dave Barach0691d6e2017-01-05 10:08:52 -0500386 c->message_bounce = 0; /* don't bounce this message */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 vl_msg_api_config(c);} while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400388
389 foreach_vlib_api_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390#undef _
391
Dave Barach371e4e12016-07-08 09:38:52 -0400392 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393}
394
395#define foreach_histogram_bucket \
396_(400) \
397_(200) \
398_(100) \
399_(10)
400
Dave Barach371e4e12016-07-08 09:38:52 -0400401typedef enum
402{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403#define _(n) SLEEP_##n##_US,
Dave Barach371e4e12016-07-08 09:38:52 -0400404 foreach_histogram_bucket
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405#undef _
406 SLEEP_N_BUCKETS,
407} histogram_index_t;
408
409static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
410
Dave Barach371e4e12016-07-08 09:38:52 -0400411static void memclnt_queue_callback (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
Dave Barach987e11d2017-02-27 13:10:27 -0500413/*
414 * Callback to send ourselves a plugin numbering-space trace msg
415 */
416static void
417send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
418{
419 vl_api_trace_plugin_msg_ids_t *mp;
420 api_main_t *am = &api_main;
421 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
422 unix_shared_memory_queue_t *q;
423
424 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
425 memset (mp, 0, sizeof (*mp));
426
427 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
428 strncpy ((char *) mp->plugin_name, (char *) name,
429 sizeof (mp->plugin_name) - 1);
430 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
431 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
432
433 q = shmem_hdr->vl_input_queue;
434
435 vl_msg_api_send_shmem (q, (u8 *) & mp);
436}
437
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438static uword
439memclnt_process (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400440 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441{
Dave Barach371e4e12016-07-08 09:38:52 -0400442 uword mp;
443 vl_shmem_hdr_t *shm;
444 unix_shared_memory_queue_t *q;
445 clib_error_t *e;
446 int rv;
447 api_main_t *am = &api_main;
448 f64 dead_client_scan_time;
449 f64 sleep_time, start_time;
450 f64 vector_rate;
Dave Barach987e11d2017-02-27 13:10:27 -0500451 int i;
Dave Barach371e4e12016-07-08 09:38:52 -0400452
453 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
454
455 if ((rv = memory_api_init (am->region_name)) < 0)
456 {
457 clib_warning ("memory_api_init returned %d, wait for godot...", rv);
458 vlib_process_suspend (vm, 1e70);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 }
460
Dave Barach371e4e12016-07-08 09:38:52 -0400461 shm = am->shmem_hdr;
462 ASSERT (shm);
463 q = shm->vl_input_queue;
464 ASSERT (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Dave Barach371e4e12016-07-08 09:38:52 -0400466 e = vlib_call_init_exit_functions
467 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
468 if (e)
469 clib_error_report (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470
Dave Barach371e4e12016-07-08 09:38:52 -0400471 sleep_time = 20.0;
472 dead_client_scan_time = vlib_time_now (vm) + 20.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
Dave Barach987e11d2017-02-27 13:10:27 -0500474 /*
475 * Send plugin message range messages for each plugin we loaded
476 */
477 for (i = 0; i < vec_len (am->msg_ranges); i++)
478 {
479 vl_api_msg_range_t *rp = am->msg_ranges + i;
480 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
481 rp->last_msg_id);
482 }
483
Dave Barach371e4e12016-07-08 09:38:52 -0400484 /* $$$ pay attention to frame size, control CPU usage */
485 while (1)
486 {
487 uword event_type __attribute__ ((unused));
488 i8 *headp;
489 int need_broadcast;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490
Dave Barach371e4e12016-07-08 09:38:52 -0400491 /*
492 * There's a reason for checking the queue before
493 * sleeping. If the vlib application crashes, it's entirely
494 * possible for a client to enqueue a connect request
495 * during the process restart interval.
496 *
497 * Unless some force of physics causes the new incarnation
498 * of the application to process the request, the client will
499 * sit and wait for Godot...
500 */
501 vector_rate = vlib_last_vector_length_per_node (vm);
502 start_time = vlib_time_now (vm);
503 while (1)
504 {
505 pthread_mutex_lock (&q->mutex);
506 if (q->cursize == 0)
507 {
508 vm->api_queue_nonempty = 0;
509 pthread_mutex_unlock (&q->mutex);
510
511 if (TRACE_VLIB_MEMORY_QUEUE)
512 {
513 /* *INDENT-OFF* */
514 ELOG_TYPE_DECLARE (e) =
515 {
516 .format = "q-underflow: len %d",
517 .format_args = "i4",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 };
Dave Barach371e4e12016-07-08 09:38:52 -0400519 /* *INDENT-ON* */
520 struct
521 {
522 u32 len;
523 } *ed;
524 ed = ELOG_DATA (&vm->elog_main, e);
525 ed->len = 0;
526 }
527 sleep_time = 20.0;
528 break;
529 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Dave Barach371e4e12016-07-08 09:38:52 -0400531 headp = (i8 *) (q->data + sizeof (uword) * q->head);
532 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
Dave Barach371e4e12016-07-08 09:38:52 -0400534 q->head++;
535 need_broadcast = (q->cursize == q->maxsize / 2);
536 q->cursize--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Dave Barach371e4e12016-07-08 09:38:52 -0400538 if (PREDICT_FALSE (q->head == q->maxsize))
539 q->head = 0;
540 pthread_mutex_unlock (&q->mutex);
541 if (need_broadcast)
542 (void) pthread_cond_broadcast (&q->condvar);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Dave Barach371e4e12016-07-08 09:38:52 -0400544 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545
Dave Barach371e4e12016-07-08 09:38:52 -0400546 /* Allow no more than 10us without a pause */
547 if (vlib_time_now (vm) > start_time + 10e-6)
548 {
549 int index = SLEEP_400_US;
550 if (vector_rate > 40.0)
551 sleep_time = 400e-6;
552 else if (vector_rate > 20.0)
553 {
554 index = SLEEP_200_US;
555 sleep_time = 200e-6;
556 }
557 else if (vector_rate >= 1.0)
558 {
559 index = SLEEP_100_US;
560 sleep_time = 100e-6;
561 }
562 else
563 {
564 index = SLEEP_10_US;
565 sleep_time = 10e-6;
566 }
567 vector_rate_histogram[index] += 1;
568 break;
569 }
570 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571
Dave Barach371e4e12016-07-08 09:38:52 -0400572 event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time);
573 vm->queue_signal_pending = 0;
574 vlib_process_get_events (vm, 0 /* event_data */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Dave Barach371e4e12016-07-08 09:38:52 -0400576 if (vlib_time_now (vm) > dead_client_scan_time)
577 {
578 vl_api_registration_t **regpp;
579 vl_api_registration_t *regp;
580 unix_shared_memory_queue_t *q;
581 static u32 *dead_indices;
582 static u32 *confused_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
Dave Barach371e4e12016-07-08 09:38:52 -0400584 vec_reset_length (dead_indices);
585 vec_reset_length (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Dave Barach371e4e12016-07-08 09:38:52 -0400587 /* *INDENT-OFF* */
588 pool_foreach (regpp, am->vl_clients,
589 ({
590 regp = *regpp;
591 if (regp)
592 {
593 q = regp->vl_input_queue;
594 if (kill (q->consumer_pid, 0) < 0)
595 {
596 vec_add1(dead_indices, regpp - am->vl_clients);
597 }
598 }
599 else
600 {
601 clib_warning ("NULL client registration index %d",
602 regpp - am->vl_clients);
603 vec_add1 (confused_indices, regpp - am->vl_clients);
604 }
605 }));
606 /* *INDENT-ON* */
607 /* This should "never happen," but if it does, fix it... */
608 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
609 {
610 int i;
611 for (i = 0; i < vec_len (confused_indices); i++)
612 {
613 pool_put_index (am->vl_clients, confused_indices[i]);
614 }
615 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616
Dave Barach371e4e12016-07-08 09:38:52 -0400617 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
618 {
619 int i;
620 svm_region_t *svm;
621 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
Dave Barach371e4e12016-07-08 09:38:52 -0400623 /* Allow the application to clean up its registrations */
624 for (i = 0; i < vec_len (dead_indices); i++)
625 {
626 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
627 if (regpp)
628 {
629 u32 handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
Dave Barach371e4e12016-07-08 09:38:52 -0400631 handle = vl_msg_api_handle_from_index_and_epoch
632 (dead_indices[i], shm->application_restarts);
Dave Barachb64e4e22017-03-14 09:10:56 -0400633 (void) call_reaper_functions (handle);
Dave Barach371e4e12016-07-08 09:38:52 -0400634 }
635 }
636
637 svm = am->vlib_rp;
638 pthread_mutex_lock (&svm->mutex);
639 oldheap = svm_push_data_heap (svm);
640
641 for (i = 0; i < vec_len (dead_indices); i++)
642 {
643 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
644 if (regpp)
645 {
646 /* Poison the old registration */
647 memset (*regpp, 0xF3, sizeof (**regpp));
648 clib_mem_free (*regpp);
649 /* no dangling references, please */
650 *regpp = 0;
651 }
652 else
653 {
654 svm_pop_heap (oldheap);
655 clib_warning ("Duplicate free, client index %d",
656 regpp - am->vl_clients);
657 oldheap = svm_push_data_heap (svm);
658 }
659 }
660
661 svm_client_scan_this_region_nolock (am->vlib_rp);
662
663 pthread_mutex_unlock (&svm->mutex);
664 svm_pop_heap (oldheap);
665 for (i = 0; i < vec_len (dead_indices); i++)
666 pool_put_index (am->vl_clients, dead_indices[i]);
667 }
668
669 dead_client_scan_time = vlib_time_now (vm) + 20.0;
670 }
671
672 if (TRACE_VLIB_MEMORY_QUEUE)
673 {
674 /* *INDENT-OFF* */
675 ELOG_TYPE_DECLARE (e) = {
676 .format = "q-awake: len %d",
677 .format_args = "i4",
678 };
679 /* *INDENT-ON* */
680 struct
681 {
682 u32 len;
683 } *ed;
684 ed = ELOG_DATA (&vm->elog_main, e);
685 ed->len = q->cursize;
686 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 }
688
Dave Barach371e4e12016-07-08 09:38:52 -0400689 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690}
691
692static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400693vl_api_show_histogram_command (vlib_main_t * vm,
694 unformat_input_t * input,
695 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696{
Dave Barach371e4e12016-07-08 09:38:52 -0400697 u64 total_counts = 0;
698 int i;
699
700 for (i = 0; i < SLEEP_N_BUCKETS; i++)
701 {
702 total_counts += vector_rate_histogram[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 }
704
Dave Barach371e4e12016-07-08 09:38:52 -0400705 if (total_counts == 0)
706 {
707 vlib_cli_output (vm, "No control-plane activity.");
708 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709 }
710
711#define _(n) \
712 do { \
713 f64 percent; \
714 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
715 / (f64) total_counts; \
716 percent *= 100.0; \
717 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
718 vector_rate_histogram[SLEEP_##n##_US], \
719 percent); \
720 } while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400721 foreach_histogram_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722#undef _
723
Dave Barach371e4e12016-07-08 09:38:52 -0400724 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725}
726
Dave Barach371e4e12016-07-08 09:38:52 -0400727/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) = {
Calvin16649372016-07-28 13:52:05 -0400729 .path = "show api histogram",
730 .short_help = "show api histogram",
731 .function = vl_api_show_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732};
Dave Barach371e4e12016-07-08 09:38:52 -0400733/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734
735static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400736vl_api_clear_histogram_command (vlib_main_t * vm,
737 unformat_input_t * input,
738 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739{
Dave Barach371e4e12016-07-08 09:38:52 -0400740 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
Dave Barach371e4e12016-07-08 09:38:52 -0400742 for (i = 0; i < SLEEP_N_BUCKETS; i++)
743 vector_rate_histogram[i] = 0;
744 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745}
746
Dave Barach371e4e12016-07-08 09:38:52 -0400747/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) = {
749 .path = "clear api histogram",
750 .short_help = "clear api histogram",
751 .function = vl_api_clear_histogram_command,
752};
Dave Barach371e4e12016-07-08 09:38:52 -0400753/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754
755
Dave Barach371e4e12016-07-08 09:38:52 -0400756/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757VLIB_REGISTER_NODE (memclnt_node,static) = {
758 .function = memclnt_process,
759 .type = VLIB_NODE_TYPE_PROCESS,
760 .name = "api-rx-from-ring",
761 .state = VLIB_NODE_STATE_DISABLED,
762};
Dave Barach371e4e12016-07-08 09:38:52 -0400763/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Dave Barach371e4e12016-07-08 09:38:52 -0400765static void
766memclnt_queue_callback (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767{
Dave Barach371e4e12016-07-08 09:38:52 -0400768 static volatile int *cursizep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Dave Barach16c75df2016-05-31 14:05:46 -0400770 if (PREDICT_FALSE (cursizep == 0))
771 {
772 api_main_t *am = &api_main;
773 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barach371e4e12016-07-08 09:38:52 -0400774 unix_shared_memory_queue_t *q;
775
Dave Barach16c75df2016-05-31 14:05:46 -0400776 if (shmem_hdr == 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400777 return;
778
Dave Barach16c75df2016-05-31 14:05:46 -0400779 q = shmem_hdr->vl_input_queue;
780 if (q == 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400781 return;
Dave Barach16c75df2016-05-31 14:05:46 -0400782 cursizep = &q->cursize;
783 }
Dave Barach371e4e12016-07-08 09:38:52 -0400784
Dave Barach16c75df2016-05-31 14:05:46 -0400785 if (*cursizep >= 1)
786 {
787 vm->queue_signal_pending = 1;
788 vm->api_queue_nonempty = 1;
Dave Barach371e4e12016-07-08 09:38:52 -0400789 vlib_process_signal_event (vm, memclnt_node.index,
790 /* event_type */ 0, /* event_data */ 0);
Dave Barach16c75df2016-05-31 14:05:46 -0400791 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792}
793
Dave Barach371e4e12016-07-08 09:38:52 -0400794void
795vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796{
Dave Barach371e4e12016-07-08 09:38:52 -0400797 vlib_node_set_state (vm, memclnt_node.index,
798 (enable
799 ? VLIB_NODE_STATE_POLLING
800 : VLIB_NODE_STATE_DISABLED));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801}
802
803static uword
804api_rx_from_node (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400805 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806{
Dave Barach371e4e12016-07-08 09:38:52 -0400807 uword n_packets = frame->n_vectors;
808 uword n_left_from;
809 u32 *from;
810 static u8 *long_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811
Dave Barach371e4e12016-07-08 09:38:52 -0400812 vec_validate (long_msg, 4095);
813 n_left_from = frame->n_vectors;
814 from = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815
Dave Barach371e4e12016-07-08 09:38:52 -0400816 while (n_left_from > 0)
817 {
818 u32 bi0;
819 vlib_buffer_t *b0;
820 void *msg;
821 uword msg_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822
Dave Barach371e4e12016-07-08 09:38:52 -0400823 bi0 = from[0];
824 b0 = vlib_get_buffer (vm, bi0);
825 from += 1;
826 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
Dave Barach371e4e12016-07-08 09:38:52 -0400828 msg = b0->data + b0->current_data;
829 msg_len = b0->current_length;
830 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
831 {
832 ASSERT (long_msg != 0);
833 _vec_len (long_msg) = 0;
834 vec_add (long_msg, msg, msg_len);
835 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
836 {
837 b0 = vlib_get_buffer (vm, b0->next_buffer);
838 msg = b0->data + b0->current_data;
839 msg_len = b0->current_length;
840 vec_add (long_msg, msg, msg_len);
841 }
842 msg = long_msg;
843 }
844 vl_msg_api_handler_no_trace_no_free (msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845 }
846
Dave Barach371e4e12016-07-08 09:38:52 -0400847 /* Free what we've been given. */
848 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Dave Barach371e4e12016-07-08 09:38:52 -0400850 return n_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851}
852
Dave Barach371e4e12016-07-08 09:38:52 -0400853/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
855 .function = api_rx_from_node,
856 .type = VLIB_NODE_TYPE_INTERNAL,
857 .vector_size = 4,
858 .name = "api-rx-from-node",
859};
Dave Barach371e4e12016-07-08 09:38:52 -0400860/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
862static clib_error_t *
863setup_memclnt_exit (vlib_main_t * vm)
864{
Dave Barach371e4e12016-07-08 09:38:52 -0400865 atexit (vl_unmap_shmem);
866 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867}
868
869VLIB_INIT_FUNCTION (setup_memclnt_exit);
870
871
872static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400873vl_api_ring_command (vlib_main_t * vm,
874 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875{
Dave Barach371e4e12016-07-08 09:38:52 -0400876 int i;
877 ring_alloc_t *ap;
878 vl_shmem_hdr_t *shmem_hdr;
879 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880
Dave Barach371e4e12016-07-08 09:38:52 -0400881 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
Dave Barach371e4e12016-07-08 09:38:52 -0400883 if (shmem_hdr == 0)
884 {
885 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
886 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 }
888
Dave Barach371e4e12016-07-08 09:38:52 -0400889 vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
890 "Owner", "Size", "Nitems", "Hits", "Misses");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891
Dave Barach371e4e12016-07-08 09:38:52 -0400892 ap = shmem_hdr->vl_rings;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
Dave Barach371e4e12016-07-08 09:38:52 -0400894 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
895 {
896 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
897 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
898 ap++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 }
900
Dave Barach371e4e12016-07-08 09:38:52 -0400901 ap = shmem_hdr->client_rings;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
Dave Barach371e4e12016-07-08 09:38:52 -0400903 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
904 {
905 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
906 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
907 ap++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 }
909
Dave Barach371e4e12016-07-08 09:38:52 -0400910 vlib_cli_output (vm, "%d ring miss fallback allocations\n",
911 am->ring_misses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
Dave Barach842b9c52017-01-09 15:54:00 -0500913 vlib_cli_output
914 (vm, "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
915 shmem_hdr->application_restarts,
916 shmem_hdr->restart_reclaims, shmem_hdr->garbage_collects);
Dave Barach371e4e12016-07-08 09:38:52 -0400917 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918}
919
Dave Barach371e4e12016-07-08 09:38:52 -0400920void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
921 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922
Dave Barach371e4e12016-07-08 09:38:52 -0400923void
924dump_socket_clients (vlib_main_t * vm, api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925{
926}
927
928static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400929vl_api_client_command (vlib_main_t * vm,
930 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931{
Dave Barach371e4e12016-07-08 09:38:52 -0400932 vl_api_registration_t **regpp, *regp;
933 unix_shared_memory_queue_t *q;
934 char *health;
935 api_main_t *am = &api_main;
936 u32 *confused_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
Dave Barach371e4e12016-07-08 09:38:52 -0400938 if (!pool_elts (am->vl_clients))
939 goto socket_clients;
940 vlib_cli_output (vm, "Shared memory clients");
941 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
942 "Name", "PID", "Queue Length", "Queue VA", "Health");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943
Dave Barach371e4e12016-07-08 09:38:52 -0400944 /* *INDENT-OFF* */
945 pool_foreach (regpp, am->vl_clients,
946 ({
947 regp = *regpp;
948
949 if (regp)
950 {
951 q = regp->vl_input_queue;
952 if (kill (q->consumer_pid, 0) < 0)
953 {
954 health = "DEAD";
955 }
956 else
957 {
958 health = "alive";
959 }
960 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
961 regp->name, q->consumer_pid, q->cursize,
962 q, health);
963 }
964 else
965 {
966 clib_warning ("NULL client registration index %d",
967 regpp - am->vl_clients);
968 vec_add1 (confused_indices, regpp - am->vl_clients);
969 }
970 }));
971 /* *INDENT-ON* */
972
973 /* This should "never happen," but if it does, fix it... */
974 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
975 {
976 int i;
977 for (i = 0; i < vec_len (confused_indices); i++)
978 {
979 pool_put_index (am->vl_clients, confused_indices[i]);
980 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981 }
Dave Barach371e4e12016-07-08 09:38:52 -0400982 vec_free (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
Dave Barach371e4e12016-07-08 09:38:52 -0400984 if (am->missing_clients)
985 vlib_cli_output (vm, "%u messages with missing clients",
986 am->missing_clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987socket_clients:
Dave Barach371e4e12016-07-08 09:38:52 -0400988 dump_socket_clients (vm, am);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989
Dave Barach371e4e12016-07-08 09:38:52 -0400990 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991}
992
Calvin16649372016-07-28 13:52:05 -0400993static clib_error_t *
994vl_api_status_command (vlib_main_t * vm,
995 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
996{
997 api_main_t *am = &api_main;
998
999 // check if rx_trace and tx_trace are not null pointers
1000
1001 if (am->rx_trace == 0)
1002 {
1003 vlib_cli_output (vm, "RX Trace disabled\n");
1004 }
1005 else
1006 {
1007 if (am->rx_trace->enabled == 0)
1008 vlib_cli_output (vm, "RX Trace disabled\n");
1009 else
1010 vlib_cli_output (vm, "RX Trace enabled\n");
1011 }
1012
1013 if (am->tx_trace == 0)
1014 {
1015 vlib_cli_output (vm, "TX Trace disabled\n");
1016 }
1017 else
1018 {
1019 if (am->tx_trace->enabled == 0)
1020 vlib_cli_output (vm, "TX Trace disabled\n");
1021 else
1022 vlib_cli_output (vm, "TX Trace enabled\n");
1023 }
1024
1025 return 0;
1026}
1027
Dave Barach371e4e12016-07-08 09:38:52 -04001028/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029VLIB_CLI_COMMAND (cli_show_api_command, static) = {
1030 .path = "show api",
1031 .short_help = "Show API information",
1032};
Dave Barach371e4e12016-07-08 09:38:52 -04001033/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
Dave Barach371e4e12016-07-08 09:38:52 -04001035/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = {
1037 .path = "show api ring-stats",
1038 .short_help = "Message ring statistics",
1039 .function = vl_api_ring_command,
1040};
Dave Barach371e4e12016-07-08 09:38:52 -04001041/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042
Dave Barach371e4e12016-07-08 09:38:52 -04001043/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = {
1045 .path = "show api clients",
1046 .short_help = "Client information",
1047 .function = vl_api_client_command,
1048};
Dave Barach371e4e12016-07-08 09:38:52 -04001049/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Calvin16649372016-07-28 13:52:05 -04001051/* *INDENT-OFF* */
1052VLIB_CLI_COMMAND (cli_show_api_status_command, static) = {
1053 .path = "show api status",
1054 .short_help = "Show API trace status",
1055 .function = vl_api_status_command,
1056};
1057/* *INDENT-ON* */
1058
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001060vl_api_message_table_command (vlib_main_t * vm,
1061 unformat_input_t * input,
1062 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063{
Dave Barach371e4e12016-07-08 09:38:52 -04001064 api_main_t *am = &api_main;
1065 int i;
1066 int verbose = 0;
1067
1068 if (unformat (input, "verbose"))
1069 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070
1071
Dave Barach371e4e12016-07-08 09:38:52 -04001072 if (verbose == 0)
1073 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1074 else
1075 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1076 "MP-safe");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077
Dave Barach371e4e12016-07-08 09:38:52 -04001078 for (i = 1; i < vec_len (am->msg_names); i++)
1079 {
1080 if (verbose == 0)
1081 {
1082 vlib_cli_output (vm, "%-4d %s", i,
1083 am->msg_names[i] ? am->msg_names[i] :
1084 " [no handler]");
1085 }
1086 else
1087 {
1088 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1089 am->msg_names[i] ? am->msg_names[i] :
1090 " [no handler]", am->message_bounce[i],
1091 am->is_mp_safe[i]);
1092 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093 }
1094
Dave Barach371e4e12016-07-08 09:38:52 -04001095 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096}
1097
Dave Barach371e4e12016-07-08 09:38:52 -04001098/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) = {
1100 .path = "show api message-table",
1101 .short_help = "Message Table",
1102 .function = vl_api_message_table_command,
1103};
Dave Barach371e4e12016-07-08 09:38:52 -04001104/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001107vl_api_trace_command (vlib_main_t * vm,
1108 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109{
Dave Barach371e4e12016-07-08 09:38:52 -04001110 u32 nitems = 1024;
1111 vl_api_trace_which_t which = VL_API_TRACE_RX;
Dave Barach371e4e12016-07-08 09:38:52 -04001112 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113
Dave Barach371e4e12016-07-08 09:38:52 -04001114 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1115 {
1116 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1117 goto configure;
1118 else if (unformat (input, "tx nitems %u", &nitems)
1119 || unformat (input, "tx"))
1120 {
1121 which = VL_API_TRACE_RX;
1122 goto configure;
1123 }
1124 else if (unformat (input, "on rx"))
1125 {
1126 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1127 }
1128 else if (unformat (input, "on tx"))
1129 {
1130 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1131 }
1132 else if (unformat (input, "on"))
1133 {
1134 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1135 }
1136 else if (unformat (input, "off"))
1137 {
1138 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1139 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1140 }
1141 else if (unformat (input, "free"))
1142 {
1143 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1144 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1145 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1146 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1147 }
Dave Barach371e4e12016-07-08 09:38:52 -04001148 else if (unformat (input, "debug on"))
1149 {
1150 am->msg_print_flag = 1;
1151 }
1152 else if (unformat (input, "debug off"))
1153 {
1154 am->msg_print_flag = 0;
1155 }
1156 else
1157 return clib_error_return (0, "unknown input `%U'",
1158 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 }
Dave Barach371e4e12016-07-08 09:38:52 -04001160 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161
Dave Barach371e4e12016-07-08 09:38:52 -04001162configure:
1163 if (vl_msg_api_trace_configure (am, which, nitems))
1164 {
1165 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1166 which, nitems);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167 }
1168
Dave Barach371e4e12016-07-08 09:38:52 -04001169 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170}
1171
Dave Barach371e4e12016-07-08 09:38:52 -04001172/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173VLIB_CLI_COMMAND (trace, static) = {
1174 .path = "set api-trace",
1175 .short_help = "API trace",
1176 .function = vl_api_trace_command,
1177};
Dave Barach371e4e12016-07-08 09:38:52 -04001178/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179
1180clib_error_t *
1181vlibmemory_init (vlib_main_t * vm)
1182{
Dave Barach309bef22016-01-22 16:09:52 -05001183 api_main_t *am = &api_main;
Dave Barachb3d93da2016-08-03 14:34:38 -04001184 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -04001185
Dave Barachb3d93da2016-08-03 14:34:38 -04001186 memset (a, 0, sizeof (*a));
1187 a->root_path = am->root_path;
1188 a->name = SVM_GLOBAL_REGION_NAME;
Dave Barachc3799992016-08-15 11:12:27 -04001189 a->baseva = (am->global_baseva != 0) ?
Dave Barachb3d93da2016-08-03 14:34:38 -04001190 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1191 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1192 a->flags = SVM_FLAGS_NODATA;
1193 a->uid = am->api_uid;
1194 a->gid = am->api_gid;
Dave Barachc3799992016-08-15 11:12:27 -04001195 a->pvt_heap_size =
1196 (am->global_pvt_heap_size !=
1197 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
Dave Barachb3d93da2016-08-03 14:34:38 -04001198
1199 svm_region_init_args (a);
Dave Barach309bef22016-01-22 16:09:52 -05001200 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201}
1202
1203VLIB_INIT_FUNCTION (vlibmemory_init);
1204
Dave Barach371e4e12016-07-08 09:38:52 -04001205void
Neale Rannse72be392017-04-26 13:59:20 -07001206vl_set_memory_region_name (const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207{
Dave Barach371e4e12016-07-08 09:38:52 -04001208 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
Dave Barach371e4e12016-07-08 09:38:52 -04001210 am->region_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211}
1212
Dave Barach371e4e12016-07-08 09:38:52 -04001213static int
1214range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215{
Dave Barach371e4e12016-07-08 09:38:52 -04001216 int len0, len1, clen;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
Dave Barach371e4e12016-07-08 09:38:52 -04001218 len0 = vec_len (a0->name);
1219 len1 = vec_len (a1->name);
1220 clen = len0 < len1 ? len0 : len1;
1221 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222}
1223
Dave Barach371e4e12016-07-08 09:38:52 -04001224static u8 *
1225format_api_msg_range (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226{
Dave Barach371e4e12016-07-08 09:38:52 -04001227 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barach371e4e12016-07-08 09:38:52 -04001229 if (rp == 0)
1230 s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID");
1231 else
1232 s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id,
1233 rp->last_msg_id);
1234
1235 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236}
1237
1238static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001239vl_api_show_plugin_command (vlib_main_t * vm,
1240 unformat_input_t * input,
1241 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242{
Dave Barach371e4e12016-07-08 09:38:52 -04001243 api_main_t *am = &api_main;
1244 vl_api_msg_range_t *rp = 0;
1245 int i;
1246
1247 if (vec_len (am->msg_ranges) == 0)
1248 {
1249 vlib_cli_output (vm, "No plugin API message ranges configured...");
1250 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
Dave Barach371e4e12016-07-08 09:38:52 -04001253 rp = vec_dup (am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001254
Dave Barach371e4e12016-07-08 09:38:52 -04001255 vec_sort_with_function (rp, range_compare);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256
Dave Barach371e4e12016-07-08 09:38:52 -04001257 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1258 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1259
1260 for (i = 0; i < vec_len (rp); i++)
1261 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1262
Dave Barachdfbee412017-03-02 18:24:10 -05001263 vec_free (rp);
1264
Dave Barach371e4e12016-07-08 09:38:52 -04001265 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266}
1267
Dave Barach371e4e12016-07-08 09:38:52 -04001268/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) = {
1270 .path = "show api plugin",
1271 .short_help = "show api plugin",
1272 .function = vl_api_show_plugin_command,
1273};
Dave Barach371e4e12016-07-08 09:38:52 -04001274/* *INDENT-ON* */
Dave Barach4e281a42015-12-14 11:13:29 -05001275
Dave Barach371e4e12016-07-08 09:38:52 -04001276static void
1277vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001278{
Dave Barach11b8dbf2017-04-24 10:46:54 -04001279 vl_api_rpc_call_reply_t *rmp;
Dave Barach371e4e12016-07-08 09:38:52 -04001280 int (*fp) (void *);
Dave Barach4e281a42015-12-14 11:13:29 -05001281 i32 rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -04001282 vlib_main_t *vm = vlib_get_main ();
Dave Barach4e281a42015-12-14 11:13:29 -05001283
1284 if (mp->function == 0)
1285 {
1286 rv = -1;
1287 clib_warning ("rpc NULL function pointer");
1288 }
Dave Barach371e4e12016-07-08 09:38:52 -04001289
Dave Barach4e281a42015-12-14 11:13:29 -05001290 else
1291 {
1292 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001293 vlib_worker_thread_barrier_sync (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001294
Dave Barach371e4e12016-07-08 09:38:52 -04001295 fp = uword_to_pointer (mp->function, int (*)(void *));
1296 rv = fp (mp->data);
Dave Barach4e281a42015-12-14 11:13:29 -05001297
1298 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001299 vlib_worker_thread_barrier_release (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001300 }
1301
1302 if (mp->send_reply)
1303 {
Dave Barach371e4e12016-07-08 09:38:52 -04001304 unix_shared_memory_queue_t *q =
1305 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach4e281a42015-12-14 11:13:29 -05001306 if (q)
Dave Barach371e4e12016-07-08 09:38:52 -04001307 {
1308 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barach11b8dbf2017-04-24 10:46:54 -04001309 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
Dave Barach371e4e12016-07-08 09:38:52 -04001310 rmp->context = mp->context;
1311 rmp->retval = rv;
1312 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1313 }
Dave Barach4e281a42015-12-14 11:13:29 -05001314 }
1315 if (mp->multicast)
1316 {
1317 clib_warning ("multicast not yet implemented...");
1318 }
1319}
1320
Dave Barach371e4e12016-07-08 09:38:52 -04001321static void
Dave Barach11b8dbf2017-04-24 10:46:54 -04001322vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001323{
Dave Barach371e4e12016-07-08 09:38:52 -04001324 clib_warning ("unimplemented");
1325}
1326
1327void
1328vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1329{
1330 vl_api_rpc_call_t *mp;
Dave Barach4e281a42015-12-14 11:13:29 -05001331 api_main_t *am = &api_main;
1332 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barache9183632016-10-04 16:53:56 -04001333 unix_shared_memory_queue_t *q;
Dave Barach4e281a42015-12-14 11:13:29 -05001334
Dave Barache9183632016-10-04 16:53:56 -04001335 /* Main thread: call the function directly */
Damjan Marionf55f9b82017-05-10 21:06:28 +02001336 if (vlib_get_thread_index () == 0)
Dave Barache9183632016-10-04 16:53:56 -04001337 {
1338 vlib_main_t *vm = vlib_get_main ();
1339 void (*call_fp) (void *);
1340
1341 vlib_worker_thread_barrier_sync (vm);
1342
1343 call_fp = fp;
1344 call_fp (data);
1345
1346 vlib_worker_thread_barrier_release (vm);
1347 return;
1348 }
1349
1350 /* Any other thread, actually do an RPC call... */
Dave Barach4e281a42015-12-14 11:13:29 -05001351 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
Dave Barache9183632016-10-04 16:53:56 -04001352
Dave Barach4e281a42015-12-14 11:13:29 -05001353 memset (mp, 0, sizeof (*mp));
Damjan Marionf1213b82016-03-13 02:22:06 +01001354 clib_memcpy (mp->data, data, data_length);
Dave Barach4e281a42015-12-14 11:13:29 -05001355 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
Dave Barach371e4e12016-07-08 09:38:52 -04001356 mp->function = pointer_to_uword (fp);
Dave Barach4e281a42015-12-14 11:13:29 -05001357 mp->need_barrier_sync = 1;
Dave Barach371e4e12016-07-08 09:38:52 -04001358
Dave Barache9183632016-10-04 16:53:56 -04001359 /*
1360 * Use the "normal" control-plane mechanism for the main thread.
1361 * Well, almost. if the main input queue is full, we cannot
1362 * block. Otherwise, we can expect a barrier sync timeout.
1363 */
1364 q = shmem_hdr->vl_input_queue;
1365
1366 while (pthread_mutex_trylock (&q->mutex))
1367 vlib_worker_thread_barrier_check ();
1368
1369 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1370 {
1371 pthread_mutex_unlock (&q->mutex);
1372 vlib_worker_thread_barrier_check ();
1373 while (pthread_mutex_trylock (&q->mutex))
1374 vlib_worker_thread_barrier_check ();
1375 }
1376
1377 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1378
1379 pthread_mutex_unlock (&q->mutex);
Dave Barach4e281a42015-12-14 11:13:29 -05001380}
1381
Dave Barach987e11d2017-02-27 13:10:27 -05001382static void
1383vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1384{
Dave Barachdfbee412017-03-02 18:24:10 -05001385 api_main_t *am = &api_main;
1386 vl_api_msg_range_t *rp;
1387 uword *p;
Dave Barach987e11d2017-02-27 13:10:27 -05001388
Dave Barachdfbee412017-03-02 18:24:10 -05001389 /* Noop (except for tracing) during normal operation */
1390 if (am->replay_in_progress == 0)
1391 return;
1392
1393 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1394 if (p == 0)
1395 {
1396 clib_warning ("WARNING: traced plugin '%s' not in current image",
1397 mp->plugin_name);
1398 return;
1399 }
1400
1401 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1402 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1403 {
1404 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1405 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1406 rp->first_msg_id);
1407 }
1408
1409 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1410 {
1411 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1412 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1413 rp->last_msg_id);
1414 }
1415}
Dave Barach987e11d2017-02-27 13:10:27 -05001416
Dave Barach4e281a42015-12-14 11:13:29 -05001417#define foreach_rpc_api_msg \
1418_(RPC_CALL,rpc_call) \
Dave Barach11b8dbf2017-04-24 10:46:54 -04001419_(RPC_CALL_REPLY,rpc_call_reply)
Dave Barach4e281a42015-12-14 11:13:29 -05001420
Dave Barach987e11d2017-02-27 13:10:27 -05001421#define foreach_plugin_trace_msg \
1422_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1423
Dave Barach4e281a42015-12-14 11:13:29 -05001424static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001425rpc_api_hookup (vlib_main_t * vm)
Dave Barach4e281a42015-12-14 11:13:29 -05001426{
1427#define _(N,n) \
1428 vl_msg_api_set_handlers(VL_API_##N, #n, \
1429 vl_api_##n##_t_handler, \
1430 vl_noop_handler, \
1431 vl_noop_handler, \
1432 vl_api_##n##_t_print, \
Dave Barach371e4e12016-07-08 09:38:52 -04001433 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1434 foreach_rpc_api_msg;
Dave Barach4e281a42015-12-14 11:13:29 -05001435#undef _
Dave Barach987e11d2017-02-27 13:10:27 -05001436
1437#define _(N,n) \
1438 vl_msg_api_set_handlers(VL_API_##N, #n, \
1439 vl_api_##n##_t_handler, \
1440 vl_noop_handler, \
1441 vl_noop_handler, \
1442 vl_api_##n##_t_print, \
1443 sizeof(vl_api_##n##_t), 1 /* do trace */);
1444 foreach_plugin_trace_msg;
1445#undef _
Dave Barach371e4e12016-07-08 09:38:52 -04001446 return 0;
Dave Barach4e281a42015-12-14 11:13:29 -05001447}
1448
Dave Barach371e4e12016-07-08 09:38:52 -04001449VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1450
Dave Barach80f54e22017-03-08 19:08:56 -05001451typedef enum
1452{
1453 DUMP,
1454 CUSTOM_DUMP,
1455 REPLAY,
1456 INITIALIZERS,
1457} vl_api_replay_t;
1458
1459u8 *
1460format_vl_msg_api_trace_status (u8 * s, va_list * args)
1461{
1462 api_main_t *am = va_arg (*args, api_main_t *);
1463 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
1464 vl_api_trace_t *tp;
1465 char *trace_name;
1466
1467 switch (which)
1468 {
1469 case VL_API_TRACE_TX:
1470 tp = am->tx_trace;
1471 trace_name = "TX trace";
1472 break;
1473
1474 case VL_API_TRACE_RX:
1475 tp = am->rx_trace;
1476 trace_name = "RX trace";
1477 break;
1478
1479 default:
1480 abort ();
1481 }
1482
1483 if (tp == 0)
1484 {
1485 s = format (s, "%s: not yet configured.\n", trace_name);
1486 return s;
1487 }
1488
1489 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
1490 trace_name, vec_len (tp->traces), tp->nitems,
1491 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
1492 return s;
1493}
1494
1495void vl_msg_api_custom_dump_configure (api_main_t * am)
1496 __attribute__ ((weak));
1497void
1498vl_msg_api_custom_dump_configure (api_main_t * am)
1499{
1500}
1501
1502static void
1503vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
1504 u32 first_index, u32 last_index,
1505 vl_api_replay_t which)
1506{
1507 vl_api_trace_file_header_t *hp;
1508 int i, fd;
1509 struct stat statb;
1510 size_t file_size;
1511 u8 *msg;
1512 u8 endian_swap_needed = 0;
1513 api_main_t *am = &api_main;
1514 u8 *tmpbuf = 0;
1515 u32 nitems;
1516 void **saved_print_handlers = 0;
1517
1518 fd = open ((char *) filename, O_RDONLY);
1519
1520 if (fd < 0)
1521 {
1522 vlib_cli_output (vm, "Couldn't open %s\n", filename);
1523 return;
1524 }
1525
1526 if (fstat (fd, &statb) < 0)
1527 {
1528 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
1529 close (fd);
1530 return;
1531 }
1532
1533 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
1534 {
1535 vlib_cli_output (vm, "File not plausible: %s\n", filename);
1536 close (fd);
1537 return;
1538 }
1539
1540 file_size = statb.st_size;
1541 file_size = (file_size + 4095) & ~(4096);
1542
1543 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1544
1545 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
1546 {
1547 vlib_cli_output (vm, "mmap failed: %s\n", filename);
1548 close (fd);
1549 return;
1550 }
1551 close (fd);
1552
1553 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
1554 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
1555 endian_swap_needed = 1;
1556
1557 if (endian_swap_needed)
1558 nitems = ntohl (hp->nitems);
1559 else
1560 nitems = hp->nitems;
1561
1562 if (last_index == (u32) ~ 0)
1563 {
1564 last_index = nitems - 1;
1565 }
1566
1567 if (first_index >= nitems || last_index >= nitems)
1568 {
1569 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
1570 first_index, last_index, nitems - 1);
1571 munmap (hp, file_size);
1572 return;
1573 }
1574 if (hp->wrapped)
1575 vlib_cli_output (vm,
1576 "Note: wrapped/incomplete trace, results may vary\n");
1577
1578 if (which == CUSTOM_DUMP)
1579 {
1580 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
1581 vl_msg_api_custom_dump_configure (am);
1582 }
1583
1584
1585 msg = (u8 *) (hp + 1);
1586
1587 for (i = 0; i < first_index; i++)
1588 {
1589 trace_cfg_t *cfgp;
1590 int size;
1591 u16 msg_id;
1592
1593 size = clib_host_to_net_u32 (*(u32 *) msg);
1594 msg += sizeof (u32);
1595
1596 if (clib_arch_is_little_endian)
1597 msg_id = ntohs (*((u16 *) msg));
1598 else
1599 msg_id = *((u16 *) msg);
1600
1601 cfgp = am->api_trace_cfg + msg_id;
1602 if (!cfgp)
1603 {
1604 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1605 munmap (hp, file_size);
1606 return;
1607 }
1608 msg += size;
1609 }
1610
1611 if (which == REPLAY)
1612 am->replay_in_progress = 1;
1613
1614 for (; i <= last_index; i++)
1615 {
1616 trace_cfg_t *cfgp;
1617 u16 *msg_idp;
1618 u16 msg_id;
1619 int size;
1620
1621 if (which == DUMP)
1622 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
1623
1624 size = clib_host_to_net_u32 (*(u32 *) msg);
1625 msg += sizeof (u32);
1626
1627 if (clib_arch_is_little_endian)
1628 msg_id = ntohs (*((u16 *) msg));
1629 else
1630 msg_id = *((u16 *) msg);
1631
1632 cfgp = am->api_trace_cfg + msg_id;
1633 if (!cfgp)
1634 {
1635 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1636 munmap (hp, file_size);
1637 vec_free (tmpbuf);
1638 am->replay_in_progress = 0;
1639 return;
1640 }
1641
1642 /* Copy the buffer (from the read-only mmap'ed file) */
1643 vec_validate (tmpbuf, size - 1 + sizeof (uword));
1644 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
1645 memset (tmpbuf, 0xf, sizeof (uword));
1646
1647 /*
1648 * Endian swap if needed. All msg data is supposed to be
1649 * in network byte order. All msg handlers are supposed to
1650 * know that. The generic message dumpers don't know that.
1651 * One could fix apigen, I suppose.
1652 */
1653 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
1654 {
1655 void (*endian_fp) (void *);
1656 if (msg_id >= vec_len (am->msg_endian_handlers)
1657 || (am->msg_endian_handlers[msg_id] == 0))
1658 {
1659 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
1660 munmap (hp, file_size);
1661 vec_free (tmpbuf);
1662 am->replay_in_progress = 0;
1663 return;
1664 }
1665 endian_fp = am->msg_endian_handlers[msg_id];
1666 (*endian_fp) (tmpbuf + sizeof (uword));
1667 }
1668
1669 /* msg_id always in network byte order */
1670 if (clib_arch_is_little_endian)
1671 {
1672 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
1673 *msg_idp = msg_id;
1674 }
1675
1676 switch (which)
1677 {
1678 case CUSTOM_DUMP:
1679 case DUMP:
1680 if (msg_id < vec_len (am->msg_print_handlers) &&
1681 am->msg_print_handlers[msg_id])
1682 {
1683 u8 *(*print_fp) (void *, void *);
1684
1685 print_fp = (void *) am->msg_print_handlers[msg_id];
1686 (*print_fp) (tmpbuf + sizeof (uword), vm);
1687 }
1688 else
1689 {
1690 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
1691 msg_id);
1692 break;
1693 }
1694 break;
1695
1696 case INITIALIZERS:
1697 if (msg_id < vec_len (am->msg_print_handlers) &&
1698 am->msg_print_handlers[msg_id])
1699 {
1700 u8 *s;
1701 int j;
1702 u8 *(*print_fp) (void *, void *);
1703
1704 print_fp = (void *) am->msg_print_handlers[msg_id];
1705
1706 vlib_cli_output (vm, "/*");
1707
1708 (*print_fp) (tmpbuf + sizeof (uword), vm);
1709 vlib_cli_output (vm, "*/\n");
1710
1711 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
1712 am->msg_names[msg_id], i,
1713 am->api_trace_cfg[msg_id].size);
1714
1715 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
1716 {
1717 if ((j & 7) == 0)
1718 s = format (s, "\n ");
1719 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
1720 }
1721 s = format (s, "\n};\n%c", 0);
1722 vlib_cli_output (vm, (char *) s);
1723 vec_free (s);
1724 }
1725 break;
1726
1727 case REPLAY:
1728 if (msg_id < vec_len (am->msg_print_handlers) &&
1729 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
1730 {
1731 void (*handler) (void *);
1732
1733 handler = (void *) am->msg_handlers[msg_id];
1734
1735 if (!am->is_mp_safe[msg_id])
1736 vl_msg_api_barrier_sync ();
1737 (*handler) (tmpbuf + sizeof (uword));
1738 if (!am->is_mp_safe[msg_id])
1739 vl_msg_api_barrier_release ();
1740 }
1741 else
1742 {
1743 if (cfgp->replay_enable)
1744 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
1745 msg_id);
1746 break;
1747 }
1748 break;
1749 }
1750
1751 _vec_len (tmpbuf) = 0;
1752 msg += size;
1753 }
1754
1755 if (saved_print_handlers)
1756 {
1757 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
1758 vec_len (am->msg_print_handlers) * sizeof (void *));
1759 vec_free (saved_print_handlers);
1760 }
1761
1762 munmap (hp, file_size);
1763 vec_free (tmpbuf);
1764 am->replay_in_progress = 0;
1765}
1766
1767static clib_error_t *
1768api_trace_command_fn (vlib_main_t * vm,
1769 unformat_input_t * input, vlib_cli_command_t * cmd)
1770{
1771 u32 nitems = 256 << 10;
1772 api_main_t *am = &api_main;
1773 vl_api_trace_which_t which = VL_API_TRACE_RX;
1774 u8 *filename;
1775 u32 first = 0;
1776 u32 last = (u32) ~ 0;
1777 FILE *fp;
1778 int rv;
1779
1780 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1781 {
1782 if (unformat (input, "on") || unformat (input, "enable"))
1783 {
1784 if (unformat (input, "nitems %d", &nitems))
1785 ;
1786 vl_msg_api_trace_configure (am, which, nitems);
1787 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1788 }
1789 else if (unformat (input, "off"))
1790 {
1791 vl_msg_api_trace_onoff (am, which, 0);
1792 }
1793 else if (unformat (input, "save %s", &filename))
1794 {
1795 u8 *chroot_filename;
1796 if (strstr ((char *) filename, "..")
1797 || index ((char *) filename, '/'))
1798 {
1799 vlib_cli_output (vm, "illegal characters in filename '%s'",
1800 filename);
1801 return 0;
1802 }
1803
1804 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1805
1806 vec_free (filename);
1807
1808 fp = fopen ((char *) chroot_filename, "w");
1809 if (fp == NULL)
1810 {
1811 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
1812 return 0;
1813 }
1814 rv = vl_msg_api_trace_save (am, which, fp);
1815 fclose (fp);
1816 if (rv == -1)
1817 vlib_cli_output (vm, "API Trace data not present\n");
1818 else if (rv == -2)
1819 vlib_cli_output (vm, "File for writing is closed\n");
1820 else if (rv == -10)
1821 vlib_cli_output (vm, "Error while writing header to file\n");
1822 else if (rv == -11)
1823 vlib_cli_output (vm, "Error while writing trace to file\n");
1824 else if (rv == -12)
1825 vlib_cli_output (vm,
1826 "Error while writing end of buffer trace to file\n");
1827 else if (rv == -13)
1828 vlib_cli_output (vm,
1829 "Error while writing start of buffer trace to file\n");
1830 else if (rv < 0)
1831 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
1832 else
1833 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
1834 vec_free (chroot_filename);
1835 }
1836 else if (unformat (input, "dump %s", &filename))
1837 {
1838 vl_msg_api_process_file (vm, filename, first, last, DUMP);
1839 }
1840 else if (unformat (input, "custom-dump %s", &filename))
1841 {
1842 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
1843 }
1844 else if (unformat (input, "replay %s", &filename))
1845 {
1846 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
1847 }
1848 else if (unformat (input, "initializers %s", &filename))
1849 {
1850 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
1851 }
1852 else if (unformat (input, "tx"))
1853 {
1854 which = VL_API_TRACE_TX;
1855 }
1856 else if (unformat (input, "first %d", &first))
1857 {
1858 ;
1859 }
1860 else if (unformat (input, "last %d", &last))
1861 {
1862 ;
1863 }
1864 else if (unformat (input, "status"))
1865 {
1866 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
1867 am, which);
1868 }
1869 else if (unformat (input, "free"))
1870 {
1871 vl_msg_api_trace_onoff (am, which, 0);
1872 vl_msg_api_trace_free (am, which);
1873 }
1874 else if (unformat (input, "post-mortem-on"))
1875 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
1876 else if (unformat (input, "post-mortem-off"))
1877 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
1878 else
1879 return clib_error_return (0, "unknown input `%U'",
1880 format_unformat_error, input);
1881 }
1882 return 0;
1883}
1884
1885/* *INDENT-OFF* */
1886VLIB_CLI_COMMAND (api_trace_command, static) = {
1887 .path = "api trace",
1888 .short_help =
1889 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
1890 .function = api_trace_command_fn,
1891};
1892/* *INDENT-ON* */
1893
1894static clib_error_t *
1895api_config_fn (vlib_main_t * vm, unformat_input_t * input)
1896{
1897 u32 nitems = 256 << 10;
1898 vl_api_trace_which_t which = VL_API_TRACE_RX;
1899 api_main_t *am = &api_main;
1900
1901 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1902 {
1903 if (unformat (input, "on") || unformat (input, "enable"))
1904 {
1905 if (unformat (input, "nitems %d", &nitems))
1906 ;
1907 vl_msg_api_trace_configure (am, which, nitems);
1908 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1909 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
1910 }
1911 else
1912 return clib_error_return (0, "unknown input `%U'",
1913 format_unformat_error, input);
1914 }
1915 return 0;
1916}
1917
1918VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
1919
Dave Barach371e4e12016-07-08 09:38:52 -04001920/*
1921 * fd.io coding-style-patch-verification: ON
1922 *
1923 * Local Variables:
1924 * eval: (c-set-style "gnu")
1925 * End:
1926 */