Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ namespace swss {
#define APP_SFLOW_SESSION_TABLE_NAME "SFLOW_SESSION_TABLE"
#define APP_SFLOW_SAMPLE_RATE_TABLE_NAME "SFLOW_SAMPLE_RATE_TABLE"

#define APP_DEBUGFM_CONFIG_TABLE_NAME "DEBUGFM_CONFIG_TABLE"
#define APP_DEBUG_RCOMPONENT_TABLE_NAME "DEBUG_RCOMPONENT_TABLE"
#define APP_DEBUG_COMPONENT_TABLE_NAME "DEBUG_COMPONENT_TABLE"
#define APP_DEBUG_COMP_DONE_TABLE_NAME "DEBUG_COMP_DONE_TABLE"

#define APP_NAT_TABLE_NAME "NAT_TABLE"
#define APP_NAPT_TABLE_NAME "NAPT_TABLE"
#define APP_NAT_TWICE_TABLE_NAME "NAT_TWICE_TABLE"
Expand All @@ -123,6 +128,12 @@ namespace swss {
#define APP_STP_MST_PORT_TABLE_NAME "STP_MST_PORT_TABLE"
#define APP_STP_INST_PORT_FLUSH_TABLE_NAME "STP_INST_PORT_FLUSH_TABLE"

/* Multicast related APP_DB Tables */
#define APP_L2MC_VLAN_TABLE_NAME "L2MC_VLAN_TABLE"
#define APP_L2MC_SUPPRESS_TABLE_NAME "L2MC_SUPPRESS_TABLE"
#define APP_L2MC_MEMBER_TABLE_NAME "L2MC_MEMBER_TABLE"
#define APP_L2MC_MROUTER_TABLE_NAME "L2MC_MROUTER_TABLE"


#define APP_SAG_TABLE_NAME "SAG_TABLE"

Expand Down Expand Up @@ -401,6 +412,17 @@ after libswsscommon deb make.
#define CHASSIS_APP_SYSTEM_NEIGH_TABLE_NAME "SYSTEM_NEIGH"
#define CHASSIS_APP_LAG_TABLE_NAME "SYSTEM_LAG_TABLE"
#define CHASSIS_APP_LAG_MEMBER_TABLE_NAME "SYSTEM_LAG_MEMBER_TABLE"

#define CFG_L2MC_TABLE_NAME "L2MC"
#define CFG_L2MC_SUPPRESS_TABLE_NAME "L2MC_SUPPRESS"
#define CFG_L2MC_MROUTER_TABLE_NAME "L2MC_MROUTER"
#define CFG_L2MC_STATIC_TABLE_NAME "L2MC_STATIC_MEMBER"
#define CFG_L2MC_STATIC_GROUP_TABLE_NAME "L2MC_STATIC_GROUP"

#define CFG_MLD_L2MC_TABLE_NAME "MLD_L2MC"
#define CFG_MLD_L2MC_MROUTER_TABLE_NAME "MLD_L2MC_MROUTER"
#define CFG_MLD_L2MC_STATIC_TABLE_NAME "MLD_L2MC_STATIC_MEMBER"
#define CFG_MLD_L2MC_STATIC_GROUP_TABLE_NAME "MLD_L2MC_STATIC_GROUP"
/***** STATE DATABASE *****/

#define STATE_SWITCH_CAPABILITY_TABLE_NAME "SWITCH_CAPABILITY"
Expand Down Expand Up @@ -499,6 +521,9 @@ after libswsscommon deb make.

#define STATE_BGP_PEER_CONFIGURED_TABLE_NAME "BGP_PEER_CONFIGURED_TABLE"

#define STATE_L2MC_MEMBER_TABLE_NAME "L2MC_MEMBER_TABLE"
#define STATE_L2MC_MROUTER_TABLE_NAME "L2MC_MROUTER_TABLE"

// ACL table and ACL rule table
#define STATE_ACL_TABLE_TABLE_NAME "ACL_TABLE_TABLE"
#define STATE_ACL_RULE_TABLE_NAME "ACL_RULE_TABLE"
Expand Down
10 changes: 0 additions & 10 deletions sonic-db-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ using namespace std;

int main(int argc, char** argv)
{
auto initializeGlobalConfig = []()
{
SonicDBConfig::initializeGlobalConfig(SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
};

auto initializeConfig = []()
{
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
};

return cli_exception_wrapper(
argc,
argv,
Expand Down
100 changes: 85 additions & 15 deletions sonic-db-cli/sonic-db-cli.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
#include <future>
#include <fstream>
#include <iostream>
#include <getopt.h>
#include <list>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/find.hpp>
#include "common/redisreply.h"
#include <nlohmann/json.hpp>
#include "sonic-db-cli.h"

using namespace swss;
using namespace std;

void initializeGlobalConfig()
{
SonicDBConfig::initializeGlobalConfig(SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
}

void initializeConfig(const string& container_name = "")
{
if(container_name.empty())
{
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
}
else
{
auto path = getContainerFilePath(container_name, SONIC_DB_CONFIG_DIR, SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
SonicDBConfig::initialize(path);
}
};

void printUsage()
{
cout << "usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] db_or_op [cmd [cmd ...]]" << endl;
cout << "usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] [-c CONTAINER_NAME] db_or_op [cmd [cmd ...]]" << endl;
cout << endl;
cout << "SONiC DB CLI:" << endl;
cout << endl;
Expand All @@ -25,6 +45,8 @@ void printUsage()
cout << " -s, --unixsocket Override use of tcp_port and use unixsocket" << endl;
cout << " -n NAMESPACE, --namespace NAMESPACE" << endl;
cout << " Namespace string to use asic0/asic1.../asicn" << endl;
cout << " -c CONTAINER_NAME, --container_name CONTAINER_NAME" << endl;
cout << " Container name for accessing container database instead of default dpu0/dpu1.../dpu3" << endl;
cout << endl;
cout << "**sudo** needed for commands accesing a different namespace [-n], or using unixsocket connection [-s]" << endl;
cout << endl;
Expand Down Expand Up @@ -116,7 +138,7 @@ int handleAllInstances(
{
return 1;
}

if (operation == "PING")
{
cout << "PONG" << endl;
Expand Down Expand Up @@ -186,15 +208,16 @@ void parseCliArguments(
Options &options)
{
// Parse argument with getopt https://man7.org/linux/man-pages/man3/getopt.3.html
const char* short_options = "hsn";
const char* short_options = "hsnc";
static struct option long_options[] = {
{"help", optional_argument, NULL, 'h' },
{"unixsocket", optional_argument, NULL, 's' },
{"namespace", optional_argument, NULL, 'n' },
{"container_name", optional_argument, NULL, 'c' },
// The last element of the array has to be filled with zeros.
{0, 0, 0, 0 }
};

// prevent getopt_long print "invalid option" message.
opterr = 0;
while(optind < argc)
Expand Down Expand Up @@ -223,10 +246,31 @@ void parseCliArguments(
}
break;

case 'c':
if (optind < argc)
{
options.m_container_name = argv[optind];
optind++;
}
else
{
throw invalid_argument("container_name value option used but container name is missing.");
}
break;

default:
// argv contains unknown argument
throw invalid_argument("Unknown argument:" + string(argv[optind]));
}

if(!options.m_namespace.empty() && !options.m_container_name.empty())
{
throw invalid_argument("container_name and namespace flags cannot be used together.");
}
else if(options.m_unixsocket && !options.m_container_name.empty())
{
throw invalid_argument("container_name and unixsocket flags cannot be used together.");
}
}
else
{
Expand All @@ -248,7 +292,7 @@ int sonic_db_cli(
int argc,
char** argv,
function<void()> initializeGlobalConfig,
function<void()> initializeConfig)
function<void(const string&)> initializeConfig)
{
Options options;
try
Expand All @@ -275,6 +319,9 @@ int sonic_db_cli(
return 0;
}

// Need to reset SonicDBConfig to remove information from other database config files
SonicDBConfig::reset();

if (!options.m_db_or_op.empty())
{
auto dbOrOperation = options.m_db_or_op;
Expand All @@ -293,10 +340,7 @@ int sonic_db_cli(
{
auto commands = options.m_cmd;

if (netns.empty())
{
initializeConfig();
}
initializeConfig(options.m_container_name);

return executeCommands(dbOrOperation, commands, netns, useUnixSocket);
}
Expand All @@ -308,11 +352,9 @@ int sonic_db_cli(
// sonic-db-cli catch all possible exceptions and handle it as a failure case which not return 'OK' or 'PONG'
try
{
if (netns.empty())
{
// When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
initializeConfig();
}

// When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
initializeConfig(options.m_container_name);

return handleAllInstances(netns, dbOrOperation, useUnixSocket);
}
Expand Down Expand Up @@ -345,7 +387,7 @@ int cli_exception_wrapper(
int argc,
char** argv,
function<void()> initializeGlobalConfig,
function<void()> initializeConfig)
function<void(const string&)> initializeConfig)
{
try
{
Expand Down Expand Up @@ -381,3 +423,31 @@ string getCommandName(vector<string>& commands)

return boost::to_upper_copy<string>(commands[0]);
}

string getContainerFilePath(const string& container_name, const string& config_directory, const string& global_config_file)
{
using json = nlohmann::json;
ifstream i(global_config_file);
json global_config = json::parse(i);
for (auto& element : global_config["INCLUDES"])
{
if (element["container_name"] == container_name)
{
auto relative_path = to_string(element["include"]);

// remove the trailing " from the relative path (JSON string quotes)
if (relative_path.front() == '"' && relative_path.back() == '"') {
relative_path = relative_path.substr(1, relative_path.size() - 2);
}

// remove all preceding "../" sequences from the relative path
while (relative_path.substr(0, 3) == "../") {
relative_path = relative_path.substr(3);
}
std::stringstream path_stream;
path_stream << config_directory << "/" << relative_path;
return path_stream.str();
}
}
throw invalid_argument("container name " + container_name + " not found in global config file");
}
15 changes: 12 additions & 3 deletions sonic-db-cli/sonic-db-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@
#include "common/dbinterface.h"
#include "common/redisreply.h"

static constexpr const char *SONIC_DB_CONFIG_DIR = "/var/run";

struct Options
{
bool m_help = false;
bool m_unixsocket = false;
std::string m_container_name = "";
std::string m_namespace;
std::string m_db_or_op;
std::vector<std::string> m_cmd;
};

void initializeGlobalConfig();

void initializeConfig(const std::string& container_name);

void printUsage();

void printRedisReply(swss::RedisReply& reply);
Expand Down Expand Up @@ -52,12 +59,14 @@ int sonic_db_cli(
int argc,
char** argv,
std::function<void()> initializeGlobalConfig,
std::function<void()> initializeConfig);
std::function<void(const std::string&)> initializeConfig);

int cli_exception_wrapper(
int argc,
char** argv,
std::function<void()> initializeGlobalConfig,
std::function<void()> initializeConfig);
std::function<void(const std::string&)> initializeConfig);

std::string getCommandName(std::vector<std::string>& command);

std::string getCommandName(std::vector<std::string>& command);
std::string getContainerFilePath(const std::string& container_name, const std::string& config_directory, const std::string& global_config_file);
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tests_tests_SOURCES = tests/redis_ut.cpp \
tests/saiaclschema_ut.cpp \
tests/countertable_ut.cpp \
tests/timer_ut.cpp \
tests/config_ut.cpp \
tests/cli_ut.cpp \
tests/events_common_ut.cpp \
tests/events_service_ut.cpp \
Expand Down
16 changes: 15 additions & 1 deletion tests/c_api_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ static char LOG_LEVEL_FOR_TEST[32] = "";
static char LOG_OUTPUT_FOR_TEST[32] = "";
static const char* LOG_NAME_FOR_TEST = "test";

const string config_file = "./tests/redis_multi_db_ut_config/database_config.json";
const string global_config_file = "./tests/redis_multi_db_ut_config/database_global.json";
static void reloadConfig() {
SonicDBConfig::reset();
SonicDBConfig::initializeGlobalConfig(global_config_file);
}

static void clearDB() {
DBConnector db("TEST_DB", 0, true);
RedisReply r(&db, "FLUSHALL", REDIS_REPLY_STATUS);
Expand Down Expand Up @@ -99,6 +106,7 @@ struct SWSSStringManager {
};

TEST(c_api, ConfigDBConnector) {
reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down Expand Up @@ -231,6 +239,7 @@ void logOutputNotify(const char* component, const char* outputStr)
}

TEST(c_api, DBConnector) {
reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down Expand Up @@ -299,6 +308,7 @@ TEST(c_api, DBConnector) {
}

TEST(c_api, Table) {
reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down Expand Up @@ -359,6 +369,7 @@ TEST(c_api, Table) {
}

TEST(c_api, ConsumerProducerStateTables) {
reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down Expand Up @@ -445,6 +456,7 @@ TEST(c_api, ConsumerProducerStateTables) {
}

TEST(c_api, SubscriberStateTable) {
reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down Expand Up @@ -486,6 +498,7 @@ TEST(c_api, SubscriberStateTable) {
}

TEST(c_api, ZmqConsumerProducerStateTable) {
reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down Expand Up @@ -615,6 +628,7 @@ TEST(c_api, ZmqConsumerProducerStateTable) {
}

TEST(c_api, EventPublisher) {
reloadConfig();
SWSSStringManager sm;

// Test EventPublisher creation
Expand Down Expand Up @@ -684,7 +698,7 @@ TEST(c_api, exceptions) {
}

TEST(c_api, Logger) {

reloadConfig();
clearDB();
SWSSStringManager sm;

Expand Down
Loading