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 int vl_map_shmem (char *region_name, int is_vlib)
197 svm_map_region_args_t *a = 0;
198 svm_region_t *vlib_rp, *root_rp;
200 vl_shmem_hdr_t *shmem_hdr=0;
201 api_main_t *am = &api_main;
203 struct timespec ts, tsrem;
210 a->name = region_name;
212 a->flags = SVM_FLAGS_MHEAP;
214 vlib_rp = svm_region_find_or_create (a);
221 pthread_mutex_lock (&vlib_rp->mutex);
222 /* Has someone else set up the shared-memory variable table? */
223 if (vlib_rp->user_ctx) {
224 am->shmem_hdr = (void *) vlib_rp->user_ctx;
225 am->our_pid = getpid();
227 unix_shared_memory_queue_t *q;
230 * application restart. Reset cached pids, API message
231 * rings, list of clients; otherwise, various things
232 * fail. (e.g. queue non-empty notification)
235 /* ghosts keep the region from disappearing properly */
236 svm_client_scan_this_region_nolock(vlib_rp);
237 am->shmem_hdr->application_restarts++;
238 q = am->shmem_hdr->vl_input_queue;
239 am->shmem_hdr->vl_pid = getpid();
240 q->consumer_pid = am->shmem_hdr->vl_pid;
241 /* Drain the input queue, freeing msgs */
242 for (i = 0; i < 10; i++) {
243 if (pthread_mutex_trylock (&q->mutex) == 0) {
244 pthread_mutex_unlock (&q->mutex);
248 ts.tv_nsec = 10000*1000; /* 10 ms */
249 while (nanosleep(&ts, &tsrem) < 0)
252 /* Mutex buggered, "fix" it */
253 memset (&q->mutex, 0, sizeof (q->mutex));
254 clib_warning ("forcibly release main input queue mutex");
257 am->vlib_rp = vlib_rp;
258 while (unix_shared_memory_queue_sub (q,
261 != -2 /* queue underflow */) {
262 vl_msg_api_free_nolock ((void *)old_msg);
263 am->shmem_hdr->restart_reclaims++;
265 pthread_mutex_unlock (&vlib_rp->mutex);
266 root_rp = svm_get_root_rp();
268 /* Clean up the root region client list */
269 pthread_mutex_lock (&root_rp->mutex);
270 svm_client_scan_this_region_nolock (root_rp);
271 pthread_mutex_unlock (&root_rp->mutex);
273 pthread_mutex_unlock (&vlib_rp->mutex);
275 * Make sure the vlib app is really there...
276 * Wait up to 100 seconds...
278 for (i = 0; i < 10000; i++) {
279 /* Yup, it's there, off we go... */
280 if (kill (am->shmem_hdr->vl_pid, 0) >= 0)
284 ts.tv_nsec = 10000*1000; /* 10 ms */
285 while (nanosleep(&ts, &tsrem) < 0)
290 am->vlib_rp = vlib_rp;
291 vec_add1(am->mapped_shmem_regions, vlib_rp);
294 /* Clients simply have to wait... */
296 pthread_mutex_unlock (&vlib_rp->mutex);
298 /* Wait up to 100 seconds... */
299 for (i = 0; i < 10000; i++) {
301 ts.tv_nsec = 10000*1000; /* 10 ms */
302 while (nanosleep(&ts, &tsrem) < 0)
304 if (vlib_rp->user_ctx)
307 /* Clean up and leave... */
308 svm_region_unmap (vlib_rp);
309 clib_warning ("region init fail");
313 am->shmem_hdr = (void *)vlib_rp->user_ctx;
314 am->our_pid = getpid();
315 am->vlib_rp = vlib_rp;
316 vec_add1(am->mapped_shmem_regions, vlib_rp);
320 /* Nope, it's our problem... */
322 oldheap = svm_push_data_heap (vlib_rp);
324 vec_validate(shmem_hdr, 0);
325 shmem_hdr->version = VL_SHM_VERSION;
327 /* vlib main input queue */
328 shmem_hdr->vl_input_queue =
329 unix_shared_memory_queue_init (1024, sizeof (uword), getpid(),
332 /* Set up the msg ring allocator */
336 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
341 vec_add1(shmem_hdr->vl_rings, _rp); \
344 foreach_vl_aring_size;
350 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
355 vec_add1(shmem_hdr->client_rings, _rp); \
358 foreach_clnt_aring_size;
361 am->shmem_hdr = shmem_hdr;
362 am->vlib_rp = vlib_rp;
363 am->our_pid = getpid();
365 am->shmem_hdr->vl_pid = am->our_pid;
367 svm_pop_heap (oldheap);
370 * After absolutely everything that a client might see is set up,
371 * declare the shmem region valid
373 vlib_rp->user_ctx = shmem_hdr;
375 pthread_mutex_unlock (&vlib_rp->mutex);
376 vec_add1(am->mapped_shmem_regions, vlib_rp);
380 void vl_register_mapped_shmem_region(svm_region_t *rp)
382 api_main_t *am = &api_main;
384 vec_add1(am->mapped_shmem_regions, rp);
387 void vl_unmap_shmem (void)
391 api_main_t *am = &api_main;
393 if (! svm_get_root_rp())
396 for (i = 0; i < vec_len(am->mapped_shmem_regions); i++) {
397 rp = am->mapped_shmem_regions[i];
398 svm_region_unmap (rp);
401 vec_free(am->mapped_shmem_regions);
405 /* $$$ more careful cleanup, valgrind run... */
406 vec_free (am->msg_handlers);
407 vec_free (am->msg_endian_handlers);
408 vec_free (am->msg_print_handlers);
411 void vl_msg_api_send_shmem (unix_shared_memory_queue_t *q, u8 *elem)
413 api_main_t *am = &api_main;
414 uword *trace = (uword *)elem;
416 if (am->tx_trace && am->tx_trace->enabled)
417 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
419 (void)unix_shared_memory_queue_add(q, elem, 0 /* nowait */);
422 void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t *q, u8 *elem)
424 api_main_t *am = &api_main;
425 uword *trace = (uword *)elem;
427 if (am->tx_trace && am->tx_trace->enabled)
428 vl_msg_api_trace(am, am->tx_trace, (void *)trace[0]);
430 (void)unix_shared_memory_queue_add_nolock (q, elem);
433 static void vl_api_memclnt_create_reply_t_handler (
434 vl_api_memclnt_create_reply_t *mp)
436 api_main_t *am = &api_main;
439 am->my_client_index = mp->index;
440 am->my_registration = (vl_api_registration_t *)(uword)
443 rv = ntohl(mp->response);
446 clib_warning ("WARNING: API mismatch detected");
449 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
450 __attribute__((weak));
452 void vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
456 for (i = 0; i < ARRAY_LEN(mp->api_versions); i++)
457 mp->api_versions[i] = 0;
460 int vl_client_connect (char *name, int ctx_quota, int input_queue_size)
463 vl_api_memclnt_create_t *mp;
464 vl_api_memclnt_create_reply_t *rp;
465 unix_shared_memory_queue_t *vl_input_queue;
466 vl_shmem_hdr_t *shmem_hdr;
469 api_main_t *am = &api_main;
471 if (am->my_registration) {
472 clib_warning ("client %s already connected...", name);
476 if (am->vlib_rp == 0) {
477 clib_warning ("am->vlib_rp NULL");
482 shmem_hdr = am->shmem_hdr;
484 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) {
485 clib_warning ("shmem_hdr / input queue NULL");
489 pthread_mutex_lock (&svm->mutex);
490 oldheap = svm_push_data_heap(svm);
492 unix_shared_memory_queue_init (input_queue_size, sizeof(uword),
494 pthread_mutex_unlock(&svm->mutex);
495 svm_pop_heap (oldheap);
497 am->my_client_index = ~0;
498 am->my_registration = 0;
499 am->vl_input_queue = vl_input_queue;
501 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_create_t));
502 memset(mp, 0, sizeof (*mp));
503 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_CREATE);
504 mp->ctx_quota = ctx_quota;
505 mp->input_queue = (uword)vl_input_queue;
506 strncpy ((char *) mp->name, name, sizeof(mp->name)-1);
508 vl_client_add_api_signatures(mp);
510 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
514 struct timespec ts, tsrem;
517 /* Wait up to 10 seconds */
518 for (i = 0; i < 1000; i++) {
519 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp,
524 ts.tv_nsec = 10000*1000; /* 10 ms */
525 while (nanosleep(&ts, &tsrem) < 0)
529 clib_warning ("memclnt_create_reply timeout");
533 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) {
534 clib_warning ("unexpected reply: id %d", ntohs(rp->_vl_msg_id));
537 rv = clib_net_to_host_u32(rp->response);
539 vl_msg_api_handler((void *)rp);
545 static void vl_api_memclnt_delete_reply_t_handler (
546 vl_api_memclnt_delete_reply_t *mp)
549 api_main_t *am = &api_main;
551 pthread_mutex_lock (&am->vlib_rp->mutex);
552 oldheap = svm_push_data_heap(am->vlib_rp);
553 unix_shared_memory_queue_free (am->vl_input_queue);
554 pthread_mutex_unlock (&am->vlib_rp->mutex);
555 svm_pop_heap (oldheap);
557 am->my_client_index = ~0;
558 am->my_registration = 0;
559 am->vl_input_queue = 0;
562 void vl_client_disconnect (void)
564 vl_api_memclnt_delete_t *mp;
565 vl_api_memclnt_delete_reply_t *rp;
566 unix_shared_memory_queue_t *vl_input_queue;
567 vl_shmem_hdr_t *shmem_hdr;
569 api_main_t *am = &api_main;
572 shmem_hdr = am->shmem_hdr;
573 ASSERT(shmem_hdr && shmem_hdr->vl_input_queue);
575 vl_input_queue = am->vl_input_queue;
577 mp = vl_msg_api_alloc(sizeof(vl_api_memclnt_delete_t));
578 memset(mp, 0, sizeof (*mp));
579 mp->_vl_msg_id = ntohs(VL_API_MEMCLNT_DELETE);
580 mp->index = am->my_client_index;
581 mp->handle = (uword) am->my_registration;
583 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *)&mp);
586 * Have to be careful here, in case the client is disconnecting
587 * because e.g. the vlib process died, or is unresponsive.
596 if (now >= (begin + 2)) {
597 clib_warning ("peer unresponsive, give up");
598 am->my_client_index = ~0;
599 am->my_registration = 0;
603 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *)&rp, 1) < 0)
606 /* drain the queue */
607 if (ntohs(rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) {
608 vl_msg_api_handler ((void *)rp);
611 vl_msg_api_handler((void *)rp);
616 static inline vl_api_registration_t
617 *vl_api_client_index_to_registration_internal (u32 handle)
619 vl_api_registration_t **regpp;
620 vl_api_registration_t *regp;
621 api_main_t *am = &api_main;
624 index = vl_msg_api_handle_get_index (handle);
625 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
626 != vl_msg_api_handle_get_epoch (handle)) {
627 vl_msg_api_increment_missing_client_counter();
631 regpp = am->vl_clients + index;
633 if (pool_is_free(am->vl_clients, regpp)) {
634 vl_msg_api_increment_missing_client_counter();
641 vl_api_registration_t *vl_api_client_index_to_registration (u32 index)
643 return (vl_api_client_index_to_registration_internal (index));
646 unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index)
648 vl_api_registration_t *regp;
650 regp = vl_api_client_index_to_registration_internal (index);
653 return (regp->vl_input_queue);
656 #define foreach_api_client_msg \
657 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
658 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
660 int vl_client_api_map (char *region_name)
664 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */)) < 0) {
669 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
670 vl_api_##n##_t_handler, \
671 0/* cleanup */, 0/* endian */, 0/* print */, \
672 sizeof(vl_api_##n##_t), 1);
673 foreach_api_client_msg;
678 void vl_client_api_unmap (void)