From 677ebcad2803ede3e0cf5606680803b5ced79a65 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Thu, 24 Jun 2021 14:44:55 +0200 Subject: [PATCH] [sairedis] Client/Server support zmq configuration file (#845) --- lib/inc/ClientConfig.h | 29 +++++++++++++++ lib/inc/ServerConfig.h | 29 +++++++++++++++ lib/inc/sairedis.h | 16 +++++++++ lib/src/ClientConfig.cpp | 74 ++++++++++++++++++++++++++++++++++++++ lib/src/ClientSai.cpp | 19 ++++++++-- lib/src/Makefile.am | 2 ++ lib/src/ServerConfig.cpp | 74 ++++++++++++++++++++++++++++++++++++++ lib/src/ServerSai.cpp | 8 +++-- lib/src/client_config.json | 4 +++ 9 files changed, 250 insertions(+), 5 deletions(-) create mode 100644 lib/inc/ClientConfig.h create mode 100644 lib/inc/ServerConfig.h create mode 100644 lib/src/ClientConfig.cpp create mode 100644 lib/src/ServerConfig.cpp create mode 100644 lib/src/client_config.json diff --git a/lib/inc/ClientConfig.h b/lib/inc/ClientConfig.h new file mode 100644 index 000000000000..3ad8804e5ec3 --- /dev/null +++ b/lib/inc/ClientConfig.h @@ -0,0 +1,29 @@ +#pragma once + +#include "swss/sal.h" + +#include +#include + +namespace sairedis +{ + class ClientConfig + { + public: + + ClientConfig(); + + virtual ~ClientConfig(); + + public: + + static std::shared_ptr loadFromFile( + _In_ const char* path); + + public: + + std::string m_zmqEndpoint; + + std::string m_zmqNtfEndpoint; + }; +} diff --git a/lib/inc/ServerConfig.h b/lib/inc/ServerConfig.h new file mode 100644 index 000000000000..4eea4dba1ec4 --- /dev/null +++ b/lib/inc/ServerConfig.h @@ -0,0 +1,29 @@ +#pragma once + +#include "swss/sal.h" + +#include +#include + +namespace sairedis +{ + class ServerConfig + { + public: + + ServerConfig(); + + virtual ~ServerConfig(); + + public: + + static std::shared_ptr loadFromFile( + _In_ const char* path); + + public: + + std::string m_zmqEndpoint; + + std::string m_zmqNtfEndpoint; + }; +} diff --git a/lib/inc/sairedis.h b/lib/inc/sairedis.h index 036bded2c6d8..924226205b36 100644 --- a/lib/inc/sairedis.h +++ b/lib/inc/sairedis.h @@ -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. */ diff --git a/lib/src/ClientConfig.cpp b/lib/src/ClientConfig.cpp new file mode 100644 index 000000000000..7a165d5ad70a --- /dev/null +++ b/lib/src/ClientConfig.cpp @@ -0,0 +1,74 @@ +#include "ClientConfig.h" + +#include "swss/logger.h" +#include "swss/json.hpp" + +#include +#include + +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::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(); + } + + 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(); + } + + try + { + json j; + ifs >> j; + + auto cc = std::make_shared(); + + 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(); + } +} diff --git a/lib/src/ClientSai.cpp b/lib/src/ClientSai.cpp index 8e6e0782b637..9260830346a4 100644 --- a/lib/src/ClientSai.cpp +++ b/lib/src/ClientSai.cpp @@ -6,6 +6,7 @@ #include "sairediscommon.h" #include "PerformanceIntervalTimer.h" #include "NotificationFactory.h" +#include "ClientConfig.h" #include "swss/logger.h" @@ -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(); - // 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( - "ipc:///tmp/saiServer", - "ipc:///tmp/saiServerNtf", + cc->m_zmqEndpoint, + cc->m_zmqNtfEndpoint, std::bind(&ClientSai::handleNotification, this, _1, _2, _3)); m_apiInitialized = true; diff --git a/lib/src/Makefile.am b/lib/src/Makefile.am index bb4ced5468e9..0f23a8f19d34 100644 --- a/lib/src/Makefile.am +++ b/lib/src/Makefile.am @@ -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 \ diff --git a/lib/src/ServerConfig.cpp b/lib/src/ServerConfig.cpp new file mode 100644 index 000000000000..09f8e7a0df3d --- /dev/null +++ b/lib/src/ServerConfig.cpp @@ -0,0 +1,74 @@ +#include "ServerConfig.h" + +#include "swss/logger.h" +#include "swss/json.hpp" + +#include +#include + +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::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(); + } + + 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(); + } + + try + { + json j; + ifs >> j; + + auto cc = std::make_shared(); + + 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(); + } +} diff --git a/lib/src/ServerSai.cpp b/lib/src/ServerSai.cpp index ad03f3c3d76f..8befd78f0ed3 100644 --- a/lib/src/ServerSai.cpp +++ b/lib/src/ServerSai.cpp @@ -1,6 +1,7 @@ #include "ServerSai.h" #include "SaiInternal.h" #include "Sai.h" +#include "ServerConfig.h" #include "sairediscommon.h" #include "syncd/ZeroMQSelectableChannel.h" @@ -82,8 +83,11 @@ sai_status_t ServerSai::initialize( if (status == SAI_STATUS_SUCCESS) { - // TODO from config - m_selectableChannel = std::make_shared("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(cc->m_zmqEndpoint); SWSS_LOG_NOTICE("starting server thread"); diff --git a/lib/src/client_config.json b/lib/src/client_config.json new file mode 100644 index 000000000000..21f45e3cc520 --- /dev/null +++ b/lib/src/client_config.json @@ -0,0 +1,4 @@ +{ + "zmq_endpoint": "ipc:///tmp/saiServer", + "zmq_ntf_endpoint": "ipc:///tmp/saiServerNtf" +}