Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
TACappleman committed Oct 19, 2021
1 parent cf32453 commit 0ae4eb7
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 212 deletions.
14 changes: 8 additions & 6 deletions orchagent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ INCLUDES = -I $(top_srcdir)/lib \
-I $(top_srcdir)/warmrestart \
-I flex_counter \
-I debug_counter \
-I pbh
-I pbh \
-I nhg

CFLAGS_SAI = -I /usr/include/sai

Expand Down Expand Up @@ -39,9 +40,11 @@ orchagent_SOURCES = \
orchdaemon.cpp \
orch.cpp \
notifications.cpp \
nhgbase.cpp \
nhghandler.cpp \
cbfnhghandler.cpp \
nhgorch.cpp \
nhg/nhgbase.cpp \
nhg/nhghandler.cpp \
nhg/cbfnhghandler.cpp \
nhgmaporch.cpp \
routeorch.cpp \
mplsrouteorch.cpp \
neighorch.cpp \
Expand Down Expand Up @@ -82,8 +85,7 @@ orchagent_SOURCES = \
isolationgrouporch.cpp \
muxorch.cpp \
macsecorch.cpp \
lagid.cpp \
nhgmaporch.cpp
lagid.cpp

orchagent_SOURCES += flex_counter/flex_counter_manager.cpp flex_counter/flex_counter_stat_manager.cpp
orchagent_SOURCES += debug_counter/debug_counter.cpp debug_counter/drop_counter.cpp
Expand Down
22 changes: 11 additions & 11 deletions orchagent/cbfnhghandler.cpp → orchagent/nhg/cbfnhghandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void CbfNhgHandler::doTask(Consumer& consumer)
string op = kfvOp(t);

bool success;
const auto &cbf_nhg_it = m_syncedNextHopGroups.find(index);
const auto &cbf_nhg_it = m_syncdNextHopGroups.find(index);

if (op == SET_COMMAND)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ void CbfNhgHandler::doTask(Consumer& consumer)
/*
* If the CBF group does not exist, create it.
*/
if (cbf_nhg_it == m_syncedNextHopGroups.end())
if (cbf_nhg_it == m_syncdNextHopGroups.end())
{
/*
* If we reached the NHG limit, postpone the creation.
Expand All @@ -93,21 +93,21 @@ void CbfNhgHandler::doTask(Consumer& consumer)
}
else
{
auto cbf_nhg = CbfNhg(index, p.second, selection_map);
success = cbf_nhg.sync();
auto cbf_nhg = std::make_unique<CbfNhg>(index, p.second, selection_map);
success = cbf_nhg->sync();

if (success)
{
/*
* If the CBF NHG contains temporary NHGs as members,
* we have to keep checking for updates.
*/
if (cbf_nhg.hasTemps())
if (cbf_nhg->hasTemps())
{
success = false;
}

m_syncedNextHopGroups.emplace(index, NhgEntry<CbfNhg>(move(cbf_nhg)));
m_syncdNextHopGroups.emplace(index, NhgEntry<CbfNhg>(move(cbf_nhg)));
}
}
}
Expand All @@ -116,13 +116,13 @@ void CbfNhgHandler::doTask(Consumer& consumer)
*/
else
{
success = cbf_nhg_it->second.nhg.update(p.second, selection_map);
success = cbf_nhg_it->second.nhg->update(p.second, selection_map);

/*
* If the CBF NHG has temporary NHGs synced, we need to keep
* checking this group in case they are promoted.
*/
if (cbf_nhg_it->second.nhg.hasTemps())
if (cbf_nhg_it->second.nhg->hasTemps())
{
success = false;
}
Expand All @@ -143,7 +143,7 @@ void CbfNhgHandler::doTask(Consumer& consumer)
success = true;
}
/* If the group doesn't exist, do nothing. */
else if (cbf_nhg_it == m_syncedNextHopGroups.end())
else if (cbf_nhg_it == m_syncdNextHopGroups.end())
{
SWSS_LOG_WARN("Deleting inexistent CBF NHG %s", index.c_str());
/*
Expand All @@ -161,11 +161,11 @@ void CbfNhgHandler::doTask(Consumer& consumer)
/* Otherwise, delete it. */
else
{
success = cbf_nhg_it->second.nhg.remove();
success = cbf_nhg_it->second.nhg->remove();

if (success)
{
m_syncedNextHopGroups.erase(cbf_nhg_it);
m_syncdNextHopGroups.erase(cbf_nhg_it);
}
}
}
Expand Down
File renamed without changes.
15 changes: 7 additions & 8 deletions orchagent/nhgbase.cpp → orchagent/nhg/nhgbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

extern sai_object_id_t gSwitchId;

unsigned NhgBase::m_syncedCount = 0;
unsigned NhgBase::m_syncdCount = 0;

/*
* Purpose: Destructor.
Expand All @@ -17,11 +17,10 @@ NhgBase::~NhgBase()
{
SWSS_LOG_ENTER();

if (isSynced())
{
SWSS_LOG_ERROR("Destroying next hop group with SAI ID %lu which is still synced.", m_id);
assert(false);
}
/*
* The group member should be removed from its group before destroying it.
*/
assert(!isSynced());
}

/*
Expand All @@ -35,11 +34,11 @@ void NhgBase::decSyncedCount()
{
SWSS_LOG_ENTER();

if (m_syncedCount == 0)
if (m_syncdCount == 0)
{
SWSS_LOG_ERROR("Decreasing next hop groups count while already 0");
throw logic_error("Decreasing next hop groups count while already 0");
}

--m_syncedCount;
--m_syncdCount;
}
64 changes: 20 additions & 44 deletions orchagent/nhgbase.h → orchagent/nhg/nhgbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class NhgBase
*/
inline sai_object_id_t getId() const { SWSS_LOG_ENTER(); return m_id; }
static inline unsigned getSyncedCount()
{ SWSS_LOG_ENTER(); return m_syncedCount; }
{ SWSS_LOG_ENTER(); return m_syncdCount; }

/*
* Check if the next hop group is synced or not.
Expand All @@ -67,7 +67,7 @@ class NhgBase
virtual NextHopGroupKey getNhgKey() const = 0;

/* Increment the number of existing groups. */
static inline void incSyncedCount() { SWSS_LOG_ENTER(); ++m_syncedCount; }
static inline void incSyncedCount() { SWSS_LOG_ENTER(); ++m_syncdCount; }

/* Decrement the number of existing groups. */
static void decSyncedCount();
Expand All @@ -83,7 +83,7 @@ class NhgBase
* decremented when an object is removed. This will also account for the
* groups created by RouteOrch.
*/
static unsigned m_syncedCount;
static unsigned m_syncdCount;
};

/*
Expand Down Expand Up @@ -116,34 +116,15 @@ class NhgMember
NhgMember(const NhgMember&) = delete;
void operator=(const NhgMember&) = delete;

virtual ~NhgMember()
{
SWSS_LOG_ENTER();

if (isSynced())
{
SWSS_LOG_ERROR("Deleting next hop group member which is still synced");
assert(false);
}
}

/*
* Sync the NHG member, setting its SAI ID.
*/
virtual void sync(sai_object_id_t gm_id)
{
SWSS_LOG_ENTER();

/*
* The SAI ID should be updated from invalid to something valid.
*/
if ((m_id != SAI_NULL_OBJECT_ID) || (gm_id == SAI_NULL_OBJECT_ID))
{
SWSS_LOG_ERROR("Setting invalid SAI ID %lu to next hop group "
"membeer %s, with current SAI ID %lu",
gm_id, to_string().c_str(), m_id);
throw logic_error("Invalid SAI ID assigned to next hop group member");
}
/* The SAI ID should be updated from invalid to something valid. */
assert((m_gm_id == SAI_NULL_OBJECT_ID) && (gm_id != SAI_NULL_OBJECT_ID));

m_id = gm_id;
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER);
Expand Down Expand Up @@ -388,22 +369,24 @@ class NhgCommon : public NhgBase
};

/*
* Structure describing a next hop group which NhgHandler owns. Beside having
* a next hop group, we also want to keep a ref count so we don't delete
* objects that are still referenced.
* Structure describing a next hop group which NhgOrch owns. Beside having a
* unique pointer to that next hop group, we also want to keep a ref count so
* NhgOrch knows how many other objects reference the next hop group in order
* not to remove them while still being referenced.
*/
template <typename NhgClass>
struct NhgEntry
{
/* The next hop group object in this entry. */
NhgClass nhg;
/* Pointer to the next hop group. NhgOrch is the sole owner of it. */
std::unique_ptr<NhgClass> nhg;

/* Number of external objects referencing this next hop group. */
unsigned ref_count;

NhgEntry() = default;
explicit NhgEntry(NhgClass&& _nhg, unsigned int _ref_count = 0) :
nhg(move(_nhg)), ref_count(_ref_count) { SWSS_LOG_ENTER(); }
explicit NhgEntry(std::unique_ptr<NhgClass>&& _nhg,
unsigned int _ref_count = 0) :
nhg(std::move(_nhg)), ref_count(_ref_count) {}
};

/*
Expand All @@ -419,22 +402,22 @@ class NhgHandlerCommon
inline bool hasNhg(const string &index) const
{
SWSS_LOG_ENTER();
return m_syncedNextHopGroups.find(index) != m_syncedNextHopGroups.end();
return m_syncdNextHopGroups.find(index) != m_syncdNextHopGroups.end();
}

/*
* Get the next hop group with the given index. If the index does not
* exist in the map, a out_of_range eexception will be thrown.
*/
inline const NhgClass& getNhg(const string &index) const
{ SWSS_LOG_ENTER(); return m_syncedNextHopGroups.at(index).nhg; }
{ return *m_syncdNextHopGroups.at(index).nhg; }

/* Increase the ref count for a NHG given by it's index. */
void incNhgRefCount(const string& index)
{
SWSS_LOG_ENTER();

auto& nhg_entry = m_syncedNextHopGroups.at(index);
auto& nhg_entry = m_syncdNextHopGroups.at(index);
++nhg_entry.ref_count;
}

Expand All @@ -443,23 +426,16 @@ class NhgHandlerCommon
{
SWSS_LOG_ENTER();

auto& nhg_entry = m_syncedNextHopGroups.at(index);
auto& nhg_entry = m_syncdNextHopGroups.at(index);

/* Sanity check so we don't overflow. */
if (nhg_entry.ref_count == 0)
{
SWSS_LOG_ERROR("Trying to decrement next hop group %s reference "
"count while none are left.",
nhg_entry.nhg.to_string().c_str());
throw logic_error("Decreasing ref count which is already 0");
}

assert(nhg_entry.ref_count > 0);
--nhg_entry.ref_count;
}

protected:
/*
* Map of synced next hop groups.
*/
unordered_map<string, NhgEntry<NhgClass>> m_syncedNextHopGroups;
unordered_map<string, NhgEntry<NhgClass>> m_syncdNextHopGroups;
};
Loading

0 comments on commit 0ae4eb7

Please sign in to comment.