blob: c78a78a9680c68ca39ded17d3ae92f1c2008c0cb [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 */
18#include <vnet/flow/flow_report.h>
19#include <vnet/api_errno.h>
20
Juraj Sloboda837fbb12016-07-06 23:11:47 -070021flow_report_main_t flow_report_main;
22
Juraj Slobodaffa652a2016-08-07 23:43:42 -070023static_always_inline u8 stream_index_valid (u32 index)
24{
25 flow_report_main_t * frm = &flow_report_main;
26 return index < vec_len(frm->streams) &&
27 frm->streams[index].domain_id != ~0;
28}
29
30static_always_inline flow_report_stream_t * add_stream (void)
31{
32 flow_report_main_t * frm = &flow_report_main;
33 u32 i;
34 for (i = 0; i < vec_len(frm->streams); i++)
35 if (!stream_index_valid(i))
36 return &frm->streams[i];
37 u32 index = vec_len(frm->streams);
38 vec_validate(frm->streams, index);
39 return &frm->streams[index];
40}
41
42static_always_inline void delete_stream (u32 index)
43{
44 flow_report_main_t * frm = &flow_report_main;
45 ASSERT (index < vec_len(frm->streams));
46 ASSERT (frm->streams[index].domain_id != ~0);
47 frm->streams[index].domain_id = ~0;
48}
49
50static i32 find_stream (u32 domain_id, u16 src_port)
51{
52 flow_report_main_t * frm = &flow_report_main;
53 flow_report_stream_t * stream;
54 u32 i;
55 for (i = 0; i < vec_len(frm->streams); i++)
56 if (stream_index_valid(i)) {
57 stream = &frm->streams[i];
58 if (domain_id == stream->domain_id) {
59 if (src_port != stream->src_port)
60 return -2;
61 return i;
62 } else if (src_port == stream->src_port) {
63 return -2;
64 }
65 }
66 return -1;
67}
68
Ed Warnickecb9cada2015-12-08 15:45:58 -070069int send_template_packet (flow_report_main_t *frm,
70 flow_report_t *fr,
71 u32 * buffer_indexp)
72{
73 u32 bi0;
74 vlib_buffer_t * b0;
75 ip4_ipfix_template_packet_t * tp;
76 ipfix_message_header_t * h;
77 ip4_header_t * ip;
78 udp_header_t * udp;
79 vlib_main_t * vm = frm->vlib_main;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070080 flow_report_stream_t * stream;
Dave Barach393252a2016-11-02 18:37:56 -040081 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 ASSERT (buffer_indexp);
84
85 if (fr->update_rewrite || fr->rewrite == 0)
86 {
87 if (frm->ipfix_collector.as_u32 == 0
88 || frm->src_address.as_u32 == 0)
89 {
90 clib_warning ("no collector: disabling flow collector process");
91 vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
92 VLIB_NODE_STATE_DISABLED);
93 return -1;
94 }
95 vec_free (fr->rewrite);
96 fr->update_rewrite = 1;
97 }
98
99 if (fr->update_rewrite)
100 {
101 fr->rewrite = fr->rewrite_callback (frm, fr,
102 &frm->ipfix_collector,
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700103 &frm->src_address,
104 frm->collector_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 fr->update_rewrite = 0;
106 }
107
108 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
109 return -1;
110
111 b0 = vlib_get_buffer (vm, bi0);
112
Dave Barach393252a2016-11-02 18:37:56 -0400113 /* Initialize the buffer */
114 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
115 vlib_buffer_init_for_free_list (b0, fl);
116 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
117
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
119
Damjan Marionf1213b82016-03-13 02:22:06 +0100120 clib_memcpy (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);
Dave Barachad41b352016-10-07 08:30:23 -0700123 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_FLOW_REPORT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700125 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
127 tp = vlib_buffer_get_current (b0);
128 ip = (ip4_header_t *) &tp->ip4;
129 udp = (udp_header_t *) (ip+1);
130 h = (ipfix_message_header_t *)(udp+1);
131
132 /* FIXUP: message header export_time */
133 h->export_time = (u32)
134 (((f64)frm->unix_time_0) +
135 (vlib_time_now(frm->vlib_main) - frm->vlib_time_0));
136 h->export_time = clib_host_to_net_u32(h->export_time);
137
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700138 stream = &frm->streams[fr->stream_index];
139
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 /* FIXUP: message header sequence_number. Templates do not increase it */
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700141 h->sequence_number = clib_host_to_net_u32(stream->sequence_number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
143 /* FIXUP: udp length */
144 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
145
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700146 if (frm->udp_checksum)
147 {
148 /* RFC 7011 section 10.3.2. */
149 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
150 if (udp->checksum == 0)
151 udp->checksum = 0xffff;
152 }
153
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 *buffer_indexp = bi0;
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700155
156 fr->last_template_sent = vlib_time_now (vm);
157
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 return 0;
159}
160
161static uword
162flow_report_process (vlib_main_t * vm,
163 vlib_node_runtime_t * rt,
164 vlib_frame_t * f)
165{
166 flow_report_main_t * frm = &flow_report_main;
167 flow_report_t * fr;
168 u32 ip4_lookup_node_index;
169 vlib_node_t * ip4_lookup_node;
170 vlib_frame_t * nf = 0;
171 u32 template_bi;
172 u32 * to_next;
173 int send_template;
174 f64 now;
175 int rv;
176 uword event_type;
177 uword *event_data = 0;
178
179 /* Wait for Godot... */
180 vlib_process_wait_for_event_or_clock (vm, 1e9);
181 event_type = vlib_process_get_events (vm, &event_data);
182 if (event_type != 1)
183 clib_warning ("bogus kickoff event received, %d", event_type);
184 vec_reset_length (event_data);
185
186 /* Enqueue pkts to ip4-lookup */
187 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
188 ip4_lookup_node_index = ip4_lookup_node->index;
189
190 while (1)
191 {
Dave Barach0f3b6802016-12-23 15:15:48 -0500192 vlib_process_wait_for_event_or_clock (vm, 5.0);
193 event_type = vlib_process_get_events (vm, &event_data);
194 vec_reset_length (event_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
196 vec_foreach (fr, frm->reports)
197 {
198 now = vlib_time_now (vm);
199
200 /* Need to send a template packet? */
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700201 send_template =
202 now > (fr->last_template_sent + frm->template_interval);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 send_template += fr->last_template_sent == 0;
204 template_bi = ~0;
205 rv = 0;
206
207 if (send_template)
208 rv = send_template_packet (frm, fr, &template_bi);
209
210 if (rv < 0)
211 continue;
212
213 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
214 nf->n_vectors = 0;
215 to_next = vlib_frame_vector_args (nf);
216
217 if (template_bi != ~0)
218 {
219 to_next[0] = template_bi;
220 to_next++;
221 nf->n_vectors++;
222 }
223
224 nf = fr->flow_data_callback (frm, fr,
225 nf, to_next, ip4_lookup_node_index);
226 if (nf)
227 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
228 }
229 }
230
231 return 0; /* not so much */
232}
233
234VLIB_REGISTER_NODE (flow_report_process_node) = {
235 .function = flow_report_process,
236 .type = VLIB_NODE_TYPE_PROCESS,
237 .name = "flow-report-process",
238};
239
240int vnet_flow_report_add_del (flow_report_main_t *frm,
241 vnet_flow_report_add_del_args_t *a)
242{
243 int i;
244 int found_index = ~0;
245 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700246 flow_report_stream_t * stream;
247 u32 si;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700249 si = find_stream(a->domain_id, a->src_port);
250 if (si == -2)
251 return VNET_API_ERROR_INVALID_VALUE;
252 if (si == -1 && a->is_add == 0)
253 return VNET_API_ERROR_NO_SUCH_ENTRY;
254
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 for (i = 0; i < vec_len(frm->reports); i++)
256 {
257 fr = vec_elt_at_index (frm->reports, i);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700258 if (fr->opaque.as_uword == a->opaque.as_uword
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 && fr->rewrite_callback == a->rewrite_callback
260 && fr->flow_data_callback == a->flow_data_callback)
261 {
262 found_index = i;
263 break;
264 }
265 }
266
267 if (a->is_add == 0)
268 {
269 if (found_index != ~0)
270 {
271 vec_delete (frm->reports, 1, found_index);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700272 stream = &frm->streams[si];
273 stream->n_reports--;
274 if (stream->n_reports == 0)
275 delete_stream(si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 return 0;
277 }
278 return VNET_API_ERROR_NO_SUCH_ENTRY;
279 }
280
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700281 if (found_index != ~0)
282 return VNET_API_ERROR_VALUE_EXIST;
283
284 if (si == -1)
285 {
286 stream = add_stream();
287 stream->domain_id = a->domain_id;
288 stream->src_port = a->src_port;
289 stream->sequence_number = 0;
290 stream->n_reports = 0;
291 si = stream - frm->streams;
292 }
293 else
294 stream = &frm->streams[si];
295
296 stream->n_reports++;
297
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 vec_add2 (frm->reports, fr, 1);
299
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700300 fr->stream_index = si;
301 fr->template_id = 256 + stream->next_template_no;
302 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 fr->update_rewrite = 1;
304 fr->opaque = a->opaque;
305 fr->rewrite_callback = a->rewrite_callback;
306 fr->flow_data_callback = a->flow_data_callback;
307
308 return 0;
309}
310
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700311clib_error_t * flow_report_add_del_error_to_clib_error (int error)
312{
313 switch (error)
314 {
315 case 0:
316 return 0;
317 case VNET_API_ERROR_NO_SUCH_ENTRY:
318 return clib_error_return (0, "Flow report not found");
319 case VNET_API_ERROR_VALUE_EXIST:
320 return clib_error_return (0, "Flow report already exists");
321 case VNET_API_ERROR_INVALID_VALUE:
322 return clib_error_return (0, "Expecting either still unused values "
323 "for both domain_id and src_port "
324 "or already used values for both fields");
325 default:
326 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
327 error);
328 }
329}
330
Juraj Sloboda618ab082016-07-06 06:11:00 -0700331void vnet_flow_reports_reset (flow_report_main_t * frm)
332{
333 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700334 u32 i;
335
336 for (i = 0; i < vec_len(frm->streams); i++)
337 if (stream_index_valid(i))
338 frm->streams[i].sequence_number = 0;
339
Juraj Sloboda618ab082016-07-06 06:11:00 -0700340 vec_foreach (fr, frm->reports)
341 {
Juraj Sloboda618ab082016-07-06 06:11:00 -0700342 fr->update_rewrite = 1;
343 fr->last_template_sent = 0;
344 }
345}
346
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700347void vnet_stream_reset (flow_report_main_t * frm, u32 stream_index)
348{
349 flow_report_t *fr;
350
351 frm->streams[stream_index].sequence_number = 0;
352
353 vec_foreach (fr, frm->reports)
354 if (frm->reports->stream_index == stream_index) {
355 fr->update_rewrite = 1;
356 fr->last_template_sent = 0;
357 }
358}
359
360int vnet_stream_change (flow_report_main_t * frm,
361 u32 old_domain_id, u16 old_src_port,
362 u32 new_domain_id, u16 new_src_port)
363{
364 i32 stream_index = find_stream (old_domain_id, old_src_port);
365 if (stream_index < 0)
366 return 1;
367 flow_report_stream_t * stream = &frm->streams[stream_index];
368 stream->domain_id = new_domain_id;
369 stream->src_port = new_src_port;
370 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
371 vnet_stream_reset (frm, stream_index);
372 return 0;
373}
374
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375static clib_error_t *
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700376set_ipfix_exporter_command_fn (vlib_main_t * vm,
377 unformat_input_t * input,
378 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379{
380 flow_report_main_t * frm = &flow_report_main;
381 ip4_address_t collector, src;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700382 u16 collector_port = UDP_DST_PORT_ipfix;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700383 u32 fib_id;
384 u32 fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
386 collector.as_u32 = 0;
387 src.as_u32 = 0;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700388 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
389 u32 template_interval = 20;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700390 u8 udp_checksum = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
392 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
393 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
394 ;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700395 else if (unformat (input, "port %u", &collector_port))
396 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 else if (unformat (input, "src %U", unformat_ip4_address, &src))
398 ;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700399 else if (unformat (input, "fib-id %u", &fib_id))
400 {
401 ip4_main_t * im = &ip4_main;
402 uword * p = hash_get (im->fib_index_by_table_id, fib_id);
403 if (! p)
404 return clib_error_return (0, "fib ID %d doesn't exist\n",
405 fib_id);
406 fib_index = p[0];
407 }
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700408 else if (unformat (input, "path-mtu %u", &path_mtu))
409 ;
410 else if (unformat (input, "template-interval %u", &template_interval))
411 ;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700412 else if (unformat (input, "udp-checksum"))
413 udp_checksum = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 else
415 break;
416 }
417
418 if (collector.as_u32 == 0)
419 return clib_error_return (0, "collector address required");
420
421 if (src.as_u32 == 0)
422 return clib_error_return (0, "src address required");
423
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700424 if (path_mtu > 1450 /* vpp does not support fragmentation */)
425 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
426
427 if (path_mtu < 68)
428 return clib_error_return (0, "too small path-mtu value, minimum is 68");
429
Juraj Sloboda618ab082016-07-06 06:11:00 -0700430 /* Reset report streams if we are reconfiguring IP addresses */
431 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
432 frm->src_address.as_u32 != src.as_u32 ||
433 frm->collector_port != collector_port)
434 vnet_flow_reports_reset(frm);
435
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 frm->ipfix_collector.as_u32 = collector.as_u32;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700437 frm->collector_port = collector_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 frm->src_address.as_u32 = src.as_u32;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700439 frm->fib_index = fib_index;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700440 frm->path_mtu = path_mtu;
441 frm->template_interval = template_interval;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700442 frm->udp_checksum = udp_checksum;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700444 vlib_cli_output (vm, "Collector %U, src address %U, "
445 "fib index %d, path MTU %u, "
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700446 "template resend interval %us, "
447 "udp checksum %s",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 format_ip4_address, &frm->ipfix_collector,
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700449 format_ip4_address, &frm->src_address,
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700450 fib_index, path_mtu, template_interval,
451 udp_checksum ? "enabled" : "disabled");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700452
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 /* Turn on the flow reporting process */
454 vlib_process_signal_event (vm, flow_report_process_node.index,
455 1, 0);
456 return 0;
457}
458
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700459VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
460 .path = "set ipfix exporter",
461 .short_help = "set ipfix exporter "
462 "collector <ip4-address> [port <port>] "
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700463 "src <ip4-address> [fib-id <fib-id>] "
464 "[path-mtu <path-mtu>] "
465 "[template-interval <template-interval>]",
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700466 "[udp-checksum]",
467 .function = set_ipfix_exporter_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468};
469
Dave Barach0f3b6802016-12-23 15:15:48 -0500470
471static clib_error_t *
472ipfix_flush_command_fn (vlib_main_t * vm,
473 unformat_input_t * input,
474 vlib_cli_command_t * cmd)
475{
476 /* poke the flow reporting process */
477 vlib_process_signal_event (vm, flow_report_process_node.index,
478 1, 0);
479 return 0;
480}
481
482VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
483 .path = "ipfix flush",
484 .short_help = "flush the current ipfix data [for make test]",
485 .function = ipfix_flush_command_fn,
486};
487
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488static clib_error_t *
489flow_report_init (vlib_main_t *vm)
490{
491 flow_report_main_t * frm = &flow_report_main;
492
493 frm->vlib_main = vm;
494 frm->vnet_main = vnet_get_main();
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700495 frm->unix_time_0 = time(0);
496 frm->vlib_time_0 = vlib_time_now(frm->vlib_main);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700497 frm->fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498
499 return 0;
500}
501
502VLIB_INIT_FUNCTION (flow_report_init)