2 *------------------------------------------------------------------
3 * Copyright (c) 2018 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
19 #include <vlib/vlib.h>
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22 #include <vlibmemory/memory_api.h>
24 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
26 #define vl_typedefs /* define message structures */
27 #include <vlibmemory/vl_memory_api_h.h>
30 /* instantiate all the print functions we know about */
31 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
33 #include <vlibmemory/vl_memory_api_h.h>
36 /* instantiate all the endian swap functions we know about */
38 #include <vlibmemory/vl_memory_api_h.h>
42 vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
44 vl_print (handle, "vl_api_memclnt_create_t:\n");
45 vl_print (handle, "name: %s\n", a->name);
46 vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
47 vl_print (handle, "context: %u\n", (unsigned) a->context);
48 vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
53 vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
55 vl_print (handle, "vl_api_memclnt_delete_t:\n");
56 vl_print (handle, "index: %u\n", (unsigned) a->index);
57 vl_print (handle, "handle: 0x%wx\n", a->handle);
61 volatile int **vl_api_queue_cursizes;
64 memclnt_queue_callback (vlib_main_t * vm)
67 api_main_t *am = vlibapi_get_main ();
68 int have_pending_rpcs;
70 if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
71 1 + vec_len (am->vlib_private_rps)))
73 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
79 q = shmem_hdr->vl_input_queue;
83 vec_add1 (vl_api_queue_cursizes, &q->cursize);
85 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
87 svm_region_t *vlib_rp = am->vlib_private_rps[i];
89 shmem_hdr = (void *) vlib_rp->user_ctx;
90 q = shmem_hdr->vl_input_queue;
91 vec_add1 (vl_api_queue_cursizes, &q->cursize);
95 for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
97 if (*vl_api_queue_cursizes[i])
99 vm->queue_signal_pending = 1;
100 vm->api_queue_nonempty = 1;
101 vlib_process_signal_event (vm, vl_api_clnt_node.index,
102 /* event_type */ QUEUE_SIGNAL_EVENT,
108 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
109 have_pending_rpcs = vec_len (vm->pending_rpc_requests) > 0;
110 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
112 if (have_pending_rpcs)
114 vm->queue_signal_pending = 1;
115 vm->api_queue_nonempty = 1;
116 vlib_process_signal_event (vm, vl_api_clnt_node.index,
117 /* event_type */ QUEUE_SIGNAL_EVENT,
123 * vl_api_memclnt_create_internal
126 vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
128 vl_api_registration_t **regpp;
129 vl_api_registration_t *regp;
131 api_main_t *am = vlibapi_get_main ();
133 ASSERT (vlib_get_thread_index () == 0);
134 pool_get (am->vl_clients, regpp);
137 oldheap = vl_msg_push_heap ();
138 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
141 clib_memset (regp, 0, sizeof (*regp));
142 regp->registration_type = REGISTRATION_TYPE_SHMEM;
143 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
144 regp->vlib_rp = am->vlib_rp;
145 regp->shmem_hdr = am->shmem_hdr;
147 regp->vl_input_queue = q;
148 regp->name = format (0, "%s%c", name, 0);
150 vl_msg_pop_heap (oldheap);
151 return vl_msg_api_handle_from_index_and_epoch
152 (regp->vl_api_registration_pool_index,
153 am->shmem_hdr->application_restarts);
157 * vl_api_memclnt_create_t_handler
160 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
162 vl_api_registration_t **regpp;
163 vl_api_registration_t *regp;
164 vl_api_memclnt_create_reply_t *rp;
168 api_main_t *am = vlibapi_get_main ();
172 * This is tortured. Maintain a vlib-address-space private
173 * pool of client registrations. We use the shared-memory virtual
174 * address of client structure as a handle, to allow direct
175 * manipulation of context quota vbls from the client library.
177 * This scheme causes trouble w/ API message trace replay, since
178 * some random VA from clib_mem_alloc() certainly won't
179 * occur in the Linux sim. The (very) few places
180 * that care need to use the pool index.
182 * Putting the registration object(s) into a pool in shared memory and
183 * using the pool index as a handle seems like a great idea.
184 * Unfortunately, each and every reference to that pool would need
185 * to be protected by a mutex:
189 * convert pool index to
197 pool_get (am->vl_clients, regpp);
199 oldheap = vl_msg_push_heap ();
200 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
203 clib_memset (regp, 0, sizeof (*regp));
204 regp->registration_type = REGISTRATION_TYPE_SHMEM;
205 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
206 regp->vlib_rp = am->vlib_rp;
207 regp->shmem_hdr = am->shmem_hdr;
208 regp->clib_file_index = am->shmem_hdr->clib_file_index;
210 q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
211 VL_MSG_API_SVM_QUEUE_UNPOISON (q);
213 regp->name = format (0, "%s", mp->name);
214 vec_add1 (regp->name, 0);
216 if (am->serialized_message_table_in_shmem == 0)
217 am->serialized_message_table_in_shmem =
218 vl_api_serialize_message_table (am, 0);
220 if (am->vlib_rp != am->vlib_primary_rp)
221 msg_table = vl_api_serialize_message_table (am, 0);
223 msg_table = am->serialized_message_table_in_shmem;
225 vl_msg_pop_heap (oldheap);
227 rp = vl_msg_api_alloc (sizeof (*rp));
228 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
229 rp->handle = (uword) regp;
230 rp->index = vl_msg_api_handle_from_index_and_epoch
231 (regp->vl_api_registration_pool_index,
232 am->shmem_hdr->application_restarts);
233 rp->context = mp->context;
234 rp->response = ntohl (rv);
235 rp->message_table = pointer_to_uword (msg_table);
237 vl_msg_api_send_shmem (q, (u8 *) & rp);
241 vl_api_call_reaper_functions (u32 client_index)
243 clib_error_t *error = 0;
244 _vl_msg_api_function_list_elt_t *i;
246 i = vlibapi_get_main ()->reaper_function_registrations;
249 error = i->f (client_index);
251 clib_error_report (error);
252 i = i->next_init_function;
257 * vl_api_memclnt_delete_t_handler
260 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
262 vl_api_registration_t **regpp;
263 vl_api_registration_t *regp;
264 vl_api_memclnt_delete_reply_t *rp;
266 api_main_t *am = vlibapi_get_main ();
267 u32 handle, client_index, epoch;
271 vl_api_call_reaper_functions (handle);
273 epoch = vl_msg_api_handle_get_epoch (handle);
274 client_index = vl_msg_api_handle_get_index (handle);
276 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
279 ("Stale clnt delete index %d old epoch %d cur epoch %d",
281 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
285 regpp = pool_elt_at_index (am->vl_clients, client_index);
287 if (!pool_is_free (am->vl_clients, regpp))
291 int private_registration = 0;
293 /* Send reply unless client asked us to do the cleanup */
297 * Note: the API message handling path will set am->vlib_rp
298 * as appropriate for pairwise / private memory segments
300 rp = vl_msg_api_alloc (sizeof (*rp));
301 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
302 rp->handle = mp->handle;
305 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
306 if (client_index != regp->vl_api_registration_pool_index)
308 clib_warning ("mismatch client_index %d pool_index %d",
310 regp->vl_api_registration_pool_index);
311 vl_msg_api_free (rp);
316 /* No dangling references, please */
319 /* For horizontal scaling, add a hash table... */
320 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
322 /* Is this a pairwise / private API segment? */
323 if (am->vlib_private_rps[i] == am->vlib_rp)
325 /* Note: account for the memfd header page */
326 uword virtual_base = am->vlib_rp->virtual_base - MMAP_PAGESIZE;
327 uword virtual_size = am->vlib_rp->virtual_size + MMAP_PAGESIZE;
330 * Kill the registration pool element before we make
331 * the index vanish forever
333 pool_put_index (am->vl_clients,
334 regp->vl_api_registration_pool_index);
336 vec_delete (am->vlib_private_rps, 1, i);
337 /* Kill it, accounting for the memfd header page */
338 if (munmap ((void *) virtual_base, virtual_size) < 0)
339 clib_unix_warning ("munmap");
340 /* Reset the queue-length-address cache */
341 vec_reset_length (vl_api_queue_cursizes);
342 private_registration = 1;
347 if (private_registration == 0)
349 pool_put_index (am->vl_clients,
350 regp->vl_api_registration_pool_index);
351 oldheap = vl_msg_push_heap ();
353 svm_queue_free (regp->vl_input_queue);
354 vec_free (regp->name);
355 /* Poison the old registration */
356 clib_memset (regp, 0xF1, sizeof (*regp));
357 clib_mem_free (regp);
358 vl_msg_pop_heap (oldheap);
360 * These messages must be freed manually, since they're set up
361 * as "bounce" messages. In the private_registration == 1 case,
362 * we kill the shared-memory segment which contains the message
365 vl_msg_api_free (mp);
370 clib_warning ("unknown client ID %d", mp->index);
375 * client answered a ping, stave off the grim reaper...
378 vl_api_memclnt_keepalive_reply_t_handler
379 (vl_api_memclnt_keepalive_reply_t * mp)
381 vl_api_registration_t *regp;
382 vlib_main_t *vm = vlib_get_main ();
384 regp = vl_api_client_index_to_registration (mp->context);
387 regp->last_heard = vlib_time_now (vm);
388 regp->unanswered_pings = 0;
391 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
395 * We can send ourselves these messages if someone uses the
396 * builtin binary api test tool...
399 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
401 vl_api_memclnt_keepalive_reply_t *rmp;
403 vl_shmem_hdr_t *shmem_hdr;
405 am = vlibapi_get_main ();
406 shmem_hdr = am->shmem_hdr;
408 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
409 clib_memset (rmp, 0, sizeof (*rmp));
410 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
411 rmp->context = mp->context;
412 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
416 * To avoid filling the API trace buffer with boring messages,
417 * don't trace memclnt_keepalive[_reply] msgs
420 #define foreach_vlib_api_msg \
421 _(MEMCLNT_CREATE, memclnt_create, 1) \
422 _(MEMCLNT_DELETE, memclnt_delete, 1) \
423 _(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \
424 _(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
430 vl_mem_api_init (const char *region_name)
433 api_main_t *am = vlibapi_get_main ();
434 vl_msg_api_msg_config_t cfg;
435 vl_msg_api_msg_config_t *c = &cfg;
437 vlib_main_t *vm = vlib_get_main ();
439 clib_memset (c, 0, sizeof (*c));
441 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
444 #define _(N,n,t) do { \
445 c->id = VL_API_##N; \
447 c->handler = vl_api_##n##_t_handler; \
448 c->cleanup = vl_noop_handler; \
449 c->endian = vl_api_##n##_t_endian; \
450 c->print = vl_api_##n##_t_print; \
451 c->size = sizeof(vl_api_##n##_t); \
452 c->traced = t; /* trace, so these msgs print */ \
453 c->replay = 0; /* don't replay client create/delete msgs */ \
454 c->message_bounce = 0; /* don't bounce this message */ \
455 vl_msg_api_config(c);} while (0);
457 foreach_vlib_api_msg;
461 * special-case freeing of memclnt_delete messages, so we can
462 * simply munmap pairwise / private API segments...
464 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
465 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
466 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1;
468 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
471 ASSERT (shm && shm->vl_input_queue);
473 /* Make a note so we can always find the primary region easily */
474 am->vlib_primary_rp = am->vlib_rp;
480 map_api_segment_init (vlib_main_t * vm)
482 api_main_t *am = vlibapi_get_main ();
485 if ((rv = vl_mem_api_init (am->region_name)) < 0)
487 return clib_error_return (0, "vl_mem_api_init (%s) failed",
494 send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
496 vl_api_memclnt_keepalive_t *mp;
498 api_main_t *am = vlibapi_get_main ();
500 q = regp->vl_input_queue;
503 * If the queue head is moving, assume that the client is processing
504 * messages and skip the ping. This heuristic may fail if the queue
505 * is in the same position as last time, net of wrapping; in which
506 * case, the client will receive a keepalive.
508 if (regp->last_queue_head != q->head)
510 regp->last_heard = now;
511 regp->unanswered_pings = 0;
512 regp->last_queue_head = q->head;
517 * push/pop shared memory segment, so this routine
518 * will work with "normal" as well as "private segment"
522 mp = vl_mem_api_alloc_as_if_client_w_reg (regp, sizeof (*mp));
523 clib_memset (mp, 0, sizeof (*mp));
524 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
525 mp->context = mp->client_index =
526 vl_msg_api_handle_from_index_and_epoch
527 (regp->vl_api_registration_pool_index,
528 am->shmem_hdr->application_restarts);
530 regp->unanswered_pings++;
532 /* Failure-to-send due to a stuffed queue is absolutely expected */
533 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
534 vl_msg_api_free_w_region (regp->vlib_rp, mp);
538 vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
539 vl_api_registration_t ** regpp,
541 u32 ** confused_indices)
543 vl_api_registration_t *regp = *regpp;
546 /* If we haven't heard from this client recently... */
547 if (regp->last_heard < (now - 10.0))
549 if (regp->unanswered_pings == 2)
552 q = regp->vl_input_queue;
553 if (kill (q->consumer_pid, 0) >= 0)
555 clib_warning ("REAPER: lazy binary API client '%s'",
557 regp->unanswered_pings = 0;
558 regp->last_heard = now;
562 clib_warning ("REAPER: binary API client '%s' died",
564 vec_add1 (*dead_indices, regpp - am->vl_clients);
568 send_memclnt_keepalive (regp, now);
571 regp->unanswered_pings = 0;
575 clib_warning ("NULL client registration index %d",
576 regpp - am->vl_clients);
577 vec_add1 (*confused_indices, regpp - am->vl_clients);
582 vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
584 vl_api_registration_t **regpp;
585 static u32 *dead_indices;
586 static u32 *confused_indices;
588 vec_reset_length (dead_indices);
589 vec_reset_length (confused_indices);
592 pool_foreach (regpp, am->vl_clients) {
593 vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
598 /* This should "never happen," but if it does, fix it... */
599 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
602 for (i = 0; i < vec_len (confused_indices); i++)
604 pool_put_index (am->vl_clients, confused_indices[i]);
608 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
613 /* Allow the application to clean up its registrations */
614 for (i = 0; i < vec_len (dead_indices); i++)
616 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
621 handle = vl_msg_api_handle_from_index_and_epoch
622 (dead_indices[i], shm->application_restarts);
623 vl_api_call_reaper_functions (handle);
627 oldheap = vl_msg_push_heap ();
629 for (i = 0; i < vec_len (dead_indices); i++)
631 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
634 /* Is this a pairwise SVM segment? */
635 if ((*regpp)->vlib_rp != am->vlib_rp)
638 svm_region_t *dead_rp = (*regpp)->vlib_rp;
639 /* Note: account for the memfd header page */
640 uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
641 uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
643 /* For horizontal scaling, add a hash table... */
644 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
645 if (am->vlib_private_rps[i] == dead_rp)
647 vec_delete (am->vlib_private_rps, 1, i);
650 svm_pop_heap (oldheap);
651 clib_warning ("private rp %llx AWOL", dead_rp);
652 oldheap = svm_push_data_heap (am->vlib_rp);
655 /* Kill it, accounting for the memfd header page */
656 svm_pop_heap (oldheap);
657 if (munmap ((void *) virtual_base, virtual_size) < 0)
658 clib_unix_warning ("munmap");
659 /* Reset the queue-length-address cache */
660 vec_reset_length (vl_api_queue_cursizes);
661 oldheap = svm_push_data_heap (am->vlib_rp);
665 /* Poison the old registration */
666 clib_memset (*regpp, 0xF3, sizeof (**regpp));
667 clib_mem_free (*regpp);
669 /* no dangling references, please */
674 svm_pop_heap (oldheap);
675 clib_warning ("Duplicate free, client index %d",
676 regpp - am->vl_clients);
677 oldheap = svm_push_data_heap (am->vlib_rp);
681 svm_client_scan_this_region_nolock (am->vlib_rp);
683 vl_msg_pop_heap (oldheap);
684 for (i = 0; i < vec_len (dead_indices); i++)
685 pool_put_index (am->vl_clients, dead_indices[i]);
690 void_mem_api_handle_msg_i (api_main_t * am, svm_region_t * vlib_rp,
691 vlib_main_t * vm, vlib_node_runtime_t * node,
697 q = ((vl_shmem_hdr_t *) (void *) vlib_rp->user_ctx)->vl_input_queue;
699 if (!svm_queue_sub2 (q, (u8 *) & mp))
701 VL_MSG_API_UNPOISON ((void *) mp);
702 vl_msg_api_handler_with_vm_node (am, vlib_rp, (void *) mp, vm, node,
710 vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
712 api_main_t *am = vlibapi_get_main ();
713 return void_mem_api_handle_msg_i (am, am->vlib_rp, vm, node,
714 0 /* is_private */ );
718 vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
720 api_main_t *am = vlibapi_get_main ();
725 * Swap pending and processing vectors, then process the RPCs
726 * Avoid deadlock conditions by construction.
728 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
729 tmp = vm->processing_rpc_requests;
730 vec_reset_length (tmp);
731 vm->processing_rpc_requests = vm->pending_rpc_requests;
732 vm->pending_rpc_requests = tmp;
733 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
736 * RPCs are used to reflect function calls to thread 0
737 * when the underlying code is not thread-safe.
739 * Grabbing the thread barrier across a set of RPCs
740 * greatly increases efficiency, and avoids
741 * running afoul of the barrier sync holddown timer.
742 * The barrier sync code supports recursive locking.
744 * We really need to rewrite RPC-based code...
746 if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
748 vl_msg_api_barrier_sync ();
749 for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
751 mp = vm->processing_rpc_requests[i];
752 vl_msg_api_handler_with_vm_node (am, am->vlib_rp, (void *) mp, vm,
753 node, 0 /* is_private */ );
755 vl_msg_api_barrier_release ();
762 vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
765 api_main_t *am = vlibapi_get_main ();
766 return void_mem_api_handle_msg_i (am, am->vlib_private_rps[reg_index], vm,
767 node, 1 /* is_private */ );
770 vl_api_registration_t *
771 vl_mem_api_client_index_to_registration (u32 handle)
773 vl_api_registration_t **regpp;
774 vl_api_registration_t *regp;
775 api_main_t *am = vlibapi_get_main ();
776 vl_shmem_hdr_t *shmem_hdr;
779 index = vl_msg_api_handle_get_index (handle);
780 regpp = am->vl_clients + index;
782 if (pool_is_free (am->vl_clients, regpp))
784 vl_msg_api_increment_missing_client_counter ();
789 shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
790 if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
792 vl_msg_api_increment_missing_client_counter ();
800 vl_api_client_index_to_input_queue (u32 index)
802 vl_api_registration_t *regp;
803 api_main_t *am = vlibapi_get_main ();
805 /* Special case: vlib trying to send itself a message */
806 if (index == (u32) ~ 0)
807 return (am->shmem_hdr->vl_input_queue);
809 regp = vl_mem_api_client_index_to_registration (index);
812 return (regp->vl_input_queue);
815 static clib_error_t *
816 setup_memclnt_exit (vlib_main_t * vm)
818 atexit (vl_unmap_shmem);
822 VLIB_INIT_FUNCTION (setup_memclnt_exit);
825 format_api_message_rings (u8 * s, va_list * args)
827 api_main_t *am = va_arg (*args, api_main_t *);
828 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
829 int main_segment = va_arg (*args, int);
834 return format (s, "%8s %8s %8s %8s %8s\n",
835 "Owner", "Size", "Nitems", "Hits", "Misses");
837 ap = shmem_hdr->vl_rings;
839 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
841 s = format (s, "%8s %8d %8d %8d %8d\n",
842 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
846 ap = shmem_hdr->client_rings;
848 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
850 s = format (s, "%8s %8d %8d %8d %8d\n",
851 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
857 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
860 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
861 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
862 shmem_hdr->garbage_collects);
867 static clib_error_t *
868 vl_api_ring_command (vlib_main_t * vm,
869 unformat_input_t * input, vlib_cli_command_t * cli_cmd)
872 vl_shmem_hdr_t *shmem_hdr;
873 api_main_t *am = vlibapi_get_main ();
875 /* First, dump the primary region rings.. */
877 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
879 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
883 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
885 vlib_cli_output (vm, "Main API segment rings:");
887 vlib_cli_output (vm, "%U", format_api_message_rings, am,
888 0 /* print header */ , 0 /* notused */ );
890 vlib_cli_output (vm, "%U", format_api_message_rings, am,
891 shmem_hdr, 1 /* main segment */ );
893 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
895 svm_region_t *vlib_rp = am->vlib_private_rps[i];
896 shmem_hdr = (void *) vlib_rp->user_ctx;
897 vl_api_registration_t **regpp;
898 vl_api_registration_t *regp = 0;
900 /* For horizontal scaling, add a hash table... */
902 pool_foreach (regpp, am->vl_clients)
905 if (regp && regp->vlib_rp == vlib_rp)
907 vlib_cli_output (vm, "%s segment rings:", regp->name);
911 vlib_cli_output (vm, "regp %llx not found?", regp);
915 vlib_cli_output (vm, "%U", format_api_message_rings, am,
916 0 /* print header */ , 0 /* notused */ );
917 vlib_cli_output (vm, "%U", format_api_message_rings, am,
918 shmem_hdr, 0 /* main segment */ );
925 * Display binary api message allocation ring statistics
928 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
930 .path = "show api ring-stats",
931 .short_help = "Message ring statistics",
932 .function = vl_api_ring_command,
937 vlibmemory_init (vlib_main_t * vm)
939 api_main_t *am = vlibapi_get_main ();
940 svm_map_region_args_t _a, *a = &_a;
941 u8 *remove_path1, *remove_path2;
942 void vlibsocket_reference (void);
944 vlibsocket_reference ();
947 * By popular request / to avoid support fires, remove any old api segment
950 if (am->root_path == 0)
952 remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
953 remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
957 remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
958 remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
961 (void) unlink ((char *) remove_path1);
962 (void) unlink ((char *) remove_path2);
964 vec_free (remove_path1);
965 vec_free (remove_path2);
967 clib_memset (a, 0, sizeof (*a));
968 a->root_path = am->root_path;
969 a->name = SVM_GLOBAL_REGION_NAME;
970 a->baseva = (am->global_baseva != 0) ?
971 am->global_baseva : +svm_get_global_region_base_va ();
972 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
973 a->flags = SVM_FLAGS_NODATA;
974 a->uid = am->api_uid;
975 a->gid = am->api_gid;
977 (am->global_pvt_heap_size !=
978 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
980 svm_region_init_args (a);
986 vl_set_memory_region_name (const char *name)
988 api_main_t *am = vlibapi_get_main ();
989 am->region_name = name;
993 * fd.io coding-style-patch-verification: ON
996 * eval: (c-set-style "gnu")