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)
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");
70 if (shmem_hdr->client_rings == 0)
72 clib_warning ("client_rings NULL");
76 ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
77 for (i = 0; i < vec_len (ap); i++)
80 if (nbytes > ap[i].size)
88 pthread_mutex_lock (&q->mutex);
90 rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
92 * Is this item still in use?
96 /* yes, loser; try next larger pool */
99 pthread_mutex_unlock (&q->mutex);
102 /* OK, we have a winner */
105 * Remember the source queue, although we
106 * don't need to know the queue to free the item.
110 if (q->head == q->maxsize)
114 pthread_mutex_unlock (&q->mutex);
119 * Request too big, or head element of all size-compatible rings
120 * still in use. Fall back to shared-memory malloc.
124 pthread_mutex_lock (&am->vlib_rp->mutex);
125 oldheap = svm_push_data_heap (am->vlib_rp);
126 rv = clib_mem_alloc (nbytes);
128 svm_pop_heap (oldheap);
129 pthread_mutex_unlock (&am->vlib_rp->mutex);
132 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
137 vl_msg_api_alloc (int nbytes)
140 api_main_t *am = &api_main;
141 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
144 * Clients use pool-0, vlib proc uses pool 1
146 pool = (am->our_pid == shmem_hdr->vl_pid);
147 return vl_msg_api_alloc_internal (nbytes, pool);
151 vl_msg_api_alloc_as_if_client (int nbytes)
153 return vl_msg_api_alloc_internal (nbytes, 0);
157 vl_msg_api_free (void *a)
161 api_main_t *am = &api_main;
163 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
166 * Here's the beauty of the scheme. Only one proc/thread has
167 * control of a given message buffer. To free a buffer, we just clear the
168 * queue field, and leave. No locks, no hits, no errors...
176 pthread_mutex_lock (&am->vlib_rp->mutex);
177 oldheap = svm_push_data_heap (am->vlib_rp);
179 svm_pop_heap (oldheap);
180 pthread_mutex_unlock (&am->vlib_rp->mutex);
184 vl_msg_api_free_nolock (void *a)
188 api_main_t *am = &api_main;
190 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
192 * Here's the beauty of the scheme. Only one proc/thread has
193 * control of a given message buffer. To free a buffer, we just clear the
194 * queue field, and leave. No locks, no hits, no errors...
202 oldheap = svm_push_data_heap (am->vlib_rp);
204 svm_pop_heap (oldheap);
208 vl_set_memory_root_path (char *name)
210 api_main_t *am = &api_main;
212 am->root_path = name;
216 vl_set_memory_uid (int uid)
218 api_main_t *am = &api_main;
224 vl_set_memory_gid (int gid)
226 api_main_t *am = &api_main;
232 vl_set_global_memory_baseva (u64 baseva)
234 api_main_t *am = &api_main;
236 am->global_baseva = baseva;
240 vl_set_global_memory_size (u64 size)
242 api_main_t *am = &api_main;
244 am->global_size = size;
248 vl_set_api_memory_size (u64 size)
250 api_main_t *am = &api_main;
256 vl_set_global_pvt_heap_size (u64 size)
258 api_main_t *am = &api_main;
260 am->global_pvt_heap_size = size;
264 vl_set_api_pvt_heap_size (u64 size)
266 api_main_t *am = &api_main;
268 am->api_pvt_heap_size = size;
272 vl_map_shmem (char *region_name, int is_vlib)
274 svm_map_region_args_t _a, *a = &_a;
275 svm_region_t *vlib_rp, *root_rp;
277 vl_shmem_hdr_t *shmem_hdr = 0;
278 api_main_t *am = &api_main;
280 struct timespec ts, tsrem;
283 svm_region_init_chroot (am->root_path);
285 memset (a, 0, sizeof (*a));
287 a->name = region_name;
288 a->size = am->api_size ? am->api_size : (16 << 20);
289 a->flags = SVM_FLAGS_MHEAP;
290 a->uid = am->api_uid;
291 a->gid = am->api_gid;
292 a->pvt_heap_size = am->api_pvt_heap_size;
294 vlib_rp = svm_region_find_or_create (a);
299 pthread_mutex_lock (&vlib_rp->mutex);
300 /* Has someone else set up the shared-memory variable table? */
301 if (vlib_rp->user_ctx)
303 am->shmem_hdr = (void *) vlib_rp->user_ctx;
304 am->our_pid = getpid ();
307 unix_shared_memory_queue_t *q;
310 * application restart. Reset cached pids, API message
311 * rings, list of clients; otherwise, various things
312 * fail. (e.g. queue non-empty notification)
315 /* ghosts keep the region from disappearing properly */
316 svm_client_scan_this_region_nolock (vlib_rp);
317 am->shmem_hdr->application_restarts++;
318 q = am->shmem_hdr->vl_input_queue;
319 am->shmem_hdr->vl_pid = getpid ();
320 q->consumer_pid = am->shmem_hdr->vl_pid;
321 /* Drain the input queue, freeing msgs */
322 for (i = 0; i < 10; i++)
324 if (pthread_mutex_trylock (&q->mutex) == 0)
326 pthread_mutex_unlock (&q->mutex);
330 ts.tv_nsec = 10000 * 1000; /* 10 ms */
331 while (nanosleep (&ts, &tsrem) < 0)
334 /* Mutex buggered, "fix" it */
335 memset (&q->mutex, 0, sizeof (q->mutex));
336 clib_warning ("forcibly release main input queue mutex");
339 am->vlib_rp = vlib_rp;
340 while (unix_shared_memory_queue_sub (q,
343 != -2 /* queue underflow */ )
345 vl_msg_api_free_nolock ((void *) old_msg);
346 am->shmem_hdr->restart_reclaims++;
348 pthread_mutex_unlock (&vlib_rp->mutex);
349 root_rp = svm_get_root_rp ();
351 /* Clean up the root region client list */
352 pthread_mutex_lock (&root_rp->mutex);
353 svm_client_scan_this_region_nolock (root_rp);
354 pthread_mutex_unlock (&root_rp->mutex);
358 pthread_mutex_unlock (&vlib_rp->mutex);
360 am->vlib_rp = vlib_rp;
361 vec_add1 (am->mapped_shmem_regions, vlib_rp);
364 /* Clients simply have to wait... */
367 pthread_mutex_unlock (&vlib_rp->mutex);
369 /* Wait up to 100 seconds... */
370 for (i = 0; i < 10000; i++)
373 ts.tv_nsec = 10000 * 1000; /* 10 ms */
374 while (nanosleep (&ts, &tsrem) < 0)
376 if (vlib_rp->user_ctx)
379 /* Clean up and leave... */
380 svm_region_unmap (vlib_rp);
381 clib_warning ("region init fail");
385 am->shmem_hdr = (void *) vlib_rp->user_ctx;
386 am->our_pid = getpid ();
387 am->vlib_rp = vlib_rp;
388 vec_add1 (am->mapped_shmem_regions, vlib_rp);
392 /* Nope, it's our problem... */
394 oldheap = svm_push_data_heap (vlib_rp);
396 vec_validate (shmem_hdr, 0);
397 shmem_hdr->version = VL_SHM_VERSION;
399 /* vlib main input queue */
400 shmem_hdr->vl_input_queue =
401 unix_shared_memory_queue_init (1024, sizeof (uword), getpid (),
404 /* Set up the msg ring allocator */
408 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
413 vec_add1(shmem_hdr->vl_rings, _rp); \
416 foreach_vl_aring_size;
422 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
427 vec_add1(shmem_hdr->client_rings, _rp); \
430 foreach_clnt_aring_size;
433 am->shmem_hdr = shmem_hdr;
434 am->vlib_rp = vlib_rp;
435 am->our_pid = getpid ();
437 am->shmem_hdr->vl_pid = am->our_pid;
439 svm_pop_heap (oldheap);
442 * After absolutely everything that a client might see is set up,
443 * declare the shmem region valid
445 vlib_rp->user_ctx = shmem_hdr;
447 pthread_mutex_unlock (&vlib_rp->mutex);
448 vec_add1 (am->mapped_shmem_regions, vlib_rp);
453 vl_register_mapped_shmem_region (svm_region_t * rp)
455 api_main_t *am = &api_main;
457 vec_add1 (am->mapped_shmem_regions, rp);
461 vl_unmap_shmem (void)
465 api_main_t *am = &api_main;
467 if (!svm_get_root_rp ())
470 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
472 rp = am->mapped_shmem_regions[i];
473 svm_region_unmap (rp);
476 vec_free (am->mapped_shmem_regions);
480 /* $$$ more careful cleanup, valgrind run... */
481 vec_free (am->msg_handlers);
482 vec_free (am->msg_endian_handlers);
483 vec_free (am->msg_print_handlers);
487 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
489 api_main_t *am = &api_main;
490 uword *trace = (uword *) elem;
492 if (am->tx_trace && am->tx_trace->enabled)
493 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
495 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
499 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
501 api_main_t *am = &api_main;
502 uword *trace = (uword *) elem;
504 if (am->tx_trace && am->tx_trace->enabled)
505 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
507 (void) unix_shared_memory_queue_add_nolock (q, elem);
511 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
513 api_main_t *am = &api_main;
516 am->my_client_index = mp->index;
517 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
519 rv = ntohl (mp->response);
522 clib_warning ("WARNING: API mismatch detected");
525 void vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
526 __attribute__ ((weak));
529 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
533 for (i = 0; i < ARRAY_LEN (mp->api_versions); i++)
534 mp->api_versions[i] = 0;
538 vl_client_connect (char *name, int ctx_quota, int input_queue_size)
541 vl_api_memclnt_create_t *mp;
542 vl_api_memclnt_create_reply_t *rp;
543 unix_shared_memory_queue_t *vl_input_queue;
544 vl_shmem_hdr_t *shmem_hdr;
547 api_main_t *am = &api_main;
549 if (am->my_registration)
551 clib_warning ("client %s already connected...", name);
555 if (am->vlib_rp == 0)
557 clib_warning ("am->vlib_rp NULL");
562 shmem_hdr = am->shmem_hdr;
564 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
566 clib_warning ("shmem_hdr / input queue NULL");
570 pthread_mutex_lock (&svm->mutex);
571 oldheap = svm_push_data_heap (svm);
573 unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
575 pthread_mutex_unlock (&svm->mutex);
576 svm_pop_heap (oldheap);
578 am->my_client_index = ~0;
579 am->my_registration = 0;
580 am->vl_input_queue = vl_input_queue;
582 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
583 memset (mp, 0, sizeof (*mp));
584 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
585 mp->ctx_quota = ctx_quota;
586 mp->input_queue = (uword) vl_input_queue;
587 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
589 vl_client_add_api_signatures (mp);
591 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
596 struct timespec ts, tsrem;
599 /* Wait up to 10 seconds */
600 for (i = 0; i < 1000; i++)
602 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
607 ts.tv_nsec = 10000 * 1000; /* 10 ms */
608 while (nanosleep (&ts, &tsrem) < 0)
612 clib_warning ("memclnt_create_reply timeout");
616 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
618 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
621 rv = clib_net_to_host_u32 (rp->response);
623 vl_msg_api_handler ((void *) rp);
630 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
633 api_main_t *am = &api_main;
635 pthread_mutex_lock (&am->vlib_rp->mutex);
636 oldheap = svm_push_data_heap (am->vlib_rp);
637 unix_shared_memory_queue_free (am->vl_input_queue);
638 pthread_mutex_unlock (&am->vlib_rp->mutex);
639 svm_pop_heap (oldheap);
641 am->my_client_index = ~0;
642 am->my_registration = 0;
643 am->vl_input_queue = 0;
647 vl_client_disconnect (void)
649 vl_api_memclnt_delete_t *mp;
650 vl_api_memclnt_delete_reply_t *rp;
651 unix_shared_memory_queue_t *vl_input_queue;
652 vl_shmem_hdr_t *shmem_hdr;
654 api_main_t *am = &api_main;
656 ASSERT (am->vlib_rp);
657 shmem_hdr = am->shmem_hdr;
658 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
660 vl_input_queue = am->vl_input_queue;
662 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
663 memset (mp, 0, sizeof (*mp));
664 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
665 mp->index = am->my_client_index;
666 mp->handle = (uword) am->my_registration;
668 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
671 * Have to be careful here, in case the client is disconnecting
672 * because e.g. the vlib process died, or is unresponsive.
682 if (now >= (begin + 2))
684 clib_warning ("peer unresponsive, give up");
685 am->my_client_index = ~0;
686 am->my_registration = 0;
690 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
693 /* drain the queue */
694 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
696 vl_msg_api_handler ((void *) rp);
699 vl_msg_api_handler ((void *) rp);
704 static inline vl_api_registration_t *
705 vl_api_client_index_to_registration_internal (u32 handle)
707 vl_api_registration_t **regpp;
708 vl_api_registration_t *regp;
709 api_main_t *am = &api_main;
712 index = vl_msg_api_handle_get_index (handle);
713 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
714 != vl_msg_api_handle_get_epoch (handle))
716 vl_msg_api_increment_missing_client_counter ();
720 regpp = am->vl_clients + index;
722 if (pool_is_free (am->vl_clients, regpp))
724 vl_msg_api_increment_missing_client_counter ();
731 vl_api_registration_t *
732 vl_api_client_index_to_registration (u32 index)
734 return (vl_api_client_index_to_registration_internal (index));
737 unix_shared_memory_queue_t *
738 vl_api_client_index_to_input_queue (u32 index)
740 vl_api_registration_t *regp;
742 regp = vl_api_client_index_to_registration_internal (index);
745 return (regp->vl_input_queue);
748 #define foreach_api_client_msg \
749 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
750 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
753 vl_client_api_map (char *region_name)
757 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
763 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
764 vl_api_##n##_t_handler, \
765 0/* cleanup */, 0/* endian */, 0/* print */, \
766 sizeof(vl_api_##n##_t), 1);
767 foreach_api_client_msg;
773 vl_client_api_unmap (void)
779 * fd.io coding-style-patch-verification: ON
782 * eval: (c-set-style "gnu")