blob: 55a90d641bcb6b2775f62a424da6a4822f90452a [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/*
Dave Barach52851e62017-08-07 09:35:25 -0400140 * vl_api_memclnt_create_internal
141 */
142
143u32
144vl_api_memclnt_create_internal (char *name, unix_shared_memory_queue_t * q)
145{
146 vl_api_registration_t **regpp;
147 vl_api_registration_t *regp;
148 svm_region_t *svm;
149 void *oldheap;
150 api_main_t *am = &api_main;
151
152 ASSERT (vlib_get_thread_index () == 0);
153 pool_get (am->vl_clients, regpp);
154
155 svm = am->vlib_rp;
156
157 pthread_mutex_lock (&svm->mutex);
158 oldheap = svm_push_data_heap (svm);
159 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
160
161 regp = *regpp;
162 memset (regp, 0, sizeof (*regp));
163 regp->registration_type = REGISTRATION_TYPE_SHMEM;
164 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
165
166 regp->vl_input_queue = q;
167 regp->name = format (0, "%s%c", name, 0);
168
169 pthread_mutex_unlock (&svm->mutex);
170 svm_pop_heap (oldheap);
171 return vl_msg_api_handle_from_index_and_epoch
172 (regp->vl_api_registration_pool_index,
173 am->shmem_hdr->application_restarts);
174}
175
176
177/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 * vl_api_memclnt_create_t_handler
179 */
Dave Barach371e4e12016-07-08 09:38:52 -0400180void
181vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182{
Dave Barach371e4e12016-07-08 09:38:52 -0400183 vl_api_registration_t **regpp;
184 vl_api_registration_t *regp;
185 vl_api_memclnt_create_reply_t *rp;
186 svm_region_t *svm;
187 unix_shared_memory_queue_t *q;
Dave Barach557d1282016-11-10 14:22:49 -0500188 int rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400189 void *oldheap;
190 api_main_t *am = &api_main;
Dave Barach557d1282016-11-10 14:22:49 -0500191 u8 *serialized_message_table = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barach371e4e12016-07-08 09:38:52 -0400193 /*
194 * This is tortured. Maintain a vlib-address-space private
195 * pool of client registrations. We use the shared-memory virtual
196 * address of client structure as a handle, to allow direct
197 * manipulation of context quota vbls from the client library.
198 *
199 * This scheme causes trouble w/ API message trace replay, since
200 * some random VA from clib_mem_alloc() certainly won't
201 * occur in the Linux sim. The (very) few places
202 * that care need to use the pool index.
203 *
204 * Putting the registration object(s) into a pool in shared memory and
205 * using the pool index as a handle seems like a great idea.
206 * Unfortunately, each and every reference to that pool would need
207 * to be protected by a mutex:
208 *
209 * Client VLIB
210 * ------ ----
211 * convert pool index to
212 * pointer.
213 * <deschedule>
214 * expand pool
215 * <deschedule>
216 * kaboom!
217 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Dave Barach371e4e12016-07-08 09:38:52 -0400219 pool_get (am->vl_clients, regpp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Dave Barach371e4e12016-07-08 09:38:52 -0400221 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
Dave Barach557d1282016-11-10 14:22:49 -0500223 if (am->serialized_message_table_in_shmem == 0)
224 serialized_message_table = vl_api_serialize_message_table (am, 0);
225
Dave Barach371e4e12016-07-08 09:38:52 -0400226 pthread_mutex_lock (&svm->mutex);
227 oldheap = svm_push_data_heap (svm);
228 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Dave Barach371e4e12016-07-08 09:38:52 -0400230 regp = *regpp;
231 memset (regp, 0, sizeof (*regp));
232 regp->registration_type = REGISTRATION_TYPE_SHMEM;
233 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
Dave Barach371e4e12016-07-08 09:38:52 -0400235 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
236 mp->input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Dave Barach371e4e12016-07-08 09:38:52 -0400238 regp->name = format (0, "%s", mp->name);
239 vec_add1 (regp->name, 0);
Dave Barach557d1282016-11-10 14:22:49 -0500240 if (serialized_message_table)
241 am->serialized_message_table_in_shmem =
242 vec_dup (serialized_message_table);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Dave Barach371e4e12016-07-08 09:38:52 -0400244 pthread_mutex_unlock (&svm->mutex);
245 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Dave Barach557d1282016-11-10 14:22:49 -0500247 vec_free (serialized_message_table);
248
Dave Barach371e4e12016-07-08 09:38:52 -0400249 rp = vl_msg_api_alloc (sizeof (*rp));
250 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
251 rp->handle = (uword) regp;
252 rp->index = vl_msg_api_handle_from_index_and_epoch
253 (regp->vl_api_registration_pool_index,
254 am->shmem_hdr->application_restarts);
255 rp->context = mp->context;
256 rp->response = ntohl (rv);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200257 rp->message_table =
258 pointer_to_uword (am->serialized_message_table_in_shmem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Dave Barach371e4e12016-07-08 09:38:52 -0400260 vl_msg_api_send_shmem (q, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261}
262
Dave Barachb64e4e22017-03-14 09:10:56 -0400263static int
264call_reaper_functions (u32 client_index)
Dave Barach371e4e12016-07-08 09:38:52 -0400265{
Dave Barachb64e4e22017-03-14 09:10:56 -0400266 clib_error_t *error = 0;
267 _vl_msg_api_function_list_elt_t *i;
268
269 i = api_main.reaper_function_registrations;
270 while (i)
271 {
272 error = i->f (client_index);
273 if (error)
274 clib_error_report (error);
275 i = i->next_init_function;
276 }
Dave Barach371e4e12016-07-08 09:38:52 -0400277 return 0;
278}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
280/*
281 * vl_api_memclnt_delete_t_handler
282 */
Dave Barach371e4e12016-07-08 09:38:52 -0400283void
284vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285{
Dave Barach371e4e12016-07-08 09:38:52 -0400286 vl_api_registration_t **regpp;
287 vl_api_registration_t *regp;
288 vl_api_memclnt_delete_reply_t *rp;
289 svm_region_t *svm;
290 void *oldheap;
291 api_main_t *am = &api_main;
292 u32 handle, client_index, epoch;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Dave Barach371e4e12016-07-08 09:38:52 -0400294 handle = mp->index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Dave Barachb64e4e22017-03-14 09:10:56 -0400296 if (call_reaper_functions (handle))
Dave Barach371e4e12016-07-08 09:38:52 -0400297 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Dave Barach371e4e12016-07-08 09:38:52 -0400299 epoch = vl_msg_api_handle_get_epoch (handle);
300 client_index = vl_msg_api_handle_get_index (handle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Dave Barach371e4e12016-07-08 09:38:52 -0400302 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
303 {
304 clib_warning
305 ("Stale clnt delete index %d old epoch %d cur epoch %d",
306 client_index, epoch,
307 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
308 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 }
310
Dave Barach371e4e12016-07-08 09:38:52 -0400311 regpp = am->vl_clients + client_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Dave Barach371e4e12016-07-08 09:38:52 -0400313 if (!pool_is_free (am->vl_clients, regpp))
314 {
315 regp = *regpp;
316 svm = am->vlib_rp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Dave Barach371e4e12016-07-08 09:38:52 -0400318 /* $$$ check the input queue for e.g. punted sf's */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
Dave Barach371e4e12016-07-08 09:38:52 -0400320 rp = vl_msg_api_alloc (sizeof (*rp));
321 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
322 rp->handle = mp->handle;
323 rp->response = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Dave Barach371e4e12016-07-08 09:38:52 -0400325 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Dave Barach371e4e12016-07-08 09:38:52 -0400327 if (client_index != regp->vl_api_registration_pool_index)
328 {
329 clib_warning ("mismatch client_index %d pool_index %d",
330 client_index, regp->vl_api_registration_pool_index);
331 vl_msg_api_free (rp);
332 return;
333 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Dave Barach371e4e12016-07-08 09:38:52 -0400335 /* No dangling references, please */
336 *regpp = 0;
337
338 pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
339
340 pthread_mutex_lock (&svm->mutex);
341 oldheap = svm_push_data_heap (svm);
342 /* Poison the old registration */
343 memset (regp, 0xF1, sizeof (*regp));
344 clib_mem_free (regp);
345 pthread_mutex_unlock (&svm->mutex);
346 svm_pop_heap (oldheap);
347 }
348 else
349 {
350 clib_warning ("unknown client ID %d", mp->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 }
352}
353
Dave Barach371e4e12016-07-08 09:38:52 -0400354void
355vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356{
Dave Barach371e4e12016-07-08 09:38:52 -0400357 vl_api_get_first_msg_id_reply_t *rmp;
358 unix_shared_memory_queue_t *q;
359 uword *p;
360 api_main_t *am = &api_main;
361 vl_api_msg_range_t *rp;
362 u8 name[64];
363 u16 first_msg_id = ~0;
364 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Dave Barach371e4e12016-07-08 09:38:52 -0400366 q = vl_api_client_index_to_input_queue (mp->client_index);
367 if (!q)
368 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Dave Barach371e4e12016-07-08 09:38:52 -0400370 if (am->msg_range_by_name == 0)
371 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372
Dave Barach371e4e12016-07-08 09:38:52 -0400373 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
Dave Barach371e4e12016-07-08 09:38:52 -0400375 p = hash_get_mem (am->msg_range_by_name, name);
376 if (p == 0)
377 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Dave Barach371e4e12016-07-08 09:38:52 -0400379 rp = vec_elt_at_index (am->msg_ranges, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Dave Barach371e4e12016-07-08 09:38:52 -0400381 first_msg_id = rp->first_msg_id;
382 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
384out:
385
Dave Barach371e4e12016-07-08 09:38:52 -0400386 rmp = vl_msg_api_alloc (sizeof (*rmp));
387 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
388 rmp->context = mp->context;
389 rmp->retval = ntohl (rv);
390 rmp->first_msg_id = ntohs (first_msg_id);
391 vl_msg_api_send_shmem (q, (u8 *) & rmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392}
393
394#define foreach_vlib_api_msg \
395_(MEMCLNT_CREATE, memclnt_create) \
396_(MEMCLNT_DELETE, memclnt_delete) \
397_(GET_FIRST_MSG_ID, get_first_msg_id)
398
399/*
400 * vl_api_init
401 */
Dave Barach371e4e12016-07-08 09:38:52 -0400402static int
Neale Rannse72be392017-04-26 13:59:20 -0700403memory_api_init (const char *region_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404{
Dave Barach371e4e12016-07-08 09:38:52 -0400405 int rv;
406 vl_msg_api_msg_config_t cfg;
407 vl_msg_api_msg_config_t *c = &cfg;
408
Dave Barach0691d6e2017-01-05 10:08:52 -0500409 memset (c, 0, sizeof (*c));
410
Dave Barach371e4e12016-07-08 09:38:52 -0400411 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
412 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413
414#define _(N,n) do { \
415 c->id = VL_API_##N; \
416 c->name = #n; \
417 c->handler = vl_api_##n##_t_handler; \
418 c->cleanup = vl_noop_handler; \
419 c->endian = vl_api_##n##_t_endian; \
420 c->print = vl_api_##n##_t_print; \
421 c->size = sizeof(vl_api_##n##_t); \
422 c->traced = 1; /* trace, so these msgs print */ \
423 c->replay = 0; /* don't replay client create/delete msgs */ \
Dave Barach0691d6e2017-01-05 10:08:52 -0500424 c->message_bounce = 0; /* don't bounce this message */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 vl_msg_api_config(c);} while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400426
427 foreach_vlib_api_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428#undef _
429
Dave Barach371e4e12016-07-08 09:38:52 -0400430 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431}
432
433#define foreach_histogram_bucket \
434_(400) \
435_(200) \
436_(100) \
437_(10)
438
Dave Barach371e4e12016-07-08 09:38:52 -0400439typedef enum
440{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441#define _(n) SLEEP_##n##_US,
Dave Barach371e4e12016-07-08 09:38:52 -0400442 foreach_histogram_bucket
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443#undef _
444 SLEEP_N_BUCKETS,
445} histogram_index_t;
446
447static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
448
Dave Barach371e4e12016-07-08 09:38:52 -0400449static void memclnt_queue_callback (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
Dave Barach987e11d2017-02-27 13:10:27 -0500451/*
452 * Callback to send ourselves a plugin numbering-space trace msg
453 */
454static void
455send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
456{
457 vl_api_trace_plugin_msg_ids_t *mp;
458 api_main_t *am = &api_main;
459 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
460 unix_shared_memory_queue_t *q;
461
462 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
463 memset (mp, 0, sizeof (*mp));
464
465 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
466 strncpy ((char *) mp->plugin_name, (char *) name,
467 sizeof (mp->plugin_name) - 1);
468 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
469 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
470
471 q = shmem_hdr->vl_input_queue;
472
473 vl_msg_api_send_shmem (q, (u8 *) & mp);
474}
475
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476static uword
477memclnt_process (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400478 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479{
Dave Barach371e4e12016-07-08 09:38:52 -0400480 uword mp;
481 vl_shmem_hdr_t *shm;
482 unix_shared_memory_queue_t *q;
483 clib_error_t *e;
484 int rv;
485 api_main_t *am = &api_main;
486 f64 dead_client_scan_time;
487 f64 sleep_time, start_time;
488 f64 vector_rate;
Dave Barach987e11d2017-02-27 13:10:27 -0500489 int i;
Dave Barach371e4e12016-07-08 09:38:52 -0400490
491 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
492
493 if ((rv = memory_api_init (am->region_name)) < 0)
494 {
495 clib_warning ("memory_api_init returned %d, wait for godot...", rv);
496 vlib_process_suspend (vm, 1e70);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 }
498
Dave Barach371e4e12016-07-08 09:38:52 -0400499 shm = am->shmem_hdr;
500 ASSERT (shm);
501 q = shm->vl_input_queue;
502 ASSERT (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
Dave Barach371e4e12016-07-08 09:38:52 -0400504 e = vlib_call_init_exit_functions
505 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
506 if (e)
507 clib_error_report (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Dave Barach371e4e12016-07-08 09:38:52 -0400509 sleep_time = 20.0;
510 dead_client_scan_time = vlib_time_now (vm) + 20.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
Dave Barach987e11d2017-02-27 13:10:27 -0500512 /*
513 * Send plugin message range messages for each plugin we loaded
514 */
515 for (i = 0; i < vec_len (am->msg_ranges); i++)
516 {
517 vl_api_msg_range_t *rp = am->msg_ranges + i;
518 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
519 rp->last_msg_id);
520 }
521
Dave Barach371e4e12016-07-08 09:38:52 -0400522 /* $$$ pay attention to frame size, control CPU usage */
523 while (1)
524 {
525 uword event_type __attribute__ ((unused));
526 i8 *headp;
527 int need_broadcast;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Dave Barach371e4e12016-07-08 09:38:52 -0400529 /*
530 * There's a reason for checking the queue before
531 * sleeping. If the vlib application crashes, it's entirely
532 * possible for a client to enqueue a connect request
533 * during the process restart interval.
534 *
535 * Unless some force of physics causes the new incarnation
536 * of the application to process the request, the client will
537 * sit and wait for Godot...
538 */
539 vector_rate = vlib_last_vector_length_per_node (vm);
540 start_time = vlib_time_now (vm);
541 while (1)
542 {
543 pthread_mutex_lock (&q->mutex);
544 if (q->cursize == 0)
545 {
546 vm->api_queue_nonempty = 0;
547 pthread_mutex_unlock (&q->mutex);
548
549 if (TRACE_VLIB_MEMORY_QUEUE)
550 {
551 /* *INDENT-OFF* */
552 ELOG_TYPE_DECLARE (e) =
553 {
554 .format = "q-underflow: len %d",
555 .format_args = "i4",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 };
Dave Barach371e4e12016-07-08 09:38:52 -0400557 /* *INDENT-ON* */
558 struct
559 {
560 u32 len;
561 } *ed;
562 ed = ELOG_DATA (&vm->elog_main, e);
563 ed->len = 0;
564 }
565 sleep_time = 20.0;
566 break;
567 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
Dave Barach371e4e12016-07-08 09:38:52 -0400569 headp = (i8 *) (q->data + sizeof (uword) * q->head);
570 clib_memcpy (&mp, headp, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571
Dave Barach371e4e12016-07-08 09:38:52 -0400572 q->head++;
573 need_broadcast = (q->cursize == q->maxsize / 2);
574 q->cursize--;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Dave Barach371e4e12016-07-08 09:38:52 -0400576 if (PREDICT_FALSE (q->head == q->maxsize))
577 q->head = 0;
578 pthread_mutex_unlock (&q->mutex);
579 if (need_broadcast)
580 (void) pthread_cond_broadcast (&q->condvar);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Dave Barach371e4e12016-07-08 09:38:52 -0400582 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
Dave Barach371e4e12016-07-08 09:38:52 -0400584 /* Allow no more than 10us without a pause */
585 if (vlib_time_now (vm) > start_time + 10e-6)
586 {
587 int index = SLEEP_400_US;
588 if (vector_rate > 40.0)
589 sleep_time = 400e-6;
590 else if (vector_rate > 20.0)
591 {
592 index = SLEEP_200_US;
593 sleep_time = 200e-6;
594 }
595 else if (vector_rate >= 1.0)
596 {
597 index = SLEEP_100_US;
598 sleep_time = 100e-6;
599 }
600 else
601 {
602 index = SLEEP_10_US;
603 sleep_time = 10e-6;
604 }
605 vector_rate_histogram[index] += 1;
606 break;
607 }
608 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
Dave Barach371e4e12016-07-08 09:38:52 -0400610 event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time);
611 vm->queue_signal_pending = 0;
612 vlib_process_get_events (vm, 0 /* event_data */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
Dave Barach371e4e12016-07-08 09:38:52 -0400614 if (vlib_time_now (vm) > dead_client_scan_time)
615 {
616 vl_api_registration_t **regpp;
617 vl_api_registration_t *regp;
618 unix_shared_memory_queue_t *q;
619 static u32 *dead_indices;
620 static u32 *confused_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
Dave Barach371e4e12016-07-08 09:38:52 -0400622 vec_reset_length (dead_indices);
623 vec_reset_length (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
Dave Barach371e4e12016-07-08 09:38:52 -0400625 /* *INDENT-OFF* */
626 pool_foreach (regpp, am->vl_clients,
627 ({
628 regp = *regpp;
629 if (regp)
630 {
631 q = regp->vl_input_queue;
632 if (kill (q->consumer_pid, 0) < 0)
633 {
634 vec_add1(dead_indices, regpp - am->vl_clients);
635 }
636 }
637 else
638 {
639 clib_warning ("NULL client registration index %d",
640 regpp - am->vl_clients);
641 vec_add1 (confused_indices, regpp - am->vl_clients);
642 }
643 }));
644 /* *INDENT-ON* */
645 /* This should "never happen," but if it does, fix it... */
646 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
647 {
648 int i;
649 for (i = 0; i < vec_len (confused_indices); i++)
650 {
651 pool_put_index (am->vl_clients, confused_indices[i]);
652 }
653 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654
Dave Barach371e4e12016-07-08 09:38:52 -0400655 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
656 {
657 int i;
658 svm_region_t *svm;
659 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660
Dave Barach371e4e12016-07-08 09:38:52 -0400661 /* Allow the application to clean up its registrations */
662 for (i = 0; i < vec_len (dead_indices); i++)
663 {
664 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
665 if (regpp)
666 {
667 u32 handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Dave Barach371e4e12016-07-08 09:38:52 -0400669 handle = vl_msg_api_handle_from_index_and_epoch
670 (dead_indices[i], shm->application_restarts);
Dave Barachb64e4e22017-03-14 09:10:56 -0400671 (void) call_reaper_functions (handle);
Dave Barach371e4e12016-07-08 09:38:52 -0400672 }
673 }
674
675 svm = am->vlib_rp;
676 pthread_mutex_lock (&svm->mutex);
677 oldheap = svm_push_data_heap (svm);
678
679 for (i = 0; i < vec_len (dead_indices); i++)
680 {
681 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
682 if (regpp)
683 {
684 /* Poison the old registration */
685 memset (*regpp, 0xF3, sizeof (**regpp));
686 clib_mem_free (*regpp);
687 /* no dangling references, please */
688 *regpp = 0;
689 }
690 else
691 {
692 svm_pop_heap (oldheap);
693 clib_warning ("Duplicate free, client index %d",
694 regpp - am->vl_clients);
695 oldheap = svm_push_data_heap (svm);
696 }
697 }
698
699 svm_client_scan_this_region_nolock (am->vlib_rp);
700
701 pthread_mutex_unlock (&svm->mutex);
702 svm_pop_heap (oldheap);
703 for (i = 0; i < vec_len (dead_indices); i++)
704 pool_put_index (am->vl_clients, dead_indices[i]);
705 }
706
707 dead_client_scan_time = vlib_time_now (vm) + 20.0;
708 }
709
710 if (TRACE_VLIB_MEMORY_QUEUE)
711 {
712 /* *INDENT-OFF* */
713 ELOG_TYPE_DECLARE (e) = {
714 .format = "q-awake: len %d",
715 .format_args = "i4",
716 };
717 /* *INDENT-ON* */
718 struct
719 {
720 u32 len;
721 } *ed;
722 ed = ELOG_DATA (&vm->elog_main, e);
723 ed->len = q->cursize;
724 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 }
726
Dave Barach371e4e12016-07-08 09:38:52 -0400727 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728}
729
730static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400731vl_api_show_histogram_command (vlib_main_t * vm,
732 unformat_input_t * input,
733 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734{
Dave Barach371e4e12016-07-08 09:38:52 -0400735 u64 total_counts = 0;
736 int i;
737
738 for (i = 0; i < SLEEP_N_BUCKETS; i++)
739 {
740 total_counts += vector_rate_histogram[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 }
742
Dave Barach371e4e12016-07-08 09:38:52 -0400743 if (total_counts == 0)
744 {
745 vlib_cli_output (vm, "No control-plane activity.");
746 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 }
748
749#define _(n) \
750 do { \
751 f64 percent; \
752 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
753 / (f64) total_counts; \
754 percent *= 100.0; \
755 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
756 vector_rate_histogram[SLEEP_##n##_US], \
757 percent); \
758 } while (0);
Dave Barach371e4e12016-07-08 09:38:52 -0400759 foreach_histogram_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760#undef _
761
Dave Barach371e4e12016-07-08 09:38:52 -0400762 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763}
764
Dave Barach371e4e12016-07-08 09:38:52 -0400765/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) = {
Calvin16649372016-07-28 13:52:05 -0400767 .path = "show api histogram",
768 .short_help = "show api histogram",
769 .function = vl_api_show_histogram_command,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770};
Dave Barach371e4e12016-07-08 09:38:52 -0400771/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
773static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400774vl_api_clear_histogram_command (vlib_main_t * vm,
775 unformat_input_t * input,
776 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777{
Dave Barach371e4e12016-07-08 09:38:52 -0400778 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779
Dave Barach371e4e12016-07-08 09:38:52 -0400780 for (i = 0; i < SLEEP_N_BUCKETS; i++)
781 vector_rate_histogram[i] = 0;
782 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783}
784
Dave Barach371e4e12016-07-08 09:38:52 -0400785/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) = {
787 .path = "clear api histogram",
788 .short_help = "clear api histogram",
789 .function = vl_api_clear_histogram_command,
790};
Dave Barach371e4e12016-07-08 09:38:52 -0400791/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792
793
Dave Barach371e4e12016-07-08 09:38:52 -0400794/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795VLIB_REGISTER_NODE (memclnt_node,static) = {
796 .function = memclnt_process,
797 .type = VLIB_NODE_TYPE_PROCESS,
798 .name = "api-rx-from-ring",
799 .state = VLIB_NODE_STATE_DISABLED,
800};
Dave Barach371e4e12016-07-08 09:38:52 -0400801/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802
Dave Barach371e4e12016-07-08 09:38:52 -0400803static void
804memclnt_queue_callback (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805{
Dave Barach371e4e12016-07-08 09:38:52 -0400806 static volatile int *cursizep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
Dave Barach16c75df2016-05-31 14:05:46 -0400808 if (PREDICT_FALSE (cursizep == 0))
809 {
810 api_main_t *am = &api_main;
811 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barach371e4e12016-07-08 09:38:52 -0400812 unix_shared_memory_queue_t *q;
813
Dave Barach16c75df2016-05-31 14:05:46 -0400814 if (shmem_hdr == 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400815 return;
816
Dave Barach16c75df2016-05-31 14:05:46 -0400817 q = shmem_hdr->vl_input_queue;
818 if (q == 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400819 return;
Dave Barach16c75df2016-05-31 14:05:46 -0400820 cursizep = &q->cursize;
821 }
Dave Barach371e4e12016-07-08 09:38:52 -0400822
Dave Barach16c75df2016-05-31 14:05:46 -0400823 if (*cursizep >= 1)
824 {
825 vm->queue_signal_pending = 1;
826 vm->api_queue_nonempty = 1;
Dave Barach371e4e12016-07-08 09:38:52 -0400827 vlib_process_signal_event (vm, memclnt_node.index,
828 /* event_type */ 0, /* event_data */ 0);
Dave Barach16c75df2016-05-31 14:05:46 -0400829 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830}
831
Dave Barach371e4e12016-07-08 09:38:52 -0400832void
833vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834{
Dave Barach371e4e12016-07-08 09:38:52 -0400835 vlib_node_set_state (vm, memclnt_node.index,
836 (enable
837 ? VLIB_NODE_STATE_POLLING
838 : VLIB_NODE_STATE_DISABLED));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839}
840
841static uword
842api_rx_from_node (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400843 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844{
Dave Barach371e4e12016-07-08 09:38:52 -0400845 uword n_packets = frame->n_vectors;
846 uword n_left_from;
847 u32 *from;
848 static u8 *long_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Dave Barach371e4e12016-07-08 09:38:52 -0400850 vec_validate (long_msg, 4095);
851 n_left_from = frame->n_vectors;
852 from = vlib_frame_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853
Dave Barach371e4e12016-07-08 09:38:52 -0400854 while (n_left_from > 0)
855 {
856 u32 bi0;
857 vlib_buffer_t *b0;
858 void *msg;
859 uword msg_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
Dave Barach371e4e12016-07-08 09:38:52 -0400861 bi0 = from[0];
862 b0 = vlib_get_buffer (vm, bi0);
863 from += 1;
864 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Dave Barach371e4e12016-07-08 09:38:52 -0400866 msg = b0->data + b0->current_data;
867 msg_len = b0->current_length;
868 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
869 {
870 ASSERT (long_msg != 0);
871 _vec_len (long_msg) = 0;
872 vec_add (long_msg, msg, msg_len);
873 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
874 {
875 b0 = vlib_get_buffer (vm, b0->next_buffer);
876 msg = b0->data + b0->current_data;
877 msg_len = b0->current_length;
878 vec_add (long_msg, msg, msg_len);
879 }
880 msg = long_msg;
881 }
882 vl_msg_api_handler_no_trace_no_free (msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883 }
884
Dave Barach371e4e12016-07-08 09:38:52 -0400885 /* Free what we've been given. */
886 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887
Dave Barach371e4e12016-07-08 09:38:52 -0400888 return n_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889}
890
Dave Barach371e4e12016-07-08 09:38:52 -0400891/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
893 .function = api_rx_from_node,
894 .type = VLIB_NODE_TYPE_INTERNAL,
895 .vector_size = 4,
896 .name = "api-rx-from-node",
897};
Dave Barach371e4e12016-07-08 09:38:52 -0400898/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899
900static clib_error_t *
901setup_memclnt_exit (vlib_main_t * vm)
902{
Dave Barach371e4e12016-07-08 09:38:52 -0400903 atexit (vl_unmap_shmem);
904 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905}
906
907VLIB_INIT_FUNCTION (setup_memclnt_exit);
908
909
910static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400911vl_api_ring_command (vlib_main_t * vm,
912 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913{
Dave Barach371e4e12016-07-08 09:38:52 -0400914 int i;
915 ring_alloc_t *ap;
916 vl_shmem_hdr_t *shmem_hdr;
917 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918
Dave Barach371e4e12016-07-08 09:38:52 -0400919 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920
Dave Barach371e4e12016-07-08 09:38:52 -0400921 if (shmem_hdr == 0)
922 {
923 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
924 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 }
926
Dave Barach371e4e12016-07-08 09:38:52 -0400927 vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
928 "Owner", "Size", "Nitems", "Hits", "Misses");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929
Dave Barach371e4e12016-07-08 09:38:52 -0400930 ap = shmem_hdr->vl_rings;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931
Dave Barach371e4e12016-07-08 09:38:52 -0400932 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
933 {
934 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
935 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
936 ap++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 }
938
Dave Barach371e4e12016-07-08 09:38:52 -0400939 ap = shmem_hdr->client_rings;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
Dave Barach371e4e12016-07-08 09:38:52 -0400941 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
942 {
943 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
944 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
945 ap++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 }
947
Dave Barach371e4e12016-07-08 09:38:52 -0400948 vlib_cli_output (vm, "%d ring miss fallback allocations\n",
949 am->ring_misses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
Dave Barach842b9c52017-01-09 15:54:00 -0500951 vlib_cli_output
952 (vm, "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
953 shmem_hdr->application_restarts,
954 shmem_hdr->restart_reclaims, shmem_hdr->garbage_collects);
Dave Barach371e4e12016-07-08 09:38:52 -0400955 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956}
957
Dave Barach371e4e12016-07-08 09:38:52 -0400958void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
959 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960
Dave Barach371e4e12016-07-08 09:38:52 -0400961void
962dump_socket_clients (vlib_main_t * vm, api_main_t * am)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963{
964}
965
966static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -0400967vl_api_client_command (vlib_main_t * vm,
968 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969{
Dave Barach371e4e12016-07-08 09:38:52 -0400970 vl_api_registration_t **regpp, *regp;
971 unix_shared_memory_queue_t *q;
972 char *health;
973 api_main_t *am = &api_main;
974 u32 *confused_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
Dave Barach371e4e12016-07-08 09:38:52 -0400976 if (!pool_elts (am->vl_clients))
977 goto socket_clients;
978 vlib_cli_output (vm, "Shared memory clients");
979 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
980 "Name", "PID", "Queue Length", "Queue VA", "Health");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981
Dave Barach371e4e12016-07-08 09:38:52 -0400982 /* *INDENT-OFF* */
983 pool_foreach (regpp, am->vl_clients,
984 ({
985 regp = *regpp;
986
987 if (regp)
988 {
989 q = regp->vl_input_queue;
990 if (kill (q->consumer_pid, 0) < 0)
991 {
992 health = "DEAD";
993 }
994 else
995 {
996 health = "alive";
997 }
998 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
999 regp->name, q->consumer_pid, q->cursize,
1000 q, health);
1001 }
1002 else
1003 {
1004 clib_warning ("NULL client registration index %d",
1005 regpp - am->vl_clients);
1006 vec_add1 (confused_indices, regpp - am->vl_clients);
1007 }
1008 }));
1009 /* *INDENT-ON* */
1010
1011 /* This should "never happen," but if it does, fix it... */
1012 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
1013 {
1014 int i;
1015 for (i = 0; i < vec_len (confused_indices); i++)
1016 {
1017 pool_put_index (am->vl_clients, confused_indices[i]);
1018 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019 }
Dave Barach371e4e12016-07-08 09:38:52 -04001020 vec_free (confused_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
Dave Barach371e4e12016-07-08 09:38:52 -04001022 if (am->missing_clients)
1023 vlib_cli_output (vm, "%u messages with missing clients",
1024 am->missing_clients);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025socket_clients:
Dave Barach371e4e12016-07-08 09:38:52 -04001026 dump_socket_clients (vm, am);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027
Dave Barach371e4e12016-07-08 09:38:52 -04001028 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029}
1030
Calvin16649372016-07-28 13:52:05 -04001031static clib_error_t *
1032vl_api_status_command (vlib_main_t * vm,
1033 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1034{
1035 api_main_t *am = &api_main;
1036
1037 // check if rx_trace and tx_trace are not null pointers
1038
1039 if (am->rx_trace == 0)
1040 {
1041 vlib_cli_output (vm, "RX Trace disabled\n");
1042 }
1043 else
1044 {
1045 if (am->rx_trace->enabled == 0)
1046 vlib_cli_output (vm, "RX Trace disabled\n");
1047 else
1048 vlib_cli_output (vm, "RX Trace enabled\n");
1049 }
1050
1051 if (am->tx_trace == 0)
1052 {
1053 vlib_cli_output (vm, "TX Trace disabled\n");
1054 }
1055 else
1056 {
1057 if (am->tx_trace->enabled == 0)
1058 vlib_cli_output (vm, "TX Trace disabled\n");
1059 else
1060 vlib_cli_output (vm, "TX Trace enabled\n");
1061 }
1062
1063 return 0;
1064}
1065
Dave Barach371e4e12016-07-08 09:38:52 -04001066/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067VLIB_CLI_COMMAND (cli_show_api_command, static) = {
1068 .path = "show api",
1069 .short_help = "Show API information",
1070};
Dave Barach371e4e12016-07-08 09:38:52 -04001071/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
Dave Barach371e4e12016-07-08 09:38:52 -04001073/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = {
1075 .path = "show api ring-stats",
1076 .short_help = "Message ring statistics",
1077 .function = vl_api_ring_command,
1078};
Dave Barach371e4e12016-07-08 09:38:52 -04001079/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080
Dave Barach371e4e12016-07-08 09:38:52 -04001081/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = {
1083 .path = "show api clients",
1084 .short_help = "Client information",
1085 .function = vl_api_client_command,
1086};
Dave Barach371e4e12016-07-08 09:38:52 -04001087/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088
Calvin16649372016-07-28 13:52:05 -04001089/* *INDENT-OFF* */
1090VLIB_CLI_COMMAND (cli_show_api_status_command, static) = {
1091 .path = "show api status",
1092 .short_help = "Show API trace status",
1093 .function = vl_api_status_command,
1094};
1095/* *INDENT-ON* */
1096
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001098vl_api_message_table_command (vlib_main_t * vm,
1099 unformat_input_t * input,
1100 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101{
Dave Barach371e4e12016-07-08 09:38:52 -04001102 api_main_t *am = &api_main;
1103 int i;
1104 int verbose = 0;
1105
1106 if (unformat (input, "verbose"))
1107 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108
1109
Dave Barach371e4e12016-07-08 09:38:52 -04001110 if (verbose == 0)
1111 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1112 else
1113 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1114 "MP-safe");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115
Dave Barach371e4e12016-07-08 09:38:52 -04001116 for (i = 1; i < vec_len (am->msg_names); i++)
1117 {
1118 if (verbose == 0)
1119 {
1120 vlib_cli_output (vm, "%-4d %s", i,
1121 am->msg_names[i] ? am->msg_names[i] :
1122 " [no handler]");
1123 }
1124 else
1125 {
1126 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1127 am->msg_names[i] ? am->msg_names[i] :
1128 " [no handler]", am->message_bounce[i],
1129 am->is_mp_safe[i]);
1130 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 }
1132
Dave Barach371e4e12016-07-08 09:38:52 -04001133 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134}
1135
Dave Barach371e4e12016-07-08 09:38:52 -04001136/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) = {
1138 .path = "show api message-table",
1139 .short_help = "Message Table",
1140 .function = vl_api_message_table_command,
1141};
Dave Barach371e4e12016-07-08 09:38:52 -04001142/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001145vl_api_trace_command (vlib_main_t * vm,
1146 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147{
Dave Barach371e4e12016-07-08 09:38:52 -04001148 u32 nitems = 1024;
1149 vl_api_trace_which_t which = VL_API_TRACE_RX;
Dave Barach371e4e12016-07-08 09:38:52 -04001150 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
Dave Barach371e4e12016-07-08 09:38:52 -04001152 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1153 {
1154 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1155 goto configure;
1156 else if (unformat (input, "tx nitems %u", &nitems)
1157 || unformat (input, "tx"))
1158 {
1159 which = VL_API_TRACE_RX;
1160 goto configure;
1161 }
1162 else if (unformat (input, "on rx"))
1163 {
1164 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1165 }
1166 else if (unformat (input, "on tx"))
1167 {
1168 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1169 }
1170 else if (unformat (input, "on"))
1171 {
1172 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1173 }
1174 else if (unformat (input, "off"))
1175 {
1176 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1177 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1178 }
1179 else if (unformat (input, "free"))
1180 {
1181 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1182 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1183 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1184 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1185 }
Dave Barach371e4e12016-07-08 09:38:52 -04001186 else if (unformat (input, "debug on"))
1187 {
1188 am->msg_print_flag = 1;
1189 }
1190 else if (unformat (input, "debug off"))
1191 {
1192 am->msg_print_flag = 0;
1193 }
1194 else
1195 return clib_error_return (0, "unknown input `%U'",
1196 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197 }
Dave Barach371e4e12016-07-08 09:38:52 -04001198 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Dave Barach371e4e12016-07-08 09:38:52 -04001200configure:
1201 if (vl_msg_api_trace_configure (am, which, nitems))
1202 {
1203 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1204 which, nitems);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205 }
1206
Dave Barach371e4e12016-07-08 09:38:52 -04001207 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208}
1209
Dave Barach371e4e12016-07-08 09:38:52 -04001210/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211VLIB_CLI_COMMAND (trace, static) = {
1212 .path = "set api-trace",
1213 .short_help = "API trace",
1214 .function = vl_api_trace_command,
1215};
Dave Barach371e4e12016-07-08 09:38:52 -04001216/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
1218clib_error_t *
1219vlibmemory_init (vlib_main_t * vm)
1220{
Dave Barach309bef22016-01-22 16:09:52 -05001221 api_main_t *am = &api_main;
Dave Barachb3d93da2016-08-03 14:34:38 -04001222 svm_map_region_args_t _a, *a = &_a;
Dave Barachc3799992016-08-15 11:12:27 -04001223
Dave Barachb3d93da2016-08-03 14:34:38 -04001224 memset (a, 0, sizeof (*a));
1225 a->root_path = am->root_path;
1226 a->name = SVM_GLOBAL_REGION_NAME;
Dave Barachc3799992016-08-15 11:12:27 -04001227 a->baseva = (am->global_baseva != 0) ?
Dave Barachb3d93da2016-08-03 14:34:38 -04001228 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1229 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1230 a->flags = SVM_FLAGS_NODATA;
1231 a->uid = am->api_uid;
1232 a->gid = am->api_gid;
Dave Barachc3799992016-08-15 11:12:27 -04001233 a->pvt_heap_size =
1234 (am->global_pvt_heap_size !=
1235 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
Dave Barachb3d93da2016-08-03 14:34:38 -04001236
1237 svm_region_init_args (a);
Dave Barach309bef22016-01-22 16:09:52 -05001238 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239}
1240
1241VLIB_INIT_FUNCTION (vlibmemory_init);
1242
Dave Barach371e4e12016-07-08 09:38:52 -04001243void
Neale Rannse72be392017-04-26 13:59:20 -07001244vl_set_memory_region_name (const char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001245{
Dave Barach371e4e12016-07-08 09:38:52 -04001246 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247
Dave Barach371e4e12016-07-08 09:38:52 -04001248 am->region_name = name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249}
1250
Dave Barach371e4e12016-07-08 09:38:52 -04001251static int
1252range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253{
Dave Barach371e4e12016-07-08 09:38:52 -04001254 int len0, len1, clen;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255
Dave Barach371e4e12016-07-08 09:38:52 -04001256 len0 = vec_len (a0->name);
1257 len1 = vec_len (a1->name);
1258 clen = len0 < len1 ? len0 : len1;
1259 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260}
1261
Dave Barach371e4e12016-07-08 09:38:52 -04001262static u8 *
1263format_api_msg_range (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264{
Dave Barach371e4e12016-07-08 09:38:52 -04001265 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
Dave Barach371e4e12016-07-08 09:38:52 -04001267 if (rp == 0)
1268 s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID");
1269 else
1270 s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id,
1271 rp->last_msg_id);
1272
1273 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274}
1275
1276static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001277vl_api_show_plugin_command (vlib_main_t * vm,
1278 unformat_input_t * input,
1279 vlib_cli_command_t * cli_cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280{
Dave Barach371e4e12016-07-08 09:38:52 -04001281 api_main_t *am = &api_main;
1282 vl_api_msg_range_t *rp = 0;
1283 int i;
1284
1285 if (vec_len (am->msg_ranges) == 0)
1286 {
1287 vlib_cli_output (vm, "No plugin API message ranges configured...");
1288 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290
Dave Barach371e4e12016-07-08 09:38:52 -04001291 rp = vec_dup (am->msg_ranges);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292
Dave Barach371e4e12016-07-08 09:38:52 -04001293 vec_sort_with_function (rp, range_compare);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294
Dave Barach371e4e12016-07-08 09:38:52 -04001295 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1296 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1297
1298 for (i = 0; i < vec_len (rp); i++)
1299 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1300
Dave Barachdfbee412017-03-02 18:24:10 -05001301 vec_free (rp);
1302
Dave Barach371e4e12016-07-08 09:38:52 -04001303 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304}
1305
Dave Barach371e4e12016-07-08 09:38:52 -04001306/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) = {
1308 .path = "show api plugin",
1309 .short_help = "show api plugin",
1310 .function = vl_api_show_plugin_command,
1311};
Dave Barach371e4e12016-07-08 09:38:52 -04001312/* *INDENT-ON* */
Dave Barach4e281a42015-12-14 11:13:29 -05001313
Dave Barach371e4e12016-07-08 09:38:52 -04001314static void
1315vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001316{
Dave Barach11b8dbf2017-04-24 10:46:54 -04001317 vl_api_rpc_call_reply_t *rmp;
Dave Barach371e4e12016-07-08 09:38:52 -04001318 int (*fp) (void *);
Dave Barach4e281a42015-12-14 11:13:29 -05001319 i32 rv = 0;
Dave Barach371e4e12016-07-08 09:38:52 -04001320 vlib_main_t *vm = vlib_get_main ();
Dave Barach4e281a42015-12-14 11:13:29 -05001321
1322 if (mp->function == 0)
1323 {
1324 rv = -1;
1325 clib_warning ("rpc NULL function pointer");
1326 }
Dave Barach371e4e12016-07-08 09:38:52 -04001327
Dave Barach4e281a42015-12-14 11:13:29 -05001328 else
1329 {
1330 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001331 vlib_worker_thread_barrier_sync (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001332
Dave Barach371e4e12016-07-08 09:38:52 -04001333 fp = uword_to_pointer (mp->function, int (*)(void *));
1334 rv = fp (mp->data);
Dave Barach4e281a42015-12-14 11:13:29 -05001335
1336 if (mp->need_barrier_sync)
Dave Barach371e4e12016-07-08 09:38:52 -04001337 vlib_worker_thread_barrier_release (vm);
Dave Barach4e281a42015-12-14 11:13:29 -05001338 }
1339
1340 if (mp->send_reply)
1341 {
Dave Barach371e4e12016-07-08 09:38:52 -04001342 unix_shared_memory_queue_t *q =
1343 vl_api_client_index_to_input_queue (mp->client_index);
Dave Barach4e281a42015-12-14 11:13:29 -05001344 if (q)
Dave Barach371e4e12016-07-08 09:38:52 -04001345 {
1346 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barach11b8dbf2017-04-24 10:46:54 -04001347 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
Dave Barach371e4e12016-07-08 09:38:52 -04001348 rmp->context = mp->context;
1349 rmp->retval = rv;
1350 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1351 }
Dave Barach4e281a42015-12-14 11:13:29 -05001352 }
1353 if (mp->multicast)
1354 {
1355 clib_warning ("multicast not yet implemented...");
1356 }
1357}
1358
Dave Barach371e4e12016-07-08 09:38:52 -04001359static void
Dave Barach11b8dbf2017-04-24 10:46:54 -04001360vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
Dave Barach4e281a42015-12-14 11:13:29 -05001361{
Dave Barach371e4e12016-07-08 09:38:52 -04001362 clib_warning ("unimplemented");
1363}
1364
1365void
1366vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1367{
1368 vl_api_rpc_call_t *mp;
Dave Barach4e281a42015-12-14 11:13:29 -05001369 api_main_t *am = &api_main;
1370 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Dave Barache9183632016-10-04 16:53:56 -04001371 unix_shared_memory_queue_t *q;
Dave Barach4e281a42015-12-14 11:13:29 -05001372
Dave Barache9183632016-10-04 16:53:56 -04001373 /* Main thread: call the function directly */
Damjan Marionf55f9b82017-05-10 21:06:28 +02001374 if (vlib_get_thread_index () == 0)
Dave Barache9183632016-10-04 16:53:56 -04001375 {
1376 vlib_main_t *vm = vlib_get_main ();
1377 void (*call_fp) (void *);
1378
1379 vlib_worker_thread_barrier_sync (vm);
1380
1381 call_fp = fp;
1382 call_fp (data);
1383
1384 vlib_worker_thread_barrier_release (vm);
1385 return;
1386 }
1387
1388 /* Any other thread, actually do an RPC call... */
Dave Barach4e281a42015-12-14 11:13:29 -05001389 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
Dave Barache9183632016-10-04 16:53:56 -04001390
Dave Barach4e281a42015-12-14 11:13:29 -05001391 memset (mp, 0, sizeof (*mp));
Damjan Marionf1213b82016-03-13 02:22:06 +01001392 clib_memcpy (mp->data, data, data_length);
Dave Barach4e281a42015-12-14 11:13:29 -05001393 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
Dave Barach371e4e12016-07-08 09:38:52 -04001394 mp->function = pointer_to_uword (fp);
Dave Barach4e281a42015-12-14 11:13:29 -05001395 mp->need_barrier_sync = 1;
Dave Barach371e4e12016-07-08 09:38:52 -04001396
Dave Barache9183632016-10-04 16:53:56 -04001397 /*
1398 * Use the "normal" control-plane mechanism for the main thread.
1399 * Well, almost. if the main input queue is full, we cannot
1400 * block. Otherwise, we can expect a barrier sync timeout.
1401 */
1402 q = shmem_hdr->vl_input_queue;
1403
1404 while (pthread_mutex_trylock (&q->mutex))
1405 vlib_worker_thread_barrier_check ();
1406
1407 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1408 {
1409 pthread_mutex_unlock (&q->mutex);
1410 vlib_worker_thread_barrier_check ();
1411 while (pthread_mutex_trylock (&q->mutex))
1412 vlib_worker_thread_barrier_check ();
1413 }
1414
1415 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1416
1417 pthread_mutex_unlock (&q->mutex);
Dave Barach4e281a42015-12-14 11:13:29 -05001418}
1419
Dave Barach987e11d2017-02-27 13:10:27 -05001420static void
1421vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1422{
Dave Barachdfbee412017-03-02 18:24:10 -05001423 api_main_t *am = &api_main;
1424 vl_api_msg_range_t *rp;
1425 uword *p;
Dave Barach987e11d2017-02-27 13:10:27 -05001426
Dave Barachdfbee412017-03-02 18:24:10 -05001427 /* Noop (except for tracing) during normal operation */
1428 if (am->replay_in_progress == 0)
1429 return;
1430
1431 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1432 if (p == 0)
1433 {
1434 clib_warning ("WARNING: traced plugin '%s' not in current image",
1435 mp->plugin_name);
1436 return;
1437 }
1438
1439 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1440 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1441 {
1442 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1443 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1444 rp->first_msg_id);
1445 }
1446
1447 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1448 {
1449 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1450 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1451 rp->last_msg_id);
1452 }
1453}
Dave Barach987e11d2017-02-27 13:10:27 -05001454
Dave Barach4e281a42015-12-14 11:13:29 -05001455#define foreach_rpc_api_msg \
1456_(RPC_CALL,rpc_call) \
Dave Barach11b8dbf2017-04-24 10:46:54 -04001457_(RPC_CALL_REPLY,rpc_call_reply)
Dave Barach4e281a42015-12-14 11:13:29 -05001458
Dave Barach987e11d2017-02-27 13:10:27 -05001459#define foreach_plugin_trace_msg \
1460_(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1461
Dave Barach4e281a42015-12-14 11:13:29 -05001462static clib_error_t *
Dave Barach371e4e12016-07-08 09:38:52 -04001463rpc_api_hookup (vlib_main_t * vm)
Dave Barach4e281a42015-12-14 11:13:29 -05001464{
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001465 api_main_t *am = &api_main;
Dave Barach4e281a42015-12-14 11:13:29 -05001466#define _(N,n) \
1467 vl_msg_api_set_handlers(VL_API_##N, #n, \
1468 vl_api_##n##_t_handler, \
1469 vl_noop_handler, \
1470 vl_noop_handler, \
1471 vl_api_##n##_t_print, \
Dave Barach371e4e12016-07-08 09:38:52 -04001472 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1473 foreach_rpc_api_msg;
Dave Barach4e281a42015-12-14 11:13:29 -05001474#undef _
Dave Barach987e11d2017-02-27 13:10:27 -05001475
1476#define _(N,n) \
1477 vl_msg_api_set_handlers(VL_API_##N, #n, \
1478 vl_api_##n##_t_handler, \
1479 vl_noop_handler, \
1480 vl_noop_handler, \
1481 vl_api_##n##_t_print, \
1482 sizeof(vl_api_##n##_t), 1 /* do trace */);
1483 foreach_plugin_trace_msg;
1484#undef _
Colin Tregenza Dancereb1ac172017-09-06 20:23:24 +01001485
1486 /* No reason to halt the parade to create a trace record... */
1487 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
1488
Dave Barach371e4e12016-07-08 09:38:52 -04001489 return 0;
Dave Barach4e281a42015-12-14 11:13:29 -05001490}
1491
Dave Barach371e4e12016-07-08 09:38:52 -04001492VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1493
Dave Barach80f54e22017-03-08 19:08:56 -05001494typedef enum
1495{
1496 DUMP,
1497 CUSTOM_DUMP,
1498 REPLAY,
1499 INITIALIZERS,
1500} vl_api_replay_t;
1501
1502u8 *
1503format_vl_msg_api_trace_status (u8 * s, va_list * args)
1504{
1505 api_main_t *am = va_arg (*args, api_main_t *);
1506 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
1507 vl_api_trace_t *tp;
1508 char *trace_name;
1509
1510 switch (which)
1511 {
1512 case VL_API_TRACE_TX:
1513 tp = am->tx_trace;
1514 trace_name = "TX trace";
1515 break;
1516
1517 case VL_API_TRACE_RX:
1518 tp = am->rx_trace;
1519 trace_name = "RX trace";
1520 break;
1521
1522 default:
1523 abort ();
1524 }
1525
1526 if (tp == 0)
1527 {
1528 s = format (s, "%s: not yet configured.\n", trace_name);
1529 return s;
1530 }
1531
1532 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
1533 trace_name, vec_len (tp->traces), tp->nitems,
1534 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
1535 return s;
1536}
1537
1538void vl_msg_api_custom_dump_configure (api_main_t * am)
1539 __attribute__ ((weak));
1540void
1541vl_msg_api_custom_dump_configure (api_main_t * am)
1542{
1543}
1544
1545static void
1546vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
1547 u32 first_index, u32 last_index,
1548 vl_api_replay_t which)
1549{
1550 vl_api_trace_file_header_t *hp;
1551 int i, fd;
1552 struct stat statb;
1553 size_t file_size;
1554 u8 *msg;
1555 u8 endian_swap_needed = 0;
1556 api_main_t *am = &api_main;
1557 u8 *tmpbuf = 0;
1558 u32 nitems;
1559 void **saved_print_handlers = 0;
1560
1561 fd = open ((char *) filename, O_RDONLY);
1562
1563 if (fd < 0)
1564 {
1565 vlib_cli_output (vm, "Couldn't open %s\n", filename);
1566 return;
1567 }
1568
1569 if (fstat (fd, &statb) < 0)
1570 {
1571 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
1572 close (fd);
1573 return;
1574 }
1575
1576 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
1577 {
1578 vlib_cli_output (vm, "File not plausible: %s\n", filename);
1579 close (fd);
1580 return;
1581 }
1582
1583 file_size = statb.st_size;
1584 file_size = (file_size + 4095) & ~(4096);
1585
1586 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1587
1588 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
1589 {
1590 vlib_cli_output (vm, "mmap failed: %s\n", filename);
1591 close (fd);
1592 return;
1593 }
1594 close (fd);
1595
1596 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
1597 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
1598 endian_swap_needed = 1;
1599
1600 if (endian_swap_needed)
1601 nitems = ntohl (hp->nitems);
1602 else
1603 nitems = hp->nitems;
1604
1605 if (last_index == (u32) ~ 0)
1606 {
1607 last_index = nitems - 1;
1608 }
1609
1610 if (first_index >= nitems || last_index >= nitems)
1611 {
1612 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
1613 first_index, last_index, nitems - 1);
1614 munmap (hp, file_size);
1615 return;
1616 }
1617 if (hp->wrapped)
1618 vlib_cli_output (vm,
1619 "Note: wrapped/incomplete trace, results may vary\n");
1620
1621 if (which == CUSTOM_DUMP)
1622 {
1623 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
1624 vl_msg_api_custom_dump_configure (am);
1625 }
1626
1627
1628 msg = (u8 *) (hp + 1);
1629
1630 for (i = 0; i < first_index; i++)
1631 {
1632 trace_cfg_t *cfgp;
1633 int size;
1634 u16 msg_id;
1635
1636 size = clib_host_to_net_u32 (*(u32 *) msg);
1637 msg += sizeof (u32);
1638
1639 if (clib_arch_is_little_endian)
1640 msg_id = ntohs (*((u16 *) msg));
1641 else
1642 msg_id = *((u16 *) msg);
1643
1644 cfgp = am->api_trace_cfg + msg_id;
1645 if (!cfgp)
1646 {
1647 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1648 munmap (hp, file_size);
1649 return;
1650 }
1651 msg += size;
1652 }
1653
1654 if (which == REPLAY)
1655 am->replay_in_progress = 1;
1656
1657 for (; i <= last_index; i++)
1658 {
1659 trace_cfg_t *cfgp;
1660 u16 *msg_idp;
1661 u16 msg_id;
1662 int size;
1663
1664 if (which == DUMP)
1665 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
1666
1667 size = clib_host_to_net_u32 (*(u32 *) msg);
1668 msg += sizeof (u32);
1669
1670 if (clib_arch_is_little_endian)
1671 msg_id = ntohs (*((u16 *) msg));
1672 else
1673 msg_id = *((u16 *) msg);
1674
1675 cfgp = am->api_trace_cfg + msg_id;
1676 if (!cfgp)
1677 {
1678 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1679 munmap (hp, file_size);
1680 vec_free (tmpbuf);
1681 am->replay_in_progress = 0;
1682 return;
1683 }
1684
1685 /* Copy the buffer (from the read-only mmap'ed file) */
1686 vec_validate (tmpbuf, size - 1 + sizeof (uword));
1687 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
1688 memset (tmpbuf, 0xf, sizeof (uword));
1689
1690 /*
1691 * Endian swap if needed. All msg data is supposed to be
1692 * in network byte order. All msg handlers are supposed to
1693 * know that. The generic message dumpers don't know that.
1694 * One could fix apigen, I suppose.
1695 */
1696 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
1697 {
1698 void (*endian_fp) (void *);
1699 if (msg_id >= vec_len (am->msg_endian_handlers)
1700 || (am->msg_endian_handlers[msg_id] == 0))
1701 {
1702 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
1703 munmap (hp, file_size);
1704 vec_free (tmpbuf);
1705 am->replay_in_progress = 0;
1706 return;
1707 }
1708 endian_fp = am->msg_endian_handlers[msg_id];
1709 (*endian_fp) (tmpbuf + sizeof (uword));
1710 }
1711
1712 /* msg_id always in network byte order */
1713 if (clib_arch_is_little_endian)
1714 {
1715 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
1716 *msg_idp = msg_id;
1717 }
1718
1719 switch (which)
1720 {
1721 case CUSTOM_DUMP:
1722 case DUMP:
1723 if (msg_id < vec_len (am->msg_print_handlers) &&
1724 am->msg_print_handlers[msg_id])
1725 {
1726 u8 *(*print_fp) (void *, void *);
1727
1728 print_fp = (void *) am->msg_print_handlers[msg_id];
1729 (*print_fp) (tmpbuf + sizeof (uword), vm);
1730 }
1731 else
1732 {
1733 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
1734 msg_id);
1735 break;
1736 }
1737 break;
1738
1739 case INITIALIZERS:
1740 if (msg_id < vec_len (am->msg_print_handlers) &&
1741 am->msg_print_handlers[msg_id])
1742 {
1743 u8 *s;
1744 int j;
1745 u8 *(*print_fp) (void *, void *);
1746
1747 print_fp = (void *) am->msg_print_handlers[msg_id];
1748
1749 vlib_cli_output (vm, "/*");
1750
1751 (*print_fp) (tmpbuf + sizeof (uword), vm);
1752 vlib_cli_output (vm, "*/\n");
1753
1754 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
1755 am->msg_names[msg_id], i,
1756 am->api_trace_cfg[msg_id].size);
1757
1758 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
1759 {
1760 if ((j & 7) == 0)
1761 s = format (s, "\n ");
1762 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
1763 }
1764 s = format (s, "\n};\n%c", 0);
1765 vlib_cli_output (vm, (char *) s);
1766 vec_free (s);
1767 }
1768 break;
1769
1770 case REPLAY:
1771 if (msg_id < vec_len (am->msg_print_handlers) &&
1772 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
1773 {
1774 void (*handler) (void *);
1775
1776 handler = (void *) am->msg_handlers[msg_id];
1777
1778 if (!am->is_mp_safe[msg_id])
1779 vl_msg_api_barrier_sync ();
1780 (*handler) (tmpbuf + sizeof (uword));
1781 if (!am->is_mp_safe[msg_id])
1782 vl_msg_api_barrier_release ();
1783 }
1784 else
1785 {
1786 if (cfgp->replay_enable)
1787 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
1788 msg_id);
1789 break;
1790 }
1791 break;
1792 }
1793
1794 _vec_len (tmpbuf) = 0;
1795 msg += size;
1796 }
1797
1798 if (saved_print_handlers)
1799 {
1800 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
1801 vec_len (am->msg_print_handlers) * sizeof (void *));
1802 vec_free (saved_print_handlers);
1803 }
1804
1805 munmap (hp, file_size);
1806 vec_free (tmpbuf);
1807 am->replay_in_progress = 0;
1808}
1809
1810static clib_error_t *
1811api_trace_command_fn (vlib_main_t * vm,
1812 unformat_input_t * input, vlib_cli_command_t * cmd)
1813{
1814 u32 nitems = 256 << 10;
1815 api_main_t *am = &api_main;
1816 vl_api_trace_which_t which = VL_API_TRACE_RX;
1817 u8 *filename;
1818 u32 first = 0;
1819 u32 last = (u32) ~ 0;
1820 FILE *fp;
1821 int rv;
1822
1823 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1824 {
1825 if (unformat (input, "on") || unformat (input, "enable"))
1826 {
1827 if (unformat (input, "nitems %d", &nitems))
1828 ;
1829 vl_msg_api_trace_configure (am, which, nitems);
1830 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1831 }
1832 else if (unformat (input, "off"))
1833 {
1834 vl_msg_api_trace_onoff (am, which, 0);
1835 }
1836 else if (unformat (input, "save %s", &filename))
1837 {
1838 u8 *chroot_filename;
1839 if (strstr ((char *) filename, "..")
1840 || index ((char *) filename, '/'))
1841 {
1842 vlib_cli_output (vm, "illegal characters in filename '%s'",
1843 filename);
1844 return 0;
1845 }
1846
1847 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1848
1849 vec_free (filename);
1850
1851 fp = fopen ((char *) chroot_filename, "w");
1852 if (fp == NULL)
1853 {
1854 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
1855 return 0;
1856 }
1857 rv = vl_msg_api_trace_save (am, which, fp);
1858 fclose (fp);
1859 if (rv == -1)
1860 vlib_cli_output (vm, "API Trace data not present\n");
1861 else if (rv == -2)
1862 vlib_cli_output (vm, "File for writing is closed\n");
1863 else if (rv == -10)
1864 vlib_cli_output (vm, "Error while writing header to file\n");
1865 else if (rv == -11)
1866 vlib_cli_output (vm, "Error while writing trace to file\n");
1867 else if (rv == -12)
1868 vlib_cli_output (vm,
1869 "Error while writing end of buffer trace to file\n");
1870 else if (rv == -13)
1871 vlib_cli_output (vm,
1872 "Error while writing start of buffer trace to file\n");
1873 else if (rv < 0)
1874 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
1875 else
1876 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
1877 vec_free (chroot_filename);
1878 }
1879 else if (unformat (input, "dump %s", &filename))
1880 {
1881 vl_msg_api_process_file (vm, filename, first, last, DUMP);
1882 }
1883 else if (unformat (input, "custom-dump %s", &filename))
1884 {
1885 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
1886 }
1887 else if (unformat (input, "replay %s", &filename))
1888 {
1889 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
1890 }
1891 else if (unformat (input, "initializers %s", &filename))
1892 {
1893 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
1894 }
1895 else if (unformat (input, "tx"))
1896 {
1897 which = VL_API_TRACE_TX;
1898 }
1899 else if (unformat (input, "first %d", &first))
1900 {
1901 ;
1902 }
1903 else if (unformat (input, "last %d", &last))
1904 {
1905 ;
1906 }
1907 else if (unformat (input, "status"))
1908 {
1909 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
1910 am, which);
1911 }
1912 else if (unformat (input, "free"))
1913 {
1914 vl_msg_api_trace_onoff (am, which, 0);
1915 vl_msg_api_trace_free (am, which);
1916 }
1917 else if (unformat (input, "post-mortem-on"))
1918 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
1919 else if (unformat (input, "post-mortem-off"))
1920 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
1921 else
1922 return clib_error_return (0, "unknown input `%U'",
1923 format_unformat_error, input);
1924 }
1925 return 0;
1926}
1927
1928/* *INDENT-OFF* */
1929VLIB_CLI_COMMAND (api_trace_command, static) = {
1930 .path = "api trace",
1931 .short_help =
1932 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
1933 .function = api_trace_command_fn,
1934};
1935/* *INDENT-ON* */
1936
1937static clib_error_t *
1938api_config_fn (vlib_main_t * vm, unformat_input_t * input)
1939{
1940 u32 nitems = 256 << 10;
1941 vl_api_trace_which_t which = VL_API_TRACE_RX;
1942 api_main_t *am = &api_main;
1943
1944 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1945 {
1946 if (unformat (input, "on") || unformat (input, "enable"))
1947 {
1948 if (unformat (input, "nitems %d", &nitems))
1949 ;
1950 vl_msg_api_trace_configure (am, which, nitems);
1951 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1952 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
1953 }
1954 else
1955 return clib_error_return (0, "unknown input `%U'",
1956 format_unformat_error, input);
1957 }
1958 return 0;
1959}
1960
1961VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
1962
Dave Barach10d8cc62017-05-30 09:30:07 -04001963static clib_error_t *
1964api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
1965{
1966 api_main_t *am = &api_main;
1967 u32 nitems;
1968
1969 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1970 {
1971 if (unformat (input, "length %d", &nitems) ||
1972 (unformat (input, "len %d", &nitems)))
1973 {
1974 if (nitems >= 1024)
1975 am->vlib_input_queue_length = nitems;
1976 else
1977 clib_warning ("vlib input queue length %d too small, ignored",
1978 nitems);
1979 }
1980 else
1981 return clib_error_return (0, "unknown input `%U'",
1982 format_unformat_error, input);
1983 }
1984 return 0;
1985}
1986
1987VLIB_CONFIG_FUNCTION (api_queue_config_fn, "api-queue");
1988
Dave Barach371e4e12016-07-08 09:38:52 -04001989/*
1990 * fd.io coding-style-patch-verification: ON
1991 *
1992 * Local Variables:
1993 * eval: (c-set-style "gnu")
1994 * End:
1995 */