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>
35 static api_main_t api_global_main = {
36 .region_name = "/unset",
41 /* Please use vlibapi_get_main() to access my_api_main */
42 __thread api_main_t *my_api_main = &api_global_main;
45 vl_msg_api_set_global_main (void *am_arg)
48 my_api_main = (api_main_t *) am_arg;
52 vl_msg_api_increment_missing_client_counter (void)
54 api_main_t *am = vlibapi_get_main ();
55 am->missing_clients++;
59 vl_msg_api_rx_trace_enabled (api_main_t * am)
61 return (am->rx_trace && am->rx_trace->enabled);
65 vl_msg_api_tx_trace_enabled (api_main_t * am)
67 return (am->tx_trace && am->tx_trace->enabled);
74 vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
80 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
81 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
82 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
84 if (!m || !m->trace_enable)
91 clib_warning ("tp->nitems is 0");
95 if (vec_len (tp->traces) < tp->nitems)
97 vec_add1 (tp->traces, 0);
98 this_trace = tp->traces + vec_len (tp->traces) - 1;
103 old_trace = tp->traces + tp->curindex++;
104 if (tp->curindex == tp->nitems)
106 /* Reuse the trace record, may save some memory allocator traffic */
107 msg_copy = *old_trace;
108 vec_reset_length (msg_copy);
109 this_trace = old_trace;
112 length = clib_net_to_host_u32 (header->data_len);
114 vec_validate (msg_copy, length - 1);
115 clib_memcpy_fast (msg_copy, msg, length);
116 *this_trace = msg_copy;
120 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
128 case VL_API_TRACE_TX:
132 vl_msg_api_trace_configure (am, which, 1024);
137 case VL_API_TRACE_RX:
141 vl_msg_api_trace_configure (am, which, 1024);
152 if (tp == 0 || tp->nitems == 0)
162 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
169 case VL_API_TRACE_TX:
173 case VL_API_TRACE_RX:
183 if (!tp || tp->nitems == 0)
189 for (i = 0; i < vec_len (tp->traces); i++)
191 vec_free (tp->traces[i]);
193 vec_free (tp->traces);
199 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
201 serialize_main_t _sm, *sm = &_sm;
203 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
205 serialize_open_vector (sm, vector);
207 /* serialize the count */
208 serialize_integer (sm, nmsg, sizeof (u32));
210 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
212 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
213 serialize_cstring (sm, (char *) hp->key);
216 return serialize_close_vector (sm);
220 vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
225 u32 msg_length = vec_len (msg);
226 vec_validate (tmpmem, msg_length - 1);
227 clib_memcpy_fast (tmpmem, msg, msg_length);
228 u16 id = clib_net_to_host_u16 (*((u16 *) msg));
229 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
231 if (m && m->endian_handler)
233 m->endian_handler (tmpmem, 1);
236 if (m && m->tojson_handler)
238 cJSON *o = m->tojson_handler (tmpmem);
239 char *s = cJSON_Print (o);
241 tlen = fwrite (s, 1, slen, fp);
247 fformat (stderr, "writing to file error\n");
252 fformat (stderr, " [no registered tojson fn]\n");
257 #define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
264 } vl_msg_write_json_args_t;
267 vl_msg_write_json_fn (u8 *msg, void *ctx)
269 vl_msg_write_json_args_t *arg = ctx;
271 api_main_t *am = vlibapi_get_main ();
272 int rc = vl_msg_api_trace_write_one (am, msg, fp);
276 if (arg->i < arg->n_traces - 1)
277 vl_msg_fwrite (",\n", fp);
283 vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
285 vl_msg_write_json_args_t args;
286 clib_memset (&args, 0, sizeof (args));
288 args.n_traces = vec_len (tp->traces);
289 vl_msg_fwrite ("[\n", fp);
291 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
295 vl_msg_fwrite ("]", fp);
300 vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
308 if (tp->wrapped == 0)
310 for (i = 0; i < vec_len (tp->traces); i++)
312 /*sa_ignore NO_NULL_CHK */
324 /* Wrap case: write oldest -> end of buffer */
325 for (i = tp->curindex; i < vec_len (tp->traces); i++)
335 /* write beginning of buffer -> oldest-1 */
336 for (i = 0; i < tp->curindex; i++)
338 /*sa_ignore NO_NULL_CHK */
352 vl_api_msg_write_fn (u8 *msg, void *ctx)
355 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
356 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
360 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
368 vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
372 vl_api_trace_file_header_t fh;
376 case VL_API_TRACE_TX:
380 case VL_API_TRACE_RX:
389 /* Configured, data present? */
390 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
393 /* "Dare to be stupid" check */
400 return vl_msg_api_trace_write_json (am, tp, fp);
402 /* Write the file header */
403 fh.wrapped = tp->wrapped;
404 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
406 u8 *m = vl_api_serialize_message_table (am, 0);
407 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
409 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
414 /* Write the message table */
415 if (fwrite (m, vec_len (m), 1, fp) != 1)
421 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
425 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
433 case VL_API_TRACE_TX:
437 vec_validate (am->tx_trace, 0);
442 case VL_API_TRACE_RX:
446 vec_validate (am->rx_trace, 0);
459 was_on = vl_msg_api_trace_onoff (am, which, 0);
463 vl_msg_api_trace_free (am, which);
466 clib_memset (tp, 0, sizeof (*tp));
468 if (clib_arch_is_big_endian)
470 tp->endian = VL_API_BIG_ENDIAN;
474 tp->endian = VL_API_LITTLE_ENDIAN;
480 (void) vl_msg_api_trace_onoff (am, which, was_on);
486 vl_msg_api_barrier_sync (void)
491 vl_msg_api_barrier_release (void)
496 msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
497 int trace_it, int do_it, int free_it)
499 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
500 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
502 if (PREDICT_FALSE (am->elog_trace_api_messages))
504 ELOG_TYPE_DECLARE (e) =
506 .format = "api-msg: %s",
513 ed = ELOG_DATA (am->elog_main, e);
515 ed->c = elog_string (am->elog_main, (char *) m->name);
517 ed->c = elog_string (am->elog_main, "BOGUS");
523 vl_msg_api_trace (am, am->rx_trace, the_msg);
525 if (am->msg_print_flag)
527 fformat (stdout, "[%d]: %s\n", id, m->name);
528 fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
532 ASSERT (NULL != m->calc_size_func);
533 if (m->calc_size_func)
535 calc_size = m->calc_size_func (the_msg);
536 if (calc_size > msg_len)
539 "Truncated message '%s' (id %u) received, calculated size "
540 "%lu is bigger than actual size %llu, message dropped.",
541 m->name, id, calc_size, msg_len);
546 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
547 "verify message size is correct",
551 /* don't process message if it's truncated, otherwise byte swaps
552 * and stuff could corrupt memory even beyond message if it's malicious
553 * e.g. VLA length field set to 1M elements, but VLA empty */
554 if (do_it && calc_size <= msg_len)
559 vl_msg_api_barrier_trace_context (am->msg_names[id]);
560 vl_msg_api_barrier_sync ();
563 if (m->is_autoendian)
564 m->endian_handler (the_msg, 0);
566 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
567 clib_call_callbacks (am->perf_counter_cbs, am, id,
570 m->handler (the_msg);
572 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
573 clib_call_callbacks (am->perf_counter_cbs, am, id,
577 vl_msg_api_barrier_release ();
582 clib_warning ("no handler for msg id %d", id);
586 vl_msg_api_free (the_msg);
588 if (PREDICT_FALSE (am->elog_trace_api_messages))
590 ELOG_TYPE_DECLARE (e) =
592 .format = "api-msg-done(%s): %s",
593 .format_args = "t4T4",
607 ed = ELOG_DATA (am->elog_main, e);
610 ed->c = elog_string (am->elog_main, (char *) m->name);
611 ed->barrier = !m->is_mp_safe;
615 ed->c = elog_string (am->elog_main, "BOGUS");
622 vl_msg_api_handler (void *the_msg, uword msg_len)
624 api_main_t *am = vlibapi_get_main ();
626 msg_handler_internal (am, the_msg, msg_len,
627 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
628 1 /* do_it */, 1 /* free_it */);
632 vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
634 api_main_t *am = vlibapi_get_main ();
635 msg_handler_internal (am, the_msg, msg_len,
636 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
637 1 /* do_it */, 0 /* free_it */);
641 vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
643 api_main_t *am = vlibapi_get_main ();
644 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
649 * Add a trace record to the API message trace buffer, if
650 * API message tracing is enabled. Handy for adding sufficient
651 * data to the trace to reproduce autonomous state, as opposed to
652 * state downloaded via control-plane API messages. Example: the NAT
653 * application creates database entries based on packet traffic, not
654 * control-plane messages.
658 vl_msg_api_trace_only (void *the_msg, uword msg_len)
660 api_main_t *am = vlibapi_get_main ();
662 msg_handler_internal (am, the_msg, msg_len,
663 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
664 0 /* do_it */, 0 /* free_it */);
668 vl_msg_api_cleanup_handler (void *the_msg)
670 api_main_t *am = vlibapi_get_main ();
671 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
672 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
674 if (PREDICT_FALSE (!m))
676 clib_warning ("_vl_msg_id too large: %d\n", id);
680 if (m->cleanup_handler)
681 m->cleanup_handler (the_msg);
683 vl_msg_api_free (the_msg);
687 * vl_msg_api_replay_handler
690 vl_msg_api_replay_handler (void *the_msg)
692 api_main_t *am = vlibapi_get_main ();
693 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
694 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
696 if (PREDICT_FALSE (!m))
698 clib_warning ("_vl_msg_id too large: %d\n", id);
701 /* do NOT trace the message... */
703 m->handler (the_msg);
704 /* do NOT free the message buffer... */
708 vl_msg_api_get_msg_length (void *msg_arg)
710 return vl_msg_api_get_msg_length_inline (msg_arg);
714 * vl_msg_api_socket_handler
717 vl_msg_api_socket_handler (void *the_msg, uword msg_len)
719 api_main_t *am = vlibapi_get_main ();
721 msg_handler_internal (am, the_msg, msg_len,
722 (am->rx_trace && am->rx_trace->enabled) /* trace_it */,
723 1 /* do_it */, 0 /* free_it */);
727 vl_msg_api_config (vl_msg_api_msg_config_t * c)
729 api_main_t *am = vlibapi_get_main ();
730 vl_api_msg_data_t *m;
733 * This happens during the java core tests if the message
734 * dictionary is missing newly added xxx_reply_t messages.
735 * Should never happen, but since I shot myself in the foot once
736 * this way, I thought I'd make it easy to debug if I ever do
737 * it again... (;-)...
742 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
744 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
745 clib_warning ("Did you forget to call setup_message_id_table?");
749 vec_validate (am->msg_data, c->id);
750 m = vl_api_get_msg_data (am, c->id);
752 if (m->handler && m->handler != c->handler)
753 clib_warning ("BUG: re-registering 'vl_api_%s_t_handler'."
754 "Handler was %llx, replaced by %llx",
755 c->name, m->handler, c->handler);
758 m->handler = c->handler;
759 m->cleanup_handler = c->cleanup;
760 m->endian_handler = c->endian;
761 m->format_fn = c->format_fn;
762 m->tojson_handler = c->tojson;
763 m->fromjson_handler = c->fromjson;
764 m->calc_size_func = c->calc_size;
765 m->bounce = c->message_bounce;
766 m->is_mp_safe = c->is_mp_safe;
767 m->is_autoendian = c->is_autoendian;
769 m->trace_size = c->size;
770 m->trace_enable = c->traced;
771 m->replay_allowed = c->replay;
773 if (!am->msg_id_by_name)
774 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
776 hash_set_mem (am->msg_id_by_name, c->name, c->id);
780 vl_msg_api_clean_handlers (int msg_id)
782 vl_msg_api_msg_config_t cfg;
783 vl_msg_api_msg_config_t *c = &cfg;
785 clib_memset (c, 0, sizeof (*c));
788 vl_msg_api_config (c);
792 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
794 api_main_t *am = vlibapi_get_main ();
795 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
798 m->cleanup_handler = fp;
802 vl_msg_api_queue_handler (svm_queue_t * q)
806 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
808 VL_MSG_API_UNPOISON ((u8 *) msg);
809 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
810 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
815 vl_msg_api_max_length (void *mp)
820 /* Work out the maximum sane message length, and return it */
821 if (PREDICT_TRUE (mp != 0))
823 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
824 data_len = clib_net_to_host_u32 (mb->data_len);
830 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
834 case VL_API_TRACE_RX:
836 case VL_API_TRACE_TX:
843 static u8 post_mortem_dump_enabled;
846 vl_msg_api_post_mortem_dump_enable_disable (int enable)
848 post_mortem_dump_enabled = enable;
852 vl_msg_api_post_mortem_dump (void)
854 api_main_t *am = vlibapi_get_main ();
859 if (post_mortem_dump_enabled == 0)
862 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
865 fp = fopen (filename, "w");
868 rv = write (2, "Couldn't create ", 16);
869 rv = write (2, filename, strlen (filename));
870 rv = write (2, "\n", 1);
873 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
877 rv = write (2, "Failed to save post-mortem API trace to ", 40);
878 rv = write (2, filename, strlen (filename));
879 rv = write (2, "\n", 1);
884 /* Layered message handling support */
887 vl_msg_api_set_first_available_msg_id (u16 first_avail)
889 api_main_t *am = vlibapi_get_main ();
891 am->first_available_msg_id = first_avail;
895 vl_msg_api_get_msg_ids (const char *name, int n)
897 api_main_t *am = vlibapi_get_main ();
899 vl_api_msg_range_t *rp;
903 if (am->msg_range_by_name == 0)
904 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
906 name_copy = format (0, "%s%c", name, 0);
908 p = hash_get_mem (am->msg_range_by_name, name_copy);
911 clib_warning ("WARNING: duplicate message range registration for '%s'",
913 vec_free (name_copy);
917 if (n < 0 || n > 1024)
920 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
922 vec_free (name_copy);
926 vec_add2 (am->msg_ranges, rp, 1);
928 rv = rp->first_msg_id = am->first_available_msg_id;
929 am->first_available_msg_id += n;
930 rp->last_msg_id = am->first_available_msg_id - 1;
931 rp->name = name_copy;
933 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
939 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
943 if (am->msg_index_by_name_and_crc == 0)
944 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
946 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
949 clib_warning ("attempt to redefine '%s' ignored...", string);
953 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
957 vl_msg_api_add_version (api_main_t * am, const char *string,
958 u32 major, u32 minor, u32 patch)
960 api_version_t version = {.major = major,.minor = minor,.patch = patch };
961 ASSERT (strlen (string) < 64);
962 strncpy (version.name, string, 64 - 1);
963 vec_add1 (am->api_version_list, version);
967 vl_msg_api_get_msg_index (u8 * name_and_crc)
969 api_main_t *am = vlibapi_get_main ();
972 if (am->msg_index_by_name_and_crc)
974 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
982 vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
984 pthread_mutex_lock (&vlib_rp->mutex);
985 return svm_push_data_heap (vlib_rp);
989 vl_msg_push_heap (void)
991 api_main_t *am = vlibapi_get_main ();
992 return vl_msg_push_heap_w_region (am->vlib_rp);
996 vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
998 svm_pop_heap (oldheap);
999 pthread_mutex_unlock (&vlib_rp->mutex);
1003 vl_msg_pop_heap (void *oldheap)
1005 api_main_t *am = vlibapi_get_main ();
1006 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1009 /* Must be nul terminated */
1011 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
1013 /* copy without nul terminator */
1014 u32 len = strlen (buf);
1016 clib_memcpy_fast (str->buf, buf, len);
1017 str->length = htonl (len);
1018 return len + sizeof (u32);
1021 /* Must NOT be nul terminated */
1023 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1025 u32 len = vec_len (vec);
1026 clib_memcpy (str->buf, vec, len);
1027 str->length = htonl (len);
1028 return len + sizeof (u32);
1032 vl_api_string_len (vl_api_string_t * astr)
1034 return clib_net_to_host_u32 (astr->length);
1038 vl_api_format_string (u8 * s, va_list * args)
1040 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1041 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1046 * Returns a new vector. Remember to free it after use.
1047 * NOT nul terminated.
1050 vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
1054 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1055 return format (0, "Invalid astr->length %u > max (%u)%c",
1056 clib_net_to_host_u32 (astr->length),
1057 vl_msg_api_max_length (mp), 0);
1058 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1063 * Returns a new vector. Remember to free it after use.
1067 vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1070 if (clib_net_to_host_u32 (astr->length) > 0)
1072 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1079 vl_api_set_elog_main (elog_main_t * m)
1081 api_main_t *am = vlibapi_get_main ();
1086 vl_api_set_elog_trace_api_messages (int enable)
1089 api_main_t *am = vlibapi_get_main ();
1091 rv = am->elog_trace_api_messages;
1092 am->elog_trace_api_messages = enable;
1097 vl_api_get_elog_trace_api_messages (void)
1099 api_main_t *am = vlibapi_get_main ();
1101 return am->elog_trace_api_messages;
1105 * fd.io coding-style-patch-verification: ON
1108 * eval: (c-set-style "gnu")