|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Generic interface of processing plugin |
| 4 | + * @author Pavel Siska <[email protected]> |
| 5 | + * @author Vaclav Bartos <[email protected]> |
| 6 | + * @author Jiri Havranek <[email protected]> |
| 7 | + * @date 2025 |
| 8 | + * |
| 9 | + * Copyright (c) 2025 CESNET |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: BSD-3-Clause |
| 12 | + */ |
| 13 | + |
| 14 | +#pragma once |
| 15 | + |
| 16 | +#include "flowifc.hpp" |
| 17 | +#include "packet.hpp" |
| 18 | +#include "plugin.hpp" |
| 19 | + |
| 20 | +#include <atomic> |
| 21 | + |
| 22 | +namespace ipxp { |
| 23 | + |
| 24 | +/** |
| 25 | + * \brief Tell storage plugin to flush (immediately export) current flow. |
| 26 | + * Behavior when called from post_create, pre_update and post_update: flush current Flow and erase |
| 27 | + * FlowRecord. |
| 28 | + */ |
| 29 | +#define FLOW_FLUSH 0x1 |
| 30 | + |
| 31 | +/** |
| 32 | + * \brief Tell storage plugin to flush (immediately export) current flow. |
| 33 | + * Behavior when called from post_create: flush current Flow and erase FlowRecord. |
| 34 | + * Behavior when called from pre_update and post_update: flush current Flow, erase FlowRecord and |
| 35 | + * call post_create on packet. |
| 36 | + */ |
| 37 | +#define FLOW_FLUSH_WITH_REINSERT 0x3 |
| 38 | + |
| 39 | +/** |
| 40 | + * \brief Class template for flow cache plugins. |
| 41 | + */ |
| 42 | +class IPXP_API ProcessPlugin : public Plugin { |
| 43 | +public: |
| 44 | + ProcessPlugin() {} |
| 45 | + virtual ~ProcessPlugin() {} |
| 46 | + virtual ProcessPlugin* copy() = 0; |
| 47 | + |
| 48 | + virtual RecordExt* get_ext() const { return nullptr; } |
| 49 | + |
| 50 | + /** |
| 51 | + * \brief Called before a new flow record is created. |
| 52 | + * \param [in] pkt Parsed packet. |
| 53 | + * \return 0 on success or FLOW_FLUSH option. |
| 54 | + */ |
| 55 | + virtual int pre_create(Packet& pkt) { return 0; } |
| 56 | + |
| 57 | + /** |
| 58 | + * \brief Called after a new flow record is created. |
| 59 | + * \param [in,out] rec Reference to flow record. |
| 60 | + * \param [in] pkt Parsed packet. |
| 61 | + * \return 0 on success or FLOW_FLUSH option. |
| 62 | + */ |
| 63 | + virtual int post_create(Flow& rec, const Packet& pkt) { return 0; } |
| 64 | + |
| 65 | + /** |
| 66 | + * \brief Called before an existing record is update. |
| 67 | + * \param [in,out] rec Reference to flow record. |
| 68 | + * \param [in,out] pkt Parsed packet. |
| 69 | + * \return 0 on success or FLOW_FLUSH option. |
| 70 | + */ |
| 71 | + virtual int pre_update(Flow& rec, Packet& pkt) { return 0; } |
| 72 | + |
| 73 | + /** |
| 74 | + * \brief Called after an existing record is updated. |
| 75 | + * \param [in,out] rec Reference to flow record. |
| 76 | + * \param [in,out] pkt Parsed packet. |
| 77 | + * \return 0 on success or FLOW_FLUSH option. |
| 78 | + */ |
| 79 | + virtual int post_update(Flow& rec, const Packet& pkt) { return 0; } |
| 80 | + |
| 81 | + /** |
| 82 | + * \brief Called before a flow record is exported from the cache. |
| 83 | + * \param [in,out] rec Reference to flow record. |
| 84 | + */ |
| 85 | + virtual void pre_export(Flow& rec) {} |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief A class for generating unique plugin IDs. |
| 90 | + * |
| 91 | + * This class is designed to ensure atomic generation of unique IDs for process plugins. |
| 92 | + * The ID is incremented and stored using an atomic variable, ensuring thread-safe access |
| 93 | + * even with concurrent calls from multiple threads. The class is implemented as a Singleton. |
| 94 | + */ |
| 95 | +class IPXP_API ProcessPluginIDGenerator { |
| 96 | +public: |
| 97 | + /** |
| 98 | + * @brief Gets the instance of the ProcessPluginIDGenerator (Singleton). |
| 99 | + * @return A reference to the single instance of the ProcessPluginIDGenerator. |
| 100 | + * |
| 101 | + * This method ensures that there is only one instance of the ProcessPluginIDGenerator |
| 102 | + * class throughout the application's lifecycle. |
| 103 | + */ |
| 104 | + static ProcessPluginIDGenerator& instance() |
| 105 | + { |
| 106 | + static ProcessPluginIDGenerator instance; |
| 107 | + return instance; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @brief Generates a unique plugin ID. |
| 112 | + * @return The generated plugin ID. |
| 113 | + * |
| 114 | + * This method atomically increments the internal counter and returns the ID |
| 115 | + * that was generated before the increment. This ensures that each call produces |
| 116 | + * a unique, thread-safe ID. |
| 117 | + */ |
| 118 | + |
| 119 | + int generatePluginID() { return m_id.fetch_add(1, std::memory_order_relaxed); } |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Gets the current count of generated plugin IDs. |
| 123 | + * @return The total count of generated plugin IDs. |
| 124 | + * |
| 125 | + * This method returns the current value of the ID counter, representing the |
| 126 | + * total number of plugin IDs that have been generated so far. |
| 127 | + */ |
| 128 | + int getPluginsCount() const { return m_id.load(std::memory_order_relaxed); } |
| 129 | + |
| 130 | +private: |
| 131 | + std::atomic<int> m_id = 0; |
| 132 | +}; |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief Factory template for creating plugins. |
| 136 | + * |
| 137 | + * This template allows dynamic creation of plugin instances based on the specified |
| 138 | + * base class and constructor argument types. |
| 139 | + * |
| 140 | + * @tparam Base The base class for the plugin. |
| 141 | + * @tparam Args The argument types required for the plugin constructor. |
| 142 | + */ |
| 143 | +template<typename Base, typename... Args> |
| 144 | +class IPXP_API PluginFactory; |
| 145 | + |
| 146 | +/** |
| 147 | + * @brief Type alias for the ProcessPlugin factory. |
| 148 | + * |
| 149 | + * Provides a factory for creating ProcessPlugin instances using a string-based constructor. |
| 150 | + */ |
| 151 | +using ProcessPluginFactory = PluginFactory<ProcessPlugin, const std::string&>; |
| 152 | + |
| 153 | +} // namespace ipxp |
0 commit comments