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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
21 #include <arpa/inet.h>
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 <vppinfra/pool.h>
30 #include <vlib/vlib.h>
31 #include <vlibapi/api_common.h>
32 #include <vlibmemory/api_common.h>
34 /* we need to use control pings for some stuff and because we're forced to put
35 * the code in headers, we need a way to be able to grab the ids of these
36 * messages - so declare them here as extern */
37 vapi_msg_id_t vapi_msg_id_control_ping = 0;
38 vapi_msg_id_t vapi_msg_id_control_ping_reply = 0;
43 vapi_message_desc_t **msgs;
44 size_t max_len_name_with_crc;
55 static const u32 context_counter_mask = (1 << 31);
59 vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id,
62 } vapi_generic_cb_with_ctx;
66 vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, void *payload);
68 } vapi_event_cb_with_ctx;
73 int requests_size; /* size of the requests array (circular queue) */
74 int requests_start; /* index of first request */
75 int requests_count; /* number of used slots */
78 vapi_generic_cb_with_ctx generic_cb;
79 vapi_event_cb_with_ctx *event_cbs;
80 u16 *vapi_msg_id_t_to_vl_msg_id;
82 vapi_msg_id_t *vl_msg_id_to_vapi_msg_t;
84 pthread_mutex_t requests_mutex;
88 vapi_gen_req_context (vapi_ctx_t ctx)
90 ++ctx->context_counter;
91 ctx->context_counter %= context_counter_mask;
92 return ctx->context_counter | context_counter_mask;
96 vapi_get_request_count (vapi_ctx_t ctx)
98 return ctx->requests_count;
102 vapi_requests_full (vapi_ctx_t ctx)
104 return (ctx->requests_count == ctx->requests_size);
108 vapi_requests_empty (vapi_ctx_t ctx)
110 return (0 == ctx->requests_count);
114 vapi_requests_end (vapi_ctx_t ctx)
116 return (ctx->requests_start + ctx->requests_count) % ctx->requests_size;
120 vapi_store_request (vapi_ctx_t ctx, u32 context, bool is_dump,
121 vapi_cb_t callback, void *callback_ctx)
123 assert (!vapi_requests_full (ctx));
124 /* if the mutex is not held, bad things will happen */
125 assert (0 != pthread_mutex_trylock (&ctx->requests_mutex));
126 const int requests_end = vapi_requests_end (ctx);
127 vapi_req_t *slot = &ctx->requests[requests_end];
128 slot->is_dump = is_dump;
129 slot->context = context;
130 slot->callback = callback;
131 slot->callback_ctx = callback_ctx;
132 VAPI_DBG ("stored@%d: context:%x (start is @%d)", requests_end, context,
133 ctx->requests_start);
134 ++ctx->requests_count;
135 assert (!vapi_requests_empty (ctx));
139 struct to_be_freed_s;
143 struct to_be_freed_s *next;
146 static struct to_be_freed_s *to_be_freed = NULL;
149 vapi_add_to_be_freed (void *v)
151 struct to_be_freed_s *prev = NULL;
152 struct to_be_freed_s *tmp;
154 while (tmp && tmp->v)
163 tmp = to_be_freed = calloc (1, sizeof (*to_be_freed));
167 tmp = prev->next = calloc (1, sizeof (*to_be_freed));
170 VAPI_DBG ("To be freed %p", v);
175 vapi_trace_free (void *v)
177 struct to_be_freed_s *tmp = to_be_freed;
178 while (tmp && tmp->v != v)
182 if (tmp && tmp->v == v)
184 VAPI_DBG ("Freed %p", v);
189 VAPI_ERR ("Trying to free untracked pointer %p", v);
195 vapi_to_be_freed_validate ()
197 struct to_be_freed_s *tmp = to_be_freed;
202 VAPI_ERR ("Unfreed msg %p!", tmp->v);
211 vapi_msg_alloc (vapi_ctx_t ctx, size_t size)
217 void *rv = vl_msg_api_alloc_or_null (size);
222 vapi_msg_free (vapi_ctx_t ctx, void *msg)
229 vapi_trace_free (msg);
231 vl_msg_api_free (msg);
235 vapi_lookup_vapi_msg_id_t (vapi_ctx_t ctx, u16 vl_msg_id)
237 if (vl_msg_id <= ctx->vl_msg_id_max)
239 return ctx->vl_msg_id_to_vapi_msg_t[vl_msg_id];
241 return INVALID_MSG_ID;
245 vapi_ctx_alloc (vapi_ctx_t * result)
247 vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
252 ctx->context_counter = 0;
253 ctx->vapi_msg_id_t_to_vl_msg_id =
254 malloc (__vapi_metadata.count *
255 sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
256 if (!ctx->vapi_msg_id_t_to_vl_msg_id)
260 ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
265 pthread_mutex_init (&ctx->requests_mutex, NULL);
274 vapi_ctx_free (vapi_ctx_t ctx)
276 assert (!ctx->connected);
277 free (ctx->requests);
278 free (ctx->vapi_msg_id_t_to_vl_msg_id);
279 free (ctx->event_cbs);
280 free (ctx->vl_msg_id_to_vapi_msg_t);
281 pthread_mutex_destroy (&ctx->requests_mutex);
286 vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t id)
288 return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
292 vapi_connect (vapi_ctx_t ctx, const char *name,
293 const char *chroot_prefix,
294 int max_outstanding_requests,
295 int response_queue_size, vapi_mode_e mode)
297 if (response_queue_size <= 0 || max_outstanding_requests <= 0)
301 ctx->requests_size = max_outstanding_requests;
302 const size_t size = ctx->requests_size * sizeof (*ctx->requests);
303 void *tmp = realloc (ctx->requests, size);
309 memset (ctx->requests, 0, size);
310 /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
311 ctx->requests_start = ctx->requests_count = 0;
314 VAPI_DBG ("set memory root path `%s'", chroot_prefix);
315 vl_set_memory_root_path ((char *) chroot_prefix);
317 static char api_map[] = "/vpe-api";
318 VAPI_DBG ("client api map `%s'", api_map);
319 if ((vl_client_api_map (api_map)) < 0)
321 return VAPI_EMAP_FAIL;
323 VAPI_DBG ("connect client `%s'", name);
324 if (vl_client_connect ((char *) name, 0, response_queue_size) < 0)
326 vl_client_api_unmap ();
327 return VAPI_ECON_FAIL;
329 #if VAPI_DEBUG_CONNECT
330 VAPI_DBG ("start probing messages");
334 for (i = 0; i < __vapi_metadata.count; ++i)
336 vapi_message_desc_t *m = __vapi_metadata.msgs[i];
337 u8 scratch[m->name_with_crc_len + 1];
338 memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
339 u32 id = vl_api_get_msg_index (scratch);
340 if (INVALID_MSG_ID != id)
344 VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
349 if (id > ctx->vl_msg_id_max)
351 vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t,
353 (*ctx->vl_msg_id_to_vapi_msg_t) *
360 ctx->vl_msg_id_to_vapi_msg_t = tmp;
361 ctx->vl_msg_id_max = id;
363 ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
364 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
365 #if VAPI_DEBUG_CONNECT
366 VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
372 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
373 VAPI_DBG ("Message `%s' not available", m->name_with_crc);
376 #if VAPI_DEBUG_CONNECT
377 VAPI_DBG ("finished probing messages");
379 if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
380 !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
383 ("control ping or control ping reply not available, cannot connect");
384 rv = VAPI_EINCOMPATIBLE;
388 ctx->connected = true;
391 vl_client_disconnect ();
392 vl_client_api_unmap ();
397 vapi_disconnect (vapi_ctx_t ctx)
403 vl_client_disconnect ();
404 vl_client_api_unmap ();
406 vapi_to_be_freed_validate ();
408 ctx->connected = false;
413 vapi_get_fd (vapi_ctx_t ctx, int *fd)
419 vapi_send (vapi_ctx_t ctx, void *msg)
421 vapi_error_e rv = VAPI_OK;
422 if (!ctx || !msg || !ctx->connected)
428 unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue;
430 unsigned msgid = be16toh (*(u16 *) msg);
431 if (msgid <= ctx->vl_msg_id_max)
433 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
434 if (id < __vapi_metadata.count)
436 VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
437 __vapi_metadata.msgs[id]->name);
441 VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
446 VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
449 tmp = unix_shared_memory_queue_add (q, (u8 *) & msg,
450 VAPI_MODE_BLOCKING ==
457 VAPI_DBG ("vapi_send() rv = %d", rv);
462 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
464 vapi_error_e rv = VAPI_OK;
465 if (!ctx || !msg1 || !msg2 || !ctx->connected)
470 unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue;
472 unsigned msgid1 = be16toh (*(u16 *) msg1);
473 unsigned msgid2 = be16toh (*(u16 *) msg2);
474 const char *name1 = "UNKNOWN";
475 const char *name2 = "UNKNOWN";
476 if (msgid1 <= ctx->vl_msg_id_max)
478 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
479 if (id < __vapi_metadata.count)
481 name1 = __vapi_metadata.msgs[id]->name;
484 if (msgid2 <= ctx->vl_msg_id_max)
486 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
487 if (id < __vapi_metadata.count)
489 name2 = __vapi_metadata.msgs[id]->name;
492 VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
494 int tmp = unix_shared_memory_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
495 VAPI_MODE_BLOCKING ==
502 VAPI_DBG ("vapi_send() rv = %d", rv);
507 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size)
509 if (!ctx || !ctx->connected || !msg || !msg_size)
513 vapi_error_e rv = VAPI_OK;
514 api_main_t *am = &api_main;
517 if (am->our_pid == 0)
522 unix_shared_memory_queue_t *q = am->vl_input_queue;
523 VAPI_DBG ("doing shm queue sub");
524 int tmp = unix_shared_memory_queue_sub (q, (u8 *) & data, 0);
528 vapi_add_to_be_freed ((void *) data);
531 (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
532 if (!msgbuf->data_len)
534 vapi_msg_free (ctx, (u8 *) data);
538 *msg_size = ntohl (msgbuf->data_len);
540 unsigned msgid = be16toh (*(u16 *) * msg);
541 if (msgid <= ctx->vl_msg_id_max)
543 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
544 if (id < __vapi_metadata.count)
546 VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
547 __vapi_metadata.msgs[id]->name);
551 VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
556 VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
568 vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode)
574 vapi_dispatch_response (vapi_ctx_t ctx, vapi_msg_id_t id,
575 u32 context, void *msg)
578 if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
580 VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
581 return VAPI_MUTEX_FAILURE;
583 int tmp = ctx->requests_start;
584 const int requests_end = vapi_requests_end (ctx);
585 while (ctx->requests[tmp].context != context && tmp != requests_end)
588 if (tmp == ctx->requests_size)
593 VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
594 ctx->requests[tmp].context == context ? "matched" : "stopped",
596 vapi_error_e rv = VAPI_OK;
597 if (ctx->requests[tmp].context == context)
599 while (ctx->requests_start != tmp)
601 VAPI_ERR ("No response to req with context=%u",
602 (unsigned) ctx->requests[tmp].context);
603 ctx->requests[ctx->requests_start].callback (ctx,
606 requests_start].callback_ctx,
609 memset (&ctx->requests[ctx->requests_start], 0,
610 sizeof (ctx->requests[ctx->requests_start]));
611 ++ctx->requests_start;
612 --ctx->requests_count;
613 if (ctx->requests_start == ctx->requests_size)
615 ctx->requests_start = 0;
618 // now ctx->requests_start == tmp
619 int payload_offset = vapi_get_payload_offset (id);
620 void *payload = ((u8 *) msg) + payload_offset;
622 if (ctx->requests[tmp].is_dump)
624 if (vapi_msg_id_control_ping_reply == id)
633 if (payload_offset != -1)
636 ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx,
637 VAPI_OK, is_last, payload);
641 /* this is a message without payload, so bend the callback a little
644 ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
645 ctx->requests[tmp].callback) (ctx,
646 ctx->requests[tmp].callback_ctx,
651 memset (&ctx->requests[ctx->requests_start], 0,
652 sizeof (ctx->requests[ctx->requests_start]));
653 ++ctx->requests_start;
654 --ctx->requests_count;
655 if (ctx->requests_start == ctx->requests_size)
657 ctx->requests_start = 0;
660 VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
661 ctx->requests_start, requests_end, ctx->requests_count);
663 if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
665 VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
667 abort (); /* this really shouldn't happen */
673 vapi_dispatch_event (vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
675 if (ctx->event_cbs[id].cb)
677 return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
679 else if (ctx->generic_cb.cb)
681 return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
686 ("No handler/generic handler for msg id %u[%s], message ignored",
687 (unsigned) id, __vapi_metadata.msgs[id]->name);
693 vapi_msg_is_with_context (vapi_msg_id_t id)
695 assert (id <= __vapi_metadata.count);
696 return __vapi_metadata.msgs[id]->has_context;
700 vapi_dispatch_one (vapi_ctx_t ctx)
702 VAPI_DBG ("vapi_dispatch_one()");
705 vapi_error_e rv = vapi_recv (ctx, &msg, &size);
708 VAPI_DBG ("vapi_recv failed with rv=%d", rv);
711 u16 vpp_id = be16toh (*(u16 *) msg);
712 if (vpp_id > ctx->vl_msg_id_max)
714 VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
715 (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
716 vapi_msg_free (ctx, msg);
719 if (INVALID_MSG_ID == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
721 VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
723 vapi_msg_free (ctx, msg);
726 const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
727 const size_t expect_size = vapi_get_message_size (id);
728 if (size < expect_size)
731 ("Invalid msg received, unexpected size `%zu' < expected min `%zu'",
733 vapi_msg_free (ctx, msg);
737 vapi_get_swap_to_host_func (id) (msg);
738 if (vapi_msg_is_with_context (id))
740 context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
741 /* is this a message originating from VAPI? */
742 VAPI_DBG ("dispatch, context is %x", context);
743 if (context & context_counter_mask)
745 rv = vapi_dispatch_response (ctx, id, context, msg);
749 rv = vapi_dispatch_event (ctx, id, msg);
752 vapi_msg_free (ctx, msg);
757 vapi_dispatch (vapi_ctx_t ctx)
759 vapi_error_e rv = VAPI_OK;
760 while (!vapi_requests_empty (ctx))
762 rv = vapi_dispatch_one (ctx);
772 vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
773 vapi_event_cb callback, void *callback_ctx)
775 vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
777 c->ctx = callback_ctx;
781 vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id)
783 vapi_set_event_cb (ctx, id, NULL, NULL);
787 vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
790 ctx->generic_cb.cb = callback;
791 ctx->generic_cb.ctx = callback_ctx;
795 vapi_clear_generic_event_cb (vapi_ctx_t ctx)
797 ctx->generic_cb.cb = NULL;
798 ctx->generic_cb.ctx = NULL;
802 vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id)
804 assert (id < __vapi_metadata.count);
805 return ctx->vapi_msg_id_t_to_vl_msg_id[id];
809 vapi_get_client_index (vapi_ctx_t ctx)
811 return api_main.my_client_index;
815 vapi_is_nonblocking (vapi_ctx_t ctx)
817 return (VAPI_MODE_NONBLOCKING == ctx->mode);
821 vapi_get_max_request_count (vapi_ctx_t ctx)
823 return ctx->requests_size - 1;
827 vapi_get_payload_offset (vapi_msg_id_t id)
829 assert (id < __vapi_metadata.count);
830 return __vapi_metadata.msgs[id]->payload_offset;
833 void (*vapi_get_swap_to_host_func (vapi_msg_id_t id)) (void *msg)
835 assert (id < __vapi_metadata.count);
836 return __vapi_metadata.msgs[id]->swap_to_host;
839 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
841 assert (id < __vapi_metadata.count);
842 return __vapi_metadata.msgs[id]->swap_to_be;
846 vapi_get_message_size (vapi_msg_id_t id)
848 assert (id < __vapi_metadata.count);
849 return __vapi_metadata.msgs[id]->size;
853 vapi_get_context_offset (vapi_msg_id_t id)
855 assert (id < __vapi_metadata.count);
856 return __vapi_metadata.msgs[id]->context_offset;
860 vapi_register_msg (vapi_message_desc_t * msg)
863 for (i = 0; i < __vapi_metadata.count; ++i)
866 (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
868 /* this happens if somebody is linking together several objects while
869 * using the static inline headers, just fill in the already
870 * assigned id here so that all the objects are in sync */
871 msg->id = __vapi_metadata.msgs[i]->id;
875 vapi_msg_id_t id = __vapi_metadata.count;
876 ++__vapi_metadata.count;
877 __vapi_metadata.msgs =
878 realloc (__vapi_metadata.msgs,
879 sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
880 __vapi_metadata.msgs[id] = msg;
881 size_t s = strlen (msg->name_with_crc);
882 if (s > __vapi_metadata.max_len_name_with_crc)
884 __vapi_metadata.max_len_name_with_crc = s;
891 vapi_producer_lock (vapi_ctx_t ctx)
894 if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
896 VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
897 (void) mrv; /* avoid warning if the above debug is not enabled */
898 return VAPI_MUTEX_FAILURE;
904 vapi_producer_unlock (vapi_ctx_t ctx)
907 if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
909 VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
911 (void) mrv; /* avoid warning if the above debug is not enabled */
912 return VAPI_MUTEX_FAILURE;
918 vapi_get_message_count ()
920 return __vapi_metadata.count;
924 vapi_get_msg_name (vapi_msg_id_t id)
926 return __vapi_metadata.msgs[id]->name;
930 * fd.io coding-style-patch-verification: ON
933 * eval: (c-set-style "gnu")