Reorganize source tree to use single autotools instance
[vpp.git] / src / vlibmemory / memory_shared.c
1 /*
2  *------------------------------------------------------------------
3  * memclnt_shared.c - API message handling, common code for both clients
4  * and the vlib process itself.
5  *
6  *
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:
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *------------------------------------------------------------------
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <signal.h>
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>
35
36 #include <vlibmemory/vl_memory_msg_enum.h>
37
38 #define vl_typedefs
39 #include <vlibmemory/vl_memory_api_h.h>
40 #undef vl_typedefs
41
42 static inline void *
43 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
44 {
45   int i;
46   msgbuf_t *rv;
47   ring_alloc_t *ap;
48   unix_shared_memory_queue_t *q;
49   void *oldheap;
50   vl_shmem_hdr_t *shmem_hdr;
51   api_main_t *am = &api_main;
52
53   shmem_hdr = am->shmem_hdr;
54
55   if (shmem_hdr == 0)
56     {
57       clib_warning ("shared memory header NULL");
58       return 0;
59     }
60
61   /* account for the msgbuf_t header */
62   nbytes += sizeof (msgbuf_t);
63
64   if (shmem_hdr->vl_rings == 0)
65     {
66       clib_warning ("vl_rings NULL");
67       ASSERT (0);
68       abort ();
69     }
70
71   if (shmem_hdr->client_rings == 0)
72     {
73       clib_warning ("client_rings NULL");
74       ASSERT (0);
75       abort ();
76     }
77
78   ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
79   for (i = 0; i < vec_len (ap); i++)
80     {
81       /* Too big? */
82       if (nbytes > ap[i].size)
83         {
84           continue;
85         }
86
87       q = ap[i].rp;
88       if (pool == 0)
89         {
90           pthread_mutex_lock (&q->mutex);
91         }
92       rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
93       /*
94        * Is this item still in use?
95        */
96       if (rv->q)
97         {
98           /* yes, loser; try next larger pool */
99           ap[i].misses++;
100           if (pool == 0)
101             pthread_mutex_unlock (&q->mutex);
102           continue;
103         }
104       /* OK, we have a winner */
105       ap[i].hits++;
106       /*
107        * Remember the source queue, although we
108        * don't need to know the queue to free the item.
109        */
110       rv->q = q;
111       q->head++;
112       if (q->head == q->maxsize)
113         q->head = 0;
114
115       if (pool == 0)
116         pthread_mutex_unlock (&q->mutex);
117       goto out;
118     }
119
120   /*
121    * Request too big, or head element of all size-compatible rings
122    * still in use. Fall back to shared-memory malloc.
123    */
124   am->ring_misses++;
125
126   pthread_mutex_lock (&am->vlib_rp->mutex);
127   oldheap = svm_push_data_heap (am->vlib_rp);
128   if (may_return_null)
129     {
130       rv = clib_mem_alloc_or_null (nbytes);
131       if (PREDICT_FALSE (rv == 0))
132         {
133           svm_pop_heap (oldheap);
134           pthread_mutex_unlock (&am->vlib_rp->mutex);
135           return 0;
136         }
137     }
138   else
139     rv = clib_mem_alloc (nbytes);
140
141   rv->q = 0;
142   svm_pop_heap (oldheap);
143   pthread_mutex_unlock (&am->vlib_rp->mutex);
144
145 out:
146   rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
147   return (rv->data);
148 }
149
150 void *
151 vl_msg_api_alloc (int nbytes)
152 {
153   int pool;
154   api_main_t *am = &api_main;
155   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
156
157   /*
158    * Clients use pool-0, vlib proc uses pool 1
159    */
160   pool = (am->our_pid == shmem_hdr->vl_pid);
161   return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
162 }
163
164 void *
165 vl_msg_api_alloc_or_null (int nbytes)
166 {
167   int pool;
168   api_main_t *am = &api_main;
169   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
170
171   pool = (am->our_pid == shmem_hdr->vl_pid);
172   return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
173 }
174
175 void *
176 vl_msg_api_alloc_as_if_client (int nbytes)
177 {
178   return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
179 }
180
181 void *
182 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
183 {
184   return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
185 }
186
187 void
188 vl_msg_api_free (void *a)
189 {
190   msgbuf_t *rv;
191   void *oldheap;
192   api_main_t *am = &api_main;
193
194   rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
195
196   /*
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...
200    */
201   if (rv->q)
202     {
203       rv->q = 0;
204       return;
205     }
206
207   pthread_mutex_lock (&am->vlib_rp->mutex);
208   oldheap = svm_push_data_heap (am->vlib_rp);
209   clib_mem_free (rv);
210   svm_pop_heap (oldheap);
211   pthread_mutex_unlock (&am->vlib_rp->mutex);
212 }
213
214 static void
215 vl_msg_api_free_nolock (void *a)
216 {
217   msgbuf_t *rv;
218   void *oldheap;
219   api_main_t *am = &api_main;
220
221   rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
222   /*
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...
226    */
227   if (rv->q)
228     {
229       rv->q = 0;
230       return;
231     }
232
233   oldheap = svm_push_data_heap (am->vlib_rp);
234   clib_mem_free (rv);
235   svm_pop_heap (oldheap);
236 }
237
238 void
239 vl_set_memory_root_path (char *name)
240 {
241   api_main_t *am = &api_main;
242
243   am->root_path = name;
244 }
245
246 void
247 vl_set_memory_uid (int uid)
248 {
249   api_main_t *am = &api_main;
250
251   am->api_uid = uid;
252 }
253
254 void
255 vl_set_memory_gid (int gid)
256 {
257   api_main_t *am = &api_main;
258
259   am->api_gid = gid;
260 }
261
262 void
263 vl_set_global_memory_baseva (u64 baseva)
264 {
265   api_main_t *am = &api_main;
266
267   am->global_baseva = baseva;
268 }
269
270 void
271 vl_set_global_memory_size (u64 size)
272 {
273   api_main_t *am = &api_main;
274
275   am->global_size = size;
276 }
277
278 void
279 vl_set_api_memory_size (u64 size)
280 {
281   api_main_t *am = &api_main;
282
283   am->api_size = size;
284 }
285
286 void
287 vl_set_global_pvt_heap_size (u64 size)
288 {
289   api_main_t *am = &api_main;
290
291   am->global_pvt_heap_size = size;
292 }
293
294 void
295 vl_set_api_pvt_heap_size (u64 size)
296 {
297   api_main_t *am = &api_main;
298
299   am->api_pvt_heap_size = size;
300 }
301
302 int
303 vl_map_shmem (char *region_name, int is_vlib)
304 {
305   svm_map_region_args_t _a, *a = &_a;
306   svm_region_t *vlib_rp, *root_rp;
307   void *oldheap;
308   vl_shmem_hdr_t *shmem_hdr = 0;
309   api_main_t *am = &api_main;
310   int i;
311   struct timespec ts, tsrem;
312
313   if (is_vlib == 0)
314     svm_region_init_chroot (am->root_path);
315
316   memset (a, 0, sizeof (*a));
317
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;
324
325   vlib_rp = svm_region_find_or_create (a);
326
327   if (vlib_rp == 0)
328     return (-2);
329
330   pthread_mutex_lock (&vlib_rp->mutex);
331   /* Has someone else set up the shared-memory variable table? */
332   if (vlib_rp->user_ctx)
333     {
334       am->shmem_hdr = (void *) vlib_rp->user_ctx;
335       am->our_pid = getpid ();
336       if (is_vlib)
337         {
338           unix_shared_memory_queue_t *q;
339           uword old_msg;
340           /*
341            * application restart. Reset cached pids, API message
342            * rings, list of clients; otherwise, various things
343            * fail. (e.g. queue non-empty notification)
344            */
345
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++)
354             {
355               if (pthread_mutex_trylock (&q->mutex) == 0)
356                 {
357                   pthread_mutex_unlock (&q->mutex);
358                   goto mutex_ok;
359                 }
360               ts.tv_sec = 0;
361               ts.tv_nsec = 10000 * 1000;        /* 10 ms */
362               while (nanosleep (&ts, &tsrem) < 0)
363                 ts = tsrem;
364             }
365           /* Mutex buggered, "fix" it */
366           memset (&q->mutex, 0, sizeof (q->mutex));
367           clib_warning ("forcibly release main input queue mutex");
368
369         mutex_ok:
370           am->vlib_rp = vlib_rp;
371           while (unix_shared_memory_queue_sub (q,
372                                                (u8 *) & old_msg,
373                                                1 /* nowait */ )
374                  != -2 /* queue underflow */ )
375             {
376               vl_msg_api_free_nolock ((void *) old_msg);
377               am->shmem_hdr->restart_reclaims++;
378             }
379           pthread_mutex_unlock (&vlib_rp->mutex);
380           root_rp = svm_get_root_rp ();
381           ASSERT (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);
386         }
387       else
388         {
389           pthread_mutex_unlock (&vlib_rp->mutex);
390         }
391       am->vlib_rp = vlib_rp;
392       vec_add1 (am->mapped_shmem_regions, vlib_rp);
393       return 0;
394     }
395   /* Clients simply have to wait... */
396   if (!is_vlib)
397     {
398       pthread_mutex_unlock (&vlib_rp->mutex);
399
400       /* Wait up to 100 seconds... */
401       for (i = 0; i < 10000; i++)
402         {
403           ts.tv_sec = 0;
404           ts.tv_nsec = 10000 * 1000;    /* 10 ms */
405           while (nanosleep (&ts, &tsrem) < 0)
406             ts = tsrem;
407           if (vlib_rp->user_ctx)
408             goto ready;
409         }
410       /* Clean up and leave... */
411       svm_region_unmap (vlib_rp);
412       clib_warning ("region init fail");
413       return (-2);
414
415     ready:
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);
420       return 0;
421     }
422
423   /* Nope, it's our problem... */
424
425   oldheap = svm_push_data_heap (vlib_rp);
426
427   vec_validate (shmem_hdr, 0);
428   shmem_hdr->version = VL_SHM_VERSION;
429
430   /* vlib main input queue */
431   shmem_hdr->vl_input_queue =
432     unix_shared_memory_queue_init (1024, sizeof (uword), getpid (),
433                                    am->vlib_signal);
434
435   /* Set up the msg ring allocator */
436 #define _(sz,n)                                                 \
437     do {                                                        \
438         ring_alloc_t _rp;                                       \
439         _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
440         _rp.size = (sz);                                        \
441         _rp.nitems = n;                                         \
442         _rp.hits = 0;                                           \
443         _rp.misses = 0;                                         \
444         vec_add1(shmem_hdr->vl_rings, _rp);                     \
445     } while (0);
446
447   foreach_vl_aring_size;
448 #undef _
449
450 #define _(sz,n)                                                 \
451     do {                                                        \
452         ring_alloc_t _rp;                                       \
453         _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
454         _rp.size = (sz);                                        \
455         _rp.nitems = n;                                         \
456         _rp.hits = 0;                                           \
457         _rp.misses = 0;                                         \
458         vec_add1(shmem_hdr->client_rings, _rp);                 \
459     } while (0);
460
461   foreach_clnt_aring_size;
462 #undef _
463
464   am->shmem_hdr = shmem_hdr;
465   am->vlib_rp = vlib_rp;
466   am->our_pid = getpid ();
467   if (is_vlib)
468     am->shmem_hdr->vl_pid = am->our_pid;
469
470   svm_pop_heap (oldheap);
471
472   /*
473    * After absolutely everything that a client might see is set up,
474    * declare the shmem region valid
475    */
476   vlib_rp->user_ctx = shmem_hdr;
477
478   pthread_mutex_unlock (&vlib_rp->mutex);
479   vec_add1 (am->mapped_shmem_regions, vlib_rp);
480   return 0;
481 }
482
483 void
484 vl_register_mapped_shmem_region (svm_region_t * rp)
485 {
486   api_main_t *am = &api_main;
487
488   vec_add1 (am->mapped_shmem_regions, rp);
489 }
490
491 void
492 vl_unmap_shmem (void)
493 {
494   svm_region_t *rp;
495   int i;
496   api_main_t *am = &api_main;
497
498   if (!svm_get_root_rp ())
499     return;
500
501   for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
502     {
503       rp = am->mapped_shmem_regions[i];
504       svm_region_unmap (rp);
505     }
506
507   vec_free (am->mapped_shmem_regions);
508   am->shmem_hdr = 0;
509
510   svm_region_exit ();
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);
515 }
516
517 void
518 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
519 {
520   api_main_t *am = &api_main;
521   uword *trace = (uword *) elem;
522
523   if (am->tx_trace && am->tx_trace->enabled)
524     vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
525
526   (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
527 }
528
529 void
530 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
531 {
532   api_main_t *am = &api_main;
533   uword *trace = (uword *) elem;
534
535   if (am->tx_trace && am->tx_trace->enabled)
536     vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
537
538   (void) unix_shared_memory_queue_add_nolock (q, elem);
539 }
540
541 static void
542 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
543 {
544   serialize_main_t _sm, *sm = &_sm;
545   api_main_t *am = &api_main;
546   u8 *tblv;
547   u32 nmsgs;
548   int i;
549   u8 *name_and_crc;
550   u32 msg_index;
551
552   am->my_client_index = mp->index;
553   am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
554
555   /* Clean out any previous hash table (unlikely) */
556   if (am->msg_index_by_name_and_crc)
557     {
558       int i;
559       u8 **keys = 0;
560       hash_pair_t *hp;
561       /* *INDENT-OFF* */
562       hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
563       ({
564         vec_add1 (keys, (u8 *) hp->key);
565       }));
566       /* *INDENT-ON* */
567       for (i = 0; i < vec_len (keys); i++)
568         vec_free (keys[i]);
569       vec_free (keys);
570     }
571
572   am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
573
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));
578
579   for (i = 0; i < nmsgs; i++)
580     {
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);
584     }
585 }
586
587 u32
588 vl_api_get_msg_index (u8 * name_and_crc)
589 {
590   api_main_t *am = &api_main;
591   uword *p;
592
593   if (am->msg_index_by_name_and_crc)
594     {
595       p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
596       if (p)
597         return p[0];
598     }
599   return ~0;
600 }
601
602 int
603 vl_client_connect (char *name, int ctx_quota, int input_queue_size)
604 {
605   svm_region_t *svm;
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;
610   int rv = 0;
611   void *oldheap;
612   api_main_t *am = &api_main;
613
614   if (am->my_registration)
615     {
616       clib_warning ("client %s already connected...", name);
617       return -1;
618     }
619
620   if (am->vlib_rp == 0)
621     {
622       clib_warning ("am->vlib_rp NULL");
623       return -1;
624     }
625
626   svm = am->vlib_rp;
627   shmem_hdr = am->shmem_hdr;
628
629   if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
630     {
631       clib_warning ("shmem_hdr / input queue NULL");
632       return -1;
633     }
634
635   pthread_mutex_lock (&svm->mutex);
636   oldheap = svm_push_data_heap (svm);
637   vl_input_queue =
638     unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
639                                    getpid (), 0);
640   pthread_mutex_unlock (&svm->mutex);
641   svm_pop_heap (oldheap);
642
643   am->my_client_index = ~0;
644   am->my_registration = 0;
645   am->vl_input_queue = vl_input_queue;
646
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);
653
654   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
655
656   while (1)
657     {
658       int qstatus;
659       struct timespec ts, tsrem;
660       int i;
661
662       /* Wait up to 10 seconds */
663       for (i = 0; i < 1000; i++)
664         {
665           qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
666                                                   1 /* nowait */ );
667           if (qstatus == 0)
668             goto read_one_msg;
669           ts.tv_sec = 0;
670           ts.tv_nsec = 10000 * 1000;    /* 10 ms */
671           while (nanosleep (&ts, &tsrem) < 0)
672             ts = tsrem;
673         }
674       /* Timeout... */
675       clib_warning ("memclnt_create_reply timeout");
676       return -1;
677
678     read_one_msg:
679       if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
680         {
681           clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
682           continue;
683         }
684       rv = clib_net_to_host_u32 (rp->response);
685
686       vl_msg_api_handler ((void *) rp);
687       break;
688     }
689   return (rv);
690 }
691
692 static void
693 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
694 {
695   void *oldheap;
696   api_main_t *am = &api_main;
697
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);
703
704   am->my_client_index = ~0;
705   am->my_registration = 0;
706   am->vl_input_queue = 0;
707 }
708
709 void
710 vl_client_disconnect (void)
711 {
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;
716   time_t begin;
717   api_main_t *am = &api_main;
718
719   ASSERT (am->vlib_rp);
720   shmem_hdr = am->shmem_hdr;
721   ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
722
723   vl_input_queue = am->vl_input_queue;
724
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;
730
731   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
732
733   /*
734    * Have to be careful here, in case the client is disconnecting
735    * because e.g. the vlib process died, or is unresponsive.
736    */
737
738   begin = time (0);
739   while (1)
740     {
741       time_t now;
742
743       now = time (0);
744
745       if (now >= (begin + 2))
746         {
747           clib_warning ("peer unresponsive, give up");
748           am->my_client_index = ~0;
749           am->my_registration = 0;
750           am->shmem_hdr = 0;
751           break;
752         }
753       if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
754         continue;
755
756       /* drain the queue */
757       if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
758         {
759           vl_msg_api_handler ((void *) rp);
760           continue;
761         }
762       vl_msg_api_handler ((void *) rp);
763       break;
764     }
765 }
766
767 static inline vl_api_registration_t *
768 vl_api_client_index_to_registration_internal (u32 handle)
769 {
770   vl_api_registration_t **regpp;
771   vl_api_registration_t *regp;
772   api_main_t *am = &api_main;
773   u32 index;
774
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))
778     {
779       vl_msg_api_increment_missing_client_counter ();
780       return 0;
781     }
782
783   regpp = am->vl_clients + index;
784
785   if (pool_is_free (am->vl_clients, regpp))
786     {
787       vl_msg_api_increment_missing_client_counter ();
788       return 0;
789     }
790   regp = *regpp;
791   return (regp);
792 }
793
794 vl_api_registration_t *
795 vl_api_client_index_to_registration (u32 index)
796 {
797   return (vl_api_client_index_to_registration_internal (index));
798 }
799
800 unix_shared_memory_queue_t *
801 vl_api_client_index_to_input_queue (u32 index)
802 {
803   vl_api_registration_t *regp;
804   api_main_t *am = &api_main;
805
806   /* Special case: vlib trying to send itself a message */
807   if (index == (u32) ~ 0)
808     return (am->shmem_hdr->vl_input_queue);
809
810   regp = vl_api_client_index_to_registration_internal (index);
811   if (!regp)
812     return 0;
813   return (regp->vl_input_queue);
814 }
815
816 #define foreach_api_client_msg                  \
817 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply)   \
818 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
819
820 int
821 vl_client_api_map (char *region_name)
822 {
823   int rv;
824
825   if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
826     {
827       return rv;
828     }
829
830 #define _(N,n)                                                          \
831     vl_msg_api_set_handlers(VL_API_##N, 0 /* name */,                   \
832                            vl_api_##n##_t_handler,                      \
833                            0/* cleanup */, 0/* endian */, 0/* print */, \
834                            sizeof(vl_api_##n##_t), 1);
835   foreach_api_client_msg;
836 #undef _
837   return 0;
838 }
839
840 void
841 vl_client_api_unmap (void)
842 {
843   vl_unmap_shmem ();
844 }
845
846 /*
847  * fd.io coding-style-patch-verification: ON
848  *
849  * Local Variables:
850  * eval: (c-set-style "gnu")
851  * End:
852  */