Skip to content

Commit

Permalink
[teammgrd]: Add teammgrd to manage LAG related configurations (sonic-…
Browse files Browse the repository at this point in the history
…net#626)

teammgrd will monitor configuration changes in CFG_LAG_TABLE_NAME
and CFG_LAG_MEMBER_TABLE_NAME. It supports creation/removal of
LAGs as well as membership changes of the LAG members.

Signed-off-by: Shu0T1an ChenG <shuche@microsoft.com>
  • Loading branch information
Shuotian Cheng authored and lguohan committed Oct 19, 2018
1 parent e1cc0de commit 2115a99
Show file tree
Hide file tree
Showing 12 changed files with 691 additions and 103 deletions.
8 changes: 7 additions & 1 deletion cfgmgr/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/orchagent -I $(top_srcdir)/warmrestart
CFLAGS_SAI = -I /usr/include/sai

bin_PROGRAMS = vlanmgrd portmgrd intfmgrd buffermgrd vrfmgrd
bin_PROGRAMS = vlanmgrd teammgrd portmgrd intfmgrd buffermgrd vrfmgrd

if DEBUG
DBGFLAGS = -ggdb -DDEBUG
Expand All @@ -15,6 +15,12 @@ vlanmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
vlanmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
vlanmgrd_LDADD = -lswsscommon

teammgrd_SOURCES = teammgrd.cpp teammgr.cpp $(top_srcdir)/orchagent/orch.cpp $(top_srcdir)/orchagent/request_parser.cpp \
shellcmd.h $(top_srcdir)/warmrestart/warm_restart.cpp $(top_srcdir)/warmrestart/warm_restart.h
teammgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
teammgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
teammgrd_LDADD = -lswsscommon

portmgrd_SOURCES = portmgrd.cpp portmgr.cpp $(top_srcdir)/orchagent/orch.cpp $(top_srcdir)/orchagent/request_parser.cpp shellcmd.h
portmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
portmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
Expand Down
97 changes: 38 additions & 59 deletions cfgmgr/portmgr.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <string>

#include "logger.h"
#include "dbconnector.h"
#include "producerstatetable.h"
Expand All @@ -15,57 +13,27 @@ using namespace swss;
PortMgr::PortMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) :
Orch(cfgDb, tableNames),
m_cfgPortTable(cfgDb, CFG_PORT_TABLE_NAME),
m_cfgLagTable(cfgDb, CFG_LAG_TABLE_NAME),
m_cfgLagMemberTable(cfgDb, CFG_LAG_MEMBER_TABLE_NAME),
m_statePortTable(stateDb, STATE_PORT_TABLE_NAME),
m_stateLagTable(stateDb, STATE_LAG_TABLE_NAME),
m_appPortTable(appDb, APP_PORT_TABLE_NAME),
m_appLagTable(appDb, APP_LAG_TABLE_NAME)
m_appPortTable(appDb, APP_PORT_TABLE_NAME)
{
}

bool PortMgr::setPortMtu(const string &table, const string &alias, const string &mtu)
bool PortMgr::setPortMtu(const string &alias, const string &mtu)
{
stringstream cmd;
string res;

// ip link set dev <port_name> mtu <mtu>
cmd << IP_CMD << " link set dev " << alias << " mtu " << mtu;
EXEC_WITH_ERROR_THROW(cmd.str(), res);

if (table == CFG_PORT_TABLE_NAME)
{
// Set the port MTU in application database to update both
// the port MTU and possibly the port based router interface MTU
vector<FieldValueTuple> fvs;
FieldValueTuple fv("mtu", mtu);
fvs.push_back(fv);
m_appPortTable.set(alias, fvs);
}
else if (table == CFG_LAG_TABLE_NAME)
{
// Set the port channel MTU in application database to update
// the LAG based router interface MTU in orchagent
vector<FieldValueTuple> fvs;
FieldValueTuple fv("mtu", mtu);
fvs.push_back(fv);
m_appLagTable.set(alias, fvs);

m_cfgLagTable.get(alias, fvs);
for (auto fv: fvs)
{
// Set the port channel members MTU in application database
// to update the port MTU in orchagent
if (fvField(fv) == "members")
{
for (auto member : tokenize(fvValue(fv), ','))
{
vector<FieldValueTuple> member_fvs;
FieldValueTuple member_fv("mtu", mtu);
member_fvs.push_back(member_fv);
m_appPortTable.set(member, member_fvs);
}
}
}
}
// Set the port MTU in application database to update both
// the port MTU and possibly the port based router interface MTU
vector<FieldValueTuple> fvs;
FieldValueTuple fv("mtu", mtu);
fvs.push_back(fv);
m_appPortTable.set(alias, fvs);

return true;
}
Expand All @@ -75,31 +43,26 @@ bool PortMgr::setPortAdminStatus(const string &alias, const bool up)
stringstream cmd;
string res;

// ip link set dev <port_name> [up|down]
cmd << IP_CMD << " link set dev " << alias << (up ? " up" : " down");
EXEC_WITH_ERROR_THROW(cmd.str(), res);

vector<FieldValueTuple> fvs;
FieldValueTuple fv("admin_status", (up ? "up" : "down"));
fvs.push_back(fv);
m_appPortTable.set(alias, fvs);

return true;
}

bool PortMgr::isPortStateOk(const string &table, const string &alias)
bool PortMgr::isPortStateOk(const string &alias)
{
vector<FieldValueTuple> temp;

if (table == CFG_PORT_TABLE_NAME)
{
if (m_statePortTable.get(alias, temp))
{
SWSS_LOG_INFO("Port %s is ready", alias.c_str());
return true;
}
}
else if (table == CFG_LAG_TABLE_NAME)
if (m_statePortTable.get(alias, temp))
{
if (m_stateLagTable.get(alias, temp))
{
SWSS_LOG_INFO("Lag %s is ready", alias.c_str());
return true;
}
SWSS_LOG_INFO("Port %s is ready", alias.c_str());
return true;
}

return false;
Expand All @@ -119,9 +82,25 @@ void PortMgr::doTask(Consumer &consumer)
string alias = kfvKey(t);
string op = kfvOp(t);

// Skip port which is a member of a port channel
vector<string> keys;
m_cfgLagMemberTable.getKeys(keys);

for (auto key : keys)
{
auto tokens = tokenize(key, '|');
auto member = tokens[1];

if (alias == member)
{
it = consumer.m_toSync.erase(it);
continue;
}
}

if (op == SET_COMMAND)
{
if (!isPortStateOk(table, alias))
if (!isPortStateOk(alias))
{
SWSS_LOG_INFO("Port %s is not ready, pending...", alias.c_str());
it++;
Expand All @@ -133,7 +112,7 @@ void PortMgr::doTask(Consumer &consumer)
if (fvField(i) == "mtu")
{
auto mtu = fvValue(i);
setPortMtu(table, alias, mtu);
setPortMtu(alias, mtu);
SWSS_LOG_NOTICE("Configure %s MTU to %s",
alias.c_str(), mtu.c_str());
}
Expand Down
18 changes: 7 additions & 11 deletions cfgmgr/portmgr.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef __INTFMGR__
#define __INTFMGR__
#pragma once

#include "dbconnector.h"
#include "producerstatetable.h"
#include "orch.h"
#include "producerstatetable.h"

#include <map>
#include <set>
#include <string>

namespace swss {
Expand All @@ -14,22 +14,18 @@ class PortMgr : public Orch
{
public:
PortMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames);
using Orch::doTask;

using Orch::doTask;
private:
Table m_cfgPortTable;
Table m_cfgLagTable;
Table m_cfgLagMemberTable;
Table m_statePortTable;
Table m_stateLagTable;
ProducerStateTable m_appPortTable;
ProducerStateTable m_appLagTable;

void doTask(Consumer &consumer);
bool setPortMtu(const string &table, const string &alias, const string &mtu);
bool setPortMtu(const string &alias, const string &mtu);
bool setPortAdminStatus(const string &alias, const bool up);
bool isPortStateOk(const string &table, const string &alias);
bool isPortStateOk(const string &alias);
};

}

#endif
17 changes: 8 additions & 9 deletions cfgmgr/portmgrd.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <fstream>
#include <iostream>
#include <mutex>
#include <unistd.h>
#include <vector>
#include <mutex>
#include "dbconnector.h"
#include "select.h"

#include "exec.h"
#include "schema.h"
#include "portmgr.h"
#include <fstream>
#include <iostream>
#include "schema.h"
#include "select.h"

using namespace std;
using namespace swss;
Expand Down Expand Up @@ -42,7 +42,6 @@ int main(int argc, char **argv)
{
vector<string> cfg_port_tables = {
CFG_PORT_TABLE_NAME,
CFG_LAG_TABLE_NAME,
};

DBConnector cfgDb(CONFIG_DB, DBConnector::DEFAULT_UNIXSOCKET, 0);
Expand All @@ -52,7 +51,7 @@ int main(int argc, char **argv)
PortMgr portmgr(&cfgDb, &appDb, &stateDb, cfg_port_tables);

// TODO: add tables in stateDB which interface depends on to monitor list
std::vector<Orch *> cfgOrchList = {&portmgr};
vector<Orch *> cfgOrchList = {&portmgr};

swss::Select s;
for (Orch *o : cfgOrchList)
Expand Down Expand Up @@ -81,7 +80,7 @@ int main(int argc, char **argv)
c->execute();
}
}
catch(const std::exception &e)
catch (const exception &e)
{
SWSS_LOG_ERROR("Runtime error: %s", e.what());
}
Expand Down
2 changes: 2 additions & 0 deletions cfgmgr/shellcmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define ECHO_CMD "/bin/echo"
#define BASH_CMD "/bin/bash"
#define GREP_CMD "/bin/grep"
#define TEAMD_CMD "/usr/bin/teamd"
#define TEAMDCTL_CMD "/usr/bin/teamdctl"

#define EXEC_WITH_ERROR_THROW(cmd, res) ({ \
int ret = swss::exec(cmd, res); \
Expand Down
Loading

0 comments on commit 2115a99

Please sign in to comment.