blob: 760de5f8c66eb35ccf2d892b7249568ff554f860 [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 */
Ole Troana9855ef2018-05-02 12:45:10 +020018#include <vnet/ipfix-export/flow_report.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019#include <vnet/api_errno.h>
Florin Corasb040f982020-10-20 14:59:43 -070020#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
Juraj Sloboda837fbb12016-07-06 23:11:47 -070022flow_report_main_t flow_report_main;
23
Swarup Nayak6bcac062017-11-26 23:11:40 +053024static_always_inline u8
25stream_index_valid (u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070026{
Swarup Nayak6bcac062017-11-26 23:11:40 +053027 flow_report_main_t *frm = &flow_report_main;
28 return index < vec_len (frm->streams) &&
29 frm->streams[index].domain_id != ~0;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070030}
31
Swarup Nayak6bcac062017-11-26 23:11:40 +053032static_always_inline flow_report_stream_t *
33add_stream (void)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070034{
Swarup Nayak6bcac062017-11-26 23:11:40 +053035 flow_report_main_t *frm = &flow_report_main;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070036 u32 i;
Swarup Nayak6bcac062017-11-26 23:11:40 +053037 for (i = 0; i < vec_len (frm->streams); i++)
38 if (!stream_index_valid (i))
Juraj Slobodaffa652a2016-08-07 23:43:42 -070039 return &frm->streams[i];
Swarup Nayak6bcac062017-11-26 23:11:40 +053040 u32 index = vec_len (frm->streams);
41 vec_validate (frm->streams, index);
Juraj Slobodaffa652a2016-08-07 23:43:42 -070042 return &frm->streams[index];
43}
44
Swarup Nayak6bcac062017-11-26 23:11:40 +053045static_always_inline void
46delete_stream (u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070047{
Swarup Nayak6bcac062017-11-26 23:11:40 +053048 flow_report_main_t *frm = &flow_report_main;
49 ASSERT (index < vec_len (frm->streams));
Juraj Slobodaffa652a2016-08-07 23:43:42 -070050 ASSERT (frm->streams[index].domain_id != ~0);
51 frm->streams[index].domain_id = ~0;
52}
53
Swarup Nayak6bcac062017-11-26 23:11:40 +053054static i32
55find_stream (u32 domain_id, u16 src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070056{
Swarup Nayak6bcac062017-11-26 23:11:40 +053057 flow_report_main_t *frm = &flow_report_main;
58 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070059 u32 i;
Swarup Nayak6bcac062017-11-26 23:11:40 +053060 for (i = 0; i < vec_len (frm->streams); i++)
61 if (stream_index_valid (i))
62 {
63 stream = &frm->streams[i];
64 if (domain_id == stream->domain_id)
65 {
66 if (src_port != stream->src_port)
67 return -2;
68 return i;
69 }
70 else if (src_port == stream->src_port)
71 {
72 return -2;
73 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070074 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070075 return -1;
76}
77
Swarup Nayak6bcac062017-11-26 23:11:40 +053078int
79send_template_packet (flow_report_main_t * frm,
80 flow_report_t * fr, u32 * buffer_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070081{
82 u32 bi0;
Swarup Nayak6bcac062017-11-26 23:11:40 +053083 vlib_buffer_t *b0;
84 ip4_ipfix_template_packet_t *tp;
85 ipfix_message_header_t *h;
86 ip4_header_t *ip;
87 udp_header_t *udp;
88 vlib_main_t *vm = frm->vlib_main;
89 flow_report_stream_t *stream;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
91 ASSERT (buffer_indexp);
92
93 if (fr->update_rewrite || fr->rewrite == 0)
94 {
Swarup Nayak6bcac062017-11-26 23:11:40 +053095 if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
96 {
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 {
107 fr->rewrite = fr->rewrite_callback (frm, fr,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530108 &frm->ipfix_collector,
109 &frm->src_address,
Dave Barach2be45812018-05-13 08:50:25 -0400110 frm->collector_port,
111 fr->report_elements,
112 fr->n_report_elements,
113 fr->stream_indexp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 fr->update_rewrite = 0;
115 }
116
117 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
118 return -1;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530119
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 b0 = vlib_get_buffer (vm, bi0);
121
Damjan Marion8934a042019-02-09 23:29:26 +0100122 ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm));
Swarup Nayak6bcac062017-11-26 23:11:40 +0530123
Dave Barach178cf492018-11-13 16:34:13 -0500124 clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 b0->current_data = 0;
126 b0->current_length = vec_len (fr->rewrite);
Damjan Mariondac03522018-02-01 15:30:13 +0100127 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700129 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131 tp = vlib_buffer_get_current (b0);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530132 ip = (ip4_header_t *) & tp->ip4;
133 udp = (udp_header_t *) (ip + 1);
134 h = (ipfix_message_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Swarup Nayak6bcac062017-11-26 23:11:40 +0530136 /* FIXUP: message header export_time */
137 h->export_time = (u32)
138 (((f64) frm->unix_time_0) +
139 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
140 h->export_time = clib_host_to_net_u32 (h->export_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700142 stream = &frm->streams[fr->stream_index];
143
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 /* FIXUP: message header sequence_number. Templates do not increase it */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530145 h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 /* FIXUP: udp length */
148 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
149
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700150 if (frm->udp_checksum)
151 {
152 /* RFC 7011 section 10.3.2. */
153 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
154 if (udp->checksum == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530155 udp->checksum = 0xffff;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700156 }
157
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 *buffer_indexp = bi0;
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700159
160 fr->last_template_sent = vlib_time_now (vm);
161
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 return 0;
163}
164
Dave Barach2be45812018-05-13 08:50:25 -0400165u8 *
166vnet_flow_rewrite_generic_callback (flow_report_main_t * frm,
167 flow_report_t * fr,
168 ip4_address_t * collector_address,
169 ip4_address_t * src_address,
170 u16 collector_port,
171 ipfix_report_element_t * report_elts,
172 u32 n_elts, u32 * stream_indexp)
173{
174 ip4_header_t *ip;
175 udp_header_t *udp;
176 ipfix_message_header_t *h;
177 ipfix_set_header_t *s;
178 ipfix_template_header_t *t;
179 ipfix_field_specifier_t *f;
180 ipfix_field_specifier_t *first_field;
181 u8 *rewrite = 0;
182 ip4_ipfix_template_packet_t *tp;
183 flow_report_stream_t *stream;
184 int i;
185 ipfix_report_element_t *ep;
186
187 ASSERT (stream_indexp);
188 ASSERT (n_elts);
189 ASSERT (report_elts);
190
191 stream = &frm->streams[fr->stream_index];
192 *stream_indexp = fr->stream_index;
193
194 /* allocate rewrite space */
195 vec_validate_aligned (rewrite,
196 sizeof (ip4_ipfix_template_packet_t)
197 + n_elts * sizeof (ipfix_field_specifier_t) - 1,
198 CLIB_CACHE_LINE_BYTES);
199
200 /* create the packet rewrite string */
201 tp = (ip4_ipfix_template_packet_t *) rewrite;
202 ip = (ip4_header_t *) & tp->ip4;
203 udp = (udp_header_t *) (ip + 1);
204 h = (ipfix_message_header_t *) (udp + 1);
205 s = (ipfix_set_header_t *) (h + 1);
206 t = (ipfix_template_header_t *) (s + 1);
207 first_field = f = (ipfix_field_specifier_t *) (t + 1);
208
209 ip->ip_version_and_header_length = 0x45;
210 ip->ttl = 254;
211 ip->protocol = IP_PROTOCOL_UDP;
212 ip->src_address.as_u32 = src_address->as_u32;
213 ip->dst_address.as_u32 = collector_address->as_u32;
214 udp->src_port = clib_host_to_net_u16 (stream->src_port);
215 udp->dst_port = clib_host_to_net_u16 (collector_port);
216 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
217
218 /* FIXUP LATER: message header export_time */
219 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
220
221 ep = report_elts;
222
223 for (i = 0; i < n_elts; i++)
224 {
225 f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size);
226 f++;
227 ep++;
228 }
229
230 /* Back to the template packet... */
231 ip = (ip4_header_t *) & tp->ip4;
232 udp = (udp_header_t *) (ip + 1);
233
234 ASSERT (f - first_field);
235 /* Field count in this template */
236 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
237
238 /* set length in octets */
239 s->set_id_length =
240 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
241
242 /* message length in octets */
243 h->version_length = version_length ((u8 *) f - (u8 *) h);
244
245 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
246 ip->checksum = ip4_header_checksum (ip);
247
248 return rewrite;
249}
250
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251static uword
252flow_report_process (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530253 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530255 flow_report_main_t *frm = &flow_report_main;
256 flow_report_t *fr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 u32 ip4_lookup_node_index;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530258 vlib_node_t *ip4_lookup_node;
259 vlib_frame_t *nf = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 u32 template_bi;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530261 u32 *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 int send_template;
Matthew Smithbaa18702021-04-28 11:48:39 -0500263 f64 now, wait_time;
264 f64 def_wait_time = 5.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 int rv;
266 uword event_type;
267 uword *event_data = 0;
268
269 /* Wait for Godot... */
270 vlib_process_wait_for_event_or_clock (vm, 1e9);
271 event_type = vlib_process_get_events (vm, &event_data);
272 if (event_type != 1)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530273 clib_warning ("bogus kickoff event received, %d", event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 vec_reset_length (event_data);
275
276 /* Enqueue pkts to ip4-lookup */
277 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
278 ip4_lookup_node_index = ip4_lookup_node->index;
279
Matthew Smithbaa18702021-04-28 11:48:39 -0500280 wait_time = def_wait_time;
281
Swarup Nayak6bcac062017-11-26 23:11:40 +0530282 while (1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 {
Matthew Smithbaa18702021-04-28 11:48:39 -0500284 vlib_process_wait_for_event_or_clock (vm, wait_time);
Dave Barach0f3b6802016-12-23 15:15:48 -0500285 event_type = vlib_process_get_events (vm, &event_data);
286 vec_reset_length (event_data);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530287
Matthew Smithbaa18702021-04-28 11:48:39 -0500288 /* 5s delay by default, possibly reduced by template intervals */
289 wait_time = def_wait_time;
290
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 vec_foreach (fr, frm->reports)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530292 {
Matthew Smithbaa18702021-04-28 11:48:39 -0500293 f64 next_template;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530294 now = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Swarup Nayak6bcac062017-11-26 23:11:40 +0530296 /* Need to send a template packet? */
297 send_template =
298 now > (fr->last_template_sent + frm->template_interval);
299 send_template += fr->last_template_sent == 0;
300 template_bi = ~0;
301 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
Swarup Nayak6bcac062017-11-26 23:11:40 +0530303 if (send_template)
304 rv = send_template_packet (frm, fr, &template_bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Swarup Nayak6bcac062017-11-26 23:11:40 +0530306 if (rv < 0)
307 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Matthew Smithbaa18702021-04-28 11:48:39 -0500309 /* decide if template should be sent sooner than current wait time */
310 next_template =
311 (fr->last_template_sent + frm->template_interval) - now;
312 wait_time = clib_min (wait_time, next_template);
313
Swarup Nayak6bcac062017-11-26 23:11:40 +0530314 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
315 nf->n_vectors = 0;
316 to_next = vlib_frame_vector_args (nf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Swarup Nayak6bcac062017-11-26 23:11:40 +0530318 if (template_bi != ~0)
319 {
320 to_next[0] = template_bi;
321 to_next++;
322 nf->n_vectors++;
323 }
324
325 nf = fr->flow_data_callback (frm, fr,
326 nf, to_next, ip4_lookup_node_index);
327 if (nf)
328 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
329 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 }
331
Swarup Nayak6bcac062017-11-26 23:11:40 +0530332 return 0; /* not so much */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333}
334
Swarup Nayak6bcac062017-11-26 23:11:40 +0530335/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336VLIB_REGISTER_NODE (flow_report_process_node) = {
337 .function = flow_report_process,
338 .type = VLIB_NODE_TYPE_PROCESS,
339 .name = "flow-report-process",
340};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530341/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Swarup Nayak6bcac062017-11-26 23:11:40 +0530343int
344vnet_flow_report_add_del (flow_report_main_t * frm,
345 vnet_flow_report_add_del_args_t * a,
346 u16 * template_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347{
348 int i;
349 int found_index = ~0;
350 flow_report_t *fr;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530351 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700352 u32 si;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530353
354 si = find_stream (a->domain_id, a->src_port);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700355 if (si == -2)
356 return VNET_API_ERROR_INVALID_VALUE;
357 if (si == -1 && a->is_add == 0)
358 return VNET_API_ERROR_NO_SUCH_ENTRY;
359
Swarup Nayak6bcac062017-11-26 23:11:40 +0530360 for (i = 0; i < vec_len (frm->reports); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 {
362 fr = vec_elt_at_index (frm->reports, i);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700363 if (fr->opaque.as_uword == a->opaque.as_uword
Swarup Nayak6bcac062017-11-26 23:11:40 +0530364 && fr->rewrite_callback == a->rewrite_callback
365 && fr->flow_data_callback == a->flow_data_callback)
366 {
367 found_index = i;
368 if (template_id)
369 *template_id = fr->template_id;
370 break;
371 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 }
373
374 if (a->is_add == 0)
375 {
376 if (found_index != ~0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530377 {
378 vec_delete (frm->reports, 1, found_index);
379 stream = &frm->streams[si];
380 stream->n_reports--;
381 if (stream->n_reports == 0)
382 delete_stream (si);
383 return 0;
384 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 return VNET_API_ERROR_NO_SUCH_ENTRY;
386 }
387
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700388 if (found_index != ~0)
389 return VNET_API_ERROR_VALUE_EXIST;
390
391 if (si == -1)
392 {
Swarup Nayak6bcac062017-11-26 23:11:40 +0530393 stream = add_stream ();
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700394 stream->domain_id = a->domain_id;
395 stream->src_port = a->src_port;
396 stream->sequence_number = 0;
397 stream->n_reports = 0;
398 si = stream - frm->streams;
399 }
400 else
401 stream = &frm->streams[si];
402
403 stream->n_reports++;
404
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 vec_add2 (frm->reports, fr, 1);
406
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700407 fr->stream_index = si;
408 fr->template_id = 256 + stream->next_template_no;
409 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 fr->update_rewrite = 1;
411 fr->opaque = a->opaque;
412 fr->rewrite_callback = a->rewrite_callback;
413 fr->flow_data_callback = a->flow_data_callback;
Dave Barach2be45812018-05-13 08:50:25 -0400414 fr->report_elements = a->report_elements;
415 fr->n_report_elements = a->n_report_elements;
416 fr->stream_indexp = a->stream_indexp;
Ole Troan5c749732017-03-13 13:39:52 +0100417 if (template_id)
418 *template_id = fr->template_id;
419
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 return 0;
421}
422
Swarup Nayak6bcac062017-11-26 23:11:40 +0530423clib_error_t *
424flow_report_add_del_error_to_clib_error (int error)
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700425{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530426 switch (error)
427 {
428 case 0:
429 return 0;
430 case VNET_API_ERROR_NO_SUCH_ENTRY:
431 return clib_error_return (0, "Flow report not found");
432 case VNET_API_ERROR_VALUE_EXIST:
433 return clib_error_return (0, "Flow report already exists");
434 case VNET_API_ERROR_INVALID_VALUE:
435 return clib_error_return (0, "Expecting either still unused values "
436 "for both domain_id and src_port "
437 "or already used values for both fields");
438 default:
439 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
440 error);
441 }
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700442}
443
Swarup Nayak6bcac062017-11-26 23:11:40 +0530444void
445vnet_flow_reports_reset (flow_report_main_t * frm)
Juraj Sloboda618ab082016-07-06 06:11:00 -0700446{
447 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700448 u32 i;
449
Swarup Nayak6bcac062017-11-26 23:11:40 +0530450 for (i = 0; i < vec_len (frm->streams); i++)
451 if (stream_index_valid (i))
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700452 frm->streams[i].sequence_number = 0;
453
Juraj Sloboda618ab082016-07-06 06:11:00 -0700454 vec_foreach (fr, frm->reports)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530455 {
456 fr->update_rewrite = 1;
457 fr->last_template_sent = 0;
458 }
Juraj Sloboda618ab082016-07-06 06:11:00 -0700459}
460
Swarup Nayak6bcac062017-11-26 23:11:40 +0530461void
462vnet_stream_reset (flow_report_main_t * frm, u32 stream_index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700463{
464 flow_report_t *fr;
465
466 frm->streams[stream_index].sequence_number = 0;
467
468 vec_foreach (fr, frm->reports)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530469 if (frm->reports->stream_index == stream_index)
470 {
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700471 fr->update_rewrite = 1;
472 fr->last_template_sent = 0;
473 }
474}
475
Swarup Nayak6bcac062017-11-26 23:11:40 +0530476int
477vnet_stream_change (flow_report_main_t * frm,
478 u32 old_domain_id, u16 old_src_port,
479 u32 new_domain_id, u16 new_src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700480{
481 i32 stream_index = find_stream (old_domain_id, old_src_port);
482 if (stream_index < 0)
483 return 1;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530484 flow_report_stream_t *stream = &frm->streams[stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700485 stream->domain_id = new_domain_id;
486 stream->src_port = new_src_port;
487 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
488 vnet_stream_reset (frm, stream_index);
489 return 0;
490}
491
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492static clib_error_t *
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700493set_ipfix_exporter_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530494 unformat_input_t * input,
495 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530497 flow_report_main_t *frm = &flow_report_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 ip4_address_t collector, src;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700499 u16 collector_port = UDP_DST_PORT_ipfix;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700500 u32 fib_id;
501 u32 fib_index = ~0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530502
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 collector.as_u32 = 0;
504 src.as_u32 = 0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530505 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700506 u32 template_interval = 20;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700507 u8 udp_checksum = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Swarup Nayak6bcac062017-11-26 23:11:40 +0530509 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
510 {
511 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
512 ;
Elias Rudberg2dca1802020-05-27 01:03:46 +0200513 else if (unformat (input, "port %U", unformat_udp_port,
514 &collector_port))
Swarup Nayak6bcac062017-11-26 23:11:40 +0530515 ;
516 else if (unformat (input, "src %U", unformat_ip4_address, &src))
517 ;
518 else if (unformat (input, "fib-id %u", &fib_id))
519 {
520 ip4_main_t *im = &ip4_main;
521 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
522 if (!p)
523 return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
524 fib_index = p[0];
525 }
526 else if (unformat (input, "path-mtu %u", &path_mtu))
527 ;
528 else if (unformat (input, "template-interval %u", &template_interval))
529 ;
530 else if (unformat (input, "udp-checksum"))
531 udp_checksum = 1;
532 else
533 break;
534 }
535
Ole Troan5c749732017-03-13 13:39:52 +0100536 if (collector.as_u32 != 0 && src.as_u32 == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 return clib_error_return (0, "src address required");
538
Swarup Nayak6bcac062017-11-26 23:11:40 +0530539 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
540 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700541
542 if (path_mtu < 68)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530543 return clib_error_return (0, "too small path-mtu value, minimum is 68");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700544
Juraj Sloboda618ab082016-07-06 06:11:00 -0700545 /* Reset report streams if we are reconfiguring IP addresses */
546 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
547 frm->src_address.as_u32 != src.as_u32 ||
548 frm->collector_port != collector_port)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530549 vnet_flow_reports_reset (frm);
Juraj Sloboda618ab082016-07-06 06:11:00 -0700550
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 frm->ipfix_collector.as_u32 = collector.as_u32;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700552 frm->collector_port = collector_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 frm->src_address.as_u32 = src.as_u32;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700554 frm->fib_index = fib_index;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700555 frm->path_mtu = path_mtu;
556 frm->template_interval = template_interval;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700557 frm->udp_checksum = udp_checksum;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530558
Ole Troan5c749732017-03-13 13:39:52 +0100559 if (collector.as_u32)
560 vlib_cli_output (vm, "Collector %U, src address %U, "
Swarup Nayak6bcac062017-11-26 23:11:40 +0530561 "fib index %d, path MTU %u, "
562 "template resend interval %us, "
563 "udp checksum %s",
564 format_ip4_address, &frm->ipfix_collector,
565 format_ip4_address, &frm->src_address,
566 fib_index, path_mtu, template_interval,
567 udp_checksum ? "enabled" : "disabled");
Ole Troan5c749732017-03-13 13:39:52 +0100568 else
569 vlib_cli_output (vm, "IPFIX Collector is disabled");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700570
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 /* Turn on the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530572 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 return 0;
574}
575
Swarup Nayak6bcac062017-11-26 23:11:40 +0530576/* *INDENT-OFF* */
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700577VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
578 .path = "set ipfix exporter",
579 .short_help = "set ipfix exporter "
580 "collector <ip4-address> [port <port>] "
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700581 "src <ip4-address> [fib-id <fib-id>] "
582 "[path-mtu <path-mtu>] "
Ignas Baciusf3a522f2020-02-18 12:33:09 +0200583 "[template-interval <template-interval>] "
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700584 "[udp-checksum]",
585 .function = set_ipfix_exporter_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530587/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
Dave Barach0f3b6802016-12-23 15:15:48 -0500589
590static clib_error_t *
591ipfix_flush_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530592 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach0f3b6802016-12-23 15:15:48 -0500593{
594 /* poke the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530595 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Dave Barach0f3b6802016-12-23 15:15:48 -0500596 return 0;
597}
598
Swarup Nayak6bcac062017-11-26 23:11:40 +0530599/* *INDENT-OFF* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500600VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
601 .path = "ipfix flush",
602 .short_help = "flush the current ipfix data [for make test]",
603 .function = ipfix_flush_command_fn,
604};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530605/* *INDENT-ON* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500606
Swarup Nayak6bcac062017-11-26 23:11:40 +0530607static clib_error_t *
608flow_report_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530610 flow_report_main_t *frm = &flow_report_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
612 frm->vlib_main = vm;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530613 frm->vnet_main = vnet_get_main ();
614 frm->unix_time_0 = time (0);
615 frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700616 frm->fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
618 return 0;
619}
620
Dave Barachf8d50682019-05-14 18:01:44 -0400621VLIB_INIT_FUNCTION (flow_report_init);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530622/*
623 * fd.io coding-style-patch-verification: ON
624 *
625 * Local Variables:
626 * eval: (c-set-style "gnu")
627 * End:
628 */