X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlibmemory%2Fmemory_shared.c;h=1716f27146625c4071d1b5241e559cc701d0e236;hb=95eb01fdc65b692065ace6d76870d8cc9f3e2c84;hp=fbdabd06e36f037ee5f330a1329d58fe9bbbfda3;hpb=3cdc25ffbaa572639f99e197172c568e4324bc03;p=vpp.git diff --git a/src/vlibmemory/memory_shared.c b/src/vlibmemory/memory_shared.c index fbdabd06e36..1716f271466 100644 --- a/src/vlibmemory/memory_shared.c +++ b/src/vlibmemory/memory_shared.c @@ -25,32 +25,43 @@ #include #include #include + #include #include #include +#include +#include #include #include -#include -#include - +#include #include +#include #define vl_typedefs #include #undef vl_typedefs -static inline void * -vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) +#define DEBUG_MESSAGE_BUFFER_OVERRUN 0 + +CLIB_NOSANITIZE_ADDR static inline void * +vl_msg_api_alloc_internal (svm_region_t * vlib_rp, 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; + api_main_t *am = vlibapi_get_main (); + + shmem_hdr = (void *) vlib_rp->user_ctx; - 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) { @@ -111,9 +122,9 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) 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)) + if (msg_id < vec_len (vlibapi_get_main ()->msg_names)) clib_warning ("msg id %d name %s", (u32) msg_id, - api_main.msg_names[msg_id]); + vlibapi_get_main ()->msg_names[msg_id]); } shmem_hdr->garbage_collects++; goto collected; @@ -152,15 +163,13 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) */ am->ring_misses++; - pthread_mutex_lock (&am->vlib_rp->mutex); - oldheap = svm_push_data_heap (am->vlib_rp); + oldheap = vl_msg_push_heap_w_region (vlib_rp); if (may_return_null) { rv = clib_mem_alloc_or_null (nbytes); if (PREDICT_FALSE (rv == 0)) { - svm_pop_heap (oldheap); - pthread_mutex_unlock (&am->vlib_rp->mutex); + vl_msg_pop_heap_w_region (vlib_rp, oldheap); return 0; } } @@ -168,11 +177,21 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null) rv = clib_mem_alloc (nbytes); rv->q = 0; - svm_pop_heap (oldheap); - pthread_mutex_unlock (&am->vlib_rp->mutex); + rv->gc_mark_timestamp = 0; + vl_msg_pop_heap_w_region (vlib_rp, oldheap); 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)); + + VL_MSG_API_UNPOISON (rv->data); return (rv->data); } @@ -180,45 +199,77 @@ void * vl_msg_api_alloc (int nbytes) { int pool; - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; /* * Clients use pool-0, vlib proc uses pool 1 */ pool = (am->our_pid == shmem_hdr->vl_pid); - return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ ); + return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool, + 0 /* may_return_null */ ); +} + +void * +vl_msg_api_alloc_zero (int nbytes) +{ + void *ret; + + ret = vl_msg_api_alloc (nbytes); + clib_memset (ret, 0, nbytes); + return ret; } void * vl_msg_api_alloc_or_null (int nbytes) { int pool; - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; pool = (am->our_pid == shmem_hdr->vl_pid); - return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ ); + return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool, + 1 /* may_return_null */ ); } void * vl_msg_api_alloc_as_if_client (int nbytes) { - return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ ); + api_main_t *am = vlibapi_get_main (); + return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0, + 0 /* may_return_null */ ); +} + +void * +vl_msg_api_alloc_zero_as_if_client (int nbytes) +{ + void *ret; + + ret = vl_msg_api_alloc_as_if_client (nbytes); + clib_memset (ret, 0, nbytes); + return ret; } void * vl_msg_api_alloc_as_if_client_or_null (int nbytes) { - return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ ); + api_main_t *am = vlibapi_get_main (); + return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0, + 1 /* may_return_null */ ); +} + +void * +vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes) +{ + return vl_msg_api_alloc_internal (reg->vlib_rp, nbytes, 0, + 0 /* may_return_null */ ); } void -vl_msg_api_free (void *a) +vl_msg_api_free_w_region (svm_region_t * vlib_rp, void *a) { msgbuf_t *rv; void *oldheap; - api_main_t *am = &api_main; rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data)); @@ -231,14 +282,36 @@ vl_msg_api_free (void *a) { 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 + VL_MSG_API_POISON (rv->data); return; } - pthread_mutex_lock (&am->vlib_rp->mutex); - oldheap = svm_push_data_heap (am->vlib_rp); + oldheap = vl_msg_push_heap_w_region (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); + vl_msg_pop_heap_w_region (vlib_rp, oldheap); +} + +void +vl_msg_api_free (void *a) +{ + api_main_t *am = vlibapi_get_main (); + vl_msg_api_free_w_region (am->vlib_rp, a); } static void @@ -246,7 +319,7 @@ vl_msg_api_free_nolock (void *a) { msgbuf_t *rv; void *oldheap; - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data)); /* @@ -257,6 +330,7 @@ vl_msg_api_free_nolock (void *a) if (rv->q) { rv->q = 0; + VL_MSG_API_POISON (rv->data); return; } @@ -268,7 +342,7 @@ vl_msg_api_free_nolock (void *a) void vl_set_memory_root_path (const char *name) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->root_path = name; } @@ -276,7 +350,7 @@ vl_set_memory_root_path (const char *name) void vl_set_memory_uid (int uid) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->api_uid = uid; } @@ -284,7 +358,7 @@ vl_set_memory_uid (int uid) void vl_set_memory_gid (int gid) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->api_gid = gid; } @@ -292,7 +366,7 @@ vl_set_memory_gid (int gid) void vl_set_global_memory_baseva (u64 baseva) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->global_baseva = baseva; } @@ -300,7 +374,7 @@ vl_set_global_memory_baseva (u64 baseva) void vl_set_global_memory_size (u64 size) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->global_size = size; } @@ -308,7 +382,7 @@ vl_set_global_memory_size (u64 size) void vl_set_api_memory_size (u64 size) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->api_size = size; } @@ -316,7 +390,7 @@ vl_set_api_memory_size (u64 size) void vl_set_global_pvt_heap_size (u64 size) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->global_pvt_heap_size = size; } @@ -324,38 +398,192 @@ vl_set_global_pvt_heap_size (u64 size) void vl_set_api_pvt_heap_size (u64 size) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); am->api_pvt_heap_size = size; } +static void +vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr) +{ + api_main_t *am = vlibapi_get_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_alloc_and_init (vlib_input_queue_length, sizeof (uword), + getpid ()); + +#define _(sz,n) \ + do { \ + ring_alloc_t _rp; \ + _rp.rp = svm_queue_alloc_and_init ((n), (sz), 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_alloc_and_init ((n), (sz), 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) +{ + 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_alloc_and_init (c->count, c->size, + getpid ()); + 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_alloc_and_init (c->count, size, 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 = vlibapi_get_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 (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, rv; + api_main_t *am = vlibapi_get_main (); + int i; struct timespec ts, tsrem; - u32 vlib_input_queue_length; + char *vpe_api_region_suffix = "-vpe-api"; - memset (a, 0, sizeof (*a)); + clib_memset (a, 0, sizeof (*a)); - if (strstr (region_name, "-vpe-api")) + if (strstr (region_name, vpe_api_region_suffix)) { - char root_path[strlen (region_name)]; - strncpy (root_path, region_name, strlen (region_name) - 8); - a->root_path = root_path; - am->root_path = root_path; + 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; + int tfd; + u8 *api_name; + /* + * Clients wait for vpp to set up the root / API regioins + */ + if (am->root_path) + api_name = format (0, "/dev/shm/%s-%s%c", am->root_path, + region_name + 1, 0); + else + api_name = format (0, "/dev/shm%s%c", region_name, 0); + + /* Wait up to 100 seconds... */ + for (i = 0; i < 10000; i++) + { + ts.tv_sec = 0; + ts.tv_nsec = 10000 * 1000; /* 10 ms */ + while (nanosleep (&ts, &tsrem) < 0) + ts = tsrem; + tfd = open ((char *) api_name, O_RDWR); + if (tfd >= 0) + break; + } + vec_free (api_name); + if (tfd < 0) + { + clib_warning ("region init fail"); + return -2; + } + close (tfd); + svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ()); } if (a->root_path != NULL) @@ -383,7 +611,7 @@ vl_map_shmem (const 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 @@ -411,14 +639,12 @@ vl_map_shmem (const char *region_name, int is_vlib) ts = tsrem; } /* Mutex buggered, "fix" it */ - memset (&q->mutex, 0, sizeof (q->mutex)); + clib_memset (&q->mutex, 0, sizeof (q->mutex)); clib_warning ("forcibly release main input queue mutex"); 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); @@ -469,65 +695,9 @@ vl_map_shmem (const 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 */ - 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 = - unix_shared_memory_queue_init (vlib_input_queue_length, 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; } @@ -535,17 +705,17 @@ vl_map_shmem (const char *region_name, int is_vlib) void vl_register_mapped_shmem_region (svm_region_t * rp) { - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); vec_add1 (am->mapped_shmem_regions, rp); } -void -vl_unmap_shmem (void) +static void +vl_unmap_shmem_internal (u8 is_client) { svm_region_t *rp; int i; - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); if (!svm_get_root_rp ()) return; @@ -553,13 +723,14 @@ vl_unmap_shmem (void) for (i = 0; i < vec_len (am->mapped_shmem_regions); i++) { rp = am->mapped_shmem_regions[i]; - svm_region_unmap (rp); + is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp); } vec_free (am->mapped_shmem_regions); am->shmem_hdr = 0; - svm_region_exit (); + is_client ? svm_region_exit_client () : svm_region_exit (); + /* $$$ more careful cleanup, valgrind run... */ vec_free (am->msg_handlers); vec_free (am->msg_endian_handlers); @@ -567,91 +738,75 @@ vl_unmap_shmem (void) } void -vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem) +vl_unmap_shmem (void) { - 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 */ ); + vl_unmap_shmem_internal (0); } void -vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem) +vl_unmap_shmem_client (void) { - 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_nolock (q, elem); + vl_unmap_shmem_internal (1); } -u32 -vl_api_get_msg_index (u8 * name_and_crc) +void +vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem) { - api_main_t *am = &api_main; - uword *p; + api_main_t *am = vlibapi_get_main (); + void *msg = (void *) *(uword *) elem; - 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; -} - -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 (am->tx_trace && am->tx_trace->enabled) + vl_msg_api_trace (am, am->tx_trace, msg); - if (pool_is_free (am->vl_clients, regpp)) + /* + * Announce a probable binary API client bug: + * some client's input queue is stuffed. + * The situation may be recoverable, or not. + */ + if (PREDICT_FALSE + (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize))) { - vl_msg_api_increment_missing_client_counter (); - return 0; + if (PREDICT_FALSE (am->elog_trace_api_messages)) + { + /* *INDENT-OFF* */ + ELOG_TYPE_DECLARE (e) = + { + .format = "api-client-queue-stuffed: %x%x", + .format_args = "i4i4", + }; + /* *INDENT-ON* */ + struct + { + u32 hi, low; + } *ed; + ed = ELOG_DATA (am->elog_main, e); + ed->hi = (uword) q >> 32; + ed->low = (uword) q & 0xFFFFFFFF; + clib_warning ("WARNING: client input queue at %llx is stuffed...", + q); + } } - regp = *regpp; - return (regp); + VL_MSG_API_POISON (msg); + (void) svm_queue_add (q, elem, 0 /* nowait */ ); } -vl_api_registration_t * -vl_api_client_index_to_registration (u32 index) +int +vl_mem_api_can_send (svm_queue_t * q) { - return (vl_api_client_index_to_registration_internal (index)); + return (q->cursize < q->maxsize); } -unix_shared_memory_queue_t * -vl_api_client_index_to_input_queue (u32 index) +void +vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem) { - vl_api_registration_t *regp; - api_main_t *am = &api_main; + api_main_t *am = vlibapi_get_main (); + void *msg = (void *) *(uword *) elem; - /* Special case: vlib trying to send itself a message */ - if (index == (u32) ~ 0) - return (am->shmem_hdr->vl_input_queue); + if (am->tx_trace && am->tx_trace->enabled) + vl_msg_api_trace (am, am->tx_trace, msg); - regp = vl_api_client_index_to_registration_internal (index); - if (!regp) - return 0; - return (regp->vl_input_queue); + (void) svm_queue_add_nolock (q, elem); + VL_MSG_API_POISON (msg); } /*