From: Josh Date: Wed, 23 Apr 2014 16:57:36 +0000 (-0400) Subject: statistics framework added. codec api changed. X-Git-Tag: 3.0.0-233~1556 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dd433c8b0da2e101eeb91667260c6497dc259ba6;p=thirdparty%2Fsnort3.git statistics framework added. codec api changed. --- diff --git a/src/codecs/CMakeLists.txt b/src/codecs/CMakeLists.txt index c90bfc973..a041b438a 100644 --- a/src/codecs/CMakeLists.txt +++ b/src/codecs/CMakeLists.txt @@ -29,6 +29,8 @@ add_library( codecs STATIC decode_module.cc codec_api.h codec_api.cc + cd_stats.h + cd_stats.cc ) diff --git a/src/codecs/basic/cd_esp.cc b/src/codecs/basic/cd_esp.cc index cea7def6a..9579b6b30 100644 --- a/src/codecs/basic/cd_esp.cc +++ b/src/codecs/basic/cd_esp.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -25,14 +23,6 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif -#if 0 - -#ifdef HAVE_DUMBNET_H -#include -#else -#include -#endif -#endif #include "framework/codec.h" #include "codecs/codec_events.h" @@ -53,8 +43,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - - virtual void get_protocol_ids(std::vector&); }; @@ -154,9 +142,7 @@ bool EspCodec::decode(const uint8_t *raw_pkt, const uint32_t len, return true; } - - -void EspCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ESP_PROT_ID); } @@ -196,6 +182,8 @@ static const CodecApi esp_api = NULL, // tterm ctor, // ctor dtor, // dtor + NULL, // get_dlt() + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/basic/cd_eth.cc b/src/codecs/basic/cd_eth.cc index 4ea76e65f..cfd61001b 100644 --- a/src/codecs/basic/cd_eth.cc +++ b/src/codecs/basic/cd_eth.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -52,8 +50,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *p, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); - virtual void get_data_link_type(std::vector&); // DELETE #include "codecs/sf_protocols.h" virtual inline PROTO_ID get_proto_id() { return PROTO_ETH; }; @@ -63,7 +59,6 @@ public: } // anonymous namespace - //-------------------------------------------------------------------- // decode.c::Ethernet //-------------------------------------------------------------------- @@ -209,14 +204,11 @@ void Eth_Format (EncodeFlags f, const Packet* p, Packet* c, Layer* lyr) #endif -void EthCodec::get_data_link_type(std::vector&v) +static void get_data_link_type(std::vector&v) { v.push_back(DLT_EN10MB); } -void EthCodec::get_protocol_ids(std::vector& v) -{ -} static Codec* ctor() { @@ -239,8 +231,10 @@ static const CodecApi eth_api = NULL, // tterm ctor, // ctor dtor, // dtor + get_data_link_type, + NULL, + NULL, NULL, - NULL }; const BaseApi* cd_eth = ð_api.base; diff --git a/src/codecs/basic/cd_ipv4.cc b/src/codecs/basic/cd_ipv4.cc index 2aa1d791e..67a6ec47f 100644 --- a/src/codecs/basic/cd_ipv4.cc +++ b/src/codecs/basic/cd_ipv4.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch diff --git a/src/codecs/cd_stats.cc b/src/codecs/cd_stats.cc new file mode 100644 index 000000000..dd498d971 --- /dev/null +++ b/src/codecs/cd_stats.cc @@ -0,0 +1,39 @@ +/* +** Copyright (C) 2002-2013 Sourcefire, Inc. +** Copyright (C) 1998-2002 Martin Roesch +** +** 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. +*/ + +#include "cd_stats.h" +#include "src/thread.h" + +namespace codec_statistics +{ + +static THREAD_LOCAL ProtType curr_type; + + +void set_state(ProtType ct) +{ + curr_type = ct; +} + +ProtType get_state(){ + return curr_type; +} + +} //namespace codec_statistics diff --git a/src/codecs/cd_stats.h b/src/codecs/cd_stats.h new file mode 100644 index 000000000..f6826f72c --- /dev/null +++ b/src/codecs/cd_stats.h @@ -0,0 +1,43 @@ +/* +** Copyright (C) 2002-2013 Sourcefire, Inc. +** Copyright (C) 1998-2002 Martin Roesch +** +** 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. +*/ + +#ifndef CD_STATS_H +#define CD_STATS_H + +namespace codec_statistics +{ + +enum class ProtType{ + PROT_IPV4, + PROT_IPV6, + PROT_IPV6_EXT, + PROT_ICMP4, + PROT_ICMP6, + PROT_UDP, + PROT_TCP, + PROT_GRE, +}; + +void set_state(ProtType); +ProtType get_state(); + +}; // namespace codec_statistics + +#endif diff --git a/src/codecs/plugins/cd_ah.cc b/src/codecs/plugins/cd_ah.cc index 99408e288..c7e14f610 100644 --- a/src/codecs/plugins/cd_ah.cc +++ b/src/codecs/plugins/cd_ah.cc @@ -43,7 +43,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); // DELETE from here and below @@ -76,7 +75,7 @@ bool AhCodec::decode(const uint8_t *raw_pkt, const uint32_t len, -void AhCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(AH_PROT_ID); } @@ -116,6 +115,8 @@ static const CodecApi ah_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_arp.cc b/src/codecs/plugins/cd_arp.cc index 130720dd6..2c38ce179 100644 --- a/src/codecs/plugins/cd_arp.cc +++ b/src/codecs/plugins/cd_arp.cc @@ -42,7 +42,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); // DELETE from here and below @@ -101,7 +100,7 @@ bool ArpCodec::decode(const uint8_t *raw_pkt, const uint32_t len, -void ArpCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERNET_TYPE_ARP); v.push_back(ETHERNET_TYPE_REVARP); @@ -142,6 +141,8 @@ static const CodecApi arp_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, // get_dlt + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_erspan2.cc b/src/codecs/plugins/cd_erspan2.cc index 5e310f334..eda44193a 100644 --- a/src/codecs/plugins/cd_erspan2.cc +++ b/src/codecs/plugins/cd_erspan2.cc @@ -38,8 +38,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - - virtual void get_protocol_ids(std::vector&); // DELETE from here and below #include "codecs/sf_protocols.h" @@ -111,7 +109,7 @@ bool Erspan2Codec::decode(const uint8_t *raw_pkt, const uint32_t len, -void Erspan2Codec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERTYPE_ERSPAN_TYPE2); } @@ -151,6 +149,8 @@ static const CodecApi erspan2_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_erspan3.cc b/src/codecs/plugins/cd_erspan3.cc index 9df6c0bcf..cb0b6da0d 100644 --- a/src/codecs/plugins/cd_erspan3.cc +++ b/src/codecs/plugins/cd_erspan3.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -40,8 +38,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - - virtual void get_protocol_ids(std::vector&); // DELETE from here and below #include "codecs/sf_protocols.h" @@ -114,7 +110,7 @@ bool Erspan3Codec::decode(const uint8_t *raw_pkt, const uint32_t len, } -void Erspan3Codec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERTYPE_ERSPAN_TYPE3); } @@ -154,6 +150,8 @@ static const CodecApi erspan3_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, // get_dlt + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_ethloopback.cc b/src/codecs/plugins/cd_ethloopback.cc index c160238df..aa15a4834 100644 --- a/src/codecs/plugins/cd_ethloopback.cc +++ b/src/codecs/plugins/cd_ethloopback.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -38,8 +36,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); - virtual void get_data_link_type(std::vector&){}; }; @@ -69,7 +65,7 @@ bool EthLoopbackCodec::decode(const uint8_t *raw_pkt, const uint32_t len, //------------------------------------------------------------------------- -void EthLoopbackCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERNET_TYPE_LOOP); } @@ -109,6 +105,8 @@ static const CodecApi ethloopback_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, // get_dlt + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_gre.cc b/src/codecs/plugins/cd_gre.cc index 87634d3ce..49fab3826 100644 --- a/src/codecs/plugins/cd_gre.cc +++ b/src/codecs/plugins/cd_gre.cc @@ -41,10 +41,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); - virtual void get_data_link_type(std::vector&){}; - - // DELETE from here and below #include "codecs/sf_protocols.h" virtual inline PROTO_ID get_proto_id() { return PROTO_GRE; }; @@ -253,7 +249,7 @@ void GRE_Format (EncodeFlags, const Packet*, Packet* c, Layer* lyr) -void GreCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(GRE_PROT_ID); } @@ -293,6 +289,8 @@ static const CodecApi gre_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_mpls.cc b/src/codecs/plugins/cd_mpls.cc index 281b1fbed..49bbbeae7 100644 --- a/src/codecs/plugins/cd_mpls.cc +++ b/src/codecs/plugins/cd_mpls.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -45,9 +43,7 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, - Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); - + Packet *, uint16_t &p_hdr_len, int &next_prot_id); // DELETE from here and below #include "codecs/sf_protocols.h" @@ -258,7 +254,7 @@ static int checkMplsHdr( } -void MplsCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERNET_TYPE_MPLS_UNICAST); v.push_back(ETHERNET_TYPE_MPLS_MULTICAST); @@ -299,6 +295,8 @@ static const CodecApi mpls_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, // get_dlt + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_pppencap.cc b/src/codecs/plugins/cd_pppencap.cc index 775446578..1c57fec31 100644 --- a/src/codecs/plugins/cd_pppencap.cc +++ b/src/codecs/plugins/cd_pppencap.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -45,8 +43,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); - // DELETE from here and below #include "codecs/sf_protocols.h" @@ -173,10 +169,7 @@ bool PppEncap::decode(const uint8_t *raw_pkt, const uint32_t len, return true; } - - - -void PppEncap::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERTYPE_PPP); } @@ -216,6 +209,8 @@ static const CodecApi pppencap_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, // get_dlt + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_pppoepkt.cc b/src/codecs/plugins/cd_pppoepkt.cc index b2a4dfea6..80479a7ca 100644 --- a/src/codecs/plugins/cd_pppoepkt.cc +++ b/src/codecs/plugins/cd_pppoepkt.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -39,7 +37,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); // DELETE from here and below #include "codecs/sf_protocols.h" @@ -286,7 +283,7 @@ EncStatus PPPoE_Encode (EncState* enc, Buffer* in, Buffer* out) } #endif -void PPPoEPkt::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERNET_TYPE_PPPoE_DISC); v.push_back(ETHERNET_TYPE_PPPoE_SESS); @@ -327,6 +324,8 @@ static const CodecApi pppoe_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_transbridge.cc b/src/codecs/plugins/cd_transbridge.cc index f71820aa7..2d7c84f1e 100644 --- a/src/codecs/plugins/cd_transbridge.cc +++ b/src/codecs/plugins/cd_transbridge.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -46,8 +44,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - - virtual void get_protocol_ids(std::vector&); }; @@ -97,7 +93,7 @@ bool TransbridgeCodec::decode(const uint8_t *raw_pkt, const uint32_t len, -void TransbridgeCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERTYPE_TRANS_ETHER_BRIDGING); // defined in ethertypes.h" } @@ -137,6 +133,8 @@ static const CodecApi transbridge_api = NULL, // tterm ctor, // ctor dtor, // dtor + nullptr, + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/codecs/plugins/cd_vlan.cc b/src/codecs/plugins/cd_vlan.cc index 555518392..c9cebfae3 100644 --- a/src/codecs/plugins/cd_vlan.cc +++ b/src/codecs/plugins/cd_vlan.cc @@ -1,5 +1,3 @@ -/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */ - /* ** Copyright (C) 2002-2013 Sourcefire, Inc. ** Copyright (C) 1998-2002 Martin Roesch @@ -54,9 +52,6 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); - virtual void get_data_link_type(std::vector&){}; - // DELETE from here and below #include "codecs/sf_protocols.h" @@ -176,7 +171,7 @@ void VLAN_Format (EncodeFlags, const Packet*, Packet* c, Layer* lyr) -void VlanCodec::get_protocol_ids(std::vector& v) +static void get_protocol_ids(std::vector& v) { v.push_back(ETHERNET_TYPE_8021Q); } @@ -216,6 +211,8 @@ static const CodecApi vlan_api = NULL, // tterm ctor, // ctor dtor, // dtor + NULL, // get_dlt + get_protocol_ids, sum, // sum stats // stats }; diff --git a/src/framework/codec.h b/src/framework/codec.h index 8abecb9f9..0ce33b686 100644 --- a/src/framework/codec.h +++ b/src/framework/codec.h @@ -55,8 +55,6 @@ public: // do nothing unless methods overridden. // ONE OF THESE METHODS MUST BE IMPLEMENTED!! - virtual void get_protocol_ids(std::vector&){}; - virtual void get_data_link_type(std::vector&){}; virtual inline const char* get_name(){return name; }; @@ -80,10 +78,9 @@ typedef Codec* (*cd_new_f)(); typedef void (*cd_del_f)(Codec *); typedef void (*cd_aux_f)(); -typedef void (*cd_get_protos)(std::vector&); -typedef void (*cd_get_dlt)(std::vector&); typedef bool (*decode_f)(const uint8_t *, const uint32_t, Packet *, uint16_t &, uint16_t &); - +typedef void (*cd_dlt_f)(std::vector&v); +typedef void (*cd_prot_id_f)(std::vector&); // add every protocol id, included IP protocols and // ethertypes, to the passed in vector @@ -107,6 +104,9 @@ struct CodecApi cd_new_f ctor; // get eval optional instance data cd_del_f dtor; // clean up instance data + cd_dlt_f dlt; // get the data link type + cd_prot_id_f proto_id; // get the protocol ids + cd_aux_f sum; cd_aux_f stats; }; diff --git a/src/managers/packet_manager.cc b/src/managers/packet_manager.cc index 8a9e7091a..caf88ae2c 100644 --- a/src/managers/packet_manager.cc +++ b/src/managers/packet_manager.cc @@ -1,5 +1,24 @@ +/* +** Copyright (C) 2013-2013 Sourcefire, Inc. +** +** 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. +*/ #include +#include using namespace std; #include "packet_manager.h" @@ -11,33 +30,37 @@ using namespace std; #include "protocols/packet.h" #include "protocols/undefined_protocols.h" +#include "time/profiler.h" -#if 0 -#include "codecs/decode.h" -#include "codecs/essential/root_eth.h" -#include "codecs/root/root_raw4.h" -#include "codecs/root/root_raw6.h" -#include "codecs/root/root_null.h" -#endif -#include "time/profiler.h" +namespace +{ +struct CdGenPegs{ + PegCount total_processed = 0; + PegCount other_codecs = 0; + PegCount discards = 0; +}; + +std::vector CdGenPegNames = +{ + "total", + "other", + "discards" +}; +} // anonymous namespace -//static list s_codecs; -//static THREAD_LOCAL decode_f grinder; #ifdef PERF_PROFILING THREAD_LOCAL PreprocStats decodePerfStats; #endif +static const uint16_t max_protocol_id = 65535; +static std::array s_protocols; +static list s_codecs; +static THREAD_LOCAL CdGenPegs pkt_cnt; +static CdGenPegs gpkt_cnt; -//namespace -//{ - static const uint16_t max_protocol_id = 65535; - static std::array s_protocols; - static list s_codecs; - -//} // namespace //------------------------------------------------------------------------- // plugins @@ -67,6 +90,12 @@ void PacketManager::add_plugin(const CodecApi* api) void PacketManager::release_plugins() { + for ( auto* p : s_codecs ) + { + p->gterm(); + p->tterm(); +// p->dtor(); + } s_codecs.clear(); } @@ -97,72 +126,33 @@ void PacketManager::decode( p->pkt = pkt; len = pkthdr->caplen; curr_prot_id = GRINDER_ID; + pkt_cnt.total_processed++; - // The boolean check in this order so - while(curr_prot_id >= 0 && - curr_prot_id < max_protocol_id && - s_protocols[curr_prot_id] != 0 && - s_protocols[curr_prot_id]->decode(pkt, len, p, p_hdr_len, next_prot_id)) + // loop until the protocol id is no longer valid + while(curr_prot_id >= 0 && curr_prot_id < max_protocol_id) { + if (s_protocols[curr_prot_id] == 0) + { + pkt_cnt.other_codecs++; + break; + } + else if( !s_protocols[curr_prot_id]->decode(pkt, len, p, p_hdr_len, next_prot_id)) + { + pkt_cnt.discards++; + break; + } - - // if we have succesfully decoded this layer, push the layer PacketClass::PushLayer(p, s_protocols[curr_prot_id], pkt, p_hdr_len); curr_prot_id = next_prot_id; len -= p_hdr_len; pkt += p_hdr_len; - } p->dsize = len; p->data = pkt; - PREPROC_PROFILE_END(decodePerfStats); } -#if 0 -const CodecApi *PacketManager::get_data_link_type(int dlt) -{ - vector dlt_vec; - - for ( auto* p : s_codecs ) - { - dlt_vec.clear(); -// p->get_dlt(dlt_vec); - - for (auto *it = dlt_vec.begin(); it != dlt_vec.end(); ++it) - { - if (*it == dlt) - return p; - } - } - - return nullptr; -} - -void PacketManager::set_grinder(void) -{ - const char* slink = NULL; - const char* extra = NULL; - - // initialize values - - int dlt = DAQ_GetBaseProtocol(); - const CodecApi *cd_api = get_data_link_type(dlt); - - if(cd_api != nullptr) - { - grinder = cd_api->ctor(); - - if ( !ScReadMode() || ScPcapShow() ) - LogMessage("Decoding %s\n", slink); - } - - - FatalError("%s(%d) Could not find codec for Data Link Type %d.\n", - __FILE__, __LINE__, dlt); -} -#endif void PacketManager::set_grinder(void) { @@ -188,7 +178,7 @@ void PacketManager::set_grinder(void) proto.clear(); - cd->get_protocol_ids(proto); + p->proto_id(proto); for (auto proto_id : proto) { if(s_protocols[proto_id] != NULL) @@ -203,7 +193,7 @@ void PacketManager::set_grinder(void) dlt.clear(); - cd->get_data_link_type(dlt); + p->dlt(dlt); // set the grinder if the data link types match for (auto curr_dlt : dlt ) { @@ -222,21 +212,55 @@ void PacketManager::set_grinder(void) if (!codec_registered) WarningMessage("The Codec %s is never used\n", cd->get_name()); - // ERRRO: If multiple correct grinders found. } - - - -// FatalError("Codec installation checking!!"); } void PacketManager::dump_stats() { -// for ( auto* cd : s_codecs ) -// cd->sum(); + sum_stats((PegCount*)&gpkt_cnt, (PegCount*)&pkt_cnt, array_size(CdGenPegNames)); + + for ( auto* cd : s_codecs ) + if (cd->sum != nullptr) + cd->sum(); + + std::vector pegNames(CdGenPegNames); + std::vector pegs; + pegs.push_back(gpkt_cnt.total_processed); + pegs.push_back(gpkt_cnt.other_codecs); + pegs.push_back(gpkt_cnt.discards); + + // using two temporary vectors to ensure codecs cannot + // see any other codecs statistics + std::vector tmpNames; + std::vector tmpPegs; + + for ( auto* cd : s_codecs ) + { + if (cd->stats != nullptr) + { + tmpPegs.clear(); + tmpNames.clear(); +// cd->stats(tmpPegs, tmpNames); + if (tmpNames.size() == tmpPegs.size()) + { + pegs.insert(pegs.end(), tmpPegs.begin(), tmpPegs.end()); + pegNames.insert(pegNames.end(), tmpNames.begin(), tmpNames.end()); + } + else + { + WarningMessage("The %s Codecs stats function returned a " + "different %d PegCounts and %d PegNames. the two " + "values must be equal\n", + cd->base.name, pegs.size(), pegNames.size()); + } + } + } + show_percent_stats(&pegs[0], &pegNames[0], pegNames.size(), + "codecs"); } + bool PacketManager::has_codec(uint16_t cd_id) { return s_protocols[cd_id] != 0; diff --git a/src/managers/packet_manager.h b/src/managers/packet_manager.h index bc3668ffa..8a4626f1d 100644 --- a/src/managers/packet_manager.h +++ b/src/managers/packet_manager.h @@ -23,6 +23,7 @@ #include "snort_types.h" #include "framework/codec.h" #include "time/profiler.h" +#include "utils/stats.h" #include #include @@ -50,15 +51,12 @@ public: static void init_codecs(); static void dump_stats(); - static bool has_codec(uint16_t); // static void encode_update(Packet *); // static void encode_format(Packet *); private: - static const CodecApi *get_data_link_type(int dlt); - };