]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4407: daq: add outstanding packets counter
authorARUNKUMAR KAYAMBU -X (akayambu - XORIANT CORPORATION at Cisco) <akayambu@cisco.com>
Mon, 12 Aug 2024 17:55:58 +0000 (17:55 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Mon, 12 Aug 2024 17:55:58 +0000 (17:55 +0000)
Merge in SNORT/snort3 from ~AKAYAMBU/snort3:outstanding_counter to master

Squashed commit of the following:

commit cf04baa02339a76fdf0f234255815f1a4349bbd7
Author: Arunkumar Kayambu <akayambu@cisco.com>
Date:   Fri Aug 2 05:36:13 2024 -0400

    daq: add outstanding packets counter

src/packet_io/CMakeLists.txt
src/packet_io/sfdaq_module.cc
src/packet_io/test/CMakeLists.txt [new file with mode: 0644]
src/packet_io/test/sfdaq_counters_test.cc [new file with mode: 0644]
src/packet_io/test/sfdaq_module_stubs.h [new file with mode: 0644]

index 4b5a581f107c7daabbe274040fe8d7bc04e4d07b..183fecbdd72a38e65c1df78354667d99a6c7b75f 100644 (file)
@@ -48,6 +48,8 @@ add_library (packet_io OBJECT
     ${TEST_FILES}
 )
 
+add_subdirectory ( test )
+
 install (FILES ${PACKET_IO_INCLUDES}
     DESTINATION "${INCLUDE_INSTALL_PATH}/packet_io"
 )
index 0557d50f34f1efad2dae01ec2ad09e5369722b1d..2db664baae657c4d4b42c186cf350c0a7e0496bb 100644 (file)
@@ -271,8 +271,8 @@ void SFDAQModule::prep_counts(bool dump_stats)
     for ( unsigned i = 0; i < MAX_DAQ_VERDICT; i++ )
         daq_stats.verdicts[i] = daq_stats_delta.verdicts[i];
 
-    daq_stats.outstanding = new_daq_stats.hw_packets_received -
-        new_daq_stats.packets_filtered - new_daq_stats.packets_received;
+    daq_stats.outstanding = new_daq_stats.packets_outstanding;
+
     if ( daq_stats.outstanding > daq_stats.outstanding_max )
         daq_stats.outstanding_max = daq_stats.outstanding;
 
diff --git a/src/packet_io/test/CMakeLists.txt b/src/packet_io/test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..777a08e
--- /dev/null
@@ -0,0 +1,4 @@
+add_cpputest(sfdaq_counters_test
+    SOURCES
+        ../sfdaq_module.cc
+)
diff --git a/src/packet_io/test/sfdaq_counters_test.cc b/src/packet_io/test/sfdaq_counters_test.cc
new file mode 100644 (file)
index 0000000..35f835d
--- /dev/null
@@ -0,0 +1,84 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2024 Cisco and/or its affiliates. All rights reserved.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License Version 2 as published
+// by the Free Software Foundation.  You may not use, modify or distribute
+// this program under any other version of the GNU General Public License.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//--------------------------------------------------------------------------
+// sfdaq_counters_test.cc author Arunkumar Kayambu <akayambu@cisco.com>
+
+// -----------------------------------------------------------------------------
+// unit tests
+// -----------------------------------------------------------------------------
+
+#include "packet_io/sfdaq.h"
+#include "packet_io/sfdaq_module.h"
+#include "sfdaq_module_stubs.h"
+
+#include <CppUTest/CommandLineTestRunner.h>
+#include <CppUTest/TestHarness.h>
+#include <CppUTestExt/MockSupport.h>
+
+using namespace snort;
+
+static DAQ_Stats_t* mock_stats_ptr = nullptr;
+SFDAQInstance *local_instance = nullptr;
+
+const DAQ_Stats_t* SFDAQ::get_stats() {
+    mock().actualCall("get_stats");
+    mock_stats_ptr->packets_outstanding = 20;
+    return mock_stats_ptr;
+}
+
+SFDAQInstance* SFDAQ::get_local_instance() {
+    mock().actualCall("get_local_instance");
+    return local_instance;
+}
+
+TEST_GROUP(sfdaq_module_counters)
+{
+    void setup() {
+        mock_stats_ptr = new DAQ_Stats_t();
+        local_instance = new SFDAQInstance(nullptr, 0, nullptr);
+    }
+
+    void teardown() {
+        mock().clear();
+        delete mock_stats_ptr;
+        delete local_instance;
+    }
+};
+
+TEST(sfdaq_module_counters, check_outstanding_counter)
+{
+    SFDAQModule sfdm;
+    const PegInfo* infos = sfdm.get_pegs();
+
+    // Set up the expectation
+    mock().expectOneCall("get_stats");
+    mock().expectOneCall("get_local_instance");
+
+    sfdm.prep_counts(false);
+    PegCount* p = sfdm.get_counts();
+    mock().checkExpectations();
+    for ( unsigned i = 0; infos[i].name; i++ )
+    {
+        if ( strcmp(infos[i].name, "packets_outstanding") == 0 )
+            CHECK(20 == p[i]);
+    }
+}
+
+int main(int argc, char** argv)
+{
+    return CommandLineTestRunner::RunAllTests(argc, argv);
+}
diff --git a/src/packet_io/test/sfdaq_module_stubs.h b/src/packet_io/test/sfdaq_module_stubs.h
new file mode 100644 (file)
index 0000000..59bc955
--- /dev/null
@@ -0,0 +1,68 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2024 Cisco and/or its affiliates. All rights reserved.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License Version 2 as published
+// by the Free Software Foundation.  You may not use, modify or distribute
+// this program under any other version of the GNU General Public License.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//--------------------------------------------------------------------------
+// sfdaq_module_stubs.h author Arunkumar Kayambu <akayambu@cisco.com>
+
+#include "main/snort.h"
+#include "packet_io/sfdaq_instance.h"
+#include "packet_io/sfdaq_config.h"
+#include "../trough.h"
+
+#ifndef SFDAQ_MODULE_STUBS_H
+#define SFDAQ_MODULE_STUBS_H
+
+namespace snort
+{
+Module::Module(char const*, char const*, snort::Parameter const*, bool)
+{
+    help = nullptr;
+    name = nullptr;
+    params = nullptr;
+    list = false;
+}
+PegCount Module::get_global_count(const char*) const { return 0; }
+void Module::sum_stats(bool) { }
+void Module::init_stats(bool) { }
+void Module::main_accumulate_stats() { }
+void Module::show_interval_stats(std::vector<unsigned>&, FILE*) { }
+void Module::show_stats() { }
+void Module::reset_stats() { }
+void ParseError(char const*, ...)  {}
+SFDAQInstance::SFDAQInstance(char const*, unsigned int, SFDAQConfig const*)
+{
+    batch_size = 0;
+    instance_id = 1;
+    daq_msgs = nullptr;
+}
+SFDAQInstance::~SFDAQInstance() { }
+}
+
+SFDAQConfig::SFDAQConfig()
+{
+   batch_size = 0;
+   mru_size = 0;
+   timeout = 0;
+}
+SFDAQConfig::~SFDAQConfig() = default;
+void SFDAQModuleConfig::set_variable(char const*){}
+void SFDAQConfig::add_module_dir(char const*){}
+void SFDAQConfig::add_input(char const*){}
+void SFDAQConfig::set_mru_size(int){}
+void SFDAQConfig::set_batch_size(unsigned int){}
+void SFDAQConfig::overlay(SFDAQConfig const*){}
+std::atomic<unsigned> Trough::file_count{0};
+#endif