2 *------------------------------------------------------------------
3 * api_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
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:
12 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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>
36 api_main_t api_global_main =
38 .region_name = "/unset",
44 /* Please use vlibapi_get_main() to access my_api_main */
45 __thread api_main_t *my_api_main = &api_global_main;
48 vl_msg_api_set_global_main (void *am_arg)
51 my_api_main = (api_main_t *) am_arg;
55 vl_msg_api_increment_missing_client_counter (void)
57 api_main_t *am = vlibapi_get_main ();
58 am->missing_clients++;
62 vl_msg_api_rx_trace_enabled (api_main_t * am)
64 return (am->rx_trace && am->rx_trace->enabled);
68 vl_msg_api_tx_trace_enabled (api_main_t * am)
70 return (am->tx_trace && am->tx_trace->enabled);
77 vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
84 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
85 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
87 cfgp = am->api_trace_cfg + msg_id;
89 if (!cfgp || !cfgp->trace_enable)
96 clib_warning ("tp->nitems is 0");
100 if (vec_len (tp->traces) < tp->nitems)
102 vec_add1 (tp->traces, 0);
103 this_trace = tp->traces + vec_len (tp->traces) - 1;
108 old_trace = tp->traces + tp->curindex++;
109 if (tp->curindex == tp->nitems)
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;
117 length = clib_net_to_host_u32 (header->data_len);
119 vec_validate (msg_copy, length - 1);
120 clib_memcpy_fast (msg_copy, msg, length);
121 *this_trace = msg_copy;
125 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
133 case VL_API_TRACE_TX:
137 vl_msg_api_trace_configure (am, which, 1024);
142 case VL_API_TRACE_RX:
146 vl_msg_api_trace_configure (am, which, 1024);
157 if (tp == 0 || tp->nitems == 0)
167 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
174 case VL_API_TRACE_TX:
178 case VL_API_TRACE_RX:
188 if (!tp || tp->nitems == 0)
194 for (i = 0; i < vec_len (tp->traces); i++)
196 vec_free (tp->traces[i]);
198 vec_free (tp->traces);
204 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
206 serialize_main_t _sm, *sm = &_sm;
208 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
210 serialize_open_vector (sm, vector);
212 /* serialize the count */
213 serialize_integer (sm, nmsg, sizeof (u32));
216 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
218 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
219 serialize_cstring (sm, (char *) hp->key);
223 return serialize_close_vector (sm);
227 vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
231 cJSON *(*tojson_fn) (void *);
233 u32 msg_length = vec_len (msg);
234 vec_validate (tmpmem, msg_length - 1);
235 clib_memcpy_fast (tmpmem, msg, msg_length);
236 u16 id = clib_net_to_host_u16 (*((u16 *) msg));
238 void (*endian_fp) (void *);
239 endian_fp = am->msg_endian_handlers[id];
240 (*endian_fp) (tmpmem);
242 if (id < vec_len (am->msg_tojson_handlers) && am->msg_tojson_handlers[id])
244 tojson_fn = am->msg_tojson_handlers[id];
245 cJSON *o = tojson_fn (tmpmem);
246 char *s = cJSON_Print (o);
248 tlen = fwrite (s, 1, slen, fp);
254 fformat (stderr, "writing to file error\n");
259 fformat (stderr, " [no registered tojson fn]\n");
264 #define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
271 } vl_msg_write_json_args_t;
274 vl_msg_write_json_fn (u8 *msg, void *ctx)
276 vl_msg_write_json_args_t *arg = ctx;
278 api_main_t *am = vlibapi_get_main ();
279 int rc = vl_msg_api_trace_write_one (am, msg, fp);
283 if (arg->i < arg->n_traces - 1)
284 vl_msg_fwrite (",\n", fp);
290 vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
292 vl_msg_write_json_args_t args;
293 clib_memset (&args, 0, sizeof (args));
295 args.n_traces = vec_len (tp->traces);
296 vl_msg_fwrite ("[\n", fp);
298 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
302 vl_msg_fwrite ("]", fp);
307 vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
315 if (tp->wrapped == 0)
317 for (i = 0; i < vec_len (tp->traces); i++)
319 /*sa_ignore NO_NULL_CHK */
331 /* Wrap case: write oldest -> end of buffer */
332 for (i = tp->curindex; i < vec_len (tp->traces); i++)
342 /* write beginning of buffer -> oldest-1 */
343 for (i = 0; i < tp->curindex; i++)
345 /*sa_ignore NO_NULL_CHK */
359 vl_api_msg_write_fn (u8 *msg, void *ctx)
362 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
363 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
367 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
375 vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
379 vl_api_trace_file_header_t fh;
383 case VL_API_TRACE_TX:
387 case VL_API_TRACE_RX:
396 /* Configured, data present? */
397 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
400 /* "Dare to be stupid" check */
407 return vl_msg_api_trace_write_json (am, tp, fp);
409 /* Write the file header */
410 fh.wrapped = tp->wrapped;
411 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
413 u8 *m = vl_api_serialize_message_table (am, 0);
414 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
416 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
421 /* Write the message table */
422 if (fwrite (m, vec_len (m), 1, fp) != 1)
428 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
432 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
440 case VL_API_TRACE_TX:
444 vec_validate (am->tx_trace, 0);
449 case VL_API_TRACE_RX:
453 vec_validate (am->rx_trace, 0);
466 was_on = vl_msg_api_trace_onoff (am, which, 0);
470 vl_msg_api_trace_free (am, which);
473 clib_memset (tp, 0, sizeof (*tp));
475 if (clib_arch_is_big_endian)
477 tp->endian = VL_API_BIG_ENDIAN;
481 tp->endian = VL_API_LITTLE_ENDIAN;
487 (void) vl_msg_api_trace_onoff (am, which, was_on);
493 vl_msg_api_barrier_sync (void)
498 vl_msg_api_barrier_release (void)
503 msg_handler_internal (api_main_t * am,
504 void *the_msg, int trace_it, int do_it, int free_it)
506 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
507 u8 *(*print_fp) (void *, void *);
509 if (PREDICT_FALSE (am->elog_trace_api_messages))
512 ELOG_TYPE_DECLARE (e) =
514 .format = "api-msg: %s",
522 ed = ELOG_DATA (am->elog_main, e);
523 if (id < vec_len (am->msg_names) && am->msg_names[id])
524 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
526 ed->c = elog_string (am->elog_main, "BOGUS");
529 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
532 vl_msg_api_trace (am, am->rx_trace, the_msg);
534 if (am->msg_print_flag)
536 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
537 print_fp = (void *) am->msg_print_handlers[id];
540 fformat (stdout, " [no registered print fn]\n");
544 (*print_fp) (the_msg, stdout);
550 if (!am->is_mp_safe[id])
552 vl_msg_api_barrier_trace_context (am->msg_names[id]);
553 vl_msg_api_barrier_sync ();
556 if (am->is_autoendian[id])
558 void (*endian_fp) (void *);
559 endian_fp = am->msg_endian_handlers[id];
560 (*endian_fp) (the_msg);
563 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
564 clib_call_callbacks (am->perf_counter_cbs, am, id,
567 (*am->msg_handlers[id]) (the_msg);
569 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
570 clib_call_callbacks (am->perf_counter_cbs, am, id,
572 if (!am->is_mp_safe[id])
573 vl_msg_api_barrier_release ();
578 clib_warning ("no handler for msg id %d", id);
582 vl_msg_api_free (the_msg);
584 if (PREDICT_FALSE (am->elog_trace_api_messages))
587 ELOG_TYPE_DECLARE (e) =
589 .format = "api-msg-done(%s): %s",
590 .format_args = "t4T4",
605 ed = ELOG_DATA (am->elog_main, e);
606 if (id < vec_len (am->msg_names) && am->msg_names[id])
608 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
609 ed->barrier = !am->is_mp_safe[id];
613 ed->c = elog_string (am->elog_main, "BOGUS");
619 void (*vl_msg_api_fuzz_hook) (u16, void *);
621 /* This is only to be called from a vlib/vnet app */
623 vl_msg_api_handler_with_vm_node (api_main_t * am, svm_region_t * vlib_rp,
624 void *the_msg, vlib_main_t * vm,
625 vlib_node_runtime_t * node, u8 is_private)
627 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
628 u8 *(*handler) (void *, void *, void *);
629 u8 *(*print_fp) (void *, void *);
630 svm_region_t *old_vlib_rp;
631 void *save_shmem_hdr;
634 if (PREDICT_FALSE (am->elog_trace_api_messages))
637 ELOG_TYPE_DECLARE (e) =
639 .format = "api-msg: %s",
647 ed = ELOG_DATA (am->elog_main, e);
648 if (id < vec_len (am->msg_names) && am->msg_names[id])
649 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
651 ed->c = elog_string (am->elog_main, "BOGUS");
654 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
656 handler = (void *) am->msg_handlers[id];
658 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
659 vl_msg_api_trace (am, am->rx_trace, the_msg);
661 if (PREDICT_FALSE (am->msg_print_flag))
663 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
664 print_fp = (void *) am->msg_print_handlers[id];
667 fformat (stdout, " [no registered print fn for msg %d]\n", id);
671 (*print_fp) (the_msg, vm);
674 is_mp_safe = am->is_mp_safe[id];
678 vl_msg_api_barrier_trace_context (am->msg_names[id]);
679 vl_msg_api_barrier_sync ();
683 old_vlib_rp = am->vlib_rp;
684 save_shmem_hdr = am->shmem_hdr;
685 am->vlib_rp = vlib_rp;
686 am->shmem_hdr = (void *) vlib_rp->user_ctx;
689 if (PREDICT_FALSE (vl_msg_api_fuzz_hook != 0))
690 (*vl_msg_api_fuzz_hook) (id, the_msg);
692 if (am->is_autoendian[id])
694 void (*endian_fp) (void *);
695 endian_fp = am->msg_endian_handlers[id];
696 (*endian_fp) (the_msg);
698 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
699 clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */ );
701 (*handler) (the_msg, vm, node);
703 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
704 clib_call_callbacks (am->perf_counter_cbs, am, id, 1 /* after */ );
707 am->vlib_rp = old_vlib_rp;
708 am->shmem_hdr = save_shmem_hdr;
711 vl_msg_api_barrier_release ();
715 clib_warning ("no handler for msg id %d", id);
719 * Special-case, so we can e.g. bounce messages off the vnet
720 * main thread without copying them...
722 if (id >= vec_len (am->message_bounce) || !(am->message_bounce[id]))
726 old_vlib_rp = am->vlib_rp;
727 save_shmem_hdr = am->shmem_hdr;
728 am->vlib_rp = vlib_rp;
729 am->shmem_hdr = (void *) vlib_rp->user_ctx;
731 vl_msg_api_free (the_msg);
734 am->vlib_rp = old_vlib_rp;
735 am->shmem_hdr = save_shmem_hdr;
739 if (PREDICT_FALSE (am->elog_trace_api_messages))
742 ELOG_TYPE_DECLARE (e) =
744 .format = "api-msg-done(%s): %s",
745 .format_args = "t4T4",
760 ed = ELOG_DATA (am->elog_main, e);
761 if (id < vec_len (am->msg_names) && am->msg_names[id])
762 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
764 ed->c = elog_string (am->elog_main, "BOGUS");
765 ed->barrier = is_mp_safe;
770 vl_msg_api_handler (void *the_msg)
772 api_main_t *am = vlibapi_get_main ();
774 msg_handler_internal (am, the_msg,
776 && am->rx_trace->enabled) /* trace_it */ ,
777 1 /* do_it */ , 1 /* free_it */ );
781 vl_msg_api_handler_no_free (void *the_msg)
783 api_main_t *am = vlibapi_get_main ();
784 msg_handler_internal (am, the_msg,
786 && am->rx_trace->enabled) /* trace_it */ ,
787 1 /* do_it */ , 0 /* free_it */ );
791 vl_msg_api_handler_no_trace_no_free (void *the_msg)
793 api_main_t *am = vlibapi_get_main ();
794 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
799 * Add a trace record to the API message trace buffer, if
800 * API message tracing is enabled. Handy for adding sufficient
801 * data to the trace to reproduce autonomous state, as opposed to
802 * state downloaded via control-plane API messages. Example: the NAT
803 * application creates database entries based on packet traffic, not
804 * control-plane messages.
808 vl_msg_api_trace_only (void *the_msg)
810 api_main_t *am = vlibapi_get_main ();
812 msg_handler_internal (am, the_msg,
814 && am->rx_trace->enabled) /* trace_it */ ,
815 0 /* do_it */ , 0 /* free_it */ );
819 vl_msg_api_cleanup_handler (void *the_msg)
821 api_main_t *am = vlibapi_get_main ();
822 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
824 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
826 clib_warning ("_vl_msg_id too large: %d\n", id);
829 if (am->msg_cleanup_handlers[id])
830 (*am->msg_cleanup_handlers[id]) (the_msg);
832 vl_msg_api_free (the_msg);
836 * vl_msg_api_replay_handler
839 vl_msg_api_replay_handler (void *the_msg)
841 api_main_t *am = vlibapi_get_main ();
843 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
845 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
847 clib_warning ("_vl_msg_id too large: %d\n", id);
850 /* do NOT trace the message... */
851 if (am->msg_handlers[id])
852 (*am->msg_handlers[id]) (the_msg);
853 /* do NOT free the message buffer... */
857 vl_msg_api_get_msg_length (void *msg_arg)
859 return vl_msg_api_get_msg_length_inline (msg_arg);
863 * vl_msg_api_socket_handler
866 vl_msg_api_socket_handler (void *the_msg)
868 api_main_t *am = vlibapi_get_main ();
870 msg_handler_internal (am, the_msg,
872 && am->rx_trace->enabled) /* trace_it */ ,
873 1 /* do_it */ , 0 /* free_it */ );
876 #define foreach_msg_api_vector \
879 _ (msg_cleanup_handlers) \
880 _ (msg_endian_handlers) \
881 _ (msg_print_handlers) \
882 _ (msg_print_json_handlers) \
883 _ (msg_tojson_handlers) \
884 _ (msg_fromjson_handlers) \
891 vl_msg_api_config (vl_msg_api_msg_config_t * c)
893 api_main_t *am = vlibapi_get_main ();
896 * This happens during the java core tests if the message
897 * dictionary is missing newly added xxx_reply_t messages.
898 * Should never happen, but since I shot myself in the foot once
899 * this way, I thought I'd make it easy to debug if I ever do
900 * it again... (;-)...
905 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
907 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
908 clib_warning ("Did you forget to call setup_message_id_table?");
912 #define _(a) vec_validate (am->a, c->id);
913 foreach_msg_api_vector;
916 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
918 ("BUG: re-registering 'vl_api_%s_t_handler'."
919 "Handler was %llx, replaced by %llx",
920 c->name, am->msg_handlers[c->id], c->handler);
922 am->msg_names[c->id] = c->name;
923 am->msg_handlers[c->id] = c->handler;
924 am->msg_cleanup_handlers[c->id] = c->cleanup;
925 am->msg_endian_handlers[c->id] = c->endian;
926 am->msg_print_handlers[c->id] = c->print;
927 am->msg_print_json_handlers[c->id] = c->print_json;
928 am->msg_tojson_handlers[c->id] = c->tojson;
929 am->msg_fromjson_handlers[c->id] = c->fromjson;
930 am->message_bounce[c->id] = c->message_bounce;
931 am->is_mp_safe[c->id] = c->is_mp_safe;
932 am->is_autoendian[c->id] = c->is_autoendian;
934 am->api_trace_cfg[c->id].size = c->size;
935 am->api_trace_cfg[c->id].trace_enable = c->traced;
936 am->api_trace_cfg[c->id].replay_enable = c->replay;
938 if (!am->msg_id_by_name)
939 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
941 hash_set_mem (am->msg_id_by_name, c->name, c->id);
945 * vl_msg_api_set_handlers
946 * preserve the old API for a while
949 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
950 void *endian, void *print, int size, int traced,
951 void *print_json, void *tojson, void *fromjson)
953 vl_msg_api_msg_config_t cfg;
954 vl_msg_api_msg_config_t *c = &cfg;
956 clib_memset (c, 0, sizeof (*c));
960 c->handler = handler;
961 c->cleanup = cleanup;
966 c->message_bounce = 0;
968 c->is_autoendian = 0;
970 c->fromjson = fromjson;
971 c->print_json = print_json;
972 vl_msg_api_config (c);
976 vl_msg_api_clean_handlers (int msg_id)
978 vl_msg_api_msg_config_t cfg;
979 vl_msg_api_msg_config_t *c = &cfg;
981 clib_memset (c, 0, sizeof (*c));
984 vl_msg_api_config (c);
988 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
990 api_main_t *am = vlibapi_get_main ();
993 vec_validate (am->msg_cleanup_handlers, msg_id);
994 am->msg_cleanup_handlers[msg_id] = fp;
998 vl_msg_api_queue_handler (svm_queue_t * q)
1002 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
1003 vl_msg_api_handler ((void *) msg);
1007 vl_msg_api_max_length (void *mp)
1012 /* Work out the maximum sane message length, and return it */
1013 if (PREDICT_TRUE (mp != 0))
1015 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
1016 data_len = clib_net_to_host_u32 (mb->data_len);
1022 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
1026 case VL_API_TRACE_RX:
1027 return am->rx_trace;
1028 case VL_API_TRACE_TX:
1029 return am->tx_trace;
1036 vl_noop_handler (void *mp)
1041 static u8 post_mortem_dump_enabled;
1044 vl_msg_api_post_mortem_dump_enable_disable (int enable)
1046 post_mortem_dump_enabled = enable;
1050 vl_msg_api_post_mortem_dump (void)
1052 api_main_t *am = vlibapi_get_main ();
1057 if (post_mortem_dump_enabled == 0)
1060 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
1063 fp = fopen (filename, "w");
1066 rv = write (2, "Couldn't create ", 16);
1067 rv = write (2, filename, strlen (filename));
1068 rv = write (2, "\n", 1);
1071 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
1075 rv = write (2, "Failed to save post-mortem API trace to ", 40);
1076 rv = write (2, filename, strlen (filename));
1077 rv = write (2, "\n", 1);
1082 /* Layered message handling support */
1085 vl_msg_api_set_first_available_msg_id (u16 first_avail)
1087 api_main_t *am = vlibapi_get_main ();
1089 am->first_available_msg_id = first_avail;
1093 vl_msg_api_get_msg_ids (const char *name, int n)
1095 api_main_t *am = vlibapi_get_main ();
1097 vl_api_msg_range_t *rp;
1101 if (am->msg_range_by_name == 0)
1102 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
1104 name_copy = format (0, "%s%c", name, 0);
1106 p = hash_get_mem (am->msg_range_by_name, name_copy);
1109 clib_warning ("WARNING: duplicate message range registration for '%s'",
1111 vec_free (name_copy);
1115 if (n < 0 || n > 1024)
1118 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
1120 vec_free (name_copy);
1124 vec_add2 (am->msg_ranges, rp, 1);
1126 rv = rp->first_msg_id = am->first_available_msg_id;
1127 am->first_available_msg_id += n;
1128 rp->last_msg_id = am->first_available_msg_id - 1;
1129 rp->name = name_copy;
1131 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
1137 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
1141 if (am->msg_index_by_name_and_crc == 0)
1142 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
1144 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1147 clib_warning ("attempt to redefine '%s' ignored...", string);
1151 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1155 vl_msg_api_add_version (api_main_t * am, const char *string,
1156 u32 major, u32 minor, u32 patch)
1158 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1159 ASSERT (strlen (string) < 64);
1160 strncpy (version.name, string, 64 - 1);
1161 vec_add1 (am->api_version_list, version);
1165 vl_msg_api_get_msg_index (u8 * name_and_crc)
1167 api_main_t *am = vlibapi_get_main ();
1170 if (am->msg_index_by_name_and_crc)
1172 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1180 vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1182 pthread_mutex_lock (&vlib_rp->mutex);
1183 return svm_push_data_heap (vlib_rp);
1187 vl_msg_push_heap (void)
1189 api_main_t *am = vlibapi_get_main ();
1190 return vl_msg_push_heap_w_region (am->vlib_rp);
1194 vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1196 svm_pop_heap (oldheap);
1197 pthread_mutex_unlock (&vlib_rp->mutex);
1201 vl_msg_pop_heap (void *oldheap)
1203 api_main_t *am = vlibapi_get_main ();
1204 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1207 /* Must be nul terminated */
1209 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
1211 /* copy without nul terminator */
1212 u32 len = strlen (buf);
1214 clib_memcpy_fast (str->buf, buf, len);
1215 str->length = htonl (len);
1216 return len + sizeof (u32);
1219 /* Must NOT be nul terminated */
1221 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1223 u32 len = vec_len (vec);
1224 clib_memcpy (str->buf, vec, len);
1225 str->length = htonl (len);
1226 return len + sizeof (u32);
1230 vl_api_string_len (vl_api_string_t * astr)
1232 return clib_net_to_host_u32 (astr->length);
1236 vl_api_format_string (u8 * s, va_list * args)
1238 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1239 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1244 * Returns a new vector. Remember to free it after use.
1245 * NOT nul terminated.
1248 vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
1252 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1253 return format (0, "insane astr->length %u%c",
1254 clib_net_to_host_u32 (astr->length), 0);
1255 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1260 * Returns a new vector. Remember to free it after use.
1264 vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1267 if (clib_net_to_host_u32 (astr->length) > 0)
1269 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1276 vl_api_set_elog_main (elog_main_t * m)
1278 api_main_t *am = vlibapi_get_main ();
1283 vl_api_set_elog_trace_api_messages (int enable)
1286 api_main_t *am = vlibapi_get_main ();
1288 rv = am->elog_trace_api_messages;
1289 am->elog_trace_api_messages = enable;
1294 vl_api_get_elog_trace_api_messages (void)
1296 api_main_t *am = vlibapi_get_main ();
1298 return am->elog_trace_api_messages;
1302 * fd.io coding-style-patch-verification: ON
1305 * eval: (c-set-style "gnu")