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))
483 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
485 ed->c = elog_string (am->elog_main, "BOGUS");
486 ed->barrier = !am->is_mp_safe[id];
490 /* This is only to be called from a vlib/vnet app */
492 vl_msg_api_handler_with_vm_node (api_main_t * am,
493 void *the_msg, vlib_main_t * vm,
494 vlib_node_runtime_t * node)
496 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
497 u8 *(*handler) (void *, void *, void *);
498 u8 *(*print_fp) (void *, void *);
501 if (PREDICT_FALSE (am->elog_trace_api_messages))
504 ELOG_TYPE_DECLARE (e) =
506 .format = "api-msg: %s",
514 ed = ELOG_DATA (am->elog_main, e);
515 if (id < vec_len (am->msg_names))
516 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
518 ed->c = elog_string (am->elog_main, "BOGUS");
521 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
523 handler = (void *) am->msg_handlers[id];
525 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
526 vl_msg_api_trace (am, am->rx_trace, the_msg);
528 if (PREDICT_FALSE (am->msg_print_flag))
530 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
531 print_fp = (void *) am->msg_print_handlers[id];
534 fformat (stdout, " [no registered print fn for msg %d]\n", id);
538 (*print_fp) (the_msg, vm);
541 is_mp_safe = am->is_mp_safe[id];
545 vl_msg_api_barrier_trace_context (am->msg_names[id]);
546 vl_msg_api_barrier_sync ();
548 (*handler) (the_msg, vm, node);
550 vl_msg_api_barrier_release ();
554 clib_warning ("no handler for msg id %d", id);
558 * Special-case, so we can e.g. bounce messages off the vnet
559 * main thread without copying them...
561 if (!(am->message_bounce[id]))
562 vl_msg_api_free (the_msg);
564 if (PREDICT_FALSE (am->elog_trace_api_messages))
567 ELOG_TYPE_DECLARE (e) =
569 .format = "api-msg-done(%s): %s",
570 .format_args = "t4T4",
585 ed = ELOG_DATA (am->elog_main, e);
586 if (id < vec_len (am->msg_names))
587 ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]);
589 ed->c = elog_string (am->elog_main, "BOGUS");
590 ed->barrier = is_mp_safe;
595 vl_msg_api_handler (void *the_msg)
597 api_main_t *am = &api_main;
599 msg_handler_internal (am, the_msg,
601 && am->rx_trace->enabled) /* trace_it */ ,
602 1 /* do_it */ , 1 /* free_it */ );
606 vl_msg_api_handler_no_free (void *the_msg)
608 api_main_t *am = &api_main;
609 msg_handler_internal (am, the_msg,
611 && am->rx_trace->enabled) /* trace_it */ ,
612 1 /* do_it */ , 0 /* free_it */ );
616 vl_msg_api_handler_no_trace_no_free (void *the_msg)
618 api_main_t *am = &api_main;
619 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
624 * Add a trace record to the API message trace buffer, if
625 * API message tracing is enabled. Handy for adding sufficient
626 * data to the trace to reproduce autonomous state, as opposed to
627 * state downloaded via control-plane API messages. Example: the NAT
628 * application creates database entries based on packet traffic, not
629 * control-plane messages.
633 vl_msg_api_trace_only (void *the_msg)
635 api_main_t *am = &api_main;
637 msg_handler_internal (am, the_msg,
639 && am->rx_trace->enabled) /* trace_it */ ,
640 0 /* do_it */ , 0 /* free_it */ );
644 vl_msg_api_cleanup_handler (void *the_msg)
646 api_main_t *am = &api_main;
647 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
649 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
651 clib_warning ("_vl_msg_id too large: %d\n", id);
654 if (am->msg_cleanup_handlers[id])
655 (*am->msg_cleanup_handlers[id]) (the_msg);
657 vl_msg_api_free (the_msg);
661 * vl_msg_api_replay_handler
664 vl_msg_api_replay_handler (void *the_msg)
666 api_main_t *am = &api_main;
668 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
670 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
672 clib_warning ("_vl_msg_id too large: %d\n", id);
675 /* do NOT trace the message... */
676 if (am->msg_handlers[id])
677 (*am->msg_handlers[id]) (the_msg);
678 /* do NOT free the message buffer... */
682 vl_msg_api_get_msg_length (void *msg_arg)
684 return vl_msg_api_get_msg_length_inline (msg_arg);
688 * vl_msg_api_socket_handler
691 vl_msg_api_socket_handler (void *the_msg)
693 api_main_t *am = &api_main;
695 msg_handler_internal (am, the_msg,
697 && am->rx_trace->enabled) /* trace_it */ ,
698 1 /* do_it */ , 0 /* free_it */ );
701 #define foreach_msg_api_vector \
704 _(msg_cleanup_handlers) \
705 _(msg_endian_handlers) \
706 _(msg_print_handlers) \
712 vl_msg_api_config (vl_msg_api_msg_config_t * c)
714 api_main_t *am = &api_main;
717 * This happens during the java core tests if the message
718 * dictionary is missing newly added xxx_reply_t messages.
719 * Should never happen, but since I shot myself in the foot once
720 * this way, I thought I'd make it easy to debug if I ever do
721 * it again... (;-)...
726 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
728 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
729 clib_warning ("Did you forget to call setup_message_id_table?");
733 #define _(a) vec_validate (am->a, c->id);
734 foreach_msg_api_vector;
737 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
739 ("BUG: re-registering 'vl_api_%s_t_handler'."
740 "Handler was %llx, replaced by %llx",
741 c->name, am->msg_handlers[c->id], c->handler);
743 am->msg_names[c->id] = c->name;
744 am->msg_handlers[c->id] = c->handler;
745 am->msg_cleanup_handlers[c->id] = c->cleanup;
746 am->msg_endian_handlers[c->id] = c->endian;
747 am->msg_print_handlers[c->id] = c->print;
748 am->message_bounce[c->id] = c->message_bounce;
749 am->is_mp_safe[c->id] = c->is_mp_safe;
751 am->api_trace_cfg[c->id].size = c->size;
752 am->api_trace_cfg[c->id].trace_enable = c->traced;
753 am->api_trace_cfg[c->id].replay_enable = c->replay;
757 * vl_msg_api_set_handlers
758 * preserve the old API for a while
761 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
762 void *endian, void *print, int size, int traced)
764 vl_msg_api_msg_config_t cfg;
765 vl_msg_api_msg_config_t *c = &cfg;
767 clib_memset (c, 0, sizeof (*c));
771 c->handler = handler;
772 c->cleanup = cleanup;
777 c->message_bounce = 0;
779 vl_msg_api_config (c);
783 vl_msg_api_clean_handlers (int msg_id)
785 vl_msg_api_msg_config_t cfg;
786 vl_msg_api_msg_config_t *c = &cfg;
788 clib_memset (c, 0, sizeof (*c));
791 vl_msg_api_config (c);
795 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
797 api_main_t *am = &api_main;
800 vec_validate (am->msg_cleanup_handlers, msg_id);
801 am->msg_cleanup_handlers[msg_id] = fp;
805 vl_msg_api_queue_handler (svm_queue_t * q)
809 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
810 vl_msg_api_handler ((void *) msg);
814 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
818 case VL_API_TRACE_RX:
820 case VL_API_TRACE_TX:
828 vl_noop_handler (void *mp)
833 static u8 post_mortem_dump_enabled;
836 vl_msg_api_post_mortem_dump_enable_disable (int enable)
838 post_mortem_dump_enabled = enable;
842 vl_msg_api_post_mortem_dump (void)
844 api_main_t *am = &api_main;
849 if (post_mortem_dump_enabled == 0)
852 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
855 fp = fopen (filename, "w");
858 rv = write (2, "Couldn't create ", 16);
859 rv = write (2, filename, strlen (filename));
860 rv = write (2, "\n", 1);
863 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
867 rv = write (2, "Failed to save post-mortem API trace to ", 40);
868 rv = write (2, filename, strlen (filename));
869 rv = write (2, "\n", 1);
874 /* Layered message handling support */
877 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
879 api_main_t *am = &api_main;
881 /* Mild idiot proofing */
882 if (msg_id_host_byte_order > 10000)
883 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
884 msg_id_host_byte_order,
885 clib_net_to_host_u16 (msg_id_host_byte_order));
886 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
887 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
891 vl_msg_api_pd_handler (void *mp, int rv)
893 api_main_t *am = &api_main;
894 int (*fp) (void *, int);
897 if (clib_arch_is_little_endian)
898 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
900 msg_id = *((u16 *) mp);
902 if (msg_id >= vec_len (am->pd_msg_handlers)
903 || am->pd_msg_handlers[msg_id] == 0)
906 fp = am->pd_msg_handlers[msg_id];
912 vl_msg_api_set_first_available_msg_id (u16 first_avail)
914 api_main_t *am = &api_main;
916 am->first_available_msg_id = first_avail;
920 vl_msg_api_get_msg_ids (const char *name, int n)
922 api_main_t *am = &api_main;
924 vl_api_msg_range_t *rp;
928 if (am->msg_range_by_name == 0)
929 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
931 name_copy = format (0, "%s%c", name, 0);
933 p = hash_get_mem (am->msg_range_by_name, name_copy);
936 clib_warning ("WARNING: duplicate message range registration for '%s'",
938 vec_free (name_copy);
942 if (n < 0 || n > 1024)
945 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
947 vec_free (name_copy);
951 vec_add2 (am->msg_ranges, rp, 1);
953 rv = rp->first_msg_id = am->first_available_msg_id;
954 am->first_available_msg_id += n;
955 rp->last_msg_id = am->first_available_msg_id - 1;
956 rp->name = name_copy;
958 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
964 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
968 if (am->msg_index_by_name_and_crc == 0)
969 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
971 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
974 clib_warning ("attempt to redefine '%s' ignored...", string);
978 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
982 vl_msg_api_add_version (api_main_t * am, const char *string,
983 u32 major, u32 minor, u32 patch)
985 api_version_t version = {.major = major,.minor = minor,.patch = patch };
986 ASSERT (strlen (string) < 64);
987 strncpy (version.name, string, 64 - 1);
988 vec_add1 (am->api_version_list, version);
992 vl_msg_api_get_msg_index (u8 * name_and_crc)
994 api_main_t *am = &api_main;
997 if (am->msg_index_by_name_and_crc)
999 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
1007 vl_msg_push_heap (void)
1009 api_main_t *am = &api_main;
1010 pthread_mutex_lock (&am->vlib_rp->mutex);
1011 return svm_push_data_heap (am->vlib_rp);
1015 vl_msg_pop_heap (void *oldheap)
1017 api_main_t *am = &api_main;
1018 svm_pop_heap (oldheap);
1019 pthread_mutex_unlock (&am->vlib_rp->mutex);
1023 vl_api_to_api_string (u32 len, const char *buf, vl_api_string_t * str)
1025 clib_memcpy_fast (str->buf, buf, len);
1026 str->length = htonl (len);
1027 return len + sizeof (u32);
1031 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1033 u32 len = vec_len (vec);
1034 clib_memcpy (str->buf, vec, len);
1035 str->length = htonl (len);
1036 return len + sizeof (u32);
1039 /* Return a pointer to the API string (not nul terminated */
1041 vl_api_from_api_string (vl_api_string_t * astr)
1047 vl_api_string_len (vl_api_string_t * astr)
1049 return clib_net_to_host_u32 (astr->length);
1053 * Returns a new vector. Remember to free it after use.
1056 vl_api_from_api_to_vec (vl_api_string_t * astr)
1059 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1064 vl_api_set_elog_main (elog_main_t * m)
1066 api_main_t *am = &api_main;
1071 vl_api_set_elog_trace_api_messages (int enable)
1074 api_main_t *am = &api_main;
1076 rv = am->elog_trace_api_messages;
1077 am->elog_trace_api_messages = enable;
1082 vl_api_get_elog_trace_api_messages (void)
1084 api_main_t *am = &api_main;
1086 return am->elog_trace_api_messages;
1090 * fd.io coding-style-patch-verification: ON
1093 * eval: (c-set-style "gnu")