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