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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ipfix-export/flow_report.h>
19 #include <vnet/api_errno.h>
21 flow_report_main_t flow_report_main;
23 static_always_inline u8
24 stream_index_valid (u32 index)
26 flow_report_main_t *frm = &flow_report_main;
27 return index < vec_len (frm->streams) &&
28 frm->streams[index].domain_id != ~0;
31 static_always_inline flow_report_stream_t *
34 flow_report_main_t *frm = &flow_report_main;
36 for (i = 0; i < vec_len (frm->streams); i++)
37 if (!stream_index_valid (i))
38 return &frm->streams[i];
39 u32 index = vec_len (frm->streams);
40 vec_validate (frm->streams, index);
41 return &frm->streams[index];
44 static_always_inline void
45 delete_stream (u32 index)
47 flow_report_main_t *frm = &flow_report_main;
48 ASSERT (index < vec_len (frm->streams));
49 ASSERT (frm->streams[index].domain_id != ~0);
50 frm->streams[index].domain_id = ~0;
54 find_stream (u32 domain_id, u16 src_port)
56 flow_report_main_t *frm = &flow_report_main;
57 flow_report_stream_t *stream;
59 for (i = 0; i < vec_len (frm->streams); i++)
60 if (stream_index_valid (i))
62 stream = &frm->streams[i];
63 if (domain_id == stream->domain_id)
65 if (src_port != stream->src_port)
69 else if (src_port == stream->src_port)
78 send_template_packet (flow_report_main_t * frm,
79 flow_report_t * fr, u32 * buffer_indexp)
83 ip4_ipfix_template_packet_t *tp;
84 ipfix_message_header_t *h;
87 vlib_main_t *vm = frm->vlib_main;
88 flow_report_stream_t *stream;
89 vlib_buffer_free_list_t *fl;
91 ASSERT (buffer_indexp);
93 if (fr->update_rewrite || fr->rewrite == 0)
95 if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
97 vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
98 VLIB_NODE_STATE_DISABLED);
101 vec_free (fr->rewrite);
102 fr->update_rewrite = 1;
105 if (fr->update_rewrite)
107 fr->rewrite = fr->rewrite_callback (frm, fr,
108 &frm->ipfix_collector,
110 frm->collector_port);
111 fr->update_rewrite = 0;
114 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
117 b0 = vlib_get_buffer (vm, bi0);
119 /* Initialize the buffer */
120 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
121 vlib_buffer_init_for_free_list (b0, fl);
122 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
124 ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
126 clib_memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
127 b0->current_data = 0;
128 b0->current_length = vec_len (fr->rewrite);
129 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
130 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
131 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
133 tp = vlib_buffer_get_current (b0);
134 ip = (ip4_header_t *) & tp->ip4;
135 udp = (udp_header_t *) (ip + 1);
136 h = (ipfix_message_header_t *) (udp + 1);
138 /* FIXUP: message header export_time */
139 h->export_time = (u32)
140 (((f64) frm->unix_time_0) +
141 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
142 h->export_time = clib_host_to_net_u32 (h->export_time);
144 stream = &frm->streams[fr->stream_index];
146 /* FIXUP: message header sequence_number. Templates do not increase it */
147 h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
149 /* FIXUP: udp length */
150 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
152 if (frm->udp_checksum)
154 /* RFC 7011 section 10.3.2. */
155 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
156 if (udp->checksum == 0)
157 udp->checksum = 0xffff;
160 *buffer_indexp = bi0;
162 fr->last_template_sent = vlib_time_now (vm);
168 flow_report_process (vlib_main_t * vm,
169 vlib_node_runtime_t * rt, vlib_frame_t * f)
171 flow_report_main_t *frm = &flow_report_main;
173 u32 ip4_lookup_node_index;
174 vlib_node_t *ip4_lookup_node;
175 vlib_frame_t *nf = 0;
182 uword *event_data = 0;
184 /* Wait for Godot... */
185 vlib_process_wait_for_event_or_clock (vm, 1e9);
186 event_type = vlib_process_get_events (vm, &event_data);
188 clib_warning ("bogus kickoff event received, %d", event_type);
189 vec_reset_length (event_data);
191 /* Enqueue pkts to ip4-lookup */
192 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
193 ip4_lookup_node_index = ip4_lookup_node->index;
197 vlib_process_wait_for_event_or_clock (vm, 5.0);
198 event_type = vlib_process_get_events (vm, &event_data);
199 vec_reset_length (event_data);
201 vec_foreach (fr, frm->reports)
203 now = vlib_time_now (vm);
205 /* Need to send a template packet? */
207 now > (fr->last_template_sent + frm->template_interval);
208 send_template += fr->last_template_sent == 0;
213 rv = send_template_packet (frm, fr, &template_bi);
218 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
220 to_next = vlib_frame_vector_args (nf);
222 if (template_bi != ~0)
224 to_next[0] = template_bi;
229 nf = fr->flow_data_callback (frm, fr,
230 nf, to_next, ip4_lookup_node_index);
232 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
236 return 0; /* not so much */
240 VLIB_REGISTER_NODE (flow_report_process_node) = {
241 .function = flow_report_process,
242 .type = VLIB_NODE_TYPE_PROCESS,
243 .name = "flow-report-process",
248 vnet_flow_report_add_del (flow_report_main_t * frm,
249 vnet_flow_report_add_del_args_t * a,
253 int found_index = ~0;
255 flow_report_stream_t *stream;
258 si = find_stream (a->domain_id, a->src_port);
260 return VNET_API_ERROR_INVALID_VALUE;
261 if (si == -1 && a->is_add == 0)
262 return VNET_API_ERROR_NO_SUCH_ENTRY;
264 for (i = 0; i < vec_len (frm->reports); i++)
266 fr = vec_elt_at_index (frm->reports, i);
267 if (fr->opaque.as_uword == a->opaque.as_uword
268 && fr->rewrite_callback == a->rewrite_callback
269 && fr->flow_data_callback == a->flow_data_callback)
273 *template_id = fr->template_id;
280 if (found_index != ~0)
282 vec_delete (frm->reports, 1, found_index);
283 stream = &frm->streams[si];
285 if (stream->n_reports == 0)
289 return VNET_API_ERROR_NO_SUCH_ENTRY;
292 if (found_index != ~0)
293 return VNET_API_ERROR_VALUE_EXIST;
297 stream = add_stream ();
298 stream->domain_id = a->domain_id;
299 stream->src_port = a->src_port;
300 stream->sequence_number = 0;
301 stream->n_reports = 0;
302 si = stream - frm->streams;
305 stream = &frm->streams[si];
309 vec_add2 (frm->reports, fr, 1);
311 fr->stream_index = si;
312 fr->template_id = 256 + stream->next_template_no;
313 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
314 fr->update_rewrite = 1;
315 fr->opaque = a->opaque;
316 fr->rewrite_callback = a->rewrite_callback;
317 fr->flow_data_callback = a->flow_data_callback;
320 *template_id = fr->template_id;
326 flow_report_add_del_error_to_clib_error (int error)
332 case VNET_API_ERROR_NO_SUCH_ENTRY:
333 return clib_error_return (0, "Flow report not found");
334 case VNET_API_ERROR_VALUE_EXIST:
335 return clib_error_return (0, "Flow report already exists");
336 case VNET_API_ERROR_INVALID_VALUE:
337 return clib_error_return (0, "Expecting either still unused values "
338 "for both domain_id and src_port "
339 "or already used values for both fields");
341 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
347 vnet_flow_reports_reset (flow_report_main_t * frm)
352 for (i = 0; i < vec_len (frm->streams); i++)
353 if (stream_index_valid (i))
354 frm->streams[i].sequence_number = 0;
356 vec_foreach (fr, frm->reports)
358 fr->update_rewrite = 1;
359 fr->last_template_sent = 0;
364 vnet_stream_reset (flow_report_main_t * frm, u32 stream_index)
368 frm->streams[stream_index].sequence_number = 0;
370 vec_foreach (fr, frm->reports)
371 if (frm->reports->stream_index == stream_index)
373 fr->update_rewrite = 1;
374 fr->last_template_sent = 0;
379 vnet_stream_change (flow_report_main_t * frm,
380 u32 old_domain_id, u16 old_src_port,
381 u32 new_domain_id, u16 new_src_port)
383 i32 stream_index = find_stream (old_domain_id, old_src_port);
384 if (stream_index < 0)
386 flow_report_stream_t *stream = &frm->streams[stream_index];
387 stream->domain_id = new_domain_id;
388 stream->src_port = new_src_port;
389 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
390 vnet_stream_reset (frm, stream_index);
394 static clib_error_t *
395 set_ipfix_exporter_command_fn (vlib_main_t * vm,
396 unformat_input_t * input,
397 vlib_cli_command_t * cmd)
399 flow_report_main_t *frm = &flow_report_main;
400 ip4_address_t collector, src;
401 u16 collector_port = UDP_DST_PORT_ipfix;
405 collector.as_u32 = 0;
407 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
408 u32 template_interval = 20;
411 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
413 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
415 else if (unformat (input, "port %u", &collector_port))
417 else if (unformat (input, "src %U", unformat_ip4_address, &src))
419 else if (unformat (input, "fib-id %u", &fib_id))
421 ip4_main_t *im = &ip4_main;
422 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
424 return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
427 else if (unformat (input, "path-mtu %u", &path_mtu))
429 else if (unformat (input, "template-interval %u", &template_interval))
431 else if (unformat (input, "udp-checksum"))
437 if (collector.as_u32 != 0 && src.as_u32 == 0)
438 return clib_error_return (0, "src address required");
440 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
441 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
444 return clib_error_return (0, "too small path-mtu value, minimum is 68");
446 /* Reset report streams if we are reconfiguring IP addresses */
447 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
448 frm->src_address.as_u32 != src.as_u32 ||
449 frm->collector_port != collector_port)
450 vnet_flow_reports_reset (frm);
452 frm->ipfix_collector.as_u32 = collector.as_u32;
453 frm->collector_port = collector_port;
454 frm->src_address.as_u32 = src.as_u32;
455 frm->fib_index = fib_index;
456 frm->path_mtu = path_mtu;
457 frm->template_interval = template_interval;
458 frm->udp_checksum = udp_checksum;
460 if (collector.as_u32)
461 vlib_cli_output (vm, "Collector %U, src address %U, "
462 "fib index %d, path MTU %u, "
463 "template resend interval %us, "
465 format_ip4_address, &frm->ipfix_collector,
466 format_ip4_address, &frm->src_address,
467 fib_index, path_mtu, template_interval,
468 udp_checksum ? "enabled" : "disabled");
470 vlib_cli_output (vm, "IPFIX Collector is disabled");
472 /* Turn on the flow reporting process */
473 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
478 VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
479 .path = "set ipfix exporter",
480 .short_help = "set ipfix exporter "
481 "collector <ip4-address> [port <port>] "
482 "src <ip4-address> [fib-id <fib-id>] "
483 "[path-mtu <path-mtu>] "
484 "[template-interval <template-interval>]",
486 .function = set_ipfix_exporter_command_fn,
491 static clib_error_t *
492 ipfix_flush_command_fn (vlib_main_t * vm,
493 unformat_input_t * input, vlib_cli_command_t * cmd)
495 /* poke the flow reporting process */
496 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
501 VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
502 .path = "ipfix flush",
503 .short_help = "flush the current ipfix data [for make test]",
504 .function = ipfix_flush_command_fn,
508 static clib_error_t *
509 flow_report_init (vlib_main_t * vm)
511 flow_report_main_t *frm = &flow_report_main;
514 frm->vnet_main = vnet_get_main ();
515 frm->unix_time_0 = time (0);
516 frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
522 VLIB_INIT_FUNCTION (flow_report_init)
524 * fd.io coding-style-patch-verification: ON
527 * eval: (c-set-style "gnu")