diff --git a/orchagent/main.cpp b/orchagent/main.cpp index 6907c10ba3e6..d9a4264fcf0f 100644 --- a/orchagent/main.cpp +++ b/orchagent/main.cpp @@ -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; @@ -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; @@ -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 attrs; attr.id = SAI_SWITCH_ATTR_INIT_SWITCH; diff --git a/orchagent/saihelper.cpp b/orchagent/saihelper.cpp index cab990dc35fa..f6a0b3f526df 100644 --- a/orchagent/saihelper.cpp +++ b/orchagent/saihelper.cpp @@ -3,8 +3,13 @@ extern "C" { #include "saistatus.h" } +#include #include +#include + #include +#include + #include "saihelper.h" using namespace std; @@ -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 gProfileMap; const char *test_profile_get_value ( @@ -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"); + +} \ No newline at end of file diff --git a/orchagent/saihelper.h b/orchagent/saihelper.h index ead18c69ced6..8e733ef5584f 100644 --- a/orchagent/saihelper.h +++ b/orchagent/saihelper.h @@ -1,3 +1,6 @@ #pragma once -void initSaiApi(); \ No newline at end of file +#include + +void initSaiApi(); +void initSaiRedis(const std::string &record_location); \ No newline at end of file