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/memory_client.h>
33 #include <vlibmemory/memory_api.h>
34 #include <vlibmemory/api.h>
36 #include <vapi/memclnt.api.vapi.h>
37 #include <vapi/vlib.api.vapi.h>
39 #include <vlibmemory/vl_memory_msg_enum.h>
41 #define vl_typedefs /* define message structures */
42 #include <vlibmemory/vl_memory_api_h.h>
45 /* we need to use control pings for some stuff and because we're forced to put
46 * the code in headers, we need a way to be able to grab the ids of these
47 * messages - so declare them here as extern */
48 vapi_msg_id_t vapi_msg_id_control_ping = 0;
49 vapi_msg_id_t vapi_msg_id_control_ping_reply = 0;
51 DEFINE_VAPI_MSG_IDS_MEMCLNT_API_JSON;
52 DEFINE_VAPI_MSG_IDS_VLIB_API_JSON;
57 vapi_message_desc_t **msgs;
58 size_t max_len_name_with_crc;
66 vapi_msg_id_t response_id;
67 enum vapi_request_type type;
70 static const u32 context_counter_mask = (1 << 31);
74 vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id,
77 } vapi_generic_cb_with_ctx;
81 vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, void *payload);
83 } vapi_event_cb_with_ctx;
88 int requests_size; /* size of the requests array (circular queue) */
89 int requests_start; /* index of first request */
90 int requests_count; /* number of used slots */
93 vapi_generic_cb_with_ctx generic_cb;
94 vapi_event_cb_with_ctx *event_cbs;
95 u16 *vapi_msg_id_t_to_vl_msg_id;
97 vapi_msg_id_t *vl_msg_id_to_vapi_msg_t;
99 bool handle_keepalives;
100 pthread_mutex_t requests_mutex;
102 svm_queue_t *vl_input_queue;
104 /** client message index hash table */
105 uword *msg_index_by_name_and_crc;
109 vapi_gen_req_context (vapi_ctx_t ctx)
111 ++ctx->context_counter;
112 ctx->context_counter %= context_counter_mask;
113 return ctx->context_counter | context_counter_mask;
117 vapi_get_request_count (vapi_ctx_t ctx)
119 return ctx->requests_count;
123 vapi_requests_full (vapi_ctx_t ctx)
125 return (ctx->requests_count == ctx->requests_size);
129 vapi_requests_empty (vapi_ctx_t ctx)
131 return (0 == ctx->requests_count);
135 vapi_requests_end (vapi_ctx_t ctx)
137 return (ctx->requests_start + ctx->requests_count) % ctx->requests_size;
141 vapi_store_request (vapi_ctx_t ctx, u32 context, vapi_msg_id_t response_id,
142 enum vapi_request_type request_type, vapi_cb_t callback,
145 assert (!vapi_requests_full (ctx));
146 /* if the mutex is not held, bad things will happen */
147 assert (0 != pthread_mutex_trylock (&ctx->requests_mutex));
148 const int requests_end = vapi_requests_end (ctx);
149 vapi_req_t *slot = &ctx->requests[requests_end];
150 slot->type = request_type;
151 slot->response_id = response_id;
152 slot->context = context;
153 slot->callback = callback;
154 slot->callback_ctx = callback_ctx;
155 VAPI_DBG ("stored@%d: context:%x (start is @%d)", requests_end, context,
156 ctx->requests_start);
157 ++ctx->requests_count;
158 assert (!vapi_requests_empty (ctx));
162 struct to_be_freed_s;
166 struct to_be_freed_s *next;
169 static struct to_be_freed_s *to_be_freed = NULL;
172 vapi_add_to_be_freed (void *v)
174 struct to_be_freed_s *prev = NULL;
175 struct to_be_freed_s *tmp;
177 while (tmp && tmp->v)
186 tmp = to_be_freed = calloc (1, sizeof (*to_be_freed));
190 tmp = prev->next = calloc (1, sizeof (*to_be_freed));
193 VAPI_DBG ("To be freed %p", v);
198 vapi_trace_free (void *v)
200 struct to_be_freed_s *tmp = to_be_freed;
201 while (tmp && tmp->v != v)
205 if (tmp && tmp->v == v)
207 VAPI_DBG ("Freed %p", v);
212 VAPI_ERR ("Trying to free untracked pointer %p", v);
218 vapi_to_be_freed_validate ()
220 struct to_be_freed_s *tmp = to_be_freed;
225 VAPI_ERR ("Unfreed msg %p!", tmp->v);
234 vapi_msg_alloc (vapi_ctx_t ctx, size_t size)
240 void *rv = vl_msg_api_alloc_as_if_client_or_null (size);
243 clib_memset (rv, 0, size);
249 vapi_msg_free (vapi_ctx_t ctx, void *msg)
256 vapi_trace_free (msg);
258 vl_msg_api_free (msg);
262 vapi_lookup_vapi_msg_id_t (vapi_ctx_t ctx, u16 vl_msg_id)
264 if (vl_msg_id <= ctx->vl_msg_id_max)
266 return ctx->vl_msg_id_to_vapi_msg_t[vl_msg_id];
268 return VAPI_INVALID_MSG_ID;
272 vapi_ctx_alloc (vapi_ctx_t * result)
274 vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
279 ctx->context_counter = 0;
280 ctx->vapi_msg_id_t_to_vl_msg_id =
281 malloc (__vapi_metadata.count *
282 sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
283 if (!ctx->vapi_msg_id_t_to_vl_msg_id)
287 clib_memset (ctx->vapi_msg_id_t_to_vl_msg_id, ~0,
288 __vapi_metadata.count *
289 sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
290 ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
295 pthread_mutex_init (&ctx->requests_mutex, NULL);
304 vapi_ctx_free (vapi_ctx_t ctx)
306 assert (!ctx->connected);
307 free (ctx->requests);
308 free (ctx->vapi_msg_id_t_to_vl_msg_id);
309 free (ctx->event_cbs);
310 free (ctx->vl_msg_id_to_vapi_msg_t);
311 pthread_mutex_destroy (&ctx->requests_mutex);
316 vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t id)
318 return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
321 /* Cut and paste to avoid adding dependency to client library */
322 __clib_nosanitize_addr static void
323 VL_API_VEC_UNPOISON (const void *v)
325 const vec_header_t *vh = &((vec_header_t *) v)[-1];
326 clib_mem_unpoison (vh, sizeof (*vh) + vec_len (v));
330 vapi_api_name_and_crc_free (vapi_ctx_t ctx)
336 if (!ctx->msg_index_by_name_and_crc)
338 hash_foreach_pair (hp, ctx->msg_index_by_name_and_crc,
339 ({ vec_add1 (keys, (u8 *) hp->key); }));
340 for (i = 0; i < vec_len (keys); i++)
343 hash_free (ctx->msg_index_by_name_and_crc);
347 vapi_memclnt_create_v2_reply_t_handler (vapi_ctx_t ctx,
348 vl_api_memclnt_create_v2_reply_t *mp)
350 serialize_main_t _sm, *sm = &_sm;
357 ctx->my_client_index = mp->index;
359 /* Clean out any previous hash table (unlikely) */
360 vapi_api_name_and_crc_free (ctx);
362 ctx->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
364 /* Recreate the vnet-side API message handler table */
365 tblv = uword_to_pointer (mp->message_table, u8 *);
366 unserialize_open_data (sm, tblv, vec_len (tblv));
367 unserialize_integer (sm, &nmsgs, sizeof (u32));
369 VL_API_VEC_UNPOISON (tblv);
371 for (i = 0; i < nmsgs; i++)
373 msg_index = unserialize_likely_small_unsigned_integer (sm);
374 unserialize_cstring (sm, (char **) &name_and_crc);
375 hash_set_mem (ctx->msg_index_by_name_and_crc, name_and_crc, msg_index);
380 vapi_memclnt_delete_reply_t_handler (vapi_ctx_t ctx,
381 vl_api_memclnt_delete_reply_t *mp)
384 oldheap = vl_msg_push_heap ();
385 svm_queue_free (ctx->vl_input_queue);
386 vl_msg_pop_heap (oldheap);
388 ctx->my_client_index = ~0;
389 ctx->vl_input_queue = 0;
393 vapi_client_connect (vapi_ctx_t ctx, const char *name, int ctx_quota,
394 int input_queue_size, bool keepalive)
396 vl_api_memclnt_create_v2_t *mp;
397 vl_api_memclnt_create_v2_reply_t *rp;
398 svm_queue_t *vl_input_queue;
399 vl_shmem_hdr_t *shmem_hdr;
402 api_main_t *am = vlibapi_get_main ();
404 shmem_hdr = am->shmem_hdr;
406 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
408 clib_warning ("shmem_hdr / input queue NULL");
412 clib_mem_unpoison (shmem_hdr, sizeof (*shmem_hdr));
413 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
415 oldheap = vl_msg_push_heap ();
417 svm_queue_alloc_and_init (input_queue_size, sizeof (uword), getpid ());
418 vl_msg_pop_heap (oldheap);
420 ctx->my_client_index = ~0;
421 ctx->vl_input_queue = vl_input_queue;
423 mp = vl_msg_api_alloc_as_if_client (sizeof (vl_api_memclnt_create_v2_t));
424 clib_memset (mp, 0, sizeof (*mp));
425 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_V2);
426 mp->ctx_quota = ctx_quota;
427 mp->input_queue = (uword) vl_input_queue;
428 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
429 mp->keepalive = keepalive;
431 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) &mp);
436 struct timespec ts, tsrem;
439 /* Wait up to 10 seconds */
440 for (i = 0; i < 1000; i++)
443 svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0);
447 ts.tv_nsec = 10000 * 1000; /* 10 ms */
448 while (nanosleep (&ts, &tsrem) < 0)
455 VL_MSG_API_UNPOISON (rp);
456 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_V2_REPLY)
458 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
461 rv = clib_net_to_host_u32 (rp->response);
462 vapi_memclnt_create_v2_reply_t_handler (ctx, rp);
469 vapi_client_send_disconnect (vapi_ctx_t ctx, u8 do_cleanup)
471 vl_api_memclnt_delete_t *mp;
472 vl_shmem_hdr_t *shmem_hdr;
473 api_main_t *am = vlibapi_get_main ();
475 ASSERT (am->vlib_rp);
476 shmem_hdr = am->shmem_hdr;
477 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
479 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
480 clib_memset (mp, 0, sizeof (*mp));
481 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
482 mp->index = ctx->my_client_index;
483 mp->do_cleanup = do_cleanup;
485 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) &mp);
489 vapi_client_disconnect (vapi_ctx_t ctx)
491 vl_api_memclnt_delete_reply_t *rp;
492 svm_queue_t *vl_input_queue;
496 vl_input_queue = ctx->vl_input_queue;
497 vapi_client_send_disconnect (ctx, 0 /* wait for reply */);
500 * Have to be careful here, in case the client is disconnecting
501 * because e.g. the vlib process died, or is unresponsive.
510 if (now >= (begin + 2))
512 clib_warning ("peer unresponsive, give up");
513 ctx->my_client_index = ~0;
516 if (svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0) < 0)
519 VL_MSG_API_UNPOISON (rp);
521 /* drain the queue */
522 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
524 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
525 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
526 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
529 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
530 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
534 vapi_api_name_and_crc_free (ctx);
539 vapi_api_get_msg_index (vapi_ctx_t ctx, u8 *name_and_crc)
543 if (ctx->msg_index_by_name_and_crc)
545 p = hash_get_mem (ctx->msg_index_by_name_and_crc, name_and_crc);
553 vapi_connect (vapi_ctx_t ctx, const char *name, const char *chroot_prefix,
554 int max_outstanding_requests, int response_queue_size,
555 vapi_mode_e mode, bool handle_keepalives)
559 if (response_queue_size <= 0 || max_outstanding_requests <= 0)
563 if (!clib_mem_get_per_cpu_heap () && !clib_mem_init (0, 1024 * 1024 * 32))
568 ctx->requests_size = max_outstanding_requests;
569 const size_t size = ctx->requests_size * sizeof (*ctx->requests);
570 void *tmp = realloc (ctx->requests, size);
576 clib_memset (ctx->requests, 0, size);
577 /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
578 ctx->requests_start = ctx->requests_count = 0;
582 VAPI_DBG ("set memory root path `%s'", chroot_prefix);
583 vl_set_memory_root_path ((char *) chroot_prefix);
585 static char api_map[] = "/vpe-api";
586 VAPI_DBG ("client api map `%s'", api_map);
587 if ((rv = vl_map_shmem (api_map, 0 /* is_vlib */)) < 0)
589 return VAPI_EMAP_FAIL;
591 VAPI_DBG ("connect client `%s'", name);
592 if (vapi_client_connect (ctx, (char *) name, 0, response_queue_size, true) <
595 vl_client_api_unmap ();
596 return VAPI_ECON_FAIL;
598 #if VAPI_DEBUG_CONNECT
599 VAPI_DBG ("start probing messages");
603 for (i = 0; i < __vapi_metadata.count; ++i)
605 vapi_message_desc_t *m = __vapi_metadata.msgs[i];
606 u8 scratch[m->name_with_crc_len + 1];
607 memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
608 u32 id = vapi_api_get_msg_index (ctx, scratch);
610 if (VAPI_INVALID_MSG_ID != id)
614 VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
619 if (id > ctx->vl_msg_id_max)
622 realloc (ctx->vl_msg_id_to_vapi_msg_t,
623 sizeof (*ctx->vl_msg_id_to_vapi_msg_t) * (id + 1));
629 ctx->vl_msg_id_to_vapi_msg_t = tmp;
630 ctx->vl_msg_id_max = id;
632 ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
633 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
634 #if VAPI_DEBUG_CONNECT
635 VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
641 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
642 VAPI_DBG ("Message `%s' not available", m->name_with_crc);
645 #if VAPI_DEBUG_CONNECT
646 VAPI_DBG ("finished probing messages");
648 if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
649 !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
652 "control ping or control ping reply not available, cannot connect");
653 rv = VAPI_EINCOMPATIBLE;
657 ctx->connected = true;
658 if (vapi_is_msg_available (ctx, vapi_msg_id_memclnt_keepalive))
660 ctx->handle_keepalives = handle_keepalives;
664 ctx->handle_keepalives = false;
668 vapi_client_disconnect (ctx);
669 vl_client_api_unmap ();
674 * API client running in the same process as VPP
677 vapi_connect_from_vpp (vapi_ctx_t ctx, const char *name,
678 int max_outstanding_requests, int response_queue_size,
679 vapi_mode_e mode, bool handle_keepalives)
683 if (response_queue_size <= 0 || max_outstanding_requests <= 0)
688 ctx->requests_size = max_outstanding_requests;
689 const size_t size = ctx->requests_size * sizeof (*ctx->requests);
690 void *tmp = realloc (ctx->requests, size);
696 clib_memset (ctx->requests, 0, size);
697 /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
698 ctx->requests_start = ctx->requests_count = 0;
700 VAPI_DBG ("connect client `%s'", name);
701 if (vapi_client_connect (ctx, (char *) name, 0, response_queue_size,
702 handle_keepalives) < 0)
704 return VAPI_ECON_FAIL;
708 for (i = 0; i < __vapi_metadata.count; ++i)
710 vapi_message_desc_t *m = __vapi_metadata.msgs[i];
711 u8 scratch[m->name_with_crc_len + 1];
712 memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
713 u32 id = vapi_api_get_msg_index (ctx, scratch);
714 if (VAPI_INVALID_MSG_ID != id)
718 VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
723 if (id > ctx->vl_msg_id_max)
726 realloc (ctx->vl_msg_id_to_vapi_msg_t,
727 sizeof (*ctx->vl_msg_id_to_vapi_msg_t) * (id + 1));
733 ctx->vl_msg_id_to_vapi_msg_t = tmp;
734 ctx->vl_msg_id_max = id;
736 ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
737 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
741 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
742 VAPI_DBG ("Message `%s' not available", m->name_with_crc);
745 if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
746 !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
749 "control ping or control ping reply not available, cannot connect");
750 rv = VAPI_EINCOMPATIBLE;
754 ctx->connected = true;
755 if (vapi_is_msg_available (ctx, vapi_msg_id_memclnt_keepalive))
757 ctx->handle_keepalives = handle_keepalives;
761 ctx->handle_keepalives = false;
765 vapi_client_disconnect (ctx);
770 vapi_disconnect_from_vpp (vapi_ctx_t ctx)
776 vl_api_memclnt_delete_reply_t *rp;
777 svm_queue_t *vl_input_queue;
779 vl_input_queue = ctx->vl_input_queue;
780 vapi_client_send_disconnect (ctx, 0 /* wait for reply */);
783 * Have to be careful here, in case the client is disconnecting
784 * because e.g. the vlib process died, or is unresponsive.
787 vapi_error_e rv = VAPI_OK;
794 if (now >= (begin + 2))
796 clib_warning ("peer unresponsive, give up");
797 ctx->my_client_index = ~0;
801 if (svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0) < 0)
804 VL_MSG_API_UNPOISON (rp);
806 /* drain the queue */
807 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
809 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
810 vl_msg_api_free (rp);
813 vapi_memclnt_delete_reply_t_handler (
814 ctx, (void *) rp /*, ntohl (msgbuf->data_len)*/);
818 vapi_api_name_and_crc_free (ctx);
820 ctx->connected = false;
825 vapi_disconnect (vapi_ctx_t ctx)
832 vl_api_memclnt_delete_reply_t *rp;
833 svm_queue_t *vl_input_queue;
835 vl_input_queue = ctx->vl_input_queue;
836 vapi_client_send_disconnect (ctx, 0 /* wait for reply */);
839 * Have to be careful here, in case the client is disconnecting
840 * because e.g. the vlib process died, or is unresponsive.
843 vapi_error_e rv = VAPI_OK;
850 if (now >= (begin + 2))
852 clib_warning ("peer unresponsive, give up");
853 ctx->my_client_index = ~0;
857 if (svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0) < 0)
860 VL_MSG_API_UNPOISON (rp);
862 /* drain the queue */
863 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
865 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
866 vl_msg_api_free (rp);
869 vapi_memclnt_delete_reply_t_handler (
870 ctx, (void *) rp /*, ntohl (msgbuf->data_len)*/);
874 vapi_api_name_and_crc_free (ctx);
876 vl_client_api_unmap ();
878 vapi_to_be_freed_validate ();
880 ctx->connected = false;
885 vapi_get_fd (vapi_ctx_t ctx, int *fd)
891 vapi_send (vapi_ctx_t ctx, void *msg)
893 vapi_error_e rv = VAPI_OK;
894 if (!ctx || !msg || !ctx->connected)
900 svm_queue_t *q = vlibapi_get_main ()->shmem_hdr->vl_input_queue;
902 unsigned msgid = be16toh (*(u16 *) msg);
903 if (msgid <= ctx->vl_msg_id_max)
905 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
906 if (id < __vapi_metadata.count)
908 VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
909 __vapi_metadata.msgs[id]->name);
913 VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
918 VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
921 tmp = svm_queue_add (q, (u8 *) & msg,
922 VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
928 VL_MSG_API_POISON (msg);
930 VAPI_DBG ("vapi_send() rv = %d", rv);
935 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
937 vapi_error_e rv = VAPI_OK;
938 if (!ctx || !msg1 || !msg2 || !ctx->connected)
943 svm_queue_t *q = vlibapi_get_main ()->shmem_hdr->vl_input_queue;
945 unsigned msgid1 = be16toh (*(u16 *) msg1);
946 unsigned msgid2 = be16toh (*(u16 *) msg2);
947 const char *name1 = "UNKNOWN";
948 const char *name2 = "UNKNOWN";
949 if (msgid1 <= ctx->vl_msg_id_max)
951 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
952 if (id < __vapi_metadata.count)
954 name1 = __vapi_metadata.msgs[id]->name;
957 if (msgid2 <= ctx->vl_msg_id_max)
959 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
960 if (id < __vapi_metadata.count)
962 name2 = __vapi_metadata.msgs[id]->name;
965 VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
967 int tmp = svm_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
968 VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
974 VL_MSG_API_POISON (msg1);
976 VAPI_DBG ("vapi_send() rv = %d", rv);
981 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size,
982 svm_q_conditional_wait_t cond, u32 time)
984 if (!ctx || !ctx->connected || !msg || !msg_size)
988 vapi_error_e rv = VAPI_OK;
991 svm_queue_t *q = ctx->vl_input_queue;
994 VAPI_DBG ("doing shm queue sub");
996 int tmp = svm_queue_sub (q, (u8 *) & data, cond, time);
1000 VL_MSG_API_UNPOISON ((void *) data);
1001 #if VAPI_DEBUG_ALLOC
1002 vapi_add_to_be_freed ((void *) data);
1005 (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
1006 if (!msgbuf->data_len)
1008 vapi_msg_free (ctx, (u8 *) data);
1012 *msg_size = ntohl (msgbuf->data_len);
1014 unsigned msgid = be16toh (*(u16 *) * msg);
1015 if (msgid <= ctx->vl_msg_id_max)
1017 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
1018 if (id < __vapi_metadata.count)
1020 VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
1021 __vapi_metadata.msgs[id]->name);
1025 VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
1030 VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
1033 if (ctx->handle_keepalives)
1035 unsigned msgid = be16toh (*(u16 *) * msg);
1037 vapi_lookup_vl_msg_id (ctx, vapi_msg_id_memclnt_keepalive))
1039 vapi_msg_memclnt_keepalive_reply *reply = NULL;
1042 reply = vapi_msg_alloc (ctx, sizeof (*reply));
1045 reply->header.context = vapi_get_client_index (ctx);
1046 reply->header._vl_msg_id =
1047 vapi_lookup_vl_msg_id (ctx,
1048 vapi_msg_id_memclnt_keepalive_reply);
1049 reply->payload.retval = 0;
1050 vapi_msg_memclnt_keepalive_reply_hton (reply);
1051 while (VAPI_EAGAIN == vapi_send (ctx, reply));
1052 vapi_msg_free (ctx, *msg);
1065 vapi_wait (vapi_ctx_t ctx)
1067 svm_queue_lock (ctx->vl_input_queue);
1068 svm_queue_wait (ctx->vl_input_queue);
1069 svm_queue_unlock (ctx->vl_input_queue);
1075 vapi_dispatch_response (vapi_ctx_t ctx, vapi_msg_id_t id,
1076 u32 context, void *msg)
1079 if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
1081 VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
1082 return VAPI_MUTEX_FAILURE;
1084 int tmp = ctx->requests_start;
1085 const int requests_end = vapi_requests_end (ctx);
1086 while (ctx->requests[tmp].context != context && tmp != requests_end)
1089 if (tmp == ctx->requests_size)
1094 VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
1095 ctx->requests[tmp].context == context ? "matched" : "stopped",
1097 vapi_error_e rv = VAPI_OK;
1098 if (ctx->requests[tmp].context == context)
1100 while (ctx->requests_start != tmp)
1102 VAPI_ERR ("No response to req with context=%u",
1103 (unsigned) ctx->requests[tmp].context);
1104 ctx->requests[ctx->requests_start].callback (ctx, ctx->requests
1106 requests_start].callback_ctx,
1109 clib_memset (&ctx->requests[ctx->requests_start], 0,
1110 sizeof (ctx->requests[ctx->requests_start]));
1111 ++ctx->requests_start;
1112 --ctx->requests_count;
1113 if (ctx->requests_start == ctx->requests_size)
1115 ctx->requests_start = 0;
1118 // now ctx->requests_start == tmp
1119 int payload_offset = vapi_get_payload_offset (id);
1120 void *payload = ((u8 *) msg) + payload_offset;
1121 bool is_last = true;
1122 switch (ctx->requests[tmp].type)
1124 case VAPI_REQUEST_STREAM:
1125 if (ctx->requests[tmp].response_id == id)
1131 VAPI_DBG ("Stream response ID doesn't match current ID, move to "
1133 clib_memset (&ctx->requests[tmp], 0,
1134 sizeof (ctx->requests[tmp]));
1135 ++ctx->requests_start;
1136 --ctx->requests_count;
1137 if (ctx->requests_start == ctx->requests_size)
1139 ctx->requests_start = 0;
1141 tmp = ctx->requests_start;
1142 if (ctx->requests[tmp].context != context)
1144 VAPI_ERR ("Unexpected context %u, expected context %u!",
1145 ctx->requests[tmp].context, context);
1149 case VAPI_REQUEST_DUMP:
1150 if (vapi_msg_id_control_ping_reply == id)
1159 case VAPI_REQUEST_REG:
1162 if (payload_offset != -1)
1164 rv = ctx->requests[tmp].callback (
1165 ctx, ctx->requests[tmp].callback_ctx, VAPI_OK, is_last, payload);
1169 /* this is a message without payload, so bend the callback a little
1172 ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
1173 ctx->requests[tmp].callback) (ctx,
1174 ctx->requests[tmp].callback_ctx,
1179 clib_memset (&ctx->requests[ctx->requests_start], 0,
1180 sizeof (ctx->requests[ctx->requests_start]));
1181 ++ctx->requests_start;
1182 --ctx->requests_count;
1183 if (ctx->requests_start == ctx->requests_size)
1185 ctx->requests_start = 0;
1188 VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
1189 ctx->requests_start, requests_end, ctx->requests_count);
1191 if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
1193 VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
1195 abort (); /* this really shouldn't happen */
1201 vapi_dispatch_event (vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
1203 if (ctx->event_cbs[id].cb)
1205 return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
1207 else if (ctx->generic_cb.cb)
1209 return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
1214 ("No handler/generic handler for msg id %u[%s], message ignored",
1215 (unsigned) id, __vapi_metadata.msgs[id]->name);
1221 vapi_msg_is_with_context (vapi_msg_id_t id)
1223 assert (id <= __vapi_metadata.count);
1224 return __vapi_metadata.msgs[id]->has_context;
1228 vapi_verify_msg_size (vapi_msg_id_t id, void *buf, uword buf_size)
1230 assert (id < __vapi_metadata.count);
1231 return __vapi_metadata.msgs[id]->verify_msg_size (buf, buf_size);
1235 vapi_dispatch_one (vapi_ctx_t ctx)
1237 VAPI_DBG ("vapi_dispatch_one()");
1240 svm_q_conditional_wait_t cond =
1241 vapi_is_nonblocking (ctx) ? SVM_Q_NOWAIT : SVM_Q_WAIT;
1242 vapi_error_e rv = vapi_recv (ctx, &msg, &size, cond, 0);
1245 VAPI_DBG ("vapi_recv failed with rv=%d", rv);
1248 u16 vpp_id = be16toh (*(u16 *) msg);
1249 if (vpp_id > ctx->vl_msg_id_max)
1251 VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
1252 (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
1253 vapi_msg_free (ctx, msg);
1256 if (VAPI_INVALID_MSG_ID == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
1258 VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
1260 vapi_msg_free (ctx, msg);
1263 const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
1264 vapi_get_swap_to_host_func (id) (msg);
1265 if (vapi_verify_msg_size (id, msg, size))
1267 vapi_msg_free (ctx, msg);
1271 if (vapi_msg_is_with_context (id))
1273 context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
1274 /* is this a message originating from VAPI? */
1275 VAPI_DBG ("dispatch, context is %x", context);
1276 if (context & context_counter_mask)
1278 rv = vapi_dispatch_response (ctx, id, context, msg);
1282 rv = vapi_dispatch_event (ctx, id, msg);
1285 vapi_msg_free (ctx, msg);
1290 vapi_dispatch (vapi_ctx_t ctx)
1292 vapi_error_e rv = VAPI_OK;
1293 while (!vapi_requests_empty (ctx))
1295 rv = vapi_dispatch_one (ctx);
1305 vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
1306 vapi_event_cb callback, void *callback_ctx)
1308 vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
1310 c->ctx = callback_ctx;
1314 vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id)
1316 vapi_set_event_cb (ctx, id, NULL, NULL);
1320 vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
1323 ctx->generic_cb.cb = callback;
1324 ctx->generic_cb.ctx = callback_ctx;
1328 vapi_clear_generic_event_cb (vapi_ctx_t ctx)
1330 ctx->generic_cb.cb = NULL;
1331 ctx->generic_cb.ctx = NULL;
1335 vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id)
1337 assert (id < __vapi_metadata.count);
1338 return ctx->vapi_msg_id_t_to_vl_msg_id[id];
1342 vapi_get_client_index (vapi_ctx_t ctx)
1344 return ctx->my_client_index;
1348 vapi_is_nonblocking (vapi_ctx_t ctx)
1350 return (VAPI_MODE_NONBLOCKING == ctx->mode);
1354 vapi_get_max_request_count (vapi_ctx_t ctx)
1356 return ctx->requests_size - 1;
1360 vapi_get_payload_offset (vapi_msg_id_t id)
1362 assert (id < __vapi_metadata.count);
1363 return __vapi_metadata.msgs[id]->payload_offset;
1366 void (*vapi_get_swap_to_host_func (vapi_msg_id_t id)) (void *msg)
1368 assert (id < __vapi_metadata.count);
1369 return __vapi_metadata.msgs[id]->swap_to_host;
1372 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
1374 assert (id < __vapi_metadata.count);
1375 return __vapi_metadata.msgs[id]->swap_to_be;
1379 vapi_get_context_offset (vapi_msg_id_t id)
1381 assert (id < __vapi_metadata.count);
1382 return __vapi_metadata.msgs[id]->context_offset;
1386 vapi_register_msg (vapi_message_desc_t * msg)
1389 for (i = 0; i < __vapi_metadata.count; ++i)
1392 (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
1394 /* this happens if somebody is linking together several objects while
1395 * using the static inline headers, just fill in the already
1396 * assigned id here so that all the objects are in sync */
1397 msg->id = __vapi_metadata.msgs[i]->id;
1401 vapi_msg_id_t id = __vapi_metadata.count;
1402 ++__vapi_metadata.count;
1403 __vapi_metadata.msgs =
1404 realloc (__vapi_metadata.msgs,
1405 sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
1406 __vapi_metadata.msgs[id] = msg;
1407 size_t s = strlen (msg->name_with_crc);
1408 if (s > __vapi_metadata.max_len_name_with_crc)
1410 __vapi_metadata.max_len_name_with_crc = s;
1417 vapi_producer_lock (vapi_ctx_t ctx)
1420 if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
1422 VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
1423 (void) mrv; /* avoid warning if the above debug is not enabled */
1424 return VAPI_MUTEX_FAILURE;
1430 vapi_producer_unlock (vapi_ctx_t ctx)
1433 if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
1435 VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
1437 (void) mrv; /* avoid warning if the above debug is not enabled */
1438 return VAPI_MUTEX_FAILURE;
1444 vapi_get_message_count ()
1446 return __vapi_metadata.count;
1450 vapi_get_msg_name (vapi_msg_id_t id)
1452 return __vapi_metadata.msgs[id]->name;
1456 vapi_stop_rx_thread (vapi_ctx_t ctx)
1458 if (!ctx || !ctx->connected || !ctx->vl_input_queue)
1463 vl_client_stop_rx_thread (ctx->vl_input_queue);
1466 * fd.io coding-style-patch-verification: ON
1469 * eval: (c-set-style "gnu")