Skip to content

Commit 6cc51dc

Browse files
authored
Merge pull request #68 from CESNET/unirec_output_fix
fixed unirec interface flow duplication when multiple plugins enabled
2 parents c14238e + 0763113 commit 6cc51dc

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

output/unirec.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ UR_FIELDS (
9494
/**
9595
* \brief Constructor.
9696
*/
97-
UnirecExporter::UnirecExporter() : m_basic_idx(-1), m_ext_cnt(0), m_ifc_map(nullptr),
98-
m_tmplts(nullptr), m_records(nullptr), m_ifc_cnt(0), m_eof(false), m_odid(false), m_link_bit_field(0), m_dir_bit_field(0)
97+
UnirecExporter::UnirecExporter() : m_basic_idx(-1), m_ext_cnt(0),
98+
m_ifc_map(nullptr), m_tmplts(nullptr), m_records(nullptr), m_ifc_cnt(0),
99+
m_ext_id_flgs(nullptr), m_eof(false), m_odid(false), m_link_bit_field(0),
100+
m_dir_bit_field(0)
99101
{
100102
}
101103

@@ -189,6 +191,7 @@ void UnirecExporter::init(const char *params)
189191
m_tmplts = new ur_template_t*[m_ifc_cnt];
190192
m_records = new void*[m_ifc_cnt];
191193
m_ifc_map = new int[m_ext_cnt];
194+
m_ext_id_flgs = new int[m_ext_cnt];
192195
} catch (std::bad_alloc &e) {
193196
throw PluginError("not enough memory");
194197
}
@@ -311,6 +314,7 @@ void UnirecExporter::close()
311314

312315
m_basic_idx = -1;
313316
m_ifc_cnt = 0;
317+
delete [] m_ext_id_flgs;
314318
}
315319

316320
/**
@@ -359,10 +363,13 @@ int UnirecExporter::export_flow(const Flow &flow)
359363

360364
m_flows_seen++;
361365
uint64_t tmplt_dbits = 0; // templates dirty bits
366+
memset(m_ext_id_flgs, 0, sizeof(int) * m_ext_cnt); // in case one flow has multiple extension of same type
367+
int ext_processed_cnd = 0;
362368
while (ext != nullptr) {
363369
if (ext->m_ext_id >= static_cast<int>(m_ext_cnt)) {
364370
throw PluginError("encountered invalid extension id");
365371
}
372+
ext_processed_cnd++;
366373
int ifc_num = m_ifc_map[ext->m_ext_id];
367374
if (ifc_num >= 0) {
368375
tmplt_ptr = m_tmplts[ifc_num];
@@ -374,14 +381,24 @@ int UnirecExporter::export_flow(const Flow &flow)
374381
tmplt_dbits |= (1 << ifc_num);
375382
}
376383

384+
if (m_ext_id_flgs[ext->m_ext_id] == 1) {
385+
// send the previously filled unirec record
386+
trap_send(ifc_num, record_ptr, ur_rec_size(tmplt_ptr, record_ptr));
387+
} else {
388+
m_ext_id_flgs[ext->m_ext_id] = 1;
389+
}
390+
377391
fill_basic_flow(flow, tmplt_ptr, record_ptr);
378392
ext->fill_unirec(tmplt_ptr, record_ptr); /* Add each extension header into unirec record. */
379-
380-
trap_send(ifc_num, record_ptr, ur_rec_fixlen_size(tmplt_ptr) + ur_rec_varlen_size(tmplt_ptr, record_ptr));
381393
}
382394
ext = ext->m_next;
383395
}
384-
396+
//send the last record with all plugin data
397+
for (size_t ifc_num = 0; ifc_num < m_ifc_cnt && !(m_basic_idx >= 0) && ext_processed_cnd > 0; ifc_num++) {
398+
tmplt_ptr = m_tmplts[ifc_num];
399+
record_ptr = m_records[ifc_num];
400+
trap_send(ifc_num, record_ptr, ur_rec_size(tmplt_ptr, record_ptr));
401+
}
385402
return 0;
386403
}
387404

output/unirec.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class UnirecExporter : public OutputPlugin
202202
ur_template_t **m_tmplts; /**< Pointer to unirec templates. */
203203
void **m_records; /**< Pointer to unirec records. */
204204
size_t m_ifc_cnt; /**< Number of output interfaces. */
205+
int *m_ext_id_flgs; /** flags of used extension during export*/
205206

206207
bool m_eof; /**< Send eof when module exits. */
207208
bool m_odid; /**< Export ODID field instead of LINK_BIT_FIELD. */

0 commit comments

Comments
 (0)