Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
197 lines (174 loc) · 5.99 KB
/
Copy pathmain.cpp
File metadata and controls
197 lines (174 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @file
* @author Damir Zainullin <zaidamilda@gmail.com>
* @brief Deduplication Module: Deduplicate flowdata
*
* This file contains the main function and supporting functions for the Unirec Deduplication
* Module. This module process Unirec records thourgh a bidirectional interface and ignores flows
* that seem to be duplicates of already processed flows.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "deduplicator.hpp"
#include "logger/logger.hpp"
#include "unirec/unirec-telemetry.hpp"
#include <appFs.hpp>
#include <argparse/argparse.hpp>
#include <iostream>
#include <stdexcept>
#include <telemetry.hpp>
#include <unirec++/unirec.hpp>
using namespace Nemea;
/**
* @brief Handle a format change exception by adjusting the template.
*
* This function is called when a `FormatChangeException` is caught in the main loop.
* It adjusts the template in the bidirectional interface to handle the format change.
*
* @param biInterface Bidirectional interface for Unirec communication.
* @param deduplicator Deduplicator instance.
*/
static void handleFormatChange(
UnirecBidirectionalInterface& biInterface,
Deduplicator::Deduplicator& deduplicator)
{
biInterface.changeTemplate();
deduplicator.updateUnirecIds();
}
/**
* @brief Process the next Unirec record and sample them.
*
* This function receives the next Unirec record through the bidirectional interface
* saves it the hash map and send non-duplicate flows back to the bidirectional interface.
*
* @param biInterface Bidirectional interface for Unirec communication.
* @param deduplicator Deduplicator instance to process flows.
*/
static void processNextRecord(
UnirecBidirectionalInterface& biInterface,
Deduplicator::Deduplicator& deduplicator)
{
std::optional<UnirecRecordView> unirecRecord = biInterface.receive();
if (!unirecRecord) {
return;
}
if (!deduplicator.isDuplicate(*unirecRecord)) {
biInterface.send(*unirecRecord);
}
}
/**
* @brief Process Unirec records.
*
* The `processUnirecRecords` function continuously receives Unirec records through the provided
* bidirectional interface (`biInterface`) and process them. The loop runs indefinitely until
* an end-of-file condition is encountered.
*
* @param biInterface Bidirectional interface for Unirec communication.
* @param deduplicator Deduplicator instance to process flows.
*/
static void processUnirecRecords(
UnirecBidirectionalInterface& biInterface,
Deduplicator::Deduplicator& deduplicator)
{
while (true) {
try {
processNextRecord(biInterface, deduplicator);
} catch (FormatChangeException& ex) {
handleFormatChange(biInterface, deduplicator);
} catch (const EoFException& ex) {
break;
} catch (const std::exception& ex) {
throw;
}
}
}
int main(int argc, char** argv)
{
argparse::ArgumentParser program("Unirec Deduplicator");
Unirec unirec({1, 1, "deduplicator", "Unirec deduplicator module"});
Nm::loggerInit();
auto logger = Nm::loggerGet("main");
try {
unirec.init(argc, argv);
} catch (const HelpException& ex) {
std::cerr << program;
return EXIT_SUCCESS;
} catch (const std::exception& ex) {
logger->error(ex.what());
return EXIT_FAILURE;
}
try {
program.add_argument("-s", "--size")
.required()
.help("Exponent N for the hash map size (2^N entries). Default: 20 (~1 048 576).")
.default_value(Deduplicator::Deduplicator::DeduplicatorHashMap::
TimeoutHashMapParameters::DEFAULT_HASHMAP_EXPONENT)
.scan<'u', uint32_t>();
program.add_argument("-t", "--timeout")
.required()
.help(
"Count of millisecond to consider flows as duplicates. Default value is 5000 (5s).")
.default_value(Deduplicator::Deduplicator::DEFAULT_HASHMAP_TIMEOUT)
.scan<'u', uint64_t>();
program.add_argument("-m", "--appfs-mountpoint")
.required()
.help("path where the appFs directory will be mounted")
.default_value(std::string(""));
program.parse_args(argc, argv);
} catch (const std::exception& ex) {
logger->error(ex.what());
std::cerr << program;
return EXIT_FAILURE;
}
std::shared_ptr<telemetry::Directory> telemetryRootDirectory;
telemetryRootDirectory = telemetry::Directory::create();
std::unique_ptr<telemetry::appFs::AppFsFuse> appFs;
try {
auto mountPoint = program.get<std::string>("--appfs-mountpoint");
if (!mountPoint.empty()) {
const bool tryToUnmountOnStart = true;
const bool createMountPoint = true;
appFs = std::make_unique<telemetry::appFs::AppFsFuse>(
telemetryRootDirectory,
mountPoint,
tryToUnmountOnStart,
createMountPoint);
appFs->start();
}
} catch (std::exception& ex) {
logger->error(ex.what());
return EXIT_FAILURE;
}
try {
const auto tableSize = program.get<uint32_t>("--size");
if (tableSize <= 0) {
std::cerr << "Table size must be at least 8.\n";
return EXIT_FAILURE;
}
const auto timeout = program.get<uint64_t>("--timeout");
if (timeout <= 0) {
std::cerr << "Timeout must be higher than zero.\n";
return EXIT_FAILURE;
}
UnirecBidirectionalInterface biInterface = unirec.buildBidirectionalInterface();
auto telemetryInputDirectory = telemetryRootDirectory->addDir("input");
const telemetry::FileOps inputFileOps
= {[&biInterface]() { return Nm::getInterfaceTelemetry(biInterface); }, nullptr};
const auto inputFile = telemetryInputDirectory->addFile("stats", inputFileOps);
auto telemetryDeduplicatorDirectory = telemetryRootDirectory->addDir("deduplicator");
Deduplicator::Deduplicator::DeduplicatorHashMap::TimeoutHashMapParameters parameters;
parameters.bucketCountExponent = tableSize;
parameters.timeout = timeout;
Deduplicator::Deduplicator deduplicator(parameters);
deduplicator.setTelemetryDirectory(telemetryDeduplicatorDirectory);
biInterface.setRequieredFormat(
"uint16 SRC_PORT, uint16 DST_PORT, ipaddr DST_IP,ipaddr SRC_IP, uint64 LINK_BIT_FIELD, "
"uint8 PROTOCOL, time TIME_LAST");
deduplicator.updateUnirecIds();
processUnirecRecords(biInterface, deduplicator);
} catch (std::exception& ex) {
logger->error(ex.what());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}