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>
37 .region_name = "/unset",
44 vl_msg_api_increment_missing_client_counter (void)
46 api_main_t *am = &api_main;
47 am->missing_clients++;
51 vl_msg_api_rx_trace_enabled (api_main_t * am)
53 return (am->rx_trace && am->rx_trace->enabled);
57 vl_msg_api_tx_trace_enabled (api_main_t * am)
59 return (am->tx_trace && am->tx_trace->enabled);
66 vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg)
73 u16 msg_id = clib_net_to_host_u16 (*((u16 *) msg));
74 msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
76 cfgp = am->api_trace_cfg + msg_id;
78 if (!cfgp || !cfgp->trace_enable)
85 clib_warning ("tp->nitems is 0");
89 if (vec_len (tp->traces) < tp->nitems)
91 vec_add1 (tp->traces, 0);
92 this_trace = tp->traces + vec_len (tp->traces) - 1;
97 old_trace = tp->traces + tp->curindex++;
98 if (tp->curindex == tp->nitems)
100 /* Reuse the trace record, may save some memory allocator traffic */
101 msg_copy = *old_trace;
102 vec_reset_length (msg_copy);
103 this_trace = old_trace;
106 length = clib_net_to_host_u32 (header->data_len);
108 vec_validate (msg_copy, length - 1);
109 clib_memcpy_fast (msg_copy, msg, length);
110 *this_trace = msg_copy;
114 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
122 case VL_API_TRACE_TX:
126 vl_msg_api_trace_configure (am, which, 1024);
131 case VL_API_TRACE_RX:
135 vl_msg_api_trace_configure (am, which, 1024);
146 if (tp == 0 || tp->nitems == 0)
156 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
163 case VL_API_TRACE_TX:
167 case VL_API_TRACE_RX:
177 if (!tp || tp->nitems == 0)
183 for (i = 0; i < vec_len (tp->traces); i++)
185 vec_free (tp->traces[i]);
187 vec_free (tp->traces);
193 vl_api_serialize_message_table (api_main_t * am, u8 * vector)
195 serialize_main_t _sm, *sm = &_sm;
197 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
199 serialize_open_vector (sm, vector);
201 /* serialize the count */
202 serialize_integer (sm, nmsg, sizeof (u32));
205 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
207 serialize_likely_small_unsigned_integer (sm, hp->value[0]);
208 serialize_cstring (sm, (char *) hp->key);
212 return serialize_close_vector (sm);
216 vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp)
219 vl_api_trace_file_header_t fh;
225 case VL_API_TRACE_TX:
229 case VL_API_TRACE_RX:
238 /* Configured, data present? */
239 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
242 /* "Dare to be stupid" check */
248 /* Write the file header */
249 fh.wrapped = tp->wrapped;
250 fh.nitems = clib_host_to_net_u32 (vec_len (tp->traces));
251 u8 *m = vl_api_serialize_message_table (am, 0);
252 clib_warning ("Message table length %d", vec_len (m));
253 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
255 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
260 /* Write the message table */
261 if (fwrite (m, vec_len (m), 1, fp) != 1)
268 if (tp->wrapped == 0)
271 * Note: vec_len return 0 when fed a NULL pointer.
272 * Unfortunately, the static analysis tool doesn't
273 * figure it out, hence the suppressed warnings.
274 * What a great use of my time.
276 for (i = 0; i < vec_len (tp->traces); i++)
279 /*sa_ignore NO_NULL_CHK */
282 * This retarded check required to pass
288 msg_length = clib_host_to_net_u32 (vec_len (msg));
289 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
290 != sizeof (msg_length))
294 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
302 /* Wrap case: write oldest -> end of buffer */
303 for (i = tp->curindex; i < vec_len (tp->traces); i++)
308 * This retarded check required to pass
314 msg_length = clib_host_to_net_u32 (vec_len (msg));
315 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
316 != sizeof (msg_length))
321 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
326 /* write beginning of buffer -> oldest-1 */
327 for (i = 0; i < tp->curindex; i++)
330 /*sa_ignore NO_NULL_CHK */
333 * This retarded check required to pass
339 msg_length = clib_host_to_net_u32 (vec_len (msg));
340 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
341 != sizeof (msg_length))
346 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
356 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
364 case VL_API_TRACE_TX:
368 vec_validate (am->tx_trace, 0);
373 case VL_API_TRACE_RX:
377 vec_validate (am->rx_trace, 0);
390 was_on = vl_msg_api_trace_onoff (am, which, 0);
394 vl_msg_api_trace_free (am, which);
397 clib_memset (tp, 0, sizeof (*tp));
399 if (clib_arch_is_big_endian)
401 tp->endian = VL_API_BIG_ENDIAN;
405 tp->endian = VL_API_LITTLE_ENDIAN;
411 (void) vl_msg_api_trace_onoff (am, which, was_on);
417 vl_msg_api_barrier_sync (void)
422 vl_msg_api_barrier_release (void)
427 msg_handler_internal (api_main_t * am,
428 void *the_msg, int trace_it, int do_it, int free_it)
430 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
431 u8 *(*print_fp) (void *, void *);
433 if (PREDICT_FALSE (am->elog_trace_api_messages))
436 ELOG_TYPE_DECLARE (e) =
438 .format = "api-msg: %s",
446 ed = ELOG_DATA (am->elog_main, e);
447 if (id < vec_len (am->msg_names))
448 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
450 ed->c = elog_string (am->elog_main, "BOGUS");
453 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
456 vl_msg_api_trace (am, am->rx_trace, the_msg);
458 if (am->msg_print_flag)
460 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
461 print_fp = (void *) am->msg_print_handlers[id];
464 fformat (stdout, " [no registered print fn]\n");
468 (*print_fp) (the_msg, stdout);
474 if (!am->is_mp_safe[id])
476 vl_msg_api_barrier_trace_context (am->msg_names[id]);
477 vl_msg_api_barrier_sync ();
479 (*am->msg_handlers[id]) (the_msg);
480 if (!am->is_mp_safe[id])
481 vl_msg_api_barrier_release ();
486 clib_warning ("no handler for msg id %d", id);
490 vl_msg_api_free (the_msg);
492 if (PREDICT_FALSE (am->elog_trace_api_messages))
495 ELOG_TYPE_DECLARE (e) =
497 .format = "api-msg-done(%s): %s",
498 .format_args = "t4T4",
513 ed = ELOG_DATA (am->elog_main, e);
514 if (id < vec_len (am->msg_names))
516 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
517 ed->barrier = !am->is_mp_safe[id];
521 ed->c = elog_string (am->elog_main, "BOGUS");
527 /* This is only to be called from a vlib/vnet app */
529 vl_msg_api_handler_with_vm_node (api_main_t * am,
530 void *the_msg, vlib_main_t * vm,
531 vlib_node_runtime_t * node)
533 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
534 u8 *(*handler) (void *, void *, void *);
535 u8 *(*print_fp) (void *, void *);
538 if (PREDICT_FALSE (am->elog_trace_api_messages))
541 ELOG_TYPE_DECLARE (e) =
543 .format = "api-msg: %s",
551 ed = ELOG_DATA (am->elog_main, e);
552 if (id < vec_len (am->msg_names))
553 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
555 ed->c = elog_string (am->elog_main, "BOGUS");
558 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
560 handler = (void *) am->msg_handlers[id];
562 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
563 vl_msg_api_trace (am, am->rx_trace, the_msg);
565 if (PREDICT_FALSE (am->msg_print_flag))
567 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
568 print_fp = (void *) am->msg_print_handlers[id];
571 fformat (stdout, " [no registered print fn for msg %d]\n", id);
575 (*print_fp) (the_msg, vm);
578 is_mp_safe = am->is_mp_safe[id];
582 vl_msg_api_barrier_trace_context (am->msg_names[id]);
583 vl_msg_api_barrier_sync ();
585 (*handler) (the_msg, vm, node);
587 vl_msg_api_barrier_release ();
591 clib_warning ("no handler for msg id %d", id);
595 * Special-case, so we can e.g. bounce messages off the vnet
596 * main thread without copying them...
598 if (!(am->message_bounce[id]))
599 vl_msg_api_free (the_msg);
601 if (PREDICT_FALSE (am->elog_trace_api_messages))
604 ELOG_TYPE_DECLARE (e) =
606 .format = "api-msg-done(%s): %s",
607 .format_args = "t4T4",
622 ed = ELOG_DATA (am->elog_main, e);
623 if (id < vec_len (am->msg_names))
624 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
626 ed->c = elog_string (am->elog_main, "BOGUS");
627 ed->barrier = is_mp_safe;
632 vl_msg_api_handler (void *the_msg)
634 api_main_t *am = &api_main;
636 msg_handler_internal (am, the_msg,
638 && am->rx_trace->enabled) /* trace_it */ ,
639 1 /* do_it */ , 1 /* free_it */ );
643 vl_msg_api_handler_no_free (void *the_msg)
645 api_main_t *am = &api_main;
646 msg_handler_internal (am, the_msg,
648 && am->rx_trace->enabled) /* trace_it */ ,
649 1 /* do_it */ , 0 /* free_it */ );
653 vl_msg_api_handler_no_trace_no_free (void *the_msg)
655 api_main_t *am = &api_main;
656 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
661 * Add a trace record to the API message trace buffer, if
662 * API message tracing is enabled. Handy for adding sufficient
663 * data to the trace to reproduce autonomous state, as opposed to
664 * state downloaded via control-plane API messages. Example: the NAT
665 * application creates database entries based on packet traffic, not
666 * control-plane messages.
670 vl_msg_api_trace_only (void *the_msg)
672 api_main_t *am = &api_main;
674 msg_handler_internal (am, the_msg,
676 && am->rx_trace->enabled) /* trace_it */ ,
677 0 /* do_it */ , 0 /* free_it */ );
681 vl_msg_api_cleanup_handler (void *the_msg)
683 api_main_t *am = &api_main;
684 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
686 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
688 clib_warning ("_vl_msg_id too large: %d\n", id);
691 if (am->msg_cleanup_handlers[id])
692 (*am->msg_cleanup_handlers[id]) (the_msg);
694 vl_msg_api_free (the_msg);
698 * vl_msg_api_replay_handler
701 vl_msg_api_replay_handler (void *the_msg)
703 api_main_t *am = &api_main;
705 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
707 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
709 clib_warning ("_vl_msg_id too large: %d\n", id);
712 /* do NOT trace the message... */
713 if (am->msg_handlers[id])
714 (*am->msg_handlers[id]) (the_msg);
715 /* do NOT free the message buffer... */
719 vl_msg_api_get_msg_length (void *msg_arg)
721 return vl_msg_api_get_msg_length_inline (msg_arg);
725 * vl_msg_api_socket_handler
728 vl_msg_api_socket_handler (void *the_msg)
730 api_main_t *am = &api_main;
732 msg_handler_internal (am, the_msg,
734 && am->rx_trace->enabled) /* trace_it */ ,
735 1 /* do_it */ , 0 /* free_it */ );
738 #define foreach_msg_api_vector \
741 _(msg_cleanup_handlers) \
742 _(msg_endian_handlers) \
743 _(msg_print_handlers) \
749 vl_msg_api_config (vl_msg_api_msg_config_t * c)
751 api_main_t *am = &api_main;
754 * This happens during the java core tests if the message
755 * dictionary is missing newly added xxx_reply_t messages.
756 * Should never happen, but since I shot myself in the foot once
757 * this way, I thought I'd make it easy to debug if I ever do
758 * it again... (;-)...
763 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
765 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
766 clib_warning ("Did you forget to call setup_message_id_table?");
770 #define _(a) vec_validate (am->a, c->id);
771 foreach_msg_api_vector;
774 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
776 ("BUG: re-registering 'vl_api_%s_t_handler'."
777 "Handler was %llx, replaced by %llx",
778 c->name, am->msg_handlers[c->id], c->handler);
780 am->msg_names[c->id] = c->name;
781 am->msg_handlers[c->id] = c->handler;
782 am->msg_cleanup_handlers[c->id] = c->cleanup;
783 am->msg_endian_handlers[c->id] = c->endian;
784 am->msg_print_handlers[c->id] = c->print;
785 am->message_bounce[c->id] = c->message_bounce;
786 am->is_mp_safe[c->id] = c->is_mp_safe;
788 am->api_trace_cfg[c->id].size = c->size;
789 am->api_trace_cfg[c->id].trace_enable = c->traced;
790 am->api_trace_cfg[c->id].replay_enable = c->replay;
794 * vl_msg_api_set_handlers
795 * preserve the old API for a while
798 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
799 void *endian, void *print, int size, int traced)
801 vl_msg_api_msg_config_t cfg;
802 vl_msg_api_msg_config_t *c = &cfg;
804 clib_memset (c, 0, sizeof (*c));
808 c->handler = handler;
809 c->cleanup = cleanup;
814 c->message_bounce = 0;
816 vl_msg_api_config (c);
820 vl_msg_api_clean_handlers (int msg_id)
822 vl_msg_api_msg_config_t cfg;
823 vl_msg_api_msg_config_t *c = &cfg;
825 clib_memset (c, 0, sizeof (*c));
828 vl_msg_api_config (c);
832 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
834 api_main_t *am = &api_main;
837 vec_validate (am->msg_cleanup_handlers, msg_id);
838 am->msg_cleanup_handlers[msg_id] = fp;
842 vl_msg_api_queue_handler (svm_queue_t * q)
846 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
847 vl_msg_api_handler ((void *) msg);
851 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
855 case VL_API_TRACE_RX:
857 case VL_API_TRACE_TX:
865 vl_noop_handler (void *mp)
870 static u8 post_mortem_dump_enabled;
873 vl_msg_api_post_mortem_dump_enable_disable (int enable)
875 post_mortem_dump_enabled = enable;
879 vl_msg_api_post_mortem_dump (void)
881 api_main_t *am = &api_main;
886 if (post_mortem_dump_enabled == 0)
889 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
892 fp = fopen (filename, "w");
895 rv = write (2, "Couldn't create ", 16);
896 rv = write (2, filename, strlen (filename));
897 rv = write (2, "\n", 1);
900 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
904 rv = write (2, "Failed to save post-mortem API trace to ", 40);
905 rv = write (2, filename, strlen (filename));
906 rv = write (2, "\n", 1);
911 /* Layered message handling support */
914 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
916 api_main_t *am = &api_main;
918 /* Mild idiot proofing */
919 if (msg_id_host_byte_order > 10000)
920 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
921 msg_id_host_byte_order,
922 clib_net_to_host_u16 (msg_id_host_byte_order));
923 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
924 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
928 vl_msg_api_pd_handler (void *mp, int rv)
930 api_main_t *am = &api_main;
931 int (*fp) (void *, int);
934 if (clib_arch_is_little_endian)
935 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
937 msg_id = *((u16 *) mp);
939 if (msg_id >= vec_len (am->pd_msg_handlers)
940 || am->pd_msg_handlers[msg_id] == 0)
943 fp = am->pd_msg_handlers[msg_id];
949 vl_msg_api_set_first_available_msg_id (u16 first_avail)
951 api_main_t *am = &api_main;
953 am->first_available_msg_id = first_avail;
957 vl_msg_api_get_msg_ids (const char *name, int n)
959 api_main_t *am = &api_main;
961 vl_api_msg_range_t *rp;
965 if (am->msg_range_by_name == 0)
966 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
968 name_copy = format (0, "%s%c", name, 0);
970 p = hash_get_mem (am->msg_range_by_name, name_copy);
973 clib_warning ("WARNING: duplicate message range registration for '%s'",
975 vec_free (name_copy);
979 if (n < 0 || n > 1024)
982 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
984 vec_free (name_copy);
988 vec_add2 (am->msg_ranges, rp, 1);
990 rv = rp->first_msg_id = am->first_available_msg_id;
991 am->first_available_msg_id += n;
992 rp->last_msg_id = am->first_available_msg_id - 1;
993 rp->name = name_copy;
995 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
1001 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
1005 if (am->msg_index_by_name_and_crc == 0)
1006 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
1008 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1011 clib_warning ("attempt to redefine '%s' ignored...", string);
1015 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1019 vl_msg_api_add_version (api_main_t * am, const char *string,
1020 u32 major, u32 minor, u32 patch)
1022 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1023 ASSERT (strlen (string) < 64);
1024 strncpy (version.name, string, 64 - 1);
1025 vec_add1 (am->api_version_list, version);
1029 vl_msg_api_get_msg_index (u8 * name_and_crc)
1031 api_main_t *am = &api_main;
1034 if (am->msg_index_by_name_and_crc)
1036 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1044 vl_msg_push_heap (void)
1046 api_main_t *am = &api_main;
1047 pthread_mutex_lock (&am->vlib_rp->mutex);
1048 return svm_push_data_heap (am->vlib_rp);
1052 vl_msg_pop_heap (void *oldheap)
1054 api_main_t *am = &api_main;
1055 svm_pop_heap (oldheap);
1056 pthread_mutex_unlock (&am->vlib_rp->mutex);
1060 vl_api_to_api_string (u32 len, const char *buf, vl_api_string_t * str)
1062 clib_memcpy_fast (str->buf, buf, len);
1063 str->length = htonl (len);
1064 return len + sizeof (u32);
1068 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1070 u32 len = vec_len (vec);
1071 clib_memcpy (str->buf, vec, len);
1072 str->length = htonl (len);
1073 return len + sizeof (u32);
1076 /* Return a pointer to the API string (not nul terminated */
1078 vl_api_from_api_string (vl_api_string_t * astr)
1084 vl_api_string_len (vl_api_string_t * astr)
1086 return clib_net_to_host_u32 (astr->length);
1090 * Returns a new vector. Remember to free it after use.
1093 vl_api_from_api_to_vec (vl_api_string_t * astr)
1096 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1101 vl_api_set_elog_main (elog_main_t * m)
1103 api_main_t *am = &api_main;
1108 vl_api_set_elog_trace_api_messages (int enable)
1111 api_main_t *am = &api_main;
1113 rv = am->elog_trace_api_messages;
1114 am->elog_trace_api_messages = enable;
1119 vl_api_get_elog_trace_api_messages (void)
1121 api_main_t *am = &api_main;
1123 return am->elog_trace_api_messages;
1127 * fd.io coding-style-patch-verification: ON
1130 * eval: (c-set-style "gnu")