build-data: Common makefile for NXP DPAA1/DPAA2 platforms
[vpp.git] / src / vnet / flow / flow_report.c
1 /*
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
21 flow_report_main_t flow_report_main;
22
23 static_always_inline u8
24 stream_index_valid (u32 index)
25 {
26   flow_report_main_t *frm = &flow_report_main;
27   return index < vec_len (frm->streams) &&
28     frm->streams[index].domain_id != ~0;
29 }
30
31 static_always_inline flow_report_stream_t *
32 add_stream (void)
33 {
34   flow_report_main_t *frm = &flow_report_main;
35   u32 i;
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];
42 }
43
44 static_always_inline void
45 delete_stream (u32 index)
46 {
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;
51 }
52
53 static i32
54 find_stream (u32 domain_id, u16 src_port)
55 {
56   flow_report_main_t *frm = &flow_report_main;
57   flow_report_stream_t *stream;
58   u32 i;
59   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           }
73       }
74   return -1;
75 }
76
77 int
78 send_template_packet (flow_report_main_t * frm,
79                       flow_report_t * fr, u32 * buffer_indexp)
80 {
81   u32 bi0;
82   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;
89   vlib_buffer_free_list_t *fl;
90
91   ASSERT (buffer_indexp);
92
93   if (fr->update_rewrite || fr->rewrite == 0)
94     {
95       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         }
101       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,
108                                           &frm->ipfix_collector,
109                                           &frm->src_address,
110                                           frm->collector_port);
111       fr->update_rewrite = 0;
112     }
113
114   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
115     return -1;
116
117   b0 = vlib_get_buffer (vm, bi0);
118
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);
123
124   ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
125
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;
132
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);
137
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);
143
144   stream = &frm->streams[fr->stream_index];
145
146   /* FIXUP: message header sequence_number. Templates do not increase it */
147   h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
148
149   /* FIXUP: udp length */
150   udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
151
152   if (frm->udp_checksum)
153     {
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;
158     }
159
160   *buffer_indexp = bi0;
161
162   fr->last_template_sent = vlib_time_now (vm);
163
164   return 0;
165 }
166
167 static uword
168 flow_report_process (vlib_main_t * vm,
169                      vlib_node_runtime_t * rt, vlib_frame_t * f)
170 {
171   flow_report_main_t *frm = &flow_report_main;
172   flow_report_t *fr;
173   u32 ip4_lookup_node_index;
174   vlib_node_t *ip4_lookup_node;
175   vlib_frame_t *nf = 0;
176   u32 template_bi;
177   u32 *to_next;
178   int send_template;
179   f64 now;
180   int rv;
181   uword event_type;
182   uword *event_data = 0;
183
184   /* Wait for Godot... */
185   vlib_process_wait_for_event_or_clock (vm, 1e9);
186   event_type = vlib_process_get_events (vm, &event_data);
187   if (event_type != 1)
188     clib_warning ("bogus kickoff event received, %d", event_type);
189   vec_reset_length (event_data);
190
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;
194
195   while (1)
196     {
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);
200
201       vec_foreach (fr, frm->reports)
202       {
203         now = vlib_time_now (vm);
204
205         /* Need to send a template packet? */
206         send_template =
207           now > (fr->last_template_sent + frm->template_interval);
208         send_template += fr->last_template_sent == 0;
209         template_bi = ~0;
210         rv = 0;
211
212         if (send_template)
213           rv = send_template_packet (frm, fr, &template_bi);
214
215         if (rv < 0)
216           continue;
217
218         nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
219         nf->n_vectors = 0;
220         to_next = vlib_frame_vector_args (nf);
221
222         if (template_bi != ~0)
223           {
224             to_next[0] = template_bi;
225             to_next++;
226             nf->n_vectors++;
227           }
228
229         nf = fr->flow_data_callback (frm, fr,
230                                      nf, to_next, ip4_lookup_node_index);
231         if (nf)
232           vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
233       }
234     }
235
236   return 0;                     /* not so much */
237 }
238
239 /* *INDENT-OFF* */
240 VLIB_REGISTER_NODE (flow_report_process_node) = {
241     .function = flow_report_process,
242     .type = VLIB_NODE_TYPE_PROCESS,
243     .name = "flow-report-process",
244 };
245 /* *INDENT-ON* */
246
247 int
248 vnet_flow_report_add_del (flow_report_main_t * frm,
249                           vnet_flow_report_add_del_args_t * a,
250                           u16 * template_id)
251 {
252   int i;
253   int found_index = ~0;
254   flow_report_t *fr;
255   flow_report_stream_t *stream;
256   u32 si;
257
258   si = find_stream (a->domain_id, a->src_port);
259   if (si == -2)
260     return VNET_API_ERROR_INVALID_VALUE;
261   if (si == -1 && a->is_add == 0)
262     return VNET_API_ERROR_NO_SUCH_ENTRY;
263
264   for (i = 0; i < vec_len (frm->reports); i++)
265     {
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)
270         {
271           found_index = i;
272           if (template_id)
273             *template_id = fr->template_id;
274           break;
275         }
276     }
277
278   if (a->is_add == 0)
279     {
280       if (found_index != ~0)
281         {
282           vec_delete (frm->reports, 1, found_index);
283           stream = &frm->streams[si];
284           stream->n_reports--;
285           if (stream->n_reports == 0)
286             delete_stream (si);
287           return 0;
288         }
289       return VNET_API_ERROR_NO_SUCH_ENTRY;
290     }
291
292   if (found_index != ~0)
293     return VNET_API_ERROR_VALUE_EXIST;
294
295   if (si == -1)
296     {
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;
303     }
304   else
305     stream = &frm->streams[si];
306
307   stream->n_reports++;
308
309   vec_add2 (frm->reports, fr, 1);
310
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;
318
319   if (template_id)
320     *template_id = fr->template_id;
321
322   return 0;
323 }
324
325 clib_error_t *
326 flow_report_add_del_error_to_clib_error (int error)
327 {
328   switch (error)
329     {
330     case 0:
331       return 0;
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");
340     default:
341       return clib_error_return (0, "vnet_flow_report_add_del returned %d",
342                                 error);
343     }
344 }
345
346 void
347 vnet_flow_reports_reset (flow_report_main_t * frm)
348 {
349   flow_report_t *fr;
350   u32 i;
351
352   for (i = 0; i < vec_len (frm->streams); i++)
353     if (stream_index_valid (i))
354       frm->streams[i].sequence_number = 0;
355
356   vec_foreach (fr, frm->reports)
357   {
358     fr->update_rewrite = 1;
359     fr->last_template_sent = 0;
360   }
361 }
362
363 void
364 vnet_stream_reset (flow_report_main_t * frm, u32 stream_index)
365 {
366   flow_report_t *fr;
367
368   frm->streams[stream_index].sequence_number = 0;
369
370   vec_foreach (fr, frm->reports)
371     if (frm->reports->stream_index == stream_index)
372     {
373       fr->update_rewrite = 1;
374       fr->last_template_sent = 0;
375     }
376 }
377
378 int
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)
382 {
383   i32 stream_index = find_stream (old_domain_id, old_src_port);
384   if (stream_index < 0)
385     return 1;
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);
391   return 0;
392 }
393
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)
398 {
399   flow_report_main_t *frm = &flow_report_main;
400   ip4_address_t collector, src;
401   u16 collector_port = UDP_DST_PORT_ipfix;
402   u32 fib_id;
403   u32 fib_index = ~0;
404
405   collector.as_u32 = 0;
406   src.as_u32 = 0;
407   u32 path_mtu = 512;           // RFC 7011 section 10.3.3.
408   u32 template_interval = 20;
409   u8 udp_checksum = 0;
410
411   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
412     {
413       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
414         ;
415       else if (unformat (input, "port %u", &collector_port))
416         ;
417       else if (unformat (input, "src %U", unformat_ip4_address, &src))
418         ;
419       else if (unformat (input, "fib-id %u", &fib_id))
420         {
421           ip4_main_t *im = &ip4_main;
422           uword *p = hash_get (im->fib_index_by_table_id, fib_id);
423           if (!p)
424             return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
425           fib_index = p[0];
426         }
427       else if (unformat (input, "path-mtu %u", &path_mtu))
428         ;
429       else if (unformat (input, "template-interval %u", &template_interval))
430         ;
431       else if (unformat (input, "udp-checksum"))
432         udp_checksum = 1;
433       else
434         break;
435     }
436
437   if (collector.as_u32 != 0 && src.as_u32 == 0)
438     return clib_error_return (0, "src address required");
439
440   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
441     return clib_error_return (0, "too big path-mtu value, maximum is 1450");
442
443   if (path_mtu < 68)
444     return clib_error_return (0, "too small path-mtu value, minimum is 68");
445
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);
451
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;
459
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, "
464                      "udp checksum %s",
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");
469   else
470     vlib_cli_output (vm, "IPFIX Collector is disabled");
471
472   /* Turn on the flow reporting process */
473   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
474   return 0;
475 }
476
477 /* *INDENT-OFF* */
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>]",
485                   "[udp-checksum]",
486     .function = set_ipfix_exporter_command_fn,
487 };
488 /* *INDENT-ON* */
489
490
491 static clib_error_t *
492 ipfix_flush_command_fn (vlib_main_t * vm,
493                         unformat_input_t * input, vlib_cli_command_t * cmd)
494 {
495   /* poke the flow reporting process */
496   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
497   return 0;
498 }
499
500 /* *INDENT-OFF* */
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,
505 };
506 /* *INDENT-ON* */
507
508 static clib_error_t *
509 flow_report_init (vlib_main_t * vm)
510 {
511   flow_report_main_t *frm = &flow_report_main;
512
513   frm->vlib_main = vm;
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);
517   frm->fib_index = ~0;
518
519   return 0;
520 }
521
522 VLIB_INIT_FUNCTION (flow_report_init)
523 /*
524  * fd.io coding-style-patch-verification: ON
525  *
526  * Local Variables:
527  * eval: (c-set-style "gnu")
528  * End:
529  */