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_map_shmem (char *region_name, int is_vlib)
234 svm_map_region_args_t _a, *a = &_a;
235 svm_region_t *vlib_rp, *root_rp;
237 vl_shmem_hdr_t *shmem_hdr = 0;
238 api_main_t *am = &api_main;
240 struct timespec ts, tsrem;
243 svm_region_init_chroot (am->root_path);
245 memset (a, 0, sizeof (*a));
247 a->name = region_name;
249 a->flags = SVM_FLAGS_MHEAP;
250 a->uid = am->api_uid;
251 a->gid = am->api_gid;
253 vlib_rp = svm_region_find_or_create (a);
258 pthread_mutex_lock (&vlib_rp->mutex);
259 /* Has someone else set up the shared-memory variable table? */
260 if (vlib_rp->user_ctx)
262 am->shmem_hdr = (void *) vlib_rp->user_ctx;
263 am->our_pid = getpid ();
266 unix_shared_memory_queue_t *q;
269 * application restart. Reset cached pids, API message
270 * rings, list of clients; otherwise, various things
271 * fail. (e.g. queue non-empty notification)
274 /* ghosts keep the region from disappearing properly */
275 svm_client_scan_this_region_nolock (vlib_rp);
276 am->shmem_hdr->application_restarts++;
277 q = am->shmem_hdr->vl_input_queue;
278 am->shmem_hdr->vl_pid = getpid ();
279 q->consumer_pid = am->shmem_hdr->vl_pid;
280 /* Drain the input queue, freeing msgs */
281 for (i = 0; i < 10; i++)
283 if (pthread_mutex_trylock (&q->mutex) == 0)
285 pthread_mutex_unlock (&q->mutex);
289 ts.tv_nsec = 10000 * 1000; /* 10 ms */
290 while (nanosleep (&ts, &tsrem) < 0)
293 /* Mutex buggered, "fix" it */
294 memset (&q->mutex, 0, sizeof (q->mutex));
295 clib_warning ("forcibly release main input queue mutex");
298 am->vlib_rp = vlib_rp;
299 while (unix_shared_memory_queue_sub (q,
302 != -2 /* queue underflow */ )
304 vl_msg_api_free_nolock ((void *) old_msg);
305 am->shmem_hdr->restart_reclaims++;
307 pthread_mutex_unlock (&vlib_rp->mutex);
308 root_rp = svm_get_root_rp ();
310 /* Clean up the root region client list */
311 pthread_mutex_lock (&root_rp->mutex);
312 svm_client_scan_this_region_nolock (root_rp);
313 pthread_mutex_unlock (&root_rp->mutex);
317 pthread_mutex_unlock (&vlib_rp->mutex);
319 am->vlib_rp = vlib_rp;
320 vec_add1 (am->mapped_shmem_regions, vlib_rp);
323 /* Clients simply have to wait... */
326 pthread_mutex_unlock (&vlib_rp->mutex);
328 /* Wait up to 100 seconds... */
329 for (i = 0; i < 10000; i++)
332 ts.tv_nsec = 10000 * 1000; /* 10 ms */
333 while (nanosleep (&ts, &tsrem) < 0)
335 if (vlib_rp->user_ctx)
338 /* Clean up and leave... */
339 svm_region_unmap (vlib_rp);
340 clib_warning ("region init fail");
344 am->shmem_hdr = (void *) vlib_rp->user_ctx;
345 am->our_pid = getpid ();
346 am->vlib_rp = vlib_rp;
347 vec_add1 (am->mapped_shmem_regions, vlib_rp);
351 /* Nope, it's our problem... */
353 oldheap = svm_push_data_heap (vlib_rp);
355 vec_validate (shmem_hdr, 0);
356 shmem_hdr->version = VL_SHM_VERSION;
358 /* vlib main input queue */
359 shmem_hdr->vl_input_queue =
360 unix_shared_memory_queue_init (1024, sizeof (uword), getpid (),
363 /* Set up the msg ring allocator */
367 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
372 vec_add1(shmem_hdr->vl_rings, _rp); \
375 foreach_vl_aring_size;
381 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
386 vec_add1(shmem_hdr->client_rings, _rp); \
389 foreach_clnt_aring_size;
392 am->shmem_hdr = shmem_hdr;
393 am->vlib_rp = vlib_rp;
394 am->our_pid = getpid ();
396 am->shmem_hdr->vl_pid = am->our_pid;
398 svm_pop_heap (oldheap);
401 * After absolutely everything that a client might see is set up,
402 * declare the shmem region valid
404 vlib_rp->user_ctx = shmem_hdr;
406 pthread_mutex_unlock (&vlib_rp->mutex);
407 vec_add1 (am->mapped_shmem_regions, vlib_rp);
412 vl_register_mapped_shmem_region (svm_region_t * rp)
414 api_main_t *am = &api_main;
416 vec_add1 (am->mapped_shmem_regions, rp);
420 vl_unmap_shmem (void)
424 api_main_t *am = &api_main;
426 if (!svm_get_root_rp ())
429 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
431 rp = am->mapped_shmem_regions[i];
432 svm_region_unmap (rp);
435 vec_free (am->mapped_shmem_regions);
439 /* $$$ more careful cleanup, valgrind run... */
440 vec_free (am->msg_handlers);
441 vec_free (am->msg_endian_handlers);
442 vec_free (am->msg_print_handlers);
446 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
448 api_main_t *am = &api_main;
449 uword *trace = (uword *) elem;
451 if (am->tx_trace && am->tx_trace->enabled)
452 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
454 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
458 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
460 api_main_t *am = &api_main;
461 uword *trace = (uword *) elem;
463 if (am->tx_trace && am->tx_trace->enabled)
464 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
466 (void) unix_shared_memory_queue_add_nolock (q, elem);
470 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
472 api_main_t *am = &api_main;
475 am->my_client_index = mp->index;
476 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
478 rv = ntohl (mp->response);
481 clib_warning ("WARNING: API mismatch detected");
484 void vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
485 __attribute__ ((weak));
488 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
492 for (i = 0; i < ARRAY_LEN (mp->api_versions); i++)
493 mp->api_versions[i] = 0;
497 vl_client_connect (char *name, int ctx_quota, int input_queue_size)
500 vl_api_memclnt_create_t *mp;
501 vl_api_memclnt_create_reply_t *rp;
502 unix_shared_memory_queue_t *vl_input_queue;
503 vl_shmem_hdr_t *shmem_hdr;
506 api_main_t *am = &api_main;
508 if (am->my_registration)
510 clib_warning ("client %s already connected...", name);
514 if (am->vlib_rp == 0)
516 clib_warning ("am->vlib_rp NULL");
521 shmem_hdr = am->shmem_hdr;
523 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
525 clib_warning ("shmem_hdr / input queue NULL");
529 pthread_mutex_lock (&svm->mutex);
530 oldheap = svm_push_data_heap (svm);
532 unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
534 pthread_mutex_unlock (&svm->mutex);
535 svm_pop_heap (oldheap);
537 am->my_client_index = ~0;
538 am->my_registration = 0;
539 am->vl_input_queue = vl_input_queue;
541 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
542 memset (mp, 0, sizeof (*mp));
543 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
544 mp->ctx_quota = ctx_quota;
545 mp->input_queue = (uword) vl_input_queue;
546 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
548 vl_client_add_api_signatures (mp);
550 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
555 struct timespec ts, tsrem;
558 /* Wait up to 10 seconds */
559 for (i = 0; i < 1000; i++)
561 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
566 ts.tv_nsec = 10000 * 1000; /* 10 ms */
567 while (nanosleep (&ts, &tsrem) < 0)
571 clib_warning ("memclnt_create_reply timeout");
575 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
577 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
580 rv = clib_net_to_host_u32 (rp->response);
582 vl_msg_api_handler ((void *) rp);
589 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
592 api_main_t *am = &api_main;
594 pthread_mutex_lock (&am->vlib_rp->mutex);
595 oldheap = svm_push_data_heap (am->vlib_rp);
596 unix_shared_memory_queue_free (am->vl_input_queue);
597 pthread_mutex_unlock (&am->vlib_rp->mutex);
598 svm_pop_heap (oldheap);
600 am->my_client_index = ~0;
601 am->my_registration = 0;
602 am->vl_input_queue = 0;
606 vl_client_disconnect (void)
608 vl_api_memclnt_delete_t *mp;
609 vl_api_memclnt_delete_reply_t *rp;
610 unix_shared_memory_queue_t *vl_input_queue;
611 vl_shmem_hdr_t *shmem_hdr;
613 api_main_t *am = &api_main;
615 ASSERT (am->vlib_rp);
616 shmem_hdr = am->shmem_hdr;
617 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
619 vl_input_queue = am->vl_input_queue;
621 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
622 memset (mp, 0, sizeof (*mp));
623 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
624 mp->index = am->my_client_index;
625 mp->handle = (uword) am->my_registration;
627 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
630 * Have to be careful here, in case the client is disconnecting
631 * because e.g. the vlib process died, or is unresponsive.
641 if (now >= (begin + 2))
643 clib_warning ("peer unresponsive, give up");
644 am->my_client_index = ~0;
645 am->my_registration = 0;
649 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
652 /* drain the queue */
653 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
655 vl_msg_api_handler ((void *) rp);
658 vl_msg_api_handler ((void *) rp);
663 static inline vl_api_registration_t *
664 vl_api_client_index_to_registration_internal (u32 handle)
666 vl_api_registration_t **regpp;
667 vl_api_registration_t *regp;
668 api_main_t *am = &api_main;
671 index = vl_msg_api_handle_get_index (handle);
672 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
673 != vl_msg_api_handle_get_epoch (handle))
675 vl_msg_api_increment_missing_client_counter ();
679 regpp = am->vl_clients + index;
681 if (pool_is_free (am->vl_clients, regpp))
683 vl_msg_api_increment_missing_client_counter ();
690 vl_api_registration_t *
691 vl_api_client_index_to_registration (u32 index)
693 return (vl_api_client_index_to_registration_internal (index));
696 unix_shared_memory_queue_t *
697 vl_api_client_index_to_input_queue (u32 index)
699 vl_api_registration_t *regp;
701 regp = vl_api_client_index_to_registration_internal (index);
704 return (regp->vl_input_queue);
707 #define foreach_api_client_msg \
708 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
709 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
712 vl_client_api_map (char *region_name)
716 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
722 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
723 vl_api_##n##_t_handler, \
724 0/* cleanup */, 0/* endian */, 0/* print */, \
725 sizeof(vl_api_##n##_t), 1);
726 foreach_api_client_msg;
732 vl_client_api_unmap (void)
738 * fd.io coding-style-patch-verification: ON
741 * eval: (c-set-style "gnu")