Skip to content

Commit

Permalink
[saihelper]: Create initSaiRedis function and put related code inside…
Browse files Browse the repository at this point in the history
… it (sonic-net#254)

Move SAI Redis initialization related code outside main function.
In the future, global variables could be eliminated to make code cleaner.
getTimestamp could be moved to common library as well.
  • Loading branch information
Shuotian Cheng committed Jul 16, 2017
1 parent c592731 commit 17a6d61
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 80 deletions.
80 changes: 1 addition & 79 deletions orchagent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ string gRecordFile;
/* Global database mutex */
mutex gDbMutex;

string getTimestamp()
{
char buffer[64];
struct timeval tv;
gettimeofday(&tv, NULL);

size_t size = strftime(buffer, 32 ,"%Y-%m-%d.%T.", localtime(&tv.tv_sec));
snprintf(&buffer[size], 32, "%06ld", tv.tv_usec);

return string(buffer);
}

void usage()
{
cout << "usage: orchagent [-h] [-r record_type] [-d record_location] [-b batch_size] [-m MAC]" << endl;
Expand All @@ -80,7 +68,6 @@ void sighup_handler(int signo)
gLogRotate = true;

sai_attribute_t attr;

attr.id = SAI_REDIS_SWITCH_ATTR_PERFORM_LOG_ROTATE;
attr.value.booldata = true;

Expand Down Expand Up @@ -160,74 +147,9 @@ int main(int argc, char **argv)
SWSS_LOG_NOTICE("--- Starting Orchestration Agent ---");

initSaiApi();

/*
* NOTE: Notice that all redis attributes here are using SAI_NULL_OBJECT_ID
* as switch id, because thsoe operations don't require actual switch to be
* performed, and they should be executed before creating switch.
*/
initSaiRedis(record_location);

sai_attribute_t attr;

/* Disable/enable SAI Redis recording */
if (gSairedisRecord)
{
attr.id = SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR;
attr.value.s8list.count = record_location.size();
attr.value.s8list.list = (signed char *) record_location.c_str();

status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set SAI Redis recording output folder to %s, rv:%d", record_location.c_str(), status);
exit(EXIT_FAILURE);
}
}

attr.id = SAI_REDIS_SWITCH_ATTR_RECORD;
attr.value.booldata = gSairedisRecord;

status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set SAI Redis recording to %s, rv:%d", gSairedisRecord ? "true" : "false", status);
exit(EXIT_FAILURE);
}

/* Disable/enable SwSS recording */
if (gSwssRecord)
{
gRecordFile = record_location + "/" + "swss." + getTimestamp() + ".rec";
gRecordOfs.open(gRecordFile);
if (!gRecordOfs.is_open())
{
SWSS_LOG_ERROR("Failed to open SwSS recording file %s", gRecordFile.c_str());
exit(EXIT_FAILURE);
}
}

attr.id = SAI_REDIS_SWITCH_ATTR_NOTIFY_SYNCD;
attr.value.s32 = SAI_REDIS_NOTIFY_SYNCD_INIT_VIEW;
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to notify syncd INIT_VIEW, rv:%d", status);
exit(EXIT_FAILURE);
}
SWSS_LOG_NOTICE("Notify syncd INIT_VIEW");

attr.id = SAI_REDIS_SWITCH_ATTR_USE_PIPELINE;
attr.value.booldata = true;

status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to enable redis pipeline, rv:%d", status);
exit(EXIT_FAILURE);
}
SWSS_LOG_NOTICE("Enable redis pipeline");

vector<sai_attribute_t> attrs;

attr.id = SAI_SWITCH_ATTR_INIT_SWITCH;
Expand Down
95 changes: 95 additions & 0 deletions orchagent/saihelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ extern "C" {
#include "saistatus.h"
}

#include <fstream>
#include <map>
#include <sys/time.h>

#include <logger.h>
#include <sairedis.h>

#include "saihelper.h"

using namespace std;
Expand Down Expand Up @@ -35,6 +40,12 @@ sai_acl_api_t* sai_acl_api;
sai_mirror_api_t* sai_mirror_api;
sai_fdb_api_t* sai_fdb_api;

extern sai_object_id_t gSwitchId;
extern bool gSairedisRecord;
extern bool gSwssRecord;
extern ofstream gRecordOfs;
extern string gRecordFile;

map<string, string> gProfileMap;

const char *test_profile_get_value (
Expand Down Expand Up @@ -140,3 +151,87 @@ void initSaiApi()
sai_log_set(SAI_API_SCHEDULER_GROUP, SAI_LOG_LEVEL_NOTICE);
sai_log_set(SAI_API_ACL, SAI_LOG_LEVEL_NOTICE);
}

string getTimestamp()
{
char buffer[64];
struct timeval tv;
gettimeofday(&tv, NULL);

size_t size = strftime(buffer, 32 ,"%Y-%m-%d.%T.", localtime(&tv.tv_sec));
snprintf(&buffer[size], 32, "%06ld", tv.tv_usec);

return string(buffer);
}

void initSaiRedis(const string &record_location)
{
/**
* NOTE: Notice that all Redis attributes here are using SAI_NULL_OBJECT_ID
* as the switch ID, because those operations don't require actual switch
* to be performed, and they should be executed before creating switch.
*/

/* Disable/enable SAI Redis recording */
sai_attribute_t attr;
attr.id = SAI_REDIS_SWITCH_ATTR_RECORD;
attr.value.booldata = gSairedisRecord;

sai_status_t status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to %s SAI Redis recording, rv:%d",
gSairedisRecord ? "enable" : "disable", status);
exit(EXIT_FAILURE);
}

if (gSairedisRecord)
{
attr.id = SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR;
attr.value.s8list.count = record_location.size();
attr.value.s8list.list = (signed char *) record_location.c_str();

status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set SAI Redis recording output folder to %s, rv:%d",
record_location.c_str(), status);
exit(EXIT_FAILURE);
}
}

/* Disable/enable SwSS recording */
if (gSwssRecord)
{
gRecordFile = record_location + "/" + "swss." + getTimestamp() + ".rec";
gRecordOfs.open(gRecordFile);
if (!gRecordOfs.is_open())
{
SWSS_LOG_ERROR("Failed to open SwSS recording file %s", gRecordFile.c_str());
exit(EXIT_FAILURE);
}
}

attr.id = SAI_REDIS_SWITCH_ATTR_USE_PIPELINE;
attr.value.booldata = true;

status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to enable redis pipeline, rv:%d", status);
exit(EXIT_FAILURE);
}
SWSS_LOG_NOTICE("Enable redis pipeline");

attr.id = SAI_REDIS_SWITCH_ATTR_NOTIFY_SYNCD;
attr.value.s32 = SAI_REDIS_NOTIFY_SYNCD_INIT_VIEW;
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to notify syncd INIT_VIEW, rv:%d", status);
exit(EXIT_FAILURE);
}
SWSS_LOG_NOTICE("Notify syncd INIT_VIEW");

}
5 changes: 4 additions & 1 deletion orchagent/saihelper.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma once

void initSaiApi();
#include <string>

void initSaiApi();
void initSaiRedis(const std::string &record_location);

0 comments on commit 17a6d61

Please sign in to comment.