|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Pavel Siska <[email protected]> |
| 4 | + * @brief Common interfaces and data structures for flow-processing plugins. |
| 5 | + * |
| 6 | + * This header defines the `ProcessPlugin` base class and related types |
| 7 | + * that form the contract between plugins and the flow-processing framework. |
| 8 | + * |
| 9 | + * Plugins built on this interface can observe and modify flows throughout |
| 10 | + * their lifecycle: initialization, per-packet updates, record export, |
| 11 | + * and destruction. The API ensures consistent integration while allowing |
| 12 | + * each plugin to manage its own per-flow state and memory layout. |
| 13 | + */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | + |
| 19 | +namespace ipxp { |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief Forward declarations for packet processing classes. |
| 23 | + */ |
| 24 | +class FlowRecord; |
| 25 | +class Packet; |
| 26 | +class PacketFeatures; |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief Context passed to plugin methods, containing references to flow and packet. |
| 30 | + * |
| 31 | + * This structure provides access to the current flow record, packet, and extracted packet features. |
| 32 | + * It is passed to plugin methods to allow them to inspect and modify flow and packet data. |
| 33 | + */ |
| 34 | +struct FlowContext { |
| 35 | + /**< Reference to the flow record being processed. */ |
| 36 | + FlowRecord& flowRecord; |
| 37 | + /**< Reference to the current packet being processed. */ |
| 38 | + Packet& packet; |
| 39 | + /**< Reference to extracted features of the current packet. */ |
| 40 | + PacketFeatures& features; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Indicates whether a plugin was successfully constructed for a flow. |
| 45 | + */ |
| 46 | +enum class ConstructionState : uint8_t { |
| 47 | + /**< Plugin was constructed and is active for the flow. */ |
| 48 | + Constructed = 0, |
| 49 | + /**< Plugin was not constructed for the flow. */ |
| 50 | + NotConstructed, |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Describes whether a plugin requires further updates for a flow. |
| 55 | + */ |
| 56 | +enum class UpdateRequirement : uint8_t { |
| 57 | + /**< Plugin wants to continue processing packets for the flow. */ |
| 58 | + RequiresUpdate = 0, |
| 59 | + /**< Plugin does not require further updates for the flow. */ |
| 60 | + NoUpdateNeeded, |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Action to be taken for a flow or plugin after processing. |
| 65 | + */ |
| 66 | +enum class FlowAction : uint8_t { |
| 67 | + /**< No special action required. */ |
| 68 | + NoAction = 0, |
| 69 | + /**< Flow should be flushed (exported). */ |
| 70 | + Flush, |
| 71 | + /**< Plugin should be removed from the flow. */ |
| 72 | + RemovePlugin, |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * @brief Result of plugin initialization for a flow. |
| 77 | + * |
| 78 | + * Contains information about whether the plugin was constructed, if it requires updates, |
| 79 | + * and what action should be taken for the flow or plugin. |
| 80 | + */ |
| 81 | +struct PluginInitResult { |
| 82 | + /**< Indicates if the plugin was constructed for the flow. */ |
| 83 | + ConstructionState constructionState; |
| 84 | + /**< Specifies if the plugin requires further updates. */ |
| 85 | + UpdateRequirement updateRequirement; |
| 86 | + /**< Action to be taken for the flow or plugin after initialization. */ |
| 87 | + FlowAction flowAction; |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * @brief Result of plugin update for a flow. |
| 92 | + * |
| 93 | + * Contains information about whether the plugin requires further updates and what action should be |
| 94 | + * taken. |
| 95 | + */ |
| 96 | +struct PluginUpdateResult { |
| 97 | + /**< Specifies if the plugin requires further updates. */ |
| 98 | + UpdateRequirement updateRequirement; |
| 99 | + /**< Action to be taken for the flow or plugin after update. */ |
| 100 | + FlowAction flowAction; |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Result of plugin export for a flow. |
| 105 | + * |
| 106 | + * Contains information about what action should be taken for the flow or plugin after export. |
| 107 | + */ |
| 108 | +struct PluginExportResult { |
| 109 | + /**< Action to be taken for the flow or plugin after export. */ |
| 110 | + FlowAction flowAction; |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief Describes memory layout for plugin-specific data. |
| 115 | + * |
| 116 | + * This structure specifies the size and alignment requirements for memory allocated |
| 117 | + * for plugin-specific context data. Plugins should use this information to allocate |
| 118 | + * and access their per-flow state. |
| 119 | + */ |
| 120 | +struct PluginDataMemoryLayout { |
| 121 | + /**< Size in bytes required for the plugin's context data. */ |
| 122 | + std::size_t size; |
| 123 | + /**< Alignment in bytes required for the plugin's context data. */ |
| 124 | + std::size_t alignment; |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief Abstract base class for all flow-processing plugins. |
| 129 | + * |
| 130 | + * Provides a common interface for plugins that react to flow lifecycle events. |
| 131 | + * All custom flow-processing plugins should inherit from this class and implement its methods. |
| 132 | + */ |
| 133 | +class ProcessPlugin { |
| 134 | +public: |
| 135 | + ProcessPlugin() = default; |
| 136 | + virtual ~ProcessPlugin() noexcept = default; |
| 137 | + |
| 138 | + ProcessPlugin(const ProcessPlugin&) = delete; |
| 139 | + ProcessPlugin& operator=(const ProcessPlugin&) = delete; |
| 140 | + ProcessPlugin(ProcessPlugin&&) = delete; |
| 141 | + ProcessPlugin& operator=(ProcessPlugin&&) = delete; |
| 142 | + |
| 143 | + /** |
| 144 | + * @brief Called to attempt plugin construction for a flow. |
| 145 | + * |
| 146 | + * This method is called repeatedly for a flow until the plugin is either constructed |
| 147 | + * (returns ConstructionState::Constructed) or no longer requires updates (returns |
| 148 | + * UpdateRequirement::NoUpdateNeeded). If the plugin is constructed and update is required, the |
| 149 | + * framework will then call onUpdate(). |
| 150 | + * |
| 151 | + * Typical plugin lifecycle: Init → Update → Export → Destroy. |
| 152 | + * |
| 153 | + * @param flowContext Context containing references to the flow and packet. |
| 154 | + * @param pluginContext Pointer to plugin-specific context memory. |
| 155 | + * @return PluginInitResult describing construction and update requirements. |
| 156 | + */ |
| 157 | + [[nodiscard]] virtual PluginInitResult |
| 158 | + onInit(const FlowContext& flowContext, void* pluginContext) |
| 159 | + = 0; |
| 160 | + |
| 161 | + /** |
| 162 | + * @brief Called to update plugin state for a constructed flow. |
| 163 | + * |
| 164 | + * This method is called for each packet of a flow after the plugin has been constructed |
| 165 | + * (onInit returned ConstructionState::Constructed and RequiresUpdate). It allows the plugin |
| 166 | + * to process packets and update its internal state. If the plugin no longer requires updates, |
| 167 | + * it should return UpdateRequirement::NoUpdateNeeded. The method can also request actions such |
| 168 | + * as flushing or removing the plugin from the flow. |
| 169 | + * |
| 170 | + * @param flowContext Context containing references to the flow and packet. |
| 171 | + * @param pluginContext Pointer to plugin-specific context memory. |
| 172 | + * @return PluginUpdateResult describing update requirements and actions. |
| 173 | + */ |
| 174 | + [[nodiscard]] virtual PluginUpdateResult |
| 175 | + onUpdate(const FlowContext& flowContext, void* pluginContext) |
| 176 | + { |
| 177 | + (void) flowContext; |
| 178 | + (void) pluginContext; |
| 179 | + return { |
| 180 | + .updateRequirement = UpdateRequirement::NoUpdateNeeded, |
| 181 | + .flowAction = FlowAction::NoAction, |
| 182 | + }; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @brief Called to export the flow record processed by the plugin. |
| 187 | + * |
| 188 | + * This method is called when the flow record is being exported. It allows the plugin to |
| 189 | + * perform any final processing or cleanup before the flow is exported. The plugin can also |
| 190 | + * request to be removed from the flow by returning FlowAction::RemovePlugin. |
| 191 | + * |
| 192 | + * @param flowRecord Reference to the flow record being exported. |
| 193 | + * @param pluginContext Pointer to plugin-specific context memory. |
| 194 | + * @return PluginExportResult containing the flow action to be taken. |
| 195 | + */ |
| 196 | + [[nodiscard]] virtual PluginExportResult |
| 197 | + onExport(const FlowRecord& flowRecord, void* pluginContext) |
| 198 | + { |
| 199 | + (void) flowRecord; |
| 200 | + (void) pluginContext; |
| 201 | + return { |
| 202 | + .flowAction = FlowAction::NoAction, |
| 203 | + }; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @brief Called when the plugin is destroyed for a flow. |
| 208 | + * |
| 209 | + * This method is called when the plugin is being detached from a flow and should clean up any |
| 210 | + * resources. It is only called if the plugin was successfully constructed (i.e., onInit |
| 211 | + * returned ConstructionState::Constructed). |
| 212 | + * |
| 213 | + * @param pluginContext Pointer to plugin-specific context memory. |
| 214 | + */ |
| 215 | + virtual void onDestroy(void* pluginContext) = 0; |
| 216 | + |
| 217 | + /** |
| 218 | + * @brief Returns memory layout for plugin-specific data. |
| 219 | + * |
| 220 | + * This method should return the size and alignment requirements for the plugin's context data. |
| 221 | + * |
| 222 | + * @return PluginDataMemoryLayout structure describing memory layout. |
| 223 | + */ |
| 224 | + [[nodiscard]] virtual PluginDataMemoryLayout getDataMemoryLayout() const noexcept = 0; |
| 225 | +}; |
| 226 | + |
| 227 | +} // namespace ipxp |
0 commit comments