FD.io VPP  v18.07-rc0-415-g6c78436
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 <vppinfra/pool.h>
30 #include <vlib/vlib.h>
31 #include <vlibapi/api_common.h>
33 
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 */
39 
40 struct
41 {
42  size_t count;
45 } __vapi_metadata;
46 
47 typedef struct
48 {
51  void *callback_ctx;
52  bool is_dump;
53 } vapi_req_t;
54 
55 static const u32 context_counter_mask = (1 << 31);
56 
57 typedef struct
58 {
59  vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id,
60  void *payload);
61  void *ctx;
63 
64 typedef struct
65 {
66  vapi_error_e (*cb) (vapi_ctx_t ctx, void *callback_ctx, void *payload);
67  void *ctx;
69 
70 struct vapi_ctx_s
71 {
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 */
83  bool connected;
84  pthread_mutex_t requests_mutex;
85 };
86 
87 u32
89 {
90  ++ctx->context_counter;
93 }
94 
95 size_t
97 {
98  return ctx->requests_count;
99 }
100 
101 bool
103 {
104  return (ctx->requests_count == ctx->requests_size);
105 }
106 
107 bool
109 {
110  return (0 == ctx->requests_count);
111 }
112 
113 static int
115 {
116  return (ctx->requests_start + ctx->requests_count) % ctx->requests_size;
117 }
118 
119 void
120 vapi_store_request (vapi_ctx_t ctx, u32 context, bool is_dump,
121  vapi_cb_t callback, void *callback_ctx)
122 {
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));
136 }
137 
138 #if VAPI_DEBUG_ALLOC
139 struct to_be_freed_s;
140 struct to_be_freed_s
141 {
142  void *v;
143  struct to_be_freed_s *next;
144 };
145 
146 static struct to_be_freed_s *to_be_freed = NULL;
147 
148 void
149 vapi_add_to_be_freed (void *v)
150 {
151  struct to_be_freed_s *prev = NULL;
152  struct to_be_freed_s *tmp;
153  tmp = to_be_freed;
154  while (tmp && tmp->v)
155  {
156  prev = tmp;
157  tmp = tmp->next;
158  }
159  if (!tmp)
160  {
161  if (!prev)
162  {
163  tmp = to_be_freed = calloc (1, sizeof (*to_be_freed));
164  }
165  else
166  {
167  tmp = prev->next = calloc (1, sizeof (*to_be_freed));
168  }
169  }
170  VAPI_DBG ("To be freed %p", v);
171  tmp->v = v;
172 }
173 
174 void
175 vapi_trace_free (void *v)
176 {
177  struct to_be_freed_s *tmp = to_be_freed;
178  while (tmp && tmp->v != v)
179  {
180  tmp = tmp->next;
181  }
182  if (tmp && tmp->v == v)
183  {
184  VAPI_DBG ("Freed %p", v);
185  tmp->v = NULL;
186  }
187  else
188  {
189  VAPI_ERR ("Trying to free untracked pointer %p", v);
190  abort ();
191  }
192 }
193 
194 void
195 vapi_to_be_freed_validate ()
196 {
197  struct to_be_freed_s *tmp = to_be_freed;
198  while (tmp)
199  {
200  if (tmp->v)
201  {
202  VAPI_ERR ("Unfreed msg %p!", tmp->v);
203  }
204  tmp = tmp->next;
205  }
206 }
207 
208 #endif
209 
210 void *
212 {
213  if (!ctx->connected)
214  {
215  return NULL;
216  }
217  void *rv = vl_msg_api_alloc_or_null (size);
218  return rv;
219 }
220 
221 void
223 {
224  if (!ctx->connected)
225  {
226  return;
227  }
228 #if VAPI_DEBUG_ALLOC
229  vapi_trace_free (msg);
230 #endif
231  vl_msg_api_free (msg);
232 }
233 
236 {
237  if (vl_msg_id <= ctx->vl_msg_id_max)
238  {
239  return ctx->vl_msg_id_to_vapi_msg_t[vl_msg_id];
240  }
241  return INVALID_MSG_ID;
242 }
243 
246 {
247  vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
248  if (!ctx)
249  {
250  return VAPI_ENOMEM;
251  }
252  ctx->context_counter = 0;
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)
257  {
258  goto fail;
259  }
260  ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
261  if (!ctx->event_cbs)
262  {
263  goto fail;
264  }
265  pthread_mutex_init (&ctx->requests_mutex, NULL);
266  *result = ctx;
267  return VAPI_OK;
268 fail:
269  vapi_ctx_free (ctx);
270  return VAPI_ENOMEM;
271 }
272 
273 void
275 {
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);
282  free (ctx);
283 }
284 
285 bool
287 {
288  return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
289 }
290 
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)
296 {
297  if (response_queue_size <= 0 || max_outstanding_requests <= 0)
298  {
299  return VAPI_EINVAL;
300  }
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);
304  if (!tmp)
305  {
306  return VAPI_ENOMEM;
307  }
308  ctx->requests = tmp;
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;
312  if (chroot_prefix)
313  {
314  VAPI_DBG ("set memory root path `%s'", chroot_prefix);
315  vl_set_memory_root_path ((char *) chroot_prefix);
316  }
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)
320  {
321  return VAPI_EMAP_FAIL;
322  }
323  VAPI_DBG ("connect client `%s'", name);
324  if (vl_client_connect ((char *) name, 0, response_queue_size) < 0)
325  {
327  return VAPI_ECON_FAIL;
328  }
329 #if VAPI_DEBUG_CONNECT
330  VAPI_DBG ("start probing messages");
331 #endif
332  int rv;
333  int i;
334  for (i = 0; i < __vapi_metadata.count; ++i)
335  {
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_msg_api_get_msg_index (scratch);
340  if (INVALID_MSG_ID != id)
341  {
342  if (id > UINT16_MAX)
343  {
344  VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
345  UINT16_MAX);
346  rv = VAPI_EINVAL;
347  goto fail;
348  }
349  if (id > ctx->vl_msg_id_max)
350  {
351  vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t,
352  sizeof
353  (*ctx->vl_msg_id_to_vapi_msg_t) *
354  (id + 1));
355  if (!tmp)
356  {
357  rv = VAPI_ENOMEM;
358  goto fail;
359  }
360  ctx->vl_msg_id_to_vapi_msg_t = tmp;
361  ctx->vl_msg_id_max = id;
362  }
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,
367  (unsigned) id);
368 #endif
369  }
370  else
371  {
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);
374  }
375  }
376 #if VAPI_DEBUG_CONNECT
377  VAPI_DBG ("finished probing messages");
378 #endif
379  if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) ||
380  !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply))
381  {
382  VAPI_ERR
383  ("control ping or control ping reply not available, cannot connect");
384  rv = VAPI_EINCOMPATIBLE;
385  goto fail;
386  }
387  ctx->mode = mode;
388  ctx->connected = true;
389  return VAPI_OK;
390 fail:
393  return rv;
394 }
395 
398 {
399  if (!ctx->connected)
400  {
401  return VAPI_EINVAL;
402  }
405 #if VAPI_DEBUG_ALLOC
406  vapi_to_be_freed_validate ();
407 #endif
408  ctx->connected = false;
409  return VAPI_OK;
410 }
411 
414 {
415  return VAPI_ENOTSUP;
416 }
417 
420 {
421  vapi_error_e rv = VAPI_OK;
422  if (!ctx || !msg || !ctx->connected)
423  {
424  rv = VAPI_EINVAL;
425  goto out;
426  }
427  int tmp;
429 #if VAPI_DEBUG
430  unsigned msgid = be16toh (*(u16 *) msg);
431  if (msgid <= ctx->vl_msg_id_max)
432  {
433  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
434  if (id < __vapi_metadata.count)
435  {
436  VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
437  __vapi_metadata.msgs[id]->name);
438  }
439  else
440  {
441  VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
442  }
443  }
444  else
445  {
446  VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
447  }
448 #endif
449  tmp = svm_queue_add (q, (u8 *) & msg,
450  VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
451  if (tmp < 0)
452  {
453  rv = VAPI_EAGAIN;
454  }
455 out:
456  VAPI_DBG ("vapi_send() rv = %d", rv);
457  return rv;
458 }
459 
461 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
462 {
463  vapi_error_e rv = VAPI_OK;
464  if (!ctx || !msg1 || !msg2 || !ctx->connected)
465  {
466  rv = VAPI_EINVAL;
467  goto out;
468  }
470 #if VAPI_DEBUG
471  unsigned msgid1 = be16toh (*(u16 *) msg1);
472  unsigned msgid2 = be16toh (*(u16 *) msg2);
473  const char *name1 = "UNKNOWN";
474  const char *name2 = "UNKNOWN";
475  if (msgid1 <= ctx->vl_msg_id_max)
476  {
477  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
478  if (id < __vapi_metadata.count)
479  {
480  name1 = __vapi_metadata.msgs[id]->name;
481  }
482  }
483  if (msgid2 <= ctx->vl_msg_id_max)
484  {
485  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
486  if (id < __vapi_metadata.count)
487  {
488  name2 = __vapi_metadata.msgs[id]->name;
489  }
490  }
491  VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
492 #endif
493  int tmp = svm_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
494  VAPI_MODE_BLOCKING == 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  svm_q_conditional_wait_t cond, u32 time)
507 {
508  if (!ctx || !ctx->connected || !msg || !msg_size)
509  {
510  return VAPI_EINVAL;
511  }
512  vapi_error_e rv = VAPI_OK;
513  api_main_t *am = &api_main;
514  uword data;
515 
516  if (am->our_pid == 0)
517  {
518  return VAPI_EINVAL;
519  }
520 
521  svm_queue_t *q = am->vl_input_queue;
522  VAPI_DBG ("doing shm queue sub");
523 
524  int tmp = svm_queue_sub (q, (u8 *) & data, cond, time);
525 
526  if (tmp == 0)
527  {
528 #if VAPI_DEBUG_ALLOC
529  vapi_add_to_be_freed ((void *) data);
530 #endif
531  msgbuf_t *msgbuf =
532  (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
533  if (!msgbuf->data_len)
534  {
535  vapi_msg_free (ctx, (u8 *) data);
536  return VAPI_EAGAIN;
537  }
538  *msg = (u8 *) data;
539  *msg_size = ntohl (msgbuf->data_len);
540 #if VAPI_DEBUG
541  unsigned msgid = be16toh (*(u16 *) * msg);
542  if (msgid <= ctx->vl_msg_id_max)
543  {
544  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
545  if (id < __vapi_metadata.count)
546  {
547  VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
548  __vapi_metadata.msgs[id]->name);
549  }
550  else
551  {
552  VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
553  }
554  }
555  else
556  {
557  VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
558  }
559 #endif
560  }
561  else
562  {
563  rv = VAPI_EAGAIN;
564  }
565  return rv;
566 }
567 
570 {
571  return VAPI_ENOTSUP;
572 }
573 
574 static vapi_error_e
576  u32 context, void *msg)
577 {
578  int mrv;
579  if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
580  {
581  VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
582  return VAPI_MUTEX_FAILURE;
583  }
584  int tmp = ctx->requests_start;
585  const int requests_end = vapi_requests_end (ctx);
586  while (ctx->requests[tmp].context != context && tmp != requests_end)
587  {
588  ++tmp;
589  if (tmp == ctx->requests_size)
590  {
591  tmp = 0;
592  }
593  }
594  VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
595  ctx->requests[tmp].context == context ? "matched" : "stopped",
596  tmp);
597  vapi_error_e rv = VAPI_OK;
598  if (ctx->requests[tmp].context == context)
599  {
600  while (ctx->requests_start != tmp)
601  {
602  VAPI_ERR ("No response to req with context=%u",
603  (unsigned) ctx->requests[tmp].context);
604  ctx->requests[ctx->requests_start].callback (ctx,
605  ctx->requests
606  [ctx->
608  VAPI_ENORESP, true,
609  NULL);
610  memset (&ctx->requests[ctx->requests_start], 0,
611  sizeof (ctx->requests[ctx->requests_start]));
612  ++ctx->requests_start;
613  --ctx->requests_count;
614  if (ctx->requests_start == ctx->requests_size)
615  {
616  ctx->requests_start = 0;
617  }
618  }
619  // now ctx->requests_start == tmp
620  int payload_offset = vapi_get_payload_offset (id);
621  void *payload = ((u8 *) msg) + payload_offset;
622  bool is_last = true;
623  if (ctx->requests[tmp].is_dump)
624  {
625  if (vapi_msg_id_control_ping_reply == id)
626  {
627  payload = NULL;
628  }
629  else
630  {
631  is_last = false;
632  }
633  }
634  if (payload_offset != -1)
635  {
636  rv =
637  ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx,
638  VAPI_OK, is_last, payload);
639  }
640  else
641  {
642  /* this is a message without payload, so bend the callback a little
643  */
644  rv =
645  ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
646  ctx->requests[tmp].callback) (ctx,
647  ctx->requests[tmp].callback_ctx,
648  VAPI_OK, is_last);
649  }
650  if (is_last)
651  {
652  memset (&ctx->requests[ctx->requests_start], 0,
653  sizeof (ctx->requests[ctx->requests_start]));
654  ++ctx->requests_start;
655  --ctx->requests_count;
656  if (ctx->requests_start == ctx->requests_size)
657  {
658  ctx->requests_start = 0;
659  }
660  }
661  VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
662  ctx->requests_start, requests_end, ctx->requests_count);
663  }
664  if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
665  {
666  VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
667  strerror (mrv));
668  abort (); /* this really shouldn't happen */
669  }
670  return rv;
671 }
672 
673 static vapi_error_e
675 {
676  if (ctx->event_cbs[id].cb)
677  {
678  return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
679  }
680  else if (ctx->generic_cb.cb)
681  {
682  return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
683  }
684  else
685  {
686  VAPI_DBG
687  ("No handler/generic handler for msg id %u[%s], message ignored",
688  (unsigned) id, __vapi_metadata.msgs[id]->name);
689  }
690  return VAPI_OK;
691 }
692 
693 bool
695 {
696  assert (id <= __vapi_metadata.count);
697  return __vapi_metadata.msgs[id]->has_context;
698 }
699 
702 {
703  VAPI_DBG ("vapi_dispatch_one()");
704  void *msg;
705  size_t size;
706  vapi_error_e rv = vapi_recv (ctx, &msg, &size, SVM_Q_WAIT, 0);
707  if (VAPI_OK != rv)
708  {
709  VAPI_DBG ("vapi_recv failed with rv=%d", rv);
710  return rv;
711  }
712  u16 vpp_id = be16toh (*(u16 *) msg);
713  if (vpp_id > ctx->vl_msg_id_max)
714  {
715  VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
716  (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
717  vapi_msg_free (ctx, msg);
718  return VAPI_EINVAL;
719  }
720  if (INVALID_MSG_ID == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
721  {
722  VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
723  (unsigned) vpp_id);
724  vapi_msg_free (ctx, msg);
725  return VAPI_EINVAL;
726  }
727  const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
728  const size_t expect_size = vapi_get_message_size (id);
729  if (size < expect_size)
730  {
731  VAPI_ERR
732  ("Invalid msg received, unexpected size `%zu' < expected min `%zu'",
733  size, expect_size);
734  vapi_msg_free (ctx, msg);
735  return VAPI_EINVAL;
736  }
737  u32 context;
738  vapi_get_swap_to_host_func (id) (msg);
739  if (vapi_msg_is_with_context (id))
740  {
741  context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
742  /* is this a message originating from VAPI? */
743  VAPI_DBG ("dispatch, context is %x", context);
744  if (context & context_counter_mask)
745  {
746  rv = vapi_dispatch_response (ctx, id, context, msg);
747  goto done;
748  }
749  }
750  rv = vapi_dispatch_event (ctx, id, msg);
751 
752 done:
753  vapi_msg_free (ctx, msg);
754  return rv;
755 }
756 
759 {
760  vapi_error_e rv = VAPI_OK;
761  while (!vapi_requests_empty (ctx))
762  {
763  rv = vapi_dispatch_one (ctx);
764  if (VAPI_OK != rv)
765  {
766  return rv;
767  }
768  }
769  return rv;
770 }
771 
772 void
774  vapi_event_cb callback, void *callback_ctx)
775 {
776  vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
777  c->cb = callback;
778  c->ctx = callback_ctx;
779 }
780 
781 void
783 {
784  vapi_set_event_cb (ctx, id, NULL, NULL);
785 }
786 
787 void
789  void *callback_ctx)
790 {
791  ctx->generic_cb.cb = callback;
792  ctx->generic_cb.ctx = callback_ctx;
793 }
794 
795 void
797 {
798  ctx->generic_cb.cb = NULL;
799  ctx->generic_cb.ctx = NULL;
800 }
801 
802 u16
804 {
805  assert (id < __vapi_metadata.count);
806  return ctx->vapi_msg_id_t_to_vl_msg_id[id];
807 }
808 
809 int
811 {
812  return api_main.my_client_index;
813 }
814 
815 bool
817 {
818  return (VAPI_MODE_NONBLOCKING == ctx->mode);
819 }
820 
821 size_t
823 {
824  return ctx->requests_size - 1;
825 }
826 
827 int
829 {
830  assert (id < __vapi_metadata.count);
831  return __vapi_metadata.msgs[id]->payload_offset;
832 }
833 
835 {
836  assert (id < __vapi_metadata.count);
837  return __vapi_metadata.msgs[id]->swap_to_host;
838 }
839 
840 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
841 {
842  assert (id < __vapi_metadata.count);
843  return __vapi_metadata.msgs[id]->swap_to_be;
844 }
845 
846 size_t
848 {
849  assert (id < __vapi_metadata.count);
850  return __vapi_metadata.msgs[id]->size;
851 }
852 
853 size_t
855 {
856  assert (id < __vapi_metadata.count);
857  return __vapi_metadata.msgs[id]->context_offset;
858 }
859 
862 {
863  int i = 0;
864  for (i = 0; i < __vapi_metadata.count; ++i)
865  {
866  if (!strcmp
867  (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
868  {
869  /* this happens if somebody is linking together several objects while
870  * using the static inline headers, just fill in the already
871  * assigned id here so that all the objects are in sync */
872  msg->id = __vapi_metadata.msgs[i]->id;
873  return msg->id;
874  }
875  }
876  vapi_msg_id_t id = __vapi_metadata.count;
877  ++__vapi_metadata.count;
878  __vapi_metadata.msgs =
879  realloc (__vapi_metadata.msgs,
880  sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
881  __vapi_metadata.msgs[id] = msg;
882  size_t s = strlen (msg->name_with_crc);
883  if (s > __vapi_metadata.max_len_name_with_crc)
884  {
885  __vapi_metadata.max_len_name_with_crc = s;
886  }
887  msg->id = id;
888  return id;
889 }
890 
893 {
894  int mrv;
895  if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
896  {
897  VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
898  (void) mrv; /* avoid warning if the above debug is not enabled */
899  return VAPI_MUTEX_FAILURE;
900  }
901  return VAPI_OK;
902 }
903 
906 {
907  int mrv;
908  if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
909  {
910  VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
911  strerror (mrv));
912  (void) mrv; /* avoid warning if the above debug is not enabled */
913  return VAPI_MUTEX_FAILURE;
914  }
915  return VAPI_OK;
916 }
917 
918 size_t
920 {
921  return __vapi_metadata.count;
922 }
923 
924 const char *
926 {
927  return __vapi_metadata.msgs[id]->name;
928 }
929 
930 /*
931  * fd.io coding-style-patch-verification: ON
932  *
933  * Local Variables:
934  * eval: (c-set-style "gnu")
935  * End:
936  */
failure manipulating internal mutex(es)
Definition: vapi_common.h:37
vapi_msg_id_t id
Definition: vapi_internal.h:98
operations block until response received
Definition: vapi_common.h:44
int svm_queue_add(svm_queue_t *q, u8 *elem, int nowait)
Definition: queue.c:184
operations never block
Definition: vapi_common.h:45
size_t vapi_get_message_count()
Definition: vapi.c:919
void * callback_ctx
Definition: vapi.c:51
Fixed length block allocator.
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:309
u32 context
Definition: vapi.c:49
#define NULL
Definition: clib.h:55
u16 vl_msg_id_max
Definition: vapi.c:81
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:59
vapi_error_e vapi_recv(vapi_ctx_t ctx, void **msg, size_t *msg_size, svm_q_conditional_wait_t cond, u32 time)
low-level api for reading messages from vpp
Definition: vapi.c:505
int i
vapi_error_e vapi_send(vapi_ctx_t ctx, void *msg)
low-level api for sending messages to vpp
Definition: vapi.c:419
vapi_mode_e
Definition: vapi_common.h:42
int requests_start
Definition: vapi.c:74
bool connected
Definition: vapi.c:83
int requests_size
Definition: vapi.c:73
vapi_cb_t callback
Definition: vapi.c:50
svm_queue_t * vl_input_queue
Peer input queue pointer.
Definition: api_common.h:303
void * vapi_msg_alloc(vapi_ctx_t ctx, size_t size)
allocate vapi message of given size
Definition: vapi.c:211
void vapi_store_request(vapi_ctx_t ctx, u32 context, bool is_dump, vapi_cb_t callback, void *callback_ctx)
Definition: vapi.c:120
vapi_msg_id_t * vl_msg_id_to_vapi_msg_t
Definition: vapi.c:82
#define vl_msg_id(n, h)
unsigned char u8
Definition: types.h:56
out of memory
Definition: vapi_common.h:31
void vl_client_api_unmap(void)
int our_pid
Current process PID.
Definition: api_common.h:251
vapi_req_t * requests
Definition: vapi.c:76
vapi_msg_id_t vapi_register_msg(vapi_message_desc_t *msg)
Definition: vapi.c:861
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:231
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:461
size_t vapi_get_message_size(vapi_msg_id_t id)
Definition: vapi.c:847
int svm_queue_sub(svm_queue_t *q, u8 *elem, svm_q_conditional_wait_t cond, u32 time)
Definition: queue.c:303
fundamental incompatibility while connecting to vpp (control ping/control ping reply mismatch) ...
Definition: vapi_common.h:35
size_t vapi_get_request_count(vapi_ctx_t ctx)
Definition: vapi.c:96
operation not supported
Definition: vapi_common.h:30
vapi_error_e vapi_disconnect(vapi_ctx_t ctx)
disconnect from vpp
Definition: vapi.c:397
static vapi_error_e vapi_dispatch_event(vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
Definition: vapi.c:674
bool vapi_is_nonblocking(vapi_ctx_t ctx)
Definition: vapi.c:816
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:264
vapi_message_desc_t ** msgs
Definition: vapi.c:43
unsigned int u32
Definition: types.h:88
int vapi_get_payload_offset(vapi_msg_id_t id)
Definition: vapi.c:828
size_t vapi_get_context_offset(vapi_msg_id_t id)
Definition: vapi.c:854
vapi_event_cb_with_ctx * event_cbs
Definition: vapi.c:79
int svm_queue_add2(svm_queue_t *q, u8 *elem, u8 *elem2, int nowait)
Definition: queue.c:239
size_t vapi_get_max_request_count(vapi_ctx_t ctx)
Definition: vapi.c:822
void vl_set_memory_root_path(const char *name)
vapi_msg_id_t vapi_msg_id_control_ping_reply
Definition: vapi.c:38
int vl_client_connect(const char *name, int ctx_quota, int input_queue_size)
const char * vapi_get_msg_name(vapi_msg_id_t id)
Definition: vapi.c:925
uword size
int requests_count
Definition: vapi.c:75
const char * name_with_crc
Definition: vapi_internal.h:90
u32 vapi_gen_req_context(vapi_ctx_t ctx)
Definition: vapi.c:88
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:788
bool vapi_requests_empty(vapi_ctx_t ctx)
Definition: vapi.c:108
#define v
Definition: acl.c:491
unsigned short u16
Definition: types.h:57
void(*)(void *msg) vapi_get_swap_to_host_func(vapi_msg_id_t id)
Definition: vapi.c:834
void(*)(void *msg) vapi_get_swap_to_be_func(vapi_msg_id_t id)
Definition: vapi.c:840
invalid value encountered
Definition: vapi_common.h:28
common vpp api C declarations
size_t max_len_name_with_crc
Definition: vapi.c:44
#define INVALID_MSG_ID
Definition: vapi_common.h:57
#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:758
no response to request
Definition: vapi_common.h:32
bool is_dump
Definition: vapi.c:52
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:201
bool vapi_msg_is_with_context(vapi_msg_id_t id)
Definition: vapi.c:694
static vapi_error_e vapi_dispatch_response(vapi_ctx_t ctx, vapi_msg_id_t id, u32 context, void *msg)
Definition: vapi.c:575
vapi_msg_id_t vapi_lookup_vapi_msg_id_t(vapi_ctx_t ctx, u16 vl_msg_id)
Definition: vapi.c:235
svmdb_client_t * c
svm_q_conditional_wait_t
Definition: queue.h:39
u32 vl_msg_api_get_msg_index(u8 *name_and_crc)
Definition: api_shared.c:944
struct vapi_ctx_s * vapi_ctx_t
Definition: vapi.h:46
int vl_client_api_map(const char *region_name)
static const u32 context_counter_mask
Definition: vapi.c:55
blocking call
Definition: queue.h:44
vapi_wait_mode_e
Definition: vapi_common.h:48
pthread_mutex_t requests_mutex
Definition: vapi.c:84
internal vpp api C declarations
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
vapi_msg_id_t vapi_msg_id_control_ping
Definition: vapi.c:37
u16 vapi_lookup_vl_msg_id(vapi_ctx_t ctx, vapi_msg_id_t id)
Definition: vapi.c:803
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:286
success
Definition: vapi_common.h:27
long ctx[MAX_CONNS]
Definition: main.c:126
u32 data_len
message length not including header
Definition: api_common.h:138
Message header structure.
Definition: api_common.h:135
#define bool
Definition: radix.c:95
size_t count
Definition: vapi.c:42
int vapi_get_client_index(vapi_ctx_t ctx)
Definition: vapi.c:810
void vl_msg_api_free(void *)
void vapi_ctx_free(vapi_ctx_t ctx)
free vapi context
Definition: vapi.c:274
u32 context_counter
Definition: vapi.c:77
void vapi_clear_generic_event_cb(vapi_ctx_t ctx)
clear generic event callback
Definition: vapi.c:796
vapi_error_e(* vapi_event_cb)(vapi_ctx_t ctx, void *callback_ctx, void *payload)
generic vapi event callback
Definition: vapi.h:208
vapi_error_e vapi_dispatch_one(vapi_ctx_t ctx)
pick next message sent by vpp and call the appropriate callback
Definition: vapi.c:701
vapi_error_e vapi_producer_unlock(vapi_ctx_t ctx)
Definition: vapi.c:905
int vl_client_disconnect(void)
u64 uword
Definition: types.h:112
bool vapi_requests_full(vapi_ctx_t ctx)
Definition: vapi.c:102
vapi_error_e vapi_get_fd(vapi_ctx_t ctx, int *fd)
get event file descriptor
Definition: vapi.c:413
struct _svm_queue svm_queue_t
vapi_error_e(* cb)(vapi_ctx_t ctx, void *callback_ctx, void *payload)
Definition: vapi.c:66
void vapi_msg_free(vapi_ctx_t ctx, void *msg)
free a vapi message
Definition: vapi.c:222
vapi_error_e vapi_producer_lock(vapi_ctx_t ctx)
Definition: vapi.c:892
u16 * vapi_msg_id_t_to_vl_msg_id
Definition: vapi.c:80
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:292
vapi_mode_e mode
Definition: vapi.c:72
vapi_error_e vapi_ctx_alloc(vapi_ctx_t *result)
allocate vapi context
Definition: vapi.c:245
void * vl_msg_api_alloc_or_null(int nbytes)
unsigned 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:773
api_main_t api_main
Definition: api_shared.c:35
void vapi_clear_event_cb(vapi_ctx_t ctx, vapi_msg_id_t id)
clear event callback for given message id
Definition: vapi.c:782
vapi_generic_cb_with_ctx generic_cb
Definition: vapi.c:78
static int vapi_requests_end(vapi_ctx_t ctx)
Definition: vapi.c:114
vapi_error_e
Definition: vapi_common.h:25
#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:569
failure while connecting to vpp
Definition: vapi_common.h:34