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>
43 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
48 unix_shared_memory_queue_t *q;
50 vl_shmem_hdr_t *shmem_hdr;
51 api_main_t *am = &api_main;
53 shmem_hdr = am->shmem_hdr;
57 clib_warning ("shared memory header NULL");
61 /* account for the msgbuf_t header */
62 nbytes += sizeof (msgbuf_t);
64 if (shmem_hdr->vl_rings == 0)
66 clib_warning ("vl_rings NULL");
71 if (shmem_hdr->client_rings == 0)
73 clib_warning ("client_rings NULL");
78 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
79 for (i = 0; i < vec_len (ap); i++)
82 if (nbytes > ap[i].size)
90 pthread_mutex_lock (&q->mutex);
92 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
94 * Is this item still in use?
98 /* yes, loser; try next larger pool */
101 pthread_mutex_unlock (&q->mutex);
104 /* OK, we have a winner */
107 * Remember the source queue, although we
108 * don't need to know the queue to free the item.
112 if (q->head == q->maxsize)
116 pthread_mutex_unlock (&q->mutex);
121 * Request too big, or head element of all size-compatible rings
122 * still in use. Fall back to shared-memory malloc.
126 pthread_mutex_lock (&am->vlib_rp->mutex);
127 oldheap = svm_push_data_heap (am->vlib_rp);
130 rv = clib_mem_alloc_or_null (nbytes);
131 if (PREDICT_FALSE (rv == 0))
133 svm_pop_heap (oldheap);
134 pthread_mutex_unlock (&am->vlib_rp->mutex);
139 rv = clib_mem_alloc (nbytes);
142 svm_pop_heap (oldheap);
143 pthread_mutex_unlock (&am->vlib_rp->mutex);
146 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
151 vl_msg_api_alloc (int nbytes)
154 api_main_t *am = &api_main;
155 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
158 * Clients use pool-0, vlib proc uses pool 1
160 pool = (am->our_pid == shmem_hdr->vl_pid);
161 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
165 vl_msg_api_alloc_or_null (int nbytes)
168 api_main_t *am = &api_main;
169 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
171 pool = (am->our_pid == shmem_hdr->vl_pid);
172 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
176 vl_msg_api_alloc_as_if_client (int nbytes)
178 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
182 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
184 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
188 vl_msg_api_free (void *a)
192 api_main_t *am = &api_main;
194 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
197 * Here's the beauty of the scheme. Only one proc/thread has
198 * control of a given message buffer. To free a buffer, we just clear the
199 * queue field, and leave. No locks, no hits, no errors...
207 pthread_mutex_lock (&am->vlib_rp->mutex);
208 oldheap = svm_push_data_heap (am->vlib_rp);
210 svm_pop_heap (oldheap);
211 pthread_mutex_unlock (&am->vlib_rp->mutex);
215 vl_msg_api_free_nolock (void *a)
219 api_main_t *am = &api_main;
221 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
223 * Here's the beauty of the scheme. Only one proc/thread has
224 * control of a given message buffer. To free a buffer, we just clear the
225 * queue field, and leave. No locks, no hits, no errors...
233 oldheap = svm_push_data_heap (am->vlib_rp);
235 svm_pop_heap (oldheap);
239 vl_set_memory_root_path (char *name)
241 api_main_t *am = &api_main;
243 am->root_path = name;
247 vl_set_memory_uid (int uid)
249 api_main_t *am = &api_main;
255 vl_set_memory_gid (int gid)
257 api_main_t *am = &api_main;
263 vl_set_global_memory_baseva (u64 baseva)
265 api_main_t *am = &api_main;
267 am->global_baseva = baseva;
271 vl_set_global_memory_size (u64 size)
273 api_main_t *am = &api_main;
275 am->global_size = size;
279 vl_set_api_memory_size (u64 size)
281 api_main_t *am = &api_main;
287 vl_set_global_pvt_heap_size (u64 size)
289 api_main_t *am = &api_main;
291 am->global_pvt_heap_size = size;
295 vl_set_api_pvt_heap_size (u64 size)
297 api_main_t *am = &api_main;
299 am->api_pvt_heap_size = size;
303 vl_map_shmem (char *region_name, int is_vlib)
305 svm_map_region_args_t _a, *a = &_a;
306 svm_region_t *vlib_rp, *root_rp;
308 vl_shmem_hdr_t *shmem_hdr = 0;
309 api_main_t *am = &api_main;
311 struct timespec ts, tsrem;
314 svm_region_init_chroot (am->root_path);
316 memset (a, 0, sizeof (*a));
318 a->name = region_name;
319 a->size = am->api_size ? am->api_size : (16 << 20);
320 a->flags = SVM_FLAGS_MHEAP;
321 a->uid = am->api_uid;
322 a->gid = am->api_gid;
323 a->pvt_heap_size = am->api_pvt_heap_size;
325 vlib_rp = svm_region_find_or_create (a);
330 pthread_mutex_lock (&vlib_rp->mutex);
331 /* Has someone else set up the shared-memory variable table? */
332 if (vlib_rp->user_ctx)
334 am->shmem_hdr = (void *) vlib_rp->user_ctx;
335 am->our_pid = getpid ();
338 unix_shared_memory_queue_t *q;
341 * application restart. Reset cached pids, API message
342 * rings, list of clients; otherwise, various things
343 * fail. (e.g. queue non-empty notification)
346 /* ghosts keep the region from disappearing properly */
347 svm_client_scan_this_region_nolock (vlib_rp);
348 am->shmem_hdr->application_restarts++;
349 q = am->shmem_hdr->vl_input_queue;
350 am->shmem_hdr->vl_pid = getpid ();
351 q->consumer_pid = am->shmem_hdr->vl_pid;
352 /* Drain the input queue, freeing msgs */
353 for (i = 0; i < 10; i++)
355 if (pthread_mutex_trylock (&q->mutex) == 0)
357 pthread_mutex_unlock (&q->mutex);
361 ts.tv_nsec = 10000 * 1000; /* 10 ms */
362 while (nanosleep (&ts, &tsrem) < 0)
365 /* Mutex buggered, "fix" it */
366 memset (&q->mutex, 0, sizeof (q->mutex));
367 clib_warning ("forcibly release main input queue mutex");
370 am->vlib_rp = vlib_rp;
371 while (unix_shared_memory_queue_sub (q,
374 != -2 /* queue underflow */ )
376 vl_msg_api_free_nolock ((void *) old_msg);
377 am->shmem_hdr->restart_reclaims++;
379 pthread_mutex_unlock (&vlib_rp->mutex);
380 root_rp = svm_get_root_rp ();
382 /* Clean up the root region client list */
383 pthread_mutex_lock (&root_rp->mutex);
384 svm_client_scan_this_region_nolock (root_rp);
385 pthread_mutex_unlock (&root_rp->mutex);
389 pthread_mutex_unlock (&vlib_rp->mutex);
391 am->vlib_rp = vlib_rp;
392 vec_add1 (am->mapped_shmem_regions, vlib_rp);
395 /* Clients simply have to wait... */
398 pthread_mutex_unlock (&vlib_rp->mutex);
400 /* Wait up to 100 seconds... */
401 for (i = 0; i < 10000; i++)
404 ts.tv_nsec = 10000 * 1000; /* 10 ms */
405 while (nanosleep (&ts, &tsrem) < 0)
407 if (vlib_rp->user_ctx)
410 /* Clean up and leave... */
411 svm_region_unmap (vlib_rp);
412 clib_warning ("region init fail");
416 am->shmem_hdr = (void *) vlib_rp->user_ctx;
417 am->our_pid = getpid ();
418 am->vlib_rp = vlib_rp;
419 vec_add1 (am->mapped_shmem_regions, vlib_rp);
423 /* Nope, it's our problem... */
425 oldheap = svm_push_data_heap (vlib_rp);
427 vec_validate (shmem_hdr, 0);
428 shmem_hdr->version = VL_SHM_VERSION;
430 /* vlib main input queue */
431 shmem_hdr->vl_input_queue =
432 unix_shared_memory_queue_init (1024, sizeof (uword), getpid (),
435 /* Set up the msg ring allocator */
439 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
444 vec_add1(shmem_hdr->vl_rings, _rp); \
447 foreach_vl_aring_size;
453 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
458 vec_add1(shmem_hdr->client_rings, _rp); \
461 foreach_clnt_aring_size;
464 am->shmem_hdr = shmem_hdr;
465 am->vlib_rp = vlib_rp;
466 am->our_pid = getpid ();
468 am->shmem_hdr->vl_pid = am->our_pid;
470 svm_pop_heap (oldheap);
473 * After absolutely everything that a client might see is set up,
474 * declare the shmem region valid
476 vlib_rp->user_ctx = shmem_hdr;
478 pthread_mutex_unlock (&vlib_rp->mutex);
479 vec_add1 (am->mapped_shmem_regions, vlib_rp);
484 vl_register_mapped_shmem_region (svm_region_t * rp)
486 api_main_t *am = &api_main;
488 vec_add1 (am->mapped_shmem_regions, rp);
492 vl_unmap_shmem (void)
496 api_main_t *am = &api_main;
498 if (!svm_get_root_rp ())
501 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
503 rp = am->mapped_shmem_regions[i];
504 svm_region_unmap (rp);
507 vec_free (am->mapped_shmem_regions);
511 /* $$$ more careful cleanup, valgrind run... */
512 vec_free (am->msg_handlers);
513 vec_free (am->msg_endian_handlers);
514 vec_free (am->msg_print_handlers);
518 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
520 api_main_t *am = &api_main;
521 uword *trace = (uword *) elem;
523 if (am->tx_trace && am->tx_trace->enabled)
524 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
526 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
530 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
532 api_main_t *am = &api_main;
533 uword *trace = (uword *) elem;
535 if (am->tx_trace && am->tx_trace->enabled)
536 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
538 (void) unix_shared_memory_queue_add_nolock (q, elem);
542 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
544 serialize_main_t _sm, *sm = &_sm;
545 api_main_t *am = &api_main;
552 am->my_client_index = mp->index;
553 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
555 /* Clean out any previous hash table (unlikely) */
556 if (am->msg_index_by_name_and_crc)
562 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
564 vec_add1 (keys, (u8 *) hp->key);
567 for (i = 0; i < vec_len (keys); i++)
572 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
574 /* Recreate the vnet-side API message handler table */
575 tblv = (u8 *) mp->message_table;
576 serialize_open_vector (sm, tblv);
577 unserialize_integer (sm, &nmsgs, sizeof (u32));
579 for (i = 0; i < nmsgs; i++)
581 msg_index = unserialize_likely_small_unsigned_integer (sm);
582 unserialize_cstring (sm, (char **) &name_and_crc);
583 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
588 vl_api_get_msg_index (u8 * name_and_crc)
590 api_main_t *am = &api_main;
593 if (am->msg_index_by_name_and_crc)
595 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
603 vl_client_connect (char *name, int ctx_quota, int input_queue_size)
606 vl_api_memclnt_create_t *mp;
607 vl_api_memclnt_create_reply_t *rp;
608 unix_shared_memory_queue_t *vl_input_queue;
609 vl_shmem_hdr_t *shmem_hdr;
612 api_main_t *am = &api_main;
614 if (am->my_registration)
616 clib_warning ("client %s already connected...", name);
620 if (am->vlib_rp == 0)
622 clib_warning ("am->vlib_rp NULL");
627 shmem_hdr = am->shmem_hdr;
629 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
631 clib_warning ("shmem_hdr / input queue NULL");
635 pthread_mutex_lock (&svm->mutex);
636 oldheap = svm_push_data_heap (svm);
638 unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
640 pthread_mutex_unlock (&svm->mutex);
641 svm_pop_heap (oldheap);
643 am->my_client_index = ~0;
644 am->my_registration = 0;
645 am->vl_input_queue = vl_input_queue;
647 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
648 memset (mp, 0, sizeof (*mp));
649 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
650 mp->ctx_quota = ctx_quota;
651 mp->input_queue = (uword) vl_input_queue;
652 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
654 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
659 struct timespec ts, tsrem;
662 /* Wait up to 10 seconds */
663 for (i = 0; i < 1000; i++)
665 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
670 ts.tv_nsec = 10000 * 1000; /* 10 ms */
671 while (nanosleep (&ts, &tsrem) < 0)
675 clib_warning ("memclnt_create_reply timeout");
679 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
681 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
684 rv = clib_net_to_host_u32 (rp->response);
686 vl_msg_api_handler ((void *) rp);
693 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
696 api_main_t *am = &api_main;
698 pthread_mutex_lock (&am->vlib_rp->mutex);
699 oldheap = svm_push_data_heap (am->vlib_rp);
700 unix_shared_memory_queue_free (am->vl_input_queue);
701 pthread_mutex_unlock (&am->vlib_rp->mutex);
702 svm_pop_heap (oldheap);
704 am->my_client_index = ~0;
705 am->my_registration = 0;
706 am->vl_input_queue = 0;
710 vl_client_disconnect (void)
712 vl_api_memclnt_delete_t *mp;
713 vl_api_memclnt_delete_reply_t *rp;
714 unix_shared_memory_queue_t *vl_input_queue;
715 vl_shmem_hdr_t *shmem_hdr;
717 api_main_t *am = &api_main;
719 ASSERT (am->vlib_rp);
720 shmem_hdr = am->shmem_hdr;
721 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
723 vl_input_queue = am->vl_input_queue;
725 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
726 memset (mp, 0, sizeof (*mp));
727 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
728 mp->index = am->my_client_index;
729 mp->handle = (uword) am->my_registration;
731 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
734 * Have to be careful here, in case the client is disconnecting
735 * because e.g. the vlib process died, or is unresponsive.
745 if (now >= (begin + 2))
747 clib_warning ("peer unresponsive, give up");
748 am->my_client_index = ~0;
749 am->my_registration = 0;
753 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
756 /* drain the queue */
757 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
759 vl_msg_api_handler ((void *) rp);
762 vl_msg_api_handler ((void *) rp);
767 static inline vl_api_registration_t *
768 vl_api_client_index_to_registration_internal (u32 handle)
770 vl_api_registration_t **regpp;
771 vl_api_registration_t *regp;
772 api_main_t *am = &api_main;
775 index = vl_msg_api_handle_get_index (handle);
776 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
777 != vl_msg_api_handle_get_epoch (handle))
779 vl_msg_api_increment_missing_client_counter ();
783 regpp = am->vl_clients + index;
785 if (pool_is_free (am->vl_clients, regpp))
787 vl_msg_api_increment_missing_client_counter ();
794 vl_api_registration_t *
795 vl_api_client_index_to_registration (u32 index)
797 return (vl_api_client_index_to_registration_internal (index));
800 unix_shared_memory_queue_t *
801 vl_api_client_index_to_input_queue (u32 index)
803 vl_api_registration_t *regp;
805 regp = vl_api_client_index_to_registration_internal (index);
808 return (regp->vl_input_queue);
811 #define foreach_api_client_msg \
812 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
813 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
816 vl_client_api_map (char *region_name)
820 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
826 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
827 vl_api_##n##_t_handler, \
828 0/* cleanup */, 0/* endian */, 0/* print */, \
829 sizeof(vl_api_##n##_t), 1);
830 foreach_api_client_msg;
836 vl_client_api_unmap (void)
842 * fd.io coding-style-patch-verification: ON
845 * eval: (c-set-style "gnu")