c11 safe string handling support
[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
34 /* *INDENT-OFF* */
35 api_main_t api_main =
36   {
37     .region_name = "/unset",
38     .api_uid = -1,
39     .api_gid = -1,
40   };
41 /* *INDENT-ON* */
42
43 void
44 vl_msg_api_increment_missing_client_counter (void)
45 {
46   api_main_t *am = &api_main;
47   am->missing_clients++;
48 }
49
50 int
51 vl_msg_api_rx_trace_enabled (api_main_t * am)
52 {
53   return (am->rx_trace && am->rx_trace->enabled);
54 }
55
56 int
57 vl_msg_api_tx_trace_enabled (api_main_t * am)
58 {
59   return (am->tx_trace && am->tx_trace->enabled);
60 }
61
62 /*
63  * vl_msg_api_trace
64  */
65 void
66 vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
67 {
68   u8 **this_trace;
69   u8 **old_trace;
70   u8 *msg_copy;
71   u32 length;
72   trace_cfg_t *cfgp;
73   u16 msg_id = ntohs (*((u16 *) msg));
74   msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
75
76   cfgp = am->api_trace_cfg + msg_id;
77
78   if (!cfgp || !cfgp->trace_enable)
79     return;
80
81   msg_copy = 0;
82
83   if (tp->nitems == 0)
84     {
85       clib_warning ("tp->nitems is 0");
86       return;
87     }
88
89   if (vec_len (tp->traces) < tp->nitems)
90     {
91       vec_add1 (tp->traces, 0);
92       this_trace = tp->traces + vec_len (tp->traces) - 1;
93     }
94   else
95     {
96       tp->wrapped = 1;
97       old_trace = tp->traces + tp->curindex++;
98       if (tp->curindex == tp->nitems)
99         tp->curindex = 0;
100       vec_free (*old_trace);
101       this_trace = old_trace;
102     }
103
104   length = clib_net_to_host_u32 (header->data_len);
105
106   vec_validate (msg_copy, length - 1);
107   clib_memcpy (msg_copy, msg, length);
108   *this_trace = msg_copy;
109 }
110
111 int
112 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
113                         int onoff)
114 {
115   vl_api_trace_t *tp;
116   int rv;
117
118   switch (which)
119     {
120     case VL_API_TRACE_TX:
121       tp = am->tx_trace;
122       if (tp == 0)
123         {
124           vl_msg_api_trace_configure (am, which, 1024);
125           tp = am->tx_trace;
126         }
127       break;
128
129     case VL_API_TRACE_RX:
130       tp = am->rx_trace;
131       if (tp == 0)
132         {
133           vl_msg_api_trace_configure (am, which, 1024);
134           tp = am->rx_trace;
135         }
136       break;
137
138     default:
139       /* duh? */
140       return -1;
141     }
142
143   /* Configured? */
144   if (tp == 0 || tp->nitems == 0)
145     return -1;
146
147   rv = tp->enabled;
148   tp->enabled = onoff;
149
150   return rv;
151 }
152
153 int
154 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
155 {
156   vl_api_trace_t *tp;
157   int i;
158
159   switch (which)
160     {
161     case VL_API_TRACE_TX:
162       tp = am->tx_trace;
163       break;
164
165     case VL_API_TRACE_RX:
166       tp = am->rx_trace;
167       break;
168
169     default:
170       /* duh? */
171       return -1;
172     }
173
174   /* Configured? */
175   if (!tp || tp->nitems == 0)
176     return -1;
177
178   tp->curindex = 0;
179   tp->wrapped = 0;
180
181   for (i = 0; i < vec_len (tp->traces); i++)
182     {
183       vec_free (tp->traces[i]);
184     }
185   vec_free (tp->traces);
186
187   return 0;
188 }
189
190 int
191 vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp)
192 {
193   vl_api_trace_t *tp;
194   vl_api_trace_file_header_t fh;
195   int i;
196   u8 *msg;
197
198   switch (which)
199     {
200     case VL_API_TRACE_TX:
201       tp = am->tx_trace;
202       break;
203
204     case VL_API_TRACE_RX:
205       tp = am->rx_trace;
206       break;
207
208     default:
209       /* duh? */
210       return -1;
211     }
212
213   /* Configured, data present? */
214   if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
215     return -1;
216
217   /* "Dare to be stupid" check */
218   if (fp == 0)
219     {
220       return -2;
221     }
222
223   /* Write the file header */
224   fh.nitems = vec_len (tp->traces);
225   fh.endian = tp->endian;
226   fh.wrapped = tp->wrapped;
227
228   if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
229     {
230       return (-10);
231     }
232
233   /* No-wrap case */
234   if (tp->wrapped == 0)
235     {
236       /*
237        * Note: vec_len return 0 when fed a NULL pointer.
238        * Unfortunately, the static analysis tool doesn't
239        * figure it out, hence the suppressed warnings.
240        * What a great use of my time.
241        */
242       for (i = 0; i < vec_len (tp->traces); i++)
243         {
244           u32 msg_length;
245           /*sa_ignore NO_NULL_CHK */
246           msg = tp->traces[i];
247           /*
248            * This retarded check required to pass
249            * [sic] SA-checking.
250            */
251           if (!msg)
252             continue;
253
254           msg_length = clib_host_to_net_u32 (vec_len (msg));
255           if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
256               != sizeof (msg_length))
257             {
258               return (-14);
259             }
260           if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
261             {
262               return (-11);
263             }
264         }
265     }
266   else
267     {
268       /* Wrap case: write oldest -> end of buffer */
269       for (i = tp->curindex; i < vec_len (tp->traces); i++)
270         {
271           u32 msg_length;
272           msg = tp->traces[i];
273           /*
274            * This retarded check required to pass
275            * [sic] SA-checking
276            */
277           if (!msg)
278             continue;
279
280           msg_length = clib_host_to_net_u32 (vec_len (msg));
281           if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
282               != sizeof (msg_length))
283             {
284               return (-14);
285             }
286
287           if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
288             {
289               return (-12);
290             }
291         }
292       /* write beginning of buffer -> oldest-1 */
293       for (i = 0; i < tp->curindex; i++)
294         {
295           u32 msg_length;
296           /*sa_ignore NO_NULL_CHK */
297           msg = tp->traces[i];
298           /*
299            * This retarded check required to pass
300            * [sic] SA-checking
301            */
302           if (!msg)
303             continue;
304
305           msg_length = clib_host_to_net_u32 (vec_len (msg));
306           if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
307               != sizeof (msg_length))
308             {
309               return (-14);
310             }
311
312           if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
313             {
314               return (-13);
315             }
316         }
317     }
318   return 0;
319 }
320
321 int
322 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
323                             u32 nitems)
324 {
325   vl_api_trace_t *tp;
326   int was_on = 0;
327
328   switch (which)
329     {
330     case VL_API_TRACE_TX:
331       tp = am->tx_trace;
332       if (tp == 0)
333         {
334           vec_validate (am->tx_trace, 0);
335           tp = am->tx_trace;
336         }
337       break;
338
339     case VL_API_TRACE_RX:
340       tp = am->rx_trace;
341       if (tp == 0)
342         {
343           vec_validate (am->rx_trace, 0);
344           tp = am->rx_trace;
345         }
346
347       break;
348
349     default:
350       return -1;
351
352     }
353
354   if (tp->enabled)
355     {
356       was_on = vl_msg_api_trace_onoff (am, which, 0);
357     }
358   if (tp->traces)
359     {
360       vl_msg_api_trace_free (am, which);
361     }
362
363   clib_memset (tp, 0, sizeof (*tp));
364
365   if (clib_arch_is_big_endian)
366     {
367       tp->endian = VL_API_BIG_ENDIAN;
368     }
369   else
370     {
371       tp->endian = VL_API_LITTLE_ENDIAN;
372     }
373
374   tp->nitems = nitems;
375   if (was_on)
376     {
377       (void) vl_msg_api_trace_onoff (am, which, was_on);
378     }
379   return 0;
380 }
381
382 void
383 vl_msg_api_barrier_sync (void)
384 {
385 }
386
387 void
388 vl_msg_api_barrier_release (void)
389 {
390 }
391
392 always_inline void
393 msg_handler_internal (api_main_t * am,
394                       void *the_msg, int trace_it, int do_it, int free_it)
395 {
396   u16 id = ntohs (*((u16 *) the_msg));
397   u8 *(*print_fp) (void *, void *);
398
399   if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
400     {
401       if (trace_it)
402         vl_msg_api_trace (am, am->rx_trace, the_msg);
403
404       if (am->msg_print_flag)
405         {
406           fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
407           print_fp = (void *) am->msg_print_handlers[id];
408           if (print_fp == 0)
409             {
410               fformat (stdout, "  [no registered print fn]\n");
411             }
412           else
413             {
414               (*print_fp) (the_msg, stdout);
415             }
416         }
417
418       if (do_it)
419         {
420           if (!am->is_mp_safe[id])
421             {
422               vl_msg_api_barrier_trace_context (am->msg_names[id]);
423               vl_msg_api_barrier_sync ();
424             }
425           (*am->msg_handlers[id]) (the_msg);
426           if (!am->is_mp_safe[id])
427             vl_msg_api_barrier_release ();
428         }
429     }
430   else
431     {
432       clib_warning ("no handler for msg id %d", id);
433     }
434
435   if (free_it)
436     vl_msg_api_free (the_msg);
437 }
438
439 static u32
440 elog_id_for_msg_name (vlib_main_t * vm, const char *msg_name)
441 {
442   uword *p, r;
443   static uword *h;
444   u8 *name_copy;
445
446   if (!h)
447     h = hash_create_string (0, sizeof (uword));
448
449   p = hash_get_mem (h, msg_name);
450   if (p)
451     return p[0];
452   r = elog_string (&vm->elog_main, "%s", msg_name);
453
454   name_copy = format (0, "%s%c", msg_name, 0);
455
456   hash_set_mem (h, name_copy, r);
457
458   return r;
459 }
460
461 /* This is only to be called from a vlib/vnet app */
462 void
463 vl_msg_api_handler_with_vm_node (api_main_t * am,
464                                  void *the_msg, vlib_main_t * vm,
465                                  vlib_node_runtime_t * node)
466 {
467   u16 id = ntohs (*((u16 *) the_msg));
468   u8 *(*handler) (void *, void *, void *);
469
470   if (PREDICT_FALSE (vm->elog_trace_api_messages))
471     {
472       /* *INDENT-OFF* */
473       ELOG_TYPE_DECLARE (e) =
474         {
475           .format = "api-msg: %s",
476           .format_args = "T4",
477         };
478       /* *INDENT-ON* */
479       struct
480       {
481         u32 c;
482       } *ed;
483       ed = ELOG_DATA (&vm->elog_main, e);
484       if (id < vec_len (am->msg_names))
485         ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
486       else
487         ed->c = elog_id_for_msg_name (vm, "BOGUS");
488     }
489
490   if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
491     {
492       handler = (void *) am->msg_handlers[id];
493
494       if (am->rx_trace && am->rx_trace->enabled)
495         vl_msg_api_trace (am, am->rx_trace, the_msg);
496
497       if (!am->is_mp_safe[id])
498         {
499           vl_msg_api_barrier_trace_context (am->msg_names[id]);
500           vl_msg_api_barrier_sync ();
501         }
502       (*handler) (the_msg, vm, node);
503       if (!am->is_mp_safe[id])
504         vl_msg_api_barrier_release ();
505     }
506   else
507     {
508       clib_warning ("no handler for msg id %d", id);
509     }
510
511   /*
512    * Special-case, so we can e.g. bounce messages off the vnet
513    * main thread without copying them...
514    */
515   if (!(am->message_bounce[id]))
516     vl_msg_api_free (the_msg);
517
518   if (PREDICT_FALSE (vm->elog_trace_api_messages))
519     {
520       /* *INDENT-OFF* */
521       ELOG_TYPE_DECLARE (e) = {
522         .format = "api-msg-done: %s",
523         .format_args = "T4",
524       };
525       /* *INDENT-ON* */
526
527       struct
528       {
529         u32 c;
530       } *ed;
531       ed = ELOG_DATA (&vm->elog_main, e);
532       if (id < vec_len (am->msg_names))
533         ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
534       else
535         ed->c = elog_id_for_msg_name (vm, "BOGUS");
536     }
537 }
538
539 void
540 vl_msg_api_handler (void *the_msg)
541 {
542   api_main_t *am = &api_main;
543
544   msg_handler_internal (am, the_msg,
545                         (am->rx_trace
546                          && am->rx_trace->enabled) /* trace_it */ ,
547                         1 /* do_it */ , 1 /* free_it */ );
548 }
549
550 void
551 vl_msg_api_handler_no_free (void *the_msg)
552 {
553   api_main_t *am = &api_main;
554   msg_handler_internal (am, the_msg,
555                         (am->rx_trace
556                          && am->rx_trace->enabled) /* trace_it */ ,
557                         1 /* do_it */ , 0 /* free_it */ );
558 }
559
560 void
561 vl_msg_api_handler_no_trace_no_free (void *the_msg)
562 {
563   api_main_t *am = &api_main;
564   msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
565                         0 /* free_it */ );
566 }
567
568 /*
569  * Add a trace record to the API message trace buffer, if
570  * API message tracing is enabled. Handy for adding sufficient
571  * data to the trace to reproduce autonomous state, as opposed to
572  * state downloaded via control-plane API messages. Example: the NAT
573  * application creates database entries based on packet traffic, not
574  * control-plane messages.
575  *
576  */
577 void
578 vl_msg_api_trace_only (void *the_msg)
579 {
580   api_main_t *am = &api_main;
581
582   msg_handler_internal (am, the_msg,
583                         (am->rx_trace
584                          && am->rx_trace->enabled) /* trace_it */ ,
585                         0 /* do_it */ , 0 /* free_it */ );
586 }
587
588 void
589 vl_msg_api_cleanup_handler (void *the_msg)
590 {
591   api_main_t *am = &api_main;
592   u16 id = ntohs (*((u16 *) the_msg));
593
594   if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
595     {
596       clib_warning ("_vl_msg_id too large: %d\n", id);
597       return;
598     }
599   if (am->msg_cleanup_handlers[id])
600     (*am->msg_cleanup_handlers[id]) (the_msg);
601
602   vl_msg_api_free (the_msg);
603 }
604
605 /*
606  * vl_msg_api_replay_handler
607  */
608 void
609 vl_msg_api_replay_handler (void *the_msg)
610 {
611   api_main_t *am = &api_main;
612
613   u16 id = ntohs (*((u16 *) the_msg));
614
615   if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
616     {
617       clib_warning ("_vl_msg_id too large: %d\n", id);
618       return;
619     }
620   /* do NOT trace the message... */
621   if (am->msg_handlers[id])
622     (*am->msg_handlers[id]) (the_msg);
623   /* do NOT free the message buffer... */
624 }
625
626 u32
627 vl_msg_api_get_msg_length (void *msg_arg)
628 {
629   return vl_msg_api_get_msg_length_inline (msg_arg);
630 }
631
632 /*
633  * vl_msg_api_socket_handler
634  */
635 void
636 vl_msg_api_socket_handler (void *the_msg)
637 {
638   api_main_t *am = &api_main;
639
640   msg_handler_internal (am, the_msg,
641                         (am->rx_trace
642                          && am->rx_trace->enabled) /* trace_it */ ,
643                         1 /* do_it */ , 0 /* free_it */ );
644 }
645
646 #define foreach_msg_api_vector                  \
647 _(msg_names)                                    \
648 _(msg_handlers)                                 \
649 _(msg_cleanup_handlers)                         \
650 _(msg_endian_handlers)                          \
651 _(msg_print_handlers)                           \
652 _(api_trace_cfg)                                \
653 _(message_bounce)                               \
654 _(is_mp_safe)
655
656 void
657 vl_msg_api_config (vl_msg_api_msg_config_t * c)
658 {
659   api_main_t *am = &api_main;
660
661   /*
662    * This happens during the java core tests if the message
663    * dictionary is missing newly added xxx_reply_t messages.
664    * Should never happen, but since I shot myself in the foot once
665    * this way, I thought I'd make it easy to debug if I ever do
666    * it again... (;-)...
667    */
668   if (c->id == 0)
669     {
670       if (c->name)
671         clib_warning ("Trying to register %s with a NULL msg id!", c->name);
672       else
673         clib_warning ("Trying to register a NULL msg with a NULL msg id!");
674       clib_warning ("Did you forget to call setup_message_id_table?");
675       return;
676     }
677
678 #define _(a) vec_validate (am->a, c->id);
679   foreach_msg_api_vector;
680 #undef _
681
682   if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
683     clib_warning
684       ("BUG: re-registering 'vl_api_%s_t_handler'."
685        "Handler was %llx, replaced by %llx",
686        c->name, am->msg_handlers[c->id], c->handler);
687
688   am->msg_names[c->id] = c->name;
689   am->msg_handlers[c->id] = c->handler;
690   am->msg_cleanup_handlers[c->id] = c->cleanup;
691   am->msg_endian_handlers[c->id] = c->endian;
692   am->msg_print_handlers[c->id] = c->print;
693   am->message_bounce[c->id] = c->message_bounce;
694   am->is_mp_safe[c->id] = c->is_mp_safe;
695
696   am->api_trace_cfg[c->id].size = c->size;
697   am->api_trace_cfg[c->id].trace_enable = c->traced;
698   am->api_trace_cfg[c->id].replay_enable = c->replay;
699 }
700
701 /*
702  * vl_msg_api_set_handlers
703  * preserve the old API for a while
704  */
705 void
706 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
707                          void *endian, void *print, int size, int traced)
708 {
709   vl_msg_api_msg_config_t cfg;
710   vl_msg_api_msg_config_t *c = &cfg;
711
712   clib_memset (c, 0, sizeof (*c));
713
714   c->id = id;
715   c->name = name;
716   c->handler = handler;
717   c->cleanup = cleanup;
718   c->endian = endian;
719   c->print = print;
720   c->traced = traced;
721   c->replay = 1;
722   c->message_bounce = 0;
723   c->is_mp_safe = 0;
724   vl_msg_api_config (c);
725 }
726
727 void
728 vl_msg_api_clean_handlers (int msg_id)
729 {
730   vl_msg_api_msg_config_t cfg;
731   vl_msg_api_msg_config_t *c = &cfg;
732
733   clib_memset (c, 0, sizeof (*c));
734
735   c->id = msg_id;
736   vl_msg_api_config (c);
737 }
738
739 void
740 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
741 {
742   api_main_t *am = &api_main;
743   ASSERT (msg_id > 0);
744
745   vec_validate (am->msg_cleanup_handlers, msg_id);
746   am->msg_cleanup_handlers[msg_id] = fp;
747 }
748
749 void
750 vl_msg_api_queue_handler (svm_queue_t * q)
751 {
752   uword msg;
753
754   while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
755     vl_msg_api_handler ((void *) msg);
756 }
757
758 vl_api_trace_t *
759 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
760 {
761   switch (which)
762     {
763     case VL_API_TRACE_RX:
764       return am->rx_trace;
765     case VL_API_TRACE_TX:
766       return am->tx_trace;
767     default:
768       return 0;
769     }
770 }
771
772 void
773 vl_noop_handler (void *mp)
774 {
775 }
776
777
778 static u8 post_mortem_dump_enabled;
779
780 void
781 vl_msg_api_post_mortem_dump_enable_disable (int enable)
782 {
783   post_mortem_dump_enabled = enable;
784 }
785
786 void
787 vl_msg_api_post_mortem_dump (void)
788 {
789   api_main_t *am = &api_main;
790   FILE *fp;
791   char filename[64];
792   int rv;
793
794   if (post_mortem_dump_enabled == 0)
795     return;
796
797   snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
798             getpid ());
799
800   fp = fopen (filename, "w");
801   if (fp == NULL)
802     {
803       rv = write (2, "Couldn't create ", 16);
804       rv = write (2, filename, strlen (filename));
805       rv = write (2, "\n", 1);
806       return;
807     }
808   rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
809   fclose (fp);
810   if (rv < 0)
811     {
812       rv = write (2, "Failed to save post-mortem API trace to ", 40);
813       rv = write (2, filename, strlen (filename));
814       rv = write (2, "\n", 1);
815     }
816
817 }
818
819 /* Layered message handling support */
820
821 void
822 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
823 {
824   api_main_t *am = &api_main;
825
826   /* Mild idiot proofing */
827   if (msg_id_host_byte_order > 10000)
828     clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
829                   msg_id_host_byte_order,
830                   clib_net_to_host_u16 (msg_id_host_byte_order));
831   vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
832   am->pd_msg_handlers[msg_id_host_byte_order] = fp;
833 }
834
835 int
836 vl_msg_api_pd_handler (void *mp, int rv)
837 {
838   api_main_t *am = &api_main;
839   int (*fp) (void *, int);
840   u16 msg_id;
841
842   if (clib_arch_is_little_endian)
843     msg_id = clib_net_to_host_u16 (*((u16 *) mp));
844   else
845     msg_id = *((u16 *) mp);
846
847   if (msg_id >= vec_len (am->pd_msg_handlers)
848       || am->pd_msg_handlers[msg_id] == 0)
849     return rv;
850
851   fp = am->pd_msg_handlers[msg_id];
852   rv = (*fp) (mp, rv);
853   return rv;
854 }
855
856 void
857 vl_msg_api_set_first_available_msg_id (u16 first_avail)
858 {
859   api_main_t *am = &api_main;
860
861   am->first_available_msg_id = first_avail;
862 }
863
864 u16
865 vl_msg_api_get_msg_ids (const char *name, int n)
866 {
867   api_main_t *am = &api_main;
868   u8 *name_copy;
869   vl_api_msg_range_t *rp;
870   uword *p;
871   u16 rv;
872
873   if (am->msg_range_by_name == 0)
874     am->msg_range_by_name = hash_create_string (0, sizeof (uword));
875
876   name_copy = format (0, "%s%c", name, 0);
877
878   p = hash_get_mem (am->msg_range_by_name, name_copy);
879   if (p)
880     {
881       clib_warning ("WARNING: duplicate message range registration for '%s'",
882                     name_copy);
883       vec_free (name_copy);
884       return ((u16) ~ 0);
885     }
886
887   if (n < 0 || n > 1024)
888     {
889       clib_warning
890         ("WARNING: bad number of message-IDs (%d) requested by '%s'",
891          n, name_copy);
892       vec_free (name_copy);
893       return ((u16) ~ 0);
894     }
895
896   vec_add2 (am->msg_ranges, rp, 1);
897
898   rv = rp->first_msg_id = am->first_available_msg_id;
899   am->first_available_msg_id += n;
900   rp->last_msg_id = am->first_available_msg_id - 1;
901   rp->name = name_copy;
902
903   hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
904
905   return rv;
906 }
907
908 void
909 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
910 {
911   uword *p;
912
913   if (am->msg_index_by_name_and_crc == 0)
914     am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
915
916   p = hash_get_mem (am->msg_index_by_name_and_crc, string);
917   if (p)
918     {
919       clib_warning ("attempt to redefine '%s' ignored...", string);
920       return;
921     }
922
923   hash_set_mem (am->msg_index_by_name_and_crc, string, id);
924 }
925
926 void
927 vl_msg_api_add_version (api_main_t * am, const char *string,
928                         u32 major, u32 minor, u32 patch)
929 {
930   api_version_t version = {.major = major,.minor = minor,.patch = patch };
931   ASSERT (strlen (string) < 64);
932   strncpy (version.name, string, 64 - 1);
933   vec_add1 (am->api_version_list, version);
934 }
935
936 u32
937 vl_msg_api_get_msg_index (u8 * name_and_crc)
938 {
939   api_main_t *am = &api_main;
940   uword *p;
941
942   if (am->msg_index_by_name_and_crc)
943     {
944       p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
945       if (p)
946         return p[0];
947     }
948   return ~0;
949 }
950
951 void *
952 vl_msg_push_heap (void)
953 {
954   api_main_t *am = &api_main;
955   pthread_mutex_lock (&am->vlib_rp->mutex);
956   return svm_push_data_heap (am->vlib_rp);
957 }
958
959 void
960 vl_msg_pop_heap (void *oldheap)
961 {
962   api_main_t *am = &api_main;
963   svm_pop_heap (oldheap);
964   pthread_mutex_unlock (&am->vlib_rp->mutex);
965 }
966
967
968 /*
969  * fd.io coding-style-patch-verification: ON
970  *
971  * Local Variables:
972  * eval: (c-set-style "gnu")
973  * End:
974  */