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 <svm/queue.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 #include <vlibmemory/memory_api.h>
36 #include <vlibmemory/vl_memory_msg_enum.h>
39 #include <vlibmemory/vl_memory_api_h.h>
42 #define DEBUG_MESSAGE_BUFFER_OVERRUN 0
45 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
52 vl_shmem_hdr_t *shmem_hdr;
53 api_main_t *am = &api_main;
55 shmem_hdr = am->shmem_hdr;
57 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
61 ASSERT (pool == 0 || vlib_get_thread_index () == 0);
65 clib_warning ("shared memory header NULL");
69 /* account for the msgbuf_t header */
70 nbytes += sizeof (msgbuf_t);
72 if (shmem_hdr->vl_rings == 0)
74 clib_warning ("vl_rings NULL");
79 if (shmem_hdr->client_rings == 0)
81 clib_warning ("client_rings NULL");
86 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
87 for (i = 0; i < vec_len (ap); i++)
90 if (nbytes > ap[i].size)
98 pthread_mutex_lock (&q->mutex);
100 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
102 * Is this item still in use?
106 u32 now = (u32) time (0);
108 if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
109 rv->gc_mark_timestamp = now;
112 if (now - rv->gc_mark_timestamp > 10)
116 u16 *msg_idp, msg_id;
118 ("garbage collect pool %d ring %d index %d", pool, i,
120 msg_idp = (u16 *) (rv->data);
121 msg_id = clib_net_to_host_u16 (*msg_idp);
122 if (msg_id < vec_len (api_main.msg_names))
123 clib_warning ("msg id %d name %s", (u32) msg_id,
124 api_main.msg_names[msg_id]);
126 shmem_hdr->garbage_collects++;
132 /* yes, loser; try next larger pool */
135 pthread_mutex_unlock (&q->mutex);
140 /* OK, we have a winner */
143 * Remember the source queue, although we
144 * don't need to know the queue to free the item.
147 rv->gc_mark_timestamp = 0;
149 if (q->head == q->maxsize)
153 pthread_mutex_unlock (&q->mutex);
158 * Request too big, or head element of all size-compatible rings
159 * still in use. Fall back to shared-memory malloc.
163 pthread_mutex_lock (&am->vlib_rp->mutex);
164 oldheap = svm_push_data_heap (am->vlib_rp);
167 rv = clib_mem_alloc_or_null (nbytes);
168 if (PREDICT_FALSE (rv == 0))
170 svm_pop_heap (oldheap);
171 pthread_mutex_unlock (&am->vlib_rp->mutex);
176 rv = clib_mem_alloc (nbytes);
179 svm_pop_heap (oldheap);
180 pthread_mutex_unlock (&am->vlib_rp->mutex);
183 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
187 overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
188 *overrun = 0x1badbabe;
191 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
197 vl_msg_api_alloc (int nbytes)
200 api_main_t *am = &api_main;
201 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
204 * Clients use pool-0, vlib proc uses pool 1
206 pool = (am->our_pid == shmem_hdr->vl_pid);
207 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
211 vl_msg_api_alloc_or_null (int nbytes)
214 api_main_t *am = &api_main;
215 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
217 pool = (am->our_pid == shmem_hdr->vl_pid);
218 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
222 vl_msg_api_alloc_as_if_client (int nbytes)
224 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
228 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
230 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
234 vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
236 api_main_t *am = &api_main;
237 vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
238 svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
241 vlib_rp = am->vlib_rp = reg->vlib_rp;
242 am->shmem_hdr = (void *) vlib_rp->user_ctx;
244 msg = vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
246 am->shmem_hdr = save_shmem_hdr;
247 am->vlib_rp = save_vlib_rp;
253 vl_msg_api_free (void *a)
257 api_main_t *am = &api_main;
259 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
262 * Here's the beauty of the scheme. Only one proc/thread has
263 * control of a given message buffer. To free a buffer, we just clear the
264 * queue field, and leave. No locks, no hits, no errors...
269 rv->gc_mark_timestamp = 0;
270 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
273 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
274 ASSERT (*overrun == 0x1badbabe);
280 pthread_mutex_lock (&am->vlib_rp->mutex);
281 oldheap = svm_push_data_heap (am->vlib_rp);
283 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
286 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
287 ASSERT (*overrun == 0x1badbabe);
292 svm_pop_heap (oldheap);
293 pthread_mutex_unlock (&am->vlib_rp->mutex);
297 vl_msg_api_free_nolock (void *a)
301 api_main_t *am = &api_main;
303 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
305 * Here's the beauty of the scheme. Only one proc/thread has
306 * control of a given message buffer. To free a buffer, we just clear the
307 * queue field, and leave. No locks, no hits, no errors...
315 oldheap = svm_push_data_heap (am->vlib_rp);
317 svm_pop_heap (oldheap);
321 vl_set_memory_root_path (const char *name)
323 api_main_t *am = &api_main;
325 am->root_path = name;
329 vl_set_memory_uid (int uid)
331 api_main_t *am = &api_main;
337 vl_set_memory_gid (int gid)
339 api_main_t *am = &api_main;
345 vl_set_global_memory_baseva (u64 baseva)
347 api_main_t *am = &api_main;
349 am->global_baseva = baseva;
353 vl_set_global_memory_size (u64 size)
355 api_main_t *am = &api_main;
357 am->global_size = size;
361 vl_set_api_memory_size (u64 size)
363 api_main_t *am = &api_main;
369 vl_set_global_pvt_heap_size (u64 size)
371 api_main_t *am = &api_main;
373 am->global_pvt_heap_size = size;
377 vl_set_api_pvt_heap_size (u64 size)
379 api_main_t *am = &api_main;
381 am->api_pvt_heap_size = size;
385 vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
387 api_main_t *am = &api_main;
388 u32 vlib_input_queue_length;
390 /* vlib main input queue */
391 vlib_input_queue_length = 1024;
392 if (am->vlib_input_queue_length)
393 vlib_input_queue_length = am->vlib_input_queue_length;
395 shmem_hdr->vl_input_queue =
396 svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
402 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
407 vec_add1(shmem_hdr->vl_rings, _rp); \
410 foreach_vl_aring_size;
416 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
421 vec_add1(shmem_hdr->client_rings, _rp); \
424 foreach_clnt_aring_size;
429 vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
431 vl_api_shm_elem_config_t *c;
437 vl_api_default_mem_config (hdr);
441 vec_foreach (c, config)
446 hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
449 case VL_API_VLIB_RING:
450 vec_add2 (hdr->vl_rings, rp, 1);
452 case VL_API_CLIENT_RING:
453 vec_add2 (hdr->client_rings, rp, 1);
456 clib_warning ("unknown config type: %d", c->type);
460 size = sizeof (ring_alloc_t) + c->size;
461 rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
463 rp->nitems = c->count;
470 vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
471 int is_vlib, int is_private_region)
473 api_main_t *am = &api_main;
474 vl_shmem_hdr_t *shmem_hdr = 0;
478 /* $$$$ need private region config parameters */
480 oldheap = svm_push_data_heap (vlib_rp);
482 vec_validate (shmem_hdr, 0);
483 shmem_hdr->version = VL_SHM_VERSION;
484 shmem_hdr->clib_file_index = VL_API_INVALID_FI;
486 /* Set up the queue and msg ring allocator */
487 vl_api_mem_config (shmem_hdr, config);
489 if (is_private_region == 0)
491 am->shmem_hdr = shmem_hdr;
492 am->vlib_rp = vlib_rp;
493 am->our_pid = getpid ();
495 am->shmem_hdr->vl_pid = am->our_pid;
498 shmem_hdr->vl_pid = am->our_pid;
500 svm_pop_heap (oldheap);
503 * After absolutely everything that a client might see is set up,
504 * declare the shmem region valid
506 vlib_rp->user_ctx = shmem_hdr;
508 pthread_mutex_unlock (&vlib_rp->mutex);
512 vl_map_shmem (const char *region_name, int is_vlib)
514 svm_map_region_args_t _a, *a = &_a;
515 svm_region_t *vlib_rp, *root_rp;
516 api_main_t *am = &api_main;
518 struct timespec ts, tsrem;
519 char *vpe_api_region_suffix = "-vpe-api";
521 clib_memset (a, 0, sizeof (*a));
523 if (strstr (region_name, vpe_api_region_suffix))
525 u8 *root_path = format (0, "%s", region_name);
526 _vec_len (root_path) = (vec_len (root_path) -
527 strlen (vpe_api_region_suffix));
528 vec_terminate_c_string (root_path);
529 a->root_path = (const char *) root_path;
530 am->root_path = (const char *) root_path;
535 rv = svm_region_init_chroot (am->root_path);
540 if (a->root_path != NULL)
542 a->name = "/vpe-api";
545 a->name = region_name;
546 a->size = am->api_size ? am->api_size : (16 << 20);
547 a->flags = SVM_FLAGS_MHEAP;
548 a->uid = am->api_uid;
549 a->gid = am->api_gid;
550 a->pvt_heap_size = am->api_pvt_heap_size;
552 vlib_rp = svm_region_find_or_create (a);
557 pthread_mutex_lock (&vlib_rp->mutex);
558 /* Has someone else set up the shared-memory variable table? */
559 if (vlib_rp->user_ctx)
561 am->shmem_hdr = (void *) vlib_rp->user_ctx;
562 am->our_pid = getpid ();
568 * application restart. Reset cached pids, API message
569 * rings, list of clients; otherwise, various things
570 * fail. (e.g. queue non-empty notification)
573 /* ghosts keep the region from disappearing properly */
574 svm_client_scan_this_region_nolock (vlib_rp);
575 am->shmem_hdr->application_restarts++;
576 q = am->shmem_hdr->vl_input_queue;
577 am->shmem_hdr->vl_pid = getpid ();
578 q->consumer_pid = am->shmem_hdr->vl_pid;
579 /* Drain the input queue, freeing msgs */
580 for (i = 0; i < 10; i++)
582 if (pthread_mutex_trylock (&q->mutex) == 0)
584 pthread_mutex_unlock (&q->mutex);
588 ts.tv_nsec = 10000 * 1000; /* 10 ms */
589 while (nanosleep (&ts, &tsrem) < 0)
592 /* Mutex buggered, "fix" it */
593 clib_memset (&q->mutex, 0, sizeof (q->mutex));
594 clib_warning ("forcibly release main input queue mutex");
597 am->vlib_rp = vlib_rp;
598 while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
599 != -2 /* queue underflow */ )
601 vl_msg_api_free_nolock ((void *) old_msg);
602 am->shmem_hdr->restart_reclaims++;
604 pthread_mutex_unlock (&vlib_rp->mutex);
605 root_rp = svm_get_root_rp ();
607 /* Clean up the root region client list */
608 pthread_mutex_lock (&root_rp->mutex);
609 svm_client_scan_this_region_nolock (root_rp);
610 pthread_mutex_unlock (&root_rp->mutex);
614 pthread_mutex_unlock (&vlib_rp->mutex);
616 am->vlib_rp = vlib_rp;
617 vec_add1 (am->mapped_shmem_regions, vlib_rp);
620 /* Clients simply have to wait... */
623 pthread_mutex_unlock (&vlib_rp->mutex);
625 /* Wait up to 100 seconds... */
626 for (i = 0; i < 10000; i++)
629 ts.tv_nsec = 10000 * 1000; /* 10 ms */
630 while (nanosleep (&ts, &tsrem) < 0)
632 if (vlib_rp->user_ctx)
635 /* Clean up and leave... */
636 svm_region_unmap (vlib_rp);
637 clib_warning ("region init fail");
641 am->shmem_hdr = (void *) vlib_rp->user_ctx;
642 am->our_pid = getpid ();
643 am->vlib_rp = vlib_rp;
644 vec_add1 (am->mapped_shmem_regions, vlib_rp);
648 /* Nope, it's our problem... */
649 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
650 0 /* is_private_region */ );
652 vec_add1 (am->mapped_shmem_regions, vlib_rp);
657 vl_register_mapped_shmem_region (svm_region_t * rp)
659 api_main_t *am = &api_main;
661 vec_add1 (am->mapped_shmem_regions, rp);
665 vl_unmap_shmem_internal (u8 is_client)
669 api_main_t *am = &api_main;
671 if (!svm_get_root_rp ())
674 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
676 rp = am->mapped_shmem_regions[i];
677 is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
680 vec_free (am->mapped_shmem_regions);
683 is_client ? svm_region_exit_client () : svm_region_exit ();
685 /* $$$ more careful cleanup, valgrind run... */
686 vec_free (am->msg_handlers);
687 vec_free (am->msg_endian_handlers);
688 vec_free (am->msg_print_handlers);
692 vl_unmap_shmem (void)
694 vl_unmap_shmem_internal (0);
698 vl_unmap_shmem_client (void)
700 vl_unmap_shmem_internal (1);
704 vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
706 api_main_t *am = &api_main;
707 uword *trace = (uword *) elem;
709 if (am->tx_trace && am->tx_trace->enabled)
710 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
713 * Announce a probable binary API client bug:
714 * some client's input queue is stuffed.
715 * The situation may be recoverable, or not.
718 (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize)))
719 clib_warning ("WARNING: client input queue at %llx is stuffed...", q);
720 (void) svm_queue_add (q, elem, 0 /* nowait */ );
724 vl_mem_api_can_send (svm_queue_t * q)
726 return (q->cursize < q->maxsize);
730 vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
732 api_main_t *am = &api_main;
733 uword *trace = (uword *) elem;
735 if (am->tx_trace && am->tx_trace->enabled)
736 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
738 (void) svm_queue_add_nolock (q, elem);
742 * fd.io coding-style-patch-verification: ON
745 * eval: (c-set-style "gnu")