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 fh.msgtbl_size = clib_host_to_net_u32 (vec_len (m));
254 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
259 /* Write the message table */
260 if (fwrite (m, vec_len (m), 1, fp) != 1)
267 if (tp->wrapped == 0)
270 * Note: vec_len return 0 when fed a NULL pointer.
271 * Unfortunately, the static analysis tool doesn't
272 * figure it out, hence the suppressed warnings.
273 * What a great use of my time.
275 for (i = 0; i < vec_len (tp->traces); i++)
278 /*sa_ignore NO_NULL_CHK */
281 * This retarded check required to pass
287 msg_length = clib_host_to_net_u32 (vec_len (msg));
288 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
289 != sizeof (msg_length))
293 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
301 /* Wrap case: write oldest -> end of buffer */
302 for (i = tp->curindex; i < vec_len (tp->traces); i++)
307 * This retarded check required to pass
313 msg_length = clib_host_to_net_u32 (vec_len (msg));
314 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
315 != sizeof (msg_length))
320 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
325 /* write beginning of buffer -> oldest-1 */
326 for (i = 0; i < tp->curindex; i++)
329 /*sa_ignore NO_NULL_CHK */
332 * This retarded check required to pass
338 msg_length = clib_host_to_net_u32 (vec_len (msg));
339 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
340 != sizeof (msg_length))
345 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
355 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
363 case VL_API_TRACE_TX:
367 vec_validate (am->tx_trace, 0);
372 case VL_API_TRACE_RX:
376 vec_validate (am->rx_trace, 0);
389 was_on = vl_msg_api_trace_onoff (am, which, 0);
393 vl_msg_api_trace_free (am, which);
396 clib_memset (tp, 0, sizeof (*tp));
398 if (clib_arch_is_big_endian)
400 tp->endian = VL_API_BIG_ENDIAN;
404 tp->endian = VL_API_LITTLE_ENDIAN;
410 (void) vl_msg_api_trace_onoff (am, which, was_on);
416 vl_msg_api_barrier_sync (void)
421 vl_msg_api_barrier_release (void)
426 msg_handler_internal (api_main_t * am,
427 void *the_msg, int trace_it, int do_it, int free_it)
429 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
430 u8 *(*print_fp) (void *, void *);
432 if (PREDICT_FALSE (am->elog_trace_api_messages))
435 ELOG_TYPE_DECLARE (e) =
437 .format = "api-msg: %s",
445 ed = ELOG_DATA (am->elog_main, e);
446 if (id < vec_len (am->msg_names))
447 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
449 ed->c = elog_string (am->elog_main, "BOGUS");
452 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
455 vl_msg_api_trace (am, am->rx_trace, the_msg);
457 if (am->msg_print_flag)
459 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
460 print_fp = (void *) am->msg_print_handlers[id];
463 fformat (stdout, " [no registered print fn]\n");
467 (*print_fp) (the_msg, stdout);
473 if (!am->is_mp_safe[id])
475 vl_msg_api_barrier_trace_context (am->msg_names[id]);
476 vl_msg_api_barrier_sync ();
478 (*am->msg_handlers[id]) (the_msg);
479 if (!am->is_mp_safe[id])
480 vl_msg_api_barrier_release ();
485 clib_warning ("no handler for msg id %d", id);
489 vl_msg_api_free (the_msg);
491 if (PREDICT_FALSE (am->elog_trace_api_messages))
494 ELOG_TYPE_DECLARE (e) =
496 .format = "api-msg-done(%s): %s",
497 .format_args = "t4T4",
512 ed = ELOG_DATA (am->elog_main, e);
513 if (id < vec_len (am->msg_names))
515 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
516 ed->barrier = !am->is_mp_safe[id];
520 ed->c = elog_string (am->elog_main, "BOGUS");
526 /* This is only to be called from a vlib/vnet app */
528 vl_msg_api_handler_with_vm_node (api_main_t * am,
529 void *the_msg, vlib_main_t * vm,
530 vlib_node_runtime_t * node)
532 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
533 u8 *(*handler) (void *, void *, void *);
534 u8 *(*print_fp) (void *, void *);
537 if (PREDICT_FALSE (am->elog_trace_api_messages))
540 ELOG_TYPE_DECLARE (e) =
542 .format = "api-msg: %s",
550 ed = ELOG_DATA (am->elog_main, e);
551 if (id < vec_len (am->msg_names))
552 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
554 ed->c = elog_string (am->elog_main, "BOGUS");
557 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
559 handler = (void *) am->msg_handlers[id];
561 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
562 vl_msg_api_trace (am, am->rx_trace, the_msg);
564 if (PREDICT_FALSE (am->msg_print_flag))
566 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
567 print_fp = (void *) am->msg_print_handlers[id];
570 fformat (stdout, " [no registered print fn for msg %d]\n", id);
574 (*print_fp) (the_msg, vm);
577 is_mp_safe = am->is_mp_safe[id];
581 vl_msg_api_barrier_trace_context (am->msg_names[id]);
582 vl_msg_api_barrier_sync ();
584 (*handler) (the_msg, vm, node);
586 vl_msg_api_barrier_release ();
590 clib_warning ("no handler for msg id %d", id);
594 * Special-case, so we can e.g. bounce messages off the vnet
595 * main thread without copying them...
597 if (!(am->message_bounce[id]))
598 vl_msg_api_free (the_msg);
600 if (PREDICT_FALSE (am->elog_trace_api_messages))
603 ELOG_TYPE_DECLARE (e) =
605 .format = "api-msg-done(%s): %s",
606 .format_args = "t4T4",
621 ed = ELOG_DATA (am->elog_main, e);
622 if (id < vec_len (am->msg_names))
623 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
625 ed->c = elog_string (am->elog_main, "BOGUS");
626 ed->barrier = is_mp_safe;
631 vl_msg_api_handler (void *the_msg)
633 api_main_t *am = &api_main;
635 msg_handler_internal (am, the_msg,
637 && am->rx_trace->enabled) /* trace_it */ ,
638 1 /* do_it */ , 1 /* free_it */ );
642 vl_msg_api_handler_no_free (void *the_msg)
644 api_main_t *am = &api_main;
645 msg_handler_internal (am, the_msg,
647 && am->rx_trace->enabled) /* trace_it */ ,
648 1 /* do_it */ , 0 /* free_it */ );
652 vl_msg_api_handler_no_trace_no_free (void *the_msg)
654 api_main_t *am = &api_main;
655 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
660 * Add a trace record to the API message trace buffer, if
661 * API message tracing is enabled. Handy for adding sufficient
662 * data to the trace to reproduce autonomous state, as opposed to
663 * state downloaded via control-plane API messages. Example: the NAT
664 * application creates database entries based on packet traffic, not
665 * control-plane messages.
669 vl_msg_api_trace_only (void *the_msg)
671 api_main_t *am = &api_main;
673 msg_handler_internal (am, the_msg,
675 && am->rx_trace->enabled) /* trace_it */ ,
676 0 /* do_it */ , 0 /* free_it */ );
680 vl_msg_api_cleanup_handler (void *the_msg)
682 api_main_t *am = &api_main;
683 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
685 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
687 clib_warning ("_vl_msg_id too large: %d\n", id);
690 if (am->msg_cleanup_handlers[id])
691 (*am->msg_cleanup_handlers[id]) (the_msg);
693 vl_msg_api_free (the_msg);
697 * vl_msg_api_replay_handler
700 vl_msg_api_replay_handler (void *the_msg)
702 api_main_t *am = &api_main;
704 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
706 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
708 clib_warning ("_vl_msg_id too large: %d\n", id);
711 /* do NOT trace the message... */
712 if (am->msg_handlers[id])
713 (*am->msg_handlers[id]) (the_msg);
714 /* do NOT free the message buffer... */
718 vl_msg_api_get_msg_length (void *msg_arg)
720 return vl_msg_api_get_msg_length_inline (msg_arg);
724 * vl_msg_api_socket_handler
727 vl_msg_api_socket_handler (void *the_msg)
729 api_main_t *am = &api_main;
731 msg_handler_internal (am, the_msg,
733 && am->rx_trace->enabled) /* trace_it */ ,
734 1 /* do_it */ , 0 /* free_it */ );
737 #define foreach_msg_api_vector \
740 _(msg_cleanup_handlers) \
741 _(msg_endian_handlers) \
742 _(msg_print_handlers) \
748 vl_msg_api_config (vl_msg_api_msg_config_t * c)
750 api_main_t *am = &api_main;
753 * This happens during the java core tests if the message
754 * dictionary is missing newly added xxx_reply_t messages.
755 * Should never happen, but since I shot myself in the foot once
756 * this way, I thought I'd make it easy to debug if I ever do
757 * it again... (;-)...
762 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
764 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
765 clib_warning ("Did you forget to call setup_message_id_table?");
769 #define _(a) vec_validate (am->a, c->id);
770 foreach_msg_api_vector;
773 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
775 ("BUG: re-registering 'vl_api_%s_t_handler'."
776 "Handler was %llx, replaced by %llx",
777 c->name, am->msg_handlers[c->id], c->handler);
779 am->msg_names[c->id] = c->name;
780 am->msg_handlers[c->id] = c->handler;
781 am->msg_cleanup_handlers[c->id] = c->cleanup;
782 am->msg_endian_handlers[c->id] = c->endian;
783 am->msg_print_handlers[c->id] = c->print;
784 am->message_bounce[c->id] = c->message_bounce;
785 am->is_mp_safe[c->id] = c->is_mp_safe;
787 am->api_trace_cfg[c->id].size = c->size;
788 am->api_trace_cfg[c->id].trace_enable = c->traced;
789 am->api_trace_cfg[c->id].replay_enable = c->replay;
793 * vl_msg_api_set_handlers
794 * preserve the old API for a while
797 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
798 void *endian, void *print, int size, int traced)
800 vl_msg_api_msg_config_t cfg;
801 vl_msg_api_msg_config_t *c = &cfg;
803 clib_memset (c, 0, sizeof (*c));
807 c->handler = handler;
808 c->cleanup = cleanup;
813 c->message_bounce = 0;
815 vl_msg_api_config (c);
819 vl_msg_api_clean_handlers (int msg_id)
821 vl_msg_api_msg_config_t cfg;
822 vl_msg_api_msg_config_t *c = &cfg;
824 clib_memset (c, 0, sizeof (*c));
827 vl_msg_api_config (c);
831 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
833 api_main_t *am = &api_main;
836 vec_validate (am->msg_cleanup_handlers, msg_id);
837 am->msg_cleanup_handlers[msg_id] = fp;
841 vl_msg_api_queue_handler (svm_queue_t * q)
845 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
846 vl_msg_api_handler ((void *) msg);
850 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
854 case VL_API_TRACE_RX:
856 case VL_API_TRACE_TX:
864 vl_noop_handler (void *mp)
869 static u8 post_mortem_dump_enabled;
872 vl_msg_api_post_mortem_dump_enable_disable (int enable)
874 post_mortem_dump_enabled = enable;
878 vl_msg_api_post_mortem_dump (void)
880 api_main_t *am = &api_main;
885 if (post_mortem_dump_enabled == 0)
888 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
891 fp = fopen (filename, "w");
894 rv = write (2, "Couldn't create ", 16);
895 rv = write (2, filename, strlen (filename));
896 rv = write (2, "\n", 1);
899 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
903 rv = write (2, "Failed to save post-mortem API trace to ", 40);
904 rv = write (2, filename, strlen (filename));
905 rv = write (2, "\n", 1);
910 /* Layered message handling support */
913 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
915 api_main_t *am = &api_main;
917 /* Mild idiot proofing */
918 if (msg_id_host_byte_order > 10000)
919 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
920 msg_id_host_byte_order,
921 clib_net_to_host_u16 (msg_id_host_byte_order));
922 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
923 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
927 vl_msg_api_pd_handler (void *mp, int rv)
929 api_main_t *am = &api_main;
930 int (*fp) (void *, int);
933 if (clib_arch_is_little_endian)
934 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
936 msg_id = *((u16 *) mp);
938 if (msg_id >= vec_len (am->pd_msg_handlers)
939 || am->pd_msg_handlers[msg_id] == 0)
942 fp = am->pd_msg_handlers[msg_id];
948 vl_msg_api_set_first_available_msg_id (u16 first_avail)
950 api_main_t *am = &api_main;
952 am->first_available_msg_id = first_avail;
956 vl_msg_api_get_msg_ids (const char *name, int n)
958 api_main_t *am = &api_main;
960 vl_api_msg_range_t *rp;
964 if (am->msg_range_by_name == 0)
965 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
967 name_copy = format (0, "%s%c", name, 0);
969 p = hash_get_mem (am->msg_range_by_name, name_copy);
972 clib_warning ("WARNING: duplicate message range registration for '%s'",
974 vec_free (name_copy);
978 if (n < 0 || n > 1024)
981 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
983 vec_free (name_copy);
987 vec_add2 (am->msg_ranges, rp, 1);
989 rv = rp->first_msg_id = am->first_available_msg_id;
990 am->first_available_msg_id += n;
991 rp->last_msg_id = am->first_available_msg_id - 1;
992 rp->name = name_copy;
994 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
1000 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
1004 if (am->msg_index_by_name_and_crc == 0)
1005 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
1007 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
1010 clib_warning ("attempt to redefine '%s' ignored...", string);
1014 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
1018 vl_msg_api_add_version (api_main_t * am, const char *string,
1019 u32 major, u32 minor, u32 patch)
1021 api_version_t version = {.major = major,.minor = minor,.patch = patch };
1022 ASSERT (strlen (string) < 64);
1023 strncpy (version.name, string, 64 - 1);
1024 vec_add1 (am->api_version_list, version);
1028 vl_msg_api_get_msg_index (u8 * name_and_crc)
1030 api_main_t *am = &api_main;
1033 if (am->msg_index_by_name_and_crc)
1035 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1043 vl_msg_push_heap (void)
1045 api_main_t *am = &api_main;
1046 pthread_mutex_lock (&am->vlib_rp->mutex);
1047 return svm_push_data_heap (am->vlib_rp);
1051 vl_msg_pop_heap (void *oldheap)
1053 api_main_t *am = &api_main;
1054 svm_pop_heap (oldheap);
1055 pthread_mutex_unlock (&am->vlib_rp->mutex);
1059 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 vl_api_format_string (u8 * s, va_list * args)
1092 vl_api_string_t *a = va_arg (*args, vl_api_string_t *);
1093 vec_add (s, a->buf, clib_net_to_host_u32 (a->length));
1098 * Returns a new vector. Remember to free it after use.
1101 vl_api_from_api_to_vec (vl_api_string_t * astr)
1104 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1109 vl_api_set_elog_main (elog_main_t * m)
1111 api_main_t *am = &api_main;
1116 vl_api_set_elog_trace_api_messages (int enable)
1119 api_main_t *am = &api_main;
1121 rv = am->elog_trace_api_messages;
1122 am->elog_trace_api_messages = enable;
1127 vl_api_get_elog_trace_api_messages (void)
1129 api_main_t *am = &api_main;
1131 return am->elog_trace_api_messages;
1135 * fd.io coding-style-patch-verification: ON
1138 * eval: (c-set-style "gnu")