2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
24 #include <sys/types.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>
36 #include <vlib/vlib.h>
37 #include <vlib/unix/unix.h>
38 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
41 #define TRACE_VLIB_MEMORY_QUEUE 0
43 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
45 #define vl_typedefs /* define message structures */
46 #include <vlibmemory/vl_memory_api_h.h>
49 /* instantiate all the print functions we know about */
50 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
52 #include <vlibmemory/vl_memory_api_h.h>
56 vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
58 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);
67 vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
69 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);
76 vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
79 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
81 clib_host_to_net_u16 (a->first_msg_id),
82 clib_host_to_net_u16 (a->last_msg_id));
86 /* instantiate all the endian swap functions we know about */
88 #include <vlibmemory/vl_memory_api_h.h>
91 void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
92 __attribute__ ((weak));
95 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
100 clib_warning ("need to link against -lvlibsocket, msg not sent!");
104 vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
106 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
108 vl_socket_api_send (rp, elem);
112 vl_msg_api_send_shmem (rp->vl_input_queue, elem);
117 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
119 serialize_main_t _sm, *sm = &_sm;
121 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
123 serialize_open_vector (sm, vector);
125 /* serialize the count */
126 serialize_integer (sm, nmsg, sizeof (u32));
129 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
131 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
132 serialize_cstring (sm, (char *) hp->key);
136 return serialize_close_vector (sm);
140 * vl_api_memclnt_create_internal
144 vl_api_memclnt_create_internal (char *name, unix_shared_memory_queue_t * q)
146 vl_api_registration_t **regpp;
147 vl_api_registration_t *regp;
150 api_main_t *am = &api_main;
152 ASSERT (vlib_get_thread_index () == 0);
153 pool_get (am->vl_clients, regpp);
157 pthread_mutex_lock (&svm->mutex);
158 oldheap = svm_push_data_heap (svm);
159 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
162 memset (regp, 0, sizeof (*regp));
163 regp->registration_type = REGISTRATION_TYPE_SHMEM;
164 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
166 regp->vl_input_queue = q;
167 regp->name = format (0, "%s%c", name, 0);
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);
178 * vl_api_memclnt_create_t_handler
181 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
183 vl_api_registration_t **regpp;
184 vl_api_registration_t *regp;
185 vl_api_memclnt_create_reply_t *rp;
187 unix_shared_memory_queue_t *q;
190 api_main_t *am = &api_main;
191 u8 *serialized_message_table = 0;
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.
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.
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:
211 * convert pool index to
219 pool_get (am->vl_clients, regpp);
223 if (am->serialized_message_table_in_shmem == 0)
224 serialized_message_table = vl_api_serialize_message_table (am, 0);
226 pthread_mutex_lock (&svm->mutex);
227 oldheap = svm_push_data_heap (svm);
228 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
231 memset (regp, 0, sizeof (*regp));
232 regp->registration_type = REGISTRATION_TYPE_SHMEM;
233 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
235 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
238 regp->name = format (0, "%s", mp->name);
239 vec_add1 (regp->name, 0);
240 if (serialized_message_table)
241 am->serialized_message_table_in_shmem =
242 vec_dup (serialized_message_table);
244 pthread_mutex_unlock (&svm->mutex);
245 svm_pop_heap (oldheap);
247 vec_free (serialized_message_table);
249 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);
258 pointer_to_uword (am->serialized_message_table_in_shmem);
260 vl_msg_api_send_shmem (q, (u8 *) & rp);
264 call_reaper_functions (u32 client_index)
266 clib_error_t *error = 0;
267 _vl_msg_api_function_list_elt_t *i;
269 i = api_main.reaper_function_registrations;
272 error = i->f (client_index);
274 clib_error_report (error);
275 i = i->next_init_function;
281 * vl_api_memclnt_delete_t_handler
284 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
286 vl_api_registration_t **regpp;
287 vl_api_registration_t *regp;
288 vl_api_memclnt_delete_reply_t *rp;
291 api_main_t *am = &api_main;
292 u32 handle, client_index, epoch;
296 if (call_reaper_functions (handle))
299 epoch = vl_msg_api_handle_get_epoch (handle);
300 client_index = vl_msg_api_handle_get_index (handle);
302 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
305 ("Stale clnt delete index %d old epoch %d cur epoch %d",
307 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
311 regpp = am->vl_clients + client_index;
313 if (!pool_is_free (am->vl_clients, regpp))
318 /* $$$ check the input queue for e.g. punted sf's */
320 rp = vl_msg_api_alloc (sizeof (*rp));
321 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
322 rp->handle = mp->handle;
325 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
327 if (client_index != regp->vl_api_registration_pool_index)
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);
335 /* No dangling references, please */
338 pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
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);
350 clib_warning ("unknown client ID %d", mp->index);
355 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
357 vl_api_get_first_msg_id_reply_t *rmp;
358 unix_shared_memory_queue_t *q;
360 api_main_t *am = &api_main;
361 vl_api_msg_range_t *rp;
363 u16 first_msg_id = ~0;
364 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
366 q = vl_api_client_index_to_input_queue (mp->client_index);
370 if (am->msg_range_by_name == 0)
373 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
375 p = hash_get_mem (am->msg_range_by_name, name);
379 rp = vec_elt_at_index (am->msg_ranges, p[0]);
381 first_msg_id = rp->first_msg_id;
386 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);
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)
403 memory_api_init (const char *region_name)
406 vl_msg_api_msg_config_t cfg;
407 vl_msg_api_msg_config_t *c = &cfg;
409 memset (c, 0, sizeof (*c));
411 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
414 #define _(N,n) do { \
415 c->id = VL_API_##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 */ \
424 c->message_bounce = 0; /* don't bounce this message */ \
425 vl_msg_api_config(c);} while (0);
427 foreach_vlib_api_msg;
433 #define foreach_histogram_bucket \
441 #define _(n) SLEEP_##n##_US,
442 foreach_histogram_bucket
447 static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
449 static void memclnt_queue_callback (vlib_main_t * vm);
452 * Callback to send ourselves a plugin numbering-space trace msg
455 send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
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;
462 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
463 memset (mp, 0, sizeof (*mp));
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);
471 q = shmem_hdr->vl_input_queue;
473 vl_msg_api_send_shmem (q, (u8 *) & mp);
477 memclnt_process (vlib_main_t * vm,
478 vlib_node_runtime_t * node, vlib_frame_t * f)
482 unix_shared_memory_queue_t *q;
485 api_main_t *am = &api_main;
486 f64 dead_client_scan_time;
487 f64 sleep_time, start_time;
491 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
493 if ((rv = memory_api_init (am->region_name)) < 0)
495 clib_warning ("memory_api_init returned %d, wait for godot...", rv);
496 vlib_process_suspend (vm, 1e70);
501 q = shm->vl_input_queue;
504 e = vlib_call_init_exit_functions
505 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
507 clib_error_report (e);
510 dead_client_scan_time = vlib_time_now (vm) + 20.0;
513 * Send plugin message range messages for each plugin we loaded
515 for (i = 0; i < vec_len (am->msg_ranges); i++)
517 vl_api_msg_range_t *rp = am->msg_ranges + i;
518 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
522 /* $$$ pay attention to frame size, control CPU usage */
525 uword event_type __attribute__ ((unused));
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.
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...
539 vector_rate = vlib_last_vector_length_per_node (vm);
540 start_time = vlib_time_now (vm);
543 pthread_mutex_lock (&q->mutex);
546 vm->api_queue_nonempty = 0;
547 pthread_mutex_unlock (&q->mutex);
549 if (TRACE_VLIB_MEMORY_QUEUE)
552 ELOG_TYPE_DECLARE (e) =
554 .format = "q-underflow: len %d",
562 ed = ELOG_DATA (&vm->elog_main, e);
569 headp = (i8 *) (q->data + sizeof (uword) * q->head);
570 clib_memcpy (&mp, headp, sizeof (uword));
573 need_broadcast = (q->cursize == q->maxsize / 2);
576 if (PREDICT_FALSE (q->head == q->maxsize))
578 pthread_mutex_unlock (&q->mutex);
580 (void) pthread_cond_broadcast (&q->condvar);
582 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
584 /* Allow no more than 10us without a pause */
585 if (vlib_time_now (vm) > start_time + 10e-6)
587 int index = SLEEP_400_US;
588 if (vector_rate > 40.0)
590 else if (vector_rate > 20.0)
592 index = SLEEP_200_US;
595 else if (vector_rate >= 1.0)
597 index = SLEEP_100_US;
605 vector_rate_histogram[index] += 1;
610 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 */ );
614 if (vlib_time_now (vm) > dead_client_scan_time)
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;
622 vec_reset_length (dead_indices);
623 vec_reset_length (confused_indices);
626 pool_foreach (regpp, am->vl_clients,
631 q = regp->vl_input_queue;
632 if (kill (q->consumer_pid, 0) < 0)
634 vec_add1(dead_indices, regpp - am->vl_clients);
639 clib_warning ("NULL client registration index %d",
640 regpp - am->vl_clients);
641 vec_add1 (confused_indices, regpp - am->vl_clients);
645 /* This should "never happen," but if it does, fix it... */
646 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
649 for (i = 0; i < vec_len (confused_indices); i++)
651 pool_put_index (am->vl_clients, confused_indices[i]);
655 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
661 /* Allow the application to clean up its registrations */
662 for (i = 0; i < vec_len (dead_indices); i++)
664 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
669 handle = vl_msg_api_handle_from_index_and_epoch
670 (dead_indices[i], shm->application_restarts);
671 (void) call_reaper_functions (handle);
676 pthread_mutex_lock (&svm->mutex);
677 oldheap = svm_push_data_heap (svm);
679 for (i = 0; i < vec_len (dead_indices); i++)
681 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
684 /* Poison the old registration */
685 memset (*regpp, 0xF3, sizeof (**regpp));
686 clib_mem_free (*regpp);
687 /* no dangling references, please */
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);
699 svm_client_scan_this_region_nolock (am->vlib_rp);
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]);
707 dead_client_scan_time = vlib_time_now (vm) + 20.0;
710 if (TRACE_VLIB_MEMORY_QUEUE)
713 ELOG_TYPE_DECLARE (e) = {
714 .format = "q-awake: len %d",
722 ed = ELOG_DATA (&vm->elog_main, e);
723 ed->len = q->cursize;
730 static clib_error_t *
731 vl_api_show_histogram_command (vlib_main_t * vm,
732 unformat_input_t * input,
733 vlib_cli_command_t * cli_cmd)
735 u64 total_counts = 0;
738 for (i = 0; i < SLEEP_N_BUCKETS; i++)
740 total_counts += vector_rate_histogram[i];
743 if (total_counts == 0)
745 vlib_cli_output (vm, "No control-plane activity.");
752 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
753 / (f64) total_counts; \
755 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
756 vector_rate_histogram[SLEEP_##n##_US], \
759 foreach_histogram_bucket;
766 VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) = {
767 .path = "show api histogram",
768 .short_help = "show api histogram",
769 .function = vl_api_show_histogram_command,
773 static clib_error_t *
774 vl_api_clear_histogram_command (vlib_main_t * vm,
775 unformat_input_t * input,
776 vlib_cli_command_t * cli_cmd)
780 for (i = 0; i < SLEEP_N_BUCKETS; i++)
781 vector_rate_histogram[i] = 0;
786 VLIB_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,
795 VLIB_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,
804 memclnt_queue_callback (vlib_main_t * vm)
806 static volatile int *cursizep;
808 if (PREDICT_FALSE (cursizep == 0))
810 api_main_t *am = &api_main;
811 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
812 unix_shared_memory_queue_t *q;
817 q = shmem_hdr->vl_input_queue;
820 cursizep = &q->cursize;
825 vm->queue_signal_pending = 1;
826 vm->api_queue_nonempty = 1;
827 vlib_process_signal_event (vm, memclnt_node.index,
828 /* event_type */ 0, /* event_data */ 0);
833 vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
835 vlib_node_set_state (vm, memclnt_node.index,
837 ? VLIB_NODE_STATE_POLLING
838 : VLIB_NODE_STATE_DISABLED));
842 api_rx_from_node (vlib_main_t * vm,
843 vlib_node_runtime_t * node, vlib_frame_t * frame)
845 uword n_packets = frame->n_vectors;
850 vec_validate (long_msg, 4095);
851 n_left_from = frame->n_vectors;
852 from = vlib_frame_args (frame);
854 while (n_left_from > 0)
862 b0 = vlib_get_buffer (vm, bi0);
866 msg = b0->data + b0->current_data;
867 msg_len = b0->current_length;
868 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
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)
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);
882 vl_msg_api_handler_no_trace_no_free (msg);
885 /* Free what we've been given. */
886 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
892 VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
893 .function = api_rx_from_node,
894 .type = VLIB_NODE_TYPE_INTERNAL,
896 .name = "api-rx-from-node",
900 static clib_error_t *
901 setup_memclnt_exit (vlib_main_t * vm)
903 atexit (vl_unmap_shmem);
907 VLIB_INIT_FUNCTION (setup_memclnt_exit);
910 static clib_error_t *
911 vl_api_ring_command (vlib_main_t * vm,
912 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
916 vl_shmem_hdr_t *shmem_hdr;
917 api_main_t *am = &api_main;
919 shmem_hdr = am->shmem_hdr;
923 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
927 vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
928 "Owner", "Size", "Nitems", "Hits", "Misses");
930 ap = shmem_hdr->vl_rings;
932 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
934 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
935 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
939 ap = shmem_hdr->client_rings;
941 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
943 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
944 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
948 vlib_cli_output (vm, "%d ring miss fallback allocations\n",
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);
958 void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
959 __attribute__ ((weak));
962 dump_socket_clients (vlib_main_t * vm, api_main_t * am)
966 static clib_error_t *
967 vl_api_client_command (vlib_main_t * vm,
968 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
970 vl_api_registration_t **regpp, *regp;
971 unix_shared_memory_queue_t *q;
973 api_main_t *am = &api_main;
974 u32 *confused_indices = 0;
976 if (!pool_elts (am->vl_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");
983 pool_foreach (regpp, am->vl_clients,
989 q = regp->vl_input_queue;
990 if (kill (q->consumer_pid, 0) < 0)
998 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
999 regp->name, q->consumer_pid, q->cursize,
1004 clib_warning ("NULL client registration index %d",
1005 regpp - am->vl_clients);
1006 vec_add1 (confused_indices, regpp - am->vl_clients);
1011 /* This should "never happen," but if it does, fix it... */
1012 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
1015 for (i = 0; i < vec_len (confused_indices); i++)
1017 pool_put_index (am->vl_clients, confused_indices[i]);
1020 vec_free (confused_indices);
1022 if (am->missing_clients)
1023 vlib_cli_output (vm, "%u messages with missing clients",
1024 am->missing_clients);
1026 dump_socket_clients (vm, am);
1031 static clib_error_t *
1032 vl_api_status_command (vlib_main_t * vm,
1033 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1035 api_main_t *am = &api_main;
1037 // check if rx_trace and tx_trace are not null pointers
1039 if (am->rx_trace == 0)
1041 vlib_cli_output (vm, "RX Trace disabled\n");
1045 if (am->rx_trace->enabled == 0)
1046 vlib_cli_output (vm, "RX Trace disabled\n");
1048 vlib_cli_output (vm, "RX Trace enabled\n");
1051 if (am->tx_trace == 0)
1053 vlib_cli_output (vm, "TX Trace disabled\n");
1057 if (am->tx_trace->enabled == 0)
1058 vlib_cli_output (vm, "TX Trace disabled\n");
1060 vlib_cli_output (vm, "TX Trace enabled\n");
1067 VLIB_CLI_COMMAND (cli_show_api_command, static) = {
1069 .short_help = "Show API information",
1074 VLIB_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,
1082 VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = {
1083 .path = "show api clients",
1084 .short_help = "Client information",
1085 .function = vl_api_client_command,
1090 VLIB_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,
1097 static clib_error_t *
1098 vl_api_message_table_command (vlib_main_t * vm,
1099 unformat_input_t * input,
1100 vlib_cli_command_t * cli_cmd)
1102 api_main_t *am = &api_main;
1106 if (unformat (input, "verbose"))
1111 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1113 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1116 for (i = 1; i < vec_len (am->msg_names); i++)
1120 vlib_cli_output (vm, "%-4d %s", i,
1121 am->msg_names[i] ? am->msg_names[i] :
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],
1137 VLIB_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,
1144 static clib_error_t *
1145 vl_api_trace_command (vlib_main_t * vm,
1146 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1149 vl_api_trace_which_t which = VL_API_TRACE_RX;
1150 api_main_t *am = &api_main;
1152 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1154 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1156 else if (unformat (input, "tx nitems %u", &nitems)
1157 || unformat (input, "tx"))
1159 which = VL_API_TRACE_RX;
1162 else if (unformat (input, "on rx"))
1164 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1166 else if (unformat (input, "on tx"))
1168 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1170 else if (unformat (input, "on"))
1172 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1174 else if (unformat (input, "off"))
1176 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1177 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1179 else if (unformat (input, "free"))
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);
1186 else if (unformat (input, "debug on"))
1188 am->msg_print_flag = 1;
1190 else if (unformat (input, "debug off"))
1192 am->msg_print_flag = 0;
1195 return clib_error_return (0, "unknown input `%U'",
1196 format_unformat_error, input);
1201 if (vl_msg_api_trace_configure (am, which, nitems))
1203 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1211 VLIB_CLI_COMMAND (trace, static) = {
1212 .path = "set api-trace",
1213 .short_help = "API trace",
1214 .function = vl_api_trace_command,
1219 vlibmemory_init (vlib_main_t * vm)
1221 api_main_t *am = &api_main;
1222 svm_map_region_args_t _a, *a = &_a;
1224 memset (a, 0, sizeof (*a));
1225 a->root_path = am->root_path;
1226 a->name = SVM_GLOBAL_REGION_NAME;
1227 a->baseva = (am->global_baseva != 0) ?
1228 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;
1234 (am->global_pvt_heap_size !=
1235 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
1237 svm_region_init_args (a);
1241 VLIB_INIT_FUNCTION (vlibmemory_init);
1244 vl_set_memory_region_name (const char *name)
1246 api_main_t *am = &api_main;
1248 am->region_name = name;
1252 range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
1254 int len0, len1, clen;
1256 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));
1263 format_api_msg_range (u8 * s, va_list * args)
1265 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
1268 s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID");
1270 s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id,
1276 static clib_error_t *
1277 vl_api_show_plugin_command (vlib_main_t * vm,
1278 unformat_input_t * input,
1279 vlib_cli_command_t * cli_cmd)
1281 api_main_t *am = &api_main;
1282 vl_api_msg_range_t *rp = 0;
1285 if (vec_len (am->msg_ranges) == 0)
1287 vlib_cli_output (vm, "No plugin API message ranges configured...");
1291 rp = vec_dup (am->msg_ranges);
1293 vec_sort_with_function (rp, range_compare);
1295 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1296 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1298 for (i = 0; i < vec_len (rp); i++)
1299 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1307 VLIB_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,
1315 vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
1317 vl_api_rpc_call_reply_t *rmp;
1320 vlib_main_t *vm = vlib_get_main ();
1322 if (mp->function == 0)
1325 clib_warning ("rpc NULL function pointer");
1330 if (mp->need_barrier_sync)
1331 vlib_worker_thread_barrier_sync (vm);
1333 fp = uword_to_pointer (mp->function, int (*)(void *));
1336 if (mp->need_barrier_sync)
1337 vlib_worker_thread_barrier_release (vm);
1342 unix_shared_memory_queue_t *q =
1343 vl_api_client_index_to_input_queue (mp->client_index);
1346 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
1347 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
1348 rmp->context = mp->context;
1350 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1355 clib_warning ("multicast not yet implemented...");
1360 vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
1362 clib_warning ("unimplemented");
1366 vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1368 vl_api_rpc_call_t *mp;
1369 api_main_t *am = &api_main;
1370 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
1371 unix_shared_memory_queue_t *q;
1373 /* Main thread: call the function directly */
1374 if (vlib_get_thread_index () == 0)
1376 vlib_main_t *vm = vlib_get_main ();
1377 void (*call_fp) (void *);
1379 vlib_worker_thread_barrier_sync (vm);
1384 vlib_worker_thread_barrier_release (vm);
1388 /* Any other thread, actually do an RPC call... */
1389 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
1391 memset (mp, 0, sizeof (*mp));
1392 clib_memcpy (mp->data, data, data_length);
1393 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
1394 mp->function = pointer_to_uword (fp);
1395 mp->need_barrier_sync = 1;
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.
1402 q = shmem_hdr->vl_input_queue;
1404 while (pthread_mutex_trylock (&q->mutex))
1405 vlib_worker_thread_barrier_check ();
1407 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
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 ();
1415 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1417 pthread_mutex_unlock (&q->mutex);
1421 vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1423 api_main_t *am = &api_main;
1424 vl_api_msg_range_t *rp;
1427 /* Noop (except for tracing) during normal operation */
1428 if (am->replay_in_progress == 0)
1431 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1434 clib_warning ("WARNING: traced plugin '%s' not in current image",
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))
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),
1447 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
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),
1455 #define foreach_rpc_api_msg \
1456 _(RPC_CALL,rpc_call) \
1457 _(RPC_CALL_REPLY,rpc_call_reply)
1459 #define foreach_plugin_trace_msg \
1460 _(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1462 static clib_error_t *
1463 rpc_api_hookup (vlib_main_t * vm)
1466 vl_msg_api_set_handlers(VL_API_##N, #n, \
1467 vl_api_##n##_t_handler, \
1470 vl_api_##n##_t_print, \
1471 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1472 foreach_rpc_api_msg;
1476 vl_msg_api_set_handlers(VL_API_##N, #n, \
1477 vl_api_##n##_t_handler, \
1480 vl_api_##n##_t_print, \
1481 sizeof(vl_api_##n##_t), 1 /* do trace */);
1482 foreach_plugin_trace_msg;
1487 VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1498 format_vl_msg_api_trace_status (u8 * s, va_list * args)
1500 api_main_t *am = va_arg (*args, api_main_t *);
1501 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
1507 case VL_API_TRACE_TX:
1509 trace_name = "TX trace";
1512 case VL_API_TRACE_RX:
1514 trace_name = "RX trace";
1523 s = format (s, "%s: not yet configured.\n", trace_name);
1527 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
1528 trace_name, vec_len (tp->traces), tp->nitems,
1529 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
1533 void vl_msg_api_custom_dump_configure (api_main_t * am)
1534 __attribute__ ((weak));
1536 vl_msg_api_custom_dump_configure (api_main_t * am)
1541 vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
1542 u32 first_index, u32 last_index,
1543 vl_api_replay_t which)
1545 vl_api_trace_file_header_t *hp;
1550 u8 endian_swap_needed = 0;
1551 api_main_t *am = &api_main;
1554 void **saved_print_handlers = 0;
1556 fd = open ((char *) filename, O_RDONLY);
1560 vlib_cli_output (vm, "Couldn't open %s\n", filename);
1564 if (fstat (fd, &statb) < 0)
1566 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
1571 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
1573 vlib_cli_output (vm, "File not plausible: %s\n", filename);
1578 file_size = statb.st_size;
1579 file_size = (file_size + 4095) & ~(4096);
1581 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1583 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
1585 vlib_cli_output (vm, "mmap failed: %s\n", filename);
1591 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
1592 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
1593 endian_swap_needed = 1;
1595 if (endian_swap_needed)
1596 nitems = ntohl (hp->nitems);
1598 nitems = hp->nitems;
1600 if (last_index == (u32) ~ 0)
1602 last_index = nitems - 1;
1605 if (first_index >= nitems || last_index >= nitems)
1607 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
1608 first_index, last_index, nitems - 1);
1609 munmap (hp, file_size);
1613 vlib_cli_output (vm,
1614 "Note: wrapped/incomplete trace, results may vary\n");
1616 if (which == CUSTOM_DUMP)
1618 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
1619 vl_msg_api_custom_dump_configure (am);
1623 msg = (u8 *) (hp + 1);
1625 for (i = 0; i < first_index; i++)
1631 size = clib_host_to_net_u32 (*(u32 *) msg);
1632 msg += sizeof (u32);
1634 if (clib_arch_is_little_endian)
1635 msg_id = ntohs (*((u16 *) msg));
1637 msg_id = *((u16 *) msg);
1639 cfgp = am->api_trace_cfg + msg_id;
1642 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1643 munmap (hp, file_size);
1649 if (which == REPLAY)
1650 am->replay_in_progress = 1;
1652 for (; i <= last_index; i++)
1660 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
1662 size = clib_host_to_net_u32 (*(u32 *) msg);
1663 msg += sizeof (u32);
1665 if (clib_arch_is_little_endian)
1666 msg_id = ntohs (*((u16 *) msg));
1668 msg_id = *((u16 *) msg);
1670 cfgp = am->api_trace_cfg + msg_id;
1673 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1674 munmap (hp, file_size);
1676 am->replay_in_progress = 0;
1680 /* Copy the buffer (from the read-only mmap'ed file) */
1681 vec_validate (tmpbuf, size - 1 + sizeof (uword));
1682 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
1683 memset (tmpbuf, 0xf, sizeof (uword));
1686 * Endian swap if needed. All msg data is supposed to be
1687 * in network byte order. All msg handlers are supposed to
1688 * know that. The generic message dumpers don't know that.
1689 * One could fix apigen, I suppose.
1691 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
1693 void (*endian_fp) (void *);
1694 if (msg_id >= vec_len (am->msg_endian_handlers)
1695 || (am->msg_endian_handlers[msg_id] == 0))
1697 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
1698 munmap (hp, file_size);
1700 am->replay_in_progress = 0;
1703 endian_fp = am->msg_endian_handlers[msg_id];
1704 (*endian_fp) (tmpbuf + sizeof (uword));
1707 /* msg_id always in network byte order */
1708 if (clib_arch_is_little_endian)
1710 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
1718 if (msg_id < vec_len (am->msg_print_handlers) &&
1719 am->msg_print_handlers[msg_id])
1721 u8 *(*print_fp) (void *, void *);
1723 print_fp = (void *) am->msg_print_handlers[msg_id];
1724 (*print_fp) (tmpbuf + sizeof (uword), vm);
1728 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
1735 if (msg_id < vec_len (am->msg_print_handlers) &&
1736 am->msg_print_handlers[msg_id])
1740 u8 *(*print_fp) (void *, void *);
1742 print_fp = (void *) am->msg_print_handlers[msg_id];
1744 vlib_cli_output (vm, "/*");
1746 (*print_fp) (tmpbuf + sizeof (uword), vm);
1747 vlib_cli_output (vm, "*/\n");
1749 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
1750 am->msg_names[msg_id], i,
1751 am->api_trace_cfg[msg_id].size);
1753 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
1756 s = format (s, "\n ");
1757 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
1759 s = format (s, "\n};\n%c", 0);
1760 vlib_cli_output (vm, (char *) s);
1766 if (msg_id < vec_len (am->msg_print_handlers) &&
1767 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
1769 void (*handler) (void *);
1771 handler = (void *) am->msg_handlers[msg_id];
1773 if (!am->is_mp_safe[msg_id])
1774 vl_msg_api_barrier_sync ();
1775 (*handler) (tmpbuf + sizeof (uword));
1776 if (!am->is_mp_safe[msg_id])
1777 vl_msg_api_barrier_release ();
1781 if (cfgp->replay_enable)
1782 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
1789 _vec_len (tmpbuf) = 0;
1793 if (saved_print_handlers)
1795 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
1796 vec_len (am->msg_print_handlers) * sizeof (void *));
1797 vec_free (saved_print_handlers);
1800 munmap (hp, file_size);
1802 am->replay_in_progress = 0;
1805 static clib_error_t *
1806 api_trace_command_fn (vlib_main_t * vm,
1807 unformat_input_t * input, vlib_cli_command_t * cmd)
1809 u32 nitems = 256 << 10;
1810 api_main_t *am = &api_main;
1811 vl_api_trace_which_t which = VL_API_TRACE_RX;
1814 u32 last = (u32) ~ 0;
1818 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1820 if (unformat (input, "on") || unformat (input, "enable"))
1822 if (unformat (input, "nitems %d", &nitems))
1824 vl_msg_api_trace_configure (am, which, nitems);
1825 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1827 else if (unformat (input, "off"))
1829 vl_msg_api_trace_onoff (am, which, 0);
1831 else if (unformat (input, "save %s", &filename))
1833 u8 *chroot_filename;
1834 if (strstr ((char *) filename, "..")
1835 || index ((char *) filename, '/'))
1837 vlib_cli_output (vm, "illegal characters in filename '%s'",
1842 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1844 vec_free (filename);
1846 fp = fopen ((char *) chroot_filename, "w");
1849 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
1852 rv = vl_msg_api_trace_save (am, which, fp);
1855 vlib_cli_output (vm, "API Trace data not present\n");
1857 vlib_cli_output (vm, "File for writing is closed\n");
1859 vlib_cli_output (vm, "Error while writing header to file\n");
1861 vlib_cli_output (vm, "Error while writing trace to file\n");
1863 vlib_cli_output (vm,
1864 "Error while writing end of buffer trace to file\n");
1866 vlib_cli_output (vm,
1867 "Error while writing start of buffer trace to file\n");
1869 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
1871 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
1872 vec_free (chroot_filename);
1874 else if (unformat (input, "dump %s", &filename))
1876 vl_msg_api_process_file (vm, filename, first, last, DUMP);
1878 else if (unformat (input, "custom-dump %s", &filename))
1880 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
1882 else if (unformat (input, "replay %s", &filename))
1884 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
1886 else if (unformat (input, "initializers %s", &filename))
1888 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
1890 else if (unformat (input, "tx"))
1892 which = VL_API_TRACE_TX;
1894 else if (unformat (input, "first %d", &first))
1898 else if (unformat (input, "last %d", &last))
1902 else if (unformat (input, "status"))
1904 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
1907 else if (unformat (input, "free"))
1909 vl_msg_api_trace_onoff (am, which, 0);
1910 vl_msg_api_trace_free (am, which);
1912 else if (unformat (input, "post-mortem-on"))
1913 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
1914 else if (unformat (input, "post-mortem-off"))
1915 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
1917 return clib_error_return (0, "unknown input `%U'",
1918 format_unformat_error, input);
1924 VLIB_CLI_COMMAND (api_trace_command, static) = {
1925 .path = "api trace",
1927 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
1928 .function = api_trace_command_fn,
1932 static clib_error_t *
1933 api_config_fn (vlib_main_t * vm, unformat_input_t * input)
1935 u32 nitems = 256 << 10;
1936 vl_api_trace_which_t which = VL_API_TRACE_RX;
1937 api_main_t *am = &api_main;
1939 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1941 if (unformat (input, "on") || unformat (input, "enable"))
1943 if (unformat (input, "nitems %d", &nitems))
1945 vl_msg_api_trace_configure (am, which, nitems);
1946 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1947 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
1950 return clib_error_return (0, "unknown input `%U'",
1951 format_unformat_error, input);
1956 VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
1958 static clib_error_t *
1959 api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
1961 api_main_t *am = &api_main;
1964 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1966 if (unformat (input, "length %d", &nitems) ||
1967 (unformat (input, "len %d", &nitems)))
1970 am->vlib_input_queue_length = nitems;
1972 clib_warning ("vlib input queue length %d too small, ignored",
1976 return clib_error_return (0, "unknown input `%U'",
1977 format_unformat_error, input);
1982 VLIB_CONFIG_FUNCTION (api_queue_config_fn, "api-queue");
1985 * fd.io coding-style-patch-verification: ON
1988 * eval: (c-set-style "gnu")