In-band OAM active probe (VPP-471)
[vpp.git] / src / plugins / ioam / analyse / ioam_analyse.h
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 #ifndef PLUGINS_IOAM_PLUGIN_IOAM_ANALYSE_IOAM_ANALYSE_H_
17 #define PLUGINS_IOAM_PLUGIN_IOAM_ANALYSE_IOAM_ANALYSE_H_
18
19 #include <vlib/vlib.h>
20 #include <vnet/vnet.h>
21 #include <vppinfra/types.h>
22 #include <ioam/lib-e2e/e2e_util.h>
23 #include <ioam/lib-trace/trace_util.h>
24
25 #define IOAM_FLOW_TEMPLATE_ID    260
26 #define IOAM_TRACE_MAX_NODES      10
27 #define IOAM_MAX_PATHS_PER_FLOW   10
28
29 typedef struct
30 {
31   u16 ingress_if;
32   u16 egress_if;
33   u32 node_id;
34   u32 state_up;
35 } ioam_path_map_t;
36
37 /** @brief Analysed iOAM trace data.
38     @note cache aligned.
39 */
40 typedef struct
41 {
42   /** No of nodes in path. */
43   u8 num_nodes;
44
45   /** Data contained in trace - NodeId, TTL, Ingress & Egress Link, Timestamp. */
46   u8 trace_type;
47
48   /** Flag to indicate whether node is allocated. */
49   u8 is_free;
50
51   u8 pad[5];
52
53   /** Actual PATH flow has taken. */
54   ioam_path_map_t path[IOAM_TRACE_MAX_NODES];
55
56   /** Num of pkts in the flow going over path. */
57   u32 pkt_counter;
58
59   /** Num of bytes in the flow going over path. */
60   u32 bytes_counter;
61
62   /** Minumum Dealay for the flow. */
63   u32 min_delay;
64
65   /** Maximum Dealay for the flow. */
66   u32 max_delay;
67
68   /** Average Dealay for the flow. */
69   u32 mean_delay;
70
71   u32 reserve;
72 } ioam_analyse_trace_record;
73
74 typedef struct
75 {
76   ioam_analyse_trace_record path_data[IOAM_MAX_PATHS_PER_FLOW];
77 } ioam_analyse_trace_data;
78
79 /** @brief Analysed iOAM pot data.
80     @note cache aligned.
81 */
82 typedef struct
83 {
84   /** Number of packets validated (passes through the service chain)
85       within the timestamps. */
86   u32 sfc_validated_count;
87
88   /** Number of packets invalidated (failed through the service chain)
89       within the timestamps. */
90   u32 sfc_invalidated_count;
91 } ioam_analyse_pot_data;
92
93 /** @brief Analysed iOAM data.
94     @note cache aligned.
95 */
96 typedef struct ioam_analyser_data_t_
97 {
98   u8 is_free;
99   u8 pad[3];
100
101   /** Num of pkts sent for this flow. */
102   u32 pkt_sent;
103
104   /** Num of pkts matching this flow. */
105   u32 pkt_counter;
106
107   /** Num of bytes matching this flow. */
108   u32 bytes_counter;
109
110   /** Analysed iOAM trace data. */
111   ioam_analyse_trace_data trace_data;
112
113   /** Analysed iOAM pot data. */
114   ioam_analyse_pot_data pot_data;
115
116   /** Analysed iOAM seqno data. */
117   seqno_rx_info seqno_data;
118
119   /** Cache of previously analysed data, useful for export. */
120   struct ioam_analyser_data_t_ *chached_data_list;
121
122   /** Lock to since we use this to export the data in other thread. */
123   volatile u32 *writer_lock;
124 } ioam_analyser_data_t;
125
126 always_inline f64
127 ip6_ioam_analyse_calc_delay (ioam_trace_hdr_t * trace, u16 trace_len,
128                              u8 oneway)
129 {
130   u16 size_of_traceopt_per_node, size_of_all_traceopts;
131   u8 num_nodes;
132   u32 *start_elt, *end_elt, *uturn_elt;;
133   u32 start_time, end_time;
134   u8 done = 0;
135
136   size_of_traceopt_per_node = fetch_trace_data_size (trace->ioam_trace_type);
137   // Unknown trace type
138   if (size_of_traceopt_per_node == 0)
139     return 0;
140   size_of_all_traceopts = trace_len;    /*ioam_trace_type,data_list_elts_left */
141
142   num_nodes = (u8) (size_of_all_traceopts / size_of_traceopt_per_node);
143
144   num_nodes -= trace->data_list_elts_left;
145
146   start_elt = trace->elts;
147   end_elt =
148     trace->elts +
149     (u32) (size_of_traceopt_per_node * (num_nodes - 1) / sizeof (u32));
150
151   if (oneway && (trace->ioam_trace_type & BIT_TTL_NODEID))
152     {
153       done = 0;
154       do
155         {
156           uturn_elt = start_elt - size_of_traceopt_per_node / sizeof (u32);
157
158           if ((clib_net_to_host_u32 (*start_elt) >> 24) <=
159               (clib_net_to_host_u32 (*uturn_elt) >> 24))
160             done = 1;
161         }
162       while (!done && (start_elt = uturn_elt) != end_elt);
163     }
164   if (trace->ioam_trace_type & BIT_TTL_NODEID)
165     {
166       start_elt++;
167       end_elt++;
168     }
169   if (trace->ioam_trace_type & BIT_ING_INTERFACE)
170     {
171       start_elt++;
172       end_elt++;
173     }
174   start_time = clib_net_to_host_u32 (*start_elt);
175   end_time = clib_net_to_host_u32 (*end_elt);
176
177   return (f64) (end_time - start_time);
178 }
179
180 always_inline void
181 ip6_ioam_analyse_set_paths_down (ioam_analyser_data_t * data)
182 {
183   ioam_analyse_trace_data *trace_data;
184   ioam_analyse_trace_record *trace_record;
185   ioam_path_map_t *path;
186   u8 k, i;
187
188   while (__sync_lock_test_and_set (data->writer_lock, 1))
189     ;
190
191   trace_data = &data->trace_data;
192
193   for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
194     {
195       trace_record = trace_data->path_data + i;
196
197       if (trace_record->is_free)
198         continue;
199
200       path = trace_record->path;
201
202       for (k = 0; k < trace_record->num_nodes; k++)
203         path[k].state_up = 0;
204     }
205   *(data->writer_lock) = 0;
206 }
207
208 always_inline void
209 ip6_ioam_analyse_hbh_trace_loopback (ioam_analyser_data_t * data,
210                                      ioam_trace_hdr_t * trace, u16 trace_len)
211 {
212   ioam_analyse_trace_data *trace_data;
213   ioam_analyse_trace_record *trace_record;
214   ioam_path_map_t *path;
215   u8 i, j, k, num_nodes, max_nodes;
216   u8 *ptr;
217   u32 nodeid;
218   u16 ingress_if, egress_if;
219   u16 size_of_traceopt_per_node;
220   u16 size_of_all_traceopts;
221
222   while (__sync_lock_test_and_set (data->writer_lock, 1))
223     ;
224
225   trace_data = &data->trace_data;
226
227   size_of_traceopt_per_node = fetch_trace_data_size (trace->ioam_trace_type);
228   size_of_all_traceopts = trace_len;
229
230   ptr = (u8 *) trace->elts;
231   max_nodes = (u8) (size_of_all_traceopts / size_of_traceopt_per_node);
232   num_nodes = max_nodes - trace->data_list_elts_left;
233
234   for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
235     {
236       trace_record = trace_data->path_data + i;
237       path = trace_record->path;
238
239       if (trace_record->is_free)
240         continue;
241
242       for (j = max_nodes, k = 0; k < num_nodes; j--, k++)
243         {
244           ptr =
245             (u8 *) ((u8 *) trace->elts +
246                     (size_of_traceopt_per_node * (j - 1)));
247
248           nodeid = clib_net_to_host_u32 (*((u32 *) ptr)) & 0x00ffffff;
249           ptr += 4;
250
251           if (nodeid != path[k].node_id)
252             goto end;
253
254           if ((trace->ioam_trace_type == TRACE_TYPE_IF_TS_APP) ||
255               (trace->ioam_trace_type == TRACE_TYPE_IF))
256             {
257               ingress_if = clib_net_to_host_u16 (*((u16 *) ptr));
258               ptr += 2;
259               egress_if = clib_net_to_host_u16 (*((u16 *) ptr));
260               if ((ingress_if != path[k].ingress_if) ||
261                   (egress_if != path[k].egress_if))
262                 {
263                   goto end;
264                 }
265             }
266           /* Found Match - set path hop state to up */
267           path[k].state_up = 1;
268         }
269     }
270 end:
271   *(data->writer_lock) = 0;
272 }
273
274 always_inline int
275 ip6_ioam_analyse_hbh_trace (ioam_analyser_data_t * data,
276                             ioam_trace_hdr_t * trace, u16 pak_len,
277                             u16 trace_len)
278 {
279   ioam_analyse_trace_data *trace_data;
280   u16 size_of_traceopt_per_node;
281   u16 size_of_all_traceopts;
282   u8 i, j, k, num_nodes, max_nodes;
283   u8 *ptr;
284   u32 nodeid;
285   u16 ingress_if, egress_if;
286   ioam_path_map_t *path = NULL;
287   ioam_analyse_trace_record *trace_record;
288
289   while (__sync_lock_test_and_set (data->writer_lock, 1))
290     ;
291
292   trace_data = &data->trace_data;
293
294   size_of_traceopt_per_node = fetch_trace_data_size (trace->ioam_trace_type);
295   // Unknown trace type
296   if (size_of_traceopt_per_node == 0)
297     goto DONE;
298   size_of_all_traceopts = trace_len;
299
300   ptr = (u8 *) trace->elts;
301   max_nodes = (u8) (size_of_all_traceopts / size_of_traceopt_per_node);
302   num_nodes = max_nodes - trace->data_list_elts_left;
303
304   for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
305     {
306       trace_record = trace_data->path_data + i;
307
308       if (trace_record->is_free ||
309           (num_nodes != trace_record->num_nodes) ||
310           (trace->ioam_trace_type != trace_record->trace_type))
311         continue;
312
313       path = trace_record->path;
314
315       for (j = max_nodes, k = 0; k < num_nodes; j--, k++)
316         {
317           ptr =
318             (u8 *) ((u8 *) trace->elts +
319                     (size_of_traceopt_per_node * (j - 1)));
320
321           nodeid = clib_net_to_host_u32 (*((u32 *) ptr)) & 0x00ffffff;
322           ptr += 4;
323
324           if (nodeid != path[k].node_id)
325             break;
326
327           if ((trace->ioam_trace_type == TRACE_TYPE_IF_TS_APP) ||
328               (trace->ioam_trace_type == TRACE_TYPE_IF))
329             {
330               ingress_if = clib_net_to_host_u16 (*((u16 *) ptr));
331               ptr += 2;
332               egress_if = clib_net_to_host_u16 (*((u16 *) ptr));
333               if ((ingress_if != path[k].ingress_if) ||
334                   (egress_if != path[k].egress_if))
335                 {
336                   break;
337                 }
338             }
339         }
340
341       if (k == num_nodes)
342         {
343           goto found_match;
344         }
345     }
346
347   for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
348     {
349       trace_record = trace_data->path_data + i;
350       if (trace_record->is_free)
351         {
352           trace_record->is_free = 0;
353           trace_record->num_nodes = num_nodes;
354           trace_record->trace_type = trace->ioam_trace_type;
355           path = trace_data->path_data[i].path;
356           trace_record->pkt_counter = 0;
357           trace_record->bytes_counter = 0;
358           trace_record->min_delay = 0xFFFFFFFF;
359           trace_record->max_delay = 0;
360           trace_record->mean_delay = 0;
361           break;
362         }
363     }
364
365   for (j = max_nodes, k = 0; k < num_nodes; j--, k++)
366     {
367       ptr =
368         (u8 *) ((u8 *) trace->elts + (size_of_traceopt_per_node * (j - 1)));
369
370       path[k].node_id = clib_net_to_host_u32 (*((u32 *) ptr)) & 0x00ffffff;
371       ptr += 4;
372
373       if ((trace->ioam_trace_type == TRACE_TYPE_IF_TS_APP) ||
374           (trace->ioam_trace_type == TRACE_TYPE_IF))
375         {
376           path[k].ingress_if = clib_net_to_host_u16 (*((u16 *) ptr));
377           ptr += 2;
378           path[k].egress_if = clib_net_to_host_u16 (*((u16 *) ptr));
379         }
380     }
381
382 found_match:
383   /* Set path state to UP */
384   for (k = 0; k < num_nodes; k++)
385     path[k].state_up = 1;
386
387   trace_record->pkt_counter++;
388   trace_record->bytes_counter += pak_len;
389   if (trace->ioam_trace_type & BIT_TIMESTAMP)
390     {
391       /* Calculate time delay */
392       u32 delay = (u32) ip6_ioam_analyse_calc_delay (trace, trace_len, 0);
393       if (delay < trace_record->min_delay)
394         trace_record->min_delay = delay;
395       else if (delay > trace_record->max_delay)
396         trace_record->max_delay = delay;
397
398       u64 sum = (trace_record->mean_delay * data->seqno_data.rx_packets);
399       trace_record->mean_delay =
400         (u32) ((sum + delay) / (data->seqno_data.rx_packets + 1));
401     }
402 DONE:
403   *(data->writer_lock) = 0;
404   return 0;
405 }
406
407 always_inline int
408 ip6_ioam_analyse_hbh_e2e (ioam_analyser_data_t * data,
409                           ioam_e2e_packet_t * e2e, u16 len)
410 {
411   while (__sync_lock_test_and_set (data->writer_lock, 1))
412     ;
413
414   ioam_analyze_seqno (&data->seqno_data,
415                       (u64) clib_net_to_host_u32 (e2e->e2e_data));
416
417   *(data->writer_lock) = 0;
418   return 0;
419 }
420
421 always_inline u8 *
422 format_path_map (u8 * s, va_list * args)
423 {
424   ioam_path_map_t *pm = va_arg (*args, ioam_path_map_t *);
425   u32 num_of_elts = va_arg (*args, u32);
426   u32 i;
427
428   for (i = 0; i < num_of_elts; i++)
429     {
430       s =
431         format (s,
432                 "node_id: 0x%x, ingress_if: 0x%x, egress_if:0x%x, state:%s\n",
433                 pm->node_id, pm->ingress_if, pm->egress_if,
434                 pm->state_up ? "UP" : "DOWN");
435       pm++;
436     }
437
438   return (s);
439 }
440
441 always_inline u8 *
442 print_analyse_flow (u8 * s, ioam_analyser_data_t * record)
443 {
444   int j;
445   ioam_analyse_trace_record *trace_record;
446
447   s = format (s, "pkt_sent : %u\n", record->pkt_sent);
448   s = format (s, "pkt_counter : %u\n", record->pkt_counter);
449   s = format (s, "bytes_counter : %u\n", record->bytes_counter);
450
451   s = format (s, "Trace data: \n");
452
453   for (j = 0; j < IOAM_MAX_PATHS_PER_FLOW; j++)
454     {
455       trace_record = record->trace_data.path_data + j;
456       if (trace_record->is_free)
457         continue;
458
459       s = format (s, "path_map:\n%U", format_path_map,
460                   trace_record->path, trace_record->num_nodes);
461       s = format (s, "pkt_counter: %u\n", trace_record->pkt_counter);
462       s = format (s, "bytes_counter: %u\n", trace_record->bytes_counter);
463
464       s = format (s, "min_delay: %u\n", trace_record->min_delay);
465       s = format (s, "max_delay: %u\n", trace_record->max_delay);
466       s = format (s, "mean_delay: %u\n", trace_record->mean_delay);
467     }
468
469   s = format (s, "\nPOT data: \n");
470   s = format (s, "sfc_validated_count : %u\n",
471               record->pot_data.sfc_validated_count);
472   s = format (s, "sfc_invalidated_count : %u\n",
473               record->pot_data.sfc_invalidated_count);
474
475   s = format (s, "\nSeqno Data:\n");
476   s = format (s,
477               "RX Packets        : %lu\n"
478               "Lost Packets      : %lu\n"
479               "Duplicate Packets : %lu\n"
480               "Reordered Packets : %lu\n",
481               record->seqno_data.rx_packets,
482               record->seqno_data.lost_packets,
483               record->seqno_data.dup_packets,
484               record->seqno_data.reordered_packets);
485
486   s = format (s, "\n");
487   return s;
488 }
489
490 always_inline void
491 ioam_analyse_init_data (ioam_analyser_data_t * data)
492 {
493   u16 j;
494   ioam_analyse_trace_data *trace_data;
495
496   data->is_free = 1;
497
498   /* We maintain data corresponding to last IP-Fix export, this may
499    * get extended in future to maintain history of data */
500   vec_validate_aligned (data->chached_data_list, 0, CLIB_CACHE_LINE_BYTES);
501
502   data->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
503                                               CLIB_CACHE_LINE_BYTES);
504   *(data->writer_lock) = 0;
505
506   trace_data = &(data->trace_data);
507   for (j = 0; j < IOAM_MAX_PATHS_PER_FLOW; j++)
508     trace_data->path_data[j].is_free = 1;
509 }
510
511 #endif /* PLUGINS_IOAM_PLUGIN_IOAM_ANALYSE_IOAM_ANALYSE_H_ */
512
513 /*
514  * fd.io coding-style-patch-verification: ON
515  *
516  * Local Variables:
517  * eval: (c-set-style "gnu")
518  * End:
519  */