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 if (m && m->endian_handler)
237 m->endian_handler (tmpmem);
240 if (m && m->tojson_handler)
242 cJSON *o = m->tojson_handler (tmpmem);
243 char *s = cJSON_Print (o);
245 tlen = fwrite (s, 1, slen, fp);
251 fformat (stderr, "writing to file error\n");
256 fformat (stderr, " [no registered tojson fn]\n");
261 #define vl_msg_fwrite(_s, _f) fwrite (_s, 1, sizeof (_s) - 1, _f)
268 } vl_msg_write_json_args_t;
271 vl_msg_write_json_fn (u8 *msg, void *ctx)
273 vl_msg_write_json_args_t *arg = ctx;
275 api_main_t *am = vlibapi_get_main ();
276 int rc = vl_msg_api_trace_write_one (am, msg, fp);
280 if (arg->i < arg->n_traces - 1)
281 vl_msg_fwrite (",\n", fp);
287 vl_msg_api_trace_write_json (api_main_t *am, vl_api_trace_t *tp, FILE *fp)
289 vl_msg_write_json_args_t args;
290 clib_memset (&args, 0, sizeof (args));
292 args.n_traces = vec_len (tp->traces);
293 vl_msg_fwrite ("[\n", fp);
295 int rv = vl_msg_traverse_trace (tp, vl_msg_write_json_fn, &args);
299 vl_msg_fwrite ("]", fp);
304 vl_msg_traverse_trace (vl_api_trace_t *tp, vl_msg_traverse_trace_fn fn,
312 if (tp->wrapped == 0)
314 for (i = 0; i < vec_len (tp->traces); i++)
316 /*sa_ignore NO_NULL_CHK */
328 /* Wrap case: write oldest -> end of buffer */
329 for (i = tp->curindex; i < vec_len (tp->traces); i++)
339 /* write beginning of buffer -> oldest-1 */
340 for (i = 0; i < tp->curindex; i++)
342 /*sa_ignore NO_NULL_CHK */
356 vl_api_msg_write_fn (u8 *msg, void *ctx)
359 u32 msg_length = clib_host_to_net_u32 (vec_len (msg));
360 if (fwrite (&msg_length, 1, sizeof (msg_length), fp) != sizeof (msg_length))
364 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
372 vl_msg_api_trace_save (api_main_t *am, vl_api_trace_which_t which, FILE *fp,
376 vl_api_trace_file_header_t fh;
380 case VL_API_TRACE_TX:
384 case VL_API_TRACE_RX:
393 /* Configured, data present? */
394 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
397 /* "Dare to be stupid" check */
404 return vl_msg_api_trace_write_json (am, tp, fp);
406 /* Write the file header */
407 fh.wrapped = tp->wrapped;
408 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
410 u8 *m = vl_api_serialize_message_table (am, 0);
411 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
413 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
418 /* Write the message table */
419 if (fwrite (m, vec_len (m), 1, fp) != 1)
425 return vl_msg_traverse_trace (tp, vl_api_msg_write_fn, fp);
429 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
437 case VL_API_TRACE_TX:
441 vec_validate (am->tx_trace, 0);
446 case VL_API_TRACE_RX:
450 vec_validate (am->rx_trace, 0);
463 was_on = vl_msg_api_trace_onoff (am, which, 0);
467 vl_msg_api_trace_free (am, which);
470 clib_memset (tp, 0, sizeof (*tp));
472 if (clib_arch_is_big_endian)
474 tp->endian = VL_API_BIG_ENDIAN;
478 tp->endian = VL_API_LITTLE_ENDIAN;
484 (void) vl_msg_api_trace_onoff (am, which, was_on);
490 vl_msg_api_barrier_sync (void)
495 vl_msg_api_barrier_release (void)
500 msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
501 int trace_it, int do_it, int free_it)
503 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
504 vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
506 if (PREDICT_FALSE (am->elog_trace_api_messages))
509 ELOG_TYPE_DECLARE (e) =
511 .format = "api-msg: %s",
519 ed = ELOG_DATA (am->elog_main, e);
521 ed->c = elog_string (am->elog_main, (char *) m->name);
523 ed->c = elog_string (am->elog_main, "BOGUS");
529 vl_msg_api_trace (am, am->rx_trace, the_msg);
531 if (am->msg_print_flag)
533 fformat (stdout, "[%d]: %s\n", id, m->name);
534 fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
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->format_fn = c->format_fn;
770 m->tojson_handler = c->tojson;
771 m->fromjson_handler = c->fromjson;
772 m->calc_size_func = c->calc_size;
773 m->bounce = c->message_bounce;
774 m->is_mp_safe = c->is_mp_safe;
775 m->is_autoendian = c->is_autoendian;
777 m->trace_size = c->size;
778 m->trace_enable = c->traced;
779 m->replay_allowed = c->replay;
781 if (!am->msg_id_by_name)
782 am->msg_id_by_name = hash_create_string (0, sizeof (uword));
784 hash_set_mem (am->msg_id_by_name, c->name, c->id);
788 vl_msg_api_clean_handlers (int msg_id)
790 vl_msg_api_msg_config_t cfg;
791 vl_msg_api_msg_config_t *c = &cfg;
793 clib_memset (c, 0, sizeof (*c));
796 vl_msg_api_config (c);
800 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
802 api_main_t *am = vlibapi_get_main ();
803 vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
806 m->cleanup_handler = fp;
810 vl_msg_api_queue_handler (svm_queue_t * q)
814 while (!svm_queue_sub (q, (u8 *) &msg, SVM_Q_WAIT, 0))
816 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) msg - offsetof (msgbuf_t, data));
817 vl_msg_api_handler ((void *) msg, ntohl (msgbuf->data_len));
822 vl_msg_api_max_length (void *mp)
827 /* Work out the maximum sane message length, and return it */
828 if (PREDICT_TRUE (mp != 0))
830 mb = (msgbuf_t *) (((u8 *) mp) - offsetof (msgbuf_t, data));
831 data_len = clib_net_to_host_u32 (mb->data_len);
837 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
841 case VL_API_TRACE_RX:
843 case VL_API_TRACE_TX:
850 static u8 post_mortem_dump_enabled;
853 vl_msg_api_post_mortem_dump_enable_disable (int enable)
855 post_mortem_dump_enabled = enable;
859 vl_msg_api_post_mortem_dump (void)
861 api_main_t *am = vlibapi_get_main ();
866 if (post_mortem_dump_enabled == 0)
869 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
872 fp = fopen (filename, "w");
875 rv = write (2, "Couldn't create ", 16);
876 rv = write (2, filename, strlen (filename));
877 rv = write (2, "\n", 1);
880 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp, 0);
884 rv = write (2, "Failed to save post-mortem API trace to ", 40);
885 rv = write (2, filename, strlen (filename));
886 rv = write (2, "\n", 1);
891 /* Layered message handling support */
894 vl_msg_api_set_first_available_msg_id (u16 first_avail)
896 api_main_t *am = vlibapi_get_main ();
898 am->first_available_msg_id = first_avail;
902 vl_msg_api_get_msg_ids (const char *name, int n)
904 api_main_t *am = vlibapi_get_main ();
906 vl_api_msg_range_t *rp;
910 if (am->msg_range_by_name == 0)
911 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
913 name_copy = format (0, "%s%c", name, 0);
915 p = hash_get_mem (am->msg_range_by_name, name_copy);
918 clib_warning ("WARNING: duplicate message range registration for '%s'",
920 vec_free (name_copy);
924 if (n < 0 || n > 1024)
927 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
929 vec_free (name_copy);
933 vec_add2 (am->msg_ranges, rp, 1);
935 rv = rp->first_msg_id = am->first_available_msg_id;
936 am->first_available_msg_id += n;
937 rp->last_msg_id = am->first_available_msg_id - 1;
938 rp->name = name_copy;
940 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
946 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
950 if (am->msg_index_by_name_and_crc == 0)
951 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
953 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
956 clib_warning ("attempt to redefine '%s' ignored...", string);
960 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
964 vl_msg_api_add_version (api_main_t * am, const char *string,
965 u32 major, u32 minor, u32 patch)
967 api_version_t version = {.major = major,.minor = minor,.patch = patch };
968 ASSERT (strlen (string) < 64);
969 strncpy (version.name, string, 64 - 1);
970 vec_add1 (am->api_version_list, version);
974 vl_msg_api_get_msg_index (u8 * name_and_crc)
976 api_main_t *am = vlibapi_get_main ();
979 if (am->msg_index_by_name_and_crc)
981 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
989 vl_msg_push_heap_w_region (svm_region_t * vlib_rp)
991 pthread_mutex_lock (&vlib_rp->mutex);
992 return svm_push_data_heap (vlib_rp);
996 vl_msg_push_heap (void)
998 api_main_t *am = vlibapi_get_main ();
999 return vl_msg_push_heap_w_region (am->vlib_rp);
1003 vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap)
1005 svm_pop_heap (oldheap);
1006 pthread_mutex_unlock (&vlib_rp->mutex);
1010 vl_msg_pop_heap (void *oldheap)
1012 api_main_t *am = vlibapi_get_main ();
1013 vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
1016 /* Must be nul terminated */
1018 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
1020 /* copy without nul terminator */
1021 u32 len = strlen (buf);
1023 clib_memcpy_fast (str->buf, buf, len);
1024 str->length = htonl (len);
1025 return len + sizeof (u32);
1028 /* Must NOT be nul terminated */
1030 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1032 u32 len = vec_len (vec);
1033 clib_memcpy (str->buf, vec, len);
1034 str->length = htonl (len);
1035 return len + sizeof (u32);
1039 vl_api_string_len (vl_api_string_t * astr)
1041 return clib_net_to_host_u32 (astr->length);
1045 vl_api_format_string (u8 * s, va_list * args)
1047 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1048 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1053 * Returns a new vector. Remember to free it after use.
1054 * NOT nul terminated.
1057 vl_api_from_api_to_new_vec (void *mp, vl_api_string_t * astr)
1061 if (vl_msg_api_max_length (mp) < clib_net_to_host_u32 (astr->length))
1062 return format (0, "insane astr->length %u%c",
1063 clib_net_to_host_u32 (astr->length), 0);
1064 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1069 * Returns a new vector. Remember to free it after use.
1073 vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
1076 if (clib_net_to_host_u32 (astr->length) > 0)
1078 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1085 vl_api_set_elog_main (elog_main_t * m)
1087 api_main_t *am = vlibapi_get_main ();
1092 vl_api_set_elog_trace_api_messages (int enable)
1095 api_main_t *am = vlibapi_get_main ();
1097 rv = am->elog_trace_api_messages;
1098 am->elog_trace_api_messages = enable;
1103 vl_api_get_elog_trace_api_messages (void)
1105 api_main_t *am = vlibapi_get_main ();
1107 return am->elog_trace_api_messages;
1111 * fd.io coding-style-patch-verification: ON
1114 * eval: (c-set-style "gnu")