blob: 1660a2bfdb1d25c3e61f068c290297d6828127df [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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/*
16 * flow_report.c
17 */
Paul Atkins19a5f232021-09-27 21:30:13 +010018#include <vppinfra/atomics.h>
Ole Troana9855ef2018-05-02 12:45:10 +020019#include <vnet/ipfix-export/flow_report.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/api_errno.h>
Florin Corasb040f982020-10-20 14:59:43 -070021#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
Juraj Sloboda837fbb12016-07-06 23:11:47 -070023flow_report_main_t flow_report_main;
24
Swarup Nayak6bcac062017-11-26 23:11:40 +053025static_always_inline u8
Paul Atkins9e827812021-09-22 08:15:03 +010026stream_index_valid (ipfix_exporter_t *exp, u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070027{
Paul Atkins9ec64492021-09-21 20:49:12 +010028 return index < vec_len (exp->streams) && exp->streams[index].domain_id != ~0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070029}
30
Swarup Nayak6bcac062017-11-26 23:11:40 +053031static_always_inline flow_report_stream_t *
Paul Atkins9e827812021-09-22 08:15:03 +010032add_stream (ipfix_exporter_t *exp)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070033{
Juraj Slobodaffa652a2016-08-07 23:43:42 -070034 u32 i;
Paul Atkins9ec64492021-09-21 20:49:12 +010035 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +010036 if (!stream_index_valid (exp, i))
Paul Atkins9ec64492021-09-21 20:49:12 +010037 return &exp->streams[i];
38 u32 index = vec_len (exp->streams);
39 vec_validate (exp->streams, index);
40 return &exp->streams[index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -070041}
42
Swarup Nayak6bcac062017-11-26 23:11:40 +053043static_always_inline void
Paul Atkins9e827812021-09-22 08:15:03 +010044delete_stream (ipfix_exporter_t *exp, u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070045{
Paul Atkins9ec64492021-09-21 20:49:12 +010046 ASSERT (index < vec_len (exp->streams));
47 ASSERT (exp->streams[index].domain_id != ~0);
48 exp->streams[index].domain_id = ~0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070049}
50
Swarup Nayak6bcac062017-11-26 23:11:40 +053051static i32
Paul Atkins9e827812021-09-22 08:15:03 +010052find_stream (ipfix_exporter_t *exp, u32 domain_id, u16 src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070053{
Swarup Nayak6bcac062017-11-26 23:11:40 +053054 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070055 u32 i;
Paul Atkins9ec64492021-09-21 20:49:12 +010056 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +010057 if (stream_index_valid (exp, i))
Swarup Nayak6bcac062017-11-26 23:11:40 +053058 {
Paul Atkins9ec64492021-09-21 20:49:12 +010059 stream = &exp->streams[i];
Swarup Nayak6bcac062017-11-26 23:11:40 +053060 if (domain_id == stream->domain_id)
61 {
62 if (src_port != stream->src_port)
63 return -2;
64 return i;
65 }
66 else if (src_port == stream->src_port)
67 {
68 return -2;
69 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070070 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070071 return -1;
72}
73
Swarup Nayak6bcac062017-11-26 23:11:40 +053074int
Paul Atkinsd747dd92021-09-22 14:56:17 +010075send_template_packet (flow_report_main_t *frm, ipfix_exporter_t *exp,
76 flow_report_t *fr, u32 *buffer_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
78 u32 bi0;
Swarup Nayak6bcac062017-11-26 23:11:40 +053079 vlib_buffer_t *b0;
Paul Atkins0ea14852021-10-04 17:29:54 +010080 ip4_ipfix_template_packet_t *tp4;
Swarup Nayak6bcac062017-11-26 23:11:40 +053081 ipfix_message_header_t *h;
Paul Atkins0ea14852021-10-04 17:29:54 +010082 ip4_header_t *ip4;
Swarup Nayak6bcac062017-11-26 23:11:40 +053083 udp_header_t *udp;
84 vlib_main_t *vm = frm->vlib_main;
85 flow_report_stream_t *stream;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87 ASSERT (buffer_indexp);
88
89 if (fr->update_rewrite || fr->rewrite == 0)
90 {
Paul Atkins9ec64492021-09-21 20:49:12 +010091 if (exp->ipfix_collector.as_u32 == 0 || exp->src_address.as_u32 == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +053092 {
93 vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
94 VLIB_NODE_STATE_DISABLED);
95 return -1;
96 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 vec_free (fr->rewrite);
98 fr->update_rewrite = 1;
99 }
100
101 if (fr->update_rewrite)
102 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100103 fr->rewrite = fr->rewrite_callback (
Paul Atkinsc2d476b2021-10-04 16:29:52 +0100104 exp, fr, exp->collector_port, fr->report_elements,
105 fr->n_report_elements, fr->stream_indexp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 fr->update_rewrite = 0;
107 }
108
109 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
110 return -1;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530111
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 b0 = vlib_get_buffer (vm, bi0);
113
Damjan Marion8934a042019-02-09 23:29:26 +0100114 ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm));
Swarup Nayak6bcac062017-11-26 23:11:40 +0530115
Dave Barach178cf492018-11-13 16:34:13 -0500116 clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 b0->current_data = 0;
118 b0->current_length = vec_len (fr->rewrite);
Damjan Mariondac03522018-02-01 15:30:13 +0100119 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100121 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
Paul Atkins0ea14852021-10-04 17:29:54 +0100123 tp4 = vlib_buffer_get_current (b0);
124 ip4 = (ip4_header_t *) &tp4->ip4;
125 udp = (udp_header_t *) (ip4 + 1);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530126 h = (ipfix_message_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Swarup Nayak6bcac062017-11-26 23:11:40 +0530128 /* FIXUP: message header export_time */
129 h->export_time = (u32)
130 (((f64) frm->unix_time_0) +
131 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
132 h->export_time = clib_host_to_net_u32 (h->export_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
Paul Atkins9ec64492021-09-21 20:49:12 +0100134 stream = &exp->streams[fr->stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700135
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 /* FIXUP: message header sequence_number. Templates do not increase it */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530137 h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139 /* FIXUP: udp length */
Paul Atkins0ea14852021-10-04 17:29:54 +0100140 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip4));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Paul Atkins9ec64492021-09-21 20:49:12 +0100142 if (exp->udp_checksum)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700143 {
144 /* RFC 7011 section 10.3.2. */
Paul Atkins0ea14852021-10-04 17:29:54 +0100145 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip4);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700146 if (udp->checksum == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530147 udp->checksum = 0xffff;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700148 }
149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 *buffer_indexp = bi0;
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700151
152 fr->last_template_sent = vlib_time_now (vm);
153
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 return 0;
155}
156
Dave Barach2be45812018-05-13 08:50:25 -0400157u8 *
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100158vnet_flow_rewrite_generic_callback (ipfix_exporter_t *exp, flow_report_t *fr,
Dave Barach2be45812018-05-13 08:50:25 -0400159 u16 collector_port,
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100160 ipfix_report_element_t *report_elts,
161 u32 n_elts, u32 *stream_indexp)
Dave Barach2be45812018-05-13 08:50:25 -0400162{
Paul Atkins0ea14852021-10-04 17:29:54 +0100163 ip4_header_t *ip4;
Dave Barach2be45812018-05-13 08:50:25 -0400164 udp_header_t *udp;
165 ipfix_message_header_t *h;
166 ipfix_set_header_t *s;
167 ipfix_template_header_t *t;
168 ipfix_field_specifier_t *f;
169 ipfix_field_specifier_t *first_field;
170 u8 *rewrite = 0;
Paul Atkins0ea14852021-10-04 17:29:54 +0100171 ip4_ipfix_template_packet_t *tp4;
Dave Barach2be45812018-05-13 08:50:25 -0400172 flow_report_stream_t *stream;
173 int i;
174 ipfix_report_element_t *ep;
175
176 ASSERT (stream_indexp);
177 ASSERT (n_elts);
178 ASSERT (report_elts);
179
Paul Atkins9ec64492021-09-21 20:49:12 +0100180 stream = &exp->streams[fr->stream_index];
Dave Barach2be45812018-05-13 08:50:25 -0400181 *stream_indexp = fr->stream_index;
182
183 /* allocate rewrite space */
184 vec_validate_aligned (rewrite,
185 sizeof (ip4_ipfix_template_packet_t)
186 + n_elts * sizeof (ipfix_field_specifier_t) - 1,
187 CLIB_CACHE_LINE_BYTES);
188
189 /* create the packet rewrite string */
Paul Atkins0ea14852021-10-04 17:29:54 +0100190 tp4 = (ip4_ipfix_template_packet_t *) rewrite;
191 ip4 = (ip4_header_t *) &tp4->ip4;
192 udp = (udp_header_t *) (ip4 + 1);
Dave Barach2be45812018-05-13 08:50:25 -0400193 h = (ipfix_message_header_t *) (udp + 1);
194 s = (ipfix_set_header_t *) (h + 1);
195 t = (ipfix_template_header_t *) (s + 1);
196 first_field = f = (ipfix_field_specifier_t *) (t + 1);
197
Paul Atkins0ea14852021-10-04 17:29:54 +0100198 ip4->ip_version_and_header_length = 0x45;
199 ip4->ttl = 254;
200 ip4->protocol = IP_PROTOCOL_UDP;
201 ip4->src_address.as_u32 = exp->src_address.as_u32;
202 ip4->dst_address.as_u32 = exp->ipfix_collector.as_u32;
Dave Barach2be45812018-05-13 08:50:25 -0400203 udp->src_port = clib_host_to_net_u16 (stream->src_port);
204 udp->dst_port = clib_host_to_net_u16 (collector_port);
Paul Atkins0ea14852021-10-04 17:29:54 +0100205 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip4));
Dave Barach2be45812018-05-13 08:50:25 -0400206
207 /* FIXUP LATER: message header export_time */
208 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
209
210 ep = report_elts;
211
212 for (i = 0; i < n_elts; i++)
213 {
214 f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size);
215 f++;
216 ep++;
217 }
218
219 /* Back to the template packet... */
Paul Atkins0ea14852021-10-04 17:29:54 +0100220 ip4 = (ip4_header_t *) &tp4->ip4;
221 udp = (udp_header_t *) (ip4 + 1);
Dave Barach2be45812018-05-13 08:50:25 -0400222
223 ASSERT (f - first_field);
224 /* Field count in this template */
225 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
226
227 /* set length in octets */
228 s->set_id_length =
229 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
230
231 /* message length in octets */
232 h->version_length = version_length ((u8 *) f - (u8 *) h);
233
Paul Atkins0ea14852021-10-04 17:29:54 +0100234 ip4->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip4);
235 ip4->checksum = ip4_header_checksum (ip4);
Dave Barach2be45812018-05-13 08:50:25 -0400236
237 return rewrite;
238}
239
Paul Atkins19a5f232021-09-27 21:30:13 +0100240vlib_buffer_t *
241vnet_ipfix_exp_get_buffer (vlib_main_t *vm, ipfix_exporter_t *exp,
242 flow_report_t *fr, u32 thread_index)
243{
244 u32 bi0;
245 vlib_buffer_t *b0;
246
247 if (fr->per_thread_data[thread_index].buffer)
248 return fr->per_thread_data[thread_index].buffer;
249
250 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
251 return NULL;
252
253 /* Initialize the buffer */
254 b0 = fr->per_thread_data[thread_index].buffer = vlib_get_buffer (vm, bi0);
255
256 b0->current_data = 0;
257 b0->current_length = exp->all_headers_size;
258 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
259 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
260 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
261 fr->per_thread_data[thread_index].next_data_offset = b0->current_length;
262
263 return b0;
264}
265
266/*
267 * Send a buffer that is mostly populated. Has flow records but needs some
268 * header fields updated.
269 */
270void
271vnet_ipfix_exp_send_buffer (vlib_main_t *vm, ipfix_exporter_t *exp,
272 flow_report_t *fr, flow_report_stream_t *stream,
273 u32 thread_index, vlib_buffer_t *b0)
274{
275 flow_report_main_t *frm = &flow_report_main;
276 vlib_frame_t *f;
Paul Atkins0ea14852021-10-04 17:29:54 +0100277 ip4_ipfix_template_packet_t *tp4;
Paul Atkins19a5f232021-09-27 21:30:13 +0100278 ipfix_set_header_t *s;
279 ipfix_message_header_t *h;
Paul Atkins0ea14852021-10-04 17:29:54 +0100280 ip4_header_t *ip4;
Paul Atkins19a5f232021-09-27 21:30:13 +0100281 udp_header_t *udp;
282
283 /* nothing to send */
284 if (fr->per_thread_data[thread_index].next_data_offset <=
285 exp->all_headers_size)
286 return;
287
Paul Atkins0ea14852021-10-04 17:29:54 +0100288 tp4 = vlib_buffer_get_current (b0);
289 ip4 = (ip4_header_t *) &tp4->ip4;
290 udp = (udp_header_t *) (ip4 + 1);
Paul Atkins19a5f232021-09-27 21:30:13 +0100291 h = (ipfix_message_header_t *) (udp + 1);
292 s = (ipfix_set_header_t *) (h + 1);
293
Paul Atkins0ea14852021-10-04 17:29:54 +0100294 ip4->ip_version_and_header_length = 0x45;
295 ip4->ttl = 254;
296 ip4->protocol = IP_PROTOCOL_UDP;
297 ip4->flags_and_fragment_offset = 0;
298 ip4->src_address.as_u32 = exp->src_address.as_u32;
299 ip4->dst_address.as_u32 = exp->ipfix_collector.as_u32;
Paul Atkins19a5f232021-09-27 21:30:13 +0100300 udp->src_port = clib_host_to_net_u16 (stream->src_port);
301 udp->dst_port = clib_host_to_net_u16 (exp->collector_port);
302 udp->checksum = 0;
303
304 /* FIXUP: message header export_time */
305 h->export_time =
306 (u32) (((f64) frm->unix_time_0) + (vlib_time_now (vm) - frm->vlib_time_0));
307 h->export_time = clib_host_to_net_u32 (h->export_time);
308 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
309
310 /*
311 * RFC 7011: Section 3.2
312 *
313 * Incremental sequence counter modulo 2^32 of all IPFIX Data Records
314 * sent in the current stream from the current Observation Domain by
315 * the Exporting Process
316 */
317 h->sequence_number =
318 clib_atomic_fetch_add (&stream->sequence_number,
319 fr->per_thread_data[thread_index].n_data_records);
320 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
321
322 /*
323 * For data records we use the template ID as the set ID.
324 * RFC 7011: 3.4.3
325 */
326 s->set_id_length = ipfix_set_id_length (
327 fr->template_id,
Paul Atkins0ea14852021-10-04 17:29:54 +0100328 b0->current_length - (sizeof (*ip4) + sizeof (*udp) + sizeof (*h)));
Paul Atkins19a5f232021-09-27 21:30:13 +0100329 h->version_length =
Paul Atkins0ea14852021-10-04 17:29:54 +0100330 version_length (b0->current_length - (sizeof (*ip4) + sizeof (*udp)));
Paul Atkins19a5f232021-09-27 21:30:13 +0100331
Paul Atkins0ea14852021-10-04 17:29:54 +0100332 ip4->length = clib_host_to_net_u16 (b0->current_length);
Paul Atkins19a5f232021-09-27 21:30:13 +0100333
Paul Atkins0ea14852021-10-04 17:29:54 +0100334 ip4->checksum = ip4_header_checksum (ip4);
335 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip4));
Paul Atkins19a5f232021-09-27 21:30:13 +0100336
337 if (exp->udp_checksum)
338 {
339 /* RFC 7011 section 10.3.2. */
Paul Atkins0ea14852021-10-04 17:29:54 +0100340 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip4);
Paul Atkins19a5f232021-09-27 21:30:13 +0100341 if (udp->checksum == 0)
342 udp->checksum = 0xffff;
343 }
344
Paul Atkins0ea14852021-10-04 17:29:54 +0100345 ASSERT (ip4_header_checksum_is_valid (ip4));
Paul Atkins19a5f232021-09-27 21:30:13 +0100346
347 /* Find or allocate a frame */
348 f = fr->per_thread_data[thread_index].frame;
349 if (PREDICT_FALSE (f == 0))
350 {
351 u32 *to_next;
352 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
353 fr->per_thread_data[thread_index].frame = f;
354 u32 bi0 = vlib_get_buffer_index (vm, b0);
355
356 /* Enqueue the buffer */
357 to_next = vlib_frame_vector_args (f);
358 to_next[0] = bi0;
359 f->n_vectors = 1;
360 }
361
362 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
363
364 fr->per_thread_data[thread_index].frame = NULL;
365 fr->per_thread_data[thread_index].buffer = NULL;
366 fr->per_thread_data[thread_index].next_data_offset = 0;
367}
368
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369static uword
370flow_report_process (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530371 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530373 flow_report_main_t *frm = &flow_report_main;
374 flow_report_t *fr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 u32 ip4_lookup_node_index;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530376 vlib_node_t *ip4_lookup_node;
377 vlib_frame_t *nf = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 u32 template_bi;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530379 u32 *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 int send_template;
Matthew Smithbaa18702021-04-28 11:48:39 -0500381 f64 now, wait_time;
382 f64 def_wait_time = 5.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 int rv;
384 uword event_type;
385 uword *event_data = 0;
386
387 /* Wait for Godot... */
388 vlib_process_wait_for_event_or_clock (vm, 1e9);
389 event_type = vlib_process_get_events (vm, &event_data);
390 if (event_type != 1)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530391 clib_warning ("bogus kickoff event received, %d", event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 vec_reset_length (event_data);
393
394 /* Enqueue pkts to ip4-lookup */
395 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
396 ip4_lookup_node_index = ip4_lookup_node->index;
397
Matthew Smithbaa18702021-04-28 11:48:39 -0500398 wait_time = def_wait_time;
399
Swarup Nayak6bcac062017-11-26 23:11:40 +0530400 while (1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 {
Matthew Smithbaa18702021-04-28 11:48:39 -0500402 vlib_process_wait_for_event_or_clock (vm, wait_time);
Dave Barach0f3b6802016-12-23 15:15:48 -0500403 event_type = vlib_process_get_events (vm, &event_data);
404 vec_reset_length (event_data);
Paul Atkinsd747dd92021-09-22 14:56:17 +0100405 ipfix_exporter_t *exp;
406 pool_foreach (exp, frm->exporters)
Paul Atkins292992e2021-09-21 21:08:14 +0100407 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Paul Atkinsd747dd92021-09-22 14:56:17 +0100409 /* 5s delay by default, possibly reduced by template intervals */
410 wait_time = def_wait_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Paul Atkinsd747dd92021-09-22 14:56:17 +0100412 vec_foreach (fr, exp->reports)
Paul Atkins292992e2021-09-21 21:08:14 +0100413 {
Paul Atkinsd747dd92021-09-22 14:56:17 +0100414 f64 next_template;
415 now = vlib_time_now (vm);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530416
Paul Atkinsd747dd92021-09-22 14:56:17 +0100417 /* Need to send a template packet? */
418 send_template =
419 now > (fr->last_template_sent + exp->template_interval);
420 send_template += fr->last_template_sent == 0;
421 template_bi = ~0;
422 rv = 0;
423
424 if (send_template)
425 rv = send_template_packet (frm, exp, fr, &template_bi);
426
427 if (rv < 0)
428 continue;
429
430 /*
431 * decide if template should be sent sooner than current wait
432 * time
433 */
434 next_template =
435 (fr->last_template_sent + exp->template_interval) - now;
436 wait_time = clib_min (wait_time, next_template);
437
438 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
439 nf->n_vectors = 0;
440 to_next = vlib_frame_vector_args (nf);
441
442 if (template_bi != ~0)
443 {
444 to_next[0] = template_bi;
445 to_next++;
446 nf->n_vectors++;
447 }
448
449 nf = fr->flow_data_callback (frm, exp, fr, nf, to_next,
450 ip4_lookup_node_index);
451 if (nf)
452 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
453 }
Paul Atkins292992e2021-09-21 21:08:14 +0100454 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 }
456
Swarup Nayak6bcac062017-11-26 23:11:40 +0530457 return 0; /* not so much */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458}
459
Swarup Nayak6bcac062017-11-26 23:11:40 +0530460/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461VLIB_REGISTER_NODE (flow_report_process_node) = {
462 .function = flow_report_process,
463 .type = VLIB_NODE_TYPE_PROCESS,
464 .name = "flow-report-process",
465};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530466/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
Swarup Nayak6bcac062017-11-26 23:11:40 +0530468int
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100469vnet_flow_report_add_del (ipfix_exporter_t *exp,
470 vnet_flow_report_add_del_args_t *a, u16 *template_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471{
472 int i;
473 int found_index = ~0;
474 flow_report_t *fr;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530475 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700476 u32 si;
Paul Atkins19a5f232021-09-27 21:30:13 +0100477 vlib_thread_main_t *tm = &vlib_thread_main;
478 flow_report_main_t *frm = &flow_report_main;
479 vlib_main_t *vm = frm->vlib_main;
480 int size;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530481
Paul Atkins9e827812021-09-22 08:15:03 +0100482 si = find_stream (exp, a->domain_id, a->src_port);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700483 if (si == -2)
484 return VNET_API_ERROR_INVALID_VALUE;
485 if (si == -1 && a->is_add == 0)
486 return VNET_API_ERROR_NO_SUCH_ENTRY;
487
Paul Atkins9ec64492021-09-21 20:49:12 +0100488 for (i = 0; i < vec_len (exp->reports); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100490 fr = vec_elt_at_index (exp->reports, i);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700491 if (fr->opaque.as_uword == a->opaque.as_uword
Swarup Nayak6bcac062017-11-26 23:11:40 +0530492 && fr->rewrite_callback == a->rewrite_callback
493 && fr->flow_data_callback == a->flow_data_callback)
494 {
495 found_index = i;
496 if (template_id)
497 *template_id = fr->template_id;
498 break;
499 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 }
501
502 if (a->is_add == 0)
503 {
504 if (found_index != ~0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530505 {
Paul Atkins19a5f232021-09-27 21:30:13 +0100506 for (int i = 0;
507 i < vec_len (exp->reports[found_index].per_thread_data); i++)
508 {
509 u32 bi;
510 if (exp->reports[found_index].per_thread_data[i].buffer)
511 {
512 bi = vlib_get_buffer_index (
513 vm, exp->reports[found_index].per_thread_data[i].buffer);
514 vlib_buffer_free (vm, &bi, 1);
515 }
516 }
517 vec_free (exp->reports[found_index].per_thread_data);
518
Paul Atkins9ec64492021-09-21 20:49:12 +0100519 vec_delete (exp->reports, 1, found_index);
520 stream = &exp->streams[si];
Swarup Nayak6bcac062017-11-26 23:11:40 +0530521 stream->n_reports--;
522 if (stream->n_reports == 0)
Paul Atkins9e827812021-09-22 08:15:03 +0100523 delete_stream (exp, si);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530524 return 0;
525 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 return VNET_API_ERROR_NO_SUCH_ENTRY;
527 }
528
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700529 if (found_index != ~0)
530 return VNET_API_ERROR_VALUE_EXIST;
531
532 if (si == -1)
533 {
Paul Atkins9e827812021-09-22 08:15:03 +0100534 stream = add_stream (exp);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700535 stream->domain_id = a->domain_id;
536 stream->src_port = a->src_port;
537 stream->sequence_number = 0;
538 stream->n_reports = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100539 si = stream - exp->streams;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700540 }
541 else
Paul Atkins9ec64492021-09-21 20:49:12 +0100542 stream = &exp->streams[si];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700543
544 stream->n_reports++;
545
Paul Atkins9ec64492021-09-21 20:49:12 +0100546 vec_add2 (exp->reports, fr, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700548 fr->stream_index = si;
549 fr->template_id = 256 + stream->next_template_no;
550 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 fr->update_rewrite = 1;
552 fr->opaque = a->opaque;
553 fr->rewrite_callback = a->rewrite_callback;
554 fr->flow_data_callback = a->flow_data_callback;
Dave Barach2be45812018-05-13 08:50:25 -0400555 fr->report_elements = a->report_elements;
556 fr->n_report_elements = a->n_report_elements;
557 fr->stream_indexp = a->stream_indexp;
Paul Atkins19a5f232021-09-27 21:30:13 +0100558 vec_validate (fr->per_thread_data, tm->n_threads);
559 /* Store the flow_report index back in the args struct */
560 a->flow_report_index = fr - exp->reports;
561
562 size = 0;
563 for (int i = 0; i < fr->n_report_elements; i++)
564 size += fr->report_elements[i].size;
565 fr->data_record_size = size;
Ole Troan5c749732017-03-13 13:39:52 +0100566 if (template_id)
567 *template_id = fr->template_id;
568
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 return 0;
570}
571
Swarup Nayak6bcac062017-11-26 23:11:40 +0530572clib_error_t *
573flow_report_add_del_error_to_clib_error (int error)
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700574{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530575 switch (error)
576 {
577 case 0:
578 return 0;
579 case VNET_API_ERROR_NO_SUCH_ENTRY:
580 return clib_error_return (0, "Flow report not found");
581 case VNET_API_ERROR_VALUE_EXIST:
582 return clib_error_return (0, "Flow report already exists");
583 case VNET_API_ERROR_INVALID_VALUE:
584 return clib_error_return (0, "Expecting either still unused values "
585 "for both domain_id and src_port "
586 "or already used values for both fields");
587 default:
588 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
589 error);
590 }
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700591}
592
Swarup Nayak6bcac062017-11-26 23:11:40 +0530593void
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100594vnet_flow_reports_reset (ipfix_exporter_t *exp)
Juraj Sloboda618ab082016-07-06 06:11:00 -0700595{
596 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700597 u32 i;
598
Paul Atkins9ec64492021-09-21 20:49:12 +0100599 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +0100600 if (stream_index_valid (exp, i))
Paul Atkins9ec64492021-09-21 20:49:12 +0100601 exp->streams[i].sequence_number = 0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700602
Paul Atkins9ec64492021-09-21 20:49:12 +0100603 vec_foreach (fr, exp->reports)
Paul Atkins292992e2021-09-21 21:08:14 +0100604 {
605 fr->update_rewrite = 1;
606 fr->last_template_sent = 0;
607 }
Juraj Sloboda618ab082016-07-06 06:11:00 -0700608}
609
Swarup Nayak6bcac062017-11-26 23:11:40 +0530610void
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100611vnet_stream_reset (ipfix_exporter_t *exp, u32 stream_index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700612{
613 flow_report_t *fr;
614
Paul Atkins9ec64492021-09-21 20:49:12 +0100615 exp->streams[stream_index].sequence_number = 0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700616
Paul Atkins9ec64492021-09-21 20:49:12 +0100617 vec_foreach (fr, exp->reports)
618 if (exp->reports->stream_index == stream_index)
Paul Atkins292992e2021-09-21 21:08:14 +0100619 {
620 fr->update_rewrite = 1;
621 fr->last_template_sent = 0;
622 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700623}
624
Swarup Nayak6bcac062017-11-26 23:11:40 +0530625int
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100626vnet_stream_change (ipfix_exporter_t *exp, u32 old_domain_id, u16 old_src_port,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530627 u32 new_domain_id, u16 new_src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700628{
Paul Atkins9e827812021-09-22 08:15:03 +0100629 i32 stream_index = find_stream (exp, old_domain_id, old_src_port);
Paul Atkins9ec64492021-09-21 20:49:12 +0100630
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700631 if (stream_index < 0)
632 return 1;
Paul Atkins9ec64492021-09-21 20:49:12 +0100633 flow_report_stream_t *stream = &exp->streams[stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700634 stream->domain_id = new_domain_id;
635 stream->src_port = new_src_port;
636 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100637 vnet_stream_reset (exp, stream_index);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700638 return 0;
639}
640
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641static clib_error_t *
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700642set_ipfix_exporter_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530643 unformat_input_t * input,
644 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530646 flow_report_main_t *frm = &flow_report_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647 ip4_address_t collector, src;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700648 u16 collector_port = UDP_DST_PORT_ipfix;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700649 u32 fib_id;
650 u32 fib_index = ~0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530651
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 collector.as_u32 = 0;
653 src.as_u32 = 0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530654 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700655 u32 template_interval = 20;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700656 u8 udp_checksum = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100657 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Swarup Nayak6bcac062017-11-26 23:11:40 +0530659 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
660 {
661 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
662 ;
Elias Rudberg2dca1802020-05-27 01:03:46 +0200663 else if (unformat (input, "port %U", unformat_udp_port,
664 &collector_port))
Swarup Nayak6bcac062017-11-26 23:11:40 +0530665 ;
666 else if (unformat (input, "src %U", unformat_ip4_address, &src))
667 ;
668 else if (unformat (input, "fib-id %u", &fib_id))
669 {
670 ip4_main_t *im = &ip4_main;
671 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
672 if (!p)
673 return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
674 fib_index = p[0];
675 }
676 else if (unformat (input, "path-mtu %u", &path_mtu))
677 ;
678 else if (unformat (input, "template-interval %u", &template_interval))
679 ;
680 else if (unformat (input, "udp-checksum"))
681 udp_checksum = 1;
682 else
683 break;
684 }
685
Ole Troan5c749732017-03-13 13:39:52 +0100686 if (collector.as_u32 != 0 && src.as_u32 == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 return clib_error_return (0, "src address required");
688
Swarup Nayak6bcac062017-11-26 23:11:40 +0530689 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
690 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700691
692 if (path_mtu < 68)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530693 return clib_error_return (0, "too small path-mtu value, minimum is 68");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700694
Paul Atkins19a5f232021-09-27 21:30:13 +0100695 /* Calculate how much header data we need. */
696 exp->all_headers_size = sizeof (ip4_header_t) + sizeof (udp_header_t) +
697 sizeof (ipfix_message_header_t) +
698 sizeof (ipfix_set_header_t);
699
Juraj Sloboda618ab082016-07-06 06:11:00 -0700700 /* Reset report streams if we are reconfiguring IP addresses */
Paul Atkins9ec64492021-09-21 20:49:12 +0100701 if (exp->ipfix_collector.as_u32 != collector.as_u32 ||
702 exp->src_address.as_u32 != src.as_u32 ||
703 exp->collector_port != collector_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100704 vnet_flow_reports_reset (exp);
Juraj Sloboda618ab082016-07-06 06:11:00 -0700705
Paul Atkins9ec64492021-09-21 20:49:12 +0100706 exp->ipfix_collector.as_u32 = collector.as_u32;
707 exp->collector_port = collector_port;
708 exp->src_address.as_u32 = src.as_u32;
709 exp->fib_index = fib_index;
710 exp->path_mtu = path_mtu;
711 exp->template_interval = template_interval;
712 exp->udp_checksum = udp_checksum;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530713
Ole Troan5c749732017-03-13 13:39:52 +0100714 if (collector.as_u32)
Paul Atkins9ec64492021-09-21 20:49:12 +0100715 vlib_cli_output (vm,
716 "Collector %U, src address %U, "
Swarup Nayak6bcac062017-11-26 23:11:40 +0530717 "fib index %d, path MTU %u, "
718 "template resend interval %us, "
719 "udp checksum %s",
Paul Atkins9ec64492021-09-21 20:49:12 +0100720 format_ip4_address, exp->ipfix_collector,
721 format_ip4_address, exp->src_address, fib_index, path_mtu,
722 template_interval, udp_checksum ? "enabled" : "disabled");
Ole Troan5c749732017-03-13 13:39:52 +0100723 else
724 vlib_cli_output (vm, "IPFIX Collector is disabled");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700725
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 /* Turn on the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530727 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 return 0;
729}
730
Swarup Nayak6bcac062017-11-26 23:11:40 +0530731/* *INDENT-OFF* */
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700732VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
733 .path = "set ipfix exporter",
734 .short_help = "set ipfix exporter "
735 "collector <ip4-address> [port <port>] "
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700736 "src <ip4-address> [fib-id <fib-id>] "
737 "[path-mtu <path-mtu>] "
Ignas Baciusf3a522f2020-02-18 12:33:09 +0200738 "[template-interval <template-interval>] "
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700739 "[udp-checksum]",
740 .function = set_ipfix_exporter_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530742/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743
Dave Barach0f3b6802016-12-23 15:15:48 -0500744
745static clib_error_t *
746ipfix_flush_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530747 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach0f3b6802016-12-23 15:15:48 -0500748{
749 /* poke the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530750 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Dave Barach0f3b6802016-12-23 15:15:48 -0500751 return 0;
752}
753
Swarup Nayak6bcac062017-11-26 23:11:40 +0530754/* *INDENT-OFF* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500755VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
756 .path = "ipfix flush",
757 .short_help = "flush the current ipfix data [for make test]",
758 .function = ipfix_flush_command_fn,
759};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530760/* *INDENT-ON* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500761
Swarup Nayak6bcac062017-11-26 23:11:40 +0530762static clib_error_t *
763flow_report_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530765 flow_report_main_t *frm = &flow_report_main;
Paul Atkins9ec64492021-09-21 20:49:12 +0100766 ipfix_exporter_t *exp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
768 frm->vlib_main = vm;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530769 frm->vnet_main = vnet_get_main ();
770 frm->unix_time_0 = time (0);
771 frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
Paul Atkins9ec64492021-09-21 20:49:12 +0100772 /*
773 * Make sure that we can always access the first exporter for
774 * backwards compatibility reasons.
775 */
776 pool_alloc (frm->exporters, IPFIX_EXPORTERS_MAX);
777 pool_get (frm->exporters, exp);
778 /* Verify that this is at index 0 */
779 ASSERT (frm->exporters == exp);
780 exp->fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 return 0;
782}
783
Dave Barachf8d50682019-05-14 18:01:44 -0400784VLIB_INIT_FUNCTION (flow_report_init);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530785/*
786 * fd.io coding-style-patch-verification: ON
787 *
788 * Local Variables:
789 * eval: (c-set-style "gnu")
790 * End:
791 */