b9c81a1307a62a72891748851be8621bc6747147
[vpp.git] / src / vpp-api / vapi / vapi.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <arpa/inet.h>
22 #include <stddef.h>
23 #include <assert.h>
24
25 #include <vpp-api/vapi/vapi_dbg.h>
26 #include <vpp-api/vapi/vapi.h>
27 #include <vpp-api/vapi/vapi_internal.h>
28 #include <vppinfra/types.h>
29 #include <vlibapi/api_common.h>
30 #include <vlibmemory/api_common.h>
31
32 /* we need to use control pings for some stuff and because we're forced to put
33  * the code in headers, we need a way to be able to grab the ids of these
34  * messages - so declare them here as extern */
35 vapi_msg_id_t vapi_msg_id_control_ping = 0;
36 vapi_msg_id_t vapi_msg_id_control_ping_reply = 0;
37
38 struct
39 {
40   size_t count;
41   vapi_message_desc_t **msgs;
42   size_t max_len_name_with_crc;
43 } __vapi_metadata;
44
45 typedef struct
46 {
47   u32 context;
48   vapi_cb_t callback;
49   void *callback_ctx;
50   bool is_dump;
51 } vapi_req_t;
52
53 static const u32 context_counter_mask = (1 << 31);
54
55 typedef struct
56 {
57   vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id,
58                       void *payload);
59   void *ctx;
60 } vapi_generic_cb_with_ctx;
61
62 typedef struct
63 {
64   vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, void *payload);
65   void *ctx;
66 } vapi_event_cb_with_ctx;
67
68 struct vapi_ctx_s
69 {
70   vapi_mode_e mode;
71   int requests_size;            /* size of the requests array (circular queue) */
72   int requests_start;           /* index of first request */
73   int requests_count;           /* number of used slots */
74   vapi_req_t *requests;
75   u32 context_counter;
76   vapi_generic_cb_with_ctx generic_cb;
77   vapi_event_cb_with_ctx *event_cbs;
78   u16 *vapi_msg_id_t_to_vl_msg_id;
79   u16 vl_msg_id_max;
80   vapi_msg_id_t *vl_msg_id_to_vapi_msg_t;
81   bool connected;
82   pthread_mutex_t requests_mutex;
83 };
84
85 u32
86 vapi_gen_req_context (vapi_ctx_t ctx)
87 {
88   ++ctx->context_counter;
89   ctx->context_counter %= context_counter_mask;
90   return ctx->context_counter | context_counter_mask;
91 }
92
93 size_t
94 vapi_get_request_count (vapi_ctx_t ctx)
95 {
96   return ctx->requests_count;
97 }
98
99 bool
100 vapi_requests_full (vapi_ctx_t ctx)
101 {
102   return (ctx->requests_count == ctx->requests_size);
103 }
104
105 static bool
106 vapi_requests_empty (vapi_ctx_t ctx)
107 {
108   return (0 == ctx->requests_count);
109 }
110
111 static int
112 vapi_requests_end (vapi_ctx_t ctx)
113 {
114   return (ctx->requests_start + ctx->requests_count) % ctx->requests_size;
115 }
116
117 void
118 vapi_store_request (vapi_ctx_t ctx, u32 context, bool is_dump,
119                     vapi_cb_t callback, void *callback_ctx)
120 {
121   assert (!vapi_requests_full (ctx));
122   /* if the mutex is not held, bad things will happen */
123   assert (0 != pthread_mutex_trylock (&ctx->requests_mutex));
124   const int requests_end = vapi_requests_end (ctx);
125   vapi_req_t *slot = &ctx->requests[requests_end];
126   slot->is_dump = is_dump;
127   slot->context = context;
128   slot->callback = callback;
129   slot->callback_ctx = callback_ctx;
130   VAPI_DBG ("stored@%d: context:%x (start is @%d)", requests_end, context,
131             ctx->requests_start);
132   ++ctx->requests_count;
133   assert (!vapi_requests_empty (ctx));
134 }
135
136 #if VAPI_DEBUG_ALLOC
137 struct to_be_freed_s;
138 struct to_be_freed_s
139 {
140   void *v;
141   struct to_be_freed_s *next;
142 };
143
144 static struct to_be_freed_s *to_be_freed = NULL;
145
146 void
147 vapi_add_to_be_freed (void *v)
148 {
149   struct to_be_freed_s *prev = NULL;
150   struct to_be_freed_s *tmp;
151   tmp = to_be_freed;
152   while (tmp && tmp->v)
153     {
154       prev = tmp;
155       tmp = tmp->next;
156     }
157   if (!tmp)
158     {
159       if (!prev)
160         {
161           tmp = to_be_freed = calloc (1, sizeof (*to_be_freed));
162         }
163       else
164         {
165           tmp = prev->next = calloc (1, sizeof (*to_be_freed));
166         }
167     }
168   VAPI_DBG ("To be freed %p", v);
169   tmp->v = v;
170 }
171
172 void
173 vapi_trace_free (void *v)
174 {
175   struct to_be_freed_s *tmp = to_be_freed;
176   while (tmp && tmp->v != v)
177     {
178       tmp = tmp->next;
179     }
180   if (tmp && tmp->v == v)
181     {
182       VAPI_DBG ("Freed %p", v);
183       tmp->v = NULL;
184     }
185   else
186     {
187       VAPI_ERR ("Trying to free untracked pointer %p", v);
188       abort ();
189     }
190 }
191
192 void
193 vapi_to_be_freed_validate ()
194 {
195   struct to_be_freed_s *tmp = to_be_freed;
196   while (tmp)
197     {
198       if (tmp->v)
199         {
200           VAPI_ERR ("Unfreed msg %p!", tmp->v);
201         }
202       tmp = tmp->next;
203     }
204 }
205
206 #endif
207
208 void *
209 vapi_msg_alloc (vapi_ctx_t ctx, size_t size)
210 {
211   if (!ctx->connected)
212     {
213       return NULL;
214     }
215   void *rv = vl_msg_api_alloc_or_null (size);
216   return rv;
217 }
218
219 void
220 vapi_msg_free (vapi_ctx_t ctx, void *msg)
221 {
222   if (!ctx->connected)
223     {
224       return;
225     }
226 #if VAPI_DEBUG_ALLOC
227   vapi_trace_free (msg);
228 #endif
229   vl_msg_api_free (msg);
230 }
231
232 vapi_error_e
233 vapi_ctx_alloc (vapi_ctx_t * result)
234 {
235   vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
236   if (!ctx)
237     {
238       return VAPI_ENOMEM;
239     }
240   ctx->context_counter = 0;
241   ctx->vapi_msg_id_t_to_vl_msg_id =
242     malloc (__vapi_metadata.count *
243             sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
244   if (!ctx->vapi_msg_id_t_to_vl_msg_id)
245     {
246       goto fail;
247     }
248   ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
249   if (!ctx->event_cbs)
250     {
251       goto fail;
252     }
253   pthread_mutex_init (&ctx->requests_mutex, NULL);
254   *result = ctx;
255   return VAPI_OK;
256 fail:
257   vapi_ctx_free (ctx);
258   return VAPI_ENOMEM;
259 }
260
261 void
262 vapi_ctx_free (vapi_ctx_t ctx)
263 {
264   assert (!ctx->connected);
265   free (ctx->requests);
266   free (ctx->vapi_msg_id_t_to_vl_msg_id);
267   free (ctx->event_cbs);
268   free (ctx->vl_msg_id_to_vapi_msg_t);
269   pthread_mutex_destroy (&ctx->requests_mutex);
270   free (ctx);
271 }
272
273 bool
274 vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t id)
275 {
276   return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
277 }
278
279 vapi_error_e
280 vapi_connect (vapi_ctx_t ctx, const char *name,
281               const char *chroot_prefix,
282               int max_outstanding_requests,
283               int response_queue_size, vapi_mode_e mode)
284 {
285   if (response_queue_size <= 0 || max_outstanding_requests <= 0)
286     {
287       return VAPI_EINVAL;
288     }
289   ctx->requests_size = max_outstanding_requests;
290   const size_t size = ctx->requests_size * sizeof (*ctx->requests);
291   void *tmp = realloc (ctx->requests, size);
292   if (!tmp)
293     {
294       return VAPI_ENOMEM;
295     }
296   ctx->requests = tmp;
297   memset (ctx->requests, 0, size);
298   ctx->requests_start = ctx->requests_count = 0;
299   if (chroot_prefix)
300     {
301       VAPI_DBG ("set memory root path `%s'", chroot_prefix);
302       vl_set_memory_root_path ((char *) chroot_prefix);
303     }
304   static char api_map[] = "/vpe-api";
305   VAPI_DBG ("client api map `%s'", api_map);
306   if ((vl_client_api_map (api_map)) < 0)
307     {
308       return VAPI_EMAP_FAIL;
309     }
310   VAPI_DBG ("connect client `%s'", name);
311   if (vl_client_connect ((char *) name, 0, response_queue_size) < 0)
312     {
313       vl_client_api_unmap ();
314       return VAPI_ECON_FAIL;
315     }
316 #if VAPI_DEBUG_CONNECT
317   VAPI_DBG ("start probing messages");
318 #endif
319   int rv;
320   int i;
321   for (i = 0; i < __vapi_metadata.count; ++i)
322     {
323       vapi_message_desc_t *m = __vapi_metadata.msgs[i];
324       u8 scratch[m->name_with_crc_len + 1];
325       memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
326       u32 id = vl_api_get_msg_index (scratch);
327       if (~0 != id)
328         {
329           if (id > UINT16_MAX)
330             {
331               VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
332                         UINT16_MAX);
333               rv = VAPI_EINVAL;
334               goto fail;
335             }
336           if (id > ctx->vl_msg_id_max)
337             {
338               vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t,
339                                             sizeof
340                                             (*ctx->vl_msg_id_to_vapi_msg_t) *
341                                             (id + 1));
342               if (!tmp)
343                 {
344                   rv = VAPI_ENOMEM;
345                   goto fail;
346                 }
347               ctx->vl_msg_id_to_vapi_msg_t = tmp;
348               ctx->vl_msg_id_max = id;
349             }
350           ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
351           ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
352 #if VAPI_DEBUG_CONNECT
353           VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
354                     (unsigned) id);
355 #endif
356         }
357       else
358         {
359           ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
360           VAPI_DBG ("Message `%s' not available", m->name_with_crc);
361         }
362     }
363 #if VAPI_DEBUG_CONNECT
364   VAPI_DBG ("finished probing messages");
365 #endif
366   if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
367       !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
368     {
369       VAPI_ERR
370         ("control ping or control ping reply not available, cannot connect");
371       rv = VAPI_EINCOMPATIBLE;
372       goto fail;
373     }
374   ctx->mode = mode;
375   ctx->connected = true;
376   return VAPI_OK;
377 fail:
378   vl_client_disconnect ();
379   vl_client_api_unmap ();
380   return rv;
381 }
382
383 vapi_error_e
384 vapi_disconnect (vapi_ctx_t ctx)
385 {
386   if (!ctx->connected)
387     {
388       return VAPI_EINVAL;
389     }
390   vl_client_disconnect ();
391   vl_client_api_unmap ();
392 #if VAPI_DEBUG_ALLOC
393   vapi_to_be_freed_validate ();
394 #endif
395   ctx->connected = false;
396   return VAPI_OK;
397 }
398
399 vapi_error_e
400 vapi_get_fd (vapi_ctx_t ctx, int *fd)
401 {
402   return VAPI_ENOTSUP;
403 }
404
405 vapi_error_e
406 vapi_send (vapi_ctx_t ctx, void *msg)
407 {
408   vapi_error_e rv = VAPI_OK;
409   if (!ctx || !msg || !ctx->connected)
410     {
411       rv = VAPI_EINVAL;
412       goto out;
413     }
414   int tmp;
415   unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue;
416 #if VAPI_DEBUG
417   unsigned msgid = be16toh (*(u16 *) msg);
418   if (msgid <= ctx->vl_msg_id_max)
419     {
420       vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
421       if (id < __vapi_metadata.count)
422         {
423           VAPI_DBG ("send msg %u[%s]", msgid, __vapi_metadata.msgs[id]->name);
424         }
425       else
426         {
427           VAPI_DBG ("send msg %u[UNKNOWN]", msgid);
428         }
429     }
430   else
431     {
432       VAPI_DBG ("send msg %u[UNKNOWN]", msgid);
433     }
434 #endif
435   tmp = unix_shared_memory_queue_add (q, (u8 *) & msg,
436                                       VAPI_MODE_BLOCKING ==
437                                       ctx->mode ? 0 : 1);
438   if (tmp < 0)
439     {
440       rv = VAPI_EAGAIN;
441     }
442 out:
443   VAPI_DBG ("vapi_send() rv = %d", rv);
444   return rv;
445 }
446
447 vapi_error_e
448 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
449 {
450   vapi_error_e rv = VAPI_OK;
451   if (!ctx || !msg1 || !msg2 || !ctx->connected)
452     {
453       rv = VAPI_EINVAL;
454       goto out;
455     }
456   unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue;
457 #if VAPI_DEBUG
458   unsigned msgid1 = be16toh (*(u16 *) msg1);
459   unsigned msgid2 = be16toh (*(u16 *) msg2);
460   const char *name1 = "UNKNOWN";
461   const char *name2 = "UNKNOWN";
462   if (msgid1 <= ctx->vl_msg_id_max)
463     {
464       vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
465       if (id < __vapi_metadata.count)
466         {
467           name1 = __vapi_metadata.msgs[id]->name;
468         }
469     }
470   if (msgid2 <= ctx->vl_msg_id_max)
471     {
472       vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
473       if (id < __vapi_metadata.count)
474         {
475           name2 = __vapi_metadata.msgs[id]->name;
476         }
477     }
478   VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
479 #endif
480   int tmp = unix_shared_memory_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
481                                            VAPI_MODE_BLOCKING ==
482                                            ctx->mode ? 0 : 1);
483   if (tmp < 0)
484     {
485       rv = VAPI_EAGAIN;
486     }
487 out:
488   VAPI_DBG ("vapi_send() rv = %d", rv);
489   return rv;
490 }
491
492 vapi_error_e
493 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size)
494 {
495   if (!ctx || !ctx->connected || !msg || !msg_size)
496     {
497       return VAPI_EINVAL;
498     }
499   vapi_error_e rv = VAPI_OK;
500   api_main_t *am = &api_main;
501   uword data;
502
503   if (am->our_pid == 0)
504     {
505       return VAPI_EINVAL;
506     }
507
508   unix_shared_memory_queue_t *q = am->vl_input_queue;
509   VAPI_DBG ("doing shm queue sub");
510   int tmp = unix_shared_memory_queue_sub (q, (u8 *) & data, 0);
511   if (tmp == 0)
512     {
513 #if VAPI_DEBUG_ALLOC
514       vapi_add_to_be_freed ((void *) data);
515 #endif
516       msgbuf_t *msgbuf =
517         (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
518       if (!msgbuf->data_len)
519         {
520           vapi_msg_free (ctx, (u8 *) data);
521           return VAPI_EAGAIN;
522         }
523       *msg = (u8 *) data;
524       *msg_size = ntohl (msgbuf->data_len);
525       VAPI_DBG ("recv msg %p", *msg);
526     }
527   else
528     {
529       rv = VAPI_EAGAIN;
530     }
531   return rv;
532 }
533
534 vapi_error_e
535 vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode)
536 {
537   /* FIXME */
538   return VAPI_ENOTSUP;
539 }
540
541 static vapi_error_e
542 vapi_dispatch_response (vapi_ctx_t ctx, vapi_msg_id_t id,
543                         u32 context, void *msg)
544 {
545   int mrv;
546   if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
547     {
548       VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
549       return VAPI_MUTEX_FAILURE;
550     }
551   int tmp = ctx->requests_start;
552   const int requests_end = vapi_requests_end (ctx);
553   while (ctx->requests[tmp].context != context && tmp != requests_end)
554     {
555       ++tmp;
556       if (tmp == ctx->requests_size)
557         {
558           tmp = 0;
559         }
560     }
561   VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
562             ctx->requests[tmp].context == context ? "matched" : "stopped",
563             tmp);
564   vapi_error_e rv = VAPI_OK;
565   if (ctx->requests[tmp].context == context)
566     {
567       while (ctx->requests_start != tmp)
568         {
569           VAPI_ERR ("No response to req with context=%u",
570                     (unsigned) ctx->requests[tmp].context);
571           ctx->requests[ctx->requests_start].callback (ctx,
572                                                        ctx->requests
573                                                        [ctx->
574                                                         requests_start].callback_ctx,
575                                                        VAPI_ENORESP, true,
576                                                        NULL);
577           memset (&ctx->requests[ctx->requests_start], 0,
578                   sizeof (ctx->requests[ctx->requests_start]));
579           ++ctx->requests_start;
580           --ctx->requests_count;
581           if (ctx->requests_start == ctx->requests_size)
582             {
583               ctx->requests_start = 0;
584             }
585         }
586       // now ctx->requests_start == tmp
587       int payload_offset = vapi_get_payload_offset (id);
588       void *payload = ((u8 *) msg) + payload_offset;
589       bool is_last = true;
590       if (ctx->requests[tmp].is_dump)
591         {
592           if (vapi_msg_id_control_ping_reply == id)
593             {
594               payload = NULL;
595             }
596           else
597             {
598               is_last = false;
599             }
600         }
601       if (payload_offset != -1)
602         {
603           rv =
604             ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx,
605                                          VAPI_OK, is_last, payload);
606         }
607       else
608         {
609           /* this is a message without payload, so bend the callback a little
610            */
611           rv =
612             ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
613              ctx->requests[tmp].callback) (ctx,
614                                            ctx->requests[tmp].callback_ctx,
615                                            VAPI_OK, is_last);
616         }
617       if (is_last)
618         {
619           memset (&ctx->requests[ctx->requests_start], 0,
620                   sizeof (ctx->requests[ctx->requests_start]));
621           ++ctx->requests_start;
622           --ctx->requests_count;
623           if (ctx->requests_start == ctx->requests_size)
624             {
625               ctx->requests_start = 0;
626             }
627         }
628       VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
629                 ctx->requests_start, requests_end, ctx->requests_count);
630     }
631   if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
632     {
633       VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
634                 strerror (mrv));
635       abort ();                 /* this really shouldn't happen */
636     }
637   return rv;
638 }
639
640 static vapi_error_e
641 vapi_dispatch_event (vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
642 {
643   if (ctx->event_cbs[id].cb)
644     {
645       return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
646     }
647   else if (ctx->generic_cb.cb)
648     {
649       return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
650     }
651   else
652     {
653       VAPI_DBG
654         ("No handler/generic handler for msg id %u[%s], message ignored",
655          (unsigned) id, __vapi_metadata.msgs[id]->name);
656     }
657   return VAPI_OK;
658 }
659
660 static bool
661 vapi_msg_is_with_context (vapi_msg_id_t id)
662 {
663   assert (id <= __vapi_metadata.count);
664   return __vapi_metadata.msgs[id]->has_context;
665 }
666
667 vapi_error_e
668 vapi_dispatch_one (vapi_ctx_t ctx)
669 {
670   VAPI_DBG ("vapi_dispatch_one()");
671   void *msg;
672   size_t size;
673   vapi_error_e rv = vapi_recv (ctx, &msg, &size);
674   if (VAPI_OK != rv)
675     {
676       VAPI_DBG ("vapi_recv failed with rv=%d", rv);
677       return rv;
678     }
679   u16 vpp_id = be16toh (*(u16 *) msg);
680   if (vpp_id > ctx->vl_msg_id_max)
681     {
682       VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
683                 (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
684       vapi_msg_free (ctx, msg);
685       return VAPI_EINVAL;
686     }
687   if (~0 == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
688     {
689       VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
690                 (unsigned) vpp_id);
691       vapi_msg_free (ctx, msg);
692       return VAPI_EINVAL;
693     }
694   const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
695   const size_t expect_size = vapi_get_message_size (id);
696   if (size < expect_size)
697     {
698       VAPI_ERR
699         ("Invalid msg received, unexpected size `%zu' < expected min `%zu'",
700          size, expect_size);
701       vapi_msg_free (ctx, msg);
702       return VAPI_EINVAL;
703     }
704   u32 context;
705   vapi_get_swap_to_host_func (id) (msg);
706   if (vapi_msg_is_with_context (id))
707     {
708       context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
709       /* is this a message originating from VAPI? */
710       VAPI_DBG ("dispatch, context is %x", context);
711       if (context & context_counter_mask)
712         {
713           rv = vapi_dispatch_response (ctx, id, context, msg);
714           goto done;
715         }
716     }
717   rv = vapi_dispatch_event (ctx, id, msg);
718
719 done:
720   vapi_msg_free (ctx, msg);
721   return rv;
722 }
723
724 vapi_error_e
725 vapi_dispatch (vapi_ctx_t ctx)
726 {
727   vapi_error_e rv = VAPI_OK;
728   while (!vapi_requests_empty (ctx))
729     {
730       rv = vapi_dispatch_one (ctx);
731       if (VAPI_OK != rv)
732         {
733           return rv;
734         }
735     }
736   return rv;
737 }
738
739 void
740 vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
741                    vapi_event_cb callback, void *callback_ctx)
742 {
743   vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
744   c->cb = callback;
745   c->ctx = callback_ctx;
746 }
747
748 void
749 vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id)
750 {
751   vapi_set_event_cb (ctx, id, NULL, NULL);
752 }
753
754 void
755 vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
756                            void *callback_ctx)
757 {
758   ctx->generic_cb.cb = callback;
759   ctx->generic_cb.ctx = callback_ctx;
760 }
761
762 void
763 vapi_clear_generic_event_cb (vapi_ctx_t ctx)
764 {
765   ctx->generic_cb.cb = NULL;
766   ctx->generic_cb.ctx = NULL;
767 }
768
769 u16
770 vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id)
771 {
772   assert (id < __vapi_metadata.count);
773   return ctx->vapi_msg_id_t_to_vl_msg_id[id];
774 }
775
776 int
777 vapi_get_client_index (vapi_ctx_t ctx)
778 {
779   return api_main.my_client_index;
780 }
781
782 bool
783 vapi_is_nonblocking (vapi_ctx_t ctx)
784 {
785   return (VAPI_MODE_NONBLOCKING == ctx->mode);
786 }
787
788 bool vapi_requests_full (vapi_ctx_t ctx);
789
790 size_t vapi_get_request_count (vapi_ctx_t ctx);
791
792 size_t
793 vapi_get_max_request_count (vapi_ctx_t ctx)
794 {
795   return ctx->requests_size - 1;
796 }
797
798 int
799 vapi_get_payload_offset (vapi_msg_id_t id)
800 {
801   assert (id < __vapi_metadata.count);
802   return __vapi_metadata.msgs[id]->payload_offset;
803 }
804
805 void (*vapi_get_swap_to_host_func (vapi_msg_id_t id)) (void *msg)
806 {
807   assert (id < __vapi_metadata.count);
808   return __vapi_metadata.msgs[id]->swap_to_host;
809 }
810
811 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
812 {
813   assert (id < __vapi_metadata.count);
814   return __vapi_metadata.msgs[id]->swap_to_be;
815 }
816
817 size_t
818 vapi_get_message_size (vapi_msg_id_t id)
819 {
820   assert (id < __vapi_metadata.count);
821   return __vapi_metadata.msgs[id]->size;
822 }
823
824 size_t
825 vapi_get_context_offset (vapi_msg_id_t id)
826 {
827   assert (id < __vapi_metadata.count);
828   return __vapi_metadata.msgs[id]->context_offset;
829 }
830
831 vapi_msg_id_t
832 vapi_register_msg (vapi_message_desc_t * msg)
833 {
834   int i = 0;
835   for (i = 0; i < __vapi_metadata.count; ++i)
836     {
837       if (!strcmp
838           (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
839         {
840           /* this happens if somebody is linking together several objects while
841            * using the static inline headers, just fill in the already
842            * assigned id here so that all the objects are in sync */
843           msg->id = __vapi_metadata.msgs[i]->id;
844           return msg->id;
845         }
846     }
847   vapi_msg_id_t id = __vapi_metadata.count;
848   ++__vapi_metadata.count;
849   __vapi_metadata.msgs =
850     realloc (__vapi_metadata.msgs,
851              sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
852   __vapi_metadata.msgs[id] = msg;
853   size_t s = strlen (msg->name_with_crc);
854   if (s > __vapi_metadata.max_len_name_with_crc)
855     {
856       __vapi_metadata.max_len_name_with_crc = s;
857     }
858   msg->id = id;
859   return id;
860 }
861
862 vapi_error_e
863 vapi_producer_lock (vapi_ctx_t ctx)
864 {
865   int mrv;
866   if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
867     {
868       VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
869       (void) mrv;               /* avoid warning if the above debug is not enabled */
870       return VAPI_MUTEX_FAILURE;
871     }
872   return VAPI_OK;
873 }
874
875 vapi_error_e
876 vapi_producer_unlock (vapi_ctx_t ctx)
877 {
878   int mrv;
879   if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
880     {
881       VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
882                 strerror (mrv));
883       (void) mrv;               /* avoid warning if the above debug is not enabled */
884       return VAPI_MUTEX_FAILURE;
885     }
886   return VAPI_OK;
887 }
888
889 /*
890  * fd.io coding-style-patch-verification: ON
891  *
892  * Local Variables:
893  * eval: (c-set-style "gnu")
894  * End:
895  */