forked from CESNET/ipfixprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.cpp
230 lines (204 loc) · 6.35 KB
/
workers.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* \file workers.cpp
* \brief Exporter worker procedures source
* \author Jiri Havranek <[email protected]>
* \date 2021
*/
/*
* Copyright (C) 2021 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
*
*
*/
#include <unistd.h>
#include <sys/time.h>
#include "workers.hpp"
#include "ipfixprobe.hpp"
namespace ipxp {
#define MICRO_SEC 1000000L
void input_storage_worker(InputPlugin *plugin, StoragePlugin *cache, size_t queue_size, uint64_t pkt_limit,
std::promise<WorkerResult> *out, std::atomic<InputStats> *out_stats)
{
struct timespec start_cache;
struct timespec end_cache;
struct timespec begin = {0, 0};
struct timespec end = {0, 0};
struct timeval ts = {0, 0};
bool timeout = false;
InputPlugin::Result ret;
InputStats stats = {0, 0, 0, 0, 0};
WorkerResult res = {false, ""};
PacketBlock block(queue_size);
#ifdef __linux__
const clockid_t clk_id = CLOCK_MONOTONIC_COARSE;
#else
const clockid_t clk_id = CLOCK_MONOTONIC;
#endif
while (!terminate_input) {
block.cnt = 0;
block.bytes = 0;
if (pkt_limit && plugin->m_parsed + block.size >= pkt_limit) {
if (plugin->m_parsed >= pkt_limit) {
break;
}
block.size = pkt_limit - plugin->m_parsed;
}
try {
ret = plugin->get(block);
} catch (PluginError &e) {
res.error = true;
res.msg = e.what();
break;
}
if (ret == InputPlugin::Result::TIMEOUT) {
clock_gettime(clk_id, &end);
if (!timeout) {
timeout = true;
begin = end;
}
struct timespec diff = {end.tv_sec - begin.tv_sec, end.tv_nsec - begin.tv_nsec};
if (diff.tv_nsec < 0) {
diff.tv_nsec += 1000000000;
diff.tv_sec--;
}
cache->export_expired(ts.tv_sec + diff.tv_sec);
usleep(1);
continue;
} else if (ret == InputPlugin::Result::PARSED) {
stats.packets = plugin->m_seen;
stats.parsed = plugin->m_parsed;
stats.dropped = plugin->m_dropped;
stats.bytes += block.bytes;
clock_gettime(clk_id, &start_cache);
try {
for (unsigned i = 0; i < block.cnt; i++) {
cache->put_pkt(block.pkts[i]);
}
ts = block.pkts[block.cnt - 1].ts;
} catch (PluginError &e) {
res.error = true;
res.msg = e.what();
break;
}
timeout = false;
clock_gettime(clk_id, &end_cache);
int64_t time = end_cache.tv_nsec - start_cache.tv_nsec;
if (start_cache.tv_sec != end_cache.tv_sec) {
time += 1000000000;
}
stats.qtime += time;
out_stats->store(stats);
} else if (ret == InputPlugin::Result::ERROR) {
res.error = true;
res.msg = "error occured during reading";
break;
} else if (ret == InputPlugin::Result::END_OF_FILE) {
break;
}
}
stats.packets = plugin->m_seen;
stats.parsed = plugin->m_parsed;
stats.dropped = plugin->m_dropped;
out_stats->store(stats);
cache->finish();
auto outq = cache->get_queue();
while (ipx_ring_cnt(outq)) {
usleep(1);
}
out->set_value(res);
}
static long timeval_diff(const struct timeval *start, const struct timeval *end)
{
return (end->tv_sec - start->tv_sec) * MICRO_SEC
+ (end->tv_usec - start->tv_usec);
}
void output_worker(OutputPlugin *exp, ipx_ring_t *queue, std::promise<WorkerResult> *out, std::atomic<OutputStats> *out_stats,
uint32_t fps)
{
WorkerResult res = {false, ""};
OutputStats stats = {0, 0, 0, 0};
struct timespec sleep_time = {0};
struct timeval begin;
struct timeval end;
struct timeval last_flush;
uint32_t pkts_from_begin = 0;
double time_per_pkt = 0;
if (fps != 0) {
time_per_pkt = 1000000.0 / fps; // [micro seconds]
}
// Rate limiting algorithm from https://github.com/CESNET/ipfixcol2/blob/master/src/tools/ipfixsend/sender.c#L98
gettimeofday(&begin, nullptr);
last_flush = begin;
while (1) {
gettimeofday(&end, nullptr);
Flow *flow = static_cast<Flow *>(ipx_ring_pop(queue));
if (!flow) {
if (end.tv_sec - last_flush.tv_sec > 1) {
last_flush = end;
exp->flush();
}
if (terminate_export && !ipx_ring_cnt(queue)) {
break;
}
continue;
}
stats.biflows++;
stats.bytes += flow->src_bytes + flow->dst_bytes;
stats.packets += flow->src_packets + flow->dst_packets;
stats.dropped = exp->m_flows_dropped;
out_stats->store(stats);
try {
exp->export_flow(*flow);
} catch (PluginError &e) {
res.error = true;
res.msg = e.what();
break;
}
pkts_from_begin++;
if (fps == 0) {
// Limit for packets/s is not enabled
continue;
}
// Calculate expected time of sending next packet
long elapsed = timeval_diff(&begin, &end);
if (elapsed < 0) {
// Should be never negative. Just for sure...
elapsed = pkts_from_begin * time_per_pkt;
}
long next_start = pkts_from_begin * time_per_pkt;
long diff = next_start - elapsed;
if (diff >= MICRO_SEC) {
diff = MICRO_SEC - 1;
}
// Sleep
if (diff > 0) {
sleep_time.tv_nsec = diff * 1000L;
nanosleep(&sleep_time, nullptr);
}
if (pkts_from_begin >= fps) {
// Restart counter
gettimeofday(&begin, nullptr);
pkts_from_begin = 0;
}
}
exp->flush();
stats.dropped = exp->m_flows_dropped;
out_stats->store(stats);
out->set_value(res);
}
}