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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef PLUGINS_IOAM_PLUGIN_IOAM_ANALYSE_IOAM_ANALYSE_H_
17 #define PLUGINS_IOAM_PLUGIN_IOAM_ANALYSE_IOAM_ANALYSE_H_
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 #include <ioam/lib-trace/trace_config.h>
25 #include <vppinfra/lock.h>
27 #define IOAM_FLOW_TEMPLATE_ID 260
28 #define IOAM_TRACE_MAX_NODES 10
29 #define IOAM_MAX_PATHS_PER_FLOW 10
39 /** @brief Analysed iOAM trace data.
44 /** No of nodes in path. */
47 /** Data contained in trace - NodeId, TTL, Ingress & Egress Link, Timestamp. */
50 /** Flag to indicate whether node is allocated. */
55 /** Actual PATH flow has taken. */
56 ioam_path_map_t path[IOAM_TRACE_MAX_NODES];
58 /** Num of pkts in the flow going over path. */
61 /** Num of bytes in the flow going over path. */
64 /** Minumum Dealay for the flow. */
67 /** Maximum Dealay for the flow. */
70 /** Average Dealay for the flow. */
74 } ioam_analyse_trace_record;
78 ioam_analyse_trace_record path_data[IOAM_MAX_PATHS_PER_FLOW];
79 } ioam_analyse_trace_data;
81 /** @brief Analysed iOAM pot data.
86 /** Number of packets validated (passes through the service chain)
87 within the timestamps. */
88 u32 sfc_validated_count;
90 /** Number of packets invalidated (failed through the service chain)
91 within the timestamps. */
92 u32 sfc_invalidated_count;
93 } ioam_analyse_pot_data;
95 /** @brief Analysed iOAM data.
98 typedef struct ioam_analyser_data_t_
100 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
105 /** Num of pkts sent for this flow. */
108 /** Num of pkts matching this flow. */
111 /** Num of bytes matching this flow. */
114 /** Analysed iOAM trace data. */
115 ioam_analyse_trace_data trace_data;
117 /** Analysed iOAM pot data. */
118 ioam_analyse_pot_data pot_data;
120 /** Analysed iOAM seqno data. */
121 seqno_rx_info seqno_data;
123 /** Cache of previously analysed data, useful for export. */
124 struct ioam_analyser_data_t_ *chached_data_list;
126 /** Lock to since we use this to export the data in other thread. */
127 clib_spinlock_t writer_lock;
128 } ioam_analyser_data_t;
131 ip6_ioam_analyse_calc_delay (ioam_trace_hdr_t * trace, u16 trace_len,
134 u16 size_of_all_traceopts;
135 u8 size_of_traceopt_per_node;
137 u32 *start_elt, *end_elt, *uturn_elt;;
138 u32 start_time, end_time;
141 size_of_traceopt_per_node = fetch_trace_data_size (trace->ioam_trace_type);
142 // Unknown trace type
143 if (size_of_traceopt_per_node == 0)
145 size_of_all_traceopts = trace_len; /*ioam_trace_type,data_list_elts_left */
147 num_nodes = (u8) (size_of_all_traceopts / size_of_traceopt_per_node);
148 if ((num_nodes == 0) || (num_nodes <= trace->data_list_elts_left))
151 num_nodes -= trace->data_list_elts_left;
153 start_elt = trace->elts;
156 (u32) ((size_of_traceopt_per_node / sizeof (u32)) * (num_nodes - 1));
158 if (oneway && (trace->ioam_trace_type & BIT_TTL_NODEID))
163 uturn_elt = start_elt - size_of_traceopt_per_node / sizeof (u32);
165 if ((clib_net_to_host_u32 (*start_elt) >> 24) <=
166 (clib_net_to_host_u32 (*uturn_elt) >> 24))
169 while (!done && (start_elt = uturn_elt) != end_elt);
171 if (trace->ioam_trace_type & BIT_TTL_NODEID)
176 if (trace->ioam_trace_type & BIT_ING_INTERFACE)
181 start_time = clib_net_to_host_u32 (*start_elt);
182 end_time = clib_net_to_host_u32 (*end_elt);
184 return (f64) (end_time - start_time);
188 ip6_ioam_analyse_set_paths_down (ioam_analyser_data_t * data)
190 ioam_analyse_trace_data *trace_data;
191 ioam_analyse_trace_record *trace_record;
192 ioam_path_map_t *path;
195 clib_spinlock_lock (&data->writer_lock);
197 trace_data = &data->trace_data;
199 for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
201 trace_record = trace_data->path_data + i;
203 if (trace_record->is_free)
206 path = trace_record->path;
208 for (k = 0; k < trace_record->num_nodes; k++)
209 path[k].state_up = 0;
211 clib_spinlock_unlock (&data->writer_lock);
215 ip6_ioam_analyse_hbh_trace_loopback (ioam_analyser_data_t * data,
216 ioam_trace_hdr_t * trace, u16 trace_len)
218 ioam_analyse_trace_data *trace_data;
219 ioam_analyse_trace_record *trace_record;
220 ioam_path_map_t *path;
221 u8 i, j, k, num_nodes, max_nodes;
224 u16 ingress_if, egress_if;
225 u16 size_of_traceopt_per_node;
226 u16 size_of_all_traceopts;
228 clib_spinlock_lock (&data->writer_lock);
230 trace_data = &data->trace_data;
232 size_of_traceopt_per_node = fetch_trace_data_size (trace->ioam_trace_type);
233 if (0 == size_of_traceopt_per_node)
236 size_of_all_traceopts = trace_len;
238 ptr = (u8 *) trace->elts;
239 max_nodes = (u8) (size_of_all_traceopts / size_of_traceopt_per_node);
240 num_nodes = max_nodes - trace->data_list_elts_left;
242 for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
244 trace_record = trace_data->path_data + i;
245 path = trace_record->path;
247 if (trace_record->is_free)
250 for (j = max_nodes, k = 0; k < num_nodes; j--, k++)
253 (u8 *) ((u8 *) trace->elts +
254 (size_of_traceopt_per_node * (j - 1)));
256 nodeid = clib_net_to_host_u32 (*((u32 *) ptr)) & 0x00ffffff;
259 if (nodeid != path[k].node_id)
262 if ((trace->ioam_trace_type == TRACE_TYPE_IF_TS_APP) ||
263 (trace->ioam_trace_type == TRACE_TYPE_IF))
265 ingress_if = clib_net_to_host_u16 (*((u16 *) ptr));
267 egress_if = clib_net_to_host_u16 (*((u16 *) ptr));
268 if ((ingress_if != path[k].ingress_if) ||
269 (egress_if != path[k].egress_if))
274 /* Found Match - set path hop state to up */
275 path[k].state_up = 1;
279 clib_spinlock_unlock (&data->writer_lock);
283 ip6_ioam_analyse_hbh_trace (ioam_analyser_data_t * data,
284 ioam_trace_hdr_t * trace, u16 pak_len,
287 ioam_analyse_trace_data *trace_data;
288 u16 size_of_traceopt_per_node;
289 u16 size_of_all_traceopts;
290 u8 i, j, k, num_nodes, max_nodes;
293 u16 ingress_if, egress_if;
294 ioam_path_map_t *path = NULL;
295 ioam_analyse_trace_record *trace_record;
297 clib_spinlock_lock (&data->writer_lock);
299 trace_data = &data->trace_data;
301 size_of_traceopt_per_node = fetch_trace_data_size (trace->ioam_trace_type);
302 // Unknown trace type
303 if (size_of_traceopt_per_node == 0)
305 size_of_all_traceopts = trace_len;
307 ptr = (u8 *) trace->elts;
308 max_nodes = (u8) (size_of_all_traceopts / size_of_traceopt_per_node);
309 num_nodes = max_nodes - trace->data_list_elts_left;
311 for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
313 trace_record = trace_data->path_data + i;
315 if (trace_record->is_free ||
316 (num_nodes != trace_record->num_nodes) ||
317 (trace->ioam_trace_type != trace_record->trace_type))
320 path = trace_record->path;
322 for (j = max_nodes, k = 0; k < num_nodes; j--, k++)
325 (u8 *) ((u8 *) trace->elts +
326 (size_of_traceopt_per_node * (j - 1)));
328 nodeid = clib_net_to_host_u32 (*((u32 *) ptr)) & 0x00ffffff;
331 if (nodeid != path[k].node_id)
334 if ((trace->ioam_trace_type == TRACE_TYPE_IF_TS_APP) ||
335 (trace->ioam_trace_type == TRACE_TYPE_IF))
337 ingress_if = clib_net_to_host_u16 (*((u16 *) ptr));
339 egress_if = clib_net_to_host_u16 (*((u16 *) ptr));
340 if ((ingress_if != path[k].ingress_if) ||
341 (egress_if != path[k].egress_if))
354 for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
356 trace_record = trace_data->path_data + i;
357 if (trace_record->is_free)
359 trace_record->is_free = 0;
360 trace_record->num_nodes = num_nodes;
361 trace_record->trace_type = trace->ioam_trace_type;
362 path = trace_data->path_data[i].path;
363 trace_record->pkt_counter = 0;
364 trace_record->bytes_counter = 0;
365 trace_record->min_delay = 0xFFFFFFFF;
366 trace_record->max_delay = 0;
367 trace_record->mean_delay = 0;
372 for (j = max_nodes, k = 0; k < num_nodes; j--, k++)
375 (u8 *) ((u8 *) trace->elts + (size_of_traceopt_per_node * (j - 1)));
377 path[k].node_id = clib_net_to_host_u32 (*((u32 *) ptr)) & 0x00ffffff;
380 if ((trace->ioam_trace_type == TRACE_TYPE_IF_TS_APP) ||
381 (trace->ioam_trace_type == TRACE_TYPE_IF))
383 path[k].ingress_if = clib_net_to_host_u16 (*((u16 *) ptr));
385 path[k].egress_if = clib_net_to_host_u16 (*((u16 *) ptr));
390 /* Set path state to UP */
391 for (k = 0; k < num_nodes; k++)
392 path[k].state_up = 1;
394 trace_record->pkt_counter++;
395 trace_record->bytes_counter += pak_len;
396 if (trace->ioam_trace_type & BIT_TIMESTAMP)
398 /* Calculate time delay */
399 u32 delay = (u32) ip6_ioam_analyse_calc_delay (trace, trace_len, 0);
400 if (delay < trace_record->min_delay)
401 trace_record->min_delay = delay;
402 else if (delay > trace_record->max_delay)
403 trace_record->max_delay = delay;
405 u64 sum = (trace_record->mean_delay * data->seqno_data.rx_packets);
406 trace_record->mean_delay =
407 (u32) ((sum + delay) / (data->seqno_data.rx_packets + 1));
410 clib_spinlock_unlock (&data->writer_lock);
415 ip6_ioam_analyse_hbh_e2e (ioam_analyser_data_t * data,
416 ioam_e2e_packet_t * e2e, u16 len)
418 clib_spinlock_lock (&data->writer_lock);
420 ioam_analyze_seqno (&data->seqno_data,
421 (u64) clib_net_to_host_u32 (e2e->e2e_data));
423 clib_spinlock_unlock (&data->writer_lock);
429 format_path_map (u8 * s, va_list * args)
431 ioam_path_map_t *pm = va_arg (*args, ioam_path_map_t *);
432 u32 num_of_elts = va_arg (*args, u32);
435 for (i = 0; i < num_of_elts; i++)
439 "node_id: 0x%x, ingress_if: 0x%x, egress_if:0x%x, state:%s\n",
440 pm->node_id, pm->ingress_if, pm->egress_if,
441 pm->state_up ? "UP" : "DOWN");
449 print_analyse_flow (u8 * s, ioam_analyser_data_t * record)
452 ioam_analyse_trace_record *trace_record;
454 s = format (s, "pkt_sent : %u\n", record->pkt_sent);
455 s = format (s, "pkt_counter : %u\n", record->pkt_counter);
456 s = format (s, "bytes_counter : %u\n", record->bytes_counter);
458 s = format (s, "Trace data: \n");
460 for (j = 0; j < IOAM_MAX_PATHS_PER_FLOW; j++)
462 trace_record = record->trace_data.path_data + j;
463 if (trace_record->is_free)
466 s = format (s, "path_map:\n%U", format_path_map,
467 trace_record->path, trace_record->num_nodes);
468 s = format (s, "pkt_counter: %u\n", trace_record->pkt_counter);
469 s = format (s, "bytes_counter: %u\n", trace_record->bytes_counter);
471 s = format (s, "min_delay: %u\n", trace_record->min_delay);
472 s = format (s, "max_delay: %u\n", trace_record->max_delay);
473 s = format (s, "mean_delay: %u\n", trace_record->mean_delay);
476 s = format (s, "\nPOT data: \n");
477 s = format (s, "sfc_validated_count : %u\n",
478 record->pot_data.sfc_validated_count);
479 s = format (s, "sfc_invalidated_count : %u\n",
480 record->pot_data.sfc_invalidated_count);
482 s = format (s, "\nSeqno Data:\n");
485 "Lost Packets : %lu\n"
486 "Duplicate Packets : %lu\n"
487 "Reordered Packets : %lu\n",
488 record->seqno_data.rx_packets,
489 record->seqno_data.lost_packets,
490 record->seqno_data.dup_packets,
491 record->seqno_data.reordered_packets);
493 s = format (s, "\n");
498 ioam_analyse_init_data (ioam_analyser_data_t * data)
501 ioam_analyse_trace_data *trace_data;
505 /* We maintain data corresponding to last IP-Fix export, this may
506 * get extended in future to maintain history of data */
507 vec_validate_aligned (data->chached_data_list, 0, CLIB_CACHE_LINE_BYTES);
509 clib_spinlock_init (&data->writer_lock);
511 trace_data = &(data->trace_data);
512 for (j = 0; j < IOAM_MAX_PATHS_PER_FLOW; j++)
513 trace_data->path_data[j].is_free = 1;
516 #endif /* PLUGINS_IOAM_PLUGIN_IOAM_ANALYSE_IOAM_ANALYSE_H_ */
519 * fd.io coding-style-patch-verification: ON
522 * eval: (c-set-style "gnu")