ipfix-export: refactor fields in flow_report_main
[vpp.git] / src / plugins / ioam / analyse / ioam_summary_export.c
1 /*
2  * Copyright (c) 2017 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 #include <vlib/vlib.h>
17 #include <vnet/ip/ip6_packet.h>
18 #include <vnet/udp/udp_local.h>
19 #include <ioam/analyse/ioam_summary_export.h>
20 #include <ioam/analyse/ip6/ip6_ioam_analyse.h>
21
22 u8 *
23 ioam_template_rewrite (flow_report_main_t * frm, flow_report_t * fr,
24                        ip4_address_t * collector_address,
25                        ip4_address_t * src_address, u16 collector_port,
26                        ipfix_report_element_t * elts,
27                        u32 n_elts, u32 * stream_index)
28 {
29   ip4_header_t *ip;
30   udp_header_t *udp;
31   ipfix_message_header_t *h;
32   ipfix_set_header_t *s;
33   ipfix_template_header_t *t;
34   ipfix_field_specifier_t *f;
35   ipfix_field_specifier_t *first_field;
36   u8 *rewrite = 0;
37   ip4_ipfix_template_packet_t *tp;
38   u32 field_count = 0;
39   u32 field_index = 0;
40   flow_report_stream_t *stream;
41   ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
42
43   stream = &exp->streams[fr->stream_index];
44
45   /* Determine field count */
46 #define _(field,mask,item,length)                                   \
47     {                                                               \
48   field_count++;                                                    \
49   fr->fields_to_send = clib_bitmap_set (fr->fields_to_send,         \
50                                         field_index, 1);            \
51     }                                                               \
52     field_index++;
53
54   foreach_ioam_ipfix_field;
55 #undef _
56
57   /* Add Src address, dest address, src port, dest port
58    * path map,  number of paths manually */
59   field_count += 6;
60
61   /* allocate rewrite space */
62   vec_validate_aligned (rewrite,
63                         sizeof (ip4_ipfix_template_packet_t)
64                         + field_count * sizeof (ipfix_field_specifier_t) - 1,
65                         CLIB_CACHE_LINE_BYTES);
66
67   tp = (ip4_ipfix_template_packet_t *) rewrite;
68   ip = (ip4_header_t *) & tp->ip4;
69   udp = (udp_header_t *) (ip + 1);
70   h = (ipfix_message_header_t *) (udp + 1);
71   s = (ipfix_set_header_t *) (h + 1);
72   t = (ipfix_template_header_t *) (s + 1);
73   first_field = f = (ipfix_field_specifier_t *) (t + 1);
74
75   ip->ip_version_and_header_length = 0x45;
76   ip->ttl = 254;
77   ip->protocol = IP_PROTOCOL_UDP;
78   ip->src_address.as_u32 = src_address->as_u32;
79   ip->dst_address.as_u32 = collector_address->as_u32;
80   udp->src_port = clib_host_to_net_u16 (collector_port);
81   udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
82   udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
83
84   h->domain_id = clib_host_to_net_u32 (stream->domain_id);      //fr->domain_id);
85
86   /* Add Src address, dest address, src port, dest port
87    * path map,  number of paths manually */
88   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
89                                       sourceIPv6Address,
90                                       sizeof (ip6_address_t));
91   f++;
92
93   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
94                                       destinationIPv6Address,
95                                       sizeof (ip6_address_t));
96   f++;
97
98   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
99                                       sourceTransportPort, 2);
100   f++;
101
102   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
103                                       destinationTransportPort, 2);
104   f++;
105
106 #define _(field,mask,item,length)                               \
107     {                                                           \
108   f->e_id_length = ipfix_e_id_length (0 /* enterprise */,       \
109     item, length);                                              \
110     f++;                                                        \
111     }
112   foreach_ioam_ipfix_field;
113 #undef _
114
115   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
116                                       ioamNumberOfPaths, 2);
117   f++;
118
119   /* Add ioamPathMap manually */
120   f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
121                                       ioamPathMap,
122                                       (sizeof (ioam_path) +
123                                        (sizeof (ioam_path_map_t) *
124                                         IOAM_TRACE_MAX_NODES)));
125   f++;
126
127   /* Back to the template packet... */
128   ip = (ip4_header_t *) & tp->ip4;
129   udp = (udp_header_t *) (ip + 1);
130
131   ASSERT (f - first_field);
132   /* Field count in this template */
133   t->id_count = ipfix_id_count (IOAM_FLOW_TEMPLATE_ID, f - first_field);
134
135   /* set length in octets */
136   s->set_id_length =
137     ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
138
139   /* message length in octets */
140   h->version_length = version_length ((u8 *) f - (u8 *) h);
141
142   ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
143   ip->checksum = ip4_header_checksum (ip);
144
145   return rewrite;
146 }
147
148 u16
149 ioam_analyse_add_ipfix_record (flow_report_t * fr,
150                                ioam_analyser_data_t * record,
151                                vlib_buffer_t * b0, u16 offset,
152                                ip6_address_t * src, ip6_address_t * dst,
153                                u16 src_port, u16 dst_port)
154 {
155   clib_spinlock_lock (&record->writer_lock);
156
157   int field_index = 0;
158   u16 tmp;
159   int i, j;
160   u16 num_paths = 0;
161   u16 num_paths_offset;
162
163
164   /* Add IPv6 source address manually */
165   memcpy (b0->data + offset, &src->as_u64[0], sizeof (u64));
166   offset += sizeof (u64);
167   memcpy (b0->data + offset, &src->as_u64[1], sizeof (u64));
168   offset += sizeof (u64);
169
170   /* Add IPv6 destination address manually */
171   memcpy (b0->data + offset, &dst->as_u64[0], sizeof (u64));
172   offset += sizeof (u64);
173   memcpy (b0->data + offset, &dst->as_u64[1], sizeof (u64));
174   offset += sizeof (u64);
175
176   /* Add source port manually */
177   tmp = clib_host_to_net_u16 (src_port);
178   memcpy (b0->data + offset, &tmp, sizeof (u16));
179   offset += sizeof (u16);
180
181   /* Add dest port manually */
182   tmp = clib_host_to_net_u16 (dst_port);
183   memcpy (b0->data + offset, &tmp, sizeof (u16));
184   offset += sizeof (u16);
185
186 #define _(field,mask,item,length)                            \
187     if (clib_bitmap_get (fr->fields_to_send, field_index))   \
188     {                                                        \
189       /* Expect only 4 bytes */               \
190       u32 tmp;                                             \
191       tmp = clib_host_to_net_u32((u32)record->field - (u32)record->chached_data_list->field);\
192       memcpy (b0->data + offset, &tmp, length);       \
193       offset += length;                                 \
194     }
195   field_index++;
196   foreach_ioam_ipfix_field;
197 #undef _
198
199   /* Store num_paths_offset here and update later */
200   num_paths_offset = offset;
201   offset += sizeof (u16);
202
203   /* Add ioamPathMap manually */
204   for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
205     {
206       ioam_analyse_trace_record *trace = record->trace_data.path_data + i;
207       ioam_analyse_trace_record *trace_cached =
208         record->chached_data_list->trace_data.path_data + i;
209       ioam_path *path = (ioam_path *) (b0->data + offset);
210
211       if (!trace->is_free)
212         {
213           num_paths++;
214
215           path->num_nodes = trace->num_nodes;
216
217           path->trace_type = trace->trace_type;
218           if (0 < (trace->pkt_counter - trace_cached->pkt_counter))
219             {
220               u64 new_sum = trace->mean_delay * record->seqno_data.rx_packets;
221               u64 old_sum =
222                 trace_cached->mean_delay *
223                 record->chached_data_list->seqno_data.rx_packets;
224               path->mean_delay =
225                 (u32) ((new_sum - old_sum) / (trace->pkt_counter -
226                                               trace_cached->pkt_counter));
227               path->mean_delay = clib_host_to_net_u32 (path->mean_delay);
228             }
229           else
230             path->mean_delay = 0;
231
232           path->bytes_counter =
233             trace->bytes_counter - trace_cached->bytes_counter;
234           path->bytes_counter = clib_host_to_net_u32 (path->bytes_counter);
235
236           path->pkt_counter = trace->pkt_counter - trace_cached->pkt_counter;
237           path->pkt_counter = clib_host_to_net_u32 (path->pkt_counter);
238           offset += sizeof (ioam_path);
239
240           for (j = 0; j < trace->num_nodes; j++)
241             {
242               path->path[j].node_id =
243                 clib_host_to_net_u32 (trace->path[j].node_id);
244               path->path[j].ingress_if =
245                 clib_host_to_net_u16 (trace->path[j].ingress_if);
246               path->path[j].egress_if =
247                 clib_host_to_net_u16 (trace->path[j].egress_if);
248               path->path[j].state_up = trace->path[j].state_up;
249             }
250
251           //offset += (sizeof(ioam_path_map_t) * trace->num_nodes);
252           offset += (sizeof (ioam_path_map_t) * IOAM_TRACE_MAX_NODES);  //FIXME
253         }
254     }
255
256   num_paths = clib_host_to_net_u16 (num_paths);
257   memcpy (b0->data + num_paths_offset, &num_paths, sizeof (u16));
258
259   /* Update cache */
260   *(record->chached_data_list) = *record;
261   record->chached_data_list->chached_data_list = NULL;
262
263   clib_spinlock_unlock (&record->writer_lock);
264   return offset;
265 }
266
267 vlib_frame_t *
268 ioam_send_flows (flow_report_main_t * frm, flow_report_t * fr,
269                  vlib_frame_t * f, u32 * to_next, u32 node_index)
270 {
271   vlib_buffer_t *b0 = NULL;
272   u32 next_offset = 0;
273   u32 bi0 = ~0;
274   int i;
275   ip4_ipfix_template_packet_t *tp;
276   ipfix_message_header_t *h;
277   ipfix_set_header_t *s = NULL;
278   ip4_header_t *ip;
279   udp_header_t *udp;
280   u32 records_this_buffer;
281   u16 new_l0, old_l0;
282   ip_csum_t sum0;
283   vlib_main_t *vm = frm->vlib_main;
284   ip6_address_t temp;
285   ioam_analyser_data_t *record = NULL;
286   flow_report_stream_t *stream;
287   ioam_analyser_data_t *aggregated_data;
288   u16 data_len;
289   ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, 0);
290
291   stream = &exp->streams[fr->stream_index];
292
293   clib_memset (&temp, 0, sizeof (ip6_address_t));
294
295   aggregated_data = ioam_analyser_main.aggregated_data;
296   data_len = vec_len (aggregated_data);
297
298   vec_foreach_index (i, aggregated_data)
299   {
300     u8 flush = 0;
301     record = aggregated_data + i;
302
303     /* Flush if last entry */
304     if (i == (data_len - 1))
305       flush = 1;
306
307     if (!record->is_free)
308       {
309
310         if (PREDICT_FALSE (b0 == NULL))
311           {
312             if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
313               break;
314
315             b0 = vlib_get_buffer (vm, bi0);
316             memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
317             b0->current_data = 0;
318             b0->current_length = vec_len (fr->rewrite);
319             b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
320             vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
321             vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
322
323             tp = vlib_buffer_get_current (b0);
324             ip = &tp->ip4;
325             h = &tp->ipfix.h;
326             s = &tp->ipfix.s;
327
328             /* FIXUP: message header export_time */
329             h->export_time = clib_host_to_net_u32 (((u32) time (NULL)));
330
331             /* FIXUP: message header sequence_number */
332             h->sequence_number = stream->sequence_number++;
333             h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
334             next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
335             records_this_buffer = 0;
336           }
337
338         next_offset = ioam_analyse_add_ipfix_record (fr, record,
339                                                      b0, next_offset,
340                                                      &temp, &temp, 0, 0);
341         records_this_buffer++;
342
343         /* Flush data if packet len is about to reach path mtu */
344         if (next_offset > (exp->path_mtu - 250))
345           flush = 1;
346       }
347
348     if (PREDICT_FALSE (flush && b0))
349       {
350         s->set_id_length = ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
351                                                 next_offset - (sizeof (*ip) +
352                                                                sizeof (*udp) +
353                                                                sizeof (*h)));
354         b0->current_length = next_offset;
355         b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
356         tp = vlib_buffer_get_current (b0);
357         ip = (ip4_header_t *) & tp->ip4;
358         udp = (udp_header_t *) (ip + 1);
359
360         sum0 = ip->checksum;
361         old_l0 = ip->length;
362         new_l0 = clib_host_to_net_u16 ((u16) next_offset);
363         sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
364                                length /* changed member */ );
365
366         ip->checksum = ip_csum_fold (sum0);
367         ip->length = new_l0;
368         udp->length =
369           clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
370
371         if (exp->udp_checksum)
372           {
373             /* RFC 7011 section 10.3.2. */
374             udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
375             if (udp->checksum == 0)
376               udp->checksum = 0xffff;
377           }
378
379         to_next[0] = bi0;
380         f->n_vectors++;
381         to_next++;
382
383         if (f->n_vectors == VLIB_FRAME_SIZE)
384           {
385             vlib_put_frame_to_node (vm, node_index, f);
386             f = vlib_get_frame_to_node (vm, node_index);
387             f->n_vectors = 0;
388             to_next = vlib_frame_vector_args (f);
389           }
390         b0 = 0;
391         bi0 = ~0;
392       }
393   }
394
395   return f;
396 }
397
398 clib_error_t *
399 ioam_flow_create (u8 del)
400 {
401   vnet_flow_report_add_del_args_t args;
402   int rv;
403   u32 domain_id = 0;
404   flow_report_main_t *frm = &flow_report_main;
405   u16 template_id;
406
407   clib_memset (&args, 0, sizeof (args));
408   args.rewrite_callback = ioam_template_rewrite;
409   args.flow_data_callback = ioam_send_flows;
410   del ? (args.is_add = 0) : (args.is_add = 1);
411   args.domain_id = domain_id;
412
413   rv = vnet_flow_report_add_del (frm, &args, &template_id);
414
415   switch (rv)
416     {
417     case 0:
418       break;
419     case VNET_API_ERROR_NO_SUCH_ENTRY:
420       return clib_error_return (0, "registration not found...");
421     default:
422       return clib_error_return (0, "vnet_flow_report_add_del returned %d",
423                                 rv);
424     }
425
426   return 0;
427 }
428
429 clib_error_t *
430 ioam_flow_report_init (vlib_main_t * vm)
431 {
432   return 0;
433 }
434
435 /* *INDENT-OFF* */
436 VLIB_INIT_FUNCTION (ioam_flow_report_init) =
437 {
438   .runs_after = VLIB_INITS("flow_report_init"),
439 };
440 /* *INDENT-ON* */
441
442 /*
443  * fd.io coding-style-patch-verification: ON
444  *
445  * Local Variables:
446  * eval: (c-set-style "gnu")
447  * End:
448  */