Skip to content

Commit 2ddebf6

Browse files
committed
Add asic compile/switch skeleton in redis/syncd (#9)
* Prevent copy and assignment for SaiAttributeList * Make sairedis threadsafe * Add asic compile/switch skeleton in redis/syncd * Add SaiAttribute class * Fix producer and consumer calls * Add more serialization types
1 parent 0aaefff commit 2ddebf6

13 files changed

+356
-25
lines changed

common/saiattribute.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "saiattribute.h"
2+
3+
#include "swss/logger.h"
4+
#include "saiserialize.h"
5+
6+
#include "string.h"
7+
extern "C" {
8+
#include "sai.h"
9+
}
10+
11+
SaiAttribute::SaiAttribute(
12+
_In_ const sai_object_type_t objectType,
13+
_In_ const swss::FieldValueTuple &value,
14+
_In_ bool onlyCount) :
15+
m_onlyCount(onlyCount),
16+
m_objectType(objectType)
17+
{
18+
// TODO save those to make possible for copy constructor and assignment
19+
const std::string &strAttrId = fvField(value);
20+
const std::string &strAttrValue = fvValue(value);
21+
22+
if (strAttrId == "NULL")
23+
{
24+
SWSS_LOG_ERROR("NULL attribute passed");
25+
26+
exit(EXIT_FAILURE);
27+
}
28+
29+
memset(&m_attr, 0, sizeof(sai_attribute_t));
30+
31+
int index = 0;
32+
sai_deserialize_primitive(strAttrId, index, m_attr.id);
33+
34+
sai_status_t status = sai_get_serialization_type(m_objectType, m_attr.id, m_serializationType);
35+
36+
if (status != SAI_STATUS_SUCCESS)
37+
{
38+
SWSS_LOG_ERROR("failed to get serialization type for object type %lx, attribute id: %lx",
39+
m_objectType,
40+
m_attr.id);
41+
42+
exit(EXIT_FAILURE);
43+
}
44+
45+
index = 0;
46+
status = sai_deserialize_attr_value(strAttrValue, index, m_serializationType, m_attr, m_onlyCount);
47+
48+
if (status != SAI_STATUS_SUCCESS)
49+
{
50+
SWSS_LOG_ERROR("failed to deserialize attribute value: %s, serialization type: %d",
51+
strAttrValue.c_str(),
52+
m_serializationType);
53+
54+
exit(EXIT_FAILURE);
55+
}
56+
}
57+
58+
SaiAttribute::~SaiAttribute()
59+
{
60+
sai_status_t status = sai_deserialize_free_attribute_value(m_serializationType, m_attr);
61+
62+
if (status != SAI_STATUS_SUCCESS)
63+
{
64+
SWSS_LOG_ERROR("failed to deserialize free");
65+
66+
exit(EXIT_FAILURE);
67+
}
68+
}
69+
70+
sai_attribute_t* SaiAttribute::getAttr()
71+
{
72+
// reference to member, we may need to
73+
// replace VID to RID or vice versa
74+
return &m_attr;
75+
}

common/saiattribute.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef __SAI_ATTRIBUTE__
2+
#define __SAI_ATTRIBUTE__
3+
4+
#include <string>
5+
#include <vector>
6+
7+
#include <hiredis/hiredis.h>
8+
#include "swss/dbconnector.h"
9+
#include "swss/table.h"
10+
#include "swss/logger.h"
11+
#include "sai.h"
12+
#include "saiserialize.h"
13+
#include "string.h"
14+
15+
class SaiAttribute
16+
{
17+
public:
18+
19+
SaiAttribute(
20+
_In_ const sai_object_type_t objectType,
21+
_In_ const swss::FieldValueTuple &value,
22+
_In_ bool onlyCount);
23+
24+
~SaiAttribute();
25+
26+
sai_attribute_t* getAttr();
27+
28+
private:
29+
30+
SaiAttribute(const SaiAttribute&);
31+
SaiAttribute& operator=(const SaiAttribute&);
32+
33+
bool m_onlyCount;
34+
35+
sai_object_type_t m_objectType;
36+
sai_attribute_t m_attr;
37+
38+
sai_attr_serialization_type_t m_serializationType;
39+
};
40+
41+
#endif // __SAI_ATTRIBUTE__
42+

common/saiattributelist.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class SaiAttributeList
3535

3636
private:
3737

38+
SaiAttributeList(const SaiAttributeList&);
39+
SaiAttributeList& operator=(const SaiAttributeList&);
40+
3841
std::vector<sai_attribute_t> m_attr_list;
3942
std::vector<sai_attr_serialization_type_t> m_serialization_type_list;
4043
};

common/saiserialize.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ sai_serialization_map_t sai_get_serialization_map()
5959
map[SAI_OBJECT_TYPE_VLAN_MEMBER][SAI_VLAN_MEMBER_ATTR_PORT_ID] = SAI_SERIALIZATION_TYPE_OBJECT_ID;
6060

6161
map[SAI_OBJECT_TYPE_TRAP][SAI_HOSTIF_TRAP_ATTR_PACKET_ACTION] = SAI_SERIALIZATION_TYPE_INT32;
62+
map[SAI_OBJECT_TYPE_TRAP][SAI_HOSTIF_TRAP_ATTR_TRAP_CHANNEL] = SAI_SERIALIZATION_TYPE_INT32;
63+
map[SAI_OBJECT_TYPE_TRAP][SAI_HOSTIF_TRAP_ATTR_TRAP_PRIORITY] = SAI_SERIALIZATION_TYPE_UINT32;
6264

6365
return map;
6466
}
@@ -146,7 +148,7 @@ sai_status_t sai_get_serialization_type(
146148

147149
if (mit == map.end())
148150
{
149-
SWSS_LOG_ERROR("serialization attribute id not found %u", attr_id);
151+
SWSS_LOG_ERROR("serialization attribute id not found %u for object type : %u", attr_id, object_type);
150152

151153
return SAI_STATUS_NOT_IMPLEMENTED;
152154
}

lib/inc/sai_redis.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef __SAI_REDIS__
22
#define __SAI_REDIS__
33

4+
#include <mutex>
5+
46
#include "stdint.h"
57
#include "stdio.h"
68

@@ -23,15 +25,19 @@ extern service_method_table_t g_services;
2325
extern swss::DBConnector *g_db;
2426
extern swss::ProducerTable *g_asicState;
2527

28+
extern swss::ProducerTable *g_notifySyncdProducer;
2629
extern swss::ProducerTable *g_redisGetProducer;
2730
extern swss::ConsumerTable *g_redisGetConsumer;
2831
extern swss::ConsumerTable *g_redisNotifications;
32+
extern swss::ConsumerTable *g_notifySyncdConsumer;
2933

3034
extern swss::Table *g_vidToRid;
3135
extern swss::Table *g_ridToVid;
3236

3337
extern swss::RedisClient *g_redisClient;
3438

39+
extern std::mutex g_mutex;
40+
3541
extern const sai_acl_api_t redis_acl_api;
3642
extern const sai_buffer_api_t redis_buffer_api;
3743
extern const sai_fdb_api_t redis_fdb_api;

lib/src/sai_redis_generic_create.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ sai_status_t internal_redis_generic_create(
4545
_In_ uint32_t attr_count,
4646
_In_ const sai_attribute_t *attr_list)
4747
{
48+
std::lock_guard<std::mutex> lock(g_mutex);
49+
4850
SWSS_LOG_ENTER();
4951

5052
std::vector<swss::FieldValueTuple> entry = SaiAttributeList::serialize_attr_list(

lib/src/sai_redis_generic_get.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ sai_status_t internal_redis_generic_get(
5757
_In_ uint32_t attr_count,
5858
_Out_ sai_attribute_t *attr_list)
5959
{
60+
std::lock_guard<std::mutex> lock(g_mutex);
61+
6062
SWSS_LOG_ENTER();
6163

6264
std::vector<swss::FieldValueTuple> entry = SaiAttributeList::serialize_attr_list(
@@ -101,7 +103,7 @@ sai_status_t internal_redis_generic_get(
101103
const std::string &op = kfvOp(kco);
102104
const std::string &key = kfvKey(kco);
103105

104-
SWSS_LOG_DEBUG("response: %s op = %s, key = %s", key.c_str(), op.c_str());
106+
SWSS_LOG_DEBUG("response: op = %s, key = %s", key.c_str(), op.c_str());
105107

106108
if (op != "getresponse") // ignore non response messages
107109
continue;

lib/src/sai_redis_generic_remove.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ sai_status_t internal_redis_generic_remove(
44
_In_ sai_object_type_t object_type,
55
_In_ const std::string &serialized_object_id)
66
{
7+
std::lock_guard<std::mutex> lock(g_mutex);
8+
79
SWSS_LOG_ENTER();
810

911
std::string str_object_type;

lib/src/sai_redis_generic_set.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ sai_status_t internal_redis_generic_set(
1818
_In_ const std::string &serialized_object_id,
1919
_In_ const sai_attribute_t *attr)
2020
{
21+
std::lock_guard<std::mutex> lock(g_mutex);
22+
2123
SWSS_LOG_ENTER();
2224

2325
std::vector<swss::FieldValueTuple> entry = SaiAttributeList::serialize_attr_list(

lib/src/sai_redis_interfacequery.cpp

+46-14
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,43 @@
22

33
#include <string.h>
44

5+
std::mutex g_mutex;
6+
57
service_method_table_t g_services;
6-
bool g_initialized = false;
8+
bool g_apiInitialized = false;
79

810
swss::DBConnector *g_db = NULL;
911
swss::DBConnector *g_dbNtf = NULL;
1012
swss::ProducerTable *g_asicState = NULL;
1113

1214
// we probably don't need those to tables to access GET requests
15+
swss::ProducerTable *g_notifySyncdProducer = NULL;
1316
swss::ProducerTable *g_redisGetProducer = NULL;
1417
swss::ConsumerTable *g_redisGetConsumer = NULL;
1518
swss::ConsumerTable *g_redisNotifications = NULL;
19+
swss::ConsumerTable *g_notifySyncdConsumer = NULL;
1620

1721
swss::RedisClient *g_redisClient = NULL;
1822

1923
sai_status_t sai_api_initialize(
2024
_In_ uint64_t flags,
2125
_In_ const service_method_table_t* services)
2226
{
27+
std::lock_guard<std::mutex> lock(g_mutex);
28+
2329
SWSS_LOG_ENTER();
2430

2531
if ((NULL == services) || (NULL == services->profile_get_next_value) || (NULL == services->profile_get_value))
2632
{
27-
SWSS_LOG_ERROR("Invalid services handle passed to SAI API initialize\n");
33+
SWSS_LOG_ERROR("Invalid services handle passed to SAI API initialize");
2834
return SAI_STATUS_INVALID_PARAMETER;
2935
}
3036

3137
memcpy(&g_services, services, sizeof(g_services));
3238

3339
if (0 != flags)
3440
{
35-
SWSS_LOG_ERROR("Invalid flags passed to SAI API initialize\n");
41+
SWSS_LOG_ERROR("Invalid flags passed to SAI API initialize");
3642
return SAI_STATUS_INVALID_PARAMETER;
3743
}
3844

@@ -51,11 +57,21 @@ sai_status_t sai_api_initialize(
5157

5258
g_asicState = new swss::ProducerTable(g_db, "ASIC_STATE");
5359

60+
if (g_notifySyncdProducer != NULL)
61+
delete g_notifySyncdProducer;
62+
63+
g_notifySyncdProducer = new swss::ProducerTable(g_db, "NOTIFYSYNCDREQUERY");
64+
5465
if (g_redisGetProducer != NULL)
5566
delete g_redisGetProducer;
5667

5768
g_redisGetProducer = new swss::ProducerTable(g_db, "GETREQUEST");
5869

70+
if (g_notifySyncdConsumer != NULL)
71+
delete g_notifySyncdConsumer;
72+
73+
g_notifySyncdConsumer = new swss::ConsumerTable(g_db, "NOTIFYSYNCRESPONSE");
74+
5975
if (g_redisGetConsumer != NULL)
6076
delete g_redisGetConsumer;
6177

@@ -71,15 +87,17 @@ sai_status_t sai_api_initialize(
7187

7288
g_redisClient = new swss::RedisClient(g_db);
7389

74-
g_initialized = true;
90+
g_apiInitialized = true;
7591

7692
return SAI_STATUS_SUCCESS;
7793
}
7894

7995
sai_status_t sai_log_set(
80-
_In_ sai_api_t sai_api_id,
96+
_In_ sai_api_t sai_api_id,
8197
_In_ sai_log_level_t log_level)
8298
{
99+
std::lock_guard<std::mutex> lock(g_mutex);
100+
83101
SWSS_LOG_ENTER();
84102

85103
switch (log_level)
@@ -103,11 +121,11 @@ sai_status_t sai_log_set(
103121
break;
104122

105123
default:
106-
SWSS_LOG_ERROR("Invalid log level %d\n", log_level);
124+
SWSS_LOG_ERROR("Invalid log level %d", log_level);
107125
return SAI_STATUS_INVALID_PARAMETER;
108126
}
109127

110-
switch (sai_api_id)
128+
switch (sai_api_id)
111129
{
112130
case SAI_API_SWITCH:
113131
break;
@@ -158,28 +176,30 @@ sai_status_t sai_log_set(
158176
break;
159177

160178
default:
161-
SWSS_LOG_ERROR("Invalid API type %d\n", sai_api_id);
179+
SWSS_LOG_ERROR("Invalid API type %d", sai_api_id);
162180
return SAI_STATUS_INVALID_PARAMETER;
163181
}
164182

165183
return SAI_STATUS_SUCCESS;
166184
}
167185

168186
sai_status_t sai_api_query(
169-
_In_ sai_api_t sai_api_id,
187+
_In_ sai_api_t sai_api_id,
170188
_Out_ void** api_method_table)
171189
{
190+
std::lock_guard<std::mutex> lock(g_mutex);
191+
172192
SWSS_LOG_ENTER();
173193

174-
if (NULL == api_method_table)
194+
if (NULL == api_method_table)
175195
{
176-
SWSS_LOG_ERROR("NULL method table passed to SAI API initialize\n");
196+
SWSS_LOG_ERROR("NULL method table passed to SAI API initialize");
177197
return SAI_STATUS_INVALID_PARAMETER;
178198
}
179199

180-
if (!g_initialized)
200+
if (!g_apiInitialized)
181201
{
182-
SWSS_LOG_ERROR("SAI API not initialized before calling API query\n");
202+
SWSS_LOG_ERROR("SAI API not initialized before calling API query");
183203
return SAI_STATUS_UNINITIALIZED;
184204
}
185205

@@ -285,8 +305,20 @@ sai_status_t sai_api_query(
285305
return SAI_STATUS_SUCCESS;
286306

287307
default:
288-
SWSS_LOG_ERROR("Invalid API type %d\n", sai_api_id);
308+
SWSS_LOG_ERROR("Invalid API type %d", sai_api_id);
289309
return SAI_STATUS_INVALID_PARAMETER;
290310
}
291311
}
292312

313+
sai_status_t sai_api_uninitialize(void)
314+
{
315+
std::lock_guard<std::mutex> lock(g_mutex);
316+
317+
SWSS_LOG_ENTER();
318+
319+
g_apiInitialized = false;
320+
321+
SWSS_LOG_ERROR("not implemented");
322+
323+
return SAI_STATUS_NOT_IMPLEMENTED;
324+
}

0 commit comments

Comments
 (0)