Skip to content

Commit 2529c7f

Browse files
author
Damir Zainullin
committed
++
1 parent c81fb8d commit 2529c7f

File tree

7 files changed

+296
-76
lines changed

7 files changed

+296
-76
lines changed

new-process-api/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SRC := main.cpp \
66
$(wildcard process/common/*/*.cpp) \
77
$(wildcard process/*/src/*.cpp)
88

9-
INCLUDE := -I. -I../include/ipfixprobe/pluginFactory -Iprocess/common
9+
INCLUDE := -I. -I../include/ipfixprobe/pluginFactory -Iprocess/common -I../include
1010

1111
OBJ := $(SRC:.cpp=.o)
1212

new-process-api/packet.hpp

Lines changed: 156 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,170 @@
1-
#pragma once
1+
/**
2+
* \file packet.hpp
3+
* \brief Structs/classes for communication between packet reader and flow cache
4+
* \author Vaclav Bartos <[email protected]>
5+
* \author Jiri Havranek <[email protected]>
6+
* \date 2014
7+
* \date 2015
8+
* \date 2016
9+
*/
10+
/*
11+
* Copyright (C) 2014-2016 CESNET
12+
*
13+
* LICENSE TERMS
14+
*
15+
* Redistribution and use in source and binary forms, with or without
16+
* modification, are permitted provided that the following conditions
17+
* are met:
18+
* 1. Redistributions of source code must retain the above copyright
19+
* notice, this list of conditions and the following disclaimer.
20+
* 2. Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimer in
22+
* the documentation and/or other materials provided with the
23+
* distribution.
24+
* 3. Neither the name of the Company nor the names of its contributors
25+
* may be used to endorse or promote products derived from this
26+
* software without specific prior written permission.
27+
*
28+
*
29+
*
30+
*/
231

3-
#include <cstdint>
32+
#ifndef IPXP_PACKET_HPP
33+
#define IPXP_PACKET_HPP
34+
35+
#include <ipfixprobe/flowifc.hpp>
36+
#include <ipfixprobe/ipaddr.hpp>
37+
#include <stdint.h>
38+
#include <stdlib.h>
439
#include <sys/time.h>
5-
#include <span>
6-
#include <optional>
740

8-
#include "directionalField.hpp"
9-
#include "tcpData.hpp"
10-
#include "flowKey.hpp"
41+
namespace ipxp {
42+
43+
/**
44+
* \brief Structure for storing parsed packet fields
45+
*/
46+
struct Packet : public Record {
47+
struct timeval ts;
48+
49+
uint8_t dst_mac[6];
50+
uint8_t src_mac[6];
51+
uint16_t ethertype;
52+
53+
uint16_t ip_len; /**< Length of IP header + its payload */
54+
uint16_t tcp_window;
55+
uint64_t tcp_options;
56+
uint32_t tcp_mss;
57+
uint16_t ip_payload_len; /**< Length of IP payload */
58+
uint8_t ip_version;
59+
uint8_t ip_ttl;
60+
uint8_t ip_proto;
61+
uint8_t ip_tos;
62+
uint8_t ip_flags;
63+
ipaddr_t src_ip;
64+
ipaddr_t dst_ip;
65+
uint32_t vlan_id;
66+
uint32_t frag_id;
67+
uint16_t frag_off;
68+
bool more_fragments;
1169

12-
namespace ipxp
13-
{
70+
uint16_t src_port;
71+
uint16_t dst_port;
72+
uint8_t tcp_flags;
73+
uint16_t tcp_window;
74+
uint64_t tcp_options;
75+
uint32_t tcp_mss;
76+
uint32_t tcp_seq;
77+
uint32_t tcp_ack;
1478

15-
struct Packet {
16-
FlowKey flowKey{};
79+
/**
80+
* @brief The top level mpls
81+
*
82+
* Contents are (from MSb to LSb):
83+
* 20-bit - Label,
84+
* 3-bit - Traffic class / EXP,
85+
* 1-bit - Bottom of stack (true if last),
86+
* 8-bit - TTL (Time To Live)
87+
*/
88+
uint32_t mplsTop;
1789

18-
uint64_t timestamp{0};
19-
uint8_t ipTTL{0};
20-
uint8_t ipFlags{0};
21-
uint16_t ipLength{0};
90+
const uint8_t* packet; /**< Pointer to begin of packet, if available */
91+
uint16_t packet_len; /**< Length of data in packet buffer, packet_len <= packet_len_wire */
92+
uint16_t packet_len_wire; /**< Original packet length on wire */
2293

23-
std::optional<TCPData> tcpData{std::nullopt};
94+
const uint8_t* payload; /**< Pointer to begin of payload, if available */
95+
uint16_t payload_len; /**< Length of data in payload buffer, payload_len <= payload_len_wire */
96+
uint16_t payload_len_wire; /**< Original payload length computed from headers */
2497

25-
uint32_t realLength{0};
26-
//uint32_t receivedLength{0};
98+
uint8_t* custom; /**< Pointer to begin of custom data, if available */
99+
uint16_t custom_len; /**< Length of data in custom buffer */
27100

28-
Direction direction{Direction::Forward};
101+
// TODO REMOVE
102+
uint8_t* buffer; /**< Buffer for packet, payload and custom data */
103+
uint16_t buffer_size; /**< Size of buffer */
29104

30-
std::span<const std::byte> payload;
31-
std::optional<uint32_t> mplsTopLabel;
32-
std::optional<uint16_t> vlanId;
105+
bool source_pkt; /**< Direction of packet from flow point of view */
33106

107+
/**
108+
* \brief Constructor.
109+
*/
110+
Packet()
111+
: ts({0, 0})
112+
, dst_mac()
113+
, src_mac()
114+
, ethertype(0)
115+
, ip_len(0)
116+
, ip_payload_len(0)
117+
, ip_version(0)
118+
, ip_ttl(0)
119+
, ip_proto(0)
120+
, ip_tos(0)
121+
, ip_flags(0)
122+
, src_ip({0})
123+
, dst_ip({0})
124+
, vlan_id(0)
125+
, frag_id(0)
126+
, frag_off(0)
127+
, more_fragments(false)
128+
, src_port(0)
129+
, dst_port(0)
130+
, tcp_flags(0)
131+
, tcp_window(0)
132+
, tcp_options(0)
133+
, tcp_mss(0)
134+
, tcp_seq(0)
135+
, tcp_ack(0)
136+
, mplsTop(0)
137+
, packet(nullptr)
138+
, packet_len(0)
139+
, packet_len_wire(0)
140+
, payload(nullptr)
141+
, payload_len(0)
142+
, payload_len_wire(0)
143+
, custom(nullptr)
144+
, custom_len(0)
145+
, buffer(nullptr)
146+
, buffer_size(0)
147+
, source_pkt(true)
148+
{
149+
}
34150
};
35151

36-
struct PacketFeatures {};
152+
struct PacketBlock {
153+
Packet* pkts;
154+
size_t cnt;
155+
size_t bytes;
156+
size_t size;
157+
158+
PacketBlock(size_t pkts_size)
159+
: cnt(0)
160+
, bytes(0)
161+
, size(pkts_size)
162+
{
163+
pkts = new Packet[pkts_size];
164+
}
165+
166+
~PacketBlock() { delete[] pkts; }
167+
};
37168

38-
} // namespace ipxp
169+
} // namespace ipxp
170+
#endif /* IPXP_PACKET_HPP */

new-process-api/packetNew.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <sys/time.h>
5+
#include <span>
6+
#include <optional>
7+
8+
#include "directionalField.hpp"
9+
#include "tcpData.hpp"
10+
#include "flowKey.hpp"
11+
12+
namespace ipxp
13+
{
14+
15+
struct Packet {
16+
FlowKey flowKey{};
17+
18+
uint64_t timestamp{0};
19+
uint8_t ipTTL{0};
20+
uint8_t ipFlags{0};
21+
uint16_t ipLength{0};
22+
23+
std::optional<TCPData> tcpData{std::nullopt};
24+
25+
uint32_t realLength{0};
26+
//uint32_t receivedLength{0};
27+
28+
Direction direction{Direction::Forward};
29+
30+
std::span<const std::byte> payload;
31+
std::optional<uint32_t> mplsTopLabel;
32+
std::optional<uint16_t> vlanId;
33+
34+
};
35+
36+
struct PacketFeatures {};
37+
38+
} // namespace ipxp

new-process-api/process/pstats/src/packetStats.cpp

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
namespace ipxp {
2525

26-
2726
static const PluginManifest packetStatsPluginManifest = {
2827
.name = "pstats",
2928
.description = "Pstats process plugin for computing packet bursts stats.",
@@ -36,15 +35,7 @@ static const PluginManifest packetStatsPluginManifest = {
3635
},
3736
};
3837

39-
const inline std::vector<FieldPair<PacketStatsFields>> fields = {
40-
{PacketStatsFields::PPI_PKT_LENGTHS, "PPI_PKT_LENGTHS"},
41-
{PacketStatsFields::PPI_PKT_TIMES, "PPI_PKT_TIMES"},
42-
{PacketStatsFields::PPI_PKT_FLAGS, "PPI_PKT_FLAGS"},
43-
{PacketStatsFields::PPI_PKT_DIRECTIONS, "PPI_PKT_DIRECTIONS"},
44-
};
45-
46-
47-
static FieldSchema createPacketStatsSchema(FieldManager& manager, FieldHandlers<PacketStatsFields>& handlers) noexcept
38+
static void createPacketStatsSchema(FieldManager& manager, FieldHandlers<PacketStatsFields>& handlers) noexcept
4839
{
4940
FieldSchema schema = fieldManager.createFieldSchema("pstats");
5041

@@ -58,15 +49,12 @@ static FieldSchema createPacketStatsSchema(FieldManager& manager, FieldHandlers<
5849
}));
5950
handlers.insert(PacketStatsFields::PPI_PKT_DIRECTIONS, schema.addVectorField(
6051
"PPI_PKT_DIRECTIONS",
61-
FieldDirection::DirectionalIndifferent,
6252
[](const void* context) { return getSpan(reinterpret_cast<const PacketStatsExport*>(context)->directions);
6353
}));
6454
handlers.insert(PacketStatsFields::PPI_PKT_TIMES, schema.addVectorField(
6555
"PPI_PKT_TIMES",
6656
[](const void* context) {return toSpan(reinterpret_cast<const PacketStatsExport*>(context)->timestamps);
6757
}));
68-
69-
return schema;
7058
}
7159

7260
PacketStatsPlugin::PacketStatsPlugin([[maybe_unused]]const std::string& params, FieldManager& manager)
@@ -100,9 +88,8 @@ PluginUpdateResult PacketStatsPlugin::onUpdate(const FlowContext& flowContext, v
10088
PluginExportResult PacketStatsPlugin::onExport(const FlowRecord& flowRecord, void* pluginContext)
10189
{
10290
const std::size_t packetsTotal
103-
= flowRecord.dataForward.packets + flowRecord.dataReverse.packets;
91+
= flowRecord.src_packets + flowRecord.dst_packets;
10492

105-
constexpr static std::size_t MIN_FLOW_LENGTH = 1;
10693
if (packetsTotal <= MIN_FLOW_LENGTH) {
10794
return {
10895
.flowAction = FlowAction::RemovePlugin,
@@ -125,32 +112,31 @@ bool isSequenceOverflowed(const uint32_t currentValue, const uint32_t prevValue)
125112
constexpr int64_t MAX_DIFF = static_cast<int64_t>(
126113
static_cast<double>(std::numeric_limits<uint32_t>::max()) / 100);
127114

128-
return static_cast<int64_t>(currentValue)
129-
- static_cast<int64_t>(prevValue) < -MAX_DIFF;
115+
return static_cast<int64_t>(prevValue)
116+
- static_cast<int64_t>(currentValue) > MAX_DIFF
130117
}
131118

132119
static
133120
bool isDuplicate(const Packet& packet, const PacketStatsData& pluginData) noexcept
134121
{
135-
// TODO USE VALUES FROM DISSECTOR
136122
constexpr std::size_t TCP = 6;
137-
if (packet.flowKey.l4Protocol != TCP) {
123+
if (packet.ip_proto != TCP) {
138124
return false;
139125
}
140126

141127
// Current seq <= previous ack?
142128
const bool suspiciousSequence
143-
= packet.tcpData->sequence <= pluginData.processingState.lastSequence[packet.direction]
144-
&& !isSequenceOverflowed(packet.tcpData->sequence, pluginData.processingState.lastSequence[packet.direction]);
129+
= packet.tcp_seq <= pluginData.processingState.lastSequence[packet.source_pkt]
130+
&& !isSequenceOverflowed(packet.tcp_seq, pluginData.processingState.lastSequence[packet.source_pkt]);
145131

146132
// Current ack <= previous ack?
147133
const bool suspiciousAcknowledgment
148-
= packet.tcpData->acknowledgment <= pluginData.processingState.lastAcknowledgment[packet.direction]
149-
&& !isSequenceOverflowed(packet.tcpData->acknowledgment, pluginData.processingState.lastAcknowledgment[packet.direction]);
134+
= packet.tcp_ack <= pluginData.processingState.lastAcknowledgment[packet.source_pkt]
135+
&& !isSequenceOverflowed(packet.tcp_ack, pluginData.processingState.lastAcknowledgment[packet.source_pkt]);
150136

151137
if (suspiciousSequence && suspiciousAcknowledgment
152-
&& packet.payload.size() == pluginData.processingState.lastLength[packet.direction]
153-
&& packet.tcpData->flags == pluginData.processingState.lastFlags[packet.direction]
138+
&& packet.payload_len == pluginData.processingState.lastLength[packet.source_pkt]
139+
&& packet.tcp_flags == pluginData.processingState.lastFlags[packet.source_pkt]
154140
&& pluginData.lengths.size() != 0) {
155141
return true;
156142
}
@@ -160,38 +146,34 @@ bool isDuplicate(const Packet& packet, const PacketStatsData& pluginData) noexce
160146

161147
void PacketStatsPlugin::updatePacketsData(const Packet& packet, PacketStatsData& pluginData) noexcept
162148
{
163-
if (!packet.tcpData.has_value()) {
164-
return;
165-
}
166-
167149
if (m_skipDuplicates && isDuplicate(packet, pluginData)) {
168150
return;
169151
}
170152

171-
pluginData.processingState.lastSequence[packet.direction] = packet.tcpData->sequence;
172-
pluginData.processingState.lastAcknowledgment[packet.direction] = packet.tcpData->acknowledgment;
173-
pluginData.processingState.lastLength[packet.direction] = packet.realLength;
174-
pluginData.processingState.lastFlags[packet.direction] = packet.tcpData->flags;
153+
pluginData.processingState.lastSequence[packet.source_pkt] = packet.tcp_seq;
154+
pluginData.processingState.lastAcknowledgment[packet.source_pkt] = packet.tcp_ack;
155+
pluginData.processingState.lastLength[packet.source_pkt] = packet.payload_len;
156+
pluginData.processingState.lastFlags[packet.source_pkt] = TcpFlags(packet.tcp_flags);
175157

176-
if (packet.realLength == 0 && !m_countEmptyPackets) {
158+
if (packet.packet_len == 0 && !m_countEmptyPackets) {
177159
return;
178160
}
179161

180162
if (pluginData.lengths.size() == pluginData.lengths.capacity()) {
181163
return;
182164
}
183165

184-
pluginData.lengths.push_back(static_cast<uint16_t>(packet.realLength));
166+
pluginData.lengths.push_back(static_cast<uint16_t>(packet.payload_len_wire));
185167

186-
pluginData.tcpFlags.push_back(packet.tcpData->flags);
187-
188-
pluginData.timestamps.push_back(packet.timestamp);
168+
pluginData.tcpFlags.push_back(TcpFlags(packet.tcp_flags));
169+
170+
pluginData.timestamps.push_back(packet.ts);
189171

190172
/*
191173
* direction = 1 iff client -> server
192174
* direction = -1 iff server -> client
193175
*/
194-
const int8_t direction = packet.direction ? 1 : -1;
176+
const int8_t direction = packet.source_pkt ? 1 : -1;
195177
pluginData.directions.push_back(direction);
196178
}
197179

0 commit comments

Comments
 (0)