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 (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 = clib_net_to_host_u16 (*((u16 *) the_msg));
470 u8 *(*handler) (void *, void *, void *);
471 u8 *(*print_fp) (void *, void *);
474 if (PREDICT_FALSE (vm->elog_trace_api_messages))
477 ELOG_TYPE_DECLARE (e) =
479 .format = "api-msg: %s",
487 ed = ELOG_DATA (&vm->elog_main, e);
488 if (id < vec_len (am->msg_names))
489 ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
491 ed->c = elog_id_for_msg_name (vm, "BOGUS");
494 if (id < vec_len (am->msg_handlers) && am->msg_handlers[id])
496 handler = (void *) am->msg_handlers[id];
498 if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled))
499 vl_msg_api_trace (am, am->rx_trace, the_msg);
501 if (PREDICT_FALSE (am->msg_print_flag))
503 fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]);
504 print_fp = (void *) am->msg_print_handlers[id];
507 fformat (stdout, " [no registered print fn for msg %d]\n", id);
511 (*print_fp) (the_msg, vm);
514 is_mp_safe = am->is_mp_safe[id];
518 vl_msg_api_barrier_trace_context (am->msg_names[id]);
519 vl_msg_api_barrier_sync ();
521 (*handler) (the_msg, vm, node);
523 vl_msg_api_barrier_release ();
527 clib_warning ("no handler for msg id %d", id);
531 * Special-case, so we can e.g. bounce messages off the vnet
532 * main thread without copying them...
534 if (!(am->message_bounce[id]))
535 vl_msg_api_free (the_msg);
537 if (PREDICT_FALSE (vm->elog_trace_api_messages))
540 ELOG_TYPE_DECLARE (e) =
542 .format = "api-msg-done(%s): %s",
543 .format_args = "t4T4",
558 ed = ELOG_DATA (&vm->elog_main, e);
559 if (id < vec_len (am->msg_names))
560 ed->c = elog_id_for_msg_name (vm, (const char *) am->msg_names[id]);
562 ed->c = elog_id_for_msg_name (vm, "BOGUS");
563 ed->barrier = is_mp_safe;
568 vl_msg_api_handler (void *the_msg)
570 api_main_t *am = &api_main;
572 msg_handler_internal (am, the_msg,
574 && am->rx_trace->enabled) /* trace_it */ ,
575 1 /* do_it */ , 1 /* free_it */ );
579 vl_msg_api_handler_no_free (void *the_msg)
581 api_main_t *am = &api_main;
582 msg_handler_internal (am, the_msg,
584 && am->rx_trace->enabled) /* trace_it */ ,
585 1 /* do_it */ , 0 /* free_it */ );
589 vl_msg_api_handler_no_trace_no_free (void *the_msg)
591 api_main_t *am = &api_main;
592 msg_handler_internal (am, the_msg, 0 /* trace_it */ , 1 /* do_it */ ,
597 * Add a trace record to the API message trace buffer, if
598 * API message tracing is enabled. Handy for adding sufficient
599 * data to the trace to reproduce autonomous state, as opposed to
600 * state downloaded via control-plane API messages. Example: the NAT
601 * application creates database entries based on packet traffic, not
602 * control-plane messages.
606 vl_msg_api_trace_only (void *the_msg)
608 api_main_t *am = &api_main;
610 msg_handler_internal (am, the_msg,
612 && am->rx_trace->enabled) /* trace_it */ ,
613 0 /* do_it */ , 0 /* free_it */ );
617 vl_msg_api_cleanup_handler (void *the_msg)
619 api_main_t *am = &api_main;
620 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
622 if (PREDICT_FALSE (id >= vec_len (am->msg_cleanup_handlers)))
624 clib_warning ("_vl_msg_id too large: %d\n", id);
627 if (am->msg_cleanup_handlers[id])
628 (*am->msg_cleanup_handlers[id]) (the_msg);
630 vl_msg_api_free (the_msg);
634 * vl_msg_api_replay_handler
637 vl_msg_api_replay_handler (void *the_msg)
639 api_main_t *am = &api_main;
641 u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
643 if (PREDICT_FALSE (id >= vec_len (am->msg_handlers)))
645 clib_warning ("_vl_msg_id too large: %d\n", id);
648 /* do NOT trace the message... */
649 if (am->msg_handlers[id])
650 (*am->msg_handlers[id]) (the_msg);
651 /* do NOT free the message buffer... */
655 vl_msg_api_get_msg_length (void *msg_arg)
657 return vl_msg_api_get_msg_length_inline (msg_arg);
661 * vl_msg_api_socket_handler
664 vl_msg_api_socket_handler (void *the_msg)
666 api_main_t *am = &api_main;
668 msg_handler_internal (am, the_msg,
670 && am->rx_trace->enabled) /* trace_it */ ,
671 1 /* do_it */ , 0 /* free_it */ );
674 #define foreach_msg_api_vector \
677 _(msg_cleanup_handlers) \
678 _(msg_endian_handlers) \
679 _(msg_print_handlers) \
685 vl_msg_api_config (vl_msg_api_msg_config_t * c)
687 api_main_t *am = &api_main;
690 * This happens during the java core tests if the message
691 * dictionary is missing newly added xxx_reply_t messages.
692 * Should never happen, but since I shot myself in the foot once
693 * this way, I thought I'd make it easy to debug if I ever do
694 * it again... (;-)...
699 clib_warning ("Trying to register %s with a NULL msg id!", c->name);
701 clib_warning ("Trying to register a NULL msg with a NULL msg id!");
702 clib_warning ("Did you forget to call setup_message_id_table?");
706 #define _(a) vec_validate (am->a, c->id);
707 foreach_msg_api_vector;
710 if (am->msg_handlers[c->id] && am->msg_handlers[c->id] != c->handler)
712 ("BUG: re-registering 'vl_api_%s_t_handler'."
713 "Handler was %llx, replaced by %llx",
714 c->name, am->msg_handlers[c->id], c->handler);
716 am->msg_names[c->id] = c->name;
717 am->msg_handlers[c->id] = c->handler;
718 am->msg_cleanup_handlers[c->id] = c->cleanup;
719 am->msg_endian_handlers[c->id] = c->endian;
720 am->msg_print_handlers[c->id] = c->print;
721 am->message_bounce[c->id] = c->message_bounce;
722 am->is_mp_safe[c->id] = c->is_mp_safe;
724 am->api_trace_cfg[c->id].size = c->size;
725 am->api_trace_cfg[c->id].trace_enable = c->traced;
726 am->api_trace_cfg[c->id].replay_enable = c->replay;
730 * vl_msg_api_set_handlers
731 * preserve the old API for a while
734 vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
735 void *endian, void *print, int size, int traced)
737 vl_msg_api_msg_config_t cfg;
738 vl_msg_api_msg_config_t *c = &cfg;
740 clib_memset (c, 0, sizeof (*c));
744 c->handler = handler;
745 c->cleanup = cleanup;
750 c->message_bounce = 0;
752 vl_msg_api_config (c);
756 vl_msg_api_clean_handlers (int msg_id)
758 vl_msg_api_msg_config_t cfg;
759 vl_msg_api_msg_config_t *c = &cfg;
761 clib_memset (c, 0, sizeof (*c));
764 vl_msg_api_config (c);
768 vl_msg_api_set_cleanup_handler (int msg_id, void *fp)
770 api_main_t *am = &api_main;
773 vec_validate (am->msg_cleanup_handlers, msg_id);
774 am->msg_cleanup_handlers[msg_id] = fp;
778 vl_msg_api_queue_handler (svm_queue_t * q)
782 while (!svm_queue_sub (q, (u8 *) & msg, SVM_Q_WAIT, 0))
783 vl_msg_api_handler ((void *) msg);
787 vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which)
791 case VL_API_TRACE_RX:
793 case VL_API_TRACE_TX:
801 vl_noop_handler (void *mp)
806 static u8 post_mortem_dump_enabled;
809 vl_msg_api_post_mortem_dump_enable_disable (int enable)
811 post_mortem_dump_enabled = enable;
815 vl_msg_api_post_mortem_dump (void)
817 api_main_t *am = &api_main;
822 if (post_mortem_dump_enabled == 0)
825 snprintf (filename, sizeof (filename), "/tmp/api_post_mortem.%d",
828 fp = fopen (filename, "w");
831 rv = write (2, "Couldn't create ", 16);
832 rv = write (2, filename, strlen (filename));
833 rv = write (2, "\n", 1);
836 rv = vl_msg_api_trace_save (am, VL_API_TRACE_RX, fp);
840 rv = write (2, "Failed to save post-mortem API trace to ", 40);
841 rv = write (2, filename, strlen (filename));
842 rv = write (2, "\n", 1);
847 /* Layered message handling support */
850 vl_msg_api_register_pd_handler (void *fp, u16 msg_id_host_byte_order)
852 api_main_t *am = &api_main;
854 /* Mild idiot proofing */
855 if (msg_id_host_byte_order > 10000)
856 clib_warning ("msg_id_host_byte_order endian issue? %d arg vs %d",
857 msg_id_host_byte_order,
858 clib_net_to_host_u16 (msg_id_host_byte_order));
859 vec_validate (am->pd_msg_handlers, msg_id_host_byte_order);
860 am->pd_msg_handlers[msg_id_host_byte_order] = fp;
864 vl_msg_api_pd_handler (void *mp, int rv)
866 api_main_t *am = &api_main;
867 int (*fp) (void *, int);
870 if (clib_arch_is_little_endian)
871 msg_id = clib_net_to_host_u16 (*((u16 *) mp));
873 msg_id = *((u16 *) mp);
875 if (msg_id >= vec_len (am->pd_msg_handlers)
876 || am->pd_msg_handlers[msg_id] == 0)
879 fp = am->pd_msg_handlers[msg_id];
885 vl_msg_api_set_first_available_msg_id (u16 first_avail)
887 api_main_t *am = &api_main;
889 am->first_available_msg_id = first_avail;
893 vl_msg_api_get_msg_ids (const char *name, int n)
895 api_main_t *am = &api_main;
897 vl_api_msg_range_t *rp;
901 if (am->msg_range_by_name == 0)
902 am->msg_range_by_name = hash_create_string (0, sizeof (uword));
904 name_copy = format (0, "%s%c", name, 0);
906 p = hash_get_mem (am->msg_range_by_name, name_copy);
909 clib_warning ("WARNING: duplicate message range registration for '%s'",
911 vec_free (name_copy);
915 if (n < 0 || n > 1024)
918 ("WARNING: bad number of message-IDs (%d) requested by '%s'",
920 vec_free (name_copy);
924 vec_add2 (am->msg_ranges, rp, 1);
926 rv = rp->first_msg_id = am->first_available_msg_id;
927 am->first_available_msg_id += n;
928 rp->last_msg_id = am->first_available_msg_id - 1;
929 rp->name = name_copy;
931 hash_set_mem (am->msg_range_by_name, name_copy, rp - am->msg_ranges);
937 vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string, u32 id)
941 if (am->msg_index_by_name_and_crc == 0)
942 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
944 p = hash_get_mem (am->msg_index_by_name_and_crc, string);
947 clib_warning ("attempt to redefine '%s' ignored...", string);
951 hash_set_mem (am->msg_index_by_name_and_crc, string, id);
955 vl_msg_api_add_version (api_main_t * am, const char *string,
956 u32 major, u32 minor, u32 patch)
958 api_version_t version = {.major = major,.minor = minor,.patch = patch };
959 ASSERT (strlen (string) < 64);
960 strncpy (version.name, string, 64 - 1);
961 vec_add1 (am->api_version_list, version);
965 vl_msg_api_get_msg_index (u8 * name_and_crc)
967 api_main_t *am = &api_main;
970 if (am->msg_index_by_name_and_crc)
972 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
980 vl_msg_push_heap (void)
982 api_main_t *am = &api_main;
983 pthread_mutex_lock (&am->vlib_rp->mutex);
984 return svm_push_data_heap (am->vlib_rp);
988 vl_msg_pop_heap (void *oldheap)
990 api_main_t *am = &api_main;
991 svm_pop_heap (oldheap);
992 pthread_mutex_unlock (&am->vlib_rp->mutex);
996 vl_api_to_api_string (u32 len, const char *buf, vl_api_string_t * str)
998 clib_memcpy_fast (str->buf, buf, len);
999 str->length = htonl (len);
1000 return len + sizeof (u32);
1004 vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
1006 u32 len = vec_len (vec);
1007 clib_memcpy (str->buf, vec, len);
1008 str->length = htonl (len);
1009 return len + sizeof (u32);
1012 /* Return a pointer to the API string (not nul terminated */
1014 vl_api_from_api_string (vl_api_string_t * astr)
1020 vl_api_string_len (vl_api_string_t * astr)
1022 return clib_net_to_host_u32 (astr->length);
1026 * Returns a new vector. Remember to free it after use.
1029 vl_api_from_api_to_vec (vl_api_string_t * astr)
1032 vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
1037 * fd.io coding-style-patch-verification: ON
1040 * eval: (c-set-style "gnu")