Skip to content

Commit 677ebca

Browse files
authored
[sairedis] Client/Server support zmq configuration file (#845)
1 parent 7c70e34 commit 677ebca

9 files changed

+250
-5
lines changed

lib/inc/ClientConfig.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "swss/sal.h"
4+
5+
#include <memory>
6+
#include <string>
7+
8+
namespace sairedis
9+
{
10+
class ClientConfig
11+
{
12+
public:
13+
14+
ClientConfig();
15+
16+
virtual ~ClientConfig();
17+
18+
public:
19+
20+
static std::shared_ptr<ClientConfig> loadFromFile(
21+
_In_ const char* path);
22+
23+
public:
24+
25+
std::string m_zmqEndpoint;
26+
27+
std::string m_zmqNtfEndpoint;
28+
};
29+
}

lib/inc/ServerConfig.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "swss/sal.h"
4+
5+
#include <memory>
6+
#include <string>
7+
8+
namespace sairedis
9+
{
10+
class ServerConfig
11+
{
12+
public:
13+
14+
ServerConfig();
15+
16+
virtual ~ServerConfig();
17+
18+
public:
19+
20+
static std::shared_ptr<ServerConfig> loadFromFile(
21+
_In_ const char* path);
22+
23+
public:
24+
25+
std::string m_zmqEndpoint;
26+
27+
std::string m_zmqNtfEndpoint;
28+
};
29+
}

lib/inc/sairedis.h

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ extern "C" {
2424
*/
2525
#define SAI_REDIS_KEY_ENABLE_CLIENT "SAI_REDIS_ENABLE_CLIENT"
2626

27+
/**
28+
* @brief Redis client config.
29+
*
30+
* Optional. Should point to client_config.json file which contains
31+
* client/server channel configuration for client side.
32+
*/
33+
#define SAI_REDIS_KEY_CLIENT_CONFIG "SAI_REDIS_CLIENT_CONFIG"
34+
35+
/**
36+
* @brief Redis server config.
37+
*
38+
* Optional. Should point to server_config.json file which contains
39+
* client/server channel configuration for server side.
40+
*/
41+
#define SAI_REDIS_KEY_SERVER_CONFIG "SAI_REDIS_SERVER_CONFIG"
42+
2743
/**
2844
* @brief Default synchronous operation response timeout in milliseconds.
2945
*/

lib/src/ClientConfig.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "ClientConfig.h"
2+
3+
#include "swss/logger.h"
4+
#include "swss/json.hpp"
5+
6+
#include <cstring>
7+
#include <fstream>
8+
9+
using json = nlohmann::json;
10+
11+
using namespace sairedis;
12+
13+
ClientConfig::ClientConfig():
14+
m_zmqEndpoint("ipc:///tmp/saiServer"),
15+
m_zmqNtfEndpoint("ipc:///tmp/saiServerNtf")
16+
{
17+
SWSS_LOG_ENTER();
18+
19+
// empty intentionally
20+
}
21+
22+
ClientConfig::~ClientConfig()
23+
{
24+
SWSS_LOG_ENTER();
25+
26+
// empty intentionally
27+
}
28+
29+
std::shared_ptr<ClientConfig> ClientConfig::loadFromFile(
30+
_In_ const char* path)
31+
{
32+
SWSS_LOG_ENTER();
33+
34+
if (path == nullptr || strlen(path) == 0)
35+
{
36+
SWSS_LOG_NOTICE("no client config specified, will load default");
37+
38+
return std::make_shared<ClientConfig>();
39+
}
40+
41+
std::ifstream ifs(path);
42+
43+
if (!ifs.good())
44+
{
45+
SWSS_LOG_ERROR("failed to read '%s', err: %s, returning default", path, strerror(errno));
46+
47+
return std::make_shared<ClientConfig>();
48+
}
49+
50+
try
51+
{
52+
json j;
53+
ifs >> j;
54+
55+
auto cc = std::make_shared<ClientConfig>();
56+
57+
cc->m_zmqEndpoint = j["zmq_endpoint"];
58+
cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"];
59+
60+
SWSS_LOG_NOTICE("client config: %s, %s",
61+
cc->m_zmqEndpoint.c_str(),
62+
cc->m_zmqNtfEndpoint.c_str());
63+
64+
SWSS_LOG_NOTICE("loaded %s client config", path);
65+
66+
return cc;
67+
}
68+
catch (const std::exception& e)
69+
{
70+
SWSS_LOG_ERROR("Failed to load '%s': %s, returning default", path, e.what());
71+
72+
return std::make_shared<ClientConfig>();
73+
}
74+
}

lib/src/ClientSai.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "sairediscommon.h"
77
#include "PerformanceIntervalTimer.h"
88
#include "NotificationFactory.h"
9+
#include "ClientConfig.h"
910

1011
#include "swss/logger.h"
1112

@@ -65,14 +66,26 @@ sai_status_t ClientSai::initialize(
6566
return SAI_STATUS_FAILURE;
6667
}
6768

69+
if ((service_method_table == NULL) ||
70+
(service_method_table->profile_get_next_value == NULL) ||
71+
(service_method_table->profile_get_value == NULL))
72+
{
73+
SWSS_LOG_ERROR("invalid service_method_table handle passed to SAI API initialize");
74+
75+
return SAI_STATUS_INVALID_PARAMETER;
76+
}
77+
6878
// TODO support context config
6979

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

72-
// TODO from config/method table
82+
auto clientConfig = service_method_table->profile_get_value(0, SAI_REDIS_KEY_CLIENT_CONFIG);
83+
84+
auto cc = ClientConfig::loadFromFile(clientConfig);
85+
7386
m_communicationChannel = std::make_shared<ZeroMQChannel>(
74-
"ipc:///tmp/saiServer",
75-
"ipc:///tmp/saiServerNtf",
87+
cc->m_zmqEndpoint,
88+
cc->m_zmqNtfEndpoint,
7689
std::bind(&ClientSai::handleNotification, this, _1, _2, _3));
7790

7891
m_apiInitialized = true;

lib/src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ noinst_LIBRARIES = libSaiRedis.a
1212
libSaiRedis_a_SOURCES = \
1313
../../syncd/ZeroMQSelectableChannel.cpp \
1414
../../syncd/SelectableChannel.cpp \
15+
ClientConfig.cpp \
16+
ServerConfig.cpp \
1517
ClientServerSai.cpp \
1618
ClientSai.cpp \
1719
ServerSai.cpp \

lib/src/ServerConfig.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "ServerConfig.h"
2+
3+
#include "swss/logger.h"
4+
#include "swss/json.hpp"
5+
6+
#include <cstring>
7+
#include <fstream>
8+
9+
using json = nlohmann::json;
10+
11+
using namespace sairedis;
12+
13+
ServerConfig::ServerConfig():
14+
m_zmqEndpoint("ipc:///tmp/saiServer"),
15+
m_zmqNtfEndpoint("ipc:///tmp/saiServerNtf")
16+
{
17+
SWSS_LOG_ENTER();
18+
19+
// empty intentionally
20+
}
21+
22+
ServerConfig::~ServerConfig()
23+
{
24+
SWSS_LOG_ENTER();
25+
26+
// empty intentionally
27+
}
28+
29+
std::shared_ptr<ServerConfig> ServerConfig::loadFromFile(
30+
_In_ const char* path)
31+
{
32+
SWSS_LOG_ENTER();
33+
34+
if (path == nullptr || strlen(path) == 0)
35+
{
36+
SWSS_LOG_NOTICE("no server config specified, will load default");
37+
38+
return std::make_shared<ServerConfig>();
39+
}
40+
41+
std::ifstream ifs(path);
42+
43+
if (!ifs.good())
44+
{
45+
SWSS_LOG_ERROR("failed to read '%s', err: %s, returning default", path, strerror(errno));
46+
47+
return std::make_shared<ServerConfig>();
48+
}
49+
50+
try
51+
{
52+
json j;
53+
ifs >> j;
54+
55+
auto cc = std::make_shared<ServerConfig>();
56+
57+
cc->m_zmqEndpoint = j["zmq_endpoint"];
58+
cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"];
59+
60+
SWSS_LOG_NOTICE("server config: %s, %s",
61+
cc->m_zmqEndpoint.c_str(),
62+
cc->m_zmqNtfEndpoint.c_str());
63+
64+
SWSS_LOG_NOTICE("loaded %s server config", path);
65+
66+
return cc;
67+
}
68+
catch (const std::exception& e)
69+
{
70+
SWSS_LOG_ERROR("Failed to load '%s': %s, returning default", path, e.what());
71+
72+
return std::make_shared<ServerConfig>();
73+
}
74+
}

lib/src/ServerSai.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ServerSai.h"
22
#include "SaiInternal.h"
33
#include "Sai.h"
4+
#include "ServerConfig.h"
45
#include "sairediscommon.h"
56

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

8384
if (status == SAI_STATUS_SUCCESS)
8485
{
85-
// TODO from config
86-
m_selectableChannel = std::make_shared<syncd::ZeroMQSelectableChannel>("ipc:///tmp/saiServer");
86+
auto serverConfig = service_method_table->profile_get_value(0, SAI_REDIS_KEY_SERVER_CONFIG);
87+
88+
auto cc = ServerConfig::loadFromFile(serverConfig);
89+
90+
m_selectableChannel = std::make_shared<syncd::ZeroMQSelectableChannel>(cc->m_zmqEndpoint);
8791

8892
SWSS_LOG_NOTICE("starting server thread");
8993

lib/src/client_config.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"zmq_endpoint": "ipc:///tmp/saiServer",
3+
"zmq_ntf_endpoint": "ipc:///tmp/saiServerNtf"
4+
}

0 commit comments

Comments
 (0)