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>
43 * @brief Binary API messaging via shared memory
44 * Low-level, primary provisioning interface
46 /*? %%clicmd:group_label Binary API CLI %% ?*/
47 /*? %%syscfg:group_label Binary API configuration %% ?*/
49 #define TRACE_VLIB_MEMORY_QUEUE 0
51 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
53 #define vl_typedefs /* define message structures */
54 #include <vlibmemory/vl_memory_api_h.h>
57 /* instantiate all the print functions we know about */
58 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
60 #include <vlibmemory/vl_memory_api_h.h>
64 vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
66 vl_print (handle, "vl_api_memclnt_create_t:\n");
67 vl_print (handle, "name: %s\n", a->name);
68 vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
69 vl_print (handle, "context: %u\n", (unsigned) a->context);
70 vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
75 vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
77 vl_print (handle, "vl_api_memclnt_delete_t:\n");
78 vl_print (handle, "index: %u\n", (unsigned) a->index);
79 vl_print (handle, "handle: 0x%wx\n", a->handle);
84 vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
87 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
89 clib_host_to_net_u16 (a->first_msg_id),
90 clib_host_to_net_u16 (a->last_msg_id));
94 /* instantiate all the endian swap functions we know about */
96 #include <vlibmemory/vl_memory_api_h.h>
99 void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
100 __attribute__ ((weak));
103 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
108 clib_warning ("need to link against -lvlibsocket, msg not sent!");
112 vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
114 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
116 vl_socket_api_send (rp, elem);
120 vl_msg_api_send_shmem (rp->vl_input_queue, elem);
125 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
127 serialize_main_t _sm, *sm = &_sm;
129 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
131 serialize_open_vector (sm, vector);
133 /* serialize the count */
134 serialize_integer (sm, nmsg, sizeof (u32));
137 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
139 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
140 serialize_cstring (sm, (char *) hp->key);
144 return serialize_close_vector (sm);
148 * vl_api_memclnt_create_internal
152 vl_api_memclnt_create_internal (char *name, unix_shared_memory_queue_t * q)
154 vl_api_registration_t **regpp;
155 vl_api_registration_t *regp;
158 api_main_t *am = &api_main;
160 ASSERT (vlib_get_thread_index () == 0);
161 pool_get (am->vl_clients, regpp);
165 pthread_mutex_lock (&svm->mutex);
166 oldheap = svm_push_data_heap (svm);
167 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
170 memset (regp, 0, sizeof (*regp));
171 regp->registration_type = REGISTRATION_TYPE_SHMEM;
172 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
174 regp->vl_input_queue = q;
175 regp->name = format (0, "%s%c", name, 0);
177 pthread_mutex_unlock (&svm->mutex);
178 svm_pop_heap (oldheap);
179 return vl_msg_api_handle_from_index_and_epoch
180 (regp->vl_api_registration_pool_index,
181 am->shmem_hdr->application_restarts);
186 * vl_api_memclnt_create_t_handler
189 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
191 vl_api_registration_t **regpp;
192 vl_api_registration_t *regp;
193 vl_api_memclnt_create_reply_t *rp;
195 unix_shared_memory_queue_t *q;
198 api_main_t *am = &api_main;
201 * This is tortured. Maintain a vlib-address-space private
202 * pool of client registrations. We use the shared-memory virtual
203 * address of client structure as a handle, to allow direct
204 * manipulation of context quota vbls from the client library.
206 * This scheme causes trouble w/ API message trace replay, since
207 * some random VA from clib_mem_alloc() certainly won't
208 * occur in the Linux sim. The (very) few places
209 * that care need to use the pool index.
211 * Putting the registration object(s) into a pool in shared memory and
212 * using the pool index as a handle seems like a great idea.
213 * Unfortunately, each and every reference to that pool would need
214 * to be protected by a mutex:
218 * convert pool index to
226 pool_get (am->vl_clients, regpp);
230 pthread_mutex_lock (&svm->mutex);
231 oldheap = svm_push_data_heap (svm);
232 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
235 memset (regp, 0, sizeof (*regp));
236 regp->registration_type = REGISTRATION_TYPE_SHMEM;
237 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
239 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
242 regp->name = format (0, "%s", mp->name);
243 vec_add1 (regp->name, 0);
245 pthread_mutex_unlock (&svm->mutex);
246 svm_pop_heap (oldheap);
248 ASSERT (am->serialized_message_table_in_shmem);
250 rp = vl_msg_api_alloc (sizeof (*rp));
251 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
252 rp->handle = (uword) regp;
253 rp->index = vl_msg_api_handle_from_index_and_epoch
254 (regp->vl_api_registration_pool_index,
255 am->shmem_hdr->application_restarts);
256 rp->context = mp->context;
257 rp->response = ntohl (rv);
259 pointer_to_uword (am->serialized_message_table_in_shmem);
261 vl_msg_api_send_shmem (q, (u8 *) & rp);
265 call_reaper_functions (u32 client_index)
267 clib_error_t *error = 0;
268 _vl_msg_api_function_list_elt_t *i;
270 i = api_main.reaper_function_registrations;
273 error = i->f (client_index);
275 clib_error_report (error);
276 i = i->next_init_function;
282 * vl_api_memclnt_delete_t_handler
285 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
287 vl_api_registration_t **regpp;
288 vl_api_registration_t *regp;
289 vl_api_memclnt_delete_reply_t *rp;
292 api_main_t *am = &api_main;
293 u32 handle, client_index, epoch;
297 if (call_reaper_functions (handle))
300 epoch = vl_msg_api_handle_get_epoch (handle);
301 client_index = vl_msg_api_handle_get_index (handle);
303 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
306 ("Stale clnt delete index %d old epoch %d cur epoch %d",
308 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
312 regpp = am->vl_clients + client_index;
314 if (!pool_is_free (am->vl_clients, regpp))
319 /* $$$ check the input queue for e.g. punted sf's */
321 rp = vl_msg_api_alloc (sizeof (*rp));
322 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
323 rp->handle = mp->handle;
326 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
328 if (client_index != regp->vl_api_registration_pool_index)
330 clib_warning ("mismatch client_index %d pool_index %d",
331 client_index, regp->vl_api_registration_pool_index);
332 vl_msg_api_free (rp);
336 /* No dangling references, please */
339 pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
341 pthread_mutex_lock (&svm->mutex);
342 oldheap = svm_push_data_heap (svm);
343 /* Poison the old registration */
344 memset (regp, 0xF1, sizeof (*regp));
345 clib_mem_free (regp);
346 pthread_mutex_unlock (&svm->mutex);
347 svm_pop_heap (oldheap);
351 clib_warning ("unknown client ID %d", mp->index);
356 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
358 vl_api_get_first_msg_id_reply_t *rmp;
359 unix_shared_memory_queue_t *q;
361 api_main_t *am = &api_main;
362 vl_api_msg_range_t *rp;
364 u16 first_msg_id = ~0;
365 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
367 q = vl_api_client_index_to_input_queue (mp->client_index);
371 if (am->msg_range_by_name == 0)
374 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
376 p = hash_get_mem (am->msg_range_by_name, name);
380 rp = vec_elt_at_index (am->msg_ranges, p[0]);
382 first_msg_id = rp->first_msg_id;
387 rmp = vl_msg_api_alloc (sizeof (*rmp));
388 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
389 rmp->context = mp->context;
390 rmp->retval = ntohl (rv);
391 rmp->first_msg_id = ntohs (first_msg_id);
392 vl_msg_api_send_shmem (q, (u8 *) & rmp);
395 #define foreach_vlib_api_msg \
396 _(MEMCLNT_CREATE, memclnt_create) \
397 _(MEMCLNT_DELETE, memclnt_delete) \
398 _(GET_FIRST_MSG_ID, get_first_msg_id)
404 memory_api_init (const char *region_name)
407 vl_msg_api_msg_config_t cfg;
408 vl_msg_api_msg_config_t *c = &cfg;
410 memset (c, 0, sizeof (*c));
412 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
415 #define _(N,n) do { \
416 c->id = VL_API_##N; \
418 c->handler = vl_api_##n##_t_handler; \
419 c->cleanup = vl_noop_handler; \
420 c->endian = vl_api_##n##_t_endian; \
421 c->print = vl_api_##n##_t_print; \
422 c->size = sizeof(vl_api_##n##_t); \
423 c->traced = 1; /* trace, so these msgs print */ \
424 c->replay = 0; /* don't replay client create/delete msgs */ \
425 c->message_bounce = 0; /* don't bounce this message */ \
426 vl_msg_api_config(c);} while (0);
428 foreach_vlib_api_msg;
434 #define foreach_histogram_bucket \
442 #define _(n) SLEEP_##n##_US,
443 foreach_histogram_bucket
448 static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
450 static void memclnt_queue_callback (vlib_main_t * vm);
453 * Callback to send ourselves a plugin numbering-space trace msg
456 send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
458 vl_api_trace_plugin_msg_ids_t *mp;
459 api_main_t *am = &api_main;
460 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
461 unix_shared_memory_queue_t *q;
463 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
464 memset (mp, 0, sizeof (*mp));
466 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
467 strncpy ((char *) mp->plugin_name, (char *) name,
468 sizeof (mp->plugin_name) - 1);
469 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
470 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
472 q = shmem_hdr->vl_input_queue;
474 vl_msg_api_send_shmem (q, (u8 *) & mp);
478 memclnt_process (vlib_main_t * vm,
479 vlib_node_runtime_t * node, vlib_frame_t * f)
483 unix_shared_memory_queue_t *q;
486 api_main_t *am = &api_main;
487 f64 dead_client_scan_time;
488 f64 sleep_time, start_time;
491 u8 *serialized_message_table = 0;
495 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
497 if ((rv = memory_api_init (am->region_name)) < 0)
499 clib_warning ("memory_api_init returned %d, wait for godot...", rv);
500 vlib_process_suspend (vm, 1e70);
505 q = shm->vl_input_queue;
508 e = vlib_call_init_exit_functions
509 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
511 clib_error_report (e);
514 dead_client_scan_time = vlib_time_now (vm) + 20.0;
517 * Send plugin message range messages for each plugin we loaded
519 for (i = 0; i < vec_len (am->msg_ranges); i++)
521 vl_api_msg_range_t *rp = am->msg_ranges + i;
522 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
527 * Snapshoot the api message table.
529 serialized_message_table = vl_api_serialize_message_table (am, 0);
532 pthread_mutex_lock (&svm->mutex);
533 oldheap = svm_push_data_heap (svm);
535 am->serialized_message_table_in_shmem = vec_dup (serialized_message_table);
537 pthread_mutex_unlock (&svm->mutex);
538 svm_pop_heap (oldheap);
541 * Save the api message table snapshot, if configured
543 if (am->save_msg_table_filename)
547 if (strstr ((char *) am->save_msg_table_filename, "..")
548 || index ((char *) am->save_msg_table_filename, '/'))
550 clib_warning ("illegal save-message-table filename '%s'",
551 am->save_msg_table_filename);
555 chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
557 fd = creat ((char *) chroot_file, 0644);
561 clib_unix_warning ("creat");
564 rv = write (fd, serialized_message_table,
565 vec_len (serialized_message_table));
567 if (rv != vec_len (serialized_message_table))
568 clib_unix_warning ("write");
572 clib_unix_warning ("close");
574 vec_free (chroot_file);
578 vec_free (serialized_message_table);
580 /* $$$ pay attention to frame size, control CPU usage */
583 uword event_type __attribute__ ((unused));
588 * There's a reason for checking the queue before
589 * sleeping. If the vlib application crashes, it's entirely
590 * possible for a client to enqueue a connect request
591 * during the process restart interval.
593 * Unless some force of physics causes the new incarnation
594 * of the application to process the request, the client will
595 * sit and wait for Godot...
597 vector_rate = vlib_last_vector_length_per_node (vm);
598 start_time = vlib_time_now (vm);
601 pthread_mutex_lock (&q->mutex);
604 vm->api_queue_nonempty = 0;
605 pthread_mutex_unlock (&q->mutex);
607 if (TRACE_VLIB_MEMORY_QUEUE)
610 ELOG_TYPE_DECLARE (e) =
612 .format = "q-underflow: len %d",
620 ed = ELOG_DATA (&vm->elog_main, e);
627 headp = (i8 *) (q->data + sizeof (uword) * q->head);
628 clib_memcpy (&mp, headp, sizeof (uword));
631 need_broadcast = (q->cursize == q->maxsize / 2);
634 if (PREDICT_FALSE (q->head == q->maxsize))
636 pthread_mutex_unlock (&q->mutex);
638 (void) pthread_cond_broadcast (&q->condvar);
640 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
642 /* Allow no more than 10us without a pause */
643 if (vlib_time_now (vm) > start_time + 10e-6)
645 int index = SLEEP_400_US;
646 if (vector_rate > 40.0)
648 else if (vector_rate > 20.0)
650 index = SLEEP_200_US;
653 else if (vector_rate >= 1.0)
655 index = SLEEP_100_US;
663 vector_rate_histogram[index] += 1;
668 event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time);
669 vm->queue_signal_pending = 0;
670 vlib_process_get_events (vm, 0 /* event_data */ );
672 if (vlib_time_now (vm) > dead_client_scan_time)
674 vl_api_registration_t **regpp;
675 vl_api_registration_t *regp;
676 unix_shared_memory_queue_t *q;
677 static u32 *dead_indices;
678 static u32 *confused_indices;
680 vec_reset_length (dead_indices);
681 vec_reset_length (confused_indices);
684 pool_foreach (regpp, am->vl_clients,
689 q = regp->vl_input_queue;
690 if (kill (q->consumer_pid, 0) < 0)
692 vec_add1(dead_indices, regpp - am->vl_clients);
697 clib_warning ("NULL client registration index %d",
698 regpp - am->vl_clients);
699 vec_add1 (confused_indices, regpp - am->vl_clients);
703 /* This should "never happen," but if it does, fix it... */
704 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
707 for (i = 0; i < vec_len (confused_indices); i++)
709 pool_put_index (am->vl_clients, confused_indices[i]);
713 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
719 /* Allow the application to clean up its registrations */
720 for (i = 0; i < vec_len (dead_indices); i++)
722 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
727 handle = vl_msg_api_handle_from_index_and_epoch
728 (dead_indices[i], shm->application_restarts);
729 (void) call_reaper_functions (handle);
734 pthread_mutex_lock (&svm->mutex);
735 oldheap = svm_push_data_heap (svm);
737 for (i = 0; i < vec_len (dead_indices); i++)
739 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
742 /* Poison the old registration */
743 memset (*regpp, 0xF3, sizeof (**regpp));
744 clib_mem_free (*regpp);
745 /* no dangling references, please */
750 svm_pop_heap (oldheap);
751 clib_warning ("Duplicate free, client index %d",
752 regpp - am->vl_clients);
753 oldheap = svm_push_data_heap (svm);
757 svm_client_scan_this_region_nolock (am->vlib_rp);
759 pthread_mutex_unlock (&svm->mutex);
760 svm_pop_heap (oldheap);
761 for (i = 0; i < vec_len (dead_indices); i++)
762 pool_put_index (am->vl_clients, dead_indices[i]);
765 dead_client_scan_time = vlib_time_now (vm) + 20.0;
768 if (TRACE_VLIB_MEMORY_QUEUE)
771 ELOG_TYPE_DECLARE (e) = {
772 .format = "q-awake: len %d",
780 ed = ELOG_DATA (&vm->elog_main, e);
781 ed->len = q->cursize;
788 VLIB_REGISTER_NODE (memclnt_node,static) = {
789 .function = memclnt_process,
790 .type = VLIB_NODE_TYPE_PROCESS,
791 .name = "api-rx-from-ring",
792 .state = VLIB_NODE_STATE_DISABLED,
797 static clib_error_t *
798 vl_api_show_histogram_command (vlib_main_t * vm,
799 unformat_input_t * input,
800 vlib_cli_command_t * cli_cmd)
802 u64 total_counts = 0;
805 for (i = 0; i < SLEEP_N_BUCKETS; i++)
807 total_counts += vector_rate_histogram[i];
810 if (total_counts == 0)
812 vlib_cli_output (vm, "No control-plane activity.");
819 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
820 / (f64) total_counts; \
822 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
823 vector_rate_histogram[SLEEP_##n##_US], \
826 foreach_histogram_bucket;
833 * Display the binary api sleep-time histogram
836 VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) =
838 .path = "show api histogram",
839 .short_help = "show api histogram",
840 .function = vl_api_show_histogram_command,
844 static clib_error_t *
845 vl_api_clear_histogram_command (vlib_main_t * vm,
846 unformat_input_t * input,
847 vlib_cli_command_t * cli_cmd)
851 for (i = 0; i < SLEEP_N_BUCKETS; i++)
852 vector_rate_histogram[i] = 0;
857 * Clear the binary api sleep-time histogram
860 VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) =
862 .path = "clear api histogram",
863 .short_help = "clear api histogram",
864 .function = vl_api_clear_histogram_command,
869 memclnt_queue_callback (vlib_main_t * vm)
871 static volatile int *cursizep;
873 if (PREDICT_FALSE (cursizep == 0))
875 api_main_t *am = &api_main;
876 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
877 unix_shared_memory_queue_t *q;
882 q = shmem_hdr->vl_input_queue;
885 cursizep = &q->cursize;
890 vm->queue_signal_pending = 1;
891 vm->api_queue_nonempty = 1;
892 vlib_process_signal_event (vm, memclnt_node.index,
893 /* event_type */ 0, /* event_data */ 0);
898 vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
900 vlib_node_set_state (vm, memclnt_node.index,
902 ? VLIB_NODE_STATE_POLLING
903 : VLIB_NODE_STATE_DISABLED));
907 api_rx_from_node (vlib_main_t * vm,
908 vlib_node_runtime_t * node, vlib_frame_t * frame)
910 uword n_packets = frame->n_vectors;
915 vec_validate (long_msg, 4095);
916 n_left_from = frame->n_vectors;
917 from = vlib_frame_args (frame);
919 while (n_left_from > 0)
927 b0 = vlib_get_buffer (vm, bi0);
931 msg = b0->data + b0->current_data;
932 msg_len = b0->current_length;
933 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
935 ASSERT (long_msg != 0);
936 _vec_len (long_msg) = 0;
937 vec_add (long_msg, msg, msg_len);
938 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
940 b0 = vlib_get_buffer (vm, b0->next_buffer);
941 msg = b0->data + b0->current_data;
942 msg_len = b0->current_length;
943 vec_add (long_msg, msg, msg_len);
947 vl_msg_api_handler_no_trace_no_free (msg);
950 /* Free what we've been given. */
951 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
957 VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
958 .function = api_rx_from_node,
959 .type = VLIB_NODE_TYPE_INTERNAL,
961 .name = "api-rx-from-node",
965 static clib_error_t *
966 setup_memclnt_exit (vlib_main_t * vm)
968 atexit (vl_unmap_shmem);
972 VLIB_INIT_FUNCTION (setup_memclnt_exit);
975 static clib_error_t *
976 vl_api_ring_command (vlib_main_t * vm,
977 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
981 vl_shmem_hdr_t *shmem_hdr;
982 api_main_t *am = &api_main;
984 shmem_hdr = am->shmem_hdr;
988 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
992 vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
993 "Owner", "Size", "Nitems", "Hits", "Misses");
995 ap = shmem_hdr->vl_rings;
997 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
999 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
1000 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
1004 ap = shmem_hdr->client_rings;
1006 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
1008 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
1009 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
1013 vlib_cli_output (vm, "%d ring miss fallback allocations\n",
1017 (vm, "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
1018 shmem_hdr->application_restarts,
1019 shmem_hdr->restart_reclaims, shmem_hdr->garbage_collects);
1023 void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
1024 __attribute__ ((weak));
1027 dump_socket_clients (vlib_main_t * vm, api_main_t * am)
1031 static clib_error_t *
1032 vl_api_client_command (vlib_main_t * vm,
1033 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1035 vl_api_registration_t **regpp, *regp;
1036 unix_shared_memory_queue_t *q;
1038 api_main_t *am = &api_main;
1039 u32 *confused_indices = 0;
1041 if (!pool_elts (am->vl_clients))
1042 goto socket_clients;
1043 vlib_cli_output (vm, "Shared memory clients");
1044 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
1045 "Name", "PID", "Queue Length", "Queue VA", "Health");
1048 pool_foreach (regpp, am->vl_clients,
1054 q = regp->vl_input_queue;
1055 if (kill (q->consumer_pid, 0) < 0)
1063 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
1064 regp->name, q->consumer_pid, q->cursize,
1069 clib_warning ("NULL client registration index %d",
1070 regpp - am->vl_clients);
1071 vec_add1 (confused_indices, regpp - am->vl_clients);
1076 /* This should "never happen," but if it does, fix it... */
1077 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
1080 for (i = 0; i < vec_len (confused_indices); i++)
1082 pool_put_index (am->vl_clients, confused_indices[i]);
1085 vec_free (confused_indices);
1087 if (am->missing_clients)
1088 vlib_cli_output (vm, "%u messages with missing clients",
1089 am->missing_clients);
1091 dump_socket_clients (vm, am);
1096 static clib_error_t *
1097 vl_api_status_command (vlib_main_t * vm,
1098 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1100 api_main_t *am = &api_main;
1102 // check if rx_trace and tx_trace are not null pointers
1104 if (am->rx_trace == 0)
1106 vlib_cli_output (vm, "RX Trace disabled\n");
1110 if (am->rx_trace->enabled == 0)
1111 vlib_cli_output (vm, "RX Trace disabled\n");
1113 vlib_cli_output (vm, "RX Trace enabled\n");
1116 if (am->tx_trace == 0)
1118 vlib_cli_output (vm, "TX Trace disabled\n");
1122 if (am->tx_trace->enabled == 0)
1123 vlib_cli_output (vm, "TX Trace disabled\n");
1125 vlib_cli_output (vm, "TX Trace enabled\n");
1132 VLIB_CLI_COMMAND (cli_show_api_command, static) =
1135 .short_help = "Show API information",
1140 * Display binary api message allocation ring statistics
1143 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
1145 .path = "show api ring-stats",
1146 .short_help = "Message ring statistics",
1147 .function = vl_api_ring_command,
1152 * Display current api client connections
1155 VLIB_CLI_COMMAND (cli_show_api_clients_command, static) =
1157 .path = "show api clients",
1158 .short_help = "Client information",
1159 .function = vl_api_client_command,
1164 * Display the current api message tracing status
1167 VLIB_CLI_COMMAND (cli_show_api_status_command, static) =
1169 .path = "show api trace-status",
1170 .short_help = "Display API trace status",
1171 .function = vl_api_status_command,
1175 static clib_error_t *
1176 vl_api_message_table_command (vlib_main_t * vm,
1177 unformat_input_t * input,
1178 vlib_cli_command_t * cli_cmd)
1180 api_main_t *am = &api_main;
1184 if (unformat (input, "verbose"))
1189 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1191 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1194 for (i = 1; i < vec_len (am->msg_names); i++)
1198 vlib_cli_output (vm, "%-4d %s", i,
1199 am->msg_names[i] ? am->msg_names[i] :
1204 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1205 am->msg_names[i] ? am->msg_names[i] :
1206 " [no handler]", am->message_bounce[i],
1215 * Display the current api message decode tables
1218 VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) =
1220 .path = "show api message-table",
1221 .short_help = "Message Table",
1222 .function = vl_api_message_table_command,
1226 static clib_error_t *
1227 vl_api_trace_command (vlib_main_t * vm,
1228 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1231 vl_api_trace_which_t which = VL_API_TRACE_RX;
1232 api_main_t *am = &api_main;
1234 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1236 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1238 else if (unformat (input, "tx nitems %u", &nitems)
1239 || unformat (input, "tx"))
1241 which = VL_API_TRACE_RX;
1244 else if (unformat (input, "on rx"))
1246 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1248 else if (unformat (input, "on tx"))
1250 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1252 else if (unformat (input, "on"))
1254 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1256 else if (unformat (input, "off"))
1258 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1259 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1261 else if (unformat (input, "free"))
1263 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1264 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1265 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1266 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1268 else if (unformat (input, "debug on"))
1270 am->msg_print_flag = 1;
1272 else if (unformat (input, "debug off"))
1274 am->msg_print_flag = 0;
1277 return clib_error_return (0, "unknown input `%U'",
1278 format_unformat_error, input);
1283 if (vl_msg_api_trace_configure (am, which, nitems))
1285 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1293 * Control the binary API trace mechanism
1296 VLIB_CLI_COMMAND (trace, static) =
1298 .path = "set api-trace [on][on tx][on rx][off][free][debug on][debug off]",
1299 .short_help = "API trace",
1300 .function = vl_api_trace_command,
1305 vlibmemory_init (vlib_main_t * vm)
1307 api_main_t *am = &api_main;
1308 svm_map_region_args_t _a, *a = &_a;
1310 memset (a, 0, sizeof (*a));
1311 a->root_path = am->root_path;
1312 a->name = SVM_GLOBAL_REGION_NAME;
1313 a->baseva = (am->global_baseva != 0) ?
1314 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1315 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1316 a->flags = SVM_FLAGS_NODATA;
1317 a->uid = am->api_uid;
1318 a->gid = am->api_gid;
1320 (am->global_pvt_heap_size !=
1321 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
1323 svm_region_init_args (a);
1327 VLIB_INIT_FUNCTION (vlibmemory_init);
1330 vl_set_memory_region_name (const char *name)
1332 api_main_t *am = &api_main;
1334 am->region_name = name;
1338 range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
1340 int len0, len1, clen;
1342 len0 = vec_len (a0->name);
1343 len1 = vec_len (a1->name);
1344 clen = len0 < len1 ? len0 : len1;
1345 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
1349 format_api_msg_range (u8 * s, va_list * args)
1351 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
1354 s = format (s, "%-50s%9s%9s", "Name", "First-ID", "Last-ID");
1356 s = format (s, "%-50s%9d%9d", rp->name, rp->first_msg_id,
1362 static clib_error_t *
1363 vl_api_show_plugin_command (vlib_main_t * vm,
1364 unformat_input_t * input,
1365 vlib_cli_command_t * cli_cmd)
1367 api_main_t *am = &api_main;
1368 vl_api_msg_range_t *rp = 0;
1371 if (vec_len (am->msg_ranges) == 0)
1373 vlib_cli_output (vm, "No plugin API message ranges configured...");
1377 rp = vec_dup (am->msg_ranges);
1379 vec_sort_with_function (rp, range_compare);
1381 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1382 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1384 for (i = 0; i < vec_len (rp); i++)
1385 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1393 * Display the plugin binary API message range table
1396 VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) =
1398 .path = "show api plugin",
1399 .short_help = "show api plugin",
1400 .function = vl_api_show_plugin_command,
1405 vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
1407 vl_api_rpc_call_reply_t *rmp;
1410 vlib_main_t *vm = vlib_get_main ();
1412 if (mp->function == 0)
1415 clib_warning ("rpc NULL function pointer");
1420 if (mp->need_barrier_sync)
1421 vlib_worker_thread_barrier_sync (vm);
1423 fp = uword_to_pointer (mp->function, int (*)(void *));
1426 if (mp->need_barrier_sync)
1427 vlib_worker_thread_barrier_release (vm);
1432 unix_shared_memory_queue_t *q =
1433 vl_api_client_index_to_input_queue (mp->client_index);
1436 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
1437 rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
1438 rmp->context = mp->context;
1440 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1445 clib_warning ("multicast not yet implemented...");
1450 vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t * mp)
1452 clib_warning ("unimplemented");
1456 vl_api_rpc_call_main_thread_inline (void *fp, u8 * data, u32 data_length,
1459 vl_api_rpc_call_t *mp;
1460 api_main_t *am = &api_main;
1461 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
1462 unix_shared_memory_queue_t *q;
1464 /* Main thread: call the function directly */
1465 if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
1467 vlib_main_t *vm = vlib_get_main ();
1468 void (*call_fp) (void *);
1470 vlib_worker_thread_barrier_sync (vm);
1475 vlib_worker_thread_barrier_release (vm);
1479 /* Any other thread, actually do an RPC call... */
1480 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
1482 memset (mp, 0, sizeof (*mp));
1483 clib_memcpy (mp->data, data, data_length);
1484 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
1485 mp->function = pointer_to_uword (fp);
1486 mp->need_barrier_sync = 1;
1489 * Use the "normal" control-plane mechanism for the main thread.
1490 * Well, almost. if the main input queue is full, we cannot
1491 * block. Otherwise, we can expect a barrier sync timeout.
1493 q = shmem_hdr->vl_input_queue;
1495 while (pthread_mutex_trylock (&q->mutex))
1496 vlib_worker_thread_barrier_check ();
1498 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1500 pthread_mutex_unlock (&q->mutex);
1501 vlib_worker_thread_barrier_check ();
1502 while (pthread_mutex_trylock (&q->mutex))
1503 vlib_worker_thread_barrier_check ();
1506 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1508 pthread_mutex_unlock (&q->mutex);
1512 * Check if called from worker threads.
1513 * If so, make rpc call of fp through shmem.
1514 * Otherwise, call fp directly
1517 vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1519 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1524 * Always make rpc call of fp through shmem, useful for calling from threads
1525 * not setup as worker threads, such as DPDK callback thread
1528 vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1530 vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
1535 vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1537 api_main_t *am = &api_main;
1538 vl_api_msg_range_t *rp;
1541 /* Noop (except for tracing) during normal operation */
1542 if (am->replay_in_progress == 0)
1545 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1548 clib_warning ("WARNING: traced plugin '%s' not in current image",
1553 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1554 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1556 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1557 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1561 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1563 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1564 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1569 #define foreach_rpc_api_msg \
1570 _(RPC_CALL,rpc_call) \
1571 _(RPC_CALL_REPLY,rpc_call_reply)
1573 #define foreach_plugin_trace_msg \
1574 _(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1577 * Set the rpc callback at our earliest possible convenience.
1578 * This avoids ordering issues between thread_init() -> start_workers and
1579 * an init function which we could define here. If we ever intend to use
1580 * vlib all by itself, we can't create a link-time dependency on
1581 * an init function here and a typical "call foo_init first"
1585 extern void *rpc_call_main_thread_cb_fn;
1587 static clib_error_t *
1588 rpc_api_hookup (vlib_main_t * vm)
1590 api_main_t *am = &api_main;
1592 vl_msg_api_set_handlers(VL_API_##N, #n, \
1593 vl_api_##n##_t_handler, \
1596 vl_api_##n##_t_print, \
1597 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1598 foreach_rpc_api_msg;
1602 vl_msg_api_set_handlers(VL_API_##N, #n, \
1603 vl_api_##n##_t_handler, \
1606 vl_api_##n##_t_print, \
1607 sizeof(vl_api_##n##_t), 1 /* do trace */);
1608 foreach_plugin_trace_msg;
1611 /* No reason to halt the parade to create a trace record... */
1612 am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
1613 rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
1617 VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1628 format_vl_msg_api_trace_status (u8 * s, va_list * args)
1630 api_main_t *am = va_arg (*args, api_main_t *);
1631 vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t);
1637 case VL_API_TRACE_TX:
1639 trace_name = "TX trace";
1642 case VL_API_TRACE_RX:
1644 trace_name = "RX trace";
1653 s = format (s, "%s: not yet configured.\n", trace_name);
1657 s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n",
1658 trace_name, vec_len (tp->traces), tp->nitems,
1659 tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not");
1663 void vl_msg_api_custom_dump_configure (api_main_t * am)
1664 __attribute__ ((weak));
1666 vl_msg_api_custom_dump_configure (api_main_t * am)
1671 vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
1672 u32 first_index, u32 last_index,
1673 vl_api_replay_t which)
1675 vl_api_trace_file_header_t *hp;
1680 u8 endian_swap_needed = 0;
1681 api_main_t *am = &api_main;
1684 void **saved_print_handlers = 0;
1686 fd = open ((char *) filename, O_RDONLY);
1690 vlib_cli_output (vm, "Couldn't open %s\n", filename);
1694 if (fstat (fd, &statb) < 0)
1696 vlib_cli_output (vm, "Couldn't stat %s\n", filename);
1701 if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
1703 vlib_cli_output (vm, "File not plausible: %s\n", filename);
1708 file_size = statb.st_size;
1709 file_size = (file_size + 4095) & ~(4096);
1711 hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1713 if (hp == (vl_api_trace_file_header_t *) MAP_FAILED)
1715 vlib_cli_output (vm, "mmap failed: %s\n", filename);
1721 if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
1722 || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
1723 endian_swap_needed = 1;
1725 if (endian_swap_needed)
1726 nitems = ntohl (hp->nitems);
1728 nitems = hp->nitems;
1730 if (last_index == (u32) ~ 0)
1732 last_index = nitems - 1;
1735 if (first_index >= nitems || last_index >= nitems)
1737 vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n",
1738 first_index, last_index, nitems - 1);
1739 munmap (hp, file_size);
1743 vlib_cli_output (vm,
1744 "Note: wrapped/incomplete trace, results may vary\n");
1746 if (which == CUSTOM_DUMP)
1748 saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
1749 vl_msg_api_custom_dump_configure (am);
1753 msg = (u8 *) (hp + 1);
1755 for (i = 0; i < first_index; i++)
1761 size = clib_host_to_net_u32 (*(u32 *) msg);
1762 msg += sizeof (u32);
1764 if (clib_arch_is_little_endian)
1765 msg_id = ntohs (*((u16 *) msg));
1767 msg_id = *((u16 *) msg);
1769 cfgp = am->api_trace_cfg + msg_id;
1772 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1773 munmap (hp, file_size);
1779 if (which == REPLAY)
1780 am->replay_in_progress = 1;
1782 for (; i <= last_index; i++)
1790 vlib_cli_output (vm, "---------- trace %d -----------\n", i);
1792 size = clib_host_to_net_u32 (*(u32 *) msg);
1793 msg += sizeof (u32);
1795 if (clib_arch_is_little_endian)
1796 msg_id = ntohs (*((u16 *) msg));
1798 msg_id = *((u16 *) msg);
1800 cfgp = am->api_trace_cfg + msg_id;
1803 vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
1804 munmap (hp, file_size);
1806 am->replay_in_progress = 0;
1810 /* Copy the buffer (from the read-only mmap'ed file) */
1811 vec_validate (tmpbuf, size - 1 + sizeof (uword));
1812 clib_memcpy (tmpbuf + sizeof (uword), msg, size);
1813 memset (tmpbuf, 0xf, sizeof (uword));
1816 * Endian swap if needed. All msg data is supposed to be
1817 * in network byte order. All msg handlers are supposed to
1818 * know that. The generic message dumpers don't know that.
1819 * One could fix apigen, I suppose.
1821 if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
1823 void (*endian_fp) (void *);
1824 if (msg_id >= vec_len (am->msg_endian_handlers)
1825 || (am->msg_endian_handlers[msg_id] == 0))
1827 vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
1828 munmap (hp, file_size);
1830 am->replay_in_progress = 0;
1833 endian_fp = am->msg_endian_handlers[msg_id];
1834 (*endian_fp) (tmpbuf + sizeof (uword));
1837 /* msg_id always in network byte order */
1838 if (clib_arch_is_little_endian)
1840 msg_idp = (u16 *) (tmpbuf + sizeof (uword));
1848 if (msg_id < vec_len (am->msg_print_handlers) &&
1849 am->msg_print_handlers[msg_id])
1851 u8 *(*print_fp) (void *, void *);
1853 print_fp = (void *) am->msg_print_handlers[msg_id];
1854 (*print_fp) (tmpbuf + sizeof (uword), vm);
1858 vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
1865 if (msg_id < vec_len (am->msg_print_handlers) &&
1866 am->msg_print_handlers[msg_id])
1870 u8 *(*print_fp) (void *, void *);
1872 print_fp = (void *) am->msg_print_handlers[msg_id];
1874 vlib_cli_output (vm, "/*");
1876 (*print_fp) (tmpbuf + sizeof (uword), vm);
1877 vlib_cli_output (vm, "*/\n");
1879 s = format (0, "static u8 * vl_api_%s_%d[%d] = {",
1880 am->msg_names[msg_id], i,
1881 am->api_trace_cfg[msg_id].size);
1883 for (j = 0; j < am->api_trace_cfg[msg_id].size; j++)
1886 s = format (s, "\n ");
1887 s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]);
1889 s = format (s, "\n};\n%c", 0);
1890 vlib_cli_output (vm, (char *) s);
1896 if (msg_id < vec_len (am->msg_print_handlers) &&
1897 am->msg_print_handlers[msg_id] && cfgp->replay_enable)
1899 void (*handler) (void *);
1901 handler = (void *) am->msg_handlers[msg_id];
1903 if (!am->is_mp_safe[msg_id])
1904 vl_msg_api_barrier_sync ();
1905 (*handler) (tmpbuf + sizeof (uword));
1906 if (!am->is_mp_safe[msg_id])
1907 vl_msg_api_barrier_release ();
1911 if (cfgp->replay_enable)
1912 vlib_cli_output (vm, "Skipping msg id %d: no handler\n",
1919 _vec_len (tmpbuf) = 0;
1923 if (saved_print_handlers)
1925 clib_memcpy (am->msg_print_handlers, saved_print_handlers,
1926 vec_len (am->msg_print_handlers) * sizeof (void *));
1927 vec_free (saved_print_handlers);
1930 munmap (hp, file_size);
1932 am->replay_in_progress = 0;
1935 static clib_error_t *
1936 api_trace_command_fn (vlib_main_t * vm,
1937 unformat_input_t * input, vlib_cli_command_t * cmd)
1939 u32 nitems = 256 << 10;
1940 api_main_t *am = &api_main;
1941 vl_api_trace_which_t which = VL_API_TRACE_RX;
1944 u32 last = (u32) ~ 0;
1948 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1950 if (unformat (input, "on") || unformat (input, "enable"))
1952 if (unformat (input, "nitems %d", &nitems))
1954 vl_msg_api_trace_configure (am, which, nitems);
1955 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
1957 else if (unformat (input, "off"))
1959 vl_msg_api_trace_onoff (am, which, 0);
1961 else if (unformat (input, "save %s", &filename))
1963 u8 *chroot_filename;
1964 if (strstr ((char *) filename, "..")
1965 || index ((char *) filename, '/'))
1967 vlib_cli_output (vm, "illegal characters in filename '%s'",
1972 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1974 vec_free (filename);
1976 fp = fopen ((char *) chroot_filename, "w");
1979 vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
1982 rv = vl_msg_api_trace_save (am, which, fp);
1985 vlib_cli_output (vm, "API Trace data not present\n");
1987 vlib_cli_output (vm, "File for writing is closed\n");
1989 vlib_cli_output (vm, "Error while writing header to file\n");
1991 vlib_cli_output (vm, "Error while writing trace to file\n");
1993 vlib_cli_output (vm,
1994 "Error while writing end of buffer trace to file\n");
1996 vlib_cli_output (vm,
1997 "Error while writing start of buffer trace to file\n");
1999 vlib_cli_output (vm, "Unkown error while saving: %d", rv);
2001 vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
2002 vec_free (chroot_filename);
2004 else if (unformat (input, "dump %s", &filename))
2006 vl_msg_api_process_file (vm, filename, first, last, DUMP);
2008 else if (unformat (input, "custom-dump %s", &filename))
2010 vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP);
2012 else if (unformat (input, "replay %s", &filename))
2014 vl_msg_api_process_file (vm, filename, first, last, REPLAY);
2016 else if (unformat (input, "initializers %s", &filename))
2018 vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS);
2020 else if (unformat (input, "tx"))
2022 which = VL_API_TRACE_TX;
2024 else if (unformat (input, "first %d", &first))
2028 else if (unformat (input, "last %d", &last))
2032 else if (unformat (input, "status"))
2034 vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status,
2037 else if (unformat (input, "free"))
2039 vl_msg_api_trace_onoff (am, which, 0);
2040 vl_msg_api_trace_free (am, which);
2042 else if (unformat (input, "post-mortem-on"))
2043 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2044 else if (unformat (input, "post-mortem-off"))
2045 vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ );
2047 return clib_error_return (0, "unknown input `%U'",
2048 format_unformat_error, input);
2054 * Display, replay, or save a binary API trace
2058 VLIB_CLI_COMMAND (api_trace_command, static) =
2060 .path = "api trace",
2062 "api trace [on|off][dump|save|replay <file>][status][free][post-mortem-on]",
2063 .function = api_trace_command_fn,
2067 static clib_error_t *
2068 api_config_fn (vlib_main_t * vm, unformat_input_t * input)
2070 u32 nitems = 256 << 10;
2071 vl_api_trace_which_t which = VL_API_TRACE_RX;
2072 api_main_t *am = &api_main;
2074 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2076 if (unformat (input, "on") || unformat (input, "enable"))
2078 if (unformat (input, "nitems %d", &nitems))
2080 vl_msg_api_trace_configure (am, which, nitems);
2081 vl_msg_api_trace_onoff (am, which, 1 /* on */ );
2082 vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ );
2084 else if (unformat (input, "save-api-table %s",
2085 &am->save_msg_table_filename))
2088 return clib_error_return (0, "unknown input `%U'",
2089 format_unformat_error, input);
2095 * This module has three configuration parameters:
2096 * "on" or "enable" - enables binary api tracing
2097 * "nitems <nnn>" - sets the size of the circular buffer to <nnn>
2098 * "save-api-table <filename>" - dumps the API message table to /tmp/<filename>
2100 VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace");
2102 static clib_error_t *
2103 api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
2105 api_main_t *am = &api_main;
2108 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2110 if (unformat (input, "length %d", &nitems) ||
2111 (unformat (input, "len %d", &nitems)))
2114 am->vlib_input_queue_length = nitems;
2116 clib_warning ("vlib input queue length %d too small, ignored",
2120 return clib_error_return (0, "unknown input `%U'",
2121 format_unformat_error, input);
2126 VLIB_CONFIG_FUNCTION (api_queue_config_fn, "api-queue");
2129 extract_name (u8 * s)
2135 while (vec_len (rv) && rv[vec_len (rv)] != '_')
2138 rv[vec_len (rv)] = 0;
2144 extract_crc (u8 * s)
2151 for (i = vec_len (rv) - 1; i >= 0; i--)
2155 vec_delete (rv, i + 1, 0);
2169 } msg_table_unserialize_t;
2172 table_id_cmp (void *a1, void *a2)
2174 msg_table_unserialize_t *n1 = a1;
2175 msg_table_unserialize_t *n2 = a2;
2177 return (n1->msg_index - n2->msg_index);
2181 table_name_and_crc_cmp (void *a1, void *a2)
2183 msg_table_unserialize_t *n1 = a1;
2184 msg_table_unserialize_t *n2 = a2;
2186 return strcmp ((char *) n1->name_and_crc, (char *) n2->name_and_crc);
2189 static clib_error_t *
2190 dump_api_table_file_command_fn (vlib_main_t * vm,
2191 unformat_input_t * input,
2192 vlib_cli_command_t * cmd)
2195 api_main_t *am = &api_main;
2196 serialize_main_t _sm, *sm = &_sm;
2197 clib_error_t *error;
2201 int compare_current = 0;
2202 int numeric_sort = 0;
2203 msg_table_unserialize_t *table = 0, *item;
2205 u32 ndifferences = 0;
2207 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2209 if (unformat (input, "file %s", &filename))
2211 else if (unformat (input, "compare-current")
2212 || unformat (input, "compare"))
2213 compare_current = 1;
2214 else if (unformat (input, "numeric"))
2217 return clib_error_return (0, "unknown input `%U'",
2218 format_unformat_error, input);
2221 if (numeric_sort && compare_current)
2222 return clib_error_return
2223 (0, "Comparison and numeric sorting are incompatible");
2226 return clib_error_return (0, "File not specified");
2228 /* Load the serialized message table from the table dump */
2230 error = unserialize_open_unix_file (sm, (char *) filename);
2235 unserialize_integer (sm, &nmsgs, sizeof (u32));
2237 for (i = 0; i < nmsgs; i++)
2239 msg_index = unserialize_likely_small_unsigned_integer (sm);
2240 unserialize_cstring (sm, (char **) &name_and_crc);
2241 vec_add2 (table, item, 1);
2242 item->msg_index = msg_index;
2243 item->name_and_crc = name_and_crc;
2244 item->name = extract_name (name_and_crc);
2245 item->crc = extract_crc (name_and_crc);
2246 item->which = 0; /* file */
2248 serialize_close (sm);
2250 /* Compare with the current image? */
2251 if (compare_current)
2253 /* Append the current message table */
2254 u8 *tblv = vec_dup (am->serialized_message_table_in_shmem);
2256 serialize_open_vector (sm, tblv);
2257 unserialize_integer (sm, &nmsgs, sizeof (u32));
2259 for (i = 0; i < nmsgs; i++)
2261 msg_index = unserialize_likely_small_unsigned_integer (sm);
2262 unserialize_cstring (sm, (char **) &name_and_crc);
2264 vec_add2 (table, item, 1);
2265 item->msg_index = msg_index;
2266 item->name_and_crc = name_and_crc;
2267 item->name = extract_name (name_and_crc);
2268 item->crc = extract_crc (name_and_crc);
2269 item->which = 1; /* current_image */
2273 /* Sort the table. */
2275 vec_sort_with_function (table, table_id_cmp);
2277 vec_sort_with_function (table, table_name_and_crc_cmp);
2279 if (compare_current)
2284 * In this case, the recovered table will have two entries per
2285 * API message. So, if entries i and i+1 match, the message definitions
2286 * are identical. Otherwise, the crc is different, or a message is
2287 * present in only one of the tables.
2289 vlib_cli_output (vm, "%=60s %s", "Message Name", "Result");
2291 for (i = 0; i < vec_len (table);)
2293 /* Last message lonely? */
2294 if (i == vec_len (table) - 1)
2300 /* Identical pair? */
2302 ((char *) table[i].name_and_crc,
2303 (char *) table[i + 1].name_and_crc,
2304 vec_len (table[i].name_and_crc)))
2312 /* Only in one of two tables? */
2313 if (strncmp ((char *) table[i].name, (char *) table[i + 1].name,
2314 vec_len (table[i].name)))
2317 vlib_cli_output (vm, "%-60s only in %s",
2318 table[i].name, table[i].which ?
2323 /* In both tables, but with different signatures */
2324 vlib_cli_output (vm, "%-60s definition changed", table[i].name);
2327 if (ndifferences == 0)
2328 vlib_cli_output (vm, "No api message signature differences found.");
2330 vlib_cli_output (vm, "Found %u api message signature differences",
2335 /* Dump the table, sorted as shown above */
2336 vlib_cli_output (vm, "%=60s %=8s %=10s", "Message name", "MsgID", "CRC");
2338 for (i = 0; i < vec_len (table); i++)
2341 vlib_cli_output (vm, "%-60s %8u %10s", item->name,
2342 item->msg_index, item->crc);
2346 for (i = 0; i < vec_len (table); i++)
2348 vec_free (table[i].name_and_crc);
2349 vec_free (table[i].name);
2350 vec_free (table[i].crc);
2359 * Displays a serialized API message decode table, sorted by message name
2362 * @cliexstart{show api dump file <filename>}
2363 * Message name MsgID CRC
2364 * accept_session 407 8e2a127e
2365 * accept_session_reply 408 67d8c22a
2366 * add_node_next 549 e4202993
2367 * add_node_next_reply 550 e89d6eed
2373 * Compares a serialized API message decode table with the current image
2376 * @cliexstart{show api dump file <filename> compare}
2377 * ip_add_del_route definition changed
2378 * ip_table_add_del definition changed
2379 * l2_macs_event only in image
2380 * vnet_ip4_fib_counters only in file
2381 * vnet_ip4_nbr_counters only in file
2386 * Display a serialized API message decode table, compare a saved
2387 * decode table with the current image, to establish API differences.
2391 VLIB_CLI_COMMAND (dump_api_table_file, static) =
2393 .path = "show api dump",
2394 .short_help = "show api dump file <filename> [numeric | compare-current]",
2395 .function = dump_api_table_file_command_fn,
2400 * fd.io coding-style-patch-verification: ON
2403 * eval: (c-set-style "gnu")