Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #ifndef __included_vnet_flow_report_h__ |
| 16 | #define __included_vnet_flow_report_h__ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vnet/vnet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | #include <vnet/ethernet/ethernet.h> |
| 21 | #include <vnet/ethernet/packet.h> |
| 22 | #include <vnet/ip/ip_packet.h> |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 23 | #include <vnet/ip/ip_types.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | #include <vnet/ip/ip4_packet.h> |
| 25 | #include <vnet/ip/ip6_packet.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 26 | #include <vnet/udp/udp_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | #include <vlib/cli.h> |
| 28 | #include <vppinfra/error.h> |
| 29 | #include <vppinfra/hash.h> |
| 30 | #include <vppinfra/cache.h> |
| 31 | |
Ole Troan | a9855ef | 2018-05-02 12:45:10 +0200 | [diff] [blame] | 32 | #include <vnet/ipfix-export/ipfix_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 34 | /* ipfix field definitions for a particular report */ |
| 35 | typedef struct |
| 36 | { |
| 37 | u32 info_element; |
| 38 | u32 size; |
| 39 | } ipfix_report_element_t; |
| 40 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | /* Used to build the rewrite */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 42 | typedef struct |
| 43 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | ip4_header_t ip4; |
| 45 | udp_header_t udp; |
| 46 | ipfix_template_packet_t ipfix; |
| 47 | } ip4_ipfix_template_packet_t; |
| 48 | |
Paul Atkins | 5140484 | 2021-10-04 15:43:56 +0100 | [diff] [blame^] | 49 | /* Used to build the rewrite */ |
| 50 | typedef struct |
| 51 | { |
| 52 | ip6_header_t ip6; |
| 53 | udp_header_t udp; |
| 54 | ipfix_template_packet_t ipfix; |
| 55 | } ip6_ipfix_template_packet_t; |
| 56 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | struct flow_report_main; |
| 58 | struct flow_report; |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 59 | struct ipfix_exporter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 61 | typedef vlib_frame_t *(vnet_flow_data_callback_t) ( |
| 62 | struct flow_report_main *frm, struct ipfix_exporter *exp, |
| 63 | struct flow_report *, vlib_frame_t *, u32 *, u32); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 64 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 65 | typedef u8 *(vnet_flow_rewrite_callback_t) (struct ipfix_exporter *exp, |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 66 | struct flow_report *, |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 67 | u16, ipfix_report_element_t *elts, |
| 68 | u32 n_elts, u32 *stream_index); |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 69 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 70 | u8 *vnet_flow_rewrite_generic_callback (struct ipfix_exporter *exp, |
Paul Atkins | c2d476b | 2021-10-04 16:29:52 +0100 | [diff] [blame] | 71 | struct flow_report *, u16, |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 72 | ipfix_report_element_t *elts, |
| 73 | u32 n_elts, u32 *stream_index); |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 74 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 75 | typedef union |
| 76 | { |
| 77 | void *as_ptr; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 78 | uword as_uword; |
| 79 | } opaque_t; |
| 80 | |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 81 | /* |
| 82 | * A stream represents an IPFIX session to a destination. We can have |
| 83 | * multiple streams to the same destination, but each one has its own |
| 84 | * domain and source port. A stream has a sequence number for that |
| 85 | * session. A stream may contain multiple templates (i.e multiple for |
| 86 | * reports) and each stream also has its own template space. |
| 87 | * |
| 88 | * A stream has per thread state so that data packets can be built |
| 89 | * and send on multiple threads at the same time. |
| 90 | */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 91 | typedef struct |
| 92 | { |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 93 | u32 domain_id; |
| 94 | u32 sequence_number; |
| 95 | u16 src_port; |
| 96 | u16 n_reports; |
| 97 | u16 next_template_no; |
| 98 | } flow_report_stream_t; |
| 99 | |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 100 | /* |
| 101 | * For each flow_report we want to be able to build buffers/frames per thread. |
| 102 | */ |
| 103 | typedef struct |
| 104 | { |
| 105 | vlib_buffer_t *buffer; |
| 106 | vlib_frame_t *frame; |
| 107 | u16 next_data_offset; |
| 108 | /* |
| 109 | * We need this per stream as the IPFIX sequence number is the count of |
| 110 | * data record sent, not the count of packets with data records sent. |
| 111 | * See RFC 7011, Sec 3.1 |
| 112 | */ |
| 113 | u8 n_data_records; |
| 114 | } flow_report_per_thread_t; |
| 115 | |
| 116 | /* |
| 117 | * A flow report represents a group of fields that are to be exported. |
| 118 | * Each flow_report has an associated template that is generated when |
| 119 | * the flow_report is added. Each flow_report is associated with a |
| 120 | * stream, and multiple flow_reports can use the same stream. When |
| 121 | * adding a flow_report the keys for the stream are the domain_id |
| 122 | * and the source_port. |
| 123 | */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 124 | typedef struct flow_report |
| 125 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | /* ipfix rewrite, set by callback */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 127 | u8 *rewrite; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 128 | u16 template_id; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 129 | int data_record_size; |
| 130 | flow_report_per_thread_t *per_thread_data; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 131 | u32 stream_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | f64 last_template_sent; |
| 133 | int update_rewrite; |
| 134 | |
| 135 | /* Bitmap of fields to send */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 136 | uword *fields_to_send; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
| 138 | /* Opaque data */ |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 139 | opaque_t opaque; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 141 | /* build-the-template-packet rewrite callback */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | vnet_flow_rewrite_callback_t *rewrite_callback; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 143 | ipfix_report_element_t *report_elements; |
| 144 | u32 n_report_elements; |
| 145 | u32 *stream_indexp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
| 147 | /* Send-flow-data callback */ |
| 148 | vnet_flow_data_callback_t *flow_data_callback; |
| 149 | } flow_report_t; |
| 150 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 151 | /* |
| 152 | * The maximum number of ipfix exporters we can have at once |
| 153 | */ |
| 154 | #define IPFIX_EXPORTERS_MAX 5 |
| 155 | |
| 156 | /* |
| 157 | * We support multiple exporters. Each one has its own configured |
| 158 | * destination, and its own set of reports and streams. |
| 159 | */ |
| 160 | typedef struct ipfix_exporter |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 161 | { |
| 162 | flow_report_t *reports; |
| 163 | flow_report_stream_t *streams; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 165 | /* ipfix collector ip address, port, our ip address, fib index */ |
Paul Atkins | 5140484 | 2021-10-04 15:43:56 +0100 | [diff] [blame^] | 166 | ip_address_t ipfix_collector; |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 167 | u16 collector_port; |
Paul Atkins | 5140484 | 2021-10-04 15:43:56 +0100 | [diff] [blame^] | 168 | ip_address_t src_address; |
Juraj Sloboda | 86634f0 | 2016-07-01 06:12:58 -0700 | [diff] [blame] | 169 | u32 fib_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Juraj Sloboda | 5a49bb9 | 2016-07-07 03:23:15 -0700 | [diff] [blame] | 171 | /* Path MTU */ |
| 172 | u32 path_mtu; |
| 173 | |
| 174 | /* time interval in seconds after which to resend templates */ |
| 175 | u32 template_interval; |
| 176 | |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 177 | /* UDP checksum calculation enable flag */ |
| 178 | u8 udp_checksum; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * The amount of data needed for all the headers, prior to the first |
| 182 | * flowset (template or data or ...) This is mostly dependent on the |
| 183 | * L3 and L4 protocols in use. |
| 184 | */ |
| 185 | u32 all_headers_size; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 186 | } ipfix_exporter_t; |
| 187 | |
| 188 | typedef struct flow_report_main |
| 189 | { |
| 190 | /* |
| 191 | * A pool of the exporters. Entry 0 is always there for backwards |
| 192 | * compatability reasons. Entries 1 and above have to be created by |
| 193 | * the users. |
| 194 | */ |
| 195 | ipfix_exporter_t *exporters; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 196 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | /* time scale transform. Joy. */ |
| 198 | u32 unix_time_0; |
| 199 | f64 vlib_time_0; |
| 200 | |
| 201 | /* convenience variables */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 202 | vlib_main_t *vlib_main; |
| 203 | vnet_main_t *vnet_main; |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 204 | |
| 205 | u16 msg_id_base; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | } flow_report_main_t; |
| 207 | |
Juraj Sloboda | 837fbb1 | 2016-07-06 23:11:47 -0700 | [diff] [blame] | 208 | extern flow_report_main_t flow_report_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
Damjan Marion | b8abf87 | 2016-03-14 20:02:35 +0100 | [diff] [blame] | 210 | extern vlib_node_registration_t flow_report_process_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 212 | typedef struct |
| 213 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | vnet_flow_data_callback_t *flow_data_callback; |
| 215 | vnet_flow_rewrite_callback_t *rewrite_callback; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 216 | ipfix_report_element_t *report_elements; |
| 217 | u32 n_report_elements; |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 218 | opaque_t opaque; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | int is_add; |
| 220 | u32 domain_id; |
Juraj Sloboda | 483f668 | 2016-07-06 17:04:06 -0700 | [diff] [blame] | 221 | u16 src_port; |
Dave Barach | 2be4581 | 2018-05-13 08:50:25 -0400 | [diff] [blame] | 222 | u32 *stream_indexp; |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 223 | /* |
| 224 | * When adding a flow report, the index of the flow report is stored |
| 225 | * here on success. |
| 226 | */ |
| 227 | u32 flow_report_index; |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 228 | } vnet_flow_report_add_del_args_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 230 | int vnet_flow_report_add_del (ipfix_exporter_t *exp, |
| 231 | vnet_flow_report_add_del_args_t *a, |
| 232 | u16 *template_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 234 | clib_error_t *flow_report_add_del_error_to_clib_error (int error); |
Juraj Sloboda | 24648ad | 2016-09-06 04:43:52 -0700 | [diff] [blame] | 235 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 236 | void vnet_flow_reports_reset (ipfix_exporter_t *exp); |
Juraj Sloboda | 837fbb1 | 2016-07-06 23:11:47 -0700 | [diff] [blame] | 237 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 238 | void vnet_stream_reset (ipfix_exporter_t *exp, u32 stream_index); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 239 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 240 | int vnet_stream_change (ipfix_exporter_t *exp, u32 old_domain_id, |
| 241 | u16 old_src_port, u32 new_domain_id, u16 new_src_port); |
Juraj Sloboda | ffa652a | 2016-08-07 23:43:42 -0700 | [diff] [blame] | 242 | |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 243 | /* |
| 244 | * Search all the exporters for one that has a matching destination address. |
| 245 | */ |
Paul Atkins | 5140484 | 2021-10-04 15:43:56 +0100 | [diff] [blame^] | 246 | ipfix_exporter_t * |
| 247 | vnet_ipfix_exporter_lookup (const ip_address_t *ipfix_collector); |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 248 | |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 249 | /* |
| 250 | * Get the currently in use buffer for the given stream on the given core. |
| 251 | * If there is no current buffer then allocate a new one and return that. |
| 252 | * This is the buffer that data records should be written into. The offset |
| 253 | * currently in use is stored in the per-thread data for the stream and |
| 254 | * should be updated as new records are written in. |
| 255 | */ |
| 256 | vlib_buffer_t *vnet_ipfix_exp_get_buffer (vlib_main_t *vm, |
| 257 | ipfix_exporter_t *exp, |
| 258 | flow_report_t *fr, u32 thread_index); |
| 259 | |
| 260 | /* |
| 261 | * Send the provided buffer. At this stage the buffer should be populated |
| 262 | * with data records, with the offset in use stored in the stream per thread |
| 263 | * data. This func will fix up all the headers and then send the buffer. |
| 264 | */ |
| 265 | void vnet_ipfix_exp_send_buffer (vlib_main_t *vm, ipfix_exporter_t *exp, |
| 266 | flow_report_t *fr, |
| 267 | flow_report_stream_t *stream, |
| 268 | u32 thread_index, vlib_buffer_t *b0); |
| 269 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | #endif /* __included_vnet_flow_report_h__ */ |
Swarup Nayak | 6bcac06 | 2017-11-26 23:11:40 +0530 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * fd.io coding-style-patch-verification: ON |
| 274 | * |
| 275 | * Local Variables: |
| 276 | * eval: (c-set-style "gnu") |
| 277 | * End: |
| 278 | */ |