blob: cf23ccd7815126abe23432bbe6e0e31b35293059 [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;
Paul Atkinse494ad12021-10-05 08:51:48 +010081 ip6_ipfix_template_packet_t *tp6;
Swarup Nayak6bcac062017-11-26 23:11:40 +053082 ipfix_message_header_t *h;
Paul Atkins0ea14852021-10-04 17:29:54 +010083 ip4_header_t *ip4;
Paul Atkinse494ad12021-10-05 08:51:48 +010084 ip6_header_t *ip6;
85 void *ip;
Swarup Nayak6bcac062017-11-26 23:11:40 +053086 udp_header_t *udp;
87 vlib_main_t *vm = frm->vlib_main;
88 flow_report_stream_t *stream;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
90 ASSERT (buffer_indexp);
91
92 if (fr->update_rewrite || fr->rewrite == 0)
93 {
Paul Atkins51404842021-10-04 15:43:56 +010094 if (ip_address_is_zero (&exp->ipfix_collector) ||
95 ip_address_is_zero (&exp->src_address))
Swarup Nayak6bcac062017-11-26 23:11:40 +053096 {
97 vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
98 VLIB_NODE_STATE_DISABLED);
99 return -1;
100 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 vec_free (fr->rewrite);
102 fr->update_rewrite = 1;
103 }
104
105 if (fr->update_rewrite)
106 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100107 fr->rewrite = fr->rewrite_callback (
Paul Atkinsc2d476b2021-10-04 16:29:52 +0100108 exp, fr, exp->collector_port, fr->report_elements,
109 fr->n_report_elements, fr->stream_indexp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 fr->update_rewrite = 0;
111 }
112
113 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
114 return -1;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530115
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 b0 = vlib_get_buffer (vm, bi0);
117
Damjan Marion8934a042019-02-09 23:29:26 +0100118 ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm));
Swarup Nayak6bcac062017-11-26 23:11:40 +0530119
Dave Barach178cf492018-11-13 16:34:13 -0500120 clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 b0->current_data = 0;
122 b0->current_length = vec_len (fr->rewrite);
Damjan Mariondac03522018-02-01 15:30:13 +0100123 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100125 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Paul Atkinse494ad12021-10-05 08:51:48 +0100127 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
128 {
129 tp4 = vlib_buffer_get_current (b0);
130 ip4 = (ip4_header_t *) &tp4->ip4;
131 ip = ip4;
132 udp = (udp_header_t *) (ip4 + 1);
133 }
134 else
135 {
136 tp6 = vlib_buffer_get_current (b0);
137 ip6 = (ip6_header_t *) &tp6->ip6;
138 ip = ip6;
139 udp = (udp_header_t *) (ip6 + 1);
140 }
Swarup Nayak6bcac062017-11-26 23:11:40 +0530141 h = (ipfix_message_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Swarup Nayak6bcac062017-11-26 23:11:40 +0530143 /* FIXUP: message header export_time */
144 h->export_time = (u32)
145 (((f64) frm->unix_time_0) +
146 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
147 h->export_time = clib_host_to_net_u32 (h->export_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Paul Atkins9ec64492021-09-21 20:49:12 +0100149 stream = &exp->streams[fr->stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700150
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 /* FIXUP: message header sequence_number. Templates do not increase it */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530152 h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
154 /* FIXUP: udp length */
Paul Atkinse494ad12021-10-05 08:51:48 +0100155 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
156 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip4));
157 else
158 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip6));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Paul Atkinse494ad12021-10-05 08:51:48 +0100160 if (exp->udp_checksum || ip_addr_version (&exp->ipfix_collector) == AF_IP6)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700161 {
162 /* RFC 7011 section 10.3.2. */
Paul Atkinse494ad12021-10-05 08:51:48 +0100163
164 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
165 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
166 else
167 {
168 int bogus = 0;
169 udp->checksum =
170 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip, &bogus);
171 }
172
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700173 if (udp->checksum == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530174 udp->checksum = 0xffff;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700175 }
176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 *buffer_indexp = bi0;
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700178
179 fr->last_template_sent = vlib_time_now (vm);
180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 return 0;
182}
183
Paul Atkinse494ad12021-10-05 08:51:48 +0100184u32 always_inline
185ipfix_write_headers (ipfix_exporter_t *exp, void *data, void **ip,
186 udp_header_t **udp, u32 len)
187{
188 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
189 {
190 ip4_ipfix_template_packet_t *tp4;
191 ip4_header_t *ip4;
192
193 tp4 = (ip4_ipfix_template_packet_t *) data;
194 ip4 = (ip4_header_t *) &tp4->ip4;
195 ip4->ip_version_and_header_length = 0x45;
196 ip4->ttl = 254;
197 ip4->protocol = IP_PROTOCOL_UDP;
198 ip4->flags_and_fragment_offset = 0;
199 ip4->src_address.as_u32 = exp->src_address.ip.ip4.as_u32;
200 ip4->dst_address.as_u32 = exp->ipfix_collector.ip.ip4.as_u32;
201 *ip = ip4;
202 *udp = (udp_header_t *) (ip4 + 1);
203
204 (*udp)->length = clib_host_to_net_u16 (len - sizeof (*ip4));
205 return sizeof (*ip4);
206 }
207 else
208 {
209 ip6_ipfix_template_packet_t *tp6;
210 ip6_header_t *ip6;
211
212 tp6 = (ip6_ipfix_template_packet_t *) data;
213 ip6 = (ip6_header_t *) &tp6->ip6;
214 ip6->ip_version_traffic_class_and_flow_label =
215 clib_host_to_net_u32 (6 << 28);
216 ip6->hop_limit = 254;
217 ip6->protocol = IP_PROTOCOL_UDP;
218 ip6->src_address = exp->src_address.ip.ip6;
219 ip6->dst_address = exp->ipfix_collector.ip.ip6;
220 *ip = ip6;
221 *udp = (udp_header_t *) (ip6 + 1);
222 (*udp)->length = clib_host_to_net_u16 (len - sizeof (*ip6));
223 return sizeof (*ip6);
224 }
225}
226
Dave Barach2be45812018-05-13 08:50:25 -0400227u8 *
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100228vnet_flow_rewrite_generic_callback (ipfix_exporter_t *exp, flow_report_t *fr,
Dave Barach2be45812018-05-13 08:50:25 -0400229 u16 collector_port,
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100230 ipfix_report_element_t *report_elts,
231 u32 n_elts, u32 *stream_indexp)
Dave Barach2be45812018-05-13 08:50:25 -0400232{
Paul Atkins0ea14852021-10-04 17:29:54 +0100233 ip4_header_t *ip4;
Paul Atkinse494ad12021-10-05 08:51:48 +0100234 ip6_header_t *ip6;
235 void *ip;
Dave Barach2be45812018-05-13 08:50:25 -0400236 udp_header_t *udp;
237 ipfix_message_header_t *h;
238 ipfix_set_header_t *s;
239 ipfix_template_header_t *t;
240 ipfix_field_specifier_t *f;
241 ipfix_field_specifier_t *first_field;
242 u8 *rewrite = 0;
Dave Barach2be45812018-05-13 08:50:25 -0400243 flow_report_stream_t *stream;
244 int i;
245 ipfix_report_element_t *ep;
Paul Atkinse494ad12021-10-05 08:51:48 +0100246 u32 size;
Dave Barach2be45812018-05-13 08:50:25 -0400247
248 ASSERT (stream_indexp);
249 ASSERT (n_elts);
250 ASSERT (report_elts);
251
Paul Atkins9ec64492021-09-21 20:49:12 +0100252 stream = &exp->streams[fr->stream_index];
Dave Barach2be45812018-05-13 08:50:25 -0400253 *stream_indexp = fr->stream_index;
254
Paul Atkinse494ad12021-10-05 08:51:48 +0100255 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
256 size = sizeof (ip4_ipfix_template_packet_t);
257 else
258 size = sizeof (ip6_ipfix_template_packet_t);
Dave Barach2be45812018-05-13 08:50:25 -0400259 /* allocate rewrite space */
260 vec_validate_aligned (rewrite,
Paul Atkinse494ad12021-10-05 08:51:48 +0100261 size + n_elts * sizeof (ipfix_field_specifier_t) - 1,
Dave Barach2be45812018-05-13 08:50:25 -0400262 CLIB_CACHE_LINE_BYTES);
263
264 /* create the packet rewrite string */
Paul Atkinse494ad12021-10-05 08:51:48 +0100265 ipfix_write_headers (exp, rewrite, &ip, &udp, vec_len (rewrite));
266
Dave Barach2be45812018-05-13 08:50:25 -0400267 h = (ipfix_message_header_t *) (udp + 1);
268 s = (ipfix_set_header_t *) (h + 1);
269 t = (ipfix_template_header_t *) (s + 1);
270 first_field = f = (ipfix_field_specifier_t *) (t + 1);
Dave Barach2be45812018-05-13 08:50:25 -0400271 udp->src_port = clib_host_to_net_u16 (stream->src_port);
272 udp->dst_port = clib_host_to_net_u16 (collector_port);
Dave Barach2be45812018-05-13 08:50:25 -0400273
274 /* FIXUP LATER: message header export_time */
275 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
276
277 ep = report_elts;
278
279 for (i = 0; i < n_elts; i++)
280 {
281 f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size);
282 f++;
283 ep++;
284 }
285
Dave Barach2be45812018-05-13 08:50:25 -0400286 ASSERT (f - first_field);
287 /* Field count in this template */
288 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
289
290 /* set length in octets */
291 s->set_id_length =
292 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
293
294 /* message length in octets */
295 h->version_length = version_length ((u8 *) f - (u8 *) h);
296
Paul Atkinse494ad12021-10-05 08:51:48 +0100297 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
298 {
299 ip4 = (ip4_header_t *) ip;
300 ip4->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip4);
301 ip4->checksum = ip4_header_checksum (ip4);
302 }
303 else
304 {
305 ip6 = (ip6_header_t *) ip;
306 /* IPv6 payload length does not include the IPv6 header */
307 ip6->payload_length = clib_host_to_net_u16 ((u8 *) f - (u8 *) udp);
308 }
Dave Barach2be45812018-05-13 08:50:25 -0400309
310 return rewrite;
311}
312
Paul Atkins19a5f232021-09-27 21:30:13 +0100313vlib_buffer_t *
314vnet_ipfix_exp_get_buffer (vlib_main_t *vm, ipfix_exporter_t *exp,
315 flow_report_t *fr, u32 thread_index)
316{
317 u32 bi0;
318 vlib_buffer_t *b0;
319
320 if (fr->per_thread_data[thread_index].buffer)
321 return fr->per_thread_data[thread_index].buffer;
322
323 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
324 return NULL;
325
326 /* Initialize the buffer */
327 b0 = fr->per_thread_data[thread_index].buffer = vlib_get_buffer (vm, bi0);
328
329 b0->current_data = 0;
330 b0->current_length = exp->all_headers_size;
331 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
332 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
333 vnet_buffer (b0)->sw_if_index[VLIB_TX] = exp->fib_index;
334 fr->per_thread_data[thread_index].next_data_offset = b0->current_length;
335
336 return b0;
337}
338
339/*
340 * Send a buffer that is mostly populated. Has flow records but needs some
341 * header fields updated.
342 */
343void
344vnet_ipfix_exp_send_buffer (vlib_main_t *vm, ipfix_exporter_t *exp,
345 flow_report_t *fr, flow_report_stream_t *stream,
346 u32 thread_index, vlib_buffer_t *b0)
347{
348 flow_report_main_t *frm = &flow_report_main;
349 vlib_frame_t *f;
Paul Atkins19a5f232021-09-27 21:30:13 +0100350 ipfix_set_header_t *s;
351 ipfix_message_header_t *h;
Mohsin Kazmi3626a7c2022-02-22 11:42:35 +0000352 ip4_header_t *ip4 = 0;
353 ip6_header_t *ip6 = 0;
Paul Atkinse494ad12021-10-05 08:51:48 +0100354 void *ip;
Paul Atkins19a5f232021-09-27 21:30:13 +0100355 udp_header_t *udp;
Paul Atkinse494ad12021-10-05 08:51:48 +0100356 int ip_len;
Paul Atkins19a5f232021-09-27 21:30:13 +0100357
358 /* nothing to send */
359 if (fr->per_thread_data[thread_index].next_data_offset <=
360 exp->all_headers_size)
361 return;
362
Paul Atkinse494ad12021-10-05 08:51:48 +0100363 ip_len = ipfix_write_headers (exp, (void *) vlib_buffer_get_current (b0),
364 &ip, &udp, b0->current_length);
365
Paul Atkins19a5f232021-09-27 21:30:13 +0100366 h = (ipfix_message_header_t *) (udp + 1);
367 s = (ipfix_set_header_t *) (h + 1);
368
Paul Atkins19a5f232021-09-27 21:30:13 +0100369 udp->src_port = clib_host_to_net_u16 (stream->src_port);
370 udp->dst_port = clib_host_to_net_u16 (exp->collector_port);
371 udp->checksum = 0;
372
373 /* FIXUP: message header export_time */
374 h->export_time =
375 (u32) (((f64) frm->unix_time_0) + (vlib_time_now (vm) - frm->vlib_time_0));
376 h->export_time = clib_host_to_net_u32 (h->export_time);
377 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
378
379 /*
380 * RFC 7011: Section 3.2
381 *
382 * Incremental sequence counter modulo 2^32 of all IPFIX Data Records
383 * sent in the current stream from the current Observation Domain by
384 * the Exporting Process
385 */
386 h->sequence_number =
387 clib_atomic_fetch_add (&stream->sequence_number,
388 fr->per_thread_data[thread_index].n_data_records);
389 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
390
391 /*
392 * For data records we use the template ID as the set ID.
393 * RFC 7011: 3.4.3
394 */
395 s->set_id_length = ipfix_set_id_length (
396 fr->template_id,
Paul Atkinse494ad12021-10-05 08:51:48 +0100397 b0->current_length - (ip_len + sizeof (*udp) + sizeof (*h)));
Paul Atkins19a5f232021-09-27 21:30:13 +0100398 h->version_length =
Paul Atkinse494ad12021-10-05 08:51:48 +0100399 version_length (b0->current_length - (ip_len + sizeof (*udp)));
Paul Atkins19a5f232021-09-27 21:30:13 +0100400
Paul Atkinse494ad12021-10-05 08:51:48 +0100401 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
402 {
403 ip4 = (ip4_header_t *) ip;
404 ip4->length = clib_host_to_net_u16 (b0->current_length);
405 ip4->checksum = ip4_header_checksum (ip4);
406 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip4));
407 ASSERT (ip4_header_checksum_is_valid (ip4));
408 }
409 else
410 {
411 ip6 = (ip6_header_t *) ip;
412 /* Ipv6 payload length does not include the IPv6 header */
413 ip6->payload_length =
414 clib_host_to_net_u16 (b0->current_length - sizeof (*ip6));
415 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip6));
416 }
Paul Atkins19a5f232021-09-27 21:30:13 +0100417
Paul Atkinse494ad12021-10-05 08:51:48 +0100418 if (exp->udp_checksum || ip_addr_version (&exp->ipfix_collector) == AF_IP6)
Paul Atkins19a5f232021-09-27 21:30:13 +0100419 {
420 /* RFC 7011 section 10.3.2. */
Paul Atkinse494ad12021-10-05 08:51:48 +0100421 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
422 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip4);
423 else
424 {
425 int bogus = 0;
426 udp->checksum =
427 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6, &bogus);
428 }
Paul Atkins19a5f232021-09-27 21:30:13 +0100429 if (udp->checksum == 0)
430 udp->checksum = 0xffff;
431 }
432
Paul Atkins19a5f232021-09-27 21:30:13 +0100433 /* Find or allocate a frame */
434 f = fr->per_thread_data[thread_index].frame;
435 if (PREDICT_FALSE (f == 0))
436 {
437 u32 *to_next;
Paul Atkinse494ad12021-10-05 08:51:48 +0100438 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
439 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
440 else
441 f = vlib_get_frame_to_node (vm, ip6_lookup_node.index);
Paul Atkins19a5f232021-09-27 21:30:13 +0100442 fr->per_thread_data[thread_index].frame = f;
443 u32 bi0 = vlib_get_buffer_index (vm, b0);
444
445 /* Enqueue the buffer */
446 to_next = vlib_frame_vector_args (f);
447 to_next[0] = bi0;
448 f->n_vectors = 1;
449 }
450
Paul Atkinse494ad12021-10-05 08:51:48 +0100451 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
452 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
453 else
454 vlib_put_frame_to_node (vm, ip6_lookup_node.index, f);
Paul Atkins19a5f232021-09-27 21:30:13 +0100455
456 fr->per_thread_data[thread_index].frame = NULL;
457 fr->per_thread_data[thread_index].buffer = NULL;
458 fr->per_thread_data[thread_index].next_data_offset = 0;
459}
460
Paul Atkinse494ad12021-10-05 08:51:48 +0100461static void
462flow_report_process_send (vlib_main_t *vm, flow_report_main_t *frm,
463 ipfix_exporter_t *exp, flow_report_t *fr,
464 u32 next_node, u32 template_bi)
465{
466 vlib_frame_t *nf = 0;
467 u32 *to_next;
468
469 nf = vlib_get_frame_to_node (vm, next_node);
470 nf->n_vectors = 0;
471 to_next = vlib_frame_vector_args (nf);
472
473 if (template_bi != ~0)
474 {
475 to_next[0] = template_bi;
476 to_next++;
477 nf->n_vectors++;
478 }
479
480 nf = fr->flow_data_callback (frm, exp, fr, nf, to_next, next_node);
481 if (nf)
Jon Loeligereaa83c02022-06-02 15:18:54 -0500482 {
483 if (nf->n_vectors)
484 vlib_put_frame_to_node (vm, next_node, nf);
485 else
486 {
487 vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, next_node);
488 vlib_frame_free (vm, rt, nf);
489 }
490 }
Paul Atkinse494ad12021-10-05 08:51:48 +0100491}
492
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493static uword
494flow_report_process (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530495 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530497 flow_report_main_t *frm = &flow_report_main;
498 flow_report_t *fr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 u32 ip4_lookup_node_index;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530500 vlib_node_t *ip4_lookup_node;
Paul Atkinse494ad12021-10-05 08:51:48 +0100501 u32 ip6_lookup_node_index;
502 vlib_node_t *ip6_lookup_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 u32 template_bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 int send_template;
Matthew Smithbaa18702021-04-28 11:48:39 -0500505 f64 now, wait_time;
506 f64 def_wait_time = 5.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 int rv;
508 uword event_type;
509 uword *event_data = 0;
510
511 /* Wait for Godot... */
512 vlib_process_wait_for_event_or_clock (vm, 1e9);
513 event_type = vlib_process_get_events (vm, &event_data);
514 if (event_type != 1)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530515 clib_warning ("bogus kickoff event received, %d", event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 vec_reset_length (event_data);
517
518 /* Enqueue pkts to ip4-lookup */
519 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
520 ip4_lookup_node_index = ip4_lookup_node->index;
521
Paul Atkinse494ad12021-10-05 08:51:48 +0100522 /* Enqueue pkts to ip6-lookup */
523 ip6_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip6-lookup");
524 ip6_lookup_node_index = ip6_lookup_node->index;
525
Matthew Smithbaa18702021-04-28 11:48:39 -0500526 wait_time = def_wait_time;
527
Swarup Nayak6bcac062017-11-26 23:11:40 +0530528 while (1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529 {
Matthew Smithbaa18702021-04-28 11:48:39 -0500530 vlib_process_wait_for_event_or_clock (vm, wait_time);
Dave Barach0f3b6802016-12-23 15:15:48 -0500531 event_type = vlib_process_get_events (vm, &event_data);
532 vec_reset_length (event_data);
Paul Atkinsd747dd92021-09-22 14:56:17 +0100533 ipfix_exporter_t *exp;
534 pool_foreach (exp, frm->exporters)
Paul Atkins292992e2021-09-21 21:08:14 +0100535 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
Paul Atkinsd747dd92021-09-22 14:56:17 +0100537 /* 5s delay by default, possibly reduced by template intervals */
538 wait_time = def_wait_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
Paul Atkinsd747dd92021-09-22 14:56:17 +0100540 vec_foreach (fr, exp->reports)
Paul Atkins292992e2021-09-21 21:08:14 +0100541 {
Paul Atkinsd747dd92021-09-22 14:56:17 +0100542 f64 next_template;
543 now = vlib_time_now (vm);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530544
Paul Atkinsd747dd92021-09-22 14:56:17 +0100545 /* Need to send a template packet? */
546 send_template =
547 now > (fr->last_template_sent + exp->template_interval);
548 send_template += fr->last_template_sent == 0;
549 template_bi = ~0;
550 rv = 0;
551
552 if (send_template)
553 rv = send_template_packet (frm, exp, fr, &template_bi);
554
555 if (rv < 0)
556 continue;
557
558 /*
559 * decide if template should be sent sooner than current wait
560 * time
561 */
562 next_template =
563 (fr->last_template_sent + exp->template_interval) - now;
564 wait_time = clib_min (wait_time, next_template);
565
Paul Atkinse494ad12021-10-05 08:51:48 +0100566 if (ip_addr_version (&exp->ipfix_collector) == AF_IP4)
Paul Atkinsd747dd92021-09-22 14:56:17 +0100567 {
Paul Atkinse494ad12021-10-05 08:51:48 +0100568 flow_report_process_send (
569 vm, frm, exp, fr, ip4_lookup_node_index, template_bi);
Paul Atkinsd747dd92021-09-22 14:56:17 +0100570 }
Paul Atkinse494ad12021-10-05 08:51:48 +0100571 else
572 {
573 flow_report_process_send (
574 vm, frm, exp, fr, ip6_lookup_node_index, template_bi);
575 }
Paul Atkinsd747dd92021-09-22 14:56:17 +0100576 }
Paul Atkins292992e2021-09-21 21:08:14 +0100577 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 }
579
Swarup Nayak6bcac062017-11-26 23:11:40 +0530580 return 0; /* not so much */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581}
582
Swarup Nayak6bcac062017-11-26 23:11:40 +0530583/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584VLIB_REGISTER_NODE (flow_report_process_node) = {
585 .function = flow_report_process,
586 .type = VLIB_NODE_TYPE_PROCESS,
587 .name = "flow-report-process",
588};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530589/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590
Swarup Nayak6bcac062017-11-26 23:11:40 +0530591int
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100592vnet_flow_report_add_del (ipfix_exporter_t *exp,
593 vnet_flow_report_add_del_args_t *a, u16 *template_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594{
595 int i;
596 int found_index = ~0;
597 flow_report_t *fr;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530598 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700599 u32 si;
Paul Atkins19a5f232021-09-27 21:30:13 +0100600 vlib_thread_main_t *tm = &vlib_thread_main;
601 flow_report_main_t *frm = &flow_report_main;
602 vlib_main_t *vm = frm->vlib_main;
603 int size;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530604
Paul Atkins9e827812021-09-22 08:15:03 +0100605 si = find_stream (exp, a->domain_id, a->src_port);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700606 if (si == -2)
607 return VNET_API_ERROR_INVALID_VALUE;
608 if (si == -1 && a->is_add == 0)
609 return VNET_API_ERROR_NO_SUCH_ENTRY;
610
Paul Atkins9ec64492021-09-21 20:49:12 +0100611 for (i = 0; i < vec_len (exp->reports); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612 {
Paul Atkins9ec64492021-09-21 20:49:12 +0100613 fr = vec_elt_at_index (exp->reports, i);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700614 if (fr->opaque.as_uword == a->opaque.as_uword
Swarup Nayak6bcac062017-11-26 23:11:40 +0530615 && fr->rewrite_callback == a->rewrite_callback
616 && fr->flow_data_callback == a->flow_data_callback)
617 {
618 found_index = i;
619 if (template_id)
620 *template_id = fr->template_id;
621 break;
622 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 }
624
625 if (a->is_add == 0)
626 {
627 if (found_index != ~0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530628 {
Paul Atkins19a5f232021-09-27 21:30:13 +0100629 for (int i = 0;
630 i < vec_len (exp->reports[found_index].per_thread_data); i++)
631 {
632 u32 bi;
633 if (exp->reports[found_index].per_thread_data[i].buffer)
634 {
635 bi = vlib_get_buffer_index (
636 vm, exp->reports[found_index].per_thread_data[i].buffer);
637 vlib_buffer_free (vm, &bi, 1);
638 }
639 }
640 vec_free (exp->reports[found_index].per_thread_data);
641
Paul Atkins9ec64492021-09-21 20:49:12 +0100642 vec_delete (exp->reports, 1, found_index);
643 stream = &exp->streams[si];
Swarup Nayak6bcac062017-11-26 23:11:40 +0530644 stream->n_reports--;
645 if (stream->n_reports == 0)
Paul Atkins9e827812021-09-22 08:15:03 +0100646 delete_stream (exp, si);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530647 return 0;
648 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 return VNET_API_ERROR_NO_SUCH_ENTRY;
650 }
651
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700652 if (found_index != ~0)
653 return VNET_API_ERROR_VALUE_EXIST;
654
655 if (si == -1)
656 {
Paul Atkins9e827812021-09-22 08:15:03 +0100657 stream = add_stream (exp);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700658 stream->domain_id = a->domain_id;
659 stream->src_port = a->src_port;
660 stream->sequence_number = 0;
661 stream->n_reports = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100662 si = stream - exp->streams;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700663 }
664 else
Paul Atkins9ec64492021-09-21 20:49:12 +0100665 stream = &exp->streams[si];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700666
667 stream->n_reports++;
668
Paul Atkins9ec64492021-09-21 20:49:12 +0100669 vec_add2 (exp->reports, fr, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700671 fr->stream_index = si;
672 fr->template_id = 256 + stream->next_template_no;
673 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 fr->update_rewrite = 1;
675 fr->opaque = a->opaque;
676 fr->rewrite_callback = a->rewrite_callback;
677 fr->flow_data_callback = a->flow_data_callback;
Dave Barach2be45812018-05-13 08:50:25 -0400678 fr->report_elements = a->report_elements;
679 fr->n_report_elements = a->n_report_elements;
680 fr->stream_indexp = a->stream_indexp;
Paul Atkins19a5f232021-09-27 21:30:13 +0100681 vec_validate (fr->per_thread_data, tm->n_threads);
682 /* Store the flow_report index back in the args struct */
683 a->flow_report_index = fr - exp->reports;
684
685 size = 0;
686 for (int i = 0; i < fr->n_report_elements; i++)
687 size += fr->report_elements[i].size;
688 fr->data_record_size = size;
Ole Troan5c749732017-03-13 13:39:52 +0100689 if (template_id)
690 *template_id = fr->template_id;
691
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 return 0;
693}
694
Swarup Nayak6bcac062017-11-26 23:11:40 +0530695clib_error_t *
696flow_report_add_del_error_to_clib_error (int error)
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700697{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530698 switch (error)
699 {
700 case 0:
701 return 0;
702 case VNET_API_ERROR_NO_SUCH_ENTRY:
703 return clib_error_return (0, "Flow report not found");
704 case VNET_API_ERROR_VALUE_EXIST:
705 return clib_error_return (0, "Flow report already exists");
706 case VNET_API_ERROR_INVALID_VALUE:
707 return clib_error_return (0, "Expecting either still unused values "
708 "for both domain_id and src_port "
709 "or already used values for both fields");
710 default:
711 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
712 error);
713 }
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700714}
715
Swarup Nayak6bcac062017-11-26 23:11:40 +0530716void
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100717vnet_flow_reports_reset (ipfix_exporter_t *exp)
Juraj Sloboda618ab082016-07-06 06:11:00 -0700718{
719 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700720 u32 i;
721
Paul Atkins9ec64492021-09-21 20:49:12 +0100722 for (i = 0; i < vec_len (exp->streams); i++)
Paul Atkins9e827812021-09-22 08:15:03 +0100723 if (stream_index_valid (exp, i))
Paul Atkins9ec64492021-09-21 20:49:12 +0100724 exp->streams[i].sequence_number = 0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700725
Paul Atkins9ec64492021-09-21 20:49:12 +0100726 vec_foreach (fr, exp->reports)
Paul Atkins292992e2021-09-21 21:08:14 +0100727 {
728 fr->update_rewrite = 1;
729 fr->last_template_sent = 0;
730 }
Juraj Sloboda618ab082016-07-06 06:11:00 -0700731}
732
Swarup Nayak6bcac062017-11-26 23:11:40 +0530733void
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100734vnet_stream_reset (ipfix_exporter_t *exp, u32 stream_index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700735{
736 flow_report_t *fr;
737
Paul Atkins9ec64492021-09-21 20:49:12 +0100738 exp->streams[stream_index].sequence_number = 0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700739
Paul Atkins9ec64492021-09-21 20:49:12 +0100740 vec_foreach (fr, exp->reports)
741 if (exp->reports->stream_index == stream_index)
Paul Atkins292992e2021-09-21 21:08:14 +0100742 {
743 fr->update_rewrite = 1;
744 fr->last_template_sent = 0;
745 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700746}
747
Swarup Nayak6bcac062017-11-26 23:11:40 +0530748int
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100749vnet_stream_change (ipfix_exporter_t *exp, u32 old_domain_id, u16 old_src_port,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530750 u32 new_domain_id, u16 new_src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700751{
Paul Atkins9e827812021-09-22 08:15:03 +0100752 i32 stream_index = find_stream (exp, old_domain_id, old_src_port);
Paul Atkins9ec64492021-09-21 20:49:12 +0100753
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700754 if (stream_index < 0)
755 return 1;
Paul Atkins9ec64492021-09-21 20:49:12 +0100756 flow_report_stream_t *stream = &exp->streams[stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700757 stream->domain_id = new_domain_id;
758 stream->src_port = new_src_port;
759 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100760 vnet_stream_reset (exp, stream_index);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700761 return 0;
762}
763
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764static clib_error_t *
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700765set_ipfix_exporter_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530766 unformat_input_t * input,
767 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530769 flow_report_main_t *frm = &flow_report_main;
Paul Atkins51404842021-10-04 15:43:56 +0100770 ip_address_t collector = IP_ADDRESS_V4_ALL_0S, src = IP_ADDRESS_V4_ALL_0S;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700771 u16 collector_port = UDP_DST_PORT_ipfix;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700772 u32 fib_id;
773 u32 fib_index = ~0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530774
Swarup Nayak6bcac062017-11-26 23:11:40 +0530775 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700776 u32 template_interval = 20;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700777 u8 udp_checksum = 0;
Paul Atkins9ec64492021-09-21 20:49:12 +0100778 ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
Paul Atkins51404842021-10-04 15:43:56 +0100779 u32 ip_header_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Swarup Nayak6bcac062017-11-26 23:11:40 +0530781 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
782 {
Paul Atkins51404842021-10-04 15:43:56 +0100783 if (unformat (input, "collector %U", unformat_ip4_address,
784 &collector.ip.ip4))
Swarup Nayak6bcac062017-11-26 23:11:40 +0530785 ;
Elias Rudberg2dca1802020-05-27 01:03:46 +0200786 else if (unformat (input, "port %U", unformat_udp_port,
787 &collector_port))
Swarup Nayak6bcac062017-11-26 23:11:40 +0530788 ;
Paul Atkins51404842021-10-04 15:43:56 +0100789 else if (unformat (input, "src %U", unformat_ip4_address, &src.ip.ip4))
Swarup Nayak6bcac062017-11-26 23:11:40 +0530790 ;
791 else if (unformat (input, "fib-id %u", &fib_id))
792 {
793 ip4_main_t *im = &ip4_main;
794 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
795 if (!p)
796 return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
797 fib_index = p[0];
798 }
799 else if (unformat (input, "path-mtu %u", &path_mtu))
800 ;
801 else if (unformat (input, "template-interval %u", &template_interval))
802 ;
803 else if (unformat (input, "udp-checksum"))
804 udp_checksum = 1;
805 else
806 break;
807 }
808
Paul Atkins51404842021-10-04 15:43:56 +0100809 /*
810 * If the collector address is set then the src must be too.
811 * Collector address can be set to 0 to disable exporter
812 */
813 if (!ip_address_is_zero (&collector) && ip_address_is_zero (&src))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814 return clib_error_return (0, "src address required");
Paul Atkins51404842021-10-04 15:43:56 +0100815 if (collector.version != src.version)
816 return clib_error_return (
817 0, "src address and dest address must use same IP version");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818
Swarup Nayak6bcac062017-11-26 23:11:40 +0530819 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
820 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700821
822 if (path_mtu < 68)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530823 return clib_error_return (0, "too small path-mtu value, minimum is 68");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700824
Paul Atkins19a5f232021-09-27 21:30:13 +0100825 /* Calculate how much header data we need. */
Paul Atkins51404842021-10-04 15:43:56 +0100826 if (collector.version == AF_IP4)
827 ip_header_size = sizeof (ip4_header_t);
828 else
829 ip_header_size = sizeof (ip6_header_t);
830 exp->all_headers_size = ip_header_size + sizeof (udp_header_t) +
Paul Atkins19a5f232021-09-27 21:30:13 +0100831 sizeof (ipfix_message_header_t) +
832 sizeof (ipfix_set_header_t);
833
Juraj Sloboda618ab082016-07-06 06:11:00 -0700834 /* Reset report streams if we are reconfiguring IP addresses */
Paul Atkins51404842021-10-04 15:43:56 +0100835 if (ip_address_cmp (&exp->ipfix_collector, &collector) ||
836 ip_address_cmp (&exp->src_address, &src) ||
Paul Atkins9ec64492021-09-21 20:49:12 +0100837 exp->collector_port != collector_port)
Paul Atkins40f9a7a2021-09-22 10:06:23 +0100838 vnet_flow_reports_reset (exp);
Juraj Sloboda618ab082016-07-06 06:11:00 -0700839
Paul Atkins51404842021-10-04 15:43:56 +0100840 exp->ipfix_collector = collector;
Paul Atkins9ec64492021-09-21 20:49:12 +0100841 exp->collector_port = collector_port;
Paul Atkins51404842021-10-04 15:43:56 +0100842 exp->src_address = src;
Paul Atkins9ec64492021-09-21 20:49:12 +0100843 exp->fib_index = fib_index;
844 exp->path_mtu = path_mtu;
845 exp->template_interval = template_interval;
846 exp->udp_checksum = udp_checksum;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530847
Paul Atkins51404842021-10-04 15:43:56 +0100848 if (collector.ip.ip4.as_u32)
Paul Atkins9ec64492021-09-21 20:49:12 +0100849 vlib_cli_output (vm,
850 "Collector %U, src address %U, "
Swarup Nayak6bcac062017-11-26 23:11:40 +0530851 "fib index %d, path MTU %u, "
852 "template resend interval %us, "
853 "udp checksum %s",
Paul Atkinsbf9918a2021-12-01 13:13:16 +0000854 format_ip4_address, &exp->ipfix_collector.ip.ip4,
855 format_ip4_address, &exp->src_address.ip.ip4, fib_index,
856 path_mtu, template_interval,
857 udp_checksum ? "enabled" : "disabled");
Ole Troan5c749732017-03-13 13:39:52 +0100858 else
859 vlib_cli_output (vm, "IPFIX Collector is disabled");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700860
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861 /* Turn on the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530862 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 return 0;
864}
865
Swarup Nayak6bcac062017-11-26 23:11:40 +0530866/* *INDENT-OFF* */
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700867VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
868 .path = "set ipfix exporter",
869 .short_help = "set ipfix exporter "
870 "collector <ip4-address> [port <port>] "
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700871 "src <ip4-address> [fib-id <fib-id>] "
872 "[path-mtu <path-mtu>] "
Ignas Baciusf3a522f2020-02-18 12:33:09 +0200873 "[template-interval <template-interval>] "
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700874 "[udp-checksum]",
875 .function = set_ipfix_exporter_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530877/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878
Dave Barach0f3b6802016-12-23 15:15:48 -0500879
880static clib_error_t *
881ipfix_flush_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530882 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach0f3b6802016-12-23 15:15:48 -0500883{
884 /* poke the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530885 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Dave Barach0f3b6802016-12-23 15:15:48 -0500886 return 0;
887}
888
Swarup Nayak6bcac062017-11-26 23:11:40 +0530889/* *INDENT-OFF* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500890VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
891 .path = "ipfix flush",
892 .short_help = "flush the current ipfix data [for make test]",
893 .function = ipfix_flush_command_fn,
894};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530895/* *INDENT-ON* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500896
Swarup Nayak6bcac062017-11-26 23:11:40 +0530897static clib_error_t *
898flow_report_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530900 flow_report_main_t *frm = &flow_report_main;
Paul Atkins9ec64492021-09-21 20:49:12 +0100901 ipfix_exporter_t *exp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
903 frm->vlib_main = vm;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530904 frm->vnet_main = vnet_get_main ();
905 frm->unix_time_0 = time (0);
906 frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
Paul Atkins9ec64492021-09-21 20:49:12 +0100907 /*
908 * Make sure that we can always access the first exporter for
909 * backwards compatibility reasons.
910 */
911 pool_alloc (frm->exporters, IPFIX_EXPORTERS_MAX);
912 pool_get (frm->exporters, exp);
913 /* Verify that this is at index 0 */
914 ASSERT (frm->exporters == exp);
915 exp->fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 return 0;
917}
918
Dave Barachf8d50682019-05-14 18:01:44 -0400919VLIB_INIT_FUNCTION (flow_report_init);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530920/*
921 * fd.io coding-style-patch-verification: ON
922 *
923 * Local Variables:
924 * eval: (c-set-style "gnu")
925 * End:
926 */