-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2019 Broadcom Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <vector> | ||
#include "errorlistener.h" | ||
#include "common/json.h" | ||
#include "common/json.hpp" | ||
using json = nlohmann::json; | ||
|
||
namespace swss { | ||
|
||
/* Creates an error listener object that listens for h/w errors on table entries | ||
* in the APP_DB table that the caller is intereted in. */ | ||
|
||
ErrorListener::ErrorListener(std::string appTableName, int errFlags) | ||
{ | ||
SWSS_LOG_ENTER(); | ||
|
||
m_errorFlags = errFlags; | ||
m_errorChannelName = getErrorChannelName(appTableName); | ||
m_errorNotificationsDb = new DBConnector(ERROR_DB, DBConnector::DEFAULT_UNIXSOCKET, 0); | ||
m_errorNotificationConsumer = new swss::NotificationConsumer(m_errorNotificationsDb, | ||
m_errorChannelName); | ||
} | ||
|
||
ErrorListener::~ErrorListener() | ||
{ | ||
SWSS_LOG_ENTER(); | ||
delete m_errorNotificationConsumer; | ||
delete m_errorNotificationsDb; | ||
} | ||
|
||
/* Returns the Error notification corresponding to an entry in the APP_DB. | ||
* Owner of the error listener object calls this function whenever there is | ||
* a select event on the error listerner. | ||
* 0 is returned if a failure or postive ack of interest is read successfully. | ||
* -1 is returned if there are no errors of interest to the caller. */ | ||
int ErrorListener::getError(std::string &key, std::string &opCode, | ||
std::vector<swss::FieldValueTuple> &attrs) | ||
{ | ||
std::string op, data; | ||
std::vector<swss::FieldValueTuple> values; | ||
|
||
SWSS_LOG_ENTER(); | ||
|
||
m_errorNotificationConsumer->pop(op, data, values); | ||
SWSS_LOG_DEBUG("ErrorListener op: %s data: %s", op.c_str(), data.c_str()); | ||
|
||
json j = json::parse(data); | ||
|
||
// Filter the error notifications that the caller is not interested in. | ||
if (!(((m_errorFlags & ERR_NOTIFY_POSITIVE_ACK) && (j["rc"] == "SWSS_RC_SUCCESS")) || | ||
((m_errorFlags & ERR_NOTIFY_FAIL) && (j["rc"] != "SWSS_RC_SUCCESS")))) | ||
{ | ||
return -1; | ||
} | ||
|
||
key = j["key"]; | ||
opCode = j["operation"]; | ||
for (json::iterator it = j.begin(); it != j.end(); ++it) | ||
{ | ||
if(it.key() != "key" && it.key() != "operation") | ||
{ | ||
attrs.emplace_back(it.key(), it.value()); | ||
} | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2019 Broadcom Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef SWSS_ERRORLISTENER_H | ||
#define SWSS_ERRORLISTENER_H | ||
|
||
#include "notificationconsumer.h" | ||
#include "selectable.h" | ||
#include "table.h" | ||
#include <dbconnector.h> | ||
|
||
// Error notifications of interest to the error listener | ||
typedef enum | ||
{ | ||
ERR_NOTIFY_FAIL = 1, | ||
ERR_NOTIFY_POSITIVE_ACK | ||
} error_notify_flags_t; | ||
|
||
namespace swss { | ||
|
||
class ErrorListener : public Selectable | ||
{ | ||
public: | ||
ErrorListener(std::string appTableName, int errFlags); | ||
~ErrorListener(); | ||
|
||
static std::string getErrorChannelName(std::string& appTableName) | ||
{ | ||
std::string errorChnl = "ERROR_"; | ||
errorChnl += appTableName + "_CHANNEL"; | ||
|
||
return errorChnl; | ||
} | ||
|
||
int getFd() { return m_errorNotificationConsumer->getFd(); } | ||
uint64_t readData() { return m_errorNotificationConsumer->readData(); } | ||
bool hasData() { return m_errorNotificationConsumer->hasData(); } | ||
bool hasCachedData() override { return m_errorNotificationConsumer->hasCachedData(); } | ||
bool initializedWithData() override { return m_errorNotificationConsumer->initializedWithData(); } | ||
void updateAfterRead() override { m_errorNotificationConsumer->updateAfterRead(); } | ||
int getError(std::string &key, std::string &opCode, std::vector<swss::FieldValueTuple> &attrs); | ||
|
||
private: | ||
swss::NotificationConsumer *m_errorNotificationConsumer; | ||
DBConnector *m_errorNotificationsDb; | ||
int m_errorFlags; | ||
std::string m_errorChannelName; | ||
}; | ||
|
||
} | ||
#endif /* SWSS_ERRORLISTENER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2019 Broadcom Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "error.h" | ||
#include "logger.h" | ||
#include "errormap.h" | ||
|
||
namespace swss { | ||
|
||
ErrorMap::~ErrorMap() { } | ||
|
||
const ErrorMap::SwssStrToRCMap ErrorMap::m_swssStrToRC = { | ||
{ std::make_pair("SWSS_RC_SUCCESS", SWSS_RC_SUCCESS) }, | ||
{ std::make_pair("SWSS_RC_INVALID_PARAM", SWSS_RC_INVALID_PARAM) }, | ||
{ std::make_pair("SWSS_RC_UNAVAIL", SWSS_RC_UNAVAIL) }, | ||
{ std::make_pair("SWSS_RC_NOT_FOUND", SWSS_RC_NOT_FOUND) }, | ||
{ std::make_pair("SWSS_RC_NO_MEMORY", SWSS_RC_NO_MEMORY) }, | ||
{ std::make_pair("SWSS_RC_EXISTS", SWSS_RC_EXISTS) }, | ||
{ std::make_pair("SWSS_RC_TABLE_FULL", SWSS_RC_TABLE_FULL) }, | ||
{ std::make_pair("SWSS_RC_IN_USE", SWSS_RC_IN_USE) }, | ||
{ std::make_pair("SWSS_RC_NOT_IMPLEMENTED", SWSS_RC_NOT_IMPLEMENTED) }, | ||
{ std::make_pair("SWSS_RC_FAILURE", SWSS_RC_FAILURE) } | ||
}; | ||
|
||
const ErrorMap::SaiToSwssRCMap ErrorMap::m_saiToSwssRC = { | ||
{ "SAI_STATUS_SUCCESS", "SWSS_RC_SUCCESS" }, | ||
{ "SAI_STATUS_INVALID_PARAMETER", "SWSS_RC_INVALID_PARAM" }, | ||
{ "SAI_STATUS_NOT_SUPPORTED", "SWSS_RC_UNAVAIL" }, | ||
{ "SAI_STATUS_ITEM_NOT_FOUND", "SWSS_RC_NOT_FOUND" }, | ||
{ "SAI_STATUS_NO_MEMEORY", "SWSS_RC_NO_MEMORY" }, | ||
{ "SAI_STATUS_ITEM_ALREADY_EXISTS", "SWSS_RC_EXISTS" }, | ||
{ "SAI_STATUS_TABLE_FULL", "SWSS_RC_TABLE_FULL" }, | ||
{ "SAI_STATUS_OBJECT_IN_USE", "SWSS_RC_IN_USE" }, | ||
{ "SAI_STATUS_NOT_IMPLEMENTED", "SWSS_RC_NOT_IMPLEMENTED" }, | ||
{ "SAI_STATUS_FAILURE", "SWSS_RC_FAILURE" } | ||
}; | ||
|
||
ErrorMap &ErrorMap::getInstance() | ||
{ | ||
static ErrorMap m_errorMap; | ||
return m_errorMap; | ||
} | ||
|
||
std::string ErrorMap::getSwssRCStr(const std::string &saiRCStr) | ||
{ | ||
std::string swssRCStr; | ||
|
||
if (m_saiToSwssRC.find(saiRCStr) == m_saiToSwssRC.end()) | ||
{ | ||
SWSS_LOG_ERROR("Failed to map SAI error %s to SWSS error code", saiRCStr.c_str()); | ||
swssRCStr = "SWSS_RC_UNKNOWN"; | ||
} | ||
else | ||
{ | ||
swssRCStr = m_saiToSwssRC.at(saiRCStr); | ||
} | ||
return swssRCStr; | ||
} | ||
|
||
ErrorMap::SwssRC ErrorMap::getSwssRC(const std::string &swssRCStr) | ||
{ | ||
for (auto &elem : m_swssStrToRC) | ||
{ | ||
if (elem.first == swssRCStr) | ||
{ | ||
return elem.second; | ||
break; | ||
} | ||
} | ||
|
||
/* Error mapping is not found */ | ||
SWSS_LOG_ERROR("Invalid SWSS error string %s is received", swssRCStr.c_str()); | ||
return SWSS_RC_UNKNOWN; | ||
} | ||
|
||
std::string ErrorMap::getSaiRCStr(SwssRC rc) | ||
{ | ||
for (auto &elem : m_swssStrToRC) | ||
{ | ||
if (elem.second == rc) | ||
{ | ||
return elem.first; | ||
} | ||
} | ||
|
||
/* Error mapping is not found */ | ||
SWSS_LOG_ERROR("Invalid SWSS error code %d is received", rc); | ||
|
||
return "SWSS_RC_UNKNOWN"; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2019 Broadcom Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef SWSS_COMMON_ERRORMAP_H | ||
#define SWSS_COMMON_ERRORMAP_H | ||
|
||
#include <string> | ||
#include <map> | ||
#include <vector> | ||
|
||
namespace swss { | ||
|
||
class ErrorMap | ||
{ | ||
public: | ||
enum SwssRC | ||
{ | ||
SWSS_RC_SUCCESS, | ||
SWSS_RC_INVALID_PARAM, | ||
SWSS_RC_UNAVAIL, | ||
SWSS_RC_NOT_FOUND, | ||
SWSS_RC_NO_MEMORY, | ||
SWSS_RC_EXISTS, | ||
SWSS_RC_TABLE_FULL, | ||
SWSS_RC_IN_USE, | ||
SWSS_RC_NOT_IMPLEMENTED, | ||
SWSS_RC_FAILURE, | ||
SWSS_RC_UNKNOWN | ||
}; | ||
|
||
typedef std::map<std::string, std::string> SaiToSwssRCMap; | ||
typedef std::vector<std::pair<std::string, SwssRC>> SwssStrToRCMap; | ||
|
||
static const SwssStrToRCMap m_swssStrToRC; | ||
static const SaiToSwssRCMap m_saiToSwssRC; | ||
|
||
static ErrorMap &getInstance(); | ||
static std::string getSwssRCStr(const std::string &saiRCStr); | ||
static SwssRC getSwssRC(const std::string &swssRCStr); | ||
static std::string getSaiRCStr(SwssRC rc); | ||
|
||
private: | ||
ErrorMap() = default; | ||
~ErrorMap(); | ||
}; | ||
|
||
} | ||
|
||
#endif /* SWSS_COMMON_ERRORMAP_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters