Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
56 changes: 56 additions & 0 deletions teamsyncd/teamsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "warm_restart.h"
#include "teamsync.h"

#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;
Expand All @@ -20,6 +24,7 @@ using namespace swss;

/* Taken from drivers/net/team/team.c */
#define TEAM_DRV_NAME "team"
#define PID_FILE_PATH "/var/run/teamd/"

TeamSync::TeamSync(DBConnector *db, DBConnector *stateDb, Select *select) :
m_select(select),
Expand Down Expand Up @@ -109,6 +114,22 @@ void TeamSync::onMsg(int nlmsg_type, struct nl_object *obj)
if (!type || (strcmp(type, TEAM_DRV_NAME) != 0))
return;

unsigned int flags = rtnl_link_get_flags(link);
bool admin = flags & IFF_UP;
bool oper = flags & IFF_LOWER_UP;
unsigned int ifindex = rtnl_link_get_ifindex(link);

if (type)
{
SWSS_LOG_INFO(" nlmsg type:%d key:%s admin:%d oper:%d ifindex:%d type:%s",
nlmsg_type, lagName.c_str(), admin, oper, ifindex, type);
}
else
{
SWSS_LOG_INFO(" nlmsg type:%d key:%s admin:%d oper:%d ifindex:%d",
nlmsg_type, lagName.c_str(), admin, oper, ifindex);
}

if (nlmsg_type == RTM_DELLINK)
{
if (m_teamSelectables.find(lagName) != m_teamSelectables.end())
Expand Down Expand Up @@ -194,6 +215,41 @@ void TeamSync::removeLag(const string &lagName)
m_selectablesToRemove.insert(lagName);
}

#define MAX_PID_STRLEN 10
void TeamSync::cleanTeamProcesses(int signo)
{
ssize_t sz = 0;
int fd, pid;
char pid_str[MAX_PID_STRLEN] = {0};
char *endptr = NULL;
string file_path;

for (auto it=m_teamSelectables.begin(); it!=m_teamSelectables.end(); ++it)
{
/* There are PID files created for each teamd process in /var/run/teamd DIR */
file_path = string(PID_FILE_PATH) + it->first + string(".pid");

/* Open the file, get the PID and send signal to the process */
fd = open(file_path.c_str(), O_RDONLY);
if (fd > 0)
{
memset(pid_str, 0, MAX_PID_STRLEN);
sz = read(fd, pid_str, MAX_PID_STRLEN-1);
if(sz > 0)
{
pid = (pid_t)strtol(pid_str, &endptr, 10);
if(pid > 0)
{
kill(pid, signo);
}
}
close(fd);
}
file_path.clear();
}
return;
}

const struct team_change_handler TeamSync::TeamPortSync::gPortChangeHandler = {
.func = TeamSync::TeamPortSync::teamdHandler,
.type_mask = TEAM_PORT_CHANGE | TEAM_OPTION_CHANGE
Expand Down
1 change: 1 addition & 0 deletions teamsyncd/teamsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TeamSync : public NetMsg
TeamSync(DBConnector *db, DBConnector *stateDb, Select *select);

void periodic();
void cleanTeamProcesses(int signo);

/* Listen to RTM_NEWLINK, RTM_DELLINK to track team devices */
virtual void onMsg(int nlmsg_type, struct nl_object *obj);
Expand Down
20 changes: 20 additions & 0 deletions teamsyncd/teamsyncd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <team.h>
#include <signal.h>
#include "logger.h"
#include "select.h"
#include "netdispatcher.h"
Expand All @@ -9,17 +10,36 @@
using namespace std;
using namespace swss;

TeamSync *syncPtr = NULL;

void sig_handler(int signo)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signo [](start = 21, length = 5)

the same

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Signal handler is only registered only for SIGTERM now. The handler will be called only on SIGTERM signal.

{
/* Stop the teamd processes present */
if(syncPtr)
{
syncPtr->cleanTeamProcesses(signo);
}

return;
}

int main(int argc, char **argv)
{
swss::Logger::linkToDbNative(TEAMSYNCD_APP_NAME);
DBConnector db("APPL_DB", 0);
DBConnector stateDb("STATE_DB", 0);
Select s;
TeamSync sync(&db, &stateDb, &s);
syncPtr = &sync;

NetDispatcher::getInstance().registerMessageHandler(RTM_NEWLINK, &sync);
NetDispatcher::getInstance().registerMessageHandler(RTM_DELLINK, &sync);

/* Register the signal handler for various signals */
signal(SIGTERM, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGQUIT, sig_handler);

while (1)
{
try
Expand Down