memclnt_queue_callback (vlib_main_t * vm)
{
int i;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
1 + vec_len (am->vlib_private_rps)))
break;
}
}
+ if (vec_len (vm->pending_rpc_requests))
+ {
+ vm->queue_signal_pending = 1;
+ vm->api_queue_nonempty = 1;
+ vlib_process_signal_event (vm, vl_api_clnt_node.index,
+ /* event_type */ QUEUE_SIGNAL_EVENT,
+ /* event_data */ 0);
+ }
}
/*
{
vl_api_registration_t **regpp;
vl_api_registration_t *regp;
- svm_region_t *svm;
void *oldheap;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
ASSERT (vlib_get_thread_index () == 0);
pool_get (am->vl_clients, regpp);
- svm = am->vlib_rp;
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
+ oldheap = vl_msg_push_heap ();
*regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
regp = *regpp;
clib_memset (regp, 0, sizeof (*regp));
regp->registration_type = REGISTRATION_TYPE_SHMEM;
regp->vl_api_registration_pool_index = regpp - am->vl_clients;
- regp->vlib_rp = svm;
+ regp->vlib_rp = am->vlib_rp;
regp->shmem_hdr = am->shmem_hdr;
regp->vl_input_queue = q;
regp->name = format (0, "%s%c", name, 0);
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
+ vl_msg_pop_heap (oldheap);
return vl_msg_api_handle_from_index_and_epoch
(regp->vl_api_registration_pool_index,
am->shmem_hdr->application_restarts);
vl_api_registration_t **regpp;
vl_api_registration_t *regp;
vl_api_memclnt_create_reply_t *rp;
- svm_region_t *svm;
svm_queue_t *q;
int rv = 0;
void *oldheap;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
u8 *msg_table;
/*
pool_get (am->vl_clients, regpp);
- svm = am->vlib_rp;
-
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
+ oldheap = vl_msg_push_heap ();
*regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
regp = *regpp;
clib_memset (regp, 0, sizeof (*regp));
regp->registration_type = REGISTRATION_TYPE_SHMEM;
regp->vl_api_registration_pool_index = regpp - am->vl_clients;
- regp->vlib_rp = svm;
+ regp->vlib_rp = am->vlib_rp;
regp->shmem_hdr = am->shmem_hdr;
regp->clib_file_index = am->shmem_hdr->clib_file_index;
q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
+ VL_MSG_API_SVM_QUEUE_UNPOISON (q);
regp->name = format (0, "%s", mp->name);
vec_add1 (regp->name, 0);
else
msg_table = am->serialized_message_table_in_shmem;
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
+ vl_msg_pop_heap (oldheap);
rp = vl_msg_api_alloc (sizeof (*rp));
rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
clib_error_t *error = 0;
_vl_msg_api_function_list_elt_t *i;
- i = api_main.reaper_function_registrations;
+ i = vlibapi_get_main ()->reaper_function_registrations;
while (i)
{
error = i->f (client_index);
vl_api_registration_t **regpp;
vl_api_registration_t *regp;
vl_api_memclnt_delete_reply_t *rp;
- svm_region_t *svm;
void *oldheap;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
u32 handle, client_index, epoch;
handle = mp->index;
{
int i;
regp = *regpp;
- svm = am->vlib_rp;
int private_registration = 0;
- /*
- * Note: the API message handling path will set am->vlib_rp
- * as appropriate for pairwise / private memory segments
- */
- rp = vl_msg_api_alloc (sizeof (*rp));
- rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
- rp->handle = mp->handle;
- rp->response = 1;
-
- vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
-
- if (client_index != regp->vl_api_registration_pool_index)
+ /* Send reply unless client asked us to do the cleanup */
+ if (!mp->do_cleanup)
{
- clib_warning ("mismatch client_index %d pool_index %d",
- client_index, regp->vl_api_registration_pool_index);
- vl_msg_api_free (rp);
- return;
+ /*
+ * Note: the API message handling path will set am->vlib_rp
+ * as appropriate for pairwise / private memory segments
+ */
+ rp = vl_msg_api_alloc (sizeof (*rp));
+ rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
+ rp->handle = mp->handle;
+ rp->response = 1;
+
+ vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
+ if (client_index != regp->vl_api_registration_pool_index)
+ {
+ clib_warning ("mismatch client_index %d pool_index %d",
+ client_index,
+ regp->vl_api_registration_pool_index);
+ vl_msg_api_free (rp);
+ return;
+ }
}
+ /* No dangling references, please */
+ *regpp = 0;
+
/* For horizontal scaling, add a hash table... */
for (i = 0; i < vec_len (am->vlib_private_rps); i++)
{
/* Is this a pairwise / private API segment? */
- if (am->vlib_private_rps[i] == svm)
+ if (am->vlib_private_rps[i] == am->vlib_rp)
{
/* Note: account for the memfd header page */
- u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
- u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
+ uword virtual_base = am->vlib_rp->virtual_base - MMAP_PAGESIZE;
+ uword virtual_size = am->vlib_rp->virtual_size + MMAP_PAGESIZE;
/*
* Kill the registration pool element before we make
}
}
- /* No dangling references, please */
- *regpp = 0;
-
if (private_registration == 0)
{
pool_put_index (am->vl_clients,
regp->vl_api_registration_pool_index);
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
+ oldheap = vl_msg_push_heap ();
+ if (mp->do_cleanup)
+ svm_queue_free (regp->vl_input_queue);
vec_free (regp->name);
/* Poison the old registration */
clib_memset (regp, 0xF1, sizeof (*regp));
clib_mem_free (regp);
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
+ vl_msg_pop_heap (oldheap);
/*
* These messages must be freed manually, since they're set up
* as "bounce" messages. In the private_registration == 1 case,
api_main_t *am;
vl_shmem_hdr_t *shmem_hdr;
- am = &api_main;
+ am = vlibapi_get_main ();
shmem_hdr = am->shmem_hdr;
rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
}
+/*
+ * To avoid filling the API trace buffer with boring messages,
+ * don't trace memclnt_keepalive[_reply] msgs
+ */
+
#define foreach_vlib_api_msg \
-_(MEMCLNT_CREATE, memclnt_create) \
-_(MEMCLNT_DELETE, memclnt_delete) \
-_(MEMCLNT_KEEPALIVE, memclnt_keepalive) \
-_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply) \
+_(MEMCLNT_CREATE, memclnt_create, 1) \
+_(MEMCLNT_DELETE, memclnt_delete, 1) \
+_(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \
+_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
/*
* memory_api_init
vl_mem_api_init (const char *region_name)
{
int rv;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
vl_msg_api_msg_config_t cfg;
vl_msg_api_msg_config_t *c = &cfg;
vl_shmem_hdr_t *shm;
if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
return rv;
-#define _(N,n) do { \
+#define _(N,n,t) do { \
c->id = VL_API_##N; \
c->name = #n; \
c->handler = vl_api_##n##_t_handler; \
c->endian = vl_api_##n##_t_endian; \
c->print = vl_api_##n##_t_print; \
c->size = sizeof(vl_api_##n##_t); \
- c->traced = 1; /* trace, so these msgs print */ \
+ c->traced = t; /* trace, so these msgs print */ \
c->replay = 0; /* don't replay client create/delete msgs */ \
c->message_bounce = 0; /* don't bounce this message */ \
vl_msg_api_config(c);} while (0);
*/
am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
+ am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1;
vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
clib_error_t *
map_api_segment_init (vlib_main_t * vm)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
int rv;
if ((rv = vl_mem_api_init (am->region_name)) < 0)
{
vl_api_memclnt_keepalive_t *mp;
svm_queue_t *q;
- api_main_t *am = &api_main;
- svm_region_t *save_vlib_rp = am->vlib_rp;
- vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
+ api_main_t *am = vlibapi_get_main ();
q = regp->vl_input_queue;
* memory clients..
*/
- am->vlib_rp = regp->vlib_rp;
- am->shmem_hdr = regp->shmem_hdr;
-
- mp = vl_msg_api_alloc (sizeof (*mp));
+ mp = vl_mem_api_alloc_as_if_client_w_reg (regp, sizeof (*mp));
clib_memset (mp, 0, sizeof (*mp));
mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
mp->context = mp->client_index =
/* Failure-to-send due to a stuffed queue is absolutely expected */
if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
- vl_msg_api_free (mp);
-
- am->vlib_rp = save_vlib_rp;
- am->shmem_hdr = save_shmem_hdr;
+ vl_msg_api_free_w_region (regp->vlib_rp, mp);
}
static void
if (PREDICT_FALSE (vec_len (dead_indices) > 0))
{
int i;
- svm_region_t *svm;
void *oldheap;
/* Allow the application to clean up its registrations */
}
}
- svm = am->vlib_rp;
- pthread_mutex_lock (&svm->mutex);
- oldheap = svm_push_data_heap (svm);
+ oldheap = vl_msg_push_heap ();
for (i = 0; i < vec_len (dead_indices); i++)
{
if (regpp)
{
/* Is this a pairwise SVM segment? */
- if ((*regpp)->vlib_rp != svm)
+ if ((*regpp)->vlib_rp != am->vlib_rp)
{
int i;
svm_region_t *dead_rp = (*regpp)->vlib_rp;
/* Note: account for the memfd header page */
- u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
- u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
+ uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
+ uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
/* For horizontal scaling, add a hash table... */
for (i = 0; i < vec_len (am->vlib_private_rps); i++)
vec_delete (am->vlib_private_rps, 1, i);
goto found;
}
+ svm_pop_heap (oldheap);
clib_warning ("private rp %llx AWOL", dead_rp);
+ oldheap = svm_push_data_heap (am->vlib_rp);
found:
/* Kill it, accounting for the memfd header page */
+ svm_pop_heap (oldheap);
if (munmap ((void *) virtual_base, virtual_size) < 0)
clib_unix_warning ("munmap");
/* Reset the queue-length-address cache */
vec_reset_length (vl_api_queue_cursizes);
+ oldheap = svm_push_data_heap (am->vlib_rp);
}
else
{
svm_pop_heap (oldheap);
clib_warning ("Duplicate free, client index %d",
regpp - am->vl_clients);
- oldheap = svm_push_data_heap (svm);
+ oldheap = svm_push_data_heap (am->vlib_rp);
}
}
svm_client_scan_this_region_nolock (am->vlib_rp);
- pthread_mutex_unlock (&svm->mutex);
- svm_pop_heap (oldheap);
+ vl_msg_pop_heap (oldheap);
for (i = 0; i < vec_len (dead_indices); i++)
pool_put_index (am->vl_clients, dead_indices[i]);
}
}
static inline int
-void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
- vlib_node_runtime_t * node, svm_queue_t * q)
+void_mem_api_handle_msg_i (api_main_t * am, svm_region_t * vlib_rp,
+ vlib_main_t * vm, vlib_node_runtime_t * node,
+ u8 is_private)
{
+ svm_queue_t *q;
uword mp;
+
+ q = ((vl_shmem_hdr_t *) (void *) vlib_rp->user_ctx)->vl_input_queue;
+
if (!svm_queue_sub2 (q, (u8 *) & mp))
{
- vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
+ VL_MSG_API_UNPOISON ((void *) mp);
+ vl_msg_api_handler_with_vm_node (am, vlib_rp, (void *) mp, vm, node,
+ is_private);
return 0;
}
return -1;
int
vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
{
- api_main_t *am = &api_main;
- return void_mem_api_handle_msg_i (am, vm, node,
- am->shmem_hdr->vl_input_queue);
+ api_main_t *am = vlibapi_get_main ();
+ return void_mem_api_handle_msg_i (am, am->vlib_rp, vm, node,
+ 0 /* is_private */ );
}
int
vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
int i;
- uword *rpc_requests, mp;
+ uword *tmp, mp;
+ /*
+ * Swap pending and processing vectors, then process the RPCs
+ * Avoid deadlock conditions by construction.
+ */
clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
- rpc_requests = vm->pending_rpc_requests;
+ tmp = vm->processing_rpc_requests;
+ vec_reset_length (tmp);
+ vm->processing_rpc_requests = vm->pending_rpc_requests;
+ vm->pending_rpc_requests = tmp;
+ clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
- for (i = 0; i < vec_len (rpc_requests); i++)
+ /*
+ * RPCs are used to reflect function calls to thread 0
+ * when the underlying code is not thread-safe.
+ *
+ * Grabbing the thread barrier across a set of RPCs
+ * greatly increases efficiency, and avoids
+ * running afoul of the barrier sync holddown timer.
+ * The barrier sync code supports recursive locking.
+ *
+ * We really need to rewrite RPC-based code...
+ */
+ if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
{
- mp = rpc_requests[i];
- vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
+ vl_msg_api_barrier_sync ();
+ for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
+ {
+ mp = vm->processing_rpc_requests[i];
+ vl_msg_api_handler_with_vm_node (am, am->vlib_rp, (void *) mp, vm,
+ node, 0 /* is_private */ );
+ }
+ vl_msg_api_barrier_release ();
}
- vec_reset_length (vm->pending_rpc_requests);
- clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
+
return 0;
}
vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
u32 reg_index)
{
- api_main_t *am = &api_main;
- vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
- svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
- svm_queue_t *q;
- int rv;
-
- vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
-
- am->shmem_hdr = (void *) vlib_rp->user_ctx;
- q = am->shmem_hdr->vl_input_queue;
-
- rv = void_mem_api_handle_msg_i (am, vm, node, q);
-
- am->shmem_hdr = save_shmem_hdr;
- am->vlib_rp = save_vlib_rp;
-
- return rv;
+ api_main_t *am = vlibapi_get_main ();
+ return void_mem_api_handle_msg_i (am, am->vlib_private_rps[reg_index], vm,
+ node, 1 /* is_private */ );
}
vl_api_registration_t *
{
vl_api_registration_t **regpp;
vl_api_registration_t *regp;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
vl_shmem_hdr_t *shmem_hdr;
u32 index;
vl_api_client_index_to_input_queue (u32 index)
{
vl_api_registration_t *regp;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
/* Special case: vlib trying to send itself a message */
if (index == (u32) ~ 0)
{
int i;
vl_shmem_hdr_t *shmem_hdr;
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
/* First, dump the primary region rings.. */
clib_error_t *
vlibmemory_init (vlib_main_t * vm)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
svm_map_region_args_t _a, *a = &_a;
- clib_error_t *error;
+ u8 *remove_path1, *remove_path2;
+ void vlibsocket_reference (void);
+
+ vlibsocket_reference ();
+
+ /*
+ * By popular request / to avoid support fires, remove any old api segment
+ * files Right Here.
+ */
+ if (am->root_path == 0)
+ {
+ remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
+ remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
+ }
+ else
+ {
+ remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
+ remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
+ }
+
+ (void) unlink ((char *) remove_path1);
+ (void) unlink ((char *) remove_path2);
+
+ vec_free (remove_path1);
+ vec_free (remove_path2);
clib_memset (a, 0, sizeof (*a));
a->root_path = am->root_path;
svm_region_init_args (a);
- error = vlib_call_init_function (vm, vlibsocket_init);
-
- return error;
+ return 0;
}
void
vl_set_memory_region_name (const char *name)
{
- api_main_t *am = &api_main;
+ api_main_t *am = vlibapi_get_main ();
am->region_name = name;
}