api: clean up error message
[vpp.git] / src / vlibapi / api_shared.c
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 static api_main_t api_global_main = {
37   .region_name = "/unset",
38   .api_uid = -1,
39   .api_gid = -1,
40 };
41 /* *INDENT-ON* */
42
43 /* Please use vlibapi_get_main() to access my_api_main */
44 __thread api_main_t *my_api_main = &api_global_main;
45
46 void
47 vl_msg_api_set_global_main (void *am_arg)
48 {
49   ASSERT (am_arg);
50   my_api_main = (api_main_t *) am_arg;
51 }
52
53 void
54 vl_msg_api_increment_missing_client_counter (void)
55 {
56   api_main_t *am = vlibapi_get_main ();
57   am->missing_clients++;
58 }
59
60 int
61 vl_msg_api_rx_trace_enabled (api_main_t * am)
62 {
63   return (am->rx_trace && am->rx_trace->enabled);
64 }
65
66 int
67 vl_msg_api_tx_trace_enabled (api_main_t * am)
68 {
69   return (am->tx_trace && am->tx_trace->enabled);
70 }
71
72 /*
73  * vl_msg_api_trace
74  */
75 void
76 vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
77 {
78   u8 **this_trace;
79   u8 **old_trace;
80   u8 *msg_copy;
81   u32 length;
82   u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
83   vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
84   msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
85
86   if (!m || !m->trace_enable)
87     return;
88
89   msg_copy = 0;
90
91   if (tp->nitems == 0)
92     {
93       clib_warning ("tp->nitems is 0");
94       return;
95     }
96
97   if (vec_len (tp->traces) < tp->nitems)
98     {
99       vec_add1 (tp->traces, 0);
100       this_trace = tp->traces + vec_len (tp->traces) - 1;
101     }
102   else
103     {
104       tp->wrapped = 1;
105       old_trace = tp->traces + tp->curindex++;
106       if (tp->curindex == tp->nitems)
107         tp->curindex = 0;
108       /* Reuse the trace record, may save some memory allocator traffic */
109       msg_copy = *old_trace;
110       vec_reset_length (msg_copy);
111       this_trace = old_trace;
112     }
113
114   length = clib_net_to_host_u32 (header->data_len);
115
116   vec_validate (msg_copy, length - 1);
117   clib_memcpy_fast (msg_copy, msg, length);
118   *this_trace = msg_copy;
119 }
120
121 int
122 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
123                         int onoff)
124 {
125   vl_api_trace_t *tp;
126   int rv;
127
128   switch (which)
129     {
130     case VL_API_TRACE_TX:
131       tp = am->tx_trace;
132       if (tp == 0)
133         {
134           vl_msg_api_trace_configure (am, which, 1024);
135           tp = am->tx_trace;
136         }
137       break;
138
139     case VL_API_TRACE_RX:
140       tp = am->rx_trace;
141       if (tp == 0)
142         {
143           vl_msg_api_trace_configure (am, which, 1024);
144           tp = am->rx_trace;
145         }
146       break;
147
148     default:
149       /* duh? */
150       return -1;
151     }
152
153   /* Configured? */
154   if (tp == 0 || tp->nitems == 0)
155     return -1;
156
157   rv = tp->enabled;
158   tp->enabled = onoff;
159
160   return rv;
161 }
162
163 int
164 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
165 {
166   vl_api_trace_t *tp;
167   int i;
168
169   switch (which)
170     {
171     case VL_API_TRACE_TX:
172       tp = am->tx_trace;
173       break;
174
175     case VL_API_TRACE_RX:
176       tp = am->rx_trace;
177       break;
178
179     default:
180       /* duh? */
181       return -1;
182     }
183
184   /* Configured? */
185   if (!tp || tp->nitems == 0)
186     return -1;
187
188   tp->curindex = 0;
189   tp->wrapped = 0;
190
191   for (i = 0; i < vec_len (tp->traces); i++)
192     {
193       vec_free (tp->traces[i]);
194     }
195   vec_free (tp->traces);
196
197   return 0;
198 }
199
200 u8 *
201 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
202 {
203   serialize_main_t _sm, *sm = &_sm;
204   hash_pair_t *hp;
205   u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
206
207   serialize_open_vector (sm, vector);
208
209   /* serialize the count */
210   serialize_integer (sm, nmsg, sizeof (u32));
211
212   /* *INDENT-OFF* */
213   hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
214   ({
215     serialize_likely_small_unsigned_integer (sm, hp->value[0]);
216     serialize_cstring (sm, (char *) hp->key);
217   }));
218   /* *INDENT-ON* */
219
220   return serialize_close_vector (sm);
221 }
222
223 static int
224 vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
225 {
226   u8 *tmpmem = 0;
227   int tlen, slen;
228
229   u32 msg_length = vec_len (msg);
230   vec_validate (tmpmem, msg_length - 1);
231   clib_memcpy_fast (tmpmem, msg, msg_length);
232   u16 id = clib_net_to_host_u16 (*((u16 *) msg));
233   vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
234
235   if (m && m->endian_handler)
236     {
237       m->endian_handler (tmpmem);
238     }
239
240   if (m && m->tojson_handler)
241     {
242       cJSON *o = m->tojson_handler (tmpmem);
243       char *s = cJSON_Print (o);
244       slen = strlen (s);
245       tlen = fwrite (s, 1, slen, fp);
246       cJSON_free (s);
247       cJSON_Delete (o);
248       vec_free (tmpmem);
249       if (tlen != slen)
250         {
251           fformat (stderr, "writing to file error\n");
252           return -11;
253         }
254     }
255   else
256     fformat (stderr, "  [no registered tojson fn]\n");
257
258   return 0;
259 }
260
261 #define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
262
263 typedef struct
264 {
265   FILE *fp;
266   u32 n_traces;
267   u32 i;
268 } vl_msg_write_json_args_t;
269
270 static int
271 vl_msg_write_json_fn (u8 *msg, void *ctx)
272 {
273   vl_msg_write_json_args_t *arg = ctx;
274   FILE *fp = arg->fp;
275   api_main_t *am = vlibapi_get_main ();
276   int rc = vl_msg_api_trace_write_one (am, msg, fp);
277   if (rc < 0)
278     return rc;
279
280   if (arg->i < arg->n_traces - 1)
281     vl_msg_fwrite (",\n", fp);
282   arg->i++;
283   return 0;
284 }
285
286 static int
287 vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
288 {
289   vl_msg_write_json_args_t args;
290   clib_memset (&args, 0, sizeof (args));
291   args.fp = fp;
292   args.n_traces = vec_len (tp->traces);
293   vl_msg_fwrite ("[\n", fp);
294
295   int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
296   if (rv < 0)
297     return rv;
298
299   vl_msg_fwrite ("]", fp);
300   return 0;
301 }
302
303 int
304 vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
305                        void *ctx)
306 {
307   int i;
308   u8 *msg;
309   int rv = 0;
310
311   /* No-wrap case */
312   if (tp->wrapped == 0)
313     {
314       for (i = 0; i < vec_len (tp->traces); i++)
315         {
316           /*sa_ignore NO_NULL_CHK */
317           msg = tp->traces[i];
318           if (!msg)
319             continue;
320
321           rv = fn (msg, ctx);
322           if (rv < 0)
323             return rv;
324         }
325     }
326   else
327     {
328       /* Wrap case: write oldest -> end of buffer */
329       for (i = tp->curindex; i < vec_len (tp->traces); i++)
330         {
331           msg = tp->traces[i];
332           if (!msg)
333             continue;
334
335           rv = fn (msg, ctx);
336           if (rv < 0)
337             return rv;
338         }
339       /* write beginning of buffer -> oldest-1 */
340       for (i = 0; i < tp->curindex; i++)
341         {
342           /*sa_ignore NO_NULL_CHK */
343           msg = tp->traces[i];
344           if (!msg)
345             continue;
346
347           rv = fn (msg, ctx);
348           if (rv < 0)
349             return rv;
350         }
351     }
352   return 0;
353 }
354
355 static int
356 vl_api_msg_write_fn (u8 *msg, void *ctx)
357 {
358   FILE *fp = ctx;
359   u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
360   if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
361     {
362       return (-14);
363     }
364   if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
365     {
366       return (-14);
367     }
368   return 0;
369 }
370
371 int
372 vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
373                        u8 is_json)
374 {
375   vl_api_trace_t *tp;
376   vl_api_trace_file_header_t fh;
377
378   switch (which)
379     {
380     case VL_API_TRACE_TX:
381       tp = am->tx_trace;
382       break;
383
384     case VL_API_TRACE_RX:
385       tp = am->rx_trace;
386       break;
387
388     default:
389       /* duh? */
390       return -1;
391     }
392
393   /* Configured, data present? */
394   if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
395     return -1;
396
397   /* "Dare to be stupid" check */
398   if (fp == 0)
399     {
400       return -2;
401     }
402
403   if (is_json)
404     return vl_msg_api_trace_write_json (am, tp, fp);
405
406   /* Write the file header */
407   fh.wrapped = tp->wrapped;
408   fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
409
410   u8 *m = vl_api_serialize_message_table (am, 0);
411   fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
412
413   if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
414     {
415       return (-10);
416     }
417
418   /* Write the message table */
419   if (fwrite (m, vec_len (m), 1, fp) != 1)
420     {
421       return (-14);
422     }
423   vec_free (m);
424
425   return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
426 }
427
428 int
429 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
430                             u32 nitems)
431 {
432   vl_api_trace_t *tp;
433   int was_on = 0;
434
435   switch (which)
436     {
437     case VL_API_TRACE_TX:
438       tp = am->tx_trace;
439       if (tp == 0)
440         {
441           vec_validate (am->tx_trace, 0);
442           tp = am->tx_trace;
443         }
444       break;
445
446     case VL_API_TRACE_RX:
447       tp = am->rx_trace;
448       if (tp == 0)
449         {
450           vec_validate (am->rx_trace, 0);
451           tp = am->rx_trace;
452         }
453
454       break;
455
456     default:
457       return -1;
458
459     }
460
461   if (tp->enabled)
462     {
463       was_on = vl_msg_api_trace_onoff (am, which, 0);
464     }
465   if (tp->traces)
466     {
467       vl_msg_api_trace_free (am, which);
468     }
469
470   clib_memset (tp, 0, sizeof (*tp));
471
472   if (clib_arch_is_big_endian)
473     {
474       tp->endian = VL_API_BIG_ENDIAN;
475     }
476   else
477     {
478       tp->endian = VL_API_LITTLE_ENDIAN;
479     }
480
481   tp->nitems = nitems;
482   if (was_on)
483     {
484       (void) vl_msg_api_trace_onoff (am, which, was_on);
485     }
486   return 0;
487 }
488
489 void
490 vl_msg_api_barrier_sync (void)
491 {
492 }
493
494 void
495 vl_msg_api_barrier_release (void)
496 {
497 }
498
499 always_inline void
500 msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
501                       int trace_it, int do_it, int free_it)
502 {
503   u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
504   vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
505
506   if (PREDICT_FALSE (am->elog_trace_api_messages))
507     {
508       /* *INDENT-OFF* */
509       ELOG_TYPE_DECLARE (e) =
510         {
511           .format = "api-msg: %s",
512           .format_args = "T4",
513         };
514       /* *INDENT-ON* */
515       struct
516       {
517         u32 c;
518       } *ed;
519       ed = ELOG_DATA (am->elog_main, e);
520       if (m && m->name)
521         ed->c = elog_string (am->elog_main, (char *) m->name);
522       else
523         ed->c = elog_string (am->elog_main, "BOGUS");
524     }
525
526   if (m && m->handler)
527     {
528       if (trace_it)
529         vl_msg_api_trace (am, am->rx_trace, the_msg);
530
531       if (am->msg_print_flag)
532         {
533           fformat (stdout, "[%d]: %s\n", id, m->name);
534           fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
535         }
536
537       uword calc_size = 0;
538       ASSERT (NULL != m->calc_size_func);
539       if (m->calc_size_func)
540         {
541           calc_size = m->calc_size_func (the_msg);
542           if (calc_size > msg_len)
543             {
544               clib_warning (
545                 "Truncated message '%s' (id %u) received, calculated size "
546                 "%lu is bigger than actual size %llu, message dropped.",
547                 m->name, id, calc_size, msg_len);
548             }
549         }
550       else
551         {
552           clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
553                         "verify message size is correct",
554                         m->name, id);
555         }
556
557       /* don't process message if it's truncated, otherwise byte swaps
558        * and stuff could corrupt memory even beyond message if it's malicious
559        * e.g. VLA length field set to 1M elements, but VLA empty */
560       if (do_it && calc_size <= msg_len)
561         {
562
563           if (!m->is_mp_safe)
564             {
565               vl_msg_api_barrier_trace_context (am->msg_names[id]);
566               vl_msg_api_barrier_sync ();
567             }
568
569           if (m->is_autoendian)
570             m->endian_handler (the_msg);
571
572           if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
573             clib_call_callbacks (am->perf_counter_cbs, am, id,
574                                  0 /* before */ );
575
576           m->handler (the_msg);
577
578           if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
579             clib_call_callbacks (am->perf_counter_cbs, am, id,
580                                  1 /* after */ );
581
582           if (!m->is_mp_safe)
583             vl_msg_api_barrier_release ();
584         }
585     }
586   else
587     {
588       clib_warning ("no handler for msg id %d", id);
589     }
590
591   if (free_it)
592     vl_msg_api_free (the_msg);
593
594   if (PREDICT_FALSE (am->elog_trace_api_messages))
595     {
596       /* *INDENT-OFF* */
597       ELOG_TYPE_DECLARE (e) =
598         {
599           .format = "api-msg-done(%s): %s",
600           .format_args = "t4T4",
601           .n_enum_strings = 2,
602           .enum_strings =
603           {
604             "barrier",
605             "mp-safe",
606           }
607         };
608       /* *INDENT-ON* */
609
610       struct
611       {
612         u32 barrier;
613         u32 c;
614       } *ed;
615       ed = ELOG_DATA (am->elog_main, e);
616       if (m && m->name)
617         {
618           ed->c = elog_string (am->elog_main, (char *) m->name);
619           ed->barrier = !m->is_mp_safe;
620         }
621       else
622         {
623           ed->c = elog_string (am->elog_main, "BOGUS");
624           ed->barrier = 0;
625         }
626     }
627 }
628
629 void
630 vl_msg_api_handler (void *the_msg, uword msg_len)
631 {
632   api_main_t *am = vlibapi_get_main ();
633
634   msg_handler_internal (am, the_msg, msg_len,
635                         (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
636                         1 /* do_it */, 1 /* free_it */);
637 }
638
639 void
640 vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
641 {
642   api_main_t *am = vlibapi_get_main ();
643   msg_handler_internal (am, the_msg, msg_len,
644                         (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
645                         1 /* do_it */, 0 /* free_it */);
646 }
647
648 void
649 vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
650 {
651   api_main_t *am = vlibapi_get_main ();
652   msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
653                         0 /* free_it */);
654 }
655
656 /*
657  * Add a trace record to the API message trace buffer, if
658  * API message tracing is enabled. Handy for adding sufficient
659  * data to the trace to reproduce autonomous state, as opposed to
660  * state downloaded via control-plane API messages. Example: the NAT
661  * application creates database entries based on packet traffic, not
662  * control-plane messages.
663  *
664  */
665 void
666 vl_msg_api_trace_only (void *the_msg, uword msg_len)
667 {
668   api_main_t *am = vlibapi_get_main ();
669
670   msg_handler_internal (am, the_msg, msg_len,
671                         (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
672                         0 /* do_it */, 0 /* free_it */);
673 }
674
675 void
676 vl_msg_api_cleanup_handler (void *the_msg)
677 {
678   api_main_t *am = vlibapi_get_main ();
679   u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
680   vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
681
682   if (PREDICT_FALSE (!m))
683     {
684       clib_warning ("_vl_msg_id too large: %d\n", id);
685       return;
686     }
687
688   if (m->cleanup_handler)
689     m->cleanup_handler (the_msg);
690
691   vl_msg_api_free (the_msg);
692 }
693
694 /*
695  * vl_msg_api_replay_handler
696  */
697 void
698 vl_msg_api_replay_handler (void *the_msg)
699 {
700   api_main_t *am = vlibapi_get_main ();
701   u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
702   vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
703
704   if (PREDICT_FALSE (!m))
705     {
706       clib_warning ("_vl_msg_id too large: %d\n", id);
707       return;
708     }
709   /* do NOT trace the message... */
710   if (m->handler)
711     m->handler (the_msg);
712   /* do NOT free the message buffer... */
713 }
714
715 u32
716 vl_msg_api_get_msg_length (void *msg_arg)
717 {
718   return vl_msg_api_get_msg_length_inline (msg_arg);
719 }
720
721 /*
722  * vl_msg_api_socket_handler
723  */
724 void
725 vl_msg_api_socket_handler (void *the_msg, uword msg_len)
726 {
727   api_main_t *am = vlibapi_get_main ();
728
729   msg_handler_internal (am, the_msg, msg_len,
730                         (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
731                         1 /* do_it */, 0 /* free_it */);
732 }
733
734 void
735 vl_msg_api_config (vl_msg_api_msg_config_t * c)
736 {
737   api_main_t *am = vlibapi_get_main ();
738   vl_api_msg_data_t *m;
739
740   /*
741    * This happens during the java core tests if the message
742    * dictionary is missing newly added xxx_reply_t messages.
743    * Should never happen, but since I shot myself in the foot once
744    * this way, I thought I'd make it easy to debug if I ever do
745    * it again... (;-)...
746    */
747   if (c->id == 0)
748     {
749       if (c->name)
750         clib_warning ("Trying to register %s with a NULL msg id!", c->name);
751       else
752         clib_warning ("Trying to register a NULL msg with a NULL msg id!");
753       clib_warning ("Did you forget to call setup_message_id_table?");
754       return;
755     }
756
757   vec_validate (am->msg_data, c->id);
758   m = vl_api_get_msg_data (am, c->id);
759
760   if (m->handler && m->handler != c->handler)
761     clib_warning ("BUG: re-registering 'vl_api_%s_t_handler'."
762                   "Handler was %llx, replaced by %llx",
763                   c->name, m->handler, c->handler);
764
765   m->name = c->name;
766   m->handler = c->handler;
767   m->cleanup_handler = c->cleanup;
768   m->endian_handler = c->endian;
769   m->format_fn = c->format_fn;
770   m->tojson_handler = c->tojson;
771   m->fromjson_handler = c->fromjson;
772   m->calc_size_func = c->calc_size;
773   m->bounce = c->message_bounce;
774   m->is_mp_safe = c->is_mp_safe;
775   m->is_autoendian = c->is_autoendian;
776
777   m->trace_size = c->size;
778   m->trace_enable = c->traced;
779   m->replay_allowed = c->replay;
780
781   if (!am->msg_id_by_name)
782     am->msg_id_by_name = hash_create_string (0, sizeof (uword));
783
784   hash_set_mem (am->msg_id_by_name, c->name, c->id);
785 }
786
787 void
788 vl_msg_api_clean_handlers (int msg_id)
789 {
790   vl_msg_api_msg_config_t cfg;
791   vl_msg_api_msg_config_t *c = &cfg;
792
793   clib_memset (c, 0, sizeof (*c));
794
795   c->id = msg_id;
796   vl_msg_api_config (c);
797 }
798
799 void
800 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
801 {
802   api_main_t *am = vlibapi_get_main ();
803   vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
804   ASSERT (msg_id > 0);
805
806   m->cleanup_handler = fp;
807 }
808
809 void
810 vl_msg_api_queue_handler (svm_queue_t * q)
811 {
812   uword msg;
813
814   while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
815     {
816       VL_MSG_API_UNPOISON ((u8 *) msg);
817       msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
818       vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
819     }
820 }
821
822 u32
823 vl_msg_api_max_length (void *mp)
824 {
825   msgbuf_t *mb;
826   u32 data_len = ~0;
827
828   /* Work out the maximum sane message length, and return it */
829   if (PREDICT_TRUE (mp != 0))
830     {
831       mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
832       data_len = clib_net_to_host_u32 (mb->data_len);
833     }
834   return data_len;
835 }
836
837 vl_api_trace_t *
838 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
839 {
840   switch (which)
841     {
842     case VL_API_TRACE_RX:
843       return am->rx_trace;
844     case VL_API_TRACE_TX:
845       return am->tx_trace;
846     default:
847       return 0;
848     }
849 }
850
851 static u8 post_mortem_dump_enabled;
852
853 void
854 vl_msg_api_post_mortem_dump_enable_disable (int enable)
855 {
856   post_mortem_dump_enabled = enable;
857 }
858
859 void
860 vl_msg_api_post_mortem_dump (void)
861 {
862   api_main_t *am = vlibapi_get_main ();
863   FILE *fp;
864   char filename[64];
865   int rv;
866
867   if (post_mortem_dump_enabled == 0)
868     return;
869
870   snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
871             getpid ());
872
873   fp = fopen (filename, "w");
874   if (fp == NULL)
875     {
876       rv = write (2, "Couldn't create ", 16);
877       rv = write (2, filename, strlen (filename));
878       rv = write (2, "\n", 1);
879       return;
880     }
881   rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
882   fclose (fp);
883   if (rv < 0)
884     {
885       rv = write (2, "Failed to save post-mortem API trace to ", 40);
886       rv = write (2, filename, strlen (filename));
887       rv = write (2, "\n", 1);
888     }
889
890 }
891
892 /* Layered message handling support */
893
894 void
895 vl_msg_api_set_first_available_msg_id (u16 first_avail)
896 {
897   api_main_t *am = vlibapi_get_main ();
898
899   am->first_available_msg_id = first_avail;
900 }
901
902 u16
903 vl_msg_api_get_msg_ids (const char *name, int n)
904 {
905   api_main_t *am = vlibapi_get_main ();
906   u8 *name_copy;
907   vl_api_msg_range_t *rp;
908   uword *p;
909   u16 rv;
910
911   if (am->msg_range_by_name == 0)
912     am->msg_range_by_name = hash_create_string (0, sizeof (uword));
913
914   name_copy = format (0, "%s%c", name, 0);
915
916   p = hash_get_mem (am->msg_range_by_name, name_copy);
917   if (p)
918     {
919       clib_warning ("WARNING: duplicate message range registration for '%s'",
920                     name_copy);
921       vec_free (name_copy);
922       return ((u16) ~ 0);
923     }
924
925   if (n < 0 || n > 1024)
926     {
927       clib_warning
928         ("WARNING: bad number of message-IDs (%d) requested by '%s'",
929          n, name_copy);
930       vec_free (name_copy);
931       return ((u16) ~ 0);
932     }
933
934   vec_add2 (am->msg_ranges, rp, 1);
935
936   rv = rp->first_msg_id = am->first_available_msg_id;
937   am->first_available_msg_id += n;
938   rp->last_msg_id = am->first_available_msg_id - 1;
939   rp->name = name_copy;
940
941   hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
942
943   return rv;
944 }
945
946 void
947 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
948 {
949   uword *p;
950
951   if (am->msg_index_by_name_and_crc == 0)
952     am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
953
954   p = hash_get_mem (am->msg_index_by_name_and_crc, string);
955   if (p)
956     {
957       clib_warning ("attempt to redefine '%s' ignored...", string);
958       return;
959     }
960
961   hash_set_mem (am->msg_index_by_name_and_crc, string, id);
962 }
963
964 void
965 vl_msg_api_add_version (api_main_t * am, const char *string,
966                         u32 major, u32 minor, u32 patch)
967 {
968   api_version_t version = {.major = major,.minor = minor,.patch = patch };
969   ASSERT (strlen (string) < 64);
970   strncpy (version.name, string, 64 - 1);
971   vec_add1 (am->api_version_list, version);
972 }
973
974 u32
975 vl_msg_api_get_msg_index (u8 * name_and_crc)
976 {
977   api_main_t *am = vlibapi_get_main ();
978   uword *p;
979
980   if (am->msg_index_by_name_and_crc)
981     {
982       p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
983       if (p)
984         return p[0];
985     }
986   return ~0;
987 }
988
989 void *
990 vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
991 {
992   pthread_mutex_lock (&vlib_rp->mutex);
993   return svm_push_data_heap (vlib_rp);
994 }
995
996 void *
997 vl_msg_push_heap (void)
998 {
999   api_main_t *am = vlibapi_get_main ();
1000   return vl_msg_push_heap_w_region (am->vlib_rp);
1001 }
1002
1003 void
1004 vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1005 {
1006   svm_pop_heap (oldheap);
1007   pthread_mutex_unlock (&vlib_rp->mutex);
1008 }
1009
1010 void
1011 vl_msg_pop_heap (void *oldheap)
1012 {
1013   api_main_t *am = vlibapi_get_main ();
1014   vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1015 }
1016
1017 /* Must be nul terminated */
1018 int
1019 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
1020 {
1021   /* copy without nul terminator */
1022   u32 len = strlen (buf);
1023   if (len > 0)
1024     clib_memcpy_fast (str->buf, buf, len);
1025   str->length = htonl (len);
1026   return len + sizeof (u32);
1027 }
1028
1029 /* Must NOT be nul terminated */
1030 int
1031 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1032 {
1033   u32 len = vec_len (vec);
1034   clib_memcpy (str->buf, vec, len);
1035   str->length = htonl (len);
1036   return len + sizeof (u32);
1037 }
1038
1039 u32
1040 vl_api_string_len (vl_api_string_t * astr)
1041 {
1042   return clib_net_to_host_u32 (astr->length);
1043 }
1044
1045 u8 *
1046 vl_api_format_string (u8 * s, va_list * args)
1047 {
1048   vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1049   vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1050   return s;
1051 }
1052
1053 /*
1054  * Returns a new vector. Remember to free it after use.
1055  * NOT nul terminated.
1056  */
1057 u8 *
1058 vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
1059 {
1060   u8 *v = 0;
1061
1062   if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1063     return format (0, "Invalid astr->length %u > max (%u)%c",
1064                    clib_net_to_host_u32 (astr->length),
1065                    vl_msg_api_max_length (mp), 0);
1066   vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1067   return v;
1068 }
1069
1070 /*
1071  * Returns a new vector. Remember to free it after use.
1072  * Nul terminated.
1073  */
1074 char *
1075 vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1076 {
1077   char *v = 0;
1078   if (clib_net_to_host_u32 (astr->length) > 0)
1079     {
1080       vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1081       vec_add1 (v, 0);
1082     }
1083   return v;
1084 }
1085
1086 void
1087 vl_api_set_elog_main (elog_main_t * m)
1088 {
1089   api_main_t *am = vlibapi_get_main ();
1090   am->elog_main = m;
1091 }
1092
1093 int
1094 vl_api_set_elog_trace_api_messages (int enable)
1095 {
1096   int rv;
1097   api_main_t *am = vlibapi_get_main ();
1098
1099   rv = am->elog_trace_api_messages;
1100   am->elog_trace_api_messages = enable;
1101   return rv;
1102 }
1103
1104 int
1105 vl_api_get_elog_trace_api_messages (void)
1106 {
1107   api_main_t *am = vlibapi_get_main ();
1108
1109   return am->elog_trace_api_messages;
1110 }
1111
1112 /*
1113  * fd.io coding-style-patch-verification: ON
1114  *
1115  * Local Variables:
1116  * eval: (c-set-style "gnu")
1117  * End:
1118  */