Skip to content

Commit 294f2f4

Browse files
Zainullin DamirZainullin Damir
authored andcommitted
++
1 parent 2e60280 commit 294f2f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+671
-364
lines changed

external/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ include(ExternalProject)
44
set(FETCHCONTENT_QUIET OFF)
55

66
include(telemetry.cmake)
7+
include(adaptmon.cmake)

external/adaptmon.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Telemetry library (C++ library for telemetry data collection with Fuse integration)
2+
#
3+
# The Telemetry library consists of two libraries that can be added as dependency:
4+
#
5+
# - telemetry::telemetry (C++ library for telemetry data collection)
6+
# - telemetry::appFs (C++ library that expose telemetry data as a Fuse filesystem)
7+
8+
#set(TELEMETRY_BUILD_SHARED OFF)
9+
#set(TELEMETRY_INSTALL_TARGETS OFF)
10+
#set(TELEMETRY_PACKAGE_BUILDER OFF)
11+
#set(TELEMETRY_ENABLE_TESTS OFF)
12+
13+
#set(CMAKE_POSITION_INDEPENDENT_CODE ON)
14+
15+
set(GIT_REPO https://gitlab.liberouter.org/monitoring/adaptmon.git)
16+
17+
FetchContent_Declare(
18+
adaptmon
19+
GIT_REPOSITORY ${GIT_REPO}
20+
GIT_TAG main
21+
)
22+
23+
# Make sure that subproject accepts predefined build options without warnings.
24+
#set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
25+
26+
set(FETCHCONTENT_BASE_DIR "${CMAKE_SOURCE_DIR}/.deps")
27+
set(FETCHCONTENT_FULLY_DISCONNECTED OFF)
28+
29+
FetchContent_MakeAvailable(adaptmon)
30+
#include_directories(${adaptmon_SOURCE_DIR}/lib/include/public/amon)

include/ipfixprobe/processPlugin/flowRecord.hpp

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "directionalField.hpp"
44
#include "flowKey.hpp"
55
#include "macAddress.hpp"
6-
#include "packet.hpp"
76
#include "tcpFlags.hpp"
87
#include "timestamp.hpp"
98
#include "xxhash.h"
@@ -19,6 +18,13 @@
1918
#include <memory>
2019
#include <span>
2120

21+
#include <amon/Packet.hpp>
22+
#include <amon/layers/Ethernet.hpp>
23+
#include <amon/layers/IPv4.hpp>
24+
#include <amon/layers/IPv6.hpp>
25+
#include <amon/layers/TCP.hpp>
26+
#include <amon/layers/UDP.hpp>
27+
2228
namespace ipxp {
2329

2430
static constexpr std::size_t MAX_PLUGIN_COUNT = 32;
@@ -61,11 +67,11 @@ class FlowRecord {
6167
public:
6268
uint64_t hash;
6369

64-
Timestamp timeCreation;
65-
Timestamp timeLastUpdate;
70+
amon::types::Timestamp timeCreation;
71+
amon::types::Timestamp timeLastUpdate;
6672

6773
FlowKey flowKey;
68-
DirectionalField<MACAddress> macAddress;
74+
DirectionalField<amon::types::MACAddress> macAddress;
6975

7076
DirectionalField<DirectionalData> directionalData;
7177

@@ -123,39 +129,66 @@ class FlowRecord {
123129
}*/
124130

125131
// TODO remove hashval
126-
void createFrom(const Packet& packet, uint64_t hashval)
132+
void createFrom(const amon::Packet& packet, uint64_t hashval)
127133
{
128134
directionalData[Direction::Forward].packets = 1;
129-
directionalData[Direction::Forward].bytes = packet.ip_len;
130135

131-
flowKey.srcPort = packet.src_port;
132-
flowKey.dstPort = packet.dst_port;
133-
flowKey.l4Protocol = packet.ip_proto;
136+
const std::optional<amon::EthernetView> ethernetView
137+
= packet.getLayerView<amon::EthernetView>();
138+
if (!ethernetView.has_value()) {
139+
return;
140+
}
141+
142+
macAddress = {ethernetView->src(), ethernetView->dst()};
143+
144+
if (const std::optional<amon::layers::IPv4View> ipv4View
145+
= packet.getLayerView<amon::layers::IPv4View>();
146+
ipv4View.has_value()) {
147+
flowKey.srcIp = ipv4View->srcIP();
148+
flowKey.dstIp = ipv4View->dstIP();
149+
flowKey.l4Protocol = ipv4View->protocol();
150+
directionalData[Direction::Forward].bytes = ipv4View->totalLength();
151+
} else if (const std::optional<amon::IPv6View> ipv6View
152+
= packet.getLayerView<amon::IPv6View>();
153+
ipv6View.has_value()) {
154+
flowKey.srcIp = ipv6View->srcIP();
155+
flowKey.dstIp = ipv6View->dstIP();
156+
flowKey.l4Protocol = ipv6View->nextHeader();
157+
directionalData[Direction::Forward].bytes = ipv6View->payloadLength();
158+
} else {
159+
return;
160+
}
134161

135162
hash = hashval;
136163

137-
timeCreation = packet.ts;
138-
timeLastUpdate = packet.ts;
164+
timeCreation = packet.timestamp;
165+
timeLastUpdate = packet.timestamp;
166+
167+
if (const std::optional<amon::TCPView> tcpView = packet.getLayerView<amon::TCPView>();
168+
tcpView.has_value()) {
169+
flowKey.srcPort = tcpView->srcPort();
170+
flowKey.dstPort = tcpView->dstPort();
171+
directionalData[Direction::Forward].bytes += tcpView->headerLength();
172+
directionalData[Direction::Forward].tcpFlags = tcpView->flags();
173+
} else if (const std::optional<amon::UDPView> udpView
174+
= packet.getLayerView<amon::UDPView>();
175+
udpView.has_value()) {
176+
flowKey.srcPort = udpView->srcPort();
177+
flowKey.dstPort = udpView->dstPort();
178+
directionalData[Direction::Forward].bytes += udpView->headerLength();
179+
} else {
180+
flowKey.srcPort = 0;
181+
flowKey.dstPort = 0;
182+
}
139183

140-
macAddress[Direction::Forward]
184+
/*macAddress[Direction::Forward]
141185
= std::span<const std::byte, 6>(reinterpret_cast<const std::byte*>(packet.src_mac), 6);
142186
macAddress[Direction::Reverse]
143187
= std::span<const std::byte, 6>(reinterpret_cast<const std::byte*>(packet.dst_mac), 6);
144-
145-
if (packet.ip_version == IP::v4) {
146-
flowKey.srcIp = packet.src_ip.v4;
147-
flowKey.dstIp = packet.dst_ip.v4;
148-
} else if (packet.ip_version == IP::v6) {
149-
flowKey.srcIp = std::span<const std::byte, 16>(
150-
reinterpret_cast<const std::byte*>(packet.src_ip.v6),
151-
16);
152-
flowKey.dstIp = std::span<const std::byte, 16>(
153-
reinterpret_cast<const std::byte*>(packet.dst_ip.v6),
154-
16);
155-
}
188+
*/
156189
}
157190

158-
void update(const Packet& packet, bool src)
191+
void update(const amon::Packet& packet, bool src)
159192
{
160193
/*m_flow.time_last = pkt.ts;
161194
if (src) {

include/ipfixprobe/processPlugin/ipAddress.hpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#pragma once
22

3-
#include <iostream>
3+
#include <algorithm>
44
#include <array>
5+
#include <compare>
56
#include <cstdint>
67
#include <cstring>
7-
#include <limits>
8-
#include <compare>
9-
#include <algorithm>
108
#include <format>
9+
#include <iostream>
10+
#include <limits>
1111

1212
#include <ipfixprobe/ipaddr.hpp>
1313

@@ -19,10 +19,7 @@ union IPAddress {
1919
std::array<uint32_t, 4> u32;
2020
std::array<uint64_t, 2> u64;
2121

22-
constexpr IPAddress() noexcept
23-
{
24-
std::memset(&u8, 0, sizeof(IPAddress));
25-
}
22+
constexpr IPAddress() noexcept { std::memset(&u8, 0, sizeof(IPAddress)); }
2623

2724
constexpr IPAddress(const uint32_t ipv4) noexcept
2825
{
@@ -37,85 +34,85 @@ union IPAddress {
3734
}
3835

3936
constexpr IPAddress(const ipaddr_t address, IP version) noexcept
40-
: IPAddress(address.v4)
37+
: IPAddress(address.v4)
4138
{
4239
if (version == IP::v6) {
4340
std::memcpy(&u8, address.v6, 16);
4441
}
4542
}
4643

47-
constexpr IPAddress(const IPAddress& other) noexcept
44+
// TODO remove. Added because of AMON compatibility
45+
constexpr IPAddress(const auto& container)
4846
{
49-
u8 = other.u8;
50-
}
47+
if (container.size() != 4 && container.size() != 16) {
48+
throw std::invalid_argument("IPAddress: container must have size 4 or 16");
49+
}
5150

52-
constexpr bool isIPv4() const noexcept
53-
{
54-
return u32[1] == 0 &&
55-
u32[2] == std::numeric_limits<uint32_t>::max() &&
56-
u32[3] == std::numeric_limits<uint32_t>::max();
51+
std::memcpy(&u8, container.data(), container.size());
52+
if (container.size() == 4) {
53+
u32[1] = 0;
54+
u32[2] = u32[3] = std::numeric_limits<uint32_t>::max();
55+
}
5756
}
5857

59-
constexpr bool isIPv6() const noexcept
58+
constexpr IPAddress(const IPAddress& other) noexcept { u8 = other.u8; }
59+
60+
constexpr bool isIPv4() const noexcept
6061
{
61-
return !isIPv4();
62+
return u32[1] == 0 && u32[2] == std::numeric_limits<uint32_t>::max()
63+
&& u32[3] == std::numeric_limits<uint32_t>::max();
6264
}
6365

64-
//constexpr bool operator==(const IPAddress&) const noexcept = default;
66+
constexpr bool isIPv6() const noexcept { return !isIPv4(); }
6567

68+
// constexpr bool operator==(const IPAddress&) const noexcept = default;
6669

67-
constexpr auto operator<=>(const IPAddress& other) const noexcept
68-
{
69-
return u64 <=> other.u64;
70-
}
70+
constexpr auto operator<=>(const IPAddress& other) const noexcept { return u64 <=> other.u64; }
7171

72-
constexpr bool operator==(const IPAddress& other) const noexcept
73-
{
74-
return u64 == other.u64;
75-
}
72+
constexpr bool operator==(const IPAddress& other) const noexcept { return u64 == other.u64; }
7673

77-
//constexpr std::strong_ordering operator<=>(const IPAddress& other) const noexcept = default;
74+
// constexpr std::strong_ordering operator<=>(const IPAddress& other) const noexcept = default;
7875

7976
constexpr IPAddress& operator=(const IPAddress& other) noexcept
8077
{
81-
if (this != &other) {
78+
if (this != &other) {
8279
u8 = other.u8;
8380
}
8481

8582
return *this;
8683
}
8784

88-
constexpr std::size_t size() const noexcept
89-
{
90-
return isIPv4() ? 4 : 16;
91-
}
85+
constexpr std::size_t size() const noexcept { return isIPv4() ? 4 : 16; }
9286

93-
std::string toString() const noexcept
87+
std::string toString() const noexcept
9488
{
9589
std::string res;
9690
constexpr std::size_t MAX_IP_AS_TEXT_SIZE = 30;
9791
res.reserve(MAX_IP_AS_TEXT_SIZE);
9892

9993
if (isIPv4()) {
100-
std::for_each_n(reinterpret_cast<const std::byte*>(u8.data()), size(),
101-
[&](const std::byte ipByte) {
102-
std::format_to(std::back_inserter(res), "{}.", static_cast<int>(ipByte));
103-
});
94+
std::for_each_n(
95+
reinterpret_cast<const std::byte*>(u8.data()),
96+
size(),
97+
[&](const std::byte ipByte) {
98+
std::format_to(std::back_inserter(res), "{}.", static_cast<int>(ipByte));
99+
});
104100
} else {
105-
std::for_each_n(reinterpret_cast<const std::byte*>(u8.data()), size(),
106-
[&](const std::byte ipByte) {
107-
std::format_to(std::back_inserter(res), "{:02x}:", static_cast<int>(ipByte));
108-
});
101+
std::for_each_n(
102+
reinterpret_cast<const std::byte*>(u8.data()),
103+
size(),
104+
[&](const std::byte ipByte) {
105+
std::format_to(std::back_inserter(res), "{:02x}:", static_cast<int>(ipByte));
106+
});
109107
}
110108
res.pop_back();
111109
return res;
112110
}
113-
114111
};
115112

116-
/*friend constexpr auto operator<=>(const IPAddress& lhs, const IPAddress& rhs) noexcept
113+
/*friend constexpr auto operator<=>(const IPAddress& lhs, const IPAddress& rhs) noexcept
117114
{
118-
return lhs.u64 <=> rhs.u64;
115+
return lhs.u64 <=> rhs.u64;
119116
}*/
120117

121118
inline std::ostream& operator<<(std::ostream& os, const IPAddress& ip)
File renamed without changes.

include/ipfixprobe/processPlugin/processPlugin.hpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,41 @@
1414

1515
#pragma once
1616

17-
#include <cstdint>
18-
#include "fieldManager.hpp"
1917
#include "../api.hpp"
18+
#include "fieldManager.hpp"
19+
#include "tcpOptions.hpp"
20+
21+
#include <cstdint>
22+
#include <optional>
23+
24+
#include <amon/Packet.hpp>
25+
#include <amon/layers/TCP.hpp>
2026

2127
namespace ipxp {
2228

2329
/**
2430
* @brief Forward declarations for packet processing classes.
2531
*/
2632
class FlowRecord;
27-
class Packet;
28-
class PacketFeatures{};
33+
// class Packet;
34+
struct PacketFeatures {
35+
Direction direction;
36+
37+
std::optional<amon::layers::TCPView> tcp;
38+
std::optional<TCPOptions> tcpOptions;
39+
uint16_t ipPayloadLength {0};
40+
41+
// TODO TCP view does not provide seq/ack getters
42+
uint32_t tcpSequence {0};
43+
uint32_t tcpAcknowledgment {0};
44+
};
45+
46+
// TODO remove?
47+
constexpr std::span<const std::byte> getPayload(const amon::Packet& packet) noexcept
48+
{
49+
return packet.data.subspan(
50+
std::get<amon::PacketLayer>(packet.layers[*packet.layout.l7]).offset);
51+
}
2952

3053
/**
3154
* @brief Context passed to plugin methods, containing references to flow and packet.
@@ -37,7 +60,7 @@ struct FlowContext {
3760
/**< Reference to the flow record being processed. */
3861
FlowRecord& flowRecord;
3962
/**< Reference to the current packet being processed. */
40-
Packet& packet;
63+
amon::Packet& packet;
4164
/**< Reference to extracted features of the current packet. */
4265
PacketFeatures& features;
4366
};

0 commit comments

Comments
 (0)