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 *------------------------------------------------------------------
27 #include <vppinfra/format.h>
28 #include <vppinfra/byte_order.h>
29 #include <vppinfra/error.h>
30 #include <vlib/vlib.h>
31 #include <vlib/unix/unix.h>
32 #include <vlibmemory/api.h>
33 #include <vlibmemory/unix_shared_memory_queue.h>
35 #include <vlibmemory/vl_memory_msg_enum.h>
38 #include <vlibmemory/vl_memory_api_h.h>
41 typedef struct msgbuf_ {
42 unix_shared_memory_queue_t *q;
46 static inline void *vl_msg_api_alloc_internal(int nbytes, int pool)
51 unix_shared_memory_queue_t *q;
53 vl_shmem_hdr_t *shmem_hdr;
54 api_main_t *am = &api_main;
56 shmem_hdr = am->shmem_hdr;
59 clib_warning ("shared memory header NULL");
63 /* account for the msgbuf_t header*/
64 nbytes += sizeof(msgbuf_t);
66 if (shmem_hdr->vl_rings == 0) {
67 clib_warning ("vl_rings NULL");
71 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++) {
79 if (nbytes > ap[i].size) {
85 pthread_mutex_lock(&q->mutex);
87 rv = (msgbuf_t *) (&q->data[0] + q->head*q->elsize);
89 * Is this item still in use?
92 /* yes, loser; try next larger pool */
95 pthread_mutex_unlock(&q->mutex);
98 /* OK, we have a winner */
101 * Remember the source queue, although we
102 * don't need to know the queue to free the item.
106 if (q->head == q->maxsize)
110 pthread_mutex_unlock(&q->mutex);
115 * Request too big, or head element of all size-compatible rings
116 * still in use. Fall back to shared-memory malloc.
120 pthread_mutex_lock (&am->vlib_rp->mutex);
121 oldheap = svm_push_data_heap (am->vlib_rp);
122 rv = clib_mem_alloc(nbytes + sizeof(msgbuf_t));
124 svm_pop_heap (oldheap);
125 pthread_mutex_unlock (&am->vlib_rp->mutex);
131 void *vl_msg_api_alloc (int nbytes)
134 api_main_t *am = &api_main;
135 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
138 * Clients use pool-0, vlib proc uses pool 1
140 pool = (am->our_pid == shmem_hdr->vl_pid);
141 return vl_msg_api_alloc_internal (nbytes, pool);
144 void *vl_msg_api_alloc_as_if_client (int nbytes)
146 return vl_msg_api_alloc_internal (nbytes, 0);
149 void vl_msg_api_free(void *a)
153 api_main_t *am = &api_main;
155 rv = (msgbuf_t *)(((u8 *)a) - sizeof(*rv));
157 * Here's the beauty of the scheme. Only one proc/thread has
158 * control of a given message buffer. To free a buffer, we just clear the
159 * queue field, and leave. No locks, no hits, no errors...
166 pthread_mutex_lock (&am->vlib_rp->mutex);
167 oldheap = svm_push_data_heap (am->vlib_rp);
169 svm_pop_heap (oldheap);
170 pthread_mutex_unlock (&am->vlib_rp->mutex);
173 static void vl_msg_api_free_nolock (void *a)
177 api_main_t *am = &api_main;
179 rv = (msgbuf_t *)(((u8 *)a) - sizeof(*rv));
181 * Here's the beauty of the scheme. Only one proc/thread has
182 * control of a given message buffer. To free a buffer, we just clear the
183 * queue field, and leave. No locks, no hits, no errors...
190 oldheap = svm_push_data_heap (am->vlib_rp);
192 svm_pop_heap (oldheap);
195 void vl_set_memory_root_path (char *name)
197 api_main_t *am = &api_main;
199 am->root_path = name;
202 int vl_map_shmem (char *region_name, int is_vlib)
204 svm_map_region_args_t *a = 0;
205 svm_region_t *vlib_rp, *root_rp;
207 vl_shmem_hdr_t *shmem_hdr=0;
208 api_main_t *am = &api_main;
210 struct timespec ts, tsrem;
213 svm_region_init_chroot(am->root_path);
217 a->name = region_name;
219 a->flags = SVM_FLAGS_MHEAP;
221 vlib_rp = svm_region_find_or_create (a);
228 pthread_mutex_lock (&vlib_rp->mutex);
229 /* Has someone else set up the shared-memory variable table? */
230 if (vlib_rp->user_ctx) {
231 am->shmem_hdr = (void *) vlib_rp->user_ctx;
232 am->our_pid = getpid();
234 unix_shared_memory_queue_t *q;
237 * application restart. Reset cached pids, API message
238 * rings, list of clients; otherwise, various things
239 * fail. (e.g. queue non-empty notification)
242 /* ghosts keep the region from disappearing properly */
243 svm_client_scan_this_region_nolock(vlib_rp);
244 am->shmem_hdr->application_restarts++;
245 q = am->shmem_hdr->vl_input_queue;
246 am->shmem_hdr->vl_pid = getpid();
247 q->consumer_pid = am->shmem_hdr->vl_pid;
248 /* Drain the input queue, freeing msgs */
249 for (i = 0; i < 10; i++) {
250 if (pthread_mutex_trylock (&q->mutex) == 0) {
251 pthread_mutex_unlock (&q->mutex);
255 ts.tv_nsec = 10000*1000; /* 10 ms */
256 while (nanosleep(&ts, &tsrem) < 0)
259 /* Mutex buggered, "fix" it */
260 memset (&q->mutex, 0, sizeof (q->mutex));
261 clib_warning ("forcibly release main input queue mutex");
264 am->vlib_rp = vlib_rp;
265 while (unix_shared_memory_queue_sub (q,
268 != -2 /* queue underflow */) {
269 vl_msg_api_free_nolock ((void *)old_msg);
270 am->shmem_hdr->restart_reclaims++;
272 pthread_mutex_unlock (&vlib_rp->mutex);
273 root_rp = svm_get_root_rp();
275 /* Clean up the root region client list */
276 pthread_mutex_lock (&root_rp->mutex);
277 svm_client_scan_this_region_nolock (root_rp);
278 pthread_mutex_unlock (&root_rp->mutex);
280 pthread_mutex_unlock (&vlib_rp->mutex);
282 * Make sure the vlib app is really there...
283 * Wait up to 100 seconds...
285 for (i = 0; i < 10000; i++) {
286 /* Yup, it's there, off we go... */
287 if (kill (am->shmem_hdr->vl_pid, 0) >= 0)
291 ts.tv_nsec = 10000*1000; /* 10 ms */
292 while (nanosleep(&ts, &tsrem) < 0)
297 am->vlib_rp = vlib_rp;
298 vec_add1(am->mapped_shmem_regions, vlib_rp);
301 /* Clients simply have to wait... */
303 pthread_mutex_unlock (&vlib_rp->mutex);
305 /* Wait up to 100 seconds... */
306 for (i = 0; i < 10000; i++) {
308 ts.tv_nsec = 10000*1000; /* 10 ms */
309 while (nanosleep(&ts, &tsrem) < 0)
311 if (vlib_rp->user_ctx)
314 /* Clean up and leave... */
315 svm_region_unmap (vlib_rp);
316 clib_warning ("region init fail");
320 am->shmem_hdr = (void *)vlib_rp->user_ctx;
321 am->our_pid = getpid();
322 am->vlib_rp = vlib_rp;
323 vec_add1(am->mapped_shmem_regions, vlib_rp);
327 /* Nope, it's our problem... */
329 oldheap = svm_push_data_heap (vlib_rp);
331 vec_validate(shmem_hdr, 0);
332 shmem_hdr->version = VL_SHM_VERSION;
334 /* vlib main input queue */
335 shmem_hdr->vl_input_queue =
336 unix_shared_memory_queue_init (1024, sizeof (uword), getpid(),
339 /* Set up the msg ring allocator */
343 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
348 vec_add1(shmem_hdr->vl_rings, _rp); \
351 foreach_vl_aring_size;
357 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
362 vec_add1(shmem_hdr->client_rings, _rp); \
365 foreach_clnt_aring_size;
368 am->shmem_hdr = shmem_hdr;
369 am->vlib_rp = vlib_rp;
370 am->our_pid = getpid();
372 am->shmem_hdr->vl_pid = am->our_pid;
374 svm_pop_heap (oldheap);
377 * After absolutely everything that a client might see is set up,
378 * declare the shmem region valid
380 vlib_rp->user_ctx = shmem_hdr;
382 pthread_mutex_unlock (&vlib_rp->mutex);
383 vec_add1(am->mapped_shmem_regions, vlib_rp);
387 void vl_register_mapped_shmem_region(svm_region_t *rp)
389 api_main_t *am = &api_main;
391 vec_add1(am->mapped_shmem_regions, rp);
394 void vl_unmap_shmem (void)
398 api_main_t *am = &api_main;
400 if (! svm_get_root_rp())
403 for (i = 0; i < vec_len(am->mapped_shmem_regions); i++) {
404 rp = am->mapped_shmem_regions[i];
405 svm_region_unmap (rp);
408 vec_free(am->mapped_shmem_regions);
412 /* $$$ more careful cleanup, valgrind run... */
413 vec_free (am->msg_handlers);
414 vec_free (am->msg_endian_handlers);
415 vec_free (am->msg_print_handlers);
418 void vl_msg_api_send_shmem (unix_shared_memory_queue_t *q, u8 *elem)
420 api_main_t *am = &api_main;
421 uword *trace = (uword *)elem;
423 if (am->tx_trace && am->tx_trace->enabled)
424 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
426 (void)unix_shared_memory_queue_add(q, elem, 0 /* nowait */);
429 void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t *q, u8 *elem)
431 api_main_t *am = &api_main;
432 uword *trace = (uword *)elem;
434 if (am->tx_trace && am->tx_trace->enabled)
435 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
437 (void)unix_shared_memory_queue_add_nolock (q, elem);
440 static void vl_api_memclnt_create_reply_t_handler (
441 vl_api_memclnt_create_reply_t *mp)
443 api_main_t *am = &api_main;
446 am->my_client_index = mp->index;
447 am->my_registration = (vl_api_registration_t *)(uword)
450 rv = ntohl(mp->response);
453 clib_warning ("WARNING: API mismatch detected");
456 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
457 __attribute__((weak));
459 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
463 for (i = 0; i < ARRAY_LEN(mp->api_versions); i++)
464 mp->api_versions[i] = 0;
467 int vl_client_connect (char *name, int ctx_quota, int input_queue_size)
470 vl_api_memclnt_create_t *mp;
471 vl_api_memclnt_create_reply_t *rp;
472 unix_shared_memory_queue_t *vl_input_queue;
473 vl_shmem_hdr_t *shmem_hdr;
476 api_main_t *am = &api_main;
478 if (am->my_registration) {
479 clib_warning ("client %s already connected...", name);
483 if (am->vlib_rp == 0) {
484 clib_warning ("am->vlib_rp NULL");
489 shmem_hdr = am->shmem_hdr;
491 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) {
492 clib_warning ("shmem_hdr / input queue NULL");
496 pthread_mutex_lock (&svm->mutex);
497 oldheap = svm_push_data_heap(svm);
499 unix_shared_memory_queue_init (input_queue_size, sizeof(uword),
501 pthread_mutex_unlock(&svm->mutex);
502 svm_pop_heap (oldheap);
504 am->my_client_index = ~0;
505 am->my_registration = 0;
506 am->vl_input_queue = vl_input_queue;
508 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_create_t));
509 memset(mp, 0, sizeof (*mp));
510 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_CREATE);
511 mp->ctx_quota = ctx_quota;
512 mp->input_queue = (uword)vl_input_queue;
513 strncpy ((char *) mp->name, name, sizeof(mp->name)-1);
515 vl_client_add_api_signatures(mp);
517 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
521 struct timespec ts, tsrem;
524 /* Wait up to 10 seconds */
525 for (i = 0; i < 1000; i++) {
526 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp,
531 ts.tv_nsec = 10000*1000; /* 10 ms */
532 while (nanosleep(&ts, &tsrem) < 0)
536 clib_warning ("memclnt_create_reply timeout");
540 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) {
541 clib_warning ("unexpected reply: id %d", ntohs(rp->_vl_msg_id));
544 rv = clib_net_to_host_u32(rp->response);
546 vl_msg_api_handler((void *)rp);
552 static void vl_api_memclnt_delete_reply_t_handler (
553 vl_api_memclnt_delete_reply_t *mp)
556 api_main_t *am = &api_main;
558 pthread_mutex_lock (&am->vlib_rp->mutex);
559 oldheap = svm_push_data_heap(am->vlib_rp);
560 unix_shared_memory_queue_free (am->vl_input_queue);
561 pthread_mutex_unlock (&am->vlib_rp->mutex);
562 svm_pop_heap (oldheap);
564 am->my_client_index = ~0;
565 am->my_registration = 0;
566 am->vl_input_queue = 0;
569 void vl_client_disconnect (void)
571 vl_api_memclnt_delete_t *mp;
572 vl_api_memclnt_delete_reply_t *rp;
573 unix_shared_memory_queue_t *vl_input_queue;
574 vl_shmem_hdr_t *shmem_hdr;
576 api_main_t *am = &api_main;
579 shmem_hdr = am->shmem_hdr;
580 ASSERT(shmem_hdr && shmem_hdr->vl_input_queue);
582 vl_input_queue = am->vl_input_queue;
584 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_delete_t));
585 memset(mp, 0, sizeof (*mp));
586 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_DELETE);
587 mp->index = am->my_client_index;
588 mp->handle = (uword) am->my_registration;
590 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
593 * Have to be careful here, in case the client is disconnecting
594 * because e.g. the vlib process died, or is unresponsive.
603 if (now >= (begin + 2)) {
604 clib_warning ("peer unresponsive, give up");
605 am->my_client_index = ~0;
606 am->my_registration = 0;
610 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp, 1) < 0)
613 /* drain the queue */
614 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) {
615 vl_msg_api_handler ((void *)rp);
618 vl_msg_api_handler((void *)rp);
623 static inline vl_api_registration_t
624 *vl_api_client_index_to_registration_internal (u32 handle)
626 vl_api_registration_t **regpp;
627 vl_api_registration_t *regp;
628 api_main_t *am = &api_main;
631 index = vl_msg_api_handle_get_index (handle);
632 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
633 != vl_msg_api_handle_get_epoch (handle)) {
634 vl_msg_api_increment_missing_client_counter();
638 regpp = am->vl_clients + index;
640 if (pool_is_free(am->vl_clients, regpp)) {
641 vl_msg_api_increment_missing_client_counter();
648 vl_api_registration_t *vl_api_client_index_to_registration (u32 index)
650 return (vl_api_client_index_to_registration_internal (index));
653 unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index)
655 vl_api_registration_t *regp;
657 regp = vl_api_client_index_to_registration_internal (index);
660 return (regp->vl_input_queue);
663 #define foreach_api_client_msg \
664 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
665 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
667 int vl_client_api_map (char *region_name)
671 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */)) < 0) {
676 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
677 vl_api_##n##_t_handler, \
678 0/* cleanup */, 0/* endian */, 0/* print */, \
679 sizeof(vl_api_##n##_t), 1);
680 foreach_api_client_msg;
685 void vl_client_api_unmap (void)