Skip to content

Commit e102b4d

Browse files
author
Pavel Siska
committed
WIP
1 parent bb30bc1 commit e102b4d

File tree

3 files changed

+130
-23
lines changed

3 files changed

+130
-23
lines changed

process-plugin-api/fieldHandler.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <limits>
77
#include <stdexcept>
88

9+
using BitIndexType = uint8_t;
10+
911
/**
1012
* @brief Represents a handle to a single field within a FlowRecord.
1113
*
@@ -57,7 +59,7 @@ class FieldHandler {
5759

5860
/// Returns the bit index associated with this field.
5961
[[nodiscard]]
60-
std::size_t getIndex() const noexcept
62+
BitIndexType getIndex() const noexcept
6163
{
6264
return m_bitIndex;
6365
}
@@ -73,7 +75,7 @@ class FieldHandler {
7375
friend class FieldManager;
7476

7577
/// Constructor used only by FieldManager to create a valid handler.
76-
explicit FieldHandler(std::size_t bitIndex) noexcept
78+
explicit FieldHandler(BitIndexType bitIndex) noexcept
7779
: m_bitIndex(bitIndex)
7880
{
7981
}
@@ -87,6 +89,6 @@ class FieldHandler {
8789
}
8890

8991
/// Special value used to mark an uninitialized handler.
90-
static constexpr std::size_t s_invalidIndex = std::numeric_limits<std::size_t>::max();
91-
std::size_t m_bitIndex;
92+
static constexpr BitIndexType s_invalidIndex = std::numeric_limits<BitIndexType>::max();
93+
BitIndexType m_bitIndex;
9294
};

process-plugin-api/flowRecordBuilder.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// #include <ipfixprobe/pluginFactory/pluginFactory.hpp>
1313
#include "dummyProcessPlugin.hpp"
14+
#include "icmp.hpp"
1415
#include "processPlugin.hpp"
1516

1617
static size_t alignUp(size_t offset, size_t alignment)
@@ -37,7 +38,7 @@ class FlowRecordBuilder {
3738
.name = pluginName,
3839
.size = 5 * 64, // TODO
3940
.alignment = 8, // TODO
40-
.prototype = std::make_unique<DummyPlugin>(std::forward<Args>(
41+
.prototype = std::make_unique<IcmpPlugin>(std::forward<Args>(
4142
args)...), // pluginFactory.createUnique(pluginName, std::forward<Args>(args)...),
4243
};
4344

process-plugin-api/processPlugin.hpp

Lines changed: 122 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,89 +4,193 @@
44
#include "flowRecord.hpp"
55
#include "packet.hpp"
66

7+
#include <array>
78
#include <cstdint>
9+
#include <memory>
810
#include <string>
11+
#include <type_traits>
912

13+
/**
14+
* @brief Represents the possible actions a processing plugin can request after handling a packet.
15+
*/
1016
enum class FlowAction : int {
1117
/**
12-
* @brief Request complete flow data (packets + metadata).
18+
* Request complete flow data (packets + metadata).
1319
*/
1420
RequestFullData,
1521

1622
/**
17-
* @brief Request only trimmed flow data (no payload).
23+
* Request only trimmed flow data (no payload).
1824
*/
1925
RequestTrimmedData,
2026

2127
/**
22-
* @brief Indicate that no further processing is needed for this flow.
28+
* Indicate that no further processing is needed for this flow.
2329
*/
2430
RequestNoData,
2531

2632
/**
27-
* @brief Export the flow immediately and erase its record.
33+
* Export the flow immediately and erase its record.
2834
*/
2935
Flush,
3036

3137
/**
32-
* @brief Export the flow immediately, erase its record, and re-insert new flow.
38+
* Export the flow immediately, erase its record, and re-insert a new flow.
3339
*/
3440
FlushAndReinsert
3541
};
3642

43+
/**
44+
* @brief Abstract base class for all flow-processing plugins.
45+
*
46+
* Provides a common interface for plugins that react to flow lifecycle events.
47+
*/
3748
class ProcessPlugin {
3849
public:
3950
ProcessPlugin() = default;
51+
virtual ~ProcessPlugin() = default;
4052

53+
/**
54+
* @brief Called when a new flow is created.
55+
*
56+
* @param flowRecord Reference to the new flow record.
57+
* @param packet Packet that triggered the flow creation.
58+
* @return Requested action after processing.
59+
*/
4160
virtual FlowAction onFlowCreate(FlowRecord& flowRecord, const Packet& packet)
4261
{
4362
(void) flowRecord;
4463
(void) packet;
45-
4664
return FlowAction::RequestNoData;
4765
}
4866

67+
/**
68+
* @brief Called when an existing flow is updated with a new packet.
69+
*
70+
* @param flowRecord Reference to the existing flow record.
71+
* @param packet The incoming packet.
72+
* @return Requested action after processing.
73+
*/
4974
virtual FlowAction onFlowUpdate(FlowRecord& flowRecord, const Packet& packet)
5075
{
5176
(void) flowRecord;
5277
(void) packet;
53-
5478
return FlowAction::RequestNoData;
5579
}
5680

81+
/**
82+
* @brief Called right before a flow is exported.
83+
*
84+
* Can be used for cleanup or finalization.
85+
*/
5786
virtual void onFlowExport() {}
5887

88+
/**
89+
* @brief Returns a pointer to the data to be exported.
90+
*
91+
* Typically points to a POD structure.
92+
*
93+
* @return Const void pointer to export data.
94+
*/
5995
virtual const void* getExportData() const noexcept = 0;
6096

97+
/**
98+
* @brief Clone the plugin into pre-allocated memory.
99+
*
100+
* Uses placement new semantics.
101+
*
102+
* @param constructAtAddress Address where the clone should be constructed.
103+
* @return Pointer to the newly constructed clone.
104+
*/
61105
virtual ProcessPlugin* clone(std::byte* constructAtAddress) const = 0;
62106

107+
/**
108+
* @brief Returns the unique name of the plugin.
109+
*
110+
* Used e.g. for schema identification or logging.
111+
*
112+
* @return Name of the plugin.
113+
*/
63114
virtual std::string getName() const = 0;
115+
};
64116

65-
virtual ~ProcessPlugin() = default;
117+
/**
118+
* @brief CRTP base class that provides default clone() implementation.
119+
*
120+
* Use this class as a base if your derived plugin has copy constructor.
121+
*/
122+
template<typename Derived>
123+
class ProcessPluginWithClone : public ProcessPlugin {
124+
public:
125+
ProcessPluginWithClone() = default;
126+
~ProcessPluginWithClone() override = default;
127+
128+
/**
129+
* @brief Clone the derived plugin using placement new.
130+
*/
131+
ProcessPlugin* clone(std::byte* constructAtAddress) const override
132+
{
133+
return std::construct_at(
134+
reinterpret_cast<Derived*>(constructAtAddress),
135+
static_cast<const Derived&>(*this));
136+
}
66137
};
67138

139+
/**
140+
* @brief Helper to determine the number of enum values.
141+
*
142+
* Requires that the enum defines a final enumerator named `FIELDS_SIZE`.
143+
*
144+
* @tparam E The enum type.
145+
* @return Number of elements.
146+
*/
68147
template<typename E>
69-
constexpr std::size_t enum_size()
148+
constexpr uint8_t enum_size()
70149
{
71-
return static_cast<std::size_t>(E::FIELDS_SIZE);
150+
return static_cast<uint8_t>(E::FIELDS_SIZE);
72151
}
73152

74-
template<typename Enum, typename T, std::size_t Size>
153+
/**
154+
* @brief Fixed-size array indexed by enum class.
155+
*
156+
* Simplifies code by allowing strongly typed enum indexing.
157+
*
158+
* @tparam Enum Enum class type (must be contiguous, starting at 0).
159+
* @tparam T Stored type.
160+
* @tparam Size Size of the enum (should match number of fields).
161+
*/
162+
template<typename Enum, typename T, uint8_t Size>
75163
class EnumArray {
76164
public:
77165
static_assert(std::is_enum_v<Enum>, "EnumArray requires an enum type");
78166

79-
// Přetížený operator[], který umožní indexovat pomocí enum class
80-
T& operator[](Enum index) { return m_data[static_cast<std::size_t>(index)]; }
167+
/**
168+
* @brief Access element by enum index.
169+
*
170+
* @param index Enum value.
171+
* @return Reference to the stored value.
172+
*/
173+
T& operator[](Enum index) { return m_data[static_cast<uint8_t>(index)]; }
81174

82-
const T& operator[](Enum index) const { return m_data[static_cast<std::size_t>(index)]; }
175+
/**
176+
* @brief Const access to element by enum index.
177+
*
178+
* @param index Enum value.
179+
* @return Const reference to the stored value.
180+
*/
181+
const T& operator[](Enum index) const { return m_data[static_cast<uint8_t>(index)]; }
83182

84183
private:
85184
std::array<T, Size> m_data;
86185
};
87186

187+
/**
188+
* @brief Storage for field handlers indexed by enum.
189+
*
190+
* Designed to hold field accessors for a plugin schema.
191+
*
192+
* @tparam Enum Enum type used to represent individual fields.
193+
*/
194+
88195
template<typename Enum>
89-
class FieldHandlers {
90-
protected:
91-
EnumArray<Enum, FieldHandler, enum_size<Enum>()> m_fieldHandlers;
92-
};
196+
using FieldHandlers = EnumArray<Enum, FieldHandler, enum_size<Enum>()>;

0 commit comments

Comments
 (0)