Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d72b471
Update build status link
qiluo-msft Dec 23, 2017
cb48511
[MacAddress]: Optimize to_string() and parseMacString methods (#171)
pavel-shirshov Jan 4, 2018
617381c
Remove code from stackoverflow. (#172)
pavel-shirshov Jan 5, 2018
aa55f69
Add a name for VRF table (#173)
pavel-shirshov Jan 8, 2018
5178014
Add table names for buffer config in ConfigDB (#169)
andriymoroz-mlnx Jan 10, 2018
6f7db0b
export notification{producer,consumer} py bindings (#174)
lguohan Jan 10, 2018
d630d57
Add RedisClient to pyext (#176)
kcudnik Jan 15, 2018
8908bf9
Revert "Add RedisClient to pyext (#176)" (#177)
lguohan Jan 16, 2018
04b8b84
[schema]: add define of PFC_WD_POLL_MSECS (#179)
sihuihan88 Jan 17, 2018
e54baca
Add fdb flush support in lua script (#180)
kcudnik Jan 23, 2018
453c5f9
[crm]: Add CRM tables (#178)
Jan 23, 2018
9b00920
Table class supports redis pipeline (#181)
qiluo-msft Jan 26, 2018
4b29d77
[schema]: update flex counter schema (#183)
sihuihan88 Jan 30, 2018
db7498a
update build instruction (#185)
lguohan Feb 1, 2018
2bbb6c9
Extending IpPrefix class to return the subnet associated to any given…
rodnymolina Feb 8, 2018
53df32e
Extending IpAddress class with method to return the scope of any give…
rodnymolina Feb 15, 2018
14ca39f
[schema] Update Config DB Table name #defines (#188)
jleveque Feb 26, 2018
9db96b0
[logger] Support logging to stdout/stderr (#186)
Mar 23, 2018
7047c5f
Add vxlan tunnel decap table name (#191)
pavel-shirshov Mar 26, 2018
e10a745
Fix a bug which caused SAI use unintialized log levels (#193)
pavel-shirshov Apr 9, 2018
8213777
Add map of database ID -> table name separator; No longer pass table …
jleveque Apr 10, 2018
dc57477
Make C++ Unit Tests work with recent table name separator changes (#194)
jleveque Apr 12, 2018
947c86d
Fix the C++ unit tests. Use more specific EXPECT_ clauses (#195)
pavel-shirshov Apr 13, 2018
1ef337a
Refactor Selectables. Add support for selectable priorities (#192)
pavel-shirshov Apr 16, 2018
82a17e3
[rules]: Clean pyext/*.py files in dh_clean (#196)
Apr 24, 2018
07a03cb
Pass arguments by references for constructors and methods (#197)
pavel-shirshov Apr 28, 2018
d2e627f
Use references when it's possible and other small enhancements. (#198)
pavel-shirshov May 2, 2018
e3f0b98
Make code using more C++11 features. (#199)
pavel-shirshov May 4, 2018
b4367a4
[schema]: add CFG_FLEX_COUNTER_TABLE_NAME macro (#200)
sihuihan88 May 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://sonic-jenkins.westus.cloudapp.azure.com/job/common/job/sonic-swss-common-build/badge/icon)](https://sonic-jenkins.westus.cloudapp.azure.com/job/common/job/sonic-swss-common-build/)
[![Build Status](https://sonic-jenkins.westus2.cloudapp.azure.com/job/common/job/sonic-swss-common-build/badge/icon)](https://sonic-jenkins.westus2.cloudapp.azure.com/job/common/job/sonic-swss-common-build/)

# SONiC - SWitch State Service Common Library - SWSS-COMMON

Expand All @@ -7,36 +7,18 @@ The SWitch State Service (SWSS) common library provides libraries for database c

## Getting Started

### Install
### Build from Source

Before installing, add key and package sources:
Checkout the source:

sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
echo 'deb http://apt-mo.trafficmanager.net/repos/sonic/ trusty main' | sudo tee -a /etc/apt/sources.list.d/sonic.list
sudo apt-get update
git clone --recursive https://github.com/Azure/sonic-swss-common

Install dependencies:

sudo apt-get install redis-server -t trusty
sudo apt-get install libhiredis0.13 -t trusty
Install build dependencies:

Install building dependencies:

sudo apt-get install libtool
sudo apt-get install autoconf automake
sudo apt-get install dh-exec

There are a few different ways you can install SONiC-SWSS.

#### Install from Debian Repo

For your convenience, you can install prepared packages on Debian Jessie:

sudo apt-get install sonic-swss-common

#### Install from Source

Checkout the source: `git clone https://github.com/Azure/sonic-swss-common.git` and install it yourself.
sudo apt-get install make libtool m4 autoconf dh-exec debhelper cmake pkg-config \
libhiredis-dev libnl-3-dev libnl-genl-3-dev libnl-route-3-dev swig3.0 \
libpython2.7-dev

You can compile and install from source using:

Expand All @@ -48,7 +30,7 @@ You can also build a debian package using:

./autogen.sh
./configure
fakeroot debian/rules binary
dpkg-buildpackage -us -uc -b

## Need Help?

Expand Down
2 changes: 1 addition & 1 deletion common/consumer_table_pops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ for i = n, 1, -1 do
st = st + 2
end

elseif op ~= 'get' and op ~= 'getresponse' and op ~= 'notify' then
elseif op ~= 'flush' and op ~= 'flushresponse' and op ~= 'get' and op ~= 'getresponse' and op ~= 'notify' then
local keyname = KEYS[4] .. ':' .. key
if key == '' then
keyname = KEYS[4]
Expand Down
6 changes: 3 additions & 3 deletions common/consumerstatetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace swss {

ConsumerStateTable::ConsumerStateTable(DBConnector *db, std::string tableName, int popBatchSize)
: ConsumerTableBase(db, tableName, popBatchSize)
ConsumerStateTable::ConsumerStateTable(DBConnector *db, const std::string &tableName, int popBatchSize, int pri)
: ConsumerTableBase(db, tableName, popBatchSize, pri)
, TableName_KeySet(tableName)
{
for (;;)
Expand All @@ -30,7 +30,7 @@ ConsumerStateTable::ConsumerStateTable(DBConnector *db, std::string tableName, i
setQueueLength(r.getReply<long long int>());
}

void ConsumerStateTable::pops(std::deque<KeyOpFieldsValuesTuple> &vkco, std::string /*prefix*/)
void ConsumerStateTable::pops(std::deque<KeyOpFieldsValuesTuple> &vkco, const std::string& /*prefix*/)
{
static std::string luaScript = loadLuaScript("consumer_state_table_pops.lua");

Expand Down
4 changes: 2 additions & 2 deletions common/consumerstatetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace swss {
class ConsumerStateTable : public ConsumerTableBase, public TableName_KeySet
{
public:
ConsumerStateTable(DBConnector *db, std::string tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE);
ConsumerStateTable(DBConnector *db, const std::string &tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE, int pri = 0);

/* Get multiple pop elements */
void pops(std::deque<KeyOpFieldsValuesTuple> &vkco, std::string prefix = EMPTY_PREFIX);
void pops(std::deque<KeyOpFieldsValuesTuple> &vkco, const std::string &prefix = EMPTY_PREFIX);
};

}
6 changes: 3 additions & 3 deletions common/consumertable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ using namespace std;

namespace swss {

ConsumerTable::ConsumerTable(DBConnector *db, string tableName, int popBatchSize)
: ConsumerTableBase(db, tableName, popBatchSize)
ConsumerTable::ConsumerTable(DBConnector *db, const string &tableName, int popBatchSize, int pri)
: ConsumerTableBase(db, tableName, popBatchSize, pri)
, TableName_KeyValueOpQueues(tableName)
{
for (;;)
Expand All @@ -34,7 +34,7 @@ ConsumerTable::ConsumerTable(DBConnector *db, string tableName, int popBatchSize
setQueueLength(r.getReply<long long int>());
}

void ConsumerTable::pops(deque<KeyOpFieldsValuesTuple> &vkco, string prefix)
void ConsumerTable::pops(deque<KeyOpFieldsValuesTuple> &vkco, const string &prefix)
{
static std::string luaScript = loadLuaScript("consumer_table_pops.lua");

Expand Down
4 changes: 2 additions & 2 deletions common/consumertable.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace swss {
class ConsumerTable : public ConsumerTableBase, public TableName_KeyValueOpQueues
{
public:
ConsumerTable(DBConnector *db, std::string tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE);
ConsumerTable(DBConnector *db, const std::string &tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE, int pri = 0);

/* Get multiple pop elements */
void pops(std::deque<KeyOpFieldsValuesTuple> &vkco, std::string prefix = EMPTY_PREFIX);
void pops(std::deque<KeyOpFieldsValuesTuple> &vkco, const std::string &prefix = EMPTY_PREFIX);
};

}
Expand Down
12 changes: 4 additions & 8 deletions common/consumertablebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

namespace swss {

ConsumerTableBase::ConsumerTableBase(DBConnector *db, std::string tableName, int popBatchSize):
TableConsumable(tableName),
ConsumerTableBase::ConsumerTableBase(DBConnector *db, const std::string &tableName, int popBatchSize, int pri):
TableConsumable(db->getDbId(), tableName, pri),
RedisTransactioner(db),
POP_BATCH_SIZE(popBatchSize)
{
}

ConsumerTableBase::~ConsumerTableBase()
{
}

void ConsumerTableBase::pop(KeyOpFieldsValuesTuple &kco, std::string prefix)
void ConsumerTableBase::pop(KeyOpFieldsValuesTuple &kco, const std::string &prefix)
{
pop(kfvKey(kco), kfvOp(kco), kfvFieldsValues(kco), prefix);
}

void ConsumerTableBase::pop(std::string &key, std::string &op, std::vector<FieldValueTuple> &fvs, std::string prefix)
void ConsumerTableBase::pop(std::string &key, std::string &op, std::vector<FieldValueTuple> &fvs, const std::string &prefix)
{
if (m_buffer.empty())
{
Expand Down
8 changes: 4 additions & 4 deletions common/consumertablebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class ConsumerTableBase: public TableConsumable, public RedisTransactioner
public:
const int POP_BATCH_SIZE;

ConsumerTableBase(DBConnector *db, std::string tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE);
ConsumerTableBase(DBConnector *db, const std::string &tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE, int pri = 0);

virtual ~ConsumerTableBase();
~ConsumerTableBase() override = default;

void pop(KeyOpFieldsValuesTuple &kco, std::string prefix = EMPTY_PREFIX);
void pop(KeyOpFieldsValuesTuple &kco, const std::string &prefix = EMPTY_PREFIX);

void pop(std::string &key, std::string &op, std::vector<FieldValueTuple> &fvs, std::string prefix = EMPTY_PREFIX);
void pop(std::string &key, std::string &op, std::vector<FieldValueTuple> &fvs, const std::string &prefix = EMPTY_PREFIX);

protected:

Expand Down
8 changes: 4 additions & 4 deletions common/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace swss {

static inline uint64_t __to_uint64(std::string str, uint64_t min = std::numeric_limits<uint64_t>::min(), uint64_t max = std::numeric_limits<uint64_t>::max())
static inline uint64_t __to_uint64(const std::string &str, uint64_t min = std::numeric_limits<uint64_t>::min(), uint64_t max = std::numeric_limits<uint64_t>::max())
{
size_t idx = 0;
uint64_t ret = stoul(str, &idx, 0);
Expand All @@ -27,7 +27,7 @@ static inline uint64_t __to_uint64(std::string str, uint64_t min = std::numeric_
return ret;
}

static inline int64_t __to_int64(std::string str, int64_t min = std::numeric_limits<int64_t>::min(), int64_t max = std::numeric_limits<int64_t>::max())
static inline int64_t __to_int64(const std::string &str, int64_t min = std::numeric_limits<int64_t>::min(), int64_t max = std::numeric_limits<int64_t>::max())
{
size_t idx = 0;
int64_t ret = stol(str, &idx, 0);
Expand All @@ -45,7 +45,7 @@ static inline int64_t __to_int64(std::string str, int64_t min = std::numeric_lim
}

template <typename T>
T to_int(std::string str, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
T to_int(const std::string &str, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
{
static_assert(std::is_signed<T>::value, "Signed integer is expected");
static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<int64_t>::max(), "Type is too big");
Expand All @@ -54,7 +54,7 @@ T to_int(std::string str, T min = std::numeric_limits<T>::min(), T max = std::nu
}

template <typename T>
T to_uint(std::string str, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
T to_uint(const std::string &str, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
{
static_assert(std::is_unsigned<T>::value, "Unsigned integer is expected");
static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<uint64_t>::max(), "Type is too big");
Expand Down
18 changes: 9 additions & 9 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ constexpr const char *DBConnector::DEFAULT_UNIXSOCKET;
void DBConnector::select(DBConnector *db)
{
string select("SELECT ");
select += to_string(db->getDB());
select += to_string(db->getDbId());

RedisReply r(db, select, REDIS_REPLY_STATUS);
r.checkStatusOK();
Expand All @@ -28,9 +28,9 @@ DBConnector::~DBConnector()
redisFree(m_conn);
}

DBConnector::DBConnector(int db, string hostname, int port,
DBConnector::DBConnector(int dbId, const string& hostname, int port,
unsigned int timeout) :
m_db(db)
m_dbId(dbId)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};

Expand All @@ -46,8 +46,8 @@ DBConnector::DBConnector(int db, string hostname, int port,
select(this);
}

DBConnector::DBConnector(int db, string unixPath, unsigned int timeout) :
m_db(db)
DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout) :
m_dbId(dbId)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};

Expand All @@ -68,20 +68,20 @@ redisContext *DBConnector::getContext()
return m_conn;
}

int DBConnector::getDB()
int DBConnector::getDbId()
{
return m_db;
return m_dbId;
}

DBConnector *DBConnector::newConnector(unsigned int timeout)
{
if (getContext()->connection_type == REDIS_CONN_TCP)
return new DBConnector(getDB(),
return new DBConnector(getDbId(),
getContext()->tcp.host,
getContext()->tcp.port,
timeout);
else
return new DBConnector(getDB(),
return new DBConnector(getDbId(),
getContext()->unix_sock.path,
timeout);
}
Expand Down
8 changes: 4 additions & 4 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class DBConnector
* Timeout - The time in milisecond until exception is been thrown. For
* infinite wait, set this value to 0
*/
DBConnector(int db, std::string hostname, int port, unsigned int timeout);
DBConnector(int db, std::string unixPath, unsigned int timeout);
DBConnector(int dbId, const std::string &hostname, int port, unsigned int timeout);
DBConnector(int dbId, const std::string &unixPath, unsigned int timeout);

~DBConnector();

redisContext *getContext();
int getDB();
int getDbId();

static void select(DBConnector *db);

Expand All @@ -35,7 +35,7 @@ class DBConnector

private:
redisContext *m_conn;
int m_db;
int m_dbId;
};

}
Expand Down
44 changes: 43 additions & 1 deletion common/ipaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ IpAddress::IpAddress(uint32_t ip)

IpAddress::IpAddress(const std::string &ipStr)
{
if (ipStr.find(":") != std::string::npos)
if (ipStr.find(':') != std::string::npos)
{
m_ip.family = AF_INET6;
}
Expand All @@ -37,3 +37,45 @@ std::string IpAddress::to_string() const

return ipStr;
}

IpAddress::AddrScope IpAddress::getAddrScope() const
{
/* Auxiliary prefixes used to determine the scope of any given address */
static const IpAddress ipv4LinkScopeAddress = IpAddress("169.254.0.0");
static const IpAddress ipv6LinkScopeAddress = IpAddress("FE80::0");
static const IpAddress ipv4HostScopeAddress = IpAddress("127.0.0.1");
static const IpAddress ipv6HostScopeAddress = IpAddress("::1");

if (isV4())
{
const uint32_t ip1 = htonl(getV4Addr());
const uint32_t ip2 = htonl(ipv4LinkScopeAddress.getV4Addr());

/* IPv4 local-scope mask is 16 bits long -- mask = 0xffff0000 */
if ((ip1 & 0xffff0000) == ip2)
{
return LINK_SCOPE;
}
else if (*this == ipv4HostScopeAddress)
{
return HOST_SCOPE;
}
}
else
{
const uint8_t *ip1 = getV6Addr();
const uint8_t *ip2 = ipv6LinkScopeAddress.getV6Addr();

/* IPv6 local-scope mask is 10 bits long -- mask = 0xffc0::0 */
if ((ip1[0] == ip2[0]) && ((ip1[1] & 0xc0) == ip2[1]))
{
return LINK_SCOPE;
}
else if (*this == ipv6HostScopeAddress)
{
return HOST_SCOPE;
}
}

return GLOBAL_SCOPE;
}
12 changes: 10 additions & 2 deletions common/ipaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct ip_addr_t {
class IpAddress
{
public:
IpAddress() {}
IpAddress(const ip_addr_t &ip) { m_ip = ip; }
IpAddress() = default;
IpAddress(const ip_addr_t &ip) : m_ip(ip) {}
IpAddress(uint32_t ip);
IpAddress(const std::string &ipStr);

Expand Down Expand Up @@ -74,6 +74,14 @@ class IpAddress

std::string to_string() const;

enum AddrScope {
GLOBAL_SCOPE,
LINK_SCOPE,
HOST_SCOPE
};

IpAddress::AddrScope getAddrScope() const;

private:
struct ip_addr_t m_ip;
};
Expand Down
4 changes: 2 additions & 2 deletions common/ipaddresses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace swss;
IpAddresses::IpAddresses(const string &ipsStr)
{
auto ips = tokenize(ipsStr, IP_DELIMITER);
for (auto ip : ips)
for (const auto &ip : ips)
m_ips.insert(ip);
}

Expand Down Expand Up @@ -39,7 +39,7 @@ bool IpAddresses::contains(const IpAddress &ip) const

bool IpAddresses::contains(const IpAddresses &ips) const
{
for (auto ip : ips.getIpAddresses())
for (const auto &ip : ips.getIpAddresses())
{
if (!contains(ip))
{
Expand Down
Loading