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