nsh_md2_ioam_trace.c revision f126e1c3
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#include <vlib/vlib.h>
16#include <vnet/vnet.h>
17#include <vnet/pg/pg.h>
18#include <vppinfra/error.h>
19
20#include <vppinfra/hash.h>
21#include <vppinfra/error.h>
22#include <vppinfra/elog.h>
23
24#include <ioam/lib-trace/trace_util.h>
25#include <nsh-md2-ioam/nsh_md2_ioam.h>
26#include <nsh/nsh_packet.h>
27
28/* Timestamp precision multipliers for seconds, milliseconds, microseconds
29 * and nanoseconds respectively.
30 */
31static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
32#define NSH_MD2_IOAM_TRACE_SIZE_DUMMY 20
33
34typedef union
35{
36  u64 as_u64;
37  u32 as_u32[2];
38} time_u64_t;
39
40
41/* *INDENT-OFF* */
42typedef CLIB_PACKED(struct {
43  u16 class;
44  u8 type;
45  u8 length;
46  u8 data_list_elts_left;
47  u16 ioam_trace_type;
48  u8 reserve;
49  u32 elts[0]; /* Variable type. So keep it generic */
50}) nsh_md2_ioam_trace_option_t;
51/* *INDENT-ON* */
52
53
54#define foreach_nsh_md2_ioam_trace_stats				\
55  _(SUCCESS, "Pkts updated with TRACE records")					\
56  _(FAILED, "Errors in TRACE due to lack of TRACE records")
57
58static char *nsh_md2_ioam_trace_stats_strings[] = {
59#define _(sym,string) string,
60  foreach_nsh_md2_ioam_trace_stats
61#undef _
62};
63
64typedef enum
65{
66#define _(sym,str) NSH_MD2_IOAM_TRACE_##sym,
67  foreach_nsh_md2_ioam_trace_stats
68#undef _
69    NSH_MD2_IOAM_TRACE_N_STATS,
70} nsh_md2_ioam_trace_stats_t;
71
72
73typedef struct
74{
75  /* stats */
76  u64 counters[ARRAY_LEN (nsh_md2_ioam_trace_stats_strings)];
77
78  /* convenience */
79  vlib_main_t *vlib_main;
80  vnet_main_t *vnet_main;
81} nsh_md2_ioam_trace_main_t;
82
83nsh_md2_ioam_trace_main_t nsh_md2_ioam_trace_main;
84
85/*
86 * Find a trace profile
87 */
88
89extern u8 *nsh_trace_main;
90always_inline trace_profile *
91nsh_trace_profile_find (void)
92{
93  trace_main_t *sm = (trace_main_t *) nsh_trace_main;
94
95  return (&(sm->profile));
96}
97
98
99always_inline void
100nsh_md2_ioam_trace_stats_increment_counter (u32 counter_index,
101						 u64 increment)
102{
103  nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
104
105  hm->counters[counter_index] += increment;
106}
107
108
109static u8 *
110format_ioam_data_list_element (u8 * s, va_list * args)
111{
112  u32 *elt = va_arg (*args, u32 *);
113  u8 *trace_type_p = va_arg (*args, u8 *);
114  u8 trace_type = *trace_type_p;
115
116
117  if (trace_type & BIT_TTL_NODEID)
118    {
119      u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
120      s = format (s, "ttl 0x%x node id 0x%x ",
121		  ttl_node_id_host_byte_order >> 24,
122		  ttl_node_id_host_byte_order & 0x00FFFFFF);
123
124      elt++;
125    }
126
127  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
128    {
129      u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
130      s = format (s, "ingress 0x%x egress 0x%x ",
131		  ingress_host_byte_order >> 16,
132		  ingress_host_byte_order & 0xFFFF);
133      elt++;
134    }
135
136  if (trace_type & BIT_TIMESTAMP)
137    {
138      u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
139      s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
140      elt++;
141    }
142
143  if (trace_type & BIT_APPDATA)
144    {
145      u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
146      s = format (s, "app 0x%x ", appdata_in_host_byte_order);
147      elt++;
148    }
149
150  return s;
151}
152
153
154
155int
156nsh_md2_ioam_trace_rewrite_handler (u8 * rewrite_string,
157					 u8 * rewrite_size)
158{
159  nsh_md2_ioam_trace_option_t *trace_option = NULL;
160  u8 trace_data_size = 0;
161  u8 trace_option_elts = 0;
162  trace_profile *profile = NULL;
163
164  profile = nsh_trace_profile_find ();
165
166  if (PREDICT_FALSE (!profile))
167    {
168      return (-1);
169    }
170
171  if (PREDICT_FALSE (!rewrite_string))
172    return -1;
173
174  trace_option_elts = profile->num_elts;
175  trace_data_size = fetch_trace_data_size (profile->trace_type);
176
177  trace_option = (nsh_md2_ioam_trace_option_t *) rewrite_string;
178  trace_option->class = clib_host_to_net_u16(0x9);
179  trace_option->type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
180  trace_option->length = (trace_option_elts * trace_data_size) + 4;
181  trace_option->data_list_elts_left = trace_option_elts;
182  trace_option->ioam_trace_type =
183        clib_host_to_net_u16(profile->trace_type & TRACE_TYPE_MASK);
184
185  *rewrite_size =
186    sizeof (nsh_md2_ioam_trace_option_t) +
187    (trace_option_elts * trace_data_size);
188
189  return 0;
190}
191
192
193int
194nsh_md2_ioam_trace_data_list_handler (vlib_buffer_t * b,
195					   nsh_tlv_header_t * opt)
196{
197  u8 elt_index = 0;
198  nsh_md2_ioam_trace_option_t *trace =
199    (nsh_md2_ioam_trace_option_t *) ((u8 *)opt);
200  time_u64_t time_u64;
201  u32 *elt;
202  int rv = 0;
203  trace_profile *profile = NULL;
204  nsh_md2_ioam_main_t *hm = &nsh_md2_ioam_main;
205  nsh_main_t *gm = &nsh_main;
206  u16 ioam_trace_type = 0;
207
208  profile = nsh_trace_profile_find ();
209
210  if (PREDICT_FALSE (!profile))
211    {
212      return (-1);
213    }
214
215
216  ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
217  time_u64.as_u64 = 0;
218
219  if (PREDICT_TRUE (trace->data_list_elts_left))
220    {
221      trace->data_list_elts_left--;
222      /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
223       * to skip to this node's location.
224       */
225      elt_index =
226	trace->data_list_elts_left *
227	fetch_trace_data_size (ioam_trace_type) / 4;
228      elt = &trace->elts[elt_index];
229	  if (ioam_trace_type & BIT_TTL_NODEID)
230	    {
231	      ip4_header_t *ip0 = vlib_buffer_get_current (b);
232	      *elt = clib_host_to_net_u32 (((ip0->ttl - 1) << 24) |
233					   profile->node_id);
234	      elt++;
235	    }
236
237	  if (ioam_trace_type & BIT_ING_INTERFACE)
238	    {
239	      u16 tx_if = vnet_buffer(b)->sw_if_index[VLIB_TX];
240
241	      *elt =
242		(vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
243		tx_if;
244	      *elt = clib_host_to_net_u32 (*elt);
245	      elt++;
246	    }
247
248
249      if (ioam_trace_type & BIT_TIMESTAMP)
250	{
251	  /* Send least significant 32 bits */
252	  f64 time_f64 =
253	    (f64) (((f64) hm->unix_time_0) +
254		   (vlib_time_now (gm->vlib_main) - hm->vlib_time_0));
255
256	  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
257	  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
258	  elt++;
259	}
260
261      if (ioam_trace_type & BIT_APPDATA)
262	{
263	  /* $$$ set elt0->app_data */
264	  *elt = clib_host_to_net_u32 (profile->app_data);
265	  elt++;
266	}
267      nsh_md2_ioam_trace_stats_increment_counter
268	(NSH_MD2_IOAM_TRACE_SUCCESS, 1);
269    }
270  else
271    {
272      nsh_md2_ioam_trace_stats_increment_counter
273	(NSH_MD2_IOAM_TRACE_FAILED, 1);
274    }
275  return (rv);
276}
277
278
279
280u8 *
281nsh_md2_ioam_trace_data_list_trace_handler (u8 * s,
282					    nsh_tlv_header_t * opt)
283{
284  nsh_md2_ioam_trace_option_t *trace;
285  u8 trace_data_size_in_words = 0;
286  u32 *elt;
287  int elt_index = 0;
288  u16 ioam_trace_type = 0;
289
290  trace = (nsh_md2_ioam_trace_option_t *) ((u8 *)opt);
291  ioam_trace_type = clib_net_to_host_u16(trace->ioam_trace_type);
292  trace_data_size_in_words =
293    fetch_trace_data_size (ioam_trace_type) / 4;
294  elt = &trace->elts[0];
295  s =
296    format (s, "  Trace Type 0x%x , %d elts left\n", ioam_trace_type,
297	    trace->data_list_elts_left);
298  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->length  - 4
299		       /* -2 accounts for ioam_trace_type,elts_left */ ))
300    {
301      s = format (s, "    [%d] %U\n", elt_index,
302		  format_ioam_data_list_element,
303		  elt, &ioam_trace_type);
304      elt_index++;
305      elt += trace_data_size_in_words;
306    }
307  return (s);
308}
309
310int
311nsh_md2_ioam_trace_swap_handler (vlib_buffer_t * b,
312				 nsh_tlv_header_t * old_opt,
313				 nsh_tlv_header_t * new_opt)
314{
315
316  clib_memcpy(new_opt, old_opt, new_opt->length + sizeof(nsh_tlv_header_t));
317  return nsh_md2_ioam_trace_data_list_handler (b, new_opt);
318}
319
320static clib_error_t *
321nsh_md2_ioam_show_ioam_trace_cmd_fn (vlib_main_t * vm,
322					  unformat_input_t * input,
323					  vlib_cli_command_t * cmd)
324{
325  nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
326  u8 *s = 0;
327  int i = 0;
328
329  for (i = 0; i < NSH_MD2_IOAM_TRACE_N_STATS; i++)
330    {
331      s = format (s, " %s - %lu\n", nsh_md2_ioam_trace_stats_strings[i],
332		  hm->counters[i]);
333    }
334
335  vlib_cli_output (vm, "%v", s);
336  vec_free (s);
337  return 0;
338}
339
340
341/* *INDENT-OFF* */
342VLIB_CLI_COMMAND (nsh_md2_ioam_show_ioam_trace_cmd, static) = {
343  .path = "show ioam nsh-lisp-gpe trace",
344  .short_help = "iOAM trace statistics",
345  .function = nsh_md2_ioam_show_ioam_trace_cmd_fn,
346};
347/* *INDENT-ON* */
348
349
350int
351nsh_md2_ioam_trace_pop_handler (vlib_buffer_t * b,
352		     nsh_tlv_header_t * opt)
353{
354  return nsh_md2_ioam_trace_data_list_handler (b, opt);
355}
356
357static clib_error_t *
358nsh_md2_ioam_trace_init (vlib_main_t * vm)
359{
360  nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
361  nsh_md2_ioam_main_t *gm = &nsh_md2_ioam_main;
362  clib_error_t *error;
363
364  if ((error = vlib_call_init_function (vm, nsh_init)))
365    return (error);
366
367  if ((error = vlib_call_init_function (vm, nsh_md2_ioam_init)))
368    return (error);
369
370  hm->vlib_main = vm;
371  hm->vnet_main = vnet_get_main ();
372  gm->unix_time_0 = (u32) time (0);     /* Store starting time */
373  gm->vlib_time_0 = vlib_time_now (vm);
374
375  memset (hm->counters, 0, sizeof (hm->counters));
376
377  if (nsh_md2_register_option
378      (clib_host_to_net_u16(0x9),
379       NSH_MD2_IOAM_OPTION_TYPE_TRACE,
380       NSH_MD2_IOAM_TRACE_SIZE_DUMMY,
381       nsh_md2_ioam_trace_rewrite_handler,
382       nsh_md2_ioam_trace_data_list_handler,
383       nsh_md2_ioam_trace_swap_handler,
384       nsh_md2_ioam_trace_pop_handler,
385       nsh_md2_ioam_trace_data_list_trace_handler) < 0)
386    return (clib_error_create
387	    ("registration of NSH_MD2_IOAM_OPTION_TYPE_TRACE failed"));
388
389  return (0);
390}
391
392VLIB_INIT_FUNCTION (nsh_md2_ioam_trace_init);
393
394int
395nsh_md2_ioam_trace_profile_cleanup (void)
396{
397  nsh_main_t *hm = &nsh_main;
398
399  hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = 0;
400
401  return 0;
402
403}
404
405static int
406nsh_md2_ioam_trace_get_sizeof_handler (u32 * result)
407{
408  u16 size = 0;
409  u8 trace_data_size = 0;
410  trace_profile *profile = NULL;
411
412  *result = 0;
413
414  profile = nsh_trace_profile_find ();
415
416  if (PREDICT_FALSE (!profile))
417    {
418      return (-1);
419    }
420
421  trace_data_size = fetch_trace_data_size (profile->trace_type);
422  if (PREDICT_FALSE (trace_data_size == 0))
423    return VNET_API_ERROR_INVALID_VALUE;
424
425  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
426    return VNET_API_ERROR_INVALID_VALUE;
427
428  size +=
429    sizeof (nsh_md2_ioam_trace_option_t) +
430    profile->num_elts * trace_data_size;
431  *result = size;
432
433  return 0;
434}
435
436
437int
438nsh_md2_ioam_trace_profile_setup (void)
439{
440  u32 trace_size = 0;
441  nsh_main_t *hm = &nsh_main;
442
443  trace_profile *profile = NULL;
444
445
446  profile = nsh_trace_profile_find ();
447
448  if (PREDICT_FALSE (!profile))
449    {
450      return (-1);
451    }
452
453
454  if (nsh_md2_ioam_trace_get_sizeof_handler (&trace_size) < 0)
455    return (-1);
456
457  hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = trace_size;
458
459  return (0);
460}
461
462
463
464/*
465 * fd.io coding-style-patch-verification: ON
466 *
467 * Local Variables:
468 * eval: (c-set-style "gnu")
469 * End:
470 */
471
472