]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4225: flow: updated flow_data linklist with STL container
authorRaza Shafiq (rshafiq) <rshafiq@cisco.com>
Tue, 27 Feb 2024 18:24:51 +0000 (18:24 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Tue, 27 Feb 2024 18:24:51 +0000 (18:24 +0000)
Merge in SNORT/snort3 from ~RSHAFIQ/snort3:master_flow_data to master

Squashed commit of the following:

commit 3e6805d43c0eb9da2a94820da3fc86ec94b1d80f
Author: rshafiq <rshafiq@cisco.com>
Date:   Thu Feb 22 15:08:43 2024 -0500

    flow: updated flow_data linklist with STL container

src/flow/flow.cc
src/flow/flow.h
src/network_inspectors/appid/appid_session.cc

index bb1b297a2ed9bd1498d2d09e2d44971e23bcb36a..cba3e0264d385d82778f3519465cc69c729b210a 100644 (file)
@@ -201,68 +201,40 @@ void Flow::trust()
 
 int Flow::set_flow_data(FlowData* fd)
 {
-    FlowData* old = get_flow_data(fd->get_id());
-    assert(old != fd);
-
-    if (old)
-        free_flow_data(old);
-
-    fd->prev = nullptr;
-    fd->next = flow_data;
-
-    if ( flow_data )
-        flow_data->prev = fd;
-
-    flow_data = fd;
+    if ( !fd ) return -1;
+
+    current_flow_data = fd;
+    uint32_t id = fd->get_id();
+    // operator[] will create a new entry if it does not exist
+    // or replace the existing one if it does
+    // when replacing, the old entry is deleted
+    flow_data[id] = std::unique_ptr<FlowData>(fd);
     return 0;
 }
 
+
 FlowData* Flow::get_flow_data(unsigned id) const
 {
-    FlowData* fd = flow_data;
-
-    while (fd)
-    {
-        if (fd->get_id() == id)
-            return fd;
-
-        fd = fd->next;
-    }
+    auto it = flow_data.find(id);
+    if ( it != flow_data.end() )
+        return it->second.get();
     return nullptr;
 }
 
-// FIXIT-L: implement doubly linked list with STL to cut down on code we maintain
 void Flow::free_flow_data(FlowData* fd)
 {
-    if ( fd == flow_data )
-    {
-        flow_data = fd->next;
-        if ( flow_data )
-            flow_data->prev = nullptr;
-    }
-    else if ( !fd->next )
-    {
-        fd->prev->next = nullptr;
-    }
-    else
-    {
-        fd->prev->next = fd->next;
-        fd->next->prev = fd->prev;
-    }
-    delete fd;
+    if ( !fd ) return;
+    flow_data.erase(fd->get_id());
 }
 
 void Flow::free_flow_data(uint32_t proto)
 {
-    FlowData* fd = get_flow_data(proto);
-
-    if ( fd )
-        free_flow_data(fd);
+    flow_data.erase(proto);
 }
 
 void Flow::free_flow_data()
 {
-    if (!flow_data)
+    if ( flow_data.empty() )
         return;
     const SnortConfig* sc = SnortConfig::get_conf();
     PolicySelector* ps = sc->policy_map->get_policy_selector();
@@ -291,12 +263,7 @@ void Flow::free_flow_data()
         }
     }
 
-    while (flow_data)
-    {
-        FlowData* tmp = flow_data;
-        flow_data = flow_data->next;
-        delete tmp;
-    }
+    flow_data.clear();
 
     if (ps)
     {
@@ -308,16 +275,13 @@ void Flow::free_flow_data()
 
 void Flow::call_handlers(Packet* p, bool eof)
 {
-    FlowData* fd = flow_data;
-
-    while (fd)
+    for (auto& fd_pair : flow_data)
     {
+        FlowData* fd = fd_pair.second.get(); 
         if ( eof )
             fd->handle_eof(p);
         else
             fd->handle_retransmit(p);
-
-        fd = fd->next;
     }
 }
 
index 29fcca07eca42afa58663bef8b222ae1af804010..3fd020c34860f3b4002f5fa45e1389113fae18b0 100644 (file)
@@ -418,6 +418,8 @@ public:  // FIXIT-M privatize if possible
     // fields are organized by initialization and size to minimize
     // void space
 
+    std::unordered_map<uint32_t, std::unique_ptr<FlowData>> flow_data;
+
     DeferredTrust deferred_trust;
 
     const FlowKey* key = nullptr;
@@ -441,7 +443,7 @@ public:  // FIXIT-M privatize if possible
     Layer mpls_server = {};
 
     IpsContextChain context_chain;
-    FlowData* flow_data = nullptr;
+    FlowData* current_flow_data = nullptr;
     FlowStats flowstats = {};
     StreamFlowIntf* stream_intf = nullptr;
 
index ec85268e3c7ec8b79bd875075823fdaadbca9df5..9b7927431a7e03e9af4d322507eeaf91a1163cda 100644 (file)
@@ -222,7 +222,7 @@ AppIdSession* AppIdSession::create_future_session(const Packet* ctrlPkt, const S
     char src_ip[INET6_ADDRSTRLEN];
     char dst_ip[INET6_ADDRSTRLEN];
 
-    AppIdInspector* inspector = (AppIdInspector*)ctrlPkt->flow->flow_data->get_handler();
+    AppIdInspector* inspector = (AppIdInspector*)ctrlPkt->flow->current_flow_data->get_handler();
     if ((inspector == nullptr) or strcmp(inspector->get_name(), MOD_NAME))
         inspector = (AppIdInspector*)InspectorManager::get_inspector(MOD_NAME, true);