2 *------------------------------------------------------------------
3 * memclnt_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
7 * Copyright (c) 2009 Cisco and/or its affiliates.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *------------------------------------------------------------------
29 #include <vppinfra/format.h>
30 #include <vppinfra/byte_order.h>
31 #include <vppinfra/error.h>
32 #include <vppinfra/elog.h>
33 #include <svm/queue.h>
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
36 #include <vlibmemory/memory_api.h>
37 #include <vlibmemory/vl_memory_msg_enum.h>
40 #include <vlibmemory/vl_memory_api_h.h>
43 #define DEBUG_MESSAGE_BUFFER_OVERRUN 0
45 CLIB_NOSANITIZE_ADDR static inline void *
46 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
53 vl_shmem_hdr_t *shmem_hdr;
54 api_main_t *am = &api_main;
56 shmem_hdr = am->shmem_hdr;
58 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
62 ASSERT (pool == 0 || vlib_get_thread_index () == 0);
66 clib_warning ("shared memory header NULL");
70 /* account for the msgbuf_t header */
71 nbytes += sizeof (msgbuf_t);
73 if (shmem_hdr->vl_rings == 0)
75 clib_warning ("vl_rings NULL");
80 if (shmem_hdr->client_rings == 0)
82 clib_warning ("client_rings NULL");
87 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
88 for (i = 0; i < vec_len (ap); i++)
91 if (nbytes > ap[i].size)
99 pthread_mutex_lock (&q->mutex);
101 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
103 * Is this item still in use?
107 u32 now = (u32) time (0);
109 if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
110 rv->gc_mark_timestamp = now;
113 if (now - rv->gc_mark_timestamp > 10)
117 u16 *msg_idp, msg_id;
119 ("garbage collect pool %d ring %d index %d", pool, i,
121 msg_idp = (u16 *) (rv->data);
122 msg_id = clib_net_to_host_u16 (*msg_idp);
123 if (msg_id < vec_len (api_main.msg_names))
124 clib_warning ("msg id %d name %s", (u32) msg_id,
125 api_main.msg_names[msg_id]);
127 shmem_hdr->garbage_collects++;
133 /* yes, loser; try next larger pool */
136 pthread_mutex_unlock (&q->mutex);
141 /* OK, we have a winner */
144 * Remember the source queue, although we
145 * don't need to know the queue to free the item.
148 rv->gc_mark_timestamp = 0;
150 if (q->head == q->maxsize)
154 pthread_mutex_unlock (&q->mutex);
159 * Request too big, or head element of all size-compatible rings
160 * still in use. Fall back to shared-memory malloc.
164 pthread_mutex_lock (&am->vlib_rp->mutex);
165 oldheap = svm_push_data_heap (am->vlib_rp);
168 rv = clib_mem_alloc_or_null (nbytes);
169 if (PREDICT_FALSE (rv == 0))
171 svm_pop_heap (oldheap);
172 pthread_mutex_unlock (&am->vlib_rp->mutex);
177 rv = clib_mem_alloc (nbytes);
180 rv->gc_mark_timestamp = 0;
181 svm_pop_heap (oldheap);
182 pthread_mutex_unlock (&am->vlib_rp->mutex);
185 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
189 overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
190 *overrun = 0x1badbabe;
193 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
195 VL_MSG_API_UNPOISON (rv->data);
200 vl_msg_api_alloc (int nbytes)
203 api_main_t *am = &api_main;
204 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
207 * Clients use pool-0, vlib proc uses pool 1
209 pool = (am->our_pid == shmem_hdr->vl_pid);
210 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
214 vl_msg_api_alloc_zero (int nbytes)
218 ret = vl_msg_api_alloc (nbytes);
219 clib_memset (ret, 0, nbytes);
224 vl_msg_api_alloc_or_null (int nbytes)
227 api_main_t *am = &api_main;
228 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
230 pool = (am->our_pid == shmem_hdr->vl_pid);
231 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
235 vl_msg_api_alloc_as_if_client (int nbytes)
237 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
241 vl_msg_api_alloc_zero_as_if_client (int nbytes)
245 ret = vl_msg_api_alloc_as_if_client (nbytes);
246 clib_memset (ret, 0, nbytes);
251 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
253 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
257 vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
259 api_main_t *am = &api_main;
260 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
261 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
264 vlib_rp = am->vlib_rp = reg->vlib_rp;
265 am->shmem_hdr = (void *) vlib_rp->user_ctx;
267 msg = vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
269 am->shmem_hdr = save_shmem_hdr;
270 am->vlib_rp = save_vlib_rp;
276 vl_msg_api_free (void *a)
280 api_main_t *am = &api_main;
282 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
285 * Here's the beauty of the scheme. Only one proc/thread has
286 * control of a given message buffer. To free a buffer, we just clear the
287 * queue field, and leave. No locks, no hits, no errors...
292 rv->gc_mark_timestamp = 0;
293 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
296 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
297 ASSERT (*overrun == 0x1badbabe);
300 VL_MSG_API_POISON (rv->data);
304 pthread_mutex_lock (&am->vlib_rp->mutex);
305 oldheap = svm_push_data_heap (am->vlib_rp);
307 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
310 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
311 ASSERT (*overrun == 0x1badbabe);
316 svm_pop_heap (oldheap);
317 pthread_mutex_unlock (&am->vlib_rp->mutex);
321 vl_msg_api_free_nolock (void *a)
325 api_main_t *am = &api_main;
327 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
329 * Here's the beauty of the scheme. Only one proc/thread has
330 * control of a given message buffer. To free a buffer, we just clear the
331 * queue field, and leave. No locks, no hits, no errors...
336 VL_MSG_API_POISON (rv->data);
340 oldheap = svm_push_data_heap (am->vlib_rp);
342 svm_pop_heap (oldheap);
346 vl_set_memory_root_path (const char *name)
348 api_main_t *am = &api_main;
350 am->root_path = name;
354 vl_set_memory_uid (int uid)
356 api_main_t *am = &api_main;
362 vl_set_memory_gid (int gid)
364 api_main_t *am = &api_main;
370 vl_set_global_memory_baseva (u64 baseva)
372 api_main_t *am = &api_main;
374 am->global_baseva = baseva;
378 vl_set_global_memory_size (u64 size)
380 api_main_t *am = &api_main;
382 am->global_size = size;
386 vl_set_api_memory_size (u64 size)
388 api_main_t *am = &api_main;
394 vl_set_global_pvt_heap_size (u64 size)
396 api_main_t *am = &api_main;
398 am->global_pvt_heap_size = size;
402 vl_set_api_pvt_heap_size (u64 size)
404 api_main_t *am = &api_main;
406 am->api_pvt_heap_size = size;
410 vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
412 api_main_t *am = &api_main;
413 u32 vlib_input_queue_length;
415 /* vlib main input queue */
416 vlib_input_queue_length = 1024;
417 if (am->vlib_input_queue_length)
418 vlib_input_queue_length = am->vlib_input_queue_length;
420 shmem_hdr->vl_input_queue =
421 svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
427 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
432 vec_add1(shmem_hdr->vl_rings, _rp); \
435 foreach_vl_aring_size;
441 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
446 vec_add1(shmem_hdr->client_rings, _rp); \
449 foreach_clnt_aring_size;
454 vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
456 vl_api_shm_elem_config_t *c;
462 vl_api_default_mem_config (hdr);
466 vec_foreach (c, config)
471 hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
474 case VL_API_VLIB_RING:
475 vec_add2 (hdr->vl_rings, rp, 1);
477 case VL_API_CLIENT_RING:
478 vec_add2 (hdr->client_rings, rp, 1);
481 clib_warning ("unknown config type: %d", c->type);
485 size = sizeof (ring_alloc_t) + c->size;
486 rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
488 rp->nitems = c->count;
495 vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
496 int is_vlib, int is_private_region)
498 api_main_t *am = &api_main;
499 vl_shmem_hdr_t *shmem_hdr = 0;
503 /* $$$$ need private region config parameters */
505 oldheap = svm_push_data_heap (vlib_rp);
507 vec_validate (shmem_hdr, 0);
508 shmem_hdr->version = VL_SHM_VERSION;
509 shmem_hdr->clib_file_index = VL_API_INVALID_FI;
511 /* Set up the queue and msg ring allocator */
512 vl_api_mem_config (shmem_hdr, config);
514 if (is_private_region == 0)
516 am->shmem_hdr = shmem_hdr;
517 am->vlib_rp = vlib_rp;
518 am->our_pid = getpid ();
520 am->shmem_hdr->vl_pid = am->our_pid;
523 shmem_hdr->vl_pid = am->our_pid;
525 svm_pop_heap (oldheap);
528 * After absolutely everything that a client might see is set up,
529 * declare the shmem region valid
531 vlib_rp->user_ctx = shmem_hdr;
533 pthread_mutex_unlock (&vlib_rp->mutex);
537 vl_map_shmem (const char *region_name, int is_vlib)
539 svm_map_region_args_t _a, *a = &_a;
540 svm_region_t *vlib_rp, *root_rp;
541 api_main_t *am = &api_main;
543 struct timespec ts, tsrem;
544 char *vpe_api_region_suffix = "-vpe-api";
546 clib_memset (a, 0, sizeof (*a));
548 if (strstr (region_name, vpe_api_region_suffix))
550 u8 *root_path = format (0, "%s", region_name);
551 _vec_len (root_path) = (vec_len (root_path) -
552 strlen (vpe_api_region_suffix));
553 vec_terminate_c_string (root_path);
554 a->root_path = (const char *) root_path;
555 am->root_path = (const char *) root_path;
563 * Clients wait for vpp to set up the root / API regioins
566 api_name = format (0, "/dev/shm/%s-%s%c", am->root_path,
569 api_name = format (0, "/dev/shm%s%c", region_name, 0);
571 /* Wait up to 100 seconds... */
572 for (i = 0; i < 10000; i++)
575 ts.tv_nsec = 10000 * 1000; /* 10 ms */
576 while (nanosleep (&ts, &tsrem) < 0)
578 tfd = open ((char *) api_name, O_RDWR);
585 clib_warning ("region init fail");
589 svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ());
592 if (a->root_path != NULL)
594 a->name = "/vpe-api";
597 a->name = region_name;
598 a->size = am->api_size ? am->api_size : (16 << 20);
599 a->flags = SVM_FLAGS_MHEAP;
600 a->uid = am->api_uid;
601 a->gid = am->api_gid;
602 a->pvt_heap_size = am->api_pvt_heap_size;
604 vlib_rp = svm_region_find_or_create (a);
609 pthread_mutex_lock (&vlib_rp->mutex);
610 /* Has someone else set up the shared-memory variable table? */
611 if (vlib_rp->user_ctx)
613 am->shmem_hdr = (void *) vlib_rp->user_ctx;
614 am->our_pid = getpid ();
620 * application restart. Reset cached pids, API message
621 * rings, list of clients; otherwise, various things
622 * fail. (e.g. queue non-empty notification)
625 /* ghosts keep the region from disappearing properly */
626 svm_client_scan_this_region_nolock (vlib_rp);
627 am->shmem_hdr->application_restarts++;
628 q = am->shmem_hdr->vl_input_queue;
629 am->shmem_hdr->vl_pid = getpid ();
630 q->consumer_pid = am->shmem_hdr->vl_pid;
631 /* Drain the input queue, freeing msgs */
632 for (i = 0; i < 10; i++)
634 if (pthread_mutex_trylock (&q->mutex) == 0)
636 pthread_mutex_unlock (&q->mutex);
640 ts.tv_nsec = 10000 * 1000; /* 10 ms */
641 while (nanosleep (&ts, &tsrem) < 0)
644 /* Mutex buggered, "fix" it */
645 clib_memset (&q->mutex, 0, sizeof (q->mutex));
646 clib_warning ("forcibly release main input queue mutex");
649 am->vlib_rp = vlib_rp;
650 while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
651 != -2 /* queue underflow */ )
653 vl_msg_api_free_nolock ((void *) old_msg);
654 am->shmem_hdr->restart_reclaims++;
656 pthread_mutex_unlock (&vlib_rp->mutex);
657 root_rp = svm_get_root_rp ();
659 /* Clean up the root region client list */
660 pthread_mutex_lock (&root_rp->mutex);
661 svm_client_scan_this_region_nolock (root_rp);
662 pthread_mutex_unlock (&root_rp->mutex);
666 pthread_mutex_unlock (&vlib_rp->mutex);
668 am->vlib_rp = vlib_rp;
669 vec_add1 (am->mapped_shmem_regions, vlib_rp);
672 /* Clients simply have to wait... */
675 pthread_mutex_unlock (&vlib_rp->mutex);
677 /* Wait up to 100 seconds... */
678 for (i = 0; i < 10000; i++)
681 ts.tv_nsec = 10000 * 1000; /* 10 ms */
682 while (nanosleep (&ts, &tsrem) < 0)
684 if (vlib_rp->user_ctx)
687 /* Clean up and leave... */
688 svm_region_unmap (vlib_rp);
689 clib_warning ("region init fail");
693 am->shmem_hdr = (void *) vlib_rp->user_ctx;
694 am->our_pid = getpid ();
695 am->vlib_rp = vlib_rp;
696 vec_add1 (am->mapped_shmem_regions, vlib_rp);
700 /* Nope, it's our problem... */
701 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
702 0 /* is_private_region */ );
704 vec_add1 (am->mapped_shmem_regions, vlib_rp);
709 vl_register_mapped_shmem_region (svm_region_t * rp)
711 api_main_t *am = &api_main;
713 vec_add1 (am->mapped_shmem_regions, rp);
717 vl_unmap_shmem_internal (u8 is_client)
721 api_main_t *am = &api_main;
723 if (!svm_get_root_rp ())
726 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
728 rp = am->mapped_shmem_regions[i];
729 is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
732 vec_free (am->mapped_shmem_regions);
735 is_client ? svm_region_exit_client () : svm_region_exit ();
737 /* $$$ more careful cleanup, valgrind run... */
738 vec_free (am->msg_handlers);
739 vec_free (am->msg_endian_handlers);
740 vec_free (am->msg_print_handlers);
744 vl_unmap_shmem (void)
746 vl_unmap_shmem_internal (0);
750 vl_unmap_shmem_client (void)
752 vl_unmap_shmem_internal (1);
756 vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
758 api_main_t *am = &api_main;
759 void *msg = (void *) *(uword *) elem;
761 if (am->tx_trace && am->tx_trace->enabled)
762 vl_msg_api_trace (am, am->tx_trace, msg);
765 * Announce a probable binary API client bug:
766 * some client's input queue is stuffed.
767 * The situation may be recoverable, or not.
770 (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize)))
772 if (PREDICT_FALSE (am->elog_trace_api_messages))
775 ELOG_TYPE_DECLARE (e) =
777 .format = "api-client-queue-stuffed: %x%x",
778 .format_args = "i4i4",
785 ed = ELOG_DATA (am->elog_main, e);
786 ed->hi = (uword) q >> 32;
787 ed->low = (uword) q & 0xFFFFFFFF;
788 clib_warning ("WARNING: client input queue at %llx is stuffed...",
792 VL_MSG_API_POISON (msg);
793 (void) svm_queue_add (q, elem, 0 /* nowait */ );
797 vl_mem_api_can_send (svm_queue_t * q)
799 return (q->cursize < q->maxsize);
803 vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
805 api_main_t *am = &api_main;
806 void *msg = (void *) *(uword *) elem;
808 if (am->tx_trace && am->tx_trace->enabled)
809 vl_msg_api_trace (am, am->tx_trace, msg);
811 (void) svm_queue_add_nolock (q, elem);
812 VL_MSG_API_POISON (msg);
816 * fd.io coding-style-patch-verification: ON
819 * eval: (c-set-style "gnu")