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