31 #include <json-c/json.h>
42 #define MAX_DESCRIPTION_LEN 256
43 #define DATA_ID_STRING_MAX_LEN 20
45 #define DEFAULT_SAMPLE_PERIOD_NS 100000
46 #define DEFAULT_LOG_MAX_NUM_SAMPLES 10
47 #define DEFAULT_MAX_NUM_SAMPLES_PER_READ 128
48 #define DEFAULT_SAMPLE_MODE DOCA_TELEMETRY_DIAG_SAMPLE_MODE_REPETITIVE
49 #define DEFAULT_OUTPUT_FORMAT DOCA_TELEMETRY_DIAG_OUTPUT_FORMAT_1
50 #define DEFAULT_TOTAL_RUN_TIME_SECS 1
51 #define DEFAULT_OUTPUT_PATH "/tmp/out.csv"
52 #define DEFAULT_FORCE_OWNERSHIP 0
53 #define DEFAULT_DATA_IDS_PATH "/0"
54 #define DEFAULT_EXAMPLE_JSON_PATH "/0"
56 #define JSON_NAME_KEY "name"
57 #define JSON_DATA_ID_KEY "data_id"
59 #define DATA_ID_PORT_0_RX_BYTES 0x1020000100000000
60 #define DATA_ID_PORT_0_TX_BYTES 0x1140000100000000
61 #define DATA_ID_PORT_0_RX_PACKETS 0x1020000300000000
62 #define DATA_ID_PORT_0_TX_PACKETS 0x1140000300000000
74 char *pci_address = (
char *)param;
79 DOCA_LOG_ERR(
"Entered device PCI address exceeding the maximum size of %d",
98 char *json_path = (
char *)param;
103 DOCA_LOG_ERR(
"Invalid path: data-ids file name length exceeds the maximum of %d characters",
107 if (access(json_path, F_OK) == -1) {
126 char *file = (
char *)param;
131 DOCA_LOG_ERR(
"Invalid path: output file name length exceeds the maximum of %d characters",
149 uint32_t *
run_time = (uint32_t *)param;
184 DOCA_LOG_ERR(
"Parameter log_max_num_samples larger than uint8. log_max_num_samples=%d",
205 DOCA_LOG_ERR(
"Parameter max_num_samples_per_read larger than uint8. max_num_samples_per_read=%d",
273 char *file = (
char *)param;
278 DOCA_LOG_ERR(
"Invalid path: output file name length exceeds the maximum of %d characters",
295 struct doca_argp_param *pci_param, *data_ids_param, *run_time_param, *sample_period_param,
296 *log_max_num_samples_param, *max_num_samples_per_read_param, *sample_mode_param, *output_format_param,
467 "Generate an example json file with the default data_ids to the given path \
468 and exit immediately. This file can be used as input later on. \
469 All other flags are ignored");
491 struct json_object *json_data_ids,
494 struct json_object *json_entry, *json_data_id, *json_name;
495 const char *data_id_str;
498 for (
int i = 0; i < array_len; i++) {
499 json_entry = json_object_array_get_idx(json_data_ids, i);
503 if (!json_object_object_get_ex(json_entry,
JSON_DATA_ID_KEY, &json_data_id)) {
509 if (!json_object_object_get_ex(json_entry,
JSON_NAME_KEY, &json_name)) {
515 data_id_str = json_object_get_string(json_data_id);
516 if (sscanf(data_id_str,
"%lx", &
cfg->data_ids_struct[i].data_id) != 1) {
517 DOCA_LOG_ERR(
"Failed to parse data_id (expected hexadecimal number): '%s'", data_id_str);
521 strncpy(
cfg->data_ids_struct[i].name, json_object_get_string(json_name),
MAX_NAME_SIZE - 1);
526 cfg->num_data_ids = array_len;
539 cfg->num_data_ids = 4;
542 if (!
cfg->data_ids_struct) {
543 DOCA_LOG_ERR(
"Failed to allocate memory for data_ids_struct");
548 strncpy(
cfg->data_ids_struct[0].name,
"port_0_rx_bytes",
MAX_NAME_SIZE - 1);
551 strncpy(
cfg->data_ids_struct[1].name,
"port_0_tx_bytes",
MAX_NAME_SIZE - 1);
554 strncpy(
cfg->data_ids_struct[2].name,
"port_0_rx_packets",
MAX_NAME_SIZE - 1);
557 strncpy(
cfg->data_ids_struct[3].name,
"port_0_tx_packets",
MAX_NAME_SIZE - 1);
574 if (!
cfg->import_json) {
581 char *json_data =
NULL;
582 struct json_object *parsed_json, *json_data_ids;
586 json_fp = fopen(
cfg->data_ids_input_path,
"r");
587 if (json_fp ==
NULL) {
593 if (fseek(json_fp, 0, SEEK_END) != 0) {
599 temp_length = ftell(json_fp);
600 if (temp_length < 0) {
605 file_length = (
size_t)temp_length;
609 json_data = malloc(file_length + 1);
610 if (json_data ==
NULL) {
611 DOCA_LOG_ERR(
"Failed to allocate data buffer for the json file");
617 if (fread(json_data, 1, file_length, json_fp) < file_length)
620 json_data[file_length] =
'\0';
622 parsed_json = json_tokener_parse(json_data);
625 if (!json_object_object_get_ex(parsed_json,
"data-ids", &json_data_ids)) {
626 DOCA_LOG_ERR(
"Missing \"data-ids\" parameter in the data ids JSON file");
631 array_len = json_object_array_length(json_data_ids);
632 if (array_len == 0) {
633 DOCA_LOG_ERR(
"The \"data-ids\" array in the JSON file is empty");
642 if (!
cfg->data_ids_struct) {
644 DOCA_LOG_ERR(
"Failed to allocate memory for data_ids_struct");
651 free(
cfg->data_ids_struct);
656 json_object_put(parsed_json);
673 json_object *data_id, *pair, *name_obj;
675 pair = json_object_new_object();
681 name_obj = json_object_new_string(name);
682 if (name_obj ==
NULL) {
688 DOCA_LOG_ERR(
"Failed to add name to default data_ids json");
689 json_object_put(name_obj);
693 data_id = json_object_new_string(data_id_string);
694 if (data_id ==
NULL) {
695 DOCA_LOG_ERR(
"Failed to create new data_id json object");
700 DOCA_LOG_ERR(
"Failed to add data_id to default data_ids json");
701 json_object_put(data_id);
708 json_object_put(pair);
720 json_object *data_id_array = json_object_new_array();
724 if (data_id_array ==
NULL) {
725 DOCA_LOG_ERR(
"Failed to create data_id_array json object");
729 for (uint32_t i = 0; i <
cfg->num_data_ids; i++) {
730 if (snprintf(data_id_string,
sizeof(data_id_string),
"0x%lx",
cfg->data_ids_struct[i].data_id) < 0) {
732 goto put_data_id_array;
737 goto put_data_id_array;
739 if (json_object_array_add(data_id_array, pair)) {
745 if (json_object_object_add(example_json,
"data-ids", data_id_array)) {
746 DOCA_LOG_ERR(
"Failed to add data_id_array to example json");
747 goto put_data_id_array;
752 json_object_put(pair);
754 json_object_put(data_id_array);
765 json_object *example_json;
768 const char *exported_json;
776 json_file = fopen(
cfg->data_ids_example_export_path,
"w");
777 if (json_file ==
NULL) {
778 DOCA_LOG_ERR(
"Failed to open output file \"%s\" with errno=%s (%d)",
779 cfg->data_ids_example_export_path,
785 example_json = json_object_new_object();
786 if (example_json ==
NULL) {
795 goto put_json_object;
798 exported_json = json_object_to_json_string_ext(example_json, JSON_C_TO_STRING_PRETTY);
799 if (fwrite(exported_json,
sizeof(
char), strlen(exported_json), json_file) == 0) {
802 goto put_json_object;
808 json_object_put(example_json);
812 free(
cfg->data_ids_struct);
830 cfg->pci_set =
false;
831 cfg->import_json =
false;
832 cfg->export_json =
false;
845 int main(
int argc,
char **argv)
848 int exit_status = EXIT_FAILURE;
850 struct doca_log_backend *sdk_log;
890 DOCA_LOG_INFO(
"Example data_ids json exported successfully, exiting");
891 exit_status = EXIT_SUCCESS;
915 exit_status = EXIT_SUCCESS;
923 if (exit_status == EXIT_SUCCESS)
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_name(doca_error_t error)
Returns the string representation of an error code name.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_INVALID_VALUE
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
#define DOCA_LOG_DBG(format,...)
Generates a DEBUG application log message.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
doca_telemetry_diag_sample_mode
description of data sampling mode
doca_telemetry_diag_output_format
Defines the layout of the diagnostic data output:
const struct ip_frag_config * cfg
uint32_t max_num_samples_per_read
char data_ids_input_path[TELEMETRY_DIAG_SAMPLE_MAX_FILE_NAME]
uint8_t log_max_num_samples
enum doca_telemetry_diag_output_format output_format
struct data_id_entry * data_ids_struct
char data_ids_example_export_path[TELEMETRY_DIAG_SAMPLE_MAX_FILE_NAME]
char output_path[TELEMETRY_DIAG_SAMPLE_MAX_FILE_NAME]
enum doca_telemetry_diag_sample_mode sample_mode
char pci_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]
#define DEFAULT_SAMPLE_MODE
static doca_error_t register_telemetry_diag_params(void)
static doca_error_t data_ids_callback(void *param, void *config)
#define DEFAULT_MAX_NUM_SAMPLES_PER_READ
#define DEFAULT_SAMPLE_PERIOD_NS
static doca_error_t generate_example_json(struct telemetry_diag_sample_cfg *cfg)
DOCA_LOG_REGISTER(TELEMETRY_DIAG::MAIN)
int main(int argc, char **argv)
#define DATA_ID_PORT_0_RX_PACKETS
static doca_error_t pci_address_callback(void *param, void *config)
static doca_error_t parse_and_read_data_ids_json_file(struct telemetry_diag_sample_cfg *cfg)
static doca_error_t log_max_num_samples_callback(void *param, void *config)
#define DEFAULT_LOG_MAX_NUM_SAMPLES
#define DATA_ID_PORT_0_TX_BYTES
#define DEFAULT_FORCE_OWNERSHIP
static json_object * create_data_id_pair(char *name, char *data_id_string)
static void set_default_params(struct telemetry_diag_sample_cfg *cfg)
static doca_error_t sample_mode_callback(void *param, void *config)
#define DATA_ID_PORT_0_RX_BYTES
static doca_error_t create_default_json(struct telemetry_diag_sample_cfg *cfg, struct json_object *example_json)
static doca_error_t example_json_file_callback(void *param, void *config)
static doca_error_t parse_json_data_ids(struct telemetry_diag_sample_cfg *cfg, struct json_object *json_data_ids, int array_len)
#define DEFAULT_TOTAL_RUN_TIME_SECS
static doca_error_t sample_period_callback(void *param, void *config)
#define DEFAULT_OUTPUT_FORMAT
static doca_error_t output_callback(void *param, void *config)
static doca_error_t run_time_callback(void *param, void *config)
#define DEFAULT_EXAMPLE_JSON_PATH
#define DATA_ID_STRING_MAX_LEN
#define DEFAULT_OUTPUT_PATH
static doca_error_t max_num_samples_per_read_callback(void *param, void *config)
static doca_error_t output_format_callback(void *param, void *config)
static doca_error_t set_default_data_ids(struct telemetry_diag_sample_cfg *cfg)
static doca_error_t force_ownership_callback(void *param, void *config)
#define DATA_ID_PORT_0_TX_PACKETS
#define DEFAULT_DATA_IDS_PATH
doca_error_t telemetry_diag_sample_run(const struct telemetry_diag_sample_cfg *cfg)
#define TELEMETRY_DIAG_SAMPLE_MAX_FILE_NAME