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/flow/flow_report.h>
19 #include <vnet/api_errno.h>
21 flow_report_main_t flow_report_main;
23 int send_template_packet (flow_report_main_t *frm,
29 ip4_ipfix_template_packet_t * tp;
30 ipfix_message_header_t * h;
33 vlib_main_t * vm = frm->vlib_main;
35 ASSERT (buffer_indexp);
37 if (fr->update_rewrite || fr->rewrite == 0)
39 if (frm->ipfix_collector.as_u32 == 0
40 || frm->src_address.as_u32 == 0)
42 clib_warning ("no collector: disabling flow collector process");
43 vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
44 VLIB_NODE_STATE_DISABLED);
47 vec_free (fr->rewrite);
48 fr->update_rewrite = 1;
51 if (fr->update_rewrite)
53 fr->rewrite = fr->rewrite_callback (frm, fr,
54 &frm->ipfix_collector,
57 fr->update_rewrite = 0;
60 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
63 b0 = vlib_get_buffer (vm, bi0);
65 ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
67 clib_memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
69 b0->current_length = vec_len (fr->rewrite);
70 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
71 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
72 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
74 tp = vlib_buffer_get_current (b0);
75 ip = (ip4_header_t *) &tp->ip4;
76 udp = (udp_header_t *) (ip+1);
77 h = (ipfix_message_header_t *)(udp+1);
79 /* FIXUP: message header export_time */
80 h->export_time = (u32)
81 (((f64)frm->unix_time_0) +
82 (vlib_time_now(frm->vlib_main) - frm->vlib_time_0));
83 h->export_time = clib_host_to_net_u32(h->export_time);
85 /* FIXUP: message header sequence_number. Templates do not increase it */
86 h->sequence_number = clib_host_to_net_u32(fr->sequence_number);
88 /* FIXUP: udp length */
89 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
93 fr->last_template_sent = vlib_time_now (vm);
99 flow_report_process (vlib_main_t * vm,
100 vlib_node_runtime_t * rt,
103 flow_report_main_t * frm = &flow_report_main;
105 u32 ip4_lookup_node_index;
106 vlib_node_t * ip4_lookup_node;
107 vlib_frame_t * nf = 0;
114 uword *event_data = 0;
116 /* Wait for Godot... */
117 vlib_process_wait_for_event_or_clock (vm, 1e9);
118 event_type = vlib_process_get_events (vm, &event_data);
120 clib_warning ("bogus kickoff event received, %d", event_type);
121 vec_reset_length (event_data);
123 /* Enqueue pkts to ip4-lookup */
124 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
125 ip4_lookup_node_index = ip4_lookup_node->index;
129 vlib_process_suspend (vm, 5.0);
131 vec_foreach (fr, frm->reports)
133 now = vlib_time_now (vm);
135 /* Need to send a template packet? */
137 now > (fr->last_template_sent + frm->template_interval);
138 send_template += fr->last_template_sent == 0;
143 rv = send_template_packet (frm, fr, &template_bi);
148 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
150 to_next = vlib_frame_vector_args (nf);
152 if (template_bi != ~0)
154 to_next[0] = template_bi;
159 nf = fr->flow_data_callback (frm, fr,
160 nf, to_next, ip4_lookup_node_index);
162 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
166 return 0; /* not so much */
169 VLIB_REGISTER_NODE (flow_report_process_node) = {
170 .function = flow_report_process,
171 .type = VLIB_NODE_TYPE_PROCESS,
172 .name = "flow-report-process",
175 int vnet_flow_report_add_del (flow_report_main_t *frm,
176 vnet_flow_report_add_del_args_t *a)
179 int found_index = ~0;
182 for (i = 0; i < vec_len(frm->reports); i++)
184 fr = vec_elt_at_index (frm->reports, i);
185 if (fr->opaque == a->opaque
186 && fr->rewrite_callback == a->rewrite_callback
187 && fr->flow_data_callback == a->flow_data_callback)
196 if (found_index != ~0)
198 vec_delete (frm->reports, 1, found_index);
201 return VNET_API_ERROR_NO_SUCH_ENTRY;
204 vec_add2 (frm->reports, fr, 1);
206 fr->sequence_number = 0;
207 fr->domain_id = a->domain_id;
208 fr->src_port = a->src_port;
209 fr->update_rewrite = 1;
210 fr->opaque = a->opaque;
211 fr->rewrite_callback = a->rewrite_callback;
212 fr->flow_data_callback = a->flow_data_callback;
217 void vnet_flow_reports_reset (flow_report_main_t * frm)
220 vec_foreach (fr, frm->reports)
222 fr->sequence_number = 0;
223 fr->update_rewrite = 1;
224 fr->last_template_sent = 0;
228 static clib_error_t *
229 set_ipfix_command_fn (vlib_main_t * vm,
230 unformat_input_t * input,
231 vlib_cli_command_t * cmd)
233 flow_report_main_t * frm = &flow_report_main;
234 ip4_address_t collector, src;
235 u16 collector_port = UDP_DST_PORT_ipfix;
239 collector.as_u32 = 0;
241 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
242 u32 template_interval = 20;
244 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
245 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
247 else if (unformat (input, "port %u", &collector_port))
249 else if (unformat (input, "src %U", unformat_ip4_address, &src))
251 else if (unformat (input, "fib-id %u", &fib_id))
253 ip4_main_t * im = &ip4_main;
254 uword * p = hash_get (im->fib_index_by_table_id, fib_id);
256 return clib_error_return (0, "fib ID %d doesn't exist\n",
260 else if (unformat (input, "path-mtu %u", &path_mtu))
262 else if (unformat (input, "template-interval %u", &template_interval))
268 if (collector.as_u32 == 0)
269 return clib_error_return (0, "collector address required");
272 return clib_error_return (0, "src address required");
274 if (path_mtu > 1450 /* vpp does not support fragmentation */)
275 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
278 return clib_error_return (0, "too small path-mtu value, minimum is 68");
280 /* Reset report streams if we are reconfiguring IP addresses */
281 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
282 frm->src_address.as_u32 != src.as_u32 ||
283 frm->collector_port != collector_port)
284 vnet_flow_reports_reset(frm);
286 frm->ipfix_collector.as_u32 = collector.as_u32;
287 frm->collector_port = collector_port;
288 frm->src_address.as_u32 = src.as_u32;
289 frm->fib_index = fib_index;
290 frm->path_mtu = path_mtu;
291 frm->template_interval = template_interval;
293 vlib_cli_output (vm, "Collector %U, src address %U, "
294 "fib index %d, path MTU %u, "
295 "template resend interval %us",
296 format_ip4_address, &frm->ipfix_collector,
297 format_ip4_address, &frm->src_address,
298 fib_index, path_mtu, template_interval);
300 /* Turn on the flow reporting process */
301 vlib_process_signal_event (vm, flow_report_process_node.index,
306 VLIB_CLI_COMMAND (set_ipfix_command, static) = {
308 .short_help = "set ipfix collector <ip4-address> "
310 "src <ip4-address> [fib-id <fib-id>] "
311 "[path-mtu <path-mtu>] "
312 "[template-interval <template-interval>]",
313 .function = set_ipfix_command_fn,
316 static clib_error_t *
317 flow_report_init (vlib_main_t *vm)
319 flow_report_main_t * frm = &flow_report_main;
322 frm->vnet_main = vnet_get_main();
323 frm->unix_time_0 = time(0);
324 frm->vlib_time_0 = vlib_time_now(frm->vlib_main);
329 VLIB_INIT_FUNCTION (flow_report_init)