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>
38 #include <vlibapi/api_common.h>
41 #include <vlibmemory/vl_memory_api_h.h>
44 #define DEBUG_MESSAGE_BUFFER_OVERRUN 0
46 __clib_nosanitize_addr static inline void *
47 vl_msg_api_alloc_internal (svm_region_t *vlib_rp, int nbytes, int pool,
55 vl_shmem_hdr_t *shmem_hdr;
56 api_main_t *am = vlibapi_get_main ();
58 shmem_hdr = (void *) vlib_rp->user_ctx;
60 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
64 ASSERT (pool == 0 || vlib_get_thread_index () == 0);
68 clib_warning ("shared memory header NULL");
72 /* account for the msgbuf_t header */
73 nbytes += sizeof (msgbuf_t);
75 if (shmem_hdr->vl_rings == 0)
77 clib_warning ("vl_rings NULL");
82 if (shmem_hdr->client_rings == 0)
84 clib_warning ("client_rings NULL");
89 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
90 for (i = 0; i < vec_len (ap); i++)
93 if (nbytes > ap[i].size)
101 pthread_mutex_lock (&q->mutex);
103 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
105 * Is this item still in use?
109 u32 now = (u32) time (0);
111 if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
112 rv->gc_mark_timestamp = now;
115 if (now - rv->gc_mark_timestamp > 10)
119 u16 *msg_idp, msg_id;
120 vl_api_msg_data_t *m;
122 ("garbage collect pool %d ring %d index %d", pool, i,
124 msg_idp = (u16 *) (rv->data);
125 msg_id = clib_net_to_host_u16 (*msg_idp);
126 m = vl_api_get_msg_data (am, msg_id);
128 clib_warning ("msg id %d name %s", (u32) msg_id,
131 shmem_hdr->garbage_collects++;
137 /* yes, loser; try next larger pool */
140 pthread_mutex_unlock (&q->mutex);
145 /* OK, we have a winner */
148 * Remember the source queue, although we
149 * don't need to know the queue to free the item.
152 rv->gc_mark_timestamp = 0;
154 if (q->head == q->maxsize)
158 pthread_mutex_unlock (&q->mutex);
163 * Request too big, or head element of all size-compatible rings
164 * still in use. Fall back to shared-memory malloc.
168 oldheap = vl_msg_push_heap_w_region (vlib_rp);
171 rv = clib_mem_alloc_or_null (nbytes);
172 if (PREDICT_FALSE (rv == 0))
174 vl_msg_pop_heap_w_region (vlib_rp, oldheap);
179 rv = clib_mem_alloc (nbytes);
182 rv->gc_mark_timestamp = 0;
183 vl_msg_pop_heap_w_region (vlib_rp, oldheap);
186 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
190 overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
191 *overrun = 0x1badbabe;
194 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
196 VL_MSG_API_UNPOISON (rv->data);
201 vl_msg_api_alloc (int nbytes)
204 api_main_t *am = vlibapi_get_main ();
205 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
208 * Clients use pool-0, vlib proc uses pool 1
210 pool = (am->our_pid == shmem_hdr->vl_pid);
211 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
212 0 /* may_return_null */ );
216 vl_msg_api_alloc_zero (int nbytes)
220 ret = vl_msg_api_alloc (nbytes);
221 clib_memset (ret, 0, nbytes);
226 vl_msg_api_alloc_or_null (int nbytes)
229 api_main_t *am = vlibapi_get_main ();
230 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
232 pool = (am->our_pid == shmem_hdr->vl_pid);
233 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
234 1 /* may_return_null */ );
238 vl_msg_api_alloc_as_if_client (int nbytes)
240 api_main_t *am = vlibapi_get_main ();
241 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
242 0 /* may_return_null */ );
246 vl_msg_api_alloc_zero_as_if_client (int nbytes)
250 ret = vl_msg_api_alloc_as_if_client (nbytes);
251 clib_memset (ret, 0, nbytes);
256 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
258 api_main_t *am = vlibapi_get_main ();
259 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
260 1 /* may_return_null */ );
264 vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
266 return vl_msg_api_alloc_internal (reg->vlib_rp, nbytes, 0,
267 0 /* may_return_null */ );
271 vl_msg_api_free_w_region (svm_region_t * vlib_rp, void *a)
276 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
279 * Here's the beauty of the scheme. Only one proc/thread has
280 * control of a given message buffer. To free a buffer, we just clear the
281 * queue field, and leave. No locks, no hits, no errors...
286 rv->gc_mark_timestamp = 0;
287 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
290 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
291 ASSERT (*overrun == 0x1badbabe);
294 VL_MSG_API_POISON (rv->data);
298 oldheap = vl_msg_push_heap_w_region (vlib_rp);
300 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
303 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
304 ASSERT (*overrun == 0x1badbabe);
309 vl_msg_pop_heap_w_region (vlib_rp, oldheap);
313 vl_msg_api_free (void *a)
315 api_main_t *am = vlibapi_get_main ();
316 vl_msg_api_free_w_region (am->vlib_rp, a);
320 vl_msg_api_free_nolock (void *a)
324 api_main_t *am = vlibapi_get_main ();
326 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
328 * Here's the beauty of the scheme. Only one proc/thread has
329 * control of a given message buffer. To free a buffer, we just clear the
330 * queue field, and leave. No locks, no hits, no errors...
335 VL_MSG_API_POISON (rv->data);
339 oldheap = svm_push_data_heap (am->vlib_rp);
341 svm_pop_heap (oldheap);
345 vl_set_memory_root_path (const char *name)
347 api_main_t *am = vlibapi_get_main ();
349 am->root_path = name;
353 vl_set_memory_uid (int uid)
355 api_main_t *am = vlibapi_get_main ();
361 vl_set_memory_gid (int gid)
363 api_main_t *am = vlibapi_get_main ();
369 vl_set_global_memory_baseva (u64 baseva)
371 api_main_t *am = vlibapi_get_main ();
373 am->global_baseva = baseva;
377 vl_set_global_memory_size (u64 size)
379 api_main_t *am = vlibapi_get_main ();
381 am->global_size = size;
385 vl_set_api_memory_size (u64 size)
387 api_main_t *am = vlibapi_get_main ();
393 vl_set_global_pvt_heap_size (u64 size)
395 api_main_t *am = vlibapi_get_main ();
397 am->global_pvt_heap_size = size;
401 vl_set_api_pvt_heap_size (u64 size)
403 api_main_t *am = vlibapi_get_main ();
405 am->api_pvt_heap_size = size;
409 vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
411 api_main_t *am = vlibapi_get_main ();
412 u32 vlib_input_queue_length;
414 /* vlib main input queue */
415 vlib_input_queue_length = 1024;
416 if (am->vlib_input_queue_length)
417 vlib_input_queue_length = am->vlib_input_queue_length;
419 shmem_hdr->vl_input_queue =
420 svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
426 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
431 vec_add1(shmem_hdr->vl_rings, _rp); \
434 foreach_vl_aring_size;
440 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
445 vec_add1(shmem_hdr->client_rings, _rp); \
448 foreach_clnt_aring_size;
453 vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
455 vl_api_shm_elem_config_t *c;
461 vl_api_default_mem_config (hdr);
465 vec_foreach (c, config)
470 hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
473 case VL_API_VLIB_RING:
474 vec_add2 (hdr->vl_rings, rp, 1);
476 case VL_API_CLIENT_RING:
477 vec_add2 (hdr->client_rings, rp, 1);
480 clib_warning ("unknown config type: %d", c->type);
484 size = sizeof (ring_alloc_t) + c->size;
485 rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
487 rp->nitems = c->count;
494 vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
495 int is_vlib, int is_private_region)
497 api_main_t *am = vlibapi_get_main ();
498 vl_shmem_hdr_t *shmem_hdr = 0;
502 /* $$$$ need private region config parameters */
504 oldheap = svm_push_data_heap (vlib_rp);
506 vec_validate (shmem_hdr, 0);
507 shmem_hdr->version = VL_SHM_VERSION;
508 shmem_hdr->clib_file_index = VL_API_INVALID_FI;
510 /* Set up the queue and msg ring allocator */
511 vl_api_mem_config (shmem_hdr, config);
513 if (is_private_region == 0)
515 am->shmem_hdr = shmem_hdr;
516 am->vlib_rp = vlib_rp;
517 am->our_pid = getpid ();
519 am->shmem_hdr->vl_pid = am->our_pid;
522 shmem_hdr->vl_pid = am->our_pid;
524 svm_pop_heap (oldheap);
527 * After absolutely everything that a client might see is set up,
528 * declare the shmem region valid
530 vlib_rp->user_ctx = shmem_hdr;
532 pthread_mutex_unlock (&vlib_rp->mutex);
536 vl_map_shmem (const char *region_name, int is_vlib)
538 svm_map_region_args_t _a, *a = &_a;
539 svm_region_t *vlib_rp, *root_rp;
540 api_main_t *am = vlibapi_get_main ();
542 struct timespec ts, tsrem;
543 char *vpe_api_region_suffix = "-vpe-api";
545 clib_memset (a, 0, sizeof (*a));
547 if (strstr (region_name, vpe_api_region_suffix))
549 u8 *root_path = format (0, "%s", region_name);
550 vec_set_len (root_path,
551 vec_len (root_path) - strlen (vpe_api_region_suffix));
552 vec_terminate_c_string (root_path);
553 a->root_path = (const char *) root_path;
554 am->root_path = (const char *) root_path;
562 * Clients wait for vpp to set up the root / API regioins
565 api_name = format (0, "/dev/shm/%s-%s%c", am->root_path,
568 api_name = format (0, "/dev/shm%s%c", region_name, 0);
570 /* Wait up to 100 seconds... */
571 for (i = 0; i < 10000; i++)
574 ts.tv_nsec = 10000 * 1000; /* 10 ms */
575 while (nanosleep (&ts, &tsrem) < 0)
577 tfd = open ((char *) api_name, O_RDWR);
584 clib_warning ("region init fail");
588 svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ());
591 if (a->root_path != NULL)
593 a->name = "/vpe-api";
596 a->name = region_name;
597 a->size = am->api_size ? am->api_size : (16 << 20);
598 a->flags = SVM_FLAGS_MHEAP;
599 a->uid = am->api_uid;
600 a->gid = am->api_gid;
601 a->pvt_heap_size = am->api_pvt_heap_size;
603 vlib_rp = svm_region_find_or_create (a);
608 pthread_mutex_lock (&vlib_rp->mutex);
609 /* Has someone else set up the shared-memory variable table? */
610 if (vlib_rp->user_ctx)
612 am->shmem_hdr = (void *) vlib_rp->user_ctx;
613 am->our_pid = getpid ();
619 * application restart. Reset cached pids, API message
620 * rings, list of clients; otherwise, various things
621 * fail. (e.g. queue non-empty notification)
624 /* ghosts keep the region from disappearing properly */
625 svm_client_scan_this_region_nolock (vlib_rp);
626 am->shmem_hdr->application_restarts++;
627 q = am->shmem_hdr->vl_input_queue;
628 am->shmem_hdr->vl_pid = getpid ();
629 q->consumer_pid = am->shmem_hdr->vl_pid;
630 /* Drain the input queue, freeing msgs */
631 for (i = 0; i < 10; i++)
633 if (pthread_mutex_trylock (&q->mutex) == 0)
635 pthread_mutex_unlock (&q->mutex);
639 ts.tv_nsec = 10000 * 1000; /* 10 ms */
640 while (nanosleep (&ts, &tsrem) < 0)
643 /* Mutex buggered, "fix" it */
644 clib_memset (&q->mutex, 0, sizeof (q->mutex));
645 clib_warning ("forcibly release main input queue mutex");
648 am->vlib_rp = vlib_rp;
649 while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
650 != -2 /* queue underflow */ )
652 vl_msg_api_free_nolock ((void *) old_msg);
653 am->shmem_hdr->restart_reclaims++;
655 pthread_mutex_unlock (&vlib_rp->mutex);
656 root_rp = svm_get_root_rp ();
658 /* Clean up the root region client list */
659 pthread_mutex_lock (&root_rp->mutex);
660 svm_client_scan_this_region_nolock (root_rp);
661 pthread_mutex_unlock (&root_rp->mutex);
665 pthread_mutex_unlock (&vlib_rp->mutex);
667 am->vlib_rp = vlib_rp;
668 vec_add1 (am->mapped_shmem_regions, vlib_rp);
671 /* Clients simply have to wait... */
674 pthread_mutex_unlock (&vlib_rp->mutex);
676 /* Wait up to 100 seconds... */
677 for (i = 0; i < 10000; i++)
680 ts.tv_nsec = 10000 * 1000; /* 10 ms */
681 while (nanosleep (&ts, &tsrem) < 0)
683 if (vlib_rp->user_ctx)
686 /* Clean up and leave... */
687 svm_region_unmap (vlib_rp);
688 clib_warning ("region init fail");
692 am->shmem_hdr = (void *) vlib_rp->user_ctx;
693 am->our_pid = getpid ();
694 am->vlib_rp = vlib_rp;
695 vec_add1 (am->mapped_shmem_regions, vlib_rp);
699 /* Nope, it's our problem... */
700 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
701 0 /* is_private_region */ );
703 vec_add1 (am->mapped_shmem_regions, vlib_rp);
708 vl_register_mapped_shmem_region (svm_region_t * rp)
710 api_main_t *am = vlibapi_get_main ();
712 vec_add1 (am->mapped_shmem_regions, rp);
716 vl_unmap_shmem_internal (u8 is_client)
720 api_main_t *am = vlibapi_get_main ();
722 if (!svm_get_root_rp ())
725 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
727 rp = am->mapped_shmem_regions[i];
728 is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
731 vec_free (am->mapped_shmem_regions);
734 is_client ? svm_region_exit_client () : svm_region_exit ();
736 vec_free (am->msg_data);
740 vl_unmap_shmem (void)
742 vl_unmap_shmem_internal (0);
746 vl_unmap_shmem_client (void)
748 vl_unmap_shmem_internal (1);
752 vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
754 api_main_t *am = vlibapi_get_main ();
755 void *msg = (void *) *(uword *) elem;
757 if (am->tx_trace && am->tx_trace->enabled)
758 vl_msg_api_trace (am, am->tx_trace, msg);
761 * Announce a probable binary API client bug:
762 * some client's input queue is stuffed.
763 * The situation may be recoverable, or not.
766 (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize)))
768 if (PREDICT_FALSE (am->elog_trace_api_messages))
771 ELOG_TYPE_DECLARE (e) =
773 .format = "api-client-queue-stuffed: %x%x",
774 .format_args = "i4i4",
781 ed = ELOG_DATA (am->elog_main, e);
782 ed->hi = (uword) q >> 32;
783 ed->low = (uword) q & 0xFFFFFFFF;
784 clib_warning ("WARNING: client input queue at %llx is stuffed...",
788 VL_MSG_API_POISON (msg);
789 (void) svm_queue_add (q, elem, 0 /* nowait */ );
793 vl_mem_api_can_send (svm_queue_t * q)
795 return (q->cursize < q->maxsize);
799 vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
801 api_main_t *am = vlibapi_get_main ();
802 void *msg = (void *) *(uword *) elem;
804 if (am->tx_trace && am->tx_trace->enabled)
805 vl_msg_api_trace (am, am->tx_trace, msg);
807 (void) svm_queue_add_nolock (q, elem);
808 VL_MSG_API_POISON (msg);
812 * fd.io coding-style-patch-verification: ON
815 * eval: (c-set-style "gnu")