Skip to content

Commit

Permalink
[teamsyncd]: Add LAG members to syncd when created.
Browse files Browse the repository at this point in the history
It is possible that LAG members are created after teamd
creates LAG. In that case we have to monitor RTM_NEWLINK
messages and addd them via teamdctl.

Signed-off-by: marian-pritsak <[email protected]>
  • Loading branch information
marian-pritsak committed Mar 27, 2017
1 parent dc33b75 commit aebece4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion teamsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ teamsyncd_SOURCES = teamsyncd.cpp teamsync.cpp

teamsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
teamsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
teamsyncd_LDADD = -lnl-3 -lnl-route-3 -lhiredis -lswsscommon -lteam
teamsyncd_LDADD = -lnl-3 -lnl-route-3 -lhiredis -lswsscommon -lteam -lteamdctl
58 changes: 54 additions & 4 deletions teamsyncd/teamsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sys/socket.h>
#include <linux/if.h>
#include <netlink/route/link.h>
#include <teamdctl.h>
#include "logger.h"
#include "netmsg.h"
#include "dbconnector.h"
Expand All @@ -28,18 +29,27 @@ void TeamSync::onMsg(int nlmsg_type, struct nl_object *obj)
if ((nlmsg_type != RTM_NEWLINK) && (nlmsg_type != RTM_DELLINK))
return;

string lagName = rtnl_link_get_name(link);
string intfName = rtnl_link_get_name(link);
/* Listens to LAG messages */
char *type = rtnl_link_get_type(link);
if (!type || (strcmp(type, TEAM_DRV_NAME) != 0))
{
if (nlmsg_type == RTM_NEWLINK)
{
for (const auto& i : m_teamPorts)
{
i.second->syncMember(intfName);
}
}
return;
}

bool tracked = m_teamPorts.find(lagName) != m_teamPorts.end();
bool tracked = m_teamPorts.find(intfName) != m_teamPorts.end();

if ((nlmsg_type == RTM_DELLINK) && tracked)
{
/* Remove LAG ports and delete LAG */
removeLag(lagName);
removeLag(intfName);
return;
}

Expand All @@ -50,7 +60,7 @@ void TeamSync::onMsg(int nlmsg_type, struct nl_object *obj)
* New LAG was dedcated for the first time. Sync admin and oper state since
* portsyncd reflects only changes
*/
addLag(lagName, rtnl_link_get_ifindex(link),
addLag(intfName, rtnl_link_get_ifindex(link),
rtnl_link_get_flags(link) & IFF_UP,
rtnl_link_get_flags(link) & IFF_LOWER_UP,
rtnl_link_get_mtu(link));
Expand Down Expand Up @@ -119,6 +129,19 @@ TeamSync::TeamPortSync::TeamPortSync(const string &lagName, int ifindex,
"Unable to register port change event");
}

m_tdc = teamdctl_alloc();

if (m_tdc == NULL)
{
SWSS_LOG_THROW("Failed to allocate teamdctl context.");
}

err = teamdctl_connect(m_tdc, m_lagName.c_str(), NULL, NULL);
if (err)
{
SWSS_LOG_THROW("Failed to connect to %s teamdctl.", m_lagName.c_str());
}

/* Sync LAG at first */
onChange();
}
Expand All @@ -130,6 +153,12 @@ TeamSync::TeamPortSync::~TeamPortSync()
team_change_handler_unregister(m_team, &gPortChangeHandler, this);
team_free(m_team);
}

if (m_tdc)
{
teamdctl_disconnect(m_tdc);
teamdctl_free(m_tdc);
}
}

int TeamSync::TeamPortSync::onChange()
Expand Down Expand Up @@ -207,3 +236,24 @@ void TeamSync::TeamPortSync::readMe()
{
team_handle_events(m_team);
}

void TeamSync::TeamPortSync::syncMember(const std::string& intfName)
{
const std::string config(teamdctl_config_get_raw(m_tdc));
const std::string intfNameStr("\"" + (intfName + "\""));

if (config.find(intfNameStr) == std::string::npos)
{
return;
}

if (m_lagMembers.find(intfName) != m_lagMembers.end())
{
return;
}

if (teamdctl_port_add(m_tdc, intfName.c_str()))
{
SWSS_LOG_ERROR("Failed to add port %s to %s", intfName.c_str(), m_lagName.c_str());
}
}
2 changes: 2 additions & 0 deletions teamsyncd/teamsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TeamSync : public NetMsg
virtual bool isMe(fd_set *fd);
virtual int readCache();
virtual void readMe();
void syncMember(const std::string& intfName);

protected:
int onChange();
Expand All @@ -45,6 +46,7 @@ class TeamSync : public NetMsg
private:
ProducerStateTable *m_lagTable;
struct team_handle *m_team;
struct teamdctl *m_tdc;
std::string m_lagName;
int m_ifindex;
std::map<std::string, bool> m_lagMembers; /* map[ifname] = status (enabled|disabled) */
Expand Down

0 comments on commit aebece4

Please sign in to comment.