|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "../../deduplicator/src/timeoutHashMap.hpp" |
| 4 | + |
| 5 | +#include <atomic> |
| 6 | +#include <memory> |
| 7 | +#include <telemetry.hpp> |
| 8 | +#include <thread> |
| 9 | +#include <unirec++/unirecRecordView.hpp> |
| 10 | +#include <unirec++/unirecRecord.hpp> |
| 11 | +#include <unirec++/urTime.hpp> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +namespace FlowGrouper { |
| 15 | + |
| 16 | +/** |
| 17 | + * @brief FlowGrouper class to add same flowID to duplicate records |
| 18 | + */ |
| 19 | +class FlowGrouper { |
| 20 | +public: |
| 21 | + /** |
| 22 | + * @brief Timestamp type used by flowGrouper. |
| 23 | + */ |
| 24 | + using Timestamp = std::chrono::time_point<std::chrono::steady_clock>; |
| 25 | + |
| 26 | + /** |
| 27 | + * @brief Field type representing Flow ID (FLOW_GROUP_KEY). |
| 28 | + */ |
| 29 | + using FlowGroupKey = uint64_t; |
| 30 | + |
| 31 | + /** |
| 32 | + * @brief Represents key fields of flow that belong to the same group. |
| 33 | + */ |
| 34 | + struct FlowKey { |
| 35 | + Nemea::IpAddress srcIp; ///< Source IP address. |
| 36 | + Nemea::IpAddress dstIp; ///< Destination IP address. |
| 37 | + uint16_t srcPort; ///< Source port. |
| 38 | + uint16_t dstPort; ///< Destination port. |
| 39 | + uint8_t proto; ///< Protocol ID. |
| 40 | + }; |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief Timeout hash map type used by FlowGrouper. |
| 46 | + */ |
| 47 | + using FlowGrouperHashMap = Deduplicator::TimeoutHashMap< |
| 48 | + FlowKey, |
| 49 | + FlowGroupKey, |
| 50 | + Timestamp, |
| 51 | + std::function<size_t(const FlowKey&)>, |
| 52 | + std::function<bool(const Timestamp&, const Timestamp&)>, |
| 53 | + std::function<Timestamp(const Timestamp&, uint64_t)>>; |
| 54 | + |
| 55 | + static inline const uint64_t DEFAULT_HASHMAP_TIMEOUT = 5000; ///< Default timeout - 5s |
| 56 | + static inline const uint32_t DEFAULT_HASHMAP_EXPONENT = 20; ///< Default size exponent - 2^20 entries |
| 57 | + |
| 58 | + /** |
| 59 | + * @brief FlowGrouper constructor |
| 60 | + * |
| 61 | + * @param parameters Parameters to build hash table of flowGrouper |
| 62 | + */ |
| 63 | + explicit FlowGrouper(const FlowGrouperHashMap::TimeoutHashMapParameters& parameters); |
| 64 | + |
| 65 | + /** |
| 66 | + * @brief Checks if the given UnirecRecordView group already exists in the hash map if not adds it. |
| 67 | + * @param view The Unirec record to check. |
| 68 | + * @return FlowGroupKey of the flow. |
| 69 | + */ |
| 70 | + FlowGroupKey getFlowKey(Nemea::UnirecRecordView& view); |
| 71 | + |
| 72 | + /** |
| 73 | + * @brief Adds FLOW_GROUP_KEY field to the output Unirec record. |
| 74 | + * @param inputRecord The input Unirec record view to get field values from. |
| 75 | + * @param outputRecord The output Unirec record where FLOW_GROUP_KEY will be added. |
| 76 | + */ |
| 77 | + void addFlowKey(Nemea::UnirecRecordView& inputRecord, Nemea::UnirecRecord& outputRecord); |
| 78 | + |
| 79 | + /** |
| 80 | + * @brief Sets the telemetry directory for the flowGrouper. |
| 81 | + * @param directory directory for flowGrouper telemetry. |
| 82 | + */ |
| 83 | + void setTelemetryDirectory(const std::shared_ptr<telemetry::Directory>& directory); |
| 84 | + |
| 85 | + /** |
| 86 | + * @brief Update Unirec Id of required fields after template format change. |
| 87 | + */ |
| 88 | + void updateUnirecIds(); |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief Gets the name of the output field added by FlowGrouper. |
| 92 | + * @return Name of the output field. |
| 93 | + */ |
| 94 | + static std::string getOutputFieldName() { |
| 95 | + return "FLOW_GROUP_KEY"; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Gets the output template string after adding FLOW_GROUP_KEY field. |
| 100 | + * @param inputTemplate The input Unirec template string. |
| 101 | + * @return The output Unirec template string with FLOW_GROUP_KEY field added. |
| 102 | + */ |
| 103 | + static std::string getOutputTemplate(std::string inputTemplate) { |
| 104 | + //check if input template already contains output field |
| 105 | + if (inputTemplate.find(" "+getOutputFieldName()) != std::string::npos) { |
| 106 | + return inputTemplate; |
| 107 | + } |
| 108 | + return inputTemplate + ", uint64 " + getOutputFieldName(); |
| 109 | + } |
| 110 | + |
| 111 | +private: |
| 112 | + FlowGrouperHashMap m_hashMap; ///< Hash map to keep flows |
| 113 | + |
| 114 | + uint32_t m_newInserted {0}; ///< Count of new groups |
| 115 | + uint32_t m_replaced {0}; ///< Count of replaced groups |
| 116 | + uint32_t m_found {0}; ///< Count of when groupkey was found |
| 117 | + |
| 118 | + telemetry::Holder m_holder; ///< Telemetry holder |
| 119 | + |
| 120 | + struct UnirecIdStorage { |
| 121 | + ur_field_id_t srcIpId; ///< Unirec ID of source ip. |
| 122 | + ur_field_id_t dstIpId; ///< Unirec ID of destination ip. |
| 123 | + ur_field_id_t srcPortId; ///< Unirec ID of source port. |
| 124 | + ur_field_id_t dstPortId; ///< Unirec ID of destination port. |
| 125 | + ur_field_id_t protocolId; ///< Unirec ID of protocol field. |
| 126 | + |
| 127 | + ur_field_id_t flowGroupKeyId; ///< Unirec ID of FLOW_GROUP_KEY field. |
| 128 | + }; |
| 129 | + |
| 130 | + UnirecIdStorage m_ids; ///< Ids of Unirec fields used by flowGrouper module |
| 131 | +}; |
| 132 | + |
| 133 | +} // namespace FlowGrouper |
0 commit comments