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 static api_main_t api_global_main = {
37 .region_name = "/unset",
43 /* Please use vlibapi_get_main() to access my_api_main */
44 __thread api_main_t *my_api_main = &api_global_main;
47 vl_msg_api_set_global_main (void *am_arg)
50 my_api_main = (api_main_t *) am_arg;
54 vl_msg_api_increment_missing_client_counter (void)
56 api_main_t *am = vlibapi_get_main ();
57 am->missing_clients++;
61 vl_msg_api_rx_trace_enabled (api_main_t * am)
63 return (am->rx_trace && am->rx_trace->enabled);
67 vl_msg_api_tx_trace_enabled (api_main_t * am)
69 return (am->tx_trace && am->tx_trace->enabled);
76 vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
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));
86 if (!m || !m->trace_enable)
93 clib_warning ("tp->nitems is 0");
97 if (vec_len (tp->traces) < tp->nitems)
99 vec_add1 (tp->traces, 0);
100 this_trace = tp->traces + vec_len (tp->traces) - 1;
105 old_trace = tp->traces + tp->curindex++;
106 if (tp->curindex == tp->nitems)
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;
114 length = clib_net_to_host_u32 (header->data_len);
116 vec_validate (msg_copy, length - 1);
117 clib_memcpy_fast (msg_copy, msg, length);
118 *this_trace = msg_copy;
122 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
130 case VL_API_TRACE_TX:
134 vl_msg_api_trace_configure (am, which, 1024);
139 case VL_API_TRACE_RX:
143 vl_msg_api_trace_configure (am, which, 1024);
154 if (tp == 0 || tp->nitems == 0)
164 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
171 case VL_API_TRACE_TX:
175 case VL_API_TRACE_RX:
185 if (!tp || tp->nitems == 0)
191 for (i = 0; i < vec_len (tp->traces); i++)
193 vec_free (tp->traces[i]);
195 vec_free (tp->traces);
201 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
203 serialize_main_t _sm, *sm = &_sm;
205 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
207 serialize_open_vector (sm, vector);
209 /* serialize the count */
210 serialize_integer (sm, nmsg, sizeof (u32));
213 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
215 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
216 serialize_cstring (sm, (char *) hp->key);
220 return serialize_close_vector (sm);
224 vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
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);
235 m->endian_handler (tmpmem);
237 if (m && m->tojson_handler)
239 cJSON *o = m->tojson_handler (tmpmem);
240 char *s = cJSON_Print (o);
242 tlen = fwrite (s, 1, slen, fp);
248 fformat (stderr, "writing to file error\n");
253 fformat (stderr, " [no registered tojson fn]\n");
258 #define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
265 } vl_msg_write_json_args_t;
268 vl_msg_write_json_fn (u8 *msg, void *ctx)
270 vl_msg_write_json_args_t *arg = ctx;
272 api_main_t *am = vlibapi_get_main ();
273 int rc = vl_msg_api_trace_write_one (am, msg, fp);
277 if (arg->i < arg->n_traces - 1)
278 vl_msg_fwrite (",\n", fp);
284 vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
286 vl_msg_write_json_args_t args;
287 clib_memset (&args, 0, sizeof (args));
289 args.n_traces = vec_len (tp->traces);
290 vl_msg_fwrite ("[\n", fp);
292 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
296 vl_msg_fwrite ("]", fp);
301 vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
309 if (tp->wrapped == 0)
311 for (i = 0; i < vec_len (tp->traces); i++)
313 /*sa_ignore NO_NULL_CHK */
325 /* Wrap case: write oldest -> end of buffer */
326 for (i = tp->curindex; i < vec_len (tp->traces); i++)
336 /* write beginning of buffer -> oldest-1 */
337 for (i = 0; i < tp->curindex; i++)
339 /*sa_ignore NO_NULL_CHK */
353 vl_api_msg_write_fn (u8 *msg, void *ctx)
356 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
357 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
361 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
369 vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
373 vl_api_trace_file_header_t fh;
377 case VL_API_TRACE_TX:
381 case VL_API_TRACE_RX:
390 /* Configured, data present? */
391 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
394 /* "Dare to be stupid" check */
401 return vl_msg_api_trace_write_json (am, tp, fp);
403 /* Write the file header */
404 fh.wrapped = tp->wrapped;
405 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
407 u8 *m = vl_api_serialize_message_table (am, 0);
408 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
410 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
415 /* Write the message table */
416 if (fwrite (m, vec_len (m), 1, fp) != 1)
422 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
426 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
434 case VL_API_TRACE_TX:
438 vec_validate (am->tx_trace, 0);
443 case VL_API_TRACE_RX:
447 vec_validate (am->rx_trace, 0);
460 was_on = vl_msg_api_trace_onoff (am, which, 0);
464 vl_msg_api_trace_free (am, which);
467 clib_memset (tp, 0, sizeof (*tp));
469 if (clib_arch_is_big_endian)
471 tp->endian = VL_API_BIG_ENDIAN;
475 tp->endian = VL_API_LITTLE_ENDIAN;
481 (void) vl_msg_api_trace_onoff (am, which, was_on);
487 vl_msg_api_barrier_sync (void)
492 vl_msg_api_barrier_release (void)
497 msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
498 int trace_it, int do_it, int free_it)
500 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
501 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
503 if (PREDICT_FALSE (am->elog_trace_api_messages))
506 ELOG_TYPE_DECLARE (e) =
508 .format = "api-msg: %s",
516 ed = ELOG_DATA (am->elog_main, e);
518 ed->c = elog_string (am->elog_main, (char *) m->name);
520 ed->c = elog_string (am->elog_main, "BOGUS");
526 vl_msg_api_trace (am, am->rx_trace, the_msg);
528 if (am->msg_print_flag)
530 fformat (stdout, "[%d]: %s\n", id, m->name);
531 if (m->print_handler)
532 m->print_handler (the_msg, stdout);
534 fformat (stdout, " [no registered print fn]\n");
538 ASSERT (NULL != m->calc_size_func);
539 if (m->calc_size_func)
541 calc_size = m->calc_size_func (the_msg);
542 if (calc_size > msg_len)
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);
552 clib_warning ("Message '%s' (id %u) has NULL calc_size_func, cannot "
553 "verify message size is correct",
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)
565 vl_msg_api_barrier_trace_context (am->msg_names[id]);
566 vl_msg_api_barrier_sync ();
569 if (m->is_autoendian)
570 m->endian_handler (the_msg);
572 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
573 clib_call_callbacks (am->perf_counter_cbs, am, id,
576 m->handler (the_msg);
578 if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
579 clib_call_callbacks (am->perf_counter_cbs, am, id,
583 vl_msg_api_barrier_release ();
588 clib_warning ("no handler for msg id %d", id);
592 vl_msg_api_free (the_msg);
594 if (PREDICT_FALSE (am->elog_trace_api_messages))
597 ELOG_TYPE_DECLARE (e) =
599 .format = "api-msg-done(%s): %s",
600 .format_args = "t4T4",
615 ed = ELOG_DATA (am->elog_main, e);
618 ed->c = elog_string (am->elog_main, (char *) m->name);
619 ed->barrier = !m->is_mp_safe;
623 ed->c = elog_string (am->elog_main, "BOGUS");
630 vl_msg_api_handler (void *the_msg, uword msg_len)
632 api_main_t *am = vlibapi_get_main ();
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 */);
640 vl_msg_api_handler_no_free (void *the_msg, uword msg_len)
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 */);
649 vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len)
651 api_main_t *am = vlibapi_get_main ();
652 msg_handler_internal (am, the_msg, msg_len, 0 /* trace_it */, 1 /* do_it */,
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.
666 vl_msg_api_trace_only (void *the_msg, uword msg_len)
668 api_main_t *am = vlibapi_get_main ();
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 */);
676 vl_msg_api_cleanup_handler (void *the_msg)
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);
682 if (PREDICT_FALSE (!m))
684 clib_warning ("_vl_msg_id too large: %d\n", id);
688 if (m->cleanup_handler)
689 m->cleanup_handler (the_msg);
691 vl_msg_api_free (the_msg);
695 * vl_msg_api_replay_handler
698 vl_msg_api_replay_handler (void *the_msg)
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);
704 if (PREDICT_FALSE (!m))
706 clib_warning ("_vl_msg_id too large: %d\n", id);
709 /* do NOT trace the message... */
711 m->handler (the_msg);
712 /* do NOT free the message buffer... */
716 vl_msg_api_get_msg_length (void *msg_arg)
718 return vl_msg_api_get_msg_length_inline (msg_arg);
722 * vl_msg_api_socket_handler
725 vl_msg_api_socket_handler (void *the_msg, uword msg_len)
727 api_main_t *am = vlibapi_get_main ();
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 */);
735 vl_msg_api_config (vl_msg_api_msg_config_t * c)
737 api_main_t *am = vlibapi_get_main ();
738 vl_api_msg_data_t *m;
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... (;-)...
750 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
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?");
757 vec_validate (am->msg_data, c->id);
758 m = vl_api_get_msg_data (am, c->id);
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);
766 m->handler = c->handler;
767 m->cleanup_handler = c->cleanup;
768 m->endian_handler = c->endian;
769 m->print_handler = c->print;
770 m->print_json_handler = c->print_json;
771 m->tojson_handler = c->tojson;
772 m->fromjson_handler = c->fromjson;
773 m->calc_size_func = c->calc_size;
774 m->bounce = c->message_bounce;
775 m->is_mp_safe = c->is_mp_safe;
776 m->is_autoendian = c->is_autoendian;
778 m->trace_size = c->size;
779 m->trace_enable = c->traced;
780 m->replay_allowed = c->replay;
782 if (!am->msg_id_by_name)
783 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
785 hash_set_mem (am->msg_id_by_name, c->name, c->id);
789 * vl_msg_api_set_handlers
790 * preserve the old API for a while
793 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
794 void *endian, void *print, int size, int traced,
795 void *print_json, void *tojson, void *fromjson,
798 vl_msg_api_msg_config_t cfg;
799 vl_msg_api_msg_config_t *c = &cfg;
801 clib_memset (c, 0, sizeof (*c));
805 c->handler = handler;
806 c->cleanup = cleanup;
811 c->message_bounce = 0;
813 c->is_autoendian = 0;
815 c->fromjson = fromjson;
816 c->print_json = print_json;
817 c->calc_size = calc_size;
818 vl_msg_api_config (c);
822 vl_msg_api_clean_handlers (int msg_id)
824 vl_msg_api_msg_config_t cfg;
825 vl_msg_api_msg_config_t *c = &cfg;
827 clib_memset (c, 0, sizeof (*c));
830 vl_msg_api_config (c);
834 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
836 api_main_t *am = vlibapi_get_main ();
837 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
840 m->cleanup_handler = fp;
844 vl_msg_api_queue_handler (svm_queue_t * q)
848 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
850 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
851 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
856 vl_msg_api_max_length (void *mp)
861 /* Work out the maximum sane message length, and return it */
862 if (PREDICT_TRUE (mp != 0))
864 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
865 data_len = clib_net_to_host_u32 (mb->data_len);
871 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
875 case VL_API_TRACE_RX:
877 case VL_API_TRACE_TX:
885 vl_noop_handler (void *mp)
890 static u8 post_mortem_dump_enabled;
893 vl_msg_api_post_mortem_dump_enable_disable (int enable)
895 post_mortem_dump_enabled = enable;
899 vl_msg_api_post_mortem_dump (void)
901 api_main_t *am = vlibapi_get_main ();
906 if (post_mortem_dump_enabled == 0)
909 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
912 fp = fopen (filename, "w");
915 rv = write (2, "Couldn't create ", 16);
916 rv = write (2, filename, strlen (filename));
917 rv = write (2, "\n", 1);
920 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
924 rv = write (2, "Failed to save post-mortem API trace to ", 40);
925 rv = write (2, filename, strlen (filename));
926 rv = write (2, "\n", 1);
931 /* Layered message handling support */
934 vl_msg_api_set_first_available_msg_id (u16 first_avail)
936 api_main_t *am = vlibapi_get_main ();
938 am->first_available_msg_id = first_avail;
942 vl_msg_api_get_msg_ids (const char *name, int n)
944 api_main_t *am = vlibapi_get_main ();
946 vl_api_msg_range_t *rp;
950 if (am->msg_range_by_name == 0)
951 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
953 name_copy = format (0, "%s%c", name, 0);
955 p = hash_get_mem (am->msg_range_by_name, name_copy);
958 clib_warning ("WARNING: duplicate message range registration for '%s'",
960 vec_free (name_copy);
964 if (n < 0 || n > 1024)
967 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
969 vec_free (name_copy);
973 vec_add2 (am->msg_ranges, rp, 1);
975 rv = rp->first_msg_id = am->first_available_msg_id;
976 am->first_available_msg_id += n;
977 rp->last_msg_id = am->first_available_msg_id - 1;
978 rp->name = name_copy;
980 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
986 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
990 if (am->msg_index_by_name_and_crc == 0)
991 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
993 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
996 clib_warning ("attempt to redefine '%s' ignored...", string);
1000 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1004 vl_msg_api_add_version (api_main_t * am, const char *string,
1005 u32 major, u32 minor, u32 patch)
1007 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1008 ASSERT (strlen (string) < 64);
1009 strncpy (version.name, string, 64 - 1);
1010 vec_add1 (am->api_version_list, version);
1014 vl_msg_api_get_msg_index (u8 * name_and_crc)
1016 api_main_t *am = vlibapi_get_main ();
1019 if (am->msg_index_by_name_and_crc)
1021 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1029 vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
1031 pthread_mutex_lock (&vlib_rp->mutex);
1032 return svm_push_data_heap (vlib_rp);
1036 vl_msg_push_heap (void)
1038 api_main_t *am = vlibapi_get_main ();
1039 return vl_msg_push_heap_w_region (am->vlib_rp);
1043 vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1045 svm_pop_heap (oldheap);
1046 pthread_mutex_unlock (&vlib_rp->mutex);
1050 vl_msg_pop_heap (void *oldheap)
1052 api_main_t *am = vlibapi_get_main ();
1053 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1056 /* Must be nul terminated */
1058 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
1060 /* copy without nul terminator */
1061 u32 len = strlen (buf);
1063 clib_memcpy_fast (str->buf, buf, len);
1064 str->length = htonl (len);
1065 return len + sizeof (u32);
1068 /* Must NOT be nul terminated */
1070 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1072 u32 len = vec_len (vec);
1073 clib_memcpy (str->buf, vec, len);
1074 str->length = htonl (len);
1075 return len + sizeof (u32);
1079 vl_api_string_len (vl_api_string_t * astr)
1081 return clib_net_to_host_u32 (astr->length);
1085 vl_api_format_string (u8 * s, va_list * args)
1087 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1088 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1093 * Returns a new vector. Remember to free it after use.
1094 * NOT nul terminated.
1097 vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
1101 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1102 return format (0, "insane astr->length %u%c",
1103 clib_net_to_host_u32 (astr->length), 0);
1104 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1109 * Returns a new vector. Remember to free it after use.
1113 vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1116 if (clib_net_to_host_u32 (astr->length) > 0)
1118 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1125 vl_api_set_elog_main (elog_main_t * m)
1127 api_main_t *am = vlibapi_get_main ();
1132 vl_api_set_elog_trace_api_messages (int enable)
1135 api_main_t *am = vlibapi_get_main ();
1137 rv = am->elog_trace_api_messages;
1138 am->elog_trace_api_messages = enable;
1143 vl_api_get_elog_trace_api_messages (void)
1145 api_main_t *am = vlibapi_get_main ();
1147 return am->elog_trace_api_messages;
1151 * fd.io coding-style-patch-verification: ON
1154 * eval: (c-set-style "gnu")