vlib: refactor trajectory trace debug feature
[vpp.git] / src / vnet / ipfix-export / 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/ipfix-export/flow_report.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/udp/udp.h>
21
22 flow_report_main_t flow_report_main;
23
24 static_always_inline u8
25 stream_index_valid (u32 index)
26 {
27   flow_report_main_t *frm = &flow_report_main;
28   return index < vec_len (frm->streams) &&
29     frm->streams[index].domain_id != ~0;
30 }
31
32 static_always_inline flow_report_stream_t *
33 add_stream (void)
34 {
35   flow_report_main_t *frm = &flow_report_main;
36   u32 i;
37   for (i = 0; i < vec_len (frm->streams); i++)
38     if (!stream_index_valid (i))
39       return &frm->streams[i];
40   u32 index = vec_len (frm->streams);
41   vec_validate (frm->streams, index);
42   return &frm->streams[index];
43 }
44
45 static_always_inline void
46 delete_stream (u32 index)
47 {
48   flow_report_main_t *frm = &flow_report_main;
49   ASSERT (index < vec_len (frm->streams));
50   ASSERT (frm->streams[index].domain_id != ~0);
51   frm->streams[index].domain_id = ~0;
52 }
53
54 static i32
55 find_stream (u32 domain_id, u16 src_port)
56 {
57   flow_report_main_t *frm = &flow_report_main;
58   flow_report_stream_t *stream;
59   u32 i;
60   for (i = 0; i < vec_len (frm->streams); i++)
61     if (stream_index_valid (i))
62       {
63         stream = &frm->streams[i];
64         if (domain_id == stream->domain_id)
65           {
66             if (src_port != stream->src_port)
67               return -2;
68             return i;
69           }
70         else if (src_port == stream->src_port)
71           {
72             return -2;
73           }
74       }
75   return -1;
76 }
77
78 int
79 send_template_packet (flow_report_main_t * frm,
80                       flow_report_t * fr, u32 * buffer_indexp)
81 {
82   u32 bi0;
83   vlib_buffer_t *b0;
84   ip4_ipfix_template_packet_t *tp;
85   ipfix_message_header_t *h;
86   ip4_header_t *ip;
87   udp_header_t *udp;
88   vlib_main_t *vm = frm->vlib_main;
89   flow_report_stream_t *stream;
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->report_elements,
112                                           fr->n_report_elements,
113                                           fr->stream_indexp);
114       fr->update_rewrite = 0;
115     }
116
117   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
118     return -1;
119
120   b0 = vlib_get_buffer (vm, bi0);
121
122   ASSERT (vec_len (fr->rewrite) < vlib_buffer_get_default_data_size (vm));
123
124   clib_memcpy_fast (b0->data, fr->rewrite, vec_len (fr->rewrite));
125   b0->current_data = 0;
126   b0->current_length = vec_len (fr->rewrite);
127   b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
128   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
129   vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
130
131   tp = vlib_buffer_get_current (b0);
132   ip = (ip4_header_t *) & tp->ip4;
133   udp = (udp_header_t *) (ip + 1);
134   h = (ipfix_message_header_t *) (udp + 1);
135
136   /* FIXUP: message header export_time */
137   h->export_time = (u32)
138     (((f64) frm->unix_time_0) +
139      (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
140   h->export_time = clib_host_to_net_u32 (h->export_time);
141
142   stream = &frm->streams[fr->stream_index];
143
144   /* FIXUP: message header sequence_number. Templates do not increase it */
145   h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
146
147   /* FIXUP: udp length */
148   udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
149
150   if (frm->udp_checksum)
151     {
152       /* RFC 7011 section 10.3.2. */
153       udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
154       if (udp->checksum == 0)
155         udp->checksum = 0xffff;
156     }
157
158   *buffer_indexp = bi0;
159
160   fr->last_template_sent = vlib_time_now (vm);
161
162   return 0;
163 }
164
165 u8 *
166 vnet_flow_rewrite_generic_callback (flow_report_main_t * frm,
167                                     flow_report_t * fr,
168                                     ip4_address_t * collector_address,
169                                     ip4_address_t * src_address,
170                                     u16 collector_port,
171                                     ipfix_report_element_t * report_elts,
172                                     u32 n_elts, u32 * stream_indexp)
173 {
174   ip4_header_t *ip;
175   udp_header_t *udp;
176   ipfix_message_header_t *h;
177   ipfix_set_header_t *s;
178   ipfix_template_header_t *t;
179   ipfix_field_specifier_t *f;
180   ipfix_field_specifier_t *first_field;
181   u8 *rewrite = 0;
182   ip4_ipfix_template_packet_t *tp;
183   flow_report_stream_t *stream;
184   int i;
185   ipfix_report_element_t *ep;
186
187   ASSERT (stream_indexp);
188   ASSERT (n_elts);
189   ASSERT (report_elts);
190
191   stream = &frm->streams[fr->stream_index];
192   *stream_indexp = fr->stream_index;
193
194   /* allocate rewrite space */
195   vec_validate_aligned (rewrite,
196                         sizeof (ip4_ipfix_template_packet_t)
197                         + n_elts * sizeof (ipfix_field_specifier_t) - 1,
198                         CLIB_CACHE_LINE_BYTES);
199
200   /* create the packet rewrite string */
201   tp = (ip4_ipfix_template_packet_t *) rewrite;
202   ip = (ip4_header_t *) & tp->ip4;
203   udp = (udp_header_t *) (ip + 1);
204   h = (ipfix_message_header_t *) (udp + 1);
205   s = (ipfix_set_header_t *) (h + 1);
206   t = (ipfix_template_header_t *) (s + 1);
207   first_field = f = (ipfix_field_specifier_t *) (t + 1);
208
209   ip->ip_version_and_header_length = 0x45;
210   ip->ttl = 254;
211   ip->protocol = IP_PROTOCOL_UDP;
212   ip->src_address.as_u32 = src_address->as_u32;
213   ip->dst_address.as_u32 = collector_address->as_u32;
214   udp->src_port = clib_host_to_net_u16 (stream->src_port);
215   udp->dst_port = clib_host_to_net_u16 (collector_port);
216   udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
217
218   /* FIXUP LATER: message header export_time */
219   h->domain_id = clib_host_to_net_u32 (stream->domain_id);
220
221   ep = report_elts;
222
223   for (i = 0; i < n_elts; i++)
224     {
225       f->e_id_length = ipfix_e_id_length (0, ep->info_element, ep->size);
226       f++;
227       ep++;
228     }
229
230   /* Back to the template packet... */
231   ip = (ip4_header_t *) & tp->ip4;
232   udp = (udp_header_t *) (ip + 1);
233
234   ASSERT (f - first_field);
235   /* Field count in this template */
236   t->id_count = ipfix_id_count (fr->template_id, f - first_field);
237
238   /* set length in octets */
239   s->set_id_length =
240     ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
241
242   /* message length in octets */
243   h->version_length = version_length ((u8 *) f - (u8 *) h);
244
245   ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
246   ip->checksum = ip4_header_checksum (ip);
247
248   return rewrite;
249 }
250
251 static uword
252 flow_report_process (vlib_main_t * vm,
253                      vlib_node_runtime_t * rt, vlib_frame_t * f)
254 {
255   flow_report_main_t *frm = &flow_report_main;
256   flow_report_t *fr;
257   u32 ip4_lookup_node_index;
258   vlib_node_t *ip4_lookup_node;
259   vlib_frame_t *nf = 0;
260   u32 template_bi;
261   u32 *to_next;
262   int send_template;
263   f64 now;
264   int rv;
265   uword event_type;
266   uword *event_data = 0;
267
268   /* Wait for Godot... */
269   vlib_process_wait_for_event_or_clock (vm, 1e9);
270   event_type = vlib_process_get_events (vm, &event_data);
271   if (event_type != 1)
272     clib_warning ("bogus kickoff event received, %d", event_type);
273   vec_reset_length (event_data);
274
275   /* Enqueue pkts to ip4-lookup */
276   ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
277   ip4_lookup_node_index = ip4_lookup_node->index;
278
279   while (1)
280     {
281       vlib_process_wait_for_event_or_clock (vm, 5.0);
282       event_type = vlib_process_get_events (vm, &event_data);
283       vec_reset_length (event_data);
284
285       vec_foreach (fr, frm->reports)
286       {
287         now = vlib_time_now (vm);
288
289         /* Need to send a template packet? */
290         send_template =
291           now > (fr->last_template_sent + frm->template_interval);
292         send_template += fr->last_template_sent == 0;
293         template_bi = ~0;
294         rv = 0;
295
296         if (send_template)
297           rv = send_template_packet (frm, fr, &template_bi);
298
299         if (rv < 0)
300           continue;
301
302         nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
303         nf->n_vectors = 0;
304         to_next = vlib_frame_vector_args (nf);
305
306         if (template_bi != ~0)
307           {
308             to_next[0] = template_bi;
309             to_next++;
310             nf->n_vectors++;
311           }
312
313         nf = fr->flow_data_callback (frm, fr,
314                                      nf, to_next, ip4_lookup_node_index);
315         if (nf)
316           vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
317       }
318     }
319
320   return 0;                     /* not so much */
321 }
322
323 /* *INDENT-OFF* */
324 VLIB_REGISTER_NODE (flow_report_process_node) = {
325     .function = flow_report_process,
326     .type = VLIB_NODE_TYPE_PROCESS,
327     .name = "flow-report-process",
328 };
329 /* *INDENT-ON* */
330
331 int
332 vnet_flow_report_add_del (flow_report_main_t * frm,
333                           vnet_flow_report_add_del_args_t * a,
334                           u16 * template_id)
335 {
336   int i;
337   int found_index = ~0;
338   flow_report_t *fr;
339   flow_report_stream_t *stream;
340   u32 si;
341
342   si = find_stream (a->domain_id, a->src_port);
343   if (si == -2)
344     return VNET_API_ERROR_INVALID_VALUE;
345   if (si == -1 && a->is_add == 0)
346     return VNET_API_ERROR_NO_SUCH_ENTRY;
347
348   for (i = 0; i < vec_len (frm->reports); i++)
349     {
350       fr = vec_elt_at_index (frm->reports, i);
351       if (fr->opaque.as_uword == a->opaque.as_uword
352           && fr->rewrite_callback == a->rewrite_callback
353           && fr->flow_data_callback == a->flow_data_callback)
354         {
355           found_index = i;
356           if (template_id)
357             *template_id = fr->template_id;
358           break;
359         }
360     }
361
362   if (a->is_add == 0)
363     {
364       if (found_index != ~0)
365         {
366           vec_delete (frm->reports, 1, found_index);
367           stream = &frm->streams[si];
368           stream->n_reports--;
369           if (stream->n_reports == 0)
370             delete_stream (si);
371           return 0;
372         }
373       return VNET_API_ERROR_NO_SUCH_ENTRY;
374     }
375
376   if (found_index != ~0)
377     return VNET_API_ERROR_VALUE_EXIST;
378
379   if (si == -1)
380     {
381       stream = add_stream ();
382       stream->domain_id = a->domain_id;
383       stream->src_port = a->src_port;
384       stream->sequence_number = 0;
385       stream->n_reports = 0;
386       si = stream - frm->streams;
387     }
388   else
389     stream = &frm->streams[si];
390
391   stream->n_reports++;
392
393   vec_add2 (frm->reports, fr, 1);
394
395   fr->stream_index = si;
396   fr->template_id = 256 + stream->next_template_no;
397   stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
398   fr->update_rewrite = 1;
399   fr->opaque = a->opaque;
400   fr->rewrite_callback = a->rewrite_callback;
401   fr->flow_data_callback = a->flow_data_callback;
402   fr->report_elements = a->report_elements;
403   fr->n_report_elements = a->n_report_elements;
404   fr->stream_indexp = a->stream_indexp;
405   if (template_id)
406     *template_id = fr->template_id;
407
408   return 0;
409 }
410
411 clib_error_t *
412 flow_report_add_del_error_to_clib_error (int error)
413 {
414   switch (error)
415     {
416     case 0:
417       return 0;
418     case VNET_API_ERROR_NO_SUCH_ENTRY:
419       return clib_error_return (0, "Flow report not found");
420     case VNET_API_ERROR_VALUE_EXIST:
421       return clib_error_return (0, "Flow report already exists");
422     case VNET_API_ERROR_INVALID_VALUE:
423       return clib_error_return (0, "Expecting either still unused values "
424                                 "for both domain_id and src_port "
425                                 "or already used values for both fields");
426     default:
427       return clib_error_return (0, "vnet_flow_report_add_del returned %d",
428                                 error);
429     }
430 }
431
432 void
433 vnet_flow_reports_reset (flow_report_main_t * frm)
434 {
435   flow_report_t *fr;
436   u32 i;
437
438   for (i = 0; i < vec_len (frm->streams); i++)
439     if (stream_index_valid (i))
440       frm->streams[i].sequence_number = 0;
441
442   vec_foreach (fr, frm->reports)
443   {
444     fr->update_rewrite = 1;
445     fr->last_template_sent = 0;
446   }
447 }
448
449 void
450 vnet_stream_reset (flow_report_main_t * frm, u32 stream_index)
451 {
452   flow_report_t *fr;
453
454   frm->streams[stream_index].sequence_number = 0;
455
456   vec_foreach (fr, frm->reports)
457     if (frm->reports->stream_index == stream_index)
458     {
459       fr->update_rewrite = 1;
460       fr->last_template_sent = 0;
461     }
462 }
463
464 int
465 vnet_stream_change (flow_report_main_t * frm,
466                     u32 old_domain_id, u16 old_src_port,
467                     u32 new_domain_id, u16 new_src_port)
468 {
469   i32 stream_index = find_stream (old_domain_id, old_src_port);
470   if (stream_index < 0)
471     return 1;
472   flow_report_stream_t *stream = &frm->streams[stream_index];
473   stream->domain_id = new_domain_id;
474   stream->src_port = new_src_port;
475   if (old_domain_id != new_domain_id || old_src_port != new_src_port)
476     vnet_stream_reset (frm, stream_index);
477   return 0;
478 }
479
480 static clib_error_t *
481 set_ipfix_exporter_command_fn (vlib_main_t * vm,
482                                unformat_input_t * input,
483                                vlib_cli_command_t * cmd)
484 {
485   flow_report_main_t *frm = &flow_report_main;
486   ip4_address_t collector, src;
487   u16 collector_port = UDP_DST_PORT_ipfix;
488   u32 fib_id;
489   u32 fib_index = ~0;
490
491   collector.as_u32 = 0;
492   src.as_u32 = 0;
493   u32 path_mtu = 512;           // RFC 7011 section 10.3.3.
494   u32 template_interval = 20;
495   u8 udp_checksum = 0;
496
497   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
498     {
499       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
500         ;
501       else if (unformat (input, "port %U", unformat_udp_port,
502                          &collector_port))
503         ;
504       else if (unformat (input, "src %U", unformat_ip4_address, &src))
505         ;
506       else if (unformat (input, "fib-id %u", &fib_id))
507         {
508           ip4_main_t *im = &ip4_main;
509           uword *p = hash_get (im->fib_index_by_table_id, fib_id);
510           if (!p)
511             return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
512           fib_index = p[0];
513         }
514       else if (unformat (input, "path-mtu %u", &path_mtu))
515         ;
516       else if (unformat (input, "template-interval %u", &template_interval))
517         ;
518       else if (unformat (input, "udp-checksum"))
519         udp_checksum = 1;
520       else
521         break;
522     }
523
524   if (collector.as_u32 != 0 && src.as_u32 == 0)
525     return clib_error_return (0, "src address required");
526
527   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
528     return clib_error_return (0, "too big path-mtu value, maximum is 1450");
529
530   if (path_mtu < 68)
531     return clib_error_return (0, "too small path-mtu value, minimum is 68");
532
533   /* Reset report streams if we are reconfiguring IP addresses */
534   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
535       frm->src_address.as_u32 != src.as_u32 ||
536       frm->collector_port != collector_port)
537     vnet_flow_reports_reset (frm);
538
539   frm->ipfix_collector.as_u32 = collector.as_u32;
540   frm->collector_port = collector_port;
541   frm->src_address.as_u32 = src.as_u32;
542   frm->fib_index = fib_index;
543   frm->path_mtu = path_mtu;
544   frm->template_interval = template_interval;
545   frm->udp_checksum = udp_checksum;
546
547   if (collector.as_u32)
548     vlib_cli_output (vm, "Collector %U, src address %U, "
549                      "fib index %d, path MTU %u, "
550                      "template resend interval %us, "
551                      "udp checksum %s",
552                      format_ip4_address, &frm->ipfix_collector,
553                      format_ip4_address, &frm->src_address,
554                      fib_index, path_mtu, template_interval,
555                      udp_checksum ? "enabled" : "disabled");
556   else
557     vlib_cli_output (vm, "IPFIX Collector is disabled");
558
559   /* Turn on the flow reporting process */
560   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
561   return 0;
562 }
563
564 /* *INDENT-OFF* */
565 VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
566     .path = "set ipfix exporter",
567     .short_help = "set ipfix exporter "
568                   "collector <ip4-address> [port <port>] "
569                   "src <ip4-address> [fib-id <fib-id>] "
570                   "[path-mtu <path-mtu>] "
571                   "[template-interval <template-interval>] "
572                   "[udp-checksum]",
573     .function = set_ipfix_exporter_command_fn,
574 };
575 /* *INDENT-ON* */
576
577
578 static clib_error_t *
579 ipfix_flush_command_fn (vlib_main_t * vm,
580                         unformat_input_t * input, vlib_cli_command_t * cmd)
581 {
582   /* poke the flow reporting process */
583   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
584   return 0;
585 }
586
587 /* *INDENT-OFF* */
588 VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
589     .path = "ipfix flush",
590     .short_help = "flush the current ipfix data [for make test]",
591     .function = ipfix_flush_command_fn,
592 };
593 /* *INDENT-ON* */
594
595 static clib_error_t *
596 flow_report_init (vlib_main_t * vm)
597 {
598   flow_report_main_t *frm = &flow_report_main;
599
600   frm->vlib_main = vm;
601   frm->vnet_main = vnet_get_main ();
602   frm->unix_time_0 = time (0);
603   frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
604   frm->fib_index = ~0;
605
606   return 0;
607 }
608
609 VLIB_INIT_FUNCTION (flow_report_init);
610 /*
611  * fd.io coding-style-patch-verification: ON
612  *
613  * Local Variables:
614  * eval: (c-set-style "gnu")
615  * End:
616  */