X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlibmemory%2Fmemory_shared.c;h=1ccd563639f716dd87fb2bad64dc3edb9924714d;hb=22af447454cf097ac4fc59f9072d805c371ab803;hp=0a3c23cbb3edde396cd41c39a2a2ed1f51d261a6;hpb=90a63988fa01685626b6d6a01b79ea5370f7fbac;p=vpp.git diff --git a/src/vlibmemory/memory_shared.c b/src/vlibmemory/memory_shared.c index 0a3c23cbb3e..1ccd563639f 100644 --- a/src/vlibmemory/memory_shared.c +++ b/src/vlibmemory/memory_shared.c @@ -25,22 +25,20 @@ #include #include #include + #include #include #include +#include #include #include -#include -#include - +#include #include #define vl_typedefs #include #undef vl_typedefs -socket_main_t socket_main; - #define DEBUG_MESSAGE_BUFFER_OVERRUN 0 static inline void * @@ -49,7 +47,7 @@ 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; @@ -232,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) { @@ -376,13 +393,13 @@ vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr) 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); + svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword), + getpid ()); #define _(sz,n) \ do { \ ring_alloc_t _rp; \ - _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \ + _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \ _rp.size = (sz); \ _rp.nitems = n; \ _rp.hits = 0; \ @@ -396,7 +413,7 @@ vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr) #define _(sz,n) \ do { \ ring_alloc_t _rp; \ - _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \ + _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \ _rp.size = (sz); \ _rp.nitems = n; \ _rp.hits = 0; \ @@ -411,7 +428,6 @@ vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr) 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; @@ -427,10 +443,8 @@ vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config) switch (c->type) { case VL_API_QUEUE: - hdr->vl_input_queue = unix_shared_memory_queue_init (c->count, - c->size, - getpid (), - am->vlib_signal); + 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); @@ -444,7 +458,7 @@ vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config) } size = sizeof (ring_alloc_t) + c->size; - rp->rp = unix_shared_memory_queue_init (c->count, size, 0, 0); + rp->rp = svm_queue_alloc_and_init (c->count, size, 0); rp->size = size; rp->nitems = c->count; rp->hits = 0; @@ -467,6 +481,7 @@ vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config, 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); @@ -493,7 +508,6 @@ vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config, pthread_mutex_unlock (&vlib_rp->mutex); } - int vl_map_shmem (const char *region_name, int is_vlib) { @@ -504,7 +518,7 @@ vl_map_shmem (const char *region_name, int is_vlib) struct timespec ts, tsrem; char *vpe_api_region_suffix = "-vpe-api"; - memset (a, 0, sizeof (*a)); + clib_memset (a, 0, sizeof (*a)); if (strstr (region_name, vpe_api_region_suffix)) { @@ -518,6 +532,35 @@ vl_map_shmem (const char *region_name, int is_vlib) if (is_vlib == 0) { + 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); rv = svm_region_init_chroot (am->root_path); if (rv) return rv; @@ -548,7 +591,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 @@ -576,14 +619,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); @@ -649,8 +690,8 @@ vl_register_mapped_shmem_region (svm_region_t * rp) 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; @@ -662,13 +703,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); @@ -676,19 +718,19 @@ 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]); + vl_unmap_shmem_internal (0); +} - (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ ); +void +vl_unmap_shmem_client (void) +{ + vl_unmap_shmem_internal (1); } 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; @@ -696,74 +738,33 @@ 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); -} - -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; -} - -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); + /* + * 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))) + clib_warning ("WARNING: client input queue at %llx is stuffed...", q); + (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) { - if (PREDICT_FALSE (socket_main.current_rp != 0)) - return socket_main.current_rp; - - 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; + uword *trace = (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, (void *) trace[0]); - 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); } /*