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, void *the_msg, uword msg_len,
504 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);
549 uword (*calc_size_fp) (void *);
550 calc_size_fp = am->msg_calc_size_funcs[id];
551 ASSERT (NULL != calc_size_fp);
554 calc_size = (*calc_size_fp) (the_msg);
555 ASSERT (calc_size <= msg_len);
556 if (calc_size > msg_len)
559 "Truncated message '%s' (id %u) received, calculated size "
560 "%lu is bigger than actual size %llu, message dropped.",
561 am->msg_names[id], id, calc_size, msg_len);
566 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
567 "verify message size is correct",
568 am->msg_names[id], id);
571 /* don't process message if it's truncated, otherwise byte swaps
572 * and stuff could corrupt memory even beyond message if it's malicious
573 * e.g. VLA length field set to 1M elements, but VLA empty */
574 if (do_it && calc_size <= msg_len)
577 if (!am->is_mp_safe[id])
579 vl_msg_api_barrier_trace_context (am->msg_names[id]);
580 vl_msg_api_barrier_sync ();
583 if (am->is_autoendian[id])
585 void (*endian_fp) (void *);
586 endian_fp = am->msg_endian_handlers[id];
587 (*endian_fp) (the_msg);
590 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
591 clib_call_callbacks (am->perf_counter_cbs, am, id,
594 (*am->msg_handlers[id]) (the_msg);
596 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
597 clib_call_callbacks (am->perf_counter_cbs, am, id,
600 if (!am->is_mp_safe[id])
601 vl_msg_api_barrier_release ();
606 clib_warning ("no handler for msg id %d", id);
610 vl_msg_api_free (the_msg);
612 if (PREDICT_FALSE (am->elog_trace_api_messages))
615 ELOG_TYPE_DECLARE (e) =
617 .format = "api-msg-done(%s): %s",
618 .format_args = "t4T4",
633 ed = ELOG_DATA (am->elog_main, e);
634 if (id < vec_len (am->msg_names) && am->msg_names[id])
636 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
637 ed->barrier = !am->is_mp_safe[id];
641 ed->c = elog_string (am->elog_main, "BOGUS");
647 void (*vl_msg_api_fuzz_hook) (u16, void *);
649 /* This is only to be called from a vlib/vnet app */
651 vl_msg_api_handler_with_vm_node (api_main_t * am, svm_region_t * vlib_rp,
652 void *the_msg, vlib_main_t * vm,
653 vlib_node_runtime_t * node, u8 is_private)
655 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
656 u8 *(*handler) (void *, void *, void *);
657 u8 *(*print_fp) (void *, void *);
658 svm_region_t *old_vlib_rp;
659 void *save_shmem_hdr;
662 if (PREDICT_FALSE (am->elog_trace_api_messages))
665 ELOG_TYPE_DECLARE (e) =
667 .format = "api-msg: %s",
675 ed = ELOG_DATA (am->elog_main, e);
676 if (id < vec_len (am->msg_names) && am->msg_names[id])
677 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
679 ed->c = elog_string (am->elog_main, "BOGUS");
682 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
684 handler = (void *) am->msg_handlers[id];
686 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
687 vl_msg_api_trace (am, am->rx_trace, the_msg);
689 if (PREDICT_FALSE (am->msg_print_flag))
691 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
692 print_fp = (void *) am->msg_print_handlers[id];
695 fformat (stdout, " [no registered print fn for msg %d]\n", id);
699 (*print_fp) (the_msg, vm);
702 is_mp_safe = am->is_mp_safe[id];
706 vl_msg_api_barrier_trace_context (am->msg_names[id]);
707 vl_msg_api_barrier_sync ();
711 old_vlib_rp = am->vlib_rp;
712 save_shmem_hdr = am->shmem_hdr;
713 am->vlib_rp = vlib_rp;
714 am->shmem_hdr = (void *) vlib_rp->user_ctx;
717 if (PREDICT_FALSE (vl_msg_api_fuzz_hook != 0))
718 (*vl_msg_api_fuzz_hook) (id, the_msg);
720 if (am->is_autoendian[id])
722 void (*endian_fp) (void *);
723 endian_fp = am->msg_endian_handlers[id];
724 (*endian_fp) (the_msg);
726 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
727 clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */ );
729 (*handler) (the_msg, vm, node);
731 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
732 clib_call_callbacks (am->perf_counter_cbs, am, id, 1 /* after */ );
735 am->vlib_rp = old_vlib_rp;
736 am->shmem_hdr = save_shmem_hdr;
739 vl_msg_api_barrier_release ();
743 clib_warning ("no handler for msg id %d", id);
747 * Special-case, so we can e.g. bounce messages off the vnet
748 * main thread without copying them...
750 if (id >= vec_len (am->message_bounce) || !(am->message_bounce[id]))
754 old_vlib_rp = am->vlib_rp;
755 save_shmem_hdr = am->shmem_hdr;
756 am->vlib_rp = vlib_rp;
757 am->shmem_hdr = (void *) vlib_rp->user_ctx;
759 vl_msg_api_free (the_msg);
762 am->vlib_rp = old_vlib_rp;
763 am->shmem_hdr = save_shmem_hdr;
767 if (PREDICT_FALSE (am->elog_trace_api_messages))
770 ELOG_TYPE_DECLARE (e) =
772 .format = "api-msg-done(%s): %s",
773 .format_args = "t4T4",
788 ed = ELOG_DATA (am->elog_main, e);
789 if (id < vec_len (am->msg_names) && am->msg_names[id])
790 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
792 ed->c = elog_string (am->elog_main, "BOGUS");
793 ed->barrier = is_mp_safe;
798 vl_msg_api_handler (void *the_msg, uword msg_len)
800 api_main_t *am = vlibapi_get_main ();
802 msg_handler_internal (am, the_msg, msg_len,
803 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
804 1 /* do_it */, 1 /* free_it */);
808 vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
810 api_main_t *am = vlibapi_get_main ();
811 msg_handler_internal (am, the_msg, msg_len,
812 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
813 1 /* do_it */, 0 /* free_it */);
817 vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
819 api_main_t *am = vlibapi_get_main ();
820 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
825 * Add a trace record to the API message trace buffer, if
826 * API message tracing is enabled. Handy for adding sufficient
827 * data to the trace to reproduce autonomous state, as opposed to
828 * state downloaded via control-plane API messages. Example: the NAT
829 * application creates database entries based on packet traffic, not
830 * control-plane messages.
834 vl_msg_api_trace_only (void *the_msg, uword msg_len)
836 api_main_t *am = vlibapi_get_main ();
838 msg_handler_internal (am, the_msg, msg_len,
839 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
840 0 /* do_it */, 0 /* free_it */);
844 vl_msg_api_cleanup_handler (void *the_msg)
846 api_main_t *am = vlibapi_get_main ();
847 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
849 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
851 clib_warning ("_vl_msg_id too large: %d\n", id);
854 if (am->msg_cleanup_handlers[id])
855 (*am->msg_cleanup_handlers[id]) (the_msg);
857 vl_msg_api_free (the_msg);
861 * vl_msg_api_replay_handler
864 vl_msg_api_replay_handler (void *the_msg)
866 api_main_t *am = vlibapi_get_main ();
868 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
870 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
872 clib_warning ("_vl_msg_id too large: %d\n", id);
875 /* do NOT trace the message... */
876 if (am->msg_handlers[id])
877 (*am->msg_handlers[id]) (the_msg);
878 /* do NOT free the message buffer... */
882 vl_msg_api_get_msg_length (void *msg_arg)
884 return vl_msg_api_get_msg_length_inline (msg_arg);
888 * vl_msg_api_socket_handler
891 vl_msg_api_socket_handler (void *the_msg, uword msg_len)
893 api_main_t *am = vlibapi_get_main ();
895 msg_handler_internal (am, the_msg, msg_len,
896 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
897 1 /* do_it */, 0 /* free_it */);
900 #define foreach_msg_api_vector \
903 _ (msg_cleanup_handlers) \
904 _ (msg_endian_handlers) \
905 _ (msg_print_handlers) \
906 _ (msg_print_json_handlers) \
907 _ (msg_tojson_handlers) \
908 _ (msg_fromjson_handlers) \
909 _ (msg_calc_size_funcs) \
916 vl_msg_api_config (vl_msg_api_msg_config_t * c)
918 api_main_t *am = vlibapi_get_main ();
921 * This happens during the java core tests if the message
922 * dictionary is missing newly added xxx_reply_t messages.
923 * Should never happen, but since I shot myself in the foot once
924 * this way, I thought I'd make it easy to debug if I ever do
925 * it again... (;-)...
930 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
932 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
933 clib_warning ("Did you forget to call setup_message_id_table?");
937 #define _(a) vec_validate (am->a, c->id);
938 foreach_msg_api_vector;
941 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
943 ("BUG: re-registering 'vl_api_%s_t_handler'."
944 "Handler was %llx, replaced by %llx",
945 c->name, am->msg_handlers[c->id], c->handler);
947 am->msg_names[c->id] = c->name;
948 am->msg_handlers[c->id] = c->handler;
949 am->msg_cleanup_handlers[c->id] = c->cleanup;
950 am->msg_endian_handlers[c->id] = c->endian;
951 am->msg_print_handlers[c->id] = c->print;
952 am->msg_print_json_handlers[c->id] = c->print_json;
953 am->msg_tojson_handlers[c->id] = c->tojson;
954 am->msg_fromjson_handlers[c->id] = c->fromjson;
955 am->msg_calc_size_funcs[c->id] = c->calc_size;
956 am->message_bounce[c->id] = c->message_bounce;
957 am->is_mp_safe[c->id] = c->is_mp_safe;
958 am->is_autoendian[c->id] = c->is_autoendian;
960 am->api_trace_cfg[c->id].size = c->size;
961 am->api_trace_cfg[c->id].trace_enable = c->traced;
962 am->api_trace_cfg[c->id].replay_enable = c->replay;
964 if (!am->msg_id_by_name)
965 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
967 hash_set_mem (am->msg_id_by_name, c->name, c->id);
971 * vl_msg_api_set_handlers
972 * preserve the old API for a while
975 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
976 void *endian, void *print, int size, int traced,
977 void *print_json, void *tojson, void *fromjson,
980 vl_msg_api_msg_config_t cfg;
981 vl_msg_api_msg_config_t *c = &cfg;
983 clib_memset (c, 0, sizeof (*c));
987 c->handler = handler;
988 c->cleanup = cleanup;
993 c->message_bounce = 0;
995 c->is_autoendian = 0;
997 c->fromjson = fromjson;
998 c->print_json = print_json;
999 c->calc_size = calc_size;
1000 vl_msg_api_config (c);
1004 vl_msg_api_clean_handlers (int msg_id)
1006 vl_msg_api_msg_config_t cfg;
1007 vl_msg_api_msg_config_t *c = &cfg;
1009 clib_memset (c, 0, sizeof (*c));
1012 vl_msg_api_config (c);
1016 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
1018 api_main_t *am = vlibapi_get_main ();
1019 ASSERT (msg_id > 0);
1021 vec_validate (am->msg_cleanup_handlers, msg_id);
1022 am->msg_cleanup_handlers[msg_id] = fp;
1026 vl_msg_api_queue_handler (svm_queue_t * q)
1030 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
1032 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
1033 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
1038 vl_msg_api_max_length (void *mp)
1043 /* Work out the maximum sane message length, and return it */
1044 if (PREDICT_TRUE (mp != 0))
1046 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
1047 data_len = clib_net_to_host_u32 (mb->data_len);
1053 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
1057 case VL_API_TRACE_RX:
1058 return am->rx_trace;
1059 case VL_API_TRACE_TX:
1060 return am->tx_trace;
1067 vl_noop_handler (void *mp)
1072 static u8 post_mortem_dump_enabled;
1075 vl_msg_api_post_mortem_dump_enable_disable (int enable)
1077 post_mortem_dump_enabled = enable;
1081 vl_msg_api_post_mortem_dump (void)
1083 api_main_t *am = vlibapi_get_main ();
1088 if (post_mortem_dump_enabled == 0)
1091 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
1094 fp = fopen (filename, "w");
1097 rv = write (2, "Couldn't create ", 16);
1098 rv = write (2, filename, strlen (filename));
1099 rv = write (2, "\n", 1);
1102 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
1106 rv = write (2, "Failed to save post-mortem API trace to ", 40);
1107 rv = write (2, filename, strlen (filename));
1108 rv = write (2, "\n", 1);
1113 /* Layered message handling support */
1116 vl_msg_api_set_first_available_msg_id (u16 first_avail)
1118 api_main_t *am = vlibapi_get_main ();
1120 am->first_available_msg_id = first_avail;
1124 vl_msg_api_get_msg_ids (const char *name, int n)
1126 api_main_t *am = vlibapi_get_main ();
1128 vl_api_msg_range_t *rp;
1132 if (am->msg_range_by_name == 0)
1133 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
1135 name_copy = format (0, "%s%c", name, 0);
1137 p = hash_get_mem (am->msg_range_by_name, name_copy);
1140 clib_warning ("WARNING: duplicate message range registration for '%s'",
1142 vec_free (name_copy);
1146 if (n < 0 || n > 1024)
1149 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
1151 vec_free (name_copy);
1155 vec_add2 (am->msg_ranges, rp, 1);
1157 rv = rp->first_msg_id = am->first_available_msg_id;
1158 am->first_available_msg_id += n;
1159 rp->last_msg_id = am->first_available_msg_id - 1;
1160 rp->name = name_copy;
1162 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
1168 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
1172 if (am->msg_index_by_name_and_crc == 0)
1173 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
1175 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1178 clib_warning ("attempt to redefine '%s' ignored...", string);
1182 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1186 vl_msg_api_add_version (api_main_t * am, const char *string,
1187 u32 major, u32 minor, u32 patch)
1189 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1190 ASSERT (strlen (string) < 64);
1191 strncpy (version.name, string, 64 - 1);
1192 vec_add1 (am->api_version_list, version);
1196 vl_msg_api_get_msg_index (u8 * name_and_crc)
1198 api_main_t *am = vlibapi_get_main ();
1201 if (am->msg_index_by_name_and_crc)
1203 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1211 vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1213 pthread_mutex_lock (&vlib_rp->mutex);
1214 return svm_push_data_heap (vlib_rp);
1218 vl_msg_push_heap (void)
1220 api_main_t *am = vlibapi_get_main ();
1221 return vl_msg_push_heap_w_region (am->vlib_rp);
1225 vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1227 svm_pop_heap (oldheap);
1228 pthread_mutex_unlock (&vlib_rp->mutex);
1232 vl_msg_pop_heap (void *oldheap)
1234 api_main_t *am = vlibapi_get_main ();
1235 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1238 /* Must be nul terminated */
1240 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
1242 /* copy without nul terminator */
1243 u32 len = strlen (buf);
1245 clib_memcpy_fast (str->buf, buf, len);
1246 str->length = htonl (len);
1247 return len + sizeof (u32);
1250 /* Must NOT be nul terminated */
1252 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1254 u32 len = vec_len (vec);
1255 clib_memcpy (str->buf, vec, len);
1256 str->length = htonl (len);
1257 return len + sizeof (u32);
1261 vl_api_string_len (vl_api_string_t * astr)
1263 return clib_net_to_host_u32 (astr->length);
1267 vl_api_format_string (u8 * s, va_list * args)
1269 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1270 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1275 * Returns a new vector. Remember to free it after use.
1276 * NOT nul terminated.
1279 vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
1283 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1284 return format (0, "insane astr->length %u%c",
1285 clib_net_to_host_u32 (astr->length), 0);
1286 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1291 * Returns a new vector. Remember to free it after use.
1295 vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1298 if (clib_net_to_host_u32 (astr->length) > 0)
1300 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1307 vl_api_set_elog_main (elog_main_t * m)
1309 api_main_t *am = vlibapi_get_main ();
1314 vl_api_set_elog_trace_api_messages (int enable)
1317 api_main_t *am = vlibapi_get_main ();
1319 rv = am->elog_trace_api_messages;
1320 am->elog_trace_api_messages = enable;
1325 vl_api_get_elog_trace_api_messages (void)
1327 api_main_t *am = vlibapi_get_main ();
1329 return am->elog_trace_api_messages;
1333 * fd.io coding-style-patch-verification: ON
1336 * eval: (c-set-style "gnu")