Skip to content

Commit

Permalink
[sairedis] Client/Server support zmq configuration file (sonic-net#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Jun 24, 2021
1 parent 7c70e34 commit 677ebca
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 5 deletions.
29 changes: 29 additions & 0 deletions lib/inc/ClientConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "swss/sal.h"

#include <memory>
#include <string>

namespace sairedis
{
class ClientConfig
{
public:

ClientConfig();

virtual ~ClientConfig();

public:

static std::shared_ptr<ClientConfig> loadFromFile(
_In_ const char* path);

public:

std::string m_zmqEndpoint;

std::string m_zmqNtfEndpoint;
};
}
29 changes: 29 additions & 0 deletions lib/inc/ServerConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "swss/sal.h"

#include <memory>
#include <string>

namespace sairedis
{
class ServerConfig
{
public:

ServerConfig();

virtual ~ServerConfig();

public:

static std::shared_ptr<ServerConfig> loadFromFile(
_In_ const char* path);

public:

std::string m_zmqEndpoint;

std::string m_zmqNtfEndpoint;
};
}
16 changes: 16 additions & 0 deletions lib/inc/sairedis.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ extern "C" {
*/
#define SAI_REDIS_KEY_ENABLE_CLIENT "SAI_REDIS_ENABLE_CLIENT"

/**
* @brief Redis client config.
*
* Optional. Should point to client_config.json file which contains
* client/server channel configuration for client side.
*/
#define SAI_REDIS_KEY_CLIENT_CONFIG "SAI_REDIS_CLIENT_CONFIG"

/**
* @brief Redis server config.
*
* Optional. Should point to server_config.json file which contains
* client/server channel configuration for server side.
*/
#define SAI_REDIS_KEY_SERVER_CONFIG "SAI_REDIS_SERVER_CONFIG"

/**
* @brief Default synchronous operation response timeout in milliseconds.
*/
Expand Down
74 changes: 74 additions & 0 deletions lib/src/ClientConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "ClientConfig.h"

#include "swss/logger.h"
#include "swss/json.hpp"

#include <cstring>
#include <fstream>

using json = nlohmann::json;

using namespace sairedis;

ClientConfig::ClientConfig():
m_zmqEndpoint("ipc:///tmp/saiServer"),
m_zmqNtfEndpoint("ipc:///tmp/saiServerNtf")
{
SWSS_LOG_ENTER();

// empty intentionally
}

ClientConfig::~ClientConfig()
{
SWSS_LOG_ENTER();

// empty intentionally
}

std::shared_ptr<ClientConfig> ClientConfig::loadFromFile(
_In_ const char* path)
{
SWSS_LOG_ENTER();

if (path == nullptr || strlen(path) == 0)
{
SWSS_LOG_NOTICE("no client config specified, will load default");

return std::make_shared<ClientConfig>();
}

std::ifstream ifs(path);

if (!ifs.good())
{
SWSS_LOG_ERROR("failed to read '%s', err: %s, returning default", path, strerror(errno));

return std::make_shared<ClientConfig>();
}

try
{
json j;
ifs >> j;

auto cc = std::make_shared<ClientConfig>();

cc->m_zmqEndpoint = j["zmq_endpoint"];
cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"];

SWSS_LOG_NOTICE("client config: %s, %s",
cc->m_zmqEndpoint.c_str(),
cc->m_zmqNtfEndpoint.c_str());

SWSS_LOG_NOTICE("loaded %s client config", path);

return cc;
}
catch (const std::exception& e)
{
SWSS_LOG_ERROR("Failed to load '%s': %s, returning default", path, e.what());

return std::make_shared<ClientConfig>();
}
}
19 changes: 16 additions & 3 deletions lib/src/ClientSai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "sairediscommon.h"
#include "PerformanceIntervalTimer.h"
#include "NotificationFactory.h"
#include "ClientConfig.h"

#include "swss/logger.h"

Expand Down Expand Up @@ -65,14 +66,26 @@ sai_status_t ClientSai::initialize(
return SAI_STATUS_FAILURE;
}

if ((service_method_table == NULL) ||
(service_method_table->profile_get_next_value == NULL) ||
(service_method_table->profile_get_value == NULL))
{
SWSS_LOG_ERROR("invalid service_method_table handle passed to SAI API initialize");

return SAI_STATUS_INVALID_PARAMETER;
}

// TODO support context config

m_switchContainer = std::make_shared<SwitchContainer>();

// TODO from config/method table
auto clientConfig = service_method_table->profile_get_value(0, SAI_REDIS_KEY_CLIENT_CONFIG);

auto cc = ClientConfig::loadFromFile(clientConfig);

m_communicationChannel = std::make_shared<ZeroMQChannel>(
"ipc:///tmp/saiServer",
"ipc:///tmp/saiServerNtf",
cc->m_zmqEndpoint,
cc->m_zmqNtfEndpoint,
std::bind(&ClientSai::handleNotification, this, _1, _2, _3));

m_apiInitialized = true;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ noinst_LIBRARIES = libSaiRedis.a
libSaiRedis_a_SOURCES = \
../../syncd/ZeroMQSelectableChannel.cpp \
../../syncd/SelectableChannel.cpp \
ClientConfig.cpp \
ServerConfig.cpp \
ClientServerSai.cpp \
ClientSai.cpp \
ServerSai.cpp \
Expand Down
74 changes: 74 additions & 0 deletions lib/src/ServerConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "ServerConfig.h"

#include "swss/logger.h"
#include "swss/json.hpp"

#include <cstring>
#include <fstream>

using json = nlohmann::json;

using namespace sairedis;

ServerConfig::ServerConfig():
m_zmqEndpoint("ipc:///tmp/saiServer"),
m_zmqNtfEndpoint("ipc:///tmp/saiServerNtf")
{
SWSS_LOG_ENTER();

// empty intentionally
}

ServerConfig::~ServerConfig()
{
SWSS_LOG_ENTER();

// empty intentionally
}

std::shared_ptr<ServerConfig> ServerConfig::loadFromFile(
_In_ const char* path)
{
SWSS_LOG_ENTER();

if (path == nullptr || strlen(path) == 0)
{
SWSS_LOG_NOTICE("no server config specified, will load default");

return std::make_shared<ServerConfig>();
}

std::ifstream ifs(path);

if (!ifs.good())
{
SWSS_LOG_ERROR("failed to read '%s', err: %s, returning default", path, strerror(errno));

return std::make_shared<ServerConfig>();
}

try
{
json j;
ifs >> j;

auto cc = std::make_shared<ServerConfig>();

cc->m_zmqEndpoint = j["zmq_endpoint"];
cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"];

SWSS_LOG_NOTICE("server config: %s, %s",
cc->m_zmqEndpoint.c_str(),
cc->m_zmqNtfEndpoint.c_str());

SWSS_LOG_NOTICE("loaded %s server config", path);

return cc;
}
catch (const std::exception& e)
{
SWSS_LOG_ERROR("Failed to load '%s': %s, returning default", path, e.what());

return std::make_shared<ServerConfig>();
}
}
8 changes: 6 additions & 2 deletions lib/src/ServerSai.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ServerSai.h"
#include "SaiInternal.h"
#include "Sai.h"
#include "ServerConfig.h"
#include "sairediscommon.h"

#include "syncd/ZeroMQSelectableChannel.h"
Expand Down Expand Up @@ -82,8 +83,11 @@ sai_status_t ServerSai::initialize(

if (status == SAI_STATUS_SUCCESS)
{
// TODO from config
m_selectableChannel = std::make_shared<syncd::ZeroMQSelectableChannel>("ipc:///tmp/saiServer");
auto serverConfig = service_method_table->profile_get_value(0, SAI_REDIS_KEY_SERVER_CONFIG);

auto cc = ServerConfig::loadFromFile(serverConfig);

m_selectableChannel = std::make_shared<syncd::ZeroMQSelectableChannel>(cc->m_zmqEndpoint);

SWSS_LOG_NOTICE("starting server thread");

Expand Down
4 changes: 4 additions & 0 deletions lib/src/client_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"zmq_endpoint": "ipc:///tmp/saiServer",
"zmq_ntf_endpoint": "ipc:///tmp/saiServerNtf"
}

0 comments on commit 677ebca

Please sign in to comment.