FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
vapi.c
Go to the documentation of this file.
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>
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 */
37 
38 struct
39 {
40  size_t count;
43 } __vapi_metadata;
44 
45 typedef struct
46 {
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;
61 
62 typedef struct
63 {
64  vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, void *payload);
65  void *ctx;
67 
68 struct vapi_ctx_s
69 {
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 */
81  bool connected;
82  pthread_mutex_t requests_mutex;
83 };
84 
85 u32
87 {
88  ++ctx->context_counter;
91 }
92 
93 size_t
95 {
96  return ctx->requests_count;
97 }
98 
99 bool
101 {
102  return (ctx->requests_count == ctx->requests_size);
103 }
104 
105 bool
107 {
108  return (0 == ctx->requests_count);
109 }
110 
111 static int
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 *
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
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 
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 
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;
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
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
285 {
286  return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
287 }
288 
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  /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
309  ctx->requests_start = ctx->requests_count = 0;
310  if (chroot_prefix)
311  {
312  VAPI_DBG ("set memory root path `%s'", chroot_prefix);
313  vl_set_memory_root_path ((char *) chroot_prefix);
314  }
315  static char api_map[] = "/vpe-api";
316  VAPI_DBG ("client api map `%s'", api_map);
317  if ((vl_client_api_map (api_map)) < 0)
318  {
319  return VAPI_EMAP_FAIL;
320  }
321  VAPI_DBG ("connect client `%s'", name);
322  if (vl_client_connect ((char *) name, 0, response_queue_size) < 0)
323  {
325  return VAPI_ECON_FAIL;
326  }
327 #if VAPI_DEBUG_CONNECT
328  VAPI_DBG ("start probing messages");
329 #endif
330  int rv;
331  int i;
332  for (i = 0; i < __vapi_metadata.count; ++i)
333  {
334  vapi_message_desc_t *m = __vapi_metadata.msgs[i];
335  u8 scratch[m->name_with_crc_len + 1];
336  memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
337  u32 id = vl_api_get_msg_index (scratch);
338  if (~0 != id)
339  {
340  if (id > UINT16_MAX)
341  {
342  VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
343  UINT16_MAX);
344  rv = VAPI_EINVAL;
345  goto fail;
346  }
347  if (id > ctx->vl_msg_id_max)
348  {
349  vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t,
350  sizeof
351  (*ctx->vl_msg_id_to_vapi_msg_t) *
352  (id + 1));
353  if (!tmp)
354  {
355  rv = VAPI_ENOMEM;
356  goto fail;
357  }
358  ctx->vl_msg_id_to_vapi_msg_t = tmp;
359  ctx->vl_msg_id_max = id;
360  }
361  ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
362  ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
363 #if VAPI_DEBUG_CONNECT
364  VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
365  (unsigned) id);
366 #endif
367  }
368  else
369  {
370  ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
371  VAPI_DBG ("Message `%s' not available", m->name_with_crc);
372  }
373  }
374 #if VAPI_DEBUG_CONNECT
375  VAPI_DBG ("finished probing messages");
376 #endif
377  if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
378  !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
379  {
380  VAPI_ERR
381  ("control ping or control ping reply not available, cannot connect");
382  rv = VAPI_EINCOMPATIBLE;
383  goto fail;
384  }
385  ctx->mode = mode;
386  ctx->connected = true;
387  return VAPI_OK;
388 fail:
391  return rv;
392 }
393 
396 {
397  if (!ctx->connected)
398  {
399  return VAPI_EINVAL;
400  }
403 #if VAPI_DEBUG_ALLOC
404  vapi_to_be_freed_validate ();
405 #endif
406  ctx->connected = false;
407  return VAPI_OK;
408 }
409 
412 {
413  return VAPI_ENOTSUP;
414 }
415 
418 {
419  vapi_error_e rv = VAPI_OK;
420  if (!ctx || !msg || !ctx->connected)
421  {
422  rv = VAPI_EINVAL;
423  goto out;
424  }
425  int tmp;
427 #if VAPI_DEBUG
428  unsigned msgid = be16toh (*(u16 *) msg);
429  if (msgid <= ctx->vl_msg_id_max)
430  {
431  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
432  if (id < __vapi_metadata.count)
433  {
434  VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
435  __vapi_metadata.msgs[id]->name);
436  }
437  else
438  {
439  VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
440  }
441  }
442  else
443  {
444  VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
445  }
446 #endif
447  tmp = unix_shared_memory_queue_add (q, (u8 *) & msg,
449  ctx->mode ? 0 : 1);
450  if (tmp < 0)
451  {
452  rv = VAPI_EAGAIN;
453  }
454 out:
455  VAPI_DBG ("vapi_send() rv = %d", rv);
456  return rv;
457 }
458 
460 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
461 {
462  vapi_error_e rv = VAPI_OK;
463  if (!ctx || !msg1 || !msg2 || !ctx->connected)
464  {
465  rv = VAPI_EINVAL;
466  goto out;
467  }
469 #if VAPI_DEBUG
470  unsigned msgid1 = be16toh (*(u16 *) msg1);
471  unsigned msgid2 = be16toh (*(u16 *) msg2);
472  const char *name1 = "UNKNOWN";
473  const char *name2 = "UNKNOWN";
474  if (msgid1 <= ctx->vl_msg_id_max)
475  {
476  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
477  if (id < __vapi_metadata.count)
478  {
479  name1 = __vapi_metadata.msgs[id]->name;
480  }
481  }
482  if (msgid2 <= ctx->vl_msg_id_max)
483  {
484  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
485  if (id < __vapi_metadata.count)
486  {
487  name2 = __vapi_metadata.msgs[id]->name;
488  }
489  }
490  VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
491 #endif
492  int tmp = unix_shared_memory_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
494  ctx->mode ? 0 : 1);
495  if (tmp < 0)
496  {
497  rv = VAPI_EAGAIN;
498  }
499 out:
500  VAPI_DBG ("vapi_send() rv = %d", rv);
501  return rv;
502 }
503 
505 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size)
506 {
507  if (!ctx || !ctx->connected || !msg || !msg_size)
508  {
509  return VAPI_EINVAL;
510  }
511  vapi_error_e rv = VAPI_OK;
512  api_main_t *am = &api_main;
513  uword data;
514 
515  if (am->our_pid == 0)
516  {
517  return VAPI_EINVAL;
518  }
519 
521  VAPI_DBG ("doing shm queue sub");
522  int tmp = unix_shared_memory_queue_sub (q, (u8 *) & data, 0);
523  if (tmp == 0)
524  {
525 #if VAPI_DEBUG_ALLOC
526  vapi_add_to_be_freed ((void *) data);
527 #endif
528  msgbuf_t *msgbuf =
529  (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
530  if (!msgbuf->data_len)
531  {
532  vapi_msg_free (ctx, (u8 *) data);
533  return VAPI_EAGAIN;
534  }
535  *msg = (u8 *) data;
536  *msg_size = ntohl (msgbuf->data_len);
537 #if VAPI_DEBUG
538  unsigned msgid = be16toh (*(u16 *) * msg);
539  if (msgid <= ctx->vl_msg_id_max)
540  {
541  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
542  if (id < __vapi_metadata.count)
543  {
544  VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
545  __vapi_metadata.msgs[id]->name);
546  }
547  else
548  {
549  VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
550  }
551  }
552  else
553  {
554  VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
555  }
556 #endif
557  }
558  else
559  {
560  rv = VAPI_EAGAIN;
561  }
562  return rv;
563 }
564 
567 {
568  return VAPI_ENOTSUP;
569 }
570 
571 static vapi_error_e
573  u32 context, void *msg)
574 {
575  int mrv;
576  if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
577  {
578  VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
579  return VAPI_MUTEX_FAILURE;
580  }
581  int tmp = ctx->requests_start;
582  const int requests_end = vapi_requests_end (ctx);
583  while (ctx->requests[tmp].context != context && tmp != requests_end)
584  {
585  ++tmp;
586  if (tmp == ctx->requests_size)
587  {
588  tmp = 0;
589  }
590  }
591  VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
592  ctx->requests[tmp].context == context ? "matched" : "stopped",
593  tmp);
594  vapi_error_e rv = VAPI_OK;
595  if (ctx->requests[tmp].context == context)
596  {
597  while (ctx->requests_start != tmp)
598  {
599  VAPI_ERR ("No response to req with context=%u",
600  (unsigned) ctx->requests[tmp].context);
601  ctx->requests[ctx->requests_start].callback (ctx,
602  ctx->requests
603  [ctx->
605  VAPI_ENORESP, true,
606  NULL);
607  memset (&ctx->requests[ctx->requests_start], 0,
608  sizeof (ctx->requests[ctx->requests_start]));
609  ++ctx->requests_start;
610  --ctx->requests_count;
611  if (ctx->requests_start == ctx->requests_size)
612  {
613  ctx->requests_start = 0;
614  }
615  }
616  // now ctx->requests_start == tmp
617  int payload_offset = vapi_get_payload_offset (id);
618  void *payload = ((u8 *) msg) + payload_offset;
619  bool is_last = true;
620  if (ctx->requests[tmp].is_dump)
621  {
622  if (vapi_msg_id_control_ping_reply == id)
623  {
624  payload = NULL;
625  }
626  else
627  {
628  is_last = false;
629  }
630  }
631  if (payload_offset != -1)
632  {
633  rv =
634  ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx,
635  VAPI_OK, is_last, payload);
636  }
637  else
638  {
639  /* this is a message without payload, so bend the callback a little
640  */
641  rv =
642  ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
643  ctx->requests[tmp].callback) (ctx,
644  ctx->requests[tmp].callback_ctx,
645  VAPI_OK, is_last);
646  }
647  if (is_last)
648  {
649  memset (&ctx->requests[ctx->requests_start], 0,
650  sizeof (ctx->requests[ctx->requests_start]));
651  ++ctx->requests_start;
652  --ctx->requests_count;
653  if (ctx->requests_start == ctx->requests_size)
654  {
655  ctx->requests_start = 0;
656  }
657  }
658  VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
659  ctx->requests_start, requests_end, ctx->requests_count);
660  }
661  if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
662  {
663  VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
664  strerror (mrv));
665  abort (); /* this really shouldn't happen */
666  }
667  return rv;
668 }
669 
670 static vapi_error_e
672 {
673  if (ctx->event_cbs[id].cb)
674  {
675  return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
676  }
677  else if (ctx->generic_cb.cb)
678  {
679  return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
680  }
681  else
682  {
683  VAPI_DBG
684  ("No handler/generic handler for msg id %u[%s], message ignored",
685  (unsigned) id, __vapi_metadata.msgs[id]->name);
686  }
687  return VAPI_OK;
688 }
689 
690 bool
692 {
693  assert (id <= __vapi_metadata.count);
694  return __vapi_metadata.msgs[id]->has_context;
695 }
696 
699 {
700  VAPI_DBG ("vapi_dispatch_one()");
701  void *msg;
702  size_t size;
703  vapi_error_e rv = vapi_recv (ctx, &msg, &size);
704  if (VAPI_OK != rv)
705  {
706  VAPI_DBG ("vapi_recv failed with rv=%d", rv);
707  return rv;
708  }
709  u16 vpp_id = be16toh (*(u16 *) msg);
710  if (vpp_id > ctx->vl_msg_id_max)
711  {
712  VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
713  (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
714  vapi_msg_free (ctx, msg);
715  return VAPI_EINVAL;
716  }
717  if (~0 == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
718  {
719  VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
720  (unsigned) vpp_id);
721  vapi_msg_free (ctx, msg);
722  return VAPI_EINVAL;
723  }
724  const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
725  const size_t expect_size = vapi_get_message_size (id);
726  if (size < expect_size)
727  {
728  VAPI_ERR
729  ("Invalid msg received, unexpected size `%zu' < expected min `%zu'",
730  size, expect_size);
731  vapi_msg_free (ctx, msg);
732  return VAPI_EINVAL;
733  }
734  u32 context;
735  vapi_get_swap_to_host_func (id) (msg);
736  if (vapi_msg_is_with_context (id))
737  {
738  context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
739  /* is this a message originating from VAPI? */
740  VAPI_DBG ("dispatch, context is %x", context);
741  if (context & context_counter_mask)
742  {
743  rv = vapi_dispatch_response (ctx, id, context, msg);
744  goto done;
745  }
746  }
747  rv = vapi_dispatch_event (ctx, id, msg);
748 
749 done:
750  vapi_msg_free (ctx, msg);
751  return rv;
752 }
753 
756 {
757  vapi_error_e rv = VAPI_OK;
758  while (!vapi_requests_empty (ctx))
759  {
760  rv = vapi_dispatch_one (ctx);
761  if (VAPI_OK != rv)
762  {
763  return rv;
764  }
765  }
766  return rv;
767 }
768 
769 void
771  vapi_event_cb callback, void *callback_ctx)
772 {
773  vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
774  c->cb = callback;
775  c->ctx = callback_ctx;
776 }
777 
778 void
780 {
781  vapi_set_event_cb (ctx, id, NULL, NULL);
782 }
783 
784 void
786  void *callback_ctx)
787 {
788  ctx->generic_cb.cb = callback;
789  ctx->generic_cb.ctx = callback_ctx;
790 }
791 
792 void
794 {
795  ctx->generic_cb.cb = NULL;
796  ctx->generic_cb.ctx = NULL;
797 }
798 
799 u16
801 {
802  assert (id < __vapi_metadata.count);
803  return ctx->vapi_msg_id_t_to_vl_msg_id[id];
804 }
805 
806 int
808 {
809  return api_main.my_client_index;
810 }
811 
812 bool
814 {
815  return (VAPI_MODE_NONBLOCKING == ctx->mode);
816 }
817 
818 size_t
820 {
821  return ctx->requests_size - 1;
822 }
823 
824 int
826 {
827  assert (id < __vapi_metadata.count);
828  return __vapi_metadata.msgs[id]->payload_offset;
829 }
830 
832 {
833  assert (id < __vapi_metadata.count);
834  return __vapi_metadata.msgs[id]->swap_to_host;
835 }
836 
837 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
838 {
839  assert (id < __vapi_metadata.count);
840  return __vapi_metadata.msgs[id]->swap_to_be;
841 }
842 
843 size_t
845 {
846  assert (id < __vapi_metadata.count);
847  return __vapi_metadata.msgs[id]->size;
848 }
849 
850 size_t
852 {
853  assert (id < __vapi_metadata.count);
854  return __vapi_metadata.msgs[id]->context_offset;
855 }
856 
859 {
860  int i = 0;
861  for (i = 0; i < __vapi_metadata.count; ++i)
862  {
863  if (!strcmp
864  (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
865  {
866  /* this happens if somebody is linking together several objects while
867  * using the static inline headers, just fill in the already
868  * assigned id here so that all the objects are in sync */
869  msg->id = __vapi_metadata.msgs[i]->id;
870  return msg->id;
871  }
872  }
873  vapi_msg_id_t id = __vapi_metadata.count;
874  ++__vapi_metadata.count;
875  __vapi_metadata.msgs =
876  realloc (__vapi_metadata.msgs,
877  sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
878  __vapi_metadata.msgs[id] = msg;
879  size_t s = strlen (msg->name_with_crc);
880  if (s > __vapi_metadata.max_len_name_with_crc)
881  {
882  __vapi_metadata.max_len_name_with_crc = s;
883  }
884  msg->id = id;
885  return id;
886 }
887 
890 {
891  int mrv;
892  if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
893  {
894  VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
895  (void) mrv; /* avoid warning if the above debug is not enabled */
896  return VAPI_MUTEX_FAILURE;
897  }
898  return VAPI_OK;
899 }
900 
903 {
904  int mrv;
905  if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
906  {
907  VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
908  strerror (mrv));
909  (void) mrv; /* avoid warning if the above debug is not enabled */
910  return VAPI_MUTEX_FAILURE;
911  }
912  return VAPI_OK;
913 }
914 
915 size_t
917 {
918  return __vapi_metadata.count;
919 }
920 
921 const char *
923 {
924  return __vapi_metadata.msgs[id]->name;
925 }
926 
927 /*
928  * fd.io coding-style-patch-verification: ON
929  *
930  * Local Variables:
931  * eval: (c-set-style "gnu")
932  * End:
933  */
failure manipulating internal mutex(es)
Definition: vapi_common.h:37
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
vapi_msg_id_t id
Definition: vapi_internal.h:98
operations block until response received
Definition: vapi_common.h:44
void vl_client_disconnect(void)
operations never block
Definition: vapi_common.h:45
size_t vapi_get_message_count()
Definition: vapi.c:916
void * callback_ctx
Definition: vapi.c:49
unix_shared_memory_queue_t * vl_input_queue
Definition: api_common.h:68
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:286
u32 context
Definition: vapi.c:47
#define NULL
Definition: clib.h:55
u16 vl_msg_id_max
Definition: vapi.c:79
operation would block
Definition: vapi_common.h:29
vapi_error_e(* cb)(vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *payload)
Definition: vapi.c:57
vapi_error_e vapi_send(vapi_ctx_t ctx, void *msg)
low-level api for sending messages to vpp
Definition: vapi.c:417
vapi_mode_e
Definition: vapi_common.h:42
int requests_start
Definition: vapi.c:72
bool connected
Definition: vapi.c:81
int requests_size
Definition: vapi.c:71
vapi_cb_t callback
Definition: vapi.c:48
void * vapi_msg_alloc(vapi_ctx_t ctx, size_t size)
allocate vapi message of given size
Definition: vapi.c:209
void vapi_store_request(vapi_ctx_t ctx, u32 context, bool is_dump, vapi_cb_t callback, void *callback_ctx)
Definition: vapi.c:118
vapi_msg_id_t * vl_msg_id_to_vapi_msg_t
Definition: vapi.c:80
#define vl_msg_id(n, h)
out of memory
Definition: vapi_common.h:31
unix_shared_memory_queue_t * vl_input_queue
Peer input queue pointer.
Definition: api_common.h:280
int our_pid
Current process PID.
Definition: api_common.h:232
vapi_req_t * requests
Definition: vapi.c:74
vapi_msg_id_t vapi_register_msg(vapi_message_desc_t *msg)
Definition: vapi.c:858
vapi_error_e(* vapi_generic_event_cb)(vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
generic vapi event callback
Definition: vapi.h:227
vapi_error_e vapi_send2(vapi_ctx_t ctx, void *msg1, void *msg2)
low-level api for atomically sending two messages to vpp - either both messages are sent or neither o...
Definition: vapi.c:460
size_t vapi_get_message_size(vapi_msg_id_t id)
Definition: vapi.c:844
int vl_client_connect(const char *name, int ctx_quota, int input_queue_size)
fundamental incompatibility while connecting to vpp (control ping/control ping reply mismatch) ...
Definition: vapi_common.h:35
vapi_error_e vapi_recv(vapi_ctx_t ctx, void **msg, size_t *msg_size)
low-level api for reading messages from vpp
Definition: vapi.c:505
size_t vapi_get_request_count(vapi_ctx_t ctx)
Definition: vapi.c:94
operation not supported
Definition: vapi_common.h:30
void vl_client_api_unmap(void)
vapi_error_e vapi_disconnect(vapi_ctx_t ctx)
disconnect from vpp
Definition: vapi.c:395
static vapi_error_e vapi_dispatch_event(vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
Definition: vapi.c:671
int unix_shared_memory_queue_add2(unix_shared_memory_queue_t *q, u8 *elem, u8 *elem2, int nowait)
bool vapi_is_nonblocking(vapi_ctx_t ctx)
Definition: vapi.c:813
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:241
vapi_message_desc_t ** msgs
Definition: vapi.c:41
int vapi_get_payload_offset(vapi_msg_id_t id)
Definition: vapi.c:825
u32 vl_api_get_msg_index(u8 *name_and_crc)
size_t vapi_get_context_offset(vapi_msg_id_t id)
Definition: vapi.c:851
vapi_event_cb_with_ctx * event_cbs
Definition: vapi.c:77
size_t vapi_get_max_request_count(vapi_ctx_t ctx)
Definition: vapi.c:819
int unix_shared_memory_queue_add(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
vapi_msg_id_t vapi_msg_id_control_ping_reply
Definition: vapi.c:36
const char * vapi_get_msg_name(vapi_msg_id_t id)
Definition: vapi.c:922
int requests_count
Definition: vapi.c:73
const char * name_with_crc
Definition: vapi_internal.h:90
u32 vapi_gen_req_context(vapi_ctx_t ctx)
Definition: vapi.c:86
void vapi_set_generic_event_cb(vapi_ctx_t ctx, vapi_generic_event_cb callback, void *callback_ctx)
set generic event callback
Definition: vapi.c:785
bool vapi_requests_empty(vapi_ctx_t ctx)
Definition: vapi.c:106
#define v
Definition: acl.c:323
void(*)(void *msg) vapi_get_swap_to_host_func(vapi_msg_id_t id)
Definition: vapi.c:831
void(*)(void *msg) vapi_get_swap_to_be_func(vapi_msg_id_t id)
Definition: vapi.c:837
void vl_msg_api_free(void *)
invalid value encountered
Definition: vapi_common.h:28
common vpp api C declarations
size_t max_len_name_with_crc
Definition: vapi.c:42
#define VAPI_DBG(...)
Definition: vapi_dbg.h:65
vapi_error_e vapi_dispatch(vapi_ctx_t ctx)
loop vapi_dispatch_one until responses to all currently outstanding requests have been received and t...
Definition: vapi.c:755
no response to request
Definition: vapi_common.h:32
bool is_dump
Definition: vapi.c:50
failure while mapping api
Definition: vapi_common.h:33
vapi_error_e(* vapi_cb_t)(struct vapi_ctx_s *, void *, vapi_error_e, bool, void *)
Definition: vapi_internal.h:81
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:182
bool vapi_msg_is_with_context(vapi_msg_id_t id)
Definition: vapi.c:691
int unix_shared_memory_queue_sub(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
static vapi_error_e vapi_dispatch_response(vapi_ctx_t ctx, vapi_msg_id_t id, u32 context, void *msg)
Definition: vapi.c:572
api_main_t api_main
Definition: api_shared.c:35
vapi_msg_id_t vapi_lookup_vapi_msg_id_t(vapi_ctx_t ctx, u16 vl_msg_id)
Definition: vapi.c:233
svmdb_client_t * c
struct vapi_ctx_s * vapi_ctx_t
Definition: vapi.h:45
static const u32 context_counter_mask
Definition: vapi.c:53
vapi_wait_mode_e
Definition: vapi_common.h:48
pthread_mutex_t requests_mutex
Definition: vapi.c:82
internal vpp api C declarations
vapi_msg_id_t vapi_msg_id_control_ping
Definition: vapi.c:35
u16 vapi_lookup_vl_msg_id(vapi_ctx_t ctx, vapi_msg_id_t id)
Definition: vapi.c:800
bool vapi_is_msg_available(vapi_ctx_t ctx, vapi_msg_id_t id)
check if message identified by it&#39;s message id is known by the vpp to which the connection is open ...
Definition: vapi.c:284
success
Definition: vapi_common.h:27
unsigned int u32
Definition: types.h:88
long ctx[MAX_CONNS]
Definition: main.c:95
u32 data_len
message length not including header
Definition: api_common.h:129
Message header structure.
Definition: api_common.h:126
u64 size
Definition: vhost-user.h:76
#define bool
Definition: radix.c:95
size_t count
Definition: vapi.c:40
int vapi_get_client_index(vapi_ctx_t ctx)
Definition: vapi.c:807
u64 uword
Definition: types.h:112
void vapi_ctx_free(vapi_ctx_t ctx)
free vapi context
Definition: vapi.c:272
u32 context_counter
Definition: vapi.c:75
unsigned short u16
Definition: types.h:57
void vapi_clear_generic_event_cb(vapi_ctx_t ctx)
clear generic event callback
Definition: vapi.c:793
vapi_error_e(* vapi_event_cb)(vapi_ctx_t ctx, void *callback_ctx, void *payload)
generic vapi event callback
Definition: vapi.h:204
vapi_error_e vapi_dispatch_one(vapi_ctx_t ctx)
pick next message sent by vpp and call the appropriate callback
Definition: vapi.c:698
unsigned char u8
Definition: types.h:56
vapi_error_e vapi_producer_unlock(vapi_ctx_t ctx)
Definition: vapi.c:902
bool vapi_requests_full(vapi_ctx_t ctx)
Definition: vapi.c:100
vapi_error_e vapi_get_fd(vapi_ctx_t ctx, int *fd)
get event file descriptor
Definition: vapi.c:411
vapi_error_e(* cb)(vapi_ctx_t ctx, void *callback_ctx, void *payload)
Definition: vapi.c:64
void vapi_msg_free(vapi_ctx_t ctx, void *msg)
free a vapi message
Definition: vapi.c:220
vapi_error_e vapi_producer_lock(vapi_ctx_t ctx)
Definition: vapi.c:889
u16 * vapi_msg_id_t_to_vl_msg_id
Definition: vapi.c:78
int vl_client_api_map(const char *region_name)
vapi_error_e vapi_connect(vapi_ctx_t ctx, const char *name, const char *chroot_prefix, int max_outstanding_requests, int response_queue_size, vapi_mode_e mode)
connect to vpp
Definition: vapi.c:290
void vl_set_memory_root_path(const char *root_path)
vapi_mode_e mode
Definition: vapi.c:70
vapi_error_e vapi_ctx_alloc(vapi_ctx_t *result)
allocate vapi context
Definition: vapi.c:243
int vapi_msg_id_t
Definition: vapi_common.h:55
void vapi_set_event_cb(vapi_ctx_t ctx, vapi_msg_id_t id, vapi_event_cb callback, void *callback_ctx)
set event callback to call when message with given id is dispatched
Definition: vapi.c:770
void * vl_msg_api_alloc_or_null(int nbytes)
void vapi_clear_event_cb(vapi_ctx_t ctx, vapi_msg_id_t id)
clear event callback for given message id
Definition: vapi.c:779
vapi_generic_cb_with_ctx generic_cb
Definition: vapi.c:76
static int vapi_requests_end(vapi_ctx_t ctx)
Definition: vapi.c:112
vapi_error_e
Definition: vapi_common.h:25
struct _unix_shared_memory_queue unix_shared_memory_queue_t
#define VAPI_ERR(...)
Definition: vapi_dbg.h:66
vapi_error_e vapi_wait(vapi_ctx_t ctx, vapi_wait_mode_e mode)
wait for connection to become readable or writable
Definition: vapi.c:566
failure while connecting to vpp
Definition: vapi_common.h:34