]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4065: appid: TP_appid profiler
authorLukasz Czarnik -X (lczarnik - SOFTSERVE INC at Cisco) <lczarnik@cisco.com>
Mon, 27 Nov 2023 18:09:41 +0000 (18:09 +0000)
committerChris Sherwin (chsherwi) <chsherwi@cisco.com>
Mon, 27 Nov 2023 18:09:41 +0000 (18:09 +0000)
Merge in SNORT/snort3 from ~LCZARNIK/snort3:tp_appid_profiler to master

Squashed commit of the following:

commit be69c7d14d16fe44a508dc8587176ced4f00e13f
Author: Lukasz Czarnik <lczarnik@cisco.com>
Date:   Tue Nov 7 08:18:01 2023 -0500

    appid: Adding support for memory profiling of third party lib

    appid: Adds missed cppcheck warning

15 files changed:
src/network_inspectors/appid/CMakeLists.txt
src/network_inspectors/appid/appid_inspector.cc
src/network_inspectors/appid/appid_module.cc
src/network_inspectors/appid/appid_module.h
src/network_inspectors/appid/client_plugins/client_app_msn.cc
src/network_inspectors/appid/detector_plugins/test/detector_plugins_mock.h
src/network_inspectors/appid/test/CMakeLists.txt
src/network_inspectors/appid/test/appid_discovery_test.cc
src/network_inspectors/appid/test/appid_mock_inspector.h
src/network_inspectors/appid/test/tp_lib_handler_test.cc
src/network_inspectors/appid/test/tp_mock.cc
src/network_inspectors/appid/tp_appid_module_api.cc [new file with mode: 0644]
src/network_inspectors/appid/tp_appid_module_api.h
src/network_inspectors/appid/tp_lib_handler.cc
src/network_inspectors/appid/tp_lib_handler.h

index af177e5d2447bed6b9bc557545831348f5160cc6..d386787d00e13be8f7d038f6f87de195d5ba045f 100644 (file)
@@ -204,6 +204,9 @@ set ( APPID_SOURCES
     tp_lib_handler.cc
     tp_lib_handler.h
     tp_appid_types.h
+    tp_appid_session_api.h
+    tp_appid_module_api.h
+    tp_appid_module_api.cc
 )
 
 #if (STATIC_INSPECTORS)
index 626db65fb851189219b5a3033da59efd94b64d03..4cdb0c5d7e22939cd54bebcf5a375f5e09149fdf 100644 (file)
@@ -115,7 +115,8 @@ AppIdContext& AppIdInspector::get_ctxt() const
 bool AppIdInspector::configure(SnortConfig* sc)
 {
     assert(!ctxt);
-
+    // cppcheck-suppress unreadVariable
+    Profile profile(appid_perf_stats);
     struct rusage ru;
     long prev_maxrss = -1;
     #ifdef REG_TEST
index d2571cbf4cdc3cd3b452925dd785199493f31a2a..6097be65daedd375d25033061eac6d0c212323cd 100644 (file)
@@ -61,6 +61,7 @@ THREAD_LOCAL const Trace* appid_trace = nullptr;
 //-------------------------------------------------------------------------
 
 THREAD_LOCAL ProfileStats appid_perf_stats;
+THREAD_LOCAL ProfileStats tp_appid_perf_stats;
 THREAD_LOCAL AppIdStats appid_stats;
 THREAD_LOCAL bool ThirdPartyAppIdContext::tp_reload_in_progress = false;
 
@@ -491,9 +492,25 @@ const TraceOption* AppIdModule::get_trace_options() const
     return &appid_trace_options;
 }
 
-ProfileStats* AppIdModule::get_profile() const
+
+
+snort::ProfileStats* AppIdModule::get_profile(
+        unsigned i, const char*& name, const char*& parent) const
 {
-    return &appid_perf_stats;
+    switch (i) 
+    {
+    
+        case 0:
+            name = get_name();
+            parent = nullptr;
+            return &appid_perf_stats;
+
+        case 1:
+            name = "tp_appid";
+            parent = get_name();
+            return &tp_appid_perf_stats;
+    }
+    return nullptr;
 }
 
 const AppIdConfig* AppIdModule::get_data()
index 1e1b6061511d7d29ef4be428ad0bcb0eaebd7be4..36315545aa79d4827ccb9bf67872572bce23ef4e 100644 (file)
@@ -40,6 +40,7 @@ class Trace;
 }
 
 extern THREAD_LOCAL snort::ProfileStats appid_perf_stats;
+extern THREAD_LOCAL snort::ProfileStats tp_appid_perf_stats;
 extern THREAD_LOCAL const snort::Trace* appid_trace;
 
 #define MOD_NAME "appid"
@@ -85,7 +86,8 @@ public:
     const snort::Command* get_commands() const override;
     const PegInfo* get_pegs() const override;
     PegCount* get_counts() const override;
-    snort::ProfileStats* get_profile() const override;
+    snort::ProfileStats* get_profile(
+        unsigned i, const char*& name, const char*& parent) const override;
 
     const AppIdConfig* get_data();
 
index f8e0115fc551b70c8c9342231006ad66e32e0813..d8b6b38acca3aa947a094fc11b04c467af2e3a8f 100644 (file)
@@ -115,6 +115,7 @@ int MsnClientDetector::validate(AppIdDiscoveryArgs& args)
 
             args.data++; /* skip the space */
         }
+        // cppcheck-suppress knownConditionTrueFalse
         else if ( end - args.data >= (int)sizeof(MSMSGS) &&
             memcmp(args.data, MSMSGS, sizeof(MSMSGS)-1) == 0 )
         {
index 0e1d3bf6dd0a8903e225fd4dbb7f0de02406d7d9..aca770abe9113ce4b156a0845c0d5bf53557d50f 100644 (file)
@@ -140,7 +140,8 @@ PegCount* AppIdModule::get_counts() const
     return nullptr;
 }
 
-snort::ProfileStats* AppIdModule::get_profile() const
+snort::ProfileStats* AppIdModule::get_profile(
+        unsigned, const char*&, const char*&) const
 {
     return nullptr;
 }
index 794da44a64dfcd0655c8470b89a03a81a3ec1fab..0eebbf372aff1e424896688265dea09d14a4b2ed 100644 (file)
@@ -50,6 +50,7 @@ add_cpputest( tp_lib_handler_test
         tp_lib_handler_test.cc
         ../../../network_inspectors/rna/test/rna_flow_stubs.cc
         ../tp_lib_handler.cc
+        ../tp_appid_module_api.cc
     LIBS
         dl
 )
index b95616f589692571a34fd556ab014b71b9dc8904..ad88c46a59291ab84b2ed1e24e9a7a77b08f8d97 100644 (file)
@@ -169,7 +169,8 @@ bool AppIdModule::set(char const*, Value&, SnortConfig*) { return true; }
 const Command* AppIdModule::get_commands() const { return nullptr; }
 const PegInfo* AppIdModule::get_pegs() const { return nullptr; }
 PegCount* AppIdModule::get_counts() const { return nullptr; }
-ProfileStats* AppIdModule::get_profile() const { return nullptr; }
+ProfileStats* AppIdModule::get_profile(
+        unsigned i, const char*& name, const char*& parent) const { return nullptr; }
 void AppIdModule::set_trace(const Trace*) const { }
 const TraceOption* AppIdModule::get_trace_options() const { return nullptr; }
 THREAD_LOCAL bool ThirdPartyAppIdContext::tp_reload_in_progress = false;
index 19f255ee3faa818ec9cf5207ae2ecdcb85dddb92..f0d976ea2dd9ce16c63df83097fa094c8de7aac5 100644 (file)
@@ -56,7 +56,8 @@ bool AppIdModule::set(char const*, snort::Value&, snort::SnortConfig*) { return
 const snort::Command* AppIdModule::get_commands() const { return nullptr; }
 const PegInfo* AppIdModule::get_pegs() const { return nullptr; }
 PegCount* AppIdModule::get_counts() const { return nullptr; }
-snort::ProfileStats* AppIdModule::get_profile() const { return nullptr; }
+snort::ProfileStats* AppIdModule::get_profile(
+        unsigned, const char*&, const char*& ) const { return nullptr; }
 void AppIdModule::set_trace(const Trace*) const { }
 const TraceOption* AppIdModule::get_trace_options() const { return nullptr; }
 
index 5788a00716c8c066686753f61442b2e126fd8cd8..f6d9748f823536253255df164dfbfdff15fd3d17 100644 (file)
@@ -35,6 +35,7 @@
 #include <CppUTest/TestHarness.h>
 
 using namespace std;
+using namespace snort;
 
 static TPLibHandler* tph = nullptr;
 static AppIdConfig config;
@@ -71,6 +72,14 @@ int ServiceDiscovery::add_service_port(AppIdDetector*, const ServiceDetectorPort
 { return 0; }
 void appid_log(const snort::Packet*, unsigned char, char const*, ...) { }
 
+
+THREAD_LOCAL ProfileStats tp_appid_perf_stats;
+THREAD_LOCAL bool TimeProfilerStats::enabled = false;
+MemoryContext::MemoryContext(MemoryTracker&) { }
+MemoryContext::~MemoryContext() = default;
+THREAD_LOCAL TimeContext* ProfileContext::curr_time = nullptr;
+
+
 TEST_GROUP(tp_lib_handler)
 {
 };
index 9bbc57b94534ea7995dc2d6932bbe1fb7242502a..ff3615eeda412e0a5313adca5142e119d7bdea42 100644 (file)
@@ -48,19 +48,24 @@ public:
         : ThirdPartyAppIdContext(ver, mname, config)
     {
         cerr << WhereMacro << endl;
+        // For tp_appid profiler coverage
+        data = (int*)cfg.tp_appid_profiler_functions.appid_malloc(sizeof(int));
     }
 
     ~ThirdPartyAppIdContextImpl() override
     {
         cerr << WhereMacro << endl;
+        // For tp_appid profiler coverage
+        cfg.tp_appid_profiler_functions.appid_free(data);
     }
 
-    int tinit() override { return 0; }
-    bool tfini(bool) override { return false; }
+    int tinit() override {return 0;}
+    bool tfini(bool) override {return false;}
     const string& get_user_config() const override { return user_config; }
 
 private:
     const string user_config = "";
+    int* data;
 };
 
 class ThirdPartyAppIdSessionImpl : public ThirdPartyAppIdSession
diff --git a/src/network_inspectors/appid/tp_appid_module_api.cc b/src/network_inspectors/appid/tp_appid_module_api.cc
new file mode 100644 (file)
index 0000000..bf318d6
--- /dev/null
@@ -0,0 +1,30 @@
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "tp_appid_module_api.h"
+#include "managers/module_manager.h"
+#include "appid_module.h"
+#include "tp_lib_handler.h"
+
+
+static void* tp_appid_profiler_malloc(size_t size)
+{
+    // cppcheck-suppress unreadVariable
+    snort::Profile profile(tp_appid_perf_stats);
+    return operator new(size);
+}
+
+static void tp_appid_profiler_free(void* p)
+{
+    // cppcheck-suppress unreadVariable
+    snort::Profile profile(tp_appid_perf_stats);
+    if (p)
+        operator delete(p);
+}
+
+TPAppidProfilerFunctions get_tp_appid_profiler_functions()
+{
+    return {tp_appid_profiler_malloc,tp_appid_profiler_free};
+}
index ed5e166581d724d43d9073fed775d866c6bd05e6..33d9cddaf312306f5de98a12f65a4ba43072f542 100644 (file)
 
 #include <vector>
 #include <string>
+
 #include "main/thread.h"
+#include "profiler/profiler_defs.h"
 #include "tp_appid_types.h"
 
 #define THIRD_PARTY_APPID_API_VERSION 6
 
+struct TPAppidProfilerFunctions
+{
+    void* (*appid_malloc) (size_t);
+    void (*appid_free) (void*);
+};
+
 class ThirdPartyConfig
 {
 public:
@@ -38,6 +46,7 @@ public:
     std::string tp_appid_config;
     bool tp_appid_stats_enable = false;
     bool tp_appid_config_dump = false;
+    TPAppidProfilerFunctions tp_appid_profiler_functions = {};
 };
 
 class ThirdPartyAppIdContext
index 64cf7eb7ac3bd977d02a4b435811bc029068b0e1..468eebc73d5a841cab38be14cc9667504768b880 100644 (file)
@@ -103,6 +103,7 @@ ThirdPartyAppIdContext* TPLibHandler::create_tp_appid_ctxt(const AppIdConfig& co
     tp_config.chp_body_collection_disabled =
         odp_ctxt.chp_body_collection_disabled;
     tp_config.tp_allow_probes = odp_ctxt.tp_allow_probes;
+    tp_config.tp_appid_profiler_functions = get_tp_appid_profiler_functions();
 
     ThirdPartyAppIdContext* tp_appid_ctxt = self->tp_appid_create_ctxt(tp_config);
     if (tp_appid_ctxt == nullptr)
index b355564c90490b4872a7d73ccc21e1e602f556c5..059bde0c938c430308d5741a7c7dd2216d08e146 100644 (file)
@@ -70,4 +70,6 @@ private:
     bool load_callback(const char* path);
 };
 
+TPAppidProfilerFunctions get_tp_appid_profiler_functions();
+
 #endif