|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <ctime> |
| 4 | +#include <sstream> |
| 5 | + |
| 6 | +#include <unistd.h> |
| 7 | +#include <getopt.h> |
| 8 | + |
| 9 | +#include "swss/logger.h" |
| 10 | + |
| 11 | +extern "C" { |
| 12 | +#include <sai.h> |
| 13 | +} |
| 14 | + |
| 15 | +std::string sai_profile = "/tmp/sai.profile"; |
| 16 | + |
| 17 | +void print_usage() |
| 18 | +{ |
| 19 | + std::cerr << "Following SAI dump options can be specified:" << std::endl; |
| 20 | + std::cerr << "-------------------------------------------" << std::endl; |
| 21 | + std::cerr << "--dump_file -f Full path for dump file" << std::endl; |
| 22 | + std::cerr << "--profile -p Full path to SAI profile file [ default is " << sai_profile << " ]" << std::endl; |
| 23 | + std::cerr << "--help -h usage" << std::endl; |
| 24 | +} |
| 25 | + |
| 26 | +__attribute__((__noreturn__)) void exit_with_sai_failure(const char *msg, sai_status_t status) |
| 27 | +{ |
| 28 | + if (msg) |
| 29 | + { |
| 30 | + std::cerr << msg << " rc=" << status << std::endl; |
| 31 | + } |
| 32 | + SWSS_LOG_ERROR("saisdkdump exited with SAI rc: 0x%x, msg: %s .", status, (msg != NULL ? msg : "")); |
| 33 | + exit(EXIT_FAILURE); |
| 34 | +} |
| 35 | + |
| 36 | +const char* profile_get_value( |
| 37 | + _In_ sai_switch_profile_id_t profile_id, |
| 38 | + _In_ const char* variable) |
| 39 | +{ |
| 40 | + return sai_profile.c_str(); |
| 41 | +} |
| 42 | + |
| 43 | +int profile_get_next_value( |
| 44 | + _In_ sai_switch_profile_id_t profile_id, |
| 45 | + _Out_ const char** variable, |
| 46 | + _Out_ const char** value) |
| 47 | +{ |
| 48 | + return -1; |
| 49 | +} |
| 50 | + |
| 51 | +service_method_table_t test_services = { |
| 52 | + profile_get_value, |
| 53 | + profile_get_next_value |
| 54 | +}; |
| 55 | + |
| 56 | +int main(int argc, char **argv) |
| 57 | +{ |
| 58 | + swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_NOTICE); |
| 59 | + |
| 60 | + SWSS_LOG_ENTER(); |
| 61 | + |
| 62 | + static struct option longOptions[] = |
| 63 | + { |
| 64 | + { "help", no_argument, 0, 'h' }, |
| 65 | + { "dump_file", required_argument, 0, 'f' }, |
| 66 | + { "profile", required_argument, 0, 'p' } |
| 67 | + }; |
| 68 | + |
| 69 | + bool fileSpecified = false; |
| 70 | + std::string fileName; |
| 71 | + int option_index = 0; |
| 72 | + int c = 0; |
| 73 | + |
| 74 | + while((c = getopt_long(argc, argv, "hf:p:", longOptions, &option_index)) != -1) |
| 75 | + { |
| 76 | + switch (c) |
| 77 | + { |
| 78 | + case 'f': |
| 79 | + if (optarg != NULL) |
| 80 | + { |
| 81 | + fileName = std::string(optarg); |
| 82 | + fileSpecified = true; |
| 83 | + } |
| 84 | + break; |
| 85 | + case 'p': |
| 86 | + if (optarg != NULL) |
| 87 | + { |
| 88 | + sai_profile = std::string(optarg); |
| 89 | + } |
| 90 | + break; |
| 91 | + |
| 92 | + case 'h': |
| 93 | + print_usage(); |
| 94 | + break; |
| 95 | + |
| 96 | + default: |
| 97 | + SWSS_LOG_ERROR("getopt failure"); |
| 98 | + exit(EXIT_FAILURE); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (!fileSpecified) |
| 103 | + { |
| 104 | + std::ostringstream strStream; |
| 105 | + time_t t = time(NULL); |
| 106 | + struct tm *now = localtime(&t); |
| 107 | + strStream << "/tmp/saisdkdump_" << now->tm_mday << "_" << now->tm_mon + 1 << "_" << now->tm_year + 1900 << "_" << now->tm_hour << "_" << now->tm_min << "_" << now->tm_sec; |
| 108 | + fileName = strStream.str(); |
| 109 | + SWSS_LOG_INFO("The dump file is not specified, generated \"%s\" file name", fileName.c_str()); |
| 110 | + } |
| 111 | + |
| 112 | + sai_status_t status = sai_api_initialize(0, (service_method_table_t*)&test_services); |
| 113 | + if (status != SAI_STATUS_SUCCESS) |
| 114 | + { |
| 115 | + exit_with_sai_failure("Failed to initialize SAI api", status); |
| 116 | + } |
| 117 | + |
| 118 | + sai_switch_api_t* switch_api; |
| 119 | + status = sai_api_query(SAI_API_SWITCH, (void**) &switch_api); |
| 120 | + if (status != SAI_STATUS_SUCCESS) |
| 121 | + { |
| 122 | + exit_with_sai_failure("Failed to query switch api", status); |
| 123 | + } |
| 124 | + |
| 125 | + sai_object_id_t switch_id; |
| 126 | + const uint32_t AttributesCount = 1; |
| 127 | + sai_attribute_t attrs[AttributesCount]; |
| 128 | + attrs[0].id = SAI_SWITCH_ATTR_INIT_SWITCH; |
| 129 | + attrs[0].value.booldata = false; |
| 130 | + |
| 131 | + status = switch_api->create_switch(&switch_id, AttributesCount, attrs); |
| 132 | + if (status != SAI_STATUS_SUCCESS) |
| 133 | + { |
| 134 | + exit_with_sai_failure("Failed to create a switch", status); |
| 135 | + } |
| 136 | + |
| 137 | + status = sai_dbg_generate_dump(fileName.c_str()); |
| 138 | + if (status != SAI_STATUS_SUCCESS) |
| 139 | + { |
| 140 | + exit_with_sai_failure("Failed to generate SAI dump", status); |
| 141 | + } |
| 142 | + |
| 143 | + SWSS_LOG_NOTICE("The SAI dump is generated to %s .", fileName.c_str()); |
| 144 | + std::cout << "The SAI dump is generated to " << fileName << std::endl; |
| 145 | + |
| 146 | + status = switch_api->remove_switch(switch_id); |
| 147 | + if (status != SAI_STATUS_SUCCESS) |
| 148 | + { |
| 149 | + SWSS_LOG_ERROR("remove switch 0x%x failed: 0x%x", switch_id, status); |
| 150 | + } |
| 151 | + |
| 152 | + status = sai_api_uninitialize(); |
| 153 | + if (status != SAI_STATUS_SUCCESS) |
| 154 | + { |
| 155 | + exit_with_sai_failure("SAI api uninitialize failed", status); |
| 156 | + } |
| 157 | + |
| 158 | + return EXIT_SUCCESS; |
| 159 | +} |
0 commit comments