This appendix provides a summary of the libswsscommon API usage guide.
libswsscommon is SONiC’s C++ helper library that wraps:
- Redis client operations
- Notification channels
- Table abstractions
- Producer/consumer semantics
- Event loops (Select interface)
The library is used by:
- orchagent
- vlanmgrd, intfmgrd, teammgrd
- neighsyncd, portsyncd
- fdbsyncd
- Various platform daemons
Lowest-level Redis connector in SONiC.
#include "dbconnector.h"
using namespace swss;
DBConnector appl_db("APPL_DB", 0); // 0 = timeout in msParameters:
"APPL_DB"— database name0— timeout (0 = no timeout)
DBConnector is passed into all other objects.
Use Table when:
- reading CONFIG_DB
- reading STATE_DB
- reading static APPL_DB entries
- writing without notification semantics
DBConnector db("CONFIG_DB", 0);
Table portTable(&db, "PORT");
std::vector<FieldValueTuple> fvs;
portTable.get("Ethernet0", fvs);
for (auto &fv : fvs)
{
std::cout << fvField(fv) << "=" << fvValue(fv) << std::endl;
}FieldValueTuple fv1("admin_status", "up");
FieldValueTuple fv2("mtu", "9216");
std::vector<FieldValueTuple> fvs = {fv1, fv2};
portTable.set("Ethernet0", fvs);portTable.del("Ethernet0");Used heavily when a daemon wants to:
- send desired state to orchagent
- notify orchagent of configuration changes
DBConnector db("APPL_DB", 0);
ProducerStateTable tbl(&db, "VLAN_MEMBER_TABLE");
FieldValueTuple fv("tagging_mode", "untagged");
std::vector<FieldValueTuple> fvs = {fv};
tbl.set("Vlan10|Ethernet0", fvs);tbl.del("Vlan10|Ethernet0");ProducerStateTable ensures:
- value is written to Redis
- event is queued atomically
- orchagent receives update in-order
Used by:
- orchagent (reading APPL_DB)
- neighsyncd (reading kernel → APPL_DB writes)
- teammgrd, vlanmgrd, intfmgrd
DBConnector db("APPL_DB", 0);
ConsumerStateTable cst(&db, "PORT_TABLE");
std::string key;
std::vector<FieldValueTuple> fvs;
if (cst.pop(key, fvs))
{
// process event for key
}Select allows SONiC daemons to listen to multiple tables.
DBConnector db("APPL_DB", 0);
ConsumerStateTable portT(&db, "PORT_TABLE");
ConsumerStateTable vlanT(&db, "VLAN_TABLE");
ConsumerStateTable fdbT(&db, "FDB_TABLE");
Select selector;
selector.addSelectable(&portT);
selector.addSelectable(&vlanT);
selector.addSelectable(&fdbT);
while (true)
{
Selectable *sel;
int result = selector.select(&sel);
if (result == Select::ERROR)
continue;
std::string key;
std::vector<FieldValueTuple> fvs;
if (sel == &portT)
{
portT.pop(key, fvs);
// handle port event
}
else if (sel == &vlanT)
{
vlanT.pop(key, fvs);
// handle VLAN event
}
else if (sel == &fdbT)
{
fdbT.pop(key, fvs);
// handle FDB event
}
}This pattern guarantees:
- no busy waiting
- fair scheduling
- deterministic in-table ordering
- Use
Tableto read CONFIG_DB - Use Linux syscalls (netlink) for system configuration
- Use ProducerStateTable to signal orchagent
- syncd receives event
- writes to APPL_DB via ProducerStateTable
- orchagent processes event
Tables read via:
Table.get()- full table iteration via
getTableKeys()
- Always prefer ProducerStateTable for APPL_DB writes
- Use Table for CONFIG_DB/STATE_DB
- Use Select for multi-table event loops
- Never block inside the event loop
- Keep FieldValuePairs minimal
- Always validate keys before writing
libswsscommon provides the glue that allows SONiC daemons to:
- read config
- publish desired state
- subscribe to updates
- receive event-driven notifications
- interact cleanly with orchagent
It is the foundation for building new SONiC daemons and extending existing ones.