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