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 *------------------------------------------------------------------
28 #include <vppinfra/format.h>
29 #include <vppinfra/byte_order.h>
30 #include <vppinfra/error.h>
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33 #include <vlibmemory/api.h>
34 #include <vlibmemory/unix_shared_memory_queue.h>
36 #include <vlibmemory/vl_memory_msg_enum.h>
39 #include <vlibmemory/vl_memory_api_h.h>
42 socket_main_t socket_main;
44 #define DEBUG_MESSAGE_BUFFER_OVERRUN 0
47 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
52 unix_shared_memory_queue_t *q;
54 vl_shmem_hdr_t *shmem_hdr;
55 api_main_t *am = &api_main;
57 shmem_hdr = am->shmem_hdr;
59 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
63 ASSERT (pool == 0 || vlib_get_thread_index () == 0);
67 clib_warning ("shared memory header NULL");
71 /* account for the msgbuf_t header */
72 nbytes += sizeof (msgbuf_t);
74 if (shmem_hdr->vl_rings == 0)
76 clib_warning ("vl_rings NULL");
81 if (shmem_hdr->client_rings == 0)
83 clib_warning ("client_rings NULL");
88 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
89 for (i = 0; i < vec_len (ap); i++)
92 if (nbytes > ap[i].size)
100 pthread_mutex_lock (&q->mutex);
102 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
104 * Is this item still in use?
108 u32 now = (u32) time (0);
110 if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
111 rv->gc_mark_timestamp = now;
114 if (now - rv->gc_mark_timestamp > 10)
118 u16 *msg_idp, msg_id;
120 ("garbage collect pool %d ring %d index %d", pool, i,
122 msg_idp = (u16 *) (rv->data);
123 msg_id = clib_net_to_host_u16 (*msg_idp);
124 if (msg_id < vec_len (api_main.msg_names))
125 clib_warning ("msg id %d name %s", (u32) msg_id,
126 api_main.msg_names[msg_id]);
128 shmem_hdr->garbage_collects++;
134 /* yes, loser; try next larger pool */
137 pthread_mutex_unlock (&q->mutex);
142 /* OK, we have a winner */
145 * Remember the source queue, although we
146 * don't need to know the queue to free the item.
149 rv->gc_mark_timestamp = 0;
151 if (q->head == q->maxsize)
155 pthread_mutex_unlock (&q->mutex);
160 * Request too big, or head element of all size-compatible rings
161 * still in use. Fall back to shared-memory malloc.
165 pthread_mutex_lock (&am->vlib_rp->mutex);
166 oldheap = svm_push_data_heap (am->vlib_rp);
169 rv = clib_mem_alloc_or_null (nbytes);
170 if (PREDICT_FALSE (rv == 0))
172 svm_pop_heap (oldheap);
173 pthread_mutex_unlock (&am->vlib_rp->mutex);
178 rv = clib_mem_alloc (nbytes);
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));
199 vl_msg_api_alloc (int nbytes)
202 api_main_t *am = &api_main;
203 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
206 * Clients use pool-0, vlib proc uses pool 1
208 pool = (am->our_pid == shmem_hdr->vl_pid);
209 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
213 vl_msg_api_alloc_or_null (int nbytes)
216 api_main_t *am = &api_main;
217 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
219 pool = (am->our_pid == shmem_hdr->vl_pid);
220 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
224 vl_msg_api_alloc_as_if_client (int nbytes)
226 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
230 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
232 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
236 vl_msg_api_free (void *a)
240 api_main_t *am = &api_main;
242 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
245 * Here's the beauty of the scheme. Only one proc/thread has
246 * control of a given message buffer. To free a buffer, we just clear the
247 * queue field, and leave. No locks, no hits, no errors...
252 rv->gc_mark_timestamp = 0;
253 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
256 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
257 ASSERT (*overrun == 0x1badbabe);
263 pthread_mutex_lock (&am->vlib_rp->mutex);
264 oldheap = svm_push_data_heap (am->vlib_rp);
266 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
269 overrun = (u32 *) (rv->data + ntohl (rv->data_len));
270 ASSERT (*overrun == 0x1badbabe);
275 svm_pop_heap (oldheap);
276 pthread_mutex_unlock (&am->vlib_rp->mutex);
280 vl_msg_api_free_nolock (void *a)
284 api_main_t *am = &api_main;
286 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
288 * Here's the beauty of the scheme. Only one proc/thread has
289 * control of a given message buffer. To free a buffer, we just clear the
290 * queue field, and leave. No locks, no hits, no errors...
298 oldheap = svm_push_data_heap (am->vlib_rp);
300 svm_pop_heap (oldheap);
304 vl_set_memory_root_path (const char *name)
306 api_main_t *am = &api_main;
308 am->root_path = name;
312 vl_set_memory_uid (int uid)
314 api_main_t *am = &api_main;
320 vl_set_memory_gid (int gid)
322 api_main_t *am = &api_main;
328 vl_set_global_memory_baseva (u64 baseva)
330 api_main_t *am = &api_main;
332 am->global_baseva = baseva;
336 vl_set_global_memory_size (u64 size)
338 api_main_t *am = &api_main;
340 am->global_size = size;
344 vl_set_api_memory_size (u64 size)
346 api_main_t *am = &api_main;
352 vl_set_global_pvt_heap_size (u64 size)
354 api_main_t *am = &api_main;
356 am->global_pvt_heap_size = size;
360 vl_set_api_pvt_heap_size (u64 size)
362 api_main_t *am = &api_main;
364 am->api_pvt_heap_size = size;
368 vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
370 api_main_t *am = &api_main;
371 u32 vlib_input_queue_length;
373 /* vlib main input queue */
374 vlib_input_queue_length = 1024;
375 if (am->vlib_input_queue_length)
376 vlib_input_queue_length = am->vlib_input_queue_length;
378 shmem_hdr->vl_input_queue =
379 unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
380 getpid (), am->vlib_signal);
385 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
390 vec_add1(shmem_hdr->vl_rings, _rp); \
393 foreach_vl_aring_size;
399 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
404 vec_add1(shmem_hdr->client_rings, _rp); \
407 foreach_clnt_aring_size;
412 vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
414 api_main_t *am = &api_main;
415 vl_api_shm_elem_config_t *c;
421 vl_api_default_mem_config (hdr);
425 vec_foreach (c, config)
430 hdr->vl_input_queue = unix_shared_memory_queue_init (c->count,
435 case VL_API_VLIB_RING:
436 vec_add2 (hdr->vl_rings, rp, 1);
438 case VL_API_CLIENT_RING:
439 vec_add2 (hdr->client_rings, rp, 1);
442 clib_warning ("unknown config type: %d", c->type);
446 size = sizeof (ring_alloc_t) + c->size;
447 rp->rp = unix_shared_memory_queue_init (c->count, size, 0, 0);
449 rp->nitems = c->count;
456 vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
457 int is_vlib, int is_private_region)
459 api_main_t *am = &api_main;
460 vl_shmem_hdr_t *shmem_hdr = 0;
464 /* $$$$ need private region config parameters */
466 oldheap = svm_push_data_heap (vlib_rp);
468 vec_validate (shmem_hdr, 0);
469 shmem_hdr->version = VL_SHM_VERSION;
471 /* Set up the queue and msg ring allocator */
472 vl_api_mem_config (shmem_hdr, config);
474 if (is_private_region == 0)
476 am->shmem_hdr = shmem_hdr;
477 am->vlib_rp = vlib_rp;
478 am->our_pid = getpid ();
480 am->shmem_hdr->vl_pid = am->our_pid;
483 shmem_hdr->vl_pid = am->our_pid;
485 svm_pop_heap (oldheap);
488 * After absolutely everything that a client might see is set up,
489 * declare the shmem region valid
491 vlib_rp->user_ctx = shmem_hdr;
493 pthread_mutex_unlock (&vlib_rp->mutex);
498 vl_map_shmem (const char *region_name, int is_vlib)
500 svm_map_region_args_t _a, *a = &_a;
501 svm_region_t *vlib_rp, *root_rp;
502 api_main_t *am = &api_main;
504 struct timespec ts, tsrem;
505 char *vpe_api_region_suffix = "-vpe-api";
507 memset (a, 0, sizeof (*a));
509 if (strstr (region_name, vpe_api_region_suffix))
511 u8 *root_path = format (0, "%s", region_name);
512 _vec_len (root_path) = (vec_len (root_path) -
513 strlen (vpe_api_region_suffix));
514 vec_terminate_c_string (root_path);
515 a->root_path = (const char *) root_path;
516 am->root_path = (const char *) root_path;
521 rv = svm_region_init_chroot (am->root_path);
526 if (a->root_path != NULL)
528 a->name = "/vpe-api";
531 a->name = region_name;
532 a->size = am->api_size ? am->api_size : (16 << 20);
533 a->flags = SVM_FLAGS_MHEAP;
534 a->uid = am->api_uid;
535 a->gid = am->api_gid;
536 a->pvt_heap_size = am->api_pvt_heap_size;
538 vlib_rp = svm_region_find_or_create (a);
543 pthread_mutex_lock (&vlib_rp->mutex);
544 /* Has someone else set up the shared-memory variable table? */
545 if (vlib_rp->user_ctx)
547 am->shmem_hdr = (void *) vlib_rp->user_ctx;
548 am->our_pid = getpid ();
551 unix_shared_memory_queue_t *q;
554 * application restart. Reset cached pids, API message
555 * rings, list of clients; otherwise, various things
556 * fail. (e.g. queue non-empty notification)
559 /* ghosts keep the region from disappearing properly */
560 svm_client_scan_this_region_nolock (vlib_rp);
561 am->shmem_hdr->application_restarts++;
562 q = am->shmem_hdr->vl_input_queue;
563 am->shmem_hdr->vl_pid = getpid ();
564 q->consumer_pid = am->shmem_hdr->vl_pid;
565 /* Drain the input queue, freeing msgs */
566 for (i = 0; i < 10; i++)
568 if (pthread_mutex_trylock (&q->mutex) == 0)
570 pthread_mutex_unlock (&q->mutex);
574 ts.tv_nsec = 10000 * 1000; /* 10 ms */
575 while (nanosleep (&ts, &tsrem) < 0)
578 /* Mutex buggered, "fix" it */
579 memset (&q->mutex, 0, sizeof (q->mutex));
580 clib_warning ("forcibly release main input queue mutex");
583 am->vlib_rp = vlib_rp;
584 while (unix_shared_memory_queue_sub (q,
587 != -2 /* queue underflow */ )
589 vl_msg_api_free_nolock ((void *) old_msg);
590 am->shmem_hdr->restart_reclaims++;
592 pthread_mutex_unlock (&vlib_rp->mutex);
593 root_rp = svm_get_root_rp ();
595 /* Clean up the root region client list */
596 pthread_mutex_lock (&root_rp->mutex);
597 svm_client_scan_this_region_nolock (root_rp);
598 pthread_mutex_unlock (&root_rp->mutex);
602 pthread_mutex_unlock (&vlib_rp->mutex);
604 am->vlib_rp = vlib_rp;
605 vec_add1 (am->mapped_shmem_regions, vlib_rp);
608 /* Clients simply have to wait... */
611 pthread_mutex_unlock (&vlib_rp->mutex);
613 /* Wait up to 100 seconds... */
614 for (i = 0; i < 10000; i++)
617 ts.tv_nsec = 10000 * 1000; /* 10 ms */
618 while (nanosleep (&ts, &tsrem) < 0)
620 if (vlib_rp->user_ctx)
623 /* Clean up and leave... */
624 svm_region_unmap (vlib_rp);
625 clib_warning ("region init fail");
629 am->shmem_hdr = (void *) vlib_rp->user_ctx;
630 am->our_pid = getpid ();
631 am->vlib_rp = vlib_rp;
632 vec_add1 (am->mapped_shmem_regions, vlib_rp);
636 /* Nope, it's our problem... */
637 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
638 0 /* is_private_region */ );
640 vec_add1 (am->mapped_shmem_regions, vlib_rp);
645 vl_register_mapped_shmem_region (svm_region_t * rp)
647 api_main_t *am = &api_main;
649 vec_add1 (am->mapped_shmem_regions, rp);
653 vl_unmap_shmem (void)
657 api_main_t *am = &api_main;
659 if (!svm_get_root_rp ())
662 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
664 rp = am->mapped_shmem_regions[i];
665 svm_region_unmap (rp);
668 vec_free (am->mapped_shmem_regions);
672 /* $$$ more careful cleanup, valgrind run... */
673 vec_free (am->msg_handlers);
674 vec_free (am->msg_endian_handlers);
675 vec_free (am->msg_print_handlers);
679 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
681 api_main_t *am = &api_main;
682 uword *trace = (uword *) elem;
684 if (am->tx_trace && am->tx_trace->enabled)
685 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
687 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
691 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
693 api_main_t *am = &api_main;
694 uword *trace = (uword *) elem;
696 if (am->tx_trace && am->tx_trace->enabled)
697 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
699 (void) unix_shared_memory_queue_add_nolock (q, elem);
703 vl_api_get_msg_index (u8 * name_and_crc)
705 api_main_t *am = &api_main;
708 if (am->msg_index_by_name_and_crc)
710 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
717 static inline vl_api_registration_t *
718 vl_api_client_index_to_registration_internal (u32 handle)
720 vl_api_registration_t **regpp;
721 vl_api_registration_t *regp;
722 api_main_t *am = &api_main;
725 index = vl_msg_api_handle_get_index (handle);
726 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
727 != vl_msg_api_handle_get_epoch (handle))
729 vl_msg_api_increment_missing_client_counter ();
733 regpp = am->vl_clients + index;
735 if (pool_is_free (am->vl_clients, regpp))
737 vl_msg_api_increment_missing_client_counter ();
744 vl_api_registration_t *
745 vl_api_client_index_to_registration (u32 index)
747 if (PREDICT_FALSE (socket_main.current_rp != 0))
748 return socket_main.current_rp;
750 return (vl_api_client_index_to_registration_internal (index));
753 unix_shared_memory_queue_t *
754 vl_api_client_index_to_input_queue (u32 index)
756 vl_api_registration_t *regp;
757 api_main_t *am = &api_main;
759 /* Special case: vlib trying to send itself a message */
760 if (index == (u32) ~ 0)
761 return (am->shmem_hdr->vl_input_queue);
763 regp = vl_api_client_index_to_registration_internal (index);
766 return (regp->vl_input_queue);
770 * fd.io coding-style-patch-verification: ON
773 * eval: (c-set-style "gnu")