Skip to content

Commit 82584be

Browse files
committed
...
1 parent ead73c2 commit 82584be

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

extra_plugins/output/clickhouse/src/datatype.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ template <unsigned Precision> class ColumnDateTime64 : public clickhouse::Column
1818
ColumnDateTime64() : clickhouse::ColumnDateTime64(Precision) {}
1919
};
2020

21-
DataType type_from_ipfix(fds_iemgr_element_type type)
21+
DataType type_from_ipfix(const fds_iemgr_elem *elem)
2222
{
23-
switch (type) {
23+
// Special cases
24+
static constexpr uint32_t PEN_CESNET = 8057;
25+
static constexpr uint16_t ID_FLOWUUID = 1300;
26+
if (elem->scope->pen == PEN_CESNET && elem->id == ID_FLOWUUID) {
27+
return DataType::Uuid;
28+
}
29+
30+
// Otherwise
31+
switch (elem->data_type) {
2432
case FDS_ET_STRING: return DataType::String;
2533
case FDS_ET_SIGNED_8: return DataType::Int8;
2634
case FDS_ET_SIGNED_16: return DataType::Int16;
@@ -36,7 +44,7 @@ DataType type_from_ipfix(fds_iemgr_element_type type)
3644
case FDS_ET_DATE_TIME_MILLISECONDS: return DataType::DatetimeMillisecs;
3745
case FDS_ET_DATE_TIME_MICROSECONDS: return DataType::DatetimeMicrosecs;
3846
case FDS_ET_DATE_TIME_NANOSECONDS: return DataType::DatetimeNanosecs;
39-
default: throw Error("unsupported IPFIX data type {}", type);
47+
default: throw Error("unsupported IPFIX data type {}", elem->data_type);
4048
}
4149
}
4250

@@ -91,9 +99,9 @@ DataType find_common_type(const fds_iemgr_alias &alias)
9199
throw Error("alias \"{}\" has no sources", alias.name);
92100
}
93101

94-
DataType common_type = type_from_ipfix(alias.sources[0]->data_type);
102+
DataType common_type = type_from_ipfix(alias.sources[0]);
95103
for (size_t i = 1; i < alias.sources_cnt; i++) {
96-
common_type = unify_type(common_type, type_from_ipfix(alias.sources[i]->data_type));
104+
common_type = unify_type(common_type, type_from_ipfix(alias.sources[i]));
97105
}
98106
return common_type;
99107
}
@@ -199,6 +207,19 @@ static int64_t get_datetime64(fds_drec_field field)
199207
value = (static_cast<int64_t>(ts.tv_sec) * 1'000'000'000 + static_cast<int64_t>(ts.tv_nsec)) / Divisor;
200208
return value;
201209
}
210+
211+
static std::pair<uint64_t, uint64_t> get_uuid(fds_drec_field field)
212+
{
213+
if (field.size != 16) {
214+
throw Error("invalid uuid field size. expected 16, got {}", field.size);
215+
}
216+
217+
return {
218+
*reinterpret_cast<uint64_t *>(&field.data[0]),
219+
*reinterpret_cast<uint64_t *>(&field.data[8])
220+
};
221+
}
222+
202223
}
203224

204225
template<DataType> struct DataTypeTraits {};
@@ -299,6 +320,12 @@ template<> struct DataTypeTraits<DataType::DatetimeNanosecs> {
299320
static constexpr auto Getter = &getters::get_datetime64<1>;
300321
};
301322

323+
template<> struct DataTypeTraits<DataType::Uuid> {
324+
using ColumnType = clickhouse::ColumnUUID;
325+
static constexpr std::string_view ClickhouseTypeName = "UUID";
326+
static constexpr auto Getter = &getters::get_uuid;
327+
};
328+
302329
template <typename Func>
303330
static void visit(DataType type, Func func)
304331
{
@@ -319,6 +346,7 @@ static void visit(DataType type, Func func)
319346
case DataType::IPv4: func(DataTypeTraits<DataType::IPv4>{}); break;
320347
case DataType::IPv6: func(DataTypeTraits<DataType::IPv6>{}); break;
321348
case DataType::IP: func(DataTypeTraits<DataType::IP>{}); break;
349+
case DataType::Uuid: func(DataTypeTraits<DataType::Uuid>{}); break;
322350
case DataType::Invalid: throw std::runtime_error("invalid data type");
323351
}
324352
}

extra_plugins/output/clickhouse/src/datatype.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ enum class DataType {
4747
DatetimeMillisecs,
4848
DatetimeMicrosecs,
4949
DatetimeNanosecs,
50+
51+
Uuid,
5052
};
5153

5254
enum class DataTypeNullable {
@@ -115,6 +117,7 @@ template <> struct fmt::formatter<DataType> : formatter<string_view> {
115117
case DataType::DatetimeMicrosecs: return "DatetimeMicrosecs";
116118
case DataType::DatetimeMillisecs: return "DatetimeMillisecs";
117119
case DataType::DatetimeSecs: return "DatetimeSecs";
120+
case DataType::Uuid: return "Uuid";
118121
}
119122
return "???";
120123
}();
@@ -159,12 +162,12 @@ template <> struct fmt::formatter<fds_iemgr_element_type> : formatter<string_vie
159162
};
160163

161164
/**
162-
* @brief Get intermediary data type for a corresponding IPFIX element type
165+
* @brief Get intermediary data type for a corresponding IPFIX element
163166
*
164-
* @param type The IPFIX type
167+
* @param elem The IPFIX element definition
165168
* @return The intermediary data type
166169
*/
167-
DataType type_from_ipfix(fds_iemgr_element_type type);
170+
DataType type_from_ipfix(const fds_iemgr_elem *elem);
168171

169172
/**
170173
* @brief Get Clickhouse data type for the intermediary data type
@@ -196,7 +199,8 @@ using ValueVariant = std::variant<
196199
int64_t,
197200
IP4Addr,
198201
IP6Addr,
199-
std::string
202+
std::string,
203+
std::pair<uint64_t, uint64_t>
200204
>;
201205

202206
using GetterFn = std::function<void (fds_drec_field field, ValueVariant &value)>;

extra_plugins/output/clickhouse/src/plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static std::vector<ColumnCtx> prepare_columns(std::vector<Config::Column> &colum
5959

6060
if (std::holds_alternative<const fds_iemgr_elem *>(column_cfg.source)) {
6161
const fds_iemgr_elem *elem = std::get<const fds_iemgr_elem *>(column_cfg.source);
62-
type = type_from_ipfix(elem->data_type);
62+
type = type_from_ipfix(elem);
6363
column.extractor = make_extractor(*elem);
6464

6565
} else if (std::holds_alternative<const fds_iemgr_alias *>(column_cfg.source)) {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# List of output plugin to build and install
22
add_subdirectory(anonymization)
3-
add_subdirectory(dummy_enricher)
4-
add_subdirectory(asn)
5-
add_subdirectory(geoip)
3+
# add_subdirectory(dummy_enricher)
4+
# add_subdirectory(asn)
5+
# add_subdirectory(geoip)
66
add_subdirectory(uuid)

src/plugins/intermediate/uuid/uuid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ modifier_callback(const struct fds_drec *rec, struct ipx_modifier_output output[
6565

6666
uint32_t r = (uint32_t)rand();
6767
fill_uuid(instance->unix_time_ms, instance->counter, r, output->raw);
68+
output->length = 16;
6869
instance->counter++;
6970

7071
return IPX_OK;
@@ -357,7 +358,7 @@ IPX_API struct ipx_plugin_info ipx_plugin_info = {
357358
// Plugin type
358359
.type = IPX_PT_INTERMEDIATE,
359360
// Plugin identification name
360-
.name = "asn",
361+
.name = "uuid",
361362
// Brief description of plugin
362363
.dsc = "IPv4/IPv6 autonomous system number (ASN) module",
363364
// Configuration flags (reserved for future use)

0 commit comments

Comments
 (0)