Add C++ API
[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 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_msg_id_t
233 vapi_lookup_vapi_msg_id_t (vapi_ctx_t ctx, u16 vl_msg_id)
234 {
235   if (vl_msg_id <= ctx->vl_msg_id_max)
236     {
237       return ctx->vl_msg_id_to_vapi_msg_t[vl_msg_id];
238     }
239   return ~0;
240 }
241
242 vapi_error_e
243 vapi_ctx_alloc (vapi_ctx_t * result)
244 {
245   vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
246   if (!ctx)
247     {
248       return VAPI_ENOMEM;
249     }
250   ctx->context_counter = 0;
251   ctx->vapi_msg_id_t_to_vl_msg_id =
252     malloc (__vapi_metadata.count *
253             sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
254   if (!ctx->vapi_msg_id_t_to_vl_msg_id)
255     {
256       goto fail;
257     }
258   ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
259   if (!ctx->event_cbs)
260     {
261       goto fail;
262     }
263   pthread_mutex_init (&ctx->requests_mutex, NULL);
264   *result = ctx;
265   return VAPI_OK;
266 fail:
267   vapi_ctx_free (ctx);
268   return VAPI_ENOMEM;
269 }
270
271 void
272 vapi_ctx_free (vapi_ctx_t ctx)
273 {
274   assert (!ctx->connected);
275   free (ctx->requests);
276   free (ctx->vapi_msg_id_t_to_vl_msg_id);
277   free (ctx->event_cbs);
278   free (ctx->vl_msg_id_to_vapi_msg_t);
279   pthread_mutex_destroy (&ctx->requests_mutex);
280   free (ctx);
281 }
282
283 bool
284 vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t id)
285 {
286   return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
287 }
288
289 vapi_error_e
290 vapi_connect (vapi_ctx_t ctx, const char *name,
291               const char *chroot_prefix,
292               int max_outstanding_requests,
293               int response_queue_size, vapi_mode_e mode)
294 {
295   if (response_queue_size <= 0 || max_outstanding_requests <= 0)
296     {
297       return VAPI_EINVAL;
298     }
299   ctx->requests_size = max_outstanding_requests;
300   const size_t size = ctx->requests_size * sizeof (*ctx->requests);
301   void *tmp = realloc (ctx->requests, size);
302   if (!tmp)
303     {
304       return VAPI_ENOMEM;
305     }
306   ctx->requests = tmp;
307   memset (ctx->requests, 0, size);
308   ctx->requests_start = ctx->requests_count = 0;
309   if (chroot_prefix)
310     {
311       VAPI_DBG ("set memory root path `%s'", chroot_prefix);
312       vl_set_memory_root_path ((char *) chroot_prefix);
313     }
314   static char api_map[] = "/vpe-api";
315   VAPI_DBG ("client api map `%s'", api_map);
316   if ((vl_client_api_map (api_map)) < 0)
317     {
318       return VAPI_EMAP_FAIL;
319     }
320   VAPI_DBG ("connect client `%s'", name);
321   if (vl_client_connect ((char *) name, 0, response_queue_size) < 0)
322     {
323       vl_client_api_unmap ();
324       return VAPI_ECON_FAIL;
325     }
326 #if VAPI_DEBUG_CONNECT
327   VAPI_DBG ("start probing messages");
328 #endif
329   int rv;
330   int i;
331   for (i = 0; i < __vapi_metadata.count; ++i)
332     {
333       vapi_message_desc_t *m = __vapi_metadata.msgs[i];
334       u8 scratch[m->name_with_crc_len + 1];
335       memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
336       u32 id = vl_api_get_msg_index (scratch);
337       if (~0 != id)
338         {
339           if (id > UINT16_MAX)
340             {
341               VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
342                         UINT16_MAX);
343               rv = VAPI_EINVAL;
344               goto fail;
345             }
346           if (id > ctx->vl_msg_id_max)
347             {
348               vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t,
349                                             sizeof
350                                             (*ctx->vl_msg_id_to_vapi_msg_t) *
351                                             (id + 1));
352               if (!tmp)
353                 {
354                   rv = VAPI_ENOMEM;
355                   goto fail;
356                 }
357               ctx->vl_msg_id_to_vapi_msg_t = tmp;
358               ctx->vl_msg_id_max = id;
359             }
360           ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
361           ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
362 #if VAPI_DEBUG_CONNECT
363           VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
364                     (unsigned) id);
365 #endif
366         }
367       else
368         {
369           ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
370           VAPI_DBG ("Message `%s' not available", m->name_with_crc);
371         }
372     }
373 #if VAPI_DEBUG_CONNECT
374   VAPI_DBG ("finished probing messages");
375 #endif
376   if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
377       !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
378     {
379       VAPI_ERR
380         ("control ping or control ping reply not available, cannot connect");
381       rv = VAPI_EINCOMPATIBLE;
382       goto fail;
383     }
384   ctx->mode = mode;
385   ctx->connected = true;
386   return VAPI_OK;
387 fail:
388   vl_client_disconnect ();
389   vl_client_api_unmap ();
390   return rv;
391 }
392
393 vapi_error_e
394 vapi_disconnect (vapi_ctx_t ctx)
395 {
396   if (!ctx->connected)
397     {
398       return VAPI_EINVAL;
399     }
400   vl_client_disconnect ();
401   vl_client_api_unmap ();
402 #if VAPI_DEBUG_ALLOC
403   vapi_to_be_freed_validate ();
404 #endif
405   ctx->connected = false;
406   return VAPI_OK;
407 }
408
409 vapi_error_e
410 vapi_get_fd (vapi_ctx_t ctx, int *fd)
411 {
412   return VAPI_ENOTSUP;
413 }
414
415 vapi_error_e
416 vapi_send (vapi_ctx_t ctx, void *msg)
417 {
418   vapi_error_e rv = VAPI_OK;
419   if (!ctx || !msg || !ctx->connected)
420     {
421       rv = VAPI_EINVAL;
422       goto out;
423     }
424   int tmp;
425   unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue;
426 #if VAPI_DEBUG
427   unsigned msgid = be16toh (*(u16 *) msg);
428   if (msgid <= ctx->vl_msg_id_max)
429     {
430       vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
431       if (id < __vapi_metadata.count)
432         {
433           VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
434                     __vapi_metadata.msgs[id]->name);
435         }
436       else
437         {
438           VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
439         }
440     }
441   else
442     {
443       VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
444     }
445 #endif
446   tmp = unix_shared_memory_queue_add (q, (u8 *) & msg,
447                                       VAPI_MODE_BLOCKING ==
448                                       ctx->mode ? 0 : 1);
449   if (tmp < 0)
450     {
451       rv = VAPI_EAGAIN;
452     }
453 out:
454   VAPI_DBG ("vapi_send() rv = %d", rv);
455   return rv;
456 }
457
458 vapi_error_e
459 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
460 {
461   vapi_error_e rv = VAPI_OK;
462   if (!ctx || !msg1 || !msg2 || !ctx->connected)
463     {
464       rv = VAPI_EINVAL;
465       goto out;
466     }
467   unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue;
468 #if VAPI_DEBUG
469   unsigned msgid1 = be16toh (*(u16 *) msg1);
470   unsigned msgid2 = be16toh (*(u16 *) msg2);
471   const char *name1 = "UNKNOWN";
472   const char *name2 = "UNKNOWN";
473   if (msgid1 <= ctx->vl_msg_id_max)
474     {
475       vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
476       if (id < __vapi_metadata.count)
477         {
478           name1 = __vapi_metadata.msgs[id]->name;
479         }
480     }
481   if (msgid2 <= ctx->vl_msg_id_max)
482     {
483       vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
484       if (id < __vapi_metadata.count)
485         {
486           name2 = __vapi_metadata.msgs[id]->name;
487         }
488     }
489   VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
490 #endif
491   int tmp = unix_shared_memory_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
492                                            VAPI_MODE_BLOCKING ==
493                                            ctx->mode ? 0 : 1);
494   if (tmp < 0)
495     {
496       rv = VAPI_EAGAIN;
497     }
498 out:
499   VAPI_DBG ("vapi_send() rv = %d", rv);
500   return rv;
501 }
502
503 vapi_error_e
504 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size)
505 {
506   if (!ctx || !ctx->connected || !msg || !msg_size)
507     {
508       return VAPI_EINVAL;
509     }
510   vapi_error_e rv = VAPI_OK;
511   api_main_t *am = &api_main;
512   uword data;
513
514   if (am->our_pid == 0)
515     {
516       return VAPI_EINVAL;
517     }
518
519   unix_shared_memory_queue_t *q = am->vl_input_queue;
520   VAPI_DBG ("doing shm queue sub");
521   int tmp = unix_shared_memory_queue_sub (q, (u8 *) & data, 0);
522   if (tmp == 0)
523     {
524 #if VAPI_DEBUG_ALLOC
525       vapi_add_to_be_freed ((void *) data);
526 #endif
527       msgbuf_t *msgbuf =
528         (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
529       if (!msgbuf->data_len)
530         {
531           vapi_msg_free (ctx, (u8 *) data);
532           return VAPI_EAGAIN;
533         }
534       *msg = (u8 *) data;
535       *msg_size = ntohl (msgbuf->data_len);
536 #if VAPI_DEBUG
537       unsigned msgid = be16toh (*(u16 *) * msg);
538       if (msgid <= ctx->vl_msg_id_max)
539         {
540           vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
541           if (id < __vapi_metadata.count)
542             {
543               VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
544                         __vapi_metadata.msgs[id]->name);
545             }
546           else
547             {
548               VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
549             }
550         }
551       else
552         {
553           VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
554         }
555 #endif
556     }
557   else
558     {
559       rv = VAPI_EAGAIN;
560     }
561   return rv;
562 }
563
564 vapi_error_e
565 vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode)
566 {
567   return VAPI_ENOTSUP;
568 }
569
570 static vapi_error_e
571 vapi_dispatch_response (vapi_ctx_t ctx, vapi_msg_id_t id,
572                         u32 context, void *msg)
573 {
574   int mrv;
575   if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
576     {
577       VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
578       return VAPI_MUTEX_FAILURE;
579     }
580   int tmp = ctx->requests_start;
581   const int requests_end = vapi_requests_end (ctx);
582   while (ctx->requests[tmp].context != context && tmp != requests_end)
583     {
584       ++tmp;
585       if (tmp == ctx->requests_size)
586         {
587           tmp = 0;
588         }
589     }
590   VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
591             ctx->requests[tmp].context == context ? "matched" : "stopped",
592             tmp);
593   vapi_error_e rv = VAPI_OK;
594   if (ctx->requests[tmp].context == context)
595     {
596       while (ctx->requests_start != tmp)
597         {
598           VAPI_ERR ("No response to req with context=%u",
599                     (unsigned) ctx->requests[tmp].context);
600           ctx->requests[ctx->requests_start].callback (ctx,
601                                                        ctx->requests
602                                                        [ctx->
603                                                         requests_start].callback_ctx,
604                                                        VAPI_ENORESP, true,
605                                                        NULL);
606           memset (&ctx->requests[ctx->requests_start], 0,
607                   sizeof (ctx->requests[ctx->requests_start]));
608           ++ctx->requests_start;
609           --ctx->requests_count;
610           if (ctx->requests_start == ctx->requests_size)
611             {
612               ctx->requests_start = 0;
613             }
614         }
615       // now ctx->requests_start == tmp
616       int payload_offset = vapi_get_payload_offset (id);
617       void *payload = ((u8 *) msg) + payload_offset;
618       bool is_last = true;
619       if (ctx->requests[tmp].is_dump)
620         {
621           if (vapi_msg_id_control_ping_reply == id)
622             {
623               payload = NULL;
624             }
625           else
626             {
627               is_last = false;
628             }
629         }
630       if (payload_offset != -1)
631         {
632           rv =
633             ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx,
634                                          VAPI_OK, is_last, payload);
635         }
636       else
637         {
638           /* this is a message without payload, so bend the callback a little
639            */
640           rv =
641             ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
642              ctx->requests[tmp].callback) (ctx,
643                                            ctx->requests[tmp].callback_ctx,
644                                            VAPI_OK, is_last);
645         }
646       if (is_last)
647         {
648           memset (&ctx->requests[ctx->requests_start], 0,
649                   sizeof (ctx->requests[ctx->requests_start]));
650           ++ctx->requests_start;
651           --ctx->requests_count;
652           if (ctx->requests_start == ctx->requests_size)
653             {
654               ctx->requests_start = 0;
655             }
656         }
657       VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
658                 ctx->requests_start, requests_end, ctx->requests_count);
659     }
660   if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
661     {
662       VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
663                 strerror (mrv));
664       abort ();                 /* this really shouldn't happen */
665     }
666   return rv;
667 }
668
669 static vapi_error_e
670 vapi_dispatch_event (vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
671 {
672   if (ctx->event_cbs[id].cb)
673     {
674       return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
675     }
676   else if (ctx->generic_cb.cb)
677     {
678       return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
679     }
680   else
681     {
682       VAPI_DBG
683         ("No handler/generic handler for msg id %u[%s], message ignored",
684          (unsigned) id, __vapi_metadata.msgs[id]->name);
685     }
686   return VAPI_OK;
687 }
688
689 bool
690 vapi_msg_is_with_context (vapi_msg_id_t id)
691 {
692   assert (id <= __vapi_metadata.count);
693   return __vapi_metadata.msgs[id]->has_context;
694 }
695
696 vapi_error_e
697 vapi_dispatch_one (vapi_ctx_t ctx)
698 {
699   VAPI_DBG ("vapi_dispatch_one()");
700   void *msg;
701   size_t size;
702   vapi_error_e rv = vapi_recv (ctx, &msg, &size);
703   if (VAPI_OK != rv)
704     {
705       VAPI_DBG ("vapi_recv failed with rv=%d", rv);
706       return rv;
707     }
708   u16 vpp_id = be16toh (*(u16 *) msg);
709   if (vpp_id > ctx->vl_msg_id_max)
710     {
711       VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
712                 (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
713       vapi_msg_free (ctx, msg);
714       return VAPI_EINVAL;
715     }
716   if (~0 == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
717     {
718       VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
719                 (unsigned) vpp_id);
720       vapi_msg_free (ctx, msg);
721       return VAPI_EINVAL;
722     }
723   const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
724   const size_t expect_size = vapi_get_message_size (id);
725   if (size < expect_size)
726     {
727       VAPI_ERR
728         ("Invalid msg received, unexpected size `%zu' < expected min `%zu'",
729          size, expect_size);
730       vapi_msg_free (ctx, msg);
731       return VAPI_EINVAL;
732     }
733   u32 context;
734   vapi_get_swap_to_host_func (id) (msg);
735   if (vapi_msg_is_with_context (id))
736     {
737       context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
738       /* is this a message originating from VAPI? */
739       VAPI_DBG ("dispatch, context is %x", context);
740       if (context & context_counter_mask)
741         {
742           rv = vapi_dispatch_response (ctx, id, context, msg);
743           goto done;
744         }
745     }
746   rv = vapi_dispatch_event (ctx, id, msg);
747
748 done:
749   vapi_msg_free (ctx, msg);
750   return rv;
751 }
752
753 vapi_error_e
754 vapi_dispatch (vapi_ctx_t ctx)
755 {
756   vapi_error_e rv = VAPI_OK;
757   while (!vapi_requests_empty (ctx))
758     {
759       rv = vapi_dispatch_one (ctx);
760       if (VAPI_OK != rv)
761         {
762           return rv;
763         }
764     }
765   return rv;
766 }
767
768 void
769 vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
770                    vapi_event_cb callback, void *callback_ctx)
771 {
772   vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
773   c->cb = callback;
774   c->ctx = callback_ctx;
775 }
776
777 void
778 vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id)
779 {
780   vapi_set_event_cb (ctx, id, NULL, NULL);
781 }
782
783 void
784 vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
785                            void *callback_ctx)
786 {
787   ctx->generic_cb.cb = callback;
788   ctx->generic_cb.ctx = callback_ctx;
789 }
790
791 void
792 vapi_clear_generic_event_cb (vapi_ctx_t ctx)
793 {
794   ctx->generic_cb.cb = NULL;
795   ctx->generic_cb.ctx = NULL;
796 }
797
798 u16
799 vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id)
800 {
801   assert (id < __vapi_metadata.count);
802   return ctx->vapi_msg_id_t_to_vl_msg_id[id];
803 }
804
805 int
806 vapi_get_client_index (vapi_ctx_t ctx)
807 {
808   return api_main.my_client_index;
809 }
810
811 bool
812 vapi_is_nonblocking (vapi_ctx_t ctx)
813 {
814   return (VAPI_MODE_NONBLOCKING == ctx->mode);
815 }
816
817 size_t
818 vapi_get_max_request_count (vapi_ctx_t ctx)
819 {
820   return ctx->requests_size - 1;
821 }
822
823 int
824 vapi_get_payload_offset (vapi_msg_id_t id)
825 {
826   assert (id < __vapi_metadata.count);
827   return __vapi_metadata.msgs[id]->payload_offset;
828 }
829
830 void (*vapi_get_swap_to_host_func (vapi_msg_id_t id)) (void *msg)
831 {
832   assert (id < __vapi_metadata.count);
833   return __vapi_metadata.msgs[id]->swap_to_host;
834 }
835
836 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
837 {
838   assert (id < __vapi_metadata.count);
839   return __vapi_metadata.msgs[id]->swap_to_be;
840 }
841
842 size_t
843 vapi_get_message_size (vapi_msg_id_t id)
844 {
845   assert (id < __vapi_metadata.count);
846   return __vapi_metadata.msgs[id]->size;
847 }
848
849 size_t
850 vapi_get_context_offset (vapi_msg_id_t id)
851 {
852   assert (id < __vapi_metadata.count);
853   return __vapi_metadata.msgs[id]->context_offset;
854 }
855
856 vapi_msg_id_t
857 vapi_register_msg (vapi_message_desc_t * msg)
858 {
859   int i = 0;
860   for (i = 0; i < __vapi_metadata.count; ++i)
861     {
862       if (!strcmp
863           (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
864         {
865           /* this happens if somebody is linking together several objects while
866            * using the static inline headers, just fill in the already
867            * assigned id here so that all the objects are in sync */
868           msg->id = __vapi_metadata.msgs[i]->id;
869           return msg->id;
870         }
871     }
872   vapi_msg_id_t id = __vapi_metadata.count;
873   ++__vapi_metadata.count;
874   __vapi_metadata.msgs =
875     realloc (__vapi_metadata.msgs,
876              sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
877   __vapi_metadata.msgs[id] = msg;
878   size_t s = strlen (msg->name_with_crc);
879   if (s > __vapi_metadata.max_len_name_with_crc)
880     {
881       __vapi_metadata.max_len_name_with_crc = s;
882     }
883   msg->id = id;
884   return id;
885 }
886
887 vapi_error_e
888 vapi_producer_lock (vapi_ctx_t ctx)
889 {
890   int mrv;
891   if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
892     {
893       VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
894       (void) mrv;               /* avoid warning if the above debug is not enabled */
895       return VAPI_MUTEX_FAILURE;
896     }
897   return VAPI_OK;
898 }
899
900 vapi_error_e
901 vapi_producer_unlock (vapi_ctx_t ctx)
902 {
903   int mrv;
904   if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
905     {
906       VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
907                 strerror (mrv));
908       (void) mrv;               /* avoid warning if the above debug is not enabled */
909       return VAPI_MUTEX_FAILURE;
910     }
911   return VAPI_OK;
912 }
913
914 size_t
915 vapi_get_message_count ()
916 {
917   return __vapi_metadata.count;
918 }
919
920 const char *
921 vapi_get_msg_name (vapi_msg_id_t id)
922 {
923   return __vapi_metadata.msgs[id]->name;
924 }
925
926 /*
927  * fd.io coding-style-patch-verification: ON
928  *
929  * Local Variables:
930  * eval: (c-set-style "gnu")
931  * End:
932  */