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