Skip to content

Commit d07b893

Browse files
committed
Error Handling Framework changes
1 parent aaa8133 commit d07b893

7 files changed

Lines changed: 325 additions & 2 deletions

File tree

common/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ libswsscommon_la_SOURCES = \
5252
exec.cpp \
5353
subscriberstatetable.cpp \
5454
timestamp.cpp \
55-
warm_restart.cpp
55+
warm_restart.cpp \
56+
errormap.cpp \
57+
errorlistener.cpp
5658

5759
libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS)
5860
libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS)

common/errorlistener.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2019 Broadcom Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <vector>
18+
#include "errorlistener.h"
19+
#include "common/json.h"
20+
#include "common/json.hpp"
21+
using json = nlohmann::json;
22+
23+
namespace swss {
24+
25+
/* Creates an error listener object that listens for h/w errors on table entries
26+
* in the APP_DB table that the caller is intereted in. */
27+
28+
ErrorListener::ErrorListener(std::string appTableName, int errFlags)
29+
{
30+
SWSS_LOG_ENTER();
31+
32+
m_errorFlags = errFlags;
33+
m_errorChannelName = getErrorChannelName(appTableName);
34+
m_errorNotificationsDb = new DBConnector(ERROR_DB, DBConnector::DEFAULT_UNIXSOCKET, 0);
35+
m_errorNotificationConsumer = new swss::NotificationConsumer(m_errorNotificationsDb,
36+
m_errorChannelName);
37+
}
38+
39+
ErrorListener::~ErrorListener()
40+
{
41+
SWSS_LOG_ENTER();
42+
delete m_errorNotificationConsumer;
43+
delete m_errorNotificationsDb;
44+
}
45+
46+
/* Returns the Error notification corresponding to an entry in the APP_DB.
47+
* Owner of the error listener object calls this function whenever there is
48+
* a select event on the error listerner.
49+
* 0 is returned if a failure or postive ack of interest is read successfully.
50+
* -1 is returned if there are no errors of interest to the caller. */
51+
int ErrorListener::getError(std::string &key, std::string &opCode,
52+
std::vector<swss::FieldValueTuple> &attrs)
53+
{
54+
std::string op, data;
55+
std::vector<swss::FieldValueTuple> values;
56+
57+
SWSS_LOG_ENTER();
58+
59+
m_errorNotificationConsumer->pop(op, data, values);
60+
SWSS_LOG_DEBUG("ErrorListener op: %s data: %s", op.c_str(), data.c_str());
61+
62+
json j = json::parse(data);
63+
64+
// Filter the error notifications that the caller is not interested in.
65+
if (!(((m_errorFlags & ERR_NOTIFY_POSITIVE_ACK) && (j["rc"] == "SWSS_RC_SUCCESS")) ||
66+
((m_errorFlags & ERR_NOTIFY_FAIL) && (j["rc"] != "SWSS_RC_SUCCESS"))))
67+
{
68+
return -1;
69+
}
70+
71+
key = j["key"];
72+
opCode = j["operation"];
73+
for (json::iterator it = j.begin(); it != j.end(); ++it)
74+
{
75+
if(it.key() != "key" && it.key() != "operation")
76+
{
77+
attrs.emplace_back(it.key(), it.value());
78+
}
79+
}
80+
return 0;
81+
}
82+
}

common/errorlistener.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2019 Broadcom Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SWSS_ERRORLISTENER_H
18+
#define SWSS_ERRORLISTENER_H
19+
20+
#include "notificationconsumer.h"
21+
#include "selectable.h"
22+
#include "table.h"
23+
#include <dbconnector.h>
24+
25+
// Error notifications of interest to the error listener
26+
typedef enum
27+
{
28+
ERR_NOTIFY_FAIL = 1,
29+
ERR_NOTIFY_POSITIVE_ACK
30+
} error_notify_flags_t;
31+
32+
namespace swss {
33+
34+
class ErrorListener : public Selectable
35+
{
36+
public:
37+
ErrorListener(std::string appTableName, int errFlags);
38+
~ErrorListener();
39+
40+
static std::string getErrorChannelName(std::string& appTableName)
41+
{
42+
std::string errorChnl = "ERROR_";
43+
errorChnl += appTableName + "_CHANNEL";
44+
45+
return errorChnl;
46+
}
47+
48+
int getFd() { return m_errorNotificationConsumer->getFd(); }
49+
uint64_t readData() { return m_errorNotificationConsumer->readData(); }
50+
bool hasData() { return m_errorNotificationConsumer->hasData(); }
51+
bool hasCachedData() override { return m_errorNotificationConsumer->hasCachedData(); }
52+
bool initializedWithData() override { return m_errorNotificationConsumer->initializedWithData(); }
53+
void updateAfterRead() override { m_errorNotificationConsumer->updateAfterRead(); }
54+
int getError(std::string &key, std::string &opCode, std::vector<swss::FieldValueTuple> &attrs);
55+
56+
private:
57+
swss::NotificationConsumer *m_errorNotificationConsumer;
58+
DBConnector *m_errorNotificationsDb;
59+
int m_errorFlags;
60+
std::string m_errorChannelName;
61+
};
62+
63+
}
64+
#endif /* SWSS_ERRORLISTENER_H */

common/errormap.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2019 Broadcom Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "error.h"
18+
#include "logger.h"
19+
#include "errormap.h"
20+
21+
namespace swss {
22+
23+
ErrorMap::~ErrorMap() { }
24+
25+
const ErrorMap::SwssStrToRCMap ErrorMap::m_swssStrToRC = {
26+
{ std::make_pair("SWSS_RC_SUCCESS", SWSS_RC_SUCCESS) },
27+
{ std::make_pair("SWSS_RC_INVALID_PARAM", SWSS_RC_INVALID_PARAM) },
28+
{ std::make_pair("SWSS_RC_UNAVAIL", SWSS_RC_UNAVAIL) },
29+
{ std::make_pair("SWSS_RC_NOT_FOUND", SWSS_RC_NOT_FOUND) },
30+
{ std::make_pair("SWSS_RC_NO_MEMORY", SWSS_RC_NO_MEMORY) },
31+
{ std::make_pair("SWSS_RC_EXISTS", SWSS_RC_EXISTS) },
32+
{ std::make_pair("SWSS_RC_TABLE_FULL", SWSS_RC_TABLE_FULL) },
33+
{ std::make_pair("SWSS_RC_IN_USE", SWSS_RC_IN_USE) },
34+
{ std::make_pair("SWSS_RC_NOT_IMPLEMENTED", SWSS_RC_NOT_IMPLEMENTED) },
35+
{ std::make_pair("SWSS_RC_FAILURE", SWSS_RC_FAILURE) }
36+
};
37+
38+
const ErrorMap::SaiToSwssRCMap ErrorMap::m_saiToSwssRC = {
39+
{ "SAI_STATUS_SUCCESS", "SWSS_RC_SUCCESS" },
40+
{ "SAI_STATUS_INVALID_PARAMETER", "SWSS_RC_INVALID_PARAM" },
41+
{ "SAI_STATUS_NOT_SUPPORTED", "SWSS_RC_UNAVAIL" },
42+
{ "SAI_STATUS_ITEM_NOT_FOUND", "SWSS_RC_NOT_FOUND" },
43+
{ "SAI_STATUS_NO_MEMEORY", "SWSS_RC_NO_MEMORY" },
44+
{ "SAI_STATUS_ITEM_ALREADY_EXISTS", "SWSS_RC_EXISTS" },
45+
{ "SAI_STATUS_TABLE_FULL", "SWSS_RC_TABLE_FULL" },
46+
{ "SAI_STATUS_OBJECT_IN_USE", "SWSS_RC_IN_USE" },
47+
{ "SAI_STATUS_NOT_IMPLEMENTED", "SWSS_RC_NOT_IMPLEMENTED" },
48+
{ "SAI_STATUS_FAILURE", "SWSS_RC_FAILURE" }
49+
};
50+
51+
ErrorMap &ErrorMap::getInstance()
52+
{
53+
static ErrorMap m_errorMap;
54+
return m_errorMap;
55+
}
56+
57+
std::string ErrorMap::getSwssRCStr(const std::string &saiRCStr)
58+
{
59+
std::string swssRCStr;
60+
61+
if (m_saiToSwssRC.find(saiRCStr) == m_saiToSwssRC.end())
62+
{
63+
SWSS_LOG_ERROR("Failed to map SAI error %s to SWSS error code", saiRCStr.c_str());
64+
swssRCStr = "SWSS_RC_UNKNOWN";
65+
}
66+
else
67+
{
68+
swssRCStr = m_saiToSwssRC.at(saiRCStr);
69+
}
70+
return swssRCStr;
71+
}
72+
73+
ErrorMap::SwssRC ErrorMap::getSwssRC(const std::string &swssRCStr)
74+
{
75+
for (auto &elem : m_swssStrToRC)
76+
{
77+
if (elem.first == swssRCStr)
78+
{
79+
return elem.second;
80+
break;
81+
}
82+
}
83+
84+
/* Error mapping is not found */
85+
SWSS_LOG_ERROR("Invalid SWSS error string %s is received", swssRCStr.c_str());
86+
return SWSS_RC_UNKNOWN;
87+
}
88+
89+
std::string ErrorMap::getSaiRCStr(SwssRC rc)
90+
{
91+
for (auto &elem : m_swssStrToRC)
92+
{
93+
if (elem.second == rc)
94+
{
95+
return elem.first;
96+
}
97+
}
98+
99+
/* Error mapping is not found */
100+
SWSS_LOG_ERROR("Invalid SWSS error code %d is received", rc);
101+
102+
return "SWSS_RC_UNKNOWN";
103+
}
104+
105+
};

common/errormap.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 Broadcom Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SWSS_COMMON_ERRORMAP_H
18+
#define SWSS_COMMON_ERRORMAP_H
19+
20+
#include <string>
21+
#include <map>
22+
#include <vector>
23+
24+
namespace swss {
25+
26+
class ErrorMap
27+
{
28+
public:
29+
enum SwssRC
30+
{
31+
SWSS_RC_SUCCESS,
32+
SWSS_RC_INVALID_PARAM,
33+
SWSS_RC_UNAVAIL,
34+
SWSS_RC_NOT_FOUND,
35+
SWSS_RC_NO_MEMORY,
36+
SWSS_RC_EXISTS,
37+
SWSS_RC_TABLE_FULL,
38+
SWSS_RC_IN_USE,
39+
SWSS_RC_NOT_IMPLEMENTED,
40+
SWSS_RC_FAILURE,
41+
SWSS_RC_UNKNOWN
42+
};
43+
44+
typedef std::map<std::string, std::string> SaiToSwssRCMap;
45+
typedef std::vector<std::pair<std::string, SwssRC>> SwssStrToRCMap;
46+
47+
static const SwssStrToRCMap m_swssStrToRC;
48+
static const SaiToSwssRCMap m_saiToSwssRC;
49+
50+
static ErrorMap &getInstance();
51+
static std::string getSwssRCStr(const std::string &saiRCStr);
52+
static SwssRC getSwssRC(const std::string &swssRCStr);
53+
static std::string getSaiRCStr(SwssRC rc);
54+
55+
private:
56+
ErrorMap() = default;
57+
~ErrorMap();
58+
};
59+
60+
}
61+
62+
#endif /* SWSS_COMMON_ERRORMAP_H */

common/schema.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace swss {
1414
#define FLEX_COUNTER_DB 5
1515
#define STATE_DB 6
1616
#define SNMP_OVERLAY_DB 7
17+
#define ERROR_DB 8
1718

1819
/***** APPLICATION DATABASE *****/
1920

@@ -200,6 +201,8 @@ namespace swss {
200201
#define CFG_DEBUG_COUNTER_TABLE_NAME "DEBUG_COUNTER"
201202
#define CFG_DEBUG_COUNTER_DROP_REASON_TABLE_NAME "DEBUG_COUNTER_DROP_REASON"
202203

204+
#define CFG_BGP_ERROR_TABLE_NAME "BGP_ERROR_CFG_TABLE"
205+
203206
/***** STATE DATABASE *****/
204207

205208
#define STATE_SWITCH_CAPABILITY_TABLE_NAME "SWITCH_CAPABILITY_TABLE"
@@ -239,6 +242,10 @@ namespace swss {
239242
#define CFG_DTEL_QUEUE_REPORT_TABLE_NAME "DTEL_QUEUE_REPORT"
240243
#define CFG_DTEL_EVENT_TABLE_NAME "DTEL_EVENT"
241244

245+
/***** ERROR DATABASE *****/
246+
#define ERROR_NEIGH_TABLE_NAME "ERROR_NEIGH_TABLE"
247+
#define ERROR_ROUTE_TABLE_NAME "ERROR_ROUTE_TABLE"
248+
242249
}
243250

244251
#endif

common/table.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const TableNameSeparatorMap TableBase::tableNameSeparatorMap = {
2626
{ CONFIG_DB, TABLE_NAME_SEPARATOR_VBAR },
2727
{ PFC_WD_DB, TABLE_NAME_SEPARATOR_COLON },
2828
{ FLEX_COUNTER_DB, TABLE_NAME_SEPARATOR_COLON },
29-
{ STATE_DB, TABLE_NAME_SEPARATOR_VBAR }
29+
{ STATE_DB, TABLE_NAME_SEPARATOR_VBAR },
30+
{ ERROR_DB, TABLE_NAME_SEPARATOR_COLON }
3031
};
3132

3233
Table::Table(const DBConnector *db, const string &tableName)

0 commit comments

Comments
 (0)