/* *------------------------------------------------------------------ * memory_vlib.c * * Copyright (c) 2009 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define TRACE_VLIB_MEMORY_QUEUE 0 #include /* enumerate all vlib messages */ #define vl_typedefs /* define message structures */ #include #undef vl_typedefs /* instantiate all the print functions we know about */ #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) #define vl_printfun #include #undef vl_printfun static inline void * vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle) { vl_print (handle, "vl_api_memclnt_create_t:\n"); vl_print (handle, "name: %s\n", a->name); vl_print (handle, "input_queue: 0x%wx\n", a->input_queue); vl_print (handle, "context: %u\n", (unsigned) a->context); vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota); return handle; } static inline void * vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle) { vl_print (handle, "vl_api_memclnt_delete_t:\n"); vl_print (handle, "index: %u\n", (unsigned) a->index); vl_print (handle, "handle: 0x%wx\n", a->handle); return handle; } static inline void * vl_api_trace_plugin_msg_ids_t_print (vl_api_trace_plugin_msg_ids_t * a, void *handle) { vl_print (handle, "vl_api_trace_plugin_msg_ids: %s first %u last %u\n", a->plugin_name, clib_host_to_net_u16 (a->first_msg_id), clib_host_to_net_u16 (a->last_msg_id)); return handle; } /* instantiate all the endian swap functions we know about */ #define vl_endianfun #include #undef vl_endianfun void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem) __attribute__ ((weak)); void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem) { static int count; if (count++ < 5) clib_warning ("need to link against -lvlibsocket, msg not sent!"); } void vl_msg_api_send (vl_api_registration_t * rp, u8 * elem) { if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM)) { vl_socket_api_send (rp, elem); } else { vl_msg_api_send_shmem (rp->vl_input_queue, elem); } } u8 * vl_api_serialize_message_table (api_main_t * am, u8 * vector) { serialize_main_t _sm, *sm = &_sm; hash_pair_t *hp; u32 nmsg = hash_elts (am->msg_index_by_name_and_crc); serialize_open_vector (sm, vector); /* serialize the count */ serialize_integer (sm, nmsg, sizeof (u32)); /* *INDENT-OFF* */ hash_foreach_pair (hp, am->msg_index_by_name_and_crc, ({ serialize_likely_small_unsigned_integer (sm, hp->value[0]); serialize_cstring (sm, (char *) hp->key); })); /* *INDENT-ON* */ return serialize_close_vector (sm); } /* * vl_api_memclnt_create_t_handler */ void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp) { vl_api_registration_t **regpp; vl_api_registration_t *regp; vl_api_memclnt_create_reply_t *rp; svm_region_t *svm; unix_shared_memory_queue_t *q; int rv = 0; void *oldheap; api_main_t *am = &api_main; u8 *serialized_message_table = 0; /* * This is tortured. Maintain a vlib-address-space private * pool of client registrations. We use the shared-memory virtual * address of client structure as a handle, to allow direct * manipulation of context quota vbls from the client library. * * This scheme causes trouble w/ API message trace replay, since * some random VA from clib_mem_alloc() certainly won't * occur in the Linux sim. The (very) few places * that care need to use the pool index. * * Putting the registration object(s) into a pool in shared memory and * using the pool index as a handle seems like a great idea. * Unfortunately, each and every reference to that pool would need * to be protected by a mutex: * * Client VLIB * ------ ---- * convert pool index to * pointer. * * expand pool * * kaboom! */ pool_get (am->vl_clients, regpp); svm = am->vlib_rp; if (am->serialized_message_table_in_shmem == 0) serialized_message_table = vl_api_serialize_message_table (am, 0); pthread_mutex_lock (&svm->mutex); oldheap = svm_push_data_heap (svm); *regpp = clib_mem_alloc (sizeof (vl_api_registration_t)); regp = *regpp; memset (regp, 0, sizeof (*regp)); regp->registration_type = REGISTRATION_TYPE_SHMEM; regp->vl_api_registration_pool_index = regpp - am->vl_clients; q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword) mp->input_queue; regp->name = format (0, "%s", mp->name); vec_add1 (regp->name, 0); if (serialized_message_table) am->serialized_message_table_in_shmem = vec_dup (serialized_message_table); pthread_mutex_unlock (&svm->mutex); svm_pop_heap (oldheap); vec_free (serialized_message_table); rp = vl_msg_api_alloc (sizeof (*rp)); rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY); rp->handle = (uword) regp; rp->index = vl_msg_api_handle_from_index_and_epoch (regp->vl_api_registration_pool_index, am->shmem_hdr->application_restarts); rp->context = mp->context; rp->response = ntohl (rv); rp->message_table = (u64) am->serialized_message_table_in_shmem; vl_msg_api_send_shmem (q, (u8 *) & rp); } static int call_reaper_functions (u32 client_index) { clib_error_t *error = 0; _vl_msg_api_function_list_elt_t *i; i = api_main.reaper_function_registrations; while (i) { error = i->f (client_index); if (error) clib_error_report (error); i = i->next_init_function; } return 0; } /* * vl_api_memclnt_delete_t_handler */ void vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp) { vl_api_registration_t **regpp; vl_api_registration_t *regp; vl_api_memclnt_delete_rep
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation
 */

#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <dirent.h>

#include <rte_log.h>
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_common.h>
#include <rte_debug.h>

#include "eal_private.h"
#include "eal_thread.h"

/*
 * Parse /sys/devices/system/cpu to get the number of physical and logical
 * processors on the machine. The function will fill the cpu_info
 * structure.
 */
int
rte_eal_cpu_init(void)
{
	/* pointer to global configuration */
	struct rte_config *config = rte_eal_get_configuration();
	unsigned lcore_id;
	unsigned count = 0;

	/*
	 * Parse the maximum set of logical cores, detect the subset of running
	 * ones and enable them by default.
	 */
	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
		lcore_config[lcore_id].core_index = count;

		/* init cpuset for per lcore config */
		CPU_ZERO(&lcore_config[lcore_id].cpuset);

		/* in 1:1 mapping, record related cpu detected state */
		lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
		if (lcore_config[lcore_id].detected == 0) {
			config->lcore_role[lcore_id] = ROLE_OFF;
			lcore_config[lcore_id].core_index = -1;
			continue;
		}

		/* By default, lcore 1:1 map to cpu id */
		CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);

		/* By default, each detected core is enabled */
		config->lcore_role[lcore_id] = ROLE_RTE;
		lcore_config[lcore_id].core_role = ROLE_RTE;
		lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
		lcore_config[lcore_id].socket_id = eal_cpu_socket_id(lcore_id);
		if (lcore_config[lcore_id].socket_id >= RTE_MAX_NUMA_NODES) {
#ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
			lcore_config[lcore_id].socket_id = 0;
#else
			RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than "
				"RTE_MAX_NUMA_NODES (%d)\n",
				lcore_config[lcore_id].socket_id,
				RTE_MAX_NUMA_NODES);
			return -1;
#endif
		}
		RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
				"core %u on socket %u\n",
				lcore_id, lcore_config[lcore_id].core_id,
				lcore_config[lcore_id].socket_id);
		count++;
	}
	/* Set the count of enabled logical cores of the EAL configuration */
	config->lcore_count = count;
	RTE_LOG(DEBUG, EAL,
		"Support maximum %u logical core(s) by configuration.\n",
		RTE_MAX_LCORE);
	RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);

	return 0;
}
m, "RX Trace disabled\n"); else vlib_cli_output (vm, "RX Trace enabled\n"); } if (am->tx_trace == 0) { vlib_cli_output (vm, "TX Trace disabled\n"); } else { if (am->tx_trace->enabled == 0) vlib_cli_output (vm, "TX Trace disabled\n"); else vlib_cli_output (vm, "TX Trace enabled\n"); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_api_command, static) = { .path = "show api", .short_help = "Show API information", }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = { .path = "show api ring-stats", .short_help = "Message ring statistics", .function = vl_api_ring_command, }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = { .path = "show api clients", .short_help = "Client information", .function = vl_api_client_command, }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_api_status_command, static) = { .path = "show api status", .short_help = "Show API trace status", .function = vl_api_status_command, }; /* *INDENT-ON* */ static clib_error_t * vl_api_message_table_command (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cli_cmd) { api_main_t *am = &api_main; int i; int verbose = 0; if (unformat (input, "verbose")) verbose = 1; if (verbose == 0) vlib_cli_output (vm, "%-4s %s", "ID", "Name"); else vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce", "MP-safe"); for (i = 1; i < vec_len (am->msg_names); i++) { if (verbose == 0) { vlib_cli_output (vm, "%-4d %s", i, am->msg_names[i] ? am->msg_names[i] : " [no handler]"); } else { vlib_cli_output (vm, "%-4d %-40s %6d %7d", i, am->msg_names[i] ? am->msg_names[i] : " [no handler]", am->message_bounce[i], am->is_mp_safe[i]); } } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) = { .path = "show api message-table", .short_help = "Message Table", .function = vl_api_message_table_command, }; /* *INDENT-ON* */ static clib_error_t * vl_api_trace_command (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cli_cmd) { u32 nitems = 1024; vl_api_trace_which_t which = VL_API_TRACE_RX; api_main_t *am = &api_main; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx")) goto configure; else if (unformat (input, "tx nitems %u", &nitems) || unformat (input, "tx")) { which = VL_API_TRACE_RX; goto configure; } else if (unformat (input, "on rx")) { vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1); } else if (unformat (input, "on tx")) { vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1); } else if (unformat (input, "on")) { vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1); } else if (unformat (input, "off")) { vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0); vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0); } else if (unformat (input, "free")) { vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0); vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0); vl_msg_api_trace_free (am, VL_API_TRACE_RX); vl_msg_api_trace_free (am, VL_API_TRACE_TX); } else if (unformat (input, "debug on")) { am->msg_print_flag = 1; } else if (unformat (input, "debug off")) { am->msg_print_flag = 0; } else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } return 0; configure: if (vl_msg_api_trace_configure (am, which, nitems)) { vlib_cli_output (vm, "warning: trace configure error (%d, %d)", which, nitems); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (trace, static) = { .path = "set api-trace", .short_help = "API trace", .function = vl_api_trace_command, }; /* *INDENT-ON* */ clib_error_t * vlibmemory_init (vlib_main_t * vm) { api_main_t *am = &api_main; svm_map_region_args_t _a, *a = &_a; memset (a, 0, sizeof (*a)); a->root_path = am->root_path; a->name = SVM_GLOBAL_REGION_NAME; a->baseva = (am->global_baseva != 0) ? am->global_baseva : SVM_GLOBAL_REGION_BASEVA; a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE; a->flags = SVM_FLAGS_NODATA; a->uid = am->api_uid; a->gid = am->api_gid; a->pvt_heap_size = (am->global_pvt_heap_size != 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE; svm_region_init_args (a); return 0; } VLIB_INIT_FUNCTION (vlibmemory_init); void vl_set_memory_region_name (char *name) { api_main_t *am = &api_main; am->region_name = name; } static int range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1) { int len0, len1, clen; len0 = vec_len (a0->name); len1 = vec_len (a1->name); clen = len0 < len1 ? len0 : len1; return (strncmp ((char *) a0->name, (char *) a1->name, clen)); } static u8 * format_api_msg_range (u8 * s, va_list * args) { vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *); if (rp == 0) s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID"); else s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id, rp->last_msg_id); return s; } static clib_error_t * vl_api_show_plugin_command (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cli_cmd) { api_main_t *am = &api_main; vl_api_msg_range_t *rp = 0; int i; if (vec_len (am->msg_ranges) == 0) { vlib_cli_output (vm, "No plugin API message ranges configured..."); return 0; } rp = vec_dup (am->msg_ranges); vec_sort_with_function (rp, range_compare); vlib_cli_output (vm, "Plugin API message ID ranges...\n"); vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ ); for (i = 0; i < vec_len (rp); i++) vlib_cli_output (vm, "%U", format_api_msg_range, rp + i); vec_free (rp); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) = { .path = "show api plugin", .short_help = "show api plugin", .function = vl_api_show_plugin_command, }; /* *INDENT-ON* */ static void vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp) { vl_api_rpc_reply_t *rmp; int (*fp) (void *); i32 rv = 0; vlib_main_t *vm = vlib_get_main (); if (mp->function == 0) { rv = -1; clib_warning ("rpc NULL function pointer"); } else { if (mp->need_barrier_sync) vlib_worker_thread_barrier_sync (vm); fp = uword_to_pointer (mp->function, int (*)(void *)); rv = fp (mp->data); if (mp->need_barrier_sync) vlib_worker_thread_barrier_release (vm); } if (mp->send_reply) { unix_shared_memory_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index); if (q) { rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_RPC_REPLY); rmp->context = mp->context; rmp->retval = rv; vl_msg_api_send_shmem (q, (u8 *) & rmp); } } if (mp->multicast) { clib_warning ("multicast not yet implemented..."); } } static void vl_api_rpc_reply_t_handler (vl_api_rpc_reply_t * mp) { clib_warning ("unimplemented"); } void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length) { vl_api_rpc_call_t *mp; api_main_t *am = &api_main; vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; unix_shared_memory_queue_t *q; /* Main thread: call the function directly */ if (os_get_cpu_number () == 0) { vlib_main_t *vm = vlib_get_main (); void (*call_fp) (void *); vlib_worker_thread_barrier_sync (vm); call_fp = fp; call_fp (data); vlib_worker_thread_barrier_release (vm); return; } /* Any other thread, actually do an RPC call... */ mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length); memset (mp, 0, sizeof (*mp)); clib_memcpy (mp->data, data, data_length); mp->_vl_msg_id = ntohs (VL_API_RPC_CALL); mp->function = pointer_to_uword (fp); mp->need_barrier_sync = 1; /* * Use the "normal" control-plane mechanism for the main thread. * Well, almost. if the main input queue is full, we cannot * block. Otherwise, we can expect a barrier sync timeout. */ q = shmem_hdr->vl_input_queue; while (pthread_mutex_trylock (&q->mutex)) vlib_worker_thread_barrier_check (); while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q))) { pthread_mutex_unlock (&q->mutex); vlib_worker_thread_barrier_check (); while (pthread_mutex_trylock (&q->mutex)) vlib_worker_thread_barrier_check (); } vl_msg_api_send_shmem_nolock (q, (u8 *) & mp); pthread_mutex_unlock (&q->mutex); } static void vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t * mp) { api_main_t *am = &api_main; vl_api_msg_range_t *rp; uword *p; /* Noop (except for tracing) during normal operation */ if (am->replay_in_progress == 0) return; p = hash_get_mem (am->msg_range_by_name, mp->plugin_name); if (p == 0) { clib_warning ("WARNING: traced plugin '%s' not in current image", mp->plugin_name); return; } rp = vec_elt_at_index (am->msg_ranges, p[0]); if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id)) { clib_warning ("WARNING: traced plugin '%s' first message id %d not %d", mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id), rp->first_msg_id); } if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id)) { clib_warning ("WARNING: traced plugin '%s' last message id %d not %d", mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id), rp->last_msg_id); } } #define foreach_rpc_api_msg \ _(RPC_CALL,rpc_call) \ _(RPC_REPLY,rpc_reply) #define foreach_plugin_trace_msg \ _(TRACE_PLUGIN_MSG_IDS,trace_plugin_msg_ids) static clib_error_t * rpc_api_hookup (vlib_main_t * vm) { #define _(N,n) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_noop_handler, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 0 /* do not trace */); foreach_rpc_api_msg; #undef _ #define _(N,n) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_noop_handler, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1 /* do trace */); foreach_plugin_trace_msg; #undef _ return 0; } VLIB_API_INIT_FUNCTION (rpc_api_hookup); typedef enum { DUMP, CUSTOM_DUMP, REPLAY, INITIALIZERS, } vl_api_replay_t; u8 * format_vl_msg_api_trace_status (u8 * s, va_list * args) { api_main_t *am = va_arg (*args, api_main_t *); vl_api_trace_which_t which = va_arg (*args, vl_api_trace_which_t); vl_api_trace_t *tp; char *trace_name; switch (which) { case VL_API_TRACE_TX: tp = am->tx_trace; trace_name = "TX trace"; break; case VL_API_TRACE_RX: tp = am->rx_trace; trace_name = "RX trace"; break; default: abort (); } if (tp == 0) { s = format (s, "%s: not yet configured.\n", trace_name); return s; } s = format (s, "%s: used %d of %d items, %s enabled, %s wrapped\n", trace_name, vec_len (tp->traces), tp->nitems, tp->enabled ? "is" : "is not", tp->wrapped ? "has" : "has not"); return s; } void vl_msg_api_custom_dump_configure (api_main_t * am) __attribute__ ((weak)); void vl_msg_api_custom_dump_configure (api_main_t * am) { } static void vl_msg_api_process_file (vlib_main_t * vm, u8 * filename, u32 first_index, u32 last_index, vl_api_replay_t which) { vl_api_trace_file_header_t *hp; int i, fd; struct stat statb; size_t file_size; u8 *msg; u8 endian_swap_needed = 0; api_main_t *am = &api_main; u8 *tmpbuf = 0; u32 nitems; void **saved_print_handlers = 0; fd = open ((char *) filename, O_RDONLY); if (fd < 0) { vlib_cli_output (vm, "Couldn't open %s\n", filename); return; } if (fstat (fd, &statb) < 0) { vlib_cli_output (vm, "Couldn't stat %s\n", filename); close (fd); return; } if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp))) { vlib_cli_output (vm, "File not plausible: %s\n", filename); close (fd); return; } file_size = statb.st_size; file_size = (file_size + 4095) & ~(4096); hp = mmap (0, file_size, PROT_READ, MAP_PRIVATE, fd, 0); if (hp == (vl_api_trace_file_header_t *) MAP_FAILED) { vlib_cli_output (vm, "mmap failed: %s\n", filename); close (fd); return; } close (fd); if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN) || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN)) endian_swap_needed = 1; if (endian_swap_needed) nitems = ntohl (hp->nitems); else nitems = hp->nitems; if (last_index == (u32) ~ 0) { last_index = nitems - 1; } if (first_index >= nitems || last_index >= nitems) { vlib_cli_output (vm, "Range (%d, %d) outside file range (0, %d)\n", first_index, last_index, nitems - 1); munmap (hp, file_size); return; } if (hp->wrapped) vlib_cli_output (vm, "Note: wrapped/incomplete trace, results may vary\n"); if (which == CUSTOM_DUMP) { saved_print_handlers = (void **) vec_dup (am->msg_print_handlers); vl_msg_api_custom_dump_configure (am); } msg = (u8 *) (hp + 1); for (i = 0; i < first_index; i++) { trace_cfg_t *cfgp; int size; u16 msg_id; size = clib_host_to_net_u32 (*(u32 *) msg); msg += sizeof (u32); if (clib_arch_is_little_endian) msg_id = ntohs (*((u16 *) msg)); else msg_id = *((u16 *) msg); cfgp = am->api_trace_cfg + msg_id; if (!cfgp) { vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id); munmap (hp, file_size); return; } msg += size; } if (which == REPLAY) am->replay_in_progress = 1; for (; i <= last_index; i++) { trace_cfg_t *cfgp; u16 *msg_idp; u16 msg_id; int size; if (which == DUMP) vlib_cli_output (vm, "---------- trace %d -----------\n", i); size = clib_host_to_net_u32 (*(u32 *) msg); msg += sizeof (u32); if (clib_arch_is_little_endian) msg_id = ntohs (*((u16 *) msg)); else msg_id = *((u16 *) msg); cfgp = am->api_trace_cfg + msg_id; if (!cfgp) { vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id); munmap (hp, file_size); vec_free (tmpbuf); am->replay_in_progress = 0; return; } /* Copy the buffer (from the read-only mmap'ed file) */ vec_validate (tmpbuf, size - 1 + sizeof (uword)); clib_memcpy (tmpbuf + sizeof (uword), msg, size); memset (tmpbuf, 0xf, sizeof (uword)); /* * Endian swap if needed. All msg data is supposed to be * in network byte order. All msg handlers are supposed to * know that. The generic message dumpers don't know that. * One could fix apigen, I suppose. */ if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed) { void (*endian_fp) (void *); if (msg_id >= vec_len (am->msg_endian_handlers) || (am->msg_endian_handlers[msg_id] == 0)) { vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id); munmap (hp, file_size); vec_free (tmpbuf); am->replay_in_progress = 0; return; } endian_fp = am->msg_endian_handlers[msg_id]; (*endian_fp) (tmpbuf + sizeof (uword)); } /* msg_id always in network byte order */ if (clib_arch_is_little_endian) { msg_idp = (u16 *) (tmpbuf + sizeof (uword)); *msg_idp = msg_id; } switch (which) { case CUSTOM_DUMP: case DUMP: if (msg_id < vec_len (am->msg_print_handlers) && am->msg_print_handlers[msg_id]) { u8 *(*print_fp) (void *, void *); print_fp = (void *) am->msg_print_handlers[msg_id]; (*print_fp) (tmpbuf + sizeof (uword), vm); } else { vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n", msg_id); break; } break; case INITIALIZERS: if (msg_id < vec_len (am->msg_print_handlers) && am->msg_print_handlers[msg_id]) { u8 *s; int j; u8 *(*print_fp) (void *, void *); print_fp = (void *) am->msg_print_handlers[msg_id]; vlib_cli_output (vm, "/*"); (*print_fp) (tmpbuf + sizeof (uword), vm); vlib_cli_output (vm, "*/\n"); s = format (0, "static u8 * vl_api_%s_%d[%d] = {", am->msg_names[msg_id], i, am->api_trace_cfg[msg_id].size); for (j = 0; j < am->api_trace_cfg[msg_id].size; j++) { if ((j & 7) == 0) s = format (s, "\n "); s = format (s, "0x%02x,", tmpbuf[sizeof (uword) + j]); } s = format (s, "\n};\n%c", 0); vlib_cli_output (vm, (char *) s); vec_free (s); } break; case REPLAY: if (msg_id < vec_len (am->msg_print_handlers) && am->msg_print_handlers[msg_id] && cfgp->replay_enable) { void (*handler) (void *); handler = (void *) am->msg_handlers[msg_id]; if (!am->is_mp_safe[msg_id]) vl_msg_api_barrier_sync (); (*handler) (tmpbuf + sizeof (uword)); if (!am->is_mp_safe[msg_id]) vl_msg_api_barrier_release (); } else { if (cfgp->replay_enable) vlib_cli_output (vm, "Skipping msg id %d: no handler\n", msg_id); break; } break; } _vec_len (tmpbuf) = 0; msg += size; } if (saved_print_handlers) { clib_memcpy (am->msg_print_handlers, saved_print_handlers, vec_len (am->msg_print_handlers) * sizeof (void *)); vec_free (saved_print_handlers); } munmap (hp, file_size); vec_free (tmpbuf); am->replay_in_progress = 0; } static clib_error_t * api_trace_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u32 nitems = 256 << 10; api_main_t *am = &api_main; vl_api_trace_which_t which = VL_API_TRACE_RX; u8 *filename; u32 first = 0; u32 last = (u32) ~ 0; FILE *fp; int rv; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "on") || unformat (input, "enable")) { if (unformat (input, "nitems %d", &nitems)) ; vl_msg_api_trace_configure (am, which, nitems); vl_msg_api_trace_onoff (am, which, 1 /* on */ ); } else if (unformat (input, "off")) { vl_msg_api_trace_onoff (am, which, 0); } else if (unformat (input, "save %s", &filename)) { u8 *chroot_filename; if (strstr ((char *) filename, "..") || index ((char *) filename, '/')) { vlib_cli_output (vm, "illegal characters in filename '%s'", filename); return 0; } chroot_filename = format (0, "/tmp/%s%c", filename, 0); vec_free (filename); fp = fopen ((char *) chroot_filename, "w"); if (fp == NULL) { vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename); return 0; } rv = vl_msg_api_trace_save (am, which, fp); fclose (fp); if (rv == -1) vlib_cli_output (vm, "API Trace data not present\n"); else if (rv == -2) vlib_cli_output (vm, "File for writing is closed\n"); else if (rv == -10) vlib_cli_output (vm, "Error while writing header to file\n"); else if (rv == -11) vlib_cli_output (vm, "Error while writing trace to file\n"); else if (rv == -12) vlib_cli_output (vm, "Error while writing end of buffer trace to file\n"); else if (rv == -13) vlib_cli_output (vm, "Error while writing start of buffer trace to file\n"); else if (rv < 0) vlib_cli_output (vm, "Unkown error while saving: %d", rv); else vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename); vec_free (chroot_filename); } else if (unformat (input, "dump %s", &filename)) { vl_msg_api_process_file (vm, filename, first, last, DUMP); } else if (unformat (input, "custom-dump %s", &filename)) { vl_msg_api_process_file (vm, filename, first, last, CUSTOM_DUMP); } else if (unformat (input, "replay %s", &filename)) { vl_msg_api_process_file (vm, filename, first, last, REPLAY); } else if (unformat (input, "initializers %s", &filename)) { vl_msg_api_process_file (vm, filename, first, last, INITIALIZERS); } else if (unformat (input, "tx")) { which = VL_API_TRACE_TX; } else if (unformat (input, "first %d", &first)) { ; } else if (unformat (input, "last %d", &last)) { ; } else if (unformat (input, "status")) { vlib_cli_output (vm, "%U", format_vl_msg_api_trace_status, am, which); } else if (unformat (input, "free")) { vl_msg_api_trace_onoff (am, which, 0); vl_msg_api_trace_free (am, which); } else if (unformat (input, "post-mortem-on")) vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ ); else if (unformat (input, "post-mortem-off")) vl_msg_api_post_mortem_dump_enable_disable (0 /* enable */ ); else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (api_trace_command, static) = { .path = "api trace", .short_help = "api trace [on|off][dump|save|replay ][status][free][post-mortem-on]", .function = api_trace_command_fn, }; /* *INDENT-ON* */ static clib_error_t * api_config_fn (vlib_main_t * vm, unformat_input_t * input) { u32 nitems = 256 << 10; vl_api_trace_which_t which = VL_API_TRACE_RX; api_main_t *am = &api_main; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "on") || unformat (input, "enable")) { if (unformat (input, "nitems %d", &nitems)) ; vl_msg_api_trace_configure (am, which, nitems); vl_msg_api_trace_onoff (am, which, 1 /* on */ ); vl_msg_api_post_mortem_dump_enable_disable (1 /* enable */ ); } else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } return 0; } VLIB_CONFIG_FUNCTION (api_config_fn, "api-trace"); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */