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_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp)
196 vl_api_trace_file_header_t fh;
202 case VL_API_TRACE_TX:
206 case VL_API_TRACE_RX:
215 /* Configured, data present? */
216 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
219 /* "Dare to be stupid" check */
225 /* Write the file header */
226 fh.nitems = vec_len (tp->traces);
227 fh.endian = tp->endian;
228 fh.wrapped = tp->wrapped;
230 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
236 if (tp->wrapped == 0)
239 * Note: vec_len return 0 when fed a NULL pointer.
240 * Unfortunately, the static analysis tool doesn't
241 * figure it out, hence the suppressed warnings.
242 * What a great use of my time.
244 for (i = 0; i < vec_len (tp->traces); i++)
247 /*sa_ignore NO_NULL_CHK */
250 * This retarded check required to pass
256 msg_length = clib_host_to_net_u32 (vec_len (msg));
257 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
258 != sizeof (msg_length))
262 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
270 /* Wrap case: write oldest -> end of buffer */
271 for (i = tp->curindex; i < vec_len (tp->traces); i++)
276 * This retarded check required to pass
282 msg_length = clib_host_to_net_u32 (vec_len (msg));
283 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
284 != sizeof (msg_length))
289 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
294 /* write beginning of buffer -> oldest-1 */
295 for (i = 0; i < tp->curindex; i++)
298 /*sa_ignore NO_NULL_CHK */
301 * This retarded check required to pass
307 msg_length = clib_host_to_net_u32 (vec_len (msg));
308 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
309 != sizeof (msg_length))
314 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
324 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
332 case VL_API_TRACE_TX:
336 vec_validate (am->tx_trace, 0);
341 case VL_API_TRACE_RX:
345 vec_validate (am->rx_trace, 0);
358 was_on = vl_msg_api_trace_onoff (am, which, 0);
362 vl_msg_api_trace_free (am, which);
365 clib_memset (tp, 0, sizeof (*tp));
367 if (clib_arch_is_big_endian)
369 tp->endian = VL_API_BIG_ENDIAN;
373 tp->endian = VL_API_LITTLE_ENDIAN;
379 (void) vl_msg_api_trace_onoff (am, which, was_on);
385 vl_msg_api_barrier_sync (void)
390 vl_msg_api_barrier_release (void)
395 msg_handler_internal (api_main_t * am,
396 void *the_msg, int trace_it, int do_it, int free_it)
398 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
399 u8 *(*print_fp) (void *, void *);
401 if (PREDICT_FALSE (am->elog_trace_api_messages))
404 ELOG_TYPE_DECLARE (e) =
406 .format = "api-msg: %s",
414 ed = ELOG_DATA (am->elog_main, e);
415 if (id < vec_len (am->msg_names))
416 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
418 ed->c = elog_string (am->elog_main, "BOGUS");
421 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
424 vl_msg_api_trace (am, am->rx_trace, the_msg);
426 if (am->msg_print_flag)
428 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
429 print_fp = (void *) am->msg_print_handlers[id];
432 fformat (stdout, " [no registered print fn]\n");
436 (*print_fp) (the_msg, stdout);
442 if (!am->is_mp_safe[id])
444 vl_msg_api_barrier_trace_context (am->msg_names[id]);
445 vl_msg_api_barrier_sync ();
447 (*am->msg_handlers[id]) (the_msg);
448 if (!am->is_mp_safe[id])
449 vl_msg_api_barrier_release ();
454 clib_warning ("no handler for msg id %d", id);
458 vl_msg_api_free (the_msg);
460 if (PREDICT_FALSE (am->elog_trace_api_messages))
463 ELOG_TYPE_DECLARE (e) =
465 .format = "api-msg-done(%s): %s",
466 .format_args = "t4T4",
481 ed = ELOG_DATA (am->elog_main, e);
482 if (id < vec_len (am->msg_names))
484 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
485 ed->barrier = !am->is_mp_safe[id];
489 ed->c = elog_string (am->elog_main, "BOGUS");
495 /* This is only to be called from a vlib/vnet app */
497 vl_msg_api_handler_with_vm_node (api_main_t * am,
498 void *the_msg, vlib_main_t * vm,
499 vlib_node_runtime_t * node)
501 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
502 u8 *(*handler) (void *, void *, void *);
503 u8 *(*print_fp) (void *, void *);
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);
520 if (id < vec_len (am->msg_names))
521 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
523 ed->c = elog_string (am->elog_main, "BOGUS");
526 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
528 handler = (void *) am->msg_handlers[id];
530 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
531 vl_msg_api_trace (am, am->rx_trace, the_msg);
533 if (PREDICT_FALSE (am->msg_print_flag))
535 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
536 print_fp = (void *) am->msg_print_handlers[id];
539 fformat (stdout, " [no registered print fn for msg %d]\n", id);
543 (*print_fp) (the_msg, vm);
546 is_mp_safe = am->is_mp_safe[id];
550 vl_msg_api_barrier_trace_context (am->msg_names[id]);
551 vl_msg_api_barrier_sync ();
553 (*handler) (the_msg, vm, node);
555 vl_msg_api_barrier_release ();
559 clib_warning ("no handler for msg id %d", id);
563 * Special-case, so we can e.g. bounce messages off the vnet
564 * main thread without copying them...
566 if (!(am->message_bounce[id]))
567 vl_msg_api_free (the_msg);
569 if (PREDICT_FALSE (am->elog_trace_api_messages))
572 ELOG_TYPE_DECLARE (e) =
574 .format = "api-msg-done(%s): %s",
575 .format_args = "t4T4",
590 ed = ELOG_DATA (am->elog_main, e);
591 if (id < vec_len (am->msg_names))
592 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
594 ed->c = elog_string (am->elog_main, "BOGUS");
595 ed->barrier = is_mp_safe;
600 vl_msg_api_handler (void *the_msg)
602 api_main_t *am = &api_main;
604 msg_handler_internal (am, the_msg,
606 && am->rx_trace->enabled) /* trace_it */ ,
607 1 /* do_it */ , 1 /* free_it */ );
611 vl_msg_api_handler_no_free (void *the_msg)
613 api_main_t *am = &api_main;
614 msg_handler_internal (am, the_msg,
616 && am->rx_trace->enabled) /* trace_it */ ,
617 1 /* do_it */ , 0 /* free_it */ );
621 vl_msg_api_handler_no_trace_no_free (void *the_msg)
623 api_main_t *am = &api_main;
624 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
629 * Add a trace record to the API message trace buffer, if
630 * API message tracing is enabled. Handy for adding sufficient
631 * data to the trace to reproduce autonomous state, as opposed to
632 * state downloaded via control-plane API messages. Example: the NAT
633 * application creates database entries based on packet traffic, not
634 * control-plane messages.
638 vl_msg_api_trace_only (void *the_msg)
640 api_main_t *am = &api_main;
642 msg_handler_internal (am, the_msg,
644 && am->rx_trace->enabled) /* trace_it */ ,
645 0 /* do_it */ , 0 /* free_it */ );
649 vl_msg_api_cleanup_handler (void *the_msg)
651 api_main_t *am = &api_main;
652 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
654 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
656 clib_warning ("_vl_msg_id too large: %d\n", id);
659 if (am->msg_cleanup_handlers[id])
660 (*am->msg_cleanup_handlers[id]) (the_msg);
662 vl_msg_api_free (the_msg);
666 * vl_msg_api_replay_handler
669 vl_msg_api_replay_handler (void *the_msg)
671 api_main_t *am = &api_main;
673 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
675 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
677 clib_warning ("_vl_msg_id too large: %d\n", id);
680 /* do NOT trace the message... */
681 if (am->msg_handlers[id])
682 (*am->msg_handlers[id]) (the_msg);
683 /* do NOT free the message buffer... */
687 vl_msg_api_get_msg_length (void *msg_arg)
689 return vl_msg_api_get_msg_length_inline (msg_arg);
693 * vl_msg_api_socket_handler
696 vl_msg_api_socket_handler (void *the_msg)
698 api_main_t *am = &api_main;
700 msg_handler_internal (am, the_msg,
702 && am->rx_trace->enabled) /* trace_it */ ,
703 1 /* do_it */ , 0 /* free_it */ );
706 #define foreach_msg_api_vector \
709 _(msg_cleanup_handlers) \
710 _(msg_endian_handlers) \
711 _(msg_print_handlers) \
717 vl_msg_api_config (vl_msg_api_msg_config_t * c)
719 api_main_t *am = &api_main;
722 * This happens during the java core tests if the message
723 * dictionary is missing newly added xxx_reply_t messages.
724 * Should never happen, but since I shot myself in the foot once
725 * this way, I thought I'd make it easy to debug if I ever do
726 * it again... (;-)...
731 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
733 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
734 clib_warning ("Did you forget to call setup_message_id_table?");
738 #define _(a) vec_validate (am->a, c->id);
739 foreach_msg_api_vector;
742 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
744 ("BUG: re-registering 'vl_api_%s_t_handler'."
745 "Handler was %llx, replaced by %llx",
746 c->name, am->msg_handlers[c->id], c->handler);
748 am->msg_names[c->id] = c->name;
749 am->msg_handlers[c->id] = c->handler;
750 am->msg_cleanup_handlers[c->id] = c->cleanup;
751 am->msg_endian_handlers[c->id] = c->endian;
752 am->msg_print_handlers[c->id] = c->print;
753 am->message_bounce[c->id] = c->message_bounce;
754 am->is_mp_safe[c->id] = c->is_mp_safe;
756 am->api_trace_cfg[c->id].size = c->size;
757 am->api_trace_cfg[c->id].trace_enable = c->traced;
758 am->api_trace_cfg[c->id].replay_enable = c->replay;
762 * vl_msg_api_set_handlers
763 * preserve the old API for a while
766 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
767 void *endian, void *print, int size, int traced)
769 vl_msg_api_msg_config_t cfg;
770 vl_msg_api_msg_config_t *c = &cfg;
772 clib_memset (c, 0, sizeof (*c));
776 c->handler = handler;
777 c->cleanup = cleanup;
782 c->message_bounce = 0;
784 vl_msg_api_config (c);
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 = &api_main;
805 vec_validate (am->msg_cleanup_handlers, msg_id);
806 am->msg_cleanup_handlers[msg_id] = fp;
810 vl_msg_api_queue_handler (svm_queue_t * q)
814 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
815 vl_msg_api_handler ((void *) msg);
819 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
823 case VL_API_TRACE_RX:
825 case VL_API_TRACE_TX:
833 vl_noop_handler (void *mp)
838 static u8 post_mortem_dump_enabled;
841 vl_msg_api_post_mortem_dump_enable_disable (int enable)
843 post_mortem_dump_enabled = enable;
847 vl_msg_api_post_mortem_dump (void)
849 api_main_t *am = &api_main;
854 if (post_mortem_dump_enabled == 0)
857 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
860 fp = fopen (filename, "w");
863 rv = write (2, "Couldn't create ", 16);
864 rv = write (2, filename, strlen (filename));
865 rv = write (2, "\n", 1);
868 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
872 rv = write (2, "Failed to save post-mortem API trace to ", 40);
873 rv = write (2, filename, strlen (filename));
874 rv = write (2, "\n", 1);
879 /* Layered message handling support */
882 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
884 api_main_t *am = &api_main;
886 /* Mild idiot proofing */
887 if (msg_id_host_byte_order > 10000)
888 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
889 msg_id_host_byte_order,
890 clib_net_to_host_u16 (msg_id_host_byte_order));
891 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
892 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
896 vl_msg_api_pd_handler (void *mp, int rv)
898 api_main_t *am = &api_main;
899 int (*fp) (void *, int);
902 if (clib_arch_is_little_endian)
903 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
905 msg_id = *((u16 *) mp);
907 if (msg_id >= vec_len (am->pd_msg_handlers)
908 || am->pd_msg_handlers[msg_id] == 0)
911 fp = am->pd_msg_handlers[msg_id];
917 vl_msg_api_set_first_available_msg_id (u16 first_avail)
919 api_main_t *am = &api_main;
921 am->first_available_msg_id = first_avail;
925 vl_msg_api_get_msg_ids (const char *name, int n)
927 api_main_t *am = &api_main;
929 vl_api_msg_range_t *rp;
933 if (am->msg_range_by_name == 0)
934 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
936 name_copy = format (0, "%s%c", name, 0);
938 p = hash_get_mem (am->msg_range_by_name, name_copy);
941 clib_warning ("WARNING: duplicate message range registration for '%s'",
943 vec_free (name_copy);
947 if (n < 0 || n > 1024)
950 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
952 vec_free (name_copy);
956 vec_add2 (am->msg_ranges, rp, 1);
958 rv = rp->first_msg_id = am->first_available_msg_id;
959 am->first_available_msg_id += n;
960 rp->last_msg_id = am->first_available_msg_id - 1;
961 rp->name = name_copy;
963 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
969 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
973 if (am->msg_index_by_name_and_crc == 0)
974 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
976 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
979 clib_warning ("attempt to redefine '%s' ignored...", string);
983 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
987 vl_msg_api_add_version (api_main_t * am, const char *string,
988 u32 major, u32 minor, u32 patch)
990 api_version_t version = {.major = major,.minor = minor,.patch = patch };
991 ASSERT (strlen (string) < 64);
992 strncpy (version.name, string, 64 - 1);
993 vec_add1 (am->api_version_list, version);
997 vl_msg_api_get_msg_index (u8 * name_and_crc)
999 api_main_t *am = &api_main;
1002 if (am->msg_index_by_name_and_crc)
1004 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1012 vl_msg_push_heap (void)
1014 api_main_t *am = &api_main;
1015 pthread_mutex_lock (&am->vlib_rp->mutex);
1016 return svm_push_data_heap (am->vlib_rp);
1020 vl_msg_pop_heap (void *oldheap)
1022 api_main_t *am = &api_main;
1023 svm_pop_heap (oldheap);
1024 pthread_mutex_unlock (&am->vlib_rp->mutex);
1028 vl_api_to_api_string (u32 len, const char *buf, vl_api_string_t * str)
1030 clib_memcpy_fast (str->buf, buf, len);
1031 str->length = htonl (len);
1032 return len + sizeof (u32);
1036 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1038 u32 len = vec_len (vec);
1039 clib_memcpy (str->buf, vec, len);
1040 str->length = htonl (len);
1041 return len + sizeof (u32);
1044 /* Return a pointer to the API string (not nul terminated */
1046 vl_api_from_api_string (vl_api_string_t * astr)
1052 vl_api_string_len (vl_api_string_t * astr)
1054 return clib_net_to_host_u32 (astr->length);
1058 * Returns a new vector. Remember to free it after use.
1061 vl_api_from_api_to_vec (vl_api_string_t * astr)
1064 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1069 vl_api_set_elog_main (elog_main_t * m)
1071 api_main_t *am = &api_main;
1076 vl_api_set_elog_trace_api_messages (int enable)
1079 api_main_t *am = &api_main;
1081 rv = am->elog_trace_api_messages;
1082 am->elog_trace_api_messages = enable;
1087 vl_api_get_elog_trace_api_messages (void)
1089 api_main_t *am = &api_main;
1091 return am->elog_trace_api_messages;
1095 * fd.io coding-style-patch-verification: ON
1098 * eval: (c-set-style "gnu")