forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move command line options and parser to separate classes (sonic-net#541)
* Move ntf_queue to proper NotificationQueue class * Move command line options and parser to separate classes * Address comments * Address comments
- Loading branch information
Showing
9 changed files
with
416 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include "CommandLineOptions.h" | ||
|
||
#include "swss/logger.h" | ||
|
||
#include <sstream> | ||
|
||
CommandLineOptions::CommandLineOptions() | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
// default values for command line options | ||
|
||
m_enableDiagShell = false; | ||
m_enableTempView = false; | ||
m_disableExitSleep = false; | ||
m_enableUnittests = false; | ||
m_enableConsistencyCheck = false; | ||
m_enableSyncMode = false; | ||
|
||
m_startType = SAI_START_TYPE_COLD_BOOT; | ||
|
||
m_profileMapFile = ""; | ||
|
||
#ifdef SAITHRIFT | ||
|
||
m_runRPCServer = false; | ||
|
||
m_portMapFile = ""; | ||
|
||
#endif // SAITHRIFT | ||
|
||
} | ||
|
||
std::string CommandLineOptions::getCommandLineString() const | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
std::stringstream ss; | ||
|
||
ss << " EnableDiagShell=" << (m_enableDiagShell ? "YES" : "NO"); | ||
ss << " EnableTempView=" << (m_enableTempView ? "YES" : "NO"); | ||
ss << " DisableExitSleep=" << (m_disableExitSleep ? "YES" : "NO"); | ||
ss << " EnableUnittests=" << (m_enableUnittests ? "YES" : "NO"); | ||
ss << " EnableConsistencyCheck=" << (m_enableConsistencyCheck ? "YES" : "NO"); | ||
ss << " EnableSyncMode=" << (m_enableSyncMode ? "YES" : "NO"); | ||
ss << " StartType=" << startTypeToString(m_startType); | ||
ss << " ProfileMapFile=" << m_profileMapFile; | ||
|
||
#ifdef SAITHRIFT | ||
|
||
ss << " RunRPCServer=" << (m_runRPCServer ? "YES" ? "NO"); | ||
ss << " PortMapFile=" << m_portMapFile; | ||
|
||
#endif // SAITHRIFT | ||
|
||
return ss.str(); | ||
} | ||
|
||
sai_start_type_t CommandLineOptions::startTypeStringToStartType( | ||
_In_ const std::string& startType) | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
if (startType == STRING_SAI_START_TYPE_COLD_BOOT) | ||
return SAI_START_TYPE_COLD_BOOT; | ||
|
||
if (startType == STRING_SAI_START_TYPE_WARM_BOOT) | ||
return SAI_START_TYPE_WARM_BOOT; | ||
|
||
if (startType == STRING_SAI_START_TYPE_FAST_BOOT) | ||
return SAI_START_TYPE_FAST_BOOT; | ||
|
||
if (startType == STRING_SAI_START_TYPE_FASTFAST_BOOT) | ||
return SAI_START_TYPE_FASTFAST_BOOT; | ||
|
||
if (startType == STRING_SAI_START_TYPE_UNKNOWN) | ||
return SAI_START_TYPE_UNKNOWN; | ||
|
||
SWSS_LOG_WARN("unknown startType: '%s'", startType.c_str()); | ||
|
||
return SAI_START_TYPE_UNKNOWN; | ||
} | ||
|
||
std::string CommandLineOptions::startTypeToString( | ||
_In_ sai_start_type_t startType) | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
switch (startType) | ||
{ | ||
case SAI_START_TYPE_COLD_BOOT: | ||
return STRING_SAI_START_TYPE_COLD_BOOT; | ||
|
||
case SAI_START_TYPE_WARM_BOOT: | ||
return STRING_SAI_START_TYPE_WARM_BOOT; | ||
|
||
case SAI_START_TYPE_FAST_BOOT: | ||
return STRING_SAI_START_TYPE_FAST_BOOT; | ||
|
||
case SAI_START_TYPE_FASTFAST_BOOT: | ||
return STRING_SAI_START_TYPE_FASTFAST_BOOT; | ||
|
||
case SAI_START_TYPE_UNKNOWN: | ||
return STRING_SAI_START_TYPE_UNKNOWN; | ||
|
||
default: | ||
|
||
SWSS_LOG_WARN("unknown startType '%d'", startType); | ||
|
||
return STRING_SAI_START_TYPE_UNKNOWN; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#pragma once | ||
|
||
#include "swss/sal.h" | ||
|
||
#include <string> | ||
|
||
#define STRING_SAI_START_TYPE_COLD_BOOT "cold" | ||
#define STRING_SAI_START_TYPE_WARM_BOOT "warm" | ||
#define STRING_SAI_START_TYPE_FAST_BOOT "fast" | ||
#define STRING_SAI_START_TYPE_FASTFAST_BOOT "fastfast" | ||
#define STRING_SAI_START_TYPE_UNKNOWN "unknown" | ||
|
||
typedef enum _sai_start_type_t | ||
{ | ||
SAI_START_TYPE_COLD_BOOT = 0, | ||
|
||
SAI_START_TYPE_WARM_BOOT = 1, | ||
|
||
SAI_START_TYPE_FAST_BOOT = 2, | ||
|
||
/** | ||
* A special type of boot used by Mellanox platforms to start in 'fastfast' | ||
* boot mode | ||
*/ | ||
SAI_START_TYPE_FASTFAST_BOOT = 3, | ||
|
||
/** | ||
* Set at last, just for error purpose. | ||
*/ | ||
SAI_START_TYPE_UNKNOWN | ||
|
||
} sai_start_type_t; | ||
|
||
class CommandLineOptions | ||
{ | ||
public: | ||
|
||
CommandLineOptions(); | ||
|
||
virtual ~CommandLineOptions() = default; | ||
|
||
public: | ||
|
||
virtual std::string getCommandLineString() const; | ||
|
||
public: | ||
|
||
static sai_start_type_t startTypeStringToStartType( | ||
_In_ const std::string& startType); | ||
|
||
static std::string startTypeToString( | ||
_In_ sai_start_type_t startType); | ||
|
||
public: | ||
|
||
bool m_enableDiagShell; | ||
bool m_enableTempView; | ||
bool m_disableExitSleep; | ||
bool m_enableUnittests; | ||
|
||
/** | ||
* When set to true will enable DB vs ASIC consistency check after | ||
* comparison logic. | ||
*/ | ||
bool m_enableConsistencyCheck; | ||
|
||
bool m_enableSyncMode; | ||
|
||
sai_start_type_t m_startType; | ||
|
||
std::string m_profileMapFile; | ||
|
||
#ifdef SAITHRIFT | ||
bool m_runRPCServer; | ||
std::string m_portMapFile; | ||
#endif // SAITHRIFT | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#include "CommandLineOptionsParser.h" | ||
|
||
#include <getopt.h> | ||
|
||
std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine( | ||
_In_ int argc, | ||
_In_ char **argv) | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
auto options = std::make_shared<CommandLineOptions>(); | ||
|
||
#ifdef SAITHRIFT | ||
const char* const optstring = "dp:t:uSUCsrm:h"; | ||
#else | ||
const char* const optstring = "dp:t:uSUCsh"; | ||
#endif // SAITHRIFT | ||
|
||
while(true) | ||
{ | ||
static struct option long_options[] = | ||
{ | ||
{ "diag", no_argument, 0, 'd' }, | ||
{ "profile", required_argument, 0, 'p' }, | ||
{ "startType", required_argument, 0, 't' }, | ||
{ "useTempView", no_argument, 0, 'u' }, | ||
{ "disableExitSleep", no_argument, 0, 'S' }, | ||
{ "enableUnittests", no_argument, 0, 'U' }, | ||
{ "enableConsistencyCheck", no_argument, 0, 'C' }, | ||
{ "syncMode", no_argument, 0, 's' }, | ||
#ifdef SAITHRIFT | ||
{ "rpcserver", no_argument, 0, 'r' }, | ||
{ "portmap", required_argument, 0, 'm' }, | ||
#endif // SAITHRIFT | ||
{ "help", no_argument, 0, 'h' }, | ||
{ 0, 0, 0, 0 } | ||
}; | ||
|
||
int option_index = 0; | ||
|
||
int c = getopt_long(argc, argv, optstring, long_options, &option_index); | ||
|
||
if (c == -1) | ||
{ | ||
break; | ||
} | ||
|
||
switch (c) | ||
{ | ||
case 'd': | ||
options->m_enableDiagShell = true; | ||
break; | ||
|
||
case 'p': | ||
options->m_profileMapFile = std::string(optarg); | ||
break; | ||
|
||
case 't': | ||
options->m_startType = CommandLineOptions::startTypeStringToStartType(optarg); | ||
|
||
if (options->m_startType == SAI_START_TYPE_UNKNOWN) | ||
{ | ||
SWSS_LOG_ERROR("unknown start type '%s'", optarg); | ||
exit(EXIT_FAILURE); | ||
} | ||
break; | ||
|
||
case 'u': | ||
options->m_enableTempView = true; | ||
break; | ||
|
||
case 'S': | ||
options->m_disableExitSleep = true; | ||
break; | ||
|
||
case 'U': | ||
options->m_enableUnittests = true; | ||
break; | ||
|
||
case 'C': | ||
options->m_enableConsistencyCheck = true; | ||
break; | ||
|
||
case 's': | ||
options->m_enableSyncMode = true; | ||
break; | ||
|
||
#ifdef SAITHRIFT | ||
case 'r': | ||
options->m_runRPCServer = true; | ||
break; | ||
case 'm': | ||
options->m_portMapFile = std::string(optarg); | ||
break; | ||
#endif // SAITHRIFT | ||
|
||
case 'h': | ||
printUsage(); | ||
exit(EXIT_SUCCESS); | ||
|
||
case '?': | ||
SWSS_LOG_WARN("unknown option %c", optopt); | ||
printUsage(); | ||
exit(EXIT_FAILURE); | ||
|
||
default: | ||
SWSS_LOG_ERROR("getopt_long failure"); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
|
||
void CommandLineOptionsParser::printUsage() | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
#ifdef SAITHRIFT | ||
std::cout << "Usage: syncd [-d] [-p profile] [-t type] [-u] [-S] [-U] [-C] [-s] [-r] [-m portmap] [-h]" << std::endl; | ||
#else | ||
std::cout << "Usage: syncd [-d] [-p profile] [-t type] [-u] [-S] [-U] [-C] [-s] [-h]" << std::endl; | ||
#endif // SAITHRIFT | ||
|
||
std::cout << " -d --diag" << std::endl; | ||
std::cout << " Enable diagnostic shell" << std::endl; | ||
std::cout << " -p --profile profile" << std::endl; | ||
std::cout << " Provide profile map file" << std::endl; | ||
std::cout << " -t --startType type" << std::endl; | ||
std::cout << " Specify start type (cold|warm|fast|fastfast) " << std::endl; | ||
std::cout << " -u --useTempView" << std::endl; | ||
std::cout << " Use temporary view between init and apply" << std::endl; | ||
std::cout << " -S --disableExitSleep" << std::endl; | ||
std::cout << " Disable sleep when syncd crashes" << std::endl; | ||
std::cout << " -U --enableUnittests" << std::endl; | ||
std::cout << " Metadata enable unittests" << std::endl; | ||
std::cout << " -C --enableConsistencyCheck" << std::endl; | ||
std::cout << " Enable consisteny check DB vs ASIC after comparison logic" << std::endl; | ||
std::cout << " -s --syncMode" << std::endl; | ||
std::cout << " Enable synchronous mode" << std::endl; | ||
|
||
#ifdef SAITHRIFT | ||
|
||
std::cout << " -r --rpcserver" << std::endl; | ||
std::cout << " Enable rpcserver" << std::endl; | ||
std::cout << " -m --portmap portmap" << std::endl; | ||
std::cout << " Specify port map file" << std::endl; | ||
|
||
#endif // SAITHRIFT | ||
|
||
std::cout << " -h --help" << std::endl; | ||
std::cout << " Print out this message" << std::endl; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include "syncd.h" | ||
#include "CommandLineOptions.h" | ||
|
||
#include <memory> | ||
|
||
class CommandLineOptionsParser | ||
{ | ||
private: | ||
|
||
CommandLineOptionsParser() = delete; | ||
|
||
~CommandLineOptionsParser() = delete; | ||
|
||
public: | ||
|
||
static std::shared_ptr<CommandLineOptions> parseCommandLine( | ||
_In_ int argc, | ||
_In_ char **argv); | ||
|
||
static void printUsage(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.