FD.io VPP  v21.06-3-gbb25fbf28
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;
97  ctx->context_counter %= context_counter_mask;
98  return ctx->context_counter | context_counter_mask;
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 {
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;
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  if (rv)
225  {
226  clib_memset (rv, 0, size);
227  }
228  return rv;
229 }
230 
231 void
233 {
234  if (!ctx->connected)
235  {
236  return;
237  }
238 #if VAPI_DEBUG_ALLOC
239  vapi_trace_free (msg);
240 #endif
241  vl_msg_api_free (msg);
242 }
243 
246 {
247  if (vl_msg_id <= ctx->vl_msg_id_max)
248  {
249  return ctx->vl_msg_id_to_vapi_msg_t[vl_msg_id];
250  }
251  return VAPI_INVALID_MSG_ID;
252 }
253 
256 {
257  vapi_ctx_t ctx = calloc (1, sizeof (struct vapi_ctx_s));
258  if (!ctx)
259  {
260  return VAPI_ENOMEM;
261  }
262  ctx->context_counter = 0;
263  ctx->vapi_msg_id_t_to_vl_msg_id =
264  malloc (__vapi_metadata.count *
265  sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
266  if (!ctx->vapi_msg_id_t_to_vl_msg_id)
267  {
268  goto fail;
269  }
270  clib_memset (ctx->vapi_msg_id_t_to_vl_msg_id, ~0,
271  __vapi_metadata.count *
272  sizeof (*ctx->vapi_msg_id_t_to_vl_msg_id));
273  ctx->event_cbs = calloc (__vapi_metadata.count, sizeof (*ctx->event_cbs));
274  if (!ctx->event_cbs)
275  {
276  goto fail;
277  }
278  pthread_mutex_init (&ctx->requests_mutex, NULL);
279  *result = ctx;
280  return VAPI_OK;
281 fail:
282  vapi_ctx_free (ctx);
283  return VAPI_ENOMEM;
284 }
285 
286 void
288 {
289  assert (!ctx->connected);
290  free (ctx->requests);
291  free (ctx->vapi_msg_id_t_to_vl_msg_id);
292  free (ctx->event_cbs);
293  free (ctx->vl_msg_id_to_vapi_msg_t);
294  pthread_mutex_destroy (&ctx->requests_mutex);
295  free (ctx);
296 }
297 
298 bool
300 {
301  return vapi_lookup_vl_msg_id (ctx, id) != UINT16_MAX;
302 }
303 
306  const char *chroot_prefix,
309  bool handle_keepalives)
310 {
312  {
313  return VAPI_EINVAL;
314  }
315  if (!clib_mem_get_per_cpu_heap () && !clib_mem_init (0, 1024 * 1024 * 32))
316  {
317  return VAPI_ENOMEM;
318  }
319  ctx->requests_size = max_outstanding_requests;
320  const size_t size = ctx->requests_size * sizeof (*ctx->requests);
321  void *tmp = realloc (ctx->requests, size);
322  if (!tmp)
323  {
324  return VAPI_ENOMEM;
325  }
326  ctx->requests = tmp;
327  clib_memset (ctx->requests, 0, size);
328  /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */
329  ctx->requests_start = ctx->requests_count = 0;
330  if (chroot_prefix)
331  {
332  VAPI_DBG ("set memory root path `%s'", chroot_prefix);
333  vl_set_memory_root_path ((char *) chroot_prefix);
334  }
335  static char api_map[] = "/vpe-api";
336  VAPI_DBG ("client api map `%s'", api_map);
337  if ((vl_client_api_map (api_map)) < 0)
338  {
339  return VAPI_EMAP_FAIL;
340  }
341  VAPI_DBG ("connect client `%s'", name);
342  if (vl_client_connect ((char *) name, 0, response_queue_size) < 0)
343  {
345  return VAPI_ECON_FAIL;
346  }
347 #if VAPI_DEBUG_CONNECT
348  VAPI_DBG ("start probing messages");
349 #endif
350  int rv;
351  int i;
352  for (i = 0; i < __vapi_metadata.count; ++i)
353  {
354  vapi_message_desc_t *m = __vapi_metadata.msgs[i];
355  u8 scratch[m->name_with_crc_len + 1];
356  memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1);
357  u32 id = vl_msg_api_get_msg_index (scratch);
358  if (VAPI_INVALID_MSG_ID != id)
359  {
360  if (id > UINT16_MAX)
361  {
362  VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id,
363  UINT16_MAX);
364  rv = VAPI_EINVAL;
365  goto fail;
366  }
367  if (id > ctx->vl_msg_id_max)
368  {
369  vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t,
370  sizeof
371  (*ctx->vl_msg_id_to_vapi_msg_t) *
372  (id + 1));
373  if (!tmp)
374  {
375  rv = VAPI_ENOMEM;
376  goto fail;
377  }
378  ctx->vl_msg_id_to_vapi_msg_t = tmp;
379  ctx->vl_msg_id_max = id;
380  }
381  ctx->vl_msg_id_to_vapi_msg_t[id] = m->id;
382  ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id;
383 #if VAPI_DEBUG_CONNECT
384  VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc,
385  (unsigned) id);
386 #endif
387  }
388  else
389  {
390  ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX;
391  VAPI_DBG ("Message `%s' not available", m->name_with_crc);
392  }
393  }
394 #if VAPI_DEBUG_CONNECT
395  VAPI_DBG ("finished probing messages");
396 #endif
399  {
400  VAPI_ERR
401  ("control ping or control ping reply not available, cannot connect");
403  goto fail;
404  }
405  ctx->mode = mode;
406  ctx->connected = true;
407  if (vapi_is_msg_available (ctx, vapi_msg_id_memclnt_keepalive))
408  {
409  ctx->handle_keepalives = handle_keepalives;
410  }
411  else
412  {
413  ctx->handle_keepalives = false;
414  }
415  return VAPI_OK;
416 fail:
419  return rv;
420 }
421 
424 {
425  if (!ctx->connected)
426  {
427  return VAPI_EINVAL;
428  }
431 #if VAPI_DEBUG_ALLOC
432  vapi_to_be_freed_validate ();
433 #endif
434  ctx->connected = false;
435  return VAPI_OK;
436 }
437 
440 {
441  return VAPI_ENOTSUP;
442 }
443 
446 {
448  if (!ctx || !msg || !ctx->connected)
449  {
450  rv = VAPI_EINVAL;
451  goto out;
452  }
453  int tmp;
455 #if VAPI_DEBUG
456  unsigned msgid = be16toh (*(u16 *) msg);
457  if (msgid <= ctx->vl_msg_id_max)
458  {
459  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
460  if (id < __vapi_metadata.count)
461  {
462  VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid,
463  __vapi_metadata.msgs[id]->name);
464  }
465  else
466  {
467  VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
468  }
469  }
470  else
471  {
472  VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid);
473  }
474 #endif
475  tmp = svm_queue_add (q, (u8 *) & msg,
476  VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
477  if (tmp < 0)
478  {
479  rv = VAPI_EAGAIN;
480  }
481  else
482  VL_MSG_API_POISON (msg);
483 out:
484  VAPI_DBG ("vapi_send() rv = %d", rv);
485  return rv;
486 }
487 
489 vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
490 {
492  if (!ctx || !msg1 || !msg2 || !ctx->connected)
493  {
494  rv = VAPI_EINVAL;
495  goto out;
496  }
498 #if VAPI_DEBUG
499  unsigned msgid1 = be16toh (*(u16 *) msg1);
500  unsigned msgid2 = be16toh (*(u16 *) msg2);
501  const char *name1 = "UNKNOWN";
502  const char *name2 = "UNKNOWN";
503  if (msgid1 <= ctx->vl_msg_id_max)
504  {
505  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1];
506  if (id < __vapi_metadata.count)
507  {
508  name1 = __vapi_metadata.msgs[id]->name;
509  }
510  }
511  if (msgid2 <= ctx->vl_msg_id_max)
512  {
513  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2];
514  if (id < __vapi_metadata.count)
515  {
516  name2 = __vapi_metadata.msgs[id]->name;
517  }
518  }
519  VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2);
520 #endif
521  int tmp = svm_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2,
522  VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1);
523  if (tmp < 0)
524  {
525  rv = VAPI_EAGAIN;
526  }
527  else
528  VL_MSG_API_POISON (msg1);
529 out:
530  VAPI_DBG ("vapi_send() rv = %d", rv);
531  return rv;
532 }
533 
535 vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size,
536  svm_q_conditional_wait_t cond, u32 time)
537 {
538  if (!ctx || !ctx->connected || !msg || !msg_size)
539  {
540  return VAPI_EINVAL;
541  }
544  uword data;
545 
546  if (am->our_pid == 0)
547  {
548  return VAPI_EINVAL;
549  }
550 
551  svm_queue_t *q = am->vl_input_queue;
552 again:
553  VAPI_DBG ("doing shm queue sub");
554 
555  int tmp = svm_queue_sub (q, (u8 *) & data, cond, time);
556 
557  if (tmp == 0)
558  {
559  VL_MSG_API_UNPOISON ((void *) data);
560 #if VAPI_DEBUG_ALLOC
561  vapi_add_to_be_freed ((void *) data);
562 #endif
563  msgbuf_t *msgbuf =
564  (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data));
565  if (!msgbuf->data_len)
566  {
567  vapi_msg_free (ctx, (u8 *) data);
568  return VAPI_EAGAIN;
569  }
570  *msg = (u8 *) data;
571  *msg_size = ntohl (msgbuf->data_len);
572 #if VAPI_DEBUG
573  unsigned msgid = be16toh (*(u16 *) * msg);
574  if (msgid <= ctx->vl_msg_id_max)
575  {
576  vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid];
577  if (id < __vapi_metadata.count)
578  {
579  VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid,
580  __vapi_metadata.msgs[id]->name);
581  }
582  else
583  {
584  VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
585  }
586  }
587  else
588  {
589  VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid);
590  }
591 #endif
592  if (ctx->handle_keepalives)
593  {
594  unsigned msgid = be16toh (*(u16 *) * msg);
595  if (msgid ==
596  vapi_lookup_vl_msg_id (ctx, vapi_msg_id_memclnt_keepalive))
597  {
598  vapi_msg_memclnt_keepalive_reply *reply = NULL;
599  do
600  {
601  reply = vapi_msg_alloc (ctx, sizeof (*reply));
602  }
603  while (!reply);
604  reply->header.context = vapi_get_client_index (ctx);
605  reply->header._vl_msg_id =
607  vapi_msg_id_memclnt_keepalive_reply);
608  reply->payload.retval = 0;
609  vapi_msg_memclnt_keepalive_reply_hton (reply);
610  while (VAPI_EAGAIN == vapi_send (ctx, reply));
611  vapi_msg_free (ctx, *msg);
612  VAPI_DBG ("autohandled memclnt_keepalive");
613  goto again;
614  }
615  }
616  }
617  else
618  {
619  rv = VAPI_EAGAIN;
620  }
621  return rv;
622 }
623 
626 {
627  return VAPI_ENOTSUP;
628 }
629 
630 static vapi_error_e
632  u32 context, void *msg)
633 {
634  int mrv;
635  if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
636  {
637  VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
638  return VAPI_MUTEX_FAILURE;
639  }
640  int tmp = ctx->requests_start;
641  const int requests_end = vapi_requests_end (ctx);
642  while (ctx->requests[tmp].context != context && tmp != requests_end)
643  {
644  ++tmp;
645  if (tmp == ctx->requests_size)
646  {
647  tmp = 0;
648  }
649  }
650  VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start,
651  ctx->requests[tmp].context == context ? "matched" : "stopped",
652  tmp);
654  if (ctx->requests[tmp].context == context)
655  {
656  while (ctx->requests_start != tmp)
657  {
658  VAPI_ERR ("No response to req with context=%u",
659  (unsigned) ctx->requests[tmp].context);
660  ctx->requests[ctx->requests_start].callback (ctx, ctx->requests
661  [ctx->
662  requests_start].callback_ctx,
663  VAPI_ENORESP, true,
664  NULL);
665  clib_memset (&ctx->requests[ctx->requests_start], 0,
666  sizeof (ctx->requests[ctx->requests_start]));
667  ++ctx->requests_start;
668  --ctx->requests_count;
669  if (ctx->requests_start == ctx->requests_size)
670  {
671  ctx->requests_start = 0;
672  }
673  }
674  // now ctx->requests_start == tmp
675  int payload_offset = vapi_get_payload_offset (id);
676  void *payload = ((u8 *) msg) + payload_offset;
677  bool is_last = true;
678  if (ctx->requests[tmp].is_dump)
679  {
681  {
682  payload = NULL;
683  }
684  else
685  {
686  is_last = false;
687  }
688  }
689  if (payload_offset != -1)
690  {
691  rv =
692  ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx,
693  VAPI_OK, is_last, payload);
694  }
695  else
696  {
697  /* this is a message without payload, so bend the callback a little
698  */
699  rv =
700  ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool))
701  ctx->requests[tmp].callback) (ctx,
702  ctx->requests[tmp].callback_ctx,
703  VAPI_OK, is_last);
704  }
705  if (is_last)
706  {
707  clib_memset (&ctx->requests[ctx->requests_start], 0,
708  sizeof (ctx->requests[ctx->requests_start]));
709  ++ctx->requests_start;
710  --ctx->requests_count;
711  if (ctx->requests_start == ctx->requests_size)
712  {
713  ctx->requests_start = 0;
714  }
715  }
716  VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d",
717  ctx->requests_start, requests_end, ctx->requests_count);
718  }
719  if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
720  {
721  VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
722  strerror (mrv));
723  abort (); /* this really shouldn't happen */
724  }
725  return rv;
726 }
727 
728 static vapi_error_e
730 {
731  if (ctx->event_cbs[id].cb)
732  {
733  return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg);
734  }
735  else if (ctx->generic_cb.cb)
736  {
737  return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg);
738  }
739  else
740  {
741  VAPI_DBG
742  ("No handler/generic handler for msg id %u[%s], message ignored",
743  (unsigned) id, __vapi_metadata.msgs[id]->name);
744  }
745  return VAPI_OK;
746 }
747 
748 bool
750 {
751  assert (id <= __vapi_metadata.count);
752  return __vapi_metadata.msgs[id]->has_context;
753 }
754 
757 {
758  VAPI_DBG ("vapi_dispatch_one()");
759  void *msg;
760  size_t size;
761  vapi_error_e rv = vapi_recv (ctx, &msg, &size, SVM_Q_WAIT, 0);
762  if (VAPI_OK != rv)
763  {
764  VAPI_DBG ("vapi_recv failed with rv=%d", rv);
765  return rv;
766  }
767  u16 vpp_id = be16toh (*(u16 *) msg);
768  if (vpp_id > ctx->vl_msg_id_max)
769  {
770  VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>",
771  (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max);
772  vapi_msg_free (ctx, msg);
773  return VAPI_EINVAL;
774  }
775  if (VAPI_INVALID_MSG_ID == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id])
776  {
777  VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported",
778  (unsigned) vpp_id);
779  vapi_msg_free (ctx, msg);
780  return VAPI_EINVAL;
781  }
782  const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id];
783  const size_t expect_size = vapi_get_message_size (id);
784  if (size < expect_size)
785  {
786  VAPI_ERR
787  ("Invalid msg received, unexpected size `%zu' < expected min `%zu'",
788  size, expect_size);
789  vapi_msg_free (ctx, msg);
790  return VAPI_EINVAL;
791  }
792  u32 context;
793  vapi_get_swap_to_host_func (id) (msg);
794  if (vapi_msg_is_with_context (id))
795  {
796  context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id));
797  /* is this a message originating from VAPI? */
798  VAPI_DBG ("dispatch, context is %x", context);
800  {
801  rv = vapi_dispatch_response (ctx, id, context, msg);
802  goto done;
803  }
804  }
805  rv = vapi_dispatch_event (ctx, id, msg);
806 
807 done:
808  vapi_msg_free (ctx, msg);
809  return rv;
810 }
811 
814 {
816  while (!vapi_requests_empty (ctx))
817  {
819  if (VAPI_OK != rv)
820  {
821  return rv;
822  }
823  }
824  return rv;
825 }
826 
827 void
829  vapi_event_cb callback, void *callback_ctx)
830 {
831  vapi_event_cb_with_ctx *c = &ctx->event_cbs[id];
832  c->cb = callback;
833  c->ctx = callback_ctx;
834 }
835 
836 void
838 {
839  vapi_set_event_cb (ctx, id, NULL, NULL);
840 }
841 
842 void
844  void *callback_ctx)
845 {
846  ctx->generic_cb.cb = callback;
847  ctx->generic_cb.ctx = callback_ctx;
848 }
849 
850 void
852 {
853  ctx->generic_cb.cb = NULL;
854  ctx->generic_cb.ctx = NULL;
855 }
856 
857 u16
859 {
860  assert (id < __vapi_metadata.count);
861  return ctx->vapi_msg_id_t_to_vl_msg_id[id];
862 }
863 
864 int
866 {
868 }
869 
870 bool
872 {
873  return (VAPI_MODE_NONBLOCKING == ctx->mode);
874 }
875 
876 size_t
878 {
879  return ctx->requests_size - 1;
880 }
881 
882 int
884 {
885  assert (id < __vapi_metadata.count);
886  return __vapi_metadata.msgs[id]->payload_offset;
887 }
888 
890 {
891  assert (id < __vapi_metadata.count);
892  return __vapi_metadata.msgs[id]->swap_to_host;
893 }
894 
895 void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg)
896 {
897  assert (id < __vapi_metadata.count);
898  return __vapi_metadata.msgs[id]->swap_to_be;
899 }
900 
901 size_t
903 {
904  assert (id < __vapi_metadata.count);
905  return __vapi_metadata.msgs[id]->size;
906 }
907 
908 size_t
910 {
911  assert (id < __vapi_metadata.count);
912  return __vapi_metadata.msgs[id]->context_offset;
913 }
914 
917 {
918  int i = 0;
919  for (i = 0; i < __vapi_metadata.count; ++i)
920  {
921  if (!strcmp
922  (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc))
923  {
924  /* this happens if somebody is linking together several objects while
925  * using the static inline headers, just fill in the already
926  * assigned id here so that all the objects are in sync */
927  msg->id = __vapi_metadata.msgs[i]->id;
928  return msg->id;
929  }
930  }
931  vapi_msg_id_t id = __vapi_metadata.count;
932  ++__vapi_metadata.count;
933  __vapi_metadata.msgs =
934  realloc (__vapi_metadata.msgs,
935  sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count);
936  __vapi_metadata.msgs[id] = msg;
937  size_t s = strlen (msg->name_with_crc);
938  if (s > __vapi_metadata.max_len_name_with_crc)
939  {
940  __vapi_metadata.max_len_name_with_crc = s;
941  }
942  msg->id = id;
943  return id;
944 }
945 
948 {
949  int mrv;
950  if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex)))
951  {
952  VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv));
953  (void) mrv; /* avoid warning if the above debug is not enabled */
954  return VAPI_MUTEX_FAILURE;
955  }
956  return VAPI_OK;
957 }
958 
961 {
962  int mrv;
963  if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex)))
964  {
965  VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv,
966  strerror (mrv));
967  (void) mrv; /* avoid warning if the above debug is not enabled */
968  return VAPI_MUTEX_FAILURE;
969  }
970  return VAPI_OK;
971 }
972 
973 size_t
975 {
976  return __vapi_metadata.count;
977 }
978 
979 const char *
981 {
982  return __vapi_metadata.msgs[id]->name;
983 }
984 
985 /*
986  * fd.io coding-style-patch-verification: ON
987  *
988  * Local Variables:
989  * eval: (c-set-style "gnu")
990  * End:
991  */
DEFINE_VAPI_MSG_IDS_MEMCLNT_API_JSON
DEFINE_VAPI_MSG_IDS_MEMCLNT_API_JSON
Definition: vapi.c:42
memory_client.h
vlib.h
vapi_error_e
vapi_error_e
Definition: vapi_common.h:25
vapi_internal.h
tmp
u32 * tmp
Definition: interface_output.c:1078
vapi_recv
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:535
vl_msg_api_alloc_or_null
void * vl_msg_api_alloc_or_null(int nbytes)
Definition: memory_shared.c:224
vapi_ctx_s
Definition: vapi.c:75
vapi_connect
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:305
assert
#define assert(x)
Definition: dlmalloc.c:31
vapi_ctx_free
void vapi_ctx_free(vapi_ctx_t ctx)
free vapi context
Definition: vapi.c:287
realloc
void * realloc(void *p, size_t size)
Definition: mem.c:67
vl_client_api_unmap
void vl_client_api_unmap(void)
Definition: memory_client.c:390
types.h
vapi_req_t::context
u32 context
Definition: vapi.c:54
vl_msg_id
#define vl_msg_id(n, h)
Definition: vnet_msg_enum.h:20
name
string name[64]
Definition: fib.api:25
vapi_get_payload_offset
int vapi_get_payload_offset(vapi_msg_id_t id)
Definition: vapi.c:883
next
u16 * next
Definition: nat44_ei_out2in.c:718
vapi_ctx_s::requests_count
int requests_count
Definition: vapi.c:80
svm_queue_t
struct _svm_queue svm_queue_t
vapi_set_generic_event_cb
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:843
vapi_set_event_cb
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:828
vapi_message_desc_t::id
vapi_msg_id_t id
Definition: vapi_internal.h:98
u16
unsigned short u16
Definition: types.h:57
vapi_producer_unlock
vapi_error_e vapi_producer_unlock(vapi_ctx_t ctx)
Definition: vapi.c:960
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
am
app_main_t * am
Definition: application.c:489
vapi_ctx_t
struct vapi_ctx_s * vapi_ctx_t
Definition: vapi.h:47
max_len_name_with_crc
size_t max_len_name_with_crc
Definition: vapi.c:49
msgbuf_::data_len
u32 data_len
message length not including header
Definition: api_common.h:143
vapi_requests_full
bool vapi_requests_full(vapi_ctx_t ctx)
Definition: vapi.c:108
vapi_clear_event_cb
void vapi_clear_event_cb(vapi_ctx_t ctx, vapi_msg_id_t id)
clear event callback for given message id
Definition: vapi.c:837
VAPI_OK
@ VAPI_OK
success
Definition: vapi_common.h:27
vapi_msg_is_with_context
bool vapi_msg_is_with_context(vapi_msg_id_t id)
Definition: vapi.c:749
vl_client_disconnect
int vl_client_disconnect(void)
Definition: memory_client.c:286
vapi_dispatch_response
static vapi_error_e vapi_dispatch_response(vapi_ctx_t ctx, vapi_msg_id_t id, u32 context, void *msg)
Definition: vapi.c:631
vapi_event_cb_with_ctx
Definition: vapi.c:69
vapi_event_cb_with_ctx::ctx
void * ctx
Definition: vapi.c:72
VAPI_MODE_NONBLOCKING
@ VAPI_MODE_NONBLOCKING
operations never block
Definition: vapi_common.h:45
DEFINE_VAPI_MSG_IDS_VPE_API_JSON
DEFINE_VAPI_MSG_IDS_VPE_API_JSON
Definition: vapi.c:43
context_counter_mask
static const u32 context_counter_mask
Definition: vapi.c:60
VL_MSG_API_UNPOISON
static CLIB_NOSANITIZE_ADDR void VL_MSG_API_UNPOISON(const void *a)
Definition: api_common.h:149
vl_client_api_map
int vl_client_api_map(const char *region_name)
Definition: memory_client.c:378
vapi_ctx_s::event_cbs
vapi_event_cb_with_ctx * event_cbs
Definition: vapi.c:84
vapi_ctx_s::generic_cb
vapi_generic_cb_with_ctx generic_cb
Definition: vapi.c:83
clib_mem_get_per_cpu_heap
static clib_mem_heap_t * clib_mem_get_per_cpu_heap(void)
Definition: mem.h:161
vapi_generic_cb_with_ctx
Definition: vapi.c:62
vapi_requests_end
static int vapi_requests_end(vapi_ctx_t ctx)
Definition: vapi.c:120
slot
u8 slot
Definition: pci_types.api:22
vapi_msg_id_control_ping
vapi_msg_id_t vapi_msg_id_control_ping
Definition: vapi.c:39
vapi_ctx_s::context_counter
u32 context_counter
Definition: vapi.c:82
vapi_lookup_vl_msg_id
u16 vapi_lookup_vl_msg_id(vapi_ctx_t ctx, vapi_msg_id_t id)
Definition: vapi.c:858
VAPI_MODE_BLOCKING
@ VAPI_MODE_BLOCKING
operations block until response received
Definition: vapi_common.h:44
VAPI_EAGAIN
@ VAPI_EAGAIN
operation would block
Definition: vapi_common.h:29
vapi_lookup_vapi_msg_id_t
vapi_msg_id_t vapi_lookup_vapi_msg_id_t(vapi_ctx_t ctx, u16 vl_msg_id)
Definition: vapi.c:245
vapi_generic_cb_with_ctx::ctx
void * ctx
Definition: vapi.c:66
VAPI_EINCOMPATIBLE
@ VAPI_EINCOMPATIBLE
fundamental incompatibility while connecting to vpp (control ping/control ping reply mismatch)
Definition: vapi_common.h:35
vapi_ctx_s::requests_mutex
pthread_mutex_t requests_mutex
Definition: vapi.c:90
count
size_t count
Definition: vapi.c:47
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
api_main_t::my_client_index
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:339
api_main_t::shmem_hdr
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:294
vapi_msg_id_t
unsigned int vapi_msg_id_t
Definition: vapi_common.h:55
vl_shmem_hdr_::vl_input_queue
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
uword
u64 uword
Definition: types.h:112
vapi_ctx_s::requests
vapi_req_t * requests
Definition: vapi.c:81
VAPI_ENORESP
@ VAPI_ENORESP
no response to request
Definition: vapi_common.h:32
vapi_req_t
Definition: vapi.c:52
vl_set_memory_root_path
void vl_set_memory_root_path(const char *name)
Definition: memory_shared.c:343
pool.h
Fixed length block allocator. Pools are built from clib vectors and bitmaps. Use pools when repeatedl...
vapi_get_client_index
int vapi_get_client_index(vapi_ctx_t ctx)
Definition: vapi.c:865
vlibapi_get_main
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:390
vapi_get_message_count
size_t vapi_get_message_count()
Definition: vapi.c:974
vapi_event_cb
vapi_error_e(* vapi_event_cb)(vapi_ctx_t ctx, void *callback_ctx, void *payload)
generic vapi event callback
Definition: vapi.h:211
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
vapi_is_msg_available
bool vapi_is_msg_available(vapi_ctx_t ctx, vapi_msg_id_t id)
check if message identified by it's message id is known by the vpp to which the connection is open
Definition: vapi.c:299
vl_msg_api_get_msg_index
u32 vl_msg_api_get_msg_index(u8 *name_and_crc)
Definition: api_shared.c:1119
VL_MSG_API_POISON
static void VL_MSG_API_POISON(const void *a)
Definition: api_common.h:162
vapi_message_desc_t::name_with_crc_len
size_t name_with_crc_len
Definition: vapi_internal.h:91
vapi_dispatch_event
static vapi_error_e vapi_dispatch_event(vapi_ctx_t ctx, vapi_msg_id_t id, void *msg)
Definition: vapi.c:729
response_queue_size
static const int response_queue_size
Definition: vapi_cpp_test.cpp:36
vapi_req_t::callback_ctx
void * callback_ctx
Definition: vapi.c:56
vapi_get_context_offset
size_t vapi_get_context_offset(vapi_msg_id_t id)
Definition: vapi.c:909
vapi_req_t::is_dump
bool is_dump
Definition: vapi.c:57
vapi_get_fd
vapi_error_e vapi_get_fd(vapi_ctx_t ctx, int *fd)
get event file descriptor
Definition: vapi.c:439
vapi_dispatch_one
vapi_error_e vapi_dispatch_one(vapi_ctx_t ctx)
pick next message sent by vpp and call the appropriate callback
Definition: vapi.c:756
vapi_get_request_count
size_t vapi_get_request_count(vapi_ctx_t ctx)
Definition: vapi.c:102
VAPI_DBG
#define VAPI_DBG(...)
Definition: vapi_dbg.h:65
vapi_register_msg
vapi_msg_id_t vapi_register_msg(vapi_message_desc_t *msg)
Definition: vapi.c:916
vapi_msg_free
void vapi_msg_free(vapi_ctx_t ctx, void *msg)
free a vapi message
Definition: vapi.c:232
vapi_message_desc_t::name_with_crc
const char * name_with_crc
Definition: vapi_internal.h:90
vapi_send
vapi_error_e vapi_send(vapi_ctx_t ctx, void *msg)
low-level api for sending messages to vpp
Definition: vapi.c:445
vapi_get_message_size
size_t vapi_get_message_size(vapi_msg_id_t id)
Definition: vapi.c:902
vapi_requests_empty
bool vapi_requests_empty(vapi_ctx_t ctx)
Definition: vapi.c:114
api_main_t
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:228
data
u8 data[128]
Definition: ipsec_types.api:92
id
u8 id[64]
Definition: dhcp.api:160
vapi_ctx_s::requests_size
int requests_size
Definition: vapi.c:78
vapi_ctx_alloc
vapi_error_e vapi_ctx_alloc(vapi_ctx_t *result)
allocate vapi context
Definition: vapi.c:255
vapi_cb_t
vapi_error_e(* vapi_cb_t)(struct vapi_ctx_s *, void *, vapi_error_e, bool, void *)
Definition: vapi_internal.h:81
size
u32 size
Definition: vhost_user.h:125
svm_queue_add
int svm_queue_add(svm_queue_t *q, u8 *elem, int nowait)
Definition: queue.c:260
vl_msg_api_free
void vl_msg_api_free(void *)
Definition: memory_shared.c:311
vapi_msg_id_control_ping_reply
vapi_msg_id_t vapi_msg_id_control_ping_reply
Definition: vapi.c:40
vapi_get_msg_name
const char * vapi_get_msg_name(vapi_msg_id_t id)
Definition: vapi.c:980
vapi_send2
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:489
vapi_get_max_request_count
size_t vapi_get_max_request_count(vapi_ctx_t ctx)
Definition: vapi.c:877
u32
unsigned int u32
Definition: types.h:88
vapi_get_swap_to_be_func
void(*)(void *msg) vapi_get_swap_to_be_func(vapi_msg_id_t id)
Definition: vapi.c:895
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
vapi_disconnect
vapi_error_e vapi_disconnect(vapi_ctx_t ctx)
disconnect from vpp
Definition: vapi.c:423
svm_queue_add2
int svm_queue_add2(svm_queue_t *q, u8 *elem, u8 *elem2, int nowait)
Definition: queue.c:310
vapi_gen_req_context
u32 vapi_gen_req_context(vapi_ctx_t ctx)
Definition: vapi.c:94
vapi_producer_lock
vapi_error_e vapi_producer_lock(vapi_ctx_t ctx)
Definition: vapi.c:947
bool
#define bool
Definition: radix.c:95
vapi_wait
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:625
vapi_wait_mode_e
vapi_wait_mode_e
Definition: vapi_common.h:48
msgs
vapi_message_desc_t ** msgs
Definition: vapi.c:48
VAPI_ERR
#define VAPI_ERR(...)
Definition: vapi_dbg.h:66
msgbuf_
Message header structure.
Definition: api_common.h:140
vapi_message_desc_t
Definition: vapi_internal.h:86
vapi_dbg.h
clib_mem_init
void * clib_mem_init(void *base, uword size)
Definition: mem_dlmalloc.c:266
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vapi_ctx_s::mode
vapi_mode_e mode
Definition: vapi.c:77
vapi_req_t::callback
vapi_cb_t callback
Definition: vapi.c:55
calloc
void * calloc(size_t nmemb, size_t size)
Definition: mem.c:54
u8
unsigned char u8
Definition: types.h:56
vapi_dispatch
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:813
vapi_ctx_s::requests_start
int requests_start
Definition: vapi.c:79
vapi_ctx_s::connected
bool connected
Definition: vapi.c:88
vapi_ctx_s::vl_msg_id_to_vapi_msg_t
vapi_msg_id_t * vl_msg_id_to_vapi_msg_t
Definition: vapi.c:87
VAPI_INVALID_MSG_ID
#define VAPI_INVALID_MSG_ID
Definition: vapi_common.h:57
vapi_ctx_s::vapi_msg_id_t_to_vl_msg_id
u16 * vapi_msg_id_t_to_vl_msg_id
Definition: vapi.c:85
free
void free(void *p)
Definition: mem.c:42
vapi_mode_e
vapi_mode_e
Definition: vapi_common.h:42
vapi.h
vapi_get_swap_to_host_func
void(*)(void *msg) vapi_get_swap_to_host_func(vapi_msg_id_t id)
Definition: vapi.c:889
malloc
void * malloc(size_t size)
Definition: mem.c:33
svm_queue_sub
int svm_queue_sub(svm_queue_t *q, u8 *elem, svm_q_conditional_wait_t cond, u32 time)
Definition: queue.c:369
context
u32 context
Definition: ip.api:780
api_common.h
rv
int __clib_unused rv
Definition: application.c:491
VAPI_ECON_FAIL
@ VAPI_ECON_FAIL
failure while connecting to vpp
Definition: vapi_common.h:34
SVM_Q_WAIT
@ SVM_Q_WAIT
blocking call - best used in combination with condvars, for eventfds we don't yield the cpu
Definition: queue.h:42
vapi_generic_event_cb
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_ENOMEM
@ VAPI_ENOMEM
out of memory
Definition: vapi_common.h:31
VAPI_EMAP_FAIL
@ VAPI_EMAP_FAIL
failure while mapping api
Definition: vapi_common.h:33
vapi_ctx_s::handle_keepalives
bool handle_keepalives
Definition: vapi.c:89
vapi_ctx_s::vl_msg_id_max
u16 vl_msg_id_max
Definition: vapi.c:86
VAPI_ENOTSUP
@ VAPI_ENOTSUP
operation not supported
Definition: vapi_common.h:30
max_outstanding_requests
static const int max_outstanding_requests
Definition: vapi_cpp_test.cpp:35
vapi_clear_generic_event_cb
void vapi_clear_generic_event_cb(vapi_ctx_t ctx)
clear generic event callback
Definition: vapi.c:851
vl_client_connect
int vl_client_connect(const char *name, int ctx_quota, int input_queue_size)
Definition: memory_client.c:161
vapi_is_nonblocking
bool vapi_is_nonblocking(vapi_ctx_t ctx)
Definition: vapi.c:871
svm_q_conditional_wait_t
svm_q_conditional_wait_t
Definition: queue.h:40
vapi_msg_alloc
void * vapi_msg_alloc(vapi_ctx_t ctx, size_t size)
allocate vapi message of given size
Definition: vapi.c:217
vapi_store_request
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_EINVAL
@ VAPI_EINVAL
invalid value encountered
Definition: vapi_common.h:28
VAPI_MUTEX_FAILURE
@ VAPI_MUTEX_FAILURE
failure manipulating internal mutex(es)
Definition: vapi_common.h:37