From: ARUNKUMAR KAYAMBU -X (akayambu - XORIANT CORPORATION at Cisco) Date: Mon, 12 Aug 2024 17:55:58 +0000 (+0000) Subject: Pull request #4407: daq: add outstanding packets counter X-Git-Tag: 3.3.3.0~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61385fdd32e0de90b9718825c7cccc66a1c94302;p=thirdparty%2Fsnort3.git Pull request #4407: daq: add outstanding packets counter Merge in SNORT/snort3 from ~AKAYAMBU/snort3:outstanding_counter to master Squashed commit of the following: commit cf04baa02339a76fdf0f234255815f1a4349bbd7 Author: Arunkumar Kayambu Date: Fri Aug 2 05:36:13 2024 -0400 daq: add outstanding packets counter --- diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index 4b5a581f1..183fecbdd 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -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" ) diff --git a/src/packet_io/sfdaq_module.cc b/src/packet_io/sfdaq_module.cc index 0557d50f3..2db664baa 100644 --- a/src/packet_io/sfdaq_module.cc +++ b/src/packet_io/sfdaq_module.cc @@ -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 index 000000000..777a08e06 --- /dev/null +++ b/src/packet_io/test/CMakeLists.txt @@ -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 index 000000000..35f835d3b --- /dev/null +++ b/src/packet_io/test/sfdaq_counters_test.cc @@ -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 + +// ----------------------------------------------------------------------------- +// unit tests +// ----------------------------------------------------------------------------- + +#include "packet_io/sfdaq.h" +#include "packet_io/sfdaq_module.h" +#include "sfdaq_module_stubs.h" + +#include +#include +#include + +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 index 000000000..59bc95544 --- /dev/null +++ b/src/packet_io/test/sfdaq_module_stubs.h @@ -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 + +#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&, 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 Trough::file_count{0}; +#endif