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 = ntohs (*((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 vec_free (*old_trace);
101 this_trace = old_trace;
104 length = clib_net_to_host_u32 (header->data_len);
106 vec_validate (msg_copy, length - 1);
107 clib_memcpy_fast (msg_copy, msg, length);
108 *this_trace = msg_copy;
112 vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
120 case VL_API_TRACE_TX:
124 vl_msg_api_trace_configure (am, which, 1024);
129 case VL_API_TRACE_RX:
133 vl_msg_api_trace_configure (am, which, 1024);
144 if (tp == 0 || tp->nitems == 0)
154 vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which)
161 case VL_API_TRACE_TX:
165 case VL_API_TRACE_RX:
175 if (!tp || tp->nitems == 0)
181 for (i = 0; i < vec_len (tp->traces); i++)
183 vec_free (tp->traces[i]);
185 vec_free (tp->traces);
191 vl_msg_api_trace_save (api_main_t * am, vl_api_trace_which_t which, FILE * fp)
194 vl_api_trace_file_header_t fh;
200 case VL_API_TRACE_TX:
204 case VL_API_TRACE_RX:
213 /* Configured, data present? */
214 if (tp == 0 || tp->nitems == 0 || vec_len (tp->traces) == 0)
217 /* "Dare to be stupid" check */
223 /* Write the file header */
224 fh.nitems = vec_len (tp->traces);
225 fh.endian = tp->endian;
226 fh.wrapped = tp->wrapped;
228 if (fwrite (&fh, sizeof (fh), 1, fp) != 1)
234 if (tp->wrapped == 0)
237 * Note: vec_len return 0 when fed a NULL pointer.
238 * Unfortunately, the static analysis tool doesn't
239 * figure it out, hence the suppressed warnings.
240 * What a great use of my time.
242 for (i = 0; i < vec_len (tp->traces); i++)
245 /*sa_ignore NO_NULL_CHK */
248 * This retarded check required to pass
254 msg_length = clib_host_to_net_u32 (vec_len (msg));
255 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
256 != sizeof (msg_length))
260 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
268 /* Wrap case: write oldest -> end of buffer */
269 for (i = tp->curindex; i < vec_len (tp->traces); i++)
274 * This retarded check required to pass
280 msg_length = clib_host_to_net_u32 (vec_len (msg));
281 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
282 != sizeof (msg_length))
287 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
292 /* write beginning of buffer -> oldest-1 */
293 for (i = 0; i < tp->curindex; i++)
296 /*sa_ignore NO_NULL_CHK */
299 * This retarded check required to pass
305 msg_length = clib_host_to_net_u32 (vec_len (msg));
306 if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
307 != sizeof (msg_length))
312 if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
322 vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
330 case VL_API_TRACE_TX:
334 vec_validate (am->tx_trace, 0);
339 case VL_API_TRACE_RX:
343 vec_validate (am->rx_trace, 0);
356 was_on = vl_msg_api_trace_onoff (am, which, 0);
360 vl_msg_api_trace_free (am, which);
363 clib_memset (tp, 0, sizeof (*tp));
365 if (clib_arch_is_big_endian)
367 tp->endian = VL_API_BIG_ENDIAN;
371 tp->endian = VL_API_LITTLE_ENDIAN;
377 (void) vl_msg_api_trace_onoff (am, which, was_on);
383 vl_msg_api_barrier_sync (void)
388 vl_msg_api_barrier_release (void)
393 msg_handler_internal (api_main_t * am,
394 void *the_msg, int trace_it, int do_it, int free_it)
396 u16 id = ntohs (*((u16 *) the_msg));
397 u8 *(*print_fp) (void *, void *);
399 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
402 vl_msg_api_trace (am, am->rx_trace, the_msg);
404 if (am->msg_print_flag)
406 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
407 print_fp = (void *) am->msg_print_handlers[id];
410 fformat (stdout, " [no registered print fn]\n");
414 (*print_fp) (the_msg, stdout);
420 if (!am->is_mp_safe[id])
422 vl_msg_api_barrier_trace_context (am->msg_names[id]);
423 vl_msg_api_barrier_sync ();
425 (*am->msg_handlers[id]) (the_msg);
426 if (!am->is_mp_safe[id])
427 vl_msg_api_barrier_release ();
432 clib_warning ("no handler for msg id %d", id);
436 vl_msg_api_free (the_msg);
440 elog_id_for_msg_name (vlib_main_t * vm, const char *msg_name)
447 h = hash_create_string (0, sizeof (uword));
449 p = hash_get_mem (h, msg_name);
452 r = elog_string (&vm->elog_main, "%s", msg_name);
454 name_copy = format (0, "%s%c", msg_name, 0);
456 hash_set_mem (h, name_copy, r);
461 /* This is only to be called from a vlib/vnet app */
463 vl_msg_api_handler_with_vm_node (api_main_t * am,
464 void *the_msg, vlib_main_t * vm,
465 vlib_node_runtime_t * node)
467 u16 id = ntohs (*((u16 *) the_msg));
468 u8 *(*handler) (void *, void *, void *);
469 u8 *(*print_fp) (void *, void *);
471 if (PREDICT_FALSE (vm->elog_trace_api_messages))
474 ELOG_TYPE_DECLARE (e) =
476 .format = "api-msg: %s",
484 ed = ELOG_DATA (&vm->elog_main, e);
485 if (id < vec_len (am->msg_names))
486 ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
488 ed->c = elog_id_for_msg_name (vm, "BOGUS");
491 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
493 handler = (void *) am->msg_handlers[id];
495 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
496 vl_msg_api_trace (am, am->rx_trace, the_msg);
498 if (PREDICT_FALSE (am->msg_print_flag))
500 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
501 print_fp = (void *) am->msg_print_handlers[id];
504 fformat (stdout, " [no registered print fn for msg %d]\n", id);
508 (*print_fp) (the_msg, vm);
512 if (!am->is_mp_safe[id])
514 vl_msg_api_barrier_trace_context (am->msg_names[id]);
515 vl_msg_api_barrier_sync ();
517 (*handler) (the_msg, vm, node);
518 if (!am->is_mp_safe[id])
519 vl_msg_api_barrier_release ();
523 clib_warning ("no handler for msg id %d", id);
527 * Special-case, so we can e.g. bounce messages off the vnet
528 * main thread without copying them...
530 if (!(am->message_bounce[id]))
531 vl_msg_api_free (the_msg);
533 if (PREDICT_FALSE (vm->elog_trace_api_messages))
536 ELOG_TYPE_DECLARE (e) = {
537 .format = "api-msg-done: %s",
546 ed = ELOG_DATA (&vm->elog_main, e);
547 if (id < vec_len (am->msg_names))
548 ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
550 ed->c = elog_id_for_msg_name (vm, "BOGUS");
555 vl_msg_api_handler (void *the_msg)
557 api_main_t *am = &api_main;
559 msg_handler_internal (am, the_msg,
561 && am->rx_trace->enabled) /* trace_it */ ,
562 1 /* do_it */ , 1 /* free_it */ );
566 vl_msg_api_handler_no_free (void *the_msg)
568 api_main_t *am = &api_main;
569 msg_handler_internal (am, the_msg,
571 && am->rx_trace->enabled) /* trace_it */ ,
572 1 /* do_it */ , 0 /* free_it */ );
576 vl_msg_api_handler_no_trace_no_free (void *the_msg)
578 api_main_t *am = &api_main;
579 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
584 * Add a trace record to the API message trace buffer, if
585 * API message tracing is enabled. Handy for adding sufficient
586 * data to the trace to reproduce autonomous state, as opposed to
587 * state downloaded via control-plane API messages. Example: the NAT
588 * application creates database entries based on packet traffic, not
589 * control-plane messages.
593 vl_msg_api_trace_only (void *the_msg)
595 api_main_t *am = &api_main;
597 msg_handler_internal (am, the_msg,
599 && am->rx_trace->enabled) /* trace_it */ ,
600 0 /* do_it */ , 0 /* free_it */ );
604 vl_msg_api_cleanup_handler (void *the_msg)
606 api_main_t *am = &api_main;
607 u16 id = ntohs (*((u16 *) the_msg));
609 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
611 clib_warning ("_vl_msg_id too large: %d\n", id);
614 if (am->msg_cleanup_handlers[id])
615 (*am->msg_cleanup_handlers[id]) (the_msg);
617 vl_msg_api_free (the_msg);
621 * vl_msg_api_replay_handler
624 vl_msg_api_replay_handler (void *the_msg)
626 api_main_t *am = &api_main;
628 u16 id = ntohs (*((u16 *) the_msg));
630 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
632 clib_warning ("_vl_msg_id too large: %d\n", id);
635 /* do NOT trace the message... */
636 if (am->msg_handlers[id])
637 (*am->msg_handlers[id]) (the_msg);
638 /* do NOT free the message buffer... */
642 vl_msg_api_get_msg_length (void *msg_arg)
644 return vl_msg_api_get_msg_length_inline (msg_arg);
648 * vl_msg_api_socket_handler
651 vl_msg_api_socket_handler (void *the_msg)
653 api_main_t *am = &api_main;
655 msg_handler_internal (am, the_msg,
657 && am->rx_trace->enabled) /* trace_it */ ,
658 1 /* do_it */ , 0 /* free_it */ );
661 #define foreach_msg_api_vector \
664 _(msg_cleanup_handlers) \
665 _(msg_endian_handlers) \
666 _(msg_print_handlers) \
672 vl_msg_api_config (vl_msg_api_msg_config_t * c)
674 api_main_t *am = &api_main;
677 * This happens during the java core tests if the message
678 * dictionary is missing newly added xxx_reply_t messages.
679 * Should never happen, but since I shot myself in the foot once
680 * this way, I thought I'd make it easy to debug if I ever do
681 * it again... (;-)...
686 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
688 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
689 clib_warning ("Did you forget to call setup_message_id_table?");
693 #define _(a) vec_validate (am->a, c->id);
694 foreach_msg_api_vector;
697 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
699 ("BUG: re-registering 'vl_api_%s_t_handler'."
700 "Handler was %llx, replaced by %llx",
701 c->name, am->msg_handlers[c->id], c->handler);
703 am->msg_names[c->id] = c->name;
704 am->msg_handlers[c->id] = c->handler;
705 am->msg_cleanup_handlers[c->id] = c->cleanup;
706 am->msg_endian_handlers[c->id] = c->endian;
707 am->msg_print_handlers[c->id] = c->print;
708 am->message_bounce[c->id] = c->message_bounce;
709 am->is_mp_safe[c->id] = c->is_mp_safe;
711 am->api_trace_cfg[c->id].size = c->size;
712 am->api_trace_cfg[c->id].trace_enable = c->traced;
713 am->api_trace_cfg[c->id].replay_enable = c->replay;
717 * vl_msg_api_set_handlers
718 * preserve the old API for a while
721 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
722 void *endian, void *print, int size, int traced)
724 vl_msg_api_msg_config_t cfg;
725 vl_msg_api_msg_config_t *c = &cfg;
727 clib_memset (c, 0, sizeof (*c));
731 c->handler = handler;
732 c->cleanup = cleanup;
737 c->message_bounce = 0;
739 vl_msg_api_config (c);
743 vl_msg_api_clean_handlers (int msg_id)
745 vl_msg_api_msg_config_t cfg;
746 vl_msg_api_msg_config_t *c = &cfg;
748 clib_memset (c, 0, sizeof (*c));
751 vl_msg_api_config (c);
755 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
757 api_main_t *am = &api_main;
760 vec_validate (am->msg_cleanup_handlers, msg_id);
761 am->msg_cleanup_handlers[msg_id] = fp;
765 vl_msg_api_queue_handler (svm_queue_t * q)
769 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
770 vl_msg_api_handler ((void *) msg);
774 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
778 case VL_API_TRACE_RX:
780 case VL_API_TRACE_TX:
788 vl_noop_handler (void *mp)
793 static u8 post_mortem_dump_enabled;
796 vl_msg_api_post_mortem_dump_enable_disable (int enable)
798 post_mortem_dump_enabled = enable;
802 vl_msg_api_post_mortem_dump (void)
804 api_main_t *am = &api_main;
809 if (post_mortem_dump_enabled == 0)
812 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
815 fp = fopen (filename, "w");
818 rv = write (2, "Couldn't create ", 16);
819 rv = write (2, filename, strlen (filename));
820 rv = write (2, "\n", 1);
823 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
827 rv = write (2, "Failed to save post-mortem API trace to ", 40);
828 rv = write (2, filename, strlen (filename));
829 rv = write (2, "\n", 1);
834 /* Layered message handling support */
837 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
839 api_main_t *am = &api_main;
841 /* Mild idiot proofing */
842 if (msg_id_host_byte_order > 10000)
843 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
844 msg_id_host_byte_order,
845 clib_net_to_host_u16 (msg_id_host_byte_order));
846 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
847 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
851 vl_msg_api_pd_handler (void *mp, int rv)
853 api_main_t *am = &api_main;
854 int (*fp) (void *, int);
857 if (clib_arch_is_little_endian)
858 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
860 msg_id = *((u16 *) mp);
862 if (msg_id >= vec_len (am->pd_msg_handlers)
863 || am->pd_msg_handlers[msg_id] == 0)
866 fp = am->pd_msg_handlers[msg_id];
872 vl_msg_api_set_first_available_msg_id (u16 first_avail)
874 api_main_t *am = &api_main;
876 am->first_available_msg_id = first_avail;
880 vl_msg_api_get_msg_ids (const char *name, int n)
882 api_main_t *am = &api_main;
884 vl_api_msg_range_t *rp;
888 if (am->msg_range_by_name == 0)
889 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
891 name_copy = format (0, "%s%c", name, 0);
893 p = hash_get_mem (am->msg_range_by_name, name_copy);
896 clib_warning ("WARNING: duplicate message range registration for '%s'",
898 vec_free (name_copy);
902 if (n < 0 || n > 1024)
905 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
907 vec_free (name_copy);
911 vec_add2 (am->msg_ranges, rp, 1);
913 rv = rp->first_msg_id = am->first_available_msg_id;
914 am->first_available_msg_id += n;
915 rp->last_msg_id = am->first_available_msg_id - 1;
916 rp->name = name_copy;
918 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
924 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
928 if (am->msg_index_by_name_and_crc == 0)
929 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
931 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
934 clib_warning ("attempt to redefine '%s' ignored...", string);
938 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
942 vl_msg_api_add_version (api_main_t * am, const char *string,
943 u32 major, u32 minor, u32 patch)
945 api_version_t version = {.major = major,.minor = minor,.patch = patch };
946 ASSERT (strlen (string) < 64);
947 strncpy (version.name, string, 64 - 1);
948 vec_add1 (am->api_version_list, version);
952 vl_msg_api_get_msg_index (u8 * name_and_crc)
954 api_main_t *am = &api_main;
957 if (am->msg_index_by_name_and_crc)
959 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
967 vl_msg_push_heap (void)
969 api_main_t *am = &api_main;
970 pthread_mutex_lock (&am->vlib_rp->mutex);
971 return svm_push_data_heap (am->vlib_rp);
975 vl_msg_pop_heap (void *oldheap)
977 api_main_t *am = &api_main;
978 svm_pop_heap (oldheap);
979 pthread_mutex_unlock (&am->vlib_rp->mutex);
984 * fd.io coding-style-patch-verification: ON
987 * eval: (c-set-style "gnu")