Skip to content

Commit cb361e3

Browse files
Merge branch 'master' into feat_poe
2 parents 150e2fa + 45d7cb0 commit cb361e3

34 files changed

+1398
-35
lines changed

.azure-pipelines/test-docker-sonic-vs-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
7777
# run pytests in sets of 20
7878
all_tests=$(ls test_*.py)
79-
all_tests="${all_tests} p4rt"
79+
all_tests="${all_tests} p4rt dash"
8080
test_set=()
8181
for test in ${all_tests}; do
8282
test_set+=("${test}")

common/Makefile.am

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ common_libswsscommon_la_SOURCES = \
6868
common/zmqclient.cpp \
6969
common/zmqserver.cpp \
7070
common/asyncdbupdater.cpp \
71-
common/redis_table_waiter.cpp
71+
common/redis_table_waiter.cpp \
72+
common/c-api/util.cpp \
73+
common/c-api/dbconnector.cpp \
74+
common/c-api/consumerstatetable.cpp \
75+
common/c-api/producerstatetable.cpp \
76+
common/c-api/subscriberstatetable.cpp \
77+
common/c-api/zmqclient.cpp \
78+
common/c-api/zmqserver.cpp \
79+
common/c-api/zmqconsumerstatetable.cpp \
80+
common/c-api/zmqproducerstatetable.cpp
7281

7382
common_libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)
7483
common_libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS) $(CODE_COVERAGE_CPPFLAGS)

common/binaryserializer.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define __BINARY_SERIALIZER__
33

44
#include "common/armhelper.h"
5+
#include "common/rediscommand.h"
6+
#include "common/table.h"
57

68
#include <string>
79

@@ -11,6 +13,26 @@ namespace swss {
1113

1214
class BinarySerializer {
1315
public:
16+
static size_t serializedSize(const string &dbName, const string &tableName,
17+
const vector<KeyOpFieldsValuesTuple> &kcos) {
18+
size_t n = 0;
19+
n += dbName.size() + sizeof(size_t);
20+
n += tableName.size() + sizeof(size_t);
21+
22+
for (const KeyOpFieldsValuesTuple &kco : kcos) {
23+
const vector<FieldValueTuple> &fvs = kfvFieldsValues(kco);
24+
n += kfvKey(kco).size() + sizeof(size_t);
25+
n += to_string(fvs.size()).size() + sizeof(size_t);
26+
27+
for (const FieldValueTuple &fv : fvs) {
28+
n += fvField(fv).size() + sizeof(size_t);
29+
n += fvValue(fv).size() + sizeof(size_t);
30+
}
31+
}
32+
33+
return n + sizeof(size_t);
34+
}
35+
1436
static size_t serializeBuffer(
1537
const char* buffer,
1638
const size_t size,
@@ -192,8 +214,8 @@ class BinarySerializer {
192214
{
193215
if ((size_t)(m_current_position - m_buffer + datalen + sizeof(size_t)) > m_buffer_size)
194216
{
195-
SWSS_LOG_THROW("There are not enough buffer for binary serializer to serialize,\
196-
key count: %zu, data length %zu, buffer size: %zu",
217+
SWSS_LOG_THROW("There are not enough buffer for binary serializer to serialize,\n"
218+
" key count: %zu, data length %zu, buffer size: %zu",
197219
m_kvp_count,
198220
datalen,
199221
m_buffer_size);

common/c-api/consumerstatetable.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <cstdlib>
2+
#include <cstring>
3+
#include <deque>
4+
5+
#include "../consumerstatetable.h"
6+
#include "../dbconnector.h"
7+
#include "../table.h"
8+
#include "consumerstatetable.h"
9+
#include "util.h"
10+
11+
using namespace swss;
12+
using namespace std;
13+
14+
SWSSConsumerStateTable SWSSConsumerStateTable_new(SWSSDBConnector db, const char *tableName,
15+
const int32_t *p_popBatchSize,
16+
const int32_t *p_pri) {
17+
int popBatchSize = p_popBatchSize ? numeric_cast<int>(*p_popBatchSize)
18+
: TableConsumable::DEFAULT_POP_BATCH_SIZE;
19+
int pri = p_pri ? numeric_cast<int>(*p_pri) : 0;
20+
SWSSTry(return (SWSSConsumerStateTable) new ConsumerStateTable(
21+
(DBConnector *)db, string(tableName), popBatchSize, pri));
22+
}
23+
24+
void SWSSConsumerStateTable_free(SWSSConsumerStateTable tbl) {
25+
SWSSTry(delete (ConsumerStateTable *)tbl);
26+
}
27+
28+
SWSSKeyOpFieldValuesArray SWSSConsumerStateTable_pops(SWSSConsumerStateTable tbl) {
29+
SWSSTry({
30+
deque<KeyOpFieldsValuesTuple> vkco;
31+
((ConsumerStateTable *)tbl)->pops(vkco);
32+
return makeKeyOpFieldValuesArray(vkco);
33+
});
34+
}

common/c-api/consumerstatetable.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SWSS_COMMON_C_API_CONSUMERSTATETABLE_H
2+
#define SWSS_COMMON_C_API_CONSUMERSTATETABLE_H
3+
4+
#include "dbconnector.h"
5+
#include "util.h"
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
#include <stdint.h>
12+
13+
typedef struct SWSSConsumerStateTableOpaque *SWSSConsumerStateTable;
14+
15+
// Pass NULL for popBatchSize and/or pri to use the default values
16+
SWSSConsumerStateTable SWSSConsumerStateTable_new(SWSSDBConnector db, const char *tableName,
17+
const int32_t *popBatchSize, const int32_t *pri);
18+
19+
void SWSSConsumerStateTable_free(SWSSConsumerStateTable tbl);
20+
21+
// Result array and all of its members must be freed using free()
22+
SWSSKeyOpFieldValuesArray SWSSConsumerStateTable_pops(SWSSConsumerStateTable tbl);
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
27+
28+
#endif

common/c-api/dbconnector.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <cstring>
2+
#include <string>
3+
4+
#include "../dbconnector.h"
5+
#include "dbconnector.h"
6+
#include "util.h"
7+
8+
using namespace swss;
9+
using namespace std;
10+
11+
void SWSSSonicDBConfig_initialize(const char *path) {
12+
SWSSTry(SonicDBConfig::initialize(path));
13+
}
14+
15+
void SWSSSonicDBConfig_initializeGlobalConfig(const char *path) {
16+
SWSSTry(SonicDBConfig::initializeGlobalConfig(path));
17+
}
18+
19+
SWSSDBConnector SWSSDBConnector_new_tcp(int32_t dbId, const char *hostname, uint16_t port,
20+
uint32_t timeout) {
21+
SWSSTry(return (SWSSDBConnector) new DBConnector(dbId, string(hostname), port, timeout));
22+
}
23+
24+
SWSSDBConnector SWSSDBConnector_new_unix(int32_t dbId, const char *sock_path, uint32_t timeout) {
25+
SWSSTry(return (SWSSDBConnector) new DBConnector(dbId, string(sock_path), timeout));
26+
}
27+
28+
SWSSDBConnector SWSSDBConnector_new_named(const char *dbName, uint32_t timeout_ms, uint8_t isTcpConn) {
29+
SWSSTry(return (SWSSDBConnector) new DBConnector(string(dbName), timeout_ms, isTcpConn));
30+
}
31+
32+
void SWSSDBConnector_free(SWSSDBConnector db) {
33+
delete (DBConnector *)db;
34+
}
35+
36+
int8_t SWSSDBConnector_del(SWSSDBConnector db, const char *key) {
37+
SWSSTry(return ((DBConnector *)db)->del(string(key)) ? 1 : 0);
38+
}
39+
40+
void SWSSDBConnector_set(SWSSDBConnector db, const char *key, const char *value) {
41+
SWSSTry(((DBConnector *)db)->set(string(key), string(value)));
42+
}
43+
44+
char *SWSSDBConnector_get(SWSSDBConnector db, const char *key) {
45+
SWSSTry({
46+
shared_ptr<string> s = ((DBConnector *)db)->get(string(key));
47+
return s ? strdup(s->c_str()) : nullptr;
48+
});
49+
}
50+
51+
int8_t SWSSDBConnector_exists(SWSSDBConnector db, const char *key) {
52+
SWSSTry(return ((DBConnector *)db)->exists(string(key)) ? 1 : 0);
53+
}
54+
55+
int8_t SWSSDBConnector_hdel(SWSSDBConnector db, const char *key, const char *field) {
56+
SWSSTry(return ((DBConnector *)db)->hdel(string(key), string(field)) ? 1 : 0);
57+
}
58+
59+
void SWSSDBConnector_hset(SWSSDBConnector db, const char *key, const char *field,
60+
const char *value) {
61+
SWSSTry(((DBConnector *)db)->hset(string(key), string(field), string(value)));
62+
}
63+
64+
char *SWSSDBConnector_hget(SWSSDBConnector db, const char *key, const char *field) {
65+
SWSSTry({
66+
shared_ptr<string> s = ((DBConnector *)db)->hget(string(key), string(field));
67+
return s ? strdup(s->c_str()) : nullptr;
68+
});
69+
}
70+
71+
SWSSFieldValueArray SWSSDBConnector_hgetall(SWSSDBConnector db, const char *key) {
72+
SWSSTry({
73+
auto map = ((DBConnector *)db)->hgetall(key);
74+
return makeFieldValueArray(map);
75+
});
76+
}
77+
78+
int8_t SWSSDBConnector_hexists(SWSSDBConnector db, const char *key, const char *field) {
79+
SWSSTry(return ((DBConnector *)db)->hexists(string(key), string(field)) ? 1 : 0);
80+
}
81+
82+
int8_t SWSSDBConnector_flushdb(SWSSDBConnector db) {
83+
SWSSTry(return ((DBConnector *)db)->flushdb() ? 1 : 0);
84+
}

common/c-api/dbconnector.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#ifndef SWSS_COMMON_C_API_DBCONNECTOR_H
2+
#define SWSS_COMMON_C_API_DBCONNECTOR_H
3+
4+
#include "util.h"
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#include <stdint.h>
10+
11+
void SWSSSonicDBConfig_initialize(const char *path);
12+
13+
void SWSSSonicDBConfig_initializeGlobalConfig(const char *path);
14+
15+
typedef struct SWSSDBConnectorOpaque *SWSSDBConnector;
16+
17+
// Pass 0 to timeout for infinity
18+
SWSSDBConnector SWSSDBConnector_new_tcp(int32_t dbId, const char *hostname, uint16_t port,
19+
uint32_t timeout_ms);
20+
21+
// Pass 0 to timeout for infinity
22+
SWSSDBConnector SWSSDBConnector_new_unix(int32_t dbId, const char *sock_path, uint32_t timeout_ms);
23+
24+
// Pass 0 to timeout for infinity
25+
SWSSDBConnector SWSSDBConnector_new_named(const char *dbName, uint32_t timeout_ms, uint8_t isTcpConn);
26+
27+
void SWSSDBConnector_free(SWSSDBConnector db);
28+
29+
// Returns 0 when key doesn't exist, 1 when key was deleted
30+
int8_t SWSSDBConnector_del(SWSSDBConnector db, const char *key);
31+
32+
void SWSSDBConnector_set(SWSSDBConnector db, const char *key, const char *value);
33+
34+
// Returns NULL if key doesn't exist.
35+
// Result must be freed using free()
36+
char *SWSSDBConnector_get(SWSSDBConnector db, const char *key);
37+
38+
// Returns 0 for false, 1 for true
39+
int8_t SWSSDBConnector_exists(SWSSDBConnector db, const char *key);
40+
41+
// Returns 0 when key or field doesn't exist, 1 when field was deleted
42+
int8_t SWSSDBConnector_hdel(SWSSDBConnector db, const char *key, const char *field);
43+
44+
void SWSSDBConnector_hset(SWSSDBConnector db, const char *key, const char *field,
45+
const char *value);
46+
47+
// Returns NULL if key or field doesn't exist.
48+
// Result must be freed using free()
49+
char *SWSSDBConnector_hget(SWSSDBConnector db, const char *key, const char *field);
50+
51+
// Returns an empty map when the key doesn't exist.
52+
// Result array and all of its elements must be freed using free()
53+
SWSSFieldValueArray SWSSDBConnector_hgetall(SWSSDBConnector db, const char *key);
54+
55+
// Returns 0 when key or field doesn't exist, 1 when field exists
56+
int8_t SWSSDBConnector_hexists(SWSSDBConnector db, const char *key, const char *field);
57+
58+
// std::vector<std::string> keys(const std::string &key);
59+
60+
// std::pair<int, std::vector<std::string>> scan(int cursor = 0, const char
61+
// *match = "", uint32_t count = 10);
62+
63+
// template<typename InputIterator>
64+
// void hmset(const std::string &key, InputIterator start, InputIterator stop);
65+
66+
// void hmset(const std::unordered_map<std::string,
67+
// std::vector<std::pair<std::string, std::string>>>& multiHash);
68+
69+
// std::shared_ptr<std::string> get(const std::string &key);
70+
71+
// std::shared_ptr<std::string> hget(const std::string &key, const std::string
72+
// &field);
73+
74+
// int64_t incr(const std::string &key);
75+
76+
// int64_t decr(const std::string &key);
77+
78+
// int64_t rpush(const std::string &list, const std::string &item);
79+
80+
// std::shared_ptr<std::string> blpop(const std::string &list, int timeout);
81+
82+
// void subscribe(const std::string &pattern);
83+
84+
// void psubscribe(const std::string &pattern);
85+
86+
// void punsubscribe(const std::string &pattern);
87+
88+
// int64_t publish(const std::string &channel, const std::string &message);
89+
90+
// void config_set(const std::string &key, const std::string &value);
91+
92+
// Returns 1 on success, 0 on failure
93+
int8_t SWSSDBConnector_flushdb(SWSSDBConnector db);
94+
95+
// std::map<std::string, std::map<std::string, std::map<std::string,
96+
// std::string>>> getall();
97+
#ifdef __cplusplus
98+
}
99+
#endif
100+
101+
#endif

common/c-api/producerstatetable.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <cstring>
2+
#include <string>
3+
4+
#include "../dbconnector.h"
5+
#include "../producerstatetable.h"
6+
#include "dbconnector.h"
7+
#include "producerstatetable.h"
8+
#include "util.h"
9+
10+
using namespace swss;
11+
using namespace std;
12+
13+
SWSSProducerStateTable SWSSProducerStateTable_new(SWSSDBConnector db, const char *tableName) {
14+
SWSSTry(return (SWSSProducerStateTable) new ProducerStateTable((DBConnector *)db,
15+
string(tableName)));
16+
}
17+
18+
void SWSSProducerStateTable_free(SWSSProducerStateTable tbl) {
19+
SWSSTry(delete ((ProducerStateTable *)tbl));
20+
}
21+
22+
void SWSSProducerStateTable_setBuffered(SWSSProducerStateTable tbl, uint8_t buffered) {
23+
SWSSTry(((ProducerStateTable *)tbl)->setBuffered((bool)buffered))
24+
}
25+
26+
void SWSSProducerStateTable_set(SWSSProducerStateTable tbl, const char *key,
27+
SWSSFieldValueArray values) {
28+
SWSSTry(((ProducerStateTable *)tbl)->set(string(key), takeFieldValueArray(values)));
29+
}
30+
31+
void SWSSProducerStateTable_del(SWSSProducerStateTable tbl, const char *key) {
32+
SWSSTry(((ProducerStateTable *)tbl)->del(string(key)));
33+
}
34+
35+
void SWSSProducerStateTable_flush(SWSSProducerStateTable tbl) {
36+
SWSSTry(((ProducerStateTable *)tbl)->flush());
37+
}
38+
39+
int64_t SWSSProducerStateTable_count(SWSSProducerStateTable tbl) {
40+
SWSSTry(return ((ProducerStateTable *)tbl)->count());
41+
}
42+
43+
void SWSSProducerStateTable_clear(SWSSProducerStateTable tbl) {
44+
SWSSTry(((ProducerStateTable *)tbl)->clear());
45+
}
46+
47+
void SWSSProducerStateTable_create_temp_view(SWSSProducerStateTable tbl) {
48+
SWSSTry(((ProducerStateTable *)tbl)->create_temp_view());
49+
}
50+
51+
void SWSSProducerStateTable_apply_temp_view(SWSSProducerStateTable tbl) {
52+
SWSSTry(((ProducerStateTable *)tbl)->apply_temp_view());
53+
}

0 commit comments

Comments
 (0)