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 /* 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 = ntohs (*((u16 *) the_msg));
399 u8 *(*print_fp) (void *, void *);
401 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
404 vl_msg_api_trace (am, am->rx_trace, the_msg);
406 if (am->msg_print_flag)
408 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
409 print_fp = (void *) am->msg_print_handlers[id];
412 fformat (stdout, " [no registered print fn]\n");
416 (*print_fp) (the_msg, stdout);
422 if (!am->is_mp_safe[id])
424 vl_msg_api_barrier_trace_context (am->msg_names[id]);
425 vl_msg_api_barrier_sync ();
427 (*am->msg_handlers[id]) (the_msg);
428 if (!am->is_mp_safe[id])
429 vl_msg_api_barrier_release ();
434 clib_warning ("no handler for msg id %d", id);
438 vl_msg_api_free (the_msg);
442 elog_id_for_msg_name (vlib_main_t * vm, const char *msg_name)
449 h = hash_create_string (0, sizeof (uword));
451 p = hash_get_mem (h, msg_name);
454 r = elog_string (&vm->elog_main, "%s", msg_name);
456 name_copy = format (0, "%s%c", msg_name, 0);
458 hash_set_mem (h, name_copy, r);
463 /* This is only to be called from a vlib/vnet app */
465 vl_msg_api_handler_with_vm_node (api_main_t * am,
466 void *the_msg, vlib_main_t * vm,
467 vlib_node_runtime_t * node)
469 u16 id = ntohs (*((u16 *) the_msg));
470 u8 *(*handler) (void *, void *, void *);
471 u8 *(*print_fp) (void *, void *);
473 if (PREDICT_FALSE (vm->elog_trace_api_messages))
476 ELOG_TYPE_DECLARE (e) =
478 .format = "api-msg: %s",
486 ed = ELOG_DATA (&vm->elog_main, e);
487 if (id < vec_len (am->msg_names))
488 ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
490 ed->c = elog_id_for_msg_name (vm, "BOGUS");
493 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
495 handler = (void *) am->msg_handlers[id];
497 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
498 vl_msg_api_trace (am, am->rx_trace, the_msg);
500 if (PREDICT_FALSE (am->msg_print_flag))
502 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
503 print_fp = (void *) am->msg_print_handlers[id];
506 fformat (stdout, " [no registered print fn for msg %d]\n", id);
510 (*print_fp) (the_msg, vm);
514 if (!am->is_mp_safe[id])
516 vl_msg_api_barrier_trace_context (am->msg_names[id]);
517 vl_msg_api_barrier_sync ();
519 (*handler) (the_msg, vm, node);
520 if (!am->is_mp_safe[id])
521 vl_msg_api_barrier_release ();
525 clib_warning ("no handler for msg id %d", id);
529 * Special-case, so we can e.g. bounce messages off the vnet
530 * main thread without copying them...
532 if (!(am->message_bounce[id]))
533 vl_msg_api_free (the_msg);
535 if (PREDICT_FALSE (vm->elog_trace_api_messages))
538 ELOG_TYPE_DECLARE (e) = {
539 .format = "api-msg-done: %s",
548 ed = ELOG_DATA (&vm->elog_main, e);
549 if (id < vec_len (am->msg_names))
550 ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
552 ed->c = elog_id_for_msg_name (vm, "BOGUS");
557 vl_msg_api_handler (void *the_msg)
559 api_main_t *am = &api_main;
561 msg_handler_internal (am, the_msg,
563 && am->rx_trace->enabled) /* trace_it */ ,
564 1 /* do_it */ , 1 /* free_it */ );
568 vl_msg_api_handler_no_free (void *the_msg)
570 api_main_t *am = &api_main;
571 msg_handler_internal (am, the_msg,
573 && am->rx_trace->enabled) /* trace_it */ ,
574 1 /* do_it */ , 0 /* free_it */ );
578 vl_msg_api_handler_no_trace_no_free (void *the_msg)
580 api_main_t *am = &api_main;
581 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
586 * Add a trace record to the API message trace buffer, if
587 * API message tracing is enabled. Handy for adding sufficient
588 * data to the trace to reproduce autonomous state, as opposed to
589 * state downloaded via control-plane API messages. Example: the NAT
590 * application creates database entries based on packet traffic, not
591 * control-plane messages.
595 vl_msg_api_trace_only (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 0 /* do_it */ , 0 /* free_it */ );
606 vl_msg_api_cleanup_handler (void *the_msg)
608 api_main_t *am = &api_main;
609 u16 id = ntohs (*((u16 *) the_msg));
611 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
613 clib_warning ("_vl_msg_id too large: %d\n", id);
616 if (am->msg_cleanup_handlers[id])
617 (*am->msg_cleanup_handlers[id]) (the_msg);
619 vl_msg_api_free (the_msg);
623 * vl_msg_api_replay_handler
626 vl_msg_api_replay_handler (void *the_msg)
628 api_main_t *am = &api_main;
630 u16 id = ntohs (*((u16 *) the_msg));
632 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
634 clib_warning ("_vl_msg_id too large: %d\n", id);
637 /* do NOT trace the message... */
638 if (am->msg_handlers[id])
639 (*am->msg_handlers[id]) (the_msg);
640 /* do NOT free the message buffer... */
644 vl_msg_api_get_msg_length (void *msg_arg)
646 return vl_msg_api_get_msg_length_inline (msg_arg);
650 * vl_msg_api_socket_handler
653 vl_msg_api_socket_handler (void *the_msg)
655 api_main_t *am = &api_main;
657 msg_handler_internal (am, the_msg,
659 && am->rx_trace->enabled) /* trace_it */ ,
660 1 /* do_it */ , 0 /* free_it */ );
663 #define foreach_msg_api_vector \
666 _(msg_cleanup_handlers) \
667 _(msg_endian_handlers) \
668 _(msg_print_handlers) \
674 vl_msg_api_config (vl_msg_api_msg_config_t * c)
676 api_main_t *am = &api_main;
679 * This happens during the java core tests if the message
680 * dictionary is missing newly added xxx_reply_t messages.
681 * Should never happen, but since I shot myself in the foot once
682 * this way, I thought I'd make it easy to debug if I ever do
683 * it again... (;-)...
688 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
690 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
691 clib_warning ("Did you forget to call setup_message_id_table?");
695 #define _(a) vec_validate (am->a, c->id);
696 foreach_msg_api_vector;
699 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
701 ("BUG: re-registering 'vl_api_%s_t_handler'."
702 "Handler was %llx, replaced by %llx",
703 c->name, am->msg_handlers[c->id], c->handler);
705 am->msg_names[c->id] = c->name;
706 am->msg_handlers[c->id] = c->handler;
707 am->msg_cleanup_handlers[c->id] = c->cleanup;
708 am->msg_endian_handlers[c->id] = c->endian;
709 am->msg_print_handlers[c->id] = c->print;
710 am->message_bounce[c->id] = c->message_bounce;
711 am->is_mp_safe[c->id] = c->is_mp_safe;
713 am->api_trace_cfg[c->id].size = c->size;
714 am->api_trace_cfg[c->id].trace_enable = c->traced;
715 am->api_trace_cfg[c->id].replay_enable = c->replay;
719 * vl_msg_api_set_handlers
720 * preserve the old API for a while
723 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
724 void *endian, void *print, int size, int traced)
726 vl_msg_api_msg_config_t cfg;
727 vl_msg_api_msg_config_t *c = &cfg;
729 clib_memset (c, 0, sizeof (*c));
733 c->handler = handler;
734 c->cleanup = cleanup;
739 c->message_bounce = 0;
741 vl_msg_api_config (c);
745 vl_msg_api_clean_handlers (int msg_id)
747 vl_msg_api_msg_config_t cfg;
748 vl_msg_api_msg_config_t *c = &cfg;
750 clib_memset (c, 0, sizeof (*c));
753 vl_msg_api_config (c);
757 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
759 api_main_t *am = &api_main;
762 vec_validate (am->msg_cleanup_handlers, msg_id);
763 am->msg_cleanup_handlers[msg_id] = fp;
767 vl_msg_api_queue_handler (svm_queue_t * q)
771 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
772 vl_msg_api_handler ((void *) msg);
776 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
780 case VL_API_TRACE_RX:
782 case VL_API_TRACE_TX:
790 vl_noop_handler (void *mp)
795 static u8 post_mortem_dump_enabled;
798 vl_msg_api_post_mortem_dump_enable_disable (int enable)
800 post_mortem_dump_enabled = enable;
804 vl_msg_api_post_mortem_dump (void)
806 api_main_t *am = &api_main;
811 if (post_mortem_dump_enabled == 0)
814 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
817 fp = fopen (filename, "w");
820 rv = write (2, "Couldn't create ", 16);
821 rv = write (2, filename, strlen (filename));
822 rv = write (2, "\n", 1);
825 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
829 rv = write (2, "Failed to save post-mortem API trace to ", 40);
830 rv = write (2, filename, strlen (filename));
831 rv = write (2, "\n", 1);
836 /* Layered message handling support */
839 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
841 api_main_t *am = &api_main;
843 /* Mild idiot proofing */
844 if (msg_id_host_byte_order > 10000)
845 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
846 msg_id_host_byte_order,
847 clib_net_to_host_u16 (msg_id_host_byte_order));
848 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
849 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
853 vl_msg_api_pd_handler (void *mp, int rv)
855 api_main_t *am = &api_main;
856 int (*fp) (void *, int);
859 if (clib_arch_is_little_endian)
860 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
862 msg_id = *((u16 *) mp);
864 if (msg_id >= vec_len (am->pd_msg_handlers)
865 || am->pd_msg_handlers[msg_id] == 0)
868 fp = am->pd_msg_handlers[msg_id];
874 vl_msg_api_set_first_available_msg_id (u16 first_avail)
876 api_main_t *am = &api_main;
878 am->first_available_msg_id = first_avail;
882 vl_msg_api_get_msg_ids (const char *name, int n)
884 api_main_t *am = &api_main;
886 vl_api_msg_range_t *rp;
890 if (am->msg_range_by_name == 0)
891 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
893 name_copy = format (0, "%s%c", name, 0);
895 p = hash_get_mem (am->msg_range_by_name, name_copy);
898 clib_warning ("WARNING: duplicate message range registration for '%s'",
900 vec_free (name_copy);
904 if (n < 0 || n > 1024)
907 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
909 vec_free (name_copy);
913 vec_add2 (am->msg_ranges, rp, 1);
915 rv = rp->first_msg_id = am->first_available_msg_id;
916 am->first_available_msg_id += n;
917 rp->last_msg_id = am->first_available_msg_id - 1;
918 rp->name = name_copy;
920 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
926 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
930 if (am->msg_index_by_name_and_crc == 0)
931 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
933 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
936 clib_warning ("attempt to redefine '%s' ignored...", string);
940 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
944 vl_msg_api_add_version (api_main_t * am, const char *string,
945 u32 major, u32 minor, u32 patch)
947 api_version_t version = {.major = major,.minor = minor,.patch = patch };
948 ASSERT (strlen (string) < 64);
949 strncpy (version.name, string, 64 - 1);
950 vec_add1 (am->api_version_list, version);
954 vl_msg_api_get_msg_index (u8 * name_and_crc)
956 api_main_t *am = &api_main;
959 if (am->msg_index_by_name_and_crc)
961 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
969 vl_msg_push_heap (void)
971 api_main_t *am = &api_main;
972 pthread_mutex_lock (&am->vlib_rp->mutex);
973 return svm_push_data_heap (am->vlib_rp);
977 vl_msg_pop_heap (void *oldheap)
979 api_main_t *am = &api_main;
980 svm_pop_heap (oldheap);
981 pthread_mutex_unlock (&am->vlib_rp->mutex);
986 * fd.io coding-style-patch-verification: ON
989 * eval: (c-set-style "gnu")