FD.io VPP  v21.01.1
Vector Packet Processing
api_shared.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * api_shared.c - API message handling, common code for both clients
4  * and the vlib process itself.
5  *
6  *
7  * Copyright (c) 2009 Cisco and/or its affiliates.
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *------------------------------------------------------------------
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <vppinfra/format.h>
27 #include <vppinfra/byte_order.h>
28 #include <vppinfra/error.h>
29 #include <vlib/vlib.h>
30 #include <vlib/unix/unix.h>
31 #include <vlibapi/api.h>
32 #include <vppinfra/elog.h>
33 #include <vppinfra/callback.h>
34 
35 /* *INDENT-OFF* */
36 api_main_t api_global_main =
37  {
38  .region_name = "/unset",
39  .api_uid = -1,
40  .api_gid = -1,
41  };
42 /* *INDENT-ON* */
43 
44 /* Please use vlibapi_get_main() to access my_api_main */
46 
47 void
49 {
50  ASSERT (am_arg);
51  my_api_main = (api_main_t *) am_arg;
52 }
53 
54 void
56 {
58  am->missing_clients++;
59 }
60 
61 int
63 {
64  return (am->rx_trace && am->rx_trace->enabled);
65 }
66 
67 int
69 {
70  return (am->tx_trace && am->tx_trace->enabled);
71 }
72 
73 /*
74  * vl_msg_api_trace
75  */
76 void
78 {
79  u8 **this_trace;
80  u8 **old_trace;
81  u8 *msg_copy;
82  u32 length;
83  trace_cfg_t *cfgp;
84  u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
85  msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
86 
87  cfgp = am->api_trace_cfg + msg_id;
88 
89  if (!cfgp || !cfgp->trace_enable)
90  return;
91 
92  msg_copy = 0;
93 
94  if (tp->nitems == 0)
95  {
96  clib_warning ("tp->nitems is 0");
97  return;
98  }
99 
100  if (vec_len (tp->traces) < tp->nitems)
101  {
102  vec_add1 (tp->traces, 0);
103  this_trace = tp->traces + vec_len (tp->traces) - 1;
104  }
105  else
106  {
107  tp->wrapped = 1;
108  old_trace = tp->traces + tp->curindex++;
109  if (tp->curindex == tp->nitems)
110  tp->curindex = 0;
111  /* Reuse the trace record, may save some memory allocator traffic */
112  msg_copy = *old_trace;
113  vec_reset_length (msg_copy);
114  this_trace = old_trace;
115  }
116 
117  length = clib_net_to_host_u32 (header->data_len);
118 
119  vec_validate (msg_copy, length - 1);
120  clib_memcpy_fast (msg_copy, msg, length);
121  *this_trace = msg_copy;
122 }
123 
124 int
126  int onoff)
127 {
128  vl_api_trace_t *tp;
129  int rv;
130 
131  switch (which)
132  {
133  case VL_API_TRACE_TX:
134  tp = am->tx_trace;
135  if (tp == 0)
136  {
137  vl_msg_api_trace_configure (am, which, 1024);
138  tp = am->tx_trace;
139  }
140  break;
141 
142  case VL_API_TRACE_RX:
143  tp = am->rx_trace;
144  if (tp == 0)
145  {
146  vl_msg_api_trace_configure (am, which, 1024);
147  tp = am->rx_trace;
148  }
149  break;
150 
151  default:
152  /* duh? */
153  return -1;
154  }
155 
156  /* Configured? */
157  if (tp == 0 || tp->nitems == 0)
158  return -1;
159 
160  rv = tp->enabled;
161  tp->enabled = onoff;
162 
163  return rv;
164 }
165 
166 int
168 {
169  vl_api_trace_t *tp;
170  int i;
171 
172  switch (which)
173  {
174  case VL_API_TRACE_TX:
175  tp = am->tx_trace;
176  break;
177 
178  case VL_API_TRACE_RX:
179  tp = am->rx_trace;
180  break;
181 
182  default:
183  /* duh? */
184  return -1;
185  }
186 
187  /* Configured? */
188  if (!tp || tp->nitems == 0)
189  return -1;
190 
191  tp->curindex = 0;
192  tp->wrapped = 0;
193 
194  for (i = 0; i < vec_len (tp->traces); i++)
195  {
196  vec_free (tp->traces[i]);
197  }
198  vec_free (tp->traces);
199 
200  return 0;
201 }
202 
203 u8 *
205 {
206  serialize_main_t _sm, *sm = &_sm;
207  hash_pair_t *hp;
209 
210  serialize_open_vector (sm, vector);
211 
212  /* serialize the count */
213  serialize_integer (sm, nmsg, sizeof (u32));
214 
215  /* *INDENT-OFF* */
217  ({
218  serialize_likely_small_unsigned_integer (sm, hp->value[0]);
219  serialize_cstring (sm, (char *) hp->key);
220  }));
221  /* *INDENT-ON* */
222 
223  return serialize_close_vector (sm);
224 }
225 
226 int
228 {
229  vl_api_trace_t *tp;
230  vl_api_trace_file_header_t fh;
231  int i;
232  u8 *msg;
233 
234  switch (which)
235  {
236  case VL_API_TRACE_TX:
237  tp = am->tx_trace;
238  break;
239 
240  case VL_API_TRACE_RX:
241  tp = am->rx_trace;
242  break;
243 
244  default:
245  /* duh? */
246  return -1;
247  }
248 
249  /* Configured, data present? */
250  if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
251  return -1;
252 
253  /* "Dare to be stupid" check */
254  if (fp == 0)
255  {
256  return -2;
257  }
258 
259  /* Write the file header */
260  fh.wrapped = tp->wrapped;
261  fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
262  u8 *m = vl_api_serialize_message_table (am, 0);
263  fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
264 
265  if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
266  {
267  return (-10);
268  }
269 
270  /* Write the message table */
271  if (fwrite (m, vec_len (m), 1, fp) != 1)
272  {
273  return (-14);
274  }
275  vec_free (m);
276 
277  /* No-wrap case */
278  if (tp->wrapped == 0)
279  {
280  /*
281  * Note: vec_len return 0 when fed a NULL pointer.
282  * Unfortunately, the static analysis tool doesn't
283  * figure it out, hence the suppressed warnings.
284  * What a great use of my time.
285  */
286  for (i = 0; i < vec_len (tp->traces); i++)
287  {
288  u32 msg_length;
289  /*sa_ignore NO_NULL_CHK */
290  msg = tp->traces[i];
291  /*
292  * This retarded check required to pass
293  * [sic] SA-checking.
294  */
295  if (!msg)
296  continue;
297 
298  msg_length = clib_host_to_net_u32 (vec_len (msg));
299  if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
300  != sizeof (msg_length))
301  {
302  return (-14);
303  }
304  if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
305  {
306  return (-11);
307  }
308  }
309  }
310  else
311  {
312  /* Wrap case: write oldest -> end of buffer */
313  for (i = tp->curindex; i < vec_len (tp->traces); i++)
314  {
315  u32 msg_length;
316  msg = tp->traces[i];
317  /*
318  * This retarded check required to pass
319  * [sic] SA-checking
320  */
321  if (!msg)
322  continue;
323 
324  msg_length = clib_host_to_net_u32 (vec_len (msg));
325  if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
326  != sizeof (msg_length))
327  {
328  return (-14);
329  }
330 
331  if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
332  {
333  return (-12);
334  }
335  }
336  /* write beginning of buffer -> oldest-1 */
337  for (i = 0; i < tp->curindex; i++)
338  {
339  u32 msg_length;
340  /*sa_ignore NO_NULL_CHK */
341  msg = tp->traces[i];
342  /*
343  * This retarded check required to pass
344  * [sic] SA-checking
345  */
346  if (!msg)
347  continue;
348 
349  msg_length = clib_host_to_net_u32 (vec_len (msg));
350  if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
351  != sizeof (msg_length))
352  {
353  return (-14);
354  }
355 
356  if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
357  {
358  return (-13);
359  }
360  }
361  }
362  return 0;
363 }
364 
365 int
367  u32 nitems)
368 {
369  vl_api_trace_t *tp;
370  int was_on = 0;
371 
372  switch (which)
373  {
374  case VL_API_TRACE_TX:
375  tp = am->tx_trace;
376  if (tp == 0)
377  {
378  vec_validate (am->tx_trace, 0);
379  tp = am->tx_trace;
380  }
381  break;
382 
383  case VL_API_TRACE_RX:
384  tp = am->rx_trace;
385  if (tp == 0)
386  {
387  vec_validate (am->rx_trace, 0);
388  tp = am->rx_trace;
389  }
390 
391  break;
392 
393  default:
394  return -1;
395 
396  }
397 
398  if (tp->enabled)
399  {
400  was_on = vl_msg_api_trace_onoff (am, which, 0);
401  }
402  if (tp->traces)
403  {
404  vl_msg_api_trace_free (am, which);
405  }
406 
407  clib_memset (tp, 0, sizeof (*tp));
408 
410  {
412  }
413  else
414  {
416  }
417 
418  tp->nitems = nitems;
419  if (was_on)
420  {
421  (void) vl_msg_api_trace_onoff (am, which, was_on);
422  }
423  return 0;
424 }
425 
426 void
428 {
429 }
430 
431 void
433 {
434 }
435 
436 always_inline void
438  void *the_msg, int trace_it, int do_it, int free_it)
439 {
440  u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
441  u8 *(*print_fp) (void *, void *);
442 
444  {
445  /* *INDENT-OFF* */
446  ELOG_TYPE_DECLARE (e) =
447  {
448  .format = "api-msg: %s",
449  .format_args = "T4",
450  };
451  /* *INDENT-ON* */
452  struct
453  {
454  u32 c;
455  } *ed;
456  ed = ELOG_DATA (am->elog_main, e);
457  if (id < vec_len (am->msg_names))
458  ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
459  else
460  ed->c = elog_string (am->elog_main, "BOGUS");
461  }
462 
463  if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
464  {
465  if (trace_it)
466  vl_msg_api_trace (am, am->rx_trace, the_msg);
467 
468  if (am->msg_print_flag)
469  {
470  fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
471  print_fp = (void *) am->msg_print_handlers[id];
472  if (print_fp == 0)
473  {
474  fformat (stdout, " [no registered print fn]\n");
475  }
476  else
477  {
478  (*print_fp) (the_msg, stdout);
479  }
480  }
481 
482  if (do_it)
483  {
484  if (!am->is_mp_safe[id])
485  {
488  }
489 
490  if (am->is_autoendian[id])
491  {
492  void (*endian_fp) (void *);
493  endian_fp = am->msg_endian_handlers[id];
494  (*endian_fp) (the_msg);
495  }
496 
497  if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
499  0 /* before */ );
500 
501  (*am->msg_handlers[id]) (the_msg);
502 
503  if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
505  1 /* after */ );
506  if (!am->is_mp_safe[id])
508  }
509  }
510  else
511  {
512  clib_warning ("no handler for msg id %d", id);
513  }
514 
515  if (free_it)
516  vl_msg_api_free (the_msg);
517 
519  {
520  /* *INDENT-OFF* */
521  ELOG_TYPE_DECLARE (e) =
522  {
523  .format = "api-msg-done(%s): %s",
524  .format_args = "t4T4",
525  .n_enum_strings = 2,
526  .enum_strings =
527  {
528  "barrier",
529  "mp-safe",
530  }
531  };
532  /* *INDENT-ON* */
533 
534  struct
535  {
536  u32 barrier;
537  u32 c;
538  } *ed;
539  ed = ELOG_DATA (am->elog_main, e);
540  if (id < vec_len (am->msg_names))
541  {
542  ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
543  ed->barrier = !am->is_mp_safe[id];
544  }
545  else
546  {
547  ed->c = elog_string (am->elog_main, "BOGUS");
548  ed->barrier = 0;
549  }
550  }
551 }
552 
553 void (*vl_msg_api_fuzz_hook) (u16, void *);
554 
555 /* This is only to be called from a vlib/vnet app */
556 void
558  void *the_msg, vlib_main_t * vm,
559  vlib_node_runtime_t * node, u8 is_private)
560 {
561  u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
562  u8 *(*handler) (void *, void *, void *);
563  u8 *(*print_fp) (void *, void *);
564  svm_region_t *old_vlib_rp;
565  void *save_shmem_hdr;
566  int is_mp_safe = 1;
567 
569  {
570  /* *INDENT-OFF* */
571  ELOG_TYPE_DECLARE (e) =
572  {
573  .format = "api-msg: %s",
574  .format_args = "T4",
575  };
576  /* *INDENT-ON* */
577  struct
578  {
579  u32 c;
580  } *ed;
581  ed = ELOG_DATA (am->elog_main, e);
582  if (id < vec_len (am->msg_names))
583  ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
584  else
585  ed->c = elog_string (am->elog_main, "BOGUS");
586  }
587 
588  if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
589  {
590  handler = (void *) am->msg_handlers[id];
591 
592  if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
593  vl_msg_api_trace (am, am->rx_trace, the_msg);
594 
595  if (PREDICT_FALSE (am->msg_print_flag))
596  {
597  fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
598  print_fp = (void *) am->msg_print_handlers[id];
599  if (print_fp == 0)
600  {
601  fformat (stdout, " [no registered print fn for msg %d]\n", id);
602  }
603  else
604  {
605  (*print_fp) (the_msg, vm);
606  }
607  }
608  is_mp_safe = am->is_mp_safe[id];
609 
610  if (!is_mp_safe)
611  {
614  }
615  if (is_private)
616  {
617  old_vlib_rp = am->vlib_rp;
618  save_shmem_hdr = am->shmem_hdr;
619  am->vlib_rp = vlib_rp;
620  am->shmem_hdr = (void *) vlib_rp->user_ctx;
621  }
622 
624  (*vl_msg_api_fuzz_hook) (id, the_msg);
625 
626  if (am->is_autoendian[id])
627  {
628  void (*endian_fp) (void *);
629  endian_fp = am->msg_endian_handlers[id];
630  (*endian_fp) (the_msg);
631  }
632  if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
633  clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */ );
634 
635  (*handler) (the_msg, vm, node);
636 
637  if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
638  clib_call_callbacks (am->perf_counter_cbs, am, id, 1 /* after */ );
639  if (is_private)
640  {
641  am->vlib_rp = old_vlib_rp;
642  am->shmem_hdr = save_shmem_hdr;
643  }
644  if (!is_mp_safe)
646  }
647  else
648  {
649  clib_warning ("no handler for msg id %d", id);
650  }
651 
652  /*
653  * Special-case, so we can e.g. bounce messages off the vnet
654  * main thread without copying them...
655  */
656  if (id >= vec_len (am->message_bounce) || !(am->message_bounce[id]))
657  vl_msg_api_free (the_msg);
658 
660  {
661  /* *INDENT-OFF* */
662  ELOG_TYPE_DECLARE (e) =
663  {
664  .format = "api-msg-done(%s): %s",
665  .format_args = "t4T4",
666  .n_enum_strings = 2,
667  .enum_strings =
668  {
669  "barrier",
670  "mp-safe",
671  }
672  };
673  /* *INDENT-ON* */
674 
675  struct
676  {
677  u32 barrier;
678  u32 c;
679  } *ed;
680  ed = ELOG_DATA (am->elog_main, e);
681  if (id < vec_len (am->msg_names))
682  ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
683  else
684  ed->c = elog_string (am->elog_main, "BOGUS");
685  ed->barrier = is_mp_safe;
686  }
687 }
688 
689 void
690 vl_msg_api_handler (void *the_msg)
691 {
692  api_main_t *am = vlibapi_get_main ();
693 
694  msg_handler_internal (am, the_msg,
695  (am->rx_trace
696  && am->rx_trace->enabled) /* trace_it */ ,
697  1 /* do_it */ , 1 /* free_it */ );
698 }
699 
700 void
702 {
703  api_main_t *am = vlibapi_get_main ();
704  msg_handler_internal (am, the_msg,
705  (am->rx_trace
706  && am->rx_trace->enabled) /* trace_it */ ,
707  1 /* do_it */ , 0 /* free_it */ );
708 }
709 
710 void
712 {
713  api_main_t *am = vlibapi_get_main ();
714  msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
715  0 /* free_it */ );
716 }
717 
718 /*
719  * Add a trace record to the API message trace buffer, if
720  * API message tracing is enabled. Handy for adding sufficient
721  * data to the trace to reproduce autonomous state, as opposed to
722  * state downloaded via control-plane API messages. Example: the NAT
723  * application creates database entries based on packet traffic, not
724  * control-plane messages.
725  *
726  */
727 void
728 vl_msg_api_trace_only (void *the_msg)
729 {
730  api_main_t *am = vlibapi_get_main ();
731 
732  msg_handler_internal (am, the_msg,
733  (am->rx_trace
734  && am->rx_trace->enabled) /* trace_it */ ,
735  0 /* do_it */ , 0 /* free_it */ );
736 }
737 
738 void
740 {
741  api_main_t *am = vlibapi_get_main ();
742  u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
743 
744  if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
745  {
746  clib_warning ("_vl_msg_id too large: %d\n", id);
747  return;
748  }
749  if (am->msg_cleanup_handlers[id])
750  (*am->msg_cleanup_handlers[id]) (the_msg);
751 
752  vl_msg_api_free (the_msg);
753 }
754 
755 /*
756  * vl_msg_api_replay_handler
757  */
758 void
760 {
761  api_main_t *am = vlibapi_get_main ();
762 
763  u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
764 
765  if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
766  {
767  clib_warning ("_vl_msg_id too large: %d\n", id);
768  return;
769  }
770  /* do NOT trace the message... */
771  if (am->msg_handlers[id])
772  (*am->msg_handlers[id]) (the_msg);
773  /* do NOT free the message buffer... */
774 }
775 
776 u32
778 {
779  return vl_msg_api_get_msg_length_inline (msg_arg);
780 }
781 
782 /*
783  * vl_msg_api_socket_handler
784  */
785 void
787 {
788  api_main_t *am = vlibapi_get_main ();
789 
790  msg_handler_internal (am, the_msg,
791  (am->rx_trace
792  && am->rx_trace->enabled) /* trace_it */ ,
793  1 /* do_it */ , 0 /* free_it */ );
794 }
795 
796 #define foreach_msg_api_vector \
797 _(msg_names) \
798 _(msg_handlers) \
799 _(msg_cleanup_handlers) \
800 _(msg_endian_handlers) \
801 _(msg_print_handlers) \
802 _(api_trace_cfg) \
803 _(message_bounce) \
804 _(is_mp_safe) \
805 _(is_autoendian)
806 
807 void
809 {
810  api_main_t *am = vlibapi_get_main ();
811 
812  /*
813  * This happens during the java core tests if the message
814  * dictionary is missing newly added xxx_reply_t messages.
815  * Should never happen, but since I shot myself in the foot once
816  * this way, I thought I'd make it easy to debug if I ever do
817  * it again... (;-)...
818  */
819  if (c->id == 0)
820  {
821  if (c->name)
822  clib_warning ("Trying to register %s with a NULL msg id!", c->name);
823  else
824  clib_warning ("Trying to register a NULL msg with a NULL msg id!");
825  clib_warning ("Did you forget to call setup_message_id_table?");
826  return;
827  }
828 
829 #define _(a) vec_validate (am->a, c->id);
831 #undef _
832 
833  if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
835  ("BUG: re-registering 'vl_api_%s_t_handler'."
836  "Handler was %llx, replaced by %llx",
837  c->name, am->msg_handlers[c->id], c->handler);
838 
839  am->msg_names[c->id] = c->name;
840  am->msg_handlers[c->id] = c->handler;
841  am->msg_cleanup_handlers[c->id] = c->cleanup;
842  am->msg_endian_handlers[c->id] = c->endian;
843  am->msg_print_handlers[c->id] = c->print;
844  am->message_bounce[c->id] = c->message_bounce;
845  am->is_mp_safe[c->id] = c->is_mp_safe;
846  am->is_autoendian[c->id] = c->is_autoendian;
847 
848  am->api_trace_cfg[c->id].size = c->size;
849  am->api_trace_cfg[c->id].trace_enable = c->traced;
850  am->api_trace_cfg[c->id].replay_enable = c->replay;
851 }
852 
853 /*
854  * vl_msg_api_set_handlers
855  * preserve the old API for a while
856  */
857 void
858 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
859  void *endian, void *print, int size, int traced)
860 {
862  vl_msg_api_msg_config_t *c = &cfg;
863 
864  clib_memset (c, 0, sizeof (*c));
865 
866  c->id = id;
867  c->name = name;
868  c->handler = handler;
869  c->cleanup = cleanup;
870  c->endian = endian;
871  c->print = print;
872  c->traced = traced;
873  c->replay = 1;
874  c->message_bounce = 0;
875  c->is_mp_safe = 0;
876  c->is_autoendian = 0;
877  vl_msg_api_config (c);
878 }
879 
880 void
882 {
884  vl_msg_api_msg_config_t *c = &cfg;
885 
886  clib_memset (c, 0, sizeof (*c));
887 
888  c->id = msg_id;
889  vl_msg_api_config (c);
890 }
891 
892 void
893 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
894 {
895  api_main_t *am = vlibapi_get_main ();
896  ASSERT (msg_id > 0);
897 
898  vec_validate (am->msg_cleanup_handlers, msg_id);
899  am->msg_cleanup_handlers[msg_id] = fp;
900 }
901 
902 void
904 {
905  uword msg;
906 
907  while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
908  vl_msg_api_handler ((void *) msg);
909 }
910 
911 u32
913 {
914  msgbuf_t *mb;
915  u32 data_len = ~0;
916 
917  /* Work out the maximum sane message length, and return it */
918  if (PREDICT_TRUE (mp != 0))
919  {
920  mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
921  data_len = clib_net_to_host_u32 (mb->data_len);
922  }
923  return data_len;
924 }
925 
928 {
929  switch (which)
930  {
931  case VL_API_TRACE_RX:
932  return am->rx_trace;
933  case VL_API_TRACE_TX:
934  return am->tx_trace;
935  default:
936  return 0;
937  }
938 }
939 
940 void
941 vl_noop_handler (void *mp)
942 {
943 }
944 
945 
947 
948 void
950 {
951  post_mortem_dump_enabled = enable;
952 }
953 
954 void
956 {
957  api_main_t *am = vlibapi_get_main ();
958  FILE *fp;
959  char filename[64];
960  int rv;
961 
962  if (post_mortem_dump_enabled == 0)
963  return;
964 
965  snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
966  getpid ());
967 
968  fp = fopen (filename, "w");
969  if (fp == NULL)
970  {
971  rv = write (2, "Couldn't create ", 16);
972  rv = write (2, filename, strlen (filename));
973  rv = write (2, "\n", 1);
974  return;
975  }
976  rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
977  fclose (fp);
978  if (rv < 0)
979  {
980  rv = write (2, "Failed to save post-mortem API trace to ", 40);
981  rv = write (2, filename, strlen (filename));
982  rv = write (2, "\n", 1);
983  }
984 
985 }
986 
987 /* Layered message handling support */
988 
989 void
990 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
991 {
992  api_main_t *am = vlibapi_get_main ();
993 
994  /* Mild idiot proofing */
995  if (msg_id_host_byte_order > 10000)
996  clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
997  msg_id_host_byte_order,
998  clib_net_to_host_u16 (msg_id_host_byte_order));
999  vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
1000  am->pd_msg_handlers[msg_id_host_byte_order] = fp;
1001 }
1002 
1003 int
1004 vl_msg_api_pd_handler (void *mp, int rv)
1005 {
1006  api_main_t *am = vlibapi_get_main ();
1007  int (*fp) (void *, int);
1008  u16 msg_id;
1009 
1011  msg_id = clib_net_to_host_u16 (*((u16 *) mp));
1012  else
1013  msg_id = *((u16 *) mp);
1014 
1015  if (msg_id >= vec_len (am->pd_msg_handlers)
1016  || am->pd_msg_handlers[msg_id] == 0)
1017  return rv;
1018 
1019  fp = am->pd_msg_handlers[msg_id];
1020  rv = (*fp) (mp, rv);
1021  return rv;
1022 }
1023 
1024 void
1026 {
1027  api_main_t *am = vlibapi_get_main ();
1028 
1029  am->first_available_msg_id = first_avail;
1030 }
1031 
1032 u16
1033 vl_msg_api_get_msg_ids (const char *name, int n)
1034 {
1035  api_main_t *am = vlibapi_get_main ();
1036  u8 *name_copy;
1037  vl_api_msg_range_t *rp;
1038  uword *p;
1039  u16 rv;
1040 
1041  if (am->msg_range_by_name == 0)
1042  am->msg_range_by_name = hash_create_string (0, sizeof (uword));
1043 
1044  name_copy = format (0, "%s%c", name, 0);
1045 
1046  p = hash_get_mem (am->msg_range_by_name, name_copy);
1047  if (p)
1048  {
1049  clib_warning ("WARNING: duplicate message range registration for '%s'",
1050  name_copy);
1051  vec_free (name_copy);
1052  return ((u16) ~ 0);
1053  }
1054 
1055  if (n < 0 || n > 1024)
1056  {
1057  clib_warning
1058  ("WARNING: bad number of message-IDs (%d) requested by '%s'",
1059  n, name_copy);
1060  vec_free (name_copy);
1061  return ((u16) ~ 0);
1062  }
1063 
1064  vec_add2 (am->msg_ranges, rp, 1);
1065 
1066  rv = rp->first_msg_id = am->first_available_msg_id;
1067  am->first_available_msg_id += n;
1068  rp->last_msg_id = am->first_available_msg_id - 1;
1069  rp->name = name_copy;
1070 
1071  hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
1072 
1073  return rv;
1074 }
1075 
1076 void
1077 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
1078 {
1079  uword *p;
1080 
1081  if (am->msg_index_by_name_and_crc == 0)
1083 
1084  p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1085  if (p)
1086  {
1087  clib_warning ("attempt to redefine '%s' ignored...", string);
1088  return;
1089  }
1090 
1091  hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1092 }
1093 
1094 void
1095 vl_msg_api_add_version (api_main_t * am, const char *string,
1096  u32 major, u32 minor, u32 patch)
1097 {
1098  api_version_t version = {.major = major,.minor = minor,.patch = patch };
1099  ASSERT (strlen (string) < 64);
1100  strncpy (version.name, string, 64 - 1);
1101  vec_add1 (am->api_version_list, version);
1102 }
1103 
1104 u32
1106 {
1107  api_main_t *am = vlibapi_get_main ();
1108  uword *p;
1109 
1110  if (am->msg_index_by_name_and_crc)
1111  {
1112  p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1113  if (p)
1114  return p[0];
1115  }
1116  return ~0;
1117 }
1118 
1119 void *
1121 {
1122  pthread_mutex_lock (&vlib_rp->mutex);
1123  return svm_push_data_heap (vlib_rp);
1124 }
1125 
1126 void *
1128 {
1129  api_main_t *am = vlibapi_get_main ();
1130  return vl_msg_push_heap_w_region (am->vlib_rp);
1131 }
1132 
1133 void
1135 {
1136  svm_pop_heap (oldheap);
1137  pthread_mutex_unlock (&vlib_rp->mutex);
1138 }
1139 
1140 void
1141 vl_msg_pop_heap (void *oldheap)
1142 {
1143  api_main_t *am = vlibapi_get_main ();
1144  vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1145 }
1146 
1147 /* Must be nul terminated */
1148 int
1150 {
1151  /* copy without nul terminator */
1152  u32 len = strlen (buf);
1153  if (len > 0)
1154  clib_memcpy_fast (str->buf, buf, len);
1155  str->length = htonl (len);
1156  return len + sizeof (u32);
1157 }
1158 
1159 /* Must NOT be nul terminated */
1160 int
1162 {
1163  u32 len = vec_len (vec);
1164  clib_memcpy (str->buf, vec, len);
1165  str->length = htonl (len);
1166  return len + sizeof (u32);
1167 }
1168 
1169 u32
1171 {
1172  return clib_net_to_host_u32 (astr->length);
1173 }
1174 
1175 u8 *
1176 vl_api_format_string (u8 * s, va_list * args)
1177 {
1178  vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1179  vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1180  return s;
1181 }
1182 
1183 /*
1184  * Returns a new vector. Remember to free it after use.
1185  * NOT nul terminated.
1186  */
1187 u8 *
1189 {
1190  u8 *v = 0;
1191 
1192  if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1193  return format (0, "insane astr->length %u%c",
1194  clib_net_to_host_u32 (astr->length), 0);
1195  vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1196  return v;
1197 }
1198 
1199 /*
1200  * Returns a new vector. Remember to free it after use.
1201  * Nul terminated.
1202  */
1203 char *
1205 {
1206  char *v = 0;
1207  if (clib_net_to_host_u32 (astr->length) > 0)
1208  {
1209  vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1210  vec_add1 (v, 0);
1211  }
1212  return v;
1213 }
1214 
1215 void
1217 {
1218  api_main_t *am = vlibapi_get_main ();
1219  am->elog_main = m;
1220 }
1221 
1222 int
1224 {
1225  int rv;
1226  api_main_t *am = vlibapi_get_main ();
1227 
1228  rv = am->elog_trace_api_messages;
1229  am->elog_trace_api_messages = enable;
1230  return rv;
1231 }
1232 
1233 int
1235 {
1236  api_main_t *am = vlibapi_get_main ();
1237 
1238  return am->elog_trace_api_messages;
1239 }
1240 
1241 /*
1242  * fd.io coding-style-patch-verification: ON
1243  *
1244  * Local Variables:
1245  * eval: (c-set-style "gnu")
1246  * End:
1247  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
void vl_msg_api_set_first_available_msg_id(u16 first_avail)
Definition: api_shared.c:1025
Message range (belonging to a plugin)
Definition: api_common.h:113
int vl_msg_api_trace_onoff(api_main_t *am, vl_api_trace_which_t which, int onoff)
Definition: api_shared.c:125
int vl_msg_api_trace_save(api_main_t *am, vl_api_trace_which_t which, FILE *fp)
Definition: api_shared.c:227
void vl_msg_api_set_handlers(int id, char *name, void *handler, void *cleanup, void *endian, void *print, int size, int traced)
Definition: api_shared.c:858
int vl_msg_api_trace_free(api_main_t *am, vl_api_trace_which_t which)
Definition: api_shared.c:167
int id
the message ID
Definition: api_common.h:123
u8 * name
name of the plugin
Definition: api_common.h:115
static void svm_pop_heap(void *oldheap)
Definition: svm.h:94
void vl_msg_api_set_cleanup_handler(int msg_id, void *fp)
Definition: api_shared.c:893
int vl_msg_api_pd_handler(void *mp, int rv)
Definition: api_shared.c:1004
a
Definition: bitmap.h:544
void vl_msg_api_handler(void *the_msg)
Definition: api_shared.c:690
void * print
message print function
Definition: api_common.h:129
u8 wrapped
trace has wrapped
Definition: api_common.h:95
static u64 do_it(pg_main_t *pg, pg_stream_t *s, u32 *buffers, u32 n_buffers, u32 lo_bit, u32 hi_bit, u64 v_min, u64 v_max, u64 v, pg_edit_type_t edit_type)
Definition: input.c:757
Optimized string handling code, including c11-compliant "safe C library" variants.
#define PREDICT_TRUE(x)
Definition: clib.h:122
int size
for sanity checking
Definition: api_common.h:83
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static void msg_handler_internal(api_main_t *am, void *the_msg, int trace_it, int do_it, int free_it)
Definition: api_shared.c:437
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u8 * message_bounce
Don&#39;t automatically free message buffer vetor.
Definition: api_common.h:247
static u8 post_mortem_dump_enabled
Definition: api_shared.c:946
Message configuration definition.
Definition: api_common.h:121
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
vl_api_trace_t * vl_msg_api_trace_get(api_main_t *am, vl_api_trace_which_t which)
Definition: api_shared.c:927
u32 vl_msg_api_get_msg_length(void *msg_arg)
Definition: api_shared.c:777
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
api_version_t * api_version_list
api version list
Definition: api_common.h:353
#define hash_set_mem(h, key, value)
Definition: hash.h:275
vlib_main_t * vm
Definition: in2out_ed.c:1580
int vl_msg_api_trace_configure(api_main_t *am, vl_api_trace_which_t which, u32 nitems)
Definition: api_shared.c:366
void vl_msg_api_set_global_main(void *am_arg)
Definition: api_shared.c:48
#define vl_msg_api_barrier_trace_context(X)
Definition: api_common.h:191
void(** msg_cleanup_handlers)(void *)
non-default message cleanup handler vector
Definition: api_common.h:235
void vl_noop_handler(void *mp)
Definition: api_shared.c:941
void * cleanup
non-default message cleanup handler
Definition: api_common.h:127
u32 minor
Definition: memclnt.api:119
unsigned char u8
Definition: types.h:56
trace_cfg_t * api_trace_cfg
Current trace configuration.
Definition: api_common.h:277
void vl_msg_api_handler_no_trace_no_free(void *the_msg)
Definition: api_shared.c:711
u8 data[128]
Definition: ipsec_types.api:90
int size
message size
Definition: api_common.h:130
u8 id[64]
Definition: dhcp.api:160
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
__clib_export void * serialize_close_vector(serialize_main_t *m)
Definition: serialize.c:919
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:668
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
void vl_msg_api_cleanup_handler(void *the_msg)
Definition: api_shared.c:739
void * endian
message endian function
Definition: api_common.h:128
int svm_queue_sub(svm_queue_t *q, u8 *elem, svm_q_conditional_wait_t cond, u32 time)
Definition: queue.c:369
vl_api_trace_t * rx_trace
Received message trace configuration.
Definition: api_common.h:268
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:86
int which
Definition: cJSON.h:234
Callback multiplex scheme For a fully worked-out example, see .../src/vlib/main.
void vl_msg_api_config(vl_msg_api_msg_config_t *c)
Definition: api_shared.c:808
volatile void * user_ctx
Definition: svm_common.h:47
description fragment has unexpected format
Definition: map.api:433
void(* vl_msg_api_fuzz_hook)(u16, void *)
Definition: api_shared.c:553
void vl_msg_api_post_mortem_dump_enable_disable(int enable)
Definition: api_shared.c:949
#define clib_arch_is_little_endian
Definition: byte_order.h:54
svm_region_t * vlib_rp
Current binary api segment descriptor.
Definition: api_common.h:283
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:293
unsigned int u32
Definition: types.h:88
#define VL_API_BIG_ENDIAN
Definition: api_common.h:110
int vl_msg_api_tx_trace_enabled(api_main_t *am)
Definition: api_shared.c:68
char name[64]
Definition: api_common.h:223
void vl_msg_api_trace_only(void *the_msg)
Definition: api_shared.c:728
void vl_msg_api_clean_handlers(int msg_id)
Definition: api_shared.c:881
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
u32 patch
Definition: memclnt.api:120
u16 last_msg_id
last assigned message ID
Definition: api_common.h:117
int vl_api_get_elog_trace_api_messages(void)
Definition: api_shared.c:1234
int elog_trace_api_messages
Definition: api_common.h:375
void vl_msg_pop_heap_w_region(svm_region_t *vlib_rp, void *oldheap)
Definition: api_shared.c:1134
void(** perf_counter_cbs)(struct api_main_t *, u32 id, int before_or_after)
performance counter callback
Definition: api_common.h:379
void vl_msg_api_add_msg_name_crc(api_main_t *am, const char *string, u32 id)
Definition: api_shared.c:1077
int vl_msg_api_rx_trace_enabled(api_main_t *am)
Definition: api_shared.c:62
void(** msg_print_handlers)(void *, void *)
Message print function vector.
Definition: api_common.h:241
int replay_enable
This message can be replayed.
Definition: api_common.h:85
unsigned short u16
Definition: types.h:57
u8 data_len
Definition: ikev2_types.api:24
u32 size
Definition: vhost_user.h:106
int message_bounce
do not free message after processing
Definition: api_common.h:133
u32 curindex
Current index in circular buffer.
Definition: api_common.h:98
static void cleanup(void)
Definition: client.c:101
#define ELOG_DATA(em, f)
Definition: elog.h:484
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
u32 vl_api_string_len(vl_api_string_t *astr)
Definition: api_shared.c:1170
u32 vl_msg_api_max_length(void *mp)
Definition: api_shared.c:912
elog_main_t * elog_main
event log
Definition: api_common.h:374
u8 len
Definition: ip_types.api:103
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
u8 enabled
trace is enabled
Definition: api_common.h:94
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
#define clib_arch_is_big_endian
Definition: byte_order.h:53
svmdb_client_t * c
API trace state.
Definition: api_common.h:91
void vl_msg_api_handler_with_vm_node(api_main_t *am, svm_region_t *vlib_rp, void *the_msg, vlib_main_t *vm, vlib_node_runtime_t *node, u8 is_private)
Definition: api_shared.c:557
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
void vl_msg_api_barrier_release(void)
Definition: api_shared.c:432
vl_api_msg_range_t * msg_ranges
vector of message ranges
Definition: api_common.h:308
void vl_msg_api_post_mortem_dump(void)
Definition: api_shared.c:955
#define clib_warning(format, args...)
Definition: error.h:59
int vl_api_set_elog_trace_api_messages(int enable)
Definition: api_shared.c:1223
char * vl_api_from_api_to_new_c_string(vl_api_string_t *astr)
Definition: api_shared.c:1204
u8 ** traces
Trace ring.
Definition: api_common.h:99
u8 * is_autoendian
Message requires us to do endian conversion.
Definition: api_common.h:253
static void serialize_integer(serialize_main_t *m, u64 x, u32 n_bytes)
Definition: serialize.h:185
blocking call - best used in combination with condvars, for eventfds we don&#39;t yield the cpu ...
Definition: queue.h:42
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:442
const char ** msg_names
Message name vector.
Definition: api_common.h:244
string name[64]
Definition: ip.api:44
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
int replay
is this message to be replayed?
Definition: api_common.h:132
void vl_msg_api_socket_handler(void *the_msg)
Definition: api_shared.c:786
vl_api_trace_t * tx_trace
Sent message trace configuration.
Definition: api_common.h:271
static uword hash_elts(void *v)
Definition: hash.h:118
#define ASSERT(truth)
int vl_api_c_string_to_api_string(const char *buf, vl_api_string_t *str)
Definition: api_shared.c:1149
__thread api_main_t * my_api_main
Definition: api_shared.c:45
int is_autoendian
endian conversion required?
Definition: api_common.h:135
api_main_t api_global_main
Definition: api_shared.c:36
u32 data_len
message length not including header
Definition: api_common.h:142
Message header structure.
Definition: api_common.h:139
vl_api_trace_which_t
Trace RX / TX enum.
Definition: api_common.h:103
u8 * vl_api_serialize_message_table(api_main_t *am, u8 *vector)
Definition: api_shared.c:204
void vl_msg_api_replay_handler(void *the_msg)
Definition: api_shared.c:759
char const int length
Definition: cJSON.h:163
void vl_msg_api_add_version(api_main_t *am, const char *string, u32 major, u32 minor, u32 patch)
Definition: api_shared.c:1095
void vl_msg_api_handler_no_free(void *the_msg)
Definition: api_shared.c:701
void vl_msg_api_free(void *)
void vl_api_set_elog_main(elog_main_t *m)
Definition: api_shared.c:1216
u32 missing_clients
Number of missing clients / failed message sends.
Definition: api_common.h:265
void * vl_msg_push_heap(void)
Definition: api_shared.c:1127
u32 vl_msg_api_get_msg_index(u8 *name_and_crc)
Definition: api_shared.c:1105
void vl_msg_api_increment_missing_client_counter(void)
Definition: api_shared.c:55
u16 first_msg_id
first assigned message ID
Definition: api_common.h:116
void vl_msg_api_register_pd_handler(void *fp, u16 msg_id_host_byte_order)
Definition: api_shared.c:990
void(** msg_endian_handlers)(void *)
Message endian handler vector.
Definition: api_common.h:238
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
u64 uword
Definition: types.h:112
static u32 vl_msg_api_get_msg_length_inline(void *msg_arg)
Definition: api.h:91
int(** pd_msg_handlers)(void *, int)
Plaform-dependent (aka hardware) message handler vector.
Definition: api_common.h:232
__clib_export u32 elog_string(elog_main_t *em, char *fmt,...)
add a string to the event-log string table
Definition: elog.c:582
#define foreach_msg_api_vector
Definition: api_shared.c:796
struct _svm_queue svm_queue_t
option version
Definition: sample.api:19
#define hash_get_mem(h, key)
Definition: hash.h:269
__clib_export void serialize_open_vector(serialize_main_t *m, u8 *vector)
Definition: serialize.c:909
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:1033
u16 first_available_msg_id
First available message ID, for theplugin msg allocator.
Definition: api_common.h:302
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
void vl_msg_pop_heap(void *oldheap)
Definition: api_shared.c:1141
u8 * vl_api_format_string(u8 *s, va_list *args)
Definition: api_shared.c:1176
u8 endian
trace endianness
Definition: api_common.h:93
void(** msg_handlers)(void *)
Message handler vector.
Definition: api_common.h:230
#define clib_call_callbacks(h,...)
call the specified callback set
Definition: callback.h:67
u8 * is_mp_safe
Message is mp safe vector.
Definition: api_common.h:250
void vl_msg_api_queue_handler(svm_queue_t *q)
Definition: api_shared.c:903
void vl_msg_api_trace(api_main_t *am, vl_api_trace_t *tp, void *msg)
Definition: api_shared.c:77
int trace_enable
trace this message
Definition: api_common.h:84
int msg_print_flag
Print every received message.
Definition: api_common.h:274
int is_mp_safe
worker thread barrier required?
Definition: api_common.h:134
int traced
is this message to be traced?
Definition: api_common.h:131
const char * region_name
Shared VM binary API region name.
Definition: api_common.h:356
void * vl_msg_push_heap_w_region(svm_region_t *vlib_rp)
Definition: api_shared.c:1120
Trace configuration for a single message.
Definition: api_common.h:81
#define VL_API_LITTLE_ENDIAN
Definition: api_common.h:109
uword * msg_index_by_name_and_crc
client message index hash table
Definition: api_common.h:350
uword * msg_range_by_name
Message range by name hash.
Definition: api_common.h:305
char * name
the message name
Definition: api_common.h:124
static unsigned char * print(const cJSON *const item, cJSON_bool format, const internal_hooks *const hooks)
Definition: cJSON.c:1181
u8 * vl_api_from_api_to_new_vec(void *mp, vl_api_string_t *astr)
Definition: api_shared.c:1188
u32 nitems
Number of trace records.
Definition: api_common.h:97
int vl_api_vec_to_api_string(const u8 *vec, vl_api_string_t *str)
Definition: api_shared.c:1161
void * handler
the message handler
Definition: api_common.h:126
pthread_mutex_t mutex
Definition: svm_common.h:37
void vl_msg_api_barrier_sync(void)
Definition: api_shared.c:427