|
| 1 | +/** |
| 2 | + * \file flow_hash.hpp |
| 3 | + * \brief Plugin for parsing flow_hash traffic. |
| 4 | + * \author Jakub Antonín Štigler [email protected] |
| 5 | + * \date 2023 |
| 6 | + */ |
| 7 | +/* |
| 8 | + * Copyright (C) 2023 CESNET |
| 9 | + * |
| 10 | + * LICENSE TERMS |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or without |
| 13 | + * modification, are permitted provided that the following conditions |
| 14 | + * are met: |
| 15 | + * 1. Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in |
| 19 | + * the documentation and/or other materials provided with the |
| 20 | + * distribution. |
| 21 | + * 3. Neither the name of the Company nor the names of its contributors |
| 22 | + * may be used to endorse or promote products derived from this |
| 23 | + * software without specific prior written permission. |
| 24 | + * |
| 25 | + * |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef IPXP_PROCESS_FLOW_HASH_HPP |
| 30 | +#define IPXP_PROCESS_FLOW_HASH_HPP |
| 31 | + |
| 32 | +#include <cstring> |
| 33 | + |
| 34 | +#ifdef WITH_NEMEA |
| 35 | + #include "fields.h" |
| 36 | +#endif |
| 37 | + |
| 38 | +#include <string> |
| 39 | +#include <sstream> |
| 40 | + |
| 41 | +#include <ipfixprobe/process.hpp> |
| 42 | +#include <ipfixprobe/flowifc.hpp> |
| 43 | +#include <ipfixprobe/packet.hpp> |
| 44 | +#include <ipfixprobe/ipfix-elements.hpp> |
| 45 | +#include <ipfixprobe/byte-utils.hpp> |
| 46 | + |
| 47 | +namespace ipxp { |
| 48 | + |
| 49 | +#define FLOW_HASH_UNIREC_TEMPLATE "FLOW_ID" |
| 50 | + |
| 51 | +UR_FIELDS ( |
| 52 | + uint64 FLOW_ID |
| 53 | +) |
| 54 | + |
| 55 | +/** |
| 56 | + * \brief Flow record extension header for storing parsed FLOW_HASH data. |
| 57 | + */ |
| 58 | +struct RecordExtFLOW_HASH : public RecordExt { |
| 59 | + static int REGISTERED_ID; |
| 60 | + |
| 61 | + // Value in host byte order |
| 62 | + uint64_t flow_hash; |
| 63 | + |
| 64 | + RecordExtFLOW_HASH() : RecordExt(REGISTERED_ID) |
| 65 | + { |
| 66 | + flow_hash = 0; |
| 67 | + } |
| 68 | + |
| 69 | +#ifdef WITH_NEMEA |
| 70 | + void fill_unirec(ur_template_t *tmplt, void *record) override |
| 71 | + { |
| 72 | + ur_set(tmplt, record, F_FLOW_ID, flow_hash); |
| 73 | + } |
| 74 | + |
| 75 | + const char *get_unirec_tmplt() const |
| 76 | + { |
| 77 | + return FLOW_HASH_UNIREC_TEMPLATE; |
| 78 | + } |
| 79 | +#endif |
| 80 | + |
| 81 | + int fill_ipfix(uint8_t *buffer, int size) override |
| 82 | + { |
| 83 | + constexpr int LEN = sizeof(flow_hash); |
| 84 | + |
| 85 | + if (size < LEN) { |
| 86 | + return -1; |
| 87 | + } |
| 88 | + |
| 89 | + // value is converted from host byte-order to network byte-order |
| 90 | + *reinterpret_cast<decltype(flow_hash) *>(buffer) = swap_uint64(flow_hash); |
| 91 | + |
| 92 | + return LEN; |
| 93 | + } |
| 94 | + |
| 95 | + const char **get_ipfix_tmplt() const |
| 96 | + { |
| 97 | + static const char *ipfix_template[] = { |
| 98 | + IPFIX_FLOW_HASH_TEMPLATE(IPFIX_FIELD_NAMES) |
| 99 | + NULL |
| 100 | + }; |
| 101 | + return ipfix_template; |
| 102 | + } |
| 103 | + |
| 104 | + std::string get_text() const |
| 105 | + { |
| 106 | + std::ostringstream out; |
| 107 | + out << std::hex << "flow_id=\"" << flow_hash << '"'; |
| 108 | + |
| 109 | + return out.str(); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * \brief Process plugin for parsing FLOW_HASH packets. |
| 115 | + */ |
| 116 | +class FLOW_HASHPlugin : public ProcessPlugin |
| 117 | +{ |
| 118 | +public: |
| 119 | + FLOW_HASHPlugin(); |
| 120 | + ~FLOW_HASHPlugin(); |
| 121 | + void init(const char *params); |
| 122 | + void close(); |
| 123 | + OptionsParser *get_parser() const { return new OptionsParser("flow_hash", "Export flow hash as flow id"); } |
| 124 | + std::string get_name() const { return "flow_hash"; } |
| 125 | + RecordExt *get_ext() const { return new RecordExtFLOW_HASH(); } |
| 126 | + ProcessPlugin *copy(); |
| 127 | + |
| 128 | + int post_create(Flow &rec, const Packet &pkt); |
| 129 | +}; |
| 130 | + |
| 131 | +} |
| 132 | +#endif /* IPXP_PROCESS_FLOW_HASH_HPP */ |
| 133 | + |
0 commit comments