X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlibmemory%2Fmemory_shared.c;h=488a302bbba3304bf84fe760831343dfd56ba899;hb=b384b543313b6b47a277c903e9d4fcd4343054fa;hp=d8d3200485e088e054fc12d7c11dcd460409fcb5;hpb=7cd468a3d7dee7d6c92f69a0bb7061ae208ec727;p=vpp.git diff --git a/src/vlibmemory/memory_shared.c b/src/vlibmemory/memory_shared.c index d8d3200485e..488a302bbba 100644 --- a/src/vlibmemory/memory_shared.c +++ b/src/vlibmemory/memory_shared.c @@ -25,33 +25,41 @@ #include #include #include + #include #include #include +#include #include #include -#include -#include - +#include #include #define vl_typedefs #include #undef vl_typedefs +#define DEBUG_MESSAGE_BUFFER_OVERRUN 0 + static inline void * vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) { int i; msgbuf_t *rv; ring_alloc_t *ap; - unix_shared_memory_queue_t *q; + svm_queue_t *q; void *oldheap; vl_shmem_hdr_t *shmem_hdr; api_main_t *am = &api_main; shmem_hdr = am->shmem_hdr; +#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 + nbytes += 4; +#endif + + ASSERT (pool == 0 || vlib_get_thread_index () == 0); + if (shmem_hdr == 0) { clib_warning ("shared memory header NULL"); @@ -95,12 +103,40 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) */ if (rv->q) { + u32 now = (u32) time (0); + + if (PREDICT_TRUE (rv->gc_mark_timestamp == 0)) + rv->gc_mark_timestamp = now; + else + { + if (now - rv->gc_mark_timestamp > 10) + { + if (CLIB_DEBUG > 0) + { + u16 *msg_idp, msg_id; + clib_warning + ("garbage collect pool %d ring %d index %d", pool, i, + q->head); + msg_idp = (u16 *) (rv->data); + msg_id = clib_net_to_host_u16 (*msg_idp); + if (msg_id < vec_len (api_main.msg_names)) + clib_warning ("msg id %d name %s", (u32) msg_id, + api_main.msg_names[msg_id]); + } + shmem_hdr->garbage_collects++; + goto collected; + } + } + + /* yes, loser; try next larger pool */ ap[i].misses++; if (pool == 0) pthread_mutex_unlock (&q->mutex); continue; } + collected: + /* OK, we have a winner */ ap[i].hits++; /* @@ -108,6 +144,7 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) * don't need to know the queue to free the item. */ rv->q = q; + rv->gc_mark_timestamp = 0; q->head++; if (q->head == q->maxsize) q->head = 0; @@ -143,7 +180,16 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) pthread_mutex_unlock (&am->vlib_rp->mutex); out: +#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 + { + nbytes -= 4; + u32 *overrun; + overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t)); + *overrun = 0x1badbabe; + } +#endif rv->data_len = htonl (nbytes - sizeof (msgbuf_t)); + return (rv->data); } @@ -184,6 +230,25 @@ vl_msg_api_alloc_as_if_client_or_null (int nbytes) return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ ); } +void * +vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes) +{ + 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; + void *msg; + + vlib_rp = am->vlib_rp = reg->vlib_rp; + am->shmem_hdr = (void *) vlib_rp->user_ctx; + + msg = vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ ); + + am->shmem_hdr = save_shmem_hdr; + am->vlib_rp = save_vlib_rp; + + return msg; +} + void vl_msg_api_free (void *a) { @@ -201,11 +266,28 @@ vl_msg_api_free (void *a) if (rv->q) { rv->q = 0; + rv->gc_mark_timestamp = 0; +#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 + { + u32 *overrun; + overrun = (u32 *) (rv->data + ntohl (rv->data_len)); + ASSERT (*overrun == 0x1badbabe); + } +#endif return; } pthread_mutex_lock (&am->vlib_rp->mutex); oldheap = svm_push_data_heap (am->vlib_rp); + +#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 + { + u32 *overrun; + overrun = (u32 *) (rv->data + ntohl (rv->data_len)); + ASSERT (*overrun == 0x1badbabe); + } +#endif + clib_mem_free (rv); svm_pop_heap (oldheap); pthread_mutex_unlock (&am->vlib_rp->mutex); @@ -236,7 +318,7 @@ vl_msg_api_free_nolock (void *a) } void -vl_set_memory_root_path (char *name) +vl_set_memory_root_path (const char *name) { api_main_t *am = &api_main; @@ -299,23 +381,170 @@ vl_set_api_pvt_heap_size (u64 size) am->api_pvt_heap_size = size; } +static void +vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr) +{ + api_main_t *am = &api_main; + u32 vlib_input_queue_length; + + /* vlib main input queue */ + vlib_input_queue_length = 1024; + if (am->vlib_input_queue_length) + vlib_input_queue_length = am->vlib_input_queue_length; + + shmem_hdr->vl_input_queue = + svm_queue_init (vlib_input_queue_length, sizeof (uword), + getpid (), am->vlib_signal); + +#define _(sz,n) \ + do { \ + ring_alloc_t _rp; \ + _rp.rp = svm_queue_init ((n), (sz), 0, 0); \ + _rp.size = (sz); \ + _rp.nitems = n; \ + _rp.hits = 0; \ + _rp.misses = 0; \ + vec_add1(shmem_hdr->vl_rings, _rp); \ + } while (0); + + foreach_vl_aring_size; +#undef _ + +#define _(sz,n) \ + do { \ + ring_alloc_t _rp; \ + _rp.rp = svm_queue_init ((n), (sz), 0, 0); \ + _rp.size = (sz); \ + _rp.nitems = n; \ + _rp.hits = 0; \ + _rp.misses = 0; \ + vec_add1(shmem_hdr->client_rings, _rp); \ + } while (0); + + foreach_clnt_aring_size; +#undef _ +} + +void +vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config) +{ + api_main_t *am = &api_main; + vl_api_shm_elem_config_t *c; + ring_alloc_t *rp; + u32 size; + + if (!config) + { + vl_api_default_mem_config (hdr); + return; + } + + vec_foreach (c, config) + { + switch (c->type) + { + case VL_API_QUEUE: + hdr->vl_input_queue = svm_queue_init (c->count, + c->size, + getpid (), am->vlib_signal); + continue; + case VL_API_VLIB_RING: + vec_add2 (hdr->vl_rings, rp, 1); + break; + case VL_API_CLIENT_RING: + vec_add2 (hdr->client_rings, rp, 1); + break; + default: + clib_warning ("unknown config type: %d", c->type); + continue; + } + + size = sizeof (ring_alloc_t) + c->size; + rp->rp = svm_queue_init (c->count, size, 0, 0); + rp->size = size; + rp->nitems = c->count; + rp->hits = 0; + rp->misses = 0; + } +} + +void +vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config, + int is_vlib, int is_private_region) +{ + api_main_t *am = &api_main; + vl_shmem_hdr_t *shmem_hdr = 0; + void *oldheap; + ASSERT (vlib_rp); + + /* $$$$ need private region config parameters */ + + oldheap = svm_push_data_heap (vlib_rp); + + vec_validate (shmem_hdr, 0); + shmem_hdr->version = VL_SHM_VERSION; + shmem_hdr->clib_file_index = VL_API_INVALID_FI; + + /* Set up the queue and msg ring allocator */ + vl_api_mem_config (shmem_hdr, config); + + if (is_private_region == 0) + { + am->shmem_hdr = shmem_hdr; + am->vlib_rp = vlib_rp; + am->our_pid = getpid (); + if (is_vlib) + am->shmem_hdr->vl_pid = am->our_pid; + } + else + shmem_hdr->vl_pid = am->our_pid; + + svm_pop_heap (oldheap); + + /* + * After absolutely everything that a client might see is set up, + * declare the shmem region valid + */ + vlib_rp->user_ctx = shmem_hdr; + + pthread_mutex_unlock (&vlib_rp->mutex); +} + int -vl_map_shmem (char *region_name, int is_vlib) +vl_map_shmem (const char *region_name, int is_vlib) { svm_map_region_args_t _a, *a = &_a; svm_region_t *vlib_rp, *root_rp; - void *oldheap; - vl_shmem_hdr_t *shmem_hdr = 0; api_main_t *am = &api_main; - int i; + int i, rv; struct timespec ts, tsrem; - - if (is_vlib == 0) - svm_region_init_chroot (am->root_path); + char *vpe_api_region_suffix = "-vpe-api"; memset (a, 0, sizeof (*a)); - a->name = region_name; + if (strstr (region_name, vpe_api_region_suffix)) + { + u8 *root_path = format (0, "%s", region_name); + _vec_len (root_path) = (vec_len (root_path) - + strlen (vpe_api_region_suffix)); + vec_terminate_c_string (root_path); + a->root_path = (const char *) root_path; + am->root_path = (const char *) root_path; + } + + if (is_vlib == 0) + { + rv = svm_region_init_chroot (am->root_path); + if (rv) + return rv; + } + + if (a->root_path != NULL) + { + a->name = "/vpe-api"; + } + else + a->name = region_name; a->size = am->api_size ? am->api_size : (16 << 20); a->flags = SVM_FLAGS_MHEAP; a->uid = am->api_uid; @@ -335,7 +564,7 @@ vl_map_shmem (char *region_name, int is_vlib) am->our_pid = getpid (); if (is_vlib) { - unix_shared_memory_queue_t *q; + svm_queue_t *q; uword old_msg; /* * application restart. Reset cached pids, API message @@ -368,9 +597,7 @@ vl_map_shmem (char *region_name, int is_vlib) mutex_ok: am->vlib_rp = vlib_rp; - while (unix_shared_memory_queue_sub (q, - (u8 *) & old_msg, - 1 /* nowait */ ) + while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0) != -2 /* queue underflow */ ) { vl_msg_api_free_nolock ((void *) old_msg); @@ -421,61 +648,9 @@ vl_map_shmem (char *region_name, int is_vlib) } /* Nope, it's our problem... */ + vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ , + 0 /* is_private_region */ ); - oldheap = svm_push_data_heap (vlib_rp); - - vec_validate (shmem_hdr, 0); - shmem_hdr->version = VL_SHM_VERSION; - - /* vlib main input queue */ - shmem_hdr->vl_input_queue = - unix_shared_memory_queue_init (1024, sizeof (uword), getpid (), - am->vlib_signal); - - /* Set up the msg ring allocator */ -#define _(sz,n) \ - do { \ - ring_alloc_t _rp; \ - _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \ - _rp.size = (sz); \ - _rp.nitems = n; \ - _rp.hits = 0; \ - _rp.misses = 0; \ - vec_add1(shmem_hdr->vl_rings, _rp); \ - } while (0); - - foreach_vl_aring_size; -#undef _ - -#define _(sz,n) \ - do { \ - ring_alloc_t _rp; \ - _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \ - _rp.size = (sz); \ - _rp.nitems = n; \ - _rp.hits = 0; \ - _rp.misses = 0; \ - vec_add1(shmem_hdr->client_rings, _rp); \ - } while (0); - - foreach_clnt_aring_size; -#undef _ - - am->shmem_hdr = shmem_hdr; - am->vlib_rp = vlib_rp; - am->our_pid = getpid (); - if (is_vlib) - am->shmem_hdr->vl_pid = am->our_pid; - - svm_pop_heap (oldheap); - - /* - * After absolutely everything that a client might see is set up, - * declare the shmem region valid - */ - vlib_rp->user_ctx = shmem_hdr; - - pthread_mutex_unlock (&vlib_rp->mutex); vec_add1 (am->mapped_shmem_regions, vlib_rp); return 0; } @@ -515,19 +690,7 @@ vl_unmap_shmem (void) } void -vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem) -{ - api_main_t *am = &api_main; - uword *trace = (uword *) elem; - - if (am->tx_trace && am->tx_trace->enabled) - vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); - - (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ ); -} - -void -vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem) +vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem) { api_main_t *am = &api_main; uword *trace = (uword *) elem; @@ -535,312 +698,25 @@ vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem) if (am->tx_trace && am->tx_trace->enabled) vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); - (void) unix_shared_memory_queue_add_nolock (q, elem); -} - -static void -vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) -{ - serialize_main_t _sm, *sm = &_sm; - api_main_t *am = &api_main; - u8 *tblv; - u32 nmsgs; - int i; - u8 *name_and_crc; - u32 msg_index; - - am->my_client_index = mp->index; - am->my_registration = (vl_api_registration_t *) (uword) mp->handle; - - /* Clean out any previous hash table (unlikely) */ - if (am->msg_index_by_name_and_crc) - { - int i; - u8 **keys = 0; - hash_pair_t *hp; - /* *INDENT-OFF* */ - hash_foreach_pair (hp, am->msg_index_by_name_and_crc, - ({ - vec_add1 (keys, (u8 *) hp->key); - })); - /* *INDENT-ON* */ - for (i = 0; i < vec_len (keys); i++) - vec_free (keys[i]); - vec_free (keys); - } - - am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); - - /* Recreate the vnet-side API message handler table */ - tblv = (u8 *) mp->message_table; - serialize_open_vector (sm, tblv); - unserialize_integer (sm, &nmsgs, sizeof (u32)); - - for (i = 0; i < nmsgs; i++) - { - msg_index = unserialize_likely_small_unsigned_integer (sm); - unserialize_cstring (sm, (char **) &name_and_crc); - hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index); - } -} - -u32 -vl_api_get_msg_index (u8 * name_and_crc) -{ - api_main_t *am = &api_main; - uword *p; - - if (am->msg_index_by_name_and_crc) - { - p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc); - if (p) - return p[0]; - } - return ~0; + (void) svm_queue_add (q, elem, 0 /* nowait */ ); } int -vl_client_connect (char *name, int ctx_quota, int input_queue_size) +vl_mem_api_can_send (svm_queue_t * q) { - svm_region_t *svm; - vl_api_memclnt_create_t *mp; - vl_api_memclnt_create_reply_t *rp; - unix_shared_memory_queue_t *vl_input_queue; - vl_shmem_hdr_t *shmem_hdr; - int rv = 0; - void *oldheap; - api_main_t *am = &api_main; - - if (am->my_registration) - { - clib_warning ("client %s already connected...", name); - return -1; - } - - if (am->vlib_rp == 0) - { - clib_warning ("am->vlib_rp NULL"); - return -1; - } - - svm = am->vlib_rp; - shmem_hdr = am->shmem_hdr; - - if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) - { - clib_warning ("shmem_hdr / input queue NULL"); - return -1; - } - - pthread_mutex_lock (&svm->mutex); - oldheap = svm_push_data_heap (svm); - vl_input_queue = - unix_shared_memory_queue_init (input_queue_size, sizeof (uword), - getpid (), 0); - pthread_mutex_unlock (&svm->mutex); - svm_pop_heap (oldheap); - - am->my_client_index = ~0; - am->my_registration = 0; - am->vl_input_queue = vl_input_queue; - - mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t)); - memset (mp, 0, sizeof (*mp)); - mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE); - mp->ctx_quota = ctx_quota; - mp->input_queue = (uword) vl_input_queue; - strncpy ((char *) mp->name, name, sizeof (mp->name) - 1); - - vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); - - while (1) - { - int qstatus; - struct timespec ts, tsrem; - int i; - - /* Wait up to 10 seconds */ - for (i = 0; i < 1000; i++) - { - qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, - 1 /* nowait */ ); - if (qstatus == 0) - goto read_one_msg; - ts.tv_sec = 0; - ts.tv_nsec = 10000 * 1000; /* 10 ms */ - while (nanosleep (&ts, &tsrem) < 0) - ts = tsrem; - } - /* Timeout... */ - clib_warning ("memclnt_create_reply timeout"); - return -1; - - read_one_msg: - if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) - { - clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id)); - continue; - } - rv = clib_net_to_host_u32 (rp->response); - - vl_msg_api_handler ((void *) rp); - break; - } - return (rv); -} - -static void -vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp) -{ - void *oldheap; - api_main_t *am = &api_main; - - pthread_mutex_lock (&am->vlib_rp->mutex); - oldheap = svm_push_data_heap (am->vlib_rp); - unix_shared_memory_queue_free (am->vl_input_queue); - pthread_mutex_unlock (&am->vlib_rp->mutex); - svm_pop_heap (oldheap); - - am->my_client_index = ~0; - am->my_registration = 0; - am->vl_input_queue = 0; + return (q->cursize < q->maxsize); } void -vl_client_disconnect (void) -{ - vl_api_memclnt_delete_t *mp; - vl_api_memclnt_delete_reply_t *rp; - unix_shared_memory_queue_t *vl_input_queue; - vl_shmem_hdr_t *shmem_hdr; - time_t begin; - api_main_t *am = &api_main; - - ASSERT (am->vlib_rp); - shmem_hdr = am->shmem_hdr; - ASSERT (shmem_hdr && shmem_hdr->vl_input_queue); - - vl_input_queue = am->vl_input_queue; - - mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t)); - memset (mp, 0, sizeof (*mp)); - mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE); - mp->index = am->my_client_index; - mp->handle = (uword) am->my_registration; - - vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); - - /* - * Have to be careful here, in case the client is disconnecting - * because e.g. the vlib process died, or is unresponsive. - */ - - begin = time (0); - while (1) - { - time_t now; - - now = time (0); - - if (now >= (begin + 2)) - { - clib_warning ("peer unresponsive, give up"); - am->my_client_index = ~0; - am->my_registration = 0; - am->shmem_hdr = 0; - break; - } - if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0) - continue; - - /* drain the queue */ - if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) - { - vl_msg_api_handler ((void *) rp); - continue; - } - vl_msg_api_handler ((void *) rp); - break; - } -} - -static inline vl_api_registration_t * -vl_api_client_index_to_registration_internal (u32 handle) -{ - vl_api_registration_t **regpp; - vl_api_registration_t *regp; - api_main_t *am = &api_main; - u32 index; - - index = vl_msg_api_handle_get_index (handle); - if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK) - != vl_msg_api_handle_get_epoch (handle)) - { - vl_msg_api_increment_missing_client_counter (); - return 0; - } - - regpp = am->vl_clients + index; - - if (pool_is_free (am->vl_clients, regpp)) - { - vl_msg_api_increment_missing_client_counter (); - return 0; - } - regp = *regpp; - return (regp); -} - -vl_api_registration_t * -vl_api_client_index_to_registration (u32 index) -{ - return (vl_api_client_index_to_registration_internal (index)); -} - -unix_shared_memory_queue_t * -vl_api_client_index_to_input_queue (u32 index) +vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem) { - vl_api_registration_t *regp; api_main_t *am = &api_main; + uword *trace = (uword *) elem; - /* Special case: vlib trying to send itself a message */ - if (index == (u32) ~ 0) - return (am->shmem_hdr->vl_input_queue); - - regp = vl_api_client_index_to_registration_internal (index); - if (!regp) - return 0; - return (regp->vl_input_queue); -} - -#define foreach_api_client_msg \ -_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ -_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) - -int -vl_client_api_map (char *region_name) -{ - int rv; - - if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0) - { - return rv; - } - -#define _(N,n) \ - vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \ - vl_api_##n##_t_handler, \ - 0/* cleanup */, 0/* endian */, 0/* print */, \ - sizeof(vl_api_##n##_t), 1); - foreach_api_client_msg; -#undef _ - return 0; -} + if (am->tx_trace && am->tx_trace->enabled) + vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); -void -vl_client_api_unmap (void) -{ - vl_unmap_shmem (); + (void) svm_queue_add_nolock (q, elem); } /*