Skip to content

Commit fd6c83f

Browse files
committed
parser-stats - add parser stats to input plugins
1 parent 19c87d0 commit fd6c83f

File tree

8 files changed

+40
-10
lines changed

8 files changed

+40
-10
lines changed

include/ipfixprobe/input.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ class InputPlugin : public TelemetryUtils, public Plugin
7373
std::shared_ptr<telemetry::Directory> plugin_dir,
7474
std::shared_ptr<telemetry::Directory> queues_dir) {};
7575

76+
ParserStats m_parser_stats;
77+
7678
private:
7779
void create_parser_stats_telemetry(std::shared_ptr<telemetry::Directory> queues_dir);
78-
79-
ParserStats m_parser_stats;
8080
};
8181

8282
}

input/dpdk-ring.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ InputPlugin::Result DpdkRingReader::get(PacketBlock& packets)
179179
}
180180
for (auto i = 0; i < pkts_read_; i++) {
181181
parse_packet(&opt,
182+
m_parser_stats,
182183
getTimestamp(mbufs_[i]),
183184
rte_pktmbuf_mtod(mbufs_[i], const std::uint8_t*),
184185
rte_pktmbuf_data_len(mbufs_[i]),

input/dpdk.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ InputPlugin::Result DpdkReader::get(PacketBlock& packets)
278278
packets.cnt++;
279279
#else
280280
parse_packet(&opt,
281+
m_parser_stats,
281282
dpdkDevice.getPacketTimestamp(mBufs[packetID]),
282283
rte_pktmbuf_mtod(mBufs[packetID], const std::uint8_t*),
283284
rte_pktmbuf_data_len(mBufs[packetID]),

input/ndp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ InputPlugin::Result NdpPacketReader::get(PacketBlock &packets)
107107
throw PluginError(ndpReader.error_msg);
108108
}
109109
read_pkts++;
110-
parse_packet(&opt, timestamp, ndp_packet->data, ndp_packet->data_length, ndp_packet->data_length);
110+
parse_packet(&opt, m_parser_stats, timestamp, ndp_packet->data, ndp_packet->data_length, ndp_packet->data_length);
111111
}
112112

113113
m_seen += read_pkts;

input/parser.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ inline uint16_t process_pppoe(const u_char *data_ptr, uint16_t data_len, Packet
640640
return length;
641641
}
642642

643-
void parse_packet(parser_opt_t *opt, struct timeval ts, const uint8_t *data, uint16_t len, uint16_t caplen)
643+
void parse_packet(parser_opt_t *opt, ParserStats& stats, struct timeval ts, const uint8_t *data, uint16_t len, uint16_t caplen)
644644
{
645645
if (opt->pblock->cnt >= opt->pblock->size) {
646646
return;
@@ -672,6 +672,8 @@ void parse_packet(parser_opt_t *opt, struct timeval ts, const uint8_t *data, uin
672672
pkt->tcp_mss = 0;
673673
pkt->mplsTop = 0;
674674

675+
stats.seen_packets++;
676+
675677
uint32_t l3_hdr_offset = 0;
676678
uint32_t l4_hdr_offset = 0;
677679
try {
@@ -697,6 +699,7 @@ void parse_packet(parser_opt_t *opt, struct timeval ts, const uint8_t *data, uin
697699

698700
if (pkt->ethertype == ETH_P_TRILL) {
699701
data_offset += parse_trill(data + data_offset, caplen - data_offset, pkt);
702+
stats.trill_packets++;
700703
data_offset += parse_eth_hdr(data + data_offset, caplen - data_offset, pkt);
701704
}
702705
l3_hdr_offset = data_offset;
@@ -706,24 +709,39 @@ void parse_packet(parser_opt_t *opt, struct timeval ts, const uint8_t *data, uin
706709
data_offset += parse_ipv6_hdr(data + data_offset, caplen - data_offset, pkt);
707710
} else if (pkt->ethertype == ETH_P_MPLS_UC || pkt->ethertype == ETH_P_MPLS_MC) {
708711
data_offset += process_mpls(data + data_offset, caplen - data_offset, pkt);
712+
stats.mpls_packets++;
709713
} else if (pkt->ethertype == ETH_P_PPP_SES) {
710714
data_offset += process_pppoe(data + data_offset, caplen - data_offset, pkt);
715+
stats.pppoe_packets++;
711716
} else if (!opt->parse_all) {
717+
stats.unknown_packets++;
712718
DEBUG_MSG("Unknown ethertype %x\n", pkt->ethertype);
713719
return;
714720
}
715721

716722
l4_hdr_offset = data_offset;
717723
if (pkt->ip_proto == IPPROTO_TCP) {
718724
data_offset += parse_tcp_hdr(data + data_offset, caplen - data_offset, pkt);
725+
stats.tcp_packets++;
719726
} else if (pkt->ip_proto == IPPROTO_UDP) {
720727
data_offset += parse_udp_hdr(data + data_offset, caplen - data_offset, pkt);
728+
stats.udp_packets++;
721729
}
722730
} catch (const char *err) {
723731
DEBUG_MSG("%s\n", err);
724732
return;
725733
}
726734

735+
if (pkt->vlan_id) {
736+
stats.vlan_packets++;
737+
}
738+
739+
if (pkt->ethertype == ETH_P_IP) {
740+
stats.ipv4_packets++;
741+
} else if (pkt->ethertype == ETH_P_IPV6) {
742+
stats.ipv6_packets++;
743+
}
744+
727745
uint16_t pkt_len = caplen;
728746
pkt->packet = data;
729747
pkt->packet_len = caplen;

input/parser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define IPXP_INPUT_PARSER_HPP
3131

3232
#include <ipfixprobe/packet.hpp>
33+
#include <ipfixprobe/parser-stats.hpp>
3334

3435
#ifdef WITH_PCAP
3536
#include <pcap/pcap.h>
@@ -65,7 +66,7 @@ typedef struct parser_opt_s {
6566
int datalink;
6667
} parser_opt_t;
6768

68-
void parse_packet(parser_opt_t *opt, struct timeval ts, const uint8_t *data, uint16_t len, uint16_t caplen);
69+
void parse_packet(parser_opt_t *opt, ParserStats& stats, struct timeval ts, const uint8_t *data, uint16_t len, uint16_t caplen);
6970

7071
}
7172
#endif /* IPXP_INPUT_PARSER_HPP */

input/pcap.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ namespace ipxp {
4141
// Read only 1 packet into packet block
4242
constexpr size_t PCAP_PACKET_BLOCK_SIZE = 1;
4343

44+
struct UserData {
45+
parser_opt_t* opt;
46+
ParserStats& stats;
47+
};
48+
4449
__attribute__((constructor)) static void register_this_plugin()
4550
{
4651
static PluginRecord rec = PluginRecord("pcap", [](){return new PcapReader();});
@@ -49,12 +54,14 @@ __attribute__((constructor)) static void register_this_plugin()
4954

5055
/**
5156
* \brief Parsing callback function for pcap_dispatch() call. Parse packets up to transport layer.
52-
* \param [in,out] arg Serves for passing pointer to Packet structure into callback function.
57+
* \param [in,out] arg Serves for passing pointer to Packet user data structure into callback function.
5358
* \param [in] h Contains timestamp and packet size.
5459
* \param [in] data Pointer to the captured packet data.
5560
*/
5661
void packet_handler(u_char *arg, const struct pcap_pkthdr *h, const u_char *data)
5762
{
63+
UserData *user_data = reinterpret_cast<UserData *>(arg);
64+
5865
#ifdef __CYGWIN__
5966
// WinPcap, uses Microsoft's definition of struct timeval, which has `long` data type
6067
// used for both tv_sec and tv_usec and has 32 bit even on 64 bit platform.
@@ -64,9 +71,9 @@ void packet_handler(u_char *arg, const struct pcap_pkthdr *h, const u_char *data
6471
new_h.ts.tv_usec = *(reinterpret_cast<const uint32_t *>(h) + 1);
6572
new_h.caplen = *(reinterpret_cast<const uint32_t *>(h) + 2);
6673
new_h.len = *(reinterpret_cast<const uint32_t *>(h) + 3);
67-
parse_packet((parser_opt_t *) arg, new_h.ts, data, new_h.len, new_h.caplen);
74+
parse_packet(user_data->opt, user_data->stats, new_h.ts, data, new_h.len, new_h.caplen);
6875
#else
69-
parse_packet((parser_opt_t *) arg, h->ts, data, h->len, h->caplen);
76+
parse_packet(user_data->opt, user_data->stats, h->ts, data, h->len, h->caplen);
7077
#endif
7178
}
7279

@@ -249,12 +256,14 @@ InputPlugin::Result PcapReader::get(PacketBlock &packets)
249256
parser_opt_t opt = {&packets, false, false, m_datalink};
250257
int ret;
251258

259+
UserData user_data = {&opt, m_parser_stats};
260+
252261
if (m_handle == nullptr) {
253262
throw PluginError("no interface capture or file opened");
254263
}
255264

256265
packets.cnt = 0;
257-
ret = pcap_dispatch(m_handle, PCAP_PACKET_BLOCK_SIZE, packet_handler, (u_char *) (&opt));
266+
ret = pcap_dispatch(m_handle, PCAP_PACKET_BLOCK_SIZE, packet_handler, (u_char *) (&user_data));
258267
if (m_live) {
259268
if (ret == 0) {
260269
return Result::TIMEOUT;

input/raw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ int RawReader::process_packets(struct tpacket_block_desc *pbd, PacketBlock &pack
309309
size_t snaplen = ppd->tp_snaplen;
310310
struct timeval ts = {ppd->tp_sec, ppd->tp_nsec / 1000};
311311

312-
parse_packet(&opt, ts, data, len, snaplen);
312+
parse_packet(&opt, m_parser_stats, ts, data, len, snaplen);
313313
ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
314314
}
315315
m_last_ppd = ppd;

0 commit comments

Comments
 (0)