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>
27 #include <vppinfra/vec.h>
28 #include <vppinfra/hash.h>
29 #include <vppinfra/pool.h>
30 #include <vppinfra/format.h>
31 #include <vppinfra/byte_order.h>
32 #include <vppinfra/elog.h>
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
36 #include <vlibapi/api.h>
37 #include <vlibmemory/api.h>
39 #define TRACE_VLIB_MEMORY_QUEUE 0
41 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
43 #define vl_typedefs /* define message structures */
44 #include <vlibmemory/vl_memory_api_h.h>
47 /* instantiate all the print functions we know about */
48 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
50 #include <vlibmemory/vl_memory_api_h.h>
54 vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
56 vl_print (handle, "vl_api_memclnt_create_t:\n");
57 vl_print (handle, "name: %s\n", a->name);
58 vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
59 vl_print (handle, "context: %u\n", (unsigned) a->context);
60 vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
65 vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
67 vl_print (handle, "vl_api_memclnt_delete_t:\n");
68 vl_print (handle, "index: %u\n", (unsigned) a->index);
69 vl_print (handle, "handle: 0x%wx\n", a->handle);
74 vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a,
77 vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n",
79 clib_host_to_net_u16 (a->first_msg_id),
80 clib_host_to_net_u16 (a->last_msg_id));
84 /* instantiate all the endian swap functions we know about */
86 #include <vlibmemory/vl_memory_api_h.h>
89 void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
90 __attribute__ ((weak));
93 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
98 clib_warning ("need to link against -lvlibsocket, msg not sent!");
102 vl_msg_api_send (vl_api_registration_t * rp, u8 * elem)
104 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
106 vl_socket_api_send (rp, elem);
110 vl_msg_api_send_shmem (rp->vl_input_queue, elem);
115 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
117 serialize_main_t _sm, *sm = &_sm;
119 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
121 serialize_open_vector (sm, vector);
123 /* serialize the count */
124 serialize_integer (sm, nmsg, sizeof (u32));
127 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
129 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
130 serialize_cstring (sm, (char *) hp->key);
134 return serialize_close_vector (sm);
138 * vl_api_memclnt_create_t_handler
141 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
143 vl_api_registration_t **regpp;
144 vl_api_registration_t *regp;
145 vl_api_memclnt_create_reply_t *rp;
147 unix_shared_memory_queue_t *q;
150 api_main_t *am = &api_main;
151 u8 *serialized_message_table = 0;
154 * This is tortured. Maintain a vlib-address-space private
155 * pool of client registrations. We use the shared-memory virtual
156 * address of client structure as a handle, to allow direct
157 * manipulation of context quota vbls from the client library.
159 * This scheme causes trouble w/ API message trace replay, since
160 * some random VA from clib_mem_alloc() certainly won't
161 * occur in the Linux sim. The (very) few places
162 * that care need to use the pool index.
164 * Putting the registration object(s) into a pool in shared memory and
165 * using the pool index as a handle seems like a great idea.
166 * Unfortunately, each and every reference to that pool would need
167 * to be protected by a mutex:
171 * convert pool index to
179 pool_get (am->vl_clients, regpp);
183 if (am->serialized_message_table_in_shmem == 0)
184 serialized_message_table = vl_api_serialize_message_table (am, 0);
186 pthread_mutex_lock (&svm->mutex);
187 oldheap = svm_push_data_heap (svm);
188 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
191 memset (regp, 0, sizeof (*regp));
192 regp->registration_type = REGISTRATION_TYPE_SHMEM;
193 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
195 q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword)
198 regp->name = format (0, "%s", mp->name);
199 vec_add1 (regp->name, 0);
200 if (serialized_message_table)
201 am->serialized_message_table_in_shmem =
202 vec_dup (serialized_message_table);
204 pthread_mutex_unlock (&svm->mutex);
205 svm_pop_heap (oldheap);
207 vec_free (serialized_message_table);
209 rp = vl_msg_api_alloc (sizeof (*rp));
210 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
211 rp->handle = (uword) regp;
212 rp->index = vl_msg_api_handle_from_index_and_epoch
213 (regp->vl_api_registration_pool_index,
214 am->shmem_hdr->application_restarts);
215 rp->context = mp->context;
216 rp->response = ntohl (rv);
217 rp->message_table = (u64) am->serialized_message_table_in_shmem;
219 vl_msg_api_send_shmem (q, (u8 *) & rp);
222 /* Application callback to clean up leftover registrations from this client */
223 int vl_api_memclnt_delete_callback (u32 client_index) __attribute__ ((weak));
226 vl_api_memclnt_delete_callback (u32 client_index)
232 * vl_api_memclnt_delete_t_handler
235 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
237 vl_api_registration_t **regpp;
238 vl_api_registration_t *regp;
239 vl_api_memclnt_delete_reply_t *rp;
242 api_main_t *am = &api_main;
243 u32 handle, client_index, epoch;
247 if (vl_api_memclnt_delete_callback (handle))
250 epoch = vl_msg_api_handle_get_epoch (handle);
251 client_index = vl_msg_api_handle_get_index (handle);
253 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
256 ("Stale clnt delete index %d old epoch %d cur epoch %d",
258 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
262 regpp = am->vl_clients + client_index;
264 if (!pool_is_free (am->vl_clients, regpp))
269 /* $$$ check the input queue for e.g. punted sf's */
271 rp = vl_msg_api_alloc (sizeof (*rp));
272 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
273 rp->handle = mp->handle;
276 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
278 if (client_index != regp->vl_api_registration_pool_index)
280 clib_warning ("mismatch client_index %d pool_index %d",
281 client_index, regp->vl_api_registration_pool_index);
282 vl_msg_api_free (rp);
286 /* No dangling references, please */
289 pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index);
291 pthread_mutex_lock (&svm->mutex);
292 oldheap = svm_push_data_heap (svm);
293 /* Poison the old registration */
294 memset (regp, 0xF1, sizeof (*regp));
295 clib_mem_free (regp);
296 pthread_mutex_unlock (&svm->mutex);
297 svm_pop_heap (oldheap);
301 clib_warning ("unknown client ID %d", mp->index);
306 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp)
308 vl_api_get_first_msg_id_reply_t *rmp;
309 unix_shared_memory_queue_t *q;
311 api_main_t *am = &api_main;
312 vl_api_msg_range_t *rp;
314 u16 first_msg_id = ~0;
315 int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
317 q = vl_api_client_index_to_input_queue (mp->client_index);
321 if (am->msg_range_by_name == 0)
324 strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1);
326 p = hash_get_mem (am->msg_range_by_name, name);
330 rp = vec_elt_at_index (am->msg_ranges, p[0]);
332 first_msg_id = rp->first_msg_id;
337 rmp = vl_msg_api_alloc (sizeof (*rmp));
338 rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
339 rmp->context = mp->context;
340 rmp->retval = ntohl (rv);
341 rmp->first_msg_id = ntohs (first_msg_id);
342 vl_msg_api_send_shmem (q, (u8 *) & rmp);
345 #define foreach_vlib_api_msg \
346 _(MEMCLNT_CREATE, memclnt_create) \
347 _(MEMCLNT_DELETE, memclnt_delete) \
348 _(GET_FIRST_MSG_ID, get_first_msg_id)
354 memory_api_init (char *region_name)
357 vl_msg_api_msg_config_t cfg;
358 vl_msg_api_msg_config_t *c = &cfg;
360 memset (c, 0, sizeof (*c));
362 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
365 #define _(N,n) do { \
366 c->id = VL_API_##N; \
368 c->handler = vl_api_##n##_t_handler; \
369 c->cleanup = vl_noop_handler; \
370 c->endian = vl_api_##n##_t_endian; \
371 c->print = vl_api_##n##_t_print; \
372 c->size = sizeof(vl_api_##n##_t); \
373 c->traced = 1; /* trace, so these msgs print */ \
374 c->replay = 0; /* don't replay client create/delete msgs */ \
375 c->message_bounce = 0; /* don't bounce this message */ \
376 vl_msg_api_config(c);} while (0);
378 foreach_vlib_api_msg;
384 #define foreach_histogram_bucket \
392 #define _(n) SLEEP_##n##_US,
393 foreach_histogram_bucket
398 static u64 vector_rate_histogram[SLEEP_N_BUCKETS];
400 static void memclnt_queue_callback (vlib_main_t * vm);
403 * Callback to send ourselves a plugin numbering-space trace msg
406 send_one_plugin_msg_ids_msg (u8 * name, u16 first_msg_id, u16 last_msg_id)
408 vl_api_trace_plugin_msg_ids_t *mp;
409 api_main_t *am = &api_main;
410 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
411 unix_shared_memory_queue_t *q;
413 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
414 memset (mp, 0, sizeof (*mp));
416 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
417 strncpy ((char *) mp->plugin_name, (char *) name,
418 sizeof (mp->plugin_name) - 1);
419 mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
420 mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
422 q = shmem_hdr->vl_input_queue;
424 vl_msg_api_send_shmem (q, (u8 *) & mp);
428 memclnt_process (vlib_main_t * vm,
429 vlib_node_runtime_t * node, vlib_frame_t * f)
433 unix_shared_memory_queue_t *q;
436 api_main_t *am = &api_main;
437 f64 dead_client_scan_time;
438 f64 sleep_time, start_time;
442 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
444 if ((rv = memory_api_init (am->region_name)) < 0)
446 clib_warning ("memory_api_init returned %d, wait for godot...", rv);
447 vlib_process_suspend (vm, 1e70);
452 q = shm->vl_input_queue;
455 e = vlib_call_init_exit_functions
456 (vm, vm->api_init_function_registrations, 1 /* call_once */ );
458 clib_error_report (e);
461 dead_client_scan_time = vlib_time_now (vm) + 20.0;
464 * Send plugin message range messages for each plugin we loaded
466 for (i = 0; i < vec_len (am->msg_ranges); i++)
468 vl_api_msg_range_t *rp = am->msg_ranges + i;
469 send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
473 /* $$$ pay attention to frame size, control CPU usage */
476 uword event_type __attribute__ ((unused));
481 * There's a reason for checking the queue before
482 * sleeping. If the vlib application crashes, it's entirely
483 * possible for a client to enqueue a connect request
484 * during the process restart interval.
486 * Unless some force of physics causes the new incarnation
487 * of the application to process the request, the client will
488 * sit and wait for Godot...
490 vector_rate = vlib_last_vector_length_per_node (vm);
491 start_time = vlib_time_now (vm);
494 pthread_mutex_lock (&q->mutex);
497 vm->api_queue_nonempty = 0;
498 pthread_mutex_unlock (&q->mutex);
500 if (TRACE_VLIB_MEMORY_QUEUE)
503 ELOG_TYPE_DECLARE (e) =
505 .format = "q-underflow: len %d",
513 ed = ELOG_DATA (&vm->elog_main, e);
520 headp = (i8 *) (q->data + sizeof (uword) * q->head);
521 clib_memcpy (&mp, headp, sizeof (uword));
524 need_broadcast = (q->cursize == q->maxsize / 2);
527 if (PREDICT_FALSE (q->head == q->maxsize))
529 pthread_mutex_unlock (&q->mutex);
531 (void) pthread_cond_broadcast (&q->condvar);
533 vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
535 /* Allow no more than 10us without a pause */
536 if (vlib_time_now (vm) > start_time + 10e-6)
538 int index = SLEEP_400_US;
539 if (vector_rate > 40.0)
541 else if (vector_rate > 20.0)
543 index = SLEEP_200_US;
546 else if (vector_rate >= 1.0)
548 index = SLEEP_100_US;
556 vector_rate_histogram[index] += 1;
561 event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time);
562 vm->queue_signal_pending = 0;
563 vlib_process_get_events (vm, 0 /* event_data */ );
565 if (vlib_time_now (vm) > dead_client_scan_time)
567 vl_api_registration_t **regpp;
568 vl_api_registration_t *regp;
569 unix_shared_memory_queue_t *q;
570 static u32 *dead_indices;
571 static u32 *confused_indices;
573 vec_reset_length (dead_indices);
574 vec_reset_length (confused_indices);
577 pool_foreach (regpp, am->vl_clients,
582 q = regp->vl_input_queue;
583 if (kill (q->consumer_pid, 0) < 0)
585 vec_add1(dead_indices, regpp - am->vl_clients);
590 clib_warning ("NULL client registration index %d",
591 regpp - am->vl_clients);
592 vec_add1 (confused_indices, regpp - am->vl_clients);
596 /* This should "never happen," but if it does, fix it... */
597 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
600 for (i = 0; i < vec_len (confused_indices); i++)
602 pool_put_index (am->vl_clients, confused_indices[i]);
606 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
612 /* Allow the application to clean up its registrations */
613 for (i = 0; i < vec_len (dead_indices); i++)
615 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
620 handle = vl_msg_api_handle_from_index_and_epoch
621 (dead_indices[i], shm->application_restarts);
622 (void) vl_api_memclnt_delete_callback (handle);
627 pthread_mutex_lock (&svm->mutex);
628 oldheap = svm_push_data_heap (svm);
630 for (i = 0; i < vec_len (dead_indices); i++)
632 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
635 /* Poison the old registration */
636 memset (*regpp, 0xF3, sizeof (**regpp));
637 clib_mem_free (*regpp);
638 /* no dangling references, please */
643 svm_pop_heap (oldheap);
644 clib_warning ("Duplicate free, client index %d",
645 regpp - am->vl_clients);
646 oldheap = svm_push_data_heap (svm);
650 svm_client_scan_this_region_nolock (am->vlib_rp);
652 pthread_mutex_unlock (&svm->mutex);
653 svm_pop_heap (oldheap);
654 for (i = 0; i < vec_len (dead_indices); i++)
655 pool_put_index (am->vl_clients, dead_indices[i]);
658 dead_client_scan_time = vlib_time_now (vm) + 20.0;
661 if (TRACE_VLIB_MEMORY_QUEUE)
664 ELOG_TYPE_DECLARE (e) = {
665 .format = "q-awake: len %d",
673 ed = ELOG_DATA (&vm->elog_main, e);
674 ed->len = q->cursize;
681 static clib_error_t *
682 vl_api_show_histogram_command (vlib_main_t * vm,
683 unformat_input_t * input,
684 vlib_cli_command_t * cli_cmd)
686 u64 total_counts = 0;
689 for (i = 0; i < SLEEP_N_BUCKETS; i++)
691 total_counts += vector_rate_histogram[i];
694 if (total_counts == 0)
696 vlib_cli_output (vm, "No control-plane activity.");
703 percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \
704 / (f64) total_counts; \
706 vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \
707 vector_rate_histogram[SLEEP_##n##_US], \
710 foreach_histogram_bucket;
717 VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) = {
718 .path = "show api histogram",
719 .short_help = "show api histogram",
720 .function = vl_api_show_histogram_command,
724 static clib_error_t *
725 vl_api_clear_histogram_command (vlib_main_t * vm,
726 unformat_input_t * input,
727 vlib_cli_command_t * cli_cmd)
731 for (i = 0; i < SLEEP_N_BUCKETS; i++)
732 vector_rate_histogram[i] = 0;
737 VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) = {
738 .path = "clear api histogram",
739 .short_help = "clear api histogram",
740 .function = vl_api_clear_histogram_command,
746 VLIB_REGISTER_NODE (memclnt_node,static) = {
747 .function = memclnt_process,
748 .type = VLIB_NODE_TYPE_PROCESS,
749 .name = "api-rx-from-ring",
750 .state = VLIB_NODE_STATE_DISABLED,
755 memclnt_queue_callback (vlib_main_t * vm)
757 static volatile int *cursizep;
759 if (PREDICT_FALSE (cursizep == 0))
761 api_main_t *am = &api_main;
762 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
763 unix_shared_memory_queue_t *q;
768 q = shmem_hdr->vl_input_queue;
771 cursizep = &q->cursize;
776 vm->queue_signal_pending = 1;
777 vm->api_queue_nonempty = 1;
778 vlib_process_signal_event (vm, memclnt_node.index,
779 /* event_type */ 0, /* event_data */ 0);
784 vl_enable_disable_memory_api (vlib_main_t * vm, int enable)
786 vlib_node_set_state (vm, memclnt_node.index,
788 ? VLIB_NODE_STATE_POLLING
789 : VLIB_NODE_STATE_DISABLED));
793 api_rx_from_node (vlib_main_t * vm,
794 vlib_node_runtime_t * node, vlib_frame_t * frame)
796 uword n_packets = frame->n_vectors;
801 vec_validate (long_msg, 4095);
802 n_left_from = frame->n_vectors;
803 from = vlib_frame_args (frame);
805 while (n_left_from > 0)
813 b0 = vlib_get_buffer (vm, bi0);
817 msg = b0->data + b0->current_data;
818 msg_len = b0->current_length;
819 if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
821 ASSERT (long_msg != 0);
822 _vec_len (long_msg) = 0;
823 vec_add (long_msg, msg, msg_len);
824 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
826 b0 = vlib_get_buffer (vm, b0->next_buffer);
827 msg = b0->data + b0->current_data;
828 msg_len = b0->current_length;
829 vec_add (long_msg, msg, msg_len);
833 vl_msg_api_handler_no_trace_no_free (msg);
836 /* Free what we've been given. */
837 vlib_buffer_free (vm, vlib_frame_args (frame), n_packets);
843 VLIB_REGISTER_NODE (api_rx_from_node_node,static) = {
844 .function = api_rx_from_node,
845 .type = VLIB_NODE_TYPE_INTERNAL,
847 .name = "api-rx-from-node",
851 static clib_error_t *
852 setup_memclnt_exit (vlib_main_t * vm)
854 atexit (vl_unmap_shmem);
858 VLIB_INIT_FUNCTION (setup_memclnt_exit);
861 static clib_error_t *
862 vl_api_ring_command (vlib_main_t * vm,
863 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
867 vl_shmem_hdr_t *shmem_hdr;
868 api_main_t *am = &api_main;
870 shmem_hdr = am->shmem_hdr;
874 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
878 vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n",
879 "Owner", "Size", "Nitems", "Hits", "Misses");
881 ap = shmem_hdr->vl_rings;
883 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
885 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
886 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
890 ap = shmem_hdr->client_rings;
892 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
894 vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n",
895 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
899 vlib_cli_output (vm, "%d ring miss fallback allocations\n",
903 (vm, "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
904 shmem_hdr->application_restarts,
905 shmem_hdr->restart_reclaims, shmem_hdr->garbage_collects);
909 void dump_socket_clients (vlib_main_t * vm, api_main_t * am)
910 __attribute__ ((weak));
913 dump_socket_clients (vlib_main_t * vm, api_main_t * am)
917 static clib_error_t *
918 vl_api_client_command (vlib_main_t * vm,
919 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
921 vl_api_registration_t **regpp, *regp;
922 unix_shared_memory_queue_t *q;
924 api_main_t *am = &api_main;
925 u32 *confused_indices = 0;
927 if (!pool_elts (am->vl_clients))
929 vlib_cli_output (vm, "Shared memory clients");
930 vlib_cli_output (vm, "%16s %8s %14s %18s %s",
931 "Name", "PID", "Queue Length", "Queue VA", "Health");
934 pool_foreach (regpp, am->vl_clients,
940 q = regp->vl_input_queue;
941 if (kill (q->consumer_pid, 0) < 0)
949 vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n",
950 regp->name, q->consumer_pid, q->cursize,
955 clib_warning ("NULL client registration index %d",
956 regpp - am->vl_clients);
957 vec_add1 (confused_indices, regpp - am->vl_clients);
962 /* This should "never happen," but if it does, fix it... */
963 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
966 for (i = 0; i < vec_len (confused_indices); i++)
968 pool_put_index (am->vl_clients, confused_indices[i]);
971 vec_free (confused_indices);
973 if (am->missing_clients)
974 vlib_cli_output (vm, "%u messages with missing clients",
975 am->missing_clients);
977 dump_socket_clients (vm, am);
982 static clib_error_t *
983 vl_api_status_command (vlib_main_t * vm,
984 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
986 api_main_t *am = &api_main;
988 // check if rx_trace and tx_trace are not null pointers
990 if (am->rx_trace == 0)
992 vlib_cli_output (vm, "RX Trace disabled\n");
996 if (am->rx_trace->enabled == 0)
997 vlib_cli_output (vm, "RX Trace disabled\n");
999 vlib_cli_output (vm, "RX Trace enabled\n");
1002 if (am->tx_trace == 0)
1004 vlib_cli_output (vm, "TX Trace disabled\n");
1008 if (am->tx_trace->enabled == 0)
1009 vlib_cli_output (vm, "TX Trace disabled\n");
1011 vlib_cli_output (vm, "TX Trace enabled\n");
1018 VLIB_CLI_COMMAND (cli_show_api_command, static) = {
1020 .short_help = "Show API information",
1025 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = {
1026 .path = "show api ring-stats",
1027 .short_help = "Message ring statistics",
1028 .function = vl_api_ring_command,
1033 VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = {
1034 .path = "show api clients",
1035 .short_help = "Client information",
1036 .function = vl_api_client_command,
1041 VLIB_CLI_COMMAND (cli_show_api_status_command, static) = {
1042 .path = "show api status",
1043 .short_help = "Show API trace status",
1044 .function = vl_api_status_command,
1048 static clib_error_t *
1049 vl_api_message_table_command (vlib_main_t * vm,
1050 unformat_input_t * input,
1051 vlib_cli_command_t * cli_cmd)
1053 api_main_t *am = &api_main;
1057 if (unformat (input, "verbose"))
1062 vlib_cli_output (vm, "%-4s %s", "ID", "Name");
1064 vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce",
1067 for (i = 1; i < vec_len (am->msg_names); i++)
1071 vlib_cli_output (vm, "%-4d %s", i,
1072 am->msg_names[i] ? am->msg_names[i] :
1077 vlib_cli_output (vm, "%-4d %-40s %6d %7d", i,
1078 am->msg_names[i] ? am->msg_names[i] :
1079 " [no handler]", am->message_bounce[i],
1088 VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) = {
1089 .path = "show api message-table",
1090 .short_help = "Message Table",
1091 .function = vl_api_message_table_command,
1095 static clib_error_t *
1096 vl_api_trace_command (vlib_main_t * vm,
1097 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
1100 vl_api_trace_which_t which = VL_API_TRACE_RX;
1101 api_main_t *am = &api_main;
1103 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1105 if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx"))
1107 else if (unformat (input, "tx nitems %u", &nitems)
1108 || unformat (input, "tx"))
1110 which = VL_API_TRACE_RX;
1113 else if (unformat (input, "on rx"))
1115 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1117 else if (unformat (input, "on tx"))
1119 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1);
1121 else if (unformat (input, "on"))
1123 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1);
1125 else if (unformat (input, "off"))
1127 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1128 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1130 else if (unformat (input, "free"))
1132 vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0);
1133 vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0);
1134 vl_msg_api_trace_free (am, VL_API_TRACE_RX);
1135 vl_msg_api_trace_free (am, VL_API_TRACE_TX);
1137 else if (unformat (input, "debug on"))
1139 am->msg_print_flag = 1;
1141 else if (unformat (input, "debug off"))
1143 am->msg_print_flag = 0;
1146 return clib_error_return (0, "unknown input `%U'",
1147 format_unformat_error, input);
1152 if (vl_msg_api_trace_configure (am, which, nitems))
1154 vlib_cli_output (vm, "warning: trace configure error (%d, %d)",
1162 VLIB_CLI_COMMAND (trace, static) = {
1163 .path = "set api-trace",
1164 .short_help = "API trace",
1165 .function = vl_api_trace_command,
1170 vlibmemory_init (vlib_main_t * vm)
1172 api_main_t *am = &api_main;
1173 svm_map_region_args_t _a, *a = &_a;
1175 memset (a, 0, sizeof (*a));
1176 a->root_path = am->root_path;
1177 a->name = SVM_GLOBAL_REGION_NAME;
1178 a->baseva = (am->global_baseva != 0) ?
1179 am->global_baseva : SVM_GLOBAL_REGION_BASEVA;
1180 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
1181 a->flags = SVM_FLAGS_NODATA;
1182 a->uid = am->api_uid;
1183 a->gid = am->api_gid;
1185 (am->global_pvt_heap_size !=
1186 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
1188 svm_region_init_args (a);
1192 VLIB_INIT_FUNCTION (vlibmemory_init);
1195 vl_set_memory_region_name (char *name)
1197 api_main_t *am = &api_main;
1199 am->region_name = name;
1203 range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1)
1205 int len0, len1, clen;
1207 len0 = vec_len (a0->name);
1208 len1 = vec_len (a1->name);
1209 clen = len0 < len1 ? len0 : len1;
1210 return (strncmp ((char *) a0->name, (char *) a1->name, clen));
1214 format_api_msg_range (u8 * s, va_list * args)
1216 vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *);
1219 s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID");
1221 s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id,
1227 static clib_error_t *
1228 vl_api_show_plugin_command (vlib_main_t * vm,
1229 unformat_input_t * input,
1230 vlib_cli_command_t * cli_cmd)
1232 api_main_t *am = &api_main;
1233 vl_api_msg_range_t *rp = 0;
1236 if (vec_len (am->msg_ranges) == 0)
1238 vlib_cli_output (vm, "No plugin API message ranges configured...");
1242 rp = vec_dup (am->msg_ranges);
1244 vec_sort_with_function (rp, range_compare);
1246 vlib_cli_output (vm, "Plugin API message ID ranges...\n");
1247 vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ );
1249 for (i = 0; i < vec_len (rp); i++)
1250 vlib_cli_output (vm, "%U", format_api_msg_range, rp + i);
1258 VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) = {
1259 .path = "show api plugin",
1260 .short_help = "show api plugin",
1261 .function = vl_api_show_plugin_command,
1266 vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp)
1268 vl_api_rpc_reply_t *rmp;
1271 vlib_main_t *vm = vlib_get_main ();
1273 if (mp->function == 0)
1276 clib_warning ("rpc NULL function pointer");
1281 if (mp->need_barrier_sync)
1282 vlib_worker_thread_barrier_sync (vm);
1284 fp = uword_to_pointer (mp->function, int (*)(void *));
1287 if (mp->need_barrier_sync)
1288 vlib_worker_thread_barrier_release (vm);
1293 unix_shared_memory_queue_t *q =
1294 vl_api_client_index_to_input_queue (mp->client_index);
1297 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
1298 rmp->_vl_msg_id = ntohs (VL_API_RPC_REPLY);
1299 rmp->context = mp->context;
1301 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1306 clib_warning ("multicast not yet implemented...");
1311 vl_api_rpc_reply_t_handler (vl_api_rpc_reply_t * mp)
1313 clib_warning ("unimplemented");
1317 vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length)
1319 vl_api_rpc_call_t *mp;
1320 api_main_t *am = &api_main;
1321 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
1322 unix_shared_memory_queue_t *q;
1324 /* Main thread: call the function directly */
1325 if (os_get_cpu_number () == 0)
1327 vlib_main_t *vm = vlib_get_main ();
1328 void (*call_fp) (void *);
1330 vlib_worker_thread_barrier_sync (vm);
1335 vlib_worker_thread_barrier_release (vm);
1339 /* Any other thread, actually do an RPC call... */
1340 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
1342 memset (mp, 0, sizeof (*mp));
1343 clib_memcpy (mp->data, data, data_length);
1344 mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
1345 mp->function = pointer_to_uword (fp);
1346 mp->need_barrier_sync = 1;
1349 * Use the "normal" control-plane mechanism for the main thread.
1350 * Well, almost. if the main input queue is full, we cannot
1351 * block. Otherwise, we can expect a barrier sync timeout.
1353 q = shmem_hdr->vl_input_queue;
1355 while (pthread_mutex_trylock (&q->mutex))
1356 vlib_worker_thread_barrier_check ();
1358 while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q)))
1360 pthread_mutex_unlock (&q->mutex);
1361 vlib_worker_thread_barrier_check ();
1362 while (pthread_mutex_trylock (&q->mutex))
1363 vlib_worker_thread_barrier_check ();
1366 vl_msg_api_send_shmem_nolock (q, (u8 *) & mp);
1368 pthread_mutex_unlock (&q->mutex);
1372 vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp)
1374 api_main_t *am = &api_main;
1375 vl_api_msg_range_t *rp;
1378 /* Noop (except for tracing) during normal operation */
1379 if (am->replay_in_progress == 0)
1382 p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
1385 clib_warning ("WARNING: traced plugin '%s' not in current image",
1390 rp = vec_elt_at_index (am->msg_ranges, p[0]);
1391 if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
1393 clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
1394 mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
1398 if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
1400 clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
1401 mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
1406 #define foreach_rpc_api_msg \
1407 _(RPC_CALL,rpc_call) \
1408 _(RPC_REPLY,rpc_reply)
1410 #define foreach_plugin_trace_msg \
1411 _(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids)
1413 static clib_error_t *
1414 rpc_api_hookup (vlib_main_t * vm)
1417 vl_msg_api_set_handlers(VL_API_##N, #n, \
1418 vl_api_##n##_t_handler, \
1421 vl_api_##n##_t_print, \
1422 sizeof(vl_api_##n##_t), 0 /* do not trace */);
1423 foreach_rpc_api_msg;
1427 vl_msg_api_set_handlers(VL_API_##N, #n, \
1428 vl_api_##n##_t_handler, \
1431 vl_api_##n##_t_print, \
1432 sizeof(vl_api_##n##_t), 1 /* do trace */);
1433 foreach_plugin_trace_msg;
1438 VLIB_API_INIT_FUNCTION (rpc_api_hookup);
1441 * fd.io coding-style-patch-verification: ON
1444 * eval: (c-set-style "gnu")