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;
69 static const u32 context_counter_mask = (1 << 31);
73 vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id,
76 } vapi_generic_cb_with_ctx;
80 vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, void *payload);
82 } vapi_event_cb_with_ctx;
87 int requests_size; /* size of the requests array (circular queue) */
88 int requests_start; /* index of first request */
89 int requests_count; /* number of used slots */
92 vapi_generic_cb_with_ctx generic_cb;
93 vapi_event_cb_with_ctx *event_cbs;
94 u16 *vapi_msg_id_t_to_vl_msg_id;
96 vapi_msg_id_t *vl_msg_id_to_vapi_msg_t;
98 bool handle_keepalives;
99 pthread_mutex_t requests_mutex;
101 svm_queue_t *vl_input_queue;
103 /** client message index hash table */
104 uword *msg_index_by_name_and_crc;
108 vapi_gen_req_context (vapi_ctx_t ctx)
110 ++ctx->context_counter;
111 ctx->context_counter %= context_counter_mask;
112 return ctx->context_counter | context_counter_mask;
116 vapi_get_request_count (vapi_ctx_t ctx)
118 return ctx->requests_count;
122 vapi_requests_full (vapi_ctx_t ctx)
124 return (ctx->requests_count == ctx->requests_size);
128 vapi_requests_empty (vapi_ctx_t ctx)
130 return (0 == ctx->requests_count);
134 vapi_requests_end (vapi_ctx_t ctx)
136 return (ctx->requests_start + ctx->requests_count) % ctx->requests_size;
140 vapi_store_request (vapi_ctx_t ctx, u32 context, bool is_dump,
141 vapi_cb_t callback, void *callback_ctx)
143 assert (!vapi_requests_full (ctx));
144 /* if the mutex is not held, bad things will happen */
145 assert (0 != pthread_mutex_trylock (&ctx->requests_mutex));
146 const int requests_end = vapi_requests_end (ctx);
147 vapi_req_t *slot = &ctx->requests[requests_end];
148 slot->is_dump = is_dump;
149 slot->context = context;
150 slot->callback = callback;
151 slot->callback_ctx = callback_ctx;
152 VAPI_DBG ("stored@%d: context:%x (start is @%d)", requests_end, context,
153 ctx->requests_start);
154 ++ctx->requests_count;
155 assert (!vapi_requests_empty (ctx));
159 struct to_be_freed_s;
163 struct to_be_freed_s *next;
166 static struct to_be_freed_s *to_be_freed = NULL;
169 vapi_add_to_be_freed (void *v)
171 struct to_be_freed_s *prev = NULL;
172 struct to_be_freed_s *tmp;
174 while (tmp && tmp->v)
183 tmp = to_be_freed = calloc (1, sizeof (*to_be_freed));
187 tmp = prev->next = calloc (1, sizeof (*to_be_freed));
190 VAPI_DBG ("To be freed %p", v);
195 vapi_trace_free (void *v)
197 struct to_be_freed_s *tmp = to_be_freed;
198 while (tmp && tmp->v != v)
202 if (tmp && tmp->v == v)
204 VAPI_DBG ("Freed %p", v);
209 VAPI_ERR ("Trying to free untracked pointer %p", v);
215 vapi_to_be_freed_validate ()
217 struct to_be_freed_s *tmp = to_be_freed;
222 VAPI_ERR ("Unfreed msg %p!", tmp->v);
231 vapi_msg_alloc (vapi_ctx_t ctx, size_t size)
237 void *rv = vl_msg_api_alloc_as_if_client_or_null (size);
240 clib_memset (rv, 0, size);
246 vapi_msg_free (vapi_ctx_t ctx, void *msg)
253 vapi_trace_free (msg);
255 vl_msg_api_free (msg);
259 vapi_lookup_vapi_msg_id_t (vapi_ctx_t ctx, u16 vl_msg_id)
261 if (vl_msg_id <= ctx->vl_msg_id_max)
263 return ctx->vl_msg_id_to_vapi_msg_t[vl_msg_id];
265 return VAPI_INVALID_MSG_ID;
269 vapi_ctx_alloc (vapi_ctx_t * result)
271 vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
276 ctx->context_counter = 0;
277 ctx->vapi_msg_id_t_to_vl_msg_id =
278 malloc (__vapi_metadata.count *
279 sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
280 if (!ctx->vapi_msg_id_t_to_vl_msg_id)
284 clib_memset (ctx->vapi_msg_id_t_to_vl_msg_id, ~0,
285 __vapi_metadata.count *
286 sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
287 ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
292 pthread_mutex_init (&ctx->requests_mutex, NULL);
301 vapi_ctx_free (vapi_ctx_t ctx)
303 assert (!ctx->connected);
304 free (ctx->requests);
305 free (ctx->vapi_msg_id_t_to_vl_msg_id);
306 free (ctx->event_cbs);
307 free (ctx->vl_msg_id_to_vapi_msg_t);
308 pthread_mutex_destroy (&ctx->requests_mutex);
313 vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t id)
315 return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
318 /* Cut and paste to avoid adding dependency to client library */
319 __clib_nosanitize_addr static void
320 VL_API_VEC_UNPOISON (const void *v)
322 const vec_header_t *vh = &((vec_header_t *) v)[-1];
323 clib_mem_unpoison (vh, sizeof (*vh) + vec_len (v));
327 vapi_api_name_and_crc_free (vapi_ctx_t ctx)
333 if (!ctx->msg_index_by_name_and_crc)
335 hash_foreach_pair (hp, ctx->msg_index_by_name_and_crc,
336 ({ vec_add1 (keys, (u8 *) hp->key); }));
337 for (i = 0; i < vec_len (keys); i++)
340 hash_free (ctx->msg_index_by_name_and_crc);
344 vapi_memclnt_create_v2_reply_t_handler (vapi_ctx_t ctx,
345 vl_api_memclnt_create_v2_reply_t *mp)
347 serialize_main_t _sm, *sm = &_sm;
354 ctx->my_client_index = mp->index;
356 /* Clean out any previous hash table (unlikely) */
357 vapi_api_name_and_crc_free (ctx);
359 ctx->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
361 /* Recreate the vnet-side API message handler table */
362 tblv = uword_to_pointer (mp->message_table, u8 *);
363 unserialize_open_data (sm, tblv, vec_len (tblv));
364 unserialize_integer (sm, &nmsgs, sizeof (u32));
366 VL_API_VEC_UNPOISON (tblv);
368 for (i = 0; i < nmsgs; i++)
370 msg_index = unserialize_likely_small_unsigned_integer (sm);
371 unserialize_cstring (sm, (char **) &name_and_crc);
372 hash_set_mem (ctx->msg_index_by_name_and_crc, name_and_crc, msg_index);
377 vapi_memclnt_delete_reply_t_handler (vapi_ctx_t ctx,
378 vl_api_memclnt_delete_reply_t *mp)
381 oldheap = vl_msg_push_heap ();
382 svm_queue_free (ctx->vl_input_queue);
383 vl_msg_pop_heap (oldheap);
385 ctx->my_client_index = ~0;
386 ctx->vl_input_queue = 0;
390 vapi_client_connect (vapi_ctx_t ctx, const char *name, int ctx_quota,
391 int input_queue_size, bool keepalive)
393 vl_api_memclnt_create_v2_t *mp;
394 vl_api_memclnt_create_v2_reply_t *rp;
395 svm_queue_t *vl_input_queue;
396 vl_shmem_hdr_t *shmem_hdr;
399 api_main_t *am = vlibapi_get_main ();
401 shmem_hdr = am->shmem_hdr;
403 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
405 clib_warning ("shmem_hdr / input queue NULL");
409 clib_mem_unpoison (shmem_hdr, sizeof (*shmem_hdr));
410 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
412 oldheap = vl_msg_push_heap ();
414 svm_queue_alloc_and_init (input_queue_size, sizeof (uword), getpid ());
415 vl_msg_pop_heap (oldheap);
417 ctx->my_client_index = ~0;
418 ctx->vl_input_queue = vl_input_queue;
420 mp = vl_msg_api_alloc_as_if_client (sizeof (vl_api_memclnt_create_v2_t));
421 clib_memset (mp, 0, sizeof (*mp));
422 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_V2);
423 mp->ctx_quota = ctx_quota;
424 mp->input_queue = (uword) vl_input_queue;
425 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
426 mp->keepalive = keepalive;
428 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) &mp);
433 struct timespec ts, tsrem;
436 /* Wait up to 10 seconds */
437 for (i = 0; i < 1000; i++)
440 svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0);
444 ts.tv_nsec = 10000 * 1000; /* 10 ms */
445 while (nanosleep (&ts, &tsrem) < 0)
452 VL_MSG_API_UNPOISON (rp);
453 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_V2_REPLY)
455 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
458 rv = clib_net_to_host_u32 (rp->response);
459 vapi_memclnt_create_v2_reply_t_handler (ctx, rp);
466 vapi_api_get_msg_index (vapi_ctx_t ctx, u8 *name_and_crc)
470 if (ctx->msg_index_by_name_and_crc)
472 p = hash_get_mem (ctx->msg_index_by_name_and_crc, name_and_crc);
480 vapi_connect (vapi_ctx_t ctx, const char *name, const char *chroot_prefix,
481 int max_outstanding_requests, int response_queue_size,
482 vapi_mode_e mode, bool handle_keepalives)
486 if (response_queue_size <= 0 || max_outstanding_requests <= 0)
490 if (!clib_mem_get_per_cpu_heap () && !clib_mem_init (0, 1024 * 1024 * 32))
495 ctx->requests_size = max_outstanding_requests;
496 const size_t size = ctx->requests_size * sizeof (*ctx->requests);
497 void *tmp = realloc (ctx->requests, size);
503 clib_memset (ctx->requests, 0, size);
504 /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
505 ctx->requests_start = ctx->requests_count = 0;
509 VAPI_DBG ("set memory root path `%s'", chroot_prefix);
510 vl_set_memory_root_path ((char *) chroot_prefix);
512 static char api_map[] = "/vpe-api";
513 VAPI_DBG ("client api map `%s'", api_map);
514 if ((rv = vl_map_shmem (api_map, 0 /* is_vlib */)) < 0)
516 return VAPI_EMAP_FAIL;
518 VAPI_DBG ("connect client `%s'", name);
519 if (vapi_client_connect (ctx, (char *) name, 0, response_queue_size, true) <
522 vl_client_api_unmap ();
523 return VAPI_ECON_FAIL;
525 #if VAPI_DEBUG_CONNECT
526 VAPI_DBG ("start probing messages");
530 for (i = 0; i < __vapi_metadata.count; ++i)
532 vapi_message_desc_t *m = __vapi_metadata.msgs[i];
533 u8 scratch[m->name_with_crc_len + 1];
534 memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
535 u32 id = vapi_api_get_msg_index (ctx, scratch);
537 if (VAPI_INVALID_MSG_ID != id)
541 VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
546 if (id > ctx->vl_msg_id_max)
549 realloc (ctx->vl_msg_id_to_vapi_msg_t,
550 sizeof (*ctx->vl_msg_id_to_vapi_msg_t) * (id + 1));
556 ctx->vl_msg_id_to_vapi_msg_t = tmp;
557 ctx->vl_msg_id_max = id;
559 ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
560 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
561 #if VAPI_DEBUG_CONNECT
562 VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
568 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
569 VAPI_DBG ("Message `%s' not available", m->name_with_crc);
572 #if VAPI_DEBUG_CONNECT
573 VAPI_DBG ("finished probing messages");
575 if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
576 !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
579 "control ping or control ping reply not available, cannot connect");
580 rv = VAPI_EINCOMPATIBLE;
584 ctx->connected = true;
585 if (vapi_is_msg_available (ctx, vapi_msg_id_memclnt_keepalive))
587 ctx->handle_keepalives = handle_keepalives;
591 ctx->handle_keepalives = false;
595 vl_client_disconnect ();
596 vl_client_api_unmap ();
601 * API client running in the same process as VPP
604 vapi_connect_from_vpp (vapi_ctx_t ctx, const char *name,
605 int max_outstanding_requests, int response_queue_size,
606 vapi_mode_e mode, bool handle_keepalives)
610 if (response_queue_size <= 0 || max_outstanding_requests <= 0)
615 ctx->requests_size = max_outstanding_requests;
616 const size_t size = ctx->requests_size * sizeof (*ctx->requests);
617 void *tmp = realloc (ctx->requests, size);
623 clib_memset (ctx->requests, 0, size);
624 /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
625 ctx->requests_start = ctx->requests_count = 0;
627 VAPI_DBG ("connect client `%s'", name);
628 if (vapi_client_connect (ctx, (char *) name, 0, response_queue_size,
629 handle_keepalives) < 0)
631 return VAPI_ECON_FAIL;
635 for (i = 0; i < __vapi_metadata.count; ++i)
637 vapi_message_desc_t *m = __vapi_metadata.msgs[i];
638 u8 scratch[m->name_with_crc_len + 1];
639 memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
640 u32 id = vapi_api_get_msg_index (ctx, scratch);
641 if (VAPI_INVALID_MSG_ID != id)
645 VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
650 if (id > ctx->vl_msg_id_max)
653 realloc (ctx->vl_msg_id_to_vapi_msg_t,
654 sizeof (*ctx->vl_msg_id_to_vapi_msg_t) * (id + 1));
660 ctx->vl_msg_id_to_vapi_msg_t = tmp;
661 ctx->vl_msg_id_max = id;
663 ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
664 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
668 ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
669 VAPI_DBG ("Message `%s' not available", m->name_with_crc);
672 if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
673 !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
676 "control ping or control ping reply not available, cannot connect");
677 rv = VAPI_EINCOMPATIBLE;
681 ctx->connected = true;
682 if (vapi_is_msg_available (ctx, vapi_msg_id_memclnt_keepalive))
684 ctx->handle_keepalives = handle_keepalives;
688 ctx->handle_keepalives = false;
692 vl_client_disconnect ();
697 vapi_disconnect_from_vpp (vapi_ctx_t ctx)
703 vl_api_memclnt_delete_reply_t *rp;
704 svm_queue_t *vl_input_queue;
706 vl_input_queue = ctx->vl_input_queue;
707 vl_client_send_disconnect (0 /* wait for reply */);
710 * Have to be careful here, in case the client is disconnecting
711 * because e.g. the vlib process died, or is unresponsive.
714 vapi_error_e rv = VAPI_OK;
721 if (now >= (begin + 2))
723 clib_warning ("peer unresponsive, give up");
724 ctx->my_client_index = ~0;
728 if (svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0) < 0)
731 VL_MSG_API_UNPOISON (rp);
733 /* drain the queue */
734 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
736 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
737 vl_msg_api_free (rp);
740 vapi_memclnt_delete_reply_t_handler (
741 ctx, (void *) rp /*, ntohl (msgbuf->data_len)*/);
745 vapi_api_name_and_crc_free (ctx);
747 ctx->connected = false;
752 vapi_disconnect (vapi_ctx_t ctx)
759 vl_api_memclnt_delete_reply_t *rp;
760 svm_queue_t *vl_input_queue;
762 vl_input_queue = ctx->vl_input_queue;
763 vl_client_send_disconnect (0 /* wait for reply */);
766 * Have to be careful here, in case the client is disconnecting
767 * because e.g. the vlib process died, or is unresponsive.
770 vapi_error_e rv = VAPI_OK;
777 if (now >= (begin + 2))
779 clib_warning ("peer unresponsive, give up");
780 ctx->my_client_index = ~0;
784 if (svm_queue_sub (vl_input_queue, (u8 *) &rp, SVM_Q_NOWAIT, 0) < 0)
787 VL_MSG_API_UNPOISON (rp);
789 /* drain the queue */
790 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
792 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
793 vl_msg_api_free (rp);
796 vapi_memclnt_delete_reply_t_handler (
797 ctx, (void *) rp /*, ntohl (msgbuf->data_len)*/);
801 vapi_api_name_and_crc_free (ctx);
803 vl_client_api_unmap ();
805 vapi_to_be_freed_validate ();
807 ctx->connected = false;
812 vapi_get_fd (vapi_ctx_t ctx, int *fd)
818 vapi_send (vapi_ctx_t ctx, void *msg)
820 vapi_error_e rv = VAPI_OK;
821 if (!ctx || !msg || !ctx->connected)
827 svm_queue_t *q = vlibapi_get_main ()->shmem_hdr->vl_input_queue;
829 unsigned msgid = be16toh (*(u16 *) msg);
830 if (msgid <= ctx->vl_msg_id_max)
832 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
833 if (id < __vapi_metadata.count)
835 VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
836 __vapi_metadata.msgs[id]->name);
840 VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
845 VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
848 tmp = svm_queue_add (q, (u8 *) & msg,
849 VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
855 VL_MSG_API_POISON (msg);
857 VAPI_DBG ("vapi_send() rv = %d", rv);
862 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
864 vapi_error_e rv = VAPI_OK;
865 if (!ctx || !msg1 || !msg2 || !ctx->connected)
870 svm_queue_t *q = vlibapi_get_main ()->shmem_hdr->vl_input_queue;
872 unsigned msgid1 = be16toh (*(u16 *) msg1);
873 unsigned msgid2 = be16toh (*(u16 *) msg2);
874 const char *name1 = "UNKNOWN";
875 const char *name2 = "UNKNOWN";
876 if (msgid1 <= ctx->vl_msg_id_max)
878 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
879 if (id < __vapi_metadata.count)
881 name1 = __vapi_metadata.msgs[id]->name;
884 if (msgid2 <= ctx->vl_msg_id_max)
886 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
887 if (id < __vapi_metadata.count)
889 name2 = __vapi_metadata.msgs[id]->name;
892 VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
894 int tmp = svm_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
895 VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
901 VL_MSG_API_POISON (msg1);
903 VAPI_DBG ("vapi_send() rv = %d", rv);
908 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size,
909 svm_q_conditional_wait_t cond, u32 time)
911 if (!ctx || !ctx->connected || !msg || !msg_size)
915 vapi_error_e rv = VAPI_OK;
918 svm_queue_t *q = ctx->vl_input_queue;
921 VAPI_DBG ("doing shm queue sub");
923 int tmp = svm_queue_sub (q, (u8 *) & data, cond, time);
927 VL_MSG_API_UNPOISON ((void *) data);
929 vapi_add_to_be_freed ((void *) data);
932 (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
933 if (!msgbuf->data_len)
935 vapi_msg_free (ctx, (u8 *) data);
939 *msg_size = ntohl (msgbuf->data_len);
941 unsigned msgid = be16toh (*(u16 *) * msg);
942 if (msgid <= ctx->vl_msg_id_max)
944 vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
945 if (id < __vapi_metadata.count)
947 VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
948 __vapi_metadata.msgs[id]->name);
952 VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
957 VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
960 if (ctx->handle_keepalives)
962 unsigned msgid = be16toh (*(u16 *) * msg);
964 vapi_lookup_vl_msg_id (ctx, vapi_msg_id_memclnt_keepalive))
966 vapi_msg_memclnt_keepalive_reply *reply = NULL;
969 reply = vapi_msg_alloc (ctx, sizeof (*reply));
972 reply->header.context = vapi_get_client_index (ctx);
973 reply->header._vl_msg_id =
974 vapi_lookup_vl_msg_id (ctx,
975 vapi_msg_id_memclnt_keepalive_reply);
976 reply->payload.retval = 0;
977 vapi_msg_memclnt_keepalive_reply_hton (reply);
978 while (VAPI_EAGAIN == vapi_send (ctx, reply));
979 vapi_msg_free (ctx, *msg);
992 vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode)
998 vapi_dispatch_response (vapi_ctx_t ctx, vapi_msg_id_t id,
999 u32 context, void *msg)
1002 if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
1004 VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
1005 return VAPI_MUTEX_FAILURE;
1007 int tmp = ctx->requests_start;
1008 const int requests_end = vapi_requests_end (ctx);
1009 while (ctx->requests[tmp].context != context && tmp != requests_end)
1012 if (tmp == ctx->requests_size)
1017 VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
1018 ctx->requests[tmp].context == context ? "matched" : "stopped",
1020 vapi_error_e rv = VAPI_OK;
1021 if (ctx->requests[tmp].context == context)
1023 while (ctx->requests_start != tmp)
1025 VAPI_ERR ("No response to req with context=%u",
1026 (unsigned) ctx->requests[tmp].context);
1027 ctx->requests[ctx->requests_start].callback (ctx, ctx->requests
1029 requests_start].callback_ctx,
1032 clib_memset (&ctx->requests[ctx->requests_start], 0,
1033 sizeof (ctx->requests[ctx->requests_start]));
1034 ++ctx->requests_start;
1035 --ctx->requests_count;
1036 if (ctx->requests_start == ctx->requests_size)
1038 ctx->requests_start = 0;
1041 // now ctx->requests_start == tmp
1042 int payload_offset = vapi_get_payload_offset (id);
1043 void *payload = ((u8 *) msg) + payload_offset;
1044 bool is_last = true;
1045 if (ctx->requests[tmp].is_dump)
1047 if (vapi_msg_id_control_ping_reply == id)
1056 if (payload_offset != -1)
1058 rv = ctx->requests[tmp].callback (
1059 ctx, ctx->requests[tmp].callback_ctx, VAPI_OK, is_last, payload);
1063 /* this is a message without payload, so bend the callback a little
1066 ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
1067 ctx->requests[tmp].callback) (ctx,
1068 ctx->requests[tmp].callback_ctx,
1073 clib_memset (&ctx->requests[ctx->requests_start], 0,
1074 sizeof (ctx->requests[ctx->requests_start]));
1075 ++ctx->requests_start;
1076 --ctx->requests_count;
1077 if (ctx->requests_start == ctx->requests_size)
1079 ctx->requests_start = 0;
1082 VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
1083 ctx->requests_start, requests_end, ctx->requests_count);
1085 if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
1087 VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
1089 abort (); /* this really shouldn't happen */
1095 vapi_dispatch_event (vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
1097 if (ctx->event_cbs[id].cb)
1099 return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
1101 else if (ctx->generic_cb.cb)
1103 return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
1108 ("No handler/generic handler for msg id %u[%s], message ignored",
1109 (unsigned) id, __vapi_metadata.msgs[id]->name);
1115 vapi_msg_is_with_context (vapi_msg_id_t id)
1117 assert (id <= __vapi_metadata.count);
1118 return __vapi_metadata.msgs[id]->has_context;
1122 vapi_verify_msg_size (vapi_msg_id_t id, void *buf, uword buf_size)
1124 assert (id < __vapi_metadata.count);
1125 return __vapi_metadata.msgs[id]->verify_msg_size (buf, buf_size);
1129 vapi_dispatch_one (vapi_ctx_t ctx)
1131 VAPI_DBG ("vapi_dispatch_one()");
1134 svm_q_conditional_wait_t cond =
1135 vapi_is_nonblocking (ctx) ? SVM_Q_NOWAIT : SVM_Q_WAIT;
1136 vapi_error_e rv = vapi_recv (ctx, &msg, &size, cond, 0);
1139 VAPI_DBG ("vapi_recv failed with rv=%d", rv);
1142 u16 vpp_id = be16toh (*(u16 *) msg);
1143 if (vpp_id > ctx->vl_msg_id_max)
1145 VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
1146 (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
1147 vapi_msg_free (ctx, msg);
1150 if (VAPI_INVALID_MSG_ID == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
1152 VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
1154 vapi_msg_free (ctx, msg);
1157 const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
1158 if (vapi_verify_msg_size (id, msg, size))
1160 vapi_msg_free (ctx, msg);
1164 vapi_get_swap_to_host_func (id) (msg);
1165 if (vapi_msg_is_with_context (id))
1167 context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
1168 /* is this a message originating from VAPI? */
1169 VAPI_DBG ("dispatch, context is %x", context);
1170 if (context & context_counter_mask)
1172 rv = vapi_dispatch_response (ctx, id, context, msg);
1176 rv = vapi_dispatch_event (ctx, id, msg);
1179 vapi_msg_free (ctx, msg);
1184 vapi_dispatch (vapi_ctx_t ctx)
1186 vapi_error_e rv = VAPI_OK;
1187 while (!vapi_requests_empty (ctx))
1189 rv = vapi_dispatch_one (ctx);
1199 vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
1200 vapi_event_cb callback, void *callback_ctx)
1202 vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
1204 c->ctx = callback_ctx;
1208 vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id)
1210 vapi_set_event_cb (ctx, id, NULL, NULL);
1214 vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
1217 ctx->generic_cb.cb = callback;
1218 ctx->generic_cb.ctx = callback_ctx;
1222 vapi_clear_generic_event_cb (vapi_ctx_t ctx)
1224 ctx->generic_cb.cb = NULL;
1225 ctx->generic_cb.ctx = NULL;
1229 vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id)
1231 assert (id < __vapi_metadata.count);
1232 return ctx->vapi_msg_id_t_to_vl_msg_id[id];
1236 vapi_get_client_index (vapi_ctx_t ctx)
1238 return ctx->my_client_index;
1242 vapi_is_nonblocking (vapi_ctx_t ctx)
1244 return (VAPI_MODE_NONBLOCKING == ctx->mode);
1248 vapi_get_max_request_count (vapi_ctx_t ctx)
1250 return ctx->requests_size - 1;
1254 vapi_get_payload_offset (vapi_msg_id_t id)
1256 assert (id < __vapi_metadata.count);
1257 return __vapi_metadata.msgs[id]->payload_offset;
1260 void (*vapi_get_swap_to_host_func (vapi_msg_id_t id)) (void *msg)
1262 assert (id < __vapi_metadata.count);
1263 return __vapi_metadata.msgs[id]->swap_to_host;
1266 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
1268 assert (id < __vapi_metadata.count);
1269 return __vapi_metadata.msgs[id]->swap_to_be;
1273 vapi_get_context_offset (vapi_msg_id_t id)
1275 assert (id < __vapi_metadata.count);
1276 return __vapi_metadata.msgs[id]->context_offset;
1280 vapi_register_msg (vapi_message_desc_t * msg)
1283 for (i = 0; i < __vapi_metadata.count; ++i)
1286 (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
1288 /* this happens if somebody is linking together several objects while
1289 * using the static inline headers, just fill in the already
1290 * assigned id here so that all the objects are in sync */
1291 msg->id = __vapi_metadata.msgs[i]->id;
1295 vapi_msg_id_t id = __vapi_metadata.count;
1296 ++__vapi_metadata.count;
1297 __vapi_metadata.msgs =
1298 realloc (__vapi_metadata.msgs,
1299 sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
1300 __vapi_metadata.msgs[id] = msg;
1301 size_t s = strlen (msg->name_with_crc);
1302 if (s > __vapi_metadata.max_len_name_with_crc)
1304 __vapi_metadata.max_len_name_with_crc = s;
1311 vapi_producer_lock (vapi_ctx_t ctx)
1314 if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
1316 VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
1317 (void) mrv; /* avoid warning if the above debug is not enabled */
1318 return VAPI_MUTEX_FAILURE;
1324 vapi_producer_unlock (vapi_ctx_t ctx)
1327 if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
1329 VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
1331 (void) mrv; /* avoid warning if the above debug is not enabled */
1332 return VAPI_MUTEX_FAILURE;
1338 vapi_get_message_count ()
1340 return __vapi_metadata.count;
1344 vapi_get_msg_name (vapi_msg_id_t id)
1346 return __vapi_metadata.msgs[id]->name;
1350 * fd.io coding-style-patch-verification: ON
1353 * eval: (c-set-style "gnu")