Skip to content

Commit 2e321ba

Browse files
committed
Improve performance of IpfixDecoder.
Copying the functionality of DecodeBuffer directly to IpfixDecoder greatly improves performance of the IpfixDecoder.
1 parent c93f1ca commit 2e321ba

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/plugins/input/tcp/src/IpfixDecoder.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,65 @@
1010

1111
#include "IpfixDecoder.hpp"
1212

13+
#include <stdexcept>
14+
1315
#include "DecodeBuffer.hpp" // DecodeBuffer
1416
#include "Reader.hpp"
1517

1618
namespace tcp_in {
1719

1820
void IpfixDecoder::progress() {
19-
m_decoded.read_from(m_reader);
21+
// Directly calling `m_decoded.read_from` is somehow much slower than copying the code directly
22+
// here.
23+
24+
while (!m_decoded.enough_data()) {
25+
if (!read_header()) {
26+
break;
27+
}
28+
29+
if (!read_body()) {
30+
break;
31+
}
32+
}
33+
}
34+
35+
bool IpfixDecoder::read_header() {
36+
if (m_decoded_size != 0) {
37+
return true;
38+
}
39+
40+
if (!read_until_n(sizeof(fds_ipfix_msg_hdr))) {
41+
return false;
42+
}
43+
44+
45+
// The header has been successfully read. Load the size of the message.
46+
auto hdr = reinterpret_cast<const fds_ipfix_msg_hdr *>(m_part_decoded.data());
47+
m_decoded_size = ntohs(hdr->length);
48+
49+
if (m_decoded_size < sizeof(fds_ipfix_msg_hdr)) {
50+
throw std::runtime_error("Invalid IPFIX message header size.");
51+
}
52+
53+
return true;
54+
}
55+
56+
bool IpfixDecoder::read_body() {
57+
// Read the body
58+
if (!read_until_n(m_decoded_size)) {
59+
// There is not enough data to read the whole body.
60+
return false;
61+
}
62+
63+
m_decoded.add(std::move(m_part_decoded));
64+
m_part_decoded = ByteVector();
65+
m_decoded_size = 0;
66+
return true;
67+
}
68+
69+
bool IpfixDecoder::read_until_n(size_t n) {
70+
m_reader.read_until_n(n, m_part_decoded, m_decoded);
71+
return m_part_decoded.size() == n;
2072
}
2173

2274
} // namespace tcp_in

src/plugins/input/tcp/src/IpfixDecoder.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class IpfixDecoder : public Decoder {
4040
}
4141

4242
private:
43+
bool read_header();
44+
bool read_body();
45+
bool read_until_n(size_t n);
46+
47+
ByteVector m_part_decoded;
48+
size_t m_decoded_size = 0;
49+
4350
TcpReader m_reader;
4451
DecodeBuffer m_decoded;
4552
};

0 commit comments

Comments
 (0)