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 ();
69 if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
70 1 + vec_len (am->vlib_private_rps)))
72 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
78 q = shmem_hdr->vl_input_queue;
82 vec_add1 (vl_api_queue_cursizes, &q->cursize);
84 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
86 svm_region_t *vlib_rp = am->vlib_private_rps[i];
88 shmem_hdr = (void *) vlib_rp->user_ctx;
89 q = shmem_hdr->vl_input_queue;
90 vec_add1 (vl_api_queue_cursizes, &q->cursize);
94 for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
96 if (*vl_api_queue_cursizes[i])
98 vm->queue_signal_pending = 1;
99 vm->api_queue_nonempty = 1;
100 vlib_process_signal_event (vm, vl_api_clnt_node.index,
101 /* event_type */ QUEUE_SIGNAL_EVENT,
106 if (vec_len (vm->pending_rpc_requests))
108 vm->queue_signal_pending = 1;
109 vm->api_queue_nonempty = 1;
110 vlib_process_signal_event (vm, vl_api_clnt_node.index,
111 /* event_type */ QUEUE_SIGNAL_EVENT,
117 * vl_api_memclnt_create_internal
120 vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
122 vl_api_registration_t **regpp;
123 vl_api_registration_t *regp;
125 api_main_t *am = vlibapi_get_main ();
127 ASSERT (vlib_get_thread_index () == 0);
128 pool_get (am->vl_clients, regpp);
131 oldheap = vl_msg_push_heap ();
132 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
135 clib_memset (regp, 0, sizeof (*regp));
136 regp->registration_type = REGISTRATION_TYPE_SHMEM;
137 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
138 regp->vlib_rp = am->vlib_rp;
139 regp->shmem_hdr = am->shmem_hdr;
141 regp->vl_input_queue = q;
142 regp->name = format (0, "%s%c", name, 0);
144 vl_msg_pop_heap (oldheap);
145 return vl_msg_api_handle_from_index_and_epoch
146 (regp->vl_api_registration_pool_index,
147 am->shmem_hdr->application_restarts);
151 * vl_api_memclnt_create_t_handler
154 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
156 vl_api_registration_t **regpp;
157 vl_api_registration_t *regp;
158 vl_api_memclnt_create_reply_t *rp;
162 api_main_t *am = vlibapi_get_main ();
166 * This is tortured. Maintain a vlib-address-space private
167 * pool of client registrations. We use the shared-memory virtual
168 * address of client structure as a handle, to allow direct
169 * manipulation of context quota vbls from the client library.
171 * This scheme causes trouble w/ API message trace replay, since
172 * some random VA from clib_mem_alloc() certainly won't
173 * occur in the Linux sim. The (very) few places
174 * that care need to use the pool index.
176 * Putting the registration object(s) into a pool in shared memory and
177 * using the pool index as a handle seems like a great idea.
178 * Unfortunately, each and every reference to that pool would need
179 * to be protected by a mutex:
183 * convert pool index to
191 pool_get (am->vl_clients, regpp);
193 oldheap = vl_msg_push_heap ();
194 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
197 clib_memset (regp, 0, sizeof (*regp));
198 regp->registration_type = REGISTRATION_TYPE_SHMEM;
199 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
200 regp->vlib_rp = am->vlib_rp;
201 regp->shmem_hdr = am->shmem_hdr;
202 regp->clib_file_index = am->shmem_hdr->clib_file_index;
204 q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
205 VL_MSG_API_SVM_QUEUE_UNPOISON (q);
207 regp->name = format (0, "%s", mp->name);
208 vec_add1 (regp->name, 0);
210 if (am->serialized_message_table_in_shmem == 0)
211 am->serialized_message_table_in_shmem =
212 vl_api_serialize_message_table (am, 0);
214 if (am->vlib_rp != am->vlib_primary_rp)
215 msg_table = vl_api_serialize_message_table (am, 0);
217 msg_table = am->serialized_message_table_in_shmem;
219 vl_msg_pop_heap (oldheap);
221 rp = vl_msg_api_alloc (sizeof (*rp));
222 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
223 rp->handle = (uword) regp;
224 rp->index = vl_msg_api_handle_from_index_and_epoch
225 (regp->vl_api_registration_pool_index,
226 am->shmem_hdr->application_restarts);
227 rp->context = mp->context;
228 rp->response = ntohl (rv);
229 rp->message_table = pointer_to_uword (msg_table);
231 vl_msg_api_send_shmem (q, (u8 *) & rp);
235 vl_api_call_reaper_functions (u32 client_index)
237 clib_error_t *error = 0;
238 _vl_msg_api_function_list_elt_t *i;
240 i = vlibapi_get_main ()->reaper_function_registrations;
243 error = i->f (client_index);
245 clib_error_report (error);
246 i = i->next_init_function;
251 * vl_api_memclnt_delete_t_handler
254 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
256 vl_api_registration_t **regpp;
257 vl_api_registration_t *regp;
258 vl_api_memclnt_delete_reply_t *rp;
260 api_main_t *am = vlibapi_get_main ();
261 u32 handle, client_index, epoch;
265 vl_api_call_reaper_functions (handle);
267 epoch = vl_msg_api_handle_get_epoch (handle);
268 client_index = vl_msg_api_handle_get_index (handle);
270 if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
273 ("Stale clnt delete index %d old epoch %d cur epoch %d",
275 (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
279 regpp = pool_elt_at_index (am->vl_clients, client_index);
281 if (!pool_is_free (am->vl_clients, regpp))
285 int private_registration = 0;
287 /* Send reply unless client asked us to do the cleanup */
291 * Note: the API message handling path will set am->vlib_rp
292 * as appropriate for pairwise / private memory segments
294 rp = vl_msg_api_alloc (sizeof (*rp));
295 rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
296 rp->handle = mp->handle;
299 vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
300 if (client_index != regp->vl_api_registration_pool_index)
302 clib_warning ("mismatch client_index %d pool_index %d",
304 regp->vl_api_registration_pool_index);
305 vl_msg_api_free (rp);
310 /* No dangling references, please */
313 /* For horizontal scaling, add a hash table... */
314 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
316 /* Is this a pairwise / private API segment? */
317 if (am->vlib_private_rps[i] == am->vlib_rp)
319 /* Note: account for the memfd header page */
320 uword virtual_base = am->vlib_rp->virtual_base - MMAP_PAGESIZE;
321 uword virtual_size = am->vlib_rp->virtual_size + MMAP_PAGESIZE;
324 * Kill the registration pool element before we make
325 * the index vanish forever
327 pool_put_index (am->vl_clients,
328 regp->vl_api_registration_pool_index);
330 vec_delete (am->vlib_private_rps, 1, i);
331 /* Kill it, accounting for the memfd header page */
332 if (munmap ((void *) virtual_base, virtual_size) < 0)
333 clib_unix_warning ("munmap");
334 /* Reset the queue-length-address cache */
335 vec_reset_length (vl_api_queue_cursizes);
336 private_registration = 1;
341 if (private_registration == 0)
343 pool_put_index (am->vl_clients,
344 regp->vl_api_registration_pool_index);
345 oldheap = vl_msg_push_heap ();
347 svm_queue_free (regp->vl_input_queue);
348 vec_free (regp->name);
349 /* Poison the old registration */
350 clib_memset (regp, 0xF1, sizeof (*regp));
351 clib_mem_free (regp);
352 vl_msg_pop_heap (oldheap);
354 * These messages must be freed manually, since they're set up
355 * as "bounce" messages. In the private_registration == 1 case,
356 * we kill the shared-memory segment which contains the message
359 vl_msg_api_free (mp);
364 clib_warning ("unknown client ID %d", mp->index);
369 * client answered a ping, stave off the grim reaper...
372 vl_api_memclnt_keepalive_reply_t_handler
373 (vl_api_memclnt_keepalive_reply_t * mp)
375 vl_api_registration_t *regp;
376 vlib_main_t *vm = vlib_get_main ();
378 regp = vl_api_client_index_to_registration (mp->context);
381 regp->last_heard = vlib_time_now (vm);
382 regp->unanswered_pings = 0;
385 clib_warning ("BUG: anonymous memclnt_keepalive_reply");
389 * We can send ourselves these messages if someone uses the
390 * builtin binary api test tool...
393 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
395 vl_api_memclnt_keepalive_reply_t *rmp;
397 vl_shmem_hdr_t *shmem_hdr;
399 am = vlibapi_get_main ();
400 shmem_hdr = am->shmem_hdr;
402 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
403 clib_memset (rmp, 0, sizeof (*rmp));
404 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
405 rmp->context = mp->context;
406 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
410 * To avoid filling the API trace buffer with boring messages,
411 * don't trace memclnt_keepalive[_reply] msgs
414 #define foreach_vlib_api_msg \
415 _(MEMCLNT_CREATE, memclnt_create, 1) \
416 _(MEMCLNT_DELETE, memclnt_delete, 1) \
417 _(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \
418 _(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
424 vl_mem_api_init (const char *region_name)
427 api_main_t *am = vlibapi_get_main ();
428 vl_msg_api_msg_config_t cfg;
429 vl_msg_api_msg_config_t *c = &cfg;
431 vlib_main_t *vm = vlib_get_main ();
433 clib_memset (c, 0, sizeof (*c));
435 if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
438 #define _(N,n,t) do { \
439 c->id = VL_API_##N; \
441 c->handler = vl_api_##n##_t_handler; \
442 c->cleanup = vl_noop_handler; \
443 c->endian = vl_api_##n##_t_endian; \
444 c->print = vl_api_##n##_t_print; \
445 c->size = sizeof(vl_api_##n##_t); \
446 c->traced = t; /* trace, so these msgs print */ \
447 c->replay = 0; /* don't replay client create/delete msgs */ \
448 c->message_bounce = 0; /* don't bounce this message */ \
449 vl_msg_api_config(c);} while (0);
451 foreach_vlib_api_msg;
455 * special-case freeing of memclnt_delete messages, so we can
456 * simply munmap pairwise / private API segments...
458 am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
459 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
460 am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1;
462 vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
465 ASSERT (shm && shm->vl_input_queue);
467 /* Make a note so we can always find the primary region easily */
468 am->vlib_primary_rp = am->vlib_rp;
474 map_api_segment_init (vlib_main_t * vm)
476 api_main_t *am = vlibapi_get_main ();
479 if ((rv = vl_mem_api_init (am->region_name)) < 0)
481 return clib_error_return (0, "vl_mem_api_init (%s) failed",
488 send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
490 vl_api_memclnt_keepalive_t *mp;
492 api_main_t *am = vlibapi_get_main ();
494 q = regp->vl_input_queue;
497 * If the queue head is moving, assume that the client is processing
498 * messages and skip the ping. This heuristic may fail if the queue
499 * is in the same position as last time, net of wrapping; in which
500 * case, the client will receive a keepalive.
502 if (regp->last_queue_head != q->head)
504 regp->last_heard = now;
505 regp->unanswered_pings = 0;
506 regp->last_queue_head = q->head;
511 * push/pop shared memory segment, so this routine
512 * will work with "normal" as well as "private segment"
516 mp = vl_mem_api_alloc_as_if_client_w_reg (regp, sizeof (*mp));
517 clib_memset (mp, 0, sizeof (*mp));
518 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
519 mp->context = mp->client_index =
520 vl_msg_api_handle_from_index_and_epoch
521 (regp->vl_api_registration_pool_index,
522 am->shmem_hdr->application_restarts);
524 regp->unanswered_pings++;
526 /* Failure-to-send due to a stuffed queue is absolutely expected */
527 if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
528 vl_msg_api_free_w_region (regp->vlib_rp, mp);
532 vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
533 vl_api_registration_t ** regpp,
535 u32 ** confused_indices)
537 vl_api_registration_t *regp = *regpp;
540 /* If we haven't heard from this client recently... */
541 if (regp->last_heard < (now - 10.0))
543 if (regp->unanswered_pings == 2)
546 q = regp->vl_input_queue;
547 if (kill (q->consumer_pid, 0) >= 0)
549 clib_warning ("REAPER: lazy binary API client '%s'",
551 regp->unanswered_pings = 0;
552 regp->last_heard = now;
556 clib_warning ("REAPER: binary API client '%s' died",
558 vec_add1 (*dead_indices, regpp - am->vl_clients);
562 send_memclnt_keepalive (regp, now);
565 regp->unanswered_pings = 0;
569 clib_warning ("NULL client registration index %d",
570 regpp - am->vl_clients);
571 vec_add1 (*confused_indices, regpp - am->vl_clients);
576 vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
578 vl_api_registration_t **regpp;
579 static u32 *dead_indices;
580 static u32 *confused_indices;
582 vec_reset_length (dead_indices);
583 vec_reset_length (confused_indices);
586 pool_foreach (regpp, am->vl_clients, ({
587 vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
592 /* This should "never happen," but if it does, fix it... */
593 if (PREDICT_FALSE (vec_len (confused_indices) > 0))
596 for (i = 0; i < vec_len (confused_indices); i++)
598 pool_put_index (am->vl_clients, confused_indices[i]);
602 if (PREDICT_FALSE (vec_len (dead_indices) > 0))
607 /* Allow the application to clean up its registrations */
608 for (i = 0; i < vec_len (dead_indices); i++)
610 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
615 handle = vl_msg_api_handle_from_index_and_epoch
616 (dead_indices[i], shm->application_restarts);
617 vl_api_call_reaper_functions (handle);
621 oldheap = vl_msg_push_heap ();
623 for (i = 0; i < vec_len (dead_indices); i++)
625 regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
628 /* Is this a pairwise SVM segment? */
629 if ((*regpp)->vlib_rp != am->vlib_rp)
632 svm_region_t *dead_rp = (*regpp)->vlib_rp;
633 /* Note: account for the memfd header page */
634 uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
635 uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
637 /* For horizontal scaling, add a hash table... */
638 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
639 if (am->vlib_private_rps[i] == dead_rp)
641 vec_delete (am->vlib_private_rps, 1, i);
644 svm_pop_heap (oldheap);
645 clib_warning ("private rp %llx AWOL", dead_rp);
646 oldheap = svm_push_data_heap (am->vlib_rp);
649 /* Kill it, accounting for the memfd header page */
650 svm_pop_heap (oldheap);
651 if (munmap ((void *) virtual_base, virtual_size) < 0)
652 clib_unix_warning ("munmap");
653 /* Reset the queue-length-address cache */
654 vec_reset_length (vl_api_queue_cursizes);
655 oldheap = svm_push_data_heap (am->vlib_rp);
659 /* Poison the old registration */
660 clib_memset (*regpp, 0xF3, sizeof (**regpp));
661 clib_mem_free (*regpp);
663 /* no dangling references, please */
668 svm_pop_heap (oldheap);
669 clib_warning ("Duplicate free, client index %d",
670 regpp - am->vl_clients);
671 oldheap = svm_push_data_heap (am->vlib_rp);
675 svm_client_scan_this_region_nolock (am->vlib_rp);
677 vl_msg_pop_heap (oldheap);
678 for (i = 0; i < vec_len (dead_indices); i++)
679 pool_put_index (am->vl_clients, dead_indices[i]);
684 void_mem_api_handle_msg_i (api_main_t * am, svm_region_t * vlib_rp,
685 vlib_main_t * vm, vlib_node_runtime_t * node,
691 q = ((vl_shmem_hdr_t *) (void *) vlib_rp->user_ctx)->vl_input_queue;
693 if (!svm_queue_sub2 (q, (u8 *) & mp))
695 VL_MSG_API_UNPOISON ((void *) mp);
696 vl_msg_api_handler_with_vm_node (am, vlib_rp, (void *) mp, vm, node,
704 vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
706 api_main_t *am = vlibapi_get_main ();
707 return void_mem_api_handle_msg_i (am, am->vlib_rp, vm, node,
708 0 /* is_private */ );
712 vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
714 api_main_t *am = vlibapi_get_main ();
719 * Swap pending and processing vectors, then process the RPCs
720 * Avoid deadlock conditions by construction.
722 clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
723 tmp = vm->processing_rpc_requests;
724 vec_reset_length (tmp);
725 vm->processing_rpc_requests = vm->pending_rpc_requests;
726 vm->pending_rpc_requests = tmp;
727 clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
730 * RPCs are used to reflect function calls to thread 0
731 * when the underlying code is not thread-safe.
733 * Grabbing the thread barrier across a set of RPCs
734 * greatly increases efficiency, and avoids
735 * running afoul of the barrier sync holddown timer.
736 * The barrier sync code supports recursive locking.
738 * We really need to rewrite RPC-based code...
740 if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
742 vl_msg_api_barrier_sync ();
743 for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
745 mp = vm->processing_rpc_requests[i];
746 vl_msg_api_handler_with_vm_node (am, am->vlib_rp, (void *) mp, vm,
747 node, 0 /* is_private */ );
749 vl_msg_api_barrier_release ();
756 vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
759 api_main_t *am = vlibapi_get_main ();
760 return void_mem_api_handle_msg_i (am, am->vlib_private_rps[reg_index], vm,
761 node, 1 /* is_private */ );
764 vl_api_registration_t *
765 vl_mem_api_client_index_to_registration (u32 handle)
767 vl_api_registration_t **regpp;
768 vl_api_registration_t *regp;
769 api_main_t *am = vlibapi_get_main ();
770 vl_shmem_hdr_t *shmem_hdr;
773 index = vl_msg_api_handle_get_index (handle);
774 regpp = am->vl_clients + index;
776 if (pool_is_free (am->vl_clients, regpp))
778 vl_msg_api_increment_missing_client_counter ();
783 shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
784 if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
786 vl_msg_api_increment_missing_client_counter ();
794 vl_api_client_index_to_input_queue (u32 index)
796 vl_api_registration_t *regp;
797 api_main_t *am = vlibapi_get_main ();
799 /* Special case: vlib trying to send itself a message */
800 if (index == (u32) ~ 0)
801 return (am->shmem_hdr->vl_input_queue);
803 regp = vl_mem_api_client_index_to_registration (index);
806 return (regp->vl_input_queue);
809 static clib_error_t *
810 setup_memclnt_exit (vlib_main_t * vm)
812 atexit (vl_unmap_shmem);
816 VLIB_INIT_FUNCTION (setup_memclnt_exit);
819 format_api_message_rings (u8 * s, va_list * args)
821 api_main_t *am = va_arg (*args, api_main_t *);
822 vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
823 int main_segment = va_arg (*args, int);
828 return format (s, "%8s %8s %8s %8s %8s\n",
829 "Owner", "Size", "Nitems", "Hits", "Misses");
831 ap = shmem_hdr->vl_rings;
833 for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
835 s = format (s, "%8s %8d %8d %8d %8d\n",
836 "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
840 ap = shmem_hdr->client_rings;
842 for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
844 s = format (s, "%8s %8d %8d %8d %8d\n",
845 "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
851 s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
854 "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
855 shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
856 shmem_hdr->garbage_collects);
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)
866 vl_shmem_hdr_t *shmem_hdr;
867 api_main_t *am = vlibapi_get_main ();
869 /* First, dump the primary region rings.. */
871 if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
873 vlib_cli_output (vm, "Shared memory segment not initialized...\n");
877 shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
879 vlib_cli_output (vm, "Main API segment rings:");
881 vlib_cli_output (vm, "%U", format_api_message_rings, am,
882 0 /* print header */ , 0 /* notused */ );
884 vlib_cli_output (vm, "%U", format_api_message_rings, am,
885 shmem_hdr, 1 /* main segment */ );
887 for (i = 0; i < vec_len (am->vlib_private_rps); i++)
889 svm_region_t *vlib_rp = am->vlib_private_rps[i];
890 shmem_hdr = (void *) vlib_rp->user_ctx;
891 vl_api_registration_t **regpp;
892 vl_api_registration_t *regp = 0;
894 /* For horizontal scaling, add a hash table... */
896 pool_foreach (regpp, am->vl_clients,
899 if (regp && regp->vlib_rp == vlib_rp)
901 vlib_cli_output (vm, "%s segment rings:", regp->name);
905 vlib_cli_output (vm, "regp %llx not found?", regp);
909 vlib_cli_output (vm, "%U", format_api_message_rings, am,
910 0 /* print header */ , 0 /* notused */ );
911 vlib_cli_output (vm, "%U", format_api_message_rings, am,
912 shmem_hdr, 0 /* main segment */ );
919 * Display binary api message allocation ring statistics
922 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
924 .path = "show api ring-stats",
925 .short_help = "Message ring statistics",
926 .function = vl_api_ring_command,
931 vlibmemory_init (vlib_main_t * vm)
933 api_main_t *am = vlibapi_get_main ();
934 svm_map_region_args_t _a, *a = &_a;
935 u8 *remove_path1, *remove_path2;
936 void vlibsocket_reference (void);
938 vlibsocket_reference ();
941 * By popular request / to avoid support fires, remove any old api segment
944 if (am->root_path == 0)
946 remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
947 remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
951 remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
952 remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
955 (void) unlink ((char *) remove_path1);
956 (void) unlink ((char *) remove_path2);
958 vec_free (remove_path1);
959 vec_free (remove_path2);
961 clib_memset (a, 0, sizeof (*a));
962 a->root_path = am->root_path;
963 a->name = SVM_GLOBAL_REGION_NAME;
964 a->baseva = (am->global_baseva != 0) ?
965 am->global_baseva : +svm_get_global_region_base_va ();
966 a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
967 a->flags = SVM_FLAGS_NODATA;
968 a->uid = am->api_uid;
969 a->gid = am->api_gid;
971 (am->global_pvt_heap_size !=
972 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
974 svm_region_init_args (a);
980 vl_set_memory_region_name (const char *name)
982 api_main_t *am = vlibapi_get_main ();
983 am->region_name = name;
987 * fd.io coding-style-patch-verification: ON
990 * eval: (c-set-style "gnu")