blob: c9370a748cf42a3be66ab33afc12b03d8092a72e [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>
20
Juraj Sloboda837fbb12016-07-06 23:11:47 -070021flow_report_main_t flow_report_main;
22
Swarup Nayak6bcac062017-11-26 23:11:40 +053023static_always_inline u8
24stream_index_valid (u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070025{
Swarup Nayak6bcac062017-11-26 23:11:40 +053026 flow_report_main_t *frm = &flow_report_main;
27 return index < vec_len (frm->streams) &&
28 frm->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 *
32add_stream (void)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070033{
Swarup Nayak6bcac062017-11-26 23:11:40 +053034 flow_report_main_t *frm = &flow_report_main;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070035 u32 i;
Swarup Nayak6bcac062017-11-26 23:11:40 +053036 for (i = 0; i < vec_len (frm->streams); i++)
37 if (!stream_index_valid (i))
Juraj Slobodaffa652a2016-08-07 23:43:42 -070038 return &frm->streams[i];
Swarup Nayak6bcac062017-11-26 23:11:40 +053039 u32 index = vec_len (frm->streams);
40 vec_validate (frm->streams, index);
Juraj Slobodaffa652a2016-08-07 23:43:42 -070041 return &frm->streams[index];
42}
43
Swarup Nayak6bcac062017-11-26 23:11:40 +053044static_always_inline void
45delete_stream (u32 index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070046{
Swarup Nayak6bcac062017-11-26 23:11:40 +053047 flow_report_main_t *frm = &flow_report_main;
48 ASSERT (index < vec_len (frm->streams));
Juraj Slobodaffa652a2016-08-07 23:43:42 -070049 ASSERT (frm->streams[index].domain_id != ~0);
50 frm->streams[index].domain_id = ~0;
51}
52
Swarup Nayak6bcac062017-11-26 23:11:40 +053053static i32
54find_stream (u32 domain_id, u16 src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -070055{
Swarup Nayak6bcac062017-11-26 23:11:40 +053056 flow_report_main_t *frm = &flow_report_main;
57 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -070058 u32 i;
Swarup Nayak6bcac062017-11-26 23:11:40 +053059 for (i = 0; i < vec_len (frm->streams); i++)
60 if (stream_index_valid (i))
61 {
62 stream = &frm->streams[i];
63 if (domain_id == stream->domain_id)
64 {
65 if (src_port != stream->src_port)
66 return -2;
67 return i;
68 }
69 else if (src_port == stream->src_port)
70 {
71 return -2;
72 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070073 }
Juraj Slobodaffa652a2016-08-07 23:43:42 -070074 return -1;
75}
76
Swarup Nayak6bcac062017-11-26 23:11:40 +053077int
78send_template_packet (flow_report_main_t * frm,
79 flow_report_t * fr, u32 * buffer_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
81 u32 bi0;
Swarup Nayak6bcac062017-11-26 23:11:40 +053082 vlib_buffer_t *b0;
83 ip4_ipfix_template_packet_t *tp;
84 ipfix_message_header_t *h;
85 ip4_header_t *ip;
86 udp_header_t *udp;
87 vlib_main_t *vm = frm->vlib_main;
88 flow_report_stream_t *stream;
Dave Barach393252a2016-11-02 18:37:56 -040089 vlib_buffer_free_list_t *fl;
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
Dave Barach393252a2016-11-02 18:37:56 -0400122 /* Initialize the buffer */
123 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
124 vlib_buffer_init_for_free_list (b0, fl);
125 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
126
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530128
Dave Barach178cf492018-11-13 16:34:13 -0500129 clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 b0->current_data = 0;
131 b0->current_length = vec_len (fr->rewrite);
Damjan Mariondac03522018-02-01 15:30:13 +0100132 b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700134 vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
136 tp = vlib_buffer_get_current (b0);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530137 ip = (ip4_header_t *) & tp->ip4;
138 udp = (udp_header_t *) (ip + 1);
139 h = (ipfix_message_header_t *) (udp + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Swarup Nayak6bcac062017-11-26 23:11:40 +0530141 /* FIXUP: message header export_time */
142 h->export_time = (u32)
143 (((f64) frm->unix_time_0) +
144 (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
145 h->export_time = clib_host_to_net_u32 (h->export_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700147 stream = &frm->streams[fr->stream_index];
148
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 /* FIXUP: message header sequence_number. Templates do not increase it */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530150 h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
152 /* FIXUP: udp length */
153 udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
154
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700155 if (frm->udp_checksum)
156 {
157 /* RFC 7011 section 10.3.2. */
158 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
159 if (udp->checksum == 0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530160 udp->checksum = 0xffff;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700161 }
162
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 *buffer_indexp = bi0;
Juraj Sloboda0d2a8e72016-07-07 02:59:28 -0700164
165 fr->last_template_sent = vlib_time_now (vm);
166
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 return 0;
168}
169
Dave Barach2be45812018-05-13 08:50:25 -0400170u8 *
171vnet_flow_rewrite_generic_callback (flow_report_main_t * frm,
172 flow_report_t * fr,
173 ip4_address_t * collector_address,
174 ip4_address_t * src_address,
175 u16 collector_port,
176 ipfix_report_element_t * report_elts,
177 u32 n_elts, u32 * stream_indexp)
178{
179 ip4_header_t *ip;
180 udp_header_t *udp;
181 ipfix_message_header_t *h;
182 ipfix_set_header_t *s;
183 ipfix_template_header_t *t;
184 ipfix_field_specifier_t *f;
185 ipfix_field_specifier_t *first_field;
186 u8 *rewrite = 0;
187 ip4_ipfix_template_packet_t *tp;
188 flow_report_stream_t *stream;
189 int i;
190 ipfix_report_element_t *ep;
191
192 ASSERT (stream_indexp);
193 ASSERT (n_elts);
194 ASSERT (report_elts);
195
196 stream = &frm->streams[fr->stream_index];
197 *stream_indexp = fr->stream_index;
198
199 /* allocate rewrite space */
200 vec_validate_aligned (rewrite,
201 sizeof (ip4_ipfix_template_packet_t)
202 + n_elts * sizeof (ipfix_field_specifier_t) - 1,
203 CLIB_CACHE_LINE_BYTES);
204
205 /* create the packet rewrite string */
206 tp = (ip4_ipfix_template_packet_t *) rewrite;
207 ip = (ip4_header_t *) & tp->ip4;
208 udp = (udp_header_t *) (ip + 1);
209 h = (ipfix_message_header_t *) (udp + 1);
210 s = (ipfix_set_header_t *) (h + 1);
211 t = (ipfix_template_header_t *) (s + 1);
212 first_field = f = (ipfix_field_specifier_t *) (t + 1);
213
214 ip->ip_version_and_header_length = 0x45;
215 ip->ttl = 254;
216 ip->protocol = IP_PROTOCOL_UDP;
217 ip->src_address.as_u32 = src_address->as_u32;
218 ip->dst_address.as_u32 = collector_address->as_u32;
219 udp->src_port = clib_host_to_net_u16 (stream->src_port);
220 udp->dst_port = clib_host_to_net_u16 (collector_port);
221 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
222
223 /* FIXUP LATER: message header export_time */
224 h->domain_id = clib_host_to_net_u32 (stream->domain_id);
225
226 ep = report_elts;
227
228 for (i = 0; i < n_elts; i++)
229 {
230 f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size);
231 f++;
232 ep++;
233 }
234
235 /* Back to the template packet... */
236 ip = (ip4_header_t *) & tp->ip4;
237 udp = (udp_header_t *) (ip + 1);
238
239 ASSERT (f - first_field);
240 /* Field count in this template */
241 t->id_count = ipfix_id_count (fr->template_id, f - first_field);
242
243 /* set length in octets */
244 s->set_id_length =
245 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
246
247 /* message length in octets */
248 h->version_length = version_length ((u8 *) f - (u8 *) h);
249
250 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
251 ip->checksum = ip4_header_checksum (ip);
252
253 return rewrite;
254}
255
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256static uword
257flow_report_process (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530258 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530260 flow_report_main_t *frm = &flow_report_main;
261 flow_report_t *fr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 u32 ip4_lookup_node_index;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530263 vlib_node_t *ip4_lookup_node;
264 vlib_frame_t *nf = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 u32 template_bi;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530266 u32 *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 int send_template;
268 f64 now;
269 int rv;
270 uword event_type;
271 uword *event_data = 0;
272
273 /* Wait for Godot... */
274 vlib_process_wait_for_event_or_clock (vm, 1e9);
275 event_type = vlib_process_get_events (vm, &event_data);
276 if (event_type != 1)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530277 clib_warning ("bogus kickoff event received, %d", event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 vec_reset_length (event_data);
279
280 /* Enqueue pkts to ip4-lookup */
281 ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
282 ip4_lookup_node_index = ip4_lookup_node->index;
283
Swarup Nayak6bcac062017-11-26 23:11:40 +0530284 while (1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285 {
Dave Barach0f3b6802016-12-23 15:15:48 -0500286 vlib_process_wait_for_event_or_clock (vm, 5.0);
287 event_type = vlib_process_get_events (vm, &event_data);
288 vec_reset_length (event_data);
Swarup Nayak6bcac062017-11-26 23:11:40 +0530289
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 vec_foreach (fr, frm->reports)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530291 {
292 now = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Swarup Nayak6bcac062017-11-26 23:11:40 +0530294 /* Need to send a template packet? */
295 send_template =
296 now > (fr->last_template_sent + frm->template_interval);
297 send_template += fr->last_template_sent == 0;
298 template_bi = ~0;
299 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Swarup Nayak6bcac062017-11-26 23:11:40 +0530301 if (send_template)
302 rv = send_template_packet (frm, fr, &template_bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
Swarup Nayak6bcac062017-11-26 23:11:40 +0530304 if (rv < 0)
305 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Swarup Nayak6bcac062017-11-26 23:11:40 +0530307 nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
308 nf->n_vectors = 0;
309 to_next = vlib_frame_vector_args (nf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
Swarup Nayak6bcac062017-11-26 23:11:40 +0530311 if (template_bi != ~0)
312 {
313 to_next[0] = template_bi;
314 to_next++;
315 nf->n_vectors++;
316 }
317
318 nf = fr->flow_data_callback (frm, fr,
319 nf, to_next, ip4_lookup_node_index);
320 if (nf)
321 vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
322 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 }
324
Swarup Nayak6bcac062017-11-26 23:11:40 +0530325 return 0; /* not so much */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326}
327
Swarup Nayak6bcac062017-11-26 23:11:40 +0530328/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329VLIB_REGISTER_NODE (flow_report_process_node) = {
330 .function = flow_report_process,
331 .type = VLIB_NODE_TYPE_PROCESS,
332 .name = "flow-report-process",
333};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530334/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Swarup Nayak6bcac062017-11-26 23:11:40 +0530336int
337vnet_flow_report_add_del (flow_report_main_t * frm,
338 vnet_flow_report_add_del_args_t * a,
339 u16 * template_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340{
341 int i;
342 int found_index = ~0;
343 flow_report_t *fr;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530344 flow_report_stream_t *stream;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700345 u32 si;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530346
347 si = find_stream (a->domain_id, a->src_port);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700348 if (si == -2)
349 return VNET_API_ERROR_INVALID_VALUE;
350 if (si == -1 && a->is_add == 0)
351 return VNET_API_ERROR_NO_SUCH_ENTRY;
352
Swarup Nayak6bcac062017-11-26 23:11:40 +0530353 for (i = 0; i < vec_len (frm->reports); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 {
355 fr = vec_elt_at_index (frm->reports, i);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700356 if (fr->opaque.as_uword == a->opaque.as_uword
Swarup Nayak6bcac062017-11-26 23:11:40 +0530357 && fr->rewrite_callback == a->rewrite_callback
358 && fr->flow_data_callback == a->flow_data_callback)
359 {
360 found_index = i;
361 if (template_id)
362 *template_id = fr->template_id;
363 break;
364 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365 }
366
367 if (a->is_add == 0)
368 {
369 if (found_index != ~0)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530370 {
371 vec_delete (frm->reports, 1, found_index);
372 stream = &frm->streams[si];
373 stream->n_reports--;
374 if (stream->n_reports == 0)
375 delete_stream (si);
376 return 0;
377 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 return VNET_API_ERROR_NO_SUCH_ENTRY;
379 }
380
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700381 if (found_index != ~0)
382 return VNET_API_ERROR_VALUE_EXIST;
383
384 if (si == -1)
385 {
Swarup Nayak6bcac062017-11-26 23:11:40 +0530386 stream = add_stream ();
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700387 stream->domain_id = a->domain_id;
388 stream->src_port = a->src_port;
389 stream->sequence_number = 0;
390 stream->n_reports = 0;
391 si = stream - frm->streams;
392 }
393 else
394 stream = &frm->streams[si];
395
396 stream->n_reports++;
397
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 vec_add2 (frm->reports, fr, 1);
399
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700400 fr->stream_index = si;
401 fr->template_id = 256 + stream->next_template_no;
402 stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 fr->update_rewrite = 1;
404 fr->opaque = a->opaque;
405 fr->rewrite_callback = a->rewrite_callback;
406 fr->flow_data_callback = a->flow_data_callback;
Dave Barach2be45812018-05-13 08:50:25 -0400407 fr->report_elements = a->report_elements;
408 fr->n_report_elements = a->n_report_elements;
409 fr->stream_indexp = a->stream_indexp;
Ole Troan5c749732017-03-13 13:39:52 +0100410 if (template_id)
411 *template_id = fr->template_id;
412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 return 0;
414}
415
Swarup Nayak6bcac062017-11-26 23:11:40 +0530416clib_error_t *
417flow_report_add_del_error_to_clib_error (int error)
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700418{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530419 switch (error)
420 {
421 case 0:
422 return 0;
423 case VNET_API_ERROR_NO_SUCH_ENTRY:
424 return clib_error_return (0, "Flow report not found");
425 case VNET_API_ERROR_VALUE_EXIST:
426 return clib_error_return (0, "Flow report already exists");
427 case VNET_API_ERROR_INVALID_VALUE:
428 return clib_error_return (0, "Expecting either still unused values "
429 "for both domain_id and src_port "
430 "or already used values for both fields");
431 default:
432 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
433 error);
434 }
Juraj Sloboda24648ad2016-09-06 04:43:52 -0700435}
436
Swarup Nayak6bcac062017-11-26 23:11:40 +0530437void
438vnet_flow_reports_reset (flow_report_main_t * frm)
Juraj Sloboda618ab082016-07-06 06:11:00 -0700439{
440 flow_report_t *fr;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700441 u32 i;
442
Swarup Nayak6bcac062017-11-26 23:11:40 +0530443 for (i = 0; i < vec_len (frm->streams); i++)
444 if (stream_index_valid (i))
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700445 frm->streams[i].sequence_number = 0;
446
Juraj Sloboda618ab082016-07-06 06:11:00 -0700447 vec_foreach (fr, frm->reports)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530448 {
449 fr->update_rewrite = 1;
450 fr->last_template_sent = 0;
451 }
Juraj Sloboda618ab082016-07-06 06:11:00 -0700452}
453
Swarup Nayak6bcac062017-11-26 23:11:40 +0530454void
455vnet_stream_reset (flow_report_main_t * frm, u32 stream_index)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700456{
457 flow_report_t *fr;
458
459 frm->streams[stream_index].sequence_number = 0;
460
461 vec_foreach (fr, frm->reports)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530462 if (frm->reports->stream_index == stream_index)
463 {
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700464 fr->update_rewrite = 1;
465 fr->last_template_sent = 0;
466 }
467}
468
Swarup Nayak6bcac062017-11-26 23:11:40 +0530469int
470vnet_stream_change (flow_report_main_t * frm,
471 u32 old_domain_id, u16 old_src_port,
472 u32 new_domain_id, u16 new_src_port)
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700473{
474 i32 stream_index = find_stream (old_domain_id, old_src_port);
475 if (stream_index < 0)
476 return 1;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530477 flow_report_stream_t *stream = &frm->streams[stream_index];
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700478 stream->domain_id = new_domain_id;
479 stream->src_port = new_src_port;
480 if (old_domain_id != new_domain_id || old_src_port != new_src_port)
481 vnet_stream_reset (frm, stream_index);
482 return 0;
483}
484
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485static clib_error_t *
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700486set_ipfix_exporter_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530487 unformat_input_t * input,
488 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530490 flow_report_main_t *frm = &flow_report_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 ip4_address_t collector, src;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700492 u16 collector_port = UDP_DST_PORT_ipfix;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700493 u32 fib_id;
494 u32 fib_index = ~0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530495
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 collector.as_u32 = 0;
497 src.as_u32 = 0;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530498 u32 path_mtu = 512; // RFC 7011 section 10.3.3.
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700499 u32 template_interval = 20;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700500 u8 udp_checksum = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
Swarup Nayak6bcac062017-11-26 23:11:40 +0530502 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
503 {
504 if (unformat (input, "collector %U", unformat_ip4_address, &collector))
505 ;
506 else if (unformat (input, "port %u", &collector_port))
507 ;
508 else if (unformat (input, "src %U", unformat_ip4_address, &src))
509 ;
510 else if (unformat (input, "fib-id %u", &fib_id))
511 {
512 ip4_main_t *im = &ip4_main;
513 uword *p = hash_get (im->fib_index_by_table_id, fib_id);
514 if (!p)
515 return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
516 fib_index = p[0];
517 }
518 else if (unformat (input, "path-mtu %u", &path_mtu))
519 ;
520 else if (unformat (input, "template-interval %u", &template_interval))
521 ;
522 else if (unformat (input, "udp-checksum"))
523 udp_checksum = 1;
524 else
525 break;
526 }
527
Ole Troan5c749732017-03-13 13:39:52 +0100528 if (collector.as_u32 != 0 && src.as_u32 == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529 return clib_error_return (0, "src address required");
530
Swarup Nayak6bcac062017-11-26 23:11:40 +0530531 if (path_mtu > 1450 /* vpp does not support fragmentation */ )
532 return clib_error_return (0, "too big path-mtu value, maximum is 1450");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700533
534 if (path_mtu < 68)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530535 return clib_error_return (0, "too small path-mtu value, minimum is 68");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700536
Juraj Sloboda618ab082016-07-06 06:11:00 -0700537 /* Reset report streams if we are reconfiguring IP addresses */
538 if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
539 frm->src_address.as_u32 != src.as_u32 ||
540 frm->collector_port != collector_port)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530541 vnet_flow_reports_reset (frm);
Juraj Sloboda618ab082016-07-06 06:11:00 -0700542
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 frm->ipfix_collector.as_u32 = collector.as_u32;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700544 frm->collector_port = collector_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 frm->src_address.as_u32 = src.as_u32;
Juraj Sloboda86634f02016-07-01 06:12:58 -0700546 frm->fib_index = fib_index;
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700547 frm->path_mtu = path_mtu;
548 frm->template_interval = template_interval;
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700549 frm->udp_checksum = udp_checksum;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530550
Ole Troan5c749732017-03-13 13:39:52 +0100551 if (collector.as_u32)
552 vlib_cli_output (vm, "Collector %U, src address %U, "
Swarup Nayak6bcac062017-11-26 23:11:40 +0530553 "fib index %d, path MTU %u, "
554 "template resend interval %us, "
555 "udp checksum %s",
556 format_ip4_address, &frm->ipfix_collector,
557 format_ip4_address, &frm->src_address,
558 fib_index, path_mtu, template_interval,
559 udp_checksum ? "enabled" : "disabled");
Ole Troan5c749732017-03-13 13:39:52 +0100560 else
561 vlib_cli_output (vm, "IPFIX Collector is disabled");
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700562
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 /* Turn on the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530564 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 return 0;
566}
567
Swarup Nayak6bcac062017-11-26 23:11:40 +0530568/* *INDENT-OFF* */
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700569VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
570 .path = "set ipfix exporter",
571 .short_help = "set ipfix exporter "
572 "collector <ip4-address> [port <port>] "
Juraj Sloboda5a49bb92016-07-07 03:23:15 -0700573 "src <ip4-address> [fib-id <fib-id>] "
574 "[path-mtu <path-mtu>] "
575 "[template-interval <template-interval>]",
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700576 "[udp-checksum]",
577 .function = set_ipfix_exporter_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530579/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barach0f3b6802016-12-23 15:15:48 -0500581
582static clib_error_t *
583ipfix_flush_command_fn (vlib_main_t * vm,
Swarup Nayak6bcac062017-11-26 23:11:40 +0530584 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach0f3b6802016-12-23 15:15:48 -0500585{
586 /* poke the flow reporting process */
Swarup Nayak6bcac062017-11-26 23:11:40 +0530587 vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
Dave Barach0f3b6802016-12-23 15:15:48 -0500588 return 0;
589}
590
Swarup Nayak6bcac062017-11-26 23:11:40 +0530591/* *INDENT-OFF* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500592VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
593 .path = "ipfix flush",
594 .short_help = "flush the current ipfix data [for make test]",
595 .function = ipfix_flush_command_fn,
596};
Swarup Nayak6bcac062017-11-26 23:11:40 +0530597/* *INDENT-ON* */
Dave Barach0f3b6802016-12-23 15:15:48 -0500598
Swarup Nayak6bcac062017-11-26 23:11:40 +0530599static clib_error_t *
600flow_report_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601{
Swarup Nayak6bcac062017-11-26 23:11:40 +0530602 flow_report_main_t *frm = &flow_report_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 frm->vlib_main = vm;
Swarup Nayak6bcac062017-11-26 23:11:40 +0530605 frm->vnet_main = vnet_get_main ();
606 frm->unix_time_0 = time (0);
607 frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
Juraj Slobodaffa652a2016-08-07 23:43:42 -0700608 frm->fib_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
610 return 0;
611}
612
613VLIB_INIT_FUNCTION (flow_report_init)
Swarup Nayak6bcac062017-11-26 23:11:40 +0530614/*
615 * fd.io coding-style-patch-verification: ON
616 *
617 * Local Variables:
618 * eval: (c-set-style "gnu")
619 * End:
620 */