misc: remove GNU Indent directives
[vpp.git] / src / plugins / ioam / lib-vxlan-gpe / vxlan_gpe_ioam_trace.c
1 /*
2  * Copyright (c) 2016 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 <vnet/vxlan-gpe/vxlan_gpe.h>
20 #include <vnet/vxlan-gpe/vxlan_gpe_packet.h>
21
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
25
26 #include <ioam/lib-trace/trace_util.h>
27 #include <ioam/lib-trace/trace_config.h>
28 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
29
30 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
31  * and nanoseconds respectively.
32  */
33 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
34
35 typedef union
36 {
37   u64 as_u64;
38   u32 as_u32[2];
39 } time_u64_t;
40
41
42 typedef CLIB_PACKED(struct {
43   vxlan_gpe_ioam_option_t hdr;
44   u8 ioam_trace_type;
45   u8 data_list_elts_left;
46   u32 elts[0]; /* Variable type. So keep it generic */
47 }) vxlan_gpe_ioam_trace_option_t;
48
49
50 #define foreach_vxlan_gpe_ioam_trace_stats                              \
51   _(SUCCESS, "Pkts updated with TRACE records")                                 \
52   _(FAILED, "Errors in TRACE due to lack of TRACE records")
53
54 static char *vxlan_gpe_ioam_trace_stats_strings[] = {
55 #define _(sym,string) string,
56   foreach_vxlan_gpe_ioam_trace_stats
57 #undef _
58 };
59
60 typedef enum
61 {
62 #define _(sym,str) VXLAN_GPE_IOAM_TRACE_##sym,
63   foreach_vxlan_gpe_ioam_trace_stats
64 #undef _
65     VXLAN_GPE_IOAM_TRACE_N_STATS,
66 } vxlan_gpe_ioam_trace_stats_t;
67
68
69 typedef struct
70 {
71   /* stats */
72   u64 counters[ARRAY_LEN (vxlan_gpe_ioam_trace_stats_strings)];
73
74   /* convenience */
75   vlib_main_t *vlib_main;
76   vnet_main_t *vnet_main;
77 } vxlan_gpe_ioam_trace_main_t;
78
79 vxlan_gpe_ioam_trace_main_t vxlan_gpe_ioam_trace_main;
80
81 int
82 vxlan_gpe_ioam_add_register_option (u8 option,
83                                     u8 size,
84                                     int rewrite_options (u8 * rewrite_string,
85                                                          u8 * rewrite_size))
86 {
87   vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
88
89   ASSERT ((u32) option < ARRAY_LEN (hm->add_options));
90
91   /* Already registered */
92   if (hm->add_options[option])
93     return (-1);
94
95   hm->add_options[option] = rewrite_options;
96   hm->options_size[option] = size;
97
98   return (0);
99 }
100
101 int
102 vxlan_gpe_add_unregister_option (u8 option)
103 {
104   vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
105
106   ASSERT ((u32) option < ARRAY_LEN (hm->add_options));
107
108   /* Not registered */
109   if (!hm->add_options[option])
110     return (-1);
111
112   hm->add_options[option] = NULL;
113   hm->options_size[option] = 0;
114   return (0);
115 }
116
117
118 int
119 vxlan_gpe_ioam_register_option (u8 option,
120                                 int options (vlib_buffer_t * b,
121                                              vxlan_gpe_ioam_option_t * opt,
122                                              u8 is_ipv4, u8 use_adj),
123                                 u8 * trace (u8 * s,
124                                             vxlan_gpe_ioam_option_t * opt))
125 {
126   vxlan_gpe_ioam_main_t *im = &vxlan_gpe_ioam_main;
127
128   ASSERT ((u32) option < ARRAY_LEN (im->options));
129
130   /* Already registered */
131   if (im->options[option])
132     return (-1);
133
134   im->options[option] = options;
135   im->trace[option] = trace;
136
137   return (0);
138 }
139
140 int
141 vxlan_gpe_ioam_unregister_option (u8 option)
142 {
143   vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
144
145   ASSERT ((u32) option < ARRAY_LEN (hm->options));
146
147   /* Not registered */
148   if (!hm->options[option])
149     return (-1);
150
151   hm->options[option] = NULL;
152   hm->trace[option] = NULL;
153
154   return (0);
155 }
156
157
158 always_inline void
159 vxlan_gpe_ioam_trace_stats_increment_counter (u32 counter_index,
160                                               u64 increment)
161 {
162   vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
163
164   hm->counters[counter_index] += increment;
165 }
166
167
168 static u8 *
169 format_ioam_data_list_element (u8 * s, va_list * args)
170 {
171   u32 *elt = va_arg (*args, u32 *);
172   u8 *trace_type_p = va_arg (*args, u8 *);
173   u8 trace_type = *trace_type_p;
174
175
176   if (trace_type & BIT_TTL_NODEID)
177     {
178       u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
179       s = format (s, "ttl 0x%x node id 0x%x ",
180                   ttl_node_id_host_byte_order >> 24,
181                   ttl_node_id_host_byte_order & 0x00FFFFFF);
182
183       elt++;
184     }
185
186   if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
187     {
188       u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
189       s = format (s, "ingress 0x%x egress 0x%x ",
190                   ingress_host_byte_order >> 16,
191                   ingress_host_byte_order & 0xFFFF);
192       elt++;
193     }
194
195   if (trace_type & BIT_TIMESTAMP)
196     {
197       u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
198       s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
199       elt++;
200     }
201
202   if (trace_type & BIT_APPDATA)
203     {
204       u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
205       s = format (s, "app 0x%x ", appdata_in_host_byte_order);
206       elt++;
207     }
208
209   return s;
210 }
211
212
213
214 int
215 vxlan_gpe_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
216 {
217   vxlan_gpe_ioam_trace_option_t *trace_option = NULL;
218   u8 trace_data_size = 0;
219   u8 trace_option_elts = 0;
220   trace_profile *profile = NULL;
221
222
223   profile = trace_profile_find ();
224
225   if (PREDICT_FALSE (!profile))
226     {
227       return (-1);
228     }
229
230   if (PREDICT_FALSE (!rewrite_string))
231     return -1;
232
233   trace_option_elts = profile->num_elts;
234   trace_data_size = fetch_trace_data_size (profile->trace_type);
235   trace_option = (vxlan_gpe_ioam_trace_option_t *) rewrite_string;
236   trace_option->hdr.type = VXLAN_GPE_OPTION_TYPE_IOAM_TRACE;
237   trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */  +
238     trace_option_elts * trace_data_size;
239   trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
240   trace_option->data_list_elts_left = trace_option_elts;
241   *rewrite_size =
242     sizeof (vxlan_gpe_ioam_trace_option_t) +
243     (trace_option_elts * trace_data_size);
244
245   return 0;
246 }
247
248
249 int
250 vxlan_gpe_ioam_trace_data_list_handler (vlib_buffer_t * b,
251                                         vxlan_gpe_ioam_option_t * opt,
252                                         u8 is_ipv4, u8 use_adj)
253 {
254   u8 elt_index = 0;
255   vxlan_gpe_ioam_trace_option_t *trace =
256     (vxlan_gpe_ioam_trace_option_t *) opt;
257   time_u64_t time_u64;
258   u32 *elt;
259   int rv = 0;
260   trace_profile *profile = NULL;
261   vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
262
263
264   profile = trace_profile_find ();
265
266   if (PREDICT_FALSE (!profile))
267     {
268       return (-1);
269     }
270
271
272   time_u64.as_u64 = 0;
273
274   if (PREDICT_TRUE (trace->data_list_elts_left))
275     {
276       trace->data_list_elts_left--;
277       /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
278        * to skip to this node's location.
279        */
280       elt_index =
281         trace->data_list_elts_left *
282         fetch_trace_data_size (trace->ioam_trace_type) / 4;
283       elt = &trace->elts[elt_index];
284       if (is_ipv4)
285         {
286           if (trace->ioam_trace_type & BIT_TTL_NODEID)
287             {
288               ip4_header_t *ip0 = vlib_buffer_get_current (b);
289               /* The transit case is the only case where the TTL decrement happens
290                * before iOAM processing. For now, use the use_adj flag as an overload.
291                * We can probably use a separate flag instead of overloading the use_adj flag.
292                */
293               *elt = clib_host_to_net_u32 (((ip0->ttl - 1 + use_adj) << 24) |
294                                            profile->node_id);
295               elt++;
296             }
297
298           if (trace->ioam_trace_type & BIT_ING_INTERFACE)
299             {
300               u16 tx_if = 0;
301               u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
302
303               if (use_adj)
304                 {
305                   ip_adjacency_t *adj = adj_get (adj_index);
306                   tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
307                 }
308
309               *elt =
310                 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
311                 tx_if;
312               *elt = clib_host_to_net_u32 (*elt);
313               elt++;
314             }
315         }
316       else
317         {
318           if (trace->ioam_trace_type & BIT_TTL_NODEID)
319             {
320               ip6_header_t *ip0 = vlib_buffer_get_current (b);
321               *elt = clib_host_to_net_u32 ((ip0->hop_limit << 24) |
322                                            profile->node_id);
323               elt++;
324             }
325           if (trace->ioam_trace_type & BIT_ING_INTERFACE)
326             {
327               u16 tx_if = 0;
328               u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
329
330               if (use_adj)
331                 {
332                   ip_adjacency_t *adj = adj_get (adj_index);
333                   tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
334                 }
335
336               *elt =
337                 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
338                 tx_if;
339               *elt = clib_host_to_net_u32 (*elt);
340               elt++;
341             }
342         }
343
344       if (trace->ioam_trace_type & BIT_TIMESTAMP)
345         {
346           /* Send least significant 32 bits */
347           f64 time_f64 =
348             (f64) (((f64) hm->unix_time_0) +
349                    (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
350
351           time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
352           *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
353           elt++;
354         }
355
356       if (trace->ioam_trace_type & BIT_APPDATA)
357         {
358           /* $$$ set elt0->app_data */
359           *elt = clib_host_to_net_u32 (profile->app_data);
360           elt++;
361         }
362       vxlan_gpe_ioam_trace_stats_increment_counter
363         (VXLAN_GPE_IOAM_TRACE_SUCCESS, 1);
364     }
365   else
366     {
367       vxlan_gpe_ioam_trace_stats_increment_counter
368         (VXLAN_GPE_IOAM_TRACE_FAILED, 1);
369     }
370   return (rv);
371 }
372
373 u8 *
374 vxlan_gpe_ioam_trace_data_list_trace_handler (u8 * s,
375                                               vxlan_gpe_ioam_option_t * opt)
376 {
377   vxlan_gpe_ioam_trace_option_t *trace;
378   u8 trace_data_size_in_words = 0;
379   u32 *elt;
380   int elt_index = 0;
381
382   trace = (vxlan_gpe_ioam_trace_option_t *) opt;
383   s =
384     format (s, "  Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
385             trace->data_list_elts_left);
386   trace_data_size_in_words =
387     fetch_trace_data_size (trace->ioam_trace_type) / 4;
388   elt = &trace->elts[0];
389   while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
390                        /* -2 accounts for ioam_trace_type,elts_left */ ))
391     {
392       s = format (s, "    [%d] %U\n", elt_index,
393                   format_ioam_data_list_element,
394                   elt, &trace->ioam_trace_type);
395       elt_index++;
396       elt += trace_data_size_in_words;
397     }
398   return (s);
399 }
400
401
402 static clib_error_t *
403 vxlan_gpe_show_ioam_trace_cmd_fn (vlib_main_t * vm,
404                                   unformat_input_t * input,
405                                   vlib_cli_command_t * cmd)
406 {
407   vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
408   u8 *s = 0;
409   int i = 0;
410
411   for (i = 0; i < VXLAN_GPE_IOAM_TRACE_N_STATS; i++)
412     {
413       s = format (s, " %s - %lu\n", vxlan_gpe_ioam_trace_stats_strings[i],
414                   hm->counters[i]);
415     }
416
417   vlib_cli_output (vm, "%v", s);
418   vec_free (s);
419   return 0;
420 }
421
422
423 VLIB_CLI_COMMAND (vxlan_gpe_show_ioam_trace_cmd, static) = {
424   .path = "show ioam vxlan-gpe trace",
425   .short_help = "iOAM trace statistics",
426   .function = vxlan_gpe_show_ioam_trace_cmd_fn,
427 };
428
429
430 static clib_error_t *
431 vxlan_gpe_ioam_trace_init (vlib_main_t * vm)
432 {
433   vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
434
435   hm->vlib_main = vm;
436   hm->vnet_main = vnet_get_main ();
437   clib_memset (hm->counters, 0, sizeof (hm->counters));
438
439   if (vxlan_gpe_ioam_register_option
440       (VXLAN_GPE_OPTION_TYPE_IOAM_TRACE,
441        vxlan_gpe_ioam_trace_data_list_handler,
442        vxlan_gpe_ioam_trace_data_list_trace_handler) < 0)
443     return (clib_error_create
444             ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE failed"));
445
446
447   if (vxlan_gpe_ioam_add_register_option
448       (VXLAN_GPE_OPTION_TYPE_IOAM_TRACE,
449        sizeof (vxlan_gpe_ioam_trace_option_t),
450        vxlan_gpe_ioam_trace_rewrite_handler) < 0)
451     return (clib_error_create
452             ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE for rewrite failed"));
453
454
455   return (0);
456 }
457
458 VLIB_INIT_FUNCTION (vxlan_gpe_ioam_trace_init) =
459 {
460   .runs_after = VLIB_INITS("ip_main_init", "ip6_lookup_init",
461                            "vxlan_gpe_init"),
462 };
463
464
465 int
466 vxlan_gpe_trace_profile_cleanup (void)
467 {
468   vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
469
470   hm->options_size[VXLAN_GPE_OPTION_TYPE_IOAM_TRACE] = 0;
471
472   return 0;
473
474 }
475
476 static int
477 vxlan_gpe_ioam_trace_get_sizeof_handler (u32 * result)
478 {
479   u16 size = 0;
480   u8 trace_data_size = 0;
481   trace_profile *profile = NULL;
482
483   *result = 0;
484
485   profile = trace_profile_find ();
486
487   if (PREDICT_FALSE (!profile))
488     {
489       return (-1);
490     }
491
492   trace_data_size = fetch_trace_data_size (profile->trace_type);
493   if (PREDICT_FALSE (trace_data_size == 0))
494     return VNET_API_ERROR_INVALID_VALUE;
495
496   if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
497     return VNET_API_ERROR_INVALID_VALUE;
498
499   size +=
500     sizeof (vxlan_gpe_ioam_trace_option_t) +
501     profile->num_elts * trace_data_size;
502   *result = size;
503
504   return 0;
505 }
506
507
508 int
509 vxlan_gpe_trace_profile_setup (void)
510 {
511   u32 trace_size = 0;
512   vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
513
514   trace_profile *profile = NULL;
515
516
517   profile = trace_profile_find ();
518
519   if (PREDICT_FALSE (!profile))
520     {
521       return (-1);
522     }
523
524
525   if (vxlan_gpe_ioam_trace_get_sizeof_handler (&trace_size) < 0)
526     return (-1);
527
528   hm->options_size[VXLAN_GPE_OPTION_TYPE_IOAM_TRACE] = trace_size;
529
530   return (0);
531 }
532
533
534
535 /*
536  * fd.io coding-style-patch-verification: ON
537  *
538  * Local Variables:
539  * eval: (c-set-style "gnu")
540  * End:
541  */