Skip to content

Commit 507d68d

Browse files
authored
Simplify TcpReassembly connection purge to use multimap. (#2104)
* Simplify TcpReassembly connection purge to use multimap instead of map<K, list<V>>. The change removes the need to create a list for situations where only 1 connection will be purged at time T. Instead the additional flow keys at time T will be directly inserted into the multimap as additional nodes. * Rename CleanupList to CleanupMultimap * Rename `insertIntoCleanupList` to `scheduleCleanup`. * Simplify insert to emplace. * Lint
1 parent 060df2a commit 507d68d

2 files changed

Lines changed: 23 additions & 32 deletions

File tree

Packet++/header/TcpReassembly.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@ namespace pcpp
460460
};
461461

462462
using ConnectionList = std::unordered_map<uint32_t, TcpReassemblyData>;
463-
using CleanupList = std::map<time_t, std::list<uint32_t>>;
463+
using CleanupMultiMap = std::multimap<time_t, uint32_t>;
464464

465465
OnTcpMessageReady m_OnMessageReadyCallback;
466466
OnTcpConnectionStart m_OnConnStart;
467467
OnTcpConnectionEnd m_OnConnEnd;
468468
void* m_UserCookie;
469469
ConnectionList m_ConnectionList;
470470
ConnectionInfoList m_ConnectionInfo;
471-
CleanupList m_CleanupList;
471+
CleanupMultiMap m_CleanupMultimap;
472472
bool m_RemoveConnInfo;
473473
uint32_t m_ClosedConnectionDelay;
474474
uint32_t m_MaxNumToClean;
@@ -483,7 +483,7 @@ namespace pcpp
483483

484484
void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
485485

486-
void insertIntoCleanupList(uint32_t flowKey);
486+
void scheduleCleanup(uint32_t flowKey);
487487
};
488488

489489
} // namespace pcpp

Packet++/src/TcpReassembly.cpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ namespace pcpp
751751
m_OnConnEnd(tcpReassemblyData.connData, reason, m_UserCookie);
752752

753753
tcpReassemblyData.closed = true; // mark the connection as closed
754-
insertIntoCleanupList(flowKey);
754+
scheduleCleanup(flowKey);
755755

756756
PCPP_LOG_DEBUG("Connection with flow key 0x" << std::hex << flowKey << " is closed");
757757
}
@@ -781,7 +781,7 @@ namespace pcpp
781781
m_OnConnEnd(tcpReassemblyData.connData, TcpReassemblyConnectionClosedManually, m_UserCookie);
782782

783783
tcpReassemblyData.closed = true; // mark the connection as closed
784-
insertIntoCleanupList(flowKey);
784+
scheduleCleanup(flowKey);
785785

786786
PCPP_LOG_DEBUG("Connection with flow key 0x" << std::hex << flowKey << " is closed");
787787
}
@@ -796,18 +796,16 @@ namespace pcpp
796796
return -1;
797797
}
798798

799-
void TcpReassembly::insertIntoCleanupList(uint32_t flowKey)
799+
void TcpReassembly::scheduleCleanup(uint32_t flowKey)
800800
{
801-
// m_CleanupList is a map with key of type time_t (expiration time). The mapped type is a list that stores the
802-
// flow keys to be cleared in certain point of time. m_CleanupList.insert inserts an empty list if the container
803-
// does not already contain an element with an equivalent key, otherwise this method returns an iterator to the
804-
// element that prevents insertion.
805-
std::pair<CleanupList::iterator, bool> pair =
806-
m_CleanupList.insert(std::make_pair(time(nullptr) + m_ClosedConnectionDelay, CleanupList::mapped_type()));
807-
808-
// getting the reference to list
809-
CleanupList::mapped_type& keysList = pair.first->second;
810-
keysList.push_front(flowKey);
801+
// m_CleanupMultimap is a multimap with key of type time_t (expiration time) and a value type uint32_t (flow
802+
// key). Due to being a multimap, multiple flow keys can have the same expiration time.
803+
//
804+
// During purge, up to 'maxNumToClean' entries with expiration time smaller than the current time will be
805+
// removed from storage.
806+
807+
auto expireTime = time(nullptr) + m_ClosedConnectionDelay;
808+
m_CleanupMultimap.emplace(expireTime, flowKey);
811809
}
812810

813811
uint32_t TcpReassembly::purgeClosedConnections(uint32_t maxNumToClean)
@@ -817,25 +815,18 @@ namespace pcpp
817815
if (maxNumToClean == 0)
818816
maxNumToClean = m_MaxNumToClean;
819817

820-
CleanupList::iterator iterTime = m_CleanupList.begin(), iterTimeEnd = m_CleanupList.upper_bound(time(nullptr));
821-
while (iterTime != iterTimeEnd && count < maxNumToClean)
822-
{
823-
CleanupList::mapped_type& keysList = iterTime->second;
818+
auto it = m_CleanupMultimap.begin();
819+
auto itEnd = m_CleanupMultimap.upper_bound(time(nullptr));
824820

825-
for (; !keysList.empty() && count < maxNumToClean; ++count)
826-
{
827-
CleanupList::mapped_type::const_reference key = keysList.front();
828-
m_ConnectionInfo.erase(key);
829-
m_ConnectionList.erase(key);
830-
keysList.pop_front();
831-
}
832-
833-
if (keysList.empty())
834-
m_CleanupList.erase(iterTime++);
835-
else
836-
++iterTime;
821+
for (; it != itEnd && count < maxNumToClean; ++it, ++count)
822+
{
823+
auto flowKey = it->second;
824+
m_ConnectionInfo.erase(flowKey);
825+
m_ConnectionList.erase(flowKey);
837826
}
838827

828+
m_CleanupMultimap.erase(m_CleanupMultimap.begin(), it);
829+
839830
return count;
840831
}
841832

0 commit comments

Comments
 (0)