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;
121 ("garbage collect pool %d ring %d index %d", pool, i,
123 msg_idp = (u16 *) (rv->data);
124 msg_id = clib_net_to_host_u16 (*msg_idp);
125 if (msg_id < vec_len (vlibapi_get_main ()->msg_names))
126 clib_warning ("msg id %d name %s", (u32) msg_id,
127 vlibapi_get_main ()->msg_names[msg_id]);
129 shmem_hdr->garbage_collects++;
135 /* yes, loser; try next larger pool */
138 pthread_mutex_unlock (&q->mutex);
143 /* OK, we have a winner */
146 * Remember the source queue, although we
147 * don't need to know the queue to free the item.
150 rv->gc_mark_timestamp = 0;
152 if (q->head == q->maxsize)
156 pthread_mutex_unlock (&q->mutex);
161 * Request too big, or head element of all size-compatible rings
162 * still in use. Fall back to shared-memory malloc.
166 pthread_mutex_lock (&vlib_rp->mutex);
167 oldheap = svm_push_data_heap (vlib_rp);
170 rv = clib_mem_alloc_or_null (nbytes);
171 if (PREDICT_FALSE (rv == 0))
173 svm_pop_heap (oldheap);
174 pthread_mutex_unlock (&vlib_rp->mutex);
179 rv = clib_mem_alloc (nbytes);
182 rv->gc_mark_timestamp = 0;
183 svm_pop_heap (oldheap);
184 pthread_mutex_unlock (&vlib_rp->mutex);
187 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
191 overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
192 *overrun = 0x1badbabe;
195 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
197 VL_MSG_API_UNPOISON (rv->data);
202 vl_msg_api_alloc (int nbytes)
205 api_main_t *am = vlibapi_get_main ();
206 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
209 * Clients use pool-0, vlib proc uses pool 1
211 pool = (am->our_pid == shmem_hdr->vl_pid);
212 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
213 0 /* may_return_null */ );
217 vl_msg_api_alloc_zero (int nbytes)
221 ret = vl_msg_api_alloc (nbytes);
222 clib_memset (ret, 0, nbytes);
227 vl_msg_api_alloc_or_null (int nbytes)
230 api_main_t *am = vlibapi_get_main ();
231 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
233 pool = (am->our_pid == shmem_hdr->vl_pid);
234 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
235 1 /* may_return_null */ );
239 vl_msg_api_alloc_as_if_client (int nbytes)
241 api_main_t *am = vlibapi_get_main ();
242 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
243 0 /* may_return_null */ );
247 vl_msg_api_alloc_zero_as_if_client (int nbytes)
251 ret = vl_msg_api_alloc_as_if_client (nbytes);
252 clib_memset (ret, 0, nbytes);
257 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
259 api_main_t *am = vlibapi_get_main ();
260 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
261 1 /* may_return_null */ );
265 vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
267 return vl_msg_api_alloc_internal (reg->vlib_rp, nbytes, 0,
268 0 /* may_return_null */ );
272 vl_msg_api_free_w_region (svm_region_t * vlib_rp, void *a)
277 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
280 * Here's the beauty of the scheme. Only one proc/thread has
281 * control of a given message buffer. To free a buffer, we just clear the
282 * queue field, and leave. No locks, no hits, no errors...
287 rv->gc_mark_timestamp = 0;
288 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
291 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
292 ASSERT (*overrun == 0x1badbabe);
295 VL_MSG_API_POISON (rv->data);
299 pthread_mutex_lock (&vlib_rp->mutex);
300 oldheap = svm_push_data_heap (vlib_rp);
302 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
305 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
306 ASSERT (*overrun == 0x1badbabe);
311 svm_pop_heap (oldheap);
312 pthread_mutex_unlock (&vlib_rp->mutex);
316 vl_msg_api_free (void *a)
318 api_main_t *am = vlibapi_get_main ();
319 vl_msg_api_free_w_region (am->vlib_rp, a);
323 vl_msg_api_free_nolock (void *a)
327 api_main_t *am = vlibapi_get_main ();
329 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
331 * Here's the beauty of the scheme. Only one proc/thread has
332 * control of a given message buffer. To free a buffer, we just clear the
333 * queue field, and leave. No locks, no hits, no errors...
338 VL_MSG_API_POISON (rv->data);
342 oldheap = svm_push_data_heap (am->vlib_rp);
344 svm_pop_heap (oldheap);
348 vl_set_memory_root_path (const char *name)
350 api_main_t *am = vlibapi_get_main ();
352 am->root_path = name;
356 vl_set_memory_uid (int uid)
358 api_main_t *am = vlibapi_get_main ();
364 vl_set_memory_gid (int gid)
366 api_main_t *am = vlibapi_get_main ();
372 vl_set_global_memory_baseva (u64 baseva)
374 api_main_t *am = vlibapi_get_main ();
376 am->global_baseva = baseva;
380 vl_set_global_memory_size (u64 size)
382 api_main_t *am = vlibapi_get_main ();
384 am->global_size = size;
388 vl_set_api_memory_size (u64 size)
390 api_main_t *am = vlibapi_get_main ();
396 vl_set_global_pvt_heap_size (u64 size)
398 api_main_t *am = vlibapi_get_main ();
400 am->global_pvt_heap_size = size;
404 vl_set_api_pvt_heap_size (u64 size)
406 api_main_t *am = vlibapi_get_main ();
408 am->api_pvt_heap_size = size;
412 vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
414 api_main_t *am = vlibapi_get_main ();
415 u32 vlib_input_queue_length;
417 /* vlib main input queue */
418 vlib_input_queue_length = 1024;
419 if (am->vlib_input_queue_length)
420 vlib_input_queue_length = am->vlib_input_queue_length;
422 shmem_hdr->vl_input_queue =
423 svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
429 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
434 vec_add1(shmem_hdr->vl_rings, _rp); \
437 foreach_vl_aring_size;
443 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
448 vec_add1(shmem_hdr->client_rings, _rp); \
451 foreach_clnt_aring_size;
456 vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
458 vl_api_shm_elem_config_t *c;
464 vl_api_default_mem_config (hdr);
468 vec_foreach (c, config)
473 hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
476 case VL_API_VLIB_RING:
477 vec_add2 (hdr->vl_rings, rp, 1);
479 case VL_API_CLIENT_RING:
480 vec_add2 (hdr->client_rings, rp, 1);
483 clib_warning ("unknown config type: %d", c->type);
487 size = sizeof (ring_alloc_t) + c->size;
488 rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
490 rp->nitems = c->count;
497 vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
498 int is_vlib, int is_private_region)
500 api_main_t *am = vlibapi_get_main ();
501 vl_shmem_hdr_t *shmem_hdr = 0;
505 /* $$$$ need private region config parameters */
507 oldheap = svm_push_data_heap (vlib_rp);
509 vec_validate (shmem_hdr, 0);
510 shmem_hdr->version = VL_SHM_VERSION;
511 shmem_hdr->clib_file_index = VL_API_INVALID_FI;
513 /* Set up the queue and msg ring allocator */
514 vl_api_mem_config (shmem_hdr, config);
516 if (is_private_region == 0)
518 am->shmem_hdr = shmem_hdr;
519 am->vlib_rp = vlib_rp;
520 am->our_pid = getpid ();
522 am->shmem_hdr->vl_pid = am->our_pid;
525 shmem_hdr->vl_pid = am->our_pid;
527 svm_pop_heap (oldheap);
530 * After absolutely everything that a client might see is set up,
531 * declare the shmem region valid
533 vlib_rp->user_ctx = shmem_hdr;
535 pthread_mutex_unlock (&vlib_rp->mutex);
539 vl_map_shmem (const char *region_name, int is_vlib)
541 svm_map_region_args_t _a, *a = &_a;
542 svm_region_t *vlib_rp, *root_rp;
543 api_main_t *am = vlibapi_get_main ();
545 struct timespec ts, tsrem;
546 char *vpe_api_region_suffix = "-vpe-api";
548 clib_memset (a, 0, sizeof (*a));
550 if (strstr (region_name, vpe_api_region_suffix))
552 u8 *root_path = format (0, "%s", region_name);
553 _vec_len (root_path) = (vec_len (root_path) -
554 strlen (vpe_api_region_suffix));
555 vec_terminate_c_string (root_path);
556 a->root_path = (const char *) root_path;
557 am->root_path = (const char *) root_path;
565 * Clients wait for vpp to set up the root / API regioins
568 api_name = format (0, "/dev/shm/%s-%s%c", am->root_path,
571 api_name = format (0, "/dev/shm%s%c", region_name, 0);
573 /* Wait up to 100 seconds... */
574 for (i = 0; i < 10000; i++)
577 ts.tv_nsec = 10000 * 1000; /* 10 ms */
578 while (nanosleep (&ts, &tsrem) < 0)
580 tfd = open ((char *) api_name, O_RDWR);
587 clib_warning ("region init fail");
591 svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ());
594 if (a->root_path != NULL)
596 a->name = "/vpe-api";
599 a->name = region_name;
600 a->size = am->api_size ? am->api_size : (16 << 20);
601 a->flags = SVM_FLAGS_MHEAP;
602 a->uid = am->api_uid;
603 a->gid = am->api_gid;
604 a->pvt_heap_size = am->api_pvt_heap_size;
606 vlib_rp = svm_region_find_or_create (a);
611 pthread_mutex_lock (&vlib_rp->mutex);
612 /* Has someone else set up the shared-memory variable table? */
613 if (vlib_rp->user_ctx)
615 am->shmem_hdr = (void *) vlib_rp->user_ctx;
616 am->our_pid = getpid ();
622 * application restart. Reset cached pids, API message
623 * rings, list of clients; otherwise, various things
624 * fail. (e.g. queue non-empty notification)
627 /* ghosts keep the region from disappearing properly */
628 svm_client_scan_this_region_nolock (vlib_rp);
629 am->shmem_hdr->application_restarts++;
630 q = am->shmem_hdr->vl_input_queue;
631 am->shmem_hdr->vl_pid = getpid ();
632 q->consumer_pid = am->shmem_hdr->vl_pid;
633 /* Drain the input queue, freeing msgs */
634 for (i = 0; i < 10; i++)
636 if (pthread_mutex_trylock (&q->mutex) == 0)
638 pthread_mutex_unlock (&q->mutex);
642 ts.tv_nsec = 10000 * 1000; /* 10 ms */
643 while (nanosleep (&ts, &tsrem) < 0)
646 /* Mutex buggered, "fix" it */
647 clib_memset (&q->mutex, 0, sizeof (q->mutex));
648 clib_warning ("forcibly release main input queue mutex");
651 am->vlib_rp = vlib_rp;
652 while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
653 != -2 /* queue underflow */ )
655 vl_msg_api_free_nolock ((void *) old_msg);
656 am->shmem_hdr->restart_reclaims++;
658 pthread_mutex_unlock (&vlib_rp->mutex);
659 root_rp = svm_get_root_rp ();
661 /* Clean up the root region client list */
662 pthread_mutex_lock (&root_rp->mutex);
663 svm_client_scan_this_region_nolock (root_rp);
664 pthread_mutex_unlock (&root_rp->mutex);
668 pthread_mutex_unlock (&vlib_rp->mutex);
670 am->vlib_rp = vlib_rp;
671 vec_add1 (am->mapped_shmem_regions, vlib_rp);
674 /* Clients simply have to wait... */
677 pthread_mutex_unlock (&vlib_rp->mutex);
679 /* Wait up to 100 seconds... */
680 for (i = 0; i < 10000; i++)
683 ts.tv_nsec = 10000 * 1000; /* 10 ms */
684 while (nanosleep (&ts, &tsrem) < 0)
686 if (vlib_rp->user_ctx)
689 /* Clean up and leave... */
690 svm_region_unmap (vlib_rp);
691 clib_warning ("region init fail");
695 am->shmem_hdr = (void *) vlib_rp->user_ctx;
696 am->our_pid = getpid ();
697 am->vlib_rp = vlib_rp;
698 vec_add1 (am->mapped_shmem_regions, vlib_rp);
702 /* Nope, it's our problem... */
703 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
704 0 /* is_private_region */ );
706 vec_add1 (am->mapped_shmem_regions, vlib_rp);
711 vl_register_mapped_shmem_region (svm_region_t * rp)
713 api_main_t *am = vlibapi_get_main ();
715 vec_add1 (am->mapped_shmem_regions, rp);
719 vl_unmap_shmem_internal (u8 is_client)
723 api_main_t *am = vlibapi_get_main ();
725 if (!svm_get_root_rp ())
728 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
730 rp = am->mapped_shmem_regions[i];
731 is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
734 vec_free (am->mapped_shmem_regions);
737 is_client ? svm_region_exit_client () : svm_region_exit ();
739 /* $$$ more careful cleanup, valgrind run... */
740 vec_free (am->msg_handlers);
741 vec_free (am->msg_endian_handlers);
742 vec_free (am->msg_print_handlers);
746 vl_unmap_shmem (void)
748 vl_unmap_shmem_internal (0);
752 vl_unmap_shmem_client (void)
754 vl_unmap_shmem_internal (1);
758 vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
760 api_main_t *am = vlibapi_get_main ();
761 void *msg = (void *) *(uword *) elem;
763 if (am->tx_trace && am->tx_trace->enabled)
764 vl_msg_api_trace (am, am->tx_trace, msg);
767 * Announce a probable binary API client bug:
768 * some client's input queue is stuffed.
769 * The situation may be recoverable, or not.
772 (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize)))
774 if (PREDICT_FALSE (am->elog_trace_api_messages))
777 ELOG_TYPE_DECLARE (e) =
779 .format = "api-client-queue-stuffed: %x%x",
780 .format_args = "i4i4",
787 ed = ELOG_DATA (am->elog_main, e);
788 ed->hi = (uword) q >> 32;
789 ed->low = (uword) q & 0xFFFFFFFF;
790 clib_warning ("WARNING: client input queue at %llx is stuffed...",
794 VL_MSG_API_POISON (msg);
795 (void) svm_queue_add (q, elem, 0 /* nowait */ );
799 vl_mem_api_can_send (svm_queue_t * q)
801 return (q->cursize < q->maxsize);
805 vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
807 api_main_t *am = vlibapi_get_main ();
808 void *msg = (void *) *(uword *) elem;
810 if (am->tx_trace && am->tx_trace->enabled)
811 vl_msg_api_trace (am, am->tx_trace, msg);
813 (void) svm_queue_add_nolock (q, elem);
814 VL_MSG_API_POISON (msg);
818 * fd.io coding-style-patch-verification: ON
821 * eval: (c-set-style "gnu")