3a6758cd859ae64bfa916aa0f45207aa8032bcaf
[vpp.git] / src / plugins / ioam / encap / ip6_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/ip/ip6.h>
21 #include <vnet/ip/ip6_hop_by_hop.h>
22 #include <vnet/ip/ip6_hop_by_hop_packet.h>
23
24 #include <vppinfra/hash.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/elog.h>
27 #include <vnet/plugin/plugin.h>
28
29 #include <ioam/lib-trace/trace_util.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 /* *INDENT-OFF* */
43 typedef CLIB_PACKED(struct {
44   ip6_hop_by_hop_option_t hdr;
45   u8 ioam_trace_type;
46   u8 data_list_elts_left;
47   u32 elts[0]; /* Variable type. So keep it generic */
48 }) ioam_trace_option_t;
49 /* *INDENT-ON* */
50
51
52 extern ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
53 extern ip6_main_t ip6_main;
54
55 #define foreach_ip6_hop_by_hop_ioam_trace_stats                                \
56   _(PROCESSED, "Pkts with ip6 hop-by-hop trace options")                        \
57   _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
58   _(UPDATED, "Pkts with trace updated")                                        \
59   _(FULL, "Pkts with trace options but no space")
60
61 static char *ip6_hop_by_hop_ioam_trace_stats_strings[] = {
62 #define _(sym,string) string,
63   foreach_ip6_hop_by_hop_ioam_trace_stats
64 #undef _
65 };
66
67 typedef enum
68 {
69 #define _(sym,str) IP6_IOAM_TRACE_##sym,
70   foreach_ip6_hop_by_hop_ioam_trace_stats
71 #undef _
72     IP6_IOAM_TRACE_N_STATS,
73 } ip6_ioam_trace_stats_t;
74
75
76 typedef struct
77 {
78   /* stats */
79   u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
80
81   /* convenience */
82   vlib_main_t *vlib_main;
83   vnet_main_t *vnet_main;
84 } ip6_hop_by_hop_ioam_trace_main_t;
85
86 ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main;
87
88 always_inline void
89 ip6_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
90 {
91   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
92
93   hm->counters[counter_index] += increment;
94 }
95
96
97 static u8 *
98 format_ioam_data_list_element (u8 * s, va_list * args)
99 {
100   u32 *elt = va_arg (*args, u32 *);
101   u8 *trace_type_p = va_arg (*args, u8 *);
102   u8 trace_type = *trace_type_p;
103
104
105   if (trace_type & BIT_TTL_NODEID)
106     {
107       u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
108       s = format (s, "ttl 0x%x node id 0x%x ",
109                   ttl_node_id_host_byte_order >> 24,
110                   ttl_node_id_host_byte_order & 0x00FFFFFF);
111
112       elt++;
113     }
114
115   if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
116     {
117       u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
118       s = format (s, "ingress 0x%x egress 0x%x ",
119                   ingress_host_byte_order >> 16,
120                   ingress_host_byte_order & 0xFFFF);
121       elt++;
122     }
123
124   if (trace_type & BIT_TIMESTAMP)
125     {
126       u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
127       s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
128       elt++;
129     }
130
131   if (trace_type & BIT_APPDATA)
132     {
133       u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
134       s = format (s, "app 0x%x ", appdata_in_host_byte_order);
135       elt++;
136     }
137
138   return s;
139 }
140
141
142 int
143 ip6_ioam_trace_get_sizeof_handler (u32 * result)
144 {
145   u16 size = 0;
146   u8 trace_data_size = 0;
147   trace_profile *profile = NULL;
148
149   *result = 0;
150
151   profile = trace_profile_find ();
152
153   if (PREDICT_FALSE (!profile))
154     {
155       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
156       return (-1);
157     }
158
159   trace_data_size = fetch_trace_data_size (profile->trace_type);
160   if (PREDICT_FALSE (trace_data_size == 0))
161     return VNET_API_ERROR_INVALID_VALUE;
162
163   if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
164     return VNET_API_ERROR_INVALID_VALUE;
165
166   size +=
167     sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
168   *result = size;
169
170   return 0;
171 }
172
173
174
175 int
176 ip6_hop_by_hop_ioam_trace_rewrite_handler (u8 * rewrite_string,
177                                            u8 * rewrite_size)
178 {
179   ioam_trace_option_t *trace_option = NULL;
180   u8 trace_data_size = 0;
181   u8 trace_option_elts = 0;
182   trace_profile *profile = NULL;
183
184
185   profile = trace_profile_find ();
186
187   if (PREDICT_FALSE (!profile))
188     {
189       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
190       return (-1);
191     }
192
193   if (PREDICT_FALSE (!rewrite_string))
194     return -1;
195
196   trace_option_elts = profile->num_elts;
197   trace_data_size = fetch_trace_data_size (profile->trace_type);
198   trace_option = (ioam_trace_option_t *) rewrite_string;
199   trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
200     HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
201   trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */  +
202     trace_option_elts * trace_data_size;
203   trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
204   trace_option->data_list_elts_left = trace_option_elts;
205   *rewrite_size =
206     sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
207
208   return 0;
209 }
210
211
212 int
213 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
214                                       ip6_hop_by_hop_option_t * opt)
215 {
216   ip6_main_t *im = &ip6_main;
217   ip_lookup_main_t *lm = &im->lookup_main;
218   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
219   u8 elt_index = 0;
220   ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
221   u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
222   ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
223   time_u64_t time_u64;
224   u32 *elt;
225   int rv = 0;
226   trace_profile *profile = NULL;
227
228
229   profile = trace_profile_find ();
230
231   if (PREDICT_FALSE (!profile))
232     {
233       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
234       return (-1);
235     }
236
237
238   time_u64.as_u64 = 0;
239
240   if (PREDICT_TRUE (trace->data_list_elts_left))
241     {
242       trace->data_list_elts_left--;
243       /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
244        * to skip to this node's location.
245        */
246       elt_index =
247         trace->data_list_elts_left *
248         fetch_trace_data_size (trace->ioam_trace_type) / 4;
249       elt = &trace->elts[elt_index];
250       if (trace->ioam_trace_type & BIT_TTL_NODEID)
251         {
252           *elt =
253             clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
254           elt++;
255         }
256
257       if (trace->ioam_trace_type & BIT_ING_INTERFACE)
258         {
259           *elt =
260             (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
261             (adj->rewrite_header.sw_if_index & 0xFFFF);
262           *elt = clib_host_to_net_u32 (*elt);
263           elt++;
264         }
265
266       if (trace->ioam_trace_type & BIT_TIMESTAMP)
267         {
268           /* Send least significant 32 bits */
269           f64 time_f64 =
270             (f64) (((f64) hm->unix_time_0) +
271                    (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
272
273           time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
274           *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
275           elt++;
276         }
277
278       if (trace->ioam_trace_type & BIT_APPDATA)
279         {
280           /* $$$ set elt0->app_data */
281           *elt = clib_host_to_net_u32 (profile->app_data);
282           elt++;
283         }
284       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
285     }
286   else
287     {
288       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
289     }
290   return (rv);
291 }
292
293 u8 *
294 ip6_hbh_ioam_trace_data_list_trace_handler (u8 * s,
295                                             ip6_hop_by_hop_option_t * opt)
296 {
297   ioam_trace_option_t *trace;
298   u8 trace_data_size_in_words = 0;
299   u32 *elt;
300   int elt_index = 0;
301
302   trace = (ioam_trace_option_t *) opt;
303   s =
304     format (s, "  Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
305             trace->data_list_elts_left);
306   trace_data_size_in_words =
307     fetch_trace_data_size (trace->ioam_trace_type) / 4;
308   elt = &trace->elts[0];
309   while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
310                        /* -2 accounts for ioam_trace_type,elts_left */ ))
311     {
312       s = format (s, "    [%d] %U\n", elt_index,
313                   format_ioam_data_list_element,
314                   elt, &trace->ioam_trace_type);
315       elt_index++;
316       elt += trace_data_size_in_words;
317     }
318   return (s);
319 }
320
321
322 static clib_error_t *
323 ip6_show_ioam_trace_cmd_fn (vlib_main_t * vm,
324                             unformat_input_t * input,
325                             vlib_cli_command_t * cmd)
326 {
327   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
328   u8 *s = 0;
329   int i = 0;
330
331   for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
332     {
333       s =
334         format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
335                 hm->counters[i]);
336     }
337
338   vlib_cli_output (vm, "%v", s);
339   vec_free (s);
340   return 0;
341 }
342
343
344 /* *INDENT-OFF* */
345 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
346   .path = "show ioam trace",
347   .short_help = "iOAM trace statistics",
348   .function = ip6_show_ioam_trace_cmd_fn,
349 };
350 /* *INDENT-ON* */
351
352 /*
353  * This routine exists to convince the vlib plugin framework that
354  * we haven't accidentally copied a random .dll into the plugin directory.
355  *
356  * Also collects global variable pointers passed from the vpp engine
357  */
358 clib_error_t *
359 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
360                       int from_early_init)
361 {
362   return 0;
363 }
364
365 static clib_error_t *
366 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
367 {
368   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
369   clib_error_t *error;
370
371   if ((error = vlib_call_init_function (vm, ip_main_init)))
372     return (error);
373
374   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
375     return error;
376
377   if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
378     return (error);
379
380   hm->vlib_main = vm;
381   hm->vnet_main = vnet_get_main ();
382   memset (hm->counters, 0, sizeof (hm->counters));
383
384
385   if (ip6_hbh_register_option
386       (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
387        ip6_hbh_ioam_trace_data_list_handler,
388        ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
389     return (clib_error_create
390             ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
391
392
393   if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
394                                    sizeof (ioam_trace_option_t),
395                                    ip6_hop_by_hop_ioam_trace_rewrite_handler)
396       < 0)
397     return (clib_error_create
398             ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
399
400
401   return (0);
402 }
403
404 int
405 ip6_trace_profile_cleanup (void)
406 {
407   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
408
409   hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
410
411   return 0;
412
413 }
414
415
416 int
417 ip6_trace_profile_setup (void)
418 {
419   u32 trace_size = 0;
420   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
421
422   trace_profile *profile = NULL;
423
424
425   profile = trace_profile_find ();
426
427   if (PREDICT_FALSE (!profile))
428     {
429       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
430       return (-1);
431     }
432
433
434   if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
435     return (-1);
436
437   hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
438
439   return (0);
440 }
441
442
443 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init);
444
445 /*
446  * fd.io coding-style-patch-verification: ON
447  *
448  * Local Variables:
449  * eval: (c-set-style "gnu")
450  * End:
451  */